blob: 4755a01b48f98085a22555fc975673bfeb4b88aa [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;
2612#endif
2613
2614 return ea.nextcmd;
2615}
2616#if (_MSC_VER == 1200)
Bram Moolenaar281bdce2005-01-25 21:53:18 +00002617 #pragma optimize( "", on )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002618#endif
2619
2620/*
Bram Moolenaareffed932018-08-14 13:38:17 +02002621 * Parse and skip over command modifiers:
2622 * - update eap->cmd
2623 * - store flags in "cmdmod".
2624 * - Set ex_pressedreturn for an empty command line.
2625 * - set msg_silent for ":silent"
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002626 * - set 'eventignore' to "all" for ":noautocmd"
Bram Moolenaareffed932018-08-14 13:38:17 +02002627 * - set p_verbose for ":verbose"
2628 * - Increment "sandbox" for ":sandbox"
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002629 * When "skip_only" is TRUE the global variables are not changed, except for
2630 * "cmdmod".
Bram Moolenaareffed932018-08-14 13:38:17 +02002631 * Return FAIL when the command is not to be executed.
2632 * May set "errormsg" to an error message.
2633 */
2634 int
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002635parse_command_modifiers(exarg_T *eap, char **errormsg, int skip_only)
Bram Moolenaareffed932018-08-14 13:38:17 +02002636{
2637 char_u *p;
2638
Bram Moolenaara80faa82020-04-12 19:37:17 +02002639 CLEAR_FIELD(cmdmod);
Bram Moolenaareffed932018-08-14 13:38:17 +02002640 eap->verbose_save = -1;
2641 eap->save_msg_silent = -1;
2642
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002643 // Repeat until no more command modifiers are found.
Bram Moolenaareffed932018-08-14 13:38:17 +02002644 for (;;)
2645 {
Bram Moolenaareffed932018-08-14 13:38:17 +02002646 while (*eap->cmd == ' ' || *eap->cmd == '\t' || *eap->cmd == ':')
2647 ++eap->cmd;
2648
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002649 // in ex mode, an empty line works like :+
Bram Moolenaareffed932018-08-14 13:38:17 +02002650 if (*eap->cmd == NUL && exmode_active
2651 && (getline_equal(eap->getline, eap->cookie, getexmodeline)
2652 || getline_equal(eap->getline, eap->cookie, getexline))
2653 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
2654 {
2655 eap->cmd = (char_u *)"+";
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002656 if (!skip_only)
2657 ex_pressedreturn = TRUE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002658 }
2659
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002660 // ignore comment and empty lines
Bram Moolenaareffed932018-08-14 13:38:17 +02002661 if (*eap->cmd == '"')
2662 return FAIL;
2663 if (*eap->cmd == NUL)
2664 {
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002665 if (!skip_only)
2666 ex_pressedreturn = TRUE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002667 return FAIL;
2668 }
2669
Bram Moolenaareffed932018-08-14 13:38:17 +02002670 p = skip_range(eap->cmd, NULL);
2671 switch (*p)
2672 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002673 // When adding an entry, also modify cmd_exists().
Bram Moolenaareffed932018-08-14 13:38:17 +02002674 case 'a': if (!checkforcmd(&eap->cmd, "aboveleft", 3))
2675 break;
2676 cmdmod.split |= WSP_ABOVE;
2677 continue;
2678
2679 case 'b': if (checkforcmd(&eap->cmd, "belowright", 3))
2680 {
2681 cmdmod.split |= WSP_BELOW;
2682 continue;
2683 }
2684 if (checkforcmd(&eap->cmd, "browse", 3))
2685 {
2686#ifdef FEAT_BROWSE_CMD
2687 cmdmod.browse = TRUE;
2688#endif
2689 continue;
2690 }
2691 if (!checkforcmd(&eap->cmd, "botright", 2))
2692 break;
2693 cmdmod.split |= WSP_BOT;
2694 continue;
2695
2696 case 'c': if (!checkforcmd(&eap->cmd, "confirm", 4))
2697 break;
2698#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
2699 cmdmod.confirm = TRUE;
2700#endif
2701 continue;
2702
2703 case 'k': if (checkforcmd(&eap->cmd, "keepmarks", 3))
2704 {
2705 cmdmod.keepmarks = TRUE;
2706 continue;
2707 }
2708 if (checkforcmd(&eap->cmd, "keepalt", 5))
2709 {
2710 cmdmod.keepalt = TRUE;
2711 continue;
2712 }
2713 if (checkforcmd(&eap->cmd, "keeppatterns", 5))
2714 {
2715 cmdmod.keeppatterns = TRUE;
2716 continue;
2717 }
2718 if (!checkforcmd(&eap->cmd, "keepjumps", 5))
2719 break;
2720 cmdmod.keepjumps = TRUE;
2721 continue;
2722
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002723 case 'f': // only accept ":filter {pat} cmd"
Bram Moolenaareffed932018-08-14 13:38:17 +02002724 {
2725 char_u *reg_pat;
2726
2727 if (!checkforcmd(&p, "filter", 4)
2728 || *p == NUL || ends_excmd(*p))
2729 break;
2730 if (*p == '!')
2731 {
2732 cmdmod.filter_force = TRUE;
2733 p = skipwhite(p + 1);
2734 if (*p == NUL || ends_excmd(*p))
2735 break;
2736 }
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002737 if (skip_only)
2738 p = skip_vimgrep_pat(p, NULL, NULL);
2739 else
2740 // NOTE: This puts a NUL after the pattern.
2741 p = skip_vimgrep_pat(p, &reg_pat, NULL);
Bram Moolenaareffed932018-08-14 13:38:17 +02002742 if (p == NULL || *p == NUL)
2743 break;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002744 if (!skip_only)
2745 {
2746 cmdmod.filter_regmatch.regprog =
Bram Moolenaareffed932018-08-14 13:38:17 +02002747 vim_regcomp(reg_pat, RE_MAGIC);
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002748 if (cmdmod.filter_regmatch.regprog == NULL)
2749 break;
2750 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002751 eap->cmd = p;
2752 continue;
2753 }
2754
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002755 // ":hide" and ":hide | cmd" are not modifiers
Bram Moolenaareffed932018-08-14 13:38:17 +02002756 case 'h': if (p != eap->cmd || !checkforcmd(&p, "hide", 3)
2757 || *p == NUL || ends_excmd(*p))
2758 break;
2759 eap->cmd = p;
2760 cmdmod.hide = TRUE;
2761 continue;
2762
2763 case 'l': if (checkforcmd(&eap->cmd, "lockmarks", 3))
2764 {
2765 cmdmod.lockmarks = TRUE;
2766 continue;
2767 }
2768
2769 if (!checkforcmd(&eap->cmd, "leftabove", 5))
2770 break;
2771 cmdmod.split |= WSP_ABOVE;
2772 continue;
2773
2774 case 'n': if (checkforcmd(&eap->cmd, "noautocmd", 3))
2775 {
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002776 if (cmdmod.save_ei == NULL && !skip_only)
Bram Moolenaareffed932018-08-14 13:38:17 +02002777 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002778 // Set 'eventignore' to "all". Restore the
2779 // existing option value later.
Bram Moolenaareffed932018-08-14 13:38:17 +02002780 cmdmod.save_ei = vim_strsave(p_ei);
2781 set_string_option_direct((char_u *)"ei", -1,
2782 (char_u *)"all", OPT_FREE, SID_NONE);
2783 }
2784 continue;
2785 }
2786 if (!checkforcmd(&eap->cmd, "noswapfile", 3))
2787 break;
2788 cmdmod.noswapfile = TRUE;
2789 continue;
2790
2791 case 'r': if (!checkforcmd(&eap->cmd, "rightbelow", 6))
2792 break;
2793 cmdmod.split |= WSP_BELOW;
2794 continue;
2795
2796 case 's': if (checkforcmd(&eap->cmd, "sandbox", 3))
2797 {
2798#ifdef HAVE_SANDBOX
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002799 if (!skip_only)
2800 {
2801 if (!eap->did_sandbox)
2802 ++sandbox;
2803 eap->did_sandbox = TRUE;
2804 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002805#endif
2806 continue;
2807 }
2808 if (!checkforcmd(&eap->cmd, "silent", 3))
2809 break;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002810 if (!skip_only)
2811 {
2812 if (eap->save_msg_silent == -1)
2813 eap->save_msg_silent = msg_silent;
2814 ++msg_silent;
2815 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002816 if (*eap->cmd == '!' && !VIM_ISWHITE(eap->cmd[-1]))
2817 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002818 // ":silent!", but not "silent !cmd"
Bram Moolenaareffed932018-08-14 13:38:17 +02002819 eap->cmd = skipwhite(eap->cmd + 1);
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002820 if (!skip_only)
2821 {
2822 ++emsg_silent;
2823 ++eap->did_esilent;
2824 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002825 }
2826 continue;
2827
2828 case 't': if (checkforcmd(&p, "tab", 3))
2829 {
Bram Moolenaar548e5982018-12-26 21:45:00 +01002830 if (!skip_only)
Bram Moolenaareffed932018-08-14 13:38:17 +02002831 {
Bram Moolenaar548e5982018-12-26 21:45:00 +01002832 long tabnr = get_address(eap, &eap->cmd,
2833 ADDR_TABS, eap->skip,
2834 skip_only, FALSE, 1);
2835 if (tabnr == MAXLNUM)
2836 cmdmod.tab = tabpage_index(curtab) + 1;
2837 else
Bram Moolenaareffed932018-08-14 13:38:17 +02002838 {
Bram Moolenaar548e5982018-12-26 21:45:00 +01002839 if (tabnr < 0 || tabnr > LAST_TAB_NR)
2840 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002841 *errormsg = _(e_invrange);
Bram Moolenaar548e5982018-12-26 21:45:00 +01002842 return FAIL;
2843 }
2844 cmdmod.tab = tabnr + 1;
Bram Moolenaareffed932018-08-14 13:38:17 +02002845 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002846 }
2847 eap->cmd = p;
2848 continue;
2849 }
2850 if (!checkforcmd(&eap->cmd, "topleft", 2))
2851 break;
2852 cmdmod.split |= WSP_TOP;
2853 continue;
2854
2855 case 'u': if (!checkforcmd(&eap->cmd, "unsilent", 3))
2856 break;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002857 if (!skip_only)
2858 {
2859 if (eap->save_msg_silent == -1)
2860 eap->save_msg_silent = msg_silent;
2861 msg_silent = 0;
2862 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002863 continue;
2864
2865 case 'v': if (checkforcmd(&eap->cmd, "vertical", 4))
2866 {
2867 cmdmod.split |= WSP_VERT;
2868 continue;
2869 }
2870 if (!checkforcmd(&p, "verbose", 4))
2871 break;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002872 if (!skip_only)
2873 {
2874 if (eap->verbose_save < 0)
2875 eap->verbose_save = p_verbose;
2876 if (vim_isdigit(*eap->cmd))
2877 p_verbose = atoi((char *)eap->cmd);
2878 else
2879 p_verbose = 1;
2880 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002881 eap->cmd = p;
2882 continue;
2883 }
2884 break;
2885 }
2886
2887 return OK;
2888}
2889
2890/*
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002891 * Free contents of "cmdmod".
2892 */
2893 static void
2894free_cmdmod(void)
2895{
2896 if (cmdmod.save_ei != NULL)
2897 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002898 // Restore 'eventignore' to the value before ":noautocmd".
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002899 set_string_option_direct((char_u *)"ei", -1, cmdmod.save_ei,
2900 OPT_FREE, SID_NONE);
2901 free_string_option(cmdmod.save_ei);
2902 }
2903
2904 if (cmdmod.filter_regmatch.regprog != NULL)
2905 vim_regfree(cmdmod.filter_regmatch.regprog);
2906}
2907
2908/*
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002909 * Parse the address range, if any, in "eap".
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02002910 * May set the last search pattern, unless "silent" is TRUE.
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002911 * Return FAIL and set "errormsg" or return OK.
2912 */
2913 int
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002914parse_cmd_address(exarg_T *eap, char **errormsg, int silent)
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002915{
2916 int address_count = 1;
2917 linenr_T lnum;
2918
2919 // Repeat for all ',' or ';' separated addresses.
2920 for (;;)
2921 {
2922 eap->line1 = eap->line2;
2923 switch (eap->addr_type)
2924 {
2925 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02002926 case ADDR_OTHER:
Bram Moolenaarb5383b12020-05-18 19:46:48 +02002927 // Default is the cursor line number. Avoid using an invalid
2928 // line number though.
2929 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
2930 eap->line2 = curbuf->b_ml.ml_line_count;
2931 else
2932 eap->line2 = curwin->w_cursor.lnum;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002933 break;
2934 case ADDR_WINDOWS:
2935 eap->line2 = CURRENT_WIN_NR;
2936 break;
2937 case ADDR_ARGUMENTS:
2938 eap->line2 = curwin->w_arg_idx + 1;
2939 if (eap->line2 > ARGCOUNT)
2940 eap->line2 = ARGCOUNT;
2941 break;
2942 case ADDR_LOADED_BUFFERS:
2943 case ADDR_BUFFERS:
2944 eap->line2 = curbuf->b_fnum;
2945 break;
2946 case ADDR_TABS:
2947 eap->line2 = CURRENT_TAB_NR;
2948 break;
2949 case ADDR_TABS_RELATIVE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02002950 case ADDR_UNSIGNED:
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002951 eap->line2 = 1;
2952 break;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002953 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02002954#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02002955 eap->line2 = qf_get_cur_idx(eap);
2956#endif
2957 break;
2958 case ADDR_QUICKFIX_VALID:
2959#ifdef FEAT_QUICKFIX
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002960 eap->line2 = qf_get_cur_valid_idx(eap);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002961#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02002962 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02002963 case ADDR_NONE:
2964 // Will give an error later if a range is found.
2965 break;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002966 }
2967 eap->cmd = skipwhite(eap->cmd);
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02002968 lnum = get_address(eap, &eap->cmd, eap->addr_type, eap->skip, silent,
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002969 eap->addr_count == 0, address_count++);
2970 if (eap->cmd == NULL) // error detected
2971 return FAIL;
2972 if (lnum == MAXLNUM)
2973 {
2974 if (*eap->cmd == '%') // '%' - all lines
2975 {
2976 ++eap->cmd;
2977 switch (eap->addr_type)
2978 {
2979 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02002980 case ADDR_OTHER:
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002981 eap->line1 = 1;
2982 eap->line2 = curbuf->b_ml.ml_line_count;
2983 break;
2984 case ADDR_LOADED_BUFFERS:
2985 {
2986 buf_T *buf = firstbuf;
2987
2988 while (buf->b_next != NULL
2989 && buf->b_ml.ml_mfp == NULL)
2990 buf = buf->b_next;
2991 eap->line1 = buf->b_fnum;
2992 buf = lastbuf;
2993 while (buf->b_prev != NULL
2994 && buf->b_ml.ml_mfp == NULL)
2995 buf = buf->b_prev;
2996 eap->line2 = buf->b_fnum;
2997 break;
2998 }
2999 case ADDR_BUFFERS:
3000 eap->line1 = firstbuf->b_fnum;
3001 eap->line2 = lastbuf->b_fnum;
3002 break;
3003 case ADDR_WINDOWS:
3004 case ADDR_TABS:
3005 if (IS_USER_CMDIDX(eap->cmdidx))
3006 {
3007 eap->line1 = 1;
3008 eap->line2 = eap->addr_type == ADDR_WINDOWS
3009 ? LAST_WIN_NR : LAST_TAB_NR;
3010 }
3011 else
3012 {
3013 // there is no Vim command which uses '%' and
3014 // ADDR_WINDOWS or ADDR_TABS
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003015 *errormsg = _(e_invrange);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003016 return FAIL;
3017 }
3018 break;
3019 case ADDR_TABS_RELATIVE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02003020 case ADDR_UNSIGNED:
3021 case ADDR_QUICKFIX:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003022 *errormsg = _(e_invrange);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003023 return FAIL;
3024 case ADDR_ARGUMENTS:
3025 if (ARGCOUNT == 0)
3026 eap->line1 = eap->line2 = 0;
3027 else
3028 {
3029 eap->line1 = 1;
3030 eap->line2 = ARGCOUNT;
3031 }
3032 break;
Bram Moolenaar25190db2019-05-04 15:05:28 +02003033 case ADDR_QUICKFIX_VALID:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003034#ifdef FEAT_QUICKFIX
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003035 eap->line1 = 1;
Bram Moolenaar25190db2019-05-04 15:05:28 +02003036 eap->line2 = qf_get_valid_size(eap);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003037 if (eap->line2 == 0)
3038 eap->line2 = 1;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003039#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003040 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02003041 case ADDR_NONE:
3042 // Will give an error later if a range is found.
3043 break;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003044 }
3045 ++eap->addr_count;
3046 }
3047 else if (*eap->cmd == '*' && vim_strchr(p_cpo, CPO_STAR) == NULL)
3048 {
3049 pos_T *fp;
3050
3051 // '*' - visual area
3052 if (eap->addr_type != ADDR_LINES)
3053 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003054 *errormsg = _(e_invrange);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003055 return FAIL;
3056 }
3057
3058 ++eap->cmd;
3059 if (!eap->skip)
3060 {
3061 fp = getmark('<', FALSE);
3062 if (check_mark(fp) == FAIL)
3063 return FAIL;
3064 eap->line1 = fp->lnum;
3065 fp = getmark('>', FALSE);
3066 if (check_mark(fp) == FAIL)
3067 return FAIL;
3068 eap->line2 = fp->lnum;
3069 ++eap->addr_count;
3070 }
3071 }
3072 }
3073 else
3074 eap->line2 = lnum;
3075 eap->addr_count++;
3076
3077 if (*eap->cmd == ';')
3078 {
3079 if (!eap->skip)
3080 {
3081 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaar0e717042020-04-27 19:29:01 +02003082 // Don't leave the cursor on an illegal line or column, but do
3083 // accept zero as address, so 0;/PATTERN/ works correctly.
3084 if (eap->line2 > 0)
3085 check_cursor();
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003086 }
3087 }
3088 else if (*eap->cmd != ',')
3089 break;
3090 ++eap->cmd;
3091 }
3092
3093 // One address given: set start and end lines.
3094 if (eap->addr_count == 1)
3095 {
3096 eap->line1 = eap->line2;
3097 // ... but only implicit: really no address given
3098 if (lnum == MAXLNUM)
3099 eap->addr_count = 0;
3100 }
3101 return OK;
3102}
3103
3104/*
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00003105 * Check for an Ex command with optional tail.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003106 * If there is a match advance "pp" to the argument and return TRUE.
3107 */
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00003108 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003109checkforcmd(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003110 char_u **pp, // start of command
3111 char *cmd, // name of command
3112 int len) // required length
Bram Moolenaar071d4272004-06-13 20:20:40 +00003113{
3114 int i;
3115
3116 for (i = 0; cmd[i] != NUL; ++i)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003117 if (((char_u *)cmd)[i] != (*pp)[i])
Bram Moolenaar071d4272004-06-13 20:20:40 +00003118 break;
3119 if (i >= len && !isalpha((*pp)[i]))
3120 {
3121 *pp = skipwhite(*pp + i);
3122 return TRUE;
3123 }
3124 return FALSE;
3125}
3126
3127/*
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003128 * Append "cmd" to the error message in IObuff.
3129 * Takes care of limiting the length and handling 0xa0, which would be
3130 * invisible otherwise.
3131 */
3132 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003133append_command(char_u *cmd)
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003134{
3135 char_u *s = cmd;
3136 char_u *d;
3137
3138 STRCAT(IObuff, ": ");
3139 d = IObuff + STRLEN(IObuff);
3140 while (*s != NUL && d - IObuff < IOSIZE - 7)
3141 {
Bram Moolenaar13505972019-01-24 15:04:48 +01003142 if (enc_utf8 ? (s[0] == 0xc2 && s[1] == 0xa0) : *s == 0xa0)
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003143 {
Bram Moolenaar13505972019-01-24 15:04:48 +01003144 s += enc_utf8 ? 2 : 1;
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003145 STRCPY(d, "<a0>");
3146 d += 4;
3147 }
3148 else
3149 MB_COPY_CHAR(s, d);
3150 }
3151 *d = NUL;
3152}
3153
3154/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003155 * Find an Ex command by its name, either built-in or user.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003156 * Start of the name can be found at eap->cmd.
Bram Moolenaar25190db2019-05-04 15:05:28 +02003157 * Sets eap->cmdidx and returns a pointer to char after the command name.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003158 * "full" is set to TRUE if the whole command name matched.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003159 *
3160 * If "lookup" is not NULL recognize expression without "eval" or "call" and
3161 * assignment without "let". Sets eap->cmdidx to the command while returning
3162 * "eap->cmd".
3163 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00003164 * Returns NULL for an ambiguous user command.
3165 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003166 char_u *
3167find_ex_command(
3168 exarg_T *eap,
Bram Moolenaara494f562020-04-18 17:45:38 +02003169 int *full UNUSED,
Bram Moolenaarb84a3812020-05-01 15:44:29 +02003170 void *(*lookup)(char_u *, size_t, cctx_T *) UNUSED,
Bram Moolenaara494f562020-04-18 17:45:38 +02003171 cctx_T *cctx UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003172{
3173 int len;
3174 char_u *p;
Bram Moolenaardf177f62005-02-22 08:39:57 +00003175 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003176
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003177#ifdef FEAT_EVAL
3178 /*
3179 * Recognize a Vim9 script function/method call and assignment:
3180 * "lvar = value", "lvar(arg)", "[1, 2 3]->Func()"
3181 */
Bram Moolenaar0c6ceaf2020-02-22 18:36:32 +01003182 p = eap->cmd;
3183 if (lookup != NULL && (*p == '('
3184 || ((p = to_name_const_end(eap->cmd)) > eap->cmd && *p != NUL)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003185 {
3186 int oplen;
3187 int heredoc;
3188
3189 // "funcname(" is always a function call.
3190 // "varname[]" is an expression.
3191 // "g:varname" is an expression.
3192 // "varname->expr" is an expression.
Bram Moolenaar0c6ceaf2020-02-22 18:36:32 +01003193 // "(..." is an expression.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003194 if (*p == '('
3195 || *p == '['
3196 || p[1] == ':'
3197 || (*p == '-' && p[1] == '>'))
3198 {
3199 eap->cmdidx = CMD_eval;
3200 return eap->cmd;
3201 }
3202
3203 oplen = assignment_len(skipwhite(p), &heredoc);
3204 if (oplen > 0)
3205 {
3206 // Recognize an assignment if we recognize the variable name:
3207 // "g:var = expr"
3208 // "var = expr" where "var" is a local var name.
3209 if (((p - eap->cmd) > 2 && eap->cmd[1] == ':')
Bram Moolenaarb84a3812020-05-01 15:44:29 +02003210 || lookup(eap->cmd, p - eap->cmd, cctx) != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003211 {
3212 eap->cmdidx = CMD_let;
3213 return eap->cmd;
3214 }
3215 }
3216 }
3217#endif
3218
Bram Moolenaar071d4272004-06-13 20:20:40 +00003219 /*
3220 * Isolate the command and search for it in the command table.
Bram Moolenaar81870892007-11-11 18:17:28 +00003221 * Exceptions:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003222 * - the 'k' command can directly be followed by any character.
3223 * - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003224 * but :sre[wind] is another command, as are :scr[iptnames],
Bram Moolenaar071d4272004-06-13 20:20:40 +00003225 * :scs[cope], :sim[alt], :sig[ns] and :sil[ent].
Bram Moolenaardf177f62005-02-22 08:39:57 +00003226 * - the "d" command can directly be followed by 'l' or 'p' flag.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003227 */
3228 p = eap->cmd;
3229 if (*p == 'k')
3230 {
3231 eap->cmdidx = CMD_k;
3232 ++p;
3233 }
3234 else if (p[0] == 's'
Bram Moolenaar204b93f2015-08-04 22:02:51 +02003235 && ((p[1] == 'c' && (p[2] == NUL || (p[2] != 's' && p[2] != 'r'
3236 && (p[3] == NUL || (p[3] != 'i' && p[4] != 'p')))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003237 || p[1] == 'g'
3238 || (p[1] == 'i' && p[2] != 'm' && p[2] != 'l' && p[2] != 'g')
3239 || p[1] == 'I'
3240 || (p[1] == 'r' && p[2] != 'e')))
3241 {
3242 eap->cmdidx = CMD_substitute;
3243 ++p;
3244 }
3245 else
3246 {
3247 while (ASCII_ISALPHA(*p))
3248 ++p;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003249 // for python 3.x support ":py3", ":python3", ":py3file", etc.
Bram Moolenaar55d5c032010-07-17 23:52:29 +02003250 if (eap->cmd[0] == 'p' && eap->cmd[1] == 'y')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003251 {
Bram Moolenaarb6590522010-07-21 16:00:43 +02003252 while (ASCII_ISALNUM(*p))
3253 ++p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003254 }
3255 else if (*p == '9' && STRNCMP("vim9", eap->cmd, 4) == 0)
3256 {
3257 // include "9" for "vim9script"
3258 ++p;
3259 while (ASCII_ISALPHA(*p))
3260 ++p;
3261 }
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02003262
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003263 // check for non-alpha command
Bram Moolenaar071d4272004-06-13 20:20:40 +00003264 if (p == eap->cmd && vim_strchr((char_u *)"@*!=><&~#", *p) != NULL)
3265 ++p;
3266 len = (int)(p - eap->cmd);
Bram Moolenaardf177f62005-02-22 08:39:57 +00003267 if (*eap->cmd == 'd' && (p[-1] == 'l' || p[-1] == 'p'))
3268 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003269 // Check for ":dl", ":dell", etc. to ":deletel": that's
3270 // :delete with the 'l' flag. Same for 'p'.
Bram Moolenaardf177f62005-02-22 08:39:57 +00003271 for (i = 0; i < len; ++i)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003272 if (eap->cmd[i] != ((char_u *)"delete")[i])
Bram Moolenaardf177f62005-02-22 08:39:57 +00003273 break;
3274 if (i == len - 1)
3275 {
3276 --len;
3277 if (p[-1] == 'l')
3278 eap->flags |= EXFLAG_LIST;
3279 else
3280 eap->flags |= EXFLAG_PRINT;
3281 }
3282 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003283
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003284 if (ASCII_ISLOWER(eap->cmd[0]))
3285 {
Bram Moolenaar6c0c1e82017-03-25 15:07:43 +01003286 int c1 = eap->cmd[0];
Bram Moolenaar94f82cb2019-07-24 22:30:27 +02003287 int c2 = len == 1 ? NUL : eap->cmd[1];
Bram Moolenaar6c0c1e82017-03-25 15:07:43 +01003288
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003289 if (command_count != (int)CMD_SIZE)
3290 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003291 iemsg(_("E943: Command table needs to be updated, run 'make cmdidxs'"));
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003292 getout(1);
3293 }
3294
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003295 // Use a precomputed index for fast look-up in cmdnames[]
3296 // taking into account the first 2 letters of eap->cmd.
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003297 eap->cmdidx = cmdidxs1[CharOrdLow(c1)];
3298 if (ASCII_ISLOWER(c2))
3299 eap->cmdidx += cmdidxs2[CharOrdLow(c1)][CharOrdLow(c2)];
3300 }
Bram Moolenaara494f562020-04-18 17:45:38 +02003301 else if (ASCII_ISUPPER(eap->cmd[0]))
3302 eap->cmdidx = CMD_Next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003303 else
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003304 eap->cmdidx = CMD_bang;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003305
3306 for ( ; (int)eap->cmdidx < (int)CMD_SIZE;
3307 eap->cmdidx = (cmdidx_T)((int)eap->cmdidx + 1))
3308 if (STRNCMP(cmdnames[(int)eap->cmdidx].cmd_name, (char *)eap->cmd,
3309 (size_t)len) == 0)
3310 {
3311#ifdef FEAT_EVAL
3312 if (full != NULL
3313 && cmdnames[(int)eap->cmdidx].cmd_name[len] == NUL)
3314 *full = TRUE;
3315#endif
3316 break;
3317 }
3318
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003319 // Look for a user defined command as a last resort. Let ":Print" be
3320 // overruled by a user defined command.
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01003321 if ((eap->cmdidx == CMD_SIZE || eap->cmdidx == CMD_Print)
3322 && *eap->cmd >= 'A' && *eap->cmd <= 'Z')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003323 {
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003324 // User defined commands may contain digits.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003325 while (ASCII_ISALNUM(*p))
3326 ++p;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003327 p = find_ucmd(eap, p, full, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003328 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003329 if (p == eap->cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003330 eap->cmdidx = CMD_SIZE;
3331 }
3332
3333 return p;
3334}
3335
3336#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003337static struct cmdmod
3338{
3339 char *name;
3340 int minlen;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003341 int has_count; // :123verbose :3tab
Bram Moolenaared53fb92007-11-24 20:50:24 +00003342} cmdmods[] = {
3343 {"aboveleft", 3, FALSE},
3344 {"belowright", 3, FALSE},
3345 {"botright", 2, FALSE},
3346 {"browse", 3, FALSE},
3347 {"confirm", 4, FALSE},
Bram Moolenaar7b668e82016-08-23 23:51:21 +02003348 {"filter", 4, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003349 {"hide", 3, FALSE},
3350 {"keepalt", 5, FALSE},
3351 {"keepjumps", 5, FALSE},
3352 {"keepmarks", 3, FALSE},
Bram Moolenaara939e432013-11-09 05:30:26 +01003353 {"keeppatterns", 5, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003354 {"leftabove", 5, FALSE},
3355 {"lockmarks", 3, FALSE},
Bram Moolenaarca9f9582008-09-18 10:44:28 +00003356 {"noautocmd", 3, FALSE},
Bram Moolenaar5803ae62014-03-23 16:04:02 +01003357 {"noswapfile", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003358 {"rightbelow", 6, FALSE},
3359 {"sandbox", 3, FALSE},
3360 {"silent", 3, FALSE},
3361 {"tab", 3, TRUE},
3362 {"topleft", 2, FALSE},
Bram Moolenaar8e258a42009-07-09 13:55:43 +00003363 {"unsilent", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003364 {"verbose", 4, TRUE},
3365 {"vertical", 4, FALSE},
3366};
3367
3368/*
3369 * Return length of a command modifier (including optional count).
3370 * Return zero when it's not a modifier.
3371 */
3372 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003373modifier_len(char_u *cmd)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003374{
3375 int i, j;
3376 char_u *p = cmd;
3377
3378 if (VIM_ISDIGIT(*cmd))
3379 p = skipwhite(skipdigits(cmd));
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00003380 for (i = 0; i < (int)(sizeof(cmdmods) / sizeof(struct cmdmod)); ++i)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003381 {
3382 for (j = 0; p[j] != NUL; ++j)
3383 if (p[j] != cmdmods[i].name[j])
3384 break;
Bram Moolenaar2d473ab2013-06-12 17:12:24 +02003385 if (!ASCII_ISALPHA(p[j]) && j >= cmdmods[i].minlen
Bram Moolenaared53fb92007-11-24 20:50:24 +00003386 && (p == cmd || cmdmods[i].has_count))
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00003387 return j + (int)(p - cmd);
Bram Moolenaared53fb92007-11-24 20:50:24 +00003388 }
3389 return 0;
3390}
3391
Bram Moolenaar071d4272004-06-13 20:20:40 +00003392/*
3393 * Return > 0 if an Ex command "name" exists.
3394 * Return 2 if there is an exact match.
3395 * Return 3 if there is an ambiguous match.
3396 */
3397 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003398cmd_exists(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003399{
3400 exarg_T ea;
3401 int full = FALSE;
3402 int i;
3403 int j;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003404 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003405
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003406 // Check command modifiers.
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00003407 for (i = 0; i < (int)(sizeof(cmdmods) / sizeof(struct cmdmod)); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003408 {
3409 for (j = 0; name[j] != NUL; ++j)
3410 if (name[j] != cmdmods[i].name[j])
3411 break;
3412 if (name[j] == NUL && j >= cmdmods[i].minlen)
3413 return (cmdmods[i].name[j] == NUL ? 2 : 1);
3414 }
3415
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003416 // Check built-in commands and user defined commands.
3417 // For ":2match" and ":3match" we need to skip the number.
Bram Moolenaara9587612006-05-04 21:47:50 +00003418 ea.cmd = (*name == '2' || *name == '3') ? name + 1 : name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003419 ea.cmdidx = (cmdidx_T)0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003420 p = find_ex_command(&ea, &full, NULL, NULL);
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003421 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003422 return 3;
Bram Moolenaara9587612006-05-04 21:47:50 +00003423 if (vim_isdigit(*name) && ea.cmdidx != CMD_match)
3424 return 0;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003425 if (*skipwhite(p) != NUL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003426 return 0; // trailing garbage
Bram Moolenaar071d4272004-06-13 20:20:40 +00003427 return (ea.cmdidx == CMD_SIZE ? 0 : (full ? 2 : 1));
3428}
3429#endif
3430
Bram Moolenaard0190392019-08-23 21:17:35 +02003431 cmdidx_T
3432excmd_get_cmdidx(char_u *cmd, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003433{
Bram Moolenaard0190392019-08-23 21:17:35 +02003434 cmdidx_T idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003435
Bram Moolenaard0190392019-08-23 21:17:35 +02003436 for (idx = (cmdidx_T)0; (int)idx < (int)CMD_SIZE;
3437 idx = (cmdidx_T)((int)idx + 1))
3438 if (STRNCMP(cmdnames[(int)idx].cmd_name, cmd, (size_t)len) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003439 break;
3440
Bram Moolenaard0190392019-08-23 21:17:35 +02003441 return idx;
3442}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003443
Bram Moolenaard0190392019-08-23 21:17:35 +02003444 long
3445excmd_get_argt(cmdidx_T idx)
3446{
3447 return (long)cmdnames[(int)idx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003448}
3449
3450/*
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003451 * Skip a range specifier of the form: addr [,addr] [;addr] ..
Bram Moolenaar071d4272004-06-13 20:20:40 +00003452 *
3453 * Backslashed delimiters after / or ? will be skipped, and commands will
3454 * not be expanded between /'s and ?'s or after "'".
3455 *
Bram Moolenaardf069ee2020-06-22 23:02:51 +02003456 * Also skip white space and ":" characters after the range.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003457 * Returns the "cmd" pointer advanced to beyond the range.
3458 */
3459 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003460skip_range(
3461 char_u *cmd,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003462 int *ctx) // pointer to xp_context or NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003463{
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003464 unsigned delim;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003465
Bram Moolenaarcbf20fb2017-02-03 21:19:04 +01003466 while (vim_strchr((char_u *)" \t0123456789.$%'/?-+,;\\", *cmd) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003467 {
Bram Moolenaarcbf20fb2017-02-03 21:19:04 +01003468 if (*cmd == '\\')
3469 {
3470 if (cmd[1] == '?' || cmd[1] == '/' || cmd[1] == '&')
3471 ++cmd;
3472 else
3473 break;
3474 }
3475 else if (*cmd == '\'')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003476 {
3477 if (*++cmd == NUL && ctx != NULL)
3478 *ctx = EXPAND_NOTHING;
3479 }
3480 else if (*cmd == '/' || *cmd == '?')
3481 {
3482 delim = *cmd++;
3483 while (*cmd != NUL && *cmd != delim)
3484 if (*cmd++ == '\\' && *cmd != NUL)
3485 ++cmd;
3486 if (*cmd == NUL && ctx != NULL)
3487 *ctx = EXPAND_NOTHING;
3488 }
3489 if (*cmd != NUL)
3490 ++cmd;
3491 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00003492
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003493 // Skip ":" and white space.
Bram Moolenaardf177f62005-02-22 08:39:57 +00003494 while (*cmd == ':')
3495 cmd = skipwhite(cmd + 1);
3496
Bram Moolenaar071d4272004-06-13 20:20:40 +00003497 return cmd;
3498}
3499
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003500 static void
3501addr_error(cmd_addr_T addr_type)
3502{
3503 if (addr_type == ADDR_NONE)
3504 emsg(_(e_norange));
3505 else
3506 emsg(_(e_invrange));
3507}
3508
Bram Moolenaar071d4272004-06-13 20:20:40 +00003509/*
Bram Moolenaar198cb662018-09-06 21:44:17 +02003510 * Get a single EX address.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003511 *
3512 * Set ptr to the next character after the part that was interpreted.
3513 * Set ptr to NULL when an error is encountered.
Bram Moolenaar198cb662018-09-06 21:44:17 +02003514 * This may set the last used search pattern.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003515 *
3516 * Return MAXLNUM when no Ex address was found.
3517 */
3518 static linenr_T
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003519get_address(
3520 exarg_T *eap UNUSED,
3521 char_u **ptr,
Bram Moolenaar1b03a192019-12-08 17:08:29 +01003522 cmd_addr_T addr_type,
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02003523 int skip, // only skip the address, don't use it
3524 int silent, // no errors or side effects
3525 int to_other_file, // flag: may jump to other file
3526 int address_count UNUSED) // 1 for first address, >1 after comma
Bram Moolenaar071d4272004-06-13 20:20:40 +00003527{
3528 int c;
3529 int i;
3530 long n;
3531 char_u *cmd;
3532 pos_T pos;
3533 pos_T *fp;
3534 linenr_T lnum;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003535 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003536
3537 cmd = skipwhite(*ptr);
3538 lnum = MAXLNUM;
3539 do
3540 {
3541 switch (*cmd)
3542 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003543 case '.': // '.' - Cursor position
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003544 ++cmd;
3545 switch (addr_type)
3546 {
3547 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02003548 case ADDR_OTHER:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003549 lnum = curwin->w_cursor.lnum;
3550 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003551 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01003552 lnum = CURRENT_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003553 break;
3554 case ADDR_ARGUMENTS:
3555 lnum = curwin->w_arg_idx + 1;
3556 break;
3557 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003558 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003559 lnum = curbuf->b_fnum;
3560 break;
3561 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01003562 lnum = CURRENT_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003563 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02003564 case ADDR_NONE:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003565 case ADDR_TABS_RELATIVE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02003566 case ADDR_UNSIGNED:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003567 addr_error(addr_type);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01003568 cmd = NULL;
3569 goto error;
3570 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003571 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003572#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02003573 lnum = qf_get_cur_idx(eap);
3574#endif
3575 break;
3576 case ADDR_QUICKFIX_VALID:
3577#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003578 lnum = qf_get_cur_valid_idx(eap);
Bram Moolenaare906c502015-09-09 21:10:39 +02003579#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003580 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003581 }
3582 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003583
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003584 case '$': // '$' - last line
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003585 ++cmd;
3586 switch (addr_type)
3587 {
3588 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02003589 case ADDR_OTHER:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003590 lnum = curbuf->b_ml.ml_line_count;
3591 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003592 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01003593 lnum = LAST_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003594 break;
3595 case ADDR_ARGUMENTS:
3596 lnum = ARGCOUNT;
3597 break;
3598 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003599 buf = lastbuf;
3600 while (buf->b_ml.ml_mfp == NULL)
3601 {
3602 if (buf->b_prev == NULL)
3603 break;
3604 buf = buf->b_prev;
3605 }
3606 lnum = buf->b_fnum;
3607 break;
3608 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003609 lnum = lastbuf->b_fnum;
3610 break;
3611 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01003612 lnum = LAST_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003613 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02003614 case ADDR_NONE:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003615 case ADDR_TABS_RELATIVE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02003616 case ADDR_UNSIGNED:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003617 addr_error(addr_type);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01003618 cmd = NULL;
3619 goto error;
3620 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003621 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003622#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003623 lnum = qf_get_size(eap);
3624 if (lnum == 0)
3625 lnum = 1;
Bram Moolenaare906c502015-09-09 21:10:39 +02003626#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003627 break;
Bram Moolenaar25190db2019-05-04 15:05:28 +02003628 case ADDR_QUICKFIX_VALID:
3629#ifdef FEAT_QUICKFIX
3630 lnum = qf_get_valid_size(eap);
3631 if (lnum == 0)
3632 lnum = 1;
3633#endif
3634 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003635 }
3636 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003637
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003638 case '\'': // ''' - mark
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003639 if (*++cmd == NUL)
3640 {
3641 cmd = NULL;
3642 goto error;
3643 }
3644 if (addr_type != ADDR_LINES)
3645 {
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003646 addr_error(addr_type);
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01003647 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003648 goto error;
3649 }
3650 if (skip)
3651 ++cmd;
3652 else
3653 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003654 // Only accept a mark in another file when it is
3655 // used by itself: ":'M".
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003656 fp = getmark(*cmd, to_other_file && cmd[1] == NUL);
3657 ++cmd;
3658 if (fp == (pos_T *)-1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003659 // Jumped to another file.
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003660 lnum = curwin->w_cursor.lnum;
3661 else
3662 {
3663 if (check_mark(fp) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003664 {
3665 cmd = NULL;
3666 goto error;
3667 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003668 lnum = fp->lnum;
3669 }
3670 }
3671 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003672
3673 case '/':
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003674 case '?': // '/' or '?' - search
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003675 c = *cmd++;
3676 if (addr_type != ADDR_LINES)
3677 {
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003678 addr_error(addr_type);
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01003679 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003680 goto error;
3681 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003682 if (skip) // skip "/pat/"
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003683 {
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02003684 cmd = skip_regexp(cmd, c, (int)p_magic);
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003685 if (*cmd == c)
3686 ++cmd;
3687 }
3688 else
3689 {
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02003690 int flags;
3691
3692 pos = curwin->w_cursor; // save curwin->w_cursor
3693
3694 // When '/' or '?' follows another address, start from
3695 // there.
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003696 if (lnum != MAXLNUM)
3697 curwin->w_cursor.lnum = lnum;
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02003698
3699 // Start a forward search at the end of the line (unless
3700 // before the first line).
3701 // Start a backward search at the start of the line.
3702 // This makes sure we never match in the current
3703 // line, and can match anywhere in the
3704 // next/previous line.
Bram Moolenaar8ada6aa2017-12-19 21:23:21 +01003705 if (c == '/' && curwin->w_cursor.lnum > 0)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003706 curwin->w_cursor.col = MAXCOL;
3707 else
3708 curwin->w_cursor.col = 0;
3709 searchcmdlen = 0;
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02003710 flags = silent ? 0 : SEARCH_HIS | SEARCH_MSG;
Bram Moolenaarc036e872020-02-21 21:30:52 +01003711 if (!do_search(NULL, c, c, cmd, 1L, flags, NULL))
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003712 {
3713 curwin->w_cursor = pos;
3714 cmd = NULL;
3715 goto error;
3716 }
3717 lnum = curwin->w_cursor.lnum;
3718 curwin->w_cursor = pos;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003719 // adjust command string pointer
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003720 cmd += searchcmdlen;
3721 }
3722 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003723
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003724 case '\\': // "\?", "\/" or "\&", repeat search
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003725 ++cmd;
3726 if (addr_type != ADDR_LINES)
3727 {
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003728 addr_error(addr_type);
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01003729 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003730 goto error;
3731 }
3732 if (*cmd == '&')
3733 i = RE_SUBST;
3734 else if (*cmd == '?' || *cmd == '/')
3735 i = RE_SEARCH;
3736 else
3737 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003738 emsg(_(e_backslash));
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003739 cmd = NULL;
3740 goto error;
3741 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003742
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003743 if (!skip)
3744 {
3745 /*
3746 * When search follows another address, start from
3747 * there.
3748 */
3749 if (lnum != MAXLNUM)
3750 pos.lnum = lnum;
3751 else
3752 pos.lnum = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003753
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003754 /*
3755 * Start the search just like for the above
3756 * do_search().
3757 */
3758 if (*cmd != '?')
3759 pos.col = MAXCOL;
3760 else
3761 pos.col = 0;
Bram Moolenaarbd8539a2015-08-11 18:53:03 +02003762 pos.coladd = 0;
Bram Moolenaar5d24a222018-12-23 19:10:09 +01003763 if (searchit(curwin, curbuf, &pos, NULL,
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003764 *cmd == '?' ? BACKWARD : FORWARD,
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02003765 (char_u *)"", 1L, SEARCH_MSG, i, NULL) != FAIL)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003766 lnum = pos.lnum;
3767 else
3768 {
3769 cmd = NULL;
3770 goto error;
3771 }
3772 }
3773 ++cmd;
3774 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003775
3776 default:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003777 if (VIM_ISDIGIT(*cmd)) // absolute line number
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003778 lnum = getdigits(&cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003779 }
3780
3781 for (;;)
3782 {
3783 cmd = skipwhite(cmd);
3784 if (*cmd != '-' && *cmd != '+' && !VIM_ISDIGIT(*cmd))
3785 break;
3786
3787 if (lnum == MAXLNUM)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003788 {
3789 switch (addr_type)
3790 {
3791 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02003792 case ADDR_OTHER:
3793 // "+1" is same as ".+1"
Bram Moolenaarf240e182014-11-27 18:33:02 +01003794 lnum = curwin->w_cursor.lnum;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003795 break;
3796 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01003797 lnum = CURRENT_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003798 break;
3799 case ADDR_ARGUMENTS:
3800 lnum = curwin->w_arg_idx + 1;
3801 break;
3802 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003803 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003804 lnum = curbuf->b_fnum;
3805 break;
3806 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01003807 lnum = CURRENT_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003808 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01003809 case ADDR_TABS_RELATIVE:
3810 lnum = 1;
3811 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003812 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003813#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02003814 lnum = qf_get_cur_idx(eap);
3815#endif
3816 break;
3817 case ADDR_QUICKFIX_VALID:
3818#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003819 lnum = qf_get_cur_valid_idx(eap);
Bram Moolenaare906c502015-09-09 21:10:39 +02003820#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003821 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02003822 case ADDR_NONE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02003823 case ADDR_UNSIGNED:
3824 lnum = 0;
Bram Moolenaarb7316892019-05-01 18:08:42 +02003825 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003826 }
3827 }
3828
Bram Moolenaar071d4272004-06-13 20:20:40 +00003829 if (VIM_ISDIGIT(*cmd))
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003830 i = '+'; // "number" is same as "+number"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003831 else
3832 i = *cmd++;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003833 if (!VIM_ISDIGIT(*cmd)) // '+' is '+1', but '+0' is not '+1'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003834 n = 1;
3835 else
3836 n = getdigits(&cmd);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01003837
3838 if (addr_type == ADDR_TABS_RELATIVE)
3839 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003840 emsg(_(e_invrange));
Bram Moolenaar2f72c702017-01-29 14:48:10 +01003841 cmd = NULL;
3842 goto error;
3843 }
3844 else if (addr_type == ADDR_LOADED_BUFFERS
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003845 || addr_type == ADDR_BUFFERS)
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01003846 lnum = compute_buffer_local_count(
3847 addr_type, lnum, (i == '-') ? -1 * n : n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003848 else
Bram Moolenaarded27822017-01-02 14:27:34 +01003849 {
3850#ifdef FEAT_FOLDING
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003851 // Relative line addressing, need to adjust for folded lines
3852 // now, but only do it after the first address.
Bram Moolenaarded27822017-01-02 14:27:34 +01003853 if (addr_type == ADDR_LINES && (i == '-' || i == '+')
3854 && address_count >= 2)
3855 (void)hasFolding(lnum, NULL, &lnum);
3856#endif
3857 if (i == '-')
3858 lnum -= n;
3859 else
3860 lnum += n;
3861 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003862 }
3863 } while (*cmd == '/' || *cmd == '?');
3864
3865error:
3866 *ptr = cmd;
3867 return lnum;
3868}
3869
3870/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00003871 * Get flags from an Ex command argument.
3872 */
3873 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003874get_flags(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00003875{
3876 while (vim_strchr((char_u *)"lp#", *eap->arg) != NULL)
3877 {
3878 if (*eap->arg == 'l')
3879 eap->flags |= EXFLAG_LIST;
3880 else if (*eap->arg == 'p')
3881 eap->flags |= EXFLAG_PRINT;
3882 else
3883 eap->flags |= EXFLAG_NR;
3884 eap->arg = skipwhite(eap->arg + 1);
3885 }
3886}
3887
3888/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003889 * Function called for command which is Not Implemented. NI!
3890 */
3891 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003892ex_ni(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003893{
3894 if (!eap->skip)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003895 eap->errmsg = N_("E319: Sorry, the command is not available in this version");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003896}
3897
Bram Moolenaar7bb75552007-07-16 18:39:49 +00003898#ifdef HAVE_EX_SCRIPT_NI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003899/*
3900 * Function called for script command which is Not Implemented. NI!
3901 * Skips over ":perl <<EOF" constructs.
3902 */
3903 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003904ex_script_ni(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003905{
3906 if (!eap->skip)
3907 ex_ni(eap);
3908 else
3909 vim_free(script_get(eap, eap->arg));
3910}
3911#endif
3912
3913/*
3914 * Check range in Ex command for validity.
3915 * Return NULL when valid, error message when invalid.
3916 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003917 static char *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003918invalid_range(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003919{
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003920 buf_T *buf;
Bram Moolenaar25190db2019-05-04 15:05:28 +02003921
Bram Moolenaar071d4272004-06-13 20:20:40 +00003922 if ( eap->line1 < 0
3923 || eap->line2 < 0
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003924 || eap->line1 > eap->line2)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003925 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003926
Bram Moolenaar8071cb22019-07-12 17:58:01 +02003927 if (eap->argt & EX_RANGE)
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003928 {
Bram Moolenaarb7316892019-05-01 18:08:42 +02003929 switch (eap->addr_type)
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003930 {
3931 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02003932 if (eap->line2 > curbuf->b_ml.ml_line_count
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003933#ifdef FEAT_DIFF
3934 + (eap->cmdidx == CMD_diffget)
3935#endif
3936 )
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003937 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003938 break;
3939 case ADDR_ARGUMENTS:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003940 // add 1 if ARGCOUNT is 0
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01003941 if (eap->line2 > ARGCOUNT + (!ARGCOUNT))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003942 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003943 break;
3944 case ADDR_BUFFERS:
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02003945 // Only a boundary check, not whether the buffers actually
3946 // exist.
3947 if (eap->line1 < 1 || eap->line2 > get_highest_fnum())
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003948 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003949 break;
3950 case ADDR_LOADED_BUFFERS:
3951 buf = firstbuf;
3952 while (buf->b_ml.ml_mfp == NULL)
3953 {
3954 if (buf->b_next == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003955 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003956 buf = buf->b_next;
3957 }
3958 if (eap->line1 < buf->b_fnum)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003959 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003960 buf = lastbuf;
3961 while (buf->b_ml.ml_mfp == NULL)
3962 {
3963 if (buf->b_prev == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003964 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003965 buf = buf->b_prev;
3966 }
3967 if (eap->line2 > buf->b_fnum)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003968 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003969 break;
3970 case ADDR_WINDOWS:
Bram Moolenaar8be63882015-01-14 11:25:05 +01003971 if (eap->line2 > LAST_WIN_NR)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003972 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003973 break;
3974 case ADDR_TABS:
3975 if (eap->line2 > LAST_TAB_NR)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003976 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003977 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01003978 case ADDR_TABS_RELATIVE:
Bram Moolenaarb7316892019-05-01 18:08:42 +02003979 case ADDR_OTHER:
3980 // Any range is OK.
Bram Moolenaar2f72c702017-01-29 14:48:10 +01003981 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003982 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003983#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02003984 // No error for value that is too big, will use the last entry.
3985 if (eap->line2 <= 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003986 return _(e_invrange);
Bram Moolenaare906c502015-09-09 21:10:39 +02003987#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003988 break;
Bram Moolenaar25190db2019-05-04 15:05:28 +02003989 case ADDR_QUICKFIX_VALID:
3990#ifdef FEAT_QUICKFIX
3991 if ((eap->line2 != 1 && eap->line2 > qf_get_valid_size(eap))
3992 || eap->line2 < 0)
3993 return _(e_invrange);
3994#endif
3995 break;
3996 case ADDR_UNSIGNED:
3997 if (eap->line2 < 0)
3998 return _(e_invrange);
3999 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02004000 case ADDR_NONE:
4001 // Will give an error elsewhere.
4002 break;
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004003 }
4004 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004005 return NULL;
4006}
4007
4008/*
4009 * Correct the range for zero line number, if required.
4010 */
4011 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004012correct_range(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004013{
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004014 if (!(eap->argt & EX_ZEROR)) // zero in range not allowed
Bram Moolenaar071d4272004-06-13 20:20:40 +00004015 {
4016 if (eap->line1 == 0)
4017 eap->line1 = 1;
4018 if (eap->line2 == 0)
4019 eap->line2 = 1;
4020 }
4021}
4022
Bram Moolenaar748bf032005-02-02 23:04:36 +00004023#ifdef FEAT_QUICKFIX
Bram Moolenaar748bf032005-02-02 23:04:36 +00004024/*
4025 * For a ":vimgrep" or ":vimgrepadd" command return a pointer past the
4026 * pattern. Otherwise return eap->arg.
4027 */
4028 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004029skip_grep_pat(exarg_T *eap)
Bram Moolenaar748bf032005-02-02 23:04:36 +00004030{
4031 char_u *p = eap->arg;
4032
Bram Moolenaara37420f2006-02-04 22:37:47 +00004033 if (*p != NUL && (eap->cmdidx == CMD_vimgrep || eap->cmdidx == CMD_lvimgrep
4034 || eap->cmdidx == CMD_vimgrepadd
4035 || eap->cmdidx == CMD_lvimgrepadd
4036 || grep_internal(eap->cmdidx)))
Bram Moolenaar748bf032005-02-02 23:04:36 +00004037 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00004038 p = skip_vimgrep_pat(p, NULL, NULL);
Bram Moolenaar748bf032005-02-02 23:04:36 +00004039 if (p == NULL)
4040 p = eap->arg;
Bram Moolenaar748bf032005-02-02 23:04:36 +00004041 }
4042 return p;
4043}
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004044
4045/*
4046 * For the ":make" and ":grep" commands insert the 'makeprg'/'grepprg' option
4047 * in the command line, so that things like % get expanded.
4048 */
4049 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004050replace_makeprg(exarg_T *eap, char_u *p, char_u **cmdlinep)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004051{
4052 char_u *new_cmdline;
4053 char_u *program;
4054 char_u *pos;
4055 char_u *ptr;
4056 int len;
4057 int i;
4058
4059 /*
4060 * Don't do it when ":vimgrep" is used for ":grep".
4061 */
Bram Moolenaara37420f2006-02-04 22:37:47 +00004062 if ((eap->cmdidx == CMD_make || eap->cmdidx == CMD_lmake
4063 || eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4064 || eap->cmdidx == CMD_grepadd
4065 || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004066 && !grep_internal(eap->cmdidx))
4067 {
Bram Moolenaara37420f2006-02-04 22:37:47 +00004068 if (eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4069 || eap->cmdidx == CMD_grepadd || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004070 {
4071 if (*curbuf->b_p_gp == NUL)
4072 program = p_gp;
4073 else
4074 program = curbuf->b_p_gp;
4075 }
4076 else
4077 {
4078 if (*curbuf->b_p_mp == NUL)
4079 program = p_mp;
4080 else
4081 program = curbuf->b_p_mp;
4082 }
4083
4084 p = skipwhite(p);
4085
4086 if ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4087 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004088 // replace $* by given arguments
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004089 i = 1;
4090 while ((pos = (char_u *)strstr((char *)pos + 2, "$*")) != NULL)
4091 ++i;
4092 len = (int)STRLEN(p);
Bram Moolenaar51e14382019-05-25 20:21:28 +02004093 new_cmdline = alloc(STRLEN(program) + i * (len - 2) + 1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004094 if (new_cmdline == NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004095 return NULL; // out of memory
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004096 ptr = new_cmdline;
4097 while ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4098 {
4099 i = (int)(pos - program);
4100 STRNCPY(ptr, program, i);
4101 STRCPY(ptr += i, p);
4102 ptr += len;
4103 program = pos + 2;
4104 }
4105 STRCPY(ptr, program);
4106 }
4107 else
4108 {
Bram Moolenaar51e14382019-05-25 20:21:28 +02004109 new_cmdline = alloc(STRLEN(program) + STRLEN(p) + 2);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004110 if (new_cmdline == NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004111 return NULL; // out of memory
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004112 STRCPY(new_cmdline, program);
4113 STRCAT(new_cmdline, " ");
4114 STRCAT(new_cmdline, p);
4115 }
4116 msg_make(p);
4117
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004118 // 'eap->cmd' is not set here, because it is not used at CMD_make
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004119 vim_free(*cmdlinep);
4120 *cmdlinep = new_cmdline;
4121 p = new_cmdline;
4122 }
4123 return p;
4124}
Bram Moolenaar748bf032005-02-02 23:04:36 +00004125#endif
4126
Bram Moolenaar071d4272004-06-13 20:20:40 +00004127/*
4128 * Expand file name in Ex command argument.
Bram Moolenaar0abb4272019-06-15 16:06:00 +02004129 * When an error is detected, "errormsgp" is set to a non-NULL pointer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004130 * Return FAIL for failure, OK otherwise.
4131 */
4132 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004133expand_filename(
4134 exarg_T *eap,
4135 char_u **cmdlinep,
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004136 char **errormsgp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004137{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004138 int has_wildcards; // need to expand wildcards
Bram Moolenaar071d4272004-06-13 20:20:40 +00004139 char_u *repl;
4140 int srclen;
4141 char_u *p;
4142 int n;
Bram Moolenaar63b92542007-03-27 14:57:09 +00004143 int escaped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004144
Bram Moolenaar748bf032005-02-02 23:04:36 +00004145#ifdef FEAT_QUICKFIX
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004146 // Skip a regexp pattern for ":vimgrep[add] pat file..."
Bram Moolenaar748bf032005-02-02 23:04:36 +00004147 p = skip_grep_pat(eap);
4148#else
4149 p = eap->arg;
4150#endif
4151
Bram Moolenaar071d4272004-06-13 20:20:40 +00004152 /*
4153 * Decide to expand wildcards *before* replacing '%', '#', etc. If
4154 * the file name contains a wildcard it should not cause expanding.
4155 * (it will be expanded anyway if there is a wildcard before replacing).
4156 */
Bram Moolenaar748bf032005-02-02 23:04:36 +00004157 has_wildcards = mch_has_wildcard(p);
4158 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004159 {
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004160#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004161 // Skip over `=expr`, wildcards in it are not expanded.
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004162 if (p[0] == '`' && p[1] == '=')
4163 {
4164 p += 2;
4165 (void)skip_expr(&p);
4166 if (*p == '`')
4167 ++p;
4168 continue;
4169 }
4170#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004171 /*
4172 * Quick check if this cannot be the start of a special string.
4173 * Also removes backslash before '%', '#' and '<'.
4174 */
4175 if (vim_strchr((char_u *)"%#<", *p) == NULL)
4176 {
4177 ++p;
4178 continue;
4179 }
4180
4181 /*
4182 * Try to find a match at this position.
4183 */
Bram Moolenaar63b92542007-03-27 14:57:09 +00004184 repl = eval_vars(p, eap->arg, &srclen, &(eap->do_ecmd_lnum),
4185 errormsgp, &escaped);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004186 if (*errormsgp != NULL) // error detected
Bram Moolenaar071d4272004-06-13 20:20:40 +00004187 return FAIL;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004188 if (repl == NULL) // no match found
Bram Moolenaar071d4272004-06-13 20:20:40 +00004189 {
4190 p += srclen;
4191 continue;
4192 }
4193
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004194 // Wildcards won't be expanded below, the replacement is taken
4195 // literally. But do expand "~/file", "~user/file" and "$HOME/file".
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00004196 if (vim_strchr(repl, '$') != NULL || vim_strchr(repl, '~') != NULL)
4197 {
4198 char_u *l = repl;
4199
4200 repl = expand_env_save(repl);
4201 vim_free(l);
4202 }
4203
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004204 // Need to escape white space et al. with a backslash.
4205 // Don't do this for:
4206 // - replacement that already has been escaped: "##"
4207 // - shell commands (may have to use quotes instead).
4208 // - non-unix systems when there is a single argument (spaces don't
4209 // separate arguments then).
Bram Moolenaar071d4272004-06-13 20:20:40 +00004210 if (!eap->usefilter
Bram Moolenaar63b92542007-03-27 14:57:09 +00004211 && !escaped
Bram Moolenaar071d4272004-06-13 20:20:40 +00004212 && eap->cmdidx != CMD_bang
Bram Moolenaar071d4272004-06-13 20:20:40 +00004213 && eap->cmdidx != CMD_grep
4214 && eap->cmdidx != CMD_grepadd
Bram Moolenaarbf15b8d2017-06-04 20:43:48 +02004215 && eap->cmdidx != CMD_hardcopy
Bram Moolenaar67883b42017-07-27 22:57:00 +02004216 && eap->cmdidx != CMD_lgrep
4217 && eap->cmdidx != CMD_lgrepadd
4218 && eap->cmdidx != CMD_lmake
4219 && eap->cmdidx != CMD_make
4220 && eap->cmdidx != CMD_terminal
Bram Moolenaar071d4272004-06-13 20:20:40 +00004221#ifndef UNIX
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004222 && !(eap->argt & EX_NOSPC)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004223#endif
4224 )
4225 {
4226 char_u *l;
4227#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004228 // Don't escape a backslash here, because rem_backslash() doesn't
4229 // remove it later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004230 static char_u *nobslash = (char_u *)" \t\"|";
4231# define ESCAPE_CHARS nobslash
4232#else
4233# define ESCAPE_CHARS escape_chars
4234#endif
4235
4236 for (l = repl; *l; ++l)
4237 if (vim_strchr(ESCAPE_CHARS, *l) != NULL)
4238 {
4239 l = vim_strsave_escaped(repl, ESCAPE_CHARS);
4240 if (l != NULL)
4241 {
4242 vim_free(repl);
4243 repl = l;
4244 }
4245 break;
4246 }
4247 }
4248
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004249 // For a shell command a '!' must be escaped.
Bram Moolenaar67883b42017-07-27 22:57:00 +02004250 if ((eap->usefilter || eap->cmdidx == CMD_bang
4251 || eap->cmdidx == CMD_terminal)
Bram Moolenaar31b7d382014-04-01 18:54:48 +02004252 && vim_strpbrk(repl, (char_u *)"!") != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004253 {
4254 char_u *l;
4255
Bram Moolenaar31b7d382014-04-01 18:54:48 +02004256 l = vim_strsave_escaped(repl, (char_u *)"!");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004257 if (l != NULL)
4258 {
4259 vim_free(repl);
4260 repl = l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004261 }
4262 }
4263
4264 p = repl_cmdline(eap, p, srclen, repl, cmdlinep);
4265 vim_free(repl);
4266 if (p == NULL)
4267 return FAIL;
4268 }
4269
4270 /*
4271 * One file argument: Expand wildcards.
4272 * Don't do this with ":r !command" or ":w !command".
4273 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004274 if ((eap->argt & EX_NOSPC) && !eap->usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004275 {
4276 /*
4277 * May do this twice:
4278 * 1. Replace environment variables.
4279 * 2. Replace any other wildcards, remove backslashes.
4280 */
4281 for (n = 1; n <= 2; ++n)
4282 {
4283 if (n == 2)
4284 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004285 /*
4286 * Halve the number of backslashes (this is Vi compatible).
4287 * For Unix and OS/2, when wildcards are expanded, this is
4288 * done by ExpandOne() below.
4289 */
Bram Moolenaare7fedb62015-12-31 19:07:19 +01004290#if defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004291 if (!has_wildcards)
4292#endif
4293 backslash_halve(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004294 }
4295
4296 if (has_wildcards)
4297 {
4298 if (n == 1)
4299 {
4300 /*
4301 * First loop: May expand environment variables. This
4302 * can be done much faster with expand_env() than with
4303 * something else (e.g., calling a shell).
4304 * After expanding environment variables, check again
4305 * if there are still wildcards present.
4306 */
4307 if (vim_strchr(eap->arg, '$') != NULL
4308 || vim_strchr(eap->arg, '~') != NULL)
4309 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +00004310 expand_env_esc(eap->arg, NameBuff, MAXPATHL,
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00004311 TRUE, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004312 has_wildcards = mch_has_wildcard(NameBuff);
4313 p = NameBuff;
4314 }
4315 else
4316 p = NULL;
4317 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004318 else // n == 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00004319 {
4320 expand_T xpc;
Bram Moolenaarb40c2572019-10-19 21:01:05 +02004321 int options = WILD_LIST_NOTFOUND
4322 | WILD_NOERROR | WILD_ADD_SLASH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004323
4324 ExpandInit(&xpc);
4325 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar94950a92010-12-02 16:01:29 +01004326 if (p_wic)
4327 options += WILD_ICASE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004328 p = ExpandOne(&xpc, eap->arg, NULL,
Bram Moolenaar94950a92010-12-02 16:01:29 +01004329 options, WILD_EXPAND_FREE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004330 if (p == NULL)
4331 return FAIL;
4332 }
4333 if (p != NULL)
4334 {
4335 (void)repl_cmdline(eap, eap->arg, (int)STRLEN(eap->arg),
4336 p, cmdlinep);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004337 if (n == 2) // p came from ExpandOne()
Bram Moolenaar071d4272004-06-13 20:20:40 +00004338 vim_free(p);
4339 }
4340 }
4341 }
4342 }
4343 return OK;
4344}
4345
4346/*
4347 * Replace part of the command line, keeping eap->cmd, eap->arg and
4348 * eap->nextcmd correct.
4349 * "src" points to the part that is to be replaced, of length "srclen".
4350 * "repl" is the replacement string.
4351 * Returns a pointer to the character after the replaced string.
4352 * Returns NULL for failure.
4353 */
4354 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004355repl_cmdline(
4356 exarg_T *eap,
4357 char_u *src,
4358 int srclen,
4359 char_u *repl,
4360 char_u **cmdlinep)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004361{
4362 int len;
4363 int i;
4364 char_u *new_cmdline;
4365
4366 /*
4367 * The new command line is build in new_cmdline[].
4368 * First allocate it.
4369 * Careful: a "+cmd" argument may have been NUL terminated.
4370 */
4371 len = (int)STRLEN(repl);
4372 i = (int)(src - *cmdlinep) + (int)STRLEN(src + srclen) + len + 3;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004373 if (eap->nextcmd != NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004374 i += (int)STRLEN(eap->nextcmd);// add space for next command
Bram Moolenaar964b3742019-05-24 18:54:09 +02004375 if ((new_cmdline = alloc(i)) == NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004376 return NULL; // out of memory!
Bram Moolenaar071d4272004-06-13 20:20:40 +00004377
4378 /*
4379 * Copy the stuff before the expanded part.
4380 * Copy the expanded stuff.
4381 * Copy what came after the expanded part.
4382 * Copy the next commands, if there are any.
4383 */
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004384 i = (int)(src - *cmdlinep); // length of part before match
Bram Moolenaar071d4272004-06-13 20:20:40 +00004385 mch_memmove(new_cmdline, *cmdlinep, (size_t)i);
Bram Moolenaara3ffd9c2005-07-21 21:03:15 +00004386
Bram Moolenaar071d4272004-06-13 20:20:40 +00004387 mch_memmove(new_cmdline + i, repl, (size_t)len);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004388 i += len; // remember the end of the string
Bram Moolenaar071d4272004-06-13 20:20:40 +00004389 STRCPY(new_cmdline + i, src + srclen);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004390 src = new_cmdline + i; // remember where to continue
Bram Moolenaar071d4272004-06-13 20:20:40 +00004391
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004392 if (eap->nextcmd != NULL) // append next command
Bram Moolenaar071d4272004-06-13 20:20:40 +00004393 {
4394 i = (int)STRLEN(new_cmdline) + 1;
4395 STRCPY(new_cmdline + i, eap->nextcmd);
4396 eap->nextcmd = new_cmdline + i;
4397 }
4398 eap->cmd = new_cmdline + (eap->cmd - *cmdlinep);
4399 eap->arg = new_cmdline + (eap->arg - *cmdlinep);
4400 if (eap->do_ecmd_cmd != NULL && eap->do_ecmd_cmd != dollar_command)
4401 eap->do_ecmd_cmd = new_cmdline + (eap->do_ecmd_cmd - *cmdlinep);
4402 vim_free(*cmdlinep);
4403 *cmdlinep = new_cmdline;
4404
4405 return src;
4406}
4407
4408/*
4409 * Check for '|' to separate commands and '"' to start comments.
4410 */
4411 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004412separate_nextcmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004413{
4414 char_u *p;
4415
Bram Moolenaar86b68352004-12-27 21:59:20 +00004416#ifdef FEAT_QUICKFIX
Bram Moolenaar748bf032005-02-02 23:04:36 +00004417 p = skip_grep_pat(eap);
4418#else
4419 p = eap->arg;
Bram Moolenaar86b68352004-12-27 21:59:20 +00004420#endif
4421
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004422 for ( ; *p; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004423 {
4424 if (*p == Ctrl_V)
4425 {
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004426 if (eap->argt & (EX_CTRLV | EX_XFILE))
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004427 ++p; // skip CTRL-V and next char
Bram Moolenaar071d4272004-06-13 20:20:40 +00004428 else
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004429 // remove CTRL-V and skip next char
Bram Moolenaara7241f52008-06-24 20:39:31 +00004430 STRMOVE(p, p + 1);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004431 if (*p == NUL) // stop at NUL after CTRL-V
Bram Moolenaar071d4272004-06-13 20:20:40 +00004432 break;
4433 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004434
4435#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004436 // Skip over `=expr` when wildcards are expanded.
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004437 else if (p[0] == '`' && p[1] == '=' && (eap->argt & EX_XFILE))
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004438 {
4439 p += 2;
4440 (void)skip_expr(&p);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004441 }
4442#endif
4443
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004444 // Check for '"': start of comment or '|': next command
4445 // :@" and :*" do not start a comment!
4446 // :redir @" doesn't either.
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004447 else if ((*p == '"' && !(eap->argt & EX_NOTRLCOM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004448 && ((eap->cmdidx != CMD_at && eap->cmdidx != CMD_star)
4449 || p != eap->arg)
4450 && (eap->cmdidx != CMD_redir
4451 || p != eap->arg + 1 || p[-1] != '@'))
Bram Moolenaara26b9702020-04-18 19:53:28 +02004452#ifdef FEAT_EVAL
4453 || (*p == '#' && in_vim9script()
4454 && p[1] != '{' && p > eap->cmd && VIM_ISWHITE(p[-1]))
4455#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004456 || *p == '|' || *p == '\n')
4457 {
4458 /*
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004459 * We remove the '\' before the '|', unless EX_CTRLV is used
Bram Moolenaar071d4272004-06-13 20:20:40 +00004460 * AND 'b' is present in 'cpoptions'.
4461 */
4462 if ((vim_strchr(p_cpo, CPO_BAR) == NULL
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004463 || !(eap->argt & EX_CTRLV)) && *(p - 1) == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004464 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004465 STRMOVE(p - 1, p); // remove the '\'
Bram Moolenaar071d4272004-06-13 20:20:40 +00004466 --p;
4467 }
4468 else
4469 {
4470 eap->nextcmd = check_nextcmd(p);
4471 *p = NUL;
4472 break;
4473 }
4474 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004475 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00004476
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004477 if (!(eap->argt & EX_NOTRLCOM)) // remove trailing spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00004478 del_trailing_spaces(eap->arg);
4479}
4480
4481/*
4482 * get + command from ex argument
4483 */
4484 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004485getargcmd(char_u **argp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004486{
4487 char_u *arg = *argp;
4488 char_u *command = NULL;
4489
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004490 if (*arg == '+') // +[command]
Bram Moolenaar071d4272004-06-13 20:20:40 +00004491 {
4492 ++arg;
Bram Moolenaar3e451592014-04-02 14:22:05 +02004493 if (vim_isspace(*arg) || *arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004494 command = dollar_command;
4495 else
4496 {
4497 command = arg;
4498 arg = skip_cmd_arg(command, TRUE);
4499 if (*arg != NUL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004500 *arg++ = NUL; // terminate command with NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004501 }
4502
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004503 arg = skipwhite(arg); // skip over spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00004504 *argp = arg;
4505 }
4506 return command;
4507}
4508
4509/*
4510 * Find end of "+command" argument. Skip over "\ " and "\\".
4511 */
Bram Moolenaard0190392019-08-23 21:17:35 +02004512 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004513skip_cmd_arg(
4514 char_u *p,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004515 int rembs) // TRUE to halve the number of backslashes
Bram Moolenaar071d4272004-06-13 20:20:40 +00004516{
4517 while (*p && !vim_isspace(*p))
4518 {
4519 if (*p == '\\' && p[1] != NUL)
4520 {
4521 if (rembs)
Bram Moolenaara7241f52008-06-24 20:39:31 +00004522 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004523 else
4524 ++p;
4525 }
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004526 MB_PTR_ADV(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004527 }
4528 return p;
4529}
4530
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004531 int
4532get_bad_opt(char_u *p, exarg_T *eap)
4533{
4534 if (STRICMP(p, "keep") == 0)
4535 eap->bad_char = BAD_KEEP;
4536 else if (STRICMP(p, "drop") == 0)
4537 eap->bad_char = BAD_DROP;
4538 else if (MB_BYTE2LEN(*p) == 1 && p[1] == NUL)
4539 eap->bad_char = *p;
Bram Moolenaar75808492018-06-12 12:39:41 +02004540 else
4541 return FAIL;
4542 return OK;
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004543}
4544
Bram Moolenaar071d4272004-06-13 20:20:40 +00004545/*
4546 * Get "++opt=arg" argument.
4547 * Return FAIL or OK.
4548 */
4549 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004550getargopt(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004551{
4552 char_u *arg = eap->arg + 2;
4553 int *pp = NULL;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02004554 int bad_char_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004555 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004556
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004557 // ":edit ++[no]bin[ary] file"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004558 if (STRNCMP(arg, "bin", 3) == 0 || STRNCMP(arg, "nobin", 5) == 0)
4559 {
4560 if (*arg == 'n')
4561 {
4562 arg += 2;
4563 eap->force_bin = FORCE_NOBIN;
4564 }
4565 else
4566 eap->force_bin = FORCE_BIN;
4567 if (!checkforcmd(&arg, "binary", 3))
4568 return FAIL;
4569 eap->arg = skipwhite(arg);
4570 return OK;
4571 }
4572
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004573 // ":read ++edit file"
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004574 if (STRNCMP(arg, "edit", 4) == 0)
4575 {
4576 eap->read_edit = TRUE;
4577 eap->arg = skipwhite(arg + 4);
4578 return OK;
4579 }
4580
Bram Moolenaar071d4272004-06-13 20:20:40 +00004581 if (STRNCMP(arg, "ff", 2) == 0)
4582 {
4583 arg += 2;
4584 pp = &eap->force_ff;
4585 }
4586 else if (STRNCMP(arg, "fileformat", 10) == 0)
4587 {
4588 arg += 10;
4589 pp = &eap->force_ff;
4590 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004591 else if (STRNCMP(arg, "enc", 3) == 0)
4592 {
Bram Moolenaarb38e9ab2011-12-14 14:49:45 +01004593 if (STRNCMP(arg, "encoding", 8) == 0)
4594 arg += 8;
4595 else
4596 arg += 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004597 pp = &eap->force_enc;
4598 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004599 else if (STRNCMP(arg, "bad", 3) == 0)
4600 {
4601 arg += 3;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02004602 pp = &bad_char_idx;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004603 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004604
4605 if (pp == NULL || *arg != '=')
4606 return FAIL;
4607
4608 ++arg;
4609 *pp = (int)(arg - eap->cmd);
4610 arg = skip_cmd_arg(arg, FALSE);
4611 eap->arg = skipwhite(arg);
4612 *arg = NUL;
4613
Bram Moolenaar071d4272004-06-13 20:20:40 +00004614 if (pp == &eap->force_ff)
4615 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004616 if (check_ff_value(eap->cmd + eap->force_ff) == FAIL)
4617 return FAIL;
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004618 eap->force_ff = eap->cmd[eap->force_ff];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004619 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004620 else if (pp == &eap->force_enc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004621 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004622 // Make 'fileencoding' lower case.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004623 for (p = eap->cmd + eap->force_enc; *p != NUL; ++p)
4624 *p = TOLOWER_ASC(*p);
4625 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004626 else
4627 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004628 // Check ++bad= argument. Must be a single-byte character, "keep" or
4629 // "drop".
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004630 if (get_bad_opt(eap->cmd + bad_char_idx, eap) == FAIL)
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004631 return FAIL;
4632 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004633
4634 return OK;
4635}
4636
Bram Moolenaar071d4272004-06-13 20:20:40 +00004637 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004638ex_autocmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004639{
4640 /*
Bram Moolenaar26d4b892018-07-04 22:26:28 +02004641 * Disallow autocommands from .exrc and .vimrc in current
Bram Moolenaar071d4272004-06-13 20:20:40 +00004642 * directory for security reasons.
4643 */
4644 if (secure)
4645 {
4646 secure = 2;
4647 eap->errmsg = e_curdir;
4648 }
4649 else if (eap->cmdidx == CMD_autocmd)
4650 do_autocmd(eap->arg, eap->forceit);
4651 else
4652 do_augroup(eap->arg, eap->forceit);
4653}
4654
4655/*
4656 * ":doautocmd": Apply the automatic commands to the current buffer.
4657 */
4658 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004659ex_doautocmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004660{
Bram Moolenaar60542ac2012-02-12 20:14:01 +01004661 char_u *arg = eap->arg;
4662 int call_do_modelines = check_nomodeline(&arg);
Bram Moolenaar1610d052016-06-09 22:53:01 +02004663 int did_aucmd;
Bram Moolenaar60542ac2012-02-12 20:14:01 +01004664
Bram Moolenaar1610d052016-06-09 22:53:01 +02004665 (void)do_doautocmd(arg, TRUE, &did_aucmd);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004666 // Only when there is no <nomodeline>.
Bram Moolenaar1610d052016-06-09 22:53:01 +02004667 if (call_do_modelines && did_aucmd)
Bram Moolenaar60542ac2012-02-12 20:14:01 +01004668 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004669}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004670
Bram Moolenaar071d4272004-06-13 20:20:40 +00004671/*
4672 * :[N]bunload[!] [N] [bufname] unload buffer
4673 * :[N]bdelete[!] [N] [bufname] delete buffer from buffer list
4674 * :[N]bwipeout[!] [N] [bufname] delete buffer really
4675 */
4676 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004677ex_bunload(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004678{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01004679 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02004680 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004681 eap->errmsg = do_bufdel(
4682 eap->cmdidx == CMD_bdelete ? DOBUF_DEL
4683 : eap->cmdidx == CMD_bwipeout ? DOBUF_WIPE
4684 : DOBUF_UNLOAD, eap->arg,
4685 eap->addr_count, (int)eap->line1, (int)eap->line2, eap->forceit);
4686}
4687
4688/*
4689 * :[N]buffer [N] to buffer N
4690 * :[N]sbuffer [N] to buffer N
4691 */
4692 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004693ex_buffer(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004694{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01004695 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02004696 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004697 if (*eap->arg)
4698 eap->errmsg = e_trailing;
4699 else
4700 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004701 if (eap->addr_count == 0) // default is current buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00004702 goto_buffer(eap, DOBUF_CURRENT, FORWARD, 0);
4703 else
4704 goto_buffer(eap, DOBUF_FIRST, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02004705 if (eap->do_ecmd_cmd != NULL)
4706 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004707 }
4708}
4709
4710/*
4711 * :[N]bmodified [N] to next mod. buffer
4712 * :[N]sbmodified [N] to next mod. buffer
4713 */
4714 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004715ex_bmodified(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004716{
4717 goto_buffer(eap, DOBUF_MOD, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02004718 if (eap->do_ecmd_cmd != NULL)
4719 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004720}
4721
4722/*
4723 * :[N]bnext [N] to next buffer
4724 * :[N]sbnext [N] split and to next buffer
4725 */
4726 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004727ex_bnext(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004728{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01004729 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar3029bcc2020-01-18 15:06:19 +01004730 return;
4731
Bram Moolenaar071d4272004-06-13 20:20:40 +00004732 goto_buffer(eap, DOBUF_CURRENT, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02004733 if (eap->do_ecmd_cmd != NULL)
4734 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004735}
4736
4737/*
4738 * :[N]bNext [N] to previous buffer
4739 * :[N]bprevious [N] to previous buffer
4740 * :[N]sbNext [N] split and to previous buffer
4741 * :[N]sbprevious [N] split and to previous buffer
4742 */
4743 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004744ex_bprevious(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004745{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01004746 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar3029bcc2020-01-18 15:06:19 +01004747 return;
4748
Bram Moolenaar071d4272004-06-13 20:20:40 +00004749 goto_buffer(eap, DOBUF_CURRENT, BACKWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02004750 if (eap->do_ecmd_cmd != NULL)
4751 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004752}
4753
4754/*
4755 * :brewind to first buffer
4756 * :bfirst to first buffer
4757 * :sbrewind split and to first buffer
4758 * :sbfirst split and to first buffer
4759 */
4760 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004761ex_brewind(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004762{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01004763 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar3029bcc2020-01-18 15:06:19 +01004764 return;
4765
Bram Moolenaar071d4272004-06-13 20:20:40 +00004766 goto_buffer(eap, DOBUF_FIRST, FORWARD, 0);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02004767 if (eap->do_ecmd_cmd != NULL)
4768 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004769}
4770
4771/*
4772 * :blast to last buffer
4773 * :sblast split and to last buffer
4774 */
4775 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004776ex_blast(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004777{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01004778 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar3029bcc2020-01-18 15:06:19 +01004779 return;
4780
Bram Moolenaar071d4272004-06-13 20:20:40 +00004781 goto_buffer(eap, DOBUF_LAST, BACKWARD, 0);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02004782 if (eap->do_ecmd_cmd != NULL)
4783 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004784}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004785
Bram Moolenaar7a092242020-04-16 22:10:49 +02004786/*
4787 * Check if "c" ends an Ex command.
Bram Moolenaara494f562020-04-18 17:45:38 +02004788 * In Vim9 script does not check for white space before # or #{.
Bram Moolenaar7a092242020-04-16 22:10:49 +02004789 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004790 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004791ends_excmd(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004792{
Bram Moolenaarfbda69b2020-04-13 15:06:53 +02004793#ifdef FEAT_EVAL
Bram Moolenaar2c330432020-04-13 14:41:35 +02004794 if (c == '#')
Bram Moolenaar7a092242020-04-16 22:10:49 +02004795 return in_vim9script();
4796#endif
4797 return (c == NUL || c == '|' || c == '"' || c == '\n');
4798}
4799
4800/*
4801 * Like ends_excmd() but checks that a # in Vim9 script either has "cmd" equal
4802 * to "cmd_start" or has a white space character before it.
4803 */
4804 int
Bram Moolenaar21cfe502020-04-16 23:01:50 +02004805ends_excmd2(char_u *cmd_start UNUSED, char_u *cmd)
Bram Moolenaar7a092242020-04-16 22:10:49 +02004806{
4807 int c = *cmd;
4808
4809#ifdef FEAT_EVAL
Bram Moolenaara26b9702020-04-18 19:53:28 +02004810 if (c == '#' && cmd[1] != '{' && (cmd == cmd_start || VIM_ISWHITE(cmd[-1])))
Bram Moolenaar2c330432020-04-13 14:41:35 +02004811 return in_vim9script();
Bram Moolenaarfbda69b2020-04-13 15:06:53 +02004812#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004813 return (c == NUL || c == '|' || c == '"' || c == '\n');
4814}
4815
4816#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA) || defined(FEAT_EVAL) \
4817 || defined(PROTO)
4818/*
4819 * Return the next command, after the first '|' or '\n'.
4820 * Return NULL if not found.
4821 */
4822 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004823find_nextcmd(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004824{
4825 while (*p != '|' && *p != '\n')
4826 {
4827 if (*p == NUL)
4828 return NULL;
4829 ++p;
4830 }
4831 return (p + 1);
4832}
4833#endif
4834
4835/*
Bram Moolenaar2256c992016-11-15 21:17:07 +01004836 * Check if *p is a separator between Ex commands, skipping over white space.
4837 * Return NULL if it isn't, the following character if it is.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004838 */
4839 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004840check_nextcmd(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004841{
Bram Moolenaar2256c992016-11-15 21:17:07 +01004842 char_u *s = skipwhite(p);
4843
4844 if (*s == '|' || *s == '\n')
4845 return (s + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004846 else
4847 return NULL;
4848}
4849
4850/*
4851 * - if there are more files to edit
4852 * - and this is the last window
4853 * - and forceit not used
4854 * - and not repeated twice on a row
4855 * return FAIL and give error message if 'message' TRUE
4856 * return OK otherwise
4857 */
4858 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004859check_more(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004860 int message, // when FALSE check only, no messages
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004861 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004862{
4863 int n = ARGCOUNT - curwin->w_arg_idx - 1;
4864
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00004865 if (!forceit && only_one_window()
4866 && ARGCOUNT > 1 && !arg_had_last && n >= 0 && quitmore == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004867 {
4868 if (message)
4869 {
4870#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
4871 if ((p_confirm || cmdmod.confirm) && curbuf->b_fname != NULL)
4872 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02004873 char_u buff[DIALOG_MSG_SIZE];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004874
Bram Moolenaarda6e8912018-08-21 15:12:14 +02004875 vim_snprintf((char *)buff, DIALOG_MSG_SIZE,
4876 NGETTEXT("%d more file to edit. Quit anyway?",
4877 "%d more files to edit. Quit anyway?", n), n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004878 if (vim_dialog_yesno(VIM_QUESTION, NULL, buff, 1) == VIM_YES)
4879 return OK;
4880 return FAIL;
4881 }
4882#endif
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01004883 semsg(NGETTEXT("E173: %d more file to edit",
4884 "E173: %d more files to edit", n), n);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004885 quitmore = 2; // next try to quit is allowed
Bram Moolenaar071d4272004-06-13 20:20:40 +00004886 }
4887 return FAIL;
4888 }
4889 return OK;
4890}
4891
Bram Moolenaar071d4272004-06-13 20:20:40 +00004892/*
4893 * Function given to ExpandGeneric() to obtain the list of command names.
4894 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004895 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004896get_command_name(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004897{
4898 if (idx >= (int)CMD_SIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004899 return get_user_command_name(idx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004900 return cmdnames[idx].cmd_name;
4901}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004902
Bram Moolenaar071d4272004-06-13 20:20:40 +00004903 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004904ex_colorscheme(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004905{
Bram Moolenaare6850792010-05-14 15:28:44 +02004906 if (*eap->arg == NUL)
4907 {
4908#ifdef FEAT_EVAL
4909 char_u *expr = vim_strsave((char_u *)"g:colors_name");
4910 char_u *p = NULL;
4911
4912 if (expr != NULL)
4913 {
4914 ++emsg_off;
4915 p = eval_to_string(expr, NULL, FALSE);
4916 --emsg_off;
4917 vim_free(expr);
4918 }
4919 if (p != NULL)
4920 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01004921 msg((char *)p);
Bram Moolenaare6850792010-05-14 15:28:44 +02004922 vim_free(p);
4923 }
4924 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01004925 msg("default");
Bram Moolenaare6850792010-05-14 15:28:44 +02004926#else
Bram Moolenaar32526b32019-01-19 17:43:09 +01004927 msg(_("unknown"));
Bram Moolenaare6850792010-05-14 15:28:44 +02004928#endif
4929 }
4930 else if (load_colors(eap->arg) == FAIL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004931 semsg(_("E185: Cannot find color scheme '%s'"), eap->arg);
Bram Moolenaarf58d81a2019-01-28 20:19:05 +01004932
4933#ifdef FEAT_VTP
4934 else if (has_vtp_working())
4935 {
4936 // background color change requires clear + redraw
4937 update_screen(CLEAR);
4938 redrawcmd();
4939 }
4940#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004941}
4942
4943 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004944ex_highlight(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004945{
4946 if (*eap->arg == NUL && eap->cmd[2] == '!')
Bram Moolenaar32526b32019-01-19 17:43:09 +01004947 msg(_("Greetings, Vim user!"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004948 do_highlight(eap->arg, eap->forceit, FALSE);
4949}
4950
4951
4952/*
4953 * Call this function if we thought we were going to exit, but we won't
4954 * (because of an error). May need to restore the terminal mode.
4955 */
4956 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004957not_exiting(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004958{
4959 exiting = FALSE;
4960 settmode(TMODE_RAW);
4961}
4962
Bram Moolenaar12a96de2018-03-11 14:44:18 +01004963 static int
4964before_quit_autocmds(win_T *wp, int quit_all, int forceit)
4965{
4966 apply_autocmds(EVENT_QUITPRE, NULL, NULL, FALSE, wp->w_buffer);
4967
Bram Moolenaar34ba06b2019-10-20 22:27:10 +02004968 // Bail out when autocommands closed the window.
4969 // Refuse to quit when the buffer in the last window is being closed (can
4970 // only happen in autocommands).
Bram Moolenaar12a96de2018-03-11 14:44:18 +01004971 if (!win_valid(wp)
4972 || curbuf_locked()
4973 || (wp->w_buffer->b_nwindows == 1 && wp->w_buffer->b_locked > 0))
4974 return TRUE;
4975
4976 if (quit_all || (check_more(FALSE, forceit) == OK && only_one_window()))
4977 {
4978 apply_autocmds(EVENT_EXITPRE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar34ba06b2019-10-20 22:27:10 +02004979 // Refuse to quit when locked or when the window was closed or the
4980 // buffer in the last window is being closed (can only happen in
4981 // autocommands).
4982 if (!win_valid(wp) || curbuf_locked()
Bram Moolenaar12a96de2018-03-11 14:44:18 +01004983 || (curbuf->b_nwindows == 1 && curbuf->b_locked > 0))
4984 return TRUE;
4985 }
4986
4987 return FALSE;
4988}
4989
Bram Moolenaar071d4272004-06-13 20:20:40 +00004990/*
Bram Moolenaarf240e182014-11-27 18:33:02 +01004991 * ":quit": quit current window, quit Vim if the last window is closed.
Bram Moolenaar12a96de2018-03-11 14:44:18 +01004992 * ":{nr}quit": quit window {nr}
Bram Moolenaar4d14bac2019-10-20 21:15:15 +02004993 * Also used when closing a terminal window that's the last one.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004994 */
Bram Moolenaar4d14bac2019-10-20 21:15:15 +02004995 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004996ex_quit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004997{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004998 win_T *wp;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004999
Bram Moolenaar071d4272004-06-13 20:20:40 +00005000#ifdef FEAT_CMDWIN
5001 if (cmdwin_type != 0)
5002 {
5003 cmdwin_result = Ctrl_C;
5004 return;
5005 }
5006#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005007 // Don't quit while editing the command line.
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005008 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005009 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005010 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005011 return;
5012 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005013 if (eap->addr_count > 0)
5014 {
Bram Moolenaarf240e182014-11-27 18:33:02 +01005015 int wnr = eap->line2;
5016
5017 for (wp = firstwin; wp->w_next != NULL; wp = wp->w_next)
5018 if (--wnr <= 0)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005019 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005020 }
5021 else
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005022 wp = curwin;
Bram Moolenaarf240e182014-11-27 18:33:02 +01005023
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005024 // Refuse to quit when locked.
Bram Moolenaar87ffb5c2017-10-19 12:37:42 +02005025 if (curbuf_locked())
5026 return;
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005027
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005028 // Trigger QuitPre and maybe ExitPre
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005029 if (before_quit_autocmds(wp, FALSE, eap->forceit))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005030 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005031
5032#ifdef FEAT_NETBEANS_INTG
5033 netbeansForcedQuit = eap->forceit;
5034#endif
5035
5036 /*
5037 * If there are more files or windows we won't exit.
5038 */
5039 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
5040 exiting = TRUE;
Bram Moolenaarff930ca2017-10-19 17:12:10 +02005041 if ((!buf_hide(wp->w_buffer)
5042 && check_changed(wp->w_buffer, (p_awa ? CCGD_AW : 0)
Bram Moolenaar45d3b142013-11-09 03:31:51 +01005043 | (eap->forceit ? CCGD_FORCEIT : 0)
5044 | CCGD_EXCMD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005045 || check_more(TRUE, eap->forceit) == FAIL
Bram Moolenaar027387f2016-01-02 22:25:52 +01005046 || (only_one_window() && check_changed_any(eap->forceit, TRUE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005047 {
5048 not_exiting();
5049 }
5050 else
5051 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005052 // quit last window
5053 // Note: only_one_window() returns true, even so a help window is
5054 // still open. In that case only quit, if no address has been
5055 // specified. Example:
5056 // :h|wincmd w|1q - don't quit
5057 // :h|wincmd w|q - quit
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01005058 if (only_one_window() && (ONE_WINDOW || eap->addr_count == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005059 getout(0);
Bram Moolenaar2c33d7b2017-10-14 16:06:20 +02005060 not_exiting();
Bram Moolenaar4033c552017-09-16 20:54:51 +02005061#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00005062 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005063#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005064 // close window; may free buffer
Bram Moolenaareb44a682017-08-03 22:44:55 +02005065 win_close(wp, !buf_hide(wp->w_buffer) || eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005066 }
5067}
5068
5069/*
5070 * ":cquit".
5071 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005072 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005073ex_cquit(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005074{
Bram Moolenaar1860bde2020-01-06 21:47:21 +01005075 // this does not always pass on the exit code to the Manx compiler. why?
5076 getout(eap->addr_count > 0 ? (int)eap->line2 : EXIT_FAILURE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005077}
5078
5079/*
5080 * ":qall": try to quit all windows
5081 */
5082 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005083ex_quit_all(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005084{
5085# ifdef FEAT_CMDWIN
5086 if (cmdwin_type != 0)
5087 {
5088 if (eap->forceit)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005089 cmdwin_result = K_XF1; // ex_window() takes care of this
Bram Moolenaar071d4272004-06-13 20:20:40 +00005090 else
5091 cmdwin_result = K_XF2;
5092 return;
5093 }
5094# endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005095
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005096 // Don't quit while editing the command line.
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005097 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005098 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005099 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005100 return;
5101 }
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005102
5103 if (before_quit_autocmds(curwin, TRUE, eap->forceit))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005104 return;
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005105
Bram Moolenaar071d4272004-06-13 20:20:40 +00005106 exiting = TRUE;
Bram Moolenaar027387f2016-01-02 22:25:52 +01005107 if (eap->forceit || !check_changed_any(FALSE, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005108 getout(0);
5109 not_exiting();
5110}
5111
Bram Moolenaar071d4272004-06-13 20:20:40 +00005112/*
5113 * ":close": close current window, unless it is the last one
5114 */
5115 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005116ex_close(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005117{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005118 win_T *win;
5119 int winnr = 0;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005120#ifdef FEAT_CMDWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00005121 if (cmdwin_type != 0)
Bram Moolenaar9bd1a7e2011-05-19 14:50:54 +02005122 cmdwin_result = Ctrl_C;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005123 else
Bram Moolenaar4033c552017-09-16 20:54:51 +02005124#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005125 if (!text_locked() && !curbuf_locked())
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005126 {
5127 if (eap->addr_count == 0)
5128 ex_win_close(eap->forceit, curwin, NULL);
Bram Moolenaar6d41c782018-06-06 09:11:12 +02005129 else
5130 {
Bram Moolenaar29323592016-07-24 22:04:11 +02005131 FOR_ALL_WINDOWS(win)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005132 {
5133 winnr++;
5134 if (winnr == eap->line2)
5135 break;
5136 }
5137 if (win == NULL)
5138 win = lastwin;
5139 ex_win_close(eap->forceit, win, NULL);
5140 }
5141 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005142}
5143
Bram Moolenaar4033c552017-09-16 20:54:51 +02005144#ifdef FEAT_QUICKFIX
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005145/*
5146 * ":pclose": Close any preview window.
5147 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005148 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005149ex_pclose(exarg_T *eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005150{
5151 win_T *win;
5152
Bram Moolenaar79648732019-07-18 21:43:07 +02005153 // First close any normal window.
Bram Moolenaar29323592016-07-24 22:04:11 +02005154 FOR_ALL_WINDOWS(win)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005155 if (win->w_p_pvw)
5156 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00005157 ex_win_close(eap->forceit, win, NULL);
Bram Moolenaar79648732019-07-18 21:43:07 +02005158 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005159 }
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01005160# ifdef FEAT_PROP_POPUP
Bram Moolenaar79648732019-07-18 21:43:07 +02005161 // Also when 'previewpopup' is empty, it might have been cleared.
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02005162 popup_close_preview();
Bram Moolenaar79648732019-07-18 21:43:07 +02005163# endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005164}
Bram Moolenaar4033c552017-09-16 20:54:51 +02005165#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005166
Bram Moolenaarf740b292006-02-16 22:11:02 +00005167/*
5168 * Close window "win" and take care of handling closing the last window for a
5169 * modified buffer.
5170 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005171 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005172ex_win_close(
5173 int forceit,
5174 win_T *win,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005175 tabpage_T *tp) // NULL or the tab page "win" is in
Bram Moolenaar071d4272004-06-13 20:20:40 +00005176{
5177 int need_hide;
5178 buf_T *buf = win->w_buffer;
5179
5180 need_hide = (bufIsChanged(buf) && buf->b_nwindows <= 1);
Bram Moolenaareb44a682017-08-03 22:44:55 +02005181 if (need_hide && !buf_hide(buf) && !forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005182 {
Bram Moolenaar4033c552017-09-16 20:54:51 +02005183#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005184 if ((p_confirm || cmdmod.confirm) && p_write)
5185 {
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005186 bufref_T bufref;
5187
5188 set_bufref(&bufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005189 dialog_changed(buf, FALSE);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005190 if (bufref_valid(&bufref) && bufIsChanged(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005191 return;
5192 need_hide = FALSE;
5193 }
5194 else
Bram Moolenaar4033c552017-09-16 20:54:51 +02005195#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005196 {
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02005197 no_write_message();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005198 return;
5199 }
5200 }
5201
Bram Moolenaar4033c552017-09-16 20:54:51 +02005202#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00005203 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005204#endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00005205
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005206 // free buffer when not hiding it or when it's a scratch buffer
Bram Moolenaarf740b292006-02-16 22:11:02 +00005207 if (tp == NULL)
Bram Moolenaareb44a682017-08-03 22:44:55 +02005208 win_close(win, !need_hide && !buf_hide(buf));
Bram Moolenaarf740b292006-02-16 22:11:02 +00005209 else
Bram Moolenaareb44a682017-08-03 22:44:55 +02005210 win_close_othertab(win, !need_hide && !buf_hide(buf), tp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005211}
5212
Bram Moolenaar071d4272004-06-13 20:20:40 +00005213/*
Bram Moolenaardea25702017-01-29 15:18:10 +01005214 * Handle the argument for a tabpage related ex command.
5215 * Returns a tabpage number.
5216 * When an error is encountered then eap->errmsg is set.
5217 */
5218 static int
5219get_tabpage_arg(exarg_T *eap)
5220{
5221 int tab_number;
5222 int unaccept_arg0 = (eap->cmdidx == CMD_tabmove) ? 0 : 1;
5223
5224 if (eap->arg && *eap->arg != NUL)
5225 {
5226 char_u *p = eap->arg;
5227 char_u *p_save;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005228 int relative = 0; // argument +N/-N means: go to N places to the
5229 // right/left relative to the current position.
Bram Moolenaardea25702017-01-29 15:18:10 +01005230
5231 if (*p == '-')
5232 {
5233 relative = -1;
5234 p++;
5235 }
5236 else if (*p == '+')
5237 {
5238 relative = 1;
5239 p++;
5240 }
5241
5242 p_save = p;
5243 tab_number = getdigits(&p);
5244
5245 if (relative == 0)
5246 {
5247 if (STRCMP(p, "$") == 0)
5248 tab_number = LAST_TAB_NR;
5249 else if (p == p_save || *p_save == '-' || *p != NUL
5250 || tab_number > LAST_TAB_NR)
5251 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005252 // No numbers as argument.
Bram Moolenaardea25702017-01-29 15:18:10 +01005253 eap->errmsg = e_invarg;
5254 goto theend;
5255 }
5256 }
5257 else
5258 {
5259 if (*p_save == NUL)
5260 tab_number = 1;
5261 else if (p == p_save || *p_save == '-' || *p != NUL
5262 || tab_number == 0)
5263 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005264 // No numbers as argument.
Bram Moolenaardea25702017-01-29 15:18:10 +01005265 eap->errmsg = e_invarg;
5266 goto theend;
5267 }
5268 tab_number = tab_number * relative + tabpage_index(curtab);
5269 if (!unaccept_arg0 && relative == -1)
5270 --tab_number;
5271 }
5272 if (tab_number < unaccept_arg0 || tab_number > LAST_TAB_NR)
5273 eap->errmsg = e_invarg;
5274 }
5275 else if (eap->addr_count > 0)
5276 {
5277 if (unaccept_arg0 && eap->line2 == 0)
Bram Moolenaarc6251552017-01-29 21:42:20 +01005278 {
Bram Moolenaardea25702017-01-29 15:18:10 +01005279 eap->errmsg = e_invrange;
Bram Moolenaarc6251552017-01-29 21:42:20 +01005280 tab_number = 0;
5281 }
Bram Moolenaardea25702017-01-29 15:18:10 +01005282 else
5283 {
5284 tab_number = eap->line2;
Bram Moolenaar82a12462019-01-22 22:41:42 +01005285 if (!unaccept_arg0 && *skipwhite(*eap->cmdlinep) == '-')
Bram Moolenaardea25702017-01-29 15:18:10 +01005286 {
5287 --tab_number;
5288 if (tab_number < unaccept_arg0)
5289 eap->errmsg = e_invarg;
5290 }
5291 }
5292 }
5293 else
5294 {
5295 switch (eap->cmdidx)
5296 {
5297 case CMD_tabnext:
5298 tab_number = tabpage_index(curtab) + 1;
5299 if (tab_number > LAST_TAB_NR)
5300 tab_number = 1;
5301 break;
5302 case CMD_tabmove:
5303 tab_number = LAST_TAB_NR;
5304 break;
5305 default:
5306 tab_number = tabpage_index(curtab);
5307 }
5308 }
5309
5310theend:
5311 return tab_number;
5312}
5313
5314/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00005315 * ":tabclose": close current tab page, unless it is the last one.
5316 * ":tabclose N": close tab page N.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005317 */
5318 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005319ex_tabclose(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005320{
Bram Moolenaarf740b292006-02-16 22:11:02 +00005321 tabpage_T *tp;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005322 int tab_number;
Bram Moolenaarf740b292006-02-16 22:11:02 +00005323
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005324# ifdef FEAT_CMDWIN
5325 if (cmdwin_type != 0)
5326 cmdwin_result = K_IGNORE;
5327 else
5328# endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00005329 if (first_tabpage->tp_next == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005330 emsg(_("E784: Cannot close last tab page"));
Bram Moolenaarf740b292006-02-16 22:11:02 +00005331 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005332 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005333 tab_number = get_tabpage_arg(eap);
5334 if (eap->errmsg == NULL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005335 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005336 tp = find_tabpage(tab_number);
Bram Moolenaarf740b292006-02-16 22:11:02 +00005337 if (tp == NULL)
5338 {
5339 beep_flush();
5340 return;
5341 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005342 if (tp != curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00005343 {
5344 tabpage_close_other(tp, eap->forceit);
5345 return;
5346 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005347 else if (!text_locked() && !curbuf_locked())
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005348 tabpage_close(eap->forceit);
5349 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005350 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005351}
5352
5353/*
5354 * ":tabonly": close all tab pages except the current one
5355 */
5356 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005357ex_tabonly(exarg_T *eap)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005358{
5359 tabpage_T *tp;
5360 int done;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005361 int tab_number;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005362
5363# ifdef FEAT_CMDWIN
5364 if (cmdwin_type != 0)
5365 cmdwin_result = K_IGNORE;
5366 else
5367# endif
5368 if (first_tabpage->tp_next == NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01005369 msg(_("Already only one tab page"));
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005370 else
5371 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005372 tab_number = get_tabpage_arg(eap);
5373 if (eap->errmsg == NULL)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005374 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005375 goto_tabpage(tab_number);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005376 // Repeat this up to a 1000 times, because autocommands may
5377 // mess up the lists.
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005378 for (done = 0; done < 1000; ++done)
5379 {
5380 FOR_ALL_TABPAGES(tp)
5381 if (tp->tp_topframe != topframe)
5382 {
5383 tabpage_close_other(tp, eap->forceit);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005384 // if we failed to close it quit
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005385 if (valid_tabpage(tp))
5386 done = 1000;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005387 // start over, "tp" is now invalid
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005388 break;
5389 }
5390 if (first_tabpage->tp_next == NULL)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005391 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005392 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005393 }
5394 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005395}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005396
5397/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00005398 * Close the current tab page.
5399 */
5400 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005401tabpage_close(int forceit)
Bram Moolenaarf740b292006-02-16 22:11:02 +00005402{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005403 // First close all the windows but the current one. If that worked then
5404 // close the last window in this tab, that will close it.
Bram Moolenaar459ca562016-11-10 18:16:33 +01005405 if (!ONE_WINDOW)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005406 close_others(TRUE, forceit);
Bram Moolenaar459ca562016-11-10 18:16:33 +01005407 if (ONE_WINDOW)
Bram Moolenaarf740b292006-02-16 22:11:02 +00005408 ex_win_close(forceit, curwin, NULL);
5409# ifdef FEAT_GUI
5410 need_mouse_correct = TRUE;
5411# endif
5412}
5413
5414/*
5415 * Close tab page "tp", which is not the current tab page.
5416 * Note that autocommands may make "tp" invalid.
Bram Moolenaar7875acc2006-09-10 13:51:17 +00005417 * Also takes care of the tab pages line disappearing when closing the
5418 * last-but-one tab page.
Bram Moolenaarf740b292006-02-16 22:11:02 +00005419 */
5420 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005421tabpage_close_other(tabpage_T *tp, int forceit)
Bram Moolenaarf740b292006-02-16 22:11:02 +00005422{
5423 int done = 0;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005424 win_T *wp;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00005425 int h = tabline_height();
Bram Moolenaarf740b292006-02-16 22:11:02 +00005426
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005427 // Limit to 1000 windows, autocommands may add a window while we close
5428 // one. OK, so I'm paranoid...
Bram Moolenaarf740b292006-02-16 22:11:02 +00005429 while (++done < 1000)
5430 {
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005431 wp = tp->tp_firstwin;
5432 ex_win_close(forceit, wp, tp);
Bram Moolenaarf740b292006-02-16 22:11:02 +00005433
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005434 // Autocommands may delete the tab page under our fingers and we may
5435 // fail to close a window with a modified buffer.
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005436 if (!valid_tabpage(tp) || tp->tp_firstwin == wp)
Bram Moolenaarf740b292006-02-16 22:11:02 +00005437 break;
5438 }
Bram Moolenaar7875acc2006-09-10 13:51:17 +00005439
Bram Moolenaar29323592016-07-24 22:04:11 +02005440 apply_autocmds(EVENT_TABCLOSED, NULL, NULL, FALSE, curbuf);
Bram Moolenaar29323592016-07-24 22:04:11 +02005441
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005442 redraw_tabline = TRUE;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00005443 if (h != tabline_height())
5444 shell_new_rows();
Bram Moolenaarf740b292006-02-16 22:11:02 +00005445}
5446
5447/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005448 * ":only".
5449 */
5450 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005451ex_only(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005452{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005453 win_T *wp;
5454 int wnr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005455# ifdef FEAT_GUI
5456 need_mouse_correct = TRUE;
5457# endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005458 if (eap->addr_count > 0)
5459 {
5460 wnr = eap->line2;
5461 for (wp = firstwin; --wnr > 0; )
5462 {
5463 if (wp->w_next == NULL)
5464 break;
5465 else
5466 wp = wp->w_next;
5467 }
5468 win_goto(wp);
5469 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005470 close_others(TRUE, eap->forceit);
5471}
5472
Bram Moolenaar071d4272004-06-13 20:20:40 +00005473 static void
Bram Moolenaar5e1e6d22017-01-02 17:26:00 +01005474ex_hide(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005475{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005476 // ":hide" or ":hide | cmd": hide current window
Bram Moolenaar2256c992016-11-15 21:17:07 +01005477 if (!eap->skip)
5478 {
Bram Moolenaar4033c552017-09-16 20:54:51 +02005479#ifdef FEAT_GUI
Bram Moolenaar2256c992016-11-15 21:17:07 +01005480 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005481#endif
Bram Moolenaar2256c992016-11-15 21:17:07 +01005482 if (eap->addr_count == 0)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005483 win_close(curwin, FALSE); // don't free buffer
Bram Moolenaar2256c992016-11-15 21:17:07 +01005484 else
5485 {
5486 int winnr = 0;
5487 win_T *win;
Bram Moolenaarf240e182014-11-27 18:33:02 +01005488
Bram Moolenaar2256c992016-11-15 21:17:07 +01005489 FOR_ALL_WINDOWS(win)
5490 {
5491 winnr++;
5492 if (winnr == eap->line2)
5493 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005494 }
Bram Moolenaar2256c992016-11-15 21:17:07 +01005495 if (win == NULL)
5496 win = lastwin;
5497 win_close(win, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005498 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005499 }
5500}
5501
5502/*
5503 * ":stop" and ":suspend": Suspend Vim.
5504 */
5505 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005506ex_stop(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005507{
5508 /*
5509 * Disallow suspending for "rvim".
5510 */
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005511 if (!check_restricted())
Bram Moolenaar071d4272004-06-13 20:20:40 +00005512 {
5513 if (!eap->forceit)
5514 autowrite_all();
5515 windgoto((int)Rows - 1, 0);
5516 out_char('\n');
5517 out_flush();
5518 stoptermcap();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005519 out_flush(); // needed for SUN to restore xterm buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00005520#ifdef FEAT_TITLE
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005521 mch_restore_title(SAVE_RESTORE_BOTH); // restore window titles
Bram Moolenaar071d4272004-06-13 20:20:40 +00005522#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005523 ui_suspend(); // call machine specific function
Bram Moolenaar071d4272004-06-13 20:20:40 +00005524#ifdef FEAT_TITLE
5525 maketitle();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005526 resettitle(); // force updating the title
Bram Moolenaar071d4272004-06-13 20:20:40 +00005527#endif
5528 starttermcap();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005529 scroll_start(); // scroll screen before redrawing
Bram Moolenaar071d4272004-06-13 20:20:40 +00005530 redraw_later_clear();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005531 shell_resized(); // may have resized window
Bram Moolenaar071d4272004-06-13 20:20:40 +00005532 }
5533}
5534
5535/*
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005536 * ":exit", ":xit" and ":wq": Write file and quite the current window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005537 */
5538 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005539ex_exit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005540{
5541#ifdef FEAT_CMDWIN
5542 if (cmdwin_type != 0)
5543 {
5544 cmdwin_result = Ctrl_C;
5545 return;
5546 }
5547#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005548 // Don't quit while editing the command line.
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005549 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005550 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005551 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005552 return;
5553 }
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005554
5555 if (before_quit_autocmds(curwin, FALSE, eap->forceit))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005556 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005557
5558 /*
5559 * if more files or windows we won't exit
5560 */
5561 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
5562 exiting = TRUE;
5563 if ( ((eap->cmdidx == CMD_wq
5564 || curbufIsChanged())
5565 && do_write(eap) == FAIL)
5566 || check_more(TRUE, eap->forceit) == FAIL
Bram Moolenaar027387f2016-01-02 22:25:52 +01005567 || (only_one_window() && check_changed_any(eap->forceit, FALSE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005568 {
5569 not_exiting();
5570 }
5571 else
5572 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005573 if (only_one_window()) // quit last window, exit Vim
Bram Moolenaar071d4272004-06-13 20:20:40 +00005574 getout(0);
Bram Moolenaar2c33d7b2017-10-14 16:06:20 +02005575 not_exiting();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005576# ifdef FEAT_GUI
5577 need_mouse_correct = TRUE;
5578# endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005579 // Quit current window, may free the buffer.
Bram Moolenaareb44a682017-08-03 22:44:55 +02005580 win_close(curwin, !buf_hide(curwin->w_buffer));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005581 }
5582}
5583
5584/*
5585 * ":print", ":list", ":number".
5586 */
5587 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005588ex_print(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005589{
Bram Moolenaardf177f62005-02-22 08:39:57 +00005590 if (curbuf->b_ml.ml_flags & ML_EMPTY)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005591 emsg(_(e_emptybuf));
Bram Moolenaardf177f62005-02-22 08:39:57 +00005592 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005593 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00005594 for ( ;!got_int; ui_breakcheck())
5595 {
5596 print_line(eap->line1,
5597 (eap->cmdidx == CMD_number || eap->cmdidx == CMD_pound
5598 || (eap->flags & EXFLAG_NR)),
5599 eap->cmdidx == CMD_list || (eap->flags & EXFLAG_LIST));
5600 if (++eap->line1 > eap->line2)
5601 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005602 out_flush(); // show one line at a time
Bram Moolenaardf177f62005-02-22 08:39:57 +00005603 }
5604 setpcmark();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005605 // put cursor at last line
Bram Moolenaardf177f62005-02-22 08:39:57 +00005606 curwin->w_cursor.lnum = eap->line2;
5607 beginline(BL_SOL | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005608 }
5609
Bram Moolenaar071d4272004-06-13 20:20:40 +00005610 ex_no_reprint = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005611}
5612
5613#ifdef FEAT_BYTEOFF
5614 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005615ex_goto(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005616{
5617 goto_byte(eap->line2);
5618}
5619#endif
5620
5621/*
5622 * ":shell".
5623 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005624 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005625ex_shell(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005626{
5627 do_shell(NULL, 0);
5628}
5629
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005630#if defined(HAVE_DROP_FILE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005631
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005632static int drop_busy = FALSE;
5633static int drop_filec;
5634static char_u **drop_filev = NULL;
5635static int drop_split;
5636static void (*drop_callback)(void *);
5637static void *drop_cookie;
5638
5639 static void
5640handle_drop_internal(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005641{
5642 exarg_T ea;
5643 int save_msg_scroll = msg_scroll;
5644
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005645 // Setting the argument list may cause screen updates and being called
5646 // recursively. Avoid that by setting drop_busy.
5647 drop_busy = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005648
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005649 // Check whether the current buffer is changed. If so, we will need
5650 // to split the current window or data could be lost.
5651 // We don't need to check if the 'hidden' option is set, as in this
5652 // case the buffer won't be lost.
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005653 if (!buf_hide(curbuf) && !drop_split)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005654 {
5655 ++emsg_off;
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005656 drop_split = check_changed(curbuf, CCGD_AW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005657 --emsg_off;
5658 }
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005659 if (drop_split)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005660 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005661 if (win_split(0, 0) == FAIL)
5662 return;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02005663 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005664
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005665 // When splitting the window, create a new alist. Otherwise the
5666 // existing one is overwritten.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005667 alist_unlink(curwin->w_alist);
5668 alist_new();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005669 }
5670
5671 /*
5672 * Set up the new argument list.
5673 */
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005674 alist_set(ALIST(curwin), drop_filec, drop_filev, FALSE, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005675
5676 /*
5677 * Move to the first file.
5678 */
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005679 // Fake up a minimal "next" command for do_argfile()
Bram Moolenaara80faa82020-04-12 19:37:17 +02005680 CLEAR_FIELD(ea);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005681 ea.cmd = (char_u *)"next";
5682 do_argfile(&ea, 0);
5683
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005684 // do_ecmd() may set need_start_insertmode, but since we never left Insert
5685 // mode that is not needed here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005686 need_start_insertmode = FALSE;
5687
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005688 // Restore msg_scroll, otherwise a following command may cause scrolling
5689 // unexpectedly. The screen will be redrawn by the caller, thus
5690 // msg_scroll being set by displaying a message is irrelevant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005691 msg_scroll = save_msg_scroll;
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005692
5693 if (drop_callback != NULL)
5694 drop_callback(drop_cookie);
5695
5696 drop_filev = NULL;
5697 drop_busy = FALSE;
5698}
5699
5700/*
5701 * Handle a file drop. The code is here because a drop is *nearly* like an
5702 * :args command, but not quite (we have a list of exact filenames, so we
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01005703 * don't want to (a) parse a command line, or (b) expand wildcards). So the
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005704 * code is very similar to :args and hence needs access to a lot of the static
5705 * functions in this file.
5706 *
5707 * The "filev" list must have been allocated using alloc(), as should each item
5708 * in the list. This function takes over responsibility for freeing the "filev"
5709 * list.
5710 */
5711 void
5712handle_drop(
5713 int filec, // the number of files dropped
5714 char_u **filev, // the list of files dropped
5715 int split, // force splitting the window
5716 void (*callback)(void *), // to be called after setting the argument
5717 // list
5718 void *cookie) // argument for "callback" (allocated)
5719{
5720 // Cannot handle recursive drops, finish the pending one.
5721 if (drop_busy)
5722 {
5723 FreeWild(filec, filev);
5724 vim_free(cookie);
5725 return;
5726 }
5727
5728 // When calling handle_drop() more than once in a row we only use the last
5729 // one.
5730 if (drop_filev != NULL)
5731 {
5732 FreeWild(drop_filec, drop_filev);
5733 vim_free(drop_cookie);
5734 }
5735
5736 drop_filec = filec;
5737 drop_filev = filev;
5738 drop_split = split;
5739 drop_callback = callback;
5740 drop_cookie = cookie;
5741
5742 // Postpone this when:
5743 // - editing the command line
5744 // - not possible to change the current buffer
5745 // - updating the screen
5746 // As it may change buffers and window structures that are in use and cause
5747 // freed memory to be used.
5748 if (text_locked() || curbuf_locked() || updating_screen)
5749 return;
5750
5751 handle_drop_internal();
5752}
5753
5754/*
5755 * To be called when text is unlocked, curbuf is unlocked or updating_screen is
5756 * reset: Handle a postponed drop.
5757 */
5758 void
5759handle_any_postponed_drop(void)
5760{
5761 if (!drop_busy && drop_filev != NULL
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +02005762 && !text_locked() && !curbuf_locked() && !updating_screen)
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005763 handle_drop_internal();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005764}
5765#endif
5766
Bram Moolenaar071d4272004-06-13 20:20:40 +00005767/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005768 * ":preserve".
5769 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005770 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005771ex_preserve(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005772{
Bram Moolenaar4399ef42005-02-12 14:29:27 +00005773 curbuf->b_flags |= BF_PRESERVED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005774 ml_preserve(curbuf, TRUE);
5775}
5776
5777/*
5778 * ":recover".
5779 */
5780 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005781ex_recover(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005782{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005783 // Set recoverymode right away to avoid the ATTENTION prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005784 recoverymode = TRUE;
Bram Moolenaar45d3b142013-11-09 03:31:51 +01005785 if (!check_changed(curbuf, (p_awa ? CCGD_AW : 0)
5786 | CCGD_MULTWIN
5787 | (eap->forceit ? CCGD_FORCEIT : 0)
5788 | CCGD_EXCMD)
5789
Bram Moolenaar071d4272004-06-13 20:20:40 +00005790 && (*eap->arg == NUL
5791 || setfname(curbuf, eap->arg, NULL, TRUE) == OK))
Bram Moolenaar99499b12019-05-23 21:35:48 +02005792 ml_recover(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005793 recoverymode = FALSE;
5794}
5795
5796/*
5797 * Command modifier used in a wrong way.
5798 */
5799 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005800ex_wrongmodifier(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005801{
5802 eap->errmsg = e_invcmd;
5803}
5804
Bram Moolenaar071d4272004-06-13 20:20:40 +00005805/*
5806 * :sview [+command] file split window with new file, read-only
5807 * :split [[+command] file] split window with current or new file
5808 * :vsplit [[+command] file] split window vertically with current or new file
5809 * :new [[+command] file] split window with no or new file
5810 * :vnew [[+command] file] split vertically window with no or new file
5811 * :sfind [+command] file split window with file in 'path'
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005812 *
5813 * :tabedit open new Tab page with empty window
5814 * :tabedit [+command] file open new Tab page and edit "file"
5815 * :tabnew [[+command] file] just like :tabedit
5816 * :tabfind [+command] file open new Tab page and find "file"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005817 */
5818 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005819ex_splitview(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005820{
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005821 win_T *old_curwin = curwin;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005822#if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005823 char_u *fname = NULL;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005824#endif
5825#ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00005826 int browse_flag = cmdmod.browse;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005827#endif
Bram Moolenaar39902a02018-06-21 22:10:08 +02005828 int use_tab = eap->cmdidx == CMD_tabedit
5829 || eap->cmdidx == CMD_tabfind
5830 || eap->cmdidx == CMD_tabnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005831
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01005832 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02005833 return;
5834
Bram Moolenaar4033c552017-09-16 20:54:51 +02005835#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00005836 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005837#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005838
Bram Moolenaar4033c552017-09-16 20:54:51 +02005839#ifdef FEAT_QUICKFIX
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005840 // A ":split" in the quickfix window works like ":new". Don't want two
5841 // quickfix windows. But it's OK when doing ":tab split".
Bram Moolenaar05bb9532008-07-04 09:44:11 +00005842 if (bt_quickfix(curbuf) && cmdmod.tab == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005843 {
5844 if (eap->cmdidx == CMD_split)
5845 eap->cmdidx = CMD_new;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005846 if (eap->cmdidx == CMD_vsplit)
5847 eap->cmdidx = CMD_vnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005848 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02005849#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005850
Bram Moolenaar4033c552017-09-16 20:54:51 +02005851#ifdef FEAT_SEARCHPATH
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005852 if (eap->cmdidx == CMD_sfind || eap->cmdidx == CMD_tabfind)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005853 {
5854 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg),
5855 FNAME_MESS, TRUE, curbuf->b_ffname);
5856 if (fname == NULL)
5857 goto theend;
5858 eap->arg = fname;
5859 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005860# ifdef FEAT_BROWSE
Bram Moolenaar4033c552017-09-16 20:54:51 +02005861 else
5862# endif
5863#endif
5864#ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00005865 if (cmdmod.browse
Bram Moolenaar071d4272004-06-13 20:20:40 +00005866 && eap->cmdidx != CMD_vnew
Bram Moolenaar071d4272004-06-13 20:20:40 +00005867 && eap->cmdidx != CMD_new)
5868 {
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00005869 if (
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005870# ifdef FEAT_GUI
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00005871 !gui.in_use &&
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005872# endif
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00005873 au_has_group((char_u *)"FileExplorer"))
5874 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005875 // No browsing supported but we do have the file explorer:
5876 // Edit the directory.
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00005877 if (*eap->arg == NUL || !mch_isdir(eap->arg))
5878 eap->arg = (char_u *)".";
5879 }
5880 else
5881 {
Bram Moolenaar39902a02018-06-21 22:10:08 +02005882 fname = do_browse(0, (char_u *)(use_tab
5883 ? _("Edit File in new tab page")
5884 : _("Edit File in new window")),
Bram Moolenaar071d4272004-06-13 20:20:40 +00005885 eap->arg, NULL, NULL, NULL, curbuf);
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00005886 if (fname == NULL)
5887 goto theend;
5888 eap->arg = fname;
5889 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005890 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005891 cmdmod.browse = FALSE; // Don't browse again in do_ecmd().
Bram Moolenaar4033c552017-09-16 20:54:51 +02005892#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005893
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005894 /*
5895 * Either open new tab page or split the window.
5896 */
Bram Moolenaar39902a02018-06-21 22:10:08 +02005897 if (use_tab)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005898 {
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005899 if (win_new_tabpage(cmdmod.tab != 0 ? cmdmod.tab
5900 : eap->addr_count == 0 ? 0
5901 : (int)eap->line2 + 1) != FAIL)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005902 {
Bram Moolenaard2b66012008-01-09 19:30:36 +00005903 do_exedit(eap, old_curwin);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005904
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005905 // set the alternate buffer for the window we came from
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005906 if (curwin != old_curwin
5907 && win_valid(old_curwin)
5908 && old_curwin->w_buffer != curbuf
5909 && !cmdmod.keepalt)
5910 old_curwin->w_alt_fnum = curbuf->b_fnum;
5911 }
5912 }
5913 else if (win_split(eap->addr_count > 0 ? (int)eap->line2 : 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005914 *eap->cmd == 'v' ? WSP_VERT : 0) != FAIL)
5915 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005916 // Reset 'scrollbind' when editing another file, but keep it when
5917 // doing ":split" without arguments.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005918 if (*eap->arg != NUL
Bram Moolenaar8a3bb562018-03-04 20:14:14 +01005919# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00005920 || cmdmod.browse
Bram Moolenaar8a3bb562018-03-04 20:14:14 +01005921# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005922 )
Bram Moolenaar3368ea22010-09-21 16:56:35 +02005923 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005924 else
5925 do_check_scrollbind(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005926 do_exedit(eap, old_curwin);
5927 }
5928
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005929# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00005930 cmdmod.browse = browse_flag;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005931# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005932
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005933# if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005934theend:
5935 vim_free(fname);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005936# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005937}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005938
5939/*
Bram Moolenaar80a94a52006-02-23 21:26:58 +00005940 * Open a new tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005941 */
5942 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005943tabpage_new(void)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00005944{
5945 exarg_T ea;
5946
Bram Moolenaara80faa82020-04-12 19:37:17 +02005947 CLEAR_FIELD(ea);
Bram Moolenaar80a94a52006-02-23 21:26:58 +00005948 ea.cmdidx = CMD_tabnew;
5949 ea.cmd = (char_u *)"tabn";
5950 ea.arg = (char_u *)"";
5951 ex_splitview(&ea);
5952}
5953
5954/*
5955 * :tabnext command
5956 */
5957 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005958ex_tabnext(exarg_T *eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005959{
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005960 int tab_number;
5961
Bram Moolenaar8cdbd5b2019-06-16 15:50:45 +02005962 if (ERROR_IF_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02005963 return;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005964 switch (eap->cmdidx)
5965 {
5966 case CMD_tabfirst:
5967 case CMD_tabrewind:
5968 goto_tabpage(1);
5969 break;
5970 case CMD_tablast:
5971 goto_tabpage(9999);
5972 break;
5973 case CMD_tabprevious:
5974 case CMD_tabNext:
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005975 if (eap->arg && *eap->arg != NUL)
5976 {
5977 char_u *p = eap->arg;
5978 char_u *p_save = p;
5979
5980 tab_number = getdigits(&p);
5981 if (p == p_save || *p_save == '-' || *p != NUL
5982 || tab_number == 0)
5983 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005984 // No numbers as argument.
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005985 eap->errmsg = e_invarg;
5986 return;
5987 }
5988 }
5989 else
5990 {
5991 if (eap->addr_count == 0)
5992 tab_number = 1;
5993 else
5994 {
5995 tab_number = eap->line2;
5996 if (tab_number < 1)
5997 {
5998 eap->errmsg = e_invrange;
5999 return;
6000 }
6001 }
6002 }
6003 goto_tabpage(-tab_number);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006004 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006005 default: // CMD_tabnext
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006006 tab_number = get_tabpage_arg(eap);
6007 if (eap->errmsg == NULL)
6008 goto_tabpage(tab_number);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006009 break;
6010 }
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006011}
6012
6013/*
6014 * :tabmove command
6015 */
6016 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006017ex_tabmove(exarg_T *eap)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006018{
Bram Moolenaar40ce3a42015-04-21 18:08:39 +02006019 int tab_number;
Bram Moolenaar8cb8dca2012-07-06 18:27:39 +02006020
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006021 tab_number = get_tabpage_arg(eap);
6022 if (eap->errmsg == NULL)
6023 tabpage_move(tab_number);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006024}
6025
6026/*
6027 * :tabs command: List tabs and their contents.
6028 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006029 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006030ex_tabs(exarg_T *eap UNUSED)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006031{
6032 tabpage_T *tp;
6033 win_T *wp;
6034 int tabcount = 1;
6035
6036 msg_start();
6037 msg_scroll = TRUE;
6038 for (tp = first_tabpage; tp != NULL && !got_int; tp = tp->tp_next)
6039 {
6040 msg_putchar('\n');
6041 vim_snprintf((char *)IObuff, IOSIZE, _("Tab page %d"), tabcount++);
Bram Moolenaar8820b482017-03-16 17:23:31 +01006042 msg_outtrans_attr(IObuff, HL_ATTR(HLF_T));
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006043 out_flush(); // output one line at a time
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006044 ui_breakcheck();
6045
Bram Moolenaar030f0df2006-02-21 22:02:53 +00006046 if (tp == curtab)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006047 wp = firstwin;
6048 else
6049 wp = tp->tp_firstwin;
6050 for ( ; wp != NULL && !got_int; wp = wp->w_next)
6051 {
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006052 msg_putchar('\n');
6053 msg_putchar(wp == curwin ? '>' : ' ');
6054 msg_putchar(' ');
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006055 msg_putchar(bufIsChanged(wp->w_buffer) ? '+' : ' ');
6056 msg_putchar(' ');
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006057 if (buf_spname(wp->w_buffer) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02006058 vim_strncpy(IObuff, buf_spname(wp->w_buffer), IOSIZE - 1);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006059 else
6060 home_replace(wp->w_buffer, wp->w_buffer->b_fname,
6061 IObuff, IOSIZE, TRUE);
6062 msg_outtrans(IObuff);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006063 out_flush(); // output one line at a time
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006064 ui_breakcheck();
6065 }
6066 }
6067}
6068
Bram Moolenaar071d4272004-06-13 20:20:40 +00006069/*
6070 * ":mode": Set screen mode.
6071 * If no argument given, just get the screen size and redraw.
6072 */
6073 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006074ex_mode(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006075{
6076 if (*eap->arg == NUL)
6077 shell_resized();
6078 else
Bram Moolenaar3c71aec2020-01-17 19:32:20 +01006079 emsg(_(e_screenmode));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006080}
6081
Bram Moolenaar071d4272004-06-13 20:20:40 +00006082/*
6083 * ":resize".
6084 * set, increment or decrement current window height
6085 */
6086 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006087ex_resize(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006088{
6089 int n;
6090 win_T *wp = curwin;
6091
6092 if (eap->addr_count > 0)
6093 {
6094 n = eap->line2;
6095 for (wp = firstwin; wp->w_next != NULL && --n > 0; wp = wp->w_next)
6096 ;
6097 }
6098
Bram Moolenaar44a2f922016-03-19 22:11:51 +01006099# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00006100 need_mouse_correct = TRUE;
Bram Moolenaar44a2f922016-03-19 22:11:51 +01006101# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006102 n = atol((char *)eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006103 if (cmdmod.split & WSP_VERT)
6104 {
6105 if (*eap->arg == '-' || *eap->arg == '+')
Bram Moolenaar02631462017-09-22 15:20:32 +02006106 n += curwin->w_width;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006107 else if (n == 0 && eap->arg[0] == NUL) // default is very wide
Bram Moolenaar071d4272004-06-13 20:20:40 +00006108 n = 9999;
6109 win_setwidth_win((int)n, wp);
6110 }
6111 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006112 {
6113 if (*eap->arg == '-' || *eap->arg == '+')
6114 n += curwin->w_height;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006115 else if (n == 0 && eap->arg[0] == NUL) // default is very high
Bram Moolenaar071d4272004-06-13 20:20:40 +00006116 n = 9999;
6117 win_setheight_win((int)n, wp);
6118 }
6119}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006120
6121/*
6122 * ":find [+command] <file>" command.
6123 */
6124 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006125ex_find(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006126{
6127#ifdef FEAT_SEARCHPATH
6128 char_u *fname;
6129 int count;
6130
6131 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg), FNAME_MESS,
6132 TRUE, curbuf->b_ffname);
6133 if (eap->addr_count > 0)
6134 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006135 // Repeat finding the file "count" times. This matters when it
6136 // appears several times in the path.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006137 count = eap->line2;
6138 while (fname != NULL && --count > 0)
6139 {
6140 vim_free(fname);
6141 fname = find_file_in_path(NULL, 0, FNAME_MESS,
6142 FALSE, curbuf->b_ffname);
6143 }
6144 }
6145
6146 if (fname != NULL)
6147 {
6148 eap->arg = fname;
6149#endif
6150 do_exedit(eap, NULL);
6151#ifdef FEAT_SEARCHPATH
6152 vim_free(fname);
6153 }
6154#endif
6155}
6156
6157/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00006158 * ":open" simulation: for now just work like ":visual".
6159 */
6160 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006161ex_open(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00006162{
6163 regmatch_T regmatch;
6164 char_u *p;
6165
6166 curwin->w_cursor.lnum = eap->line2;
6167 beginline(BL_SOL | BL_FIX);
6168 if (*eap->arg == '/')
6169 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006170 // ":open /pattern/": put cursor in column found with pattern
Bram Moolenaardf177f62005-02-22 08:39:57 +00006171 ++eap->arg;
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006172 p = skip_regexp(eap->arg, '/', p_magic);
Bram Moolenaardf177f62005-02-22 08:39:57 +00006173 *p = NUL;
6174 regmatch.regprog = vim_regcomp(eap->arg, p_magic ? RE_MAGIC : 0);
6175 if (regmatch.regprog != NULL)
6176 {
6177 regmatch.rm_ic = p_ic;
6178 p = ml_get_curline();
6179 if (vim_regexec(&regmatch, p, (colnr_T)0))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006180 curwin->w_cursor.col = (colnr_T)(regmatch.startp[0] - p);
Bram Moolenaardf177f62005-02-22 08:39:57 +00006181 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006182 emsg(_(e_nomatch));
Bram Moolenaar473de612013-06-08 18:19:48 +02006183 vim_regfree(regmatch.regprog);
Bram Moolenaardf177f62005-02-22 08:39:57 +00006184 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006185 // Move to the NUL, ignore any other arguments.
Bram Moolenaardf177f62005-02-22 08:39:57 +00006186 eap->arg += STRLEN(eap->arg);
6187 }
6188 check_cursor();
6189
6190 eap->cmdidx = CMD_visual;
6191 do_exedit(eap, NULL);
6192}
6193
6194/*
6195 * ":edit", ":badd", ":visual".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006196 */
6197 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006198ex_edit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006199{
6200 do_exedit(eap, NULL);
6201}
6202
6203/*
Bram Moolenaar4b96df52020-01-26 22:00:26 +01006204 * ":edit <file>" command and alike.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006205 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006206 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006207do_exedit(
6208 exarg_T *eap,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006209 win_T *old_curwin) // curwin before doing a split or NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006210{
6211 int n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006212 int need_hide;
Bram Moolenaardf177f62005-02-22 08:39:57 +00006213 int exmode_was = exmode_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006214
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01006215 if ((eap->cmdidx != CMD_pedit && ERROR_IF_POPUP_WINDOW)
6216 || ERROR_IF_TERM_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02006217 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006218 /*
6219 * ":vi" command ends Ex mode.
6220 */
6221 if (exmode_active && (eap->cmdidx == CMD_visual
6222 || eap->cmdidx == CMD_view))
6223 {
6224 exmode_active = FALSE;
Bram Moolenaar158ea172020-06-18 17:28:39 +02006225 ex_pressedreturn = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006226 if (*eap->arg == NUL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00006227 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006228 // Special case: ":global/pat/visual\NLvi-commands"
Bram Moolenaardf177f62005-02-22 08:39:57 +00006229 if (global_busy)
6230 {
6231 int rd = RedrawingDisabled;
6232 int nwr = no_wait_return;
6233 int ms = msg_scroll;
6234#ifdef FEAT_GUI
6235 int he = hold_gui_events;
6236#endif
6237
6238 if (eap->nextcmd != NULL)
6239 {
6240 stuffReadbuff(eap->nextcmd);
6241 eap->nextcmd = NULL;
6242 }
6243
6244 if (exmode_was != EXMODE_VIM)
6245 settmode(TMODE_RAW);
6246 RedrawingDisabled = 0;
6247 no_wait_return = 0;
6248 need_wait_return = FALSE;
6249 msg_scroll = 0;
6250#ifdef FEAT_GUI
6251 hold_gui_events = 0;
6252#endif
6253 must_redraw = CLEAR;
Bram Moolenaar9e2bcb52020-02-18 21:33:00 +01006254 pending_exmode_active = TRUE;
Bram Moolenaardf177f62005-02-22 08:39:57 +00006255
6256 main_loop(FALSE, TRUE);
6257
Bram Moolenaar9e2bcb52020-02-18 21:33:00 +01006258 pending_exmode_active = FALSE;
Bram Moolenaardf177f62005-02-22 08:39:57 +00006259 RedrawingDisabled = rd;
6260 no_wait_return = nwr;
6261 msg_scroll = ms;
6262#ifdef FEAT_GUI
6263 hold_gui_events = he;
6264#endif
6265 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006266 return;
Bram Moolenaardf177f62005-02-22 08:39:57 +00006267 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006268 }
6269
6270 if ((eap->cmdidx == CMD_new
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006271 || eap->cmdidx == CMD_tabnew
6272 || eap->cmdidx == CMD_tabedit
Bram Moolenaar4033c552017-09-16 20:54:51 +02006273 || eap->cmdidx == CMD_vnew) && *eap->arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006274 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006275 // ":new" or ":tabnew" without argument: edit an new empty buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00006276 setpcmark();
6277 (void)do_ecmd(0, NULL, NULL, eap, ECMD_ONE,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00006278 ECMD_HIDE + (eap->forceit ? ECMD_FORCEIT : 0),
6279 old_curwin == NULL ? curwin : NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006280 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02006281 else if ((eap->cmdidx != CMD_split && eap->cmdidx != CMD_vsplit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006282 || *eap->arg != NUL
6283#ifdef FEAT_BROWSE
6284 || cmdmod.browse
6285#endif
6286 )
6287 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006288 // Can't edit another file when "curbuf_lock" is set. Only ":edit"
6289 // can bring us here, others are stopped earlier.
Bram Moolenaar5555acc2006-04-07 21:33:12 +00006290 if (*eap->arg != NUL && curbuf_locked())
6291 return;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006292
Bram Moolenaar071d4272004-06-13 20:20:40 +00006293 n = readonlymode;
6294 if (eap->cmdidx == CMD_view || eap->cmdidx == CMD_sview)
6295 readonlymode = TRUE;
6296 else if (eap->cmdidx == CMD_enew)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006297 readonlymode = FALSE; // 'readonly' doesn't make sense in an
6298 // empty buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00006299 setpcmark();
6300 if (do_ecmd(0, (eap->cmdidx == CMD_enew ? NULL : eap->arg),
6301 NULL, eap,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006302 // ":edit" goes to first line if Vi compatible
Bram Moolenaar071d4272004-06-13 20:20:40 +00006303 (*eap->arg == NUL && eap->do_ecmd_lnum == 0
6304 && vim_strchr(p_cpo, CPO_GOTO1) != NULL)
6305 ? ECMD_ONE : eap->do_ecmd_lnum,
Bram Moolenaareb44a682017-08-03 22:44:55 +02006306 (buf_hide(curbuf) ? ECMD_HIDE : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006307 + (eap->forceit ? ECMD_FORCEIT : 0)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006308 // after a split we can use an existing buffer
Bram Moolenaar7b449342014-03-25 13:03:48 +01006309 + (old_curwin != NULL ? ECMD_OLDBUF : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006310 + (eap->cmdidx == CMD_badd ? ECMD_ADDBUF : 0 )
Bram Moolenaar701f7af2008-11-15 13:12:07 +00006311 , old_curwin == NULL ? curwin : NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006312 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006313 // Editing the file failed. If the window was split, close it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006314 if (old_curwin != NULL)
6315 {
6316 need_hide = (curbufIsChanged() && curbuf->b_nwindows <= 1);
Bram Moolenaareb44a682017-08-03 22:44:55 +02006317 if (!need_hide || buf_hide(curbuf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006318 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006319#if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00006320 cleanup_T cs;
6321
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006322 // Reset the error/interrupt/exception state here so that
6323 // aborting() returns FALSE when closing a window.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00006324 enter_cleanup(&cs);
Bram Moolenaar4033c552017-09-16 20:54:51 +02006325#endif
6326#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00006327 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006328#endif
Bram Moolenaareb44a682017-08-03 22:44:55 +02006329 win_close(curwin, !need_hide && !buf_hide(curbuf));
Bram Moolenaarc0197e22004-09-13 20:26:32 +00006330
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006331#if defined(FEAT_EVAL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006332 // Restore the error/interrupt/exception state if not
6333 // discarded by a new aborting error, interrupt, or
6334 // uncaught exception.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00006335 leave_cleanup(&cs);
Bram Moolenaar4033c552017-09-16 20:54:51 +02006336#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006337 }
6338 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006339 }
6340 else if (readonlymode && curbuf->b_nwindows == 1)
6341 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006342 // When editing an already visited buffer, 'readonly' won't be set
6343 // but the previous value is kept. With ":view" and ":sview" we
6344 // want the file to be readonly, except when another window is
6345 // editing the same buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006346 curbuf->b_p_ro = TRUE;
6347 }
6348 readonlymode = n;
6349 }
6350 else
6351 {
6352 if (eap->do_ecmd_cmd != NULL)
6353 do_cmdline_cmd(eap->do_ecmd_cmd);
6354#ifdef FEAT_TITLE
6355 n = curwin->w_arg_idx_invalid;
6356#endif
6357 check_arg_idx(curwin);
6358#ifdef FEAT_TITLE
6359 if (n != curwin->w_arg_idx_invalid)
6360 maketitle();
6361#endif
6362 }
6363
Bram Moolenaar071d4272004-06-13 20:20:40 +00006364 /*
6365 * if ":split file" worked, set alternate file name in old window to new
6366 * file
6367 */
6368 if (old_curwin != NULL
6369 && *eap->arg != NUL
6370 && curwin != old_curwin
6371 && win_valid(old_curwin)
Bram Moolenaard4755bb2004-09-02 19:12:26 +00006372 && old_curwin->w_buffer != curbuf
6373 && !cmdmod.keepalt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006374 old_curwin->w_alt_fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006375
6376 ex_no_reprint = TRUE;
6377}
6378
6379#ifndef FEAT_GUI
6380/*
6381 * ":gui" and ":gvim" when there is no GUI.
6382 */
6383 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006384ex_nogui(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006385{
6386 eap->errmsg = e_nogvim;
6387}
6388#endif
6389
Bram Moolenaar4f974752019-02-17 17:44:42 +01006390#if defined(FEAT_GUI_MSWIN) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006391 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006392ex_tearoff(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006393{
6394 gui_make_tearoff(eap->arg);
6395}
6396#endif
6397
Bram Moolenaar29a2c082018-03-05 21:06:23 +01006398#if (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK) \
6399 || defined(FEAT_TERM_POPUP_MENU)) && defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006400 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006401ex_popup(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006402{
Bram Moolenaar29a2c082018-03-05 21:06:23 +01006403# if defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK)
6404 if (gui.in_use)
6405 gui_make_popup(eap->arg, eap->forceit);
6406# ifdef FEAT_TERM_POPUP_MENU
6407 else
6408# endif
6409# endif
6410# ifdef FEAT_TERM_POPUP_MENU
6411 pum_make_popup(eap->arg, eap->forceit);
6412# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006413}
6414#endif
6415
Bram Moolenaar071d4272004-06-13 20:20:40 +00006416 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006417ex_swapname(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006418{
6419 if (curbuf->b_ml.ml_mfp == NULL || curbuf->b_ml.ml_mfp->mf_fname == NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01006420 msg(_("No swap file"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006421 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01006422 msg((char *)curbuf->b_ml.ml_mfp->mf_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006423}
6424
6425/*
6426 * ":syncbind" forces all 'scrollbind' windows to have the same relative
6427 * offset.
6428 * (1998-11-02 16:21:01 R. Edward Ralston <eralston@computer.org>)
6429 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006430 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006431ex_syncbind(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006432{
Bram Moolenaar071d4272004-06-13 20:20:40 +00006433 win_T *wp;
Bram Moolenaardedd1b02013-12-14 13:06:17 +01006434 win_T *save_curwin = curwin;
6435 buf_T *save_curbuf = curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006436 long topline;
6437 long y;
6438 linenr_T old_linenr = curwin->w_cursor.lnum;
6439
6440 setpcmark();
6441
6442 /*
6443 * determine max topline
6444 */
6445 if (curwin->w_p_scb)
6446 {
6447 topline = curwin->w_topline;
Bram Moolenaar29323592016-07-24 22:04:11 +02006448 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006449 {
6450 if (wp->w_p_scb && wp->w_buffer)
6451 {
Bram Moolenaar375e3392019-01-31 18:26:10 +01006452 y = wp->w_buffer->b_ml.ml_line_count - get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006453 if (topline > y)
6454 topline = y;
6455 }
6456 }
6457 if (topline < 1)
6458 topline = 1;
6459 }
6460 else
6461 {
6462 topline = 1;
6463 }
6464
6465
6466 /*
Bram Moolenaardedd1b02013-12-14 13:06:17 +01006467 * Set all scrollbind windows to the same topline.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006468 */
Bram Moolenaar29323592016-07-24 22:04:11 +02006469 FOR_ALL_WINDOWS(curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006470 {
6471 if (curwin->w_p_scb)
6472 {
Bram Moolenaardedd1b02013-12-14 13:06:17 +01006473 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006474 y = topline - curwin->w_topline;
6475 if (y > 0)
6476 scrollup(y, TRUE);
6477 else
6478 scrolldown(-y, TRUE);
6479 curwin->w_scbind_pos = topline;
6480 redraw_later(VALID);
6481 cursor_correct();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006482 curwin->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006483 }
6484 }
Bram Moolenaardedd1b02013-12-14 13:06:17 +01006485 curwin = save_curwin;
6486 curbuf = save_curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006487 if (curwin->w_p_scb)
6488 {
6489 did_syncbind = TRUE;
6490 checkpcmark();
6491 if (old_linenr != curwin->w_cursor.lnum)
6492 {
6493 char_u ctrl_o[2];
6494
6495 ctrl_o[0] = Ctrl_O;
6496 ctrl_o[1] = 0;
6497 ins_typebuf(ctrl_o, REMAP_NONE, 0, TRUE, FALSE);
6498 }
6499 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006500}
6501
6502
6503 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006504ex_read(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006505{
Bram Moolenaardf177f62005-02-22 08:39:57 +00006506 int i;
6507 int empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
6508 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006509
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006510 if (eap->usefilter) // :r!cmd
Bram Moolenaar071d4272004-06-13 20:20:40 +00006511 do_bang(1, eap, FALSE, FALSE, TRUE);
6512 else
6513 {
6514 if (u_save(eap->line2, (linenr_T)(eap->line2 + 1)) == FAIL)
6515 return;
6516
6517#ifdef FEAT_BROWSE
6518 if (cmdmod.browse)
6519 {
6520 char_u *browseFile;
6521
Bram Moolenaar7171abe2004-10-11 10:06:20 +00006522 browseFile = do_browse(0, (char_u *)_("Append File"), eap->arg,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006523 NULL, NULL, NULL, curbuf);
6524 if (browseFile != NULL)
6525 {
6526 i = readfile(browseFile, NULL,
6527 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
6528 vim_free(browseFile);
6529 }
6530 else
6531 i = OK;
6532 }
6533 else
6534#endif
6535 if (*eap->arg == NUL)
6536 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006537 if (check_fname() == FAIL) // check for no file name
Bram Moolenaar071d4272004-06-13 20:20:40 +00006538 return;
6539 i = readfile(curbuf->b_ffname, curbuf->b_fname,
6540 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
6541 }
6542 else
6543 {
6544 if (vim_strchr(p_cpo, CPO_ALTREAD) != NULL)
6545 (void)setaltfname(eap->arg, eap->arg, (linenr_T)1);
6546 i = readfile(eap->arg, NULL,
6547 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
6548
6549 }
Bram Moolenaare13b9af2017-01-13 22:01:02 +01006550 if (i != OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006551 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006552#if defined(FEAT_EVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006553 if (!aborting())
6554#endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006555 semsg(_(e_notopen), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006556 }
6557 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00006558 {
6559 if (empty && exmode_active)
6560 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006561 // Delete the empty line that remains. Historically ex does
6562 // this but vi doesn't.
Bram Moolenaardf177f62005-02-22 08:39:57 +00006563 if (eap->line2 == 0)
6564 lnum = curbuf->b_ml.ml_line_count;
6565 else
6566 lnum = 1;
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00006567 if (*ml_get(lnum) == NUL && u_savedel(lnum, 1L) == OK)
Bram Moolenaardf177f62005-02-22 08:39:57 +00006568 {
Bram Moolenaarca70c072020-05-30 20:30:46 +02006569 ml_delete(lnum);
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00006570 if (curwin->w_cursor.lnum > 1
6571 && curwin->w_cursor.lnum >= lnum)
Bram Moolenaardf177f62005-02-22 08:39:57 +00006572 --curwin->w_cursor.lnum;
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00006573 deleted_lines_mark(lnum, 1L);
Bram Moolenaardf177f62005-02-22 08:39:57 +00006574 }
6575 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006576 redraw_curbuf_later(VALID);
Bram Moolenaardf177f62005-02-22 08:39:57 +00006577 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006578 }
6579}
6580
Bram Moolenaarea408852005-06-25 22:49:46 +00006581static char_u *prev_dir = NULL;
6582
6583#if defined(EXITFREE) || defined(PROTO)
6584 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006585free_cd_dir(void)
Bram Moolenaarea408852005-06-25 22:49:46 +00006586{
Bram Moolenaard23a8232018-02-10 18:45:26 +01006587 VIM_CLEAR(prev_dir);
6588 VIM_CLEAR(globaldir);
Bram Moolenaarea408852005-06-25 22:49:46 +00006589}
6590#endif
6591
Bram Moolenaarf4258302013-06-02 18:20:17 +02006592/*
Bram Moolenaar002bc792020-06-05 22:33:42 +02006593 * Get the previous directory for the given chdir scope.
6594 */
6595 static char_u *
6596get_prevdir(cdscope_T scope)
6597{
6598 if (scope == CDSCOPE_WINDOW)
6599 return curwin->w_prevdir;
6600 else if (scope == CDSCOPE_TABPAGE)
6601 return curtab->tp_prevdir;
6602 return prev_dir;
6603}
6604
6605/*
Bram Moolenaarf4258302013-06-02 18:20:17 +02006606 * Deal with the side effects of changing the current directory.
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006607 * When 'scope' is CDSCOPE_TABPAGE then this was after an ":tcd" command.
6608 * When 'scope' is CDSCOPE_WINDOW then this was after an ":lcd" command.
Bram Moolenaarf4258302013-06-02 18:20:17 +02006609 */
6610 void
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006611post_chdir(cdscope_T scope)
Bram Moolenaarf4258302013-06-02 18:20:17 +02006612{
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006613 if (scope != CDSCOPE_WINDOW)
Bram Moolenaar00aa0692019-04-27 20:37:57 +02006614 // Clear tab local directory for both :cd and :tcd
6615 VIM_CLEAR(curtab->tp_localdir);
Bram Moolenaard23a8232018-02-10 18:45:26 +01006616 VIM_CLEAR(curwin->w_localdir);
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006617 if (scope != CDSCOPE_GLOBAL)
Bram Moolenaarf4258302013-06-02 18:20:17 +02006618 {
Bram Moolenaar002bc792020-06-05 22:33:42 +02006619 char_u *pdir = get_prevdir(scope);
6620
6621 // If still in the global directory, need to remember current
6622 // directory as the global directory.
6623 if (globaldir == NULL && pdir != NULL)
6624 globaldir = vim_strsave(pdir);
6625
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006626 // Remember this local directory for the window.
Bram Moolenaarf4258302013-06-02 18:20:17 +02006627 if (mch_dirname(NameBuff, MAXPATHL) == OK)
Bram Moolenaar00aa0692019-04-27 20:37:57 +02006628 {
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006629 if (scope == CDSCOPE_TABPAGE)
Bram Moolenaar00aa0692019-04-27 20:37:57 +02006630 curtab->tp_localdir = vim_strsave(NameBuff);
6631 else
6632 curwin->w_localdir = vim_strsave(NameBuff);
6633 }
Bram Moolenaarf4258302013-06-02 18:20:17 +02006634 }
6635 else
6636 {
Bram Moolenaar002bc792020-06-05 22:33:42 +02006637 // We are now in the global directory, no need to remember its name.
Bram Moolenaard23a8232018-02-10 18:45:26 +01006638 VIM_CLEAR(globaldir);
Bram Moolenaarf4258302013-06-02 18:20:17 +02006639 }
6640
6641 shorten_fnames(TRUE);
6642}
6643
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006644/*
6645 * Change directory function used by :cd/:tcd/:lcd Ex commands and the
Bram Moolenaar95058722020-06-01 16:26:19 +02006646 * chdir() function.
6647 * scope == CDSCOPE_WINDOW: changes the window-local directory
6648 * scope == CDSCOPE_TABPAGE: changes the tab-local directory
6649 * Otherwise: changes the global directory
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006650 * Returns TRUE if the directory is successfully changed.
6651 */
6652 int
6653changedir_func(
6654 char_u *new_dir,
6655 int forceit,
6656 cdscope_T scope)
6657{
6658 char_u *tofree;
Bram Moolenaar002bc792020-06-05 22:33:42 +02006659 char_u *pdir = NULL;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006660 int dir_differs;
6661 int retval = FALSE;
6662
Bram Moolenaar7cc96922020-01-31 22:41:38 +01006663 if (new_dir == NULL || allbuf_locked())
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006664 return FALSE;
6665
6666 if (vim_strchr(p_cpo, CPO_CHDIR) != NULL && curbufIsChanged() && !forceit)
6667 {
6668 emsg(_("E747: Cannot change directory, buffer is modified (add ! to override)"));
6669 return FALSE;
6670 }
6671
6672 // ":cd -": Change to previous directory
6673 if (STRCMP(new_dir, "-") == 0)
6674 {
Bram Moolenaar002bc792020-06-05 22:33:42 +02006675 pdir = get_prevdir(scope);
6676 if (pdir == NULL)
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006677 {
6678 emsg(_("E186: No previous directory"));
6679 return FALSE;
6680 }
Bram Moolenaar002bc792020-06-05 22:33:42 +02006681 new_dir = pdir;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006682 }
6683
Bram Moolenaar002bc792020-06-05 22:33:42 +02006684 // Free the previous directory
6685 tofree = get_prevdir(scope);
6686
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006687 // Save current directory for next ":cd -"
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006688 if (mch_dirname(NameBuff, MAXPATHL) == OK)
Bram Moolenaar002bc792020-06-05 22:33:42 +02006689 pdir = vim_strsave(NameBuff);
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006690 else
Bram Moolenaar002bc792020-06-05 22:33:42 +02006691 pdir = NULL;
6692 if (scope == CDSCOPE_WINDOW)
6693 curwin->w_prevdir = pdir;
6694 else if (scope == CDSCOPE_TABPAGE)
6695 curtab->tp_prevdir = pdir;
6696 else
6697 prev_dir = pdir;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006698
6699#if defined(UNIX) || defined(VMS)
6700 // for UNIX ":cd" means: go to home directory
6701 if (*new_dir == NUL)
6702 {
6703 // use NameBuff for home directory name
6704# ifdef VMS
6705 char_u *p;
6706
6707 p = mch_getenv((char_u *)"SYS$LOGIN");
6708 if (p == NULL || *p == NUL) // empty is the same as not set
6709 NameBuff[0] = NUL;
6710 else
6711 vim_strncpy(NameBuff, p, MAXPATHL - 1);
6712# else
6713 expand_env((char_u *)"$HOME", NameBuff, MAXPATHL);
6714# endif
6715 new_dir = NameBuff;
6716 }
6717#endif
Bram Moolenaar002bc792020-06-05 22:33:42 +02006718 dir_differs = new_dir == NULL || pdir == NULL
6719 || pathcmp((char *)pdir, (char *)new_dir, -1) != 0;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006720 if (new_dir == NULL || (dir_differs && vim_chdir(new_dir)))
6721 emsg(_(e_failed));
6722 else
6723 {
6724 char_u *acmd_fname;
6725
6726 post_chdir(scope);
6727
6728 if (dir_differs)
6729 {
6730 if (scope == CDSCOPE_WINDOW)
6731 acmd_fname = (char_u *)"window";
6732 else if (scope == CDSCOPE_TABPAGE)
6733 acmd_fname = (char_u *)"tabpage";
6734 else
6735 acmd_fname = (char_u *)"global";
6736 apply_autocmds(EVENT_DIRCHANGED, acmd_fname, new_dir, FALSE,
6737 curbuf);
6738 }
6739 retval = TRUE;
6740 }
6741 vim_free(tofree);
6742
6743 return retval;
6744}
Bram Moolenaarea408852005-06-25 22:49:46 +00006745
Bram Moolenaar071d4272004-06-13 20:20:40 +00006746/*
Bram Moolenaar00aa0692019-04-27 20:37:57 +02006747 * ":cd", ":tcd", ":lcd", ":chdir" ":tchdir" and ":lchdir".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006748 */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00006749 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006750ex_cd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006751{
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01006752 char_u *new_dir;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006753
6754 new_dir = eap->arg;
6755#if !defined(UNIX) && !defined(VMS)
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006756 // for non-UNIX ":cd" means: print current directory
Bram Moolenaar071d4272004-06-13 20:20:40 +00006757 if (*new_dir == NUL)
6758 ex_pwd(NULL);
6759 else
6760#endif
6761 {
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006762 cdscope_T scope = CDSCOPE_GLOBAL;
6763
6764 if (eap->cmdidx == CMD_lcd || eap->cmdidx == CMD_lchdir)
6765 scope = CDSCOPE_WINDOW;
6766 else if (eap->cmdidx == CMD_tcd || eap->cmdidx == CMD_tchdir)
6767 scope = CDSCOPE_TABPAGE;
6768
6769 if (changedir_func(new_dir, eap->forceit, scope))
Bram Moolenaardf177f62005-02-22 08:39:57 +00006770 {
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006771 // Echo the new current directory if the command was typed.
Bram Moolenaarfcfbc672009-07-09 18:13:49 +00006772 if (KeyTyped || p_verbose >= 5)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006773 ex_pwd(eap);
6774 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006775 }
6776}
6777
6778/*
6779 * ":pwd".
6780 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006781 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006782ex_pwd(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006783{
6784 if (mch_dirname(NameBuff, MAXPATHL) == OK)
6785 {
6786#ifdef BACKSLASH_IN_FILENAME
6787 slash_adjust(NameBuff);
6788#endif
Bram Moolenaar95058722020-06-01 16:26:19 +02006789 if (p_verbose > 0)
6790 {
6791 char *context = "global";
6792
6793 if (curwin->w_localdir != NULL)
6794 context = "window";
6795 else if (curtab->tp_localdir != NULL)
6796 context = "tabpage";
6797 smsg("[%s] %s", context, (char *)NameBuff);
6798 }
6799 else
6800 msg((char *)NameBuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006801 }
6802 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006803 emsg(_("E187: Unknown"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006804}
6805
6806/*
6807 * ":=".
6808 */
6809 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006810ex_equal(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006811{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006812 smsg("%ld", (long)eap->line2);
Bram Moolenaardf177f62005-02-22 08:39:57 +00006813 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006814}
6815
6816 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006817ex_sleep(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006818{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006819 int n;
6820 long len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006821
6822 if (cursor_valid())
6823 {
6824 n = W_WINROW(curwin) + curwin->w_wrow - msg_scrolled;
6825 if (n >= 0)
Bram Moolenaar53f81742017-09-22 14:35:51 +02006826 windgoto((int)n, curwin->w_wincol + curwin->w_wcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006827 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006828
6829 len = eap->line2;
6830 switch (*eap->arg)
6831 {
6832 case 'm': break;
6833 case NUL: len *= 1000L; break;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006834 default: semsg(_(e_invarg2), eap->arg); return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006835 }
6836 do_sleep(len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006837}
6838
6839/*
6840 * Sleep for "msec" milliseconds, but keep checking for a CTRL-C every second.
6841 */
6842 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006843do_sleep(long msec)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006844{
Bram Moolenaar52953192019-08-16 10:27:13 +02006845 long done = 0;
Bram Moolenaar975b5272016-03-15 23:10:59 +01006846 long wait_now;
Bram Moolenaar52953192019-08-16 10:27:13 +02006847# ifdef ELAPSED_FUNC
6848 elapsed_T start_tv;
6849
6850 // Remember at what time we started, so that we know how much longer we
6851 // should wait after waiting for a bit.
6852 ELAPSED_INIT(start_tv);
6853# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006854
6855 cursor_on();
Bram Moolenaarf45d7472018-09-30 18:22:26 +02006856 out_flush_cursor(FALSE, FALSE);
Bram Moolenaar52953192019-08-16 10:27:13 +02006857 while (!got_int && done < msec)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006858 {
Bram Moolenaar975b5272016-03-15 23:10:59 +01006859 wait_now = msec - done > 1000L ? 1000L : msec - done;
6860#ifdef FEAT_TIMERS
6861 {
6862 long due_time = check_due_timer();
6863
6864 if (due_time > 0 && due_time < wait_now)
6865 wait_now = due_time;
6866 }
6867#endif
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02006868#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar28e67e02019-08-15 23:05:49 +02006869 if (has_any_channel() && wait_now > 20L)
6870 wait_now = 20L;
6871#endif
6872#ifdef FEAT_SOUND
6873 if (has_any_sound_callback() && wait_now > 20L)
6874 wait_now = 20L;
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02006875#endif
Bram Moolenaar975b5272016-03-15 23:10:59 +01006876 ui_delay(wait_now, TRUE);
Bram Moolenaar52953192019-08-16 10:27:13 +02006877
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02006878#ifdef FEAT_JOB_CHANNEL
6879 if (has_any_channel())
6880 ui_breakcheck_force(TRUE);
6881 else
6882#endif
6883 ui_breakcheck();
Bram Moolenaar93c88e02015-09-15 14:12:05 +02006884#ifdef MESSAGE_QUEUE
Bram Moolenaar52953192019-08-16 10:27:13 +02006885 // Process the netbeans and clientserver messages that may have been
6886 // received in the call to ui_breakcheck() when the GUI is in use. This
6887 // may occur when running a test case.
Bram Moolenaar93c88e02015-09-15 14:12:05 +02006888 parse_queued_messages();
Bram Moolenaare3cc6d42011-10-20 21:58:34 +02006889#endif
Bram Moolenaar52953192019-08-16 10:27:13 +02006890
6891# ifdef ELAPSED_FUNC
6892 // actual time passed
6893 done = ELAPSED_FUNC(start_tv);
6894# else
6895 // guestimate time passed (will actually be more)
6896 done += wait_now;
6897# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006898 }
Bram Moolenaar1ebff3d2018-07-07 16:18:13 +02006899
6900 // If CTRL-C was typed to interrupt the sleep, drop the CTRL-C from the
6901 // input buffer, otherwise a following call to input() fails.
6902 if (got_int)
6903 (void)vpeekc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006904}
6905
Bram Moolenaar071d4272004-06-13 20:20:40 +00006906/*
6907 * ":winsize" command (obsolete).
6908 */
6909 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006910ex_winsize(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006911{
6912 int w, h;
6913 char_u *arg = eap->arg;
6914 char_u *p;
6915
6916 w = getdigits(&arg);
6917 arg = skipwhite(arg);
6918 p = arg;
6919 h = getdigits(&arg);
6920 if (*p != NUL && *arg == NUL)
6921 set_shellsize(w, h, TRUE);
6922 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006923 emsg(_("E465: :winsize requires two number arguments"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006924}
6925
Bram Moolenaar071d4272004-06-13 20:20:40 +00006926 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006927ex_wincmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006928{
6929 int xchar = NUL;
6930 char_u *p;
6931
6932 if (*eap->arg == 'g' || *eap->arg == Ctrl_G)
6933 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006934 // CTRL-W g and CTRL-W CTRL-G have an extra command character
Bram Moolenaar071d4272004-06-13 20:20:40 +00006935 if (eap->arg[1] == NUL)
6936 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006937 emsg(_(e_invarg));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006938 return;
6939 }
6940 xchar = eap->arg[1];
6941 p = eap->arg + 2;
6942 }
6943 else
6944 p = eap->arg + 1;
6945
6946 eap->nextcmd = check_nextcmd(p);
6947 p = skipwhite(p);
6948 if (*p != NUL && *p != '"' && eap->nextcmd == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006949 emsg(_(e_invarg));
Bram Moolenaar12bde492011-06-13 01:19:56 +02006950 else if (!eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006951 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006952 // Pass flags on for ":vertical wincmd ]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006953 postponed_split_flags = cmdmod.split;
Bram Moolenaard326ce82007-03-11 14:48:29 +00006954 postponed_split_tab = cmdmod.tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006955 do_window(*eap->arg, eap->addr_count > 0 ? eap->line2 : 0L, xchar);
6956 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00006957 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006958 }
6959}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006960
Bram Moolenaar843ee412004-06-30 16:16:41 +00006961#if defined(FEAT_GUI) || defined(UNIX) || defined(VMS) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006962/*
6963 * ":winpos".
6964 */
6965 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006966ex_winpos(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006967{
6968 int x, y;
6969 char_u *arg = eap->arg;
6970 char_u *p;
6971
6972 if (*arg == NUL)
6973 {
Bram Moolenaar843ee412004-06-30 16:16:41 +00006974# if defined(FEAT_GUI) || defined(MSWIN)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02006975# ifdef VIMDLL
6976 if (gui.in_use ? gui_mch_get_winpos(&x, &y) != FAIL :
6977 mch_get_winpos(&x, &y) != FAIL)
6978# elif defined(FEAT_GUI)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006979 if (gui.in_use && gui_mch_get_winpos(&x, &y) != FAIL)
Bram Moolenaar843ee412004-06-30 16:16:41 +00006980# else
6981 if (mch_get_winpos(&x, &y) != FAIL)
6982# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006983 {
6984 sprintf((char *)IObuff, _("Window position: X %d, Y %d"), x, y);
Bram Moolenaar32526b32019-01-19 17:43:09 +01006985 msg((char *)IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006986 }
6987 else
6988# endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006989 emsg(_("E188: Obtaining window position not implemented for this platform"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006990 }
6991 else
6992 {
6993 x = getdigits(&arg);
6994 arg = skipwhite(arg);
6995 p = arg;
6996 y = getdigits(&arg);
6997 if (*p == NUL || *arg != NUL)
6998 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006999 emsg(_("E466: :winpos requires two number arguments"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007000 return;
7001 }
7002# ifdef FEAT_GUI
7003 if (gui.in_use)
7004 gui_mch_set_winpos(x, y);
7005 else if (gui.starting)
7006 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007007 // Remember the coordinates for when the window is opened.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007008 gui_win_x = x;
7009 gui_win_y = y;
7010 }
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007011# if defined(HAVE_TGETENT) || defined(VIMDLL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007012 else
7013# endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007014# endif
7015# if defined(MSWIN) && (!defined(FEAT_GUI) || defined(VIMDLL))
Bram Moolenaar843ee412004-06-30 16:16:41 +00007016 mch_set_winpos(x, y);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007017# endif
7018# ifdef HAVE_TGETENT
7019 if (*T_CWP)
7020 term_set_winpos(x, y);
7021# endif
7022 }
7023}
7024#endif
7025
7026/*
7027 * Handle command that work like operators: ":delete", ":yank", ":>" and ":<".
7028 */
7029 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007030ex_operators(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007031{
7032 oparg_T oa;
7033
7034 clear_oparg(&oa);
7035 oa.regname = eap->regname;
7036 oa.start.lnum = eap->line1;
7037 oa.end.lnum = eap->line2;
7038 oa.line_count = eap->line2 - eap->line1 + 1;
7039 oa.motion_type = MLINE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007040 virtual_op = FALSE;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007041 if (eap->cmdidx != CMD_yank) // position cursor for undo
Bram Moolenaar071d4272004-06-13 20:20:40 +00007042 {
7043 setpcmark();
7044 curwin->w_cursor.lnum = eap->line1;
7045 beginline(BL_SOL | BL_FIX);
7046 }
7047
Bram Moolenaard07c6e12013-11-21 14:21:40 +01007048 if (VIsual_active)
7049 end_visual_mode();
Bram Moolenaard07c6e12013-11-21 14:21:40 +01007050
Bram Moolenaar071d4272004-06-13 20:20:40 +00007051 switch (eap->cmdidx)
7052 {
7053 case CMD_delete:
7054 oa.op_type = OP_DELETE;
7055 op_delete(&oa);
7056 break;
7057
7058 case CMD_yank:
7059 oa.op_type = OP_YANK;
7060 (void)op_yank(&oa, FALSE, TRUE);
7061 break;
7062
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007063 default: // CMD_rshift or CMD_lshift
Bram Moolenaar6e707362013-06-14 19:15:58 +02007064 if (
Bram Moolenaar071d4272004-06-13 20:20:40 +00007065#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6e707362013-06-14 19:15:58 +02007066 (eap->cmdidx == CMD_rshift) ^ curwin->w_p_rl
7067#else
7068 eap->cmdidx == CMD_rshift
Bram Moolenaar071d4272004-06-13 20:20:40 +00007069#endif
Bram Moolenaar6e707362013-06-14 19:15:58 +02007070 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00007071 oa.op_type = OP_RSHIFT;
7072 else
7073 oa.op_type = OP_LSHIFT;
7074 op_shift(&oa, FALSE, eap->amount);
7075 break;
7076 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007077 virtual_op = MAYBE;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007078 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007079}
7080
7081/*
7082 * ":put".
7083 */
7084 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007085ex_put(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007086{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007087 // ":0put" works like ":1put!".
Bram Moolenaar071d4272004-06-13 20:20:40 +00007088 if (eap->line2 == 0)
7089 {
7090 eap->line2 = 1;
7091 eap->forceit = TRUE;
7092 }
7093 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007094 do_put(eap->regname, eap->forceit ? BACKWARD : FORWARD, 1L,
7095 PUT_LINE|PUT_CURSLINE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007096}
7097
7098/*
7099 * Handle ":copy" and ":move".
7100 */
7101 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007102ex_copymove(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007103{
7104 long n;
7105
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02007106 n = get_address(eap, &eap->arg, eap->addr_type, FALSE, FALSE, FALSE, 1);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007107 if (eap->arg == NULL) // error detected
Bram Moolenaar071d4272004-06-13 20:20:40 +00007108 {
7109 eap->nextcmd = NULL;
7110 return;
7111 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00007112 get_flags(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007113
7114 /*
7115 * move or copy lines from 'eap->line1'-'eap->line2' to below line 'n'
7116 */
7117 if (n == MAXLNUM || n < 0 || n > curbuf->b_ml.ml_line_count)
7118 {
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02007119 emsg(_(e_invrange));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007120 return;
7121 }
7122
7123 if (eap->cmdidx == CMD_move)
7124 {
7125 if (do_move(eap->line1, eap->line2, n) == FAIL)
7126 return;
7127 }
7128 else
7129 ex_copy(eap->line1, eap->line2, n);
7130 u_clearline();
7131 beginline(BL_SOL | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007132 ex_may_print(eap);
7133}
7134
7135/*
7136 * Print the current line if flags were given to the Ex command.
7137 */
Bram Moolenaarfd3fe982014-04-01 17:49:44 +02007138 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007139ex_may_print(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007140{
7141 if (eap->flags != 0)
7142 {
7143 print_line(curwin->w_cursor.lnum, (eap->flags & EXFLAG_NR),
7144 (eap->flags & EXFLAG_LIST));
7145 ex_no_reprint = TRUE;
7146 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007147}
7148
7149/*
7150 * ":smagic" and ":snomagic".
7151 */
7152 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007153ex_submagic(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007154{
7155 int magic_save = p_magic;
7156
7157 p_magic = (eap->cmdidx == CMD_smagic);
7158 do_sub(eap);
7159 p_magic = magic_save;
7160}
7161
7162/*
7163 * ":join".
7164 */
7165 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007166ex_join(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007167{
7168 curwin->w_cursor.lnum = eap->line1;
7169 if (eap->line1 == eap->line2)
7170 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007171 if (eap->addr_count >= 2) // :2,2join does nothing
Bram Moolenaar071d4272004-06-13 20:20:40 +00007172 return;
7173 if (eap->line2 == curbuf->b_ml.ml_line_count)
7174 {
7175 beep_flush();
7176 return;
7177 }
7178 ++eap->line2;
7179 }
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02007180 (void)do_join(eap->line2 - eap->line1 + 1, !eap->forceit, TRUE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007181 beginline(BL_WHITE | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007182 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007183}
7184
7185/*
7186 * ":[addr]@r" or ":[addr]*r": execute register
7187 */
7188 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007189ex_at(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007190{
7191 int c;
Bram Moolenaar60462872009-11-03 11:40:19 +00007192 int prev_len = typebuf.tb_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007193
7194 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaar4930a762016-09-11 14:39:53 +02007195 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007196
7197#ifdef USE_ON_FLY_SCROLL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007198 dont_scroll = TRUE; // disallow scrolling here
Bram Moolenaar071d4272004-06-13 20:20:40 +00007199#endif
7200
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007201 // get the register name. No name means to use the previous one
Bram Moolenaar071d4272004-06-13 20:20:40 +00007202 c = *eap->arg;
7203 if (c == NUL || (c == '*' && *eap->cmd == '*'))
7204 c = '@';
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007205 // Put the register in the typeahead buffer with the "silent" flag.
Bram Moolenaard333d1e2006-11-07 17:43:47 +00007206 if (do_execreg(c, TRUE, vim_strchr(p_cpo, CPO_EXECBUF) != NULL, TRUE)
7207 == FAIL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007208 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007209 beep_flush();
Bram Moolenaardf177f62005-02-22 08:39:57 +00007210 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007211 else
7212 {
7213 int save_efr = exec_from_reg;
7214
7215 exec_from_reg = TRUE;
7216
7217 /*
7218 * Execute from the typeahead buffer.
Bram Moolenaar60462872009-11-03 11:40:19 +00007219 * Continue until the stuff buffer is empty and all added characters
7220 * have been consumed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007221 */
Bram Moolenaar60462872009-11-03 11:40:19 +00007222 while (!stuff_empty() || typebuf.tb_len > prev_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007223 (void)do_cmdline(NULL, getexline, NULL, DOCMD_NOWAIT|DOCMD_VERBOSE);
7224
7225 exec_from_reg = save_efr;
7226 }
7227}
7228
7229/*
7230 * ":!".
7231 */
7232 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007233ex_bang(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007234{
7235 do_bang(eap->addr_count, eap, eap->forceit, TRUE, TRUE);
7236}
7237
7238/*
7239 * ":undo".
7240 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007241 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +01007242ex_undo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007243{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007244 if (eap->addr_count == 1) // :undo 123
Bram Moolenaar730cde92010-06-27 05:18:54 +02007245 undo_time(eap->line2, FALSE, FALSE, TRUE);
Bram Moolenaard3667a22006-03-16 21:35:52 +00007246 else
7247 u_undo(1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007248}
7249
Bram Moolenaar55debbe2010-05-23 23:34:36 +02007250#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar6df6f472010-07-18 18:04:50 +02007251 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007252ex_wundo(exarg_T *eap)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02007253{
7254 char_u hash[UNDO_HASH_SIZE];
7255
7256 u_compute_hash(hash);
7257 u_write_undo(eap->arg, eap->forceit, curbuf, hash);
7258}
7259
Bram Moolenaar6df6f472010-07-18 18:04:50 +02007260 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007261ex_rundo(exarg_T *eap)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02007262{
7263 char_u hash[UNDO_HASH_SIZE];
7264
7265 u_compute_hash(hash);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02007266 u_read_undo(eap->arg, hash, NULL);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02007267}
7268#endif
7269
Bram Moolenaar071d4272004-06-13 20:20:40 +00007270/*
7271 * ":redo".
7272 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007273 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007274ex_redo(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007275{
7276 u_redo(1);
7277}
7278
7279/*
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007280 * ":earlier" and ":later".
7281 */
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007282 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007283ex_later(exarg_T *eap)
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007284{
7285 long count = 0;
7286 int sec = FALSE;
Bram Moolenaar730cde92010-06-27 05:18:54 +02007287 int file = FALSE;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007288 char_u *p = eap->arg;
7289
7290 if (*p == NUL)
7291 count = 1;
7292 else if (isdigit(*p))
7293 {
7294 count = getdigits(&p);
7295 switch (*p)
7296 {
7297 case 's': ++p; sec = TRUE; break;
7298 case 'm': ++p; sec = TRUE; count *= 60; break;
7299 case 'h': ++p; sec = TRUE; count *= 60 * 60; break;
Bram Moolenaar730cde92010-06-27 05:18:54 +02007300 case 'd': ++p; sec = TRUE; count *= 24 * 60 * 60; break;
7301 case 'f': ++p; file = TRUE; break;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007302 }
7303 }
7304
7305 if (*p != NUL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007306 semsg(_(e_invarg2), eap->arg);
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007307 else
Bram Moolenaar730cde92010-06-27 05:18:54 +02007308 undo_time(eap->cmdidx == CMD_earlier ? -count : count,
7309 sec, file, FALSE);
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007310}
7311
7312/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007313 * ":redir": start/stop redirection.
7314 */
7315 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007316ex_redir(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007317{
7318 char *mode;
7319 char_u *fname;
Bram Moolenaarca472992005-01-21 11:46:23 +00007320 char_u *arg = eap->arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007321
Bram Moolenaarba768492016-07-08 15:32:54 +02007322#ifdef FEAT_EVAL
Bram Moolenaar79815f12016-07-09 17:07:29 +02007323 if (redir_execute)
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02007324 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007325 emsg(_("E930: Cannot use :redir inside execute()"));
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02007326 return;
7327 }
Bram Moolenaarba768492016-07-08 15:32:54 +02007328#endif
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02007329
Bram Moolenaar071d4272004-06-13 20:20:40 +00007330 if (STRICMP(eap->arg, "END") == 0)
7331 close_redir();
7332 else
7333 {
Bram Moolenaarca472992005-01-21 11:46:23 +00007334 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00007335 {
Bram Moolenaarca472992005-01-21 11:46:23 +00007336 ++arg;
7337 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00007338 {
Bram Moolenaarca472992005-01-21 11:46:23 +00007339 ++arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007340 mode = "a";
7341 }
7342 else
7343 mode = "w";
Bram Moolenaarca472992005-01-21 11:46:23 +00007344 arg = skipwhite(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007345
7346 close_redir();
7347
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007348 // Expand environment variables and "~/".
Bram Moolenaarca472992005-01-21 11:46:23 +00007349 fname = expand_env_save(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007350 if (fname == NULL)
7351 return;
7352#ifdef FEAT_BROWSE
7353 if (cmdmod.browse)
7354 {
7355 char_u *browseFile;
7356
Bram Moolenaar7171abe2004-10-11 10:06:20 +00007357 browseFile = do_browse(BROWSE_SAVE,
7358 (char_u *)_("Save Redirection"),
Bram Moolenaarc36651b2018-04-29 12:22:56 +02007359 fname, NULL, NULL,
7360 (char_u *)_(BROWSE_FILTER_ALL_FILES), curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007361 if (browseFile == NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007362 return; // operation cancelled
Bram Moolenaar071d4272004-06-13 20:20:40 +00007363 vim_free(fname);
7364 fname = browseFile;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007365 eap->forceit = TRUE; // since dialog already asked
Bram Moolenaar071d4272004-06-13 20:20:40 +00007366 }
7367#endif
7368
7369 redir_fd = open_exfile(fname, eap->forceit, mode);
7370 vim_free(fname);
7371 }
7372#ifdef FEAT_EVAL
Bram Moolenaarca472992005-01-21 11:46:23 +00007373 else if (*arg == '@')
Bram Moolenaar071d4272004-06-13 20:20:40 +00007374 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007375 // redirect to a register a-z (resp. A-Z for appending)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007376 close_redir();
Bram Moolenaarca472992005-01-21 11:46:23 +00007377 ++arg;
7378 if (ASCII_ISALPHA(*arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007379# ifdef FEAT_CLIPBOARD
Bram Moolenaarca472992005-01-21 11:46:23 +00007380 || *arg == '*'
Bram Moolenaar9a51c6e2006-11-14 19:25:02 +00007381 || *arg == '+'
Bram Moolenaar071d4272004-06-13 20:20:40 +00007382# endif
Bram Moolenaarca472992005-01-21 11:46:23 +00007383 || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00007384 {
Bram Moolenaarca472992005-01-21 11:46:23 +00007385 redir_reg = *arg++;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007386 if (*arg == '>' && arg[1] == '>') // append
Bram Moolenaard9d30582005-05-18 22:10:28 +00007387 arg += 2;
Bram Moolenaarc188b882007-10-19 14:20:54 +00007388 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007389 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007390 // Can use both "@a" and "@a>".
Bram Moolenaar2c29bee2005-06-01 21:46:07 +00007391 if (*arg == '>')
7392 arg++;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007393 // Make register empty when not using @A-@Z and the
7394 // command is valid.
Bram Moolenaarc188b882007-10-19 14:20:54 +00007395 if (*arg == NUL && !isupper(redir_reg))
7396 write_reg_contents(redir_reg, (char_u *)"", -1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007397 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00007398 }
7399 if (*arg != NUL)
7400 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00007401 redir_reg = 0;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007402 semsg(_(e_invarg2), eap->arg);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007403 }
7404 }
7405 else if (*arg == '=' && arg[1] == '>')
7406 {
7407 int append;
7408
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007409 // redirect to a variable
Bram Moolenaardf177f62005-02-22 08:39:57 +00007410 close_redir();
7411 arg += 2;
7412
7413 if (*arg == '>')
7414 {
7415 ++arg;
7416 append = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007417 }
7418 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00007419 append = FALSE;
7420
7421 if (var_redir_start(skipwhite(arg), append) == OK)
7422 redir_vname = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007423 }
7424#endif
7425
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007426 // TODO: redirect to a buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00007427
Bram Moolenaar071d4272004-06-13 20:20:40 +00007428 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007429 semsg(_(e_invarg2), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007430 }
Bram Moolenaar29b2d262006-09-10 19:07:28 +00007431
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007432 // Make sure redirection is not off. Can happen for cmdline completion
7433 // that indirectly invokes a command to catch its output.
Bram Moolenaar29b2d262006-09-10 19:07:28 +00007434 if (redir_fd != NULL
7435#ifdef FEAT_EVAL
7436 || redir_reg || redir_vname
7437#endif
7438 )
7439 redir_off = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007440}
7441
7442/*
7443 * ":redraw": force redraw
7444 */
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01007445 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007446ex_redraw(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007447{
7448 int r = RedrawingDisabled;
7449 int p = p_lz;
7450
7451 RedrawingDisabled = 0;
7452 p_lz = FALSE;
Bram Moolenaarabc39ab2017-03-01 18:04:05 +01007453 validate_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007454 update_topline();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007455 update_screen(eap->forceit ? CLEAR : VIsual_active ? INVERTED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007456#ifdef FEAT_TITLE
7457 if (need_maketitle)
7458 maketitle();
7459#endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007460#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
7461# ifdef VIMDLL
7462 if (!gui.in_use)
7463# endif
7464 resize_console_buf();
Bram Moolenaar78d21da2019-02-17 15:00:52 +01007465#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007466 RedrawingDisabled = r;
7467 p_lz = p;
7468
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007469 // Reset msg_didout, so that a message that's there is overwritten.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007470 msg_didout = FALSE;
7471 msg_col = 0;
7472
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007473 // No need to wait after an intentional redraw.
Bram Moolenaar56667a52013-07-17 11:54:28 +02007474 need_wait_return = FALSE;
7475
Bram Moolenaar071d4272004-06-13 20:20:40 +00007476 out_flush();
7477}
7478
7479/*
7480 * ":redrawstatus": force redraw of status line(s)
7481 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007482 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007483ex_redrawstatus(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007484{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007485 int r = RedrawingDisabled;
7486 int p = p_lz;
7487
7488 RedrawingDisabled = 0;
7489 p_lz = FALSE;
7490 if (eap->forceit)
7491 status_redraw_all();
7492 else
7493 status_redraw_curbuf();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007494 update_screen(VIsual_active ? INVERTED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007495 RedrawingDisabled = r;
7496 p_lz = p;
7497 out_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007498}
7499
Bram Moolenaare12bab32019-01-08 22:02:56 +01007500/*
7501 * ":redrawtabline": force redraw of the tabline
7502 */
7503 static void
7504ex_redrawtabline(exarg_T *eap UNUSED)
7505{
7506 int r = RedrawingDisabled;
7507 int p = p_lz;
7508
7509 RedrawingDisabled = 0;
7510 p_lz = FALSE;
7511
7512 draw_tabline();
7513
7514 RedrawingDisabled = r;
7515 p_lz = p;
7516 out_flush();
7517}
7518
Bram Moolenaar071d4272004-06-13 20:20:40 +00007519 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007520close_redir(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007521{
7522 if (redir_fd != NULL)
7523 {
7524 fclose(redir_fd);
7525 redir_fd = NULL;
7526 }
7527#ifdef FEAT_EVAL
7528 redir_reg = 0;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007529 if (redir_vname)
7530 {
7531 var_redir_stop();
7532 redir_vname = 0;
7533 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007534#endif
7535}
7536
Bram Moolenaarf96ae0b2019-07-28 15:21:55 +02007537#if (defined(FEAT_SESSION) || defined(FEAT_EVAL)) || defined(PROTO)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007538 int
Bram Moolenaarbd67aac2019-09-21 23:09:04 +02007539vim_mkdir_emsg(char_u *name, int prot UNUSED)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007540{
7541 if (vim_mkdir(name, prot) != 0)
7542 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007543 semsg(_("E739: Cannot create directory: %s"), name);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007544 return FAIL;
7545 }
7546 return OK;
7547}
7548#endif
7549
Bram Moolenaar071d4272004-06-13 20:20:40 +00007550/*
7551 * Open a file for writing for an Ex command, with some checks.
7552 * Return file descriptor, or NULL on failure.
7553 */
7554 FILE *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007555open_exfile(
7556 char_u *fname,
7557 int forceit,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007558 char *mode) // "w" for create new file or "a" for append
Bram Moolenaar071d4272004-06-13 20:20:40 +00007559{
7560 FILE *fd;
7561
7562#ifdef UNIX
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007563 // with Unix it is possible to open a directory
Bram Moolenaar071d4272004-06-13 20:20:40 +00007564 if (mch_isdir(fname))
7565 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007566 semsg(_(e_isadir2), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007567 return NULL;
7568 }
7569#endif
7570 if (!forceit && *mode != 'a' && vim_fexists(fname))
7571 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007572 semsg(_("E189: \"%s\" exists (add ! to override)"), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007573 return NULL;
7574 }
7575
7576 if ((fd = mch_fopen((char *)fname, mode)) == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007577 semsg(_("E190: Cannot open \"%s\" for writing"), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007578
7579 return fd;
7580}
7581
7582/*
7583 * ":mark" and ":k".
7584 */
7585 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007586ex_mark(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007587{
7588 pos_T pos;
7589
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007590 if (*eap->arg == NUL) // No argument?
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007591 emsg(_(e_argreq));
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007592 else if (eap->arg[1] != NUL) // more than one character?
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007593 emsg(_(e_trailing));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007594 else
7595 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007596 pos = curwin->w_cursor; // save curwin->w_cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00007597 curwin->w_cursor.lnum = eap->line2;
7598 beginline(BL_WHITE | BL_FIX);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007599 if (setmark(*eap->arg) == FAIL) // set mark
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007600 emsg(_("E191: Argument must be a letter or forward/backward quote"));
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007601 curwin->w_cursor = pos; // restore curwin->w_cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00007602 }
7603}
7604
7605/*
7606 * Update w_topline, w_leftcol and the cursor position.
7607 */
7608 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007609update_topline_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007610{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007611 check_cursor(); // put cursor on valid line
Bram Moolenaar071d4272004-06-13 20:20:40 +00007612 update_topline();
7613 if (!curwin->w_p_wrap)
7614 validate_cursor();
7615 update_curswant();
7616}
7617
Bram Moolenaar071d4272004-06-13 20:20:40 +00007618/*
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007619 * Save the current State and go to Normal mode.
7620 * Return TRUE if the typeahead could be saved.
7621 */
7622 int
7623save_current_state(save_state_T *sst)
7624{
7625 sst->save_msg_scroll = msg_scroll;
7626 sst->save_restart_edit = restart_edit;
7627 sst->save_msg_didout = msg_didout;
7628 sst->save_State = State;
7629 sst->save_insertmode = p_im;
7630 sst->save_finish_op = finish_op;
7631 sst->save_opcount = opcount;
Bram Moolenaarcce713d2019-03-04 11:40:12 +01007632 sst->save_reg_executing = reg_executing;
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007633
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007634 msg_scroll = FALSE; // no msg scrolling in Normal mode
7635 restart_edit = 0; // don't go to Insert mode
7636 p_im = FALSE; // don't use 'insertmode'
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007637
7638 /*
7639 * Save the current typeahead. This is required to allow using ":normal"
7640 * from an event handler and makes sure we don't hang when the argument
7641 * ends with half a command.
7642 */
7643 save_typeahead(&sst->tabuf);
7644 return sst->tabuf.typebuf_valid;
7645}
7646
7647 void
7648restore_current_state(save_state_T *sst)
7649{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007650 // Restore the previous typeahead.
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007651 restore_typeahead(&sst->tabuf);
7652
7653 msg_scroll = sst->save_msg_scroll;
7654 restart_edit = sst->save_restart_edit;
7655 p_im = sst->save_insertmode;
7656 finish_op = sst->save_finish_op;
7657 opcount = sst->save_opcount;
Bram Moolenaarcce713d2019-03-04 11:40:12 +01007658 reg_executing = sst->save_reg_executing;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007659 msg_didout |= sst->save_msg_didout; // don't reset msg_didout now
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007660
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007661 // Restore the state (needed when called from a function executed for
7662 // 'indentexpr'). Update the mouse and cursor, they may have changed.
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007663 State = sst->save_State;
7664#ifdef CURSOR_SHAPE
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007665 ui_cursor_shape(); // may show different cursor shape
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007666#endif
7667}
7668
7669/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007670 * ":normal[!] {commands}": Execute normal mode commands.
7671 */
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01007672 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007673ex_normal(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007674{
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007675 save_state_T save_state;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007676 char_u *arg = NULL;
7677 int l;
7678 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007679
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00007680 if (ex_normal_lock > 0)
7681 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007682 emsg(_(e_secure));
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00007683 return;
7684 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007685 if (ex_normal_busy >= p_mmd)
7686 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007687 emsg(_("E192: Recursive use of :normal too deep"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007688 return;
7689 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007690
Bram Moolenaar071d4272004-06-13 20:20:40 +00007691 /*
7692 * vgetc() expects a CSI and K_SPECIAL to have been escaped. Don't do
7693 * this for the K_SPECIAL leading byte, otherwise special keys will not
7694 * work.
7695 */
7696 if (has_mbyte)
7697 {
7698 int len = 0;
7699
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007700 // Count the number of characters to be escaped.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007701 for (p = eap->arg; *p != NUL; ++p)
7702 {
Bram Moolenaar13505972019-01-24 15:04:48 +01007703#ifdef FEAT_GUI
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007704 if (*p == CSI) // leadbyte CSI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007705 len += 2;
Bram Moolenaar13505972019-01-24 15:04:48 +01007706#endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007707 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007708 if (*++p == K_SPECIAL // trailbyte K_SPECIAL or CSI
Bram Moolenaar13505972019-01-24 15:04:48 +01007709#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007710 || *p == CSI
Bram Moolenaar13505972019-01-24 15:04:48 +01007711#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007712 )
7713 len += 2;
7714 }
7715 if (len > 0)
7716 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02007717 arg = alloc(STRLEN(eap->arg) + len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007718 if (arg != NULL)
7719 {
7720 len = 0;
7721 for (p = eap->arg; *p != NUL; ++p)
7722 {
7723 arg[len++] = *p;
Bram Moolenaar13505972019-01-24 15:04:48 +01007724#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007725 if (*p == CSI)
7726 {
7727 arg[len++] = KS_EXTRA;
7728 arg[len++] = (int)KE_CSI;
7729 }
Bram Moolenaar13505972019-01-24 15:04:48 +01007730#endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007731 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007732 {
7733 arg[len++] = *++p;
7734 if (*p == K_SPECIAL)
7735 {
7736 arg[len++] = KS_SPECIAL;
7737 arg[len++] = KE_FILLER;
7738 }
Bram Moolenaar13505972019-01-24 15:04:48 +01007739#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007740 else if (*p == CSI)
7741 {
7742 arg[len++] = KS_EXTRA;
7743 arg[len++] = (int)KE_CSI;
7744 }
Bram Moolenaar13505972019-01-24 15:04:48 +01007745#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007746 }
7747 arg[len] = NUL;
7748 }
7749 }
7750 }
7751 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007752
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007753 ++ex_normal_busy;
7754 if (save_current_state(&save_state))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007755 {
7756 /*
7757 * Repeat the :normal command for each line in the range. When no
7758 * range given, execute it just once, without positioning the cursor
7759 * first.
7760 */
7761 do
7762 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007763 if (eap->addr_count != 0)
7764 {
7765 curwin->w_cursor.lnum = eap->line1++;
7766 curwin->w_cursor.col = 0;
Bram Moolenaard5d37532017-03-27 23:02:07 +02007767 check_cursor_moved(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007768 }
7769
Bram Moolenaar13505972019-01-24 15:04:48 +01007770 exec_normal_cmd(arg != NULL
7771 ? arg
7772 : eap->arg, eap->forceit ? REMAP_NONE : REMAP_YES, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007773 }
7774 while (eap->addr_count > 0 && eap->line1 <= eap->line2 && !got_int);
7775 }
7776
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007777 // Might not return to the main loop when in an event handler.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007778 update_topline_cursor();
7779
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007780 restore_current_state(&save_state);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007781 --ex_normal_busy;
Bram Moolenaareda73602014-11-05 09:53:23 +01007782 setmouse();
Bram Moolenaareda73602014-11-05 09:53:23 +01007783#ifdef CURSOR_SHAPE
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007784 ui_cursor_shape(); // may show different cursor shape
Bram Moolenaareda73602014-11-05 09:53:23 +01007785#endif
7786
Bram Moolenaar071d4272004-06-13 20:20:40 +00007787 vim_free(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007788}
7789
7790/*
Bram Moolenaar64969662005-12-14 21:59:55 +00007791 * ":startinsert", ":startreplace" and ":startgreplace"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007792 */
7793 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007794ex_startinsert(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007795{
Bram Moolenaarfd371682005-01-14 21:42:54 +00007796 if (eap->forceit)
7797 {
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02007798 // cursor line can be zero on startup
Bram Moolenaar09ca9322017-09-26 17:40:45 +02007799 if (!curwin->w_cursor.lnum)
7800 curwin->w_cursor.lnum = 1;
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02007801 set_cursor_for_append_to_line();
Bram Moolenaarfd371682005-01-14 21:42:54 +00007802 }
Bram Moolenaareb58a242020-04-19 18:13:19 +02007803#ifdef FEAT_TERMINAL
7804 // Ignore this when running in an active terminal.
7805 if (term_job_running(curbuf->b_term))
7806 return;
7807#endif
Bram Moolenaarfd371682005-01-14 21:42:54 +00007808
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02007809 // Ignore the command when already in Insert mode. Inserting an
7810 // expression register that invokes a function can do this.
Bram Moolenaara40c5002005-01-09 21:16:21 +00007811 if (State & INSERT)
7812 return;
7813
Bram Moolenaar64969662005-12-14 21:59:55 +00007814 if (eap->cmdidx == CMD_startinsert)
7815 restart_edit = 'a';
7816 else if (eap->cmdidx == CMD_startreplace)
7817 restart_edit = 'R';
Bram Moolenaar071d4272004-06-13 20:20:40 +00007818 else
Bram Moolenaar64969662005-12-14 21:59:55 +00007819 restart_edit = 'V';
7820
7821 if (!eap->forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007822 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00007823 if (eap->cmdidx == CMD_startinsert)
7824 restart_edit = 'i';
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02007825 curwin->w_curswant = 0; // avoid MAXCOL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007826 }
7827}
7828
7829/*
7830 * ":stopinsert"
7831 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007832 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007833ex_stopinsert(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007834{
7835 restart_edit = 0;
7836 stop_insert_mode = TRUE;
Bram Moolenaarfd773e92016-04-02 19:39:16 +02007837 clearmode();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007838}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007839
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00007840/*
7841 * Execute normal mode command "cmd".
7842 * "remap" can be REMAP_NONE or REMAP_YES.
7843 */
7844 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007845exec_normal_cmd(char_u *cmd, int remap, int silent)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00007846{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007847 // Stuff the argument into the typeahead buffer.
Bram Moolenaar25281632016-01-21 23:32:32 +01007848 ins_typebuf(cmd, remap, 0, TRUE, silent);
Bram Moolenaar586c70c2018-10-02 16:23:58 +02007849 exec_normal(FALSE, FALSE, FALSE);
Bram Moolenaar25281632016-01-21 23:32:32 +01007850}
Bram Moolenaar25281632016-01-21 23:32:32 +01007851
Bram Moolenaar25281632016-01-21 23:32:32 +01007852/*
7853 * Execute normal_cmd() until there is no typeahead left.
Bram Moolenaar586c70c2018-10-02 16:23:58 +02007854 * When "use_vpeekc" is TRUE use vpeekc() to check for available chars.
Bram Moolenaar25281632016-01-21 23:32:32 +01007855 */
7856 void
Bram Moolenaar586c70c2018-10-02 16:23:58 +02007857exec_normal(int was_typed, int use_vpeekc, int may_use_terminal_loop UNUSED)
Bram Moolenaar25281632016-01-21 23:32:32 +01007858{
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00007859 oparg_T oa;
Bram Moolenaar905dd902019-04-07 14:21:47 +02007860 int c;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00007861
Bram Moolenaar905dd902019-04-07 14:21:47 +02007862 // When calling vpeekc() from feedkeys() it will return Ctrl_C when there
7863 // is nothing to get, so also check for Ctrl_C.
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00007864 clear_oparg(&oa);
7865 finish_op = FALSE;
Bram Moolenaar586c70c2018-10-02 16:23:58 +02007866 while ((!stuff_empty()
7867 || ((was_typed || !typebuf_typed()) && typebuf.tb_len > 0)
Bram Moolenaar905dd902019-04-07 14:21:47 +02007868 || (use_vpeekc && (c = vpeekc()) != NUL && c != Ctrl_C))
Bram Moolenaar586c70c2018-10-02 16:23:58 +02007869 && !got_int)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00007870 {
7871 update_topline_cursor();
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02007872#ifdef FEAT_TERMINAL
Bram Moolenaar655a82a2018-05-08 22:01:07 +02007873 if (may_use_terminal_loop && term_use_loop()
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02007874 && oa.op_type == OP_NOP && oa.regname == NUL
7875 && !VIsual_active)
7876 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007877 // If terminal_loop() returns OK we got a key that is handled
7878 // in Normal model. With FAIL we first need to position the
7879 // cursor and the screen needs to be redrawn.
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02007880 if (terminal_loop(TRUE) == OK)
7881 normal_cmd(&oa, TRUE);
7882 }
7883 else
7884#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007885 // execute a Normal mode cmd
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02007886 normal_cmd(&oa, TRUE);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00007887 }
7888}
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00007889
Bram Moolenaar071d4272004-06-13 20:20:40 +00007890#ifdef FEAT_FIND_ID
7891 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007892ex_checkpath(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007893{
7894 find_pattern_in_path(NULL, 0, 0, FALSE, FALSE, CHECK_PATH, 1L,
7895 eap->forceit ? ACTION_SHOW_ALL : ACTION_SHOW,
7896 (linenr_T)1, (linenr_T)MAXLNUM);
7897}
7898
Bram Moolenaar4033c552017-09-16 20:54:51 +02007899#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007900/*
7901 * ":psearch"
7902 */
7903 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007904ex_psearch(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007905{
7906 g_do_tagpreview = p_pvh;
7907 ex_findpat(eap);
7908 g_do_tagpreview = 0;
7909}
7910#endif
7911
7912 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007913ex_findpat(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007914{
7915 int whole = TRUE;
7916 long n;
7917 char_u *p;
7918 int action;
7919
7920 switch (cmdnames[eap->cmdidx].cmd_name[2])
7921 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007922 case 'e': // ":psearch", ":isearch" and ":dsearch"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007923 if (cmdnames[eap->cmdidx].cmd_name[0] == 'p')
7924 action = ACTION_GOTO;
7925 else
7926 action = ACTION_SHOW;
7927 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007928 case 'i': // ":ilist" and ":dlist"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007929 action = ACTION_SHOW_ALL;
7930 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007931 case 'u': // ":ijump" and ":djump"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007932 action = ACTION_GOTO;
7933 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007934 default: // ":isplit" and ":dsplit"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007935 action = ACTION_SPLIT;
7936 break;
7937 }
7938
7939 n = 1;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007940 if (vim_isdigit(*eap->arg)) // get count
Bram Moolenaar071d4272004-06-13 20:20:40 +00007941 {
7942 n = getdigits(&eap->arg);
7943 eap->arg = skipwhite(eap->arg);
7944 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007945 if (*eap->arg == '/') // Match regexp, not just whole words
Bram Moolenaar071d4272004-06-13 20:20:40 +00007946 {
7947 whole = FALSE;
7948 ++eap->arg;
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02007949 p = skip_regexp(eap->arg, '/', p_magic);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007950 if (*p)
7951 {
7952 *p++ = NUL;
7953 p = skipwhite(p);
7954
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007955 // Check for trailing illegal characters
Bram Moolenaarfaac4102020-04-20 17:46:14 +02007956 if (!ends_excmd2(eap->arg, p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007957 eap->errmsg = e_trailing;
7958 else
7959 eap->nextcmd = check_nextcmd(p);
7960 }
7961 }
7962 if (!eap->skip)
7963 find_pattern_in_path(eap->arg, 0, (int)STRLEN(eap->arg),
7964 whole, !eap->forceit,
7965 *eap->cmd == 'd' ? FIND_DEFINE : FIND_ANY,
7966 n, action, eap->line1, eap->line2);
7967}
7968#endif
7969
Bram Moolenaar071d4272004-06-13 20:20:40 +00007970
Bram Moolenaar4033c552017-09-16 20:54:51 +02007971#ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00007972/*
7973 * ":ptag", ":ptselect", ":ptjump", ":ptnext", etc.
7974 */
7975 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007976ex_ptag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007977{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007978 g_do_tagpreview = p_pvh; // will be reset to 0 in ex_tag_cmd()
Bram Moolenaar071d4272004-06-13 20:20:40 +00007979 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
7980}
7981
7982/*
7983 * ":pedit"
7984 */
7985 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007986ex_pedit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007987{
7988 win_T *curwin_save = curwin;
7989
Bram Moolenaar3a2505c2020-03-09 16:40:41 +01007990 if (ERROR_IF_ANY_POPUP_WINDOW)
7991 return;
7992
Bram Moolenaar026587b2019-08-17 15:08:00 +02007993 // Open the preview window or popup and make it the current window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007994 g_do_tagpreview = p_pvh;
Bram Moolenaar576a4a62019-08-18 15:25:17 +02007995 prepare_tagpreview(TRUE, TRUE, FALSE);
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02007996
Bram Moolenaar026587b2019-08-17 15:08:00 +02007997 // Edit the file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007998 do_exedit(eap, NULL);
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02007999
Bram Moolenaar071d4272004-06-13 20:20:40 +00008000 if (curwin != curwin_save && win_valid(curwin_save))
8001 {
Bram Moolenaar026587b2019-08-17 15:08:00 +02008002 // Return cursor to where we were
Bram Moolenaar071d4272004-06-13 20:20:40 +00008003 validate_cursor();
8004 redraw_later(VALID);
8005 win_enter(curwin_save, TRUE);
8006 }
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01008007# ifdef FEAT_PROP_POPUP
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02008008 else if (WIN_IS_POPUP(curwin))
8009 {
8010 // can't keep focus in popup window
8011 win_enter(firstwin, TRUE);
8012 }
8013# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008014 g_do_tagpreview = 0;
8015}
Bram Moolenaar4033c552017-09-16 20:54:51 +02008016#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008017
8018/*
8019 * ":stag", ":stselect" and ":stjump".
8020 */
8021 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008022ex_stag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008023{
8024 postponed_split = -1;
8025 postponed_split_flags = cmdmod.split;
Bram Moolenaard326ce82007-03-11 14:48:29 +00008026 postponed_split_tab = cmdmod.tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008027 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
8028 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00008029 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008030}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008031
8032/*
8033 * ":tag", ":tselect", ":tjump", ":tnext", etc.
8034 */
8035 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008036ex_tag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008037{
8038 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name);
8039}
8040
8041 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008042ex_tag_cmd(exarg_T *eap, char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008043{
8044 int cmd;
8045
8046 switch (name[1])
8047 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008048 case 'j': cmd = DT_JUMP; // ":tjump"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008049 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008050 case 's': cmd = DT_SELECT; // ":tselect"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008051 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008052 case 'p': cmd = DT_PREV; // ":tprevious"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008053 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008054 case 'N': cmd = DT_PREV; // ":tNext"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008055 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008056 case 'n': cmd = DT_NEXT; // ":tnext"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008057 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008058 case 'o': cmd = DT_POP; // ":pop"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008059 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008060 case 'f': // ":tfirst"
8061 case 'r': cmd = DT_FIRST; // ":trewind"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008062 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008063 case 'l': cmd = DT_LAST; // ":tlast"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008064 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008065 default: // ":tag"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008066#ifdef FEAT_CSCOPE
Bram Moolenaar7c94c262008-06-20 09:11:34 +00008067 if (p_cst && *eap->arg != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008068 {
Bram Moolenaard4db7712016-11-12 19:16:46 +01008069 ex_cstag(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008070 return;
8071 }
8072#endif
8073 cmd = DT_TAG;
8074 break;
8075 }
8076
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00008077 if (name[0] == 'l')
8078 {
8079#ifndef FEAT_QUICKFIX
8080 ex_ni(eap);
8081 return;
8082#else
8083 cmd = DT_LTAG;
8084#endif
8085 }
8086
Bram Moolenaar071d4272004-06-13 20:20:40 +00008087 do_tag(eap->arg, cmd, eap->addr_count > 0 ? (int)eap->line2 : 1,
8088 eap->forceit, TRUE);
8089}
8090
8091/*
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008092 * Check "str" for starting with a special cmdline variable.
8093 * If found return one of the SPEC_ values and set "*usedlen" to the length of
8094 * the variable. Otherwise return -1 and "*usedlen" is unchanged.
8095 */
8096 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008097find_cmdline_var(char_u *src, int *usedlen)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008098{
8099 int len;
8100 int i;
Bram Moolenaar8f0b2d42009-05-16 14:41:10 +00008101 static char *(spec_str[]) = {
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008102 "%",
8103#define SPEC_PERC 0
8104 "#",
Bram Moolenaar65f08472017-09-10 18:16:20 +02008105#define SPEC_HASH (SPEC_PERC + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008106 "<cword>", // cursor word
Bram Moolenaar65f08472017-09-10 18:16:20 +02008107#define SPEC_CWORD (SPEC_HASH + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008108 "<cWORD>", // cursor WORD
Bram Moolenaar65f08472017-09-10 18:16:20 +02008109#define SPEC_CCWORD (SPEC_CWORD + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008110 "<cexpr>", // expr under cursor
Bram Moolenaar65f08472017-09-10 18:16:20 +02008111#define SPEC_CEXPR (SPEC_CCWORD + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008112 "<cfile>", // cursor path name
Bram Moolenaar65f08472017-09-10 18:16:20 +02008113#define SPEC_CFILE (SPEC_CEXPR + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008114 "<sfile>", // ":so" file name
Bram Moolenaar65f08472017-09-10 18:16:20 +02008115#define SPEC_SFILE (SPEC_CFILE + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008116 "<slnum>", // ":so" file line number
Bram Moolenaar65f08472017-09-10 18:16:20 +02008117#define SPEC_SLNUM (SPEC_SFILE + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008118 "<afile>", // autocommand file name
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008119#define SPEC_AFILE (SPEC_SLNUM + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008120 "<abuf>", // autocommand buffer number
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008121#define SPEC_ABUF (SPEC_AFILE + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008122 "<amatch>", // autocommand match name
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01008123#define SPEC_AMATCH (SPEC_ABUF + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008124 "<sflnum>", // script file line number
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008125#define SPEC_SFLNUM (SPEC_AMATCH + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008126#ifdef FEAT_CLIENTSERVER
8127 "<client>"
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008128# define SPEC_CLIENT (SPEC_SFLNUM + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008129#endif
8130 };
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008131
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00008132 for (i = 0; i < (int)(sizeof(spec_str) / sizeof(char *)); ++i)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008133 {
8134 len = (int)STRLEN(spec_str[i]);
8135 if (STRNCMP(src, spec_str[i], len) == 0)
8136 {
8137 *usedlen = len;
8138 return i;
8139 }
8140 }
8141 return -1;
8142}
8143
8144/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008145 * Evaluate cmdline variables.
8146 *
8147 * change '%' to curbuf->b_ffname
8148 * '#' to curwin->w_altfile
8149 * '<cword>' to word under the cursor
8150 * '<cWORD>' to WORD under the cursor
Bram Moolenaar8071cb22019-07-12 17:58:01 +02008151 * '<cexpr>' to C-expression under the cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00008152 * '<cfile>' to path name under the cursor
8153 * '<sfile>' to sourced file name
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01008154 * '<slnum>' to sourced file line number
Bram Moolenaar071d4272004-06-13 20:20:40 +00008155 * '<afile>' to file name for autocommand
8156 * '<abuf>' to buffer number for autocommand
8157 * '<amatch>' to matching name for autocommand
8158 *
8159 * When an error is detected, "errormsg" is set to a non-NULL pointer (may be
8160 * "" for error without a message) and NULL is returned.
8161 * Returns an allocated string if a valid match was found.
8162 * Returns NULL if no match was found. "usedlen" then still contains the
8163 * number of characters to skip.
8164 */
8165 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008166eval_vars(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008167 char_u *src, // pointer into commandline
8168 char_u *srcstart, // beginning of valid memory for src
8169 int *usedlen, // characters after src that are used
8170 linenr_T *lnump, // line number for :e command, or NULL
8171 char **errormsg, // pointer to error message
8172 int *escaped) // return value has escaped white space (can
8173 // be NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008174{
8175 int i;
8176 char_u *s;
8177 char_u *result;
8178 char_u *resultbuf = NULL;
8179 int resultlen;
8180 buf_T *buf;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008181 int valid = VALID_HEAD + VALID_PATH; // assume valid result
Bram Moolenaar071d4272004-06-13 20:20:40 +00008182 int spec_idx;
Bram Moolenaarfd249462018-07-28 16:14:30 +02008183 int tilde_file = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008184 int skip_mod = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008185 char_u strbuf[30];
Bram Moolenaar071d4272004-06-13 20:20:40 +00008186
8187 *errormsg = NULL;
Bram Moolenaar63b92542007-03-27 14:57:09 +00008188 if (escaped != NULL)
8189 *escaped = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008190
8191 /*
8192 * Check if there is something to do.
8193 */
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008194 spec_idx = find_cmdline_var(src, usedlen);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008195 if (spec_idx < 0) // no match
Bram Moolenaar071d4272004-06-13 20:20:40 +00008196 {
8197 *usedlen = 1;
8198 return NULL;
8199 }
8200
8201 /*
8202 * Skip when preceded with a backslash "\%" and "\#".
8203 * Note: In "\\%" the % is also not recognized!
8204 */
8205 if (src > srcstart && src[-1] == '\\')
8206 {
8207 *usedlen = 0;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008208 STRMOVE(src - 1, src); // remove backslash
Bram Moolenaar071d4272004-06-13 20:20:40 +00008209 return NULL;
8210 }
8211
8212 /*
8213 * word or WORD under cursor
8214 */
Bram Moolenaar65f08472017-09-10 18:16:20 +02008215 if (spec_idx == SPEC_CWORD || spec_idx == SPEC_CCWORD
8216 || spec_idx == SPEC_CEXPR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008217 {
Bram Moolenaar65f08472017-09-10 18:16:20 +02008218 resultlen = find_ident_under_cursor(&result,
8219 spec_idx == SPEC_CWORD ? (FIND_IDENT | FIND_STRING)
8220 : spec_idx == SPEC_CEXPR ? (FIND_IDENT | FIND_STRING | FIND_EVAL)
8221 : FIND_STRING);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008222 if (resultlen == 0)
8223 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008224 *errormsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +00008225 return NULL;
8226 }
8227 }
8228
8229 /*
8230 * '#': Alternate file name
8231 * '%': Current file name
8232 * File name under the cursor
8233 * File name for autocommand
8234 * and following modifiers
8235 */
8236 else
8237 {
8238 switch (spec_idx)
8239 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008240 case SPEC_PERC: // '%': current file
Bram Moolenaar071d4272004-06-13 20:20:40 +00008241 if (curbuf->b_fname == NULL)
8242 {
8243 result = (char_u *)"";
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008244 valid = 0; // Must have ":p:h" to be valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00008245 }
8246 else
Bram Moolenaar00136dc2018-07-25 21:19:13 +02008247 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008248 result = curbuf->b_fname;
Bram Moolenaar00136dc2018-07-25 21:19:13 +02008249 tilde_file = STRCMP(result, "~") == 0;
8250 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008251 break;
8252
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008253 case SPEC_HASH: // '#' or "#99": alternate file
8254 if (src[1] == '#') // "##": the argument list
Bram Moolenaar071d4272004-06-13 20:20:40 +00008255 {
8256 result = arg_all();
8257 resultbuf = result;
8258 *usedlen = 2;
Bram Moolenaar63b92542007-03-27 14:57:09 +00008259 if (escaped != NULL)
8260 *escaped = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008261 skip_mod = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008262 break;
8263 }
8264 s = src + 1;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008265 if (*s == '<') // "#<99" uses v:oldfiles
Bram Moolenaard812df62008-11-09 12:46:09 +00008266 ++s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008267 i = (int)getdigits(&s);
Bram Moolenaarc312b8b2017-10-28 17:53:04 +02008268 if (s == src + 2 && src[1] == '-')
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008269 // just a minus sign, don't skip over it
Bram Moolenaarc312b8b2017-10-28 17:53:04 +02008270 s--;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008271 *usedlen = (int)(s - src); // length of what we expand
Bram Moolenaar071d4272004-06-13 20:20:40 +00008272
Bram Moolenaarc312b8b2017-10-28 17:53:04 +02008273 if (src[1] == '<' && i != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008274 {
Bram Moolenaard812df62008-11-09 12:46:09 +00008275 if (*usedlen < 2)
8276 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008277 // Should we give an error message for #<text?
Bram Moolenaard812df62008-11-09 12:46:09 +00008278 *usedlen = 1;
8279 return NULL;
8280 }
8281#ifdef FEAT_EVAL
8282 result = list_find_str(get_vim_var_list(VV_OLDFILES),
8283 (long)i);
8284 if (result == NULL)
8285 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008286 *errormsg = "";
Bram Moolenaard812df62008-11-09 12:46:09 +00008287 return NULL;
8288 }
8289#else
Bram Moolenaar8e481e82019-01-15 20:07:48 +01008290 *errormsg = _("E809: #< is not available without the +eval feature");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008291 return NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +00008292#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008293 }
8294 else
Bram Moolenaard812df62008-11-09 12:46:09 +00008295 {
Bram Moolenaarc312b8b2017-10-28 17:53:04 +02008296 if (i == 0 && src[1] == '<' && *usedlen > 1)
8297 *usedlen = 1;
Bram Moolenaard812df62008-11-09 12:46:09 +00008298 buf = buflist_findnr(i);
8299 if (buf == NULL)
8300 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008301 *errormsg = _("E194: No alternate file name to substitute for '#'");
Bram Moolenaard812df62008-11-09 12:46:09 +00008302 return NULL;
8303 }
8304 if (lnump != NULL)
8305 *lnump = ECMD_LAST;
8306 if (buf->b_fname == NULL)
8307 {
8308 result = (char_u *)"";
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008309 valid = 0; // Must have ":p:h" to be valid
Bram Moolenaard812df62008-11-09 12:46:09 +00008310 }
8311 else
Bram Moolenaar00136dc2018-07-25 21:19:13 +02008312 {
Bram Moolenaard812df62008-11-09 12:46:09 +00008313 result = buf->b_fname;
Bram Moolenaar00136dc2018-07-25 21:19:13 +02008314 tilde_file = STRCMP(result, "~") == 0;
8315 }
Bram Moolenaard812df62008-11-09 12:46:09 +00008316 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008317 break;
8318
8319#ifdef FEAT_SEARCHPATH
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008320 case SPEC_CFILE: // file name under cursor
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008321 result = file_name_at_cursor(FNAME_MESS|FNAME_HYP, 1L, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008322 if (result == NULL)
8323 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008324 *errormsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +00008325 return NULL;
8326 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008327 resultbuf = result; // remember allocated string
Bram Moolenaar071d4272004-06-13 20:20:40 +00008328 break;
8329#endif
8330
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008331 case SPEC_AFILE: // file name for autocommand
Bram Moolenaar071d4272004-06-13 20:20:40 +00008332 result = autocmd_fname;
Bram Moolenaarf6dad432008-09-18 19:29:58 +00008333 if (result != NULL && !autocmd_fname_full)
8334 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008335 // Still need to turn the fname into a full path. It is
8336 // postponed to avoid a delay when <afile> is not used.
Bram Moolenaarf6dad432008-09-18 19:29:58 +00008337 autocmd_fname_full = TRUE;
8338 result = FullName_save(autocmd_fname, FALSE);
8339 vim_free(autocmd_fname);
8340 autocmd_fname = result;
8341 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008342 if (result == NULL)
8343 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008344 *errormsg = _("E495: no autocommand file name to substitute for \"<afile>\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008345 return NULL;
8346 }
Bram Moolenaara0174af2008-01-02 20:08:25 +00008347 result = shorten_fname1(result);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008348 break;
8349
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008350 case SPEC_ABUF: // buffer number for autocommand
Bram Moolenaar071d4272004-06-13 20:20:40 +00008351 if (autocmd_bufnr <= 0)
8352 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008353 *errormsg = _("E496: no autocommand buffer number to substitute for \"<abuf>\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008354 return NULL;
8355 }
8356 sprintf((char *)strbuf, "%d", autocmd_bufnr);
8357 result = strbuf;
8358 break;
8359
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008360 case SPEC_AMATCH: // match name for autocommand
Bram Moolenaar071d4272004-06-13 20:20:40 +00008361 result = autocmd_match;
8362 if (result == NULL)
8363 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008364 *errormsg = _("E497: no autocommand match name to substitute for \"<amatch>\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008365 return NULL;
8366 }
8367 break;
8368
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008369 case SPEC_SFILE: // file name for ":so" command
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01008370 result = estack_sfile();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008371 if (result == NULL)
8372 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008373 *errormsg = _("E498: no :source file name to substitute for \"<sfile>\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008374 return NULL;
8375 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01008376 resultbuf = result; // remember allocated string
Bram Moolenaar071d4272004-06-13 20:20:40 +00008377 break;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008378
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008379 case SPEC_SLNUM: // line in file for ":so" command
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01008380 if (SOURCING_NAME == NULL || SOURCING_LNUM == 0)
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01008381 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008382 *errormsg = _("E842: no line number to use for \"<slnum>\"");
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01008383 return NULL;
8384 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01008385 sprintf((char *)strbuf, "%ld", SOURCING_LNUM);
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01008386 result = strbuf;
8387 break;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008388
8389#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008390 case SPEC_SFLNUM: // line in script file
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01008391 if (current_sctx.sc_lnum + SOURCING_LNUM == 0)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008392 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008393 *errormsg = _("E961: no line number to use for \"<sflnum>\"");
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008394 return NULL;
8395 }
8396 sprintf((char *)strbuf, "%ld",
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01008397 (long)(current_sctx.sc_lnum + SOURCING_LNUM));
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008398 result = strbuf;
8399 break;
8400#endif
8401
8402#ifdef FEAT_CLIENTSERVER
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008403 case SPEC_CLIENT: // Source of last submitted input
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008404 sprintf((char *)strbuf, PRINTF_HEX_LONG_U,
8405 (long_u)clientWindow);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008406 result = strbuf;
8407 break;
8408#endif
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008409
Bram Moolenaar9e0f6ec2017-05-16 09:36:54 +02008410 default:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008411 result = (char_u *)""; // avoid gcc warning
Bram Moolenaar9e0f6ec2017-05-16 09:36:54 +02008412 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008413 }
8414
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008415 resultlen = (int)STRLEN(result); // length of new string
8416 if (src[*usedlen] == '<') // remove the file name extension
Bram Moolenaar071d4272004-06-13 20:20:40 +00008417 {
8418 ++*usedlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008419 if ((s = vim_strrchr(result, '.')) != NULL && s >= gettail(result))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008420 resultlen = (int)(s - result);
8421 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008422 else if (!skip_mod)
8423 {
Bram Moolenaar00136dc2018-07-25 21:19:13 +02008424 valid |= modify_fname(src, tilde_file, usedlen, &result, &resultbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008425 &resultlen);
8426 if (result == NULL)
8427 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008428 *errormsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +00008429 return NULL;
8430 }
8431 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008432 }
8433
8434 if (resultlen == 0 || valid != VALID_HEAD + VALID_PATH)
8435 {
8436 if (valid != VALID_HEAD + VALID_PATH)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008437 // xgettext:no-c-format
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008438 *errormsg = _("E499: Empty file name for '%' or '#', only works with \":p:h\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008439 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008440 *errormsg = _("E500: Evaluates to an empty string");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008441 result = NULL;
8442 }
8443 else
8444 result = vim_strnsave(result, resultlen);
8445 vim_free(resultbuf);
8446 return result;
8447}
8448
8449/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008450 * Expand the <sfile> string in "arg".
8451 *
8452 * Returns an allocated string, or NULL for any error.
8453 */
8454 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008455expand_sfile(char_u *arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008456{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008457 char *errormsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008458 int len;
8459 char_u *result;
8460 char_u *newres;
8461 char_u *repl;
8462 int srclen;
8463 char_u *p;
8464
8465 result = vim_strsave(arg);
8466 if (result == NULL)
8467 return NULL;
8468
8469 for (p = result; *p; )
8470 {
8471 if (STRNCMP(p, "<sfile>", 7) != 0)
8472 ++p;
8473 else
8474 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008475 // replace "<sfile>" with the sourced file name, and do ":" stuff
Bram Moolenaar63b92542007-03-27 14:57:09 +00008476 repl = eval_vars(p, result, &srclen, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008477 if (errormsg != NULL)
8478 {
8479 if (*errormsg)
8480 emsg(errormsg);
8481 vim_free(result);
8482 return NULL;
8483 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008484 if (repl == NULL) // no match (cannot happen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008485 {
8486 p += srclen;
8487 continue;
8488 }
8489 len = (int)STRLEN(result) - srclen + (int)STRLEN(repl) + 1;
8490 newres = alloc(len);
8491 if (newres == NULL)
8492 {
8493 vim_free(repl);
8494 vim_free(result);
8495 return NULL;
8496 }
8497 mch_memmove(newres, result, (size_t)(p - result));
8498 STRCPY(newres + (p - result), repl);
8499 len = (int)STRLEN(newres);
8500 STRCAT(newres, p + srclen);
8501 vim_free(repl);
8502 vim_free(result);
8503 result = newres;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008504 p = newres + len; // continue after the match
Bram Moolenaar071d4272004-06-13 20:20:40 +00008505 }
8506 }
8507
8508 return result;
8509}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008510
Bram Moolenaar071d4272004-06-13 20:20:40 +00008511#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG) || defined(PROTO)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00008512/*
Bram Moolenaard9462e32011-04-11 21:35:11 +02008513 * Make a dialog message in "buff[DIALOG_MSG_SIZE]".
Bram Moolenaarb765d632005-06-07 21:00:02 +00008514 * "format" must contain "%s".
Bram Moolenaar9c13b352005-05-19 20:53:52 +00008515 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008516 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008517dialog_msg(char_u *buff, char *format, char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008518{
Bram Moolenaar071d4272004-06-13 20:20:40 +00008519 if (fname == NULL)
8520 fname = (char_u *)_("Untitled");
Bram Moolenaard9462e32011-04-11 21:35:11 +02008521 vim_snprintf((char *)buff, DIALOG_MSG_SIZE, format, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008522}
8523#endif
8524
8525/*
8526 * ":behave {mswin,xterm}"
8527 */
8528 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008529ex_behave(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008530{
8531 if (STRCMP(eap->arg, "mswin") == 0)
8532 {
8533 set_option_value((char_u *)"selection", 0L, (char_u *)"exclusive", 0);
8534 set_option_value((char_u *)"selectmode", 0L, (char_u *)"mouse,key", 0);
8535 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"popup", 0);
8536 set_option_value((char_u *)"keymodel", 0L,
8537 (char_u *)"startsel,stopsel", 0);
8538 }
8539 else if (STRCMP(eap->arg, "xterm") == 0)
8540 {
8541 set_option_value((char_u *)"selection", 0L, (char_u *)"inclusive", 0);
8542 set_option_value((char_u *)"selectmode", 0L, (char_u *)"", 0);
8543 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"extend", 0);
8544 set_option_value((char_u *)"keymodel", 0L, (char_u *)"", 0);
8545 }
8546 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008547 semsg(_(e_invarg2), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008548}
8549
Bram Moolenaar071d4272004-06-13 20:20:40 +00008550static int filetype_detect = FALSE;
8551static int filetype_plugin = FALSE;
8552static int filetype_indent = FALSE;
8553
8554/*
8555 * ":filetype [plugin] [indent] {on,off,detect}"
8556 * on: Load the filetype.vim file to install autocommands for file types.
8557 * off: Load the ftoff.vim file to remove all autocommands for file types.
8558 * plugin on: load filetype.vim and ftplugin.vim
8559 * plugin off: load ftplugof.vim
8560 * indent on: load filetype.vim and indent.vim
8561 * indent off: load indoff.vim
8562 */
8563 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008564ex_filetype(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008565{
8566 char_u *arg = eap->arg;
8567 int plugin = FALSE;
8568 int indent = FALSE;
8569
8570 if (*eap->arg == NUL)
8571 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008572 // Print current status.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008573 smsg("filetype detection:%s plugin:%s indent:%s",
Bram Moolenaar071d4272004-06-13 20:20:40 +00008574 filetype_detect ? "ON" : "OFF",
8575 filetype_plugin ? (filetype_detect ? "ON" : "(on)") : "OFF",
8576 filetype_indent ? (filetype_detect ? "ON" : "(on)") : "OFF");
8577 return;
8578 }
8579
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008580 // Accept "plugin" and "indent" in any order.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008581 for (;;)
8582 {
8583 if (STRNCMP(arg, "plugin", 6) == 0)
8584 {
8585 plugin = TRUE;
8586 arg = skipwhite(arg + 6);
8587 continue;
8588 }
8589 if (STRNCMP(arg, "indent", 6) == 0)
8590 {
8591 indent = TRUE;
8592 arg = skipwhite(arg + 6);
8593 continue;
8594 }
8595 break;
8596 }
8597 if (STRCMP(arg, "on") == 0 || STRCMP(arg, "detect") == 0)
8598 {
8599 if (*arg == 'o' || !filetype_detect)
8600 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01008601 source_runtime((char_u *)FILETYPE_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008602 filetype_detect = TRUE;
8603 if (plugin)
8604 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01008605 source_runtime((char_u *)FTPLUGIN_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008606 filetype_plugin = TRUE;
8607 }
8608 if (indent)
8609 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01008610 source_runtime((char_u *)INDENT_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008611 filetype_indent = TRUE;
8612 }
8613 }
8614 if (*arg == 'd')
8615 {
Bram Moolenaar1610d052016-06-09 22:53:01 +02008616 (void)do_doautocmd((char_u *)"filetypedetect BufRead", TRUE, NULL);
Bram Moolenaara3227e22006-03-08 21:32:40 +00008617 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008618 }
8619 }
8620 else if (STRCMP(arg, "off") == 0)
8621 {
8622 if (plugin || indent)
8623 {
8624 if (plugin)
8625 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01008626 source_runtime((char_u *)FTPLUGOF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008627 filetype_plugin = FALSE;
8628 }
8629 if (indent)
8630 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01008631 source_runtime((char_u *)INDOFF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008632 filetype_indent = FALSE;
8633 }
8634 }
8635 else
8636 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01008637 source_runtime((char_u *)FTOFF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008638 filetype_detect = FALSE;
8639 }
8640 }
8641 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008642 semsg(_(e_invarg2), arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008643}
8644
8645/*
Bram Moolenaar3e545692017-06-04 19:00:32 +02008646 * ":setfiletype [FALLBACK] {name}"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008647 */
8648 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008649ex_setfiletype(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008650{
8651 if (!did_filetype)
Bram Moolenaar3e545692017-06-04 19:00:32 +02008652 {
8653 char_u *arg = eap->arg;
8654
8655 if (STRNCMP(arg, "FALLBACK ", 9) == 0)
8656 arg += 9;
8657
8658 set_option_value((char_u *)"filetype", 0L, arg, OPT_LOCAL);
8659 if (arg != eap->arg)
8660 did_filetype = FALSE;
8661 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008662}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008663
Bram Moolenaar071d4272004-06-13 20:20:40 +00008664 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008665ex_digraphs(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008666{
8667#ifdef FEAT_DIGRAPHS
8668 if (*eap->arg != NUL)
8669 putdigraph(eap->arg);
8670 else
Bram Moolenaareae8ae12018-12-14 18:53:02 +01008671 listdigraphs(eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008672#else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008673 emsg(_("E196: No digraphs in this version"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008674#endif
8675}
8676
Bram Moolenaar451fc7b2018-04-27 22:53:07 +02008677#if defined(FEAT_SEARCH_EXTRA) || defined(PROTO)
8678 void
8679set_no_hlsearch(int flag)
8680{
8681 no_hlsearch = flag;
8682# ifdef FEAT_EVAL
8683 set_vim_var_nr(VV_HLSEARCH, !no_hlsearch && p_hls);
8684# endif
8685}
8686
Bram Moolenaar071d4272004-06-13 20:20:40 +00008687/*
8688 * ":nohlsearch"
8689 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008690 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008691ex_nohlsearch(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008692{
Bram Moolenaar451fc7b2018-04-27 22:53:07 +02008693 set_no_hlsearch(TRUE);
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00008694 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008695}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008696#endif
8697
8698#ifdef FEAT_CRYPT
8699/*
8700 * ":X": Get crypt key
8701 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008702 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008703ex_X(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008704{
Bram Moolenaar3a0c9082014-11-12 15:15:42 +01008705 crypt_check_current_method();
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02008706 (void)crypt_get_key(TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008707}
8708#endif
8709
8710#ifdef FEAT_FOLDING
8711 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008712ex_fold(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008713{
8714 if (foldManualAllowed(TRUE))
8715 foldCreate(eap->line1, eap->line2);
8716}
8717
8718 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008719ex_foldopen(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008720{
8721 opFoldRange(eap->line1, eap->line2, eap->cmdidx == CMD_foldopen,
8722 eap->forceit, FALSE);
8723}
8724
8725 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008726ex_folddo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008727{
8728 linenr_T lnum;
8729
Bram Moolenaar4facea32019-10-12 20:17:40 +02008730# ifdef FEAT_CLIPBOARD
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02008731 start_global_changes();
Bram Moolenaar4facea32019-10-12 20:17:40 +02008732# endif
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02008733
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008734 // First set the marks for all lines closed/open.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008735 for (lnum = eap->line1; lnum <= eap->line2; ++lnum)
8736 if (hasFolding(lnum, NULL, NULL) == (eap->cmdidx == CMD_folddoclosed))
8737 ml_setmarked(lnum);
8738
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008739 // Execute the command on the marked lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008740 global_exe(eap->arg);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008741 ml_clearmarked(); // clear rest of the marks
Bram Moolenaar4facea32019-10-12 20:17:40 +02008742# ifdef FEAT_CLIPBOARD
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02008743 end_global_changes();
Bram Moolenaar4facea32019-10-12 20:17:40 +02008744# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008745}
8746#endif
Bram Moolenaarf5291f32017-09-14 22:55:37 +02008747
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008748#if defined(FEAT_QUICKFIX) || defined(PROTO)
Bram Moolenaar39665952018-08-15 20:59:48 +02008749/*
8750 * Returns TRUE if the supplied Ex cmdidx is for a location list command
8751 * instead of a quickfix command.
8752 */
8753 int
8754is_loclist_cmd(int cmdidx)
8755{
Bram Moolenaar74c8be22018-08-23 22:51:40 +02008756 if (cmdidx < 0 || cmdidx >= CMD_SIZE)
Bram Moolenaar39665952018-08-15 20:59:48 +02008757 return FALSE;
8758 return cmdnames[cmdidx].cmd_name[0] == 'l';
8759}
8760#endif
8761
Bram Moolenaar4facea32019-10-12 20:17:40 +02008762#if defined(FEAT_TIMERS) || defined(PROTO)
Bram Moolenaarf5291f32017-09-14 22:55:37 +02008763 int
8764get_pressedreturn(void)
8765{
8766 return ex_pressedreturn;
8767}
8768
8769 void
8770set_pressedreturn(int val)
8771{
8772 ex_pressedreturn = val;
8773}
8774#endif