blob: 90f3a6dc2990704a23da4a2d449608ee4a3239bd [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;
637 struct msglist **saved_msg_list = NULL;
638 struct msglist *private_msg_list;
639
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 Moolenaar1a47ae32019-12-29 23:04:25 +0100970 if (p_verbose >= 15 && SOURCING_NAME != NULL)
971 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 Moolenaar071d4272004-06-13 20:20:40 +00001241 struct msglist *messages = NULL, *next;
1242
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 Moolenaar4facea32019-10-12 20:17:40 +02001694 if (p_verbose >= 16)
1695 msg_verbose_cmd(0, *cmdlinep);
1696
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02001697/*
1698 * 1. Skip comment lines and leading white space and colons.
1699 * 2. Handle command modifiers.
1700 */
1701 // The "ea" structure holds the arguments that can be used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001702 ea.cmd = *cmdlinep;
Bram Moolenaareffed932018-08-14 13:38:17 +02001703 ea.cmdlinep = cmdlinep;
1704 ea.getline = fgetline;
1705 ea.cookie = cookie;
1706#ifdef FEAT_EVAL
1707 ea.cstack = cstack;
Bram Moolenaar83f37b92020-02-23 14:35:01 +01001708 starts_with_colon = *skipwhite(ea.cmd) == ':';
Bram Moolenaar071d4272004-06-13 20:20:40 +00001709#endif
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02001710 if (parse_command_modifiers(&ea, &errormsg, FALSE) == FAIL)
Bram Moolenaareffed932018-08-14 13:38:17 +02001711 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001712
Bram Moolenaar7b668e82016-08-23 23:51:21 +02001713 after_modifier = ea.cmd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001714
1715#ifdef FEAT_EVAL
1716 ea.skip = did_emsg || got_int || did_throw || (cstack->cs_idx >= 0
1717 && !(cstack->cs_flags[cstack->cs_idx] & CSF_ACTIVE));
1718#else
1719 ea.skip = (if_level > 0);
1720#endif
1721
Bram Moolenaar071d4272004-06-13 20:20:40 +00001722/*
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001723 * 3. Skip over the range to find the command. Let "p" point to after it.
1724 *
1725 * We need the command to know what kind of range it uses.
1726 */
1727 cmd = ea.cmd;
1728 ea.cmd = skip_range(ea.cmd, NULL);
1729 if (*ea.cmd == '*' && vim_strchr(p_cpo, CPO_STAR) == NULL)
1730 ea.cmd = skipwhite(ea.cmd + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001731
1732#ifdef FEAT_EVAL
Bram Moolenaar83f37b92020-02-23 14:35:01 +01001733 if (current_sctx.sc_version == SCRIPT_VERSION_VIM9 && !starts_with_colon)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001734 p = find_ex_command(&ea, NULL, lookup_scriptvar, NULL);
1735 else
1736#endif
1737 p = find_ex_command(&ea, NULL, NULL, NULL);
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001738
Bram Moolenaar7feb35e2018-08-21 17:49:54 +02001739#ifdef FEAT_EVAL
1740# ifdef FEAT_PROFILE
1741 // Count this line for profiling if skip is TRUE.
1742 if (do_profiling == PROF_YES
1743 && (!ea.skip || cstack->cs_idx == 0 || (cstack->cs_idx > 0
1744 && (cstack->cs_flags[cstack->cs_idx - 1] & CSF_ACTIVE))))
1745 {
1746 int skip = did_emsg || got_int || did_throw;
1747
1748 if (ea.cmdidx == CMD_catch)
1749 skip = !skip && !(cstack->cs_idx >= 0
1750 && (cstack->cs_flags[cstack->cs_idx] & CSF_THROWN)
1751 && !(cstack->cs_flags[cstack->cs_idx] & CSF_CAUGHT));
1752 else if (ea.cmdidx == CMD_else || ea.cmdidx == CMD_elseif)
1753 skip = skip || !(cstack->cs_idx >= 0
1754 && !(cstack->cs_flags[cstack->cs_idx]
1755 & (CSF_ACTIVE | CSF_TRUE)));
1756 else if (ea.cmdidx == CMD_finally)
1757 skip = FALSE;
1758 else if (ea.cmdidx != CMD_endif
1759 && ea.cmdidx != CMD_endfor
1760 && ea.cmdidx != CMD_endtry
1761 && ea.cmdidx != CMD_endwhile)
1762 skip = ea.skip;
1763
1764 if (!skip)
1765 {
1766 if (getline_equal(fgetline, cookie, get_func_line))
1767 func_line_exec(getline_cookie(fgetline, cookie));
1768 else if (getline_equal(fgetline, cookie, getsourceline))
1769 script_line_exec();
1770 }
1771 }
1772# endif
1773
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001774 // May go to debug mode. If this happens and the ">quit" debug command is
1775 // used, throw an interrupt exception and skip the next command.
Bram Moolenaar7feb35e2018-08-21 17:49:54 +02001776 dbg_check_breakpoint(&ea);
1777 if (!ea.skip && got_int)
1778 {
1779 ea.skip = TRUE;
1780 (void)do_intthrow(cstack);
1781 }
1782#endif
1783
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001784/*
1785 * 4. parse a range specifier of the form: addr [,addr] [;addr] ..
Bram Moolenaar071d4272004-06-13 20:20:40 +00001786 *
1787 * where 'addr' is:
1788 *
1789 * % (entire file)
1790 * $ [+-NUM]
1791 * 'x [+-NUM] (where x denotes a currently defined mark)
1792 * . [+-NUM]
1793 * [+-NUM]..
1794 * NUM
1795 *
1796 * The ea.cmd pointer is updated to point to the first character following the
1797 * range spec. If an initial address is found, but no second, the upper bound
1798 * is equal to the lower.
1799 */
1800
Bram Moolenaar25190db2019-05-04 15:05:28 +02001801 // ea.addr_type for user commands is set by find_ucmd
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01001802 if (!IS_USER_CMDIDX(ea.cmdidx))
1803 {
1804 if (ea.cmdidx != CMD_SIZE)
1805 ea.addr_type = cmdnames[(int)ea.cmdidx].cmd_addr_type;
1806 else
1807 ea.addr_type = ADDR_LINES;
1808
Bram Moolenaar25190db2019-05-04 15:05:28 +02001809 // :wincmd range depends on the argument.
Bram Moolenaar164f3262015-01-14 21:22:01 +01001810 if (ea.cmdidx == CMD_wincmd && p != NULL)
1811 get_wincmd_addr_type(skipwhite(p), &ea);
Bram Moolenaar25190db2019-05-04 15:05:28 +02001812#ifdef FEAT_QUICKFIX
1813 // :.cc in quickfix window uses line number
1814 if ((ea.cmdidx == CMD_cc || ea.cmdidx == CMD_ll) && bt_quickfix(curbuf))
1815 ea.addr_type = ADDR_OTHER;
1816#endif
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01001817 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001818
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01001819 ea.cmd = cmd;
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02001820 if (parse_cmd_address(&ea, &errormsg, FALSE) == FAIL)
Bram Moolenaaree8415b2018-08-10 23:13:12 +02001821 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001822
Bram Moolenaar071d4272004-06-13 20:20:40 +00001823/*
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001824 * 5. Parse the command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001825 */
1826
1827 /*
1828 * Skip ':' and any white space
1829 */
1830 ea.cmd = skipwhite(ea.cmd);
1831 while (*ea.cmd == ':')
1832 ea.cmd = skipwhite(ea.cmd + 1);
1833
1834 /*
1835 * If we got a line, but no command, then go to the line.
1836 * If we find a '|' or '\n' we set ea.nextcmd.
1837 */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001838 if (*ea.cmd == NUL || *ea.cmd == '"'
Bram Moolenaar7a092242020-04-16 22:10:49 +02001839#ifdef FEAT_EVAL
Bram Moolenaara26b9702020-04-18 19:53:28 +02001840 || (*ea.cmd == '#' && ea.cmd[1] != '{'
1841 && !starts_with_colon && in_vim9script())
Bram Moolenaar7a092242020-04-16 22:10:49 +02001842#endif
1843 || (ea.nextcmd = check_nextcmd(ea.cmd)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001844 {
1845 /*
1846 * strange vi behaviour:
1847 * ":3" jumps to line 3
1848 * ":3|..." prints line 3
1849 * ":|" prints current line
1850 */
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001851 if (ea.skip) // skip this if inside :if
Bram Moolenaar071d4272004-06-13 20:20:40 +00001852 goto doend;
Bram Moolenaardf177f62005-02-22 08:39:57 +00001853 if (*ea.cmd == '|' || (exmode_active && ea.line1 != ea.line2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001854 {
1855 ea.cmdidx = CMD_print;
Bram Moolenaar8071cb22019-07-12 17:58:01 +02001856 ea.argt = EX_RANGE+EX_COUNT+EX_TRLBAR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001857 if ((errormsg = invalid_range(&ea)) == NULL)
1858 {
1859 correct_range(&ea);
1860 ex_print(&ea);
1861 }
1862 }
1863 else if (ea.addr_count != 0)
1864 {
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00001865 if (ea.line2 > curbuf->b_ml.ml_line_count)
1866 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001867 // With '-' in 'cpoptions' a line number past the file is an
1868 // error, otherwise put it at the end of the file.
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00001869 if (vim_strchr(p_cpo, CPO_MINUS) != NULL)
1870 ea.line2 = -1;
1871 else
1872 ea.line2 = curbuf->b_ml.ml_line_count;
1873 }
1874
1875 if (ea.line2 < 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001876 errormsg = _(e_invrange);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001877 else
1878 {
1879 if (ea.line2 == 0)
1880 curwin->w_cursor.lnum = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001881 else
1882 curwin->w_cursor.lnum = ea.line2;
1883 beginline(BL_SOL | BL_FIX);
1884 }
1885 }
1886 goto doend;
1887 }
1888
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001889 // If this looks like an undefined user command and there are CmdUndefined
1890 // autocommands defined, trigger the matching autocommands.
Bram Moolenaard5005162014-08-22 23:05:54 +02001891 if (p != NULL && ea.cmdidx == CMD_SIZE && !ea.skip
1892 && ASCII_ISUPPER(*ea.cmd)
1893 && has_cmdundefined())
1894 {
Bram Moolenaard5005162014-08-22 23:05:54 +02001895 int ret;
1896
Bram Moolenaar5a31b462014-08-23 14:16:20 +02001897 p = ea.cmd;
Bram Moolenaard5005162014-08-22 23:05:54 +02001898 while (ASCII_ISALNUM(*p))
1899 ++p;
Bram Moolenaar120f4a82014-09-09 12:22:06 +02001900 p = vim_strnsave(ea.cmd, (int)(p - ea.cmd));
Bram Moolenaard5005162014-08-22 23:05:54 +02001901 ret = apply_autocmds(EVENT_CMDUNDEFINED, p, p, TRUE, NULL);
1902 vim_free(p);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001903 // If the autocommands did something and didn't cause an error, try
1904 // finding the command again.
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001905 p = (ret
1906#ifdef FEAT_EVAL
1907 && !aborting()
Bram Moolenaard5005162014-08-22 23:05:54 +02001908#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001909 ) ? find_ex_command(&ea, NULL, NULL, NULL) : ea.cmd;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001910 }
Bram Moolenaard5005162014-08-22 23:05:54 +02001911
Bram Moolenaar071d4272004-06-13 20:20:40 +00001912 if (p == NULL)
1913 {
1914 if (!ea.skip)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001915 errormsg = _("E464: Ambiguous use of user-defined command");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001916 goto doend;
1917 }
Bram Moolenaarac9fb182019-04-27 13:04:13 +02001918 // Check for wrong commands.
Bram Moolenaar6f9a4762017-06-22 20:39:17 +02001919 if (*p == '!' && ea.cmd[1] == 0151 && ea.cmd[0] == 78
1920 && !IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001921 {
1922 errormsg = uc_fun_cmd();
1923 goto doend;
1924 }
Bram Moolenaarac9fb182019-04-27 13:04:13 +02001925
Bram Moolenaar071d4272004-06-13 20:20:40 +00001926 if (ea.cmdidx == CMD_SIZE)
1927 {
1928 if (!ea.skip)
1929 {
1930 STRCPY(IObuff, _("E492: Not an editor command"));
1931 if (!sourcing)
Bram Moolenaar7b668e82016-08-23 23:51:21 +02001932 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001933 // If the modifier was parsed OK the error must be in the
1934 // following command
Bram Moolenaar7b668e82016-08-23 23:51:21 +02001935 if (after_modifier != NULL)
1936 append_command(after_modifier);
1937 else
1938 append_command(*cmdlinep);
1939 }
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001940 errormsg = (char *)IObuff;
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001941 did_emsg_syntax = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001942 }
1943 goto doend;
1944 }
1945
Bram Moolenaar958636c2014-10-21 20:01:58 +02001946 ni = (!IS_USER_CMDIDX(ea.cmdidx)
1947 && (cmdnames[ea.cmdidx].cmd_func == ex_ni
Bram Moolenaar7bb75552007-07-16 18:39:49 +00001948#ifdef HAVE_EX_SCRIPT_NI
1949 || cmdnames[ea.cmdidx].cmd_func == ex_script_ni
1950#endif
1951 ));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001952
1953#ifndef FEAT_EVAL
1954 /*
1955 * When the expression evaluation is disabled, recognize the ":if" and
1956 * ":endif" commands and ignore everything in between it.
1957 */
1958 if (ea.cmdidx == CMD_if)
1959 ++if_level;
1960 if (if_level)
1961 {
1962 if (ea.cmdidx == CMD_endif)
1963 --if_level;
1964 goto doend;
1965 }
1966
1967#endif
1968
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001969 // forced commands
Bram Moolenaar196b3b02008-06-20 16:51:41 +00001970 if (*p == '!' && ea.cmdidx != CMD_substitute
1971 && ea.cmdidx != CMD_smagic && ea.cmdidx != CMD_snomagic)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001972 {
1973 ++p;
1974 ea.forceit = TRUE;
1975 }
1976 else
1977 ea.forceit = FALSE;
1978
1979/*
Bram Moolenaarb7316892019-05-01 18:08:42 +02001980 * 6. Parse arguments. Then check for errors.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001981 */
Bram Moolenaar958636c2014-10-21 20:01:58 +02001982 if (!IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001983 ea.argt = (long)cmdnames[(int)ea.cmdidx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001984
1985 if (!ea.skip)
1986 {
1987#ifdef HAVE_SANDBOX
Bram Moolenaar8071cb22019-07-12 17:58:01 +02001988 if (sandbox != 0 && !(ea.argt & EX_SBOXOK))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001989 {
Bram Moolenaar8c62a082019-02-08 14:34:10 +01001990 // Command not allowed in sandbox.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001991 errormsg = _(e_sandbox);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001992 goto doend;
1993 }
1994#endif
Bram Moolenaar8071cb22019-07-12 17:58:01 +02001995 if (restricted != 0 && (ea.argt & EX_RESTRICT))
Bram Moolenaar8c62a082019-02-08 14:34:10 +01001996 {
1997 errormsg = _("E981: Command not allowed in rvim");
1998 goto doend;
1999 }
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002000 if (!curbuf->b_p_ma && (ea.argt & EX_MODIFY))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002001 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002002 // Command not allowed in non-'modifiable' buffer
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002003 errormsg = _(e_modifiable);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002004 goto doend;
2005 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002006
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002007 if (text_locked() && !(ea.argt & EX_CMDWIN)
Bram Moolenaar958636c2014-10-21 20:01:58 +02002008 && !IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002009 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002010 // Command not allowed when editing the command line.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002011 errormsg = _(get_text_locked_msg());
Bram Moolenaar071d4272004-06-13 20:20:40 +00002012 goto doend;
2013 }
Bram Moolenaar379fb762018-08-30 15:58:28 +02002014
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002015 // Disallow editing another buffer when "curbuf_lock" is set.
2016 // Do allow ":checktime" (it is postponed).
2017 // Do allow ":edit" (check for an argument later).
2018 // Do allow ":file" with no arguments (check for an argument later).
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002019 if (!(ea.argt & EX_CMDWIN)
Bram Moolenaar5555acc2006-04-07 21:33:12 +00002020 && ea.cmdidx != CMD_checktime
Bram Moolenaar379fb762018-08-30 15:58:28 +02002021 && ea.cmdidx != CMD_edit
2022 && ea.cmdidx != CMD_file
Bram Moolenaar958636c2014-10-21 20:01:58 +02002023 && !IS_USER_CMDIDX(ea.cmdidx)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002024 && curbuf_locked())
2025 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002026
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002027 if (!ni && !(ea.argt & EX_RANGE) && ea.addr_count > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002028 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002029 // no range allowed
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002030 errormsg = _(e_norange);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002031 goto doend;
2032 }
2033 }
2034
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002035 if (!ni && !(ea.argt & EX_BANG) && ea.forceit) // no <!> allowed
Bram Moolenaar071d4272004-06-13 20:20:40 +00002036 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002037 errormsg = _(e_nobang);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002038 goto doend;
2039 }
2040
2041 /*
2042 * Don't complain about the range if it is not used
2043 * (could happen if line_count is accidentally set to 0).
2044 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002045 if (!ea.skip && !ni && (ea.argt & EX_RANGE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002046 {
2047 /*
2048 * If the range is backwards, ask for confirmation and, if given, swap
2049 * ea.line1 & ea.line2 so it's forwards again.
2050 * When global command is busy, don't ask, will fail below.
2051 */
2052 if (!global_busy && ea.line1 > ea.line2)
2053 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002054 if (msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002055 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002056 if (sourcing || exmode_active)
2057 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002058 errormsg = _("E493: Backwards range given");
Bram Moolenaara5792f52005-11-23 21:25:05 +00002059 goto doend;
2060 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002061 if (ask_yesno((char_u *)
2062 _("Backwards range given, OK to swap"), FALSE) != 'y')
Bram Moolenaara5792f52005-11-23 21:25:05 +00002063 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002064 }
2065 lnum = ea.line1;
2066 ea.line1 = ea.line2;
2067 ea.line2 = lnum;
2068 }
2069 if ((errormsg = invalid_range(&ea)) != NULL)
2070 goto doend;
2071 }
2072
Bram Moolenaarb7316892019-05-01 18:08:42 +02002073 if ((ea.addr_type == ADDR_OTHER) && ea.addr_count == 0)
2074 // default is 1, not cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00002075 ea.line2 = 1;
2076
2077 correct_range(&ea);
2078
2079#ifdef FEAT_FOLDING
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002080 if (((ea.argt & EX_WHOLEFOLD) || ea.addr_count >= 2) && !global_busy
Bram Moolenaara3306952016-01-02 21:41:06 +01002081 && ea.addr_type == ADDR_LINES)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002082 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002083 // Put the first line at the start of a closed fold, put the last line
2084 // at the end of a closed fold.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002085 (void)hasFolding(ea.line1, &ea.line1, NULL);
2086 (void)hasFolding(ea.line2, NULL, &ea.line2);
2087 }
2088#endif
2089
2090#ifdef FEAT_QUICKFIX
2091 /*
Bram Moolenaar86b68352004-12-27 21:59:20 +00002092 * For the ":make" and ":grep" commands we insert the 'makeprg'/'grepprg'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002093 * option here, so things like % get expanded.
2094 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002095 p = replace_makeprg(&ea, p, cmdlinep);
2096 if (p == NULL)
2097 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002098#endif
2099
2100 /*
2101 * Skip to start of argument.
2102 * Don't do this for the ":!" command, because ":!! -l" needs the space.
2103 */
2104 if (ea.cmdidx == CMD_bang)
2105 ea.arg = p;
2106 else
2107 ea.arg = skipwhite(p);
2108
Bram Moolenaar379fb762018-08-30 15:58:28 +02002109 // ":file" cannot be run with an argument when "curbuf_lock" is set
2110 if (ea.cmdidx == CMD_file && *ea.arg != NUL && curbuf_locked())
2111 goto doend;
2112
Bram Moolenaar071d4272004-06-13 20:20:40 +00002113 /*
2114 * Check for "++opt=val" argument.
2115 * Must be first, allow ":w ++enc=utf8 !cmd"
2116 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002117 if (ea.argt & EX_ARGOPT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002118 while (ea.arg[0] == '+' && ea.arg[1] == '+')
2119 if (getargopt(&ea) == FAIL && !ni)
2120 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002121 errormsg = _(e_invarg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002122 goto doend;
2123 }
2124
2125 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
2126 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002127 if (*ea.arg == '>') // append
Bram Moolenaar071d4272004-06-13 20:20:40 +00002128 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002129 if (*++ea.arg != '>') // typed wrong
Bram Moolenaar071d4272004-06-13 20:20:40 +00002130 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002131 errormsg = _("E494: Use w or w>>");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002132 goto doend;
2133 }
2134 ea.arg = skipwhite(ea.arg + 1);
2135 ea.append = TRUE;
2136 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002137 else if (*ea.arg == '!' && ea.cmdidx == CMD_write) // :w !filter
Bram Moolenaar071d4272004-06-13 20:20:40 +00002138 {
2139 ++ea.arg;
2140 ea.usefilter = TRUE;
2141 }
2142 }
2143
2144 if (ea.cmdidx == CMD_read)
2145 {
2146 if (ea.forceit)
2147 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002148 ea.usefilter = TRUE; // :r! filter if ea.forceit
Bram Moolenaar071d4272004-06-13 20:20:40 +00002149 ea.forceit = FALSE;
2150 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002151 else if (*ea.arg == '!') // :r !filter
Bram Moolenaar071d4272004-06-13 20:20:40 +00002152 {
2153 ++ea.arg;
2154 ea.usefilter = TRUE;
2155 }
2156 }
2157
2158 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
2159 {
2160 ea.amount = 1;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002161 while (*ea.arg == *ea.cmd) // count number of '>' or '<'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002162 {
2163 ++ea.arg;
2164 ++ea.amount;
2165 }
2166 ea.arg = skipwhite(ea.arg);
2167 }
2168
2169 /*
2170 * Check for "+command" argument, before checking for next command.
2171 * Don't do this for ":read !cmd" and ":write !cmd".
2172 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002173 if ((ea.argt & EX_CMDARG) && !ea.usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002174 ea.do_ecmd_cmd = getargcmd(&ea.arg);
2175
2176 /*
2177 * Check for '|' to separate commands and '"' to start comments.
2178 * Don't do this for ":read !cmd" and ":write !cmd".
2179 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002180 if ((ea.argt & EX_TRLBAR) && !ea.usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002181 separate_nextcmd(&ea);
2182
2183 /*
2184 * Check for <newline> to end a shell command.
Bram Moolenaardf177f62005-02-22 08:39:57 +00002185 * Also do this for ":read !cmd", ":write !cmd" and ":global".
2186 * Any others?
Bram Moolenaar071d4272004-06-13 20:20:40 +00002187 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00002188 else if (ea.cmdidx == CMD_bang
Bram Moolenaar67883b42017-07-27 22:57:00 +02002189 || ea.cmdidx == CMD_terminal
Bram Moolenaardf177f62005-02-22 08:39:57 +00002190 || ea.cmdidx == CMD_global
2191 || ea.cmdidx == CMD_vglobal
2192 || ea.usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002193 {
2194 for (p = ea.arg; *p; ++p)
2195 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002196 // Remove one backslash before a newline, so that it's possible to
2197 // pass a newline to the shell and also a newline that is preceded
2198 // with a backslash. This makes it impossible to end a shell
2199 // command in a backslash, but that doesn't appear useful.
2200 // Halving the number of backslashes is incompatible with previous
2201 // versions.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002202 if (*p == '\\' && p[1] == '\n')
Bram Moolenaara7241f52008-06-24 20:39:31 +00002203 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002204 else if (*p == '\n')
2205 {
2206 ea.nextcmd = p + 1;
2207 *p = NUL;
2208 break;
2209 }
2210 }
2211 }
2212
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002213 if ((ea.argt & EX_DFLALL) && ea.addr_count == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002214 {
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002215 buf_T *buf;
2216
Bram Moolenaar071d4272004-06-13 20:20:40 +00002217 ea.line1 = 1;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002218 switch (ea.addr_type)
2219 {
2220 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02002221 case ADDR_OTHER:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002222 ea.line2 = curbuf->b_ml.ml_line_count;
2223 break;
2224 case ADDR_LOADED_BUFFERS:
2225 buf = firstbuf;
2226 while (buf->b_next != NULL && buf->b_ml.ml_mfp == NULL)
2227 buf = buf->b_next;
2228 ea.line1 = buf->b_fnum;
2229 buf = lastbuf;
2230 while (buf->b_prev != NULL && buf->b_ml.ml_mfp == NULL)
2231 buf = buf->b_prev;
2232 ea.line2 = buf->b_fnum;
2233 break;
2234 case ADDR_BUFFERS:
2235 ea.line1 = firstbuf->b_fnum;
2236 ea.line2 = lastbuf->b_fnum;
2237 break;
2238 case ADDR_WINDOWS:
2239 ea.line2 = LAST_WIN_NR;
2240 break;
2241 case ADDR_TABS:
2242 ea.line2 = LAST_TAB_NR;
2243 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01002244 case ADDR_TABS_RELATIVE:
2245 ea.line2 = 1;
2246 break;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002247 case ADDR_ARGUMENTS:
2248 if (ARGCOUNT == 0)
2249 ea.line1 = ea.line2 = 0;
2250 else
2251 ea.line2 = ARGCOUNT;
2252 break;
Bram Moolenaar25190db2019-05-04 15:05:28 +02002253 case ADDR_QUICKFIX_VALID:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02002254#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02002255 ea.line2 = qf_get_valid_size(&ea);
Bram Moolenaaraa23b372015-09-08 18:46:31 +02002256 if (ea.line2 == 0)
2257 ea.line2 = 1;
Bram Moolenaare906c502015-09-09 21:10:39 +02002258#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02002259 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02002260 case ADDR_NONE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02002261 case ADDR_UNSIGNED:
2262 case ADDR_QUICKFIX:
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002263 iemsg(_("INTERNAL: Cannot use EX_DFLALL with ADDR_NONE, ADDR_UNSIGNED or ADDR_QUICKFIX"));
Bram Moolenaarb7316892019-05-01 18:08:42 +02002264 break;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002265 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002266 }
2267
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002268 // accept numbered register only when no count allowed (:put)
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002269 if ( (ea.argt & EX_REGSTR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002270 && *ea.arg != NUL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002271 // Do not allow register = for user commands
Bram Moolenaar958636c2014-10-21 20:01:58 +02002272 && (!IS_USER_CMDIDX(ea.cmdidx) || *ea.arg != '=')
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002273 && !((ea.argt & EX_COUNT) && VIM_ISDIGIT(*ea.arg)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002274 {
Bram Moolenaar85de2062011-05-05 14:26:41 +02002275#ifndef FEAT_CLIPBOARD
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002276 // check these explicitly for a more specific error message
Bram Moolenaar85de2062011-05-05 14:26:41 +02002277 if (*ea.arg == '*' || *ea.arg == '+')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002278 {
Bram Moolenaar99b12722019-01-14 20:16:40 +01002279 errormsg = _(e_invalidreg);
Bram Moolenaar85de2062011-05-05 14:26:41 +02002280 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002281 }
2282#endif
Bram Moolenaar958636c2014-10-21 20:01:58 +02002283 if (valid_yank_reg(*ea.arg, (ea.cmdidx != CMD_put
2284 && !IS_USER_CMDIDX(ea.cmdidx))))
Bram Moolenaar85de2062011-05-05 14:26:41 +02002285 {
2286 ea.regname = *ea.arg++;
2287#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002288 // for '=' register: accept the rest of the line as an expression
Bram Moolenaar85de2062011-05-05 14:26:41 +02002289 if (ea.arg[-1] == '=' && ea.arg[0] != NUL)
2290 {
2291 set_expr_line(vim_strsave(ea.arg));
2292 ea.arg += STRLEN(ea.arg);
2293 }
2294#endif
2295 ea.arg = skipwhite(ea.arg);
2296 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002297 }
2298
2299 /*
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002300 * Check for a count. When accepting a EX_BUFNAME, don't use "123foo" as a
Bram Moolenaar071d4272004-06-13 20:20:40 +00002301 * count, it's a buffer name.
2302 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002303 if ((ea.argt & EX_COUNT) && VIM_ISDIGIT(*ea.arg)
2304 && (!(ea.argt & EX_BUFNAME) || *(p = skipdigits(ea.arg)) == NUL
Bram Moolenaar1c465442017-03-12 20:10:05 +01002305 || VIM_ISWHITE(*p)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002306 {
2307 n = getdigits(&ea.arg);
2308 ea.arg = skipwhite(ea.arg);
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002309 if (n <= 0 && !ni && (ea.argt & EX_ZEROR) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002310 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002311 errormsg = _(e_zerocount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002312 goto doend;
2313 }
Bram Moolenaarb7316892019-05-01 18:08:42 +02002314 if (ea.addr_type != ADDR_LINES) // e.g. :buffer 2, :sleep 3
Bram Moolenaar071d4272004-06-13 20:20:40 +00002315 {
2316 ea.line2 = n;
2317 if (ea.addr_count == 0)
2318 ea.addr_count = 1;
2319 }
2320 else
2321 {
2322 ea.line1 = ea.line2;
2323 ea.line2 += n - 1;
2324 ++ea.addr_count;
2325 /*
2326 * Be vi compatible: no error message for out of range.
2327 */
Bram Moolenaarb7316892019-05-01 18:08:42 +02002328 if (ea.line2 > curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002329 ea.line2 = curbuf->b_ml.ml_line_count;
2330 }
2331 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00002332
2333 /*
2334 * Check for flags: 'l', 'p' and '#'.
2335 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002336 if (ea.argt & EX_FLAGS)
Bram Moolenaardf177f62005-02-22 08:39:57 +00002337 get_flags(&ea);
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002338 if (!ni && !(ea.argt & EX_EXTRA) && *ea.arg != NUL
2339 && *ea.arg != '"' && (*ea.arg != '|' || (ea.argt & EX_TRLBAR) == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002340 {
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002341 // no arguments allowed but there is something
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002342 errormsg = _(e_trailing);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002343 goto doend;
2344 }
2345
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002346 if (!ni && (ea.argt & EX_NEEDARG) && *ea.arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002347 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002348 errormsg = _(e_argreq);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002349 goto doend;
2350 }
2351
2352#ifdef FEAT_EVAL
2353 /*
2354 * Skip the command when it's not going to be executed.
2355 * The commands like :if, :endif, etc. always need to be executed.
2356 * Also make an exception for commands that handle a trailing command
2357 * themselves.
2358 */
2359 if (ea.skip)
2360 {
2361 switch (ea.cmdidx)
2362 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002363 // commands that need evaluation
Bram Moolenaar071d4272004-06-13 20:20:40 +00002364 case CMD_while:
2365 case CMD_endwhile:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00002366 case CMD_for:
2367 case CMD_endfor:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002368 case CMD_if:
2369 case CMD_elseif:
2370 case CMD_else:
2371 case CMD_endif:
2372 case CMD_try:
2373 case CMD_catch:
2374 case CMD_finally:
2375 case CMD_endtry:
2376 case CMD_function:
Bram Moolenaarab55c682020-03-01 19:41:43 +01002377 case CMD_def:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002378 break;
2379
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002380 // Commands that handle '|' themselves. Check: A command should
2381 // either have the EX_TRLBAR flag, appear in this list or appear in
2382 // the list at ":help :bar".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002383 case CMD_aboveleft:
2384 case CMD_and:
2385 case CMD_belowright:
2386 case CMD_botright:
2387 case CMD_browse:
2388 case CMD_call:
2389 case CMD_confirm:
Bram Moolenaar8f76e6b2019-11-26 16:50:30 +01002390 case CMD_const:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002391 case CMD_delfunction:
2392 case CMD_djump:
2393 case CMD_dlist:
2394 case CMD_dsearch:
2395 case CMD_dsplit:
2396 case CMD_echo:
2397 case CMD_echoerr:
2398 case CMD_echomsg:
2399 case CMD_echon:
Bram Moolenaara76b3152020-02-16 16:20:21 +01002400 case CMD_eval:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002401 case CMD_execute:
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002402 case CMD_filter:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002403 case CMD_help:
2404 case CMD_hide:
2405 case CMD_ijump:
2406 case CMD_ilist:
2407 case CMD_isearch:
2408 case CMD_isplit:
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002409 case CMD_keepalt:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002410 case CMD_keepjumps:
2411 case CMD_keepmarks:
Bram Moolenaara939e432013-11-09 05:30:26 +01002412 case CMD_keeppatterns:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002413 case CMD_leftabove:
2414 case CMD_let:
2415 case CMD_lockmarks:
Bram Moolenaarcc4423a2019-11-26 17:05:00 +01002416 case CMD_lockvar:
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002417 case CMD_lua:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002418 case CMD_match:
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002419 case CMD_mzscheme:
Bram Moolenaar5803ae62014-03-23 16:04:02 +01002420 case CMD_noautocmd:
2421 case CMD_noswapfile:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002422 case CMD_perl:
2423 case CMD_psearch:
2424 case CMD_python:
Bram Moolenaar368373e2010-07-19 20:46:22 +02002425 case CMD_py3:
Bram Moolenaarb6590522010-07-21 16:00:43 +02002426 case CMD_python3:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002427 case CMD_return:
2428 case CMD_rightbelow:
2429 case CMD_ruby:
2430 case CMD_silent:
2431 case CMD_smagic:
2432 case CMD_snomagic:
2433 case CMD_substitute:
2434 case CMD_syntax:
Bram Moolenaara226a6d2006-02-26 23:59:20 +00002435 case CMD_tab:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002436 case CMD_tcl:
2437 case CMD_throw:
2438 case CMD_tilde:
2439 case CMD_topleft:
2440 case CMD_unlet:
Bram Moolenaarcc4423a2019-11-26 17:05:00 +01002441 case CMD_unlockvar:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002442 case CMD_verbose:
2443 case CMD_vertical:
Bram Moolenaar12bde492011-06-13 01:19:56 +02002444 case CMD_wincmd:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002445 break;
2446
2447 default: goto doend;
2448 }
2449 }
2450#endif
2451
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002452 if (ea.argt & EX_XFILE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002453 {
2454 if (expand_filename(&ea, cmdlinep, &errormsg) == FAIL)
2455 goto doend;
2456 }
2457
Bram Moolenaar071d4272004-06-13 20:20:40 +00002458 /*
2459 * Accept buffer name. Cannot be used at the same time with a buffer
2460 * number. Don't do this for a user command.
2461 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002462 if ((ea.argt & EX_BUFNAME) && *ea.arg != NUL && ea.addr_count == 0
Bram Moolenaar958636c2014-10-21 20:01:58 +02002463 && !IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002464 {
2465 /*
2466 * :bdelete, :bwipeout and :bunload take several arguments, separated
2467 * by spaces: find next space (skipping over escaped characters).
2468 * The others take one argument: ignore trailing spaces.
2469 */
2470 if (ea.cmdidx == CMD_bdelete || ea.cmdidx == CMD_bwipeout
2471 || ea.cmdidx == CMD_bunload)
2472 p = skiptowhite_esc(ea.arg);
2473 else
2474 {
2475 p = ea.arg + STRLEN(ea.arg);
Bram Moolenaar1c465442017-03-12 20:10:05 +01002476 while (p > ea.arg && VIM_ISWHITE(p[-1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002477 --p;
2478 }
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002479 ea.line2 = buflist_findpat(ea.arg, p, (ea.argt & EX_BUFUNL) != 0,
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002480 FALSE, FALSE);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002481 if (ea.line2 < 0) // failed
Bram Moolenaar071d4272004-06-13 20:20:40 +00002482 goto doend;
2483 ea.addr_count = 1;
2484 ea.arg = skipwhite(p);
2485 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002486
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002487 // The :try command saves the emsg_silent flag, reset it here when
2488 // ":silent! try" was used, it should only apply to :try itself.
Bram Moolenaareffed932018-08-14 13:38:17 +02002489 if (ea.cmdidx == CMD_try && ea.did_esilent > 0)
Bram Moolenaar2be57332018-02-13 18:05:18 +01002490 {
Bram Moolenaareffed932018-08-14 13:38:17 +02002491 emsg_silent -= ea.did_esilent;
Bram Moolenaar2be57332018-02-13 18:05:18 +01002492 if (emsg_silent < 0)
2493 emsg_silent = 0;
Bram Moolenaareffed932018-08-14 13:38:17 +02002494 ea.did_esilent = 0;
Bram Moolenaar2be57332018-02-13 18:05:18 +01002495 }
2496
Bram Moolenaar071d4272004-06-13 20:20:40 +00002497/*
Bram Moolenaar2be57332018-02-13 18:05:18 +01002498 * 7. Execute the command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002499 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002500
Bram Moolenaar958636c2014-10-21 20:01:58 +02002501 if (IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002502 {
2503 /*
2504 * Execute a user-defined command.
2505 */
2506 do_ucmd(&ea);
2507 }
2508 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002509 {
2510 /*
Bram Moolenaarac9fb182019-04-27 13:04:13 +02002511 * Call the function to execute the builtin command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002512 */
2513 ea.errmsg = NULL;
2514 (cmdnames[ea.cmdidx].cmd_func)(&ea);
2515 if (ea.errmsg != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002516 errormsg = _(ea.errmsg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002517 }
2518
2519#ifdef FEAT_EVAL
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002520 // Set flag that any command was executed, used by ex_vim9script().
Bram Moolenaar16531552020-02-08 16:00:46 +01002521 if (getline_equal(ea.getline, ea.cookie, getsourceline)
2522 && current_sctx.sc_sid > 0)
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002523 SCRIPT_ITEM(current_sctx.sc_sid)->sn_had_command = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002524
Bram Moolenaar071d4272004-06-13 20:20:40 +00002525 /*
2526 * If the command just executed called do_cmdline(), any throw or ":return"
2527 * or ":finish" encountered there must also check the cstack of the still
2528 * active do_cmdline() that called this do_one_cmd(). Rethrow an uncaught
2529 * exception, or reanimate a returned function or finished script file and
2530 * return or finish it again.
2531 */
2532 if (need_rethrow)
2533 do_throw(cstack);
2534 else if (check_cstack)
2535 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002536 if (source_finished(fgetline, cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002537 do_finish(&ea, TRUE);
Bram Moolenaar89d40322006-08-29 15:30:07 +00002538 else if (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002539 && current_func_returned())
2540 do_return(&ea, TRUE, FALSE, NULL);
2541 }
2542 need_rethrow = check_cstack = FALSE;
2543#endif
2544
2545doend:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002546 if (curwin->w_cursor.lnum == 0) // can happen with zero line number
Bram Moolenaar6de5e122017-04-20 21:55:44 +02002547 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002548 curwin->w_cursor.lnum = 1;
Bram Moolenaar6de5e122017-04-20 21:55:44 +02002549 curwin->w_cursor.col = 0;
2550 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002551
2552 if (errormsg != NULL && *errormsg != NUL && !did_emsg)
2553 {
2554 if (sourcing)
2555 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002556 if (errormsg != (char *)IObuff)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002557 {
2558 STRCPY(IObuff, errormsg);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002559 errormsg = (char *)IObuff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002560 }
Bram Moolenaara6f4d612011-09-21 19:10:46 +02002561 append_command(*cmdlinep);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002562 }
2563 emsg(errormsg);
2564 }
2565#ifdef FEAT_EVAL
2566 do_errthrow(cstack,
Bram Moolenaar958636c2014-10-21 20:01:58 +02002567 (ea.cmdidx != CMD_SIZE && !IS_USER_CMDIDX(ea.cmdidx))
2568 ? cmdnames[(int)ea.cmdidx].cmd_name : (char_u *)NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002569#endif
2570
Bram Moolenaareffed932018-08-14 13:38:17 +02002571 if (ea.verbose_save >= 0)
2572 p_verbose = ea.verbose_save;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002573
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002574 free_cmdmod();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002575 cmdmod = save_cmdmod;
Bram Moolenaar9a2c0912019-03-30 14:26:18 +01002576 reg_executing = save_reg_executing;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002577
Bram Moolenaareffed932018-08-14 13:38:17 +02002578 if (ea.save_msg_silent != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002579 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002580 // messages could be enabled for a serious error, need to check if the
2581 // counters don't become negative
Bram Moolenaareffed932018-08-14 13:38:17 +02002582 if (!did_emsg || msg_silent > ea.save_msg_silent)
2583 msg_silent = ea.save_msg_silent;
2584 emsg_silent -= ea.did_esilent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002585 if (emsg_silent < 0)
2586 emsg_silent = 0;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002587 // Restore msg_scroll, it's set by file I/O commands, even when no
2588 // message is actually displayed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002589 msg_scroll = save_msg_scroll;
Bram Moolenaar77ab2802009-04-22 12:44:48 +00002590
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002591 // "silent reg" or "silent echo x" inside "redir" leaves msg_col
2592 // somewhere in the line. Put it back in the first column.
Bram Moolenaar77ab2802009-04-22 12:44:48 +00002593 if (redirecting())
2594 msg_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002595 }
2596
Bram Moolenaar7171abe2004-10-11 10:06:20 +00002597#ifdef HAVE_SANDBOX
Bram Moolenaareffed932018-08-14 13:38:17 +02002598 if (ea.did_sandbox)
Bram Moolenaar7171abe2004-10-11 10:06:20 +00002599 --sandbox;
2600#endif
2601
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002602 if (ea.nextcmd && *ea.nextcmd == NUL) // not really a next command
Bram Moolenaar071d4272004-06-13 20:20:40 +00002603 ea.nextcmd = NULL;
2604
2605#ifdef FEAT_EVAL
2606 --ex_nesting_level;
2607#endif
2608
2609 return ea.nextcmd;
2610}
2611#if (_MSC_VER == 1200)
Bram Moolenaar281bdce2005-01-25 21:53:18 +00002612 #pragma optimize( "", on )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002613#endif
2614
2615/*
Bram Moolenaareffed932018-08-14 13:38:17 +02002616 * Parse and skip over command modifiers:
2617 * - update eap->cmd
2618 * - store flags in "cmdmod".
2619 * - Set ex_pressedreturn for an empty command line.
2620 * - set msg_silent for ":silent"
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002621 * - set 'eventignore' to "all" for ":noautocmd"
Bram Moolenaareffed932018-08-14 13:38:17 +02002622 * - set p_verbose for ":verbose"
2623 * - Increment "sandbox" for ":sandbox"
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002624 * When "skip_only" is TRUE the global variables are not changed, except for
2625 * "cmdmod".
Bram Moolenaareffed932018-08-14 13:38:17 +02002626 * Return FAIL when the command is not to be executed.
2627 * May set "errormsg" to an error message.
2628 */
2629 int
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002630parse_command_modifiers(exarg_T *eap, char **errormsg, int skip_only)
Bram Moolenaareffed932018-08-14 13:38:17 +02002631{
2632 char_u *p;
2633
Bram Moolenaara80faa82020-04-12 19:37:17 +02002634 CLEAR_FIELD(cmdmod);
Bram Moolenaareffed932018-08-14 13:38:17 +02002635 eap->verbose_save = -1;
2636 eap->save_msg_silent = -1;
2637
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002638 // Repeat until no more command modifiers are found.
Bram Moolenaareffed932018-08-14 13:38:17 +02002639 for (;;)
2640 {
Bram Moolenaareffed932018-08-14 13:38:17 +02002641 while (*eap->cmd == ' ' || *eap->cmd == '\t' || *eap->cmd == ':')
2642 ++eap->cmd;
2643
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002644 // in ex mode, an empty line works like :+
Bram Moolenaareffed932018-08-14 13:38:17 +02002645 if (*eap->cmd == NUL && exmode_active
2646 && (getline_equal(eap->getline, eap->cookie, getexmodeline)
2647 || getline_equal(eap->getline, eap->cookie, getexline))
2648 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
2649 {
2650 eap->cmd = (char_u *)"+";
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002651 if (!skip_only)
2652 ex_pressedreturn = TRUE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002653 }
2654
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002655 // ignore comment and empty lines
Bram Moolenaareffed932018-08-14 13:38:17 +02002656 if (*eap->cmd == '"')
2657 return FAIL;
2658 if (*eap->cmd == NUL)
2659 {
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002660 if (!skip_only)
2661 ex_pressedreturn = TRUE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002662 return FAIL;
2663 }
2664
Bram Moolenaareffed932018-08-14 13:38:17 +02002665 p = skip_range(eap->cmd, NULL);
2666 switch (*p)
2667 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002668 // When adding an entry, also modify cmd_exists().
Bram Moolenaareffed932018-08-14 13:38:17 +02002669 case 'a': if (!checkforcmd(&eap->cmd, "aboveleft", 3))
2670 break;
2671 cmdmod.split |= WSP_ABOVE;
2672 continue;
2673
2674 case 'b': if (checkforcmd(&eap->cmd, "belowright", 3))
2675 {
2676 cmdmod.split |= WSP_BELOW;
2677 continue;
2678 }
2679 if (checkforcmd(&eap->cmd, "browse", 3))
2680 {
2681#ifdef FEAT_BROWSE_CMD
2682 cmdmod.browse = TRUE;
2683#endif
2684 continue;
2685 }
2686 if (!checkforcmd(&eap->cmd, "botright", 2))
2687 break;
2688 cmdmod.split |= WSP_BOT;
2689 continue;
2690
2691 case 'c': if (!checkforcmd(&eap->cmd, "confirm", 4))
2692 break;
2693#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
2694 cmdmod.confirm = TRUE;
2695#endif
2696 continue;
2697
2698 case 'k': if (checkforcmd(&eap->cmd, "keepmarks", 3))
2699 {
2700 cmdmod.keepmarks = TRUE;
2701 continue;
2702 }
2703 if (checkforcmd(&eap->cmd, "keepalt", 5))
2704 {
2705 cmdmod.keepalt = TRUE;
2706 continue;
2707 }
2708 if (checkforcmd(&eap->cmd, "keeppatterns", 5))
2709 {
2710 cmdmod.keeppatterns = TRUE;
2711 continue;
2712 }
2713 if (!checkforcmd(&eap->cmd, "keepjumps", 5))
2714 break;
2715 cmdmod.keepjumps = TRUE;
2716 continue;
2717
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002718 case 'f': // only accept ":filter {pat} cmd"
Bram Moolenaareffed932018-08-14 13:38:17 +02002719 {
2720 char_u *reg_pat;
2721
2722 if (!checkforcmd(&p, "filter", 4)
2723 || *p == NUL || ends_excmd(*p))
2724 break;
2725 if (*p == '!')
2726 {
2727 cmdmod.filter_force = TRUE;
2728 p = skipwhite(p + 1);
2729 if (*p == NUL || ends_excmd(*p))
2730 break;
2731 }
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002732 if (skip_only)
2733 p = skip_vimgrep_pat(p, NULL, NULL);
2734 else
2735 // NOTE: This puts a NUL after the pattern.
2736 p = skip_vimgrep_pat(p, &reg_pat, NULL);
Bram Moolenaareffed932018-08-14 13:38:17 +02002737 if (p == NULL || *p == NUL)
2738 break;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002739 if (!skip_only)
2740 {
2741 cmdmod.filter_regmatch.regprog =
Bram Moolenaareffed932018-08-14 13:38:17 +02002742 vim_regcomp(reg_pat, RE_MAGIC);
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002743 if (cmdmod.filter_regmatch.regprog == NULL)
2744 break;
2745 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002746 eap->cmd = p;
2747 continue;
2748 }
2749
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002750 // ":hide" and ":hide | cmd" are not modifiers
Bram Moolenaareffed932018-08-14 13:38:17 +02002751 case 'h': if (p != eap->cmd || !checkforcmd(&p, "hide", 3)
2752 || *p == NUL || ends_excmd(*p))
2753 break;
2754 eap->cmd = p;
2755 cmdmod.hide = TRUE;
2756 continue;
2757
2758 case 'l': if (checkforcmd(&eap->cmd, "lockmarks", 3))
2759 {
2760 cmdmod.lockmarks = TRUE;
2761 continue;
2762 }
2763
2764 if (!checkforcmd(&eap->cmd, "leftabove", 5))
2765 break;
2766 cmdmod.split |= WSP_ABOVE;
2767 continue;
2768
2769 case 'n': if (checkforcmd(&eap->cmd, "noautocmd", 3))
2770 {
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002771 if (cmdmod.save_ei == NULL && !skip_only)
Bram Moolenaareffed932018-08-14 13:38:17 +02002772 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002773 // Set 'eventignore' to "all". Restore the
2774 // existing option value later.
Bram Moolenaareffed932018-08-14 13:38:17 +02002775 cmdmod.save_ei = vim_strsave(p_ei);
2776 set_string_option_direct((char_u *)"ei", -1,
2777 (char_u *)"all", OPT_FREE, SID_NONE);
2778 }
2779 continue;
2780 }
2781 if (!checkforcmd(&eap->cmd, "noswapfile", 3))
2782 break;
2783 cmdmod.noswapfile = TRUE;
2784 continue;
2785
2786 case 'r': if (!checkforcmd(&eap->cmd, "rightbelow", 6))
2787 break;
2788 cmdmod.split |= WSP_BELOW;
2789 continue;
2790
2791 case 's': if (checkforcmd(&eap->cmd, "sandbox", 3))
2792 {
2793#ifdef HAVE_SANDBOX
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002794 if (!skip_only)
2795 {
2796 if (!eap->did_sandbox)
2797 ++sandbox;
2798 eap->did_sandbox = TRUE;
2799 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002800#endif
2801 continue;
2802 }
2803 if (!checkforcmd(&eap->cmd, "silent", 3))
2804 break;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002805 if (!skip_only)
2806 {
2807 if (eap->save_msg_silent == -1)
2808 eap->save_msg_silent = msg_silent;
2809 ++msg_silent;
2810 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002811 if (*eap->cmd == '!' && !VIM_ISWHITE(eap->cmd[-1]))
2812 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002813 // ":silent!", but not "silent !cmd"
Bram Moolenaareffed932018-08-14 13:38:17 +02002814 eap->cmd = skipwhite(eap->cmd + 1);
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002815 if (!skip_only)
2816 {
2817 ++emsg_silent;
2818 ++eap->did_esilent;
2819 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002820 }
2821 continue;
2822
2823 case 't': if (checkforcmd(&p, "tab", 3))
2824 {
Bram Moolenaar548e5982018-12-26 21:45:00 +01002825 if (!skip_only)
Bram Moolenaareffed932018-08-14 13:38:17 +02002826 {
Bram Moolenaar548e5982018-12-26 21:45:00 +01002827 long tabnr = get_address(eap, &eap->cmd,
2828 ADDR_TABS, eap->skip,
2829 skip_only, FALSE, 1);
2830 if (tabnr == MAXLNUM)
2831 cmdmod.tab = tabpage_index(curtab) + 1;
2832 else
Bram Moolenaareffed932018-08-14 13:38:17 +02002833 {
Bram Moolenaar548e5982018-12-26 21:45:00 +01002834 if (tabnr < 0 || tabnr > LAST_TAB_NR)
2835 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002836 *errormsg = _(e_invrange);
Bram Moolenaar548e5982018-12-26 21:45:00 +01002837 return FAIL;
2838 }
2839 cmdmod.tab = tabnr + 1;
Bram Moolenaareffed932018-08-14 13:38:17 +02002840 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002841 }
2842 eap->cmd = p;
2843 continue;
2844 }
2845 if (!checkforcmd(&eap->cmd, "topleft", 2))
2846 break;
2847 cmdmod.split |= WSP_TOP;
2848 continue;
2849
2850 case 'u': if (!checkforcmd(&eap->cmd, "unsilent", 3))
2851 break;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002852 if (!skip_only)
2853 {
2854 if (eap->save_msg_silent == -1)
2855 eap->save_msg_silent = msg_silent;
2856 msg_silent = 0;
2857 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002858 continue;
2859
2860 case 'v': if (checkforcmd(&eap->cmd, "vertical", 4))
2861 {
2862 cmdmod.split |= WSP_VERT;
2863 continue;
2864 }
2865 if (!checkforcmd(&p, "verbose", 4))
2866 break;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002867 if (!skip_only)
2868 {
2869 if (eap->verbose_save < 0)
2870 eap->verbose_save = p_verbose;
2871 if (vim_isdigit(*eap->cmd))
2872 p_verbose = atoi((char *)eap->cmd);
2873 else
2874 p_verbose = 1;
2875 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002876 eap->cmd = p;
2877 continue;
2878 }
2879 break;
2880 }
2881
2882 return OK;
2883}
2884
2885/*
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002886 * Free contents of "cmdmod".
2887 */
2888 static void
2889free_cmdmod(void)
2890{
2891 if (cmdmod.save_ei != NULL)
2892 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002893 // Restore 'eventignore' to the value before ":noautocmd".
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002894 set_string_option_direct((char_u *)"ei", -1, cmdmod.save_ei,
2895 OPT_FREE, SID_NONE);
2896 free_string_option(cmdmod.save_ei);
2897 }
2898
2899 if (cmdmod.filter_regmatch.regprog != NULL)
2900 vim_regfree(cmdmod.filter_regmatch.regprog);
2901}
2902
2903/*
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002904 * Parse the address range, if any, in "eap".
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02002905 * May set the last search pattern, unless "silent" is TRUE.
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002906 * Return FAIL and set "errormsg" or return OK.
2907 */
2908 int
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002909parse_cmd_address(exarg_T *eap, char **errormsg, int silent)
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002910{
2911 int address_count = 1;
2912 linenr_T lnum;
2913
2914 // Repeat for all ',' or ';' separated addresses.
2915 for (;;)
2916 {
2917 eap->line1 = eap->line2;
2918 switch (eap->addr_type)
2919 {
2920 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02002921 case ADDR_OTHER:
Bram Moolenaarb5383b12020-05-18 19:46:48 +02002922 // Default is the cursor line number. Avoid using an invalid
2923 // line number though.
2924 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
2925 eap->line2 = curbuf->b_ml.ml_line_count;
2926 else
2927 eap->line2 = curwin->w_cursor.lnum;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002928 break;
2929 case ADDR_WINDOWS:
2930 eap->line2 = CURRENT_WIN_NR;
2931 break;
2932 case ADDR_ARGUMENTS:
2933 eap->line2 = curwin->w_arg_idx + 1;
2934 if (eap->line2 > ARGCOUNT)
2935 eap->line2 = ARGCOUNT;
2936 break;
2937 case ADDR_LOADED_BUFFERS:
2938 case ADDR_BUFFERS:
2939 eap->line2 = curbuf->b_fnum;
2940 break;
2941 case ADDR_TABS:
2942 eap->line2 = CURRENT_TAB_NR;
2943 break;
2944 case ADDR_TABS_RELATIVE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02002945 case ADDR_UNSIGNED:
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002946 eap->line2 = 1;
2947 break;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002948 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02002949#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02002950 eap->line2 = qf_get_cur_idx(eap);
2951#endif
2952 break;
2953 case ADDR_QUICKFIX_VALID:
2954#ifdef FEAT_QUICKFIX
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002955 eap->line2 = qf_get_cur_valid_idx(eap);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002956#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02002957 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02002958 case ADDR_NONE:
2959 // Will give an error later if a range is found.
2960 break;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002961 }
2962 eap->cmd = skipwhite(eap->cmd);
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02002963 lnum = get_address(eap, &eap->cmd, eap->addr_type, eap->skip, silent,
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002964 eap->addr_count == 0, address_count++);
2965 if (eap->cmd == NULL) // error detected
2966 return FAIL;
2967 if (lnum == MAXLNUM)
2968 {
2969 if (*eap->cmd == '%') // '%' - all lines
2970 {
2971 ++eap->cmd;
2972 switch (eap->addr_type)
2973 {
2974 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02002975 case ADDR_OTHER:
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002976 eap->line1 = 1;
2977 eap->line2 = curbuf->b_ml.ml_line_count;
2978 break;
2979 case ADDR_LOADED_BUFFERS:
2980 {
2981 buf_T *buf = firstbuf;
2982
2983 while (buf->b_next != NULL
2984 && buf->b_ml.ml_mfp == NULL)
2985 buf = buf->b_next;
2986 eap->line1 = buf->b_fnum;
2987 buf = lastbuf;
2988 while (buf->b_prev != NULL
2989 && buf->b_ml.ml_mfp == NULL)
2990 buf = buf->b_prev;
2991 eap->line2 = buf->b_fnum;
2992 break;
2993 }
2994 case ADDR_BUFFERS:
2995 eap->line1 = firstbuf->b_fnum;
2996 eap->line2 = lastbuf->b_fnum;
2997 break;
2998 case ADDR_WINDOWS:
2999 case ADDR_TABS:
3000 if (IS_USER_CMDIDX(eap->cmdidx))
3001 {
3002 eap->line1 = 1;
3003 eap->line2 = eap->addr_type == ADDR_WINDOWS
3004 ? LAST_WIN_NR : LAST_TAB_NR;
3005 }
3006 else
3007 {
3008 // there is no Vim command which uses '%' and
3009 // ADDR_WINDOWS or ADDR_TABS
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003010 *errormsg = _(e_invrange);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003011 return FAIL;
3012 }
3013 break;
3014 case ADDR_TABS_RELATIVE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02003015 case ADDR_UNSIGNED:
3016 case ADDR_QUICKFIX:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003017 *errormsg = _(e_invrange);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003018 return FAIL;
3019 case ADDR_ARGUMENTS:
3020 if (ARGCOUNT == 0)
3021 eap->line1 = eap->line2 = 0;
3022 else
3023 {
3024 eap->line1 = 1;
3025 eap->line2 = ARGCOUNT;
3026 }
3027 break;
Bram Moolenaar25190db2019-05-04 15:05:28 +02003028 case ADDR_QUICKFIX_VALID:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003029#ifdef FEAT_QUICKFIX
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003030 eap->line1 = 1;
Bram Moolenaar25190db2019-05-04 15:05:28 +02003031 eap->line2 = qf_get_valid_size(eap);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003032 if (eap->line2 == 0)
3033 eap->line2 = 1;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003034#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003035 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02003036 case ADDR_NONE:
3037 // Will give an error later if a range is found.
3038 break;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003039 }
3040 ++eap->addr_count;
3041 }
3042 else if (*eap->cmd == '*' && vim_strchr(p_cpo, CPO_STAR) == NULL)
3043 {
3044 pos_T *fp;
3045
3046 // '*' - visual area
3047 if (eap->addr_type != ADDR_LINES)
3048 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003049 *errormsg = _(e_invrange);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003050 return FAIL;
3051 }
3052
3053 ++eap->cmd;
3054 if (!eap->skip)
3055 {
3056 fp = getmark('<', FALSE);
3057 if (check_mark(fp) == FAIL)
3058 return FAIL;
3059 eap->line1 = fp->lnum;
3060 fp = getmark('>', FALSE);
3061 if (check_mark(fp) == FAIL)
3062 return FAIL;
3063 eap->line2 = fp->lnum;
3064 ++eap->addr_count;
3065 }
3066 }
3067 }
3068 else
3069 eap->line2 = lnum;
3070 eap->addr_count++;
3071
3072 if (*eap->cmd == ';')
3073 {
3074 if (!eap->skip)
3075 {
3076 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaar0e717042020-04-27 19:29:01 +02003077 // Don't leave the cursor on an illegal line or column, but do
3078 // accept zero as address, so 0;/PATTERN/ works correctly.
3079 if (eap->line2 > 0)
3080 check_cursor();
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003081 }
3082 }
3083 else if (*eap->cmd != ',')
3084 break;
3085 ++eap->cmd;
3086 }
3087
3088 // One address given: set start and end lines.
3089 if (eap->addr_count == 1)
3090 {
3091 eap->line1 = eap->line2;
3092 // ... but only implicit: really no address given
3093 if (lnum == MAXLNUM)
3094 eap->addr_count = 0;
3095 }
3096 return OK;
3097}
3098
3099/*
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00003100 * Check for an Ex command with optional tail.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003101 * If there is a match advance "pp" to the argument and return TRUE.
3102 */
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00003103 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003104checkforcmd(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003105 char_u **pp, // start of command
3106 char *cmd, // name of command
3107 int len) // required length
Bram Moolenaar071d4272004-06-13 20:20:40 +00003108{
3109 int i;
3110
3111 for (i = 0; cmd[i] != NUL; ++i)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003112 if (((char_u *)cmd)[i] != (*pp)[i])
Bram Moolenaar071d4272004-06-13 20:20:40 +00003113 break;
3114 if (i >= len && !isalpha((*pp)[i]))
3115 {
3116 *pp = skipwhite(*pp + i);
3117 return TRUE;
3118 }
3119 return FALSE;
3120}
3121
3122/*
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003123 * Append "cmd" to the error message in IObuff.
3124 * Takes care of limiting the length and handling 0xa0, which would be
3125 * invisible otherwise.
3126 */
3127 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003128append_command(char_u *cmd)
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003129{
3130 char_u *s = cmd;
3131 char_u *d;
3132
3133 STRCAT(IObuff, ": ");
3134 d = IObuff + STRLEN(IObuff);
3135 while (*s != NUL && d - IObuff < IOSIZE - 7)
3136 {
Bram Moolenaar13505972019-01-24 15:04:48 +01003137 if (enc_utf8 ? (s[0] == 0xc2 && s[1] == 0xa0) : *s == 0xa0)
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003138 {
Bram Moolenaar13505972019-01-24 15:04:48 +01003139 s += enc_utf8 ? 2 : 1;
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003140 STRCPY(d, "<a0>");
3141 d += 4;
3142 }
3143 else
3144 MB_COPY_CHAR(s, d);
3145 }
3146 *d = NUL;
3147}
3148
3149/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003150 * Find an Ex command by its name, either built-in or user.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003151 * Start of the name can be found at eap->cmd.
Bram Moolenaar25190db2019-05-04 15:05:28 +02003152 * Sets eap->cmdidx and returns a pointer to char after the command name.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003153 * "full" is set to TRUE if the whole command name matched.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003154 *
3155 * If "lookup" is not NULL recognize expression without "eval" or "call" and
3156 * assignment without "let". Sets eap->cmdidx to the command while returning
3157 * "eap->cmd".
3158 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00003159 * Returns NULL for an ambiguous user command.
3160 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003161 char_u *
3162find_ex_command(
3163 exarg_T *eap,
Bram Moolenaara494f562020-04-18 17:45:38 +02003164 int *full UNUSED,
Bram Moolenaarb84a3812020-05-01 15:44:29 +02003165 void *(*lookup)(char_u *, size_t, cctx_T *) UNUSED,
Bram Moolenaara494f562020-04-18 17:45:38 +02003166 cctx_T *cctx UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003167{
3168 int len;
3169 char_u *p;
Bram Moolenaardf177f62005-02-22 08:39:57 +00003170 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003171
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003172#ifdef FEAT_EVAL
3173 /*
3174 * Recognize a Vim9 script function/method call and assignment:
3175 * "lvar = value", "lvar(arg)", "[1, 2 3]->Func()"
3176 */
Bram Moolenaar0c6ceaf2020-02-22 18:36:32 +01003177 p = eap->cmd;
3178 if (lookup != NULL && (*p == '('
3179 || ((p = to_name_const_end(eap->cmd)) > eap->cmd && *p != NUL)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003180 {
3181 int oplen;
3182 int heredoc;
3183
3184 // "funcname(" is always a function call.
3185 // "varname[]" is an expression.
3186 // "g:varname" is an expression.
3187 // "varname->expr" is an expression.
Bram Moolenaar0c6ceaf2020-02-22 18:36:32 +01003188 // "(..." is an expression.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003189 if (*p == '('
3190 || *p == '['
3191 || p[1] == ':'
3192 || (*p == '-' && p[1] == '>'))
3193 {
3194 eap->cmdidx = CMD_eval;
3195 return eap->cmd;
3196 }
3197
3198 oplen = assignment_len(skipwhite(p), &heredoc);
3199 if (oplen > 0)
3200 {
3201 // Recognize an assignment if we recognize the variable name:
3202 // "g:var = expr"
3203 // "var = expr" where "var" is a local var name.
3204 if (((p - eap->cmd) > 2 && eap->cmd[1] == ':')
Bram Moolenaarb84a3812020-05-01 15:44:29 +02003205 || lookup(eap->cmd, p - eap->cmd, cctx) != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003206 {
3207 eap->cmdidx = CMD_let;
3208 return eap->cmd;
3209 }
3210 }
3211 }
3212#endif
3213
Bram Moolenaar071d4272004-06-13 20:20:40 +00003214 /*
3215 * Isolate the command and search for it in the command table.
Bram Moolenaar81870892007-11-11 18:17:28 +00003216 * Exceptions:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003217 * - the 'k' command can directly be followed by any character.
3218 * - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003219 * but :sre[wind] is another command, as are :scr[iptnames],
Bram Moolenaar071d4272004-06-13 20:20:40 +00003220 * :scs[cope], :sim[alt], :sig[ns] and :sil[ent].
Bram Moolenaardf177f62005-02-22 08:39:57 +00003221 * - the "d" command can directly be followed by 'l' or 'p' flag.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003222 */
3223 p = eap->cmd;
3224 if (*p == 'k')
3225 {
3226 eap->cmdidx = CMD_k;
3227 ++p;
3228 }
3229 else if (p[0] == 's'
Bram Moolenaar204b93f2015-08-04 22:02:51 +02003230 && ((p[1] == 'c' && (p[2] == NUL || (p[2] != 's' && p[2] != 'r'
3231 && (p[3] == NUL || (p[3] != 'i' && p[4] != 'p')))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003232 || p[1] == 'g'
3233 || (p[1] == 'i' && p[2] != 'm' && p[2] != 'l' && p[2] != 'g')
3234 || p[1] == 'I'
3235 || (p[1] == 'r' && p[2] != 'e')))
3236 {
3237 eap->cmdidx = CMD_substitute;
3238 ++p;
3239 }
3240 else
3241 {
3242 while (ASCII_ISALPHA(*p))
3243 ++p;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003244 // for python 3.x support ":py3", ":python3", ":py3file", etc.
Bram Moolenaar55d5c032010-07-17 23:52:29 +02003245 if (eap->cmd[0] == 'p' && eap->cmd[1] == 'y')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003246 {
Bram Moolenaarb6590522010-07-21 16:00:43 +02003247 while (ASCII_ISALNUM(*p))
3248 ++p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003249 }
3250 else if (*p == '9' && STRNCMP("vim9", eap->cmd, 4) == 0)
3251 {
3252 // include "9" for "vim9script"
3253 ++p;
3254 while (ASCII_ISALPHA(*p))
3255 ++p;
3256 }
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02003257
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003258 // check for non-alpha command
Bram Moolenaar071d4272004-06-13 20:20:40 +00003259 if (p == eap->cmd && vim_strchr((char_u *)"@*!=><&~#", *p) != NULL)
3260 ++p;
3261 len = (int)(p - eap->cmd);
Bram Moolenaardf177f62005-02-22 08:39:57 +00003262 if (*eap->cmd == 'd' && (p[-1] == 'l' || p[-1] == 'p'))
3263 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003264 // Check for ":dl", ":dell", etc. to ":deletel": that's
3265 // :delete with the 'l' flag. Same for 'p'.
Bram Moolenaardf177f62005-02-22 08:39:57 +00003266 for (i = 0; i < len; ++i)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003267 if (eap->cmd[i] != ((char_u *)"delete")[i])
Bram Moolenaardf177f62005-02-22 08:39:57 +00003268 break;
3269 if (i == len - 1)
3270 {
3271 --len;
3272 if (p[-1] == 'l')
3273 eap->flags |= EXFLAG_LIST;
3274 else
3275 eap->flags |= EXFLAG_PRINT;
3276 }
3277 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003278
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003279 if (ASCII_ISLOWER(eap->cmd[0]))
3280 {
Bram Moolenaar6c0c1e82017-03-25 15:07:43 +01003281 int c1 = eap->cmd[0];
Bram Moolenaar94f82cb2019-07-24 22:30:27 +02003282 int c2 = len == 1 ? NUL : eap->cmd[1];
Bram Moolenaar6c0c1e82017-03-25 15:07:43 +01003283
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003284 if (command_count != (int)CMD_SIZE)
3285 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003286 iemsg(_("E943: Command table needs to be updated, run 'make cmdidxs'"));
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003287 getout(1);
3288 }
3289
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003290 // Use a precomputed index for fast look-up in cmdnames[]
3291 // taking into account the first 2 letters of eap->cmd.
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003292 eap->cmdidx = cmdidxs1[CharOrdLow(c1)];
3293 if (ASCII_ISLOWER(c2))
3294 eap->cmdidx += cmdidxs2[CharOrdLow(c1)][CharOrdLow(c2)];
3295 }
Bram Moolenaara494f562020-04-18 17:45:38 +02003296 else if (ASCII_ISUPPER(eap->cmd[0]))
3297 eap->cmdidx = CMD_Next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003298 else
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003299 eap->cmdidx = CMD_bang;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003300
3301 for ( ; (int)eap->cmdidx < (int)CMD_SIZE;
3302 eap->cmdidx = (cmdidx_T)((int)eap->cmdidx + 1))
3303 if (STRNCMP(cmdnames[(int)eap->cmdidx].cmd_name, (char *)eap->cmd,
3304 (size_t)len) == 0)
3305 {
3306#ifdef FEAT_EVAL
3307 if (full != NULL
3308 && cmdnames[(int)eap->cmdidx].cmd_name[len] == NUL)
3309 *full = TRUE;
3310#endif
3311 break;
3312 }
3313
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003314 // Look for a user defined command as a last resort. Let ":Print" be
3315 // overruled by a user defined command.
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01003316 if ((eap->cmdidx == CMD_SIZE || eap->cmdidx == CMD_Print)
3317 && *eap->cmd >= 'A' && *eap->cmd <= 'Z')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003318 {
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003319 // User defined commands may contain digits.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003320 while (ASCII_ISALNUM(*p))
3321 ++p;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003322 p = find_ucmd(eap, p, full, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003323 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003324 if (p == eap->cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003325 eap->cmdidx = CMD_SIZE;
3326 }
3327
3328 return p;
3329}
3330
3331#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003332static struct cmdmod
3333{
3334 char *name;
3335 int minlen;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003336 int has_count; // :123verbose :3tab
Bram Moolenaared53fb92007-11-24 20:50:24 +00003337} cmdmods[] = {
3338 {"aboveleft", 3, FALSE},
3339 {"belowright", 3, FALSE},
3340 {"botright", 2, FALSE},
3341 {"browse", 3, FALSE},
3342 {"confirm", 4, FALSE},
Bram Moolenaar7b668e82016-08-23 23:51:21 +02003343 {"filter", 4, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003344 {"hide", 3, FALSE},
3345 {"keepalt", 5, FALSE},
3346 {"keepjumps", 5, FALSE},
3347 {"keepmarks", 3, FALSE},
Bram Moolenaara939e432013-11-09 05:30:26 +01003348 {"keeppatterns", 5, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003349 {"leftabove", 5, FALSE},
3350 {"lockmarks", 3, FALSE},
Bram Moolenaarca9f9582008-09-18 10:44:28 +00003351 {"noautocmd", 3, FALSE},
Bram Moolenaar5803ae62014-03-23 16:04:02 +01003352 {"noswapfile", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003353 {"rightbelow", 6, FALSE},
3354 {"sandbox", 3, FALSE},
3355 {"silent", 3, FALSE},
3356 {"tab", 3, TRUE},
3357 {"topleft", 2, FALSE},
Bram Moolenaar8e258a42009-07-09 13:55:43 +00003358 {"unsilent", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003359 {"verbose", 4, TRUE},
3360 {"vertical", 4, FALSE},
3361};
3362
3363/*
3364 * Return length of a command modifier (including optional count).
3365 * Return zero when it's not a modifier.
3366 */
3367 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003368modifier_len(char_u *cmd)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003369{
3370 int i, j;
3371 char_u *p = cmd;
3372
3373 if (VIM_ISDIGIT(*cmd))
3374 p = skipwhite(skipdigits(cmd));
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00003375 for (i = 0; i < (int)(sizeof(cmdmods) / sizeof(struct cmdmod)); ++i)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003376 {
3377 for (j = 0; p[j] != NUL; ++j)
3378 if (p[j] != cmdmods[i].name[j])
3379 break;
Bram Moolenaar2d473ab2013-06-12 17:12:24 +02003380 if (!ASCII_ISALPHA(p[j]) && j >= cmdmods[i].minlen
Bram Moolenaared53fb92007-11-24 20:50:24 +00003381 && (p == cmd || cmdmods[i].has_count))
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00003382 return j + (int)(p - cmd);
Bram Moolenaared53fb92007-11-24 20:50:24 +00003383 }
3384 return 0;
3385}
3386
Bram Moolenaar071d4272004-06-13 20:20:40 +00003387/*
3388 * Return > 0 if an Ex command "name" exists.
3389 * Return 2 if there is an exact match.
3390 * Return 3 if there is an ambiguous match.
3391 */
3392 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003393cmd_exists(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003394{
3395 exarg_T ea;
3396 int full = FALSE;
3397 int i;
3398 int j;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003399 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003400
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003401 // Check command modifiers.
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00003402 for (i = 0; i < (int)(sizeof(cmdmods) / sizeof(struct cmdmod)); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003403 {
3404 for (j = 0; name[j] != NUL; ++j)
3405 if (name[j] != cmdmods[i].name[j])
3406 break;
3407 if (name[j] == NUL && j >= cmdmods[i].minlen)
3408 return (cmdmods[i].name[j] == NUL ? 2 : 1);
3409 }
3410
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003411 // Check built-in commands and user defined commands.
3412 // For ":2match" and ":3match" we need to skip the number.
Bram Moolenaara9587612006-05-04 21:47:50 +00003413 ea.cmd = (*name == '2' || *name == '3') ? name + 1 : name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003414 ea.cmdidx = (cmdidx_T)0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003415 p = find_ex_command(&ea, &full, NULL, NULL);
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003416 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003417 return 3;
Bram Moolenaara9587612006-05-04 21:47:50 +00003418 if (vim_isdigit(*name) && ea.cmdidx != CMD_match)
3419 return 0;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003420 if (*skipwhite(p) != NUL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003421 return 0; // trailing garbage
Bram Moolenaar071d4272004-06-13 20:20:40 +00003422 return (ea.cmdidx == CMD_SIZE ? 0 : (full ? 2 : 1));
3423}
3424#endif
3425
Bram Moolenaard0190392019-08-23 21:17:35 +02003426 cmdidx_T
3427excmd_get_cmdidx(char_u *cmd, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003428{
Bram Moolenaard0190392019-08-23 21:17:35 +02003429 cmdidx_T idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003430
Bram Moolenaard0190392019-08-23 21:17:35 +02003431 for (idx = (cmdidx_T)0; (int)idx < (int)CMD_SIZE;
3432 idx = (cmdidx_T)((int)idx + 1))
3433 if (STRNCMP(cmdnames[(int)idx].cmd_name, cmd, (size_t)len) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003434 break;
3435
Bram Moolenaard0190392019-08-23 21:17:35 +02003436 return idx;
3437}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003438
Bram Moolenaard0190392019-08-23 21:17:35 +02003439 long
3440excmd_get_argt(cmdidx_T idx)
3441{
3442 return (long)cmdnames[(int)idx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003443}
3444
3445/*
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003446 * Skip a range specifier of the form: addr [,addr] [;addr] ..
Bram Moolenaar071d4272004-06-13 20:20:40 +00003447 *
3448 * Backslashed delimiters after / or ? will be skipped, and commands will
3449 * not be expanded between /'s and ?'s or after "'".
3450 *
Bram Moolenaardf177f62005-02-22 08:39:57 +00003451 * Also skip white space and ":" characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003452 * Returns the "cmd" pointer advanced to beyond the range.
3453 */
3454 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003455skip_range(
3456 char_u *cmd,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003457 int *ctx) // pointer to xp_context or NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003458{
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003459 unsigned delim;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003460
Bram Moolenaarcbf20fb2017-02-03 21:19:04 +01003461 while (vim_strchr((char_u *)" \t0123456789.$%'/?-+,;\\", *cmd) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003462 {
Bram Moolenaarcbf20fb2017-02-03 21:19:04 +01003463 if (*cmd == '\\')
3464 {
3465 if (cmd[1] == '?' || cmd[1] == '/' || cmd[1] == '&')
3466 ++cmd;
3467 else
3468 break;
3469 }
3470 else if (*cmd == '\'')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003471 {
3472 if (*++cmd == NUL && ctx != NULL)
3473 *ctx = EXPAND_NOTHING;
3474 }
3475 else if (*cmd == '/' || *cmd == '?')
3476 {
3477 delim = *cmd++;
3478 while (*cmd != NUL && *cmd != delim)
3479 if (*cmd++ == '\\' && *cmd != NUL)
3480 ++cmd;
3481 if (*cmd == NUL && ctx != NULL)
3482 *ctx = EXPAND_NOTHING;
3483 }
3484 if (*cmd != NUL)
3485 ++cmd;
3486 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00003487
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003488 // Skip ":" and white space.
Bram Moolenaardf177f62005-02-22 08:39:57 +00003489 while (*cmd == ':')
3490 cmd = skipwhite(cmd + 1);
3491
Bram Moolenaar071d4272004-06-13 20:20:40 +00003492 return cmd;
3493}
3494
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003495 static void
3496addr_error(cmd_addr_T addr_type)
3497{
3498 if (addr_type == ADDR_NONE)
3499 emsg(_(e_norange));
3500 else
3501 emsg(_(e_invrange));
3502}
3503
Bram Moolenaar071d4272004-06-13 20:20:40 +00003504/*
Bram Moolenaar198cb662018-09-06 21:44:17 +02003505 * Get a single EX address.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003506 *
3507 * Set ptr to the next character after the part that was interpreted.
3508 * Set ptr to NULL when an error is encountered.
Bram Moolenaar198cb662018-09-06 21:44:17 +02003509 * This may set the last used search pattern.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003510 *
3511 * Return MAXLNUM when no Ex address was found.
3512 */
3513 static linenr_T
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003514get_address(
3515 exarg_T *eap UNUSED,
3516 char_u **ptr,
Bram Moolenaar1b03a192019-12-08 17:08:29 +01003517 cmd_addr_T addr_type,
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02003518 int skip, // only skip the address, don't use it
3519 int silent, // no errors or side effects
3520 int to_other_file, // flag: may jump to other file
3521 int address_count UNUSED) // 1 for first address, >1 after comma
Bram Moolenaar071d4272004-06-13 20:20:40 +00003522{
3523 int c;
3524 int i;
3525 long n;
3526 char_u *cmd;
3527 pos_T pos;
3528 pos_T *fp;
3529 linenr_T lnum;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003530 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003531
3532 cmd = skipwhite(*ptr);
3533 lnum = MAXLNUM;
3534 do
3535 {
3536 switch (*cmd)
3537 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003538 case '.': // '.' - Cursor position
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003539 ++cmd;
3540 switch (addr_type)
3541 {
3542 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02003543 case ADDR_OTHER:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003544 lnum = curwin->w_cursor.lnum;
3545 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003546 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01003547 lnum = CURRENT_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003548 break;
3549 case ADDR_ARGUMENTS:
3550 lnum = curwin->w_arg_idx + 1;
3551 break;
3552 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003553 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003554 lnum = curbuf->b_fnum;
3555 break;
3556 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01003557 lnum = CURRENT_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003558 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02003559 case ADDR_NONE:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003560 case ADDR_TABS_RELATIVE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02003561 case ADDR_UNSIGNED:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003562 addr_error(addr_type);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01003563 cmd = NULL;
3564 goto error;
3565 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003566 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003567#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02003568 lnum = qf_get_cur_idx(eap);
3569#endif
3570 break;
3571 case ADDR_QUICKFIX_VALID:
3572#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003573 lnum = qf_get_cur_valid_idx(eap);
Bram Moolenaare906c502015-09-09 21:10:39 +02003574#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003575 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003576 }
3577 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003578
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003579 case '$': // '$' - last line
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003580 ++cmd;
3581 switch (addr_type)
3582 {
3583 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02003584 case ADDR_OTHER:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003585 lnum = curbuf->b_ml.ml_line_count;
3586 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003587 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01003588 lnum = LAST_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003589 break;
3590 case ADDR_ARGUMENTS:
3591 lnum = ARGCOUNT;
3592 break;
3593 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003594 buf = lastbuf;
3595 while (buf->b_ml.ml_mfp == NULL)
3596 {
3597 if (buf->b_prev == NULL)
3598 break;
3599 buf = buf->b_prev;
3600 }
3601 lnum = buf->b_fnum;
3602 break;
3603 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003604 lnum = lastbuf->b_fnum;
3605 break;
3606 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01003607 lnum = LAST_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003608 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02003609 case ADDR_NONE:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003610 case ADDR_TABS_RELATIVE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02003611 case ADDR_UNSIGNED:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003612 addr_error(addr_type);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01003613 cmd = NULL;
3614 goto error;
3615 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003616 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003617#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003618 lnum = qf_get_size(eap);
3619 if (lnum == 0)
3620 lnum = 1;
Bram Moolenaare906c502015-09-09 21:10:39 +02003621#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003622 break;
Bram Moolenaar25190db2019-05-04 15:05:28 +02003623 case ADDR_QUICKFIX_VALID:
3624#ifdef FEAT_QUICKFIX
3625 lnum = qf_get_valid_size(eap);
3626 if (lnum == 0)
3627 lnum = 1;
3628#endif
3629 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003630 }
3631 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003632
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003633 case '\'': // ''' - mark
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003634 if (*++cmd == NUL)
3635 {
3636 cmd = NULL;
3637 goto error;
3638 }
3639 if (addr_type != ADDR_LINES)
3640 {
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003641 addr_error(addr_type);
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01003642 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003643 goto error;
3644 }
3645 if (skip)
3646 ++cmd;
3647 else
3648 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003649 // Only accept a mark in another file when it is
3650 // used by itself: ":'M".
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003651 fp = getmark(*cmd, to_other_file && cmd[1] == NUL);
3652 ++cmd;
3653 if (fp == (pos_T *)-1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003654 // Jumped to another file.
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003655 lnum = curwin->w_cursor.lnum;
3656 else
3657 {
3658 if (check_mark(fp) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003659 {
3660 cmd = NULL;
3661 goto error;
3662 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003663 lnum = fp->lnum;
3664 }
3665 }
3666 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003667
3668 case '/':
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003669 case '?': // '/' or '?' - search
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003670 c = *cmd++;
3671 if (addr_type != ADDR_LINES)
3672 {
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003673 addr_error(addr_type);
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01003674 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003675 goto error;
3676 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003677 if (skip) // skip "/pat/"
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003678 {
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02003679 cmd = skip_regexp(cmd, c, (int)p_magic);
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003680 if (*cmd == c)
3681 ++cmd;
3682 }
3683 else
3684 {
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02003685 int flags;
3686
3687 pos = curwin->w_cursor; // save curwin->w_cursor
3688
3689 // When '/' or '?' follows another address, start from
3690 // there.
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003691 if (lnum != MAXLNUM)
3692 curwin->w_cursor.lnum = lnum;
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02003693
3694 // Start a forward search at the end of the line (unless
3695 // before the first line).
3696 // Start a backward search at the start of the line.
3697 // This makes sure we never match in the current
3698 // line, and can match anywhere in the
3699 // next/previous line.
Bram Moolenaar8ada6aa2017-12-19 21:23:21 +01003700 if (c == '/' && curwin->w_cursor.lnum > 0)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003701 curwin->w_cursor.col = MAXCOL;
3702 else
3703 curwin->w_cursor.col = 0;
3704 searchcmdlen = 0;
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02003705 flags = silent ? 0 : SEARCH_HIS | SEARCH_MSG;
Bram Moolenaarc036e872020-02-21 21:30:52 +01003706 if (!do_search(NULL, c, c, cmd, 1L, flags, NULL))
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003707 {
3708 curwin->w_cursor = pos;
3709 cmd = NULL;
3710 goto error;
3711 }
3712 lnum = curwin->w_cursor.lnum;
3713 curwin->w_cursor = pos;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003714 // adjust command string pointer
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003715 cmd += searchcmdlen;
3716 }
3717 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003718
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003719 case '\\': // "\?", "\/" or "\&", repeat search
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003720 ++cmd;
3721 if (addr_type != ADDR_LINES)
3722 {
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003723 addr_error(addr_type);
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01003724 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003725 goto error;
3726 }
3727 if (*cmd == '&')
3728 i = RE_SUBST;
3729 else if (*cmd == '?' || *cmd == '/')
3730 i = RE_SEARCH;
3731 else
3732 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003733 emsg(_(e_backslash));
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003734 cmd = NULL;
3735 goto error;
3736 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003737
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003738 if (!skip)
3739 {
3740 /*
3741 * When search follows another address, start from
3742 * there.
3743 */
3744 if (lnum != MAXLNUM)
3745 pos.lnum = lnum;
3746 else
3747 pos.lnum = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003748
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003749 /*
3750 * Start the search just like for the above
3751 * do_search().
3752 */
3753 if (*cmd != '?')
3754 pos.col = MAXCOL;
3755 else
3756 pos.col = 0;
Bram Moolenaarbd8539a2015-08-11 18:53:03 +02003757 pos.coladd = 0;
Bram Moolenaar5d24a222018-12-23 19:10:09 +01003758 if (searchit(curwin, curbuf, &pos, NULL,
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003759 *cmd == '?' ? BACKWARD : FORWARD,
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02003760 (char_u *)"", 1L, SEARCH_MSG, i, NULL) != FAIL)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003761 lnum = pos.lnum;
3762 else
3763 {
3764 cmd = NULL;
3765 goto error;
3766 }
3767 }
3768 ++cmd;
3769 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003770
3771 default:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003772 if (VIM_ISDIGIT(*cmd)) // absolute line number
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003773 lnum = getdigits(&cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003774 }
3775
3776 for (;;)
3777 {
3778 cmd = skipwhite(cmd);
3779 if (*cmd != '-' && *cmd != '+' && !VIM_ISDIGIT(*cmd))
3780 break;
3781
3782 if (lnum == MAXLNUM)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003783 {
3784 switch (addr_type)
3785 {
3786 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02003787 case ADDR_OTHER:
3788 // "+1" is same as ".+1"
Bram Moolenaarf240e182014-11-27 18:33:02 +01003789 lnum = curwin->w_cursor.lnum;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003790 break;
3791 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01003792 lnum = CURRENT_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003793 break;
3794 case ADDR_ARGUMENTS:
3795 lnum = curwin->w_arg_idx + 1;
3796 break;
3797 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003798 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003799 lnum = curbuf->b_fnum;
3800 break;
3801 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01003802 lnum = CURRENT_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003803 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01003804 case ADDR_TABS_RELATIVE:
3805 lnum = 1;
3806 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003807 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003808#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02003809 lnum = qf_get_cur_idx(eap);
3810#endif
3811 break;
3812 case ADDR_QUICKFIX_VALID:
3813#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003814 lnum = qf_get_cur_valid_idx(eap);
Bram Moolenaare906c502015-09-09 21:10:39 +02003815#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003816 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02003817 case ADDR_NONE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02003818 case ADDR_UNSIGNED:
3819 lnum = 0;
Bram Moolenaarb7316892019-05-01 18:08:42 +02003820 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003821 }
3822 }
3823
Bram Moolenaar071d4272004-06-13 20:20:40 +00003824 if (VIM_ISDIGIT(*cmd))
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003825 i = '+'; // "number" is same as "+number"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003826 else
3827 i = *cmd++;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003828 if (!VIM_ISDIGIT(*cmd)) // '+' is '+1', but '+0' is not '+1'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003829 n = 1;
3830 else
3831 n = getdigits(&cmd);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01003832
3833 if (addr_type == ADDR_TABS_RELATIVE)
3834 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003835 emsg(_(e_invrange));
Bram Moolenaar2f72c702017-01-29 14:48:10 +01003836 cmd = NULL;
3837 goto error;
3838 }
3839 else if (addr_type == ADDR_LOADED_BUFFERS
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003840 || addr_type == ADDR_BUFFERS)
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01003841 lnum = compute_buffer_local_count(
3842 addr_type, lnum, (i == '-') ? -1 * n : n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003843 else
Bram Moolenaarded27822017-01-02 14:27:34 +01003844 {
3845#ifdef FEAT_FOLDING
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003846 // Relative line addressing, need to adjust for folded lines
3847 // now, but only do it after the first address.
Bram Moolenaarded27822017-01-02 14:27:34 +01003848 if (addr_type == ADDR_LINES && (i == '-' || i == '+')
3849 && address_count >= 2)
3850 (void)hasFolding(lnum, NULL, &lnum);
3851#endif
3852 if (i == '-')
3853 lnum -= n;
3854 else
3855 lnum += n;
3856 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003857 }
3858 } while (*cmd == '/' || *cmd == '?');
3859
3860error:
3861 *ptr = cmd;
3862 return lnum;
3863}
3864
3865/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00003866 * Get flags from an Ex command argument.
3867 */
3868 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003869get_flags(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00003870{
3871 while (vim_strchr((char_u *)"lp#", *eap->arg) != NULL)
3872 {
3873 if (*eap->arg == 'l')
3874 eap->flags |= EXFLAG_LIST;
3875 else if (*eap->arg == 'p')
3876 eap->flags |= EXFLAG_PRINT;
3877 else
3878 eap->flags |= EXFLAG_NR;
3879 eap->arg = skipwhite(eap->arg + 1);
3880 }
3881}
3882
3883/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003884 * Function called for command which is Not Implemented. NI!
3885 */
3886 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003887ex_ni(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003888{
3889 if (!eap->skip)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003890 eap->errmsg = N_("E319: Sorry, the command is not available in this version");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003891}
3892
Bram Moolenaar7bb75552007-07-16 18:39:49 +00003893#ifdef HAVE_EX_SCRIPT_NI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003894/*
3895 * Function called for script command which is Not Implemented. NI!
3896 * Skips over ":perl <<EOF" constructs.
3897 */
3898 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003899ex_script_ni(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003900{
3901 if (!eap->skip)
3902 ex_ni(eap);
3903 else
3904 vim_free(script_get(eap, eap->arg));
3905}
3906#endif
3907
3908/*
3909 * Check range in Ex command for validity.
3910 * Return NULL when valid, error message when invalid.
3911 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003912 static char *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003913invalid_range(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003914{
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003915 buf_T *buf;
Bram Moolenaar25190db2019-05-04 15:05:28 +02003916
Bram Moolenaar071d4272004-06-13 20:20:40 +00003917 if ( eap->line1 < 0
3918 || eap->line2 < 0
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003919 || eap->line1 > eap->line2)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003920 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003921
Bram Moolenaar8071cb22019-07-12 17:58:01 +02003922 if (eap->argt & EX_RANGE)
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003923 {
Bram Moolenaarb7316892019-05-01 18:08:42 +02003924 switch (eap->addr_type)
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003925 {
3926 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02003927 if (eap->line2 > curbuf->b_ml.ml_line_count
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003928#ifdef FEAT_DIFF
3929 + (eap->cmdidx == CMD_diffget)
3930#endif
3931 )
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003932 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003933 break;
3934 case ADDR_ARGUMENTS:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003935 // add 1 if ARGCOUNT is 0
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01003936 if (eap->line2 > ARGCOUNT + (!ARGCOUNT))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003937 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003938 break;
3939 case ADDR_BUFFERS:
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02003940 // Only a boundary check, not whether the buffers actually
3941 // exist.
3942 if (eap->line1 < 1 || eap->line2 > get_highest_fnum())
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003943 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003944 break;
3945 case ADDR_LOADED_BUFFERS:
3946 buf = firstbuf;
3947 while (buf->b_ml.ml_mfp == NULL)
3948 {
3949 if (buf->b_next == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003950 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003951 buf = buf->b_next;
3952 }
3953 if (eap->line1 < buf->b_fnum)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003954 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003955 buf = lastbuf;
3956 while (buf->b_ml.ml_mfp == NULL)
3957 {
3958 if (buf->b_prev == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003959 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003960 buf = buf->b_prev;
3961 }
3962 if (eap->line2 > buf->b_fnum)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003963 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003964 break;
3965 case ADDR_WINDOWS:
Bram Moolenaar8be63882015-01-14 11:25:05 +01003966 if (eap->line2 > LAST_WIN_NR)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003967 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003968 break;
3969 case ADDR_TABS:
3970 if (eap->line2 > LAST_TAB_NR)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003971 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003972 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01003973 case ADDR_TABS_RELATIVE:
Bram Moolenaarb7316892019-05-01 18:08:42 +02003974 case ADDR_OTHER:
3975 // Any range is OK.
Bram Moolenaar2f72c702017-01-29 14:48:10 +01003976 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003977 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003978#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02003979 // No error for value that is too big, will use the last entry.
3980 if (eap->line2 <= 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003981 return _(e_invrange);
Bram Moolenaare906c502015-09-09 21:10:39 +02003982#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003983 break;
Bram Moolenaar25190db2019-05-04 15:05:28 +02003984 case ADDR_QUICKFIX_VALID:
3985#ifdef FEAT_QUICKFIX
3986 if ((eap->line2 != 1 && eap->line2 > qf_get_valid_size(eap))
3987 || eap->line2 < 0)
3988 return _(e_invrange);
3989#endif
3990 break;
3991 case ADDR_UNSIGNED:
3992 if (eap->line2 < 0)
3993 return _(e_invrange);
3994 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02003995 case ADDR_NONE:
3996 // Will give an error elsewhere.
3997 break;
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003998 }
3999 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004000 return NULL;
4001}
4002
4003/*
4004 * Correct the range for zero line number, if required.
4005 */
4006 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004007correct_range(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004008{
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004009 if (!(eap->argt & EX_ZEROR)) // zero in range not allowed
Bram Moolenaar071d4272004-06-13 20:20:40 +00004010 {
4011 if (eap->line1 == 0)
4012 eap->line1 = 1;
4013 if (eap->line2 == 0)
4014 eap->line2 = 1;
4015 }
4016}
4017
Bram Moolenaar748bf032005-02-02 23:04:36 +00004018#ifdef FEAT_QUICKFIX
Bram Moolenaar748bf032005-02-02 23:04:36 +00004019/*
4020 * For a ":vimgrep" or ":vimgrepadd" command return a pointer past the
4021 * pattern. Otherwise return eap->arg.
4022 */
4023 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004024skip_grep_pat(exarg_T *eap)
Bram Moolenaar748bf032005-02-02 23:04:36 +00004025{
4026 char_u *p = eap->arg;
4027
Bram Moolenaara37420f2006-02-04 22:37:47 +00004028 if (*p != NUL && (eap->cmdidx == CMD_vimgrep || eap->cmdidx == CMD_lvimgrep
4029 || eap->cmdidx == CMD_vimgrepadd
4030 || eap->cmdidx == CMD_lvimgrepadd
4031 || grep_internal(eap->cmdidx)))
Bram Moolenaar748bf032005-02-02 23:04:36 +00004032 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00004033 p = skip_vimgrep_pat(p, NULL, NULL);
Bram Moolenaar748bf032005-02-02 23:04:36 +00004034 if (p == NULL)
4035 p = eap->arg;
Bram Moolenaar748bf032005-02-02 23:04:36 +00004036 }
4037 return p;
4038}
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004039
4040/*
4041 * For the ":make" and ":grep" commands insert the 'makeprg'/'grepprg' option
4042 * in the command line, so that things like % get expanded.
4043 */
4044 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004045replace_makeprg(exarg_T *eap, char_u *p, char_u **cmdlinep)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004046{
4047 char_u *new_cmdline;
4048 char_u *program;
4049 char_u *pos;
4050 char_u *ptr;
4051 int len;
4052 int i;
4053
4054 /*
4055 * Don't do it when ":vimgrep" is used for ":grep".
4056 */
Bram Moolenaara37420f2006-02-04 22:37:47 +00004057 if ((eap->cmdidx == CMD_make || eap->cmdidx == CMD_lmake
4058 || eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4059 || eap->cmdidx == CMD_grepadd
4060 || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004061 && !grep_internal(eap->cmdidx))
4062 {
Bram Moolenaara37420f2006-02-04 22:37:47 +00004063 if (eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4064 || eap->cmdidx == CMD_grepadd || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004065 {
4066 if (*curbuf->b_p_gp == NUL)
4067 program = p_gp;
4068 else
4069 program = curbuf->b_p_gp;
4070 }
4071 else
4072 {
4073 if (*curbuf->b_p_mp == NUL)
4074 program = p_mp;
4075 else
4076 program = curbuf->b_p_mp;
4077 }
4078
4079 p = skipwhite(p);
4080
4081 if ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4082 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004083 // replace $* by given arguments
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004084 i = 1;
4085 while ((pos = (char_u *)strstr((char *)pos + 2, "$*")) != NULL)
4086 ++i;
4087 len = (int)STRLEN(p);
Bram Moolenaar51e14382019-05-25 20:21:28 +02004088 new_cmdline = alloc(STRLEN(program) + i * (len - 2) + 1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004089 if (new_cmdline == NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004090 return NULL; // out of memory
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004091 ptr = new_cmdline;
4092 while ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4093 {
4094 i = (int)(pos - program);
4095 STRNCPY(ptr, program, i);
4096 STRCPY(ptr += i, p);
4097 ptr += len;
4098 program = pos + 2;
4099 }
4100 STRCPY(ptr, program);
4101 }
4102 else
4103 {
Bram Moolenaar51e14382019-05-25 20:21:28 +02004104 new_cmdline = alloc(STRLEN(program) + STRLEN(p) + 2);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004105 if (new_cmdline == NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004106 return NULL; // out of memory
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004107 STRCPY(new_cmdline, program);
4108 STRCAT(new_cmdline, " ");
4109 STRCAT(new_cmdline, p);
4110 }
4111 msg_make(p);
4112
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004113 // 'eap->cmd' is not set here, because it is not used at CMD_make
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004114 vim_free(*cmdlinep);
4115 *cmdlinep = new_cmdline;
4116 p = new_cmdline;
4117 }
4118 return p;
4119}
Bram Moolenaar748bf032005-02-02 23:04:36 +00004120#endif
4121
Bram Moolenaar071d4272004-06-13 20:20:40 +00004122/*
4123 * Expand file name in Ex command argument.
Bram Moolenaar0abb4272019-06-15 16:06:00 +02004124 * When an error is detected, "errormsgp" is set to a non-NULL pointer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004125 * Return FAIL for failure, OK otherwise.
4126 */
4127 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004128expand_filename(
4129 exarg_T *eap,
4130 char_u **cmdlinep,
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004131 char **errormsgp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004132{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004133 int has_wildcards; // need to expand wildcards
Bram Moolenaar071d4272004-06-13 20:20:40 +00004134 char_u *repl;
4135 int srclen;
4136 char_u *p;
4137 int n;
Bram Moolenaar63b92542007-03-27 14:57:09 +00004138 int escaped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004139
Bram Moolenaar748bf032005-02-02 23:04:36 +00004140#ifdef FEAT_QUICKFIX
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004141 // Skip a regexp pattern for ":vimgrep[add] pat file..."
Bram Moolenaar748bf032005-02-02 23:04:36 +00004142 p = skip_grep_pat(eap);
4143#else
4144 p = eap->arg;
4145#endif
4146
Bram Moolenaar071d4272004-06-13 20:20:40 +00004147 /*
4148 * Decide to expand wildcards *before* replacing '%', '#', etc. If
4149 * the file name contains a wildcard it should not cause expanding.
4150 * (it will be expanded anyway if there is a wildcard before replacing).
4151 */
Bram Moolenaar748bf032005-02-02 23:04:36 +00004152 has_wildcards = mch_has_wildcard(p);
4153 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004154 {
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004155#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004156 // Skip over `=expr`, wildcards in it are not expanded.
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004157 if (p[0] == '`' && p[1] == '=')
4158 {
4159 p += 2;
4160 (void)skip_expr(&p);
4161 if (*p == '`')
4162 ++p;
4163 continue;
4164 }
4165#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004166 /*
4167 * Quick check if this cannot be the start of a special string.
4168 * Also removes backslash before '%', '#' and '<'.
4169 */
4170 if (vim_strchr((char_u *)"%#<", *p) == NULL)
4171 {
4172 ++p;
4173 continue;
4174 }
4175
4176 /*
4177 * Try to find a match at this position.
4178 */
Bram Moolenaar63b92542007-03-27 14:57:09 +00004179 repl = eval_vars(p, eap->arg, &srclen, &(eap->do_ecmd_lnum),
4180 errormsgp, &escaped);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004181 if (*errormsgp != NULL) // error detected
Bram Moolenaar071d4272004-06-13 20:20:40 +00004182 return FAIL;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004183 if (repl == NULL) // no match found
Bram Moolenaar071d4272004-06-13 20:20:40 +00004184 {
4185 p += srclen;
4186 continue;
4187 }
4188
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004189 // Wildcards won't be expanded below, the replacement is taken
4190 // literally. But do expand "~/file", "~user/file" and "$HOME/file".
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00004191 if (vim_strchr(repl, '$') != NULL || vim_strchr(repl, '~') != NULL)
4192 {
4193 char_u *l = repl;
4194
4195 repl = expand_env_save(repl);
4196 vim_free(l);
4197 }
4198
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004199 // Need to escape white space et al. with a backslash.
4200 // Don't do this for:
4201 // - replacement that already has been escaped: "##"
4202 // - shell commands (may have to use quotes instead).
4203 // - non-unix systems when there is a single argument (spaces don't
4204 // separate arguments then).
Bram Moolenaar071d4272004-06-13 20:20:40 +00004205 if (!eap->usefilter
Bram Moolenaar63b92542007-03-27 14:57:09 +00004206 && !escaped
Bram Moolenaar071d4272004-06-13 20:20:40 +00004207 && eap->cmdidx != CMD_bang
Bram Moolenaar071d4272004-06-13 20:20:40 +00004208 && eap->cmdidx != CMD_grep
4209 && eap->cmdidx != CMD_grepadd
Bram Moolenaarbf15b8d2017-06-04 20:43:48 +02004210 && eap->cmdidx != CMD_hardcopy
Bram Moolenaar67883b42017-07-27 22:57:00 +02004211 && eap->cmdidx != CMD_lgrep
4212 && eap->cmdidx != CMD_lgrepadd
4213 && eap->cmdidx != CMD_lmake
4214 && eap->cmdidx != CMD_make
4215 && eap->cmdidx != CMD_terminal
Bram Moolenaar071d4272004-06-13 20:20:40 +00004216#ifndef UNIX
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004217 && !(eap->argt & EX_NOSPC)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004218#endif
4219 )
4220 {
4221 char_u *l;
4222#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004223 // Don't escape a backslash here, because rem_backslash() doesn't
4224 // remove it later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004225 static char_u *nobslash = (char_u *)" \t\"|";
4226# define ESCAPE_CHARS nobslash
4227#else
4228# define ESCAPE_CHARS escape_chars
4229#endif
4230
4231 for (l = repl; *l; ++l)
4232 if (vim_strchr(ESCAPE_CHARS, *l) != NULL)
4233 {
4234 l = vim_strsave_escaped(repl, ESCAPE_CHARS);
4235 if (l != NULL)
4236 {
4237 vim_free(repl);
4238 repl = l;
4239 }
4240 break;
4241 }
4242 }
4243
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004244 // For a shell command a '!' must be escaped.
Bram Moolenaar67883b42017-07-27 22:57:00 +02004245 if ((eap->usefilter || eap->cmdidx == CMD_bang
4246 || eap->cmdidx == CMD_terminal)
Bram Moolenaar31b7d382014-04-01 18:54:48 +02004247 && vim_strpbrk(repl, (char_u *)"!") != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004248 {
4249 char_u *l;
4250
Bram Moolenaar31b7d382014-04-01 18:54:48 +02004251 l = vim_strsave_escaped(repl, (char_u *)"!");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004252 if (l != NULL)
4253 {
4254 vim_free(repl);
4255 repl = l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004256 }
4257 }
4258
4259 p = repl_cmdline(eap, p, srclen, repl, cmdlinep);
4260 vim_free(repl);
4261 if (p == NULL)
4262 return FAIL;
4263 }
4264
4265 /*
4266 * One file argument: Expand wildcards.
4267 * Don't do this with ":r !command" or ":w !command".
4268 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004269 if ((eap->argt & EX_NOSPC) && !eap->usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004270 {
4271 /*
4272 * May do this twice:
4273 * 1. Replace environment variables.
4274 * 2. Replace any other wildcards, remove backslashes.
4275 */
4276 for (n = 1; n <= 2; ++n)
4277 {
4278 if (n == 2)
4279 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004280 /*
4281 * Halve the number of backslashes (this is Vi compatible).
4282 * For Unix and OS/2, when wildcards are expanded, this is
4283 * done by ExpandOne() below.
4284 */
Bram Moolenaare7fedb62015-12-31 19:07:19 +01004285#if defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004286 if (!has_wildcards)
4287#endif
4288 backslash_halve(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004289 }
4290
4291 if (has_wildcards)
4292 {
4293 if (n == 1)
4294 {
4295 /*
4296 * First loop: May expand environment variables. This
4297 * can be done much faster with expand_env() than with
4298 * something else (e.g., calling a shell).
4299 * After expanding environment variables, check again
4300 * if there are still wildcards present.
4301 */
4302 if (vim_strchr(eap->arg, '$') != NULL
4303 || vim_strchr(eap->arg, '~') != NULL)
4304 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +00004305 expand_env_esc(eap->arg, NameBuff, MAXPATHL,
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00004306 TRUE, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004307 has_wildcards = mch_has_wildcard(NameBuff);
4308 p = NameBuff;
4309 }
4310 else
4311 p = NULL;
4312 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004313 else // n == 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00004314 {
4315 expand_T xpc;
Bram Moolenaarb40c2572019-10-19 21:01:05 +02004316 int options = WILD_LIST_NOTFOUND
4317 | WILD_NOERROR | WILD_ADD_SLASH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004318
4319 ExpandInit(&xpc);
4320 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar94950a92010-12-02 16:01:29 +01004321 if (p_wic)
4322 options += WILD_ICASE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004323 p = ExpandOne(&xpc, eap->arg, NULL,
Bram Moolenaar94950a92010-12-02 16:01:29 +01004324 options, WILD_EXPAND_FREE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004325 if (p == NULL)
4326 return FAIL;
4327 }
4328 if (p != NULL)
4329 {
4330 (void)repl_cmdline(eap, eap->arg, (int)STRLEN(eap->arg),
4331 p, cmdlinep);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004332 if (n == 2) // p came from ExpandOne()
Bram Moolenaar071d4272004-06-13 20:20:40 +00004333 vim_free(p);
4334 }
4335 }
4336 }
4337 }
4338 return OK;
4339}
4340
4341/*
4342 * Replace part of the command line, keeping eap->cmd, eap->arg and
4343 * eap->nextcmd correct.
4344 * "src" points to the part that is to be replaced, of length "srclen".
4345 * "repl" is the replacement string.
4346 * Returns a pointer to the character after the replaced string.
4347 * Returns NULL for failure.
4348 */
4349 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004350repl_cmdline(
4351 exarg_T *eap,
4352 char_u *src,
4353 int srclen,
4354 char_u *repl,
4355 char_u **cmdlinep)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004356{
4357 int len;
4358 int i;
4359 char_u *new_cmdline;
4360
4361 /*
4362 * The new command line is build in new_cmdline[].
4363 * First allocate it.
4364 * Careful: a "+cmd" argument may have been NUL terminated.
4365 */
4366 len = (int)STRLEN(repl);
4367 i = (int)(src - *cmdlinep) + (int)STRLEN(src + srclen) + len + 3;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004368 if (eap->nextcmd != NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004369 i += (int)STRLEN(eap->nextcmd);// add space for next command
Bram Moolenaar964b3742019-05-24 18:54:09 +02004370 if ((new_cmdline = alloc(i)) == NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004371 return NULL; // out of memory!
Bram Moolenaar071d4272004-06-13 20:20:40 +00004372
4373 /*
4374 * Copy the stuff before the expanded part.
4375 * Copy the expanded stuff.
4376 * Copy what came after the expanded part.
4377 * Copy the next commands, if there are any.
4378 */
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004379 i = (int)(src - *cmdlinep); // length of part before match
Bram Moolenaar071d4272004-06-13 20:20:40 +00004380 mch_memmove(new_cmdline, *cmdlinep, (size_t)i);
Bram Moolenaara3ffd9c2005-07-21 21:03:15 +00004381
Bram Moolenaar071d4272004-06-13 20:20:40 +00004382 mch_memmove(new_cmdline + i, repl, (size_t)len);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004383 i += len; // remember the end of the string
Bram Moolenaar071d4272004-06-13 20:20:40 +00004384 STRCPY(new_cmdline + i, src + srclen);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004385 src = new_cmdline + i; // remember where to continue
Bram Moolenaar071d4272004-06-13 20:20:40 +00004386
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004387 if (eap->nextcmd != NULL) // append next command
Bram Moolenaar071d4272004-06-13 20:20:40 +00004388 {
4389 i = (int)STRLEN(new_cmdline) + 1;
4390 STRCPY(new_cmdline + i, eap->nextcmd);
4391 eap->nextcmd = new_cmdline + i;
4392 }
4393 eap->cmd = new_cmdline + (eap->cmd - *cmdlinep);
4394 eap->arg = new_cmdline + (eap->arg - *cmdlinep);
4395 if (eap->do_ecmd_cmd != NULL && eap->do_ecmd_cmd != dollar_command)
4396 eap->do_ecmd_cmd = new_cmdline + (eap->do_ecmd_cmd - *cmdlinep);
4397 vim_free(*cmdlinep);
4398 *cmdlinep = new_cmdline;
4399
4400 return src;
4401}
4402
4403/*
4404 * Check for '|' to separate commands and '"' to start comments.
4405 */
4406 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004407separate_nextcmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004408{
4409 char_u *p;
4410
Bram Moolenaar86b68352004-12-27 21:59:20 +00004411#ifdef FEAT_QUICKFIX
Bram Moolenaar748bf032005-02-02 23:04:36 +00004412 p = skip_grep_pat(eap);
4413#else
4414 p = eap->arg;
Bram Moolenaar86b68352004-12-27 21:59:20 +00004415#endif
4416
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004417 for ( ; *p; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004418 {
4419 if (*p == Ctrl_V)
4420 {
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004421 if (eap->argt & (EX_CTRLV | EX_XFILE))
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004422 ++p; // skip CTRL-V and next char
Bram Moolenaar071d4272004-06-13 20:20:40 +00004423 else
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004424 // remove CTRL-V and skip next char
Bram Moolenaara7241f52008-06-24 20:39:31 +00004425 STRMOVE(p, p + 1);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004426 if (*p == NUL) // stop at NUL after CTRL-V
Bram Moolenaar071d4272004-06-13 20:20:40 +00004427 break;
4428 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004429
4430#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004431 // Skip over `=expr` when wildcards are expanded.
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004432 else if (p[0] == '`' && p[1] == '=' && (eap->argt & EX_XFILE))
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004433 {
4434 p += 2;
4435 (void)skip_expr(&p);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004436 }
4437#endif
4438
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004439 // Check for '"': start of comment or '|': next command
4440 // :@" and :*" do not start a comment!
4441 // :redir @" doesn't either.
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004442 else if ((*p == '"' && !(eap->argt & EX_NOTRLCOM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004443 && ((eap->cmdidx != CMD_at && eap->cmdidx != CMD_star)
4444 || p != eap->arg)
4445 && (eap->cmdidx != CMD_redir
4446 || p != eap->arg + 1 || p[-1] != '@'))
Bram Moolenaara26b9702020-04-18 19:53:28 +02004447#ifdef FEAT_EVAL
4448 || (*p == '#' && in_vim9script()
4449 && p[1] != '{' && p > eap->cmd && VIM_ISWHITE(p[-1]))
4450#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004451 || *p == '|' || *p == '\n')
4452 {
4453 /*
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004454 * We remove the '\' before the '|', unless EX_CTRLV is used
Bram Moolenaar071d4272004-06-13 20:20:40 +00004455 * AND 'b' is present in 'cpoptions'.
4456 */
4457 if ((vim_strchr(p_cpo, CPO_BAR) == NULL
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004458 || !(eap->argt & EX_CTRLV)) && *(p - 1) == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004459 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004460 STRMOVE(p - 1, p); // remove the '\'
Bram Moolenaar071d4272004-06-13 20:20:40 +00004461 --p;
4462 }
4463 else
4464 {
4465 eap->nextcmd = check_nextcmd(p);
4466 *p = NUL;
4467 break;
4468 }
4469 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004470 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00004471
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004472 if (!(eap->argt & EX_NOTRLCOM)) // remove trailing spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00004473 del_trailing_spaces(eap->arg);
4474}
4475
4476/*
4477 * get + command from ex argument
4478 */
4479 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004480getargcmd(char_u **argp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004481{
4482 char_u *arg = *argp;
4483 char_u *command = NULL;
4484
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004485 if (*arg == '+') // +[command]
Bram Moolenaar071d4272004-06-13 20:20:40 +00004486 {
4487 ++arg;
Bram Moolenaar3e451592014-04-02 14:22:05 +02004488 if (vim_isspace(*arg) || *arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004489 command = dollar_command;
4490 else
4491 {
4492 command = arg;
4493 arg = skip_cmd_arg(command, TRUE);
4494 if (*arg != NUL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004495 *arg++ = NUL; // terminate command with NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004496 }
4497
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004498 arg = skipwhite(arg); // skip over spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00004499 *argp = arg;
4500 }
4501 return command;
4502}
4503
4504/*
4505 * Find end of "+command" argument. Skip over "\ " and "\\".
4506 */
Bram Moolenaard0190392019-08-23 21:17:35 +02004507 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004508skip_cmd_arg(
4509 char_u *p,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004510 int rembs) // TRUE to halve the number of backslashes
Bram Moolenaar071d4272004-06-13 20:20:40 +00004511{
4512 while (*p && !vim_isspace(*p))
4513 {
4514 if (*p == '\\' && p[1] != NUL)
4515 {
4516 if (rembs)
Bram Moolenaara7241f52008-06-24 20:39:31 +00004517 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004518 else
4519 ++p;
4520 }
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004521 MB_PTR_ADV(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004522 }
4523 return p;
4524}
4525
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004526 int
4527get_bad_opt(char_u *p, exarg_T *eap)
4528{
4529 if (STRICMP(p, "keep") == 0)
4530 eap->bad_char = BAD_KEEP;
4531 else if (STRICMP(p, "drop") == 0)
4532 eap->bad_char = BAD_DROP;
4533 else if (MB_BYTE2LEN(*p) == 1 && p[1] == NUL)
4534 eap->bad_char = *p;
Bram Moolenaar75808492018-06-12 12:39:41 +02004535 else
4536 return FAIL;
4537 return OK;
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004538}
4539
Bram Moolenaar071d4272004-06-13 20:20:40 +00004540/*
4541 * Get "++opt=arg" argument.
4542 * Return FAIL or OK.
4543 */
4544 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004545getargopt(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004546{
4547 char_u *arg = eap->arg + 2;
4548 int *pp = NULL;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02004549 int bad_char_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004550 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004551
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004552 // ":edit ++[no]bin[ary] file"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004553 if (STRNCMP(arg, "bin", 3) == 0 || STRNCMP(arg, "nobin", 5) == 0)
4554 {
4555 if (*arg == 'n')
4556 {
4557 arg += 2;
4558 eap->force_bin = FORCE_NOBIN;
4559 }
4560 else
4561 eap->force_bin = FORCE_BIN;
4562 if (!checkforcmd(&arg, "binary", 3))
4563 return FAIL;
4564 eap->arg = skipwhite(arg);
4565 return OK;
4566 }
4567
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004568 // ":read ++edit file"
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004569 if (STRNCMP(arg, "edit", 4) == 0)
4570 {
4571 eap->read_edit = TRUE;
4572 eap->arg = skipwhite(arg + 4);
4573 return OK;
4574 }
4575
Bram Moolenaar071d4272004-06-13 20:20:40 +00004576 if (STRNCMP(arg, "ff", 2) == 0)
4577 {
4578 arg += 2;
4579 pp = &eap->force_ff;
4580 }
4581 else if (STRNCMP(arg, "fileformat", 10) == 0)
4582 {
4583 arg += 10;
4584 pp = &eap->force_ff;
4585 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004586 else if (STRNCMP(arg, "enc", 3) == 0)
4587 {
Bram Moolenaarb38e9ab2011-12-14 14:49:45 +01004588 if (STRNCMP(arg, "encoding", 8) == 0)
4589 arg += 8;
4590 else
4591 arg += 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004592 pp = &eap->force_enc;
4593 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004594 else if (STRNCMP(arg, "bad", 3) == 0)
4595 {
4596 arg += 3;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02004597 pp = &bad_char_idx;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004598 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004599
4600 if (pp == NULL || *arg != '=')
4601 return FAIL;
4602
4603 ++arg;
4604 *pp = (int)(arg - eap->cmd);
4605 arg = skip_cmd_arg(arg, FALSE);
4606 eap->arg = skipwhite(arg);
4607 *arg = NUL;
4608
Bram Moolenaar071d4272004-06-13 20:20:40 +00004609 if (pp == &eap->force_ff)
4610 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004611 if (check_ff_value(eap->cmd + eap->force_ff) == FAIL)
4612 return FAIL;
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004613 eap->force_ff = eap->cmd[eap->force_ff];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004614 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004615 else if (pp == &eap->force_enc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004616 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004617 // Make 'fileencoding' lower case.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004618 for (p = eap->cmd + eap->force_enc; *p != NUL; ++p)
4619 *p = TOLOWER_ASC(*p);
4620 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004621 else
4622 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004623 // Check ++bad= argument. Must be a single-byte character, "keep" or
4624 // "drop".
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004625 if (get_bad_opt(eap->cmd + bad_char_idx, eap) == FAIL)
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004626 return FAIL;
4627 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004628
4629 return OK;
4630}
4631
Bram Moolenaar071d4272004-06-13 20:20:40 +00004632 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004633ex_autocmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004634{
4635 /*
Bram Moolenaar26d4b892018-07-04 22:26:28 +02004636 * Disallow autocommands from .exrc and .vimrc in current
Bram Moolenaar071d4272004-06-13 20:20:40 +00004637 * directory for security reasons.
4638 */
4639 if (secure)
4640 {
4641 secure = 2;
4642 eap->errmsg = e_curdir;
4643 }
4644 else if (eap->cmdidx == CMD_autocmd)
4645 do_autocmd(eap->arg, eap->forceit);
4646 else
4647 do_augroup(eap->arg, eap->forceit);
4648}
4649
4650/*
4651 * ":doautocmd": Apply the automatic commands to the current buffer.
4652 */
4653 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004654ex_doautocmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004655{
Bram Moolenaar60542ac2012-02-12 20:14:01 +01004656 char_u *arg = eap->arg;
4657 int call_do_modelines = check_nomodeline(&arg);
Bram Moolenaar1610d052016-06-09 22:53:01 +02004658 int did_aucmd;
Bram Moolenaar60542ac2012-02-12 20:14:01 +01004659
Bram Moolenaar1610d052016-06-09 22:53:01 +02004660 (void)do_doautocmd(arg, TRUE, &did_aucmd);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004661 // Only when there is no <nomodeline>.
Bram Moolenaar1610d052016-06-09 22:53:01 +02004662 if (call_do_modelines && did_aucmd)
Bram Moolenaar60542ac2012-02-12 20:14:01 +01004663 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004664}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004665
Bram Moolenaar071d4272004-06-13 20:20:40 +00004666/*
4667 * :[N]bunload[!] [N] [bufname] unload buffer
4668 * :[N]bdelete[!] [N] [bufname] delete buffer from buffer list
4669 * :[N]bwipeout[!] [N] [bufname] delete buffer really
4670 */
4671 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004672ex_bunload(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004673{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01004674 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02004675 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004676 eap->errmsg = do_bufdel(
4677 eap->cmdidx == CMD_bdelete ? DOBUF_DEL
4678 : eap->cmdidx == CMD_bwipeout ? DOBUF_WIPE
4679 : DOBUF_UNLOAD, eap->arg,
4680 eap->addr_count, (int)eap->line1, (int)eap->line2, eap->forceit);
4681}
4682
4683/*
4684 * :[N]buffer [N] to buffer N
4685 * :[N]sbuffer [N] to buffer N
4686 */
4687 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004688ex_buffer(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004689{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01004690 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02004691 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004692 if (*eap->arg)
4693 eap->errmsg = e_trailing;
4694 else
4695 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004696 if (eap->addr_count == 0) // default is current buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00004697 goto_buffer(eap, DOBUF_CURRENT, FORWARD, 0);
4698 else
4699 goto_buffer(eap, DOBUF_FIRST, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02004700 if (eap->do_ecmd_cmd != NULL)
4701 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004702 }
4703}
4704
4705/*
4706 * :[N]bmodified [N] to next mod. buffer
4707 * :[N]sbmodified [N] to next mod. buffer
4708 */
4709 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004710ex_bmodified(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004711{
4712 goto_buffer(eap, DOBUF_MOD, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02004713 if (eap->do_ecmd_cmd != NULL)
4714 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004715}
4716
4717/*
4718 * :[N]bnext [N] to next buffer
4719 * :[N]sbnext [N] split and to next buffer
4720 */
4721 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004722ex_bnext(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004723{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01004724 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar3029bcc2020-01-18 15:06:19 +01004725 return;
4726
Bram Moolenaar071d4272004-06-13 20:20:40 +00004727 goto_buffer(eap, DOBUF_CURRENT, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02004728 if (eap->do_ecmd_cmd != NULL)
4729 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004730}
4731
4732/*
4733 * :[N]bNext [N] to previous buffer
4734 * :[N]bprevious [N] to previous buffer
4735 * :[N]sbNext [N] split and to previous buffer
4736 * :[N]sbprevious [N] split and to previous buffer
4737 */
4738 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004739ex_bprevious(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004740{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01004741 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar3029bcc2020-01-18 15:06:19 +01004742 return;
4743
Bram Moolenaar071d4272004-06-13 20:20:40 +00004744 goto_buffer(eap, DOBUF_CURRENT, BACKWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02004745 if (eap->do_ecmd_cmd != NULL)
4746 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004747}
4748
4749/*
4750 * :brewind to first buffer
4751 * :bfirst to first buffer
4752 * :sbrewind split and to first buffer
4753 * :sbfirst split and to first buffer
4754 */
4755 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004756ex_brewind(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004757{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01004758 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar3029bcc2020-01-18 15:06:19 +01004759 return;
4760
Bram Moolenaar071d4272004-06-13 20:20:40 +00004761 goto_buffer(eap, DOBUF_FIRST, FORWARD, 0);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02004762 if (eap->do_ecmd_cmd != NULL)
4763 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004764}
4765
4766/*
4767 * :blast to last buffer
4768 * :sblast split and to last buffer
4769 */
4770 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004771ex_blast(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004772{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01004773 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar3029bcc2020-01-18 15:06:19 +01004774 return;
4775
Bram Moolenaar071d4272004-06-13 20:20:40 +00004776 goto_buffer(eap, DOBUF_LAST, BACKWARD, 0);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02004777 if (eap->do_ecmd_cmd != NULL)
4778 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004779}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004780
Bram Moolenaar7a092242020-04-16 22:10:49 +02004781/*
4782 * Check if "c" ends an Ex command.
Bram Moolenaara494f562020-04-18 17:45:38 +02004783 * In Vim9 script does not check for white space before # or #{.
Bram Moolenaar7a092242020-04-16 22:10:49 +02004784 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004785 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004786ends_excmd(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004787{
Bram Moolenaarfbda69b2020-04-13 15:06:53 +02004788#ifdef FEAT_EVAL
Bram Moolenaar2c330432020-04-13 14:41:35 +02004789 if (c == '#')
Bram Moolenaar7a092242020-04-16 22:10:49 +02004790 return in_vim9script();
4791#endif
4792 return (c == NUL || c == '|' || c == '"' || c == '\n');
4793}
4794
4795/*
4796 * Like ends_excmd() but checks that a # in Vim9 script either has "cmd" equal
4797 * to "cmd_start" or has a white space character before it.
4798 */
4799 int
Bram Moolenaar21cfe502020-04-16 23:01:50 +02004800ends_excmd2(char_u *cmd_start UNUSED, char_u *cmd)
Bram Moolenaar7a092242020-04-16 22:10:49 +02004801{
4802 int c = *cmd;
4803
4804#ifdef FEAT_EVAL
Bram Moolenaara26b9702020-04-18 19:53:28 +02004805 if (c == '#' && cmd[1] != '{' && (cmd == cmd_start || VIM_ISWHITE(cmd[-1])))
Bram Moolenaar2c330432020-04-13 14:41:35 +02004806 return in_vim9script();
Bram Moolenaarfbda69b2020-04-13 15:06:53 +02004807#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004808 return (c == NUL || c == '|' || c == '"' || c == '\n');
4809}
4810
4811#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA) || defined(FEAT_EVAL) \
4812 || defined(PROTO)
4813/*
4814 * Return the next command, after the first '|' or '\n'.
4815 * Return NULL if not found.
4816 */
4817 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004818find_nextcmd(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004819{
4820 while (*p != '|' && *p != '\n')
4821 {
4822 if (*p == NUL)
4823 return NULL;
4824 ++p;
4825 }
4826 return (p + 1);
4827}
4828#endif
4829
4830/*
Bram Moolenaar2256c992016-11-15 21:17:07 +01004831 * Check if *p is a separator between Ex commands, skipping over white space.
4832 * Return NULL if it isn't, the following character if it is.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004833 */
4834 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004835check_nextcmd(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004836{
Bram Moolenaar2256c992016-11-15 21:17:07 +01004837 char_u *s = skipwhite(p);
4838
4839 if (*s == '|' || *s == '\n')
4840 return (s + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004841 else
4842 return NULL;
4843}
4844
4845/*
4846 * - if there are more files to edit
4847 * - and this is the last window
4848 * - and forceit not used
4849 * - and not repeated twice on a row
4850 * return FAIL and give error message if 'message' TRUE
4851 * return OK otherwise
4852 */
4853 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004854check_more(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004855 int message, // when FALSE check only, no messages
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004856 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004857{
4858 int n = ARGCOUNT - curwin->w_arg_idx - 1;
4859
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00004860 if (!forceit && only_one_window()
4861 && ARGCOUNT > 1 && !arg_had_last && n >= 0 && quitmore == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004862 {
4863 if (message)
4864 {
4865#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
4866 if ((p_confirm || cmdmod.confirm) && curbuf->b_fname != NULL)
4867 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02004868 char_u buff[DIALOG_MSG_SIZE];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004869
Bram Moolenaarda6e8912018-08-21 15:12:14 +02004870 vim_snprintf((char *)buff, DIALOG_MSG_SIZE,
4871 NGETTEXT("%d more file to edit. Quit anyway?",
4872 "%d more files to edit. Quit anyway?", n), n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004873 if (vim_dialog_yesno(VIM_QUESTION, NULL, buff, 1) == VIM_YES)
4874 return OK;
4875 return FAIL;
4876 }
4877#endif
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01004878 semsg(NGETTEXT("E173: %d more file to edit",
4879 "E173: %d more files to edit", n), n);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004880 quitmore = 2; // next try to quit is allowed
Bram Moolenaar071d4272004-06-13 20:20:40 +00004881 }
4882 return FAIL;
4883 }
4884 return OK;
4885}
4886
Bram Moolenaar071d4272004-06-13 20:20:40 +00004887/*
4888 * Function given to ExpandGeneric() to obtain the list of command names.
4889 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004890 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004891get_command_name(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004892{
4893 if (idx >= (int)CMD_SIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004894 return get_user_command_name(idx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004895 return cmdnames[idx].cmd_name;
4896}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004897
Bram Moolenaar071d4272004-06-13 20:20:40 +00004898 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004899ex_colorscheme(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004900{
Bram Moolenaare6850792010-05-14 15:28:44 +02004901 if (*eap->arg == NUL)
4902 {
4903#ifdef FEAT_EVAL
4904 char_u *expr = vim_strsave((char_u *)"g:colors_name");
4905 char_u *p = NULL;
4906
4907 if (expr != NULL)
4908 {
4909 ++emsg_off;
4910 p = eval_to_string(expr, NULL, FALSE);
4911 --emsg_off;
4912 vim_free(expr);
4913 }
4914 if (p != NULL)
4915 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01004916 msg((char *)p);
Bram Moolenaare6850792010-05-14 15:28:44 +02004917 vim_free(p);
4918 }
4919 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01004920 msg("default");
Bram Moolenaare6850792010-05-14 15:28:44 +02004921#else
Bram Moolenaar32526b32019-01-19 17:43:09 +01004922 msg(_("unknown"));
Bram Moolenaare6850792010-05-14 15:28:44 +02004923#endif
4924 }
4925 else if (load_colors(eap->arg) == FAIL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004926 semsg(_("E185: Cannot find color scheme '%s'"), eap->arg);
Bram Moolenaarf58d81a2019-01-28 20:19:05 +01004927
4928#ifdef FEAT_VTP
4929 else if (has_vtp_working())
4930 {
4931 // background color change requires clear + redraw
4932 update_screen(CLEAR);
4933 redrawcmd();
4934 }
4935#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004936}
4937
4938 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004939ex_highlight(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004940{
4941 if (*eap->arg == NUL && eap->cmd[2] == '!')
Bram Moolenaar32526b32019-01-19 17:43:09 +01004942 msg(_("Greetings, Vim user!"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004943 do_highlight(eap->arg, eap->forceit, FALSE);
4944}
4945
4946
4947/*
4948 * Call this function if we thought we were going to exit, but we won't
4949 * (because of an error). May need to restore the terminal mode.
4950 */
4951 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004952not_exiting(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004953{
4954 exiting = FALSE;
4955 settmode(TMODE_RAW);
4956}
4957
Bram Moolenaar12a96de2018-03-11 14:44:18 +01004958 static int
4959before_quit_autocmds(win_T *wp, int quit_all, int forceit)
4960{
4961 apply_autocmds(EVENT_QUITPRE, NULL, NULL, FALSE, wp->w_buffer);
4962
Bram Moolenaar34ba06b2019-10-20 22:27:10 +02004963 // Bail out when autocommands closed the window.
4964 // Refuse to quit when the buffer in the last window is being closed (can
4965 // only happen in autocommands).
Bram Moolenaar12a96de2018-03-11 14:44:18 +01004966 if (!win_valid(wp)
4967 || curbuf_locked()
4968 || (wp->w_buffer->b_nwindows == 1 && wp->w_buffer->b_locked > 0))
4969 return TRUE;
4970
4971 if (quit_all || (check_more(FALSE, forceit) == OK && only_one_window()))
4972 {
4973 apply_autocmds(EVENT_EXITPRE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar34ba06b2019-10-20 22:27:10 +02004974 // Refuse to quit when locked or when the window was closed or the
4975 // buffer in the last window is being closed (can only happen in
4976 // autocommands).
4977 if (!win_valid(wp) || curbuf_locked()
Bram Moolenaar12a96de2018-03-11 14:44:18 +01004978 || (curbuf->b_nwindows == 1 && curbuf->b_locked > 0))
4979 return TRUE;
4980 }
4981
4982 return FALSE;
4983}
4984
Bram Moolenaar071d4272004-06-13 20:20:40 +00004985/*
Bram Moolenaarf240e182014-11-27 18:33:02 +01004986 * ":quit": quit current window, quit Vim if the last window is closed.
Bram Moolenaar12a96de2018-03-11 14:44:18 +01004987 * ":{nr}quit": quit window {nr}
Bram Moolenaar4d14bac2019-10-20 21:15:15 +02004988 * Also used when closing a terminal window that's the last one.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004989 */
Bram Moolenaar4d14bac2019-10-20 21:15:15 +02004990 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004991ex_quit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004992{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004993 win_T *wp;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004994
Bram Moolenaar071d4272004-06-13 20:20:40 +00004995#ifdef FEAT_CMDWIN
4996 if (cmdwin_type != 0)
4997 {
4998 cmdwin_result = Ctrl_C;
4999 return;
5000 }
5001#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005002 // Don't quit while editing the command line.
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005003 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005004 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005005 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005006 return;
5007 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005008 if (eap->addr_count > 0)
5009 {
Bram Moolenaarf240e182014-11-27 18:33:02 +01005010 int wnr = eap->line2;
5011
5012 for (wp = firstwin; wp->w_next != NULL; wp = wp->w_next)
5013 if (--wnr <= 0)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005014 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005015 }
5016 else
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005017 wp = curwin;
Bram Moolenaarf240e182014-11-27 18:33:02 +01005018
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005019 // Refuse to quit when locked.
Bram Moolenaar87ffb5c2017-10-19 12:37:42 +02005020 if (curbuf_locked())
5021 return;
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005022
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005023 // Trigger QuitPre and maybe ExitPre
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005024 if (before_quit_autocmds(wp, FALSE, eap->forceit))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005025 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005026
5027#ifdef FEAT_NETBEANS_INTG
5028 netbeansForcedQuit = eap->forceit;
5029#endif
5030
5031 /*
5032 * If there are more files or windows we won't exit.
5033 */
5034 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
5035 exiting = TRUE;
Bram Moolenaarff930ca2017-10-19 17:12:10 +02005036 if ((!buf_hide(wp->w_buffer)
5037 && check_changed(wp->w_buffer, (p_awa ? CCGD_AW : 0)
Bram Moolenaar45d3b142013-11-09 03:31:51 +01005038 | (eap->forceit ? CCGD_FORCEIT : 0)
5039 | CCGD_EXCMD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005040 || check_more(TRUE, eap->forceit) == FAIL
Bram Moolenaar027387f2016-01-02 22:25:52 +01005041 || (only_one_window() && check_changed_any(eap->forceit, TRUE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005042 {
5043 not_exiting();
5044 }
5045 else
5046 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005047 // quit last window
5048 // Note: only_one_window() returns true, even so a help window is
5049 // still open. In that case only quit, if no address has been
5050 // specified. Example:
5051 // :h|wincmd w|1q - don't quit
5052 // :h|wincmd w|q - quit
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01005053 if (only_one_window() && (ONE_WINDOW || eap->addr_count == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005054 getout(0);
Bram Moolenaar2c33d7b2017-10-14 16:06:20 +02005055 not_exiting();
Bram Moolenaar4033c552017-09-16 20:54:51 +02005056#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00005057 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005058#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005059 // close window; may free buffer
Bram Moolenaareb44a682017-08-03 22:44:55 +02005060 win_close(wp, !buf_hide(wp->w_buffer) || eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005061 }
5062}
5063
5064/*
5065 * ":cquit".
5066 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005067 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005068ex_cquit(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005069{
Bram Moolenaar1860bde2020-01-06 21:47:21 +01005070 // this does not always pass on the exit code to the Manx compiler. why?
5071 getout(eap->addr_count > 0 ? (int)eap->line2 : EXIT_FAILURE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005072}
5073
5074/*
5075 * ":qall": try to quit all windows
5076 */
5077 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005078ex_quit_all(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005079{
5080# ifdef FEAT_CMDWIN
5081 if (cmdwin_type != 0)
5082 {
5083 if (eap->forceit)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005084 cmdwin_result = K_XF1; // ex_window() takes care of this
Bram Moolenaar071d4272004-06-13 20:20:40 +00005085 else
5086 cmdwin_result = K_XF2;
5087 return;
5088 }
5089# endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005090
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005091 // Don't quit while editing the command line.
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005092 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005093 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005094 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005095 return;
5096 }
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005097
5098 if (before_quit_autocmds(curwin, TRUE, eap->forceit))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005099 return;
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005100
Bram Moolenaar071d4272004-06-13 20:20:40 +00005101 exiting = TRUE;
Bram Moolenaar027387f2016-01-02 22:25:52 +01005102 if (eap->forceit || !check_changed_any(FALSE, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005103 getout(0);
5104 not_exiting();
5105}
5106
Bram Moolenaar071d4272004-06-13 20:20:40 +00005107/*
5108 * ":close": close current window, unless it is the last one
5109 */
5110 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005111ex_close(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005112{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005113 win_T *win;
5114 int winnr = 0;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005115#ifdef FEAT_CMDWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00005116 if (cmdwin_type != 0)
Bram Moolenaar9bd1a7e2011-05-19 14:50:54 +02005117 cmdwin_result = Ctrl_C;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005118 else
Bram Moolenaar4033c552017-09-16 20:54:51 +02005119#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005120 if (!text_locked() && !curbuf_locked())
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005121 {
5122 if (eap->addr_count == 0)
5123 ex_win_close(eap->forceit, curwin, NULL);
Bram Moolenaar6d41c782018-06-06 09:11:12 +02005124 else
5125 {
Bram Moolenaar29323592016-07-24 22:04:11 +02005126 FOR_ALL_WINDOWS(win)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005127 {
5128 winnr++;
5129 if (winnr == eap->line2)
5130 break;
5131 }
5132 if (win == NULL)
5133 win = lastwin;
5134 ex_win_close(eap->forceit, win, NULL);
5135 }
5136 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005137}
5138
Bram Moolenaar4033c552017-09-16 20:54:51 +02005139#ifdef FEAT_QUICKFIX
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005140/*
5141 * ":pclose": Close any preview window.
5142 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005143 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005144ex_pclose(exarg_T *eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005145{
5146 win_T *win;
5147
Bram Moolenaar79648732019-07-18 21:43:07 +02005148 // First close any normal window.
Bram Moolenaar29323592016-07-24 22:04:11 +02005149 FOR_ALL_WINDOWS(win)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005150 if (win->w_p_pvw)
5151 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00005152 ex_win_close(eap->forceit, win, NULL);
Bram Moolenaar79648732019-07-18 21:43:07 +02005153 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005154 }
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01005155# ifdef FEAT_PROP_POPUP
Bram Moolenaar79648732019-07-18 21:43:07 +02005156 // Also when 'previewpopup' is empty, it might have been cleared.
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02005157 popup_close_preview();
Bram Moolenaar79648732019-07-18 21:43:07 +02005158# endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005159}
Bram Moolenaar4033c552017-09-16 20:54:51 +02005160#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005161
Bram Moolenaarf740b292006-02-16 22:11:02 +00005162/*
5163 * Close window "win" and take care of handling closing the last window for a
5164 * modified buffer.
5165 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005166 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005167ex_win_close(
5168 int forceit,
5169 win_T *win,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005170 tabpage_T *tp) // NULL or the tab page "win" is in
Bram Moolenaar071d4272004-06-13 20:20:40 +00005171{
5172 int need_hide;
5173 buf_T *buf = win->w_buffer;
5174
5175 need_hide = (bufIsChanged(buf) && buf->b_nwindows <= 1);
Bram Moolenaareb44a682017-08-03 22:44:55 +02005176 if (need_hide && !buf_hide(buf) && !forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005177 {
Bram Moolenaar4033c552017-09-16 20:54:51 +02005178#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005179 if ((p_confirm || cmdmod.confirm) && p_write)
5180 {
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005181 bufref_T bufref;
5182
5183 set_bufref(&bufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005184 dialog_changed(buf, FALSE);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005185 if (bufref_valid(&bufref) && bufIsChanged(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005186 return;
5187 need_hide = FALSE;
5188 }
5189 else
Bram Moolenaar4033c552017-09-16 20:54:51 +02005190#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005191 {
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02005192 no_write_message();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005193 return;
5194 }
5195 }
5196
Bram Moolenaar4033c552017-09-16 20:54:51 +02005197#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00005198 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005199#endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00005200
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005201 // free buffer when not hiding it or when it's a scratch buffer
Bram Moolenaarf740b292006-02-16 22:11:02 +00005202 if (tp == NULL)
Bram Moolenaareb44a682017-08-03 22:44:55 +02005203 win_close(win, !need_hide && !buf_hide(buf));
Bram Moolenaarf740b292006-02-16 22:11:02 +00005204 else
Bram Moolenaareb44a682017-08-03 22:44:55 +02005205 win_close_othertab(win, !need_hide && !buf_hide(buf), tp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005206}
5207
Bram Moolenaar071d4272004-06-13 20:20:40 +00005208/*
Bram Moolenaardea25702017-01-29 15:18:10 +01005209 * Handle the argument for a tabpage related ex command.
5210 * Returns a tabpage number.
5211 * When an error is encountered then eap->errmsg is set.
5212 */
5213 static int
5214get_tabpage_arg(exarg_T *eap)
5215{
5216 int tab_number;
5217 int unaccept_arg0 = (eap->cmdidx == CMD_tabmove) ? 0 : 1;
5218
5219 if (eap->arg && *eap->arg != NUL)
5220 {
5221 char_u *p = eap->arg;
5222 char_u *p_save;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005223 int relative = 0; // argument +N/-N means: go to N places to the
5224 // right/left relative to the current position.
Bram Moolenaardea25702017-01-29 15:18:10 +01005225
5226 if (*p == '-')
5227 {
5228 relative = -1;
5229 p++;
5230 }
5231 else if (*p == '+')
5232 {
5233 relative = 1;
5234 p++;
5235 }
5236
5237 p_save = p;
5238 tab_number = getdigits(&p);
5239
5240 if (relative == 0)
5241 {
5242 if (STRCMP(p, "$") == 0)
5243 tab_number = LAST_TAB_NR;
5244 else if (p == p_save || *p_save == '-' || *p != NUL
5245 || tab_number > LAST_TAB_NR)
5246 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005247 // No numbers as argument.
Bram Moolenaardea25702017-01-29 15:18:10 +01005248 eap->errmsg = e_invarg;
5249 goto theend;
5250 }
5251 }
5252 else
5253 {
5254 if (*p_save == NUL)
5255 tab_number = 1;
5256 else if (p == p_save || *p_save == '-' || *p != NUL
5257 || tab_number == 0)
5258 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005259 // No numbers as argument.
Bram Moolenaardea25702017-01-29 15:18:10 +01005260 eap->errmsg = e_invarg;
5261 goto theend;
5262 }
5263 tab_number = tab_number * relative + tabpage_index(curtab);
5264 if (!unaccept_arg0 && relative == -1)
5265 --tab_number;
5266 }
5267 if (tab_number < unaccept_arg0 || tab_number > LAST_TAB_NR)
5268 eap->errmsg = e_invarg;
5269 }
5270 else if (eap->addr_count > 0)
5271 {
5272 if (unaccept_arg0 && eap->line2 == 0)
Bram Moolenaarc6251552017-01-29 21:42:20 +01005273 {
Bram Moolenaardea25702017-01-29 15:18:10 +01005274 eap->errmsg = e_invrange;
Bram Moolenaarc6251552017-01-29 21:42:20 +01005275 tab_number = 0;
5276 }
Bram Moolenaardea25702017-01-29 15:18:10 +01005277 else
5278 {
5279 tab_number = eap->line2;
Bram Moolenaar82a12462019-01-22 22:41:42 +01005280 if (!unaccept_arg0 && *skipwhite(*eap->cmdlinep) == '-')
Bram Moolenaardea25702017-01-29 15:18:10 +01005281 {
5282 --tab_number;
5283 if (tab_number < unaccept_arg0)
5284 eap->errmsg = e_invarg;
5285 }
5286 }
5287 }
5288 else
5289 {
5290 switch (eap->cmdidx)
5291 {
5292 case CMD_tabnext:
5293 tab_number = tabpage_index(curtab) + 1;
5294 if (tab_number > LAST_TAB_NR)
5295 tab_number = 1;
5296 break;
5297 case CMD_tabmove:
5298 tab_number = LAST_TAB_NR;
5299 break;
5300 default:
5301 tab_number = tabpage_index(curtab);
5302 }
5303 }
5304
5305theend:
5306 return tab_number;
5307}
5308
5309/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00005310 * ":tabclose": close current tab page, unless it is the last one.
5311 * ":tabclose N": close tab page N.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005312 */
5313 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005314ex_tabclose(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005315{
Bram Moolenaarf740b292006-02-16 22:11:02 +00005316 tabpage_T *tp;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005317 int tab_number;
Bram Moolenaarf740b292006-02-16 22:11:02 +00005318
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005319# ifdef FEAT_CMDWIN
5320 if (cmdwin_type != 0)
5321 cmdwin_result = K_IGNORE;
5322 else
5323# endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00005324 if (first_tabpage->tp_next == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005325 emsg(_("E784: Cannot close last tab page"));
Bram Moolenaarf740b292006-02-16 22:11:02 +00005326 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005327 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005328 tab_number = get_tabpage_arg(eap);
5329 if (eap->errmsg == NULL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005330 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005331 tp = find_tabpage(tab_number);
Bram Moolenaarf740b292006-02-16 22:11:02 +00005332 if (tp == NULL)
5333 {
5334 beep_flush();
5335 return;
5336 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005337 if (tp != curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00005338 {
5339 tabpage_close_other(tp, eap->forceit);
5340 return;
5341 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005342 else if (!text_locked() && !curbuf_locked())
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005343 tabpage_close(eap->forceit);
5344 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005345 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005346}
5347
5348/*
5349 * ":tabonly": close all tab pages except the current one
5350 */
5351 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005352ex_tabonly(exarg_T *eap)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005353{
5354 tabpage_T *tp;
5355 int done;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005356 int tab_number;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005357
5358# ifdef FEAT_CMDWIN
5359 if (cmdwin_type != 0)
5360 cmdwin_result = K_IGNORE;
5361 else
5362# endif
5363 if (first_tabpage->tp_next == NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01005364 msg(_("Already only one tab page"));
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005365 else
5366 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005367 tab_number = get_tabpage_arg(eap);
5368 if (eap->errmsg == NULL)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005369 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005370 goto_tabpage(tab_number);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005371 // Repeat this up to a 1000 times, because autocommands may
5372 // mess up the lists.
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005373 for (done = 0; done < 1000; ++done)
5374 {
5375 FOR_ALL_TABPAGES(tp)
5376 if (tp->tp_topframe != topframe)
5377 {
5378 tabpage_close_other(tp, eap->forceit);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005379 // if we failed to close it quit
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005380 if (valid_tabpage(tp))
5381 done = 1000;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005382 // start over, "tp" is now invalid
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005383 break;
5384 }
5385 if (first_tabpage->tp_next == NULL)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005386 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005387 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005388 }
5389 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005390}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005391
5392/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00005393 * Close the current tab page.
5394 */
5395 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005396tabpage_close(int forceit)
Bram Moolenaarf740b292006-02-16 22:11:02 +00005397{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005398 // First close all the windows but the current one. If that worked then
5399 // close the last window in this tab, that will close it.
Bram Moolenaar459ca562016-11-10 18:16:33 +01005400 if (!ONE_WINDOW)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005401 close_others(TRUE, forceit);
Bram Moolenaar459ca562016-11-10 18:16:33 +01005402 if (ONE_WINDOW)
Bram Moolenaarf740b292006-02-16 22:11:02 +00005403 ex_win_close(forceit, curwin, NULL);
5404# ifdef FEAT_GUI
5405 need_mouse_correct = TRUE;
5406# endif
5407}
5408
5409/*
5410 * Close tab page "tp", which is not the current tab page.
5411 * Note that autocommands may make "tp" invalid.
Bram Moolenaar7875acc2006-09-10 13:51:17 +00005412 * Also takes care of the tab pages line disappearing when closing the
5413 * last-but-one tab page.
Bram Moolenaarf740b292006-02-16 22:11:02 +00005414 */
5415 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005416tabpage_close_other(tabpage_T *tp, int forceit)
Bram Moolenaarf740b292006-02-16 22:11:02 +00005417{
5418 int done = 0;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005419 win_T *wp;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00005420 int h = tabline_height();
Bram Moolenaarf740b292006-02-16 22:11:02 +00005421
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005422 // Limit to 1000 windows, autocommands may add a window while we close
5423 // one. OK, so I'm paranoid...
Bram Moolenaarf740b292006-02-16 22:11:02 +00005424 while (++done < 1000)
5425 {
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005426 wp = tp->tp_firstwin;
5427 ex_win_close(forceit, wp, tp);
Bram Moolenaarf740b292006-02-16 22:11:02 +00005428
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005429 // Autocommands may delete the tab page under our fingers and we may
5430 // fail to close a window with a modified buffer.
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005431 if (!valid_tabpage(tp) || tp->tp_firstwin == wp)
Bram Moolenaarf740b292006-02-16 22:11:02 +00005432 break;
5433 }
Bram Moolenaar7875acc2006-09-10 13:51:17 +00005434
Bram Moolenaar29323592016-07-24 22:04:11 +02005435 apply_autocmds(EVENT_TABCLOSED, NULL, NULL, FALSE, curbuf);
Bram Moolenaar29323592016-07-24 22:04:11 +02005436
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005437 redraw_tabline = TRUE;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00005438 if (h != tabline_height())
5439 shell_new_rows();
Bram Moolenaarf740b292006-02-16 22:11:02 +00005440}
5441
5442/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005443 * ":only".
5444 */
5445 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005446ex_only(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005447{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005448 win_T *wp;
5449 int wnr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005450# ifdef FEAT_GUI
5451 need_mouse_correct = TRUE;
5452# endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005453 if (eap->addr_count > 0)
5454 {
5455 wnr = eap->line2;
5456 for (wp = firstwin; --wnr > 0; )
5457 {
5458 if (wp->w_next == NULL)
5459 break;
5460 else
5461 wp = wp->w_next;
5462 }
5463 win_goto(wp);
5464 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005465 close_others(TRUE, eap->forceit);
5466}
5467
Bram Moolenaar071d4272004-06-13 20:20:40 +00005468 static void
Bram Moolenaar5e1e6d22017-01-02 17:26:00 +01005469ex_hide(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005470{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005471 // ":hide" or ":hide | cmd": hide current window
Bram Moolenaar2256c992016-11-15 21:17:07 +01005472 if (!eap->skip)
5473 {
Bram Moolenaar4033c552017-09-16 20:54:51 +02005474#ifdef FEAT_GUI
Bram Moolenaar2256c992016-11-15 21:17:07 +01005475 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005476#endif
Bram Moolenaar2256c992016-11-15 21:17:07 +01005477 if (eap->addr_count == 0)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005478 win_close(curwin, FALSE); // don't free buffer
Bram Moolenaar2256c992016-11-15 21:17:07 +01005479 else
5480 {
5481 int winnr = 0;
5482 win_T *win;
Bram Moolenaarf240e182014-11-27 18:33:02 +01005483
Bram Moolenaar2256c992016-11-15 21:17:07 +01005484 FOR_ALL_WINDOWS(win)
5485 {
5486 winnr++;
5487 if (winnr == eap->line2)
5488 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005489 }
Bram Moolenaar2256c992016-11-15 21:17:07 +01005490 if (win == NULL)
5491 win = lastwin;
5492 win_close(win, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005493 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005494 }
5495}
5496
5497/*
5498 * ":stop" and ":suspend": Suspend Vim.
5499 */
5500 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005501ex_stop(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005502{
5503 /*
5504 * Disallow suspending for "rvim".
5505 */
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005506 if (!check_restricted())
Bram Moolenaar071d4272004-06-13 20:20:40 +00005507 {
5508 if (!eap->forceit)
5509 autowrite_all();
5510 windgoto((int)Rows - 1, 0);
5511 out_char('\n');
5512 out_flush();
5513 stoptermcap();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005514 out_flush(); // needed for SUN to restore xterm buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00005515#ifdef FEAT_TITLE
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005516 mch_restore_title(SAVE_RESTORE_BOTH); // restore window titles
Bram Moolenaar071d4272004-06-13 20:20:40 +00005517#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005518 ui_suspend(); // call machine specific function
Bram Moolenaar071d4272004-06-13 20:20:40 +00005519#ifdef FEAT_TITLE
5520 maketitle();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005521 resettitle(); // force updating the title
Bram Moolenaar071d4272004-06-13 20:20:40 +00005522#endif
5523 starttermcap();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005524 scroll_start(); // scroll screen before redrawing
Bram Moolenaar071d4272004-06-13 20:20:40 +00005525 redraw_later_clear();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005526 shell_resized(); // may have resized window
Bram Moolenaar071d4272004-06-13 20:20:40 +00005527 }
5528}
5529
5530/*
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005531 * ":exit", ":xit" and ":wq": Write file and quite the current window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005532 */
5533 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005534ex_exit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005535{
5536#ifdef FEAT_CMDWIN
5537 if (cmdwin_type != 0)
5538 {
5539 cmdwin_result = Ctrl_C;
5540 return;
5541 }
5542#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005543 // Don't quit while editing the command line.
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005544 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005545 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005546 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005547 return;
5548 }
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005549
5550 if (before_quit_autocmds(curwin, FALSE, eap->forceit))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005551 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005552
5553 /*
5554 * if more files or windows we won't exit
5555 */
5556 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
5557 exiting = TRUE;
5558 if ( ((eap->cmdidx == CMD_wq
5559 || curbufIsChanged())
5560 && do_write(eap) == FAIL)
5561 || check_more(TRUE, eap->forceit) == FAIL
Bram Moolenaar027387f2016-01-02 22:25:52 +01005562 || (only_one_window() && check_changed_any(eap->forceit, FALSE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005563 {
5564 not_exiting();
5565 }
5566 else
5567 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005568 if (only_one_window()) // quit last window, exit Vim
Bram Moolenaar071d4272004-06-13 20:20:40 +00005569 getout(0);
Bram Moolenaar2c33d7b2017-10-14 16:06:20 +02005570 not_exiting();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005571# ifdef FEAT_GUI
5572 need_mouse_correct = TRUE;
5573# endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005574 // Quit current window, may free the buffer.
Bram Moolenaareb44a682017-08-03 22:44:55 +02005575 win_close(curwin, !buf_hide(curwin->w_buffer));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005576 }
5577}
5578
5579/*
5580 * ":print", ":list", ":number".
5581 */
5582 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005583ex_print(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005584{
Bram Moolenaardf177f62005-02-22 08:39:57 +00005585 if (curbuf->b_ml.ml_flags & ML_EMPTY)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005586 emsg(_(e_emptybuf));
Bram Moolenaardf177f62005-02-22 08:39:57 +00005587 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005588 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00005589 for ( ;!got_int; ui_breakcheck())
5590 {
5591 print_line(eap->line1,
5592 (eap->cmdidx == CMD_number || eap->cmdidx == CMD_pound
5593 || (eap->flags & EXFLAG_NR)),
5594 eap->cmdidx == CMD_list || (eap->flags & EXFLAG_LIST));
5595 if (++eap->line1 > eap->line2)
5596 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005597 out_flush(); // show one line at a time
Bram Moolenaardf177f62005-02-22 08:39:57 +00005598 }
5599 setpcmark();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005600 // put cursor at last line
Bram Moolenaardf177f62005-02-22 08:39:57 +00005601 curwin->w_cursor.lnum = eap->line2;
5602 beginline(BL_SOL | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005603 }
5604
Bram Moolenaar071d4272004-06-13 20:20:40 +00005605 ex_no_reprint = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005606}
5607
5608#ifdef FEAT_BYTEOFF
5609 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005610ex_goto(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005611{
5612 goto_byte(eap->line2);
5613}
5614#endif
5615
5616/*
5617 * ":shell".
5618 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005619 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005620ex_shell(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005621{
5622 do_shell(NULL, 0);
5623}
5624
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005625#if defined(HAVE_DROP_FILE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005626
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005627static int drop_busy = FALSE;
5628static int drop_filec;
5629static char_u **drop_filev = NULL;
5630static int drop_split;
5631static void (*drop_callback)(void *);
5632static void *drop_cookie;
5633
5634 static void
5635handle_drop_internal(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005636{
5637 exarg_T ea;
5638 int save_msg_scroll = msg_scroll;
5639
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005640 // Setting the argument list may cause screen updates and being called
5641 // recursively. Avoid that by setting drop_busy.
5642 drop_busy = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005643
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005644 // Check whether the current buffer is changed. If so, we will need
5645 // to split the current window or data could be lost.
5646 // We don't need to check if the 'hidden' option is set, as in this
5647 // case the buffer won't be lost.
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005648 if (!buf_hide(curbuf) && !drop_split)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005649 {
5650 ++emsg_off;
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005651 drop_split = check_changed(curbuf, CCGD_AW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005652 --emsg_off;
5653 }
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005654 if (drop_split)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005655 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005656 if (win_split(0, 0) == FAIL)
5657 return;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02005658 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005659
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005660 // When splitting the window, create a new alist. Otherwise the
5661 // existing one is overwritten.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005662 alist_unlink(curwin->w_alist);
5663 alist_new();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005664 }
5665
5666 /*
5667 * Set up the new argument list.
5668 */
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005669 alist_set(ALIST(curwin), drop_filec, drop_filev, FALSE, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005670
5671 /*
5672 * Move to the first file.
5673 */
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005674 // Fake up a minimal "next" command for do_argfile()
Bram Moolenaara80faa82020-04-12 19:37:17 +02005675 CLEAR_FIELD(ea);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005676 ea.cmd = (char_u *)"next";
5677 do_argfile(&ea, 0);
5678
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005679 // do_ecmd() may set need_start_insertmode, but since we never left Insert
5680 // mode that is not needed here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005681 need_start_insertmode = FALSE;
5682
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005683 // Restore msg_scroll, otherwise a following command may cause scrolling
5684 // unexpectedly. The screen will be redrawn by the caller, thus
5685 // msg_scroll being set by displaying a message is irrelevant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005686 msg_scroll = save_msg_scroll;
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005687
5688 if (drop_callback != NULL)
5689 drop_callback(drop_cookie);
5690
5691 drop_filev = NULL;
5692 drop_busy = FALSE;
5693}
5694
5695/*
5696 * Handle a file drop. The code is here because a drop is *nearly* like an
5697 * :args command, but not quite (we have a list of exact filenames, so we
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01005698 * don't want to (a) parse a command line, or (b) expand wildcards). So the
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005699 * code is very similar to :args and hence needs access to a lot of the static
5700 * functions in this file.
5701 *
5702 * The "filev" list must have been allocated using alloc(), as should each item
5703 * in the list. This function takes over responsibility for freeing the "filev"
5704 * list.
5705 */
5706 void
5707handle_drop(
5708 int filec, // the number of files dropped
5709 char_u **filev, // the list of files dropped
5710 int split, // force splitting the window
5711 void (*callback)(void *), // to be called after setting the argument
5712 // list
5713 void *cookie) // argument for "callback" (allocated)
5714{
5715 // Cannot handle recursive drops, finish the pending one.
5716 if (drop_busy)
5717 {
5718 FreeWild(filec, filev);
5719 vim_free(cookie);
5720 return;
5721 }
5722
5723 // When calling handle_drop() more than once in a row we only use the last
5724 // one.
5725 if (drop_filev != NULL)
5726 {
5727 FreeWild(drop_filec, drop_filev);
5728 vim_free(drop_cookie);
5729 }
5730
5731 drop_filec = filec;
5732 drop_filev = filev;
5733 drop_split = split;
5734 drop_callback = callback;
5735 drop_cookie = cookie;
5736
5737 // Postpone this when:
5738 // - editing the command line
5739 // - not possible to change the current buffer
5740 // - updating the screen
5741 // As it may change buffers and window structures that are in use and cause
5742 // freed memory to be used.
5743 if (text_locked() || curbuf_locked() || updating_screen)
5744 return;
5745
5746 handle_drop_internal();
5747}
5748
5749/*
5750 * To be called when text is unlocked, curbuf is unlocked or updating_screen is
5751 * reset: Handle a postponed drop.
5752 */
5753 void
5754handle_any_postponed_drop(void)
5755{
5756 if (!drop_busy && drop_filev != NULL
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +02005757 && !text_locked() && !curbuf_locked() && !updating_screen)
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005758 handle_drop_internal();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005759}
5760#endif
5761
Bram Moolenaar071d4272004-06-13 20:20:40 +00005762/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005763 * ":preserve".
5764 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005765 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005766ex_preserve(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005767{
Bram Moolenaar4399ef42005-02-12 14:29:27 +00005768 curbuf->b_flags |= BF_PRESERVED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005769 ml_preserve(curbuf, TRUE);
5770}
5771
5772/*
5773 * ":recover".
5774 */
5775 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005776ex_recover(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005777{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005778 // Set recoverymode right away to avoid the ATTENTION prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005779 recoverymode = TRUE;
Bram Moolenaar45d3b142013-11-09 03:31:51 +01005780 if (!check_changed(curbuf, (p_awa ? CCGD_AW : 0)
5781 | CCGD_MULTWIN
5782 | (eap->forceit ? CCGD_FORCEIT : 0)
5783 | CCGD_EXCMD)
5784
Bram Moolenaar071d4272004-06-13 20:20:40 +00005785 && (*eap->arg == NUL
5786 || setfname(curbuf, eap->arg, NULL, TRUE) == OK))
Bram Moolenaar99499b12019-05-23 21:35:48 +02005787 ml_recover(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005788 recoverymode = FALSE;
5789}
5790
5791/*
5792 * Command modifier used in a wrong way.
5793 */
5794 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005795ex_wrongmodifier(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005796{
5797 eap->errmsg = e_invcmd;
5798}
5799
Bram Moolenaar071d4272004-06-13 20:20:40 +00005800/*
5801 * :sview [+command] file split window with new file, read-only
5802 * :split [[+command] file] split window with current or new file
5803 * :vsplit [[+command] file] split window vertically with current or new file
5804 * :new [[+command] file] split window with no or new file
5805 * :vnew [[+command] file] split vertically window with no or new file
5806 * :sfind [+command] file split window with file in 'path'
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005807 *
5808 * :tabedit open new Tab page with empty window
5809 * :tabedit [+command] file open new Tab page and edit "file"
5810 * :tabnew [[+command] file] just like :tabedit
5811 * :tabfind [+command] file open new Tab page and find "file"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005812 */
5813 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005814ex_splitview(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005815{
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005816 win_T *old_curwin = curwin;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005817#if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005818 char_u *fname = NULL;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005819#endif
5820#ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00005821 int browse_flag = cmdmod.browse;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005822#endif
Bram Moolenaar39902a02018-06-21 22:10:08 +02005823 int use_tab = eap->cmdidx == CMD_tabedit
5824 || eap->cmdidx == CMD_tabfind
5825 || eap->cmdidx == CMD_tabnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005826
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01005827 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02005828 return;
5829
Bram Moolenaar4033c552017-09-16 20:54:51 +02005830#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00005831 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005832#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005833
Bram Moolenaar4033c552017-09-16 20:54:51 +02005834#ifdef FEAT_QUICKFIX
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005835 // A ":split" in the quickfix window works like ":new". Don't want two
5836 // quickfix windows. But it's OK when doing ":tab split".
Bram Moolenaar05bb9532008-07-04 09:44:11 +00005837 if (bt_quickfix(curbuf) && cmdmod.tab == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005838 {
5839 if (eap->cmdidx == CMD_split)
5840 eap->cmdidx = CMD_new;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005841 if (eap->cmdidx == CMD_vsplit)
5842 eap->cmdidx = CMD_vnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005843 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02005844#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005845
Bram Moolenaar4033c552017-09-16 20:54:51 +02005846#ifdef FEAT_SEARCHPATH
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005847 if (eap->cmdidx == CMD_sfind || eap->cmdidx == CMD_tabfind)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005848 {
5849 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg),
5850 FNAME_MESS, TRUE, curbuf->b_ffname);
5851 if (fname == NULL)
5852 goto theend;
5853 eap->arg = fname;
5854 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005855# ifdef FEAT_BROWSE
Bram Moolenaar4033c552017-09-16 20:54:51 +02005856 else
5857# endif
5858#endif
5859#ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00005860 if (cmdmod.browse
Bram Moolenaar071d4272004-06-13 20:20:40 +00005861 && eap->cmdidx != CMD_vnew
Bram Moolenaar071d4272004-06-13 20:20:40 +00005862 && eap->cmdidx != CMD_new)
5863 {
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00005864 if (
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005865# ifdef FEAT_GUI
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00005866 !gui.in_use &&
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005867# endif
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00005868 au_has_group((char_u *)"FileExplorer"))
5869 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005870 // No browsing supported but we do have the file explorer:
5871 // Edit the directory.
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00005872 if (*eap->arg == NUL || !mch_isdir(eap->arg))
5873 eap->arg = (char_u *)".";
5874 }
5875 else
5876 {
Bram Moolenaar39902a02018-06-21 22:10:08 +02005877 fname = do_browse(0, (char_u *)(use_tab
5878 ? _("Edit File in new tab page")
5879 : _("Edit File in new window")),
Bram Moolenaar071d4272004-06-13 20:20:40 +00005880 eap->arg, NULL, NULL, NULL, curbuf);
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00005881 if (fname == NULL)
5882 goto theend;
5883 eap->arg = fname;
5884 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005885 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005886 cmdmod.browse = FALSE; // Don't browse again in do_ecmd().
Bram Moolenaar4033c552017-09-16 20:54:51 +02005887#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005888
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005889 /*
5890 * Either open new tab page or split the window.
5891 */
Bram Moolenaar39902a02018-06-21 22:10:08 +02005892 if (use_tab)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005893 {
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005894 if (win_new_tabpage(cmdmod.tab != 0 ? cmdmod.tab
5895 : eap->addr_count == 0 ? 0
5896 : (int)eap->line2 + 1) != FAIL)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005897 {
Bram Moolenaard2b66012008-01-09 19:30:36 +00005898 do_exedit(eap, old_curwin);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005899
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005900 // set the alternate buffer for the window we came from
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005901 if (curwin != old_curwin
5902 && win_valid(old_curwin)
5903 && old_curwin->w_buffer != curbuf
5904 && !cmdmod.keepalt)
5905 old_curwin->w_alt_fnum = curbuf->b_fnum;
5906 }
5907 }
5908 else if (win_split(eap->addr_count > 0 ? (int)eap->line2 : 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005909 *eap->cmd == 'v' ? WSP_VERT : 0) != FAIL)
5910 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005911 // Reset 'scrollbind' when editing another file, but keep it when
5912 // doing ":split" without arguments.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005913 if (*eap->arg != NUL
Bram Moolenaar8a3bb562018-03-04 20:14:14 +01005914# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00005915 || cmdmod.browse
Bram Moolenaar8a3bb562018-03-04 20:14:14 +01005916# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005917 )
Bram Moolenaar3368ea22010-09-21 16:56:35 +02005918 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005919 else
5920 do_check_scrollbind(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005921 do_exedit(eap, old_curwin);
5922 }
5923
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005924# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00005925 cmdmod.browse = browse_flag;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005926# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005927
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005928# if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005929theend:
5930 vim_free(fname);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005931# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005932}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005933
5934/*
Bram Moolenaar80a94a52006-02-23 21:26:58 +00005935 * Open a new tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005936 */
5937 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005938tabpage_new(void)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00005939{
5940 exarg_T ea;
5941
Bram Moolenaara80faa82020-04-12 19:37:17 +02005942 CLEAR_FIELD(ea);
Bram Moolenaar80a94a52006-02-23 21:26:58 +00005943 ea.cmdidx = CMD_tabnew;
5944 ea.cmd = (char_u *)"tabn";
5945 ea.arg = (char_u *)"";
5946 ex_splitview(&ea);
5947}
5948
5949/*
5950 * :tabnext command
5951 */
5952 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005953ex_tabnext(exarg_T *eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005954{
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005955 int tab_number;
5956
Bram Moolenaar8cdbd5b2019-06-16 15:50:45 +02005957 if (ERROR_IF_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02005958 return;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005959 switch (eap->cmdidx)
5960 {
5961 case CMD_tabfirst:
5962 case CMD_tabrewind:
5963 goto_tabpage(1);
5964 break;
5965 case CMD_tablast:
5966 goto_tabpage(9999);
5967 break;
5968 case CMD_tabprevious:
5969 case CMD_tabNext:
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005970 if (eap->arg && *eap->arg != NUL)
5971 {
5972 char_u *p = eap->arg;
5973 char_u *p_save = p;
5974
5975 tab_number = getdigits(&p);
5976 if (p == p_save || *p_save == '-' || *p != NUL
5977 || tab_number == 0)
5978 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005979 // No numbers as argument.
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005980 eap->errmsg = e_invarg;
5981 return;
5982 }
5983 }
5984 else
5985 {
5986 if (eap->addr_count == 0)
5987 tab_number = 1;
5988 else
5989 {
5990 tab_number = eap->line2;
5991 if (tab_number < 1)
5992 {
5993 eap->errmsg = e_invrange;
5994 return;
5995 }
5996 }
5997 }
5998 goto_tabpage(-tab_number);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005999 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006000 default: // CMD_tabnext
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006001 tab_number = get_tabpage_arg(eap);
6002 if (eap->errmsg == NULL)
6003 goto_tabpage(tab_number);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006004 break;
6005 }
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006006}
6007
6008/*
6009 * :tabmove command
6010 */
6011 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006012ex_tabmove(exarg_T *eap)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006013{
Bram Moolenaar40ce3a42015-04-21 18:08:39 +02006014 int tab_number;
Bram Moolenaar8cb8dca2012-07-06 18:27:39 +02006015
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006016 tab_number = get_tabpage_arg(eap);
6017 if (eap->errmsg == NULL)
6018 tabpage_move(tab_number);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006019}
6020
6021/*
6022 * :tabs command: List tabs and their contents.
6023 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006024 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006025ex_tabs(exarg_T *eap UNUSED)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006026{
6027 tabpage_T *tp;
6028 win_T *wp;
6029 int tabcount = 1;
6030
6031 msg_start();
6032 msg_scroll = TRUE;
6033 for (tp = first_tabpage; tp != NULL && !got_int; tp = tp->tp_next)
6034 {
6035 msg_putchar('\n');
6036 vim_snprintf((char *)IObuff, IOSIZE, _("Tab page %d"), tabcount++);
Bram Moolenaar8820b482017-03-16 17:23:31 +01006037 msg_outtrans_attr(IObuff, HL_ATTR(HLF_T));
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006038 out_flush(); // output one line at a time
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006039 ui_breakcheck();
6040
Bram Moolenaar030f0df2006-02-21 22:02:53 +00006041 if (tp == curtab)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006042 wp = firstwin;
6043 else
6044 wp = tp->tp_firstwin;
6045 for ( ; wp != NULL && !got_int; wp = wp->w_next)
6046 {
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006047 msg_putchar('\n');
6048 msg_putchar(wp == curwin ? '>' : ' ');
6049 msg_putchar(' ');
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006050 msg_putchar(bufIsChanged(wp->w_buffer) ? '+' : ' ');
6051 msg_putchar(' ');
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006052 if (buf_spname(wp->w_buffer) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02006053 vim_strncpy(IObuff, buf_spname(wp->w_buffer), IOSIZE - 1);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006054 else
6055 home_replace(wp->w_buffer, wp->w_buffer->b_fname,
6056 IObuff, IOSIZE, TRUE);
6057 msg_outtrans(IObuff);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006058 out_flush(); // output one line at a time
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006059 ui_breakcheck();
6060 }
6061 }
6062}
6063
Bram Moolenaar071d4272004-06-13 20:20:40 +00006064/*
6065 * ":mode": Set screen mode.
6066 * If no argument given, just get the screen size and redraw.
6067 */
6068 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006069ex_mode(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006070{
6071 if (*eap->arg == NUL)
6072 shell_resized();
6073 else
Bram Moolenaar3c71aec2020-01-17 19:32:20 +01006074 emsg(_(e_screenmode));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006075}
6076
Bram Moolenaar071d4272004-06-13 20:20:40 +00006077/*
6078 * ":resize".
6079 * set, increment or decrement current window height
6080 */
6081 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006082ex_resize(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006083{
6084 int n;
6085 win_T *wp = curwin;
6086
6087 if (eap->addr_count > 0)
6088 {
6089 n = eap->line2;
6090 for (wp = firstwin; wp->w_next != NULL && --n > 0; wp = wp->w_next)
6091 ;
6092 }
6093
Bram Moolenaar44a2f922016-03-19 22:11:51 +01006094# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00006095 need_mouse_correct = TRUE;
Bram Moolenaar44a2f922016-03-19 22:11:51 +01006096# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006097 n = atol((char *)eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006098 if (cmdmod.split & WSP_VERT)
6099 {
6100 if (*eap->arg == '-' || *eap->arg == '+')
Bram Moolenaar02631462017-09-22 15:20:32 +02006101 n += curwin->w_width;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006102 else if (n == 0 && eap->arg[0] == NUL) // default is very wide
Bram Moolenaar071d4272004-06-13 20:20:40 +00006103 n = 9999;
6104 win_setwidth_win((int)n, wp);
6105 }
6106 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006107 {
6108 if (*eap->arg == '-' || *eap->arg == '+')
6109 n += curwin->w_height;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006110 else if (n == 0 && eap->arg[0] == NUL) // default is very high
Bram Moolenaar071d4272004-06-13 20:20:40 +00006111 n = 9999;
6112 win_setheight_win((int)n, wp);
6113 }
6114}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006115
6116/*
6117 * ":find [+command] <file>" command.
6118 */
6119 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006120ex_find(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006121{
6122#ifdef FEAT_SEARCHPATH
6123 char_u *fname;
6124 int count;
6125
6126 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg), FNAME_MESS,
6127 TRUE, curbuf->b_ffname);
6128 if (eap->addr_count > 0)
6129 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006130 // Repeat finding the file "count" times. This matters when it
6131 // appears several times in the path.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006132 count = eap->line2;
6133 while (fname != NULL && --count > 0)
6134 {
6135 vim_free(fname);
6136 fname = find_file_in_path(NULL, 0, FNAME_MESS,
6137 FALSE, curbuf->b_ffname);
6138 }
6139 }
6140
6141 if (fname != NULL)
6142 {
6143 eap->arg = fname;
6144#endif
6145 do_exedit(eap, NULL);
6146#ifdef FEAT_SEARCHPATH
6147 vim_free(fname);
6148 }
6149#endif
6150}
6151
6152/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00006153 * ":open" simulation: for now just work like ":visual".
6154 */
6155 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006156ex_open(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00006157{
6158 regmatch_T regmatch;
6159 char_u *p;
6160
6161 curwin->w_cursor.lnum = eap->line2;
6162 beginline(BL_SOL | BL_FIX);
6163 if (*eap->arg == '/')
6164 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006165 // ":open /pattern/": put cursor in column found with pattern
Bram Moolenaardf177f62005-02-22 08:39:57 +00006166 ++eap->arg;
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006167 p = skip_regexp(eap->arg, '/', p_magic);
Bram Moolenaardf177f62005-02-22 08:39:57 +00006168 *p = NUL;
6169 regmatch.regprog = vim_regcomp(eap->arg, p_magic ? RE_MAGIC : 0);
6170 if (regmatch.regprog != NULL)
6171 {
6172 regmatch.rm_ic = p_ic;
6173 p = ml_get_curline();
6174 if (vim_regexec(&regmatch, p, (colnr_T)0))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006175 curwin->w_cursor.col = (colnr_T)(regmatch.startp[0] - p);
Bram Moolenaardf177f62005-02-22 08:39:57 +00006176 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006177 emsg(_(e_nomatch));
Bram Moolenaar473de612013-06-08 18:19:48 +02006178 vim_regfree(regmatch.regprog);
Bram Moolenaardf177f62005-02-22 08:39:57 +00006179 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006180 // Move to the NUL, ignore any other arguments.
Bram Moolenaardf177f62005-02-22 08:39:57 +00006181 eap->arg += STRLEN(eap->arg);
6182 }
6183 check_cursor();
6184
6185 eap->cmdidx = CMD_visual;
6186 do_exedit(eap, NULL);
6187}
6188
6189/*
6190 * ":edit", ":badd", ":visual".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006191 */
6192 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006193ex_edit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006194{
6195 do_exedit(eap, NULL);
6196}
6197
6198/*
Bram Moolenaar4b96df52020-01-26 22:00:26 +01006199 * ":edit <file>" command and alike.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006200 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006201 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006202do_exedit(
6203 exarg_T *eap,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006204 win_T *old_curwin) // curwin before doing a split or NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006205{
6206 int n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006207 int need_hide;
Bram Moolenaardf177f62005-02-22 08:39:57 +00006208 int exmode_was = exmode_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006209
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01006210 if ((eap->cmdidx != CMD_pedit && ERROR_IF_POPUP_WINDOW)
6211 || ERROR_IF_TERM_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02006212 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006213 /*
6214 * ":vi" command ends Ex mode.
6215 */
6216 if (exmode_active && (eap->cmdidx == CMD_visual
6217 || eap->cmdidx == CMD_view))
6218 {
6219 exmode_active = FALSE;
6220 if (*eap->arg == NUL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00006221 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006222 // Special case: ":global/pat/visual\NLvi-commands"
Bram Moolenaardf177f62005-02-22 08:39:57 +00006223 if (global_busy)
6224 {
6225 int rd = RedrawingDisabled;
6226 int nwr = no_wait_return;
6227 int ms = msg_scroll;
6228#ifdef FEAT_GUI
6229 int he = hold_gui_events;
6230#endif
6231
6232 if (eap->nextcmd != NULL)
6233 {
6234 stuffReadbuff(eap->nextcmd);
6235 eap->nextcmd = NULL;
6236 }
6237
6238 if (exmode_was != EXMODE_VIM)
6239 settmode(TMODE_RAW);
6240 RedrawingDisabled = 0;
6241 no_wait_return = 0;
6242 need_wait_return = FALSE;
6243 msg_scroll = 0;
6244#ifdef FEAT_GUI
6245 hold_gui_events = 0;
6246#endif
6247 must_redraw = CLEAR;
Bram Moolenaar9e2bcb52020-02-18 21:33:00 +01006248 pending_exmode_active = TRUE;
Bram Moolenaardf177f62005-02-22 08:39:57 +00006249
6250 main_loop(FALSE, TRUE);
6251
Bram Moolenaar9e2bcb52020-02-18 21:33:00 +01006252 pending_exmode_active = FALSE;
Bram Moolenaardf177f62005-02-22 08:39:57 +00006253 RedrawingDisabled = rd;
6254 no_wait_return = nwr;
6255 msg_scroll = ms;
6256#ifdef FEAT_GUI
6257 hold_gui_events = he;
6258#endif
6259 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006260 return;
Bram Moolenaardf177f62005-02-22 08:39:57 +00006261 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006262 }
6263
6264 if ((eap->cmdidx == CMD_new
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006265 || eap->cmdidx == CMD_tabnew
6266 || eap->cmdidx == CMD_tabedit
Bram Moolenaar4033c552017-09-16 20:54:51 +02006267 || eap->cmdidx == CMD_vnew) && *eap->arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006268 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006269 // ":new" or ":tabnew" without argument: edit an new empty buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00006270 setpcmark();
6271 (void)do_ecmd(0, NULL, NULL, eap, ECMD_ONE,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00006272 ECMD_HIDE + (eap->forceit ? ECMD_FORCEIT : 0),
6273 old_curwin == NULL ? curwin : NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006274 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02006275 else if ((eap->cmdidx != CMD_split && eap->cmdidx != CMD_vsplit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006276 || *eap->arg != NUL
6277#ifdef FEAT_BROWSE
6278 || cmdmod.browse
6279#endif
6280 )
6281 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006282 // Can't edit another file when "curbuf_lock" is set. Only ":edit"
6283 // can bring us here, others are stopped earlier.
Bram Moolenaar5555acc2006-04-07 21:33:12 +00006284 if (*eap->arg != NUL && curbuf_locked())
6285 return;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006286
Bram Moolenaar071d4272004-06-13 20:20:40 +00006287 n = readonlymode;
6288 if (eap->cmdidx == CMD_view || eap->cmdidx == CMD_sview)
6289 readonlymode = TRUE;
6290 else if (eap->cmdidx == CMD_enew)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006291 readonlymode = FALSE; // 'readonly' doesn't make sense in an
6292 // empty buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00006293 setpcmark();
6294 if (do_ecmd(0, (eap->cmdidx == CMD_enew ? NULL : eap->arg),
6295 NULL, eap,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006296 // ":edit" goes to first line if Vi compatible
Bram Moolenaar071d4272004-06-13 20:20:40 +00006297 (*eap->arg == NUL && eap->do_ecmd_lnum == 0
6298 && vim_strchr(p_cpo, CPO_GOTO1) != NULL)
6299 ? ECMD_ONE : eap->do_ecmd_lnum,
Bram Moolenaareb44a682017-08-03 22:44:55 +02006300 (buf_hide(curbuf) ? ECMD_HIDE : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006301 + (eap->forceit ? ECMD_FORCEIT : 0)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006302 // after a split we can use an existing buffer
Bram Moolenaar7b449342014-03-25 13:03:48 +01006303 + (old_curwin != NULL ? ECMD_OLDBUF : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006304 + (eap->cmdidx == CMD_badd ? ECMD_ADDBUF : 0 )
Bram Moolenaar701f7af2008-11-15 13:12:07 +00006305 , old_curwin == NULL ? curwin : NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006306 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006307 // Editing the file failed. If the window was split, close it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006308 if (old_curwin != NULL)
6309 {
6310 need_hide = (curbufIsChanged() && curbuf->b_nwindows <= 1);
Bram Moolenaareb44a682017-08-03 22:44:55 +02006311 if (!need_hide || buf_hide(curbuf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006312 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006313#if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00006314 cleanup_T cs;
6315
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006316 // Reset the error/interrupt/exception state here so that
6317 // aborting() returns FALSE when closing a window.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00006318 enter_cleanup(&cs);
Bram Moolenaar4033c552017-09-16 20:54:51 +02006319#endif
6320#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00006321 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006322#endif
Bram Moolenaareb44a682017-08-03 22:44:55 +02006323 win_close(curwin, !need_hide && !buf_hide(curbuf));
Bram Moolenaarc0197e22004-09-13 20:26:32 +00006324
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006325#if defined(FEAT_EVAL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006326 // Restore the error/interrupt/exception state if not
6327 // discarded by a new aborting error, interrupt, or
6328 // uncaught exception.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00006329 leave_cleanup(&cs);
Bram Moolenaar4033c552017-09-16 20:54:51 +02006330#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006331 }
6332 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006333 }
6334 else if (readonlymode && curbuf->b_nwindows == 1)
6335 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006336 // When editing an already visited buffer, 'readonly' won't be set
6337 // but the previous value is kept. With ":view" and ":sview" we
6338 // want the file to be readonly, except when another window is
6339 // editing the same buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006340 curbuf->b_p_ro = TRUE;
6341 }
6342 readonlymode = n;
6343 }
6344 else
6345 {
6346 if (eap->do_ecmd_cmd != NULL)
6347 do_cmdline_cmd(eap->do_ecmd_cmd);
6348#ifdef FEAT_TITLE
6349 n = curwin->w_arg_idx_invalid;
6350#endif
6351 check_arg_idx(curwin);
6352#ifdef FEAT_TITLE
6353 if (n != curwin->w_arg_idx_invalid)
6354 maketitle();
6355#endif
6356 }
6357
Bram Moolenaar071d4272004-06-13 20:20:40 +00006358 /*
6359 * if ":split file" worked, set alternate file name in old window to new
6360 * file
6361 */
6362 if (old_curwin != NULL
6363 && *eap->arg != NUL
6364 && curwin != old_curwin
6365 && win_valid(old_curwin)
Bram Moolenaard4755bb2004-09-02 19:12:26 +00006366 && old_curwin->w_buffer != curbuf
6367 && !cmdmod.keepalt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006368 old_curwin->w_alt_fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006369
6370 ex_no_reprint = TRUE;
6371}
6372
6373#ifndef FEAT_GUI
6374/*
6375 * ":gui" and ":gvim" when there is no GUI.
6376 */
6377 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006378ex_nogui(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006379{
6380 eap->errmsg = e_nogvim;
6381}
6382#endif
6383
Bram Moolenaar4f974752019-02-17 17:44:42 +01006384#if defined(FEAT_GUI_MSWIN) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006385 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006386ex_tearoff(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006387{
6388 gui_make_tearoff(eap->arg);
6389}
6390#endif
6391
Bram Moolenaar29a2c082018-03-05 21:06:23 +01006392#if (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK) \
6393 || defined(FEAT_TERM_POPUP_MENU)) && defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006394 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006395ex_popup(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006396{
Bram Moolenaar29a2c082018-03-05 21:06:23 +01006397# if defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK)
6398 if (gui.in_use)
6399 gui_make_popup(eap->arg, eap->forceit);
6400# ifdef FEAT_TERM_POPUP_MENU
6401 else
6402# endif
6403# endif
6404# ifdef FEAT_TERM_POPUP_MENU
6405 pum_make_popup(eap->arg, eap->forceit);
6406# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006407}
6408#endif
6409
Bram Moolenaar071d4272004-06-13 20:20:40 +00006410 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006411ex_swapname(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006412{
6413 if (curbuf->b_ml.ml_mfp == NULL || curbuf->b_ml.ml_mfp->mf_fname == NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01006414 msg(_("No swap file"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006415 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01006416 msg((char *)curbuf->b_ml.ml_mfp->mf_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006417}
6418
6419/*
6420 * ":syncbind" forces all 'scrollbind' windows to have the same relative
6421 * offset.
6422 * (1998-11-02 16:21:01 R. Edward Ralston <eralston@computer.org>)
6423 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006424 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006425ex_syncbind(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006426{
Bram Moolenaar071d4272004-06-13 20:20:40 +00006427 win_T *wp;
Bram Moolenaardedd1b02013-12-14 13:06:17 +01006428 win_T *save_curwin = curwin;
6429 buf_T *save_curbuf = curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006430 long topline;
6431 long y;
6432 linenr_T old_linenr = curwin->w_cursor.lnum;
6433
6434 setpcmark();
6435
6436 /*
6437 * determine max topline
6438 */
6439 if (curwin->w_p_scb)
6440 {
6441 topline = curwin->w_topline;
Bram Moolenaar29323592016-07-24 22:04:11 +02006442 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006443 {
6444 if (wp->w_p_scb && wp->w_buffer)
6445 {
Bram Moolenaar375e3392019-01-31 18:26:10 +01006446 y = wp->w_buffer->b_ml.ml_line_count - get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006447 if (topline > y)
6448 topline = y;
6449 }
6450 }
6451 if (topline < 1)
6452 topline = 1;
6453 }
6454 else
6455 {
6456 topline = 1;
6457 }
6458
6459
6460 /*
Bram Moolenaardedd1b02013-12-14 13:06:17 +01006461 * Set all scrollbind windows to the same topline.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006462 */
Bram Moolenaar29323592016-07-24 22:04:11 +02006463 FOR_ALL_WINDOWS(curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006464 {
6465 if (curwin->w_p_scb)
6466 {
Bram Moolenaardedd1b02013-12-14 13:06:17 +01006467 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006468 y = topline - curwin->w_topline;
6469 if (y > 0)
6470 scrollup(y, TRUE);
6471 else
6472 scrolldown(-y, TRUE);
6473 curwin->w_scbind_pos = topline;
6474 redraw_later(VALID);
6475 cursor_correct();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006476 curwin->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006477 }
6478 }
Bram Moolenaardedd1b02013-12-14 13:06:17 +01006479 curwin = save_curwin;
6480 curbuf = save_curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006481 if (curwin->w_p_scb)
6482 {
6483 did_syncbind = TRUE;
6484 checkpcmark();
6485 if (old_linenr != curwin->w_cursor.lnum)
6486 {
6487 char_u ctrl_o[2];
6488
6489 ctrl_o[0] = Ctrl_O;
6490 ctrl_o[1] = 0;
6491 ins_typebuf(ctrl_o, REMAP_NONE, 0, TRUE, FALSE);
6492 }
6493 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006494}
6495
6496
6497 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006498ex_read(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006499{
Bram Moolenaardf177f62005-02-22 08:39:57 +00006500 int i;
6501 int empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
6502 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006503
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006504 if (eap->usefilter) // :r!cmd
Bram Moolenaar071d4272004-06-13 20:20:40 +00006505 do_bang(1, eap, FALSE, FALSE, TRUE);
6506 else
6507 {
6508 if (u_save(eap->line2, (linenr_T)(eap->line2 + 1)) == FAIL)
6509 return;
6510
6511#ifdef FEAT_BROWSE
6512 if (cmdmod.browse)
6513 {
6514 char_u *browseFile;
6515
Bram Moolenaar7171abe2004-10-11 10:06:20 +00006516 browseFile = do_browse(0, (char_u *)_("Append File"), eap->arg,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006517 NULL, NULL, NULL, curbuf);
6518 if (browseFile != NULL)
6519 {
6520 i = readfile(browseFile, NULL,
6521 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
6522 vim_free(browseFile);
6523 }
6524 else
6525 i = OK;
6526 }
6527 else
6528#endif
6529 if (*eap->arg == NUL)
6530 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006531 if (check_fname() == FAIL) // check for no file name
Bram Moolenaar071d4272004-06-13 20:20:40 +00006532 return;
6533 i = readfile(curbuf->b_ffname, curbuf->b_fname,
6534 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
6535 }
6536 else
6537 {
6538 if (vim_strchr(p_cpo, CPO_ALTREAD) != NULL)
6539 (void)setaltfname(eap->arg, eap->arg, (linenr_T)1);
6540 i = readfile(eap->arg, NULL,
6541 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
6542
6543 }
Bram Moolenaare13b9af2017-01-13 22:01:02 +01006544 if (i != OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006545 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006546#if defined(FEAT_EVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006547 if (!aborting())
6548#endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006549 semsg(_(e_notopen), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006550 }
6551 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00006552 {
6553 if (empty && exmode_active)
6554 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006555 // Delete the empty line that remains. Historically ex does
6556 // this but vi doesn't.
Bram Moolenaardf177f62005-02-22 08:39:57 +00006557 if (eap->line2 == 0)
6558 lnum = curbuf->b_ml.ml_line_count;
6559 else
6560 lnum = 1;
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00006561 if (*ml_get(lnum) == NUL && u_savedel(lnum, 1L) == OK)
Bram Moolenaardf177f62005-02-22 08:39:57 +00006562 {
6563 ml_delete(lnum, FALSE);
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00006564 if (curwin->w_cursor.lnum > 1
6565 && curwin->w_cursor.lnum >= lnum)
Bram Moolenaardf177f62005-02-22 08:39:57 +00006566 --curwin->w_cursor.lnum;
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00006567 deleted_lines_mark(lnum, 1L);
Bram Moolenaardf177f62005-02-22 08:39:57 +00006568 }
6569 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006570 redraw_curbuf_later(VALID);
Bram Moolenaardf177f62005-02-22 08:39:57 +00006571 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006572 }
6573}
6574
Bram Moolenaarea408852005-06-25 22:49:46 +00006575static char_u *prev_dir = NULL;
6576
6577#if defined(EXITFREE) || defined(PROTO)
6578 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006579free_cd_dir(void)
Bram Moolenaarea408852005-06-25 22:49:46 +00006580{
Bram Moolenaard23a8232018-02-10 18:45:26 +01006581 VIM_CLEAR(prev_dir);
6582 VIM_CLEAR(globaldir);
Bram Moolenaarea408852005-06-25 22:49:46 +00006583}
6584#endif
6585
Bram Moolenaarf4258302013-06-02 18:20:17 +02006586/*
6587 * Deal with the side effects of changing the current directory.
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006588 * When 'scope' is CDSCOPE_TABPAGE then this was after an ":tcd" command.
6589 * When 'scope' is CDSCOPE_WINDOW then this was after an ":lcd" command.
Bram Moolenaarf4258302013-06-02 18:20:17 +02006590 */
6591 void
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006592post_chdir(cdscope_T scope)
Bram Moolenaarf4258302013-06-02 18:20:17 +02006593{
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006594 if (scope != CDSCOPE_WINDOW)
Bram Moolenaar00aa0692019-04-27 20:37:57 +02006595 // Clear tab local directory for both :cd and :tcd
6596 VIM_CLEAR(curtab->tp_localdir);
Bram Moolenaard23a8232018-02-10 18:45:26 +01006597 VIM_CLEAR(curwin->w_localdir);
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006598 if (scope != CDSCOPE_GLOBAL)
Bram Moolenaarf4258302013-06-02 18:20:17 +02006599 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006600 // If still in global directory, need to remember current
6601 // directory as global directory.
Bram Moolenaarf4258302013-06-02 18:20:17 +02006602 if (globaldir == NULL && prev_dir != NULL)
6603 globaldir = vim_strsave(prev_dir);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006604 // Remember this local directory for the window.
Bram Moolenaarf4258302013-06-02 18:20:17 +02006605 if (mch_dirname(NameBuff, MAXPATHL) == OK)
Bram Moolenaar00aa0692019-04-27 20:37:57 +02006606 {
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006607 if (scope == CDSCOPE_TABPAGE)
Bram Moolenaar00aa0692019-04-27 20:37:57 +02006608 curtab->tp_localdir = vim_strsave(NameBuff);
6609 else
6610 curwin->w_localdir = vim_strsave(NameBuff);
6611 }
Bram Moolenaarf4258302013-06-02 18:20:17 +02006612 }
6613 else
6614 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006615 // We are now in the global directory, no need to remember its
6616 // name.
Bram Moolenaard23a8232018-02-10 18:45:26 +01006617 VIM_CLEAR(globaldir);
Bram Moolenaarf4258302013-06-02 18:20:17 +02006618 }
6619
6620 shorten_fnames(TRUE);
6621}
6622
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006623/*
6624 * Change directory function used by :cd/:tcd/:lcd Ex commands and the
6625 * chdir() function. If 'winlocaldir' is TRUE, then changes the window-local
6626 * directory. If 'tablocaldir' is TRUE, then changes the tab-local directory.
6627 * Otherwise changes the global directory.
6628 * Returns TRUE if the directory is successfully changed.
6629 */
6630 int
6631changedir_func(
6632 char_u *new_dir,
6633 int forceit,
6634 cdscope_T scope)
6635{
6636 char_u *tofree;
6637 int dir_differs;
6638 int retval = FALSE;
6639
Bram Moolenaar7cc96922020-01-31 22:41:38 +01006640 if (new_dir == NULL || allbuf_locked())
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006641 return FALSE;
6642
6643 if (vim_strchr(p_cpo, CPO_CHDIR) != NULL && curbufIsChanged() && !forceit)
6644 {
6645 emsg(_("E747: Cannot change directory, buffer is modified (add ! to override)"));
6646 return FALSE;
6647 }
6648
6649 // ":cd -": Change to previous directory
6650 if (STRCMP(new_dir, "-") == 0)
6651 {
6652 if (prev_dir == NULL)
6653 {
6654 emsg(_("E186: No previous directory"));
6655 return FALSE;
6656 }
6657 new_dir = prev_dir;
6658 }
6659
6660 // Save current directory for next ":cd -"
6661 tofree = prev_dir;
6662 if (mch_dirname(NameBuff, MAXPATHL) == OK)
6663 prev_dir = vim_strsave(NameBuff);
6664 else
6665 prev_dir = NULL;
6666
6667#if defined(UNIX) || defined(VMS)
6668 // for UNIX ":cd" means: go to home directory
6669 if (*new_dir == NUL)
6670 {
6671 // use NameBuff for home directory name
6672# ifdef VMS
6673 char_u *p;
6674
6675 p = mch_getenv((char_u *)"SYS$LOGIN");
6676 if (p == NULL || *p == NUL) // empty is the same as not set
6677 NameBuff[0] = NUL;
6678 else
6679 vim_strncpy(NameBuff, p, MAXPATHL - 1);
6680# else
6681 expand_env((char_u *)"$HOME", NameBuff, MAXPATHL);
6682# endif
6683 new_dir = NameBuff;
6684 }
6685#endif
6686 dir_differs = new_dir == NULL || prev_dir == NULL
6687 || pathcmp((char *)prev_dir, (char *)new_dir, -1) != 0;
6688 if (new_dir == NULL || (dir_differs && vim_chdir(new_dir)))
6689 emsg(_(e_failed));
6690 else
6691 {
6692 char_u *acmd_fname;
6693
6694 post_chdir(scope);
6695
6696 if (dir_differs)
6697 {
6698 if (scope == CDSCOPE_WINDOW)
6699 acmd_fname = (char_u *)"window";
6700 else if (scope == CDSCOPE_TABPAGE)
6701 acmd_fname = (char_u *)"tabpage";
6702 else
6703 acmd_fname = (char_u *)"global";
6704 apply_autocmds(EVENT_DIRCHANGED, acmd_fname, new_dir, FALSE,
6705 curbuf);
6706 }
6707 retval = TRUE;
6708 }
6709 vim_free(tofree);
6710
6711 return retval;
6712}
Bram Moolenaarea408852005-06-25 22:49:46 +00006713
Bram Moolenaar071d4272004-06-13 20:20:40 +00006714/*
Bram Moolenaar00aa0692019-04-27 20:37:57 +02006715 * ":cd", ":tcd", ":lcd", ":chdir" ":tchdir" and ":lchdir".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006716 */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00006717 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006718ex_cd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006719{
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01006720 char_u *new_dir;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006721
6722 new_dir = eap->arg;
6723#if !defined(UNIX) && !defined(VMS)
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006724 // for non-UNIX ":cd" means: print current directory
Bram Moolenaar071d4272004-06-13 20:20:40 +00006725 if (*new_dir == NUL)
6726 ex_pwd(NULL);
6727 else
6728#endif
6729 {
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006730 cdscope_T scope = CDSCOPE_GLOBAL;
6731
6732 if (eap->cmdidx == CMD_lcd || eap->cmdidx == CMD_lchdir)
6733 scope = CDSCOPE_WINDOW;
6734 else if (eap->cmdidx == CMD_tcd || eap->cmdidx == CMD_tchdir)
6735 scope = CDSCOPE_TABPAGE;
6736
6737 if (changedir_func(new_dir, eap->forceit, scope))
Bram Moolenaardf177f62005-02-22 08:39:57 +00006738 {
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006739 // Echo the new current directory if the command was typed.
Bram Moolenaarfcfbc672009-07-09 18:13:49 +00006740 if (KeyTyped || p_verbose >= 5)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006741 ex_pwd(eap);
6742 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006743 }
6744}
6745
6746/*
6747 * ":pwd".
6748 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006749 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006750ex_pwd(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006751{
6752 if (mch_dirname(NameBuff, MAXPATHL) == OK)
6753 {
6754#ifdef BACKSLASH_IN_FILENAME
6755 slash_adjust(NameBuff);
6756#endif
Bram Moolenaar32526b32019-01-19 17:43:09 +01006757 msg((char *)NameBuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006758 }
6759 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006760 emsg(_("E187: Unknown"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006761}
6762
6763/*
6764 * ":=".
6765 */
6766 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006767ex_equal(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006768{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006769 smsg("%ld", (long)eap->line2);
Bram Moolenaardf177f62005-02-22 08:39:57 +00006770 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006771}
6772
6773 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006774ex_sleep(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006775{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006776 int n;
6777 long len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006778
6779 if (cursor_valid())
6780 {
6781 n = W_WINROW(curwin) + curwin->w_wrow - msg_scrolled;
6782 if (n >= 0)
Bram Moolenaar53f81742017-09-22 14:35:51 +02006783 windgoto((int)n, curwin->w_wincol + curwin->w_wcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006784 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006785
6786 len = eap->line2;
6787 switch (*eap->arg)
6788 {
6789 case 'm': break;
6790 case NUL: len *= 1000L; break;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006791 default: semsg(_(e_invarg2), eap->arg); return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006792 }
6793 do_sleep(len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006794}
6795
6796/*
6797 * Sleep for "msec" milliseconds, but keep checking for a CTRL-C every second.
6798 */
6799 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006800do_sleep(long msec)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006801{
Bram Moolenaar52953192019-08-16 10:27:13 +02006802 long done = 0;
Bram Moolenaar975b5272016-03-15 23:10:59 +01006803 long wait_now;
Bram Moolenaar52953192019-08-16 10:27:13 +02006804# ifdef ELAPSED_FUNC
6805 elapsed_T start_tv;
6806
6807 // Remember at what time we started, so that we know how much longer we
6808 // should wait after waiting for a bit.
6809 ELAPSED_INIT(start_tv);
6810# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006811
6812 cursor_on();
Bram Moolenaarf45d7472018-09-30 18:22:26 +02006813 out_flush_cursor(FALSE, FALSE);
Bram Moolenaar52953192019-08-16 10:27:13 +02006814 while (!got_int && done < msec)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006815 {
Bram Moolenaar975b5272016-03-15 23:10:59 +01006816 wait_now = msec - done > 1000L ? 1000L : msec - done;
6817#ifdef FEAT_TIMERS
6818 {
6819 long due_time = check_due_timer();
6820
6821 if (due_time > 0 && due_time < wait_now)
6822 wait_now = due_time;
6823 }
6824#endif
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02006825#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar28e67e02019-08-15 23:05:49 +02006826 if (has_any_channel() && wait_now > 20L)
6827 wait_now = 20L;
6828#endif
6829#ifdef FEAT_SOUND
6830 if (has_any_sound_callback() && wait_now > 20L)
6831 wait_now = 20L;
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02006832#endif
Bram Moolenaar975b5272016-03-15 23:10:59 +01006833 ui_delay(wait_now, TRUE);
Bram Moolenaar52953192019-08-16 10:27:13 +02006834
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02006835#ifdef FEAT_JOB_CHANNEL
6836 if (has_any_channel())
6837 ui_breakcheck_force(TRUE);
6838 else
6839#endif
6840 ui_breakcheck();
Bram Moolenaar93c88e02015-09-15 14:12:05 +02006841#ifdef MESSAGE_QUEUE
Bram Moolenaar52953192019-08-16 10:27:13 +02006842 // Process the netbeans and clientserver messages that may have been
6843 // received in the call to ui_breakcheck() when the GUI is in use. This
6844 // may occur when running a test case.
Bram Moolenaar93c88e02015-09-15 14:12:05 +02006845 parse_queued_messages();
Bram Moolenaare3cc6d42011-10-20 21:58:34 +02006846#endif
Bram Moolenaar52953192019-08-16 10:27:13 +02006847
6848# ifdef ELAPSED_FUNC
6849 // actual time passed
6850 done = ELAPSED_FUNC(start_tv);
6851# else
6852 // guestimate time passed (will actually be more)
6853 done += wait_now;
6854# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006855 }
Bram Moolenaar1ebff3d2018-07-07 16:18:13 +02006856
6857 // If CTRL-C was typed to interrupt the sleep, drop the CTRL-C from the
6858 // input buffer, otherwise a following call to input() fails.
6859 if (got_int)
6860 (void)vpeekc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006861}
6862
Bram Moolenaar071d4272004-06-13 20:20:40 +00006863/*
6864 * ":winsize" command (obsolete).
6865 */
6866 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006867ex_winsize(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006868{
6869 int w, h;
6870 char_u *arg = eap->arg;
6871 char_u *p;
6872
6873 w = getdigits(&arg);
6874 arg = skipwhite(arg);
6875 p = arg;
6876 h = getdigits(&arg);
6877 if (*p != NUL && *arg == NUL)
6878 set_shellsize(w, h, TRUE);
6879 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006880 emsg(_("E465: :winsize requires two number arguments"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006881}
6882
Bram Moolenaar071d4272004-06-13 20:20:40 +00006883 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006884ex_wincmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006885{
6886 int xchar = NUL;
6887 char_u *p;
6888
6889 if (*eap->arg == 'g' || *eap->arg == Ctrl_G)
6890 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006891 // CTRL-W g and CTRL-W CTRL-G have an extra command character
Bram Moolenaar071d4272004-06-13 20:20:40 +00006892 if (eap->arg[1] == NUL)
6893 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006894 emsg(_(e_invarg));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006895 return;
6896 }
6897 xchar = eap->arg[1];
6898 p = eap->arg + 2;
6899 }
6900 else
6901 p = eap->arg + 1;
6902
6903 eap->nextcmd = check_nextcmd(p);
6904 p = skipwhite(p);
6905 if (*p != NUL && *p != '"' && eap->nextcmd == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006906 emsg(_(e_invarg));
Bram Moolenaar12bde492011-06-13 01:19:56 +02006907 else if (!eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006908 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006909 // Pass flags on for ":vertical wincmd ]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006910 postponed_split_flags = cmdmod.split;
Bram Moolenaard326ce82007-03-11 14:48:29 +00006911 postponed_split_tab = cmdmod.tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006912 do_window(*eap->arg, eap->addr_count > 0 ? eap->line2 : 0L, xchar);
6913 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00006914 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006915 }
6916}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006917
Bram Moolenaar843ee412004-06-30 16:16:41 +00006918#if defined(FEAT_GUI) || defined(UNIX) || defined(VMS) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006919/*
6920 * ":winpos".
6921 */
6922 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006923ex_winpos(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006924{
6925 int x, y;
6926 char_u *arg = eap->arg;
6927 char_u *p;
6928
6929 if (*arg == NUL)
6930 {
Bram Moolenaar843ee412004-06-30 16:16:41 +00006931# if defined(FEAT_GUI) || defined(MSWIN)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02006932# ifdef VIMDLL
6933 if (gui.in_use ? gui_mch_get_winpos(&x, &y) != FAIL :
6934 mch_get_winpos(&x, &y) != FAIL)
6935# elif defined(FEAT_GUI)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006936 if (gui.in_use && gui_mch_get_winpos(&x, &y) != FAIL)
Bram Moolenaar843ee412004-06-30 16:16:41 +00006937# else
6938 if (mch_get_winpos(&x, &y) != FAIL)
6939# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006940 {
6941 sprintf((char *)IObuff, _("Window position: X %d, Y %d"), x, y);
Bram Moolenaar32526b32019-01-19 17:43:09 +01006942 msg((char *)IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006943 }
6944 else
6945# endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006946 emsg(_("E188: Obtaining window position not implemented for this platform"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006947 }
6948 else
6949 {
6950 x = getdigits(&arg);
6951 arg = skipwhite(arg);
6952 p = arg;
6953 y = getdigits(&arg);
6954 if (*p == NUL || *arg != NUL)
6955 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006956 emsg(_("E466: :winpos requires two number arguments"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006957 return;
6958 }
6959# ifdef FEAT_GUI
6960 if (gui.in_use)
6961 gui_mch_set_winpos(x, y);
6962 else if (gui.starting)
6963 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006964 // Remember the coordinates for when the window is opened.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006965 gui_win_x = x;
6966 gui_win_y = y;
6967 }
Bram Moolenaarafde13b2019-04-28 19:46:49 +02006968# if defined(HAVE_TGETENT) || defined(VIMDLL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006969 else
6970# endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02006971# endif
6972# if defined(MSWIN) && (!defined(FEAT_GUI) || defined(VIMDLL))
Bram Moolenaar843ee412004-06-30 16:16:41 +00006973 mch_set_winpos(x, y);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006974# endif
6975# ifdef HAVE_TGETENT
6976 if (*T_CWP)
6977 term_set_winpos(x, y);
6978# endif
6979 }
6980}
6981#endif
6982
6983/*
6984 * Handle command that work like operators: ":delete", ":yank", ":>" and ":<".
6985 */
6986 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006987ex_operators(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006988{
6989 oparg_T oa;
6990
6991 clear_oparg(&oa);
6992 oa.regname = eap->regname;
6993 oa.start.lnum = eap->line1;
6994 oa.end.lnum = eap->line2;
6995 oa.line_count = eap->line2 - eap->line1 + 1;
6996 oa.motion_type = MLINE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006997 virtual_op = FALSE;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006998 if (eap->cmdidx != CMD_yank) // position cursor for undo
Bram Moolenaar071d4272004-06-13 20:20:40 +00006999 {
7000 setpcmark();
7001 curwin->w_cursor.lnum = eap->line1;
7002 beginline(BL_SOL | BL_FIX);
7003 }
7004
Bram Moolenaard07c6e12013-11-21 14:21:40 +01007005 if (VIsual_active)
7006 end_visual_mode();
Bram Moolenaard07c6e12013-11-21 14:21:40 +01007007
Bram Moolenaar071d4272004-06-13 20:20:40 +00007008 switch (eap->cmdidx)
7009 {
7010 case CMD_delete:
7011 oa.op_type = OP_DELETE;
7012 op_delete(&oa);
7013 break;
7014
7015 case CMD_yank:
7016 oa.op_type = OP_YANK;
7017 (void)op_yank(&oa, FALSE, TRUE);
7018 break;
7019
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007020 default: // CMD_rshift or CMD_lshift
Bram Moolenaar6e707362013-06-14 19:15:58 +02007021 if (
Bram Moolenaar071d4272004-06-13 20:20:40 +00007022#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6e707362013-06-14 19:15:58 +02007023 (eap->cmdidx == CMD_rshift) ^ curwin->w_p_rl
7024#else
7025 eap->cmdidx == CMD_rshift
Bram Moolenaar071d4272004-06-13 20:20:40 +00007026#endif
Bram Moolenaar6e707362013-06-14 19:15:58 +02007027 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00007028 oa.op_type = OP_RSHIFT;
7029 else
7030 oa.op_type = OP_LSHIFT;
7031 op_shift(&oa, FALSE, eap->amount);
7032 break;
7033 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007034 virtual_op = MAYBE;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007035 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007036}
7037
7038/*
7039 * ":put".
7040 */
7041 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007042ex_put(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007043{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007044 // ":0put" works like ":1put!".
Bram Moolenaar071d4272004-06-13 20:20:40 +00007045 if (eap->line2 == 0)
7046 {
7047 eap->line2 = 1;
7048 eap->forceit = TRUE;
7049 }
7050 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007051 do_put(eap->regname, eap->forceit ? BACKWARD : FORWARD, 1L,
7052 PUT_LINE|PUT_CURSLINE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007053}
7054
7055/*
7056 * Handle ":copy" and ":move".
7057 */
7058 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007059ex_copymove(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007060{
7061 long n;
7062
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02007063 n = get_address(eap, &eap->arg, eap->addr_type, FALSE, FALSE, FALSE, 1);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007064 if (eap->arg == NULL) // error detected
Bram Moolenaar071d4272004-06-13 20:20:40 +00007065 {
7066 eap->nextcmd = NULL;
7067 return;
7068 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00007069 get_flags(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007070
7071 /*
7072 * move or copy lines from 'eap->line1'-'eap->line2' to below line 'n'
7073 */
7074 if (n == MAXLNUM || n < 0 || n > curbuf->b_ml.ml_line_count)
7075 {
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02007076 emsg(_(e_invrange));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007077 return;
7078 }
7079
7080 if (eap->cmdidx == CMD_move)
7081 {
7082 if (do_move(eap->line1, eap->line2, n) == FAIL)
7083 return;
7084 }
7085 else
7086 ex_copy(eap->line1, eap->line2, n);
7087 u_clearline();
7088 beginline(BL_SOL | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007089 ex_may_print(eap);
7090}
7091
7092/*
7093 * Print the current line if flags were given to the Ex command.
7094 */
Bram Moolenaarfd3fe982014-04-01 17:49:44 +02007095 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007096ex_may_print(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007097{
7098 if (eap->flags != 0)
7099 {
7100 print_line(curwin->w_cursor.lnum, (eap->flags & EXFLAG_NR),
7101 (eap->flags & EXFLAG_LIST));
7102 ex_no_reprint = TRUE;
7103 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007104}
7105
7106/*
7107 * ":smagic" and ":snomagic".
7108 */
7109 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007110ex_submagic(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007111{
7112 int magic_save = p_magic;
7113
7114 p_magic = (eap->cmdidx == CMD_smagic);
7115 do_sub(eap);
7116 p_magic = magic_save;
7117}
7118
7119/*
7120 * ":join".
7121 */
7122 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007123ex_join(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007124{
7125 curwin->w_cursor.lnum = eap->line1;
7126 if (eap->line1 == eap->line2)
7127 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007128 if (eap->addr_count >= 2) // :2,2join does nothing
Bram Moolenaar071d4272004-06-13 20:20:40 +00007129 return;
7130 if (eap->line2 == curbuf->b_ml.ml_line_count)
7131 {
7132 beep_flush();
7133 return;
7134 }
7135 ++eap->line2;
7136 }
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02007137 (void)do_join(eap->line2 - eap->line1 + 1, !eap->forceit, TRUE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007138 beginline(BL_WHITE | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007139 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007140}
7141
7142/*
7143 * ":[addr]@r" or ":[addr]*r": execute register
7144 */
7145 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007146ex_at(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007147{
7148 int c;
Bram Moolenaar60462872009-11-03 11:40:19 +00007149 int prev_len = typebuf.tb_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007150
7151 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaar4930a762016-09-11 14:39:53 +02007152 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007153
7154#ifdef USE_ON_FLY_SCROLL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007155 dont_scroll = TRUE; // disallow scrolling here
Bram Moolenaar071d4272004-06-13 20:20:40 +00007156#endif
7157
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007158 // get the register name. No name means to use the previous one
Bram Moolenaar071d4272004-06-13 20:20:40 +00007159 c = *eap->arg;
7160 if (c == NUL || (c == '*' && *eap->cmd == '*'))
7161 c = '@';
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007162 // Put the register in the typeahead buffer with the "silent" flag.
Bram Moolenaard333d1e2006-11-07 17:43:47 +00007163 if (do_execreg(c, TRUE, vim_strchr(p_cpo, CPO_EXECBUF) != NULL, TRUE)
7164 == FAIL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007165 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007166 beep_flush();
Bram Moolenaardf177f62005-02-22 08:39:57 +00007167 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007168 else
7169 {
7170 int save_efr = exec_from_reg;
7171
7172 exec_from_reg = TRUE;
7173
7174 /*
7175 * Execute from the typeahead buffer.
Bram Moolenaar60462872009-11-03 11:40:19 +00007176 * Continue until the stuff buffer is empty and all added characters
7177 * have been consumed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007178 */
Bram Moolenaar60462872009-11-03 11:40:19 +00007179 while (!stuff_empty() || typebuf.tb_len > prev_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007180 (void)do_cmdline(NULL, getexline, NULL, DOCMD_NOWAIT|DOCMD_VERBOSE);
7181
7182 exec_from_reg = save_efr;
7183 }
7184}
7185
7186/*
7187 * ":!".
7188 */
7189 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007190ex_bang(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007191{
7192 do_bang(eap->addr_count, eap, eap->forceit, TRUE, TRUE);
7193}
7194
7195/*
7196 * ":undo".
7197 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007198 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +01007199ex_undo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007200{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007201 if (eap->addr_count == 1) // :undo 123
Bram Moolenaar730cde92010-06-27 05:18:54 +02007202 undo_time(eap->line2, FALSE, FALSE, TRUE);
Bram Moolenaard3667a22006-03-16 21:35:52 +00007203 else
7204 u_undo(1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007205}
7206
Bram Moolenaar55debbe2010-05-23 23:34:36 +02007207#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar6df6f472010-07-18 18:04:50 +02007208 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007209ex_wundo(exarg_T *eap)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02007210{
7211 char_u hash[UNDO_HASH_SIZE];
7212
7213 u_compute_hash(hash);
7214 u_write_undo(eap->arg, eap->forceit, curbuf, hash);
7215}
7216
Bram Moolenaar6df6f472010-07-18 18:04:50 +02007217 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007218ex_rundo(exarg_T *eap)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02007219{
7220 char_u hash[UNDO_HASH_SIZE];
7221
7222 u_compute_hash(hash);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02007223 u_read_undo(eap->arg, hash, NULL);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02007224}
7225#endif
7226
Bram Moolenaar071d4272004-06-13 20:20:40 +00007227/*
7228 * ":redo".
7229 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007230 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007231ex_redo(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007232{
7233 u_redo(1);
7234}
7235
7236/*
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007237 * ":earlier" and ":later".
7238 */
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007239 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007240ex_later(exarg_T *eap)
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007241{
7242 long count = 0;
7243 int sec = FALSE;
Bram Moolenaar730cde92010-06-27 05:18:54 +02007244 int file = FALSE;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007245 char_u *p = eap->arg;
7246
7247 if (*p == NUL)
7248 count = 1;
7249 else if (isdigit(*p))
7250 {
7251 count = getdigits(&p);
7252 switch (*p)
7253 {
7254 case 's': ++p; sec = TRUE; break;
7255 case 'm': ++p; sec = TRUE; count *= 60; break;
7256 case 'h': ++p; sec = TRUE; count *= 60 * 60; break;
Bram Moolenaar730cde92010-06-27 05:18:54 +02007257 case 'd': ++p; sec = TRUE; count *= 24 * 60 * 60; break;
7258 case 'f': ++p; file = TRUE; break;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007259 }
7260 }
7261
7262 if (*p != NUL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007263 semsg(_(e_invarg2), eap->arg);
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007264 else
Bram Moolenaar730cde92010-06-27 05:18:54 +02007265 undo_time(eap->cmdidx == CMD_earlier ? -count : count,
7266 sec, file, FALSE);
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007267}
7268
7269/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007270 * ":redir": start/stop redirection.
7271 */
7272 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007273ex_redir(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007274{
7275 char *mode;
7276 char_u *fname;
Bram Moolenaarca472992005-01-21 11:46:23 +00007277 char_u *arg = eap->arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007278
Bram Moolenaarba768492016-07-08 15:32:54 +02007279#ifdef FEAT_EVAL
Bram Moolenaar79815f12016-07-09 17:07:29 +02007280 if (redir_execute)
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02007281 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007282 emsg(_("E930: Cannot use :redir inside execute()"));
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02007283 return;
7284 }
Bram Moolenaarba768492016-07-08 15:32:54 +02007285#endif
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02007286
Bram Moolenaar071d4272004-06-13 20:20:40 +00007287 if (STRICMP(eap->arg, "END") == 0)
7288 close_redir();
7289 else
7290 {
Bram Moolenaarca472992005-01-21 11:46:23 +00007291 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00007292 {
Bram Moolenaarca472992005-01-21 11:46:23 +00007293 ++arg;
7294 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00007295 {
Bram Moolenaarca472992005-01-21 11:46:23 +00007296 ++arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007297 mode = "a";
7298 }
7299 else
7300 mode = "w";
Bram Moolenaarca472992005-01-21 11:46:23 +00007301 arg = skipwhite(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007302
7303 close_redir();
7304
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007305 // Expand environment variables and "~/".
Bram Moolenaarca472992005-01-21 11:46:23 +00007306 fname = expand_env_save(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007307 if (fname == NULL)
7308 return;
7309#ifdef FEAT_BROWSE
7310 if (cmdmod.browse)
7311 {
7312 char_u *browseFile;
7313
Bram Moolenaar7171abe2004-10-11 10:06:20 +00007314 browseFile = do_browse(BROWSE_SAVE,
7315 (char_u *)_("Save Redirection"),
Bram Moolenaarc36651b2018-04-29 12:22:56 +02007316 fname, NULL, NULL,
7317 (char_u *)_(BROWSE_FILTER_ALL_FILES), curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007318 if (browseFile == NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007319 return; // operation cancelled
Bram Moolenaar071d4272004-06-13 20:20:40 +00007320 vim_free(fname);
7321 fname = browseFile;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007322 eap->forceit = TRUE; // since dialog already asked
Bram Moolenaar071d4272004-06-13 20:20:40 +00007323 }
7324#endif
7325
7326 redir_fd = open_exfile(fname, eap->forceit, mode);
7327 vim_free(fname);
7328 }
7329#ifdef FEAT_EVAL
Bram Moolenaarca472992005-01-21 11:46:23 +00007330 else if (*arg == '@')
Bram Moolenaar071d4272004-06-13 20:20:40 +00007331 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007332 // redirect to a register a-z (resp. A-Z for appending)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007333 close_redir();
Bram Moolenaarca472992005-01-21 11:46:23 +00007334 ++arg;
7335 if (ASCII_ISALPHA(*arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007336# ifdef FEAT_CLIPBOARD
Bram Moolenaarca472992005-01-21 11:46:23 +00007337 || *arg == '*'
Bram Moolenaar9a51c6e2006-11-14 19:25:02 +00007338 || *arg == '+'
Bram Moolenaar071d4272004-06-13 20:20:40 +00007339# endif
Bram Moolenaarca472992005-01-21 11:46:23 +00007340 || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00007341 {
Bram Moolenaarca472992005-01-21 11:46:23 +00007342 redir_reg = *arg++;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007343 if (*arg == '>' && arg[1] == '>') // append
Bram Moolenaard9d30582005-05-18 22:10:28 +00007344 arg += 2;
Bram Moolenaarc188b882007-10-19 14:20:54 +00007345 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007346 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007347 // Can use both "@a" and "@a>".
Bram Moolenaar2c29bee2005-06-01 21:46:07 +00007348 if (*arg == '>')
7349 arg++;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007350 // Make register empty when not using @A-@Z and the
7351 // command is valid.
Bram Moolenaarc188b882007-10-19 14:20:54 +00007352 if (*arg == NUL && !isupper(redir_reg))
7353 write_reg_contents(redir_reg, (char_u *)"", -1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007354 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00007355 }
7356 if (*arg != NUL)
7357 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00007358 redir_reg = 0;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007359 semsg(_(e_invarg2), eap->arg);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007360 }
7361 }
7362 else if (*arg == '=' && arg[1] == '>')
7363 {
7364 int append;
7365
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007366 // redirect to a variable
Bram Moolenaardf177f62005-02-22 08:39:57 +00007367 close_redir();
7368 arg += 2;
7369
7370 if (*arg == '>')
7371 {
7372 ++arg;
7373 append = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007374 }
7375 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00007376 append = FALSE;
7377
7378 if (var_redir_start(skipwhite(arg), append) == OK)
7379 redir_vname = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007380 }
7381#endif
7382
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007383 // TODO: redirect to a buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00007384
Bram Moolenaar071d4272004-06-13 20:20:40 +00007385 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007386 semsg(_(e_invarg2), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007387 }
Bram Moolenaar29b2d262006-09-10 19:07:28 +00007388
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007389 // Make sure redirection is not off. Can happen for cmdline completion
7390 // that indirectly invokes a command to catch its output.
Bram Moolenaar29b2d262006-09-10 19:07:28 +00007391 if (redir_fd != NULL
7392#ifdef FEAT_EVAL
7393 || redir_reg || redir_vname
7394#endif
7395 )
7396 redir_off = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007397}
7398
7399/*
7400 * ":redraw": force redraw
7401 */
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01007402 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007403ex_redraw(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007404{
7405 int r = RedrawingDisabled;
7406 int p = p_lz;
7407
7408 RedrawingDisabled = 0;
7409 p_lz = FALSE;
Bram Moolenaarabc39ab2017-03-01 18:04:05 +01007410 validate_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007411 update_topline();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007412 update_screen(eap->forceit ? CLEAR : VIsual_active ? INVERTED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007413#ifdef FEAT_TITLE
7414 if (need_maketitle)
7415 maketitle();
7416#endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007417#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
7418# ifdef VIMDLL
7419 if (!gui.in_use)
7420# endif
7421 resize_console_buf();
Bram Moolenaar78d21da2019-02-17 15:00:52 +01007422#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007423 RedrawingDisabled = r;
7424 p_lz = p;
7425
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007426 // Reset msg_didout, so that a message that's there is overwritten.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007427 msg_didout = FALSE;
7428 msg_col = 0;
7429
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007430 // No need to wait after an intentional redraw.
Bram Moolenaar56667a52013-07-17 11:54:28 +02007431 need_wait_return = FALSE;
7432
Bram Moolenaar071d4272004-06-13 20:20:40 +00007433 out_flush();
7434}
7435
7436/*
7437 * ":redrawstatus": force redraw of status line(s)
7438 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007439 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007440ex_redrawstatus(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007441{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007442 int r = RedrawingDisabled;
7443 int p = p_lz;
7444
7445 RedrawingDisabled = 0;
7446 p_lz = FALSE;
7447 if (eap->forceit)
7448 status_redraw_all();
7449 else
7450 status_redraw_curbuf();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007451 update_screen(VIsual_active ? INVERTED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007452 RedrawingDisabled = r;
7453 p_lz = p;
7454 out_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007455}
7456
Bram Moolenaare12bab32019-01-08 22:02:56 +01007457/*
7458 * ":redrawtabline": force redraw of the tabline
7459 */
7460 static void
7461ex_redrawtabline(exarg_T *eap UNUSED)
7462{
7463 int r = RedrawingDisabled;
7464 int p = p_lz;
7465
7466 RedrawingDisabled = 0;
7467 p_lz = FALSE;
7468
7469 draw_tabline();
7470
7471 RedrawingDisabled = r;
7472 p_lz = p;
7473 out_flush();
7474}
7475
Bram Moolenaar071d4272004-06-13 20:20:40 +00007476 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007477close_redir(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007478{
7479 if (redir_fd != NULL)
7480 {
7481 fclose(redir_fd);
7482 redir_fd = NULL;
7483 }
7484#ifdef FEAT_EVAL
7485 redir_reg = 0;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007486 if (redir_vname)
7487 {
7488 var_redir_stop();
7489 redir_vname = 0;
7490 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007491#endif
7492}
7493
Bram Moolenaarf96ae0b2019-07-28 15:21:55 +02007494#if (defined(FEAT_SESSION) || defined(FEAT_EVAL)) || defined(PROTO)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007495 int
Bram Moolenaarbd67aac2019-09-21 23:09:04 +02007496vim_mkdir_emsg(char_u *name, int prot UNUSED)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007497{
7498 if (vim_mkdir(name, prot) != 0)
7499 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007500 semsg(_("E739: Cannot create directory: %s"), name);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007501 return FAIL;
7502 }
7503 return OK;
7504}
7505#endif
7506
Bram Moolenaar071d4272004-06-13 20:20:40 +00007507/*
7508 * Open a file for writing for an Ex command, with some checks.
7509 * Return file descriptor, or NULL on failure.
7510 */
7511 FILE *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007512open_exfile(
7513 char_u *fname,
7514 int forceit,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007515 char *mode) // "w" for create new file or "a" for append
Bram Moolenaar071d4272004-06-13 20:20:40 +00007516{
7517 FILE *fd;
7518
7519#ifdef UNIX
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007520 // with Unix it is possible to open a directory
Bram Moolenaar071d4272004-06-13 20:20:40 +00007521 if (mch_isdir(fname))
7522 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007523 semsg(_(e_isadir2), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007524 return NULL;
7525 }
7526#endif
7527 if (!forceit && *mode != 'a' && vim_fexists(fname))
7528 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007529 semsg(_("E189: \"%s\" exists (add ! to override)"), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007530 return NULL;
7531 }
7532
7533 if ((fd = mch_fopen((char *)fname, mode)) == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007534 semsg(_("E190: Cannot open \"%s\" for writing"), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007535
7536 return fd;
7537}
7538
7539/*
7540 * ":mark" and ":k".
7541 */
7542 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007543ex_mark(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007544{
7545 pos_T pos;
7546
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007547 if (*eap->arg == NUL) // No argument?
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007548 emsg(_(e_argreq));
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007549 else if (eap->arg[1] != NUL) // more than one character?
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007550 emsg(_(e_trailing));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007551 else
7552 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007553 pos = curwin->w_cursor; // save curwin->w_cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00007554 curwin->w_cursor.lnum = eap->line2;
7555 beginline(BL_WHITE | BL_FIX);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007556 if (setmark(*eap->arg) == FAIL) // set mark
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007557 emsg(_("E191: Argument must be a letter or forward/backward quote"));
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007558 curwin->w_cursor = pos; // restore curwin->w_cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00007559 }
7560}
7561
7562/*
7563 * Update w_topline, w_leftcol and the cursor position.
7564 */
7565 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007566update_topline_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007567{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007568 check_cursor(); // put cursor on valid line
Bram Moolenaar071d4272004-06-13 20:20:40 +00007569 update_topline();
7570 if (!curwin->w_p_wrap)
7571 validate_cursor();
7572 update_curswant();
7573}
7574
Bram Moolenaar071d4272004-06-13 20:20:40 +00007575/*
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007576 * Save the current State and go to Normal mode.
7577 * Return TRUE if the typeahead could be saved.
7578 */
7579 int
7580save_current_state(save_state_T *sst)
7581{
7582 sst->save_msg_scroll = msg_scroll;
7583 sst->save_restart_edit = restart_edit;
7584 sst->save_msg_didout = msg_didout;
7585 sst->save_State = State;
7586 sst->save_insertmode = p_im;
7587 sst->save_finish_op = finish_op;
7588 sst->save_opcount = opcount;
Bram Moolenaarcce713d2019-03-04 11:40:12 +01007589 sst->save_reg_executing = reg_executing;
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007590
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007591 msg_scroll = FALSE; // no msg scrolling in Normal mode
7592 restart_edit = 0; // don't go to Insert mode
7593 p_im = FALSE; // don't use 'insertmode'
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007594
7595 /*
7596 * Save the current typeahead. This is required to allow using ":normal"
7597 * from an event handler and makes sure we don't hang when the argument
7598 * ends with half a command.
7599 */
7600 save_typeahead(&sst->tabuf);
7601 return sst->tabuf.typebuf_valid;
7602}
7603
7604 void
7605restore_current_state(save_state_T *sst)
7606{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007607 // Restore the previous typeahead.
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007608 restore_typeahead(&sst->tabuf);
7609
7610 msg_scroll = sst->save_msg_scroll;
7611 restart_edit = sst->save_restart_edit;
7612 p_im = sst->save_insertmode;
7613 finish_op = sst->save_finish_op;
7614 opcount = sst->save_opcount;
Bram Moolenaarcce713d2019-03-04 11:40:12 +01007615 reg_executing = sst->save_reg_executing;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007616 msg_didout |= sst->save_msg_didout; // don't reset msg_didout now
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007617
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007618 // Restore the state (needed when called from a function executed for
7619 // 'indentexpr'). Update the mouse and cursor, they may have changed.
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007620 State = sst->save_State;
7621#ifdef CURSOR_SHAPE
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007622 ui_cursor_shape(); // may show different cursor shape
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007623#endif
7624}
7625
7626/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007627 * ":normal[!] {commands}": Execute normal mode commands.
7628 */
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01007629 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007630ex_normal(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007631{
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007632 save_state_T save_state;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007633 char_u *arg = NULL;
7634 int l;
7635 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007636
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00007637 if (ex_normal_lock > 0)
7638 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007639 emsg(_(e_secure));
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00007640 return;
7641 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007642 if (ex_normal_busy >= p_mmd)
7643 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007644 emsg(_("E192: Recursive use of :normal too deep"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007645 return;
7646 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007647
Bram Moolenaar071d4272004-06-13 20:20:40 +00007648 /*
7649 * vgetc() expects a CSI and K_SPECIAL to have been escaped. Don't do
7650 * this for the K_SPECIAL leading byte, otherwise special keys will not
7651 * work.
7652 */
7653 if (has_mbyte)
7654 {
7655 int len = 0;
7656
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007657 // Count the number of characters to be escaped.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007658 for (p = eap->arg; *p != NUL; ++p)
7659 {
Bram Moolenaar13505972019-01-24 15:04:48 +01007660#ifdef FEAT_GUI
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007661 if (*p == CSI) // leadbyte CSI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007662 len += 2;
Bram Moolenaar13505972019-01-24 15:04:48 +01007663#endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007664 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007665 if (*++p == K_SPECIAL // trailbyte K_SPECIAL or CSI
Bram Moolenaar13505972019-01-24 15:04:48 +01007666#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007667 || *p == CSI
Bram Moolenaar13505972019-01-24 15:04:48 +01007668#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007669 )
7670 len += 2;
7671 }
7672 if (len > 0)
7673 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02007674 arg = alloc(STRLEN(eap->arg) + len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007675 if (arg != NULL)
7676 {
7677 len = 0;
7678 for (p = eap->arg; *p != NUL; ++p)
7679 {
7680 arg[len++] = *p;
Bram Moolenaar13505972019-01-24 15:04:48 +01007681#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007682 if (*p == CSI)
7683 {
7684 arg[len++] = KS_EXTRA;
7685 arg[len++] = (int)KE_CSI;
7686 }
Bram Moolenaar13505972019-01-24 15:04:48 +01007687#endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007688 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007689 {
7690 arg[len++] = *++p;
7691 if (*p == K_SPECIAL)
7692 {
7693 arg[len++] = KS_SPECIAL;
7694 arg[len++] = KE_FILLER;
7695 }
Bram Moolenaar13505972019-01-24 15:04:48 +01007696#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007697 else if (*p == CSI)
7698 {
7699 arg[len++] = KS_EXTRA;
7700 arg[len++] = (int)KE_CSI;
7701 }
Bram Moolenaar13505972019-01-24 15:04:48 +01007702#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007703 }
7704 arg[len] = NUL;
7705 }
7706 }
7707 }
7708 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007709
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007710 ++ex_normal_busy;
7711 if (save_current_state(&save_state))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007712 {
7713 /*
7714 * Repeat the :normal command for each line in the range. When no
7715 * range given, execute it just once, without positioning the cursor
7716 * first.
7717 */
7718 do
7719 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007720 if (eap->addr_count != 0)
7721 {
7722 curwin->w_cursor.lnum = eap->line1++;
7723 curwin->w_cursor.col = 0;
Bram Moolenaard5d37532017-03-27 23:02:07 +02007724 check_cursor_moved(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007725 }
7726
Bram Moolenaar13505972019-01-24 15:04:48 +01007727 exec_normal_cmd(arg != NULL
7728 ? arg
7729 : eap->arg, eap->forceit ? REMAP_NONE : REMAP_YES, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007730 }
7731 while (eap->addr_count > 0 && eap->line1 <= eap->line2 && !got_int);
7732 }
7733
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007734 // Might not return to the main loop when in an event handler.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007735 update_topline_cursor();
7736
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007737 restore_current_state(&save_state);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007738 --ex_normal_busy;
Bram Moolenaareda73602014-11-05 09:53:23 +01007739 setmouse();
Bram Moolenaareda73602014-11-05 09:53:23 +01007740#ifdef CURSOR_SHAPE
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007741 ui_cursor_shape(); // may show different cursor shape
Bram Moolenaareda73602014-11-05 09:53:23 +01007742#endif
7743
Bram Moolenaar071d4272004-06-13 20:20:40 +00007744 vim_free(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007745}
7746
7747/*
Bram Moolenaar64969662005-12-14 21:59:55 +00007748 * ":startinsert", ":startreplace" and ":startgreplace"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007749 */
7750 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007751ex_startinsert(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007752{
Bram Moolenaarfd371682005-01-14 21:42:54 +00007753 if (eap->forceit)
7754 {
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02007755 // cursor line can be zero on startup
Bram Moolenaar09ca9322017-09-26 17:40:45 +02007756 if (!curwin->w_cursor.lnum)
7757 curwin->w_cursor.lnum = 1;
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02007758 set_cursor_for_append_to_line();
Bram Moolenaarfd371682005-01-14 21:42:54 +00007759 }
Bram Moolenaareb58a242020-04-19 18:13:19 +02007760#ifdef FEAT_TERMINAL
7761 // Ignore this when running in an active terminal.
7762 if (term_job_running(curbuf->b_term))
7763 return;
7764#endif
Bram Moolenaarfd371682005-01-14 21:42:54 +00007765
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02007766 // Ignore the command when already in Insert mode. Inserting an
7767 // expression register that invokes a function can do this.
Bram Moolenaara40c5002005-01-09 21:16:21 +00007768 if (State & INSERT)
7769 return;
7770
Bram Moolenaar64969662005-12-14 21:59:55 +00007771 if (eap->cmdidx == CMD_startinsert)
7772 restart_edit = 'a';
7773 else if (eap->cmdidx == CMD_startreplace)
7774 restart_edit = 'R';
Bram Moolenaar071d4272004-06-13 20:20:40 +00007775 else
Bram Moolenaar64969662005-12-14 21:59:55 +00007776 restart_edit = 'V';
7777
7778 if (!eap->forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007779 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00007780 if (eap->cmdidx == CMD_startinsert)
7781 restart_edit = 'i';
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02007782 curwin->w_curswant = 0; // avoid MAXCOL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007783 }
7784}
7785
7786/*
7787 * ":stopinsert"
7788 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007789 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007790ex_stopinsert(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007791{
7792 restart_edit = 0;
7793 stop_insert_mode = TRUE;
Bram Moolenaarfd773e92016-04-02 19:39:16 +02007794 clearmode();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007795}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007796
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00007797/*
7798 * Execute normal mode command "cmd".
7799 * "remap" can be REMAP_NONE or REMAP_YES.
7800 */
7801 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007802exec_normal_cmd(char_u *cmd, int remap, int silent)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00007803{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007804 // Stuff the argument into the typeahead buffer.
Bram Moolenaar25281632016-01-21 23:32:32 +01007805 ins_typebuf(cmd, remap, 0, TRUE, silent);
Bram Moolenaar586c70c2018-10-02 16:23:58 +02007806 exec_normal(FALSE, FALSE, FALSE);
Bram Moolenaar25281632016-01-21 23:32:32 +01007807}
Bram Moolenaar25281632016-01-21 23:32:32 +01007808
Bram Moolenaar25281632016-01-21 23:32:32 +01007809/*
7810 * Execute normal_cmd() until there is no typeahead left.
Bram Moolenaar586c70c2018-10-02 16:23:58 +02007811 * When "use_vpeekc" is TRUE use vpeekc() to check for available chars.
Bram Moolenaar25281632016-01-21 23:32:32 +01007812 */
7813 void
Bram Moolenaar586c70c2018-10-02 16:23:58 +02007814exec_normal(int was_typed, int use_vpeekc, int may_use_terminal_loop UNUSED)
Bram Moolenaar25281632016-01-21 23:32:32 +01007815{
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00007816 oparg_T oa;
Bram Moolenaar905dd902019-04-07 14:21:47 +02007817 int c;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00007818
Bram Moolenaar905dd902019-04-07 14:21:47 +02007819 // When calling vpeekc() from feedkeys() it will return Ctrl_C when there
7820 // is nothing to get, so also check for Ctrl_C.
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00007821 clear_oparg(&oa);
7822 finish_op = FALSE;
Bram Moolenaar586c70c2018-10-02 16:23:58 +02007823 while ((!stuff_empty()
7824 || ((was_typed || !typebuf_typed()) && typebuf.tb_len > 0)
Bram Moolenaar905dd902019-04-07 14:21:47 +02007825 || (use_vpeekc && (c = vpeekc()) != NUL && c != Ctrl_C))
Bram Moolenaar586c70c2018-10-02 16:23:58 +02007826 && !got_int)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00007827 {
7828 update_topline_cursor();
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02007829#ifdef FEAT_TERMINAL
Bram Moolenaar655a82a2018-05-08 22:01:07 +02007830 if (may_use_terminal_loop && term_use_loop()
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02007831 && oa.op_type == OP_NOP && oa.regname == NUL
7832 && !VIsual_active)
7833 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007834 // If terminal_loop() returns OK we got a key that is handled
7835 // in Normal model. With FAIL we first need to position the
7836 // cursor and the screen needs to be redrawn.
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02007837 if (terminal_loop(TRUE) == OK)
7838 normal_cmd(&oa, TRUE);
7839 }
7840 else
7841#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007842 // execute a Normal mode cmd
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02007843 normal_cmd(&oa, TRUE);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00007844 }
7845}
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00007846
Bram Moolenaar071d4272004-06-13 20:20:40 +00007847#ifdef FEAT_FIND_ID
7848 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007849ex_checkpath(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007850{
7851 find_pattern_in_path(NULL, 0, 0, FALSE, FALSE, CHECK_PATH, 1L,
7852 eap->forceit ? ACTION_SHOW_ALL : ACTION_SHOW,
7853 (linenr_T)1, (linenr_T)MAXLNUM);
7854}
7855
Bram Moolenaar4033c552017-09-16 20:54:51 +02007856#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007857/*
7858 * ":psearch"
7859 */
7860 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007861ex_psearch(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007862{
7863 g_do_tagpreview = p_pvh;
7864 ex_findpat(eap);
7865 g_do_tagpreview = 0;
7866}
7867#endif
7868
7869 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007870ex_findpat(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007871{
7872 int whole = TRUE;
7873 long n;
7874 char_u *p;
7875 int action;
7876
7877 switch (cmdnames[eap->cmdidx].cmd_name[2])
7878 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007879 case 'e': // ":psearch", ":isearch" and ":dsearch"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007880 if (cmdnames[eap->cmdidx].cmd_name[0] == 'p')
7881 action = ACTION_GOTO;
7882 else
7883 action = ACTION_SHOW;
7884 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007885 case 'i': // ":ilist" and ":dlist"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007886 action = ACTION_SHOW_ALL;
7887 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007888 case 'u': // ":ijump" and ":djump"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007889 action = ACTION_GOTO;
7890 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007891 default: // ":isplit" and ":dsplit"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007892 action = ACTION_SPLIT;
7893 break;
7894 }
7895
7896 n = 1;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007897 if (vim_isdigit(*eap->arg)) // get count
Bram Moolenaar071d4272004-06-13 20:20:40 +00007898 {
7899 n = getdigits(&eap->arg);
7900 eap->arg = skipwhite(eap->arg);
7901 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007902 if (*eap->arg == '/') // Match regexp, not just whole words
Bram Moolenaar071d4272004-06-13 20:20:40 +00007903 {
7904 whole = FALSE;
7905 ++eap->arg;
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02007906 p = skip_regexp(eap->arg, '/', p_magic);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007907 if (*p)
7908 {
7909 *p++ = NUL;
7910 p = skipwhite(p);
7911
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007912 // Check for trailing illegal characters
Bram Moolenaarfaac4102020-04-20 17:46:14 +02007913 if (!ends_excmd2(eap->arg, p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007914 eap->errmsg = e_trailing;
7915 else
7916 eap->nextcmd = check_nextcmd(p);
7917 }
7918 }
7919 if (!eap->skip)
7920 find_pattern_in_path(eap->arg, 0, (int)STRLEN(eap->arg),
7921 whole, !eap->forceit,
7922 *eap->cmd == 'd' ? FIND_DEFINE : FIND_ANY,
7923 n, action, eap->line1, eap->line2);
7924}
7925#endif
7926
Bram Moolenaar071d4272004-06-13 20:20:40 +00007927
Bram Moolenaar4033c552017-09-16 20:54:51 +02007928#ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00007929/*
7930 * ":ptag", ":ptselect", ":ptjump", ":ptnext", etc.
7931 */
7932 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007933ex_ptag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007934{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007935 g_do_tagpreview = p_pvh; // will be reset to 0 in ex_tag_cmd()
Bram Moolenaar071d4272004-06-13 20:20:40 +00007936 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
7937}
7938
7939/*
7940 * ":pedit"
7941 */
7942 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007943ex_pedit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007944{
7945 win_T *curwin_save = curwin;
7946
Bram Moolenaar3a2505c2020-03-09 16:40:41 +01007947 if (ERROR_IF_ANY_POPUP_WINDOW)
7948 return;
7949
Bram Moolenaar026587b2019-08-17 15:08:00 +02007950 // Open the preview window or popup and make it the current window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007951 g_do_tagpreview = p_pvh;
Bram Moolenaar576a4a62019-08-18 15:25:17 +02007952 prepare_tagpreview(TRUE, TRUE, FALSE);
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02007953
Bram Moolenaar026587b2019-08-17 15:08:00 +02007954 // Edit the file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007955 do_exedit(eap, NULL);
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02007956
Bram Moolenaar071d4272004-06-13 20:20:40 +00007957 if (curwin != curwin_save && win_valid(curwin_save))
7958 {
Bram Moolenaar026587b2019-08-17 15:08:00 +02007959 // Return cursor to where we were
Bram Moolenaar071d4272004-06-13 20:20:40 +00007960 validate_cursor();
7961 redraw_later(VALID);
7962 win_enter(curwin_save, TRUE);
7963 }
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01007964# ifdef FEAT_PROP_POPUP
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02007965 else if (WIN_IS_POPUP(curwin))
7966 {
7967 // can't keep focus in popup window
7968 win_enter(firstwin, TRUE);
7969 }
7970# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007971 g_do_tagpreview = 0;
7972}
Bram Moolenaar4033c552017-09-16 20:54:51 +02007973#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007974
7975/*
7976 * ":stag", ":stselect" and ":stjump".
7977 */
7978 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007979ex_stag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007980{
7981 postponed_split = -1;
7982 postponed_split_flags = cmdmod.split;
Bram Moolenaard326ce82007-03-11 14:48:29 +00007983 postponed_split_tab = cmdmod.tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007984 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
7985 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00007986 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007987}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007988
7989/*
7990 * ":tag", ":tselect", ":tjump", ":tnext", etc.
7991 */
7992 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007993ex_tag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007994{
7995 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name);
7996}
7997
7998 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007999ex_tag_cmd(exarg_T *eap, char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008000{
8001 int cmd;
8002
8003 switch (name[1])
8004 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008005 case 'j': cmd = DT_JUMP; // ":tjump"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008006 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008007 case 's': cmd = DT_SELECT; // ":tselect"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008008 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008009 case 'p': cmd = DT_PREV; // ":tprevious"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008010 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008011 case 'N': cmd = DT_PREV; // ":tNext"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008012 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008013 case 'n': cmd = DT_NEXT; // ":tnext"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008014 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008015 case 'o': cmd = DT_POP; // ":pop"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008016 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008017 case 'f': // ":tfirst"
8018 case 'r': cmd = DT_FIRST; // ":trewind"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008019 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008020 case 'l': cmd = DT_LAST; // ":tlast"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008021 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008022 default: // ":tag"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008023#ifdef FEAT_CSCOPE
Bram Moolenaar7c94c262008-06-20 09:11:34 +00008024 if (p_cst && *eap->arg != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008025 {
Bram Moolenaard4db7712016-11-12 19:16:46 +01008026 ex_cstag(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008027 return;
8028 }
8029#endif
8030 cmd = DT_TAG;
8031 break;
8032 }
8033
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00008034 if (name[0] == 'l')
8035 {
8036#ifndef FEAT_QUICKFIX
8037 ex_ni(eap);
8038 return;
8039#else
8040 cmd = DT_LTAG;
8041#endif
8042 }
8043
Bram Moolenaar071d4272004-06-13 20:20:40 +00008044 do_tag(eap->arg, cmd, eap->addr_count > 0 ? (int)eap->line2 : 1,
8045 eap->forceit, TRUE);
8046}
8047
8048/*
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008049 * Check "str" for starting with a special cmdline variable.
8050 * If found return one of the SPEC_ values and set "*usedlen" to the length of
8051 * the variable. Otherwise return -1 and "*usedlen" is unchanged.
8052 */
8053 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008054find_cmdline_var(char_u *src, int *usedlen)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008055{
8056 int len;
8057 int i;
Bram Moolenaar8f0b2d42009-05-16 14:41:10 +00008058 static char *(spec_str[]) = {
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008059 "%",
8060#define SPEC_PERC 0
8061 "#",
Bram Moolenaar65f08472017-09-10 18:16:20 +02008062#define SPEC_HASH (SPEC_PERC + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008063 "<cword>", // cursor word
Bram Moolenaar65f08472017-09-10 18:16:20 +02008064#define SPEC_CWORD (SPEC_HASH + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008065 "<cWORD>", // cursor WORD
Bram Moolenaar65f08472017-09-10 18:16:20 +02008066#define SPEC_CCWORD (SPEC_CWORD + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008067 "<cexpr>", // expr under cursor
Bram Moolenaar65f08472017-09-10 18:16:20 +02008068#define SPEC_CEXPR (SPEC_CCWORD + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008069 "<cfile>", // cursor path name
Bram Moolenaar65f08472017-09-10 18:16:20 +02008070#define SPEC_CFILE (SPEC_CEXPR + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008071 "<sfile>", // ":so" file name
Bram Moolenaar65f08472017-09-10 18:16:20 +02008072#define SPEC_SFILE (SPEC_CFILE + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008073 "<slnum>", // ":so" file line number
Bram Moolenaar65f08472017-09-10 18:16:20 +02008074#define SPEC_SLNUM (SPEC_SFILE + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008075 "<afile>", // autocommand file name
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008076#define SPEC_AFILE (SPEC_SLNUM + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008077 "<abuf>", // autocommand buffer number
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008078#define SPEC_ABUF (SPEC_AFILE + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008079 "<amatch>", // autocommand match name
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01008080#define SPEC_AMATCH (SPEC_ABUF + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008081 "<sflnum>", // script file line number
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008082#define SPEC_SFLNUM (SPEC_AMATCH + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008083#ifdef FEAT_CLIENTSERVER
8084 "<client>"
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008085# define SPEC_CLIENT (SPEC_SFLNUM + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008086#endif
8087 };
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008088
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00008089 for (i = 0; i < (int)(sizeof(spec_str) / sizeof(char *)); ++i)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008090 {
8091 len = (int)STRLEN(spec_str[i]);
8092 if (STRNCMP(src, spec_str[i], len) == 0)
8093 {
8094 *usedlen = len;
8095 return i;
8096 }
8097 }
8098 return -1;
8099}
8100
8101/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008102 * Evaluate cmdline variables.
8103 *
8104 * change '%' to curbuf->b_ffname
8105 * '#' to curwin->w_altfile
8106 * '<cword>' to word under the cursor
8107 * '<cWORD>' to WORD under the cursor
Bram Moolenaar8071cb22019-07-12 17:58:01 +02008108 * '<cexpr>' to C-expression under the cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00008109 * '<cfile>' to path name under the cursor
8110 * '<sfile>' to sourced file name
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01008111 * '<slnum>' to sourced file line number
Bram Moolenaar071d4272004-06-13 20:20:40 +00008112 * '<afile>' to file name for autocommand
8113 * '<abuf>' to buffer number for autocommand
8114 * '<amatch>' to matching name for autocommand
8115 *
8116 * When an error is detected, "errormsg" is set to a non-NULL pointer (may be
8117 * "" for error without a message) and NULL is returned.
8118 * Returns an allocated string if a valid match was found.
8119 * Returns NULL if no match was found. "usedlen" then still contains the
8120 * number of characters to skip.
8121 */
8122 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008123eval_vars(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008124 char_u *src, // pointer into commandline
8125 char_u *srcstart, // beginning of valid memory for src
8126 int *usedlen, // characters after src that are used
8127 linenr_T *lnump, // line number for :e command, or NULL
8128 char **errormsg, // pointer to error message
8129 int *escaped) // return value has escaped white space (can
8130 // be NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008131{
8132 int i;
8133 char_u *s;
8134 char_u *result;
8135 char_u *resultbuf = NULL;
8136 int resultlen;
8137 buf_T *buf;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008138 int valid = VALID_HEAD + VALID_PATH; // assume valid result
Bram Moolenaar071d4272004-06-13 20:20:40 +00008139 int spec_idx;
Bram Moolenaarfd249462018-07-28 16:14:30 +02008140 int tilde_file = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008141 int skip_mod = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008142 char_u strbuf[30];
Bram Moolenaar071d4272004-06-13 20:20:40 +00008143
8144 *errormsg = NULL;
Bram Moolenaar63b92542007-03-27 14:57:09 +00008145 if (escaped != NULL)
8146 *escaped = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008147
8148 /*
8149 * Check if there is something to do.
8150 */
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008151 spec_idx = find_cmdline_var(src, usedlen);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008152 if (spec_idx < 0) // no match
Bram Moolenaar071d4272004-06-13 20:20:40 +00008153 {
8154 *usedlen = 1;
8155 return NULL;
8156 }
8157
8158 /*
8159 * Skip when preceded with a backslash "\%" and "\#".
8160 * Note: In "\\%" the % is also not recognized!
8161 */
8162 if (src > srcstart && src[-1] == '\\')
8163 {
8164 *usedlen = 0;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008165 STRMOVE(src - 1, src); // remove backslash
Bram Moolenaar071d4272004-06-13 20:20:40 +00008166 return NULL;
8167 }
8168
8169 /*
8170 * word or WORD under cursor
8171 */
Bram Moolenaar65f08472017-09-10 18:16:20 +02008172 if (spec_idx == SPEC_CWORD || spec_idx == SPEC_CCWORD
8173 || spec_idx == SPEC_CEXPR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008174 {
Bram Moolenaar65f08472017-09-10 18:16:20 +02008175 resultlen = find_ident_under_cursor(&result,
8176 spec_idx == SPEC_CWORD ? (FIND_IDENT | FIND_STRING)
8177 : spec_idx == SPEC_CEXPR ? (FIND_IDENT | FIND_STRING | FIND_EVAL)
8178 : FIND_STRING);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008179 if (resultlen == 0)
8180 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008181 *errormsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +00008182 return NULL;
8183 }
8184 }
8185
8186 /*
8187 * '#': Alternate file name
8188 * '%': Current file name
8189 * File name under the cursor
8190 * File name for autocommand
8191 * and following modifiers
8192 */
8193 else
8194 {
8195 switch (spec_idx)
8196 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008197 case SPEC_PERC: // '%': current file
Bram Moolenaar071d4272004-06-13 20:20:40 +00008198 if (curbuf->b_fname == NULL)
8199 {
8200 result = (char_u *)"";
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008201 valid = 0; // Must have ":p:h" to be valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00008202 }
8203 else
Bram Moolenaar00136dc2018-07-25 21:19:13 +02008204 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008205 result = curbuf->b_fname;
Bram Moolenaar00136dc2018-07-25 21:19:13 +02008206 tilde_file = STRCMP(result, "~") == 0;
8207 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008208 break;
8209
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008210 case SPEC_HASH: // '#' or "#99": alternate file
8211 if (src[1] == '#') // "##": the argument list
Bram Moolenaar071d4272004-06-13 20:20:40 +00008212 {
8213 result = arg_all();
8214 resultbuf = result;
8215 *usedlen = 2;
Bram Moolenaar63b92542007-03-27 14:57:09 +00008216 if (escaped != NULL)
8217 *escaped = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008218 skip_mod = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008219 break;
8220 }
8221 s = src + 1;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008222 if (*s == '<') // "#<99" uses v:oldfiles
Bram Moolenaard812df62008-11-09 12:46:09 +00008223 ++s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008224 i = (int)getdigits(&s);
Bram Moolenaarc312b8b2017-10-28 17:53:04 +02008225 if (s == src + 2 && src[1] == '-')
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008226 // just a minus sign, don't skip over it
Bram Moolenaarc312b8b2017-10-28 17:53:04 +02008227 s--;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008228 *usedlen = (int)(s - src); // length of what we expand
Bram Moolenaar071d4272004-06-13 20:20:40 +00008229
Bram Moolenaarc312b8b2017-10-28 17:53:04 +02008230 if (src[1] == '<' && i != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008231 {
Bram Moolenaard812df62008-11-09 12:46:09 +00008232 if (*usedlen < 2)
8233 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008234 // Should we give an error message for #<text?
Bram Moolenaard812df62008-11-09 12:46:09 +00008235 *usedlen = 1;
8236 return NULL;
8237 }
8238#ifdef FEAT_EVAL
8239 result = list_find_str(get_vim_var_list(VV_OLDFILES),
8240 (long)i);
8241 if (result == NULL)
8242 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008243 *errormsg = "";
Bram Moolenaard812df62008-11-09 12:46:09 +00008244 return NULL;
8245 }
8246#else
Bram Moolenaar8e481e82019-01-15 20:07:48 +01008247 *errormsg = _("E809: #< is not available without the +eval feature");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008248 return NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +00008249#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008250 }
8251 else
Bram Moolenaard812df62008-11-09 12:46:09 +00008252 {
Bram Moolenaarc312b8b2017-10-28 17:53:04 +02008253 if (i == 0 && src[1] == '<' && *usedlen > 1)
8254 *usedlen = 1;
Bram Moolenaard812df62008-11-09 12:46:09 +00008255 buf = buflist_findnr(i);
8256 if (buf == NULL)
8257 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008258 *errormsg = _("E194: No alternate file name to substitute for '#'");
Bram Moolenaard812df62008-11-09 12:46:09 +00008259 return NULL;
8260 }
8261 if (lnump != NULL)
8262 *lnump = ECMD_LAST;
8263 if (buf->b_fname == NULL)
8264 {
8265 result = (char_u *)"";
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008266 valid = 0; // Must have ":p:h" to be valid
Bram Moolenaard812df62008-11-09 12:46:09 +00008267 }
8268 else
Bram Moolenaar00136dc2018-07-25 21:19:13 +02008269 {
Bram Moolenaard812df62008-11-09 12:46:09 +00008270 result = buf->b_fname;
Bram Moolenaar00136dc2018-07-25 21:19:13 +02008271 tilde_file = STRCMP(result, "~") == 0;
8272 }
Bram Moolenaard812df62008-11-09 12:46:09 +00008273 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008274 break;
8275
8276#ifdef FEAT_SEARCHPATH
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008277 case SPEC_CFILE: // file name under cursor
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008278 result = file_name_at_cursor(FNAME_MESS|FNAME_HYP, 1L, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008279 if (result == NULL)
8280 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008281 *errormsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +00008282 return NULL;
8283 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008284 resultbuf = result; // remember allocated string
Bram Moolenaar071d4272004-06-13 20:20:40 +00008285 break;
8286#endif
8287
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008288 case SPEC_AFILE: // file name for autocommand
Bram Moolenaar071d4272004-06-13 20:20:40 +00008289 result = autocmd_fname;
Bram Moolenaarf6dad432008-09-18 19:29:58 +00008290 if (result != NULL && !autocmd_fname_full)
8291 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008292 // Still need to turn the fname into a full path. It is
8293 // postponed to avoid a delay when <afile> is not used.
Bram Moolenaarf6dad432008-09-18 19:29:58 +00008294 autocmd_fname_full = TRUE;
8295 result = FullName_save(autocmd_fname, FALSE);
8296 vim_free(autocmd_fname);
8297 autocmd_fname = result;
8298 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008299 if (result == NULL)
8300 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008301 *errormsg = _("E495: no autocommand file name to substitute for \"<afile>\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008302 return NULL;
8303 }
Bram Moolenaara0174af2008-01-02 20:08:25 +00008304 result = shorten_fname1(result);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008305 break;
8306
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008307 case SPEC_ABUF: // buffer number for autocommand
Bram Moolenaar071d4272004-06-13 20:20:40 +00008308 if (autocmd_bufnr <= 0)
8309 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008310 *errormsg = _("E496: no autocommand buffer number to substitute for \"<abuf>\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008311 return NULL;
8312 }
8313 sprintf((char *)strbuf, "%d", autocmd_bufnr);
8314 result = strbuf;
8315 break;
8316
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008317 case SPEC_AMATCH: // match name for autocommand
Bram Moolenaar071d4272004-06-13 20:20:40 +00008318 result = autocmd_match;
8319 if (result == NULL)
8320 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008321 *errormsg = _("E497: no autocommand match name to substitute for \"<amatch>\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008322 return NULL;
8323 }
8324 break;
8325
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008326 case SPEC_SFILE: // file name for ":so" command
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01008327 result = estack_sfile();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008328 if (result == NULL)
8329 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008330 *errormsg = _("E498: no :source file name to substitute for \"<sfile>\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008331 return NULL;
8332 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01008333 resultbuf = result; // remember allocated string
Bram Moolenaar071d4272004-06-13 20:20:40 +00008334 break;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008335
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008336 case SPEC_SLNUM: // line in file for ":so" command
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01008337 if (SOURCING_NAME == NULL || SOURCING_LNUM == 0)
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01008338 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008339 *errormsg = _("E842: no line number to use for \"<slnum>\"");
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01008340 return NULL;
8341 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01008342 sprintf((char *)strbuf, "%ld", SOURCING_LNUM);
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01008343 result = strbuf;
8344 break;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008345
8346#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008347 case SPEC_SFLNUM: // line in script file
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01008348 if (current_sctx.sc_lnum + SOURCING_LNUM == 0)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008349 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008350 *errormsg = _("E961: no line number to use for \"<sflnum>\"");
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008351 return NULL;
8352 }
8353 sprintf((char *)strbuf, "%ld",
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01008354 (long)(current_sctx.sc_lnum + SOURCING_LNUM));
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008355 result = strbuf;
8356 break;
8357#endif
8358
8359#ifdef FEAT_CLIENTSERVER
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008360 case SPEC_CLIENT: // Source of last submitted input
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008361 sprintf((char *)strbuf, PRINTF_HEX_LONG_U,
8362 (long_u)clientWindow);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008363 result = strbuf;
8364 break;
8365#endif
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008366
Bram Moolenaar9e0f6ec2017-05-16 09:36:54 +02008367 default:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008368 result = (char_u *)""; // avoid gcc warning
Bram Moolenaar9e0f6ec2017-05-16 09:36:54 +02008369 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008370 }
8371
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008372 resultlen = (int)STRLEN(result); // length of new string
8373 if (src[*usedlen] == '<') // remove the file name extension
Bram Moolenaar071d4272004-06-13 20:20:40 +00008374 {
8375 ++*usedlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008376 if ((s = vim_strrchr(result, '.')) != NULL && s >= gettail(result))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008377 resultlen = (int)(s - result);
8378 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008379 else if (!skip_mod)
8380 {
Bram Moolenaar00136dc2018-07-25 21:19:13 +02008381 valid |= modify_fname(src, tilde_file, usedlen, &result, &resultbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008382 &resultlen);
8383 if (result == NULL)
8384 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008385 *errormsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +00008386 return NULL;
8387 }
8388 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008389 }
8390
8391 if (resultlen == 0 || valid != VALID_HEAD + VALID_PATH)
8392 {
8393 if (valid != VALID_HEAD + VALID_PATH)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008394 // xgettext:no-c-format
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008395 *errormsg = _("E499: Empty file name for '%' or '#', only works with \":p:h\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008396 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008397 *errormsg = _("E500: Evaluates to an empty string");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008398 result = NULL;
8399 }
8400 else
8401 result = vim_strnsave(result, resultlen);
8402 vim_free(resultbuf);
8403 return result;
8404}
8405
8406/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008407 * Expand the <sfile> string in "arg".
8408 *
8409 * Returns an allocated string, or NULL for any error.
8410 */
8411 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008412expand_sfile(char_u *arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008413{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008414 char *errormsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008415 int len;
8416 char_u *result;
8417 char_u *newres;
8418 char_u *repl;
8419 int srclen;
8420 char_u *p;
8421
8422 result = vim_strsave(arg);
8423 if (result == NULL)
8424 return NULL;
8425
8426 for (p = result; *p; )
8427 {
8428 if (STRNCMP(p, "<sfile>", 7) != 0)
8429 ++p;
8430 else
8431 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008432 // replace "<sfile>" with the sourced file name, and do ":" stuff
Bram Moolenaar63b92542007-03-27 14:57:09 +00008433 repl = eval_vars(p, result, &srclen, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008434 if (errormsg != NULL)
8435 {
8436 if (*errormsg)
8437 emsg(errormsg);
8438 vim_free(result);
8439 return NULL;
8440 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008441 if (repl == NULL) // no match (cannot happen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008442 {
8443 p += srclen;
8444 continue;
8445 }
8446 len = (int)STRLEN(result) - srclen + (int)STRLEN(repl) + 1;
8447 newres = alloc(len);
8448 if (newres == NULL)
8449 {
8450 vim_free(repl);
8451 vim_free(result);
8452 return NULL;
8453 }
8454 mch_memmove(newres, result, (size_t)(p - result));
8455 STRCPY(newres + (p - result), repl);
8456 len = (int)STRLEN(newres);
8457 STRCAT(newres, p + srclen);
8458 vim_free(repl);
8459 vim_free(result);
8460 result = newres;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008461 p = newres + len; // continue after the match
Bram Moolenaar071d4272004-06-13 20:20:40 +00008462 }
8463 }
8464
8465 return result;
8466}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008467
Bram Moolenaar071d4272004-06-13 20:20:40 +00008468#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG) || defined(PROTO)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00008469/*
Bram Moolenaard9462e32011-04-11 21:35:11 +02008470 * Make a dialog message in "buff[DIALOG_MSG_SIZE]".
Bram Moolenaarb765d632005-06-07 21:00:02 +00008471 * "format" must contain "%s".
Bram Moolenaar9c13b352005-05-19 20:53:52 +00008472 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008473 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008474dialog_msg(char_u *buff, char *format, char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008475{
Bram Moolenaar071d4272004-06-13 20:20:40 +00008476 if (fname == NULL)
8477 fname = (char_u *)_("Untitled");
Bram Moolenaard9462e32011-04-11 21:35:11 +02008478 vim_snprintf((char *)buff, DIALOG_MSG_SIZE, format, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008479}
8480#endif
8481
8482/*
8483 * ":behave {mswin,xterm}"
8484 */
8485 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008486ex_behave(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008487{
8488 if (STRCMP(eap->arg, "mswin") == 0)
8489 {
8490 set_option_value((char_u *)"selection", 0L, (char_u *)"exclusive", 0);
8491 set_option_value((char_u *)"selectmode", 0L, (char_u *)"mouse,key", 0);
8492 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"popup", 0);
8493 set_option_value((char_u *)"keymodel", 0L,
8494 (char_u *)"startsel,stopsel", 0);
8495 }
8496 else if (STRCMP(eap->arg, "xterm") == 0)
8497 {
8498 set_option_value((char_u *)"selection", 0L, (char_u *)"inclusive", 0);
8499 set_option_value((char_u *)"selectmode", 0L, (char_u *)"", 0);
8500 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"extend", 0);
8501 set_option_value((char_u *)"keymodel", 0L, (char_u *)"", 0);
8502 }
8503 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008504 semsg(_(e_invarg2), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008505}
8506
Bram Moolenaar071d4272004-06-13 20:20:40 +00008507static int filetype_detect = FALSE;
8508static int filetype_plugin = FALSE;
8509static int filetype_indent = FALSE;
8510
8511/*
8512 * ":filetype [plugin] [indent] {on,off,detect}"
8513 * on: Load the filetype.vim file to install autocommands for file types.
8514 * off: Load the ftoff.vim file to remove all autocommands for file types.
8515 * plugin on: load filetype.vim and ftplugin.vim
8516 * plugin off: load ftplugof.vim
8517 * indent on: load filetype.vim and indent.vim
8518 * indent off: load indoff.vim
8519 */
8520 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008521ex_filetype(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008522{
8523 char_u *arg = eap->arg;
8524 int plugin = FALSE;
8525 int indent = FALSE;
8526
8527 if (*eap->arg == NUL)
8528 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008529 // Print current status.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008530 smsg("filetype detection:%s plugin:%s indent:%s",
Bram Moolenaar071d4272004-06-13 20:20:40 +00008531 filetype_detect ? "ON" : "OFF",
8532 filetype_plugin ? (filetype_detect ? "ON" : "(on)") : "OFF",
8533 filetype_indent ? (filetype_detect ? "ON" : "(on)") : "OFF");
8534 return;
8535 }
8536
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008537 // Accept "plugin" and "indent" in any order.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008538 for (;;)
8539 {
8540 if (STRNCMP(arg, "plugin", 6) == 0)
8541 {
8542 plugin = TRUE;
8543 arg = skipwhite(arg + 6);
8544 continue;
8545 }
8546 if (STRNCMP(arg, "indent", 6) == 0)
8547 {
8548 indent = TRUE;
8549 arg = skipwhite(arg + 6);
8550 continue;
8551 }
8552 break;
8553 }
8554 if (STRCMP(arg, "on") == 0 || STRCMP(arg, "detect") == 0)
8555 {
8556 if (*arg == 'o' || !filetype_detect)
8557 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01008558 source_runtime((char_u *)FILETYPE_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008559 filetype_detect = TRUE;
8560 if (plugin)
8561 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01008562 source_runtime((char_u *)FTPLUGIN_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008563 filetype_plugin = TRUE;
8564 }
8565 if (indent)
8566 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01008567 source_runtime((char_u *)INDENT_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008568 filetype_indent = TRUE;
8569 }
8570 }
8571 if (*arg == 'd')
8572 {
Bram Moolenaar1610d052016-06-09 22:53:01 +02008573 (void)do_doautocmd((char_u *)"filetypedetect BufRead", TRUE, NULL);
Bram Moolenaara3227e22006-03-08 21:32:40 +00008574 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008575 }
8576 }
8577 else if (STRCMP(arg, "off") == 0)
8578 {
8579 if (plugin || indent)
8580 {
8581 if (plugin)
8582 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01008583 source_runtime((char_u *)FTPLUGOF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008584 filetype_plugin = FALSE;
8585 }
8586 if (indent)
8587 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01008588 source_runtime((char_u *)INDOFF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008589 filetype_indent = FALSE;
8590 }
8591 }
8592 else
8593 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01008594 source_runtime((char_u *)FTOFF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008595 filetype_detect = FALSE;
8596 }
8597 }
8598 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008599 semsg(_(e_invarg2), arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008600}
8601
8602/*
Bram Moolenaar3e545692017-06-04 19:00:32 +02008603 * ":setfiletype [FALLBACK] {name}"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008604 */
8605 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008606ex_setfiletype(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008607{
8608 if (!did_filetype)
Bram Moolenaar3e545692017-06-04 19:00:32 +02008609 {
8610 char_u *arg = eap->arg;
8611
8612 if (STRNCMP(arg, "FALLBACK ", 9) == 0)
8613 arg += 9;
8614
8615 set_option_value((char_u *)"filetype", 0L, arg, OPT_LOCAL);
8616 if (arg != eap->arg)
8617 did_filetype = FALSE;
8618 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008619}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008620
Bram Moolenaar071d4272004-06-13 20:20:40 +00008621 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008622ex_digraphs(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008623{
8624#ifdef FEAT_DIGRAPHS
8625 if (*eap->arg != NUL)
8626 putdigraph(eap->arg);
8627 else
Bram Moolenaareae8ae12018-12-14 18:53:02 +01008628 listdigraphs(eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008629#else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008630 emsg(_("E196: No digraphs in this version"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008631#endif
8632}
8633
Bram Moolenaar451fc7b2018-04-27 22:53:07 +02008634#if defined(FEAT_SEARCH_EXTRA) || defined(PROTO)
8635 void
8636set_no_hlsearch(int flag)
8637{
8638 no_hlsearch = flag;
8639# ifdef FEAT_EVAL
8640 set_vim_var_nr(VV_HLSEARCH, !no_hlsearch && p_hls);
8641# endif
8642}
8643
Bram Moolenaar071d4272004-06-13 20:20:40 +00008644/*
8645 * ":nohlsearch"
8646 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008647 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008648ex_nohlsearch(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008649{
Bram Moolenaar451fc7b2018-04-27 22:53:07 +02008650 set_no_hlsearch(TRUE);
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00008651 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008652}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008653#endif
8654
8655#ifdef FEAT_CRYPT
8656/*
8657 * ":X": Get crypt key
8658 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008659 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008660ex_X(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008661{
Bram Moolenaar3a0c9082014-11-12 15:15:42 +01008662 crypt_check_current_method();
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02008663 (void)crypt_get_key(TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008664}
8665#endif
8666
8667#ifdef FEAT_FOLDING
8668 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008669ex_fold(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008670{
8671 if (foldManualAllowed(TRUE))
8672 foldCreate(eap->line1, eap->line2);
8673}
8674
8675 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008676ex_foldopen(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008677{
8678 opFoldRange(eap->line1, eap->line2, eap->cmdidx == CMD_foldopen,
8679 eap->forceit, FALSE);
8680}
8681
8682 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008683ex_folddo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008684{
8685 linenr_T lnum;
8686
Bram Moolenaar4facea32019-10-12 20:17:40 +02008687# ifdef FEAT_CLIPBOARD
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02008688 start_global_changes();
Bram Moolenaar4facea32019-10-12 20:17:40 +02008689# endif
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02008690
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008691 // First set the marks for all lines closed/open.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008692 for (lnum = eap->line1; lnum <= eap->line2; ++lnum)
8693 if (hasFolding(lnum, NULL, NULL) == (eap->cmdidx == CMD_folddoclosed))
8694 ml_setmarked(lnum);
8695
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008696 // Execute the command on the marked lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008697 global_exe(eap->arg);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008698 ml_clearmarked(); // clear rest of the marks
Bram Moolenaar4facea32019-10-12 20:17:40 +02008699# ifdef FEAT_CLIPBOARD
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02008700 end_global_changes();
Bram Moolenaar4facea32019-10-12 20:17:40 +02008701# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008702}
8703#endif
Bram Moolenaarf5291f32017-09-14 22:55:37 +02008704
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008705#if defined(FEAT_QUICKFIX) || defined(PROTO)
Bram Moolenaar39665952018-08-15 20:59:48 +02008706/*
8707 * Returns TRUE if the supplied Ex cmdidx is for a location list command
8708 * instead of a quickfix command.
8709 */
8710 int
8711is_loclist_cmd(int cmdidx)
8712{
Bram Moolenaar74c8be22018-08-23 22:51:40 +02008713 if (cmdidx < 0 || cmdidx >= CMD_SIZE)
Bram Moolenaar39665952018-08-15 20:59:48 +02008714 return FALSE;
8715 return cmdnames[cmdidx].cmd_name[0] == 'l';
8716}
8717#endif
8718
Bram Moolenaar4facea32019-10-12 20:17:40 +02008719#if defined(FEAT_TIMERS) || defined(PROTO)
Bram Moolenaarf5291f32017-09-14 22:55:37 +02008720 int
8721get_pressedreturn(void)
8722{
8723 return ex_pressedreturn;
8724}
8725
8726 void
8727set_pressedreturn(int val)
8728{
8729 ex_pressedreturn = val;
8730}
8731#endif