blob: 6c887fae1c2ea452afb05dfb04054a9c246aa8cb [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
632 char_u *fname = NULL; // function or script name
633 linenr_T *breakpoint = NULL; // ptr to breakpoint field in cookie
634 int *dbg_tick = NULL; // ptr to dbg_tick field in cookie
635 struct dbg_stuff debug_saved; // saved things for debug mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000636 int initial_trylevel;
Bram Moolenaar25e0f582020-05-25 22:36:50 +0200637 msglist_T **saved_msg_list = NULL;
638 msglist_T *private_msg_list;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000639
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100640 // "fgetline" and "cookie" passed to do_one_cmd()
Bram Moolenaare96a2492019-06-25 04:12:16 +0200641 char_u *(*cmd_getline)(int, void *, int, int);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000642 void *cmd_cookie;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000643 struct loop_cookie cmd_loop_cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000644 void *real_cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +0000645 int getline_is_func;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000646#else
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100647# define cmd_getline fgetline
Bram Moolenaar071d4272004-06-13 20:20:40 +0000648# define cmd_cookie cookie
649#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100650 static int call_depth = 0; // recursiveness
Bram Moolenaar2196bce2020-04-12 20:01:11 +0200651#ifdef FEAT_EVAL
Bram Moolenaare31ee862020-01-07 20:59:34 +0100652 ESTACK_CHECK_DECLARATION
Bram Moolenaar071d4272004-06-13 20:20:40 +0000653
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100654 // For every pair of do_cmdline()/do_one_cmd() calls, use an extra memory
655 // location for storing error messages to be converted to an exception.
656 // This ensures that the do_errthrow() call in do_one_cmd() does not
657 // combine the messages stored by an earlier invocation of do_one_cmd()
658 // with the command name of the later one. This would happen when
659 // BufWritePost autocommands are executed after a write error.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000660 saved_msg_list = msg_list;
661 msg_list = &private_msg_list;
662 private_msg_list = NULL;
663#endif
664
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100665 // It's possible to create an endless loop with ":execute", catch that
666 // here. The value of 200 allows nested function calls, ":source", etc.
667 // Allow 200 or 'maxfuncdepth', whatever is larger.
Bram Moolenaarb094ff42017-01-02 16:16:39 +0100668 if (call_depth >= 200
669#ifdef FEAT_EVAL
670 && call_depth >= p_mfd
671#endif
672 )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000673 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100674 emsg(_("E169: Command too recursive"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000675#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100676 // When converting to an exception, we do not include the command name
677 // since this is not an error of the specific command.
Bram Moolenaarddef1292019-12-16 17:10:33 +0100678 do_errthrow((cstack_T *)NULL, (char_u *)NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000679 msg_list = saved_msg_list;
680#endif
681 return FAIL;
682 }
683 ++call_depth;
684
685#ifdef FEAT_EVAL
686 cstack.cs_idx = -1;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000687 cstack.cs_looplevel = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000688 cstack.cs_trylevel = 0;
689 cstack.cs_emsg_silent_list = NULL;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000690 cstack.cs_lflags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000691 ga_init2(&lines_ga, (int)sizeof(wcmd_T), 10);
692
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100693 real_cookie = getline_cookie(fgetline, cookie);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000694
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100695 // Inside a function use a higher nesting level.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100696 getline_is_func = getline_equal(fgetline, cookie, get_func_line);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000697 if (getline_is_func && ex_nesting_level == func_level(real_cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000698 ++ex_nesting_level;
699
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100700 // Get the function or script name and the address where the next breakpoint
701 // line and the debug tick for a function or script are stored.
Bram Moolenaar05159a02005-02-26 23:04:13 +0000702 if (getline_is_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000703 {
704 fname = func_name(real_cookie);
705 breakpoint = func_breakpoint(real_cookie);
706 dbg_tick = func_dbg_tick(real_cookie);
707 }
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100708 else if (getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000709 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100710 fname = SOURCING_NAME;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000711 breakpoint = source_breakpoint(real_cookie);
712 dbg_tick = source_dbg_tick(real_cookie);
713 }
714
715 /*
716 * Initialize "force_abort" and "suppress_errthrow" at the top level.
717 */
718 if (!recursive)
719 {
720 force_abort = FALSE;
721 suppress_errthrow = FALSE;
722 }
723
724 /*
725 * If requested, store and reset the global values controlling the
Bram Moolenaar89d40322006-08-29 15:30:07 +0000726 * exception handling (used when debugging). Otherwise clear it to avoid
727 * a bogus compiler warning when the optimizer uses inline functions...
Bram Moolenaar071d4272004-06-13 20:20:40 +0000728 */
Bram Moolenaarb4872942006-05-13 10:32:52 +0000729 if (flags & DOCMD_EXCRESET)
Bram Moolenaared203462004-06-16 11:19:22 +0000730 save_dbg_stuff(&debug_saved);
Bram Moolenaar89d40322006-08-29 15:30:07 +0000731 else
Bram Moolenaara80faa82020-04-12 19:37:17 +0200732 CLEAR_FIELD(debug_saved);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000733
734 initial_trylevel = trylevel;
735
736 /*
737 * "did_throw" will be set to TRUE when an exception is being thrown.
738 */
739 did_throw = FALSE;
740#endif
741 /*
742 * "did_emsg" will be set to TRUE when emsg() is used, in which case we
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000743 * cancel the whole command line, and any if/endif or loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000744 * If force_abort is set, we cancel everything.
745 */
746 did_emsg = FALSE;
747
748 /*
749 * KeyTyped is only set when calling vgetc(). Reset it here when not
750 * calling vgetc() (sourced command lines).
751 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100752 if (!(flags & DOCMD_KEYTYPED)
753 && !getline_equal(fgetline, cookie, getexline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000754 KeyTyped = FALSE;
755
756 /*
757 * Continue executing command lines:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000758 * - when inside an ":if", ":while" or ":for"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000759 * - for multiple commands on one line, separated with '|'
760 * - when repeating until there are no more lines (for ":source")
761 */
762 next_cmdline = cmdline;
763 do
764 {
Bram Moolenaar05159a02005-02-26 23:04:13 +0000765#ifdef FEAT_EVAL
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100766 getline_is_func = getline_equal(fgetline, cookie, get_func_line);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000767#endif
768
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100769 // stop skipping cmds for an error msg after all endif/while/for
Bram Moolenaar071d4272004-06-13 20:20:40 +0000770 if (next_cmdline == NULL
771#ifdef FEAT_EVAL
772 && !force_abort
773 && cstack.cs_idx < 0
Bram Moolenaar05159a02005-02-26 23:04:13 +0000774 && !(getline_is_func && func_has_abort(real_cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000775#endif
776 )
777 did_emsg = FALSE;
778
779 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000780 * 1. If repeating a line in a loop, get a line from lines_ga.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100781 * 2. If no line given: Get an allocated line with fgetline().
Bram Moolenaar071d4272004-06-13 20:20:40 +0000782 * 3. If a line is given: Make a copy, so we can mess with it.
783 */
784
785#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100786 // 1. If repeating, get a previous line from lines_ga.
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000787 if (cstack.cs_looplevel > 0 && current_line < lines_ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000788 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100789 // Each '|' separated command is stored separately in lines_ga, to
790 // be able to jump to it. Don't use next_cmdline now.
Bram Moolenaard23a8232018-02-10 18:45:26 +0100791 VIM_CLEAR(cmdline_copy);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000792
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100793 // Check if a function has returned or, unless it has an unclosed
794 // try conditional, aborted.
Bram Moolenaar05159a02005-02-26 23:04:13 +0000795 if (getline_is_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000796 {
Bram Moolenaar05159a02005-02-26 23:04:13 +0000797# ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000798 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +0000799 func_line_end(real_cookie);
800# endif
801 if (func_has_ended(real_cookie))
802 {
803 retval = FAIL;
804 break;
805 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000806 }
Bram Moolenaar05159a02005-02-26 23:04:13 +0000807#ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000808 else if (do_profiling == PROF_YES
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100809 && getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar05159a02005-02-26 23:04:13 +0000810 script_line_end();
811#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000812
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100813 // Check if a sourced file hit a ":finish" command.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100814 if (source_finished(fgetline, cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000815 {
816 retval = FAIL;
817 break;
818 }
819
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100820 // If breakpoints have been added/deleted need to check for it.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000821 if (breakpoint != NULL && dbg_tick != NULL
822 && *dbg_tick != debug_tick)
823 {
824 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100825 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100826 fname, SOURCING_LNUM);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000827 *dbg_tick = debug_tick;
828 }
829
830 next_cmdline = ((wcmd_T *)(lines_ga.ga_data))[current_line].line;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100831 SOURCING_LNUM = ((wcmd_T *)(lines_ga.ga_data))[current_line].lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000832
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100833 // Did we encounter a breakpoint?
Bram Moolenaar071d4272004-06-13 20:20:40 +0000834 if (breakpoint != NULL && *breakpoint != 0
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100835 && *breakpoint <= SOURCING_LNUM)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000836 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100837 dbg_breakpoint(fname, SOURCING_LNUM);
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100838 // Find next breakpoint.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000839 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100840 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100841 fname, SOURCING_LNUM);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000842 *dbg_tick = debug_tick;
843 }
Bram Moolenaar05159a02005-02-26 23:04:13 +0000844# ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000845 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +0000846 {
847 if (getline_is_func)
848 func_line_start(real_cookie);
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100849 else if (getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar05159a02005-02-26 23:04:13 +0000850 script_line_start();
851 }
852# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000853 }
854
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000855 if (cstack.cs_looplevel > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000856 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100857 // Inside a while/for loop we need to store the lines and use them
858 // again. Pass a different "fgetline" function to do_one_cmd()
859 // below, so that it stores lines in or reads them from
860 // "lines_ga". Makes it possible to define a function inside a
861 // while/for loop.
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000862 cmd_getline = get_loop_line;
863 cmd_cookie = (void *)&cmd_loop_cookie;
864 cmd_loop_cookie.lines_gap = &lines_ga;
865 cmd_loop_cookie.current_line = current_line;
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100866 cmd_loop_cookie.getline = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000867 cmd_loop_cookie.cookie = cookie;
868 cmd_loop_cookie.repeating = (current_line < lines_ga.ga_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000869 }
870 else
871 {
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100872 cmd_getline = fgetline;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000873 cmd_cookie = cookie;
874 }
875#endif
876
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100877 // 2. If no line given, get an allocated line with fgetline().
Bram Moolenaar071d4272004-06-13 20:20:40 +0000878 if (next_cmdline == NULL)
879 {
880 /*
881 * Need to set msg_didout for the first line after an ":if",
882 * otherwise the ":if" will be overwritten.
883 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100884 if (count == 1 && getline_equal(fgetline, cookie, getexline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000885 msg_didout = TRUE;
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100886 if (fgetline == NULL || (next_cmdline = fgetline(':', cookie,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000887#ifdef FEAT_EVAL
888 cstack.cs_idx < 0 ? 0 : (cstack.cs_idx + 1) * 2
889#else
890 0
891#endif
Bram Moolenaare96a2492019-06-25 04:12:16 +0200892 , TRUE)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000893 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100894 // Don't call wait_return for aborted command line. The NULL
895 // returned for the end of a sourced file or executed function
896 // doesn't do this.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000897 if (KeyTyped && !(flags & DOCMD_REPEAT))
898 need_wait_return = FALSE;
899 retval = FAIL;
900 break;
901 }
902 used_getline = TRUE;
903
904 /*
905 * Keep the first typed line. Clear it when more lines are typed.
906 */
907 if (flags & DOCMD_KEEPLINE)
908 {
909 vim_free(repeat_cmdline);
910 if (count == 0)
911 repeat_cmdline = vim_strsave(next_cmdline);
912 else
913 repeat_cmdline = NULL;
914 }
915 }
916
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100917 // 3. Make a copy of the command so we can mess with it.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000918 else if (cmdline_copy == NULL)
919 {
920 next_cmdline = vim_strsave(next_cmdline);
921 if (next_cmdline == NULL)
922 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100923 emsg(_(e_outofmem));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000924 retval = FAIL;
925 break;
926 }
927 }
928 cmdline_copy = next_cmdline;
929
930#ifdef FEAT_EVAL
931 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000932 * Save the current line when inside a ":while" or ":for", and when
933 * the command looks like a ":while" or ":for", because we may need it
934 * later. When there is a '|' and another command, it is stored
935 * separately, because we need to be able to jump back to it from an
936 * :endwhile/:endfor.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000937 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000938 if (current_line == lines_ga.ga_len
939 && (cstack.cs_looplevel || has_loop_cmd(next_cmdline)))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000940 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000941 if (store_loop_line(&lines_ga, next_cmdline) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000942 {
943 retval = FAIL;
944 break;
945 }
946 }
947 did_endif = FALSE;
948#endif
949
950 if (count++ == 0)
951 {
952 /*
953 * All output from the commands is put below each other, without
954 * waiting for a return. Don't do this when executing commands
955 * from a script or when being called recursive (e.g. for ":e
956 * +command file").
957 */
958 if (!(flags & DOCMD_NOWAIT) && !recursive)
959 {
960 msg_didout_before_start = msg_didout;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100961 msg_didany = FALSE; // no output yet
Bram Moolenaar071d4272004-06-13 20:20:40 +0000962 msg_start();
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100963 msg_scroll = TRUE; // put messages below each other
964 ++no_wait_return; // don't wait for return until finished
Bram Moolenaar071d4272004-06-13 20:20:40 +0000965 ++RedrawingDisabled;
966 did_inc = TRUE;
967 }
968 }
969
Bram Moolenaar823654b2020-05-29 23:03:09 +0200970 if ((p_verbose >= 15 && SOURCING_NAME != NULL) || p_verbose >= 16)
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100971 msg_verbose_cmd(SOURCING_LNUM, cmdline_copy);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000972
973 /*
974 * 2. Execute one '|' separated command.
975 * do_one_cmd() will return NULL if there is no trailing '|'.
976 * "cmdline_copy" can change, e.g. for '%' and '#' expansion.
977 */
978 ++recursive;
979 next_cmdline = do_one_cmd(&cmdline_copy, flags & DOCMD_VERBOSE,
980#ifdef FEAT_EVAL
981 &cstack,
982#endif
983 cmd_getline, cmd_cookie);
984 --recursive;
985
986#ifdef FEAT_EVAL
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000987 if (cmd_cookie == (void *)&cmd_loop_cookie)
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100988 // Use "current_line" from "cmd_loop_cookie", it may have been
989 // incremented when defining a function.
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000990 current_line = cmd_loop_cookie.current_line;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000991#endif
992
993 if (next_cmdline == NULL)
994 {
Bram Moolenaard23a8232018-02-10 18:45:26 +0100995 VIM_CLEAR(cmdline_copy);
Bram Moolenaard7663c22019-08-06 21:59:57 +0200996
Bram Moolenaar071d4272004-06-13 20:20:40 +0000997 /*
998 * If the command was typed, remember it for the ':' register.
999 * Do this AFTER executing the command to make :@: work.
1000 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001001 if (getline_equal(fgetline, cookie, getexline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001002 && new_last_cmdline != NULL)
1003 {
1004 vim_free(last_cmdline);
1005 last_cmdline = new_last_cmdline;
1006 new_last_cmdline = NULL;
1007 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001008 }
1009 else
1010 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001011 // need to copy the command after the '|' to cmdline_copy, for the
1012 // next do_one_cmd()
Bram Moolenaara7241f52008-06-24 20:39:31 +00001013 STRMOVE(cmdline_copy, next_cmdline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001014 next_cmdline = cmdline_copy;
1015 }
1016
1017
1018#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001019 // reset did_emsg for a function that is not aborted by an error
Bram Moolenaar071d4272004-06-13 20:20:40 +00001020 if (did_emsg && !force_abort
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001021 && getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001022 && !func_has_abort(real_cookie))
1023 did_emsg = FALSE;
1024
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001025 if (cstack.cs_looplevel > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001026 {
1027 ++current_line;
1028
1029 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001030 * An ":endwhile", ":endfor" and ":continue" is handled here.
1031 * If we were executing commands, jump back to the ":while" or
1032 * ":for".
1033 * If we were not executing commands, decrement cs_looplevel.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001034 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001035 if (cstack.cs_lflags & (CSL_HAD_CONT | CSL_HAD_ENDLOOP))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001036 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001037 cstack.cs_lflags &= ~(CSL_HAD_CONT | CSL_HAD_ENDLOOP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001038
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001039 // Jump back to the matching ":while" or ":for". Be careful
1040 // not to use a cs_line[] from an entry that isn't a ":while"
1041 // or ":for": It would make "current_line" invalid and can
1042 // cause a crash.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001043 if (!did_emsg && !got_int && !did_throw
1044 && cstack.cs_idx >= 0
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001045 && (cstack.cs_flags[cstack.cs_idx]
1046 & (CSF_WHILE | CSF_FOR))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001047 && cstack.cs_line[cstack.cs_idx] >= 0
1048 && (cstack.cs_flags[cstack.cs_idx] & CSF_ACTIVE))
1049 {
1050 current_line = cstack.cs_line[cstack.cs_idx];
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001051 // remember we jumped there
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001052 cstack.cs_lflags |= CSL_HAD_LOOP;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001053 line_breakcheck(); // check if CTRL-C typed
Bram Moolenaar071d4272004-06-13 20:20:40 +00001054
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001055 // Check for the next breakpoint at or after the ":while"
1056 // or ":for".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001057 if (breakpoint != NULL)
1058 {
1059 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001060 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001061 fname,
1062 ((wcmd_T *)lines_ga.ga_data)[current_line].lnum-1);
1063 *dbg_tick = debug_tick;
1064 }
1065 }
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001066 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001067 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001068 // can only get here with ":endwhile" or ":endfor"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001069 if (cstack.cs_idx >= 0)
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001070 rewind_conditionals(&cstack, cstack.cs_idx - 1,
1071 CSF_WHILE | CSF_FOR, &cstack.cs_looplevel);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001072 }
1073 }
1074
1075 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001076 * For a ":while" or ":for" we need to remember the line number.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001077 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001078 else if (cstack.cs_lflags & CSL_HAD_LOOP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001079 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001080 cstack.cs_lflags &= ~CSL_HAD_LOOP;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001081 cstack.cs_line[cstack.cs_idx] = current_line - 1;
1082 }
1083 }
1084
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001085 // Check for the next breakpoint after a watchexpression
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01001086 if (breakpoint != NULL && has_watchexpr())
1087 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001088 *breakpoint = dbg_find_breakpoint(FALSE, fname, SOURCING_LNUM);
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01001089 *dbg_tick = debug_tick;
1090 }
1091
Bram Moolenaar071d4272004-06-13 20:20:40 +00001092 /*
1093 * When not inside any ":while" loop, clear remembered lines.
1094 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001095 if (cstack.cs_looplevel == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001096 {
1097 if (lines_ga.ga_len > 0)
1098 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001099 SOURCING_LNUM =
Bram Moolenaar071d4272004-06-13 20:20:40 +00001100 ((wcmd_T *)lines_ga.ga_data)[lines_ga.ga_len - 1].lnum;
1101 free_cmdlines(&lines_ga);
1102 }
1103 current_line = 0;
1104 }
1105
1106 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001107 * A ":finally" makes did_emsg, got_int, and did_throw pending for
1108 * being restored at the ":endtry". Reset them here and set the
1109 * ACTIVE and FINALLY flags, so that the finally clause gets executed.
1110 * This includes the case where a missing ":endif", ":endwhile" or
1111 * ":endfor" was detected by the ":finally" itself.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001112 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001113 if (cstack.cs_lflags & CSL_HAD_FINA)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001114 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001115 cstack.cs_lflags &= ~CSL_HAD_FINA;
1116 report_make_pending(cstack.cs_pending[cstack.cs_idx]
1117 & (CSTP_ERROR | CSTP_INTERRUPT | CSTP_THROW),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001118 did_throw ? (void *)current_exception : NULL);
1119 did_emsg = got_int = did_throw = FALSE;
1120 cstack.cs_flags[cstack.cs_idx] |= CSF_ACTIVE | CSF_FINALLY;
1121 }
1122
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001123 // Update global "trylevel" for recursive calls to do_cmdline() from
1124 // within this loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001125 trylevel = initial_trylevel + cstack.cs_trylevel;
1126
1127 /*
Bram Moolenaarc1762cc2007-05-10 16:56:30 +00001128 * If the outermost try conditional (across function calls and sourced
Bram Moolenaar071d4272004-06-13 20:20:40 +00001129 * files) is aborted because of an error, an interrupt, or an uncaught
1130 * exception, cancel everything. If it is left normally, reset
1131 * force_abort to get the non-EH compatible abortion behavior for
1132 * the rest of the script.
1133 */
1134 if (trylevel == 0 && !did_emsg && !got_int && !did_throw)
1135 force_abort = FALSE;
1136
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001137 // Convert an interrupt to an exception if appropriate.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001138 (void)do_intthrow(&cstack);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001139#endif // FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001140
1141 }
1142 /*
1143 * Continue executing command lines when:
1144 * - no CTRL-C typed, no aborting error, no exception thrown or try
1145 * conditionals need to be checked for executing finally clauses or
1146 * catching an interrupt exception
1147 * - didn't get an error message or lines are not typed
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001148 * - there is a command after '|', inside a :if, :while, :for or :try, or
Bram Moolenaar071d4272004-06-13 20:20:40 +00001149 * looping for ":source" command or function call.
1150 */
1151 while (!((got_int
1152#ifdef FEAT_EVAL
1153 || (did_emsg && force_abort) || did_throw
1154#endif
1155 )
1156#ifdef FEAT_EVAL
1157 && cstack.cs_trylevel == 0
1158#endif
1159 )
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001160 && !(did_emsg
1161#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001162 // Keep going when inside try/catch, so that the error can be
1163 // deal with, except when it is a syntax error, it may cause
1164 // the :endtry to be missed.
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001165 && (cstack.cs_trylevel == 0 || did_emsg_syntax)
1166#endif
1167 && used_getline
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001168 && (getline_equal(fgetline, cookie, getexmodeline)
1169 || getline_equal(fgetline, cookie, getexline)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001170 && (next_cmdline != NULL
1171#ifdef FEAT_EVAL
1172 || cstack.cs_idx >= 0
1173#endif
1174 || (flags & DOCMD_REPEAT)));
1175
1176 vim_free(cmdline_copy);
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001177 did_emsg_syntax = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001178#ifdef FEAT_EVAL
1179 free_cmdlines(&lines_ga);
1180 ga_clear(&lines_ga);
1181
1182 if (cstack.cs_idx >= 0)
1183 {
1184 /*
1185 * If a sourced file or executed function ran to its end, report the
1186 * unclosed conditional.
1187 */
1188 if (!got_int && !did_throw
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001189 && ((getline_equal(fgetline, cookie, getsourceline)
1190 && !source_finished(fgetline, cookie))
1191 || (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001192 && !func_has_ended(real_cookie))))
1193 {
1194 if (cstack.cs_flags[cstack.cs_idx] & CSF_TRY)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001195 emsg(_(e_endtry));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001196 else if (cstack.cs_flags[cstack.cs_idx] & CSF_WHILE)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001197 emsg(_(e_endwhile));
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001198 else if (cstack.cs_flags[cstack.cs_idx] & CSF_FOR)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001199 emsg(_(e_endfor));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001200 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001201 emsg(_(e_endif));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001202 }
1203
1204 /*
1205 * Reset "trylevel" in case of a ":finish" or ":return" or a missing
1206 * ":endtry" in a sourced file or executed function. If the try
1207 * conditional is in its finally clause, ignore anything pending.
1208 * If it is in a catch clause, finish the caught exception.
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001209 * Also cleanup any "cs_forinfo" structures.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001210 */
1211 do
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001212 {
1213 int idx = cleanup_conditionals(&cstack, 0, TRUE);
1214
Bram Moolenaar89e5d682005-01-17 22:06:23 +00001215 if (idx >= 0)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001216 --idx; // remove try block not in its finally clause
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001217 rewind_conditionals(&cstack, idx, CSF_WHILE | CSF_FOR,
1218 &cstack.cs_looplevel);
1219 }
1220 while (cstack.cs_idx >= 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001221 trylevel = initial_trylevel;
1222 }
1223
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001224 // If a missing ":endtry", ":endwhile", ":endfor", or ":endif" or a memory
1225 // lack was reported above and the error message is to be converted to an
1226 // exception, do this now after rewinding the cstack.
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001227 do_errthrow(&cstack, getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001228 ? (char_u *)"endfunction" : (char_u *)NULL);
1229
1230 if (trylevel == 0)
1231 {
1232 /*
1233 * When an exception is being thrown out of the outermost try
1234 * conditional, discard the uncaught exception, disable the conversion
1235 * of interrupts or errors to exceptions, and ensure that no more
1236 * commands are executed.
1237 */
1238 if (did_throw)
1239 {
1240 void *p = NULL;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02001241 msglist_T *messages = NULL, *next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001242
1243 /*
1244 * If the uncaught exception is a user exception, report it as an
1245 * error. If it is an error exception, display the saved error
1246 * message now. For an interrupt exception, do nothing; the
1247 * interrupt message is given elsewhere.
1248 */
1249 switch (current_exception->type)
1250 {
1251 case ET_USER:
Bram Moolenaar9c13b352005-05-19 20:53:52 +00001252 vim_snprintf((char *)IObuff, IOSIZE,
1253 _("E605: Exception not caught: %s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001254 current_exception->value);
1255 p = vim_strsave(IObuff);
1256 break;
1257 case ET_ERROR:
1258 messages = current_exception->messages;
1259 current_exception->messages = NULL;
1260 break;
1261 case ET_INTERRUPT:
1262 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001263 }
1264
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001265 estack_push(ETYPE_EXCEPT, current_exception->throw_name,
1266 current_exception->throw_lnum);
Bram Moolenaare31ee862020-01-07 20:59:34 +01001267 ESTACK_CHECK_SETUP
Bram Moolenaar071d4272004-06-13 20:20:40 +00001268 current_exception->throw_name = NULL;
1269
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001270 discard_current_exception(); // uses IObuff if 'verbose'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001271 suppress_errthrow = TRUE;
1272 force_abort = TRUE;
1273
1274 if (messages != NULL)
1275 {
1276 do
1277 {
1278 next = messages->next;
1279 emsg(messages->msg);
1280 vim_free(messages->msg);
Bram Moolenaar5fbf3bc2020-06-01 21:13:11 +02001281 vim_free(messages->sfile);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001282 vim_free(messages);
1283 messages = next;
1284 }
1285 while (messages != NULL);
1286 }
1287 else if (p != NULL)
1288 {
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01001289 emsg(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001290 vim_free(p);
1291 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001292 vim_free(SOURCING_NAME);
Bram Moolenaare31ee862020-01-07 20:59:34 +01001293 ESTACK_CHECK_NOW
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001294 estack_pop();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001295 }
1296
1297 /*
1298 * On an interrupt or an aborting error not converted to an exception,
1299 * disable the conversion of errors to exceptions. (Interrupts are not
Bram Moolenaar2196bce2020-04-12 20:01:11 +02001300 * converted anymore, here.) This enables also the interrupt message
Bram Moolenaar071d4272004-06-13 20:20:40 +00001301 * when force_abort is set and did_emsg unset in case of an interrupt
1302 * from a finally clause after an error.
1303 */
1304 else if (got_int || (did_emsg && force_abort))
1305 suppress_errthrow = TRUE;
1306 }
1307
1308 /*
1309 * The current cstack will be freed when do_cmdline() returns. An uncaught
1310 * exception will have to be rethrown in the previous cstack. If a function
1311 * has just returned or a script file was just finished and the previous
1312 * cstack belongs to the same function or, respectively, script file, it
1313 * will have to be checked for finally clauses to be executed due to the
1314 * ":return" or ":finish". This is done in do_one_cmd().
1315 */
1316 if (did_throw)
1317 need_rethrow = TRUE;
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001318 if ((getline_equal(fgetline, cookie, getsourceline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001319 && ex_nesting_level > source_level(real_cookie))
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001320 || (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001321 && ex_nesting_level > func_level(real_cookie) + 1))
1322 {
1323 if (!did_throw)
1324 check_cstack = TRUE;
1325 }
1326 else
1327 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001328 // When leaving a function, reduce nesting level.
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001329 if (getline_equal(fgetline, cookie, get_func_line))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001330 --ex_nesting_level;
1331 /*
1332 * Go to debug mode when returning from a function in which we are
1333 * single-stepping.
1334 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001335 if ((getline_equal(fgetline, cookie, getsourceline)
1336 || getline_equal(fgetline, cookie, get_func_line))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001337 && ex_nesting_level + 1 <= debug_break_level)
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001338 do_debug(getline_equal(fgetline, cookie, getsourceline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001339 ? (char_u *)_("End of sourced file")
1340 : (char_u *)_("End of function"));
1341 }
1342
1343 /*
1344 * Restore the exception environment (done after returning from the
1345 * debugger).
1346 */
1347 if (flags & DOCMD_EXCRESET)
Bram Moolenaared203462004-06-16 11:19:22 +00001348 restore_dbg_stuff(&debug_saved);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001349
1350 msg_list = saved_msg_list;
Bram Moolenaar292b90d2020-03-18 15:23:16 +01001351
1352 // Cleanup if "cs_emsg_silent_list" remains.
1353 if (cstack.cs_emsg_silent_list != NULL)
1354 {
1355 eslist_T *elem, *temp;
1356
1357 for (elem = cstack.cs_emsg_silent_list; elem != NULL; elem = temp)
1358 {
1359 temp = elem->next;
1360 vim_free(elem);
1361 }
1362 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001363#endif // FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001364
1365 /*
1366 * If there was too much output to fit on the command line, ask the user to
1367 * hit return before redrawing the screen. With the ":global" command we do
1368 * this only once after the command is finished.
1369 */
1370 if (did_inc)
1371 {
1372 --RedrawingDisabled;
1373 --no_wait_return;
1374 msg_scroll = FALSE;
1375
1376 /*
1377 * When just finished an ":if"-":else" which was typed, no need to
1378 * wait for hit-return. Also for an error situation.
1379 */
1380 if (retval == FAIL
1381#ifdef FEAT_EVAL
1382 || (did_endif && KeyTyped && !did_emsg)
1383#endif
1384 )
1385 {
1386 need_wait_return = FALSE;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001387 msg_didany = FALSE; // don't wait when restarting edit
Bram Moolenaar071d4272004-06-13 20:20:40 +00001388 }
1389 else if (need_wait_return)
1390 {
1391 /*
1392 * The msg_start() above clears msg_didout. The wait_return we do
1393 * here should not overwrite the command that may be shown before
1394 * doing that.
1395 */
1396 msg_didout |= msg_didout_before_start;
1397 wait_return(FALSE);
1398 }
1399 }
1400
Bram Moolenaar2a942252012-11-28 23:03:07 +01001401#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001402 did_endif = FALSE; // in case do_cmdline used recursively
Bram Moolenaar2a942252012-11-28 23:03:07 +01001403#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001404 /*
1405 * Reset if_level, in case a sourced script file contains more ":if" than
1406 * ":endif" (could be ":if x | foo | endif").
1407 */
1408 if_level = 0;
Bram Moolenaar2e18a122012-11-28 19:10:54 +01001409#endif
Bram Moolenaard4ad0d42012-11-28 17:34:48 +01001410
Bram Moolenaar071d4272004-06-13 20:20:40 +00001411 --call_depth;
1412 return retval;
1413}
1414
1415#ifdef FEAT_EVAL
1416/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001417 * Obtain a line when inside a ":while" or ":for" loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001418 */
1419 static char_u *
Bram Moolenaare96a2492019-06-25 04:12:16 +02001420get_loop_line(int c, void *cookie, int indent, int do_concat)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001421{
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001422 struct loop_cookie *cp = (struct loop_cookie *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001423 wcmd_T *wp;
1424 char_u *line;
1425
1426 if (cp->current_line + 1 >= cp->lines_gap->ga_len)
1427 {
1428 if (cp->repeating)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001429 return NULL; // trying to read past ":endwhile"/":endfor"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001430
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001431 // First time inside the ":while"/":for": get line normally.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001432 if (cp->getline == NULL)
Bram Moolenaare96a2492019-06-25 04:12:16 +02001433 line = getcmdline(c, 0L, indent, do_concat);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001434 else
Bram Moolenaare96a2492019-06-25 04:12:16 +02001435 line = cp->getline(c, cp->cookie, indent, do_concat);
Bram Moolenaard68071d2006-05-02 22:08:30 +00001436 if (line != NULL && store_loop_line(cp->lines_gap, line) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001437 ++cp->current_line;
1438
1439 return line;
1440 }
1441
1442 KeyTyped = FALSE;
1443 ++cp->current_line;
1444 wp = (wcmd_T *)(cp->lines_gap->ga_data) + cp->current_line;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001445 SOURCING_LNUM = wp->lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001446 return vim_strsave(wp->line);
1447}
1448
1449/*
1450 * Store a line in "gap" so that a ":while" loop can execute it again.
1451 */
1452 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001453store_loop_line(garray_T *gap, char_u *line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001454{
1455 if (ga_grow(gap, 1) == FAIL)
1456 return FAIL;
1457 ((wcmd_T *)(gap->ga_data))[gap->ga_len].line = vim_strsave(line);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001458 ((wcmd_T *)(gap->ga_data))[gap->ga_len].lnum = SOURCING_LNUM;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001459 ++gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001460 return OK;
1461}
1462
1463/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001464 * Free the lines stored for a ":while" or ":for" loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001465 */
1466 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001467free_cmdlines(garray_T *gap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001468{
1469 while (gap->ga_len > 0)
1470 {
1471 vim_free(((wcmd_T *)(gap->ga_data))[gap->ga_len - 1].line);
1472 --gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001473 }
1474}
1475#endif
1476
1477/*
Bram Moolenaar89d40322006-08-29 15:30:07 +00001478 * If "fgetline" is get_loop_line(), return TRUE if the getline it uses equals
1479 * "func". * Otherwise return TRUE when "fgetline" equals "func".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001480 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001481 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001482getline_equal(
Bram Moolenaare96a2492019-06-25 04:12:16 +02001483 char_u *(*fgetline)(int, void *, int, int),
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001484 void *cookie UNUSED, // argument for fgetline()
Bram Moolenaare96a2492019-06-25 04:12:16 +02001485 char_u *(*func)(int, void *, int, int))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001486{
1487#ifdef FEAT_EVAL
Bram Moolenaare96a2492019-06-25 04:12:16 +02001488 char_u *(*gp)(int, void *, int, int);
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001489 struct loop_cookie *cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001490
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001491 // When "fgetline" is "get_loop_line()" use the "cookie" to find the
1492 // function that's originally used to obtain the lines. This may be
1493 // nested several levels.
Bram Moolenaar89d40322006-08-29 15:30:07 +00001494 gp = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001495 cp = (struct loop_cookie *)cookie;
1496 while (gp == get_loop_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001497 {
1498 gp = cp->getline;
1499 cp = cp->cookie;
1500 }
1501 return gp == func;
1502#else
Bram Moolenaar89d40322006-08-29 15:30:07 +00001503 return fgetline == func;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001504#endif
1505}
1506
Bram Moolenaar071d4272004-06-13 20:20:40 +00001507/*
Bram Moolenaar89d40322006-08-29 15:30:07 +00001508 * If "fgetline" is get_loop_line(), return the cookie used by the original
Bram Moolenaar071d4272004-06-13 20:20:40 +00001509 * getline function. Otherwise return "cookie".
1510 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001511 void *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001512getline_cookie(
Bram Moolenaare96a2492019-06-25 04:12:16 +02001513 char_u *(*fgetline)(int, void *, int, int) UNUSED,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001514 void *cookie) // argument for fgetline()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001515{
Bram Moolenaar13505972019-01-24 15:04:48 +01001516#ifdef FEAT_EVAL
Bram Moolenaare96a2492019-06-25 04:12:16 +02001517 char_u *(*gp)(int, void *, int, int);
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001518 struct loop_cookie *cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001519
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001520 // When "fgetline" is "get_loop_line()" use the "cookie" to find the
1521 // cookie that's originally used to obtain the lines. This may be nested
1522 // several levels.
Bram Moolenaar89d40322006-08-29 15:30:07 +00001523 gp = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001524 cp = (struct loop_cookie *)cookie;
1525 while (gp == get_loop_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001526 {
1527 gp = cp->getline;
1528 cp = cp->cookie;
1529 }
1530 return cp;
Bram Moolenaar13505972019-01-24 15:04:48 +01001531#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001532 return cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001533#endif
Bram Moolenaar13505972019-01-24 15:04:48 +01001534}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001535
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001536
1537/*
1538 * Helper function to apply an offset for buffer commands, i.e. ":bdelete",
1539 * ":bwipeout", etc.
1540 * Returns the buffer number.
1541 */
1542 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001543compute_buffer_local_count(int addr_type, int lnum, int offset)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001544{
1545 buf_T *buf;
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001546 buf_T *nextbuf;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001547 int count = offset;
1548
1549 buf = firstbuf;
1550 while (buf->b_next != NULL && buf->b_fnum < lnum)
1551 buf = buf->b_next;
1552 while (count != 0)
1553 {
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001554 count += (offset < 0) ? 1 : -1;
1555 nextbuf = (offset < 0) ? buf->b_prev : buf->b_next;
1556 if (nextbuf == NULL)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001557 break;
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001558 buf = nextbuf;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001559 if (addr_type == ADDR_LOADED_BUFFERS)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001560 // skip over unloaded buffers
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001561 while (buf->b_ml.ml_mfp == NULL)
1562 {
1563 nextbuf = (offset < 0) ? buf->b_prev : buf->b_next;
1564 if (nextbuf == NULL)
1565 break;
1566 buf = nextbuf;
1567 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001568 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001569 // we might have gone too far, last buffer is not loadedd
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001570 if (addr_type == ADDR_LOADED_BUFFERS)
1571 while (buf->b_ml.ml_mfp == NULL)
1572 {
1573 nextbuf = (offset >= 0) ? buf->b_prev : buf->b_next;
1574 if (nextbuf == NULL)
1575 break;
1576 buf = nextbuf;
1577 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001578 return buf->b_fnum;
1579}
1580
Bram Moolenaarb7316892019-05-01 18:08:42 +02001581/*
1582 * Return the window number of "win".
1583 * When "win" is NULL return the number of windows.
1584 */
Bram Moolenaarf240e182014-11-27 18:33:02 +01001585 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001586current_win_nr(win_T *win)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001587{
1588 win_T *wp;
1589 int nr = 0;
1590
Bram Moolenaar29323592016-07-24 22:04:11 +02001591 FOR_ALL_WINDOWS(wp)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001592 {
1593 ++nr;
1594 if (wp == win)
1595 break;
1596 }
1597 return nr;
1598}
1599
1600 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001601current_tab_nr(tabpage_T *tab)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001602{
1603 tabpage_T *tp;
1604 int nr = 0;
1605
Bram Moolenaar29323592016-07-24 22:04:11 +02001606 FOR_ALL_TABPAGES(tp)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001607 {
1608 ++nr;
1609 if (tp == tab)
1610 break;
1611 }
1612 return nr;
1613}
1614
1615# define CURRENT_WIN_NR current_win_nr(curwin)
1616# define LAST_WIN_NR current_win_nr(NULL)
1617# define CURRENT_TAB_NR current_tab_nr(curtab)
1618# define LAST_TAB_NR current_tab_nr(NULL)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001619
Bram Moolenaar071d4272004-06-13 20:20:40 +00001620/*
1621 * Execute one Ex command.
1622 *
1623 * If 'sourcing' is TRUE, the command will be included in the error message.
1624 *
1625 * 1. skip comment lines and leading space
1626 * 2. handle command modifiers
Bram Moolenaar1c40a662014-11-27 16:38:11 +01001627 * 3. find the command
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001628 * 4. parse range
Bram Moolenaar1c40a662014-11-27 16:38:11 +01001629 * 5. Parse the command.
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001630 * 6. parse arguments
1631 * 7. switch on command name
Bram Moolenaar071d4272004-06-13 20:20:40 +00001632 *
Bram Moolenaar89d40322006-08-29 15:30:07 +00001633 * Note: "fgetline" can be NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001634 *
1635 * This function may be called recursively!
1636 */
1637#if (_MSC_VER == 1200)
1638/*
Bram Moolenaared203462004-06-16 11:19:22 +00001639 * Avoid optimisation bug in VC++ version 6.0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001640 */
Bram Moolenaar281bdce2005-01-25 21:53:18 +00001641 #pragma optimize( "g", off )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001642#endif
1643 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001644do_one_cmd(
Bram Moolenaarddef1292019-12-16 17:10:33 +01001645 char_u **cmdlinep,
1646 int sourcing,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001647#ifdef FEAT_EVAL
Bram Moolenaarddef1292019-12-16 17:10:33 +01001648 cstack_T *cstack,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001649#endif
Bram Moolenaarddef1292019-12-16 17:10:33 +01001650 char_u *(*fgetline)(int, void *, int, int),
1651 void *cookie) // argument for fgetline()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001652{
Bram Moolenaarddef1292019-12-16 17:10:33 +01001653 char_u *p;
1654 linenr_T lnum;
1655 long n;
1656 char *errormsg = NULL; // error message
1657 char_u *after_modifier = NULL;
1658 exarg_T ea; // Ex command arguments
1659 int save_msg_scroll = msg_scroll;
1660 cmdmod_T save_cmdmod;
1661 int save_reg_executing = reg_executing;
1662 int ni; // set when Not Implemented
1663 char_u *cmd;
Bram Moolenaar83f37b92020-02-23 14:35:01 +01001664#ifdef FEAT_EVAL
1665 int starts_with_colon;
1666#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001667
Bram Moolenaara80faa82020-04-12 19:37:17 +02001668 CLEAR_FIELD(ea);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001669 ea.line1 = 1;
1670 ea.line2 = 1;
1671#ifdef FEAT_EVAL
1672 ++ex_nesting_level;
1673#endif
1674
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001675 // When the last file has not been edited :q has to be typed twice.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001676 if (quitmore
1677#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001678 // avoid that a function call in 'statusline' does this
Bram Moolenaar89d40322006-08-29 15:30:07 +00001679 && !getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001680#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001681 // avoid that an autocommand, e.g. QuitPre, does this
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001682 && !getline_equal(fgetline, cookie, getnextac))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001683 --quitmore;
1684
1685 /*
1686 * Reset browse, confirm, etc.. They are restored when returning, for
1687 * recursive calls.
1688 */
1689 save_cmdmod = cmdmod;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001690
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001691 // "#!anything" is handled like a comment.
Bram Moolenaarcbb37ad2006-08-16 15:04:21 +00001692 if ((*cmdlinep)[0] == '#' && (*cmdlinep)[1] == '!')
1693 goto doend;
1694
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02001695/*
1696 * 1. Skip comment lines and leading white space and colons.
1697 * 2. Handle command modifiers.
1698 */
1699 // The "ea" structure holds the arguments that can be used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001700 ea.cmd = *cmdlinep;
Bram Moolenaareffed932018-08-14 13:38:17 +02001701 ea.cmdlinep = cmdlinep;
1702 ea.getline = fgetline;
1703 ea.cookie = cookie;
1704#ifdef FEAT_EVAL
1705 ea.cstack = cstack;
Bram Moolenaar83f37b92020-02-23 14:35:01 +01001706 starts_with_colon = *skipwhite(ea.cmd) == ':';
Bram Moolenaar071d4272004-06-13 20:20:40 +00001707#endif
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02001708 if (parse_command_modifiers(&ea, &errormsg, FALSE) == FAIL)
Bram Moolenaareffed932018-08-14 13:38:17 +02001709 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001710
Bram Moolenaar7b668e82016-08-23 23:51:21 +02001711 after_modifier = ea.cmd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001712
1713#ifdef FEAT_EVAL
1714 ea.skip = did_emsg || got_int || did_throw || (cstack->cs_idx >= 0
1715 && !(cstack->cs_flags[cstack->cs_idx] & CSF_ACTIVE));
1716#else
1717 ea.skip = (if_level > 0);
1718#endif
1719
Bram Moolenaar071d4272004-06-13 20:20:40 +00001720/*
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001721 * 3. Skip over the range to find the command. Let "p" point to after it.
1722 *
1723 * We need the command to know what kind of range it uses.
1724 */
1725 cmd = ea.cmd;
1726 ea.cmd = skip_range(ea.cmd, NULL);
1727 if (*ea.cmd == '*' && vim_strchr(p_cpo, CPO_STAR) == NULL)
1728 ea.cmd = skipwhite(ea.cmd + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001729
1730#ifdef FEAT_EVAL
Bram Moolenaar83f37b92020-02-23 14:35:01 +01001731 if (current_sctx.sc_version == SCRIPT_VERSION_VIM9 && !starts_with_colon)
Bram Moolenaardf069ee2020-06-22 23:02:51 +02001732 {
1733 if (ea.cmd > cmd)
1734 {
1735 emsg(_(e_colon_required));
1736 goto doend;
1737 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001738 p = find_ex_command(&ea, NULL, lookup_scriptvar, NULL);
Bram Moolenaardf069ee2020-06-22 23:02:51 +02001739 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001740 else
1741#endif
1742 p = find_ex_command(&ea, NULL, NULL, NULL);
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001743
Bram Moolenaar7feb35e2018-08-21 17:49:54 +02001744#ifdef FEAT_EVAL
1745# ifdef FEAT_PROFILE
1746 // Count this line for profiling if skip is TRUE.
1747 if (do_profiling == PROF_YES
1748 && (!ea.skip || cstack->cs_idx == 0 || (cstack->cs_idx > 0
1749 && (cstack->cs_flags[cstack->cs_idx - 1] & CSF_ACTIVE))))
1750 {
1751 int skip = did_emsg || got_int || did_throw;
1752
1753 if (ea.cmdidx == CMD_catch)
1754 skip = !skip && !(cstack->cs_idx >= 0
1755 && (cstack->cs_flags[cstack->cs_idx] & CSF_THROWN)
1756 && !(cstack->cs_flags[cstack->cs_idx] & CSF_CAUGHT));
1757 else if (ea.cmdidx == CMD_else || ea.cmdidx == CMD_elseif)
1758 skip = skip || !(cstack->cs_idx >= 0
1759 && !(cstack->cs_flags[cstack->cs_idx]
1760 & (CSF_ACTIVE | CSF_TRUE)));
1761 else if (ea.cmdidx == CMD_finally)
1762 skip = FALSE;
1763 else if (ea.cmdidx != CMD_endif
1764 && ea.cmdidx != CMD_endfor
1765 && ea.cmdidx != CMD_endtry
1766 && ea.cmdidx != CMD_endwhile)
1767 skip = ea.skip;
1768
1769 if (!skip)
1770 {
1771 if (getline_equal(fgetline, cookie, get_func_line))
1772 func_line_exec(getline_cookie(fgetline, cookie));
1773 else if (getline_equal(fgetline, cookie, getsourceline))
1774 script_line_exec();
1775 }
1776 }
1777# endif
1778
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001779 // May go to debug mode. If this happens and the ">quit" debug command is
1780 // used, throw an interrupt exception and skip the next command.
Bram Moolenaar7feb35e2018-08-21 17:49:54 +02001781 dbg_check_breakpoint(&ea);
1782 if (!ea.skip && got_int)
1783 {
1784 ea.skip = TRUE;
1785 (void)do_intthrow(cstack);
1786 }
1787#endif
1788
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001789/*
1790 * 4. parse a range specifier of the form: addr [,addr] [;addr] ..
Bram Moolenaar071d4272004-06-13 20:20:40 +00001791 *
1792 * where 'addr' is:
1793 *
1794 * % (entire file)
1795 * $ [+-NUM]
1796 * 'x [+-NUM] (where x denotes a currently defined mark)
1797 * . [+-NUM]
1798 * [+-NUM]..
1799 * NUM
1800 *
1801 * The ea.cmd pointer is updated to point to the first character following the
1802 * range spec. If an initial address is found, but no second, the upper bound
1803 * is equal to the lower.
1804 */
1805
Bram Moolenaar25190db2019-05-04 15:05:28 +02001806 // ea.addr_type for user commands is set by find_ucmd
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01001807 if (!IS_USER_CMDIDX(ea.cmdidx))
1808 {
1809 if (ea.cmdidx != CMD_SIZE)
1810 ea.addr_type = cmdnames[(int)ea.cmdidx].cmd_addr_type;
1811 else
1812 ea.addr_type = ADDR_LINES;
1813
Bram Moolenaar25190db2019-05-04 15:05:28 +02001814 // :wincmd range depends on the argument.
Bram Moolenaar164f3262015-01-14 21:22:01 +01001815 if (ea.cmdidx == CMD_wincmd && p != NULL)
1816 get_wincmd_addr_type(skipwhite(p), &ea);
Bram Moolenaar25190db2019-05-04 15:05:28 +02001817#ifdef FEAT_QUICKFIX
1818 // :.cc in quickfix window uses line number
1819 if ((ea.cmdidx == CMD_cc || ea.cmdidx == CMD_ll) && bt_quickfix(curbuf))
1820 ea.addr_type = ADDR_OTHER;
1821#endif
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01001822 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001823
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01001824 ea.cmd = cmd;
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02001825 if (parse_cmd_address(&ea, &errormsg, FALSE) == FAIL)
Bram Moolenaaree8415b2018-08-10 23:13:12 +02001826 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001827
Bram Moolenaar071d4272004-06-13 20:20:40 +00001828/*
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001829 * 5. Parse the command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001830 */
1831
1832 /*
1833 * Skip ':' and any white space
1834 */
1835 ea.cmd = skipwhite(ea.cmd);
1836 while (*ea.cmd == ':')
1837 ea.cmd = skipwhite(ea.cmd + 1);
1838
1839 /*
1840 * If we got a line, but no command, then go to the line.
1841 * If we find a '|' or '\n' we set ea.nextcmd.
1842 */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001843 if (*ea.cmd == NUL || *ea.cmd == '"'
Bram Moolenaar7a092242020-04-16 22:10:49 +02001844#ifdef FEAT_EVAL
Bram Moolenaara26b9702020-04-18 19:53:28 +02001845 || (*ea.cmd == '#' && ea.cmd[1] != '{'
1846 && !starts_with_colon && in_vim9script())
Bram Moolenaar7a092242020-04-16 22:10:49 +02001847#endif
1848 || (ea.nextcmd = check_nextcmd(ea.cmd)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001849 {
1850 /*
1851 * strange vi behaviour:
1852 * ":3" jumps to line 3
1853 * ":3|..." prints line 3
1854 * ":|" prints current line
1855 */
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001856 if (ea.skip) // skip this if inside :if
Bram Moolenaar071d4272004-06-13 20:20:40 +00001857 goto doend;
Bram Moolenaardf177f62005-02-22 08:39:57 +00001858 if (*ea.cmd == '|' || (exmode_active && ea.line1 != ea.line2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001859 {
1860 ea.cmdidx = CMD_print;
Bram Moolenaar8071cb22019-07-12 17:58:01 +02001861 ea.argt = EX_RANGE+EX_COUNT+EX_TRLBAR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001862 if ((errormsg = invalid_range(&ea)) == NULL)
1863 {
1864 correct_range(&ea);
1865 ex_print(&ea);
1866 }
1867 }
1868 else if (ea.addr_count != 0)
1869 {
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00001870 if (ea.line2 > curbuf->b_ml.ml_line_count)
1871 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001872 // With '-' in 'cpoptions' a line number past the file is an
1873 // error, otherwise put it at the end of the file.
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00001874 if (vim_strchr(p_cpo, CPO_MINUS) != NULL)
1875 ea.line2 = -1;
1876 else
1877 ea.line2 = curbuf->b_ml.ml_line_count;
1878 }
1879
1880 if (ea.line2 < 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001881 errormsg = _(e_invrange);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001882 else
1883 {
1884 if (ea.line2 == 0)
1885 curwin->w_cursor.lnum = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001886 else
1887 curwin->w_cursor.lnum = ea.line2;
1888 beginline(BL_SOL | BL_FIX);
1889 }
1890 }
1891 goto doend;
1892 }
1893
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001894 // If this looks like an undefined user command and there are CmdUndefined
1895 // autocommands defined, trigger the matching autocommands.
Bram Moolenaard5005162014-08-22 23:05:54 +02001896 if (p != NULL && ea.cmdidx == CMD_SIZE && !ea.skip
1897 && ASCII_ISUPPER(*ea.cmd)
1898 && has_cmdundefined())
1899 {
Bram Moolenaard5005162014-08-22 23:05:54 +02001900 int ret;
1901
Bram Moolenaar5a31b462014-08-23 14:16:20 +02001902 p = ea.cmd;
Bram Moolenaard5005162014-08-22 23:05:54 +02001903 while (ASCII_ISALNUM(*p))
1904 ++p;
Bram Moolenaar158ea172020-06-18 17:28:39 +02001905 p = vim_strnsave(ea.cmd, p - ea.cmd);
Bram Moolenaard5005162014-08-22 23:05:54 +02001906 ret = apply_autocmds(EVENT_CMDUNDEFINED, p, p, TRUE, NULL);
1907 vim_free(p);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001908 // If the autocommands did something and didn't cause an error, try
1909 // finding the command again.
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001910 p = (ret
1911#ifdef FEAT_EVAL
1912 && !aborting()
Bram Moolenaard5005162014-08-22 23:05:54 +02001913#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001914 ) ? find_ex_command(&ea, NULL, NULL, NULL) : ea.cmd;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001915 }
Bram Moolenaard5005162014-08-22 23:05:54 +02001916
Bram Moolenaar071d4272004-06-13 20:20:40 +00001917 if (p == NULL)
1918 {
1919 if (!ea.skip)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001920 errormsg = _("E464: Ambiguous use of user-defined command");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001921 goto doend;
1922 }
Bram Moolenaarac9fb182019-04-27 13:04:13 +02001923 // Check for wrong commands.
Bram Moolenaar6f9a4762017-06-22 20:39:17 +02001924 if (*p == '!' && ea.cmd[1] == 0151 && ea.cmd[0] == 78
1925 && !IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001926 {
1927 errormsg = uc_fun_cmd();
1928 goto doend;
1929 }
Bram Moolenaarac9fb182019-04-27 13:04:13 +02001930
Bram Moolenaar071d4272004-06-13 20:20:40 +00001931 if (ea.cmdidx == CMD_SIZE)
1932 {
1933 if (!ea.skip)
1934 {
1935 STRCPY(IObuff, _("E492: Not an editor command"));
1936 if (!sourcing)
Bram Moolenaar7b668e82016-08-23 23:51:21 +02001937 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001938 // If the modifier was parsed OK the error must be in the
1939 // following command
Bram Moolenaar7b668e82016-08-23 23:51:21 +02001940 if (after_modifier != NULL)
1941 append_command(after_modifier);
1942 else
1943 append_command(*cmdlinep);
1944 }
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001945 errormsg = (char *)IObuff;
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001946 did_emsg_syntax = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001947 }
1948 goto doend;
1949 }
1950
Bram Moolenaar958636c2014-10-21 20:01:58 +02001951 ni = (!IS_USER_CMDIDX(ea.cmdidx)
1952 && (cmdnames[ea.cmdidx].cmd_func == ex_ni
Bram Moolenaar7bb75552007-07-16 18:39:49 +00001953#ifdef HAVE_EX_SCRIPT_NI
1954 || cmdnames[ea.cmdidx].cmd_func == ex_script_ni
1955#endif
1956 ));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001957
1958#ifndef FEAT_EVAL
1959 /*
1960 * When the expression evaluation is disabled, recognize the ":if" and
1961 * ":endif" commands and ignore everything in between it.
1962 */
1963 if (ea.cmdidx == CMD_if)
1964 ++if_level;
1965 if (if_level)
1966 {
1967 if (ea.cmdidx == CMD_endif)
1968 --if_level;
1969 goto doend;
1970 }
1971
1972#endif
1973
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001974 // forced commands
Bram Moolenaar196b3b02008-06-20 16:51:41 +00001975 if (*p == '!' && ea.cmdidx != CMD_substitute
1976 && ea.cmdidx != CMD_smagic && ea.cmdidx != CMD_snomagic)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001977 {
1978 ++p;
1979 ea.forceit = TRUE;
1980 }
1981 else
1982 ea.forceit = FALSE;
1983
1984/*
Bram Moolenaarb7316892019-05-01 18:08:42 +02001985 * 6. Parse arguments. Then check for errors.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001986 */
Bram Moolenaar958636c2014-10-21 20:01:58 +02001987 if (!IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001988 ea.argt = (long)cmdnames[(int)ea.cmdidx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001989
1990 if (!ea.skip)
1991 {
1992#ifdef HAVE_SANDBOX
Bram Moolenaar8071cb22019-07-12 17:58:01 +02001993 if (sandbox != 0 && !(ea.argt & EX_SBOXOK))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001994 {
Bram Moolenaar8c62a082019-02-08 14:34:10 +01001995 // Command not allowed in sandbox.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001996 errormsg = _(e_sandbox);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001997 goto doend;
1998 }
1999#endif
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002000 if (restricted != 0 && (ea.argt & EX_RESTRICT))
Bram Moolenaar8c62a082019-02-08 14:34:10 +01002001 {
2002 errormsg = _("E981: Command not allowed in rvim");
2003 goto doend;
2004 }
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002005 if (!curbuf->b_p_ma && (ea.argt & EX_MODIFY))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002006 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002007 // Command not allowed in non-'modifiable' buffer
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002008 errormsg = _(e_modifiable);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002009 goto doend;
2010 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002011
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002012 if (text_locked() && !(ea.argt & EX_CMDWIN)
Bram Moolenaar958636c2014-10-21 20:01:58 +02002013 && !IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002014 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002015 // Command not allowed when editing the command line.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002016 errormsg = _(get_text_locked_msg());
Bram Moolenaar071d4272004-06-13 20:20:40 +00002017 goto doend;
2018 }
Bram Moolenaar379fb762018-08-30 15:58:28 +02002019
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002020 // Disallow editing another buffer when "curbuf_lock" is set.
2021 // Do allow ":checktime" (it is postponed).
2022 // Do allow ":edit" (check for an argument later).
2023 // Do allow ":file" with no arguments (check for an argument later).
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002024 if (!(ea.argt & EX_CMDWIN)
Bram Moolenaar5555acc2006-04-07 21:33:12 +00002025 && ea.cmdidx != CMD_checktime
Bram Moolenaar379fb762018-08-30 15:58:28 +02002026 && ea.cmdidx != CMD_edit
2027 && ea.cmdidx != CMD_file
Bram Moolenaar958636c2014-10-21 20:01:58 +02002028 && !IS_USER_CMDIDX(ea.cmdidx)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002029 && curbuf_locked())
2030 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002031
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002032 if (!ni && !(ea.argt & EX_RANGE) && ea.addr_count > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002033 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002034 // no range allowed
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002035 errormsg = _(e_norange);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002036 goto doend;
2037 }
2038 }
2039
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002040 if (!ni && !(ea.argt & EX_BANG) && ea.forceit) // no <!> allowed
Bram Moolenaar071d4272004-06-13 20:20:40 +00002041 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002042 errormsg = _(e_nobang);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002043 goto doend;
2044 }
2045
2046 /*
2047 * Don't complain about the range if it is not used
2048 * (could happen if line_count is accidentally set to 0).
2049 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002050 if (!ea.skip && !ni && (ea.argt & EX_RANGE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002051 {
2052 /*
2053 * If the range is backwards, ask for confirmation and, if given, swap
2054 * ea.line1 & ea.line2 so it's forwards again.
2055 * When global command is busy, don't ask, will fail below.
2056 */
2057 if (!global_busy && ea.line1 > ea.line2)
2058 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002059 if (msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002060 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002061 if (sourcing || exmode_active)
2062 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002063 errormsg = _("E493: Backwards range given");
Bram Moolenaara5792f52005-11-23 21:25:05 +00002064 goto doend;
2065 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002066 if (ask_yesno((char_u *)
2067 _("Backwards range given, OK to swap"), FALSE) != 'y')
Bram Moolenaara5792f52005-11-23 21:25:05 +00002068 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002069 }
2070 lnum = ea.line1;
2071 ea.line1 = ea.line2;
2072 ea.line2 = lnum;
2073 }
2074 if ((errormsg = invalid_range(&ea)) != NULL)
2075 goto doend;
2076 }
2077
Bram Moolenaarb7316892019-05-01 18:08:42 +02002078 if ((ea.addr_type == ADDR_OTHER) && ea.addr_count == 0)
2079 // default is 1, not cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00002080 ea.line2 = 1;
2081
2082 correct_range(&ea);
2083
2084#ifdef FEAT_FOLDING
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002085 if (((ea.argt & EX_WHOLEFOLD) || ea.addr_count >= 2) && !global_busy
Bram Moolenaara3306952016-01-02 21:41:06 +01002086 && ea.addr_type == ADDR_LINES)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002087 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002088 // Put the first line at the start of a closed fold, put the last line
2089 // at the end of a closed fold.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002090 (void)hasFolding(ea.line1, &ea.line1, NULL);
2091 (void)hasFolding(ea.line2, NULL, &ea.line2);
2092 }
2093#endif
2094
2095#ifdef FEAT_QUICKFIX
2096 /*
Bram Moolenaar86b68352004-12-27 21:59:20 +00002097 * For the ":make" and ":grep" commands we insert the 'makeprg'/'grepprg'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002098 * option here, so things like % get expanded.
2099 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002100 p = replace_makeprg(&ea, p, cmdlinep);
2101 if (p == NULL)
2102 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002103#endif
2104
2105 /*
2106 * Skip to start of argument.
2107 * Don't do this for the ":!" command, because ":!! -l" needs the space.
2108 */
2109 if (ea.cmdidx == CMD_bang)
2110 ea.arg = p;
2111 else
2112 ea.arg = skipwhite(p);
2113
Bram Moolenaar379fb762018-08-30 15:58:28 +02002114 // ":file" cannot be run with an argument when "curbuf_lock" is set
2115 if (ea.cmdidx == CMD_file && *ea.arg != NUL && curbuf_locked())
2116 goto doend;
2117
Bram Moolenaar071d4272004-06-13 20:20:40 +00002118 /*
2119 * Check for "++opt=val" argument.
2120 * Must be first, allow ":w ++enc=utf8 !cmd"
2121 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002122 if (ea.argt & EX_ARGOPT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002123 while (ea.arg[0] == '+' && ea.arg[1] == '+')
2124 if (getargopt(&ea) == FAIL && !ni)
2125 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002126 errormsg = _(e_invarg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002127 goto doend;
2128 }
2129
2130 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
2131 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002132 if (*ea.arg == '>') // append
Bram Moolenaar071d4272004-06-13 20:20:40 +00002133 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002134 if (*++ea.arg != '>') // typed wrong
Bram Moolenaar071d4272004-06-13 20:20:40 +00002135 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002136 errormsg = _("E494: Use w or w>>");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002137 goto doend;
2138 }
2139 ea.arg = skipwhite(ea.arg + 1);
2140 ea.append = TRUE;
2141 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002142 else if (*ea.arg == '!' && ea.cmdidx == CMD_write) // :w !filter
Bram Moolenaar071d4272004-06-13 20:20:40 +00002143 {
2144 ++ea.arg;
2145 ea.usefilter = TRUE;
2146 }
2147 }
2148
2149 if (ea.cmdidx == CMD_read)
2150 {
2151 if (ea.forceit)
2152 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002153 ea.usefilter = TRUE; // :r! filter if ea.forceit
Bram Moolenaar071d4272004-06-13 20:20:40 +00002154 ea.forceit = FALSE;
2155 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002156 else if (*ea.arg == '!') // :r !filter
Bram Moolenaar071d4272004-06-13 20:20:40 +00002157 {
2158 ++ea.arg;
2159 ea.usefilter = TRUE;
2160 }
2161 }
2162
2163 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
2164 {
2165 ea.amount = 1;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002166 while (*ea.arg == *ea.cmd) // count number of '>' or '<'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002167 {
2168 ++ea.arg;
2169 ++ea.amount;
2170 }
2171 ea.arg = skipwhite(ea.arg);
2172 }
2173
2174 /*
2175 * Check for "+command" argument, before checking for next command.
2176 * Don't do this for ":read !cmd" and ":write !cmd".
2177 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002178 if ((ea.argt & EX_CMDARG) && !ea.usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002179 ea.do_ecmd_cmd = getargcmd(&ea.arg);
2180
2181 /*
2182 * Check for '|' to separate commands and '"' to start comments.
2183 * Don't do this for ":read !cmd" and ":write !cmd".
2184 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002185 if ((ea.argt & EX_TRLBAR) && !ea.usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002186 separate_nextcmd(&ea);
2187
2188 /*
2189 * Check for <newline> to end a shell command.
Bram Moolenaardf177f62005-02-22 08:39:57 +00002190 * Also do this for ":read !cmd", ":write !cmd" and ":global".
2191 * Any others?
Bram Moolenaar071d4272004-06-13 20:20:40 +00002192 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00002193 else if (ea.cmdidx == CMD_bang
Bram Moolenaar67883b42017-07-27 22:57:00 +02002194 || ea.cmdidx == CMD_terminal
Bram Moolenaardf177f62005-02-22 08:39:57 +00002195 || ea.cmdidx == CMD_global
2196 || ea.cmdidx == CMD_vglobal
2197 || ea.usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002198 {
2199 for (p = ea.arg; *p; ++p)
2200 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002201 // Remove one backslash before a newline, so that it's possible to
2202 // pass a newline to the shell and also a newline that is preceded
2203 // with a backslash. This makes it impossible to end a shell
2204 // command in a backslash, but that doesn't appear useful.
2205 // Halving the number of backslashes is incompatible with previous
2206 // versions.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002207 if (*p == '\\' && p[1] == '\n')
Bram Moolenaara7241f52008-06-24 20:39:31 +00002208 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002209 else if (*p == '\n')
2210 {
2211 ea.nextcmd = p + 1;
2212 *p = NUL;
2213 break;
2214 }
2215 }
2216 }
2217
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002218 if ((ea.argt & EX_DFLALL) && ea.addr_count == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002219 {
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002220 buf_T *buf;
2221
Bram Moolenaar071d4272004-06-13 20:20:40 +00002222 ea.line1 = 1;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002223 switch (ea.addr_type)
2224 {
2225 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02002226 case ADDR_OTHER:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002227 ea.line2 = curbuf->b_ml.ml_line_count;
2228 break;
2229 case ADDR_LOADED_BUFFERS:
2230 buf = firstbuf;
2231 while (buf->b_next != NULL && buf->b_ml.ml_mfp == NULL)
2232 buf = buf->b_next;
2233 ea.line1 = buf->b_fnum;
2234 buf = lastbuf;
2235 while (buf->b_prev != NULL && buf->b_ml.ml_mfp == NULL)
2236 buf = buf->b_prev;
2237 ea.line2 = buf->b_fnum;
2238 break;
2239 case ADDR_BUFFERS:
2240 ea.line1 = firstbuf->b_fnum;
2241 ea.line2 = lastbuf->b_fnum;
2242 break;
2243 case ADDR_WINDOWS:
2244 ea.line2 = LAST_WIN_NR;
2245 break;
2246 case ADDR_TABS:
2247 ea.line2 = LAST_TAB_NR;
2248 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01002249 case ADDR_TABS_RELATIVE:
2250 ea.line2 = 1;
2251 break;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002252 case ADDR_ARGUMENTS:
2253 if (ARGCOUNT == 0)
2254 ea.line1 = ea.line2 = 0;
2255 else
2256 ea.line2 = ARGCOUNT;
2257 break;
Bram Moolenaar25190db2019-05-04 15:05:28 +02002258 case ADDR_QUICKFIX_VALID:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02002259#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02002260 ea.line2 = qf_get_valid_size(&ea);
Bram Moolenaaraa23b372015-09-08 18:46:31 +02002261 if (ea.line2 == 0)
2262 ea.line2 = 1;
Bram Moolenaare906c502015-09-09 21:10:39 +02002263#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02002264 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02002265 case ADDR_NONE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02002266 case ADDR_UNSIGNED:
2267 case ADDR_QUICKFIX:
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002268 iemsg(_("INTERNAL: Cannot use EX_DFLALL with ADDR_NONE, ADDR_UNSIGNED or ADDR_QUICKFIX"));
Bram Moolenaarb7316892019-05-01 18:08:42 +02002269 break;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002270 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002271 }
2272
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002273 // accept numbered register only when no count allowed (:put)
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002274 if ( (ea.argt & EX_REGSTR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002275 && *ea.arg != NUL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002276 // Do not allow register = for user commands
Bram Moolenaar958636c2014-10-21 20:01:58 +02002277 && (!IS_USER_CMDIDX(ea.cmdidx) || *ea.arg != '=')
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002278 && !((ea.argt & EX_COUNT) && VIM_ISDIGIT(*ea.arg)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002279 {
Bram Moolenaar85de2062011-05-05 14:26:41 +02002280#ifndef FEAT_CLIPBOARD
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002281 // check these explicitly for a more specific error message
Bram Moolenaar85de2062011-05-05 14:26:41 +02002282 if (*ea.arg == '*' || *ea.arg == '+')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002283 {
Bram Moolenaar99b12722019-01-14 20:16:40 +01002284 errormsg = _(e_invalidreg);
Bram Moolenaar85de2062011-05-05 14:26:41 +02002285 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002286 }
2287#endif
Bram Moolenaar958636c2014-10-21 20:01:58 +02002288 if (valid_yank_reg(*ea.arg, (ea.cmdidx != CMD_put
2289 && !IS_USER_CMDIDX(ea.cmdidx))))
Bram Moolenaar85de2062011-05-05 14:26:41 +02002290 {
2291 ea.regname = *ea.arg++;
2292#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002293 // for '=' register: accept the rest of the line as an expression
Bram Moolenaar85de2062011-05-05 14:26:41 +02002294 if (ea.arg[-1] == '=' && ea.arg[0] != NUL)
2295 {
2296 set_expr_line(vim_strsave(ea.arg));
2297 ea.arg += STRLEN(ea.arg);
2298 }
2299#endif
2300 ea.arg = skipwhite(ea.arg);
2301 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002302 }
2303
2304 /*
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002305 * Check for a count. When accepting a EX_BUFNAME, don't use "123foo" as a
Bram Moolenaar071d4272004-06-13 20:20:40 +00002306 * count, it's a buffer name.
2307 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002308 if ((ea.argt & EX_COUNT) && VIM_ISDIGIT(*ea.arg)
2309 && (!(ea.argt & EX_BUFNAME) || *(p = skipdigits(ea.arg)) == NUL
Bram Moolenaar1c465442017-03-12 20:10:05 +01002310 || VIM_ISWHITE(*p)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002311 {
2312 n = getdigits(&ea.arg);
2313 ea.arg = skipwhite(ea.arg);
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002314 if (n <= 0 && !ni && (ea.argt & EX_ZEROR) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002315 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002316 errormsg = _(e_zerocount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002317 goto doend;
2318 }
Bram Moolenaarb7316892019-05-01 18:08:42 +02002319 if (ea.addr_type != ADDR_LINES) // e.g. :buffer 2, :sleep 3
Bram Moolenaar071d4272004-06-13 20:20:40 +00002320 {
2321 ea.line2 = n;
2322 if (ea.addr_count == 0)
2323 ea.addr_count = 1;
2324 }
2325 else
2326 {
2327 ea.line1 = ea.line2;
2328 ea.line2 += n - 1;
2329 ++ea.addr_count;
2330 /*
2331 * Be vi compatible: no error message for out of range.
2332 */
Bram Moolenaarb7316892019-05-01 18:08:42 +02002333 if (ea.line2 > curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002334 ea.line2 = curbuf->b_ml.ml_line_count;
2335 }
2336 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00002337
2338 /*
2339 * Check for flags: 'l', 'p' and '#'.
2340 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002341 if (ea.argt & EX_FLAGS)
Bram Moolenaardf177f62005-02-22 08:39:57 +00002342 get_flags(&ea);
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002343 if (!ni && !(ea.argt & EX_EXTRA) && *ea.arg != NUL
2344 && *ea.arg != '"' && (*ea.arg != '|' || (ea.argt & EX_TRLBAR) == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002345 {
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002346 // no arguments allowed but there is something
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002347 errormsg = _(e_trailing);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002348 goto doend;
2349 }
2350
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002351 if (!ni && (ea.argt & EX_NEEDARG) && *ea.arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002352 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002353 errormsg = _(e_argreq);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002354 goto doend;
2355 }
2356
2357#ifdef FEAT_EVAL
2358 /*
2359 * Skip the command when it's not going to be executed.
2360 * The commands like :if, :endif, etc. always need to be executed.
2361 * Also make an exception for commands that handle a trailing command
2362 * themselves.
2363 */
2364 if (ea.skip)
2365 {
2366 switch (ea.cmdidx)
2367 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002368 // commands that need evaluation
Bram Moolenaar071d4272004-06-13 20:20:40 +00002369 case CMD_while:
2370 case CMD_endwhile:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00002371 case CMD_for:
2372 case CMD_endfor:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002373 case CMD_if:
2374 case CMD_elseif:
2375 case CMD_else:
2376 case CMD_endif:
2377 case CMD_try:
2378 case CMD_catch:
2379 case CMD_finally:
2380 case CMD_endtry:
2381 case CMD_function:
Bram Moolenaarab55c682020-03-01 19:41:43 +01002382 case CMD_def:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002383 break;
2384
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002385 // Commands that handle '|' themselves. Check: A command should
2386 // either have the EX_TRLBAR flag, appear in this list or appear in
2387 // the list at ":help :bar".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002388 case CMD_aboveleft:
2389 case CMD_and:
2390 case CMD_belowright:
2391 case CMD_botright:
2392 case CMD_browse:
2393 case CMD_call:
2394 case CMD_confirm:
Bram Moolenaar8f76e6b2019-11-26 16:50:30 +01002395 case CMD_const:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002396 case CMD_delfunction:
2397 case CMD_djump:
2398 case CMD_dlist:
2399 case CMD_dsearch:
2400 case CMD_dsplit:
2401 case CMD_echo:
2402 case CMD_echoerr:
2403 case CMD_echomsg:
2404 case CMD_echon:
Bram Moolenaara76b3152020-02-16 16:20:21 +01002405 case CMD_eval:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002406 case CMD_execute:
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002407 case CMD_filter:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002408 case CMD_help:
2409 case CMD_hide:
2410 case CMD_ijump:
2411 case CMD_ilist:
2412 case CMD_isearch:
2413 case CMD_isplit:
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002414 case CMD_keepalt:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002415 case CMD_keepjumps:
2416 case CMD_keepmarks:
Bram Moolenaara939e432013-11-09 05:30:26 +01002417 case CMD_keeppatterns:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002418 case CMD_leftabove:
2419 case CMD_let:
2420 case CMD_lockmarks:
Bram Moolenaarcc4423a2019-11-26 17:05:00 +01002421 case CMD_lockvar:
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002422 case CMD_lua:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002423 case CMD_match:
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002424 case CMD_mzscheme:
Bram Moolenaar5803ae62014-03-23 16:04:02 +01002425 case CMD_noautocmd:
2426 case CMD_noswapfile:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002427 case CMD_perl:
2428 case CMD_psearch:
2429 case CMD_python:
Bram Moolenaar368373e2010-07-19 20:46:22 +02002430 case CMD_py3:
Bram Moolenaarb6590522010-07-21 16:00:43 +02002431 case CMD_python3:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002432 case CMD_return:
2433 case CMD_rightbelow:
2434 case CMD_ruby:
2435 case CMD_silent:
2436 case CMD_smagic:
2437 case CMD_snomagic:
2438 case CMD_substitute:
2439 case CMD_syntax:
Bram Moolenaara226a6d2006-02-26 23:59:20 +00002440 case CMD_tab:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002441 case CMD_tcl:
2442 case CMD_throw:
2443 case CMD_tilde:
2444 case CMD_topleft:
2445 case CMD_unlet:
Bram Moolenaarcc4423a2019-11-26 17:05:00 +01002446 case CMD_unlockvar:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002447 case CMD_verbose:
2448 case CMD_vertical:
Bram Moolenaar12bde492011-06-13 01:19:56 +02002449 case CMD_wincmd:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002450 break;
2451
2452 default: goto doend;
2453 }
2454 }
2455#endif
2456
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002457 if (ea.argt & EX_XFILE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002458 {
2459 if (expand_filename(&ea, cmdlinep, &errormsg) == FAIL)
2460 goto doend;
2461 }
2462
Bram Moolenaar071d4272004-06-13 20:20:40 +00002463 /*
2464 * Accept buffer name. Cannot be used at the same time with a buffer
2465 * number. Don't do this for a user command.
2466 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002467 if ((ea.argt & EX_BUFNAME) && *ea.arg != NUL && ea.addr_count == 0
Bram Moolenaar958636c2014-10-21 20:01:58 +02002468 && !IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002469 {
2470 /*
2471 * :bdelete, :bwipeout and :bunload take several arguments, separated
2472 * by spaces: find next space (skipping over escaped characters).
2473 * The others take one argument: ignore trailing spaces.
2474 */
2475 if (ea.cmdidx == CMD_bdelete || ea.cmdidx == CMD_bwipeout
2476 || ea.cmdidx == CMD_bunload)
2477 p = skiptowhite_esc(ea.arg);
2478 else
2479 {
2480 p = ea.arg + STRLEN(ea.arg);
Bram Moolenaar1c465442017-03-12 20:10:05 +01002481 while (p > ea.arg && VIM_ISWHITE(p[-1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002482 --p;
2483 }
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002484 ea.line2 = buflist_findpat(ea.arg, p, (ea.argt & EX_BUFUNL) != 0,
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002485 FALSE, FALSE);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002486 if (ea.line2 < 0) // failed
Bram Moolenaar071d4272004-06-13 20:20:40 +00002487 goto doend;
2488 ea.addr_count = 1;
2489 ea.arg = skipwhite(p);
2490 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002491
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002492 // The :try command saves the emsg_silent flag, reset it here when
2493 // ":silent! try" was used, it should only apply to :try itself.
Bram Moolenaareffed932018-08-14 13:38:17 +02002494 if (ea.cmdidx == CMD_try && ea.did_esilent > 0)
Bram Moolenaar2be57332018-02-13 18:05:18 +01002495 {
Bram Moolenaareffed932018-08-14 13:38:17 +02002496 emsg_silent -= ea.did_esilent;
Bram Moolenaar2be57332018-02-13 18:05:18 +01002497 if (emsg_silent < 0)
2498 emsg_silent = 0;
Bram Moolenaareffed932018-08-14 13:38:17 +02002499 ea.did_esilent = 0;
Bram Moolenaar2be57332018-02-13 18:05:18 +01002500 }
2501
Bram Moolenaar071d4272004-06-13 20:20:40 +00002502/*
Bram Moolenaar2be57332018-02-13 18:05:18 +01002503 * 7. Execute the command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002504 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002505
Bram Moolenaar958636c2014-10-21 20:01:58 +02002506 if (IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002507 {
2508 /*
2509 * Execute a user-defined command.
2510 */
2511 do_ucmd(&ea);
2512 }
2513 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002514 {
2515 /*
Bram Moolenaarac9fb182019-04-27 13:04:13 +02002516 * Call the function to execute the builtin command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002517 */
2518 ea.errmsg = NULL;
2519 (cmdnames[ea.cmdidx].cmd_func)(&ea);
2520 if (ea.errmsg != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002521 errormsg = _(ea.errmsg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002522 }
2523
2524#ifdef FEAT_EVAL
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002525 // Set flag that any command was executed, used by ex_vim9script().
Bram Moolenaar16531552020-02-08 16:00:46 +01002526 if (getline_equal(ea.getline, ea.cookie, getsourceline)
2527 && current_sctx.sc_sid > 0)
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002528 SCRIPT_ITEM(current_sctx.sc_sid)->sn_had_command = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002529
Bram Moolenaar071d4272004-06-13 20:20:40 +00002530 /*
2531 * If the command just executed called do_cmdline(), any throw or ":return"
2532 * or ":finish" encountered there must also check the cstack of the still
2533 * active do_cmdline() that called this do_one_cmd(). Rethrow an uncaught
2534 * exception, or reanimate a returned function or finished script file and
2535 * return or finish it again.
2536 */
2537 if (need_rethrow)
2538 do_throw(cstack);
2539 else if (check_cstack)
2540 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002541 if (source_finished(fgetline, cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002542 do_finish(&ea, TRUE);
Bram Moolenaar89d40322006-08-29 15:30:07 +00002543 else if (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002544 && current_func_returned())
2545 do_return(&ea, TRUE, FALSE, NULL);
2546 }
2547 need_rethrow = check_cstack = FALSE;
2548#endif
2549
2550doend:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002551 if (curwin->w_cursor.lnum == 0) // can happen with zero line number
Bram Moolenaar6de5e122017-04-20 21:55:44 +02002552 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002553 curwin->w_cursor.lnum = 1;
Bram Moolenaar6de5e122017-04-20 21:55:44 +02002554 curwin->w_cursor.col = 0;
2555 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002556
2557 if (errormsg != NULL && *errormsg != NUL && !did_emsg)
2558 {
2559 if (sourcing)
2560 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002561 if (errormsg != (char *)IObuff)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002562 {
2563 STRCPY(IObuff, errormsg);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002564 errormsg = (char *)IObuff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002565 }
Bram Moolenaara6f4d612011-09-21 19:10:46 +02002566 append_command(*cmdlinep);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002567 }
2568 emsg(errormsg);
2569 }
2570#ifdef FEAT_EVAL
2571 do_errthrow(cstack,
Bram Moolenaar958636c2014-10-21 20:01:58 +02002572 (ea.cmdidx != CMD_SIZE && !IS_USER_CMDIDX(ea.cmdidx))
2573 ? cmdnames[(int)ea.cmdidx].cmd_name : (char_u *)NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002574#endif
2575
Bram Moolenaareffed932018-08-14 13:38:17 +02002576 if (ea.verbose_save >= 0)
2577 p_verbose = ea.verbose_save;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002578
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002579 free_cmdmod();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002580 cmdmod = save_cmdmod;
Bram Moolenaar9a2c0912019-03-30 14:26:18 +01002581 reg_executing = save_reg_executing;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002582
Bram Moolenaareffed932018-08-14 13:38:17 +02002583 if (ea.save_msg_silent != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002584 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002585 // messages could be enabled for a serious error, need to check if the
2586 // counters don't become negative
Bram Moolenaareffed932018-08-14 13:38:17 +02002587 if (!did_emsg || msg_silent > ea.save_msg_silent)
2588 msg_silent = ea.save_msg_silent;
2589 emsg_silent -= ea.did_esilent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002590 if (emsg_silent < 0)
2591 emsg_silent = 0;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002592 // Restore msg_scroll, it's set by file I/O commands, even when no
2593 // message is actually displayed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002594 msg_scroll = save_msg_scroll;
Bram Moolenaar77ab2802009-04-22 12:44:48 +00002595
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002596 // "silent reg" or "silent echo x" inside "redir" leaves msg_col
2597 // somewhere in the line. Put it back in the first column.
Bram Moolenaar77ab2802009-04-22 12:44:48 +00002598 if (redirecting())
2599 msg_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002600 }
2601
Bram Moolenaar7171abe2004-10-11 10:06:20 +00002602#ifdef HAVE_SANDBOX
Bram Moolenaareffed932018-08-14 13:38:17 +02002603 if (ea.did_sandbox)
Bram Moolenaar7171abe2004-10-11 10:06:20 +00002604 --sandbox;
2605#endif
2606
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002607 if (ea.nextcmd && *ea.nextcmd == NUL) // not really a next command
Bram Moolenaar071d4272004-06-13 20:20:40 +00002608 ea.nextcmd = NULL;
2609
2610#ifdef FEAT_EVAL
2611 --ex_nesting_level;
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002612 vim_free(ea.cmdline_tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002613#endif
2614
2615 return ea.nextcmd;
2616}
2617#if (_MSC_VER == 1200)
Bram Moolenaar281bdce2005-01-25 21:53:18 +00002618 #pragma optimize( "", on )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002619#endif
2620
2621/*
Bram Moolenaareffed932018-08-14 13:38:17 +02002622 * Parse and skip over command modifiers:
2623 * - update eap->cmd
2624 * - store flags in "cmdmod".
2625 * - Set ex_pressedreturn for an empty command line.
2626 * - set msg_silent for ":silent"
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002627 * - set 'eventignore' to "all" for ":noautocmd"
Bram Moolenaareffed932018-08-14 13:38:17 +02002628 * - set p_verbose for ":verbose"
2629 * - Increment "sandbox" for ":sandbox"
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002630 * When "skip_only" is TRUE the global variables are not changed, except for
2631 * "cmdmod".
Bram Moolenaareffed932018-08-14 13:38:17 +02002632 * Return FAIL when the command is not to be executed.
2633 * May set "errormsg" to an error message.
2634 */
2635 int
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002636parse_command_modifiers(exarg_T *eap, char **errormsg, int skip_only)
Bram Moolenaareffed932018-08-14 13:38:17 +02002637{
2638 char_u *p;
2639
Bram Moolenaara80faa82020-04-12 19:37:17 +02002640 CLEAR_FIELD(cmdmod);
Bram Moolenaareffed932018-08-14 13:38:17 +02002641 eap->verbose_save = -1;
2642 eap->save_msg_silent = -1;
2643
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002644 // Repeat until no more command modifiers are found.
Bram Moolenaareffed932018-08-14 13:38:17 +02002645 for (;;)
2646 {
Bram Moolenaareffed932018-08-14 13:38:17 +02002647 while (*eap->cmd == ' ' || *eap->cmd == '\t' || *eap->cmd == ':')
2648 ++eap->cmd;
2649
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002650 // in ex mode, an empty line works like :+
Bram Moolenaareffed932018-08-14 13:38:17 +02002651 if (*eap->cmd == NUL && exmode_active
2652 && (getline_equal(eap->getline, eap->cookie, getexmodeline)
2653 || getline_equal(eap->getline, eap->cookie, getexline))
2654 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
2655 {
2656 eap->cmd = (char_u *)"+";
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002657 if (!skip_only)
2658 ex_pressedreturn = TRUE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002659 }
2660
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002661 // ignore comment and empty lines
Bram Moolenaareffed932018-08-14 13:38:17 +02002662 if (*eap->cmd == '"')
2663 return FAIL;
2664 if (*eap->cmd == NUL)
2665 {
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002666 if (!skip_only)
2667 ex_pressedreturn = TRUE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002668 return FAIL;
2669 }
2670
Bram Moolenaareffed932018-08-14 13:38:17 +02002671 p = skip_range(eap->cmd, NULL);
2672 switch (*p)
2673 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002674 // When adding an entry, also modify cmd_exists().
Bram Moolenaareffed932018-08-14 13:38:17 +02002675 case 'a': if (!checkforcmd(&eap->cmd, "aboveleft", 3))
2676 break;
2677 cmdmod.split |= WSP_ABOVE;
2678 continue;
2679
2680 case 'b': if (checkforcmd(&eap->cmd, "belowright", 3))
2681 {
2682 cmdmod.split |= WSP_BELOW;
2683 continue;
2684 }
2685 if (checkforcmd(&eap->cmd, "browse", 3))
2686 {
2687#ifdef FEAT_BROWSE_CMD
2688 cmdmod.browse = TRUE;
2689#endif
2690 continue;
2691 }
2692 if (!checkforcmd(&eap->cmd, "botright", 2))
2693 break;
2694 cmdmod.split |= WSP_BOT;
2695 continue;
2696
2697 case 'c': if (!checkforcmd(&eap->cmd, "confirm", 4))
2698 break;
2699#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
2700 cmdmod.confirm = TRUE;
2701#endif
2702 continue;
2703
2704 case 'k': if (checkforcmd(&eap->cmd, "keepmarks", 3))
2705 {
2706 cmdmod.keepmarks = TRUE;
2707 continue;
2708 }
2709 if (checkforcmd(&eap->cmd, "keepalt", 5))
2710 {
2711 cmdmod.keepalt = TRUE;
2712 continue;
2713 }
2714 if (checkforcmd(&eap->cmd, "keeppatterns", 5))
2715 {
2716 cmdmod.keeppatterns = TRUE;
2717 continue;
2718 }
2719 if (!checkforcmd(&eap->cmd, "keepjumps", 5))
2720 break;
2721 cmdmod.keepjumps = TRUE;
2722 continue;
2723
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002724 case 'f': // only accept ":filter {pat} cmd"
Bram Moolenaareffed932018-08-14 13:38:17 +02002725 {
2726 char_u *reg_pat;
2727
2728 if (!checkforcmd(&p, "filter", 4)
2729 || *p == NUL || ends_excmd(*p))
2730 break;
2731 if (*p == '!')
2732 {
2733 cmdmod.filter_force = TRUE;
2734 p = skipwhite(p + 1);
2735 if (*p == NUL || ends_excmd(*p))
2736 break;
2737 }
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002738 if (skip_only)
2739 p = skip_vimgrep_pat(p, NULL, NULL);
2740 else
2741 // NOTE: This puts a NUL after the pattern.
2742 p = skip_vimgrep_pat(p, &reg_pat, NULL);
Bram Moolenaareffed932018-08-14 13:38:17 +02002743 if (p == NULL || *p == NUL)
2744 break;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002745 if (!skip_only)
2746 {
2747 cmdmod.filter_regmatch.regprog =
Bram Moolenaareffed932018-08-14 13:38:17 +02002748 vim_regcomp(reg_pat, RE_MAGIC);
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002749 if (cmdmod.filter_regmatch.regprog == NULL)
2750 break;
2751 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002752 eap->cmd = p;
2753 continue;
2754 }
2755
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002756 // ":hide" and ":hide | cmd" are not modifiers
Bram Moolenaareffed932018-08-14 13:38:17 +02002757 case 'h': if (p != eap->cmd || !checkforcmd(&p, "hide", 3)
2758 || *p == NUL || ends_excmd(*p))
2759 break;
2760 eap->cmd = p;
2761 cmdmod.hide = TRUE;
2762 continue;
2763
2764 case 'l': if (checkforcmd(&eap->cmd, "lockmarks", 3))
2765 {
2766 cmdmod.lockmarks = TRUE;
2767 continue;
2768 }
2769
2770 if (!checkforcmd(&eap->cmd, "leftabove", 5))
2771 break;
2772 cmdmod.split |= WSP_ABOVE;
2773 continue;
2774
2775 case 'n': if (checkforcmd(&eap->cmd, "noautocmd", 3))
2776 {
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002777 if (cmdmod.save_ei == NULL && !skip_only)
Bram Moolenaareffed932018-08-14 13:38:17 +02002778 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002779 // Set 'eventignore' to "all". Restore the
2780 // existing option value later.
Bram Moolenaareffed932018-08-14 13:38:17 +02002781 cmdmod.save_ei = vim_strsave(p_ei);
2782 set_string_option_direct((char_u *)"ei", -1,
2783 (char_u *)"all", OPT_FREE, SID_NONE);
2784 }
2785 continue;
2786 }
2787 if (!checkforcmd(&eap->cmd, "noswapfile", 3))
2788 break;
2789 cmdmod.noswapfile = TRUE;
2790 continue;
2791
2792 case 'r': if (!checkforcmd(&eap->cmd, "rightbelow", 6))
2793 break;
2794 cmdmod.split |= WSP_BELOW;
2795 continue;
2796
2797 case 's': if (checkforcmd(&eap->cmd, "sandbox", 3))
2798 {
2799#ifdef HAVE_SANDBOX
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002800 if (!skip_only)
2801 {
2802 if (!eap->did_sandbox)
2803 ++sandbox;
2804 eap->did_sandbox = TRUE;
2805 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002806#endif
2807 continue;
2808 }
2809 if (!checkforcmd(&eap->cmd, "silent", 3))
2810 break;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002811 if (!skip_only)
2812 {
2813 if (eap->save_msg_silent == -1)
2814 eap->save_msg_silent = msg_silent;
2815 ++msg_silent;
2816 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002817 if (*eap->cmd == '!' && !VIM_ISWHITE(eap->cmd[-1]))
2818 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002819 // ":silent!", but not "silent !cmd"
Bram Moolenaareffed932018-08-14 13:38:17 +02002820 eap->cmd = skipwhite(eap->cmd + 1);
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002821 if (!skip_only)
2822 {
2823 ++emsg_silent;
2824 ++eap->did_esilent;
2825 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002826 }
2827 continue;
2828
2829 case 't': if (checkforcmd(&p, "tab", 3))
2830 {
Bram Moolenaar548e5982018-12-26 21:45:00 +01002831 if (!skip_only)
Bram Moolenaareffed932018-08-14 13:38:17 +02002832 {
Bram Moolenaar548e5982018-12-26 21:45:00 +01002833 long tabnr = get_address(eap, &eap->cmd,
2834 ADDR_TABS, eap->skip,
2835 skip_only, FALSE, 1);
2836 if (tabnr == MAXLNUM)
2837 cmdmod.tab = tabpage_index(curtab) + 1;
2838 else
Bram Moolenaareffed932018-08-14 13:38:17 +02002839 {
Bram Moolenaar548e5982018-12-26 21:45:00 +01002840 if (tabnr < 0 || tabnr > LAST_TAB_NR)
2841 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002842 *errormsg = _(e_invrange);
Bram Moolenaar548e5982018-12-26 21:45:00 +01002843 return FAIL;
2844 }
2845 cmdmod.tab = tabnr + 1;
Bram Moolenaareffed932018-08-14 13:38:17 +02002846 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002847 }
2848 eap->cmd = p;
2849 continue;
2850 }
2851 if (!checkforcmd(&eap->cmd, "topleft", 2))
2852 break;
2853 cmdmod.split |= WSP_TOP;
2854 continue;
2855
2856 case 'u': if (!checkforcmd(&eap->cmd, "unsilent", 3))
2857 break;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002858 if (!skip_only)
2859 {
2860 if (eap->save_msg_silent == -1)
2861 eap->save_msg_silent = msg_silent;
2862 msg_silent = 0;
2863 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002864 continue;
2865
2866 case 'v': if (checkforcmd(&eap->cmd, "vertical", 4))
2867 {
2868 cmdmod.split |= WSP_VERT;
2869 continue;
2870 }
2871 if (!checkforcmd(&p, "verbose", 4))
2872 break;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002873 if (!skip_only)
2874 {
2875 if (eap->verbose_save < 0)
2876 eap->verbose_save = p_verbose;
2877 if (vim_isdigit(*eap->cmd))
2878 p_verbose = atoi((char *)eap->cmd);
2879 else
2880 p_verbose = 1;
2881 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002882 eap->cmd = p;
2883 continue;
2884 }
2885 break;
2886 }
2887
2888 return OK;
2889}
2890
2891/*
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002892 * Free contents of "cmdmod".
2893 */
2894 static void
2895free_cmdmod(void)
2896{
2897 if (cmdmod.save_ei != NULL)
2898 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002899 // Restore 'eventignore' to the value before ":noautocmd".
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002900 set_string_option_direct((char_u *)"ei", -1, cmdmod.save_ei,
2901 OPT_FREE, SID_NONE);
2902 free_string_option(cmdmod.save_ei);
2903 }
2904
2905 if (cmdmod.filter_regmatch.regprog != NULL)
2906 vim_regfree(cmdmod.filter_regmatch.regprog);
2907}
2908
2909/*
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002910 * Parse the address range, if any, in "eap".
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02002911 * May set the last search pattern, unless "silent" is TRUE.
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002912 * Return FAIL and set "errormsg" or return OK.
2913 */
2914 int
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002915parse_cmd_address(exarg_T *eap, char **errormsg, int silent)
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002916{
2917 int address_count = 1;
2918 linenr_T lnum;
2919
2920 // Repeat for all ',' or ';' separated addresses.
2921 for (;;)
2922 {
2923 eap->line1 = eap->line2;
2924 switch (eap->addr_type)
2925 {
2926 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02002927 case ADDR_OTHER:
Bram Moolenaarb5383b12020-05-18 19:46:48 +02002928 // Default is the cursor line number. Avoid using an invalid
2929 // line number though.
2930 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
2931 eap->line2 = curbuf->b_ml.ml_line_count;
2932 else
2933 eap->line2 = curwin->w_cursor.lnum;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002934 break;
2935 case ADDR_WINDOWS:
2936 eap->line2 = CURRENT_WIN_NR;
2937 break;
2938 case ADDR_ARGUMENTS:
2939 eap->line2 = curwin->w_arg_idx + 1;
2940 if (eap->line2 > ARGCOUNT)
2941 eap->line2 = ARGCOUNT;
2942 break;
2943 case ADDR_LOADED_BUFFERS:
2944 case ADDR_BUFFERS:
2945 eap->line2 = curbuf->b_fnum;
2946 break;
2947 case ADDR_TABS:
2948 eap->line2 = CURRENT_TAB_NR;
2949 break;
2950 case ADDR_TABS_RELATIVE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02002951 case ADDR_UNSIGNED:
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002952 eap->line2 = 1;
2953 break;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002954 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02002955#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02002956 eap->line2 = qf_get_cur_idx(eap);
2957#endif
2958 break;
2959 case ADDR_QUICKFIX_VALID:
2960#ifdef FEAT_QUICKFIX
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002961 eap->line2 = qf_get_cur_valid_idx(eap);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002962#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02002963 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02002964 case ADDR_NONE:
2965 // Will give an error later if a range is found.
2966 break;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002967 }
2968 eap->cmd = skipwhite(eap->cmd);
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02002969 lnum = get_address(eap, &eap->cmd, eap->addr_type, eap->skip, silent,
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002970 eap->addr_count == 0, address_count++);
2971 if (eap->cmd == NULL) // error detected
2972 return FAIL;
2973 if (lnum == MAXLNUM)
2974 {
2975 if (*eap->cmd == '%') // '%' - all lines
2976 {
2977 ++eap->cmd;
2978 switch (eap->addr_type)
2979 {
2980 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02002981 case ADDR_OTHER:
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002982 eap->line1 = 1;
2983 eap->line2 = curbuf->b_ml.ml_line_count;
2984 break;
2985 case ADDR_LOADED_BUFFERS:
2986 {
2987 buf_T *buf = firstbuf;
2988
2989 while (buf->b_next != NULL
2990 && buf->b_ml.ml_mfp == NULL)
2991 buf = buf->b_next;
2992 eap->line1 = buf->b_fnum;
2993 buf = lastbuf;
2994 while (buf->b_prev != NULL
2995 && buf->b_ml.ml_mfp == NULL)
2996 buf = buf->b_prev;
2997 eap->line2 = buf->b_fnum;
2998 break;
2999 }
3000 case ADDR_BUFFERS:
3001 eap->line1 = firstbuf->b_fnum;
3002 eap->line2 = lastbuf->b_fnum;
3003 break;
3004 case ADDR_WINDOWS:
3005 case ADDR_TABS:
3006 if (IS_USER_CMDIDX(eap->cmdidx))
3007 {
3008 eap->line1 = 1;
3009 eap->line2 = eap->addr_type == ADDR_WINDOWS
3010 ? LAST_WIN_NR : LAST_TAB_NR;
3011 }
3012 else
3013 {
3014 // there is no Vim command which uses '%' and
3015 // ADDR_WINDOWS or ADDR_TABS
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003016 *errormsg = _(e_invrange);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003017 return FAIL;
3018 }
3019 break;
3020 case ADDR_TABS_RELATIVE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02003021 case ADDR_UNSIGNED:
3022 case ADDR_QUICKFIX:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003023 *errormsg = _(e_invrange);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003024 return FAIL;
3025 case ADDR_ARGUMENTS:
3026 if (ARGCOUNT == 0)
3027 eap->line1 = eap->line2 = 0;
3028 else
3029 {
3030 eap->line1 = 1;
3031 eap->line2 = ARGCOUNT;
3032 }
3033 break;
Bram Moolenaar25190db2019-05-04 15:05:28 +02003034 case ADDR_QUICKFIX_VALID:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003035#ifdef FEAT_QUICKFIX
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003036 eap->line1 = 1;
Bram Moolenaar25190db2019-05-04 15:05:28 +02003037 eap->line2 = qf_get_valid_size(eap);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003038 if (eap->line2 == 0)
3039 eap->line2 = 1;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003040#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003041 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02003042 case ADDR_NONE:
3043 // Will give an error later if a range is found.
3044 break;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003045 }
3046 ++eap->addr_count;
3047 }
3048 else if (*eap->cmd == '*' && vim_strchr(p_cpo, CPO_STAR) == NULL)
3049 {
3050 pos_T *fp;
3051
3052 // '*' - visual area
3053 if (eap->addr_type != ADDR_LINES)
3054 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003055 *errormsg = _(e_invrange);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003056 return FAIL;
3057 }
3058
3059 ++eap->cmd;
3060 if (!eap->skip)
3061 {
3062 fp = getmark('<', FALSE);
3063 if (check_mark(fp) == FAIL)
3064 return FAIL;
3065 eap->line1 = fp->lnum;
3066 fp = getmark('>', FALSE);
3067 if (check_mark(fp) == FAIL)
3068 return FAIL;
3069 eap->line2 = fp->lnum;
3070 ++eap->addr_count;
3071 }
3072 }
3073 }
3074 else
3075 eap->line2 = lnum;
3076 eap->addr_count++;
3077
3078 if (*eap->cmd == ';')
3079 {
3080 if (!eap->skip)
3081 {
3082 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaar0e717042020-04-27 19:29:01 +02003083 // Don't leave the cursor on an illegal line or column, but do
3084 // accept zero as address, so 0;/PATTERN/ works correctly.
3085 if (eap->line2 > 0)
3086 check_cursor();
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003087 }
3088 }
3089 else if (*eap->cmd != ',')
3090 break;
3091 ++eap->cmd;
3092 }
3093
3094 // One address given: set start and end lines.
3095 if (eap->addr_count == 1)
3096 {
3097 eap->line1 = eap->line2;
3098 // ... but only implicit: really no address given
3099 if (lnum == MAXLNUM)
3100 eap->addr_count = 0;
3101 }
3102 return OK;
3103}
3104
3105/*
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00003106 * Check for an Ex command with optional tail.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003107 * If there is a match advance "pp" to the argument and return TRUE.
3108 */
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00003109 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003110checkforcmd(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003111 char_u **pp, // start of command
3112 char *cmd, // name of command
3113 int len) // required length
Bram Moolenaar071d4272004-06-13 20:20:40 +00003114{
3115 int i;
3116
3117 for (i = 0; cmd[i] != NUL; ++i)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003118 if (((char_u *)cmd)[i] != (*pp)[i])
Bram Moolenaar071d4272004-06-13 20:20:40 +00003119 break;
3120 if (i >= len && !isalpha((*pp)[i]))
3121 {
3122 *pp = skipwhite(*pp + i);
3123 return TRUE;
3124 }
3125 return FALSE;
3126}
3127
3128/*
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003129 * Append "cmd" to the error message in IObuff.
3130 * Takes care of limiting the length and handling 0xa0, which would be
3131 * invisible otherwise.
3132 */
3133 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003134append_command(char_u *cmd)
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003135{
3136 char_u *s = cmd;
3137 char_u *d;
3138
3139 STRCAT(IObuff, ": ");
3140 d = IObuff + STRLEN(IObuff);
3141 while (*s != NUL && d - IObuff < IOSIZE - 7)
3142 {
Bram Moolenaar13505972019-01-24 15:04:48 +01003143 if (enc_utf8 ? (s[0] == 0xc2 && s[1] == 0xa0) : *s == 0xa0)
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003144 {
Bram Moolenaar13505972019-01-24 15:04:48 +01003145 s += enc_utf8 ? 2 : 1;
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003146 STRCPY(d, "<a0>");
3147 d += 4;
3148 }
3149 else
3150 MB_COPY_CHAR(s, d);
3151 }
3152 *d = NUL;
3153}
3154
3155/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003156 * Find an Ex command by its name, either built-in or user.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003157 * Start of the name can be found at eap->cmd.
Bram Moolenaar25190db2019-05-04 15:05:28 +02003158 * Sets eap->cmdidx and returns a pointer to char after the command name.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003159 * "full" is set to TRUE if the whole command name matched.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003160 *
3161 * If "lookup" is not NULL recognize expression without "eval" or "call" and
3162 * assignment without "let". Sets eap->cmdidx to the command while returning
3163 * "eap->cmd".
3164 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00003165 * Returns NULL for an ambiguous user command.
3166 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003167 char_u *
3168find_ex_command(
3169 exarg_T *eap,
Bram Moolenaara494f562020-04-18 17:45:38 +02003170 int *full UNUSED,
Bram Moolenaarb84a3812020-05-01 15:44:29 +02003171 void *(*lookup)(char_u *, size_t, cctx_T *) UNUSED,
Bram Moolenaara494f562020-04-18 17:45:38 +02003172 cctx_T *cctx UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003173{
3174 int len;
3175 char_u *p;
Bram Moolenaardf177f62005-02-22 08:39:57 +00003176 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003177
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003178#ifdef FEAT_EVAL
3179 /*
3180 * Recognize a Vim9 script function/method call and assignment:
3181 * "lvar = value", "lvar(arg)", "[1, 2 3]->Func()"
3182 */
Bram Moolenaar0c6ceaf2020-02-22 18:36:32 +01003183 p = eap->cmd;
3184 if (lookup != NULL && (*p == '('
3185 || ((p = to_name_const_end(eap->cmd)) > eap->cmd && *p != NUL)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003186 {
3187 int oplen;
3188 int heredoc;
3189
3190 // "funcname(" is always a function call.
3191 // "varname[]" is an expression.
3192 // "g:varname" is an expression.
3193 // "varname->expr" is an expression.
Bram Moolenaar0c6ceaf2020-02-22 18:36:32 +01003194 // "(..." is an expression.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003195 if (*p == '('
3196 || *p == '['
3197 || p[1] == ':'
3198 || (*p == '-' && p[1] == '>'))
3199 {
3200 eap->cmdidx = CMD_eval;
3201 return eap->cmd;
3202 }
3203
3204 oplen = assignment_len(skipwhite(p), &heredoc);
3205 if (oplen > 0)
3206 {
3207 // Recognize an assignment if we recognize the variable name:
3208 // "g:var = expr"
3209 // "var = expr" where "var" is a local var name.
3210 if (((p - eap->cmd) > 2 && eap->cmd[1] == ':')
Bram Moolenaarb84a3812020-05-01 15:44:29 +02003211 || lookup(eap->cmd, p - eap->cmd, cctx) != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003212 {
3213 eap->cmdidx = CMD_let;
3214 return eap->cmd;
3215 }
3216 }
3217 }
3218#endif
3219
Bram Moolenaar071d4272004-06-13 20:20:40 +00003220 /*
3221 * Isolate the command and search for it in the command table.
Bram Moolenaar81870892007-11-11 18:17:28 +00003222 * Exceptions:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003223 * - the 'k' command can directly be followed by any character.
3224 * - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003225 * but :sre[wind] is another command, as are :scr[iptnames],
Bram Moolenaar071d4272004-06-13 20:20:40 +00003226 * :scs[cope], :sim[alt], :sig[ns] and :sil[ent].
Bram Moolenaardf177f62005-02-22 08:39:57 +00003227 * - the "d" command can directly be followed by 'l' or 'p' flag.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003228 */
3229 p = eap->cmd;
3230 if (*p == 'k')
3231 {
3232 eap->cmdidx = CMD_k;
3233 ++p;
3234 }
3235 else if (p[0] == 's'
Bram Moolenaar204b93f2015-08-04 22:02:51 +02003236 && ((p[1] == 'c' && (p[2] == NUL || (p[2] != 's' && p[2] != 'r'
3237 && (p[3] == NUL || (p[3] != 'i' && p[4] != 'p')))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003238 || p[1] == 'g'
3239 || (p[1] == 'i' && p[2] != 'm' && p[2] != 'l' && p[2] != 'g')
3240 || p[1] == 'I'
3241 || (p[1] == 'r' && p[2] != 'e')))
3242 {
3243 eap->cmdidx = CMD_substitute;
3244 ++p;
3245 }
3246 else
3247 {
3248 while (ASCII_ISALPHA(*p))
3249 ++p;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003250 // for python 3.x support ":py3", ":python3", ":py3file", etc.
Bram Moolenaar55d5c032010-07-17 23:52:29 +02003251 if (eap->cmd[0] == 'p' && eap->cmd[1] == 'y')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003252 {
Bram Moolenaarb6590522010-07-21 16:00:43 +02003253 while (ASCII_ISALNUM(*p))
3254 ++p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003255 }
3256 else if (*p == '9' && STRNCMP("vim9", eap->cmd, 4) == 0)
3257 {
3258 // include "9" for "vim9script"
3259 ++p;
3260 while (ASCII_ISALPHA(*p))
3261 ++p;
3262 }
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02003263
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003264 // check for non-alpha command
Bram Moolenaar071d4272004-06-13 20:20:40 +00003265 if (p == eap->cmd && vim_strchr((char_u *)"@*!=><&~#", *p) != NULL)
3266 ++p;
3267 len = (int)(p - eap->cmd);
Bram Moolenaardf177f62005-02-22 08:39:57 +00003268 if (*eap->cmd == 'd' && (p[-1] == 'l' || p[-1] == 'p'))
3269 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003270 // Check for ":dl", ":dell", etc. to ":deletel": that's
3271 // :delete with the 'l' flag. Same for 'p'.
Bram Moolenaardf177f62005-02-22 08:39:57 +00003272 for (i = 0; i < len; ++i)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003273 if (eap->cmd[i] != ((char_u *)"delete")[i])
Bram Moolenaardf177f62005-02-22 08:39:57 +00003274 break;
3275 if (i == len - 1)
3276 {
3277 --len;
3278 if (p[-1] == 'l')
3279 eap->flags |= EXFLAG_LIST;
3280 else
3281 eap->flags |= EXFLAG_PRINT;
3282 }
3283 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003284
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003285 if (ASCII_ISLOWER(eap->cmd[0]))
3286 {
Bram Moolenaar6c0c1e82017-03-25 15:07:43 +01003287 int c1 = eap->cmd[0];
Bram Moolenaar94f82cb2019-07-24 22:30:27 +02003288 int c2 = len == 1 ? NUL : eap->cmd[1];
Bram Moolenaar6c0c1e82017-03-25 15:07:43 +01003289
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003290 if (command_count != (int)CMD_SIZE)
3291 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003292 iemsg(_("E943: Command table needs to be updated, run 'make cmdidxs'"));
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003293 getout(1);
3294 }
3295
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003296 // Use a precomputed index for fast look-up in cmdnames[]
3297 // taking into account the first 2 letters of eap->cmd.
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003298 eap->cmdidx = cmdidxs1[CharOrdLow(c1)];
3299 if (ASCII_ISLOWER(c2))
3300 eap->cmdidx += cmdidxs2[CharOrdLow(c1)][CharOrdLow(c2)];
3301 }
Bram Moolenaara494f562020-04-18 17:45:38 +02003302 else if (ASCII_ISUPPER(eap->cmd[0]))
3303 eap->cmdidx = CMD_Next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003304 else
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003305 eap->cmdidx = CMD_bang;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003306
3307 for ( ; (int)eap->cmdidx < (int)CMD_SIZE;
3308 eap->cmdidx = (cmdidx_T)((int)eap->cmdidx + 1))
3309 if (STRNCMP(cmdnames[(int)eap->cmdidx].cmd_name, (char *)eap->cmd,
3310 (size_t)len) == 0)
3311 {
3312#ifdef FEAT_EVAL
3313 if (full != NULL
3314 && cmdnames[(int)eap->cmdidx].cmd_name[len] == NUL)
3315 *full = TRUE;
3316#endif
3317 break;
3318 }
3319
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003320 // Look for a user defined command as a last resort. Let ":Print" be
3321 // overruled by a user defined command.
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01003322 if ((eap->cmdidx == CMD_SIZE || eap->cmdidx == CMD_Print)
3323 && *eap->cmd >= 'A' && *eap->cmd <= 'Z')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003324 {
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003325 // User defined commands may contain digits.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003326 while (ASCII_ISALNUM(*p))
3327 ++p;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003328 p = find_ucmd(eap, p, full, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003329 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003330 if (p == eap->cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003331 eap->cmdidx = CMD_SIZE;
3332 }
3333
3334 return p;
3335}
3336
3337#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003338static struct cmdmod
3339{
3340 char *name;
3341 int minlen;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003342 int has_count; // :123verbose :3tab
Bram Moolenaared53fb92007-11-24 20:50:24 +00003343} cmdmods[] = {
3344 {"aboveleft", 3, FALSE},
3345 {"belowright", 3, FALSE},
3346 {"botright", 2, FALSE},
3347 {"browse", 3, FALSE},
3348 {"confirm", 4, FALSE},
Bram Moolenaar7b668e82016-08-23 23:51:21 +02003349 {"filter", 4, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003350 {"hide", 3, FALSE},
3351 {"keepalt", 5, FALSE},
3352 {"keepjumps", 5, FALSE},
3353 {"keepmarks", 3, FALSE},
Bram Moolenaara939e432013-11-09 05:30:26 +01003354 {"keeppatterns", 5, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003355 {"leftabove", 5, FALSE},
3356 {"lockmarks", 3, FALSE},
Bram Moolenaarca9f9582008-09-18 10:44:28 +00003357 {"noautocmd", 3, FALSE},
Bram Moolenaar5803ae62014-03-23 16:04:02 +01003358 {"noswapfile", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003359 {"rightbelow", 6, FALSE},
3360 {"sandbox", 3, FALSE},
3361 {"silent", 3, FALSE},
3362 {"tab", 3, TRUE},
3363 {"topleft", 2, FALSE},
Bram Moolenaar8e258a42009-07-09 13:55:43 +00003364 {"unsilent", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003365 {"verbose", 4, TRUE},
3366 {"vertical", 4, FALSE},
3367};
3368
3369/*
3370 * Return length of a command modifier (including optional count).
3371 * Return zero when it's not a modifier.
3372 */
3373 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003374modifier_len(char_u *cmd)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003375{
3376 int i, j;
3377 char_u *p = cmd;
3378
3379 if (VIM_ISDIGIT(*cmd))
3380 p = skipwhite(skipdigits(cmd));
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00003381 for (i = 0; i < (int)(sizeof(cmdmods) / sizeof(struct cmdmod)); ++i)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003382 {
3383 for (j = 0; p[j] != NUL; ++j)
3384 if (p[j] != cmdmods[i].name[j])
3385 break;
Bram Moolenaar2d473ab2013-06-12 17:12:24 +02003386 if (!ASCII_ISALPHA(p[j]) && j >= cmdmods[i].minlen
Bram Moolenaared53fb92007-11-24 20:50:24 +00003387 && (p == cmd || cmdmods[i].has_count))
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00003388 return j + (int)(p - cmd);
Bram Moolenaared53fb92007-11-24 20:50:24 +00003389 }
3390 return 0;
3391}
3392
Bram Moolenaar071d4272004-06-13 20:20:40 +00003393/*
3394 * Return > 0 if an Ex command "name" exists.
3395 * Return 2 if there is an exact match.
3396 * Return 3 if there is an ambiguous match.
3397 */
3398 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003399cmd_exists(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003400{
3401 exarg_T ea;
3402 int full = FALSE;
3403 int i;
3404 int j;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003405 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003406
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003407 // Check command modifiers.
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00003408 for (i = 0; i < (int)(sizeof(cmdmods) / sizeof(struct cmdmod)); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003409 {
3410 for (j = 0; name[j] != NUL; ++j)
3411 if (name[j] != cmdmods[i].name[j])
3412 break;
3413 if (name[j] == NUL && j >= cmdmods[i].minlen)
3414 return (cmdmods[i].name[j] == NUL ? 2 : 1);
3415 }
3416
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003417 // Check built-in commands and user defined commands.
3418 // For ":2match" and ":3match" we need to skip the number.
Bram Moolenaara9587612006-05-04 21:47:50 +00003419 ea.cmd = (*name == '2' || *name == '3') ? name + 1 : name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003420 ea.cmdidx = (cmdidx_T)0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003421 p = find_ex_command(&ea, &full, NULL, NULL);
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003422 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003423 return 3;
Bram Moolenaara9587612006-05-04 21:47:50 +00003424 if (vim_isdigit(*name) && ea.cmdidx != CMD_match)
3425 return 0;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003426 if (*skipwhite(p) != NUL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003427 return 0; // trailing garbage
Bram Moolenaar071d4272004-06-13 20:20:40 +00003428 return (ea.cmdidx == CMD_SIZE ? 0 : (full ? 2 : 1));
3429}
3430#endif
3431
Bram Moolenaard0190392019-08-23 21:17:35 +02003432 cmdidx_T
3433excmd_get_cmdidx(char_u *cmd, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003434{
Bram Moolenaard0190392019-08-23 21:17:35 +02003435 cmdidx_T idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003436
Bram Moolenaard0190392019-08-23 21:17:35 +02003437 for (idx = (cmdidx_T)0; (int)idx < (int)CMD_SIZE;
3438 idx = (cmdidx_T)((int)idx + 1))
3439 if (STRNCMP(cmdnames[(int)idx].cmd_name, cmd, (size_t)len) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003440 break;
3441
Bram Moolenaard0190392019-08-23 21:17:35 +02003442 return idx;
3443}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003444
Bram Moolenaard0190392019-08-23 21:17:35 +02003445 long
3446excmd_get_argt(cmdidx_T idx)
3447{
3448 return (long)cmdnames[(int)idx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003449}
3450
3451/*
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003452 * Skip a range specifier of the form: addr [,addr] [;addr] ..
Bram Moolenaar071d4272004-06-13 20:20:40 +00003453 *
3454 * Backslashed delimiters after / or ? will be skipped, and commands will
3455 * not be expanded between /'s and ?'s or after "'".
3456 *
Bram Moolenaardf069ee2020-06-22 23:02:51 +02003457 * Also skip white space and ":" characters after the range.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003458 * Returns the "cmd" pointer advanced to beyond the range.
3459 */
3460 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003461skip_range(
3462 char_u *cmd,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003463 int *ctx) // pointer to xp_context or NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003464{
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003465 unsigned delim;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003466
Bram Moolenaarcbf20fb2017-02-03 21:19:04 +01003467 while (vim_strchr((char_u *)" \t0123456789.$%'/?-+,;\\", *cmd) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003468 {
Bram Moolenaarcbf20fb2017-02-03 21:19:04 +01003469 if (*cmd == '\\')
3470 {
3471 if (cmd[1] == '?' || cmd[1] == '/' || cmd[1] == '&')
3472 ++cmd;
3473 else
3474 break;
3475 }
3476 else if (*cmd == '\'')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003477 {
3478 if (*++cmd == NUL && ctx != NULL)
3479 *ctx = EXPAND_NOTHING;
3480 }
3481 else if (*cmd == '/' || *cmd == '?')
3482 {
3483 delim = *cmd++;
3484 while (*cmd != NUL && *cmd != delim)
3485 if (*cmd++ == '\\' && *cmd != NUL)
3486 ++cmd;
3487 if (*cmd == NUL && ctx != NULL)
3488 *ctx = EXPAND_NOTHING;
3489 }
3490 if (*cmd != NUL)
3491 ++cmd;
3492 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00003493
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003494 // Skip ":" and white space.
Bram Moolenaardf177f62005-02-22 08:39:57 +00003495 while (*cmd == ':')
3496 cmd = skipwhite(cmd + 1);
3497
Bram Moolenaar071d4272004-06-13 20:20:40 +00003498 return cmd;
3499}
3500
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003501 static void
3502addr_error(cmd_addr_T addr_type)
3503{
3504 if (addr_type == ADDR_NONE)
3505 emsg(_(e_norange));
3506 else
3507 emsg(_(e_invrange));
3508}
3509
Bram Moolenaar071d4272004-06-13 20:20:40 +00003510/*
Bram Moolenaar198cb662018-09-06 21:44:17 +02003511 * Get a single EX address.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003512 *
3513 * Set ptr to the next character after the part that was interpreted.
3514 * Set ptr to NULL when an error is encountered.
Bram Moolenaar198cb662018-09-06 21:44:17 +02003515 * This may set the last used search pattern.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003516 *
3517 * Return MAXLNUM when no Ex address was found.
3518 */
3519 static linenr_T
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003520get_address(
3521 exarg_T *eap UNUSED,
3522 char_u **ptr,
Bram Moolenaar1b03a192019-12-08 17:08:29 +01003523 cmd_addr_T addr_type,
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02003524 int skip, // only skip the address, don't use it
3525 int silent, // no errors or side effects
3526 int to_other_file, // flag: may jump to other file
3527 int address_count UNUSED) // 1 for first address, >1 after comma
Bram Moolenaar071d4272004-06-13 20:20:40 +00003528{
3529 int c;
3530 int i;
3531 long n;
3532 char_u *cmd;
3533 pos_T pos;
3534 pos_T *fp;
3535 linenr_T lnum;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003536 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003537
3538 cmd = skipwhite(*ptr);
3539 lnum = MAXLNUM;
3540 do
3541 {
3542 switch (*cmd)
3543 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003544 case '.': // '.' - Cursor position
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003545 ++cmd;
3546 switch (addr_type)
3547 {
3548 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02003549 case ADDR_OTHER:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003550 lnum = curwin->w_cursor.lnum;
3551 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003552 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01003553 lnum = CURRENT_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003554 break;
3555 case ADDR_ARGUMENTS:
3556 lnum = curwin->w_arg_idx + 1;
3557 break;
3558 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003559 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003560 lnum = curbuf->b_fnum;
3561 break;
3562 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01003563 lnum = CURRENT_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003564 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02003565 case ADDR_NONE:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003566 case ADDR_TABS_RELATIVE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02003567 case ADDR_UNSIGNED:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003568 addr_error(addr_type);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01003569 cmd = NULL;
3570 goto error;
3571 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003572 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003573#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02003574 lnum = qf_get_cur_idx(eap);
3575#endif
3576 break;
3577 case ADDR_QUICKFIX_VALID:
3578#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003579 lnum = qf_get_cur_valid_idx(eap);
Bram Moolenaare906c502015-09-09 21:10:39 +02003580#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003581 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003582 }
3583 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003584
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003585 case '$': // '$' - last line
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003586 ++cmd;
3587 switch (addr_type)
3588 {
3589 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02003590 case ADDR_OTHER:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003591 lnum = curbuf->b_ml.ml_line_count;
3592 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003593 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01003594 lnum = LAST_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003595 break;
3596 case ADDR_ARGUMENTS:
3597 lnum = ARGCOUNT;
3598 break;
3599 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003600 buf = lastbuf;
3601 while (buf->b_ml.ml_mfp == NULL)
3602 {
3603 if (buf->b_prev == NULL)
3604 break;
3605 buf = buf->b_prev;
3606 }
3607 lnum = buf->b_fnum;
3608 break;
3609 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003610 lnum = lastbuf->b_fnum;
3611 break;
3612 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01003613 lnum = LAST_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003614 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02003615 case ADDR_NONE:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003616 case ADDR_TABS_RELATIVE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02003617 case ADDR_UNSIGNED:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003618 addr_error(addr_type);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01003619 cmd = NULL;
3620 goto error;
3621 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003622 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003623#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003624 lnum = qf_get_size(eap);
3625 if (lnum == 0)
3626 lnum = 1;
Bram Moolenaare906c502015-09-09 21:10:39 +02003627#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003628 break;
Bram Moolenaar25190db2019-05-04 15:05:28 +02003629 case ADDR_QUICKFIX_VALID:
3630#ifdef FEAT_QUICKFIX
3631 lnum = qf_get_valid_size(eap);
3632 if (lnum == 0)
3633 lnum = 1;
3634#endif
3635 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003636 }
3637 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003638
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003639 case '\'': // ''' - mark
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003640 if (*++cmd == NUL)
3641 {
3642 cmd = NULL;
3643 goto error;
3644 }
3645 if (addr_type != ADDR_LINES)
3646 {
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003647 addr_error(addr_type);
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01003648 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003649 goto error;
3650 }
3651 if (skip)
3652 ++cmd;
3653 else
3654 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003655 // Only accept a mark in another file when it is
3656 // used by itself: ":'M".
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003657 fp = getmark(*cmd, to_other_file && cmd[1] == NUL);
3658 ++cmd;
3659 if (fp == (pos_T *)-1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003660 // Jumped to another file.
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003661 lnum = curwin->w_cursor.lnum;
3662 else
3663 {
3664 if (check_mark(fp) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003665 {
3666 cmd = NULL;
3667 goto error;
3668 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003669 lnum = fp->lnum;
3670 }
3671 }
3672 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003673
3674 case '/':
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003675 case '?': // '/' or '?' - search
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003676 c = *cmd++;
3677 if (addr_type != ADDR_LINES)
3678 {
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003679 addr_error(addr_type);
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01003680 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003681 goto error;
3682 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003683 if (skip) // skip "/pat/"
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003684 {
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02003685 cmd = skip_regexp(cmd, c, (int)p_magic);
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003686 if (*cmd == c)
3687 ++cmd;
3688 }
3689 else
3690 {
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02003691 int flags;
3692
3693 pos = curwin->w_cursor; // save curwin->w_cursor
3694
3695 // When '/' or '?' follows another address, start from
3696 // there.
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003697 if (lnum != MAXLNUM)
3698 curwin->w_cursor.lnum = lnum;
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02003699
3700 // Start a forward search at the end of the line (unless
3701 // before the first line).
3702 // Start a backward search at the start of the line.
3703 // This makes sure we never match in the current
3704 // line, and can match anywhere in the
3705 // next/previous line.
Bram Moolenaar8ada6aa2017-12-19 21:23:21 +01003706 if (c == '/' && curwin->w_cursor.lnum > 0)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003707 curwin->w_cursor.col = MAXCOL;
3708 else
3709 curwin->w_cursor.col = 0;
3710 searchcmdlen = 0;
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02003711 flags = silent ? 0 : SEARCH_HIS | SEARCH_MSG;
Bram Moolenaarc036e872020-02-21 21:30:52 +01003712 if (!do_search(NULL, c, c, cmd, 1L, flags, NULL))
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003713 {
3714 curwin->w_cursor = pos;
3715 cmd = NULL;
3716 goto error;
3717 }
3718 lnum = curwin->w_cursor.lnum;
3719 curwin->w_cursor = pos;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003720 // adjust command string pointer
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003721 cmd += searchcmdlen;
3722 }
3723 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003724
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003725 case '\\': // "\?", "\/" or "\&", repeat search
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003726 ++cmd;
3727 if (addr_type != ADDR_LINES)
3728 {
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003729 addr_error(addr_type);
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01003730 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003731 goto error;
3732 }
3733 if (*cmd == '&')
3734 i = RE_SUBST;
3735 else if (*cmd == '?' || *cmd == '/')
3736 i = RE_SEARCH;
3737 else
3738 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003739 emsg(_(e_backslash));
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003740 cmd = NULL;
3741 goto error;
3742 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003743
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003744 if (!skip)
3745 {
3746 /*
3747 * When search follows another address, start from
3748 * there.
3749 */
3750 if (lnum != MAXLNUM)
3751 pos.lnum = lnum;
3752 else
3753 pos.lnum = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003754
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003755 /*
3756 * Start the search just like for the above
3757 * do_search().
3758 */
3759 if (*cmd != '?')
3760 pos.col = MAXCOL;
3761 else
3762 pos.col = 0;
Bram Moolenaarbd8539a2015-08-11 18:53:03 +02003763 pos.coladd = 0;
Bram Moolenaar5d24a222018-12-23 19:10:09 +01003764 if (searchit(curwin, curbuf, &pos, NULL,
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003765 *cmd == '?' ? BACKWARD : FORWARD,
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02003766 (char_u *)"", 1L, SEARCH_MSG, i, NULL) != FAIL)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003767 lnum = pos.lnum;
3768 else
3769 {
3770 cmd = NULL;
3771 goto error;
3772 }
3773 }
3774 ++cmd;
3775 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003776
3777 default:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003778 if (VIM_ISDIGIT(*cmd)) // absolute line number
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003779 lnum = getdigits(&cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003780 }
3781
3782 for (;;)
3783 {
3784 cmd = skipwhite(cmd);
3785 if (*cmd != '-' && *cmd != '+' && !VIM_ISDIGIT(*cmd))
3786 break;
3787
3788 if (lnum == MAXLNUM)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003789 {
3790 switch (addr_type)
3791 {
3792 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02003793 case ADDR_OTHER:
3794 // "+1" is same as ".+1"
Bram Moolenaarf240e182014-11-27 18:33:02 +01003795 lnum = curwin->w_cursor.lnum;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003796 break;
3797 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01003798 lnum = CURRENT_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003799 break;
3800 case ADDR_ARGUMENTS:
3801 lnum = curwin->w_arg_idx + 1;
3802 break;
3803 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003804 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003805 lnum = curbuf->b_fnum;
3806 break;
3807 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01003808 lnum = CURRENT_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003809 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01003810 case ADDR_TABS_RELATIVE:
3811 lnum = 1;
3812 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003813 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003814#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02003815 lnum = qf_get_cur_idx(eap);
3816#endif
3817 break;
3818 case ADDR_QUICKFIX_VALID:
3819#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003820 lnum = qf_get_cur_valid_idx(eap);
Bram Moolenaare906c502015-09-09 21:10:39 +02003821#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003822 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02003823 case ADDR_NONE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02003824 case ADDR_UNSIGNED:
3825 lnum = 0;
Bram Moolenaarb7316892019-05-01 18:08:42 +02003826 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003827 }
3828 }
3829
Bram Moolenaar071d4272004-06-13 20:20:40 +00003830 if (VIM_ISDIGIT(*cmd))
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003831 i = '+'; // "number" is same as "+number"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003832 else
3833 i = *cmd++;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003834 if (!VIM_ISDIGIT(*cmd)) // '+' is '+1', but '+0' is not '+1'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003835 n = 1;
3836 else
3837 n = getdigits(&cmd);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01003838
3839 if (addr_type == ADDR_TABS_RELATIVE)
3840 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003841 emsg(_(e_invrange));
Bram Moolenaar2f72c702017-01-29 14:48:10 +01003842 cmd = NULL;
3843 goto error;
3844 }
3845 else if (addr_type == ADDR_LOADED_BUFFERS
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003846 || addr_type == ADDR_BUFFERS)
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01003847 lnum = compute_buffer_local_count(
3848 addr_type, lnum, (i == '-') ? -1 * n : n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003849 else
Bram Moolenaarded27822017-01-02 14:27:34 +01003850 {
3851#ifdef FEAT_FOLDING
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003852 // Relative line addressing, need to adjust for folded lines
3853 // now, but only do it after the first address.
Bram Moolenaarded27822017-01-02 14:27:34 +01003854 if (addr_type == ADDR_LINES && (i == '-' || i == '+')
3855 && address_count >= 2)
3856 (void)hasFolding(lnum, NULL, &lnum);
3857#endif
3858 if (i == '-')
3859 lnum -= n;
3860 else
3861 lnum += n;
3862 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003863 }
3864 } while (*cmd == '/' || *cmd == '?');
3865
3866error:
3867 *ptr = cmd;
3868 return lnum;
3869}
3870
3871/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00003872 * Get flags from an Ex command argument.
3873 */
3874 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003875get_flags(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00003876{
3877 while (vim_strchr((char_u *)"lp#", *eap->arg) != NULL)
3878 {
3879 if (*eap->arg == 'l')
3880 eap->flags |= EXFLAG_LIST;
3881 else if (*eap->arg == 'p')
3882 eap->flags |= EXFLAG_PRINT;
3883 else
3884 eap->flags |= EXFLAG_NR;
3885 eap->arg = skipwhite(eap->arg + 1);
3886 }
3887}
3888
3889/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003890 * Function called for command which is Not Implemented. NI!
3891 */
3892 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003893ex_ni(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003894{
3895 if (!eap->skip)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003896 eap->errmsg = N_("E319: Sorry, the command is not available in this version");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003897}
3898
Bram Moolenaar7bb75552007-07-16 18:39:49 +00003899#ifdef HAVE_EX_SCRIPT_NI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003900/*
3901 * Function called for script command which is Not Implemented. NI!
3902 * Skips over ":perl <<EOF" constructs.
3903 */
3904 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003905ex_script_ni(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003906{
3907 if (!eap->skip)
3908 ex_ni(eap);
3909 else
3910 vim_free(script_get(eap, eap->arg));
3911}
3912#endif
3913
3914/*
3915 * Check range in Ex command for validity.
3916 * Return NULL when valid, error message when invalid.
3917 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003918 static char *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003919invalid_range(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003920{
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003921 buf_T *buf;
Bram Moolenaar25190db2019-05-04 15:05:28 +02003922
Bram Moolenaar071d4272004-06-13 20:20:40 +00003923 if ( eap->line1 < 0
3924 || eap->line2 < 0
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003925 || eap->line1 > eap->line2)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003926 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003927
Bram Moolenaar8071cb22019-07-12 17:58:01 +02003928 if (eap->argt & EX_RANGE)
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003929 {
Bram Moolenaarb7316892019-05-01 18:08:42 +02003930 switch (eap->addr_type)
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003931 {
3932 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02003933 if (eap->line2 > curbuf->b_ml.ml_line_count
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003934#ifdef FEAT_DIFF
3935 + (eap->cmdidx == CMD_diffget)
3936#endif
3937 )
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003938 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003939 break;
3940 case ADDR_ARGUMENTS:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003941 // add 1 if ARGCOUNT is 0
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01003942 if (eap->line2 > ARGCOUNT + (!ARGCOUNT))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003943 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003944 break;
3945 case ADDR_BUFFERS:
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02003946 // Only a boundary check, not whether the buffers actually
3947 // exist.
3948 if (eap->line1 < 1 || eap->line2 > get_highest_fnum())
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003949 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003950 break;
3951 case ADDR_LOADED_BUFFERS:
3952 buf = firstbuf;
3953 while (buf->b_ml.ml_mfp == NULL)
3954 {
3955 if (buf->b_next == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003956 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003957 buf = buf->b_next;
3958 }
3959 if (eap->line1 < buf->b_fnum)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003960 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003961 buf = lastbuf;
3962 while (buf->b_ml.ml_mfp == NULL)
3963 {
3964 if (buf->b_prev == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003965 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003966 buf = buf->b_prev;
3967 }
3968 if (eap->line2 > buf->b_fnum)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003969 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003970 break;
3971 case ADDR_WINDOWS:
Bram Moolenaar8be63882015-01-14 11:25:05 +01003972 if (eap->line2 > LAST_WIN_NR)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003973 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003974 break;
3975 case ADDR_TABS:
3976 if (eap->line2 > LAST_TAB_NR)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003977 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003978 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01003979 case ADDR_TABS_RELATIVE:
Bram Moolenaarb7316892019-05-01 18:08:42 +02003980 case ADDR_OTHER:
3981 // Any range is OK.
Bram Moolenaar2f72c702017-01-29 14:48:10 +01003982 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003983 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003984#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02003985 // No error for value that is too big, will use the last entry.
3986 if (eap->line2 <= 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003987 return _(e_invrange);
Bram Moolenaare906c502015-09-09 21:10:39 +02003988#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003989 break;
Bram Moolenaar25190db2019-05-04 15:05:28 +02003990 case ADDR_QUICKFIX_VALID:
3991#ifdef FEAT_QUICKFIX
3992 if ((eap->line2 != 1 && eap->line2 > qf_get_valid_size(eap))
3993 || eap->line2 < 0)
3994 return _(e_invrange);
3995#endif
3996 break;
3997 case ADDR_UNSIGNED:
3998 if (eap->line2 < 0)
3999 return _(e_invrange);
4000 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02004001 case ADDR_NONE:
4002 // Will give an error elsewhere.
4003 break;
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004004 }
4005 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004006 return NULL;
4007}
4008
4009/*
4010 * Correct the range for zero line number, if required.
4011 */
4012 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004013correct_range(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004014{
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004015 if (!(eap->argt & EX_ZEROR)) // zero in range not allowed
Bram Moolenaar071d4272004-06-13 20:20:40 +00004016 {
4017 if (eap->line1 == 0)
4018 eap->line1 = 1;
4019 if (eap->line2 == 0)
4020 eap->line2 = 1;
4021 }
4022}
4023
Bram Moolenaar748bf032005-02-02 23:04:36 +00004024#ifdef FEAT_QUICKFIX
Bram Moolenaar748bf032005-02-02 23:04:36 +00004025/*
4026 * For a ":vimgrep" or ":vimgrepadd" command return a pointer past the
4027 * pattern. Otherwise return eap->arg.
4028 */
4029 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004030skip_grep_pat(exarg_T *eap)
Bram Moolenaar748bf032005-02-02 23:04:36 +00004031{
4032 char_u *p = eap->arg;
4033
Bram Moolenaara37420f2006-02-04 22:37:47 +00004034 if (*p != NUL && (eap->cmdidx == CMD_vimgrep || eap->cmdidx == CMD_lvimgrep
4035 || eap->cmdidx == CMD_vimgrepadd
4036 || eap->cmdidx == CMD_lvimgrepadd
4037 || grep_internal(eap->cmdidx)))
Bram Moolenaar748bf032005-02-02 23:04:36 +00004038 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00004039 p = skip_vimgrep_pat(p, NULL, NULL);
Bram Moolenaar748bf032005-02-02 23:04:36 +00004040 if (p == NULL)
4041 p = eap->arg;
Bram Moolenaar748bf032005-02-02 23:04:36 +00004042 }
4043 return p;
4044}
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004045
4046/*
4047 * For the ":make" and ":grep" commands insert the 'makeprg'/'grepprg' option
4048 * in the command line, so that things like % get expanded.
4049 */
4050 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004051replace_makeprg(exarg_T *eap, char_u *p, char_u **cmdlinep)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004052{
4053 char_u *new_cmdline;
4054 char_u *program;
4055 char_u *pos;
4056 char_u *ptr;
4057 int len;
4058 int i;
4059
4060 /*
4061 * Don't do it when ":vimgrep" is used for ":grep".
4062 */
Bram Moolenaara37420f2006-02-04 22:37:47 +00004063 if ((eap->cmdidx == CMD_make || eap->cmdidx == CMD_lmake
4064 || eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4065 || eap->cmdidx == CMD_grepadd
4066 || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004067 && !grep_internal(eap->cmdidx))
4068 {
Bram Moolenaara37420f2006-02-04 22:37:47 +00004069 if (eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4070 || eap->cmdidx == CMD_grepadd || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004071 {
4072 if (*curbuf->b_p_gp == NUL)
4073 program = p_gp;
4074 else
4075 program = curbuf->b_p_gp;
4076 }
4077 else
4078 {
4079 if (*curbuf->b_p_mp == NUL)
4080 program = p_mp;
4081 else
4082 program = curbuf->b_p_mp;
4083 }
4084
4085 p = skipwhite(p);
4086
4087 if ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4088 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004089 // replace $* by given arguments
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004090 i = 1;
4091 while ((pos = (char_u *)strstr((char *)pos + 2, "$*")) != NULL)
4092 ++i;
4093 len = (int)STRLEN(p);
Bram Moolenaar51e14382019-05-25 20:21:28 +02004094 new_cmdline = alloc(STRLEN(program) + i * (len - 2) + 1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004095 if (new_cmdline == NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004096 return NULL; // out of memory
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004097 ptr = new_cmdline;
4098 while ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4099 {
4100 i = (int)(pos - program);
4101 STRNCPY(ptr, program, i);
4102 STRCPY(ptr += i, p);
4103 ptr += len;
4104 program = pos + 2;
4105 }
4106 STRCPY(ptr, program);
4107 }
4108 else
4109 {
Bram Moolenaar51e14382019-05-25 20:21:28 +02004110 new_cmdline = alloc(STRLEN(program) + STRLEN(p) + 2);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004111 if (new_cmdline == NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004112 return NULL; // out of memory
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004113 STRCPY(new_cmdline, program);
4114 STRCAT(new_cmdline, " ");
4115 STRCAT(new_cmdline, p);
4116 }
4117 msg_make(p);
4118
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004119 // 'eap->cmd' is not set here, because it is not used at CMD_make
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004120 vim_free(*cmdlinep);
4121 *cmdlinep = new_cmdline;
4122 p = new_cmdline;
4123 }
4124 return p;
4125}
Bram Moolenaar748bf032005-02-02 23:04:36 +00004126#endif
4127
Bram Moolenaar071d4272004-06-13 20:20:40 +00004128/*
4129 * Expand file name in Ex command argument.
Bram Moolenaar0abb4272019-06-15 16:06:00 +02004130 * When an error is detected, "errormsgp" is set to a non-NULL pointer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004131 * Return FAIL for failure, OK otherwise.
4132 */
4133 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004134expand_filename(
4135 exarg_T *eap,
4136 char_u **cmdlinep,
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004137 char **errormsgp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004138{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004139 int has_wildcards; // need to expand wildcards
Bram Moolenaar071d4272004-06-13 20:20:40 +00004140 char_u *repl;
4141 int srclen;
4142 char_u *p;
4143 int n;
Bram Moolenaar63b92542007-03-27 14:57:09 +00004144 int escaped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004145
Bram Moolenaar748bf032005-02-02 23:04:36 +00004146#ifdef FEAT_QUICKFIX
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004147 // Skip a regexp pattern for ":vimgrep[add] pat file..."
Bram Moolenaar748bf032005-02-02 23:04:36 +00004148 p = skip_grep_pat(eap);
4149#else
4150 p = eap->arg;
4151#endif
4152
Bram Moolenaar071d4272004-06-13 20:20:40 +00004153 /*
4154 * Decide to expand wildcards *before* replacing '%', '#', etc. If
4155 * the file name contains a wildcard it should not cause expanding.
4156 * (it will be expanded anyway if there is a wildcard before replacing).
4157 */
Bram Moolenaar748bf032005-02-02 23:04:36 +00004158 has_wildcards = mch_has_wildcard(p);
4159 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004160 {
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004161#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004162 // Skip over `=expr`, wildcards in it are not expanded.
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004163 if (p[0] == '`' && p[1] == '=')
4164 {
4165 p += 2;
4166 (void)skip_expr(&p);
4167 if (*p == '`')
4168 ++p;
4169 continue;
4170 }
4171#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004172 /*
4173 * Quick check if this cannot be the start of a special string.
4174 * Also removes backslash before '%', '#' and '<'.
4175 */
4176 if (vim_strchr((char_u *)"%#<", *p) == NULL)
4177 {
4178 ++p;
4179 continue;
4180 }
4181
4182 /*
4183 * Try to find a match at this position.
4184 */
Bram Moolenaar63b92542007-03-27 14:57:09 +00004185 repl = eval_vars(p, eap->arg, &srclen, &(eap->do_ecmd_lnum),
4186 errormsgp, &escaped);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004187 if (*errormsgp != NULL) // error detected
Bram Moolenaar071d4272004-06-13 20:20:40 +00004188 return FAIL;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004189 if (repl == NULL) // no match found
Bram Moolenaar071d4272004-06-13 20:20:40 +00004190 {
4191 p += srclen;
4192 continue;
4193 }
4194
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004195 // Wildcards won't be expanded below, the replacement is taken
4196 // literally. But do expand "~/file", "~user/file" and "$HOME/file".
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00004197 if (vim_strchr(repl, '$') != NULL || vim_strchr(repl, '~') != NULL)
4198 {
4199 char_u *l = repl;
4200
4201 repl = expand_env_save(repl);
4202 vim_free(l);
4203 }
4204
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004205 // Need to escape white space et al. with a backslash.
4206 // Don't do this for:
4207 // - replacement that already has been escaped: "##"
4208 // - shell commands (may have to use quotes instead).
4209 // - non-unix systems when there is a single argument (spaces don't
4210 // separate arguments then).
Bram Moolenaar071d4272004-06-13 20:20:40 +00004211 if (!eap->usefilter
Bram Moolenaar63b92542007-03-27 14:57:09 +00004212 && !escaped
Bram Moolenaar071d4272004-06-13 20:20:40 +00004213 && eap->cmdidx != CMD_bang
Bram Moolenaar071d4272004-06-13 20:20:40 +00004214 && eap->cmdidx != CMD_grep
4215 && eap->cmdidx != CMD_grepadd
Bram Moolenaarbf15b8d2017-06-04 20:43:48 +02004216 && eap->cmdidx != CMD_hardcopy
Bram Moolenaar67883b42017-07-27 22:57:00 +02004217 && eap->cmdidx != CMD_lgrep
4218 && eap->cmdidx != CMD_lgrepadd
4219 && eap->cmdidx != CMD_lmake
4220 && eap->cmdidx != CMD_make
4221 && eap->cmdidx != CMD_terminal
Bram Moolenaar071d4272004-06-13 20:20:40 +00004222#ifndef UNIX
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004223 && !(eap->argt & EX_NOSPC)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004224#endif
4225 )
4226 {
4227 char_u *l;
4228#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004229 // Don't escape a backslash here, because rem_backslash() doesn't
4230 // remove it later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004231 static char_u *nobslash = (char_u *)" \t\"|";
4232# define ESCAPE_CHARS nobslash
4233#else
4234# define ESCAPE_CHARS escape_chars
4235#endif
4236
4237 for (l = repl; *l; ++l)
4238 if (vim_strchr(ESCAPE_CHARS, *l) != NULL)
4239 {
4240 l = vim_strsave_escaped(repl, ESCAPE_CHARS);
4241 if (l != NULL)
4242 {
4243 vim_free(repl);
4244 repl = l;
4245 }
4246 break;
4247 }
4248 }
4249
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004250 // For a shell command a '!' must be escaped.
Bram Moolenaar67883b42017-07-27 22:57:00 +02004251 if ((eap->usefilter || eap->cmdidx == CMD_bang
4252 || eap->cmdidx == CMD_terminal)
Bram Moolenaar31b7d382014-04-01 18:54:48 +02004253 && vim_strpbrk(repl, (char_u *)"!") != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004254 {
4255 char_u *l;
4256
Bram Moolenaar31b7d382014-04-01 18:54:48 +02004257 l = vim_strsave_escaped(repl, (char_u *)"!");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004258 if (l != NULL)
4259 {
4260 vim_free(repl);
4261 repl = l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004262 }
4263 }
4264
4265 p = repl_cmdline(eap, p, srclen, repl, cmdlinep);
4266 vim_free(repl);
4267 if (p == NULL)
4268 return FAIL;
4269 }
4270
4271 /*
4272 * One file argument: Expand wildcards.
4273 * Don't do this with ":r !command" or ":w !command".
4274 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004275 if ((eap->argt & EX_NOSPC) && !eap->usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004276 {
4277 /*
4278 * May do this twice:
4279 * 1. Replace environment variables.
4280 * 2. Replace any other wildcards, remove backslashes.
4281 */
4282 for (n = 1; n <= 2; ++n)
4283 {
4284 if (n == 2)
4285 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004286 /*
4287 * Halve the number of backslashes (this is Vi compatible).
4288 * For Unix and OS/2, when wildcards are expanded, this is
4289 * done by ExpandOne() below.
4290 */
Bram Moolenaare7fedb62015-12-31 19:07:19 +01004291#if defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004292 if (!has_wildcards)
4293#endif
4294 backslash_halve(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004295 }
4296
4297 if (has_wildcards)
4298 {
4299 if (n == 1)
4300 {
4301 /*
4302 * First loop: May expand environment variables. This
4303 * can be done much faster with expand_env() than with
4304 * something else (e.g., calling a shell).
4305 * After expanding environment variables, check again
4306 * if there are still wildcards present.
4307 */
4308 if (vim_strchr(eap->arg, '$') != NULL
4309 || vim_strchr(eap->arg, '~') != NULL)
4310 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +00004311 expand_env_esc(eap->arg, NameBuff, MAXPATHL,
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00004312 TRUE, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004313 has_wildcards = mch_has_wildcard(NameBuff);
4314 p = NameBuff;
4315 }
4316 else
4317 p = NULL;
4318 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004319 else // n == 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00004320 {
4321 expand_T xpc;
Bram Moolenaarb40c2572019-10-19 21:01:05 +02004322 int options = WILD_LIST_NOTFOUND
4323 | WILD_NOERROR | WILD_ADD_SLASH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004324
4325 ExpandInit(&xpc);
4326 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar94950a92010-12-02 16:01:29 +01004327 if (p_wic)
4328 options += WILD_ICASE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004329 p = ExpandOne(&xpc, eap->arg, NULL,
Bram Moolenaar94950a92010-12-02 16:01:29 +01004330 options, WILD_EXPAND_FREE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004331 if (p == NULL)
4332 return FAIL;
4333 }
4334 if (p != NULL)
4335 {
4336 (void)repl_cmdline(eap, eap->arg, (int)STRLEN(eap->arg),
4337 p, cmdlinep);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004338 if (n == 2) // p came from ExpandOne()
Bram Moolenaar071d4272004-06-13 20:20:40 +00004339 vim_free(p);
4340 }
4341 }
4342 }
4343 }
4344 return OK;
4345}
4346
4347/*
4348 * Replace part of the command line, keeping eap->cmd, eap->arg and
4349 * eap->nextcmd correct.
4350 * "src" points to the part that is to be replaced, of length "srclen".
4351 * "repl" is the replacement string.
4352 * Returns a pointer to the character after the replaced string.
4353 * Returns NULL for failure.
4354 */
4355 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004356repl_cmdline(
4357 exarg_T *eap,
4358 char_u *src,
4359 int srclen,
4360 char_u *repl,
4361 char_u **cmdlinep)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004362{
4363 int len;
4364 int i;
4365 char_u *new_cmdline;
4366
4367 /*
4368 * The new command line is build in new_cmdline[].
4369 * First allocate it.
4370 * Careful: a "+cmd" argument may have been NUL terminated.
4371 */
4372 len = (int)STRLEN(repl);
4373 i = (int)(src - *cmdlinep) + (int)STRLEN(src + srclen) + len + 3;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004374 if (eap->nextcmd != NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004375 i += (int)STRLEN(eap->nextcmd);// add space for next command
Bram Moolenaar964b3742019-05-24 18:54:09 +02004376 if ((new_cmdline = alloc(i)) == NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004377 return NULL; // out of memory!
Bram Moolenaar071d4272004-06-13 20:20:40 +00004378
4379 /*
4380 * Copy the stuff before the expanded part.
4381 * Copy the expanded stuff.
4382 * Copy what came after the expanded part.
4383 * Copy the next commands, if there are any.
4384 */
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004385 i = (int)(src - *cmdlinep); // length of part before match
Bram Moolenaar071d4272004-06-13 20:20:40 +00004386 mch_memmove(new_cmdline, *cmdlinep, (size_t)i);
Bram Moolenaara3ffd9c2005-07-21 21:03:15 +00004387
Bram Moolenaar071d4272004-06-13 20:20:40 +00004388 mch_memmove(new_cmdline + i, repl, (size_t)len);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004389 i += len; // remember the end of the string
Bram Moolenaar071d4272004-06-13 20:20:40 +00004390 STRCPY(new_cmdline + i, src + srclen);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004391 src = new_cmdline + i; // remember where to continue
Bram Moolenaar071d4272004-06-13 20:20:40 +00004392
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004393 if (eap->nextcmd != NULL) // append next command
Bram Moolenaar071d4272004-06-13 20:20:40 +00004394 {
4395 i = (int)STRLEN(new_cmdline) + 1;
4396 STRCPY(new_cmdline + i, eap->nextcmd);
4397 eap->nextcmd = new_cmdline + i;
4398 }
4399 eap->cmd = new_cmdline + (eap->cmd - *cmdlinep);
4400 eap->arg = new_cmdline + (eap->arg - *cmdlinep);
4401 if (eap->do_ecmd_cmd != NULL && eap->do_ecmd_cmd != dollar_command)
4402 eap->do_ecmd_cmd = new_cmdline + (eap->do_ecmd_cmd - *cmdlinep);
4403 vim_free(*cmdlinep);
4404 *cmdlinep = new_cmdline;
4405
4406 return src;
4407}
4408
4409/*
4410 * Check for '|' to separate commands and '"' to start comments.
4411 */
4412 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004413separate_nextcmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004414{
4415 char_u *p;
4416
Bram Moolenaar86b68352004-12-27 21:59:20 +00004417#ifdef FEAT_QUICKFIX
Bram Moolenaar748bf032005-02-02 23:04:36 +00004418 p = skip_grep_pat(eap);
4419#else
4420 p = eap->arg;
Bram Moolenaar86b68352004-12-27 21:59:20 +00004421#endif
4422
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004423 for ( ; *p; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004424 {
4425 if (*p == Ctrl_V)
4426 {
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004427 if (eap->argt & (EX_CTRLV | EX_XFILE))
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004428 ++p; // skip CTRL-V and next char
Bram Moolenaar071d4272004-06-13 20:20:40 +00004429 else
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004430 // remove CTRL-V and skip next char
Bram Moolenaara7241f52008-06-24 20:39:31 +00004431 STRMOVE(p, p + 1);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004432 if (*p == NUL) // stop at NUL after CTRL-V
Bram Moolenaar071d4272004-06-13 20:20:40 +00004433 break;
4434 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004435
4436#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004437 // Skip over `=expr` when wildcards are expanded.
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004438 else if (p[0] == '`' && p[1] == '=' && (eap->argt & EX_XFILE))
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004439 {
4440 p += 2;
4441 (void)skip_expr(&p);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004442 }
4443#endif
4444
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004445 // Check for '"': start of comment or '|': next command
4446 // :@" and :*" do not start a comment!
4447 // :redir @" doesn't either.
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004448 else if ((*p == '"' && !(eap->argt & EX_NOTRLCOM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004449 && ((eap->cmdidx != CMD_at && eap->cmdidx != CMD_star)
4450 || p != eap->arg)
4451 && (eap->cmdidx != CMD_redir
4452 || p != eap->arg + 1 || p[-1] != '@'))
Bram Moolenaara26b9702020-04-18 19:53:28 +02004453#ifdef FEAT_EVAL
4454 || (*p == '#' && in_vim9script()
4455 && p[1] != '{' && p > eap->cmd && VIM_ISWHITE(p[-1]))
4456#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004457 || *p == '|' || *p == '\n')
4458 {
4459 /*
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004460 * We remove the '\' before the '|', unless EX_CTRLV is used
Bram Moolenaar071d4272004-06-13 20:20:40 +00004461 * AND 'b' is present in 'cpoptions'.
4462 */
4463 if ((vim_strchr(p_cpo, CPO_BAR) == NULL
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004464 || !(eap->argt & EX_CTRLV)) && *(p - 1) == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004465 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004466 STRMOVE(p - 1, p); // remove the '\'
Bram Moolenaar071d4272004-06-13 20:20:40 +00004467 --p;
4468 }
4469 else
4470 {
4471 eap->nextcmd = check_nextcmd(p);
4472 *p = NUL;
4473 break;
4474 }
4475 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004476 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00004477
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004478 if (!(eap->argt & EX_NOTRLCOM)) // remove trailing spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00004479 del_trailing_spaces(eap->arg);
4480}
4481
4482/*
4483 * get + command from ex argument
4484 */
4485 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004486getargcmd(char_u **argp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004487{
4488 char_u *arg = *argp;
4489 char_u *command = NULL;
4490
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004491 if (*arg == '+') // +[command]
Bram Moolenaar071d4272004-06-13 20:20:40 +00004492 {
4493 ++arg;
Bram Moolenaar3e451592014-04-02 14:22:05 +02004494 if (vim_isspace(*arg) || *arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004495 command = dollar_command;
4496 else
4497 {
4498 command = arg;
4499 arg = skip_cmd_arg(command, TRUE);
4500 if (*arg != NUL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004501 *arg++ = NUL; // terminate command with NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004502 }
4503
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004504 arg = skipwhite(arg); // skip over spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00004505 *argp = arg;
4506 }
4507 return command;
4508}
4509
4510/*
4511 * Find end of "+command" argument. Skip over "\ " and "\\".
4512 */
Bram Moolenaard0190392019-08-23 21:17:35 +02004513 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004514skip_cmd_arg(
4515 char_u *p,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004516 int rembs) // TRUE to halve the number of backslashes
Bram Moolenaar071d4272004-06-13 20:20:40 +00004517{
4518 while (*p && !vim_isspace(*p))
4519 {
4520 if (*p == '\\' && p[1] != NUL)
4521 {
4522 if (rembs)
Bram Moolenaara7241f52008-06-24 20:39:31 +00004523 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004524 else
4525 ++p;
4526 }
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004527 MB_PTR_ADV(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004528 }
4529 return p;
4530}
4531
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004532 int
4533get_bad_opt(char_u *p, exarg_T *eap)
4534{
4535 if (STRICMP(p, "keep") == 0)
4536 eap->bad_char = BAD_KEEP;
4537 else if (STRICMP(p, "drop") == 0)
4538 eap->bad_char = BAD_DROP;
4539 else if (MB_BYTE2LEN(*p) == 1 && p[1] == NUL)
4540 eap->bad_char = *p;
Bram Moolenaar75808492018-06-12 12:39:41 +02004541 else
4542 return FAIL;
4543 return OK;
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004544}
4545
Bram Moolenaar071d4272004-06-13 20:20:40 +00004546/*
4547 * Get "++opt=arg" argument.
4548 * Return FAIL or OK.
4549 */
4550 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004551getargopt(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004552{
4553 char_u *arg = eap->arg + 2;
4554 int *pp = NULL;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02004555 int bad_char_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004556 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004557
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004558 // ":edit ++[no]bin[ary] file"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004559 if (STRNCMP(arg, "bin", 3) == 0 || STRNCMP(arg, "nobin", 5) == 0)
4560 {
4561 if (*arg == 'n')
4562 {
4563 arg += 2;
4564 eap->force_bin = FORCE_NOBIN;
4565 }
4566 else
4567 eap->force_bin = FORCE_BIN;
4568 if (!checkforcmd(&arg, "binary", 3))
4569 return FAIL;
4570 eap->arg = skipwhite(arg);
4571 return OK;
4572 }
4573
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004574 // ":read ++edit file"
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004575 if (STRNCMP(arg, "edit", 4) == 0)
4576 {
4577 eap->read_edit = TRUE;
4578 eap->arg = skipwhite(arg + 4);
4579 return OK;
4580 }
4581
Bram Moolenaar071d4272004-06-13 20:20:40 +00004582 if (STRNCMP(arg, "ff", 2) == 0)
4583 {
4584 arg += 2;
4585 pp = &eap->force_ff;
4586 }
4587 else if (STRNCMP(arg, "fileformat", 10) == 0)
4588 {
4589 arg += 10;
4590 pp = &eap->force_ff;
4591 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004592 else if (STRNCMP(arg, "enc", 3) == 0)
4593 {
Bram Moolenaarb38e9ab2011-12-14 14:49:45 +01004594 if (STRNCMP(arg, "encoding", 8) == 0)
4595 arg += 8;
4596 else
4597 arg += 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004598 pp = &eap->force_enc;
4599 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004600 else if (STRNCMP(arg, "bad", 3) == 0)
4601 {
4602 arg += 3;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02004603 pp = &bad_char_idx;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004604 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004605
4606 if (pp == NULL || *arg != '=')
4607 return FAIL;
4608
4609 ++arg;
4610 *pp = (int)(arg - eap->cmd);
4611 arg = skip_cmd_arg(arg, FALSE);
4612 eap->arg = skipwhite(arg);
4613 *arg = NUL;
4614
Bram Moolenaar071d4272004-06-13 20:20:40 +00004615 if (pp == &eap->force_ff)
4616 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004617 if (check_ff_value(eap->cmd + eap->force_ff) == FAIL)
4618 return FAIL;
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004619 eap->force_ff = eap->cmd[eap->force_ff];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004620 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004621 else if (pp == &eap->force_enc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004622 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004623 // Make 'fileencoding' lower case.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004624 for (p = eap->cmd + eap->force_enc; *p != NUL; ++p)
4625 *p = TOLOWER_ASC(*p);
4626 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004627 else
4628 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004629 // Check ++bad= argument. Must be a single-byte character, "keep" or
4630 // "drop".
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004631 if (get_bad_opt(eap->cmd + bad_char_idx, eap) == FAIL)
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004632 return FAIL;
4633 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004634
4635 return OK;
4636}
4637
Bram Moolenaar071d4272004-06-13 20:20:40 +00004638 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004639ex_autocmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004640{
4641 /*
Bram Moolenaar26d4b892018-07-04 22:26:28 +02004642 * Disallow autocommands from .exrc and .vimrc in current
Bram Moolenaar071d4272004-06-13 20:20:40 +00004643 * directory for security reasons.
4644 */
4645 if (secure)
4646 {
4647 secure = 2;
4648 eap->errmsg = e_curdir;
4649 }
4650 else if (eap->cmdidx == CMD_autocmd)
4651 do_autocmd(eap->arg, eap->forceit);
4652 else
4653 do_augroup(eap->arg, eap->forceit);
4654}
4655
4656/*
4657 * ":doautocmd": Apply the automatic commands to the current buffer.
4658 */
4659 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004660ex_doautocmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004661{
Bram Moolenaar60542ac2012-02-12 20:14:01 +01004662 char_u *arg = eap->arg;
4663 int call_do_modelines = check_nomodeline(&arg);
Bram Moolenaar1610d052016-06-09 22:53:01 +02004664 int did_aucmd;
Bram Moolenaar60542ac2012-02-12 20:14:01 +01004665
Bram Moolenaar1610d052016-06-09 22:53:01 +02004666 (void)do_doautocmd(arg, TRUE, &did_aucmd);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004667 // Only when there is no <nomodeline>.
Bram Moolenaar1610d052016-06-09 22:53:01 +02004668 if (call_do_modelines && did_aucmd)
Bram Moolenaar60542ac2012-02-12 20:14:01 +01004669 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004670}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004671
Bram Moolenaar071d4272004-06-13 20:20:40 +00004672/*
4673 * :[N]bunload[!] [N] [bufname] unload buffer
4674 * :[N]bdelete[!] [N] [bufname] delete buffer from buffer list
4675 * :[N]bwipeout[!] [N] [bufname] delete buffer really
4676 */
4677 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004678ex_bunload(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004679{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01004680 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02004681 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004682 eap->errmsg = do_bufdel(
4683 eap->cmdidx == CMD_bdelete ? DOBUF_DEL
4684 : eap->cmdidx == CMD_bwipeout ? DOBUF_WIPE
4685 : DOBUF_UNLOAD, eap->arg,
4686 eap->addr_count, (int)eap->line1, (int)eap->line2, eap->forceit);
4687}
4688
4689/*
4690 * :[N]buffer [N] to buffer N
4691 * :[N]sbuffer [N] to buffer N
4692 */
4693 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004694ex_buffer(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004695{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01004696 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02004697 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004698 if (*eap->arg)
4699 eap->errmsg = e_trailing;
4700 else
4701 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004702 if (eap->addr_count == 0) // default is current buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00004703 goto_buffer(eap, DOBUF_CURRENT, FORWARD, 0);
4704 else
4705 goto_buffer(eap, DOBUF_FIRST, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02004706 if (eap->do_ecmd_cmd != NULL)
4707 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004708 }
4709}
4710
4711/*
4712 * :[N]bmodified [N] to next mod. buffer
4713 * :[N]sbmodified [N] to next mod. buffer
4714 */
4715 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004716ex_bmodified(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004717{
4718 goto_buffer(eap, DOBUF_MOD, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02004719 if (eap->do_ecmd_cmd != NULL)
4720 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004721}
4722
4723/*
4724 * :[N]bnext [N] to next buffer
4725 * :[N]sbnext [N] split and to next buffer
4726 */
4727 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004728ex_bnext(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004729{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01004730 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar3029bcc2020-01-18 15:06:19 +01004731 return;
4732
Bram Moolenaar071d4272004-06-13 20:20:40 +00004733 goto_buffer(eap, DOBUF_CURRENT, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02004734 if (eap->do_ecmd_cmd != NULL)
4735 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004736}
4737
4738/*
4739 * :[N]bNext [N] to previous buffer
4740 * :[N]bprevious [N] to previous buffer
4741 * :[N]sbNext [N] split and to previous buffer
4742 * :[N]sbprevious [N] split and to previous buffer
4743 */
4744 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004745ex_bprevious(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004746{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01004747 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar3029bcc2020-01-18 15:06:19 +01004748 return;
4749
Bram Moolenaar071d4272004-06-13 20:20:40 +00004750 goto_buffer(eap, DOBUF_CURRENT, BACKWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02004751 if (eap->do_ecmd_cmd != NULL)
4752 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004753}
4754
4755/*
4756 * :brewind to first buffer
4757 * :bfirst to first buffer
4758 * :sbrewind split and to first buffer
4759 * :sbfirst split and to first buffer
4760 */
4761 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004762ex_brewind(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004763{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01004764 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar3029bcc2020-01-18 15:06:19 +01004765 return;
4766
Bram Moolenaar071d4272004-06-13 20:20:40 +00004767 goto_buffer(eap, DOBUF_FIRST, FORWARD, 0);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02004768 if (eap->do_ecmd_cmd != NULL)
4769 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004770}
4771
4772/*
4773 * :blast to last buffer
4774 * :sblast split and to last buffer
4775 */
4776 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004777ex_blast(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004778{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01004779 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar3029bcc2020-01-18 15:06:19 +01004780 return;
4781
Bram Moolenaar071d4272004-06-13 20:20:40 +00004782 goto_buffer(eap, DOBUF_LAST, BACKWARD, 0);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02004783 if (eap->do_ecmd_cmd != NULL)
4784 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004785}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004786
Bram Moolenaar7a092242020-04-16 22:10:49 +02004787/*
4788 * Check if "c" ends an Ex command.
Bram Moolenaara494f562020-04-18 17:45:38 +02004789 * In Vim9 script does not check for white space before # or #{.
Bram Moolenaar7a092242020-04-16 22:10:49 +02004790 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004791 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004792ends_excmd(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004793{
Bram Moolenaarfbda69b2020-04-13 15:06:53 +02004794#ifdef FEAT_EVAL
Bram Moolenaar2c330432020-04-13 14:41:35 +02004795 if (c == '#')
Bram Moolenaar7a092242020-04-16 22:10:49 +02004796 return in_vim9script();
4797#endif
4798 return (c == NUL || c == '|' || c == '"' || c == '\n');
4799}
4800
4801/*
4802 * Like ends_excmd() but checks that a # in Vim9 script either has "cmd" equal
4803 * to "cmd_start" or has a white space character before it.
4804 */
4805 int
Bram Moolenaar21cfe502020-04-16 23:01:50 +02004806ends_excmd2(char_u *cmd_start UNUSED, char_u *cmd)
Bram Moolenaar7a092242020-04-16 22:10:49 +02004807{
4808 int c = *cmd;
4809
4810#ifdef FEAT_EVAL
Bram Moolenaara26b9702020-04-18 19:53:28 +02004811 if (c == '#' && cmd[1] != '{' && (cmd == cmd_start || VIM_ISWHITE(cmd[-1])))
Bram Moolenaar2c330432020-04-13 14:41:35 +02004812 return in_vim9script();
Bram Moolenaarfbda69b2020-04-13 15:06:53 +02004813#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004814 return (c == NUL || c == '|' || c == '"' || c == '\n');
4815}
4816
4817#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA) || defined(FEAT_EVAL) \
4818 || defined(PROTO)
4819/*
4820 * Return the next command, after the first '|' or '\n'.
4821 * Return NULL if not found.
4822 */
4823 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004824find_nextcmd(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004825{
4826 while (*p != '|' && *p != '\n')
4827 {
4828 if (*p == NUL)
4829 return NULL;
4830 ++p;
4831 }
4832 return (p + 1);
4833}
4834#endif
4835
4836/*
Bram Moolenaar2256c992016-11-15 21:17:07 +01004837 * Check if *p is a separator between Ex commands, skipping over white space.
4838 * Return NULL if it isn't, the following character if it is.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004839 */
4840 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004841check_nextcmd(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004842{
Bram Moolenaar2256c992016-11-15 21:17:07 +01004843 char_u *s = skipwhite(p);
4844
4845 if (*s == '|' || *s == '\n')
4846 return (s + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004847 else
4848 return NULL;
4849}
4850
4851/*
4852 * - if there are more files to edit
4853 * - and this is the last window
4854 * - and forceit not used
4855 * - and not repeated twice on a row
4856 * return FAIL and give error message if 'message' TRUE
4857 * return OK otherwise
4858 */
4859 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004860check_more(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004861 int message, // when FALSE check only, no messages
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004862 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004863{
4864 int n = ARGCOUNT - curwin->w_arg_idx - 1;
4865
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00004866 if (!forceit && only_one_window()
4867 && ARGCOUNT > 1 && !arg_had_last && n >= 0 && quitmore == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004868 {
4869 if (message)
4870 {
4871#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
4872 if ((p_confirm || cmdmod.confirm) && curbuf->b_fname != NULL)
4873 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02004874 char_u buff[DIALOG_MSG_SIZE];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004875
Bram Moolenaarda6e8912018-08-21 15:12:14 +02004876 vim_snprintf((char *)buff, DIALOG_MSG_SIZE,
4877 NGETTEXT("%d more file to edit. Quit anyway?",
4878 "%d more files to edit. Quit anyway?", n), n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004879 if (vim_dialog_yesno(VIM_QUESTION, NULL, buff, 1) == VIM_YES)
4880 return OK;
4881 return FAIL;
4882 }
4883#endif
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01004884 semsg(NGETTEXT("E173: %d more file to edit",
4885 "E173: %d more files to edit", n), n);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004886 quitmore = 2; // next try to quit is allowed
Bram Moolenaar071d4272004-06-13 20:20:40 +00004887 }
4888 return FAIL;
4889 }
4890 return OK;
4891}
4892
Bram Moolenaar071d4272004-06-13 20:20:40 +00004893/*
4894 * Function given to ExpandGeneric() to obtain the list of command names.
4895 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004896 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004897get_command_name(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004898{
4899 if (idx >= (int)CMD_SIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004900 return get_user_command_name(idx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004901 return cmdnames[idx].cmd_name;
4902}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004903
Bram Moolenaar071d4272004-06-13 20:20:40 +00004904 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004905ex_colorscheme(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004906{
Bram Moolenaare6850792010-05-14 15:28:44 +02004907 if (*eap->arg == NUL)
4908 {
4909#ifdef FEAT_EVAL
4910 char_u *expr = vim_strsave((char_u *)"g:colors_name");
4911 char_u *p = NULL;
4912
4913 if (expr != NULL)
4914 {
4915 ++emsg_off;
Bram Moolenaarb171fb12020-06-24 20:34:03 +02004916 p = eval_to_string(expr, FALSE);
Bram Moolenaare6850792010-05-14 15:28:44 +02004917 --emsg_off;
4918 vim_free(expr);
4919 }
4920 if (p != NULL)
4921 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01004922 msg((char *)p);
Bram Moolenaare6850792010-05-14 15:28:44 +02004923 vim_free(p);
4924 }
4925 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01004926 msg("default");
Bram Moolenaare6850792010-05-14 15:28:44 +02004927#else
Bram Moolenaar32526b32019-01-19 17:43:09 +01004928 msg(_("unknown"));
Bram Moolenaare6850792010-05-14 15:28:44 +02004929#endif
4930 }
4931 else if (load_colors(eap->arg) == FAIL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004932 semsg(_("E185: Cannot find color scheme '%s'"), eap->arg);
Bram Moolenaarf58d81a2019-01-28 20:19:05 +01004933
4934#ifdef FEAT_VTP
4935 else if (has_vtp_working())
4936 {
4937 // background color change requires clear + redraw
4938 update_screen(CLEAR);
4939 redrawcmd();
4940 }
4941#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004942}
4943
4944 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004945ex_highlight(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004946{
4947 if (*eap->arg == NUL && eap->cmd[2] == '!')
Bram Moolenaar32526b32019-01-19 17:43:09 +01004948 msg(_("Greetings, Vim user!"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004949 do_highlight(eap->arg, eap->forceit, FALSE);
4950}
4951
4952
4953/*
4954 * Call this function if we thought we were going to exit, but we won't
4955 * (because of an error). May need to restore the terminal mode.
4956 */
4957 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004958not_exiting(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004959{
4960 exiting = FALSE;
4961 settmode(TMODE_RAW);
4962}
4963
Bram Moolenaar12a96de2018-03-11 14:44:18 +01004964 static int
4965before_quit_autocmds(win_T *wp, int quit_all, int forceit)
4966{
4967 apply_autocmds(EVENT_QUITPRE, NULL, NULL, FALSE, wp->w_buffer);
4968
Bram Moolenaar34ba06b2019-10-20 22:27:10 +02004969 // Bail out when autocommands closed the window.
4970 // Refuse to quit when the buffer in the last window is being closed (can
4971 // only happen in autocommands).
Bram Moolenaar12a96de2018-03-11 14:44:18 +01004972 if (!win_valid(wp)
4973 || curbuf_locked()
4974 || (wp->w_buffer->b_nwindows == 1 && wp->w_buffer->b_locked > 0))
4975 return TRUE;
4976
4977 if (quit_all || (check_more(FALSE, forceit) == OK && only_one_window()))
4978 {
4979 apply_autocmds(EVENT_EXITPRE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar34ba06b2019-10-20 22:27:10 +02004980 // Refuse to quit when locked or when the window was closed or the
4981 // buffer in the last window is being closed (can only happen in
4982 // autocommands).
4983 if (!win_valid(wp) || curbuf_locked()
Bram Moolenaar12a96de2018-03-11 14:44:18 +01004984 || (curbuf->b_nwindows == 1 && curbuf->b_locked > 0))
4985 return TRUE;
4986 }
4987
4988 return FALSE;
4989}
4990
Bram Moolenaar071d4272004-06-13 20:20:40 +00004991/*
Bram Moolenaarf240e182014-11-27 18:33:02 +01004992 * ":quit": quit current window, quit Vim if the last window is closed.
Bram Moolenaar12a96de2018-03-11 14:44:18 +01004993 * ":{nr}quit": quit window {nr}
Bram Moolenaar4d14bac2019-10-20 21:15:15 +02004994 * Also used when closing a terminal window that's the last one.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004995 */
Bram Moolenaar4d14bac2019-10-20 21:15:15 +02004996 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004997ex_quit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004998{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004999 win_T *wp;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005000
Bram Moolenaar071d4272004-06-13 20:20:40 +00005001#ifdef FEAT_CMDWIN
5002 if (cmdwin_type != 0)
5003 {
5004 cmdwin_result = Ctrl_C;
5005 return;
5006 }
5007#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005008 // Don't quit while editing the command line.
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005009 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005010 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005011 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005012 return;
5013 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005014 if (eap->addr_count > 0)
5015 {
Bram Moolenaarf240e182014-11-27 18:33:02 +01005016 int wnr = eap->line2;
5017
5018 for (wp = firstwin; wp->w_next != NULL; wp = wp->w_next)
5019 if (--wnr <= 0)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005020 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005021 }
5022 else
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005023 wp = curwin;
Bram Moolenaarf240e182014-11-27 18:33:02 +01005024
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005025 // Refuse to quit when locked.
Bram Moolenaar87ffb5c2017-10-19 12:37:42 +02005026 if (curbuf_locked())
5027 return;
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005028
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005029 // Trigger QuitPre and maybe ExitPre
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005030 if (before_quit_autocmds(wp, FALSE, eap->forceit))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005031 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005032
5033#ifdef FEAT_NETBEANS_INTG
5034 netbeansForcedQuit = eap->forceit;
5035#endif
5036
5037 /*
5038 * If there are more files or windows we won't exit.
5039 */
5040 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
5041 exiting = TRUE;
Bram Moolenaarff930ca2017-10-19 17:12:10 +02005042 if ((!buf_hide(wp->w_buffer)
5043 && check_changed(wp->w_buffer, (p_awa ? CCGD_AW : 0)
Bram Moolenaar45d3b142013-11-09 03:31:51 +01005044 | (eap->forceit ? CCGD_FORCEIT : 0)
5045 | CCGD_EXCMD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005046 || check_more(TRUE, eap->forceit) == FAIL
Bram Moolenaar027387f2016-01-02 22:25:52 +01005047 || (only_one_window() && check_changed_any(eap->forceit, TRUE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005048 {
5049 not_exiting();
5050 }
5051 else
5052 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005053 // quit last window
5054 // Note: only_one_window() returns true, even so a help window is
5055 // still open. In that case only quit, if no address has been
5056 // specified. Example:
5057 // :h|wincmd w|1q - don't quit
5058 // :h|wincmd w|q - quit
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01005059 if (only_one_window() && (ONE_WINDOW || eap->addr_count == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005060 getout(0);
Bram Moolenaar2c33d7b2017-10-14 16:06:20 +02005061 not_exiting();
Bram Moolenaar4033c552017-09-16 20:54:51 +02005062#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00005063 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005064#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005065 // close window; may free buffer
Bram Moolenaareb44a682017-08-03 22:44:55 +02005066 win_close(wp, !buf_hide(wp->w_buffer) || eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005067 }
5068}
5069
5070/*
5071 * ":cquit".
5072 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005073 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005074ex_cquit(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005075{
Bram Moolenaar1860bde2020-01-06 21:47:21 +01005076 // this does not always pass on the exit code to the Manx compiler. why?
5077 getout(eap->addr_count > 0 ? (int)eap->line2 : EXIT_FAILURE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005078}
5079
5080/*
5081 * ":qall": try to quit all windows
5082 */
5083 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005084ex_quit_all(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005085{
5086# ifdef FEAT_CMDWIN
5087 if (cmdwin_type != 0)
5088 {
5089 if (eap->forceit)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005090 cmdwin_result = K_XF1; // ex_window() takes care of this
Bram Moolenaar071d4272004-06-13 20:20:40 +00005091 else
5092 cmdwin_result = K_XF2;
5093 return;
5094 }
5095# endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005096
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005097 // Don't quit while editing the command line.
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005098 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005099 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005100 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005101 return;
5102 }
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005103
5104 if (before_quit_autocmds(curwin, TRUE, eap->forceit))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005105 return;
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005106
Bram Moolenaar071d4272004-06-13 20:20:40 +00005107 exiting = TRUE;
Bram Moolenaar027387f2016-01-02 22:25:52 +01005108 if (eap->forceit || !check_changed_any(FALSE, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005109 getout(0);
5110 not_exiting();
5111}
5112
Bram Moolenaar071d4272004-06-13 20:20:40 +00005113/*
5114 * ":close": close current window, unless it is the last one
5115 */
5116 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005117ex_close(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005118{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005119 win_T *win;
5120 int winnr = 0;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005121#ifdef FEAT_CMDWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00005122 if (cmdwin_type != 0)
Bram Moolenaar9bd1a7e2011-05-19 14:50:54 +02005123 cmdwin_result = Ctrl_C;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005124 else
Bram Moolenaar4033c552017-09-16 20:54:51 +02005125#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005126 if (!text_locked() && !curbuf_locked())
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005127 {
5128 if (eap->addr_count == 0)
5129 ex_win_close(eap->forceit, curwin, NULL);
Bram Moolenaar6d41c782018-06-06 09:11:12 +02005130 else
5131 {
Bram Moolenaar29323592016-07-24 22:04:11 +02005132 FOR_ALL_WINDOWS(win)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005133 {
5134 winnr++;
5135 if (winnr == eap->line2)
5136 break;
5137 }
5138 if (win == NULL)
5139 win = lastwin;
5140 ex_win_close(eap->forceit, win, NULL);
5141 }
5142 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005143}
5144
Bram Moolenaar4033c552017-09-16 20:54:51 +02005145#ifdef FEAT_QUICKFIX
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005146/*
5147 * ":pclose": Close any preview window.
5148 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005149 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005150ex_pclose(exarg_T *eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005151{
5152 win_T *win;
5153
Bram Moolenaar79648732019-07-18 21:43:07 +02005154 // First close any normal window.
Bram Moolenaar29323592016-07-24 22:04:11 +02005155 FOR_ALL_WINDOWS(win)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005156 if (win->w_p_pvw)
5157 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00005158 ex_win_close(eap->forceit, win, NULL);
Bram Moolenaar79648732019-07-18 21:43:07 +02005159 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005160 }
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01005161# ifdef FEAT_PROP_POPUP
Bram Moolenaar79648732019-07-18 21:43:07 +02005162 // Also when 'previewpopup' is empty, it might have been cleared.
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02005163 popup_close_preview();
Bram Moolenaar79648732019-07-18 21:43:07 +02005164# endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005165}
Bram Moolenaar4033c552017-09-16 20:54:51 +02005166#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005167
Bram Moolenaarf740b292006-02-16 22:11:02 +00005168/*
5169 * Close window "win" and take care of handling closing the last window for a
5170 * modified buffer.
5171 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005172 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005173ex_win_close(
5174 int forceit,
5175 win_T *win,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005176 tabpage_T *tp) // NULL or the tab page "win" is in
Bram Moolenaar071d4272004-06-13 20:20:40 +00005177{
5178 int need_hide;
5179 buf_T *buf = win->w_buffer;
5180
5181 need_hide = (bufIsChanged(buf) && buf->b_nwindows <= 1);
Bram Moolenaareb44a682017-08-03 22:44:55 +02005182 if (need_hide && !buf_hide(buf) && !forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005183 {
Bram Moolenaar4033c552017-09-16 20:54:51 +02005184#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005185 if ((p_confirm || cmdmod.confirm) && p_write)
5186 {
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005187 bufref_T bufref;
5188
5189 set_bufref(&bufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005190 dialog_changed(buf, FALSE);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005191 if (bufref_valid(&bufref) && bufIsChanged(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005192 return;
5193 need_hide = FALSE;
5194 }
5195 else
Bram Moolenaar4033c552017-09-16 20:54:51 +02005196#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005197 {
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02005198 no_write_message();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005199 return;
5200 }
5201 }
5202
Bram Moolenaar4033c552017-09-16 20:54:51 +02005203#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00005204 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005205#endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00005206
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005207 // free buffer when not hiding it or when it's a scratch buffer
Bram Moolenaarf740b292006-02-16 22:11:02 +00005208 if (tp == NULL)
Bram Moolenaareb44a682017-08-03 22:44:55 +02005209 win_close(win, !need_hide && !buf_hide(buf));
Bram Moolenaarf740b292006-02-16 22:11:02 +00005210 else
Bram Moolenaareb44a682017-08-03 22:44:55 +02005211 win_close_othertab(win, !need_hide && !buf_hide(buf), tp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005212}
5213
Bram Moolenaar071d4272004-06-13 20:20:40 +00005214/*
Bram Moolenaardea25702017-01-29 15:18:10 +01005215 * Handle the argument for a tabpage related ex command.
5216 * Returns a tabpage number.
5217 * When an error is encountered then eap->errmsg is set.
5218 */
5219 static int
5220get_tabpage_arg(exarg_T *eap)
5221{
5222 int tab_number;
5223 int unaccept_arg0 = (eap->cmdidx == CMD_tabmove) ? 0 : 1;
5224
5225 if (eap->arg && *eap->arg != NUL)
5226 {
5227 char_u *p = eap->arg;
5228 char_u *p_save;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005229 int relative = 0; // argument +N/-N means: go to N places to the
5230 // right/left relative to the current position.
Bram Moolenaardea25702017-01-29 15:18:10 +01005231
5232 if (*p == '-')
5233 {
5234 relative = -1;
5235 p++;
5236 }
5237 else if (*p == '+')
5238 {
5239 relative = 1;
5240 p++;
5241 }
5242
5243 p_save = p;
5244 tab_number = getdigits(&p);
5245
5246 if (relative == 0)
5247 {
5248 if (STRCMP(p, "$") == 0)
5249 tab_number = LAST_TAB_NR;
5250 else if (p == p_save || *p_save == '-' || *p != NUL
5251 || tab_number > LAST_TAB_NR)
5252 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005253 // No numbers as argument.
Bram Moolenaardea25702017-01-29 15:18:10 +01005254 eap->errmsg = e_invarg;
5255 goto theend;
5256 }
5257 }
5258 else
5259 {
5260 if (*p_save == NUL)
5261 tab_number = 1;
5262 else if (p == p_save || *p_save == '-' || *p != NUL
5263 || tab_number == 0)
5264 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005265 // No numbers as argument.
Bram Moolenaardea25702017-01-29 15:18:10 +01005266 eap->errmsg = e_invarg;
5267 goto theend;
5268 }
5269 tab_number = tab_number * relative + tabpage_index(curtab);
5270 if (!unaccept_arg0 && relative == -1)
5271 --tab_number;
5272 }
5273 if (tab_number < unaccept_arg0 || tab_number > LAST_TAB_NR)
5274 eap->errmsg = e_invarg;
5275 }
5276 else if (eap->addr_count > 0)
5277 {
5278 if (unaccept_arg0 && eap->line2 == 0)
Bram Moolenaarc6251552017-01-29 21:42:20 +01005279 {
Bram Moolenaardea25702017-01-29 15:18:10 +01005280 eap->errmsg = e_invrange;
Bram Moolenaarc6251552017-01-29 21:42:20 +01005281 tab_number = 0;
5282 }
Bram Moolenaardea25702017-01-29 15:18:10 +01005283 else
5284 {
5285 tab_number = eap->line2;
Bram Moolenaar82a12462019-01-22 22:41:42 +01005286 if (!unaccept_arg0 && *skipwhite(*eap->cmdlinep) == '-')
Bram Moolenaardea25702017-01-29 15:18:10 +01005287 {
5288 --tab_number;
5289 if (tab_number < unaccept_arg0)
5290 eap->errmsg = e_invarg;
5291 }
5292 }
5293 }
5294 else
5295 {
5296 switch (eap->cmdidx)
5297 {
5298 case CMD_tabnext:
5299 tab_number = tabpage_index(curtab) + 1;
5300 if (tab_number > LAST_TAB_NR)
5301 tab_number = 1;
5302 break;
5303 case CMD_tabmove:
5304 tab_number = LAST_TAB_NR;
5305 break;
5306 default:
5307 tab_number = tabpage_index(curtab);
5308 }
5309 }
5310
5311theend:
5312 return tab_number;
5313}
5314
5315/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00005316 * ":tabclose": close current tab page, unless it is the last one.
5317 * ":tabclose N": close tab page N.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005318 */
5319 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005320ex_tabclose(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005321{
Bram Moolenaarf740b292006-02-16 22:11:02 +00005322 tabpage_T *tp;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005323 int tab_number;
Bram Moolenaarf740b292006-02-16 22:11:02 +00005324
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005325# ifdef FEAT_CMDWIN
5326 if (cmdwin_type != 0)
5327 cmdwin_result = K_IGNORE;
5328 else
5329# endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00005330 if (first_tabpage->tp_next == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005331 emsg(_("E784: Cannot close last tab page"));
Bram Moolenaarf740b292006-02-16 22:11:02 +00005332 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005333 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005334 tab_number = get_tabpage_arg(eap);
5335 if (eap->errmsg == NULL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005336 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005337 tp = find_tabpage(tab_number);
Bram Moolenaarf740b292006-02-16 22:11:02 +00005338 if (tp == NULL)
5339 {
5340 beep_flush();
5341 return;
5342 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005343 if (tp != curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00005344 {
5345 tabpage_close_other(tp, eap->forceit);
5346 return;
5347 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005348 else if (!text_locked() && !curbuf_locked())
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005349 tabpage_close(eap->forceit);
5350 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005351 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005352}
5353
5354/*
5355 * ":tabonly": close all tab pages except the current one
5356 */
5357 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005358ex_tabonly(exarg_T *eap)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005359{
5360 tabpage_T *tp;
5361 int done;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005362 int tab_number;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005363
5364# ifdef FEAT_CMDWIN
5365 if (cmdwin_type != 0)
5366 cmdwin_result = K_IGNORE;
5367 else
5368# endif
5369 if (first_tabpage->tp_next == NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01005370 msg(_("Already only one tab page"));
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005371 else
5372 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005373 tab_number = get_tabpage_arg(eap);
5374 if (eap->errmsg == NULL)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005375 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005376 goto_tabpage(tab_number);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005377 // Repeat this up to a 1000 times, because autocommands may
5378 // mess up the lists.
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005379 for (done = 0; done < 1000; ++done)
5380 {
5381 FOR_ALL_TABPAGES(tp)
5382 if (tp->tp_topframe != topframe)
5383 {
5384 tabpage_close_other(tp, eap->forceit);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005385 // if we failed to close it quit
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005386 if (valid_tabpage(tp))
5387 done = 1000;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005388 // start over, "tp" is now invalid
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005389 break;
5390 }
5391 if (first_tabpage->tp_next == NULL)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005392 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005393 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005394 }
5395 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005396}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005397
5398/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00005399 * Close the current tab page.
5400 */
5401 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005402tabpage_close(int forceit)
Bram Moolenaarf740b292006-02-16 22:11:02 +00005403{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005404 // First close all the windows but the current one. If that worked then
5405 // close the last window in this tab, that will close it.
Bram Moolenaar459ca562016-11-10 18:16:33 +01005406 if (!ONE_WINDOW)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005407 close_others(TRUE, forceit);
Bram Moolenaar459ca562016-11-10 18:16:33 +01005408 if (ONE_WINDOW)
Bram Moolenaarf740b292006-02-16 22:11:02 +00005409 ex_win_close(forceit, curwin, NULL);
5410# ifdef FEAT_GUI
5411 need_mouse_correct = TRUE;
5412# endif
5413}
5414
5415/*
5416 * Close tab page "tp", which is not the current tab page.
5417 * Note that autocommands may make "tp" invalid.
Bram Moolenaar7875acc2006-09-10 13:51:17 +00005418 * Also takes care of the tab pages line disappearing when closing the
5419 * last-but-one tab page.
Bram Moolenaarf740b292006-02-16 22:11:02 +00005420 */
5421 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005422tabpage_close_other(tabpage_T *tp, int forceit)
Bram Moolenaarf740b292006-02-16 22:11:02 +00005423{
5424 int done = 0;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005425 win_T *wp;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00005426 int h = tabline_height();
Bram Moolenaarf740b292006-02-16 22:11:02 +00005427
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005428 // Limit to 1000 windows, autocommands may add a window while we close
5429 // one. OK, so I'm paranoid...
Bram Moolenaarf740b292006-02-16 22:11:02 +00005430 while (++done < 1000)
5431 {
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005432 wp = tp->tp_firstwin;
5433 ex_win_close(forceit, wp, tp);
Bram Moolenaarf740b292006-02-16 22:11:02 +00005434
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005435 // Autocommands may delete the tab page under our fingers and we may
5436 // fail to close a window with a modified buffer.
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005437 if (!valid_tabpage(tp) || tp->tp_firstwin == wp)
Bram Moolenaarf740b292006-02-16 22:11:02 +00005438 break;
5439 }
Bram Moolenaar7875acc2006-09-10 13:51:17 +00005440
Bram Moolenaar29323592016-07-24 22:04:11 +02005441 apply_autocmds(EVENT_TABCLOSED, NULL, NULL, FALSE, curbuf);
Bram Moolenaar29323592016-07-24 22:04:11 +02005442
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005443 redraw_tabline = TRUE;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00005444 if (h != tabline_height())
5445 shell_new_rows();
Bram Moolenaarf740b292006-02-16 22:11:02 +00005446}
5447
5448/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005449 * ":only".
5450 */
5451 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005452ex_only(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005453{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005454 win_T *wp;
5455 int wnr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005456# ifdef FEAT_GUI
5457 need_mouse_correct = TRUE;
5458# endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005459 if (eap->addr_count > 0)
5460 {
5461 wnr = eap->line2;
5462 for (wp = firstwin; --wnr > 0; )
5463 {
5464 if (wp->w_next == NULL)
5465 break;
5466 else
5467 wp = wp->w_next;
5468 }
5469 win_goto(wp);
5470 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005471 close_others(TRUE, eap->forceit);
5472}
5473
Bram Moolenaar071d4272004-06-13 20:20:40 +00005474 static void
Bram Moolenaar5e1e6d22017-01-02 17:26:00 +01005475ex_hide(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005476{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005477 // ":hide" or ":hide | cmd": hide current window
Bram Moolenaar2256c992016-11-15 21:17:07 +01005478 if (!eap->skip)
5479 {
Bram Moolenaar4033c552017-09-16 20:54:51 +02005480#ifdef FEAT_GUI
Bram Moolenaar2256c992016-11-15 21:17:07 +01005481 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005482#endif
Bram Moolenaar2256c992016-11-15 21:17:07 +01005483 if (eap->addr_count == 0)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005484 win_close(curwin, FALSE); // don't free buffer
Bram Moolenaar2256c992016-11-15 21:17:07 +01005485 else
5486 {
5487 int winnr = 0;
5488 win_T *win;
Bram Moolenaarf240e182014-11-27 18:33:02 +01005489
Bram Moolenaar2256c992016-11-15 21:17:07 +01005490 FOR_ALL_WINDOWS(win)
5491 {
5492 winnr++;
5493 if (winnr == eap->line2)
5494 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005495 }
Bram Moolenaar2256c992016-11-15 21:17:07 +01005496 if (win == NULL)
5497 win = lastwin;
5498 win_close(win, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005499 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005500 }
5501}
5502
5503/*
5504 * ":stop" and ":suspend": Suspend Vim.
5505 */
5506 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005507ex_stop(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005508{
5509 /*
5510 * Disallow suspending for "rvim".
5511 */
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005512 if (!check_restricted())
Bram Moolenaar071d4272004-06-13 20:20:40 +00005513 {
5514 if (!eap->forceit)
5515 autowrite_all();
5516 windgoto((int)Rows - 1, 0);
5517 out_char('\n');
5518 out_flush();
5519 stoptermcap();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005520 out_flush(); // needed for SUN to restore xterm buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00005521#ifdef FEAT_TITLE
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005522 mch_restore_title(SAVE_RESTORE_BOTH); // restore window titles
Bram Moolenaar071d4272004-06-13 20:20:40 +00005523#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005524 ui_suspend(); // call machine specific function
Bram Moolenaar071d4272004-06-13 20:20:40 +00005525#ifdef FEAT_TITLE
5526 maketitle();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005527 resettitle(); // force updating the title
Bram Moolenaar071d4272004-06-13 20:20:40 +00005528#endif
5529 starttermcap();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005530 scroll_start(); // scroll screen before redrawing
Bram Moolenaar071d4272004-06-13 20:20:40 +00005531 redraw_later_clear();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005532 shell_resized(); // may have resized window
Bram Moolenaar071d4272004-06-13 20:20:40 +00005533 }
5534}
5535
5536/*
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005537 * ":exit", ":xit" and ":wq": Write file and quite the current window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005538 */
5539 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005540ex_exit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005541{
5542#ifdef FEAT_CMDWIN
5543 if (cmdwin_type != 0)
5544 {
5545 cmdwin_result = Ctrl_C;
5546 return;
5547 }
5548#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005549 // Don't quit while editing the command line.
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005550 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005551 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005552 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005553 return;
5554 }
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005555
5556 if (before_quit_autocmds(curwin, FALSE, eap->forceit))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005557 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005558
5559 /*
5560 * if more files or windows we won't exit
5561 */
5562 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
5563 exiting = TRUE;
5564 if ( ((eap->cmdidx == CMD_wq
5565 || curbufIsChanged())
5566 && do_write(eap) == FAIL)
5567 || check_more(TRUE, eap->forceit) == FAIL
Bram Moolenaar027387f2016-01-02 22:25:52 +01005568 || (only_one_window() && check_changed_any(eap->forceit, FALSE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005569 {
5570 not_exiting();
5571 }
5572 else
5573 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005574 if (only_one_window()) // quit last window, exit Vim
Bram Moolenaar071d4272004-06-13 20:20:40 +00005575 getout(0);
Bram Moolenaar2c33d7b2017-10-14 16:06:20 +02005576 not_exiting();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005577# ifdef FEAT_GUI
5578 need_mouse_correct = TRUE;
5579# endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005580 // Quit current window, may free the buffer.
Bram Moolenaareb44a682017-08-03 22:44:55 +02005581 win_close(curwin, !buf_hide(curwin->w_buffer));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005582 }
5583}
5584
5585/*
5586 * ":print", ":list", ":number".
5587 */
5588 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005589ex_print(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005590{
Bram Moolenaardf177f62005-02-22 08:39:57 +00005591 if (curbuf->b_ml.ml_flags & ML_EMPTY)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005592 emsg(_(e_emptybuf));
Bram Moolenaardf177f62005-02-22 08:39:57 +00005593 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005594 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00005595 for ( ;!got_int; ui_breakcheck())
5596 {
5597 print_line(eap->line1,
5598 (eap->cmdidx == CMD_number || eap->cmdidx == CMD_pound
5599 || (eap->flags & EXFLAG_NR)),
5600 eap->cmdidx == CMD_list || (eap->flags & EXFLAG_LIST));
5601 if (++eap->line1 > eap->line2)
5602 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005603 out_flush(); // show one line at a time
Bram Moolenaardf177f62005-02-22 08:39:57 +00005604 }
5605 setpcmark();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005606 // put cursor at last line
Bram Moolenaardf177f62005-02-22 08:39:57 +00005607 curwin->w_cursor.lnum = eap->line2;
5608 beginline(BL_SOL | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005609 }
5610
Bram Moolenaar071d4272004-06-13 20:20:40 +00005611 ex_no_reprint = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005612}
5613
5614#ifdef FEAT_BYTEOFF
5615 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005616ex_goto(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005617{
5618 goto_byte(eap->line2);
5619}
5620#endif
5621
5622/*
5623 * ":shell".
5624 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005625 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005626ex_shell(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005627{
5628 do_shell(NULL, 0);
5629}
5630
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005631#if defined(HAVE_DROP_FILE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005632
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005633static int drop_busy = FALSE;
5634static int drop_filec;
5635static char_u **drop_filev = NULL;
5636static int drop_split;
5637static void (*drop_callback)(void *);
5638static void *drop_cookie;
5639
5640 static void
5641handle_drop_internal(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005642{
5643 exarg_T ea;
5644 int save_msg_scroll = msg_scroll;
5645
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005646 // Setting the argument list may cause screen updates and being called
5647 // recursively. Avoid that by setting drop_busy.
5648 drop_busy = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005649
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005650 // Check whether the current buffer is changed. If so, we will need
5651 // to split the current window or data could be lost.
5652 // We don't need to check if the 'hidden' option is set, as in this
5653 // case the buffer won't be lost.
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005654 if (!buf_hide(curbuf) && !drop_split)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005655 {
5656 ++emsg_off;
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005657 drop_split = check_changed(curbuf, CCGD_AW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005658 --emsg_off;
5659 }
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005660 if (drop_split)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005661 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005662 if (win_split(0, 0) == FAIL)
5663 return;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02005664 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005665
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005666 // When splitting the window, create a new alist. Otherwise the
5667 // existing one is overwritten.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005668 alist_unlink(curwin->w_alist);
5669 alist_new();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005670 }
5671
5672 /*
5673 * Set up the new argument list.
5674 */
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005675 alist_set(ALIST(curwin), drop_filec, drop_filev, FALSE, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005676
5677 /*
5678 * Move to the first file.
5679 */
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005680 // Fake up a minimal "next" command for do_argfile()
Bram Moolenaara80faa82020-04-12 19:37:17 +02005681 CLEAR_FIELD(ea);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005682 ea.cmd = (char_u *)"next";
5683 do_argfile(&ea, 0);
5684
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005685 // do_ecmd() may set need_start_insertmode, but since we never left Insert
5686 // mode that is not needed here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005687 need_start_insertmode = FALSE;
5688
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005689 // Restore msg_scroll, otherwise a following command may cause scrolling
5690 // unexpectedly. The screen will be redrawn by the caller, thus
5691 // msg_scroll being set by displaying a message is irrelevant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005692 msg_scroll = save_msg_scroll;
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005693
5694 if (drop_callback != NULL)
5695 drop_callback(drop_cookie);
5696
5697 drop_filev = NULL;
5698 drop_busy = FALSE;
5699}
5700
5701/*
5702 * Handle a file drop. The code is here because a drop is *nearly* like an
5703 * :args command, but not quite (we have a list of exact filenames, so we
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01005704 * don't want to (a) parse a command line, or (b) expand wildcards). So the
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005705 * code is very similar to :args and hence needs access to a lot of the static
5706 * functions in this file.
5707 *
5708 * The "filev" list must have been allocated using alloc(), as should each item
5709 * in the list. This function takes over responsibility for freeing the "filev"
5710 * list.
5711 */
5712 void
5713handle_drop(
5714 int filec, // the number of files dropped
5715 char_u **filev, // the list of files dropped
5716 int split, // force splitting the window
5717 void (*callback)(void *), // to be called after setting the argument
5718 // list
5719 void *cookie) // argument for "callback" (allocated)
5720{
5721 // Cannot handle recursive drops, finish the pending one.
5722 if (drop_busy)
5723 {
5724 FreeWild(filec, filev);
5725 vim_free(cookie);
5726 return;
5727 }
5728
5729 // When calling handle_drop() more than once in a row we only use the last
5730 // one.
5731 if (drop_filev != NULL)
5732 {
5733 FreeWild(drop_filec, drop_filev);
5734 vim_free(drop_cookie);
5735 }
5736
5737 drop_filec = filec;
5738 drop_filev = filev;
5739 drop_split = split;
5740 drop_callback = callback;
5741 drop_cookie = cookie;
5742
5743 // Postpone this when:
5744 // - editing the command line
5745 // - not possible to change the current buffer
5746 // - updating the screen
5747 // As it may change buffers and window structures that are in use and cause
5748 // freed memory to be used.
5749 if (text_locked() || curbuf_locked() || updating_screen)
5750 return;
5751
5752 handle_drop_internal();
5753}
5754
5755/*
5756 * To be called when text is unlocked, curbuf is unlocked or updating_screen is
5757 * reset: Handle a postponed drop.
5758 */
5759 void
5760handle_any_postponed_drop(void)
5761{
5762 if (!drop_busy && drop_filev != NULL
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +02005763 && !text_locked() && !curbuf_locked() && !updating_screen)
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005764 handle_drop_internal();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005765}
5766#endif
5767
Bram Moolenaar071d4272004-06-13 20:20:40 +00005768/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005769 * ":preserve".
5770 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005771 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005772ex_preserve(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005773{
Bram Moolenaar4399ef42005-02-12 14:29:27 +00005774 curbuf->b_flags |= BF_PRESERVED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005775 ml_preserve(curbuf, TRUE);
5776}
5777
5778/*
5779 * ":recover".
5780 */
5781 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005782ex_recover(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005783{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005784 // Set recoverymode right away to avoid the ATTENTION prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005785 recoverymode = TRUE;
Bram Moolenaar45d3b142013-11-09 03:31:51 +01005786 if (!check_changed(curbuf, (p_awa ? CCGD_AW : 0)
5787 | CCGD_MULTWIN
5788 | (eap->forceit ? CCGD_FORCEIT : 0)
5789 | CCGD_EXCMD)
5790
Bram Moolenaar071d4272004-06-13 20:20:40 +00005791 && (*eap->arg == NUL
5792 || setfname(curbuf, eap->arg, NULL, TRUE) == OK))
Bram Moolenaar99499b12019-05-23 21:35:48 +02005793 ml_recover(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005794 recoverymode = FALSE;
5795}
5796
5797/*
5798 * Command modifier used in a wrong way.
5799 */
5800 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005801ex_wrongmodifier(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005802{
5803 eap->errmsg = e_invcmd;
5804}
5805
Bram Moolenaar071d4272004-06-13 20:20:40 +00005806/*
5807 * :sview [+command] file split window with new file, read-only
5808 * :split [[+command] file] split window with current or new file
5809 * :vsplit [[+command] file] split window vertically with current or new file
5810 * :new [[+command] file] split window with no or new file
5811 * :vnew [[+command] file] split vertically window with no or new file
5812 * :sfind [+command] file split window with file in 'path'
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005813 *
5814 * :tabedit open new Tab page with empty window
5815 * :tabedit [+command] file open new Tab page and edit "file"
5816 * :tabnew [[+command] file] just like :tabedit
5817 * :tabfind [+command] file open new Tab page and find "file"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005818 */
5819 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005820ex_splitview(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005821{
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005822 win_T *old_curwin = curwin;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005823#if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005824 char_u *fname = NULL;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005825#endif
5826#ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00005827 int browse_flag = cmdmod.browse;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005828#endif
Bram Moolenaar39902a02018-06-21 22:10:08 +02005829 int use_tab = eap->cmdidx == CMD_tabedit
5830 || eap->cmdidx == CMD_tabfind
5831 || eap->cmdidx == CMD_tabnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005832
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01005833 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02005834 return;
5835
Bram Moolenaar4033c552017-09-16 20:54:51 +02005836#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00005837 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005838#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005839
Bram Moolenaar4033c552017-09-16 20:54:51 +02005840#ifdef FEAT_QUICKFIX
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005841 // A ":split" in the quickfix window works like ":new". Don't want two
5842 // quickfix windows. But it's OK when doing ":tab split".
Bram Moolenaar05bb9532008-07-04 09:44:11 +00005843 if (bt_quickfix(curbuf) && cmdmod.tab == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005844 {
5845 if (eap->cmdidx == CMD_split)
5846 eap->cmdidx = CMD_new;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005847 if (eap->cmdidx == CMD_vsplit)
5848 eap->cmdidx = CMD_vnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005849 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02005850#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005851
Bram Moolenaar4033c552017-09-16 20:54:51 +02005852#ifdef FEAT_SEARCHPATH
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005853 if (eap->cmdidx == CMD_sfind || eap->cmdidx == CMD_tabfind)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005854 {
5855 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg),
5856 FNAME_MESS, TRUE, curbuf->b_ffname);
5857 if (fname == NULL)
5858 goto theend;
5859 eap->arg = fname;
5860 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005861# ifdef FEAT_BROWSE
Bram Moolenaar4033c552017-09-16 20:54:51 +02005862 else
5863# endif
5864#endif
5865#ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00005866 if (cmdmod.browse
Bram Moolenaar071d4272004-06-13 20:20:40 +00005867 && eap->cmdidx != CMD_vnew
Bram Moolenaar071d4272004-06-13 20:20:40 +00005868 && eap->cmdidx != CMD_new)
5869 {
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00005870 if (
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005871# ifdef FEAT_GUI
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00005872 !gui.in_use &&
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005873# endif
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00005874 au_has_group((char_u *)"FileExplorer"))
5875 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005876 // No browsing supported but we do have the file explorer:
5877 // Edit the directory.
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00005878 if (*eap->arg == NUL || !mch_isdir(eap->arg))
5879 eap->arg = (char_u *)".";
5880 }
5881 else
5882 {
Bram Moolenaar39902a02018-06-21 22:10:08 +02005883 fname = do_browse(0, (char_u *)(use_tab
5884 ? _("Edit File in new tab page")
5885 : _("Edit File in new window")),
Bram Moolenaar071d4272004-06-13 20:20:40 +00005886 eap->arg, NULL, NULL, NULL, curbuf);
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00005887 if (fname == NULL)
5888 goto theend;
5889 eap->arg = fname;
5890 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005891 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005892 cmdmod.browse = FALSE; // Don't browse again in do_ecmd().
Bram Moolenaar4033c552017-09-16 20:54:51 +02005893#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005894
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005895 /*
5896 * Either open new tab page or split the window.
5897 */
Bram Moolenaar39902a02018-06-21 22:10:08 +02005898 if (use_tab)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005899 {
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005900 if (win_new_tabpage(cmdmod.tab != 0 ? cmdmod.tab
5901 : eap->addr_count == 0 ? 0
5902 : (int)eap->line2 + 1) != FAIL)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005903 {
Bram Moolenaard2b66012008-01-09 19:30:36 +00005904 do_exedit(eap, old_curwin);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005905
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005906 // set the alternate buffer for the window we came from
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005907 if (curwin != old_curwin
5908 && win_valid(old_curwin)
5909 && old_curwin->w_buffer != curbuf
5910 && !cmdmod.keepalt)
5911 old_curwin->w_alt_fnum = curbuf->b_fnum;
5912 }
5913 }
5914 else if (win_split(eap->addr_count > 0 ? (int)eap->line2 : 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005915 *eap->cmd == 'v' ? WSP_VERT : 0) != FAIL)
5916 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005917 // Reset 'scrollbind' when editing another file, but keep it when
5918 // doing ":split" without arguments.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005919 if (*eap->arg != NUL
Bram Moolenaar8a3bb562018-03-04 20:14:14 +01005920# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00005921 || cmdmod.browse
Bram Moolenaar8a3bb562018-03-04 20:14:14 +01005922# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005923 )
Bram Moolenaar3368ea22010-09-21 16:56:35 +02005924 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005925 else
5926 do_check_scrollbind(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005927 do_exedit(eap, old_curwin);
5928 }
5929
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005930# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00005931 cmdmod.browse = browse_flag;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005932# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005933
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005934# if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005935theend:
5936 vim_free(fname);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005937# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005938}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005939
5940/*
Bram Moolenaar80a94a52006-02-23 21:26:58 +00005941 * Open a new tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005942 */
5943 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005944tabpage_new(void)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00005945{
5946 exarg_T ea;
5947
Bram Moolenaara80faa82020-04-12 19:37:17 +02005948 CLEAR_FIELD(ea);
Bram Moolenaar80a94a52006-02-23 21:26:58 +00005949 ea.cmdidx = CMD_tabnew;
5950 ea.cmd = (char_u *)"tabn";
5951 ea.arg = (char_u *)"";
5952 ex_splitview(&ea);
5953}
5954
5955/*
5956 * :tabnext command
5957 */
5958 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005959ex_tabnext(exarg_T *eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005960{
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005961 int tab_number;
5962
Bram Moolenaar8cdbd5b2019-06-16 15:50:45 +02005963 if (ERROR_IF_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02005964 return;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005965 switch (eap->cmdidx)
5966 {
5967 case CMD_tabfirst:
5968 case CMD_tabrewind:
5969 goto_tabpage(1);
5970 break;
5971 case CMD_tablast:
5972 goto_tabpage(9999);
5973 break;
5974 case CMD_tabprevious:
5975 case CMD_tabNext:
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005976 if (eap->arg && *eap->arg != NUL)
5977 {
5978 char_u *p = eap->arg;
5979 char_u *p_save = p;
5980
5981 tab_number = getdigits(&p);
5982 if (p == p_save || *p_save == '-' || *p != NUL
5983 || tab_number == 0)
5984 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005985 // No numbers as argument.
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005986 eap->errmsg = e_invarg;
5987 return;
5988 }
5989 }
5990 else
5991 {
5992 if (eap->addr_count == 0)
5993 tab_number = 1;
5994 else
5995 {
5996 tab_number = eap->line2;
5997 if (tab_number < 1)
5998 {
5999 eap->errmsg = e_invrange;
6000 return;
6001 }
6002 }
6003 }
6004 goto_tabpage(-tab_number);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006005 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006006 default: // CMD_tabnext
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006007 tab_number = get_tabpage_arg(eap);
6008 if (eap->errmsg == NULL)
6009 goto_tabpage(tab_number);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006010 break;
6011 }
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006012}
6013
6014/*
6015 * :tabmove command
6016 */
6017 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006018ex_tabmove(exarg_T *eap)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006019{
Bram Moolenaar40ce3a42015-04-21 18:08:39 +02006020 int tab_number;
Bram Moolenaar8cb8dca2012-07-06 18:27:39 +02006021
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006022 tab_number = get_tabpage_arg(eap);
6023 if (eap->errmsg == NULL)
6024 tabpage_move(tab_number);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006025}
6026
6027/*
6028 * :tabs command: List tabs and their contents.
6029 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006030 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006031ex_tabs(exarg_T *eap UNUSED)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006032{
6033 tabpage_T *tp;
6034 win_T *wp;
6035 int tabcount = 1;
6036
6037 msg_start();
6038 msg_scroll = TRUE;
6039 for (tp = first_tabpage; tp != NULL && !got_int; tp = tp->tp_next)
6040 {
6041 msg_putchar('\n');
6042 vim_snprintf((char *)IObuff, IOSIZE, _("Tab page %d"), tabcount++);
Bram Moolenaar8820b482017-03-16 17:23:31 +01006043 msg_outtrans_attr(IObuff, HL_ATTR(HLF_T));
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006044 out_flush(); // output one line at a time
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006045 ui_breakcheck();
6046
Bram Moolenaar030f0df2006-02-21 22:02:53 +00006047 if (tp == curtab)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006048 wp = firstwin;
6049 else
6050 wp = tp->tp_firstwin;
6051 for ( ; wp != NULL && !got_int; wp = wp->w_next)
6052 {
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006053 msg_putchar('\n');
6054 msg_putchar(wp == curwin ? '>' : ' ');
6055 msg_putchar(' ');
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006056 msg_putchar(bufIsChanged(wp->w_buffer) ? '+' : ' ');
6057 msg_putchar(' ');
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006058 if (buf_spname(wp->w_buffer) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02006059 vim_strncpy(IObuff, buf_spname(wp->w_buffer), IOSIZE - 1);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006060 else
6061 home_replace(wp->w_buffer, wp->w_buffer->b_fname,
6062 IObuff, IOSIZE, TRUE);
6063 msg_outtrans(IObuff);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006064 out_flush(); // output one line at a time
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006065 ui_breakcheck();
6066 }
6067 }
6068}
6069
Bram Moolenaar071d4272004-06-13 20:20:40 +00006070/*
6071 * ":mode": Set screen mode.
6072 * If no argument given, just get the screen size and redraw.
6073 */
6074 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006075ex_mode(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006076{
6077 if (*eap->arg == NUL)
6078 shell_resized();
6079 else
Bram Moolenaar3c71aec2020-01-17 19:32:20 +01006080 emsg(_(e_screenmode));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006081}
6082
Bram Moolenaar071d4272004-06-13 20:20:40 +00006083/*
6084 * ":resize".
6085 * set, increment or decrement current window height
6086 */
6087 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006088ex_resize(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006089{
6090 int n;
6091 win_T *wp = curwin;
6092
6093 if (eap->addr_count > 0)
6094 {
6095 n = eap->line2;
6096 for (wp = firstwin; wp->w_next != NULL && --n > 0; wp = wp->w_next)
6097 ;
6098 }
6099
Bram Moolenaar44a2f922016-03-19 22:11:51 +01006100# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00006101 need_mouse_correct = TRUE;
Bram Moolenaar44a2f922016-03-19 22:11:51 +01006102# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006103 n = atol((char *)eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006104 if (cmdmod.split & WSP_VERT)
6105 {
6106 if (*eap->arg == '-' || *eap->arg == '+')
Bram Moolenaar02631462017-09-22 15:20:32 +02006107 n += curwin->w_width;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006108 else if (n == 0 && eap->arg[0] == NUL) // default is very wide
Bram Moolenaar071d4272004-06-13 20:20:40 +00006109 n = 9999;
6110 win_setwidth_win((int)n, wp);
6111 }
6112 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006113 {
6114 if (*eap->arg == '-' || *eap->arg == '+')
6115 n += curwin->w_height;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006116 else if (n == 0 && eap->arg[0] == NUL) // default is very high
Bram Moolenaar071d4272004-06-13 20:20:40 +00006117 n = 9999;
6118 win_setheight_win((int)n, wp);
6119 }
6120}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006121
6122/*
6123 * ":find [+command] <file>" command.
6124 */
6125 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006126ex_find(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006127{
6128#ifdef FEAT_SEARCHPATH
6129 char_u *fname;
6130 int count;
6131
6132 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg), FNAME_MESS,
6133 TRUE, curbuf->b_ffname);
6134 if (eap->addr_count > 0)
6135 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006136 // Repeat finding the file "count" times. This matters when it
6137 // appears several times in the path.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006138 count = eap->line2;
6139 while (fname != NULL && --count > 0)
6140 {
6141 vim_free(fname);
6142 fname = find_file_in_path(NULL, 0, FNAME_MESS,
6143 FALSE, curbuf->b_ffname);
6144 }
6145 }
6146
6147 if (fname != NULL)
6148 {
6149 eap->arg = fname;
6150#endif
6151 do_exedit(eap, NULL);
6152#ifdef FEAT_SEARCHPATH
6153 vim_free(fname);
6154 }
6155#endif
6156}
6157
6158/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00006159 * ":open" simulation: for now just work like ":visual".
6160 */
6161 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006162ex_open(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00006163{
6164 regmatch_T regmatch;
6165 char_u *p;
6166
6167 curwin->w_cursor.lnum = eap->line2;
6168 beginline(BL_SOL | BL_FIX);
6169 if (*eap->arg == '/')
6170 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006171 // ":open /pattern/": put cursor in column found with pattern
Bram Moolenaardf177f62005-02-22 08:39:57 +00006172 ++eap->arg;
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006173 p = skip_regexp(eap->arg, '/', p_magic);
Bram Moolenaardf177f62005-02-22 08:39:57 +00006174 *p = NUL;
6175 regmatch.regprog = vim_regcomp(eap->arg, p_magic ? RE_MAGIC : 0);
6176 if (regmatch.regprog != NULL)
6177 {
6178 regmatch.rm_ic = p_ic;
6179 p = ml_get_curline();
6180 if (vim_regexec(&regmatch, p, (colnr_T)0))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006181 curwin->w_cursor.col = (colnr_T)(regmatch.startp[0] - p);
Bram Moolenaardf177f62005-02-22 08:39:57 +00006182 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006183 emsg(_(e_nomatch));
Bram Moolenaar473de612013-06-08 18:19:48 +02006184 vim_regfree(regmatch.regprog);
Bram Moolenaardf177f62005-02-22 08:39:57 +00006185 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006186 // Move to the NUL, ignore any other arguments.
Bram Moolenaardf177f62005-02-22 08:39:57 +00006187 eap->arg += STRLEN(eap->arg);
6188 }
6189 check_cursor();
6190
6191 eap->cmdidx = CMD_visual;
6192 do_exedit(eap, NULL);
6193}
6194
6195/*
6196 * ":edit", ":badd", ":visual".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006197 */
6198 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006199ex_edit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006200{
6201 do_exedit(eap, NULL);
6202}
6203
6204/*
Bram Moolenaar4b96df52020-01-26 22:00:26 +01006205 * ":edit <file>" command and alike.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006206 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006207 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006208do_exedit(
6209 exarg_T *eap,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006210 win_T *old_curwin) // curwin before doing a split or NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006211{
6212 int n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006213 int need_hide;
Bram Moolenaardf177f62005-02-22 08:39:57 +00006214 int exmode_was = exmode_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006215
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01006216 if ((eap->cmdidx != CMD_pedit && ERROR_IF_POPUP_WINDOW)
6217 || ERROR_IF_TERM_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02006218 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006219 /*
6220 * ":vi" command ends Ex mode.
6221 */
6222 if (exmode_active && (eap->cmdidx == CMD_visual
6223 || eap->cmdidx == CMD_view))
6224 {
6225 exmode_active = FALSE;
Bram Moolenaar158ea172020-06-18 17:28:39 +02006226 ex_pressedreturn = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006227 if (*eap->arg == NUL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00006228 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006229 // Special case: ":global/pat/visual\NLvi-commands"
Bram Moolenaardf177f62005-02-22 08:39:57 +00006230 if (global_busy)
6231 {
6232 int rd = RedrawingDisabled;
6233 int nwr = no_wait_return;
6234 int ms = msg_scroll;
6235#ifdef FEAT_GUI
6236 int he = hold_gui_events;
6237#endif
6238
6239 if (eap->nextcmd != NULL)
6240 {
6241 stuffReadbuff(eap->nextcmd);
6242 eap->nextcmd = NULL;
6243 }
6244
6245 if (exmode_was != EXMODE_VIM)
6246 settmode(TMODE_RAW);
6247 RedrawingDisabled = 0;
6248 no_wait_return = 0;
6249 need_wait_return = FALSE;
6250 msg_scroll = 0;
6251#ifdef FEAT_GUI
6252 hold_gui_events = 0;
6253#endif
6254 must_redraw = CLEAR;
Bram Moolenaar9e2bcb52020-02-18 21:33:00 +01006255 pending_exmode_active = TRUE;
Bram Moolenaardf177f62005-02-22 08:39:57 +00006256
6257 main_loop(FALSE, TRUE);
6258
Bram Moolenaar9e2bcb52020-02-18 21:33:00 +01006259 pending_exmode_active = FALSE;
Bram Moolenaardf177f62005-02-22 08:39:57 +00006260 RedrawingDisabled = rd;
6261 no_wait_return = nwr;
6262 msg_scroll = ms;
6263#ifdef FEAT_GUI
6264 hold_gui_events = he;
6265#endif
6266 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006267 return;
Bram Moolenaardf177f62005-02-22 08:39:57 +00006268 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006269 }
6270
6271 if ((eap->cmdidx == CMD_new
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006272 || eap->cmdidx == CMD_tabnew
6273 || eap->cmdidx == CMD_tabedit
Bram Moolenaar4033c552017-09-16 20:54:51 +02006274 || eap->cmdidx == CMD_vnew) && *eap->arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006275 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006276 // ":new" or ":tabnew" without argument: edit an new empty buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00006277 setpcmark();
6278 (void)do_ecmd(0, NULL, NULL, eap, ECMD_ONE,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00006279 ECMD_HIDE + (eap->forceit ? ECMD_FORCEIT : 0),
6280 old_curwin == NULL ? curwin : NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006281 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02006282 else if ((eap->cmdidx != CMD_split && eap->cmdidx != CMD_vsplit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006283 || *eap->arg != NUL
6284#ifdef FEAT_BROWSE
6285 || cmdmod.browse
6286#endif
6287 )
6288 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006289 // Can't edit another file when "curbuf_lock" is set. Only ":edit"
6290 // can bring us here, others are stopped earlier.
Bram Moolenaar5555acc2006-04-07 21:33:12 +00006291 if (*eap->arg != NUL && curbuf_locked())
6292 return;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006293
Bram Moolenaar071d4272004-06-13 20:20:40 +00006294 n = readonlymode;
6295 if (eap->cmdidx == CMD_view || eap->cmdidx == CMD_sview)
6296 readonlymode = TRUE;
6297 else if (eap->cmdidx == CMD_enew)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006298 readonlymode = FALSE; // 'readonly' doesn't make sense in an
6299 // empty buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00006300 setpcmark();
6301 if (do_ecmd(0, (eap->cmdidx == CMD_enew ? NULL : eap->arg),
6302 NULL, eap,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006303 // ":edit" goes to first line if Vi compatible
Bram Moolenaar071d4272004-06-13 20:20:40 +00006304 (*eap->arg == NUL && eap->do_ecmd_lnum == 0
6305 && vim_strchr(p_cpo, CPO_GOTO1) != NULL)
6306 ? ECMD_ONE : eap->do_ecmd_lnum,
Bram Moolenaareb44a682017-08-03 22:44:55 +02006307 (buf_hide(curbuf) ? ECMD_HIDE : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006308 + (eap->forceit ? ECMD_FORCEIT : 0)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006309 // after a split we can use an existing buffer
Bram Moolenaar7b449342014-03-25 13:03:48 +01006310 + (old_curwin != NULL ? ECMD_OLDBUF : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006311 + (eap->cmdidx == CMD_badd ? ECMD_ADDBUF : 0 )
Bram Moolenaar701f7af2008-11-15 13:12:07 +00006312 , old_curwin == NULL ? curwin : NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006313 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006314 // Editing the file failed. If the window was split, close it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006315 if (old_curwin != NULL)
6316 {
6317 need_hide = (curbufIsChanged() && curbuf->b_nwindows <= 1);
Bram Moolenaareb44a682017-08-03 22:44:55 +02006318 if (!need_hide || buf_hide(curbuf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006319 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006320#if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00006321 cleanup_T cs;
6322
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006323 // Reset the error/interrupt/exception state here so that
6324 // aborting() returns FALSE when closing a window.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00006325 enter_cleanup(&cs);
Bram Moolenaar4033c552017-09-16 20:54:51 +02006326#endif
6327#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00006328 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006329#endif
Bram Moolenaareb44a682017-08-03 22:44:55 +02006330 win_close(curwin, !need_hide && !buf_hide(curbuf));
Bram Moolenaarc0197e22004-09-13 20:26:32 +00006331
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006332#if defined(FEAT_EVAL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006333 // Restore the error/interrupt/exception state if not
6334 // discarded by a new aborting error, interrupt, or
6335 // uncaught exception.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00006336 leave_cleanup(&cs);
Bram Moolenaar4033c552017-09-16 20:54:51 +02006337#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006338 }
6339 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006340 }
6341 else if (readonlymode && curbuf->b_nwindows == 1)
6342 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006343 // When editing an already visited buffer, 'readonly' won't be set
6344 // but the previous value is kept. With ":view" and ":sview" we
6345 // want the file to be readonly, except when another window is
6346 // editing the same buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006347 curbuf->b_p_ro = TRUE;
6348 }
6349 readonlymode = n;
6350 }
6351 else
6352 {
6353 if (eap->do_ecmd_cmd != NULL)
6354 do_cmdline_cmd(eap->do_ecmd_cmd);
6355#ifdef FEAT_TITLE
6356 n = curwin->w_arg_idx_invalid;
6357#endif
6358 check_arg_idx(curwin);
6359#ifdef FEAT_TITLE
6360 if (n != curwin->w_arg_idx_invalid)
6361 maketitle();
6362#endif
6363 }
6364
Bram Moolenaar071d4272004-06-13 20:20:40 +00006365 /*
6366 * if ":split file" worked, set alternate file name in old window to new
6367 * file
6368 */
6369 if (old_curwin != NULL
6370 && *eap->arg != NUL
6371 && curwin != old_curwin
6372 && win_valid(old_curwin)
Bram Moolenaard4755bb2004-09-02 19:12:26 +00006373 && old_curwin->w_buffer != curbuf
6374 && !cmdmod.keepalt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006375 old_curwin->w_alt_fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006376
6377 ex_no_reprint = TRUE;
6378}
6379
6380#ifndef FEAT_GUI
6381/*
6382 * ":gui" and ":gvim" when there is no GUI.
6383 */
6384 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006385ex_nogui(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006386{
6387 eap->errmsg = e_nogvim;
6388}
6389#endif
6390
Bram Moolenaar4f974752019-02-17 17:44:42 +01006391#if defined(FEAT_GUI_MSWIN) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006392 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006393ex_tearoff(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006394{
6395 gui_make_tearoff(eap->arg);
6396}
6397#endif
6398
Bram Moolenaar29a2c082018-03-05 21:06:23 +01006399#if (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK) \
6400 || defined(FEAT_TERM_POPUP_MENU)) && defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006401 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006402ex_popup(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006403{
Bram Moolenaar29a2c082018-03-05 21:06:23 +01006404# if defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK)
6405 if (gui.in_use)
6406 gui_make_popup(eap->arg, eap->forceit);
6407# ifdef FEAT_TERM_POPUP_MENU
6408 else
6409# endif
6410# endif
6411# ifdef FEAT_TERM_POPUP_MENU
6412 pum_make_popup(eap->arg, eap->forceit);
6413# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006414}
6415#endif
6416
Bram Moolenaar071d4272004-06-13 20:20:40 +00006417 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006418ex_swapname(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006419{
6420 if (curbuf->b_ml.ml_mfp == NULL || curbuf->b_ml.ml_mfp->mf_fname == NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01006421 msg(_("No swap file"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006422 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01006423 msg((char *)curbuf->b_ml.ml_mfp->mf_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006424}
6425
6426/*
6427 * ":syncbind" forces all 'scrollbind' windows to have the same relative
6428 * offset.
6429 * (1998-11-02 16:21:01 R. Edward Ralston <eralston@computer.org>)
6430 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006431 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006432ex_syncbind(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006433{
Bram Moolenaar071d4272004-06-13 20:20:40 +00006434 win_T *wp;
Bram Moolenaardedd1b02013-12-14 13:06:17 +01006435 win_T *save_curwin = curwin;
6436 buf_T *save_curbuf = curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006437 long topline;
6438 long y;
6439 linenr_T old_linenr = curwin->w_cursor.lnum;
6440
6441 setpcmark();
6442
6443 /*
6444 * determine max topline
6445 */
6446 if (curwin->w_p_scb)
6447 {
6448 topline = curwin->w_topline;
Bram Moolenaar29323592016-07-24 22:04:11 +02006449 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006450 {
6451 if (wp->w_p_scb && wp->w_buffer)
6452 {
Bram Moolenaar375e3392019-01-31 18:26:10 +01006453 y = wp->w_buffer->b_ml.ml_line_count - get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006454 if (topline > y)
6455 topline = y;
6456 }
6457 }
6458 if (topline < 1)
6459 topline = 1;
6460 }
6461 else
6462 {
6463 topline = 1;
6464 }
6465
6466
6467 /*
Bram Moolenaardedd1b02013-12-14 13:06:17 +01006468 * Set all scrollbind windows to the same topline.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006469 */
Bram Moolenaar29323592016-07-24 22:04:11 +02006470 FOR_ALL_WINDOWS(curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006471 {
6472 if (curwin->w_p_scb)
6473 {
Bram Moolenaardedd1b02013-12-14 13:06:17 +01006474 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006475 y = topline - curwin->w_topline;
6476 if (y > 0)
6477 scrollup(y, TRUE);
6478 else
6479 scrolldown(-y, TRUE);
6480 curwin->w_scbind_pos = topline;
6481 redraw_later(VALID);
6482 cursor_correct();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006483 curwin->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006484 }
6485 }
Bram Moolenaardedd1b02013-12-14 13:06:17 +01006486 curwin = save_curwin;
6487 curbuf = save_curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006488 if (curwin->w_p_scb)
6489 {
6490 did_syncbind = TRUE;
6491 checkpcmark();
6492 if (old_linenr != curwin->w_cursor.lnum)
6493 {
6494 char_u ctrl_o[2];
6495
6496 ctrl_o[0] = Ctrl_O;
6497 ctrl_o[1] = 0;
6498 ins_typebuf(ctrl_o, REMAP_NONE, 0, TRUE, FALSE);
6499 }
6500 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006501}
6502
6503
6504 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006505ex_read(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006506{
Bram Moolenaardf177f62005-02-22 08:39:57 +00006507 int i;
6508 int empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
6509 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006510
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006511 if (eap->usefilter) // :r!cmd
Bram Moolenaar071d4272004-06-13 20:20:40 +00006512 do_bang(1, eap, FALSE, FALSE, TRUE);
6513 else
6514 {
6515 if (u_save(eap->line2, (linenr_T)(eap->line2 + 1)) == FAIL)
6516 return;
6517
6518#ifdef FEAT_BROWSE
6519 if (cmdmod.browse)
6520 {
6521 char_u *browseFile;
6522
Bram Moolenaar7171abe2004-10-11 10:06:20 +00006523 browseFile = do_browse(0, (char_u *)_("Append File"), eap->arg,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006524 NULL, NULL, NULL, curbuf);
6525 if (browseFile != NULL)
6526 {
6527 i = readfile(browseFile, NULL,
6528 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
6529 vim_free(browseFile);
6530 }
6531 else
6532 i = OK;
6533 }
6534 else
6535#endif
6536 if (*eap->arg == NUL)
6537 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006538 if (check_fname() == FAIL) // check for no file name
Bram Moolenaar071d4272004-06-13 20:20:40 +00006539 return;
6540 i = readfile(curbuf->b_ffname, curbuf->b_fname,
6541 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
6542 }
6543 else
6544 {
6545 if (vim_strchr(p_cpo, CPO_ALTREAD) != NULL)
6546 (void)setaltfname(eap->arg, eap->arg, (linenr_T)1);
6547 i = readfile(eap->arg, NULL,
6548 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
6549
6550 }
Bram Moolenaare13b9af2017-01-13 22:01:02 +01006551 if (i != OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006552 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006553#if defined(FEAT_EVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006554 if (!aborting())
6555#endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006556 semsg(_(e_notopen), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006557 }
6558 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00006559 {
6560 if (empty && exmode_active)
6561 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006562 // Delete the empty line that remains. Historically ex does
6563 // this but vi doesn't.
Bram Moolenaardf177f62005-02-22 08:39:57 +00006564 if (eap->line2 == 0)
6565 lnum = curbuf->b_ml.ml_line_count;
6566 else
6567 lnum = 1;
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00006568 if (*ml_get(lnum) == NUL && u_savedel(lnum, 1L) == OK)
Bram Moolenaardf177f62005-02-22 08:39:57 +00006569 {
Bram Moolenaarca70c072020-05-30 20:30:46 +02006570 ml_delete(lnum);
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00006571 if (curwin->w_cursor.lnum > 1
6572 && curwin->w_cursor.lnum >= lnum)
Bram Moolenaardf177f62005-02-22 08:39:57 +00006573 --curwin->w_cursor.lnum;
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00006574 deleted_lines_mark(lnum, 1L);
Bram Moolenaardf177f62005-02-22 08:39:57 +00006575 }
6576 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006577 redraw_curbuf_later(VALID);
Bram Moolenaardf177f62005-02-22 08:39:57 +00006578 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006579 }
6580}
6581
Bram Moolenaarea408852005-06-25 22:49:46 +00006582static char_u *prev_dir = NULL;
6583
6584#if defined(EXITFREE) || defined(PROTO)
6585 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006586free_cd_dir(void)
Bram Moolenaarea408852005-06-25 22:49:46 +00006587{
Bram Moolenaard23a8232018-02-10 18:45:26 +01006588 VIM_CLEAR(prev_dir);
6589 VIM_CLEAR(globaldir);
Bram Moolenaarea408852005-06-25 22:49:46 +00006590}
6591#endif
6592
Bram Moolenaarf4258302013-06-02 18:20:17 +02006593/*
Bram Moolenaar002bc792020-06-05 22:33:42 +02006594 * Get the previous directory for the given chdir scope.
6595 */
6596 static char_u *
6597get_prevdir(cdscope_T scope)
6598{
6599 if (scope == CDSCOPE_WINDOW)
6600 return curwin->w_prevdir;
6601 else if (scope == CDSCOPE_TABPAGE)
6602 return curtab->tp_prevdir;
6603 return prev_dir;
6604}
6605
6606/*
Bram Moolenaarf4258302013-06-02 18:20:17 +02006607 * Deal with the side effects of changing the current directory.
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006608 * When 'scope' is CDSCOPE_TABPAGE then this was after an ":tcd" command.
6609 * When 'scope' is CDSCOPE_WINDOW then this was after an ":lcd" command.
Bram Moolenaarf4258302013-06-02 18:20:17 +02006610 */
6611 void
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006612post_chdir(cdscope_T scope)
Bram Moolenaarf4258302013-06-02 18:20:17 +02006613{
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006614 if (scope != CDSCOPE_WINDOW)
Bram Moolenaar00aa0692019-04-27 20:37:57 +02006615 // Clear tab local directory for both :cd and :tcd
6616 VIM_CLEAR(curtab->tp_localdir);
Bram Moolenaard23a8232018-02-10 18:45:26 +01006617 VIM_CLEAR(curwin->w_localdir);
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006618 if (scope != CDSCOPE_GLOBAL)
Bram Moolenaarf4258302013-06-02 18:20:17 +02006619 {
Bram Moolenaar002bc792020-06-05 22:33:42 +02006620 char_u *pdir = get_prevdir(scope);
6621
6622 // If still in the global directory, need to remember current
6623 // directory as the global directory.
6624 if (globaldir == NULL && pdir != NULL)
6625 globaldir = vim_strsave(pdir);
6626
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006627 // Remember this local directory for the window.
Bram Moolenaarf4258302013-06-02 18:20:17 +02006628 if (mch_dirname(NameBuff, MAXPATHL) == OK)
Bram Moolenaar00aa0692019-04-27 20:37:57 +02006629 {
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006630 if (scope == CDSCOPE_TABPAGE)
Bram Moolenaar00aa0692019-04-27 20:37:57 +02006631 curtab->tp_localdir = vim_strsave(NameBuff);
6632 else
6633 curwin->w_localdir = vim_strsave(NameBuff);
6634 }
Bram Moolenaarf4258302013-06-02 18:20:17 +02006635 }
6636 else
6637 {
Bram Moolenaar002bc792020-06-05 22:33:42 +02006638 // We are now in the global directory, no need to remember its name.
Bram Moolenaard23a8232018-02-10 18:45:26 +01006639 VIM_CLEAR(globaldir);
Bram Moolenaarf4258302013-06-02 18:20:17 +02006640 }
6641
6642 shorten_fnames(TRUE);
6643}
6644
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006645/*
6646 * Change directory function used by :cd/:tcd/:lcd Ex commands and the
Bram Moolenaar95058722020-06-01 16:26:19 +02006647 * chdir() function.
6648 * scope == CDSCOPE_WINDOW: changes the window-local directory
6649 * scope == CDSCOPE_TABPAGE: changes the tab-local directory
6650 * Otherwise: changes the global directory
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006651 * Returns TRUE if the directory is successfully changed.
6652 */
6653 int
6654changedir_func(
6655 char_u *new_dir,
6656 int forceit,
6657 cdscope_T scope)
6658{
6659 char_u *tofree;
Bram Moolenaar002bc792020-06-05 22:33:42 +02006660 char_u *pdir = NULL;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006661 int dir_differs;
6662 int retval = FALSE;
6663
Bram Moolenaar7cc96922020-01-31 22:41:38 +01006664 if (new_dir == NULL || allbuf_locked())
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006665 return FALSE;
6666
6667 if (vim_strchr(p_cpo, CPO_CHDIR) != NULL && curbufIsChanged() && !forceit)
6668 {
6669 emsg(_("E747: Cannot change directory, buffer is modified (add ! to override)"));
6670 return FALSE;
6671 }
6672
6673 // ":cd -": Change to previous directory
6674 if (STRCMP(new_dir, "-") == 0)
6675 {
Bram Moolenaar002bc792020-06-05 22:33:42 +02006676 pdir = get_prevdir(scope);
6677 if (pdir == NULL)
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006678 {
6679 emsg(_("E186: No previous directory"));
6680 return FALSE;
6681 }
Bram Moolenaar002bc792020-06-05 22:33:42 +02006682 new_dir = pdir;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006683 }
6684
Bram Moolenaar002bc792020-06-05 22:33:42 +02006685 // Free the previous directory
6686 tofree = get_prevdir(scope);
6687
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006688 // Save current directory for next ":cd -"
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006689 if (mch_dirname(NameBuff, MAXPATHL) == OK)
Bram Moolenaar002bc792020-06-05 22:33:42 +02006690 pdir = vim_strsave(NameBuff);
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006691 else
Bram Moolenaar002bc792020-06-05 22:33:42 +02006692 pdir = NULL;
6693 if (scope == CDSCOPE_WINDOW)
6694 curwin->w_prevdir = pdir;
6695 else if (scope == CDSCOPE_TABPAGE)
6696 curtab->tp_prevdir = pdir;
6697 else
6698 prev_dir = pdir;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006699
6700#if defined(UNIX) || defined(VMS)
6701 // for UNIX ":cd" means: go to home directory
6702 if (*new_dir == NUL)
6703 {
6704 // use NameBuff for home directory name
6705# ifdef VMS
6706 char_u *p;
6707
6708 p = mch_getenv((char_u *)"SYS$LOGIN");
6709 if (p == NULL || *p == NUL) // empty is the same as not set
6710 NameBuff[0] = NUL;
6711 else
6712 vim_strncpy(NameBuff, p, MAXPATHL - 1);
6713# else
6714 expand_env((char_u *)"$HOME", NameBuff, MAXPATHL);
6715# endif
6716 new_dir = NameBuff;
6717 }
6718#endif
Bram Moolenaar002bc792020-06-05 22:33:42 +02006719 dir_differs = new_dir == NULL || pdir == NULL
6720 || pathcmp((char *)pdir, (char *)new_dir, -1) != 0;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006721 if (new_dir == NULL || (dir_differs && vim_chdir(new_dir)))
6722 emsg(_(e_failed));
6723 else
6724 {
6725 char_u *acmd_fname;
6726
6727 post_chdir(scope);
6728
6729 if (dir_differs)
6730 {
6731 if (scope == CDSCOPE_WINDOW)
6732 acmd_fname = (char_u *)"window";
6733 else if (scope == CDSCOPE_TABPAGE)
6734 acmd_fname = (char_u *)"tabpage";
6735 else
6736 acmd_fname = (char_u *)"global";
6737 apply_autocmds(EVENT_DIRCHANGED, acmd_fname, new_dir, FALSE,
6738 curbuf);
6739 }
6740 retval = TRUE;
6741 }
6742 vim_free(tofree);
6743
6744 return retval;
6745}
Bram Moolenaarea408852005-06-25 22:49:46 +00006746
Bram Moolenaar071d4272004-06-13 20:20:40 +00006747/*
Bram Moolenaar00aa0692019-04-27 20:37:57 +02006748 * ":cd", ":tcd", ":lcd", ":chdir" ":tchdir" and ":lchdir".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006749 */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00006750 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006751ex_cd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006752{
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01006753 char_u *new_dir;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006754
6755 new_dir = eap->arg;
6756#if !defined(UNIX) && !defined(VMS)
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006757 // for non-UNIX ":cd" means: print current directory
Bram Moolenaar071d4272004-06-13 20:20:40 +00006758 if (*new_dir == NUL)
6759 ex_pwd(NULL);
6760 else
6761#endif
6762 {
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006763 cdscope_T scope = CDSCOPE_GLOBAL;
6764
6765 if (eap->cmdidx == CMD_lcd || eap->cmdidx == CMD_lchdir)
6766 scope = CDSCOPE_WINDOW;
6767 else if (eap->cmdidx == CMD_tcd || eap->cmdidx == CMD_tchdir)
6768 scope = CDSCOPE_TABPAGE;
6769
6770 if (changedir_func(new_dir, eap->forceit, scope))
Bram Moolenaardf177f62005-02-22 08:39:57 +00006771 {
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006772 // Echo the new current directory if the command was typed.
Bram Moolenaarfcfbc672009-07-09 18:13:49 +00006773 if (KeyTyped || p_verbose >= 5)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006774 ex_pwd(eap);
6775 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006776 }
6777}
6778
6779/*
6780 * ":pwd".
6781 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006782 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006783ex_pwd(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006784{
6785 if (mch_dirname(NameBuff, MAXPATHL) == OK)
6786 {
6787#ifdef BACKSLASH_IN_FILENAME
6788 slash_adjust(NameBuff);
6789#endif
Bram Moolenaar95058722020-06-01 16:26:19 +02006790 if (p_verbose > 0)
6791 {
6792 char *context = "global";
6793
6794 if (curwin->w_localdir != NULL)
6795 context = "window";
6796 else if (curtab->tp_localdir != NULL)
6797 context = "tabpage";
6798 smsg("[%s] %s", context, (char *)NameBuff);
6799 }
6800 else
6801 msg((char *)NameBuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006802 }
6803 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006804 emsg(_("E187: Unknown"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006805}
6806
6807/*
6808 * ":=".
6809 */
6810 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006811ex_equal(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006812{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006813 smsg("%ld", (long)eap->line2);
Bram Moolenaardf177f62005-02-22 08:39:57 +00006814 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006815}
6816
6817 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006818ex_sleep(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006819{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006820 int n;
6821 long len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006822
6823 if (cursor_valid())
6824 {
6825 n = W_WINROW(curwin) + curwin->w_wrow - msg_scrolled;
6826 if (n >= 0)
Bram Moolenaar53f81742017-09-22 14:35:51 +02006827 windgoto((int)n, curwin->w_wincol + curwin->w_wcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006828 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006829
6830 len = eap->line2;
6831 switch (*eap->arg)
6832 {
6833 case 'm': break;
6834 case NUL: len *= 1000L; break;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006835 default: semsg(_(e_invarg2), eap->arg); return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006836 }
6837 do_sleep(len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006838}
6839
6840/*
6841 * Sleep for "msec" milliseconds, but keep checking for a CTRL-C every second.
6842 */
6843 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006844do_sleep(long msec)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006845{
Bram Moolenaar52953192019-08-16 10:27:13 +02006846 long done = 0;
Bram Moolenaar975b5272016-03-15 23:10:59 +01006847 long wait_now;
Bram Moolenaar52953192019-08-16 10:27:13 +02006848# ifdef ELAPSED_FUNC
6849 elapsed_T start_tv;
6850
6851 // Remember at what time we started, so that we know how much longer we
6852 // should wait after waiting for a bit.
6853 ELAPSED_INIT(start_tv);
6854# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006855
6856 cursor_on();
Bram Moolenaarf45d7472018-09-30 18:22:26 +02006857 out_flush_cursor(FALSE, FALSE);
Bram Moolenaar52953192019-08-16 10:27:13 +02006858 while (!got_int && done < msec)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006859 {
Bram Moolenaar975b5272016-03-15 23:10:59 +01006860 wait_now = msec - done > 1000L ? 1000L : msec - done;
6861#ifdef FEAT_TIMERS
6862 {
6863 long due_time = check_due_timer();
6864
6865 if (due_time > 0 && due_time < wait_now)
6866 wait_now = due_time;
6867 }
6868#endif
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02006869#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar28e67e02019-08-15 23:05:49 +02006870 if (has_any_channel() && wait_now > 20L)
6871 wait_now = 20L;
6872#endif
6873#ifdef FEAT_SOUND
6874 if (has_any_sound_callback() && wait_now > 20L)
6875 wait_now = 20L;
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02006876#endif
Bram Moolenaar975b5272016-03-15 23:10:59 +01006877 ui_delay(wait_now, TRUE);
Bram Moolenaar52953192019-08-16 10:27:13 +02006878
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02006879#ifdef FEAT_JOB_CHANNEL
6880 if (has_any_channel())
6881 ui_breakcheck_force(TRUE);
6882 else
6883#endif
6884 ui_breakcheck();
Bram Moolenaar93c88e02015-09-15 14:12:05 +02006885#ifdef MESSAGE_QUEUE
Bram Moolenaar52953192019-08-16 10:27:13 +02006886 // Process the netbeans and clientserver messages that may have been
6887 // received in the call to ui_breakcheck() when the GUI is in use. This
6888 // may occur when running a test case.
Bram Moolenaar93c88e02015-09-15 14:12:05 +02006889 parse_queued_messages();
Bram Moolenaare3cc6d42011-10-20 21:58:34 +02006890#endif
Bram Moolenaar52953192019-08-16 10:27:13 +02006891
6892# ifdef ELAPSED_FUNC
6893 // actual time passed
6894 done = ELAPSED_FUNC(start_tv);
6895# else
6896 // guestimate time passed (will actually be more)
6897 done += wait_now;
6898# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006899 }
Bram Moolenaar1ebff3d2018-07-07 16:18:13 +02006900
6901 // If CTRL-C was typed to interrupt the sleep, drop the CTRL-C from the
6902 // input buffer, otherwise a following call to input() fails.
6903 if (got_int)
6904 (void)vpeekc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006905}
6906
Bram Moolenaar071d4272004-06-13 20:20:40 +00006907/*
6908 * ":winsize" command (obsolete).
6909 */
6910 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006911ex_winsize(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006912{
6913 int w, h;
6914 char_u *arg = eap->arg;
6915 char_u *p;
6916
6917 w = getdigits(&arg);
6918 arg = skipwhite(arg);
6919 p = arg;
6920 h = getdigits(&arg);
6921 if (*p != NUL && *arg == NUL)
6922 set_shellsize(w, h, TRUE);
6923 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006924 emsg(_("E465: :winsize requires two number arguments"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006925}
6926
Bram Moolenaar071d4272004-06-13 20:20:40 +00006927 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006928ex_wincmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006929{
6930 int xchar = NUL;
6931 char_u *p;
6932
6933 if (*eap->arg == 'g' || *eap->arg == Ctrl_G)
6934 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006935 // CTRL-W g and CTRL-W CTRL-G have an extra command character
Bram Moolenaar071d4272004-06-13 20:20:40 +00006936 if (eap->arg[1] == NUL)
6937 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006938 emsg(_(e_invarg));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006939 return;
6940 }
6941 xchar = eap->arg[1];
6942 p = eap->arg + 2;
6943 }
6944 else
6945 p = eap->arg + 1;
6946
6947 eap->nextcmd = check_nextcmd(p);
6948 p = skipwhite(p);
6949 if (*p != NUL && *p != '"' && eap->nextcmd == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006950 emsg(_(e_invarg));
Bram Moolenaar12bde492011-06-13 01:19:56 +02006951 else if (!eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006952 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006953 // Pass flags on for ":vertical wincmd ]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006954 postponed_split_flags = cmdmod.split;
Bram Moolenaard326ce82007-03-11 14:48:29 +00006955 postponed_split_tab = cmdmod.tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006956 do_window(*eap->arg, eap->addr_count > 0 ? eap->line2 : 0L, xchar);
6957 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00006958 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006959 }
6960}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006961
Bram Moolenaar843ee412004-06-30 16:16:41 +00006962#if defined(FEAT_GUI) || defined(UNIX) || defined(VMS) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006963/*
6964 * ":winpos".
6965 */
6966 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006967ex_winpos(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006968{
6969 int x, y;
6970 char_u *arg = eap->arg;
6971 char_u *p;
6972
6973 if (*arg == NUL)
6974 {
Bram Moolenaar843ee412004-06-30 16:16:41 +00006975# if defined(FEAT_GUI) || defined(MSWIN)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02006976# ifdef VIMDLL
6977 if (gui.in_use ? gui_mch_get_winpos(&x, &y) != FAIL :
6978 mch_get_winpos(&x, &y) != FAIL)
6979# elif defined(FEAT_GUI)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006980 if (gui.in_use && gui_mch_get_winpos(&x, &y) != FAIL)
Bram Moolenaar843ee412004-06-30 16:16:41 +00006981# else
6982 if (mch_get_winpos(&x, &y) != FAIL)
6983# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006984 {
6985 sprintf((char *)IObuff, _("Window position: X %d, Y %d"), x, y);
Bram Moolenaar32526b32019-01-19 17:43:09 +01006986 msg((char *)IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006987 }
6988 else
6989# endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006990 emsg(_("E188: Obtaining window position not implemented for this platform"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006991 }
6992 else
6993 {
6994 x = getdigits(&arg);
6995 arg = skipwhite(arg);
6996 p = arg;
6997 y = getdigits(&arg);
6998 if (*p == NUL || *arg != NUL)
6999 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007000 emsg(_("E466: :winpos requires two number arguments"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007001 return;
7002 }
7003# ifdef FEAT_GUI
7004 if (gui.in_use)
7005 gui_mch_set_winpos(x, y);
7006 else if (gui.starting)
7007 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007008 // Remember the coordinates for when the window is opened.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007009 gui_win_x = x;
7010 gui_win_y = y;
7011 }
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007012# if defined(HAVE_TGETENT) || defined(VIMDLL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007013 else
7014# endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007015# endif
7016# if defined(MSWIN) && (!defined(FEAT_GUI) || defined(VIMDLL))
Bram Moolenaar843ee412004-06-30 16:16:41 +00007017 mch_set_winpos(x, y);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007018# endif
7019# ifdef HAVE_TGETENT
7020 if (*T_CWP)
7021 term_set_winpos(x, y);
7022# endif
7023 }
7024}
7025#endif
7026
7027/*
7028 * Handle command that work like operators: ":delete", ":yank", ":>" and ":<".
7029 */
7030 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007031ex_operators(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007032{
7033 oparg_T oa;
7034
7035 clear_oparg(&oa);
7036 oa.regname = eap->regname;
7037 oa.start.lnum = eap->line1;
7038 oa.end.lnum = eap->line2;
7039 oa.line_count = eap->line2 - eap->line1 + 1;
7040 oa.motion_type = MLINE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007041 virtual_op = FALSE;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007042 if (eap->cmdidx != CMD_yank) // position cursor for undo
Bram Moolenaar071d4272004-06-13 20:20:40 +00007043 {
7044 setpcmark();
7045 curwin->w_cursor.lnum = eap->line1;
7046 beginline(BL_SOL | BL_FIX);
7047 }
7048
Bram Moolenaard07c6e12013-11-21 14:21:40 +01007049 if (VIsual_active)
7050 end_visual_mode();
Bram Moolenaard07c6e12013-11-21 14:21:40 +01007051
Bram Moolenaar071d4272004-06-13 20:20:40 +00007052 switch (eap->cmdidx)
7053 {
7054 case CMD_delete:
7055 oa.op_type = OP_DELETE;
7056 op_delete(&oa);
7057 break;
7058
7059 case CMD_yank:
7060 oa.op_type = OP_YANK;
7061 (void)op_yank(&oa, FALSE, TRUE);
7062 break;
7063
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007064 default: // CMD_rshift or CMD_lshift
Bram Moolenaar6e707362013-06-14 19:15:58 +02007065 if (
Bram Moolenaar071d4272004-06-13 20:20:40 +00007066#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6e707362013-06-14 19:15:58 +02007067 (eap->cmdidx == CMD_rshift) ^ curwin->w_p_rl
7068#else
7069 eap->cmdidx == CMD_rshift
Bram Moolenaar071d4272004-06-13 20:20:40 +00007070#endif
Bram Moolenaar6e707362013-06-14 19:15:58 +02007071 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00007072 oa.op_type = OP_RSHIFT;
7073 else
7074 oa.op_type = OP_LSHIFT;
7075 op_shift(&oa, FALSE, eap->amount);
7076 break;
7077 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007078 virtual_op = MAYBE;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007079 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007080}
7081
7082/*
7083 * ":put".
7084 */
7085 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007086ex_put(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007087{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007088 // ":0put" works like ":1put!".
Bram Moolenaar071d4272004-06-13 20:20:40 +00007089 if (eap->line2 == 0)
7090 {
7091 eap->line2 = 1;
7092 eap->forceit = TRUE;
7093 }
7094 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007095 do_put(eap->regname, eap->forceit ? BACKWARD : FORWARD, 1L,
7096 PUT_LINE|PUT_CURSLINE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007097}
7098
7099/*
7100 * Handle ":copy" and ":move".
7101 */
7102 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007103ex_copymove(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007104{
7105 long n;
7106
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02007107 n = get_address(eap, &eap->arg, eap->addr_type, FALSE, FALSE, FALSE, 1);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007108 if (eap->arg == NULL) // error detected
Bram Moolenaar071d4272004-06-13 20:20:40 +00007109 {
7110 eap->nextcmd = NULL;
7111 return;
7112 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00007113 get_flags(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007114
7115 /*
7116 * move or copy lines from 'eap->line1'-'eap->line2' to below line 'n'
7117 */
7118 if (n == MAXLNUM || n < 0 || n > curbuf->b_ml.ml_line_count)
7119 {
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02007120 emsg(_(e_invrange));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007121 return;
7122 }
7123
7124 if (eap->cmdidx == CMD_move)
7125 {
7126 if (do_move(eap->line1, eap->line2, n) == FAIL)
7127 return;
7128 }
7129 else
7130 ex_copy(eap->line1, eap->line2, n);
7131 u_clearline();
7132 beginline(BL_SOL | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007133 ex_may_print(eap);
7134}
7135
7136/*
7137 * Print the current line if flags were given to the Ex command.
7138 */
Bram Moolenaarfd3fe982014-04-01 17:49:44 +02007139 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007140ex_may_print(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007141{
7142 if (eap->flags != 0)
7143 {
7144 print_line(curwin->w_cursor.lnum, (eap->flags & EXFLAG_NR),
7145 (eap->flags & EXFLAG_LIST));
7146 ex_no_reprint = TRUE;
7147 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007148}
7149
7150/*
7151 * ":smagic" and ":snomagic".
7152 */
7153 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007154ex_submagic(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007155{
7156 int magic_save = p_magic;
7157
7158 p_magic = (eap->cmdidx == CMD_smagic);
7159 do_sub(eap);
7160 p_magic = magic_save;
7161}
7162
7163/*
7164 * ":join".
7165 */
7166 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007167ex_join(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007168{
7169 curwin->w_cursor.lnum = eap->line1;
7170 if (eap->line1 == eap->line2)
7171 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007172 if (eap->addr_count >= 2) // :2,2join does nothing
Bram Moolenaar071d4272004-06-13 20:20:40 +00007173 return;
7174 if (eap->line2 == curbuf->b_ml.ml_line_count)
7175 {
7176 beep_flush();
7177 return;
7178 }
7179 ++eap->line2;
7180 }
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02007181 (void)do_join(eap->line2 - eap->line1 + 1, !eap->forceit, TRUE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007182 beginline(BL_WHITE | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007183 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007184}
7185
7186/*
7187 * ":[addr]@r" or ":[addr]*r": execute register
7188 */
7189 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007190ex_at(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007191{
7192 int c;
Bram Moolenaar60462872009-11-03 11:40:19 +00007193 int prev_len = typebuf.tb_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007194
7195 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaar4930a762016-09-11 14:39:53 +02007196 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007197
7198#ifdef USE_ON_FLY_SCROLL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007199 dont_scroll = TRUE; // disallow scrolling here
Bram Moolenaar071d4272004-06-13 20:20:40 +00007200#endif
7201
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007202 // get the register name. No name means to use the previous one
Bram Moolenaar071d4272004-06-13 20:20:40 +00007203 c = *eap->arg;
7204 if (c == NUL || (c == '*' && *eap->cmd == '*'))
7205 c = '@';
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007206 // Put the register in the typeahead buffer with the "silent" flag.
Bram Moolenaard333d1e2006-11-07 17:43:47 +00007207 if (do_execreg(c, TRUE, vim_strchr(p_cpo, CPO_EXECBUF) != NULL, TRUE)
7208 == FAIL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007209 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007210 beep_flush();
Bram Moolenaardf177f62005-02-22 08:39:57 +00007211 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007212 else
7213 {
7214 int save_efr = exec_from_reg;
7215
7216 exec_from_reg = TRUE;
7217
7218 /*
7219 * Execute from the typeahead buffer.
Bram Moolenaar60462872009-11-03 11:40:19 +00007220 * Continue until the stuff buffer is empty and all added characters
7221 * have been consumed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007222 */
Bram Moolenaar60462872009-11-03 11:40:19 +00007223 while (!stuff_empty() || typebuf.tb_len > prev_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007224 (void)do_cmdline(NULL, getexline, NULL, DOCMD_NOWAIT|DOCMD_VERBOSE);
7225
7226 exec_from_reg = save_efr;
7227 }
7228}
7229
7230/*
7231 * ":!".
7232 */
7233 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007234ex_bang(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007235{
7236 do_bang(eap->addr_count, eap, eap->forceit, TRUE, TRUE);
7237}
7238
7239/*
7240 * ":undo".
7241 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007242 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +01007243ex_undo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007244{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007245 if (eap->addr_count == 1) // :undo 123
Bram Moolenaar730cde92010-06-27 05:18:54 +02007246 undo_time(eap->line2, FALSE, FALSE, TRUE);
Bram Moolenaard3667a22006-03-16 21:35:52 +00007247 else
7248 u_undo(1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007249}
7250
Bram Moolenaar55debbe2010-05-23 23:34:36 +02007251#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar6df6f472010-07-18 18:04:50 +02007252 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007253ex_wundo(exarg_T *eap)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02007254{
7255 char_u hash[UNDO_HASH_SIZE];
7256
7257 u_compute_hash(hash);
7258 u_write_undo(eap->arg, eap->forceit, curbuf, hash);
7259}
7260
Bram Moolenaar6df6f472010-07-18 18:04:50 +02007261 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007262ex_rundo(exarg_T *eap)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02007263{
7264 char_u hash[UNDO_HASH_SIZE];
7265
7266 u_compute_hash(hash);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02007267 u_read_undo(eap->arg, hash, NULL);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02007268}
7269#endif
7270
Bram Moolenaar071d4272004-06-13 20:20:40 +00007271/*
7272 * ":redo".
7273 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007274 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007275ex_redo(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007276{
7277 u_redo(1);
7278}
7279
7280/*
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007281 * ":earlier" and ":later".
7282 */
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007283 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007284ex_later(exarg_T *eap)
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007285{
7286 long count = 0;
7287 int sec = FALSE;
Bram Moolenaar730cde92010-06-27 05:18:54 +02007288 int file = FALSE;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007289 char_u *p = eap->arg;
7290
7291 if (*p == NUL)
7292 count = 1;
7293 else if (isdigit(*p))
7294 {
7295 count = getdigits(&p);
7296 switch (*p)
7297 {
7298 case 's': ++p; sec = TRUE; break;
7299 case 'm': ++p; sec = TRUE; count *= 60; break;
7300 case 'h': ++p; sec = TRUE; count *= 60 * 60; break;
Bram Moolenaar730cde92010-06-27 05:18:54 +02007301 case 'd': ++p; sec = TRUE; count *= 24 * 60 * 60; break;
7302 case 'f': ++p; file = TRUE; break;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007303 }
7304 }
7305
7306 if (*p != NUL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007307 semsg(_(e_invarg2), eap->arg);
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007308 else
Bram Moolenaar730cde92010-06-27 05:18:54 +02007309 undo_time(eap->cmdidx == CMD_earlier ? -count : count,
7310 sec, file, FALSE);
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007311}
7312
7313/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007314 * ":redir": start/stop redirection.
7315 */
7316 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007317ex_redir(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007318{
7319 char *mode;
7320 char_u *fname;
Bram Moolenaarca472992005-01-21 11:46:23 +00007321 char_u *arg = eap->arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007322
Bram Moolenaarba768492016-07-08 15:32:54 +02007323#ifdef FEAT_EVAL
Bram Moolenaar79815f12016-07-09 17:07:29 +02007324 if (redir_execute)
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02007325 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007326 emsg(_("E930: Cannot use :redir inside execute()"));
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02007327 return;
7328 }
Bram Moolenaarba768492016-07-08 15:32:54 +02007329#endif
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02007330
Bram Moolenaar071d4272004-06-13 20:20:40 +00007331 if (STRICMP(eap->arg, "END") == 0)
7332 close_redir();
7333 else
7334 {
Bram Moolenaarca472992005-01-21 11:46:23 +00007335 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00007336 {
Bram Moolenaarca472992005-01-21 11:46:23 +00007337 ++arg;
7338 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00007339 {
Bram Moolenaarca472992005-01-21 11:46:23 +00007340 ++arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007341 mode = "a";
7342 }
7343 else
7344 mode = "w";
Bram Moolenaarca472992005-01-21 11:46:23 +00007345 arg = skipwhite(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007346
7347 close_redir();
7348
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007349 // Expand environment variables and "~/".
Bram Moolenaarca472992005-01-21 11:46:23 +00007350 fname = expand_env_save(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007351 if (fname == NULL)
7352 return;
7353#ifdef FEAT_BROWSE
7354 if (cmdmod.browse)
7355 {
7356 char_u *browseFile;
7357
Bram Moolenaar7171abe2004-10-11 10:06:20 +00007358 browseFile = do_browse(BROWSE_SAVE,
7359 (char_u *)_("Save Redirection"),
Bram Moolenaarc36651b2018-04-29 12:22:56 +02007360 fname, NULL, NULL,
7361 (char_u *)_(BROWSE_FILTER_ALL_FILES), curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007362 if (browseFile == NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007363 return; // operation cancelled
Bram Moolenaar071d4272004-06-13 20:20:40 +00007364 vim_free(fname);
7365 fname = browseFile;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007366 eap->forceit = TRUE; // since dialog already asked
Bram Moolenaar071d4272004-06-13 20:20:40 +00007367 }
7368#endif
7369
7370 redir_fd = open_exfile(fname, eap->forceit, mode);
7371 vim_free(fname);
7372 }
7373#ifdef FEAT_EVAL
Bram Moolenaarca472992005-01-21 11:46:23 +00007374 else if (*arg == '@')
Bram Moolenaar071d4272004-06-13 20:20:40 +00007375 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007376 // redirect to a register a-z (resp. A-Z for appending)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007377 close_redir();
Bram Moolenaarca472992005-01-21 11:46:23 +00007378 ++arg;
7379 if (ASCII_ISALPHA(*arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007380# ifdef FEAT_CLIPBOARD
Bram Moolenaarca472992005-01-21 11:46:23 +00007381 || *arg == '*'
Bram Moolenaar9a51c6e2006-11-14 19:25:02 +00007382 || *arg == '+'
Bram Moolenaar071d4272004-06-13 20:20:40 +00007383# endif
Bram Moolenaarca472992005-01-21 11:46:23 +00007384 || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00007385 {
Bram Moolenaarca472992005-01-21 11:46:23 +00007386 redir_reg = *arg++;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007387 if (*arg == '>' && arg[1] == '>') // append
Bram Moolenaard9d30582005-05-18 22:10:28 +00007388 arg += 2;
Bram Moolenaarc188b882007-10-19 14:20:54 +00007389 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007390 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007391 // Can use both "@a" and "@a>".
Bram Moolenaar2c29bee2005-06-01 21:46:07 +00007392 if (*arg == '>')
7393 arg++;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007394 // Make register empty when not using @A-@Z and the
7395 // command is valid.
Bram Moolenaarc188b882007-10-19 14:20:54 +00007396 if (*arg == NUL && !isupper(redir_reg))
7397 write_reg_contents(redir_reg, (char_u *)"", -1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007398 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00007399 }
7400 if (*arg != NUL)
7401 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00007402 redir_reg = 0;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007403 semsg(_(e_invarg2), eap->arg);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007404 }
7405 }
7406 else if (*arg == '=' && arg[1] == '>')
7407 {
7408 int append;
7409
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007410 // redirect to a variable
Bram Moolenaardf177f62005-02-22 08:39:57 +00007411 close_redir();
7412 arg += 2;
7413
7414 if (*arg == '>')
7415 {
7416 ++arg;
7417 append = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007418 }
7419 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00007420 append = FALSE;
7421
7422 if (var_redir_start(skipwhite(arg), append) == OK)
7423 redir_vname = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007424 }
7425#endif
7426
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007427 // TODO: redirect to a buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00007428
Bram Moolenaar071d4272004-06-13 20:20:40 +00007429 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007430 semsg(_(e_invarg2), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007431 }
Bram Moolenaar29b2d262006-09-10 19:07:28 +00007432
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007433 // Make sure redirection is not off. Can happen for cmdline completion
7434 // that indirectly invokes a command to catch its output.
Bram Moolenaar29b2d262006-09-10 19:07:28 +00007435 if (redir_fd != NULL
7436#ifdef FEAT_EVAL
7437 || redir_reg || redir_vname
7438#endif
7439 )
7440 redir_off = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007441}
7442
7443/*
7444 * ":redraw": force redraw
7445 */
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01007446 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007447ex_redraw(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007448{
7449 int r = RedrawingDisabled;
7450 int p = p_lz;
7451
7452 RedrawingDisabled = 0;
7453 p_lz = FALSE;
Bram Moolenaarabc39ab2017-03-01 18:04:05 +01007454 validate_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007455 update_topline();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007456 update_screen(eap->forceit ? CLEAR : VIsual_active ? INVERTED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007457#ifdef FEAT_TITLE
7458 if (need_maketitle)
7459 maketitle();
7460#endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007461#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
7462# ifdef VIMDLL
7463 if (!gui.in_use)
7464# endif
7465 resize_console_buf();
Bram Moolenaar78d21da2019-02-17 15:00:52 +01007466#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007467 RedrawingDisabled = r;
7468 p_lz = p;
7469
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007470 // Reset msg_didout, so that a message that's there is overwritten.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007471 msg_didout = FALSE;
7472 msg_col = 0;
7473
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007474 // No need to wait after an intentional redraw.
Bram Moolenaar56667a52013-07-17 11:54:28 +02007475 need_wait_return = FALSE;
7476
Bram Moolenaar071d4272004-06-13 20:20:40 +00007477 out_flush();
7478}
7479
7480/*
7481 * ":redrawstatus": force redraw of status line(s)
7482 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007483 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007484ex_redrawstatus(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007485{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007486 int r = RedrawingDisabled;
7487 int p = p_lz;
7488
7489 RedrawingDisabled = 0;
7490 p_lz = FALSE;
7491 if (eap->forceit)
7492 status_redraw_all();
7493 else
7494 status_redraw_curbuf();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007495 update_screen(VIsual_active ? INVERTED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007496 RedrawingDisabled = r;
7497 p_lz = p;
7498 out_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007499}
7500
Bram Moolenaare12bab32019-01-08 22:02:56 +01007501/*
7502 * ":redrawtabline": force redraw of the tabline
7503 */
7504 static void
7505ex_redrawtabline(exarg_T *eap UNUSED)
7506{
7507 int r = RedrawingDisabled;
7508 int p = p_lz;
7509
7510 RedrawingDisabled = 0;
7511 p_lz = FALSE;
7512
7513 draw_tabline();
7514
7515 RedrawingDisabled = r;
7516 p_lz = p;
7517 out_flush();
7518}
7519
Bram Moolenaar071d4272004-06-13 20:20:40 +00007520 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007521close_redir(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007522{
7523 if (redir_fd != NULL)
7524 {
7525 fclose(redir_fd);
7526 redir_fd = NULL;
7527 }
7528#ifdef FEAT_EVAL
7529 redir_reg = 0;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007530 if (redir_vname)
7531 {
7532 var_redir_stop();
7533 redir_vname = 0;
7534 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007535#endif
7536}
7537
Bram Moolenaarf96ae0b2019-07-28 15:21:55 +02007538#if (defined(FEAT_SESSION) || defined(FEAT_EVAL)) || defined(PROTO)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007539 int
Bram Moolenaarbd67aac2019-09-21 23:09:04 +02007540vim_mkdir_emsg(char_u *name, int prot UNUSED)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007541{
7542 if (vim_mkdir(name, prot) != 0)
7543 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007544 semsg(_("E739: Cannot create directory: %s"), name);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007545 return FAIL;
7546 }
7547 return OK;
7548}
7549#endif
7550
Bram Moolenaar071d4272004-06-13 20:20:40 +00007551/*
7552 * Open a file for writing for an Ex command, with some checks.
7553 * Return file descriptor, or NULL on failure.
7554 */
7555 FILE *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007556open_exfile(
7557 char_u *fname,
7558 int forceit,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007559 char *mode) // "w" for create new file or "a" for append
Bram Moolenaar071d4272004-06-13 20:20:40 +00007560{
7561 FILE *fd;
7562
7563#ifdef UNIX
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007564 // with Unix it is possible to open a directory
Bram Moolenaar071d4272004-06-13 20:20:40 +00007565 if (mch_isdir(fname))
7566 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007567 semsg(_(e_isadir2), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007568 return NULL;
7569 }
7570#endif
7571 if (!forceit && *mode != 'a' && vim_fexists(fname))
7572 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007573 semsg(_("E189: \"%s\" exists (add ! to override)"), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007574 return NULL;
7575 }
7576
7577 if ((fd = mch_fopen((char *)fname, mode)) == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007578 semsg(_("E190: Cannot open \"%s\" for writing"), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007579
7580 return fd;
7581}
7582
7583/*
7584 * ":mark" and ":k".
7585 */
7586 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007587ex_mark(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007588{
7589 pos_T pos;
7590
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007591 if (*eap->arg == NUL) // No argument?
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007592 emsg(_(e_argreq));
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007593 else if (eap->arg[1] != NUL) // more than one character?
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007594 emsg(_(e_trailing));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007595 else
7596 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007597 pos = curwin->w_cursor; // save curwin->w_cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00007598 curwin->w_cursor.lnum = eap->line2;
7599 beginline(BL_WHITE | BL_FIX);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007600 if (setmark(*eap->arg) == FAIL) // set mark
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007601 emsg(_("E191: Argument must be a letter or forward/backward quote"));
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007602 curwin->w_cursor = pos; // restore curwin->w_cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00007603 }
7604}
7605
7606/*
7607 * Update w_topline, w_leftcol and the cursor position.
7608 */
7609 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007610update_topline_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007611{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007612 check_cursor(); // put cursor on valid line
Bram Moolenaar071d4272004-06-13 20:20:40 +00007613 update_topline();
7614 if (!curwin->w_p_wrap)
7615 validate_cursor();
7616 update_curswant();
7617}
7618
Bram Moolenaar071d4272004-06-13 20:20:40 +00007619/*
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007620 * Save the current State and go to Normal mode.
7621 * Return TRUE if the typeahead could be saved.
7622 */
7623 int
7624save_current_state(save_state_T *sst)
7625{
7626 sst->save_msg_scroll = msg_scroll;
7627 sst->save_restart_edit = restart_edit;
7628 sst->save_msg_didout = msg_didout;
7629 sst->save_State = State;
7630 sst->save_insertmode = p_im;
7631 sst->save_finish_op = finish_op;
7632 sst->save_opcount = opcount;
Bram Moolenaarcce713d2019-03-04 11:40:12 +01007633 sst->save_reg_executing = reg_executing;
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007634
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007635 msg_scroll = FALSE; // no msg scrolling in Normal mode
7636 restart_edit = 0; // don't go to Insert mode
7637 p_im = FALSE; // don't use 'insertmode'
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007638
7639 /*
7640 * Save the current typeahead. This is required to allow using ":normal"
7641 * from an event handler and makes sure we don't hang when the argument
7642 * ends with half a command.
7643 */
7644 save_typeahead(&sst->tabuf);
7645 return sst->tabuf.typebuf_valid;
7646}
7647
7648 void
7649restore_current_state(save_state_T *sst)
7650{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007651 // Restore the previous typeahead.
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007652 restore_typeahead(&sst->tabuf);
7653
7654 msg_scroll = sst->save_msg_scroll;
7655 restart_edit = sst->save_restart_edit;
7656 p_im = sst->save_insertmode;
7657 finish_op = sst->save_finish_op;
7658 opcount = sst->save_opcount;
Bram Moolenaarcce713d2019-03-04 11:40:12 +01007659 reg_executing = sst->save_reg_executing;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007660 msg_didout |= sst->save_msg_didout; // don't reset msg_didout now
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007661
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007662 // Restore the state (needed when called from a function executed for
7663 // 'indentexpr'). Update the mouse and cursor, they may have changed.
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007664 State = sst->save_State;
7665#ifdef CURSOR_SHAPE
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007666 ui_cursor_shape(); // may show different cursor shape
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007667#endif
7668}
7669
7670/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007671 * ":normal[!] {commands}": Execute normal mode commands.
7672 */
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01007673 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007674ex_normal(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007675{
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007676 save_state_T save_state;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007677 char_u *arg = NULL;
7678 int l;
7679 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007680
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00007681 if (ex_normal_lock > 0)
7682 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007683 emsg(_(e_secure));
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00007684 return;
7685 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007686 if (ex_normal_busy >= p_mmd)
7687 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007688 emsg(_("E192: Recursive use of :normal too deep"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007689 return;
7690 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007691
Bram Moolenaar071d4272004-06-13 20:20:40 +00007692 /*
7693 * vgetc() expects a CSI and K_SPECIAL to have been escaped. Don't do
7694 * this for the K_SPECIAL leading byte, otherwise special keys will not
7695 * work.
7696 */
7697 if (has_mbyte)
7698 {
7699 int len = 0;
7700
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007701 // Count the number of characters to be escaped.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007702 for (p = eap->arg; *p != NUL; ++p)
7703 {
Bram Moolenaar13505972019-01-24 15:04:48 +01007704#ifdef FEAT_GUI
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007705 if (*p == CSI) // leadbyte CSI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007706 len += 2;
Bram Moolenaar13505972019-01-24 15:04:48 +01007707#endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007708 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007709 if (*++p == K_SPECIAL // trailbyte K_SPECIAL or CSI
Bram Moolenaar13505972019-01-24 15:04:48 +01007710#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007711 || *p == CSI
Bram Moolenaar13505972019-01-24 15:04:48 +01007712#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007713 )
7714 len += 2;
7715 }
7716 if (len > 0)
7717 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02007718 arg = alloc(STRLEN(eap->arg) + len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007719 if (arg != NULL)
7720 {
7721 len = 0;
7722 for (p = eap->arg; *p != NUL; ++p)
7723 {
7724 arg[len++] = *p;
Bram Moolenaar13505972019-01-24 15:04:48 +01007725#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007726 if (*p == CSI)
7727 {
7728 arg[len++] = KS_EXTRA;
7729 arg[len++] = (int)KE_CSI;
7730 }
Bram Moolenaar13505972019-01-24 15:04:48 +01007731#endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007732 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007733 {
7734 arg[len++] = *++p;
7735 if (*p == K_SPECIAL)
7736 {
7737 arg[len++] = KS_SPECIAL;
7738 arg[len++] = KE_FILLER;
7739 }
Bram Moolenaar13505972019-01-24 15:04:48 +01007740#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007741 else if (*p == CSI)
7742 {
7743 arg[len++] = KS_EXTRA;
7744 arg[len++] = (int)KE_CSI;
7745 }
Bram Moolenaar13505972019-01-24 15:04:48 +01007746#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007747 }
7748 arg[len] = NUL;
7749 }
7750 }
7751 }
7752 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007753
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007754 ++ex_normal_busy;
7755 if (save_current_state(&save_state))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007756 {
7757 /*
7758 * Repeat the :normal command for each line in the range. When no
7759 * range given, execute it just once, without positioning the cursor
7760 * first.
7761 */
7762 do
7763 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007764 if (eap->addr_count != 0)
7765 {
7766 curwin->w_cursor.lnum = eap->line1++;
7767 curwin->w_cursor.col = 0;
Bram Moolenaard5d37532017-03-27 23:02:07 +02007768 check_cursor_moved(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007769 }
7770
Bram Moolenaar13505972019-01-24 15:04:48 +01007771 exec_normal_cmd(arg != NULL
7772 ? arg
7773 : eap->arg, eap->forceit ? REMAP_NONE : REMAP_YES, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007774 }
7775 while (eap->addr_count > 0 && eap->line1 <= eap->line2 && !got_int);
7776 }
7777
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007778 // Might not return to the main loop when in an event handler.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007779 update_topline_cursor();
7780
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007781 restore_current_state(&save_state);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007782 --ex_normal_busy;
Bram Moolenaareda73602014-11-05 09:53:23 +01007783 setmouse();
Bram Moolenaareda73602014-11-05 09:53:23 +01007784#ifdef CURSOR_SHAPE
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007785 ui_cursor_shape(); // may show different cursor shape
Bram Moolenaareda73602014-11-05 09:53:23 +01007786#endif
7787
Bram Moolenaar071d4272004-06-13 20:20:40 +00007788 vim_free(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007789}
7790
7791/*
Bram Moolenaar64969662005-12-14 21:59:55 +00007792 * ":startinsert", ":startreplace" and ":startgreplace"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007793 */
7794 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007795ex_startinsert(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007796{
Bram Moolenaarfd371682005-01-14 21:42:54 +00007797 if (eap->forceit)
7798 {
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02007799 // cursor line can be zero on startup
Bram Moolenaar09ca9322017-09-26 17:40:45 +02007800 if (!curwin->w_cursor.lnum)
7801 curwin->w_cursor.lnum = 1;
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02007802 set_cursor_for_append_to_line();
Bram Moolenaarfd371682005-01-14 21:42:54 +00007803 }
Bram Moolenaareb58a242020-04-19 18:13:19 +02007804#ifdef FEAT_TERMINAL
7805 // Ignore this when running in an active terminal.
7806 if (term_job_running(curbuf->b_term))
7807 return;
7808#endif
Bram Moolenaarfd371682005-01-14 21:42:54 +00007809
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02007810 // Ignore the command when already in Insert mode. Inserting an
7811 // expression register that invokes a function can do this.
Bram Moolenaara40c5002005-01-09 21:16:21 +00007812 if (State & INSERT)
7813 return;
7814
Bram Moolenaar64969662005-12-14 21:59:55 +00007815 if (eap->cmdidx == CMD_startinsert)
7816 restart_edit = 'a';
7817 else if (eap->cmdidx == CMD_startreplace)
7818 restart_edit = 'R';
Bram Moolenaar071d4272004-06-13 20:20:40 +00007819 else
Bram Moolenaar64969662005-12-14 21:59:55 +00007820 restart_edit = 'V';
7821
7822 if (!eap->forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007823 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00007824 if (eap->cmdidx == CMD_startinsert)
7825 restart_edit = 'i';
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02007826 curwin->w_curswant = 0; // avoid MAXCOL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007827 }
7828}
7829
7830/*
7831 * ":stopinsert"
7832 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007833 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007834ex_stopinsert(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007835{
7836 restart_edit = 0;
7837 stop_insert_mode = TRUE;
Bram Moolenaarfd773e92016-04-02 19:39:16 +02007838 clearmode();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007839}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007840
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00007841/*
7842 * Execute normal mode command "cmd".
7843 * "remap" can be REMAP_NONE or REMAP_YES.
7844 */
7845 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007846exec_normal_cmd(char_u *cmd, int remap, int silent)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00007847{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007848 // Stuff the argument into the typeahead buffer.
Bram Moolenaar25281632016-01-21 23:32:32 +01007849 ins_typebuf(cmd, remap, 0, TRUE, silent);
Bram Moolenaar586c70c2018-10-02 16:23:58 +02007850 exec_normal(FALSE, FALSE, FALSE);
Bram Moolenaar25281632016-01-21 23:32:32 +01007851}
Bram Moolenaar25281632016-01-21 23:32:32 +01007852
Bram Moolenaar25281632016-01-21 23:32:32 +01007853/*
7854 * Execute normal_cmd() until there is no typeahead left.
Bram Moolenaar586c70c2018-10-02 16:23:58 +02007855 * When "use_vpeekc" is TRUE use vpeekc() to check for available chars.
Bram Moolenaar25281632016-01-21 23:32:32 +01007856 */
7857 void
Bram Moolenaar586c70c2018-10-02 16:23:58 +02007858exec_normal(int was_typed, int use_vpeekc, int may_use_terminal_loop UNUSED)
Bram Moolenaar25281632016-01-21 23:32:32 +01007859{
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00007860 oparg_T oa;
Bram Moolenaar905dd902019-04-07 14:21:47 +02007861 int c;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00007862
Bram Moolenaar905dd902019-04-07 14:21:47 +02007863 // When calling vpeekc() from feedkeys() it will return Ctrl_C when there
7864 // is nothing to get, so also check for Ctrl_C.
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00007865 clear_oparg(&oa);
7866 finish_op = FALSE;
Bram Moolenaar586c70c2018-10-02 16:23:58 +02007867 while ((!stuff_empty()
7868 || ((was_typed || !typebuf_typed()) && typebuf.tb_len > 0)
Bram Moolenaar905dd902019-04-07 14:21:47 +02007869 || (use_vpeekc && (c = vpeekc()) != NUL && c != Ctrl_C))
Bram Moolenaar586c70c2018-10-02 16:23:58 +02007870 && !got_int)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00007871 {
7872 update_topline_cursor();
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02007873#ifdef FEAT_TERMINAL
Bram Moolenaar655a82a2018-05-08 22:01:07 +02007874 if (may_use_terminal_loop && term_use_loop()
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02007875 && oa.op_type == OP_NOP && oa.regname == NUL
7876 && !VIsual_active)
7877 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007878 // If terminal_loop() returns OK we got a key that is handled
7879 // in Normal model. With FAIL we first need to position the
7880 // cursor and the screen needs to be redrawn.
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02007881 if (terminal_loop(TRUE) == OK)
7882 normal_cmd(&oa, TRUE);
7883 }
7884 else
7885#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007886 // execute a Normal mode cmd
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02007887 normal_cmd(&oa, TRUE);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00007888 }
7889}
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00007890
Bram Moolenaar071d4272004-06-13 20:20:40 +00007891#ifdef FEAT_FIND_ID
7892 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007893ex_checkpath(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007894{
7895 find_pattern_in_path(NULL, 0, 0, FALSE, FALSE, CHECK_PATH, 1L,
7896 eap->forceit ? ACTION_SHOW_ALL : ACTION_SHOW,
7897 (linenr_T)1, (linenr_T)MAXLNUM);
7898}
7899
Bram Moolenaar4033c552017-09-16 20:54:51 +02007900#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007901/*
7902 * ":psearch"
7903 */
7904 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007905ex_psearch(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007906{
7907 g_do_tagpreview = p_pvh;
7908 ex_findpat(eap);
7909 g_do_tagpreview = 0;
7910}
7911#endif
7912
7913 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007914ex_findpat(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007915{
7916 int whole = TRUE;
7917 long n;
7918 char_u *p;
7919 int action;
7920
7921 switch (cmdnames[eap->cmdidx].cmd_name[2])
7922 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007923 case 'e': // ":psearch", ":isearch" and ":dsearch"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007924 if (cmdnames[eap->cmdidx].cmd_name[0] == 'p')
7925 action = ACTION_GOTO;
7926 else
7927 action = ACTION_SHOW;
7928 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007929 case 'i': // ":ilist" and ":dlist"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007930 action = ACTION_SHOW_ALL;
7931 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007932 case 'u': // ":ijump" and ":djump"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007933 action = ACTION_GOTO;
7934 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007935 default: // ":isplit" and ":dsplit"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007936 action = ACTION_SPLIT;
7937 break;
7938 }
7939
7940 n = 1;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007941 if (vim_isdigit(*eap->arg)) // get count
Bram Moolenaar071d4272004-06-13 20:20:40 +00007942 {
7943 n = getdigits(&eap->arg);
7944 eap->arg = skipwhite(eap->arg);
7945 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007946 if (*eap->arg == '/') // Match regexp, not just whole words
Bram Moolenaar071d4272004-06-13 20:20:40 +00007947 {
7948 whole = FALSE;
7949 ++eap->arg;
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02007950 p = skip_regexp(eap->arg, '/', p_magic);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007951 if (*p)
7952 {
7953 *p++ = NUL;
7954 p = skipwhite(p);
7955
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007956 // Check for trailing illegal characters
Bram Moolenaarfaac4102020-04-20 17:46:14 +02007957 if (!ends_excmd2(eap->arg, p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007958 eap->errmsg = e_trailing;
7959 else
7960 eap->nextcmd = check_nextcmd(p);
7961 }
7962 }
7963 if (!eap->skip)
7964 find_pattern_in_path(eap->arg, 0, (int)STRLEN(eap->arg),
7965 whole, !eap->forceit,
7966 *eap->cmd == 'd' ? FIND_DEFINE : FIND_ANY,
7967 n, action, eap->line1, eap->line2);
7968}
7969#endif
7970
Bram Moolenaar071d4272004-06-13 20:20:40 +00007971
Bram Moolenaar4033c552017-09-16 20:54:51 +02007972#ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00007973/*
7974 * ":ptag", ":ptselect", ":ptjump", ":ptnext", etc.
7975 */
7976 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007977ex_ptag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007978{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007979 g_do_tagpreview = p_pvh; // will be reset to 0 in ex_tag_cmd()
Bram Moolenaar071d4272004-06-13 20:20:40 +00007980 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
7981}
7982
7983/*
7984 * ":pedit"
7985 */
7986 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007987ex_pedit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007988{
7989 win_T *curwin_save = curwin;
7990
Bram Moolenaar3a2505c2020-03-09 16:40:41 +01007991 if (ERROR_IF_ANY_POPUP_WINDOW)
7992 return;
7993
Bram Moolenaar026587b2019-08-17 15:08:00 +02007994 // Open the preview window or popup and make it the current window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007995 g_do_tagpreview = p_pvh;
Bram Moolenaar576a4a62019-08-18 15:25:17 +02007996 prepare_tagpreview(TRUE, TRUE, FALSE);
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02007997
Bram Moolenaar026587b2019-08-17 15:08:00 +02007998 // Edit the file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007999 do_exedit(eap, NULL);
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02008000
Bram Moolenaar071d4272004-06-13 20:20:40 +00008001 if (curwin != curwin_save && win_valid(curwin_save))
8002 {
Bram Moolenaar026587b2019-08-17 15:08:00 +02008003 // Return cursor to where we were
Bram Moolenaar071d4272004-06-13 20:20:40 +00008004 validate_cursor();
8005 redraw_later(VALID);
8006 win_enter(curwin_save, TRUE);
8007 }
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01008008# ifdef FEAT_PROP_POPUP
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02008009 else if (WIN_IS_POPUP(curwin))
8010 {
8011 // can't keep focus in popup window
8012 win_enter(firstwin, TRUE);
8013 }
8014# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008015 g_do_tagpreview = 0;
8016}
Bram Moolenaar4033c552017-09-16 20:54:51 +02008017#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008018
8019/*
8020 * ":stag", ":stselect" and ":stjump".
8021 */
8022 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008023ex_stag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008024{
8025 postponed_split = -1;
8026 postponed_split_flags = cmdmod.split;
Bram Moolenaard326ce82007-03-11 14:48:29 +00008027 postponed_split_tab = cmdmod.tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008028 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
8029 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00008030 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008031}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008032
8033/*
8034 * ":tag", ":tselect", ":tjump", ":tnext", etc.
8035 */
8036 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008037ex_tag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008038{
8039 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name);
8040}
8041
8042 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008043ex_tag_cmd(exarg_T *eap, char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008044{
8045 int cmd;
8046
8047 switch (name[1])
8048 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008049 case 'j': cmd = DT_JUMP; // ":tjump"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008050 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008051 case 's': cmd = DT_SELECT; // ":tselect"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008052 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008053 case 'p': cmd = DT_PREV; // ":tprevious"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008054 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008055 case 'N': cmd = DT_PREV; // ":tNext"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008056 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008057 case 'n': cmd = DT_NEXT; // ":tnext"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008058 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008059 case 'o': cmd = DT_POP; // ":pop"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008060 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008061 case 'f': // ":tfirst"
8062 case 'r': cmd = DT_FIRST; // ":trewind"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008063 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008064 case 'l': cmd = DT_LAST; // ":tlast"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008065 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008066 default: // ":tag"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008067#ifdef FEAT_CSCOPE
Bram Moolenaar7c94c262008-06-20 09:11:34 +00008068 if (p_cst && *eap->arg != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008069 {
Bram Moolenaard4db7712016-11-12 19:16:46 +01008070 ex_cstag(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008071 return;
8072 }
8073#endif
8074 cmd = DT_TAG;
8075 break;
8076 }
8077
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00008078 if (name[0] == 'l')
8079 {
8080#ifndef FEAT_QUICKFIX
8081 ex_ni(eap);
8082 return;
8083#else
8084 cmd = DT_LTAG;
8085#endif
8086 }
8087
Bram Moolenaar071d4272004-06-13 20:20:40 +00008088 do_tag(eap->arg, cmd, eap->addr_count > 0 ? (int)eap->line2 : 1,
8089 eap->forceit, TRUE);
8090}
8091
8092/*
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008093 * Check "str" for starting with a special cmdline variable.
8094 * If found return one of the SPEC_ values and set "*usedlen" to the length of
8095 * the variable. Otherwise return -1 and "*usedlen" is unchanged.
8096 */
8097 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008098find_cmdline_var(char_u *src, int *usedlen)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008099{
8100 int len;
8101 int i;
Bram Moolenaar8f0b2d42009-05-16 14:41:10 +00008102 static char *(spec_str[]) = {
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008103 "%",
8104#define SPEC_PERC 0
8105 "#",
Bram Moolenaar65f08472017-09-10 18:16:20 +02008106#define SPEC_HASH (SPEC_PERC + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008107 "<cword>", // cursor word
Bram Moolenaar65f08472017-09-10 18:16:20 +02008108#define SPEC_CWORD (SPEC_HASH + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008109 "<cWORD>", // cursor WORD
Bram Moolenaar65f08472017-09-10 18:16:20 +02008110#define SPEC_CCWORD (SPEC_CWORD + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008111 "<cexpr>", // expr under cursor
Bram Moolenaar65f08472017-09-10 18:16:20 +02008112#define SPEC_CEXPR (SPEC_CCWORD + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008113 "<cfile>", // cursor path name
Bram Moolenaar65f08472017-09-10 18:16:20 +02008114#define SPEC_CFILE (SPEC_CEXPR + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008115 "<sfile>", // ":so" file name
Bram Moolenaar65f08472017-09-10 18:16:20 +02008116#define SPEC_SFILE (SPEC_CFILE + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008117 "<slnum>", // ":so" file line number
Bram Moolenaar65f08472017-09-10 18:16:20 +02008118#define SPEC_SLNUM (SPEC_SFILE + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008119 "<afile>", // autocommand file name
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008120#define SPEC_AFILE (SPEC_SLNUM + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008121 "<abuf>", // autocommand buffer number
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008122#define SPEC_ABUF (SPEC_AFILE + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008123 "<amatch>", // autocommand match name
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01008124#define SPEC_AMATCH (SPEC_ABUF + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008125 "<sflnum>", // script file line number
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008126#define SPEC_SFLNUM (SPEC_AMATCH + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008127#ifdef FEAT_CLIENTSERVER
8128 "<client>"
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008129# define SPEC_CLIENT (SPEC_SFLNUM + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008130#endif
8131 };
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008132
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00008133 for (i = 0; i < (int)(sizeof(spec_str) / sizeof(char *)); ++i)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008134 {
8135 len = (int)STRLEN(spec_str[i]);
8136 if (STRNCMP(src, spec_str[i], len) == 0)
8137 {
8138 *usedlen = len;
8139 return i;
8140 }
8141 }
8142 return -1;
8143}
8144
8145/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008146 * Evaluate cmdline variables.
8147 *
8148 * change '%' to curbuf->b_ffname
8149 * '#' to curwin->w_altfile
8150 * '<cword>' to word under the cursor
8151 * '<cWORD>' to WORD under the cursor
Bram Moolenaar8071cb22019-07-12 17:58:01 +02008152 * '<cexpr>' to C-expression under the cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00008153 * '<cfile>' to path name under the cursor
8154 * '<sfile>' to sourced file name
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01008155 * '<slnum>' to sourced file line number
Bram Moolenaar071d4272004-06-13 20:20:40 +00008156 * '<afile>' to file name for autocommand
8157 * '<abuf>' to buffer number for autocommand
8158 * '<amatch>' to matching name for autocommand
8159 *
8160 * When an error is detected, "errormsg" is set to a non-NULL pointer (may be
8161 * "" for error without a message) and NULL is returned.
8162 * Returns an allocated string if a valid match was found.
8163 * Returns NULL if no match was found. "usedlen" then still contains the
8164 * number of characters to skip.
8165 */
8166 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008167eval_vars(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008168 char_u *src, // pointer into commandline
8169 char_u *srcstart, // beginning of valid memory for src
8170 int *usedlen, // characters after src that are used
8171 linenr_T *lnump, // line number for :e command, or NULL
8172 char **errormsg, // pointer to error message
8173 int *escaped) // return value has escaped white space (can
8174 // be NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008175{
8176 int i;
8177 char_u *s;
8178 char_u *result;
8179 char_u *resultbuf = NULL;
8180 int resultlen;
8181 buf_T *buf;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008182 int valid = VALID_HEAD + VALID_PATH; // assume valid result
Bram Moolenaar071d4272004-06-13 20:20:40 +00008183 int spec_idx;
Bram Moolenaarfd249462018-07-28 16:14:30 +02008184 int tilde_file = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008185 int skip_mod = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008186 char_u strbuf[30];
Bram Moolenaar071d4272004-06-13 20:20:40 +00008187
8188 *errormsg = NULL;
Bram Moolenaar63b92542007-03-27 14:57:09 +00008189 if (escaped != NULL)
8190 *escaped = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008191
8192 /*
8193 * Check if there is something to do.
8194 */
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008195 spec_idx = find_cmdline_var(src, usedlen);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008196 if (spec_idx < 0) // no match
Bram Moolenaar071d4272004-06-13 20:20:40 +00008197 {
8198 *usedlen = 1;
8199 return NULL;
8200 }
8201
8202 /*
8203 * Skip when preceded with a backslash "\%" and "\#".
8204 * Note: In "\\%" the % is also not recognized!
8205 */
8206 if (src > srcstart && src[-1] == '\\')
8207 {
8208 *usedlen = 0;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008209 STRMOVE(src - 1, src); // remove backslash
Bram Moolenaar071d4272004-06-13 20:20:40 +00008210 return NULL;
8211 }
8212
8213 /*
8214 * word or WORD under cursor
8215 */
Bram Moolenaar65f08472017-09-10 18:16:20 +02008216 if (spec_idx == SPEC_CWORD || spec_idx == SPEC_CCWORD
8217 || spec_idx == SPEC_CEXPR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008218 {
Bram Moolenaar65f08472017-09-10 18:16:20 +02008219 resultlen = find_ident_under_cursor(&result,
8220 spec_idx == SPEC_CWORD ? (FIND_IDENT | FIND_STRING)
8221 : spec_idx == SPEC_CEXPR ? (FIND_IDENT | FIND_STRING | FIND_EVAL)
8222 : FIND_STRING);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008223 if (resultlen == 0)
8224 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008225 *errormsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +00008226 return NULL;
8227 }
8228 }
8229
8230 /*
8231 * '#': Alternate file name
8232 * '%': Current file name
8233 * File name under the cursor
8234 * File name for autocommand
8235 * and following modifiers
8236 */
8237 else
8238 {
8239 switch (spec_idx)
8240 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008241 case SPEC_PERC: // '%': current file
Bram Moolenaar071d4272004-06-13 20:20:40 +00008242 if (curbuf->b_fname == NULL)
8243 {
8244 result = (char_u *)"";
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008245 valid = 0; // Must have ":p:h" to be valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00008246 }
8247 else
Bram Moolenaar00136dc2018-07-25 21:19:13 +02008248 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008249 result = curbuf->b_fname;
Bram Moolenaar00136dc2018-07-25 21:19:13 +02008250 tilde_file = STRCMP(result, "~") == 0;
8251 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008252 break;
8253
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008254 case SPEC_HASH: // '#' or "#99": alternate file
8255 if (src[1] == '#') // "##": the argument list
Bram Moolenaar071d4272004-06-13 20:20:40 +00008256 {
8257 result = arg_all();
8258 resultbuf = result;
8259 *usedlen = 2;
Bram Moolenaar63b92542007-03-27 14:57:09 +00008260 if (escaped != NULL)
8261 *escaped = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008262 skip_mod = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008263 break;
8264 }
8265 s = src + 1;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008266 if (*s == '<') // "#<99" uses v:oldfiles
Bram Moolenaard812df62008-11-09 12:46:09 +00008267 ++s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008268 i = (int)getdigits(&s);
Bram Moolenaarc312b8b2017-10-28 17:53:04 +02008269 if (s == src + 2 && src[1] == '-')
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008270 // just a minus sign, don't skip over it
Bram Moolenaarc312b8b2017-10-28 17:53:04 +02008271 s--;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008272 *usedlen = (int)(s - src); // length of what we expand
Bram Moolenaar071d4272004-06-13 20:20:40 +00008273
Bram Moolenaarc312b8b2017-10-28 17:53:04 +02008274 if (src[1] == '<' && i != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008275 {
Bram Moolenaard812df62008-11-09 12:46:09 +00008276 if (*usedlen < 2)
8277 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008278 // Should we give an error message for #<text?
Bram Moolenaard812df62008-11-09 12:46:09 +00008279 *usedlen = 1;
8280 return NULL;
8281 }
8282#ifdef FEAT_EVAL
8283 result = list_find_str(get_vim_var_list(VV_OLDFILES),
8284 (long)i);
8285 if (result == NULL)
8286 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008287 *errormsg = "";
Bram Moolenaard812df62008-11-09 12:46:09 +00008288 return NULL;
8289 }
8290#else
Bram Moolenaar8e481e82019-01-15 20:07:48 +01008291 *errormsg = _("E809: #< is not available without the +eval feature");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008292 return NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +00008293#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008294 }
8295 else
Bram Moolenaard812df62008-11-09 12:46:09 +00008296 {
Bram Moolenaarc312b8b2017-10-28 17:53:04 +02008297 if (i == 0 && src[1] == '<' && *usedlen > 1)
8298 *usedlen = 1;
Bram Moolenaard812df62008-11-09 12:46:09 +00008299 buf = buflist_findnr(i);
8300 if (buf == NULL)
8301 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008302 *errormsg = _("E194: No alternate file name to substitute for '#'");
Bram Moolenaard812df62008-11-09 12:46:09 +00008303 return NULL;
8304 }
8305 if (lnump != NULL)
8306 *lnump = ECMD_LAST;
8307 if (buf->b_fname == NULL)
8308 {
8309 result = (char_u *)"";
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008310 valid = 0; // Must have ":p:h" to be valid
Bram Moolenaard812df62008-11-09 12:46:09 +00008311 }
8312 else
Bram Moolenaar00136dc2018-07-25 21:19:13 +02008313 {
Bram Moolenaard812df62008-11-09 12:46:09 +00008314 result = buf->b_fname;
Bram Moolenaar00136dc2018-07-25 21:19:13 +02008315 tilde_file = STRCMP(result, "~") == 0;
8316 }
Bram Moolenaard812df62008-11-09 12:46:09 +00008317 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008318 break;
8319
8320#ifdef FEAT_SEARCHPATH
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008321 case SPEC_CFILE: // file name under cursor
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008322 result = file_name_at_cursor(FNAME_MESS|FNAME_HYP, 1L, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008323 if (result == NULL)
8324 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008325 *errormsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +00008326 return NULL;
8327 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008328 resultbuf = result; // remember allocated string
Bram Moolenaar071d4272004-06-13 20:20:40 +00008329 break;
8330#endif
8331
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008332 case SPEC_AFILE: // file name for autocommand
Bram Moolenaar071d4272004-06-13 20:20:40 +00008333 result = autocmd_fname;
Bram Moolenaarf6dad432008-09-18 19:29:58 +00008334 if (result != NULL && !autocmd_fname_full)
8335 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008336 // Still need to turn the fname into a full path. It is
8337 // postponed to avoid a delay when <afile> is not used.
Bram Moolenaarf6dad432008-09-18 19:29:58 +00008338 autocmd_fname_full = TRUE;
8339 result = FullName_save(autocmd_fname, FALSE);
8340 vim_free(autocmd_fname);
8341 autocmd_fname = result;
8342 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008343 if (result == NULL)
8344 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008345 *errormsg = _("E495: no autocommand file name to substitute for \"<afile>\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008346 return NULL;
8347 }
Bram Moolenaara0174af2008-01-02 20:08:25 +00008348 result = shorten_fname1(result);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008349 break;
8350
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008351 case SPEC_ABUF: // buffer number for autocommand
Bram Moolenaar071d4272004-06-13 20:20:40 +00008352 if (autocmd_bufnr <= 0)
8353 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008354 *errormsg = _("E496: no autocommand buffer number to substitute for \"<abuf>\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008355 return NULL;
8356 }
8357 sprintf((char *)strbuf, "%d", autocmd_bufnr);
8358 result = strbuf;
8359 break;
8360
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008361 case SPEC_AMATCH: // match name for autocommand
Bram Moolenaar071d4272004-06-13 20:20:40 +00008362 result = autocmd_match;
8363 if (result == NULL)
8364 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008365 *errormsg = _("E497: no autocommand match name to substitute for \"<amatch>\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008366 return NULL;
8367 }
8368 break;
8369
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008370 case SPEC_SFILE: // file name for ":so" command
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01008371 result = estack_sfile();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008372 if (result == NULL)
8373 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008374 *errormsg = _("E498: no :source file name to substitute for \"<sfile>\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008375 return NULL;
8376 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01008377 resultbuf = result; // remember allocated string
Bram Moolenaar071d4272004-06-13 20:20:40 +00008378 break;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008379
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008380 case SPEC_SLNUM: // line in file for ":so" command
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01008381 if (SOURCING_NAME == NULL || SOURCING_LNUM == 0)
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01008382 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008383 *errormsg = _("E842: no line number to use for \"<slnum>\"");
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01008384 return NULL;
8385 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01008386 sprintf((char *)strbuf, "%ld", SOURCING_LNUM);
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01008387 result = strbuf;
8388 break;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008389
8390#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008391 case SPEC_SFLNUM: // line in script file
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01008392 if (current_sctx.sc_lnum + SOURCING_LNUM == 0)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008393 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008394 *errormsg = _("E961: no line number to use for \"<sflnum>\"");
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008395 return NULL;
8396 }
8397 sprintf((char *)strbuf, "%ld",
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01008398 (long)(current_sctx.sc_lnum + SOURCING_LNUM));
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008399 result = strbuf;
8400 break;
8401#endif
8402
8403#ifdef FEAT_CLIENTSERVER
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008404 case SPEC_CLIENT: // Source of last submitted input
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008405 sprintf((char *)strbuf, PRINTF_HEX_LONG_U,
8406 (long_u)clientWindow);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008407 result = strbuf;
8408 break;
8409#endif
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008410
Bram Moolenaar9e0f6ec2017-05-16 09:36:54 +02008411 default:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008412 result = (char_u *)""; // avoid gcc warning
Bram Moolenaar9e0f6ec2017-05-16 09:36:54 +02008413 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008414 }
8415
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008416 resultlen = (int)STRLEN(result); // length of new string
8417 if (src[*usedlen] == '<') // remove the file name extension
Bram Moolenaar071d4272004-06-13 20:20:40 +00008418 {
8419 ++*usedlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008420 if ((s = vim_strrchr(result, '.')) != NULL && s >= gettail(result))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008421 resultlen = (int)(s - result);
8422 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008423 else if (!skip_mod)
8424 {
Bram Moolenaar00136dc2018-07-25 21:19:13 +02008425 valid |= modify_fname(src, tilde_file, usedlen, &result, &resultbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008426 &resultlen);
8427 if (result == NULL)
8428 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008429 *errormsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +00008430 return NULL;
8431 }
8432 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008433 }
8434
8435 if (resultlen == 0 || valid != VALID_HEAD + VALID_PATH)
8436 {
8437 if (valid != VALID_HEAD + VALID_PATH)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008438 // xgettext:no-c-format
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008439 *errormsg = _("E499: Empty file name for '%' or '#', only works with \":p:h\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008440 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008441 *errormsg = _("E500: Evaluates to an empty string");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008442 result = NULL;
8443 }
8444 else
8445 result = vim_strnsave(result, resultlen);
8446 vim_free(resultbuf);
8447 return result;
8448}
8449
8450/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008451 * Expand the <sfile> string in "arg".
8452 *
8453 * Returns an allocated string, or NULL for any error.
8454 */
8455 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008456expand_sfile(char_u *arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008457{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008458 char *errormsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008459 int len;
8460 char_u *result;
8461 char_u *newres;
8462 char_u *repl;
8463 int srclen;
8464 char_u *p;
8465
8466 result = vim_strsave(arg);
8467 if (result == NULL)
8468 return NULL;
8469
8470 for (p = result; *p; )
8471 {
8472 if (STRNCMP(p, "<sfile>", 7) != 0)
8473 ++p;
8474 else
8475 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008476 // replace "<sfile>" with the sourced file name, and do ":" stuff
Bram Moolenaar63b92542007-03-27 14:57:09 +00008477 repl = eval_vars(p, result, &srclen, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008478 if (errormsg != NULL)
8479 {
8480 if (*errormsg)
8481 emsg(errormsg);
8482 vim_free(result);
8483 return NULL;
8484 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008485 if (repl == NULL) // no match (cannot happen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008486 {
8487 p += srclen;
8488 continue;
8489 }
8490 len = (int)STRLEN(result) - srclen + (int)STRLEN(repl) + 1;
8491 newres = alloc(len);
8492 if (newres == NULL)
8493 {
8494 vim_free(repl);
8495 vim_free(result);
8496 return NULL;
8497 }
8498 mch_memmove(newres, result, (size_t)(p - result));
8499 STRCPY(newres + (p - result), repl);
8500 len = (int)STRLEN(newres);
8501 STRCAT(newres, p + srclen);
8502 vim_free(repl);
8503 vim_free(result);
8504 result = newres;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008505 p = newres + len; // continue after the match
Bram Moolenaar071d4272004-06-13 20:20:40 +00008506 }
8507 }
8508
8509 return result;
8510}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008511
Bram Moolenaar071d4272004-06-13 20:20:40 +00008512#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG) || defined(PROTO)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00008513/*
Bram Moolenaard9462e32011-04-11 21:35:11 +02008514 * Make a dialog message in "buff[DIALOG_MSG_SIZE]".
Bram Moolenaarb765d632005-06-07 21:00:02 +00008515 * "format" must contain "%s".
Bram Moolenaar9c13b352005-05-19 20:53:52 +00008516 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008517 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008518dialog_msg(char_u *buff, char *format, char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008519{
Bram Moolenaar071d4272004-06-13 20:20:40 +00008520 if (fname == NULL)
8521 fname = (char_u *)_("Untitled");
Bram Moolenaard9462e32011-04-11 21:35:11 +02008522 vim_snprintf((char *)buff, DIALOG_MSG_SIZE, format, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008523}
8524#endif
8525
8526/*
8527 * ":behave {mswin,xterm}"
8528 */
8529 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008530ex_behave(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008531{
8532 if (STRCMP(eap->arg, "mswin") == 0)
8533 {
8534 set_option_value((char_u *)"selection", 0L, (char_u *)"exclusive", 0);
8535 set_option_value((char_u *)"selectmode", 0L, (char_u *)"mouse,key", 0);
8536 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"popup", 0);
8537 set_option_value((char_u *)"keymodel", 0L,
8538 (char_u *)"startsel,stopsel", 0);
8539 }
8540 else if (STRCMP(eap->arg, "xterm") == 0)
8541 {
8542 set_option_value((char_u *)"selection", 0L, (char_u *)"inclusive", 0);
8543 set_option_value((char_u *)"selectmode", 0L, (char_u *)"", 0);
8544 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"extend", 0);
8545 set_option_value((char_u *)"keymodel", 0L, (char_u *)"", 0);
8546 }
8547 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008548 semsg(_(e_invarg2), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008549}
8550
Bram Moolenaar071d4272004-06-13 20:20:40 +00008551static int filetype_detect = FALSE;
8552static int filetype_plugin = FALSE;
8553static int filetype_indent = FALSE;
8554
8555/*
8556 * ":filetype [plugin] [indent] {on,off,detect}"
8557 * on: Load the filetype.vim file to install autocommands for file types.
8558 * off: Load the ftoff.vim file to remove all autocommands for file types.
8559 * plugin on: load filetype.vim and ftplugin.vim
8560 * plugin off: load ftplugof.vim
8561 * indent on: load filetype.vim and indent.vim
8562 * indent off: load indoff.vim
8563 */
8564 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008565ex_filetype(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008566{
8567 char_u *arg = eap->arg;
8568 int plugin = FALSE;
8569 int indent = FALSE;
8570
8571 if (*eap->arg == NUL)
8572 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008573 // Print current status.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008574 smsg("filetype detection:%s plugin:%s indent:%s",
Bram Moolenaar071d4272004-06-13 20:20:40 +00008575 filetype_detect ? "ON" : "OFF",
8576 filetype_plugin ? (filetype_detect ? "ON" : "(on)") : "OFF",
8577 filetype_indent ? (filetype_detect ? "ON" : "(on)") : "OFF");
8578 return;
8579 }
8580
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008581 // Accept "plugin" and "indent" in any order.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008582 for (;;)
8583 {
8584 if (STRNCMP(arg, "plugin", 6) == 0)
8585 {
8586 plugin = TRUE;
8587 arg = skipwhite(arg + 6);
8588 continue;
8589 }
8590 if (STRNCMP(arg, "indent", 6) == 0)
8591 {
8592 indent = TRUE;
8593 arg = skipwhite(arg + 6);
8594 continue;
8595 }
8596 break;
8597 }
8598 if (STRCMP(arg, "on") == 0 || STRCMP(arg, "detect") == 0)
8599 {
8600 if (*arg == 'o' || !filetype_detect)
8601 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01008602 source_runtime((char_u *)FILETYPE_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008603 filetype_detect = TRUE;
8604 if (plugin)
8605 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01008606 source_runtime((char_u *)FTPLUGIN_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008607 filetype_plugin = TRUE;
8608 }
8609 if (indent)
8610 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01008611 source_runtime((char_u *)INDENT_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008612 filetype_indent = TRUE;
8613 }
8614 }
8615 if (*arg == 'd')
8616 {
Bram Moolenaar1610d052016-06-09 22:53:01 +02008617 (void)do_doautocmd((char_u *)"filetypedetect BufRead", TRUE, NULL);
Bram Moolenaara3227e22006-03-08 21:32:40 +00008618 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008619 }
8620 }
8621 else if (STRCMP(arg, "off") == 0)
8622 {
8623 if (plugin || indent)
8624 {
8625 if (plugin)
8626 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01008627 source_runtime((char_u *)FTPLUGOF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008628 filetype_plugin = FALSE;
8629 }
8630 if (indent)
8631 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01008632 source_runtime((char_u *)INDOFF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008633 filetype_indent = FALSE;
8634 }
8635 }
8636 else
8637 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01008638 source_runtime((char_u *)FTOFF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008639 filetype_detect = FALSE;
8640 }
8641 }
8642 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008643 semsg(_(e_invarg2), arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008644}
8645
8646/*
Bram Moolenaar3e545692017-06-04 19:00:32 +02008647 * ":setfiletype [FALLBACK] {name}"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008648 */
8649 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008650ex_setfiletype(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008651{
8652 if (!did_filetype)
Bram Moolenaar3e545692017-06-04 19:00:32 +02008653 {
8654 char_u *arg = eap->arg;
8655
8656 if (STRNCMP(arg, "FALLBACK ", 9) == 0)
8657 arg += 9;
8658
8659 set_option_value((char_u *)"filetype", 0L, arg, OPT_LOCAL);
8660 if (arg != eap->arg)
8661 did_filetype = FALSE;
8662 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008663}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008664
Bram Moolenaar071d4272004-06-13 20:20:40 +00008665 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008666ex_digraphs(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008667{
8668#ifdef FEAT_DIGRAPHS
8669 if (*eap->arg != NUL)
8670 putdigraph(eap->arg);
8671 else
Bram Moolenaareae8ae12018-12-14 18:53:02 +01008672 listdigraphs(eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008673#else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008674 emsg(_("E196: No digraphs in this version"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008675#endif
8676}
8677
Bram Moolenaar451fc7b2018-04-27 22:53:07 +02008678#if defined(FEAT_SEARCH_EXTRA) || defined(PROTO)
8679 void
8680set_no_hlsearch(int flag)
8681{
8682 no_hlsearch = flag;
8683# ifdef FEAT_EVAL
8684 set_vim_var_nr(VV_HLSEARCH, !no_hlsearch && p_hls);
8685# endif
8686}
8687
Bram Moolenaar071d4272004-06-13 20:20:40 +00008688/*
8689 * ":nohlsearch"
8690 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008691 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008692ex_nohlsearch(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008693{
Bram Moolenaar451fc7b2018-04-27 22:53:07 +02008694 set_no_hlsearch(TRUE);
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00008695 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008696}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008697#endif
8698
8699#ifdef FEAT_CRYPT
8700/*
8701 * ":X": Get crypt key
8702 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008703 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008704ex_X(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008705{
Bram Moolenaar3a0c9082014-11-12 15:15:42 +01008706 crypt_check_current_method();
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02008707 (void)crypt_get_key(TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008708}
8709#endif
8710
8711#ifdef FEAT_FOLDING
8712 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008713ex_fold(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008714{
8715 if (foldManualAllowed(TRUE))
8716 foldCreate(eap->line1, eap->line2);
8717}
8718
8719 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008720ex_foldopen(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008721{
8722 opFoldRange(eap->line1, eap->line2, eap->cmdidx == CMD_foldopen,
8723 eap->forceit, FALSE);
8724}
8725
8726 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008727ex_folddo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008728{
8729 linenr_T lnum;
8730
Bram Moolenaar4facea32019-10-12 20:17:40 +02008731# ifdef FEAT_CLIPBOARD
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02008732 start_global_changes();
Bram Moolenaar4facea32019-10-12 20:17:40 +02008733# endif
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02008734
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008735 // First set the marks for all lines closed/open.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008736 for (lnum = eap->line1; lnum <= eap->line2; ++lnum)
8737 if (hasFolding(lnum, NULL, NULL) == (eap->cmdidx == CMD_folddoclosed))
8738 ml_setmarked(lnum);
8739
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008740 // Execute the command on the marked lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008741 global_exe(eap->arg);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008742 ml_clearmarked(); // clear rest of the marks
Bram Moolenaar4facea32019-10-12 20:17:40 +02008743# ifdef FEAT_CLIPBOARD
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02008744 end_global_changes();
Bram Moolenaar4facea32019-10-12 20:17:40 +02008745# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008746}
8747#endif
Bram Moolenaarf5291f32017-09-14 22:55:37 +02008748
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008749#if defined(FEAT_QUICKFIX) || defined(PROTO)
Bram Moolenaar39665952018-08-15 20:59:48 +02008750/*
8751 * Returns TRUE if the supplied Ex cmdidx is for a location list command
8752 * instead of a quickfix command.
8753 */
8754 int
8755is_loclist_cmd(int cmdidx)
8756{
Bram Moolenaar74c8be22018-08-23 22:51:40 +02008757 if (cmdidx < 0 || cmdidx >= CMD_SIZE)
Bram Moolenaar39665952018-08-15 20:59:48 +02008758 return FALSE;
8759 return cmdnames[cmdidx].cmd_name[0] == 'l';
8760}
8761#endif
8762
Bram Moolenaar4facea32019-10-12 20:17:40 +02008763#if defined(FEAT_TIMERS) || defined(PROTO)
Bram Moolenaarf5291f32017-09-14 22:55:37 +02008764 int
8765get_pressedreturn(void)
8766{
8767 return ex_pressedreturn;
8768}
8769
8770 void
8771set_pressedreturn(int val)
8772{
8773 ex_pressedreturn = val;
8774}
8775#endif