blob: 93f33ae429f5fb95a863c80293b81f48a9d86093 [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 Moolenaar8a7d6542020-01-26 15:56:19 +01001732 p = find_ex_command(&ea, NULL, lookup_scriptvar, NULL);
1733 else
1734#endif
1735 p = find_ex_command(&ea, NULL, NULL, NULL);
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001736
Bram Moolenaar7feb35e2018-08-21 17:49:54 +02001737#ifdef FEAT_EVAL
1738# ifdef FEAT_PROFILE
1739 // Count this line for profiling if skip is TRUE.
1740 if (do_profiling == PROF_YES
1741 && (!ea.skip || cstack->cs_idx == 0 || (cstack->cs_idx > 0
1742 && (cstack->cs_flags[cstack->cs_idx - 1] & CSF_ACTIVE))))
1743 {
1744 int skip = did_emsg || got_int || did_throw;
1745
1746 if (ea.cmdidx == CMD_catch)
1747 skip = !skip && !(cstack->cs_idx >= 0
1748 && (cstack->cs_flags[cstack->cs_idx] & CSF_THROWN)
1749 && !(cstack->cs_flags[cstack->cs_idx] & CSF_CAUGHT));
1750 else if (ea.cmdidx == CMD_else || ea.cmdidx == CMD_elseif)
1751 skip = skip || !(cstack->cs_idx >= 0
1752 && !(cstack->cs_flags[cstack->cs_idx]
1753 & (CSF_ACTIVE | CSF_TRUE)));
1754 else if (ea.cmdidx == CMD_finally)
1755 skip = FALSE;
1756 else if (ea.cmdidx != CMD_endif
1757 && ea.cmdidx != CMD_endfor
1758 && ea.cmdidx != CMD_endtry
1759 && ea.cmdidx != CMD_endwhile)
1760 skip = ea.skip;
1761
1762 if (!skip)
1763 {
1764 if (getline_equal(fgetline, cookie, get_func_line))
1765 func_line_exec(getline_cookie(fgetline, cookie));
1766 else if (getline_equal(fgetline, cookie, getsourceline))
1767 script_line_exec();
1768 }
1769 }
1770# endif
1771
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001772 // May go to debug mode. If this happens and the ">quit" debug command is
1773 // used, throw an interrupt exception and skip the next command.
Bram Moolenaar7feb35e2018-08-21 17:49:54 +02001774 dbg_check_breakpoint(&ea);
1775 if (!ea.skip && got_int)
1776 {
1777 ea.skip = TRUE;
1778 (void)do_intthrow(cstack);
1779 }
1780#endif
1781
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001782/*
1783 * 4. parse a range specifier of the form: addr [,addr] [;addr] ..
Bram Moolenaar071d4272004-06-13 20:20:40 +00001784 *
1785 * where 'addr' is:
1786 *
1787 * % (entire file)
1788 * $ [+-NUM]
1789 * 'x [+-NUM] (where x denotes a currently defined mark)
1790 * . [+-NUM]
1791 * [+-NUM]..
1792 * NUM
1793 *
1794 * The ea.cmd pointer is updated to point to the first character following the
1795 * range spec. If an initial address is found, but no second, the upper bound
1796 * is equal to the lower.
1797 */
1798
Bram Moolenaar25190db2019-05-04 15:05:28 +02001799 // ea.addr_type for user commands is set by find_ucmd
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01001800 if (!IS_USER_CMDIDX(ea.cmdidx))
1801 {
1802 if (ea.cmdidx != CMD_SIZE)
1803 ea.addr_type = cmdnames[(int)ea.cmdidx].cmd_addr_type;
1804 else
1805 ea.addr_type = ADDR_LINES;
1806
Bram Moolenaar25190db2019-05-04 15:05:28 +02001807 // :wincmd range depends on the argument.
Bram Moolenaar164f3262015-01-14 21:22:01 +01001808 if (ea.cmdidx == CMD_wincmd && p != NULL)
1809 get_wincmd_addr_type(skipwhite(p), &ea);
Bram Moolenaar25190db2019-05-04 15:05:28 +02001810#ifdef FEAT_QUICKFIX
1811 // :.cc in quickfix window uses line number
1812 if ((ea.cmdidx == CMD_cc || ea.cmdidx == CMD_ll) && bt_quickfix(curbuf))
1813 ea.addr_type = ADDR_OTHER;
1814#endif
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01001815 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001816
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01001817 ea.cmd = cmd;
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02001818 if (parse_cmd_address(&ea, &errormsg, FALSE) == FAIL)
Bram Moolenaaree8415b2018-08-10 23:13:12 +02001819 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001820
Bram Moolenaar071d4272004-06-13 20:20:40 +00001821/*
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001822 * 5. Parse the command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001823 */
1824
1825 /*
1826 * Skip ':' and any white space
1827 */
1828 ea.cmd = skipwhite(ea.cmd);
1829 while (*ea.cmd == ':')
1830 ea.cmd = skipwhite(ea.cmd + 1);
1831
1832 /*
1833 * If we got a line, but no command, then go to the line.
1834 * If we find a '|' or '\n' we set ea.nextcmd.
1835 */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001836 if (*ea.cmd == NUL || *ea.cmd == '"'
Bram Moolenaar7a092242020-04-16 22:10:49 +02001837#ifdef FEAT_EVAL
Bram Moolenaara26b9702020-04-18 19:53:28 +02001838 || (*ea.cmd == '#' && ea.cmd[1] != '{'
1839 && !starts_with_colon && in_vim9script())
Bram Moolenaar7a092242020-04-16 22:10:49 +02001840#endif
1841 || (ea.nextcmd = check_nextcmd(ea.cmd)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001842 {
1843 /*
1844 * strange vi behaviour:
1845 * ":3" jumps to line 3
1846 * ":3|..." prints line 3
1847 * ":|" prints current line
1848 */
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001849 if (ea.skip) // skip this if inside :if
Bram Moolenaar071d4272004-06-13 20:20:40 +00001850 goto doend;
Bram Moolenaardf177f62005-02-22 08:39:57 +00001851 if (*ea.cmd == '|' || (exmode_active && ea.line1 != ea.line2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001852 {
1853 ea.cmdidx = CMD_print;
Bram Moolenaar8071cb22019-07-12 17:58:01 +02001854 ea.argt = EX_RANGE+EX_COUNT+EX_TRLBAR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001855 if ((errormsg = invalid_range(&ea)) == NULL)
1856 {
1857 correct_range(&ea);
1858 ex_print(&ea);
1859 }
1860 }
1861 else if (ea.addr_count != 0)
1862 {
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00001863 if (ea.line2 > curbuf->b_ml.ml_line_count)
1864 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001865 // With '-' in 'cpoptions' a line number past the file is an
1866 // error, otherwise put it at the end of the file.
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00001867 if (vim_strchr(p_cpo, CPO_MINUS) != NULL)
1868 ea.line2 = -1;
1869 else
1870 ea.line2 = curbuf->b_ml.ml_line_count;
1871 }
1872
1873 if (ea.line2 < 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001874 errormsg = _(e_invrange);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001875 else
1876 {
1877 if (ea.line2 == 0)
1878 curwin->w_cursor.lnum = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001879 else
1880 curwin->w_cursor.lnum = ea.line2;
1881 beginline(BL_SOL | BL_FIX);
1882 }
1883 }
1884 goto doend;
1885 }
1886
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001887 // If this looks like an undefined user command and there are CmdUndefined
1888 // autocommands defined, trigger the matching autocommands.
Bram Moolenaard5005162014-08-22 23:05:54 +02001889 if (p != NULL && ea.cmdidx == CMD_SIZE && !ea.skip
1890 && ASCII_ISUPPER(*ea.cmd)
1891 && has_cmdundefined())
1892 {
Bram Moolenaard5005162014-08-22 23:05:54 +02001893 int ret;
1894
Bram Moolenaar5a31b462014-08-23 14:16:20 +02001895 p = ea.cmd;
Bram Moolenaard5005162014-08-22 23:05:54 +02001896 while (ASCII_ISALNUM(*p))
1897 ++p;
Bram Moolenaar158ea172020-06-18 17:28:39 +02001898 p = vim_strnsave(ea.cmd, p - ea.cmd);
Bram Moolenaard5005162014-08-22 23:05:54 +02001899 ret = apply_autocmds(EVENT_CMDUNDEFINED, p, p, TRUE, NULL);
1900 vim_free(p);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001901 // If the autocommands did something and didn't cause an error, try
1902 // finding the command again.
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001903 p = (ret
1904#ifdef FEAT_EVAL
1905 && !aborting()
Bram Moolenaard5005162014-08-22 23:05:54 +02001906#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001907 ) ? find_ex_command(&ea, NULL, NULL, NULL) : ea.cmd;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001908 }
Bram Moolenaard5005162014-08-22 23:05:54 +02001909
Bram Moolenaar071d4272004-06-13 20:20:40 +00001910 if (p == NULL)
1911 {
1912 if (!ea.skip)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001913 errormsg = _("E464: Ambiguous use of user-defined command");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001914 goto doend;
1915 }
Bram Moolenaarac9fb182019-04-27 13:04:13 +02001916 // Check for wrong commands.
Bram Moolenaar6f9a4762017-06-22 20:39:17 +02001917 if (*p == '!' && ea.cmd[1] == 0151 && ea.cmd[0] == 78
1918 && !IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001919 {
1920 errormsg = uc_fun_cmd();
1921 goto doend;
1922 }
Bram Moolenaarac9fb182019-04-27 13:04:13 +02001923
Bram Moolenaar071d4272004-06-13 20:20:40 +00001924 if (ea.cmdidx == CMD_SIZE)
1925 {
1926 if (!ea.skip)
1927 {
1928 STRCPY(IObuff, _("E492: Not an editor command"));
1929 if (!sourcing)
Bram Moolenaar7b668e82016-08-23 23:51:21 +02001930 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001931 // If the modifier was parsed OK the error must be in the
1932 // following command
Bram Moolenaar7b668e82016-08-23 23:51:21 +02001933 if (after_modifier != NULL)
1934 append_command(after_modifier);
1935 else
1936 append_command(*cmdlinep);
1937 }
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001938 errormsg = (char *)IObuff;
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001939 did_emsg_syntax = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001940 }
1941 goto doend;
1942 }
1943
Bram Moolenaar958636c2014-10-21 20:01:58 +02001944 ni = (!IS_USER_CMDIDX(ea.cmdidx)
1945 && (cmdnames[ea.cmdidx].cmd_func == ex_ni
Bram Moolenaar7bb75552007-07-16 18:39:49 +00001946#ifdef HAVE_EX_SCRIPT_NI
1947 || cmdnames[ea.cmdidx].cmd_func == ex_script_ni
1948#endif
1949 ));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001950
1951#ifndef FEAT_EVAL
1952 /*
1953 * When the expression evaluation is disabled, recognize the ":if" and
1954 * ":endif" commands and ignore everything in between it.
1955 */
1956 if (ea.cmdidx == CMD_if)
1957 ++if_level;
1958 if (if_level)
1959 {
1960 if (ea.cmdidx == CMD_endif)
1961 --if_level;
1962 goto doend;
1963 }
1964
1965#endif
1966
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001967 // forced commands
Bram Moolenaar196b3b02008-06-20 16:51:41 +00001968 if (*p == '!' && ea.cmdidx != CMD_substitute
1969 && ea.cmdidx != CMD_smagic && ea.cmdidx != CMD_snomagic)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001970 {
1971 ++p;
1972 ea.forceit = TRUE;
1973 }
1974 else
1975 ea.forceit = FALSE;
1976
1977/*
Bram Moolenaarb7316892019-05-01 18:08:42 +02001978 * 6. Parse arguments. Then check for errors.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001979 */
Bram Moolenaar958636c2014-10-21 20:01:58 +02001980 if (!IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001981 ea.argt = (long)cmdnames[(int)ea.cmdidx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001982
1983 if (!ea.skip)
1984 {
1985#ifdef HAVE_SANDBOX
Bram Moolenaar8071cb22019-07-12 17:58:01 +02001986 if (sandbox != 0 && !(ea.argt & EX_SBOXOK))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001987 {
Bram Moolenaar8c62a082019-02-08 14:34:10 +01001988 // Command not allowed in sandbox.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001989 errormsg = _(e_sandbox);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001990 goto doend;
1991 }
1992#endif
Bram Moolenaar8071cb22019-07-12 17:58:01 +02001993 if (restricted != 0 && (ea.argt & EX_RESTRICT))
Bram Moolenaar8c62a082019-02-08 14:34:10 +01001994 {
1995 errormsg = _("E981: Command not allowed in rvim");
1996 goto doend;
1997 }
Bram Moolenaar8071cb22019-07-12 17:58:01 +02001998 if (!curbuf->b_p_ma && (ea.argt & EX_MODIFY))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001999 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002000 // Command not allowed in non-'modifiable' buffer
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002001 errormsg = _(e_modifiable);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002002 goto doend;
2003 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002004
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002005 if (text_locked() && !(ea.argt & EX_CMDWIN)
Bram Moolenaar958636c2014-10-21 20:01:58 +02002006 && !IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002007 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002008 // Command not allowed when editing the command line.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002009 errormsg = _(get_text_locked_msg());
Bram Moolenaar071d4272004-06-13 20:20:40 +00002010 goto doend;
2011 }
Bram Moolenaar379fb762018-08-30 15:58:28 +02002012
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002013 // Disallow editing another buffer when "curbuf_lock" is set.
2014 // Do allow ":checktime" (it is postponed).
2015 // Do allow ":edit" (check for an argument later).
2016 // Do allow ":file" with no arguments (check for an argument later).
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002017 if (!(ea.argt & EX_CMDWIN)
Bram Moolenaar5555acc2006-04-07 21:33:12 +00002018 && ea.cmdidx != CMD_checktime
Bram Moolenaar379fb762018-08-30 15:58:28 +02002019 && ea.cmdidx != CMD_edit
2020 && ea.cmdidx != CMD_file
Bram Moolenaar958636c2014-10-21 20:01:58 +02002021 && !IS_USER_CMDIDX(ea.cmdidx)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002022 && curbuf_locked())
2023 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002024
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002025 if (!ni && !(ea.argt & EX_RANGE) && ea.addr_count > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002026 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002027 // no range allowed
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002028 errormsg = _(e_norange);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002029 goto doend;
2030 }
2031 }
2032
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002033 if (!ni && !(ea.argt & EX_BANG) && ea.forceit) // no <!> allowed
Bram Moolenaar071d4272004-06-13 20:20:40 +00002034 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002035 errormsg = _(e_nobang);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002036 goto doend;
2037 }
2038
2039 /*
2040 * Don't complain about the range if it is not used
2041 * (could happen if line_count is accidentally set to 0).
2042 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002043 if (!ea.skip && !ni && (ea.argt & EX_RANGE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002044 {
2045 /*
2046 * If the range is backwards, ask for confirmation and, if given, swap
2047 * ea.line1 & ea.line2 so it's forwards again.
2048 * When global command is busy, don't ask, will fail below.
2049 */
2050 if (!global_busy && ea.line1 > ea.line2)
2051 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002052 if (msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002053 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002054 if (sourcing || exmode_active)
2055 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002056 errormsg = _("E493: Backwards range given");
Bram Moolenaara5792f52005-11-23 21:25:05 +00002057 goto doend;
2058 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002059 if (ask_yesno((char_u *)
2060 _("Backwards range given, OK to swap"), FALSE) != 'y')
Bram Moolenaara5792f52005-11-23 21:25:05 +00002061 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002062 }
2063 lnum = ea.line1;
2064 ea.line1 = ea.line2;
2065 ea.line2 = lnum;
2066 }
2067 if ((errormsg = invalid_range(&ea)) != NULL)
2068 goto doend;
2069 }
2070
Bram Moolenaarb7316892019-05-01 18:08:42 +02002071 if ((ea.addr_type == ADDR_OTHER) && ea.addr_count == 0)
2072 // default is 1, not cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00002073 ea.line2 = 1;
2074
2075 correct_range(&ea);
2076
2077#ifdef FEAT_FOLDING
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002078 if (((ea.argt & EX_WHOLEFOLD) || ea.addr_count >= 2) && !global_busy
Bram Moolenaara3306952016-01-02 21:41:06 +01002079 && ea.addr_type == ADDR_LINES)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002080 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002081 // Put the first line at the start of a closed fold, put the last line
2082 // at the end of a closed fold.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002083 (void)hasFolding(ea.line1, &ea.line1, NULL);
2084 (void)hasFolding(ea.line2, NULL, &ea.line2);
2085 }
2086#endif
2087
2088#ifdef FEAT_QUICKFIX
2089 /*
Bram Moolenaar86b68352004-12-27 21:59:20 +00002090 * For the ":make" and ":grep" commands we insert the 'makeprg'/'grepprg'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002091 * option here, so things like % get expanded.
2092 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002093 p = replace_makeprg(&ea, p, cmdlinep);
2094 if (p == NULL)
2095 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002096#endif
2097
2098 /*
2099 * Skip to start of argument.
2100 * Don't do this for the ":!" command, because ":!! -l" needs the space.
2101 */
2102 if (ea.cmdidx == CMD_bang)
2103 ea.arg = p;
2104 else
2105 ea.arg = skipwhite(p);
2106
Bram Moolenaar379fb762018-08-30 15:58:28 +02002107 // ":file" cannot be run with an argument when "curbuf_lock" is set
2108 if (ea.cmdidx == CMD_file && *ea.arg != NUL && curbuf_locked())
2109 goto doend;
2110
Bram Moolenaar071d4272004-06-13 20:20:40 +00002111 /*
2112 * Check for "++opt=val" argument.
2113 * Must be first, allow ":w ++enc=utf8 !cmd"
2114 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002115 if (ea.argt & EX_ARGOPT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002116 while (ea.arg[0] == '+' && ea.arg[1] == '+')
2117 if (getargopt(&ea) == FAIL && !ni)
2118 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002119 errormsg = _(e_invarg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002120 goto doend;
2121 }
2122
2123 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
2124 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002125 if (*ea.arg == '>') // append
Bram Moolenaar071d4272004-06-13 20:20:40 +00002126 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002127 if (*++ea.arg != '>') // typed wrong
Bram Moolenaar071d4272004-06-13 20:20:40 +00002128 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002129 errormsg = _("E494: Use w or w>>");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002130 goto doend;
2131 }
2132 ea.arg = skipwhite(ea.arg + 1);
2133 ea.append = TRUE;
2134 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002135 else if (*ea.arg == '!' && ea.cmdidx == CMD_write) // :w !filter
Bram Moolenaar071d4272004-06-13 20:20:40 +00002136 {
2137 ++ea.arg;
2138 ea.usefilter = TRUE;
2139 }
2140 }
2141
2142 if (ea.cmdidx == CMD_read)
2143 {
2144 if (ea.forceit)
2145 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002146 ea.usefilter = TRUE; // :r! filter if ea.forceit
Bram Moolenaar071d4272004-06-13 20:20:40 +00002147 ea.forceit = FALSE;
2148 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002149 else if (*ea.arg == '!') // :r !filter
Bram Moolenaar071d4272004-06-13 20:20:40 +00002150 {
2151 ++ea.arg;
2152 ea.usefilter = TRUE;
2153 }
2154 }
2155
2156 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
2157 {
2158 ea.amount = 1;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002159 while (*ea.arg == *ea.cmd) // count number of '>' or '<'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002160 {
2161 ++ea.arg;
2162 ++ea.amount;
2163 }
2164 ea.arg = skipwhite(ea.arg);
2165 }
2166
2167 /*
2168 * Check for "+command" argument, before checking for next command.
2169 * Don't do this for ":read !cmd" and ":write !cmd".
2170 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002171 if ((ea.argt & EX_CMDARG) && !ea.usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002172 ea.do_ecmd_cmd = getargcmd(&ea.arg);
2173
2174 /*
2175 * Check for '|' to separate commands and '"' to start comments.
2176 * Don't do this for ":read !cmd" and ":write !cmd".
2177 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002178 if ((ea.argt & EX_TRLBAR) && !ea.usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002179 separate_nextcmd(&ea);
2180
2181 /*
2182 * Check for <newline> to end a shell command.
Bram Moolenaardf177f62005-02-22 08:39:57 +00002183 * Also do this for ":read !cmd", ":write !cmd" and ":global".
2184 * Any others?
Bram Moolenaar071d4272004-06-13 20:20:40 +00002185 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00002186 else if (ea.cmdidx == CMD_bang
Bram Moolenaar67883b42017-07-27 22:57:00 +02002187 || ea.cmdidx == CMD_terminal
Bram Moolenaardf177f62005-02-22 08:39:57 +00002188 || ea.cmdidx == CMD_global
2189 || ea.cmdidx == CMD_vglobal
2190 || ea.usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002191 {
2192 for (p = ea.arg; *p; ++p)
2193 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002194 // Remove one backslash before a newline, so that it's possible to
2195 // pass a newline to the shell and also a newline that is preceded
2196 // with a backslash. This makes it impossible to end a shell
2197 // command in a backslash, but that doesn't appear useful.
2198 // Halving the number of backslashes is incompatible with previous
2199 // versions.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002200 if (*p == '\\' && p[1] == '\n')
Bram Moolenaara7241f52008-06-24 20:39:31 +00002201 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002202 else if (*p == '\n')
2203 {
2204 ea.nextcmd = p + 1;
2205 *p = NUL;
2206 break;
2207 }
2208 }
2209 }
2210
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002211 if ((ea.argt & EX_DFLALL) && ea.addr_count == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002212 {
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002213 buf_T *buf;
2214
Bram Moolenaar071d4272004-06-13 20:20:40 +00002215 ea.line1 = 1;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002216 switch (ea.addr_type)
2217 {
2218 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02002219 case ADDR_OTHER:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002220 ea.line2 = curbuf->b_ml.ml_line_count;
2221 break;
2222 case ADDR_LOADED_BUFFERS:
2223 buf = firstbuf;
2224 while (buf->b_next != NULL && buf->b_ml.ml_mfp == NULL)
2225 buf = buf->b_next;
2226 ea.line1 = buf->b_fnum;
2227 buf = lastbuf;
2228 while (buf->b_prev != NULL && buf->b_ml.ml_mfp == NULL)
2229 buf = buf->b_prev;
2230 ea.line2 = buf->b_fnum;
2231 break;
2232 case ADDR_BUFFERS:
2233 ea.line1 = firstbuf->b_fnum;
2234 ea.line2 = lastbuf->b_fnum;
2235 break;
2236 case ADDR_WINDOWS:
2237 ea.line2 = LAST_WIN_NR;
2238 break;
2239 case ADDR_TABS:
2240 ea.line2 = LAST_TAB_NR;
2241 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01002242 case ADDR_TABS_RELATIVE:
2243 ea.line2 = 1;
2244 break;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002245 case ADDR_ARGUMENTS:
2246 if (ARGCOUNT == 0)
2247 ea.line1 = ea.line2 = 0;
2248 else
2249 ea.line2 = ARGCOUNT;
2250 break;
Bram Moolenaar25190db2019-05-04 15:05:28 +02002251 case ADDR_QUICKFIX_VALID:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02002252#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02002253 ea.line2 = qf_get_valid_size(&ea);
Bram Moolenaaraa23b372015-09-08 18:46:31 +02002254 if (ea.line2 == 0)
2255 ea.line2 = 1;
Bram Moolenaare906c502015-09-09 21:10:39 +02002256#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02002257 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02002258 case ADDR_NONE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02002259 case ADDR_UNSIGNED:
2260 case ADDR_QUICKFIX:
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002261 iemsg(_("INTERNAL: Cannot use EX_DFLALL with ADDR_NONE, ADDR_UNSIGNED or ADDR_QUICKFIX"));
Bram Moolenaarb7316892019-05-01 18:08:42 +02002262 break;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002263 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002264 }
2265
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002266 // accept numbered register only when no count allowed (:put)
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002267 if ( (ea.argt & EX_REGSTR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002268 && *ea.arg != NUL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002269 // Do not allow register = for user commands
Bram Moolenaar958636c2014-10-21 20:01:58 +02002270 && (!IS_USER_CMDIDX(ea.cmdidx) || *ea.arg != '=')
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002271 && !((ea.argt & EX_COUNT) && VIM_ISDIGIT(*ea.arg)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002272 {
Bram Moolenaar85de2062011-05-05 14:26:41 +02002273#ifndef FEAT_CLIPBOARD
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002274 // check these explicitly for a more specific error message
Bram Moolenaar85de2062011-05-05 14:26:41 +02002275 if (*ea.arg == '*' || *ea.arg == '+')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002276 {
Bram Moolenaar99b12722019-01-14 20:16:40 +01002277 errormsg = _(e_invalidreg);
Bram Moolenaar85de2062011-05-05 14:26:41 +02002278 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002279 }
2280#endif
Bram Moolenaar958636c2014-10-21 20:01:58 +02002281 if (valid_yank_reg(*ea.arg, (ea.cmdidx != CMD_put
2282 && !IS_USER_CMDIDX(ea.cmdidx))))
Bram Moolenaar85de2062011-05-05 14:26:41 +02002283 {
2284 ea.regname = *ea.arg++;
2285#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002286 // for '=' register: accept the rest of the line as an expression
Bram Moolenaar85de2062011-05-05 14:26:41 +02002287 if (ea.arg[-1] == '=' && ea.arg[0] != NUL)
2288 {
2289 set_expr_line(vim_strsave(ea.arg));
2290 ea.arg += STRLEN(ea.arg);
2291 }
2292#endif
2293 ea.arg = skipwhite(ea.arg);
2294 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002295 }
2296
2297 /*
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002298 * Check for a count. When accepting a EX_BUFNAME, don't use "123foo" as a
Bram Moolenaar071d4272004-06-13 20:20:40 +00002299 * count, it's a buffer name.
2300 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002301 if ((ea.argt & EX_COUNT) && VIM_ISDIGIT(*ea.arg)
2302 && (!(ea.argt & EX_BUFNAME) || *(p = skipdigits(ea.arg)) == NUL
Bram Moolenaar1c465442017-03-12 20:10:05 +01002303 || VIM_ISWHITE(*p)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002304 {
2305 n = getdigits(&ea.arg);
2306 ea.arg = skipwhite(ea.arg);
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002307 if (n <= 0 && !ni && (ea.argt & EX_ZEROR) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002308 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002309 errormsg = _(e_zerocount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002310 goto doend;
2311 }
Bram Moolenaarb7316892019-05-01 18:08:42 +02002312 if (ea.addr_type != ADDR_LINES) // e.g. :buffer 2, :sleep 3
Bram Moolenaar071d4272004-06-13 20:20:40 +00002313 {
2314 ea.line2 = n;
2315 if (ea.addr_count == 0)
2316 ea.addr_count = 1;
2317 }
2318 else
2319 {
2320 ea.line1 = ea.line2;
2321 ea.line2 += n - 1;
2322 ++ea.addr_count;
2323 /*
2324 * Be vi compatible: no error message for out of range.
2325 */
Bram Moolenaarb7316892019-05-01 18:08:42 +02002326 if (ea.line2 > curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002327 ea.line2 = curbuf->b_ml.ml_line_count;
2328 }
2329 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00002330
2331 /*
2332 * Check for flags: 'l', 'p' and '#'.
2333 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002334 if (ea.argt & EX_FLAGS)
Bram Moolenaardf177f62005-02-22 08:39:57 +00002335 get_flags(&ea);
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002336 if (!ni && !(ea.argt & EX_EXTRA) && *ea.arg != NUL
2337 && *ea.arg != '"' && (*ea.arg != '|' || (ea.argt & EX_TRLBAR) == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002338 {
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002339 // no arguments allowed but there is something
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002340 errormsg = _(e_trailing);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002341 goto doend;
2342 }
2343
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002344 if (!ni && (ea.argt & EX_NEEDARG) && *ea.arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002345 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002346 errormsg = _(e_argreq);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002347 goto doend;
2348 }
2349
2350#ifdef FEAT_EVAL
2351 /*
2352 * Skip the command when it's not going to be executed.
2353 * The commands like :if, :endif, etc. always need to be executed.
2354 * Also make an exception for commands that handle a trailing command
2355 * themselves.
2356 */
2357 if (ea.skip)
2358 {
2359 switch (ea.cmdidx)
2360 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002361 // commands that need evaluation
Bram Moolenaar071d4272004-06-13 20:20:40 +00002362 case CMD_while:
2363 case CMD_endwhile:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00002364 case CMD_for:
2365 case CMD_endfor:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002366 case CMD_if:
2367 case CMD_elseif:
2368 case CMD_else:
2369 case CMD_endif:
2370 case CMD_try:
2371 case CMD_catch:
2372 case CMD_finally:
2373 case CMD_endtry:
2374 case CMD_function:
Bram Moolenaarab55c682020-03-01 19:41:43 +01002375 case CMD_def:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002376 break;
2377
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002378 // Commands that handle '|' themselves. Check: A command should
2379 // either have the EX_TRLBAR flag, appear in this list or appear in
2380 // the list at ":help :bar".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002381 case CMD_aboveleft:
2382 case CMD_and:
2383 case CMD_belowright:
2384 case CMD_botright:
2385 case CMD_browse:
2386 case CMD_call:
2387 case CMD_confirm:
Bram Moolenaar8f76e6b2019-11-26 16:50:30 +01002388 case CMD_const:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002389 case CMD_delfunction:
2390 case CMD_djump:
2391 case CMD_dlist:
2392 case CMD_dsearch:
2393 case CMD_dsplit:
2394 case CMD_echo:
2395 case CMD_echoerr:
2396 case CMD_echomsg:
2397 case CMD_echon:
Bram Moolenaara76b3152020-02-16 16:20:21 +01002398 case CMD_eval:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002399 case CMD_execute:
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002400 case CMD_filter:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002401 case CMD_help:
2402 case CMD_hide:
2403 case CMD_ijump:
2404 case CMD_ilist:
2405 case CMD_isearch:
2406 case CMD_isplit:
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002407 case CMD_keepalt:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002408 case CMD_keepjumps:
2409 case CMD_keepmarks:
Bram Moolenaara939e432013-11-09 05:30:26 +01002410 case CMD_keeppatterns:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002411 case CMD_leftabove:
2412 case CMD_let:
2413 case CMD_lockmarks:
Bram Moolenaarcc4423a2019-11-26 17:05:00 +01002414 case CMD_lockvar:
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002415 case CMD_lua:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002416 case CMD_match:
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002417 case CMD_mzscheme:
Bram Moolenaar5803ae62014-03-23 16:04:02 +01002418 case CMD_noautocmd:
2419 case CMD_noswapfile:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002420 case CMD_perl:
2421 case CMD_psearch:
2422 case CMD_python:
Bram Moolenaar368373e2010-07-19 20:46:22 +02002423 case CMD_py3:
Bram Moolenaarb6590522010-07-21 16:00:43 +02002424 case CMD_python3:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002425 case CMD_return:
2426 case CMD_rightbelow:
2427 case CMD_ruby:
2428 case CMD_silent:
2429 case CMD_smagic:
2430 case CMD_snomagic:
2431 case CMD_substitute:
2432 case CMD_syntax:
Bram Moolenaara226a6d2006-02-26 23:59:20 +00002433 case CMD_tab:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002434 case CMD_tcl:
2435 case CMD_throw:
2436 case CMD_tilde:
2437 case CMD_topleft:
2438 case CMD_unlet:
Bram Moolenaarcc4423a2019-11-26 17:05:00 +01002439 case CMD_unlockvar:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002440 case CMD_verbose:
2441 case CMD_vertical:
Bram Moolenaar12bde492011-06-13 01:19:56 +02002442 case CMD_wincmd:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002443 break;
2444
2445 default: goto doend;
2446 }
2447 }
2448#endif
2449
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002450 if (ea.argt & EX_XFILE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002451 {
2452 if (expand_filename(&ea, cmdlinep, &errormsg) == FAIL)
2453 goto doend;
2454 }
2455
Bram Moolenaar071d4272004-06-13 20:20:40 +00002456 /*
2457 * Accept buffer name. Cannot be used at the same time with a buffer
2458 * number. Don't do this for a user command.
2459 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002460 if ((ea.argt & EX_BUFNAME) && *ea.arg != NUL && ea.addr_count == 0
Bram Moolenaar958636c2014-10-21 20:01:58 +02002461 && !IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002462 {
2463 /*
2464 * :bdelete, :bwipeout and :bunload take several arguments, separated
2465 * by spaces: find next space (skipping over escaped characters).
2466 * The others take one argument: ignore trailing spaces.
2467 */
2468 if (ea.cmdidx == CMD_bdelete || ea.cmdidx == CMD_bwipeout
2469 || ea.cmdidx == CMD_bunload)
2470 p = skiptowhite_esc(ea.arg);
2471 else
2472 {
2473 p = ea.arg + STRLEN(ea.arg);
Bram Moolenaar1c465442017-03-12 20:10:05 +01002474 while (p > ea.arg && VIM_ISWHITE(p[-1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002475 --p;
2476 }
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002477 ea.line2 = buflist_findpat(ea.arg, p, (ea.argt & EX_BUFUNL) != 0,
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002478 FALSE, FALSE);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002479 if (ea.line2 < 0) // failed
Bram Moolenaar071d4272004-06-13 20:20:40 +00002480 goto doend;
2481 ea.addr_count = 1;
2482 ea.arg = skipwhite(p);
2483 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002484
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002485 // The :try command saves the emsg_silent flag, reset it here when
2486 // ":silent! try" was used, it should only apply to :try itself.
Bram Moolenaareffed932018-08-14 13:38:17 +02002487 if (ea.cmdidx == CMD_try && ea.did_esilent > 0)
Bram Moolenaar2be57332018-02-13 18:05:18 +01002488 {
Bram Moolenaareffed932018-08-14 13:38:17 +02002489 emsg_silent -= ea.did_esilent;
Bram Moolenaar2be57332018-02-13 18:05:18 +01002490 if (emsg_silent < 0)
2491 emsg_silent = 0;
Bram Moolenaareffed932018-08-14 13:38:17 +02002492 ea.did_esilent = 0;
Bram Moolenaar2be57332018-02-13 18:05:18 +01002493 }
2494
Bram Moolenaar071d4272004-06-13 20:20:40 +00002495/*
Bram Moolenaar2be57332018-02-13 18:05:18 +01002496 * 7. Execute the command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002497 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002498
Bram Moolenaar958636c2014-10-21 20:01:58 +02002499 if (IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002500 {
2501 /*
2502 * Execute a user-defined command.
2503 */
2504 do_ucmd(&ea);
2505 }
2506 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002507 {
2508 /*
Bram Moolenaarac9fb182019-04-27 13:04:13 +02002509 * Call the function to execute the builtin command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002510 */
2511 ea.errmsg = NULL;
2512 (cmdnames[ea.cmdidx].cmd_func)(&ea);
2513 if (ea.errmsg != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002514 errormsg = _(ea.errmsg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002515 }
2516
2517#ifdef FEAT_EVAL
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002518 // Set flag that any command was executed, used by ex_vim9script().
Bram Moolenaar16531552020-02-08 16:00:46 +01002519 if (getline_equal(ea.getline, ea.cookie, getsourceline)
2520 && current_sctx.sc_sid > 0)
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002521 SCRIPT_ITEM(current_sctx.sc_sid)->sn_had_command = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002522
Bram Moolenaar071d4272004-06-13 20:20:40 +00002523 /*
2524 * If the command just executed called do_cmdline(), any throw or ":return"
2525 * or ":finish" encountered there must also check the cstack of the still
2526 * active do_cmdline() that called this do_one_cmd(). Rethrow an uncaught
2527 * exception, or reanimate a returned function or finished script file and
2528 * return or finish it again.
2529 */
2530 if (need_rethrow)
2531 do_throw(cstack);
2532 else if (check_cstack)
2533 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002534 if (source_finished(fgetline, cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002535 do_finish(&ea, TRUE);
Bram Moolenaar89d40322006-08-29 15:30:07 +00002536 else if (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002537 && current_func_returned())
2538 do_return(&ea, TRUE, FALSE, NULL);
2539 }
2540 need_rethrow = check_cstack = FALSE;
2541#endif
2542
2543doend:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002544 if (curwin->w_cursor.lnum == 0) // can happen with zero line number
Bram Moolenaar6de5e122017-04-20 21:55:44 +02002545 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002546 curwin->w_cursor.lnum = 1;
Bram Moolenaar6de5e122017-04-20 21:55:44 +02002547 curwin->w_cursor.col = 0;
2548 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002549
2550 if (errormsg != NULL && *errormsg != NUL && !did_emsg)
2551 {
2552 if (sourcing)
2553 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002554 if (errormsg != (char *)IObuff)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002555 {
2556 STRCPY(IObuff, errormsg);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002557 errormsg = (char *)IObuff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002558 }
Bram Moolenaara6f4d612011-09-21 19:10:46 +02002559 append_command(*cmdlinep);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002560 }
2561 emsg(errormsg);
2562 }
2563#ifdef FEAT_EVAL
2564 do_errthrow(cstack,
Bram Moolenaar958636c2014-10-21 20:01:58 +02002565 (ea.cmdidx != CMD_SIZE && !IS_USER_CMDIDX(ea.cmdidx))
2566 ? cmdnames[(int)ea.cmdidx].cmd_name : (char_u *)NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002567#endif
2568
Bram Moolenaareffed932018-08-14 13:38:17 +02002569 if (ea.verbose_save >= 0)
2570 p_verbose = ea.verbose_save;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002571
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002572 free_cmdmod();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002573 cmdmod = save_cmdmod;
Bram Moolenaar9a2c0912019-03-30 14:26:18 +01002574 reg_executing = save_reg_executing;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002575
Bram Moolenaareffed932018-08-14 13:38:17 +02002576 if (ea.save_msg_silent != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002577 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002578 // messages could be enabled for a serious error, need to check if the
2579 // counters don't become negative
Bram Moolenaareffed932018-08-14 13:38:17 +02002580 if (!did_emsg || msg_silent > ea.save_msg_silent)
2581 msg_silent = ea.save_msg_silent;
2582 emsg_silent -= ea.did_esilent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002583 if (emsg_silent < 0)
2584 emsg_silent = 0;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002585 // Restore msg_scroll, it's set by file I/O commands, even when no
2586 // message is actually displayed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002587 msg_scroll = save_msg_scroll;
Bram Moolenaar77ab2802009-04-22 12:44:48 +00002588
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002589 // "silent reg" or "silent echo x" inside "redir" leaves msg_col
2590 // somewhere in the line. Put it back in the first column.
Bram Moolenaar77ab2802009-04-22 12:44:48 +00002591 if (redirecting())
2592 msg_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002593 }
2594
Bram Moolenaar7171abe2004-10-11 10:06:20 +00002595#ifdef HAVE_SANDBOX
Bram Moolenaareffed932018-08-14 13:38:17 +02002596 if (ea.did_sandbox)
Bram Moolenaar7171abe2004-10-11 10:06:20 +00002597 --sandbox;
2598#endif
2599
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002600 if (ea.nextcmd && *ea.nextcmd == NUL) // not really a next command
Bram Moolenaar071d4272004-06-13 20:20:40 +00002601 ea.nextcmd = NULL;
2602
2603#ifdef FEAT_EVAL
2604 --ex_nesting_level;
2605#endif
2606
2607 return ea.nextcmd;
2608}
2609#if (_MSC_VER == 1200)
Bram Moolenaar281bdce2005-01-25 21:53:18 +00002610 #pragma optimize( "", on )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002611#endif
2612
2613/*
Bram Moolenaareffed932018-08-14 13:38:17 +02002614 * Parse and skip over command modifiers:
2615 * - update eap->cmd
2616 * - store flags in "cmdmod".
2617 * - Set ex_pressedreturn for an empty command line.
2618 * - set msg_silent for ":silent"
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002619 * - set 'eventignore' to "all" for ":noautocmd"
Bram Moolenaareffed932018-08-14 13:38:17 +02002620 * - set p_verbose for ":verbose"
2621 * - Increment "sandbox" for ":sandbox"
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002622 * When "skip_only" is TRUE the global variables are not changed, except for
2623 * "cmdmod".
Bram Moolenaareffed932018-08-14 13:38:17 +02002624 * Return FAIL when the command is not to be executed.
2625 * May set "errormsg" to an error message.
2626 */
2627 int
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002628parse_command_modifiers(exarg_T *eap, char **errormsg, int skip_only)
Bram Moolenaareffed932018-08-14 13:38:17 +02002629{
2630 char_u *p;
2631
Bram Moolenaara80faa82020-04-12 19:37:17 +02002632 CLEAR_FIELD(cmdmod);
Bram Moolenaareffed932018-08-14 13:38:17 +02002633 eap->verbose_save = -1;
2634 eap->save_msg_silent = -1;
2635
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002636 // Repeat until no more command modifiers are found.
Bram Moolenaareffed932018-08-14 13:38:17 +02002637 for (;;)
2638 {
Bram Moolenaareffed932018-08-14 13:38:17 +02002639 while (*eap->cmd == ' ' || *eap->cmd == '\t' || *eap->cmd == ':')
2640 ++eap->cmd;
2641
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002642 // in ex mode, an empty line works like :+
Bram Moolenaareffed932018-08-14 13:38:17 +02002643 if (*eap->cmd == NUL && exmode_active
2644 && (getline_equal(eap->getline, eap->cookie, getexmodeline)
2645 || getline_equal(eap->getline, eap->cookie, getexline))
2646 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
2647 {
2648 eap->cmd = (char_u *)"+";
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002649 if (!skip_only)
2650 ex_pressedreturn = TRUE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002651 }
2652
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002653 // ignore comment and empty lines
Bram Moolenaareffed932018-08-14 13:38:17 +02002654 if (*eap->cmd == '"')
2655 return FAIL;
2656 if (*eap->cmd == NUL)
2657 {
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002658 if (!skip_only)
2659 ex_pressedreturn = TRUE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002660 return FAIL;
2661 }
2662
Bram Moolenaareffed932018-08-14 13:38:17 +02002663 p = skip_range(eap->cmd, NULL);
2664 switch (*p)
2665 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002666 // When adding an entry, also modify cmd_exists().
Bram Moolenaareffed932018-08-14 13:38:17 +02002667 case 'a': if (!checkforcmd(&eap->cmd, "aboveleft", 3))
2668 break;
2669 cmdmod.split |= WSP_ABOVE;
2670 continue;
2671
2672 case 'b': if (checkforcmd(&eap->cmd, "belowright", 3))
2673 {
2674 cmdmod.split |= WSP_BELOW;
2675 continue;
2676 }
2677 if (checkforcmd(&eap->cmd, "browse", 3))
2678 {
2679#ifdef FEAT_BROWSE_CMD
2680 cmdmod.browse = TRUE;
2681#endif
2682 continue;
2683 }
2684 if (!checkforcmd(&eap->cmd, "botright", 2))
2685 break;
2686 cmdmod.split |= WSP_BOT;
2687 continue;
2688
2689 case 'c': if (!checkforcmd(&eap->cmd, "confirm", 4))
2690 break;
2691#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
2692 cmdmod.confirm = TRUE;
2693#endif
2694 continue;
2695
2696 case 'k': if (checkforcmd(&eap->cmd, "keepmarks", 3))
2697 {
2698 cmdmod.keepmarks = TRUE;
2699 continue;
2700 }
2701 if (checkforcmd(&eap->cmd, "keepalt", 5))
2702 {
2703 cmdmod.keepalt = TRUE;
2704 continue;
2705 }
2706 if (checkforcmd(&eap->cmd, "keeppatterns", 5))
2707 {
2708 cmdmod.keeppatterns = TRUE;
2709 continue;
2710 }
2711 if (!checkforcmd(&eap->cmd, "keepjumps", 5))
2712 break;
2713 cmdmod.keepjumps = TRUE;
2714 continue;
2715
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002716 case 'f': // only accept ":filter {pat} cmd"
Bram Moolenaareffed932018-08-14 13:38:17 +02002717 {
2718 char_u *reg_pat;
2719
2720 if (!checkforcmd(&p, "filter", 4)
2721 || *p == NUL || ends_excmd(*p))
2722 break;
2723 if (*p == '!')
2724 {
2725 cmdmod.filter_force = TRUE;
2726 p = skipwhite(p + 1);
2727 if (*p == NUL || ends_excmd(*p))
2728 break;
2729 }
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002730 if (skip_only)
2731 p = skip_vimgrep_pat(p, NULL, NULL);
2732 else
2733 // NOTE: This puts a NUL after the pattern.
2734 p = skip_vimgrep_pat(p, &reg_pat, NULL);
Bram Moolenaareffed932018-08-14 13:38:17 +02002735 if (p == NULL || *p == NUL)
2736 break;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002737 if (!skip_only)
2738 {
2739 cmdmod.filter_regmatch.regprog =
Bram Moolenaareffed932018-08-14 13:38:17 +02002740 vim_regcomp(reg_pat, RE_MAGIC);
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002741 if (cmdmod.filter_regmatch.regprog == NULL)
2742 break;
2743 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002744 eap->cmd = p;
2745 continue;
2746 }
2747
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002748 // ":hide" and ":hide | cmd" are not modifiers
Bram Moolenaareffed932018-08-14 13:38:17 +02002749 case 'h': if (p != eap->cmd || !checkforcmd(&p, "hide", 3)
2750 || *p == NUL || ends_excmd(*p))
2751 break;
2752 eap->cmd = p;
2753 cmdmod.hide = TRUE;
2754 continue;
2755
2756 case 'l': if (checkforcmd(&eap->cmd, "lockmarks", 3))
2757 {
2758 cmdmod.lockmarks = TRUE;
2759 continue;
2760 }
2761
2762 if (!checkforcmd(&eap->cmd, "leftabove", 5))
2763 break;
2764 cmdmod.split |= WSP_ABOVE;
2765 continue;
2766
2767 case 'n': if (checkforcmd(&eap->cmd, "noautocmd", 3))
2768 {
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002769 if (cmdmod.save_ei == NULL && !skip_only)
Bram Moolenaareffed932018-08-14 13:38:17 +02002770 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002771 // Set 'eventignore' to "all". Restore the
2772 // existing option value later.
Bram Moolenaareffed932018-08-14 13:38:17 +02002773 cmdmod.save_ei = vim_strsave(p_ei);
2774 set_string_option_direct((char_u *)"ei", -1,
2775 (char_u *)"all", OPT_FREE, SID_NONE);
2776 }
2777 continue;
2778 }
2779 if (!checkforcmd(&eap->cmd, "noswapfile", 3))
2780 break;
2781 cmdmod.noswapfile = TRUE;
2782 continue;
2783
2784 case 'r': if (!checkforcmd(&eap->cmd, "rightbelow", 6))
2785 break;
2786 cmdmod.split |= WSP_BELOW;
2787 continue;
2788
2789 case 's': if (checkforcmd(&eap->cmd, "sandbox", 3))
2790 {
2791#ifdef HAVE_SANDBOX
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002792 if (!skip_only)
2793 {
2794 if (!eap->did_sandbox)
2795 ++sandbox;
2796 eap->did_sandbox = TRUE;
2797 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002798#endif
2799 continue;
2800 }
2801 if (!checkforcmd(&eap->cmd, "silent", 3))
2802 break;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002803 if (!skip_only)
2804 {
2805 if (eap->save_msg_silent == -1)
2806 eap->save_msg_silent = msg_silent;
2807 ++msg_silent;
2808 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002809 if (*eap->cmd == '!' && !VIM_ISWHITE(eap->cmd[-1]))
2810 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002811 // ":silent!", but not "silent !cmd"
Bram Moolenaareffed932018-08-14 13:38:17 +02002812 eap->cmd = skipwhite(eap->cmd + 1);
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002813 if (!skip_only)
2814 {
2815 ++emsg_silent;
2816 ++eap->did_esilent;
2817 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002818 }
2819 continue;
2820
2821 case 't': if (checkforcmd(&p, "tab", 3))
2822 {
Bram Moolenaar548e5982018-12-26 21:45:00 +01002823 if (!skip_only)
Bram Moolenaareffed932018-08-14 13:38:17 +02002824 {
Bram Moolenaar548e5982018-12-26 21:45:00 +01002825 long tabnr = get_address(eap, &eap->cmd,
2826 ADDR_TABS, eap->skip,
2827 skip_only, FALSE, 1);
2828 if (tabnr == MAXLNUM)
2829 cmdmod.tab = tabpage_index(curtab) + 1;
2830 else
Bram Moolenaareffed932018-08-14 13:38:17 +02002831 {
Bram Moolenaar548e5982018-12-26 21:45:00 +01002832 if (tabnr < 0 || tabnr > LAST_TAB_NR)
2833 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002834 *errormsg = _(e_invrange);
Bram Moolenaar548e5982018-12-26 21:45:00 +01002835 return FAIL;
2836 }
2837 cmdmod.tab = tabnr + 1;
Bram Moolenaareffed932018-08-14 13:38:17 +02002838 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002839 }
2840 eap->cmd = p;
2841 continue;
2842 }
2843 if (!checkforcmd(&eap->cmd, "topleft", 2))
2844 break;
2845 cmdmod.split |= WSP_TOP;
2846 continue;
2847
2848 case 'u': if (!checkforcmd(&eap->cmd, "unsilent", 3))
2849 break;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002850 if (!skip_only)
2851 {
2852 if (eap->save_msg_silent == -1)
2853 eap->save_msg_silent = msg_silent;
2854 msg_silent = 0;
2855 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002856 continue;
2857
2858 case 'v': if (checkforcmd(&eap->cmd, "vertical", 4))
2859 {
2860 cmdmod.split |= WSP_VERT;
2861 continue;
2862 }
2863 if (!checkforcmd(&p, "verbose", 4))
2864 break;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002865 if (!skip_only)
2866 {
2867 if (eap->verbose_save < 0)
2868 eap->verbose_save = p_verbose;
2869 if (vim_isdigit(*eap->cmd))
2870 p_verbose = atoi((char *)eap->cmd);
2871 else
2872 p_verbose = 1;
2873 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002874 eap->cmd = p;
2875 continue;
2876 }
2877 break;
2878 }
2879
2880 return OK;
2881}
2882
2883/*
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002884 * Free contents of "cmdmod".
2885 */
2886 static void
2887free_cmdmod(void)
2888{
2889 if (cmdmod.save_ei != NULL)
2890 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002891 // Restore 'eventignore' to the value before ":noautocmd".
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002892 set_string_option_direct((char_u *)"ei", -1, cmdmod.save_ei,
2893 OPT_FREE, SID_NONE);
2894 free_string_option(cmdmod.save_ei);
2895 }
2896
2897 if (cmdmod.filter_regmatch.regprog != NULL)
2898 vim_regfree(cmdmod.filter_regmatch.regprog);
2899}
2900
2901/*
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002902 * Parse the address range, if any, in "eap".
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02002903 * May set the last search pattern, unless "silent" is TRUE.
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002904 * Return FAIL and set "errormsg" or return OK.
2905 */
2906 int
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002907parse_cmd_address(exarg_T *eap, char **errormsg, int silent)
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002908{
2909 int address_count = 1;
2910 linenr_T lnum;
2911
2912 // Repeat for all ',' or ';' separated addresses.
2913 for (;;)
2914 {
2915 eap->line1 = eap->line2;
2916 switch (eap->addr_type)
2917 {
2918 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02002919 case ADDR_OTHER:
Bram Moolenaarb5383b12020-05-18 19:46:48 +02002920 // Default is the cursor line number. Avoid using an invalid
2921 // line number though.
2922 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
2923 eap->line2 = curbuf->b_ml.ml_line_count;
2924 else
2925 eap->line2 = curwin->w_cursor.lnum;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002926 break;
2927 case ADDR_WINDOWS:
2928 eap->line2 = CURRENT_WIN_NR;
2929 break;
2930 case ADDR_ARGUMENTS:
2931 eap->line2 = curwin->w_arg_idx + 1;
2932 if (eap->line2 > ARGCOUNT)
2933 eap->line2 = ARGCOUNT;
2934 break;
2935 case ADDR_LOADED_BUFFERS:
2936 case ADDR_BUFFERS:
2937 eap->line2 = curbuf->b_fnum;
2938 break;
2939 case ADDR_TABS:
2940 eap->line2 = CURRENT_TAB_NR;
2941 break;
2942 case ADDR_TABS_RELATIVE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02002943 case ADDR_UNSIGNED:
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002944 eap->line2 = 1;
2945 break;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002946 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02002947#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02002948 eap->line2 = qf_get_cur_idx(eap);
2949#endif
2950 break;
2951 case ADDR_QUICKFIX_VALID:
2952#ifdef FEAT_QUICKFIX
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002953 eap->line2 = qf_get_cur_valid_idx(eap);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002954#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02002955 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02002956 case ADDR_NONE:
2957 // Will give an error later if a range is found.
2958 break;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002959 }
2960 eap->cmd = skipwhite(eap->cmd);
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02002961 lnum = get_address(eap, &eap->cmd, eap->addr_type, eap->skip, silent,
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002962 eap->addr_count == 0, address_count++);
2963 if (eap->cmd == NULL) // error detected
2964 return FAIL;
2965 if (lnum == MAXLNUM)
2966 {
2967 if (*eap->cmd == '%') // '%' - all lines
2968 {
2969 ++eap->cmd;
2970 switch (eap->addr_type)
2971 {
2972 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02002973 case ADDR_OTHER:
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002974 eap->line1 = 1;
2975 eap->line2 = curbuf->b_ml.ml_line_count;
2976 break;
2977 case ADDR_LOADED_BUFFERS:
2978 {
2979 buf_T *buf = firstbuf;
2980
2981 while (buf->b_next != NULL
2982 && buf->b_ml.ml_mfp == NULL)
2983 buf = buf->b_next;
2984 eap->line1 = buf->b_fnum;
2985 buf = lastbuf;
2986 while (buf->b_prev != NULL
2987 && buf->b_ml.ml_mfp == NULL)
2988 buf = buf->b_prev;
2989 eap->line2 = buf->b_fnum;
2990 break;
2991 }
2992 case ADDR_BUFFERS:
2993 eap->line1 = firstbuf->b_fnum;
2994 eap->line2 = lastbuf->b_fnum;
2995 break;
2996 case ADDR_WINDOWS:
2997 case ADDR_TABS:
2998 if (IS_USER_CMDIDX(eap->cmdidx))
2999 {
3000 eap->line1 = 1;
3001 eap->line2 = eap->addr_type == ADDR_WINDOWS
3002 ? LAST_WIN_NR : LAST_TAB_NR;
3003 }
3004 else
3005 {
3006 // there is no Vim command which uses '%' and
3007 // ADDR_WINDOWS or ADDR_TABS
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003008 *errormsg = _(e_invrange);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003009 return FAIL;
3010 }
3011 break;
3012 case ADDR_TABS_RELATIVE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02003013 case ADDR_UNSIGNED:
3014 case ADDR_QUICKFIX:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003015 *errormsg = _(e_invrange);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003016 return FAIL;
3017 case ADDR_ARGUMENTS:
3018 if (ARGCOUNT == 0)
3019 eap->line1 = eap->line2 = 0;
3020 else
3021 {
3022 eap->line1 = 1;
3023 eap->line2 = ARGCOUNT;
3024 }
3025 break;
Bram Moolenaar25190db2019-05-04 15:05:28 +02003026 case ADDR_QUICKFIX_VALID:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003027#ifdef FEAT_QUICKFIX
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003028 eap->line1 = 1;
Bram Moolenaar25190db2019-05-04 15:05:28 +02003029 eap->line2 = qf_get_valid_size(eap);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003030 if (eap->line2 == 0)
3031 eap->line2 = 1;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003032#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003033 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02003034 case ADDR_NONE:
3035 // Will give an error later if a range is found.
3036 break;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003037 }
3038 ++eap->addr_count;
3039 }
3040 else if (*eap->cmd == '*' && vim_strchr(p_cpo, CPO_STAR) == NULL)
3041 {
3042 pos_T *fp;
3043
3044 // '*' - visual area
3045 if (eap->addr_type != ADDR_LINES)
3046 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003047 *errormsg = _(e_invrange);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003048 return FAIL;
3049 }
3050
3051 ++eap->cmd;
3052 if (!eap->skip)
3053 {
3054 fp = getmark('<', FALSE);
3055 if (check_mark(fp) == FAIL)
3056 return FAIL;
3057 eap->line1 = fp->lnum;
3058 fp = getmark('>', FALSE);
3059 if (check_mark(fp) == FAIL)
3060 return FAIL;
3061 eap->line2 = fp->lnum;
3062 ++eap->addr_count;
3063 }
3064 }
3065 }
3066 else
3067 eap->line2 = lnum;
3068 eap->addr_count++;
3069
3070 if (*eap->cmd == ';')
3071 {
3072 if (!eap->skip)
3073 {
3074 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaar0e717042020-04-27 19:29:01 +02003075 // Don't leave the cursor on an illegal line or column, but do
3076 // accept zero as address, so 0;/PATTERN/ works correctly.
3077 if (eap->line2 > 0)
3078 check_cursor();
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003079 }
3080 }
3081 else if (*eap->cmd != ',')
3082 break;
3083 ++eap->cmd;
3084 }
3085
3086 // One address given: set start and end lines.
3087 if (eap->addr_count == 1)
3088 {
3089 eap->line1 = eap->line2;
3090 // ... but only implicit: really no address given
3091 if (lnum == MAXLNUM)
3092 eap->addr_count = 0;
3093 }
3094 return OK;
3095}
3096
3097/*
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00003098 * Check for an Ex command with optional tail.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003099 * If there is a match advance "pp" to the argument and return TRUE.
3100 */
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00003101 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003102checkforcmd(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003103 char_u **pp, // start of command
3104 char *cmd, // name of command
3105 int len) // required length
Bram Moolenaar071d4272004-06-13 20:20:40 +00003106{
3107 int i;
3108
3109 for (i = 0; cmd[i] != NUL; ++i)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003110 if (((char_u *)cmd)[i] != (*pp)[i])
Bram Moolenaar071d4272004-06-13 20:20:40 +00003111 break;
3112 if (i >= len && !isalpha((*pp)[i]))
3113 {
3114 *pp = skipwhite(*pp + i);
3115 return TRUE;
3116 }
3117 return FALSE;
3118}
3119
3120/*
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003121 * Append "cmd" to the error message in IObuff.
3122 * Takes care of limiting the length and handling 0xa0, which would be
3123 * invisible otherwise.
3124 */
3125 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003126append_command(char_u *cmd)
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003127{
3128 char_u *s = cmd;
3129 char_u *d;
3130
3131 STRCAT(IObuff, ": ");
3132 d = IObuff + STRLEN(IObuff);
3133 while (*s != NUL && d - IObuff < IOSIZE - 7)
3134 {
Bram Moolenaar13505972019-01-24 15:04:48 +01003135 if (enc_utf8 ? (s[0] == 0xc2 && s[1] == 0xa0) : *s == 0xa0)
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003136 {
Bram Moolenaar13505972019-01-24 15:04:48 +01003137 s += enc_utf8 ? 2 : 1;
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003138 STRCPY(d, "<a0>");
3139 d += 4;
3140 }
3141 else
3142 MB_COPY_CHAR(s, d);
3143 }
3144 *d = NUL;
3145}
3146
3147/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003148 * Find an Ex command by its name, either built-in or user.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003149 * Start of the name can be found at eap->cmd.
Bram Moolenaar25190db2019-05-04 15:05:28 +02003150 * Sets eap->cmdidx and returns a pointer to char after the command name.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003151 * "full" is set to TRUE if the whole command name matched.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003152 *
3153 * If "lookup" is not NULL recognize expression without "eval" or "call" and
3154 * assignment without "let". Sets eap->cmdidx to the command while returning
3155 * "eap->cmd".
3156 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00003157 * Returns NULL for an ambiguous user command.
3158 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003159 char_u *
3160find_ex_command(
3161 exarg_T *eap,
Bram Moolenaara494f562020-04-18 17:45:38 +02003162 int *full UNUSED,
Bram Moolenaarb84a3812020-05-01 15:44:29 +02003163 void *(*lookup)(char_u *, size_t, cctx_T *) UNUSED,
Bram Moolenaara494f562020-04-18 17:45:38 +02003164 cctx_T *cctx UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003165{
3166 int len;
3167 char_u *p;
Bram Moolenaardf177f62005-02-22 08:39:57 +00003168 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003169
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003170#ifdef FEAT_EVAL
3171 /*
3172 * Recognize a Vim9 script function/method call and assignment:
3173 * "lvar = value", "lvar(arg)", "[1, 2 3]->Func()"
3174 */
Bram Moolenaar0c6ceaf2020-02-22 18:36:32 +01003175 p = eap->cmd;
3176 if (lookup != NULL && (*p == '('
3177 || ((p = to_name_const_end(eap->cmd)) > eap->cmd && *p != NUL)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003178 {
3179 int oplen;
3180 int heredoc;
3181
3182 // "funcname(" is always a function call.
3183 // "varname[]" is an expression.
3184 // "g:varname" is an expression.
3185 // "varname->expr" is an expression.
Bram Moolenaar0c6ceaf2020-02-22 18:36:32 +01003186 // "(..." is an expression.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003187 if (*p == '('
3188 || *p == '['
3189 || p[1] == ':'
3190 || (*p == '-' && p[1] == '>'))
3191 {
3192 eap->cmdidx = CMD_eval;
3193 return eap->cmd;
3194 }
3195
3196 oplen = assignment_len(skipwhite(p), &heredoc);
3197 if (oplen > 0)
3198 {
3199 // Recognize an assignment if we recognize the variable name:
3200 // "g:var = expr"
3201 // "var = expr" where "var" is a local var name.
3202 if (((p - eap->cmd) > 2 && eap->cmd[1] == ':')
Bram Moolenaarb84a3812020-05-01 15:44:29 +02003203 || lookup(eap->cmd, p - eap->cmd, cctx) != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003204 {
3205 eap->cmdidx = CMD_let;
3206 return eap->cmd;
3207 }
3208 }
3209 }
3210#endif
3211
Bram Moolenaar071d4272004-06-13 20:20:40 +00003212 /*
3213 * Isolate the command and search for it in the command table.
Bram Moolenaar81870892007-11-11 18:17:28 +00003214 * Exceptions:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003215 * - the 'k' command can directly be followed by any character.
3216 * - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003217 * but :sre[wind] is another command, as are :scr[iptnames],
Bram Moolenaar071d4272004-06-13 20:20:40 +00003218 * :scs[cope], :sim[alt], :sig[ns] and :sil[ent].
Bram Moolenaardf177f62005-02-22 08:39:57 +00003219 * - the "d" command can directly be followed by 'l' or 'p' flag.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003220 */
3221 p = eap->cmd;
3222 if (*p == 'k')
3223 {
3224 eap->cmdidx = CMD_k;
3225 ++p;
3226 }
3227 else if (p[0] == 's'
Bram Moolenaar204b93f2015-08-04 22:02:51 +02003228 && ((p[1] == 'c' && (p[2] == NUL || (p[2] != 's' && p[2] != 'r'
3229 && (p[3] == NUL || (p[3] != 'i' && p[4] != 'p')))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003230 || p[1] == 'g'
3231 || (p[1] == 'i' && p[2] != 'm' && p[2] != 'l' && p[2] != 'g')
3232 || p[1] == 'I'
3233 || (p[1] == 'r' && p[2] != 'e')))
3234 {
3235 eap->cmdidx = CMD_substitute;
3236 ++p;
3237 }
3238 else
3239 {
3240 while (ASCII_ISALPHA(*p))
3241 ++p;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003242 // for python 3.x support ":py3", ":python3", ":py3file", etc.
Bram Moolenaar55d5c032010-07-17 23:52:29 +02003243 if (eap->cmd[0] == 'p' && eap->cmd[1] == 'y')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003244 {
Bram Moolenaarb6590522010-07-21 16:00:43 +02003245 while (ASCII_ISALNUM(*p))
3246 ++p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003247 }
3248 else if (*p == '9' && STRNCMP("vim9", eap->cmd, 4) == 0)
3249 {
3250 // include "9" for "vim9script"
3251 ++p;
3252 while (ASCII_ISALPHA(*p))
3253 ++p;
3254 }
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02003255
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003256 // check for non-alpha command
Bram Moolenaar071d4272004-06-13 20:20:40 +00003257 if (p == eap->cmd && vim_strchr((char_u *)"@*!=><&~#", *p) != NULL)
3258 ++p;
3259 len = (int)(p - eap->cmd);
Bram Moolenaardf177f62005-02-22 08:39:57 +00003260 if (*eap->cmd == 'd' && (p[-1] == 'l' || p[-1] == 'p'))
3261 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003262 // Check for ":dl", ":dell", etc. to ":deletel": that's
3263 // :delete with the 'l' flag. Same for 'p'.
Bram Moolenaardf177f62005-02-22 08:39:57 +00003264 for (i = 0; i < len; ++i)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003265 if (eap->cmd[i] != ((char_u *)"delete")[i])
Bram Moolenaardf177f62005-02-22 08:39:57 +00003266 break;
3267 if (i == len - 1)
3268 {
3269 --len;
3270 if (p[-1] == 'l')
3271 eap->flags |= EXFLAG_LIST;
3272 else
3273 eap->flags |= EXFLAG_PRINT;
3274 }
3275 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003276
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003277 if (ASCII_ISLOWER(eap->cmd[0]))
3278 {
Bram Moolenaar6c0c1e82017-03-25 15:07:43 +01003279 int c1 = eap->cmd[0];
Bram Moolenaar94f82cb2019-07-24 22:30:27 +02003280 int c2 = len == 1 ? NUL : eap->cmd[1];
Bram Moolenaar6c0c1e82017-03-25 15:07:43 +01003281
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003282 if (command_count != (int)CMD_SIZE)
3283 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003284 iemsg(_("E943: Command table needs to be updated, run 'make cmdidxs'"));
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003285 getout(1);
3286 }
3287
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003288 // Use a precomputed index for fast look-up in cmdnames[]
3289 // taking into account the first 2 letters of eap->cmd.
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003290 eap->cmdidx = cmdidxs1[CharOrdLow(c1)];
3291 if (ASCII_ISLOWER(c2))
3292 eap->cmdidx += cmdidxs2[CharOrdLow(c1)][CharOrdLow(c2)];
3293 }
Bram Moolenaara494f562020-04-18 17:45:38 +02003294 else if (ASCII_ISUPPER(eap->cmd[0]))
3295 eap->cmdidx = CMD_Next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003296 else
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003297 eap->cmdidx = CMD_bang;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003298
3299 for ( ; (int)eap->cmdidx < (int)CMD_SIZE;
3300 eap->cmdidx = (cmdidx_T)((int)eap->cmdidx + 1))
3301 if (STRNCMP(cmdnames[(int)eap->cmdidx].cmd_name, (char *)eap->cmd,
3302 (size_t)len) == 0)
3303 {
3304#ifdef FEAT_EVAL
3305 if (full != NULL
3306 && cmdnames[(int)eap->cmdidx].cmd_name[len] == NUL)
3307 *full = TRUE;
3308#endif
3309 break;
3310 }
3311
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003312 // Look for a user defined command as a last resort. Let ":Print" be
3313 // overruled by a user defined command.
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01003314 if ((eap->cmdidx == CMD_SIZE || eap->cmdidx == CMD_Print)
3315 && *eap->cmd >= 'A' && *eap->cmd <= 'Z')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003316 {
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003317 // User defined commands may contain digits.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003318 while (ASCII_ISALNUM(*p))
3319 ++p;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003320 p = find_ucmd(eap, p, full, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003321 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003322 if (p == eap->cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003323 eap->cmdidx = CMD_SIZE;
3324 }
3325
3326 return p;
3327}
3328
3329#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003330static struct cmdmod
3331{
3332 char *name;
3333 int minlen;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003334 int has_count; // :123verbose :3tab
Bram Moolenaared53fb92007-11-24 20:50:24 +00003335} cmdmods[] = {
3336 {"aboveleft", 3, FALSE},
3337 {"belowright", 3, FALSE},
3338 {"botright", 2, FALSE},
3339 {"browse", 3, FALSE},
3340 {"confirm", 4, FALSE},
Bram Moolenaar7b668e82016-08-23 23:51:21 +02003341 {"filter", 4, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003342 {"hide", 3, FALSE},
3343 {"keepalt", 5, FALSE},
3344 {"keepjumps", 5, FALSE},
3345 {"keepmarks", 3, FALSE},
Bram Moolenaara939e432013-11-09 05:30:26 +01003346 {"keeppatterns", 5, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003347 {"leftabove", 5, FALSE},
3348 {"lockmarks", 3, FALSE},
Bram Moolenaarca9f9582008-09-18 10:44:28 +00003349 {"noautocmd", 3, FALSE},
Bram Moolenaar5803ae62014-03-23 16:04:02 +01003350 {"noswapfile", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003351 {"rightbelow", 6, FALSE},
3352 {"sandbox", 3, FALSE},
3353 {"silent", 3, FALSE},
3354 {"tab", 3, TRUE},
3355 {"topleft", 2, FALSE},
Bram Moolenaar8e258a42009-07-09 13:55:43 +00003356 {"unsilent", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003357 {"verbose", 4, TRUE},
3358 {"vertical", 4, FALSE},
3359};
3360
3361/*
3362 * Return length of a command modifier (including optional count).
3363 * Return zero when it's not a modifier.
3364 */
3365 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003366modifier_len(char_u *cmd)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003367{
3368 int i, j;
3369 char_u *p = cmd;
3370
3371 if (VIM_ISDIGIT(*cmd))
3372 p = skipwhite(skipdigits(cmd));
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00003373 for (i = 0; i < (int)(sizeof(cmdmods) / sizeof(struct cmdmod)); ++i)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003374 {
3375 for (j = 0; p[j] != NUL; ++j)
3376 if (p[j] != cmdmods[i].name[j])
3377 break;
Bram Moolenaar2d473ab2013-06-12 17:12:24 +02003378 if (!ASCII_ISALPHA(p[j]) && j >= cmdmods[i].minlen
Bram Moolenaared53fb92007-11-24 20:50:24 +00003379 && (p == cmd || cmdmods[i].has_count))
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00003380 return j + (int)(p - cmd);
Bram Moolenaared53fb92007-11-24 20:50:24 +00003381 }
3382 return 0;
3383}
3384
Bram Moolenaar071d4272004-06-13 20:20:40 +00003385/*
3386 * Return > 0 if an Ex command "name" exists.
3387 * Return 2 if there is an exact match.
3388 * Return 3 if there is an ambiguous match.
3389 */
3390 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003391cmd_exists(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003392{
3393 exarg_T ea;
3394 int full = FALSE;
3395 int i;
3396 int j;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003397 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003398
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003399 // Check command modifiers.
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00003400 for (i = 0; i < (int)(sizeof(cmdmods) / sizeof(struct cmdmod)); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003401 {
3402 for (j = 0; name[j] != NUL; ++j)
3403 if (name[j] != cmdmods[i].name[j])
3404 break;
3405 if (name[j] == NUL && j >= cmdmods[i].minlen)
3406 return (cmdmods[i].name[j] == NUL ? 2 : 1);
3407 }
3408
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003409 // Check built-in commands and user defined commands.
3410 // For ":2match" and ":3match" we need to skip the number.
Bram Moolenaara9587612006-05-04 21:47:50 +00003411 ea.cmd = (*name == '2' || *name == '3') ? name + 1 : name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003412 ea.cmdidx = (cmdidx_T)0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003413 p = find_ex_command(&ea, &full, NULL, NULL);
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003414 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003415 return 3;
Bram Moolenaara9587612006-05-04 21:47:50 +00003416 if (vim_isdigit(*name) && ea.cmdidx != CMD_match)
3417 return 0;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003418 if (*skipwhite(p) != NUL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003419 return 0; // trailing garbage
Bram Moolenaar071d4272004-06-13 20:20:40 +00003420 return (ea.cmdidx == CMD_SIZE ? 0 : (full ? 2 : 1));
3421}
3422#endif
3423
Bram Moolenaard0190392019-08-23 21:17:35 +02003424 cmdidx_T
3425excmd_get_cmdidx(char_u *cmd, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003426{
Bram Moolenaard0190392019-08-23 21:17:35 +02003427 cmdidx_T idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003428
Bram Moolenaard0190392019-08-23 21:17:35 +02003429 for (idx = (cmdidx_T)0; (int)idx < (int)CMD_SIZE;
3430 idx = (cmdidx_T)((int)idx + 1))
3431 if (STRNCMP(cmdnames[(int)idx].cmd_name, cmd, (size_t)len) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003432 break;
3433
Bram Moolenaard0190392019-08-23 21:17:35 +02003434 return idx;
3435}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003436
Bram Moolenaard0190392019-08-23 21:17:35 +02003437 long
3438excmd_get_argt(cmdidx_T idx)
3439{
3440 return (long)cmdnames[(int)idx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003441}
3442
3443/*
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003444 * Skip a range specifier of the form: addr [,addr] [;addr] ..
Bram Moolenaar071d4272004-06-13 20:20:40 +00003445 *
3446 * Backslashed delimiters after / or ? will be skipped, and commands will
3447 * not be expanded between /'s and ?'s or after "'".
3448 *
Bram Moolenaardf177f62005-02-22 08:39:57 +00003449 * Also skip white space and ":" characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003450 * Returns the "cmd" pointer advanced to beyond the range.
3451 */
3452 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003453skip_range(
3454 char_u *cmd,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003455 int *ctx) // pointer to xp_context or NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003456{
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003457 unsigned delim;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003458
Bram Moolenaarcbf20fb2017-02-03 21:19:04 +01003459 while (vim_strchr((char_u *)" \t0123456789.$%'/?-+,;\\", *cmd) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003460 {
Bram Moolenaarcbf20fb2017-02-03 21:19:04 +01003461 if (*cmd == '\\')
3462 {
3463 if (cmd[1] == '?' || cmd[1] == '/' || cmd[1] == '&')
3464 ++cmd;
3465 else
3466 break;
3467 }
3468 else if (*cmd == '\'')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003469 {
3470 if (*++cmd == NUL && ctx != NULL)
3471 *ctx = EXPAND_NOTHING;
3472 }
3473 else if (*cmd == '/' || *cmd == '?')
3474 {
3475 delim = *cmd++;
3476 while (*cmd != NUL && *cmd != delim)
3477 if (*cmd++ == '\\' && *cmd != NUL)
3478 ++cmd;
3479 if (*cmd == NUL && ctx != NULL)
3480 *ctx = EXPAND_NOTHING;
3481 }
3482 if (*cmd != NUL)
3483 ++cmd;
3484 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00003485
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003486 // Skip ":" and white space.
Bram Moolenaardf177f62005-02-22 08:39:57 +00003487 while (*cmd == ':')
3488 cmd = skipwhite(cmd + 1);
3489
Bram Moolenaar071d4272004-06-13 20:20:40 +00003490 return cmd;
3491}
3492
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003493 static void
3494addr_error(cmd_addr_T addr_type)
3495{
3496 if (addr_type == ADDR_NONE)
3497 emsg(_(e_norange));
3498 else
3499 emsg(_(e_invrange));
3500}
3501
Bram Moolenaar071d4272004-06-13 20:20:40 +00003502/*
Bram Moolenaar198cb662018-09-06 21:44:17 +02003503 * Get a single EX address.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003504 *
3505 * Set ptr to the next character after the part that was interpreted.
3506 * Set ptr to NULL when an error is encountered.
Bram Moolenaar198cb662018-09-06 21:44:17 +02003507 * This may set the last used search pattern.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003508 *
3509 * Return MAXLNUM when no Ex address was found.
3510 */
3511 static linenr_T
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003512get_address(
3513 exarg_T *eap UNUSED,
3514 char_u **ptr,
Bram Moolenaar1b03a192019-12-08 17:08:29 +01003515 cmd_addr_T addr_type,
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02003516 int skip, // only skip the address, don't use it
3517 int silent, // no errors or side effects
3518 int to_other_file, // flag: may jump to other file
3519 int address_count UNUSED) // 1 for first address, >1 after comma
Bram Moolenaar071d4272004-06-13 20:20:40 +00003520{
3521 int c;
3522 int i;
3523 long n;
3524 char_u *cmd;
3525 pos_T pos;
3526 pos_T *fp;
3527 linenr_T lnum;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003528 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003529
3530 cmd = skipwhite(*ptr);
3531 lnum = MAXLNUM;
3532 do
3533 {
3534 switch (*cmd)
3535 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003536 case '.': // '.' - Cursor position
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003537 ++cmd;
3538 switch (addr_type)
3539 {
3540 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02003541 case ADDR_OTHER:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003542 lnum = curwin->w_cursor.lnum;
3543 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003544 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01003545 lnum = CURRENT_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003546 break;
3547 case ADDR_ARGUMENTS:
3548 lnum = curwin->w_arg_idx + 1;
3549 break;
3550 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003551 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003552 lnum = curbuf->b_fnum;
3553 break;
3554 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01003555 lnum = CURRENT_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003556 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02003557 case ADDR_NONE:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003558 case ADDR_TABS_RELATIVE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02003559 case ADDR_UNSIGNED:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003560 addr_error(addr_type);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01003561 cmd = NULL;
3562 goto error;
3563 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003564 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003565#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02003566 lnum = qf_get_cur_idx(eap);
3567#endif
3568 break;
3569 case ADDR_QUICKFIX_VALID:
3570#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003571 lnum = qf_get_cur_valid_idx(eap);
Bram Moolenaare906c502015-09-09 21:10:39 +02003572#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003573 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003574 }
3575 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003576
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003577 case '$': // '$' - last line
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003578 ++cmd;
3579 switch (addr_type)
3580 {
3581 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02003582 case ADDR_OTHER:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003583 lnum = curbuf->b_ml.ml_line_count;
3584 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003585 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01003586 lnum = LAST_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003587 break;
3588 case ADDR_ARGUMENTS:
3589 lnum = ARGCOUNT;
3590 break;
3591 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003592 buf = lastbuf;
3593 while (buf->b_ml.ml_mfp == NULL)
3594 {
3595 if (buf->b_prev == NULL)
3596 break;
3597 buf = buf->b_prev;
3598 }
3599 lnum = buf->b_fnum;
3600 break;
3601 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003602 lnum = lastbuf->b_fnum;
3603 break;
3604 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01003605 lnum = LAST_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003606 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02003607 case ADDR_NONE:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003608 case ADDR_TABS_RELATIVE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02003609 case ADDR_UNSIGNED:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003610 addr_error(addr_type);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01003611 cmd = NULL;
3612 goto error;
3613 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003614 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003615#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003616 lnum = qf_get_size(eap);
3617 if (lnum == 0)
3618 lnum = 1;
Bram Moolenaare906c502015-09-09 21:10:39 +02003619#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003620 break;
Bram Moolenaar25190db2019-05-04 15:05:28 +02003621 case ADDR_QUICKFIX_VALID:
3622#ifdef FEAT_QUICKFIX
3623 lnum = qf_get_valid_size(eap);
3624 if (lnum == 0)
3625 lnum = 1;
3626#endif
3627 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003628 }
3629 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003630
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003631 case '\'': // ''' - mark
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003632 if (*++cmd == NUL)
3633 {
3634 cmd = NULL;
3635 goto error;
3636 }
3637 if (addr_type != ADDR_LINES)
3638 {
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003639 addr_error(addr_type);
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01003640 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003641 goto error;
3642 }
3643 if (skip)
3644 ++cmd;
3645 else
3646 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003647 // Only accept a mark in another file when it is
3648 // used by itself: ":'M".
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003649 fp = getmark(*cmd, to_other_file && cmd[1] == NUL);
3650 ++cmd;
3651 if (fp == (pos_T *)-1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003652 // Jumped to another file.
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003653 lnum = curwin->w_cursor.lnum;
3654 else
3655 {
3656 if (check_mark(fp) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003657 {
3658 cmd = NULL;
3659 goto error;
3660 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003661 lnum = fp->lnum;
3662 }
3663 }
3664 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003665
3666 case '/':
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003667 case '?': // '/' or '?' - search
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003668 c = *cmd++;
3669 if (addr_type != ADDR_LINES)
3670 {
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003671 addr_error(addr_type);
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01003672 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003673 goto error;
3674 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003675 if (skip) // skip "/pat/"
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003676 {
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02003677 cmd = skip_regexp(cmd, c, (int)p_magic);
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003678 if (*cmd == c)
3679 ++cmd;
3680 }
3681 else
3682 {
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02003683 int flags;
3684
3685 pos = curwin->w_cursor; // save curwin->w_cursor
3686
3687 // When '/' or '?' follows another address, start from
3688 // there.
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003689 if (lnum != MAXLNUM)
3690 curwin->w_cursor.lnum = lnum;
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02003691
3692 // Start a forward search at the end of the line (unless
3693 // before the first line).
3694 // Start a backward search at the start of the line.
3695 // This makes sure we never match in the current
3696 // line, and can match anywhere in the
3697 // next/previous line.
Bram Moolenaar8ada6aa2017-12-19 21:23:21 +01003698 if (c == '/' && curwin->w_cursor.lnum > 0)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003699 curwin->w_cursor.col = MAXCOL;
3700 else
3701 curwin->w_cursor.col = 0;
3702 searchcmdlen = 0;
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02003703 flags = silent ? 0 : SEARCH_HIS | SEARCH_MSG;
Bram Moolenaarc036e872020-02-21 21:30:52 +01003704 if (!do_search(NULL, c, c, cmd, 1L, flags, NULL))
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003705 {
3706 curwin->w_cursor = pos;
3707 cmd = NULL;
3708 goto error;
3709 }
3710 lnum = curwin->w_cursor.lnum;
3711 curwin->w_cursor = pos;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003712 // adjust command string pointer
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003713 cmd += searchcmdlen;
3714 }
3715 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003716
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003717 case '\\': // "\?", "\/" or "\&", repeat search
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003718 ++cmd;
3719 if (addr_type != ADDR_LINES)
3720 {
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003721 addr_error(addr_type);
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01003722 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003723 goto error;
3724 }
3725 if (*cmd == '&')
3726 i = RE_SUBST;
3727 else if (*cmd == '?' || *cmd == '/')
3728 i = RE_SEARCH;
3729 else
3730 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003731 emsg(_(e_backslash));
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003732 cmd = NULL;
3733 goto error;
3734 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003735
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003736 if (!skip)
3737 {
3738 /*
3739 * When search follows another address, start from
3740 * there.
3741 */
3742 if (lnum != MAXLNUM)
3743 pos.lnum = lnum;
3744 else
3745 pos.lnum = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003746
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003747 /*
3748 * Start the search just like for the above
3749 * do_search().
3750 */
3751 if (*cmd != '?')
3752 pos.col = MAXCOL;
3753 else
3754 pos.col = 0;
Bram Moolenaarbd8539a2015-08-11 18:53:03 +02003755 pos.coladd = 0;
Bram Moolenaar5d24a222018-12-23 19:10:09 +01003756 if (searchit(curwin, curbuf, &pos, NULL,
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003757 *cmd == '?' ? BACKWARD : FORWARD,
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02003758 (char_u *)"", 1L, SEARCH_MSG, i, NULL) != FAIL)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003759 lnum = pos.lnum;
3760 else
3761 {
3762 cmd = NULL;
3763 goto error;
3764 }
3765 }
3766 ++cmd;
3767 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003768
3769 default:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003770 if (VIM_ISDIGIT(*cmd)) // absolute line number
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003771 lnum = getdigits(&cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003772 }
3773
3774 for (;;)
3775 {
3776 cmd = skipwhite(cmd);
3777 if (*cmd != '-' && *cmd != '+' && !VIM_ISDIGIT(*cmd))
3778 break;
3779
3780 if (lnum == MAXLNUM)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003781 {
3782 switch (addr_type)
3783 {
3784 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02003785 case ADDR_OTHER:
3786 // "+1" is same as ".+1"
Bram Moolenaarf240e182014-11-27 18:33:02 +01003787 lnum = curwin->w_cursor.lnum;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003788 break;
3789 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01003790 lnum = CURRENT_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003791 break;
3792 case ADDR_ARGUMENTS:
3793 lnum = curwin->w_arg_idx + 1;
3794 break;
3795 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003796 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003797 lnum = curbuf->b_fnum;
3798 break;
3799 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01003800 lnum = CURRENT_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003801 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01003802 case ADDR_TABS_RELATIVE:
3803 lnum = 1;
3804 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003805 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003806#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02003807 lnum = qf_get_cur_idx(eap);
3808#endif
3809 break;
3810 case ADDR_QUICKFIX_VALID:
3811#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003812 lnum = qf_get_cur_valid_idx(eap);
Bram Moolenaare906c502015-09-09 21:10:39 +02003813#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003814 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02003815 case ADDR_NONE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02003816 case ADDR_UNSIGNED:
3817 lnum = 0;
Bram Moolenaarb7316892019-05-01 18:08:42 +02003818 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003819 }
3820 }
3821
Bram Moolenaar071d4272004-06-13 20:20:40 +00003822 if (VIM_ISDIGIT(*cmd))
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003823 i = '+'; // "number" is same as "+number"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003824 else
3825 i = *cmd++;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003826 if (!VIM_ISDIGIT(*cmd)) // '+' is '+1', but '+0' is not '+1'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003827 n = 1;
3828 else
3829 n = getdigits(&cmd);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01003830
3831 if (addr_type == ADDR_TABS_RELATIVE)
3832 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003833 emsg(_(e_invrange));
Bram Moolenaar2f72c702017-01-29 14:48:10 +01003834 cmd = NULL;
3835 goto error;
3836 }
3837 else if (addr_type == ADDR_LOADED_BUFFERS
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003838 || addr_type == ADDR_BUFFERS)
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01003839 lnum = compute_buffer_local_count(
3840 addr_type, lnum, (i == '-') ? -1 * n : n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003841 else
Bram Moolenaarded27822017-01-02 14:27:34 +01003842 {
3843#ifdef FEAT_FOLDING
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003844 // Relative line addressing, need to adjust for folded lines
3845 // now, but only do it after the first address.
Bram Moolenaarded27822017-01-02 14:27:34 +01003846 if (addr_type == ADDR_LINES && (i == '-' || i == '+')
3847 && address_count >= 2)
3848 (void)hasFolding(lnum, NULL, &lnum);
3849#endif
3850 if (i == '-')
3851 lnum -= n;
3852 else
3853 lnum += n;
3854 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003855 }
3856 } while (*cmd == '/' || *cmd == '?');
3857
3858error:
3859 *ptr = cmd;
3860 return lnum;
3861}
3862
3863/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00003864 * Get flags from an Ex command argument.
3865 */
3866 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003867get_flags(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00003868{
3869 while (vim_strchr((char_u *)"lp#", *eap->arg) != NULL)
3870 {
3871 if (*eap->arg == 'l')
3872 eap->flags |= EXFLAG_LIST;
3873 else if (*eap->arg == 'p')
3874 eap->flags |= EXFLAG_PRINT;
3875 else
3876 eap->flags |= EXFLAG_NR;
3877 eap->arg = skipwhite(eap->arg + 1);
3878 }
3879}
3880
3881/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003882 * Function called for command which is Not Implemented. NI!
3883 */
3884 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003885ex_ni(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003886{
3887 if (!eap->skip)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003888 eap->errmsg = N_("E319: Sorry, the command is not available in this version");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003889}
3890
Bram Moolenaar7bb75552007-07-16 18:39:49 +00003891#ifdef HAVE_EX_SCRIPT_NI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003892/*
3893 * Function called for script command which is Not Implemented. NI!
3894 * Skips over ":perl <<EOF" constructs.
3895 */
3896 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003897ex_script_ni(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003898{
3899 if (!eap->skip)
3900 ex_ni(eap);
3901 else
3902 vim_free(script_get(eap, eap->arg));
3903}
3904#endif
3905
3906/*
3907 * Check range in Ex command for validity.
3908 * Return NULL when valid, error message when invalid.
3909 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003910 static char *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003911invalid_range(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003912{
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003913 buf_T *buf;
Bram Moolenaar25190db2019-05-04 15:05:28 +02003914
Bram Moolenaar071d4272004-06-13 20:20:40 +00003915 if ( eap->line1 < 0
3916 || eap->line2 < 0
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003917 || eap->line1 > eap->line2)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003918 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003919
Bram Moolenaar8071cb22019-07-12 17:58:01 +02003920 if (eap->argt & EX_RANGE)
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003921 {
Bram Moolenaarb7316892019-05-01 18:08:42 +02003922 switch (eap->addr_type)
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003923 {
3924 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02003925 if (eap->line2 > curbuf->b_ml.ml_line_count
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003926#ifdef FEAT_DIFF
3927 + (eap->cmdidx == CMD_diffget)
3928#endif
3929 )
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003930 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003931 break;
3932 case ADDR_ARGUMENTS:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003933 // add 1 if ARGCOUNT is 0
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01003934 if (eap->line2 > ARGCOUNT + (!ARGCOUNT))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003935 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003936 break;
3937 case ADDR_BUFFERS:
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02003938 // Only a boundary check, not whether the buffers actually
3939 // exist.
3940 if (eap->line1 < 1 || eap->line2 > get_highest_fnum())
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003941 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003942 break;
3943 case ADDR_LOADED_BUFFERS:
3944 buf = firstbuf;
3945 while (buf->b_ml.ml_mfp == NULL)
3946 {
3947 if (buf->b_next == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003948 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003949 buf = buf->b_next;
3950 }
3951 if (eap->line1 < buf->b_fnum)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003952 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003953 buf = lastbuf;
3954 while (buf->b_ml.ml_mfp == NULL)
3955 {
3956 if (buf->b_prev == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003957 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003958 buf = buf->b_prev;
3959 }
3960 if (eap->line2 > buf->b_fnum)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003961 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003962 break;
3963 case ADDR_WINDOWS:
Bram Moolenaar8be63882015-01-14 11:25:05 +01003964 if (eap->line2 > LAST_WIN_NR)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003965 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003966 break;
3967 case ADDR_TABS:
3968 if (eap->line2 > LAST_TAB_NR)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003969 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003970 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01003971 case ADDR_TABS_RELATIVE:
Bram Moolenaarb7316892019-05-01 18:08:42 +02003972 case ADDR_OTHER:
3973 // Any range is OK.
Bram Moolenaar2f72c702017-01-29 14:48:10 +01003974 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003975 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003976#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02003977 // No error for value that is too big, will use the last entry.
3978 if (eap->line2 <= 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003979 return _(e_invrange);
Bram Moolenaare906c502015-09-09 21:10:39 +02003980#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003981 break;
Bram Moolenaar25190db2019-05-04 15:05:28 +02003982 case ADDR_QUICKFIX_VALID:
3983#ifdef FEAT_QUICKFIX
3984 if ((eap->line2 != 1 && eap->line2 > qf_get_valid_size(eap))
3985 || eap->line2 < 0)
3986 return _(e_invrange);
3987#endif
3988 break;
3989 case ADDR_UNSIGNED:
3990 if (eap->line2 < 0)
3991 return _(e_invrange);
3992 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02003993 case ADDR_NONE:
3994 // Will give an error elsewhere.
3995 break;
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003996 }
3997 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003998 return NULL;
3999}
4000
4001/*
4002 * Correct the range for zero line number, if required.
4003 */
4004 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004005correct_range(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004006{
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004007 if (!(eap->argt & EX_ZEROR)) // zero in range not allowed
Bram Moolenaar071d4272004-06-13 20:20:40 +00004008 {
4009 if (eap->line1 == 0)
4010 eap->line1 = 1;
4011 if (eap->line2 == 0)
4012 eap->line2 = 1;
4013 }
4014}
4015
Bram Moolenaar748bf032005-02-02 23:04:36 +00004016#ifdef FEAT_QUICKFIX
Bram Moolenaar748bf032005-02-02 23:04:36 +00004017/*
4018 * For a ":vimgrep" or ":vimgrepadd" command return a pointer past the
4019 * pattern. Otherwise return eap->arg.
4020 */
4021 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004022skip_grep_pat(exarg_T *eap)
Bram Moolenaar748bf032005-02-02 23:04:36 +00004023{
4024 char_u *p = eap->arg;
4025
Bram Moolenaara37420f2006-02-04 22:37:47 +00004026 if (*p != NUL && (eap->cmdidx == CMD_vimgrep || eap->cmdidx == CMD_lvimgrep
4027 || eap->cmdidx == CMD_vimgrepadd
4028 || eap->cmdidx == CMD_lvimgrepadd
4029 || grep_internal(eap->cmdidx)))
Bram Moolenaar748bf032005-02-02 23:04:36 +00004030 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00004031 p = skip_vimgrep_pat(p, NULL, NULL);
Bram Moolenaar748bf032005-02-02 23:04:36 +00004032 if (p == NULL)
4033 p = eap->arg;
Bram Moolenaar748bf032005-02-02 23:04:36 +00004034 }
4035 return p;
4036}
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004037
4038/*
4039 * For the ":make" and ":grep" commands insert the 'makeprg'/'grepprg' option
4040 * in the command line, so that things like % get expanded.
4041 */
4042 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004043replace_makeprg(exarg_T *eap, char_u *p, char_u **cmdlinep)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004044{
4045 char_u *new_cmdline;
4046 char_u *program;
4047 char_u *pos;
4048 char_u *ptr;
4049 int len;
4050 int i;
4051
4052 /*
4053 * Don't do it when ":vimgrep" is used for ":grep".
4054 */
Bram Moolenaara37420f2006-02-04 22:37:47 +00004055 if ((eap->cmdidx == CMD_make || eap->cmdidx == CMD_lmake
4056 || eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4057 || eap->cmdidx == CMD_grepadd
4058 || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004059 && !grep_internal(eap->cmdidx))
4060 {
Bram Moolenaara37420f2006-02-04 22:37:47 +00004061 if (eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4062 || eap->cmdidx == CMD_grepadd || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004063 {
4064 if (*curbuf->b_p_gp == NUL)
4065 program = p_gp;
4066 else
4067 program = curbuf->b_p_gp;
4068 }
4069 else
4070 {
4071 if (*curbuf->b_p_mp == NUL)
4072 program = p_mp;
4073 else
4074 program = curbuf->b_p_mp;
4075 }
4076
4077 p = skipwhite(p);
4078
4079 if ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4080 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004081 // replace $* by given arguments
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004082 i = 1;
4083 while ((pos = (char_u *)strstr((char *)pos + 2, "$*")) != NULL)
4084 ++i;
4085 len = (int)STRLEN(p);
Bram Moolenaar51e14382019-05-25 20:21:28 +02004086 new_cmdline = alloc(STRLEN(program) + i * (len - 2) + 1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004087 if (new_cmdline == NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004088 return NULL; // out of memory
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004089 ptr = new_cmdline;
4090 while ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4091 {
4092 i = (int)(pos - program);
4093 STRNCPY(ptr, program, i);
4094 STRCPY(ptr += i, p);
4095 ptr += len;
4096 program = pos + 2;
4097 }
4098 STRCPY(ptr, program);
4099 }
4100 else
4101 {
Bram Moolenaar51e14382019-05-25 20:21:28 +02004102 new_cmdline = alloc(STRLEN(program) + STRLEN(p) + 2);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004103 if (new_cmdline == NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004104 return NULL; // out of memory
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004105 STRCPY(new_cmdline, program);
4106 STRCAT(new_cmdline, " ");
4107 STRCAT(new_cmdline, p);
4108 }
4109 msg_make(p);
4110
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004111 // 'eap->cmd' is not set here, because it is not used at CMD_make
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004112 vim_free(*cmdlinep);
4113 *cmdlinep = new_cmdline;
4114 p = new_cmdline;
4115 }
4116 return p;
4117}
Bram Moolenaar748bf032005-02-02 23:04:36 +00004118#endif
4119
Bram Moolenaar071d4272004-06-13 20:20:40 +00004120/*
4121 * Expand file name in Ex command argument.
Bram Moolenaar0abb4272019-06-15 16:06:00 +02004122 * When an error is detected, "errormsgp" is set to a non-NULL pointer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004123 * Return FAIL for failure, OK otherwise.
4124 */
4125 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004126expand_filename(
4127 exarg_T *eap,
4128 char_u **cmdlinep,
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004129 char **errormsgp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004130{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004131 int has_wildcards; // need to expand wildcards
Bram Moolenaar071d4272004-06-13 20:20:40 +00004132 char_u *repl;
4133 int srclen;
4134 char_u *p;
4135 int n;
Bram Moolenaar63b92542007-03-27 14:57:09 +00004136 int escaped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004137
Bram Moolenaar748bf032005-02-02 23:04:36 +00004138#ifdef FEAT_QUICKFIX
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004139 // Skip a regexp pattern for ":vimgrep[add] pat file..."
Bram Moolenaar748bf032005-02-02 23:04:36 +00004140 p = skip_grep_pat(eap);
4141#else
4142 p = eap->arg;
4143#endif
4144
Bram Moolenaar071d4272004-06-13 20:20:40 +00004145 /*
4146 * Decide to expand wildcards *before* replacing '%', '#', etc. If
4147 * the file name contains a wildcard it should not cause expanding.
4148 * (it will be expanded anyway if there is a wildcard before replacing).
4149 */
Bram Moolenaar748bf032005-02-02 23:04:36 +00004150 has_wildcards = mch_has_wildcard(p);
4151 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004152 {
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004153#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004154 // Skip over `=expr`, wildcards in it are not expanded.
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004155 if (p[0] == '`' && p[1] == '=')
4156 {
4157 p += 2;
4158 (void)skip_expr(&p);
4159 if (*p == '`')
4160 ++p;
4161 continue;
4162 }
4163#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004164 /*
4165 * Quick check if this cannot be the start of a special string.
4166 * Also removes backslash before '%', '#' and '<'.
4167 */
4168 if (vim_strchr((char_u *)"%#<", *p) == NULL)
4169 {
4170 ++p;
4171 continue;
4172 }
4173
4174 /*
4175 * Try to find a match at this position.
4176 */
Bram Moolenaar63b92542007-03-27 14:57:09 +00004177 repl = eval_vars(p, eap->arg, &srclen, &(eap->do_ecmd_lnum),
4178 errormsgp, &escaped);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004179 if (*errormsgp != NULL) // error detected
Bram Moolenaar071d4272004-06-13 20:20:40 +00004180 return FAIL;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004181 if (repl == NULL) // no match found
Bram Moolenaar071d4272004-06-13 20:20:40 +00004182 {
4183 p += srclen;
4184 continue;
4185 }
4186
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004187 // Wildcards won't be expanded below, the replacement is taken
4188 // literally. But do expand "~/file", "~user/file" and "$HOME/file".
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00004189 if (vim_strchr(repl, '$') != NULL || vim_strchr(repl, '~') != NULL)
4190 {
4191 char_u *l = repl;
4192
4193 repl = expand_env_save(repl);
4194 vim_free(l);
4195 }
4196
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004197 // Need to escape white space et al. with a backslash.
4198 // Don't do this for:
4199 // - replacement that already has been escaped: "##"
4200 // - shell commands (may have to use quotes instead).
4201 // - non-unix systems when there is a single argument (spaces don't
4202 // separate arguments then).
Bram Moolenaar071d4272004-06-13 20:20:40 +00004203 if (!eap->usefilter
Bram Moolenaar63b92542007-03-27 14:57:09 +00004204 && !escaped
Bram Moolenaar071d4272004-06-13 20:20:40 +00004205 && eap->cmdidx != CMD_bang
Bram Moolenaar071d4272004-06-13 20:20:40 +00004206 && eap->cmdidx != CMD_grep
4207 && eap->cmdidx != CMD_grepadd
Bram Moolenaarbf15b8d2017-06-04 20:43:48 +02004208 && eap->cmdidx != CMD_hardcopy
Bram Moolenaar67883b42017-07-27 22:57:00 +02004209 && eap->cmdidx != CMD_lgrep
4210 && eap->cmdidx != CMD_lgrepadd
4211 && eap->cmdidx != CMD_lmake
4212 && eap->cmdidx != CMD_make
4213 && eap->cmdidx != CMD_terminal
Bram Moolenaar071d4272004-06-13 20:20:40 +00004214#ifndef UNIX
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004215 && !(eap->argt & EX_NOSPC)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004216#endif
4217 )
4218 {
4219 char_u *l;
4220#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004221 // Don't escape a backslash here, because rem_backslash() doesn't
4222 // remove it later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004223 static char_u *nobslash = (char_u *)" \t\"|";
4224# define ESCAPE_CHARS nobslash
4225#else
4226# define ESCAPE_CHARS escape_chars
4227#endif
4228
4229 for (l = repl; *l; ++l)
4230 if (vim_strchr(ESCAPE_CHARS, *l) != NULL)
4231 {
4232 l = vim_strsave_escaped(repl, ESCAPE_CHARS);
4233 if (l != NULL)
4234 {
4235 vim_free(repl);
4236 repl = l;
4237 }
4238 break;
4239 }
4240 }
4241
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004242 // For a shell command a '!' must be escaped.
Bram Moolenaar67883b42017-07-27 22:57:00 +02004243 if ((eap->usefilter || eap->cmdidx == CMD_bang
4244 || eap->cmdidx == CMD_terminal)
Bram Moolenaar31b7d382014-04-01 18:54:48 +02004245 && vim_strpbrk(repl, (char_u *)"!") != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004246 {
4247 char_u *l;
4248
Bram Moolenaar31b7d382014-04-01 18:54:48 +02004249 l = vim_strsave_escaped(repl, (char_u *)"!");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004250 if (l != NULL)
4251 {
4252 vim_free(repl);
4253 repl = l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004254 }
4255 }
4256
4257 p = repl_cmdline(eap, p, srclen, repl, cmdlinep);
4258 vim_free(repl);
4259 if (p == NULL)
4260 return FAIL;
4261 }
4262
4263 /*
4264 * One file argument: Expand wildcards.
4265 * Don't do this with ":r !command" or ":w !command".
4266 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004267 if ((eap->argt & EX_NOSPC) && !eap->usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004268 {
4269 /*
4270 * May do this twice:
4271 * 1. Replace environment variables.
4272 * 2. Replace any other wildcards, remove backslashes.
4273 */
4274 for (n = 1; n <= 2; ++n)
4275 {
4276 if (n == 2)
4277 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004278 /*
4279 * Halve the number of backslashes (this is Vi compatible).
4280 * For Unix and OS/2, when wildcards are expanded, this is
4281 * done by ExpandOne() below.
4282 */
Bram Moolenaare7fedb62015-12-31 19:07:19 +01004283#if defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004284 if (!has_wildcards)
4285#endif
4286 backslash_halve(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004287 }
4288
4289 if (has_wildcards)
4290 {
4291 if (n == 1)
4292 {
4293 /*
4294 * First loop: May expand environment variables. This
4295 * can be done much faster with expand_env() than with
4296 * something else (e.g., calling a shell).
4297 * After expanding environment variables, check again
4298 * if there are still wildcards present.
4299 */
4300 if (vim_strchr(eap->arg, '$') != NULL
4301 || vim_strchr(eap->arg, '~') != NULL)
4302 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +00004303 expand_env_esc(eap->arg, NameBuff, MAXPATHL,
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00004304 TRUE, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004305 has_wildcards = mch_has_wildcard(NameBuff);
4306 p = NameBuff;
4307 }
4308 else
4309 p = NULL;
4310 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004311 else // n == 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00004312 {
4313 expand_T xpc;
Bram Moolenaarb40c2572019-10-19 21:01:05 +02004314 int options = WILD_LIST_NOTFOUND
4315 | WILD_NOERROR | WILD_ADD_SLASH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004316
4317 ExpandInit(&xpc);
4318 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar94950a92010-12-02 16:01:29 +01004319 if (p_wic)
4320 options += WILD_ICASE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004321 p = ExpandOne(&xpc, eap->arg, NULL,
Bram Moolenaar94950a92010-12-02 16:01:29 +01004322 options, WILD_EXPAND_FREE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004323 if (p == NULL)
4324 return FAIL;
4325 }
4326 if (p != NULL)
4327 {
4328 (void)repl_cmdline(eap, eap->arg, (int)STRLEN(eap->arg),
4329 p, cmdlinep);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004330 if (n == 2) // p came from ExpandOne()
Bram Moolenaar071d4272004-06-13 20:20:40 +00004331 vim_free(p);
4332 }
4333 }
4334 }
4335 }
4336 return OK;
4337}
4338
4339/*
4340 * Replace part of the command line, keeping eap->cmd, eap->arg and
4341 * eap->nextcmd correct.
4342 * "src" points to the part that is to be replaced, of length "srclen".
4343 * "repl" is the replacement string.
4344 * Returns a pointer to the character after the replaced string.
4345 * Returns NULL for failure.
4346 */
4347 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004348repl_cmdline(
4349 exarg_T *eap,
4350 char_u *src,
4351 int srclen,
4352 char_u *repl,
4353 char_u **cmdlinep)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004354{
4355 int len;
4356 int i;
4357 char_u *new_cmdline;
4358
4359 /*
4360 * The new command line is build in new_cmdline[].
4361 * First allocate it.
4362 * Careful: a "+cmd" argument may have been NUL terminated.
4363 */
4364 len = (int)STRLEN(repl);
4365 i = (int)(src - *cmdlinep) + (int)STRLEN(src + srclen) + len + 3;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004366 if (eap->nextcmd != NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004367 i += (int)STRLEN(eap->nextcmd);// add space for next command
Bram Moolenaar964b3742019-05-24 18:54:09 +02004368 if ((new_cmdline = alloc(i)) == NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004369 return NULL; // out of memory!
Bram Moolenaar071d4272004-06-13 20:20:40 +00004370
4371 /*
4372 * Copy the stuff before the expanded part.
4373 * Copy the expanded stuff.
4374 * Copy what came after the expanded part.
4375 * Copy the next commands, if there are any.
4376 */
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004377 i = (int)(src - *cmdlinep); // length of part before match
Bram Moolenaar071d4272004-06-13 20:20:40 +00004378 mch_memmove(new_cmdline, *cmdlinep, (size_t)i);
Bram Moolenaara3ffd9c2005-07-21 21:03:15 +00004379
Bram Moolenaar071d4272004-06-13 20:20:40 +00004380 mch_memmove(new_cmdline + i, repl, (size_t)len);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004381 i += len; // remember the end of the string
Bram Moolenaar071d4272004-06-13 20:20:40 +00004382 STRCPY(new_cmdline + i, src + srclen);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004383 src = new_cmdline + i; // remember where to continue
Bram Moolenaar071d4272004-06-13 20:20:40 +00004384
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004385 if (eap->nextcmd != NULL) // append next command
Bram Moolenaar071d4272004-06-13 20:20:40 +00004386 {
4387 i = (int)STRLEN(new_cmdline) + 1;
4388 STRCPY(new_cmdline + i, eap->nextcmd);
4389 eap->nextcmd = new_cmdline + i;
4390 }
4391 eap->cmd = new_cmdline + (eap->cmd - *cmdlinep);
4392 eap->arg = new_cmdline + (eap->arg - *cmdlinep);
4393 if (eap->do_ecmd_cmd != NULL && eap->do_ecmd_cmd != dollar_command)
4394 eap->do_ecmd_cmd = new_cmdline + (eap->do_ecmd_cmd - *cmdlinep);
4395 vim_free(*cmdlinep);
4396 *cmdlinep = new_cmdline;
4397
4398 return src;
4399}
4400
4401/*
4402 * Check for '|' to separate commands and '"' to start comments.
4403 */
4404 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004405separate_nextcmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004406{
4407 char_u *p;
4408
Bram Moolenaar86b68352004-12-27 21:59:20 +00004409#ifdef FEAT_QUICKFIX
Bram Moolenaar748bf032005-02-02 23:04:36 +00004410 p = skip_grep_pat(eap);
4411#else
4412 p = eap->arg;
Bram Moolenaar86b68352004-12-27 21:59:20 +00004413#endif
4414
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004415 for ( ; *p; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004416 {
4417 if (*p == Ctrl_V)
4418 {
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004419 if (eap->argt & (EX_CTRLV | EX_XFILE))
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004420 ++p; // skip CTRL-V and next char
Bram Moolenaar071d4272004-06-13 20:20:40 +00004421 else
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004422 // remove CTRL-V and skip next char
Bram Moolenaara7241f52008-06-24 20:39:31 +00004423 STRMOVE(p, p + 1);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004424 if (*p == NUL) // stop at NUL after CTRL-V
Bram Moolenaar071d4272004-06-13 20:20:40 +00004425 break;
4426 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004427
4428#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004429 // Skip over `=expr` when wildcards are expanded.
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004430 else if (p[0] == '`' && p[1] == '=' && (eap->argt & EX_XFILE))
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004431 {
4432 p += 2;
4433 (void)skip_expr(&p);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004434 }
4435#endif
4436
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004437 // Check for '"': start of comment or '|': next command
4438 // :@" and :*" do not start a comment!
4439 // :redir @" doesn't either.
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004440 else if ((*p == '"' && !(eap->argt & EX_NOTRLCOM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004441 && ((eap->cmdidx != CMD_at && eap->cmdidx != CMD_star)
4442 || p != eap->arg)
4443 && (eap->cmdidx != CMD_redir
4444 || p != eap->arg + 1 || p[-1] != '@'))
Bram Moolenaara26b9702020-04-18 19:53:28 +02004445#ifdef FEAT_EVAL
4446 || (*p == '#' && in_vim9script()
4447 && p[1] != '{' && p > eap->cmd && VIM_ISWHITE(p[-1]))
4448#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004449 || *p == '|' || *p == '\n')
4450 {
4451 /*
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004452 * We remove the '\' before the '|', unless EX_CTRLV is used
Bram Moolenaar071d4272004-06-13 20:20:40 +00004453 * AND 'b' is present in 'cpoptions'.
4454 */
4455 if ((vim_strchr(p_cpo, CPO_BAR) == NULL
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004456 || !(eap->argt & EX_CTRLV)) && *(p - 1) == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004457 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004458 STRMOVE(p - 1, p); // remove the '\'
Bram Moolenaar071d4272004-06-13 20:20:40 +00004459 --p;
4460 }
4461 else
4462 {
4463 eap->nextcmd = check_nextcmd(p);
4464 *p = NUL;
4465 break;
4466 }
4467 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004468 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00004469
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004470 if (!(eap->argt & EX_NOTRLCOM)) // remove trailing spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00004471 del_trailing_spaces(eap->arg);
4472}
4473
4474/*
4475 * get + command from ex argument
4476 */
4477 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004478getargcmd(char_u **argp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004479{
4480 char_u *arg = *argp;
4481 char_u *command = NULL;
4482
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004483 if (*arg == '+') // +[command]
Bram Moolenaar071d4272004-06-13 20:20:40 +00004484 {
4485 ++arg;
Bram Moolenaar3e451592014-04-02 14:22:05 +02004486 if (vim_isspace(*arg) || *arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004487 command = dollar_command;
4488 else
4489 {
4490 command = arg;
4491 arg = skip_cmd_arg(command, TRUE);
4492 if (*arg != NUL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004493 *arg++ = NUL; // terminate command with NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004494 }
4495
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004496 arg = skipwhite(arg); // skip over spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00004497 *argp = arg;
4498 }
4499 return command;
4500}
4501
4502/*
4503 * Find end of "+command" argument. Skip over "\ " and "\\".
4504 */
Bram Moolenaard0190392019-08-23 21:17:35 +02004505 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004506skip_cmd_arg(
4507 char_u *p,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004508 int rembs) // TRUE to halve the number of backslashes
Bram Moolenaar071d4272004-06-13 20:20:40 +00004509{
4510 while (*p && !vim_isspace(*p))
4511 {
4512 if (*p == '\\' && p[1] != NUL)
4513 {
4514 if (rembs)
Bram Moolenaara7241f52008-06-24 20:39:31 +00004515 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004516 else
4517 ++p;
4518 }
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004519 MB_PTR_ADV(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004520 }
4521 return p;
4522}
4523
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004524 int
4525get_bad_opt(char_u *p, exarg_T *eap)
4526{
4527 if (STRICMP(p, "keep") == 0)
4528 eap->bad_char = BAD_KEEP;
4529 else if (STRICMP(p, "drop") == 0)
4530 eap->bad_char = BAD_DROP;
4531 else if (MB_BYTE2LEN(*p) == 1 && p[1] == NUL)
4532 eap->bad_char = *p;
Bram Moolenaar75808492018-06-12 12:39:41 +02004533 else
4534 return FAIL;
4535 return OK;
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004536}
4537
Bram Moolenaar071d4272004-06-13 20:20:40 +00004538/*
4539 * Get "++opt=arg" argument.
4540 * Return FAIL or OK.
4541 */
4542 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004543getargopt(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004544{
4545 char_u *arg = eap->arg + 2;
4546 int *pp = NULL;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02004547 int bad_char_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004548 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004549
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004550 // ":edit ++[no]bin[ary] file"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004551 if (STRNCMP(arg, "bin", 3) == 0 || STRNCMP(arg, "nobin", 5) == 0)
4552 {
4553 if (*arg == 'n')
4554 {
4555 arg += 2;
4556 eap->force_bin = FORCE_NOBIN;
4557 }
4558 else
4559 eap->force_bin = FORCE_BIN;
4560 if (!checkforcmd(&arg, "binary", 3))
4561 return FAIL;
4562 eap->arg = skipwhite(arg);
4563 return OK;
4564 }
4565
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004566 // ":read ++edit file"
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004567 if (STRNCMP(arg, "edit", 4) == 0)
4568 {
4569 eap->read_edit = TRUE;
4570 eap->arg = skipwhite(arg + 4);
4571 return OK;
4572 }
4573
Bram Moolenaar071d4272004-06-13 20:20:40 +00004574 if (STRNCMP(arg, "ff", 2) == 0)
4575 {
4576 arg += 2;
4577 pp = &eap->force_ff;
4578 }
4579 else if (STRNCMP(arg, "fileformat", 10) == 0)
4580 {
4581 arg += 10;
4582 pp = &eap->force_ff;
4583 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004584 else if (STRNCMP(arg, "enc", 3) == 0)
4585 {
Bram Moolenaarb38e9ab2011-12-14 14:49:45 +01004586 if (STRNCMP(arg, "encoding", 8) == 0)
4587 arg += 8;
4588 else
4589 arg += 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004590 pp = &eap->force_enc;
4591 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004592 else if (STRNCMP(arg, "bad", 3) == 0)
4593 {
4594 arg += 3;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02004595 pp = &bad_char_idx;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004596 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004597
4598 if (pp == NULL || *arg != '=')
4599 return FAIL;
4600
4601 ++arg;
4602 *pp = (int)(arg - eap->cmd);
4603 arg = skip_cmd_arg(arg, FALSE);
4604 eap->arg = skipwhite(arg);
4605 *arg = NUL;
4606
Bram Moolenaar071d4272004-06-13 20:20:40 +00004607 if (pp == &eap->force_ff)
4608 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004609 if (check_ff_value(eap->cmd + eap->force_ff) == FAIL)
4610 return FAIL;
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004611 eap->force_ff = eap->cmd[eap->force_ff];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004612 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004613 else if (pp == &eap->force_enc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004614 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004615 // Make 'fileencoding' lower case.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004616 for (p = eap->cmd + eap->force_enc; *p != NUL; ++p)
4617 *p = TOLOWER_ASC(*p);
4618 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004619 else
4620 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004621 // Check ++bad= argument. Must be a single-byte character, "keep" or
4622 // "drop".
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004623 if (get_bad_opt(eap->cmd + bad_char_idx, eap) == FAIL)
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004624 return FAIL;
4625 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004626
4627 return OK;
4628}
4629
Bram Moolenaar071d4272004-06-13 20:20:40 +00004630 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004631ex_autocmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004632{
4633 /*
Bram Moolenaar26d4b892018-07-04 22:26:28 +02004634 * Disallow autocommands from .exrc and .vimrc in current
Bram Moolenaar071d4272004-06-13 20:20:40 +00004635 * directory for security reasons.
4636 */
4637 if (secure)
4638 {
4639 secure = 2;
4640 eap->errmsg = e_curdir;
4641 }
4642 else if (eap->cmdidx == CMD_autocmd)
4643 do_autocmd(eap->arg, eap->forceit);
4644 else
4645 do_augroup(eap->arg, eap->forceit);
4646}
4647
4648/*
4649 * ":doautocmd": Apply the automatic commands to the current buffer.
4650 */
4651 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004652ex_doautocmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004653{
Bram Moolenaar60542ac2012-02-12 20:14:01 +01004654 char_u *arg = eap->arg;
4655 int call_do_modelines = check_nomodeline(&arg);
Bram Moolenaar1610d052016-06-09 22:53:01 +02004656 int did_aucmd;
Bram Moolenaar60542ac2012-02-12 20:14:01 +01004657
Bram Moolenaar1610d052016-06-09 22:53:01 +02004658 (void)do_doautocmd(arg, TRUE, &did_aucmd);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004659 // Only when there is no <nomodeline>.
Bram Moolenaar1610d052016-06-09 22:53:01 +02004660 if (call_do_modelines && did_aucmd)
Bram Moolenaar60542ac2012-02-12 20:14:01 +01004661 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004662}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004663
Bram Moolenaar071d4272004-06-13 20:20:40 +00004664/*
4665 * :[N]bunload[!] [N] [bufname] unload buffer
4666 * :[N]bdelete[!] [N] [bufname] delete buffer from buffer list
4667 * :[N]bwipeout[!] [N] [bufname] delete buffer really
4668 */
4669 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004670ex_bunload(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004671{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01004672 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02004673 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004674 eap->errmsg = do_bufdel(
4675 eap->cmdidx == CMD_bdelete ? DOBUF_DEL
4676 : eap->cmdidx == CMD_bwipeout ? DOBUF_WIPE
4677 : DOBUF_UNLOAD, eap->arg,
4678 eap->addr_count, (int)eap->line1, (int)eap->line2, eap->forceit);
4679}
4680
4681/*
4682 * :[N]buffer [N] to buffer N
4683 * :[N]sbuffer [N] to buffer N
4684 */
4685 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004686ex_buffer(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004687{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01004688 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02004689 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004690 if (*eap->arg)
4691 eap->errmsg = e_trailing;
4692 else
4693 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004694 if (eap->addr_count == 0) // default is current buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00004695 goto_buffer(eap, DOBUF_CURRENT, FORWARD, 0);
4696 else
4697 goto_buffer(eap, DOBUF_FIRST, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02004698 if (eap->do_ecmd_cmd != NULL)
4699 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004700 }
4701}
4702
4703/*
4704 * :[N]bmodified [N] to next mod. buffer
4705 * :[N]sbmodified [N] to next mod. buffer
4706 */
4707 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004708ex_bmodified(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004709{
4710 goto_buffer(eap, DOBUF_MOD, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02004711 if (eap->do_ecmd_cmd != NULL)
4712 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004713}
4714
4715/*
4716 * :[N]bnext [N] to next buffer
4717 * :[N]sbnext [N] split and to next buffer
4718 */
4719 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004720ex_bnext(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004721{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01004722 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar3029bcc2020-01-18 15:06:19 +01004723 return;
4724
Bram Moolenaar071d4272004-06-13 20:20:40 +00004725 goto_buffer(eap, DOBUF_CURRENT, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02004726 if (eap->do_ecmd_cmd != NULL)
4727 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004728}
4729
4730/*
4731 * :[N]bNext [N] to previous buffer
4732 * :[N]bprevious [N] to previous buffer
4733 * :[N]sbNext [N] split and to previous buffer
4734 * :[N]sbprevious [N] split and to previous buffer
4735 */
4736 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004737ex_bprevious(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004738{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01004739 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar3029bcc2020-01-18 15:06:19 +01004740 return;
4741
Bram Moolenaar071d4272004-06-13 20:20:40 +00004742 goto_buffer(eap, DOBUF_CURRENT, BACKWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02004743 if (eap->do_ecmd_cmd != NULL)
4744 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004745}
4746
4747/*
4748 * :brewind to first buffer
4749 * :bfirst to first buffer
4750 * :sbrewind split and to first buffer
4751 * :sbfirst split and to first buffer
4752 */
4753 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004754ex_brewind(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004755{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01004756 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar3029bcc2020-01-18 15:06:19 +01004757 return;
4758
Bram Moolenaar071d4272004-06-13 20:20:40 +00004759 goto_buffer(eap, DOBUF_FIRST, FORWARD, 0);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02004760 if (eap->do_ecmd_cmd != NULL)
4761 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004762}
4763
4764/*
4765 * :blast to last buffer
4766 * :sblast split and to last buffer
4767 */
4768 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004769ex_blast(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004770{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01004771 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar3029bcc2020-01-18 15:06:19 +01004772 return;
4773
Bram Moolenaar071d4272004-06-13 20:20:40 +00004774 goto_buffer(eap, DOBUF_LAST, BACKWARD, 0);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02004775 if (eap->do_ecmd_cmd != NULL)
4776 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004777}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004778
Bram Moolenaar7a092242020-04-16 22:10:49 +02004779/*
4780 * Check if "c" ends an Ex command.
Bram Moolenaara494f562020-04-18 17:45:38 +02004781 * In Vim9 script does not check for white space before # or #{.
Bram Moolenaar7a092242020-04-16 22:10:49 +02004782 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004783 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004784ends_excmd(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004785{
Bram Moolenaarfbda69b2020-04-13 15:06:53 +02004786#ifdef FEAT_EVAL
Bram Moolenaar2c330432020-04-13 14:41:35 +02004787 if (c == '#')
Bram Moolenaar7a092242020-04-16 22:10:49 +02004788 return in_vim9script();
4789#endif
4790 return (c == NUL || c == '|' || c == '"' || c == '\n');
4791}
4792
4793/*
4794 * Like ends_excmd() but checks that a # in Vim9 script either has "cmd" equal
4795 * to "cmd_start" or has a white space character before it.
4796 */
4797 int
Bram Moolenaar21cfe502020-04-16 23:01:50 +02004798ends_excmd2(char_u *cmd_start UNUSED, char_u *cmd)
Bram Moolenaar7a092242020-04-16 22:10:49 +02004799{
4800 int c = *cmd;
4801
4802#ifdef FEAT_EVAL
Bram Moolenaara26b9702020-04-18 19:53:28 +02004803 if (c == '#' && cmd[1] != '{' && (cmd == cmd_start || VIM_ISWHITE(cmd[-1])))
Bram Moolenaar2c330432020-04-13 14:41:35 +02004804 return in_vim9script();
Bram Moolenaarfbda69b2020-04-13 15:06:53 +02004805#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004806 return (c == NUL || c == '|' || c == '"' || c == '\n');
4807}
4808
4809#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA) || defined(FEAT_EVAL) \
4810 || defined(PROTO)
4811/*
4812 * Return the next command, after the first '|' or '\n'.
4813 * Return NULL if not found.
4814 */
4815 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004816find_nextcmd(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004817{
4818 while (*p != '|' && *p != '\n')
4819 {
4820 if (*p == NUL)
4821 return NULL;
4822 ++p;
4823 }
4824 return (p + 1);
4825}
4826#endif
4827
4828/*
Bram Moolenaar2256c992016-11-15 21:17:07 +01004829 * Check if *p is a separator between Ex commands, skipping over white space.
4830 * Return NULL if it isn't, the following character if it is.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004831 */
4832 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004833check_nextcmd(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004834{
Bram Moolenaar2256c992016-11-15 21:17:07 +01004835 char_u *s = skipwhite(p);
4836
4837 if (*s == '|' || *s == '\n')
4838 return (s + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004839 else
4840 return NULL;
4841}
4842
4843/*
4844 * - if there are more files to edit
4845 * - and this is the last window
4846 * - and forceit not used
4847 * - and not repeated twice on a row
4848 * return FAIL and give error message if 'message' TRUE
4849 * return OK otherwise
4850 */
4851 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004852check_more(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004853 int message, // when FALSE check only, no messages
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004854 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004855{
4856 int n = ARGCOUNT - curwin->w_arg_idx - 1;
4857
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00004858 if (!forceit && only_one_window()
4859 && ARGCOUNT > 1 && !arg_had_last && n >= 0 && quitmore == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004860 {
4861 if (message)
4862 {
4863#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
4864 if ((p_confirm || cmdmod.confirm) && curbuf->b_fname != NULL)
4865 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02004866 char_u buff[DIALOG_MSG_SIZE];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004867
Bram Moolenaarda6e8912018-08-21 15:12:14 +02004868 vim_snprintf((char *)buff, DIALOG_MSG_SIZE,
4869 NGETTEXT("%d more file to edit. Quit anyway?",
4870 "%d more files to edit. Quit anyway?", n), n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004871 if (vim_dialog_yesno(VIM_QUESTION, NULL, buff, 1) == VIM_YES)
4872 return OK;
4873 return FAIL;
4874 }
4875#endif
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01004876 semsg(NGETTEXT("E173: %d more file to edit",
4877 "E173: %d more files to edit", n), n);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004878 quitmore = 2; // next try to quit is allowed
Bram Moolenaar071d4272004-06-13 20:20:40 +00004879 }
4880 return FAIL;
4881 }
4882 return OK;
4883}
4884
Bram Moolenaar071d4272004-06-13 20:20:40 +00004885/*
4886 * Function given to ExpandGeneric() to obtain the list of command names.
4887 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004888 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004889get_command_name(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004890{
4891 if (idx >= (int)CMD_SIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004892 return get_user_command_name(idx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004893 return cmdnames[idx].cmd_name;
4894}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004895
Bram Moolenaar071d4272004-06-13 20:20:40 +00004896 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004897ex_colorscheme(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004898{
Bram Moolenaare6850792010-05-14 15:28:44 +02004899 if (*eap->arg == NUL)
4900 {
4901#ifdef FEAT_EVAL
4902 char_u *expr = vim_strsave((char_u *)"g:colors_name");
4903 char_u *p = NULL;
4904
4905 if (expr != NULL)
4906 {
4907 ++emsg_off;
4908 p = eval_to_string(expr, NULL, FALSE);
4909 --emsg_off;
4910 vim_free(expr);
4911 }
4912 if (p != NULL)
4913 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01004914 msg((char *)p);
Bram Moolenaare6850792010-05-14 15:28:44 +02004915 vim_free(p);
4916 }
4917 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01004918 msg("default");
Bram Moolenaare6850792010-05-14 15:28:44 +02004919#else
Bram Moolenaar32526b32019-01-19 17:43:09 +01004920 msg(_("unknown"));
Bram Moolenaare6850792010-05-14 15:28:44 +02004921#endif
4922 }
4923 else if (load_colors(eap->arg) == FAIL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004924 semsg(_("E185: Cannot find color scheme '%s'"), eap->arg);
Bram Moolenaarf58d81a2019-01-28 20:19:05 +01004925
4926#ifdef FEAT_VTP
4927 else if (has_vtp_working())
4928 {
4929 // background color change requires clear + redraw
4930 update_screen(CLEAR);
4931 redrawcmd();
4932 }
4933#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004934}
4935
4936 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004937ex_highlight(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004938{
4939 if (*eap->arg == NUL && eap->cmd[2] == '!')
Bram Moolenaar32526b32019-01-19 17:43:09 +01004940 msg(_("Greetings, Vim user!"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004941 do_highlight(eap->arg, eap->forceit, FALSE);
4942}
4943
4944
4945/*
4946 * Call this function if we thought we were going to exit, but we won't
4947 * (because of an error). May need to restore the terminal mode.
4948 */
4949 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004950not_exiting(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004951{
4952 exiting = FALSE;
4953 settmode(TMODE_RAW);
4954}
4955
Bram Moolenaar12a96de2018-03-11 14:44:18 +01004956 static int
4957before_quit_autocmds(win_T *wp, int quit_all, int forceit)
4958{
4959 apply_autocmds(EVENT_QUITPRE, NULL, NULL, FALSE, wp->w_buffer);
4960
Bram Moolenaar34ba06b2019-10-20 22:27:10 +02004961 // Bail out when autocommands closed the window.
4962 // Refuse to quit when the buffer in the last window is being closed (can
4963 // only happen in autocommands).
Bram Moolenaar12a96de2018-03-11 14:44:18 +01004964 if (!win_valid(wp)
4965 || curbuf_locked()
4966 || (wp->w_buffer->b_nwindows == 1 && wp->w_buffer->b_locked > 0))
4967 return TRUE;
4968
4969 if (quit_all || (check_more(FALSE, forceit) == OK && only_one_window()))
4970 {
4971 apply_autocmds(EVENT_EXITPRE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar34ba06b2019-10-20 22:27:10 +02004972 // Refuse to quit when locked or when the window was closed or the
4973 // buffer in the last window is being closed (can only happen in
4974 // autocommands).
4975 if (!win_valid(wp) || curbuf_locked()
Bram Moolenaar12a96de2018-03-11 14:44:18 +01004976 || (curbuf->b_nwindows == 1 && curbuf->b_locked > 0))
4977 return TRUE;
4978 }
4979
4980 return FALSE;
4981}
4982
Bram Moolenaar071d4272004-06-13 20:20:40 +00004983/*
Bram Moolenaarf240e182014-11-27 18:33:02 +01004984 * ":quit": quit current window, quit Vim if the last window is closed.
Bram Moolenaar12a96de2018-03-11 14:44:18 +01004985 * ":{nr}quit": quit window {nr}
Bram Moolenaar4d14bac2019-10-20 21:15:15 +02004986 * Also used when closing a terminal window that's the last one.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004987 */
Bram Moolenaar4d14bac2019-10-20 21:15:15 +02004988 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004989ex_quit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004990{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004991 win_T *wp;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004992
Bram Moolenaar071d4272004-06-13 20:20:40 +00004993#ifdef FEAT_CMDWIN
4994 if (cmdwin_type != 0)
4995 {
4996 cmdwin_result = Ctrl_C;
4997 return;
4998 }
4999#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005000 // Don't quit while editing the command line.
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005001 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005002 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005003 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005004 return;
5005 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005006 if (eap->addr_count > 0)
5007 {
Bram Moolenaarf240e182014-11-27 18:33:02 +01005008 int wnr = eap->line2;
5009
5010 for (wp = firstwin; wp->w_next != NULL; wp = wp->w_next)
5011 if (--wnr <= 0)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005012 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005013 }
5014 else
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005015 wp = curwin;
Bram Moolenaarf240e182014-11-27 18:33:02 +01005016
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005017 // Refuse to quit when locked.
Bram Moolenaar87ffb5c2017-10-19 12:37:42 +02005018 if (curbuf_locked())
5019 return;
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005020
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005021 // Trigger QuitPre and maybe ExitPre
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005022 if (before_quit_autocmds(wp, FALSE, eap->forceit))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005023 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005024
5025#ifdef FEAT_NETBEANS_INTG
5026 netbeansForcedQuit = eap->forceit;
5027#endif
5028
5029 /*
5030 * If there are more files or windows we won't exit.
5031 */
5032 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
5033 exiting = TRUE;
Bram Moolenaarff930ca2017-10-19 17:12:10 +02005034 if ((!buf_hide(wp->w_buffer)
5035 && check_changed(wp->w_buffer, (p_awa ? CCGD_AW : 0)
Bram Moolenaar45d3b142013-11-09 03:31:51 +01005036 | (eap->forceit ? CCGD_FORCEIT : 0)
5037 | CCGD_EXCMD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005038 || check_more(TRUE, eap->forceit) == FAIL
Bram Moolenaar027387f2016-01-02 22:25:52 +01005039 || (only_one_window() && check_changed_any(eap->forceit, TRUE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005040 {
5041 not_exiting();
5042 }
5043 else
5044 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005045 // quit last window
5046 // Note: only_one_window() returns true, even so a help window is
5047 // still open. In that case only quit, if no address has been
5048 // specified. Example:
5049 // :h|wincmd w|1q - don't quit
5050 // :h|wincmd w|q - quit
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01005051 if (only_one_window() && (ONE_WINDOW || eap->addr_count == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005052 getout(0);
Bram Moolenaar2c33d7b2017-10-14 16:06:20 +02005053 not_exiting();
Bram Moolenaar4033c552017-09-16 20:54:51 +02005054#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00005055 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005056#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005057 // close window; may free buffer
Bram Moolenaareb44a682017-08-03 22:44:55 +02005058 win_close(wp, !buf_hide(wp->w_buffer) || eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005059 }
5060}
5061
5062/*
5063 * ":cquit".
5064 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005065 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005066ex_cquit(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005067{
Bram Moolenaar1860bde2020-01-06 21:47:21 +01005068 // this does not always pass on the exit code to the Manx compiler. why?
5069 getout(eap->addr_count > 0 ? (int)eap->line2 : EXIT_FAILURE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005070}
5071
5072/*
5073 * ":qall": try to quit all windows
5074 */
5075 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005076ex_quit_all(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005077{
5078# ifdef FEAT_CMDWIN
5079 if (cmdwin_type != 0)
5080 {
5081 if (eap->forceit)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005082 cmdwin_result = K_XF1; // ex_window() takes care of this
Bram Moolenaar071d4272004-06-13 20:20:40 +00005083 else
5084 cmdwin_result = K_XF2;
5085 return;
5086 }
5087# endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005088
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005089 // Don't quit while editing the command line.
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005090 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005091 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005092 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005093 return;
5094 }
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005095
5096 if (before_quit_autocmds(curwin, TRUE, eap->forceit))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005097 return;
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005098
Bram Moolenaar071d4272004-06-13 20:20:40 +00005099 exiting = TRUE;
Bram Moolenaar027387f2016-01-02 22:25:52 +01005100 if (eap->forceit || !check_changed_any(FALSE, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005101 getout(0);
5102 not_exiting();
5103}
5104
Bram Moolenaar071d4272004-06-13 20:20:40 +00005105/*
5106 * ":close": close current window, unless it is the last one
5107 */
5108 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005109ex_close(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005110{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005111 win_T *win;
5112 int winnr = 0;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005113#ifdef FEAT_CMDWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00005114 if (cmdwin_type != 0)
Bram Moolenaar9bd1a7e2011-05-19 14:50:54 +02005115 cmdwin_result = Ctrl_C;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005116 else
Bram Moolenaar4033c552017-09-16 20:54:51 +02005117#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005118 if (!text_locked() && !curbuf_locked())
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005119 {
5120 if (eap->addr_count == 0)
5121 ex_win_close(eap->forceit, curwin, NULL);
Bram Moolenaar6d41c782018-06-06 09:11:12 +02005122 else
5123 {
Bram Moolenaar29323592016-07-24 22:04:11 +02005124 FOR_ALL_WINDOWS(win)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005125 {
5126 winnr++;
5127 if (winnr == eap->line2)
5128 break;
5129 }
5130 if (win == NULL)
5131 win = lastwin;
5132 ex_win_close(eap->forceit, win, NULL);
5133 }
5134 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005135}
5136
Bram Moolenaar4033c552017-09-16 20:54:51 +02005137#ifdef FEAT_QUICKFIX
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005138/*
5139 * ":pclose": Close any preview window.
5140 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005141 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005142ex_pclose(exarg_T *eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005143{
5144 win_T *win;
5145
Bram Moolenaar79648732019-07-18 21:43:07 +02005146 // First close any normal window.
Bram Moolenaar29323592016-07-24 22:04:11 +02005147 FOR_ALL_WINDOWS(win)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005148 if (win->w_p_pvw)
5149 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00005150 ex_win_close(eap->forceit, win, NULL);
Bram Moolenaar79648732019-07-18 21:43:07 +02005151 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005152 }
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01005153# ifdef FEAT_PROP_POPUP
Bram Moolenaar79648732019-07-18 21:43:07 +02005154 // Also when 'previewpopup' is empty, it might have been cleared.
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02005155 popup_close_preview();
Bram Moolenaar79648732019-07-18 21:43:07 +02005156# endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005157}
Bram Moolenaar4033c552017-09-16 20:54:51 +02005158#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005159
Bram Moolenaarf740b292006-02-16 22:11:02 +00005160/*
5161 * Close window "win" and take care of handling closing the last window for a
5162 * modified buffer.
5163 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005164 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005165ex_win_close(
5166 int forceit,
5167 win_T *win,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005168 tabpage_T *tp) // NULL or the tab page "win" is in
Bram Moolenaar071d4272004-06-13 20:20:40 +00005169{
5170 int need_hide;
5171 buf_T *buf = win->w_buffer;
5172
5173 need_hide = (bufIsChanged(buf) && buf->b_nwindows <= 1);
Bram Moolenaareb44a682017-08-03 22:44:55 +02005174 if (need_hide && !buf_hide(buf) && !forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005175 {
Bram Moolenaar4033c552017-09-16 20:54:51 +02005176#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005177 if ((p_confirm || cmdmod.confirm) && p_write)
5178 {
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005179 bufref_T bufref;
5180
5181 set_bufref(&bufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005182 dialog_changed(buf, FALSE);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005183 if (bufref_valid(&bufref) && bufIsChanged(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005184 return;
5185 need_hide = FALSE;
5186 }
5187 else
Bram Moolenaar4033c552017-09-16 20:54:51 +02005188#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005189 {
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02005190 no_write_message();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005191 return;
5192 }
5193 }
5194
Bram Moolenaar4033c552017-09-16 20:54:51 +02005195#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00005196 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005197#endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00005198
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005199 // free buffer when not hiding it or when it's a scratch buffer
Bram Moolenaarf740b292006-02-16 22:11:02 +00005200 if (tp == NULL)
Bram Moolenaareb44a682017-08-03 22:44:55 +02005201 win_close(win, !need_hide && !buf_hide(buf));
Bram Moolenaarf740b292006-02-16 22:11:02 +00005202 else
Bram Moolenaareb44a682017-08-03 22:44:55 +02005203 win_close_othertab(win, !need_hide && !buf_hide(buf), tp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005204}
5205
Bram Moolenaar071d4272004-06-13 20:20:40 +00005206/*
Bram Moolenaardea25702017-01-29 15:18:10 +01005207 * Handle the argument for a tabpage related ex command.
5208 * Returns a tabpage number.
5209 * When an error is encountered then eap->errmsg is set.
5210 */
5211 static int
5212get_tabpage_arg(exarg_T *eap)
5213{
5214 int tab_number;
5215 int unaccept_arg0 = (eap->cmdidx == CMD_tabmove) ? 0 : 1;
5216
5217 if (eap->arg && *eap->arg != NUL)
5218 {
5219 char_u *p = eap->arg;
5220 char_u *p_save;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005221 int relative = 0; // argument +N/-N means: go to N places to the
5222 // right/left relative to the current position.
Bram Moolenaardea25702017-01-29 15:18:10 +01005223
5224 if (*p == '-')
5225 {
5226 relative = -1;
5227 p++;
5228 }
5229 else if (*p == '+')
5230 {
5231 relative = 1;
5232 p++;
5233 }
5234
5235 p_save = p;
5236 tab_number = getdigits(&p);
5237
5238 if (relative == 0)
5239 {
5240 if (STRCMP(p, "$") == 0)
5241 tab_number = LAST_TAB_NR;
5242 else if (p == p_save || *p_save == '-' || *p != NUL
5243 || tab_number > LAST_TAB_NR)
5244 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005245 // No numbers as argument.
Bram Moolenaardea25702017-01-29 15:18:10 +01005246 eap->errmsg = e_invarg;
5247 goto theend;
5248 }
5249 }
5250 else
5251 {
5252 if (*p_save == NUL)
5253 tab_number = 1;
5254 else if (p == p_save || *p_save == '-' || *p != NUL
5255 || tab_number == 0)
5256 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005257 // No numbers as argument.
Bram Moolenaardea25702017-01-29 15:18:10 +01005258 eap->errmsg = e_invarg;
5259 goto theend;
5260 }
5261 tab_number = tab_number * relative + tabpage_index(curtab);
5262 if (!unaccept_arg0 && relative == -1)
5263 --tab_number;
5264 }
5265 if (tab_number < unaccept_arg0 || tab_number > LAST_TAB_NR)
5266 eap->errmsg = e_invarg;
5267 }
5268 else if (eap->addr_count > 0)
5269 {
5270 if (unaccept_arg0 && eap->line2 == 0)
Bram Moolenaarc6251552017-01-29 21:42:20 +01005271 {
Bram Moolenaardea25702017-01-29 15:18:10 +01005272 eap->errmsg = e_invrange;
Bram Moolenaarc6251552017-01-29 21:42:20 +01005273 tab_number = 0;
5274 }
Bram Moolenaardea25702017-01-29 15:18:10 +01005275 else
5276 {
5277 tab_number = eap->line2;
Bram Moolenaar82a12462019-01-22 22:41:42 +01005278 if (!unaccept_arg0 && *skipwhite(*eap->cmdlinep) == '-')
Bram Moolenaardea25702017-01-29 15:18:10 +01005279 {
5280 --tab_number;
5281 if (tab_number < unaccept_arg0)
5282 eap->errmsg = e_invarg;
5283 }
5284 }
5285 }
5286 else
5287 {
5288 switch (eap->cmdidx)
5289 {
5290 case CMD_tabnext:
5291 tab_number = tabpage_index(curtab) + 1;
5292 if (tab_number > LAST_TAB_NR)
5293 tab_number = 1;
5294 break;
5295 case CMD_tabmove:
5296 tab_number = LAST_TAB_NR;
5297 break;
5298 default:
5299 tab_number = tabpage_index(curtab);
5300 }
5301 }
5302
5303theend:
5304 return tab_number;
5305}
5306
5307/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00005308 * ":tabclose": close current tab page, unless it is the last one.
5309 * ":tabclose N": close tab page N.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005310 */
5311 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005312ex_tabclose(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005313{
Bram Moolenaarf740b292006-02-16 22:11:02 +00005314 tabpage_T *tp;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005315 int tab_number;
Bram Moolenaarf740b292006-02-16 22:11:02 +00005316
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005317# ifdef FEAT_CMDWIN
5318 if (cmdwin_type != 0)
5319 cmdwin_result = K_IGNORE;
5320 else
5321# endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00005322 if (first_tabpage->tp_next == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005323 emsg(_("E784: Cannot close last tab page"));
Bram Moolenaarf740b292006-02-16 22:11:02 +00005324 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005325 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005326 tab_number = get_tabpage_arg(eap);
5327 if (eap->errmsg == NULL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005328 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005329 tp = find_tabpage(tab_number);
Bram Moolenaarf740b292006-02-16 22:11:02 +00005330 if (tp == NULL)
5331 {
5332 beep_flush();
5333 return;
5334 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005335 if (tp != curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00005336 {
5337 tabpage_close_other(tp, eap->forceit);
5338 return;
5339 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005340 else if (!text_locked() && !curbuf_locked())
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005341 tabpage_close(eap->forceit);
5342 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005343 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005344}
5345
5346/*
5347 * ":tabonly": close all tab pages except the current one
5348 */
5349 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005350ex_tabonly(exarg_T *eap)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005351{
5352 tabpage_T *tp;
5353 int done;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005354 int tab_number;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005355
5356# ifdef FEAT_CMDWIN
5357 if (cmdwin_type != 0)
5358 cmdwin_result = K_IGNORE;
5359 else
5360# endif
5361 if (first_tabpage->tp_next == NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01005362 msg(_("Already only one tab page"));
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005363 else
5364 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005365 tab_number = get_tabpage_arg(eap);
5366 if (eap->errmsg == NULL)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005367 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005368 goto_tabpage(tab_number);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005369 // Repeat this up to a 1000 times, because autocommands may
5370 // mess up the lists.
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005371 for (done = 0; done < 1000; ++done)
5372 {
5373 FOR_ALL_TABPAGES(tp)
5374 if (tp->tp_topframe != topframe)
5375 {
5376 tabpage_close_other(tp, eap->forceit);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005377 // if we failed to close it quit
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005378 if (valid_tabpage(tp))
5379 done = 1000;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005380 // start over, "tp" is now invalid
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005381 break;
5382 }
5383 if (first_tabpage->tp_next == NULL)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005384 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005385 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005386 }
5387 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005388}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005389
5390/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00005391 * Close the current tab page.
5392 */
5393 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005394tabpage_close(int forceit)
Bram Moolenaarf740b292006-02-16 22:11:02 +00005395{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005396 // First close all the windows but the current one. If that worked then
5397 // close the last window in this tab, that will close it.
Bram Moolenaar459ca562016-11-10 18:16:33 +01005398 if (!ONE_WINDOW)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005399 close_others(TRUE, forceit);
Bram Moolenaar459ca562016-11-10 18:16:33 +01005400 if (ONE_WINDOW)
Bram Moolenaarf740b292006-02-16 22:11:02 +00005401 ex_win_close(forceit, curwin, NULL);
5402# ifdef FEAT_GUI
5403 need_mouse_correct = TRUE;
5404# endif
5405}
5406
5407/*
5408 * Close tab page "tp", which is not the current tab page.
5409 * Note that autocommands may make "tp" invalid.
Bram Moolenaar7875acc2006-09-10 13:51:17 +00005410 * Also takes care of the tab pages line disappearing when closing the
5411 * last-but-one tab page.
Bram Moolenaarf740b292006-02-16 22:11:02 +00005412 */
5413 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005414tabpage_close_other(tabpage_T *tp, int forceit)
Bram Moolenaarf740b292006-02-16 22:11:02 +00005415{
5416 int done = 0;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005417 win_T *wp;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00005418 int h = tabline_height();
Bram Moolenaarf740b292006-02-16 22:11:02 +00005419
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005420 // Limit to 1000 windows, autocommands may add a window while we close
5421 // one. OK, so I'm paranoid...
Bram Moolenaarf740b292006-02-16 22:11:02 +00005422 while (++done < 1000)
5423 {
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005424 wp = tp->tp_firstwin;
5425 ex_win_close(forceit, wp, tp);
Bram Moolenaarf740b292006-02-16 22:11:02 +00005426
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005427 // Autocommands may delete the tab page under our fingers and we may
5428 // fail to close a window with a modified buffer.
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005429 if (!valid_tabpage(tp) || tp->tp_firstwin == wp)
Bram Moolenaarf740b292006-02-16 22:11:02 +00005430 break;
5431 }
Bram Moolenaar7875acc2006-09-10 13:51:17 +00005432
Bram Moolenaar29323592016-07-24 22:04:11 +02005433 apply_autocmds(EVENT_TABCLOSED, NULL, NULL, FALSE, curbuf);
Bram Moolenaar29323592016-07-24 22:04:11 +02005434
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005435 redraw_tabline = TRUE;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00005436 if (h != tabline_height())
5437 shell_new_rows();
Bram Moolenaarf740b292006-02-16 22:11:02 +00005438}
5439
5440/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005441 * ":only".
5442 */
5443 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005444ex_only(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005445{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005446 win_T *wp;
5447 int wnr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005448# ifdef FEAT_GUI
5449 need_mouse_correct = TRUE;
5450# endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005451 if (eap->addr_count > 0)
5452 {
5453 wnr = eap->line2;
5454 for (wp = firstwin; --wnr > 0; )
5455 {
5456 if (wp->w_next == NULL)
5457 break;
5458 else
5459 wp = wp->w_next;
5460 }
5461 win_goto(wp);
5462 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005463 close_others(TRUE, eap->forceit);
5464}
5465
Bram Moolenaar071d4272004-06-13 20:20:40 +00005466 static void
Bram Moolenaar5e1e6d22017-01-02 17:26:00 +01005467ex_hide(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005468{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005469 // ":hide" or ":hide | cmd": hide current window
Bram Moolenaar2256c992016-11-15 21:17:07 +01005470 if (!eap->skip)
5471 {
Bram Moolenaar4033c552017-09-16 20:54:51 +02005472#ifdef FEAT_GUI
Bram Moolenaar2256c992016-11-15 21:17:07 +01005473 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005474#endif
Bram Moolenaar2256c992016-11-15 21:17:07 +01005475 if (eap->addr_count == 0)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005476 win_close(curwin, FALSE); // don't free buffer
Bram Moolenaar2256c992016-11-15 21:17:07 +01005477 else
5478 {
5479 int winnr = 0;
5480 win_T *win;
Bram Moolenaarf240e182014-11-27 18:33:02 +01005481
Bram Moolenaar2256c992016-11-15 21:17:07 +01005482 FOR_ALL_WINDOWS(win)
5483 {
5484 winnr++;
5485 if (winnr == eap->line2)
5486 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005487 }
Bram Moolenaar2256c992016-11-15 21:17:07 +01005488 if (win == NULL)
5489 win = lastwin;
5490 win_close(win, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005491 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005492 }
5493}
5494
5495/*
5496 * ":stop" and ":suspend": Suspend Vim.
5497 */
5498 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005499ex_stop(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005500{
5501 /*
5502 * Disallow suspending for "rvim".
5503 */
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005504 if (!check_restricted())
Bram Moolenaar071d4272004-06-13 20:20:40 +00005505 {
5506 if (!eap->forceit)
5507 autowrite_all();
5508 windgoto((int)Rows - 1, 0);
5509 out_char('\n');
5510 out_flush();
5511 stoptermcap();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005512 out_flush(); // needed for SUN to restore xterm buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00005513#ifdef FEAT_TITLE
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005514 mch_restore_title(SAVE_RESTORE_BOTH); // restore window titles
Bram Moolenaar071d4272004-06-13 20:20:40 +00005515#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005516 ui_suspend(); // call machine specific function
Bram Moolenaar071d4272004-06-13 20:20:40 +00005517#ifdef FEAT_TITLE
5518 maketitle();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005519 resettitle(); // force updating the title
Bram Moolenaar071d4272004-06-13 20:20:40 +00005520#endif
5521 starttermcap();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005522 scroll_start(); // scroll screen before redrawing
Bram Moolenaar071d4272004-06-13 20:20:40 +00005523 redraw_later_clear();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005524 shell_resized(); // may have resized window
Bram Moolenaar071d4272004-06-13 20:20:40 +00005525 }
5526}
5527
5528/*
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005529 * ":exit", ":xit" and ":wq": Write file and quite the current window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005530 */
5531 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005532ex_exit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005533{
5534#ifdef FEAT_CMDWIN
5535 if (cmdwin_type != 0)
5536 {
5537 cmdwin_result = Ctrl_C;
5538 return;
5539 }
5540#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005541 // Don't quit while editing the command line.
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005542 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005543 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005544 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005545 return;
5546 }
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005547
5548 if (before_quit_autocmds(curwin, FALSE, eap->forceit))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005549 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005550
5551 /*
5552 * if more files or windows we won't exit
5553 */
5554 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
5555 exiting = TRUE;
5556 if ( ((eap->cmdidx == CMD_wq
5557 || curbufIsChanged())
5558 && do_write(eap) == FAIL)
5559 || check_more(TRUE, eap->forceit) == FAIL
Bram Moolenaar027387f2016-01-02 22:25:52 +01005560 || (only_one_window() && check_changed_any(eap->forceit, FALSE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005561 {
5562 not_exiting();
5563 }
5564 else
5565 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005566 if (only_one_window()) // quit last window, exit Vim
Bram Moolenaar071d4272004-06-13 20:20:40 +00005567 getout(0);
Bram Moolenaar2c33d7b2017-10-14 16:06:20 +02005568 not_exiting();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005569# ifdef FEAT_GUI
5570 need_mouse_correct = TRUE;
5571# endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005572 // Quit current window, may free the buffer.
Bram Moolenaareb44a682017-08-03 22:44:55 +02005573 win_close(curwin, !buf_hide(curwin->w_buffer));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005574 }
5575}
5576
5577/*
5578 * ":print", ":list", ":number".
5579 */
5580 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005581ex_print(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005582{
Bram Moolenaardf177f62005-02-22 08:39:57 +00005583 if (curbuf->b_ml.ml_flags & ML_EMPTY)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005584 emsg(_(e_emptybuf));
Bram Moolenaardf177f62005-02-22 08:39:57 +00005585 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005586 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00005587 for ( ;!got_int; ui_breakcheck())
5588 {
5589 print_line(eap->line1,
5590 (eap->cmdidx == CMD_number || eap->cmdidx == CMD_pound
5591 || (eap->flags & EXFLAG_NR)),
5592 eap->cmdidx == CMD_list || (eap->flags & EXFLAG_LIST));
5593 if (++eap->line1 > eap->line2)
5594 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005595 out_flush(); // show one line at a time
Bram Moolenaardf177f62005-02-22 08:39:57 +00005596 }
5597 setpcmark();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005598 // put cursor at last line
Bram Moolenaardf177f62005-02-22 08:39:57 +00005599 curwin->w_cursor.lnum = eap->line2;
5600 beginline(BL_SOL | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005601 }
5602
Bram Moolenaar071d4272004-06-13 20:20:40 +00005603 ex_no_reprint = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005604}
5605
5606#ifdef FEAT_BYTEOFF
5607 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005608ex_goto(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005609{
5610 goto_byte(eap->line2);
5611}
5612#endif
5613
5614/*
5615 * ":shell".
5616 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005617 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005618ex_shell(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005619{
5620 do_shell(NULL, 0);
5621}
5622
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005623#if defined(HAVE_DROP_FILE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005624
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005625static int drop_busy = FALSE;
5626static int drop_filec;
5627static char_u **drop_filev = NULL;
5628static int drop_split;
5629static void (*drop_callback)(void *);
5630static void *drop_cookie;
5631
5632 static void
5633handle_drop_internal(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005634{
5635 exarg_T ea;
5636 int save_msg_scroll = msg_scroll;
5637
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005638 // Setting the argument list may cause screen updates and being called
5639 // recursively. Avoid that by setting drop_busy.
5640 drop_busy = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005641
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005642 // Check whether the current buffer is changed. If so, we will need
5643 // to split the current window or data could be lost.
5644 // We don't need to check if the 'hidden' option is set, as in this
5645 // case the buffer won't be lost.
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005646 if (!buf_hide(curbuf) && !drop_split)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005647 {
5648 ++emsg_off;
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005649 drop_split = check_changed(curbuf, CCGD_AW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005650 --emsg_off;
5651 }
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005652 if (drop_split)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005653 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005654 if (win_split(0, 0) == FAIL)
5655 return;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02005656 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005657
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005658 // When splitting the window, create a new alist. Otherwise the
5659 // existing one is overwritten.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005660 alist_unlink(curwin->w_alist);
5661 alist_new();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005662 }
5663
5664 /*
5665 * Set up the new argument list.
5666 */
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005667 alist_set(ALIST(curwin), drop_filec, drop_filev, FALSE, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005668
5669 /*
5670 * Move to the first file.
5671 */
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005672 // Fake up a minimal "next" command for do_argfile()
Bram Moolenaara80faa82020-04-12 19:37:17 +02005673 CLEAR_FIELD(ea);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005674 ea.cmd = (char_u *)"next";
5675 do_argfile(&ea, 0);
5676
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005677 // do_ecmd() may set need_start_insertmode, but since we never left Insert
5678 // mode that is not needed here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005679 need_start_insertmode = FALSE;
5680
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005681 // Restore msg_scroll, otherwise a following command may cause scrolling
5682 // unexpectedly. The screen will be redrawn by the caller, thus
5683 // msg_scroll being set by displaying a message is irrelevant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005684 msg_scroll = save_msg_scroll;
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005685
5686 if (drop_callback != NULL)
5687 drop_callback(drop_cookie);
5688
5689 drop_filev = NULL;
5690 drop_busy = FALSE;
5691}
5692
5693/*
5694 * Handle a file drop. The code is here because a drop is *nearly* like an
5695 * :args command, but not quite (we have a list of exact filenames, so we
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01005696 * don't want to (a) parse a command line, or (b) expand wildcards). So the
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005697 * code is very similar to :args and hence needs access to a lot of the static
5698 * functions in this file.
5699 *
5700 * The "filev" list must have been allocated using alloc(), as should each item
5701 * in the list. This function takes over responsibility for freeing the "filev"
5702 * list.
5703 */
5704 void
5705handle_drop(
5706 int filec, // the number of files dropped
5707 char_u **filev, // the list of files dropped
5708 int split, // force splitting the window
5709 void (*callback)(void *), // to be called after setting the argument
5710 // list
5711 void *cookie) // argument for "callback" (allocated)
5712{
5713 // Cannot handle recursive drops, finish the pending one.
5714 if (drop_busy)
5715 {
5716 FreeWild(filec, filev);
5717 vim_free(cookie);
5718 return;
5719 }
5720
5721 // When calling handle_drop() more than once in a row we only use the last
5722 // one.
5723 if (drop_filev != NULL)
5724 {
5725 FreeWild(drop_filec, drop_filev);
5726 vim_free(drop_cookie);
5727 }
5728
5729 drop_filec = filec;
5730 drop_filev = filev;
5731 drop_split = split;
5732 drop_callback = callback;
5733 drop_cookie = cookie;
5734
5735 // Postpone this when:
5736 // - editing the command line
5737 // - not possible to change the current buffer
5738 // - updating the screen
5739 // As it may change buffers and window structures that are in use and cause
5740 // freed memory to be used.
5741 if (text_locked() || curbuf_locked() || updating_screen)
5742 return;
5743
5744 handle_drop_internal();
5745}
5746
5747/*
5748 * To be called when text is unlocked, curbuf is unlocked or updating_screen is
5749 * reset: Handle a postponed drop.
5750 */
5751 void
5752handle_any_postponed_drop(void)
5753{
5754 if (!drop_busy && drop_filev != NULL
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +02005755 && !text_locked() && !curbuf_locked() && !updating_screen)
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005756 handle_drop_internal();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005757}
5758#endif
5759
Bram Moolenaar071d4272004-06-13 20:20:40 +00005760/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005761 * ":preserve".
5762 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005763 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005764ex_preserve(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005765{
Bram Moolenaar4399ef42005-02-12 14:29:27 +00005766 curbuf->b_flags |= BF_PRESERVED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005767 ml_preserve(curbuf, TRUE);
5768}
5769
5770/*
5771 * ":recover".
5772 */
5773 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005774ex_recover(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005775{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005776 // Set recoverymode right away to avoid the ATTENTION prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005777 recoverymode = TRUE;
Bram Moolenaar45d3b142013-11-09 03:31:51 +01005778 if (!check_changed(curbuf, (p_awa ? CCGD_AW : 0)
5779 | CCGD_MULTWIN
5780 | (eap->forceit ? CCGD_FORCEIT : 0)
5781 | CCGD_EXCMD)
5782
Bram Moolenaar071d4272004-06-13 20:20:40 +00005783 && (*eap->arg == NUL
5784 || setfname(curbuf, eap->arg, NULL, TRUE) == OK))
Bram Moolenaar99499b12019-05-23 21:35:48 +02005785 ml_recover(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005786 recoverymode = FALSE;
5787}
5788
5789/*
5790 * Command modifier used in a wrong way.
5791 */
5792 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005793ex_wrongmodifier(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005794{
5795 eap->errmsg = e_invcmd;
5796}
5797
Bram Moolenaar071d4272004-06-13 20:20:40 +00005798/*
5799 * :sview [+command] file split window with new file, read-only
5800 * :split [[+command] file] split window with current or new file
5801 * :vsplit [[+command] file] split window vertically with current or new file
5802 * :new [[+command] file] split window with no or new file
5803 * :vnew [[+command] file] split vertically window with no or new file
5804 * :sfind [+command] file split window with file in 'path'
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005805 *
5806 * :tabedit open new Tab page with empty window
5807 * :tabedit [+command] file open new Tab page and edit "file"
5808 * :tabnew [[+command] file] just like :tabedit
5809 * :tabfind [+command] file open new Tab page and find "file"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005810 */
5811 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005812ex_splitview(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005813{
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005814 win_T *old_curwin = curwin;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005815#if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005816 char_u *fname = NULL;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005817#endif
5818#ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00005819 int browse_flag = cmdmod.browse;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005820#endif
Bram Moolenaar39902a02018-06-21 22:10:08 +02005821 int use_tab = eap->cmdidx == CMD_tabedit
5822 || eap->cmdidx == CMD_tabfind
5823 || eap->cmdidx == CMD_tabnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005824
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01005825 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02005826 return;
5827
Bram Moolenaar4033c552017-09-16 20:54:51 +02005828#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00005829 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005830#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005831
Bram Moolenaar4033c552017-09-16 20:54:51 +02005832#ifdef FEAT_QUICKFIX
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005833 // A ":split" in the quickfix window works like ":new". Don't want two
5834 // quickfix windows. But it's OK when doing ":tab split".
Bram Moolenaar05bb9532008-07-04 09:44:11 +00005835 if (bt_quickfix(curbuf) && cmdmod.tab == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005836 {
5837 if (eap->cmdidx == CMD_split)
5838 eap->cmdidx = CMD_new;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005839 if (eap->cmdidx == CMD_vsplit)
5840 eap->cmdidx = CMD_vnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005841 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02005842#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005843
Bram Moolenaar4033c552017-09-16 20:54:51 +02005844#ifdef FEAT_SEARCHPATH
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005845 if (eap->cmdidx == CMD_sfind || eap->cmdidx == CMD_tabfind)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005846 {
5847 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg),
5848 FNAME_MESS, TRUE, curbuf->b_ffname);
5849 if (fname == NULL)
5850 goto theend;
5851 eap->arg = fname;
5852 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005853# ifdef FEAT_BROWSE
Bram Moolenaar4033c552017-09-16 20:54:51 +02005854 else
5855# endif
5856#endif
5857#ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00005858 if (cmdmod.browse
Bram Moolenaar071d4272004-06-13 20:20:40 +00005859 && eap->cmdidx != CMD_vnew
Bram Moolenaar071d4272004-06-13 20:20:40 +00005860 && eap->cmdidx != CMD_new)
5861 {
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00005862 if (
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005863# ifdef FEAT_GUI
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00005864 !gui.in_use &&
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005865# endif
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00005866 au_has_group((char_u *)"FileExplorer"))
5867 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005868 // No browsing supported but we do have the file explorer:
5869 // Edit the directory.
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00005870 if (*eap->arg == NUL || !mch_isdir(eap->arg))
5871 eap->arg = (char_u *)".";
5872 }
5873 else
5874 {
Bram Moolenaar39902a02018-06-21 22:10:08 +02005875 fname = do_browse(0, (char_u *)(use_tab
5876 ? _("Edit File in new tab page")
5877 : _("Edit File in new window")),
Bram Moolenaar071d4272004-06-13 20:20:40 +00005878 eap->arg, NULL, NULL, NULL, curbuf);
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00005879 if (fname == NULL)
5880 goto theend;
5881 eap->arg = fname;
5882 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005883 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005884 cmdmod.browse = FALSE; // Don't browse again in do_ecmd().
Bram Moolenaar4033c552017-09-16 20:54:51 +02005885#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005886
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005887 /*
5888 * Either open new tab page or split the window.
5889 */
Bram Moolenaar39902a02018-06-21 22:10:08 +02005890 if (use_tab)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005891 {
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005892 if (win_new_tabpage(cmdmod.tab != 0 ? cmdmod.tab
5893 : eap->addr_count == 0 ? 0
5894 : (int)eap->line2 + 1) != FAIL)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005895 {
Bram Moolenaard2b66012008-01-09 19:30:36 +00005896 do_exedit(eap, old_curwin);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005897
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005898 // set the alternate buffer for the window we came from
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005899 if (curwin != old_curwin
5900 && win_valid(old_curwin)
5901 && old_curwin->w_buffer != curbuf
5902 && !cmdmod.keepalt)
5903 old_curwin->w_alt_fnum = curbuf->b_fnum;
5904 }
5905 }
5906 else if (win_split(eap->addr_count > 0 ? (int)eap->line2 : 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005907 *eap->cmd == 'v' ? WSP_VERT : 0) != FAIL)
5908 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005909 // Reset 'scrollbind' when editing another file, but keep it when
5910 // doing ":split" without arguments.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005911 if (*eap->arg != NUL
Bram Moolenaar8a3bb562018-03-04 20:14:14 +01005912# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00005913 || cmdmod.browse
Bram Moolenaar8a3bb562018-03-04 20:14:14 +01005914# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005915 )
Bram Moolenaar3368ea22010-09-21 16:56:35 +02005916 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005917 else
5918 do_check_scrollbind(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005919 do_exedit(eap, old_curwin);
5920 }
5921
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005922# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00005923 cmdmod.browse = browse_flag;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005924# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005925
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005926# if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005927theend:
5928 vim_free(fname);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005929# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005930}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005931
5932/*
Bram Moolenaar80a94a52006-02-23 21:26:58 +00005933 * Open a new tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005934 */
5935 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005936tabpage_new(void)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00005937{
5938 exarg_T ea;
5939
Bram Moolenaara80faa82020-04-12 19:37:17 +02005940 CLEAR_FIELD(ea);
Bram Moolenaar80a94a52006-02-23 21:26:58 +00005941 ea.cmdidx = CMD_tabnew;
5942 ea.cmd = (char_u *)"tabn";
5943 ea.arg = (char_u *)"";
5944 ex_splitview(&ea);
5945}
5946
5947/*
5948 * :tabnext command
5949 */
5950 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005951ex_tabnext(exarg_T *eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005952{
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005953 int tab_number;
5954
Bram Moolenaar8cdbd5b2019-06-16 15:50:45 +02005955 if (ERROR_IF_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02005956 return;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005957 switch (eap->cmdidx)
5958 {
5959 case CMD_tabfirst:
5960 case CMD_tabrewind:
5961 goto_tabpage(1);
5962 break;
5963 case CMD_tablast:
5964 goto_tabpage(9999);
5965 break;
5966 case CMD_tabprevious:
5967 case CMD_tabNext:
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005968 if (eap->arg && *eap->arg != NUL)
5969 {
5970 char_u *p = eap->arg;
5971 char_u *p_save = p;
5972
5973 tab_number = getdigits(&p);
5974 if (p == p_save || *p_save == '-' || *p != NUL
5975 || tab_number == 0)
5976 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005977 // No numbers as argument.
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005978 eap->errmsg = e_invarg;
5979 return;
5980 }
5981 }
5982 else
5983 {
5984 if (eap->addr_count == 0)
5985 tab_number = 1;
5986 else
5987 {
5988 tab_number = eap->line2;
5989 if (tab_number < 1)
5990 {
5991 eap->errmsg = e_invrange;
5992 return;
5993 }
5994 }
5995 }
5996 goto_tabpage(-tab_number);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005997 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005998 default: // CMD_tabnext
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005999 tab_number = get_tabpage_arg(eap);
6000 if (eap->errmsg == NULL)
6001 goto_tabpage(tab_number);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006002 break;
6003 }
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006004}
6005
6006/*
6007 * :tabmove command
6008 */
6009 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006010ex_tabmove(exarg_T *eap)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006011{
Bram Moolenaar40ce3a42015-04-21 18:08:39 +02006012 int tab_number;
Bram Moolenaar8cb8dca2012-07-06 18:27:39 +02006013
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006014 tab_number = get_tabpage_arg(eap);
6015 if (eap->errmsg == NULL)
6016 tabpage_move(tab_number);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006017}
6018
6019/*
6020 * :tabs command: List tabs and their contents.
6021 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006022 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006023ex_tabs(exarg_T *eap UNUSED)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006024{
6025 tabpage_T *tp;
6026 win_T *wp;
6027 int tabcount = 1;
6028
6029 msg_start();
6030 msg_scroll = TRUE;
6031 for (tp = first_tabpage; tp != NULL && !got_int; tp = tp->tp_next)
6032 {
6033 msg_putchar('\n');
6034 vim_snprintf((char *)IObuff, IOSIZE, _("Tab page %d"), tabcount++);
Bram Moolenaar8820b482017-03-16 17:23:31 +01006035 msg_outtrans_attr(IObuff, HL_ATTR(HLF_T));
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006036 out_flush(); // output one line at a time
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006037 ui_breakcheck();
6038
Bram Moolenaar030f0df2006-02-21 22:02:53 +00006039 if (tp == curtab)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006040 wp = firstwin;
6041 else
6042 wp = tp->tp_firstwin;
6043 for ( ; wp != NULL && !got_int; wp = wp->w_next)
6044 {
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006045 msg_putchar('\n');
6046 msg_putchar(wp == curwin ? '>' : ' ');
6047 msg_putchar(' ');
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006048 msg_putchar(bufIsChanged(wp->w_buffer) ? '+' : ' ');
6049 msg_putchar(' ');
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006050 if (buf_spname(wp->w_buffer) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02006051 vim_strncpy(IObuff, buf_spname(wp->w_buffer), IOSIZE - 1);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006052 else
6053 home_replace(wp->w_buffer, wp->w_buffer->b_fname,
6054 IObuff, IOSIZE, TRUE);
6055 msg_outtrans(IObuff);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006056 out_flush(); // output one line at a time
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006057 ui_breakcheck();
6058 }
6059 }
6060}
6061
Bram Moolenaar071d4272004-06-13 20:20:40 +00006062/*
6063 * ":mode": Set screen mode.
6064 * If no argument given, just get the screen size and redraw.
6065 */
6066 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006067ex_mode(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006068{
6069 if (*eap->arg == NUL)
6070 shell_resized();
6071 else
Bram Moolenaar3c71aec2020-01-17 19:32:20 +01006072 emsg(_(e_screenmode));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006073}
6074
Bram Moolenaar071d4272004-06-13 20:20:40 +00006075/*
6076 * ":resize".
6077 * set, increment or decrement current window height
6078 */
6079 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006080ex_resize(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006081{
6082 int n;
6083 win_T *wp = curwin;
6084
6085 if (eap->addr_count > 0)
6086 {
6087 n = eap->line2;
6088 for (wp = firstwin; wp->w_next != NULL && --n > 0; wp = wp->w_next)
6089 ;
6090 }
6091
Bram Moolenaar44a2f922016-03-19 22:11:51 +01006092# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00006093 need_mouse_correct = TRUE;
Bram Moolenaar44a2f922016-03-19 22:11:51 +01006094# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006095 n = atol((char *)eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006096 if (cmdmod.split & WSP_VERT)
6097 {
6098 if (*eap->arg == '-' || *eap->arg == '+')
Bram Moolenaar02631462017-09-22 15:20:32 +02006099 n += curwin->w_width;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006100 else if (n == 0 && eap->arg[0] == NUL) // default is very wide
Bram Moolenaar071d4272004-06-13 20:20:40 +00006101 n = 9999;
6102 win_setwidth_win((int)n, wp);
6103 }
6104 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006105 {
6106 if (*eap->arg == '-' || *eap->arg == '+')
6107 n += curwin->w_height;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006108 else if (n == 0 && eap->arg[0] == NUL) // default is very high
Bram Moolenaar071d4272004-06-13 20:20:40 +00006109 n = 9999;
6110 win_setheight_win((int)n, wp);
6111 }
6112}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006113
6114/*
6115 * ":find [+command] <file>" command.
6116 */
6117 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006118ex_find(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006119{
6120#ifdef FEAT_SEARCHPATH
6121 char_u *fname;
6122 int count;
6123
6124 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg), FNAME_MESS,
6125 TRUE, curbuf->b_ffname);
6126 if (eap->addr_count > 0)
6127 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006128 // Repeat finding the file "count" times. This matters when it
6129 // appears several times in the path.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006130 count = eap->line2;
6131 while (fname != NULL && --count > 0)
6132 {
6133 vim_free(fname);
6134 fname = find_file_in_path(NULL, 0, FNAME_MESS,
6135 FALSE, curbuf->b_ffname);
6136 }
6137 }
6138
6139 if (fname != NULL)
6140 {
6141 eap->arg = fname;
6142#endif
6143 do_exedit(eap, NULL);
6144#ifdef FEAT_SEARCHPATH
6145 vim_free(fname);
6146 }
6147#endif
6148}
6149
6150/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00006151 * ":open" simulation: for now just work like ":visual".
6152 */
6153 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006154ex_open(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00006155{
6156 regmatch_T regmatch;
6157 char_u *p;
6158
6159 curwin->w_cursor.lnum = eap->line2;
6160 beginline(BL_SOL | BL_FIX);
6161 if (*eap->arg == '/')
6162 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006163 // ":open /pattern/": put cursor in column found with pattern
Bram Moolenaardf177f62005-02-22 08:39:57 +00006164 ++eap->arg;
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006165 p = skip_regexp(eap->arg, '/', p_magic);
Bram Moolenaardf177f62005-02-22 08:39:57 +00006166 *p = NUL;
6167 regmatch.regprog = vim_regcomp(eap->arg, p_magic ? RE_MAGIC : 0);
6168 if (regmatch.regprog != NULL)
6169 {
6170 regmatch.rm_ic = p_ic;
6171 p = ml_get_curline();
6172 if (vim_regexec(&regmatch, p, (colnr_T)0))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006173 curwin->w_cursor.col = (colnr_T)(regmatch.startp[0] - p);
Bram Moolenaardf177f62005-02-22 08:39:57 +00006174 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006175 emsg(_(e_nomatch));
Bram Moolenaar473de612013-06-08 18:19:48 +02006176 vim_regfree(regmatch.regprog);
Bram Moolenaardf177f62005-02-22 08:39:57 +00006177 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006178 // Move to the NUL, ignore any other arguments.
Bram Moolenaardf177f62005-02-22 08:39:57 +00006179 eap->arg += STRLEN(eap->arg);
6180 }
6181 check_cursor();
6182
6183 eap->cmdidx = CMD_visual;
6184 do_exedit(eap, NULL);
6185}
6186
6187/*
6188 * ":edit", ":badd", ":visual".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006189 */
6190 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006191ex_edit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006192{
6193 do_exedit(eap, NULL);
6194}
6195
6196/*
Bram Moolenaar4b96df52020-01-26 22:00:26 +01006197 * ":edit <file>" command and alike.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006198 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006199 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006200do_exedit(
6201 exarg_T *eap,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006202 win_T *old_curwin) // curwin before doing a split or NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006203{
6204 int n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006205 int need_hide;
Bram Moolenaardf177f62005-02-22 08:39:57 +00006206 int exmode_was = exmode_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006207
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01006208 if ((eap->cmdidx != CMD_pedit && ERROR_IF_POPUP_WINDOW)
6209 || ERROR_IF_TERM_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02006210 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006211 /*
6212 * ":vi" command ends Ex mode.
6213 */
6214 if (exmode_active && (eap->cmdidx == CMD_visual
6215 || eap->cmdidx == CMD_view))
6216 {
6217 exmode_active = FALSE;
Bram Moolenaar158ea172020-06-18 17:28:39 +02006218 ex_pressedreturn = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006219 if (*eap->arg == NUL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00006220 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006221 // Special case: ":global/pat/visual\NLvi-commands"
Bram Moolenaardf177f62005-02-22 08:39:57 +00006222 if (global_busy)
6223 {
6224 int rd = RedrawingDisabled;
6225 int nwr = no_wait_return;
6226 int ms = msg_scroll;
6227#ifdef FEAT_GUI
6228 int he = hold_gui_events;
6229#endif
6230
6231 if (eap->nextcmd != NULL)
6232 {
6233 stuffReadbuff(eap->nextcmd);
6234 eap->nextcmd = NULL;
6235 }
6236
6237 if (exmode_was != EXMODE_VIM)
6238 settmode(TMODE_RAW);
6239 RedrawingDisabled = 0;
6240 no_wait_return = 0;
6241 need_wait_return = FALSE;
6242 msg_scroll = 0;
6243#ifdef FEAT_GUI
6244 hold_gui_events = 0;
6245#endif
6246 must_redraw = CLEAR;
Bram Moolenaar9e2bcb52020-02-18 21:33:00 +01006247 pending_exmode_active = TRUE;
Bram Moolenaardf177f62005-02-22 08:39:57 +00006248
6249 main_loop(FALSE, TRUE);
6250
Bram Moolenaar9e2bcb52020-02-18 21:33:00 +01006251 pending_exmode_active = FALSE;
Bram Moolenaardf177f62005-02-22 08:39:57 +00006252 RedrawingDisabled = rd;
6253 no_wait_return = nwr;
6254 msg_scroll = ms;
6255#ifdef FEAT_GUI
6256 hold_gui_events = he;
6257#endif
6258 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006259 return;
Bram Moolenaardf177f62005-02-22 08:39:57 +00006260 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006261 }
6262
6263 if ((eap->cmdidx == CMD_new
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006264 || eap->cmdidx == CMD_tabnew
6265 || eap->cmdidx == CMD_tabedit
Bram Moolenaar4033c552017-09-16 20:54:51 +02006266 || eap->cmdidx == CMD_vnew) && *eap->arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006267 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006268 // ":new" or ":tabnew" without argument: edit an new empty buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00006269 setpcmark();
6270 (void)do_ecmd(0, NULL, NULL, eap, ECMD_ONE,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00006271 ECMD_HIDE + (eap->forceit ? ECMD_FORCEIT : 0),
6272 old_curwin == NULL ? curwin : NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006273 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02006274 else if ((eap->cmdidx != CMD_split && eap->cmdidx != CMD_vsplit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006275 || *eap->arg != NUL
6276#ifdef FEAT_BROWSE
6277 || cmdmod.browse
6278#endif
6279 )
6280 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006281 // Can't edit another file when "curbuf_lock" is set. Only ":edit"
6282 // can bring us here, others are stopped earlier.
Bram Moolenaar5555acc2006-04-07 21:33:12 +00006283 if (*eap->arg != NUL && curbuf_locked())
6284 return;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006285
Bram Moolenaar071d4272004-06-13 20:20:40 +00006286 n = readonlymode;
6287 if (eap->cmdidx == CMD_view || eap->cmdidx == CMD_sview)
6288 readonlymode = TRUE;
6289 else if (eap->cmdidx == CMD_enew)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006290 readonlymode = FALSE; // 'readonly' doesn't make sense in an
6291 // empty buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00006292 setpcmark();
6293 if (do_ecmd(0, (eap->cmdidx == CMD_enew ? NULL : eap->arg),
6294 NULL, eap,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006295 // ":edit" goes to first line if Vi compatible
Bram Moolenaar071d4272004-06-13 20:20:40 +00006296 (*eap->arg == NUL && eap->do_ecmd_lnum == 0
6297 && vim_strchr(p_cpo, CPO_GOTO1) != NULL)
6298 ? ECMD_ONE : eap->do_ecmd_lnum,
Bram Moolenaareb44a682017-08-03 22:44:55 +02006299 (buf_hide(curbuf) ? ECMD_HIDE : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006300 + (eap->forceit ? ECMD_FORCEIT : 0)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006301 // after a split we can use an existing buffer
Bram Moolenaar7b449342014-03-25 13:03:48 +01006302 + (old_curwin != NULL ? ECMD_OLDBUF : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006303 + (eap->cmdidx == CMD_badd ? ECMD_ADDBUF : 0 )
Bram Moolenaar701f7af2008-11-15 13:12:07 +00006304 , old_curwin == NULL ? curwin : NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006305 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006306 // Editing the file failed. If the window was split, close it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006307 if (old_curwin != NULL)
6308 {
6309 need_hide = (curbufIsChanged() && curbuf->b_nwindows <= 1);
Bram Moolenaareb44a682017-08-03 22:44:55 +02006310 if (!need_hide || buf_hide(curbuf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006311 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006312#if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00006313 cleanup_T cs;
6314
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006315 // Reset the error/interrupt/exception state here so that
6316 // aborting() returns FALSE when closing a window.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00006317 enter_cleanup(&cs);
Bram Moolenaar4033c552017-09-16 20:54:51 +02006318#endif
6319#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00006320 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006321#endif
Bram Moolenaareb44a682017-08-03 22:44:55 +02006322 win_close(curwin, !need_hide && !buf_hide(curbuf));
Bram Moolenaarc0197e22004-09-13 20:26:32 +00006323
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006324#if defined(FEAT_EVAL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006325 // Restore the error/interrupt/exception state if not
6326 // discarded by a new aborting error, interrupt, or
6327 // uncaught exception.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00006328 leave_cleanup(&cs);
Bram Moolenaar4033c552017-09-16 20:54:51 +02006329#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006330 }
6331 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006332 }
6333 else if (readonlymode && curbuf->b_nwindows == 1)
6334 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006335 // When editing an already visited buffer, 'readonly' won't be set
6336 // but the previous value is kept. With ":view" and ":sview" we
6337 // want the file to be readonly, except when another window is
6338 // editing the same buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006339 curbuf->b_p_ro = TRUE;
6340 }
6341 readonlymode = n;
6342 }
6343 else
6344 {
6345 if (eap->do_ecmd_cmd != NULL)
6346 do_cmdline_cmd(eap->do_ecmd_cmd);
6347#ifdef FEAT_TITLE
6348 n = curwin->w_arg_idx_invalid;
6349#endif
6350 check_arg_idx(curwin);
6351#ifdef FEAT_TITLE
6352 if (n != curwin->w_arg_idx_invalid)
6353 maketitle();
6354#endif
6355 }
6356
Bram Moolenaar071d4272004-06-13 20:20:40 +00006357 /*
6358 * if ":split file" worked, set alternate file name in old window to new
6359 * file
6360 */
6361 if (old_curwin != NULL
6362 && *eap->arg != NUL
6363 && curwin != old_curwin
6364 && win_valid(old_curwin)
Bram Moolenaard4755bb2004-09-02 19:12:26 +00006365 && old_curwin->w_buffer != curbuf
6366 && !cmdmod.keepalt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006367 old_curwin->w_alt_fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006368
6369 ex_no_reprint = TRUE;
6370}
6371
6372#ifndef FEAT_GUI
6373/*
6374 * ":gui" and ":gvim" when there is no GUI.
6375 */
6376 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006377ex_nogui(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006378{
6379 eap->errmsg = e_nogvim;
6380}
6381#endif
6382
Bram Moolenaar4f974752019-02-17 17:44:42 +01006383#if defined(FEAT_GUI_MSWIN) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006384 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006385ex_tearoff(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006386{
6387 gui_make_tearoff(eap->arg);
6388}
6389#endif
6390
Bram Moolenaar29a2c082018-03-05 21:06:23 +01006391#if (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK) \
6392 || defined(FEAT_TERM_POPUP_MENU)) && defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006393 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006394ex_popup(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006395{
Bram Moolenaar29a2c082018-03-05 21:06:23 +01006396# if defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK)
6397 if (gui.in_use)
6398 gui_make_popup(eap->arg, eap->forceit);
6399# ifdef FEAT_TERM_POPUP_MENU
6400 else
6401# endif
6402# endif
6403# ifdef FEAT_TERM_POPUP_MENU
6404 pum_make_popup(eap->arg, eap->forceit);
6405# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006406}
6407#endif
6408
Bram Moolenaar071d4272004-06-13 20:20:40 +00006409 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006410ex_swapname(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006411{
6412 if (curbuf->b_ml.ml_mfp == NULL || curbuf->b_ml.ml_mfp->mf_fname == NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01006413 msg(_("No swap file"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006414 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01006415 msg((char *)curbuf->b_ml.ml_mfp->mf_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006416}
6417
6418/*
6419 * ":syncbind" forces all 'scrollbind' windows to have the same relative
6420 * offset.
6421 * (1998-11-02 16:21:01 R. Edward Ralston <eralston@computer.org>)
6422 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006423 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006424ex_syncbind(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006425{
Bram Moolenaar071d4272004-06-13 20:20:40 +00006426 win_T *wp;
Bram Moolenaardedd1b02013-12-14 13:06:17 +01006427 win_T *save_curwin = curwin;
6428 buf_T *save_curbuf = curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006429 long topline;
6430 long y;
6431 linenr_T old_linenr = curwin->w_cursor.lnum;
6432
6433 setpcmark();
6434
6435 /*
6436 * determine max topline
6437 */
6438 if (curwin->w_p_scb)
6439 {
6440 topline = curwin->w_topline;
Bram Moolenaar29323592016-07-24 22:04:11 +02006441 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006442 {
6443 if (wp->w_p_scb && wp->w_buffer)
6444 {
Bram Moolenaar375e3392019-01-31 18:26:10 +01006445 y = wp->w_buffer->b_ml.ml_line_count - get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006446 if (topline > y)
6447 topline = y;
6448 }
6449 }
6450 if (topline < 1)
6451 topline = 1;
6452 }
6453 else
6454 {
6455 topline = 1;
6456 }
6457
6458
6459 /*
Bram Moolenaardedd1b02013-12-14 13:06:17 +01006460 * Set all scrollbind windows to the same topline.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006461 */
Bram Moolenaar29323592016-07-24 22:04:11 +02006462 FOR_ALL_WINDOWS(curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006463 {
6464 if (curwin->w_p_scb)
6465 {
Bram Moolenaardedd1b02013-12-14 13:06:17 +01006466 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006467 y = topline - curwin->w_topline;
6468 if (y > 0)
6469 scrollup(y, TRUE);
6470 else
6471 scrolldown(-y, TRUE);
6472 curwin->w_scbind_pos = topline;
6473 redraw_later(VALID);
6474 cursor_correct();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006475 curwin->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006476 }
6477 }
Bram Moolenaardedd1b02013-12-14 13:06:17 +01006478 curwin = save_curwin;
6479 curbuf = save_curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006480 if (curwin->w_p_scb)
6481 {
6482 did_syncbind = TRUE;
6483 checkpcmark();
6484 if (old_linenr != curwin->w_cursor.lnum)
6485 {
6486 char_u ctrl_o[2];
6487
6488 ctrl_o[0] = Ctrl_O;
6489 ctrl_o[1] = 0;
6490 ins_typebuf(ctrl_o, REMAP_NONE, 0, TRUE, FALSE);
6491 }
6492 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006493}
6494
6495
6496 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006497ex_read(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006498{
Bram Moolenaardf177f62005-02-22 08:39:57 +00006499 int i;
6500 int empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
6501 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006502
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006503 if (eap->usefilter) // :r!cmd
Bram Moolenaar071d4272004-06-13 20:20:40 +00006504 do_bang(1, eap, FALSE, FALSE, TRUE);
6505 else
6506 {
6507 if (u_save(eap->line2, (linenr_T)(eap->line2 + 1)) == FAIL)
6508 return;
6509
6510#ifdef FEAT_BROWSE
6511 if (cmdmod.browse)
6512 {
6513 char_u *browseFile;
6514
Bram Moolenaar7171abe2004-10-11 10:06:20 +00006515 browseFile = do_browse(0, (char_u *)_("Append File"), eap->arg,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006516 NULL, NULL, NULL, curbuf);
6517 if (browseFile != NULL)
6518 {
6519 i = readfile(browseFile, NULL,
6520 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
6521 vim_free(browseFile);
6522 }
6523 else
6524 i = OK;
6525 }
6526 else
6527#endif
6528 if (*eap->arg == NUL)
6529 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006530 if (check_fname() == FAIL) // check for no file name
Bram Moolenaar071d4272004-06-13 20:20:40 +00006531 return;
6532 i = readfile(curbuf->b_ffname, curbuf->b_fname,
6533 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
6534 }
6535 else
6536 {
6537 if (vim_strchr(p_cpo, CPO_ALTREAD) != NULL)
6538 (void)setaltfname(eap->arg, eap->arg, (linenr_T)1);
6539 i = readfile(eap->arg, NULL,
6540 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
6541
6542 }
Bram Moolenaare13b9af2017-01-13 22:01:02 +01006543 if (i != OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006544 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006545#if defined(FEAT_EVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006546 if (!aborting())
6547#endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006548 semsg(_(e_notopen), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006549 }
6550 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00006551 {
6552 if (empty && exmode_active)
6553 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006554 // Delete the empty line that remains. Historically ex does
6555 // this but vi doesn't.
Bram Moolenaardf177f62005-02-22 08:39:57 +00006556 if (eap->line2 == 0)
6557 lnum = curbuf->b_ml.ml_line_count;
6558 else
6559 lnum = 1;
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00006560 if (*ml_get(lnum) == NUL && u_savedel(lnum, 1L) == OK)
Bram Moolenaardf177f62005-02-22 08:39:57 +00006561 {
Bram Moolenaarca70c072020-05-30 20:30:46 +02006562 ml_delete(lnum);
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00006563 if (curwin->w_cursor.lnum > 1
6564 && curwin->w_cursor.lnum >= lnum)
Bram Moolenaardf177f62005-02-22 08:39:57 +00006565 --curwin->w_cursor.lnum;
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00006566 deleted_lines_mark(lnum, 1L);
Bram Moolenaardf177f62005-02-22 08:39:57 +00006567 }
6568 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006569 redraw_curbuf_later(VALID);
Bram Moolenaardf177f62005-02-22 08:39:57 +00006570 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006571 }
6572}
6573
Bram Moolenaarea408852005-06-25 22:49:46 +00006574static char_u *prev_dir = NULL;
6575
6576#if defined(EXITFREE) || defined(PROTO)
6577 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006578free_cd_dir(void)
Bram Moolenaarea408852005-06-25 22:49:46 +00006579{
Bram Moolenaard23a8232018-02-10 18:45:26 +01006580 VIM_CLEAR(prev_dir);
6581 VIM_CLEAR(globaldir);
Bram Moolenaarea408852005-06-25 22:49:46 +00006582}
6583#endif
6584
Bram Moolenaarf4258302013-06-02 18:20:17 +02006585/*
Bram Moolenaar002bc792020-06-05 22:33:42 +02006586 * Get the previous directory for the given chdir scope.
6587 */
6588 static char_u *
6589get_prevdir(cdscope_T scope)
6590{
6591 if (scope == CDSCOPE_WINDOW)
6592 return curwin->w_prevdir;
6593 else if (scope == CDSCOPE_TABPAGE)
6594 return curtab->tp_prevdir;
6595 return prev_dir;
6596}
6597
6598/*
Bram Moolenaarf4258302013-06-02 18:20:17 +02006599 * Deal with the side effects of changing the current directory.
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006600 * When 'scope' is CDSCOPE_TABPAGE then this was after an ":tcd" command.
6601 * When 'scope' is CDSCOPE_WINDOW then this was after an ":lcd" command.
Bram Moolenaarf4258302013-06-02 18:20:17 +02006602 */
6603 void
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006604post_chdir(cdscope_T scope)
Bram Moolenaarf4258302013-06-02 18:20:17 +02006605{
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006606 if (scope != CDSCOPE_WINDOW)
Bram Moolenaar00aa0692019-04-27 20:37:57 +02006607 // Clear tab local directory for both :cd and :tcd
6608 VIM_CLEAR(curtab->tp_localdir);
Bram Moolenaard23a8232018-02-10 18:45:26 +01006609 VIM_CLEAR(curwin->w_localdir);
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006610 if (scope != CDSCOPE_GLOBAL)
Bram Moolenaarf4258302013-06-02 18:20:17 +02006611 {
Bram Moolenaar002bc792020-06-05 22:33:42 +02006612 char_u *pdir = get_prevdir(scope);
6613
6614 // If still in the global directory, need to remember current
6615 // directory as the global directory.
6616 if (globaldir == NULL && pdir != NULL)
6617 globaldir = vim_strsave(pdir);
6618
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006619 // Remember this local directory for the window.
Bram Moolenaarf4258302013-06-02 18:20:17 +02006620 if (mch_dirname(NameBuff, MAXPATHL) == OK)
Bram Moolenaar00aa0692019-04-27 20:37:57 +02006621 {
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006622 if (scope == CDSCOPE_TABPAGE)
Bram Moolenaar00aa0692019-04-27 20:37:57 +02006623 curtab->tp_localdir = vim_strsave(NameBuff);
6624 else
6625 curwin->w_localdir = vim_strsave(NameBuff);
6626 }
Bram Moolenaarf4258302013-06-02 18:20:17 +02006627 }
6628 else
6629 {
Bram Moolenaar002bc792020-06-05 22:33:42 +02006630 // We are now in the global directory, no need to remember its name.
Bram Moolenaard23a8232018-02-10 18:45:26 +01006631 VIM_CLEAR(globaldir);
Bram Moolenaarf4258302013-06-02 18:20:17 +02006632 }
6633
6634 shorten_fnames(TRUE);
6635}
6636
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006637/*
6638 * Change directory function used by :cd/:tcd/:lcd Ex commands and the
Bram Moolenaar95058722020-06-01 16:26:19 +02006639 * chdir() function.
6640 * scope == CDSCOPE_WINDOW: changes the window-local directory
6641 * scope == CDSCOPE_TABPAGE: changes the tab-local directory
6642 * Otherwise: changes the global directory
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006643 * Returns TRUE if the directory is successfully changed.
6644 */
6645 int
6646changedir_func(
6647 char_u *new_dir,
6648 int forceit,
6649 cdscope_T scope)
6650{
6651 char_u *tofree;
Bram Moolenaar002bc792020-06-05 22:33:42 +02006652 char_u *pdir = NULL;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006653 int dir_differs;
6654 int retval = FALSE;
6655
Bram Moolenaar7cc96922020-01-31 22:41:38 +01006656 if (new_dir == NULL || allbuf_locked())
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006657 return FALSE;
6658
6659 if (vim_strchr(p_cpo, CPO_CHDIR) != NULL && curbufIsChanged() && !forceit)
6660 {
6661 emsg(_("E747: Cannot change directory, buffer is modified (add ! to override)"));
6662 return FALSE;
6663 }
6664
6665 // ":cd -": Change to previous directory
6666 if (STRCMP(new_dir, "-") == 0)
6667 {
Bram Moolenaar002bc792020-06-05 22:33:42 +02006668 pdir = get_prevdir(scope);
6669 if (pdir == NULL)
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006670 {
6671 emsg(_("E186: No previous directory"));
6672 return FALSE;
6673 }
Bram Moolenaar002bc792020-06-05 22:33:42 +02006674 new_dir = pdir;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006675 }
6676
Bram Moolenaar002bc792020-06-05 22:33:42 +02006677 // Free the previous directory
6678 tofree = get_prevdir(scope);
6679
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006680 // Save current directory for next ":cd -"
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006681 if (mch_dirname(NameBuff, MAXPATHL) == OK)
Bram Moolenaar002bc792020-06-05 22:33:42 +02006682 pdir = vim_strsave(NameBuff);
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006683 else
Bram Moolenaar002bc792020-06-05 22:33:42 +02006684 pdir = NULL;
6685 if (scope == CDSCOPE_WINDOW)
6686 curwin->w_prevdir = pdir;
6687 else if (scope == CDSCOPE_TABPAGE)
6688 curtab->tp_prevdir = pdir;
6689 else
6690 prev_dir = pdir;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006691
6692#if defined(UNIX) || defined(VMS)
6693 // for UNIX ":cd" means: go to home directory
6694 if (*new_dir == NUL)
6695 {
6696 // use NameBuff for home directory name
6697# ifdef VMS
6698 char_u *p;
6699
6700 p = mch_getenv((char_u *)"SYS$LOGIN");
6701 if (p == NULL || *p == NUL) // empty is the same as not set
6702 NameBuff[0] = NUL;
6703 else
6704 vim_strncpy(NameBuff, p, MAXPATHL - 1);
6705# else
6706 expand_env((char_u *)"$HOME", NameBuff, MAXPATHL);
6707# endif
6708 new_dir = NameBuff;
6709 }
6710#endif
Bram Moolenaar002bc792020-06-05 22:33:42 +02006711 dir_differs = new_dir == NULL || pdir == NULL
6712 || pathcmp((char *)pdir, (char *)new_dir, -1) != 0;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006713 if (new_dir == NULL || (dir_differs && vim_chdir(new_dir)))
6714 emsg(_(e_failed));
6715 else
6716 {
6717 char_u *acmd_fname;
6718
6719 post_chdir(scope);
6720
6721 if (dir_differs)
6722 {
6723 if (scope == CDSCOPE_WINDOW)
6724 acmd_fname = (char_u *)"window";
6725 else if (scope == CDSCOPE_TABPAGE)
6726 acmd_fname = (char_u *)"tabpage";
6727 else
6728 acmd_fname = (char_u *)"global";
6729 apply_autocmds(EVENT_DIRCHANGED, acmd_fname, new_dir, FALSE,
6730 curbuf);
6731 }
6732 retval = TRUE;
6733 }
6734 vim_free(tofree);
6735
6736 return retval;
6737}
Bram Moolenaarea408852005-06-25 22:49:46 +00006738
Bram Moolenaar071d4272004-06-13 20:20:40 +00006739/*
Bram Moolenaar00aa0692019-04-27 20:37:57 +02006740 * ":cd", ":tcd", ":lcd", ":chdir" ":tchdir" and ":lchdir".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006741 */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00006742 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006743ex_cd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006744{
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01006745 char_u *new_dir;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006746
6747 new_dir = eap->arg;
6748#if !defined(UNIX) && !defined(VMS)
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006749 // for non-UNIX ":cd" means: print current directory
Bram Moolenaar071d4272004-06-13 20:20:40 +00006750 if (*new_dir == NUL)
6751 ex_pwd(NULL);
6752 else
6753#endif
6754 {
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006755 cdscope_T scope = CDSCOPE_GLOBAL;
6756
6757 if (eap->cmdidx == CMD_lcd || eap->cmdidx == CMD_lchdir)
6758 scope = CDSCOPE_WINDOW;
6759 else if (eap->cmdidx == CMD_tcd || eap->cmdidx == CMD_tchdir)
6760 scope = CDSCOPE_TABPAGE;
6761
6762 if (changedir_func(new_dir, eap->forceit, scope))
Bram Moolenaardf177f62005-02-22 08:39:57 +00006763 {
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006764 // Echo the new current directory if the command was typed.
Bram Moolenaarfcfbc672009-07-09 18:13:49 +00006765 if (KeyTyped || p_verbose >= 5)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006766 ex_pwd(eap);
6767 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006768 }
6769}
6770
6771/*
6772 * ":pwd".
6773 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006774 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006775ex_pwd(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006776{
6777 if (mch_dirname(NameBuff, MAXPATHL) == OK)
6778 {
6779#ifdef BACKSLASH_IN_FILENAME
6780 slash_adjust(NameBuff);
6781#endif
Bram Moolenaar95058722020-06-01 16:26:19 +02006782 if (p_verbose > 0)
6783 {
6784 char *context = "global";
6785
6786 if (curwin->w_localdir != NULL)
6787 context = "window";
6788 else if (curtab->tp_localdir != NULL)
6789 context = "tabpage";
6790 smsg("[%s] %s", context, (char *)NameBuff);
6791 }
6792 else
6793 msg((char *)NameBuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006794 }
6795 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006796 emsg(_("E187: Unknown"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006797}
6798
6799/*
6800 * ":=".
6801 */
6802 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006803ex_equal(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006804{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006805 smsg("%ld", (long)eap->line2);
Bram Moolenaardf177f62005-02-22 08:39:57 +00006806 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006807}
6808
6809 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006810ex_sleep(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006811{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006812 int n;
6813 long len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006814
6815 if (cursor_valid())
6816 {
6817 n = W_WINROW(curwin) + curwin->w_wrow - msg_scrolled;
6818 if (n >= 0)
Bram Moolenaar53f81742017-09-22 14:35:51 +02006819 windgoto((int)n, curwin->w_wincol + curwin->w_wcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006820 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006821
6822 len = eap->line2;
6823 switch (*eap->arg)
6824 {
6825 case 'm': break;
6826 case NUL: len *= 1000L; break;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006827 default: semsg(_(e_invarg2), eap->arg); return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006828 }
6829 do_sleep(len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006830}
6831
6832/*
6833 * Sleep for "msec" milliseconds, but keep checking for a CTRL-C every second.
6834 */
6835 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006836do_sleep(long msec)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006837{
Bram Moolenaar52953192019-08-16 10:27:13 +02006838 long done = 0;
Bram Moolenaar975b5272016-03-15 23:10:59 +01006839 long wait_now;
Bram Moolenaar52953192019-08-16 10:27:13 +02006840# ifdef ELAPSED_FUNC
6841 elapsed_T start_tv;
6842
6843 // Remember at what time we started, so that we know how much longer we
6844 // should wait after waiting for a bit.
6845 ELAPSED_INIT(start_tv);
6846# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006847
6848 cursor_on();
Bram Moolenaarf45d7472018-09-30 18:22:26 +02006849 out_flush_cursor(FALSE, FALSE);
Bram Moolenaar52953192019-08-16 10:27:13 +02006850 while (!got_int && done < msec)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006851 {
Bram Moolenaar975b5272016-03-15 23:10:59 +01006852 wait_now = msec - done > 1000L ? 1000L : msec - done;
6853#ifdef FEAT_TIMERS
6854 {
6855 long due_time = check_due_timer();
6856
6857 if (due_time > 0 && due_time < wait_now)
6858 wait_now = due_time;
6859 }
6860#endif
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02006861#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar28e67e02019-08-15 23:05:49 +02006862 if (has_any_channel() && wait_now > 20L)
6863 wait_now = 20L;
6864#endif
6865#ifdef FEAT_SOUND
6866 if (has_any_sound_callback() && wait_now > 20L)
6867 wait_now = 20L;
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02006868#endif
Bram Moolenaar975b5272016-03-15 23:10:59 +01006869 ui_delay(wait_now, TRUE);
Bram Moolenaar52953192019-08-16 10:27:13 +02006870
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02006871#ifdef FEAT_JOB_CHANNEL
6872 if (has_any_channel())
6873 ui_breakcheck_force(TRUE);
6874 else
6875#endif
6876 ui_breakcheck();
Bram Moolenaar93c88e02015-09-15 14:12:05 +02006877#ifdef MESSAGE_QUEUE
Bram Moolenaar52953192019-08-16 10:27:13 +02006878 // Process the netbeans and clientserver messages that may have been
6879 // received in the call to ui_breakcheck() when the GUI is in use. This
6880 // may occur when running a test case.
Bram Moolenaar93c88e02015-09-15 14:12:05 +02006881 parse_queued_messages();
Bram Moolenaare3cc6d42011-10-20 21:58:34 +02006882#endif
Bram Moolenaar52953192019-08-16 10:27:13 +02006883
6884# ifdef ELAPSED_FUNC
6885 // actual time passed
6886 done = ELAPSED_FUNC(start_tv);
6887# else
6888 // guestimate time passed (will actually be more)
6889 done += wait_now;
6890# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006891 }
Bram Moolenaar1ebff3d2018-07-07 16:18:13 +02006892
6893 // If CTRL-C was typed to interrupt the sleep, drop the CTRL-C from the
6894 // input buffer, otherwise a following call to input() fails.
6895 if (got_int)
6896 (void)vpeekc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006897}
6898
Bram Moolenaar071d4272004-06-13 20:20:40 +00006899/*
6900 * ":winsize" command (obsolete).
6901 */
6902 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006903ex_winsize(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006904{
6905 int w, h;
6906 char_u *arg = eap->arg;
6907 char_u *p;
6908
6909 w = getdigits(&arg);
6910 arg = skipwhite(arg);
6911 p = arg;
6912 h = getdigits(&arg);
6913 if (*p != NUL && *arg == NUL)
6914 set_shellsize(w, h, TRUE);
6915 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006916 emsg(_("E465: :winsize requires two number arguments"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006917}
6918
Bram Moolenaar071d4272004-06-13 20:20:40 +00006919 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006920ex_wincmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006921{
6922 int xchar = NUL;
6923 char_u *p;
6924
6925 if (*eap->arg == 'g' || *eap->arg == Ctrl_G)
6926 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006927 // CTRL-W g and CTRL-W CTRL-G have an extra command character
Bram Moolenaar071d4272004-06-13 20:20:40 +00006928 if (eap->arg[1] == NUL)
6929 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006930 emsg(_(e_invarg));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006931 return;
6932 }
6933 xchar = eap->arg[1];
6934 p = eap->arg + 2;
6935 }
6936 else
6937 p = eap->arg + 1;
6938
6939 eap->nextcmd = check_nextcmd(p);
6940 p = skipwhite(p);
6941 if (*p != NUL && *p != '"' && eap->nextcmd == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006942 emsg(_(e_invarg));
Bram Moolenaar12bde492011-06-13 01:19:56 +02006943 else if (!eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006944 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006945 // Pass flags on for ":vertical wincmd ]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006946 postponed_split_flags = cmdmod.split;
Bram Moolenaard326ce82007-03-11 14:48:29 +00006947 postponed_split_tab = cmdmod.tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006948 do_window(*eap->arg, eap->addr_count > 0 ? eap->line2 : 0L, xchar);
6949 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00006950 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006951 }
6952}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006953
Bram Moolenaar843ee412004-06-30 16:16:41 +00006954#if defined(FEAT_GUI) || defined(UNIX) || defined(VMS) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006955/*
6956 * ":winpos".
6957 */
6958 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006959ex_winpos(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006960{
6961 int x, y;
6962 char_u *arg = eap->arg;
6963 char_u *p;
6964
6965 if (*arg == NUL)
6966 {
Bram Moolenaar843ee412004-06-30 16:16:41 +00006967# if defined(FEAT_GUI) || defined(MSWIN)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02006968# ifdef VIMDLL
6969 if (gui.in_use ? gui_mch_get_winpos(&x, &y) != FAIL :
6970 mch_get_winpos(&x, &y) != FAIL)
6971# elif defined(FEAT_GUI)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006972 if (gui.in_use && gui_mch_get_winpos(&x, &y) != FAIL)
Bram Moolenaar843ee412004-06-30 16:16:41 +00006973# else
6974 if (mch_get_winpos(&x, &y) != FAIL)
6975# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006976 {
6977 sprintf((char *)IObuff, _("Window position: X %d, Y %d"), x, y);
Bram Moolenaar32526b32019-01-19 17:43:09 +01006978 msg((char *)IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006979 }
6980 else
6981# endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006982 emsg(_("E188: Obtaining window position not implemented for this platform"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006983 }
6984 else
6985 {
6986 x = getdigits(&arg);
6987 arg = skipwhite(arg);
6988 p = arg;
6989 y = getdigits(&arg);
6990 if (*p == NUL || *arg != NUL)
6991 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006992 emsg(_("E466: :winpos requires two number arguments"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006993 return;
6994 }
6995# ifdef FEAT_GUI
6996 if (gui.in_use)
6997 gui_mch_set_winpos(x, y);
6998 else if (gui.starting)
6999 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007000 // Remember the coordinates for when the window is opened.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007001 gui_win_x = x;
7002 gui_win_y = y;
7003 }
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007004# if defined(HAVE_TGETENT) || defined(VIMDLL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007005 else
7006# endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007007# endif
7008# if defined(MSWIN) && (!defined(FEAT_GUI) || defined(VIMDLL))
Bram Moolenaar843ee412004-06-30 16:16:41 +00007009 mch_set_winpos(x, y);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007010# endif
7011# ifdef HAVE_TGETENT
7012 if (*T_CWP)
7013 term_set_winpos(x, y);
7014# endif
7015 }
7016}
7017#endif
7018
7019/*
7020 * Handle command that work like operators: ":delete", ":yank", ":>" and ":<".
7021 */
7022 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007023ex_operators(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007024{
7025 oparg_T oa;
7026
7027 clear_oparg(&oa);
7028 oa.regname = eap->regname;
7029 oa.start.lnum = eap->line1;
7030 oa.end.lnum = eap->line2;
7031 oa.line_count = eap->line2 - eap->line1 + 1;
7032 oa.motion_type = MLINE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007033 virtual_op = FALSE;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007034 if (eap->cmdidx != CMD_yank) // position cursor for undo
Bram Moolenaar071d4272004-06-13 20:20:40 +00007035 {
7036 setpcmark();
7037 curwin->w_cursor.lnum = eap->line1;
7038 beginline(BL_SOL | BL_FIX);
7039 }
7040
Bram Moolenaard07c6e12013-11-21 14:21:40 +01007041 if (VIsual_active)
7042 end_visual_mode();
Bram Moolenaard07c6e12013-11-21 14:21:40 +01007043
Bram Moolenaar071d4272004-06-13 20:20:40 +00007044 switch (eap->cmdidx)
7045 {
7046 case CMD_delete:
7047 oa.op_type = OP_DELETE;
7048 op_delete(&oa);
7049 break;
7050
7051 case CMD_yank:
7052 oa.op_type = OP_YANK;
7053 (void)op_yank(&oa, FALSE, TRUE);
7054 break;
7055
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007056 default: // CMD_rshift or CMD_lshift
Bram Moolenaar6e707362013-06-14 19:15:58 +02007057 if (
Bram Moolenaar071d4272004-06-13 20:20:40 +00007058#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6e707362013-06-14 19:15:58 +02007059 (eap->cmdidx == CMD_rshift) ^ curwin->w_p_rl
7060#else
7061 eap->cmdidx == CMD_rshift
Bram Moolenaar071d4272004-06-13 20:20:40 +00007062#endif
Bram Moolenaar6e707362013-06-14 19:15:58 +02007063 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00007064 oa.op_type = OP_RSHIFT;
7065 else
7066 oa.op_type = OP_LSHIFT;
7067 op_shift(&oa, FALSE, eap->amount);
7068 break;
7069 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007070 virtual_op = MAYBE;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007071 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007072}
7073
7074/*
7075 * ":put".
7076 */
7077 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007078ex_put(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007079{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007080 // ":0put" works like ":1put!".
Bram Moolenaar071d4272004-06-13 20:20:40 +00007081 if (eap->line2 == 0)
7082 {
7083 eap->line2 = 1;
7084 eap->forceit = TRUE;
7085 }
7086 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007087 do_put(eap->regname, eap->forceit ? BACKWARD : FORWARD, 1L,
7088 PUT_LINE|PUT_CURSLINE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007089}
7090
7091/*
7092 * Handle ":copy" and ":move".
7093 */
7094 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007095ex_copymove(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007096{
7097 long n;
7098
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02007099 n = get_address(eap, &eap->arg, eap->addr_type, FALSE, FALSE, FALSE, 1);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007100 if (eap->arg == NULL) // error detected
Bram Moolenaar071d4272004-06-13 20:20:40 +00007101 {
7102 eap->nextcmd = NULL;
7103 return;
7104 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00007105 get_flags(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007106
7107 /*
7108 * move or copy lines from 'eap->line1'-'eap->line2' to below line 'n'
7109 */
7110 if (n == MAXLNUM || n < 0 || n > curbuf->b_ml.ml_line_count)
7111 {
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02007112 emsg(_(e_invrange));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007113 return;
7114 }
7115
7116 if (eap->cmdidx == CMD_move)
7117 {
7118 if (do_move(eap->line1, eap->line2, n) == FAIL)
7119 return;
7120 }
7121 else
7122 ex_copy(eap->line1, eap->line2, n);
7123 u_clearline();
7124 beginline(BL_SOL | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007125 ex_may_print(eap);
7126}
7127
7128/*
7129 * Print the current line if flags were given to the Ex command.
7130 */
Bram Moolenaarfd3fe982014-04-01 17:49:44 +02007131 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007132ex_may_print(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007133{
7134 if (eap->flags != 0)
7135 {
7136 print_line(curwin->w_cursor.lnum, (eap->flags & EXFLAG_NR),
7137 (eap->flags & EXFLAG_LIST));
7138 ex_no_reprint = TRUE;
7139 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007140}
7141
7142/*
7143 * ":smagic" and ":snomagic".
7144 */
7145 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007146ex_submagic(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007147{
7148 int magic_save = p_magic;
7149
7150 p_magic = (eap->cmdidx == CMD_smagic);
7151 do_sub(eap);
7152 p_magic = magic_save;
7153}
7154
7155/*
7156 * ":join".
7157 */
7158 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007159ex_join(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007160{
7161 curwin->w_cursor.lnum = eap->line1;
7162 if (eap->line1 == eap->line2)
7163 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007164 if (eap->addr_count >= 2) // :2,2join does nothing
Bram Moolenaar071d4272004-06-13 20:20:40 +00007165 return;
7166 if (eap->line2 == curbuf->b_ml.ml_line_count)
7167 {
7168 beep_flush();
7169 return;
7170 }
7171 ++eap->line2;
7172 }
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02007173 (void)do_join(eap->line2 - eap->line1 + 1, !eap->forceit, TRUE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007174 beginline(BL_WHITE | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007175 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007176}
7177
7178/*
7179 * ":[addr]@r" or ":[addr]*r": execute register
7180 */
7181 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007182ex_at(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007183{
7184 int c;
Bram Moolenaar60462872009-11-03 11:40:19 +00007185 int prev_len = typebuf.tb_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007186
7187 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaar4930a762016-09-11 14:39:53 +02007188 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007189
7190#ifdef USE_ON_FLY_SCROLL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007191 dont_scroll = TRUE; // disallow scrolling here
Bram Moolenaar071d4272004-06-13 20:20:40 +00007192#endif
7193
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007194 // get the register name. No name means to use the previous one
Bram Moolenaar071d4272004-06-13 20:20:40 +00007195 c = *eap->arg;
7196 if (c == NUL || (c == '*' && *eap->cmd == '*'))
7197 c = '@';
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007198 // Put the register in the typeahead buffer with the "silent" flag.
Bram Moolenaard333d1e2006-11-07 17:43:47 +00007199 if (do_execreg(c, TRUE, vim_strchr(p_cpo, CPO_EXECBUF) != NULL, TRUE)
7200 == FAIL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007201 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007202 beep_flush();
Bram Moolenaardf177f62005-02-22 08:39:57 +00007203 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007204 else
7205 {
7206 int save_efr = exec_from_reg;
7207
7208 exec_from_reg = TRUE;
7209
7210 /*
7211 * Execute from the typeahead buffer.
Bram Moolenaar60462872009-11-03 11:40:19 +00007212 * Continue until the stuff buffer is empty and all added characters
7213 * have been consumed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007214 */
Bram Moolenaar60462872009-11-03 11:40:19 +00007215 while (!stuff_empty() || typebuf.tb_len > prev_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007216 (void)do_cmdline(NULL, getexline, NULL, DOCMD_NOWAIT|DOCMD_VERBOSE);
7217
7218 exec_from_reg = save_efr;
7219 }
7220}
7221
7222/*
7223 * ":!".
7224 */
7225 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007226ex_bang(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007227{
7228 do_bang(eap->addr_count, eap, eap->forceit, TRUE, TRUE);
7229}
7230
7231/*
7232 * ":undo".
7233 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007234 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +01007235ex_undo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007236{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007237 if (eap->addr_count == 1) // :undo 123
Bram Moolenaar730cde92010-06-27 05:18:54 +02007238 undo_time(eap->line2, FALSE, FALSE, TRUE);
Bram Moolenaard3667a22006-03-16 21:35:52 +00007239 else
7240 u_undo(1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007241}
7242
Bram Moolenaar55debbe2010-05-23 23:34:36 +02007243#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar6df6f472010-07-18 18:04:50 +02007244 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007245ex_wundo(exarg_T *eap)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02007246{
7247 char_u hash[UNDO_HASH_SIZE];
7248
7249 u_compute_hash(hash);
7250 u_write_undo(eap->arg, eap->forceit, curbuf, hash);
7251}
7252
Bram Moolenaar6df6f472010-07-18 18:04:50 +02007253 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007254ex_rundo(exarg_T *eap)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02007255{
7256 char_u hash[UNDO_HASH_SIZE];
7257
7258 u_compute_hash(hash);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02007259 u_read_undo(eap->arg, hash, NULL);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02007260}
7261#endif
7262
Bram Moolenaar071d4272004-06-13 20:20:40 +00007263/*
7264 * ":redo".
7265 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007266 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007267ex_redo(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007268{
7269 u_redo(1);
7270}
7271
7272/*
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007273 * ":earlier" and ":later".
7274 */
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007275 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007276ex_later(exarg_T *eap)
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007277{
7278 long count = 0;
7279 int sec = FALSE;
Bram Moolenaar730cde92010-06-27 05:18:54 +02007280 int file = FALSE;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007281 char_u *p = eap->arg;
7282
7283 if (*p == NUL)
7284 count = 1;
7285 else if (isdigit(*p))
7286 {
7287 count = getdigits(&p);
7288 switch (*p)
7289 {
7290 case 's': ++p; sec = TRUE; break;
7291 case 'm': ++p; sec = TRUE; count *= 60; break;
7292 case 'h': ++p; sec = TRUE; count *= 60 * 60; break;
Bram Moolenaar730cde92010-06-27 05:18:54 +02007293 case 'd': ++p; sec = TRUE; count *= 24 * 60 * 60; break;
7294 case 'f': ++p; file = TRUE; break;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007295 }
7296 }
7297
7298 if (*p != NUL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007299 semsg(_(e_invarg2), eap->arg);
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007300 else
Bram Moolenaar730cde92010-06-27 05:18:54 +02007301 undo_time(eap->cmdidx == CMD_earlier ? -count : count,
7302 sec, file, FALSE);
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007303}
7304
7305/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007306 * ":redir": start/stop redirection.
7307 */
7308 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007309ex_redir(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007310{
7311 char *mode;
7312 char_u *fname;
Bram Moolenaarca472992005-01-21 11:46:23 +00007313 char_u *arg = eap->arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007314
Bram Moolenaarba768492016-07-08 15:32:54 +02007315#ifdef FEAT_EVAL
Bram Moolenaar79815f12016-07-09 17:07:29 +02007316 if (redir_execute)
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02007317 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007318 emsg(_("E930: Cannot use :redir inside execute()"));
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02007319 return;
7320 }
Bram Moolenaarba768492016-07-08 15:32:54 +02007321#endif
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02007322
Bram Moolenaar071d4272004-06-13 20:20:40 +00007323 if (STRICMP(eap->arg, "END") == 0)
7324 close_redir();
7325 else
7326 {
Bram Moolenaarca472992005-01-21 11:46:23 +00007327 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00007328 {
Bram Moolenaarca472992005-01-21 11:46:23 +00007329 ++arg;
7330 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00007331 {
Bram Moolenaarca472992005-01-21 11:46:23 +00007332 ++arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007333 mode = "a";
7334 }
7335 else
7336 mode = "w";
Bram Moolenaarca472992005-01-21 11:46:23 +00007337 arg = skipwhite(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007338
7339 close_redir();
7340
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007341 // Expand environment variables and "~/".
Bram Moolenaarca472992005-01-21 11:46:23 +00007342 fname = expand_env_save(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007343 if (fname == NULL)
7344 return;
7345#ifdef FEAT_BROWSE
7346 if (cmdmod.browse)
7347 {
7348 char_u *browseFile;
7349
Bram Moolenaar7171abe2004-10-11 10:06:20 +00007350 browseFile = do_browse(BROWSE_SAVE,
7351 (char_u *)_("Save Redirection"),
Bram Moolenaarc36651b2018-04-29 12:22:56 +02007352 fname, NULL, NULL,
7353 (char_u *)_(BROWSE_FILTER_ALL_FILES), curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007354 if (browseFile == NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007355 return; // operation cancelled
Bram Moolenaar071d4272004-06-13 20:20:40 +00007356 vim_free(fname);
7357 fname = browseFile;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007358 eap->forceit = TRUE; // since dialog already asked
Bram Moolenaar071d4272004-06-13 20:20:40 +00007359 }
7360#endif
7361
7362 redir_fd = open_exfile(fname, eap->forceit, mode);
7363 vim_free(fname);
7364 }
7365#ifdef FEAT_EVAL
Bram Moolenaarca472992005-01-21 11:46:23 +00007366 else if (*arg == '@')
Bram Moolenaar071d4272004-06-13 20:20:40 +00007367 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007368 // redirect to a register a-z (resp. A-Z for appending)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007369 close_redir();
Bram Moolenaarca472992005-01-21 11:46:23 +00007370 ++arg;
7371 if (ASCII_ISALPHA(*arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007372# ifdef FEAT_CLIPBOARD
Bram Moolenaarca472992005-01-21 11:46:23 +00007373 || *arg == '*'
Bram Moolenaar9a51c6e2006-11-14 19:25:02 +00007374 || *arg == '+'
Bram Moolenaar071d4272004-06-13 20:20:40 +00007375# endif
Bram Moolenaarca472992005-01-21 11:46:23 +00007376 || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00007377 {
Bram Moolenaarca472992005-01-21 11:46:23 +00007378 redir_reg = *arg++;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007379 if (*arg == '>' && arg[1] == '>') // append
Bram Moolenaard9d30582005-05-18 22:10:28 +00007380 arg += 2;
Bram Moolenaarc188b882007-10-19 14:20:54 +00007381 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007382 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007383 // Can use both "@a" and "@a>".
Bram Moolenaar2c29bee2005-06-01 21:46:07 +00007384 if (*arg == '>')
7385 arg++;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007386 // Make register empty when not using @A-@Z and the
7387 // command is valid.
Bram Moolenaarc188b882007-10-19 14:20:54 +00007388 if (*arg == NUL && !isupper(redir_reg))
7389 write_reg_contents(redir_reg, (char_u *)"", -1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007390 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00007391 }
7392 if (*arg != NUL)
7393 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00007394 redir_reg = 0;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007395 semsg(_(e_invarg2), eap->arg);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007396 }
7397 }
7398 else if (*arg == '=' && arg[1] == '>')
7399 {
7400 int append;
7401
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007402 // redirect to a variable
Bram Moolenaardf177f62005-02-22 08:39:57 +00007403 close_redir();
7404 arg += 2;
7405
7406 if (*arg == '>')
7407 {
7408 ++arg;
7409 append = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007410 }
7411 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00007412 append = FALSE;
7413
7414 if (var_redir_start(skipwhite(arg), append) == OK)
7415 redir_vname = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007416 }
7417#endif
7418
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007419 // TODO: redirect to a buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00007420
Bram Moolenaar071d4272004-06-13 20:20:40 +00007421 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007422 semsg(_(e_invarg2), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007423 }
Bram Moolenaar29b2d262006-09-10 19:07:28 +00007424
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007425 // Make sure redirection is not off. Can happen for cmdline completion
7426 // that indirectly invokes a command to catch its output.
Bram Moolenaar29b2d262006-09-10 19:07:28 +00007427 if (redir_fd != NULL
7428#ifdef FEAT_EVAL
7429 || redir_reg || redir_vname
7430#endif
7431 )
7432 redir_off = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007433}
7434
7435/*
7436 * ":redraw": force redraw
7437 */
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01007438 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007439ex_redraw(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007440{
7441 int r = RedrawingDisabled;
7442 int p = p_lz;
7443
7444 RedrawingDisabled = 0;
7445 p_lz = FALSE;
Bram Moolenaarabc39ab2017-03-01 18:04:05 +01007446 validate_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007447 update_topline();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007448 update_screen(eap->forceit ? CLEAR : VIsual_active ? INVERTED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007449#ifdef FEAT_TITLE
7450 if (need_maketitle)
7451 maketitle();
7452#endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007453#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
7454# ifdef VIMDLL
7455 if (!gui.in_use)
7456# endif
7457 resize_console_buf();
Bram Moolenaar78d21da2019-02-17 15:00:52 +01007458#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007459 RedrawingDisabled = r;
7460 p_lz = p;
7461
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007462 // Reset msg_didout, so that a message that's there is overwritten.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007463 msg_didout = FALSE;
7464 msg_col = 0;
7465
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007466 // No need to wait after an intentional redraw.
Bram Moolenaar56667a52013-07-17 11:54:28 +02007467 need_wait_return = FALSE;
7468
Bram Moolenaar071d4272004-06-13 20:20:40 +00007469 out_flush();
7470}
7471
7472/*
7473 * ":redrawstatus": force redraw of status line(s)
7474 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007475 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007476ex_redrawstatus(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007477{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007478 int r = RedrawingDisabled;
7479 int p = p_lz;
7480
7481 RedrawingDisabled = 0;
7482 p_lz = FALSE;
7483 if (eap->forceit)
7484 status_redraw_all();
7485 else
7486 status_redraw_curbuf();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007487 update_screen(VIsual_active ? INVERTED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007488 RedrawingDisabled = r;
7489 p_lz = p;
7490 out_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007491}
7492
Bram Moolenaare12bab32019-01-08 22:02:56 +01007493/*
7494 * ":redrawtabline": force redraw of the tabline
7495 */
7496 static void
7497ex_redrawtabline(exarg_T *eap UNUSED)
7498{
7499 int r = RedrawingDisabled;
7500 int p = p_lz;
7501
7502 RedrawingDisabled = 0;
7503 p_lz = FALSE;
7504
7505 draw_tabline();
7506
7507 RedrawingDisabled = r;
7508 p_lz = p;
7509 out_flush();
7510}
7511
Bram Moolenaar071d4272004-06-13 20:20:40 +00007512 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007513close_redir(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007514{
7515 if (redir_fd != NULL)
7516 {
7517 fclose(redir_fd);
7518 redir_fd = NULL;
7519 }
7520#ifdef FEAT_EVAL
7521 redir_reg = 0;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007522 if (redir_vname)
7523 {
7524 var_redir_stop();
7525 redir_vname = 0;
7526 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007527#endif
7528}
7529
Bram Moolenaarf96ae0b2019-07-28 15:21:55 +02007530#if (defined(FEAT_SESSION) || defined(FEAT_EVAL)) || defined(PROTO)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007531 int
Bram Moolenaarbd67aac2019-09-21 23:09:04 +02007532vim_mkdir_emsg(char_u *name, int prot UNUSED)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007533{
7534 if (vim_mkdir(name, prot) != 0)
7535 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007536 semsg(_("E739: Cannot create directory: %s"), name);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007537 return FAIL;
7538 }
7539 return OK;
7540}
7541#endif
7542
Bram Moolenaar071d4272004-06-13 20:20:40 +00007543/*
7544 * Open a file for writing for an Ex command, with some checks.
7545 * Return file descriptor, or NULL on failure.
7546 */
7547 FILE *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007548open_exfile(
7549 char_u *fname,
7550 int forceit,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007551 char *mode) // "w" for create new file or "a" for append
Bram Moolenaar071d4272004-06-13 20:20:40 +00007552{
7553 FILE *fd;
7554
7555#ifdef UNIX
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007556 // with Unix it is possible to open a directory
Bram Moolenaar071d4272004-06-13 20:20:40 +00007557 if (mch_isdir(fname))
7558 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007559 semsg(_(e_isadir2), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007560 return NULL;
7561 }
7562#endif
7563 if (!forceit && *mode != 'a' && vim_fexists(fname))
7564 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007565 semsg(_("E189: \"%s\" exists (add ! to override)"), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007566 return NULL;
7567 }
7568
7569 if ((fd = mch_fopen((char *)fname, mode)) == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007570 semsg(_("E190: Cannot open \"%s\" for writing"), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007571
7572 return fd;
7573}
7574
7575/*
7576 * ":mark" and ":k".
7577 */
7578 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007579ex_mark(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007580{
7581 pos_T pos;
7582
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007583 if (*eap->arg == NUL) // No argument?
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007584 emsg(_(e_argreq));
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007585 else if (eap->arg[1] != NUL) // more than one character?
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007586 emsg(_(e_trailing));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007587 else
7588 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007589 pos = curwin->w_cursor; // save curwin->w_cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00007590 curwin->w_cursor.lnum = eap->line2;
7591 beginline(BL_WHITE | BL_FIX);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007592 if (setmark(*eap->arg) == FAIL) // set mark
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007593 emsg(_("E191: Argument must be a letter or forward/backward quote"));
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007594 curwin->w_cursor = pos; // restore curwin->w_cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00007595 }
7596}
7597
7598/*
7599 * Update w_topline, w_leftcol and the cursor position.
7600 */
7601 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007602update_topline_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007603{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007604 check_cursor(); // put cursor on valid line
Bram Moolenaar071d4272004-06-13 20:20:40 +00007605 update_topline();
7606 if (!curwin->w_p_wrap)
7607 validate_cursor();
7608 update_curswant();
7609}
7610
Bram Moolenaar071d4272004-06-13 20:20:40 +00007611/*
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007612 * Save the current State and go to Normal mode.
7613 * Return TRUE if the typeahead could be saved.
7614 */
7615 int
7616save_current_state(save_state_T *sst)
7617{
7618 sst->save_msg_scroll = msg_scroll;
7619 sst->save_restart_edit = restart_edit;
7620 sst->save_msg_didout = msg_didout;
7621 sst->save_State = State;
7622 sst->save_insertmode = p_im;
7623 sst->save_finish_op = finish_op;
7624 sst->save_opcount = opcount;
Bram Moolenaarcce713d2019-03-04 11:40:12 +01007625 sst->save_reg_executing = reg_executing;
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007626
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007627 msg_scroll = FALSE; // no msg scrolling in Normal mode
7628 restart_edit = 0; // don't go to Insert mode
7629 p_im = FALSE; // don't use 'insertmode'
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007630
7631 /*
7632 * Save the current typeahead. This is required to allow using ":normal"
7633 * from an event handler and makes sure we don't hang when the argument
7634 * ends with half a command.
7635 */
7636 save_typeahead(&sst->tabuf);
7637 return sst->tabuf.typebuf_valid;
7638}
7639
7640 void
7641restore_current_state(save_state_T *sst)
7642{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007643 // Restore the previous typeahead.
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007644 restore_typeahead(&sst->tabuf);
7645
7646 msg_scroll = sst->save_msg_scroll;
7647 restart_edit = sst->save_restart_edit;
7648 p_im = sst->save_insertmode;
7649 finish_op = sst->save_finish_op;
7650 opcount = sst->save_opcount;
Bram Moolenaarcce713d2019-03-04 11:40:12 +01007651 reg_executing = sst->save_reg_executing;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007652 msg_didout |= sst->save_msg_didout; // don't reset msg_didout now
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007653
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007654 // Restore the state (needed when called from a function executed for
7655 // 'indentexpr'). Update the mouse and cursor, they may have changed.
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007656 State = sst->save_State;
7657#ifdef CURSOR_SHAPE
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007658 ui_cursor_shape(); // may show different cursor shape
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007659#endif
7660}
7661
7662/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007663 * ":normal[!] {commands}": Execute normal mode commands.
7664 */
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01007665 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007666ex_normal(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007667{
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007668 save_state_T save_state;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007669 char_u *arg = NULL;
7670 int l;
7671 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007672
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00007673 if (ex_normal_lock > 0)
7674 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007675 emsg(_(e_secure));
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00007676 return;
7677 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007678 if (ex_normal_busy >= p_mmd)
7679 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007680 emsg(_("E192: Recursive use of :normal too deep"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007681 return;
7682 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007683
Bram Moolenaar071d4272004-06-13 20:20:40 +00007684 /*
7685 * vgetc() expects a CSI and K_SPECIAL to have been escaped. Don't do
7686 * this for the K_SPECIAL leading byte, otherwise special keys will not
7687 * work.
7688 */
7689 if (has_mbyte)
7690 {
7691 int len = 0;
7692
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007693 // Count the number of characters to be escaped.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007694 for (p = eap->arg; *p != NUL; ++p)
7695 {
Bram Moolenaar13505972019-01-24 15:04:48 +01007696#ifdef FEAT_GUI
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007697 if (*p == CSI) // leadbyte CSI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007698 len += 2;
Bram Moolenaar13505972019-01-24 15:04:48 +01007699#endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007700 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007701 if (*++p == K_SPECIAL // trailbyte K_SPECIAL or CSI
Bram Moolenaar13505972019-01-24 15:04:48 +01007702#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007703 || *p == CSI
Bram Moolenaar13505972019-01-24 15:04:48 +01007704#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007705 )
7706 len += 2;
7707 }
7708 if (len > 0)
7709 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02007710 arg = alloc(STRLEN(eap->arg) + len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007711 if (arg != NULL)
7712 {
7713 len = 0;
7714 for (p = eap->arg; *p != NUL; ++p)
7715 {
7716 arg[len++] = *p;
Bram Moolenaar13505972019-01-24 15:04:48 +01007717#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007718 if (*p == CSI)
7719 {
7720 arg[len++] = KS_EXTRA;
7721 arg[len++] = (int)KE_CSI;
7722 }
Bram Moolenaar13505972019-01-24 15:04:48 +01007723#endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007724 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007725 {
7726 arg[len++] = *++p;
7727 if (*p == K_SPECIAL)
7728 {
7729 arg[len++] = KS_SPECIAL;
7730 arg[len++] = KE_FILLER;
7731 }
Bram Moolenaar13505972019-01-24 15:04:48 +01007732#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007733 else if (*p == CSI)
7734 {
7735 arg[len++] = KS_EXTRA;
7736 arg[len++] = (int)KE_CSI;
7737 }
Bram Moolenaar13505972019-01-24 15:04:48 +01007738#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007739 }
7740 arg[len] = NUL;
7741 }
7742 }
7743 }
7744 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007745
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007746 ++ex_normal_busy;
7747 if (save_current_state(&save_state))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007748 {
7749 /*
7750 * Repeat the :normal command for each line in the range. When no
7751 * range given, execute it just once, without positioning the cursor
7752 * first.
7753 */
7754 do
7755 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007756 if (eap->addr_count != 0)
7757 {
7758 curwin->w_cursor.lnum = eap->line1++;
7759 curwin->w_cursor.col = 0;
Bram Moolenaard5d37532017-03-27 23:02:07 +02007760 check_cursor_moved(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007761 }
7762
Bram Moolenaar13505972019-01-24 15:04:48 +01007763 exec_normal_cmd(arg != NULL
7764 ? arg
7765 : eap->arg, eap->forceit ? REMAP_NONE : REMAP_YES, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007766 }
7767 while (eap->addr_count > 0 && eap->line1 <= eap->line2 && !got_int);
7768 }
7769
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007770 // Might not return to the main loop when in an event handler.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007771 update_topline_cursor();
7772
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007773 restore_current_state(&save_state);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007774 --ex_normal_busy;
Bram Moolenaareda73602014-11-05 09:53:23 +01007775 setmouse();
Bram Moolenaareda73602014-11-05 09:53:23 +01007776#ifdef CURSOR_SHAPE
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007777 ui_cursor_shape(); // may show different cursor shape
Bram Moolenaareda73602014-11-05 09:53:23 +01007778#endif
7779
Bram Moolenaar071d4272004-06-13 20:20:40 +00007780 vim_free(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007781}
7782
7783/*
Bram Moolenaar64969662005-12-14 21:59:55 +00007784 * ":startinsert", ":startreplace" and ":startgreplace"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007785 */
7786 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007787ex_startinsert(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007788{
Bram Moolenaarfd371682005-01-14 21:42:54 +00007789 if (eap->forceit)
7790 {
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02007791 // cursor line can be zero on startup
Bram Moolenaar09ca9322017-09-26 17:40:45 +02007792 if (!curwin->w_cursor.lnum)
7793 curwin->w_cursor.lnum = 1;
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02007794 set_cursor_for_append_to_line();
Bram Moolenaarfd371682005-01-14 21:42:54 +00007795 }
Bram Moolenaareb58a242020-04-19 18:13:19 +02007796#ifdef FEAT_TERMINAL
7797 // Ignore this when running in an active terminal.
7798 if (term_job_running(curbuf->b_term))
7799 return;
7800#endif
Bram Moolenaarfd371682005-01-14 21:42:54 +00007801
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02007802 // Ignore the command when already in Insert mode. Inserting an
7803 // expression register that invokes a function can do this.
Bram Moolenaara40c5002005-01-09 21:16:21 +00007804 if (State & INSERT)
7805 return;
7806
Bram Moolenaar64969662005-12-14 21:59:55 +00007807 if (eap->cmdidx == CMD_startinsert)
7808 restart_edit = 'a';
7809 else if (eap->cmdidx == CMD_startreplace)
7810 restart_edit = 'R';
Bram Moolenaar071d4272004-06-13 20:20:40 +00007811 else
Bram Moolenaar64969662005-12-14 21:59:55 +00007812 restart_edit = 'V';
7813
7814 if (!eap->forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007815 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00007816 if (eap->cmdidx == CMD_startinsert)
7817 restart_edit = 'i';
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02007818 curwin->w_curswant = 0; // avoid MAXCOL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007819 }
7820}
7821
7822/*
7823 * ":stopinsert"
7824 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007825 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007826ex_stopinsert(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007827{
7828 restart_edit = 0;
7829 stop_insert_mode = TRUE;
Bram Moolenaarfd773e92016-04-02 19:39:16 +02007830 clearmode();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007831}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007832
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00007833/*
7834 * Execute normal mode command "cmd".
7835 * "remap" can be REMAP_NONE or REMAP_YES.
7836 */
7837 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007838exec_normal_cmd(char_u *cmd, int remap, int silent)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00007839{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007840 // Stuff the argument into the typeahead buffer.
Bram Moolenaar25281632016-01-21 23:32:32 +01007841 ins_typebuf(cmd, remap, 0, TRUE, silent);
Bram Moolenaar586c70c2018-10-02 16:23:58 +02007842 exec_normal(FALSE, FALSE, FALSE);
Bram Moolenaar25281632016-01-21 23:32:32 +01007843}
Bram Moolenaar25281632016-01-21 23:32:32 +01007844
Bram Moolenaar25281632016-01-21 23:32:32 +01007845/*
7846 * Execute normal_cmd() until there is no typeahead left.
Bram Moolenaar586c70c2018-10-02 16:23:58 +02007847 * When "use_vpeekc" is TRUE use vpeekc() to check for available chars.
Bram Moolenaar25281632016-01-21 23:32:32 +01007848 */
7849 void
Bram Moolenaar586c70c2018-10-02 16:23:58 +02007850exec_normal(int was_typed, int use_vpeekc, int may_use_terminal_loop UNUSED)
Bram Moolenaar25281632016-01-21 23:32:32 +01007851{
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00007852 oparg_T oa;
Bram Moolenaar905dd902019-04-07 14:21:47 +02007853 int c;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00007854
Bram Moolenaar905dd902019-04-07 14:21:47 +02007855 // When calling vpeekc() from feedkeys() it will return Ctrl_C when there
7856 // is nothing to get, so also check for Ctrl_C.
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00007857 clear_oparg(&oa);
7858 finish_op = FALSE;
Bram Moolenaar586c70c2018-10-02 16:23:58 +02007859 while ((!stuff_empty()
7860 || ((was_typed || !typebuf_typed()) && typebuf.tb_len > 0)
Bram Moolenaar905dd902019-04-07 14:21:47 +02007861 || (use_vpeekc && (c = vpeekc()) != NUL && c != Ctrl_C))
Bram Moolenaar586c70c2018-10-02 16:23:58 +02007862 && !got_int)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00007863 {
7864 update_topline_cursor();
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02007865#ifdef FEAT_TERMINAL
Bram Moolenaar655a82a2018-05-08 22:01:07 +02007866 if (may_use_terminal_loop && term_use_loop()
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02007867 && oa.op_type == OP_NOP && oa.regname == NUL
7868 && !VIsual_active)
7869 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007870 // If terminal_loop() returns OK we got a key that is handled
7871 // in Normal model. With FAIL we first need to position the
7872 // cursor and the screen needs to be redrawn.
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02007873 if (terminal_loop(TRUE) == OK)
7874 normal_cmd(&oa, TRUE);
7875 }
7876 else
7877#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007878 // execute a Normal mode cmd
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02007879 normal_cmd(&oa, TRUE);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00007880 }
7881}
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00007882
Bram Moolenaar071d4272004-06-13 20:20:40 +00007883#ifdef FEAT_FIND_ID
7884 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007885ex_checkpath(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007886{
7887 find_pattern_in_path(NULL, 0, 0, FALSE, FALSE, CHECK_PATH, 1L,
7888 eap->forceit ? ACTION_SHOW_ALL : ACTION_SHOW,
7889 (linenr_T)1, (linenr_T)MAXLNUM);
7890}
7891
Bram Moolenaar4033c552017-09-16 20:54:51 +02007892#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007893/*
7894 * ":psearch"
7895 */
7896 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007897ex_psearch(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007898{
7899 g_do_tagpreview = p_pvh;
7900 ex_findpat(eap);
7901 g_do_tagpreview = 0;
7902}
7903#endif
7904
7905 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007906ex_findpat(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007907{
7908 int whole = TRUE;
7909 long n;
7910 char_u *p;
7911 int action;
7912
7913 switch (cmdnames[eap->cmdidx].cmd_name[2])
7914 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007915 case 'e': // ":psearch", ":isearch" and ":dsearch"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007916 if (cmdnames[eap->cmdidx].cmd_name[0] == 'p')
7917 action = ACTION_GOTO;
7918 else
7919 action = ACTION_SHOW;
7920 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007921 case 'i': // ":ilist" and ":dlist"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007922 action = ACTION_SHOW_ALL;
7923 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007924 case 'u': // ":ijump" and ":djump"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007925 action = ACTION_GOTO;
7926 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007927 default: // ":isplit" and ":dsplit"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007928 action = ACTION_SPLIT;
7929 break;
7930 }
7931
7932 n = 1;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007933 if (vim_isdigit(*eap->arg)) // get count
Bram Moolenaar071d4272004-06-13 20:20:40 +00007934 {
7935 n = getdigits(&eap->arg);
7936 eap->arg = skipwhite(eap->arg);
7937 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007938 if (*eap->arg == '/') // Match regexp, not just whole words
Bram Moolenaar071d4272004-06-13 20:20:40 +00007939 {
7940 whole = FALSE;
7941 ++eap->arg;
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02007942 p = skip_regexp(eap->arg, '/', p_magic);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007943 if (*p)
7944 {
7945 *p++ = NUL;
7946 p = skipwhite(p);
7947
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007948 // Check for trailing illegal characters
Bram Moolenaarfaac4102020-04-20 17:46:14 +02007949 if (!ends_excmd2(eap->arg, p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007950 eap->errmsg = e_trailing;
7951 else
7952 eap->nextcmd = check_nextcmd(p);
7953 }
7954 }
7955 if (!eap->skip)
7956 find_pattern_in_path(eap->arg, 0, (int)STRLEN(eap->arg),
7957 whole, !eap->forceit,
7958 *eap->cmd == 'd' ? FIND_DEFINE : FIND_ANY,
7959 n, action, eap->line1, eap->line2);
7960}
7961#endif
7962
Bram Moolenaar071d4272004-06-13 20:20:40 +00007963
Bram Moolenaar4033c552017-09-16 20:54:51 +02007964#ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00007965/*
7966 * ":ptag", ":ptselect", ":ptjump", ":ptnext", etc.
7967 */
7968 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007969ex_ptag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007970{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007971 g_do_tagpreview = p_pvh; // will be reset to 0 in ex_tag_cmd()
Bram Moolenaar071d4272004-06-13 20:20:40 +00007972 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
7973}
7974
7975/*
7976 * ":pedit"
7977 */
7978 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007979ex_pedit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007980{
7981 win_T *curwin_save = curwin;
7982
Bram Moolenaar3a2505c2020-03-09 16:40:41 +01007983 if (ERROR_IF_ANY_POPUP_WINDOW)
7984 return;
7985
Bram Moolenaar026587b2019-08-17 15:08:00 +02007986 // Open the preview window or popup and make it the current window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007987 g_do_tagpreview = p_pvh;
Bram Moolenaar576a4a62019-08-18 15:25:17 +02007988 prepare_tagpreview(TRUE, TRUE, FALSE);
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02007989
Bram Moolenaar026587b2019-08-17 15:08:00 +02007990 // Edit the file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007991 do_exedit(eap, NULL);
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02007992
Bram Moolenaar071d4272004-06-13 20:20:40 +00007993 if (curwin != curwin_save && win_valid(curwin_save))
7994 {
Bram Moolenaar026587b2019-08-17 15:08:00 +02007995 // Return cursor to where we were
Bram Moolenaar071d4272004-06-13 20:20:40 +00007996 validate_cursor();
7997 redraw_later(VALID);
7998 win_enter(curwin_save, TRUE);
7999 }
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01008000# ifdef FEAT_PROP_POPUP
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02008001 else if (WIN_IS_POPUP(curwin))
8002 {
8003 // can't keep focus in popup window
8004 win_enter(firstwin, TRUE);
8005 }
8006# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008007 g_do_tagpreview = 0;
8008}
Bram Moolenaar4033c552017-09-16 20:54:51 +02008009#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008010
8011/*
8012 * ":stag", ":stselect" and ":stjump".
8013 */
8014 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008015ex_stag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008016{
8017 postponed_split = -1;
8018 postponed_split_flags = cmdmod.split;
Bram Moolenaard326ce82007-03-11 14:48:29 +00008019 postponed_split_tab = cmdmod.tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008020 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
8021 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00008022 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008023}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008024
8025/*
8026 * ":tag", ":tselect", ":tjump", ":tnext", etc.
8027 */
8028 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008029ex_tag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008030{
8031 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name);
8032}
8033
8034 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008035ex_tag_cmd(exarg_T *eap, char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008036{
8037 int cmd;
8038
8039 switch (name[1])
8040 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008041 case 'j': cmd = DT_JUMP; // ":tjump"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008042 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008043 case 's': cmd = DT_SELECT; // ":tselect"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008044 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008045 case 'p': cmd = DT_PREV; // ":tprevious"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008046 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008047 case 'N': cmd = DT_PREV; // ":tNext"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008048 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008049 case 'n': cmd = DT_NEXT; // ":tnext"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008050 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008051 case 'o': cmd = DT_POP; // ":pop"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008052 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008053 case 'f': // ":tfirst"
8054 case 'r': cmd = DT_FIRST; // ":trewind"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008055 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008056 case 'l': cmd = DT_LAST; // ":tlast"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008057 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008058 default: // ":tag"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008059#ifdef FEAT_CSCOPE
Bram Moolenaar7c94c262008-06-20 09:11:34 +00008060 if (p_cst && *eap->arg != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008061 {
Bram Moolenaard4db7712016-11-12 19:16:46 +01008062 ex_cstag(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008063 return;
8064 }
8065#endif
8066 cmd = DT_TAG;
8067 break;
8068 }
8069
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00008070 if (name[0] == 'l')
8071 {
8072#ifndef FEAT_QUICKFIX
8073 ex_ni(eap);
8074 return;
8075#else
8076 cmd = DT_LTAG;
8077#endif
8078 }
8079
Bram Moolenaar071d4272004-06-13 20:20:40 +00008080 do_tag(eap->arg, cmd, eap->addr_count > 0 ? (int)eap->line2 : 1,
8081 eap->forceit, TRUE);
8082}
8083
8084/*
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008085 * Check "str" for starting with a special cmdline variable.
8086 * If found return one of the SPEC_ values and set "*usedlen" to the length of
8087 * the variable. Otherwise return -1 and "*usedlen" is unchanged.
8088 */
8089 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008090find_cmdline_var(char_u *src, int *usedlen)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008091{
8092 int len;
8093 int i;
Bram Moolenaar8f0b2d42009-05-16 14:41:10 +00008094 static char *(spec_str[]) = {
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008095 "%",
8096#define SPEC_PERC 0
8097 "#",
Bram Moolenaar65f08472017-09-10 18:16:20 +02008098#define SPEC_HASH (SPEC_PERC + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008099 "<cword>", // cursor word
Bram Moolenaar65f08472017-09-10 18:16:20 +02008100#define SPEC_CWORD (SPEC_HASH + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008101 "<cWORD>", // cursor WORD
Bram Moolenaar65f08472017-09-10 18:16:20 +02008102#define SPEC_CCWORD (SPEC_CWORD + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008103 "<cexpr>", // expr under cursor
Bram Moolenaar65f08472017-09-10 18:16:20 +02008104#define SPEC_CEXPR (SPEC_CCWORD + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008105 "<cfile>", // cursor path name
Bram Moolenaar65f08472017-09-10 18:16:20 +02008106#define SPEC_CFILE (SPEC_CEXPR + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008107 "<sfile>", // ":so" file name
Bram Moolenaar65f08472017-09-10 18:16:20 +02008108#define SPEC_SFILE (SPEC_CFILE + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008109 "<slnum>", // ":so" file line number
Bram Moolenaar65f08472017-09-10 18:16:20 +02008110#define SPEC_SLNUM (SPEC_SFILE + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008111 "<afile>", // autocommand file name
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008112#define SPEC_AFILE (SPEC_SLNUM + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008113 "<abuf>", // autocommand buffer number
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008114#define SPEC_ABUF (SPEC_AFILE + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008115 "<amatch>", // autocommand match name
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01008116#define SPEC_AMATCH (SPEC_ABUF + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008117 "<sflnum>", // script file line number
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008118#define SPEC_SFLNUM (SPEC_AMATCH + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008119#ifdef FEAT_CLIENTSERVER
8120 "<client>"
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008121# define SPEC_CLIENT (SPEC_SFLNUM + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008122#endif
8123 };
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008124
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00008125 for (i = 0; i < (int)(sizeof(spec_str) / sizeof(char *)); ++i)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008126 {
8127 len = (int)STRLEN(spec_str[i]);
8128 if (STRNCMP(src, spec_str[i], len) == 0)
8129 {
8130 *usedlen = len;
8131 return i;
8132 }
8133 }
8134 return -1;
8135}
8136
8137/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008138 * Evaluate cmdline variables.
8139 *
8140 * change '%' to curbuf->b_ffname
8141 * '#' to curwin->w_altfile
8142 * '<cword>' to word under the cursor
8143 * '<cWORD>' to WORD under the cursor
Bram Moolenaar8071cb22019-07-12 17:58:01 +02008144 * '<cexpr>' to C-expression under the cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00008145 * '<cfile>' to path name under the cursor
8146 * '<sfile>' to sourced file name
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01008147 * '<slnum>' to sourced file line number
Bram Moolenaar071d4272004-06-13 20:20:40 +00008148 * '<afile>' to file name for autocommand
8149 * '<abuf>' to buffer number for autocommand
8150 * '<amatch>' to matching name for autocommand
8151 *
8152 * When an error is detected, "errormsg" is set to a non-NULL pointer (may be
8153 * "" for error without a message) and NULL is returned.
8154 * Returns an allocated string if a valid match was found.
8155 * Returns NULL if no match was found. "usedlen" then still contains the
8156 * number of characters to skip.
8157 */
8158 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008159eval_vars(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008160 char_u *src, // pointer into commandline
8161 char_u *srcstart, // beginning of valid memory for src
8162 int *usedlen, // characters after src that are used
8163 linenr_T *lnump, // line number for :e command, or NULL
8164 char **errormsg, // pointer to error message
8165 int *escaped) // return value has escaped white space (can
8166 // be NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008167{
8168 int i;
8169 char_u *s;
8170 char_u *result;
8171 char_u *resultbuf = NULL;
8172 int resultlen;
8173 buf_T *buf;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008174 int valid = VALID_HEAD + VALID_PATH; // assume valid result
Bram Moolenaar071d4272004-06-13 20:20:40 +00008175 int spec_idx;
Bram Moolenaarfd249462018-07-28 16:14:30 +02008176 int tilde_file = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008177 int skip_mod = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008178 char_u strbuf[30];
Bram Moolenaar071d4272004-06-13 20:20:40 +00008179
8180 *errormsg = NULL;
Bram Moolenaar63b92542007-03-27 14:57:09 +00008181 if (escaped != NULL)
8182 *escaped = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008183
8184 /*
8185 * Check if there is something to do.
8186 */
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008187 spec_idx = find_cmdline_var(src, usedlen);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008188 if (spec_idx < 0) // no match
Bram Moolenaar071d4272004-06-13 20:20:40 +00008189 {
8190 *usedlen = 1;
8191 return NULL;
8192 }
8193
8194 /*
8195 * Skip when preceded with a backslash "\%" and "\#".
8196 * Note: In "\\%" the % is also not recognized!
8197 */
8198 if (src > srcstart && src[-1] == '\\')
8199 {
8200 *usedlen = 0;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008201 STRMOVE(src - 1, src); // remove backslash
Bram Moolenaar071d4272004-06-13 20:20:40 +00008202 return NULL;
8203 }
8204
8205 /*
8206 * word or WORD under cursor
8207 */
Bram Moolenaar65f08472017-09-10 18:16:20 +02008208 if (spec_idx == SPEC_CWORD || spec_idx == SPEC_CCWORD
8209 || spec_idx == SPEC_CEXPR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008210 {
Bram Moolenaar65f08472017-09-10 18:16:20 +02008211 resultlen = find_ident_under_cursor(&result,
8212 spec_idx == SPEC_CWORD ? (FIND_IDENT | FIND_STRING)
8213 : spec_idx == SPEC_CEXPR ? (FIND_IDENT | FIND_STRING | FIND_EVAL)
8214 : FIND_STRING);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008215 if (resultlen == 0)
8216 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008217 *errormsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +00008218 return NULL;
8219 }
8220 }
8221
8222 /*
8223 * '#': Alternate file name
8224 * '%': Current file name
8225 * File name under the cursor
8226 * File name for autocommand
8227 * and following modifiers
8228 */
8229 else
8230 {
8231 switch (spec_idx)
8232 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008233 case SPEC_PERC: // '%': current file
Bram Moolenaar071d4272004-06-13 20:20:40 +00008234 if (curbuf->b_fname == NULL)
8235 {
8236 result = (char_u *)"";
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008237 valid = 0; // Must have ":p:h" to be valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00008238 }
8239 else
Bram Moolenaar00136dc2018-07-25 21:19:13 +02008240 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008241 result = curbuf->b_fname;
Bram Moolenaar00136dc2018-07-25 21:19:13 +02008242 tilde_file = STRCMP(result, "~") == 0;
8243 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008244 break;
8245
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008246 case SPEC_HASH: // '#' or "#99": alternate file
8247 if (src[1] == '#') // "##": the argument list
Bram Moolenaar071d4272004-06-13 20:20:40 +00008248 {
8249 result = arg_all();
8250 resultbuf = result;
8251 *usedlen = 2;
Bram Moolenaar63b92542007-03-27 14:57:09 +00008252 if (escaped != NULL)
8253 *escaped = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008254 skip_mod = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008255 break;
8256 }
8257 s = src + 1;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008258 if (*s == '<') // "#<99" uses v:oldfiles
Bram Moolenaard812df62008-11-09 12:46:09 +00008259 ++s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008260 i = (int)getdigits(&s);
Bram Moolenaarc312b8b2017-10-28 17:53:04 +02008261 if (s == src + 2 && src[1] == '-')
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008262 // just a minus sign, don't skip over it
Bram Moolenaarc312b8b2017-10-28 17:53:04 +02008263 s--;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008264 *usedlen = (int)(s - src); // length of what we expand
Bram Moolenaar071d4272004-06-13 20:20:40 +00008265
Bram Moolenaarc312b8b2017-10-28 17:53:04 +02008266 if (src[1] == '<' && i != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008267 {
Bram Moolenaard812df62008-11-09 12:46:09 +00008268 if (*usedlen < 2)
8269 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008270 // Should we give an error message for #<text?
Bram Moolenaard812df62008-11-09 12:46:09 +00008271 *usedlen = 1;
8272 return NULL;
8273 }
8274#ifdef FEAT_EVAL
8275 result = list_find_str(get_vim_var_list(VV_OLDFILES),
8276 (long)i);
8277 if (result == NULL)
8278 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008279 *errormsg = "";
Bram Moolenaard812df62008-11-09 12:46:09 +00008280 return NULL;
8281 }
8282#else
Bram Moolenaar8e481e82019-01-15 20:07:48 +01008283 *errormsg = _("E809: #< is not available without the +eval feature");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008284 return NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +00008285#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008286 }
8287 else
Bram Moolenaard812df62008-11-09 12:46:09 +00008288 {
Bram Moolenaarc312b8b2017-10-28 17:53:04 +02008289 if (i == 0 && src[1] == '<' && *usedlen > 1)
8290 *usedlen = 1;
Bram Moolenaard812df62008-11-09 12:46:09 +00008291 buf = buflist_findnr(i);
8292 if (buf == NULL)
8293 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008294 *errormsg = _("E194: No alternate file name to substitute for '#'");
Bram Moolenaard812df62008-11-09 12:46:09 +00008295 return NULL;
8296 }
8297 if (lnump != NULL)
8298 *lnump = ECMD_LAST;
8299 if (buf->b_fname == NULL)
8300 {
8301 result = (char_u *)"";
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008302 valid = 0; // Must have ":p:h" to be valid
Bram Moolenaard812df62008-11-09 12:46:09 +00008303 }
8304 else
Bram Moolenaar00136dc2018-07-25 21:19:13 +02008305 {
Bram Moolenaard812df62008-11-09 12:46:09 +00008306 result = buf->b_fname;
Bram Moolenaar00136dc2018-07-25 21:19:13 +02008307 tilde_file = STRCMP(result, "~") == 0;
8308 }
Bram Moolenaard812df62008-11-09 12:46:09 +00008309 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008310 break;
8311
8312#ifdef FEAT_SEARCHPATH
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008313 case SPEC_CFILE: // file name under cursor
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008314 result = file_name_at_cursor(FNAME_MESS|FNAME_HYP, 1L, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008315 if (result == NULL)
8316 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008317 *errormsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +00008318 return NULL;
8319 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008320 resultbuf = result; // remember allocated string
Bram Moolenaar071d4272004-06-13 20:20:40 +00008321 break;
8322#endif
8323
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008324 case SPEC_AFILE: // file name for autocommand
Bram Moolenaar071d4272004-06-13 20:20:40 +00008325 result = autocmd_fname;
Bram Moolenaarf6dad432008-09-18 19:29:58 +00008326 if (result != NULL && !autocmd_fname_full)
8327 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008328 // Still need to turn the fname into a full path. It is
8329 // postponed to avoid a delay when <afile> is not used.
Bram Moolenaarf6dad432008-09-18 19:29:58 +00008330 autocmd_fname_full = TRUE;
8331 result = FullName_save(autocmd_fname, FALSE);
8332 vim_free(autocmd_fname);
8333 autocmd_fname = result;
8334 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008335 if (result == NULL)
8336 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008337 *errormsg = _("E495: no autocommand file name to substitute for \"<afile>\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008338 return NULL;
8339 }
Bram Moolenaara0174af2008-01-02 20:08:25 +00008340 result = shorten_fname1(result);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008341 break;
8342
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008343 case SPEC_ABUF: // buffer number for autocommand
Bram Moolenaar071d4272004-06-13 20:20:40 +00008344 if (autocmd_bufnr <= 0)
8345 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008346 *errormsg = _("E496: no autocommand buffer number to substitute for \"<abuf>\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008347 return NULL;
8348 }
8349 sprintf((char *)strbuf, "%d", autocmd_bufnr);
8350 result = strbuf;
8351 break;
8352
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008353 case SPEC_AMATCH: // match name for autocommand
Bram Moolenaar071d4272004-06-13 20:20:40 +00008354 result = autocmd_match;
8355 if (result == NULL)
8356 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008357 *errormsg = _("E497: no autocommand match name to substitute for \"<amatch>\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008358 return NULL;
8359 }
8360 break;
8361
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008362 case SPEC_SFILE: // file name for ":so" command
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01008363 result = estack_sfile();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008364 if (result == NULL)
8365 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008366 *errormsg = _("E498: no :source file name to substitute for \"<sfile>\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008367 return NULL;
8368 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01008369 resultbuf = result; // remember allocated string
Bram Moolenaar071d4272004-06-13 20:20:40 +00008370 break;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008371
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008372 case SPEC_SLNUM: // line in file for ":so" command
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01008373 if (SOURCING_NAME == NULL || SOURCING_LNUM == 0)
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01008374 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008375 *errormsg = _("E842: no line number to use for \"<slnum>\"");
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01008376 return NULL;
8377 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01008378 sprintf((char *)strbuf, "%ld", SOURCING_LNUM);
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01008379 result = strbuf;
8380 break;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008381
8382#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008383 case SPEC_SFLNUM: // line in script file
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01008384 if (current_sctx.sc_lnum + SOURCING_LNUM == 0)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008385 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008386 *errormsg = _("E961: no line number to use for \"<sflnum>\"");
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008387 return NULL;
8388 }
8389 sprintf((char *)strbuf, "%ld",
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01008390 (long)(current_sctx.sc_lnum + SOURCING_LNUM));
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008391 result = strbuf;
8392 break;
8393#endif
8394
8395#ifdef FEAT_CLIENTSERVER
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008396 case SPEC_CLIENT: // Source of last submitted input
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008397 sprintf((char *)strbuf, PRINTF_HEX_LONG_U,
8398 (long_u)clientWindow);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008399 result = strbuf;
8400 break;
8401#endif
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008402
Bram Moolenaar9e0f6ec2017-05-16 09:36:54 +02008403 default:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008404 result = (char_u *)""; // avoid gcc warning
Bram Moolenaar9e0f6ec2017-05-16 09:36:54 +02008405 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008406 }
8407
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008408 resultlen = (int)STRLEN(result); // length of new string
8409 if (src[*usedlen] == '<') // remove the file name extension
Bram Moolenaar071d4272004-06-13 20:20:40 +00008410 {
8411 ++*usedlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008412 if ((s = vim_strrchr(result, '.')) != NULL && s >= gettail(result))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008413 resultlen = (int)(s - result);
8414 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008415 else if (!skip_mod)
8416 {
Bram Moolenaar00136dc2018-07-25 21:19:13 +02008417 valid |= modify_fname(src, tilde_file, usedlen, &result, &resultbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008418 &resultlen);
8419 if (result == NULL)
8420 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008421 *errormsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +00008422 return NULL;
8423 }
8424 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008425 }
8426
8427 if (resultlen == 0 || valid != VALID_HEAD + VALID_PATH)
8428 {
8429 if (valid != VALID_HEAD + VALID_PATH)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008430 // xgettext:no-c-format
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008431 *errormsg = _("E499: Empty file name for '%' or '#', only works with \":p:h\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008432 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008433 *errormsg = _("E500: Evaluates to an empty string");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008434 result = NULL;
8435 }
8436 else
8437 result = vim_strnsave(result, resultlen);
8438 vim_free(resultbuf);
8439 return result;
8440}
8441
8442/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008443 * Expand the <sfile> string in "arg".
8444 *
8445 * Returns an allocated string, or NULL for any error.
8446 */
8447 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008448expand_sfile(char_u *arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008449{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008450 char *errormsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008451 int len;
8452 char_u *result;
8453 char_u *newres;
8454 char_u *repl;
8455 int srclen;
8456 char_u *p;
8457
8458 result = vim_strsave(arg);
8459 if (result == NULL)
8460 return NULL;
8461
8462 for (p = result; *p; )
8463 {
8464 if (STRNCMP(p, "<sfile>", 7) != 0)
8465 ++p;
8466 else
8467 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008468 // replace "<sfile>" with the sourced file name, and do ":" stuff
Bram Moolenaar63b92542007-03-27 14:57:09 +00008469 repl = eval_vars(p, result, &srclen, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008470 if (errormsg != NULL)
8471 {
8472 if (*errormsg)
8473 emsg(errormsg);
8474 vim_free(result);
8475 return NULL;
8476 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008477 if (repl == NULL) // no match (cannot happen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008478 {
8479 p += srclen;
8480 continue;
8481 }
8482 len = (int)STRLEN(result) - srclen + (int)STRLEN(repl) + 1;
8483 newres = alloc(len);
8484 if (newres == NULL)
8485 {
8486 vim_free(repl);
8487 vim_free(result);
8488 return NULL;
8489 }
8490 mch_memmove(newres, result, (size_t)(p - result));
8491 STRCPY(newres + (p - result), repl);
8492 len = (int)STRLEN(newres);
8493 STRCAT(newres, p + srclen);
8494 vim_free(repl);
8495 vim_free(result);
8496 result = newres;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008497 p = newres + len; // continue after the match
Bram Moolenaar071d4272004-06-13 20:20:40 +00008498 }
8499 }
8500
8501 return result;
8502}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008503
Bram Moolenaar071d4272004-06-13 20:20:40 +00008504#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG) || defined(PROTO)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00008505/*
Bram Moolenaard9462e32011-04-11 21:35:11 +02008506 * Make a dialog message in "buff[DIALOG_MSG_SIZE]".
Bram Moolenaarb765d632005-06-07 21:00:02 +00008507 * "format" must contain "%s".
Bram Moolenaar9c13b352005-05-19 20:53:52 +00008508 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008509 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008510dialog_msg(char_u *buff, char *format, char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008511{
Bram Moolenaar071d4272004-06-13 20:20:40 +00008512 if (fname == NULL)
8513 fname = (char_u *)_("Untitled");
Bram Moolenaard9462e32011-04-11 21:35:11 +02008514 vim_snprintf((char *)buff, DIALOG_MSG_SIZE, format, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008515}
8516#endif
8517
8518/*
8519 * ":behave {mswin,xterm}"
8520 */
8521 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008522ex_behave(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008523{
8524 if (STRCMP(eap->arg, "mswin") == 0)
8525 {
8526 set_option_value((char_u *)"selection", 0L, (char_u *)"exclusive", 0);
8527 set_option_value((char_u *)"selectmode", 0L, (char_u *)"mouse,key", 0);
8528 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"popup", 0);
8529 set_option_value((char_u *)"keymodel", 0L,
8530 (char_u *)"startsel,stopsel", 0);
8531 }
8532 else if (STRCMP(eap->arg, "xterm") == 0)
8533 {
8534 set_option_value((char_u *)"selection", 0L, (char_u *)"inclusive", 0);
8535 set_option_value((char_u *)"selectmode", 0L, (char_u *)"", 0);
8536 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"extend", 0);
8537 set_option_value((char_u *)"keymodel", 0L, (char_u *)"", 0);
8538 }
8539 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008540 semsg(_(e_invarg2), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008541}
8542
Bram Moolenaar071d4272004-06-13 20:20:40 +00008543static int filetype_detect = FALSE;
8544static int filetype_plugin = FALSE;
8545static int filetype_indent = FALSE;
8546
8547/*
8548 * ":filetype [plugin] [indent] {on,off,detect}"
8549 * on: Load the filetype.vim file to install autocommands for file types.
8550 * off: Load the ftoff.vim file to remove all autocommands for file types.
8551 * plugin on: load filetype.vim and ftplugin.vim
8552 * plugin off: load ftplugof.vim
8553 * indent on: load filetype.vim and indent.vim
8554 * indent off: load indoff.vim
8555 */
8556 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008557ex_filetype(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008558{
8559 char_u *arg = eap->arg;
8560 int plugin = FALSE;
8561 int indent = FALSE;
8562
8563 if (*eap->arg == NUL)
8564 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008565 // Print current status.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008566 smsg("filetype detection:%s plugin:%s indent:%s",
Bram Moolenaar071d4272004-06-13 20:20:40 +00008567 filetype_detect ? "ON" : "OFF",
8568 filetype_plugin ? (filetype_detect ? "ON" : "(on)") : "OFF",
8569 filetype_indent ? (filetype_detect ? "ON" : "(on)") : "OFF");
8570 return;
8571 }
8572
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008573 // Accept "plugin" and "indent" in any order.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008574 for (;;)
8575 {
8576 if (STRNCMP(arg, "plugin", 6) == 0)
8577 {
8578 plugin = TRUE;
8579 arg = skipwhite(arg + 6);
8580 continue;
8581 }
8582 if (STRNCMP(arg, "indent", 6) == 0)
8583 {
8584 indent = TRUE;
8585 arg = skipwhite(arg + 6);
8586 continue;
8587 }
8588 break;
8589 }
8590 if (STRCMP(arg, "on") == 0 || STRCMP(arg, "detect") == 0)
8591 {
8592 if (*arg == 'o' || !filetype_detect)
8593 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01008594 source_runtime((char_u *)FILETYPE_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008595 filetype_detect = TRUE;
8596 if (plugin)
8597 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01008598 source_runtime((char_u *)FTPLUGIN_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008599 filetype_plugin = TRUE;
8600 }
8601 if (indent)
8602 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01008603 source_runtime((char_u *)INDENT_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008604 filetype_indent = TRUE;
8605 }
8606 }
8607 if (*arg == 'd')
8608 {
Bram Moolenaar1610d052016-06-09 22:53:01 +02008609 (void)do_doautocmd((char_u *)"filetypedetect BufRead", TRUE, NULL);
Bram Moolenaara3227e22006-03-08 21:32:40 +00008610 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008611 }
8612 }
8613 else if (STRCMP(arg, "off") == 0)
8614 {
8615 if (plugin || indent)
8616 {
8617 if (plugin)
8618 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01008619 source_runtime((char_u *)FTPLUGOF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008620 filetype_plugin = FALSE;
8621 }
8622 if (indent)
8623 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01008624 source_runtime((char_u *)INDOFF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008625 filetype_indent = FALSE;
8626 }
8627 }
8628 else
8629 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01008630 source_runtime((char_u *)FTOFF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008631 filetype_detect = FALSE;
8632 }
8633 }
8634 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008635 semsg(_(e_invarg2), arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008636}
8637
8638/*
Bram Moolenaar3e545692017-06-04 19:00:32 +02008639 * ":setfiletype [FALLBACK] {name}"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008640 */
8641 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008642ex_setfiletype(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008643{
8644 if (!did_filetype)
Bram Moolenaar3e545692017-06-04 19:00:32 +02008645 {
8646 char_u *arg = eap->arg;
8647
8648 if (STRNCMP(arg, "FALLBACK ", 9) == 0)
8649 arg += 9;
8650
8651 set_option_value((char_u *)"filetype", 0L, arg, OPT_LOCAL);
8652 if (arg != eap->arg)
8653 did_filetype = FALSE;
8654 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008655}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008656
Bram Moolenaar071d4272004-06-13 20:20:40 +00008657 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008658ex_digraphs(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008659{
8660#ifdef FEAT_DIGRAPHS
8661 if (*eap->arg != NUL)
8662 putdigraph(eap->arg);
8663 else
Bram Moolenaareae8ae12018-12-14 18:53:02 +01008664 listdigraphs(eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008665#else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008666 emsg(_("E196: No digraphs in this version"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008667#endif
8668}
8669
Bram Moolenaar451fc7b2018-04-27 22:53:07 +02008670#if defined(FEAT_SEARCH_EXTRA) || defined(PROTO)
8671 void
8672set_no_hlsearch(int flag)
8673{
8674 no_hlsearch = flag;
8675# ifdef FEAT_EVAL
8676 set_vim_var_nr(VV_HLSEARCH, !no_hlsearch && p_hls);
8677# endif
8678}
8679
Bram Moolenaar071d4272004-06-13 20:20:40 +00008680/*
8681 * ":nohlsearch"
8682 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008683 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008684ex_nohlsearch(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008685{
Bram Moolenaar451fc7b2018-04-27 22:53:07 +02008686 set_no_hlsearch(TRUE);
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00008687 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008688}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008689#endif
8690
8691#ifdef FEAT_CRYPT
8692/*
8693 * ":X": Get crypt key
8694 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008695 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008696ex_X(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008697{
Bram Moolenaar3a0c9082014-11-12 15:15:42 +01008698 crypt_check_current_method();
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02008699 (void)crypt_get_key(TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008700}
8701#endif
8702
8703#ifdef FEAT_FOLDING
8704 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008705ex_fold(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008706{
8707 if (foldManualAllowed(TRUE))
8708 foldCreate(eap->line1, eap->line2);
8709}
8710
8711 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008712ex_foldopen(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008713{
8714 opFoldRange(eap->line1, eap->line2, eap->cmdidx == CMD_foldopen,
8715 eap->forceit, FALSE);
8716}
8717
8718 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008719ex_folddo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008720{
8721 linenr_T lnum;
8722
Bram Moolenaar4facea32019-10-12 20:17:40 +02008723# ifdef FEAT_CLIPBOARD
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02008724 start_global_changes();
Bram Moolenaar4facea32019-10-12 20:17:40 +02008725# endif
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02008726
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008727 // First set the marks for all lines closed/open.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008728 for (lnum = eap->line1; lnum <= eap->line2; ++lnum)
8729 if (hasFolding(lnum, NULL, NULL) == (eap->cmdidx == CMD_folddoclosed))
8730 ml_setmarked(lnum);
8731
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008732 // Execute the command on the marked lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008733 global_exe(eap->arg);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008734 ml_clearmarked(); // clear rest of the marks
Bram Moolenaar4facea32019-10-12 20:17:40 +02008735# ifdef FEAT_CLIPBOARD
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02008736 end_global_changes();
Bram Moolenaar4facea32019-10-12 20:17:40 +02008737# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008738}
8739#endif
Bram Moolenaarf5291f32017-09-14 22:55:37 +02008740
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008741#if defined(FEAT_QUICKFIX) || defined(PROTO)
Bram Moolenaar39665952018-08-15 20:59:48 +02008742/*
8743 * Returns TRUE if the supplied Ex cmdidx is for a location list command
8744 * instead of a quickfix command.
8745 */
8746 int
8747is_loclist_cmd(int cmdidx)
8748{
Bram Moolenaar74c8be22018-08-23 22:51:40 +02008749 if (cmdidx < 0 || cmdidx >= CMD_SIZE)
Bram Moolenaar39665952018-08-15 20:59:48 +02008750 return FALSE;
8751 return cmdnames[cmdidx].cmd_name[0] == 'l';
8752}
8753#endif
8754
Bram Moolenaar4facea32019-10-12 20:17:40 +02008755#if defined(FEAT_TIMERS) || defined(PROTO)
Bram Moolenaarf5291f32017-09-14 22:55:37 +02008756 int
8757get_pressedreturn(void)
8758{
8759 return ex_pressedreturn;
8760}
8761
8762 void
8763set_pressedreturn(int val)
8764{
8765 ex_pressedreturn = val;
8766}
8767#endif