blob: a4db3c84447b2f4d3ddf5119ebfe55b1a329ea25 [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);
1281 vim_free(messages);
1282 messages = next;
1283 }
1284 while (messages != NULL);
1285 }
1286 else if (p != NULL)
1287 {
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01001288 emsg(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001289 vim_free(p);
1290 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001291 vim_free(SOURCING_NAME);
Bram Moolenaare31ee862020-01-07 20:59:34 +01001292 ESTACK_CHECK_NOW
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001293 estack_pop();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001294 }
1295
1296 /*
1297 * On an interrupt or an aborting error not converted to an exception,
1298 * disable the conversion of errors to exceptions. (Interrupts are not
Bram Moolenaar2196bce2020-04-12 20:01:11 +02001299 * converted anymore, here.) This enables also the interrupt message
Bram Moolenaar071d4272004-06-13 20:20:40 +00001300 * when force_abort is set and did_emsg unset in case of an interrupt
1301 * from a finally clause after an error.
1302 */
1303 else if (got_int || (did_emsg && force_abort))
1304 suppress_errthrow = TRUE;
1305 }
1306
1307 /*
1308 * The current cstack will be freed when do_cmdline() returns. An uncaught
1309 * exception will have to be rethrown in the previous cstack. If a function
1310 * has just returned or a script file was just finished and the previous
1311 * cstack belongs to the same function or, respectively, script file, it
1312 * will have to be checked for finally clauses to be executed due to the
1313 * ":return" or ":finish". This is done in do_one_cmd().
1314 */
1315 if (did_throw)
1316 need_rethrow = TRUE;
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001317 if ((getline_equal(fgetline, cookie, getsourceline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001318 && ex_nesting_level > source_level(real_cookie))
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001319 || (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001320 && ex_nesting_level > func_level(real_cookie) + 1))
1321 {
1322 if (!did_throw)
1323 check_cstack = TRUE;
1324 }
1325 else
1326 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001327 // When leaving a function, reduce nesting level.
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001328 if (getline_equal(fgetline, cookie, get_func_line))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001329 --ex_nesting_level;
1330 /*
1331 * Go to debug mode when returning from a function in which we are
1332 * single-stepping.
1333 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001334 if ((getline_equal(fgetline, cookie, getsourceline)
1335 || getline_equal(fgetline, cookie, get_func_line))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001336 && ex_nesting_level + 1 <= debug_break_level)
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001337 do_debug(getline_equal(fgetline, cookie, getsourceline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001338 ? (char_u *)_("End of sourced file")
1339 : (char_u *)_("End of function"));
1340 }
1341
1342 /*
1343 * Restore the exception environment (done after returning from the
1344 * debugger).
1345 */
1346 if (flags & DOCMD_EXCRESET)
Bram Moolenaared203462004-06-16 11:19:22 +00001347 restore_dbg_stuff(&debug_saved);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001348
1349 msg_list = saved_msg_list;
Bram Moolenaar292b90d2020-03-18 15:23:16 +01001350
1351 // Cleanup if "cs_emsg_silent_list" remains.
1352 if (cstack.cs_emsg_silent_list != NULL)
1353 {
1354 eslist_T *elem, *temp;
1355
1356 for (elem = cstack.cs_emsg_silent_list; elem != NULL; elem = temp)
1357 {
1358 temp = elem->next;
1359 vim_free(elem);
1360 }
1361 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001362#endif // FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001363
1364 /*
1365 * If there was too much output to fit on the command line, ask the user to
1366 * hit return before redrawing the screen. With the ":global" command we do
1367 * this only once after the command is finished.
1368 */
1369 if (did_inc)
1370 {
1371 --RedrawingDisabled;
1372 --no_wait_return;
1373 msg_scroll = FALSE;
1374
1375 /*
1376 * When just finished an ":if"-":else" which was typed, no need to
1377 * wait for hit-return. Also for an error situation.
1378 */
1379 if (retval == FAIL
1380#ifdef FEAT_EVAL
1381 || (did_endif && KeyTyped && !did_emsg)
1382#endif
1383 )
1384 {
1385 need_wait_return = FALSE;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001386 msg_didany = FALSE; // don't wait when restarting edit
Bram Moolenaar071d4272004-06-13 20:20:40 +00001387 }
1388 else if (need_wait_return)
1389 {
1390 /*
1391 * The msg_start() above clears msg_didout. The wait_return we do
1392 * here should not overwrite the command that may be shown before
1393 * doing that.
1394 */
1395 msg_didout |= msg_didout_before_start;
1396 wait_return(FALSE);
1397 }
1398 }
1399
Bram Moolenaar2a942252012-11-28 23:03:07 +01001400#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001401 did_endif = FALSE; // in case do_cmdline used recursively
Bram Moolenaar2a942252012-11-28 23:03:07 +01001402#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001403 /*
1404 * Reset if_level, in case a sourced script file contains more ":if" than
1405 * ":endif" (could be ":if x | foo | endif").
1406 */
1407 if_level = 0;
Bram Moolenaar2e18a122012-11-28 19:10:54 +01001408#endif
Bram Moolenaard4ad0d42012-11-28 17:34:48 +01001409
Bram Moolenaar071d4272004-06-13 20:20:40 +00001410 --call_depth;
1411 return retval;
1412}
1413
1414#ifdef FEAT_EVAL
1415/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001416 * Obtain a line when inside a ":while" or ":for" loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001417 */
1418 static char_u *
Bram Moolenaare96a2492019-06-25 04:12:16 +02001419get_loop_line(int c, void *cookie, int indent, int do_concat)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001420{
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001421 struct loop_cookie *cp = (struct loop_cookie *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001422 wcmd_T *wp;
1423 char_u *line;
1424
1425 if (cp->current_line + 1 >= cp->lines_gap->ga_len)
1426 {
1427 if (cp->repeating)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001428 return NULL; // trying to read past ":endwhile"/":endfor"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001429
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001430 // First time inside the ":while"/":for": get line normally.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001431 if (cp->getline == NULL)
Bram Moolenaare96a2492019-06-25 04:12:16 +02001432 line = getcmdline(c, 0L, indent, do_concat);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001433 else
Bram Moolenaare96a2492019-06-25 04:12:16 +02001434 line = cp->getline(c, cp->cookie, indent, do_concat);
Bram Moolenaard68071d2006-05-02 22:08:30 +00001435 if (line != NULL && store_loop_line(cp->lines_gap, line) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001436 ++cp->current_line;
1437
1438 return line;
1439 }
1440
1441 KeyTyped = FALSE;
1442 ++cp->current_line;
1443 wp = (wcmd_T *)(cp->lines_gap->ga_data) + cp->current_line;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001444 SOURCING_LNUM = wp->lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001445 return vim_strsave(wp->line);
1446}
1447
1448/*
1449 * Store a line in "gap" so that a ":while" loop can execute it again.
1450 */
1451 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001452store_loop_line(garray_T *gap, char_u *line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001453{
1454 if (ga_grow(gap, 1) == FAIL)
1455 return FAIL;
1456 ((wcmd_T *)(gap->ga_data))[gap->ga_len].line = vim_strsave(line);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001457 ((wcmd_T *)(gap->ga_data))[gap->ga_len].lnum = SOURCING_LNUM;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001458 ++gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001459 return OK;
1460}
1461
1462/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001463 * Free the lines stored for a ":while" or ":for" loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001464 */
1465 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001466free_cmdlines(garray_T *gap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001467{
1468 while (gap->ga_len > 0)
1469 {
1470 vim_free(((wcmd_T *)(gap->ga_data))[gap->ga_len - 1].line);
1471 --gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001472 }
1473}
1474#endif
1475
1476/*
Bram Moolenaar89d40322006-08-29 15:30:07 +00001477 * If "fgetline" is get_loop_line(), return TRUE if the getline it uses equals
1478 * "func". * Otherwise return TRUE when "fgetline" equals "func".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001479 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001480 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001481getline_equal(
Bram Moolenaare96a2492019-06-25 04:12:16 +02001482 char_u *(*fgetline)(int, void *, int, int),
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001483 void *cookie UNUSED, // argument for fgetline()
Bram Moolenaare96a2492019-06-25 04:12:16 +02001484 char_u *(*func)(int, void *, int, int))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001485{
1486#ifdef FEAT_EVAL
Bram Moolenaare96a2492019-06-25 04:12:16 +02001487 char_u *(*gp)(int, void *, int, int);
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001488 struct loop_cookie *cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001489
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001490 // When "fgetline" is "get_loop_line()" use the "cookie" to find the
1491 // function that's originally used to obtain the lines. This may be
1492 // nested several levels.
Bram Moolenaar89d40322006-08-29 15:30:07 +00001493 gp = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001494 cp = (struct loop_cookie *)cookie;
1495 while (gp == get_loop_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001496 {
1497 gp = cp->getline;
1498 cp = cp->cookie;
1499 }
1500 return gp == func;
1501#else
Bram Moolenaar89d40322006-08-29 15:30:07 +00001502 return fgetline == func;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001503#endif
1504}
1505
Bram Moolenaar071d4272004-06-13 20:20:40 +00001506/*
Bram Moolenaar89d40322006-08-29 15:30:07 +00001507 * If "fgetline" is get_loop_line(), return the cookie used by the original
Bram Moolenaar071d4272004-06-13 20:20:40 +00001508 * getline function. Otherwise return "cookie".
1509 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001510 void *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001511getline_cookie(
Bram Moolenaare96a2492019-06-25 04:12:16 +02001512 char_u *(*fgetline)(int, void *, int, int) UNUSED,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001513 void *cookie) // argument for fgetline()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001514{
Bram Moolenaar13505972019-01-24 15:04:48 +01001515#ifdef FEAT_EVAL
Bram Moolenaare96a2492019-06-25 04:12:16 +02001516 char_u *(*gp)(int, void *, int, int);
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001517 struct loop_cookie *cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001518
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001519 // When "fgetline" is "get_loop_line()" use the "cookie" to find the
1520 // cookie that's originally used to obtain the lines. This may be nested
1521 // several levels.
Bram Moolenaar89d40322006-08-29 15:30:07 +00001522 gp = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001523 cp = (struct loop_cookie *)cookie;
1524 while (gp == get_loop_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001525 {
1526 gp = cp->getline;
1527 cp = cp->cookie;
1528 }
1529 return cp;
Bram Moolenaar13505972019-01-24 15:04:48 +01001530#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001531 return cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001532#endif
Bram Moolenaar13505972019-01-24 15:04:48 +01001533}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001534
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001535
1536/*
1537 * Helper function to apply an offset for buffer commands, i.e. ":bdelete",
1538 * ":bwipeout", etc.
1539 * Returns the buffer number.
1540 */
1541 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001542compute_buffer_local_count(int addr_type, int lnum, int offset)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001543{
1544 buf_T *buf;
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001545 buf_T *nextbuf;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001546 int count = offset;
1547
1548 buf = firstbuf;
1549 while (buf->b_next != NULL && buf->b_fnum < lnum)
1550 buf = buf->b_next;
1551 while (count != 0)
1552 {
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001553 count += (offset < 0) ? 1 : -1;
1554 nextbuf = (offset < 0) ? buf->b_prev : buf->b_next;
1555 if (nextbuf == NULL)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001556 break;
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001557 buf = nextbuf;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001558 if (addr_type == ADDR_LOADED_BUFFERS)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001559 // skip over unloaded buffers
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001560 while (buf->b_ml.ml_mfp == NULL)
1561 {
1562 nextbuf = (offset < 0) ? buf->b_prev : buf->b_next;
1563 if (nextbuf == NULL)
1564 break;
1565 buf = nextbuf;
1566 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001567 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001568 // we might have gone too far, last buffer is not loadedd
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001569 if (addr_type == ADDR_LOADED_BUFFERS)
1570 while (buf->b_ml.ml_mfp == NULL)
1571 {
1572 nextbuf = (offset >= 0) ? buf->b_prev : buf->b_next;
1573 if (nextbuf == NULL)
1574 break;
1575 buf = nextbuf;
1576 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001577 return buf->b_fnum;
1578}
1579
Bram Moolenaarb7316892019-05-01 18:08:42 +02001580/*
1581 * Return the window number of "win".
1582 * When "win" is NULL return the number of windows.
1583 */
Bram Moolenaarf240e182014-11-27 18:33:02 +01001584 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001585current_win_nr(win_T *win)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001586{
1587 win_T *wp;
1588 int nr = 0;
1589
Bram Moolenaar29323592016-07-24 22:04:11 +02001590 FOR_ALL_WINDOWS(wp)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001591 {
1592 ++nr;
1593 if (wp == win)
1594 break;
1595 }
1596 return nr;
1597}
1598
1599 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001600current_tab_nr(tabpage_T *tab)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001601{
1602 tabpage_T *tp;
1603 int nr = 0;
1604
Bram Moolenaar29323592016-07-24 22:04:11 +02001605 FOR_ALL_TABPAGES(tp)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001606 {
1607 ++nr;
1608 if (tp == tab)
1609 break;
1610 }
1611 return nr;
1612}
1613
1614# define CURRENT_WIN_NR current_win_nr(curwin)
1615# define LAST_WIN_NR current_win_nr(NULL)
1616# define CURRENT_TAB_NR current_tab_nr(curtab)
1617# define LAST_TAB_NR current_tab_nr(NULL)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001618
Bram Moolenaar071d4272004-06-13 20:20:40 +00001619/*
1620 * Execute one Ex command.
1621 *
1622 * If 'sourcing' is TRUE, the command will be included in the error message.
1623 *
1624 * 1. skip comment lines and leading space
1625 * 2. handle command modifiers
Bram Moolenaar1c40a662014-11-27 16:38:11 +01001626 * 3. find the command
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001627 * 4. parse range
Bram Moolenaar1c40a662014-11-27 16:38:11 +01001628 * 5. Parse the command.
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001629 * 6. parse arguments
1630 * 7. switch on command name
Bram Moolenaar071d4272004-06-13 20:20:40 +00001631 *
Bram Moolenaar89d40322006-08-29 15:30:07 +00001632 * Note: "fgetline" can be NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001633 *
1634 * This function may be called recursively!
1635 */
1636#if (_MSC_VER == 1200)
1637/*
Bram Moolenaared203462004-06-16 11:19:22 +00001638 * Avoid optimisation bug in VC++ version 6.0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001639 */
Bram Moolenaar281bdce2005-01-25 21:53:18 +00001640 #pragma optimize( "g", off )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001641#endif
1642 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001643do_one_cmd(
Bram Moolenaarddef1292019-12-16 17:10:33 +01001644 char_u **cmdlinep,
1645 int sourcing,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001646#ifdef FEAT_EVAL
Bram Moolenaarddef1292019-12-16 17:10:33 +01001647 cstack_T *cstack,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001648#endif
Bram Moolenaarddef1292019-12-16 17:10:33 +01001649 char_u *(*fgetline)(int, void *, int, int),
1650 void *cookie) // argument for fgetline()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001651{
Bram Moolenaarddef1292019-12-16 17:10:33 +01001652 char_u *p;
1653 linenr_T lnum;
1654 long n;
1655 char *errormsg = NULL; // error message
1656 char_u *after_modifier = NULL;
1657 exarg_T ea; // Ex command arguments
1658 int save_msg_scroll = msg_scroll;
1659 cmdmod_T save_cmdmod;
1660 int save_reg_executing = reg_executing;
1661 int ni; // set when Not Implemented
1662 char_u *cmd;
Bram Moolenaar83f37b92020-02-23 14:35:01 +01001663#ifdef FEAT_EVAL
1664 int starts_with_colon;
1665#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001666
Bram Moolenaara80faa82020-04-12 19:37:17 +02001667 CLEAR_FIELD(ea);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001668 ea.line1 = 1;
1669 ea.line2 = 1;
1670#ifdef FEAT_EVAL
1671 ++ex_nesting_level;
1672#endif
1673
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001674 // When the last file has not been edited :q has to be typed twice.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001675 if (quitmore
1676#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001677 // avoid that a function call in 'statusline' does this
Bram Moolenaar89d40322006-08-29 15:30:07 +00001678 && !getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001679#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001680 // avoid that an autocommand, e.g. QuitPre, does this
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001681 && !getline_equal(fgetline, cookie, getnextac))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001682 --quitmore;
1683
1684 /*
1685 * Reset browse, confirm, etc.. They are restored when returning, for
1686 * recursive calls.
1687 */
1688 save_cmdmod = cmdmod;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001689
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001690 // "#!anything" is handled like a comment.
Bram Moolenaarcbb37ad2006-08-16 15:04:21 +00001691 if ((*cmdlinep)[0] == '#' && (*cmdlinep)[1] == '!')
1692 goto doend;
1693
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02001694/*
1695 * 1. Skip comment lines and leading white space and colons.
1696 * 2. Handle command modifiers.
1697 */
1698 // The "ea" structure holds the arguments that can be used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001699 ea.cmd = *cmdlinep;
Bram Moolenaareffed932018-08-14 13:38:17 +02001700 ea.cmdlinep = cmdlinep;
1701 ea.getline = fgetline;
1702 ea.cookie = cookie;
1703#ifdef FEAT_EVAL
1704 ea.cstack = cstack;
Bram Moolenaar83f37b92020-02-23 14:35:01 +01001705 starts_with_colon = *skipwhite(ea.cmd) == ':';
Bram Moolenaar071d4272004-06-13 20:20:40 +00001706#endif
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02001707 if (parse_command_modifiers(&ea, &errormsg, FALSE) == FAIL)
Bram Moolenaareffed932018-08-14 13:38:17 +02001708 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001709
Bram Moolenaar7b668e82016-08-23 23:51:21 +02001710 after_modifier = ea.cmd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001711
1712#ifdef FEAT_EVAL
1713 ea.skip = did_emsg || got_int || did_throw || (cstack->cs_idx >= 0
1714 && !(cstack->cs_flags[cstack->cs_idx] & CSF_ACTIVE));
1715#else
1716 ea.skip = (if_level > 0);
1717#endif
1718
Bram Moolenaar071d4272004-06-13 20:20:40 +00001719/*
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001720 * 3. Skip over the range to find the command. Let "p" point to after it.
1721 *
1722 * We need the command to know what kind of range it uses.
1723 */
1724 cmd = ea.cmd;
1725 ea.cmd = skip_range(ea.cmd, NULL);
1726 if (*ea.cmd == '*' && vim_strchr(p_cpo, CPO_STAR) == NULL)
1727 ea.cmd = skipwhite(ea.cmd + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001728
1729#ifdef FEAT_EVAL
Bram Moolenaar83f37b92020-02-23 14:35:01 +01001730 if (current_sctx.sc_version == SCRIPT_VERSION_VIM9 && !starts_with_colon)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001731 p = find_ex_command(&ea, NULL, lookup_scriptvar, NULL);
1732 else
1733#endif
1734 p = find_ex_command(&ea, NULL, NULL, NULL);
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001735
Bram Moolenaar7feb35e2018-08-21 17:49:54 +02001736#ifdef FEAT_EVAL
1737# ifdef FEAT_PROFILE
1738 // Count this line for profiling if skip is TRUE.
1739 if (do_profiling == PROF_YES
1740 && (!ea.skip || cstack->cs_idx == 0 || (cstack->cs_idx > 0
1741 && (cstack->cs_flags[cstack->cs_idx - 1] & CSF_ACTIVE))))
1742 {
1743 int skip = did_emsg || got_int || did_throw;
1744
1745 if (ea.cmdidx == CMD_catch)
1746 skip = !skip && !(cstack->cs_idx >= 0
1747 && (cstack->cs_flags[cstack->cs_idx] & CSF_THROWN)
1748 && !(cstack->cs_flags[cstack->cs_idx] & CSF_CAUGHT));
1749 else if (ea.cmdidx == CMD_else || ea.cmdidx == CMD_elseif)
1750 skip = skip || !(cstack->cs_idx >= 0
1751 && !(cstack->cs_flags[cstack->cs_idx]
1752 & (CSF_ACTIVE | CSF_TRUE)));
1753 else if (ea.cmdidx == CMD_finally)
1754 skip = FALSE;
1755 else if (ea.cmdidx != CMD_endif
1756 && ea.cmdidx != CMD_endfor
1757 && ea.cmdidx != CMD_endtry
1758 && ea.cmdidx != CMD_endwhile)
1759 skip = ea.skip;
1760
1761 if (!skip)
1762 {
1763 if (getline_equal(fgetline, cookie, get_func_line))
1764 func_line_exec(getline_cookie(fgetline, cookie));
1765 else if (getline_equal(fgetline, cookie, getsourceline))
1766 script_line_exec();
1767 }
1768 }
1769# endif
1770
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001771 // May go to debug mode. If this happens and the ">quit" debug command is
1772 // used, throw an interrupt exception and skip the next command.
Bram Moolenaar7feb35e2018-08-21 17:49:54 +02001773 dbg_check_breakpoint(&ea);
1774 if (!ea.skip && got_int)
1775 {
1776 ea.skip = TRUE;
1777 (void)do_intthrow(cstack);
1778 }
1779#endif
1780
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001781/*
1782 * 4. parse a range specifier of the form: addr [,addr] [;addr] ..
Bram Moolenaar071d4272004-06-13 20:20:40 +00001783 *
1784 * where 'addr' is:
1785 *
1786 * % (entire file)
1787 * $ [+-NUM]
1788 * 'x [+-NUM] (where x denotes a currently defined mark)
1789 * . [+-NUM]
1790 * [+-NUM]..
1791 * NUM
1792 *
1793 * The ea.cmd pointer is updated to point to the first character following the
1794 * range spec. If an initial address is found, but no second, the upper bound
1795 * is equal to the lower.
1796 */
1797
Bram Moolenaar25190db2019-05-04 15:05:28 +02001798 // ea.addr_type for user commands is set by find_ucmd
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01001799 if (!IS_USER_CMDIDX(ea.cmdidx))
1800 {
1801 if (ea.cmdidx != CMD_SIZE)
1802 ea.addr_type = cmdnames[(int)ea.cmdidx].cmd_addr_type;
1803 else
1804 ea.addr_type = ADDR_LINES;
1805
Bram Moolenaar25190db2019-05-04 15:05:28 +02001806 // :wincmd range depends on the argument.
Bram Moolenaar164f3262015-01-14 21:22:01 +01001807 if (ea.cmdidx == CMD_wincmd && p != NULL)
1808 get_wincmd_addr_type(skipwhite(p), &ea);
Bram Moolenaar25190db2019-05-04 15:05:28 +02001809#ifdef FEAT_QUICKFIX
1810 // :.cc in quickfix window uses line number
1811 if ((ea.cmdidx == CMD_cc || ea.cmdidx == CMD_ll) && bt_quickfix(curbuf))
1812 ea.addr_type = ADDR_OTHER;
1813#endif
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01001814 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001815
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01001816 ea.cmd = cmd;
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02001817 if (parse_cmd_address(&ea, &errormsg, FALSE) == FAIL)
Bram Moolenaaree8415b2018-08-10 23:13:12 +02001818 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001819
Bram Moolenaar071d4272004-06-13 20:20:40 +00001820/*
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001821 * 5. Parse the command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001822 */
1823
1824 /*
1825 * Skip ':' and any white space
1826 */
1827 ea.cmd = skipwhite(ea.cmd);
1828 while (*ea.cmd == ':')
1829 ea.cmd = skipwhite(ea.cmd + 1);
1830
1831 /*
1832 * If we got a line, but no command, then go to the line.
1833 * If we find a '|' or '\n' we set ea.nextcmd.
1834 */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001835 if (*ea.cmd == NUL || *ea.cmd == '"'
Bram Moolenaar7a092242020-04-16 22:10:49 +02001836#ifdef FEAT_EVAL
Bram Moolenaara26b9702020-04-18 19:53:28 +02001837 || (*ea.cmd == '#' && ea.cmd[1] != '{'
1838 && !starts_with_colon && in_vim9script())
Bram Moolenaar7a092242020-04-16 22:10:49 +02001839#endif
1840 || (ea.nextcmd = check_nextcmd(ea.cmd)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001841 {
1842 /*
1843 * strange vi behaviour:
1844 * ":3" jumps to line 3
1845 * ":3|..." prints line 3
1846 * ":|" prints current line
1847 */
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001848 if (ea.skip) // skip this if inside :if
Bram Moolenaar071d4272004-06-13 20:20:40 +00001849 goto doend;
Bram Moolenaardf177f62005-02-22 08:39:57 +00001850 if (*ea.cmd == '|' || (exmode_active && ea.line1 != ea.line2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001851 {
1852 ea.cmdidx = CMD_print;
Bram Moolenaar8071cb22019-07-12 17:58:01 +02001853 ea.argt = EX_RANGE+EX_COUNT+EX_TRLBAR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001854 if ((errormsg = invalid_range(&ea)) == NULL)
1855 {
1856 correct_range(&ea);
1857 ex_print(&ea);
1858 }
1859 }
1860 else if (ea.addr_count != 0)
1861 {
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00001862 if (ea.line2 > curbuf->b_ml.ml_line_count)
1863 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001864 // With '-' in 'cpoptions' a line number past the file is an
1865 // error, otherwise put it at the end of the file.
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00001866 if (vim_strchr(p_cpo, CPO_MINUS) != NULL)
1867 ea.line2 = -1;
1868 else
1869 ea.line2 = curbuf->b_ml.ml_line_count;
1870 }
1871
1872 if (ea.line2 < 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001873 errormsg = _(e_invrange);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001874 else
1875 {
1876 if (ea.line2 == 0)
1877 curwin->w_cursor.lnum = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001878 else
1879 curwin->w_cursor.lnum = ea.line2;
1880 beginline(BL_SOL | BL_FIX);
1881 }
1882 }
1883 goto doend;
1884 }
1885
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001886 // If this looks like an undefined user command and there are CmdUndefined
1887 // autocommands defined, trigger the matching autocommands.
Bram Moolenaard5005162014-08-22 23:05:54 +02001888 if (p != NULL && ea.cmdidx == CMD_SIZE && !ea.skip
1889 && ASCII_ISUPPER(*ea.cmd)
1890 && has_cmdundefined())
1891 {
Bram Moolenaard5005162014-08-22 23:05:54 +02001892 int ret;
1893
Bram Moolenaar5a31b462014-08-23 14:16:20 +02001894 p = ea.cmd;
Bram Moolenaard5005162014-08-22 23:05:54 +02001895 while (ASCII_ISALNUM(*p))
1896 ++p;
Bram Moolenaar120f4a82014-09-09 12:22:06 +02001897 p = vim_strnsave(ea.cmd, (int)(p - ea.cmd));
Bram Moolenaard5005162014-08-22 23:05:54 +02001898 ret = apply_autocmds(EVENT_CMDUNDEFINED, p, p, TRUE, NULL);
1899 vim_free(p);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001900 // If the autocommands did something and didn't cause an error, try
1901 // finding the command again.
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001902 p = (ret
1903#ifdef FEAT_EVAL
1904 && !aborting()
Bram Moolenaard5005162014-08-22 23:05:54 +02001905#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001906 ) ? find_ex_command(&ea, NULL, NULL, NULL) : ea.cmd;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001907 }
Bram Moolenaard5005162014-08-22 23:05:54 +02001908
Bram Moolenaar071d4272004-06-13 20:20:40 +00001909 if (p == NULL)
1910 {
1911 if (!ea.skip)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001912 errormsg = _("E464: Ambiguous use of user-defined command");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001913 goto doend;
1914 }
Bram Moolenaarac9fb182019-04-27 13:04:13 +02001915 // Check for wrong commands.
Bram Moolenaar6f9a4762017-06-22 20:39:17 +02001916 if (*p == '!' && ea.cmd[1] == 0151 && ea.cmd[0] == 78
1917 && !IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001918 {
1919 errormsg = uc_fun_cmd();
1920 goto doend;
1921 }
Bram Moolenaarac9fb182019-04-27 13:04:13 +02001922
Bram Moolenaar071d4272004-06-13 20:20:40 +00001923 if (ea.cmdidx == CMD_SIZE)
1924 {
1925 if (!ea.skip)
1926 {
1927 STRCPY(IObuff, _("E492: Not an editor command"));
1928 if (!sourcing)
Bram Moolenaar7b668e82016-08-23 23:51:21 +02001929 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001930 // If the modifier was parsed OK the error must be in the
1931 // following command
Bram Moolenaar7b668e82016-08-23 23:51:21 +02001932 if (after_modifier != NULL)
1933 append_command(after_modifier);
1934 else
1935 append_command(*cmdlinep);
1936 }
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001937 errormsg = (char *)IObuff;
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001938 did_emsg_syntax = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001939 }
1940 goto doend;
1941 }
1942
Bram Moolenaar958636c2014-10-21 20:01:58 +02001943 ni = (!IS_USER_CMDIDX(ea.cmdidx)
1944 && (cmdnames[ea.cmdidx].cmd_func == ex_ni
Bram Moolenaar7bb75552007-07-16 18:39:49 +00001945#ifdef HAVE_EX_SCRIPT_NI
1946 || cmdnames[ea.cmdidx].cmd_func == ex_script_ni
1947#endif
1948 ));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001949
1950#ifndef FEAT_EVAL
1951 /*
1952 * When the expression evaluation is disabled, recognize the ":if" and
1953 * ":endif" commands and ignore everything in between it.
1954 */
1955 if (ea.cmdidx == CMD_if)
1956 ++if_level;
1957 if (if_level)
1958 {
1959 if (ea.cmdidx == CMD_endif)
1960 --if_level;
1961 goto doend;
1962 }
1963
1964#endif
1965
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001966 // forced commands
Bram Moolenaar196b3b02008-06-20 16:51:41 +00001967 if (*p == '!' && ea.cmdidx != CMD_substitute
1968 && ea.cmdidx != CMD_smagic && ea.cmdidx != CMD_snomagic)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001969 {
1970 ++p;
1971 ea.forceit = TRUE;
1972 }
1973 else
1974 ea.forceit = FALSE;
1975
1976/*
Bram Moolenaarb7316892019-05-01 18:08:42 +02001977 * 6. Parse arguments. Then check for errors.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001978 */
Bram Moolenaar958636c2014-10-21 20:01:58 +02001979 if (!IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001980 ea.argt = (long)cmdnames[(int)ea.cmdidx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001981
1982 if (!ea.skip)
1983 {
1984#ifdef HAVE_SANDBOX
Bram Moolenaar8071cb22019-07-12 17:58:01 +02001985 if (sandbox != 0 && !(ea.argt & EX_SBOXOK))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001986 {
Bram Moolenaar8c62a082019-02-08 14:34:10 +01001987 // Command not allowed in sandbox.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001988 errormsg = _(e_sandbox);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001989 goto doend;
1990 }
1991#endif
Bram Moolenaar8071cb22019-07-12 17:58:01 +02001992 if (restricted != 0 && (ea.argt & EX_RESTRICT))
Bram Moolenaar8c62a082019-02-08 14:34:10 +01001993 {
1994 errormsg = _("E981: Command not allowed in rvim");
1995 goto doend;
1996 }
Bram Moolenaar8071cb22019-07-12 17:58:01 +02001997 if (!curbuf->b_p_ma && (ea.argt & EX_MODIFY))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001998 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001999 // Command not allowed in non-'modifiable' buffer
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002000 errormsg = _(e_modifiable);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002001 goto doend;
2002 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002003
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002004 if (text_locked() && !(ea.argt & EX_CMDWIN)
Bram Moolenaar958636c2014-10-21 20:01:58 +02002005 && !IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002006 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002007 // Command not allowed when editing the command line.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002008 errormsg = _(get_text_locked_msg());
Bram Moolenaar071d4272004-06-13 20:20:40 +00002009 goto doend;
2010 }
Bram Moolenaar379fb762018-08-30 15:58:28 +02002011
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002012 // Disallow editing another buffer when "curbuf_lock" is set.
2013 // Do allow ":checktime" (it is postponed).
2014 // Do allow ":edit" (check for an argument later).
2015 // Do allow ":file" with no arguments (check for an argument later).
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002016 if (!(ea.argt & EX_CMDWIN)
Bram Moolenaar5555acc2006-04-07 21:33:12 +00002017 && ea.cmdidx != CMD_checktime
Bram Moolenaar379fb762018-08-30 15:58:28 +02002018 && ea.cmdidx != CMD_edit
2019 && ea.cmdidx != CMD_file
Bram Moolenaar958636c2014-10-21 20:01:58 +02002020 && !IS_USER_CMDIDX(ea.cmdidx)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002021 && curbuf_locked())
2022 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002023
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002024 if (!ni && !(ea.argt & EX_RANGE) && ea.addr_count > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002025 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002026 // no range allowed
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002027 errormsg = _(e_norange);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002028 goto doend;
2029 }
2030 }
2031
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002032 if (!ni && !(ea.argt & EX_BANG) && ea.forceit) // no <!> allowed
Bram Moolenaar071d4272004-06-13 20:20:40 +00002033 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002034 errormsg = _(e_nobang);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002035 goto doend;
2036 }
2037
2038 /*
2039 * Don't complain about the range if it is not used
2040 * (could happen if line_count is accidentally set to 0).
2041 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002042 if (!ea.skip && !ni && (ea.argt & EX_RANGE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002043 {
2044 /*
2045 * If the range is backwards, ask for confirmation and, if given, swap
2046 * ea.line1 & ea.line2 so it's forwards again.
2047 * When global command is busy, don't ask, will fail below.
2048 */
2049 if (!global_busy && ea.line1 > ea.line2)
2050 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002051 if (msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002052 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002053 if (sourcing || exmode_active)
2054 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002055 errormsg = _("E493: Backwards range given");
Bram Moolenaara5792f52005-11-23 21:25:05 +00002056 goto doend;
2057 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002058 if (ask_yesno((char_u *)
2059 _("Backwards range given, OK to swap"), FALSE) != 'y')
Bram Moolenaara5792f52005-11-23 21:25:05 +00002060 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002061 }
2062 lnum = ea.line1;
2063 ea.line1 = ea.line2;
2064 ea.line2 = lnum;
2065 }
2066 if ((errormsg = invalid_range(&ea)) != NULL)
2067 goto doend;
2068 }
2069
Bram Moolenaarb7316892019-05-01 18:08:42 +02002070 if ((ea.addr_type == ADDR_OTHER) && ea.addr_count == 0)
2071 // default is 1, not cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00002072 ea.line2 = 1;
2073
2074 correct_range(&ea);
2075
2076#ifdef FEAT_FOLDING
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002077 if (((ea.argt & EX_WHOLEFOLD) || ea.addr_count >= 2) && !global_busy
Bram Moolenaara3306952016-01-02 21:41:06 +01002078 && ea.addr_type == ADDR_LINES)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002079 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002080 // Put the first line at the start of a closed fold, put the last line
2081 // at the end of a closed fold.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002082 (void)hasFolding(ea.line1, &ea.line1, NULL);
2083 (void)hasFolding(ea.line2, NULL, &ea.line2);
2084 }
2085#endif
2086
2087#ifdef FEAT_QUICKFIX
2088 /*
Bram Moolenaar86b68352004-12-27 21:59:20 +00002089 * For the ":make" and ":grep" commands we insert the 'makeprg'/'grepprg'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002090 * option here, so things like % get expanded.
2091 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002092 p = replace_makeprg(&ea, p, cmdlinep);
2093 if (p == NULL)
2094 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002095#endif
2096
2097 /*
2098 * Skip to start of argument.
2099 * Don't do this for the ":!" command, because ":!! -l" needs the space.
2100 */
2101 if (ea.cmdidx == CMD_bang)
2102 ea.arg = p;
2103 else
2104 ea.arg = skipwhite(p);
2105
Bram Moolenaar379fb762018-08-30 15:58:28 +02002106 // ":file" cannot be run with an argument when "curbuf_lock" is set
2107 if (ea.cmdidx == CMD_file && *ea.arg != NUL && curbuf_locked())
2108 goto doend;
2109
Bram Moolenaar071d4272004-06-13 20:20:40 +00002110 /*
2111 * Check for "++opt=val" argument.
2112 * Must be first, allow ":w ++enc=utf8 !cmd"
2113 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002114 if (ea.argt & EX_ARGOPT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002115 while (ea.arg[0] == '+' && ea.arg[1] == '+')
2116 if (getargopt(&ea) == FAIL && !ni)
2117 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002118 errormsg = _(e_invarg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002119 goto doend;
2120 }
2121
2122 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
2123 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002124 if (*ea.arg == '>') // append
Bram Moolenaar071d4272004-06-13 20:20:40 +00002125 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002126 if (*++ea.arg != '>') // typed wrong
Bram Moolenaar071d4272004-06-13 20:20:40 +00002127 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002128 errormsg = _("E494: Use w or w>>");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002129 goto doend;
2130 }
2131 ea.arg = skipwhite(ea.arg + 1);
2132 ea.append = TRUE;
2133 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002134 else if (*ea.arg == '!' && ea.cmdidx == CMD_write) // :w !filter
Bram Moolenaar071d4272004-06-13 20:20:40 +00002135 {
2136 ++ea.arg;
2137 ea.usefilter = TRUE;
2138 }
2139 }
2140
2141 if (ea.cmdidx == CMD_read)
2142 {
2143 if (ea.forceit)
2144 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002145 ea.usefilter = TRUE; // :r! filter if ea.forceit
Bram Moolenaar071d4272004-06-13 20:20:40 +00002146 ea.forceit = FALSE;
2147 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002148 else if (*ea.arg == '!') // :r !filter
Bram Moolenaar071d4272004-06-13 20:20:40 +00002149 {
2150 ++ea.arg;
2151 ea.usefilter = TRUE;
2152 }
2153 }
2154
2155 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
2156 {
2157 ea.amount = 1;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002158 while (*ea.arg == *ea.cmd) // count number of '>' or '<'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002159 {
2160 ++ea.arg;
2161 ++ea.amount;
2162 }
2163 ea.arg = skipwhite(ea.arg);
2164 }
2165
2166 /*
2167 * Check for "+command" argument, before checking for next command.
2168 * Don't do this for ":read !cmd" and ":write !cmd".
2169 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002170 if ((ea.argt & EX_CMDARG) && !ea.usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002171 ea.do_ecmd_cmd = getargcmd(&ea.arg);
2172
2173 /*
2174 * Check for '|' to separate commands and '"' to start comments.
2175 * Don't do this for ":read !cmd" and ":write !cmd".
2176 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002177 if ((ea.argt & EX_TRLBAR) && !ea.usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002178 separate_nextcmd(&ea);
2179
2180 /*
2181 * Check for <newline> to end a shell command.
Bram Moolenaardf177f62005-02-22 08:39:57 +00002182 * Also do this for ":read !cmd", ":write !cmd" and ":global".
2183 * Any others?
Bram Moolenaar071d4272004-06-13 20:20:40 +00002184 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00002185 else if (ea.cmdidx == CMD_bang
Bram Moolenaar67883b42017-07-27 22:57:00 +02002186 || ea.cmdidx == CMD_terminal
Bram Moolenaardf177f62005-02-22 08:39:57 +00002187 || ea.cmdidx == CMD_global
2188 || ea.cmdidx == CMD_vglobal
2189 || ea.usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002190 {
2191 for (p = ea.arg; *p; ++p)
2192 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002193 // Remove one backslash before a newline, so that it's possible to
2194 // pass a newline to the shell and also a newline that is preceded
2195 // with a backslash. This makes it impossible to end a shell
2196 // command in a backslash, but that doesn't appear useful.
2197 // Halving the number of backslashes is incompatible with previous
2198 // versions.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002199 if (*p == '\\' && p[1] == '\n')
Bram Moolenaara7241f52008-06-24 20:39:31 +00002200 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002201 else if (*p == '\n')
2202 {
2203 ea.nextcmd = p + 1;
2204 *p = NUL;
2205 break;
2206 }
2207 }
2208 }
2209
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002210 if ((ea.argt & EX_DFLALL) && ea.addr_count == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002211 {
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002212 buf_T *buf;
2213
Bram Moolenaar071d4272004-06-13 20:20:40 +00002214 ea.line1 = 1;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002215 switch (ea.addr_type)
2216 {
2217 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02002218 case ADDR_OTHER:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002219 ea.line2 = curbuf->b_ml.ml_line_count;
2220 break;
2221 case ADDR_LOADED_BUFFERS:
2222 buf = firstbuf;
2223 while (buf->b_next != NULL && buf->b_ml.ml_mfp == NULL)
2224 buf = buf->b_next;
2225 ea.line1 = buf->b_fnum;
2226 buf = lastbuf;
2227 while (buf->b_prev != NULL && buf->b_ml.ml_mfp == NULL)
2228 buf = buf->b_prev;
2229 ea.line2 = buf->b_fnum;
2230 break;
2231 case ADDR_BUFFERS:
2232 ea.line1 = firstbuf->b_fnum;
2233 ea.line2 = lastbuf->b_fnum;
2234 break;
2235 case ADDR_WINDOWS:
2236 ea.line2 = LAST_WIN_NR;
2237 break;
2238 case ADDR_TABS:
2239 ea.line2 = LAST_TAB_NR;
2240 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01002241 case ADDR_TABS_RELATIVE:
2242 ea.line2 = 1;
2243 break;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002244 case ADDR_ARGUMENTS:
2245 if (ARGCOUNT == 0)
2246 ea.line1 = ea.line2 = 0;
2247 else
2248 ea.line2 = ARGCOUNT;
2249 break;
Bram Moolenaar25190db2019-05-04 15:05:28 +02002250 case ADDR_QUICKFIX_VALID:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02002251#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02002252 ea.line2 = qf_get_valid_size(&ea);
Bram Moolenaaraa23b372015-09-08 18:46:31 +02002253 if (ea.line2 == 0)
2254 ea.line2 = 1;
Bram Moolenaare906c502015-09-09 21:10:39 +02002255#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02002256 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02002257 case ADDR_NONE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02002258 case ADDR_UNSIGNED:
2259 case ADDR_QUICKFIX:
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002260 iemsg(_("INTERNAL: Cannot use EX_DFLALL with ADDR_NONE, ADDR_UNSIGNED or ADDR_QUICKFIX"));
Bram Moolenaarb7316892019-05-01 18:08:42 +02002261 break;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002262 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002263 }
2264
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002265 // accept numbered register only when no count allowed (:put)
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002266 if ( (ea.argt & EX_REGSTR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002267 && *ea.arg != NUL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002268 // Do not allow register = for user commands
Bram Moolenaar958636c2014-10-21 20:01:58 +02002269 && (!IS_USER_CMDIDX(ea.cmdidx) || *ea.arg != '=')
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002270 && !((ea.argt & EX_COUNT) && VIM_ISDIGIT(*ea.arg)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002271 {
Bram Moolenaar85de2062011-05-05 14:26:41 +02002272#ifndef FEAT_CLIPBOARD
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002273 // check these explicitly for a more specific error message
Bram Moolenaar85de2062011-05-05 14:26:41 +02002274 if (*ea.arg == '*' || *ea.arg == '+')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002275 {
Bram Moolenaar99b12722019-01-14 20:16:40 +01002276 errormsg = _(e_invalidreg);
Bram Moolenaar85de2062011-05-05 14:26:41 +02002277 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002278 }
2279#endif
Bram Moolenaar958636c2014-10-21 20:01:58 +02002280 if (valid_yank_reg(*ea.arg, (ea.cmdidx != CMD_put
2281 && !IS_USER_CMDIDX(ea.cmdidx))))
Bram Moolenaar85de2062011-05-05 14:26:41 +02002282 {
2283 ea.regname = *ea.arg++;
2284#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002285 // for '=' register: accept the rest of the line as an expression
Bram Moolenaar85de2062011-05-05 14:26:41 +02002286 if (ea.arg[-1] == '=' && ea.arg[0] != NUL)
2287 {
2288 set_expr_line(vim_strsave(ea.arg));
2289 ea.arg += STRLEN(ea.arg);
2290 }
2291#endif
2292 ea.arg = skipwhite(ea.arg);
2293 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002294 }
2295
2296 /*
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002297 * Check for a count. When accepting a EX_BUFNAME, don't use "123foo" as a
Bram Moolenaar071d4272004-06-13 20:20:40 +00002298 * count, it's a buffer name.
2299 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002300 if ((ea.argt & EX_COUNT) && VIM_ISDIGIT(*ea.arg)
2301 && (!(ea.argt & EX_BUFNAME) || *(p = skipdigits(ea.arg)) == NUL
Bram Moolenaar1c465442017-03-12 20:10:05 +01002302 || VIM_ISWHITE(*p)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002303 {
2304 n = getdigits(&ea.arg);
2305 ea.arg = skipwhite(ea.arg);
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002306 if (n <= 0 && !ni && (ea.argt & EX_ZEROR) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002307 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002308 errormsg = _(e_zerocount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002309 goto doend;
2310 }
Bram Moolenaarb7316892019-05-01 18:08:42 +02002311 if (ea.addr_type != ADDR_LINES) // e.g. :buffer 2, :sleep 3
Bram Moolenaar071d4272004-06-13 20:20:40 +00002312 {
2313 ea.line2 = n;
2314 if (ea.addr_count == 0)
2315 ea.addr_count = 1;
2316 }
2317 else
2318 {
2319 ea.line1 = ea.line2;
2320 ea.line2 += n - 1;
2321 ++ea.addr_count;
2322 /*
2323 * Be vi compatible: no error message for out of range.
2324 */
Bram Moolenaarb7316892019-05-01 18:08:42 +02002325 if (ea.line2 > curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002326 ea.line2 = curbuf->b_ml.ml_line_count;
2327 }
2328 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00002329
2330 /*
2331 * Check for flags: 'l', 'p' and '#'.
2332 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002333 if (ea.argt & EX_FLAGS)
Bram Moolenaardf177f62005-02-22 08:39:57 +00002334 get_flags(&ea);
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002335 if (!ni && !(ea.argt & EX_EXTRA) && *ea.arg != NUL
2336 && *ea.arg != '"' && (*ea.arg != '|' || (ea.argt & EX_TRLBAR) == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002337 {
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002338 // no arguments allowed but there is something
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002339 errormsg = _(e_trailing);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002340 goto doend;
2341 }
2342
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002343 if (!ni && (ea.argt & EX_NEEDARG) && *ea.arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002344 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002345 errormsg = _(e_argreq);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002346 goto doend;
2347 }
2348
2349#ifdef FEAT_EVAL
2350 /*
2351 * Skip the command when it's not going to be executed.
2352 * The commands like :if, :endif, etc. always need to be executed.
2353 * Also make an exception for commands that handle a trailing command
2354 * themselves.
2355 */
2356 if (ea.skip)
2357 {
2358 switch (ea.cmdidx)
2359 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002360 // commands that need evaluation
Bram Moolenaar071d4272004-06-13 20:20:40 +00002361 case CMD_while:
2362 case CMD_endwhile:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00002363 case CMD_for:
2364 case CMD_endfor:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002365 case CMD_if:
2366 case CMD_elseif:
2367 case CMD_else:
2368 case CMD_endif:
2369 case CMD_try:
2370 case CMD_catch:
2371 case CMD_finally:
2372 case CMD_endtry:
2373 case CMD_function:
Bram Moolenaarab55c682020-03-01 19:41:43 +01002374 case CMD_def:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002375 break;
2376
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002377 // Commands that handle '|' themselves. Check: A command should
2378 // either have the EX_TRLBAR flag, appear in this list or appear in
2379 // the list at ":help :bar".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002380 case CMD_aboveleft:
2381 case CMD_and:
2382 case CMD_belowright:
2383 case CMD_botright:
2384 case CMD_browse:
2385 case CMD_call:
2386 case CMD_confirm:
Bram Moolenaar8f76e6b2019-11-26 16:50:30 +01002387 case CMD_const:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002388 case CMD_delfunction:
2389 case CMD_djump:
2390 case CMD_dlist:
2391 case CMD_dsearch:
2392 case CMD_dsplit:
2393 case CMD_echo:
2394 case CMD_echoerr:
2395 case CMD_echomsg:
2396 case CMD_echon:
Bram Moolenaara76b3152020-02-16 16:20:21 +01002397 case CMD_eval:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002398 case CMD_execute:
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002399 case CMD_filter:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002400 case CMD_help:
2401 case CMD_hide:
2402 case CMD_ijump:
2403 case CMD_ilist:
2404 case CMD_isearch:
2405 case CMD_isplit:
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002406 case CMD_keepalt:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002407 case CMD_keepjumps:
2408 case CMD_keepmarks:
Bram Moolenaara939e432013-11-09 05:30:26 +01002409 case CMD_keeppatterns:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002410 case CMD_leftabove:
2411 case CMD_let:
2412 case CMD_lockmarks:
Bram Moolenaarcc4423a2019-11-26 17:05:00 +01002413 case CMD_lockvar:
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002414 case CMD_lua:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002415 case CMD_match:
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002416 case CMD_mzscheme:
Bram Moolenaar5803ae62014-03-23 16:04:02 +01002417 case CMD_noautocmd:
2418 case CMD_noswapfile:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002419 case CMD_perl:
2420 case CMD_psearch:
2421 case CMD_python:
Bram Moolenaar368373e2010-07-19 20:46:22 +02002422 case CMD_py3:
Bram Moolenaarb6590522010-07-21 16:00:43 +02002423 case CMD_python3:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002424 case CMD_return:
2425 case CMD_rightbelow:
2426 case CMD_ruby:
2427 case CMD_silent:
2428 case CMD_smagic:
2429 case CMD_snomagic:
2430 case CMD_substitute:
2431 case CMD_syntax:
Bram Moolenaara226a6d2006-02-26 23:59:20 +00002432 case CMD_tab:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002433 case CMD_tcl:
2434 case CMD_throw:
2435 case CMD_tilde:
2436 case CMD_topleft:
2437 case CMD_unlet:
Bram Moolenaarcc4423a2019-11-26 17:05:00 +01002438 case CMD_unlockvar:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002439 case CMD_verbose:
2440 case CMD_vertical:
Bram Moolenaar12bde492011-06-13 01:19:56 +02002441 case CMD_wincmd:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002442 break;
2443
2444 default: goto doend;
2445 }
2446 }
2447#endif
2448
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002449 if (ea.argt & EX_XFILE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002450 {
2451 if (expand_filename(&ea, cmdlinep, &errormsg) == FAIL)
2452 goto doend;
2453 }
2454
Bram Moolenaar071d4272004-06-13 20:20:40 +00002455 /*
2456 * Accept buffer name. Cannot be used at the same time with a buffer
2457 * number. Don't do this for a user command.
2458 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002459 if ((ea.argt & EX_BUFNAME) && *ea.arg != NUL && ea.addr_count == 0
Bram Moolenaar958636c2014-10-21 20:01:58 +02002460 && !IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002461 {
2462 /*
2463 * :bdelete, :bwipeout and :bunload take several arguments, separated
2464 * by spaces: find next space (skipping over escaped characters).
2465 * The others take one argument: ignore trailing spaces.
2466 */
2467 if (ea.cmdidx == CMD_bdelete || ea.cmdidx == CMD_bwipeout
2468 || ea.cmdidx == CMD_bunload)
2469 p = skiptowhite_esc(ea.arg);
2470 else
2471 {
2472 p = ea.arg + STRLEN(ea.arg);
Bram Moolenaar1c465442017-03-12 20:10:05 +01002473 while (p > ea.arg && VIM_ISWHITE(p[-1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002474 --p;
2475 }
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002476 ea.line2 = buflist_findpat(ea.arg, p, (ea.argt & EX_BUFUNL) != 0,
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002477 FALSE, FALSE);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002478 if (ea.line2 < 0) // failed
Bram Moolenaar071d4272004-06-13 20:20:40 +00002479 goto doend;
2480 ea.addr_count = 1;
2481 ea.arg = skipwhite(p);
2482 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002483
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002484 // The :try command saves the emsg_silent flag, reset it here when
2485 // ":silent! try" was used, it should only apply to :try itself.
Bram Moolenaareffed932018-08-14 13:38:17 +02002486 if (ea.cmdidx == CMD_try && ea.did_esilent > 0)
Bram Moolenaar2be57332018-02-13 18:05:18 +01002487 {
Bram Moolenaareffed932018-08-14 13:38:17 +02002488 emsg_silent -= ea.did_esilent;
Bram Moolenaar2be57332018-02-13 18:05:18 +01002489 if (emsg_silent < 0)
2490 emsg_silent = 0;
Bram Moolenaareffed932018-08-14 13:38:17 +02002491 ea.did_esilent = 0;
Bram Moolenaar2be57332018-02-13 18:05:18 +01002492 }
2493
Bram Moolenaar071d4272004-06-13 20:20:40 +00002494/*
Bram Moolenaar2be57332018-02-13 18:05:18 +01002495 * 7. Execute the command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002496 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002497
Bram Moolenaar958636c2014-10-21 20:01:58 +02002498 if (IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002499 {
2500 /*
2501 * Execute a user-defined command.
2502 */
2503 do_ucmd(&ea);
2504 }
2505 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002506 {
2507 /*
Bram Moolenaarac9fb182019-04-27 13:04:13 +02002508 * Call the function to execute the builtin command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002509 */
2510 ea.errmsg = NULL;
2511 (cmdnames[ea.cmdidx].cmd_func)(&ea);
2512 if (ea.errmsg != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002513 errormsg = _(ea.errmsg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002514 }
2515
2516#ifdef FEAT_EVAL
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002517 // Set flag that any command was executed, used by ex_vim9script().
Bram Moolenaar16531552020-02-08 16:00:46 +01002518 if (getline_equal(ea.getline, ea.cookie, getsourceline)
2519 && current_sctx.sc_sid > 0)
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002520 SCRIPT_ITEM(current_sctx.sc_sid)->sn_had_command = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002521
Bram Moolenaar071d4272004-06-13 20:20:40 +00002522 /*
2523 * If the command just executed called do_cmdline(), any throw or ":return"
2524 * or ":finish" encountered there must also check the cstack of the still
2525 * active do_cmdline() that called this do_one_cmd(). Rethrow an uncaught
2526 * exception, or reanimate a returned function or finished script file and
2527 * return or finish it again.
2528 */
2529 if (need_rethrow)
2530 do_throw(cstack);
2531 else if (check_cstack)
2532 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002533 if (source_finished(fgetline, cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002534 do_finish(&ea, TRUE);
Bram Moolenaar89d40322006-08-29 15:30:07 +00002535 else if (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002536 && current_func_returned())
2537 do_return(&ea, TRUE, FALSE, NULL);
2538 }
2539 need_rethrow = check_cstack = FALSE;
2540#endif
2541
2542doend:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002543 if (curwin->w_cursor.lnum == 0) // can happen with zero line number
Bram Moolenaar6de5e122017-04-20 21:55:44 +02002544 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002545 curwin->w_cursor.lnum = 1;
Bram Moolenaar6de5e122017-04-20 21:55:44 +02002546 curwin->w_cursor.col = 0;
2547 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002548
2549 if (errormsg != NULL && *errormsg != NUL && !did_emsg)
2550 {
2551 if (sourcing)
2552 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002553 if (errormsg != (char *)IObuff)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002554 {
2555 STRCPY(IObuff, errormsg);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002556 errormsg = (char *)IObuff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002557 }
Bram Moolenaara6f4d612011-09-21 19:10:46 +02002558 append_command(*cmdlinep);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002559 }
2560 emsg(errormsg);
2561 }
2562#ifdef FEAT_EVAL
2563 do_errthrow(cstack,
Bram Moolenaar958636c2014-10-21 20:01:58 +02002564 (ea.cmdidx != CMD_SIZE && !IS_USER_CMDIDX(ea.cmdidx))
2565 ? cmdnames[(int)ea.cmdidx].cmd_name : (char_u *)NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002566#endif
2567
Bram Moolenaareffed932018-08-14 13:38:17 +02002568 if (ea.verbose_save >= 0)
2569 p_verbose = ea.verbose_save;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002570
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002571 free_cmdmod();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002572 cmdmod = save_cmdmod;
Bram Moolenaar9a2c0912019-03-30 14:26:18 +01002573 reg_executing = save_reg_executing;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002574
Bram Moolenaareffed932018-08-14 13:38:17 +02002575 if (ea.save_msg_silent != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002576 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002577 // messages could be enabled for a serious error, need to check if the
2578 // counters don't become negative
Bram Moolenaareffed932018-08-14 13:38:17 +02002579 if (!did_emsg || msg_silent > ea.save_msg_silent)
2580 msg_silent = ea.save_msg_silent;
2581 emsg_silent -= ea.did_esilent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002582 if (emsg_silent < 0)
2583 emsg_silent = 0;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002584 // Restore msg_scroll, it's set by file I/O commands, even when no
2585 // message is actually displayed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002586 msg_scroll = save_msg_scroll;
Bram Moolenaar77ab2802009-04-22 12:44:48 +00002587
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002588 // "silent reg" or "silent echo x" inside "redir" leaves msg_col
2589 // somewhere in the line. Put it back in the first column.
Bram Moolenaar77ab2802009-04-22 12:44:48 +00002590 if (redirecting())
2591 msg_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002592 }
2593
Bram Moolenaar7171abe2004-10-11 10:06:20 +00002594#ifdef HAVE_SANDBOX
Bram Moolenaareffed932018-08-14 13:38:17 +02002595 if (ea.did_sandbox)
Bram Moolenaar7171abe2004-10-11 10:06:20 +00002596 --sandbox;
2597#endif
2598
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002599 if (ea.nextcmd && *ea.nextcmd == NUL) // not really a next command
Bram Moolenaar071d4272004-06-13 20:20:40 +00002600 ea.nextcmd = NULL;
2601
2602#ifdef FEAT_EVAL
2603 --ex_nesting_level;
2604#endif
2605
2606 return ea.nextcmd;
2607}
2608#if (_MSC_VER == 1200)
Bram Moolenaar281bdce2005-01-25 21:53:18 +00002609 #pragma optimize( "", on )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002610#endif
2611
2612/*
Bram Moolenaareffed932018-08-14 13:38:17 +02002613 * Parse and skip over command modifiers:
2614 * - update eap->cmd
2615 * - store flags in "cmdmod".
2616 * - Set ex_pressedreturn for an empty command line.
2617 * - set msg_silent for ":silent"
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002618 * - set 'eventignore' to "all" for ":noautocmd"
Bram Moolenaareffed932018-08-14 13:38:17 +02002619 * - set p_verbose for ":verbose"
2620 * - Increment "sandbox" for ":sandbox"
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002621 * When "skip_only" is TRUE the global variables are not changed, except for
2622 * "cmdmod".
Bram Moolenaareffed932018-08-14 13:38:17 +02002623 * Return FAIL when the command is not to be executed.
2624 * May set "errormsg" to an error message.
2625 */
2626 int
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002627parse_command_modifiers(exarg_T *eap, char **errormsg, int skip_only)
Bram Moolenaareffed932018-08-14 13:38:17 +02002628{
2629 char_u *p;
2630
Bram Moolenaara80faa82020-04-12 19:37:17 +02002631 CLEAR_FIELD(cmdmod);
Bram Moolenaareffed932018-08-14 13:38:17 +02002632 eap->verbose_save = -1;
2633 eap->save_msg_silent = -1;
2634
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002635 // Repeat until no more command modifiers are found.
Bram Moolenaareffed932018-08-14 13:38:17 +02002636 for (;;)
2637 {
Bram Moolenaareffed932018-08-14 13:38:17 +02002638 while (*eap->cmd == ' ' || *eap->cmd == '\t' || *eap->cmd == ':')
2639 ++eap->cmd;
2640
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002641 // in ex mode, an empty line works like :+
Bram Moolenaareffed932018-08-14 13:38:17 +02002642 if (*eap->cmd == NUL && exmode_active
2643 && (getline_equal(eap->getline, eap->cookie, getexmodeline)
2644 || getline_equal(eap->getline, eap->cookie, getexline))
2645 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
2646 {
2647 eap->cmd = (char_u *)"+";
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002648 if (!skip_only)
2649 ex_pressedreturn = TRUE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002650 }
2651
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002652 // ignore comment and empty lines
Bram Moolenaareffed932018-08-14 13:38:17 +02002653 if (*eap->cmd == '"')
2654 return FAIL;
2655 if (*eap->cmd == NUL)
2656 {
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002657 if (!skip_only)
2658 ex_pressedreturn = TRUE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002659 return FAIL;
2660 }
2661
Bram Moolenaareffed932018-08-14 13:38:17 +02002662 p = skip_range(eap->cmd, NULL);
2663 switch (*p)
2664 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002665 // When adding an entry, also modify cmd_exists().
Bram Moolenaareffed932018-08-14 13:38:17 +02002666 case 'a': if (!checkforcmd(&eap->cmd, "aboveleft", 3))
2667 break;
2668 cmdmod.split |= WSP_ABOVE;
2669 continue;
2670
2671 case 'b': if (checkforcmd(&eap->cmd, "belowright", 3))
2672 {
2673 cmdmod.split |= WSP_BELOW;
2674 continue;
2675 }
2676 if (checkforcmd(&eap->cmd, "browse", 3))
2677 {
2678#ifdef FEAT_BROWSE_CMD
2679 cmdmod.browse = TRUE;
2680#endif
2681 continue;
2682 }
2683 if (!checkforcmd(&eap->cmd, "botright", 2))
2684 break;
2685 cmdmod.split |= WSP_BOT;
2686 continue;
2687
2688 case 'c': if (!checkforcmd(&eap->cmd, "confirm", 4))
2689 break;
2690#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
2691 cmdmod.confirm = TRUE;
2692#endif
2693 continue;
2694
2695 case 'k': if (checkforcmd(&eap->cmd, "keepmarks", 3))
2696 {
2697 cmdmod.keepmarks = TRUE;
2698 continue;
2699 }
2700 if (checkforcmd(&eap->cmd, "keepalt", 5))
2701 {
2702 cmdmod.keepalt = TRUE;
2703 continue;
2704 }
2705 if (checkforcmd(&eap->cmd, "keeppatterns", 5))
2706 {
2707 cmdmod.keeppatterns = TRUE;
2708 continue;
2709 }
2710 if (!checkforcmd(&eap->cmd, "keepjumps", 5))
2711 break;
2712 cmdmod.keepjumps = TRUE;
2713 continue;
2714
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002715 case 'f': // only accept ":filter {pat} cmd"
Bram Moolenaareffed932018-08-14 13:38:17 +02002716 {
2717 char_u *reg_pat;
2718
2719 if (!checkforcmd(&p, "filter", 4)
2720 || *p == NUL || ends_excmd(*p))
2721 break;
2722 if (*p == '!')
2723 {
2724 cmdmod.filter_force = TRUE;
2725 p = skipwhite(p + 1);
2726 if (*p == NUL || ends_excmd(*p))
2727 break;
2728 }
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002729 if (skip_only)
2730 p = skip_vimgrep_pat(p, NULL, NULL);
2731 else
2732 // NOTE: This puts a NUL after the pattern.
2733 p = skip_vimgrep_pat(p, &reg_pat, NULL);
Bram Moolenaareffed932018-08-14 13:38:17 +02002734 if (p == NULL || *p == NUL)
2735 break;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002736 if (!skip_only)
2737 {
2738 cmdmod.filter_regmatch.regprog =
Bram Moolenaareffed932018-08-14 13:38:17 +02002739 vim_regcomp(reg_pat, RE_MAGIC);
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002740 if (cmdmod.filter_regmatch.regprog == NULL)
2741 break;
2742 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002743 eap->cmd = p;
2744 continue;
2745 }
2746
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002747 // ":hide" and ":hide | cmd" are not modifiers
Bram Moolenaareffed932018-08-14 13:38:17 +02002748 case 'h': if (p != eap->cmd || !checkforcmd(&p, "hide", 3)
2749 || *p == NUL || ends_excmd(*p))
2750 break;
2751 eap->cmd = p;
2752 cmdmod.hide = TRUE;
2753 continue;
2754
2755 case 'l': if (checkforcmd(&eap->cmd, "lockmarks", 3))
2756 {
2757 cmdmod.lockmarks = TRUE;
2758 continue;
2759 }
2760
2761 if (!checkforcmd(&eap->cmd, "leftabove", 5))
2762 break;
2763 cmdmod.split |= WSP_ABOVE;
2764 continue;
2765
2766 case 'n': if (checkforcmd(&eap->cmd, "noautocmd", 3))
2767 {
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002768 if (cmdmod.save_ei == NULL && !skip_only)
Bram Moolenaareffed932018-08-14 13:38:17 +02002769 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002770 // Set 'eventignore' to "all". Restore the
2771 // existing option value later.
Bram Moolenaareffed932018-08-14 13:38:17 +02002772 cmdmod.save_ei = vim_strsave(p_ei);
2773 set_string_option_direct((char_u *)"ei", -1,
2774 (char_u *)"all", OPT_FREE, SID_NONE);
2775 }
2776 continue;
2777 }
2778 if (!checkforcmd(&eap->cmd, "noswapfile", 3))
2779 break;
2780 cmdmod.noswapfile = TRUE;
2781 continue;
2782
2783 case 'r': if (!checkforcmd(&eap->cmd, "rightbelow", 6))
2784 break;
2785 cmdmod.split |= WSP_BELOW;
2786 continue;
2787
2788 case 's': if (checkforcmd(&eap->cmd, "sandbox", 3))
2789 {
2790#ifdef HAVE_SANDBOX
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002791 if (!skip_only)
2792 {
2793 if (!eap->did_sandbox)
2794 ++sandbox;
2795 eap->did_sandbox = TRUE;
2796 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002797#endif
2798 continue;
2799 }
2800 if (!checkforcmd(&eap->cmd, "silent", 3))
2801 break;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002802 if (!skip_only)
2803 {
2804 if (eap->save_msg_silent == -1)
2805 eap->save_msg_silent = msg_silent;
2806 ++msg_silent;
2807 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002808 if (*eap->cmd == '!' && !VIM_ISWHITE(eap->cmd[-1]))
2809 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002810 // ":silent!", but not "silent !cmd"
Bram Moolenaareffed932018-08-14 13:38:17 +02002811 eap->cmd = skipwhite(eap->cmd + 1);
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002812 if (!skip_only)
2813 {
2814 ++emsg_silent;
2815 ++eap->did_esilent;
2816 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002817 }
2818 continue;
2819
2820 case 't': if (checkforcmd(&p, "tab", 3))
2821 {
Bram Moolenaar548e5982018-12-26 21:45:00 +01002822 if (!skip_only)
Bram Moolenaareffed932018-08-14 13:38:17 +02002823 {
Bram Moolenaar548e5982018-12-26 21:45:00 +01002824 long tabnr = get_address(eap, &eap->cmd,
2825 ADDR_TABS, eap->skip,
2826 skip_only, FALSE, 1);
2827 if (tabnr == MAXLNUM)
2828 cmdmod.tab = tabpage_index(curtab) + 1;
2829 else
Bram Moolenaareffed932018-08-14 13:38:17 +02002830 {
Bram Moolenaar548e5982018-12-26 21:45:00 +01002831 if (tabnr < 0 || tabnr > LAST_TAB_NR)
2832 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002833 *errormsg = _(e_invrange);
Bram Moolenaar548e5982018-12-26 21:45:00 +01002834 return FAIL;
2835 }
2836 cmdmod.tab = tabnr + 1;
Bram Moolenaareffed932018-08-14 13:38:17 +02002837 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002838 }
2839 eap->cmd = p;
2840 continue;
2841 }
2842 if (!checkforcmd(&eap->cmd, "topleft", 2))
2843 break;
2844 cmdmod.split |= WSP_TOP;
2845 continue;
2846
2847 case 'u': if (!checkforcmd(&eap->cmd, "unsilent", 3))
2848 break;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002849 if (!skip_only)
2850 {
2851 if (eap->save_msg_silent == -1)
2852 eap->save_msg_silent = msg_silent;
2853 msg_silent = 0;
2854 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002855 continue;
2856
2857 case 'v': if (checkforcmd(&eap->cmd, "vertical", 4))
2858 {
2859 cmdmod.split |= WSP_VERT;
2860 continue;
2861 }
2862 if (!checkforcmd(&p, "verbose", 4))
2863 break;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002864 if (!skip_only)
2865 {
2866 if (eap->verbose_save < 0)
2867 eap->verbose_save = p_verbose;
2868 if (vim_isdigit(*eap->cmd))
2869 p_verbose = atoi((char *)eap->cmd);
2870 else
2871 p_verbose = 1;
2872 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002873 eap->cmd = p;
2874 continue;
2875 }
2876 break;
2877 }
2878
2879 return OK;
2880}
2881
2882/*
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002883 * Free contents of "cmdmod".
2884 */
2885 static void
2886free_cmdmod(void)
2887{
2888 if (cmdmod.save_ei != NULL)
2889 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002890 // Restore 'eventignore' to the value before ":noautocmd".
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002891 set_string_option_direct((char_u *)"ei", -1, cmdmod.save_ei,
2892 OPT_FREE, SID_NONE);
2893 free_string_option(cmdmod.save_ei);
2894 }
2895
2896 if (cmdmod.filter_regmatch.regprog != NULL)
2897 vim_regfree(cmdmod.filter_regmatch.regprog);
2898}
2899
2900/*
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002901 * Parse the address range, if any, in "eap".
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02002902 * May set the last search pattern, unless "silent" is TRUE.
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002903 * Return FAIL and set "errormsg" or return OK.
2904 */
2905 int
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002906parse_cmd_address(exarg_T *eap, char **errormsg, int silent)
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002907{
2908 int address_count = 1;
2909 linenr_T lnum;
2910
2911 // Repeat for all ',' or ';' separated addresses.
2912 for (;;)
2913 {
2914 eap->line1 = eap->line2;
2915 switch (eap->addr_type)
2916 {
2917 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02002918 case ADDR_OTHER:
Bram Moolenaarb5383b12020-05-18 19:46:48 +02002919 // Default is the cursor line number. Avoid using an invalid
2920 // line number though.
2921 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
2922 eap->line2 = curbuf->b_ml.ml_line_count;
2923 else
2924 eap->line2 = curwin->w_cursor.lnum;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002925 break;
2926 case ADDR_WINDOWS:
2927 eap->line2 = CURRENT_WIN_NR;
2928 break;
2929 case ADDR_ARGUMENTS:
2930 eap->line2 = curwin->w_arg_idx + 1;
2931 if (eap->line2 > ARGCOUNT)
2932 eap->line2 = ARGCOUNT;
2933 break;
2934 case ADDR_LOADED_BUFFERS:
2935 case ADDR_BUFFERS:
2936 eap->line2 = curbuf->b_fnum;
2937 break;
2938 case ADDR_TABS:
2939 eap->line2 = CURRENT_TAB_NR;
2940 break;
2941 case ADDR_TABS_RELATIVE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02002942 case ADDR_UNSIGNED:
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002943 eap->line2 = 1;
2944 break;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002945 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02002946#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02002947 eap->line2 = qf_get_cur_idx(eap);
2948#endif
2949 break;
2950 case ADDR_QUICKFIX_VALID:
2951#ifdef FEAT_QUICKFIX
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002952 eap->line2 = qf_get_cur_valid_idx(eap);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002953#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02002954 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02002955 case ADDR_NONE:
2956 // Will give an error later if a range is found.
2957 break;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002958 }
2959 eap->cmd = skipwhite(eap->cmd);
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02002960 lnum = get_address(eap, &eap->cmd, eap->addr_type, eap->skip, silent,
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002961 eap->addr_count == 0, address_count++);
2962 if (eap->cmd == NULL) // error detected
2963 return FAIL;
2964 if (lnum == MAXLNUM)
2965 {
2966 if (*eap->cmd == '%') // '%' - all lines
2967 {
2968 ++eap->cmd;
2969 switch (eap->addr_type)
2970 {
2971 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02002972 case ADDR_OTHER:
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002973 eap->line1 = 1;
2974 eap->line2 = curbuf->b_ml.ml_line_count;
2975 break;
2976 case ADDR_LOADED_BUFFERS:
2977 {
2978 buf_T *buf = firstbuf;
2979
2980 while (buf->b_next != NULL
2981 && buf->b_ml.ml_mfp == NULL)
2982 buf = buf->b_next;
2983 eap->line1 = buf->b_fnum;
2984 buf = lastbuf;
2985 while (buf->b_prev != NULL
2986 && buf->b_ml.ml_mfp == NULL)
2987 buf = buf->b_prev;
2988 eap->line2 = buf->b_fnum;
2989 break;
2990 }
2991 case ADDR_BUFFERS:
2992 eap->line1 = firstbuf->b_fnum;
2993 eap->line2 = lastbuf->b_fnum;
2994 break;
2995 case ADDR_WINDOWS:
2996 case ADDR_TABS:
2997 if (IS_USER_CMDIDX(eap->cmdidx))
2998 {
2999 eap->line1 = 1;
3000 eap->line2 = eap->addr_type == ADDR_WINDOWS
3001 ? LAST_WIN_NR : LAST_TAB_NR;
3002 }
3003 else
3004 {
3005 // there is no Vim command which uses '%' and
3006 // ADDR_WINDOWS or ADDR_TABS
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003007 *errormsg = _(e_invrange);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003008 return FAIL;
3009 }
3010 break;
3011 case ADDR_TABS_RELATIVE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02003012 case ADDR_UNSIGNED:
3013 case ADDR_QUICKFIX:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003014 *errormsg = _(e_invrange);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003015 return FAIL;
3016 case ADDR_ARGUMENTS:
3017 if (ARGCOUNT == 0)
3018 eap->line1 = eap->line2 = 0;
3019 else
3020 {
3021 eap->line1 = 1;
3022 eap->line2 = ARGCOUNT;
3023 }
3024 break;
Bram Moolenaar25190db2019-05-04 15:05:28 +02003025 case ADDR_QUICKFIX_VALID:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003026#ifdef FEAT_QUICKFIX
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003027 eap->line1 = 1;
Bram Moolenaar25190db2019-05-04 15:05:28 +02003028 eap->line2 = qf_get_valid_size(eap);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003029 if (eap->line2 == 0)
3030 eap->line2 = 1;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003031#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003032 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02003033 case ADDR_NONE:
3034 // Will give an error later if a range is found.
3035 break;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003036 }
3037 ++eap->addr_count;
3038 }
3039 else if (*eap->cmd == '*' && vim_strchr(p_cpo, CPO_STAR) == NULL)
3040 {
3041 pos_T *fp;
3042
3043 // '*' - visual area
3044 if (eap->addr_type != ADDR_LINES)
3045 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003046 *errormsg = _(e_invrange);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003047 return FAIL;
3048 }
3049
3050 ++eap->cmd;
3051 if (!eap->skip)
3052 {
3053 fp = getmark('<', FALSE);
3054 if (check_mark(fp) == FAIL)
3055 return FAIL;
3056 eap->line1 = fp->lnum;
3057 fp = getmark('>', FALSE);
3058 if (check_mark(fp) == FAIL)
3059 return FAIL;
3060 eap->line2 = fp->lnum;
3061 ++eap->addr_count;
3062 }
3063 }
3064 }
3065 else
3066 eap->line2 = lnum;
3067 eap->addr_count++;
3068
3069 if (*eap->cmd == ';')
3070 {
3071 if (!eap->skip)
3072 {
3073 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaar0e717042020-04-27 19:29:01 +02003074 // Don't leave the cursor on an illegal line or column, but do
3075 // accept zero as address, so 0;/PATTERN/ works correctly.
3076 if (eap->line2 > 0)
3077 check_cursor();
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003078 }
3079 }
3080 else if (*eap->cmd != ',')
3081 break;
3082 ++eap->cmd;
3083 }
3084
3085 // One address given: set start and end lines.
3086 if (eap->addr_count == 1)
3087 {
3088 eap->line1 = eap->line2;
3089 // ... but only implicit: really no address given
3090 if (lnum == MAXLNUM)
3091 eap->addr_count = 0;
3092 }
3093 return OK;
3094}
3095
3096/*
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00003097 * Check for an Ex command with optional tail.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003098 * If there is a match advance "pp" to the argument and return TRUE.
3099 */
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00003100 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003101checkforcmd(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003102 char_u **pp, // start of command
3103 char *cmd, // name of command
3104 int len) // required length
Bram Moolenaar071d4272004-06-13 20:20:40 +00003105{
3106 int i;
3107
3108 for (i = 0; cmd[i] != NUL; ++i)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003109 if (((char_u *)cmd)[i] != (*pp)[i])
Bram Moolenaar071d4272004-06-13 20:20:40 +00003110 break;
3111 if (i >= len && !isalpha((*pp)[i]))
3112 {
3113 *pp = skipwhite(*pp + i);
3114 return TRUE;
3115 }
3116 return FALSE;
3117}
3118
3119/*
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003120 * Append "cmd" to the error message in IObuff.
3121 * Takes care of limiting the length and handling 0xa0, which would be
3122 * invisible otherwise.
3123 */
3124 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003125append_command(char_u *cmd)
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003126{
3127 char_u *s = cmd;
3128 char_u *d;
3129
3130 STRCAT(IObuff, ": ");
3131 d = IObuff + STRLEN(IObuff);
3132 while (*s != NUL && d - IObuff < IOSIZE - 7)
3133 {
Bram Moolenaar13505972019-01-24 15:04:48 +01003134 if (enc_utf8 ? (s[0] == 0xc2 && s[1] == 0xa0) : *s == 0xa0)
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003135 {
Bram Moolenaar13505972019-01-24 15:04:48 +01003136 s += enc_utf8 ? 2 : 1;
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003137 STRCPY(d, "<a0>");
3138 d += 4;
3139 }
3140 else
3141 MB_COPY_CHAR(s, d);
3142 }
3143 *d = NUL;
3144}
3145
3146/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003147 * Find an Ex command by its name, either built-in or user.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003148 * Start of the name can be found at eap->cmd.
Bram Moolenaar25190db2019-05-04 15:05:28 +02003149 * Sets eap->cmdidx and returns a pointer to char after the command name.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003150 * "full" is set to TRUE if the whole command name matched.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003151 *
3152 * If "lookup" is not NULL recognize expression without "eval" or "call" and
3153 * assignment without "let". Sets eap->cmdidx to the command while returning
3154 * "eap->cmd".
3155 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00003156 * Returns NULL for an ambiguous user command.
3157 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003158 char_u *
3159find_ex_command(
3160 exarg_T *eap,
Bram Moolenaara494f562020-04-18 17:45:38 +02003161 int *full UNUSED,
Bram Moolenaarb84a3812020-05-01 15:44:29 +02003162 void *(*lookup)(char_u *, size_t, cctx_T *) UNUSED,
Bram Moolenaara494f562020-04-18 17:45:38 +02003163 cctx_T *cctx UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003164{
3165 int len;
3166 char_u *p;
Bram Moolenaardf177f62005-02-22 08:39:57 +00003167 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003168
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003169#ifdef FEAT_EVAL
3170 /*
3171 * Recognize a Vim9 script function/method call and assignment:
3172 * "lvar = value", "lvar(arg)", "[1, 2 3]->Func()"
3173 */
Bram Moolenaar0c6ceaf2020-02-22 18:36:32 +01003174 p = eap->cmd;
3175 if (lookup != NULL && (*p == '('
3176 || ((p = to_name_const_end(eap->cmd)) > eap->cmd && *p != NUL)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003177 {
3178 int oplen;
3179 int heredoc;
3180
3181 // "funcname(" is always a function call.
3182 // "varname[]" is an expression.
3183 // "g:varname" is an expression.
3184 // "varname->expr" is an expression.
Bram Moolenaar0c6ceaf2020-02-22 18:36:32 +01003185 // "(..." is an expression.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003186 if (*p == '('
3187 || *p == '['
3188 || p[1] == ':'
3189 || (*p == '-' && p[1] == '>'))
3190 {
3191 eap->cmdidx = CMD_eval;
3192 return eap->cmd;
3193 }
3194
3195 oplen = assignment_len(skipwhite(p), &heredoc);
3196 if (oplen > 0)
3197 {
3198 // Recognize an assignment if we recognize the variable name:
3199 // "g:var = expr"
3200 // "var = expr" where "var" is a local var name.
3201 if (((p - eap->cmd) > 2 && eap->cmd[1] == ':')
Bram Moolenaarb84a3812020-05-01 15:44:29 +02003202 || lookup(eap->cmd, p - eap->cmd, cctx) != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003203 {
3204 eap->cmdidx = CMD_let;
3205 return eap->cmd;
3206 }
3207 }
3208 }
3209#endif
3210
Bram Moolenaar071d4272004-06-13 20:20:40 +00003211 /*
3212 * Isolate the command and search for it in the command table.
Bram Moolenaar81870892007-11-11 18:17:28 +00003213 * Exceptions:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003214 * - the 'k' command can directly be followed by any character.
3215 * - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003216 * but :sre[wind] is another command, as are :scr[iptnames],
Bram Moolenaar071d4272004-06-13 20:20:40 +00003217 * :scs[cope], :sim[alt], :sig[ns] and :sil[ent].
Bram Moolenaardf177f62005-02-22 08:39:57 +00003218 * - the "d" command can directly be followed by 'l' or 'p' flag.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003219 */
3220 p = eap->cmd;
3221 if (*p == 'k')
3222 {
3223 eap->cmdidx = CMD_k;
3224 ++p;
3225 }
3226 else if (p[0] == 's'
Bram Moolenaar204b93f2015-08-04 22:02:51 +02003227 && ((p[1] == 'c' && (p[2] == NUL || (p[2] != 's' && p[2] != 'r'
3228 && (p[3] == NUL || (p[3] != 'i' && p[4] != 'p')))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003229 || p[1] == 'g'
3230 || (p[1] == 'i' && p[2] != 'm' && p[2] != 'l' && p[2] != 'g')
3231 || p[1] == 'I'
3232 || (p[1] == 'r' && p[2] != 'e')))
3233 {
3234 eap->cmdidx = CMD_substitute;
3235 ++p;
3236 }
3237 else
3238 {
3239 while (ASCII_ISALPHA(*p))
3240 ++p;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003241 // for python 3.x support ":py3", ":python3", ":py3file", etc.
Bram Moolenaar55d5c032010-07-17 23:52:29 +02003242 if (eap->cmd[0] == 'p' && eap->cmd[1] == 'y')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003243 {
Bram Moolenaarb6590522010-07-21 16:00:43 +02003244 while (ASCII_ISALNUM(*p))
3245 ++p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003246 }
3247 else if (*p == '9' && STRNCMP("vim9", eap->cmd, 4) == 0)
3248 {
3249 // include "9" for "vim9script"
3250 ++p;
3251 while (ASCII_ISALPHA(*p))
3252 ++p;
3253 }
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02003254
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003255 // check for non-alpha command
Bram Moolenaar071d4272004-06-13 20:20:40 +00003256 if (p == eap->cmd && vim_strchr((char_u *)"@*!=><&~#", *p) != NULL)
3257 ++p;
3258 len = (int)(p - eap->cmd);
Bram Moolenaardf177f62005-02-22 08:39:57 +00003259 if (*eap->cmd == 'd' && (p[-1] == 'l' || p[-1] == 'p'))
3260 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003261 // Check for ":dl", ":dell", etc. to ":deletel": that's
3262 // :delete with the 'l' flag. Same for 'p'.
Bram Moolenaardf177f62005-02-22 08:39:57 +00003263 for (i = 0; i < len; ++i)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003264 if (eap->cmd[i] != ((char_u *)"delete")[i])
Bram Moolenaardf177f62005-02-22 08:39:57 +00003265 break;
3266 if (i == len - 1)
3267 {
3268 --len;
3269 if (p[-1] == 'l')
3270 eap->flags |= EXFLAG_LIST;
3271 else
3272 eap->flags |= EXFLAG_PRINT;
3273 }
3274 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003275
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003276 if (ASCII_ISLOWER(eap->cmd[0]))
3277 {
Bram Moolenaar6c0c1e82017-03-25 15:07:43 +01003278 int c1 = eap->cmd[0];
Bram Moolenaar94f82cb2019-07-24 22:30:27 +02003279 int c2 = len == 1 ? NUL : eap->cmd[1];
Bram Moolenaar6c0c1e82017-03-25 15:07:43 +01003280
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003281 if (command_count != (int)CMD_SIZE)
3282 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003283 iemsg(_("E943: Command table needs to be updated, run 'make cmdidxs'"));
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003284 getout(1);
3285 }
3286
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003287 // Use a precomputed index for fast look-up in cmdnames[]
3288 // taking into account the first 2 letters of eap->cmd.
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003289 eap->cmdidx = cmdidxs1[CharOrdLow(c1)];
3290 if (ASCII_ISLOWER(c2))
3291 eap->cmdidx += cmdidxs2[CharOrdLow(c1)][CharOrdLow(c2)];
3292 }
Bram Moolenaara494f562020-04-18 17:45:38 +02003293 else if (ASCII_ISUPPER(eap->cmd[0]))
3294 eap->cmdidx = CMD_Next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003295 else
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003296 eap->cmdidx = CMD_bang;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003297
3298 for ( ; (int)eap->cmdidx < (int)CMD_SIZE;
3299 eap->cmdidx = (cmdidx_T)((int)eap->cmdidx + 1))
3300 if (STRNCMP(cmdnames[(int)eap->cmdidx].cmd_name, (char *)eap->cmd,
3301 (size_t)len) == 0)
3302 {
3303#ifdef FEAT_EVAL
3304 if (full != NULL
3305 && cmdnames[(int)eap->cmdidx].cmd_name[len] == NUL)
3306 *full = TRUE;
3307#endif
3308 break;
3309 }
3310
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003311 // Look for a user defined command as a last resort. Let ":Print" be
3312 // overruled by a user defined command.
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01003313 if ((eap->cmdidx == CMD_SIZE || eap->cmdidx == CMD_Print)
3314 && *eap->cmd >= 'A' && *eap->cmd <= 'Z')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003315 {
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003316 // User defined commands may contain digits.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003317 while (ASCII_ISALNUM(*p))
3318 ++p;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003319 p = find_ucmd(eap, p, full, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003320 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003321 if (p == eap->cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003322 eap->cmdidx = CMD_SIZE;
3323 }
3324
3325 return p;
3326}
3327
3328#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003329static struct cmdmod
3330{
3331 char *name;
3332 int minlen;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003333 int has_count; // :123verbose :3tab
Bram Moolenaared53fb92007-11-24 20:50:24 +00003334} cmdmods[] = {
3335 {"aboveleft", 3, FALSE},
3336 {"belowright", 3, FALSE},
3337 {"botright", 2, FALSE},
3338 {"browse", 3, FALSE},
3339 {"confirm", 4, FALSE},
Bram Moolenaar7b668e82016-08-23 23:51:21 +02003340 {"filter", 4, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003341 {"hide", 3, FALSE},
3342 {"keepalt", 5, FALSE},
3343 {"keepjumps", 5, FALSE},
3344 {"keepmarks", 3, FALSE},
Bram Moolenaara939e432013-11-09 05:30:26 +01003345 {"keeppatterns", 5, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003346 {"leftabove", 5, FALSE},
3347 {"lockmarks", 3, FALSE},
Bram Moolenaarca9f9582008-09-18 10:44:28 +00003348 {"noautocmd", 3, FALSE},
Bram Moolenaar5803ae62014-03-23 16:04:02 +01003349 {"noswapfile", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003350 {"rightbelow", 6, FALSE},
3351 {"sandbox", 3, FALSE},
3352 {"silent", 3, FALSE},
3353 {"tab", 3, TRUE},
3354 {"topleft", 2, FALSE},
Bram Moolenaar8e258a42009-07-09 13:55:43 +00003355 {"unsilent", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003356 {"verbose", 4, TRUE},
3357 {"vertical", 4, FALSE},
3358};
3359
3360/*
3361 * Return length of a command modifier (including optional count).
3362 * Return zero when it's not a modifier.
3363 */
3364 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003365modifier_len(char_u *cmd)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003366{
3367 int i, j;
3368 char_u *p = cmd;
3369
3370 if (VIM_ISDIGIT(*cmd))
3371 p = skipwhite(skipdigits(cmd));
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00003372 for (i = 0; i < (int)(sizeof(cmdmods) / sizeof(struct cmdmod)); ++i)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003373 {
3374 for (j = 0; p[j] != NUL; ++j)
3375 if (p[j] != cmdmods[i].name[j])
3376 break;
Bram Moolenaar2d473ab2013-06-12 17:12:24 +02003377 if (!ASCII_ISALPHA(p[j]) && j >= cmdmods[i].minlen
Bram Moolenaared53fb92007-11-24 20:50:24 +00003378 && (p == cmd || cmdmods[i].has_count))
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00003379 return j + (int)(p - cmd);
Bram Moolenaared53fb92007-11-24 20:50:24 +00003380 }
3381 return 0;
3382}
3383
Bram Moolenaar071d4272004-06-13 20:20:40 +00003384/*
3385 * Return > 0 if an Ex command "name" exists.
3386 * Return 2 if there is an exact match.
3387 * Return 3 if there is an ambiguous match.
3388 */
3389 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003390cmd_exists(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003391{
3392 exarg_T ea;
3393 int full = FALSE;
3394 int i;
3395 int j;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003396 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003397
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003398 // Check command modifiers.
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00003399 for (i = 0; i < (int)(sizeof(cmdmods) / sizeof(struct cmdmod)); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003400 {
3401 for (j = 0; name[j] != NUL; ++j)
3402 if (name[j] != cmdmods[i].name[j])
3403 break;
3404 if (name[j] == NUL && j >= cmdmods[i].minlen)
3405 return (cmdmods[i].name[j] == NUL ? 2 : 1);
3406 }
3407
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003408 // Check built-in commands and user defined commands.
3409 // For ":2match" and ":3match" we need to skip the number.
Bram Moolenaara9587612006-05-04 21:47:50 +00003410 ea.cmd = (*name == '2' || *name == '3') ? name + 1 : name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003411 ea.cmdidx = (cmdidx_T)0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003412 p = find_ex_command(&ea, &full, NULL, NULL);
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003413 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003414 return 3;
Bram Moolenaara9587612006-05-04 21:47:50 +00003415 if (vim_isdigit(*name) && ea.cmdidx != CMD_match)
3416 return 0;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003417 if (*skipwhite(p) != NUL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003418 return 0; // trailing garbage
Bram Moolenaar071d4272004-06-13 20:20:40 +00003419 return (ea.cmdidx == CMD_SIZE ? 0 : (full ? 2 : 1));
3420}
3421#endif
3422
Bram Moolenaard0190392019-08-23 21:17:35 +02003423 cmdidx_T
3424excmd_get_cmdidx(char_u *cmd, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003425{
Bram Moolenaard0190392019-08-23 21:17:35 +02003426 cmdidx_T idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003427
Bram Moolenaard0190392019-08-23 21:17:35 +02003428 for (idx = (cmdidx_T)0; (int)idx < (int)CMD_SIZE;
3429 idx = (cmdidx_T)((int)idx + 1))
3430 if (STRNCMP(cmdnames[(int)idx].cmd_name, cmd, (size_t)len) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003431 break;
3432
Bram Moolenaard0190392019-08-23 21:17:35 +02003433 return idx;
3434}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003435
Bram Moolenaard0190392019-08-23 21:17:35 +02003436 long
3437excmd_get_argt(cmdidx_T idx)
3438{
3439 return (long)cmdnames[(int)idx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003440}
3441
3442/*
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003443 * Skip a range specifier of the form: addr [,addr] [;addr] ..
Bram Moolenaar071d4272004-06-13 20:20:40 +00003444 *
3445 * Backslashed delimiters after / or ? will be skipped, and commands will
3446 * not be expanded between /'s and ?'s or after "'".
3447 *
Bram Moolenaardf177f62005-02-22 08:39:57 +00003448 * Also skip white space and ":" characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003449 * Returns the "cmd" pointer advanced to beyond the range.
3450 */
3451 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003452skip_range(
3453 char_u *cmd,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003454 int *ctx) // pointer to xp_context or NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003455{
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003456 unsigned delim;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003457
Bram Moolenaarcbf20fb2017-02-03 21:19:04 +01003458 while (vim_strchr((char_u *)" \t0123456789.$%'/?-+,;\\", *cmd) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003459 {
Bram Moolenaarcbf20fb2017-02-03 21:19:04 +01003460 if (*cmd == '\\')
3461 {
3462 if (cmd[1] == '?' || cmd[1] == '/' || cmd[1] == '&')
3463 ++cmd;
3464 else
3465 break;
3466 }
3467 else if (*cmd == '\'')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003468 {
3469 if (*++cmd == NUL && ctx != NULL)
3470 *ctx = EXPAND_NOTHING;
3471 }
3472 else if (*cmd == '/' || *cmd == '?')
3473 {
3474 delim = *cmd++;
3475 while (*cmd != NUL && *cmd != delim)
3476 if (*cmd++ == '\\' && *cmd != NUL)
3477 ++cmd;
3478 if (*cmd == NUL && ctx != NULL)
3479 *ctx = EXPAND_NOTHING;
3480 }
3481 if (*cmd != NUL)
3482 ++cmd;
3483 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00003484
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003485 // Skip ":" and white space.
Bram Moolenaardf177f62005-02-22 08:39:57 +00003486 while (*cmd == ':')
3487 cmd = skipwhite(cmd + 1);
3488
Bram Moolenaar071d4272004-06-13 20:20:40 +00003489 return cmd;
3490}
3491
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003492 static void
3493addr_error(cmd_addr_T addr_type)
3494{
3495 if (addr_type == ADDR_NONE)
3496 emsg(_(e_norange));
3497 else
3498 emsg(_(e_invrange));
3499}
3500
Bram Moolenaar071d4272004-06-13 20:20:40 +00003501/*
Bram Moolenaar198cb662018-09-06 21:44:17 +02003502 * Get a single EX address.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003503 *
3504 * Set ptr to the next character after the part that was interpreted.
3505 * Set ptr to NULL when an error is encountered.
Bram Moolenaar198cb662018-09-06 21:44:17 +02003506 * This may set the last used search pattern.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003507 *
3508 * Return MAXLNUM when no Ex address was found.
3509 */
3510 static linenr_T
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003511get_address(
3512 exarg_T *eap UNUSED,
3513 char_u **ptr,
Bram Moolenaar1b03a192019-12-08 17:08:29 +01003514 cmd_addr_T addr_type,
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02003515 int skip, // only skip the address, don't use it
3516 int silent, // no errors or side effects
3517 int to_other_file, // flag: may jump to other file
3518 int address_count UNUSED) // 1 for first address, >1 after comma
Bram Moolenaar071d4272004-06-13 20:20:40 +00003519{
3520 int c;
3521 int i;
3522 long n;
3523 char_u *cmd;
3524 pos_T pos;
3525 pos_T *fp;
3526 linenr_T lnum;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003527 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003528
3529 cmd = skipwhite(*ptr);
3530 lnum = MAXLNUM;
3531 do
3532 {
3533 switch (*cmd)
3534 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003535 case '.': // '.' - Cursor position
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003536 ++cmd;
3537 switch (addr_type)
3538 {
3539 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02003540 case ADDR_OTHER:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003541 lnum = curwin->w_cursor.lnum;
3542 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003543 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01003544 lnum = CURRENT_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003545 break;
3546 case ADDR_ARGUMENTS:
3547 lnum = curwin->w_arg_idx + 1;
3548 break;
3549 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003550 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003551 lnum = curbuf->b_fnum;
3552 break;
3553 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01003554 lnum = CURRENT_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003555 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02003556 case ADDR_NONE:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003557 case ADDR_TABS_RELATIVE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02003558 case ADDR_UNSIGNED:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003559 addr_error(addr_type);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01003560 cmd = NULL;
3561 goto error;
3562 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003563 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003564#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02003565 lnum = qf_get_cur_idx(eap);
3566#endif
3567 break;
3568 case ADDR_QUICKFIX_VALID:
3569#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003570 lnum = qf_get_cur_valid_idx(eap);
Bram Moolenaare906c502015-09-09 21:10:39 +02003571#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003572 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003573 }
3574 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003575
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003576 case '$': // '$' - last line
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003577 ++cmd;
3578 switch (addr_type)
3579 {
3580 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02003581 case ADDR_OTHER:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003582 lnum = curbuf->b_ml.ml_line_count;
3583 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003584 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01003585 lnum = LAST_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003586 break;
3587 case ADDR_ARGUMENTS:
3588 lnum = ARGCOUNT;
3589 break;
3590 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003591 buf = lastbuf;
3592 while (buf->b_ml.ml_mfp == NULL)
3593 {
3594 if (buf->b_prev == NULL)
3595 break;
3596 buf = buf->b_prev;
3597 }
3598 lnum = buf->b_fnum;
3599 break;
3600 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003601 lnum = lastbuf->b_fnum;
3602 break;
3603 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01003604 lnum = LAST_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003605 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02003606 case ADDR_NONE:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003607 case ADDR_TABS_RELATIVE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02003608 case ADDR_UNSIGNED:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003609 addr_error(addr_type);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01003610 cmd = NULL;
3611 goto error;
3612 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003613 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003614#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003615 lnum = qf_get_size(eap);
3616 if (lnum == 0)
3617 lnum = 1;
Bram Moolenaare906c502015-09-09 21:10:39 +02003618#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003619 break;
Bram Moolenaar25190db2019-05-04 15:05:28 +02003620 case ADDR_QUICKFIX_VALID:
3621#ifdef FEAT_QUICKFIX
3622 lnum = qf_get_valid_size(eap);
3623 if (lnum == 0)
3624 lnum = 1;
3625#endif
3626 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003627 }
3628 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003629
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003630 case '\'': // ''' - mark
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003631 if (*++cmd == NUL)
3632 {
3633 cmd = NULL;
3634 goto error;
3635 }
3636 if (addr_type != ADDR_LINES)
3637 {
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003638 addr_error(addr_type);
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01003639 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003640 goto error;
3641 }
3642 if (skip)
3643 ++cmd;
3644 else
3645 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003646 // Only accept a mark in another file when it is
3647 // used by itself: ":'M".
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003648 fp = getmark(*cmd, to_other_file && cmd[1] == NUL);
3649 ++cmd;
3650 if (fp == (pos_T *)-1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003651 // Jumped to another file.
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003652 lnum = curwin->w_cursor.lnum;
3653 else
3654 {
3655 if (check_mark(fp) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003656 {
3657 cmd = NULL;
3658 goto error;
3659 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003660 lnum = fp->lnum;
3661 }
3662 }
3663 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003664
3665 case '/':
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003666 case '?': // '/' or '?' - search
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003667 c = *cmd++;
3668 if (addr_type != ADDR_LINES)
3669 {
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003670 addr_error(addr_type);
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01003671 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003672 goto error;
3673 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003674 if (skip) // skip "/pat/"
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003675 {
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02003676 cmd = skip_regexp(cmd, c, (int)p_magic);
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003677 if (*cmd == c)
3678 ++cmd;
3679 }
3680 else
3681 {
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02003682 int flags;
3683
3684 pos = curwin->w_cursor; // save curwin->w_cursor
3685
3686 // When '/' or '?' follows another address, start from
3687 // there.
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003688 if (lnum != MAXLNUM)
3689 curwin->w_cursor.lnum = lnum;
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02003690
3691 // Start a forward search at the end of the line (unless
3692 // before the first line).
3693 // Start a backward search at the start of the line.
3694 // This makes sure we never match in the current
3695 // line, and can match anywhere in the
3696 // next/previous line.
Bram Moolenaar8ada6aa2017-12-19 21:23:21 +01003697 if (c == '/' && curwin->w_cursor.lnum > 0)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003698 curwin->w_cursor.col = MAXCOL;
3699 else
3700 curwin->w_cursor.col = 0;
3701 searchcmdlen = 0;
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02003702 flags = silent ? 0 : SEARCH_HIS | SEARCH_MSG;
Bram Moolenaarc036e872020-02-21 21:30:52 +01003703 if (!do_search(NULL, c, c, cmd, 1L, flags, NULL))
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003704 {
3705 curwin->w_cursor = pos;
3706 cmd = NULL;
3707 goto error;
3708 }
3709 lnum = curwin->w_cursor.lnum;
3710 curwin->w_cursor = pos;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003711 // adjust command string pointer
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003712 cmd += searchcmdlen;
3713 }
3714 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003715
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003716 case '\\': // "\?", "\/" or "\&", repeat search
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003717 ++cmd;
3718 if (addr_type != ADDR_LINES)
3719 {
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003720 addr_error(addr_type);
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01003721 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003722 goto error;
3723 }
3724 if (*cmd == '&')
3725 i = RE_SUBST;
3726 else if (*cmd == '?' || *cmd == '/')
3727 i = RE_SEARCH;
3728 else
3729 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003730 emsg(_(e_backslash));
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003731 cmd = NULL;
3732 goto error;
3733 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003734
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003735 if (!skip)
3736 {
3737 /*
3738 * When search follows another address, start from
3739 * there.
3740 */
3741 if (lnum != MAXLNUM)
3742 pos.lnum = lnum;
3743 else
3744 pos.lnum = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003745
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003746 /*
3747 * Start the search just like for the above
3748 * do_search().
3749 */
3750 if (*cmd != '?')
3751 pos.col = MAXCOL;
3752 else
3753 pos.col = 0;
Bram Moolenaarbd8539a2015-08-11 18:53:03 +02003754 pos.coladd = 0;
Bram Moolenaar5d24a222018-12-23 19:10:09 +01003755 if (searchit(curwin, curbuf, &pos, NULL,
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003756 *cmd == '?' ? BACKWARD : FORWARD,
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02003757 (char_u *)"", 1L, SEARCH_MSG, i, NULL) != FAIL)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003758 lnum = pos.lnum;
3759 else
3760 {
3761 cmd = NULL;
3762 goto error;
3763 }
3764 }
3765 ++cmd;
3766 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003767
3768 default:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003769 if (VIM_ISDIGIT(*cmd)) // absolute line number
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003770 lnum = getdigits(&cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003771 }
3772
3773 for (;;)
3774 {
3775 cmd = skipwhite(cmd);
3776 if (*cmd != '-' && *cmd != '+' && !VIM_ISDIGIT(*cmd))
3777 break;
3778
3779 if (lnum == MAXLNUM)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003780 {
3781 switch (addr_type)
3782 {
3783 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02003784 case ADDR_OTHER:
3785 // "+1" is same as ".+1"
Bram Moolenaarf240e182014-11-27 18:33:02 +01003786 lnum = curwin->w_cursor.lnum;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003787 break;
3788 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01003789 lnum = CURRENT_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003790 break;
3791 case ADDR_ARGUMENTS:
3792 lnum = curwin->w_arg_idx + 1;
3793 break;
3794 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003795 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003796 lnum = curbuf->b_fnum;
3797 break;
3798 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01003799 lnum = CURRENT_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003800 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01003801 case ADDR_TABS_RELATIVE:
3802 lnum = 1;
3803 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003804 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003805#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02003806 lnum = qf_get_cur_idx(eap);
3807#endif
3808 break;
3809 case ADDR_QUICKFIX_VALID:
3810#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003811 lnum = qf_get_cur_valid_idx(eap);
Bram Moolenaare906c502015-09-09 21:10:39 +02003812#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003813 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02003814 case ADDR_NONE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02003815 case ADDR_UNSIGNED:
3816 lnum = 0;
Bram Moolenaarb7316892019-05-01 18:08:42 +02003817 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003818 }
3819 }
3820
Bram Moolenaar071d4272004-06-13 20:20:40 +00003821 if (VIM_ISDIGIT(*cmd))
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003822 i = '+'; // "number" is same as "+number"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003823 else
3824 i = *cmd++;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003825 if (!VIM_ISDIGIT(*cmd)) // '+' is '+1', but '+0' is not '+1'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003826 n = 1;
3827 else
3828 n = getdigits(&cmd);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01003829
3830 if (addr_type == ADDR_TABS_RELATIVE)
3831 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003832 emsg(_(e_invrange));
Bram Moolenaar2f72c702017-01-29 14:48:10 +01003833 cmd = NULL;
3834 goto error;
3835 }
3836 else if (addr_type == ADDR_LOADED_BUFFERS
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003837 || addr_type == ADDR_BUFFERS)
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01003838 lnum = compute_buffer_local_count(
3839 addr_type, lnum, (i == '-') ? -1 * n : n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003840 else
Bram Moolenaarded27822017-01-02 14:27:34 +01003841 {
3842#ifdef FEAT_FOLDING
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003843 // Relative line addressing, need to adjust for folded lines
3844 // now, but only do it after the first address.
Bram Moolenaarded27822017-01-02 14:27:34 +01003845 if (addr_type == ADDR_LINES && (i == '-' || i == '+')
3846 && address_count >= 2)
3847 (void)hasFolding(lnum, NULL, &lnum);
3848#endif
3849 if (i == '-')
3850 lnum -= n;
3851 else
3852 lnum += n;
3853 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003854 }
3855 } while (*cmd == '/' || *cmd == '?');
3856
3857error:
3858 *ptr = cmd;
3859 return lnum;
3860}
3861
3862/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00003863 * Get flags from an Ex command argument.
3864 */
3865 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003866get_flags(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00003867{
3868 while (vim_strchr((char_u *)"lp#", *eap->arg) != NULL)
3869 {
3870 if (*eap->arg == 'l')
3871 eap->flags |= EXFLAG_LIST;
3872 else if (*eap->arg == 'p')
3873 eap->flags |= EXFLAG_PRINT;
3874 else
3875 eap->flags |= EXFLAG_NR;
3876 eap->arg = skipwhite(eap->arg + 1);
3877 }
3878}
3879
3880/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003881 * Function called for command which is Not Implemented. NI!
3882 */
3883 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003884ex_ni(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003885{
3886 if (!eap->skip)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003887 eap->errmsg = N_("E319: Sorry, the command is not available in this version");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003888}
3889
Bram Moolenaar7bb75552007-07-16 18:39:49 +00003890#ifdef HAVE_EX_SCRIPT_NI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003891/*
3892 * Function called for script command which is Not Implemented. NI!
3893 * Skips over ":perl <<EOF" constructs.
3894 */
3895 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003896ex_script_ni(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003897{
3898 if (!eap->skip)
3899 ex_ni(eap);
3900 else
3901 vim_free(script_get(eap, eap->arg));
3902}
3903#endif
3904
3905/*
3906 * Check range in Ex command for validity.
3907 * Return NULL when valid, error message when invalid.
3908 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003909 static char *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003910invalid_range(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003911{
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003912 buf_T *buf;
Bram Moolenaar25190db2019-05-04 15:05:28 +02003913
Bram Moolenaar071d4272004-06-13 20:20:40 +00003914 if ( eap->line1 < 0
3915 || eap->line2 < 0
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003916 || eap->line1 > eap->line2)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003917 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003918
Bram Moolenaar8071cb22019-07-12 17:58:01 +02003919 if (eap->argt & EX_RANGE)
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003920 {
Bram Moolenaarb7316892019-05-01 18:08:42 +02003921 switch (eap->addr_type)
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003922 {
3923 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02003924 if (eap->line2 > curbuf->b_ml.ml_line_count
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003925#ifdef FEAT_DIFF
3926 + (eap->cmdidx == CMD_diffget)
3927#endif
3928 )
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003929 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003930 break;
3931 case ADDR_ARGUMENTS:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003932 // add 1 if ARGCOUNT is 0
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01003933 if (eap->line2 > ARGCOUNT + (!ARGCOUNT))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003934 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003935 break;
3936 case ADDR_BUFFERS:
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02003937 // Only a boundary check, not whether the buffers actually
3938 // exist.
3939 if (eap->line1 < 1 || eap->line2 > get_highest_fnum())
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003940 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003941 break;
3942 case ADDR_LOADED_BUFFERS:
3943 buf = firstbuf;
3944 while (buf->b_ml.ml_mfp == NULL)
3945 {
3946 if (buf->b_next == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003947 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003948 buf = buf->b_next;
3949 }
3950 if (eap->line1 < buf->b_fnum)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003951 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003952 buf = lastbuf;
3953 while (buf->b_ml.ml_mfp == NULL)
3954 {
3955 if (buf->b_prev == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003956 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003957 buf = buf->b_prev;
3958 }
3959 if (eap->line2 > buf->b_fnum)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003960 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003961 break;
3962 case ADDR_WINDOWS:
Bram Moolenaar8be63882015-01-14 11:25:05 +01003963 if (eap->line2 > LAST_WIN_NR)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003964 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003965 break;
3966 case ADDR_TABS:
3967 if (eap->line2 > LAST_TAB_NR)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003968 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003969 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01003970 case ADDR_TABS_RELATIVE:
Bram Moolenaarb7316892019-05-01 18:08:42 +02003971 case ADDR_OTHER:
3972 // Any range is OK.
Bram Moolenaar2f72c702017-01-29 14:48:10 +01003973 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003974 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003975#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02003976 // No error for value that is too big, will use the last entry.
3977 if (eap->line2 <= 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003978 return _(e_invrange);
Bram Moolenaare906c502015-09-09 21:10:39 +02003979#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003980 break;
Bram Moolenaar25190db2019-05-04 15:05:28 +02003981 case ADDR_QUICKFIX_VALID:
3982#ifdef FEAT_QUICKFIX
3983 if ((eap->line2 != 1 && eap->line2 > qf_get_valid_size(eap))
3984 || eap->line2 < 0)
3985 return _(e_invrange);
3986#endif
3987 break;
3988 case ADDR_UNSIGNED:
3989 if (eap->line2 < 0)
3990 return _(e_invrange);
3991 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02003992 case ADDR_NONE:
3993 // Will give an error elsewhere.
3994 break;
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003995 }
3996 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003997 return NULL;
3998}
3999
4000/*
4001 * Correct the range for zero line number, if required.
4002 */
4003 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004004correct_range(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004005{
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004006 if (!(eap->argt & EX_ZEROR)) // zero in range not allowed
Bram Moolenaar071d4272004-06-13 20:20:40 +00004007 {
4008 if (eap->line1 == 0)
4009 eap->line1 = 1;
4010 if (eap->line2 == 0)
4011 eap->line2 = 1;
4012 }
4013}
4014
Bram Moolenaar748bf032005-02-02 23:04:36 +00004015#ifdef FEAT_QUICKFIX
Bram Moolenaar748bf032005-02-02 23:04:36 +00004016/*
4017 * For a ":vimgrep" or ":vimgrepadd" command return a pointer past the
4018 * pattern. Otherwise return eap->arg.
4019 */
4020 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004021skip_grep_pat(exarg_T *eap)
Bram Moolenaar748bf032005-02-02 23:04:36 +00004022{
4023 char_u *p = eap->arg;
4024
Bram Moolenaara37420f2006-02-04 22:37:47 +00004025 if (*p != NUL && (eap->cmdidx == CMD_vimgrep || eap->cmdidx == CMD_lvimgrep
4026 || eap->cmdidx == CMD_vimgrepadd
4027 || eap->cmdidx == CMD_lvimgrepadd
4028 || grep_internal(eap->cmdidx)))
Bram Moolenaar748bf032005-02-02 23:04:36 +00004029 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00004030 p = skip_vimgrep_pat(p, NULL, NULL);
Bram Moolenaar748bf032005-02-02 23:04:36 +00004031 if (p == NULL)
4032 p = eap->arg;
Bram Moolenaar748bf032005-02-02 23:04:36 +00004033 }
4034 return p;
4035}
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004036
4037/*
4038 * For the ":make" and ":grep" commands insert the 'makeprg'/'grepprg' option
4039 * in the command line, so that things like % get expanded.
4040 */
4041 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004042replace_makeprg(exarg_T *eap, char_u *p, char_u **cmdlinep)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004043{
4044 char_u *new_cmdline;
4045 char_u *program;
4046 char_u *pos;
4047 char_u *ptr;
4048 int len;
4049 int i;
4050
4051 /*
4052 * Don't do it when ":vimgrep" is used for ":grep".
4053 */
Bram Moolenaara37420f2006-02-04 22:37:47 +00004054 if ((eap->cmdidx == CMD_make || eap->cmdidx == CMD_lmake
4055 || eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4056 || eap->cmdidx == CMD_grepadd
4057 || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004058 && !grep_internal(eap->cmdidx))
4059 {
Bram Moolenaara37420f2006-02-04 22:37:47 +00004060 if (eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4061 || eap->cmdidx == CMD_grepadd || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004062 {
4063 if (*curbuf->b_p_gp == NUL)
4064 program = p_gp;
4065 else
4066 program = curbuf->b_p_gp;
4067 }
4068 else
4069 {
4070 if (*curbuf->b_p_mp == NUL)
4071 program = p_mp;
4072 else
4073 program = curbuf->b_p_mp;
4074 }
4075
4076 p = skipwhite(p);
4077
4078 if ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4079 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004080 // replace $* by given arguments
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004081 i = 1;
4082 while ((pos = (char_u *)strstr((char *)pos + 2, "$*")) != NULL)
4083 ++i;
4084 len = (int)STRLEN(p);
Bram Moolenaar51e14382019-05-25 20:21:28 +02004085 new_cmdline = alloc(STRLEN(program) + i * (len - 2) + 1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004086 if (new_cmdline == NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004087 return NULL; // out of memory
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004088 ptr = new_cmdline;
4089 while ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4090 {
4091 i = (int)(pos - program);
4092 STRNCPY(ptr, program, i);
4093 STRCPY(ptr += i, p);
4094 ptr += len;
4095 program = pos + 2;
4096 }
4097 STRCPY(ptr, program);
4098 }
4099 else
4100 {
Bram Moolenaar51e14382019-05-25 20:21:28 +02004101 new_cmdline = alloc(STRLEN(program) + STRLEN(p) + 2);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004102 if (new_cmdline == NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004103 return NULL; // out of memory
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004104 STRCPY(new_cmdline, program);
4105 STRCAT(new_cmdline, " ");
4106 STRCAT(new_cmdline, p);
4107 }
4108 msg_make(p);
4109
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004110 // 'eap->cmd' is not set here, because it is not used at CMD_make
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004111 vim_free(*cmdlinep);
4112 *cmdlinep = new_cmdline;
4113 p = new_cmdline;
4114 }
4115 return p;
4116}
Bram Moolenaar748bf032005-02-02 23:04:36 +00004117#endif
4118
Bram Moolenaar071d4272004-06-13 20:20:40 +00004119/*
4120 * Expand file name in Ex command argument.
Bram Moolenaar0abb4272019-06-15 16:06:00 +02004121 * When an error is detected, "errormsgp" is set to a non-NULL pointer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004122 * Return FAIL for failure, OK otherwise.
4123 */
4124 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004125expand_filename(
4126 exarg_T *eap,
4127 char_u **cmdlinep,
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004128 char **errormsgp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004129{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004130 int has_wildcards; // need to expand wildcards
Bram Moolenaar071d4272004-06-13 20:20:40 +00004131 char_u *repl;
4132 int srclen;
4133 char_u *p;
4134 int n;
Bram Moolenaar63b92542007-03-27 14:57:09 +00004135 int escaped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004136
Bram Moolenaar748bf032005-02-02 23:04:36 +00004137#ifdef FEAT_QUICKFIX
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004138 // Skip a regexp pattern for ":vimgrep[add] pat file..."
Bram Moolenaar748bf032005-02-02 23:04:36 +00004139 p = skip_grep_pat(eap);
4140#else
4141 p = eap->arg;
4142#endif
4143
Bram Moolenaar071d4272004-06-13 20:20:40 +00004144 /*
4145 * Decide to expand wildcards *before* replacing '%', '#', etc. If
4146 * the file name contains a wildcard it should not cause expanding.
4147 * (it will be expanded anyway if there is a wildcard before replacing).
4148 */
Bram Moolenaar748bf032005-02-02 23:04:36 +00004149 has_wildcards = mch_has_wildcard(p);
4150 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004151 {
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004152#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004153 // Skip over `=expr`, wildcards in it are not expanded.
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004154 if (p[0] == '`' && p[1] == '=')
4155 {
4156 p += 2;
4157 (void)skip_expr(&p);
4158 if (*p == '`')
4159 ++p;
4160 continue;
4161 }
4162#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004163 /*
4164 * Quick check if this cannot be the start of a special string.
4165 * Also removes backslash before '%', '#' and '<'.
4166 */
4167 if (vim_strchr((char_u *)"%#<", *p) == NULL)
4168 {
4169 ++p;
4170 continue;
4171 }
4172
4173 /*
4174 * Try to find a match at this position.
4175 */
Bram Moolenaar63b92542007-03-27 14:57:09 +00004176 repl = eval_vars(p, eap->arg, &srclen, &(eap->do_ecmd_lnum),
4177 errormsgp, &escaped);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004178 if (*errormsgp != NULL) // error detected
Bram Moolenaar071d4272004-06-13 20:20:40 +00004179 return FAIL;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004180 if (repl == NULL) // no match found
Bram Moolenaar071d4272004-06-13 20:20:40 +00004181 {
4182 p += srclen;
4183 continue;
4184 }
4185
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004186 // Wildcards won't be expanded below, the replacement is taken
4187 // literally. But do expand "~/file", "~user/file" and "$HOME/file".
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00004188 if (vim_strchr(repl, '$') != NULL || vim_strchr(repl, '~') != NULL)
4189 {
4190 char_u *l = repl;
4191
4192 repl = expand_env_save(repl);
4193 vim_free(l);
4194 }
4195
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004196 // Need to escape white space et al. with a backslash.
4197 // Don't do this for:
4198 // - replacement that already has been escaped: "##"
4199 // - shell commands (may have to use quotes instead).
4200 // - non-unix systems when there is a single argument (spaces don't
4201 // separate arguments then).
Bram Moolenaar071d4272004-06-13 20:20:40 +00004202 if (!eap->usefilter
Bram Moolenaar63b92542007-03-27 14:57:09 +00004203 && !escaped
Bram Moolenaar071d4272004-06-13 20:20:40 +00004204 && eap->cmdidx != CMD_bang
Bram Moolenaar071d4272004-06-13 20:20:40 +00004205 && eap->cmdidx != CMD_grep
4206 && eap->cmdidx != CMD_grepadd
Bram Moolenaarbf15b8d2017-06-04 20:43:48 +02004207 && eap->cmdidx != CMD_hardcopy
Bram Moolenaar67883b42017-07-27 22:57:00 +02004208 && eap->cmdidx != CMD_lgrep
4209 && eap->cmdidx != CMD_lgrepadd
4210 && eap->cmdidx != CMD_lmake
4211 && eap->cmdidx != CMD_make
4212 && eap->cmdidx != CMD_terminal
Bram Moolenaar071d4272004-06-13 20:20:40 +00004213#ifndef UNIX
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004214 && !(eap->argt & EX_NOSPC)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004215#endif
4216 )
4217 {
4218 char_u *l;
4219#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004220 // Don't escape a backslash here, because rem_backslash() doesn't
4221 // remove it later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004222 static char_u *nobslash = (char_u *)" \t\"|";
4223# define ESCAPE_CHARS nobslash
4224#else
4225# define ESCAPE_CHARS escape_chars
4226#endif
4227
4228 for (l = repl; *l; ++l)
4229 if (vim_strchr(ESCAPE_CHARS, *l) != NULL)
4230 {
4231 l = vim_strsave_escaped(repl, ESCAPE_CHARS);
4232 if (l != NULL)
4233 {
4234 vim_free(repl);
4235 repl = l;
4236 }
4237 break;
4238 }
4239 }
4240
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004241 // For a shell command a '!' must be escaped.
Bram Moolenaar67883b42017-07-27 22:57:00 +02004242 if ((eap->usefilter || eap->cmdidx == CMD_bang
4243 || eap->cmdidx == CMD_terminal)
Bram Moolenaar31b7d382014-04-01 18:54:48 +02004244 && vim_strpbrk(repl, (char_u *)"!") != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004245 {
4246 char_u *l;
4247
Bram Moolenaar31b7d382014-04-01 18:54:48 +02004248 l = vim_strsave_escaped(repl, (char_u *)"!");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004249 if (l != NULL)
4250 {
4251 vim_free(repl);
4252 repl = l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004253 }
4254 }
4255
4256 p = repl_cmdline(eap, p, srclen, repl, cmdlinep);
4257 vim_free(repl);
4258 if (p == NULL)
4259 return FAIL;
4260 }
4261
4262 /*
4263 * One file argument: Expand wildcards.
4264 * Don't do this with ":r !command" or ":w !command".
4265 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004266 if ((eap->argt & EX_NOSPC) && !eap->usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004267 {
4268 /*
4269 * May do this twice:
4270 * 1. Replace environment variables.
4271 * 2. Replace any other wildcards, remove backslashes.
4272 */
4273 for (n = 1; n <= 2; ++n)
4274 {
4275 if (n == 2)
4276 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004277 /*
4278 * Halve the number of backslashes (this is Vi compatible).
4279 * For Unix and OS/2, when wildcards are expanded, this is
4280 * done by ExpandOne() below.
4281 */
Bram Moolenaare7fedb62015-12-31 19:07:19 +01004282#if defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004283 if (!has_wildcards)
4284#endif
4285 backslash_halve(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004286 }
4287
4288 if (has_wildcards)
4289 {
4290 if (n == 1)
4291 {
4292 /*
4293 * First loop: May expand environment variables. This
4294 * can be done much faster with expand_env() than with
4295 * something else (e.g., calling a shell).
4296 * After expanding environment variables, check again
4297 * if there are still wildcards present.
4298 */
4299 if (vim_strchr(eap->arg, '$') != NULL
4300 || vim_strchr(eap->arg, '~') != NULL)
4301 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +00004302 expand_env_esc(eap->arg, NameBuff, MAXPATHL,
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00004303 TRUE, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004304 has_wildcards = mch_has_wildcard(NameBuff);
4305 p = NameBuff;
4306 }
4307 else
4308 p = NULL;
4309 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004310 else // n == 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00004311 {
4312 expand_T xpc;
Bram Moolenaarb40c2572019-10-19 21:01:05 +02004313 int options = WILD_LIST_NOTFOUND
4314 | WILD_NOERROR | WILD_ADD_SLASH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004315
4316 ExpandInit(&xpc);
4317 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar94950a92010-12-02 16:01:29 +01004318 if (p_wic)
4319 options += WILD_ICASE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004320 p = ExpandOne(&xpc, eap->arg, NULL,
Bram Moolenaar94950a92010-12-02 16:01:29 +01004321 options, WILD_EXPAND_FREE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004322 if (p == NULL)
4323 return FAIL;
4324 }
4325 if (p != NULL)
4326 {
4327 (void)repl_cmdline(eap, eap->arg, (int)STRLEN(eap->arg),
4328 p, cmdlinep);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004329 if (n == 2) // p came from ExpandOne()
Bram Moolenaar071d4272004-06-13 20:20:40 +00004330 vim_free(p);
4331 }
4332 }
4333 }
4334 }
4335 return OK;
4336}
4337
4338/*
4339 * Replace part of the command line, keeping eap->cmd, eap->arg and
4340 * eap->nextcmd correct.
4341 * "src" points to the part that is to be replaced, of length "srclen".
4342 * "repl" is the replacement string.
4343 * Returns a pointer to the character after the replaced string.
4344 * Returns NULL for failure.
4345 */
4346 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004347repl_cmdline(
4348 exarg_T *eap,
4349 char_u *src,
4350 int srclen,
4351 char_u *repl,
4352 char_u **cmdlinep)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004353{
4354 int len;
4355 int i;
4356 char_u *new_cmdline;
4357
4358 /*
4359 * The new command line is build in new_cmdline[].
4360 * First allocate it.
4361 * Careful: a "+cmd" argument may have been NUL terminated.
4362 */
4363 len = (int)STRLEN(repl);
4364 i = (int)(src - *cmdlinep) + (int)STRLEN(src + srclen) + len + 3;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004365 if (eap->nextcmd != NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004366 i += (int)STRLEN(eap->nextcmd);// add space for next command
Bram Moolenaar964b3742019-05-24 18:54:09 +02004367 if ((new_cmdline = alloc(i)) == NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004368 return NULL; // out of memory!
Bram Moolenaar071d4272004-06-13 20:20:40 +00004369
4370 /*
4371 * Copy the stuff before the expanded part.
4372 * Copy the expanded stuff.
4373 * Copy what came after the expanded part.
4374 * Copy the next commands, if there are any.
4375 */
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004376 i = (int)(src - *cmdlinep); // length of part before match
Bram Moolenaar071d4272004-06-13 20:20:40 +00004377 mch_memmove(new_cmdline, *cmdlinep, (size_t)i);
Bram Moolenaara3ffd9c2005-07-21 21:03:15 +00004378
Bram Moolenaar071d4272004-06-13 20:20:40 +00004379 mch_memmove(new_cmdline + i, repl, (size_t)len);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004380 i += len; // remember the end of the string
Bram Moolenaar071d4272004-06-13 20:20:40 +00004381 STRCPY(new_cmdline + i, src + srclen);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004382 src = new_cmdline + i; // remember where to continue
Bram Moolenaar071d4272004-06-13 20:20:40 +00004383
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004384 if (eap->nextcmd != NULL) // append next command
Bram Moolenaar071d4272004-06-13 20:20:40 +00004385 {
4386 i = (int)STRLEN(new_cmdline) + 1;
4387 STRCPY(new_cmdline + i, eap->nextcmd);
4388 eap->nextcmd = new_cmdline + i;
4389 }
4390 eap->cmd = new_cmdline + (eap->cmd - *cmdlinep);
4391 eap->arg = new_cmdline + (eap->arg - *cmdlinep);
4392 if (eap->do_ecmd_cmd != NULL && eap->do_ecmd_cmd != dollar_command)
4393 eap->do_ecmd_cmd = new_cmdline + (eap->do_ecmd_cmd - *cmdlinep);
4394 vim_free(*cmdlinep);
4395 *cmdlinep = new_cmdline;
4396
4397 return src;
4398}
4399
4400/*
4401 * Check for '|' to separate commands and '"' to start comments.
4402 */
4403 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004404separate_nextcmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004405{
4406 char_u *p;
4407
Bram Moolenaar86b68352004-12-27 21:59:20 +00004408#ifdef FEAT_QUICKFIX
Bram Moolenaar748bf032005-02-02 23:04:36 +00004409 p = skip_grep_pat(eap);
4410#else
4411 p = eap->arg;
Bram Moolenaar86b68352004-12-27 21:59:20 +00004412#endif
4413
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004414 for ( ; *p; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004415 {
4416 if (*p == Ctrl_V)
4417 {
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004418 if (eap->argt & (EX_CTRLV | EX_XFILE))
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004419 ++p; // skip CTRL-V and next char
Bram Moolenaar071d4272004-06-13 20:20:40 +00004420 else
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004421 // remove CTRL-V and skip next char
Bram Moolenaara7241f52008-06-24 20:39:31 +00004422 STRMOVE(p, p + 1);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004423 if (*p == NUL) // stop at NUL after CTRL-V
Bram Moolenaar071d4272004-06-13 20:20:40 +00004424 break;
4425 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004426
4427#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004428 // Skip over `=expr` when wildcards are expanded.
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004429 else if (p[0] == '`' && p[1] == '=' && (eap->argt & EX_XFILE))
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004430 {
4431 p += 2;
4432 (void)skip_expr(&p);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004433 }
4434#endif
4435
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004436 // Check for '"': start of comment or '|': next command
4437 // :@" and :*" do not start a comment!
4438 // :redir @" doesn't either.
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004439 else if ((*p == '"' && !(eap->argt & EX_NOTRLCOM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004440 && ((eap->cmdidx != CMD_at && eap->cmdidx != CMD_star)
4441 || p != eap->arg)
4442 && (eap->cmdidx != CMD_redir
4443 || p != eap->arg + 1 || p[-1] != '@'))
Bram Moolenaara26b9702020-04-18 19:53:28 +02004444#ifdef FEAT_EVAL
4445 || (*p == '#' && in_vim9script()
4446 && p[1] != '{' && p > eap->cmd && VIM_ISWHITE(p[-1]))
4447#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004448 || *p == '|' || *p == '\n')
4449 {
4450 /*
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004451 * We remove the '\' before the '|', unless EX_CTRLV is used
Bram Moolenaar071d4272004-06-13 20:20:40 +00004452 * AND 'b' is present in 'cpoptions'.
4453 */
4454 if ((vim_strchr(p_cpo, CPO_BAR) == NULL
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004455 || !(eap->argt & EX_CTRLV)) && *(p - 1) == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004456 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004457 STRMOVE(p - 1, p); // remove the '\'
Bram Moolenaar071d4272004-06-13 20:20:40 +00004458 --p;
4459 }
4460 else
4461 {
4462 eap->nextcmd = check_nextcmd(p);
4463 *p = NUL;
4464 break;
4465 }
4466 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004467 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00004468
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004469 if (!(eap->argt & EX_NOTRLCOM)) // remove trailing spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00004470 del_trailing_spaces(eap->arg);
4471}
4472
4473/*
4474 * get + command from ex argument
4475 */
4476 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004477getargcmd(char_u **argp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004478{
4479 char_u *arg = *argp;
4480 char_u *command = NULL;
4481
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004482 if (*arg == '+') // +[command]
Bram Moolenaar071d4272004-06-13 20:20:40 +00004483 {
4484 ++arg;
Bram Moolenaar3e451592014-04-02 14:22:05 +02004485 if (vim_isspace(*arg) || *arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004486 command = dollar_command;
4487 else
4488 {
4489 command = arg;
4490 arg = skip_cmd_arg(command, TRUE);
4491 if (*arg != NUL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004492 *arg++ = NUL; // terminate command with NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004493 }
4494
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004495 arg = skipwhite(arg); // skip over spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00004496 *argp = arg;
4497 }
4498 return command;
4499}
4500
4501/*
4502 * Find end of "+command" argument. Skip over "\ " and "\\".
4503 */
Bram Moolenaard0190392019-08-23 21:17:35 +02004504 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004505skip_cmd_arg(
4506 char_u *p,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004507 int rembs) // TRUE to halve the number of backslashes
Bram Moolenaar071d4272004-06-13 20:20:40 +00004508{
4509 while (*p && !vim_isspace(*p))
4510 {
4511 if (*p == '\\' && p[1] != NUL)
4512 {
4513 if (rembs)
Bram Moolenaara7241f52008-06-24 20:39:31 +00004514 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004515 else
4516 ++p;
4517 }
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004518 MB_PTR_ADV(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004519 }
4520 return p;
4521}
4522
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004523 int
4524get_bad_opt(char_u *p, exarg_T *eap)
4525{
4526 if (STRICMP(p, "keep") == 0)
4527 eap->bad_char = BAD_KEEP;
4528 else if (STRICMP(p, "drop") == 0)
4529 eap->bad_char = BAD_DROP;
4530 else if (MB_BYTE2LEN(*p) == 1 && p[1] == NUL)
4531 eap->bad_char = *p;
Bram Moolenaar75808492018-06-12 12:39:41 +02004532 else
4533 return FAIL;
4534 return OK;
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004535}
4536
Bram Moolenaar071d4272004-06-13 20:20:40 +00004537/*
4538 * Get "++opt=arg" argument.
4539 * Return FAIL or OK.
4540 */
4541 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004542getargopt(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004543{
4544 char_u *arg = eap->arg + 2;
4545 int *pp = NULL;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02004546 int bad_char_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004547 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004548
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004549 // ":edit ++[no]bin[ary] file"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004550 if (STRNCMP(arg, "bin", 3) == 0 || STRNCMP(arg, "nobin", 5) == 0)
4551 {
4552 if (*arg == 'n')
4553 {
4554 arg += 2;
4555 eap->force_bin = FORCE_NOBIN;
4556 }
4557 else
4558 eap->force_bin = FORCE_BIN;
4559 if (!checkforcmd(&arg, "binary", 3))
4560 return FAIL;
4561 eap->arg = skipwhite(arg);
4562 return OK;
4563 }
4564
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004565 // ":read ++edit file"
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004566 if (STRNCMP(arg, "edit", 4) == 0)
4567 {
4568 eap->read_edit = TRUE;
4569 eap->arg = skipwhite(arg + 4);
4570 return OK;
4571 }
4572
Bram Moolenaar071d4272004-06-13 20:20:40 +00004573 if (STRNCMP(arg, "ff", 2) == 0)
4574 {
4575 arg += 2;
4576 pp = &eap->force_ff;
4577 }
4578 else if (STRNCMP(arg, "fileformat", 10) == 0)
4579 {
4580 arg += 10;
4581 pp = &eap->force_ff;
4582 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004583 else if (STRNCMP(arg, "enc", 3) == 0)
4584 {
Bram Moolenaarb38e9ab2011-12-14 14:49:45 +01004585 if (STRNCMP(arg, "encoding", 8) == 0)
4586 arg += 8;
4587 else
4588 arg += 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004589 pp = &eap->force_enc;
4590 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004591 else if (STRNCMP(arg, "bad", 3) == 0)
4592 {
4593 arg += 3;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02004594 pp = &bad_char_idx;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004595 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004596
4597 if (pp == NULL || *arg != '=')
4598 return FAIL;
4599
4600 ++arg;
4601 *pp = (int)(arg - eap->cmd);
4602 arg = skip_cmd_arg(arg, FALSE);
4603 eap->arg = skipwhite(arg);
4604 *arg = NUL;
4605
Bram Moolenaar071d4272004-06-13 20:20:40 +00004606 if (pp == &eap->force_ff)
4607 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004608 if (check_ff_value(eap->cmd + eap->force_ff) == FAIL)
4609 return FAIL;
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004610 eap->force_ff = eap->cmd[eap->force_ff];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004611 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004612 else if (pp == &eap->force_enc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004613 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004614 // Make 'fileencoding' lower case.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004615 for (p = eap->cmd + eap->force_enc; *p != NUL; ++p)
4616 *p = TOLOWER_ASC(*p);
4617 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004618 else
4619 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004620 // Check ++bad= argument. Must be a single-byte character, "keep" or
4621 // "drop".
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004622 if (get_bad_opt(eap->cmd + bad_char_idx, eap) == FAIL)
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004623 return FAIL;
4624 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004625
4626 return OK;
4627}
4628
Bram Moolenaar071d4272004-06-13 20:20:40 +00004629 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004630ex_autocmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004631{
4632 /*
Bram Moolenaar26d4b892018-07-04 22:26:28 +02004633 * Disallow autocommands from .exrc and .vimrc in current
Bram Moolenaar071d4272004-06-13 20:20:40 +00004634 * directory for security reasons.
4635 */
4636 if (secure)
4637 {
4638 secure = 2;
4639 eap->errmsg = e_curdir;
4640 }
4641 else if (eap->cmdidx == CMD_autocmd)
4642 do_autocmd(eap->arg, eap->forceit);
4643 else
4644 do_augroup(eap->arg, eap->forceit);
4645}
4646
4647/*
4648 * ":doautocmd": Apply the automatic commands to the current buffer.
4649 */
4650 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004651ex_doautocmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004652{
Bram Moolenaar60542ac2012-02-12 20:14:01 +01004653 char_u *arg = eap->arg;
4654 int call_do_modelines = check_nomodeline(&arg);
Bram Moolenaar1610d052016-06-09 22:53:01 +02004655 int did_aucmd;
Bram Moolenaar60542ac2012-02-12 20:14:01 +01004656
Bram Moolenaar1610d052016-06-09 22:53:01 +02004657 (void)do_doautocmd(arg, TRUE, &did_aucmd);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004658 // Only when there is no <nomodeline>.
Bram Moolenaar1610d052016-06-09 22:53:01 +02004659 if (call_do_modelines && did_aucmd)
Bram Moolenaar60542ac2012-02-12 20:14:01 +01004660 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004661}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004662
Bram Moolenaar071d4272004-06-13 20:20:40 +00004663/*
4664 * :[N]bunload[!] [N] [bufname] unload buffer
4665 * :[N]bdelete[!] [N] [bufname] delete buffer from buffer list
4666 * :[N]bwipeout[!] [N] [bufname] delete buffer really
4667 */
4668 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004669ex_bunload(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004670{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01004671 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02004672 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004673 eap->errmsg = do_bufdel(
4674 eap->cmdidx == CMD_bdelete ? DOBUF_DEL
4675 : eap->cmdidx == CMD_bwipeout ? DOBUF_WIPE
4676 : DOBUF_UNLOAD, eap->arg,
4677 eap->addr_count, (int)eap->line1, (int)eap->line2, eap->forceit);
4678}
4679
4680/*
4681 * :[N]buffer [N] to buffer N
4682 * :[N]sbuffer [N] to buffer N
4683 */
4684 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004685ex_buffer(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004686{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01004687 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02004688 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004689 if (*eap->arg)
4690 eap->errmsg = e_trailing;
4691 else
4692 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004693 if (eap->addr_count == 0) // default is current buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00004694 goto_buffer(eap, DOBUF_CURRENT, FORWARD, 0);
4695 else
4696 goto_buffer(eap, DOBUF_FIRST, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02004697 if (eap->do_ecmd_cmd != NULL)
4698 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004699 }
4700}
4701
4702/*
4703 * :[N]bmodified [N] to next mod. buffer
4704 * :[N]sbmodified [N] to next mod. buffer
4705 */
4706 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004707ex_bmodified(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004708{
4709 goto_buffer(eap, DOBUF_MOD, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02004710 if (eap->do_ecmd_cmd != NULL)
4711 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004712}
4713
4714/*
4715 * :[N]bnext [N] to next buffer
4716 * :[N]sbnext [N] split and to next buffer
4717 */
4718 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004719ex_bnext(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004720{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01004721 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar3029bcc2020-01-18 15:06:19 +01004722 return;
4723
Bram Moolenaar071d4272004-06-13 20:20:40 +00004724 goto_buffer(eap, DOBUF_CURRENT, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02004725 if (eap->do_ecmd_cmd != NULL)
4726 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004727}
4728
4729/*
4730 * :[N]bNext [N] to previous buffer
4731 * :[N]bprevious [N] to previous buffer
4732 * :[N]sbNext [N] split and to previous buffer
4733 * :[N]sbprevious [N] split and to previous buffer
4734 */
4735 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004736ex_bprevious(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004737{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01004738 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar3029bcc2020-01-18 15:06:19 +01004739 return;
4740
Bram Moolenaar071d4272004-06-13 20:20:40 +00004741 goto_buffer(eap, DOBUF_CURRENT, BACKWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02004742 if (eap->do_ecmd_cmd != NULL)
4743 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004744}
4745
4746/*
4747 * :brewind to first buffer
4748 * :bfirst to first buffer
4749 * :sbrewind split and to first buffer
4750 * :sbfirst split and to first buffer
4751 */
4752 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004753ex_brewind(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004754{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01004755 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar3029bcc2020-01-18 15:06:19 +01004756 return;
4757
Bram Moolenaar071d4272004-06-13 20:20:40 +00004758 goto_buffer(eap, DOBUF_FIRST, FORWARD, 0);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02004759 if (eap->do_ecmd_cmd != NULL)
4760 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004761}
4762
4763/*
4764 * :blast to last buffer
4765 * :sblast split and to last buffer
4766 */
4767 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004768ex_blast(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004769{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01004770 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar3029bcc2020-01-18 15:06:19 +01004771 return;
4772
Bram Moolenaar071d4272004-06-13 20:20:40 +00004773 goto_buffer(eap, DOBUF_LAST, BACKWARD, 0);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02004774 if (eap->do_ecmd_cmd != NULL)
4775 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004776}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004777
Bram Moolenaar7a092242020-04-16 22:10:49 +02004778/*
4779 * Check if "c" ends an Ex command.
Bram Moolenaara494f562020-04-18 17:45:38 +02004780 * In Vim9 script does not check for white space before # or #{.
Bram Moolenaar7a092242020-04-16 22:10:49 +02004781 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004782 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004783ends_excmd(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004784{
Bram Moolenaarfbda69b2020-04-13 15:06:53 +02004785#ifdef FEAT_EVAL
Bram Moolenaar2c330432020-04-13 14:41:35 +02004786 if (c == '#')
Bram Moolenaar7a092242020-04-16 22:10:49 +02004787 return in_vim9script();
4788#endif
4789 return (c == NUL || c == '|' || c == '"' || c == '\n');
4790}
4791
4792/*
4793 * Like ends_excmd() but checks that a # in Vim9 script either has "cmd" equal
4794 * to "cmd_start" or has a white space character before it.
4795 */
4796 int
Bram Moolenaar21cfe502020-04-16 23:01:50 +02004797ends_excmd2(char_u *cmd_start UNUSED, char_u *cmd)
Bram Moolenaar7a092242020-04-16 22:10:49 +02004798{
4799 int c = *cmd;
4800
4801#ifdef FEAT_EVAL
Bram Moolenaara26b9702020-04-18 19:53:28 +02004802 if (c == '#' && cmd[1] != '{' && (cmd == cmd_start || VIM_ISWHITE(cmd[-1])))
Bram Moolenaar2c330432020-04-13 14:41:35 +02004803 return in_vim9script();
Bram Moolenaarfbda69b2020-04-13 15:06:53 +02004804#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004805 return (c == NUL || c == '|' || c == '"' || c == '\n');
4806}
4807
4808#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA) || defined(FEAT_EVAL) \
4809 || defined(PROTO)
4810/*
4811 * Return the next command, after the first '|' or '\n'.
4812 * Return NULL if not found.
4813 */
4814 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004815find_nextcmd(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004816{
4817 while (*p != '|' && *p != '\n')
4818 {
4819 if (*p == NUL)
4820 return NULL;
4821 ++p;
4822 }
4823 return (p + 1);
4824}
4825#endif
4826
4827/*
Bram Moolenaar2256c992016-11-15 21:17:07 +01004828 * Check if *p is a separator between Ex commands, skipping over white space.
4829 * Return NULL if it isn't, the following character if it is.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004830 */
4831 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004832check_nextcmd(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004833{
Bram Moolenaar2256c992016-11-15 21:17:07 +01004834 char_u *s = skipwhite(p);
4835
4836 if (*s == '|' || *s == '\n')
4837 return (s + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004838 else
4839 return NULL;
4840}
4841
4842/*
4843 * - if there are more files to edit
4844 * - and this is the last window
4845 * - and forceit not used
4846 * - and not repeated twice on a row
4847 * return FAIL and give error message if 'message' TRUE
4848 * return OK otherwise
4849 */
4850 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004851check_more(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004852 int message, // when FALSE check only, no messages
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004853 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004854{
4855 int n = ARGCOUNT - curwin->w_arg_idx - 1;
4856
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00004857 if (!forceit && only_one_window()
4858 && ARGCOUNT > 1 && !arg_had_last && n >= 0 && quitmore == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004859 {
4860 if (message)
4861 {
4862#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
4863 if ((p_confirm || cmdmod.confirm) && curbuf->b_fname != NULL)
4864 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02004865 char_u buff[DIALOG_MSG_SIZE];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004866
Bram Moolenaarda6e8912018-08-21 15:12:14 +02004867 vim_snprintf((char *)buff, DIALOG_MSG_SIZE,
4868 NGETTEXT("%d more file to edit. Quit anyway?",
4869 "%d more files to edit. Quit anyway?", n), n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004870 if (vim_dialog_yesno(VIM_QUESTION, NULL, buff, 1) == VIM_YES)
4871 return OK;
4872 return FAIL;
4873 }
4874#endif
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01004875 semsg(NGETTEXT("E173: %d more file to edit",
4876 "E173: %d more files to edit", n), n);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004877 quitmore = 2; // next try to quit is allowed
Bram Moolenaar071d4272004-06-13 20:20:40 +00004878 }
4879 return FAIL;
4880 }
4881 return OK;
4882}
4883
Bram Moolenaar071d4272004-06-13 20:20:40 +00004884/*
4885 * Function given to ExpandGeneric() to obtain the list of command names.
4886 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004887 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004888get_command_name(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004889{
4890 if (idx >= (int)CMD_SIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004891 return get_user_command_name(idx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004892 return cmdnames[idx].cmd_name;
4893}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004894
Bram Moolenaar071d4272004-06-13 20:20:40 +00004895 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004896ex_colorscheme(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004897{
Bram Moolenaare6850792010-05-14 15:28:44 +02004898 if (*eap->arg == NUL)
4899 {
4900#ifdef FEAT_EVAL
4901 char_u *expr = vim_strsave((char_u *)"g:colors_name");
4902 char_u *p = NULL;
4903
4904 if (expr != NULL)
4905 {
4906 ++emsg_off;
4907 p = eval_to_string(expr, NULL, FALSE);
4908 --emsg_off;
4909 vim_free(expr);
4910 }
4911 if (p != NULL)
4912 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01004913 msg((char *)p);
Bram Moolenaare6850792010-05-14 15:28:44 +02004914 vim_free(p);
4915 }
4916 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01004917 msg("default");
Bram Moolenaare6850792010-05-14 15:28:44 +02004918#else
Bram Moolenaar32526b32019-01-19 17:43:09 +01004919 msg(_("unknown"));
Bram Moolenaare6850792010-05-14 15:28:44 +02004920#endif
4921 }
4922 else if (load_colors(eap->arg) == FAIL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004923 semsg(_("E185: Cannot find color scheme '%s'"), eap->arg);
Bram Moolenaarf58d81a2019-01-28 20:19:05 +01004924
4925#ifdef FEAT_VTP
4926 else if (has_vtp_working())
4927 {
4928 // background color change requires clear + redraw
4929 update_screen(CLEAR);
4930 redrawcmd();
4931 }
4932#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004933}
4934
4935 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004936ex_highlight(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004937{
4938 if (*eap->arg == NUL && eap->cmd[2] == '!')
Bram Moolenaar32526b32019-01-19 17:43:09 +01004939 msg(_("Greetings, Vim user!"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004940 do_highlight(eap->arg, eap->forceit, FALSE);
4941}
4942
4943
4944/*
4945 * Call this function if we thought we were going to exit, but we won't
4946 * (because of an error). May need to restore the terminal mode.
4947 */
4948 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004949not_exiting(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004950{
4951 exiting = FALSE;
4952 settmode(TMODE_RAW);
4953}
4954
Bram Moolenaar12a96de2018-03-11 14:44:18 +01004955 static int
4956before_quit_autocmds(win_T *wp, int quit_all, int forceit)
4957{
4958 apply_autocmds(EVENT_QUITPRE, NULL, NULL, FALSE, wp->w_buffer);
4959
Bram Moolenaar34ba06b2019-10-20 22:27:10 +02004960 // Bail out when autocommands closed the window.
4961 // Refuse to quit when the buffer in the last window is being closed (can
4962 // only happen in autocommands).
Bram Moolenaar12a96de2018-03-11 14:44:18 +01004963 if (!win_valid(wp)
4964 || curbuf_locked()
4965 || (wp->w_buffer->b_nwindows == 1 && wp->w_buffer->b_locked > 0))
4966 return TRUE;
4967
4968 if (quit_all || (check_more(FALSE, forceit) == OK && only_one_window()))
4969 {
4970 apply_autocmds(EVENT_EXITPRE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar34ba06b2019-10-20 22:27:10 +02004971 // Refuse to quit when locked or when the window was closed or the
4972 // buffer in the last window is being closed (can only happen in
4973 // autocommands).
4974 if (!win_valid(wp) || curbuf_locked()
Bram Moolenaar12a96de2018-03-11 14:44:18 +01004975 || (curbuf->b_nwindows == 1 && curbuf->b_locked > 0))
4976 return TRUE;
4977 }
4978
4979 return FALSE;
4980}
4981
Bram Moolenaar071d4272004-06-13 20:20:40 +00004982/*
Bram Moolenaarf240e182014-11-27 18:33:02 +01004983 * ":quit": quit current window, quit Vim if the last window is closed.
Bram Moolenaar12a96de2018-03-11 14:44:18 +01004984 * ":{nr}quit": quit window {nr}
Bram Moolenaar4d14bac2019-10-20 21:15:15 +02004985 * Also used when closing a terminal window that's the last one.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004986 */
Bram Moolenaar4d14bac2019-10-20 21:15:15 +02004987 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004988ex_quit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004989{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004990 win_T *wp;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004991
Bram Moolenaar071d4272004-06-13 20:20:40 +00004992#ifdef FEAT_CMDWIN
4993 if (cmdwin_type != 0)
4994 {
4995 cmdwin_result = Ctrl_C;
4996 return;
4997 }
4998#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004999 // Don't quit while editing the command line.
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005000 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005001 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005002 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005003 return;
5004 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005005 if (eap->addr_count > 0)
5006 {
Bram Moolenaarf240e182014-11-27 18:33:02 +01005007 int wnr = eap->line2;
5008
5009 for (wp = firstwin; wp->w_next != NULL; wp = wp->w_next)
5010 if (--wnr <= 0)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005011 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005012 }
5013 else
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005014 wp = curwin;
Bram Moolenaarf240e182014-11-27 18:33:02 +01005015
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005016 // Refuse to quit when locked.
Bram Moolenaar87ffb5c2017-10-19 12:37:42 +02005017 if (curbuf_locked())
5018 return;
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005019
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005020 // Trigger QuitPre and maybe ExitPre
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005021 if (before_quit_autocmds(wp, FALSE, eap->forceit))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005022 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005023
5024#ifdef FEAT_NETBEANS_INTG
5025 netbeansForcedQuit = eap->forceit;
5026#endif
5027
5028 /*
5029 * If there are more files or windows we won't exit.
5030 */
5031 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
5032 exiting = TRUE;
Bram Moolenaarff930ca2017-10-19 17:12:10 +02005033 if ((!buf_hide(wp->w_buffer)
5034 && check_changed(wp->w_buffer, (p_awa ? CCGD_AW : 0)
Bram Moolenaar45d3b142013-11-09 03:31:51 +01005035 | (eap->forceit ? CCGD_FORCEIT : 0)
5036 | CCGD_EXCMD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005037 || check_more(TRUE, eap->forceit) == FAIL
Bram Moolenaar027387f2016-01-02 22:25:52 +01005038 || (only_one_window() && check_changed_any(eap->forceit, TRUE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005039 {
5040 not_exiting();
5041 }
5042 else
5043 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005044 // quit last window
5045 // Note: only_one_window() returns true, even so a help window is
5046 // still open. In that case only quit, if no address has been
5047 // specified. Example:
5048 // :h|wincmd w|1q - don't quit
5049 // :h|wincmd w|q - quit
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01005050 if (only_one_window() && (ONE_WINDOW || eap->addr_count == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005051 getout(0);
Bram Moolenaar2c33d7b2017-10-14 16:06:20 +02005052 not_exiting();
Bram Moolenaar4033c552017-09-16 20:54:51 +02005053#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00005054 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005055#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005056 // close window; may free buffer
Bram Moolenaareb44a682017-08-03 22:44:55 +02005057 win_close(wp, !buf_hide(wp->w_buffer) || eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005058 }
5059}
5060
5061/*
5062 * ":cquit".
5063 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005064 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005065ex_cquit(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005066{
Bram Moolenaar1860bde2020-01-06 21:47:21 +01005067 // this does not always pass on the exit code to the Manx compiler. why?
5068 getout(eap->addr_count > 0 ? (int)eap->line2 : EXIT_FAILURE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005069}
5070
5071/*
5072 * ":qall": try to quit all windows
5073 */
5074 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005075ex_quit_all(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005076{
5077# ifdef FEAT_CMDWIN
5078 if (cmdwin_type != 0)
5079 {
5080 if (eap->forceit)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005081 cmdwin_result = K_XF1; // ex_window() takes care of this
Bram Moolenaar071d4272004-06-13 20:20:40 +00005082 else
5083 cmdwin_result = K_XF2;
5084 return;
5085 }
5086# endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005087
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005088 // Don't quit while editing the command line.
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005089 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005090 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005091 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005092 return;
5093 }
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005094
5095 if (before_quit_autocmds(curwin, TRUE, eap->forceit))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005096 return;
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005097
Bram Moolenaar071d4272004-06-13 20:20:40 +00005098 exiting = TRUE;
Bram Moolenaar027387f2016-01-02 22:25:52 +01005099 if (eap->forceit || !check_changed_any(FALSE, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005100 getout(0);
5101 not_exiting();
5102}
5103
Bram Moolenaar071d4272004-06-13 20:20:40 +00005104/*
5105 * ":close": close current window, unless it is the last one
5106 */
5107 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005108ex_close(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005109{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005110 win_T *win;
5111 int winnr = 0;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005112#ifdef FEAT_CMDWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00005113 if (cmdwin_type != 0)
Bram Moolenaar9bd1a7e2011-05-19 14:50:54 +02005114 cmdwin_result = Ctrl_C;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005115 else
Bram Moolenaar4033c552017-09-16 20:54:51 +02005116#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005117 if (!text_locked() && !curbuf_locked())
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005118 {
5119 if (eap->addr_count == 0)
5120 ex_win_close(eap->forceit, curwin, NULL);
Bram Moolenaar6d41c782018-06-06 09:11:12 +02005121 else
5122 {
Bram Moolenaar29323592016-07-24 22:04:11 +02005123 FOR_ALL_WINDOWS(win)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005124 {
5125 winnr++;
5126 if (winnr == eap->line2)
5127 break;
5128 }
5129 if (win == NULL)
5130 win = lastwin;
5131 ex_win_close(eap->forceit, win, NULL);
5132 }
5133 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005134}
5135
Bram Moolenaar4033c552017-09-16 20:54:51 +02005136#ifdef FEAT_QUICKFIX
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005137/*
5138 * ":pclose": Close any preview window.
5139 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005140 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005141ex_pclose(exarg_T *eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005142{
5143 win_T *win;
5144
Bram Moolenaar79648732019-07-18 21:43:07 +02005145 // First close any normal window.
Bram Moolenaar29323592016-07-24 22:04:11 +02005146 FOR_ALL_WINDOWS(win)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005147 if (win->w_p_pvw)
5148 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00005149 ex_win_close(eap->forceit, win, NULL);
Bram Moolenaar79648732019-07-18 21:43:07 +02005150 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005151 }
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01005152# ifdef FEAT_PROP_POPUP
Bram Moolenaar79648732019-07-18 21:43:07 +02005153 // Also when 'previewpopup' is empty, it might have been cleared.
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02005154 popup_close_preview();
Bram Moolenaar79648732019-07-18 21:43:07 +02005155# endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005156}
Bram Moolenaar4033c552017-09-16 20:54:51 +02005157#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005158
Bram Moolenaarf740b292006-02-16 22:11:02 +00005159/*
5160 * Close window "win" and take care of handling closing the last window for a
5161 * modified buffer.
5162 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005163 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005164ex_win_close(
5165 int forceit,
5166 win_T *win,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005167 tabpage_T *tp) // NULL or the tab page "win" is in
Bram Moolenaar071d4272004-06-13 20:20:40 +00005168{
5169 int need_hide;
5170 buf_T *buf = win->w_buffer;
5171
5172 need_hide = (bufIsChanged(buf) && buf->b_nwindows <= 1);
Bram Moolenaareb44a682017-08-03 22:44:55 +02005173 if (need_hide && !buf_hide(buf) && !forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005174 {
Bram Moolenaar4033c552017-09-16 20:54:51 +02005175#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005176 if ((p_confirm || cmdmod.confirm) && p_write)
5177 {
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005178 bufref_T bufref;
5179
5180 set_bufref(&bufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005181 dialog_changed(buf, FALSE);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005182 if (bufref_valid(&bufref) && bufIsChanged(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005183 return;
5184 need_hide = FALSE;
5185 }
5186 else
Bram Moolenaar4033c552017-09-16 20:54:51 +02005187#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005188 {
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02005189 no_write_message();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005190 return;
5191 }
5192 }
5193
Bram Moolenaar4033c552017-09-16 20:54:51 +02005194#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00005195 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005196#endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00005197
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005198 // free buffer when not hiding it or when it's a scratch buffer
Bram Moolenaarf740b292006-02-16 22:11:02 +00005199 if (tp == NULL)
Bram Moolenaareb44a682017-08-03 22:44:55 +02005200 win_close(win, !need_hide && !buf_hide(buf));
Bram Moolenaarf740b292006-02-16 22:11:02 +00005201 else
Bram Moolenaareb44a682017-08-03 22:44:55 +02005202 win_close_othertab(win, !need_hide && !buf_hide(buf), tp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005203}
5204
Bram Moolenaar071d4272004-06-13 20:20:40 +00005205/*
Bram Moolenaardea25702017-01-29 15:18:10 +01005206 * Handle the argument for a tabpage related ex command.
5207 * Returns a tabpage number.
5208 * When an error is encountered then eap->errmsg is set.
5209 */
5210 static int
5211get_tabpage_arg(exarg_T *eap)
5212{
5213 int tab_number;
5214 int unaccept_arg0 = (eap->cmdidx == CMD_tabmove) ? 0 : 1;
5215
5216 if (eap->arg && *eap->arg != NUL)
5217 {
5218 char_u *p = eap->arg;
5219 char_u *p_save;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005220 int relative = 0; // argument +N/-N means: go to N places to the
5221 // right/left relative to the current position.
Bram Moolenaardea25702017-01-29 15:18:10 +01005222
5223 if (*p == '-')
5224 {
5225 relative = -1;
5226 p++;
5227 }
5228 else if (*p == '+')
5229 {
5230 relative = 1;
5231 p++;
5232 }
5233
5234 p_save = p;
5235 tab_number = getdigits(&p);
5236
5237 if (relative == 0)
5238 {
5239 if (STRCMP(p, "$") == 0)
5240 tab_number = LAST_TAB_NR;
5241 else if (p == p_save || *p_save == '-' || *p != NUL
5242 || tab_number > LAST_TAB_NR)
5243 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005244 // No numbers as argument.
Bram Moolenaardea25702017-01-29 15:18:10 +01005245 eap->errmsg = e_invarg;
5246 goto theend;
5247 }
5248 }
5249 else
5250 {
5251 if (*p_save == NUL)
5252 tab_number = 1;
5253 else if (p == p_save || *p_save == '-' || *p != NUL
5254 || tab_number == 0)
5255 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005256 // No numbers as argument.
Bram Moolenaardea25702017-01-29 15:18:10 +01005257 eap->errmsg = e_invarg;
5258 goto theend;
5259 }
5260 tab_number = tab_number * relative + tabpage_index(curtab);
5261 if (!unaccept_arg0 && relative == -1)
5262 --tab_number;
5263 }
5264 if (tab_number < unaccept_arg0 || tab_number > LAST_TAB_NR)
5265 eap->errmsg = e_invarg;
5266 }
5267 else if (eap->addr_count > 0)
5268 {
5269 if (unaccept_arg0 && eap->line2 == 0)
Bram Moolenaarc6251552017-01-29 21:42:20 +01005270 {
Bram Moolenaardea25702017-01-29 15:18:10 +01005271 eap->errmsg = e_invrange;
Bram Moolenaarc6251552017-01-29 21:42:20 +01005272 tab_number = 0;
5273 }
Bram Moolenaardea25702017-01-29 15:18:10 +01005274 else
5275 {
5276 tab_number = eap->line2;
Bram Moolenaar82a12462019-01-22 22:41:42 +01005277 if (!unaccept_arg0 && *skipwhite(*eap->cmdlinep) == '-')
Bram Moolenaardea25702017-01-29 15:18:10 +01005278 {
5279 --tab_number;
5280 if (tab_number < unaccept_arg0)
5281 eap->errmsg = e_invarg;
5282 }
5283 }
5284 }
5285 else
5286 {
5287 switch (eap->cmdidx)
5288 {
5289 case CMD_tabnext:
5290 tab_number = tabpage_index(curtab) + 1;
5291 if (tab_number > LAST_TAB_NR)
5292 tab_number = 1;
5293 break;
5294 case CMD_tabmove:
5295 tab_number = LAST_TAB_NR;
5296 break;
5297 default:
5298 tab_number = tabpage_index(curtab);
5299 }
5300 }
5301
5302theend:
5303 return tab_number;
5304}
5305
5306/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00005307 * ":tabclose": close current tab page, unless it is the last one.
5308 * ":tabclose N": close tab page N.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005309 */
5310 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005311ex_tabclose(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005312{
Bram Moolenaarf740b292006-02-16 22:11:02 +00005313 tabpage_T *tp;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005314 int tab_number;
Bram Moolenaarf740b292006-02-16 22:11:02 +00005315
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005316# ifdef FEAT_CMDWIN
5317 if (cmdwin_type != 0)
5318 cmdwin_result = K_IGNORE;
5319 else
5320# endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00005321 if (first_tabpage->tp_next == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005322 emsg(_("E784: Cannot close last tab page"));
Bram Moolenaarf740b292006-02-16 22:11:02 +00005323 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005324 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005325 tab_number = get_tabpage_arg(eap);
5326 if (eap->errmsg == NULL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005327 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005328 tp = find_tabpage(tab_number);
Bram Moolenaarf740b292006-02-16 22:11:02 +00005329 if (tp == NULL)
5330 {
5331 beep_flush();
5332 return;
5333 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005334 if (tp != curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00005335 {
5336 tabpage_close_other(tp, eap->forceit);
5337 return;
5338 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005339 else if (!text_locked() && !curbuf_locked())
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005340 tabpage_close(eap->forceit);
5341 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005342 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005343}
5344
5345/*
5346 * ":tabonly": close all tab pages except the current one
5347 */
5348 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005349ex_tabonly(exarg_T *eap)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005350{
5351 tabpage_T *tp;
5352 int done;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005353 int tab_number;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005354
5355# ifdef FEAT_CMDWIN
5356 if (cmdwin_type != 0)
5357 cmdwin_result = K_IGNORE;
5358 else
5359# endif
5360 if (first_tabpage->tp_next == NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01005361 msg(_("Already only one tab page"));
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005362 else
5363 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005364 tab_number = get_tabpage_arg(eap);
5365 if (eap->errmsg == NULL)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005366 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005367 goto_tabpage(tab_number);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005368 // Repeat this up to a 1000 times, because autocommands may
5369 // mess up the lists.
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005370 for (done = 0; done < 1000; ++done)
5371 {
5372 FOR_ALL_TABPAGES(tp)
5373 if (tp->tp_topframe != topframe)
5374 {
5375 tabpage_close_other(tp, eap->forceit);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005376 // if we failed to close it quit
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005377 if (valid_tabpage(tp))
5378 done = 1000;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005379 // start over, "tp" is now invalid
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005380 break;
5381 }
5382 if (first_tabpage->tp_next == NULL)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005383 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005384 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005385 }
5386 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005387}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005388
5389/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00005390 * Close the current tab page.
5391 */
5392 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005393tabpage_close(int forceit)
Bram Moolenaarf740b292006-02-16 22:11:02 +00005394{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005395 // First close all the windows but the current one. If that worked then
5396 // close the last window in this tab, that will close it.
Bram Moolenaar459ca562016-11-10 18:16:33 +01005397 if (!ONE_WINDOW)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005398 close_others(TRUE, forceit);
Bram Moolenaar459ca562016-11-10 18:16:33 +01005399 if (ONE_WINDOW)
Bram Moolenaarf740b292006-02-16 22:11:02 +00005400 ex_win_close(forceit, curwin, NULL);
5401# ifdef FEAT_GUI
5402 need_mouse_correct = TRUE;
5403# endif
5404}
5405
5406/*
5407 * Close tab page "tp", which is not the current tab page.
5408 * Note that autocommands may make "tp" invalid.
Bram Moolenaar7875acc2006-09-10 13:51:17 +00005409 * Also takes care of the tab pages line disappearing when closing the
5410 * last-but-one tab page.
Bram Moolenaarf740b292006-02-16 22:11:02 +00005411 */
5412 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005413tabpage_close_other(tabpage_T *tp, int forceit)
Bram Moolenaarf740b292006-02-16 22:11:02 +00005414{
5415 int done = 0;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005416 win_T *wp;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00005417 int h = tabline_height();
Bram Moolenaarf740b292006-02-16 22:11:02 +00005418
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005419 // Limit to 1000 windows, autocommands may add a window while we close
5420 // one. OK, so I'm paranoid...
Bram Moolenaarf740b292006-02-16 22:11:02 +00005421 while (++done < 1000)
5422 {
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005423 wp = tp->tp_firstwin;
5424 ex_win_close(forceit, wp, tp);
Bram Moolenaarf740b292006-02-16 22:11:02 +00005425
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005426 // Autocommands may delete the tab page under our fingers and we may
5427 // fail to close a window with a modified buffer.
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005428 if (!valid_tabpage(tp) || tp->tp_firstwin == wp)
Bram Moolenaarf740b292006-02-16 22:11:02 +00005429 break;
5430 }
Bram Moolenaar7875acc2006-09-10 13:51:17 +00005431
Bram Moolenaar29323592016-07-24 22:04:11 +02005432 apply_autocmds(EVENT_TABCLOSED, NULL, NULL, FALSE, curbuf);
Bram Moolenaar29323592016-07-24 22:04:11 +02005433
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005434 redraw_tabline = TRUE;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00005435 if (h != tabline_height())
5436 shell_new_rows();
Bram Moolenaarf740b292006-02-16 22:11:02 +00005437}
5438
5439/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005440 * ":only".
5441 */
5442 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005443ex_only(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005444{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005445 win_T *wp;
5446 int wnr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005447# ifdef FEAT_GUI
5448 need_mouse_correct = TRUE;
5449# endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005450 if (eap->addr_count > 0)
5451 {
5452 wnr = eap->line2;
5453 for (wp = firstwin; --wnr > 0; )
5454 {
5455 if (wp->w_next == NULL)
5456 break;
5457 else
5458 wp = wp->w_next;
5459 }
5460 win_goto(wp);
5461 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005462 close_others(TRUE, eap->forceit);
5463}
5464
Bram Moolenaar071d4272004-06-13 20:20:40 +00005465 static void
Bram Moolenaar5e1e6d22017-01-02 17:26:00 +01005466ex_hide(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005467{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005468 // ":hide" or ":hide | cmd": hide current window
Bram Moolenaar2256c992016-11-15 21:17:07 +01005469 if (!eap->skip)
5470 {
Bram Moolenaar4033c552017-09-16 20:54:51 +02005471#ifdef FEAT_GUI
Bram Moolenaar2256c992016-11-15 21:17:07 +01005472 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005473#endif
Bram Moolenaar2256c992016-11-15 21:17:07 +01005474 if (eap->addr_count == 0)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005475 win_close(curwin, FALSE); // don't free buffer
Bram Moolenaar2256c992016-11-15 21:17:07 +01005476 else
5477 {
5478 int winnr = 0;
5479 win_T *win;
Bram Moolenaarf240e182014-11-27 18:33:02 +01005480
Bram Moolenaar2256c992016-11-15 21:17:07 +01005481 FOR_ALL_WINDOWS(win)
5482 {
5483 winnr++;
5484 if (winnr == eap->line2)
5485 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005486 }
Bram Moolenaar2256c992016-11-15 21:17:07 +01005487 if (win == NULL)
5488 win = lastwin;
5489 win_close(win, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005490 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005491 }
5492}
5493
5494/*
5495 * ":stop" and ":suspend": Suspend Vim.
5496 */
5497 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005498ex_stop(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005499{
5500 /*
5501 * Disallow suspending for "rvim".
5502 */
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005503 if (!check_restricted())
Bram Moolenaar071d4272004-06-13 20:20:40 +00005504 {
5505 if (!eap->forceit)
5506 autowrite_all();
5507 windgoto((int)Rows - 1, 0);
5508 out_char('\n');
5509 out_flush();
5510 stoptermcap();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005511 out_flush(); // needed for SUN to restore xterm buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00005512#ifdef FEAT_TITLE
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005513 mch_restore_title(SAVE_RESTORE_BOTH); // restore window titles
Bram Moolenaar071d4272004-06-13 20:20:40 +00005514#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005515 ui_suspend(); // call machine specific function
Bram Moolenaar071d4272004-06-13 20:20:40 +00005516#ifdef FEAT_TITLE
5517 maketitle();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005518 resettitle(); // force updating the title
Bram Moolenaar071d4272004-06-13 20:20:40 +00005519#endif
5520 starttermcap();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005521 scroll_start(); // scroll screen before redrawing
Bram Moolenaar071d4272004-06-13 20:20:40 +00005522 redraw_later_clear();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005523 shell_resized(); // may have resized window
Bram Moolenaar071d4272004-06-13 20:20:40 +00005524 }
5525}
5526
5527/*
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005528 * ":exit", ":xit" and ":wq": Write file and quite the current window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005529 */
5530 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005531ex_exit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005532{
5533#ifdef FEAT_CMDWIN
5534 if (cmdwin_type != 0)
5535 {
5536 cmdwin_result = Ctrl_C;
5537 return;
5538 }
5539#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005540 // Don't quit while editing the command line.
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005541 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005542 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005543 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005544 return;
5545 }
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005546
5547 if (before_quit_autocmds(curwin, FALSE, eap->forceit))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005548 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005549
5550 /*
5551 * if more files or windows we won't exit
5552 */
5553 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
5554 exiting = TRUE;
5555 if ( ((eap->cmdidx == CMD_wq
5556 || curbufIsChanged())
5557 && do_write(eap) == FAIL)
5558 || check_more(TRUE, eap->forceit) == FAIL
Bram Moolenaar027387f2016-01-02 22:25:52 +01005559 || (only_one_window() && check_changed_any(eap->forceit, FALSE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005560 {
5561 not_exiting();
5562 }
5563 else
5564 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005565 if (only_one_window()) // quit last window, exit Vim
Bram Moolenaar071d4272004-06-13 20:20:40 +00005566 getout(0);
Bram Moolenaar2c33d7b2017-10-14 16:06:20 +02005567 not_exiting();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005568# ifdef FEAT_GUI
5569 need_mouse_correct = TRUE;
5570# endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005571 // Quit current window, may free the buffer.
Bram Moolenaareb44a682017-08-03 22:44:55 +02005572 win_close(curwin, !buf_hide(curwin->w_buffer));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005573 }
5574}
5575
5576/*
5577 * ":print", ":list", ":number".
5578 */
5579 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005580ex_print(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005581{
Bram Moolenaardf177f62005-02-22 08:39:57 +00005582 if (curbuf->b_ml.ml_flags & ML_EMPTY)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005583 emsg(_(e_emptybuf));
Bram Moolenaardf177f62005-02-22 08:39:57 +00005584 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005585 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00005586 for ( ;!got_int; ui_breakcheck())
5587 {
5588 print_line(eap->line1,
5589 (eap->cmdidx == CMD_number || eap->cmdidx == CMD_pound
5590 || (eap->flags & EXFLAG_NR)),
5591 eap->cmdidx == CMD_list || (eap->flags & EXFLAG_LIST));
5592 if (++eap->line1 > eap->line2)
5593 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005594 out_flush(); // show one line at a time
Bram Moolenaardf177f62005-02-22 08:39:57 +00005595 }
5596 setpcmark();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005597 // put cursor at last line
Bram Moolenaardf177f62005-02-22 08:39:57 +00005598 curwin->w_cursor.lnum = eap->line2;
5599 beginline(BL_SOL | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005600 }
5601
Bram Moolenaar071d4272004-06-13 20:20:40 +00005602 ex_no_reprint = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005603}
5604
5605#ifdef FEAT_BYTEOFF
5606 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005607ex_goto(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005608{
5609 goto_byte(eap->line2);
5610}
5611#endif
5612
5613/*
5614 * ":shell".
5615 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005616 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005617ex_shell(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005618{
5619 do_shell(NULL, 0);
5620}
5621
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005622#if defined(HAVE_DROP_FILE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005623
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005624static int drop_busy = FALSE;
5625static int drop_filec;
5626static char_u **drop_filev = NULL;
5627static int drop_split;
5628static void (*drop_callback)(void *);
5629static void *drop_cookie;
5630
5631 static void
5632handle_drop_internal(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005633{
5634 exarg_T ea;
5635 int save_msg_scroll = msg_scroll;
5636
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005637 // Setting the argument list may cause screen updates and being called
5638 // recursively. Avoid that by setting drop_busy.
5639 drop_busy = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005640
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005641 // Check whether the current buffer is changed. If so, we will need
5642 // to split the current window or data could be lost.
5643 // We don't need to check if the 'hidden' option is set, as in this
5644 // case the buffer won't be lost.
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005645 if (!buf_hide(curbuf) && !drop_split)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005646 {
5647 ++emsg_off;
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005648 drop_split = check_changed(curbuf, CCGD_AW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005649 --emsg_off;
5650 }
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005651 if (drop_split)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005652 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005653 if (win_split(0, 0) == FAIL)
5654 return;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02005655 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005656
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005657 // When splitting the window, create a new alist. Otherwise the
5658 // existing one is overwritten.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005659 alist_unlink(curwin->w_alist);
5660 alist_new();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005661 }
5662
5663 /*
5664 * Set up the new argument list.
5665 */
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005666 alist_set(ALIST(curwin), drop_filec, drop_filev, FALSE, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005667
5668 /*
5669 * Move to the first file.
5670 */
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005671 // Fake up a minimal "next" command for do_argfile()
Bram Moolenaara80faa82020-04-12 19:37:17 +02005672 CLEAR_FIELD(ea);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005673 ea.cmd = (char_u *)"next";
5674 do_argfile(&ea, 0);
5675
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005676 // do_ecmd() may set need_start_insertmode, but since we never left Insert
5677 // mode that is not needed here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005678 need_start_insertmode = FALSE;
5679
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005680 // Restore msg_scroll, otherwise a following command may cause scrolling
5681 // unexpectedly. The screen will be redrawn by the caller, thus
5682 // msg_scroll being set by displaying a message is irrelevant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005683 msg_scroll = save_msg_scroll;
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005684
5685 if (drop_callback != NULL)
5686 drop_callback(drop_cookie);
5687
5688 drop_filev = NULL;
5689 drop_busy = FALSE;
5690}
5691
5692/*
5693 * Handle a file drop. The code is here because a drop is *nearly* like an
5694 * :args command, but not quite (we have a list of exact filenames, so we
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01005695 * don't want to (a) parse a command line, or (b) expand wildcards). So the
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005696 * code is very similar to :args and hence needs access to a lot of the static
5697 * functions in this file.
5698 *
5699 * The "filev" list must have been allocated using alloc(), as should each item
5700 * in the list. This function takes over responsibility for freeing the "filev"
5701 * list.
5702 */
5703 void
5704handle_drop(
5705 int filec, // the number of files dropped
5706 char_u **filev, // the list of files dropped
5707 int split, // force splitting the window
5708 void (*callback)(void *), // to be called after setting the argument
5709 // list
5710 void *cookie) // argument for "callback" (allocated)
5711{
5712 // Cannot handle recursive drops, finish the pending one.
5713 if (drop_busy)
5714 {
5715 FreeWild(filec, filev);
5716 vim_free(cookie);
5717 return;
5718 }
5719
5720 // When calling handle_drop() more than once in a row we only use the last
5721 // one.
5722 if (drop_filev != NULL)
5723 {
5724 FreeWild(drop_filec, drop_filev);
5725 vim_free(drop_cookie);
5726 }
5727
5728 drop_filec = filec;
5729 drop_filev = filev;
5730 drop_split = split;
5731 drop_callback = callback;
5732 drop_cookie = cookie;
5733
5734 // Postpone this when:
5735 // - editing the command line
5736 // - not possible to change the current buffer
5737 // - updating the screen
5738 // As it may change buffers and window structures that are in use and cause
5739 // freed memory to be used.
5740 if (text_locked() || curbuf_locked() || updating_screen)
5741 return;
5742
5743 handle_drop_internal();
5744}
5745
5746/*
5747 * To be called when text is unlocked, curbuf is unlocked or updating_screen is
5748 * reset: Handle a postponed drop.
5749 */
5750 void
5751handle_any_postponed_drop(void)
5752{
5753 if (!drop_busy && drop_filev != NULL
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +02005754 && !text_locked() && !curbuf_locked() && !updating_screen)
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005755 handle_drop_internal();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005756}
5757#endif
5758
Bram Moolenaar071d4272004-06-13 20:20:40 +00005759/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005760 * ":preserve".
5761 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005762 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005763ex_preserve(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005764{
Bram Moolenaar4399ef42005-02-12 14:29:27 +00005765 curbuf->b_flags |= BF_PRESERVED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005766 ml_preserve(curbuf, TRUE);
5767}
5768
5769/*
5770 * ":recover".
5771 */
5772 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005773ex_recover(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005774{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005775 // Set recoverymode right away to avoid the ATTENTION prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005776 recoverymode = TRUE;
Bram Moolenaar45d3b142013-11-09 03:31:51 +01005777 if (!check_changed(curbuf, (p_awa ? CCGD_AW : 0)
5778 | CCGD_MULTWIN
5779 | (eap->forceit ? CCGD_FORCEIT : 0)
5780 | CCGD_EXCMD)
5781
Bram Moolenaar071d4272004-06-13 20:20:40 +00005782 && (*eap->arg == NUL
5783 || setfname(curbuf, eap->arg, NULL, TRUE) == OK))
Bram Moolenaar99499b12019-05-23 21:35:48 +02005784 ml_recover(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005785 recoverymode = FALSE;
5786}
5787
5788/*
5789 * Command modifier used in a wrong way.
5790 */
5791 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005792ex_wrongmodifier(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005793{
5794 eap->errmsg = e_invcmd;
5795}
5796
Bram Moolenaar071d4272004-06-13 20:20:40 +00005797/*
5798 * :sview [+command] file split window with new file, read-only
5799 * :split [[+command] file] split window with current or new file
5800 * :vsplit [[+command] file] split window vertically with current or new file
5801 * :new [[+command] file] split window with no or new file
5802 * :vnew [[+command] file] split vertically window with no or new file
5803 * :sfind [+command] file split window with file in 'path'
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005804 *
5805 * :tabedit open new Tab page with empty window
5806 * :tabedit [+command] file open new Tab page and edit "file"
5807 * :tabnew [[+command] file] just like :tabedit
5808 * :tabfind [+command] file open new Tab page and find "file"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005809 */
5810 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005811ex_splitview(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005812{
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005813 win_T *old_curwin = curwin;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005814#if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005815 char_u *fname = NULL;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005816#endif
5817#ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00005818 int browse_flag = cmdmod.browse;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005819#endif
Bram Moolenaar39902a02018-06-21 22:10:08 +02005820 int use_tab = eap->cmdidx == CMD_tabedit
5821 || eap->cmdidx == CMD_tabfind
5822 || eap->cmdidx == CMD_tabnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005823
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01005824 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02005825 return;
5826
Bram Moolenaar4033c552017-09-16 20:54:51 +02005827#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00005828 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005829#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005830
Bram Moolenaar4033c552017-09-16 20:54:51 +02005831#ifdef FEAT_QUICKFIX
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005832 // A ":split" in the quickfix window works like ":new". Don't want two
5833 // quickfix windows. But it's OK when doing ":tab split".
Bram Moolenaar05bb9532008-07-04 09:44:11 +00005834 if (bt_quickfix(curbuf) && cmdmod.tab == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005835 {
5836 if (eap->cmdidx == CMD_split)
5837 eap->cmdidx = CMD_new;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005838 if (eap->cmdidx == CMD_vsplit)
5839 eap->cmdidx = CMD_vnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005840 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02005841#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005842
Bram Moolenaar4033c552017-09-16 20:54:51 +02005843#ifdef FEAT_SEARCHPATH
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005844 if (eap->cmdidx == CMD_sfind || eap->cmdidx == CMD_tabfind)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005845 {
5846 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg),
5847 FNAME_MESS, TRUE, curbuf->b_ffname);
5848 if (fname == NULL)
5849 goto theend;
5850 eap->arg = fname;
5851 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005852# ifdef FEAT_BROWSE
Bram Moolenaar4033c552017-09-16 20:54:51 +02005853 else
5854# endif
5855#endif
5856#ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00005857 if (cmdmod.browse
Bram Moolenaar071d4272004-06-13 20:20:40 +00005858 && eap->cmdidx != CMD_vnew
Bram Moolenaar071d4272004-06-13 20:20:40 +00005859 && eap->cmdidx != CMD_new)
5860 {
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00005861 if (
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005862# ifdef FEAT_GUI
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00005863 !gui.in_use &&
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005864# endif
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00005865 au_has_group((char_u *)"FileExplorer"))
5866 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005867 // No browsing supported but we do have the file explorer:
5868 // Edit the directory.
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00005869 if (*eap->arg == NUL || !mch_isdir(eap->arg))
5870 eap->arg = (char_u *)".";
5871 }
5872 else
5873 {
Bram Moolenaar39902a02018-06-21 22:10:08 +02005874 fname = do_browse(0, (char_u *)(use_tab
5875 ? _("Edit File in new tab page")
5876 : _("Edit File in new window")),
Bram Moolenaar071d4272004-06-13 20:20:40 +00005877 eap->arg, NULL, NULL, NULL, curbuf);
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00005878 if (fname == NULL)
5879 goto theend;
5880 eap->arg = fname;
5881 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005882 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005883 cmdmod.browse = FALSE; // Don't browse again in do_ecmd().
Bram Moolenaar4033c552017-09-16 20:54:51 +02005884#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005885
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005886 /*
5887 * Either open new tab page or split the window.
5888 */
Bram Moolenaar39902a02018-06-21 22:10:08 +02005889 if (use_tab)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005890 {
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005891 if (win_new_tabpage(cmdmod.tab != 0 ? cmdmod.tab
5892 : eap->addr_count == 0 ? 0
5893 : (int)eap->line2 + 1) != FAIL)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005894 {
Bram Moolenaard2b66012008-01-09 19:30:36 +00005895 do_exedit(eap, old_curwin);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005896
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005897 // set the alternate buffer for the window we came from
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005898 if (curwin != old_curwin
5899 && win_valid(old_curwin)
5900 && old_curwin->w_buffer != curbuf
5901 && !cmdmod.keepalt)
5902 old_curwin->w_alt_fnum = curbuf->b_fnum;
5903 }
5904 }
5905 else if (win_split(eap->addr_count > 0 ? (int)eap->line2 : 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005906 *eap->cmd == 'v' ? WSP_VERT : 0) != FAIL)
5907 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005908 // Reset 'scrollbind' when editing another file, but keep it when
5909 // doing ":split" without arguments.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005910 if (*eap->arg != NUL
Bram Moolenaar8a3bb562018-03-04 20:14:14 +01005911# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00005912 || cmdmod.browse
Bram Moolenaar8a3bb562018-03-04 20:14:14 +01005913# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005914 )
Bram Moolenaar3368ea22010-09-21 16:56:35 +02005915 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005916 else
5917 do_check_scrollbind(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005918 do_exedit(eap, old_curwin);
5919 }
5920
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005921# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00005922 cmdmod.browse = browse_flag;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005923# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005924
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005925# if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005926theend:
5927 vim_free(fname);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005928# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005929}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005930
5931/*
Bram Moolenaar80a94a52006-02-23 21:26:58 +00005932 * Open a new tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005933 */
5934 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005935tabpage_new(void)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00005936{
5937 exarg_T ea;
5938
Bram Moolenaara80faa82020-04-12 19:37:17 +02005939 CLEAR_FIELD(ea);
Bram Moolenaar80a94a52006-02-23 21:26:58 +00005940 ea.cmdidx = CMD_tabnew;
5941 ea.cmd = (char_u *)"tabn";
5942 ea.arg = (char_u *)"";
5943 ex_splitview(&ea);
5944}
5945
5946/*
5947 * :tabnext command
5948 */
5949 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005950ex_tabnext(exarg_T *eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005951{
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005952 int tab_number;
5953
Bram Moolenaar8cdbd5b2019-06-16 15:50:45 +02005954 if (ERROR_IF_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02005955 return;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005956 switch (eap->cmdidx)
5957 {
5958 case CMD_tabfirst:
5959 case CMD_tabrewind:
5960 goto_tabpage(1);
5961 break;
5962 case CMD_tablast:
5963 goto_tabpage(9999);
5964 break;
5965 case CMD_tabprevious:
5966 case CMD_tabNext:
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005967 if (eap->arg && *eap->arg != NUL)
5968 {
5969 char_u *p = eap->arg;
5970 char_u *p_save = p;
5971
5972 tab_number = getdigits(&p);
5973 if (p == p_save || *p_save == '-' || *p != NUL
5974 || tab_number == 0)
5975 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005976 // No numbers as argument.
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005977 eap->errmsg = e_invarg;
5978 return;
5979 }
5980 }
5981 else
5982 {
5983 if (eap->addr_count == 0)
5984 tab_number = 1;
5985 else
5986 {
5987 tab_number = eap->line2;
5988 if (tab_number < 1)
5989 {
5990 eap->errmsg = e_invrange;
5991 return;
5992 }
5993 }
5994 }
5995 goto_tabpage(-tab_number);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005996 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005997 default: // CMD_tabnext
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005998 tab_number = get_tabpage_arg(eap);
5999 if (eap->errmsg == NULL)
6000 goto_tabpage(tab_number);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006001 break;
6002 }
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006003}
6004
6005/*
6006 * :tabmove command
6007 */
6008 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006009ex_tabmove(exarg_T *eap)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006010{
Bram Moolenaar40ce3a42015-04-21 18:08:39 +02006011 int tab_number;
Bram Moolenaar8cb8dca2012-07-06 18:27:39 +02006012
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006013 tab_number = get_tabpage_arg(eap);
6014 if (eap->errmsg == NULL)
6015 tabpage_move(tab_number);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006016}
6017
6018/*
6019 * :tabs command: List tabs and their contents.
6020 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006021 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006022ex_tabs(exarg_T *eap UNUSED)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006023{
6024 tabpage_T *tp;
6025 win_T *wp;
6026 int tabcount = 1;
6027
6028 msg_start();
6029 msg_scroll = TRUE;
6030 for (tp = first_tabpage; tp != NULL && !got_int; tp = tp->tp_next)
6031 {
6032 msg_putchar('\n');
6033 vim_snprintf((char *)IObuff, IOSIZE, _("Tab page %d"), tabcount++);
Bram Moolenaar8820b482017-03-16 17:23:31 +01006034 msg_outtrans_attr(IObuff, HL_ATTR(HLF_T));
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006035 out_flush(); // output one line at a time
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006036 ui_breakcheck();
6037
Bram Moolenaar030f0df2006-02-21 22:02:53 +00006038 if (tp == curtab)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006039 wp = firstwin;
6040 else
6041 wp = tp->tp_firstwin;
6042 for ( ; wp != NULL && !got_int; wp = wp->w_next)
6043 {
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006044 msg_putchar('\n');
6045 msg_putchar(wp == curwin ? '>' : ' ');
6046 msg_putchar(' ');
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006047 msg_putchar(bufIsChanged(wp->w_buffer) ? '+' : ' ');
6048 msg_putchar(' ');
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006049 if (buf_spname(wp->w_buffer) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02006050 vim_strncpy(IObuff, buf_spname(wp->w_buffer), IOSIZE - 1);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006051 else
6052 home_replace(wp->w_buffer, wp->w_buffer->b_fname,
6053 IObuff, IOSIZE, TRUE);
6054 msg_outtrans(IObuff);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006055 out_flush(); // output one line at a time
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006056 ui_breakcheck();
6057 }
6058 }
6059}
6060
Bram Moolenaar071d4272004-06-13 20:20:40 +00006061/*
6062 * ":mode": Set screen mode.
6063 * If no argument given, just get the screen size and redraw.
6064 */
6065 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006066ex_mode(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006067{
6068 if (*eap->arg == NUL)
6069 shell_resized();
6070 else
Bram Moolenaar3c71aec2020-01-17 19:32:20 +01006071 emsg(_(e_screenmode));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006072}
6073
Bram Moolenaar071d4272004-06-13 20:20:40 +00006074/*
6075 * ":resize".
6076 * set, increment or decrement current window height
6077 */
6078 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006079ex_resize(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006080{
6081 int n;
6082 win_T *wp = curwin;
6083
6084 if (eap->addr_count > 0)
6085 {
6086 n = eap->line2;
6087 for (wp = firstwin; wp->w_next != NULL && --n > 0; wp = wp->w_next)
6088 ;
6089 }
6090
Bram Moolenaar44a2f922016-03-19 22:11:51 +01006091# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00006092 need_mouse_correct = TRUE;
Bram Moolenaar44a2f922016-03-19 22:11:51 +01006093# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006094 n = atol((char *)eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006095 if (cmdmod.split & WSP_VERT)
6096 {
6097 if (*eap->arg == '-' || *eap->arg == '+')
Bram Moolenaar02631462017-09-22 15:20:32 +02006098 n += curwin->w_width;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006099 else if (n == 0 && eap->arg[0] == NUL) // default is very wide
Bram Moolenaar071d4272004-06-13 20:20:40 +00006100 n = 9999;
6101 win_setwidth_win((int)n, wp);
6102 }
6103 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006104 {
6105 if (*eap->arg == '-' || *eap->arg == '+')
6106 n += curwin->w_height;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006107 else if (n == 0 && eap->arg[0] == NUL) // default is very high
Bram Moolenaar071d4272004-06-13 20:20:40 +00006108 n = 9999;
6109 win_setheight_win((int)n, wp);
6110 }
6111}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006112
6113/*
6114 * ":find [+command] <file>" command.
6115 */
6116 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006117ex_find(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006118{
6119#ifdef FEAT_SEARCHPATH
6120 char_u *fname;
6121 int count;
6122
6123 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg), FNAME_MESS,
6124 TRUE, curbuf->b_ffname);
6125 if (eap->addr_count > 0)
6126 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006127 // Repeat finding the file "count" times. This matters when it
6128 // appears several times in the path.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006129 count = eap->line2;
6130 while (fname != NULL && --count > 0)
6131 {
6132 vim_free(fname);
6133 fname = find_file_in_path(NULL, 0, FNAME_MESS,
6134 FALSE, curbuf->b_ffname);
6135 }
6136 }
6137
6138 if (fname != NULL)
6139 {
6140 eap->arg = fname;
6141#endif
6142 do_exedit(eap, NULL);
6143#ifdef FEAT_SEARCHPATH
6144 vim_free(fname);
6145 }
6146#endif
6147}
6148
6149/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00006150 * ":open" simulation: for now just work like ":visual".
6151 */
6152 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006153ex_open(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00006154{
6155 regmatch_T regmatch;
6156 char_u *p;
6157
6158 curwin->w_cursor.lnum = eap->line2;
6159 beginline(BL_SOL | BL_FIX);
6160 if (*eap->arg == '/')
6161 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006162 // ":open /pattern/": put cursor in column found with pattern
Bram Moolenaardf177f62005-02-22 08:39:57 +00006163 ++eap->arg;
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006164 p = skip_regexp(eap->arg, '/', p_magic);
Bram Moolenaardf177f62005-02-22 08:39:57 +00006165 *p = NUL;
6166 regmatch.regprog = vim_regcomp(eap->arg, p_magic ? RE_MAGIC : 0);
6167 if (regmatch.regprog != NULL)
6168 {
6169 regmatch.rm_ic = p_ic;
6170 p = ml_get_curline();
6171 if (vim_regexec(&regmatch, p, (colnr_T)0))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006172 curwin->w_cursor.col = (colnr_T)(regmatch.startp[0] - p);
Bram Moolenaardf177f62005-02-22 08:39:57 +00006173 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006174 emsg(_(e_nomatch));
Bram Moolenaar473de612013-06-08 18:19:48 +02006175 vim_regfree(regmatch.regprog);
Bram Moolenaardf177f62005-02-22 08:39:57 +00006176 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006177 // Move to the NUL, ignore any other arguments.
Bram Moolenaardf177f62005-02-22 08:39:57 +00006178 eap->arg += STRLEN(eap->arg);
6179 }
6180 check_cursor();
6181
6182 eap->cmdidx = CMD_visual;
6183 do_exedit(eap, NULL);
6184}
6185
6186/*
6187 * ":edit", ":badd", ":visual".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006188 */
6189 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006190ex_edit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006191{
6192 do_exedit(eap, NULL);
6193}
6194
6195/*
Bram Moolenaar4b96df52020-01-26 22:00:26 +01006196 * ":edit <file>" command and alike.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006197 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006198 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006199do_exedit(
6200 exarg_T *eap,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006201 win_T *old_curwin) // curwin before doing a split or NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006202{
6203 int n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006204 int need_hide;
Bram Moolenaardf177f62005-02-22 08:39:57 +00006205 int exmode_was = exmode_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006206
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01006207 if ((eap->cmdidx != CMD_pedit && ERROR_IF_POPUP_WINDOW)
6208 || ERROR_IF_TERM_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02006209 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006210 /*
6211 * ":vi" command ends Ex mode.
6212 */
6213 if (exmode_active && (eap->cmdidx == CMD_visual
6214 || eap->cmdidx == CMD_view))
6215 {
6216 exmode_active = FALSE;
6217 if (*eap->arg == NUL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00006218 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006219 // Special case: ":global/pat/visual\NLvi-commands"
Bram Moolenaardf177f62005-02-22 08:39:57 +00006220 if (global_busy)
6221 {
6222 int rd = RedrawingDisabled;
6223 int nwr = no_wait_return;
6224 int ms = msg_scroll;
6225#ifdef FEAT_GUI
6226 int he = hold_gui_events;
6227#endif
6228
6229 if (eap->nextcmd != NULL)
6230 {
6231 stuffReadbuff(eap->nextcmd);
6232 eap->nextcmd = NULL;
6233 }
6234
6235 if (exmode_was != EXMODE_VIM)
6236 settmode(TMODE_RAW);
6237 RedrawingDisabled = 0;
6238 no_wait_return = 0;
6239 need_wait_return = FALSE;
6240 msg_scroll = 0;
6241#ifdef FEAT_GUI
6242 hold_gui_events = 0;
6243#endif
6244 must_redraw = CLEAR;
Bram Moolenaar9e2bcb52020-02-18 21:33:00 +01006245 pending_exmode_active = TRUE;
Bram Moolenaardf177f62005-02-22 08:39:57 +00006246
6247 main_loop(FALSE, TRUE);
6248
Bram Moolenaar9e2bcb52020-02-18 21:33:00 +01006249 pending_exmode_active = FALSE;
Bram Moolenaardf177f62005-02-22 08:39:57 +00006250 RedrawingDisabled = rd;
6251 no_wait_return = nwr;
6252 msg_scroll = ms;
6253#ifdef FEAT_GUI
6254 hold_gui_events = he;
6255#endif
6256 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006257 return;
Bram Moolenaardf177f62005-02-22 08:39:57 +00006258 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006259 }
6260
6261 if ((eap->cmdidx == CMD_new
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006262 || eap->cmdidx == CMD_tabnew
6263 || eap->cmdidx == CMD_tabedit
Bram Moolenaar4033c552017-09-16 20:54:51 +02006264 || eap->cmdidx == CMD_vnew) && *eap->arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006265 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006266 // ":new" or ":tabnew" without argument: edit an new empty buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00006267 setpcmark();
6268 (void)do_ecmd(0, NULL, NULL, eap, ECMD_ONE,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00006269 ECMD_HIDE + (eap->forceit ? ECMD_FORCEIT : 0),
6270 old_curwin == NULL ? curwin : NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006271 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02006272 else if ((eap->cmdidx != CMD_split && eap->cmdidx != CMD_vsplit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006273 || *eap->arg != NUL
6274#ifdef FEAT_BROWSE
6275 || cmdmod.browse
6276#endif
6277 )
6278 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006279 // Can't edit another file when "curbuf_lock" is set. Only ":edit"
6280 // can bring us here, others are stopped earlier.
Bram Moolenaar5555acc2006-04-07 21:33:12 +00006281 if (*eap->arg != NUL && curbuf_locked())
6282 return;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006283
Bram Moolenaar071d4272004-06-13 20:20:40 +00006284 n = readonlymode;
6285 if (eap->cmdidx == CMD_view || eap->cmdidx == CMD_sview)
6286 readonlymode = TRUE;
6287 else if (eap->cmdidx == CMD_enew)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006288 readonlymode = FALSE; // 'readonly' doesn't make sense in an
6289 // empty buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00006290 setpcmark();
6291 if (do_ecmd(0, (eap->cmdidx == CMD_enew ? NULL : eap->arg),
6292 NULL, eap,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006293 // ":edit" goes to first line if Vi compatible
Bram Moolenaar071d4272004-06-13 20:20:40 +00006294 (*eap->arg == NUL && eap->do_ecmd_lnum == 0
6295 && vim_strchr(p_cpo, CPO_GOTO1) != NULL)
6296 ? ECMD_ONE : eap->do_ecmd_lnum,
Bram Moolenaareb44a682017-08-03 22:44:55 +02006297 (buf_hide(curbuf) ? ECMD_HIDE : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006298 + (eap->forceit ? ECMD_FORCEIT : 0)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006299 // after a split we can use an existing buffer
Bram Moolenaar7b449342014-03-25 13:03:48 +01006300 + (old_curwin != NULL ? ECMD_OLDBUF : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006301 + (eap->cmdidx == CMD_badd ? ECMD_ADDBUF : 0 )
Bram Moolenaar701f7af2008-11-15 13:12:07 +00006302 , old_curwin == NULL ? curwin : NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006303 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006304 // Editing the file failed. If the window was split, close it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006305 if (old_curwin != NULL)
6306 {
6307 need_hide = (curbufIsChanged() && curbuf->b_nwindows <= 1);
Bram Moolenaareb44a682017-08-03 22:44:55 +02006308 if (!need_hide || buf_hide(curbuf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006309 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006310#if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00006311 cleanup_T cs;
6312
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006313 // Reset the error/interrupt/exception state here so that
6314 // aborting() returns FALSE when closing a window.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00006315 enter_cleanup(&cs);
Bram Moolenaar4033c552017-09-16 20:54:51 +02006316#endif
6317#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00006318 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006319#endif
Bram Moolenaareb44a682017-08-03 22:44:55 +02006320 win_close(curwin, !need_hide && !buf_hide(curbuf));
Bram Moolenaarc0197e22004-09-13 20:26:32 +00006321
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006322#if defined(FEAT_EVAL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006323 // Restore the error/interrupt/exception state if not
6324 // discarded by a new aborting error, interrupt, or
6325 // uncaught exception.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00006326 leave_cleanup(&cs);
Bram Moolenaar4033c552017-09-16 20:54:51 +02006327#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006328 }
6329 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006330 }
6331 else if (readonlymode && curbuf->b_nwindows == 1)
6332 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006333 // When editing an already visited buffer, 'readonly' won't be set
6334 // but the previous value is kept. With ":view" and ":sview" we
6335 // want the file to be readonly, except when another window is
6336 // editing the same buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006337 curbuf->b_p_ro = TRUE;
6338 }
6339 readonlymode = n;
6340 }
6341 else
6342 {
6343 if (eap->do_ecmd_cmd != NULL)
6344 do_cmdline_cmd(eap->do_ecmd_cmd);
6345#ifdef FEAT_TITLE
6346 n = curwin->w_arg_idx_invalid;
6347#endif
6348 check_arg_idx(curwin);
6349#ifdef FEAT_TITLE
6350 if (n != curwin->w_arg_idx_invalid)
6351 maketitle();
6352#endif
6353 }
6354
Bram Moolenaar071d4272004-06-13 20:20:40 +00006355 /*
6356 * if ":split file" worked, set alternate file name in old window to new
6357 * file
6358 */
6359 if (old_curwin != NULL
6360 && *eap->arg != NUL
6361 && curwin != old_curwin
6362 && win_valid(old_curwin)
Bram Moolenaard4755bb2004-09-02 19:12:26 +00006363 && old_curwin->w_buffer != curbuf
6364 && !cmdmod.keepalt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006365 old_curwin->w_alt_fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006366
6367 ex_no_reprint = TRUE;
6368}
6369
6370#ifndef FEAT_GUI
6371/*
6372 * ":gui" and ":gvim" when there is no GUI.
6373 */
6374 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006375ex_nogui(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006376{
6377 eap->errmsg = e_nogvim;
6378}
6379#endif
6380
Bram Moolenaar4f974752019-02-17 17:44:42 +01006381#if defined(FEAT_GUI_MSWIN) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006382 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006383ex_tearoff(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006384{
6385 gui_make_tearoff(eap->arg);
6386}
6387#endif
6388
Bram Moolenaar29a2c082018-03-05 21:06:23 +01006389#if (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK) \
6390 || defined(FEAT_TERM_POPUP_MENU)) && defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006391 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006392ex_popup(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006393{
Bram Moolenaar29a2c082018-03-05 21:06:23 +01006394# if defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK)
6395 if (gui.in_use)
6396 gui_make_popup(eap->arg, eap->forceit);
6397# ifdef FEAT_TERM_POPUP_MENU
6398 else
6399# endif
6400# endif
6401# ifdef FEAT_TERM_POPUP_MENU
6402 pum_make_popup(eap->arg, eap->forceit);
6403# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006404}
6405#endif
6406
Bram Moolenaar071d4272004-06-13 20:20:40 +00006407 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006408ex_swapname(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006409{
6410 if (curbuf->b_ml.ml_mfp == NULL || curbuf->b_ml.ml_mfp->mf_fname == NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01006411 msg(_("No swap file"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006412 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01006413 msg((char *)curbuf->b_ml.ml_mfp->mf_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006414}
6415
6416/*
6417 * ":syncbind" forces all 'scrollbind' windows to have the same relative
6418 * offset.
6419 * (1998-11-02 16:21:01 R. Edward Ralston <eralston@computer.org>)
6420 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006421 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006422ex_syncbind(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006423{
Bram Moolenaar071d4272004-06-13 20:20:40 +00006424 win_T *wp;
Bram Moolenaardedd1b02013-12-14 13:06:17 +01006425 win_T *save_curwin = curwin;
6426 buf_T *save_curbuf = curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006427 long topline;
6428 long y;
6429 linenr_T old_linenr = curwin->w_cursor.lnum;
6430
6431 setpcmark();
6432
6433 /*
6434 * determine max topline
6435 */
6436 if (curwin->w_p_scb)
6437 {
6438 topline = curwin->w_topline;
Bram Moolenaar29323592016-07-24 22:04:11 +02006439 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006440 {
6441 if (wp->w_p_scb && wp->w_buffer)
6442 {
Bram Moolenaar375e3392019-01-31 18:26:10 +01006443 y = wp->w_buffer->b_ml.ml_line_count - get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006444 if (topline > y)
6445 topline = y;
6446 }
6447 }
6448 if (topline < 1)
6449 topline = 1;
6450 }
6451 else
6452 {
6453 topline = 1;
6454 }
6455
6456
6457 /*
Bram Moolenaardedd1b02013-12-14 13:06:17 +01006458 * Set all scrollbind windows to the same topline.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006459 */
Bram Moolenaar29323592016-07-24 22:04:11 +02006460 FOR_ALL_WINDOWS(curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006461 {
6462 if (curwin->w_p_scb)
6463 {
Bram Moolenaardedd1b02013-12-14 13:06:17 +01006464 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006465 y = topline - curwin->w_topline;
6466 if (y > 0)
6467 scrollup(y, TRUE);
6468 else
6469 scrolldown(-y, TRUE);
6470 curwin->w_scbind_pos = topline;
6471 redraw_later(VALID);
6472 cursor_correct();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006473 curwin->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006474 }
6475 }
Bram Moolenaardedd1b02013-12-14 13:06:17 +01006476 curwin = save_curwin;
6477 curbuf = save_curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006478 if (curwin->w_p_scb)
6479 {
6480 did_syncbind = TRUE;
6481 checkpcmark();
6482 if (old_linenr != curwin->w_cursor.lnum)
6483 {
6484 char_u ctrl_o[2];
6485
6486 ctrl_o[0] = Ctrl_O;
6487 ctrl_o[1] = 0;
6488 ins_typebuf(ctrl_o, REMAP_NONE, 0, TRUE, FALSE);
6489 }
6490 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006491}
6492
6493
6494 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006495ex_read(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006496{
Bram Moolenaardf177f62005-02-22 08:39:57 +00006497 int i;
6498 int empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
6499 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006500
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006501 if (eap->usefilter) // :r!cmd
Bram Moolenaar071d4272004-06-13 20:20:40 +00006502 do_bang(1, eap, FALSE, FALSE, TRUE);
6503 else
6504 {
6505 if (u_save(eap->line2, (linenr_T)(eap->line2 + 1)) == FAIL)
6506 return;
6507
6508#ifdef FEAT_BROWSE
6509 if (cmdmod.browse)
6510 {
6511 char_u *browseFile;
6512
Bram Moolenaar7171abe2004-10-11 10:06:20 +00006513 browseFile = do_browse(0, (char_u *)_("Append File"), eap->arg,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006514 NULL, NULL, NULL, curbuf);
6515 if (browseFile != NULL)
6516 {
6517 i = readfile(browseFile, NULL,
6518 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
6519 vim_free(browseFile);
6520 }
6521 else
6522 i = OK;
6523 }
6524 else
6525#endif
6526 if (*eap->arg == NUL)
6527 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006528 if (check_fname() == FAIL) // check for no file name
Bram Moolenaar071d4272004-06-13 20:20:40 +00006529 return;
6530 i = readfile(curbuf->b_ffname, curbuf->b_fname,
6531 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
6532 }
6533 else
6534 {
6535 if (vim_strchr(p_cpo, CPO_ALTREAD) != NULL)
6536 (void)setaltfname(eap->arg, eap->arg, (linenr_T)1);
6537 i = readfile(eap->arg, NULL,
6538 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
6539
6540 }
Bram Moolenaare13b9af2017-01-13 22:01:02 +01006541 if (i != OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006542 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006543#if defined(FEAT_EVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006544 if (!aborting())
6545#endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006546 semsg(_(e_notopen), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006547 }
6548 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00006549 {
6550 if (empty && exmode_active)
6551 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006552 // Delete the empty line that remains. Historically ex does
6553 // this but vi doesn't.
Bram Moolenaardf177f62005-02-22 08:39:57 +00006554 if (eap->line2 == 0)
6555 lnum = curbuf->b_ml.ml_line_count;
6556 else
6557 lnum = 1;
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00006558 if (*ml_get(lnum) == NUL && u_savedel(lnum, 1L) == OK)
Bram Moolenaardf177f62005-02-22 08:39:57 +00006559 {
6560 ml_delete(lnum, FALSE);
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00006561 if (curwin->w_cursor.lnum > 1
6562 && curwin->w_cursor.lnum >= lnum)
Bram Moolenaardf177f62005-02-22 08:39:57 +00006563 --curwin->w_cursor.lnum;
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00006564 deleted_lines_mark(lnum, 1L);
Bram Moolenaardf177f62005-02-22 08:39:57 +00006565 }
6566 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006567 redraw_curbuf_later(VALID);
Bram Moolenaardf177f62005-02-22 08:39:57 +00006568 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006569 }
6570}
6571
Bram Moolenaarea408852005-06-25 22:49:46 +00006572static char_u *prev_dir = NULL;
6573
6574#if defined(EXITFREE) || defined(PROTO)
6575 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006576free_cd_dir(void)
Bram Moolenaarea408852005-06-25 22:49:46 +00006577{
Bram Moolenaard23a8232018-02-10 18:45:26 +01006578 VIM_CLEAR(prev_dir);
6579 VIM_CLEAR(globaldir);
Bram Moolenaarea408852005-06-25 22:49:46 +00006580}
6581#endif
6582
Bram Moolenaarf4258302013-06-02 18:20:17 +02006583/*
6584 * Deal with the side effects of changing the current directory.
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006585 * When 'scope' is CDSCOPE_TABPAGE then this was after an ":tcd" command.
6586 * When 'scope' is CDSCOPE_WINDOW then this was after an ":lcd" command.
Bram Moolenaarf4258302013-06-02 18:20:17 +02006587 */
6588 void
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006589post_chdir(cdscope_T scope)
Bram Moolenaarf4258302013-06-02 18:20:17 +02006590{
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006591 if (scope != CDSCOPE_WINDOW)
Bram Moolenaar00aa0692019-04-27 20:37:57 +02006592 // Clear tab local directory for both :cd and :tcd
6593 VIM_CLEAR(curtab->tp_localdir);
Bram Moolenaard23a8232018-02-10 18:45:26 +01006594 VIM_CLEAR(curwin->w_localdir);
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006595 if (scope != CDSCOPE_GLOBAL)
Bram Moolenaarf4258302013-06-02 18:20:17 +02006596 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006597 // If still in global directory, need to remember current
6598 // directory as global directory.
Bram Moolenaarf4258302013-06-02 18:20:17 +02006599 if (globaldir == NULL && prev_dir != NULL)
6600 globaldir = vim_strsave(prev_dir);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006601 // Remember this local directory for the window.
Bram Moolenaarf4258302013-06-02 18:20:17 +02006602 if (mch_dirname(NameBuff, MAXPATHL) == OK)
Bram Moolenaar00aa0692019-04-27 20:37:57 +02006603 {
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006604 if (scope == CDSCOPE_TABPAGE)
Bram Moolenaar00aa0692019-04-27 20:37:57 +02006605 curtab->tp_localdir = vim_strsave(NameBuff);
6606 else
6607 curwin->w_localdir = vim_strsave(NameBuff);
6608 }
Bram Moolenaarf4258302013-06-02 18:20:17 +02006609 }
6610 else
6611 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006612 // We are now in the global directory, no need to remember its
6613 // name.
Bram Moolenaard23a8232018-02-10 18:45:26 +01006614 VIM_CLEAR(globaldir);
Bram Moolenaarf4258302013-06-02 18:20:17 +02006615 }
6616
6617 shorten_fnames(TRUE);
6618}
6619
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006620/*
6621 * Change directory function used by :cd/:tcd/:lcd Ex commands and the
6622 * chdir() function. If 'winlocaldir' is TRUE, then changes the window-local
6623 * directory. If 'tablocaldir' is TRUE, then changes the tab-local directory.
6624 * Otherwise changes the global directory.
6625 * Returns TRUE if the directory is successfully changed.
6626 */
6627 int
6628changedir_func(
6629 char_u *new_dir,
6630 int forceit,
6631 cdscope_T scope)
6632{
6633 char_u *tofree;
6634 int dir_differs;
6635 int retval = FALSE;
6636
Bram Moolenaar7cc96922020-01-31 22:41:38 +01006637 if (new_dir == NULL || allbuf_locked())
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006638 return FALSE;
6639
6640 if (vim_strchr(p_cpo, CPO_CHDIR) != NULL && curbufIsChanged() && !forceit)
6641 {
6642 emsg(_("E747: Cannot change directory, buffer is modified (add ! to override)"));
6643 return FALSE;
6644 }
6645
6646 // ":cd -": Change to previous directory
6647 if (STRCMP(new_dir, "-") == 0)
6648 {
6649 if (prev_dir == NULL)
6650 {
6651 emsg(_("E186: No previous directory"));
6652 return FALSE;
6653 }
6654 new_dir = prev_dir;
6655 }
6656
6657 // Save current directory for next ":cd -"
6658 tofree = prev_dir;
6659 if (mch_dirname(NameBuff, MAXPATHL) == OK)
6660 prev_dir = vim_strsave(NameBuff);
6661 else
6662 prev_dir = NULL;
6663
6664#if defined(UNIX) || defined(VMS)
6665 // for UNIX ":cd" means: go to home directory
6666 if (*new_dir == NUL)
6667 {
6668 // use NameBuff for home directory name
6669# ifdef VMS
6670 char_u *p;
6671
6672 p = mch_getenv((char_u *)"SYS$LOGIN");
6673 if (p == NULL || *p == NUL) // empty is the same as not set
6674 NameBuff[0] = NUL;
6675 else
6676 vim_strncpy(NameBuff, p, MAXPATHL - 1);
6677# else
6678 expand_env((char_u *)"$HOME", NameBuff, MAXPATHL);
6679# endif
6680 new_dir = NameBuff;
6681 }
6682#endif
6683 dir_differs = new_dir == NULL || prev_dir == NULL
6684 || pathcmp((char *)prev_dir, (char *)new_dir, -1) != 0;
6685 if (new_dir == NULL || (dir_differs && vim_chdir(new_dir)))
6686 emsg(_(e_failed));
6687 else
6688 {
6689 char_u *acmd_fname;
6690
6691 post_chdir(scope);
6692
6693 if (dir_differs)
6694 {
6695 if (scope == CDSCOPE_WINDOW)
6696 acmd_fname = (char_u *)"window";
6697 else if (scope == CDSCOPE_TABPAGE)
6698 acmd_fname = (char_u *)"tabpage";
6699 else
6700 acmd_fname = (char_u *)"global";
6701 apply_autocmds(EVENT_DIRCHANGED, acmd_fname, new_dir, FALSE,
6702 curbuf);
6703 }
6704 retval = TRUE;
6705 }
6706 vim_free(tofree);
6707
6708 return retval;
6709}
Bram Moolenaarea408852005-06-25 22:49:46 +00006710
Bram Moolenaar071d4272004-06-13 20:20:40 +00006711/*
Bram Moolenaar00aa0692019-04-27 20:37:57 +02006712 * ":cd", ":tcd", ":lcd", ":chdir" ":tchdir" and ":lchdir".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006713 */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00006714 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006715ex_cd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006716{
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01006717 char_u *new_dir;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006718
6719 new_dir = eap->arg;
6720#if !defined(UNIX) && !defined(VMS)
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006721 // for non-UNIX ":cd" means: print current directory
Bram Moolenaar071d4272004-06-13 20:20:40 +00006722 if (*new_dir == NUL)
6723 ex_pwd(NULL);
6724 else
6725#endif
6726 {
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006727 cdscope_T scope = CDSCOPE_GLOBAL;
6728
6729 if (eap->cmdidx == CMD_lcd || eap->cmdidx == CMD_lchdir)
6730 scope = CDSCOPE_WINDOW;
6731 else if (eap->cmdidx == CMD_tcd || eap->cmdidx == CMD_tchdir)
6732 scope = CDSCOPE_TABPAGE;
6733
6734 if (changedir_func(new_dir, eap->forceit, scope))
Bram Moolenaardf177f62005-02-22 08:39:57 +00006735 {
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006736 // Echo the new current directory if the command was typed.
Bram Moolenaarfcfbc672009-07-09 18:13:49 +00006737 if (KeyTyped || p_verbose >= 5)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006738 ex_pwd(eap);
6739 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006740 }
6741}
6742
6743/*
6744 * ":pwd".
6745 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006746 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006747ex_pwd(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006748{
6749 if (mch_dirname(NameBuff, MAXPATHL) == OK)
6750 {
6751#ifdef BACKSLASH_IN_FILENAME
6752 slash_adjust(NameBuff);
6753#endif
Bram Moolenaar32526b32019-01-19 17:43:09 +01006754 msg((char *)NameBuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006755 }
6756 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006757 emsg(_("E187: Unknown"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006758}
6759
6760/*
6761 * ":=".
6762 */
6763 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006764ex_equal(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006765{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006766 smsg("%ld", (long)eap->line2);
Bram Moolenaardf177f62005-02-22 08:39:57 +00006767 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006768}
6769
6770 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006771ex_sleep(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006772{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006773 int n;
6774 long len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006775
6776 if (cursor_valid())
6777 {
6778 n = W_WINROW(curwin) + curwin->w_wrow - msg_scrolled;
6779 if (n >= 0)
Bram Moolenaar53f81742017-09-22 14:35:51 +02006780 windgoto((int)n, curwin->w_wincol + curwin->w_wcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006781 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006782
6783 len = eap->line2;
6784 switch (*eap->arg)
6785 {
6786 case 'm': break;
6787 case NUL: len *= 1000L; break;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006788 default: semsg(_(e_invarg2), eap->arg); return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006789 }
6790 do_sleep(len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006791}
6792
6793/*
6794 * Sleep for "msec" milliseconds, but keep checking for a CTRL-C every second.
6795 */
6796 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006797do_sleep(long msec)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006798{
Bram Moolenaar52953192019-08-16 10:27:13 +02006799 long done = 0;
Bram Moolenaar975b5272016-03-15 23:10:59 +01006800 long wait_now;
Bram Moolenaar52953192019-08-16 10:27:13 +02006801# ifdef ELAPSED_FUNC
6802 elapsed_T start_tv;
6803
6804 // Remember at what time we started, so that we know how much longer we
6805 // should wait after waiting for a bit.
6806 ELAPSED_INIT(start_tv);
6807# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006808
6809 cursor_on();
Bram Moolenaarf45d7472018-09-30 18:22:26 +02006810 out_flush_cursor(FALSE, FALSE);
Bram Moolenaar52953192019-08-16 10:27:13 +02006811 while (!got_int && done < msec)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006812 {
Bram Moolenaar975b5272016-03-15 23:10:59 +01006813 wait_now = msec - done > 1000L ? 1000L : msec - done;
6814#ifdef FEAT_TIMERS
6815 {
6816 long due_time = check_due_timer();
6817
6818 if (due_time > 0 && due_time < wait_now)
6819 wait_now = due_time;
6820 }
6821#endif
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02006822#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar28e67e02019-08-15 23:05:49 +02006823 if (has_any_channel() && wait_now > 20L)
6824 wait_now = 20L;
6825#endif
6826#ifdef FEAT_SOUND
6827 if (has_any_sound_callback() && wait_now > 20L)
6828 wait_now = 20L;
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02006829#endif
Bram Moolenaar975b5272016-03-15 23:10:59 +01006830 ui_delay(wait_now, TRUE);
Bram Moolenaar52953192019-08-16 10:27:13 +02006831
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02006832#ifdef FEAT_JOB_CHANNEL
6833 if (has_any_channel())
6834 ui_breakcheck_force(TRUE);
6835 else
6836#endif
6837 ui_breakcheck();
Bram Moolenaar93c88e02015-09-15 14:12:05 +02006838#ifdef MESSAGE_QUEUE
Bram Moolenaar52953192019-08-16 10:27:13 +02006839 // Process the netbeans and clientserver messages that may have been
6840 // received in the call to ui_breakcheck() when the GUI is in use. This
6841 // may occur when running a test case.
Bram Moolenaar93c88e02015-09-15 14:12:05 +02006842 parse_queued_messages();
Bram Moolenaare3cc6d42011-10-20 21:58:34 +02006843#endif
Bram Moolenaar52953192019-08-16 10:27:13 +02006844
6845# ifdef ELAPSED_FUNC
6846 // actual time passed
6847 done = ELAPSED_FUNC(start_tv);
6848# else
6849 // guestimate time passed (will actually be more)
6850 done += wait_now;
6851# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006852 }
Bram Moolenaar1ebff3d2018-07-07 16:18:13 +02006853
6854 // If CTRL-C was typed to interrupt the sleep, drop the CTRL-C from the
6855 // input buffer, otherwise a following call to input() fails.
6856 if (got_int)
6857 (void)vpeekc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006858}
6859
Bram Moolenaar071d4272004-06-13 20:20:40 +00006860/*
6861 * ":winsize" command (obsolete).
6862 */
6863 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006864ex_winsize(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006865{
6866 int w, h;
6867 char_u *arg = eap->arg;
6868 char_u *p;
6869
6870 w = getdigits(&arg);
6871 arg = skipwhite(arg);
6872 p = arg;
6873 h = getdigits(&arg);
6874 if (*p != NUL && *arg == NUL)
6875 set_shellsize(w, h, TRUE);
6876 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006877 emsg(_("E465: :winsize requires two number arguments"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006878}
6879
Bram Moolenaar071d4272004-06-13 20:20:40 +00006880 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006881ex_wincmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006882{
6883 int xchar = NUL;
6884 char_u *p;
6885
6886 if (*eap->arg == 'g' || *eap->arg == Ctrl_G)
6887 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006888 // CTRL-W g and CTRL-W CTRL-G have an extra command character
Bram Moolenaar071d4272004-06-13 20:20:40 +00006889 if (eap->arg[1] == NUL)
6890 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006891 emsg(_(e_invarg));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006892 return;
6893 }
6894 xchar = eap->arg[1];
6895 p = eap->arg + 2;
6896 }
6897 else
6898 p = eap->arg + 1;
6899
6900 eap->nextcmd = check_nextcmd(p);
6901 p = skipwhite(p);
6902 if (*p != NUL && *p != '"' && eap->nextcmd == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006903 emsg(_(e_invarg));
Bram Moolenaar12bde492011-06-13 01:19:56 +02006904 else if (!eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006905 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006906 // Pass flags on for ":vertical wincmd ]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006907 postponed_split_flags = cmdmod.split;
Bram Moolenaard326ce82007-03-11 14:48:29 +00006908 postponed_split_tab = cmdmod.tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006909 do_window(*eap->arg, eap->addr_count > 0 ? eap->line2 : 0L, xchar);
6910 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00006911 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006912 }
6913}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006914
Bram Moolenaar843ee412004-06-30 16:16:41 +00006915#if defined(FEAT_GUI) || defined(UNIX) || defined(VMS) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006916/*
6917 * ":winpos".
6918 */
6919 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006920ex_winpos(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006921{
6922 int x, y;
6923 char_u *arg = eap->arg;
6924 char_u *p;
6925
6926 if (*arg == NUL)
6927 {
Bram Moolenaar843ee412004-06-30 16:16:41 +00006928# if defined(FEAT_GUI) || defined(MSWIN)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02006929# ifdef VIMDLL
6930 if (gui.in_use ? gui_mch_get_winpos(&x, &y) != FAIL :
6931 mch_get_winpos(&x, &y) != FAIL)
6932# elif defined(FEAT_GUI)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006933 if (gui.in_use && gui_mch_get_winpos(&x, &y) != FAIL)
Bram Moolenaar843ee412004-06-30 16:16:41 +00006934# else
6935 if (mch_get_winpos(&x, &y) != FAIL)
6936# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006937 {
6938 sprintf((char *)IObuff, _("Window position: X %d, Y %d"), x, y);
Bram Moolenaar32526b32019-01-19 17:43:09 +01006939 msg((char *)IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006940 }
6941 else
6942# endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006943 emsg(_("E188: Obtaining window position not implemented for this platform"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006944 }
6945 else
6946 {
6947 x = getdigits(&arg);
6948 arg = skipwhite(arg);
6949 p = arg;
6950 y = getdigits(&arg);
6951 if (*p == NUL || *arg != NUL)
6952 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006953 emsg(_("E466: :winpos requires two number arguments"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006954 return;
6955 }
6956# ifdef FEAT_GUI
6957 if (gui.in_use)
6958 gui_mch_set_winpos(x, y);
6959 else if (gui.starting)
6960 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006961 // Remember the coordinates for when the window is opened.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006962 gui_win_x = x;
6963 gui_win_y = y;
6964 }
Bram Moolenaarafde13b2019-04-28 19:46:49 +02006965# if defined(HAVE_TGETENT) || defined(VIMDLL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006966 else
6967# endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02006968# endif
6969# if defined(MSWIN) && (!defined(FEAT_GUI) || defined(VIMDLL))
Bram Moolenaar843ee412004-06-30 16:16:41 +00006970 mch_set_winpos(x, y);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006971# endif
6972# ifdef HAVE_TGETENT
6973 if (*T_CWP)
6974 term_set_winpos(x, y);
6975# endif
6976 }
6977}
6978#endif
6979
6980/*
6981 * Handle command that work like operators: ":delete", ":yank", ":>" and ":<".
6982 */
6983 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006984ex_operators(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006985{
6986 oparg_T oa;
6987
6988 clear_oparg(&oa);
6989 oa.regname = eap->regname;
6990 oa.start.lnum = eap->line1;
6991 oa.end.lnum = eap->line2;
6992 oa.line_count = eap->line2 - eap->line1 + 1;
6993 oa.motion_type = MLINE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006994 virtual_op = FALSE;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006995 if (eap->cmdidx != CMD_yank) // position cursor for undo
Bram Moolenaar071d4272004-06-13 20:20:40 +00006996 {
6997 setpcmark();
6998 curwin->w_cursor.lnum = eap->line1;
6999 beginline(BL_SOL | BL_FIX);
7000 }
7001
Bram Moolenaard07c6e12013-11-21 14:21:40 +01007002 if (VIsual_active)
7003 end_visual_mode();
Bram Moolenaard07c6e12013-11-21 14:21:40 +01007004
Bram Moolenaar071d4272004-06-13 20:20:40 +00007005 switch (eap->cmdidx)
7006 {
7007 case CMD_delete:
7008 oa.op_type = OP_DELETE;
7009 op_delete(&oa);
7010 break;
7011
7012 case CMD_yank:
7013 oa.op_type = OP_YANK;
7014 (void)op_yank(&oa, FALSE, TRUE);
7015 break;
7016
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007017 default: // CMD_rshift or CMD_lshift
Bram Moolenaar6e707362013-06-14 19:15:58 +02007018 if (
Bram Moolenaar071d4272004-06-13 20:20:40 +00007019#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6e707362013-06-14 19:15:58 +02007020 (eap->cmdidx == CMD_rshift) ^ curwin->w_p_rl
7021#else
7022 eap->cmdidx == CMD_rshift
Bram Moolenaar071d4272004-06-13 20:20:40 +00007023#endif
Bram Moolenaar6e707362013-06-14 19:15:58 +02007024 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00007025 oa.op_type = OP_RSHIFT;
7026 else
7027 oa.op_type = OP_LSHIFT;
7028 op_shift(&oa, FALSE, eap->amount);
7029 break;
7030 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007031 virtual_op = MAYBE;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007032 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007033}
7034
7035/*
7036 * ":put".
7037 */
7038 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007039ex_put(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007040{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007041 // ":0put" works like ":1put!".
Bram Moolenaar071d4272004-06-13 20:20:40 +00007042 if (eap->line2 == 0)
7043 {
7044 eap->line2 = 1;
7045 eap->forceit = TRUE;
7046 }
7047 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007048 do_put(eap->regname, eap->forceit ? BACKWARD : FORWARD, 1L,
7049 PUT_LINE|PUT_CURSLINE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007050}
7051
7052/*
7053 * Handle ":copy" and ":move".
7054 */
7055 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007056ex_copymove(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007057{
7058 long n;
7059
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02007060 n = get_address(eap, &eap->arg, eap->addr_type, FALSE, FALSE, FALSE, 1);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007061 if (eap->arg == NULL) // error detected
Bram Moolenaar071d4272004-06-13 20:20:40 +00007062 {
7063 eap->nextcmd = NULL;
7064 return;
7065 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00007066 get_flags(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007067
7068 /*
7069 * move or copy lines from 'eap->line1'-'eap->line2' to below line 'n'
7070 */
7071 if (n == MAXLNUM || n < 0 || n > curbuf->b_ml.ml_line_count)
7072 {
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02007073 emsg(_(e_invrange));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007074 return;
7075 }
7076
7077 if (eap->cmdidx == CMD_move)
7078 {
7079 if (do_move(eap->line1, eap->line2, n) == FAIL)
7080 return;
7081 }
7082 else
7083 ex_copy(eap->line1, eap->line2, n);
7084 u_clearline();
7085 beginline(BL_SOL | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007086 ex_may_print(eap);
7087}
7088
7089/*
7090 * Print the current line if flags were given to the Ex command.
7091 */
Bram Moolenaarfd3fe982014-04-01 17:49:44 +02007092 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007093ex_may_print(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007094{
7095 if (eap->flags != 0)
7096 {
7097 print_line(curwin->w_cursor.lnum, (eap->flags & EXFLAG_NR),
7098 (eap->flags & EXFLAG_LIST));
7099 ex_no_reprint = TRUE;
7100 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007101}
7102
7103/*
7104 * ":smagic" and ":snomagic".
7105 */
7106 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007107ex_submagic(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007108{
7109 int magic_save = p_magic;
7110
7111 p_magic = (eap->cmdidx == CMD_smagic);
7112 do_sub(eap);
7113 p_magic = magic_save;
7114}
7115
7116/*
7117 * ":join".
7118 */
7119 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007120ex_join(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007121{
7122 curwin->w_cursor.lnum = eap->line1;
7123 if (eap->line1 == eap->line2)
7124 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007125 if (eap->addr_count >= 2) // :2,2join does nothing
Bram Moolenaar071d4272004-06-13 20:20:40 +00007126 return;
7127 if (eap->line2 == curbuf->b_ml.ml_line_count)
7128 {
7129 beep_flush();
7130 return;
7131 }
7132 ++eap->line2;
7133 }
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02007134 (void)do_join(eap->line2 - eap->line1 + 1, !eap->forceit, TRUE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007135 beginline(BL_WHITE | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007136 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007137}
7138
7139/*
7140 * ":[addr]@r" or ":[addr]*r": execute register
7141 */
7142 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007143ex_at(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007144{
7145 int c;
Bram Moolenaar60462872009-11-03 11:40:19 +00007146 int prev_len = typebuf.tb_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007147
7148 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaar4930a762016-09-11 14:39:53 +02007149 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007150
7151#ifdef USE_ON_FLY_SCROLL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007152 dont_scroll = TRUE; // disallow scrolling here
Bram Moolenaar071d4272004-06-13 20:20:40 +00007153#endif
7154
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007155 // get the register name. No name means to use the previous one
Bram Moolenaar071d4272004-06-13 20:20:40 +00007156 c = *eap->arg;
7157 if (c == NUL || (c == '*' && *eap->cmd == '*'))
7158 c = '@';
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007159 // Put the register in the typeahead buffer with the "silent" flag.
Bram Moolenaard333d1e2006-11-07 17:43:47 +00007160 if (do_execreg(c, TRUE, vim_strchr(p_cpo, CPO_EXECBUF) != NULL, TRUE)
7161 == FAIL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007162 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007163 beep_flush();
Bram Moolenaardf177f62005-02-22 08:39:57 +00007164 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007165 else
7166 {
7167 int save_efr = exec_from_reg;
7168
7169 exec_from_reg = TRUE;
7170
7171 /*
7172 * Execute from the typeahead buffer.
Bram Moolenaar60462872009-11-03 11:40:19 +00007173 * Continue until the stuff buffer is empty and all added characters
7174 * have been consumed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007175 */
Bram Moolenaar60462872009-11-03 11:40:19 +00007176 while (!stuff_empty() || typebuf.tb_len > prev_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007177 (void)do_cmdline(NULL, getexline, NULL, DOCMD_NOWAIT|DOCMD_VERBOSE);
7178
7179 exec_from_reg = save_efr;
7180 }
7181}
7182
7183/*
7184 * ":!".
7185 */
7186 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007187ex_bang(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007188{
7189 do_bang(eap->addr_count, eap, eap->forceit, TRUE, TRUE);
7190}
7191
7192/*
7193 * ":undo".
7194 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007195 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +01007196ex_undo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007197{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007198 if (eap->addr_count == 1) // :undo 123
Bram Moolenaar730cde92010-06-27 05:18:54 +02007199 undo_time(eap->line2, FALSE, FALSE, TRUE);
Bram Moolenaard3667a22006-03-16 21:35:52 +00007200 else
7201 u_undo(1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007202}
7203
Bram Moolenaar55debbe2010-05-23 23:34:36 +02007204#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar6df6f472010-07-18 18:04:50 +02007205 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007206ex_wundo(exarg_T *eap)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02007207{
7208 char_u hash[UNDO_HASH_SIZE];
7209
7210 u_compute_hash(hash);
7211 u_write_undo(eap->arg, eap->forceit, curbuf, hash);
7212}
7213
Bram Moolenaar6df6f472010-07-18 18:04:50 +02007214 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007215ex_rundo(exarg_T *eap)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02007216{
7217 char_u hash[UNDO_HASH_SIZE];
7218
7219 u_compute_hash(hash);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02007220 u_read_undo(eap->arg, hash, NULL);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02007221}
7222#endif
7223
Bram Moolenaar071d4272004-06-13 20:20:40 +00007224/*
7225 * ":redo".
7226 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007227 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007228ex_redo(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007229{
7230 u_redo(1);
7231}
7232
7233/*
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007234 * ":earlier" and ":later".
7235 */
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007236 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007237ex_later(exarg_T *eap)
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007238{
7239 long count = 0;
7240 int sec = FALSE;
Bram Moolenaar730cde92010-06-27 05:18:54 +02007241 int file = FALSE;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007242 char_u *p = eap->arg;
7243
7244 if (*p == NUL)
7245 count = 1;
7246 else if (isdigit(*p))
7247 {
7248 count = getdigits(&p);
7249 switch (*p)
7250 {
7251 case 's': ++p; sec = TRUE; break;
7252 case 'm': ++p; sec = TRUE; count *= 60; break;
7253 case 'h': ++p; sec = TRUE; count *= 60 * 60; break;
Bram Moolenaar730cde92010-06-27 05:18:54 +02007254 case 'd': ++p; sec = TRUE; count *= 24 * 60 * 60; break;
7255 case 'f': ++p; file = TRUE; break;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007256 }
7257 }
7258
7259 if (*p != NUL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007260 semsg(_(e_invarg2), eap->arg);
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007261 else
Bram Moolenaar730cde92010-06-27 05:18:54 +02007262 undo_time(eap->cmdidx == CMD_earlier ? -count : count,
7263 sec, file, FALSE);
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007264}
7265
7266/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007267 * ":redir": start/stop redirection.
7268 */
7269 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007270ex_redir(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007271{
7272 char *mode;
7273 char_u *fname;
Bram Moolenaarca472992005-01-21 11:46:23 +00007274 char_u *arg = eap->arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007275
Bram Moolenaarba768492016-07-08 15:32:54 +02007276#ifdef FEAT_EVAL
Bram Moolenaar79815f12016-07-09 17:07:29 +02007277 if (redir_execute)
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02007278 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007279 emsg(_("E930: Cannot use :redir inside execute()"));
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02007280 return;
7281 }
Bram Moolenaarba768492016-07-08 15:32:54 +02007282#endif
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02007283
Bram Moolenaar071d4272004-06-13 20:20:40 +00007284 if (STRICMP(eap->arg, "END") == 0)
7285 close_redir();
7286 else
7287 {
Bram Moolenaarca472992005-01-21 11:46:23 +00007288 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00007289 {
Bram Moolenaarca472992005-01-21 11:46:23 +00007290 ++arg;
7291 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00007292 {
Bram Moolenaarca472992005-01-21 11:46:23 +00007293 ++arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007294 mode = "a";
7295 }
7296 else
7297 mode = "w";
Bram Moolenaarca472992005-01-21 11:46:23 +00007298 arg = skipwhite(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007299
7300 close_redir();
7301
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007302 // Expand environment variables and "~/".
Bram Moolenaarca472992005-01-21 11:46:23 +00007303 fname = expand_env_save(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007304 if (fname == NULL)
7305 return;
7306#ifdef FEAT_BROWSE
7307 if (cmdmod.browse)
7308 {
7309 char_u *browseFile;
7310
Bram Moolenaar7171abe2004-10-11 10:06:20 +00007311 browseFile = do_browse(BROWSE_SAVE,
7312 (char_u *)_("Save Redirection"),
Bram Moolenaarc36651b2018-04-29 12:22:56 +02007313 fname, NULL, NULL,
7314 (char_u *)_(BROWSE_FILTER_ALL_FILES), curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007315 if (browseFile == NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007316 return; // operation cancelled
Bram Moolenaar071d4272004-06-13 20:20:40 +00007317 vim_free(fname);
7318 fname = browseFile;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007319 eap->forceit = TRUE; // since dialog already asked
Bram Moolenaar071d4272004-06-13 20:20:40 +00007320 }
7321#endif
7322
7323 redir_fd = open_exfile(fname, eap->forceit, mode);
7324 vim_free(fname);
7325 }
7326#ifdef FEAT_EVAL
Bram Moolenaarca472992005-01-21 11:46:23 +00007327 else if (*arg == '@')
Bram Moolenaar071d4272004-06-13 20:20:40 +00007328 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007329 // redirect to a register a-z (resp. A-Z for appending)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007330 close_redir();
Bram Moolenaarca472992005-01-21 11:46:23 +00007331 ++arg;
7332 if (ASCII_ISALPHA(*arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007333# ifdef FEAT_CLIPBOARD
Bram Moolenaarca472992005-01-21 11:46:23 +00007334 || *arg == '*'
Bram Moolenaar9a51c6e2006-11-14 19:25:02 +00007335 || *arg == '+'
Bram Moolenaar071d4272004-06-13 20:20:40 +00007336# endif
Bram Moolenaarca472992005-01-21 11:46:23 +00007337 || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00007338 {
Bram Moolenaarca472992005-01-21 11:46:23 +00007339 redir_reg = *arg++;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007340 if (*arg == '>' && arg[1] == '>') // append
Bram Moolenaard9d30582005-05-18 22:10:28 +00007341 arg += 2;
Bram Moolenaarc188b882007-10-19 14:20:54 +00007342 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007343 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007344 // Can use both "@a" and "@a>".
Bram Moolenaar2c29bee2005-06-01 21:46:07 +00007345 if (*arg == '>')
7346 arg++;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007347 // Make register empty when not using @A-@Z and the
7348 // command is valid.
Bram Moolenaarc188b882007-10-19 14:20:54 +00007349 if (*arg == NUL && !isupper(redir_reg))
7350 write_reg_contents(redir_reg, (char_u *)"", -1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007351 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00007352 }
7353 if (*arg != NUL)
7354 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00007355 redir_reg = 0;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007356 semsg(_(e_invarg2), eap->arg);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007357 }
7358 }
7359 else if (*arg == '=' && arg[1] == '>')
7360 {
7361 int append;
7362
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007363 // redirect to a variable
Bram Moolenaardf177f62005-02-22 08:39:57 +00007364 close_redir();
7365 arg += 2;
7366
7367 if (*arg == '>')
7368 {
7369 ++arg;
7370 append = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007371 }
7372 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00007373 append = FALSE;
7374
7375 if (var_redir_start(skipwhite(arg), append) == OK)
7376 redir_vname = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007377 }
7378#endif
7379
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007380 // TODO: redirect to a buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00007381
Bram Moolenaar071d4272004-06-13 20:20:40 +00007382 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007383 semsg(_(e_invarg2), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007384 }
Bram Moolenaar29b2d262006-09-10 19:07:28 +00007385
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007386 // Make sure redirection is not off. Can happen for cmdline completion
7387 // that indirectly invokes a command to catch its output.
Bram Moolenaar29b2d262006-09-10 19:07:28 +00007388 if (redir_fd != NULL
7389#ifdef FEAT_EVAL
7390 || redir_reg || redir_vname
7391#endif
7392 )
7393 redir_off = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007394}
7395
7396/*
7397 * ":redraw": force redraw
7398 */
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01007399 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007400ex_redraw(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007401{
7402 int r = RedrawingDisabled;
7403 int p = p_lz;
7404
7405 RedrawingDisabled = 0;
7406 p_lz = FALSE;
Bram Moolenaarabc39ab2017-03-01 18:04:05 +01007407 validate_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007408 update_topline();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007409 update_screen(eap->forceit ? CLEAR : VIsual_active ? INVERTED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007410#ifdef FEAT_TITLE
7411 if (need_maketitle)
7412 maketitle();
7413#endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007414#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
7415# ifdef VIMDLL
7416 if (!gui.in_use)
7417# endif
7418 resize_console_buf();
Bram Moolenaar78d21da2019-02-17 15:00:52 +01007419#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007420 RedrawingDisabled = r;
7421 p_lz = p;
7422
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007423 // Reset msg_didout, so that a message that's there is overwritten.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007424 msg_didout = FALSE;
7425 msg_col = 0;
7426
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007427 // No need to wait after an intentional redraw.
Bram Moolenaar56667a52013-07-17 11:54:28 +02007428 need_wait_return = FALSE;
7429
Bram Moolenaar071d4272004-06-13 20:20:40 +00007430 out_flush();
7431}
7432
7433/*
7434 * ":redrawstatus": force redraw of status line(s)
7435 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007436 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007437ex_redrawstatus(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007438{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007439 int r = RedrawingDisabled;
7440 int p = p_lz;
7441
7442 RedrawingDisabled = 0;
7443 p_lz = FALSE;
7444 if (eap->forceit)
7445 status_redraw_all();
7446 else
7447 status_redraw_curbuf();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007448 update_screen(VIsual_active ? INVERTED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007449 RedrawingDisabled = r;
7450 p_lz = p;
7451 out_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007452}
7453
Bram Moolenaare12bab32019-01-08 22:02:56 +01007454/*
7455 * ":redrawtabline": force redraw of the tabline
7456 */
7457 static void
7458ex_redrawtabline(exarg_T *eap UNUSED)
7459{
7460 int r = RedrawingDisabled;
7461 int p = p_lz;
7462
7463 RedrawingDisabled = 0;
7464 p_lz = FALSE;
7465
7466 draw_tabline();
7467
7468 RedrawingDisabled = r;
7469 p_lz = p;
7470 out_flush();
7471}
7472
Bram Moolenaar071d4272004-06-13 20:20:40 +00007473 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007474close_redir(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007475{
7476 if (redir_fd != NULL)
7477 {
7478 fclose(redir_fd);
7479 redir_fd = NULL;
7480 }
7481#ifdef FEAT_EVAL
7482 redir_reg = 0;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007483 if (redir_vname)
7484 {
7485 var_redir_stop();
7486 redir_vname = 0;
7487 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007488#endif
7489}
7490
Bram Moolenaarf96ae0b2019-07-28 15:21:55 +02007491#if (defined(FEAT_SESSION) || defined(FEAT_EVAL)) || defined(PROTO)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007492 int
Bram Moolenaarbd67aac2019-09-21 23:09:04 +02007493vim_mkdir_emsg(char_u *name, int prot UNUSED)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007494{
7495 if (vim_mkdir(name, prot) != 0)
7496 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007497 semsg(_("E739: Cannot create directory: %s"), name);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007498 return FAIL;
7499 }
7500 return OK;
7501}
7502#endif
7503
Bram Moolenaar071d4272004-06-13 20:20:40 +00007504/*
7505 * Open a file for writing for an Ex command, with some checks.
7506 * Return file descriptor, or NULL on failure.
7507 */
7508 FILE *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007509open_exfile(
7510 char_u *fname,
7511 int forceit,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007512 char *mode) // "w" for create new file or "a" for append
Bram Moolenaar071d4272004-06-13 20:20:40 +00007513{
7514 FILE *fd;
7515
7516#ifdef UNIX
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007517 // with Unix it is possible to open a directory
Bram Moolenaar071d4272004-06-13 20:20:40 +00007518 if (mch_isdir(fname))
7519 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007520 semsg(_(e_isadir2), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007521 return NULL;
7522 }
7523#endif
7524 if (!forceit && *mode != 'a' && vim_fexists(fname))
7525 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007526 semsg(_("E189: \"%s\" exists (add ! to override)"), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007527 return NULL;
7528 }
7529
7530 if ((fd = mch_fopen((char *)fname, mode)) == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007531 semsg(_("E190: Cannot open \"%s\" for writing"), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007532
7533 return fd;
7534}
7535
7536/*
7537 * ":mark" and ":k".
7538 */
7539 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007540ex_mark(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007541{
7542 pos_T pos;
7543
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007544 if (*eap->arg == NUL) // No argument?
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007545 emsg(_(e_argreq));
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007546 else if (eap->arg[1] != NUL) // more than one character?
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007547 emsg(_(e_trailing));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007548 else
7549 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007550 pos = curwin->w_cursor; // save curwin->w_cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00007551 curwin->w_cursor.lnum = eap->line2;
7552 beginline(BL_WHITE | BL_FIX);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007553 if (setmark(*eap->arg) == FAIL) // set mark
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007554 emsg(_("E191: Argument must be a letter or forward/backward quote"));
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007555 curwin->w_cursor = pos; // restore curwin->w_cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00007556 }
7557}
7558
7559/*
7560 * Update w_topline, w_leftcol and the cursor position.
7561 */
7562 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007563update_topline_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007564{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007565 check_cursor(); // put cursor on valid line
Bram Moolenaar071d4272004-06-13 20:20:40 +00007566 update_topline();
7567 if (!curwin->w_p_wrap)
7568 validate_cursor();
7569 update_curswant();
7570}
7571
Bram Moolenaar071d4272004-06-13 20:20:40 +00007572/*
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007573 * Save the current State and go to Normal mode.
7574 * Return TRUE if the typeahead could be saved.
7575 */
7576 int
7577save_current_state(save_state_T *sst)
7578{
7579 sst->save_msg_scroll = msg_scroll;
7580 sst->save_restart_edit = restart_edit;
7581 sst->save_msg_didout = msg_didout;
7582 sst->save_State = State;
7583 sst->save_insertmode = p_im;
7584 sst->save_finish_op = finish_op;
7585 sst->save_opcount = opcount;
Bram Moolenaarcce713d2019-03-04 11:40:12 +01007586 sst->save_reg_executing = reg_executing;
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007587
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007588 msg_scroll = FALSE; // no msg scrolling in Normal mode
7589 restart_edit = 0; // don't go to Insert mode
7590 p_im = FALSE; // don't use 'insertmode'
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007591
7592 /*
7593 * Save the current typeahead. This is required to allow using ":normal"
7594 * from an event handler and makes sure we don't hang when the argument
7595 * ends with half a command.
7596 */
7597 save_typeahead(&sst->tabuf);
7598 return sst->tabuf.typebuf_valid;
7599}
7600
7601 void
7602restore_current_state(save_state_T *sst)
7603{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007604 // Restore the previous typeahead.
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007605 restore_typeahead(&sst->tabuf);
7606
7607 msg_scroll = sst->save_msg_scroll;
7608 restart_edit = sst->save_restart_edit;
7609 p_im = sst->save_insertmode;
7610 finish_op = sst->save_finish_op;
7611 opcount = sst->save_opcount;
Bram Moolenaarcce713d2019-03-04 11:40:12 +01007612 reg_executing = sst->save_reg_executing;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007613 msg_didout |= sst->save_msg_didout; // don't reset msg_didout now
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007614
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007615 // Restore the state (needed when called from a function executed for
7616 // 'indentexpr'). Update the mouse and cursor, they may have changed.
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007617 State = sst->save_State;
7618#ifdef CURSOR_SHAPE
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007619 ui_cursor_shape(); // may show different cursor shape
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007620#endif
7621}
7622
7623/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007624 * ":normal[!] {commands}": Execute normal mode commands.
7625 */
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01007626 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007627ex_normal(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007628{
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007629 save_state_T save_state;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007630 char_u *arg = NULL;
7631 int l;
7632 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007633
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00007634 if (ex_normal_lock > 0)
7635 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007636 emsg(_(e_secure));
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00007637 return;
7638 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007639 if (ex_normal_busy >= p_mmd)
7640 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007641 emsg(_("E192: Recursive use of :normal too deep"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007642 return;
7643 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007644
Bram Moolenaar071d4272004-06-13 20:20:40 +00007645 /*
7646 * vgetc() expects a CSI and K_SPECIAL to have been escaped. Don't do
7647 * this for the K_SPECIAL leading byte, otherwise special keys will not
7648 * work.
7649 */
7650 if (has_mbyte)
7651 {
7652 int len = 0;
7653
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007654 // Count the number of characters to be escaped.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007655 for (p = eap->arg; *p != NUL; ++p)
7656 {
Bram Moolenaar13505972019-01-24 15:04:48 +01007657#ifdef FEAT_GUI
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007658 if (*p == CSI) // leadbyte CSI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007659 len += 2;
Bram Moolenaar13505972019-01-24 15:04:48 +01007660#endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007661 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007662 if (*++p == K_SPECIAL // trailbyte K_SPECIAL or CSI
Bram Moolenaar13505972019-01-24 15:04:48 +01007663#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007664 || *p == CSI
Bram Moolenaar13505972019-01-24 15:04:48 +01007665#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007666 )
7667 len += 2;
7668 }
7669 if (len > 0)
7670 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02007671 arg = alloc(STRLEN(eap->arg) + len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007672 if (arg != NULL)
7673 {
7674 len = 0;
7675 for (p = eap->arg; *p != NUL; ++p)
7676 {
7677 arg[len++] = *p;
Bram Moolenaar13505972019-01-24 15:04:48 +01007678#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007679 if (*p == CSI)
7680 {
7681 arg[len++] = KS_EXTRA;
7682 arg[len++] = (int)KE_CSI;
7683 }
Bram Moolenaar13505972019-01-24 15:04:48 +01007684#endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007685 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007686 {
7687 arg[len++] = *++p;
7688 if (*p == K_SPECIAL)
7689 {
7690 arg[len++] = KS_SPECIAL;
7691 arg[len++] = KE_FILLER;
7692 }
Bram Moolenaar13505972019-01-24 15:04:48 +01007693#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007694 else if (*p == CSI)
7695 {
7696 arg[len++] = KS_EXTRA;
7697 arg[len++] = (int)KE_CSI;
7698 }
Bram Moolenaar13505972019-01-24 15:04:48 +01007699#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007700 }
7701 arg[len] = NUL;
7702 }
7703 }
7704 }
7705 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007706
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007707 ++ex_normal_busy;
7708 if (save_current_state(&save_state))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007709 {
7710 /*
7711 * Repeat the :normal command for each line in the range. When no
7712 * range given, execute it just once, without positioning the cursor
7713 * first.
7714 */
7715 do
7716 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007717 if (eap->addr_count != 0)
7718 {
7719 curwin->w_cursor.lnum = eap->line1++;
7720 curwin->w_cursor.col = 0;
Bram Moolenaard5d37532017-03-27 23:02:07 +02007721 check_cursor_moved(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007722 }
7723
Bram Moolenaar13505972019-01-24 15:04:48 +01007724 exec_normal_cmd(arg != NULL
7725 ? arg
7726 : eap->arg, eap->forceit ? REMAP_NONE : REMAP_YES, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007727 }
7728 while (eap->addr_count > 0 && eap->line1 <= eap->line2 && !got_int);
7729 }
7730
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007731 // Might not return to the main loop when in an event handler.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007732 update_topline_cursor();
7733
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007734 restore_current_state(&save_state);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007735 --ex_normal_busy;
Bram Moolenaareda73602014-11-05 09:53:23 +01007736 setmouse();
Bram Moolenaareda73602014-11-05 09:53:23 +01007737#ifdef CURSOR_SHAPE
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007738 ui_cursor_shape(); // may show different cursor shape
Bram Moolenaareda73602014-11-05 09:53:23 +01007739#endif
7740
Bram Moolenaar071d4272004-06-13 20:20:40 +00007741 vim_free(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007742}
7743
7744/*
Bram Moolenaar64969662005-12-14 21:59:55 +00007745 * ":startinsert", ":startreplace" and ":startgreplace"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007746 */
7747 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007748ex_startinsert(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007749{
Bram Moolenaarfd371682005-01-14 21:42:54 +00007750 if (eap->forceit)
7751 {
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02007752 // cursor line can be zero on startup
Bram Moolenaar09ca9322017-09-26 17:40:45 +02007753 if (!curwin->w_cursor.lnum)
7754 curwin->w_cursor.lnum = 1;
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02007755 set_cursor_for_append_to_line();
Bram Moolenaarfd371682005-01-14 21:42:54 +00007756 }
Bram Moolenaareb58a242020-04-19 18:13:19 +02007757#ifdef FEAT_TERMINAL
7758 // Ignore this when running in an active terminal.
7759 if (term_job_running(curbuf->b_term))
7760 return;
7761#endif
Bram Moolenaarfd371682005-01-14 21:42:54 +00007762
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02007763 // Ignore the command when already in Insert mode. Inserting an
7764 // expression register that invokes a function can do this.
Bram Moolenaara40c5002005-01-09 21:16:21 +00007765 if (State & INSERT)
7766 return;
7767
Bram Moolenaar64969662005-12-14 21:59:55 +00007768 if (eap->cmdidx == CMD_startinsert)
7769 restart_edit = 'a';
7770 else if (eap->cmdidx == CMD_startreplace)
7771 restart_edit = 'R';
Bram Moolenaar071d4272004-06-13 20:20:40 +00007772 else
Bram Moolenaar64969662005-12-14 21:59:55 +00007773 restart_edit = 'V';
7774
7775 if (!eap->forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007776 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00007777 if (eap->cmdidx == CMD_startinsert)
7778 restart_edit = 'i';
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02007779 curwin->w_curswant = 0; // avoid MAXCOL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007780 }
7781}
7782
7783/*
7784 * ":stopinsert"
7785 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007786 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007787ex_stopinsert(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007788{
7789 restart_edit = 0;
7790 stop_insert_mode = TRUE;
Bram Moolenaarfd773e92016-04-02 19:39:16 +02007791 clearmode();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007792}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007793
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00007794/*
7795 * Execute normal mode command "cmd".
7796 * "remap" can be REMAP_NONE or REMAP_YES.
7797 */
7798 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007799exec_normal_cmd(char_u *cmd, int remap, int silent)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00007800{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007801 // Stuff the argument into the typeahead buffer.
Bram Moolenaar25281632016-01-21 23:32:32 +01007802 ins_typebuf(cmd, remap, 0, TRUE, silent);
Bram Moolenaar586c70c2018-10-02 16:23:58 +02007803 exec_normal(FALSE, FALSE, FALSE);
Bram Moolenaar25281632016-01-21 23:32:32 +01007804}
Bram Moolenaar25281632016-01-21 23:32:32 +01007805
Bram Moolenaar25281632016-01-21 23:32:32 +01007806/*
7807 * Execute normal_cmd() until there is no typeahead left.
Bram Moolenaar586c70c2018-10-02 16:23:58 +02007808 * When "use_vpeekc" is TRUE use vpeekc() to check for available chars.
Bram Moolenaar25281632016-01-21 23:32:32 +01007809 */
7810 void
Bram Moolenaar586c70c2018-10-02 16:23:58 +02007811exec_normal(int was_typed, int use_vpeekc, int may_use_terminal_loop UNUSED)
Bram Moolenaar25281632016-01-21 23:32:32 +01007812{
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00007813 oparg_T oa;
Bram Moolenaar905dd902019-04-07 14:21:47 +02007814 int c;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00007815
Bram Moolenaar905dd902019-04-07 14:21:47 +02007816 // When calling vpeekc() from feedkeys() it will return Ctrl_C when there
7817 // is nothing to get, so also check for Ctrl_C.
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00007818 clear_oparg(&oa);
7819 finish_op = FALSE;
Bram Moolenaar586c70c2018-10-02 16:23:58 +02007820 while ((!stuff_empty()
7821 || ((was_typed || !typebuf_typed()) && typebuf.tb_len > 0)
Bram Moolenaar905dd902019-04-07 14:21:47 +02007822 || (use_vpeekc && (c = vpeekc()) != NUL && c != Ctrl_C))
Bram Moolenaar586c70c2018-10-02 16:23:58 +02007823 && !got_int)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00007824 {
7825 update_topline_cursor();
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02007826#ifdef FEAT_TERMINAL
Bram Moolenaar655a82a2018-05-08 22:01:07 +02007827 if (may_use_terminal_loop && term_use_loop()
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02007828 && oa.op_type == OP_NOP && oa.regname == NUL
7829 && !VIsual_active)
7830 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007831 // If terminal_loop() returns OK we got a key that is handled
7832 // in Normal model. With FAIL we first need to position the
7833 // cursor and the screen needs to be redrawn.
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02007834 if (terminal_loop(TRUE) == OK)
7835 normal_cmd(&oa, TRUE);
7836 }
7837 else
7838#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007839 // execute a Normal mode cmd
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02007840 normal_cmd(&oa, TRUE);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00007841 }
7842}
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00007843
Bram Moolenaar071d4272004-06-13 20:20:40 +00007844#ifdef FEAT_FIND_ID
7845 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007846ex_checkpath(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007847{
7848 find_pattern_in_path(NULL, 0, 0, FALSE, FALSE, CHECK_PATH, 1L,
7849 eap->forceit ? ACTION_SHOW_ALL : ACTION_SHOW,
7850 (linenr_T)1, (linenr_T)MAXLNUM);
7851}
7852
Bram Moolenaar4033c552017-09-16 20:54:51 +02007853#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007854/*
7855 * ":psearch"
7856 */
7857 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007858ex_psearch(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007859{
7860 g_do_tagpreview = p_pvh;
7861 ex_findpat(eap);
7862 g_do_tagpreview = 0;
7863}
7864#endif
7865
7866 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007867ex_findpat(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007868{
7869 int whole = TRUE;
7870 long n;
7871 char_u *p;
7872 int action;
7873
7874 switch (cmdnames[eap->cmdidx].cmd_name[2])
7875 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007876 case 'e': // ":psearch", ":isearch" and ":dsearch"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007877 if (cmdnames[eap->cmdidx].cmd_name[0] == 'p')
7878 action = ACTION_GOTO;
7879 else
7880 action = ACTION_SHOW;
7881 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007882 case 'i': // ":ilist" and ":dlist"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007883 action = ACTION_SHOW_ALL;
7884 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007885 case 'u': // ":ijump" and ":djump"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007886 action = ACTION_GOTO;
7887 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007888 default: // ":isplit" and ":dsplit"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007889 action = ACTION_SPLIT;
7890 break;
7891 }
7892
7893 n = 1;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007894 if (vim_isdigit(*eap->arg)) // get count
Bram Moolenaar071d4272004-06-13 20:20:40 +00007895 {
7896 n = getdigits(&eap->arg);
7897 eap->arg = skipwhite(eap->arg);
7898 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007899 if (*eap->arg == '/') // Match regexp, not just whole words
Bram Moolenaar071d4272004-06-13 20:20:40 +00007900 {
7901 whole = FALSE;
7902 ++eap->arg;
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02007903 p = skip_regexp(eap->arg, '/', p_magic);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007904 if (*p)
7905 {
7906 *p++ = NUL;
7907 p = skipwhite(p);
7908
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007909 // Check for trailing illegal characters
Bram Moolenaarfaac4102020-04-20 17:46:14 +02007910 if (!ends_excmd2(eap->arg, p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007911 eap->errmsg = e_trailing;
7912 else
7913 eap->nextcmd = check_nextcmd(p);
7914 }
7915 }
7916 if (!eap->skip)
7917 find_pattern_in_path(eap->arg, 0, (int)STRLEN(eap->arg),
7918 whole, !eap->forceit,
7919 *eap->cmd == 'd' ? FIND_DEFINE : FIND_ANY,
7920 n, action, eap->line1, eap->line2);
7921}
7922#endif
7923
Bram Moolenaar071d4272004-06-13 20:20:40 +00007924
Bram Moolenaar4033c552017-09-16 20:54:51 +02007925#ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00007926/*
7927 * ":ptag", ":ptselect", ":ptjump", ":ptnext", etc.
7928 */
7929 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007930ex_ptag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007931{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007932 g_do_tagpreview = p_pvh; // will be reset to 0 in ex_tag_cmd()
Bram Moolenaar071d4272004-06-13 20:20:40 +00007933 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
7934}
7935
7936/*
7937 * ":pedit"
7938 */
7939 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007940ex_pedit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007941{
7942 win_T *curwin_save = curwin;
7943
Bram Moolenaar3a2505c2020-03-09 16:40:41 +01007944 if (ERROR_IF_ANY_POPUP_WINDOW)
7945 return;
7946
Bram Moolenaar026587b2019-08-17 15:08:00 +02007947 // Open the preview window or popup and make it the current window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007948 g_do_tagpreview = p_pvh;
Bram Moolenaar576a4a62019-08-18 15:25:17 +02007949 prepare_tagpreview(TRUE, TRUE, FALSE);
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02007950
Bram Moolenaar026587b2019-08-17 15:08:00 +02007951 // Edit the file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007952 do_exedit(eap, NULL);
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02007953
Bram Moolenaar071d4272004-06-13 20:20:40 +00007954 if (curwin != curwin_save && win_valid(curwin_save))
7955 {
Bram Moolenaar026587b2019-08-17 15:08:00 +02007956 // Return cursor to where we were
Bram Moolenaar071d4272004-06-13 20:20:40 +00007957 validate_cursor();
7958 redraw_later(VALID);
7959 win_enter(curwin_save, TRUE);
7960 }
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01007961# ifdef FEAT_PROP_POPUP
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02007962 else if (WIN_IS_POPUP(curwin))
7963 {
7964 // can't keep focus in popup window
7965 win_enter(firstwin, TRUE);
7966 }
7967# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007968 g_do_tagpreview = 0;
7969}
Bram Moolenaar4033c552017-09-16 20:54:51 +02007970#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007971
7972/*
7973 * ":stag", ":stselect" and ":stjump".
7974 */
7975 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007976ex_stag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007977{
7978 postponed_split = -1;
7979 postponed_split_flags = cmdmod.split;
Bram Moolenaard326ce82007-03-11 14:48:29 +00007980 postponed_split_tab = cmdmod.tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007981 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
7982 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00007983 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007984}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007985
7986/*
7987 * ":tag", ":tselect", ":tjump", ":tnext", etc.
7988 */
7989 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007990ex_tag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007991{
7992 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name);
7993}
7994
7995 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007996ex_tag_cmd(exarg_T *eap, char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007997{
7998 int cmd;
7999
8000 switch (name[1])
8001 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008002 case 'j': cmd = DT_JUMP; // ":tjump"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008003 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008004 case 's': cmd = DT_SELECT; // ":tselect"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008005 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008006 case 'p': cmd = DT_PREV; // ":tprevious"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008007 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008008 case 'N': cmd = DT_PREV; // ":tNext"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008009 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008010 case 'n': cmd = DT_NEXT; // ":tnext"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008011 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008012 case 'o': cmd = DT_POP; // ":pop"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008013 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008014 case 'f': // ":tfirst"
8015 case 'r': cmd = DT_FIRST; // ":trewind"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008016 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008017 case 'l': cmd = DT_LAST; // ":tlast"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008018 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008019 default: // ":tag"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008020#ifdef FEAT_CSCOPE
Bram Moolenaar7c94c262008-06-20 09:11:34 +00008021 if (p_cst && *eap->arg != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008022 {
Bram Moolenaard4db7712016-11-12 19:16:46 +01008023 ex_cstag(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008024 return;
8025 }
8026#endif
8027 cmd = DT_TAG;
8028 break;
8029 }
8030
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00008031 if (name[0] == 'l')
8032 {
8033#ifndef FEAT_QUICKFIX
8034 ex_ni(eap);
8035 return;
8036#else
8037 cmd = DT_LTAG;
8038#endif
8039 }
8040
Bram Moolenaar071d4272004-06-13 20:20:40 +00008041 do_tag(eap->arg, cmd, eap->addr_count > 0 ? (int)eap->line2 : 1,
8042 eap->forceit, TRUE);
8043}
8044
8045/*
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008046 * Check "str" for starting with a special cmdline variable.
8047 * If found return one of the SPEC_ values and set "*usedlen" to the length of
8048 * the variable. Otherwise return -1 and "*usedlen" is unchanged.
8049 */
8050 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008051find_cmdline_var(char_u *src, int *usedlen)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008052{
8053 int len;
8054 int i;
Bram Moolenaar8f0b2d42009-05-16 14:41:10 +00008055 static char *(spec_str[]) = {
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008056 "%",
8057#define SPEC_PERC 0
8058 "#",
Bram Moolenaar65f08472017-09-10 18:16:20 +02008059#define SPEC_HASH (SPEC_PERC + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008060 "<cword>", // cursor word
Bram Moolenaar65f08472017-09-10 18:16:20 +02008061#define SPEC_CWORD (SPEC_HASH + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008062 "<cWORD>", // cursor WORD
Bram Moolenaar65f08472017-09-10 18:16:20 +02008063#define SPEC_CCWORD (SPEC_CWORD + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008064 "<cexpr>", // expr under cursor
Bram Moolenaar65f08472017-09-10 18:16:20 +02008065#define SPEC_CEXPR (SPEC_CCWORD + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008066 "<cfile>", // cursor path name
Bram Moolenaar65f08472017-09-10 18:16:20 +02008067#define SPEC_CFILE (SPEC_CEXPR + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008068 "<sfile>", // ":so" file name
Bram Moolenaar65f08472017-09-10 18:16:20 +02008069#define SPEC_SFILE (SPEC_CFILE + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008070 "<slnum>", // ":so" file line number
Bram Moolenaar65f08472017-09-10 18:16:20 +02008071#define SPEC_SLNUM (SPEC_SFILE + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008072 "<afile>", // autocommand file name
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008073#define SPEC_AFILE (SPEC_SLNUM + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008074 "<abuf>", // autocommand buffer number
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008075#define SPEC_ABUF (SPEC_AFILE + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008076 "<amatch>", // autocommand match name
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01008077#define SPEC_AMATCH (SPEC_ABUF + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008078 "<sflnum>", // script file line number
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008079#define SPEC_SFLNUM (SPEC_AMATCH + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008080#ifdef FEAT_CLIENTSERVER
8081 "<client>"
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008082# define SPEC_CLIENT (SPEC_SFLNUM + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008083#endif
8084 };
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008085
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00008086 for (i = 0; i < (int)(sizeof(spec_str) / sizeof(char *)); ++i)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008087 {
8088 len = (int)STRLEN(spec_str[i]);
8089 if (STRNCMP(src, spec_str[i], len) == 0)
8090 {
8091 *usedlen = len;
8092 return i;
8093 }
8094 }
8095 return -1;
8096}
8097
8098/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008099 * Evaluate cmdline variables.
8100 *
8101 * change '%' to curbuf->b_ffname
8102 * '#' to curwin->w_altfile
8103 * '<cword>' to word under the cursor
8104 * '<cWORD>' to WORD under the cursor
Bram Moolenaar8071cb22019-07-12 17:58:01 +02008105 * '<cexpr>' to C-expression under the cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00008106 * '<cfile>' to path name under the cursor
8107 * '<sfile>' to sourced file name
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01008108 * '<slnum>' to sourced file line number
Bram Moolenaar071d4272004-06-13 20:20:40 +00008109 * '<afile>' to file name for autocommand
8110 * '<abuf>' to buffer number for autocommand
8111 * '<amatch>' to matching name for autocommand
8112 *
8113 * When an error is detected, "errormsg" is set to a non-NULL pointer (may be
8114 * "" for error without a message) and NULL is returned.
8115 * Returns an allocated string if a valid match was found.
8116 * Returns NULL if no match was found. "usedlen" then still contains the
8117 * number of characters to skip.
8118 */
8119 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008120eval_vars(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008121 char_u *src, // pointer into commandline
8122 char_u *srcstart, // beginning of valid memory for src
8123 int *usedlen, // characters after src that are used
8124 linenr_T *lnump, // line number for :e command, or NULL
8125 char **errormsg, // pointer to error message
8126 int *escaped) // return value has escaped white space (can
8127 // be NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008128{
8129 int i;
8130 char_u *s;
8131 char_u *result;
8132 char_u *resultbuf = NULL;
8133 int resultlen;
8134 buf_T *buf;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008135 int valid = VALID_HEAD + VALID_PATH; // assume valid result
Bram Moolenaar071d4272004-06-13 20:20:40 +00008136 int spec_idx;
Bram Moolenaarfd249462018-07-28 16:14:30 +02008137 int tilde_file = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008138 int skip_mod = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008139 char_u strbuf[30];
Bram Moolenaar071d4272004-06-13 20:20:40 +00008140
8141 *errormsg = NULL;
Bram Moolenaar63b92542007-03-27 14:57:09 +00008142 if (escaped != NULL)
8143 *escaped = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008144
8145 /*
8146 * Check if there is something to do.
8147 */
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008148 spec_idx = find_cmdline_var(src, usedlen);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008149 if (spec_idx < 0) // no match
Bram Moolenaar071d4272004-06-13 20:20:40 +00008150 {
8151 *usedlen = 1;
8152 return NULL;
8153 }
8154
8155 /*
8156 * Skip when preceded with a backslash "\%" and "\#".
8157 * Note: In "\\%" the % is also not recognized!
8158 */
8159 if (src > srcstart && src[-1] == '\\')
8160 {
8161 *usedlen = 0;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008162 STRMOVE(src - 1, src); // remove backslash
Bram Moolenaar071d4272004-06-13 20:20:40 +00008163 return NULL;
8164 }
8165
8166 /*
8167 * word or WORD under cursor
8168 */
Bram Moolenaar65f08472017-09-10 18:16:20 +02008169 if (spec_idx == SPEC_CWORD || spec_idx == SPEC_CCWORD
8170 || spec_idx == SPEC_CEXPR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008171 {
Bram Moolenaar65f08472017-09-10 18:16:20 +02008172 resultlen = find_ident_under_cursor(&result,
8173 spec_idx == SPEC_CWORD ? (FIND_IDENT | FIND_STRING)
8174 : spec_idx == SPEC_CEXPR ? (FIND_IDENT | FIND_STRING | FIND_EVAL)
8175 : FIND_STRING);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008176 if (resultlen == 0)
8177 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008178 *errormsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +00008179 return NULL;
8180 }
8181 }
8182
8183 /*
8184 * '#': Alternate file name
8185 * '%': Current file name
8186 * File name under the cursor
8187 * File name for autocommand
8188 * and following modifiers
8189 */
8190 else
8191 {
8192 switch (spec_idx)
8193 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008194 case SPEC_PERC: // '%': current file
Bram Moolenaar071d4272004-06-13 20:20:40 +00008195 if (curbuf->b_fname == NULL)
8196 {
8197 result = (char_u *)"";
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008198 valid = 0; // Must have ":p:h" to be valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00008199 }
8200 else
Bram Moolenaar00136dc2018-07-25 21:19:13 +02008201 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008202 result = curbuf->b_fname;
Bram Moolenaar00136dc2018-07-25 21:19:13 +02008203 tilde_file = STRCMP(result, "~") == 0;
8204 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008205 break;
8206
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008207 case SPEC_HASH: // '#' or "#99": alternate file
8208 if (src[1] == '#') // "##": the argument list
Bram Moolenaar071d4272004-06-13 20:20:40 +00008209 {
8210 result = arg_all();
8211 resultbuf = result;
8212 *usedlen = 2;
Bram Moolenaar63b92542007-03-27 14:57:09 +00008213 if (escaped != NULL)
8214 *escaped = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008215 skip_mod = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008216 break;
8217 }
8218 s = src + 1;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008219 if (*s == '<') // "#<99" uses v:oldfiles
Bram Moolenaard812df62008-11-09 12:46:09 +00008220 ++s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008221 i = (int)getdigits(&s);
Bram Moolenaarc312b8b2017-10-28 17:53:04 +02008222 if (s == src + 2 && src[1] == '-')
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008223 // just a minus sign, don't skip over it
Bram Moolenaarc312b8b2017-10-28 17:53:04 +02008224 s--;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008225 *usedlen = (int)(s - src); // length of what we expand
Bram Moolenaar071d4272004-06-13 20:20:40 +00008226
Bram Moolenaarc312b8b2017-10-28 17:53:04 +02008227 if (src[1] == '<' && i != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008228 {
Bram Moolenaard812df62008-11-09 12:46:09 +00008229 if (*usedlen < 2)
8230 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008231 // Should we give an error message for #<text?
Bram Moolenaard812df62008-11-09 12:46:09 +00008232 *usedlen = 1;
8233 return NULL;
8234 }
8235#ifdef FEAT_EVAL
8236 result = list_find_str(get_vim_var_list(VV_OLDFILES),
8237 (long)i);
8238 if (result == NULL)
8239 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008240 *errormsg = "";
Bram Moolenaard812df62008-11-09 12:46:09 +00008241 return NULL;
8242 }
8243#else
Bram Moolenaar8e481e82019-01-15 20:07:48 +01008244 *errormsg = _("E809: #< is not available without the +eval feature");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008245 return NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +00008246#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008247 }
8248 else
Bram Moolenaard812df62008-11-09 12:46:09 +00008249 {
Bram Moolenaarc312b8b2017-10-28 17:53:04 +02008250 if (i == 0 && src[1] == '<' && *usedlen > 1)
8251 *usedlen = 1;
Bram Moolenaard812df62008-11-09 12:46:09 +00008252 buf = buflist_findnr(i);
8253 if (buf == NULL)
8254 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008255 *errormsg = _("E194: No alternate file name to substitute for '#'");
Bram Moolenaard812df62008-11-09 12:46:09 +00008256 return NULL;
8257 }
8258 if (lnump != NULL)
8259 *lnump = ECMD_LAST;
8260 if (buf->b_fname == NULL)
8261 {
8262 result = (char_u *)"";
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008263 valid = 0; // Must have ":p:h" to be valid
Bram Moolenaard812df62008-11-09 12:46:09 +00008264 }
8265 else
Bram Moolenaar00136dc2018-07-25 21:19:13 +02008266 {
Bram Moolenaard812df62008-11-09 12:46:09 +00008267 result = buf->b_fname;
Bram Moolenaar00136dc2018-07-25 21:19:13 +02008268 tilde_file = STRCMP(result, "~") == 0;
8269 }
Bram Moolenaard812df62008-11-09 12:46:09 +00008270 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008271 break;
8272
8273#ifdef FEAT_SEARCHPATH
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008274 case SPEC_CFILE: // file name under cursor
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008275 result = file_name_at_cursor(FNAME_MESS|FNAME_HYP, 1L, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008276 if (result == NULL)
8277 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008278 *errormsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +00008279 return NULL;
8280 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008281 resultbuf = result; // remember allocated string
Bram Moolenaar071d4272004-06-13 20:20:40 +00008282 break;
8283#endif
8284
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008285 case SPEC_AFILE: // file name for autocommand
Bram Moolenaar071d4272004-06-13 20:20:40 +00008286 result = autocmd_fname;
Bram Moolenaarf6dad432008-09-18 19:29:58 +00008287 if (result != NULL && !autocmd_fname_full)
8288 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008289 // Still need to turn the fname into a full path. It is
8290 // postponed to avoid a delay when <afile> is not used.
Bram Moolenaarf6dad432008-09-18 19:29:58 +00008291 autocmd_fname_full = TRUE;
8292 result = FullName_save(autocmd_fname, FALSE);
8293 vim_free(autocmd_fname);
8294 autocmd_fname = result;
8295 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008296 if (result == NULL)
8297 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008298 *errormsg = _("E495: no autocommand file name to substitute for \"<afile>\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008299 return NULL;
8300 }
Bram Moolenaara0174af2008-01-02 20:08:25 +00008301 result = shorten_fname1(result);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008302 break;
8303
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008304 case SPEC_ABUF: // buffer number for autocommand
Bram Moolenaar071d4272004-06-13 20:20:40 +00008305 if (autocmd_bufnr <= 0)
8306 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008307 *errormsg = _("E496: no autocommand buffer number to substitute for \"<abuf>\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008308 return NULL;
8309 }
8310 sprintf((char *)strbuf, "%d", autocmd_bufnr);
8311 result = strbuf;
8312 break;
8313
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008314 case SPEC_AMATCH: // match name for autocommand
Bram Moolenaar071d4272004-06-13 20:20:40 +00008315 result = autocmd_match;
8316 if (result == NULL)
8317 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008318 *errormsg = _("E497: no autocommand match name to substitute for \"<amatch>\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008319 return NULL;
8320 }
8321 break;
8322
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008323 case SPEC_SFILE: // file name for ":so" command
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01008324 result = estack_sfile();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008325 if (result == NULL)
8326 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008327 *errormsg = _("E498: no :source file name to substitute for \"<sfile>\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008328 return NULL;
8329 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01008330 resultbuf = result; // remember allocated string
Bram Moolenaar071d4272004-06-13 20:20:40 +00008331 break;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008332
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008333 case SPEC_SLNUM: // line in file for ":so" command
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01008334 if (SOURCING_NAME == NULL || SOURCING_LNUM == 0)
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01008335 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008336 *errormsg = _("E842: no line number to use for \"<slnum>\"");
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01008337 return NULL;
8338 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01008339 sprintf((char *)strbuf, "%ld", SOURCING_LNUM);
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01008340 result = strbuf;
8341 break;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008342
8343#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008344 case SPEC_SFLNUM: // line in script file
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01008345 if (current_sctx.sc_lnum + SOURCING_LNUM == 0)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008346 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008347 *errormsg = _("E961: no line number to use for \"<sflnum>\"");
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008348 return NULL;
8349 }
8350 sprintf((char *)strbuf, "%ld",
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01008351 (long)(current_sctx.sc_lnum + SOURCING_LNUM));
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008352 result = strbuf;
8353 break;
8354#endif
8355
8356#ifdef FEAT_CLIENTSERVER
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008357 case SPEC_CLIENT: // Source of last submitted input
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008358 sprintf((char *)strbuf, PRINTF_HEX_LONG_U,
8359 (long_u)clientWindow);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008360 result = strbuf;
8361 break;
8362#endif
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008363
Bram Moolenaar9e0f6ec2017-05-16 09:36:54 +02008364 default:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008365 result = (char_u *)""; // avoid gcc warning
Bram Moolenaar9e0f6ec2017-05-16 09:36:54 +02008366 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008367 }
8368
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008369 resultlen = (int)STRLEN(result); // length of new string
8370 if (src[*usedlen] == '<') // remove the file name extension
Bram Moolenaar071d4272004-06-13 20:20:40 +00008371 {
8372 ++*usedlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008373 if ((s = vim_strrchr(result, '.')) != NULL && s >= gettail(result))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008374 resultlen = (int)(s - result);
8375 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008376 else if (!skip_mod)
8377 {
Bram Moolenaar00136dc2018-07-25 21:19:13 +02008378 valid |= modify_fname(src, tilde_file, usedlen, &result, &resultbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008379 &resultlen);
8380 if (result == NULL)
8381 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008382 *errormsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +00008383 return NULL;
8384 }
8385 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008386 }
8387
8388 if (resultlen == 0 || valid != VALID_HEAD + VALID_PATH)
8389 {
8390 if (valid != VALID_HEAD + VALID_PATH)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008391 // xgettext:no-c-format
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008392 *errormsg = _("E499: Empty file name for '%' or '#', only works with \":p:h\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008393 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008394 *errormsg = _("E500: Evaluates to an empty string");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008395 result = NULL;
8396 }
8397 else
8398 result = vim_strnsave(result, resultlen);
8399 vim_free(resultbuf);
8400 return result;
8401}
8402
8403/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008404 * Expand the <sfile> string in "arg".
8405 *
8406 * Returns an allocated string, or NULL for any error.
8407 */
8408 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008409expand_sfile(char_u *arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008410{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008411 char *errormsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008412 int len;
8413 char_u *result;
8414 char_u *newres;
8415 char_u *repl;
8416 int srclen;
8417 char_u *p;
8418
8419 result = vim_strsave(arg);
8420 if (result == NULL)
8421 return NULL;
8422
8423 for (p = result; *p; )
8424 {
8425 if (STRNCMP(p, "<sfile>", 7) != 0)
8426 ++p;
8427 else
8428 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008429 // replace "<sfile>" with the sourced file name, and do ":" stuff
Bram Moolenaar63b92542007-03-27 14:57:09 +00008430 repl = eval_vars(p, result, &srclen, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008431 if (errormsg != NULL)
8432 {
8433 if (*errormsg)
8434 emsg(errormsg);
8435 vim_free(result);
8436 return NULL;
8437 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008438 if (repl == NULL) // no match (cannot happen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008439 {
8440 p += srclen;
8441 continue;
8442 }
8443 len = (int)STRLEN(result) - srclen + (int)STRLEN(repl) + 1;
8444 newres = alloc(len);
8445 if (newres == NULL)
8446 {
8447 vim_free(repl);
8448 vim_free(result);
8449 return NULL;
8450 }
8451 mch_memmove(newres, result, (size_t)(p - result));
8452 STRCPY(newres + (p - result), repl);
8453 len = (int)STRLEN(newres);
8454 STRCAT(newres, p + srclen);
8455 vim_free(repl);
8456 vim_free(result);
8457 result = newres;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008458 p = newres + len; // continue after the match
Bram Moolenaar071d4272004-06-13 20:20:40 +00008459 }
8460 }
8461
8462 return result;
8463}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008464
Bram Moolenaar071d4272004-06-13 20:20:40 +00008465#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG) || defined(PROTO)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00008466/*
Bram Moolenaard9462e32011-04-11 21:35:11 +02008467 * Make a dialog message in "buff[DIALOG_MSG_SIZE]".
Bram Moolenaarb765d632005-06-07 21:00:02 +00008468 * "format" must contain "%s".
Bram Moolenaar9c13b352005-05-19 20:53:52 +00008469 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008470 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008471dialog_msg(char_u *buff, char *format, char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008472{
Bram Moolenaar071d4272004-06-13 20:20:40 +00008473 if (fname == NULL)
8474 fname = (char_u *)_("Untitled");
Bram Moolenaard9462e32011-04-11 21:35:11 +02008475 vim_snprintf((char *)buff, DIALOG_MSG_SIZE, format, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008476}
8477#endif
8478
8479/*
8480 * ":behave {mswin,xterm}"
8481 */
8482 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008483ex_behave(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008484{
8485 if (STRCMP(eap->arg, "mswin") == 0)
8486 {
8487 set_option_value((char_u *)"selection", 0L, (char_u *)"exclusive", 0);
8488 set_option_value((char_u *)"selectmode", 0L, (char_u *)"mouse,key", 0);
8489 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"popup", 0);
8490 set_option_value((char_u *)"keymodel", 0L,
8491 (char_u *)"startsel,stopsel", 0);
8492 }
8493 else if (STRCMP(eap->arg, "xterm") == 0)
8494 {
8495 set_option_value((char_u *)"selection", 0L, (char_u *)"inclusive", 0);
8496 set_option_value((char_u *)"selectmode", 0L, (char_u *)"", 0);
8497 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"extend", 0);
8498 set_option_value((char_u *)"keymodel", 0L, (char_u *)"", 0);
8499 }
8500 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008501 semsg(_(e_invarg2), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008502}
8503
Bram Moolenaar071d4272004-06-13 20:20:40 +00008504static int filetype_detect = FALSE;
8505static int filetype_plugin = FALSE;
8506static int filetype_indent = FALSE;
8507
8508/*
8509 * ":filetype [plugin] [indent] {on,off,detect}"
8510 * on: Load the filetype.vim file to install autocommands for file types.
8511 * off: Load the ftoff.vim file to remove all autocommands for file types.
8512 * plugin on: load filetype.vim and ftplugin.vim
8513 * plugin off: load ftplugof.vim
8514 * indent on: load filetype.vim and indent.vim
8515 * indent off: load indoff.vim
8516 */
8517 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008518ex_filetype(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008519{
8520 char_u *arg = eap->arg;
8521 int plugin = FALSE;
8522 int indent = FALSE;
8523
8524 if (*eap->arg == NUL)
8525 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008526 // Print current status.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008527 smsg("filetype detection:%s plugin:%s indent:%s",
Bram Moolenaar071d4272004-06-13 20:20:40 +00008528 filetype_detect ? "ON" : "OFF",
8529 filetype_plugin ? (filetype_detect ? "ON" : "(on)") : "OFF",
8530 filetype_indent ? (filetype_detect ? "ON" : "(on)") : "OFF");
8531 return;
8532 }
8533
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008534 // Accept "plugin" and "indent" in any order.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008535 for (;;)
8536 {
8537 if (STRNCMP(arg, "plugin", 6) == 0)
8538 {
8539 plugin = TRUE;
8540 arg = skipwhite(arg + 6);
8541 continue;
8542 }
8543 if (STRNCMP(arg, "indent", 6) == 0)
8544 {
8545 indent = TRUE;
8546 arg = skipwhite(arg + 6);
8547 continue;
8548 }
8549 break;
8550 }
8551 if (STRCMP(arg, "on") == 0 || STRCMP(arg, "detect") == 0)
8552 {
8553 if (*arg == 'o' || !filetype_detect)
8554 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01008555 source_runtime((char_u *)FILETYPE_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008556 filetype_detect = TRUE;
8557 if (plugin)
8558 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01008559 source_runtime((char_u *)FTPLUGIN_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008560 filetype_plugin = TRUE;
8561 }
8562 if (indent)
8563 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01008564 source_runtime((char_u *)INDENT_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008565 filetype_indent = TRUE;
8566 }
8567 }
8568 if (*arg == 'd')
8569 {
Bram Moolenaar1610d052016-06-09 22:53:01 +02008570 (void)do_doautocmd((char_u *)"filetypedetect BufRead", TRUE, NULL);
Bram Moolenaara3227e22006-03-08 21:32:40 +00008571 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008572 }
8573 }
8574 else if (STRCMP(arg, "off") == 0)
8575 {
8576 if (plugin || indent)
8577 {
8578 if (plugin)
8579 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01008580 source_runtime((char_u *)FTPLUGOF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008581 filetype_plugin = FALSE;
8582 }
8583 if (indent)
8584 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01008585 source_runtime((char_u *)INDOFF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008586 filetype_indent = FALSE;
8587 }
8588 }
8589 else
8590 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01008591 source_runtime((char_u *)FTOFF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008592 filetype_detect = FALSE;
8593 }
8594 }
8595 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008596 semsg(_(e_invarg2), arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008597}
8598
8599/*
Bram Moolenaar3e545692017-06-04 19:00:32 +02008600 * ":setfiletype [FALLBACK] {name}"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008601 */
8602 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008603ex_setfiletype(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008604{
8605 if (!did_filetype)
Bram Moolenaar3e545692017-06-04 19:00:32 +02008606 {
8607 char_u *arg = eap->arg;
8608
8609 if (STRNCMP(arg, "FALLBACK ", 9) == 0)
8610 arg += 9;
8611
8612 set_option_value((char_u *)"filetype", 0L, arg, OPT_LOCAL);
8613 if (arg != eap->arg)
8614 did_filetype = FALSE;
8615 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008616}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008617
Bram Moolenaar071d4272004-06-13 20:20:40 +00008618 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008619ex_digraphs(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008620{
8621#ifdef FEAT_DIGRAPHS
8622 if (*eap->arg != NUL)
8623 putdigraph(eap->arg);
8624 else
Bram Moolenaareae8ae12018-12-14 18:53:02 +01008625 listdigraphs(eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008626#else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008627 emsg(_("E196: No digraphs in this version"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008628#endif
8629}
8630
Bram Moolenaar451fc7b2018-04-27 22:53:07 +02008631#if defined(FEAT_SEARCH_EXTRA) || defined(PROTO)
8632 void
8633set_no_hlsearch(int flag)
8634{
8635 no_hlsearch = flag;
8636# ifdef FEAT_EVAL
8637 set_vim_var_nr(VV_HLSEARCH, !no_hlsearch && p_hls);
8638# endif
8639}
8640
Bram Moolenaar071d4272004-06-13 20:20:40 +00008641/*
8642 * ":nohlsearch"
8643 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008644 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008645ex_nohlsearch(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008646{
Bram Moolenaar451fc7b2018-04-27 22:53:07 +02008647 set_no_hlsearch(TRUE);
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00008648 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008649}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008650#endif
8651
8652#ifdef FEAT_CRYPT
8653/*
8654 * ":X": Get crypt key
8655 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008656 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008657ex_X(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008658{
Bram Moolenaar3a0c9082014-11-12 15:15:42 +01008659 crypt_check_current_method();
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02008660 (void)crypt_get_key(TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008661}
8662#endif
8663
8664#ifdef FEAT_FOLDING
8665 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008666ex_fold(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008667{
8668 if (foldManualAllowed(TRUE))
8669 foldCreate(eap->line1, eap->line2);
8670}
8671
8672 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008673ex_foldopen(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008674{
8675 opFoldRange(eap->line1, eap->line2, eap->cmdidx == CMD_foldopen,
8676 eap->forceit, FALSE);
8677}
8678
8679 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008680ex_folddo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008681{
8682 linenr_T lnum;
8683
Bram Moolenaar4facea32019-10-12 20:17:40 +02008684# ifdef FEAT_CLIPBOARD
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02008685 start_global_changes();
Bram Moolenaar4facea32019-10-12 20:17:40 +02008686# endif
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02008687
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008688 // First set the marks for all lines closed/open.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008689 for (lnum = eap->line1; lnum <= eap->line2; ++lnum)
8690 if (hasFolding(lnum, NULL, NULL) == (eap->cmdidx == CMD_folddoclosed))
8691 ml_setmarked(lnum);
8692
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008693 // Execute the command on the marked lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008694 global_exe(eap->arg);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008695 ml_clearmarked(); // clear rest of the marks
Bram Moolenaar4facea32019-10-12 20:17:40 +02008696# ifdef FEAT_CLIPBOARD
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02008697 end_global_changes();
Bram Moolenaar4facea32019-10-12 20:17:40 +02008698# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008699}
8700#endif
Bram Moolenaarf5291f32017-09-14 22:55:37 +02008701
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008702#if defined(FEAT_QUICKFIX) || defined(PROTO)
Bram Moolenaar39665952018-08-15 20:59:48 +02008703/*
8704 * Returns TRUE if the supplied Ex cmdidx is for a location list command
8705 * instead of a quickfix command.
8706 */
8707 int
8708is_loclist_cmd(int cmdidx)
8709{
Bram Moolenaar74c8be22018-08-23 22:51:40 +02008710 if (cmdidx < 0 || cmdidx >= CMD_SIZE)
Bram Moolenaar39665952018-08-15 20:59:48 +02008711 return FALSE;
8712 return cmdnames[cmdidx].cmd_name[0] == 'l';
8713}
8714#endif
8715
Bram Moolenaar4facea32019-10-12 20:17:40 +02008716#if defined(FEAT_TIMERS) || defined(PROTO)
Bram Moolenaarf5291f32017-09-14 22:55:37 +02008717 int
8718get_pressedreturn(void)
8719{
8720 return ex_pressedreturn;
8721}
8722
8723 void
8724set_pressedreturn(int val)
8725{
8726 ex_pressedreturn = val;
8727}
8728#endif