blob: 2469df341c687ad7048a73c14b0646253fdb3010 [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
Bram Moolenaarcf844172020-06-26 19:44:06 +02005181 // Never close the autocommand window.
5182 if (win == aucmd_win)
5183 {
5184 emsg(_(e_autocmd_close));
5185 return;
5186 }
5187
Bram Moolenaar071d4272004-06-13 20:20:40 +00005188 need_hide = (bufIsChanged(buf) && buf->b_nwindows <= 1);
Bram Moolenaareb44a682017-08-03 22:44:55 +02005189 if (need_hide && !buf_hide(buf) && !forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005190 {
Bram Moolenaar4033c552017-09-16 20:54:51 +02005191#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005192 if ((p_confirm || cmdmod.confirm) && p_write)
5193 {
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005194 bufref_T bufref;
5195
5196 set_bufref(&bufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005197 dialog_changed(buf, FALSE);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005198 if (bufref_valid(&bufref) && bufIsChanged(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005199 return;
5200 need_hide = FALSE;
5201 }
5202 else
Bram Moolenaar4033c552017-09-16 20:54:51 +02005203#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005204 {
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02005205 no_write_message();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005206 return;
5207 }
5208 }
5209
Bram Moolenaar4033c552017-09-16 20:54:51 +02005210#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00005211 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005212#endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00005213
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005214 // free buffer when not hiding it or when it's a scratch buffer
Bram Moolenaarf740b292006-02-16 22:11:02 +00005215 if (tp == NULL)
Bram Moolenaareb44a682017-08-03 22:44:55 +02005216 win_close(win, !need_hide && !buf_hide(buf));
Bram Moolenaarf740b292006-02-16 22:11:02 +00005217 else
Bram Moolenaareb44a682017-08-03 22:44:55 +02005218 win_close_othertab(win, !need_hide && !buf_hide(buf), tp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005219}
5220
Bram Moolenaar071d4272004-06-13 20:20:40 +00005221/*
Bram Moolenaardea25702017-01-29 15:18:10 +01005222 * Handle the argument for a tabpage related ex command.
5223 * Returns a tabpage number.
5224 * When an error is encountered then eap->errmsg is set.
5225 */
5226 static int
5227get_tabpage_arg(exarg_T *eap)
5228{
5229 int tab_number;
5230 int unaccept_arg0 = (eap->cmdidx == CMD_tabmove) ? 0 : 1;
5231
5232 if (eap->arg && *eap->arg != NUL)
5233 {
5234 char_u *p = eap->arg;
5235 char_u *p_save;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005236 int relative = 0; // argument +N/-N means: go to N places to the
5237 // right/left relative to the current position.
Bram Moolenaardea25702017-01-29 15:18:10 +01005238
5239 if (*p == '-')
5240 {
5241 relative = -1;
5242 p++;
5243 }
5244 else if (*p == '+')
5245 {
5246 relative = 1;
5247 p++;
5248 }
5249
5250 p_save = p;
5251 tab_number = getdigits(&p);
5252
5253 if (relative == 0)
5254 {
5255 if (STRCMP(p, "$") == 0)
5256 tab_number = LAST_TAB_NR;
5257 else if (p == p_save || *p_save == '-' || *p != NUL
5258 || tab_number > LAST_TAB_NR)
5259 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005260 // No numbers as argument.
Bram Moolenaardea25702017-01-29 15:18:10 +01005261 eap->errmsg = e_invarg;
5262 goto theend;
5263 }
5264 }
5265 else
5266 {
5267 if (*p_save == NUL)
5268 tab_number = 1;
5269 else if (p == p_save || *p_save == '-' || *p != NUL
5270 || tab_number == 0)
5271 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005272 // No numbers as argument.
Bram Moolenaardea25702017-01-29 15:18:10 +01005273 eap->errmsg = e_invarg;
5274 goto theend;
5275 }
5276 tab_number = tab_number * relative + tabpage_index(curtab);
5277 if (!unaccept_arg0 && relative == -1)
5278 --tab_number;
5279 }
5280 if (tab_number < unaccept_arg0 || tab_number > LAST_TAB_NR)
5281 eap->errmsg = e_invarg;
5282 }
5283 else if (eap->addr_count > 0)
5284 {
5285 if (unaccept_arg0 && eap->line2 == 0)
Bram Moolenaarc6251552017-01-29 21:42:20 +01005286 {
Bram Moolenaardea25702017-01-29 15:18:10 +01005287 eap->errmsg = e_invrange;
Bram Moolenaarc6251552017-01-29 21:42:20 +01005288 tab_number = 0;
5289 }
Bram Moolenaardea25702017-01-29 15:18:10 +01005290 else
5291 {
5292 tab_number = eap->line2;
Bram Moolenaar82a12462019-01-22 22:41:42 +01005293 if (!unaccept_arg0 && *skipwhite(*eap->cmdlinep) == '-')
Bram Moolenaardea25702017-01-29 15:18:10 +01005294 {
5295 --tab_number;
5296 if (tab_number < unaccept_arg0)
5297 eap->errmsg = e_invarg;
5298 }
5299 }
5300 }
5301 else
5302 {
5303 switch (eap->cmdidx)
5304 {
5305 case CMD_tabnext:
5306 tab_number = tabpage_index(curtab) + 1;
5307 if (tab_number > LAST_TAB_NR)
5308 tab_number = 1;
5309 break;
5310 case CMD_tabmove:
5311 tab_number = LAST_TAB_NR;
5312 break;
5313 default:
5314 tab_number = tabpage_index(curtab);
5315 }
5316 }
5317
5318theend:
5319 return tab_number;
5320}
5321
5322/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00005323 * ":tabclose": close current tab page, unless it is the last one.
5324 * ":tabclose N": close tab page N.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005325 */
5326 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005327ex_tabclose(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005328{
Bram Moolenaarf740b292006-02-16 22:11:02 +00005329 tabpage_T *tp;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005330 int tab_number;
Bram Moolenaarf740b292006-02-16 22:11:02 +00005331
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005332# ifdef FEAT_CMDWIN
5333 if (cmdwin_type != 0)
5334 cmdwin_result = K_IGNORE;
5335 else
5336# endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00005337 if (first_tabpage->tp_next == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005338 emsg(_("E784: Cannot close last tab page"));
Bram Moolenaarf740b292006-02-16 22:11:02 +00005339 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005340 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005341 tab_number = get_tabpage_arg(eap);
5342 if (eap->errmsg == NULL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005343 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005344 tp = find_tabpage(tab_number);
Bram Moolenaarf740b292006-02-16 22:11:02 +00005345 if (tp == NULL)
5346 {
5347 beep_flush();
5348 return;
5349 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005350 if (tp != curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00005351 {
5352 tabpage_close_other(tp, eap->forceit);
5353 return;
5354 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005355 else if (!text_locked() && !curbuf_locked())
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005356 tabpage_close(eap->forceit);
5357 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005358 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005359}
5360
5361/*
5362 * ":tabonly": close all tab pages except the current one
5363 */
5364 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005365ex_tabonly(exarg_T *eap)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005366{
5367 tabpage_T *tp;
5368 int done;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005369 int tab_number;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005370
5371# ifdef FEAT_CMDWIN
5372 if (cmdwin_type != 0)
5373 cmdwin_result = K_IGNORE;
5374 else
5375# endif
5376 if (first_tabpage->tp_next == NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01005377 msg(_("Already only one tab page"));
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005378 else
5379 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005380 tab_number = get_tabpage_arg(eap);
5381 if (eap->errmsg == NULL)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005382 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005383 goto_tabpage(tab_number);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005384 // Repeat this up to a 1000 times, because autocommands may
5385 // mess up the lists.
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005386 for (done = 0; done < 1000; ++done)
5387 {
5388 FOR_ALL_TABPAGES(tp)
5389 if (tp->tp_topframe != topframe)
5390 {
5391 tabpage_close_other(tp, eap->forceit);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005392 // if we failed to close it quit
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005393 if (valid_tabpage(tp))
5394 done = 1000;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005395 // start over, "tp" is now invalid
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005396 break;
5397 }
5398 if (first_tabpage->tp_next == NULL)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005399 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005400 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005401 }
5402 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005403}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005404
5405/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00005406 * Close the current tab page.
5407 */
5408 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005409tabpage_close(int forceit)
Bram Moolenaarf740b292006-02-16 22:11:02 +00005410{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005411 // First close all the windows but the current one. If that worked then
5412 // close the last window in this tab, that will close it.
Bram Moolenaar459ca562016-11-10 18:16:33 +01005413 if (!ONE_WINDOW)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005414 close_others(TRUE, forceit);
Bram Moolenaar459ca562016-11-10 18:16:33 +01005415 if (ONE_WINDOW)
Bram Moolenaarf740b292006-02-16 22:11:02 +00005416 ex_win_close(forceit, curwin, NULL);
5417# ifdef FEAT_GUI
5418 need_mouse_correct = TRUE;
5419# endif
5420}
5421
5422/*
5423 * Close tab page "tp", which is not the current tab page.
5424 * Note that autocommands may make "tp" invalid.
Bram Moolenaar7875acc2006-09-10 13:51:17 +00005425 * Also takes care of the tab pages line disappearing when closing the
5426 * last-but-one tab page.
Bram Moolenaarf740b292006-02-16 22:11:02 +00005427 */
5428 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005429tabpage_close_other(tabpage_T *tp, int forceit)
Bram Moolenaarf740b292006-02-16 22:11:02 +00005430{
5431 int done = 0;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005432 win_T *wp;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00005433 int h = tabline_height();
Bram Moolenaarf740b292006-02-16 22:11:02 +00005434
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005435 // Limit to 1000 windows, autocommands may add a window while we close
5436 // one. OK, so I'm paranoid...
Bram Moolenaarf740b292006-02-16 22:11:02 +00005437 while (++done < 1000)
5438 {
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005439 wp = tp->tp_firstwin;
5440 ex_win_close(forceit, wp, tp);
Bram Moolenaarf740b292006-02-16 22:11:02 +00005441
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005442 // Autocommands may delete the tab page under our fingers and we may
5443 // fail to close a window with a modified buffer.
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005444 if (!valid_tabpage(tp) || tp->tp_firstwin == wp)
Bram Moolenaarf740b292006-02-16 22:11:02 +00005445 break;
5446 }
Bram Moolenaar7875acc2006-09-10 13:51:17 +00005447
Bram Moolenaar29323592016-07-24 22:04:11 +02005448 apply_autocmds(EVENT_TABCLOSED, NULL, NULL, FALSE, curbuf);
Bram Moolenaar29323592016-07-24 22:04:11 +02005449
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005450 redraw_tabline = TRUE;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00005451 if (h != tabline_height())
5452 shell_new_rows();
Bram Moolenaarf740b292006-02-16 22:11:02 +00005453}
5454
5455/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005456 * ":only".
5457 */
5458 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005459ex_only(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005460{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005461 win_T *wp;
5462 int wnr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005463# ifdef FEAT_GUI
5464 need_mouse_correct = TRUE;
5465# endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005466 if (eap->addr_count > 0)
5467 {
5468 wnr = eap->line2;
5469 for (wp = firstwin; --wnr > 0; )
5470 {
5471 if (wp->w_next == NULL)
5472 break;
5473 else
5474 wp = wp->w_next;
5475 }
5476 win_goto(wp);
5477 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005478 close_others(TRUE, eap->forceit);
5479}
5480
Bram Moolenaar071d4272004-06-13 20:20:40 +00005481 static void
Bram Moolenaar5e1e6d22017-01-02 17:26:00 +01005482ex_hide(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005483{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005484 // ":hide" or ":hide | cmd": hide current window
Bram Moolenaar2256c992016-11-15 21:17:07 +01005485 if (!eap->skip)
5486 {
Bram Moolenaar4033c552017-09-16 20:54:51 +02005487#ifdef FEAT_GUI
Bram Moolenaar2256c992016-11-15 21:17:07 +01005488 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005489#endif
Bram Moolenaar2256c992016-11-15 21:17:07 +01005490 if (eap->addr_count == 0)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005491 win_close(curwin, FALSE); // don't free buffer
Bram Moolenaar2256c992016-11-15 21:17:07 +01005492 else
5493 {
5494 int winnr = 0;
5495 win_T *win;
Bram Moolenaarf240e182014-11-27 18:33:02 +01005496
Bram Moolenaar2256c992016-11-15 21:17:07 +01005497 FOR_ALL_WINDOWS(win)
5498 {
5499 winnr++;
5500 if (winnr == eap->line2)
5501 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005502 }
Bram Moolenaar2256c992016-11-15 21:17:07 +01005503 if (win == NULL)
5504 win = lastwin;
5505 win_close(win, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005506 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005507 }
5508}
5509
5510/*
5511 * ":stop" and ":suspend": Suspend Vim.
5512 */
5513 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005514ex_stop(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005515{
5516 /*
5517 * Disallow suspending for "rvim".
5518 */
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005519 if (!check_restricted())
Bram Moolenaar071d4272004-06-13 20:20:40 +00005520 {
5521 if (!eap->forceit)
5522 autowrite_all();
5523 windgoto((int)Rows - 1, 0);
5524 out_char('\n');
5525 out_flush();
5526 stoptermcap();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005527 out_flush(); // needed for SUN to restore xterm buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00005528#ifdef FEAT_TITLE
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005529 mch_restore_title(SAVE_RESTORE_BOTH); // restore window titles
Bram Moolenaar071d4272004-06-13 20:20:40 +00005530#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005531 ui_suspend(); // call machine specific function
Bram Moolenaar071d4272004-06-13 20:20:40 +00005532#ifdef FEAT_TITLE
5533 maketitle();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005534 resettitle(); // force updating the title
Bram Moolenaar071d4272004-06-13 20:20:40 +00005535#endif
5536 starttermcap();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005537 scroll_start(); // scroll screen before redrawing
Bram Moolenaar071d4272004-06-13 20:20:40 +00005538 redraw_later_clear();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005539 shell_resized(); // may have resized window
Bram Moolenaar071d4272004-06-13 20:20:40 +00005540 }
5541}
5542
5543/*
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005544 * ":exit", ":xit" and ":wq": Write file and quite the current window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005545 */
5546 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005547ex_exit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005548{
5549#ifdef FEAT_CMDWIN
5550 if (cmdwin_type != 0)
5551 {
5552 cmdwin_result = Ctrl_C;
5553 return;
5554 }
5555#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005556 // Don't quit while editing the command line.
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005557 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005558 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005559 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005560 return;
5561 }
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005562
5563 if (before_quit_autocmds(curwin, FALSE, eap->forceit))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005564 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005565
5566 /*
5567 * if more files or windows we won't exit
5568 */
5569 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
5570 exiting = TRUE;
5571 if ( ((eap->cmdidx == CMD_wq
5572 || curbufIsChanged())
5573 && do_write(eap) == FAIL)
5574 || check_more(TRUE, eap->forceit) == FAIL
Bram Moolenaar027387f2016-01-02 22:25:52 +01005575 || (only_one_window() && check_changed_any(eap->forceit, FALSE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005576 {
5577 not_exiting();
5578 }
5579 else
5580 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005581 if (only_one_window()) // quit last window, exit Vim
Bram Moolenaar071d4272004-06-13 20:20:40 +00005582 getout(0);
Bram Moolenaar2c33d7b2017-10-14 16:06:20 +02005583 not_exiting();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005584# ifdef FEAT_GUI
5585 need_mouse_correct = TRUE;
5586# endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005587 // Quit current window, may free the buffer.
Bram Moolenaareb44a682017-08-03 22:44:55 +02005588 win_close(curwin, !buf_hide(curwin->w_buffer));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005589 }
5590}
5591
5592/*
5593 * ":print", ":list", ":number".
5594 */
5595 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005596ex_print(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005597{
Bram Moolenaardf177f62005-02-22 08:39:57 +00005598 if (curbuf->b_ml.ml_flags & ML_EMPTY)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005599 emsg(_(e_emptybuf));
Bram Moolenaardf177f62005-02-22 08:39:57 +00005600 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005601 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00005602 for ( ;!got_int; ui_breakcheck())
5603 {
5604 print_line(eap->line1,
5605 (eap->cmdidx == CMD_number || eap->cmdidx == CMD_pound
5606 || (eap->flags & EXFLAG_NR)),
5607 eap->cmdidx == CMD_list || (eap->flags & EXFLAG_LIST));
5608 if (++eap->line1 > eap->line2)
5609 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005610 out_flush(); // show one line at a time
Bram Moolenaardf177f62005-02-22 08:39:57 +00005611 }
5612 setpcmark();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005613 // put cursor at last line
Bram Moolenaardf177f62005-02-22 08:39:57 +00005614 curwin->w_cursor.lnum = eap->line2;
5615 beginline(BL_SOL | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005616 }
5617
Bram Moolenaar071d4272004-06-13 20:20:40 +00005618 ex_no_reprint = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005619}
5620
5621#ifdef FEAT_BYTEOFF
5622 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005623ex_goto(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005624{
5625 goto_byte(eap->line2);
5626}
5627#endif
5628
5629/*
5630 * ":shell".
5631 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005632 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005633ex_shell(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005634{
5635 do_shell(NULL, 0);
5636}
5637
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005638#if defined(HAVE_DROP_FILE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005639
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005640static int drop_busy = FALSE;
5641static int drop_filec;
5642static char_u **drop_filev = NULL;
5643static int drop_split;
5644static void (*drop_callback)(void *);
5645static void *drop_cookie;
5646
5647 static void
5648handle_drop_internal(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005649{
5650 exarg_T ea;
5651 int save_msg_scroll = msg_scroll;
5652
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005653 // Setting the argument list may cause screen updates and being called
5654 // recursively. Avoid that by setting drop_busy.
5655 drop_busy = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005656
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005657 // Check whether the current buffer is changed. If so, we will need
5658 // to split the current window or data could be lost.
5659 // We don't need to check if the 'hidden' option is set, as in this
5660 // case the buffer won't be lost.
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005661 if (!buf_hide(curbuf) && !drop_split)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005662 {
5663 ++emsg_off;
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005664 drop_split = check_changed(curbuf, CCGD_AW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005665 --emsg_off;
5666 }
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005667 if (drop_split)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005668 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005669 if (win_split(0, 0) == FAIL)
5670 return;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02005671 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005672
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005673 // When splitting the window, create a new alist. Otherwise the
5674 // existing one is overwritten.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005675 alist_unlink(curwin->w_alist);
5676 alist_new();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005677 }
5678
5679 /*
5680 * Set up the new argument list.
5681 */
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005682 alist_set(ALIST(curwin), drop_filec, drop_filev, FALSE, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005683
5684 /*
5685 * Move to the first file.
5686 */
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005687 // Fake up a minimal "next" command for do_argfile()
Bram Moolenaara80faa82020-04-12 19:37:17 +02005688 CLEAR_FIELD(ea);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005689 ea.cmd = (char_u *)"next";
5690 do_argfile(&ea, 0);
5691
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005692 // do_ecmd() may set need_start_insertmode, but since we never left Insert
5693 // mode that is not needed here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005694 need_start_insertmode = FALSE;
5695
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005696 // Restore msg_scroll, otherwise a following command may cause scrolling
5697 // unexpectedly. The screen will be redrawn by the caller, thus
5698 // msg_scroll being set by displaying a message is irrelevant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005699 msg_scroll = save_msg_scroll;
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005700
5701 if (drop_callback != NULL)
5702 drop_callback(drop_cookie);
5703
5704 drop_filev = NULL;
5705 drop_busy = FALSE;
5706}
5707
5708/*
5709 * Handle a file drop. The code is here because a drop is *nearly* like an
5710 * :args command, but not quite (we have a list of exact filenames, so we
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01005711 * don't want to (a) parse a command line, or (b) expand wildcards). So the
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005712 * code is very similar to :args and hence needs access to a lot of the static
5713 * functions in this file.
5714 *
5715 * The "filev" list must have been allocated using alloc(), as should each item
5716 * in the list. This function takes over responsibility for freeing the "filev"
5717 * list.
5718 */
5719 void
5720handle_drop(
5721 int filec, // the number of files dropped
5722 char_u **filev, // the list of files dropped
5723 int split, // force splitting the window
5724 void (*callback)(void *), // to be called after setting the argument
5725 // list
5726 void *cookie) // argument for "callback" (allocated)
5727{
5728 // Cannot handle recursive drops, finish the pending one.
5729 if (drop_busy)
5730 {
5731 FreeWild(filec, filev);
5732 vim_free(cookie);
5733 return;
5734 }
5735
5736 // When calling handle_drop() more than once in a row we only use the last
5737 // one.
5738 if (drop_filev != NULL)
5739 {
5740 FreeWild(drop_filec, drop_filev);
5741 vim_free(drop_cookie);
5742 }
5743
5744 drop_filec = filec;
5745 drop_filev = filev;
5746 drop_split = split;
5747 drop_callback = callback;
5748 drop_cookie = cookie;
5749
5750 // Postpone this when:
5751 // - editing the command line
5752 // - not possible to change the current buffer
5753 // - updating the screen
5754 // As it may change buffers and window structures that are in use and cause
5755 // freed memory to be used.
5756 if (text_locked() || curbuf_locked() || updating_screen)
5757 return;
5758
5759 handle_drop_internal();
5760}
5761
5762/*
5763 * To be called when text is unlocked, curbuf is unlocked or updating_screen is
5764 * reset: Handle a postponed drop.
5765 */
5766 void
5767handle_any_postponed_drop(void)
5768{
5769 if (!drop_busy && drop_filev != NULL
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +02005770 && !text_locked() && !curbuf_locked() && !updating_screen)
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005771 handle_drop_internal();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005772}
5773#endif
5774
Bram Moolenaar071d4272004-06-13 20:20:40 +00005775/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005776 * ":preserve".
5777 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005778 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005779ex_preserve(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005780{
Bram Moolenaar4399ef42005-02-12 14:29:27 +00005781 curbuf->b_flags |= BF_PRESERVED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005782 ml_preserve(curbuf, TRUE);
5783}
5784
5785/*
5786 * ":recover".
5787 */
5788 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005789ex_recover(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005790{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005791 // Set recoverymode right away to avoid the ATTENTION prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005792 recoverymode = TRUE;
Bram Moolenaar45d3b142013-11-09 03:31:51 +01005793 if (!check_changed(curbuf, (p_awa ? CCGD_AW : 0)
5794 | CCGD_MULTWIN
5795 | (eap->forceit ? CCGD_FORCEIT : 0)
5796 | CCGD_EXCMD)
5797
Bram Moolenaar071d4272004-06-13 20:20:40 +00005798 && (*eap->arg == NUL
5799 || setfname(curbuf, eap->arg, NULL, TRUE) == OK))
Bram Moolenaar99499b12019-05-23 21:35:48 +02005800 ml_recover(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005801 recoverymode = FALSE;
5802}
5803
5804/*
5805 * Command modifier used in a wrong way.
5806 */
5807 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005808ex_wrongmodifier(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005809{
5810 eap->errmsg = e_invcmd;
5811}
5812
Bram Moolenaar071d4272004-06-13 20:20:40 +00005813/*
5814 * :sview [+command] file split window with new file, read-only
5815 * :split [[+command] file] split window with current or new file
5816 * :vsplit [[+command] file] split window vertically with current or new file
5817 * :new [[+command] file] split window with no or new file
5818 * :vnew [[+command] file] split vertically window with no or new file
5819 * :sfind [+command] file split window with file in 'path'
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005820 *
5821 * :tabedit open new Tab page with empty window
5822 * :tabedit [+command] file open new Tab page and edit "file"
5823 * :tabnew [[+command] file] just like :tabedit
5824 * :tabfind [+command] file open new Tab page and find "file"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005825 */
5826 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005827ex_splitview(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005828{
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005829 win_T *old_curwin = curwin;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005830#if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005831 char_u *fname = NULL;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005832#endif
5833#ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00005834 int browse_flag = cmdmod.browse;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005835#endif
Bram Moolenaar39902a02018-06-21 22:10:08 +02005836 int use_tab = eap->cmdidx == CMD_tabedit
5837 || eap->cmdidx == CMD_tabfind
5838 || eap->cmdidx == CMD_tabnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005839
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01005840 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02005841 return;
5842
Bram Moolenaar4033c552017-09-16 20:54:51 +02005843#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00005844 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005845#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005846
Bram Moolenaar4033c552017-09-16 20:54:51 +02005847#ifdef FEAT_QUICKFIX
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005848 // A ":split" in the quickfix window works like ":new". Don't want two
5849 // quickfix windows. But it's OK when doing ":tab split".
Bram Moolenaar05bb9532008-07-04 09:44:11 +00005850 if (bt_quickfix(curbuf) && cmdmod.tab == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005851 {
5852 if (eap->cmdidx == CMD_split)
5853 eap->cmdidx = CMD_new;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005854 if (eap->cmdidx == CMD_vsplit)
5855 eap->cmdidx = CMD_vnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005856 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02005857#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005858
Bram Moolenaar4033c552017-09-16 20:54:51 +02005859#ifdef FEAT_SEARCHPATH
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005860 if (eap->cmdidx == CMD_sfind || eap->cmdidx == CMD_tabfind)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005861 {
5862 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg),
5863 FNAME_MESS, TRUE, curbuf->b_ffname);
5864 if (fname == NULL)
5865 goto theend;
5866 eap->arg = fname;
5867 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005868# ifdef FEAT_BROWSE
Bram Moolenaar4033c552017-09-16 20:54:51 +02005869 else
5870# endif
5871#endif
5872#ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00005873 if (cmdmod.browse
Bram Moolenaar071d4272004-06-13 20:20:40 +00005874 && eap->cmdidx != CMD_vnew
Bram Moolenaar071d4272004-06-13 20:20:40 +00005875 && eap->cmdidx != CMD_new)
5876 {
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00005877 if (
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005878# ifdef FEAT_GUI
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00005879 !gui.in_use &&
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005880# endif
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00005881 au_has_group((char_u *)"FileExplorer"))
5882 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005883 // No browsing supported but we do have the file explorer:
5884 // Edit the directory.
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00005885 if (*eap->arg == NUL || !mch_isdir(eap->arg))
5886 eap->arg = (char_u *)".";
5887 }
5888 else
5889 {
Bram Moolenaar39902a02018-06-21 22:10:08 +02005890 fname = do_browse(0, (char_u *)(use_tab
5891 ? _("Edit File in new tab page")
5892 : _("Edit File in new window")),
Bram Moolenaar071d4272004-06-13 20:20:40 +00005893 eap->arg, NULL, NULL, NULL, curbuf);
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00005894 if (fname == NULL)
5895 goto theend;
5896 eap->arg = fname;
5897 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005898 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005899 cmdmod.browse = FALSE; // Don't browse again in do_ecmd().
Bram Moolenaar4033c552017-09-16 20:54:51 +02005900#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005901
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005902 /*
5903 * Either open new tab page or split the window.
5904 */
Bram Moolenaar39902a02018-06-21 22:10:08 +02005905 if (use_tab)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005906 {
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005907 if (win_new_tabpage(cmdmod.tab != 0 ? cmdmod.tab
5908 : eap->addr_count == 0 ? 0
5909 : (int)eap->line2 + 1) != FAIL)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005910 {
Bram Moolenaard2b66012008-01-09 19:30:36 +00005911 do_exedit(eap, old_curwin);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005912
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005913 // set the alternate buffer for the window we came from
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005914 if (curwin != old_curwin
5915 && win_valid(old_curwin)
5916 && old_curwin->w_buffer != curbuf
5917 && !cmdmod.keepalt)
5918 old_curwin->w_alt_fnum = curbuf->b_fnum;
5919 }
5920 }
5921 else if (win_split(eap->addr_count > 0 ? (int)eap->line2 : 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005922 *eap->cmd == 'v' ? WSP_VERT : 0) != FAIL)
5923 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005924 // Reset 'scrollbind' when editing another file, but keep it when
5925 // doing ":split" without arguments.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005926 if (*eap->arg != NUL
Bram Moolenaar8a3bb562018-03-04 20:14:14 +01005927# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00005928 || cmdmod.browse
Bram Moolenaar8a3bb562018-03-04 20:14:14 +01005929# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005930 )
Bram Moolenaar3368ea22010-09-21 16:56:35 +02005931 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005932 else
5933 do_check_scrollbind(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005934 do_exedit(eap, old_curwin);
5935 }
5936
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005937# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00005938 cmdmod.browse = browse_flag;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005939# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005940
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005941# if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005942theend:
5943 vim_free(fname);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005944# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005945}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005946
5947/*
Bram Moolenaar80a94a52006-02-23 21:26:58 +00005948 * Open a new tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005949 */
5950 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005951tabpage_new(void)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00005952{
5953 exarg_T ea;
5954
Bram Moolenaara80faa82020-04-12 19:37:17 +02005955 CLEAR_FIELD(ea);
Bram Moolenaar80a94a52006-02-23 21:26:58 +00005956 ea.cmdidx = CMD_tabnew;
5957 ea.cmd = (char_u *)"tabn";
5958 ea.arg = (char_u *)"";
5959 ex_splitview(&ea);
5960}
5961
5962/*
5963 * :tabnext command
5964 */
5965 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005966ex_tabnext(exarg_T *eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005967{
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005968 int tab_number;
5969
Bram Moolenaar8cdbd5b2019-06-16 15:50:45 +02005970 if (ERROR_IF_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02005971 return;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005972 switch (eap->cmdidx)
5973 {
5974 case CMD_tabfirst:
5975 case CMD_tabrewind:
5976 goto_tabpage(1);
5977 break;
5978 case CMD_tablast:
5979 goto_tabpage(9999);
5980 break;
5981 case CMD_tabprevious:
5982 case CMD_tabNext:
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005983 if (eap->arg && *eap->arg != NUL)
5984 {
5985 char_u *p = eap->arg;
5986 char_u *p_save = p;
5987
5988 tab_number = getdigits(&p);
5989 if (p == p_save || *p_save == '-' || *p != NUL
5990 || tab_number == 0)
5991 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005992 // No numbers as argument.
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005993 eap->errmsg = e_invarg;
5994 return;
5995 }
5996 }
5997 else
5998 {
5999 if (eap->addr_count == 0)
6000 tab_number = 1;
6001 else
6002 {
6003 tab_number = eap->line2;
6004 if (tab_number < 1)
6005 {
6006 eap->errmsg = e_invrange;
6007 return;
6008 }
6009 }
6010 }
6011 goto_tabpage(-tab_number);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006012 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006013 default: // CMD_tabnext
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006014 tab_number = get_tabpage_arg(eap);
6015 if (eap->errmsg == NULL)
6016 goto_tabpage(tab_number);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006017 break;
6018 }
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006019}
6020
6021/*
6022 * :tabmove command
6023 */
6024 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006025ex_tabmove(exarg_T *eap)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006026{
Bram Moolenaar40ce3a42015-04-21 18:08:39 +02006027 int tab_number;
Bram Moolenaar8cb8dca2012-07-06 18:27:39 +02006028
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006029 tab_number = get_tabpage_arg(eap);
6030 if (eap->errmsg == NULL)
6031 tabpage_move(tab_number);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006032}
6033
6034/*
6035 * :tabs command: List tabs and their contents.
6036 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006037 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006038ex_tabs(exarg_T *eap UNUSED)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006039{
6040 tabpage_T *tp;
6041 win_T *wp;
6042 int tabcount = 1;
6043
6044 msg_start();
6045 msg_scroll = TRUE;
6046 for (tp = first_tabpage; tp != NULL && !got_int; tp = tp->tp_next)
6047 {
6048 msg_putchar('\n');
6049 vim_snprintf((char *)IObuff, IOSIZE, _("Tab page %d"), tabcount++);
Bram Moolenaar8820b482017-03-16 17:23:31 +01006050 msg_outtrans_attr(IObuff, HL_ATTR(HLF_T));
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006051 out_flush(); // output one line at a time
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006052 ui_breakcheck();
6053
Bram Moolenaar030f0df2006-02-21 22:02:53 +00006054 if (tp == curtab)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006055 wp = firstwin;
6056 else
6057 wp = tp->tp_firstwin;
6058 for ( ; wp != NULL && !got_int; wp = wp->w_next)
6059 {
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006060 msg_putchar('\n');
6061 msg_putchar(wp == curwin ? '>' : ' ');
6062 msg_putchar(' ');
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006063 msg_putchar(bufIsChanged(wp->w_buffer) ? '+' : ' ');
6064 msg_putchar(' ');
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006065 if (buf_spname(wp->w_buffer) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02006066 vim_strncpy(IObuff, buf_spname(wp->w_buffer), IOSIZE - 1);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006067 else
6068 home_replace(wp->w_buffer, wp->w_buffer->b_fname,
6069 IObuff, IOSIZE, TRUE);
6070 msg_outtrans(IObuff);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006071 out_flush(); // output one line at a time
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006072 ui_breakcheck();
6073 }
6074 }
6075}
6076
Bram Moolenaar071d4272004-06-13 20:20:40 +00006077/*
6078 * ":mode": Set screen mode.
6079 * If no argument given, just get the screen size and redraw.
6080 */
6081 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006082ex_mode(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006083{
6084 if (*eap->arg == NUL)
6085 shell_resized();
6086 else
Bram Moolenaar3c71aec2020-01-17 19:32:20 +01006087 emsg(_(e_screenmode));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006088}
6089
Bram Moolenaar071d4272004-06-13 20:20:40 +00006090/*
6091 * ":resize".
6092 * set, increment or decrement current window height
6093 */
6094 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006095ex_resize(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006096{
6097 int n;
6098 win_T *wp = curwin;
6099
6100 if (eap->addr_count > 0)
6101 {
6102 n = eap->line2;
6103 for (wp = firstwin; wp->w_next != NULL && --n > 0; wp = wp->w_next)
6104 ;
6105 }
6106
Bram Moolenaar44a2f922016-03-19 22:11:51 +01006107# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00006108 need_mouse_correct = TRUE;
Bram Moolenaar44a2f922016-03-19 22:11:51 +01006109# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006110 n = atol((char *)eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006111 if (cmdmod.split & WSP_VERT)
6112 {
6113 if (*eap->arg == '-' || *eap->arg == '+')
Bram Moolenaar02631462017-09-22 15:20:32 +02006114 n += curwin->w_width;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006115 else if (n == 0 && eap->arg[0] == NUL) // default is very wide
Bram Moolenaar071d4272004-06-13 20:20:40 +00006116 n = 9999;
6117 win_setwidth_win((int)n, wp);
6118 }
6119 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006120 {
6121 if (*eap->arg == '-' || *eap->arg == '+')
6122 n += curwin->w_height;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006123 else if (n == 0 && eap->arg[0] == NUL) // default is very high
Bram Moolenaar071d4272004-06-13 20:20:40 +00006124 n = 9999;
6125 win_setheight_win((int)n, wp);
6126 }
6127}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006128
6129/*
6130 * ":find [+command] <file>" command.
6131 */
6132 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006133ex_find(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006134{
6135#ifdef FEAT_SEARCHPATH
6136 char_u *fname;
6137 int count;
6138
6139 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg), FNAME_MESS,
6140 TRUE, curbuf->b_ffname);
6141 if (eap->addr_count > 0)
6142 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006143 // Repeat finding the file "count" times. This matters when it
6144 // appears several times in the path.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006145 count = eap->line2;
6146 while (fname != NULL && --count > 0)
6147 {
6148 vim_free(fname);
6149 fname = find_file_in_path(NULL, 0, FNAME_MESS,
6150 FALSE, curbuf->b_ffname);
6151 }
6152 }
6153
6154 if (fname != NULL)
6155 {
6156 eap->arg = fname;
6157#endif
6158 do_exedit(eap, NULL);
6159#ifdef FEAT_SEARCHPATH
6160 vim_free(fname);
6161 }
6162#endif
6163}
6164
6165/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00006166 * ":open" simulation: for now just work like ":visual".
6167 */
6168 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006169ex_open(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00006170{
6171 regmatch_T regmatch;
6172 char_u *p;
6173
6174 curwin->w_cursor.lnum = eap->line2;
6175 beginline(BL_SOL | BL_FIX);
6176 if (*eap->arg == '/')
6177 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006178 // ":open /pattern/": put cursor in column found with pattern
Bram Moolenaardf177f62005-02-22 08:39:57 +00006179 ++eap->arg;
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006180 p = skip_regexp(eap->arg, '/', p_magic);
Bram Moolenaardf177f62005-02-22 08:39:57 +00006181 *p = NUL;
6182 regmatch.regprog = vim_regcomp(eap->arg, p_magic ? RE_MAGIC : 0);
6183 if (regmatch.regprog != NULL)
6184 {
6185 regmatch.rm_ic = p_ic;
6186 p = ml_get_curline();
6187 if (vim_regexec(&regmatch, p, (colnr_T)0))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006188 curwin->w_cursor.col = (colnr_T)(regmatch.startp[0] - p);
Bram Moolenaardf177f62005-02-22 08:39:57 +00006189 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006190 emsg(_(e_nomatch));
Bram Moolenaar473de612013-06-08 18:19:48 +02006191 vim_regfree(regmatch.regprog);
Bram Moolenaardf177f62005-02-22 08:39:57 +00006192 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006193 // Move to the NUL, ignore any other arguments.
Bram Moolenaardf177f62005-02-22 08:39:57 +00006194 eap->arg += STRLEN(eap->arg);
6195 }
6196 check_cursor();
6197
6198 eap->cmdidx = CMD_visual;
6199 do_exedit(eap, NULL);
6200}
6201
6202/*
6203 * ":edit", ":badd", ":visual".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006204 */
6205 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006206ex_edit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006207{
6208 do_exedit(eap, NULL);
6209}
6210
6211/*
Bram Moolenaar4b96df52020-01-26 22:00:26 +01006212 * ":edit <file>" command and alike.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006213 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006214 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006215do_exedit(
6216 exarg_T *eap,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006217 win_T *old_curwin) // curwin before doing a split or NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006218{
6219 int n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006220 int need_hide;
Bram Moolenaardf177f62005-02-22 08:39:57 +00006221 int exmode_was = exmode_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006222
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01006223 if ((eap->cmdidx != CMD_pedit && ERROR_IF_POPUP_WINDOW)
6224 || ERROR_IF_TERM_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02006225 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006226 /*
6227 * ":vi" command ends Ex mode.
6228 */
6229 if (exmode_active && (eap->cmdidx == CMD_visual
6230 || eap->cmdidx == CMD_view))
6231 {
6232 exmode_active = FALSE;
Bram Moolenaar158ea172020-06-18 17:28:39 +02006233 ex_pressedreturn = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006234 if (*eap->arg == NUL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00006235 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006236 // Special case: ":global/pat/visual\NLvi-commands"
Bram Moolenaardf177f62005-02-22 08:39:57 +00006237 if (global_busy)
6238 {
6239 int rd = RedrawingDisabled;
6240 int nwr = no_wait_return;
6241 int ms = msg_scroll;
6242#ifdef FEAT_GUI
6243 int he = hold_gui_events;
6244#endif
6245
6246 if (eap->nextcmd != NULL)
6247 {
6248 stuffReadbuff(eap->nextcmd);
6249 eap->nextcmd = NULL;
6250 }
6251
6252 if (exmode_was != EXMODE_VIM)
6253 settmode(TMODE_RAW);
6254 RedrawingDisabled = 0;
6255 no_wait_return = 0;
6256 need_wait_return = FALSE;
6257 msg_scroll = 0;
6258#ifdef FEAT_GUI
6259 hold_gui_events = 0;
6260#endif
6261 must_redraw = CLEAR;
Bram Moolenaar9e2bcb52020-02-18 21:33:00 +01006262 pending_exmode_active = TRUE;
Bram Moolenaardf177f62005-02-22 08:39:57 +00006263
6264 main_loop(FALSE, TRUE);
6265
Bram Moolenaar9e2bcb52020-02-18 21:33:00 +01006266 pending_exmode_active = FALSE;
Bram Moolenaardf177f62005-02-22 08:39:57 +00006267 RedrawingDisabled = rd;
6268 no_wait_return = nwr;
6269 msg_scroll = ms;
6270#ifdef FEAT_GUI
6271 hold_gui_events = he;
6272#endif
6273 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006274 return;
Bram Moolenaardf177f62005-02-22 08:39:57 +00006275 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006276 }
6277
6278 if ((eap->cmdidx == CMD_new
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006279 || eap->cmdidx == CMD_tabnew
6280 || eap->cmdidx == CMD_tabedit
Bram Moolenaar4033c552017-09-16 20:54:51 +02006281 || eap->cmdidx == CMD_vnew) && *eap->arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006282 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006283 // ":new" or ":tabnew" without argument: edit an new empty buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00006284 setpcmark();
6285 (void)do_ecmd(0, NULL, NULL, eap, ECMD_ONE,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00006286 ECMD_HIDE + (eap->forceit ? ECMD_FORCEIT : 0),
6287 old_curwin == NULL ? curwin : NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006288 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02006289 else if ((eap->cmdidx != CMD_split && eap->cmdidx != CMD_vsplit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006290 || *eap->arg != NUL
6291#ifdef FEAT_BROWSE
6292 || cmdmod.browse
6293#endif
6294 )
6295 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006296 // Can't edit another file when "curbuf_lock" is set. Only ":edit"
6297 // can bring us here, others are stopped earlier.
Bram Moolenaar5555acc2006-04-07 21:33:12 +00006298 if (*eap->arg != NUL && curbuf_locked())
6299 return;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006300
Bram Moolenaar071d4272004-06-13 20:20:40 +00006301 n = readonlymode;
6302 if (eap->cmdidx == CMD_view || eap->cmdidx == CMD_sview)
6303 readonlymode = TRUE;
6304 else if (eap->cmdidx == CMD_enew)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006305 readonlymode = FALSE; // 'readonly' doesn't make sense in an
6306 // empty buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00006307 setpcmark();
6308 if (do_ecmd(0, (eap->cmdidx == CMD_enew ? NULL : eap->arg),
6309 NULL, eap,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006310 // ":edit" goes to first line if Vi compatible
Bram Moolenaar071d4272004-06-13 20:20:40 +00006311 (*eap->arg == NUL && eap->do_ecmd_lnum == 0
6312 && vim_strchr(p_cpo, CPO_GOTO1) != NULL)
6313 ? ECMD_ONE : eap->do_ecmd_lnum,
Bram Moolenaareb44a682017-08-03 22:44:55 +02006314 (buf_hide(curbuf) ? ECMD_HIDE : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006315 + (eap->forceit ? ECMD_FORCEIT : 0)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006316 // after a split we can use an existing buffer
Bram Moolenaar7b449342014-03-25 13:03:48 +01006317 + (old_curwin != NULL ? ECMD_OLDBUF : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006318 + (eap->cmdidx == CMD_badd ? ECMD_ADDBUF : 0 )
Bram Moolenaar701f7af2008-11-15 13:12:07 +00006319 , old_curwin == NULL ? curwin : NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006320 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006321 // Editing the file failed. If the window was split, close it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006322 if (old_curwin != NULL)
6323 {
6324 need_hide = (curbufIsChanged() && curbuf->b_nwindows <= 1);
Bram Moolenaareb44a682017-08-03 22:44:55 +02006325 if (!need_hide || buf_hide(curbuf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006326 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006327#if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00006328 cleanup_T cs;
6329
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006330 // Reset the error/interrupt/exception state here so that
6331 // aborting() returns FALSE when closing a window.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00006332 enter_cleanup(&cs);
Bram Moolenaar4033c552017-09-16 20:54:51 +02006333#endif
6334#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00006335 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006336#endif
Bram Moolenaareb44a682017-08-03 22:44:55 +02006337 win_close(curwin, !need_hide && !buf_hide(curbuf));
Bram Moolenaarc0197e22004-09-13 20:26:32 +00006338
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006339#if defined(FEAT_EVAL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006340 // Restore the error/interrupt/exception state if not
6341 // discarded by a new aborting error, interrupt, or
6342 // uncaught exception.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00006343 leave_cleanup(&cs);
Bram Moolenaar4033c552017-09-16 20:54:51 +02006344#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006345 }
6346 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006347 }
6348 else if (readonlymode && curbuf->b_nwindows == 1)
6349 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006350 // When editing an already visited buffer, 'readonly' won't be set
6351 // but the previous value is kept. With ":view" and ":sview" we
6352 // want the file to be readonly, except when another window is
6353 // editing the same buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006354 curbuf->b_p_ro = TRUE;
6355 }
6356 readonlymode = n;
6357 }
6358 else
6359 {
6360 if (eap->do_ecmd_cmd != NULL)
6361 do_cmdline_cmd(eap->do_ecmd_cmd);
6362#ifdef FEAT_TITLE
6363 n = curwin->w_arg_idx_invalid;
6364#endif
6365 check_arg_idx(curwin);
6366#ifdef FEAT_TITLE
6367 if (n != curwin->w_arg_idx_invalid)
6368 maketitle();
6369#endif
6370 }
6371
Bram Moolenaar071d4272004-06-13 20:20:40 +00006372 /*
6373 * if ":split file" worked, set alternate file name in old window to new
6374 * file
6375 */
6376 if (old_curwin != NULL
6377 && *eap->arg != NUL
6378 && curwin != old_curwin
6379 && win_valid(old_curwin)
Bram Moolenaard4755bb2004-09-02 19:12:26 +00006380 && old_curwin->w_buffer != curbuf
6381 && !cmdmod.keepalt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006382 old_curwin->w_alt_fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006383
6384 ex_no_reprint = TRUE;
6385}
6386
6387#ifndef FEAT_GUI
6388/*
6389 * ":gui" and ":gvim" when there is no GUI.
6390 */
6391 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006392ex_nogui(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006393{
6394 eap->errmsg = e_nogvim;
6395}
6396#endif
6397
Bram Moolenaar4f974752019-02-17 17:44:42 +01006398#if defined(FEAT_GUI_MSWIN) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006399 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006400ex_tearoff(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006401{
6402 gui_make_tearoff(eap->arg);
6403}
6404#endif
6405
Bram Moolenaar29a2c082018-03-05 21:06:23 +01006406#if (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK) \
6407 || defined(FEAT_TERM_POPUP_MENU)) && defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006408 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006409ex_popup(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006410{
Bram Moolenaar29a2c082018-03-05 21:06:23 +01006411# if defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK)
6412 if (gui.in_use)
6413 gui_make_popup(eap->arg, eap->forceit);
6414# ifdef FEAT_TERM_POPUP_MENU
6415 else
6416# endif
6417# endif
6418# ifdef FEAT_TERM_POPUP_MENU
6419 pum_make_popup(eap->arg, eap->forceit);
6420# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006421}
6422#endif
6423
Bram Moolenaar071d4272004-06-13 20:20:40 +00006424 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006425ex_swapname(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006426{
6427 if (curbuf->b_ml.ml_mfp == NULL || curbuf->b_ml.ml_mfp->mf_fname == NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01006428 msg(_("No swap file"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006429 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01006430 msg((char *)curbuf->b_ml.ml_mfp->mf_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006431}
6432
6433/*
6434 * ":syncbind" forces all 'scrollbind' windows to have the same relative
6435 * offset.
6436 * (1998-11-02 16:21:01 R. Edward Ralston <eralston@computer.org>)
6437 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006438 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006439ex_syncbind(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006440{
Bram Moolenaar071d4272004-06-13 20:20:40 +00006441 win_T *wp;
Bram Moolenaardedd1b02013-12-14 13:06:17 +01006442 win_T *save_curwin = curwin;
6443 buf_T *save_curbuf = curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006444 long topline;
6445 long y;
6446 linenr_T old_linenr = curwin->w_cursor.lnum;
6447
6448 setpcmark();
6449
6450 /*
6451 * determine max topline
6452 */
6453 if (curwin->w_p_scb)
6454 {
6455 topline = curwin->w_topline;
Bram Moolenaar29323592016-07-24 22:04:11 +02006456 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006457 {
6458 if (wp->w_p_scb && wp->w_buffer)
6459 {
Bram Moolenaar375e3392019-01-31 18:26:10 +01006460 y = wp->w_buffer->b_ml.ml_line_count - get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006461 if (topline > y)
6462 topline = y;
6463 }
6464 }
6465 if (topline < 1)
6466 topline = 1;
6467 }
6468 else
6469 {
6470 topline = 1;
6471 }
6472
6473
6474 /*
Bram Moolenaardedd1b02013-12-14 13:06:17 +01006475 * Set all scrollbind windows to the same topline.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006476 */
Bram Moolenaar29323592016-07-24 22:04:11 +02006477 FOR_ALL_WINDOWS(curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006478 {
6479 if (curwin->w_p_scb)
6480 {
Bram Moolenaardedd1b02013-12-14 13:06:17 +01006481 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006482 y = topline - curwin->w_topline;
6483 if (y > 0)
6484 scrollup(y, TRUE);
6485 else
6486 scrolldown(-y, TRUE);
6487 curwin->w_scbind_pos = topline;
6488 redraw_later(VALID);
6489 cursor_correct();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006490 curwin->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006491 }
6492 }
Bram Moolenaardedd1b02013-12-14 13:06:17 +01006493 curwin = save_curwin;
6494 curbuf = save_curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006495 if (curwin->w_p_scb)
6496 {
6497 did_syncbind = TRUE;
6498 checkpcmark();
6499 if (old_linenr != curwin->w_cursor.lnum)
6500 {
6501 char_u ctrl_o[2];
6502
6503 ctrl_o[0] = Ctrl_O;
6504 ctrl_o[1] = 0;
6505 ins_typebuf(ctrl_o, REMAP_NONE, 0, TRUE, FALSE);
6506 }
6507 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006508}
6509
6510
6511 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006512ex_read(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006513{
Bram Moolenaardf177f62005-02-22 08:39:57 +00006514 int i;
6515 int empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
6516 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006517
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006518 if (eap->usefilter) // :r!cmd
Bram Moolenaar071d4272004-06-13 20:20:40 +00006519 do_bang(1, eap, FALSE, FALSE, TRUE);
6520 else
6521 {
6522 if (u_save(eap->line2, (linenr_T)(eap->line2 + 1)) == FAIL)
6523 return;
6524
6525#ifdef FEAT_BROWSE
6526 if (cmdmod.browse)
6527 {
6528 char_u *browseFile;
6529
Bram Moolenaar7171abe2004-10-11 10:06:20 +00006530 browseFile = do_browse(0, (char_u *)_("Append File"), eap->arg,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006531 NULL, NULL, NULL, curbuf);
6532 if (browseFile != NULL)
6533 {
6534 i = readfile(browseFile, NULL,
6535 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
6536 vim_free(browseFile);
6537 }
6538 else
6539 i = OK;
6540 }
6541 else
6542#endif
6543 if (*eap->arg == NUL)
6544 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006545 if (check_fname() == FAIL) // check for no file name
Bram Moolenaar071d4272004-06-13 20:20:40 +00006546 return;
6547 i = readfile(curbuf->b_ffname, curbuf->b_fname,
6548 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
6549 }
6550 else
6551 {
6552 if (vim_strchr(p_cpo, CPO_ALTREAD) != NULL)
6553 (void)setaltfname(eap->arg, eap->arg, (linenr_T)1);
6554 i = readfile(eap->arg, NULL,
6555 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
6556
6557 }
Bram Moolenaare13b9af2017-01-13 22:01:02 +01006558 if (i != OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006559 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006560#if defined(FEAT_EVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006561 if (!aborting())
6562#endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006563 semsg(_(e_notopen), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006564 }
6565 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00006566 {
6567 if (empty && exmode_active)
6568 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006569 // Delete the empty line that remains. Historically ex does
6570 // this but vi doesn't.
Bram Moolenaardf177f62005-02-22 08:39:57 +00006571 if (eap->line2 == 0)
6572 lnum = curbuf->b_ml.ml_line_count;
6573 else
6574 lnum = 1;
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00006575 if (*ml_get(lnum) == NUL && u_savedel(lnum, 1L) == OK)
Bram Moolenaardf177f62005-02-22 08:39:57 +00006576 {
Bram Moolenaarca70c072020-05-30 20:30:46 +02006577 ml_delete(lnum);
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00006578 if (curwin->w_cursor.lnum > 1
6579 && curwin->w_cursor.lnum >= lnum)
Bram Moolenaardf177f62005-02-22 08:39:57 +00006580 --curwin->w_cursor.lnum;
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00006581 deleted_lines_mark(lnum, 1L);
Bram Moolenaardf177f62005-02-22 08:39:57 +00006582 }
6583 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006584 redraw_curbuf_later(VALID);
Bram Moolenaardf177f62005-02-22 08:39:57 +00006585 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006586 }
6587}
6588
Bram Moolenaarea408852005-06-25 22:49:46 +00006589static char_u *prev_dir = NULL;
6590
6591#if defined(EXITFREE) || defined(PROTO)
6592 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006593free_cd_dir(void)
Bram Moolenaarea408852005-06-25 22:49:46 +00006594{
Bram Moolenaard23a8232018-02-10 18:45:26 +01006595 VIM_CLEAR(prev_dir);
6596 VIM_CLEAR(globaldir);
Bram Moolenaarea408852005-06-25 22:49:46 +00006597}
6598#endif
6599
Bram Moolenaarf4258302013-06-02 18:20:17 +02006600/*
Bram Moolenaar002bc792020-06-05 22:33:42 +02006601 * Get the previous directory for the given chdir scope.
6602 */
6603 static char_u *
6604get_prevdir(cdscope_T scope)
6605{
6606 if (scope == CDSCOPE_WINDOW)
6607 return curwin->w_prevdir;
6608 else if (scope == CDSCOPE_TABPAGE)
6609 return curtab->tp_prevdir;
6610 return prev_dir;
6611}
6612
6613/*
Bram Moolenaarf4258302013-06-02 18:20:17 +02006614 * Deal with the side effects of changing the current directory.
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006615 * When 'scope' is CDSCOPE_TABPAGE then this was after an ":tcd" command.
6616 * When 'scope' is CDSCOPE_WINDOW then this was after an ":lcd" command.
Bram Moolenaarf4258302013-06-02 18:20:17 +02006617 */
6618 void
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006619post_chdir(cdscope_T scope)
Bram Moolenaarf4258302013-06-02 18:20:17 +02006620{
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006621 if (scope != CDSCOPE_WINDOW)
Bram Moolenaar00aa0692019-04-27 20:37:57 +02006622 // Clear tab local directory for both :cd and :tcd
6623 VIM_CLEAR(curtab->tp_localdir);
Bram Moolenaard23a8232018-02-10 18:45:26 +01006624 VIM_CLEAR(curwin->w_localdir);
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006625 if (scope != CDSCOPE_GLOBAL)
Bram Moolenaarf4258302013-06-02 18:20:17 +02006626 {
Bram Moolenaar002bc792020-06-05 22:33:42 +02006627 char_u *pdir = get_prevdir(scope);
6628
6629 // If still in the global directory, need to remember current
6630 // directory as the global directory.
6631 if (globaldir == NULL && pdir != NULL)
6632 globaldir = vim_strsave(pdir);
6633
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006634 // Remember this local directory for the window.
Bram Moolenaarf4258302013-06-02 18:20:17 +02006635 if (mch_dirname(NameBuff, MAXPATHL) == OK)
Bram Moolenaar00aa0692019-04-27 20:37:57 +02006636 {
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006637 if (scope == CDSCOPE_TABPAGE)
Bram Moolenaar00aa0692019-04-27 20:37:57 +02006638 curtab->tp_localdir = vim_strsave(NameBuff);
6639 else
6640 curwin->w_localdir = vim_strsave(NameBuff);
6641 }
Bram Moolenaarf4258302013-06-02 18:20:17 +02006642 }
6643 else
6644 {
Bram Moolenaar002bc792020-06-05 22:33:42 +02006645 // We are now in the global directory, no need to remember its name.
Bram Moolenaard23a8232018-02-10 18:45:26 +01006646 VIM_CLEAR(globaldir);
Bram Moolenaarf4258302013-06-02 18:20:17 +02006647 }
6648
6649 shorten_fnames(TRUE);
6650}
6651
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006652/*
6653 * Change directory function used by :cd/:tcd/:lcd Ex commands and the
Bram Moolenaar95058722020-06-01 16:26:19 +02006654 * chdir() function.
6655 * scope == CDSCOPE_WINDOW: changes the window-local directory
6656 * scope == CDSCOPE_TABPAGE: changes the tab-local directory
6657 * Otherwise: changes the global directory
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006658 * Returns TRUE if the directory is successfully changed.
6659 */
6660 int
6661changedir_func(
6662 char_u *new_dir,
6663 int forceit,
6664 cdscope_T scope)
6665{
6666 char_u *tofree;
Bram Moolenaar002bc792020-06-05 22:33:42 +02006667 char_u *pdir = NULL;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006668 int dir_differs;
6669 int retval = FALSE;
6670
Bram Moolenaar7cc96922020-01-31 22:41:38 +01006671 if (new_dir == NULL || allbuf_locked())
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006672 return FALSE;
6673
6674 if (vim_strchr(p_cpo, CPO_CHDIR) != NULL && curbufIsChanged() && !forceit)
6675 {
6676 emsg(_("E747: Cannot change directory, buffer is modified (add ! to override)"));
6677 return FALSE;
6678 }
6679
6680 // ":cd -": Change to previous directory
6681 if (STRCMP(new_dir, "-") == 0)
6682 {
Bram Moolenaar002bc792020-06-05 22:33:42 +02006683 pdir = get_prevdir(scope);
6684 if (pdir == NULL)
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006685 {
6686 emsg(_("E186: No previous directory"));
6687 return FALSE;
6688 }
Bram Moolenaar002bc792020-06-05 22:33:42 +02006689 new_dir = pdir;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006690 }
6691
Bram Moolenaar002bc792020-06-05 22:33:42 +02006692 // Free the previous directory
6693 tofree = get_prevdir(scope);
6694
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006695 // Save current directory for next ":cd -"
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006696 if (mch_dirname(NameBuff, MAXPATHL) == OK)
Bram Moolenaar002bc792020-06-05 22:33:42 +02006697 pdir = vim_strsave(NameBuff);
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006698 else
Bram Moolenaar002bc792020-06-05 22:33:42 +02006699 pdir = NULL;
6700 if (scope == CDSCOPE_WINDOW)
6701 curwin->w_prevdir = pdir;
6702 else if (scope == CDSCOPE_TABPAGE)
6703 curtab->tp_prevdir = pdir;
6704 else
6705 prev_dir = pdir;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006706
6707#if defined(UNIX) || defined(VMS)
6708 // for UNIX ":cd" means: go to home directory
6709 if (*new_dir == NUL)
6710 {
6711 // use NameBuff for home directory name
6712# ifdef VMS
6713 char_u *p;
6714
6715 p = mch_getenv((char_u *)"SYS$LOGIN");
6716 if (p == NULL || *p == NUL) // empty is the same as not set
6717 NameBuff[0] = NUL;
6718 else
6719 vim_strncpy(NameBuff, p, MAXPATHL - 1);
6720# else
6721 expand_env((char_u *)"$HOME", NameBuff, MAXPATHL);
6722# endif
6723 new_dir = NameBuff;
6724 }
6725#endif
Bram Moolenaar002bc792020-06-05 22:33:42 +02006726 dir_differs = new_dir == NULL || pdir == NULL
6727 || pathcmp((char *)pdir, (char *)new_dir, -1) != 0;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006728 if (new_dir == NULL || (dir_differs && vim_chdir(new_dir)))
6729 emsg(_(e_failed));
6730 else
6731 {
6732 char_u *acmd_fname;
6733
6734 post_chdir(scope);
6735
6736 if (dir_differs)
6737 {
6738 if (scope == CDSCOPE_WINDOW)
6739 acmd_fname = (char_u *)"window";
6740 else if (scope == CDSCOPE_TABPAGE)
6741 acmd_fname = (char_u *)"tabpage";
6742 else
6743 acmd_fname = (char_u *)"global";
6744 apply_autocmds(EVENT_DIRCHANGED, acmd_fname, new_dir, FALSE,
6745 curbuf);
6746 }
6747 retval = TRUE;
6748 }
6749 vim_free(tofree);
6750
6751 return retval;
6752}
Bram Moolenaarea408852005-06-25 22:49:46 +00006753
Bram Moolenaar071d4272004-06-13 20:20:40 +00006754/*
Bram Moolenaar00aa0692019-04-27 20:37:57 +02006755 * ":cd", ":tcd", ":lcd", ":chdir" ":tchdir" and ":lchdir".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006756 */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00006757 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006758ex_cd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006759{
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01006760 char_u *new_dir;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006761
6762 new_dir = eap->arg;
6763#if !defined(UNIX) && !defined(VMS)
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006764 // for non-UNIX ":cd" means: print current directory
Bram Moolenaar071d4272004-06-13 20:20:40 +00006765 if (*new_dir == NUL)
6766 ex_pwd(NULL);
6767 else
6768#endif
6769 {
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006770 cdscope_T scope = CDSCOPE_GLOBAL;
6771
6772 if (eap->cmdidx == CMD_lcd || eap->cmdidx == CMD_lchdir)
6773 scope = CDSCOPE_WINDOW;
6774 else if (eap->cmdidx == CMD_tcd || eap->cmdidx == CMD_tchdir)
6775 scope = CDSCOPE_TABPAGE;
6776
6777 if (changedir_func(new_dir, eap->forceit, scope))
Bram Moolenaardf177f62005-02-22 08:39:57 +00006778 {
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006779 // Echo the new current directory if the command was typed.
Bram Moolenaarfcfbc672009-07-09 18:13:49 +00006780 if (KeyTyped || p_verbose >= 5)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006781 ex_pwd(eap);
6782 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006783 }
6784}
6785
6786/*
6787 * ":pwd".
6788 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006789 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006790ex_pwd(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006791{
6792 if (mch_dirname(NameBuff, MAXPATHL) == OK)
6793 {
6794#ifdef BACKSLASH_IN_FILENAME
6795 slash_adjust(NameBuff);
6796#endif
Bram Moolenaar95058722020-06-01 16:26:19 +02006797 if (p_verbose > 0)
6798 {
6799 char *context = "global";
6800
6801 if (curwin->w_localdir != NULL)
6802 context = "window";
6803 else if (curtab->tp_localdir != NULL)
6804 context = "tabpage";
6805 smsg("[%s] %s", context, (char *)NameBuff);
6806 }
6807 else
6808 msg((char *)NameBuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006809 }
6810 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006811 emsg(_("E187: Unknown"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006812}
6813
6814/*
6815 * ":=".
6816 */
6817 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006818ex_equal(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006819{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006820 smsg("%ld", (long)eap->line2);
Bram Moolenaardf177f62005-02-22 08:39:57 +00006821 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006822}
6823
6824 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006825ex_sleep(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006826{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006827 int n;
6828 long len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006829
6830 if (cursor_valid())
6831 {
6832 n = W_WINROW(curwin) + curwin->w_wrow - msg_scrolled;
6833 if (n >= 0)
Bram Moolenaar53f81742017-09-22 14:35:51 +02006834 windgoto((int)n, curwin->w_wincol + curwin->w_wcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006835 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006836
6837 len = eap->line2;
6838 switch (*eap->arg)
6839 {
6840 case 'm': break;
6841 case NUL: len *= 1000L; break;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006842 default: semsg(_(e_invarg2), eap->arg); return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006843 }
6844 do_sleep(len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006845}
6846
6847/*
6848 * Sleep for "msec" milliseconds, but keep checking for a CTRL-C every second.
6849 */
6850 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006851do_sleep(long msec)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006852{
Bram Moolenaar52953192019-08-16 10:27:13 +02006853 long done = 0;
Bram Moolenaar975b5272016-03-15 23:10:59 +01006854 long wait_now;
Bram Moolenaar52953192019-08-16 10:27:13 +02006855# ifdef ELAPSED_FUNC
6856 elapsed_T start_tv;
6857
6858 // Remember at what time we started, so that we know how much longer we
6859 // should wait after waiting for a bit.
6860 ELAPSED_INIT(start_tv);
6861# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006862
6863 cursor_on();
Bram Moolenaarf45d7472018-09-30 18:22:26 +02006864 out_flush_cursor(FALSE, FALSE);
Bram Moolenaar52953192019-08-16 10:27:13 +02006865 while (!got_int && done < msec)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006866 {
Bram Moolenaar975b5272016-03-15 23:10:59 +01006867 wait_now = msec - done > 1000L ? 1000L : msec - done;
6868#ifdef FEAT_TIMERS
6869 {
6870 long due_time = check_due_timer();
6871
6872 if (due_time > 0 && due_time < wait_now)
6873 wait_now = due_time;
6874 }
6875#endif
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02006876#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar28e67e02019-08-15 23:05:49 +02006877 if (has_any_channel() && wait_now > 20L)
6878 wait_now = 20L;
6879#endif
6880#ifdef FEAT_SOUND
6881 if (has_any_sound_callback() && wait_now > 20L)
6882 wait_now = 20L;
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02006883#endif
Bram Moolenaar975b5272016-03-15 23:10:59 +01006884 ui_delay(wait_now, TRUE);
Bram Moolenaar52953192019-08-16 10:27:13 +02006885
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02006886#ifdef FEAT_JOB_CHANNEL
6887 if (has_any_channel())
6888 ui_breakcheck_force(TRUE);
6889 else
6890#endif
6891 ui_breakcheck();
Bram Moolenaar93c88e02015-09-15 14:12:05 +02006892#ifdef MESSAGE_QUEUE
Bram Moolenaar52953192019-08-16 10:27:13 +02006893 // Process the netbeans and clientserver messages that may have been
6894 // received in the call to ui_breakcheck() when the GUI is in use. This
6895 // may occur when running a test case.
Bram Moolenaar93c88e02015-09-15 14:12:05 +02006896 parse_queued_messages();
Bram Moolenaare3cc6d42011-10-20 21:58:34 +02006897#endif
Bram Moolenaar52953192019-08-16 10:27:13 +02006898
6899# ifdef ELAPSED_FUNC
6900 // actual time passed
6901 done = ELAPSED_FUNC(start_tv);
6902# else
6903 // guestimate time passed (will actually be more)
6904 done += wait_now;
6905# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006906 }
Bram Moolenaar1ebff3d2018-07-07 16:18:13 +02006907
6908 // If CTRL-C was typed to interrupt the sleep, drop the CTRL-C from the
6909 // input buffer, otherwise a following call to input() fails.
6910 if (got_int)
6911 (void)vpeekc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006912}
6913
Bram Moolenaar071d4272004-06-13 20:20:40 +00006914/*
6915 * ":winsize" command (obsolete).
6916 */
6917 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006918ex_winsize(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006919{
6920 int w, h;
6921 char_u *arg = eap->arg;
6922 char_u *p;
6923
6924 w = getdigits(&arg);
6925 arg = skipwhite(arg);
6926 p = arg;
6927 h = getdigits(&arg);
6928 if (*p != NUL && *arg == NUL)
6929 set_shellsize(w, h, TRUE);
6930 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006931 emsg(_("E465: :winsize requires two number arguments"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006932}
6933
Bram Moolenaar071d4272004-06-13 20:20:40 +00006934 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006935ex_wincmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006936{
6937 int xchar = NUL;
6938 char_u *p;
6939
6940 if (*eap->arg == 'g' || *eap->arg == Ctrl_G)
6941 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006942 // CTRL-W g and CTRL-W CTRL-G have an extra command character
Bram Moolenaar071d4272004-06-13 20:20:40 +00006943 if (eap->arg[1] == NUL)
6944 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006945 emsg(_(e_invarg));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006946 return;
6947 }
6948 xchar = eap->arg[1];
6949 p = eap->arg + 2;
6950 }
6951 else
6952 p = eap->arg + 1;
6953
6954 eap->nextcmd = check_nextcmd(p);
6955 p = skipwhite(p);
6956 if (*p != NUL && *p != '"' && eap->nextcmd == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006957 emsg(_(e_invarg));
Bram Moolenaar12bde492011-06-13 01:19:56 +02006958 else if (!eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006959 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006960 // Pass flags on for ":vertical wincmd ]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006961 postponed_split_flags = cmdmod.split;
Bram Moolenaard326ce82007-03-11 14:48:29 +00006962 postponed_split_tab = cmdmod.tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006963 do_window(*eap->arg, eap->addr_count > 0 ? eap->line2 : 0L, xchar);
6964 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00006965 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006966 }
6967}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006968
Bram Moolenaar843ee412004-06-30 16:16:41 +00006969#if defined(FEAT_GUI) || defined(UNIX) || defined(VMS) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006970/*
6971 * ":winpos".
6972 */
6973 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006974ex_winpos(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006975{
6976 int x, y;
6977 char_u *arg = eap->arg;
6978 char_u *p;
6979
6980 if (*arg == NUL)
6981 {
Bram Moolenaar843ee412004-06-30 16:16:41 +00006982# if defined(FEAT_GUI) || defined(MSWIN)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02006983# ifdef VIMDLL
6984 if (gui.in_use ? gui_mch_get_winpos(&x, &y) != FAIL :
6985 mch_get_winpos(&x, &y) != FAIL)
6986# elif defined(FEAT_GUI)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006987 if (gui.in_use && gui_mch_get_winpos(&x, &y) != FAIL)
Bram Moolenaar843ee412004-06-30 16:16:41 +00006988# else
6989 if (mch_get_winpos(&x, &y) != FAIL)
6990# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006991 {
6992 sprintf((char *)IObuff, _("Window position: X %d, Y %d"), x, y);
Bram Moolenaar32526b32019-01-19 17:43:09 +01006993 msg((char *)IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006994 }
6995 else
6996# endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006997 emsg(_("E188: Obtaining window position not implemented for this platform"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006998 }
6999 else
7000 {
7001 x = getdigits(&arg);
7002 arg = skipwhite(arg);
7003 p = arg;
7004 y = getdigits(&arg);
7005 if (*p == NUL || *arg != NUL)
7006 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007007 emsg(_("E466: :winpos requires two number arguments"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007008 return;
7009 }
7010# ifdef FEAT_GUI
7011 if (gui.in_use)
7012 gui_mch_set_winpos(x, y);
7013 else if (gui.starting)
7014 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007015 // Remember the coordinates for when the window is opened.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007016 gui_win_x = x;
7017 gui_win_y = y;
7018 }
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007019# if defined(HAVE_TGETENT) || defined(VIMDLL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007020 else
7021# endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007022# endif
7023# if defined(MSWIN) && (!defined(FEAT_GUI) || defined(VIMDLL))
Bram Moolenaar843ee412004-06-30 16:16:41 +00007024 mch_set_winpos(x, y);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007025# endif
7026# ifdef HAVE_TGETENT
7027 if (*T_CWP)
7028 term_set_winpos(x, y);
7029# endif
7030 }
7031}
7032#endif
7033
7034/*
7035 * Handle command that work like operators: ":delete", ":yank", ":>" and ":<".
7036 */
7037 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007038ex_operators(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007039{
7040 oparg_T oa;
7041
7042 clear_oparg(&oa);
7043 oa.regname = eap->regname;
7044 oa.start.lnum = eap->line1;
7045 oa.end.lnum = eap->line2;
7046 oa.line_count = eap->line2 - eap->line1 + 1;
7047 oa.motion_type = MLINE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007048 virtual_op = FALSE;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007049 if (eap->cmdidx != CMD_yank) // position cursor for undo
Bram Moolenaar071d4272004-06-13 20:20:40 +00007050 {
7051 setpcmark();
7052 curwin->w_cursor.lnum = eap->line1;
7053 beginline(BL_SOL | BL_FIX);
7054 }
7055
Bram Moolenaard07c6e12013-11-21 14:21:40 +01007056 if (VIsual_active)
7057 end_visual_mode();
Bram Moolenaard07c6e12013-11-21 14:21:40 +01007058
Bram Moolenaar071d4272004-06-13 20:20:40 +00007059 switch (eap->cmdidx)
7060 {
7061 case CMD_delete:
7062 oa.op_type = OP_DELETE;
7063 op_delete(&oa);
7064 break;
7065
7066 case CMD_yank:
7067 oa.op_type = OP_YANK;
7068 (void)op_yank(&oa, FALSE, TRUE);
7069 break;
7070
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007071 default: // CMD_rshift or CMD_lshift
Bram Moolenaar6e707362013-06-14 19:15:58 +02007072 if (
Bram Moolenaar071d4272004-06-13 20:20:40 +00007073#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6e707362013-06-14 19:15:58 +02007074 (eap->cmdidx == CMD_rshift) ^ curwin->w_p_rl
7075#else
7076 eap->cmdidx == CMD_rshift
Bram Moolenaar071d4272004-06-13 20:20:40 +00007077#endif
Bram Moolenaar6e707362013-06-14 19:15:58 +02007078 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00007079 oa.op_type = OP_RSHIFT;
7080 else
7081 oa.op_type = OP_LSHIFT;
7082 op_shift(&oa, FALSE, eap->amount);
7083 break;
7084 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007085 virtual_op = MAYBE;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007086 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007087}
7088
7089/*
7090 * ":put".
7091 */
7092 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007093ex_put(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007094{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007095 // ":0put" works like ":1put!".
Bram Moolenaar071d4272004-06-13 20:20:40 +00007096 if (eap->line2 == 0)
7097 {
7098 eap->line2 = 1;
7099 eap->forceit = TRUE;
7100 }
7101 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007102 do_put(eap->regname, eap->forceit ? BACKWARD : FORWARD, 1L,
7103 PUT_LINE|PUT_CURSLINE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007104}
7105
7106/*
7107 * Handle ":copy" and ":move".
7108 */
7109 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007110ex_copymove(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007111{
7112 long n;
7113
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02007114 n = get_address(eap, &eap->arg, eap->addr_type, FALSE, FALSE, FALSE, 1);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007115 if (eap->arg == NULL) // error detected
Bram Moolenaar071d4272004-06-13 20:20:40 +00007116 {
7117 eap->nextcmd = NULL;
7118 return;
7119 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00007120 get_flags(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007121
7122 /*
7123 * move or copy lines from 'eap->line1'-'eap->line2' to below line 'n'
7124 */
7125 if (n == MAXLNUM || n < 0 || n > curbuf->b_ml.ml_line_count)
7126 {
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02007127 emsg(_(e_invrange));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007128 return;
7129 }
7130
7131 if (eap->cmdidx == CMD_move)
7132 {
7133 if (do_move(eap->line1, eap->line2, n) == FAIL)
7134 return;
7135 }
7136 else
7137 ex_copy(eap->line1, eap->line2, n);
7138 u_clearline();
7139 beginline(BL_SOL | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007140 ex_may_print(eap);
7141}
7142
7143/*
7144 * Print the current line if flags were given to the Ex command.
7145 */
Bram Moolenaarfd3fe982014-04-01 17:49:44 +02007146 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007147ex_may_print(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007148{
7149 if (eap->flags != 0)
7150 {
7151 print_line(curwin->w_cursor.lnum, (eap->flags & EXFLAG_NR),
7152 (eap->flags & EXFLAG_LIST));
7153 ex_no_reprint = TRUE;
7154 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007155}
7156
7157/*
7158 * ":smagic" and ":snomagic".
7159 */
7160 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007161ex_submagic(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007162{
7163 int magic_save = p_magic;
7164
7165 p_magic = (eap->cmdidx == CMD_smagic);
7166 do_sub(eap);
7167 p_magic = magic_save;
7168}
7169
7170/*
7171 * ":join".
7172 */
7173 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007174ex_join(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007175{
7176 curwin->w_cursor.lnum = eap->line1;
7177 if (eap->line1 == eap->line2)
7178 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007179 if (eap->addr_count >= 2) // :2,2join does nothing
Bram Moolenaar071d4272004-06-13 20:20:40 +00007180 return;
7181 if (eap->line2 == curbuf->b_ml.ml_line_count)
7182 {
7183 beep_flush();
7184 return;
7185 }
7186 ++eap->line2;
7187 }
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02007188 (void)do_join(eap->line2 - eap->line1 + 1, !eap->forceit, TRUE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007189 beginline(BL_WHITE | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007190 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007191}
7192
7193/*
7194 * ":[addr]@r" or ":[addr]*r": execute register
7195 */
7196 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007197ex_at(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007198{
7199 int c;
Bram Moolenaar60462872009-11-03 11:40:19 +00007200 int prev_len = typebuf.tb_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007201
7202 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaar4930a762016-09-11 14:39:53 +02007203 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007204
7205#ifdef USE_ON_FLY_SCROLL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007206 dont_scroll = TRUE; // disallow scrolling here
Bram Moolenaar071d4272004-06-13 20:20:40 +00007207#endif
7208
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007209 // get the register name. No name means to use the previous one
Bram Moolenaar071d4272004-06-13 20:20:40 +00007210 c = *eap->arg;
7211 if (c == NUL || (c == '*' && *eap->cmd == '*'))
7212 c = '@';
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007213 // Put the register in the typeahead buffer with the "silent" flag.
Bram Moolenaard333d1e2006-11-07 17:43:47 +00007214 if (do_execreg(c, TRUE, vim_strchr(p_cpo, CPO_EXECBUF) != NULL, TRUE)
7215 == FAIL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007216 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007217 beep_flush();
Bram Moolenaardf177f62005-02-22 08:39:57 +00007218 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007219 else
7220 {
7221 int save_efr = exec_from_reg;
7222
7223 exec_from_reg = TRUE;
7224
7225 /*
7226 * Execute from the typeahead buffer.
Bram Moolenaar60462872009-11-03 11:40:19 +00007227 * Continue until the stuff buffer is empty and all added characters
7228 * have been consumed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007229 */
Bram Moolenaar60462872009-11-03 11:40:19 +00007230 while (!stuff_empty() || typebuf.tb_len > prev_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007231 (void)do_cmdline(NULL, getexline, NULL, DOCMD_NOWAIT|DOCMD_VERBOSE);
7232
7233 exec_from_reg = save_efr;
7234 }
7235}
7236
7237/*
7238 * ":!".
7239 */
7240 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007241ex_bang(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007242{
7243 do_bang(eap->addr_count, eap, eap->forceit, TRUE, TRUE);
7244}
7245
7246/*
7247 * ":undo".
7248 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007249 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +01007250ex_undo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007251{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007252 if (eap->addr_count == 1) // :undo 123
Bram Moolenaar730cde92010-06-27 05:18:54 +02007253 undo_time(eap->line2, FALSE, FALSE, TRUE);
Bram Moolenaard3667a22006-03-16 21:35:52 +00007254 else
7255 u_undo(1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007256}
7257
Bram Moolenaar55debbe2010-05-23 23:34:36 +02007258#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar6df6f472010-07-18 18:04:50 +02007259 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007260ex_wundo(exarg_T *eap)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02007261{
7262 char_u hash[UNDO_HASH_SIZE];
7263
7264 u_compute_hash(hash);
7265 u_write_undo(eap->arg, eap->forceit, curbuf, hash);
7266}
7267
Bram Moolenaar6df6f472010-07-18 18:04:50 +02007268 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007269ex_rundo(exarg_T *eap)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02007270{
7271 char_u hash[UNDO_HASH_SIZE];
7272
7273 u_compute_hash(hash);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02007274 u_read_undo(eap->arg, hash, NULL);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02007275}
7276#endif
7277
Bram Moolenaar071d4272004-06-13 20:20:40 +00007278/*
7279 * ":redo".
7280 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007281 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007282ex_redo(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007283{
7284 u_redo(1);
7285}
7286
7287/*
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007288 * ":earlier" and ":later".
7289 */
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007290 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007291ex_later(exarg_T *eap)
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007292{
7293 long count = 0;
7294 int sec = FALSE;
Bram Moolenaar730cde92010-06-27 05:18:54 +02007295 int file = FALSE;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007296 char_u *p = eap->arg;
7297
7298 if (*p == NUL)
7299 count = 1;
7300 else if (isdigit(*p))
7301 {
7302 count = getdigits(&p);
7303 switch (*p)
7304 {
7305 case 's': ++p; sec = TRUE; break;
7306 case 'm': ++p; sec = TRUE; count *= 60; break;
7307 case 'h': ++p; sec = TRUE; count *= 60 * 60; break;
Bram Moolenaar730cde92010-06-27 05:18:54 +02007308 case 'd': ++p; sec = TRUE; count *= 24 * 60 * 60; break;
7309 case 'f': ++p; file = TRUE; break;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007310 }
7311 }
7312
7313 if (*p != NUL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007314 semsg(_(e_invarg2), eap->arg);
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007315 else
Bram Moolenaar730cde92010-06-27 05:18:54 +02007316 undo_time(eap->cmdidx == CMD_earlier ? -count : count,
7317 sec, file, FALSE);
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007318}
7319
7320/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007321 * ":redir": start/stop redirection.
7322 */
7323 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007324ex_redir(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007325{
7326 char *mode;
7327 char_u *fname;
Bram Moolenaarca472992005-01-21 11:46:23 +00007328 char_u *arg = eap->arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007329
Bram Moolenaarba768492016-07-08 15:32:54 +02007330#ifdef FEAT_EVAL
Bram Moolenaar79815f12016-07-09 17:07:29 +02007331 if (redir_execute)
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02007332 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007333 emsg(_("E930: Cannot use :redir inside execute()"));
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02007334 return;
7335 }
Bram Moolenaarba768492016-07-08 15:32:54 +02007336#endif
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02007337
Bram Moolenaar071d4272004-06-13 20:20:40 +00007338 if (STRICMP(eap->arg, "END") == 0)
7339 close_redir();
7340 else
7341 {
Bram Moolenaarca472992005-01-21 11:46:23 +00007342 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00007343 {
Bram Moolenaarca472992005-01-21 11:46:23 +00007344 ++arg;
7345 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00007346 {
Bram Moolenaarca472992005-01-21 11:46:23 +00007347 ++arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007348 mode = "a";
7349 }
7350 else
7351 mode = "w";
Bram Moolenaarca472992005-01-21 11:46:23 +00007352 arg = skipwhite(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007353
7354 close_redir();
7355
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007356 // Expand environment variables and "~/".
Bram Moolenaarca472992005-01-21 11:46:23 +00007357 fname = expand_env_save(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007358 if (fname == NULL)
7359 return;
7360#ifdef FEAT_BROWSE
7361 if (cmdmod.browse)
7362 {
7363 char_u *browseFile;
7364
Bram Moolenaar7171abe2004-10-11 10:06:20 +00007365 browseFile = do_browse(BROWSE_SAVE,
7366 (char_u *)_("Save Redirection"),
Bram Moolenaarc36651b2018-04-29 12:22:56 +02007367 fname, NULL, NULL,
7368 (char_u *)_(BROWSE_FILTER_ALL_FILES), curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007369 if (browseFile == NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007370 return; // operation cancelled
Bram Moolenaar071d4272004-06-13 20:20:40 +00007371 vim_free(fname);
7372 fname = browseFile;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007373 eap->forceit = TRUE; // since dialog already asked
Bram Moolenaar071d4272004-06-13 20:20:40 +00007374 }
7375#endif
7376
7377 redir_fd = open_exfile(fname, eap->forceit, mode);
7378 vim_free(fname);
7379 }
7380#ifdef FEAT_EVAL
Bram Moolenaarca472992005-01-21 11:46:23 +00007381 else if (*arg == '@')
Bram Moolenaar071d4272004-06-13 20:20:40 +00007382 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007383 // redirect to a register a-z (resp. A-Z for appending)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007384 close_redir();
Bram Moolenaarca472992005-01-21 11:46:23 +00007385 ++arg;
7386 if (ASCII_ISALPHA(*arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007387# ifdef FEAT_CLIPBOARD
Bram Moolenaarca472992005-01-21 11:46:23 +00007388 || *arg == '*'
Bram Moolenaar9a51c6e2006-11-14 19:25:02 +00007389 || *arg == '+'
Bram Moolenaar071d4272004-06-13 20:20:40 +00007390# endif
Bram Moolenaarca472992005-01-21 11:46:23 +00007391 || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00007392 {
Bram Moolenaarca472992005-01-21 11:46:23 +00007393 redir_reg = *arg++;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007394 if (*arg == '>' && arg[1] == '>') // append
Bram Moolenaard9d30582005-05-18 22:10:28 +00007395 arg += 2;
Bram Moolenaarc188b882007-10-19 14:20:54 +00007396 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007397 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007398 // Can use both "@a" and "@a>".
Bram Moolenaar2c29bee2005-06-01 21:46:07 +00007399 if (*arg == '>')
7400 arg++;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007401 // Make register empty when not using @A-@Z and the
7402 // command is valid.
Bram Moolenaarc188b882007-10-19 14:20:54 +00007403 if (*arg == NUL && !isupper(redir_reg))
7404 write_reg_contents(redir_reg, (char_u *)"", -1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007405 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00007406 }
7407 if (*arg != NUL)
7408 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00007409 redir_reg = 0;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007410 semsg(_(e_invarg2), eap->arg);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007411 }
7412 }
7413 else if (*arg == '=' && arg[1] == '>')
7414 {
7415 int append;
7416
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007417 // redirect to a variable
Bram Moolenaardf177f62005-02-22 08:39:57 +00007418 close_redir();
7419 arg += 2;
7420
7421 if (*arg == '>')
7422 {
7423 ++arg;
7424 append = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007425 }
7426 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00007427 append = FALSE;
7428
7429 if (var_redir_start(skipwhite(arg), append) == OK)
7430 redir_vname = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007431 }
7432#endif
7433
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007434 // TODO: redirect to a buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00007435
Bram Moolenaar071d4272004-06-13 20:20:40 +00007436 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007437 semsg(_(e_invarg2), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007438 }
Bram Moolenaar29b2d262006-09-10 19:07:28 +00007439
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007440 // Make sure redirection is not off. Can happen for cmdline completion
7441 // that indirectly invokes a command to catch its output.
Bram Moolenaar29b2d262006-09-10 19:07:28 +00007442 if (redir_fd != NULL
7443#ifdef FEAT_EVAL
7444 || redir_reg || redir_vname
7445#endif
7446 )
7447 redir_off = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007448}
7449
7450/*
7451 * ":redraw": force redraw
7452 */
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01007453 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007454ex_redraw(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007455{
7456 int r = RedrawingDisabled;
7457 int p = p_lz;
7458
7459 RedrawingDisabled = 0;
7460 p_lz = FALSE;
Bram Moolenaarabc39ab2017-03-01 18:04:05 +01007461 validate_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007462 update_topline();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007463 update_screen(eap->forceit ? CLEAR : VIsual_active ? INVERTED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007464#ifdef FEAT_TITLE
7465 if (need_maketitle)
7466 maketitle();
7467#endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007468#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
7469# ifdef VIMDLL
7470 if (!gui.in_use)
7471# endif
7472 resize_console_buf();
Bram Moolenaar78d21da2019-02-17 15:00:52 +01007473#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007474 RedrawingDisabled = r;
7475 p_lz = p;
7476
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007477 // Reset msg_didout, so that a message that's there is overwritten.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007478 msg_didout = FALSE;
7479 msg_col = 0;
7480
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007481 // No need to wait after an intentional redraw.
Bram Moolenaar56667a52013-07-17 11:54:28 +02007482 need_wait_return = FALSE;
7483
Bram Moolenaar071d4272004-06-13 20:20:40 +00007484 out_flush();
7485}
7486
7487/*
7488 * ":redrawstatus": force redraw of status line(s)
7489 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007490 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007491ex_redrawstatus(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007492{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007493 int r = RedrawingDisabled;
7494 int p = p_lz;
7495
7496 RedrawingDisabled = 0;
7497 p_lz = FALSE;
7498 if (eap->forceit)
7499 status_redraw_all();
7500 else
7501 status_redraw_curbuf();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007502 update_screen(VIsual_active ? INVERTED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007503 RedrawingDisabled = r;
7504 p_lz = p;
7505 out_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007506}
7507
Bram Moolenaare12bab32019-01-08 22:02:56 +01007508/*
7509 * ":redrawtabline": force redraw of the tabline
7510 */
7511 static void
7512ex_redrawtabline(exarg_T *eap UNUSED)
7513{
7514 int r = RedrawingDisabled;
7515 int p = p_lz;
7516
7517 RedrawingDisabled = 0;
7518 p_lz = FALSE;
7519
7520 draw_tabline();
7521
7522 RedrawingDisabled = r;
7523 p_lz = p;
7524 out_flush();
7525}
7526
Bram Moolenaar071d4272004-06-13 20:20:40 +00007527 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007528close_redir(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007529{
7530 if (redir_fd != NULL)
7531 {
7532 fclose(redir_fd);
7533 redir_fd = NULL;
7534 }
7535#ifdef FEAT_EVAL
7536 redir_reg = 0;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007537 if (redir_vname)
7538 {
7539 var_redir_stop();
7540 redir_vname = 0;
7541 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007542#endif
7543}
7544
Bram Moolenaarf96ae0b2019-07-28 15:21:55 +02007545#if (defined(FEAT_SESSION) || defined(FEAT_EVAL)) || defined(PROTO)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007546 int
Bram Moolenaarbd67aac2019-09-21 23:09:04 +02007547vim_mkdir_emsg(char_u *name, int prot UNUSED)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007548{
7549 if (vim_mkdir(name, prot) != 0)
7550 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007551 semsg(_("E739: Cannot create directory: %s"), name);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007552 return FAIL;
7553 }
7554 return OK;
7555}
7556#endif
7557
Bram Moolenaar071d4272004-06-13 20:20:40 +00007558/*
7559 * Open a file for writing for an Ex command, with some checks.
7560 * Return file descriptor, or NULL on failure.
7561 */
7562 FILE *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007563open_exfile(
7564 char_u *fname,
7565 int forceit,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007566 char *mode) // "w" for create new file or "a" for append
Bram Moolenaar071d4272004-06-13 20:20:40 +00007567{
7568 FILE *fd;
7569
7570#ifdef UNIX
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007571 // with Unix it is possible to open a directory
Bram Moolenaar071d4272004-06-13 20:20:40 +00007572 if (mch_isdir(fname))
7573 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007574 semsg(_(e_isadir2), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007575 return NULL;
7576 }
7577#endif
7578 if (!forceit && *mode != 'a' && vim_fexists(fname))
7579 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007580 semsg(_("E189: \"%s\" exists (add ! to override)"), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007581 return NULL;
7582 }
7583
7584 if ((fd = mch_fopen((char *)fname, mode)) == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007585 semsg(_("E190: Cannot open \"%s\" for writing"), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007586
7587 return fd;
7588}
7589
7590/*
7591 * ":mark" and ":k".
7592 */
7593 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007594ex_mark(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007595{
7596 pos_T pos;
7597
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007598 if (*eap->arg == NUL) // No argument?
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007599 emsg(_(e_argreq));
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007600 else if (eap->arg[1] != NUL) // more than one character?
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007601 emsg(_(e_trailing));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007602 else
7603 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007604 pos = curwin->w_cursor; // save curwin->w_cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00007605 curwin->w_cursor.lnum = eap->line2;
7606 beginline(BL_WHITE | BL_FIX);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007607 if (setmark(*eap->arg) == FAIL) // set mark
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007608 emsg(_("E191: Argument must be a letter or forward/backward quote"));
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007609 curwin->w_cursor = pos; // restore curwin->w_cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00007610 }
7611}
7612
7613/*
7614 * Update w_topline, w_leftcol and the cursor position.
7615 */
7616 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007617update_topline_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007618{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007619 check_cursor(); // put cursor on valid line
Bram Moolenaar071d4272004-06-13 20:20:40 +00007620 update_topline();
7621 if (!curwin->w_p_wrap)
7622 validate_cursor();
7623 update_curswant();
7624}
7625
Bram Moolenaar071d4272004-06-13 20:20:40 +00007626/*
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007627 * Save the current State and go to Normal mode.
7628 * Return TRUE if the typeahead could be saved.
7629 */
7630 int
7631save_current_state(save_state_T *sst)
7632{
7633 sst->save_msg_scroll = msg_scroll;
7634 sst->save_restart_edit = restart_edit;
7635 sst->save_msg_didout = msg_didout;
7636 sst->save_State = State;
7637 sst->save_insertmode = p_im;
7638 sst->save_finish_op = finish_op;
7639 sst->save_opcount = opcount;
Bram Moolenaarcce713d2019-03-04 11:40:12 +01007640 sst->save_reg_executing = reg_executing;
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007641
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007642 msg_scroll = FALSE; // no msg scrolling in Normal mode
7643 restart_edit = 0; // don't go to Insert mode
7644 p_im = FALSE; // don't use 'insertmode'
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007645
7646 /*
7647 * Save the current typeahead. This is required to allow using ":normal"
7648 * from an event handler and makes sure we don't hang when the argument
7649 * ends with half a command.
7650 */
7651 save_typeahead(&sst->tabuf);
7652 return sst->tabuf.typebuf_valid;
7653}
7654
7655 void
7656restore_current_state(save_state_T *sst)
7657{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007658 // Restore the previous typeahead.
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007659 restore_typeahead(&sst->tabuf);
7660
7661 msg_scroll = sst->save_msg_scroll;
7662 restart_edit = sst->save_restart_edit;
7663 p_im = sst->save_insertmode;
7664 finish_op = sst->save_finish_op;
7665 opcount = sst->save_opcount;
Bram Moolenaarcce713d2019-03-04 11:40:12 +01007666 reg_executing = sst->save_reg_executing;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007667 msg_didout |= sst->save_msg_didout; // don't reset msg_didout now
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007668
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007669 // Restore the state (needed when called from a function executed for
7670 // 'indentexpr'). Update the mouse and cursor, they may have changed.
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007671 State = sst->save_State;
7672#ifdef CURSOR_SHAPE
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007673 ui_cursor_shape(); // may show different cursor shape
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007674#endif
7675}
7676
7677/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007678 * ":normal[!] {commands}": Execute normal mode commands.
7679 */
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01007680 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007681ex_normal(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007682{
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007683 save_state_T save_state;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007684 char_u *arg = NULL;
7685 int l;
7686 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007687
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00007688 if (ex_normal_lock > 0)
7689 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007690 emsg(_(e_secure));
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00007691 return;
7692 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007693 if (ex_normal_busy >= p_mmd)
7694 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007695 emsg(_("E192: Recursive use of :normal too deep"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007696 return;
7697 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007698
Bram Moolenaar071d4272004-06-13 20:20:40 +00007699 /*
7700 * vgetc() expects a CSI and K_SPECIAL to have been escaped. Don't do
7701 * this for the K_SPECIAL leading byte, otherwise special keys will not
7702 * work.
7703 */
7704 if (has_mbyte)
7705 {
7706 int len = 0;
7707
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007708 // Count the number of characters to be escaped.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007709 for (p = eap->arg; *p != NUL; ++p)
7710 {
Bram Moolenaar13505972019-01-24 15:04:48 +01007711#ifdef FEAT_GUI
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007712 if (*p == CSI) // leadbyte CSI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007713 len += 2;
Bram Moolenaar13505972019-01-24 15:04:48 +01007714#endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007715 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007716 if (*++p == K_SPECIAL // trailbyte K_SPECIAL or CSI
Bram Moolenaar13505972019-01-24 15:04:48 +01007717#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007718 || *p == CSI
Bram Moolenaar13505972019-01-24 15:04:48 +01007719#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007720 )
7721 len += 2;
7722 }
7723 if (len > 0)
7724 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02007725 arg = alloc(STRLEN(eap->arg) + len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007726 if (arg != NULL)
7727 {
7728 len = 0;
7729 for (p = eap->arg; *p != NUL; ++p)
7730 {
7731 arg[len++] = *p;
Bram Moolenaar13505972019-01-24 15:04:48 +01007732#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007733 if (*p == CSI)
7734 {
7735 arg[len++] = KS_EXTRA;
7736 arg[len++] = (int)KE_CSI;
7737 }
Bram Moolenaar13505972019-01-24 15:04:48 +01007738#endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007739 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007740 {
7741 arg[len++] = *++p;
7742 if (*p == K_SPECIAL)
7743 {
7744 arg[len++] = KS_SPECIAL;
7745 arg[len++] = KE_FILLER;
7746 }
Bram Moolenaar13505972019-01-24 15:04:48 +01007747#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007748 else if (*p == CSI)
7749 {
7750 arg[len++] = KS_EXTRA;
7751 arg[len++] = (int)KE_CSI;
7752 }
Bram Moolenaar13505972019-01-24 15:04:48 +01007753#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007754 }
7755 arg[len] = NUL;
7756 }
7757 }
7758 }
7759 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007760
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007761 ++ex_normal_busy;
7762 if (save_current_state(&save_state))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007763 {
7764 /*
7765 * Repeat the :normal command for each line in the range. When no
7766 * range given, execute it just once, without positioning the cursor
7767 * first.
7768 */
7769 do
7770 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007771 if (eap->addr_count != 0)
7772 {
7773 curwin->w_cursor.lnum = eap->line1++;
7774 curwin->w_cursor.col = 0;
Bram Moolenaard5d37532017-03-27 23:02:07 +02007775 check_cursor_moved(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007776 }
7777
Bram Moolenaar13505972019-01-24 15:04:48 +01007778 exec_normal_cmd(arg != NULL
7779 ? arg
7780 : eap->arg, eap->forceit ? REMAP_NONE : REMAP_YES, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007781 }
7782 while (eap->addr_count > 0 && eap->line1 <= eap->line2 && !got_int);
7783 }
7784
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007785 // Might not return to the main loop when in an event handler.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007786 update_topline_cursor();
7787
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007788 restore_current_state(&save_state);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007789 --ex_normal_busy;
Bram Moolenaareda73602014-11-05 09:53:23 +01007790 setmouse();
Bram Moolenaareda73602014-11-05 09:53:23 +01007791#ifdef CURSOR_SHAPE
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007792 ui_cursor_shape(); // may show different cursor shape
Bram Moolenaareda73602014-11-05 09:53:23 +01007793#endif
7794
Bram Moolenaar071d4272004-06-13 20:20:40 +00007795 vim_free(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007796}
7797
7798/*
Bram Moolenaar64969662005-12-14 21:59:55 +00007799 * ":startinsert", ":startreplace" and ":startgreplace"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007800 */
7801 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007802ex_startinsert(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007803{
Bram Moolenaarfd371682005-01-14 21:42:54 +00007804 if (eap->forceit)
7805 {
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02007806 // cursor line can be zero on startup
Bram Moolenaar09ca9322017-09-26 17:40:45 +02007807 if (!curwin->w_cursor.lnum)
7808 curwin->w_cursor.lnum = 1;
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02007809 set_cursor_for_append_to_line();
Bram Moolenaarfd371682005-01-14 21:42:54 +00007810 }
Bram Moolenaareb58a242020-04-19 18:13:19 +02007811#ifdef FEAT_TERMINAL
7812 // Ignore this when running in an active terminal.
7813 if (term_job_running(curbuf->b_term))
7814 return;
7815#endif
Bram Moolenaarfd371682005-01-14 21:42:54 +00007816
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02007817 // Ignore the command when already in Insert mode. Inserting an
7818 // expression register that invokes a function can do this.
Bram Moolenaara40c5002005-01-09 21:16:21 +00007819 if (State & INSERT)
7820 return;
7821
Bram Moolenaar64969662005-12-14 21:59:55 +00007822 if (eap->cmdidx == CMD_startinsert)
7823 restart_edit = 'a';
7824 else if (eap->cmdidx == CMD_startreplace)
7825 restart_edit = 'R';
Bram Moolenaar071d4272004-06-13 20:20:40 +00007826 else
Bram Moolenaar64969662005-12-14 21:59:55 +00007827 restart_edit = 'V';
7828
7829 if (!eap->forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007830 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00007831 if (eap->cmdidx == CMD_startinsert)
7832 restart_edit = 'i';
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02007833 curwin->w_curswant = 0; // avoid MAXCOL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007834 }
7835}
7836
7837/*
7838 * ":stopinsert"
7839 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007840 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007841ex_stopinsert(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007842{
7843 restart_edit = 0;
7844 stop_insert_mode = TRUE;
Bram Moolenaarfd773e92016-04-02 19:39:16 +02007845 clearmode();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007846}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007847
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00007848/*
7849 * Execute normal mode command "cmd".
7850 * "remap" can be REMAP_NONE or REMAP_YES.
7851 */
7852 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007853exec_normal_cmd(char_u *cmd, int remap, int silent)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00007854{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007855 // Stuff the argument into the typeahead buffer.
Bram Moolenaar25281632016-01-21 23:32:32 +01007856 ins_typebuf(cmd, remap, 0, TRUE, silent);
Bram Moolenaar586c70c2018-10-02 16:23:58 +02007857 exec_normal(FALSE, FALSE, FALSE);
Bram Moolenaar25281632016-01-21 23:32:32 +01007858}
Bram Moolenaar25281632016-01-21 23:32:32 +01007859
Bram Moolenaar25281632016-01-21 23:32:32 +01007860/*
7861 * Execute normal_cmd() until there is no typeahead left.
Bram Moolenaar586c70c2018-10-02 16:23:58 +02007862 * When "use_vpeekc" is TRUE use vpeekc() to check for available chars.
Bram Moolenaar25281632016-01-21 23:32:32 +01007863 */
7864 void
Bram Moolenaar586c70c2018-10-02 16:23:58 +02007865exec_normal(int was_typed, int use_vpeekc, int may_use_terminal_loop UNUSED)
Bram Moolenaar25281632016-01-21 23:32:32 +01007866{
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00007867 oparg_T oa;
Bram Moolenaar905dd902019-04-07 14:21:47 +02007868 int c;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00007869
Bram Moolenaar905dd902019-04-07 14:21:47 +02007870 // When calling vpeekc() from feedkeys() it will return Ctrl_C when there
7871 // is nothing to get, so also check for Ctrl_C.
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00007872 clear_oparg(&oa);
7873 finish_op = FALSE;
Bram Moolenaar586c70c2018-10-02 16:23:58 +02007874 while ((!stuff_empty()
7875 || ((was_typed || !typebuf_typed()) && typebuf.tb_len > 0)
Bram Moolenaar905dd902019-04-07 14:21:47 +02007876 || (use_vpeekc && (c = vpeekc()) != NUL && c != Ctrl_C))
Bram Moolenaar586c70c2018-10-02 16:23:58 +02007877 && !got_int)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00007878 {
7879 update_topline_cursor();
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02007880#ifdef FEAT_TERMINAL
Bram Moolenaar655a82a2018-05-08 22:01:07 +02007881 if (may_use_terminal_loop && term_use_loop()
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02007882 && oa.op_type == OP_NOP && oa.regname == NUL
7883 && !VIsual_active)
7884 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007885 // If terminal_loop() returns OK we got a key that is handled
7886 // in Normal model. With FAIL we first need to position the
7887 // cursor and the screen needs to be redrawn.
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02007888 if (terminal_loop(TRUE) == OK)
7889 normal_cmd(&oa, TRUE);
7890 }
7891 else
7892#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007893 // execute a Normal mode cmd
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02007894 normal_cmd(&oa, TRUE);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00007895 }
7896}
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00007897
Bram Moolenaar071d4272004-06-13 20:20:40 +00007898#ifdef FEAT_FIND_ID
7899 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007900ex_checkpath(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007901{
7902 find_pattern_in_path(NULL, 0, 0, FALSE, FALSE, CHECK_PATH, 1L,
7903 eap->forceit ? ACTION_SHOW_ALL : ACTION_SHOW,
7904 (linenr_T)1, (linenr_T)MAXLNUM);
7905}
7906
Bram Moolenaar4033c552017-09-16 20:54:51 +02007907#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007908/*
7909 * ":psearch"
7910 */
7911 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007912ex_psearch(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007913{
7914 g_do_tagpreview = p_pvh;
7915 ex_findpat(eap);
7916 g_do_tagpreview = 0;
7917}
7918#endif
7919
7920 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007921ex_findpat(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007922{
7923 int whole = TRUE;
7924 long n;
7925 char_u *p;
7926 int action;
7927
7928 switch (cmdnames[eap->cmdidx].cmd_name[2])
7929 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007930 case 'e': // ":psearch", ":isearch" and ":dsearch"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007931 if (cmdnames[eap->cmdidx].cmd_name[0] == 'p')
7932 action = ACTION_GOTO;
7933 else
7934 action = ACTION_SHOW;
7935 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007936 case 'i': // ":ilist" and ":dlist"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007937 action = ACTION_SHOW_ALL;
7938 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007939 case 'u': // ":ijump" and ":djump"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007940 action = ACTION_GOTO;
7941 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007942 default: // ":isplit" and ":dsplit"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007943 action = ACTION_SPLIT;
7944 break;
7945 }
7946
7947 n = 1;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007948 if (vim_isdigit(*eap->arg)) // get count
Bram Moolenaar071d4272004-06-13 20:20:40 +00007949 {
7950 n = getdigits(&eap->arg);
7951 eap->arg = skipwhite(eap->arg);
7952 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007953 if (*eap->arg == '/') // Match regexp, not just whole words
Bram Moolenaar071d4272004-06-13 20:20:40 +00007954 {
7955 whole = FALSE;
7956 ++eap->arg;
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02007957 p = skip_regexp(eap->arg, '/', p_magic);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007958 if (*p)
7959 {
7960 *p++ = NUL;
7961 p = skipwhite(p);
7962
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007963 // Check for trailing illegal characters
Bram Moolenaarfaac4102020-04-20 17:46:14 +02007964 if (!ends_excmd2(eap->arg, p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007965 eap->errmsg = e_trailing;
7966 else
7967 eap->nextcmd = check_nextcmd(p);
7968 }
7969 }
7970 if (!eap->skip)
7971 find_pattern_in_path(eap->arg, 0, (int)STRLEN(eap->arg),
7972 whole, !eap->forceit,
7973 *eap->cmd == 'd' ? FIND_DEFINE : FIND_ANY,
7974 n, action, eap->line1, eap->line2);
7975}
7976#endif
7977
Bram Moolenaar071d4272004-06-13 20:20:40 +00007978
Bram Moolenaar4033c552017-09-16 20:54:51 +02007979#ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00007980/*
7981 * ":ptag", ":ptselect", ":ptjump", ":ptnext", etc.
7982 */
7983 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007984ex_ptag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007985{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007986 g_do_tagpreview = p_pvh; // will be reset to 0 in ex_tag_cmd()
Bram Moolenaar071d4272004-06-13 20:20:40 +00007987 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
7988}
7989
7990/*
7991 * ":pedit"
7992 */
7993 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007994ex_pedit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007995{
7996 win_T *curwin_save = curwin;
7997
Bram Moolenaar3a2505c2020-03-09 16:40:41 +01007998 if (ERROR_IF_ANY_POPUP_WINDOW)
7999 return;
8000
Bram Moolenaar026587b2019-08-17 15:08:00 +02008001 // Open the preview window or popup and make it the current window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008002 g_do_tagpreview = p_pvh;
Bram Moolenaar576a4a62019-08-18 15:25:17 +02008003 prepare_tagpreview(TRUE, TRUE, FALSE);
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02008004
Bram Moolenaar026587b2019-08-17 15:08:00 +02008005 // Edit the file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008006 do_exedit(eap, NULL);
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02008007
Bram Moolenaar071d4272004-06-13 20:20:40 +00008008 if (curwin != curwin_save && win_valid(curwin_save))
8009 {
Bram Moolenaar026587b2019-08-17 15:08:00 +02008010 // Return cursor to where we were
Bram Moolenaar071d4272004-06-13 20:20:40 +00008011 validate_cursor();
8012 redraw_later(VALID);
8013 win_enter(curwin_save, TRUE);
8014 }
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01008015# ifdef FEAT_PROP_POPUP
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02008016 else if (WIN_IS_POPUP(curwin))
8017 {
8018 // can't keep focus in popup window
8019 win_enter(firstwin, TRUE);
8020 }
8021# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008022 g_do_tagpreview = 0;
8023}
Bram Moolenaar4033c552017-09-16 20:54:51 +02008024#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008025
8026/*
8027 * ":stag", ":stselect" and ":stjump".
8028 */
8029 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008030ex_stag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008031{
8032 postponed_split = -1;
8033 postponed_split_flags = cmdmod.split;
Bram Moolenaard326ce82007-03-11 14:48:29 +00008034 postponed_split_tab = cmdmod.tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008035 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
8036 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00008037 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008038}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008039
8040/*
8041 * ":tag", ":tselect", ":tjump", ":tnext", etc.
8042 */
8043 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008044ex_tag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008045{
8046 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name);
8047}
8048
8049 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008050ex_tag_cmd(exarg_T *eap, char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008051{
8052 int cmd;
8053
8054 switch (name[1])
8055 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008056 case 'j': cmd = DT_JUMP; // ":tjump"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008057 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008058 case 's': cmd = DT_SELECT; // ":tselect"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008059 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008060 case 'p': cmd = DT_PREV; // ":tprevious"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008061 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008062 case 'N': cmd = DT_PREV; // ":tNext"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008063 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008064 case 'n': cmd = DT_NEXT; // ":tnext"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008065 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008066 case 'o': cmd = DT_POP; // ":pop"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008067 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008068 case 'f': // ":tfirst"
8069 case 'r': cmd = DT_FIRST; // ":trewind"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008070 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008071 case 'l': cmd = DT_LAST; // ":tlast"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008072 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008073 default: // ":tag"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008074#ifdef FEAT_CSCOPE
Bram Moolenaar7c94c262008-06-20 09:11:34 +00008075 if (p_cst && *eap->arg != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008076 {
Bram Moolenaard4db7712016-11-12 19:16:46 +01008077 ex_cstag(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008078 return;
8079 }
8080#endif
8081 cmd = DT_TAG;
8082 break;
8083 }
8084
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00008085 if (name[0] == 'l')
8086 {
8087#ifndef FEAT_QUICKFIX
8088 ex_ni(eap);
8089 return;
8090#else
8091 cmd = DT_LTAG;
8092#endif
8093 }
8094
Bram Moolenaar071d4272004-06-13 20:20:40 +00008095 do_tag(eap->arg, cmd, eap->addr_count > 0 ? (int)eap->line2 : 1,
8096 eap->forceit, TRUE);
8097}
8098
8099/*
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008100 * Check "str" for starting with a special cmdline variable.
8101 * If found return one of the SPEC_ values and set "*usedlen" to the length of
8102 * the variable. Otherwise return -1 and "*usedlen" is unchanged.
8103 */
8104 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008105find_cmdline_var(char_u *src, int *usedlen)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008106{
8107 int len;
8108 int i;
Bram Moolenaar8f0b2d42009-05-16 14:41:10 +00008109 static char *(spec_str[]) = {
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008110 "%",
8111#define SPEC_PERC 0
8112 "#",
Bram Moolenaar65f08472017-09-10 18:16:20 +02008113#define SPEC_HASH (SPEC_PERC + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008114 "<cword>", // cursor word
Bram Moolenaar65f08472017-09-10 18:16:20 +02008115#define SPEC_CWORD (SPEC_HASH + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008116 "<cWORD>", // cursor WORD
Bram Moolenaar65f08472017-09-10 18:16:20 +02008117#define SPEC_CCWORD (SPEC_CWORD + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008118 "<cexpr>", // expr under cursor
Bram Moolenaar65f08472017-09-10 18:16:20 +02008119#define SPEC_CEXPR (SPEC_CCWORD + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008120 "<cfile>", // cursor path name
Bram Moolenaar65f08472017-09-10 18:16:20 +02008121#define SPEC_CFILE (SPEC_CEXPR + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008122 "<sfile>", // ":so" file name
Bram Moolenaar65f08472017-09-10 18:16:20 +02008123#define SPEC_SFILE (SPEC_CFILE + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008124 "<slnum>", // ":so" file line number
Bram Moolenaar65f08472017-09-10 18:16:20 +02008125#define SPEC_SLNUM (SPEC_SFILE + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008126 "<afile>", // autocommand file name
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008127#define SPEC_AFILE (SPEC_SLNUM + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008128 "<abuf>", // autocommand buffer number
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008129#define SPEC_ABUF (SPEC_AFILE + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008130 "<amatch>", // autocommand match name
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01008131#define SPEC_AMATCH (SPEC_ABUF + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008132 "<sflnum>", // script file line number
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008133#define SPEC_SFLNUM (SPEC_AMATCH + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008134#ifdef FEAT_CLIENTSERVER
8135 "<client>"
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008136# define SPEC_CLIENT (SPEC_SFLNUM + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008137#endif
8138 };
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008139
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00008140 for (i = 0; i < (int)(sizeof(spec_str) / sizeof(char *)); ++i)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008141 {
8142 len = (int)STRLEN(spec_str[i]);
8143 if (STRNCMP(src, spec_str[i], len) == 0)
8144 {
8145 *usedlen = len;
8146 return i;
8147 }
8148 }
8149 return -1;
8150}
8151
8152/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008153 * Evaluate cmdline variables.
8154 *
8155 * change '%' to curbuf->b_ffname
8156 * '#' to curwin->w_altfile
8157 * '<cword>' to word under the cursor
8158 * '<cWORD>' to WORD under the cursor
Bram Moolenaar8071cb22019-07-12 17:58:01 +02008159 * '<cexpr>' to C-expression under the cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00008160 * '<cfile>' to path name under the cursor
8161 * '<sfile>' to sourced file name
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01008162 * '<slnum>' to sourced file line number
Bram Moolenaar071d4272004-06-13 20:20:40 +00008163 * '<afile>' to file name for autocommand
8164 * '<abuf>' to buffer number for autocommand
8165 * '<amatch>' to matching name for autocommand
8166 *
8167 * When an error is detected, "errormsg" is set to a non-NULL pointer (may be
8168 * "" for error without a message) and NULL is returned.
8169 * Returns an allocated string if a valid match was found.
8170 * Returns NULL if no match was found. "usedlen" then still contains the
8171 * number of characters to skip.
8172 */
8173 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008174eval_vars(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008175 char_u *src, // pointer into commandline
8176 char_u *srcstart, // beginning of valid memory for src
8177 int *usedlen, // characters after src that are used
8178 linenr_T *lnump, // line number for :e command, or NULL
8179 char **errormsg, // pointer to error message
8180 int *escaped) // return value has escaped white space (can
8181 // be NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008182{
8183 int i;
8184 char_u *s;
8185 char_u *result;
8186 char_u *resultbuf = NULL;
8187 int resultlen;
8188 buf_T *buf;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008189 int valid = VALID_HEAD + VALID_PATH; // assume valid result
Bram Moolenaar071d4272004-06-13 20:20:40 +00008190 int spec_idx;
Bram Moolenaarfd249462018-07-28 16:14:30 +02008191 int tilde_file = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008192 int skip_mod = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008193 char_u strbuf[30];
Bram Moolenaar071d4272004-06-13 20:20:40 +00008194
8195 *errormsg = NULL;
Bram Moolenaar63b92542007-03-27 14:57:09 +00008196 if (escaped != NULL)
8197 *escaped = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008198
8199 /*
8200 * Check if there is something to do.
8201 */
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008202 spec_idx = find_cmdline_var(src, usedlen);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008203 if (spec_idx < 0) // no match
Bram Moolenaar071d4272004-06-13 20:20:40 +00008204 {
8205 *usedlen = 1;
8206 return NULL;
8207 }
8208
8209 /*
8210 * Skip when preceded with a backslash "\%" and "\#".
8211 * Note: In "\\%" the % is also not recognized!
8212 */
8213 if (src > srcstart && src[-1] == '\\')
8214 {
8215 *usedlen = 0;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008216 STRMOVE(src - 1, src); // remove backslash
Bram Moolenaar071d4272004-06-13 20:20:40 +00008217 return NULL;
8218 }
8219
8220 /*
8221 * word or WORD under cursor
8222 */
Bram Moolenaar65f08472017-09-10 18:16:20 +02008223 if (spec_idx == SPEC_CWORD || spec_idx == SPEC_CCWORD
8224 || spec_idx == SPEC_CEXPR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008225 {
Bram Moolenaar65f08472017-09-10 18:16:20 +02008226 resultlen = find_ident_under_cursor(&result,
8227 spec_idx == SPEC_CWORD ? (FIND_IDENT | FIND_STRING)
8228 : spec_idx == SPEC_CEXPR ? (FIND_IDENT | FIND_STRING | FIND_EVAL)
8229 : FIND_STRING);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008230 if (resultlen == 0)
8231 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008232 *errormsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +00008233 return NULL;
8234 }
8235 }
8236
8237 /*
8238 * '#': Alternate file name
8239 * '%': Current file name
8240 * File name under the cursor
8241 * File name for autocommand
8242 * and following modifiers
8243 */
8244 else
8245 {
8246 switch (spec_idx)
8247 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008248 case SPEC_PERC: // '%': current file
Bram Moolenaar071d4272004-06-13 20:20:40 +00008249 if (curbuf->b_fname == NULL)
8250 {
8251 result = (char_u *)"";
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008252 valid = 0; // Must have ":p:h" to be valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00008253 }
8254 else
Bram Moolenaar00136dc2018-07-25 21:19:13 +02008255 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008256 result = curbuf->b_fname;
Bram Moolenaar00136dc2018-07-25 21:19:13 +02008257 tilde_file = STRCMP(result, "~") == 0;
8258 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008259 break;
8260
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008261 case SPEC_HASH: // '#' or "#99": alternate file
8262 if (src[1] == '#') // "##": the argument list
Bram Moolenaar071d4272004-06-13 20:20:40 +00008263 {
8264 result = arg_all();
8265 resultbuf = result;
8266 *usedlen = 2;
Bram Moolenaar63b92542007-03-27 14:57:09 +00008267 if (escaped != NULL)
8268 *escaped = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008269 skip_mod = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008270 break;
8271 }
8272 s = src + 1;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008273 if (*s == '<') // "#<99" uses v:oldfiles
Bram Moolenaard812df62008-11-09 12:46:09 +00008274 ++s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008275 i = (int)getdigits(&s);
Bram Moolenaarc312b8b2017-10-28 17:53:04 +02008276 if (s == src + 2 && src[1] == '-')
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008277 // just a minus sign, don't skip over it
Bram Moolenaarc312b8b2017-10-28 17:53:04 +02008278 s--;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008279 *usedlen = (int)(s - src); // length of what we expand
Bram Moolenaar071d4272004-06-13 20:20:40 +00008280
Bram Moolenaarc312b8b2017-10-28 17:53:04 +02008281 if (src[1] == '<' && i != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008282 {
Bram Moolenaard812df62008-11-09 12:46:09 +00008283 if (*usedlen < 2)
8284 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008285 // Should we give an error message for #<text?
Bram Moolenaard812df62008-11-09 12:46:09 +00008286 *usedlen = 1;
8287 return NULL;
8288 }
8289#ifdef FEAT_EVAL
8290 result = list_find_str(get_vim_var_list(VV_OLDFILES),
8291 (long)i);
8292 if (result == NULL)
8293 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008294 *errormsg = "";
Bram Moolenaard812df62008-11-09 12:46:09 +00008295 return NULL;
8296 }
8297#else
Bram Moolenaar8e481e82019-01-15 20:07:48 +01008298 *errormsg = _("E809: #< is not available without the +eval feature");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008299 return NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +00008300#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008301 }
8302 else
Bram Moolenaard812df62008-11-09 12:46:09 +00008303 {
Bram Moolenaarc312b8b2017-10-28 17:53:04 +02008304 if (i == 0 && src[1] == '<' && *usedlen > 1)
8305 *usedlen = 1;
Bram Moolenaard812df62008-11-09 12:46:09 +00008306 buf = buflist_findnr(i);
8307 if (buf == NULL)
8308 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008309 *errormsg = _("E194: No alternate file name to substitute for '#'");
Bram Moolenaard812df62008-11-09 12:46:09 +00008310 return NULL;
8311 }
8312 if (lnump != NULL)
8313 *lnump = ECMD_LAST;
8314 if (buf->b_fname == NULL)
8315 {
8316 result = (char_u *)"";
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008317 valid = 0; // Must have ":p:h" to be valid
Bram Moolenaard812df62008-11-09 12:46:09 +00008318 }
8319 else
Bram Moolenaar00136dc2018-07-25 21:19:13 +02008320 {
Bram Moolenaard812df62008-11-09 12:46:09 +00008321 result = buf->b_fname;
Bram Moolenaar00136dc2018-07-25 21:19:13 +02008322 tilde_file = STRCMP(result, "~") == 0;
8323 }
Bram Moolenaard812df62008-11-09 12:46:09 +00008324 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008325 break;
8326
8327#ifdef FEAT_SEARCHPATH
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008328 case SPEC_CFILE: // file name under cursor
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008329 result = file_name_at_cursor(FNAME_MESS|FNAME_HYP, 1L, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008330 if (result == NULL)
8331 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008332 *errormsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +00008333 return NULL;
8334 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008335 resultbuf = result; // remember allocated string
Bram Moolenaar071d4272004-06-13 20:20:40 +00008336 break;
8337#endif
8338
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008339 case SPEC_AFILE: // file name for autocommand
Bram Moolenaar071d4272004-06-13 20:20:40 +00008340 result = autocmd_fname;
Bram Moolenaarf6dad432008-09-18 19:29:58 +00008341 if (result != NULL && !autocmd_fname_full)
8342 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008343 // Still need to turn the fname into a full path. It is
8344 // postponed to avoid a delay when <afile> is not used.
Bram Moolenaarf6dad432008-09-18 19:29:58 +00008345 autocmd_fname_full = TRUE;
8346 result = FullName_save(autocmd_fname, FALSE);
8347 vim_free(autocmd_fname);
8348 autocmd_fname = result;
8349 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008350 if (result == NULL)
8351 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008352 *errormsg = _("E495: no autocommand file name to substitute for \"<afile>\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008353 return NULL;
8354 }
Bram Moolenaara0174af2008-01-02 20:08:25 +00008355 result = shorten_fname1(result);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008356 break;
8357
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008358 case SPEC_ABUF: // buffer number for autocommand
Bram Moolenaar071d4272004-06-13 20:20:40 +00008359 if (autocmd_bufnr <= 0)
8360 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008361 *errormsg = _("E496: no autocommand buffer number to substitute for \"<abuf>\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008362 return NULL;
8363 }
8364 sprintf((char *)strbuf, "%d", autocmd_bufnr);
8365 result = strbuf;
8366 break;
8367
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008368 case SPEC_AMATCH: // match name for autocommand
Bram Moolenaar071d4272004-06-13 20:20:40 +00008369 result = autocmd_match;
8370 if (result == NULL)
8371 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008372 *errormsg = _("E497: no autocommand match name to substitute for \"<amatch>\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008373 return NULL;
8374 }
8375 break;
8376
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008377 case SPEC_SFILE: // file name for ":so" command
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01008378 result = estack_sfile();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008379 if (result == NULL)
8380 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008381 *errormsg = _("E498: no :source file name to substitute for \"<sfile>\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008382 return NULL;
8383 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01008384 resultbuf = result; // remember allocated string
Bram Moolenaar071d4272004-06-13 20:20:40 +00008385 break;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008386
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008387 case SPEC_SLNUM: // line in file for ":so" command
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01008388 if (SOURCING_NAME == NULL || SOURCING_LNUM == 0)
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01008389 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008390 *errormsg = _("E842: no line number to use for \"<slnum>\"");
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01008391 return NULL;
8392 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01008393 sprintf((char *)strbuf, "%ld", SOURCING_LNUM);
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01008394 result = strbuf;
8395 break;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008396
8397#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008398 case SPEC_SFLNUM: // line in script file
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01008399 if (current_sctx.sc_lnum + SOURCING_LNUM == 0)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008400 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008401 *errormsg = _("E961: no line number to use for \"<sflnum>\"");
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008402 return NULL;
8403 }
8404 sprintf((char *)strbuf, "%ld",
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01008405 (long)(current_sctx.sc_lnum + SOURCING_LNUM));
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008406 result = strbuf;
8407 break;
8408#endif
8409
8410#ifdef FEAT_CLIENTSERVER
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008411 case SPEC_CLIENT: // Source of last submitted input
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008412 sprintf((char *)strbuf, PRINTF_HEX_LONG_U,
8413 (long_u)clientWindow);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008414 result = strbuf;
8415 break;
8416#endif
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008417
Bram Moolenaar9e0f6ec2017-05-16 09:36:54 +02008418 default:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008419 result = (char_u *)""; // avoid gcc warning
Bram Moolenaar9e0f6ec2017-05-16 09:36:54 +02008420 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008421 }
8422
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008423 resultlen = (int)STRLEN(result); // length of new string
8424 if (src[*usedlen] == '<') // remove the file name extension
Bram Moolenaar071d4272004-06-13 20:20:40 +00008425 {
8426 ++*usedlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008427 if ((s = vim_strrchr(result, '.')) != NULL && s >= gettail(result))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008428 resultlen = (int)(s - result);
8429 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008430 else if (!skip_mod)
8431 {
Bram Moolenaar00136dc2018-07-25 21:19:13 +02008432 valid |= modify_fname(src, tilde_file, usedlen, &result, &resultbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008433 &resultlen);
8434 if (result == NULL)
8435 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008436 *errormsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +00008437 return NULL;
8438 }
8439 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008440 }
8441
8442 if (resultlen == 0 || valid != VALID_HEAD + VALID_PATH)
8443 {
8444 if (valid != VALID_HEAD + VALID_PATH)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008445 // xgettext:no-c-format
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008446 *errormsg = _("E499: Empty file name for '%' or '#', only works with \":p:h\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008447 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008448 *errormsg = _("E500: Evaluates to an empty string");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008449 result = NULL;
8450 }
8451 else
8452 result = vim_strnsave(result, resultlen);
8453 vim_free(resultbuf);
8454 return result;
8455}
8456
8457/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008458 * Expand the <sfile> string in "arg".
8459 *
8460 * Returns an allocated string, or NULL for any error.
8461 */
8462 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008463expand_sfile(char_u *arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008464{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008465 char *errormsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008466 int len;
8467 char_u *result;
8468 char_u *newres;
8469 char_u *repl;
8470 int srclen;
8471 char_u *p;
8472
8473 result = vim_strsave(arg);
8474 if (result == NULL)
8475 return NULL;
8476
8477 for (p = result; *p; )
8478 {
8479 if (STRNCMP(p, "<sfile>", 7) != 0)
8480 ++p;
8481 else
8482 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008483 // replace "<sfile>" with the sourced file name, and do ":" stuff
Bram Moolenaar63b92542007-03-27 14:57:09 +00008484 repl = eval_vars(p, result, &srclen, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008485 if (errormsg != NULL)
8486 {
8487 if (*errormsg)
8488 emsg(errormsg);
8489 vim_free(result);
8490 return NULL;
8491 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008492 if (repl == NULL) // no match (cannot happen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008493 {
8494 p += srclen;
8495 continue;
8496 }
8497 len = (int)STRLEN(result) - srclen + (int)STRLEN(repl) + 1;
8498 newres = alloc(len);
8499 if (newres == NULL)
8500 {
8501 vim_free(repl);
8502 vim_free(result);
8503 return NULL;
8504 }
8505 mch_memmove(newres, result, (size_t)(p - result));
8506 STRCPY(newres + (p - result), repl);
8507 len = (int)STRLEN(newres);
8508 STRCAT(newres, p + srclen);
8509 vim_free(repl);
8510 vim_free(result);
8511 result = newres;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008512 p = newres + len; // continue after the match
Bram Moolenaar071d4272004-06-13 20:20:40 +00008513 }
8514 }
8515
8516 return result;
8517}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008518
Bram Moolenaar071d4272004-06-13 20:20:40 +00008519#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG) || defined(PROTO)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00008520/*
Bram Moolenaard9462e32011-04-11 21:35:11 +02008521 * Make a dialog message in "buff[DIALOG_MSG_SIZE]".
Bram Moolenaarb765d632005-06-07 21:00:02 +00008522 * "format" must contain "%s".
Bram Moolenaar9c13b352005-05-19 20:53:52 +00008523 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008524 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008525dialog_msg(char_u *buff, char *format, char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008526{
Bram Moolenaar071d4272004-06-13 20:20:40 +00008527 if (fname == NULL)
8528 fname = (char_u *)_("Untitled");
Bram Moolenaard9462e32011-04-11 21:35:11 +02008529 vim_snprintf((char *)buff, DIALOG_MSG_SIZE, format, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008530}
8531#endif
8532
8533/*
8534 * ":behave {mswin,xterm}"
8535 */
8536 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008537ex_behave(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008538{
8539 if (STRCMP(eap->arg, "mswin") == 0)
8540 {
8541 set_option_value((char_u *)"selection", 0L, (char_u *)"exclusive", 0);
8542 set_option_value((char_u *)"selectmode", 0L, (char_u *)"mouse,key", 0);
8543 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"popup", 0);
8544 set_option_value((char_u *)"keymodel", 0L,
8545 (char_u *)"startsel,stopsel", 0);
8546 }
8547 else if (STRCMP(eap->arg, "xterm") == 0)
8548 {
8549 set_option_value((char_u *)"selection", 0L, (char_u *)"inclusive", 0);
8550 set_option_value((char_u *)"selectmode", 0L, (char_u *)"", 0);
8551 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"extend", 0);
8552 set_option_value((char_u *)"keymodel", 0L, (char_u *)"", 0);
8553 }
8554 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008555 semsg(_(e_invarg2), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008556}
8557
Bram Moolenaar071d4272004-06-13 20:20:40 +00008558static int filetype_detect = FALSE;
8559static int filetype_plugin = FALSE;
8560static int filetype_indent = FALSE;
8561
8562/*
8563 * ":filetype [plugin] [indent] {on,off,detect}"
8564 * on: Load the filetype.vim file to install autocommands for file types.
8565 * off: Load the ftoff.vim file to remove all autocommands for file types.
8566 * plugin on: load filetype.vim and ftplugin.vim
8567 * plugin off: load ftplugof.vim
8568 * indent on: load filetype.vim and indent.vim
8569 * indent off: load indoff.vim
8570 */
8571 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008572ex_filetype(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008573{
8574 char_u *arg = eap->arg;
8575 int plugin = FALSE;
8576 int indent = FALSE;
8577
8578 if (*eap->arg == NUL)
8579 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008580 // Print current status.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008581 smsg("filetype detection:%s plugin:%s indent:%s",
Bram Moolenaar071d4272004-06-13 20:20:40 +00008582 filetype_detect ? "ON" : "OFF",
8583 filetype_plugin ? (filetype_detect ? "ON" : "(on)") : "OFF",
8584 filetype_indent ? (filetype_detect ? "ON" : "(on)") : "OFF");
8585 return;
8586 }
8587
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008588 // Accept "plugin" and "indent" in any order.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008589 for (;;)
8590 {
8591 if (STRNCMP(arg, "plugin", 6) == 0)
8592 {
8593 plugin = TRUE;
8594 arg = skipwhite(arg + 6);
8595 continue;
8596 }
8597 if (STRNCMP(arg, "indent", 6) == 0)
8598 {
8599 indent = TRUE;
8600 arg = skipwhite(arg + 6);
8601 continue;
8602 }
8603 break;
8604 }
8605 if (STRCMP(arg, "on") == 0 || STRCMP(arg, "detect") == 0)
8606 {
8607 if (*arg == 'o' || !filetype_detect)
8608 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01008609 source_runtime((char_u *)FILETYPE_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008610 filetype_detect = TRUE;
8611 if (plugin)
8612 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01008613 source_runtime((char_u *)FTPLUGIN_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008614 filetype_plugin = TRUE;
8615 }
8616 if (indent)
8617 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01008618 source_runtime((char_u *)INDENT_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008619 filetype_indent = TRUE;
8620 }
8621 }
8622 if (*arg == 'd')
8623 {
Bram Moolenaar1610d052016-06-09 22:53:01 +02008624 (void)do_doautocmd((char_u *)"filetypedetect BufRead", TRUE, NULL);
Bram Moolenaara3227e22006-03-08 21:32:40 +00008625 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008626 }
8627 }
8628 else if (STRCMP(arg, "off") == 0)
8629 {
8630 if (plugin || indent)
8631 {
8632 if (plugin)
8633 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01008634 source_runtime((char_u *)FTPLUGOF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008635 filetype_plugin = FALSE;
8636 }
8637 if (indent)
8638 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01008639 source_runtime((char_u *)INDOFF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008640 filetype_indent = FALSE;
8641 }
8642 }
8643 else
8644 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01008645 source_runtime((char_u *)FTOFF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008646 filetype_detect = FALSE;
8647 }
8648 }
8649 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008650 semsg(_(e_invarg2), arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008651}
8652
8653/*
Bram Moolenaar3e545692017-06-04 19:00:32 +02008654 * ":setfiletype [FALLBACK] {name}"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008655 */
8656 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008657ex_setfiletype(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008658{
8659 if (!did_filetype)
Bram Moolenaar3e545692017-06-04 19:00:32 +02008660 {
8661 char_u *arg = eap->arg;
8662
8663 if (STRNCMP(arg, "FALLBACK ", 9) == 0)
8664 arg += 9;
8665
8666 set_option_value((char_u *)"filetype", 0L, arg, OPT_LOCAL);
8667 if (arg != eap->arg)
8668 did_filetype = FALSE;
8669 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008670}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008671
Bram Moolenaar071d4272004-06-13 20:20:40 +00008672 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008673ex_digraphs(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008674{
8675#ifdef FEAT_DIGRAPHS
8676 if (*eap->arg != NUL)
8677 putdigraph(eap->arg);
8678 else
Bram Moolenaareae8ae12018-12-14 18:53:02 +01008679 listdigraphs(eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008680#else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008681 emsg(_("E196: No digraphs in this version"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008682#endif
8683}
8684
Bram Moolenaar451fc7b2018-04-27 22:53:07 +02008685#if defined(FEAT_SEARCH_EXTRA) || defined(PROTO)
8686 void
8687set_no_hlsearch(int flag)
8688{
8689 no_hlsearch = flag;
8690# ifdef FEAT_EVAL
8691 set_vim_var_nr(VV_HLSEARCH, !no_hlsearch && p_hls);
8692# endif
8693}
8694
Bram Moolenaar071d4272004-06-13 20:20:40 +00008695/*
8696 * ":nohlsearch"
8697 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008698 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008699ex_nohlsearch(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008700{
Bram Moolenaar451fc7b2018-04-27 22:53:07 +02008701 set_no_hlsearch(TRUE);
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00008702 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008703}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008704#endif
8705
8706#ifdef FEAT_CRYPT
8707/*
8708 * ":X": Get crypt key
8709 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008710 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008711ex_X(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008712{
Bram Moolenaar3a0c9082014-11-12 15:15:42 +01008713 crypt_check_current_method();
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02008714 (void)crypt_get_key(TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008715}
8716#endif
8717
8718#ifdef FEAT_FOLDING
8719 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008720ex_fold(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008721{
8722 if (foldManualAllowed(TRUE))
8723 foldCreate(eap->line1, eap->line2);
8724}
8725
8726 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008727ex_foldopen(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008728{
8729 opFoldRange(eap->line1, eap->line2, eap->cmdidx == CMD_foldopen,
8730 eap->forceit, FALSE);
8731}
8732
8733 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008734ex_folddo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008735{
8736 linenr_T lnum;
8737
Bram Moolenaar4facea32019-10-12 20:17:40 +02008738# ifdef FEAT_CLIPBOARD
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02008739 start_global_changes();
Bram Moolenaar4facea32019-10-12 20:17:40 +02008740# endif
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02008741
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008742 // First set the marks for all lines closed/open.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008743 for (lnum = eap->line1; lnum <= eap->line2; ++lnum)
8744 if (hasFolding(lnum, NULL, NULL) == (eap->cmdidx == CMD_folddoclosed))
8745 ml_setmarked(lnum);
8746
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008747 // Execute the command on the marked lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008748 global_exe(eap->arg);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008749 ml_clearmarked(); // clear rest of the marks
Bram Moolenaar4facea32019-10-12 20:17:40 +02008750# ifdef FEAT_CLIPBOARD
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02008751 end_global_changes();
Bram Moolenaar4facea32019-10-12 20:17:40 +02008752# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008753}
8754#endif
Bram Moolenaarf5291f32017-09-14 22:55:37 +02008755
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008756#if defined(FEAT_QUICKFIX) || defined(PROTO)
Bram Moolenaar39665952018-08-15 20:59:48 +02008757/*
8758 * Returns TRUE if the supplied Ex cmdidx is for a location list command
8759 * instead of a quickfix command.
8760 */
8761 int
8762is_loclist_cmd(int cmdidx)
8763{
Bram Moolenaar74c8be22018-08-23 22:51:40 +02008764 if (cmdidx < 0 || cmdidx >= CMD_SIZE)
Bram Moolenaar39665952018-08-15 20:59:48 +02008765 return FALSE;
8766 return cmdnames[cmdidx].cmd_name[0] == 'l';
8767}
8768#endif
8769
Bram Moolenaar4facea32019-10-12 20:17:40 +02008770#if defined(FEAT_TIMERS) || defined(PROTO)
Bram Moolenaarf5291f32017-09-14 22:55:37 +02008771 int
8772get_pressedreturn(void)
8773{
8774 return ex_pressedreturn;
8775}
8776
8777 void
8778set_pressedreturn(int val)
8779{
8780 ex_pressedreturn = val;
8781}
8782#endif