blob: a5562cc28a8dde1689253ad4b06a8f9bba60c201 [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * ex_docmd.c: functions for executing an Ex command line.
12 */
13
14#include "vim.h"
15
Bram Moolenaar071d4272004-06-13 20:20:40 +000016static int quitmore = 0;
17static int ex_pressedreturn = FALSE;
18#ifndef FEAT_PRINTER
19# define ex_hardcopy ex_ni
20#endif
21
Bram Moolenaar071d4272004-06-13 20:20:40 +000022#ifdef FEAT_EVAL
Bram Moolenaarddef1292019-12-16 17:10:33 +010023static char_u *do_one_cmd(char_u **, int, cstack_T *, char_u *(*fgetline)(int, void *, int, int), void *cookie);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024#else
Bram Moolenaare96a2492019-06-25 04:12:16 +020025static char_u *do_one_cmd(char_u **, int, char_u *(*fgetline)(int, void *, int, int), void *cookie);
Bram Moolenaar217e1b82019-12-01 21:41:28 +010026static int if_level = 0; // depth in :if
Bram Moolenaar071d4272004-06-13 20:20:40 +000027#endif
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +020028static void free_cmdmod(void);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010029static void append_command(char_u *cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000030
Bram Moolenaar071d4272004-06-13 20:20:40 +000031#ifndef FEAT_MENU
32# define ex_emenu ex_ni
33# define ex_menu ex_ni
34# define ex_menutranslate ex_ni
35#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010036static void ex_autocmd(exarg_T *eap);
37static void ex_doautocmd(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010038static void ex_bunload(exarg_T *eap);
39static void ex_buffer(exarg_T *eap);
40static void ex_bmodified(exarg_T *eap);
41static void ex_bnext(exarg_T *eap);
42static void ex_bprevious(exarg_T *eap);
43static void ex_brewind(exarg_T *eap);
44static void ex_blast(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010045static char_u *getargcmd(char_u **);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010046static int getargopt(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +000047#ifndef FEAT_QUICKFIX
48# define ex_make ex_ni
Bram Moolenaar86b68352004-12-27 21:59:20 +000049# define ex_cbuffer ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +000050# define ex_cc ex_ni
51# define ex_cnext ex_ni
Bram Moolenaar3ff33112019-05-03 21:56:35 +020052# define ex_cbelow ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +000053# define ex_cfile ex_ni
54# define qf_list ex_ni
55# define qf_age ex_ni
Bram Moolenaarf6acffb2016-07-16 16:54:24 +020056# define qf_history ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +000057# define ex_helpgrep ex_ni
Bram Moolenaar86b68352004-12-27 21:59:20 +000058# define ex_vimgrep ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +000059#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +020060#if !defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +000061# define ex_cclose ex_ni
62# define ex_copen ex_ni
63# define ex_cwindow ex_ni
Bram Moolenaardcb17002016-07-07 18:58:59 +020064# define ex_cbottom ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +000065#endif
Bram Moolenaar1e015462005-09-25 22:16:38 +000066#if !defined(FEAT_QUICKFIX) || !defined(FEAT_EVAL)
67# define ex_cexpr ex_ni
68#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000069
Bram Moolenaarb7316892019-05-01 18:08:42 +020070static linenr_T get_address(exarg_T *, char_u **, cmd_addr_T addr_type, int skip, int silent, int to_other_file, int address_count);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010071static void get_flags(exarg_T *eap);
Bram Moolenaar85363ab2010-07-18 13:58:26 +020072#if !defined(FEAT_PERL) \
73 || !defined(FEAT_PYTHON) || !defined(FEAT_PYTHON3) \
74 || !defined(FEAT_TCL) \
75 || !defined(FEAT_RUBY) \
76 || !defined(FEAT_LUA) \
77 || !defined(FEAT_MZSCHEME)
Bram Moolenaar7bb75552007-07-16 18:39:49 +000078# define HAVE_EX_SCRIPT_NI
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010079static void ex_script_ni(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +000080#endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010081static char *invalid_range(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010082static void correct_range(exarg_T *eap);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000083#ifdef FEAT_QUICKFIX
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010084static char_u *replace_makeprg(exarg_T *eap, char_u *p, char_u **cmdlinep);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000085#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010086static char_u *repl_cmdline(exarg_T *eap, char_u *src, int srclen, char_u *repl, char_u **cmdlinep);
87static void ex_highlight(exarg_T *eap);
88static void ex_colorscheme(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010089static void ex_cquit(exarg_T *eap);
90static void ex_quit_all(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010091static void ex_close(exarg_T *eap);
92static void ex_win_close(int forceit, win_T *win, tabpage_T *tp);
93static void ex_only(exarg_T *eap);
94static void ex_resize(exarg_T *eap);
95static void ex_stag(exarg_T *eap);
96static void ex_tabclose(exarg_T *eap);
97static void ex_tabonly(exarg_T *eap);
98static void ex_tabnext(exarg_T *eap);
99static void ex_tabmove(exarg_T *eap);
100static void ex_tabs(exarg_T *eap);
Bram Moolenaar4033c552017-09-16 20:54:51 +0200101#if defined(FEAT_QUICKFIX)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100102static void ex_pclose(exarg_T *eap);
103static void ex_ptag(exarg_T *eap);
104static void ex_pedit(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000105#else
106# define ex_pclose ex_ni
107# define ex_ptag ex_ni
108# define ex_pedit ex_ni
109#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100110static void ex_hide(exarg_T *eap);
111static void ex_stop(exarg_T *eap);
112static void ex_exit(exarg_T *eap);
113static void ex_print(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000114#ifdef FEAT_BYTEOFF
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100115static void ex_goto(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000116#else
117# define ex_goto ex_ni
118#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100119static void ex_shell(exarg_T *eap);
120static void ex_preserve(exarg_T *eap);
121static void ex_recover(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100122static void ex_mode(exarg_T *eap);
123static void ex_wrongmodifier(exarg_T *eap);
124static void ex_find(exarg_T *eap);
125static void ex_open(exarg_T *eap);
126static void ex_edit(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000127#ifndef FEAT_GUI
128# define ex_gui ex_nogui
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100129static void ex_nogui(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000130#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +0100131#if defined(FEAT_GUI_MSWIN) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100132static void ex_tearoff(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000133#else
134# define ex_tearoff ex_ni
135#endif
Bram Moolenaar29a2c082018-03-05 21:06:23 +0100136#if (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK) \
137 || defined(FEAT_TERM_POPUP_MENU)) && defined(FEAT_MENU)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100138static void ex_popup(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000139#else
140# define ex_popup ex_ni
141#endif
142#ifndef FEAT_GUI_MSWIN
143# define ex_simalt ex_ni
144#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +0000145#if !defined(FEAT_GUI_MSWIN) && !defined(FEAT_GUI_GTK) && !defined(FEAT_GUI_MOTIF)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000146# define gui_mch_find_dialog ex_ni
147# define gui_mch_replace_dialog ex_ni
148#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +0000149#if !defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000150# define ex_helpfind ex_ni
151#endif
152#ifndef FEAT_CSCOPE
Bram Moolenaard4db7712016-11-12 19:16:46 +0100153# define ex_cscope ex_ni
154# define ex_scscope ex_ni
155# define ex_cstag ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000156#endif
157#ifndef FEAT_SYN_HL
158# define ex_syntax ex_ni
Bram Moolenaar860cae12010-06-05 23:22:07 +0200159# define ex_ownsyntax ex_ni
Bram Moolenaarf71a3db2006-03-12 21:50:18 +0000160#endif
Bram Moolenaarf7512552013-06-06 14:55:19 +0200161#if !defined(FEAT_SYN_HL) || !defined(FEAT_PROFILE)
Bram Moolenaar8a7f5a22013-06-06 14:01:46 +0200162# define ex_syntime ex_ni
163#endif
Bram Moolenaarf71a3db2006-03-12 21:50:18 +0000164#ifndef FEAT_SPELL
Bram Moolenaarb765d632005-06-07 21:00:02 +0000165# define ex_spell ex_ni
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000166# define ex_mkspell ex_ni
Bram Moolenaarf417f2b2005-06-23 22:29:21 +0000167# define ex_spelldump ex_ni
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000168# define ex_spellinfo ex_ni
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000169# define ex_spellrepall ex_ni
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000170#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200171#ifndef FEAT_PERSISTENT_UNDO
172# define ex_rundo ex_ni
173# define ex_wundo ex_ni
174#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200175#ifndef FEAT_LUA
176# define ex_lua ex_script_ni
177# define ex_luado ex_ni
178# define ex_luafile ex_ni
179#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000180#ifndef FEAT_MZSCHEME
181# define ex_mzscheme ex_script_ni
182# define ex_mzfile ex_ni
183#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000184#ifndef FEAT_PERL
185# define ex_perl ex_script_ni
186# define ex_perldo ex_ni
187#endif
188#ifndef FEAT_PYTHON
189# define ex_python ex_script_ni
Bram Moolenaard620aa92013-05-17 16:40:06 +0200190# define ex_pydo ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000191# define ex_pyfile ex_ni
192#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200193#ifndef FEAT_PYTHON3
Bram Moolenaar368373e2010-07-19 20:46:22 +0200194# define ex_py3 ex_script_ni
Bram Moolenaar3dab2802013-05-15 18:28:13 +0200195# define ex_py3do ex_ni
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200196# define ex_py3file ex_ni
197#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +0100198#if !defined(FEAT_PYTHON) && !defined(FEAT_PYTHON3)
199# define ex_pyx ex_script_ni
200# define ex_pyxdo ex_ni
201# define ex_pyxfile ex_ni
202#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000203#ifndef FEAT_TCL
204# define ex_tcl ex_script_ni
205# define ex_tcldo ex_ni
206# define ex_tclfile ex_ni
207#endif
208#ifndef FEAT_RUBY
209# define ex_ruby ex_script_ni
210# define ex_rubydo ex_ni
211# define ex_rubyfile ex_ni
212#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000213#ifndef FEAT_KEYMAP
214# define ex_loadkeymap ex_ni
215#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100216static void ex_swapname(exarg_T *eap);
217static void ex_syncbind(exarg_T *eap);
218static void ex_read(exarg_T *eap);
219static void ex_pwd(exarg_T *eap);
220static void ex_equal(exarg_T *eap);
221static void ex_sleep(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100222static void ex_winsize(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100223static void ex_wincmd(exarg_T *eap);
Bram Moolenaar843ee412004-06-30 16:16:41 +0000224#if defined(FEAT_GUI) || defined(UNIX) || defined(VMS) || defined(MSWIN)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100225static void ex_winpos(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000226#else
227# define ex_winpos ex_ni
228#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100229static void ex_operators(exarg_T *eap);
230static void ex_put(exarg_T *eap);
231static void ex_copymove(exarg_T *eap);
232static void ex_submagic(exarg_T *eap);
233static void ex_join(exarg_T *eap);
234static void ex_at(exarg_T *eap);
235static void ex_bang(exarg_T *eap);
236static void ex_undo(exarg_T *eap);
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200237#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100238static void ex_wundo(exarg_T *eap);
239static void ex_rundo(exarg_T *eap);
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200240#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100241static void ex_redo(exarg_T *eap);
242static void ex_later(exarg_T *eap);
243static void ex_redir(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100244static void ex_redrawstatus(exarg_T *eap);
Bram Moolenaare12bab32019-01-08 22:02:56 +0100245static void ex_redrawtabline(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100246static void close_redir(void);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100247static void ex_mark(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100248static void ex_startinsert(exarg_T *eap);
249static void ex_stopinsert(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000250#ifdef FEAT_FIND_ID
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100251static void ex_checkpath(exarg_T *eap);
252static void ex_findpat(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000253#else
254# define ex_findpat ex_ni
255# define ex_checkpath ex_ni
256#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +0200257#if defined(FEAT_FIND_ID) && defined(FEAT_QUICKFIX)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100258static void ex_psearch(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000259#else
260# define ex_psearch ex_ni
261#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100262static void ex_tag(exarg_T *eap);
263static void ex_tag_cmd(exarg_T *eap, char_u *name);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000264#ifndef FEAT_EVAL
Bram Moolenaare4ce8252019-08-04 15:30:16 +0200265# define ex_break ex_ni
266# define ex_breakadd ex_ni
267# define ex_breakdel ex_ni
268# define ex_breaklist ex_ni
269# define ex_call ex_ni
270# define ex_catch ex_ni
271# define ex_compiler ex_ni
Bram Moolenaare4ce8252019-08-04 15:30:16 +0200272# define ex_continue ex_ni
273# define ex_debug ex_ni
274# define ex_debuggreedy ex_ni
Bram Moolenaar822ba242020-05-24 23:00:18 +0200275# define ex_def ex_ni
276# define ex_defcompile ex_ni
Bram Moolenaare4ce8252019-08-04 15:30:16 +0200277# define ex_delfunction ex_ni
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100278# define ex_disassemble ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000279# define ex_echo ex_ni
280# define ex_echohl ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000281# define ex_else ex_ni
Bram Moolenaare4ce8252019-08-04 15:30:16 +0200282# define ex_endfunction ex_ni
283# define ex_endif ex_ni
284# define ex_endtry ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000285# define ex_endwhile ex_ni
Bram Moolenaare4ce8252019-08-04 15:30:16 +0200286# define ex_eval ex_ni
287# define ex_execute ex_ni
288# define ex_finally ex_ni
289# define ex_finish ex_ni
290# define ex_function ex_ni
291# define ex_if ex_ni
292# define ex_let ex_ni
293# define ex_lockvar ex_ni
294# define ex_oldfiles ex_ni
295# define ex_options ex_ni
296# define ex_packadd ex_ni
297# define ex_packloadall ex_ni
298# define ex_return ex_ni
299# define ex_scriptnames ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000300# define ex_throw ex_ni
301# define ex_try ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000302# define ex_unlet ex_ni
Bram Moolenaar65c1b012005-01-31 19:02:28 +0000303# define ex_unlockvar ex_ni
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100304# define ex_vim9script ex_ni
Bram Moolenaare4ce8252019-08-04 15:30:16 +0200305# define ex_while ex_ni
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100306# define ex_import ex_ni
307# define ex_export ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000308#endif
Bram Moolenaar84538072019-07-28 14:15:42 +0200309#ifndef FEAT_SESSION
Bram Moolenaar071d4272004-06-13 20:20:40 +0000310# define ex_loadview ex_ni
311#endif
Bram Moolenaardefa0672019-07-21 19:25:37 +0200312#ifndef FEAT_VIMINFO
Bram Moolenaar071d4272004-06-13 20:20:40 +0000313# define ex_viminfo ex_ni
314#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100315static void ex_behave(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100316static void ex_filetype(exarg_T *eap);
317static void ex_setfiletype(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000318#ifndef FEAT_DIFF
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000319# define ex_diffoff ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000320# define ex_diffpatch ex_ni
321# define ex_diffgetput ex_ni
322# define ex_diffsplit ex_ni
323# define ex_diffthis ex_ni
324# define ex_diffupdate ex_ni
325#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100326static void ex_digraphs(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000327#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100328static void ex_nohlsearch(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000329#else
330# define ex_nohlsearch ex_ni
331# define ex_match ex_ni
332#endif
333#ifdef FEAT_CRYPT
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100334static void ex_X(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000335#else
336# define ex_X ex_ni
337#endif
338#ifdef FEAT_FOLDING
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100339static void ex_fold(exarg_T *eap);
340static void ex_foldopen(exarg_T *eap);
341static void ex_folddo(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000342#else
343# define ex_fold ex_ni
344# define ex_foldopen ex_ni
345# define ex_folddo ex_ni
346#endif
Bram Moolenaar13505972019-01-24 15:04:48 +0100347#if !(defined(HAVE_LOCALE_H) || defined(X_LOCALE))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000348# define ex_language ex_ni
349#endif
350#ifndef FEAT_SIGNS
351# define ex_sign ex_ni
352#endif
Bram Moolenaar009b2592004-10-24 19:18:58 +0000353#ifndef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200354# define ex_nbclose ex_ni
Bram Moolenaar009b2592004-10-24 19:18:58 +0000355# define ex_nbkey ex_ni
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200356# define ex_nbstart ex_ni
Bram Moolenaar009b2592004-10-24 19:18:58 +0000357#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000358
Bram Moolenaar071d4272004-06-13 20:20:40 +0000359#ifndef FEAT_JUMPLIST
360# define ex_jumps ex_ni
Bram Moolenaar2d358992016-06-12 21:20:54 +0200361# define ex_clearjumps ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000362# define ex_changes ex_ni
363#endif
364
Bram Moolenaar05159a02005-02-26 23:04:13 +0000365#ifndef FEAT_PROFILE
366# define ex_profile ex_ni
367#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200368#ifndef FEAT_TERMINAL
369# define ex_terminal ex_ni
370#endif
Bram Moolenaard4aa83a2019-05-09 18:59:31 +0200371#if !defined(FEAT_X11) || !defined(FEAT_XCLIPBOARD)
372# define ex_xrestore ex_ni
373#endif
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +0100374#if !defined(FEAT_PROP_POPUP)
Bram Moolenaar682725c2019-05-25 20:10:37 +0200375# define ex_popupclear ex_ni
376#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +0000377
Bram Moolenaar071d4272004-06-13 20:20:40 +0000378/*
379 * Declare cmdnames[].
380 */
381#define DO_DECLARE_EXCMD
382#include "ex_cmds.h"
Bram Moolenaar6de5e122017-04-20 21:55:44 +0200383#include "ex_cmdidxs.h"
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +0100384
Bram Moolenaar071d4272004-06-13 20:20:40 +0000385static char_u dollar_command[2] = {'$', 0};
386
387
388#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100389// Struct for storing a line inside a while/for loop
Bram Moolenaar071d4272004-06-13 20:20:40 +0000390typedef struct
391{
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100392 char_u *line; // command line
393 linenr_T lnum; // sourcing_lnum of the line
Bram Moolenaar071d4272004-06-13 20:20:40 +0000394} wcmd_T;
395
396/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000397 * Structure used to store info for line position in a while or for loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000398 * This is required, because do_one_cmd() may invoke ex_function(), which
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000399 * reads more lines that may come from the while/for loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000400 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000401struct loop_cookie
Bram Moolenaar071d4272004-06-13 20:20:40 +0000402{
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100403 garray_T *lines_gap; // growarray with line info
404 int current_line; // last read line from growarray
405 int repeating; // TRUE when looping a second time
406 // When "repeating" is FALSE use "getline" and "cookie" to get lines
Bram Moolenaare96a2492019-06-25 04:12:16 +0200407 char_u *(*getline)(int, void *, int, int);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000408 void *cookie;
409};
410
Bram Moolenaare96a2492019-06-25 04:12:16 +0200411static char_u *get_loop_line(int c, void *cookie, int indent, int do_concat);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100412static int store_loop_line(garray_T *gap, char_u *line);
413static void free_cmdlines(garray_T *gap);
Bram Moolenaared203462004-06-16 11:19:22 +0000414
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100415// Struct to save a few things while debugging. Used in do_cmdline() only.
Bram Moolenaared203462004-06-16 11:19:22 +0000416struct dbg_stuff
417{
418 int trylevel;
419 int force_abort;
420 except_T *caught_stack;
421 char_u *vv_exception;
422 char_u *vv_throwpoint;
423 int did_emsg;
424 int got_int;
425 int did_throw;
426 int need_rethrow;
427 int check_cstack;
428 except_T *current_exception;
429};
430
Bram Moolenaared203462004-06-16 11:19:22 +0000431 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100432save_dbg_stuff(struct dbg_stuff *dsp)
Bram Moolenaared203462004-06-16 11:19:22 +0000433{
434 dsp->trylevel = trylevel; trylevel = 0;
435 dsp->force_abort = force_abort; force_abort = FALSE;
436 dsp->caught_stack = caught_stack; caught_stack = NULL;
437 dsp->vv_exception = v_exception(NULL);
438 dsp->vv_throwpoint = v_throwpoint(NULL);
439
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100440 // Necessary for debugging an inactive ":catch", ":finally", ":endtry"
Bram Moolenaared203462004-06-16 11:19:22 +0000441 dsp->did_emsg = did_emsg; did_emsg = FALSE;
442 dsp->got_int = got_int; got_int = FALSE;
443 dsp->did_throw = did_throw; did_throw = FALSE;
444 dsp->need_rethrow = need_rethrow; need_rethrow = FALSE;
445 dsp->check_cstack = check_cstack; check_cstack = FALSE;
446 dsp->current_exception = current_exception; current_exception = NULL;
447}
448
449 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100450restore_dbg_stuff(struct dbg_stuff *dsp)
Bram Moolenaared203462004-06-16 11:19:22 +0000451{
452 suppress_errthrow = FALSE;
453 trylevel = dsp->trylevel;
454 force_abort = dsp->force_abort;
455 caught_stack = dsp->caught_stack;
456 (void)v_exception(dsp->vv_exception);
457 (void)v_throwpoint(dsp->vv_throwpoint);
458 did_emsg = dsp->did_emsg;
459 got_int = dsp->got_int;
460 did_throw = dsp->did_throw;
461 need_rethrow = dsp->need_rethrow;
462 check_cstack = dsp->check_cstack;
463 current_exception = dsp->current_exception;
464}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000465#endif
466
Bram Moolenaar071d4272004-06-13 20:20:40 +0000467/*
468 * do_exmode(): Repeatedly get commands for the "Ex" mode, until the ":vi"
469 * command is given.
470 */
471 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100472do_exmode(
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100473 int improved) // TRUE for "improved Ex" mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000474{
475 int save_msg_scroll;
476 int prev_msg_row;
477 linenr_T prev_line;
Bram Moolenaar79518e22017-02-17 16:31:35 +0100478 varnumber_T changedtick;
Bram Moolenaardf177f62005-02-22 08:39:57 +0000479
480 if (improved)
481 exmode_active = EXMODE_VIM;
482 else
483 exmode_active = EXMODE_NORMAL;
484 State = NORMAL;
485
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100486 // When using ":global /pat/ visual" and then "Q" we return to continue
487 // the :global command.
Bram Moolenaardf177f62005-02-22 08:39:57 +0000488 if (global_busy)
489 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000490
491 save_msg_scroll = msg_scroll;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100492 ++RedrawingDisabled; // don't redisplay the window
493 ++no_wait_return; // don't wait for return
Bram Moolenaar071d4272004-06-13 20:20:40 +0000494#ifdef FEAT_GUI
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100495 // Ignore scrollbar and mouse events in Ex mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000496 ++hold_gui_events;
497#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000498
Bram Moolenaar32526b32019-01-19 17:43:09 +0100499 msg(_("Entering Ex mode. Type \"visual\" to go to Normal mode."));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000500 while (exmode_active)
501 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100502 // Check for a ":normal" command and no more characters left.
Bram Moolenaar7c626922005-02-07 22:01:03 +0000503 if (ex_normal_busy > 0 && typebuf.tb_len == 0)
504 {
505 exmode_active = FALSE;
506 break;
507 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000508 msg_scroll = TRUE;
509 need_wait_return = FALSE;
510 ex_pressedreturn = FALSE;
511 ex_no_reprint = FALSE;
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100512 changedtick = CHANGEDTICK(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000513 prev_msg_row = msg_row;
514 prev_line = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000515 if (improved)
516 {
517 cmdline_row = msg_row;
518 do_cmdline(NULL, getexline, NULL, 0);
519 }
520 else
521 do_cmdline(NULL, getexmodeline, NULL, DOCMD_NOWAIT);
522 lines_left = Rows - 1;
523
Bram Moolenaardf177f62005-02-22 08:39:57 +0000524 if ((prev_line != curwin->w_cursor.lnum
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100525 || changedtick != CHANGEDTICK(curbuf)) && !ex_no_reprint)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000526 {
Bram Moolenaardf177f62005-02-22 08:39:57 +0000527 if (curbuf->b_ml.ml_flags & ML_EMPTY)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100528 emsg(_(e_emptybuf));
Bram Moolenaardf177f62005-02-22 08:39:57 +0000529 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000530 {
Bram Moolenaardf177f62005-02-22 08:39:57 +0000531 if (ex_pressedreturn)
532 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100533 // go up one line, to overwrite the ":<CR>" line, so the
534 // output doesn't contain empty lines.
Bram Moolenaardf177f62005-02-22 08:39:57 +0000535 msg_row = prev_msg_row;
536 if (prev_msg_row == Rows - 1)
537 msg_row--;
538 }
539 msg_col = 0;
540 print_line_no_prefix(curwin->w_cursor.lnum, FALSE, FALSE);
541 msg_clr_eos();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000542 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000543 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100544 else if (ex_pressedreturn && !ex_no_reprint) // must be at EOF
Bram Moolenaardf177f62005-02-22 08:39:57 +0000545 {
546 if (curbuf->b_ml.ml_flags & ML_EMPTY)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100547 emsg(_(e_emptybuf));
Bram Moolenaardf177f62005-02-22 08:39:57 +0000548 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100549 emsg(_("E501: At end-of-file"));
Bram Moolenaardf177f62005-02-22 08:39:57 +0000550 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000551 }
552
553#ifdef FEAT_GUI
554 --hold_gui_events;
555#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000556 --RedrawingDisabled;
557 --no_wait_return;
558 update_screen(CLEAR);
559 need_wait_return = FALSE;
560 msg_scroll = save_msg_scroll;
561}
562
563/*
Bram Moolenaar4facea32019-10-12 20:17:40 +0200564 * Print the executed command for when 'verbose' is set.
565 * When "lnum" is 0 only print the command.
566 */
567 static void
568msg_verbose_cmd(linenr_T lnum, char_u *cmd)
569{
570 ++no_wait_return;
571 verbose_enter_scroll();
572
573 if (lnum == 0)
574 smsg(_("Executing: %s"), cmd);
575 else
576 smsg(_("line %ld: %s"), (long)lnum, cmd);
577 if (msg_silent == 0)
578 msg_puts("\n"); // don't overwrite this
579
580 verbose_leave_scroll();
581 --no_wait_return;
582}
583
584/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000585 * Execute a simple command line. Used for translated commands like "*".
586 */
587 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100588do_cmdline_cmd(char_u *cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000589{
590 return do_cmdline(cmd, NULL, NULL,
591 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
592}
593
594/*
595 * do_cmdline(): execute one Ex command line
596 *
597 * 1. Execute "cmdline" when it is not NULL.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100598 * If "cmdline" is NULL, or more lines are needed, fgetline() is used.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000599 * 2. Split up in parts separated with '|'.
600 *
601 * This function can be called recursively!
602 *
603 * flags:
604 * DOCMD_VERBOSE - The command will be included in the error message.
605 * DOCMD_NOWAIT - Don't call wait_return() and friends.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100606 * DOCMD_REPEAT - Repeat execution until fgetline() returns NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000607 * DOCMD_KEYTYPED - Don't reset KeyTyped.
608 * DOCMD_EXCRESET - Reset the exception environment (used for debugging).
609 * DOCMD_KEEPLINE - Store first typed line (for repeating with ".").
610 *
611 * return FAIL if cmdline could not be executed, OK otherwise
612 */
613 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100614do_cmdline(
615 char_u *cmdline,
Bram Moolenaare96a2492019-06-25 04:12:16 +0200616 char_u *(*fgetline)(int, void *, int, int),
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100617 void *cookie, // argument for fgetline()
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100618 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000619{
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100620 char_u *next_cmdline; // next cmd to execute
621 char_u *cmdline_copy = NULL; // copy of cmd line
622 int used_getline = FALSE; // used "fgetline" to obtain command
623 static int recursive = 0; // recursive depth
Bram Moolenaar071d4272004-06-13 20:20:40 +0000624 int msg_didout_before_start = 0;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100625 int count = 0; // line number count
626 int did_inc = FALSE; // incremented RedrawingDisabled
Bram Moolenaar071d4272004-06-13 20:20:40 +0000627 int retval = OK;
628#ifdef FEAT_EVAL
Bram Moolenaarddef1292019-12-16 17:10:33 +0100629 cstack_T cstack; // conditional stack
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100630 garray_T lines_ga; // keep lines for ":while"/":for"
631 int current_line = 0; // active line in lines_ga
Bram Moolenaard5053d02020-06-28 15:51:16 +0200632 int current_line_before = 0;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100633 char_u *fname = NULL; // function or script name
634 linenr_T *breakpoint = NULL; // ptr to breakpoint field in cookie
635 int *dbg_tick = NULL; // ptr to dbg_tick field in cookie
636 struct dbg_stuff debug_saved; // saved things for debug mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000637 int initial_trylevel;
Bram Moolenaar25e0f582020-05-25 22:36:50 +0200638 msglist_T **saved_msg_list = NULL;
639 msglist_T *private_msg_list;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000640
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100641 // "fgetline" and "cookie" passed to do_one_cmd()
Bram Moolenaare96a2492019-06-25 04:12:16 +0200642 char_u *(*cmd_getline)(int, void *, int, int);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000643 void *cmd_cookie;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000644 struct loop_cookie cmd_loop_cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000645 void *real_cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +0000646 int getline_is_func;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000647#else
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100648# define cmd_getline fgetline
Bram Moolenaar071d4272004-06-13 20:20:40 +0000649# define cmd_cookie cookie
650#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100651 static int call_depth = 0; // recursiveness
Bram Moolenaar2196bce2020-04-12 20:01:11 +0200652#ifdef FEAT_EVAL
Bram Moolenaare31ee862020-01-07 20:59:34 +0100653 ESTACK_CHECK_DECLARATION
Bram Moolenaar071d4272004-06-13 20:20:40 +0000654
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100655 // For every pair of do_cmdline()/do_one_cmd() calls, use an extra memory
656 // location for storing error messages to be converted to an exception.
657 // This ensures that the do_errthrow() call in do_one_cmd() does not
658 // combine the messages stored by an earlier invocation of do_one_cmd()
659 // with the command name of the later one. This would happen when
660 // BufWritePost autocommands are executed after a write error.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000661 saved_msg_list = msg_list;
662 msg_list = &private_msg_list;
663 private_msg_list = NULL;
664#endif
665
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100666 // It's possible to create an endless loop with ":execute", catch that
667 // here. The value of 200 allows nested function calls, ":source", etc.
668 // Allow 200 or 'maxfuncdepth', whatever is larger.
Bram Moolenaarb094ff42017-01-02 16:16:39 +0100669 if (call_depth >= 200
670#ifdef FEAT_EVAL
671 && call_depth >= p_mfd
672#endif
673 )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000674 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100675 emsg(_("E169: Command too recursive"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000676#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100677 // When converting to an exception, we do not include the command name
678 // since this is not an error of the specific command.
Bram Moolenaarddef1292019-12-16 17:10:33 +0100679 do_errthrow((cstack_T *)NULL, (char_u *)NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000680 msg_list = saved_msg_list;
681#endif
682 return FAIL;
683 }
684 ++call_depth;
685
686#ifdef FEAT_EVAL
687 cstack.cs_idx = -1;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000688 cstack.cs_looplevel = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000689 cstack.cs_trylevel = 0;
690 cstack.cs_emsg_silent_list = NULL;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000691 cstack.cs_lflags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000692 ga_init2(&lines_ga, (int)sizeof(wcmd_T), 10);
693
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100694 real_cookie = getline_cookie(fgetline, cookie);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000695
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100696 // Inside a function use a higher nesting level.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100697 getline_is_func = getline_equal(fgetline, cookie, get_func_line);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000698 if (getline_is_func && ex_nesting_level == func_level(real_cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000699 ++ex_nesting_level;
700
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100701 // Get the function or script name and the address where the next breakpoint
702 // line and the debug tick for a function or script are stored.
Bram Moolenaar05159a02005-02-26 23:04:13 +0000703 if (getline_is_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000704 {
705 fname = func_name(real_cookie);
706 breakpoint = func_breakpoint(real_cookie);
707 dbg_tick = func_dbg_tick(real_cookie);
708 }
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100709 else if (getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000710 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100711 fname = SOURCING_NAME;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000712 breakpoint = source_breakpoint(real_cookie);
713 dbg_tick = source_dbg_tick(real_cookie);
714 }
715
716 /*
717 * Initialize "force_abort" and "suppress_errthrow" at the top level.
718 */
719 if (!recursive)
720 {
721 force_abort = FALSE;
722 suppress_errthrow = FALSE;
723 }
724
725 /*
726 * If requested, store and reset the global values controlling the
Bram Moolenaar89d40322006-08-29 15:30:07 +0000727 * exception handling (used when debugging). Otherwise clear it to avoid
728 * a bogus compiler warning when the optimizer uses inline functions...
Bram Moolenaar071d4272004-06-13 20:20:40 +0000729 */
Bram Moolenaarb4872942006-05-13 10:32:52 +0000730 if (flags & DOCMD_EXCRESET)
Bram Moolenaared203462004-06-16 11:19:22 +0000731 save_dbg_stuff(&debug_saved);
Bram Moolenaar89d40322006-08-29 15:30:07 +0000732 else
Bram Moolenaara80faa82020-04-12 19:37:17 +0200733 CLEAR_FIELD(debug_saved);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000734
735 initial_trylevel = trylevel;
736
737 /*
738 * "did_throw" will be set to TRUE when an exception is being thrown.
739 */
740 did_throw = FALSE;
741#endif
742 /*
743 * "did_emsg" will be set to TRUE when emsg() is used, in which case we
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000744 * cancel the whole command line, and any if/endif or loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000745 * If force_abort is set, we cancel everything.
746 */
747 did_emsg = FALSE;
748
749 /*
750 * KeyTyped is only set when calling vgetc(). Reset it here when not
751 * calling vgetc() (sourced command lines).
752 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100753 if (!(flags & DOCMD_KEYTYPED)
754 && !getline_equal(fgetline, cookie, getexline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000755 KeyTyped = FALSE;
756
757 /*
758 * Continue executing command lines:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000759 * - when inside an ":if", ":while" or ":for"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000760 * - for multiple commands on one line, separated with '|'
761 * - when repeating until there are no more lines (for ":source")
762 */
763 next_cmdline = cmdline;
764 do
765 {
Bram Moolenaar05159a02005-02-26 23:04:13 +0000766#ifdef FEAT_EVAL
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100767 getline_is_func = getline_equal(fgetline, cookie, get_func_line);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000768#endif
769
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100770 // stop skipping cmds for an error msg after all endif/while/for
Bram Moolenaar071d4272004-06-13 20:20:40 +0000771 if (next_cmdline == NULL
772#ifdef FEAT_EVAL
773 && !force_abort
774 && cstack.cs_idx < 0
Bram Moolenaar05159a02005-02-26 23:04:13 +0000775 && !(getline_is_func && func_has_abort(real_cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000776#endif
777 )
778 did_emsg = FALSE;
779
780 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000781 * 1. If repeating a line in a loop, get a line from lines_ga.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100782 * 2. If no line given: Get an allocated line with fgetline().
Bram Moolenaar071d4272004-06-13 20:20:40 +0000783 * 3. If a line is given: Make a copy, so we can mess with it.
784 */
785
786#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100787 // 1. If repeating, get a previous line from lines_ga.
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000788 if (cstack.cs_looplevel > 0 && current_line < lines_ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000789 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100790 // Each '|' separated command is stored separately in lines_ga, to
791 // be able to jump to it. Don't use next_cmdline now.
Bram Moolenaard23a8232018-02-10 18:45:26 +0100792 VIM_CLEAR(cmdline_copy);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000793
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100794 // Check if a function has returned or, unless it has an unclosed
795 // try conditional, aborted.
Bram Moolenaar05159a02005-02-26 23:04:13 +0000796 if (getline_is_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000797 {
Bram Moolenaar05159a02005-02-26 23:04:13 +0000798# ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000799 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +0000800 func_line_end(real_cookie);
801# endif
802 if (func_has_ended(real_cookie))
803 {
804 retval = FAIL;
805 break;
806 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000807 }
Bram Moolenaar05159a02005-02-26 23:04:13 +0000808#ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000809 else if (do_profiling == PROF_YES
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100810 && getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar05159a02005-02-26 23:04:13 +0000811 script_line_end();
812#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000813
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100814 // Check if a sourced file hit a ":finish" command.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100815 if (source_finished(fgetline, cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000816 {
817 retval = FAIL;
818 break;
819 }
820
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100821 // If breakpoints have been added/deleted need to check for it.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000822 if (breakpoint != NULL && dbg_tick != NULL
823 && *dbg_tick != debug_tick)
824 {
825 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100826 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100827 fname, SOURCING_LNUM);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000828 *dbg_tick = debug_tick;
829 }
830
831 next_cmdline = ((wcmd_T *)(lines_ga.ga_data))[current_line].line;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100832 SOURCING_LNUM = ((wcmd_T *)(lines_ga.ga_data))[current_line].lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000833
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100834 // Did we encounter a breakpoint?
Bram Moolenaar071d4272004-06-13 20:20:40 +0000835 if (breakpoint != NULL && *breakpoint != 0
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100836 && *breakpoint <= SOURCING_LNUM)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000837 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100838 dbg_breakpoint(fname, SOURCING_LNUM);
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100839 // Find next breakpoint.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000840 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100841 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100842 fname, SOURCING_LNUM);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000843 *dbg_tick = debug_tick;
844 }
Bram Moolenaar05159a02005-02-26 23:04:13 +0000845# ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000846 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +0000847 {
848 if (getline_is_func)
849 func_line_start(real_cookie);
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100850 else if (getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar05159a02005-02-26 23:04:13 +0000851 script_line_start();
852 }
853# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000854 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000855#endif
856
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100857 // 2. If no line given, get an allocated line with fgetline().
Bram Moolenaar071d4272004-06-13 20:20:40 +0000858 if (next_cmdline == NULL)
859 {
860 /*
861 * Need to set msg_didout for the first line after an ":if",
862 * otherwise the ":if" will be overwritten.
863 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100864 if (count == 1 && getline_equal(fgetline, cookie, getexline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000865 msg_didout = TRUE;
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100866 if (fgetline == NULL || (next_cmdline = fgetline(':', cookie,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000867#ifdef FEAT_EVAL
868 cstack.cs_idx < 0 ? 0 : (cstack.cs_idx + 1) * 2
869#else
870 0
871#endif
Bram Moolenaare96a2492019-06-25 04:12:16 +0200872 , TRUE)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000873 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100874 // Don't call wait_return for aborted command line. The NULL
875 // returned for the end of a sourced file or executed function
876 // doesn't do this.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000877 if (KeyTyped && !(flags & DOCMD_REPEAT))
878 need_wait_return = FALSE;
879 retval = FAIL;
880 break;
881 }
882 used_getline = TRUE;
883
884 /*
885 * Keep the first typed line. Clear it when more lines are typed.
886 */
887 if (flags & DOCMD_KEEPLINE)
888 {
889 vim_free(repeat_cmdline);
890 if (count == 0)
891 repeat_cmdline = vim_strsave(next_cmdline);
892 else
893 repeat_cmdline = NULL;
894 }
895 }
896
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100897 // 3. Make a copy of the command so we can mess with it.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000898 else if (cmdline_copy == NULL)
899 {
900 next_cmdline = vim_strsave(next_cmdline);
901 if (next_cmdline == NULL)
902 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100903 emsg(_(e_outofmem));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000904 retval = FAIL;
905 break;
906 }
907 }
908 cmdline_copy = next_cmdline;
909
910#ifdef FEAT_EVAL
911 /*
Bram Moolenaard5053d02020-06-28 15:51:16 +0200912 * Inside a while/for loop, and when the command looks like a ":while"
913 * or ":for", the line is stored, because we may need it later when
914 * looping.
915 *
916 * When there is a '|' and another command, it is stored separately,
917 * because we need to be able to jump back to it from an
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000918 * :endwhile/:endfor.
Bram Moolenaard5053d02020-06-28 15:51:16 +0200919 *
920 * Pass a different "fgetline" function to do_one_cmd() below,
921 * that it stores lines in or reads them from "lines_ga". Makes it
922 * possible to define a function inside a while/for loop and handles
923 * line continuation.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000924 */
Bram Moolenaard5053d02020-06-28 15:51:16 +0200925 if ((cstack.cs_looplevel > 0 || has_loop_cmd(next_cmdline)))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000926 {
Bram Moolenaard5053d02020-06-28 15:51:16 +0200927 cmd_getline = get_loop_line;
928 cmd_cookie = (void *)&cmd_loop_cookie;
929 cmd_loop_cookie.lines_gap = &lines_ga;
930 cmd_loop_cookie.current_line = current_line;
931 cmd_loop_cookie.getline = fgetline;
932 cmd_loop_cookie.cookie = cookie;
933 cmd_loop_cookie.repeating = (current_line < lines_ga.ga_len);
934
935 // Save the current line when encountering it the first time.
936 if (current_line == lines_ga.ga_len
937 && store_loop_line(&lines_ga, next_cmdline) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000938 {
939 retval = FAIL;
940 break;
941 }
Bram Moolenaard5053d02020-06-28 15:51:16 +0200942 current_line_before = current_line;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000943 }
Bram Moolenaard5053d02020-06-28 15:51:16 +0200944 else
945 {
946 cmd_getline = fgetline;
947 cmd_cookie = cookie;
948 }
949
Bram Moolenaar071d4272004-06-13 20:20:40 +0000950 did_endif = FALSE;
951#endif
952
953 if (count++ == 0)
954 {
955 /*
956 * All output from the commands is put below each other, without
957 * waiting for a return. Don't do this when executing commands
958 * from a script or when being called recursive (e.g. for ":e
959 * +command file").
960 */
961 if (!(flags & DOCMD_NOWAIT) && !recursive)
962 {
963 msg_didout_before_start = msg_didout;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100964 msg_didany = FALSE; // no output yet
Bram Moolenaar071d4272004-06-13 20:20:40 +0000965 msg_start();
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100966 msg_scroll = TRUE; // put messages below each other
967 ++no_wait_return; // don't wait for return until finished
Bram Moolenaar071d4272004-06-13 20:20:40 +0000968 ++RedrawingDisabled;
969 did_inc = TRUE;
970 }
971 }
972
Bram Moolenaar823654b2020-05-29 23:03:09 +0200973 if ((p_verbose >= 15 && SOURCING_NAME != NULL) || p_verbose >= 16)
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100974 msg_verbose_cmd(SOURCING_LNUM, cmdline_copy);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000975
976 /*
977 * 2. Execute one '|' separated command.
978 * do_one_cmd() will return NULL if there is no trailing '|'.
979 * "cmdline_copy" can change, e.g. for '%' and '#' expansion.
980 */
981 ++recursive;
982 next_cmdline = do_one_cmd(&cmdline_copy, flags & DOCMD_VERBOSE,
983#ifdef FEAT_EVAL
984 &cstack,
985#endif
986 cmd_getline, cmd_cookie);
987 --recursive;
988
989#ifdef FEAT_EVAL
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000990 if (cmd_cookie == (void *)&cmd_loop_cookie)
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100991 // Use "current_line" from "cmd_loop_cookie", it may have been
992 // incremented when defining a function.
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000993 current_line = cmd_loop_cookie.current_line;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000994#endif
995
996 if (next_cmdline == NULL)
997 {
Bram Moolenaard23a8232018-02-10 18:45:26 +0100998 VIM_CLEAR(cmdline_copy);
Bram Moolenaard7663c22019-08-06 21:59:57 +0200999
Bram Moolenaar071d4272004-06-13 20:20:40 +00001000 /*
1001 * If the command was typed, remember it for the ':' register.
1002 * Do this AFTER executing the command to make :@: work.
1003 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001004 if (getline_equal(fgetline, cookie, getexline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001005 && new_last_cmdline != NULL)
1006 {
1007 vim_free(last_cmdline);
1008 last_cmdline = new_last_cmdline;
1009 new_last_cmdline = NULL;
1010 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001011 }
1012 else
1013 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001014 // need to copy the command after the '|' to cmdline_copy, for the
1015 // next do_one_cmd()
Bram Moolenaara7241f52008-06-24 20:39:31 +00001016 STRMOVE(cmdline_copy, next_cmdline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001017 next_cmdline = cmdline_copy;
1018 }
1019
1020
1021#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001022 // reset did_emsg for a function that is not aborted by an error
Bram Moolenaar071d4272004-06-13 20:20:40 +00001023 if (did_emsg && !force_abort
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001024 && getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001025 && !func_has_abort(real_cookie))
1026 did_emsg = FALSE;
1027
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001028 if (cstack.cs_looplevel > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001029 {
1030 ++current_line;
1031
1032 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001033 * An ":endwhile", ":endfor" and ":continue" is handled here.
1034 * If we were executing commands, jump back to the ":while" or
1035 * ":for".
1036 * If we were not executing commands, decrement cs_looplevel.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001037 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001038 if (cstack.cs_lflags & (CSL_HAD_CONT | CSL_HAD_ENDLOOP))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001039 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001040 cstack.cs_lflags &= ~(CSL_HAD_CONT | CSL_HAD_ENDLOOP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001041
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001042 // Jump back to the matching ":while" or ":for". Be careful
1043 // not to use a cs_line[] from an entry that isn't a ":while"
1044 // or ":for": It would make "current_line" invalid and can
1045 // cause a crash.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001046 if (!did_emsg && !got_int && !did_throw
1047 && cstack.cs_idx >= 0
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001048 && (cstack.cs_flags[cstack.cs_idx]
1049 & (CSF_WHILE | CSF_FOR))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001050 && cstack.cs_line[cstack.cs_idx] >= 0
1051 && (cstack.cs_flags[cstack.cs_idx] & CSF_ACTIVE))
1052 {
1053 current_line = cstack.cs_line[cstack.cs_idx];
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001054 // remember we jumped there
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001055 cstack.cs_lflags |= CSL_HAD_LOOP;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001056 line_breakcheck(); // check if CTRL-C typed
Bram Moolenaar071d4272004-06-13 20:20:40 +00001057
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001058 // Check for the next breakpoint at or after the ":while"
1059 // or ":for".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001060 if (breakpoint != NULL)
1061 {
1062 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001063 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001064 fname,
1065 ((wcmd_T *)lines_ga.ga_data)[current_line].lnum-1);
1066 *dbg_tick = debug_tick;
1067 }
1068 }
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001069 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001070 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001071 // can only get here with ":endwhile" or ":endfor"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001072 if (cstack.cs_idx >= 0)
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001073 rewind_conditionals(&cstack, cstack.cs_idx - 1,
1074 CSF_WHILE | CSF_FOR, &cstack.cs_looplevel);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001075 }
1076 }
1077
1078 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001079 * For a ":while" or ":for" we need to remember the line number.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001080 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001081 else if (cstack.cs_lflags & CSL_HAD_LOOP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001082 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001083 cstack.cs_lflags &= ~CSL_HAD_LOOP;
Bram Moolenaard5053d02020-06-28 15:51:16 +02001084 cstack.cs_line[cstack.cs_idx] = current_line_before;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001085 }
1086 }
1087
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001088 // Check for the next breakpoint after a watchexpression
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01001089 if (breakpoint != NULL && has_watchexpr())
1090 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001091 *breakpoint = dbg_find_breakpoint(FALSE, fname, SOURCING_LNUM);
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01001092 *dbg_tick = debug_tick;
1093 }
1094
Bram Moolenaar071d4272004-06-13 20:20:40 +00001095 /*
1096 * When not inside any ":while" loop, clear remembered lines.
1097 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001098 if (cstack.cs_looplevel == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001099 {
1100 if (lines_ga.ga_len > 0)
1101 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001102 SOURCING_LNUM =
Bram Moolenaar071d4272004-06-13 20:20:40 +00001103 ((wcmd_T *)lines_ga.ga_data)[lines_ga.ga_len - 1].lnum;
1104 free_cmdlines(&lines_ga);
1105 }
1106 current_line = 0;
1107 }
1108
1109 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001110 * A ":finally" makes did_emsg, got_int, and did_throw pending for
1111 * being restored at the ":endtry". Reset them here and set the
1112 * ACTIVE and FINALLY flags, so that the finally clause gets executed.
1113 * This includes the case where a missing ":endif", ":endwhile" or
1114 * ":endfor" was detected by the ":finally" itself.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001115 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001116 if (cstack.cs_lflags & CSL_HAD_FINA)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001117 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001118 cstack.cs_lflags &= ~CSL_HAD_FINA;
1119 report_make_pending(cstack.cs_pending[cstack.cs_idx]
1120 & (CSTP_ERROR | CSTP_INTERRUPT | CSTP_THROW),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001121 did_throw ? (void *)current_exception : NULL);
1122 did_emsg = got_int = did_throw = FALSE;
1123 cstack.cs_flags[cstack.cs_idx] |= CSF_ACTIVE | CSF_FINALLY;
1124 }
1125
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001126 // Update global "trylevel" for recursive calls to do_cmdline() from
1127 // within this loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001128 trylevel = initial_trylevel + cstack.cs_trylevel;
1129
1130 /*
Bram Moolenaarc1762cc2007-05-10 16:56:30 +00001131 * If the outermost try conditional (across function calls and sourced
Bram Moolenaar071d4272004-06-13 20:20:40 +00001132 * files) is aborted because of an error, an interrupt, or an uncaught
1133 * exception, cancel everything. If it is left normally, reset
1134 * force_abort to get the non-EH compatible abortion behavior for
1135 * the rest of the script.
1136 */
1137 if (trylevel == 0 && !did_emsg && !got_int && !did_throw)
1138 force_abort = FALSE;
1139
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001140 // Convert an interrupt to an exception if appropriate.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001141 (void)do_intthrow(&cstack);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001142#endif // FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001143
1144 }
1145 /*
1146 * Continue executing command lines when:
1147 * - no CTRL-C typed, no aborting error, no exception thrown or try
1148 * conditionals need to be checked for executing finally clauses or
1149 * catching an interrupt exception
1150 * - didn't get an error message or lines are not typed
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001151 * - there is a command after '|', inside a :if, :while, :for or :try, or
Bram Moolenaar071d4272004-06-13 20:20:40 +00001152 * looping for ":source" command or function call.
1153 */
1154 while (!((got_int
1155#ifdef FEAT_EVAL
1156 || (did_emsg && force_abort) || did_throw
1157#endif
1158 )
1159#ifdef FEAT_EVAL
1160 && cstack.cs_trylevel == 0
1161#endif
1162 )
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001163 && !(did_emsg
1164#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001165 // Keep going when inside try/catch, so that the error can be
1166 // deal with, except when it is a syntax error, it may cause
1167 // the :endtry to be missed.
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001168 && (cstack.cs_trylevel == 0 || did_emsg_syntax)
1169#endif
1170 && used_getline
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001171 && (getline_equal(fgetline, cookie, getexmodeline)
1172 || getline_equal(fgetline, cookie, getexline)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001173 && (next_cmdline != NULL
1174#ifdef FEAT_EVAL
1175 || cstack.cs_idx >= 0
1176#endif
1177 || (flags & DOCMD_REPEAT)));
1178
1179 vim_free(cmdline_copy);
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001180 did_emsg_syntax = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001181#ifdef FEAT_EVAL
1182 free_cmdlines(&lines_ga);
1183 ga_clear(&lines_ga);
1184
1185 if (cstack.cs_idx >= 0)
1186 {
1187 /*
1188 * If a sourced file or executed function ran to its end, report the
1189 * unclosed conditional.
1190 */
1191 if (!got_int && !did_throw
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001192 && ((getline_equal(fgetline, cookie, getsourceline)
1193 && !source_finished(fgetline, cookie))
1194 || (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001195 && !func_has_ended(real_cookie))))
1196 {
1197 if (cstack.cs_flags[cstack.cs_idx] & CSF_TRY)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001198 emsg(_(e_endtry));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001199 else if (cstack.cs_flags[cstack.cs_idx] & CSF_WHILE)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001200 emsg(_(e_endwhile));
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001201 else if (cstack.cs_flags[cstack.cs_idx] & CSF_FOR)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001202 emsg(_(e_endfor));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001203 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001204 emsg(_(e_endif));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001205 }
1206
1207 /*
1208 * Reset "trylevel" in case of a ":finish" or ":return" or a missing
1209 * ":endtry" in a sourced file or executed function. If the try
1210 * conditional is in its finally clause, ignore anything pending.
1211 * If it is in a catch clause, finish the caught exception.
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001212 * Also cleanup any "cs_forinfo" structures.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001213 */
1214 do
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001215 {
1216 int idx = cleanup_conditionals(&cstack, 0, TRUE);
1217
Bram Moolenaar89e5d682005-01-17 22:06:23 +00001218 if (idx >= 0)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001219 --idx; // remove try block not in its finally clause
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001220 rewind_conditionals(&cstack, idx, CSF_WHILE | CSF_FOR,
1221 &cstack.cs_looplevel);
1222 }
1223 while (cstack.cs_idx >= 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001224 trylevel = initial_trylevel;
1225 }
1226
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001227 // If a missing ":endtry", ":endwhile", ":endfor", or ":endif" or a memory
1228 // lack was reported above and the error message is to be converted to an
1229 // exception, do this now after rewinding the cstack.
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001230 do_errthrow(&cstack, getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001231 ? (char_u *)"endfunction" : (char_u *)NULL);
1232
1233 if (trylevel == 0)
1234 {
1235 /*
1236 * When an exception is being thrown out of the outermost try
1237 * conditional, discard the uncaught exception, disable the conversion
1238 * of interrupts or errors to exceptions, and ensure that no more
1239 * commands are executed.
1240 */
1241 if (did_throw)
1242 {
1243 void *p = NULL;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02001244 msglist_T *messages = NULL, *next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001245
1246 /*
1247 * If the uncaught exception is a user exception, report it as an
1248 * error. If it is an error exception, display the saved error
1249 * message now. For an interrupt exception, do nothing; the
1250 * interrupt message is given elsewhere.
1251 */
1252 switch (current_exception->type)
1253 {
1254 case ET_USER:
Bram Moolenaar9c13b352005-05-19 20:53:52 +00001255 vim_snprintf((char *)IObuff, IOSIZE,
1256 _("E605: Exception not caught: %s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001257 current_exception->value);
1258 p = vim_strsave(IObuff);
1259 break;
1260 case ET_ERROR:
1261 messages = current_exception->messages;
1262 current_exception->messages = NULL;
1263 break;
1264 case ET_INTERRUPT:
1265 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001266 }
1267
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001268 estack_push(ETYPE_EXCEPT, current_exception->throw_name,
1269 current_exception->throw_lnum);
Bram Moolenaare31ee862020-01-07 20:59:34 +01001270 ESTACK_CHECK_SETUP
Bram Moolenaar071d4272004-06-13 20:20:40 +00001271 current_exception->throw_name = NULL;
1272
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001273 discard_current_exception(); // uses IObuff if 'verbose'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001274 suppress_errthrow = TRUE;
1275 force_abort = TRUE;
1276
1277 if (messages != NULL)
1278 {
1279 do
1280 {
1281 next = messages->next;
1282 emsg(messages->msg);
1283 vim_free(messages->msg);
Bram Moolenaar5fbf3bc2020-06-01 21:13:11 +02001284 vim_free(messages->sfile);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001285 vim_free(messages);
1286 messages = next;
1287 }
1288 while (messages != NULL);
1289 }
1290 else if (p != NULL)
1291 {
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01001292 emsg(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001293 vim_free(p);
1294 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001295 vim_free(SOURCING_NAME);
Bram Moolenaare31ee862020-01-07 20:59:34 +01001296 ESTACK_CHECK_NOW
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001297 estack_pop();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001298 }
1299
1300 /*
1301 * On an interrupt or an aborting error not converted to an exception,
1302 * disable the conversion of errors to exceptions. (Interrupts are not
Bram Moolenaar2196bce2020-04-12 20:01:11 +02001303 * converted anymore, here.) This enables also the interrupt message
Bram Moolenaar071d4272004-06-13 20:20:40 +00001304 * when force_abort is set and did_emsg unset in case of an interrupt
1305 * from a finally clause after an error.
1306 */
1307 else if (got_int || (did_emsg && force_abort))
1308 suppress_errthrow = TRUE;
1309 }
1310
1311 /*
1312 * The current cstack will be freed when do_cmdline() returns. An uncaught
1313 * exception will have to be rethrown in the previous cstack. If a function
1314 * has just returned or a script file was just finished and the previous
1315 * cstack belongs to the same function or, respectively, script file, it
1316 * will have to be checked for finally clauses to be executed due to the
1317 * ":return" or ":finish". This is done in do_one_cmd().
1318 */
1319 if (did_throw)
1320 need_rethrow = TRUE;
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001321 if ((getline_equal(fgetline, cookie, getsourceline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001322 && ex_nesting_level > source_level(real_cookie))
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001323 || (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001324 && ex_nesting_level > func_level(real_cookie) + 1))
1325 {
1326 if (!did_throw)
1327 check_cstack = TRUE;
1328 }
1329 else
1330 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001331 // When leaving a function, reduce nesting level.
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001332 if (getline_equal(fgetline, cookie, get_func_line))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001333 --ex_nesting_level;
1334 /*
1335 * Go to debug mode when returning from a function in which we are
1336 * single-stepping.
1337 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001338 if ((getline_equal(fgetline, cookie, getsourceline)
1339 || getline_equal(fgetline, cookie, get_func_line))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001340 && ex_nesting_level + 1 <= debug_break_level)
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001341 do_debug(getline_equal(fgetline, cookie, getsourceline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001342 ? (char_u *)_("End of sourced file")
1343 : (char_u *)_("End of function"));
1344 }
1345
1346 /*
1347 * Restore the exception environment (done after returning from the
1348 * debugger).
1349 */
1350 if (flags & DOCMD_EXCRESET)
Bram Moolenaared203462004-06-16 11:19:22 +00001351 restore_dbg_stuff(&debug_saved);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001352
1353 msg_list = saved_msg_list;
Bram Moolenaar292b90d2020-03-18 15:23:16 +01001354
1355 // Cleanup if "cs_emsg_silent_list" remains.
1356 if (cstack.cs_emsg_silent_list != NULL)
1357 {
1358 eslist_T *elem, *temp;
1359
1360 for (elem = cstack.cs_emsg_silent_list; elem != NULL; elem = temp)
1361 {
1362 temp = elem->next;
1363 vim_free(elem);
1364 }
1365 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001366#endif // FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001367
1368 /*
1369 * If there was too much output to fit on the command line, ask the user to
1370 * hit return before redrawing the screen. With the ":global" command we do
1371 * this only once after the command is finished.
1372 */
1373 if (did_inc)
1374 {
1375 --RedrawingDisabled;
1376 --no_wait_return;
1377 msg_scroll = FALSE;
1378
1379 /*
1380 * When just finished an ":if"-":else" which was typed, no need to
1381 * wait for hit-return. Also for an error situation.
1382 */
1383 if (retval == FAIL
1384#ifdef FEAT_EVAL
1385 || (did_endif && KeyTyped && !did_emsg)
1386#endif
1387 )
1388 {
1389 need_wait_return = FALSE;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001390 msg_didany = FALSE; // don't wait when restarting edit
Bram Moolenaar071d4272004-06-13 20:20:40 +00001391 }
1392 else if (need_wait_return)
1393 {
1394 /*
1395 * The msg_start() above clears msg_didout. The wait_return we do
1396 * here should not overwrite the command that may be shown before
1397 * doing that.
1398 */
1399 msg_didout |= msg_didout_before_start;
1400 wait_return(FALSE);
1401 }
1402 }
1403
Bram Moolenaar2a942252012-11-28 23:03:07 +01001404#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001405 did_endif = FALSE; // in case do_cmdline used recursively
Bram Moolenaar2a942252012-11-28 23:03:07 +01001406#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001407 /*
1408 * Reset if_level, in case a sourced script file contains more ":if" than
1409 * ":endif" (could be ":if x | foo | endif").
1410 */
1411 if_level = 0;
Bram Moolenaar2e18a122012-11-28 19:10:54 +01001412#endif
Bram Moolenaard4ad0d42012-11-28 17:34:48 +01001413
Bram Moolenaar071d4272004-06-13 20:20:40 +00001414 --call_depth;
1415 return retval;
1416}
1417
1418#ifdef FEAT_EVAL
1419/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001420 * Obtain a line when inside a ":while" or ":for" loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001421 */
1422 static char_u *
Bram Moolenaare96a2492019-06-25 04:12:16 +02001423get_loop_line(int c, void *cookie, int indent, int do_concat)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001424{
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001425 struct loop_cookie *cp = (struct loop_cookie *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001426 wcmd_T *wp;
1427 char_u *line;
1428
1429 if (cp->current_line + 1 >= cp->lines_gap->ga_len)
1430 {
1431 if (cp->repeating)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001432 return NULL; // trying to read past ":endwhile"/":endfor"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001433
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001434 // First time inside the ":while"/":for": get line normally.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001435 if (cp->getline == NULL)
Bram Moolenaare96a2492019-06-25 04:12:16 +02001436 line = getcmdline(c, 0L, indent, do_concat);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001437 else
Bram Moolenaare96a2492019-06-25 04:12:16 +02001438 line = cp->getline(c, cp->cookie, indent, do_concat);
Bram Moolenaard68071d2006-05-02 22:08:30 +00001439 if (line != NULL && store_loop_line(cp->lines_gap, line) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001440 ++cp->current_line;
1441
1442 return line;
1443 }
1444
1445 KeyTyped = FALSE;
1446 ++cp->current_line;
1447 wp = (wcmd_T *)(cp->lines_gap->ga_data) + cp->current_line;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001448 SOURCING_LNUM = wp->lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001449 return vim_strsave(wp->line);
1450}
1451
1452/*
1453 * Store a line in "gap" so that a ":while" loop can execute it again.
1454 */
1455 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001456store_loop_line(garray_T *gap, char_u *line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001457{
1458 if (ga_grow(gap, 1) == FAIL)
1459 return FAIL;
1460 ((wcmd_T *)(gap->ga_data))[gap->ga_len].line = vim_strsave(line);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001461 ((wcmd_T *)(gap->ga_data))[gap->ga_len].lnum = SOURCING_LNUM;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001462 ++gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001463 return OK;
1464}
1465
1466/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001467 * Free the lines stored for a ":while" or ":for" loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001468 */
1469 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001470free_cmdlines(garray_T *gap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001471{
1472 while (gap->ga_len > 0)
1473 {
1474 vim_free(((wcmd_T *)(gap->ga_data))[gap->ga_len - 1].line);
1475 --gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001476 }
1477}
1478#endif
1479
1480/*
Bram Moolenaar89d40322006-08-29 15:30:07 +00001481 * If "fgetline" is get_loop_line(), return TRUE if the getline it uses equals
1482 * "func". * Otherwise return TRUE when "fgetline" equals "func".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001483 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001484 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001485getline_equal(
Bram Moolenaare96a2492019-06-25 04:12:16 +02001486 char_u *(*fgetline)(int, void *, int, int),
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001487 void *cookie UNUSED, // argument for fgetline()
Bram Moolenaare96a2492019-06-25 04:12:16 +02001488 char_u *(*func)(int, void *, int, int))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001489{
1490#ifdef FEAT_EVAL
Bram Moolenaare96a2492019-06-25 04:12:16 +02001491 char_u *(*gp)(int, void *, int, int);
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001492 struct loop_cookie *cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001493
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001494 // When "fgetline" is "get_loop_line()" use the "cookie" to find the
1495 // function that's originally used to obtain the lines. This may be
1496 // nested several levels.
Bram Moolenaar89d40322006-08-29 15:30:07 +00001497 gp = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001498 cp = (struct loop_cookie *)cookie;
1499 while (gp == get_loop_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001500 {
1501 gp = cp->getline;
1502 cp = cp->cookie;
1503 }
1504 return gp == func;
1505#else
Bram Moolenaar89d40322006-08-29 15:30:07 +00001506 return fgetline == func;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001507#endif
1508}
1509
Bram Moolenaar071d4272004-06-13 20:20:40 +00001510/*
Bram Moolenaar89d40322006-08-29 15:30:07 +00001511 * If "fgetline" is get_loop_line(), return the cookie used by the original
Bram Moolenaar071d4272004-06-13 20:20:40 +00001512 * getline function. Otherwise return "cookie".
1513 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001514 void *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001515getline_cookie(
Bram Moolenaare96a2492019-06-25 04:12:16 +02001516 char_u *(*fgetline)(int, void *, int, int) UNUSED,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001517 void *cookie) // argument for fgetline()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001518{
Bram Moolenaar13505972019-01-24 15:04:48 +01001519#ifdef FEAT_EVAL
Bram Moolenaare96a2492019-06-25 04:12:16 +02001520 char_u *(*gp)(int, void *, int, int);
Bram Moolenaard5053d02020-06-28 15:51:16 +02001521 struct loop_cookie *cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001522
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001523 // When "fgetline" is "get_loop_line()" use the "cookie" to find the
1524 // cookie that's originally used to obtain the lines. This may be nested
1525 // several levels.
Bram Moolenaar89d40322006-08-29 15:30:07 +00001526 gp = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001527 cp = (struct loop_cookie *)cookie;
1528 while (gp == get_loop_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001529 {
1530 gp = cp->getline;
1531 cp = cp->cookie;
1532 }
1533 return cp;
Bram Moolenaar13505972019-01-24 15:04:48 +01001534#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001535 return cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001536#endif
Bram Moolenaar13505972019-01-24 15:04:48 +01001537}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001538
Bram Moolenaard5053d02020-06-28 15:51:16 +02001539#if defined(FEAT_EVAL) || defined(PROT)
1540/*
1541 * Get the next line source line without advancing.
1542 */
1543 char_u *
1544getline_peek(
1545 char_u *(*fgetline)(int, void *, int, int) UNUSED,
1546 void *cookie) // argument for fgetline()
1547{
1548 char_u *(*gp)(int, void *, int, int);
1549 struct loop_cookie *cp;
1550 wcmd_T *wp;
1551
1552 // When "fgetline" is "get_loop_line()" use the "cookie" to find the
1553 // cookie that's originally used to obtain the lines. This may be nested
1554 // several levels.
1555 gp = fgetline;
1556 cp = (struct loop_cookie *)cookie;
1557 while (gp == get_loop_line)
1558 {
1559 if (cp->current_line + 1 < cp->lines_gap->ga_len)
1560 {
1561 // executing lines a second time, use the stored copy
1562 wp = (wcmd_T *)(cp->lines_gap->ga_data) + cp->current_line + 1;
1563 return wp->line;
1564 }
1565 gp = cp->getline;
1566 cp = cp->cookie;
1567 }
1568 if (gp == getsourceline)
1569 return source_nextline(cp);
1570 return NULL;
1571}
1572#endif
1573
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001574
1575/*
1576 * Helper function to apply an offset for buffer commands, i.e. ":bdelete",
1577 * ":bwipeout", etc.
1578 * Returns the buffer number.
1579 */
1580 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001581compute_buffer_local_count(int addr_type, int lnum, int offset)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001582{
1583 buf_T *buf;
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001584 buf_T *nextbuf;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001585 int count = offset;
1586
1587 buf = firstbuf;
1588 while (buf->b_next != NULL && buf->b_fnum < lnum)
1589 buf = buf->b_next;
1590 while (count != 0)
1591 {
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001592 count += (offset < 0) ? 1 : -1;
1593 nextbuf = (offset < 0) ? buf->b_prev : buf->b_next;
1594 if (nextbuf == NULL)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001595 break;
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001596 buf = nextbuf;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001597 if (addr_type == ADDR_LOADED_BUFFERS)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001598 // skip over unloaded buffers
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001599 while (buf->b_ml.ml_mfp == NULL)
1600 {
1601 nextbuf = (offset < 0) ? buf->b_prev : buf->b_next;
1602 if (nextbuf == NULL)
1603 break;
1604 buf = nextbuf;
1605 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001606 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001607 // we might have gone too far, last buffer is not loadedd
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001608 if (addr_type == ADDR_LOADED_BUFFERS)
1609 while (buf->b_ml.ml_mfp == NULL)
1610 {
1611 nextbuf = (offset >= 0) ? buf->b_prev : buf->b_next;
1612 if (nextbuf == NULL)
1613 break;
1614 buf = nextbuf;
1615 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001616 return buf->b_fnum;
1617}
1618
Bram Moolenaarb7316892019-05-01 18:08:42 +02001619/*
1620 * Return the window number of "win".
1621 * When "win" is NULL return the number of windows.
1622 */
Bram Moolenaarf240e182014-11-27 18:33:02 +01001623 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001624current_win_nr(win_T *win)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001625{
1626 win_T *wp;
1627 int nr = 0;
1628
Bram Moolenaar29323592016-07-24 22:04:11 +02001629 FOR_ALL_WINDOWS(wp)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001630 {
1631 ++nr;
1632 if (wp == win)
1633 break;
1634 }
1635 return nr;
1636}
1637
1638 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001639current_tab_nr(tabpage_T *tab)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001640{
1641 tabpage_T *tp;
1642 int nr = 0;
1643
Bram Moolenaar29323592016-07-24 22:04:11 +02001644 FOR_ALL_TABPAGES(tp)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001645 {
1646 ++nr;
1647 if (tp == tab)
1648 break;
1649 }
1650 return nr;
1651}
1652
1653# define CURRENT_WIN_NR current_win_nr(curwin)
1654# define LAST_WIN_NR current_win_nr(NULL)
1655# define CURRENT_TAB_NR current_tab_nr(curtab)
1656# define LAST_TAB_NR current_tab_nr(NULL)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001657
Bram Moolenaar071d4272004-06-13 20:20:40 +00001658/*
1659 * Execute one Ex command.
1660 *
1661 * If 'sourcing' is TRUE, the command will be included in the error message.
1662 *
1663 * 1. skip comment lines and leading space
1664 * 2. handle command modifiers
Bram Moolenaar1c40a662014-11-27 16:38:11 +01001665 * 3. find the command
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001666 * 4. parse range
Bram Moolenaar1c40a662014-11-27 16:38:11 +01001667 * 5. Parse the command.
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001668 * 6. parse arguments
1669 * 7. switch on command name
Bram Moolenaar071d4272004-06-13 20:20:40 +00001670 *
Bram Moolenaar89d40322006-08-29 15:30:07 +00001671 * Note: "fgetline" can be NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001672 *
1673 * This function may be called recursively!
1674 */
1675#if (_MSC_VER == 1200)
1676/*
Bram Moolenaared203462004-06-16 11:19:22 +00001677 * Avoid optimisation bug in VC++ version 6.0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001678 */
Bram Moolenaar281bdce2005-01-25 21:53:18 +00001679 #pragma optimize( "g", off )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001680#endif
1681 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001682do_one_cmd(
Bram Moolenaarddef1292019-12-16 17:10:33 +01001683 char_u **cmdlinep,
1684 int sourcing,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001685#ifdef FEAT_EVAL
Bram Moolenaarddef1292019-12-16 17:10:33 +01001686 cstack_T *cstack,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001687#endif
Bram Moolenaarddef1292019-12-16 17:10:33 +01001688 char_u *(*fgetline)(int, void *, int, int),
1689 void *cookie) // argument for fgetline()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001690{
Bram Moolenaarddef1292019-12-16 17:10:33 +01001691 char_u *p;
1692 linenr_T lnum;
1693 long n;
1694 char *errormsg = NULL; // error message
1695 char_u *after_modifier = NULL;
1696 exarg_T ea; // Ex command arguments
1697 int save_msg_scroll = msg_scroll;
1698 cmdmod_T save_cmdmod;
1699 int save_reg_executing = reg_executing;
1700 int ni; // set when Not Implemented
1701 char_u *cmd;
Bram Moolenaar83f37b92020-02-23 14:35:01 +01001702#ifdef FEAT_EVAL
1703 int starts_with_colon;
1704#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001705
Bram Moolenaara80faa82020-04-12 19:37:17 +02001706 CLEAR_FIELD(ea);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001707 ea.line1 = 1;
1708 ea.line2 = 1;
1709#ifdef FEAT_EVAL
1710 ++ex_nesting_level;
1711#endif
1712
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001713 // When the last file has not been edited :q has to be typed twice.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001714 if (quitmore
1715#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001716 // avoid that a function call in 'statusline' does this
Bram Moolenaar89d40322006-08-29 15:30:07 +00001717 && !getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001718#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001719 // avoid that an autocommand, e.g. QuitPre, does this
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001720 && !getline_equal(fgetline, cookie, getnextac))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001721 --quitmore;
1722
1723 /*
1724 * Reset browse, confirm, etc.. They are restored when returning, for
1725 * recursive calls.
1726 */
1727 save_cmdmod = cmdmod;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001728
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001729 // "#!anything" is handled like a comment.
Bram Moolenaarcbb37ad2006-08-16 15:04:21 +00001730 if ((*cmdlinep)[0] == '#' && (*cmdlinep)[1] == '!')
1731 goto doend;
1732
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02001733/*
1734 * 1. Skip comment lines and leading white space and colons.
1735 * 2. Handle command modifiers.
1736 */
1737 // The "ea" structure holds the arguments that can be used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001738 ea.cmd = *cmdlinep;
Bram Moolenaareffed932018-08-14 13:38:17 +02001739 ea.cmdlinep = cmdlinep;
1740 ea.getline = fgetline;
1741 ea.cookie = cookie;
1742#ifdef FEAT_EVAL
1743 ea.cstack = cstack;
Bram Moolenaar83f37b92020-02-23 14:35:01 +01001744 starts_with_colon = *skipwhite(ea.cmd) == ':';
Bram Moolenaar071d4272004-06-13 20:20:40 +00001745#endif
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02001746 if (parse_command_modifiers(&ea, &errormsg, FALSE) == FAIL)
Bram Moolenaareffed932018-08-14 13:38:17 +02001747 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001748
Bram Moolenaar7b668e82016-08-23 23:51:21 +02001749 after_modifier = ea.cmd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001750
1751#ifdef FEAT_EVAL
1752 ea.skip = did_emsg || got_int || did_throw || (cstack->cs_idx >= 0
1753 && !(cstack->cs_flags[cstack->cs_idx] & CSF_ACTIVE));
1754#else
1755 ea.skip = (if_level > 0);
1756#endif
1757
Bram Moolenaar071d4272004-06-13 20:20:40 +00001758/*
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001759 * 3. Skip over the range to find the command. Let "p" point to after it.
1760 *
1761 * We need the command to know what kind of range it uses.
1762 */
1763 cmd = ea.cmd;
1764 ea.cmd = skip_range(ea.cmd, NULL);
1765 if (*ea.cmd == '*' && vim_strchr(p_cpo, CPO_STAR) == NULL)
1766 ea.cmd = skipwhite(ea.cmd + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001767
1768#ifdef FEAT_EVAL
Bram Moolenaar83f37b92020-02-23 14:35:01 +01001769 if (current_sctx.sc_version == SCRIPT_VERSION_VIM9 && !starts_with_colon)
Bram Moolenaardf069ee2020-06-22 23:02:51 +02001770 {
1771 if (ea.cmd > cmd)
1772 {
1773 emsg(_(e_colon_required));
1774 goto doend;
1775 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001776 p = find_ex_command(&ea, NULL, lookup_scriptvar, NULL);
Bram Moolenaardf069ee2020-06-22 23:02:51 +02001777 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001778 else
1779#endif
1780 p = find_ex_command(&ea, NULL, NULL, NULL);
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001781
Bram Moolenaar7feb35e2018-08-21 17:49:54 +02001782#ifdef FEAT_EVAL
1783# ifdef FEAT_PROFILE
1784 // Count this line for profiling if skip is TRUE.
1785 if (do_profiling == PROF_YES
1786 && (!ea.skip || cstack->cs_idx == 0 || (cstack->cs_idx > 0
1787 && (cstack->cs_flags[cstack->cs_idx - 1] & CSF_ACTIVE))))
1788 {
1789 int skip = did_emsg || got_int || did_throw;
1790
1791 if (ea.cmdidx == CMD_catch)
1792 skip = !skip && !(cstack->cs_idx >= 0
1793 && (cstack->cs_flags[cstack->cs_idx] & CSF_THROWN)
1794 && !(cstack->cs_flags[cstack->cs_idx] & CSF_CAUGHT));
1795 else if (ea.cmdidx == CMD_else || ea.cmdidx == CMD_elseif)
1796 skip = skip || !(cstack->cs_idx >= 0
1797 && !(cstack->cs_flags[cstack->cs_idx]
1798 & (CSF_ACTIVE | CSF_TRUE)));
1799 else if (ea.cmdidx == CMD_finally)
1800 skip = FALSE;
1801 else if (ea.cmdidx != CMD_endif
1802 && ea.cmdidx != CMD_endfor
1803 && ea.cmdidx != CMD_endtry
1804 && ea.cmdidx != CMD_endwhile)
1805 skip = ea.skip;
1806
1807 if (!skip)
1808 {
1809 if (getline_equal(fgetline, cookie, get_func_line))
1810 func_line_exec(getline_cookie(fgetline, cookie));
1811 else if (getline_equal(fgetline, cookie, getsourceline))
1812 script_line_exec();
1813 }
1814 }
1815# endif
1816
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001817 // May go to debug mode. If this happens and the ">quit" debug command is
1818 // used, throw an interrupt exception and skip the next command.
Bram Moolenaar7feb35e2018-08-21 17:49:54 +02001819 dbg_check_breakpoint(&ea);
1820 if (!ea.skip && got_int)
1821 {
1822 ea.skip = TRUE;
1823 (void)do_intthrow(cstack);
1824 }
1825#endif
1826
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001827/*
1828 * 4. parse a range specifier of the form: addr [,addr] [;addr] ..
Bram Moolenaar071d4272004-06-13 20:20:40 +00001829 *
1830 * where 'addr' is:
1831 *
1832 * % (entire file)
1833 * $ [+-NUM]
1834 * 'x [+-NUM] (where x denotes a currently defined mark)
1835 * . [+-NUM]
1836 * [+-NUM]..
1837 * NUM
1838 *
1839 * The ea.cmd pointer is updated to point to the first character following the
1840 * range spec. If an initial address is found, but no second, the upper bound
1841 * is equal to the lower.
1842 */
1843
Bram Moolenaar25190db2019-05-04 15:05:28 +02001844 // ea.addr_type for user commands is set by find_ucmd
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01001845 if (!IS_USER_CMDIDX(ea.cmdidx))
1846 {
1847 if (ea.cmdidx != CMD_SIZE)
1848 ea.addr_type = cmdnames[(int)ea.cmdidx].cmd_addr_type;
1849 else
1850 ea.addr_type = ADDR_LINES;
1851
Bram Moolenaar25190db2019-05-04 15:05:28 +02001852 // :wincmd range depends on the argument.
Bram Moolenaar164f3262015-01-14 21:22:01 +01001853 if (ea.cmdidx == CMD_wincmd && p != NULL)
1854 get_wincmd_addr_type(skipwhite(p), &ea);
Bram Moolenaar25190db2019-05-04 15:05:28 +02001855#ifdef FEAT_QUICKFIX
1856 // :.cc in quickfix window uses line number
1857 if ((ea.cmdidx == CMD_cc || ea.cmdidx == CMD_ll) && bt_quickfix(curbuf))
1858 ea.addr_type = ADDR_OTHER;
1859#endif
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01001860 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001861
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01001862 ea.cmd = cmd;
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02001863 if (parse_cmd_address(&ea, &errormsg, FALSE) == FAIL)
Bram Moolenaaree8415b2018-08-10 23:13:12 +02001864 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001865
Bram Moolenaar071d4272004-06-13 20:20:40 +00001866/*
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001867 * 5. Parse the command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001868 */
1869
1870 /*
1871 * Skip ':' and any white space
1872 */
1873 ea.cmd = skipwhite(ea.cmd);
1874 while (*ea.cmd == ':')
1875 ea.cmd = skipwhite(ea.cmd + 1);
1876
1877 /*
1878 * If we got a line, but no command, then go to the line.
1879 * If we find a '|' or '\n' we set ea.nextcmd.
1880 */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001881 if (*ea.cmd == NUL || *ea.cmd == '"'
Bram Moolenaar7a092242020-04-16 22:10:49 +02001882#ifdef FEAT_EVAL
Bram Moolenaara26b9702020-04-18 19:53:28 +02001883 || (*ea.cmd == '#' && ea.cmd[1] != '{'
1884 && !starts_with_colon && in_vim9script())
Bram Moolenaar7a092242020-04-16 22:10:49 +02001885#endif
1886 || (ea.nextcmd = check_nextcmd(ea.cmd)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001887 {
1888 /*
1889 * strange vi behaviour:
1890 * ":3" jumps to line 3
1891 * ":3|..." prints line 3
1892 * ":|" prints current line
1893 */
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001894 if (ea.skip) // skip this if inside :if
Bram Moolenaar071d4272004-06-13 20:20:40 +00001895 goto doend;
Bram Moolenaardf177f62005-02-22 08:39:57 +00001896 if (*ea.cmd == '|' || (exmode_active && ea.line1 != ea.line2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001897 {
1898 ea.cmdidx = CMD_print;
Bram Moolenaar8071cb22019-07-12 17:58:01 +02001899 ea.argt = EX_RANGE+EX_COUNT+EX_TRLBAR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001900 if ((errormsg = invalid_range(&ea)) == NULL)
1901 {
1902 correct_range(&ea);
1903 ex_print(&ea);
1904 }
1905 }
1906 else if (ea.addr_count != 0)
1907 {
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00001908 if (ea.line2 > curbuf->b_ml.ml_line_count)
1909 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001910 // With '-' in 'cpoptions' a line number past the file is an
1911 // error, otherwise put it at the end of the file.
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00001912 if (vim_strchr(p_cpo, CPO_MINUS) != NULL)
1913 ea.line2 = -1;
1914 else
1915 ea.line2 = curbuf->b_ml.ml_line_count;
1916 }
1917
1918 if (ea.line2 < 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001919 errormsg = _(e_invrange);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001920 else
1921 {
1922 if (ea.line2 == 0)
1923 curwin->w_cursor.lnum = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001924 else
1925 curwin->w_cursor.lnum = ea.line2;
1926 beginline(BL_SOL | BL_FIX);
1927 }
1928 }
1929 goto doend;
1930 }
1931
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001932 // If this looks like an undefined user command and there are CmdUndefined
1933 // autocommands defined, trigger the matching autocommands.
Bram Moolenaard5005162014-08-22 23:05:54 +02001934 if (p != NULL && ea.cmdidx == CMD_SIZE && !ea.skip
1935 && ASCII_ISUPPER(*ea.cmd)
1936 && has_cmdundefined())
1937 {
Bram Moolenaard5005162014-08-22 23:05:54 +02001938 int ret;
1939
Bram Moolenaar5a31b462014-08-23 14:16:20 +02001940 p = ea.cmd;
Bram Moolenaard5005162014-08-22 23:05:54 +02001941 while (ASCII_ISALNUM(*p))
1942 ++p;
Bram Moolenaar158ea172020-06-18 17:28:39 +02001943 p = vim_strnsave(ea.cmd, p - ea.cmd);
Bram Moolenaard5005162014-08-22 23:05:54 +02001944 ret = apply_autocmds(EVENT_CMDUNDEFINED, p, p, TRUE, NULL);
1945 vim_free(p);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001946 // If the autocommands did something and didn't cause an error, try
1947 // finding the command again.
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001948 p = (ret
1949#ifdef FEAT_EVAL
1950 && !aborting()
Bram Moolenaard5005162014-08-22 23:05:54 +02001951#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001952 ) ? find_ex_command(&ea, NULL, NULL, NULL) : ea.cmd;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001953 }
Bram Moolenaard5005162014-08-22 23:05:54 +02001954
Bram Moolenaar071d4272004-06-13 20:20:40 +00001955 if (p == NULL)
1956 {
1957 if (!ea.skip)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001958 errormsg = _("E464: Ambiguous use of user-defined command");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001959 goto doend;
1960 }
Bram Moolenaarac9fb182019-04-27 13:04:13 +02001961 // Check for wrong commands.
Bram Moolenaar6f9a4762017-06-22 20:39:17 +02001962 if (*p == '!' && ea.cmd[1] == 0151 && ea.cmd[0] == 78
1963 && !IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001964 {
1965 errormsg = uc_fun_cmd();
1966 goto doend;
1967 }
Bram Moolenaarac9fb182019-04-27 13:04:13 +02001968
Bram Moolenaar071d4272004-06-13 20:20:40 +00001969 if (ea.cmdidx == CMD_SIZE)
1970 {
1971 if (!ea.skip)
1972 {
1973 STRCPY(IObuff, _("E492: Not an editor command"));
1974 if (!sourcing)
Bram Moolenaar7b668e82016-08-23 23:51:21 +02001975 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001976 // If the modifier was parsed OK the error must be in the
1977 // following command
Bram Moolenaar7b668e82016-08-23 23:51:21 +02001978 if (after_modifier != NULL)
1979 append_command(after_modifier);
1980 else
1981 append_command(*cmdlinep);
1982 }
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001983 errormsg = (char *)IObuff;
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001984 did_emsg_syntax = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001985 }
1986 goto doend;
1987 }
1988
Bram Moolenaar958636c2014-10-21 20:01:58 +02001989 ni = (!IS_USER_CMDIDX(ea.cmdidx)
1990 && (cmdnames[ea.cmdidx].cmd_func == ex_ni
Bram Moolenaar7bb75552007-07-16 18:39:49 +00001991#ifdef HAVE_EX_SCRIPT_NI
1992 || cmdnames[ea.cmdidx].cmd_func == ex_script_ni
1993#endif
1994 ));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001995
1996#ifndef FEAT_EVAL
1997 /*
1998 * When the expression evaluation is disabled, recognize the ":if" and
1999 * ":endif" commands and ignore everything in between it.
2000 */
2001 if (ea.cmdidx == CMD_if)
2002 ++if_level;
2003 if (if_level)
2004 {
2005 if (ea.cmdidx == CMD_endif)
2006 --if_level;
2007 goto doend;
2008 }
2009
2010#endif
2011
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002012 // forced commands
Bram Moolenaar196b3b02008-06-20 16:51:41 +00002013 if (*p == '!' && ea.cmdidx != CMD_substitute
2014 && ea.cmdidx != CMD_smagic && ea.cmdidx != CMD_snomagic)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002015 {
2016 ++p;
2017 ea.forceit = TRUE;
2018 }
2019 else
2020 ea.forceit = FALSE;
2021
2022/*
Bram Moolenaarb7316892019-05-01 18:08:42 +02002023 * 6. Parse arguments. Then check for errors.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002024 */
Bram Moolenaar958636c2014-10-21 20:01:58 +02002025 if (!IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002026 ea.argt = (long)cmdnames[(int)ea.cmdidx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002027
2028 if (!ea.skip)
2029 {
2030#ifdef HAVE_SANDBOX
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002031 if (sandbox != 0 && !(ea.argt & EX_SBOXOK))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002032 {
Bram Moolenaar8c62a082019-02-08 14:34:10 +01002033 // Command not allowed in sandbox.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002034 errormsg = _(e_sandbox);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002035 goto doend;
2036 }
2037#endif
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002038 if (restricted != 0 && (ea.argt & EX_RESTRICT))
Bram Moolenaar8c62a082019-02-08 14:34:10 +01002039 {
2040 errormsg = _("E981: Command not allowed in rvim");
2041 goto doend;
2042 }
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002043 if (!curbuf->b_p_ma && (ea.argt & EX_MODIFY))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002044 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002045 // Command not allowed in non-'modifiable' buffer
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002046 errormsg = _(e_modifiable);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002047 goto doend;
2048 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002049
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002050 if (text_locked() && !(ea.argt & EX_CMDWIN)
Bram Moolenaar958636c2014-10-21 20:01:58 +02002051 && !IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002052 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002053 // Command not allowed when editing the command line.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002054 errormsg = _(get_text_locked_msg());
Bram Moolenaar071d4272004-06-13 20:20:40 +00002055 goto doend;
2056 }
Bram Moolenaar379fb762018-08-30 15:58:28 +02002057
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002058 // Disallow editing another buffer when "curbuf_lock" is set.
2059 // Do allow ":checktime" (it is postponed).
2060 // Do allow ":edit" (check for an argument later).
2061 // Do allow ":file" with no arguments (check for an argument later).
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002062 if (!(ea.argt & EX_CMDWIN)
Bram Moolenaar5555acc2006-04-07 21:33:12 +00002063 && ea.cmdidx != CMD_checktime
Bram Moolenaar379fb762018-08-30 15:58:28 +02002064 && ea.cmdidx != CMD_edit
2065 && ea.cmdidx != CMD_file
Bram Moolenaar958636c2014-10-21 20:01:58 +02002066 && !IS_USER_CMDIDX(ea.cmdidx)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002067 && curbuf_locked())
2068 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002069
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002070 if (!ni && !(ea.argt & EX_RANGE) && ea.addr_count > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002071 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002072 // no range allowed
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002073 errormsg = _(e_norange);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002074 goto doend;
2075 }
2076 }
2077
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002078 if (!ni && !(ea.argt & EX_BANG) && ea.forceit) // no <!> allowed
Bram Moolenaar071d4272004-06-13 20:20:40 +00002079 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002080 errormsg = _(e_nobang);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002081 goto doend;
2082 }
2083
2084 /*
2085 * Don't complain about the range if it is not used
2086 * (could happen if line_count is accidentally set to 0).
2087 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002088 if (!ea.skip && !ni && (ea.argt & EX_RANGE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002089 {
2090 /*
2091 * If the range is backwards, ask for confirmation and, if given, swap
2092 * ea.line1 & ea.line2 so it's forwards again.
2093 * When global command is busy, don't ask, will fail below.
2094 */
2095 if (!global_busy && ea.line1 > ea.line2)
2096 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002097 if (msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002098 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002099 if (sourcing || exmode_active)
2100 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002101 errormsg = _("E493: Backwards range given");
Bram Moolenaara5792f52005-11-23 21:25:05 +00002102 goto doend;
2103 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002104 if (ask_yesno((char_u *)
2105 _("Backwards range given, OK to swap"), FALSE) != 'y')
Bram Moolenaara5792f52005-11-23 21:25:05 +00002106 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002107 }
2108 lnum = ea.line1;
2109 ea.line1 = ea.line2;
2110 ea.line2 = lnum;
2111 }
2112 if ((errormsg = invalid_range(&ea)) != NULL)
2113 goto doend;
2114 }
2115
Bram Moolenaarb7316892019-05-01 18:08:42 +02002116 if ((ea.addr_type == ADDR_OTHER) && ea.addr_count == 0)
2117 // default is 1, not cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00002118 ea.line2 = 1;
2119
2120 correct_range(&ea);
2121
2122#ifdef FEAT_FOLDING
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002123 if (((ea.argt & EX_WHOLEFOLD) || ea.addr_count >= 2) && !global_busy
Bram Moolenaara3306952016-01-02 21:41:06 +01002124 && ea.addr_type == ADDR_LINES)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002125 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002126 // Put the first line at the start of a closed fold, put the last line
2127 // at the end of a closed fold.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002128 (void)hasFolding(ea.line1, &ea.line1, NULL);
2129 (void)hasFolding(ea.line2, NULL, &ea.line2);
2130 }
2131#endif
2132
2133#ifdef FEAT_QUICKFIX
2134 /*
Bram Moolenaar86b68352004-12-27 21:59:20 +00002135 * For the ":make" and ":grep" commands we insert the 'makeprg'/'grepprg'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002136 * option here, so things like % get expanded.
2137 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002138 p = replace_makeprg(&ea, p, cmdlinep);
2139 if (p == NULL)
2140 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002141#endif
2142
2143 /*
2144 * Skip to start of argument.
2145 * Don't do this for the ":!" command, because ":!! -l" needs the space.
2146 */
2147 if (ea.cmdidx == CMD_bang)
2148 ea.arg = p;
2149 else
2150 ea.arg = skipwhite(p);
2151
Bram Moolenaar379fb762018-08-30 15:58:28 +02002152 // ":file" cannot be run with an argument when "curbuf_lock" is set
2153 if (ea.cmdidx == CMD_file && *ea.arg != NUL && curbuf_locked())
2154 goto doend;
2155
Bram Moolenaar071d4272004-06-13 20:20:40 +00002156 /*
2157 * Check for "++opt=val" argument.
2158 * Must be first, allow ":w ++enc=utf8 !cmd"
2159 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002160 if (ea.argt & EX_ARGOPT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002161 while (ea.arg[0] == '+' && ea.arg[1] == '+')
2162 if (getargopt(&ea) == FAIL && !ni)
2163 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002164 errormsg = _(e_invarg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002165 goto doend;
2166 }
2167
2168 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
2169 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002170 if (*ea.arg == '>') // append
Bram Moolenaar071d4272004-06-13 20:20:40 +00002171 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002172 if (*++ea.arg != '>') // typed wrong
Bram Moolenaar071d4272004-06-13 20:20:40 +00002173 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002174 errormsg = _("E494: Use w or w>>");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002175 goto doend;
2176 }
2177 ea.arg = skipwhite(ea.arg + 1);
2178 ea.append = TRUE;
2179 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002180 else if (*ea.arg == '!' && ea.cmdidx == CMD_write) // :w !filter
Bram Moolenaar071d4272004-06-13 20:20:40 +00002181 {
2182 ++ea.arg;
2183 ea.usefilter = TRUE;
2184 }
2185 }
2186
2187 if (ea.cmdidx == CMD_read)
2188 {
2189 if (ea.forceit)
2190 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002191 ea.usefilter = TRUE; // :r! filter if ea.forceit
Bram Moolenaar071d4272004-06-13 20:20:40 +00002192 ea.forceit = FALSE;
2193 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002194 else if (*ea.arg == '!') // :r !filter
Bram Moolenaar071d4272004-06-13 20:20:40 +00002195 {
2196 ++ea.arg;
2197 ea.usefilter = TRUE;
2198 }
2199 }
2200
2201 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
2202 {
2203 ea.amount = 1;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002204 while (*ea.arg == *ea.cmd) // count number of '>' or '<'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002205 {
2206 ++ea.arg;
2207 ++ea.amount;
2208 }
2209 ea.arg = skipwhite(ea.arg);
2210 }
2211
2212 /*
2213 * Check for "+command" argument, before checking for next command.
2214 * Don't do this for ":read !cmd" and ":write !cmd".
2215 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002216 if ((ea.argt & EX_CMDARG) && !ea.usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002217 ea.do_ecmd_cmd = getargcmd(&ea.arg);
2218
2219 /*
2220 * Check for '|' to separate commands and '"' to start comments.
2221 * Don't do this for ":read !cmd" and ":write !cmd".
2222 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002223 if ((ea.argt & EX_TRLBAR) && !ea.usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002224 separate_nextcmd(&ea);
2225
2226 /*
2227 * Check for <newline> to end a shell command.
Bram Moolenaardf177f62005-02-22 08:39:57 +00002228 * Also do this for ":read !cmd", ":write !cmd" and ":global".
2229 * Any others?
Bram Moolenaar071d4272004-06-13 20:20:40 +00002230 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00002231 else if (ea.cmdidx == CMD_bang
Bram Moolenaar67883b42017-07-27 22:57:00 +02002232 || ea.cmdidx == CMD_terminal
Bram Moolenaardf177f62005-02-22 08:39:57 +00002233 || ea.cmdidx == CMD_global
2234 || ea.cmdidx == CMD_vglobal
2235 || ea.usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002236 {
2237 for (p = ea.arg; *p; ++p)
2238 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002239 // Remove one backslash before a newline, so that it's possible to
2240 // pass a newline to the shell and also a newline that is preceded
2241 // with a backslash. This makes it impossible to end a shell
2242 // command in a backslash, but that doesn't appear useful.
2243 // Halving the number of backslashes is incompatible with previous
2244 // versions.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002245 if (*p == '\\' && p[1] == '\n')
Bram Moolenaara7241f52008-06-24 20:39:31 +00002246 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002247 else if (*p == '\n')
2248 {
2249 ea.nextcmd = p + 1;
2250 *p = NUL;
2251 break;
2252 }
2253 }
2254 }
2255
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002256 if ((ea.argt & EX_DFLALL) && ea.addr_count == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002257 {
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002258 buf_T *buf;
2259
Bram Moolenaar071d4272004-06-13 20:20:40 +00002260 ea.line1 = 1;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002261 switch (ea.addr_type)
2262 {
2263 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02002264 case ADDR_OTHER:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002265 ea.line2 = curbuf->b_ml.ml_line_count;
2266 break;
2267 case ADDR_LOADED_BUFFERS:
2268 buf = firstbuf;
2269 while (buf->b_next != NULL && buf->b_ml.ml_mfp == NULL)
2270 buf = buf->b_next;
2271 ea.line1 = buf->b_fnum;
2272 buf = lastbuf;
2273 while (buf->b_prev != NULL && buf->b_ml.ml_mfp == NULL)
2274 buf = buf->b_prev;
2275 ea.line2 = buf->b_fnum;
2276 break;
2277 case ADDR_BUFFERS:
2278 ea.line1 = firstbuf->b_fnum;
2279 ea.line2 = lastbuf->b_fnum;
2280 break;
2281 case ADDR_WINDOWS:
2282 ea.line2 = LAST_WIN_NR;
2283 break;
2284 case ADDR_TABS:
2285 ea.line2 = LAST_TAB_NR;
2286 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01002287 case ADDR_TABS_RELATIVE:
2288 ea.line2 = 1;
2289 break;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002290 case ADDR_ARGUMENTS:
2291 if (ARGCOUNT == 0)
2292 ea.line1 = ea.line2 = 0;
2293 else
2294 ea.line2 = ARGCOUNT;
2295 break;
Bram Moolenaar25190db2019-05-04 15:05:28 +02002296 case ADDR_QUICKFIX_VALID:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02002297#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02002298 ea.line2 = qf_get_valid_size(&ea);
Bram Moolenaaraa23b372015-09-08 18:46:31 +02002299 if (ea.line2 == 0)
2300 ea.line2 = 1;
Bram Moolenaare906c502015-09-09 21:10:39 +02002301#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02002302 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02002303 case ADDR_NONE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02002304 case ADDR_UNSIGNED:
2305 case ADDR_QUICKFIX:
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002306 iemsg(_("INTERNAL: Cannot use EX_DFLALL with ADDR_NONE, ADDR_UNSIGNED or ADDR_QUICKFIX"));
Bram Moolenaarb7316892019-05-01 18:08:42 +02002307 break;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002308 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002309 }
2310
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002311 // accept numbered register only when no count allowed (:put)
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002312 if ( (ea.argt & EX_REGSTR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002313 && *ea.arg != NUL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002314 // Do not allow register = for user commands
Bram Moolenaar958636c2014-10-21 20:01:58 +02002315 && (!IS_USER_CMDIDX(ea.cmdidx) || *ea.arg != '=')
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002316 && !((ea.argt & EX_COUNT) && VIM_ISDIGIT(*ea.arg)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002317 {
Bram Moolenaar85de2062011-05-05 14:26:41 +02002318#ifndef FEAT_CLIPBOARD
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002319 // check these explicitly for a more specific error message
Bram Moolenaar85de2062011-05-05 14:26:41 +02002320 if (*ea.arg == '*' || *ea.arg == '+')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002321 {
Bram Moolenaar99b12722019-01-14 20:16:40 +01002322 errormsg = _(e_invalidreg);
Bram Moolenaar85de2062011-05-05 14:26:41 +02002323 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002324 }
2325#endif
Bram Moolenaar958636c2014-10-21 20:01:58 +02002326 if (valid_yank_reg(*ea.arg, (ea.cmdidx != CMD_put
2327 && !IS_USER_CMDIDX(ea.cmdidx))))
Bram Moolenaar85de2062011-05-05 14:26:41 +02002328 {
2329 ea.regname = *ea.arg++;
2330#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002331 // for '=' register: accept the rest of the line as an expression
Bram Moolenaar85de2062011-05-05 14:26:41 +02002332 if (ea.arg[-1] == '=' && ea.arg[0] != NUL)
2333 {
2334 set_expr_line(vim_strsave(ea.arg));
2335 ea.arg += STRLEN(ea.arg);
2336 }
2337#endif
2338 ea.arg = skipwhite(ea.arg);
2339 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002340 }
2341
2342 /*
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002343 * Check for a count. When accepting a EX_BUFNAME, don't use "123foo" as a
Bram Moolenaar071d4272004-06-13 20:20:40 +00002344 * count, it's a buffer name.
2345 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002346 if ((ea.argt & EX_COUNT) && VIM_ISDIGIT(*ea.arg)
2347 && (!(ea.argt & EX_BUFNAME) || *(p = skipdigits(ea.arg)) == NUL
Bram Moolenaar1c465442017-03-12 20:10:05 +01002348 || VIM_ISWHITE(*p)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002349 {
2350 n = getdigits(&ea.arg);
2351 ea.arg = skipwhite(ea.arg);
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002352 if (n <= 0 && !ni && (ea.argt & EX_ZEROR) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002353 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002354 errormsg = _(e_zerocount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002355 goto doend;
2356 }
Bram Moolenaarb7316892019-05-01 18:08:42 +02002357 if (ea.addr_type != ADDR_LINES) // e.g. :buffer 2, :sleep 3
Bram Moolenaar071d4272004-06-13 20:20:40 +00002358 {
2359 ea.line2 = n;
2360 if (ea.addr_count == 0)
2361 ea.addr_count = 1;
2362 }
2363 else
2364 {
2365 ea.line1 = ea.line2;
2366 ea.line2 += n - 1;
2367 ++ea.addr_count;
2368 /*
2369 * Be vi compatible: no error message for out of range.
2370 */
Bram Moolenaarb7316892019-05-01 18:08:42 +02002371 if (ea.line2 > curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002372 ea.line2 = curbuf->b_ml.ml_line_count;
2373 }
2374 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00002375
2376 /*
2377 * Check for flags: 'l', 'p' and '#'.
2378 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002379 if (ea.argt & EX_FLAGS)
Bram Moolenaardf177f62005-02-22 08:39:57 +00002380 get_flags(&ea);
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002381 if (!ni && !(ea.argt & EX_EXTRA) && *ea.arg != NUL
2382 && *ea.arg != '"' && (*ea.arg != '|' || (ea.argt & EX_TRLBAR) == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002383 {
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002384 // no arguments allowed but there is something
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002385 errormsg = _(e_trailing);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002386 goto doend;
2387 }
2388
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002389 if (!ni && (ea.argt & EX_NEEDARG) && *ea.arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002390 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002391 errormsg = _(e_argreq);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002392 goto doend;
2393 }
2394
2395#ifdef FEAT_EVAL
2396 /*
2397 * Skip the command when it's not going to be executed.
2398 * The commands like :if, :endif, etc. always need to be executed.
2399 * Also make an exception for commands that handle a trailing command
2400 * themselves.
2401 */
2402 if (ea.skip)
2403 {
2404 switch (ea.cmdidx)
2405 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002406 // commands that need evaluation
Bram Moolenaar071d4272004-06-13 20:20:40 +00002407 case CMD_while:
2408 case CMD_endwhile:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00002409 case CMD_for:
2410 case CMD_endfor:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002411 case CMD_if:
2412 case CMD_elseif:
2413 case CMD_else:
2414 case CMD_endif:
2415 case CMD_try:
2416 case CMD_catch:
2417 case CMD_finally:
2418 case CMD_endtry:
2419 case CMD_function:
Bram Moolenaarab55c682020-03-01 19:41:43 +01002420 case CMD_def:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002421 break;
2422
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002423 // Commands that handle '|' themselves. Check: A command should
2424 // either have the EX_TRLBAR flag, appear in this list or appear in
2425 // the list at ":help :bar".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002426 case CMD_aboveleft:
2427 case CMD_and:
2428 case CMD_belowright:
2429 case CMD_botright:
2430 case CMD_browse:
2431 case CMD_call:
2432 case CMD_confirm:
Bram Moolenaar8f76e6b2019-11-26 16:50:30 +01002433 case CMD_const:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002434 case CMD_delfunction:
2435 case CMD_djump:
2436 case CMD_dlist:
2437 case CMD_dsearch:
2438 case CMD_dsplit:
2439 case CMD_echo:
2440 case CMD_echoerr:
2441 case CMD_echomsg:
2442 case CMD_echon:
Bram Moolenaara76b3152020-02-16 16:20:21 +01002443 case CMD_eval:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002444 case CMD_execute:
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002445 case CMD_filter:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002446 case CMD_help:
2447 case CMD_hide:
2448 case CMD_ijump:
2449 case CMD_ilist:
2450 case CMD_isearch:
2451 case CMD_isplit:
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002452 case CMD_keepalt:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002453 case CMD_keepjumps:
2454 case CMD_keepmarks:
Bram Moolenaara939e432013-11-09 05:30:26 +01002455 case CMD_keeppatterns:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002456 case CMD_leftabove:
2457 case CMD_let:
2458 case CMD_lockmarks:
Bram Moolenaarcc4423a2019-11-26 17:05:00 +01002459 case CMD_lockvar:
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002460 case CMD_lua:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002461 case CMD_match:
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002462 case CMD_mzscheme:
Bram Moolenaar5803ae62014-03-23 16:04:02 +01002463 case CMD_noautocmd:
2464 case CMD_noswapfile:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002465 case CMD_perl:
2466 case CMD_psearch:
2467 case CMD_python:
Bram Moolenaar368373e2010-07-19 20:46:22 +02002468 case CMD_py3:
Bram Moolenaarb6590522010-07-21 16:00:43 +02002469 case CMD_python3:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002470 case CMD_return:
2471 case CMD_rightbelow:
2472 case CMD_ruby:
2473 case CMD_silent:
2474 case CMD_smagic:
2475 case CMD_snomagic:
2476 case CMD_substitute:
2477 case CMD_syntax:
Bram Moolenaara226a6d2006-02-26 23:59:20 +00002478 case CMD_tab:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002479 case CMD_tcl:
2480 case CMD_throw:
2481 case CMD_tilde:
2482 case CMD_topleft:
2483 case CMD_unlet:
Bram Moolenaarcc4423a2019-11-26 17:05:00 +01002484 case CMD_unlockvar:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002485 case CMD_verbose:
2486 case CMD_vertical:
Bram Moolenaar12bde492011-06-13 01:19:56 +02002487 case CMD_wincmd:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002488 break;
2489
2490 default: goto doend;
2491 }
2492 }
2493#endif
2494
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002495 if (ea.argt & EX_XFILE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002496 {
2497 if (expand_filename(&ea, cmdlinep, &errormsg) == FAIL)
2498 goto doend;
2499 }
2500
Bram Moolenaar071d4272004-06-13 20:20:40 +00002501 /*
2502 * Accept buffer name. Cannot be used at the same time with a buffer
2503 * number. Don't do this for a user command.
2504 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002505 if ((ea.argt & EX_BUFNAME) && *ea.arg != NUL && ea.addr_count == 0
Bram Moolenaar958636c2014-10-21 20:01:58 +02002506 && !IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002507 {
2508 /*
2509 * :bdelete, :bwipeout and :bunload take several arguments, separated
2510 * by spaces: find next space (skipping over escaped characters).
2511 * The others take one argument: ignore trailing spaces.
2512 */
2513 if (ea.cmdidx == CMD_bdelete || ea.cmdidx == CMD_bwipeout
2514 || ea.cmdidx == CMD_bunload)
2515 p = skiptowhite_esc(ea.arg);
2516 else
2517 {
2518 p = ea.arg + STRLEN(ea.arg);
Bram Moolenaar1c465442017-03-12 20:10:05 +01002519 while (p > ea.arg && VIM_ISWHITE(p[-1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002520 --p;
2521 }
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002522 ea.line2 = buflist_findpat(ea.arg, p, (ea.argt & EX_BUFUNL) != 0,
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002523 FALSE, FALSE);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002524 if (ea.line2 < 0) // failed
Bram Moolenaar071d4272004-06-13 20:20:40 +00002525 goto doend;
2526 ea.addr_count = 1;
2527 ea.arg = skipwhite(p);
2528 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002529
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002530 // The :try command saves the emsg_silent flag, reset it here when
2531 // ":silent! try" was used, it should only apply to :try itself.
Bram Moolenaareffed932018-08-14 13:38:17 +02002532 if (ea.cmdidx == CMD_try && ea.did_esilent > 0)
Bram Moolenaar2be57332018-02-13 18:05:18 +01002533 {
Bram Moolenaareffed932018-08-14 13:38:17 +02002534 emsg_silent -= ea.did_esilent;
Bram Moolenaar2be57332018-02-13 18:05:18 +01002535 if (emsg_silent < 0)
2536 emsg_silent = 0;
Bram Moolenaareffed932018-08-14 13:38:17 +02002537 ea.did_esilent = 0;
Bram Moolenaar2be57332018-02-13 18:05:18 +01002538 }
2539
Bram Moolenaar071d4272004-06-13 20:20:40 +00002540/*
Bram Moolenaar2be57332018-02-13 18:05:18 +01002541 * 7. Execute the command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002542 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002543
Bram Moolenaar958636c2014-10-21 20:01:58 +02002544 if (IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002545 {
2546 /*
2547 * Execute a user-defined command.
2548 */
2549 do_ucmd(&ea);
2550 }
2551 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002552 {
2553 /*
Bram Moolenaarac9fb182019-04-27 13:04:13 +02002554 * Call the function to execute the builtin command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002555 */
2556 ea.errmsg = NULL;
2557 (cmdnames[ea.cmdidx].cmd_func)(&ea);
2558 if (ea.errmsg != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002559 errormsg = _(ea.errmsg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002560 }
2561
2562#ifdef FEAT_EVAL
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002563 // Set flag that any command was executed, used by ex_vim9script().
Bram Moolenaar16531552020-02-08 16:00:46 +01002564 if (getline_equal(ea.getline, ea.cookie, getsourceline)
2565 && current_sctx.sc_sid > 0)
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002566 SCRIPT_ITEM(current_sctx.sc_sid)->sn_had_command = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002567
Bram Moolenaar071d4272004-06-13 20:20:40 +00002568 /*
2569 * If the command just executed called do_cmdline(), any throw or ":return"
2570 * or ":finish" encountered there must also check the cstack of the still
2571 * active do_cmdline() that called this do_one_cmd(). Rethrow an uncaught
2572 * exception, or reanimate a returned function or finished script file and
2573 * return or finish it again.
2574 */
2575 if (need_rethrow)
2576 do_throw(cstack);
2577 else if (check_cstack)
2578 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002579 if (source_finished(fgetline, cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002580 do_finish(&ea, TRUE);
Bram Moolenaar89d40322006-08-29 15:30:07 +00002581 else if (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002582 && current_func_returned())
2583 do_return(&ea, TRUE, FALSE, NULL);
2584 }
2585 need_rethrow = check_cstack = FALSE;
2586#endif
2587
2588doend:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002589 if (curwin->w_cursor.lnum == 0) // can happen with zero line number
Bram Moolenaar6de5e122017-04-20 21:55:44 +02002590 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002591 curwin->w_cursor.lnum = 1;
Bram Moolenaar6de5e122017-04-20 21:55:44 +02002592 curwin->w_cursor.col = 0;
2593 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002594
2595 if (errormsg != NULL && *errormsg != NUL && !did_emsg)
2596 {
2597 if (sourcing)
2598 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002599 if (errormsg != (char *)IObuff)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002600 {
2601 STRCPY(IObuff, errormsg);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002602 errormsg = (char *)IObuff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002603 }
Bram Moolenaara6f4d612011-09-21 19:10:46 +02002604 append_command(*cmdlinep);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002605 }
2606 emsg(errormsg);
2607 }
2608#ifdef FEAT_EVAL
2609 do_errthrow(cstack,
Bram Moolenaar958636c2014-10-21 20:01:58 +02002610 (ea.cmdidx != CMD_SIZE && !IS_USER_CMDIDX(ea.cmdidx))
2611 ? cmdnames[(int)ea.cmdidx].cmd_name : (char_u *)NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002612#endif
2613
Bram Moolenaareffed932018-08-14 13:38:17 +02002614 if (ea.verbose_save >= 0)
2615 p_verbose = ea.verbose_save;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002616
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002617 free_cmdmod();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002618 cmdmod = save_cmdmod;
Bram Moolenaar9a2c0912019-03-30 14:26:18 +01002619 reg_executing = save_reg_executing;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002620
Bram Moolenaareffed932018-08-14 13:38:17 +02002621 if (ea.save_msg_silent != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002622 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002623 // messages could be enabled for a serious error, need to check if the
2624 // counters don't become negative
Bram Moolenaareffed932018-08-14 13:38:17 +02002625 if (!did_emsg || msg_silent > ea.save_msg_silent)
2626 msg_silent = ea.save_msg_silent;
2627 emsg_silent -= ea.did_esilent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002628 if (emsg_silent < 0)
2629 emsg_silent = 0;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002630 // Restore msg_scroll, it's set by file I/O commands, even when no
2631 // message is actually displayed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002632 msg_scroll = save_msg_scroll;
Bram Moolenaar77ab2802009-04-22 12:44:48 +00002633
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002634 // "silent reg" or "silent echo x" inside "redir" leaves msg_col
2635 // somewhere in the line. Put it back in the first column.
Bram Moolenaar77ab2802009-04-22 12:44:48 +00002636 if (redirecting())
2637 msg_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002638 }
2639
Bram Moolenaar7171abe2004-10-11 10:06:20 +00002640#ifdef HAVE_SANDBOX
Bram Moolenaareffed932018-08-14 13:38:17 +02002641 if (ea.did_sandbox)
Bram Moolenaar7171abe2004-10-11 10:06:20 +00002642 --sandbox;
2643#endif
2644
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002645 if (ea.nextcmd && *ea.nextcmd == NUL) // not really a next command
Bram Moolenaar071d4272004-06-13 20:20:40 +00002646 ea.nextcmd = NULL;
2647
2648#ifdef FEAT_EVAL
2649 --ex_nesting_level;
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002650 vim_free(ea.cmdline_tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002651#endif
2652
2653 return ea.nextcmd;
2654}
2655#if (_MSC_VER == 1200)
Bram Moolenaar281bdce2005-01-25 21:53:18 +00002656 #pragma optimize( "", on )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002657#endif
2658
2659/*
Bram Moolenaareffed932018-08-14 13:38:17 +02002660 * Parse and skip over command modifiers:
2661 * - update eap->cmd
2662 * - store flags in "cmdmod".
2663 * - Set ex_pressedreturn for an empty command line.
2664 * - set msg_silent for ":silent"
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002665 * - set 'eventignore' to "all" for ":noautocmd"
Bram Moolenaareffed932018-08-14 13:38:17 +02002666 * - set p_verbose for ":verbose"
2667 * - Increment "sandbox" for ":sandbox"
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002668 * When "skip_only" is TRUE the global variables are not changed, except for
2669 * "cmdmod".
Bram Moolenaareffed932018-08-14 13:38:17 +02002670 * Return FAIL when the command is not to be executed.
2671 * May set "errormsg" to an error message.
2672 */
2673 int
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002674parse_command_modifiers(exarg_T *eap, char **errormsg, int skip_only)
Bram Moolenaareffed932018-08-14 13:38:17 +02002675{
2676 char_u *p;
2677
Bram Moolenaara80faa82020-04-12 19:37:17 +02002678 CLEAR_FIELD(cmdmod);
Bram Moolenaareffed932018-08-14 13:38:17 +02002679 eap->verbose_save = -1;
2680 eap->save_msg_silent = -1;
2681
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002682 // Repeat until no more command modifiers are found.
Bram Moolenaareffed932018-08-14 13:38:17 +02002683 for (;;)
2684 {
Bram Moolenaareffed932018-08-14 13:38:17 +02002685 while (*eap->cmd == ' ' || *eap->cmd == '\t' || *eap->cmd == ':')
2686 ++eap->cmd;
2687
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002688 // in ex mode, an empty line works like :+
Bram Moolenaareffed932018-08-14 13:38:17 +02002689 if (*eap->cmd == NUL && exmode_active
2690 && (getline_equal(eap->getline, eap->cookie, getexmodeline)
2691 || getline_equal(eap->getline, eap->cookie, getexline))
2692 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
2693 {
2694 eap->cmd = (char_u *)"+";
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002695 if (!skip_only)
2696 ex_pressedreturn = TRUE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002697 }
2698
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002699 // ignore comment and empty lines
Bram Moolenaareffed932018-08-14 13:38:17 +02002700 if (*eap->cmd == '"')
2701 return FAIL;
2702 if (*eap->cmd == NUL)
2703 {
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002704 if (!skip_only)
2705 ex_pressedreturn = TRUE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002706 return FAIL;
2707 }
2708
Bram Moolenaareffed932018-08-14 13:38:17 +02002709 p = skip_range(eap->cmd, NULL);
2710 switch (*p)
2711 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002712 // When adding an entry, also modify cmd_exists().
Bram Moolenaareffed932018-08-14 13:38:17 +02002713 case 'a': if (!checkforcmd(&eap->cmd, "aboveleft", 3))
2714 break;
2715 cmdmod.split |= WSP_ABOVE;
2716 continue;
2717
2718 case 'b': if (checkforcmd(&eap->cmd, "belowright", 3))
2719 {
2720 cmdmod.split |= WSP_BELOW;
2721 continue;
2722 }
2723 if (checkforcmd(&eap->cmd, "browse", 3))
2724 {
2725#ifdef FEAT_BROWSE_CMD
2726 cmdmod.browse = TRUE;
2727#endif
2728 continue;
2729 }
2730 if (!checkforcmd(&eap->cmd, "botright", 2))
2731 break;
2732 cmdmod.split |= WSP_BOT;
2733 continue;
2734
2735 case 'c': if (!checkforcmd(&eap->cmd, "confirm", 4))
2736 break;
2737#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
2738 cmdmod.confirm = TRUE;
2739#endif
2740 continue;
2741
2742 case 'k': if (checkforcmd(&eap->cmd, "keepmarks", 3))
2743 {
2744 cmdmod.keepmarks = TRUE;
2745 continue;
2746 }
2747 if (checkforcmd(&eap->cmd, "keepalt", 5))
2748 {
2749 cmdmod.keepalt = TRUE;
2750 continue;
2751 }
2752 if (checkforcmd(&eap->cmd, "keeppatterns", 5))
2753 {
2754 cmdmod.keeppatterns = TRUE;
2755 continue;
2756 }
2757 if (!checkforcmd(&eap->cmd, "keepjumps", 5))
2758 break;
2759 cmdmod.keepjumps = TRUE;
2760 continue;
2761
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002762 case 'f': // only accept ":filter {pat} cmd"
Bram Moolenaareffed932018-08-14 13:38:17 +02002763 {
2764 char_u *reg_pat;
2765
2766 if (!checkforcmd(&p, "filter", 4)
2767 || *p == NUL || ends_excmd(*p))
2768 break;
2769 if (*p == '!')
2770 {
2771 cmdmod.filter_force = TRUE;
2772 p = skipwhite(p + 1);
2773 if (*p == NUL || ends_excmd(*p))
2774 break;
2775 }
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002776 if (skip_only)
2777 p = skip_vimgrep_pat(p, NULL, NULL);
2778 else
2779 // NOTE: This puts a NUL after the pattern.
2780 p = skip_vimgrep_pat(p, &reg_pat, NULL);
Bram Moolenaareffed932018-08-14 13:38:17 +02002781 if (p == NULL || *p == NUL)
2782 break;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002783 if (!skip_only)
2784 {
2785 cmdmod.filter_regmatch.regprog =
Bram Moolenaareffed932018-08-14 13:38:17 +02002786 vim_regcomp(reg_pat, RE_MAGIC);
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002787 if (cmdmod.filter_regmatch.regprog == NULL)
2788 break;
2789 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002790 eap->cmd = p;
2791 continue;
2792 }
2793
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002794 // ":hide" and ":hide | cmd" are not modifiers
Bram Moolenaareffed932018-08-14 13:38:17 +02002795 case 'h': if (p != eap->cmd || !checkforcmd(&p, "hide", 3)
2796 || *p == NUL || ends_excmd(*p))
2797 break;
2798 eap->cmd = p;
2799 cmdmod.hide = TRUE;
2800 continue;
2801
2802 case 'l': if (checkforcmd(&eap->cmd, "lockmarks", 3))
2803 {
2804 cmdmod.lockmarks = TRUE;
2805 continue;
2806 }
2807
2808 if (!checkforcmd(&eap->cmd, "leftabove", 5))
2809 break;
2810 cmdmod.split |= WSP_ABOVE;
2811 continue;
2812
2813 case 'n': if (checkforcmd(&eap->cmd, "noautocmd", 3))
2814 {
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002815 if (cmdmod.save_ei == NULL && !skip_only)
Bram Moolenaareffed932018-08-14 13:38:17 +02002816 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002817 // Set 'eventignore' to "all". Restore the
2818 // existing option value later.
Bram Moolenaareffed932018-08-14 13:38:17 +02002819 cmdmod.save_ei = vim_strsave(p_ei);
2820 set_string_option_direct((char_u *)"ei", -1,
2821 (char_u *)"all", OPT_FREE, SID_NONE);
2822 }
2823 continue;
2824 }
2825 if (!checkforcmd(&eap->cmd, "noswapfile", 3))
2826 break;
2827 cmdmod.noswapfile = TRUE;
2828 continue;
2829
2830 case 'r': if (!checkforcmd(&eap->cmd, "rightbelow", 6))
2831 break;
2832 cmdmod.split |= WSP_BELOW;
2833 continue;
2834
2835 case 's': if (checkforcmd(&eap->cmd, "sandbox", 3))
2836 {
2837#ifdef HAVE_SANDBOX
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002838 if (!skip_only)
2839 {
2840 if (!eap->did_sandbox)
2841 ++sandbox;
2842 eap->did_sandbox = TRUE;
2843 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002844#endif
2845 continue;
2846 }
2847 if (!checkforcmd(&eap->cmd, "silent", 3))
2848 break;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002849 if (!skip_only)
2850 {
2851 if (eap->save_msg_silent == -1)
2852 eap->save_msg_silent = msg_silent;
2853 ++msg_silent;
2854 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002855 if (*eap->cmd == '!' && !VIM_ISWHITE(eap->cmd[-1]))
2856 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002857 // ":silent!", but not "silent !cmd"
Bram Moolenaareffed932018-08-14 13:38:17 +02002858 eap->cmd = skipwhite(eap->cmd + 1);
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002859 if (!skip_only)
2860 {
2861 ++emsg_silent;
2862 ++eap->did_esilent;
2863 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002864 }
2865 continue;
2866
2867 case 't': if (checkforcmd(&p, "tab", 3))
2868 {
Bram Moolenaar548e5982018-12-26 21:45:00 +01002869 if (!skip_only)
Bram Moolenaareffed932018-08-14 13:38:17 +02002870 {
Bram Moolenaar548e5982018-12-26 21:45:00 +01002871 long tabnr = get_address(eap, &eap->cmd,
2872 ADDR_TABS, eap->skip,
2873 skip_only, FALSE, 1);
2874 if (tabnr == MAXLNUM)
2875 cmdmod.tab = tabpage_index(curtab) + 1;
2876 else
Bram Moolenaareffed932018-08-14 13:38:17 +02002877 {
Bram Moolenaar548e5982018-12-26 21:45:00 +01002878 if (tabnr < 0 || tabnr > LAST_TAB_NR)
2879 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002880 *errormsg = _(e_invrange);
Bram Moolenaar548e5982018-12-26 21:45:00 +01002881 return FAIL;
2882 }
2883 cmdmod.tab = tabnr + 1;
Bram Moolenaareffed932018-08-14 13:38:17 +02002884 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002885 }
2886 eap->cmd = p;
2887 continue;
2888 }
2889 if (!checkforcmd(&eap->cmd, "topleft", 2))
2890 break;
2891 cmdmod.split |= WSP_TOP;
2892 continue;
2893
2894 case 'u': if (!checkforcmd(&eap->cmd, "unsilent", 3))
2895 break;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002896 if (!skip_only)
2897 {
2898 if (eap->save_msg_silent == -1)
2899 eap->save_msg_silent = msg_silent;
2900 msg_silent = 0;
2901 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002902 continue;
2903
2904 case 'v': if (checkforcmd(&eap->cmd, "vertical", 4))
2905 {
2906 cmdmod.split |= WSP_VERT;
2907 continue;
2908 }
2909 if (!checkforcmd(&p, "verbose", 4))
2910 break;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002911 if (!skip_only)
2912 {
2913 if (eap->verbose_save < 0)
2914 eap->verbose_save = p_verbose;
2915 if (vim_isdigit(*eap->cmd))
2916 p_verbose = atoi((char *)eap->cmd);
2917 else
2918 p_verbose = 1;
2919 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002920 eap->cmd = p;
2921 continue;
2922 }
2923 break;
2924 }
2925
2926 return OK;
2927}
2928
2929/*
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002930 * Free contents of "cmdmod".
2931 */
2932 static void
2933free_cmdmod(void)
2934{
2935 if (cmdmod.save_ei != NULL)
2936 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002937 // Restore 'eventignore' to the value before ":noautocmd".
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002938 set_string_option_direct((char_u *)"ei", -1, cmdmod.save_ei,
2939 OPT_FREE, SID_NONE);
2940 free_string_option(cmdmod.save_ei);
2941 }
2942
2943 if (cmdmod.filter_regmatch.regprog != NULL)
2944 vim_regfree(cmdmod.filter_regmatch.regprog);
2945}
2946
2947/*
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002948 * Parse the address range, if any, in "eap".
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02002949 * May set the last search pattern, unless "silent" is TRUE.
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002950 * Return FAIL and set "errormsg" or return OK.
2951 */
2952 int
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002953parse_cmd_address(exarg_T *eap, char **errormsg, int silent)
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002954{
2955 int address_count = 1;
2956 linenr_T lnum;
2957
2958 // Repeat for all ',' or ';' separated addresses.
2959 for (;;)
2960 {
2961 eap->line1 = eap->line2;
2962 switch (eap->addr_type)
2963 {
2964 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02002965 case ADDR_OTHER:
Bram Moolenaarb5383b12020-05-18 19:46:48 +02002966 // Default is the cursor line number. Avoid using an invalid
2967 // line number though.
2968 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
2969 eap->line2 = curbuf->b_ml.ml_line_count;
2970 else
2971 eap->line2 = curwin->w_cursor.lnum;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002972 break;
2973 case ADDR_WINDOWS:
2974 eap->line2 = CURRENT_WIN_NR;
2975 break;
2976 case ADDR_ARGUMENTS:
2977 eap->line2 = curwin->w_arg_idx + 1;
2978 if (eap->line2 > ARGCOUNT)
2979 eap->line2 = ARGCOUNT;
2980 break;
2981 case ADDR_LOADED_BUFFERS:
2982 case ADDR_BUFFERS:
2983 eap->line2 = curbuf->b_fnum;
2984 break;
2985 case ADDR_TABS:
2986 eap->line2 = CURRENT_TAB_NR;
2987 break;
2988 case ADDR_TABS_RELATIVE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02002989 case ADDR_UNSIGNED:
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002990 eap->line2 = 1;
2991 break;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002992 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02002993#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02002994 eap->line2 = qf_get_cur_idx(eap);
2995#endif
2996 break;
2997 case ADDR_QUICKFIX_VALID:
2998#ifdef FEAT_QUICKFIX
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002999 eap->line2 = qf_get_cur_valid_idx(eap);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003000#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003001 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02003002 case ADDR_NONE:
3003 // Will give an error later if a range is found.
3004 break;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003005 }
3006 eap->cmd = skipwhite(eap->cmd);
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02003007 lnum = get_address(eap, &eap->cmd, eap->addr_type, eap->skip, silent,
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003008 eap->addr_count == 0, address_count++);
3009 if (eap->cmd == NULL) // error detected
3010 return FAIL;
3011 if (lnum == MAXLNUM)
3012 {
3013 if (*eap->cmd == '%') // '%' - all lines
3014 {
3015 ++eap->cmd;
3016 switch (eap->addr_type)
3017 {
3018 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02003019 case ADDR_OTHER:
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003020 eap->line1 = 1;
3021 eap->line2 = curbuf->b_ml.ml_line_count;
3022 break;
3023 case ADDR_LOADED_BUFFERS:
3024 {
3025 buf_T *buf = firstbuf;
3026
3027 while (buf->b_next != NULL
3028 && buf->b_ml.ml_mfp == NULL)
3029 buf = buf->b_next;
3030 eap->line1 = buf->b_fnum;
3031 buf = lastbuf;
3032 while (buf->b_prev != NULL
3033 && buf->b_ml.ml_mfp == NULL)
3034 buf = buf->b_prev;
3035 eap->line2 = buf->b_fnum;
3036 break;
3037 }
3038 case ADDR_BUFFERS:
3039 eap->line1 = firstbuf->b_fnum;
3040 eap->line2 = lastbuf->b_fnum;
3041 break;
3042 case ADDR_WINDOWS:
3043 case ADDR_TABS:
3044 if (IS_USER_CMDIDX(eap->cmdidx))
3045 {
3046 eap->line1 = 1;
3047 eap->line2 = eap->addr_type == ADDR_WINDOWS
3048 ? LAST_WIN_NR : LAST_TAB_NR;
3049 }
3050 else
3051 {
3052 // there is no Vim command which uses '%' and
3053 // ADDR_WINDOWS or ADDR_TABS
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003054 *errormsg = _(e_invrange);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003055 return FAIL;
3056 }
3057 break;
3058 case ADDR_TABS_RELATIVE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02003059 case ADDR_UNSIGNED:
3060 case ADDR_QUICKFIX:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003061 *errormsg = _(e_invrange);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003062 return FAIL;
3063 case ADDR_ARGUMENTS:
3064 if (ARGCOUNT == 0)
3065 eap->line1 = eap->line2 = 0;
3066 else
3067 {
3068 eap->line1 = 1;
3069 eap->line2 = ARGCOUNT;
3070 }
3071 break;
Bram Moolenaar25190db2019-05-04 15:05:28 +02003072 case ADDR_QUICKFIX_VALID:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003073#ifdef FEAT_QUICKFIX
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003074 eap->line1 = 1;
Bram Moolenaar25190db2019-05-04 15:05:28 +02003075 eap->line2 = qf_get_valid_size(eap);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003076 if (eap->line2 == 0)
3077 eap->line2 = 1;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003078#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003079 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02003080 case ADDR_NONE:
3081 // Will give an error later if a range is found.
3082 break;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003083 }
3084 ++eap->addr_count;
3085 }
3086 else if (*eap->cmd == '*' && vim_strchr(p_cpo, CPO_STAR) == NULL)
3087 {
3088 pos_T *fp;
3089
3090 // '*' - visual area
3091 if (eap->addr_type != ADDR_LINES)
3092 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003093 *errormsg = _(e_invrange);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003094 return FAIL;
3095 }
3096
3097 ++eap->cmd;
3098 if (!eap->skip)
3099 {
3100 fp = getmark('<', FALSE);
3101 if (check_mark(fp) == FAIL)
3102 return FAIL;
3103 eap->line1 = fp->lnum;
3104 fp = getmark('>', FALSE);
3105 if (check_mark(fp) == FAIL)
3106 return FAIL;
3107 eap->line2 = fp->lnum;
3108 ++eap->addr_count;
3109 }
3110 }
3111 }
3112 else
3113 eap->line2 = lnum;
3114 eap->addr_count++;
3115
3116 if (*eap->cmd == ';')
3117 {
3118 if (!eap->skip)
3119 {
3120 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaar0e717042020-04-27 19:29:01 +02003121 // Don't leave the cursor on an illegal line or column, but do
3122 // accept zero as address, so 0;/PATTERN/ works correctly.
3123 if (eap->line2 > 0)
3124 check_cursor();
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003125 }
3126 }
3127 else if (*eap->cmd != ',')
3128 break;
3129 ++eap->cmd;
3130 }
3131
3132 // One address given: set start and end lines.
3133 if (eap->addr_count == 1)
3134 {
3135 eap->line1 = eap->line2;
3136 // ... but only implicit: really no address given
3137 if (lnum == MAXLNUM)
3138 eap->addr_count = 0;
3139 }
3140 return OK;
3141}
3142
3143/*
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00003144 * Check for an Ex command with optional tail.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003145 * If there is a match advance "pp" to the argument and return TRUE.
3146 */
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00003147 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003148checkforcmd(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003149 char_u **pp, // start of command
3150 char *cmd, // name of command
3151 int len) // required length
Bram Moolenaar071d4272004-06-13 20:20:40 +00003152{
3153 int i;
3154
3155 for (i = 0; cmd[i] != NUL; ++i)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003156 if (((char_u *)cmd)[i] != (*pp)[i])
Bram Moolenaar071d4272004-06-13 20:20:40 +00003157 break;
3158 if (i >= len && !isalpha((*pp)[i]))
3159 {
3160 *pp = skipwhite(*pp + i);
3161 return TRUE;
3162 }
3163 return FALSE;
3164}
3165
3166/*
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003167 * Append "cmd" to the error message in IObuff.
3168 * Takes care of limiting the length and handling 0xa0, which would be
3169 * invisible otherwise.
3170 */
3171 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003172append_command(char_u *cmd)
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003173{
3174 char_u *s = cmd;
3175 char_u *d;
3176
3177 STRCAT(IObuff, ": ");
3178 d = IObuff + STRLEN(IObuff);
3179 while (*s != NUL && d - IObuff < IOSIZE - 7)
3180 {
Bram Moolenaar13505972019-01-24 15:04:48 +01003181 if (enc_utf8 ? (s[0] == 0xc2 && s[1] == 0xa0) : *s == 0xa0)
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003182 {
Bram Moolenaar13505972019-01-24 15:04:48 +01003183 s += enc_utf8 ? 2 : 1;
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003184 STRCPY(d, "<a0>");
3185 d += 4;
3186 }
3187 else
3188 MB_COPY_CHAR(s, d);
3189 }
3190 *d = NUL;
3191}
3192
3193/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003194 * Find an Ex command by its name, either built-in or user.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003195 * Start of the name can be found at eap->cmd.
Bram Moolenaar25190db2019-05-04 15:05:28 +02003196 * Sets eap->cmdidx and returns a pointer to char after the command name.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003197 * "full" is set to TRUE if the whole command name matched.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003198 *
3199 * If "lookup" is not NULL recognize expression without "eval" or "call" and
3200 * assignment without "let". Sets eap->cmdidx to the command while returning
3201 * "eap->cmd".
3202 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00003203 * Returns NULL for an ambiguous user command.
3204 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003205 char_u *
3206find_ex_command(
3207 exarg_T *eap,
Bram Moolenaara494f562020-04-18 17:45:38 +02003208 int *full UNUSED,
Bram Moolenaarb84a3812020-05-01 15:44:29 +02003209 void *(*lookup)(char_u *, size_t, cctx_T *) UNUSED,
Bram Moolenaara494f562020-04-18 17:45:38 +02003210 cctx_T *cctx UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003211{
3212 int len;
3213 char_u *p;
Bram Moolenaardf177f62005-02-22 08:39:57 +00003214 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003215
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003216#ifdef FEAT_EVAL
3217 /*
3218 * Recognize a Vim9 script function/method call and assignment:
3219 * "lvar = value", "lvar(arg)", "[1, 2 3]->Func()"
3220 */
Bram Moolenaar0c6ceaf2020-02-22 18:36:32 +01003221 p = eap->cmd;
Bram Moolenaard2ef6b32020-07-02 21:11:34 +02003222 if (lookup != NULL && (*p == '(' || *p == '{'
3223 || ((p = to_name_const_end(eap->cmd)) > eap->cmd && *p != NUL)
3224 || *p == '['))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003225 {
3226 int oplen;
3227 int heredoc;
3228
3229 // "funcname(" is always a function call.
3230 // "varname[]" is an expression.
3231 // "g:varname" is an expression.
3232 // "varname->expr" is an expression.
Bram Moolenaar0c6ceaf2020-02-22 18:36:32 +01003233 // "(..." is an expression.
Bram Moolenaar5f195932020-07-01 20:07:14 +02003234 // "{..." is an dict expression.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003235 if (*p == '('
Bram Moolenaar5f195932020-07-01 20:07:14 +02003236 || *p == '{'
Bram Moolenaard2ef6b32020-07-02 21:11:34 +02003237 || (*p == '[' && p > eap->cmd)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003238 || p[1] == ':'
3239 || (*p == '-' && p[1] == '>'))
3240 {
3241 eap->cmdidx = CMD_eval;
3242 return eap->cmd;
3243 }
3244
Bram Moolenaard2ef6b32020-07-02 21:11:34 +02003245 // "[...]->Method()" is a list expression, but "[a, b] = Func()" is
3246 // an assignment.
3247 // If there is no line break inside the "[...]" then "p" is advanced to
3248 // after the "]" by to_name_const_end(): check if a "=" follows.
3249 // If "[...]" has a line break "p" still points at the "[" and it can't
3250 // be an assignment.
3251 if (*eap->cmd == '[' && (p == eap->cmd || *skipwhite(p) != '='))
3252 {
3253 eap->cmdidx = CMD_eval;
3254 return eap->cmd;
3255 }
3256
Bram Moolenaar5f195932020-07-01 20:07:14 +02003257 // Recognize an assignment if we recognize the variable name:
3258 // "g:var = expr"
3259 // "var = expr" where "var" is a local var name.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003260 oplen = assignment_len(skipwhite(p), &heredoc);
3261 if (oplen > 0)
3262 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003263 if (((p - eap->cmd) > 2 && eap->cmd[1] == ':')
Bram Moolenaarb84a3812020-05-01 15:44:29 +02003264 || lookup(eap->cmd, p - eap->cmd, cctx) != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003265 {
3266 eap->cmdidx = CMD_let;
3267 return eap->cmd;
3268 }
3269 }
3270 }
3271#endif
3272
Bram Moolenaar071d4272004-06-13 20:20:40 +00003273 /*
3274 * Isolate the command and search for it in the command table.
Bram Moolenaar81870892007-11-11 18:17:28 +00003275 * Exceptions:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003276 * - the 'k' command can directly be followed by any character.
3277 * - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003278 * but :sre[wind] is another command, as are :scr[iptnames],
Bram Moolenaar071d4272004-06-13 20:20:40 +00003279 * :scs[cope], :sim[alt], :sig[ns] and :sil[ent].
Bram Moolenaardf177f62005-02-22 08:39:57 +00003280 * - the "d" command can directly be followed by 'l' or 'p' flag.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003281 */
3282 p = eap->cmd;
3283 if (*p == 'k')
3284 {
3285 eap->cmdidx = CMD_k;
3286 ++p;
3287 }
3288 else if (p[0] == 's'
Bram Moolenaar204b93f2015-08-04 22:02:51 +02003289 && ((p[1] == 'c' && (p[2] == NUL || (p[2] != 's' && p[2] != 'r'
3290 && (p[3] == NUL || (p[3] != 'i' && p[4] != 'p')))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003291 || p[1] == 'g'
3292 || (p[1] == 'i' && p[2] != 'm' && p[2] != 'l' && p[2] != 'g')
3293 || p[1] == 'I'
3294 || (p[1] == 'r' && p[2] != 'e')))
3295 {
3296 eap->cmdidx = CMD_substitute;
3297 ++p;
3298 }
3299 else
3300 {
3301 while (ASCII_ISALPHA(*p))
3302 ++p;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003303 // for python 3.x support ":py3", ":python3", ":py3file", etc.
Bram Moolenaar55d5c032010-07-17 23:52:29 +02003304 if (eap->cmd[0] == 'p' && eap->cmd[1] == 'y')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003305 {
Bram Moolenaarb6590522010-07-21 16:00:43 +02003306 while (ASCII_ISALNUM(*p))
3307 ++p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003308 }
3309 else if (*p == '9' && STRNCMP("vim9", eap->cmd, 4) == 0)
3310 {
3311 // include "9" for "vim9script"
3312 ++p;
3313 while (ASCII_ISALPHA(*p))
3314 ++p;
3315 }
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02003316
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003317 // check for non-alpha command
Bram Moolenaar071d4272004-06-13 20:20:40 +00003318 if (p == eap->cmd && vim_strchr((char_u *)"@*!=><&~#", *p) != NULL)
3319 ++p;
3320 len = (int)(p - eap->cmd);
Bram Moolenaardf177f62005-02-22 08:39:57 +00003321 if (*eap->cmd == 'd' && (p[-1] == 'l' || p[-1] == 'p'))
3322 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003323 // Check for ":dl", ":dell", etc. to ":deletel": that's
3324 // :delete with the 'l' flag. Same for 'p'.
Bram Moolenaardf177f62005-02-22 08:39:57 +00003325 for (i = 0; i < len; ++i)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003326 if (eap->cmd[i] != ((char_u *)"delete")[i])
Bram Moolenaardf177f62005-02-22 08:39:57 +00003327 break;
3328 if (i == len - 1)
3329 {
3330 --len;
3331 if (p[-1] == 'l')
3332 eap->flags |= EXFLAG_LIST;
3333 else
3334 eap->flags |= EXFLAG_PRINT;
3335 }
3336 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003337
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003338 if (ASCII_ISLOWER(eap->cmd[0]))
3339 {
Bram Moolenaar6c0c1e82017-03-25 15:07:43 +01003340 int c1 = eap->cmd[0];
Bram Moolenaar94f82cb2019-07-24 22:30:27 +02003341 int c2 = len == 1 ? NUL : eap->cmd[1];
Bram Moolenaar6c0c1e82017-03-25 15:07:43 +01003342
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003343 if (command_count != (int)CMD_SIZE)
3344 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003345 iemsg(_("E943: Command table needs to be updated, run 'make cmdidxs'"));
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003346 getout(1);
3347 }
3348
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003349 // Use a precomputed index for fast look-up in cmdnames[]
3350 // taking into account the first 2 letters of eap->cmd.
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003351 eap->cmdidx = cmdidxs1[CharOrdLow(c1)];
3352 if (ASCII_ISLOWER(c2))
3353 eap->cmdidx += cmdidxs2[CharOrdLow(c1)][CharOrdLow(c2)];
3354 }
Bram Moolenaara494f562020-04-18 17:45:38 +02003355 else if (ASCII_ISUPPER(eap->cmd[0]))
3356 eap->cmdidx = CMD_Next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003357 else
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003358 eap->cmdidx = CMD_bang;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003359
3360 for ( ; (int)eap->cmdidx < (int)CMD_SIZE;
3361 eap->cmdidx = (cmdidx_T)((int)eap->cmdidx + 1))
3362 if (STRNCMP(cmdnames[(int)eap->cmdidx].cmd_name, (char *)eap->cmd,
3363 (size_t)len) == 0)
3364 {
3365#ifdef FEAT_EVAL
3366 if (full != NULL
3367 && cmdnames[(int)eap->cmdidx].cmd_name[len] == NUL)
3368 *full = TRUE;
3369#endif
3370 break;
3371 }
3372
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003373 // Look for a user defined command as a last resort. Let ":Print" be
3374 // overruled by a user defined command.
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01003375 if ((eap->cmdidx == CMD_SIZE || eap->cmdidx == CMD_Print)
3376 && *eap->cmd >= 'A' && *eap->cmd <= 'Z')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003377 {
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003378 // User defined commands may contain digits.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003379 while (ASCII_ISALNUM(*p))
3380 ++p;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003381 p = find_ucmd(eap, p, full, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003382 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003383 if (p == eap->cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003384 eap->cmdidx = CMD_SIZE;
3385 }
3386
3387 return p;
3388}
3389
3390#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003391static struct cmdmod
3392{
3393 char *name;
3394 int minlen;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003395 int has_count; // :123verbose :3tab
Bram Moolenaared53fb92007-11-24 20:50:24 +00003396} cmdmods[] = {
3397 {"aboveleft", 3, FALSE},
3398 {"belowright", 3, FALSE},
3399 {"botright", 2, FALSE},
3400 {"browse", 3, FALSE},
3401 {"confirm", 4, FALSE},
Bram Moolenaar7b668e82016-08-23 23:51:21 +02003402 {"filter", 4, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003403 {"hide", 3, FALSE},
3404 {"keepalt", 5, FALSE},
3405 {"keepjumps", 5, FALSE},
3406 {"keepmarks", 3, FALSE},
Bram Moolenaara939e432013-11-09 05:30:26 +01003407 {"keeppatterns", 5, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003408 {"leftabove", 5, FALSE},
3409 {"lockmarks", 3, FALSE},
Bram Moolenaarca9f9582008-09-18 10:44:28 +00003410 {"noautocmd", 3, FALSE},
Bram Moolenaar5803ae62014-03-23 16:04:02 +01003411 {"noswapfile", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003412 {"rightbelow", 6, FALSE},
3413 {"sandbox", 3, FALSE},
3414 {"silent", 3, FALSE},
3415 {"tab", 3, TRUE},
3416 {"topleft", 2, FALSE},
Bram Moolenaar8e258a42009-07-09 13:55:43 +00003417 {"unsilent", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003418 {"verbose", 4, TRUE},
3419 {"vertical", 4, FALSE},
3420};
3421
3422/*
3423 * Return length of a command modifier (including optional count).
3424 * Return zero when it's not a modifier.
3425 */
3426 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003427modifier_len(char_u *cmd)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003428{
3429 int i, j;
3430 char_u *p = cmd;
3431
3432 if (VIM_ISDIGIT(*cmd))
3433 p = skipwhite(skipdigits(cmd));
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00003434 for (i = 0; i < (int)(sizeof(cmdmods) / sizeof(struct cmdmod)); ++i)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003435 {
3436 for (j = 0; p[j] != NUL; ++j)
3437 if (p[j] != cmdmods[i].name[j])
3438 break;
Bram Moolenaar2d473ab2013-06-12 17:12:24 +02003439 if (!ASCII_ISALPHA(p[j]) && j >= cmdmods[i].minlen
Bram Moolenaared53fb92007-11-24 20:50:24 +00003440 && (p == cmd || cmdmods[i].has_count))
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00003441 return j + (int)(p - cmd);
Bram Moolenaared53fb92007-11-24 20:50:24 +00003442 }
3443 return 0;
3444}
3445
Bram Moolenaar071d4272004-06-13 20:20:40 +00003446/*
3447 * Return > 0 if an Ex command "name" exists.
3448 * Return 2 if there is an exact match.
3449 * Return 3 if there is an ambiguous match.
3450 */
3451 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003452cmd_exists(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003453{
3454 exarg_T ea;
3455 int full = FALSE;
3456 int i;
3457 int j;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003458 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003459
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003460 // Check command modifiers.
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00003461 for (i = 0; i < (int)(sizeof(cmdmods) / sizeof(struct cmdmod)); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003462 {
3463 for (j = 0; name[j] != NUL; ++j)
3464 if (name[j] != cmdmods[i].name[j])
3465 break;
3466 if (name[j] == NUL && j >= cmdmods[i].minlen)
3467 return (cmdmods[i].name[j] == NUL ? 2 : 1);
3468 }
3469
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003470 // Check built-in commands and user defined commands.
3471 // For ":2match" and ":3match" we need to skip the number.
Bram Moolenaara9587612006-05-04 21:47:50 +00003472 ea.cmd = (*name == '2' || *name == '3') ? name + 1 : name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003473 ea.cmdidx = (cmdidx_T)0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003474 p = find_ex_command(&ea, &full, NULL, NULL);
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003475 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003476 return 3;
Bram Moolenaara9587612006-05-04 21:47:50 +00003477 if (vim_isdigit(*name) && ea.cmdidx != CMD_match)
3478 return 0;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003479 if (*skipwhite(p) != NUL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003480 return 0; // trailing garbage
Bram Moolenaar071d4272004-06-13 20:20:40 +00003481 return (ea.cmdidx == CMD_SIZE ? 0 : (full ? 2 : 1));
3482}
3483#endif
3484
Bram Moolenaard0190392019-08-23 21:17:35 +02003485 cmdidx_T
3486excmd_get_cmdidx(char_u *cmd, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003487{
Bram Moolenaard0190392019-08-23 21:17:35 +02003488 cmdidx_T idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003489
Bram Moolenaard0190392019-08-23 21:17:35 +02003490 for (idx = (cmdidx_T)0; (int)idx < (int)CMD_SIZE;
3491 idx = (cmdidx_T)((int)idx + 1))
3492 if (STRNCMP(cmdnames[(int)idx].cmd_name, cmd, (size_t)len) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003493 break;
3494
Bram Moolenaard0190392019-08-23 21:17:35 +02003495 return idx;
3496}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003497
Bram Moolenaard0190392019-08-23 21:17:35 +02003498 long
3499excmd_get_argt(cmdidx_T idx)
3500{
3501 return (long)cmdnames[(int)idx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003502}
3503
3504/*
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003505 * Skip a range specifier of the form: addr [,addr] [;addr] ..
Bram Moolenaar071d4272004-06-13 20:20:40 +00003506 *
3507 * Backslashed delimiters after / or ? will be skipped, and commands will
3508 * not be expanded between /'s and ?'s or after "'".
3509 *
Bram Moolenaardf069ee2020-06-22 23:02:51 +02003510 * Also skip white space and ":" characters after the range.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003511 * Returns the "cmd" pointer advanced to beyond the range.
3512 */
3513 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003514skip_range(
3515 char_u *cmd,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003516 int *ctx) // pointer to xp_context or NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003517{
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003518 unsigned delim;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003519
Bram Moolenaarcbf20fb2017-02-03 21:19:04 +01003520 while (vim_strchr((char_u *)" \t0123456789.$%'/?-+,;\\", *cmd) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003521 {
Bram Moolenaarcbf20fb2017-02-03 21:19:04 +01003522 if (*cmd == '\\')
3523 {
3524 if (cmd[1] == '?' || cmd[1] == '/' || cmd[1] == '&')
3525 ++cmd;
3526 else
3527 break;
3528 }
3529 else if (*cmd == '\'')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003530 {
3531 if (*++cmd == NUL && ctx != NULL)
3532 *ctx = EXPAND_NOTHING;
3533 }
3534 else if (*cmd == '/' || *cmd == '?')
3535 {
3536 delim = *cmd++;
3537 while (*cmd != NUL && *cmd != delim)
3538 if (*cmd++ == '\\' && *cmd != NUL)
3539 ++cmd;
3540 if (*cmd == NUL && ctx != NULL)
3541 *ctx = EXPAND_NOTHING;
3542 }
3543 if (*cmd != NUL)
3544 ++cmd;
3545 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00003546
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003547 // Skip ":" and white space.
Bram Moolenaardf177f62005-02-22 08:39:57 +00003548 while (*cmd == ':')
3549 cmd = skipwhite(cmd + 1);
3550
Bram Moolenaar071d4272004-06-13 20:20:40 +00003551 return cmd;
3552}
3553
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003554 static void
3555addr_error(cmd_addr_T addr_type)
3556{
3557 if (addr_type == ADDR_NONE)
3558 emsg(_(e_norange));
3559 else
3560 emsg(_(e_invrange));
3561}
3562
Bram Moolenaar071d4272004-06-13 20:20:40 +00003563/*
Bram Moolenaar198cb662018-09-06 21:44:17 +02003564 * Get a single EX address.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003565 *
3566 * Set ptr to the next character after the part that was interpreted.
3567 * Set ptr to NULL when an error is encountered.
Bram Moolenaar198cb662018-09-06 21:44:17 +02003568 * This may set the last used search pattern.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003569 *
3570 * Return MAXLNUM when no Ex address was found.
3571 */
3572 static linenr_T
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003573get_address(
3574 exarg_T *eap UNUSED,
3575 char_u **ptr,
Bram Moolenaar1b03a192019-12-08 17:08:29 +01003576 cmd_addr_T addr_type,
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02003577 int skip, // only skip the address, don't use it
3578 int silent, // no errors or side effects
3579 int to_other_file, // flag: may jump to other file
3580 int address_count UNUSED) // 1 for first address, >1 after comma
Bram Moolenaar071d4272004-06-13 20:20:40 +00003581{
3582 int c;
3583 int i;
3584 long n;
3585 char_u *cmd;
3586 pos_T pos;
3587 pos_T *fp;
3588 linenr_T lnum;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003589 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003590
3591 cmd = skipwhite(*ptr);
3592 lnum = MAXLNUM;
3593 do
3594 {
3595 switch (*cmd)
3596 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003597 case '.': // '.' - Cursor position
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003598 ++cmd;
3599 switch (addr_type)
3600 {
3601 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02003602 case ADDR_OTHER:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003603 lnum = curwin->w_cursor.lnum;
3604 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003605 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01003606 lnum = CURRENT_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003607 break;
3608 case ADDR_ARGUMENTS:
3609 lnum = curwin->w_arg_idx + 1;
3610 break;
3611 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003612 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003613 lnum = curbuf->b_fnum;
3614 break;
3615 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01003616 lnum = CURRENT_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003617 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02003618 case ADDR_NONE:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003619 case ADDR_TABS_RELATIVE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02003620 case ADDR_UNSIGNED:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003621 addr_error(addr_type);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01003622 cmd = NULL;
3623 goto error;
3624 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003625 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003626#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02003627 lnum = qf_get_cur_idx(eap);
3628#endif
3629 break;
3630 case ADDR_QUICKFIX_VALID:
3631#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003632 lnum = qf_get_cur_valid_idx(eap);
Bram Moolenaare906c502015-09-09 21:10:39 +02003633#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003634 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003635 }
3636 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003637
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003638 case '$': // '$' - last line
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003639 ++cmd;
3640 switch (addr_type)
3641 {
3642 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02003643 case ADDR_OTHER:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003644 lnum = curbuf->b_ml.ml_line_count;
3645 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003646 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01003647 lnum = LAST_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003648 break;
3649 case ADDR_ARGUMENTS:
3650 lnum = ARGCOUNT;
3651 break;
3652 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003653 buf = lastbuf;
3654 while (buf->b_ml.ml_mfp == NULL)
3655 {
3656 if (buf->b_prev == NULL)
3657 break;
3658 buf = buf->b_prev;
3659 }
3660 lnum = buf->b_fnum;
3661 break;
3662 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003663 lnum = lastbuf->b_fnum;
3664 break;
3665 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01003666 lnum = LAST_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003667 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02003668 case ADDR_NONE:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003669 case ADDR_TABS_RELATIVE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02003670 case ADDR_UNSIGNED:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003671 addr_error(addr_type);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01003672 cmd = NULL;
3673 goto error;
3674 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003675 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003676#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003677 lnum = qf_get_size(eap);
3678 if (lnum == 0)
3679 lnum = 1;
Bram Moolenaare906c502015-09-09 21:10:39 +02003680#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003681 break;
Bram Moolenaar25190db2019-05-04 15:05:28 +02003682 case ADDR_QUICKFIX_VALID:
3683#ifdef FEAT_QUICKFIX
3684 lnum = qf_get_valid_size(eap);
3685 if (lnum == 0)
3686 lnum = 1;
3687#endif
3688 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003689 }
3690 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003691
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003692 case '\'': // ''' - mark
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003693 if (*++cmd == NUL)
3694 {
3695 cmd = NULL;
3696 goto error;
3697 }
3698 if (addr_type != ADDR_LINES)
3699 {
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003700 addr_error(addr_type);
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01003701 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003702 goto error;
3703 }
3704 if (skip)
3705 ++cmd;
3706 else
3707 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003708 // Only accept a mark in another file when it is
3709 // used by itself: ":'M".
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003710 fp = getmark(*cmd, to_other_file && cmd[1] == NUL);
3711 ++cmd;
3712 if (fp == (pos_T *)-1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003713 // Jumped to another file.
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003714 lnum = curwin->w_cursor.lnum;
3715 else
3716 {
3717 if (check_mark(fp) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003718 {
3719 cmd = NULL;
3720 goto error;
3721 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003722 lnum = fp->lnum;
3723 }
3724 }
3725 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003726
3727 case '/':
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003728 case '?': // '/' or '?' - search
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003729 c = *cmd++;
3730 if (addr_type != ADDR_LINES)
3731 {
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003732 addr_error(addr_type);
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01003733 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003734 goto error;
3735 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003736 if (skip) // skip "/pat/"
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003737 {
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02003738 cmd = skip_regexp(cmd, c, (int)p_magic);
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003739 if (*cmd == c)
3740 ++cmd;
3741 }
3742 else
3743 {
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02003744 int flags;
3745
3746 pos = curwin->w_cursor; // save curwin->w_cursor
3747
3748 // When '/' or '?' follows another address, start from
3749 // there.
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003750 if (lnum != MAXLNUM)
3751 curwin->w_cursor.lnum = lnum;
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02003752
3753 // Start a forward search at the end of the line (unless
3754 // before the first line).
3755 // Start a backward search at the start of the line.
3756 // This makes sure we never match in the current
3757 // line, and can match anywhere in the
3758 // next/previous line.
Bram Moolenaar8ada6aa2017-12-19 21:23:21 +01003759 if (c == '/' && curwin->w_cursor.lnum > 0)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003760 curwin->w_cursor.col = MAXCOL;
3761 else
3762 curwin->w_cursor.col = 0;
3763 searchcmdlen = 0;
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02003764 flags = silent ? 0 : SEARCH_HIS | SEARCH_MSG;
Bram Moolenaarc036e872020-02-21 21:30:52 +01003765 if (!do_search(NULL, c, c, cmd, 1L, flags, NULL))
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003766 {
3767 curwin->w_cursor = pos;
3768 cmd = NULL;
3769 goto error;
3770 }
3771 lnum = curwin->w_cursor.lnum;
3772 curwin->w_cursor = pos;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003773 // adjust command string pointer
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003774 cmd += searchcmdlen;
3775 }
3776 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003777
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003778 case '\\': // "\?", "\/" or "\&", repeat search
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003779 ++cmd;
3780 if (addr_type != ADDR_LINES)
3781 {
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003782 addr_error(addr_type);
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01003783 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003784 goto error;
3785 }
3786 if (*cmd == '&')
3787 i = RE_SUBST;
3788 else if (*cmd == '?' || *cmd == '/')
3789 i = RE_SEARCH;
3790 else
3791 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003792 emsg(_(e_backslash));
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003793 cmd = NULL;
3794 goto error;
3795 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003796
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003797 if (!skip)
3798 {
3799 /*
3800 * When search follows another address, start from
3801 * there.
3802 */
3803 if (lnum != MAXLNUM)
3804 pos.lnum = lnum;
3805 else
3806 pos.lnum = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003807
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003808 /*
3809 * Start the search just like for the above
3810 * do_search().
3811 */
3812 if (*cmd != '?')
3813 pos.col = MAXCOL;
3814 else
3815 pos.col = 0;
Bram Moolenaarbd8539a2015-08-11 18:53:03 +02003816 pos.coladd = 0;
Bram Moolenaar5d24a222018-12-23 19:10:09 +01003817 if (searchit(curwin, curbuf, &pos, NULL,
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003818 *cmd == '?' ? BACKWARD : FORWARD,
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02003819 (char_u *)"", 1L, SEARCH_MSG, i, NULL) != FAIL)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003820 lnum = pos.lnum;
3821 else
3822 {
3823 cmd = NULL;
3824 goto error;
3825 }
3826 }
3827 ++cmd;
3828 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003829
3830 default:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003831 if (VIM_ISDIGIT(*cmd)) // absolute line number
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003832 lnum = getdigits(&cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003833 }
3834
3835 for (;;)
3836 {
3837 cmd = skipwhite(cmd);
3838 if (*cmd != '-' && *cmd != '+' && !VIM_ISDIGIT(*cmd))
3839 break;
3840
3841 if (lnum == MAXLNUM)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003842 {
3843 switch (addr_type)
3844 {
3845 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02003846 case ADDR_OTHER:
3847 // "+1" is same as ".+1"
Bram Moolenaarf240e182014-11-27 18:33:02 +01003848 lnum = curwin->w_cursor.lnum;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003849 break;
3850 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01003851 lnum = CURRENT_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003852 break;
3853 case ADDR_ARGUMENTS:
3854 lnum = curwin->w_arg_idx + 1;
3855 break;
3856 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003857 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003858 lnum = curbuf->b_fnum;
3859 break;
3860 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01003861 lnum = CURRENT_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003862 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01003863 case ADDR_TABS_RELATIVE:
3864 lnum = 1;
3865 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003866 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003867#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02003868 lnum = qf_get_cur_idx(eap);
3869#endif
3870 break;
3871 case ADDR_QUICKFIX_VALID:
3872#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003873 lnum = qf_get_cur_valid_idx(eap);
Bram Moolenaare906c502015-09-09 21:10:39 +02003874#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003875 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02003876 case ADDR_NONE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02003877 case ADDR_UNSIGNED:
3878 lnum = 0;
Bram Moolenaarb7316892019-05-01 18:08:42 +02003879 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003880 }
3881 }
3882
Bram Moolenaar071d4272004-06-13 20:20:40 +00003883 if (VIM_ISDIGIT(*cmd))
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003884 i = '+'; // "number" is same as "+number"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003885 else
3886 i = *cmd++;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003887 if (!VIM_ISDIGIT(*cmd)) // '+' is '+1', but '+0' is not '+1'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003888 n = 1;
3889 else
3890 n = getdigits(&cmd);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01003891
3892 if (addr_type == ADDR_TABS_RELATIVE)
3893 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003894 emsg(_(e_invrange));
Bram Moolenaar2f72c702017-01-29 14:48:10 +01003895 cmd = NULL;
3896 goto error;
3897 }
3898 else if (addr_type == ADDR_LOADED_BUFFERS
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003899 || addr_type == ADDR_BUFFERS)
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01003900 lnum = compute_buffer_local_count(
3901 addr_type, lnum, (i == '-') ? -1 * n : n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003902 else
Bram Moolenaarded27822017-01-02 14:27:34 +01003903 {
3904#ifdef FEAT_FOLDING
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003905 // Relative line addressing, need to adjust for folded lines
3906 // now, but only do it after the first address.
Bram Moolenaarded27822017-01-02 14:27:34 +01003907 if (addr_type == ADDR_LINES && (i == '-' || i == '+')
3908 && address_count >= 2)
3909 (void)hasFolding(lnum, NULL, &lnum);
3910#endif
3911 if (i == '-')
3912 lnum -= n;
3913 else
3914 lnum += n;
3915 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003916 }
3917 } while (*cmd == '/' || *cmd == '?');
3918
3919error:
3920 *ptr = cmd;
3921 return lnum;
3922}
3923
3924/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00003925 * Get flags from an Ex command argument.
3926 */
3927 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003928get_flags(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00003929{
3930 while (vim_strchr((char_u *)"lp#", *eap->arg) != NULL)
3931 {
3932 if (*eap->arg == 'l')
3933 eap->flags |= EXFLAG_LIST;
3934 else if (*eap->arg == 'p')
3935 eap->flags |= EXFLAG_PRINT;
3936 else
3937 eap->flags |= EXFLAG_NR;
3938 eap->arg = skipwhite(eap->arg + 1);
3939 }
3940}
3941
3942/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003943 * Function called for command which is Not Implemented. NI!
3944 */
3945 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003946ex_ni(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003947{
3948 if (!eap->skip)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003949 eap->errmsg = N_("E319: Sorry, the command is not available in this version");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003950}
3951
Bram Moolenaar7bb75552007-07-16 18:39:49 +00003952#ifdef HAVE_EX_SCRIPT_NI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003953/*
3954 * Function called for script command which is Not Implemented. NI!
3955 * Skips over ":perl <<EOF" constructs.
3956 */
3957 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003958ex_script_ni(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003959{
3960 if (!eap->skip)
3961 ex_ni(eap);
3962 else
3963 vim_free(script_get(eap, eap->arg));
3964}
3965#endif
3966
3967/*
3968 * Check range in Ex command for validity.
3969 * Return NULL when valid, error message when invalid.
3970 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003971 static char *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003972invalid_range(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003973{
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003974 buf_T *buf;
Bram Moolenaar25190db2019-05-04 15:05:28 +02003975
Bram Moolenaar071d4272004-06-13 20:20:40 +00003976 if ( eap->line1 < 0
3977 || eap->line2 < 0
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003978 || eap->line1 > eap->line2)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003979 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003980
Bram Moolenaar8071cb22019-07-12 17:58:01 +02003981 if (eap->argt & EX_RANGE)
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003982 {
Bram Moolenaarb7316892019-05-01 18:08:42 +02003983 switch (eap->addr_type)
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003984 {
3985 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02003986 if (eap->line2 > curbuf->b_ml.ml_line_count
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003987#ifdef FEAT_DIFF
3988 + (eap->cmdidx == CMD_diffget)
3989#endif
3990 )
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003991 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003992 break;
3993 case ADDR_ARGUMENTS:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003994 // add 1 if ARGCOUNT is 0
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01003995 if (eap->line2 > ARGCOUNT + (!ARGCOUNT))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003996 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003997 break;
3998 case ADDR_BUFFERS:
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02003999 // Only a boundary check, not whether the buffers actually
4000 // exist.
4001 if (eap->line1 < 1 || eap->line2 > get_highest_fnum())
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004002 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004003 break;
4004 case ADDR_LOADED_BUFFERS:
4005 buf = firstbuf;
4006 while (buf->b_ml.ml_mfp == NULL)
4007 {
4008 if (buf->b_next == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004009 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004010 buf = buf->b_next;
4011 }
4012 if (eap->line1 < buf->b_fnum)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004013 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004014 buf = lastbuf;
4015 while (buf->b_ml.ml_mfp == NULL)
4016 {
4017 if (buf->b_prev == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004018 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004019 buf = buf->b_prev;
4020 }
4021 if (eap->line2 > buf->b_fnum)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004022 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004023 break;
4024 case ADDR_WINDOWS:
Bram Moolenaar8be63882015-01-14 11:25:05 +01004025 if (eap->line2 > LAST_WIN_NR)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004026 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004027 break;
4028 case ADDR_TABS:
4029 if (eap->line2 > LAST_TAB_NR)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004030 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004031 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004032 case ADDR_TABS_RELATIVE:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004033 case ADDR_OTHER:
4034 // Any range is OK.
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004035 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004036 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004037#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02004038 // No error for value that is too big, will use the last entry.
4039 if (eap->line2 <= 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004040 return _(e_invrange);
Bram Moolenaare906c502015-09-09 21:10:39 +02004041#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004042 break;
Bram Moolenaar25190db2019-05-04 15:05:28 +02004043 case ADDR_QUICKFIX_VALID:
4044#ifdef FEAT_QUICKFIX
4045 if ((eap->line2 != 1 && eap->line2 > qf_get_valid_size(eap))
4046 || eap->line2 < 0)
4047 return _(e_invrange);
4048#endif
4049 break;
4050 case ADDR_UNSIGNED:
4051 if (eap->line2 < 0)
4052 return _(e_invrange);
4053 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02004054 case ADDR_NONE:
4055 // Will give an error elsewhere.
4056 break;
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004057 }
4058 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004059 return NULL;
4060}
4061
4062/*
4063 * Correct the range for zero line number, if required.
4064 */
4065 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004066correct_range(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004067{
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004068 if (!(eap->argt & EX_ZEROR)) // zero in range not allowed
Bram Moolenaar071d4272004-06-13 20:20:40 +00004069 {
4070 if (eap->line1 == 0)
4071 eap->line1 = 1;
4072 if (eap->line2 == 0)
4073 eap->line2 = 1;
4074 }
4075}
4076
Bram Moolenaar748bf032005-02-02 23:04:36 +00004077#ifdef FEAT_QUICKFIX
Bram Moolenaar748bf032005-02-02 23:04:36 +00004078/*
4079 * For a ":vimgrep" or ":vimgrepadd" command return a pointer past the
4080 * pattern. Otherwise return eap->arg.
4081 */
4082 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004083skip_grep_pat(exarg_T *eap)
Bram Moolenaar748bf032005-02-02 23:04:36 +00004084{
4085 char_u *p = eap->arg;
4086
Bram Moolenaara37420f2006-02-04 22:37:47 +00004087 if (*p != NUL && (eap->cmdidx == CMD_vimgrep || eap->cmdidx == CMD_lvimgrep
4088 || eap->cmdidx == CMD_vimgrepadd
4089 || eap->cmdidx == CMD_lvimgrepadd
4090 || grep_internal(eap->cmdidx)))
Bram Moolenaar748bf032005-02-02 23:04:36 +00004091 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00004092 p = skip_vimgrep_pat(p, NULL, NULL);
Bram Moolenaar748bf032005-02-02 23:04:36 +00004093 if (p == NULL)
4094 p = eap->arg;
Bram Moolenaar748bf032005-02-02 23:04:36 +00004095 }
4096 return p;
4097}
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004098
4099/*
4100 * For the ":make" and ":grep" commands insert the 'makeprg'/'grepprg' option
4101 * in the command line, so that things like % get expanded.
4102 */
4103 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004104replace_makeprg(exarg_T *eap, char_u *p, char_u **cmdlinep)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004105{
4106 char_u *new_cmdline;
4107 char_u *program;
4108 char_u *pos;
4109 char_u *ptr;
4110 int len;
4111 int i;
4112
4113 /*
4114 * Don't do it when ":vimgrep" is used for ":grep".
4115 */
Bram Moolenaara37420f2006-02-04 22:37:47 +00004116 if ((eap->cmdidx == CMD_make || eap->cmdidx == CMD_lmake
4117 || eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4118 || eap->cmdidx == CMD_grepadd
4119 || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004120 && !grep_internal(eap->cmdidx))
4121 {
Bram Moolenaara37420f2006-02-04 22:37:47 +00004122 if (eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4123 || eap->cmdidx == CMD_grepadd || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004124 {
4125 if (*curbuf->b_p_gp == NUL)
4126 program = p_gp;
4127 else
4128 program = curbuf->b_p_gp;
4129 }
4130 else
4131 {
4132 if (*curbuf->b_p_mp == NUL)
4133 program = p_mp;
4134 else
4135 program = curbuf->b_p_mp;
4136 }
4137
4138 p = skipwhite(p);
4139
4140 if ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4141 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004142 // replace $* by given arguments
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004143 i = 1;
4144 while ((pos = (char_u *)strstr((char *)pos + 2, "$*")) != NULL)
4145 ++i;
4146 len = (int)STRLEN(p);
Bram Moolenaar51e14382019-05-25 20:21:28 +02004147 new_cmdline = alloc(STRLEN(program) + i * (len - 2) + 1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004148 if (new_cmdline == NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004149 return NULL; // out of memory
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004150 ptr = new_cmdline;
4151 while ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4152 {
4153 i = (int)(pos - program);
4154 STRNCPY(ptr, program, i);
4155 STRCPY(ptr += i, p);
4156 ptr += len;
4157 program = pos + 2;
4158 }
4159 STRCPY(ptr, program);
4160 }
4161 else
4162 {
Bram Moolenaar51e14382019-05-25 20:21:28 +02004163 new_cmdline = alloc(STRLEN(program) + STRLEN(p) + 2);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004164 if (new_cmdline == NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004165 return NULL; // out of memory
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004166 STRCPY(new_cmdline, program);
4167 STRCAT(new_cmdline, " ");
4168 STRCAT(new_cmdline, p);
4169 }
4170 msg_make(p);
4171
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004172 // 'eap->cmd' is not set here, because it is not used at CMD_make
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004173 vim_free(*cmdlinep);
4174 *cmdlinep = new_cmdline;
4175 p = new_cmdline;
4176 }
4177 return p;
4178}
Bram Moolenaar748bf032005-02-02 23:04:36 +00004179#endif
4180
Bram Moolenaar071d4272004-06-13 20:20:40 +00004181/*
4182 * Expand file name in Ex command argument.
Bram Moolenaar0abb4272019-06-15 16:06:00 +02004183 * When an error is detected, "errormsgp" is set to a non-NULL pointer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004184 * Return FAIL for failure, OK otherwise.
4185 */
4186 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004187expand_filename(
4188 exarg_T *eap,
4189 char_u **cmdlinep,
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004190 char **errormsgp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004191{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004192 int has_wildcards; // need to expand wildcards
Bram Moolenaar071d4272004-06-13 20:20:40 +00004193 char_u *repl;
4194 int srclen;
4195 char_u *p;
4196 int n;
Bram Moolenaar63b92542007-03-27 14:57:09 +00004197 int escaped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004198
Bram Moolenaar748bf032005-02-02 23:04:36 +00004199#ifdef FEAT_QUICKFIX
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004200 // Skip a regexp pattern for ":vimgrep[add] pat file..."
Bram Moolenaar748bf032005-02-02 23:04:36 +00004201 p = skip_grep_pat(eap);
4202#else
4203 p = eap->arg;
4204#endif
4205
Bram Moolenaar071d4272004-06-13 20:20:40 +00004206 /*
4207 * Decide to expand wildcards *before* replacing '%', '#', etc. If
4208 * the file name contains a wildcard it should not cause expanding.
4209 * (it will be expanded anyway if there is a wildcard before replacing).
4210 */
Bram Moolenaar748bf032005-02-02 23:04:36 +00004211 has_wildcards = mch_has_wildcard(p);
4212 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004213 {
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004214#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004215 // Skip over `=expr`, wildcards in it are not expanded.
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004216 if (p[0] == '`' && p[1] == '=')
4217 {
4218 p += 2;
4219 (void)skip_expr(&p);
4220 if (*p == '`')
4221 ++p;
4222 continue;
4223 }
4224#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004225 /*
4226 * Quick check if this cannot be the start of a special string.
4227 * Also removes backslash before '%', '#' and '<'.
4228 */
4229 if (vim_strchr((char_u *)"%#<", *p) == NULL)
4230 {
4231 ++p;
4232 continue;
4233 }
4234
4235 /*
4236 * Try to find a match at this position.
4237 */
Bram Moolenaar63b92542007-03-27 14:57:09 +00004238 repl = eval_vars(p, eap->arg, &srclen, &(eap->do_ecmd_lnum),
4239 errormsgp, &escaped);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004240 if (*errormsgp != NULL) // error detected
Bram Moolenaar071d4272004-06-13 20:20:40 +00004241 return FAIL;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004242 if (repl == NULL) // no match found
Bram Moolenaar071d4272004-06-13 20:20:40 +00004243 {
4244 p += srclen;
4245 continue;
4246 }
4247
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004248 // Wildcards won't be expanded below, the replacement is taken
4249 // literally. But do expand "~/file", "~user/file" and "$HOME/file".
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00004250 if (vim_strchr(repl, '$') != NULL || vim_strchr(repl, '~') != NULL)
4251 {
4252 char_u *l = repl;
4253
4254 repl = expand_env_save(repl);
4255 vim_free(l);
4256 }
4257
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004258 // Need to escape white space et al. with a backslash.
4259 // Don't do this for:
4260 // - replacement that already has been escaped: "##"
4261 // - shell commands (may have to use quotes instead).
4262 // - non-unix systems when there is a single argument (spaces don't
4263 // separate arguments then).
Bram Moolenaar071d4272004-06-13 20:20:40 +00004264 if (!eap->usefilter
Bram Moolenaar63b92542007-03-27 14:57:09 +00004265 && !escaped
Bram Moolenaar071d4272004-06-13 20:20:40 +00004266 && eap->cmdidx != CMD_bang
Bram Moolenaar071d4272004-06-13 20:20:40 +00004267 && eap->cmdidx != CMD_grep
4268 && eap->cmdidx != CMD_grepadd
Bram Moolenaarbf15b8d2017-06-04 20:43:48 +02004269 && eap->cmdidx != CMD_hardcopy
Bram Moolenaar67883b42017-07-27 22:57:00 +02004270 && eap->cmdidx != CMD_lgrep
4271 && eap->cmdidx != CMD_lgrepadd
4272 && eap->cmdidx != CMD_lmake
4273 && eap->cmdidx != CMD_make
4274 && eap->cmdidx != CMD_terminal
Bram Moolenaar071d4272004-06-13 20:20:40 +00004275#ifndef UNIX
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004276 && !(eap->argt & EX_NOSPC)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004277#endif
4278 )
4279 {
4280 char_u *l;
4281#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004282 // Don't escape a backslash here, because rem_backslash() doesn't
4283 // remove it later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004284 static char_u *nobslash = (char_u *)" \t\"|";
4285# define ESCAPE_CHARS nobslash
4286#else
4287# define ESCAPE_CHARS escape_chars
4288#endif
4289
4290 for (l = repl; *l; ++l)
4291 if (vim_strchr(ESCAPE_CHARS, *l) != NULL)
4292 {
4293 l = vim_strsave_escaped(repl, ESCAPE_CHARS);
4294 if (l != NULL)
4295 {
4296 vim_free(repl);
4297 repl = l;
4298 }
4299 break;
4300 }
4301 }
4302
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004303 // For a shell command a '!' must be escaped.
Bram Moolenaar67883b42017-07-27 22:57:00 +02004304 if ((eap->usefilter || eap->cmdidx == CMD_bang
4305 || eap->cmdidx == CMD_terminal)
Bram Moolenaar31b7d382014-04-01 18:54:48 +02004306 && vim_strpbrk(repl, (char_u *)"!") != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004307 {
4308 char_u *l;
4309
Bram Moolenaar31b7d382014-04-01 18:54:48 +02004310 l = vim_strsave_escaped(repl, (char_u *)"!");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004311 if (l != NULL)
4312 {
4313 vim_free(repl);
4314 repl = l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004315 }
4316 }
4317
4318 p = repl_cmdline(eap, p, srclen, repl, cmdlinep);
4319 vim_free(repl);
4320 if (p == NULL)
4321 return FAIL;
4322 }
4323
4324 /*
4325 * One file argument: Expand wildcards.
4326 * Don't do this with ":r !command" or ":w !command".
4327 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004328 if ((eap->argt & EX_NOSPC) && !eap->usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004329 {
4330 /*
4331 * May do this twice:
4332 * 1. Replace environment variables.
4333 * 2. Replace any other wildcards, remove backslashes.
4334 */
4335 for (n = 1; n <= 2; ++n)
4336 {
4337 if (n == 2)
4338 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004339 /*
4340 * Halve the number of backslashes (this is Vi compatible).
4341 * For Unix and OS/2, when wildcards are expanded, this is
4342 * done by ExpandOne() below.
4343 */
Bram Moolenaare7fedb62015-12-31 19:07:19 +01004344#if defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004345 if (!has_wildcards)
4346#endif
4347 backslash_halve(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004348 }
4349
4350 if (has_wildcards)
4351 {
4352 if (n == 1)
4353 {
4354 /*
4355 * First loop: May expand environment variables. This
4356 * can be done much faster with expand_env() than with
4357 * something else (e.g., calling a shell).
4358 * After expanding environment variables, check again
4359 * if there are still wildcards present.
4360 */
4361 if (vim_strchr(eap->arg, '$') != NULL
4362 || vim_strchr(eap->arg, '~') != NULL)
4363 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +00004364 expand_env_esc(eap->arg, NameBuff, MAXPATHL,
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00004365 TRUE, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004366 has_wildcards = mch_has_wildcard(NameBuff);
4367 p = NameBuff;
4368 }
4369 else
4370 p = NULL;
4371 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004372 else // n == 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00004373 {
4374 expand_T xpc;
Bram Moolenaarb40c2572019-10-19 21:01:05 +02004375 int options = WILD_LIST_NOTFOUND
4376 | WILD_NOERROR | WILD_ADD_SLASH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004377
4378 ExpandInit(&xpc);
4379 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar94950a92010-12-02 16:01:29 +01004380 if (p_wic)
4381 options += WILD_ICASE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004382 p = ExpandOne(&xpc, eap->arg, NULL,
Bram Moolenaar94950a92010-12-02 16:01:29 +01004383 options, WILD_EXPAND_FREE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004384 if (p == NULL)
4385 return FAIL;
4386 }
4387 if (p != NULL)
4388 {
4389 (void)repl_cmdline(eap, eap->arg, (int)STRLEN(eap->arg),
4390 p, cmdlinep);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004391 if (n == 2) // p came from ExpandOne()
Bram Moolenaar071d4272004-06-13 20:20:40 +00004392 vim_free(p);
4393 }
4394 }
4395 }
4396 }
4397 return OK;
4398}
4399
4400/*
4401 * Replace part of the command line, keeping eap->cmd, eap->arg and
4402 * eap->nextcmd correct.
4403 * "src" points to the part that is to be replaced, of length "srclen".
4404 * "repl" is the replacement string.
4405 * Returns a pointer to the character after the replaced string.
4406 * Returns NULL for failure.
4407 */
4408 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004409repl_cmdline(
4410 exarg_T *eap,
4411 char_u *src,
4412 int srclen,
4413 char_u *repl,
4414 char_u **cmdlinep)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004415{
4416 int len;
4417 int i;
4418 char_u *new_cmdline;
4419
4420 /*
4421 * The new command line is build in new_cmdline[].
4422 * First allocate it.
4423 * Careful: a "+cmd" argument may have been NUL terminated.
4424 */
4425 len = (int)STRLEN(repl);
4426 i = (int)(src - *cmdlinep) + (int)STRLEN(src + srclen) + len + 3;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004427 if (eap->nextcmd != NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004428 i += (int)STRLEN(eap->nextcmd);// add space for next command
Bram Moolenaar964b3742019-05-24 18:54:09 +02004429 if ((new_cmdline = alloc(i)) == NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004430 return NULL; // out of memory!
Bram Moolenaar071d4272004-06-13 20:20:40 +00004431
4432 /*
4433 * Copy the stuff before the expanded part.
4434 * Copy the expanded stuff.
4435 * Copy what came after the expanded part.
4436 * Copy the next commands, if there are any.
4437 */
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004438 i = (int)(src - *cmdlinep); // length of part before match
Bram Moolenaar071d4272004-06-13 20:20:40 +00004439 mch_memmove(new_cmdline, *cmdlinep, (size_t)i);
Bram Moolenaara3ffd9c2005-07-21 21:03:15 +00004440
Bram Moolenaar071d4272004-06-13 20:20:40 +00004441 mch_memmove(new_cmdline + i, repl, (size_t)len);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004442 i += len; // remember the end of the string
Bram Moolenaar071d4272004-06-13 20:20:40 +00004443 STRCPY(new_cmdline + i, src + srclen);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004444 src = new_cmdline + i; // remember where to continue
Bram Moolenaar071d4272004-06-13 20:20:40 +00004445
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004446 if (eap->nextcmd != NULL) // append next command
Bram Moolenaar071d4272004-06-13 20:20:40 +00004447 {
4448 i = (int)STRLEN(new_cmdline) + 1;
4449 STRCPY(new_cmdline + i, eap->nextcmd);
4450 eap->nextcmd = new_cmdline + i;
4451 }
4452 eap->cmd = new_cmdline + (eap->cmd - *cmdlinep);
4453 eap->arg = new_cmdline + (eap->arg - *cmdlinep);
4454 if (eap->do_ecmd_cmd != NULL && eap->do_ecmd_cmd != dollar_command)
4455 eap->do_ecmd_cmd = new_cmdline + (eap->do_ecmd_cmd - *cmdlinep);
4456 vim_free(*cmdlinep);
4457 *cmdlinep = new_cmdline;
4458
4459 return src;
4460}
4461
4462/*
4463 * Check for '|' to separate commands and '"' to start comments.
4464 */
4465 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004466separate_nextcmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004467{
4468 char_u *p;
4469
Bram Moolenaar86b68352004-12-27 21:59:20 +00004470#ifdef FEAT_QUICKFIX
Bram Moolenaar748bf032005-02-02 23:04:36 +00004471 p = skip_grep_pat(eap);
4472#else
4473 p = eap->arg;
Bram Moolenaar86b68352004-12-27 21:59:20 +00004474#endif
4475
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004476 for ( ; *p; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004477 {
4478 if (*p == Ctrl_V)
4479 {
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004480 if (eap->argt & (EX_CTRLV | EX_XFILE))
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004481 ++p; // skip CTRL-V and next char
Bram Moolenaar071d4272004-06-13 20:20:40 +00004482 else
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004483 // remove CTRL-V and skip next char
Bram Moolenaara7241f52008-06-24 20:39:31 +00004484 STRMOVE(p, p + 1);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004485 if (*p == NUL) // stop at NUL after CTRL-V
Bram Moolenaar071d4272004-06-13 20:20:40 +00004486 break;
4487 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004488
4489#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004490 // Skip over `=expr` when wildcards are expanded.
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004491 else if (p[0] == '`' && p[1] == '=' && (eap->argt & EX_XFILE))
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004492 {
4493 p += 2;
4494 (void)skip_expr(&p);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004495 }
4496#endif
4497
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004498 // Check for '"': start of comment or '|': next command
4499 // :@" and :*" do not start a comment!
4500 // :redir @" doesn't either.
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004501 else if ((*p == '"' && !(eap->argt & EX_NOTRLCOM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004502 && ((eap->cmdidx != CMD_at && eap->cmdidx != CMD_star)
4503 || p != eap->arg)
4504 && (eap->cmdidx != CMD_redir
4505 || p != eap->arg + 1 || p[-1] != '@'))
Bram Moolenaara26b9702020-04-18 19:53:28 +02004506#ifdef FEAT_EVAL
4507 || (*p == '#' && in_vim9script()
4508 && p[1] != '{' && p > eap->cmd && VIM_ISWHITE(p[-1]))
4509#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004510 || *p == '|' || *p == '\n')
4511 {
4512 /*
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004513 * We remove the '\' before the '|', unless EX_CTRLV is used
Bram Moolenaar071d4272004-06-13 20:20:40 +00004514 * AND 'b' is present in 'cpoptions'.
4515 */
4516 if ((vim_strchr(p_cpo, CPO_BAR) == NULL
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004517 || !(eap->argt & EX_CTRLV)) && *(p - 1) == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004518 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004519 STRMOVE(p - 1, p); // remove the '\'
Bram Moolenaar071d4272004-06-13 20:20:40 +00004520 --p;
4521 }
4522 else
4523 {
4524 eap->nextcmd = check_nextcmd(p);
4525 *p = NUL;
4526 break;
4527 }
4528 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004529 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00004530
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004531 if (!(eap->argt & EX_NOTRLCOM)) // remove trailing spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00004532 del_trailing_spaces(eap->arg);
4533}
4534
4535/*
4536 * get + command from ex argument
4537 */
4538 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004539getargcmd(char_u **argp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004540{
4541 char_u *arg = *argp;
4542 char_u *command = NULL;
4543
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004544 if (*arg == '+') // +[command]
Bram Moolenaar071d4272004-06-13 20:20:40 +00004545 {
4546 ++arg;
Bram Moolenaar3e451592014-04-02 14:22:05 +02004547 if (vim_isspace(*arg) || *arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004548 command = dollar_command;
4549 else
4550 {
4551 command = arg;
4552 arg = skip_cmd_arg(command, TRUE);
4553 if (*arg != NUL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004554 *arg++ = NUL; // terminate command with NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004555 }
4556
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004557 arg = skipwhite(arg); // skip over spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00004558 *argp = arg;
4559 }
4560 return command;
4561}
4562
4563/*
4564 * Find end of "+command" argument. Skip over "\ " and "\\".
4565 */
Bram Moolenaard0190392019-08-23 21:17:35 +02004566 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004567skip_cmd_arg(
4568 char_u *p,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004569 int rembs) // TRUE to halve the number of backslashes
Bram Moolenaar071d4272004-06-13 20:20:40 +00004570{
4571 while (*p && !vim_isspace(*p))
4572 {
4573 if (*p == '\\' && p[1] != NUL)
4574 {
4575 if (rembs)
Bram Moolenaara7241f52008-06-24 20:39:31 +00004576 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004577 else
4578 ++p;
4579 }
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004580 MB_PTR_ADV(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004581 }
4582 return p;
4583}
4584
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004585 int
4586get_bad_opt(char_u *p, exarg_T *eap)
4587{
4588 if (STRICMP(p, "keep") == 0)
4589 eap->bad_char = BAD_KEEP;
4590 else if (STRICMP(p, "drop") == 0)
4591 eap->bad_char = BAD_DROP;
4592 else if (MB_BYTE2LEN(*p) == 1 && p[1] == NUL)
4593 eap->bad_char = *p;
Bram Moolenaar75808492018-06-12 12:39:41 +02004594 else
4595 return FAIL;
4596 return OK;
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004597}
4598
Bram Moolenaar071d4272004-06-13 20:20:40 +00004599/*
4600 * Get "++opt=arg" argument.
4601 * Return FAIL or OK.
4602 */
4603 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004604getargopt(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004605{
4606 char_u *arg = eap->arg + 2;
4607 int *pp = NULL;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02004608 int bad_char_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004609 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004610
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004611 // ":edit ++[no]bin[ary] file"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004612 if (STRNCMP(arg, "bin", 3) == 0 || STRNCMP(arg, "nobin", 5) == 0)
4613 {
4614 if (*arg == 'n')
4615 {
4616 arg += 2;
4617 eap->force_bin = FORCE_NOBIN;
4618 }
4619 else
4620 eap->force_bin = FORCE_BIN;
4621 if (!checkforcmd(&arg, "binary", 3))
4622 return FAIL;
4623 eap->arg = skipwhite(arg);
4624 return OK;
4625 }
4626
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004627 // ":read ++edit file"
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004628 if (STRNCMP(arg, "edit", 4) == 0)
4629 {
4630 eap->read_edit = TRUE;
4631 eap->arg = skipwhite(arg + 4);
4632 return OK;
4633 }
4634
Bram Moolenaar071d4272004-06-13 20:20:40 +00004635 if (STRNCMP(arg, "ff", 2) == 0)
4636 {
4637 arg += 2;
4638 pp = &eap->force_ff;
4639 }
4640 else if (STRNCMP(arg, "fileformat", 10) == 0)
4641 {
4642 arg += 10;
4643 pp = &eap->force_ff;
4644 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004645 else if (STRNCMP(arg, "enc", 3) == 0)
4646 {
Bram Moolenaarb38e9ab2011-12-14 14:49:45 +01004647 if (STRNCMP(arg, "encoding", 8) == 0)
4648 arg += 8;
4649 else
4650 arg += 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004651 pp = &eap->force_enc;
4652 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004653 else if (STRNCMP(arg, "bad", 3) == 0)
4654 {
4655 arg += 3;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02004656 pp = &bad_char_idx;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004657 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004658
4659 if (pp == NULL || *arg != '=')
4660 return FAIL;
4661
4662 ++arg;
4663 *pp = (int)(arg - eap->cmd);
4664 arg = skip_cmd_arg(arg, FALSE);
4665 eap->arg = skipwhite(arg);
4666 *arg = NUL;
4667
Bram Moolenaar071d4272004-06-13 20:20:40 +00004668 if (pp == &eap->force_ff)
4669 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004670 if (check_ff_value(eap->cmd + eap->force_ff) == FAIL)
4671 return FAIL;
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004672 eap->force_ff = eap->cmd[eap->force_ff];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004673 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004674 else if (pp == &eap->force_enc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004675 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004676 // Make 'fileencoding' lower case.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004677 for (p = eap->cmd + eap->force_enc; *p != NUL; ++p)
4678 *p = TOLOWER_ASC(*p);
4679 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004680 else
4681 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004682 // Check ++bad= argument. Must be a single-byte character, "keep" or
4683 // "drop".
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004684 if (get_bad_opt(eap->cmd + bad_char_idx, eap) == FAIL)
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004685 return FAIL;
4686 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004687
4688 return OK;
4689}
4690
Bram Moolenaar071d4272004-06-13 20:20:40 +00004691 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004692ex_autocmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004693{
4694 /*
Bram Moolenaar26d4b892018-07-04 22:26:28 +02004695 * Disallow autocommands from .exrc and .vimrc in current
Bram Moolenaar071d4272004-06-13 20:20:40 +00004696 * directory for security reasons.
4697 */
4698 if (secure)
4699 {
4700 secure = 2;
4701 eap->errmsg = e_curdir;
4702 }
4703 else if (eap->cmdidx == CMD_autocmd)
4704 do_autocmd(eap->arg, eap->forceit);
4705 else
4706 do_augroup(eap->arg, eap->forceit);
4707}
4708
4709/*
4710 * ":doautocmd": Apply the automatic commands to the current buffer.
4711 */
4712 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004713ex_doautocmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004714{
Bram Moolenaar60542ac2012-02-12 20:14:01 +01004715 char_u *arg = eap->arg;
4716 int call_do_modelines = check_nomodeline(&arg);
Bram Moolenaar1610d052016-06-09 22:53:01 +02004717 int did_aucmd;
Bram Moolenaar60542ac2012-02-12 20:14:01 +01004718
Bram Moolenaar1610d052016-06-09 22:53:01 +02004719 (void)do_doautocmd(arg, TRUE, &did_aucmd);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004720 // Only when there is no <nomodeline>.
Bram Moolenaar1610d052016-06-09 22:53:01 +02004721 if (call_do_modelines && did_aucmd)
Bram Moolenaar60542ac2012-02-12 20:14:01 +01004722 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004723}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004724
Bram Moolenaar071d4272004-06-13 20:20:40 +00004725/*
4726 * :[N]bunload[!] [N] [bufname] unload buffer
4727 * :[N]bdelete[!] [N] [bufname] delete buffer from buffer list
4728 * :[N]bwipeout[!] [N] [bufname] delete buffer really
4729 */
4730 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004731ex_bunload(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004732{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01004733 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02004734 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004735 eap->errmsg = do_bufdel(
4736 eap->cmdidx == CMD_bdelete ? DOBUF_DEL
4737 : eap->cmdidx == CMD_bwipeout ? DOBUF_WIPE
4738 : DOBUF_UNLOAD, eap->arg,
4739 eap->addr_count, (int)eap->line1, (int)eap->line2, eap->forceit);
4740}
4741
4742/*
4743 * :[N]buffer [N] to buffer N
4744 * :[N]sbuffer [N] to buffer N
4745 */
4746 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004747ex_buffer(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004748{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01004749 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02004750 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004751 if (*eap->arg)
4752 eap->errmsg = e_trailing;
4753 else
4754 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004755 if (eap->addr_count == 0) // default is current buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00004756 goto_buffer(eap, DOBUF_CURRENT, FORWARD, 0);
4757 else
4758 goto_buffer(eap, DOBUF_FIRST, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02004759 if (eap->do_ecmd_cmd != NULL)
4760 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004761 }
4762}
4763
4764/*
4765 * :[N]bmodified [N] to next mod. buffer
4766 * :[N]sbmodified [N] to next mod. buffer
4767 */
4768 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004769ex_bmodified(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004770{
4771 goto_buffer(eap, DOBUF_MOD, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02004772 if (eap->do_ecmd_cmd != NULL)
4773 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004774}
4775
4776/*
4777 * :[N]bnext [N] to next buffer
4778 * :[N]sbnext [N] split and to next buffer
4779 */
4780 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004781ex_bnext(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004782{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01004783 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar3029bcc2020-01-18 15:06:19 +01004784 return;
4785
Bram Moolenaar071d4272004-06-13 20:20:40 +00004786 goto_buffer(eap, DOBUF_CURRENT, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02004787 if (eap->do_ecmd_cmd != NULL)
4788 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004789}
4790
4791/*
4792 * :[N]bNext [N] to previous buffer
4793 * :[N]bprevious [N] to previous buffer
4794 * :[N]sbNext [N] split and to previous buffer
4795 * :[N]sbprevious [N] split and to previous buffer
4796 */
4797 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004798ex_bprevious(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004799{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01004800 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar3029bcc2020-01-18 15:06:19 +01004801 return;
4802
Bram Moolenaar071d4272004-06-13 20:20:40 +00004803 goto_buffer(eap, DOBUF_CURRENT, BACKWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02004804 if (eap->do_ecmd_cmd != NULL)
4805 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004806}
4807
4808/*
4809 * :brewind to first buffer
4810 * :bfirst to first buffer
4811 * :sbrewind split and to first buffer
4812 * :sbfirst split and to first buffer
4813 */
4814 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004815ex_brewind(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004816{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01004817 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar3029bcc2020-01-18 15:06:19 +01004818 return;
4819
Bram Moolenaar071d4272004-06-13 20:20:40 +00004820 goto_buffer(eap, DOBUF_FIRST, FORWARD, 0);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02004821 if (eap->do_ecmd_cmd != NULL)
4822 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004823}
4824
4825/*
4826 * :blast to last buffer
4827 * :sblast split and to last buffer
4828 */
4829 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004830ex_blast(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004831{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01004832 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar3029bcc2020-01-18 15:06:19 +01004833 return;
4834
Bram Moolenaar071d4272004-06-13 20:20:40 +00004835 goto_buffer(eap, DOBUF_LAST, BACKWARD, 0);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02004836 if (eap->do_ecmd_cmd != NULL)
4837 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004838}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004839
Bram Moolenaar7a092242020-04-16 22:10:49 +02004840/*
4841 * Check if "c" ends an Ex command.
Bram Moolenaara494f562020-04-18 17:45:38 +02004842 * In Vim9 script does not check for white space before # or #{.
Bram Moolenaar7a092242020-04-16 22:10:49 +02004843 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004844 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004845ends_excmd(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004846{
Bram Moolenaarfbda69b2020-04-13 15:06:53 +02004847#ifdef FEAT_EVAL
Bram Moolenaar2c330432020-04-13 14:41:35 +02004848 if (c == '#')
Bram Moolenaar7a092242020-04-16 22:10:49 +02004849 return in_vim9script();
4850#endif
4851 return (c == NUL || c == '|' || c == '"' || c == '\n');
4852}
4853
4854/*
4855 * Like ends_excmd() but checks that a # in Vim9 script either has "cmd" equal
4856 * to "cmd_start" or has a white space character before it.
4857 */
4858 int
Bram Moolenaar21cfe502020-04-16 23:01:50 +02004859ends_excmd2(char_u *cmd_start UNUSED, char_u *cmd)
Bram Moolenaar7a092242020-04-16 22:10:49 +02004860{
4861 int c = *cmd;
4862
4863#ifdef FEAT_EVAL
Bram Moolenaara26b9702020-04-18 19:53:28 +02004864 if (c == '#' && cmd[1] != '{' && (cmd == cmd_start || VIM_ISWHITE(cmd[-1])))
Bram Moolenaar2c330432020-04-13 14:41:35 +02004865 return in_vim9script();
Bram Moolenaarfbda69b2020-04-13 15:06:53 +02004866#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004867 return (c == NUL || c == '|' || c == '"' || c == '\n');
4868}
4869
4870#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA) || defined(FEAT_EVAL) \
4871 || defined(PROTO)
4872/*
4873 * Return the next command, after the first '|' or '\n'.
4874 * Return NULL if not found.
4875 */
4876 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004877find_nextcmd(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004878{
4879 while (*p != '|' && *p != '\n')
4880 {
4881 if (*p == NUL)
4882 return NULL;
4883 ++p;
4884 }
4885 return (p + 1);
4886}
4887#endif
4888
4889/*
Bram Moolenaar2256c992016-11-15 21:17:07 +01004890 * Check if *p is a separator between Ex commands, skipping over white space.
4891 * Return NULL if it isn't, the following character if it is.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004892 */
4893 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004894check_nextcmd(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004895{
Bram Moolenaar2256c992016-11-15 21:17:07 +01004896 char_u *s = skipwhite(p);
4897
4898 if (*s == '|' || *s == '\n')
4899 return (s + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004900 else
4901 return NULL;
4902}
4903
4904/*
4905 * - if there are more files to edit
4906 * - and this is the last window
4907 * - and forceit not used
4908 * - and not repeated twice on a row
4909 * return FAIL and give error message if 'message' TRUE
4910 * return OK otherwise
4911 */
4912 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004913check_more(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004914 int message, // when FALSE check only, no messages
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004915 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004916{
4917 int n = ARGCOUNT - curwin->w_arg_idx - 1;
4918
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00004919 if (!forceit && only_one_window()
4920 && ARGCOUNT > 1 && !arg_had_last && n >= 0 && quitmore == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004921 {
4922 if (message)
4923 {
4924#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
4925 if ((p_confirm || cmdmod.confirm) && curbuf->b_fname != NULL)
4926 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02004927 char_u buff[DIALOG_MSG_SIZE];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004928
Bram Moolenaarda6e8912018-08-21 15:12:14 +02004929 vim_snprintf((char *)buff, DIALOG_MSG_SIZE,
4930 NGETTEXT("%d more file to edit. Quit anyway?",
4931 "%d more files to edit. Quit anyway?", n), n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004932 if (vim_dialog_yesno(VIM_QUESTION, NULL, buff, 1) == VIM_YES)
4933 return OK;
4934 return FAIL;
4935 }
4936#endif
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01004937 semsg(NGETTEXT("E173: %d more file to edit",
4938 "E173: %d more files to edit", n), n);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004939 quitmore = 2; // next try to quit is allowed
Bram Moolenaar071d4272004-06-13 20:20:40 +00004940 }
4941 return FAIL;
4942 }
4943 return OK;
4944}
4945
Bram Moolenaar071d4272004-06-13 20:20:40 +00004946/*
4947 * Function given to ExpandGeneric() to obtain the list of command names.
4948 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004949 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004950get_command_name(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004951{
4952 if (idx >= (int)CMD_SIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004953 return get_user_command_name(idx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004954 return cmdnames[idx].cmd_name;
4955}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004956
Bram Moolenaar071d4272004-06-13 20:20:40 +00004957 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004958ex_colorscheme(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004959{
Bram Moolenaare6850792010-05-14 15:28:44 +02004960 if (*eap->arg == NUL)
4961 {
4962#ifdef FEAT_EVAL
4963 char_u *expr = vim_strsave((char_u *)"g:colors_name");
4964 char_u *p = NULL;
4965
4966 if (expr != NULL)
4967 {
4968 ++emsg_off;
Bram Moolenaarb171fb12020-06-24 20:34:03 +02004969 p = eval_to_string(expr, FALSE);
Bram Moolenaare6850792010-05-14 15:28:44 +02004970 --emsg_off;
4971 vim_free(expr);
4972 }
4973 if (p != NULL)
4974 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01004975 msg((char *)p);
Bram Moolenaare6850792010-05-14 15:28:44 +02004976 vim_free(p);
4977 }
4978 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01004979 msg("default");
Bram Moolenaare6850792010-05-14 15:28:44 +02004980#else
Bram Moolenaar32526b32019-01-19 17:43:09 +01004981 msg(_("unknown"));
Bram Moolenaare6850792010-05-14 15:28:44 +02004982#endif
4983 }
4984 else if (load_colors(eap->arg) == FAIL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004985 semsg(_("E185: Cannot find color scheme '%s'"), eap->arg);
Bram Moolenaarf58d81a2019-01-28 20:19:05 +01004986
4987#ifdef FEAT_VTP
4988 else if (has_vtp_working())
4989 {
4990 // background color change requires clear + redraw
4991 update_screen(CLEAR);
4992 redrawcmd();
4993 }
4994#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004995}
4996
4997 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004998ex_highlight(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004999{
5000 if (*eap->arg == NUL && eap->cmd[2] == '!')
Bram Moolenaar32526b32019-01-19 17:43:09 +01005001 msg(_("Greetings, Vim user!"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005002 do_highlight(eap->arg, eap->forceit, FALSE);
5003}
5004
5005
5006/*
5007 * Call this function if we thought we were going to exit, but we won't
5008 * (because of an error). May need to restore the terminal mode.
5009 */
5010 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005011not_exiting(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005012{
5013 exiting = FALSE;
5014 settmode(TMODE_RAW);
5015}
5016
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005017 static int
5018before_quit_autocmds(win_T *wp, int quit_all, int forceit)
5019{
5020 apply_autocmds(EVENT_QUITPRE, NULL, NULL, FALSE, wp->w_buffer);
5021
Bram Moolenaar34ba06b2019-10-20 22:27:10 +02005022 // Bail out when autocommands closed the window.
5023 // Refuse to quit when the buffer in the last window is being closed (can
5024 // only happen in autocommands).
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005025 if (!win_valid(wp)
5026 || curbuf_locked()
5027 || (wp->w_buffer->b_nwindows == 1 && wp->w_buffer->b_locked > 0))
5028 return TRUE;
5029
5030 if (quit_all || (check_more(FALSE, forceit) == OK && only_one_window()))
5031 {
5032 apply_autocmds(EVENT_EXITPRE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar34ba06b2019-10-20 22:27:10 +02005033 // Refuse to quit when locked or when the window was closed or the
5034 // buffer in the last window is being closed (can only happen in
5035 // autocommands).
5036 if (!win_valid(wp) || curbuf_locked()
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005037 || (curbuf->b_nwindows == 1 && curbuf->b_locked > 0))
5038 return TRUE;
5039 }
5040
5041 return FALSE;
5042}
5043
Bram Moolenaar071d4272004-06-13 20:20:40 +00005044/*
Bram Moolenaarf240e182014-11-27 18:33:02 +01005045 * ":quit": quit current window, quit Vim if the last window is closed.
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005046 * ":{nr}quit": quit window {nr}
Bram Moolenaar4d14bac2019-10-20 21:15:15 +02005047 * Also used when closing a terminal window that's the last one.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005048 */
Bram Moolenaar4d14bac2019-10-20 21:15:15 +02005049 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005050ex_quit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005051{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005052 win_T *wp;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005053
Bram Moolenaar071d4272004-06-13 20:20:40 +00005054#ifdef FEAT_CMDWIN
5055 if (cmdwin_type != 0)
5056 {
5057 cmdwin_result = Ctrl_C;
5058 return;
5059 }
5060#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005061 // Don't quit while editing the command line.
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005062 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005063 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005064 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005065 return;
5066 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005067 if (eap->addr_count > 0)
5068 {
Bram Moolenaarf240e182014-11-27 18:33:02 +01005069 int wnr = eap->line2;
5070
5071 for (wp = firstwin; wp->w_next != NULL; wp = wp->w_next)
5072 if (--wnr <= 0)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005073 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005074 }
5075 else
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005076 wp = curwin;
Bram Moolenaarf240e182014-11-27 18:33:02 +01005077
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005078 // Refuse to quit when locked.
Bram Moolenaar87ffb5c2017-10-19 12:37:42 +02005079 if (curbuf_locked())
5080 return;
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005081
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005082 // Trigger QuitPre and maybe ExitPre
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005083 if (before_quit_autocmds(wp, FALSE, eap->forceit))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005084 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005085
5086#ifdef FEAT_NETBEANS_INTG
5087 netbeansForcedQuit = eap->forceit;
5088#endif
5089
5090 /*
5091 * If there are more files or windows we won't exit.
5092 */
5093 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
5094 exiting = TRUE;
Bram Moolenaarff930ca2017-10-19 17:12:10 +02005095 if ((!buf_hide(wp->w_buffer)
5096 && check_changed(wp->w_buffer, (p_awa ? CCGD_AW : 0)
Bram Moolenaar45d3b142013-11-09 03:31:51 +01005097 | (eap->forceit ? CCGD_FORCEIT : 0)
5098 | CCGD_EXCMD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005099 || check_more(TRUE, eap->forceit) == FAIL
Bram Moolenaar027387f2016-01-02 22:25:52 +01005100 || (only_one_window() && check_changed_any(eap->forceit, TRUE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005101 {
5102 not_exiting();
5103 }
5104 else
5105 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005106 // quit last window
5107 // Note: only_one_window() returns true, even so a help window is
5108 // still open. In that case only quit, if no address has been
5109 // specified. Example:
5110 // :h|wincmd w|1q - don't quit
5111 // :h|wincmd w|q - quit
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01005112 if (only_one_window() && (ONE_WINDOW || eap->addr_count == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005113 getout(0);
Bram Moolenaar2c33d7b2017-10-14 16:06:20 +02005114 not_exiting();
Bram Moolenaar4033c552017-09-16 20:54:51 +02005115#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00005116 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005117#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005118 // close window; may free buffer
Bram Moolenaareb44a682017-08-03 22:44:55 +02005119 win_close(wp, !buf_hide(wp->w_buffer) || eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005120 }
5121}
5122
5123/*
5124 * ":cquit".
5125 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005126 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005127ex_cquit(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005128{
Bram Moolenaar1860bde2020-01-06 21:47:21 +01005129 // this does not always pass on the exit code to the Manx compiler. why?
5130 getout(eap->addr_count > 0 ? (int)eap->line2 : EXIT_FAILURE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005131}
5132
5133/*
5134 * ":qall": try to quit all windows
5135 */
5136 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005137ex_quit_all(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005138{
5139# ifdef FEAT_CMDWIN
5140 if (cmdwin_type != 0)
5141 {
5142 if (eap->forceit)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005143 cmdwin_result = K_XF1; // ex_window() takes care of this
Bram Moolenaar071d4272004-06-13 20:20:40 +00005144 else
5145 cmdwin_result = K_XF2;
5146 return;
5147 }
5148# endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005149
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005150 // Don't quit while editing the command line.
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005151 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005152 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005153 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005154 return;
5155 }
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005156
5157 if (before_quit_autocmds(curwin, TRUE, eap->forceit))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005158 return;
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005159
Bram Moolenaar071d4272004-06-13 20:20:40 +00005160 exiting = TRUE;
Bram Moolenaar027387f2016-01-02 22:25:52 +01005161 if (eap->forceit || !check_changed_any(FALSE, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005162 getout(0);
5163 not_exiting();
5164}
5165
Bram Moolenaar071d4272004-06-13 20:20:40 +00005166/*
5167 * ":close": close current window, unless it is the last one
5168 */
5169 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005170ex_close(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005171{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005172 win_T *win;
5173 int winnr = 0;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005174#ifdef FEAT_CMDWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00005175 if (cmdwin_type != 0)
Bram Moolenaar9bd1a7e2011-05-19 14:50:54 +02005176 cmdwin_result = Ctrl_C;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005177 else
Bram Moolenaar4033c552017-09-16 20:54:51 +02005178#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005179 if (!text_locked() && !curbuf_locked())
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005180 {
5181 if (eap->addr_count == 0)
5182 ex_win_close(eap->forceit, curwin, NULL);
Bram Moolenaar6d41c782018-06-06 09:11:12 +02005183 else
5184 {
Bram Moolenaar29323592016-07-24 22:04:11 +02005185 FOR_ALL_WINDOWS(win)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005186 {
5187 winnr++;
5188 if (winnr == eap->line2)
5189 break;
5190 }
5191 if (win == NULL)
5192 win = lastwin;
5193 ex_win_close(eap->forceit, win, NULL);
5194 }
5195 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005196}
5197
Bram Moolenaar4033c552017-09-16 20:54:51 +02005198#ifdef FEAT_QUICKFIX
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005199/*
5200 * ":pclose": Close any preview window.
5201 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005202 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005203ex_pclose(exarg_T *eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005204{
5205 win_T *win;
5206
Bram Moolenaar79648732019-07-18 21:43:07 +02005207 // First close any normal window.
Bram Moolenaar29323592016-07-24 22:04:11 +02005208 FOR_ALL_WINDOWS(win)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005209 if (win->w_p_pvw)
5210 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00005211 ex_win_close(eap->forceit, win, NULL);
Bram Moolenaar79648732019-07-18 21:43:07 +02005212 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005213 }
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01005214# ifdef FEAT_PROP_POPUP
Bram Moolenaar79648732019-07-18 21:43:07 +02005215 // Also when 'previewpopup' is empty, it might have been cleared.
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02005216 popup_close_preview();
Bram Moolenaar79648732019-07-18 21:43:07 +02005217# endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005218}
Bram Moolenaar4033c552017-09-16 20:54:51 +02005219#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005220
Bram Moolenaarf740b292006-02-16 22:11:02 +00005221/*
5222 * Close window "win" and take care of handling closing the last window for a
5223 * modified buffer.
5224 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005225 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005226ex_win_close(
5227 int forceit,
5228 win_T *win,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005229 tabpage_T *tp) // NULL or the tab page "win" is in
Bram Moolenaar071d4272004-06-13 20:20:40 +00005230{
5231 int need_hide;
5232 buf_T *buf = win->w_buffer;
5233
Bram Moolenaarcf844172020-06-26 19:44:06 +02005234 // Never close the autocommand window.
5235 if (win == aucmd_win)
5236 {
5237 emsg(_(e_autocmd_close));
5238 return;
5239 }
5240
Bram Moolenaar071d4272004-06-13 20:20:40 +00005241 need_hide = (bufIsChanged(buf) && buf->b_nwindows <= 1);
Bram Moolenaareb44a682017-08-03 22:44:55 +02005242 if (need_hide && !buf_hide(buf) && !forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005243 {
Bram Moolenaar4033c552017-09-16 20:54:51 +02005244#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005245 if ((p_confirm || cmdmod.confirm) && p_write)
5246 {
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005247 bufref_T bufref;
5248
5249 set_bufref(&bufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005250 dialog_changed(buf, FALSE);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005251 if (bufref_valid(&bufref) && bufIsChanged(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005252 return;
5253 need_hide = FALSE;
5254 }
5255 else
Bram Moolenaar4033c552017-09-16 20:54:51 +02005256#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005257 {
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02005258 no_write_message();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005259 return;
5260 }
5261 }
5262
Bram Moolenaar4033c552017-09-16 20:54:51 +02005263#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00005264 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005265#endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00005266
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005267 // free buffer when not hiding it or when it's a scratch buffer
Bram Moolenaarf740b292006-02-16 22:11:02 +00005268 if (tp == NULL)
Bram Moolenaareb44a682017-08-03 22:44:55 +02005269 win_close(win, !need_hide && !buf_hide(buf));
Bram Moolenaarf740b292006-02-16 22:11:02 +00005270 else
Bram Moolenaareb44a682017-08-03 22:44:55 +02005271 win_close_othertab(win, !need_hide && !buf_hide(buf), tp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005272}
5273
Bram Moolenaar071d4272004-06-13 20:20:40 +00005274/*
Bram Moolenaardea25702017-01-29 15:18:10 +01005275 * Handle the argument for a tabpage related ex command.
5276 * Returns a tabpage number.
5277 * When an error is encountered then eap->errmsg is set.
5278 */
5279 static int
5280get_tabpage_arg(exarg_T *eap)
5281{
5282 int tab_number;
5283 int unaccept_arg0 = (eap->cmdidx == CMD_tabmove) ? 0 : 1;
5284
5285 if (eap->arg && *eap->arg != NUL)
5286 {
5287 char_u *p = eap->arg;
5288 char_u *p_save;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005289 int relative = 0; // argument +N/-N means: go to N places to the
5290 // right/left relative to the current position.
Bram Moolenaardea25702017-01-29 15:18:10 +01005291
5292 if (*p == '-')
5293 {
5294 relative = -1;
5295 p++;
5296 }
5297 else if (*p == '+')
5298 {
5299 relative = 1;
5300 p++;
5301 }
5302
5303 p_save = p;
5304 tab_number = getdigits(&p);
5305
5306 if (relative == 0)
5307 {
5308 if (STRCMP(p, "$") == 0)
5309 tab_number = LAST_TAB_NR;
5310 else if (p == p_save || *p_save == '-' || *p != NUL
5311 || tab_number > LAST_TAB_NR)
5312 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005313 // No numbers as argument.
Bram Moolenaardea25702017-01-29 15:18:10 +01005314 eap->errmsg = e_invarg;
5315 goto theend;
5316 }
5317 }
5318 else
5319 {
5320 if (*p_save == NUL)
5321 tab_number = 1;
5322 else if (p == p_save || *p_save == '-' || *p != NUL
5323 || tab_number == 0)
5324 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005325 // No numbers as argument.
Bram Moolenaardea25702017-01-29 15:18:10 +01005326 eap->errmsg = e_invarg;
5327 goto theend;
5328 }
5329 tab_number = tab_number * relative + tabpage_index(curtab);
5330 if (!unaccept_arg0 && relative == -1)
5331 --tab_number;
5332 }
5333 if (tab_number < unaccept_arg0 || tab_number > LAST_TAB_NR)
5334 eap->errmsg = e_invarg;
5335 }
5336 else if (eap->addr_count > 0)
5337 {
5338 if (unaccept_arg0 && eap->line2 == 0)
Bram Moolenaarc6251552017-01-29 21:42:20 +01005339 {
Bram Moolenaardea25702017-01-29 15:18:10 +01005340 eap->errmsg = e_invrange;
Bram Moolenaarc6251552017-01-29 21:42:20 +01005341 tab_number = 0;
5342 }
Bram Moolenaardea25702017-01-29 15:18:10 +01005343 else
5344 {
5345 tab_number = eap->line2;
Bram Moolenaar82a12462019-01-22 22:41:42 +01005346 if (!unaccept_arg0 && *skipwhite(*eap->cmdlinep) == '-')
Bram Moolenaardea25702017-01-29 15:18:10 +01005347 {
5348 --tab_number;
5349 if (tab_number < unaccept_arg0)
5350 eap->errmsg = e_invarg;
5351 }
5352 }
5353 }
5354 else
5355 {
5356 switch (eap->cmdidx)
5357 {
5358 case CMD_tabnext:
5359 tab_number = tabpage_index(curtab) + 1;
5360 if (tab_number > LAST_TAB_NR)
5361 tab_number = 1;
5362 break;
5363 case CMD_tabmove:
5364 tab_number = LAST_TAB_NR;
5365 break;
5366 default:
5367 tab_number = tabpage_index(curtab);
5368 }
5369 }
5370
5371theend:
5372 return tab_number;
5373}
5374
5375/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00005376 * ":tabclose": close current tab page, unless it is the last one.
5377 * ":tabclose N": close tab page N.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005378 */
5379 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005380ex_tabclose(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005381{
Bram Moolenaarf740b292006-02-16 22:11:02 +00005382 tabpage_T *tp;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005383 int tab_number;
Bram Moolenaarf740b292006-02-16 22:11:02 +00005384
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005385# ifdef FEAT_CMDWIN
5386 if (cmdwin_type != 0)
5387 cmdwin_result = K_IGNORE;
5388 else
5389# endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00005390 if (first_tabpage->tp_next == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005391 emsg(_("E784: Cannot close last tab page"));
Bram Moolenaarf740b292006-02-16 22:11:02 +00005392 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005393 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005394 tab_number = get_tabpage_arg(eap);
5395 if (eap->errmsg == NULL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005396 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005397 tp = find_tabpage(tab_number);
Bram Moolenaarf740b292006-02-16 22:11:02 +00005398 if (tp == NULL)
5399 {
5400 beep_flush();
5401 return;
5402 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005403 if (tp != curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00005404 {
5405 tabpage_close_other(tp, eap->forceit);
5406 return;
5407 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005408 else if (!text_locked() && !curbuf_locked())
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005409 tabpage_close(eap->forceit);
5410 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005411 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005412}
5413
5414/*
5415 * ":tabonly": close all tab pages except the current one
5416 */
5417 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005418ex_tabonly(exarg_T *eap)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005419{
5420 tabpage_T *tp;
5421 int done;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005422 int tab_number;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005423
5424# ifdef FEAT_CMDWIN
5425 if (cmdwin_type != 0)
5426 cmdwin_result = K_IGNORE;
5427 else
5428# endif
5429 if (first_tabpage->tp_next == NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01005430 msg(_("Already only one tab page"));
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005431 else
5432 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005433 tab_number = get_tabpage_arg(eap);
5434 if (eap->errmsg == NULL)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005435 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005436 goto_tabpage(tab_number);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005437 // Repeat this up to a 1000 times, because autocommands may
5438 // mess up the lists.
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005439 for (done = 0; done < 1000; ++done)
5440 {
5441 FOR_ALL_TABPAGES(tp)
5442 if (tp->tp_topframe != topframe)
5443 {
5444 tabpage_close_other(tp, eap->forceit);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005445 // if we failed to close it quit
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005446 if (valid_tabpage(tp))
5447 done = 1000;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005448 // start over, "tp" is now invalid
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005449 break;
5450 }
5451 if (first_tabpage->tp_next == NULL)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005452 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005453 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005454 }
5455 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005456}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005457
5458/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00005459 * Close the current tab page.
5460 */
5461 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005462tabpage_close(int forceit)
Bram Moolenaarf740b292006-02-16 22:11:02 +00005463{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005464 // First close all the windows but the current one. If that worked then
5465 // close the last window in this tab, that will close it.
Bram Moolenaar459ca562016-11-10 18:16:33 +01005466 if (!ONE_WINDOW)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005467 close_others(TRUE, forceit);
Bram Moolenaar459ca562016-11-10 18:16:33 +01005468 if (ONE_WINDOW)
Bram Moolenaarf740b292006-02-16 22:11:02 +00005469 ex_win_close(forceit, curwin, NULL);
5470# ifdef FEAT_GUI
5471 need_mouse_correct = TRUE;
5472# endif
5473}
5474
5475/*
5476 * Close tab page "tp", which is not the current tab page.
5477 * Note that autocommands may make "tp" invalid.
Bram Moolenaar7875acc2006-09-10 13:51:17 +00005478 * Also takes care of the tab pages line disappearing when closing the
5479 * last-but-one tab page.
Bram Moolenaarf740b292006-02-16 22:11:02 +00005480 */
5481 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005482tabpage_close_other(tabpage_T *tp, int forceit)
Bram Moolenaarf740b292006-02-16 22:11:02 +00005483{
5484 int done = 0;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005485 win_T *wp;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00005486 int h = tabline_height();
Bram Moolenaarf740b292006-02-16 22:11:02 +00005487
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005488 // Limit to 1000 windows, autocommands may add a window while we close
5489 // one. OK, so I'm paranoid...
Bram Moolenaarf740b292006-02-16 22:11:02 +00005490 while (++done < 1000)
5491 {
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005492 wp = tp->tp_firstwin;
5493 ex_win_close(forceit, wp, tp);
Bram Moolenaarf740b292006-02-16 22:11:02 +00005494
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005495 // Autocommands may delete the tab page under our fingers and we may
5496 // fail to close a window with a modified buffer.
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005497 if (!valid_tabpage(tp) || tp->tp_firstwin == wp)
Bram Moolenaarf740b292006-02-16 22:11:02 +00005498 break;
5499 }
Bram Moolenaar7875acc2006-09-10 13:51:17 +00005500
Bram Moolenaar29323592016-07-24 22:04:11 +02005501 apply_autocmds(EVENT_TABCLOSED, NULL, NULL, FALSE, curbuf);
Bram Moolenaar29323592016-07-24 22:04:11 +02005502
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005503 redraw_tabline = TRUE;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00005504 if (h != tabline_height())
5505 shell_new_rows();
Bram Moolenaarf740b292006-02-16 22:11:02 +00005506}
5507
5508/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005509 * ":only".
5510 */
5511 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005512ex_only(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005513{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005514 win_T *wp;
5515 int wnr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005516# ifdef FEAT_GUI
5517 need_mouse_correct = TRUE;
5518# endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005519 if (eap->addr_count > 0)
5520 {
5521 wnr = eap->line2;
5522 for (wp = firstwin; --wnr > 0; )
5523 {
5524 if (wp->w_next == NULL)
5525 break;
5526 else
5527 wp = wp->w_next;
5528 }
5529 win_goto(wp);
5530 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005531 close_others(TRUE, eap->forceit);
5532}
5533
Bram Moolenaar071d4272004-06-13 20:20:40 +00005534 static void
Bram Moolenaar5e1e6d22017-01-02 17:26:00 +01005535ex_hide(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005536{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005537 // ":hide" or ":hide | cmd": hide current window
Bram Moolenaar2256c992016-11-15 21:17:07 +01005538 if (!eap->skip)
5539 {
Bram Moolenaar4033c552017-09-16 20:54:51 +02005540#ifdef FEAT_GUI
Bram Moolenaar2256c992016-11-15 21:17:07 +01005541 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005542#endif
Bram Moolenaar2256c992016-11-15 21:17:07 +01005543 if (eap->addr_count == 0)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005544 win_close(curwin, FALSE); // don't free buffer
Bram Moolenaar2256c992016-11-15 21:17:07 +01005545 else
5546 {
5547 int winnr = 0;
5548 win_T *win;
Bram Moolenaarf240e182014-11-27 18:33:02 +01005549
Bram Moolenaar2256c992016-11-15 21:17:07 +01005550 FOR_ALL_WINDOWS(win)
5551 {
5552 winnr++;
5553 if (winnr == eap->line2)
5554 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005555 }
Bram Moolenaar2256c992016-11-15 21:17:07 +01005556 if (win == NULL)
5557 win = lastwin;
5558 win_close(win, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005559 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005560 }
5561}
5562
5563/*
5564 * ":stop" and ":suspend": Suspend Vim.
5565 */
5566 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005567ex_stop(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005568{
5569 /*
5570 * Disallow suspending for "rvim".
5571 */
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005572 if (!check_restricted())
Bram Moolenaar071d4272004-06-13 20:20:40 +00005573 {
5574 if (!eap->forceit)
5575 autowrite_all();
5576 windgoto((int)Rows - 1, 0);
5577 out_char('\n');
5578 out_flush();
5579 stoptermcap();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005580 out_flush(); // needed for SUN to restore xterm buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00005581#ifdef FEAT_TITLE
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005582 mch_restore_title(SAVE_RESTORE_BOTH); // restore window titles
Bram Moolenaar071d4272004-06-13 20:20:40 +00005583#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005584 ui_suspend(); // call machine specific function
Bram Moolenaar071d4272004-06-13 20:20:40 +00005585#ifdef FEAT_TITLE
5586 maketitle();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005587 resettitle(); // force updating the title
Bram Moolenaar071d4272004-06-13 20:20:40 +00005588#endif
5589 starttermcap();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005590 scroll_start(); // scroll screen before redrawing
Bram Moolenaar071d4272004-06-13 20:20:40 +00005591 redraw_later_clear();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005592 shell_resized(); // may have resized window
Bram Moolenaar071d4272004-06-13 20:20:40 +00005593 }
5594}
5595
5596/*
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005597 * ":exit", ":xit" and ":wq": Write file and quite the current window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005598 */
5599 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005600ex_exit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005601{
5602#ifdef FEAT_CMDWIN
5603 if (cmdwin_type != 0)
5604 {
5605 cmdwin_result = Ctrl_C;
5606 return;
5607 }
5608#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005609 // Don't quit while editing the command line.
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005610 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005611 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005612 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005613 return;
5614 }
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005615
5616 if (before_quit_autocmds(curwin, FALSE, eap->forceit))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005617 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005618
5619 /*
5620 * if more files or windows we won't exit
5621 */
5622 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
5623 exiting = TRUE;
5624 if ( ((eap->cmdidx == CMD_wq
5625 || curbufIsChanged())
5626 && do_write(eap) == FAIL)
5627 || check_more(TRUE, eap->forceit) == FAIL
Bram Moolenaar027387f2016-01-02 22:25:52 +01005628 || (only_one_window() && check_changed_any(eap->forceit, FALSE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005629 {
5630 not_exiting();
5631 }
5632 else
5633 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005634 if (only_one_window()) // quit last window, exit Vim
Bram Moolenaar071d4272004-06-13 20:20:40 +00005635 getout(0);
Bram Moolenaar2c33d7b2017-10-14 16:06:20 +02005636 not_exiting();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005637# ifdef FEAT_GUI
5638 need_mouse_correct = TRUE;
5639# endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005640 // Quit current window, may free the buffer.
Bram Moolenaareb44a682017-08-03 22:44:55 +02005641 win_close(curwin, !buf_hide(curwin->w_buffer));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005642 }
5643}
5644
5645/*
5646 * ":print", ":list", ":number".
5647 */
5648 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005649ex_print(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005650{
Bram Moolenaardf177f62005-02-22 08:39:57 +00005651 if (curbuf->b_ml.ml_flags & ML_EMPTY)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005652 emsg(_(e_emptybuf));
Bram Moolenaardf177f62005-02-22 08:39:57 +00005653 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005654 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00005655 for ( ;!got_int; ui_breakcheck())
5656 {
5657 print_line(eap->line1,
5658 (eap->cmdidx == CMD_number || eap->cmdidx == CMD_pound
5659 || (eap->flags & EXFLAG_NR)),
5660 eap->cmdidx == CMD_list || (eap->flags & EXFLAG_LIST));
5661 if (++eap->line1 > eap->line2)
5662 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005663 out_flush(); // show one line at a time
Bram Moolenaardf177f62005-02-22 08:39:57 +00005664 }
5665 setpcmark();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005666 // put cursor at last line
Bram Moolenaardf177f62005-02-22 08:39:57 +00005667 curwin->w_cursor.lnum = eap->line2;
5668 beginline(BL_SOL | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005669 }
5670
Bram Moolenaar071d4272004-06-13 20:20:40 +00005671 ex_no_reprint = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005672}
5673
5674#ifdef FEAT_BYTEOFF
5675 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005676ex_goto(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005677{
5678 goto_byte(eap->line2);
5679}
5680#endif
5681
5682/*
5683 * ":shell".
5684 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005685 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005686ex_shell(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005687{
5688 do_shell(NULL, 0);
5689}
5690
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005691#if defined(HAVE_DROP_FILE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005692
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005693static int drop_busy = FALSE;
5694static int drop_filec;
5695static char_u **drop_filev = NULL;
5696static int drop_split;
5697static void (*drop_callback)(void *);
5698static void *drop_cookie;
5699
5700 static void
5701handle_drop_internal(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005702{
5703 exarg_T ea;
5704 int save_msg_scroll = msg_scroll;
5705
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005706 // Setting the argument list may cause screen updates and being called
5707 // recursively. Avoid that by setting drop_busy.
5708 drop_busy = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005709
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005710 // Check whether the current buffer is changed. If so, we will need
5711 // to split the current window or data could be lost.
5712 // We don't need to check if the 'hidden' option is set, as in this
5713 // case the buffer won't be lost.
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005714 if (!buf_hide(curbuf) && !drop_split)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005715 {
5716 ++emsg_off;
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005717 drop_split = check_changed(curbuf, CCGD_AW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005718 --emsg_off;
5719 }
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005720 if (drop_split)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005721 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005722 if (win_split(0, 0) == FAIL)
5723 return;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02005724 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005725
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005726 // When splitting the window, create a new alist. Otherwise the
5727 // existing one is overwritten.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005728 alist_unlink(curwin->w_alist);
5729 alist_new();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005730 }
5731
5732 /*
5733 * Set up the new argument list.
5734 */
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005735 alist_set(ALIST(curwin), drop_filec, drop_filev, FALSE, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005736
5737 /*
5738 * Move to the first file.
5739 */
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005740 // Fake up a minimal "next" command for do_argfile()
Bram Moolenaara80faa82020-04-12 19:37:17 +02005741 CLEAR_FIELD(ea);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005742 ea.cmd = (char_u *)"next";
5743 do_argfile(&ea, 0);
5744
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005745 // do_ecmd() may set need_start_insertmode, but since we never left Insert
5746 // mode that is not needed here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005747 need_start_insertmode = FALSE;
5748
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005749 // Restore msg_scroll, otherwise a following command may cause scrolling
5750 // unexpectedly. The screen will be redrawn by the caller, thus
5751 // msg_scroll being set by displaying a message is irrelevant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005752 msg_scroll = save_msg_scroll;
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005753
5754 if (drop_callback != NULL)
5755 drop_callback(drop_cookie);
5756
5757 drop_filev = NULL;
5758 drop_busy = FALSE;
5759}
5760
5761/*
5762 * Handle a file drop. The code is here because a drop is *nearly* like an
5763 * :args command, but not quite (we have a list of exact filenames, so we
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01005764 * don't want to (a) parse a command line, or (b) expand wildcards). So the
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005765 * code is very similar to :args and hence needs access to a lot of the static
5766 * functions in this file.
5767 *
5768 * The "filev" list must have been allocated using alloc(), as should each item
5769 * in the list. This function takes over responsibility for freeing the "filev"
5770 * list.
5771 */
5772 void
5773handle_drop(
5774 int filec, // the number of files dropped
5775 char_u **filev, // the list of files dropped
5776 int split, // force splitting the window
5777 void (*callback)(void *), // to be called after setting the argument
5778 // list
5779 void *cookie) // argument for "callback" (allocated)
5780{
5781 // Cannot handle recursive drops, finish the pending one.
5782 if (drop_busy)
5783 {
5784 FreeWild(filec, filev);
5785 vim_free(cookie);
5786 return;
5787 }
5788
5789 // When calling handle_drop() more than once in a row we only use the last
5790 // one.
5791 if (drop_filev != NULL)
5792 {
5793 FreeWild(drop_filec, drop_filev);
5794 vim_free(drop_cookie);
5795 }
5796
5797 drop_filec = filec;
5798 drop_filev = filev;
5799 drop_split = split;
5800 drop_callback = callback;
5801 drop_cookie = cookie;
5802
5803 // Postpone this when:
5804 // - editing the command line
5805 // - not possible to change the current buffer
5806 // - updating the screen
5807 // As it may change buffers and window structures that are in use and cause
5808 // freed memory to be used.
5809 if (text_locked() || curbuf_locked() || updating_screen)
5810 return;
5811
5812 handle_drop_internal();
5813}
5814
5815/*
5816 * To be called when text is unlocked, curbuf is unlocked or updating_screen is
5817 * reset: Handle a postponed drop.
5818 */
5819 void
5820handle_any_postponed_drop(void)
5821{
5822 if (!drop_busy && drop_filev != NULL
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +02005823 && !text_locked() && !curbuf_locked() && !updating_screen)
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005824 handle_drop_internal();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005825}
5826#endif
5827
Bram Moolenaar071d4272004-06-13 20:20:40 +00005828/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005829 * ":preserve".
5830 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005831 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005832ex_preserve(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005833{
Bram Moolenaar4399ef42005-02-12 14:29:27 +00005834 curbuf->b_flags |= BF_PRESERVED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005835 ml_preserve(curbuf, TRUE);
5836}
5837
5838/*
5839 * ":recover".
5840 */
5841 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005842ex_recover(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005843{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005844 // Set recoverymode right away to avoid the ATTENTION prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005845 recoverymode = TRUE;
Bram Moolenaar45d3b142013-11-09 03:31:51 +01005846 if (!check_changed(curbuf, (p_awa ? CCGD_AW : 0)
5847 | CCGD_MULTWIN
5848 | (eap->forceit ? CCGD_FORCEIT : 0)
5849 | CCGD_EXCMD)
5850
Bram Moolenaar071d4272004-06-13 20:20:40 +00005851 && (*eap->arg == NUL
5852 || setfname(curbuf, eap->arg, NULL, TRUE) == OK))
Bram Moolenaar99499b12019-05-23 21:35:48 +02005853 ml_recover(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005854 recoverymode = FALSE;
5855}
5856
5857/*
5858 * Command modifier used in a wrong way.
5859 */
5860 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005861ex_wrongmodifier(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005862{
5863 eap->errmsg = e_invcmd;
5864}
5865
Bram Moolenaar071d4272004-06-13 20:20:40 +00005866/*
5867 * :sview [+command] file split window with new file, read-only
5868 * :split [[+command] file] split window with current or new file
5869 * :vsplit [[+command] file] split window vertically with current or new file
5870 * :new [[+command] file] split window with no or new file
5871 * :vnew [[+command] file] split vertically window with no or new file
5872 * :sfind [+command] file split window with file in 'path'
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005873 *
5874 * :tabedit open new Tab page with empty window
5875 * :tabedit [+command] file open new Tab page and edit "file"
5876 * :tabnew [[+command] file] just like :tabedit
5877 * :tabfind [+command] file open new Tab page and find "file"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005878 */
5879 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005880ex_splitview(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005881{
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005882 win_T *old_curwin = curwin;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005883#if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005884 char_u *fname = NULL;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005885#endif
5886#ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00005887 int browse_flag = cmdmod.browse;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005888#endif
Bram Moolenaar39902a02018-06-21 22:10:08 +02005889 int use_tab = eap->cmdidx == CMD_tabedit
5890 || eap->cmdidx == CMD_tabfind
5891 || eap->cmdidx == CMD_tabnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005892
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01005893 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02005894 return;
5895
Bram Moolenaar4033c552017-09-16 20:54:51 +02005896#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00005897 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005898#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005899
Bram Moolenaar4033c552017-09-16 20:54:51 +02005900#ifdef FEAT_QUICKFIX
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005901 // A ":split" in the quickfix window works like ":new". Don't want two
5902 // quickfix windows. But it's OK when doing ":tab split".
Bram Moolenaar05bb9532008-07-04 09:44:11 +00005903 if (bt_quickfix(curbuf) && cmdmod.tab == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005904 {
5905 if (eap->cmdidx == CMD_split)
5906 eap->cmdidx = CMD_new;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005907 if (eap->cmdidx == CMD_vsplit)
5908 eap->cmdidx = CMD_vnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005909 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02005910#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005911
Bram Moolenaar4033c552017-09-16 20:54:51 +02005912#ifdef FEAT_SEARCHPATH
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005913 if (eap->cmdidx == CMD_sfind || eap->cmdidx == CMD_tabfind)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005914 {
5915 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg),
5916 FNAME_MESS, TRUE, curbuf->b_ffname);
5917 if (fname == NULL)
5918 goto theend;
5919 eap->arg = fname;
5920 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005921# ifdef FEAT_BROWSE
Bram Moolenaar4033c552017-09-16 20:54:51 +02005922 else
5923# endif
5924#endif
5925#ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00005926 if (cmdmod.browse
Bram Moolenaar071d4272004-06-13 20:20:40 +00005927 && eap->cmdidx != CMD_vnew
Bram Moolenaar071d4272004-06-13 20:20:40 +00005928 && eap->cmdidx != CMD_new)
5929 {
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00005930 if (
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005931# ifdef FEAT_GUI
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00005932 !gui.in_use &&
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005933# endif
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00005934 au_has_group((char_u *)"FileExplorer"))
5935 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005936 // No browsing supported but we do have the file explorer:
5937 // Edit the directory.
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00005938 if (*eap->arg == NUL || !mch_isdir(eap->arg))
5939 eap->arg = (char_u *)".";
5940 }
5941 else
5942 {
Bram Moolenaar39902a02018-06-21 22:10:08 +02005943 fname = do_browse(0, (char_u *)(use_tab
5944 ? _("Edit File in new tab page")
5945 : _("Edit File in new window")),
Bram Moolenaar071d4272004-06-13 20:20:40 +00005946 eap->arg, NULL, NULL, NULL, curbuf);
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00005947 if (fname == NULL)
5948 goto theend;
5949 eap->arg = fname;
5950 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005951 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005952 cmdmod.browse = FALSE; // Don't browse again in do_ecmd().
Bram Moolenaar4033c552017-09-16 20:54:51 +02005953#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005954
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005955 /*
5956 * Either open new tab page or split the window.
5957 */
Bram Moolenaar39902a02018-06-21 22:10:08 +02005958 if (use_tab)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005959 {
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005960 if (win_new_tabpage(cmdmod.tab != 0 ? cmdmod.tab
5961 : eap->addr_count == 0 ? 0
5962 : (int)eap->line2 + 1) != FAIL)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005963 {
Bram Moolenaard2b66012008-01-09 19:30:36 +00005964 do_exedit(eap, old_curwin);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005965
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005966 // set the alternate buffer for the window we came from
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005967 if (curwin != old_curwin
5968 && win_valid(old_curwin)
5969 && old_curwin->w_buffer != curbuf
5970 && !cmdmod.keepalt)
5971 old_curwin->w_alt_fnum = curbuf->b_fnum;
5972 }
5973 }
5974 else if (win_split(eap->addr_count > 0 ? (int)eap->line2 : 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005975 *eap->cmd == 'v' ? WSP_VERT : 0) != FAIL)
5976 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005977 // Reset 'scrollbind' when editing another file, but keep it when
5978 // doing ":split" without arguments.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005979 if (*eap->arg != NUL
Bram Moolenaar8a3bb562018-03-04 20:14:14 +01005980# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00005981 || cmdmod.browse
Bram Moolenaar8a3bb562018-03-04 20:14:14 +01005982# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005983 )
Bram Moolenaar3368ea22010-09-21 16:56:35 +02005984 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005985 else
5986 do_check_scrollbind(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005987 do_exedit(eap, old_curwin);
5988 }
5989
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005990# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00005991 cmdmod.browse = browse_flag;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005992# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005993
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005994# if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005995theend:
5996 vim_free(fname);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005997# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005998}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005999
6000/*
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006001 * Open a new tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006002 */
6003 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006004tabpage_new(void)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006005{
6006 exarg_T ea;
6007
Bram Moolenaara80faa82020-04-12 19:37:17 +02006008 CLEAR_FIELD(ea);
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006009 ea.cmdidx = CMD_tabnew;
6010 ea.cmd = (char_u *)"tabn";
6011 ea.arg = (char_u *)"";
6012 ex_splitview(&ea);
6013}
6014
6015/*
6016 * :tabnext command
6017 */
6018 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006019ex_tabnext(exarg_T *eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006020{
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006021 int tab_number;
6022
Bram Moolenaar8cdbd5b2019-06-16 15:50:45 +02006023 if (ERROR_IF_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02006024 return;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006025 switch (eap->cmdidx)
6026 {
6027 case CMD_tabfirst:
6028 case CMD_tabrewind:
6029 goto_tabpage(1);
6030 break;
6031 case CMD_tablast:
6032 goto_tabpage(9999);
6033 break;
6034 case CMD_tabprevious:
6035 case CMD_tabNext:
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006036 if (eap->arg && *eap->arg != NUL)
6037 {
6038 char_u *p = eap->arg;
6039 char_u *p_save = p;
6040
6041 tab_number = getdigits(&p);
6042 if (p == p_save || *p_save == '-' || *p != NUL
6043 || tab_number == 0)
6044 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006045 // No numbers as argument.
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006046 eap->errmsg = e_invarg;
6047 return;
6048 }
6049 }
6050 else
6051 {
6052 if (eap->addr_count == 0)
6053 tab_number = 1;
6054 else
6055 {
6056 tab_number = eap->line2;
6057 if (tab_number < 1)
6058 {
6059 eap->errmsg = e_invrange;
6060 return;
6061 }
6062 }
6063 }
6064 goto_tabpage(-tab_number);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006065 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006066 default: // CMD_tabnext
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006067 tab_number = get_tabpage_arg(eap);
6068 if (eap->errmsg == NULL)
6069 goto_tabpage(tab_number);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006070 break;
6071 }
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006072}
6073
6074/*
6075 * :tabmove command
6076 */
6077 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006078ex_tabmove(exarg_T *eap)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006079{
Bram Moolenaar40ce3a42015-04-21 18:08:39 +02006080 int tab_number;
Bram Moolenaar8cb8dca2012-07-06 18:27:39 +02006081
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006082 tab_number = get_tabpage_arg(eap);
6083 if (eap->errmsg == NULL)
6084 tabpage_move(tab_number);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006085}
6086
6087/*
6088 * :tabs command: List tabs and their contents.
6089 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006090 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006091ex_tabs(exarg_T *eap UNUSED)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006092{
6093 tabpage_T *tp;
6094 win_T *wp;
6095 int tabcount = 1;
6096
6097 msg_start();
6098 msg_scroll = TRUE;
6099 for (tp = first_tabpage; tp != NULL && !got_int; tp = tp->tp_next)
6100 {
6101 msg_putchar('\n');
6102 vim_snprintf((char *)IObuff, IOSIZE, _("Tab page %d"), tabcount++);
Bram Moolenaar8820b482017-03-16 17:23:31 +01006103 msg_outtrans_attr(IObuff, HL_ATTR(HLF_T));
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006104 out_flush(); // output one line at a time
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006105 ui_breakcheck();
6106
Bram Moolenaar030f0df2006-02-21 22:02:53 +00006107 if (tp == curtab)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006108 wp = firstwin;
6109 else
6110 wp = tp->tp_firstwin;
6111 for ( ; wp != NULL && !got_int; wp = wp->w_next)
6112 {
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006113 msg_putchar('\n');
6114 msg_putchar(wp == curwin ? '>' : ' ');
6115 msg_putchar(' ');
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006116 msg_putchar(bufIsChanged(wp->w_buffer) ? '+' : ' ');
6117 msg_putchar(' ');
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006118 if (buf_spname(wp->w_buffer) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02006119 vim_strncpy(IObuff, buf_spname(wp->w_buffer), IOSIZE - 1);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006120 else
6121 home_replace(wp->w_buffer, wp->w_buffer->b_fname,
6122 IObuff, IOSIZE, TRUE);
6123 msg_outtrans(IObuff);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006124 out_flush(); // output one line at a time
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006125 ui_breakcheck();
6126 }
6127 }
6128}
6129
Bram Moolenaar071d4272004-06-13 20:20:40 +00006130/*
6131 * ":mode": Set screen mode.
6132 * If no argument given, just get the screen size and redraw.
6133 */
6134 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006135ex_mode(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006136{
6137 if (*eap->arg == NUL)
6138 shell_resized();
6139 else
Bram Moolenaar3c71aec2020-01-17 19:32:20 +01006140 emsg(_(e_screenmode));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006141}
6142
Bram Moolenaar071d4272004-06-13 20:20:40 +00006143/*
6144 * ":resize".
6145 * set, increment or decrement current window height
6146 */
6147 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006148ex_resize(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006149{
6150 int n;
6151 win_T *wp = curwin;
6152
6153 if (eap->addr_count > 0)
6154 {
6155 n = eap->line2;
6156 for (wp = firstwin; wp->w_next != NULL && --n > 0; wp = wp->w_next)
6157 ;
6158 }
6159
Bram Moolenaar44a2f922016-03-19 22:11:51 +01006160# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00006161 need_mouse_correct = TRUE;
Bram Moolenaar44a2f922016-03-19 22:11:51 +01006162# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006163 n = atol((char *)eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006164 if (cmdmod.split & WSP_VERT)
6165 {
6166 if (*eap->arg == '-' || *eap->arg == '+')
Bram Moolenaar02631462017-09-22 15:20:32 +02006167 n += curwin->w_width;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006168 else if (n == 0 && eap->arg[0] == NUL) // default is very wide
Bram Moolenaar071d4272004-06-13 20:20:40 +00006169 n = 9999;
6170 win_setwidth_win((int)n, wp);
6171 }
6172 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006173 {
6174 if (*eap->arg == '-' || *eap->arg == '+')
6175 n += curwin->w_height;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006176 else if (n == 0 && eap->arg[0] == NUL) // default is very high
Bram Moolenaar071d4272004-06-13 20:20:40 +00006177 n = 9999;
6178 win_setheight_win((int)n, wp);
6179 }
6180}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006181
6182/*
6183 * ":find [+command] <file>" command.
6184 */
6185 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006186ex_find(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006187{
6188#ifdef FEAT_SEARCHPATH
6189 char_u *fname;
6190 int count;
6191
6192 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg), FNAME_MESS,
6193 TRUE, curbuf->b_ffname);
6194 if (eap->addr_count > 0)
6195 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006196 // Repeat finding the file "count" times. This matters when it
6197 // appears several times in the path.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006198 count = eap->line2;
6199 while (fname != NULL && --count > 0)
6200 {
6201 vim_free(fname);
6202 fname = find_file_in_path(NULL, 0, FNAME_MESS,
6203 FALSE, curbuf->b_ffname);
6204 }
6205 }
6206
6207 if (fname != NULL)
6208 {
6209 eap->arg = fname;
6210#endif
6211 do_exedit(eap, NULL);
6212#ifdef FEAT_SEARCHPATH
6213 vim_free(fname);
6214 }
6215#endif
6216}
6217
6218/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00006219 * ":open" simulation: for now just work like ":visual".
6220 */
6221 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006222ex_open(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00006223{
6224 regmatch_T regmatch;
6225 char_u *p;
6226
6227 curwin->w_cursor.lnum = eap->line2;
6228 beginline(BL_SOL | BL_FIX);
6229 if (*eap->arg == '/')
6230 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006231 // ":open /pattern/": put cursor in column found with pattern
Bram Moolenaardf177f62005-02-22 08:39:57 +00006232 ++eap->arg;
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006233 p = skip_regexp(eap->arg, '/', p_magic);
Bram Moolenaardf177f62005-02-22 08:39:57 +00006234 *p = NUL;
6235 regmatch.regprog = vim_regcomp(eap->arg, p_magic ? RE_MAGIC : 0);
6236 if (regmatch.regprog != NULL)
6237 {
6238 regmatch.rm_ic = p_ic;
6239 p = ml_get_curline();
6240 if (vim_regexec(&regmatch, p, (colnr_T)0))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006241 curwin->w_cursor.col = (colnr_T)(regmatch.startp[0] - p);
Bram Moolenaardf177f62005-02-22 08:39:57 +00006242 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006243 emsg(_(e_nomatch));
Bram Moolenaar473de612013-06-08 18:19:48 +02006244 vim_regfree(regmatch.regprog);
Bram Moolenaardf177f62005-02-22 08:39:57 +00006245 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006246 // Move to the NUL, ignore any other arguments.
Bram Moolenaardf177f62005-02-22 08:39:57 +00006247 eap->arg += STRLEN(eap->arg);
6248 }
6249 check_cursor();
6250
6251 eap->cmdidx = CMD_visual;
6252 do_exedit(eap, NULL);
6253}
6254
6255/*
6256 * ":edit", ":badd", ":visual".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006257 */
6258 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006259ex_edit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006260{
6261 do_exedit(eap, NULL);
6262}
6263
6264/*
Bram Moolenaar4b96df52020-01-26 22:00:26 +01006265 * ":edit <file>" command and alike.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006266 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006267 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006268do_exedit(
6269 exarg_T *eap,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006270 win_T *old_curwin) // curwin before doing a split or NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006271{
6272 int n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006273 int need_hide;
Bram Moolenaardf177f62005-02-22 08:39:57 +00006274 int exmode_was = exmode_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006275
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01006276 if ((eap->cmdidx != CMD_pedit && ERROR_IF_POPUP_WINDOW)
6277 || ERROR_IF_TERM_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02006278 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006279 /*
6280 * ":vi" command ends Ex mode.
6281 */
6282 if (exmode_active && (eap->cmdidx == CMD_visual
6283 || eap->cmdidx == CMD_view))
6284 {
6285 exmode_active = FALSE;
Bram Moolenaar158ea172020-06-18 17:28:39 +02006286 ex_pressedreturn = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006287 if (*eap->arg == NUL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00006288 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006289 // Special case: ":global/pat/visual\NLvi-commands"
Bram Moolenaardf177f62005-02-22 08:39:57 +00006290 if (global_busy)
6291 {
6292 int rd = RedrawingDisabled;
6293 int nwr = no_wait_return;
6294 int ms = msg_scroll;
6295#ifdef FEAT_GUI
6296 int he = hold_gui_events;
6297#endif
6298
6299 if (eap->nextcmd != NULL)
6300 {
6301 stuffReadbuff(eap->nextcmd);
6302 eap->nextcmd = NULL;
6303 }
6304
6305 if (exmode_was != EXMODE_VIM)
6306 settmode(TMODE_RAW);
6307 RedrawingDisabled = 0;
6308 no_wait_return = 0;
6309 need_wait_return = FALSE;
6310 msg_scroll = 0;
6311#ifdef FEAT_GUI
6312 hold_gui_events = 0;
6313#endif
6314 must_redraw = CLEAR;
Bram Moolenaar9e2bcb52020-02-18 21:33:00 +01006315 pending_exmode_active = TRUE;
Bram Moolenaardf177f62005-02-22 08:39:57 +00006316
6317 main_loop(FALSE, TRUE);
6318
Bram Moolenaar9e2bcb52020-02-18 21:33:00 +01006319 pending_exmode_active = FALSE;
Bram Moolenaardf177f62005-02-22 08:39:57 +00006320 RedrawingDisabled = rd;
6321 no_wait_return = nwr;
6322 msg_scroll = ms;
6323#ifdef FEAT_GUI
6324 hold_gui_events = he;
6325#endif
6326 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006327 return;
Bram Moolenaardf177f62005-02-22 08:39:57 +00006328 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006329 }
6330
6331 if ((eap->cmdidx == CMD_new
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006332 || eap->cmdidx == CMD_tabnew
6333 || eap->cmdidx == CMD_tabedit
Bram Moolenaar4033c552017-09-16 20:54:51 +02006334 || eap->cmdidx == CMD_vnew) && *eap->arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006335 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006336 // ":new" or ":tabnew" without argument: edit an new empty buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00006337 setpcmark();
6338 (void)do_ecmd(0, NULL, NULL, eap, ECMD_ONE,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00006339 ECMD_HIDE + (eap->forceit ? ECMD_FORCEIT : 0),
6340 old_curwin == NULL ? curwin : NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006341 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02006342 else if ((eap->cmdidx != CMD_split && eap->cmdidx != CMD_vsplit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006343 || *eap->arg != NUL
6344#ifdef FEAT_BROWSE
6345 || cmdmod.browse
6346#endif
6347 )
6348 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006349 // Can't edit another file when "curbuf_lock" is set. Only ":edit"
6350 // can bring us here, others are stopped earlier.
Bram Moolenaar5555acc2006-04-07 21:33:12 +00006351 if (*eap->arg != NUL && curbuf_locked())
6352 return;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006353
Bram Moolenaar071d4272004-06-13 20:20:40 +00006354 n = readonlymode;
6355 if (eap->cmdidx == CMD_view || eap->cmdidx == CMD_sview)
6356 readonlymode = TRUE;
6357 else if (eap->cmdidx == CMD_enew)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006358 readonlymode = FALSE; // 'readonly' doesn't make sense in an
6359 // empty buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00006360 setpcmark();
6361 if (do_ecmd(0, (eap->cmdidx == CMD_enew ? NULL : eap->arg),
6362 NULL, eap,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006363 // ":edit" goes to first line if Vi compatible
Bram Moolenaar071d4272004-06-13 20:20:40 +00006364 (*eap->arg == NUL && eap->do_ecmd_lnum == 0
6365 && vim_strchr(p_cpo, CPO_GOTO1) != NULL)
6366 ? ECMD_ONE : eap->do_ecmd_lnum,
Bram Moolenaareb44a682017-08-03 22:44:55 +02006367 (buf_hide(curbuf) ? ECMD_HIDE : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006368 + (eap->forceit ? ECMD_FORCEIT : 0)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006369 // after a split we can use an existing buffer
Bram Moolenaar7b449342014-03-25 13:03:48 +01006370 + (old_curwin != NULL ? ECMD_OLDBUF : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006371 + (eap->cmdidx == CMD_badd ? ECMD_ADDBUF : 0 )
Bram Moolenaar701f7af2008-11-15 13:12:07 +00006372 , old_curwin == NULL ? curwin : NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006373 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006374 // Editing the file failed. If the window was split, close it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006375 if (old_curwin != NULL)
6376 {
6377 need_hide = (curbufIsChanged() && curbuf->b_nwindows <= 1);
Bram Moolenaareb44a682017-08-03 22:44:55 +02006378 if (!need_hide || buf_hide(curbuf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006379 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006380#if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00006381 cleanup_T cs;
6382
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006383 // Reset the error/interrupt/exception state here so that
6384 // aborting() returns FALSE when closing a window.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00006385 enter_cleanup(&cs);
Bram Moolenaar4033c552017-09-16 20:54:51 +02006386#endif
6387#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00006388 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006389#endif
Bram Moolenaareb44a682017-08-03 22:44:55 +02006390 win_close(curwin, !need_hide && !buf_hide(curbuf));
Bram Moolenaarc0197e22004-09-13 20:26:32 +00006391
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006392#if defined(FEAT_EVAL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006393 // Restore the error/interrupt/exception state if not
6394 // discarded by a new aborting error, interrupt, or
6395 // uncaught exception.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00006396 leave_cleanup(&cs);
Bram Moolenaar4033c552017-09-16 20:54:51 +02006397#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006398 }
6399 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006400 }
6401 else if (readonlymode && curbuf->b_nwindows == 1)
6402 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006403 // When editing an already visited buffer, 'readonly' won't be set
6404 // but the previous value is kept. With ":view" and ":sview" we
6405 // want the file to be readonly, except when another window is
6406 // editing the same buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006407 curbuf->b_p_ro = TRUE;
6408 }
6409 readonlymode = n;
6410 }
6411 else
6412 {
6413 if (eap->do_ecmd_cmd != NULL)
6414 do_cmdline_cmd(eap->do_ecmd_cmd);
6415#ifdef FEAT_TITLE
6416 n = curwin->w_arg_idx_invalid;
6417#endif
6418 check_arg_idx(curwin);
6419#ifdef FEAT_TITLE
6420 if (n != curwin->w_arg_idx_invalid)
6421 maketitle();
6422#endif
6423 }
6424
Bram Moolenaar071d4272004-06-13 20:20:40 +00006425 /*
6426 * if ":split file" worked, set alternate file name in old window to new
6427 * file
6428 */
6429 if (old_curwin != NULL
6430 && *eap->arg != NUL
6431 && curwin != old_curwin
6432 && win_valid(old_curwin)
Bram Moolenaard4755bb2004-09-02 19:12:26 +00006433 && old_curwin->w_buffer != curbuf
6434 && !cmdmod.keepalt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006435 old_curwin->w_alt_fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006436
6437 ex_no_reprint = TRUE;
6438}
6439
6440#ifndef FEAT_GUI
6441/*
6442 * ":gui" and ":gvim" when there is no GUI.
6443 */
6444 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006445ex_nogui(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006446{
6447 eap->errmsg = e_nogvim;
6448}
6449#endif
6450
Bram Moolenaar4f974752019-02-17 17:44:42 +01006451#if defined(FEAT_GUI_MSWIN) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006452 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006453ex_tearoff(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006454{
6455 gui_make_tearoff(eap->arg);
6456}
6457#endif
6458
Bram Moolenaar29a2c082018-03-05 21:06:23 +01006459#if (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK) \
6460 || defined(FEAT_TERM_POPUP_MENU)) && defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006461 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006462ex_popup(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006463{
Bram Moolenaar29a2c082018-03-05 21:06:23 +01006464# if defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK)
6465 if (gui.in_use)
6466 gui_make_popup(eap->arg, eap->forceit);
6467# ifdef FEAT_TERM_POPUP_MENU
6468 else
6469# endif
6470# endif
6471# ifdef FEAT_TERM_POPUP_MENU
6472 pum_make_popup(eap->arg, eap->forceit);
6473# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006474}
6475#endif
6476
Bram Moolenaar071d4272004-06-13 20:20:40 +00006477 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006478ex_swapname(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006479{
6480 if (curbuf->b_ml.ml_mfp == NULL || curbuf->b_ml.ml_mfp->mf_fname == NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01006481 msg(_("No swap file"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006482 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01006483 msg((char *)curbuf->b_ml.ml_mfp->mf_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006484}
6485
6486/*
6487 * ":syncbind" forces all 'scrollbind' windows to have the same relative
6488 * offset.
6489 * (1998-11-02 16:21:01 R. Edward Ralston <eralston@computer.org>)
6490 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006491 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006492ex_syncbind(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006493{
Bram Moolenaar071d4272004-06-13 20:20:40 +00006494 win_T *wp;
Bram Moolenaardedd1b02013-12-14 13:06:17 +01006495 win_T *save_curwin = curwin;
6496 buf_T *save_curbuf = curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006497 long topline;
6498 long y;
6499 linenr_T old_linenr = curwin->w_cursor.lnum;
6500
6501 setpcmark();
6502
6503 /*
6504 * determine max topline
6505 */
6506 if (curwin->w_p_scb)
6507 {
6508 topline = curwin->w_topline;
Bram Moolenaar29323592016-07-24 22:04:11 +02006509 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006510 {
6511 if (wp->w_p_scb && wp->w_buffer)
6512 {
Bram Moolenaar375e3392019-01-31 18:26:10 +01006513 y = wp->w_buffer->b_ml.ml_line_count - get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006514 if (topline > y)
6515 topline = y;
6516 }
6517 }
6518 if (topline < 1)
6519 topline = 1;
6520 }
6521 else
6522 {
6523 topline = 1;
6524 }
6525
6526
6527 /*
Bram Moolenaardedd1b02013-12-14 13:06:17 +01006528 * Set all scrollbind windows to the same topline.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006529 */
Bram Moolenaar29323592016-07-24 22:04:11 +02006530 FOR_ALL_WINDOWS(curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006531 {
6532 if (curwin->w_p_scb)
6533 {
Bram Moolenaardedd1b02013-12-14 13:06:17 +01006534 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006535 y = topline - curwin->w_topline;
6536 if (y > 0)
6537 scrollup(y, TRUE);
6538 else
6539 scrolldown(-y, TRUE);
6540 curwin->w_scbind_pos = topline;
6541 redraw_later(VALID);
6542 cursor_correct();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006543 curwin->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006544 }
6545 }
Bram Moolenaardedd1b02013-12-14 13:06:17 +01006546 curwin = save_curwin;
6547 curbuf = save_curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006548 if (curwin->w_p_scb)
6549 {
6550 did_syncbind = TRUE;
6551 checkpcmark();
6552 if (old_linenr != curwin->w_cursor.lnum)
6553 {
6554 char_u ctrl_o[2];
6555
6556 ctrl_o[0] = Ctrl_O;
6557 ctrl_o[1] = 0;
6558 ins_typebuf(ctrl_o, REMAP_NONE, 0, TRUE, FALSE);
6559 }
6560 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006561}
6562
6563
6564 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006565ex_read(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006566{
Bram Moolenaardf177f62005-02-22 08:39:57 +00006567 int i;
6568 int empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
6569 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006570
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006571 if (eap->usefilter) // :r!cmd
Bram Moolenaar071d4272004-06-13 20:20:40 +00006572 do_bang(1, eap, FALSE, FALSE, TRUE);
6573 else
6574 {
6575 if (u_save(eap->line2, (linenr_T)(eap->line2 + 1)) == FAIL)
6576 return;
6577
6578#ifdef FEAT_BROWSE
6579 if (cmdmod.browse)
6580 {
6581 char_u *browseFile;
6582
Bram Moolenaar7171abe2004-10-11 10:06:20 +00006583 browseFile = do_browse(0, (char_u *)_("Append File"), eap->arg,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006584 NULL, NULL, NULL, curbuf);
6585 if (browseFile != NULL)
6586 {
6587 i = readfile(browseFile, NULL,
6588 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
6589 vim_free(browseFile);
6590 }
6591 else
6592 i = OK;
6593 }
6594 else
6595#endif
6596 if (*eap->arg == NUL)
6597 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006598 if (check_fname() == FAIL) // check for no file name
Bram Moolenaar071d4272004-06-13 20:20:40 +00006599 return;
6600 i = readfile(curbuf->b_ffname, curbuf->b_fname,
6601 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
6602 }
6603 else
6604 {
6605 if (vim_strchr(p_cpo, CPO_ALTREAD) != NULL)
6606 (void)setaltfname(eap->arg, eap->arg, (linenr_T)1);
6607 i = readfile(eap->arg, NULL,
6608 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
6609
6610 }
Bram Moolenaare13b9af2017-01-13 22:01:02 +01006611 if (i != OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006612 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006613#if defined(FEAT_EVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006614 if (!aborting())
6615#endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006616 semsg(_(e_notopen), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006617 }
6618 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00006619 {
6620 if (empty && exmode_active)
6621 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006622 // Delete the empty line that remains. Historically ex does
6623 // this but vi doesn't.
Bram Moolenaardf177f62005-02-22 08:39:57 +00006624 if (eap->line2 == 0)
6625 lnum = curbuf->b_ml.ml_line_count;
6626 else
6627 lnum = 1;
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00006628 if (*ml_get(lnum) == NUL && u_savedel(lnum, 1L) == OK)
Bram Moolenaardf177f62005-02-22 08:39:57 +00006629 {
Bram Moolenaarca70c072020-05-30 20:30:46 +02006630 ml_delete(lnum);
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00006631 if (curwin->w_cursor.lnum > 1
6632 && curwin->w_cursor.lnum >= lnum)
Bram Moolenaardf177f62005-02-22 08:39:57 +00006633 --curwin->w_cursor.lnum;
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00006634 deleted_lines_mark(lnum, 1L);
Bram Moolenaardf177f62005-02-22 08:39:57 +00006635 }
6636 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006637 redraw_curbuf_later(VALID);
Bram Moolenaardf177f62005-02-22 08:39:57 +00006638 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006639 }
6640}
6641
Bram Moolenaarea408852005-06-25 22:49:46 +00006642static char_u *prev_dir = NULL;
6643
6644#if defined(EXITFREE) || defined(PROTO)
6645 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006646free_cd_dir(void)
Bram Moolenaarea408852005-06-25 22:49:46 +00006647{
Bram Moolenaard23a8232018-02-10 18:45:26 +01006648 VIM_CLEAR(prev_dir);
6649 VIM_CLEAR(globaldir);
Bram Moolenaarea408852005-06-25 22:49:46 +00006650}
6651#endif
6652
Bram Moolenaarf4258302013-06-02 18:20:17 +02006653/*
Bram Moolenaar002bc792020-06-05 22:33:42 +02006654 * Get the previous directory for the given chdir scope.
6655 */
6656 static char_u *
6657get_prevdir(cdscope_T scope)
6658{
6659 if (scope == CDSCOPE_WINDOW)
6660 return curwin->w_prevdir;
6661 else if (scope == CDSCOPE_TABPAGE)
6662 return curtab->tp_prevdir;
6663 return prev_dir;
6664}
6665
6666/*
Bram Moolenaarf4258302013-06-02 18:20:17 +02006667 * Deal with the side effects of changing the current directory.
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006668 * When 'scope' is CDSCOPE_TABPAGE then this was after an ":tcd" command.
6669 * When 'scope' is CDSCOPE_WINDOW then this was after an ":lcd" command.
Bram Moolenaarf4258302013-06-02 18:20:17 +02006670 */
6671 void
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006672post_chdir(cdscope_T scope)
Bram Moolenaarf4258302013-06-02 18:20:17 +02006673{
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006674 if (scope != CDSCOPE_WINDOW)
Bram Moolenaar00aa0692019-04-27 20:37:57 +02006675 // Clear tab local directory for both :cd and :tcd
6676 VIM_CLEAR(curtab->tp_localdir);
Bram Moolenaard23a8232018-02-10 18:45:26 +01006677 VIM_CLEAR(curwin->w_localdir);
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006678 if (scope != CDSCOPE_GLOBAL)
Bram Moolenaarf4258302013-06-02 18:20:17 +02006679 {
Bram Moolenaar002bc792020-06-05 22:33:42 +02006680 char_u *pdir = get_prevdir(scope);
6681
6682 // If still in the global directory, need to remember current
6683 // directory as the global directory.
6684 if (globaldir == NULL && pdir != NULL)
6685 globaldir = vim_strsave(pdir);
6686
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006687 // Remember this local directory for the window.
Bram Moolenaarf4258302013-06-02 18:20:17 +02006688 if (mch_dirname(NameBuff, MAXPATHL) == OK)
Bram Moolenaar00aa0692019-04-27 20:37:57 +02006689 {
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006690 if (scope == CDSCOPE_TABPAGE)
Bram Moolenaar00aa0692019-04-27 20:37:57 +02006691 curtab->tp_localdir = vim_strsave(NameBuff);
6692 else
6693 curwin->w_localdir = vim_strsave(NameBuff);
6694 }
Bram Moolenaarf4258302013-06-02 18:20:17 +02006695 }
6696 else
6697 {
Bram Moolenaar002bc792020-06-05 22:33:42 +02006698 // We are now in the global directory, no need to remember its name.
Bram Moolenaard23a8232018-02-10 18:45:26 +01006699 VIM_CLEAR(globaldir);
Bram Moolenaarf4258302013-06-02 18:20:17 +02006700 }
6701
6702 shorten_fnames(TRUE);
6703}
6704
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006705/*
6706 * Change directory function used by :cd/:tcd/:lcd Ex commands and the
Bram Moolenaar95058722020-06-01 16:26:19 +02006707 * chdir() function.
6708 * scope == CDSCOPE_WINDOW: changes the window-local directory
6709 * scope == CDSCOPE_TABPAGE: changes the tab-local directory
6710 * Otherwise: changes the global directory
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006711 * Returns TRUE if the directory is successfully changed.
6712 */
6713 int
6714changedir_func(
6715 char_u *new_dir,
6716 int forceit,
6717 cdscope_T scope)
6718{
6719 char_u *tofree;
Bram Moolenaar002bc792020-06-05 22:33:42 +02006720 char_u *pdir = NULL;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006721 int dir_differs;
6722 int retval = FALSE;
6723
Bram Moolenaar7cc96922020-01-31 22:41:38 +01006724 if (new_dir == NULL || allbuf_locked())
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006725 return FALSE;
6726
6727 if (vim_strchr(p_cpo, CPO_CHDIR) != NULL && curbufIsChanged() && !forceit)
6728 {
6729 emsg(_("E747: Cannot change directory, buffer is modified (add ! to override)"));
6730 return FALSE;
6731 }
6732
6733 // ":cd -": Change to previous directory
6734 if (STRCMP(new_dir, "-") == 0)
6735 {
Bram Moolenaar002bc792020-06-05 22:33:42 +02006736 pdir = get_prevdir(scope);
6737 if (pdir == NULL)
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006738 {
6739 emsg(_("E186: No previous directory"));
6740 return FALSE;
6741 }
Bram Moolenaar002bc792020-06-05 22:33:42 +02006742 new_dir = pdir;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006743 }
6744
Bram Moolenaar002bc792020-06-05 22:33:42 +02006745 // Free the previous directory
6746 tofree = get_prevdir(scope);
6747
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006748 // Save current directory for next ":cd -"
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006749 if (mch_dirname(NameBuff, MAXPATHL) == OK)
Bram Moolenaar002bc792020-06-05 22:33:42 +02006750 pdir = vim_strsave(NameBuff);
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006751 else
Bram Moolenaar002bc792020-06-05 22:33:42 +02006752 pdir = NULL;
6753 if (scope == CDSCOPE_WINDOW)
6754 curwin->w_prevdir = pdir;
6755 else if (scope == CDSCOPE_TABPAGE)
6756 curtab->tp_prevdir = pdir;
6757 else
6758 prev_dir = pdir;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006759
6760#if defined(UNIX) || defined(VMS)
6761 // for UNIX ":cd" means: go to home directory
6762 if (*new_dir == NUL)
6763 {
6764 // use NameBuff for home directory name
6765# ifdef VMS
6766 char_u *p;
6767
6768 p = mch_getenv((char_u *)"SYS$LOGIN");
6769 if (p == NULL || *p == NUL) // empty is the same as not set
6770 NameBuff[0] = NUL;
6771 else
6772 vim_strncpy(NameBuff, p, MAXPATHL - 1);
6773# else
6774 expand_env((char_u *)"$HOME", NameBuff, MAXPATHL);
6775# endif
6776 new_dir = NameBuff;
6777 }
6778#endif
Bram Moolenaar002bc792020-06-05 22:33:42 +02006779 dir_differs = new_dir == NULL || pdir == NULL
6780 || pathcmp((char *)pdir, (char *)new_dir, -1) != 0;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006781 if (new_dir == NULL || (dir_differs && vim_chdir(new_dir)))
6782 emsg(_(e_failed));
6783 else
6784 {
6785 char_u *acmd_fname;
6786
6787 post_chdir(scope);
6788
6789 if (dir_differs)
6790 {
6791 if (scope == CDSCOPE_WINDOW)
6792 acmd_fname = (char_u *)"window";
6793 else if (scope == CDSCOPE_TABPAGE)
6794 acmd_fname = (char_u *)"tabpage";
6795 else
6796 acmd_fname = (char_u *)"global";
6797 apply_autocmds(EVENT_DIRCHANGED, acmd_fname, new_dir, FALSE,
6798 curbuf);
6799 }
6800 retval = TRUE;
6801 }
6802 vim_free(tofree);
6803
6804 return retval;
6805}
Bram Moolenaarea408852005-06-25 22:49:46 +00006806
Bram Moolenaar071d4272004-06-13 20:20:40 +00006807/*
Bram Moolenaar00aa0692019-04-27 20:37:57 +02006808 * ":cd", ":tcd", ":lcd", ":chdir" ":tchdir" and ":lchdir".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006809 */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00006810 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006811ex_cd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006812{
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01006813 char_u *new_dir;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006814
6815 new_dir = eap->arg;
6816#if !defined(UNIX) && !defined(VMS)
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006817 // for non-UNIX ":cd" means: print current directory
Bram Moolenaar071d4272004-06-13 20:20:40 +00006818 if (*new_dir == NUL)
6819 ex_pwd(NULL);
6820 else
6821#endif
6822 {
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006823 cdscope_T scope = CDSCOPE_GLOBAL;
6824
6825 if (eap->cmdidx == CMD_lcd || eap->cmdidx == CMD_lchdir)
6826 scope = CDSCOPE_WINDOW;
6827 else if (eap->cmdidx == CMD_tcd || eap->cmdidx == CMD_tchdir)
6828 scope = CDSCOPE_TABPAGE;
6829
6830 if (changedir_func(new_dir, eap->forceit, scope))
Bram Moolenaardf177f62005-02-22 08:39:57 +00006831 {
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006832 // Echo the new current directory if the command was typed.
Bram Moolenaarfcfbc672009-07-09 18:13:49 +00006833 if (KeyTyped || p_verbose >= 5)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006834 ex_pwd(eap);
6835 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006836 }
6837}
6838
6839/*
6840 * ":pwd".
6841 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006842 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006843ex_pwd(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006844{
6845 if (mch_dirname(NameBuff, MAXPATHL) == OK)
6846 {
6847#ifdef BACKSLASH_IN_FILENAME
6848 slash_adjust(NameBuff);
6849#endif
Bram Moolenaar95058722020-06-01 16:26:19 +02006850 if (p_verbose > 0)
6851 {
6852 char *context = "global";
6853
6854 if (curwin->w_localdir != NULL)
6855 context = "window";
6856 else if (curtab->tp_localdir != NULL)
6857 context = "tabpage";
6858 smsg("[%s] %s", context, (char *)NameBuff);
6859 }
6860 else
6861 msg((char *)NameBuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006862 }
6863 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006864 emsg(_("E187: Unknown"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006865}
6866
6867/*
6868 * ":=".
6869 */
6870 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006871ex_equal(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006872{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006873 smsg("%ld", (long)eap->line2);
Bram Moolenaardf177f62005-02-22 08:39:57 +00006874 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006875}
6876
6877 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006878ex_sleep(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006879{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006880 int n;
6881 long len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006882
6883 if (cursor_valid())
6884 {
6885 n = W_WINROW(curwin) + curwin->w_wrow - msg_scrolled;
6886 if (n >= 0)
Bram Moolenaar53f81742017-09-22 14:35:51 +02006887 windgoto((int)n, curwin->w_wincol + curwin->w_wcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006888 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006889
6890 len = eap->line2;
6891 switch (*eap->arg)
6892 {
6893 case 'm': break;
6894 case NUL: len *= 1000L; break;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006895 default: semsg(_(e_invarg2), eap->arg); return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006896 }
6897 do_sleep(len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006898}
6899
6900/*
6901 * Sleep for "msec" milliseconds, but keep checking for a CTRL-C every second.
6902 */
6903 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006904do_sleep(long msec)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006905{
Bram Moolenaar52953192019-08-16 10:27:13 +02006906 long done = 0;
Bram Moolenaar975b5272016-03-15 23:10:59 +01006907 long wait_now;
Bram Moolenaar52953192019-08-16 10:27:13 +02006908# ifdef ELAPSED_FUNC
6909 elapsed_T start_tv;
6910
6911 // Remember at what time we started, so that we know how much longer we
6912 // should wait after waiting for a bit.
6913 ELAPSED_INIT(start_tv);
6914# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006915
6916 cursor_on();
Bram Moolenaarf45d7472018-09-30 18:22:26 +02006917 out_flush_cursor(FALSE, FALSE);
Bram Moolenaar52953192019-08-16 10:27:13 +02006918 while (!got_int && done < msec)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006919 {
Bram Moolenaar975b5272016-03-15 23:10:59 +01006920 wait_now = msec - done > 1000L ? 1000L : msec - done;
6921#ifdef FEAT_TIMERS
6922 {
6923 long due_time = check_due_timer();
6924
6925 if (due_time > 0 && due_time < wait_now)
6926 wait_now = due_time;
6927 }
6928#endif
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02006929#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar28e67e02019-08-15 23:05:49 +02006930 if (has_any_channel() && wait_now > 20L)
6931 wait_now = 20L;
6932#endif
6933#ifdef FEAT_SOUND
6934 if (has_any_sound_callback() && wait_now > 20L)
6935 wait_now = 20L;
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02006936#endif
Bram Moolenaar975b5272016-03-15 23:10:59 +01006937 ui_delay(wait_now, TRUE);
Bram Moolenaar52953192019-08-16 10:27:13 +02006938
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02006939#ifdef FEAT_JOB_CHANNEL
6940 if (has_any_channel())
6941 ui_breakcheck_force(TRUE);
6942 else
6943#endif
6944 ui_breakcheck();
Bram Moolenaar93c88e02015-09-15 14:12:05 +02006945#ifdef MESSAGE_QUEUE
Bram Moolenaar52953192019-08-16 10:27:13 +02006946 // Process the netbeans and clientserver messages that may have been
6947 // received in the call to ui_breakcheck() when the GUI is in use. This
6948 // may occur when running a test case.
Bram Moolenaar93c88e02015-09-15 14:12:05 +02006949 parse_queued_messages();
Bram Moolenaare3cc6d42011-10-20 21:58:34 +02006950#endif
Bram Moolenaar52953192019-08-16 10:27:13 +02006951
6952# ifdef ELAPSED_FUNC
6953 // actual time passed
6954 done = ELAPSED_FUNC(start_tv);
6955# else
6956 // guestimate time passed (will actually be more)
6957 done += wait_now;
6958# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006959 }
Bram Moolenaar1ebff3d2018-07-07 16:18:13 +02006960
6961 // If CTRL-C was typed to interrupt the sleep, drop the CTRL-C from the
6962 // input buffer, otherwise a following call to input() fails.
6963 if (got_int)
6964 (void)vpeekc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006965}
6966
Bram Moolenaar071d4272004-06-13 20:20:40 +00006967/*
6968 * ":winsize" command (obsolete).
6969 */
6970 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006971ex_winsize(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006972{
6973 int w, h;
6974 char_u *arg = eap->arg;
6975 char_u *p;
6976
6977 w = getdigits(&arg);
6978 arg = skipwhite(arg);
6979 p = arg;
6980 h = getdigits(&arg);
6981 if (*p != NUL && *arg == NUL)
6982 set_shellsize(w, h, TRUE);
6983 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006984 emsg(_("E465: :winsize requires two number arguments"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006985}
6986
Bram Moolenaar071d4272004-06-13 20:20:40 +00006987 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006988ex_wincmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006989{
6990 int xchar = NUL;
6991 char_u *p;
6992
6993 if (*eap->arg == 'g' || *eap->arg == Ctrl_G)
6994 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006995 // CTRL-W g and CTRL-W CTRL-G have an extra command character
Bram Moolenaar071d4272004-06-13 20:20:40 +00006996 if (eap->arg[1] == NUL)
6997 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006998 emsg(_(e_invarg));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006999 return;
7000 }
7001 xchar = eap->arg[1];
7002 p = eap->arg + 2;
7003 }
7004 else
7005 p = eap->arg + 1;
7006
7007 eap->nextcmd = check_nextcmd(p);
7008 p = skipwhite(p);
7009 if (*p != NUL && *p != '"' && eap->nextcmd == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007010 emsg(_(e_invarg));
Bram Moolenaar12bde492011-06-13 01:19:56 +02007011 else if (!eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007012 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007013 // Pass flags on for ":vertical wincmd ]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00007014 postponed_split_flags = cmdmod.split;
Bram Moolenaard326ce82007-03-11 14:48:29 +00007015 postponed_split_tab = cmdmod.tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007016 do_window(*eap->arg, eap->addr_count > 0 ? eap->line2 : 0L, xchar);
7017 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00007018 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007019 }
7020}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007021
Bram Moolenaar843ee412004-06-30 16:16:41 +00007022#if defined(FEAT_GUI) || defined(UNIX) || defined(VMS) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007023/*
7024 * ":winpos".
7025 */
7026 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007027ex_winpos(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007028{
7029 int x, y;
7030 char_u *arg = eap->arg;
7031 char_u *p;
7032
7033 if (*arg == NUL)
7034 {
Bram Moolenaar843ee412004-06-30 16:16:41 +00007035# if defined(FEAT_GUI) || defined(MSWIN)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007036# ifdef VIMDLL
7037 if (gui.in_use ? gui_mch_get_winpos(&x, &y) != FAIL :
7038 mch_get_winpos(&x, &y) != FAIL)
7039# elif defined(FEAT_GUI)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007040 if (gui.in_use && gui_mch_get_winpos(&x, &y) != FAIL)
Bram Moolenaar843ee412004-06-30 16:16:41 +00007041# else
7042 if (mch_get_winpos(&x, &y) != FAIL)
7043# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007044 {
7045 sprintf((char *)IObuff, _("Window position: X %d, Y %d"), x, y);
Bram Moolenaar32526b32019-01-19 17:43:09 +01007046 msg((char *)IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007047 }
7048 else
7049# endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007050 emsg(_("E188: Obtaining window position not implemented for this platform"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007051 }
7052 else
7053 {
7054 x = getdigits(&arg);
7055 arg = skipwhite(arg);
7056 p = arg;
7057 y = getdigits(&arg);
7058 if (*p == NUL || *arg != NUL)
7059 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007060 emsg(_("E466: :winpos requires two number arguments"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007061 return;
7062 }
7063# ifdef FEAT_GUI
7064 if (gui.in_use)
7065 gui_mch_set_winpos(x, y);
7066 else if (gui.starting)
7067 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007068 // Remember the coordinates for when the window is opened.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007069 gui_win_x = x;
7070 gui_win_y = y;
7071 }
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007072# if defined(HAVE_TGETENT) || defined(VIMDLL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007073 else
7074# endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007075# endif
7076# if defined(MSWIN) && (!defined(FEAT_GUI) || defined(VIMDLL))
Bram Moolenaar843ee412004-06-30 16:16:41 +00007077 mch_set_winpos(x, y);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007078# endif
7079# ifdef HAVE_TGETENT
7080 if (*T_CWP)
7081 term_set_winpos(x, y);
7082# endif
7083 }
7084}
7085#endif
7086
7087/*
7088 * Handle command that work like operators: ":delete", ":yank", ":>" and ":<".
7089 */
7090 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007091ex_operators(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007092{
7093 oparg_T oa;
7094
7095 clear_oparg(&oa);
7096 oa.regname = eap->regname;
7097 oa.start.lnum = eap->line1;
7098 oa.end.lnum = eap->line2;
7099 oa.line_count = eap->line2 - eap->line1 + 1;
7100 oa.motion_type = MLINE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007101 virtual_op = FALSE;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007102 if (eap->cmdidx != CMD_yank) // position cursor for undo
Bram Moolenaar071d4272004-06-13 20:20:40 +00007103 {
7104 setpcmark();
7105 curwin->w_cursor.lnum = eap->line1;
7106 beginline(BL_SOL | BL_FIX);
7107 }
7108
Bram Moolenaard07c6e12013-11-21 14:21:40 +01007109 if (VIsual_active)
7110 end_visual_mode();
Bram Moolenaard07c6e12013-11-21 14:21:40 +01007111
Bram Moolenaar071d4272004-06-13 20:20:40 +00007112 switch (eap->cmdidx)
7113 {
7114 case CMD_delete:
7115 oa.op_type = OP_DELETE;
7116 op_delete(&oa);
7117 break;
7118
7119 case CMD_yank:
7120 oa.op_type = OP_YANK;
7121 (void)op_yank(&oa, FALSE, TRUE);
7122 break;
7123
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007124 default: // CMD_rshift or CMD_lshift
Bram Moolenaar6e707362013-06-14 19:15:58 +02007125 if (
Bram Moolenaar071d4272004-06-13 20:20:40 +00007126#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6e707362013-06-14 19:15:58 +02007127 (eap->cmdidx == CMD_rshift) ^ curwin->w_p_rl
7128#else
7129 eap->cmdidx == CMD_rshift
Bram Moolenaar071d4272004-06-13 20:20:40 +00007130#endif
Bram Moolenaar6e707362013-06-14 19:15:58 +02007131 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00007132 oa.op_type = OP_RSHIFT;
7133 else
7134 oa.op_type = OP_LSHIFT;
7135 op_shift(&oa, FALSE, eap->amount);
7136 break;
7137 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007138 virtual_op = MAYBE;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007139 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007140}
7141
7142/*
7143 * ":put".
7144 */
7145 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007146ex_put(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007147{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007148 // ":0put" works like ":1put!".
Bram Moolenaar071d4272004-06-13 20:20:40 +00007149 if (eap->line2 == 0)
7150 {
7151 eap->line2 = 1;
7152 eap->forceit = TRUE;
7153 }
7154 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007155 do_put(eap->regname, eap->forceit ? BACKWARD : FORWARD, 1L,
7156 PUT_LINE|PUT_CURSLINE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007157}
7158
7159/*
7160 * Handle ":copy" and ":move".
7161 */
7162 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007163ex_copymove(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007164{
7165 long n;
7166
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02007167 n = get_address(eap, &eap->arg, eap->addr_type, FALSE, FALSE, FALSE, 1);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007168 if (eap->arg == NULL) // error detected
Bram Moolenaar071d4272004-06-13 20:20:40 +00007169 {
7170 eap->nextcmd = NULL;
7171 return;
7172 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00007173 get_flags(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007174
7175 /*
7176 * move or copy lines from 'eap->line1'-'eap->line2' to below line 'n'
7177 */
7178 if (n == MAXLNUM || n < 0 || n > curbuf->b_ml.ml_line_count)
7179 {
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02007180 emsg(_(e_invrange));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007181 return;
7182 }
7183
7184 if (eap->cmdidx == CMD_move)
7185 {
7186 if (do_move(eap->line1, eap->line2, n) == FAIL)
7187 return;
7188 }
7189 else
7190 ex_copy(eap->line1, eap->line2, n);
7191 u_clearline();
7192 beginline(BL_SOL | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007193 ex_may_print(eap);
7194}
7195
7196/*
7197 * Print the current line if flags were given to the Ex command.
7198 */
Bram Moolenaarfd3fe982014-04-01 17:49:44 +02007199 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007200ex_may_print(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007201{
7202 if (eap->flags != 0)
7203 {
7204 print_line(curwin->w_cursor.lnum, (eap->flags & EXFLAG_NR),
7205 (eap->flags & EXFLAG_LIST));
7206 ex_no_reprint = TRUE;
7207 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007208}
7209
7210/*
7211 * ":smagic" and ":snomagic".
7212 */
7213 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007214ex_submagic(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007215{
7216 int magic_save = p_magic;
7217
7218 p_magic = (eap->cmdidx == CMD_smagic);
7219 do_sub(eap);
7220 p_magic = magic_save;
7221}
7222
7223/*
7224 * ":join".
7225 */
7226 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007227ex_join(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007228{
7229 curwin->w_cursor.lnum = eap->line1;
7230 if (eap->line1 == eap->line2)
7231 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007232 if (eap->addr_count >= 2) // :2,2join does nothing
Bram Moolenaar071d4272004-06-13 20:20:40 +00007233 return;
7234 if (eap->line2 == curbuf->b_ml.ml_line_count)
7235 {
7236 beep_flush();
7237 return;
7238 }
7239 ++eap->line2;
7240 }
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02007241 (void)do_join(eap->line2 - eap->line1 + 1, !eap->forceit, TRUE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007242 beginline(BL_WHITE | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007243 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007244}
7245
7246/*
7247 * ":[addr]@r" or ":[addr]*r": execute register
7248 */
7249 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007250ex_at(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007251{
7252 int c;
Bram Moolenaar60462872009-11-03 11:40:19 +00007253 int prev_len = typebuf.tb_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007254
7255 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaar4930a762016-09-11 14:39:53 +02007256 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007257
7258#ifdef USE_ON_FLY_SCROLL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007259 dont_scroll = TRUE; // disallow scrolling here
Bram Moolenaar071d4272004-06-13 20:20:40 +00007260#endif
7261
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007262 // get the register name. No name means to use the previous one
Bram Moolenaar071d4272004-06-13 20:20:40 +00007263 c = *eap->arg;
7264 if (c == NUL || (c == '*' && *eap->cmd == '*'))
7265 c = '@';
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007266 // Put the register in the typeahead buffer with the "silent" flag.
Bram Moolenaard333d1e2006-11-07 17:43:47 +00007267 if (do_execreg(c, TRUE, vim_strchr(p_cpo, CPO_EXECBUF) != NULL, TRUE)
7268 == FAIL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007269 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007270 beep_flush();
Bram Moolenaardf177f62005-02-22 08:39:57 +00007271 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007272 else
7273 {
7274 int save_efr = exec_from_reg;
7275
7276 exec_from_reg = TRUE;
7277
7278 /*
7279 * Execute from the typeahead buffer.
Bram Moolenaar60462872009-11-03 11:40:19 +00007280 * Continue until the stuff buffer is empty and all added characters
7281 * have been consumed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007282 */
Bram Moolenaar60462872009-11-03 11:40:19 +00007283 while (!stuff_empty() || typebuf.tb_len > prev_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007284 (void)do_cmdline(NULL, getexline, NULL, DOCMD_NOWAIT|DOCMD_VERBOSE);
7285
7286 exec_from_reg = save_efr;
7287 }
7288}
7289
7290/*
7291 * ":!".
7292 */
7293 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007294ex_bang(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007295{
7296 do_bang(eap->addr_count, eap, eap->forceit, TRUE, TRUE);
7297}
7298
7299/*
7300 * ":undo".
7301 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007302 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +01007303ex_undo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007304{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007305 if (eap->addr_count == 1) // :undo 123
Bram Moolenaar730cde92010-06-27 05:18:54 +02007306 undo_time(eap->line2, FALSE, FALSE, TRUE);
Bram Moolenaard3667a22006-03-16 21:35:52 +00007307 else
7308 u_undo(1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007309}
7310
Bram Moolenaar55debbe2010-05-23 23:34:36 +02007311#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar6df6f472010-07-18 18:04:50 +02007312 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007313ex_wundo(exarg_T *eap)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02007314{
7315 char_u hash[UNDO_HASH_SIZE];
7316
7317 u_compute_hash(hash);
7318 u_write_undo(eap->arg, eap->forceit, curbuf, hash);
7319}
7320
Bram Moolenaar6df6f472010-07-18 18:04:50 +02007321 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007322ex_rundo(exarg_T *eap)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02007323{
7324 char_u hash[UNDO_HASH_SIZE];
7325
7326 u_compute_hash(hash);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02007327 u_read_undo(eap->arg, hash, NULL);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02007328}
7329#endif
7330
Bram Moolenaar071d4272004-06-13 20:20:40 +00007331/*
7332 * ":redo".
7333 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007334 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007335ex_redo(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007336{
7337 u_redo(1);
7338}
7339
7340/*
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007341 * ":earlier" and ":later".
7342 */
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007343 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007344ex_later(exarg_T *eap)
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007345{
7346 long count = 0;
7347 int sec = FALSE;
Bram Moolenaar730cde92010-06-27 05:18:54 +02007348 int file = FALSE;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007349 char_u *p = eap->arg;
7350
7351 if (*p == NUL)
7352 count = 1;
7353 else if (isdigit(*p))
7354 {
7355 count = getdigits(&p);
7356 switch (*p)
7357 {
7358 case 's': ++p; sec = TRUE; break;
7359 case 'm': ++p; sec = TRUE; count *= 60; break;
7360 case 'h': ++p; sec = TRUE; count *= 60 * 60; break;
Bram Moolenaar730cde92010-06-27 05:18:54 +02007361 case 'd': ++p; sec = TRUE; count *= 24 * 60 * 60; break;
7362 case 'f': ++p; file = TRUE; break;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007363 }
7364 }
7365
7366 if (*p != NUL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007367 semsg(_(e_invarg2), eap->arg);
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007368 else
Bram Moolenaar730cde92010-06-27 05:18:54 +02007369 undo_time(eap->cmdidx == CMD_earlier ? -count : count,
7370 sec, file, FALSE);
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007371}
7372
7373/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007374 * ":redir": start/stop redirection.
7375 */
7376 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007377ex_redir(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007378{
7379 char *mode;
7380 char_u *fname;
Bram Moolenaarca472992005-01-21 11:46:23 +00007381 char_u *arg = eap->arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007382
Bram Moolenaarba768492016-07-08 15:32:54 +02007383#ifdef FEAT_EVAL
Bram Moolenaar79815f12016-07-09 17:07:29 +02007384 if (redir_execute)
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02007385 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007386 emsg(_("E930: Cannot use :redir inside execute()"));
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02007387 return;
7388 }
Bram Moolenaarba768492016-07-08 15:32:54 +02007389#endif
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02007390
Bram Moolenaar071d4272004-06-13 20:20:40 +00007391 if (STRICMP(eap->arg, "END") == 0)
7392 close_redir();
7393 else
7394 {
Bram Moolenaarca472992005-01-21 11:46:23 +00007395 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00007396 {
Bram Moolenaarca472992005-01-21 11:46:23 +00007397 ++arg;
7398 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00007399 {
Bram Moolenaarca472992005-01-21 11:46:23 +00007400 ++arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007401 mode = "a";
7402 }
7403 else
7404 mode = "w";
Bram Moolenaarca472992005-01-21 11:46:23 +00007405 arg = skipwhite(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007406
7407 close_redir();
7408
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007409 // Expand environment variables and "~/".
Bram Moolenaarca472992005-01-21 11:46:23 +00007410 fname = expand_env_save(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007411 if (fname == NULL)
7412 return;
7413#ifdef FEAT_BROWSE
7414 if (cmdmod.browse)
7415 {
7416 char_u *browseFile;
7417
Bram Moolenaar7171abe2004-10-11 10:06:20 +00007418 browseFile = do_browse(BROWSE_SAVE,
7419 (char_u *)_("Save Redirection"),
Bram Moolenaarc36651b2018-04-29 12:22:56 +02007420 fname, NULL, NULL,
7421 (char_u *)_(BROWSE_FILTER_ALL_FILES), curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007422 if (browseFile == NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007423 return; // operation cancelled
Bram Moolenaar071d4272004-06-13 20:20:40 +00007424 vim_free(fname);
7425 fname = browseFile;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007426 eap->forceit = TRUE; // since dialog already asked
Bram Moolenaar071d4272004-06-13 20:20:40 +00007427 }
7428#endif
7429
7430 redir_fd = open_exfile(fname, eap->forceit, mode);
7431 vim_free(fname);
7432 }
7433#ifdef FEAT_EVAL
Bram Moolenaarca472992005-01-21 11:46:23 +00007434 else if (*arg == '@')
Bram Moolenaar071d4272004-06-13 20:20:40 +00007435 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007436 // redirect to a register a-z (resp. A-Z for appending)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007437 close_redir();
Bram Moolenaarca472992005-01-21 11:46:23 +00007438 ++arg;
7439 if (ASCII_ISALPHA(*arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007440# ifdef FEAT_CLIPBOARD
Bram Moolenaarca472992005-01-21 11:46:23 +00007441 || *arg == '*'
Bram Moolenaar9a51c6e2006-11-14 19:25:02 +00007442 || *arg == '+'
Bram Moolenaar071d4272004-06-13 20:20:40 +00007443# endif
Bram Moolenaarca472992005-01-21 11:46:23 +00007444 || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00007445 {
Bram Moolenaarca472992005-01-21 11:46:23 +00007446 redir_reg = *arg++;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007447 if (*arg == '>' && arg[1] == '>') // append
Bram Moolenaard9d30582005-05-18 22:10:28 +00007448 arg += 2;
Bram Moolenaarc188b882007-10-19 14:20:54 +00007449 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007450 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007451 // Can use both "@a" and "@a>".
Bram Moolenaar2c29bee2005-06-01 21:46:07 +00007452 if (*arg == '>')
7453 arg++;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007454 // Make register empty when not using @A-@Z and the
7455 // command is valid.
Bram Moolenaarc188b882007-10-19 14:20:54 +00007456 if (*arg == NUL && !isupper(redir_reg))
7457 write_reg_contents(redir_reg, (char_u *)"", -1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007458 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00007459 }
7460 if (*arg != NUL)
7461 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00007462 redir_reg = 0;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007463 semsg(_(e_invarg2), eap->arg);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007464 }
7465 }
7466 else if (*arg == '=' && arg[1] == '>')
7467 {
7468 int append;
7469
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007470 // redirect to a variable
Bram Moolenaardf177f62005-02-22 08:39:57 +00007471 close_redir();
7472 arg += 2;
7473
7474 if (*arg == '>')
7475 {
7476 ++arg;
7477 append = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007478 }
7479 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00007480 append = FALSE;
7481
7482 if (var_redir_start(skipwhite(arg), append) == OK)
7483 redir_vname = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007484 }
7485#endif
7486
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007487 // TODO: redirect to a buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00007488
Bram Moolenaar071d4272004-06-13 20:20:40 +00007489 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007490 semsg(_(e_invarg2), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007491 }
Bram Moolenaar29b2d262006-09-10 19:07:28 +00007492
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007493 // Make sure redirection is not off. Can happen for cmdline completion
7494 // that indirectly invokes a command to catch its output.
Bram Moolenaar29b2d262006-09-10 19:07:28 +00007495 if (redir_fd != NULL
7496#ifdef FEAT_EVAL
7497 || redir_reg || redir_vname
7498#endif
7499 )
7500 redir_off = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007501}
7502
7503/*
7504 * ":redraw": force redraw
7505 */
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01007506 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007507ex_redraw(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007508{
7509 int r = RedrawingDisabled;
7510 int p = p_lz;
7511
7512 RedrawingDisabled = 0;
7513 p_lz = FALSE;
Bram Moolenaarabc39ab2017-03-01 18:04:05 +01007514 validate_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007515 update_topline();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007516 update_screen(eap->forceit ? CLEAR : VIsual_active ? INVERTED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007517#ifdef FEAT_TITLE
7518 if (need_maketitle)
7519 maketitle();
7520#endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007521#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
7522# ifdef VIMDLL
7523 if (!gui.in_use)
7524# endif
7525 resize_console_buf();
Bram Moolenaar78d21da2019-02-17 15:00:52 +01007526#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007527 RedrawingDisabled = r;
7528 p_lz = p;
7529
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007530 // Reset msg_didout, so that a message that's there is overwritten.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007531 msg_didout = FALSE;
7532 msg_col = 0;
7533
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007534 // No need to wait after an intentional redraw.
Bram Moolenaar56667a52013-07-17 11:54:28 +02007535 need_wait_return = FALSE;
7536
Bram Moolenaar071d4272004-06-13 20:20:40 +00007537 out_flush();
7538}
7539
7540/*
7541 * ":redrawstatus": force redraw of status line(s)
7542 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007543 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007544ex_redrawstatus(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007545{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007546 int r = RedrawingDisabled;
7547 int p = p_lz;
7548
7549 RedrawingDisabled = 0;
7550 p_lz = FALSE;
7551 if (eap->forceit)
7552 status_redraw_all();
7553 else
7554 status_redraw_curbuf();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007555 update_screen(VIsual_active ? INVERTED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007556 RedrawingDisabled = r;
7557 p_lz = p;
7558 out_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007559}
7560
Bram Moolenaare12bab32019-01-08 22:02:56 +01007561/*
7562 * ":redrawtabline": force redraw of the tabline
7563 */
7564 static void
7565ex_redrawtabline(exarg_T *eap UNUSED)
7566{
7567 int r = RedrawingDisabled;
7568 int p = p_lz;
7569
7570 RedrawingDisabled = 0;
7571 p_lz = FALSE;
7572
7573 draw_tabline();
7574
7575 RedrawingDisabled = r;
7576 p_lz = p;
7577 out_flush();
7578}
7579
Bram Moolenaar071d4272004-06-13 20:20:40 +00007580 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007581close_redir(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007582{
7583 if (redir_fd != NULL)
7584 {
7585 fclose(redir_fd);
7586 redir_fd = NULL;
7587 }
7588#ifdef FEAT_EVAL
7589 redir_reg = 0;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007590 if (redir_vname)
7591 {
7592 var_redir_stop();
7593 redir_vname = 0;
7594 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007595#endif
7596}
7597
Bram Moolenaarf96ae0b2019-07-28 15:21:55 +02007598#if (defined(FEAT_SESSION) || defined(FEAT_EVAL)) || defined(PROTO)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007599 int
Bram Moolenaarbd67aac2019-09-21 23:09:04 +02007600vim_mkdir_emsg(char_u *name, int prot UNUSED)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007601{
7602 if (vim_mkdir(name, prot) != 0)
7603 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007604 semsg(_("E739: Cannot create directory: %s"), name);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007605 return FAIL;
7606 }
7607 return OK;
7608}
7609#endif
7610
Bram Moolenaar071d4272004-06-13 20:20:40 +00007611/*
7612 * Open a file for writing for an Ex command, with some checks.
7613 * Return file descriptor, or NULL on failure.
7614 */
7615 FILE *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007616open_exfile(
7617 char_u *fname,
7618 int forceit,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007619 char *mode) // "w" for create new file or "a" for append
Bram Moolenaar071d4272004-06-13 20:20:40 +00007620{
7621 FILE *fd;
7622
7623#ifdef UNIX
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007624 // with Unix it is possible to open a directory
Bram Moolenaar071d4272004-06-13 20:20:40 +00007625 if (mch_isdir(fname))
7626 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007627 semsg(_(e_isadir2), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007628 return NULL;
7629 }
7630#endif
7631 if (!forceit && *mode != 'a' && vim_fexists(fname))
7632 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007633 semsg(_("E189: \"%s\" exists (add ! to override)"), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007634 return NULL;
7635 }
7636
7637 if ((fd = mch_fopen((char *)fname, mode)) == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007638 semsg(_("E190: Cannot open \"%s\" for writing"), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007639
7640 return fd;
7641}
7642
7643/*
7644 * ":mark" and ":k".
7645 */
7646 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007647ex_mark(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007648{
7649 pos_T pos;
7650
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007651 if (*eap->arg == NUL) // No argument?
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007652 emsg(_(e_argreq));
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007653 else if (eap->arg[1] != NUL) // more than one character?
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007654 emsg(_(e_trailing));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007655 else
7656 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007657 pos = curwin->w_cursor; // save curwin->w_cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00007658 curwin->w_cursor.lnum = eap->line2;
7659 beginline(BL_WHITE | BL_FIX);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007660 if (setmark(*eap->arg) == FAIL) // set mark
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007661 emsg(_("E191: Argument must be a letter or forward/backward quote"));
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007662 curwin->w_cursor = pos; // restore curwin->w_cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00007663 }
7664}
7665
7666/*
7667 * Update w_topline, w_leftcol and the cursor position.
7668 */
7669 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007670update_topline_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007671{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007672 check_cursor(); // put cursor on valid line
Bram Moolenaar071d4272004-06-13 20:20:40 +00007673 update_topline();
7674 if (!curwin->w_p_wrap)
7675 validate_cursor();
7676 update_curswant();
7677}
7678
Bram Moolenaar071d4272004-06-13 20:20:40 +00007679/*
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007680 * Save the current State and go to Normal mode.
7681 * Return TRUE if the typeahead could be saved.
7682 */
7683 int
7684save_current_state(save_state_T *sst)
7685{
7686 sst->save_msg_scroll = msg_scroll;
7687 sst->save_restart_edit = restart_edit;
7688 sst->save_msg_didout = msg_didout;
7689 sst->save_State = State;
7690 sst->save_insertmode = p_im;
7691 sst->save_finish_op = finish_op;
7692 sst->save_opcount = opcount;
Bram Moolenaarcce713d2019-03-04 11:40:12 +01007693 sst->save_reg_executing = reg_executing;
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007694
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007695 msg_scroll = FALSE; // no msg scrolling in Normal mode
7696 restart_edit = 0; // don't go to Insert mode
7697 p_im = FALSE; // don't use 'insertmode'
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007698
7699 /*
7700 * Save the current typeahead. This is required to allow using ":normal"
7701 * from an event handler and makes sure we don't hang when the argument
7702 * ends with half a command.
7703 */
7704 save_typeahead(&sst->tabuf);
7705 return sst->tabuf.typebuf_valid;
7706}
7707
7708 void
7709restore_current_state(save_state_T *sst)
7710{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007711 // Restore the previous typeahead.
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007712 restore_typeahead(&sst->tabuf);
7713
7714 msg_scroll = sst->save_msg_scroll;
7715 restart_edit = sst->save_restart_edit;
7716 p_im = sst->save_insertmode;
7717 finish_op = sst->save_finish_op;
7718 opcount = sst->save_opcount;
Bram Moolenaarcce713d2019-03-04 11:40:12 +01007719 reg_executing = sst->save_reg_executing;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007720 msg_didout |= sst->save_msg_didout; // don't reset msg_didout now
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007721
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007722 // Restore the state (needed when called from a function executed for
7723 // 'indentexpr'). Update the mouse and cursor, they may have changed.
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007724 State = sst->save_State;
7725#ifdef CURSOR_SHAPE
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007726 ui_cursor_shape(); // may show different cursor shape
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007727#endif
7728}
7729
7730/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007731 * ":normal[!] {commands}": Execute normal mode commands.
7732 */
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01007733 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007734ex_normal(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007735{
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007736 save_state_T save_state;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007737 char_u *arg = NULL;
7738 int l;
7739 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007740
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00007741 if (ex_normal_lock > 0)
7742 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007743 emsg(_(e_secure));
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00007744 return;
7745 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007746 if (ex_normal_busy >= p_mmd)
7747 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007748 emsg(_("E192: Recursive use of :normal too deep"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007749 return;
7750 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007751
Bram Moolenaar071d4272004-06-13 20:20:40 +00007752 /*
7753 * vgetc() expects a CSI and K_SPECIAL to have been escaped. Don't do
7754 * this for the K_SPECIAL leading byte, otherwise special keys will not
7755 * work.
7756 */
7757 if (has_mbyte)
7758 {
7759 int len = 0;
7760
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007761 // Count the number of characters to be escaped.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007762 for (p = eap->arg; *p != NUL; ++p)
7763 {
Bram Moolenaar13505972019-01-24 15:04:48 +01007764#ifdef FEAT_GUI
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007765 if (*p == CSI) // leadbyte CSI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007766 len += 2;
Bram Moolenaar13505972019-01-24 15:04:48 +01007767#endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007768 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007769 if (*++p == K_SPECIAL // trailbyte K_SPECIAL or CSI
Bram Moolenaar13505972019-01-24 15:04:48 +01007770#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007771 || *p == CSI
Bram Moolenaar13505972019-01-24 15:04:48 +01007772#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007773 )
7774 len += 2;
7775 }
7776 if (len > 0)
7777 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02007778 arg = alloc(STRLEN(eap->arg) + len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007779 if (arg != NULL)
7780 {
7781 len = 0;
7782 for (p = eap->arg; *p != NUL; ++p)
7783 {
7784 arg[len++] = *p;
Bram Moolenaar13505972019-01-24 15:04:48 +01007785#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007786 if (*p == CSI)
7787 {
7788 arg[len++] = KS_EXTRA;
7789 arg[len++] = (int)KE_CSI;
7790 }
Bram Moolenaar13505972019-01-24 15:04:48 +01007791#endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007792 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007793 {
7794 arg[len++] = *++p;
7795 if (*p == K_SPECIAL)
7796 {
7797 arg[len++] = KS_SPECIAL;
7798 arg[len++] = KE_FILLER;
7799 }
Bram Moolenaar13505972019-01-24 15:04:48 +01007800#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007801 else if (*p == CSI)
7802 {
7803 arg[len++] = KS_EXTRA;
7804 arg[len++] = (int)KE_CSI;
7805 }
Bram Moolenaar13505972019-01-24 15:04:48 +01007806#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007807 }
7808 arg[len] = NUL;
7809 }
7810 }
7811 }
7812 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007813
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007814 ++ex_normal_busy;
7815 if (save_current_state(&save_state))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007816 {
7817 /*
7818 * Repeat the :normal command for each line in the range. When no
7819 * range given, execute it just once, without positioning the cursor
7820 * first.
7821 */
7822 do
7823 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007824 if (eap->addr_count != 0)
7825 {
7826 curwin->w_cursor.lnum = eap->line1++;
7827 curwin->w_cursor.col = 0;
Bram Moolenaard5d37532017-03-27 23:02:07 +02007828 check_cursor_moved(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007829 }
7830
Bram Moolenaar13505972019-01-24 15:04:48 +01007831 exec_normal_cmd(arg != NULL
7832 ? arg
7833 : eap->arg, eap->forceit ? REMAP_NONE : REMAP_YES, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007834 }
7835 while (eap->addr_count > 0 && eap->line1 <= eap->line2 && !got_int);
7836 }
7837
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007838 // Might not return to the main loop when in an event handler.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007839 update_topline_cursor();
7840
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007841 restore_current_state(&save_state);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007842 --ex_normal_busy;
Bram Moolenaareda73602014-11-05 09:53:23 +01007843 setmouse();
Bram Moolenaareda73602014-11-05 09:53:23 +01007844#ifdef CURSOR_SHAPE
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007845 ui_cursor_shape(); // may show different cursor shape
Bram Moolenaareda73602014-11-05 09:53:23 +01007846#endif
7847
Bram Moolenaar071d4272004-06-13 20:20:40 +00007848 vim_free(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007849}
7850
7851/*
Bram Moolenaar64969662005-12-14 21:59:55 +00007852 * ":startinsert", ":startreplace" and ":startgreplace"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007853 */
7854 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007855ex_startinsert(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007856{
Bram Moolenaarfd371682005-01-14 21:42:54 +00007857 if (eap->forceit)
7858 {
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02007859 // cursor line can be zero on startup
Bram Moolenaar09ca9322017-09-26 17:40:45 +02007860 if (!curwin->w_cursor.lnum)
7861 curwin->w_cursor.lnum = 1;
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02007862 set_cursor_for_append_to_line();
Bram Moolenaarfd371682005-01-14 21:42:54 +00007863 }
Bram Moolenaareb58a242020-04-19 18:13:19 +02007864#ifdef FEAT_TERMINAL
7865 // Ignore this when running in an active terminal.
7866 if (term_job_running(curbuf->b_term))
7867 return;
7868#endif
Bram Moolenaarfd371682005-01-14 21:42:54 +00007869
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02007870 // Ignore the command when already in Insert mode. Inserting an
7871 // expression register that invokes a function can do this.
Bram Moolenaara40c5002005-01-09 21:16:21 +00007872 if (State & INSERT)
7873 return;
7874
Bram Moolenaar64969662005-12-14 21:59:55 +00007875 if (eap->cmdidx == CMD_startinsert)
7876 restart_edit = 'a';
7877 else if (eap->cmdidx == CMD_startreplace)
7878 restart_edit = 'R';
Bram Moolenaar071d4272004-06-13 20:20:40 +00007879 else
Bram Moolenaar64969662005-12-14 21:59:55 +00007880 restart_edit = 'V';
7881
7882 if (!eap->forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007883 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00007884 if (eap->cmdidx == CMD_startinsert)
7885 restart_edit = 'i';
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02007886 curwin->w_curswant = 0; // avoid MAXCOL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007887 }
7888}
7889
7890/*
7891 * ":stopinsert"
7892 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007893 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007894ex_stopinsert(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007895{
7896 restart_edit = 0;
7897 stop_insert_mode = TRUE;
Bram Moolenaarfd773e92016-04-02 19:39:16 +02007898 clearmode();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007899}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007900
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00007901/*
7902 * Execute normal mode command "cmd".
7903 * "remap" can be REMAP_NONE or REMAP_YES.
7904 */
7905 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007906exec_normal_cmd(char_u *cmd, int remap, int silent)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00007907{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007908 // Stuff the argument into the typeahead buffer.
Bram Moolenaar25281632016-01-21 23:32:32 +01007909 ins_typebuf(cmd, remap, 0, TRUE, silent);
Bram Moolenaar586c70c2018-10-02 16:23:58 +02007910 exec_normal(FALSE, FALSE, FALSE);
Bram Moolenaar25281632016-01-21 23:32:32 +01007911}
Bram Moolenaar25281632016-01-21 23:32:32 +01007912
Bram Moolenaar25281632016-01-21 23:32:32 +01007913/*
7914 * Execute normal_cmd() until there is no typeahead left.
Bram Moolenaar586c70c2018-10-02 16:23:58 +02007915 * When "use_vpeekc" is TRUE use vpeekc() to check for available chars.
Bram Moolenaar25281632016-01-21 23:32:32 +01007916 */
7917 void
Bram Moolenaar586c70c2018-10-02 16:23:58 +02007918exec_normal(int was_typed, int use_vpeekc, int may_use_terminal_loop UNUSED)
Bram Moolenaar25281632016-01-21 23:32:32 +01007919{
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00007920 oparg_T oa;
Bram Moolenaar905dd902019-04-07 14:21:47 +02007921 int c;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00007922
Bram Moolenaar905dd902019-04-07 14:21:47 +02007923 // When calling vpeekc() from feedkeys() it will return Ctrl_C when there
7924 // is nothing to get, so also check for Ctrl_C.
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00007925 clear_oparg(&oa);
7926 finish_op = FALSE;
Bram Moolenaar586c70c2018-10-02 16:23:58 +02007927 while ((!stuff_empty()
7928 || ((was_typed || !typebuf_typed()) && typebuf.tb_len > 0)
Bram Moolenaar905dd902019-04-07 14:21:47 +02007929 || (use_vpeekc && (c = vpeekc()) != NUL && c != Ctrl_C))
Bram Moolenaar586c70c2018-10-02 16:23:58 +02007930 && !got_int)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00007931 {
7932 update_topline_cursor();
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02007933#ifdef FEAT_TERMINAL
Bram Moolenaar655a82a2018-05-08 22:01:07 +02007934 if (may_use_terminal_loop && term_use_loop()
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02007935 && oa.op_type == OP_NOP && oa.regname == NUL
7936 && !VIsual_active)
7937 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007938 // If terminal_loop() returns OK we got a key that is handled
7939 // in Normal model. With FAIL we first need to position the
7940 // cursor and the screen needs to be redrawn.
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02007941 if (terminal_loop(TRUE) == OK)
7942 normal_cmd(&oa, TRUE);
7943 }
7944 else
7945#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007946 // execute a Normal mode cmd
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02007947 normal_cmd(&oa, TRUE);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00007948 }
7949}
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00007950
Bram Moolenaar071d4272004-06-13 20:20:40 +00007951#ifdef FEAT_FIND_ID
7952 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007953ex_checkpath(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007954{
7955 find_pattern_in_path(NULL, 0, 0, FALSE, FALSE, CHECK_PATH, 1L,
7956 eap->forceit ? ACTION_SHOW_ALL : ACTION_SHOW,
7957 (linenr_T)1, (linenr_T)MAXLNUM);
7958}
7959
Bram Moolenaar4033c552017-09-16 20:54:51 +02007960#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007961/*
7962 * ":psearch"
7963 */
7964 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007965ex_psearch(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007966{
7967 g_do_tagpreview = p_pvh;
7968 ex_findpat(eap);
7969 g_do_tagpreview = 0;
7970}
7971#endif
7972
7973 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007974ex_findpat(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007975{
7976 int whole = TRUE;
7977 long n;
7978 char_u *p;
7979 int action;
7980
7981 switch (cmdnames[eap->cmdidx].cmd_name[2])
7982 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007983 case 'e': // ":psearch", ":isearch" and ":dsearch"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007984 if (cmdnames[eap->cmdidx].cmd_name[0] == 'p')
7985 action = ACTION_GOTO;
7986 else
7987 action = ACTION_SHOW;
7988 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007989 case 'i': // ":ilist" and ":dlist"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007990 action = ACTION_SHOW_ALL;
7991 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007992 case 'u': // ":ijump" and ":djump"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007993 action = ACTION_GOTO;
7994 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007995 default: // ":isplit" and ":dsplit"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007996 action = ACTION_SPLIT;
7997 break;
7998 }
7999
8000 n = 1;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008001 if (vim_isdigit(*eap->arg)) // get count
Bram Moolenaar071d4272004-06-13 20:20:40 +00008002 {
8003 n = getdigits(&eap->arg);
8004 eap->arg = skipwhite(eap->arg);
8005 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008006 if (*eap->arg == '/') // Match regexp, not just whole words
Bram Moolenaar071d4272004-06-13 20:20:40 +00008007 {
8008 whole = FALSE;
8009 ++eap->arg;
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02008010 p = skip_regexp(eap->arg, '/', p_magic);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008011 if (*p)
8012 {
8013 *p++ = NUL;
8014 p = skipwhite(p);
8015
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008016 // Check for trailing illegal characters
Bram Moolenaarfaac4102020-04-20 17:46:14 +02008017 if (!ends_excmd2(eap->arg, p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008018 eap->errmsg = e_trailing;
8019 else
8020 eap->nextcmd = check_nextcmd(p);
8021 }
8022 }
8023 if (!eap->skip)
8024 find_pattern_in_path(eap->arg, 0, (int)STRLEN(eap->arg),
8025 whole, !eap->forceit,
8026 *eap->cmd == 'd' ? FIND_DEFINE : FIND_ANY,
8027 n, action, eap->line1, eap->line2);
8028}
8029#endif
8030
Bram Moolenaar071d4272004-06-13 20:20:40 +00008031
Bram Moolenaar4033c552017-09-16 20:54:51 +02008032#ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00008033/*
8034 * ":ptag", ":ptselect", ":ptjump", ":ptnext", etc.
8035 */
8036 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008037ex_ptag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008038{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008039 g_do_tagpreview = p_pvh; // will be reset to 0 in ex_tag_cmd()
Bram Moolenaar071d4272004-06-13 20:20:40 +00008040 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
8041}
8042
8043/*
8044 * ":pedit"
8045 */
8046 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008047ex_pedit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008048{
8049 win_T *curwin_save = curwin;
8050
Bram Moolenaar3a2505c2020-03-09 16:40:41 +01008051 if (ERROR_IF_ANY_POPUP_WINDOW)
8052 return;
8053
Bram Moolenaar026587b2019-08-17 15:08:00 +02008054 // Open the preview window or popup and make it the current window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008055 g_do_tagpreview = p_pvh;
Bram Moolenaar576a4a62019-08-18 15:25:17 +02008056 prepare_tagpreview(TRUE, TRUE, FALSE);
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02008057
Bram Moolenaar026587b2019-08-17 15:08:00 +02008058 // Edit the file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008059 do_exedit(eap, NULL);
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02008060
Bram Moolenaar071d4272004-06-13 20:20:40 +00008061 if (curwin != curwin_save && win_valid(curwin_save))
8062 {
Bram Moolenaar026587b2019-08-17 15:08:00 +02008063 // Return cursor to where we were
Bram Moolenaar071d4272004-06-13 20:20:40 +00008064 validate_cursor();
8065 redraw_later(VALID);
8066 win_enter(curwin_save, TRUE);
8067 }
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01008068# ifdef FEAT_PROP_POPUP
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02008069 else if (WIN_IS_POPUP(curwin))
8070 {
8071 // can't keep focus in popup window
8072 win_enter(firstwin, TRUE);
8073 }
8074# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008075 g_do_tagpreview = 0;
8076}
Bram Moolenaar4033c552017-09-16 20:54:51 +02008077#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008078
8079/*
8080 * ":stag", ":stselect" and ":stjump".
8081 */
8082 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008083ex_stag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008084{
8085 postponed_split = -1;
8086 postponed_split_flags = cmdmod.split;
Bram Moolenaard326ce82007-03-11 14:48:29 +00008087 postponed_split_tab = cmdmod.tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008088 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
8089 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00008090 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008091}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008092
8093/*
8094 * ":tag", ":tselect", ":tjump", ":tnext", etc.
8095 */
8096 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008097ex_tag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008098{
8099 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name);
8100}
8101
8102 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008103ex_tag_cmd(exarg_T *eap, char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008104{
8105 int cmd;
8106
8107 switch (name[1])
8108 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008109 case 'j': cmd = DT_JUMP; // ":tjump"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008110 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008111 case 's': cmd = DT_SELECT; // ":tselect"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008112 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008113 case 'p': cmd = DT_PREV; // ":tprevious"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008114 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008115 case 'N': cmd = DT_PREV; // ":tNext"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008116 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008117 case 'n': cmd = DT_NEXT; // ":tnext"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008118 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008119 case 'o': cmd = DT_POP; // ":pop"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008120 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008121 case 'f': // ":tfirst"
8122 case 'r': cmd = DT_FIRST; // ":trewind"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008123 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008124 case 'l': cmd = DT_LAST; // ":tlast"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008125 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008126 default: // ":tag"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008127#ifdef FEAT_CSCOPE
Bram Moolenaar7c94c262008-06-20 09:11:34 +00008128 if (p_cst && *eap->arg != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008129 {
Bram Moolenaard4db7712016-11-12 19:16:46 +01008130 ex_cstag(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008131 return;
8132 }
8133#endif
8134 cmd = DT_TAG;
8135 break;
8136 }
8137
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00008138 if (name[0] == 'l')
8139 {
8140#ifndef FEAT_QUICKFIX
8141 ex_ni(eap);
8142 return;
8143#else
8144 cmd = DT_LTAG;
8145#endif
8146 }
8147
Bram Moolenaar071d4272004-06-13 20:20:40 +00008148 do_tag(eap->arg, cmd, eap->addr_count > 0 ? (int)eap->line2 : 1,
8149 eap->forceit, TRUE);
8150}
8151
8152/*
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008153 * Check "str" for starting with a special cmdline variable.
8154 * If found return one of the SPEC_ values and set "*usedlen" to the length of
8155 * the variable. Otherwise return -1 and "*usedlen" is unchanged.
8156 */
8157 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008158find_cmdline_var(char_u *src, int *usedlen)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008159{
8160 int len;
8161 int i;
Bram Moolenaar8f0b2d42009-05-16 14:41:10 +00008162 static char *(spec_str[]) = {
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008163 "%",
8164#define SPEC_PERC 0
8165 "#",
Bram Moolenaar65f08472017-09-10 18:16:20 +02008166#define SPEC_HASH (SPEC_PERC + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008167 "<cword>", // cursor word
Bram Moolenaar65f08472017-09-10 18:16:20 +02008168#define SPEC_CWORD (SPEC_HASH + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008169 "<cWORD>", // cursor WORD
Bram Moolenaar65f08472017-09-10 18:16:20 +02008170#define SPEC_CCWORD (SPEC_CWORD + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008171 "<cexpr>", // expr under cursor
Bram Moolenaar65f08472017-09-10 18:16:20 +02008172#define SPEC_CEXPR (SPEC_CCWORD + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008173 "<cfile>", // cursor path name
Bram Moolenaar65f08472017-09-10 18:16:20 +02008174#define SPEC_CFILE (SPEC_CEXPR + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008175 "<sfile>", // ":so" file name
Bram Moolenaar65f08472017-09-10 18:16:20 +02008176#define SPEC_SFILE (SPEC_CFILE + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008177 "<slnum>", // ":so" file line number
Bram Moolenaar65f08472017-09-10 18:16:20 +02008178#define SPEC_SLNUM (SPEC_SFILE + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008179 "<afile>", // autocommand file name
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008180#define SPEC_AFILE (SPEC_SLNUM + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008181 "<abuf>", // autocommand buffer number
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008182#define SPEC_ABUF (SPEC_AFILE + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008183 "<amatch>", // autocommand match name
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01008184#define SPEC_AMATCH (SPEC_ABUF + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008185 "<sflnum>", // script file line number
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008186#define SPEC_SFLNUM (SPEC_AMATCH + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008187#ifdef FEAT_CLIENTSERVER
8188 "<client>"
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008189# define SPEC_CLIENT (SPEC_SFLNUM + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008190#endif
8191 };
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008192
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00008193 for (i = 0; i < (int)(sizeof(spec_str) / sizeof(char *)); ++i)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008194 {
8195 len = (int)STRLEN(spec_str[i]);
8196 if (STRNCMP(src, spec_str[i], len) == 0)
8197 {
8198 *usedlen = len;
8199 return i;
8200 }
8201 }
8202 return -1;
8203}
8204
8205/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008206 * Evaluate cmdline variables.
8207 *
8208 * change '%' to curbuf->b_ffname
8209 * '#' to curwin->w_altfile
8210 * '<cword>' to word under the cursor
8211 * '<cWORD>' to WORD under the cursor
Bram Moolenaar8071cb22019-07-12 17:58:01 +02008212 * '<cexpr>' to C-expression under the cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00008213 * '<cfile>' to path name under the cursor
8214 * '<sfile>' to sourced file name
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01008215 * '<slnum>' to sourced file line number
Bram Moolenaar071d4272004-06-13 20:20:40 +00008216 * '<afile>' to file name for autocommand
8217 * '<abuf>' to buffer number for autocommand
8218 * '<amatch>' to matching name for autocommand
8219 *
8220 * When an error is detected, "errormsg" is set to a non-NULL pointer (may be
8221 * "" for error without a message) and NULL is returned.
8222 * Returns an allocated string if a valid match was found.
8223 * Returns NULL if no match was found. "usedlen" then still contains the
8224 * number of characters to skip.
8225 */
8226 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008227eval_vars(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008228 char_u *src, // pointer into commandline
8229 char_u *srcstart, // beginning of valid memory for src
8230 int *usedlen, // characters after src that are used
8231 linenr_T *lnump, // line number for :e command, or NULL
8232 char **errormsg, // pointer to error message
8233 int *escaped) // return value has escaped white space (can
8234 // be NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008235{
8236 int i;
8237 char_u *s;
8238 char_u *result;
8239 char_u *resultbuf = NULL;
8240 int resultlen;
8241 buf_T *buf;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008242 int valid = VALID_HEAD + VALID_PATH; // assume valid result
Bram Moolenaar071d4272004-06-13 20:20:40 +00008243 int spec_idx;
Bram Moolenaarfd249462018-07-28 16:14:30 +02008244 int tilde_file = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008245 int skip_mod = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008246 char_u strbuf[30];
Bram Moolenaar071d4272004-06-13 20:20:40 +00008247
8248 *errormsg = NULL;
Bram Moolenaar63b92542007-03-27 14:57:09 +00008249 if (escaped != NULL)
8250 *escaped = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008251
8252 /*
8253 * Check if there is something to do.
8254 */
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008255 spec_idx = find_cmdline_var(src, usedlen);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008256 if (spec_idx < 0) // no match
Bram Moolenaar071d4272004-06-13 20:20:40 +00008257 {
8258 *usedlen = 1;
8259 return NULL;
8260 }
8261
8262 /*
8263 * Skip when preceded with a backslash "\%" and "\#".
8264 * Note: In "\\%" the % is also not recognized!
8265 */
8266 if (src > srcstart && src[-1] == '\\')
8267 {
8268 *usedlen = 0;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008269 STRMOVE(src - 1, src); // remove backslash
Bram Moolenaar071d4272004-06-13 20:20:40 +00008270 return NULL;
8271 }
8272
8273 /*
8274 * word or WORD under cursor
8275 */
Bram Moolenaar65f08472017-09-10 18:16:20 +02008276 if (spec_idx == SPEC_CWORD || spec_idx == SPEC_CCWORD
8277 || spec_idx == SPEC_CEXPR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008278 {
Bram Moolenaar65f08472017-09-10 18:16:20 +02008279 resultlen = find_ident_under_cursor(&result,
8280 spec_idx == SPEC_CWORD ? (FIND_IDENT | FIND_STRING)
8281 : spec_idx == SPEC_CEXPR ? (FIND_IDENT | FIND_STRING | FIND_EVAL)
8282 : FIND_STRING);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008283 if (resultlen == 0)
8284 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008285 *errormsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +00008286 return NULL;
8287 }
8288 }
8289
8290 /*
8291 * '#': Alternate file name
8292 * '%': Current file name
8293 * File name under the cursor
8294 * File name for autocommand
8295 * and following modifiers
8296 */
8297 else
8298 {
8299 switch (spec_idx)
8300 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008301 case SPEC_PERC: // '%': current file
Bram Moolenaar071d4272004-06-13 20:20:40 +00008302 if (curbuf->b_fname == NULL)
8303 {
8304 result = (char_u *)"";
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008305 valid = 0; // Must have ":p:h" to be valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00008306 }
8307 else
Bram Moolenaar00136dc2018-07-25 21:19:13 +02008308 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008309 result = curbuf->b_fname;
Bram Moolenaar00136dc2018-07-25 21:19:13 +02008310 tilde_file = STRCMP(result, "~") == 0;
8311 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008312 break;
8313
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008314 case SPEC_HASH: // '#' or "#99": alternate file
8315 if (src[1] == '#') // "##": the argument list
Bram Moolenaar071d4272004-06-13 20:20:40 +00008316 {
8317 result = arg_all();
8318 resultbuf = result;
8319 *usedlen = 2;
Bram Moolenaar63b92542007-03-27 14:57:09 +00008320 if (escaped != NULL)
8321 *escaped = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008322 skip_mod = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008323 break;
8324 }
8325 s = src + 1;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008326 if (*s == '<') // "#<99" uses v:oldfiles
Bram Moolenaard812df62008-11-09 12:46:09 +00008327 ++s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008328 i = (int)getdigits(&s);
Bram Moolenaarc312b8b2017-10-28 17:53:04 +02008329 if (s == src + 2 && src[1] == '-')
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008330 // just a minus sign, don't skip over it
Bram Moolenaarc312b8b2017-10-28 17:53:04 +02008331 s--;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008332 *usedlen = (int)(s - src); // length of what we expand
Bram Moolenaar071d4272004-06-13 20:20:40 +00008333
Bram Moolenaarc312b8b2017-10-28 17:53:04 +02008334 if (src[1] == '<' && i != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008335 {
Bram Moolenaard812df62008-11-09 12:46:09 +00008336 if (*usedlen < 2)
8337 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008338 // Should we give an error message for #<text?
Bram Moolenaard812df62008-11-09 12:46:09 +00008339 *usedlen = 1;
8340 return NULL;
8341 }
8342#ifdef FEAT_EVAL
8343 result = list_find_str(get_vim_var_list(VV_OLDFILES),
8344 (long)i);
8345 if (result == NULL)
8346 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008347 *errormsg = "";
Bram Moolenaard812df62008-11-09 12:46:09 +00008348 return NULL;
8349 }
8350#else
Bram Moolenaar8e481e82019-01-15 20:07:48 +01008351 *errormsg = _("E809: #< is not available without the +eval feature");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008352 return NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +00008353#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008354 }
8355 else
Bram Moolenaard812df62008-11-09 12:46:09 +00008356 {
Bram Moolenaarc312b8b2017-10-28 17:53:04 +02008357 if (i == 0 && src[1] == '<' && *usedlen > 1)
8358 *usedlen = 1;
Bram Moolenaard812df62008-11-09 12:46:09 +00008359 buf = buflist_findnr(i);
8360 if (buf == NULL)
8361 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008362 *errormsg = _("E194: No alternate file name to substitute for '#'");
Bram Moolenaard812df62008-11-09 12:46:09 +00008363 return NULL;
8364 }
8365 if (lnump != NULL)
8366 *lnump = ECMD_LAST;
8367 if (buf->b_fname == NULL)
8368 {
8369 result = (char_u *)"";
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008370 valid = 0; // Must have ":p:h" to be valid
Bram Moolenaard812df62008-11-09 12:46:09 +00008371 }
8372 else
Bram Moolenaar00136dc2018-07-25 21:19:13 +02008373 {
Bram Moolenaard812df62008-11-09 12:46:09 +00008374 result = buf->b_fname;
Bram Moolenaar00136dc2018-07-25 21:19:13 +02008375 tilde_file = STRCMP(result, "~") == 0;
8376 }
Bram Moolenaard812df62008-11-09 12:46:09 +00008377 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008378 break;
8379
8380#ifdef FEAT_SEARCHPATH
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008381 case SPEC_CFILE: // file name under cursor
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008382 result = file_name_at_cursor(FNAME_MESS|FNAME_HYP, 1L, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008383 if (result == NULL)
8384 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008385 *errormsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +00008386 return NULL;
8387 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008388 resultbuf = result; // remember allocated string
Bram Moolenaar071d4272004-06-13 20:20:40 +00008389 break;
8390#endif
8391
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008392 case SPEC_AFILE: // file name for autocommand
Bram Moolenaar071d4272004-06-13 20:20:40 +00008393 result = autocmd_fname;
Bram Moolenaarf6dad432008-09-18 19:29:58 +00008394 if (result != NULL && !autocmd_fname_full)
8395 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008396 // Still need to turn the fname into a full path. It is
8397 // postponed to avoid a delay when <afile> is not used.
Bram Moolenaarf6dad432008-09-18 19:29:58 +00008398 autocmd_fname_full = TRUE;
8399 result = FullName_save(autocmd_fname, FALSE);
8400 vim_free(autocmd_fname);
8401 autocmd_fname = result;
8402 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008403 if (result == NULL)
8404 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008405 *errormsg = _("E495: no autocommand file name to substitute for \"<afile>\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008406 return NULL;
8407 }
Bram Moolenaara0174af2008-01-02 20:08:25 +00008408 result = shorten_fname1(result);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008409 break;
8410
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008411 case SPEC_ABUF: // buffer number for autocommand
Bram Moolenaar071d4272004-06-13 20:20:40 +00008412 if (autocmd_bufnr <= 0)
8413 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008414 *errormsg = _("E496: no autocommand buffer number to substitute for \"<abuf>\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008415 return NULL;
8416 }
8417 sprintf((char *)strbuf, "%d", autocmd_bufnr);
8418 result = strbuf;
8419 break;
8420
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008421 case SPEC_AMATCH: // match name for autocommand
Bram Moolenaar071d4272004-06-13 20:20:40 +00008422 result = autocmd_match;
8423 if (result == NULL)
8424 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008425 *errormsg = _("E497: no autocommand match name to substitute for \"<amatch>\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008426 return NULL;
8427 }
8428 break;
8429
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008430 case SPEC_SFILE: // file name for ":so" command
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01008431 result = estack_sfile();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008432 if (result == NULL)
8433 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008434 *errormsg = _("E498: no :source file name to substitute for \"<sfile>\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008435 return NULL;
8436 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01008437 resultbuf = result; // remember allocated string
Bram Moolenaar071d4272004-06-13 20:20:40 +00008438 break;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008439
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008440 case SPEC_SLNUM: // line in file for ":so" command
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01008441 if (SOURCING_NAME == NULL || SOURCING_LNUM == 0)
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01008442 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008443 *errormsg = _("E842: no line number to use for \"<slnum>\"");
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01008444 return NULL;
8445 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01008446 sprintf((char *)strbuf, "%ld", SOURCING_LNUM);
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01008447 result = strbuf;
8448 break;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008449
8450#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008451 case SPEC_SFLNUM: // line in script file
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01008452 if (current_sctx.sc_lnum + SOURCING_LNUM == 0)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008453 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008454 *errormsg = _("E961: no line number to use for \"<sflnum>\"");
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008455 return NULL;
8456 }
8457 sprintf((char *)strbuf, "%ld",
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01008458 (long)(current_sctx.sc_lnum + SOURCING_LNUM));
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008459 result = strbuf;
8460 break;
8461#endif
8462
8463#ifdef FEAT_CLIENTSERVER
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008464 case SPEC_CLIENT: // Source of last submitted input
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008465 sprintf((char *)strbuf, PRINTF_HEX_LONG_U,
8466 (long_u)clientWindow);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008467 result = strbuf;
8468 break;
8469#endif
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008470
Bram Moolenaar9e0f6ec2017-05-16 09:36:54 +02008471 default:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008472 result = (char_u *)""; // avoid gcc warning
Bram Moolenaar9e0f6ec2017-05-16 09:36:54 +02008473 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008474 }
8475
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008476 resultlen = (int)STRLEN(result); // length of new string
8477 if (src[*usedlen] == '<') // remove the file name extension
Bram Moolenaar071d4272004-06-13 20:20:40 +00008478 {
8479 ++*usedlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008480 if ((s = vim_strrchr(result, '.')) != NULL && s >= gettail(result))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008481 resultlen = (int)(s - result);
8482 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008483 else if (!skip_mod)
8484 {
Bram Moolenaar00136dc2018-07-25 21:19:13 +02008485 valid |= modify_fname(src, tilde_file, usedlen, &result, &resultbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008486 &resultlen);
8487 if (result == NULL)
8488 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008489 *errormsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +00008490 return NULL;
8491 }
8492 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008493 }
8494
8495 if (resultlen == 0 || valid != VALID_HEAD + VALID_PATH)
8496 {
8497 if (valid != VALID_HEAD + VALID_PATH)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008498 // xgettext:no-c-format
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008499 *errormsg = _("E499: Empty file name for '%' or '#', only works with \":p:h\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008500 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008501 *errormsg = _("E500: Evaluates to an empty string");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008502 result = NULL;
8503 }
8504 else
8505 result = vim_strnsave(result, resultlen);
8506 vim_free(resultbuf);
8507 return result;
8508}
8509
8510/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008511 * Expand the <sfile> string in "arg".
8512 *
8513 * Returns an allocated string, or NULL for any error.
8514 */
8515 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008516expand_sfile(char_u *arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008517{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008518 char *errormsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008519 int len;
8520 char_u *result;
8521 char_u *newres;
8522 char_u *repl;
8523 int srclen;
8524 char_u *p;
8525
8526 result = vim_strsave(arg);
8527 if (result == NULL)
8528 return NULL;
8529
8530 for (p = result; *p; )
8531 {
8532 if (STRNCMP(p, "<sfile>", 7) != 0)
8533 ++p;
8534 else
8535 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008536 // replace "<sfile>" with the sourced file name, and do ":" stuff
Bram Moolenaar63b92542007-03-27 14:57:09 +00008537 repl = eval_vars(p, result, &srclen, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008538 if (errormsg != NULL)
8539 {
8540 if (*errormsg)
8541 emsg(errormsg);
8542 vim_free(result);
8543 return NULL;
8544 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008545 if (repl == NULL) // no match (cannot happen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008546 {
8547 p += srclen;
8548 continue;
8549 }
8550 len = (int)STRLEN(result) - srclen + (int)STRLEN(repl) + 1;
8551 newres = alloc(len);
8552 if (newres == NULL)
8553 {
8554 vim_free(repl);
8555 vim_free(result);
8556 return NULL;
8557 }
8558 mch_memmove(newres, result, (size_t)(p - result));
8559 STRCPY(newres + (p - result), repl);
8560 len = (int)STRLEN(newres);
8561 STRCAT(newres, p + srclen);
8562 vim_free(repl);
8563 vim_free(result);
8564 result = newres;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008565 p = newres + len; // continue after the match
Bram Moolenaar071d4272004-06-13 20:20:40 +00008566 }
8567 }
8568
8569 return result;
8570}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008571
Bram Moolenaar071d4272004-06-13 20:20:40 +00008572#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG) || defined(PROTO)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00008573/*
Bram Moolenaard9462e32011-04-11 21:35:11 +02008574 * Make a dialog message in "buff[DIALOG_MSG_SIZE]".
Bram Moolenaarb765d632005-06-07 21:00:02 +00008575 * "format" must contain "%s".
Bram Moolenaar9c13b352005-05-19 20:53:52 +00008576 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008577 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008578dialog_msg(char_u *buff, char *format, char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008579{
Bram Moolenaar071d4272004-06-13 20:20:40 +00008580 if (fname == NULL)
8581 fname = (char_u *)_("Untitled");
Bram Moolenaard9462e32011-04-11 21:35:11 +02008582 vim_snprintf((char *)buff, DIALOG_MSG_SIZE, format, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008583}
8584#endif
8585
8586/*
8587 * ":behave {mswin,xterm}"
8588 */
8589 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008590ex_behave(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008591{
8592 if (STRCMP(eap->arg, "mswin") == 0)
8593 {
8594 set_option_value((char_u *)"selection", 0L, (char_u *)"exclusive", 0);
8595 set_option_value((char_u *)"selectmode", 0L, (char_u *)"mouse,key", 0);
8596 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"popup", 0);
8597 set_option_value((char_u *)"keymodel", 0L,
8598 (char_u *)"startsel,stopsel", 0);
8599 }
8600 else if (STRCMP(eap->arg, "xterm") == 0)
8601 {
8602 set_option_value((char_u *)"selection", 0L, (char_u *)"inclusive", 0);
8603 set_option_value((char_u *)"selectmode", 0L, (char_u *)"", 0);
8604 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"extend", 0);
8605 set_option_value((char_u *)"keymodel", 0L, (char_u *)"", 0);
8606 }
8607 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008608 semsg(_(e_invarg2), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008609}
8610
Bram Moolenaar071d4272004-06-13 20:20:40 +00008611static int filetype_detect = FALSE;
8612static int filetype_plugin = FALSE;
8613static int filetype_indent = FALSE;
8614
8615/*
8616 * ":filetype [plugin] [indent] {on,off,detect}"
8617 * on: Load the filetype.vim file to install autocommands for file types.
8618 * off: Load the ftoff.vim file to remove all autocommands for file types.
8619 * plugin on: load filetype.vim and ftplugin.vim
8620 * plugin off: load ftplugof.vim
8621 * indent on: load filetype.vim and indent.vim
8622 * indent off: load indoff.vim
8623 */
8624 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008625ex_filetype(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008626{
8627 char_u *arg = eap->arg;
8628 int plugin = FALSE;
8629 int indent = FALSE;
8630
8631 if (*eap->arg == NUL)
8632 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008633 // Print current status.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008634 smsg("filetype detection:%s plugin:%s indent:%s",
Bram Moolenaar071d4272004-06-13 20:20:40 +00008635 filetype_detect ? "ON" : "OFF",
8636 filetype_plugin ? (filetype_detect ? "ON" : "(on)") : "OFF",
8637 filetype_indent ? (filetype_detect ? "ON" : "(on)") : "OFF");
8638 return;
8639 }
8640
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008641 // Accept "plugin" and "indent" in any order.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008642 for (;;)
8643 {
8644 if (STRNCMP(arg, "plugin", 6) == 0)
8645 {
8646 plugin = TRUE;
8647 arg = skipwhite(arg + 6);
8648 continue;
8649 }
8650 if (STRNCMP(arg, "indent", 6) == 0)
8651 {
8652 indent = TRUE;
8653 arg = skipwhite(arg + 6);
8654 continue;
8655 }
8656 break;
8657 }
8658 if (STRCMP(arg, "on") == 0 || STRCMP(arg, "detect") == 0)
8659 {
8660 if (*arg == 'o' || !filetype_detect)
8661 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01008662 source_runtime((char_u *)FILETYPE_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008663 filetype_detect = TRUE;
8664 if (plugin)
8665 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01008666 source_runtime((char_u *)FTPLUGIN_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008667 filetype_plugin = TRUE;
8668 }
8669 if (indent)
8670 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01008671 source_runtime((char_u *)INDENT_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008672 filetype_indent = TRUE;
8673 }
8674 }
8675 if (*arg == 'd')
8676 {
Bram Moolenaar1610d052016-06-09 22:53:01 +02008677 (void)do_doautocmd((char_u *)"filetypedetect BufRead", TRUE, NULL);
Bram Moolenaara3227e22006-03-08 21:32:40 +00008678 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008679 }
8680 }
8681 else if (STRCMP(arg, "off") == 0)
8682 {
8683 if (plugin || indent)
8684 {
8685 if (plugin)
8686 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01008687 source_runtime((char_u *)FTPLUGOF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008688 filetype_plugin = FALSE;
8689 }
8690 if (indent)
8691 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01008692 source_runtime((char_u *)INDOFF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008693 filetype_indent = FALSE;
8694 }
8695 }
8696 else
8697 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01008698 source_runtime((char_u *)FTOFF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008699 filetype_detect = FALSE;
8700 }
8701 }
8702 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008703 semsg(_(e_invarg2), arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008704}
8705
8706/*
Bram Moolenaar3e545692017-06-04 19:00:32 +02008707 * ":setfiletype [FALLBACK] {name}"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008708 */
8709 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008710ex_setfiletype(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008711{
8712 if (!did_filetype)
Bram Moolenaar3e545692017-06-04 19:00:32 +02008713 {
8714 char_u *arg = eap->arg;
8715
8716 if (STRNCMP(arg, "FALLBACK ", 9) == 0)
8717 arg += 9;
8718
8719 set_option_value((char_u *)"filetype", 0L, arg, OPT_LOCAL);
8720 if (arg != eap->arg)
8721 did_filetype = FALSE;
8722 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008723}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008724
Bram Moolenaar071d4272004-06-13 20:20:40 +00008725 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008726ex_digraphs(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008727{
8728#ifdef FEAT_DIGRAPHS
8729 if (*eap->arg != NUL)
8730 putdigraph(eap->arg);
8731 else
Bram Moolenaareae8ae12018-12-14 18:53:02 +01008732 listdigraphs(eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008733#else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008734 emsg(_("E196: No digraphs in this version"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008735#endif
8736}
8737
Bram Moolenaar451fc7b2018-04-27 22:53:07 +02008738#if defined(FEAT_SEARCH_EXTRA) || defined(PROTO)
8739 void
8740set_no_hlsearch(int flag)
8741{
8742 no_hlsearch = flag;
8743# ifdef FEAT_EVAL
8744 set_vim_var_nr(VV_HLSEARCH, !no_hlsearch && p_hls);
8745# endif
8746}
8747
Bram Moolenaar071d4272004-06-13 20:20:40 +00008748/*
8749 * ":nohlsearch"
8750 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008751 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008752ex_nohlsearch(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008753{
Bram Moolenaar451fc7b2018-04-27 22:53:07 +02008754 set_no_hlsearch(TRUE);
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00008755 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008756}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008757#endif
8758
8759#ifdef FEAT_CRYPT
8760/*
8761 * ":X": Get crypt key
8762 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008763 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008764ex_X(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008765{
Bram Moolenaar3a0c9082014-11-12 15:15:42 +01008766 crypt_check_current_method();
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02008767 (void)crypt_get_key(TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008768}
8769#endif
8770
8771#ifdef FEAT_FOLDING
8772 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008773ex_fold(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008774{
8775 if (foldManualAllowed(TRUE))
8776 foldCreate(eap->line1, eap->line2);
8777}
8778
8779 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008780ex_foldopen(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008781{
8782 opFoldRange(eap->line1, eap->line2, eap->cmdidx == CMD_foldopen,
8783 eap->forceit, FALSE);
8784}
8785
8786 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008787ex_folddo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008788{
8789 linenr_T lnum;
8790
Bram Moolenaar4facea32019-10-12 20:17:40 +02008791# ifdef FEAT_CLIPBOARD
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02008792 start_global_changes();
Bram Moolenaar4facea32019-10-12 20:17:40 +02008793# endif
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02008794
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008795 // First set the marks for all lines closed/open.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008796 for (lnum = eap->line1; lnum <= eap->line2; ++lnum)
8797 if (hasFolding(lnum, NULL, NULL) == (eap->cmdidx == CMD_folddoclosed))
8798 ml_setmarked(lnum);
8799
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008800 // Execute the command on the marked lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008801 global_exe(eap->arg);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008802 ml_clearmarked(); // clear rest of the marks
Bram Moolenaar4facea32019-10-12 20:17:40 +02008803# ifdef FEAT_CLIPBOARD
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02008804 end_global_changes();
Bram Moolenaar4facea32019-10-12 20:17:40 +02008805# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008806}
8807#endif
Bram Moolenaarf5291f32017-09-14 22:55:37 +02008808
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008809#if defined(FEAT_QUICKFIX) || defined(PROTO)
Bram Moolenaar39665952018-08-15 20:59:48 +02008810/*
8811 * Returns TRUE if the supplied Ex cmdidx is for a location list command
8812 * instead of a quickfix command.
8813 */
8814 int
8815is_loclist_cmd(int cmdidx)
8816{
Bram Moolenaar74c8be22018-08-23 22:51:40 +02008817 if (cmdidx < 0 || cmdidx >= CMD_SIZE)
Bram Moolenaar39665952018-08-15 20:59:48 +02008818 return FALSE;
8819 return cmdnames[cmdidx].cmd_name[0] == 'l';
8820}
8821#endif
8822
Bram Moolenaar4facea32019-10-12 20:17:40 +02008823#if defined(FEAT_TIMERS) || defined(PROTO)
Bram Moolenaarf5291f32017-09-14 22:55:37 +02008824 int
8825get_pressedreturn(void)
8826{
8827 return ex_pressedreturn;
8828}
8829
8830 void
8831set_pressedreturn(int val)
8832{
8833 ex_pressedreturn = val;
8834}
8835#endif