blob: b78e5ac6669d734409942c93a9ea845e9807d78d [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
272# define ex_const ex_ni
273# define ex_continue ex_ni
274# define ex_debug ex_ni
275# define ex_debuggreedy ex_ni
276# define ex_delfunction ex_ni
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100277# define ex_disassemble ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000278# define ex_echo ex_ni
279# define ex_echohl ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000280# define ex_else ex_ni
Bram Moolenaare4ce8252019-08-04 15:30:16 +0200281# define ex_endfunction ex_ni
282# define ex_endif ex_ni
283# define ex_endtry ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000284# define ex_endwhile ex_ni
Bram Moolenaare4ce8252019-08-04 15:30:16 +0200285# define ex_eval ex_ni
286# define ex_execute ex_ni
287# define ex_finally ex_ni
288# define ex_finish ex_ni
289# define ex_function ex_ni
290# define ex_if ex_ni
291# define ex_let ex_ni
292# define ex_lockvar ex_ni
293# define ex_oldfiles ex_ni
294# define ex_options ex_ni
295# define ex_packadd ex_ni
296# define ex_packloadall ex_ni
297# define ex_return ex_ni
298# define ex_scriptnames ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000299# define ex_throw ex_ni
300# define ex_try ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000301# define ex_unlet ex_ni
Bram Moolenaar65c1b012005-01-31 19:02:28 +0000302# define ex_unlockvar ex_ni
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100303# define ex_vim9script ex_ni
Bram Moolenaare4ce8252019-08-04 15:30:16 +0200304# define ex_while ex_ni
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100305# define ex_import ex_ni
306# define ex_export ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000307#endif
Bram Moolenaar84538072019-07-28 14:15:42 +0200308#ifndef FEAT_SESSION
Bram Moolenaar071d4272004-06-13 20:20:40 +0000309# define ex_loadview ex_ni
310#endif
Bram Moolenaardefa0672019-07-21 19:25:37 +0200311#ifndef FEAT_VIMINFO
Bram Moolenaar071d4272004-06-13 20:20:40 +0000312# define ex_viminfo ex_ni
313#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100314static void ex_behave(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100315static void ex_filetype(exarg_T *eap);
316static void ex_setfiletype(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000317#ifndef FEAT_DIFF
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000318# define ex_diffoff ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000319# define ex_diffpatch ex_ni
320# define ex_diffgetput ex_ni
321# define ex_diffsplit ex_ni
322# define ex_diffthis ex_ni
323# define ex_diffupdate ex_ni
324#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100325static void ex_digraphs(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000326#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100327static void ex_nohlsearch(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000328#else
329# define ex_nohlsearch ex_ni
330# define ex_match ex_ni
331#endif
332#ifdef FEAT_CRYPT
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100333static void ex_X(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000334#else
335# define ex_X ex_ni
336#endif
337#ifdef FEAT_FOLDING
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100338static void ex_fold(exarg_T *eap);
339static void ex_foldopen(exarg_T *eap);
340static void ex_folddo(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000341#else
342# define ex_fold ex_ni
343# define ex_foldopen ex_ni
344# define ex_folddo ex_ni
345#endif
Bram Moolenaar13505972019-01-24 15:04:48 +0100346#if !(defined(HAVE_LOCALE_H) || defined(X_LOCALE))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000347# define ex_language ex_ni
348#endif
349#ifndef FEAT_SIGNS
350# define ex_sign ex_ni
351#endif
Bram Moolenaar009b2592004-10-24 19:18:58 +0000352#ifndef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200353# define ex_nbclose ex_ni
Bram Moolenaar009b2592004-10-24 19:18:58 +0000354# define ex_nbkey ex_ni
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200355# define ex_nbstart ex_ni
Bram Moolenaar009b2592004-10-24 19:18:58 +0000356#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000357
Bram Moolenaar071d4272004-06-13 20:20:40 +0000358#ifndef FEAT_JUMPLIST
359# define ex_jumps ex_ni
Bram Moolenaar2d358992016-06-12 21:20:54 +0200360# define ex_clearjumps ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000361# define ex_changes ex_ni
362#endif
363
Bram Moolenaar05159a02005-02-26 23:04:13 +0000364#ifndef FEAT_PROFILE
365# define ex_profile ex_ni
366#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200367#ifndef FEAT_TERMINAL
368# define ex_terminal ex_ni
369#endif
Bram Moolenaard4aa83a2019-05-09 18:59:31 +0200370#if !defined(FEAT_X11) || !defined(FEAT_XCLIPBOARD)
371# define ex_xrestore ex_ni
372#endif
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +0100373#if !defined(FEAT_PROP_POPUP)
Bram Moolenaar682725c2019-05-25 20:10:37 +0200374# define ex_popupclear ex_ni
375#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +0000376
Bram Moolenaar071d4272004-06-13 20:20:40 +0000377/*
378 * Declare cmdnames[].
379 */
380#define DO_DECLARE_EXCMD
381#include "ex_cmds.h"
Bram Moolenaar6de5e122017-04-20 21:55:44 +0200382#include "ex_cmdidxs.h"
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +0100383
Bram Moolenaar071d4272004-06-13 20:20:40 +0000384static char_u dollar_command[2] = {'$', 0};
385
386
387#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100388// Struct for storing a line inside a while/for loop
Bram Moolenaar071d4272004-06-13 20:20:40 +0000389typedef struct
390{
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100391 char_u *line; // command line
392 linenr_T lnum; // sourcing_lnum of the line
Bram Moolenaar071d4272004-06-13 20:20:40 +0000393} wcmd_T;
394
395/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000396 * Structure used to store info for line position in a while or for loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000397 * This is required, because do_one_cmd() may invoke ex_function(), which
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000398 * reads more lines that may come from the while/for loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000399 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000400struct loop_cookie
Bram Moolenaar071d4272004-06-13 20:20:40 +0000401{
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100402 garray_T *lines_gap; // growarray with line info
403 int current_line; // last read line from growarray
404 int repeating; // TRUE when looping a second time
405 // When "repeating" is FALSE use "getline" and "cookie" to get lines
Bram Moolenaare96a2492019-06-25 04:12:16 +0200406 char_u *(*getline)(int, void *, int, int);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000407 void *cookie;
408};
409
Bram Moolenaare96a2492019-06-25 04:12:16 +0200410static char_u *get_loop_line(int c, void *cookie, int indent, int do_concat);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100411static int store_loop_line(garray_T *gap, char_u *line);
412static void free_cmdlines(garray_T *gap);
Bram Moolenaared203462004-06-16 11:19:22 +0000413
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100414// Struct to save a few things while debugging. Used in do_cmdline() only.
Bram Moolenaared203462004-06-16 11:19:22 +0000415struct dbg_stuff
416{
417 int trylevel;
418 int force_abort;
419 except_T *caught_stack;
420 char_u *vv_exception;
421 char_u *vv_throwpoint;
422 int did_emsg;
423 int got_int;
424 int did_throw;
425 int need_rethrow;
426 int check_cstack;
427 except_T *current_exception;
428};
429
Bram Moolenaared203462004-06-16 11:19:22 +0000430 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100431save_dbg_stuff(struct dbg_stuff *dsp)
Bram Moolenaared203462004-06-16 11:19:22 +0000432{
433 dsp->trylevel = trylevel; trylevel = 0;
434 dsp->force_abort = force_abort; force_abort = FALSE;
435 dsp->caught_stack = caught_stack; caught_stack = NULL;
436 dsp->vv_exception = v_exception(NULL);
437 dsp->vv_throwpoint = v_throwpoint(NULL);
438
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100439 // Necessary for debugging an inactive ":catch", ":finally", ":endtry"
Bram Moolenaared203462004-06-16 11:19:22 +0000440 dsp->did_emsg = did_emsg; did_emsg = FALSE;
441 dsp->got_int = got_int; got_int = FALSE;
442 dsp->did_throw = did_throw; did_throw = FALSE;
443 dsp->need_rethrow = need_rethrow; need_rethrow = FALSE;
444 dsp->check_cstack = check_cstack; check_cstack = FALSE;
445 dsp->current_exception = current_exception; current_exception = NULL;
446}
447
448 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100449restore_dbg_stuff(struct dbg_stuff *dsp)
Bram Moolenaared203462004-06-16 11:19:22 +0000450{
451 suppress_errthrow = FALSE;
452 trylevel = dsp->trylevel;
453 force_abort = dsp->force_abort;
454 caught_stack = dsp->caught_stack;
455 (void)v_exception(dsp->vv_exception);
456 (void)v_throwpoint(dsp->vv_throwpoint);
457 did_emsg = dsp->did_emsg;
458 got_int = dsp->got_int;
459 did_throw = dsp->did_throw;
460 need_rethrow = dsp->need_rethrow;
461 check_cstack = dsp->check_cstack;
462 current_exception = dsp->current_exception;
463}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000464#endif
465
Bram Moolenaar071d4272004-06-13 20:20:40 +0000466/*
467 * do_exmode(): Repeatedly get commands for the "Ex" mode, until the ":vi"
468 * command is given.
469 */
470 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100471do_exmode(
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100472 int improved) // TRUE for "improved Ex" mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000473{
474 int save_msg_scroll;
475 int prev_msg_row;
476 linenr_T prev_line;
Bram Moolenaar79518e22017-02-17 16:31:35 +0100477 varnumber_T changedtick;
Bram Moolenaardf177f62005-02-22 08:39:57 +0000478
479 if (improved)
480 exmode_active = EXMODE_VIM;
481 else
482 exmode_active = EXMODE_NORMAL;
483 State = NORMAL;
484
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100485 // When using ":global /pat/ visual" and then "Q" we return to continue
486 // the :global command.
Bram Moolenaardf177f62005-02-22 08:39:57 +0000487 if (global_busy)
488 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000489
490 save_msg_scroll = msg_scroll;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100491 ++RedrawingDisabled; // don't redisplay the window
492 ++no_wait_return; // don't wait for return
Bram Moolenaar071d4272004-06-13 20:20:40 +0000493#ifdef FEAT_GUI
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100494 // Ignore scrollbar and mouse events in Ex mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000495 ++hold_gui_events;
496#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000497
Bram Moolenaar32526b32019-01-19 17:43:09 +0100498 msg(_("Entering Ex mode. Type \"visual\" to go to Normal mode."));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000499 while (exmode_active)
500 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100501 // Check for a ":normal" command and no more characters left.
Bram Moolenaar7c626922005-02-07 22:01:03 +0000502 if (ex_normal_busy > 0 && typebuf.tb_len == 0)
503 {
504 exmode_active = FALSE;
505 break;
506 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000507 msg_scroll = TRUE;
508 need_wait_return = FALSE;
509 ex_pressedreturn = FALSE;
510 ex_no_reprint = FALSE;
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100511 changedtick = CHANGEDTICK(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000512 prev_msg_row = msg_row;
513 prev_line = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000514 if (improved)
515 {
516 cmdline_row = msg_row;
517 do_cmdline(NULL, getexline, NULL, 0);
518 }
519 else
520 do_cmdline(NULL, getexmodeline, NULL, DOCMD_NOWAIT);
521 lines_left = Rows - 1;
522
Bram Moolenaardf177f62005-02-22 08:39:57 +0000523 if ((prev_line != curwin->w_cursor.lnum
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100524 || changedtick != CHANGEDTICK(curbuf)) && !ex_no_reprint)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000525 {
Bram Moolenaardf177f62005-02-22 08:39:57 +0000526 if (curbuf->b_ml.ml_flags & ML_EMPTY)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100527 emsg(_(e_emptybuf));
Bram Moolenaardf177f62005-02-22 08:39:57 +0000528 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000529 {
Bram Moolenaardf177f62005-02-22 08:39:57 +0000530 if (ex_pressedreturn)
531 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100532 // go up one line, to overwrite the ":<CR>" line, so the
533 // output doesn't contain empty lines.
Bram Moolenaardf177f62005-02-22 08:39:57 +0000534 msg_row = prev_msg_row;
535 if (prev_msg_row == Rows - 1)
536 msg_row--;
537 }
538 msg_col = 0;
539 print_line_no_prefix(curwin->w_cursor.lnum, FALSE, FALSE);
540 msg_clr_eos();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000541 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000542 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100543 else if (ex_pressedreturn && !ex_no_reprint) // must be at EOF
Bram Moolenaardf177f62005-02-22 08:39:57 +0000544 {
545 if (curbuf->b_ml.ml_flags & ML_EMPTY)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100546 emsg(_(e_emptybuf));
Bram Moolenaardf177f62005-02-22 08:39:57 +0000547 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100548 emsg(_("E501: At end-of-file"));
Bram Moolenaardf177f62005-02-22 08:39:57 +0000549 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000550 }
551
552#ifdef FEAT_GUI
553 --hold_gui_events;
554#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000555 --RedrawingDisabled;
556 --no_wait_return;
557 update_screen(CLEAR);
558 need_wait_return = FALSE;
559 msg_scroll = save_msg_scroll;
560}
561
562/*
Bram Moolenaar4facea32019-10-12 20:17:40 +0200563 * Print the executed command for when 'verbose' is set.
564 * When "lnum" is 0 only print the command.
565 */
566 static void
567msg_verbose_cmd(linenr_T lnum, char_u *cmd)
568{
569 ++no_wait_return;
570 verbose_enter_scroll();
571
572 if (lnum == 0)
573 smsg(_("Executing: %s"), cmd);
574 else
575 smsg(_("line %ld: %s"), (long)lnum, cmd);
576 if (msg_silent == 0)
577 msg_puts("\n"); // don't overwrite this
578
579 verbose_leave_scroll();
580 --no_wait_return;
581}
582
583/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000584 * Execute a simple command line. Used for translated commands like "*".
585 */
586 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100587do_cmdline_cmd(char_u *cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000588{
589 return do_cmdline(cmd, NULL, NULL,
590 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
591}
592
593/*
594 * do_cmdline(): execute one Ex command line
595 *
596 * 1. Execute "cmdline" when it is not NULL.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100597 * If "cmdline" is NULL, or more lines are needed, fgetline() is used.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000598 * 2. Split up in parts separated with '|'.
599 *
600 * This function can be called recursively!
601 *
602 * flags:
603 * DOCMD_VERBOSE - The command will be included in the error message.
604 * DOCMD_NOWAIT - Don't call wait_return() and friends.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100605 * DOCMD_REPEAT - Repeat execution until fgetline() returns NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000606 * DOCMD_KEYTYPED - Don't reset KeyTyped.
607 * DOCMD_EXCRESET - Reset the exception environment (used for debugging).
608 * DOCMD_KEEPLINE - Store first typed line (for repeating with ".").
609 *
610 * return FAIL if cmdline could not be executed, OK otherwise
611 */
612 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100613do_cmdline(
614 char_u *cmdline,
Bram Moolenaare96a2492019-06-25 04:12:16 +0200615 char_u *(*fgetline)(int, void *, int, int),
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100616 void *cookie, // argument for fgetline()
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100617 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000618{
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100619 char_u *next_cmdline; // next cmd to execute
620 char_u *cmdline_copy = NULL; // copy of cmd line
621 int used_getline = FALSE; // used "fgetline" to obtain command
622 static int recursive = 0; // recursive depth
Bram Moolenaar071d4272004-06-13 20:20:40 +0000623 int msg_didout_before_start = 0;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100624 int count = 0; // line number count
625 int did_inc = FALSE; // incremented RedrawingDisabled
Bram Moolenaar071d4272004-06-13 20:20:40 +0000626 int retval = OK;
627#ifdef FEAT_EVAL
Bram Moolenaarddef1292019-12-16 17:10:33 +0100628 cstack_T cstack; // conditional stack
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100629 garray_T lines_ga; // keep lines for ":while"/":for"
630 int current_line = 0; // active line in lines_ga
631 char_u *fname = NULL; // function or script name
632 linenr_T *breakpoint = NULL; // ptr to breakpoint field in cookie
633 int *dbg_tick = NULL; // ptr to dbg_tick field in cookie
634 struct dbg_stuff debug_saved; // saved things for debug mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000635 int initial_trylevel;
636 struct msglist **saved_msg_list = NULL;
637 struct msglist *private_msg_list;
638
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100639 // "fgetline" and "cookie" passed to do_one_cmd()
Bram Moolenaare96a2492019-06-25 04:12:16 +0200640 char_u *(*cmd_getline)(int, void *, int, int);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000641 void *cmd_cookie;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000642 struct loop_cookie cmd_loop_cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000643 void *real_cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +0000644 int getline_is_func;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000645#else
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100646# define cmd_getline fgetline
Bram Moolenaar071d4272004-06-13 20:20:40 +0000647# define cmd_cookie cookie
648#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100649 static int call_depth = 0; // recursiveness
Bram Moolenaare31ee862020-01-07 20:59:34 +0100650 ESTACK_CHECK_DECLARATION
Bram Moolenaar071d4272004-06-13 20:20:40 +0000651
652#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100653 // For every pair of do_cmdline()/do_one_cmd() calls, use an extra memory
654 // location for storing error messages to be converted to an exception.
655 // This ensures that the do_errthrow() call in do_one_cmd() does not
656 // combine the messages stored by an earlier invocation of do_one_cmd()
657 // with the command name of the later one. This would happen when
658 // BufWritePost autocommands are executed after a write error.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000659 saved_msg_list = msg_list;
660 msg_list = &private_msg_list;
661 private_msg_list = NULL;
662#endif
663
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100664 // It's possible to create an endless loop with ":execute", catch that
665 // here. The value of 200 allows nested function calls, ":source", etc.
666 // Allow 200 or 'maxfuncdepth', whatever is larger.
Bram Moolenaarb094ff42017-01-02 16:16:39 +0100667 if (call_depth >= 200
668#ifdef FEAT_EVAL
669 && call_depth >= p_mfd
670#endif
671 )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000672 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100673 emsg(_("E169: Command too recursive"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000674#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100675 // When converting to an exception, we do not include the command name
676 // since this is not an error of the specific command.
Bram Moolenaarddef1292019-12-16 17:10:33 +0100677 do_errthrow((cstack_T *)NULL, (char_u *)NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000678 msg_list = saved_msg_list;
679#endif
680 return FAIL;
681 }
682 ++call_depth;
683
684#ifdef FEAT_EVAL
685 cstack.cs_idx = -1;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000686 cstack.cs_looplevel = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000687 cstack.cs_trylevel = 0;
688 cstack.cs_emsg_silent_list = NULL;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000689 cstack.cs_lflags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000690 ga_init2(&lines_ga, (int)sizeof(wcmd_T), 10);
691
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100692 real_cookie = getline_cookie(fgetline, cookie);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000693
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100694 // Inside a function use a higher nesting level.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100695 getline_is_func = getline_equal(fgetline, cookie, get_func_line);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000696 if (getline_is_func && ex_nesting_level == func_level(real_cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000697 ++ex_nesting_level;
698
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100699 // Get the function or script name and the address where the next breakpoint
700 // line and the debug tick for a function or script are stored.
Bram Moolenaar05159a02005-02-26 23:04:13 +0000701 if (getline_is_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000702 {
703 fname = func_name(real_cookie);
704 breakpoint = func_breakpoint(real_cookie);
705 dbg_tick = func_dbg_tick(real_cookie);
706 }
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100707 else if (getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000708 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100709 fname = SOURCING_NAME;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000710 breakpoint = source_breakpoint(real_cookie);
711 dbg_tick = source_dbg_tick(real_cookie);
712 }
713
714 /*
715 * Initialize "force_abort" and "suppress_errthrow" at the top level.
716 */
717 if (!recursive)
718 {
719 force_abort = FALSE;
720 suppress_errthrow = FALSE;
721 }
722
723 /*
724 * If requested, store and reset the global values controlling the
Bram Moolenaar89d40322006-08-29 15:30:07 +0000725 * exception handling (used when debugging). Otherwise clear it to avoid
726 * a bogus compiler warning when the optimizer uses inline functions...
Bram Moolenaar071d4272004-06-13 20:20:40 +0000727 */
Bram Moolenaarb4872942006-05-13 10:32:52 +0000728 if (flags & DOCMD_EXCRESET)
Bram Moolenaared203462004-06-16 11:19:22 +0000729 save_dbg_stuff(&debug_saved);
Bram Moolenaar89d40322006-08-29 15:30:07 +0000730 else
Bram Moolenaar69b67f72015-09-25 16:59:47 +0200731 vim_memset(&debug_saved, 0, sizeof(debug_saved));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000732
733 initial_trylevel = trylevel;
734
735 /*
736 * "did_throw" will be set to TRUE when an exception is being thrown.
737 */
738 did_throw = FALSE;
739#endif
740 /*
741 * "did_emsg" will be set to TRUE when emsg() is used, in which case we
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000742 * cancel the whole command line, and any if/endif or loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000743 * If force_abort is set, we cancel everything.
744 */
745 did_emsg = FALSE;
746
747 /*
748 * KeyTyped is only set when calling vgetc(). Reset it here when not
749 * calling vgetc() (sourced command lines).
750 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100751 if (!(flags & DOCMD_KEYTYPED)
752 && !getline_equal(fgetline, cookie, getexline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000753 KeyTyped = FALSE;
754
755 /*
756 * Continue executing command lines:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000757 * - when inside an ":if", ":while" or ":for"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000758 * - for multiple commands on one line, separated with '|'
759 * - when repeating until there are no more lines (for ":source")
760 */
761 next_cmdline = cmdline;
762 do
763 {
Bram Moolenaar05159a02005-02-26 23:04:13 +0000764#ifdef FEAT_EVAL
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100765 getline_is_func = getline_equal(fgetline, cookie, get_func_line);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000766#endif
767
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100768 // stop skipping cmds for an error msg after all endif/while/for
Bram Moolenaar071d4272004-06-13 20:20:40 +0000769 if (next_cmdline == NULL
770#ifdef FEAT_EVAL
771 && !force_abort
772 && cstack.cs_idx < 0
Bram Moolenaar05159a02005-02-26 23:04:13 +0000773 && !(getline_is_func && func_has_abort(real_cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000774#endif
775 )
776 did_emsg = FALSE;
777
778 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000779 * 1. If repeating a line in a loop, get a line from lines_ga.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100780 * 2. If no line given: Get an allocated line with fgetline().
Bram Moolenaar071d4272004-06-13 20:20:40 +0000781 * 3. If a line is given: Make a copy, so we can mess with it.
782 */
783
784#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100785 // 1. If repeating, get a previous line from lines_ga.
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000786 if (cstack.cs_looplevel > 0 && current_line < lines_ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000787 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100788 // Each '|' separated command is stored separately in lines_ga, to
789 // be able to jump to it. Don't use next_cmdline now.
Bram Moolenaard23a8232018-02-10 18:45:26 +0100790 VIM_CLEAR(cmdline_copy);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000791
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100792 // Check if a function has returned or, unless it has an unclosed
793 // try conditional, aborted.
Bram Moolenaar05159a02005-02-26 23:04:13 +0000794 if (getline_is_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000795 {
Bram Moolenaar05159a02005-02-26 23:04:13 +0000796# ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000797 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +0000798 func_line_end(real_cookie);
799# endif
800 if (func_has_ended(real_cookie))
801 {
802 retval = FAIL;
803 break;
804 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000805 }
Bram Moolenaar05159a02005-02-26 23:04:13 +0000806#ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000807 else if (do_profiling == PROF_YES
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100808 && getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar05159a02005-02-26 23:04:13 +0000809 script_line_end();
810#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000811
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100812 // Check if a sourced file hit a ":finish" command.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100813 if (source_finished(fgetline, cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000814 {
815 retval = FAIL;
816 break;
817 }
818
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100819 // If breakpoints have been added/deleted need to check for it.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000820 if (breakpoint != NULL && dbg_tick != NULL
821 && *dbg_tick != debug_tick)
822 {
823 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100824 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100825 fname, SOURCING_LNUM);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000826 *dbg_tick = debug_tick;
827 }
828
829 next_cmdline = ((wcmd_T *)(lines_ga.ga_data))[current_line].line;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100830 SOURCING_LNUM = ((wcmd_T *)(lines_ga.ga_data))[current_line].lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000831
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100832 // Did we encounter a breakpoint?
Bram Moolenaar071d4272004-06-13 20:20:40 +0000833 if (breakpoint != NULL && *breakpoint != 0
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100834 && *breakpoint <= SOURCING_LNUM)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000835 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100836 dbg_breakpoint(fname, SOURCING_LNUM);
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100837 // Find next breakpoint.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000838 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100839 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100840 fname, SOURCING_LNUM);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000841 *dbg_tick = debug_tick;
842 }
Bram Moolenaar05159a02005-02-26 23:04:13 +0000843# ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000844 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +0000845 {
846 if (getline_is_func)
847 func_line_start(real_cookie);
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100848 else if (getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar05159a02005-02-26 23:04:13 +0000849 script_line_start();
850 }
851# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000852 }
853
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000854 if (cstack.cs_looplevel > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000855 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100856 // Inside a while/for loop we need to store the lines and use them
857 // again. Pass a different "fgetline" function to do_one_cmd()
858 // below, so that it stores lines in or reads them from
859 // "lines_ga". Makes it possible to define a function inside a
860 // while/for loop.
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000861 cmd_getline = get_loop_line;
862 cmd_cookie = (void *)&cmd_loop_cookie;
863 cmd_loop_cookie.lines_gap = &lines_ga;
864 cmd_loop_cookie.current_line = current_line;
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100865 cmd_loop_cookie.getline = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000866 cmd_loop_cookie.cookie = cookie;
867 cmd_loop_cookie.repeating = (current_line < lines_ga.ga_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000868 }
869 else
870 {
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100871 cmd_getline = fgetline;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000872 cmd_cookie = cookie;
873 }
874#endif
875
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100876 // 2. If no line given, get an allocated line with fgetline().
Bram Moolenaar071d4272004-06-13 20:20:40 +0000877 if (next_cmdline == NULL)
878 {
879 /*
880 * Need to set msg_didout for the first line after an ":if",
881 * otherwise the ":if" will be overwritten.
882 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100883 if (count == 1 && getline_equal(fgetline, cookie, getexline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000884 msg_didout = TRUE;
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100885 if (fgetline == NULL || (next_cmdline = fgetline(':', cookie,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000886#ifdef FEAT_EVAL
887 cstack.cs_idx < 0 ? 0 : (cstack.cs_idx + 1) * 2
888#else
889 0
890#endif
Bram Moolenaare96a2492019-06-25 04:12:16 +0200891 , TRUE)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000892 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100893 // Don't call wait_return for aborted command line. The NULL
894 // returned for the end of a sourced file or executed function
895 // doesn't do this.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000896 if (KeyTyped && !(flags & DOCMD_REPEAT))
897 need_wait_return = FALSE;
898 retval = FAIL;
899 break;
900 }
901 used_getline = TRUE;
902
903 /*
904 * Keep the first typed line. Clear it when more lines are typed.
905 */
906 if (flags & DOCMD_KEEPLINE)
907 {
908 vim_free(repeat_cmdline);
909 if (count == 0)
910 repeat_cmdline = vim_strsave(next_cmdline);
911 else
912 repeat_cmdline = NULL;
913 }
914 }
915
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100916 // 3. Make a copy of the command so we can mess with it.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000917 else if (cmdline_copy == NULL)
918 {
919 next_cmdline = vim_strsave(next_cmdline);
920 if (next_cmdline == NULL)
921 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100922 emsg(_(e_outofmem));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000923 retval = FAIL;
924 break;
925 }
926 }
927 cmdline_copy = next_cmdline;
928
929#ifdef FEAT_EVAL
930 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000931 * Save the current line when inside a ":while" or ":for", and when
932 * the command looks like a ":while" or ":for", because we may need it
933 * later. When there is a '|' and another command, it is stored
934 * separately, because we need to be able to jump back to it from an
935 * :endwhile/:endfor.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000936 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000937 if (current_line == lines_ga.ga_len
938 && (cstack.cs_looplevel || has_loop_cmd(next_cmdline)))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000939 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000940 if (store_loop_line(&lines_ga, next_cmdline) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000941 {
942 retval = FAIL;
943 break;
944 }
945 }
946 did_endif = FALSE;
947#endif
948
949 if (count++ == 0)
950 {
951 /*
952 * All output from the commands is put below each other, without
953 * waiting for a return. Don't do this when executing commands
954 * from a script or when being called recursive (e.g. for ":e
955 * +command file").
956 */
957 if (!(flags & DOCMD_NOWAIT) && !recursive)
958 {
959 msg_didout_before_start = msg_didout;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100960 msg_didany = FALSE; // no output yet
Bram Moolenaar071d4272004-06-13 20:20:40 +0000961 msg_start();
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100962 msg_scroll = TRUE; // put messages below each other
963 ++no_wait_return; // don't wait for return until finished
Bram Moolenaar071d4272004-06-13 20:20:40 +0000964 ++RedrawingDisabled;
965 did_inc = TRUE;
966 }
967 }
968
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100969 if (p_verbose >= 15 && SOURCING_NAME != NULL)
970 msg_verbose_cmd(SOURCING_LNUM, cmdline_copy);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000971
972 /*
973 * 2. Execute one '|' separated command.
974 * do_one_cmd() will return NULL if there is no trailing '|'.
975 * "cmdline_copy" can change, e.g. for '%' and '#' expansion.
976 */
977 ++recursive;
978 next_cmdline = do_one_cmd(&cmdline_copy, flags & DOCMD_VERBOSE,
979#ifdef FEAT_EVAL
980 &cstack,
981#endif
982 cmd_getline, cmd_cookie);
983 --recursive;
984
985#ifdef FEAT_EVAL
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000986 if (cmd_cookie == (void *)&cmd_loop_cookie)
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100987 // Use "current_line" from "cmd_loop_cookie", it may have been
988 // incremented when defining a function.
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000989 current_line = cmd_loop_cookie.current_line;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000990#endif
991
992 if (next_cmdline == NULL)
993 {
Bram Moolenaard23a8232018-02-10 18:45:26 +0100994 VIM_CLEAR(cmdline_copy);
Bram Moolenaard7663c22019-08-06 21:59:57 +0200995
Bram Moolenaar071d4272004-06-13 20:20:40 +0000996 /*
997 * If the command was typed, remember it for the ':' register.
998 * Do this AFTER executing the command to make :@: work.
999 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001000 if (getline_equal(fgetline, cookie, getexline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001001 && new_last_cmdline != NULL)
1002 {
1003 vim_free(last_cmdline);
1004 last_cmdline = new_last_cmdline;
1005 new_last_cmdline = NULL;
1006 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001007 }
1008 else
1009 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001010 // need to copy the command after the '|' to cmdline_copy, for the
1011 // next do_one_cmd()
Bram Moolenaara7241f52008-06-24 20:39:31 +00001012 STRMOVE(cmdline_copy, next_cmdline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001013 next_cmdline = cmdline_copy;
1014 }
1015
1016
1017#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001018 // reset did_emsg for a function that is not aborted by an error
Bram Moolenaar071d4272004-06-13 20:20:40 +00001019 if (did_emsg && !force_abort
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001020 && getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001021 && !func_has_abort(real_cookie))
1022 did_emsg = FALSE;
1023
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001024 if (cstack.cs_looplevel > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001025 {
1026 ++current_line;
1027
1028 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001029 * An ":endwhile", ":endfor" and ":continue" is handled here.
1030 * If we were executing commands, jump back to the ":while" or
1031 * ":for".
1032 * If we were not executing commands, decrement cs_looplevel.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001033 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001034 if (cstack.cs_lflags & (CSL_HAD_CONT | CSL_HAD_ENDLOOP))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001035 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001036 cstack.cs_lflags &= ~(CSL_HAD_CONT | CSL_HAD_ENDLOOP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001037
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001038 // Jump back to the matching ":while" or ":for". Be careful
1039 // not to use a cs_line[] from an entry that isn't a ":while"
1040 // or ":for": It would make "current_line" invalid and can
1041 // cause a crash.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001042 if (!did_emsg && !got_int && !did_throw
1043 && cstack.cs_idx >= 0
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001044 && (cstack.cs_flags[cstack.cs_idx]
1045 & (CSF_WHILE | CSF_FOR))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001046 && cstack.cs_line[cstack.cs_idx] >= 0
1047 && (cstack.cs_flags[cstack.cs_idx] & CSF_ACTIVE))
1048 {
1049 current_line = cstack.cs_line[cstack.cs_idx];
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001050 // remember we jumped there
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001051 cstack.cs_lflags |= CSL_HAD_LOOP;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001052 line_breakcheck(); // check if CTRL-C typed
Bram Moolenaar071d4272004-06-13 20:20:40 +00001053
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001054 // Check for the next breakpoint at or after the ":while"
1055 // or ":for".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001056 if (breakpoint != NULL)
1057 {
1058 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001059 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001060 fname,
1061 ((wcmd_T *)lines_ga.ga_data)[current_line].lnum-1);
1062 *dbg_tick = debug_tick;
1063 }
1064 }
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001065 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001066 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001067 // can only get here with ":endwhile" or ":endfor"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001068 if (cstack.cs_idx >= 0)
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001069 rewind_conditionals(&cstack, cstack.cs_idx - 1,
1070 CSF_WHILE | CSF_FOR, &cstack.cs_looplevel);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001071 }
1072 }
1073
1074 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001075 * For a ":while" or ":for" we need to remember the line number.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001076 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001077 else if (cstack.cs_lflags & CSL_HAD_LOOP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001078 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001079 cstack.cs_lflags &= ~CSL_HAD_LOOP;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001080 cstack.cs_line[cstack.cs_idx] = current_line - 1;
1081 }
1082 }
1083
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001084 // Check for the next breakpoint after a watchexpression
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01001085 if (breakpoint != NULL && has_watchexpr())
1086 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001087 *breakpoint = dbg_find_breakpoint(FALSE, fname, SOURCING_LNUM);
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01001088 *dbg_tick = debug_tick;
1089 }
1090
Bram Moolenaar071d4272004-06-13 20:20:40 +00001091 /*
1092 * When not inside any ":while" loop, clear remembered lines.
1093 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001094 if (cstack.cs_looplevel == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001095 {
1096 if (lines_ga.ga_len > 0)
1097 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001098 SOURCING_LNUM =
Bram Moolenaar071d4272004-06-13 20:20:40 +00001099 ((wcmd_T *)lines_ga.ga_data)[lines_ga.ga_len - 1].lnum;
1100 free_cmdlines(&lines_ga);
1101 }
1102 current_line = 0;
1103 }
1104
1105 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001106 * A ":finally" makes did_emsg, got_int, and did_throw pending for
1107 * being restored at the ":endtry". Reset them here and set the
1108 * ACTIVE and FINALLY flags, so that the finally clause gets executed.
1109 * This includes the case where a missing ":endif", ":endwhile" or
1110 * ":endfor" was detected by the ":finally" itself.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001111 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001112 if (cstack.cs_lflags & CSL_HAD_FINA)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001113 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001114 cstack.cs_lflags &= ~CSL_HAD_FINA;
1115 report_make_pending(cstack.cs_pending[cstack.cs_idx]
1116 & (CSTP_ERROR | CSTP_INTERRUPT | CSTP_THROW),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001117 did_throw ? (void *)current_exception : NULL);
1118 did_emsg = got_int = did_throw = FALSE;
1119 cstack.cs_flags[cstack.cs_idx] |= CSF_ACTIVE | CSF_FINALLY;
1120 }
1121
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001122 // Update global "trylevel" for recursive calls to do_cmdline() from
1123 // within this loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001124 trylevel = initial_trylevel + cstack.cs_trylevel;
1125
1126 /*
Bram Moolenaarc1762cc2007-05-10 16:56:30 +00001127 * If the outermost try conditional (across function calls and sourced
Bram Moolenaar071d4272004-06-13 20:20:40 +00001128 * files) is aborted because of an error, an interrupt, or an uncaught
1129 * exception, cancel everything. If it is left normally, reset
1130 * force_abort to get the non-EH compatible abortion behavior for
1131 * the rest of the script.
1132 */
1133 if (trylevel == 0 && !did_emsg && !got_int && !did_throw)
1134 force_abort = FALSE;
1135
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001136 // Convert an interrupt to an exception if appropriate.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001137 (void)do_intthrow(&cstack);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001138#endif // FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001139
1140 }
1141 /*
1142 * Continue executing command lines when:
1143 * - no CTRL-C typed, no aborting error, no exception thrown or try
1144 * conditionals need to be checked for executing finally clauses or
1145 * catching an interrupt exception
1146 * - didn't get an error message or lines are not typed
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001147 * - there is a command after '|', inside a :if, :while, :for or :try, or
Bram Moolenaar071d4272004-06-13 20:20:40 +00001148 * looping for ":source" command or function call.
1149 */
1150 while (!((got_int
1151#ifdef FEAT_EVAL
1152 || (did_emsg && force_abort) || did_throw
1153#endif
1154 )
1155#ifdef FEAT_EVAL
1156 && cstack.cs_trylevel == 0
1157#endif
1158 )
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001159 && !(did_emsg
1160#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001161 // Keep going when inside try/catch, so that the error can be
1162 // deal with, except when it is a syntax error, it may cause
1163 // the :endtry to be missed.
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001164 && (cstack.cs_trylevel == 0 || did_emsg_syntax)
1165#endif
1166 && used_getline
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001167 && (getline_equal(fgetline, cookie, getexmodeline)
1168 || getline_equal(fgetline, cookie, getexline)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001169 && (next_cmdline != NULL
1170#ifdef FEAT_EVAL
1171 || cstack.cs_idx >= 0
1172#endif
1173 || (flags & DOCMD_REPEAT)));
1174
1175 vim_free(cmdline_copy);
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001176 did_emsg_syntax = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001177#ifdef FEAT_EVAL
1178 free_cmdlines(&lines_ga);
1179 ga_clear(&lines_ga);
1180
1181 if (cstack.cs_idx >= 0)
1182 {
1183 /*
1184 * If a sourced file or executed function ran to its end, report the
1185 * unclosed conditional.
1186 */
1187 if (!got_int && !did_throw
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001188 && ((getline_equal(fgetline, cookie, getsourceline)
1189 && !source_finished(fgetline, cookie))
1190 || (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001191 && !func_has_ended(real_cookie))))
1192 {
1193 if (cstack.cs_flags[cstack.cs_idx] & CSF_TRY)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001194 emsg(_(e_endtry));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001195 else if (cstack.cs_flags[cstack.cs_idx] & CSF_WHILE)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001196 emsg(_(e_endwhile));
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001197 else if (cstack.cs_flags[cstack.cs_idx] & CSF_FOR)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001198 emsg(_(e_endfor));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001199 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001200 emsg(_(e_endif));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001201 }
1202
1203 /*
1204 * Reset "trylevel" in case of a ":finish" or ":return" or a missing
1205 * ":endtry" in a sourced file or executed function. If the try
1206 * conditional is in its finally clause, ignore anything pending.
1207 * If it is in a catch clause, finish the caught exception.
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001208 * Also cleanup any "cs_forinfo" structures.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001209 */
1210 do
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001211 {
1212 int idx = cleanup_conditionals(&cstack, 0, TRUE);
1213
Bram Moolenaar89e5d682005-01-17 22:06:23 +00001214 if (idx >= 0)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001215 --idx; // remove try block not in its finally clause
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001216 rewind_conditionals(&cstack, idx, CSF_WHILE | CSF_FOR,
1217 &cstack.cs_looplevel);
1218 }
1219 while (cstack.cs_idx >= 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001220 trylevel = initial_trylevel;
1221 }
1222
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001223 // If a missing ":endtry", ":endwhile", ":endfor", or ":endif" or a memory
1224 // lack was reported above and the error message is to be converted to an
1225 // exception, do this now after rewinding the cstack.
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001226 do_errthrow(&cstack, getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001227 ? (char_u *)"endfunction" : (char_u *)NULL);
1228
1229 if (trylevel == 0)
1230 {
1231 /*
1232 * When an exception is being thrown out of the outermost try
1233 * conditional, discard the uncaught exception, disable the conversion
1234 * of interrupts or errors to exceptions, and ensure that no more
1235 * commands are executed.
1236 */
1237 if (did_throw)
1238 {
1239 void *p = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001240 struct msglist *messages = NULL, *next;
1241
1242 /*
1243 * If the uncaught exception is a user exception, report it as an
1244 * error. If it is an error exception, display the saved error
1245 * message now. For an interrupt exception, do nothing; the
1246 * interrupt message is given elsewhere.
1247 */
1248 switch (current_exception->type)
1249 {
1250 case ET_USER:
Bram Moolenaar9c13b352005-05-19 20:53:52 +00001251 vim_snprintf((char *)IObuff, IOSIZE,
1252 _("E605: Exception not caught: %s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001253 current_exception->value);
1254 p = vim_strsave(IObuff);
1255 break;
1256 case ET_ERROR:
1257 messages = current_exception->messages;
1258 current_exception->messages = NULL;
1259 break;
1260 case ET_INTERRUPT:
1261 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001262 }
1263
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001264 estack_push(ETYPE_EXCEPT, current_exception->throw_name,
1265 current_exception->throw_lnum);
Bram Moolenaare31ee862020-01-07 20:59:34 +01001266 ESTACK_CHECK_SETUP
Bram Moolenaar071d4272004-06-13 20:20:40 +00001267 current_exception->throw_name = NULL;
1268
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001269 discard_current_exception(); // uses IObuff if 'verbose'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001270 suppress_errthrow = TRUE;
1271 force_abort = TRUE;
1272
1273 if (messages != NULL)
1274 {
1275 do
1276 {
1277 next = messages->next;
1278 emsg(messages->msg);
1279 vim_free(messages->msg);
1280 vim_free(messages);
1281 messages = next;
1282 }
1283 while (messages != NULL);
1284 }
1285 else if (p != NULL)
1286 {
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01001287 emsg(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001288 vim_free(p);
1289 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001290 vim_free(SOURCING_NAME);
Bram Moolenaare31ee862020-01-07 20:59:34 +01001291 ESTACK_CHECK_NOW
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001292 estack_pop();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001293 }
1294
1295 /*
1296 * On an interrupt or an aborting error not converted to an exception,
1297 * disable the conversion of errors to exceptions. (Interrupts are not
1298 * converted any more, here.) This enables also the interrupt message
1299 * when force_abort is set and did_emsg unset in case of an interrupt
1300 * from a finally clause after an error.
1301 */
1302 else if (got_int || (did_emsg && force_abort))
1303 suppress_errthrow = TRUE;
1304 }
1305
1306 /*
1307 * The current cstack will be freed when do_cmdline() returns. An uncaught
1308 * exception will have to be rethrown in the previous cstack. If a function
1309 * has just returned or a script file was just finished and the previous
1310 * cstack belongs to the same function or, respectively, script file, it
1311 * will have to be checked for finally clauses to be executed due to the
1312 * ":return" or ":finish". This is done in do_one_cmd().
1313 */
1314 if (did_throw)
1315 need_rethrow = TRUE;
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001316 if ((getline_equal(fgetline, cookie, getsourceline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001317 && ex_nesting_level > source_level(real_cookie))
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001318 || (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001319 && ex_nesting_level > func_level(real_cookie) + 1))
1320 {
1321 if (!did_throw)
1322 check_cstack = TRUE;
1323 }
1324 else
1325 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001326 // When leaving a function, reduce nesting level.
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001327 if (getline_equal(fgetline, cookie, get_func_line))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001328 --ex_nesting_level;
1329 /*
1330 * Go to debug mode when returning from a function in which we are
1331 * single-stepping.
1332 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001333 if ((getline_equal(fgetline, cookie, getsourceline)
1334 || getline_equal(fgetline, cookie, get_func_line))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001335 && ex_nesting_level + 1 <= debug_break_level)
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001336 do_debug(getline_equal(fgetline, cookie, getsourceline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001337 ? (char_u *)_("End of sourced file")
1338 : (char_u *)_("End of function"));
1339 }
1340
1341 /*
1342 * Restore the exception environment (done after returning from the
1343 * debugger).
1344 */
1345 if (flags & DOCMD_EXCRESET)
Bram Moolenaared203462004-06-16 11:19:22 +00001346 restore_dbg_stuff(&debug_saved);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001347
1348 msg_list = saved_msg_list;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001349#endif // FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001350
1351 /*
1352 * If there was too much output to fit on the command line, ask the user to
1353 * hit return before redrawing the screen. With the ":global" command we do
1354 * this only once after the command is finished.
1355 */
1356 if (did_inc)
1357 {
1358 --RedrawingDisabled;
1359 --no_wait_return;
1360 msg_scroll = FALSE;
1361
1362 /*
1363 * When just finished an ":if"-":else" which was typed, no need to
1364 * wait for hit-return. Also for an error situation.
1365 */
1366 if (retval == FAIL
1367#ifdef FEAT_EVAL
1368 || (did_endif && KeyTyped && !did_emsg)
1369#endif
1370 )
1371 {
1372 need_wait_return = FALSE;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001373 msg_didany = FALSE; // don't wait when restarting edit
Bram Moolenaar071d4272004-06-13 20:20:40 +00001374 }
1375 else if (need_wait_return)
1376 {
1377 /*
1378 * The msg_start() above clears msg_didout. The wait_return we do
1379 * here should not overwrite the command that may be shown before
1380 * doing that.
1381 */
1382 msg_didout |= msg_didout_before_start;
1383 wait_return(FALSE);
1384 }
1385 }
1386
Bram Moolenaar2a942252012-11-28 23:03:07 +01001387#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001388 did_endif = FALSE; // in case do_cmdline used recursively
Bram Moolenaar2a942252012-11-28 23:03:07 +01001389#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001390 /*
1391 * Reset if_level, in case a sourced script file contains more ":if" than
1392 * ":endif" (could be ":if x | foo | endif").
1393 */
1394 if_level = 0;
Bram Moolenaar2e18a122012-11-28 19:10:54 +01001395#endif
Bram Moolenaard4ad0d42012-11-28 17:34:48 +01001396
Bram Moolenaar071d4272004-06-13 20:20:40 +00001397 --call_depth;
1398 return retval;
1399}
1400
1401#ifdef FEAT_EVAL
1402/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001403 * Obtain a line when inside a ":while" or ":for" loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001404 */
1405 static char_u *
Bram Moolenaare96a2492019-06-25 04:12:16 +02001406get_loop_line(int c, void *cookie, int indent, int do_concat)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001407{
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001408 struct loop_cookie *cp = (struct loop_cookie *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001409 wcmd_T *wp;
1410 char_u *line;
1411
1412 if (cp->current_line + 1 >= cp->lines_gap->ga_len)
1413 {
1414 if (cp->repeating)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001415 return NULL; // trying to read past ":endwhile"/":endfor"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001416
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001417 // First time inside the ":while"/":for": get line normally.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001418 if (cp->getline == NULL)
Bram Moolenaare96a2492019-06-25 04:12:16 +02001419 line = getcmdline(c, 0L, indent, do_concat);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001420 else
Bram Moolenaare96a2492019-06-25 04:12:16 +02001421 line = cp->getline(c, cp->cookie, indent, do_concat);
Bram Moolenaard68071d2006-05-02 22:08:30 +00001422 if (line != NULL && store_loop_line(cp->lines_gap, line) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001423 ++cp->current_line;
1424
1425 return line;
1426 }
1427
1428 KeyTyped = FALSE;
1429 ++cp->current_line;
1430 wp = (wcmd_T *)(cp->lines_gap->ga_data) + cp->current_line;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001431 SOURCING_LNUM = wp->lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001432 return vim_strsave(wp->line);
1433}
1434
1435/*
1436 * Store a line in "gap" so that a ":while" loop can execute it again.
1437 */
1438 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001439store_loop_line(garray_T *gap, char_u *line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001440{
1441 if (ga_grow(gap, 1) == FAIL)
1442 return FAIL;
1443 ((wcmd_T *)(gap->ga_data))[gap->ga_len].line = vim_strsave(line);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001444 ((wcmd_T *)(gap->ga_data))[gap->ga_len].lnum = SOURCING_LNUM;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001445 ++gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001446 return OK;
1447}
1448
1449/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001450 * Free the lines stored for a ":while" or ":for" loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001451 */
1452 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001453free_cmdlines(garray_T *gap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001454{
1455 while (gap->ga_len > 0)
1456 {
1457 vim_free(((wcmd_T *)(gap->ga_data))[gap->ga_len - 1].line);
1458 --gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001459 }
1460}
1461#endif
1462
1463/*
Bram Moolenaar89d40322006-08-29 15:30:07 +00001464 * If "fgetline" is get_loop_line(), return TRUE if the getline it uses equals
1465 * "func". * Otherwise return TRUE when "fgetline" equals "func".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001466 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001467 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001468getline_equal(
Bram Moolenaare96a2492019-06-25 04:12:16 +02001469 char_u *(*fgetline)(int, void *, int, int),
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001470 void *cookie UNUSED, // argument for fgetline()
Bram Moolenaare96a2492019-06-25 04:12:16 +02001471 char_u *(*func)(int, void *, int, int))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001472{
1473#ifdef FEAT_EVAL
Bram Moolenaare96a2492019-06-25 04:12:16 +02001474 char_u *(*gp)(int, void *, int, int);
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001475 struct loop_cookie *cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001476
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001477 // When "fgetline" is "get_loop_line()" use the "cookie" to find the
1478 // function that's originally used to obtain the lines. This may be
1479 // nested several levels.
Bram Moolenaar89d40322006-08-29 15:30:07 +00001480 gp = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001481 cp = (struct loop_cookie *)cookie;
1482 while (gp == get_loop_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001483 {
1484 gp = cp->getline;
1485 cp = cp->cookie;
1486 }
1487 return gp == func;
1488#else
Bram Moolenaar89d40322006-08-29 15:30:07 +00001489 return fgetline == func;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001490#endif
1491}
1492
Bram Moolenaar071d4272004-06-13 20:20:40 +00001493/*
Bram Moolenaar89d40322006-08-29 15:30:07 +00001494 * If "fgetline" is get_loop_line(), return the cookie used by the original
Bram Moolenaar071d4272004-06-13 20:20:40 +00001495 * getline function. Otherwise return "cookie".
1496 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001497 void *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001498getline_cookie(
Bram Moolenaare96a2492019-06-25 04:12:16 +02001499 char_u *(*fgetline)(int, void *, int, int) UNUSED,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001500 void *cookie) // argument for fgetline()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001501{
Bram Moolenaar13505972019-01-24 15:04:48 +01001502#ifdef FEAT_EVAL
Bram Moolenaare96a2492019-06-25 04:12:16 +02001503 char_u *(*gp)(int, void *, int, int);
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001504 struct loop_cookie *cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001505
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001506 // When "fgetline" is "get_loop_line()" use the "cookie" to find the
1507 // cookie that's originally used to obtain the lines. This may be nested
1508 // several levels.
Bram Moolenaar89d40322006-08-29 15:30:07 +00001509 gp = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001510 cp = (struct loop_cookie *)cookie;
1511 while (gp == get_loop_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001512 {
1513 gp = cp->getline;
1514 cp = cp->cookie;
1515 }
1516 return cp;
Bram Moolenaar13505972019-01-24 15:04:48 +01001517#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001518 return cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001519#endif
Bram Moolenaar13505972019-01-24 15:04:48 +01001520}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001521
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001522
1523/*
1524 * Helper function to apply an offset for buffer commands, i.e. ":bdelete",
1525 * ":bwipeout", etc.
1526 * Returns the buffer number.
1527 */
1528 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001529compute_buffer_local_count(int addr_type, int lnum, int offset)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001530{
1531 buf_T *buf;
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001532 buf_T *nextbuf;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001533 int count = offset;
1534
1535 buf = firstbuf;
1536 while (buf->b_next != NULL && buf->b_fnum < lnum)
1537 buf = buf->b_next;
1538 while (count != 0)
1539 {
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001540 count += (offset < 0) ? 1 : -1;
1541 nextbuf = (offset < 0) ? buf->b_prev : buf->b_next;
1542 if (nextbuf == NULL)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001543 break;
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001544 buf = nextbuf;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001545 if (addr_type == ADDR_LOADED_BUFFERS)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001546 // skip over unloaded buffers
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001547 while (buf->b_ml.ml_mfp == NULL)
1548 {
1549 nextbuf = (offset < 0) ? buf->b_prev : buf->b_next;
1550 if (nextbuf == NULL)
1551 break;
1552 buf = nextbuf;
1553 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001554 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001555 // we might have gone too far, last buffer is not loadedd
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001556 if (addr_type == ADDR_LOADED_BUFFERS)
1557 while (buf->b_ml.ml_mfp == NULL)
1558 {
1559 nextbuf = (offset >= 0) ? buf->b_prev : buf->b_next;
1560 if (nextbuf == NULL)
1561 break;
1562 buf = nextbuf;
1563 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001564 return buf->b_fnum;
1565}
1566
Bram Moolenaarb7316892019-05-01 18:08:42 +02001567/*
1568 * Return the window number of "win".
1569 * When "win" is NULL return the number of windows.
1570 */
Bram Moolenaarf240e182014-11-27 18:33:02 +01001571 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001572current_win_nr(win_T *win)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001573{
1574 win_T *wp;
1575 int nr = 0;
1576
Bram Moolenaar29323592016-07-24 22:04:11 +02001577 FOR_ALL_WINDOWS(wp)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001578 {
1579 ++nr;
1580 if (wp == win)
1581 break;
1582 }
1583 return nr;
1584}
1585
1586 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001587current_tab_nr(tabpage_T *tab)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001588{
1589 tabpage_T *tp;
1590 int nr = 0;
1591
Bram Moolenaar29323592016-07-24 22:04:11 +02001592 FOR_ALL_TABPAGES(tp)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001593 {
1594 ++nr;
1595 if (tp == tab)
1596 break;
1597 }
1598 return nr;
1599}
1600
1601# define CURRENT_WIN_NR current_win_nr(curwin)
1602# define LAST_WIN_NR current_win_nr(NULL)
1603# define CURRENT_TAB_NR current_tab_nr(curtab)
1604# define LAST_TAB_NR current_tab_nr(NULL)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001605
Bram Moolenaar071d4272004-06-13 20:20:40 +00001606/*
1607 * Execute one Ex command.
1608 *
1609 * If 'sourcing' is TRUE, the command will be included in the error message.
1610 *
1611 * 1. skip comment lines and leading space
1612 * 2. handle command modifiers
Bram Moolenaar1c40a662014-11-27 16:38:11 +01001613 * 3. find the command
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001614 * 4. parse range
Bram Moolenaar1c40a662014-11-27 16:38:11 +01001615 * 5. Parse the command.
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001616 * 6. parse arguments
1617 * 7. switch on command name
Bram Moolenaar071d4272004-06-13 20:20:40 +00001618 *
Bram Moolenaar89d40322006-08-29 15:30:07 +00001619 * Note: "fgetline" can be NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001620 *
1621 * This function may be called recursively!
1622 */
1623#if (_MSC_VER == 1200)
1624/*
Bram Moolenaared203462004-06-16 11:19:22 +00001625 * Avoid optimisation bug in VC++ version 6.0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001626 */
Bram Moolenaar281bdce2005-01-25 21:53:18 +00001627 #pragma optimize( "g", off )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001628#endif
1629 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001630do_one_cmd(
Bram Moolenaarddef1292019-12-16 17:10:33 +01001631 char_u **cmdlinep,
1632 int sourcing,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001633#ifdef FEAT_EVAL
Bram Moolenaarddef1292019-12-16 17:10:33 +01001634 cstack_T *cstack,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001635#endif
Bram Moolenaarddef1292019-12-16 17:10:33 +01001636 char_u *(*fgetline)(int, void *, int, int),
1637 void *cookie) // argument for fgetline()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001638{
Bram Moolenaarddef1292019-12-16 17:10:33 +01001639 char_u *p;
1640 linenr_T lnum;
1641 long n;
1642 char *errormsg = NULL; // error message
1643 char_u *after_modifier = NULL;
1644 exarg_T ea; // Ex command arguments
1645 int save_msg_scroll = msg_scroll;
1646 cmdmod_T save_cmdmod;
1647 int save_reg_executing = reg_executing;
1648 int ni; // set when Not Implemented
1649 char_u *cmd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001650
1651 vim_memset(&ea, 0, sizeof(ea));
1652 ea.line1 = 1;
1653 ea.line2 = 1;
1654#ifdef FEAT_EVAL
1655 ++ex_nesting_level;
1656#endif
1657
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001658 // When the last file has not been edited :q has to be typed twice.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001659 if (quitmore
1660#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001661 // avoid that a function call in 'statusline' does this
Bram Moolenaar89d40322006-08-29 15:30:07 +00001662 && !getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001663#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001664 // avoid that an autocommand, e.g. QuitPre, does this
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001665 && !getline_equal(fgetline, cookie, getnextac))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001666 --quitmore;
1667
1668 /*
1669 * Reset browse, confirm, etc.. They are restored when returning, for
1670 * recursive calls.
1671 */
1672 save_cmdmod = cmdmod;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001673
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001674 // "#!anything" is handled like a comment.
Bram Moolenaarcbb37ad2006-08-16 15:04:21 +00001675 if ((*cmdlinep)[0] == '#' && (*cmdlinep)[1] == '!')
1676 goto doend;
1677
Bram Moolenaar4facea32019-10-12 20:17:40 +02001678 if (p_verbose >= 16)
1679 msg_verbose_cmd(0, *cmdlinep);
1680
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02001681/*
1682 * 1. Skip comment lines and leading white space and colons.
1683 * 2. Handle command modifiers.
1684 */
1685 // The "ea" structure holds the arguments that can be used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001686 ea.cmd = *cmdlinep;
Bram Moolenaareffed932018-08-14 13:38:17 +02001687 ea.cmdlinep = cmdlinep;
1688 ea.getline = fgetline;
1689 ea.cookie = cookie;
1690#ifdef FEAT_EVAL
1691 ea.cstack = cstack;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001692#endif
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02001693 if (parse_command_modifiers(&ea, &errormsg, FALSE) == FAIL)
Bram Moolenaareffed932018-08-14 13:38:17 +02001694 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001695
Bram Moolenaar7b668e82016-08-23 23:51:21 +02001696 after_modifier = ea.cmd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001697
1698#ifdef FEAT_EVAL
1699 ea.skip = did_emsg || got_int || did_throw || (cstack->cs_idx >= 0
1700 && !(cstack->cs_flags[cstack->cs_idx] & CSF_ACTIVE));
1701#else
1702 ea.skip = (if_level > 0);
1703#endif
1704
Bram Moolenaar071d4272004-06-13 20:20:40 +00001705/*
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001706 * 3. Skip over the range to find the command. Let "p" point to after it.
1707 *
1708 * We need the command to know what kind of range it uses.
1709 */
1710 cmd = ea.cmd;
1711 ea.cmd = skip_range(ea.cmd, NULL);
1712 if (*ea.cmd == '*' && vim_strchr(p_cpo, CPO_STAR) == NULL)
1713 ea.cmd = skipwhite(ea.cmd + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001714
1715#ifdef FEAT_EVAL
1716 if (current_sctx.sc_version == SCRIPT_VERSION_VIM9)
1717 p = find_ex_command(&ea, NULL, lookup_scriptvar, NULL);
1718 else
1719#endif
1720 p = find_ex_command(&ea, NULL, NULL, NULL);
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001721
Bram Moolenaar7feb35e2018-08-21 17:49:54 +02001722#ifdef FEAT_EVAL
1723# ifdef FEAT_PROFILE
1724 // Count this line for profiling if skip is TRUE.
1725 if (do_profiling == PROF_YES
1726 && (!ea.skip || cstack->cs_idx == 0 || (cstack->cs_idx > 0
1727 && (cstack->cs_flags[cstack->cs_idx - 1] & CSF_ACTIVE))))
1728 {
1729 int skip = did_emsg || got_int || did_throw;
1730
1731 if (ea.cmdidx == CMD_catch)
1732 skip = !skip && !(cstack->cs_idx >= 0
1733 && (cstack->cs_flags[cstack->cs_idx] & CSF_THROWN)
1734 && !(cstack->cs_flags[cstack->cs_idx] & CSF_CAUGHT));
1735 else if (ea.cmdidx == CMD_else || ea.cmdidx == CMD_elseif)
1736 skip = skip || !(cstack->cs_idx >= 0
1737 && !(cstack->cs_flags[cstack->cs_idx]
1738 & (CSF_ACTIVE | CSF_TRUE)));
1739 else if (ea.cmdidx == CMD_finally)
1740 skip = FALSE;
1741 else if (ea.cmdidx != CMD_endif
1742 && ea.cmdidx != CMD_endfor
1743 && ea.cmdidx != CMD_endtry
1744 && ea.cmdidx != CMD_endwhile)
1745 skip = ea.skip;
1746
1747 if (!skip)
1748 {
1749 if (getline_equal(fgetline, cookie, get_func_line))
1750 func_line_exec(getline_cookie(fgetline, cookie));
1751 else if (getline_equal(fgetline, cookie, getsourceline))
1752 script_line_exec();
1753 }
1754 }
1755# endif
1756
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001757 // May go to debug mode. If this happens and the ">quit" debug command is
1758 // used, throw an interrupt exception and skip the next command.
Bram Moolenaar7feb35e2018-08-21 17:49:54 +02001759 dbg_check_breakpoint(&ea);
1760 if (!ea.skip && got_int)
1761 {
1762 ea.skip = TRUE;
1763 (void)do_intthrow(cstack);
1764 }
1765#endif
1766
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001767/*
1768 * 4. parse a range specifier of the form: addr [,addr] [;addr] ..
Bram Moolenaar071d4272004-06-13 20:20:40 +00001769 *
1770 * where 'addr' is:
1771 *
1772 * % (entire file)
1773 * $ [+-NUM]
1774 * 'x [+-NUM] (where x denotes a currently defined mark)
1775 * . [+-NUM]
1776 * [+-NUM]..
1777 * NUM
1778 *
1779 * The ea.cmd pointer is updated to point to the first character following the
1780 * range spec. If an initial address is found, but no second, the upper bound
1781 * is equal to the lower.
1782 */
1783
Bram Moolenaar25190db2019-05-04 15:05:28 +02001784 // ea.addr_type for user commands is set by find_ucmd
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01001785 if (!IS_USER_CMDIDX(ea.cmdidx))
1786 {
1787 if (ea.cmdidx != CMD_SIZE)
1788 ea.addr_type = cmdnames[(int)ea.cmdidx].cmd_addr_type;
1789 else
1790 ea.addr_type = ADDR_LINES;
1791
Bram Moolenaar25190db2019-05-04 15:05:28 +02001792 // :wincmd range depends on the argument.
Bram Moolenaar164f3262015-01-14 21:22:01 +01001793 if (ea.cmdidx == CMD_wincmd && p != NULL)
1794 get_wincmd_addr_type(skipwhite(p), &ea);
Bram Moolenaar25190db2019-05-04 15:05:28 +02001795#ifdef FEAT_QUICKFIX
1796 // :.cc in quickfix window uses line number
1797 if ((ea.cmdidx == CMD_cc || ea.cmdidx == CMD_ll) && bt_quickfix(curbuf))
1798 ea.addr_type = ADDR_OTHER;
1799#endif
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01001800 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001801
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01001802 ea.cmd = cmd;
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02001803 if (parse_cmd_address(&ea, &errormsg, FALSE) == FAIL)
Bram Moolenaaree8415b2018-08-10 23:13:12 +02001804 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001805
Bram Moolenaar071d4272004-06-13 20:20:40 +00001806/*
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001807 * 5. Parse the command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001808 */
1809
1810 /*
1811 * Skip ':' and any white space
1812 */
1813 ea.cmd = skipwhite(ea.cmd);
1814 while (*ea.cmd == ':')
1815 ea.cmd = skipwhite(ea.cmd + 1);
1816
1817 /*
1818 * If we got a line, but no command, then go to the line.
1819 * If we find a '|' or '\n' we set ea.nextcmd.
1820 */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001821 if (*ea.cmd == NUL || *ea.cmd == '"'
1822 || (ea.nextcmd = check_nextcmd(ea.cmd)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001823 {
1824 /*
1825 * strange vi behaviour:
1826 * ":3" jumps to line 3
1827 * ":3|..." prints line 3
1828 * ":|" prints current line
1829 */
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001830 if (ea.skip) // skip this if inside :if
Bram Moolenaar071d4272004-06-13 20:20:40 +00001831 goto doend;
Bram Moolenaardf177f62005-02-22 08:39:57 +00001832 if (*ea.cmd == '|' || (exmode_active && ea.line1 != ea.line2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001833 {
1834 ea.cmdidx = CMD_print;
Bram Moolenaar8071cb22019-07-12 17:58:01 +02001835 ea.argt = EX_RANGE+EX_COUNT+EX_TRLBAR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001836 if ((errormsg = invalid_range(&ea)) == NULL)
1837 {
1838 correct_range(&ea);
1839 ex_print(&ea);
1840 }
1841 }
1842 else if (ea.addr_count != 0)
1843 {
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00001844 if (ea.line2 > curbuf->b_ml.ml_line_count)
1845 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001846 // With '-' in 'cpoptions' a line number past the file is an
1847 // error, otherwise put it at the end of the file.
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00001848 if (vim_strchr(p_cpo, CPO_MINUS) != NULL)
1849 ea.line2 = -1;
1850 else
1851 ea.line2 = curbuf->b_ml.ml_line_count;
1852 }
1853
1854 if (ea.line2 < 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001855 errormsg = _(e_invrange);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001856 else
1857 {
1858 if (ea.line2 == 0)
1859 curwin->w_cursor.lnum = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001860 else
1861 curwin->w_cursor.lnum = ea.line2;
1862 beginline(BL_SOL | BL_FIX);
1863 }
1864 }
1865 goto doend;
1866 }
1867
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001868 // If this looks like an undefined user command and there are CmdUndefined
1869 // autocommands defined, trigger the matching autocommands.
Bram Moolenaard5005162014-08-22 23:05:54 +02001870 if (p != NULL && ea.cmdidx == CMD_SIZE && !ea.skip
1871 && ASCII_ISUPPER(*ea.cmd)
1872 && has_cmdundefined())
1873 {
Bram Moolenaard5005162014-08-22 23:05:54 +02001874 int ret;
1875
Bram Moolenaar5a31b462014-08-23 14:16:20 +02001876 p = ea.cmd;
Bram Moolenaard5005162014-08-22 23:05:54 +02001877 while (ASCII_ISALNUM(*p))
1878 ++p;
Bram Moolenaar120f4a82014-09-09 12:22:06 +02001879 p = vim_strnsave(ea.cmd, (int)(p - ea.cmd));
Bram Moolenaard5005162014-08-22 23:05:54 +02001880 ret = apply_autocmds(EVENT_CMDUNDEFINED, p, p, TRUE, NULL);
1881 vim_free(p);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001882 // If the autocommands did something and didn't cause an error, try
1883 // finding the command again.
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001884 p = (ret
1885#ifdef FEAT_EVAL
1886 && !aborting()
Bram Moolenaard5005162014-08-22 23:05:54 +02001887#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001888 ) ? find_ex_command(&ea, NULL, NULL, NULL) : ea.cmd;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001889 }
Bram Moolenaard5005162014-08-22 23:05:54 +02001890
Bram Moolenaar071d4272004-06-13 20:20:40 +00001891 if (p == NULL)
1892 {
1893 if (!ea.skip)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001894 errormsg = _("E464: Ambiguous use of user-defined command");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001895 goto doend;
1896 }
Bram Moolenaarac9fb182019-04-27 13:04:13 +02001897 // Check for wrong commands.
Bram Moolenaar6f9a4762017-06-22 20:39:17 +02001898 if (*p == '!' && ea.cmd[1] == 0151 && ea.cmd[0] == 78
1899 && !IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001900 {
1901 errormsg = uc_fun_cmd();
1902 goto doend;
1903 }
Bram Moolenaarac9fb182019-04-27 13:04:13 +02001904
Bram Moolenaar071d4272004-06-13 20:20:40 +00001905 if (ea.cmdidx == CMD_SIZE)
1906 {
1907 if (!ea.skip)
1908 {
1909 STRCPY(IObuff, _("E492: Not an editor command"));
1910 if (!sourcing)
Bram Moolenaar7b668e82016-08-23 23:51:21 +02001911 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001912 // If the modifier was parsed OK the error must be in the
1913 // following command
Bram Moolenaar7b668e82016-08-23 23:51:21 +02001914 if (after_modifier != NULL)
1915 append_command(after_modifier);
1916 else
1917 append_command(*cmdlinep);
1918 }
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001919 errormsg = (char *)IObuff;
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001920 did_emsg_syntax = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001921 }
1922 goto doend;
1923 }
1924
Bram Moolenaar958636c2014-10-21 20:01:58 +02001925 ni = (!IS_USER_CMDIDX(ea.cmdidx)
1926 && (cmdnames[ea.cmdidx].cmd_func == ex_ni
Bram Moolenaar7bb75552007-07-16 18:39:49 +00001927#ifdef HAVE_EX_SCRIPT_NI
1928 || cmdnames[ea.cmdidx].cmd_func == ex_script_ni
1929#endif
1930 ));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001931
1932#ifndef FEAT_EVAL
1933 /*
1934 * When the expression evaluation is disabled, recognize the ":if" and
1935 * ":endif" commands and ignore everything in between it.
1936 */
1937 if (ea.cmdidx == CMD_if)
1938 ++if_level;
1939 if (if_level)
1940 {
1941 if (ea.cmdidx == CMD_endif)
1942 --if_level;
1943 goto doend;
1944 }
1945
1946#endif
1947
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001948 // forced commands
Bram Moolenaar196b3b02008-06-20 16:51:41 +00001949 if (*p == '!' && ea.cmdidx != CMD_substitute
1950 && ea.cmdidx != CMD_smagic && ea.cmdidx != CMD_snomagic)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001951 {
1952 ++p;
1953 ea.forceit = TRUE;
1954 }
1955 else
1956 ea.forceit = FALSE;
1957
1958/*
Bram Moolenaarb7316892019-05-01 18:08:42 +02001959 * 6. Parse arguments. Then check for errors.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001960 */
Bram Moolenaar958636c2014-10-21 20:01:58 +02001961 if (!IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001962 ea.argt = (long)cmdnames[(int)ea.cmdidx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001963
1964 if (!ea.skip)
1965 {
1966#ifdef HAVE_SANDBOX
Bram Moolenaar8071cb22019-07-12 17:58:01 +02001967 if (sandbox != 0 && !(ea.argt & EX_SBOXOK))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001968 {
Bram Moolenaar8c62a082019-02-08 14:34:10 +01001969 // Command not allowed in sandbox.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001970 errormsg = _(e_sandbox);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001971 goto doend;
1972 }
1973#endif
Bram Moolenaar8071cb22019-07-12 17:58:01 +02001974 if (restricted != 0 && (ea.argt & EX_RESTRICT))
Bram Moolenaar8c62a082019-02-08 14:34:10 +01001975 {
1976 errormsg = _("E981: Command not allowed in rvim");
1977 goto doend;
1978 }
Bram Moolenaar8071cb22019-07-12 17:58:01 +02001979 if (!curbuf->b_p_ma && (ea.argt & EX_MODIFY))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001980 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001981 // Command not allowed in non-'modifiable' buffer
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001982 errormsg = _(e_modifiable);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001983 goto doend;
1984 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00001985
Bram Moolenaar8071cb22019-07-12 17:58:01 +02001986 if (text_locked() && !(ea.argt & EX_CMDWIN)
Bram Moolenaar958636c2014-10-21 20:01:58 +02001987 && !IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001988 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001989 // Command not allowed when editing the command line.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001990 errormsg = _(get_text_locked_msg());
Bram Moolenaar071d4272004-06-13 20:20:40 +00001991 goto doend;
1992 }
Bram Moolenaar379fb762018-08-30 15:58:28 +02001993
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001994 // Disallow editing another buffer when "curbuf_lock" is set.
1995 // Do allow ":checktime" (it is postponed).
1996 // Do allow ":edit" (check for an argument later).
1997 // Do allow ":file" with no arguments (check for an argument later).
Bram Moolenaar8071cb22019-07-12 17:58:01 +02001998 if (!(ea.argt & EX_CMDWIN)
Bram Moolenaar5555acc2006-04-07 21:33:12 +00001999 && ea.cmdidx != CMD_checktime
Bram Moolenaar379fb762018-08-30 15:58:28 +02002000 && ea.cmdidx != CMD_edit
2001 && ea.cmdidx != CMD_file
Bram Moolenaar958636c2014-10-21 20:01:58 +02002002 && !IS_USER_CMDIDX(ea.cmdidx)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002003 && curbuf_locked())
2004 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002005
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002006 if (!ni && !(ea.argt & EX_RANGE) && ea.addr_count > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002007 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002008 // no range allowed
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002009 errormsg = _(e_norange);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002010 goto doend;
2011 }
2012 }
2013
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002014 if (!ni && !(ea.argt & EX_BANG) && ea.forceit) // no <!> allowed
Bram Moolenaar071d4272004-06-13 20:20:40 +00002015 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002016 errormsg = _(e_nobang);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002017 goto doend;
2018 }
2019
2020 /*
2021 * Don't complain about the range if it is not used
2022 * (could happen if line_count is accidentally set to 0).
2023 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002024 if (!ea.skip && !ni && (ea.argt & EX_RANGE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002025 {
2026 /*
2027 * If the range is backwards, ask for confirmation and, if given, swap
2028 * ea.line1 & ea.line2 so it's forwards again.
2029 * When global command is busy, don't ask, will fail below.
2030 */
2031 if (!global_busy && ea.line1 > ea.line2)
2032 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002033 if (msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002034 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002035 if (sourcing || exmode_active)
2036 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002037 errormsg = _("E493: Backwards range given");
Bram Moolenaara5792f52005-11-23 21:25:05 +00002038 goto doend;
2039 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002040 if (ask_yesno((char_u *)
2041 _("Backwards range given, OK to swap"), FALSE) != 'y')
Bram Moolenaara5792f52005-11-23 21:25:05 +00002042 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002043 }
2044 lnum = ea.line1;
2045 ea.line1 = ea.line2;
2046 ea.line2 = lnum;
2047 }
2048 if ((errormsg = invalid_range(&ea)) != NULL)
2049 goto doend;
2050 }
2051
Bram Moolenaarb7316892019-05-01 18:08:42 +02002052 if ((ea.addr_type == ADDR_OTHER) && ea.addr_count == 0)
2053 // default is 1, not cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00002054 ea.line2 = 1;
2055
2056 correct_range(&ea);
2057
2058#ifdef FEAT_FOLDING
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002059 if (((ea.argt & EX_WHOLEFOLD) || ea.addr_count >= 2) && !global_busy
Bram Moolenaara3306952016-01-02 21:41:06 +01002060 && ea.addr_type == ADDR_LINES)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002061 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002062 // Put the first line at the start of a closed fold, put the last line
2063 // at the end of a closed fold.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002064 (void)hasFolding(ea.line1, &ea.line1, NULL);
2065 (void)hasFolding(ea.line2, NULL, &ea.line2);
2066 }
2067#endif
2068
2069#ifdef FEAT_QUICKFIX
2070 /*
Bram Moolenaar86b68352004-12-27 21:59:20 +00002071 * For the ":make" and ":grep" commands we insert the 'makeprg'/'grepprg'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002072 * option here, so things like % get expanded.
2073 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002074 p = replace_makeprg(&ea, p, cmdlinep);
2075 if (p == NULL)
2076 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002077#endif
2078
2079 /*
2080 * Skip to start of argument.
2081 * Don't do this for the ":!" command, because ":!! -l" needs the space.
2082 */
2083 if (ea.cmdidx == CMD_bang)
2084 ea.arg = p;
2085 else
2086 ea.arg = skipwhite(p);
2087
Bram Moolenaar379fb762018-08-30 15:58:28 +02002088 // ":file" cannot be run with an argument when "curbuf_lock" is set
2089 if (ea.cmdidx == CMD_file && *ea.arg != NUL && curbuf_locked())
2090 goto doend;
2091
Bram Moolenaar071d4272004-06-13 20:20:40 +00002092 /*
2093 * Check for "++opt=val" argument.
2094 * Must be first, allow ":w ++enc=utf8 !cmd"
2095 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002096 if (ea.argt & EX_ARGOPT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002097 while (ea.arg[0] == '+' && ea.arg[1] == '+')
2098 if (getargopt(&ea) == FAIL && !ni)
2099 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002100 errormsg = _(e_invarg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002101 goto doend;
2102 }
2103
2104 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
2105 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002106 if (*ea.arg == '>') // append
Bram Moolenaar071d4272004-06-13 20:20:40 +00002107 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002108 if (*++ea.arg != '>') // typed wrong
Bram Moolenaar071d4272004-06-13 20:20:40 +00002109 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002110 errormsg = _("E494: Use w or w>>");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002111 goto doend;
2112 }
2113 ea.arg = skipwhite(ea.arg + 1);
2114 ea.append = TRUE;
2115 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002116 else if (*ea.arg == '!' && ea.cmdidx == CMD_write) // :w !filter
Bram Moolenaar071d4272004-06-13 20:20:40 +00002117 {
2118 ++ea.arg;
2119 ea.usefilter = TRUE;
2120 }
2121 }
2122
2123 if (ea.cmdidx == CMD_read)
2124 {
2125 if (ea.forceit)
2126 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002127 ea.usefilter = TRUE; // :r! filter if ea.forceit
Bram Moolenaar071d4272004-06-13 20:20:40 +00002128 ea.forceit = FALSE;
2129 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002130 else if (*ea.arg == '!') // :r !filter
Bram Moolenaar071d4272004-06-13 20:20:40 +00002131 {
2132 ++ea.arg;
2133 ea.usefilter = TRUE;
2134 }
2135 }
2136
2137 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
2138 {
2139 ea.amount = 1;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002140 while (*ea.arg == *ea.cmd) // count number of '>' or '<'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002141 {
2142 ++ea.arg;
2143 ++ea.amount;
2144 }
2145 ea.arg = skipwhite(ea.arg);
2146 }
2147
2148 /*
2149 * Check for "+command" argument, before checking for next command.
2150 * Don't do this for ":read !cmd" and ":write !cmd".
2151 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002152 if ((ea.argt & EX_CMDARG) && !ea.usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002153 ea.do_ecmd_cmd = getargcmd(&ea.arg);
2154
2155 /*
2156 * Check for '|' to separate commands and '"' to start comments.
2157 * Don't do this for ":read !cmd" and ":write !cmd".
2158 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002159 if ((ea.argt & EX_TRLBAR) && !ea.usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002160 separate_nextcmd(&ea);
2161
2162 /*
2163 * Check for <newline> to end a shell command.
Bram Moolenaardf177f62005-02-22 08:39:57 +00002164 * Also do this for ":read !cmd", ":write !cmd" and ":global".
2165 * Any others?
Bram Moolenaar071d4272004-06-13 20:20:40 +00002166 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00002167 else if (ea.cmdidx == CMD_bang
Bram Moolenaar67883b42017-07-27 22:57:00 +02002168 || ea.cmdidx == CMD_terminal
Bram Moolenaardf177f62005-02-22 08:39:57 +00002169 || ea.cmdidx == CMD_global
2170 || ea.cmdidx == CMD_vglobal
2171 || ea.usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002172 {
2173 for (p = ea.arg; *p; ++p)
2174 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002175 // Remove one backslash before a newline, so that it's possible to
2176 // pass a newline to the shell and also a newline that is preceded
2177 // with a backslash. This makes it impossible to end a shell
2178 // command in a backslash, but that doesn't appear useful.
2179 // Halving the number of backslashes is incompatible with previous
2180 // versions.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002181 if (*p == '\\' && p[1] == '\n')
Bram Moolenaara7241f52008-06-24 20:39:31 +00002182 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002183 else if (*p == '\n')
2184 {
2185 ea.nextcmd = p + 1;
2186 *p = NUL;
2187 break;
2188 }
2189 }
2190 }
2191
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002192 if ((ea.argt & EX_DFLALL) && ea.addr_count == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002193 {
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002194 buf_T *buf;
2195
Bram Moolenaar071d4272004-06-13 20:20:40 +00002196 ea.line1 = 1;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002197 switch (ea.addr_type)
2198 {
2199 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02002200 case ADDR_OTHER:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002201 ea.line2 = curbuf->b_ml.ml_line_count;
2202 break;
2203 case ADDR_LOADED_BUFFERS:
2204 buf = firstbuf;
2205 while (buf->b_next != NULL && buf->b_ml.ml_mfp == NULL)
2206 buf = buf->b_next;
2207 ea.line1 = buf->b_fnum;
2208 buf = lastbuf;
2209 while (buf->b_prev != NULL && buf->b_ml.ml_mfp == NULL)
2210 buf = buf->b_prev;
2211 ea.line2 = buf->b_fnum;
2212 break;
2213 case ADDR_BUFFERS:
2214 ea.line1 = firstbuf->b_fnum;
2215 ea.line2 = lastbuf->b_fnum;
2216 break;
2217 case ADDR_WINDOWS:
2218 ea.line2 = LAST_WIN_NR;
2219 break;
2220 case ADDR_TABS:
2221 ea.line2 = LAST_TAB_NR;
2222 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01002223 case ADDR_TABS_RELATIVE:
2224 ea.line2 = 1;
2225 break;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002226 case ADDR_ARGUMENTS:
2227 if (ARGCOUNT == 0)
2228 ea.line1 = ea.line2 = 0;
2229 else
2230 ea.line2 = ARGCOUNT;
2231 break;
Bram Moolenaar25190db2019-05-04 15:05:28 +02002232 case ADDR_QUICKFIX_VALID:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02002233#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02002234 ea.line2 = qf_get_valid_size(&ea);
Bram Moolenaaraa23b372015-09-08 18:46:31 +02002235 if (ea.line2 == 0)
2236 ea.line2 = 1;
Bram Moolenaare906c502015-09-09 21:10:39 +02002237#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02002238 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02002239 case ADDR_NONE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02002240 case ADDR_UNSIGNED:
2241 case ADDR_QUICKFIX:
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002242 iemsg(_("INTERNAL: Cannot use EX_DFLALL with ADDR_NONE, ADDR_UNSIGNED or ADDR_QUICKFIX"));
Bram Moolenaarb7316892019-05-01 18:08:42 +02002243 break;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002244 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002245 }
2246
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002247 // accept numbered register only when no count allowed (:put)
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002248 if ( (ea.argt & EX_REGSTR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002249 && *ea.arg != NUL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002250 // Do not allow register = for user commands
Bram Moolenaar958636c2014-10-21 20:01:58 +02002251 && (!IS_USER_CMDIDX(ea.cmdidx) || *ea.arg != '=')
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002252 && !((ea.argt & EX_COUNT) && VIM_ISDIGIT(*ea.arg)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002253 {
Bram Moolenaar85de2062011-05-05 14:26:41 +02002254#ifndef FEAT_CLIPBOARD
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002255 // check these explicitly for a more specific error message
Bram Moolenaar85de2062011-05-05 14:26:41 +02002256 if (*ea.arg == '*' || *ea.arg == '+')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002257 {
Bram Moolenaar99b12722019-01-14 20:16:40 +01002258 errormsg = _(e_invalidreg);
Bram Moolenaar85de2062011-05-05 14:26:41 +02002259 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002260 }
2261#endif
Bram Moolenaar958636c2014-10-21 20:01:58 +02002262 if (valid_yank_reg(*ea.arg, (ea.cmdidx != CMD_put
2263 && !IS_USER_CMDIDX(ea.cmdidx))))
Bram Moolenaar85de2062011-05-05 14:26:41 +02002264 {
2265 ea.regname = *ea.arg++;
2266#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002267 // for '=' register: accept the rest of the line as an expression
Bram Moolenaar85de2062011-05-05 14:26:41 +02002268 if (ea.arg[-1] == '=' && ea.arg[0] != NUL)
2269 {
2270 set_expr_line(vim_strsave(ea.arg));
2271 ea.arg += STRLEN(ea.arg);
2272 }
2273#endif
2274 ea.arg = skipwhite(ea.arg);
2275 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002276 }
2277
2278 /*
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002279 * Check for a count. When accepting a EX_BUFNAME, don't use "123foo" as a
Bram Moolenaar071d4272004-06-13 20:20:40 +00002280 * count, it's a buffer name.
2281 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002282 if ((ea.argt & EX_COUNT) && VIM_ISDIGIT(*ea.arg)
2283 && (!(ea.argt & EX_BUFNAME) || *(p = skipdigits(ea.arg)) == NUL
Bram Moolenaar1c465442017-03-12 20:10:05 +01002284 || VIM_ISWHITE(*p)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002285 {
2286 n = getdigits(&ea.arg);
2287 ea.arg = skipwhite(ea.arg);
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002288 if (n <= 0 && !ni && (ea.argt & EX_ZEROR) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002289 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002290 errormsg = _(e_zerocount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002291 goto doend;
2292 }
Bram Moolenaarb7316892019-05-01 18:08:42 +02002293 if (ea.addr_type != ADDR_LINES) // e.g. :buffer 2, :sleep 3
Bram Moolenaar071d4272004-06-13 20:20:40 +00002294 {
2295 ea.line2 = n;
2296 if (ea.addr_count == 0)
2297 ea.addr_count = 1;
2298 }
2299 else
2300 {
2301 ea.line1 = ea.line2;
2302 ea.line2 += n - 1;
2303 ++ea.addr_count;
2304 /*
2305 * Be vi compatible: no error message for out of range.
2306 */
Bram Moolenaarb7316892019-05-01 18:08:42 +02002307 if (ea.line2 > curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002308 ea.line2 = curbuf->b_ml.ml_line_count;
2309 }
2310 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00002311
2312 /*
2313 * Check for flags: 'l', 'p' and '#'.
2314 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002315 if (ea.argt & EX_FLAGS)
Bram Moolenaardf177f62005-02-22 08:39:57 +00002316 get_flags(&ea);
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002317 if (!ni && !(ea.argt & EX_EXTRA) && *ea.arg != NUL
2318 && *ea.arg != '"' && (*ea.arg != '|' || (ea.argt & EX_TRLBAR) == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002319 {
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002320 // no arguments allowed but there is something
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002321 errormsg = _(e_trailing);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002322 goto doend;
2323 }
2324
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002325 if (!ni && (ea.argt & EX_NEEDARG) && *ea.arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002326 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002327 errormsg = _(e_argreq);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002328 goto doend;
2329 }
2330
2331#ifdef FEAT_EVAL
2332 /*
2333 * Skip the command when it's not going to be executed.
2334 * The commands like :if, :endif, etc. always need to be executed.
2335 * Also make an exception for commands that handle a trailing command
2336 * themselves.
2337 */
2338 if (ea.skip)
2339 {
2340 switch (ea.cmdidx)
2341 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002342 // commands that need evaluation
Bram Moolenaar071d4272004-06-13 20:20:40 +00002343 case CMD_while:
2344 case CMD_endwhile:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00002345 case CMD_for:
2346 case CMD_endfor:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002347 case CMD_if:
2348 case CMD_elseif:
2349 case CMD_else:
2350 case CMD_endif:
2351 case CMD_try:
2352 case CMD_catch:
2353 case CMD_finally:
2354 case CMD_endtry:
2355 case CMD_function:
2356 break;
2357
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002358 // Commands that handle '|' themselves. Check: A command should
2359 // either have the EX_TRLBAR flag, appear in this list or appear in
2360 // the list at ":help :bar".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002361 case CMD_aboveleft:
2362 case CMD_and:
2363 case CMD_belowright:
2364 case CMD_botright:
2365 case CMD_browse:
2366 case CMD_call:
2367 case CMD_confirm:
Bram Moolenaar8f76e6b2019-11-26 16:50:30 +01002368 case CMD_const:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002369 case CMD_delfunction:
2370 case CMD_djump:
2371 case CMD_dlist:
2372 case CMD_dsearch:
2373 case CMD_dsplit:
2374 case CMD_echo:
2375 case CMD_echoerr:
2376 case CMD_echomsg:
2377 case CMD_echon:
Bram Moolenaara76b3152020-02-16 16:20:21 +01002378 case CMD_eval:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002379 case CMD_execute:
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002380 case CMD_filter:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002381 case CMD_help:
2382 case CMD_hide:
2383 case CMD_ijump:
2384 case CMD_ilist:
2385 case CMD_isearch:
2386 case CMD_isplit:
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002387 case CMD_keepalt:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002388 case CMD_keepjumps:
2389 case CMD_keepmarks:
Bram Moolenaara939e432013-11-09 05:30:26 +01002390 case CMD_keeppatterns:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002391 case CMD_leftabove:
2392 case CMD_let:
2393 case CMD_lockmarks:
Bram Moolenaarcc4423a2019-11-26 17:05:00 +01002394 case CMD_lockvar:
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002395 case CMD_lua:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002396 case CMD_match:
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002397 case CMD_mzscheme:
Bram Moolenaar5803ae62014-03-23 16:04:02 +01002398 case CMD_noautocmd:
2399 case CMD_noswapfile:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002400 case CMD_perl:
2401 case CMD_psearch:
2402 case CMD_python:
Bram Moolenaar368373e2010-07-19 20:46:22 +02002403 case CMD_py3:
Bram Moolenaarb6590522010-07-21 16:00:43 +02002404 case CMD_python3:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002405 case CMD_return:
2406 case CMD_rightbelow:
2407 case CMD_ruby:
2408 case CMD_silent:
2409 case CMD_smagic:
2410 case CMD_snomagic:
2411 case CMD_substitute:
2412 case CMD_syntax:
Bram Moolenaara226a6d2006-02-26 23:59:20 +00002413 case CMD_tab:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002414 case CMD_tcl:
2415 case CMD_throw:
2416 case CMD_tilde:
2417 case CMD_topleft:
2418 case CMD_unlet:
Bram Moolenaarcc4423a2019-11-26 17:05:00 +01002419 case CMD_unlockvar:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002420 case CMD_verbose:
2421 case CMD_vertical:
Bram Moolenaar12bde492011-06-13 01:19:56 +02002422 case CMD_wincmd:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002423 break;
2424
2425 default: goto doend;
2426 }
2427 }
2428#endif
2429
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002430 if (ea.argt & EX_XFILE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002431 {
2432 if (expand_filename(&ea, cmdlinep, &errormsg) == FAIL)
2433 goto doend;
2434 }
2435
Bram Moolenaar071d4272004-06-13 20:20:40 +00002436 /*
2437 * Accept buffer name. Cannot be used at the same time with a buffer
2438 * number. Don't do this for a user command.
2439 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002440 if ((ea.argt & EX_BUFNAME) && *ea.arg != NUL && ea.addr_count == 0
Bram Moolenaar958636c2014-10-21 20:01:58 +02002441 && !IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002442 {
2443 /*
2444 * :bdelete, :bwipeout and :bunload take several arguments, separated
2445 * by spaces: find next space (skipping over escaped characters).
2446 * The others take one argument: ignore trailing spaces.
2447 */
2448 if (ea.cmdidx == CMD_bdelete || ea.cmdidx == CMD_bwipeout
2449 || ea.cmdidx == CMD_bunload)
2450 p = skiptowhite_esc(ea.arg);
2451 else
2452 {
2453 p = ea.arg + STRLEN(ea.arg);
Bram Moolenaar1c465442017-03-12 20:10:05 +01002454 while (p > ea.arg && VIM_ISWHITE(p[-1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002455 --p;
2456 }
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002457 ea.line2 = buflist_findpat(ea.arg, p, (ea.argt & EX_BUFUNL) != 0,
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002458 FALSE, FALSE);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002459 if (ea.line2 < 0) // failed
Bram Moolenaar071d4272004-06-13 20:20:40 +00002460 goto doend;
2461 ea.addr_count = 1;
2462 ea.arg = skipwhite(p);
2463 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002464
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002465 // The :try command saves the emsg_silent flag, reset it here when
2466 // ":silent! try" was used, it should only apply to :try itself.
Bram Moolenaareffed932018-08-14 13:38:17 +02002467 if (ea.cmdidx == CMD_try && ea.did_esilent > 0)
Bram Moolenaar2be57332018-02-13 18:05:18 +01002468 {
Bram Moolenaareffed932018-08-14 13:38:17 +02002469 emsg_silent -= ea.did_esilent;
Bram Moolenaar2be57332018-02-13 18:05:18 +01002470 if (emsg_silent < 0)
2471 emsg_silent = 0;
Bram Moolenaareffed932018-08-14 13:38:17 +02002472 ea.did_esilent = 0;
Bram Moolenaar2be57332018-02-13 18:05:18 +01002473 }
2474
Bram Moolenaar071d4272004-06-13 20:20:40 +00002475/*
Bram Moolenaar2be57332018-02-13 18:05:18 +01002476 * 7. Execute the command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002477 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002478
Bram Moolenaar958636c2014-10-21 20:01:58 +02002479 if (IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002480 {
2481 /*
2482 * Execute a user-defined command.
2483 */
2484 do_ucmd(&ea);
2485 }
2486 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002487 {
2488 /*
Bram Moolenaarac9fb182019-04-27 13:04:13 +02002489 * Call the function to execute the builtin command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002490 */
2491 ea.errmsg = NULL;
2492 (cmdnames[ea.cmdidx].cmd_func)(&ea);
2493 if (ea.errmsg != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002494 errormsg = _(ea.errmsg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002495 }
2496
2497#ifdef FEAT_EVAL
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002498 // Set flag that any command was executed, used by ex_vim9script().
Bram Moolenaar16531552020-02-08 16:00:46 +01002499 if (getline_equal(ea.getline, ea.cookie, getsourceline)
2500 && current_sctx.sc_sid > 0)
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002501 SCRIPT_ITEM(current_sctx.sc_sid)->sn_had_command = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002502
Bram Moolenaar071d4272004-06-13 20:20:40 +00002503 /*
2504 * If the command just executed called do_cmdline(), any throw or ":return"
2505 * or ":finish" encountered there must also check the cstack of the still
2506 * active do_cmdline() that called this do_one_cmd(). Rethrow an uncaught
2507 * exception, or reanimate a returned function or finished script file and
2508 * return or finish it again.
2509 */
2510 if (need_rethrow)
2511 do_throw(cstack);
2512 else if (check_cstack)
2513 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002514 if (source_finished(fgetline, cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002515 do_finish(&ea, TRUE);
Bram Moolenaar89d40322006-08-29 15:30:07 +00002516 else if (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002517 && current_func_returned())
2518 do_return(&ea, TRUE, FALSE, NULL);
2519 }
2520 need_rethrow = check_cstack = FALSE;
2521#endif
2522
2523doend:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002524 if (curwin->w_cursor.lnum == 0) // can happen with zero line number
Bram Moolenaar6de5e122017-04-20 21:55:44 +02002525 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002526 curwin->w_cursor.lnum = 1;
Bram Moolenaar6de5e122017-04-20 21:55:44 +02002527 curwin->w_cursor.col = 0;
2528 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002529
2530 if (errormsg != NULL && *errormsg != NUL && !did_emsg)
2531 {
2532 if (sourcing)
2533 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002534 if (errormsg != (char *)IObuff)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002535 {
2536 STRCPY(IObuff, errormsg);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002537 errormsg = (char *)IObuff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002538 }
Bram Moolenaara6f4d612011-09-21 19:10:46 +02002539 append_command(*cmdlinep);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002540 }
2541 emsg(errormsg);
2542 }
2543#ifdef FEAT_EVAL
2544 do_errthrow(cstack,
Bram Moolenaar958636c2014-10-21 20:01:58 +02002545 (ea.cmdidx != CMD_SIZE && !IS_USER_CMDIDX(ea.cmdidx))
2546 ? cmdnames[(int)ea.cmdidx].cmd_name : (char_u *)NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002547#endif
2548
Bram Moolenaareffed932018-08-14 13:38:17 +02002549 if (ea.verbose_save >= 0)
2550 p_verbose = ea.verbose_save;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002551
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002552 free_cmdmod();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002553 cmdmod = save_cmdmod;
Bram Moolenaar9a2c0912019-03-30 14:26:18 +01002554 reg_executing = save_reg_executing;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002555
Bram Moolenaareffed932018-08-14 13:38:17 +02002556 if (ea.save_msg_silent != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002557 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002558 // messages could be enabled for a serious error, need to check if the
2559 // counters don't become negative
Bram Moolenaareffed932018-08-14 13:38:17 +02002560 if (!did_emsg || msg_silent > ea.save_msg_silent)
2561 msg_silent = ea.save_msg_silent;
2562 emsg_silent -= ea.did_esilent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002563 if (emsg_silent < 0)
2564 emsg_silent = 0;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002565 // Restore msg_scroll, it's set by file I/O commands, even when no
2566 // message is actually displayed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002567 msg_scroll = save_msg_scroll;
Bram Moolenaar77ab2802009-04-22 12:44:48 +00002568
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002569 // "silent reg" or "silent echo x" inside "redir" leaves msg_col
2570 // somewhere in the line. Put it back in the first column.
Bram Moolenaar77ab2802009-04-22 12:44:48 +00002571 if (redirecting())
2572 msg_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002573 }
2574
Bram Moolenaar7171abe2004-10-11 10:06:20 +00002575#ifdef HAVE_SANDBOX
Bram Moolenaareffed932018-08-14 13:38:17 +02002576 if (ea.did_sandbox)
Bram Moolenaar7171abe2004-10-11 10:06:20 +00002577 --sandbox;
2578#endif
2579
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002580 if (ea.nextcmd && *ea.nextcmd == NUL) // not really a next command
Bram Moolenaar071d4272004-06-13 20:20:40 +00002581 ea.nextcmd = NULL;
2582
2583#ifdef FEAT_EVAL
2584 --ex_nesting_level;
2585#endif
2586
2587 return ea.nextcmd;
2588}
2589#if (_MSC_VER == 1200)
Bram Moolenaar281bdce2005-01-25 21:53:18 +00002590 #pragma optimize( "", on )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002591#endif
2592
2593/*
Bram Moolenaareffed932018-08-14 13:38:17 +02002594 * Parse and skip over command modifiers:
2595 * - update eap->cmd
2596 * - store flags in "cmdmod".
2597 * - Set ex_pressedreturn for an empty command line.
2598 * - set msg_silent for ":silent"
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002599 * - set 'eventignore' to "all" for ":noautocmd"
Bram Moolenaareffed932018-08-14 13:38:17 +02002600 * - set p_verbose for ":verbose"
2601 * - Increment "sandbox" for ":sandbox"
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002602 * When "skip_only" is TRUE the global variables are not changed, except for
2603 * "cmdmod".
Bram Moolenaareffed932018-08-14 13:38:17 +02002604 * Return FAIL when the command is not to be executed.
2605 * May set "errormsg" to an error message.
2606 */
2607 int
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002608parse_command_modifiers(exarg_T *eap, char **errormsg, int skip_only)
Bram Moolenaareffed932018-08-14 13:38:17 +02002609{
2610 char_u *p;
2611
2612 vim_memset(&cmdmod, 0, sizeof(cmdmod));
2613 eap->verbose_save = -1;
2614 eap->save_msg_silent = -1;
2615
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002616 // Repeat until no more command modifiers are found.
Bram Moolenaareffed932018-08-14 13:38:17 +02002617 for (;;)
2618 {
Bram Moolenaareffed932018-08-14 13:38:17 +02002619 while (*eap->cmd == ' ' || *eap->cmd == '\t' || *eap->cmd == ':')
2620 ++eap->cmd;
2621
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002622 // in ex mode, an empty line works like :+
Bram Moolenaareffed932018-08-14 13:38:17 +02002623 if (*eap->cmd == NUL && exmode_active
2624 && (getline_equal(eap->getline, eap->cookie, getexmodeline)
2625 || getline_equal(eap->getline, eap->cookie, getexline))
2626 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
2627 {
2628 eap->cmd = (char_u *)"+";
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002629 if (!skip_only)
2630 ex_pressedreturn = TRUE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002631 }
2632
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002633 // ignore comment and empty lines
Bram Moolenaareffed932018-08-14 13:38:17 +02002634 if (*eap->cmd == '"')
2635 return FAIL;
2636 if (*eap->cmd == NUL)
2637 {
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002638 if (!skip_only)
2639 ex_pressedreturn = TRUE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002640 return FAIL;
2641 }
2642
Bram Moolenaareffed932018-08-14 13:38:17 +02002643 p = skip_range(eap->cmd, NULL);
2644 switch (*p)
2645 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002646 // When adding an entry, also modify cmd_exists().
Bram Moolenaareffed932018-08-14 13:38:17 +02002647 case 'a': if (!checkforcmd(&eap->cmd, "aboveleft", 3))
2648 break;
2649 cmdmod.split |= WSP_ABOVE;
2650 continue;
2651
2652 case 'b': if (checkforcmd(&eap->cmd, "belowright", 3))
2653 {
2654 cmdmod.split |= WSP_BELOW;
2655 continue;
2656 }
2657 if (checkforcmd(&eap->cmd, "browse", 3))
2658 {
2659#ifdef FEAT_BROWSE_CMD
2660 cmdmod.browse = TRUE;
2661#endif
2662 continue;
2663 }
2664 if (!checkforcmd(&eap->cmd, "botright", 2))
2665 break;
2666 cmdmod.split |= WSP_BOT;
2667 continue;
2668
2669 case 'c': if (!checkforcmd(&eap->cmd, "confirm", 4))
2670 break;
2671#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
2672 cmdmod.confirm = TRUE;
2673#endif
2674 continue;
2675
2676 case 'k': if (checkforcmd(&eap->cmd, "keepmarks", 3))
2677 {
2678 cmdmod.keepmarks = TRUE;
2679 continue;
2680 }
2681 if (checkforcmd(&eap->cmd, "keepalt", 5))
2682 {
2683 cmdmod.keepalt = TRUE;
2684 continue;
2685 }
2686 if (checkforcmd(&eap->cmd, "keeppatterns", 5))
2687 {
2688 cmdmod.keeppatterns = TRUE;
2689 continue;
2690 }
2691 if (!checkforcmd(&eap->cmd, "keepjumps", 5))
2692 break;
2693 cmdmod.keepjumps = TRUE;
2694 continue;
2695
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002696 case 'f': // only accept ":filter {pat} cmd"
Bram Moolenaareffed932018-08-14 13:38:17 +02002697 {
2698 char_u *reg_pat;
2699
2700 if (!checkforcmd(&p, "filter", 4)
2701 || *p == NUL || ends_excmd(*p))
2702 break;
2703 if (*p == '!')
2704 {
2705 cmdmod.filter_force = TRUE;
2706 p = skipwhite(p + 1);
2707 if (*p == NUL || ends_excmd(*p))
2708 break;
2709 }
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002710 if (skip_only)
2711 p = skip_vimgrep_pat(p, NULL, NULL);
2712 else
2713 // NOTE: This puts a NUL after the pattern.
2714 p = skip_vimgrep_pat(p, &reg_pat, NULL);
Bram Moolenaareffed932018-08-14 13:38:17 +02002715 if (p == NULL || *p == NUL)
2716 break;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002717 if (!skip_only)
2718 {
2719 cmdmod.filter_regmatch.regprog =
Bram Moolenaareffed932018-08-14 13:38:17 +02002720 vim_regcomp(reg_pat, RE_MAGIC);
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002721 if (cmdmod.filter_regmatch.regprog == NULL)
2722 break;
2723 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002724 eap->cmd = p;
2725 continue;
2726 }
2727
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002728 // ":hide" and ":hide | cmd" are not modifiers
Bram Moolenaareffed932018-08-14 13:38:17 +02002729 case 'h': if (p != eap->cmd || !checkforcmd(&p, "hide", 3)
2730 || *p == NUL || ends_excmd(*p))
2731 break;
2732 eap->cmd = p;
2733 cmdmod.hide = TRUE;
2734 continue;
2735
2736 case 'l': if (checkforcmd(&eap->cmd, "lockmarks", 3))
2737 {
2738 cmdmod.lockmarks = TRUE;
2739 continue;
2740 }
2741
2742 if (!checkforcmd(&eap->cmd, "leftabove", 5))
2743 break;
2744 cmdmod.split |= WSP_ABOVE;
2745 continue;
2746
2747 case 'n': if (checkforcmd(&eap->cmd, "noautocmd", 3))
2748 {
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002749 if (cmdmod.save_ei == NULL && !skip_only)
Bram Moolenaareffed932018-08-14 13:38:17 +02002750 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002751 // Set 'eventignore' to "all". Restore the
2752 // existing option value later.
Bram Moolenaareffed932018-08-14 13:38:17 +02002753 cmdmod.save_ei = vim_strsave(p_ei);
2754 set_string_option_direct((char_u *)"ei", -1,
2755 (char_u *)"all", OPT_FREE, SID_NONE);
2756 }
2757 continue;
2758 }
2759 if (!checkforcmd(&eap->cmd, "noswapfile", 3))
2760 break;
2761 cmdmod.noswapfile = TRUE;
2762 continue;
2763
2764 case 'r': if (!checkforcmd(&eap->cmd, "rightbelow", 6))
2765 break;
2766 cmdmod.split |= WSP_BELOW;
2767 continue;
2768
2769 case 's': if (checkforcmd(&eap->cmd, "sandbox", 3))
2770 {
2771#ifdef HAVE_SANDBOX
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002772 if (!skip_only)
2773 {
2774 if (!eap->did_sandbox)
2775 ++sandbox;
2776 eap->did_sandbox = TRUE;
2777 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002778#endif
2779 continue;
2780 }
2781 if (!checkforcmd(&eap->cmd, "silent", 3))
2782 break;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002783 if (!skip_only)
2784 {
2785 if (eap->save_msg_silent == -1)
2786 eap->save_msg_silent = msg_silent;
2787 ++msg_silent;
2788 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002789 if (*eap->cmd == '!' && !VIM_ISWHITE(eap->cmd[-1]))
2790 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002791 // ":silent!", but not "silent !cmd"
Bram Moolenaareffed932018-08-14 13:38:17 +02002792 eap->cmd = skipwhite(eap->cmd + 1);
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002793 if (!skip_only)
2794 {
2795 ++emsg_silent;
2796 ++eap->did_esilent;
2797 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002798 }
2799 continue;
2800
2801 case 't': if (checkforcmd(&p, "tab", 3))
2802 {
Bram Moolenaar548e5982018-12-26 21:45:00 +01002803 if (!skip_only)
Bram Moolenaareffed932018-08-14 13:38:17 +02002804 {
Bram Moolenaar548e5982018-12-26 21:45:00 +01002805 long tabnr = get_address(eap, &eap->cmd,
2806 ADDR_TABS, eap->skip,
2807 skip_only, FALSE, 1);
2808 if (tabnr == MAXLNUM)
2809 cmdmod.tab = tabpage_index(curtab) + 1;
2810 else
Bram Moolenaareffed932018-08-14 13:38:17 +02002811 {
Bram Moolenaar548e5982018-12-26 21:45:00 +01002812 if (tabnr < 0 || tabnr > LAST_TAB_NR)
2813 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002814 *errormsg = _(e_invrange);
Bram Moolenaar548e5982018-12-26 21:45:00 +01002815 return FAIL;
2816 }
2817 cmdmod.tab = tabnr + 1;
Bram Moolenaareffed932018-08-14 13:38:17 +02002818 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002819 }
2820 eap->cmd = p;
2821 continue;
2822 }
2823 if (!checkforcmd(&eap->cmd, "topleft", 2))
2824 break;
2825 cmdmod.split |= WSP_TOP;
2826 continue;
2827
2828 case 'u': if (!checkforcmd(&eap->cmd, "unsilent", 3))
2829 break;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002830 if (!skip_only)
2831 {
2832 if (eap->save_msg_silent == -1)
2833 eap->save_msg_silent = msg_silent;
2834 msg_silent = 0;
2835 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002836 continue;
2837
2838 case 'v': if (checkforcmd(&eap->cmd, "vertical", 4))
2839 {
2840 cmdmod.split |= WSP_VERT;
2841 continue;
2842 }
2843 if (!checkforcmd(&p, "verbose", 4))
2844 break;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002845 if (!skip_only)
2846 {
2847 if (eap->verbose_save < 0)
2848 eap->verbose_save = p_verbose;
2849 if (vim_isdigit(*eap->cmd))
2850 p_verbose = atoi((char *)eap->cmd);
2851 else
2852 p_verbose = 1;
2853 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002854 eap->cmd = p;
2855 continue;
2856 }
2857 break;
2858 }
2859
2860 return OK;
2861}
2862
2863/*
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002864 * Free contents of "cmdmod".
2865 */
2866 static void
2867free_cmdmod(void)
2868{
2869 if (cmdmod.save_ei != NULL)
2870 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002871 // Restore 'eventignore' to the value before ":noautocmd".
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002872 set_string_option_direct((char_u *)"ei", -1, cmdmod.save_ei,
2873 OPT_FREE, SID_NONE);
2874 free_string_option(cmdmod.save_ei);
2875 }
2876
2877 if (cmdmod.filter_regmatch.regprog != NULL)
2878 vim_regfree(cmdmod.filter_regmatch.regprog);
2879}
2880
2881/*
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002882 * Parse the address range, if any, in "eap".
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02002883 * May set the last search pattern, unless "silent" is TRUE.
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002884 * Return FAIL and set "errormsg" or return OK.
2885 */
2886 int
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002887parse_cmd_address(exarg_T *eap, char **errormsg, int silent)
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002888{
2889 int address_count = 1;
2890 linenr_T lnum;
2891
2892 // Repeat for all ',' or ';' separated addresses.
2893 for (;;)
2894 {
2895 eap->line1 = eap->line2;
2896 switch (eap->addr_type)
2897 {
2898 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02002899 case ADDR_OTHER:
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002900 // default is current line number
2901 eap->line2 = curwin->w_cursor.lnum;
2902 break;
2903 case ADDR_WINDOWS:
2904 eap->line2 = CURRENT_WIN_NR;
2905 break;
2906 case ADDR_ARGUMENTS:
2907 eap->line2 = curwin->w_arg_idx + 1;
2908 if (eap->line2 > ARGCOUNT)
2909 eap->line2 = ARGCOUNT;
2910 break;
2911 case ADDR_LOADED_BUFFERS:
2912 case ADDR_BUFFERS:
2913 eap->line2 = curbuf->b_fnum;
2914 break;
2915 case ADDR_TABS:
2916 eap->line2 = CURRENT_TAB_NR;
2917 break;
2918 case ADDR_TABS_RELATIVE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02002919 case ADDR_UNSIGNED:
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002920 eap->line2 = 1;
2921 break;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002922 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02002923#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02002924 eap->line2 = qf_get_cur_idx(eap);
2925#endif
2926 break;
2927 case ADDR_QUICKFIX_VALID:
2928#ifdef FEAT_QUICKFIX
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002929 eap->line2 = qf_get_cur_valid_idx(eap);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002930#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02002931 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02002932 case ADDR_NONE:
2933 // Will give an error later if a range is found.
2934 break;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002935 }
2936 eap->cmd = skipwhite(eap->cmd);
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02002937 lnum = get_address(eap, &eap->cmd, eap->addr_type, eap->skip, silent,
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002938 eap->addr_count == 0, address_count++);
2939 if (eap->cmd == NULL) // error detected
2940 return FAIL;
2941 if (lnum == MAXLNUM)
2942 {
2943 if (*eap->cmd == '%') // '%' - all lines
2944 {
2945 ++eap->cmd;
2946 switch (eap->addr_type)
2947 {
2948 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02002949 case ADDR_OTHER:
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002950 eap->line1 = 1;
2951 eap->line2 = curbuf->b_ml.ml_line_count;
2952 break;
2953 case ADDR_LOADED_BUFFERS:
2954 {
2955 buf_T *buf = firstbuf;
2956
2957 while (buf->b_next != NULL
2958 && buf->b_ml.ml_mfp == NULL)
2959 buf = buf->b_next;
2960 eap->line1 = buf->b_fnum;
2961 buf = lastbuf;
2962 while (buf->b_prev != NULL
2963 && buf->b_ml.ml_mfp == NULL)
2964 buf = buf->b_prev;
2965 eap->line2 = buf->b_fnum;
2966 break;
2967 }
2968 case ADDR_BUFFERS:
2969 eap->line1 = firstbuf->b_fnum;
2970 eap->line2 = lastbuf->b_fnum;
2971 break;
2972 case ADDR_WINDOWS:
2973 case ADDR_TABS:
2974 if (IS_USER_CMDIDX(eap->cmdidx))
2975 {
2976 eap->line1 = 1;
2977 eap->line2 = eap->addr_type == ADDR_WINDOWS
2978 ? LAST_WIN_NR : LAST_TAB_NR;
2979 }
2980 else
2981 {
2982 // there is no Vim command which uses '%' and
2983 // ADDR_WINDOWS or ADDR_TABS
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002984 *errormsg = _(e_invrange);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002985 return FAIL;
2986 }
2987 break;
2988 case ADDR_TABS_RELATIVE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02002989 case ADDR_UNSIGNED:
2990 case ADDR_QUICKFIX:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002991 *errormsg = _(e_invrange);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002992 return FAIL;
2993 case ADDR_ARGUMENTS:
2994 if (ARGCOUNT == 0)
2995 eap->line1 = eap->line2 = 0;
2996 else
2997 {
2998 eap->line1 = 1;
2999 eap->line2 = ARGCOUNT;
3000 }
3001 break;
Bram Moolenaar25190db2019-05-04 15:05:28 +02003002 case ADDR_QUICKFIX_VALID:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003003#ifdef FEAT_QUICKFIX
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003004 eap->line1 = 1;
Bram Moolenaar25190db2019-05-04 15:05:28 +02003005 eap->line2 = qf_get_valid_size(eap);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003006 if (eap->line2 == 0)
3007 eap->line2 = 1;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003008#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003009 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02003010 case ADDR_NONE:
3011 // Will give an error later if a range is found.
3012 break;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003013 }
3014 ++eap->addr_count;
3015 }
3016 else if (*eap->cmd == '*' && vim_strchr(p_cpo, CPO_STAR) == NULL)
3017 {
3018 pos_T *fp;
3019
3020 // '*' - visual area
3021 if (eap->addr_type != ADDR_LINES)
3022 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003023 *errormsg = _(e_invrange);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003024 return FAIL;
3025 }
3026
3027 ++eap->cmd;
3028 if (!eap->skip)
3029 {
3030 fp = getmark('<', FALSE);
3031 if (check_mark(fp) == FAIL)
3032 return FAIL;
3033 eap->line1 = fp->lnum;
3034 fp = getmark('>', FALSE);
3035 if (check_mark(fp) == FAIL)
3036 return FAIL;
3037 eap->line2 = fp->lnum;
3038 ++eap->addr_count;
3039 }
3040 }
3041 }
3042 else
3043 eap->line2 = lnum;
3044 eap->addr_count++;
3045
3046 if (*eap->cmd == ';')
3047 {
3048 if (!eap->skip)
3049 {
3050 curwin->w_cursor.lnum = eap->line2;
3051 // don't leave the cursor on an illegal line or column
3052 check_cursor();
3053 }
3054 }
3055 else if (*eap->cmd != ',')
3056 break;
3057 ++eap->cmd;
3058 }
3059
3060 // One address given: set start and end lines.
3061 if (eap->addr_count == 1)
3062 {
3063 eap->line1 = eap->line2;
3064 // ... but only implicit: really no address given
3065 if (lnum == MAXLNUM)
3066 eap->addr_count = 0;
3067 }
3068 return OK;
3069}
3070
3071/*
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00003072 * Check for an Ex command with optional tail.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003073 * If there is a match advance "pp" to the argument and return TRUE.
3074 */
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00003075 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003076checkforcmd(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003077 char_u **pp, // start of command
3078 char *cmd, // name of command
3079 int len) // required length
Bram Moolenaar071d4272004-06-13 20:20:40 +00003080{
3081 int i;
3082
3083 for (i = 0; cmd[i] != NUL; ++i)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003084 if (((char_u *)cmd)[i] != (*pp)[i])
Bram Moolenaar071d4272004-06-13 20:20:40 +00003085 break;
3086 if (i >= len && !isalpha((*pp)[i]))
3087 {
3088 *pp = skipwhite(*pp + i);
3089 return TRUE;
3090 }
3091 return FALSE;
3092}
3093
3094/*
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003095 * Append "cmd" to the error message in IObuff.
3096 * Takes care of limiting the length and handling 0xa0, which would be
3097 * invisible otherwise.
3098 */
3099 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003100append_command(char_u *cmd)
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003101{
3102 char_u *s = cmd;
3103 char_u *d;
3104
3105 STRCAT(IObuff, ": ");
3106 d = IObuff + STRLEN(IObuff);
3107 while (*s != NUL && d - IObuff < IOSIZE - 7)
3108 {
Bram Moolenaar13505972019-01-24 15:04:48 +01003109 if (enc_utf8 ? (s[0] == 0xc2 && s[1] == 0xa0) : *s == 0xa0)
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003110 {
Bram Moolenaar13505972019-01-24 15:04:48 +01003111 s += enc_utf8 ? 2 : 1;
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003112 STRCPY(d, "<a0>");
3113 d += 4;
3114 }
3115 else
3116 MB_COPY_CHAR(s, d);
3117 }
3118 *d = NUL;
3119}
3120
3121/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003122 * Find an Ex command by its name, either built-in or user.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003123 * Start of the name can be found at eap->cmd.
Bram Moolenaar25190db2019-05-04 15:05:28 +02003124 * Sets eap->cmdidx and returns a pointer to char after the command name.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003125 * "full" is set to TRUE if the whole command name matched.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003126 *
3127 * If "lookup" is not NULL recognize expression without "eval" or "call" and
3128 * assignment without "let". Sets eap->cmdidx to the command while returning
3129 * "eap->cmd".
3130 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00003131 * Returns NULL for an ambiguous user command.
3132 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003133 char_u *
3134find_ex_command(
3135 exarg_T *eap,
3136 int *full UNUSED,
3137 int (*lookup)(char_u *, size_t, cctx_T *) UNUSED,
3138 cctx_T *cctx UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003139{
3140 int len;
3141 char_u *p;
Bram Moolenaardf177f62005-02-22 08:39:57 +00003142 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003143
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003144#ifdef FEAT_EVAL
3145 /*
3146 * Recognize a Vim9 script function/method call and assignment:
3147 * "lvar = value", "lvar(arg)", "[1, 2 3]->Func()"
3148 */
3149 if (lookup != NULL && (p = to_name_const_end(eap->cmd)) > eap->cmd
3150 && *p != NUL)
3151 {
3152 int oplen;
3153 int heredoc;
3154
3155 // "funcname(" is always a function call.
3156 // "varname[]" is an expression.
3157 // "g:varname" is an expression.
3158 // "varname->expr" is an expression.
3159 if (*p == '('
3160 || *p == '['
3161 || p[1] == ':'
3162 || (*p == '-' && p[1] == '>'))
3163 {
3164 eap->cmdidx = CMD_eval;
3165 return eap->cmd;
3166 }
3167
3168 oplen = assignment_len(skipwhite(p), &heredoc);
3169 if (oplen > 0)
3170 {
3171 // Recognize an assignment if we recognize the variable name:
3172 // "g:var = expr"
3173 // "var = expr" where "var" is a local var name.
3174 if (((p - eap->cmd) > 2 && eap->cmd[1] == ':')
3175 || lookup(eap->cmd, p - eap->cmd, cctx) >= 0)
3176 {
3177 eap->cmdidx = CMD_let;
3178 return eap->cmd;
3179 }
3180 }
3181 }
3182#endif
3183
Bram Moolenaar071d4272004-06-13 20:20:40 +00003184 /*
3185 * Isolate the command and search for it in the command table.
Bram Moolenaar81870892007-11-11 18:17:28 +00003186 * Exceptions:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003187 * - the 'k' command can directly be followed by any character.
3188 * - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003189 * but :sre[wind] is another command, as are :scr[iptnames],
Bram Moolenaar071d4272004-06-13 20:20:40 +00003190 * :scs[cope], :sim[alt], :sig[ns] and :sil[ent].
Bram Moolenaardf177f62005-02-22 08:39:57 +00003191 * - the "d" command can directly be followed by 'l' or 'p' flag.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003192 */
3193 p = eap->cmd;
3194 if (*p == 'k')
3195 {
3196 eap->cmdidx = CMD_k;
3197 ++p;
3198 }
3199 else if (p[0] == 's'
Bram Moolenaar204b93f2015-08-04 22:02:51 +02003200 && ((p[1] == 'c' && (p[2] == NUL || (p[2] != 's' && p[2] != 'r'
3201 && (p[3] == NUL || (p[3] != 'i' && p[4] != 'p')))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003202 || p[1] == 'g'
3203 || (p[1] == 'i' && p[2] != 'm' && p[2] != 'l' && p[2] != 'g')
3204 || p[1] == 'I'
3205 || (p[1] == 'r' && p[2] != 'e')))
3206 {
3207 eap->cmdidx = CMD_substitute;
3208 ++p;
3209 }
3210 else
3211 {
3212 while (ASCII_ISALPHA(*p))
3213 ++p;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003214 // for python 3.x support ":py3", ":python3", ":py3file", etc.
Bram Moolenaar55d5c032010-07-17 23:52:29 +02003215 if (eap->cmd[0] == 'p' && eap->cmd[1] == 'y')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003216 {
Bram Moolenaarb6590522010-07-21 16:00:43 +02003217 while (ASCII_ISALNUM(*p))
3218 ++p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003219 }
3220 else if (*p == '9' && STRNCMP("vim9", eap->cmd, 4) == 0)
3221 {
3222 // include "9" for "vim9script"
3223 ++p;
3224 while (ASCII_ISALPHA(*p))
3225 ++p;
3226 }
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02003227
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003228 // check for non-alpha command
Bram Moolenaar071d4272004-06-13 20:20:40 +00003229 if (p == eap->cmd && vim_strchr((char_u *)"@*!=><&~#", *p) != NULL)
3230 ++p;
3231 len = (int)(p - eap->cmd);
Bram Moolenaardf177f62005-02-22 08:39:57 +00003232 if (*eap->cmd == 'd' && (p[-1] == 'l' || p[-1] == 'p'))
3233 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003234 // Check for ":dl", ":dell", etc. to ":deletel": that's
3235 // :delete with the 'l' flag. Same for 'p'.
Bram Moolenaardf177f62005-02-22 08:39:57 +00003236 for (i = 0; i < len; ++i)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003237 if (eap->cmd[i] != ((char_u *)"delete")[i])
Bram Moolenaardf177f62005-02-22 08:39:57 +00003238 break;
3239 if (i == len - 1)
3240 {
3241 --len;
3242 if (p[-1] == 'l')
3243 eap->flags |= EXFLAG_LIST;
3244 else
3245 eap->flags |= EXFLAG_PRINT;
3246 }
3247 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003248
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003249 if (ASCII_ISLOWER(eap->cmd[0]))
3250 {
Bram Moolenaar6c0c1e82017-03-25 15:07:43 +01003251 int c1 = eap->cmd[0];
Bram Moolenaar94f82cb2019-07-24 22:30:27 +02003252 int c2 = len == 1 ? NUL : eap->cmd[1];
Bram Moolenaar6c0c1e82017-03-25 15:07:43 +01003253
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003254 if (command_count != (int)CMD_SIZE)
3255 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003256 iemsg(_("E943: Command table needs to be updated, run 'make cmdidxs'"));
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003257 getout(1);
3258 }
3259
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003260 // Use a precomputed index for fast look-up in cmdnames[]
3261 // taking into account the first 2 letters of eap->cmd.
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003262 eap->cmdidx = cmdidxs1[CharOrdLow(c1)];
3263 if (ASCII_ISLOWER(c2))
3264 eap->cmdidx += cmdidxs2[CharOrdLow(c1)][CharOrdLow(c2)];
3265 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003266 else
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003267 eap->cmdidx = CMD_bang;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003268
3269 for ( ; (int)eap->cmdidx < (int)CMD_SIZE;
3270 eap->cmdidx = (cmdidx_T)((int)eap->cmdidx + 1))
3271 if (STRNCMP(cmdnames[(int)eap->cmdidx].cmd_name, (char *)eap->cmd,
3272 (size_t)len) == 0)
3273 {
3274#ifdef FEAT_EVAL
3275 if (full != NULL
3276 && cmdnames[(int)eap->cmdidx].cmd_name[len] == NUL)
3277 *full = TRUE;
3278#endif
3279 break;
3280 }
3281
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003282 // Look for a user defined command as a last resort. Let ":Print" be
3283 // overruled by a user defined command.
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01003284 if ((eap->cmdidx == CMD_SIZE || eap->cmdidx == CMD_Print)
3285 && *eap->cmd >= 'A' && *eap->cmd <= 'Z')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003286 {
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003287 // User defined commands may contain digits.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003288 while (ASCII_ISALNUM(*p))
3289 ++p;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003290 p = find_ucmd(eap, p, full, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003291 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003292 if (p == eap->cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003293 eap->cmdidx = CMD_SIZE;
3294 }
3295
3296 return p;
3297}
3298
3299#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003300static struct cmdmod
3301{
3302 char *name;
3303 int minlen;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003304 int has_count; // :123verbose :3tab
Bram Moolenaared53fb92007-11-24 20:50:24 +00003305} cmdmods[] = {
3306 {"aboveleft", 3, FALSE},
3307 {"belowright", 3, FALSE},
3308 {"botright", 2, FALSE},
3309 {"browse", 3, FALSE},
3310 {"confirm", 4, FALSE},
Bram Moolenaar7b668e82016-08-23 23:51:21 +02003311 {"filter", 4, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003312 {"hide", 3, FALSE},
3313 {"keepalt", 5, FALSE},
3314 {"keepjumps", 5, FALSE},
3315 {"keepmarks", 3, FALSE},
Bram Moolenaara939e432013-11-09 05:30:26 +01003316 {"keeppatterns", 5, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003317 {"leftabove", 5, FALSE},
3318 {"lockmarks", 3, FALSE},
Bram Moolenaarca9f9582008-09-18 10:44:28 +00003319 {"noautocmd", 3, FALSE},
Bram Moolenaar5803ae62014-03-23 16:04:02 +01003320 {"noswapfile", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003321 {"rightbelow", 6, FALSE},
3322 {"sandbox", 3, FALSE},
3323 {"silent", 3, FALSE},
3324 {"tab", 3, TRUE},
3325 {"topleft", 2, FALSE},
Bram Moolenaar8e258a42009-07-09 13:55:43 +00003326 {"unsilent", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003327 {"verbose", 4, TRUE},
3328 {"vertical", 4, FALSE},
3329};
3330
3331/*
3332 * Return length of a command modifier (including optional count).
3333 * Return zero when it's not a modifier.
3334 */
3335 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003336modifier_len(char_u *cmd)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003337{
3338 int i, j;
3339 char_u *p = cmd;
3340
3341 if (VIM_ISDIGIT(*cmd))
3342 p = skipwhite(skipdigits(cmd));
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00003343 for (i = 0; i < (int)(sizeof(cmdmods) / sizeof(struct cmdmod)); ++i)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003344 {
3345 for (j = 0; p[j] != NUL; ++j)
3346 if (p[j] != cmdmods[i].name[j])
3347 break;
Bram Moolenaar2d473ab2013-06-12 17:12:24 +02003348 if (!ASCII_ISALPHA(p[j]) && j >= cmdmods[i].minlen
Bram Moolenaared53fb92007-11-24 20:50:24 +00003349 && (p == cmd || cmdmods[i].has_count))
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00003350 return j + (int)(p - cmd);
Bram Moolenaared53fb92007-11-24 20:50:24 +00003351 }
3352 return 0;
3353}
3354
Bram Moolenaar071d4272004-06-13 20:20:40 +00003355/*
3356 * Return > 0 if an Ex command "name" exists.
3357 * Return 2 if there is an exact match.
3358 * Return 3 if there is an ambiguous match.
3359 */
3360 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003361cmd_exists(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003362{
3363 exarg_T ea;
3364 int full = FALSE;
3365 int i;
3366 int j;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003367 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003368
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003369 // Check command modifiers.
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00003370 for (i = 0; i < (int)(sizeof(cmdmods) / sizeof(struct cmdmod)); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003371 {
3372 for (j = 0; name[j] != NUL; ++j)
3373 if (name[j] != cmdmods[i].name[j])
3374 break;
3375 if (name[j] == NUL && j >= cmdmods[i].minlen)
3376 return (cmdmods[i].name[j] == NUL ? 2 : 1);
3377 }
3378
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003379 // Check built-in commands and user defined commands.
3380 // For ":2match" and ":3match" we need to skip the number.
Bram Moolenaara9587612006-05-04 21:47:50 +00003381 ea.cmd = (*name == '2' || *name == '3') ? name + 1 : name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003382 ea.cmdidx = (cmdidx_T)0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003383 p = find_ex_command(&ea, &full, NULL, NULL);
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003384 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003385 return 3;
Bram Moolenaara9587612006-05-04 21:47:50 +00003386 if (vim_isdigit(*name) && ea.cmdidx != CMD_match)
3387 return 0;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003388 if (*skipwhite(p) != NUL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003389 return 0; // trailing garbage
Bram Moolenaar071d4272004-06-13 20:20:40 +00003390 return (ea.cmdidx == CMD_SIZE ? 0 : (full ? 2 : 1));
3391}
3392#endif
3393
Bram Moolenaard0190392019-08-23 21:17:35 +02003394 cmdidx_T
3395excmd_get_cmdidx(char_u *cmd, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003396{
Bram Moolenaard0190392019-08-23 21:17:35 +02003397 cmdidx_T idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003398
Bram Moolenaard0190392019-08-23 21:17:35 +02003399 for (idx = (cmdidx_T)0; (int)idx < (int)CMD_SIZE;
3400 idx = (cmdidx_T)((int)idx + 1))
3401 if (STRNCMP(cmdnames[(int)idx].cmd_name, cmd, (size_t)len) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003402 break;
3403
Bram Moolenaard0190392019-08-23 21:17:35 +02003404 return idx;
3405}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003406
Bram Moolenaard0190392019-08-23 21:17:35 +02003407 long
3408excmd_get_argt(cmdidx_T idx)
3409{
3410 return (long)cmdnames[(int)idx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003411}
3412
3413/*
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003414 * Skip a range specifier of the form: addr [,addr] [;addr] ..
Bram Moolenaar071d4272004-06-13 20:20:40 +00003415 *
3416 * Backslashed delimiters after / or ? will be skipped, and commands will
3417 * not be expanded between /'s and ?'s or after "'".
3418 *
Bram Moolenaardf177f62005-02-22 08:39:57 +00003419 * Also skip white space and ":" characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003420 * Returns the "cmd" pointer advanced to beyond the range.
3421 */
3422 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003423skip_range(
3424 char_u *cmd,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003425 int *ctx) // pointer to xp_context or NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003426{
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003427 unsigned delim;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003428
Bram Moolenaarcbf20fb2017-02-03 21:19:04 +01003429 while (vim_strchr((char_u *)" \t0123456789.$%'/?-+,;\\", *cmd) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003430 {
Bram Moolenaarcbf20fb2017-02-03 21:19:04 +01003431 if (*cmd == '\\')
3432 {
3433 if (cmd[1] == '?' || cmd[1] == '/' || cmd[1] == '&')
3434 ++cmd;
3435 else
3436 break;
3437 }
3438 else if (*cmd == '\'')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003439 {
3440 if (*++cmd == NUL && ctx != NULL)
3441 *ctx = EXPAND_NOTHING;
3442 }
3443 else if (*cmd == '/' || *cmd == '?')
3444 {
3445 delim = *cmd++;
3446 while (*cmd != NUL && *cmd != delim)
3447 if (*cmd++ == '\\' && *cmd != NUL)
3448 ++cmd;
3449 if (*cmd == NUL && ctx != NULL)
3450 *ctx = EXPAND_NOTHING;
3451 }
3452 if (*cmd != NUL)
3453 ++cmd;
3454 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00003455
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003456 // Skip ":" and white space.
Bram Moolenaardf177f62005-02-22 08:39:57 +00003457 while (*cmd == ':')
3458 cmd = skipwhite(cmd + 1);
3459
Bram Moolenaar071d4272004-06-13 20:20:40 +00003460 return cmd;
3461}
3462
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003463 static void
3464addr_error(cmd_addr_T addr_type)
3465{
3466 if (addr_type == ADDR_NONE)
3467 emsg(_(e_norange));
3468 else
3469 emsg(_(e_invrange));
3470}
3471
Bram Moolenaar071d4272004-06-13 20:20:40 +00003472/*
Bram Moolenaar198cb662018-09-06 21:44:17 +02003473 * Get a single EX address.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003474 *
3475 * Set ptr to the next character after the part that was interpreted.
3476 * Set ptr to NULL when an error is encountered.
Bram Moolenaar198cb662018-09-06 21:44:17 +02003477 * This may set the last used search pattern.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003478 *
3479 * Return MAXLNUM when no Ex address was found.
3480 */
3481 static linenr_T
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003482get_address(
3483 exarg_T *eap UNUSED,
3484 char_u **ptr,
Bram Moolenaar1b03a192019-12-08 17:08:29 +01003485 cmd_addr_T addr_type,
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02003486 int skip, // only skip the address, don't use it
3487 int silent, // no errors or side effects
3488 int to_other_file, // flag: may jump to other file
3489 int address_count UNUSED) // 1 for first address, >1 after comma
Bram Moolenaar071d4272004-06-13 20:20:40 +00003490{
3491 int c;
3492 int i;
3493 long n;
3494 char_u *cmd;
3495 pos_T pos;
3496 pos_T *fp;
3497 linenr_T lnum;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003498 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003499
3500 cmd = skipwhite(*ptr);
3501 lnum = MAXLNUM;
3502 do
3503 {
3504 switch (*cmd)
3505 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003506 case '.': // '.' - Cursor position
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003507 ++cmd;
3508 switch (addr_type)
3509 {
3510 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02003511 case ADDR_OTHER:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003512 lnum = curwin->w_cursor.lnum;
3513 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003514 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01003515 lnum = CURRENT_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003516 break;
3517 case ADDR_ARGUMENTS:
3518 lnum = curwin->w_arg_idx + 1;
3519 break;
3520 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003521 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003522 lnum = curbuf->b_fnum;
3523 break;
3524 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01003525 lnum = CURRENT_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003526 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02003527 case ADDR_NONE:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003528 case ADDR_TABS_RELATIVE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02003529 case ADDR_UNSIGNED:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003530 addr_error(addr_type);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01003531 cmd = NULL;
3532 goto error;
3533 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003534 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003535#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02003536 lnum = qf_get_cur_idx(eap);
3537#endif
3538 break;
3539 case ADDR_QUICKFIX_VALID:
3540#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003541 lnum = qf_get_cur_valid_idx(eap);
Bram Moolenaare906c502015-09-09 21:10:39 +02003542#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003543 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003544 }
3545 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003546
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003547 case '$': // '$' - last line
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003548 ++cmd;
3549 switch (addr_type)
3550 {
3551 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02003552 case ADDR_OTHER:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003553 lnum = curbuf->b_ml.ml_line_count;
3554 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003555 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01003556 lnum = LAST_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003557 break;
3558 case ADDR_ARGUMENTS:
3559 lnum = ARGCOUNT;
3560 break;
3561 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003562 buf = lastbuf;
3563 while (buf->b_ml.ml_mfp == NULL)
3564 {
3565 if (buf->b_prev == NULL)
3566 break;
3567 buf = buf->b_prev;
3568 }
3569 lnum = buf->b_fnum;
3570 break;
3571 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003572 lnum = lastbuf->b_fnum;
3573 break;
3574 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01003575 lnum = LAST_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003576 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02003577 case ADDR_NONE:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003578 case ADDR_TABS_RELATIVE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02003579 case ADDR_UNSIGNED:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003580 addr_error(addr_type);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01003581 cmd = NULL;
3582 goto error;
3583 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003584 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003585#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003586 lnum = qf_get_size(eap);
3587 if (lnum == 0)
3588 lnum = 1;
Bram Moolenaare906c502015-09-09 21:10:39 +02003589#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003590 break;
Bram Moolenaar25190db2019-05-04 15:05:28 +02003591 case ADDR_QUICKFIX_VALID:
3592#ifdef FEAT_QUICKFIX
3593 lnum = qf_get_valid_size(eap);
3594 if (lnum == 0)
3595 lnum = 1;
3596#endif
3597 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003598 }
3599 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003600
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003601 case '\'': // ''' - mark
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003602 if (*++cmd == NUL)
3603 {
3604 cmd = NULL;
3605 goto error;
3606 }
3607 if (addr_type != ADDR_LINES)
3608 {
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003609 addr_error(addr_type);
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01003610 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003611 goto error;
3612 }
3613 if (skip)
3614 ++cmd;
3615 else
3616 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003617 // Only accept a mark in another file when it is
3618 // used by itself: ":'M".
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003619 fp = getmark(*cmd, to_other_file && cmd[1] == NUL);
3620 ++cmd;
3621 if (fp == (pos_T *)-1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003622 // Jumped to another file.
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003623 lnum = curwin->w_cursor.lnum;
3624 else
3625 {
3626 if (check_mark(fp) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003627 {
3628 cmd = NULL;
3629 goto error;
3630 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003631 lnum = fp->lnum;
3632 }
3633 }
3634 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003635
3636 case '/':
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003637 case '?': // '/' or '?' - search
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003638 c = *cmd++;
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 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003645 if (skip) // skip "/pat/"
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003646 {
3647 cmd = skip_regexp(cmd, c, (int)p_magic, NULL);
3648 if (*cmd == c)
3649 ++cmd;
3650 }
3651 else
3652 {
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02003653 int flags;
3654
3655 pos = curwin->w_cursor; // save curwin->w_cursor
3656
3657 // When '/' or '?' follows another address, start from
3658 // there.
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003659 if (lnum != MAXLNUM)
3660 curwin->w_cursor.lnum = lnum;
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02003661
3662 // Start a forward search at the end of the line (unless
3663 // before the first line).
3664 // Start a backward search at the start of the line.
3665 // This makes sure we never match in the current
3666 // line, and can match anywhere in the
3667 // next/previous line.
Bram Moolenaar8ada6aa2017-12-19 21:23:21 +01003668 if (c == '/' && curwin->w_cursor.lnum > 0)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003669 curwin->w_cursor.col = MAXCOL;
3670 else
3671 curwin->w_cursor.col = 0;
3672 searchcmdlen = 0;
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02003673 flags = silent ? 0 : SEARCH_HIS | SEARCH_MSG;
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02003674 if (!do_search(NULL, c, cmd, 1L, flags, NULL))
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003675 {
3676 curwin->w_cursor = pos;
3677 cmd = NULL;
3678 goto error;
3679 }
3680 lnum = curwin->w_cursor.lnum;
3681 curwin->w_cursor = pos;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003682 // adjust command string pointer
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003683 cmd += searchcmdlen;
3684 }
3685 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003686
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003687 case '\\': // "\?", "\/" or "\&", repeat search
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003688 ++cmd;
3689 if (addr_type != ADDR_LINES)
3690 {
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003691 addr_error(addr_type);
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01003692 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003693 goto error;
3694 }
3695 if (*cmd == '&')
3696 i = RE_SUBST;
3697 else if (*cmd == '?' || *cmd == '/')
3698 i = RE_SEARCH;
3699 else
3700 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003701 emsg(_(e_backslash));
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003702 cmd = NULL;
3703 goto error;
3704 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003705
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003706 if (!skip)
3707 {
3708 /*
3709 * When search follows another address, start from
3710 * there.
3711 */
3712 if (lnum != MAXLNUM)
3713 pos.lnum = lnum;
3714 else
3715 pos.lnum = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003716
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003717 /*
3718 * Start the search just like for the above
3719 * do_search().
3720 */
3721 if (*cmd != '?')
3722 pos.col = MAXCOL;
3723 else
3724 pos.col = 0;
Bram Moolenaarbd8539a2015-08-11 18:53:03 +02003725 pos.coladd = 0;
Bram Moolenaar5d24a222018-12-23 19:10:09 +01003726 if (searchit(curwin, curbuf, &pos, NULL,
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003727 *cmd == '?' ? BACKWARD : FORWARD,
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02003728 (char_u *)"", 1L, SEARCH_MSG, i, NULL) != FAIL)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003729 lnum = pos.lnum;
3730 else
3731 {
3732 cmd = NULL;
3733 goto error;
3734 }
3735 }
3736 ++cmd;
3737 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003738
3739 default:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003740 if (VIM_ISDIGIT(*cmd)) // absolute line number
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003741 lnum = getdigits(&cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003742 }
3743
3744 for (;;)
3745 {
3746 cmd = skipwhite(cmd);
3747 if (*cmd != '-' && *cmd != '+' && !VIM_ISDIGIT(*cmd))
3748 break;
3749
3750 if (lnum == MAXLNUM)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003751 {
3752 switch (addr_type)
3753 {
3754 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02003755 case ADDR_OTHER:
3756 // "+1" is same as ".+1"
Bram Moolenaarf240e182014-11-27 18:33:02 +01003757 lnum = curwin->w_cursor.lnum;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003758 break;
3759 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01003760 lnum = CURRENT_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003761 break;
3762 case ADDR_ARGUMENTS:
3763 lnum = curwin->w_arg_idx + 1;
3764 break;
3765 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003766 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003767 lnum = curbuf->b_fnum;
3768 break;
3769 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01003770 lnum = CURRENT_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003771 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01003772 case ADDR_TABS_RELATIVE:
3773 lnum = 1;
3774 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003775 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003776#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02003777 lnum = qf_get_cur_idx(eap);
3778#endif
3779 break;
3780 case ADDR_QUICKFIX_VALID:
3781#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003782 lnum = qf_get_cur_valid_idx(eap);
Bram Moolenaare906c502015-09-09 21:10:39 +02003783#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003784 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02003785 case ADDR_NONE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02003786 case ADDR_UNSIGNED:
3787 lnum = 0;
Bram Moolenaarb7316892019-05-01 18:08:42 +02003788 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003789 }
3790 }
3791
Bram Moolenaar071d4272004-06-13 20:20:40 +00003792 if (VIM_ISDIGIT(*cmd))
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003793 i = '+'; // "number" is same as "+number"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003794 else
3795 i = *cmd++;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003796 if (!VIM_ISDIGIT(*cmd)) // '+' is '+1', but '+0' is not '+1'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003797 n = 1;
3798 else
3799 n = getdigits(&cmd);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01003800
3801 if (addr_type == ADDR_TABS_RELATIVE)
3802 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003803 emsg(_(e_invrange));
Bram Moolenaar2f72c702017-01-29 14:48:10 +01003804 cmd = NULL;
3805 goto error;
3806 }
3807 else if (addr_type == ADDR_LOADED_BUFFERS
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003808 || addr_type == ADDR_BUFFERS)
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01003809 lnum = compute_buffer_local_count(
3810 addr_type, lnum, (i == '-') ? -1 * n : n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003811 else
Bram Moolenaarded27822017-01-02 14:27:34 +01003812 {
3813#ifdef FEAT_FOLDING
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003814 // Relative line addressing, need to adjust for folded lines
3815 // now, but only do it after the first address.
Bram Moolenaarded27822017-01-02 14:27:34 +01003816 if (addr_type == ADDR_LINES && (i == '-' || i == '+')
3817 && address_count >= 2)
3818 (void)hasFolding(lnum, NULL, &lnum);
3819#endif
3820 if (i == '-')
3821 lnum -= n;
3822 else
3823 lnum += n;
3824 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003825 }
3826 } while (*cmd == '/' || *cmd == '?');
3827
3828error:
3829 *ptr = cmd;
3830 return lnum;
3831}
3832
3833/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00003834 * Get flags from an Ex command argument.
3835 */
3836 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003837get_flags(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00003838{
3839 while (vim_strchr((char_u *)"lp#", *eap->arg) != NULL)
3840 {
3841 if (*eap->arg == 'l')
3842 eap->flags |= EXFLAG_LIST;
3843 else if (*eap->arg == 'p')
3844 eap->flags |= EXFLAG_PRINT;
3845 else
3846 eap->flags |= EXFLAG_NR;
3847 eap->arg = skipwhite(eap->arg + 1);
3848 }
3849}
3850
3851/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003852 * Function called for command which is Not Implemented. NI!
3853 */
3854 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003855ex_ni(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003856{
3857 if (!eap->skip)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003858 eap->errmsg = N_("E319: Sorry, the command is not available in this version");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003859}
3860
Bram Moolenaar7bb75552007-07-16 18:39:49 +00003861#ifdef HAVE_EX_SCRIPT_NI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003862/*
3863 * Function called for script command which is Not Implemented. NI!
3864 * Skips over ":perl <<EOF" constructs.
3865 */
3866 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003867ex_script_ni(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003868{
3869 if (!eap->skip)
3870 ex_ni(eap);
3871 else
3872 vim_free(script_get(eap, eap->arg));
3873}
3874#endif
3875
3876/*
3877 * Check range in Ex command for validity.
3878 * Return NULL when valid, error message when invalid.
3879 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003880 static char *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003881invalid_range(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003882{
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003883 buf_T *buf;
Bram Moolenaar25190db2019-05-04 15:05:28 +02003884
Bram Moolenaar071d4272004-06-13 20:20:40 +00003885 if ( eap->line1 < 0
3886 || eap->line2 < 0
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003887 || eap->line1 > eap->line2)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003888 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003889
Bram Moolenaar8071cb22019-07-12 17:58:01 +02003890 if (eap->argt & EX_RANGE)
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003891 {
Bram Moolenaarb7316892019-05-01 18:08:42 +02003892 switch (eap->addr_type)
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003893 {
3894 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02003895 if (eap->line2 > curbuf->b_ml.ml_line_count
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003896#ifdef FEAT_DIFF
3897 + (eap->cmdidx == CMD_diffget)
3898#endif
3899 )
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003900 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003901 break;
3902 case ADDR_ARGUMENTS:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003903 // add 1 if ARGCOUNT is 0
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01003904 if (eap->line2 > ARGCOUNT + (!ARGCOUNT))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003905 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003906 break;
3907 case ADDR_BUFFERS:
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02003908 // Only a boundary check, not whether the buffers actually
3909 // exist.
3910 if (eap->line1 < 1 || eap->line2 > get_highest_fnum())
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003911 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003912 break;
3913 case ADDR_LOADED_BUFFERS:
3914 buf = firstbuf;
3915 while (buf->b_ml.ml_mfp == NULL)
3916 {
3917 if (buf->b_next == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003918 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003919 buf = buf->b_next;
3920 }
3921 if (eap->line1 < buf->b_fnum)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003922 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003923 buf = lastbuf;
3924 while (buf->b_ml.ml_mfp == NULL)
3925 {
3926 if (buf->b_prev == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003927 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003928 buf = buf->b_prev;
3929 }
3930 if (eap->line2 > buf->b_fnum)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003931 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003932 break;
3933 case ADDR_WINDOWS:
Bram Moolenaar8be63882015-01-14 11:25:05 +01003934 if (eap->line2 > LAST_WIN_NR)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003935 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003936 break;
3937 case ADDR_TABS:
3938 if (eap->line2 > LAST_TAB_NR)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003939 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003940 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01003941 case ADDR_TABS_RELATIVE:
Bram Moolenaarb7316892019-05-01 18:08:42 +02003942 case ADDR_OTHER:
3943 // Any range is OK.
Bram Moolenaar2f72c702017-01-29 14:48:10 +01003944 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003945 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003946#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02003947 // No error for value that is too big, will use the last entry.
3948 if (eap->line2 <= 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003949 return _(e_invrange);
Bram Moolenaare906c502015-09-09 21:10:39 +02003950#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003951 break;
Bram Moolenaar25190db2019-05-04 15:05:28 +02003952 case ADDR_QUICKFIX_VALID:
3953#ifdef FEAT_QUICKFIX
3954 if ((eap->line2 != 1 && eap->line2 > qf_get_valid_size(eap))
3955 || eap->line2 < 0)
3956 return _(e_invrange);
3957#endif
3958 break;
3959 case ADDR_UNSIGNED:
3960 if (eap->line2 < 0)
3961 return _(e_invrange);
3962 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02003963 case ADDR_NONE:
3964 // Will give an error elsewhere.
3965 break;
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003966 }
3967 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003968 return NULL;
3969}
3970
3971/*
3972 * Correct the range for zero line number, if required.
3973 */
3974 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003975correct_range(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003976{
Bram Moolenaar8071cb22019-07-12 17:58:01 +02003977 if (!(eap->argt & EX_ZEROR)) // zero in range not allowed
Bram Moolenaar071d4272004-06-13 20:20:40 +00003978 {
3979 if (eap->line1 == 0)
3980 eap->line1 = 1;
3981 if (eap->line2 == 0)
3982 eap->line2 = 1;
3983 }
3984}
3985
Bram Moolenaar748bf032005-02-02 23:04:36 +00003986#ifdef FEAT_QUICKFIX
Bram Moolenaar748bf032005-02-02 23:04:36 +00003987/*
3988 * For a ":vimgrep" or ":vimgrepadd" command return a pointer past the
3989 * pattern. Otherwise return eap->arg.
3990 */
3991 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003992skip_grep_pat(exarg_T *eap)
Bram Moolenaar748bf032005-02-02 23:04:36 +00003993{
3994 char_u *p = eap->arg;
3995
Bram Moolenaara37420f2006-02-04 22:37:47 +00003996 if (*p != NUL && (eap->cmdidx == CMD_vimgrep || eap->cmdidx == CMD_lvimgrep
3997 || eap->cmdidx == CMD_vimgrepadd
3998 || eap->cmdidx == CMD_lvimgrepadd
3999 || grep_internal(eap->cmdidx)))
Bram Moolenaar748bf032005-02-02 23:04:36 +00004000 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00004001 p = skip_vimgrep_pat(p, NULL, NULL);
Bram Moolenaar748bf032005-02-02 23:04:36 +00004002 if (p == NULL)
4003 p = eap->arg;
Bram Moolenaar748bf032005-02-02 23:04:36 +00004004 }
4005 return p;
4006}
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004007
4008/*
4009 * For the ":make" and ":grep" commands insert the 'makeprg'/'grepprg' option
4010 * in the command line, so that things like % get expanded.
4011 */
4012 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004013replace_makeprg(exarg_T *eap, char_u *p, char_u **cmdlinep)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004014{
4015 char_u *new_cmdline;
4016 char_u *program;
4017 char_u *pos;
4018 char_u *ptr;
4019 int len;
4020 int i;
4021
4022 /*
4023 * Don't do it when ":vimgrep" is used for ":grep".
4024 */
Bram Moolenaara37420f2006-02-04 22:37:47 +00004025 if ((eap->cmdidx == CMD_make || eap->cmdidx == CMD_lmake
4026 || eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4027 || eap->cmdidx == CMD_grepadd
4028 || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004029 && !grep_internal(eap->cmdidx))
4030 {
Bram Moolenaara37420f2006-02-04 22:37:47 +00004031 if (eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4032 || eap->cmdidx == CMD_grepadd || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004033 {
4034 if (*curbuf->b_p_gp == NUL)
4035 program = p_gp;
4036 else
4037 program = curbuf->b_p_gp;
4038 }
4039 else
4040 {
4041 if (*curbuf->b_p_mp == NUL)
4042 program = p_mp;
4043 else
4044 program = curbuf->b_p_mp;
4045 }
4046
4047 p = skipwhite(p);
4048
4049 if ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4050 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004051 // replace $* by given arguments
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004052 i = 1;
4053 while ((pos = (char_u *)strstr((char *)pos + 2, "$*")) != NULL)
4054 ++i;
4055 len = (int)STRLEN(p);
Bram Moolenaar51e14382019-05-25 20:21:28 +02004056 new_cmdline = alloc(STRLEN(program) + i * (len - 2) + 1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004057 if (new_cmdline == NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004058 return NULL; // out of memory
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004059 ptr = new_cmdline;
4060 while ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4061 {
4062 i = (int)(pos - program);
4063 STRNCPY(ptr, program, i);
4064 STRCPY(ptr += i, p);
4065 ptr += len;
4066 program = pos + 2;
4067 }
4068 STRCPY(ptr, program);
4069 }
4070 else
4071 {
Bram Moolenaar51e14382019-05-25 20:21:28 +02004072 new_cmdline = alloc(STRLEN(program) + STRLEN(p) + 2);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004073 if (new_cmdline == NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004074 return NULL; // out of memory
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004075 STRCPY(new_cmdline, program);
4076 STRCAT(new_cmdline, " ");
4077 STRCAT(new_cmdline, p);
4078 }
4079 msg_make(p);
4080
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004081 // 'eap->cmd' is not set here, because it is not used at CMD_make
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004082 vim_free(*cmdlinep);
4083 *cmdlinep = new_cmdline;
4084 p = new_cmdline;
4085 }
4086 return p;
4087}
Bram Moolenaar748bf032005-02-02 23:04:36 +00004088#endif
4089
Bram Moolenaar071d4272004-06-13 20:20:40 +00004090/*
4091 * Expand file name in Ex command argument.
Bram Moolenaar0abb4272019-06-15 16:06:00 +02004092 * When an error is detected, "errormsgp" is set to a non-NULL pointer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004093 * Return FAIL for failure, OK otherwise.
4094 */
4095 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004096expand_filename(
4097 exarg_T *eap,
4098 char_u **cmdlinep,
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004099 char **errormsgp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004100{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004101 int has_wildcards; // need to expand wildcards
Bram Moolenaar071d4272004-06-13 20:20:40 +00004102 char_u *repl;
4103 int srclen;
4104 char_u *p;
4105 int n;
Bram Moolenaar63b92542007-03-27 14:57:09 +00004106 int escaped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004107
Bram Moolenaar748bf032005-02-02 23:04:36 +00004108#ifdef FEAT_QUICKFIX
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004109 // Skip a regexp pattern for ":vimgrep[add] pat file..."
Bram Moolenaar748bf032005-02-02 23:04:36 +00004110 p = skip_grep_pat(eap);
4111#else
4112 p = eap->arg;
4113#endif
4114
Bram Moolenaar071d4272004-06-13 20:20:40 +00004115 /*
4116 * Decide to expand wildcards *before* replacing '%', '#', etc. If
4117 * the file name contains a wildcard it should not cause expanding.
4118 * (it will be expanded anyway if there is a wildcard before replacing).
4119 */
Bram Moolenaar748bf032005-02-02 23:04:36 +00004120 has_wildcards = mch_has_wildcard(p);
4121 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004122 {
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004123#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004124 // Skip over `=expr`, wildcards in it are not expanded.
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004125 if (p[0] == '`' && p[1] == '=')
4126 {
4127 p += 2;
4128 (void)skip_expr(&p);
4129 if (*p == '`')
4130 ++p;
4131 continue;
4132 }
4133#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004134 /*
4135 * Quick check if this cannot be the start of a special string.
4136 * Also removes backslash before '%', '#' and '<'.
4137 */
4138 if (vim_strchr((char_u *)"%#<", *p) == NULL)
4139 {
4140 ++p;
4141 continue;
4142 }
4143
4144 /*
4145 * Try to find a match at this position.
4146 */
Bram Moolenaar63b92542007-03-27 14:57:09 +00004147 repl = eval_vars(p, eap->arg, &srclen, &(eap->do_ecmd_lnum),
4148 errormsgp, &escaped);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004149 if (*errormsgp != NULL) // error detected
Bram Moolenaar071d4272004-06-13 20:20:40 +00004150 return FAIL;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004151 if (repl == NULL) // no match found
Bram Moolenaar071d4272004-06-13 20:20:40 +00004152 {
4153 p += srclen;
4154 continue;
4155 }
4156
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004157 // Wildcards won't be expanded below, the replacement is taken
4158 // literally. But do expand "~/file", "~user/file" and "$HOME/file".
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00004159 if (vim_strchr(repl, '$') != NULL || vim_strchr(repl, '~') != NULL)
4160 {
4161 char_u *l = repl;
4162
4163 repl = expand_env_save(repl);
4164 vim_free(l);
4165 }
4166
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004167 // Need to escape white space et al. with a backslash.
4168 // Don't do this for:
4169 // - replacement that already has been escaped: "##"
4170 // - shell commands (may have to use quotes instead).
4171 // - non-unix systems when there is a single argument (spaces don't
4172 // separate arguments then).
Bram Moolenaar071d4272004-06-13 20:20:40 +00004173 if (!eap->usefilter
Bram Moolenaar63b92542007-03-27 14:57:09 +00004174 && !escaped
Bram Moolenaar071d4272004-06-13 20:20:40 +00004175 && eap->cmdidx != CMD_bang
Bram Moolenaar071d4272004-06-13 20:20:40 +00004176 && eap->cmdidx != CMD_grep
4177 && eap->cmdidx != CMD_grepadd
Bram Moolenaarbf15b8d2017-06-04 20:43:48 +02004178 && eap->cmdidx != CMD_hardcopy
Bram Moolenaar67883b42017-07-27 22:57:00 +02004179 && eap->cmdidx != CMD_lgrep
4180 && eap->cmdidx != CMD_lgrepadd
4181 && eap->cmdidx != CMD_lmake
4182 && eap->cmdidx != CMD_make
4183 && eap->cmdidx != CMD_terminal
Bram Moolenaar071d4272004-06-13 20:20:40 +00004184#ifndef UNIX
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004185 && !(eap->argt & EX_NOSPC)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004186#endif
4187 )
4188 {
4189 char_u *l;
4190#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004191 // Don't escape a backslash here, because rem_backslash() doesn't
4192 // remove it later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004193 static char_u *nobslash = (char_u *)" \t\"|";
4194# define ESCAPE_CHARS nobslash
4195#else
4196# define ESCAPE_CHARS escape_chars
4197#endif
4198
4199 for (l = repl; *l; ++l)
4200 if (vim_strchr(ESCAPE_CHARS, *l) != NULL)
4201 {
4202 l = vim_strsave_escaped(repl, ESCAPE_CHARS);
4203 if (l != NULL)
4204 {
4205 vim_free(repl);
4206 repl = l;
4207 }
4208 break;
4209 }
4210 }
4211
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004212 // For a shell command a '!' must be escaped.
Bram Moolenaar67883b42017-07-27 22:57:00 +02004213 if ((eap->usefilter || eap->cmdidx == CMD_bang
4214 || eap->cmdidx == CMD_terminal)
Bram Moolenaar31b7d382014-04-01 18:54:48 +02004215 && vim_strpbrk(repl, (char_u *)"!") != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004216 {
4217 char_u *l;
4218
Bram Moolenaar31b7d382014-04-01 18:54:48 +02004219 l = vim_strsave_escaped(repl, (char_u *)"!");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004220 if (l != NULL)
4221 {
4222 vim_free(repl);
4223 repl = l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004224 }
4225 }
4226
4227 p = repl_cmdline(eap, p, srclen, repl, cmdlinep);
4228 vim_free(repl);
4229 if (p == NULL)
4230 return FAIL;
4231 }
4232
4233 /*
4234 * One file argument: Expand wildcards.
4235 * Don't do this with ":r !command" or ":w !command".
4236 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004237 if ((eap->argt & EX_NOSPC) && !eap->usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004238 {
4239 /*
4240 * May do this twice:
4241 * 1. Replace environment variables.
4242 * 2. Replace any other wildcards, remove backslashes.
4243 */
4244 for (n = 1; n <= 2; ++n)
4245 {
4246 if (n == 2)
4247 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004248 /*
4249 * Halve the number of backslashes (this is Vi compatible).
4250 * For Unix and OS/2, when wildcards are expanded, this is
4251 * done by ExpandOne() below.
4252 */
Bram Moolenaare7fedb62015-12-31 19:07:19 +01004253#if defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004254 if (!has_wildcards)
4255#endif
4256 backslash_halve(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004257 }
4258
4259 if (has_wildcards)
4260 {
4261 if (n == 1)
4262 {
4263 /*
4264 * First loop: May expand environment variables. This
4265 * can be done much faster with expand_env() than with
4266 * something else (e.g., calling a shell).
4267 * After expanding environment variables, check again
4268 * if there are still wildcards present.
4269 */
4270 if (vim_strchr(eap->arg, '$') != NULL
4271 || vim_strchr(eap->arg, '~') != NULL)
4272 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +00004273 expand_env_esc(eap->arg, NameBuff, MAXPATHL,
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00004274 TRUE, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004275 has_wildcards = mch_has_wildcard(NameBuff);
4276 p = NameBuff;
4277 }
4278 else
4279 p = NULL;
4280 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004281 else // n == 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00004282 {
4283 expand_T xpc;
Bram Moolenaarb40c2572019-10-19 21:01:05 +02004284 int options = WILD_LIST_NOTFOUND
4285 | WILD_NOERROR | WILD_ADD_SLASH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004286
4287 ExpandInit(&xpc);
4288 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar94950a92010-12-02 16:01:29 +01004289 if (p_wic)
4290 options += WILD_ICASE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004291 p = ExpandOne(&xpc, eap->arg, NULL,
Bram Moolenaar94950a92010-12-02 16:01:29 +01004292 options, WILD_EXPAND_FREE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004293 if (p == NULL)
4294 return FAIL;
4295 }
4296 if (p != NULL)
4297 {
4298 (void)repl_cmdline(eap, eap->arg, (int)STRLEN(eap->arg),
4299 p, cmdlinep);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004300 if (n == 2) // p came from ExpandOne()
Bram Moolenaar071d4272004-06-13 20:20:40 +00004301 vim_free(p);
4302 }
4303 }
4304 }
4305 }
4306 return OK;
4307}
4308
4309/*
4310 * Replace part of the command line, keeping eap->cmd, eap->arg and
4311 * eap->nextcmd correct.
4312 * "src" points to the part that is to be replaced, of length "srclen".
4313 * "repl" is the replacement string.
4314 * Returns a pointer to the character after the replaced string.
4315 * Returns NULL for failure.
4316 */
4317 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004318repl_cmdline(
4319 exarg_T *eap,
4320 char_u *src,
4321 int srclen,
4322 char_u *repl,
4323 char_u **cmdlinep)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004324{
4325 int len;
4326 int i;
4327 char_u *new_cmdline;
4328
4329 /*
4330 * The new command line is build in new_cmdline[].
4331 * First allocate it.
4332 * Careful: a "+cmd" argument may have been NUL terminated.
4333 */
4334 len = (int)STRLEN(repl);
4335 i = (int)(src - *cmdlinep) + (int)STRLEN(src + srclen) + len + 3;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004336 if (eap->nextcmd != NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004337 i += (int)STRLEN(eap->nextcmd);// add space for next command
Bram Moolenaar964b3742019-05-24 18:54:09 +02004338 if ((new_cmdline = alloc(i)) == NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004339 return NULL; // out of memory!
Bram Moolenaar071d4272004-06-13 20:20:40 +00004340
4341 /*
4342 * Copy the stuff before the expanded part.
4343 * Copy the expanded stuff.
4344 * Copy what came after the expanded part.
4345 * Copy the next commands, if there are any.
4346 */
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004347 i = (int)(src - *cmdlinep); // length of part before match
Bram Moolenaar071d4272004-06-13 20:20:40 +00004348 mch_memmove(new_cmdline, *cmdlinep, (size_t)i);
Bram Moolenaara3ffd9c2005-07-21 21:03:15 +00004349
Bram Moolenaar071d4272004-06-13 20:20:40 +00004350 mch_memmove(new_cmdline + i, repl, (size_t)len);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004351 i += len; // remember the end of the string
Bram Moolenaar071d4272004-06-13 20:20:40 +00004352 STRCPY(new_cmdline + i, src + srclen);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004353 src = new_cmdline + i; // remember where to continue
Bram Moolenaar071d4272004-06-13 20:20:40 +00004354
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004355 if (eap->nextcmd != NULL) // append next command
Bram Moolenaar071d4272004-06-13 20:20:40 +00004356 {
4357 i = (int)STRLEN(new_cmdline) + 1;
4358 STRCPY(new_cmdline + i, eap->nextcmd);
4359 eap->nextcmd = new_cmdline + i;
4360 }
4361 eap->cmd = new_cmdline + (eap->cmd - *cmdlinep);
4362 eap->arg = new_cmdline + (eap->arg - *cmdlinep);
4363 if (eap->do_ecmd_cmd != NULL && eap->do_ecmd_cmd != dollar_command)
4364 eap->do_ecmd_cmd = new_cmdline + (eap->do_ecmd_cmd - *cmdlinep);
4365 vim_free(*cmdlinep);
4366 *cmdlinep = new_cmdline;
4367
4368 return src;
4369}
4370
4371/*
4372 * Check for '|' to separate commands and '"' to start comments.
4373 */
4374 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004375separate_nextcmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004376{
4377 char_u *p;
4378
Bram Moolenaar86b68352004-12-27 21:59:20 +00004379#ifdef FEAT_QUICKFIX
Bram Moolenaar748bf032005-02-02 23:04:36 +00004380 p = skip_grep_pat(eap);
4381#else
4382 p = eap->arg;
Bram Moolenaar86b68352004-12-27 21:59:20 +00004383#endif
4384
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004385 for ( ; *p; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004386 {
4387 if (*p == Ctrl_V)
4388 {
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004389 if (eap->argt & (EX_CTRLV | EX_XFILE))
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004390 ++p; // skip CTRL-V and next char
Bram Moolenaar071d4272004-06-13 20:20:40 +00004391 else
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004392 // remove CTRL-V and skip next char
Bram Moolenaara7241f52008-06-24 20:39:31 +00004393 STRMOVE(p, p + 1);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004394 if (*p == NUL) // stop at NUL after CTRL-V
Bram Moolenaar071d4272004-06-13 20:20:40 +00004395 break;
4396 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004397
4398#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004399 // Skip over `=expr` when wildcards are expanded.
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004400 else if (p[0] == '`' && p[1] == '=' && (eap->argt & EX_XFILE))
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004401 {
4402 p += 2;
4403 (void)skip_expr(&p);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004404 }
4405#endif
4406
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004407 // Check for '"': start of comment or '|': next command
4408 // :@" and :*" do not start a comment!
4409 // :redir @" doesn't either.
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004410 else if ((*p == '"' && !(eap->argt & EX_NOTRLCOM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004411 && ((eap->cmdidx != CMD_at && eap->cmdidx != CMD_star)
4412 || p != eap->arg)
4413 && (eap->cmdidx != CMD_redir
4414 || p != eap->arg + 1 || p[-1] != '@'))
4415 || *p == '|' || *p == '\n')
4416 {
4417 /*
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004418 * We remove the '\' before the '|', unless EX_CTRLV is used
Bram Moolenaar071d4272004-06-13 20:20:40 +00004419 * AND 'b' is present in 'cpoptions'.
4420 */
4421 if ((vim_strchr(p_cpo, CPO_BAR) == NULL
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004422 || !(eap->argt & EX_CTRLV)) && *(p - 1) == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004423 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004424 STRMOVE(p - 1, p); // remove the '\'
Bram Moolenaar071d4272004-06-13 20:20:40 +00004425 --p;
4426 }
4427 else
4428 {
4429 eap->nextcmd = check_nextcmd(p);
4430 *p = NUL;
4431 break;
4432 }
4433 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004434 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00004435
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004436 if (!(eap->argt & EX_NOTRLCOM)) // remove trailing spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00004437 del_trailing_spaces(eap->arg);
4438}
4439
4440/*
4441 * get + command from ex argument
4442 */
4443 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004444getargcmd(char_u **argp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004445{
4446 char_u *arg = *argp;
4447 char_u *command = NULL;
4448
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004449 if (*arg == '+') // +[command]
Bram Moolenaar071d4272004-06-13 20:20:40 +00004450 {
4451 ++arg;
Bram Moolenaar3e451592014-04-02 14:22:05 +02004452 if (vim_isspace(*arg) || *arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004453 command = dollar_command;
4454 else
4455 {
4456 command = arg;
4457 arg = skip_cmd_arg(command, TRUE);
4458 if (*arg != NUL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004459 *arg++ = NUL; // terminate command with NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004460 }
4461
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004462 arg = skipwhite(arg); // skip over spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00004463 *argp = arg;
4464 }
4465 return command;
4466}
4467
4468/*
4469 * Find end of "+command" argument. Skip over "\ " and "\\".
4470 */
Bram Moolenaard0190392019-08-23 21:17:35 +02004471 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004472skip_cmd_arg(
4473 char_u *p,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004474 int rembs) // TRUE to halve the number of backslashes
Bram Moolenaar071d4272004-06-13 20:20:40 +00004475{
4476 while (*p && !vim_isspace(*p))
4477 {
4478 if (*p == '\\' && p[1] != NUL)
4479 {
4480 if (rembs)
Bram Moolenaara7241f52008-06-24 20:39:31 +00004481 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004482 else
4483 ++p;
4484 }
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004485 MB_PTR_ADV(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004486 }
4487 return p;
4488}
4489
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004490 int
4491get_bad_opt(char_u *p, exarg_T *eap)
4492{
4493 if (STRICMP(p, "keep") == 0)
4494 eap->bad_char = BAD_KEEP;
4495 else if (STRICMP(p, "drop") == 0)
4496 eap->bad_char = BAD_DROP;
4497 else if (MB_BYTE2LEN(*p) == 1 && p[1] == NUL)
4498 eap->bad_char = *p;
Bram Moolenaar75808492018-06-12 12:39:41 +02004499 else
4500 return FAIL;
4501 return OK;
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004502}
4503
Bram Moolenaar071d4272004-06-13 20:20:40 +00004504/*
4505 * Get "++opt=arg" argument.
4506 * Return FAIL or OK.
4507 */
4508 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004509getargopt(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004510{
4511 char_u *arg = eap->arg + 2;
4512 int *pp = NULL;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02004513 int bad_char_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004514 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004515
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004516 // ":edit ++[no]bin[ary] file"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004517 if (STRNCMP(arg, "bin", 3) == 0 || STRNCMP(arg, "nobin", 5) == 0)
4518 {
4519 if (*arg == 'n')
4520 {
4521 arg += 2;
4522 eap->force_bin = FORCE_NOBIN;
4523 }
4524 else
4525 eap->force_bin = FORCE_BIN;
4526 if (!checkforcmd(&arg, "binary", 3))
4527 return FAIL;
4528 eap->arg = skipwhite(arg);
4529 return OK;
4530 }
4531
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004532 // ":read ++edit file"
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004533 if (STRNCMP(arg, "edit", 4) == 0)
4534 {
4535 eap->read_edit = TRUE;
4536 eap->arg = skipwhite(arg + 4);
4537 return OK;
4538 }
4539
Bram Moolenaar071d4272004-06-13 20:20:40 +00004540 if (STRNCMP(arg, "ff", 2) == 0)
4541 {
4542 arg += 2;
4543 pp = &eap->force_ff;
4544 }
4545 else if (STRNCMP(arg, "fileformat", 10) == 0)
4546 {
4547 arg += 10;
4548 pp = &eap->force_ff;
4549 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004550 else if (STRNCMP(arg, "enc", 3) == 0)
4551 {
Bram Moolenaarb38e9ab2011-12-14 14:49:45 +01004552 if (STRNCMP(arg, "encoding", 8) == 0)
4553 arg += 8;
4554 else
4555 arg += 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004556 pp = &eap->force_enc;
4557 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004558 else if (STRNCMP(arg, "bad", 3) == 0)
4559 {
4560 arg += 3;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02004561 pp = &bad_char_idx;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004562 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004563
4564 if (pp == NULL || *arg != '=')
4565 return FAIL;
4566
4567 ++arg;
4568 *pp = (int)(arg - eap->cmd);
4569 arg = skip_cmd_arg(arg, FALSE);
4570 eap->arg = skipwhite(arg);
4571 *arg = NUL;
4572
Bram Moolenaar071d4272004-06-13 20:20:40 +00004573 if (pp == &eap->force_ff)
4574 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004575 if (check_ff_value(eap->cmd + eap->force_ff) == FAIL)
4576 return FAIL;
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004577 eap->force_ff = eap->cmd[eap->force_ff];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004578 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004579 else if (pp == &eap->force_enc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004580 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004581 // Make 'fileencoding' lower case.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004582 for (p = eap->cmd + eap->force_enc; *p != NUL; ++p)
4583 *p = TOLOWER_ASC(*p);
4584 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004585 else
4586 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004587 // Check ++bad= argument. Must be a single-byte character, "keep" or
4588 // "drop".
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004589 if (get_bad_opt(eap->cmd + bad_char_idx, eap) == FAIL)
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004590 return FAIL;
4591 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004592
4593 return OK;
4594}
4595
Bram Moolenaar071d4272004-06-13 20:20:40 +00004596 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004597ex_autocmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004598{
4599 /*
Bram Moolenaar26d4b892018-07-04 22:26:28 +02004600 * Disallow autocommands from .exrc and .vimrc in current
Bram Moolenaar071d4272004-06-13 20:20:40 +00004601 * directory for security reasons.
4602 */
4603 if (secure)
4604 {
4605 secure = 2;
4606 eap->errmsg = e_curdir;
4607 }
4608 else if (eap->cmdidx == CMD_autocmd)
4609 do_autocmd(eap->arg, eap->forceit);
4610 else
4611 do_augroup(eap->arg, eap->forceit);
4612}
4613
4614/*
4615 * ":doautocmd": Apply the automatic commands to the current buffer.
4616 */
4617 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004618ex_doautocmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004619{
Bram Moolenaar60542ac2012-02-12 20:14:01 +01004620 char_u *arg = eap->arg;
4621 int call_do_modelines = check_nomodeline(&arg);
Bram Moolenaar1610d052016-06-09 22:53:01 +02004622 int did_aucmd;
Bram Moolenaar60542ac2012-02-12 20:14:01 +01004623
Bram Moolenaar1610d052016-06-09 22:53:01 +02004624 (void)do_doautocmd(arg, TRUE, &did_aucmd);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004625 // Only when there is no <nomodeline>.
Bram Moolenaar1610d052016-06-09 22:53:01 +02004626 if (call_do_modelines && did_aucmd)
Bram Moolenaar60542ac2012-02-12 20:14:01 +01004627 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004628}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004629
Bram Moolenaar071d4272004-06-13 20:20:40 +00004630/*
4631 * :[N]bunload[!] [N] [bufname] unload buffer
4632 * :[N]bdelete[!] [N] [bufname] delete buffer from buffer list
4633 * :[N]bwipeout[!] [N] [bufname] delete buffer really
4634 */
4635 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004636ex_bunload(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004637{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01004638 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02004639 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004640 eap->errmsg = do_bufdel(
4641 eap->cmdidx == CMD_bdelete ? DOBUF_DEL
4642 : eap->cmdidx == CMD_bwipeout ? DOBUF_WIPE
4643 : DOBUF_UNLOAD, eap->arg,
4644 eap->addr_count, (int)eap->line1, (int)eap->line2, eap->forceit);
4645}
4646
4647/*
4648 * :[N]buffer [N] to buffer N
4649 * :[N]sbuffer [N] to buffer N
4650 */
4651 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004652ex_buffer(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004653{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01004654 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02004655 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004656 if (*eap->arg)
4657 eap->errmsg = e_trailing;
4658 else
4659 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004660 if (eap->addr_count == 0) // default is current buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00004661 goto_buffer(eap, DOBUF_CURRENT, FORWARD, 0);
4662 else
4663 goto_buffer(eap, DOBUF_FIRST, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02004664 if (eap->do_ecmd_cmd != NULL)
4665 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004666 }
4667}
4668
4669/*
4670 * :[N]bmodified [N] to next mod. buffer
4671 * :[N]sbmodified [N] to next mod. buffer
4672 */
4673 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004674ex_bmodified(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004675{
4676 goto_buffer(eap, DOBUF_MOD, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02004677 if (eap->do_ecmd_cmd != NULL)
4678 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004679}
4680
4681/*
4682 * :[N]bnext [N] to next buffer
4683 * :[N]sbnext [N] split and to next buffer
4684 */
4685 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004686ex_bnext(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004687{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01004688 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar3029bcc2020-01-18 15:06:19 +01004689 return;
4690
Bram Moolenaar071d4272004-06-13 20:20:40 +00004691 goto_buffer(eap, DOBUF_CURRENT, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02004692 if (eap->do_ecmd_cmd != NULL)
4693 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004694}
4695
4696/*
4697 * :[N]bNext [N] to previous buffer
4698 * :[N]bprevious [N] to previous buffer
4699 * :[N]sbNext [N] split and to previous buffer
4700 * :[N]sbprevious [N] split and to previous buffer
4701 */
4702 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004703ex_bprevious(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004704{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01004705 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar3029bcc2020-01-18 15:06:19 +01004706 return;
4707
Bram Moolenaar071d4272004-06-13 20:20:40 +00004708 goto_buffer(eap, DOBUF_CURRENT, BACKWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02004709 if (eap->do_ecmd_cmd != NULL)
4710 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004711}
4712
4713/*
4714 * :brewind to first buffer
4715 * :bfirst to first buffer
4716 * :sbrewind split and to first buffer
4717 * :sbfirst split and to first buffer
4718 */
4719 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004720ex_brewind(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004721{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01004722 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar3029bcc2020-01-18 15:06:19 +01004723 return;
4724
Bram Moolenaar071d4272004-06-13 20:20:40 +00004725 goto_buffer(eap, DOBUF_FIRST, FORWARD, 0);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02004726 if (eap->do_ecmd_cmd != NULL)
4727 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004728}
4729
4730/*
4731 * :blast to last buffer
4732 * :sblast split and to last buffer
4733 */
4734 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004735ex_blast(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004736{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01004737 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar3029bcc2020-01-18 15:06:19 +01004738 return;
4739
Bram Moolenaar071d4272004-06-13 20:20:40 +00004740 goto_buffer(eap, DOBUF_LAST, BACKWARD, 0);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02004741 if (eap->do_ecmd_cmd != NULL)
4742 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004743}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004744
4745 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004746ends_excmd(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004747{
4748 return (c == NUL || c == '|' || c == '"' || c == '\n');
4749}
4750
4751#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA) || defined(FEAT_EVAL) \
4752 || defined(PROTO)
4753/*
4754 * Return the next command, after the first '|' or '\n'.
4755 * Return NULL if not found.
4756 */
4757 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004758find_nextcmd(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004759{
4760 while (*p != '|' && *p != '\n')
4761 {
4762 if (*p == NUL)
4763 return NULL;
4764 ++p;
4765 }
4766 return (p + 1);
4767}
4768#endif
4769
4770/*
Bram Moolenaar2256c992016-11-15 21:17:07 +01004771 * Check if *p is a separator between Ex commands, skipping over white space.
4772 * Return NULL if it isn't, the following character if it is.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004773 */
4774 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004775check_nextcmd(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004776{
Bram Moolenaar2256c992016-11-15 21:17:07 +01004777 char_u *s = skipwhite(p);
4778
4779 if (*s == '|' || *s == '\n')
4780 return (s + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004781 else
4782 return NULL;
4783}
4784
4785/*
4786 * - if there are more files to edit
4787 * - and this is the last window
4788 * - and forceit not used
4789 * - and not repeated twice on a row
4790 * return FAIL and give error message if 'message' TRUE
4791 * return OK otherwise
4792 */
4793 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004794check_more(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004795 int message, // when FALSE check only, no messages
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004796 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004797{
4798 int n = ARGCOUNT - curwin->w_arg_idx - 1;
4799
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00004800 if (!forceit && only_one_window()
4801 && ARGCOUNT > 1 && !arg_had_last && n >= 0 && quitmore == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004802 {
4803 if (message)
4804 {
4805#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
4806 if ((p_confirm || cmdmod.confirm) && curbuf->b_fname != NULL)
4807 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02004808 char_u buff[DIALOG_MSG_SIZE];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004809
Bram Moolenaarda6e8912018-08-21 15:12:14 +02004810 vim_snprintf((char *)buff, DIALOG_MSG_SIZE,
4811 NGETTEXT("%d more file to edit. Quit anyway?",
4812 "%d more files to edit. Quit anyway?", n), n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004813 if (vim_dialog_yesno(VIM_QUESTION, NULL, buff, 1) == VIM_YES)
4814 return OK;
4815 return FAIL;
4816 }
4817#endif
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01004818 semsg(NGETTEXT("E173: %d more file to edit",
4819 "E173: %d more files to edit", n), n);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004820 quitmore = 2; // next try to quit is allowed
Bram Moolenaar071d4272004-06-13 20:20:40 +00004821 }
4822 return FAIL;
4823 }
4824 return OK;
4825}
4826
Bram Moolenaar071d4272004-06-13 20:20:40 +00004827/*
4828 * Function given to ExpandGeneric() to obtain the list of command names.
4829 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004830 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004831get_command_name(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004832{
4833 if (idx >= (int)CMD_SIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004834 return get_user_command_name(idx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004835 return cmdnames[idx].cmd_name;
4836}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004837
Bram Moolenaar071d4272004-06-13 20:20:40 +00004838 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004839ex_colorscheme(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004840{
Bram Moolenaare6850792010-05-14 15:28:44 +02004841 if (*eap->arg == NUL)
4842 {
4843#ifdef FEAT_EVAL
4844 char_u *expr = vim_strsave((char_u *)"g:colors_name");
4845 char_u *p = NULL;
4846
4847 if (expr != NULL)
4848 {
4849 ++emsg_off;
4850 p = eval_to_string(expr, NULL, FALSE);
4851 --emsg_off;
4852 vim_free(expr);
4853 }
4854 if (p != NULL)
4855 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01004856 msg((char *)p);
Bram Moolenaare6850792010-05-14 15:28:44 +02004857 vim_free(p);
4858 }
4859 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01004860 msg("default");
Bram Moolenaare6850792010-05-14 15:28:44 +02004861#else
Bram Moolenaar32526b32019-01-19 17:43:09 +01004862 msg(_("unknown"));
Bram Moolenaare6850792010-05-14 15:28:44 +02004863#endif
4864 }
4865 else if (load_colors(eap->arg) == FAIL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004866 semsg(_("E185: Cannot find color scheme '%s'"), eap->arg);
Bram Moolenaarf58d81a2019-01-28 20:19:05 +01004867
4868#ifdef FEAT_VTP
4869 else if (has_vtp_working())
4870 {
4871 // background color change requires clear + redraw
4872 update_screen(CLEAR);
4873 redrawcmd();
4874 }
4875#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004876}
4877
4878 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004879ex_highlight(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004880{
4881 if (*eap->arg == NUL && eap->cmd[2] == '!')
Bram Moolenaar32526b32019-01-19 17:43:09 +01004882 msg(_("Greetings, Vim user!"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004883 do_highlight(eap->arg, eap->forceit, FALSE);
4884}
4885
4886
4887/*
4888 * Call this function if we thought we were going to exit, but we won't
4889 * (because of an error). May need to restore the terminal mode.
4890 */
4891 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004892not_exiting(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004893{
4894 exiting = FALSE;
4895 settmode(TMODE_RAW);
4896}
4897
Bram Moolenaar12a96de2018-03-11 14:44:18 +01004898 static int
4899before_quit_autocmds(win_T *wp, int quit_all, int forceit)
4900{
4901 apply_autocmds(EVENT_QUITPRE, NULL, NULL, FALSE, wp->w_buffer);
4902
Bram Moolenaar34ba06b2019-10-20 22:27:10 +02004903 // Bail out when autocommands closed the window.
4904 // Refuse to quit when the buffer in the last window is being closed (can
4905 // only happen in autocommands).
Bram Moolenaar12a96de2018-03-11 14:44:18 +01004906 if (!win_valid(wp)
4907 || curbuf_locked()
4908 || (wp->w_buffer->b_nwindows == 1 && wp->w_buffer->b_locked > 0))
4909 return TRUE;
4910
4911 if (quit_all || (check_more(FALSE, forceit) == OK && only_one_window()))
4912 {
4913 apply_autocmds(EVENT_EXITPRE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar34ba06b2019-10-20 22:27:10 +02004914 // Refuse to quit when locked or when the window was closed or the
4915 // buffer in the last window is being closed (can only happen in
4916 // autocommands).
4917 if (!win_valid(wp) || curbuf_locked()
Bram Moolenaar12a96de2018-03-11 14:44:18 +01004918 || (curbuf->b_nwindows == 1 && curbuf->b_locked > 0))
4919 return TRUE;
4920 }
4921
4922 return FALSE;
4923}
4924
Bram Moolenaar071d4272004-06-13 20:20:40 +00004925/*
Bram Moolenaarf240e182014-11-27 18:33:02 +01004926 * ":quit": quit current window, quit Vim if the last window is closed.
Bram Moolenaar12a96de2018-03-11 14:44:18 +01004927 * ":{nr}quit": quit window {nr}
Bram Moolenaar4d14bac2019-10-20 21:15:15 +02004928 * Also used when closing a terminal window that's the last one.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004929 */
Bram Moolenaar4d14bac2019-10-20 21:15:15 +02004930 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004931ex_quit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004932{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004933 win_T *wp;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004934
Bram Moolenaar071d4272004-06-13 20:20:40 +00004935#ifdef FEAT_CMDWIN
4936 if (cmdwin_type != 0)
4937 {
4938 cmdwin_result = Ctrl_C;
4939 return;
4940 }
4941#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004942 // Don't quit while editing the command line.
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00004943 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00004944 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00004945 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00004946 return;
4947 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004948 if (eap->addr_count > 0)
4949 {
Bram Moolenaarf240e182014-11-27 18:33:02 +01004950 int wnr = eap->line2;
4951
4952 for (wp = firstwin; wp->w_next != NULL; wp = wp->w_next)
4953 if (--wnr <= 0)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004954 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004955 }
4956 else
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004957 wp = curwin;
Bram Moolenaarf240e182014-11-27 18:33:02 +01004958
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004959 // Refuse to quit when locked.
Bram Moolenaar87ffb5c2017-10-19 12:37:42 +02004960 if (curbuf_locked())
4961 return;
Bram Moolenaar12a96de2018-03-11 14:44:18 +01004962
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004963 // Trigger QuitPre and maybe ExitPre
Bram Moolenaar12a96de2018-03-11 14:44:18 +01004964 if (before_quit_autocmds(wp, FALSE, eap->forceit))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004965 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004966
4967#ifdef FEAT_NETBEANS_INTG
4968 netbeansForcedQuit = eap->forceit;
4969#endif
4970
4971 /*
4972 * If there are more files or windows we won't exit.
4973 */
4974 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
4975 exiting = TRUE;
Bram Moolenaarff930ca2017-10-19 17:12:10 +02004976 if ((!buf_hide(wp->w_buffer)
4977 && check_changed(wp->w_buffer, (p_awa ? CCGD_AW : 0)
Bram Moolenaar45d3b142013-11-09 03:31:51 +01004978 | (eap->forceit ? CCGD_FORCEIT : 0)
4979 | CCGD_EXCMD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004980 || check_more(TRUE, eap->forceit) == FAIL
Bram Moolenaar027387f2016-01-02 22:25:52 +01004981 || (only_one_window() && check_changed_any(eap->forceit, TRUE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004982 {
4983 not_exiting();
4984 }
4985 else
4986 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004987 // quit last window
4988 // Note: only_one_window() returns true, even so a help window is
4989 // still open. In that case only quit, if no address has been
4990 // specified. Example:
4991 // :h|wincmd w|1q - don't quit
4992 // :h|wincmd w|q - quit
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01004993 if (only_one_window() && (ONE_WINDOW || eap->addr_count == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004994 getout(0);
Bram Moolenaar2c33d7b2017-10-14 16:06:20 +02004995 not_exiting();
Bram Moolenaar4033c552017-09-16 20:54:51 +02004996#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004997 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02004998#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004999 // close window; may free buffer
Bram Moolenaareb44a682017-08-03 22:44:55 +02005000 win_close(wp, !buf_hide(wp->w_buffer) || eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005001 }
5002}
5003
5004/*
5005 * ":cquit".
5006 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005007 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005008ex_cquit(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005009{
Bram Moolenaar1860bde2020-01-06 21:47:21 +01005010 // this does not always pass on the exit code to the Manx compiler. why?
5011 getout(eap->addr_count > 0 ? (int)eap->line2 : EXIT_FAILURE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005012}
5013
5014/*
5015 * ":qall": try to quit all windows
5016 */
5017 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005018ex_quit_all(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005019{
5020# ifdef FEAT_CMDWIN
5021 if (cmdwin_type != 0)
5022 {
5023 if (eap->forceit)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005024 cmdwin_result = K_XF1; // ex_window() takes care of this
Bram Moolenaar071d4272004-06-13 20:20:40 +00005025 else
5026 cmdwin_result = K_XF2;
5027 return;
5028 }
5029# endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005030
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005031 // Don't quit while editing the command line.
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005032 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005033 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005034 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005035 return;
5036 }
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005037
5038 if (before_quit_autocmds(curwin, TRUE, eap->forceit))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005039 return;
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005040
Bram Moolenaar071d4272004-06-13 20:20:40 +00005041 exiting = TRUE;
Bram Moolenaar027387f2016-01-02 22:25:52 +01005042 if (eap->forceit || !check_changed_any(FALSE, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005043 getout(0);
5044 not_exiting();
5045}
5046
Bram Moolenaar071d4272004-06-13 20:20:40 +00005047/*
5048 * ":close": close current window, unless it is the last one
5049 */
5050 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005051ex_close(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005052{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005053 win_T *win;
5054 int winnr = 0;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005055#ifdef FEAT_CMDWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00005056 if (cmdwin_type != 0)
Bram Moolenaar9bd1a7e2011-05-19 14:50:54 +02005057 cmdwin_result = Ctrl_C;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005058 else
Bram Moolenaar4033c552017-09-16 20:54:51 +02005059#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005060 if (!text_locked() && !curbuf_locked())
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005061 {
5062 if (eap->addr_count == 0)
5063 ex_win_close(eap->forceit, curwin, NULL);
Bram Moolenaar6d41c782018-06-06 09:11:12 +02005064 else
5065 {
Bram Moolenaar29323592016-07-24 22:04:11 +02005066 FOR_ALL_WINDOWS(win)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005067 {
5068 winnr++;
5069 if (winnr == eap->line2)
5070 break;
5071 }
5072 if (win == NULL)
5073 win = lastwin;
5074 ex_win_close(eap->forceit, win, NULL);
5075 }
5076 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005077}
5078
Bram Moolenaar4033c552017-09-16 20:54:51 +02005079#ifdef FEAT_QUICKFIX
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005080/*
5081 * ":pclose": Close any preview window.
5082 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005083 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005084ex_pclose(exarg_T *eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005085{
5086 win_T *win;
5087
Bram Moolenaar79648732019-07-18 21:43:07 +02005088 // First close any normal window.
Bram Moolenaar29323592016-07-24 22:04:11 +02005089 FOR_ALL_WINDOWS(win)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005090 if (win->w_p_pvw)
5091 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00005092 ex_win_close(eap->forceit, win, NULL);
Bram Moolenaar79648732019-07-18 21:43:07 +02005093 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005094 }
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01005095# ifdef FEAT_PROP_POPUP
Bram Moolenaar79648732019-07-18 21:43:07 +02005096 // Also when 'previewpopup' is empty, it might have been cleared.
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02005097 popup_close_preview();
Bram Moolenaar79648732019-07-18 21:43:07 +02005098# endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005099}
Bram Moolenaar4033c552017-09-16 20:54:51 +02005100#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005101
Bram Moolenaarf740b292006-02-16 22:11:02 +00005102/*
5103 * Close window "win" and take care of handling closing the last window for a
5104 * modified buffer.
5105 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005106 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005107ex_win_close(
5108 int forceit,
5109 win_T *win,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005110 tabpage_T *tp) // NULL or the tab page "win" is in
Bram Moolenaar071d4272004-06-13 20:20:40 +00005111{
5112 int need_hide;
5113 buf_T *buf = win->w_buffer;
5114
5115 need_hide = (bufIsChanged(buf) && buf->b_nwindows <= 1);
Bram Moolenaareb44a682017-08-03 22:44:55 +02005116 if (need_hide && !buf_hide(buf) && !forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005117 {
Bram Moolenaar4033c552017-09-16 20:54:51 +02005118#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005119 if ((p_confirm || cmdmod.confirm) && p_write)
5120 {
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005121 bufref_T bufref;
5122
5123 set_bufref(&bufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005124 dialog_changed(buf, FALSE);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005125 if (bufref_valid(&bufref) && bufIsChanged(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005126 return;
5127 need_hide = FALSE;
5128 }
5129 else
Bram Moolenaar4033c552017-09-16 20:54:51 +02005130#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005131 {
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02005132 no_write_message();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005133 return;
5134 }
5135 }
5136
Bram Moolenaar4033c552017-09-16 20:54:51 +02005137#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00005138 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005139#endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00005140
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005141 // free buffer when not hiding it or when it's a scratch buffer
Bram Moolenaarf740b292006-02-16 22:11:02 +00005142 if (tp == NULL)
Bram Moolenaareb44a682017-08-03 22:44:55 +02005143 win_close(win, !need_hide && !buf_hide(buf));
Bram Moolenaarf740b292006-02-16 22:11:02 +00005144 else
Bram Moolenaareb44a682017-08-03 22:44:55 +02005145 win_close_othertab(win, !need_hide && !buf_hide(buf), tp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005146}
5147
Bram Moolenaar071d4272004-06-13 20:20:40 +00005148/*
Bram Moolenaardea25702017-01-29 15:18:10 +01005149 * Handle the argument for a tabpage related ex command.
5150 * Returns a tabpage number.
5151 * When an error is encountered then eap->errmsg is set.
5152 */
5153 static int
5154get_tabpage_arg(exarg_T *eap)
5155{
5156 int tab_number;
5157 int unaccept_arg0 = (eap->cmdidx == CMD_tabmove) ? 0 : 1;
5158
5159 if (eap->arg && *eap->arg != NUL)
5160 {
5161 char_u *p = eap->arg;
5162 char_u *p_save;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005163 int relative = 0; // argument +N/-N means: go to N places to the
5164 // right/left relative to the current position.
Bram Moolenaardea25702017-01-29 15:18:10 +01005165
5166 if (*p == '-')
5167 {
5168 relative = -1;
5169 p++;
5170 }
5171 else if (*p == '+')
5172 {
5173 relative = 1;
5174 p++;
5175 }
5176
5177 p_save = p;
5178 tab_number = getdigits(&p);
5179
5180 if (relative == 0)
5181 {
5182 if (STRCMP(p, "$") == 0)
5183 tab_number = LAST_TAB_NR;
5184 else if (p == p_save || *p_save == '-' || *p != NUL
5185 || tab_number > LAST_TAB_NR)
5186 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005187 // No numbers as argument.
Bram Moolenaardea25702017-01-29 15:18:10 +01005188 eap->errmsg = e_invarg;
5189 goto theend;
5190 }
5191 }
5192 else
5193 {
5194 if (*p_save == NUL)
5195 tab_number = 1;
5196 else if (p == p_save || *p_save == '-' || *p != NUL
5197 || tab_number == 0)
5198 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005199 // No numbers as argument.
Bram Moolenaardea25702017-01-29 15:18:10 +01005200 eap->errmsg = e_invarg;
5201 goto theend;
5202 }
5203 tab_number = tab_number * relative + tabpage_index(curtab);
5204 if (!unaccept_arg0 && relative == -1)
5205 --tab_number;
5206 }
5207 if (tab_number < unaccept_arg0 || tab_number > LAST_TAB_NR)
5208 eap->errmsg = e_invarg;
5209 }
5210 else if (eap->addr_count > 0)
5211 {
5212 if (unaccept_arg0 && eap->line2 == 0)
Bram Moolenaarc6251552017-01-29 21:42:20 +01005213 {
Bram Moolenaardea25702017-01-29 15:18:10 +01005214 eap->errmsg = e_invrange;
Bram Moolenaarc6251552017-01-29 21:42:20 +01005215 tab_number = 0;
5216 }
Bram Moolenaardea25702017-01-29 15:18:10 +01005217 else
5218 {
5219 tab_number = eap->line2;
Bram Moolenaar82a12462019-01-22 22:41:42 +01005220 if (!unaccept_arg0 && *skipwhite(*eap->cmdlinep) == '-')
Bram Moolenaardea25702017-01-29 15:18:10 +01005221 {
5222 --tab_number;
5223 if (tab_number < unaccept_arg0)
5224 eap->errmsg = e_invarg;
5225 }
5226 }
5227 }
5228 else
5229 {
5230 switch (eap->cmdidx)
5231 {
5232 case CMD_tabnext:
5233 tab_number = tabpage_index(curtab) + 1;
5234 if (tab_number > LAST_TAB_NR)
5235 tab_number = 1;
5236 break;
5237 case CMD_tabmove:
5238 tab_number = LAST_TAB_NR;
5239 break;
5240 default:
5241 tab_number = tabpage_index(curtab);
5242 }
5243 }
5244
5245theend:
5246 return tab_number;
5247}
5248
5249/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00005250 * ":tabclose": close current tab page, unless it is the last one.
5251 * ":tabclose N": close tab page N.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005252 */
5253 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005254ex_tabclose(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005255{
Bram Moolenaarf740b292006-02-16 22:11:02 +00005256 tabpage_T *tp;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005257 int tab_number;
Bram Moolenaarf740b292006-02-16 22:11:02 +00005258
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005259# ifdef FEAT_CMDWIN
5260 if (cmdwin_type != 0)
5261 cmdwin_result = K_IGNORE;
5262 else
5263# endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00005264 if (first_tabpage->tp_next == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005265 emsg(_("E784: Cannot close last tab page"));
Bram Moolenaarf740b292006-02-16 22:11:02 +00005266 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005267 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005268 tab_number = get_tabpage_arg(eap);
5269 if (eap->errmsg == NULL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005270 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005271 tp = find_tabpage(tab_number);
Bram Moolenaarf740b292006-02-16 22:11:02 +00005272 if (tp == NULL)
5273 {
5274 beep_flush();
5275 return;
5276 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005277 if (tp != curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00005278 {
5279 tabpage_close_other(tp, eap->forceit);
5280 return;
5281 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005282 else if (!text_locked() && !curbuf_locked())
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005283 tabpage_close(eap->forceit);
5284 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005285 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005286}
5287
5288/*
5289 * ":tabonly": close all tab pages except the current one
5290 */
5291 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005292ex_tabonly(exarg_T *eap)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005293{
5294 tabpage_T *tp;
5295 int done;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005296 int tab_number;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005297
5298# ifdef FEAT_CMDWIN
5299 if (cmdwin_type != 0)
5300 cmdwin_result = K_IGNORE;
5301 else
5302# endif
5303 if (first_tabpage->tp_next == NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01005304 msg(_("Already only one tab page"));
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005305 else
5306 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005307 tab_number = get_tabpage_arg(eap);
5308 if (eap->errmsg == NULL)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005309 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005310 goto_tabpage(tab_number);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005311 // Repeat this up to a 1000 times, because autocommands may
5312 // mess up the lists.
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005313 for (done = 0; done < 1000; ++done)
5314 {
5315 FOR_ALL_TABPAGES(tp)
5316 if (tp->tp_topframe != topframe)
5317 {
5318 tabpage_close_other(tp, eap->forceit);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005319 // if we failed to close it quit
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005320 if (valid_tabpage(tp))
5321 done = 1000;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005322 // start over, "tp" is now invalid
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005323 break;
5324 }
5325 if (first_tabpage->tp_next == NULL)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005326 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005327 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005328 }
5329 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005330}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005331
5332/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00005333 * Close the current tab page.
5334 */
5335 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005336tabpage_close(int forceit)
Bram Moolenaarf740b292006-02-16 22:11:02 +00005337{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005338 // First close all the windows but the current one. If that worked then
5339 // close the last window in this tab, that will close it.
Bram Moolenaar459ca562016-11-10 18:16:33 +01005340 if (!ONE_WINDOW)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005341 close_others(TRUE, forceit);
Bram Moolenaar459ca562016-11-10 18:16:33 +01005342 if (ONE_WINDOW)
Bram Moolenaarf740b292006-02-16 22:11:02 +00005343 ex_win_close(forceit, curwin, NULL);
5344# ifdef FEAT_GUI
5345 need_mouse_correct = TRUE;
5346# endif
5347}
5348
5349/*
5350 * Close tab page "tp", which is not the current tab page.
5351 * Note that autocommands may make "tp" invalid.
Bram Moolenaar7875acc2006-09-10 13:51:17 +00005352 * Also takes care of the tab pages line disappearing when closing the
5353 * last-but-one tab page.
Bram Moolenaarf740b292006-02-16 22:11:02 +00005354 */
5355 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005356tabpage_close_other(tabpage_T *tp, int forceit)
Bram Moolenaarf740b292006-02-16 22:11:02 +00005357{
5358 int done = 0;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005359 win_T *wp;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00005360 int h = tabline_height();
Bram Moolenaarf740b292006-02-16 22:11:02 +00005361
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005362 // Limit to 1000 windows, autocommands may add a window while we close
5363 // one. OK, so I'm paranoid...
Bram Moolenaarf740b292006-02-16 22:11:02 +00005364 while (++done < 1000)
5365 {
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005366 wp = tp->tp_firstwin;
5367 ex_win_close(forceit, wp, tp);
Bram Moolenaarf740b292006-02-16 22:11:02 +00005368
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005369 // Autocommands may delete the tab page under our fingers and we may
5370 // fail to close a window with a modified buffer.
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005371 if (!valid_tabpage(tp) || tp->tp_firstwin == wp)
Bram Moolenaarf740b292006-02-16 22:11:02 +00005372 break;
5373 }
Bram Moolenaar7875acc2006-09-10 13:51:17 +00005374
Bram Moolenaar29323592016-07-24 22:04:11 +02005375 apply_autocmds(EVENT_TABCLOSED, NULL, NULL, FALSE, curbuf);
Bram Moolenaar29323592016-07-24 22:04:11 +02005376
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005377 redraw_tabline = TRUE;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00005378 if (h != tabline_height())
5379 shell_new_rows();
Bram Moolenaarf740b292006-02-16 22:11:02 +00005380}
5381
5382/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005383 * ":only".
5384 */
5385 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005386ex_only(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005387{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005388 win_T *wp;
5389 int wnr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005390# ifdef FEAT_GUI
5391 need_mouse_correct = TRUE;
5392# endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005393 if (eap->addr_count > 0)
5394 {
5395 wnr = eap->line2;
5396 for (wp = firstwin; --wnr > 0; )
5397 {
5398 if (wp->w_next == NULL)
5399 break;
5400 else
5401 wp = wp->w_next;
5402 }
5403 win_goto(wp);
5404 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005405 close_others(TRUE, eap->forceit);
5406}
5407
Bram Moolenaar071d4272004-06-13 20:20:40 +00005408 static void
Bram Moolenaar5e1e6d22017-01-02 17:26:00 +01005409ex_hide(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005410{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005411 // ":hide" or ":hide | cmd": hide current window
Bram Moolenaar2256c992016-11-15 21:17:07 +01005412 if (!eap->skip)
5413 {
Bram Moolenaar4033c552017-09-16 20:54:51 +02005414#ifdef FEAT_GUI
Bram Moolenaar2256c992016-11-15 21:17:07 +01005415 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005416#endif
Bram Moolenaar2256c992016-11-15 21:17:07 +01005417 if (eap->addr_count == 0)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005418 win_close(curwin, FALSE); // don't free buffer
Bram Moolenaar2256c992016-11-15 21:17:07 +01005419 else
5420 {
5421 int winnr = 0;
5422 win_T *win;
Bram Moolenaarf240e182014-11-27 18:33:02 +01005423
Bram Moolenaar2256c992016-11-15 21:17:07 +01005424 FOR_ALL_WINDOWS(win)
5425 {
5426 winnr++;
5427 if (winnr == eap->line2)
5428 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005429 }
Bram Moolenaar2256c992016-11-15 21:17:07 +01005430 if (win == NULL)
5431 win = lastwin;
5432 win_close(win, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005433 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005434 }
5435}
5436
5437/*
5438 * ":stop" and ":suspend": Suspend Vim.
5439 */
5440 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005441ex_stop(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005442{
5443 /*
5444 * Disallow suspending for "rvim".
5445 */
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005446 if (!check_restricted())
Bram Moolenaar071d4272004-06-13 20:20:40 +00005447 {
5448 if (!eap->forceit)
5449 autowrite_all();
5450 windgoto((int)Rows - 1, 0);
5451 out_char('\n');
5452 out_flush();
5453 stoptermcap();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005454 out_flush(); // needed for SUN to restore xterm buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00005455#ifdef FEAT_TITLE
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005456 mch_restore_title(SAVE_RESTORE_BOTH); // restore window titles
Bram Moolenaar071d4272004-06-13 20:20:40 +00005457#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005458 ui_suspend(); // call machine specific function
Bram Moolenaar071d4272004-06-13 20:20:40 +00005459#ifdef FEAT_TITLE
5460 maketitle();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005461 resettitle(); // force updating the title
Bram Moolenaar071d4272004-06-13 20:20:40 +00005462#endif
5463 starttermcap();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005464 scroll_start(); // scroll screen before redrawing
Bram Moolenaar071d4272004-06-13 20:20:40 +00005465 redraw_later_clear();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005466 shell_resized(); // may have resized window
Bram Moolenaar071d4272004-06-13 20:20:40 +00005467 }
5468}
5469
5470/*
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005471 * ":exit", ":xit" and ":wq": Write file and quite the current window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005472 */
5473 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005474ex_exit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005475{
5476#ifdef FEAT_CMDWIN
5477 if (cmdwin_type != 0)
5478 {
5479 cmdwin_result = Ctrl_C;
5480 return;
5481 }
5482#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005483 // Don't quit while editing the command line.
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005484 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005485 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005486 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005487 return;
5488 }
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005489
5490 if (before_quit_autocmds(curwin, FALSE, eap->forceit))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005491 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005492
5493 /*
5494 * if more files or windows we won't exit
5495 */
5496 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
5497 exiting = TRUE;
5498 if ( ((eap->cmdidx == CMD_wq
5499 || curbufIsChanged())
5500 && do_write(eap) == FAIL)
5501 || check_more(TRUE, eap->forceit) == FAIL
Bram Moolenaar027387f2016-01-02 22:25:52 +01005502 || (only_one_window() && check_changed_any(eap->forceit, FALSE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005503 {
5504 not_exiting();
5505 }
5506 else
5507 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005508 if (only_one_window()) // quit last window, exit Vim
Bram Moolenaar071d4272004-06-13 20:20:40 +00005509 getout(0);
Bram Moolenaar2c33d7b2017-10-14 16:06:20 +02005510 not_exiting();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005511# ifdef FEAT_GUI
5512 need_mouse_correct = TRUE;
5513# endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005514 // Quit current window, may free the buffer.
Bram Moolenaareb44a682017-08-03 22:44:55 +02005515 win_close(curwin, !buf_hide(curwin->w_buffer));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005516 }
5517}
5518
5519/*
5520 * ":print", ":list", ":number".
5521 */
5522 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005523ex_print(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005524{
Bram Moolenaardf177f62005-02-22 08:39:57 +00005525 if (curbuf->b_ml.ml_flags & ML_EMPTY)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005526 emsg(_(e_emptybuf));
Bram Moolenaardf177f62005-02-22 08:39:57 +00005527 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005528 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00005529 for ( ;!got_int; ui_breakcheck())
5530 {
5531 print_line(eap->line1,
5532 (eap->cmdidx == CMD_number || eap->cmdidx == CMD_pound
5533 || (eap->flags & EXFLAG_NR)),
5534 eap->cmdidx == CMD_list || (eap->flags & EXFLAG_LIST));
5535 if (++eap->line1 > eap->line2)
5536 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005537 out_flush(); // show one line at a time
Bram Moolenaardf177f62005-02-22 08:39:57 +00005538 }
5539 setpcmark();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005540 // put cursor at last line
Bram Moolenaardf177f62005-02-22 08:39:57 +00005541 curwin->w_cursor.lnum = eap->line2;
5542 beginline(BL_SOL | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005543 }
5544
Bram Moolenaar071d4272004-06-13 20:20:40 +00005545 ex_no_reprint = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005546}
5547
5548#ifdef FEAT_BYTEOFF
5549 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005550ex_goto(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005551{
5552 goto_byte(eap->line2);
5553}
5554#endif
5555
5556/*
5557 * ":shell".
5558 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005559 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005560ex_shell(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005561{
5562 do_shell(NULL, 0);
5563}
5564
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005565#if defined(HAVE_DROP_FILE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005566
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005567static int drop_busy = FALSE;
5568static int drop_filec;
5569static char_u **drop_filev = NULL;
5570static int drop_split;
5571static void (*drop_callback)(void *);
5572static void *drop_cookie;
5573
5574 static void
5575handle_drop_internal(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005576{
5577 exarg_T ea;
5578 int save_msg_scroll = msg_scroll;
5579
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005580 // Setting the argument list may cause screen updates and being called
5581 // recursively. Avoid that by setting drop_busy.
5582 drop_busy = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005583
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005584 // Check whether the current buffer is changed. If so, we will need
5585 // to split the current window or data could be lost.
5586 // We don't need to check if the 'hidden' option is set, as in this
5587 // case the buffer won't be lost.
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005588 if (!buf_hide(curbuf) && !drop_split)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005589 {
5590 ++emsg_off;
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005591 drop_split = check_changed(curbuf, CCGD_AW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005592 --emsg_off;
5593 }
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005594 if (drop_split)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005595 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005596 if (win_split(0, 0) == FAIL)
5597 return;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02005598 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005599
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005600 // When splitting the window, create a new alist. Otherwise the
5601 // existing one is overwritten.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005602 alist_unlink(curwin->w_alist);
5603 alist_new();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005604 }
5605
5606 /*
5607 * Set up the new argument list.
5608 */
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005609 alist_set(ALIST(curwin), drop_filec, drop_filev, FALSE, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005610
5611 /*
5612 * Move to the first file.
5613 */
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005614 // Fake up a minimal "next" command for do_argfile()
Bram Moolenaar071d4272004-06-13 20:20:40 +00005615 vim_memset(&ea, 0, sizeof(ea));
5616 ea.cmd = (char_u *)"next";
5617 do_argfile(&ea, 0);
5618
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005619 // do_ecmd() may set need_start_insertmode, but since we never left Insert
5620 // mode that is not needed here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005621 need_start_insertmode = FALSE;
5622
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005623 // Restore msg_scroll, otherwise a following command may cause scrolling
5624 // unexpectedly. The screen will be redrawn by the caller, thus
5625 // msg_scroll being set by displaying a message is irrelevant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005626 msg_scroll = save_msg_scroll;
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005627
5628 if (drop_callback != NULL)
5629 drop_callback(drop_cookie);
5630
5631 drop_filev = NULL;
5632 drop_busy = FALSE;
5633}
5634
5635/*
5636 * Handle a file drop. The code is here because a drop is *nearly* like an
5637 * :args command, but not quite (we have a list of exact filenames, so we
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01005638 * don't want to (a) parse a command line, or (b) expand wildcards). So the
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005639 * code is very similar to :args and hence needs access to a lot of the static
5640 * functions in this file.
5641 *
5642 * The "filev" list must have been allocated using alloc(), as should each item
5643 * in the list. This function takes over responsibility for freeing the "filev"
5644 * list.
5645 */
5646 void
5647handle_drop(
5648 int filec, // the number of files dropped
5649 char_u **filev, // the list of files dropped
5650 int split, // force splitting the window
5651 void (*callback)(void *), // to be called after setting the argument
5652 // list
5653 void *cookie) // argument for "callback" (allocated)
5654{
5655 // Cannot handle recursive drops, finish the pending one.
5656 if (drop_busy)
5657 {
5658 FreeWild(filec, filev);
5659 vim_free(cookie);
5660 return;
5661 }
5662
5663 // When calling handle_drop() more than once in a row we only use the last
5664 // one.
5665 if (drop_filev != NULL)
5666 {
5667 FreeWild(drop_filec, drop_filev);
5668 vim_free(drop_cookie);
5669 }
5670
5671 drop_filec = filec;
5672 drop_filev = filev;
5673 drop_split = split;
5674 drop_callback = callback;
5675 drop_cookie = cookie;
5676
5677 // Postpone this when:
5678 // - editing the command line
5679 // - not possible to change the current buffer
5680 // - updating the screen
5681 // As it may change buffers and window structures that are in use and cause
5682 // freed memory to be used.
5683 if (text_locked() || curbuf_locked() || updating_screen)
5684 return;
5685
5686 handle_drop_internal();
5687}
5688
5689/*
5690 * To be called when text is unlocked, curbuf is unlocked or updating_screen is
5691 * reset: Handle a postponed drop.
5692 */
5693 void
5694handle_any_postponed_drop(void)
5695{
5696 if (!drop_busy && drop_filev != NULL
5697 && !text_locked() && !curbuf_locked() && !updating_screen)
5698 handle_drop_internal();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005699}
5700#endif
5701
Bram Moolenaar071d4272004-06-13 20:20:40 +00005702/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005703 * ":preserve".
5704 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005705 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005706ex_preserve(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005707{
Bram Moolenaar4399ef42005-02-12 14:29:27 +00005708 curbuf->b_flags |= BF_PRESERVED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005709 ml_preserve(curbuf, TRUE);
5710}
5711
5712/*
5713 * ":recover".
5714 */
5715 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005716ex_recover(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005717{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005718 // Set recoverymode right away to avoid the ATTENTION prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005719 recoverymode = TRUE;
Bram Moolenaar45d3b142013-11-09 03:31:51 +01005720 if (!check_changed(curbuf, (p_awa ? CCGD_AW : 0)
5721 | CCGD_MULTWIN
5722 | (eap->forceit ? CCGD_FORCEIT : 0)
5723 | CCGD_EXCMD)
5724
Bram Moolenaar071d4272004-06-13 20:20:40 +00005725 && (*eap->arg == NUL
5726 || setfname(curbuf, eap->arg, NULL, TRUE) == OK))
Bram Moolenaar99499b12019-05-23 21:35:48 +02005727 ml_recover(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005728 recoverymode = FALSE;
5729}
5730
5731/*
5732 * Command modifier used in a wrong way.
5733 */
5734 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005735ex_wrongmodifier(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005736{
5737 eap->errmsg = e_invcmd;
5738}
5739
Bram Moolenaar071d4272004-06-13 20:20:40 +00005740/*
5741 * :sview [+command] file split window with new file, read-only
5742 * :split [[+command] file] split window with current or new file
5743 * :vsplit [[+command] file] split window vertically with current or new file
5744 * :new [[+command] file] split window with no or new file
5745 * :vnew [[+command] file] split vertically window with no or new file
5746 * :sfind [+command] file split window with file in 'path'
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005747 *
5748 * :tabedit open new Tab page with empty window
5749 * :tabedit [+command] file open new Tab page and edit "file"
5750 * :tabnew [[+command] file] just like :tabedit
5751 * :tabfind [+command] file open new Tab page and find "file"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005752 */
5753 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005754ex_splitview(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005755{
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005756 win_T *old_curwin = curwin;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005757#if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005758 char_u *fname = NULL;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005759#endif
5760#ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00005761 int browse_flag = cmdmod.browse;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005762#endif
Bram Moolenaar39902a02018-06-21 22:10:08 +02005763 int use_tab = eap->cmdidx == CMD_tabedit
5764 || eap->cmdidx == CMD_tabfind
5765 || eap->cmdidx == CMD_tabnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005766
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01005767 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02005768 return;
5769
Bram Moolenaar4033c552017-09-16 20:54:51 +02005770#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00005771 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005772#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005773
Bram Moolenaar4033c552017-09-16 20:54:51 +02005774#ifdef FEAT_QUICKFIX
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005775 // A ":split" in the quickfix window works like ":new". Don't want two
5776 // quickfix windows. But it's OK when doing ":tab split".
Bram Moolenaar05bb9532008-07-04 09:44:11 +00005777 if (bt_quickfix(curbuf) && cmdmod.tab == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005778 {
5779 if (eap->cmdidx == CMD_split)
5780 eap->cmdidx = CMD_new;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005781 if (eap->cmdidx == CMD_vsplit)
5782 eap->cmdidx = CMD_vnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005783 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02005784#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005785
Bram Moolenaar4033c552017-09-16 20:54:51 +02005786#ifdef FEAT_SEARCHPATH
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005787 if (eap->cmdidx == CMD_sfind || eap->cmdidx == CMD_tabfind)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005788 {
5789 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg),
5790 FNAME_MESS, TRUE, curbuf->b_ffname);
5791 if (fname == NULL)
5792 goto theend;
5793 eap->arg = fname;
5794 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005795# ifdef FEAT_BROWSE
Bram Moolenaar4033c552017-09-16 20:54:51 +02005796 else
5797# endif
5798#endif
5799#ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00005800 if (cmdmod.browse
Bram Moolenaar071d4272004-06-13 20:20:40 +00005801 && eap->cmdidx != CMD_vnew
Bram Moolenaar071d4272004-06-13 20:20:40 +00005802 && eap->cmdidx != CMD_new)
5803 {
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00005804 if (
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005805# ifdef FEAT_GUI
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00005806 !gui.in_use &&
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005807# endif
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00005808 au_has_group((char_u *)"FileExplorer"))
5809 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005810 // No browsing supported but we do have the file explorer:
5811 // Edit the directory.
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00005812 if (*eap->arg == NUL || !mch_isdir(eap->arg))
5813 eap->arg = (char_u *)".";
5814 }
5815 else
5816 {
Bram Moolenaar39902a02018-06-21 22:10:08 +02005817 fname = do_browse(0, (char_u *)(use_tab
5818 ? _("Edit File in new tab page")
5819 : _("Edit File in new window")),
Bram Moolenaar071d4272004-06-13 20:20:40 +00005820 eap->arg, NULL, NULL, NULL, curbuf);
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00005821 if (fname == NULL)
5822 goto theend;
5823 eap->arg = fname;
5824 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005825 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005826 cmdmod.browse = FALSE; // Don't browse again in do_ecmd().
Bram Moolenaar4033c552017-09-16 20:54:51 +02005827#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005828
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005829 /*
5830 * Either open new tab page or split the window.
5831 */
Bram Moolenaar39902a02018-06-21 22:10:08 +02005832 if (use_tab)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005833 {
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005834 if (win_new_tabpage(cmdmod.tab != 0 ? cmdmod.tab
5835 : eap->addr_count == 0 ? 0
5836 : (int)eap->line2 + 1) != FAIL)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005837 {
Bram Moolenaard2b66012008-01-09 19:30:36 +00005838 do_exedit(eap, old_curwin);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005839
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005840 // set the alternate buffer for the window we came from
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005841 if (curwin != old_curwin
5842 && win_valid(old_curwin)
5843 && old_curwin->w_buffer != curbuf
5844 && !cmdmod.keepalt)
5845 old_curwin->w_alt_fnum = curbuf->b_fnum;
5846 }
5847 }
5848 else if (win_split(eap->addr_count > 0 ? (int)eap->line2 : 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005849 *eap->cmd == 'v' ? WSP_VERT : 0) != FAIL)
5850 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005851 // Reset 'scrollbind' when editing another file, but keep it when
5852 // doing ":split" without arguments.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005853 if (*eap->arg != NUL
Bram Moolenaar8a3bb562018-03-04 20:14:14 +01005854# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00005855 || cmdmod.browse
Bram Moolenaar8a3bb562018-03-04 20:14:14 +01005856# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005857 )
Bram Moolenaar3368ea22010-09-21 16:56:35 +02005858 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005859 else
5860 do_check_scrollbind(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005861 do_exedit(eap, old_curwin);
5862 }
5863
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005864# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00005865 cmdmod.browse = browse_flag;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005866# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005867
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005868# if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005869theend:
5870 vim_free(fname);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005871# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005872}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005873
5874/*
Bram Moolenaar80a94a52006-02-23 21:26:58 +00005875 * Open a new tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005876 */
5877 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005878tabpage_new(void)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00005879{
5880 exarg_T ea;
5881
5882 vim_memset(&ea, 0, sizeof(ea));
5883 ea.cmdidx = CMD_tabnew;
5884 ea.cmd = (char_u *)"tabn";
5885 ea.arg = (char_u *)"";
5886 ex_splitview(&ea);
5887}
5888
5889/*
5890 * :tabnext command
5891 */
5892 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005893ex_tabnext(exarg_T *eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005894{
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005895 int tab_number;
5896
Bram Moolenaar8cdbd5b2019-06-16 15:50:45 +02005897 if (ERROR_IF_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02005898 return;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005899 switch (eap->cmdidx)
5900 {
5901 case CMD_tabfirst:
5902 case CMD_tabrewind:
5903 goto_tabpage(1);
5904 break;
5905 case CMD_tablast:
5906 goto_tabpage(9999);
5907 break;
5908 case CMD_tabprevious:
5909 case CMD_tabNext:
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005910 if (eap->arg && *eap->arg != NUL)
5911 {
5912 char_u *p = eap->arg;
5913 char_u *p_save = p;
5914
5915 tab_number = getdigits(&p);
5916 if (p == p_save || *p_save == '-' || *p != NUL
5917 || tab_number == 0)
5918 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005919 // No numbers as argument.
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005920 eap->errmsg = e_invarg;
5921 return;
5922 }
5923 }
5924 else
5925 {
5926 if (eap->addr_count == 0)
5927 tab_number = 1;
5928 else
5929 {
5930 tab_number = eap->line2;
5931 if (tab_number < 1)
5932 {
5933 eap->errmsg = e_invrange;
5934 return;
5935 }
5936 }
5937 }
5938 goto_tabpage(-tab_number);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005939 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005940 default: // CMD_tabnext
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005941 tab_number = get_tabpage_arg(eap);
5942 if (eap->errmsg == NULL)
5943 goto_tabpage(tab_number);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005944 break;
5945 }
Bram Moolenaar80a94a52006-02-23 21:26:58 +00005946}
5947
5948/*
5949 * :tabmove command
5950 */
5951 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005952ex_tabmove(exarg_T *eap)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00005953{
Bram Moolenaar40ce3a42015-04-21 18:08:39 +02005954 int tab_number;
Bram Moolenaar8cb8dca2012-07-06 18:27:39 +02005955
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005956 tab_number = get_tabpage_arg(eap);
5957 if (eap->errmsg == NULL)
5958 tabpage_move(tab_number);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005959}
5960
5961/*
5962 * :tabs command: List tabs and their contents.
5963 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005964 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005965ex_tabs(exarg_T *eap UNUSED)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005966{
5967 tabpage_T *tp;
5968 win_T *wp;
5969 int tabcount = 1;
5970
5971 msg_start();
5972 msg_scroll = TRUE;
5973 for (tp = first_tabpage; tp != NULL && !got_int; tp = tp->tp_next)
5974 {
5975 msg_putchar('\n');
5976 vim_snprintf((char *)IObuff, IOSIZE, _("Tab page %d"), tabcount++);
Bram Moolenaar8820b482017-03-16 17:23:31 +01005977 msg_outtrans_attr(IObuff, HL_ATTR(HLF_T));
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005978 out_flush(); // output one line at a time
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005979 ui_breakcheck();
5980
Bram Moolenaar030f0df2006-02-21 22:02:53 +00005981 if (tp == curtab)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005982 wp = firstwin;
5983 else
5984 wp = tp->tp_firstwin;
5985 for ( ; wp != NULL && !got_int; wp = wp->w_next)
5986 {
Bram Moolenaar80a94a52006-02-23 21:26:58 +00005987 msg_putchar('\n');
5988 msg_putchar(wp == curwin ? '>' : ' ');
5989 msg_putchar(' ');
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005990 msg_putchar(bufIsChanged(wp->w_buffer) ? '+' : ' ');
5991 msg_putchar(' ');
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005992 if (buf_spname(wp->w_buffer) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02005993 vim_strncpy(IObuff, buf_spname(wp->w_buffer), IOSIZE - 1);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005994 else
5995 home_replace(wp->w_buffer, wp->w_buffer->b_fname,
5996 IObuff, IOSIZE, TRUE);
5997 msg_outtrans(IObuff);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005998 out_flush(); // output one line at a time
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005999 ui_breakcheck();
6000 }
6001 }
6002}
6003
Bram Moolenaar071d4272004-06-13 20:20:40 +00006004/*
6005 * ":mode": Set screen mode.
6006 * If no argument given, just get the screen size and redraw.
6007 */
6008 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006009ex_mode(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006010{
6011 if (*eap->arg == NUL)
6012 shell_resized();
6013 else
Bram Moolenaar3c71aec2020-01-17 19:32:20 +01006014 emsg(_(e_screenmode));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006015}
6016
Bram Moolenaar071d4272004-06-13 20:20:40 +00006017/*
6018 * ":resize".
6019 * set, increment or decrement current window height
6020 */
6021 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006022ex_resize(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006023{
6024 int n;
6025 win_T *wp = curwin;
6026
6027 if (eap->addr_count > 0)
6028 {
6029 n = eap->line2;
6030 for (wp = firstwin; wp->w_next != NULL && --n > 0; wp = wp->w_next)
6031 ;
6032 }
6033
Bram Moolenaar44a2f922016-03-19 22:11:51 +01006034# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00006035 need_mouse_correct = TRUE;
Bram Moolenaar44a2f922016-03-19 22:11:51 +01006036# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006037 n = atol((char *)eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006038 if (cmdmod.split & WSP_VERT)
6039 {
6040 if (*eap->arg == '-' || *eap->arg == '+')
Bram Moolenaar02631462017-09-22 15:20:32 +02006041 n += curwin->w_width;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006042 else if (n == 0 && eap->arg[0] == NUL) // default is very wide
Bram Moolenaar071d4272004-06-13 20:20:40 +00006043 n = 9999;
6044 win_setwidth_win((int)n, wp);
6045 }
6046 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006047 {
6048 if (*eap->arg == '-' || *eap->arg == '+')
6049 n += curwin->w_height;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006050 else if (n == 0 && eap->arg[0] == NUL) // default is very high
Bram Moolenaar071d4272004-06-13 20:20:40 +00006051 n = 9999;
6052 win_setheight_win((int)n, wp);
6053 }
6054}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006055
6056/*
6057 * ":find [+command] <file>" command.
6058 */
6059 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006060ex_find(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006061{
6062#ifdef FEAT_SEARCHPATH
6063 char_u *fname;
6064 int count;
6065
6066 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg), FNAME_MESS,
6067 TRUE, curbuf->b_ffname);
6068 if (eap->addr_count > 0)
6069 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006070 // Repeat finding the file "count" times. This matters when it
6071 // appears several times in the path.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006072 count = eap->line2;
6073 while (fname != NULL && --count > 0)
6074 {
6075 vim_free(fname);
6076 fname = find_file_in_path(NULL, 0, FNAME_MESS,
6077 FALSE, curbuf->b_ffname);
6078 }
6079 }
6080
6081 if (fname != NULL)
6082 {
6083 eap->arg = fname;
6084#endif
6085 do_exedit(eap, NULL);
6086#ifdef FEAT_SEARCHPATH
6087 vim_free(fname);
6088 }
6089#endif
6090}
6091
6092/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00006093 * ":open" simulation: for now just work like ":visual".
6094 */
6095 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006096ex_open(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00006097{
6098 regmatch_T regmatch;
6099 char_u *p;
6100
6101 curwin->w_cursor.lnum = eap->line2;
6102 beginline(BL_SOL | BL_FIX);
6103 if (*eap->arg == '/')
6104 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006105 // ":open /pattern/": put cursor in column found with pattern
Bram Moolenaardf177f62005-02-22 08:39:57 +00006106 ++eap->arg;
6107 p = skip_regexp(eap->arg, '/', p_magic, NULL);
6108 *p = NUL;
6109 regmatch.regprog = vim_regcomp(eap->arg, p_magic ? RE_MAGIC : 0);
6110 if (regmatch.regprog != NULL)
6111 {
6112 regmatch.rm_ic = p_ic;
6113 p = ml_get_curline();
6114 if (vim_regexec(&regmatch, p, (colnr_T)0))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006115 curwin->w_cursor.col = (colnr_T)(regmatch.startp[0] - p);
Bram Moolenaardf177f62005-02-22 08:39:57 +00006116 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006117 emsg(_(e_nomatch));
Bram Moolenaar473de612013-06-08 18:19:48 +02006118 vim_regfree(regmatch.regprog);
Bram Moolenaardf177f62005-02-22 08:39:57 +00006119 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006120 // Move to the NUL, ignore any other arguments.
Bram Moolenaardf177f62005-02-22 08:39:57 +00006121 eap->arg += STRLEN(eap->arg);
6122 }
6123 check_cursor();
6124
6125 eap->cmdidx = CMD_visual;
6126 do_exedit(eap, NULL);
6127}
6128
6129/*
6130 * ":edit", ":badd", ":visual".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006131 */
6132 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006133ex_edit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006134{
6135 do_exedit(eap, NULL);
6136}
6137
6138/*
Bram Moolenaar4b96df52020-01-26 22:00:26 +01006139 * ":edit <file>" command and alike.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006140 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006141 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006142do_exedit(
6143 exarg_T *eap,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006144 win_T *old_curwin) // curwin before doing a split or NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006145{
6146 int n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006147 int need_hide;
Bram Moolenaardf177f62005-02-22 08:39:57 +00006148 int exmode_was = exmode_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006149
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01006150 if ((eap->cmdidx != CMD_pedit && ERROR_IF_POPUP_WINDOW)
6151 || ERROR_IF_TERM_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02006152 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006153 /*
6154 * ":vi" command ends Ex mode.
6155 */
6156 if (exmode_active && (eap->cmdidx == CMD_visual
6157 || eap->cmdidx == CMD_view))
6158 {
6159 exmode_active = FALSE;
6160 if (*eap->arg == NUL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00006161 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006162 // Special case: ":global/pat/visual\NLvi-commands"
Bram Moolenaardf177f62005-02-22 08:39:57 +00006163 if (global_busy)
6164 {
6165 int rd = RedrawingDisabled;
6166 int nwr = no_wait_return;
6167 int ms = msg_scroll;
6168#ifdef FEAT_GUI
6169 int he = hold_gui_events;
6170#endif
6171
6172 if (eap->nextcmd != NULL)
6173 {
6174 stuffReadbuff(eap->nextcmd);
6175 eap->nextcmd = NULL;
6176 }
6177
6178 if (exmode_was != EXMODE_VIM)
6179 settmode(TMODE_RAW);
6180 RedrawingDisabled = 0;
6181 no_wait_return = 0;
6182 need_wait_return = FALSE;
6183 msg_scroll = 0;
6184#ifdef FEAT_GUI
6185 hold_gui_events = 0;
6186#endif
6187 must_redraw = CLEAR;
Bram Moolenaar9e2bcb52020-02-18 21:33:00 +01006188 pending_exmode_active = TRUE;
Bram Moolenaardf177f62005-02-22 08:39:57 +00006189
6190 main_loop(FALSE, TRUE);
6191
Bram Moolenaar9e2bcb52020-02-18 21:33:00 +01006192 pending_exmode_active = FALSE;
Bram Moolenaardf177f62005-02-22 08:39:57 +00006193 RedrawingDisabled = rd;
6194 no_wait_return = nwr;
6195 msg_scroll = ms;
6196#ifdef FEAT_GUI
6197 hold_gui_events = he;
6198#endif
6199 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006200 return;
Bram Moolenaardf177f62005-02-22 08:39:57 +00006201 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006202 }
6203
6204 if ((eap->cmdidx == CMD_new
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006205 || eap->cmdidx == CMD_tabnew
6206 || eap->cmdidx == CMD_tabedit
Bram Moolenaar4033c552017-09-16 20:54:51 +02006207 || eap->cmdidx == CMD_vnew) && *eap->arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006208 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006209 // ":new" or ":tabnew" without argument: edit an new empty buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00006210 setpcmark();
6211 (void)do_ecmd(0, NULL, NULL, eap, ECMD_ONE,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00006212 ECMD_HIDE + (eap->forceit ? ECMD_FORCEIT : 0),
6213 old_curwin == NULL ? curwin : NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006214 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02006215 else if ((eap->cmdidx != CMD_split && eap->cmdidx != CMD_vsplit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006216 || *eap->arg != NUL
6217#ifdef FEAT_BROWSE
6218 || cmdmod.browse
6219#endif
6220 )
6221 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006222 // Can't edit another file when "curbuf_lock" is set. Only ":edit"
6223 // can bring us here, others are stopped earlier.
Bram Moolenaar5555acc2006-04-07 21:33:12 +00006224 if (*eap->arg != NUL && curbuf_locked())
6225 return;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006226
Bram Moolenaar071d4272004-06-13 20:20:40 +00006227 n = readonlymode;
6228 if (eap->cmdidx == CMD_view || eap->cmdidx == CMD_sview)
6229 readonlymode = TRUE;
6230 else if (eap->cmdidx == CMD_enew)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006231 readonlymode = FALSE; // 'readonly' doesn't make sense in an
6232 // empty buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00006233 setpcmark();
6234 if (do_ecmd(0, (eap->cmdidx == CMD_enew ? NULL : eap->arg),
6235 NULL, eap,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006236 // ":edit" goes to first line if Vi compatible
Bram Moolenaar071d4272004-06-13 20:20:40 +00006237 (*eap->arg == NUL && eap->do_ecmd_lnum == 0
6238 && vim_strchr(p_cpo, CPO_GOTO1) != NULL)
6239 ? ECMD_ONE : eap->do_ecmd_lnum,
Bram Moolenaareb44a682017-08-03 22:44:55 +02006240 (buf_hide(curbuf) ? ECMD_HIDE : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006241 + (eap->forceit ? ECMD_FORCEIT : 0)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006242 // after a split we can use an existing buffer
Bram Moolenaar7b449342014-03-25 13:03:48 +01006243 + (old_curwin != NULL ? ECMD_OLDBUF : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006244 + (eap->cmdidx == CMD_badd ? ECMD_ADDBUF : 0 )
Bram Moolenaar701f7af2008-11-15 13:12:07 +00006245 , old_curwin == NULL ? curwin : NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006246 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006247 // Editing the file failed. If the window was split, close it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006248 if (old_curwin != NULL)
6249 {
6250 need_hide = (curbufIsChanged() && curbuf->b_nwindows <= 1);
Bram Moolenaareb44a682017-08-03 22:44:55 +02006251 if (!need_hide || buf_hide(curbuf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006252 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006253#if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00006254 cleanup_T cs;
6255
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006256 // Reset the error/interrupt/exception state here so that
6257 // aborting() returns FALSE when closing a window.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00006258 enter_cleanup(&cs);
Bram Moolenaar4033c552017-09-16 20:54:51 +02006259#endif
6260#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00006261 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006262#endif
Bram Moolenaareb44a682017-08-03 22:44:55 +02006263 win_close(curwin, !need_hide && !buf_hide(curbuf));
Bram Moolenaarc0197e22004-09-13 20:26:32 +00006264
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006265#if defined(FEAT_EVAL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006266 // Restore the error/interrupt/exception state if not
6267 // discarded by a new aborting error, interrupt, or
6268 // uncaught exception.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00006269 leave_cleanup(&cs);
Bram Moolenaar4033c552017-09-16 20:54:51 +02006270#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006271 }
6272 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006273 }
6274 else if (readonlymode && curbuf->b_nwindows == 1)
6275 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006276 // When editing an already visited buffer, 'readonly' won't be set
6277 // but the previous value is kept. With ":view" and ":sview" we
6278 // want the file to be readonly, except when another window is
6279 // editing the same buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006280 curbuf->b_p_ro = TRUE;
6281 }
6282 readonlymode = n;
6283 }
6284 else
6285 {
6286 if (eap->do_ecmd_cmd != NULL)
6287 do_cmdline_cmd(eap->do_ecmd_cmd);
6288#ifdef FEAT_TITLE
6289 n = curwin->w_arg_idx_invalid;
6290#endif
6291 check_arg_idx(curwin);
6292#ifdef FEAT_TITLE
6293 if (n != curwin->w_arg_idx_invalid)
6294 maketitle();
6295#endif
6296 }
6297
Bram Moolenaar071d4272004-06-13 20:20:40 +00006298 /*
6299 * if ":split file" worked, set alternate file name in old window to new
6300 * file
6301 */
6302 if (old_curwin != NULL
6303 && *eap->arg != NUL
6304 && curwin != old_curwin
6305 && win_valid(old_curwin)
Bram Moolenaard4755bb2004-09-02 19:12:26 +00006306 && old_curwin->w_buffer != curbuf
6307 && !cmdmod.keepalt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006308 old_curwin->w_alt_fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006309
6310 ex_no_reprint = TRUE;
6311}
6312
6313#ifndef FEAT_GUI
6314/*
6315 * ":gui" and ":gvim" when there is no GUI.
6316 */
6317 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006318ex_nogui(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006319{
6320 eap->errmsg = e_nogvim;
6321}
6322#endif
6323
Bram Moolenaar4f974752019-02-17 17:44:42 +01006324#if defined(FEAT_GUI_MSWIN) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006325 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006326ex_tearoff(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006327{
6328 gui_make_tearoff(eap->arg);
6329}
6330#endif
6331
Bram Moolenaar29a2c082018-03-05 21:06:23 +01006332#if (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK) \
6333 || defined(FEAT_TERM_POPUP_MENU)) && defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006334 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006335ex_popup(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006336{
Bram Moolenaar29a2c082018-03-05 21:06:23 +01006337# if defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK)
6338 if (gui.in_use)
6339 gui_make_popup(eap->arg, eap->forceit);
6340# ifdef FEAT_TERM_POPUP_MENU
6341 else
6342# endif
6343# endif
6344# ifdef FEAT_TERM_POPUP_MENU
6345 pum_make_popup(eap->arg, eap->forceit);
6346# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006347}
6348#endif
6349
Bram Moolenaar071d4272004-06-13 20:20:40 +00006350 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006351ex_swapname(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006352{
6353 if (curbuf->b_ml.ml_mfp == NULL || curbuf->b_ml.ml_mfp->mf_fname == NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01006354 msg(_("No swap file"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006355 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01006356 msg((char *)curbuf->b_ml.ml_mfp->mf_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006357}
6358
6359/*
6360 * ":syncbind" forces all 'scrollbind' windows to have the same relative
6361 * offset.
6362 * (1998-11-02 16:21:01 R. Edward Ralston <eralston@computer.org>)
6363 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006364 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006365ex_syncbind(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006366{
Bram Moolenaar071d4272004-06-13 20:20:40 +00006367 win_T *wp;
Bram Moolenaardedd1b02013-12-14 13:06:17 +01006368 win_T *save_curwin = curwin;
6369 buf_T *save_curbuf = curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006370 long topline;
6371 long y;
6372 linenr_T old_linenr = curwin->w_cursor.lnum;
6373
6374 setpcmark();
6375
6376 /*
6377 * determine max topline
6378 */
6379 if (curwin->w_p_scb)
6380 {
6381 topline = curwin->w_topline;
Bram Moolenaar29323592016-07-24 22:04:11 +02006382 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006383 {
6384 if (wp->w_p_scb && wp->w_buffer)
6385 {
Bram Moolenaar375e3392019-01-31 18:26:10 +01006386 y = wp->w_buffer->b_ml.ml_line_count - get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006387 if (topline > y)
6388 topline = y;
6389 }
6390 }
6391 if (topline < 1)
6392 topline = 1;
6393 }
6394 else
6395 {
6396 topline = 1;
6397 }
6398
6399
6400 /*
Bram Moolenaardedd1b02013-12-14 13:06:17 +01006401 * Set all scrollbind windows to the same topline.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006402 */
Bram Moolenaar29323592016-07-24 22:04:11 +02006403 FOR_ALL_WINDOWS(curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006404 {
6405 if (curwin->w_p_scb)
6406 {
Bram Moolenaardedd1b02013-12-14 13:06:17 +01006407 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006408 y = topline - curwin->w_topline;
6409 if (y > 0)
6410 scrollup(y, TRUE);
6411 else
6412 scrolldown(-y, TRUE);
6413 curwin->w_scbind_pos = topline;
6414 redraw_later(VALID);
6415 cursor_correct();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006416 curwin->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006417 }
6418 }
Bram Moolenaardedd1b02013-12-14 13:06:17 +01006419 curwin = save_curwin;
6420 curbuf = save_curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006421 if (curwin->w_p_scb)
6422 {
6423 did_syncbind = TRUE;
6424 checkpcmark();
6425 if (old_linenr != curwin->w_cursor.lnum)
6426 {
6427 char_u ctrl_o[2];
6428
6429 ctrl_o[0] = Ctrl_O;
6430 ctrl_o[1] = 0;
6431 ins_typebuf(ctrl_o, REMAP_NONE, 0, TRUE, FALSE);
6432 }
6433 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006434}
6435
6436
6437 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006438ex_read(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006439{
Bram Moolenaardf177f62005-02-22 08:39:57 +00006440 int i;
6441 int empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
6442 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006443
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006444 if (eap->usefilter) // :r!cmd
Bram Moolenaar071d4272004-06-13 20:20:40 +00006445 do_bang(1, eap, FALSE, FALSE, TRUE);
6446 else
6447 {
6448 if (u_save(eap->line2, (linenr_T)(eap->line2 + 1)) == FAIL)
6449 return;
6450
6451#ifdef FEAT_BROWSE
6452 if (cmdmod.browse)
6453 {
6454 char_u *browseFile;
6455
Bram Moolenaar7171abe2004-10-11 10:06:20 +00006456 browseFile = do_browse(0, (char_u *)_("Append File"), eap->arg,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006457 NULL, NULL, NULL, curbuf);
6458 if (browseFile != NULL)
6459 {
6460 i = readfile(browseFile, NULL,
6461 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
6462 vim_free(browseFile);
6463 }
6464 else
6465 i = OK;
6466 }
6467 else
6468#endif
6469 if (*eap->arg == NUL)
6470 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006471 if (check_fname() == FAIL) // check for no file name
Bram Moolenaar071d4272004-06-13 20:20:40 +00006472 return;
6473 i = readfile(curbuf->b_ffname, curbuf->b_fname,
6474 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
6475 }
6476 else
6477 {
6478 if (vim_strchr(p_cpo, CPO_ALTREAD) != NULL)
6479 (void)setaltfname(eap->arg, eap->arg, (linenr_T)1);
6480 i = readfile(eap->arg, NULL,
6481 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
6482
6483 }
Bram Moolenaare13b9af2017-01-13 22:01:02 +01006484 if (i != OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006485 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006486#if defined(FEAT_EVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006487 if (!aborting())
6488#endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006489 semsg(_(e_notopen), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006490 }
6491 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00006492 {
6493 if (empty && exmode_active)
6494 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006495 // Delete the empty line that remains. Historically ex does
6496 // this but vi doesn't.
Bram Moolenaardf177f62005-02-22 08:39:57 +00006497 if (eap->line2 == 0)
6498 lnum = curbuf->b_ml.ml_line_count;
6499 else
6500 lnum = 1;
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00006501 if (*ml_get(lnum) == NUL && u_savedel(lnum, 1L) == OK)
Bram Moolenaardf177f62005-02-22 08:39:57 +00006502 {
6503 ml_delete(lnum, FALSE);
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00006504 if (curwin->w_cursor.lnum > 1
6505 && curwin->w_cursor.lnum >= lnum)
Bram Moolenaardf177f62005-02-22 08:39:57 +00006506 --curwin->w_cursor.lnum;
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00006507 deleted_lines_mark(lnum, 1L);
Bram Moolenaardf177f62005-02-22 08:39:57 +00006508 }
6509 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006510 redraw_curbuf_later(VALID);
Bram Moolenaardf177f62005-02-22 08:39:57 +00006511 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006512 }
6513}
6514
Bram Moolenaarea408852005-06-25 22:49:46 +00006515static char_u *prev_dir = NULL;
6516
6517#if defined(EXITFREE) || defined(PROTO)
6518 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006519free_cd_dir(void)
Bram Moolenaarea408852005-06-25 22:49:46 +00006520{
Bram Moolenaard23a8232018-02-10 18:45:26 +01006521 VIM_CLEAR(prev_dir);
6522 VIM_CLEAR(globaldir);
Bram Moolenaarea408852005-06-25 22:49:46 +00006523}
6524#endif
6525
Bram Moolenaarf4258302013-06-02 18:20:17 +02006526/*
6527 * Deal with the side effects of changing the current directory.
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006528 * When 'scope' is CDSCOPE_TABPAGE then this was after an ":tcd" command.
6529 * When 'scope' is CDSCOPE_WINDOW then this was after an ":lcd" command.
Bram Moolenaarf4258302013-06-02 18:20:17 +02006530 */
6531 void
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006532post_chdir(cdscope_T scope)
Bram Moolenaarf4258302013-06-02 18:20:17 +02006533{
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006534 if (scope != CDSCOPE_WINDOW)
Bram Moolenaar00aa0692019-04-27 20:37:57 +02006535 // Clear tab local directory for both :cd and :tcd
6536 VIM_CLEAR(curtab->tp_localdir);
Bram Moolenaard23a8232018-02-10 18:45:26 +01006537 VIM_CLEAR(curwin->w_localdir);
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006538 if (scope != CDSCOPE_GLOBAL)
Bram Moolenaarf4258302013-06-02 18:20:17 +02006539 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006540 // If still in global directory, need to remember current
6541 // directory as global directory.
Bram Moolenaarf4258302013-06-02 18:20:17 +02006542 if (globaldir == NULL && prev_dir != NULL)
6543 globaldir = vim_strsave(prev_dir);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006544 // Remember this local directory for the window.
Bram Moolenaarf4258302013-06-02 18:20:17 +02006545 if (mch_dirname(NameBuff, MAXPATHL) == OK)
Bram Moolenaar00aa0692019-04-27 20:37:57 +02006546 {
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006547 if (scope == CDSCOPE_TABPAGE)
Bram Moolenaar00aa0692019-04-27 20:37:57 +02006548 curtab->tp_localdir = vim_strsave(NameBuff);
6549 else
6550 curwin->w_localdir = vim_strsave(NameBuff);
6551 }
Bram Moolenaarf4258302013-06-02 18:20:17 +02006552 }
6553 else
6554 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006555 // We are now in the global directory, no need to remember its
6556 // name.
Bram Moolenaard23a8232018-02-10 18:45:26 +01006557 VIM_CLEAR(globaldir);
Bram Moolenaarf4258302013-06-02 18:20:17 +02006558 }
6559
6560 shorten_fnames(TRUE);
6561}
6562
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006563/*
6564 * Change directory function used by :cd/:tcd/:lcd Ex commands and the
6565 * chdir() function. If 'winlocaldir' is TRUE, then changes the window-local
6566 * directory. If 'tablocaldir' is TRUE, then changes the tab-local directory.
6567 * Otherwise changes the global directory.
6568 * Returns TRUE if the directory is successfully changed.
6569 */
6570 int
6571changedir_func(
6572 char_u *new_dir,
6573 int forceit,
6574 cdscope_T scope)
6575{
6576 char_u *tofree;
6577 int dir_differs;
6578 int retval = FALSE;
6579
Bram Moolenaar7cc96922020-01-31 22:41:38 +01006580 if (new_dir == NULL || allbuf_locked())
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006581 return FALSE;
6582
6583 if (vim_strchr(p_cpo, CPO_CHDIR) != NULL && curbufIsChanged() && !forceit)
6584 {
6585 emsg(_("E747: Cannot change directory, buffer is modified (add ! to override)"));
6586 return FALSE;
6587 }
6588
6589 // ":cd -": Change to previous directory
6590 if (STRCMP(new_dir, "-") == 0)
6591 {
6592 if (prev_dir == NULL)
6593 {
6594 emsg(_("E186: No previous directory"));
6595 return FALSE;
6596 }
6597 new_dir = prev_dir;
6598 }
6599
6600 // Save current directory for next ":cd -"
6601 tofree = prev_dir;
6602 if (mch_dirname(NameBuff, MAXPATHL) == OK)
6603 prev_dir = vim_strsave(NameBuff);
6604 else
6605 prev_dir = NULL;
6606
6607#if defined(UNIX) || defined(VMS)
6608 // for UNIX ":cd" means: go to home directory
6609 if (*new_dir == NUL)
6610 {
6611 // use NameBuff for home directory name
6612# ifdef VMS
6613 char_u *p;
6614
6615 p = mch_getenv((char_u *)"SYS$LOGIN");
6616 if (p == NULL || *p == NUL) // empty is the same as not set
6617 NameBuff[0] = NUL;
6618 else
6619 vim_strncpy(NameBuff, p, MAXPATHL - 1);
6620# else
6621 expand_env((char_u *)"$HOME", NameBuff, MAXPATHL);
6622# endif
6623 new_dir = NameBuff;
6624 }
6625#endif
6626 dir_differs = new_dir == NULL || prev_dir == NULL
6627 || pathcmp((char *)prev_dir, (char *)new_dir, -1) != 0;
6628 if (new_dir == NULL || (dir_differs && vim_chdir(new_dir)))
6629 emsg(_(e_failed));
6630 else
6631 {
6632 char_u *acmd_fname;
6633
6634 post_chdir(scope);
6635
6636 if (dir_differs)
6637 {
6638 if (scope == CDSCOPE_WINDOW)
6639 acmd_fname = (char_u *)"window";
6640 else if (scope == CDSCOPE_TABPAGE)
6641 acmd_fname = (char_u *)"tabpage";
6642 else
6643 acmd_fname = (char_u *)"global";
6644 apply_autocmds(EVENT_DIRCHANGED, acmd_fname, new_dir, FALSE,
6645 curbuf);
6646 }
6647 retval = TRUE;
6648 }
6649 vim_free(tofree);
6650
6651 return retval;
6652}
Bram Moolenaarea408852005-06-25 22:49:46 +00006653
Bram Moolenaar071d4272004-06-13 20:20:40 +00006654/*
Bram Moolenaar00aa0692019-04-27 20:37:57 +02006655 * ":cd", ":tcd", ":lcd", ":chdir" ":tchdir" and ":lchdir".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006656 */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00006657 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006658ex_cd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006659{
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01006660 char_u *new_dir;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006661
6662 new_dir = eap->arg;
6663#if !defined(UNIX) && !defined(VMS)
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006664 // for non-UNIX ":cd" means: print current directory
Bram Moolenaar071d4272004-06-13 20:20:40 +00006665 if (*new_dir == NUL)
6666 ex_pwd(NULL);
6667 else
6668#endif
6669 {
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006670 cdscope_T scope = CDSCOPE_GLOBAL;
6671
6672 if (eap->cmdidx == CMD_lcd || eap->cmdidx == CMD_lchdir)
6673 scope = CDSCOPE_WINDOW;
6674 else if (eap->cmdidx == CMD_tcd || eap->cmdidx == CMD_tchdir)
6675 scope = CDSCOPE_TABPAGE;
6676
6677 if (changedir_func(new_dir, eap->forceit, scope))
Bram Moolenaardf177f62005-02-22 08:39:57 +00006678 {
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006679 // Echo the new current directory if the command was typed.
Bram Moolenaarfcfbc672009-07-09 18:13:49 +00006680 if (KeyTyped || p_verbose >= 5)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006681 ex_pwd(eap);
6682 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006683 }
6684}
6685
6686/*
6687 * ":pwd".
6688 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006689 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006690ex_pwd(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006691{
6692 if (mch_dirname(NameBuff, MAXPATHL) == OK)
6693 {
6694#ifdef BACKSLASH_IN_FILENAME
6695 slash_adjust(NameBuff);
6696#endif
Bram Moolenaar32526b32019-01-19 17:43:09 +01006697 msg((char *)NameBuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006698 }
6699 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006700 emsg(_("E187: Unknown"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006701}
6702
6703/*
6704 * ":=".
6705 */
6706 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006707ex_equal(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006708{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006709 smsg("%ld", (long)eap->line2);
Bram Moolenaardf177f62005-02-22 08:39:57 +00006710 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006711}
6712
6713 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006714ex_sleep(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006715{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006716 int n;
6717 long len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006718
6719 if (cursor_valid())
6720 {
6721 n = W_WINROW(curwin) + curwin->w_wrow - msg_scrolled;
6722 if (n >= 0)
Bram Moolenaar53f81742017-09-22 14:35:51 +02006723 windgoto((int)n, curwin->w_wincol + curwin->w_wcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006724 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006725
6726 len = eap->line2;
6727 switch (*eap->arg)
6728 {
6729 case 'm': break;
6730 case NUL: len *= 1000L; break;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006731 default: semsg(_(e_invarg2), eap->arg); return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006732 }
6733 do_sleep(len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006734}
6735
6736/*
6737 * Sleep for "msec" milliseconds, but keep checking for a CTRL-C every second.
6738 */
6739 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006740do_sleep(long msec)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006741{
Bram Moolenaar52953192019-08-16 10:27:13 +02006742 long done = 0;
Bram Moolenaar975b5272016-03-15 23:10:59 +01006743 long wait_now;
Bram Moolenaar52953192019-08-16 10:27:13 +02006744# ifdef ELAPSED_FUNC
6745 elapsed_T start_tv;
6746
6747 // Remember at what time we started, so that we know how much longer we
6748 // should wait after waiting for a bit.
6749 ELAPSED_INIT(start_tv);
6750# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006751
6752 cursor_on();
Bram Moolenaarf45d7472018-09-30 18:22:26 +02006753 out_flush_cursor(FALSE, FALSE);
Bram Moolenaar52953192019-08-16 10:27:13 +02006754 while (!got_int && done < msec)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006755 {
Bram Moolenaar975b5272016-03-15 23:10:59 +01006756 wait_now = msec - done > 1000L ? 1000L : msec - done;
6757#ifdef FEAT_TIMERS
6758 {
6759 long due_time = check_due_timer();
6760
6761 if (due_time > 0 && due_time < wait_now)
6762 wait_now = due_time;
6763 }
6764#endif
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02006765#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar28e67e02019-08-15 23:05:49 +02006766 if (has_any_channel() && wait_now > 20L)
6767 wait_now = 20L;
6768#endif
6769#ifdef FEAT_SOUND
6770 if (has_any_sound_callback() && wait_now > 20L)
6771 wait_now = 20L;
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02006772#endif
Bram Moolenaar975b5272016-03-15 23:10:59 +01006773 ui_delay(wait_now, TRUE);
Bram Moolenaar52953192019-08-16 10:27:13 +02006774
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02006775#ifdef FEAT_JOB_CHANNEL
6776 if (has_any_channel())
6777 ui_breakcheck_force(TRUE);
6778 else
6779#endif
6780 ui_breakcheck();
Bram Moolenaar93c88e02015-09-15 14:12:05 +02006781#ifdef MESSAGE_QUEUE
Bram Moolenaar52953192019-08-16 10:27:13 +02006782 // Process the netbeans and clientserver messages that may have been
6783 // received in the call to ui_breakcheck() when the GUI is in use. This
6784 // may occur when running a test case.
Bram Moolenaar93c88e02015-09-15 14:12:05 +02006785 parse_queued_messages();
Bram Moolenaare3cc6d42011-10-20 21:58:34 +02006786#endif
Bram Moolenaar52953192019-08-16 10:27:13 +02006787
6788# ifdef ELAPSED_FUNC
6789 // actual time passed
6790 done = ELAPSED_FUNC(start_tv);
6791# else
6792 // guestimate time passed (will actually be more)
6793 done += wait_now;
6794# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006795 }
Bram Moolenaar1ebff3d2018-07-07 16:18:13 +02006796
6797 // If CTRL-C was typed to interrupt the sleep, drop the CTRL-C from the
6798 // input buffer, otherwise a following call to input() fails.
6799 if (got_int)
6800 (void)vpeekc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006801}
6802
Bram Moolenaar071d4272004-06-13 20:20:40 +00006803/*
6804 * ":winsize" command (obsolete).
6805 */
6806 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006807ex_winsize(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006808{
6809 int w, h;
6810 char_u *arg = eap->arg;
6811 char_u *p;
6812
6813 w = getdigits(&arg);
6814 arg = skipwhite(arg);
6815 p = arg;
6816 h = getdigits(&arg);
6817 if (*p != NUL && *arg == NUL)
6818 set_shellsize(w, h, TRUE);
6819 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006820 emsg(_("E465: :winsize requires two number arguments"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006821}
6822
Bram Moolenaar071d4272004-06-13 20:20:40 +00006823 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006824ex_wincmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006825{
6826 int xchar = NUL;
6827 char_u *p;
6828
6829 if (*eap->arg == 'g' || *eap->arg == Ctrl_G)
6830 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006831 // CTRL-W g and CTRL-W CTRL-G have an extra command character
Bram Moolenaar071d4272004-06-13 20:20:40 +00006832 if (eap->arg[1] == NUL)
6833 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006834 emsg(_(e_invarg));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006835 return;
6836 }
6837 xchar = eap->arg[1];
6838 p = eap->arg + 2;
6839 }
6840 else
6841 p = eap->arg + 1;
6842
6843 eap->nextcmd = check_nextcmd(p);
6844 p = skipwhite(p);
6845 if (*p != NUL && *p != '"' && eap->nextcmd == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006846 emsg(_(e_invarg));
Bram Moolenaar12bde492011-06-13 01:19:56 +02006847 else if (!eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006848 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006849 // Pass flags on for ":vertical wincmd ]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006850 postponed_split_flags = cmdmod.split;
Bram Moolenaard326ce82007-03-11 14:48:29 +00006851 postponed_split_tab = cmdmod.tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006852 do_window(*eap->arg, eap->addr_count > 0 ? eap->line2 : 0L, xchar);
6853 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00006854 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006855 }
6856}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006857
Bram Moolenaar843ee412004-06-30 16:16:41 +00006858#if defined(FEAT_GUI) || defined(UNIX) || defined(VMS) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006859/*
6860 * ":winpos".
6861 */
6862 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006863ex_winpos(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006864{
6865 int x, y;
6866 char_u *arg = eap->arg;
6867 char_u *p;
6868
6869 if (*arg == NUL)
6870 {
Bram Moolenaar843ee412004-06-30 16:16:41 +00006871# if defined(FEAT_GUI) || defined(MSWIN)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02006872# ifdef VIMDLL
6873 if (gui.in_use ? gui_mch_get_winpos(&x, &y) != FAIL :
6874 mch_get_winpos(&x, &y) != FAIL)
6875# elif defined(FEAT_GUI)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006876 if (gui.in_use && gui_mch_get_winpos(&x, &y) != FAIL)
Bram Moolenaar843ee412004-06-30 16:16:41 +00006877# else
6878 if (mch_get_winpos(&x, &y) != FAIL)
6879# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006880 {
6881 sprintf((char *)IObuff, _("Window position: X %d, Y %d"), x, y);
Bram Moolenaar32526b32019-01-19 17:43:09 +01006882 msg((char *)IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006883 }
6884 else
6885# endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006886 emsg(_("E188: Obtaining window position not implemented for this platform"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006887 }
6888 else
6889 {
6890 x = getdigits(&arg);
6891 arg = skipwhite(arg);
6892 p = arg;
6893 y = getdigits(&arg);
6894 if (*p == NUL || *arg != NUL)
6895 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006896 emsg(_("E466: :winpos requires two number arguments"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006897 return;
6898 }
6899# ifdef FEAT_GUI
6900 if (gui.in_use)
6901 gui_mch_set_winpos(x, y);
6902 else if (gui.starting)
6903 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006904 // Remember the coordinates for when the window is opened.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006905 gui_win_x = x;
6906 gui_win_y = y;
6907 }
Bram Moolenaarafde13b2019-04-28 19:46:49 +02006908# if defined(HAVE_TGETENT) || defined(VIMDLL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006909 else
6910# endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02006911# endif
6912# if defined(MSWIN) && (!defined(FEAT_GUI) || defined(VIMDLL))
Bram Moolenaar843ee412004-06-30 16:16:41 +00006913 mch_set_winpos(x, y);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006914# endif
6915# ifdef HAVE_TGETENT
6916 if (*T_CWP)
6917 term_set_winpos(x, y);
6918# endif
6919 }
6920}
6921#endif
6922
6923/*
6924 * Handle command that work like operators: ":delete", ":yank", ":>" and ":<".
6925 */
6926 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006927ex_operators(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006928{
6929 oparg_T oa;
6930
6931 clear_oparg(&oa);
6932 oa.regname = eap->regname;
6933 oa.start.lnum = eap->line1;
6934 oa.end.lnum = eap->line2;
6935 oa.line_count = eap->line2 - eap->line1 + 1;
6936 oa.motion_type = MLINE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006937 virtual_op = FALSE;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006938 if (eap->cmdidx != CMD_yank) // position cursor for undo
Bram Moolenaar071d4272004-06-13 20:20:40 +00006939 {
6940 setpcmark();
6941 curwin->w_cursor.lnum = eap->line1;
6942 beginline(BL_SOL | BL_FIX);
6943 }
6944
Bram Moolenaard07c6e12013-11-21 14:21:40 +01006945 if (VIsual_active)
6946 end_visual_mode();
Bram Moolenaard07c6e12013-11-21 14:21:40 +01006947
Bram Moolenaar071d4272004-06-13 20:20:40 +00006948 switch (eap->cmdidx)
6949 {
6950 case CMD_delete:
6951 oa.op_type = OP_DELETE;
6952 op_delete(&oa);
6953 break;
6954
6955 case CMD_yank:
6956 oa.op_type = OP_YANK;
6957 (void)op_yank(&oa, FALSE, TRUE);
6958 break;
6959
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006960 default: // CMD_rshift or CMD_lshift
Bram Moolenaar6e707362013-06-14 19:15:58 +02006961 if (
Bram Moolenaar071d4272004-06-13 20:20:40 +00006962#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6e707362013-06-14 19:15:58 +02006963 (eap->cmdidx == CMD_rshift) ^ curwin->w_p_rl
6964#else
6965 eap->cmdidx == CMD_rshift
Bram Moolenaar071d4272004-06-13 20:20:40 +00006966#endif
Bram Moolenaar6e707362013-06-14 19:15:58 +02006967 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006968 oa.op_type = OP_RSHIFT;
6969 else
6970 oa.op_type = OP_LSHIFT;
6971 op_shift(&oa, FALSE, eap->amount);
6972 break;
6973 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006974 virtual_op = MAYBE;
Bram Moolenaardf177f62005-02-22 08:39:57 +00006975 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006976}
6977
6978/*
6979 * ":put".
6980 */
6981 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006982ex_put(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006983{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006984 // ":0put" works like ":1put!".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006985 if (eap->line2 == 0)
6986 {
6987 eap->line2 = 1;
6988 eap->forceit = TRUE;
6989 }
6990 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaardf177f62005-02-22 08:39:57 +00006991 do_put(eap->regname, eap->forceit ? BACKWARD : FORWARD, 1L,
6992 PUT_LINE|PUT_CURSLINE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006993}
6994
6995/*
6996 * Handle ":copy" and ":move".
6997 */
6998 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006999ex_copymove(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007000{
7001 long n;
7002
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02007003 n = get_address(eap, &eap->arg, eap->addr_type, FALSE, FALSE, FALSE, 1);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007004 if (eap->arg == NULL) // error detected
Bram Moolenaar071d4272004-06-13 20:20:40 +00007005 {
7006 eap->nextcmd = NULL;
7007 return;
7008 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00007009 get_flags(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007010
7011 /*
7012 * move or copy lines from 'eap->line1'-'eap->line2' to below line 'n'
7013 */
7014 if (n == MAXLNUM || n < 0 || n > curbuf->b_ml.ml_line_count)
7015 {
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02007016 emsg(_(e_invrange));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007017 return;
7018 }
7019
7020 if (eap->cmdidx == CMD_move)
7021 {
7022 if (do_move(eap->line1, eap->line2, n) == FAIL)
7023 return;
7024 }
7025 else
7026 ex_copy(eap->line1, eap->line2, n);
7027 u_clearline();
7028 beginline(BL_SOL | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007029 ex_may_print(eap);
7030}
7031
7032/*
7033 * Print the current line if flags were given to the Ex command.
7034 */
Bram Moolenaarfd3fe982014-04-01 17:49:44 +02007035 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007036ex_may_print(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007037{
7038 if (eap->flags != 0)
7039 {
7040 print_line(curwin->w_cursor.lnum, (eap->flags & EXFLAG_NR),
7041 (eap->flags & EXFLAG_LIST));
7042 ex_no_reprint = TRUE;
7043 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007044}
7045
7046/*
7047 * ":smagic" and ":snomagic".
7048 */
7049 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007050ex_submagic(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007051{
7052 int magic_save = p_magic;
7053
7054 p_magic = (eap->cmdidx == CMD_smagic);
7055 do_sub(eap);
7056 p_magic = magic_save;
7057}
7058
7059/*
7060 * ":join".
7061 */
7062 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007063ex_join(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007064{
7065 curwin->w_cursor.lnum = eap->line1;
7066 if (eap->line1 == eap->line2)
7067 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007068 if (eap->addr_count >= 2) // :2,2join does nothing
Bram Moolenaar071d4272004-06-13 20:20:40 +00007069 return;
7070 if (eap->line2 == curbuf->b_ml.ml_line_count)
7071 {
7072 beep_flush();
7073 return;
7074 }
7075 ++eap->line2;
7076 }
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02007077 (void)do_join(eap->line2 - eap->line1 + 1, !eap->forceit, TRUE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007078 beginline(BL_WHITE | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007079 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007080}
7081
7082/*
7083 * ":[addr]@r" or ":[addr]*r": execute register
7084 */
7085 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007086ex_at(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007087{
7088 int c;
Bram Moolenaar60462872009-11-03 11:40:19 +00007089 int prev_len = typebuf.tb_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007090
7091 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaar4930a762016-09-11 14:39:53 +02007092 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007093
7094#ifdef USE_ON_FLY_SCROLL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007095 dont_scroll = TRUE; // disallow scrolling here
Bram Moolenaar071d4272004-06-13 20:20:40 +00007096#endif
7097
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007098 // get the register name. No name means to use the previous one
Bram Moolenaar071d4272004-06-13 20:20:40 +00007099 c = *eap->arg;
7100 if (c == NUL || (c == '*' && *eap->cmd == '*'))
7101 c = '@';
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007102 // Put the register in the typeahead buffer with the "silent" flag.
Bram Moolenaard333d1e2006-11-07 17:43:47 +00007103 if (do_execreg(c, TRUE, vim_strchr(p_cpo, CPO_EXECBUF) != NULL, TRUE)
7104 == FAIL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007105 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007106 beep_flush();
Bram Moolenaardf177f62005-02-22 08:39:57 +00007107 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007108 else
7109 {
7110 int save_efr = exec_from_reg;
7111
7112 exec_from_reg = TRUE;
7113
7114 /*
7115 * Execute from the typeahead buffer.
Bram Moolenaar60462872009-11-03 11:40:19 +00007116 * Continue until the stuff buffer is empty and all added characters
7117 * have been consumed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007118 */
Bram Moolenaar60462872009-11-03 11:40:19 +00007119 while (!stuff_empty() || typebuf.tb_len > prev_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007120 (void)do_cmdline(NULL, getexline, NULL, DOCMD_NOWAIT|DOCMD_VERBOSE);
7121
7122 exec_from_reg = save_efr;
7123 }
7124}
7125
7126/*
7127 * ":!".
7128 */
7129 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007130ex_bang(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007131{
7132 do_bang(eap->addr_count, eap, eap->forceit, TRUE, TRUE);
7133}
7134
7135/*
7136 * ":undo".
7137 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007138 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +01007139ex_undo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007140{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007141 if (eap->addr_count == 1) // :undo 123
Bram Moolenaar730cde92010-06-27 05:18:54 +02007142 undo_time(eap->line2, FALSE, FALSE, TRUE);
Bram Moolenaard3667a22006-03-16 21:35:52 +00007143 else
7144 u_undo(1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007145}
7146
Bram Moolenaar55debbe2010-05-23 23:34:36 +02007147#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar6df6f472010-07-18 18:04:50 +02007148 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007149ex_wundo(exarg_T *eap)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02007150{
7151 char_u hash[UNDO_HASH_SIZE];
7152
7153 u_compute_hash(hash);
7154 u_write_undo(eap->arg, eap->forceit, curbuf, hash);
7155}
7156
Bram Moolenaar6df6f472010-07-18 18:04:50 +02007157 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007158ex_rundo(exarg_T *eap)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02007159{
7160 char_u hash[UNDO_HASH_SIZE];
7161
7162 u_compute_hash(hash);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02007163 u_read_undo(eap->arg, hash, NULL);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02007164}
7165#endif
7166
Bram Moolenaar071d4272004-06-13 20:20:40 +00007167/*
7168 * ":redo".
7169 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007170 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007171ex_redo(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007172{
7173 u_redo(1);
7174}
7175
7176/*
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007177 * ":earlier" and ":later".
7178 */
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007179 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007180ex_later(exarg_T *eap)
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007181{
7182 long count = 0;
7183 int sec = FALSE;
Bram Moolenaar730cde92010-06-27 05:18:54 +02007184 int file = FALSE;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007185 char_u *p = eap->arg;
7186
7187 if (*p == NUL)
7188 count = 1;
7189 else if (isdigit(*p))
7190 {
7191 count = getdigits(&p);
7192 switch (*p)
7193 {
7194 case 's': ++p; sec = TRUE; break;
7195 case 'm': ++p; sec = TRUE; count *= 60; break;
7196 case 'h': ++p; sec = TRUE; count *= 60 * 60; break;
Bram Moolenaar730cde92010-06-27 05:18:54 +02007197 case 'd': ++p; sec = TRUE; count *= 24 * 60 * 60; break;
7198 case 'f': ++p; file = TRUE; break;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007199 }
7200 }
7201
7202 if (*p != NUL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007203 semsg(_(e_invarg2), eap->arg);
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007204 else
Bram Moolenaar730cde92010-06-27 05:18:54 +02007205 undo_time(eap->cmdidx == CMD_earlier ? -count : count,
7206 sec, file, FALSE);
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007207}
7208
7209/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007210 * ":redir": start/stop redirection.
7211 */
7212 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007213ex_redir(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007214{
7215 char *mode;
7216 char_u *fname;
Bram Moolenaarca472992005-01-21 11:46:23 +00007217 char_u *arg = eap->arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007218
Bram Moolenaarba768492016-07-08 15:32:54 +02007219#ifdef FEAT_EVAL
Bram Moolenaar79815f12016-07-09 17:07:29 +02007220 if (redir_execute)
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02007221 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007222 emsg(_("E930: Cannot use :redir inside execute()"));
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02007223 return;
7224 }
Bram Moolenaarba768492016-07-08 15:32:54 +02007225#endif
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02007226
Bram Moolenaar071d4272004-06-13 20:20:40 +00007227 if (STRICMP(eap->arg, "END") == 0)
7228 close_redir();
7229 else
7230 {
Bram Moolenaarca472992005-01-21 11:46:23 +00007231 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00007232 {
Bram Moolenaarca472992005-01-21 11:46:23 +00007233 ++arg;
7234 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00007235 {
Bram Moolenaarca472992005-01-21 11:46:23 +00007236 ++arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007237 mode = "a";
7238 }
7239 else
7240 mode = "w";
Bram Moolenaarca472992005-01-21 11:46:23 +00007241 arg = skipwhite(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007242
7243 close_redir();
7244
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007245 // Expand environment variables and "~/".
Bram Moolenaarca472992005-01-21 11:46:23 +00007246 fname = expand_env_save(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007247 if (fname == NULL)
7248 return;
7249#ifdef FEAT_BROWSE
7250 if (cmdmod.browse)
7251 {
7252 char_u *browseFile;
7253
Bram Moolenaar7171abe2004-10-11 10:06:20 +00007254 browseFile = do_browse(BROWSE_SAVE,
7255 (char_u *)_("Save Redirection"),
Bram Moolenaarc36651b2018-04-29 12:22:56 +02007256 fname, NULL, NULL,
7257 (char_u *)_(BROWSE_FILTER_ALL_FILES), curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007258 if (browseFile == NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007259 return; // operation cancelled
Bram Moolenaar071d4272004-06-13 20:20:40 +00007260 vim_free(fname);
7261 fname = browseFile;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007262 eap->forceit = TRUE; // since dialog already asked
Bram Moolenaar071d4272004-06-13 20:20:40 +00007263 }
7264#endif
7265
7266 redir_fd = open_exfile(fname, eap->forceit, mode);
7267 vim_free(fname);
7268 }
7269#ifdef FEAT_EVAL
Bram Moolenaarca472992005-01-21 11:46:23 +00007270 else if (*arg == '@')
Bram Moolenaar071d4272004-06-13 20:20:40 +00007271 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007272 // redirect to a register a-z (resp. A-Z for appending)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007273 close_redir();
Bram Moolenaarca472992005-01-21 11:46:23 +00007274 ++arg;
7275 if (ASCII_ISALPHA(*arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007276# ifdef FEAT_CLIPBOARD
Bram Moolenaarca472992005-01-21 11:46:23 +00007277 || *arg == '*'
Bram Moolenaar9a51c6e2006-11-14 19:25:02 +00007278 || *arg == '+'
Bram Moolenaar071d4272004-06-13 20:20:40 +00007279# endif
Bram Moolenaarca472992005-01-21 11:46:23 +00007280 || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00007281 {
Bram Moolenaarca472992005-01-21 11:46:23 +00007282 redir_reg = *arg++;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007283 if (*arg == '>' && arg[1] == '>') // append
Bram Moolenaard9d30582005-05-18 22:10:28 +00007284 arg += 2;
Bram Moolenaarc188b882007-10-19 14:20:54 +00007285 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007286 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007287 // Can use both "@a" and "@a>".
Bram Moolenaar2c29bee2005-06-01 21:46:07 +00007288 if (*arg == '>')
7289 arg++;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007290 // Make register empty when not using @A-@Z and the
7291 // command is valid.
Bram Moolenaarc188b882007-10-19 14:20:54 +00007292 if (*arg == NUL && !isupper(redir_reg))
7293 write_reg_contents(redir_reg, (char_u *)"", -1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007294 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00007295 }
7296 if (*arg != NUL)
7297 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00007298 redir_reg = 0;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007299 semsg(_(e_invarg2), eap->arg);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007300 }
7301 }
7302 else if (*arg == '=' && arg[1] == '>')
7303 {
7304 int append;
7305
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007306 // redirect to a variable
Bram Moolenaardf177f62005-02-22 08:39:57 +00007307 close_redir();
7308 arg += 2;
7309
7310 if (*arg == '>')
7311 {
7312 ++arg;
7313 append = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007314 }
7315 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00007316 append = FALSE;
7317
7318 if (var_redir_start(skipwhite(arg), append) == OK)
7319 redir_vname = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007320 }
7321#endif
7322
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007323 // TODO: redirect to a buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00007324
Bram Moolenaar071d4272004-06-13 20:20:40 +00007325 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007326 semsg(_(e_invarg2), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007327 }
Bram Moolenaar29b2d262006-09-10 19:07:28 +00007328
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007329 // Make sure redirection is not off. Can happen for cmdline completion
7330 // that indirectly invokes a command to catch its output.
Bram Moolenaar29b2d262006-09-10 19:07:28 +00007331 if (redir_fd != NULL
7332#ifdef FEAT_EVAL
7333 || redir_reg || redir_vname
7334#endif
7335 )
7336 redir_off = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007337}
7338
7339/*
7340 * ":redraw": force redraw
7341 */
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01007342 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007343ex_redraw(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007344{
7345 int r = RedrawingDisabled;
7346 int p = p_lz;
7347
7348 RedrawingDisabled = 0;
7349 p_lz = FALSE;
Bram Moolenaarabc39ab2017-03-01 18:04:05 +01007350 validate_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007351 update_topline();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007352 update_screen(eap->forceit ? CLEAR : VIsual_active ? INVERTED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007353#ifdef FEAT_TITLE
7354 if (need_maketitle)
7355 maketitle();
7356#endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007357#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
7358# ifdef VIMDLL
7359 if (!gui.in_use)
7360# endif
7361 resize_console_buf();
Bram Moolenaar78d21da2019-02-17 15:00:52 +01007362#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007363 RedrawingDisabled = r;
7364 p_lz = p;
7365
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007366 // Reset msg_didout, so that a message that's there is overwritten.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007367 msg_didout = FALSE;
7368 msg_col = 0;
7369
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007370 // No need to wait after an intentional redraw.
Bram Moolenaar56667a52013-07-17 11:54:28 +02007371 need_wait_return = FALSE;
7372
Bram Moolenaar071d4272004-06-13 20:20:40 +00007373 out_flush();
7374}
7375
7376/*
7377 * ":redrawstatus": force redraw of status line(s)
7378 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007379 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007380ex_redrawstatus(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007381{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007382 int r = RedrawingDisabled;
7383 int p = p_lz;
7384
7385 RedrawingDisabled = 0;
7386 p_lz = FALSE;
7387 if (eap->forceit)
7388 status_redraw_all();
7389 else
7390 status_redraw_curbuf();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007391 update_screen(VIsual_active ? INVERTED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007392 RedrawingDisabled = r;
7393 p_lz = p;
7394 out_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007395}
7396
Bram Moolenaare12bab32019-01-08 22:02:56 +01007397/*
7398 * ":redrawtabline": force redraw of the tabline
7399 */
7400 static void
7401ex_redrawtabline(exarg_T *eap UNUSED)
7402{
7403 int r = RedrawingDisabled;
7404 int p = p_lz;
7405
7406 RedrawingDisabled = 0;
7407 p_lz = FALSE;
7408
7409 draw_tabline();
7410
7411 RedrawingDisabled = r;
7412 p_lz = p;
7413 out_flush();
7414}
7415
Bram Moolenaar071d4272004-06-13 20:20:40 +00007416 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007417close_redir(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007418{
7419 if (redir_fd != NULL)
7420 {
7421 fclose(redir_fd);
7422 redir_fd = NULL;
7423 }
7424#ifdef FEAT_EVAL
7425 redir_reg = 0;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007426 if (redir_vname)
7427 {
7428 var_redir_stop();
7429 redir_vname = 0;
7430 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007431#endif
7432}
7433
Bram Moolenaarf96ae0b2019-07-28 15:21:55 +02007434#if (defined(FEAT_SESSION) || defined(FEAT_EVAL)) || defined(PROTO)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007435 int
Bram Moolenaarbd67aac2019-09-21 23:09:04 +02007436vim_mkdir_emsg(char_u *name, int prot UNUSED)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007437{
7438 if (vim_mkdir(name, prot) != 0)
7439 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007440 semsg(_("E739: Cannot create directory: %s"), name);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007441 return FAIL;
7442 }
7443 return OK;
7444}
7445#endif
7446
Bram Moolenaar071d4272004-06-13 20:20:40 +00007447/*
7448 * Open a file for writing for an Ex command, with some checks.
7449 * Return file descriptor, or NULL on failure.
7450 */
7451 FILE *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007452open_exfile(
7453 char_u *fname,
7454 int forceit,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007455 char *mode) // "w" for create new file or "a" for append
Bram Moolenaar071d4272004-06-13 20:20:40 +00007456{
7457 FILE *fd;
7458
7459#ifdef UNIX
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007460 // with Unix it is possible to open a directory
Bram Moolenaar071d4272004-06-13 20:20:40 +00007461 if (mch_isdir(fname))
7462 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007463 semsg(_(e_isadir2), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007464 return NULL;
7465 }
7466#endif
7467 if (!forceit && *mode != 'a' && vim_fexists(fname))
7468 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007469 semsg(_("E189: \"%s\" exists (add ! to override)"), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007470 return NULL;
7471 }
7472
7473 if ((fd = mch_fopen((char *)fname, mode)) == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007474 semsg(_("E190: Cannot open \"%s\" for writing"), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007475
7476 return fd;
7477}
7478
7479/*
7480 * ":mark" and ":k".
7481 */
7482 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007483ex_mark(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007484{
7485 pos_T pos;
7486
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007487 if (*eap->arg == NUL) // No argument?
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007488 emsg(_(e_argreq));
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007489 else if (eap->arg[1] != NUL) // more than one character?
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007490 emsg(_(e_trailing));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007491 else
7492 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007493 pos = curwin->w_cursor; // save curwin->w_cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00007494 curwin->w_cursor.lnum = eap->line2;
7495 beginline(BL_WHITE | BL_FIX);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007496 if (setmark(*eap->arg) == FAIL) // set mark
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007497 emsg(_("E191: Argument must be a letter or forward/backward quote"));
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007498 curwin->w_cursor = pos; // restore curwin->w_cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00007499 }
7500}
7501
7502/*
7503 * Update w_topline, w_leftcol and the cursor position.
7504 */
7505 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007506update_topline_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007507{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007508 check_cursor(); // put cursor on valid line
Bram Moolenaar071d4272004-06-13 20:20:40 +00007509 update_topline();
7510 if (!curwin->w_p_wrap)
7511 validate_cursor();
7512 update_curswant();
7513}
7514
Bram Moolenaar071d4272004-06-13 20:20:40 +00007515/*
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007516 * Save the current State and go to Normal mode.
7517 * Return TRUE if the typeahead could be saved.
7518 */
7519 int
7520save_current_state(save_state_T *sst)
7521{
7522 sst->save_msg_scroll = msg_scroll;
7523 sst->save_restart_edit = restart_edit;
7524 sst->save_msg_didout = msg_didout;
7525 sst->save_State = State;
7526 sst->save_insertmode = p_im;
7527 sst->save_finish_op = finish_op;
7528 sst->save_opcount = opcount;
Bram Moolenaarcce713d2019-03-04 11:40:12 +01007529 sst->save_reg_executing = reg_executing;
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007530
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007531 msg_scroll = FALSE; // no msg scrolling in Normal mode
7532 restart_edit = 0; // don't go to Insert mode
7533 p_im = FALSE; // don't use 'insertmode'
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007534
7535 /*
7536 * Save the current typeahead. This is required to allow using ":normal"
7537 * from an event handler and makes sure we don't hang when the argument
7538 * ends with half a command.
7539 */
7540 save_typeahead(&sst->tabuf);
7541 return sst->tabuf.typebuf_valid;
7542}
7543
7544 void
7545restore_current_state(save_state_T *sst)
7546{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007547 // Restore the previous typeahead.
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007548 restore_typeahead(&sst->tabuf);
7549
7550 msg_scroll = sst->save_msg_scroll;
7551 restart_edit = sst->save_restart_edit;
7552 p_im = sst->save_insertmode;
7553 finish_op = sst->save_finish_op;
7554 opcount = sst->save_opcount;
Bram Moolenaarcce713d2019-03-04 11:40:12 +01007555 reg_executing = sst->save_reg_executing;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007556 msg_didout |= sst->save_msg_didout; // don't reset msg_didout now
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007557
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007558 // Restore the state (needed when called from a function executed for
7559 // 'indentexpr'). Update the mouse and cursor, they may have changed.
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007560 State = sst->save_State;
7561#ifdef CURSOR_SHAPE
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007562 ui_cursor_shape(); // may show different cursor shape
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007563#endif
7564}
7565
7566/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007567 * ":normal[!] {commands}": Execute normal mode commands.
7568 */
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01007569 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007570ex_normal(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007571{
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007572 save_state_T save_state;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007573 char_u *arg = NULL;
7574 int l;
7575 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007576
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00007577 if (ex_normal_lock > 0)
7578 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007579 emsg(_(e_secure));
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00007580 return;
7581 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007582 if (ex_normal_busy >= p_mmd)
7583 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007584 emsg(_("E192: Recursive use of :normal too deep"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007585 return;
7586 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007587
Bram Moolenaar071d4272004-06-13 20:20:40 +00007588 /*
7589 * vgetc() expects a CSI and K_SPECIAL to have been escaped. Don't do
7590 * this for the K_SPECIAL leading byte, otherwise special keys will not
7591 * work.
7592 */
7593 if (has_mbyte)
7594 {
7595 int len = 0;
7596
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007597 // Count the number of characters to be escaped.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007598 for (p = eap->arg; *p != NUL; ++p)
7599 {
Bram Moolenaar13505972019-01-24 15:04:48 +01007600#ifdef FEAT_GUI
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007601 if (*p == CSI) // leadbyte CSI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007602 len += 2;
Bram Moolenaar13505972019-01-24 15:04:48 +01007603#endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007604 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007605 if (*++p == K_SPECIAL // trailbyte K_SPECIAL or CSI
Bram Moolenaar13505972019-01-24 15:04:48 +01007606#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007607 || *p == CSI
Bram Moolenaar13505972019-01-24 15:04:48 +01007608#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007609 )
7610 len += 2;
7611 }
7612 if (len > 0)
7613 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02007614 arg = alloc(STRLEN(eap->arg) + len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007615 if (arg != NULL)
7616 {
7617 len = 0;
7618 for (p = eap->arg; *p != NUL; ++p)
7619 {
7620 arg[len++] = *p;
Bram Moolenaar13505972019-01-24 15:04:48 +01007621#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007622 if (*p == CSI)
7623 {
7624 arg[len++] = KS_EXTRA;
7625 arg[len++] = (int)KE_CSI;
7626 }
Bram Moolenaar13505972019-01-24 15:04:48 +01007627#endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007628 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007629 {
7630 arg[len++] = *++p;
7631 if (*p == K_SPECIAL)
7632 {
7633 arg[len++] = KS_SPECIAL;
7634 arg[len++] = KE_FILLER;
7635 }
Bram Moolenaar13505972019-01-24 15:04:48 +01007636#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007637 else if (*p == CSI)
7638 {
7639 arg[len++] = KS_EXTRA;
7640 arg[len++] = (int)KE_CSI;
7641 }
Bram Moolenaar13505972019-01-24 15:04:48 +01007642#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007643 }
7644 arg[len] = NUL;
7645 }
7646 }
7647 }
7648 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007649
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007650 ++ex_normal_busy;
7651 if (save_current_state(&save_state))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007652 {
7653 /*
7654 * Repeat the :normal command for each line in the range. When no
7655 * range given, execute it just once, without positioning the cursor
7656 * first.
7657 */
7658 do
7659 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007660 if (eap->addr_count != 0)
7661 {
7662 curwin->w_cursor.lnum = eap->line1++;
7663 curwin->w_cursor.col = 0;
Bram Moolenaard5d37532017-03-27 23:02:07 +02007664 check_cursor_moved(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007665 }
7666
Bram Moolenaar13505972019-01-24 15:04:48 +01007667 exec_normal_cmd(arg != NULL
7668 ? arg
7669 : eap->arg, eap->forceit ? REMAP_NONE : REMAP_YES, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007670 }
7671 while (eap->addr_count > 0 && eap->line1 <= eap->line2 && !got_int);
7672 }
7673
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007674 // Might not return to the main loop when in an event handler.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007675 update_topline_cursor();
7676
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007677 restore_current_state(&save_state);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007678 --ex_normal_busy;
Bram Moolenaareda73602014-11-05 09:53:23 +01007679 setmouse();
Bram Moolenaareda73602014-11-05 09:53:23 +01007680#ifdef CURSOR_SHAPE
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007681 ui_cursor_shape(); // may show different cursor shape
Bram Moolenaareda73602014-11-05 09:53:23 +01007682#endif
7683
Bram Moolenaar071d4272004-06-13 20:20:40 +00007684 vim_free(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007685}
7686
7687/*
Bram Moolenaar64969662005-12-14 21:59:55 +00007688 * ":startinsert", ":startreplace" and ":startgreplace"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007689 */
7690 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007691ex_startinsert(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007692{
Bram Moolenaarfd371682005-01-14 21:42:54 +00007693 if (eap->forceit)
7694 {
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02007695 // cursor line can be zero on startup
Bram Moolenaar09ca9322017-09-26 17:40:45 +02007696 if (!curwin->w_cursor.lnum)
7697 curwin->w_cursor.lnum = 1;
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02007698 set_cursor_for_append_to_line();
Bram Moolenaarfd371682005-01-14 21:42:54 +00007699 }
7700
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02007701 // Ignore the command when already in Insert mode. Inserting an
7702 // expression register that invokes a function can do this.
Bram Moolenaara40c5002005-01-09 21:16:21 +00007703 if (State & INSERT)
7704 return;
7705
Bram Moolenaar64969662005-12-14 21:59:55 +00007706 if (eap->cmdidx == CMD_startinsert)
7707 restart_edit = 'a';
7708 else if (eap->cmdidx == CMD_startreplace)
7709 restart_edit = 'R';
Bram Moolenaar071d4272004-06-13 20:20:40 +00007710 else
Bram Moolenaar64969662005-12-14 21:59:55 +00007711 restart_edit = 'V';
7712
7713 if (!eap->forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007714 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00007715 if (eap->cmdidx == CMD_startinsert)
7716 restart_edit = 'i';
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02007717 curwin->w_curswant = 0; // avoid MAXCOL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007718 }
7719}
7720
7721/*
7722 * ":stopinsert"
7723 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007724 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007725ex_stopinsert(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007726{
7727 restart_edit = 0;
7728 stop_insert_mode = TRUE;
Bram Moolenaarfd773e92016-04-02 19:39:16 +02007729 clearmode();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007730}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007731
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00007732/*
7733 * Execute normal mode command "cmd".
7734 * "remap" can be REMAP_NONE or REMAP_YES.
7735 */
7736 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007737exec_normal_cmd(char_u *cmd, int remap, int silent)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00007738{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007739 // Stuff the argument into the typeahead buffer.
Bram Moolenaar25281632016-01-21 23:32:32 +01007740 ins_typebuf(cmd, remap, 0, TRUE, silent);
Bram Moolenaar586c70c2018-10-02 16:23:58 +02007741 exec_normal(FALSE, FALSE, FALSE);
Bram Moolenaar25281632016-01-21 23:32:32 +01007742}
Bram Moolenaar25281632016-01-21 23:32:32 +01007743
Bram Moolenaar25281632016-01-21 23:32:32 +01007744/*
7745 * Execute normal_cmd() until there is no typeahead left.
Bram Moolenaar586c70c2018-10-02 16:23:58 +02007746 * When "use_vpeekc" is TRUE use vpeekc() to check for available chars.
Bram Moolenaar25281632016-01-21 23:32:32 +01007747 */
7748 void
Bram Moolenaar586c70c2018-10-02 16:23:58 +02007749exec_normal(int was_typed, int use_vpeekc, int may_use_terminal_loop UNUSED)
Bram Moolenaar25281632016-01-21 23:32:32 +01007750{
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00007751 oparg_T oa;
Bram Moolenaar905dd902019-04-07 14:21:47 +02007752 int c;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00007753
Bram Moolenaar905dd902019-04-07 14:21:47 +02007754 // When calling vpeekc() from feedkeys() it will return Ctrl_C when there
7755 // is nothing to get, so also check for Ctrl_C.
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00007756 clear_oparg(&oa);
7757 finish_op = FALSE;
Bram Moolenaar586c70c2018-10-02 16:23:58 +02007758 while ((!stuff_empty()
7759 || ((was_typed || !typebuf_typed()) && typebuf.tb_len > 0)
Bram Moolenaar905dd902019-04-07 14:21:47 +02007760 || (use_vpeekc && (c = vpeekc()) != NUL && c != Ctrl_C))
Bram Moolenaar586c70c2018-10-02 16:23:58 +02007761 && !got_int)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00007762 {
7763 update_topline_cursor();
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02007764#ifdef FEAT_TERMINAL
Bram Moolenaar655a82a2018-05-08 22:01:07 +02007765 if (may_use_terminal_loop && term_use_loop()
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02007766 && oa.op_type == OP_NOP && oa.regname == NUL
7767 && !VIsual_active)
7768 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007769 // If terminal_loop() returns OK we got a key that is handled
7770 // in Normal model. With FAIL we first need to position the
7771 // cursor and the screen needs to be redrawn.
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02007772 if (terminal_loop(TRUE) == OK)
7773 normal_cmd(&oa, TRUE);
7774 }
7775 else
7776#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007777 // execute a Normal mode cmd
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02007778 normal_cmd(&oa, TRUE);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00007779 }
7780}
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00007781
Bram Moolenaar071d4272004-06-13 20:20:40 +00007782#ifdef FEAT_FIND_ID
7783 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007784ex_checkpath(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007785{
7786 find_pattern_in_path(NULL, 0, 0, FALSE, FALSE, CHECK_PATH, 1L,
7787 eap->forceit ? ACTION_SHOW_ALL : ACTION_SHOW,
7788 (linenr_T)1, (linenr_T)MAXLNUM);
7789}
7790
Bram Moolenaar4033c552017-09-16 20:54:51 +02007791#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007792/*
7793 * ":psearch"
7794 */
7795 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007796ex_psearch(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007797{
7798 g_do_tagpreview = p_pvh;
7799 ex_findpat(eap);
7800 g_do_tagpreview = 0;
7801}
7802#endif
7803
7804 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007805ex_findpat(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007806{
7807 int whole = TRUE;
7808 long n;
7809 char_u *p;
7810 int action;
7811
7812 switch (cmdnames[eap->cmdidx].cmd_name[2])
7813 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007814 case 'e': // ":psearch", ":isearch" and ":dsearch"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007815 if (cmdnames[eap->cmdidx].cmd_name[0] == 'p')
7816 action = ACTION_GOTO;
7817 else
7818 action = ACTION_SHOW;
7819 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007820 case 'i': // ":ilist" and ":dlist"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007821 action = ACTION_SHOW_ALL;
7822 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007823 case 'u': // ":ijump" and ":djump"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007824 action = ACTION_GOTO;
7825 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007826 default: // ":isplit" and ":dsplit"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007827 action = ACTION_SPLIT;
7828 break;
7829 }
7830
7831 n = 1;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007832 if (vim_isdigit(*eap->arg)) // get count
Bram Moolenaar071d4272004-06-13 20:20:40 +00007833 {
7834 n = getdigits(&eap->arg);
7835 eap->arg = skipwhite(eap->arg);
7836 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007837 if (*eap->arg == '/') // Match regexp, not just whole words
Bram Moolenaar071d4272004-06-13 20:20:40 +00007838 {
7839 whole = FALSE;
7840 ++eap->arg;
7841 p = skip_regexp(eap->arg, '/', p_magic, NULL);
7842 if (*p)
7843 {
7844 *p++ = NUL;
7845 p = skipwhite(p);
7846
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007847 // Check for trailing illegal characters
Bram Moolenaar071d4272004-06-13 20:20:40 +00007848 if (!ends_excmd(*p))
7849 eap->errmsg = e_trailing;
7850 else
7851 eap->nextcmd = check_nextcmd(p);
7852 }
7853 }
7854 if (!eap->skip)
7855 find_pattern_in_path(eap->arg, 0, (int)STRLEN(eap->arg),
7856 whole, !eap->forceit,
7857 *eap->cmd == 'd' ? FIND_DEFINE : FIND_ANY,
7858 n, action, eap->line1, eap->line2);
7859}
7860#endif
7861
Bram Moolenaar071d4272004-06-13 20:20:40 +00007862
Bram Moolenaar4033c552017-09-16 20:54:51 +02007863#ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00007864/*
7865 * ":ptag", ":ptselect", ":ptjump", ":ptnext", etc.
7866 */
7867 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007868ex_ptag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007869{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007870 g_do_tagpreview = p_pvh; // will be reset to 0 in ex_tag_cmd()
Bram Moolenaar071d4272004-06-13 20:20:40 +00007871 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
7872}
7873
7874/*
7875 * ":pedit"
7876 */
7877 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007878ex_pedit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007879{
7880 win_T *curwin_save = curwin;
7881
Bram Moolenaar026587b2019-08-17 15:08:00 +02007882 // Open the preview window or popup and make it the current window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007883 g_do_tagpreview = p_pvh;
Bram Moolenaar576a4a62019-08-18 15:25:17 +02007884 prepare_tagpreview(TRUE, TRUE, FALSE);
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02007885
Bram Moolenaar026587b2019-08-17 15:08:00 +02007886 // Edit the file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007887 do_exedit(eap, NULL);
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02007888
Bram Moolenaar071d4272004-06-13 20:20:40 +00007889 if (curwin != curwin_save && win_valid(curwin_save))
7890 {
Bram Moolenaar026587b2019-08-17 15:08:00 +02007891 // Return cursor to where we were
Bram Moolenaar071d4272004-06-13 20:20:40 +00007892 validate_cursor();
7893 redraw_later(VALID);
7894 win_enter(curwin_save, TRUE);
7895 }
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01007896# ifdef FEAT_PROP_POPUP
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02007897 else if (WIN_IS_POPUP(curwin))
7898 {
7899 // can't keep focus in popup window
7900 win_enter(firstwin, TRUE);
7901 }
7902# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007903 g_do_tagpreview = 0;
7904}
Bram Moolenaar4033c552017-09-16 20:54:51 +02007905#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007906
7907/*
7908 * ":stag", ":stselect" and ":stjump".
7909 */
7910 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007911ex_stag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007912{
7913 postponed_split = -1;
7914 postponed_split_flags = cmdmod.split;
Bram Moolenaard326ce82007-03-11 14:48:29 +00007915 postponed_split_tab = cmdmod.tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007916 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
7917 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00007918 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007919}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007920
7921/*
7922 * ":tag", ":tselect", ":tjump", ":tnext", etc.
7923 */
7924 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007925ex_tag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007926{
7927 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name);
7928}
7929
7930 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007931ex_tag_cmd(exarg_T *eap, char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007932{
7933 int cmd;
7934
7935 switch (name[1])
7936 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007937 case 'j': cmd = DT_JUMP; // ":tjump"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007938 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007939 case 's': cmd = DT_SELECT; // ":tselect"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007940 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007941 case 'p': cmd = DT_PREV; // ":tprevious"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007942 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007943 case 'N': cmd = DT_PREV; // ":tNext"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007944 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007945 case 'n': cmd = DT_NEXT; // ":tnext"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007946 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007947 case 'o': cmd = DT_POP; // ":pop"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007948 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007949 case 'f': // ":tfirst"
7950 case 'r': cmd = DT_FIRST; // ":trewind"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007951 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007952 case 'l': cmd = DT_LAST; // ":tlast"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007953 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007954 default: // ":tag"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007955#ifdef FEAT_CSCOPE
Bram Moolenaar7c94c262008-06-20 09:11:34 +00007956 if (p_cst && *eap->arg != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007957 {
Bram Moolenaard4db7712016-11-12 19:16:46 +01007958 ex_cstag(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007959 return;
7960 }
7961#endif
7962 cmd = DT_TAG;
7963 break;
7964 }
7965
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00007966 if (name[0] == 'l')
7967 {
7968#ifndef FEAT_QUICKFIX
7969 ex_ni(eap);
7970 return;
7971#else
7972 cmd = DT_LTAG;
7973#endif
7974 }
7975
Bram Moolenaar071d4272004-06-13 20:20:40 +00007976 do_tag(eap->arg, cmd, eap->addr_count > 0 ? (int)eap->line2 : 1,
7977 eap->forceit, TRUE);
7978}
7979
7980/*
Bram Moolenaar05bb9532008-07-04 09:44:11 +00007981 * Check "str" for starting with a special cmdline variable.
7982 * If found return one of the SPEC_ values and set "*usedlen" to the length of
7983 * the variable. Otherwise return -1 and "*usedlen" is unchanged.
7984 */
7985 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007986find_cmdline_var(char_u *src, int *usedlen)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00007987{
7988 int len;
7989 int i;
Bram Moolenaar8f0b2d42009-05-16 14:41:10 +00007990 static char *(spec_str[]) = {
Bram Moolenaar05bb9532008-07-04 09:44:11 +00007991 "%",
7992#define SPEC_PERC 0
7993 "#",
Bram Moolenaar65f08472017-09-10 18:16:20 +02007994#define SPEC_HASH (SPEC_PERC + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007995 "<cword>", // cursor word
Bram Moolenaar65f08472017-09-10 18:16:20 +02007996#define SPEC_CWORD (SPEC_HASH + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007997 "<cWORD>", // cursor WORD
Bram Moolenaar65f08472017-09-10 18:16:20 +02007998#define SPEC_CCWORD (SPEC_CWORD + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007999 "<cexpr>", // expr under cursor
Bram Moolenaar65f08472017-09-10 18:16:20 +02008000#define SPEC_CEXPR (SPEC_CCWORD + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008001 "<cfile>", // cursor path name
Bram Moolenaar65f08472017-09-10 18:16:20 +02008002#define SPEC_CFILE (SPEC_CEXPR + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008003 "<sfile>", // ":so" file name
Bram Moolenaar65f08472017-09-10 18:16:20 +02008004#define SPEC_SFILE (SPEC_CFILE + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008005 "<slnum>", // ":so" file line number
Bram Moolenaar65f08472017-09-10 18:16:20 +02008006#define SPEC_SLNUM (SPEC_SFILE + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008007 "<afile>", // autocommand file name
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008008#define SPEC_AFILE (SPEC_SLNUM + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008009 "<abuf>", // autocommand buffer number
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008010#define SPEC_ABUF (SPEC_AFILE + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008011 "<amatch>", // autocommand match name
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01008012#define SPEC_AMATCH (SPEC_ABUF + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008013 "<sflnum>", // script file line number
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008014#define SPEC_SFLNUM (SPEC_AMATCH + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008015#ifdef FEAT_CLIENTSERVER
8016 "<client>"
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008017# define SPEC_CLIENT (SPEC_SFLNUM + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008018#endif
8019 };
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008020
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00008021 for (i = 0; i < (int)(sizeof(spec_str) / sizeof(char *)); ++i)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008022 {
8023 len = (int)STRLEN(spec_str[i]);
8024 if (STRNCMP(src, spec_str[i], len) == 0)
8025 {
8026 *usedlen = len;
8027 return i;
8028 }
8029 }
8030 return -1;
8031}
8032
8033/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008034 * Evaluate cmdline variables.
8035 *
8036 * change '%' to curbuf->b_ffname
8037 * '#' to curwin->w_altfile
8038 * '<cword>' to word under the cursor
8039 * '<cWORD>' to WORD under the cursor
Bram Moolenaar8071cb22019-07-12 17:58:01 +02008040 * '<cexpr>' to C-expression under the cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00008041 * '<cfile>' to path name under the cursor
8042 * '<sfile>' to sourced file name
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01008043 * '<slnum>' to sourced file line number
Bram Moolenaar071d4272004-06-13 20:20:40 +00008044 * '<afile>' to file name for autocommand
8045 * '<abuf>' to buffer number for autocommand
8046 * '<amatch>' to matching name for autocommand
8047 *
8048 * When an error is detected, "errormsg" is set to a non-NULL pointer (may be
8049 * "" for error without a message) and NULL is returned.
8050 * Returns an allocated string if a valid match was found.
8051 * Returns NULL if no match was found. "usedlen" then still contains the
8052 * number of characters to skip.
8053 */
8054 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008055eval_vars(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008056 char_u *src, // pointer into commandline
8057 char_u *srcstart, // beginning of valid memory for src
8058 int *usedlen, // characters after src that are used
8059 linenr_T *lnump, // line number for :e command, or NULL
8060 char **errormsg, // pointer to error message
8061 int *escaped) // return value has escaped white space (can
8062 // be NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008063{
8064 int i;
8065 char_u *s;
8066 char_u *result;
8067 char_u *resultbuf = NULL;
8068 int resultlen;
8069 buf_T *buf;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008070 int valid = VALID_HEAD + VALID_PATH; // assume valid result
Bram Moolenaar071d4272004-06-13 20:20:40 +00008071 int spec_idx;
Bram Moolenaarfd249462018-07-28 16:14:30 +02008072 int tilde_file = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008073 int skip_mod = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008074 char_u strbuf[30];
Bram Moolenaar071d4272004-06-13 20:20:40 +00008075
8076 *errormsg = NULL;
Bram Moolenaar63b92542007-03-27 14:57:09 +00008077 if (escaped != NULL)
8078 *escaped = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008079
8080 /*
8081 * Check if there is something to do.
8082 */
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008083 spec_idx = find_cmdline_var(src, usedlen);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008084 if (spec_idx < 0) // no match
Bram Moolenaar071d4272004-06-13 20:20:40 +00008085 {
8086 *usedlen = 1;
8087 return NULL;
8088 }
8089
8090 /*
8091 * Skip when preceded with a backslash "\%" and "\#".
8092 * Note: In "\\%" the % is also not recognized!
8093 */
8094 if (src > srcstart && src[-1] == '\\')
8095 {
8096 *usedlen = 0;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008097 STRMOVE(src - 1, src); // remove backslash
Bram Moolenaar071d4272004-06-13 20:20:40 +00008098 return NULL;
8099 }
8100
8101 /*
8102 * word or WORD under cursor
8103 */
Bram Moolenaar65f08472017-09-10 18:16:20 +02008104 if (spec_idx == SPEC_CWORD || spec_idx == SPEC_CCWORD
8105 || spec_idx == SPEC_CEXPR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008106 {
Bram Moolenaar65f08472017-09-10 18:16:20 +02008107 resultlen = find_ident_under_cursor(&result,
8108 spec_idx == SPEC_CWORD ? (FIND_IDENT | FIND_STRING)
8109 : spec_idx == SPEC_CEXPR ? (FIND_IDENT | FIND_STRING | FIND_EVAL)
8110 : FIND_STRING);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008111 if (resultlen == 0)
8112 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008113 *errormsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +00008114 return NULL;
8115 }
8116 }
8117
8118 /*
8119 * '#': Alternate file name
8120 * '%': Current file name
8121 * File name under the cursor
8122 * File name for autocommand
8123 * and following modifiers
8124 */
8125 else
8126 {
8127 switch (spec_idx)
8128 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008129 case SPEC_PERC: // '%': current file
Bram Moolenaar071d4272004-06-13 20:20:40 +00008130 if (curbuf->b_fname == NULL)
8131 {
8132 result = (char_u *)"";
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008133 valid = 0; // Must have ":p:h" to be valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00008134 }
8135 else
Bram Moolenaar00136dc2018-07-25 21:19:13 +02008136 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008137 result = curbuf->b_fname;
Bram Moolenaar00136dc2018-07-25 21:19:13 +02008138 tilde_file = STRCMP(result, "~") == 0;
8139 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008140 break;
8141
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008142 case SPEC_HASH: // '#' or "#99": alternate file
8143 if (src[1] == '#') // "##": the argument list
Bram Moolenaar071d4272004-06-13 20:20:40 +00008144 {
8145 result = arg_all();
8146 resultbuf = result;
8147 *usedlen = 2;
Bram Moolenaar63b92542007-03-27 14:57:09 +00008148 if (escaped != NULL)
8149 *escaped = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008150 skip_mod = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008151 break;
8152 }
8153 s = src + 1;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008154 if (*s == '<') // "#<99" uses v:oldfiles
Bram Moolenaard812df62008-11-09 12:46:09 +00008155 ++s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008156 i = (int)getdigits(&s);
Bram Moolenaarc312b8b2017-10-28 17:53:04 +02008157 if (s == src + 2 && src[1] == '-')
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008158 // just a minus sign, don't skip over it
Bram Moolenaarc312b8b2017-10-28 17:53:04 +02008159 s--;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008160 *usedlen = (int)(s - src); // length of what we expand
Bram Moolenaar071d4272004-06-13 20:20:40 +00008161
Bram Moolenaarc312b8b2017-10-28 17:53:04 +02008162 if (src[1] == '<' && i != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008163 {
Bram Moolenaard812df62008-11-09 12:46:09 +00008164 if (*usedlen < 2)
8165 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008166 // Should we give an error message for #<text?
Bram Moolenaard812df62008-11-09 12:46:09 +00008167 *usedlen = 1;
8168 return NULL;
8169 }
8170#ifdef FEAT_EVAL
8171 result = list_find_str(get_vim_var_list(VV_OLDFILES),
8172 (long)i);
8173 if (result == NULL)
8174 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008175 *errormsg = "";
Bram Moolenaard812df62008-11-09 12:46:09 +00008176 return NULL;
8177 }
8178#else
Bram Moolenaar8e481e82019-01-15 20:07:48 +01008179 *errormsg = _("E809: #< is not available without the +eval feature");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008180 return NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +00008181#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008182 }
8183 else
Bram Moolenaard812df62008-11-09 12:46:09 +00008184 {
Bram Moolenaarc312b8b2017-10-28 17:53:04 +02008185 if (i == 0 && src[1] == '<' && *usedlen > 1)
8186 *usedlen = 1;
Bram Moolenaard812df62008-11-09 12:46:09 +00008187 buf = buflist_findnr(i);
8188 if (buf == NULL)
8189 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008190 *errormsg = _("E194: No alternate file name to substitute for '#'");
Bram Moolenaard812df62008-11-09 12:46:09 +00008191 return NULL;
8192 }
8193 if (lnump != NULL)
8194 *lnump = ECMD_LAST;
8195 if (buf->b_fname == NULL)
8196 {
8197 result = (char_u *)"";
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008198 valid = 0; // Must have ":p:h" to be valid
Bram Moolenaard812df62008-11-09 12:46:09 +00008199 }
8200 else
Bram Moolenaar00136dc2018-07-25 21:19:13 +02008201 {
Bram Moolenaard812df62008-11-09 12:46:09 +00008202 result = buf->b_fname;
Bram Moolenaar00136dc2018-07-25 21:19:13 +02008203 tilde_file = STRCMP(result, "~") == 0;
8204 }
Bram Moolenaard812df62008-11-09 12:46:09 +00008205 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008206 break;
8207
8208#ifdef FEAT_SEARCHPATH
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008209 case SPEC_CFILE: // file name under cursor
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008210 result = file_name_at_cursor(FNAME_MESS|FNAME_HYP, 1L, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008211 if (result == NULL)
8212 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008213 *errormsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +00008214 return NULL;
8215 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008216 resultbuf = result; // remember allocated string
Bram Moolenaar071d4272004-06-13 20:20:40 +00008217 break;
8218#endif
8219
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008220 case SPEC_AFILE: // file name for autocommand
Bram Moolenaar071d4272004-06-13 20:20:40 +00008221 result = autocmd_fname;
Bram Moolenaarf6dad432008-09-18 19:29:58 +00008222 if (result != NULL && !autocmd_fname_full)
8223 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008224 // Still need to turn the fname into a full path. It is
8225 // postponed to avoid a delay when <afile> is not used.
Bram Moolenaarf6dad432008-09-18 19:29:58 +00008226 autocmd_fname_full = TRUE;
8227 result = FullName_save(autocmd_fname, FALSE);
8228 vim_free(autocmd_fname);
8229 autocmd_fname = result;
8230 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008231 if (result == NULL)
8232 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008233 *errormsg = _("E495: no autocommand file name to substitute for \"<afile>\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008234 return NULL;
8235 }
Bram Moolenaara0174af2008-01-02 20:08:25 +00008236 result = shorten_fname1(result);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008237 break;
8238
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008239 case SPEC_ABUF: // buffer number for autocommand
Bram Moolenaar071d4272004-06-13 20:20:40 +00008240 if (autocmd_bufnr <= 0)
8241 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008242 *errormsg = _("E496: no autocommand buffer number to substitute for \"<abuf>\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008243 return NULL;
8244 }
8245 sprintf((char *)strbuf, "%d", autocmd_bufnr);
8246 result = strbuf;
8247 break;
8248
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008249 case SPEC_AMATCH: // match name for autocommand
Bram Moolenaar071d4272004-06-13 20:20:40 +00008250 result = autocmd_match;
8251 if (result == NULL)
8252 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008253 *errormsg = _("E497: no autocommand match name to substitute for \"<amatch>\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008254 return NULL;
8255 }
8256 break;
8257
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008258 case SPEC_SFILE: // file name for ":so" command
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01008259 result = estack_sfile();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008260 if (result == NULL)
8261 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008262 *errormsg = _("E498: no :source file name to substitute for \"<sfile>\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008263 return NULL;
8264 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01008265 resultbuf = result; // remember allocated string
Bram Moolenaar071d4272004-06-13 20:20:40 +00008266 break;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008267
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008268 case SPEC_SLNUM: // line in file for ":so" command
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01008269 if (SOURCING_NAME == NULL || SOURCING_LNUM == 0)
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01008270 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008271 *errormsg = _("E842: no line number to use for \"<slnum>\"");
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01008272 return NULL;
8273 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01008274 sprintf((char *)strbuf, "%ld", SOURCING_LNUM);
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01008275 result = strbuf;
8276 break;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008277
8278#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008279 case SPEC_SFLNUM: // line in script file
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01008280 if (current_sctx.sc_lnum + SOURCING_LNUM == 0)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008281 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008282 *errormsg = _("E961: no line number to use for \"<sflnum>\"");
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008283 return NULL;
8284 }
8285 sprintf((char *)strbuf, "%ld",
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01008286 (long)(current_sctx.sc_lnum + SOURCING_LNUM));
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008287 result = strbuf;
8288 break;
8289#endif
8290
8291#ifdef FEAT_CLIENTSERVER
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008292 case SPEC_CLIENT: // Source of last submitted input
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008293 sprintf((char *)strbuf, PRINTF_HEX_LONG_U,
8294 (long_u)clientWindow);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008295 result = strbuf;
8296 break;
8297#endif
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008298
Bram Moolenaar9e0f6ec2017-05-16 09:36:54 +02008299 default:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008300 result = (char_u *)""; // avoid gcc warning
Bram Moolenaar9e0f6ec2017-05-16 09:36:54 +02008301 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008302 }
8303
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008304 resultlen = (int)STRLEN(result); // length of new string
8305 if (src[*usedlen] == '<') // remove the file name extension
Bram Moolenaar071d4272004-06-13 20:20:40 +00008306 {
8307 ++*usedlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008308 if ((s = vim_strrchr(result, '.')) != NULL && s >= gettail(result))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008309 resultlen = (int)(s - result);
8310 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008311 else if (!skip_mod)
8312 {
Bram Moolenaar00136dc2018-07-25 21:19:13 +02008313 valid |= modify_fname(src, tilde_file, usedlen, &result, &resultbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008314 &resultlen);
8315 if (result == NULL)
8316 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008317 *errormsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +00008318 return NULL;
8319 }
8320 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008321 }
8322
8323 if (resultlen == 0 || valid != VALID_HEAD + VALID_PATH)
8324 {
8325 if (valid != VALID_HEAD + VALID_PATH)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008326 // xgettext:no-c-format
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008327 *errormsg = _("E499: Empty file name for '%' or '#', only works with \":p:h\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008328 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008329 *errormsg = _("E500: Evaluates to an empty string");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008330 result = NULL;
8331 }
8332 else
8333 result = vim_strnsave(result, resultlen);
8334 vim_free(resultbuf);
8335 return result;
8336}
8337
8338/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008339 * Expand the <sfile> string in "arg".
8340 *
8341 * Returns an allocated string, or NULL for any error.
8342 */
8343 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008344expand_sfile(char_u *arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008345{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008346 char *errormsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008347 int len;
8348 char_u *result;
8349 char_u *newres;
8350 char_u *repl;
8351 int srclen;
8352 char_u *p;
8353
8354 result = vim_strsave(arg);
8355 if (result == NULL)
8356 return NULL;
8357
8358 for (p = result; *p; )
8359 {
8360 if (STRNCMP(p, "<sfile>", 7) != 0)
8361 ++p;
8362 else
8363 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008364 // replace "<sfile>" with the sourced file name, and do ":" stuff
Bram Moolenaar63b92542007-03-27 14:57:09 +00008365 repl = eval_vars(p, result, &srclen, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008366 if (errormsg != NULL)
8367 {
8368 if (*errormsg)
8369 emsg(errormsg);
8370 vim_free(result);
8371 return NULL;
8372 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008373 if (repl == NULL) // no match (cannot happen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008374 {
8375 p += srclen;
8376 continue;
8377 }
8378 len = (int)STRLEN(result) - srclen + (int)STRLEN(repl) + 1;
8379 newres = alloc(len);
8380 if (newres == NULL)
8381 {
8382 vim_free(repl);
8383 vim_free(result);
8384 return NULL;
8385 }
8386 mch_memmove(newres, result, (size_t)(p - result));
8387 STRCPY(newres + (p - result), repl);
8388 len = (int)STRLEN(newres);
8389 STRCAT(newres, p + srclen);
8390 vim_free(repl);
8391 vim_free(result);
8392 result = newres;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008393 p = newres + len; // continue after the match
Bram Moolenaar071d4272004-06-13 20:20:40 +00008394 }
8395 }
8396
8397 return result;
8398}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008399
Bram Moolenaar071d4272004-06-13 20:20:40 +00008400#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG) || defined(PROTO)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00008401/*
Bram Moolenaard9462e32011-04-11 21:35:11 +02008402 * Make a dialog message in "buff[DIALOG_MSG_SIZE]".
Bram Moolenaarb765d632005-06-07 21:00:02 +00008403 * "format" must contain "%s".
Bram Moolenaar9c13b352005-05-19 20:53:52 +00008404 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008405 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008406dialog_msg(char_u *buff, char *format, char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008407{
Bram Moolenaar071d4272004-06-13 20:20:40 +00008408 if (fname == NULL)
8409 fname = (char_u *)_("Untitled");
Bram Moolenaard9462e32011-04-11 21:35:11 +02008410 vim_snprintf((char *)buff, DIALOG_MSG_SIZE, format, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008411}
8412#endif
8413
8414/*
8415 * ":behave {mswin,xterm}"
8416 */
8417 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008418ex_behave(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008419{
8420 if (STRCMP(eap->arg, "mswin") == 0)
8421 {
8422 set_option_value((char_u *)"selection", 0L, (char_u *)"exclusive", 0);
8423 set_option_value((char_u *)"selectmode", 0L, (char_u *)"mouse,key", 0);
8424 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"popup", 0);
8425 set_option_value((char_u *)"keymodel", 0L,
8426 (char_u *)"startsel,stopsel", 0);
8427 }
8428 else if (STRCMP(eap->arg, "xterm") == 0)
8429 {
8430 set_option_value((char_u *)"selection", 0L, (char_u *)"inclusive", 0);
8431 set_option_value((char_u *)"selectmode", 0L, (char_u *)"", 0);
8432 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"extend", 0);
8433 set_option_value((char_u *)"keymodel", 0L, (char_u *)"", 0);
8434 }
8435 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008436 semsg(_(e_invarg2), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008437}
8438
Bram Moolenaar071d4272004-06-13 20:20:40 +00008439static int filetype_detect = FALSE;
8440static int filetype_plugin = FALSE;
8441static int filetype_indent = FALSE;
8442
8443/*
8444 * ":filetype [plugin] [indent] {on,off,detect}"
8445 * on: Load the filetype.vim file to install autocommands for file types.
8446 * off: Load the ftoff.vim file to remove all autocommands for file types.
8447 * plugin on: load filetype.vim and ftplugin.vim
8448 * plugin off: load ftplugof.vim
8449 * indent on: load filetype.vim and indent.vim
8450 * indent off: load indoff.vim
8451 */
8452 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008453ex_filetype(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008454{
8455 char_u *arg = eap->arg;
8456 int plugin = FALSE;
8457 int indent = FALSE;
8458
8459 if (*eap->arg == NUL)
8460 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008461 // Print current status.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008462 smsg("filetype detection:%s plugin:%s indent:%s",
Bram Moolenaar071d4272004-06-13 20:20:40 +00008463 filetype_detect ? "ON" : "OFF",
8464 filetype_plugin ? (filetype_detect ? "ON" : "(on)") : "OFF",
8465 filetype_indent ? (filetype_detect ? "ON" : "(on)") : "OFF");
8466 return;
8467 }
8468
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008469 // Accept "plugin" and "indent" in any order.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008470 for (;;)
8471 {
8472 if (STRNCMP(arg, "plugin", 6) == 0)
8473 {
8474 plugin = TRUE;
8475 arg = skipwhite(arg + 6);
8476 continue;
8477 }
8478 if (STRNCMP(arg, "indent", 6) == 0)
8479 {
8480 indent = TRUE;
8481 arg = skipwhite(arg + 6);
8482 continue;
8483 }
8484 break;
8485 }
8486 if (STRCMP(arg, "on") == 0 || STRCMP(arg, "detect") == 0)
8487 {
8488 if (*arg == 'o' || !filetype_detect)
8489 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01008490 source_runtime((char_u *)FILETYPE_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008491 filetype_detect = TRUE;
8492 if (plugin)
8493 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01008494 source_runtime((char_u *)FTPLUGIN_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008495 filetype_plugin = TRUE;
8496 }
8497 if (indent)
8498 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01008499 source_runtime((char_u *)INDENT_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008500 filetype_indent = TRUE;
8501 }
8502 }
8503 if (*arg == 'd')
8504 {
Bram Moolenaar1610d052016-06-09 22:53:01 +02008505 (void)do_doautocmd((char_u *)"filetypedetect BufRead", TRUE, NULL);
Bram Moolenaara3227e22006-03-08 21:32:40 +00008506 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008507 }
8508 }
8509 else if (STRCMP(arg, "off") == 0)
8510 {
8511 if (plugin || indent)
8512 {
8513 if (plugin)
8514 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01008515 source_runtime((char_u *)FTPLUGOF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008516 filetype_plugin = FALSE;
8517 }
8518 if (indent)
8519 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01008520 source_runtime((char_u *)INDOFF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008521 filetype_indent = FALSE;
8522 }
8523 }
8524 else
8525 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01008526 source_runtime((char_u *)FTOFF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008527 filetype_detect = FALSE;
8528 }
8529 }
8530 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008531 semsg(_(e_invarg2), arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008532}
8533
8534/*
Bram Moolenaar3e545692017-06-04 19:00:32 +02008535 * ":setfiletype [FALLBACK] {name}"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008536 */
8537 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008538ex_setfiletype(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008539{
8540 if (!did_filetype)
Bram Moolenaar3e545692017-06-04 19:00:32 +02008541 {
8542 char_u *arg = eap->arg;
8543
8544 if (STRNCMP(arg, "FALLBACK ", 9) == 0)
8545 arg += 9;
8546
8547 set_option_value((char_u *)"filetype", 0L, arg, OPT_LOCAL);
8548 if (arg != eap->arg)
8549 did_filetype = FALSE;
8550 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008551}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008552
Bram Moolenaar071d4272004-06-13 20:20:40 +00008553 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008554ex_digraphs(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008555{
8556#ifdef FEAT_DIGRAPHS
8557 if (*eap->arg != NUL)
8558 putdigraph(eap->arg);
8559 else
Bram Moolenaareae8ae12018-12-14 18:53:02 +01008560 listdigraphs(eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008561#else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008562 emsg(_("E196: No digraphs in this version"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008563#endif
8564}
8565
Bram Moolenaar451fc7b2018-04-27 22:53:07 +02008566#if defined(FEAT_SEARCH_EXTRA) || defined(PROTO)
8567 void
8568set_no_hlsearch(int flag)
8569{
8570 no_hlsearch = flag;
8571# ifdef FEAT_EVAL
8572 set_vim_var_nr(VV_HLSEARCH, !no_hlsearch && p_hls);
8573# endif
8574}
8575
Bram Moolenaar071d4272004-06-13 20:20:40 +00008576/*
8577 * ":nohlsearch"
8578 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008579 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008580ex_nohlsearch(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008581{
Bram Moolenaar451fc7b2018-04-27 22:53:07 +02008582 set_no_hlsearch(TRUE);
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00008583 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008584}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008585#endif
8586
8587#ifdef FEAT_CRYPT
8588/*
8589 * ":X": Get crypt key
8590 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008591 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008592ex_X(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008593{
Bram Moolenaar3a0c9082014-11-12 15:15:42 +01008594 crypt_check_current_method();
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02008595 (void)crypt_get_key(TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008596}
8597#endif
8598
8599#ifdef FEAT_FOLDING
8600 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008601ex_fold(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008602{
8603 if (foldManualAllowed(TRUE))
8604 foldCreate(eap->line1, eap->line2);
8605}
8606
8607 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008608ex_foldopen(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008609{
8610 opFoldRange(eap->line1, eap->line2, eap->cmdidx == CMD_foldopen,
8611 eap->forceit, FALSE);
8612}
8613
8614 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008615ex_folddo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008616{
8617 linenr_T lnum;
8618
Bram Moolenaar4facea32019-10-12 20:17:40 +02008619# ifdef FEAT_CLIPBOARD
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02008620 start_global_changes();
Bram Moolenaar4facea32019-10-12 20:17:40 +02008621# endif
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02008622
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008623 // First set the marks for all lines closed/open.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008624 for (lnum = eap->line1; lnum <= eap->line2; ++lnum)
8625 if (hasFolding(lnum, NULL, NULL) == (eap->cmdidx == CMD_folddoclosed))
8626 ml_setmarked(lnum);
8627
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008628 // Execute the command on the marked lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008629 global_exe(eap->arg);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008630 ml_clearmarked(); // clear rest of the marks
Bram Moolenaar4facea32019-10-12 20:17:40 +02008631# ifdef FEAT_CLIPBOARD
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02008632 end_global_changes();
Bram Moolenaar4facea32019-10-12 20:17:40 +02008633# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008634}
8635#endif
Bram Moolenaarf5291f32017-09-14 22:55:37 +02008636
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008637#if defined(FEAT_QUICKFIX) || defined(PROTO)
Bram Moolenaar39665952018-08-15 20:59:48 +02008638/*
8639 * Returns TRUE if the supplied Ex cmdidx is for a location list command
8640 * instead of a quickfix command.
8641 */
8642 int
8643is_loclist_cmd(int cmdidx)
8644{
Bram Moolenaar74c8be22018-08-23 22:51:40 +02008645 if (cmdidx < 0 || cmdidx >= CMD_SIZE)
Bram Moolenaar39665952018-08-15 20:59:48 +02008646 return FALSE;
8647 return cmdnames[cmdidx].cmd_name[0] == 'l';
8648}
8649#endif
8650
Bram Moolenaar4facea32019-10-12 20:17:40 +02008651#if defined(FEAT_TIMERS) || defined(PROTO)
Bram Moolenaarf5291f32017-09-14 22:55:37 +02008652 int
8653get_pressedreturn(void)
8654{
8655 return ex_pressedreturn;
8656}
8657
8658 void
8659set_pressedreturn(int val)
8660{
8661 ex_pressedreturn = val;
8662}
8663#endif