blob: 0c55a57c5c292ab6f94ec57245f0b0a5518f0544 [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 Moolenaar5f195932020-07-01 20:07:14 +02003222 if (lookup != NULL && (*p == '(' || *p == '[' || *p == '{'
Bram Moolenaar0c6ceaf2020-02-22 18:36:32 +01003223 || ((p = to_name_const_end(eap->cmd)) > eap->cmd && *p != NUL)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003224 {
3225 int oplen;
3226 int heredoc;
3227
3228 // "funcname(" is always a function call.
3229 // "varname[]" is an expression.
3230 // "g:varname" is an expression.
3231 // "varname->expr" is an expression.
Bram Moolenaar0c6ceaf2020-02-22 18:36:32 +01003232 // "(..." is an expression.
Bram Moolenaar5f195932020-07-01 20:07:14 +02003233 // "{..." is an dict expression.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003234 if (*p == '('
Bram Moolenaar5f195932020-07-01 20:07:14 +02003235 || *p == '{'
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003236 || p[1] == ':'
3237 || (*p == '-' && p[1] == '>'))
3238 {
3239 eap->cmdidx = CMD_eval;
3240 return eap->cmd;
3241 }
3242
Bram Moolenaar5f195932020-07-01 20:07:14 +02003243 // Recognize an assignment if we recognize the variable name:
3244 // "g:var = expr"
3245 // "var = expr" where "var" is a local var name.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003246 oplen = assignment_len(skipwhite(p), &heredoc);
3247 if (oplen > 0)
3248 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003249 if (((p - eap->cmd) > 2 && eap->cmd[1] == ':')
Bram Moolenaarb84a3812020-05-01 15:44:29 +02003250 || lookup(eap->cmd, p - eap->cmd, cctx) != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003251 {
3252 eap->cmdidx = CMD_let;
3253 return eap->cmd;
3254 }
3255 }
Bram Moolenaar5f195932020-07-01 20:07:14 +02003256
3257 // "[...]->Method()" is a list expression. But "[a, b] = Func()" is
3258 // an assignment.
3259 if (*p == '[' && (eval_list(&p, NULL, NULL, FALSE) == FAIL
3260 || *skipwhite(p) != '='))
3261 {
3262 eap->cmdidx = CMD_eval;
3263 return eap->cmd;
3264 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003265 }
3266#endif
3267
Bram Moolenaar071d4272004-06-13 20:20:40 +00003268 /*
3269 * Isolate the command and search for it in the command table.
Bram Moolenaar81870892007-11-11 18:17:28 +00003270 * Exceptions:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003271 * - the 'k' command can directly be followed by any character.
3272 * - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003273 * but :sre[wind] is another command, as are :scr[iptnames],
Bram Moolenaar071d4272004-06-13 20:20:40 +00003274 * :scs[cope], :sim[alt], :sig[ns] and :sil[ent].
Bram Moolenaardf177f62005-02-22 08:39:57 +00003275 * - the "d" command can directly be followed by 'l' or 'p' flag.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003276 */
3277 p = eap->cmd;
3278 if (*p == 'k')
3279 {
3280 eap->cmdidx = CMD_k;
3281 ++p;
3282 }
3283 else if (p[0] == 's'
Bram Moolenaar204b93f2015-08-04 22:02:51 +02003284 && ((p[1] == 'c' && (p[2] == NUL || (p[2] != 's' && p[2] != 'r'
3285 && (p[3] == NUL || (p[3] != 'i' && p[4] != 'p')))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003286 || p[1] == 'g'
3287 || (p[1] == 'i' && p[2] != 'm' && p[2] != 'l' && p[2] != 'g')
3288 || p[1] == 'I'
3289 || (p[1] == 'r' && p[2] != 'e')))
3290 {
3291 eap->cmdidx = CMD_substitute;
3292 ++p;
3293 }
3294 else
3295 {
3296 while (ASCII_ISALPHA(*p))
3297 ++p;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003298 // for python 3.x support ":py3", ":python3", ":py3file", etc.
Bram Moolenaar55d5c032010-07-17 23:52:29 +02003299 if (eap->cmd[0] == 'p' && eap->cmd[1] == 'y')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003300 {
Bram Moolenaarb6590522010-07-21 16:00:43 +02003301 while (ASCII_ISALNUM(*p))
3302 ++p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003303 }
3304 else if (*p == '9' && STRNCMP("vim9", eap->cmd, 4) == 0)
3305 {
3306 // include "9" for "vim9script"
3307 ++p;
3308 while (ASCII_ISALPHA(*p))
3309 ++p;
3310 }
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02003311
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003312 // check for non-alpha command
Bram Moolenaar071d4272004-06-13 20:20:40 +00003313 if (p == eap->cmd && vim_strchr((char_u *)"@*!=><&~#", *p) != NULL)
3314 ++p;
3315 len = (int)(p - eap->cmd);
Bram Moolenaardf177f62005-02-22 08:39:57 +00003316 if (*eap->cmd == 'd' && (p[-1] == 'l' || p[-1] == 'p'))
3317 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003318 // Check for ":dl", ":dell", etc. to ":deletel": that's
3319 // :delete with the 'l' flag. Same for 'p'.
Bram Moolenaardf177f62005-02-22 08:39:57 +00003320 for (i = 0; i < len; ++i)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003321 if (eap->cmd[i] != ((char_u *)"delete")[i])
Bram Moolenaardf177f62005-02-22 08:39:57 +00003322 break;
3323 if (i == len - 1)
3324 {
3325 --len;
3326 if (p[-1] == 'l')
3327 eap->flags |= EXFLAG_LIST;
3328 else
3329 eap->flags |= EXFLAG_PRINT;
3330 }
3331 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003332
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003333 if (ASCII_ISLOWER(eap->cmd[0]))
3334 {
Bram Moolenaar6c0c1e82017-03-25 15:07:43 +01003335 int c1 = eap->cmd[0];
Bram Moolenaar94f82cb2019-07-24 22:30:27 +02003336 int c2 = len == 1 ? NUL : eap->cmd[1];
Bram Moolenaar6c0c1e82017-03-25 15:07:43 +01003337
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003338 if (command_count != (int)CMD_SIZE)
3339 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003340 iemsg(_("E943: Command table needs to be updated, run 'make cmdidxs'"));
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003341 getout(1);
3342 }
3343
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003344 // Use a precomputed index for fast look-up in cmdnames[]
3345 // taking into account the first 2 letters of eap->cmd.
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003346 eap->cmdidx = cmdidxs1[CharOrdLow(c1)];
3347 if (ASCII_ISLOWER(c2))
3348 eap->cmdidx += cmdidxs2[CharOrdLow(c1)][CharOrdLow(c2)];
3349 }
Bram Moolenaara494f562020-04-18 17:45:38 +02003350 else if (ASCII_ISUPPER(eap->cmd[0]))
3351 eap->cmdidx = CMD_Next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003352 else
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003353 eap->cmdidx = CMD_bang;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003354
3355 for ( ; (int)eap->cmdidx < (int)CMD_SIZE;
3356 eap->cmdidx = (cmdidx_T)((int)eap->cmdidx + 1))
3357 if (STRNCMP(cmdnames[(int)eap->cmdidx].cmd_name, (char *)eap->cmd,
3358 (size_t)len) == 0)
3359 {
3360#ifdef FEAT_EVAL
3361 if (full != NULL
3362 && cmdnames[(int)eap->cmdidx].cmd_name[len] == NUL)
3363 *full = TRUE;
3364#endif
3365 break;
3366 }
3367
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003368 // Look for a user defined command as a last resort. Let ":Print" be
3369 // overruled by a user defined command.
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01003370 if ((eap->cmdidx == CMD_SIZE || eap->cmdidx == CMD_Print)
3371 && *eap->cmd >= 'A' && *eap->cmd <= 'Z')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003372 {
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003373 // User defined commands may contain digits.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003374 while (ASCII_ISALNUM(*p))
3375 ++p;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003376 p = find_ucmd(eap, p, full, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003377 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003378 if (p == eap->cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003379 eap->cmdidx = CMD_SIZE;
3380 }
3381
3382 return p;
3383}
3384
3385#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003386static struct cmdmod
3387{
3388 char *name;
3389 int minlen;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003390 int has_count; // :123verbose :3tab
Bram Moolenaared53fb92007-11-24 20:50:24 +00003391} cmdmods[] = {
3392 {"aboveleft", 3, FALSE},
3393 {"belowright", 3, FALSE},
3394 {"botright", 2, FALSE},
3395 {"browse", 3, FALSE},
3396 {"confirm", 4, FALSE},
Bram Moolenaar7b668e82016-08-23 23:51:21 +02003397 {"filter", 4, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003398 {"hide", 3, FALSE},
3399 {"keepalt", 5, FALSE},
3400 {"keepjumps", 5, FALSE},
3401 {"keepmarks", 3, FALSE},
Bram Moolenaara939e432013-11-09 05:30:26 +01003402 {"keeppatterns", 5, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003403 {"leftabove", 5, FALSE},
3404 {"lockmarks", 3, FALSE},
Bram Moolenaarca9f9582008-09-18 10:44:28 +00003405 {"noautocmd", 3, FALSE},
Bram Moolenaar5803ae62014-03-23 16:04:02 +01003406 {"noswapfile", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003407 {"rightbelow", 6, FALSE},
3408 {"sandbox", 3, FALSE},
3409 {"silent", 3, FALSE},
3410 {"tab", 3, TRUE},
3411 {"topleft", 2, FALSE},
Bram Moolenaar8e258a42009-07-09 13:55:43 +00003412 {"unsilent", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003413 {"verbose", 4, TRUE},
3414 {"vertical", 4, FALSE},
3415};
3416
3417/*
3418 * Return length of a command modifier (including optional count).
3419 * Return zero when it's not a modifier.
3420 */
3421 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003422modifier_len(char_u *cmd)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003423{
3424 int i, j;
3425 char_u *p = cmd;
3426
3427 if (VIM_ISDIGIT(*cmd))
3428 p = skipwhite(skipdigits(cmd));
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00003429 for (i = 0; i < (int)(sizeof(cmdmods) / sizeof(struct cmdmod)); ++i)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003430 {
3431 for (j = 0; p[j] != NUL; ++j)
3432 if (p[j] != cmdmods[i].name[j])
3433 break;
Bram Moolenaar2d473ab2013-06-12 17:12:24 +02003434 if (!ASCII_ISALPHA(p[j]) && j >= cmdmods[i].minlen
Bram Moolenaared53fb92007-11-24 20:50:24 +00003435 && (p == cmd || cmdmods[i].has_count))
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00003436 return j + (int)(p - cmd);
Bram Moolenaared53fb92007-11-24 20:50:24 +00003437 }
3438 return 0;
3439}
3440
Bram Moolenaar071d4272004-06-13 20:20:40 +00003441/*
3442 * Return > 0 if an Ex command "name" exists.
3443 * Return 2 if there is an exact match.
3444 * Return 3 if there is an ambiguous match.
3445 */
3446 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003447cmd_exists(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003448{
3449 exarg_T ea;
3450 int full = FALSE;
3451 int i;
3452 int j;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003453 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003454
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003455 // Check command modifiers.
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00003456 for (i = 0; i < (int)(sizeof(cmdmods) / sizeof(struct cmdmod)); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003457 {
3458 for (j = 0; name[j] != NUL; ++j)
3459 if (name[j] != cmdmods[i].name[j])
3460 break;
3461 if (name[j] == NUL && j >= cmdmods[i].minlen)
3462 return (cmdmods[i].name[j] == NUL ? 2 : 1);
3463 }
3464
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003465 // Check built-in commands and user defined commands.
3466 // For ":2match" and ":3match" we need to skip the number.
Bram Moolenaara9587612006-05-04 21:47:50 +00003467 ea.cmd = (*name == '2' || *name == '3') ? name + 1 : name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003468 ea.cmdidx = (cmdidx_T)0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003469 p = find_ex_command(&ea, &full, NULL, NULL);
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003470 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003471 return 3;
Bram Moolenaara9587612006-05-04 21:47:50 +00003472 if (vim_isdigit(*name) && ea.cmdidx != CMD_match)
3473 return 0;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003474 if (*skipwhite(p) != NUL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003475 return 0; // trailing garbage
Bram Moolenaar071d4272004-06-13 20:20:40 +00003476 return (ea.cmdidx == CMD_SIZE ? 0 : (full ? 2 : 1));
3477}
3478#endif
3479
Bram Moolenaard0190392019-08-23 21:17:35 +02003480 cmdidx_T
3481excmd_get_cmdidx(char_u *cmd, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003482{
Bram Moolenaard0190392019-08-23 21:17:35 +02003483 cmdidx_T idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003484
Bram Moolenaard0190392019-08-23 21:17:35 +02003485 for (idx = (cmdidx_T)0; (int)idx < (int)CMD_SIZE;
3486 idx = (cmdidx_T)((int)idx + 1))
3487 if (STRNCMP(cmdnames[(int)idx].cmd_name, cmd, (size_t)len) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003488 break;
3489
Bram Moolenaard0190392019-08-23 21:17:35 +02003490 return idx;
3491}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003492
Bram Moolenaard0190392019-08-23 21:17:35 +02003493 long
3494excmd_get_argt(cmdidx_T idx)
3495{
3496 return (long)cmdnames[(int)idx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003497}
3498
3499/*
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003500 * Skip a range specifier of the form: addr [,addr] [;addr] ..
Bram Moolenaar071d4272004-06-13 20:20:40 +00003501 *
3502 * Backslashed delimiters after / or ? will be skipped, and commands will
3503 * not be expanded between /'s and ?'s or after "'".
3504 *
Bram Moolenaardf069ee2020-06-22 23:02:51 +02003505 * Also skip white space and ":" characters after the range.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003506 * Returns the "cmd" pointer advanced to beyond the range.
3507 */
3508 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003509skip_range(
3510 char_u *cmd,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003511 int *ctx) // pointer to xp_context or NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003512{
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003513 unsigned delim;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003514
Bram Moolenaarcbf20fb2017-02-03 21:19:04 +01003515 while (vim_strchr((char_u *)" \t0123456789.$%'/?-+,;\\", *cmd) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003516 {
Bram Moolenaarcbf20fb2017-02-03 21:19:04 +01003517 if (*cmd == '\\')
3518 {
3519 if (cmd[1] == '?' || cmd[1] == '/' || cmd[1] == '&')
3520 ++cmd;
3521 else
3522 break;
3523 }
3524 else if (*cmd == '\'')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003525 {
3526 if (*++cmd == NUL && ctx != NULL)
3527 *ctx = EXPAND_NOTHING;
3528 }
3529 else if (*cmd == '/' || *cmd == '?')
3530 {
3531 delim = *cmd++;
3532 while (*cmd != NUL && *cmd != delim)
3533 if (*cmd++ == '\\' && *cmd != NUL)
3534 ++cmd;
3535 if (*cmd == NUL && ctx != NULL)
3536 *ctx = EXPAND_NOTHING;
3537 }
3538 if (*cmd != NUL)
3539 ++cmd;
3540 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00003541
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003542 // Skip ":" and white space.
Bram Moolenaardf177f62005-02-22 08:39:57 +00003543 while (*cmd == ':')
3544 cmd = skipwhite(cmd + 1);
3545
Bram Moolenaar071d4272004-06-13 20:20:40 +00003546 return cmd;
3547}
3548
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003549 static void
3550addr_error(cmd_addr_T addr_type)
3551{
3552 if (addr_type == ADDR_NONE)
3553 emsg(_(e_norange));
3554 else
3555 emsg(_(e_invrange));
3556}
3557
Bram Moolenaar071d4272004-06-13 20:20:40 +00003558/*
Bram Moolenaar198cb662018-09-06 21:44:17 +02003559 * Get a single EX address.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003560 *
3561 * Set ptr to the next character after the part that was interpreted.
3562 * Set ptr to NULL when an error is encountered.
Bram Moolenaar198cb662018-09-06 21:44:17 +02003563 * This may set the last used search pattern.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003564 *
3565 * Return MAXLNUM when no Ex address was found.
3566 */
3567 static linenr_T
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003568get_address(
3569 exarg_T *eap UNUSED,
3570 char_u **ptr,
Bram Moolenaar1b03a192019-12-08 17:08:29 +01003571 cmd_addr_T addr_type,
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02003572 int skip, // only skip the address, don't use it
3573 int silent, // no errors or side effects
3574 int to_other_file, // flag: may jump to other file
3575 int address_count UNUSED) // 1 for first address, >1 after comma
Bram Moolenaar071d4272004-06-13 20:20:40 +00003576{
3577 int c;
3578 int i;
3579 long n;
3580 char_u *cmd;
3581 pos_T pos;
3582 pos_T *fp;
3583 linenr_T lnum;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003584 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003585
3586 cmd = skipwhite(*ptr);
3587 lnum = MAXLNUM;
3588 do
3589 {
3590 switch (*cmd)
3591 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003592 case '.': // '.' - Cursor position
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003593 ++cmd;
3594 switch (addr_type)
3595 {
3596 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02003597 case ADDR_OTHER:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003598 lnum = curwin->w_cursor.lnum;
3599 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003600 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01003601 lnum = CURRENT_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003602 break;
3603 case ADDR_ARGUMENTS:
3604 lnum = curwin->w_arg_idx + 1;
3605 break;
3606 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003607 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003608 lnum = curbuf->b_fnum;
3609 break;
3610 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01003611 lnum = CURRENT_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003612 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02003613 case ADDR_NONE:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003614 case ADDR_TABS_RELATIVE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02003615 case ADDR_UNSIGNED:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003616 addr_error(addr_type);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01003617 cmd = NULL;
3618 goto error;
3619 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003620 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003621#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02003622 lnum = qf_get_cur_idx(eap);
3623#endif
3624 break;
3625 case ADDR_QUICKFIX_VALID:
3626#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003627 lnum = qf_get_cur_valid_idx(eap);
Bram Moolenaare906c502015-09-09 21:10:39 +02003628#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003629 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003630 }
3631 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003632
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003633 case '$': // '$' - last line
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003634 ++cmd;
3635 switch (addr_type)
3636 {
3637 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02003638 case ADDR_OTHER:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003639 lnum = curbuf->b_ml.ml_line_count;
3640 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003641 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01003642 lnum = LAST_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003643 break;
3644 case ADDR_ARGUMENTS:
3645 lnum = ARGCOUNT;
3646 break;
3647 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003648 buf = lastbuf;
3649 while (buf->b_ml.ml_mfp == NULL)
3650 {
3651 if (buf->b_prev == NULL)
3652 break;
3653 buf = buf->b_prev;
3654 }
3655 lnum = buf->b_fnum;
3656 break;
3657 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003658 lnum = lastbuf->b_fnum;
3659 break;
3660 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01003661 lnum = LAST_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003662 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02003663 case ADDR_NONE:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003664 case ADDR_TABS_RELATIVE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02003665 case ADDR_UNSIGNED:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003666 addr_error(addr_type);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01003667 cmd = NULL;
3668 goto error;
3669 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003670 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003671#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003672 lnum = qf_get_size(eap);
3673 if (lnum == 0)
3674 lnum = 1;
Bram Moolenaare906c502015-09-09 21:10:39 +02003675#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003676 break;
Bram Moolenaar25190db2019-05-04 15:05:28 +02003677 case ADDR_QUICKFIX_VALID:
3678#ifdef FEAT_QUICKFIX
3679 lnum = qf_get_valid_size(eap);
3680 if (lnum == 0)
3681 lnum = 1;
3682#endif
3683 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003684 }
3685 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003686
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003687 case '\'': // ''' - mark
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003688 if (*++cmd == NUL)
3689 {
3690 cmd = NULL;
3691 goto error;
3692 }
3693 if (addr_type != ADDR_LINES)
3694 {
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003695 addr_error(addr_type);
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01003696 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003697 goto error;
3698 }
3699 if (skip)
3700 ++cmd;
3701 else
3702 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003703 // Only accept a mark in another file when it is
3704 // used by itself: ":'M".
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003705 fp = getmark(*cmd, to_other_file && cmd[1] == NUL);
3706 ++cmd;
3707 if (fp == (pos_T *)-1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003708 // Jumped to another file.
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003709 lnum = curwin->w_cursor.lnum;
3710 else
3711 {
3712 if (check_mark(fp) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003713 {
3714 cmd = NULL;
3715 goto error;
3716 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003717 lnum = fp->lnum;
3718 }
3719 }
3720 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003721
3722 case '/':
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003723 case '?': // '/' or '?' - search
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003724 c = *cmd++;
3725 if (addr_type != ADDR_LINES)
3726 {
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003727 addr_error(addr_type);
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01003728 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003729 goto error;
3730 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003731 if (skip) // skip "/pat/"
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003732 {
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02003733 cmd = skip_regexp(cmd, c, (int)p_magic);
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003734 if (*cmd == c)
3735 ++cmd;
3736 }
3737 else
3738 {
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02003739 int flags;
3740
3741 pos = curwin->w_cursor; // save curwin->w_cursor
3742
3743 // When '/' or '?' follows another address, start from
3744 // there.
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003745 if (lnum != MAXLNUM)
3746 curwin->w_cursor.lnum = lnum;
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02003747
3748 // Start a forward search at the end of the line (unless
3749 // before the first line).
3750 // Start a backward search at the start of the line.
3751 // This makes sure we never match in the current
3752 // line, and can match anywhere in the
3753 // next/previous line.
Bram Moolenaar8ada6aa2017-12-19 21:23:21 +01003754 if (c == '/' && curwin->w_cursor.lnum > 0)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003755 curwin->w_cursor.col = MAXCOL;
3756 else
3757 curwin->w_cursor.col = 0;
3758 searchcmdlen = 0;
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02003759 flags = silent ? 0 : SEARCH_HIS | SEARCH_MSG;
Bram Moolenaarc036e872020-02-21 21:30:52 +01003760 if (!do_search(NULL, c, c, cmd, 1L, flags, NULL))
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003761 {
3762 curwin->w_cursor = pos;
3763 cmd = NULL;
3764 goto error;
3765 }
3766 lnum = curwin->w_cursor.lnum;
3767 curwin->w_cursor = pos;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003768 // adjust command string pointer
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003769 cmd += searchcmdlen;
3770 }
3771 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003772
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003773 case '\\': // "\?", "\/" or "\&", repeat search
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003774 ++cmd;
3775 if (addr_type != ADDR_LINES)
3776 {
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003777 addr_error(addr_type);
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01003778 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003779 goto error;
3780 }
3781 if (*cmd == '&')
3782 i = RE_SUBST;
3783 else if (*cmd == '?' || *cmd == '/')
3784 i = RE_SEARCH;
3785 else
3786 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003787 emsg(_(e_backslash));
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003788 cmd = NULL;
3789 goto error;
3790 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003791
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003792 if (!skip)
3793 {
3794 /*
3795 * When search follows another address, start from
3796 * there.
3797 */
3798 if (lnum != MAXLNUM)
3799 pos.lnum = lnum;
3800 else
3801 pos.lnum = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003802
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003803 /*
3804 * Start the search just like for the above
3805 * do_search().
3806 */
3807 if (*cmd != '?')
3808 pos.col = MAXCOL;
3809 else
3810 pos.col = 0;
Bram Moolenaarbd8539a2015-08-11 18:53:03 +02003811 pos.coladd = 0;
Bram Moolenaar5d24a222018-12-23 19:10:09 +01003812 if (searchit(curwin, curbuf, &pos, NULL,
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003813 *cmd == '?' ? BACKWARD : FORWARD,
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02003814 (char_u *)"", 1L, SEARCH_MSG, i, NULL) != FAIL)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003815 lnum = pos.lnum;
3816 else
3817 {
3818 cmd = NULL;
3819 goto error;
3820 }
3821 }
3822 ++cmd;
3823 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003824
3825 default:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003826 if (VIM_ISDIGIT(*cmd)) // absolute line number
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003827 lnum = getdigits(&cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003828 }
3829
3830 for (;;)
3831 {
3832 cmd = skipwhite(cmd);
3833 if (*cmd != '-' && *cmd != '+' && !VIM_ISDIGIT(*cmd))
3834 break;
3835
3836 if (lnum == MAXLNUM)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003837 {
3838 switch (addr_type)
3839 {
3840 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02003841 case ADDR_OTHER:
3842 // "+1" is same as ".+1"
Bram Moolenaarf240e182014-11-27 18:33:02 +01003843 lnum = curwin->w_cursor.lnum;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003844 break;
3845 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01003846 lnum = CURRENT_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003847 break;
3848 case ADDR_ARGUMENTS:
3849 lnum = curwin->w_arg_idx + 1;
3850 break;
3851 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003852 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003853 lnum = curbuf->b_fnum;
3854 break;
3855 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01003856 lnum = CURRENT_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003857 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01003858 case ADDR_TABS_RELATIVE:
3859 lnum = 1;
3860 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003861 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003862#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02003863 lnum = qf_get_cur_idx(eap);
3864#endif
3865 break;
3866 case ADDR_QUICKFIX_VALID:
3867#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003868 lnum = qf_get_cur_valid_idx(eap);
Bram Moolenaare906c502015-09-09 21:10:39 +02003869#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003870 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02003871 case ADDR_NONE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02003872 case ADDR_UNSIGNED:
3873 lnum = 0;
Bram Moolenaarb7316892019-05-01 18:08:42 +02003874 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003875 }
3876 }
3877
Bram Moolenaar071d4272004-06-13 20:20:40 +00003878 if (VIM_ISDIGIT(*cmd))
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003879 i = '+'; // "number" is same as "+number"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003880 else
3881 i = *cmd++;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003882 if (!VIM_ISDIGIT(*cmd)) // '+' is '+1', but '+0' is not '+1'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003883 n = 1;
3884 else
3885 n = getdigits(&cmd);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01003886
3887 if (addr_type == ADDR_TABS_RELATIVE)
3888 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003889 emsg(_(e_invrange));
Bram Moolenaar2f72c702017-01-29 14:48:10 +01003890 cmd = NULL;
3891 goto error;
3892 }
3893 else if (addr_type == ADDR_LOADED_BUFFERS
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003894 || addr_type == ADDR_BUFFERS)
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01003895 lnum = compute_buffer_local_count(
3896 addr_type, lnum, (i == '-') ? -1 * n : n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003897 else
Bram Moolenaarded27822017-01-02 14:27:34 +01003898 {
3899#ifdef FEAT_FOLDING
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003900 // Relative line addressing, need to adjust for folded lines
3901 // now, but only do it after the first address.
Bram Moolenaarded27822017-01-02 14:27:34 +01003902 if (addr_type == ADDR_LINES && (i == '-' || i == '+')
3903 && address_count >= 2)
3904 (void)hasFolding(lnum, NULL, &lnum);
3905#endif
3906 if (i == '-')
3907 lnum -= n;
3908 else
3909 lnum += n;
3910 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003911 }
3912 } while (*cmd == '/' || *cmd == '?');
3913
3914error:
3915 *ptr = cmd;
3916 return lnum;
3917}
3918
3919/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00003920 * Get flags from an Ex command argument.
3921 */
3922 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003923get_flags(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00003924{
3925 while (vim_strchr((char_u *)"lp#", *eap->arg) != NULL)
3926 {
3927 if (*eap->arg == 'l')
3928 eap->flags |= EXFLAG_LIST;
3929 else if (*eap->arg == 'p')
3930 eap->flags |= EXFLAG_PRINT;
3931 else
3932 eap->flags |= EXFLAG_NR;
3933 eap->arg = skipwhite(eap->arg + 1);
3934 }
3935}
3936
3937/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003938 * Function called for command which is Not Implemented. NI!
3939 */
3940 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003941ex_ni(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003942{
3943 if (!eap->skip)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003944 eap->errmsg = N_("E319: Sorry, the command is not available in this version");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003945}
3946
Bram Moolenaar7bb75552007-07-16 18:39:49 +00003947#ifdef HAVE_EX_SCRIPT_NI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003948/*
3949 * Function called for script command which is Not Implemented. NI!
3950 * Skips over ":perl <<EOF" constructs.
3951 */
3952 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003953ex_script_ni(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003954{
3955 if (!eap->skip)
3956 ex_ni(eap);
3957 else
3958 vim_free(script_get(eap, eap->arg));
3959}
3960#endif
3961
3962/*
3963 * Check range in Ex command for validity.
3964 * Return NULL when valid, error message when invalid.
3965 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003966 static char *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003967invalid_range(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003968{
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003969 buf_T *buf;
Bram Moolenaar25190db2019-05-04 15:05:28 +02003970
Bram Moolenaar071d4272004-06-13 20:20:40 +00003971 if ( eap->line1 < 0
3972 || eap->line2 < 0
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003973 || eap->line1 > eap->line2)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003974 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003975
Bram Moolenaar8071cb22019-07-12 17:58:01 +02003976 if (eap->argt & EX_RANGE)
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003977 {
Bram Moolenaarb7316892019-05-01 18:08:42 +02003978 switch (eap->addr_type)
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003979 {
3980 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02003981 if (eap->line2 > curbuf->b_ml.ml_line_count
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003982#ifdef FEAT_DIFF
3983 + (eap->cmdidx == CMD_diffget)
3984#endif
3985 )
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003986 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003987 break;
3988 case ADDR_ARGUMENTS:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003989 // add 1 if ARGCOUNT is 0
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01003990 if (eap->line2 > ARGCOUNT + (!ARGCOUNT))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003991 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003992 break;
3993 case ADDR_BUFFERS:
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02003994 // Only a boundary check, not whether the buffers actually
3995 // exist.
3996 if (eap->line1 < 1 || eap->line2 > get_highest_fnum())
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003997 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003998 break;
3999 case ADDR_LOADED_BUFFERS:
4000 buf = firstbuf;
4001 while (buf->b_ml.ml_mfp == NULL)
4002 {
4003 if (buf->b_next == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004004 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004005 buf = buf->b_next;
4006 }
4007 if (eap->line1 < buf->b_fnum)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004008 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004009 buf = lastbuf;
4010 while (buf->b_ml.ml_mfp == NULL)
4011 {
4012 if (buf->b_prev == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004013 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004014 buf = buf->b_prev;
4015 }
4016 if (eap->line2 > buf->b_fnum)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004017 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004018 break;
4019 case ADDR_WINDOWS:
Bram Moolenaar8be63882015-01-14 11:25:05 +01004020 if (eap->line2 > LAST_WIN_NR)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004021 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004022 break;
4023 case ADDR_TABS:
4024 if (eap->line2 > LAST_TAB_NR)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004025 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004026 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004027 case ADDR_TABS_RELATIVE:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004028 case ADDR_OTHER:
4029 // Any range is OK.
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004030 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004031 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004032#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02004033 // No error for value that is too big, will use the last entry.
4034 if (eap->line2 <= 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004035 return _(e_invrange);
Bram Moolenaare906c502015-09-09 21:10:39 +02004036#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004037 break;
Bram Moolenaar25190db2019-05-04 15:05:28 +02004038 case ADDR_QUICKFIX_VALID:
4039#ifdef FEAT_QUICKFIX
4040 if ((eap->line2 != 1 && eap->line2 > qf_get_valid_size(eap))
4041 || eap->line2 < 0)
4042 return _(e_invrange);
4043#endif
4044 break;
4045 case ADDR_UNSIGNED:
4046 if (eap->line2 < 0)
4047 return _(e_invrange);
4048 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02004049 case ADDR_NONE:
4050 // Will give an error elsewhere.
4051 break;
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004052 }
4053 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004054 return NULL;
4055}
4056
4057/*
4058 * Correct the range for zero line number, if required.
4059 */
4060 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004061correct_range(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004062{
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004063 if (!(eap->argt & EX_ZEROR)) // zero in range not allowed
Bram Moolenaar071d4272004-06-13 20:20:40 +00004064 {
4065 if (eap->line1 == 0)
4066 eap->line1 = 1;
4067 if (eap->line2 == 0)
4068 eap->line2 = 1;
4069 }
4070}
4071
Bram Moolenaar748bf032005-02-02 23:04:36 +00004072#ifdef FEAT_QUICKFIX
Bram Moolenaar748bf032005-02-02 23:04:36 +00004073/*
4074 * For a ":vimgrep" or ":vimgrepadd" command return a pointer past the
4075 * pattern. Otherwise return eap->arg.
4076 */
4077 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004078skip_grep_pat(exarg_T *eap)
Bram Moolenaar748bf032005-02-02 23:04:36 +00004079{
4080 char_u *p = eap->arg;
4081
Bram Moolenaara37420f2006-02-04 22:37:47 +00004082 if (*p != NUL && (eap->cmdidx == CMD_vimgrep || eap->cmdidx == CMD_lvimgrep
4083 || eap->cmdidx == CMD_vimgrepadd
4084 || eap->cmdidx == CMD_lvimgrepadd
4085 || grep_internal(eap->cmdidx)))
Bram Moolenaar748bf032005-02-02 23:04:36 +00004086 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00004087 p = skip_vimgrep_pat(p, NULL, NULL);
Bram Moolenaar748bf032005-02-02 23:04:36 +00004088 if (p == NULL)
4089 p = eap->arg;
Bram Moolenaar748bf032005-02-02 23:04:36 +00004090 }
4091 return p;
4092}
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004093
4094/*
4095 * For the ":make" and ":grep" commands insert the 'makeprg'/'grepprg' option
4096 * in the command line, so that things like % get expanded.
4097 */
4098 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004099replace_makeprg(exarg_T *eap, char_u *p, char_u **cmdlinep)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004100{
4101 char_u *new_cmdline;
4102 char_u *program;
4103 char_u *pos;
4104 char_u *ptr;
4105 int len;
4106 int i;
4107
4108 /*
4109 * Don't do it when ":vimgrep" is used for ":grep".
4110 */
Bram Moolenaara37420f2006-02-04 22:37:47 +00004111 if ((eap->cmdidx == CMD_make || eap->cmdidx == CMD_lmake
4112 || eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4113 || eap->cmdidx == CMD_grepadd
4114 || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004115 && !grep_internal(eap->cmdidx))
4116 {
Bram Moolenaara37420f2006-02-04 22:37:47 +00004117 if (eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4118 || eap->cmdidx == CMD_grepadd || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004119 {
4120 if (*curbuf->b_p_gp == NUL)
4121 program = p_gp;
4122 else
4123 program = curbuf->b_p_gp;
4124 }
4125 else
4126 {
4127 if (*curbuf->b_p_mp == NUL)
4128 program = p_mp;
4129 else
4130 program = curbuf->b_p_mp;
4131 }
4132
4133 p = skipwhite(p);
4134
4135 if ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4136 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004137 // replace $* by given arguments
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004138 i = 1;
4139 while ((pos = (char_u *)strstr((char *)pos + 2, "$*")) != NULL)
4140 ++i;
4141 len = (int)STRLEN(p);
Bram Moolenaar51e14382019-05-25 20:21:28 +02004142 new_cmdline = alloc(STRLEN(program) + i * (len - 2) + 1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004143 if (new_cmdline == NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004144 return NULL; // out of memory
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004145 ptr = new_cmdline;
4146 while ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4147 {
4148 i = (int)(pos - program);
4149 STRNCPY(ptr, program, i);
4150 STRCPY(ptr += i, p);
4151 ptr += len;
4152 program = pos + 2;
4153 }
4154 STRCPY(ptr, program);
4155 }
4156 else
4157 {
Bram Moolenaar51e14382019-05-25 20:21:28 +02004158 new_cmdline = alloc(STRLEN(program) + STRLEN(p) + 2);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004159 if (new_cmdline == NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004160 return NULL; // out of memory
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004161 STRCPY(new_cmdline, program);
4162 STRCAT(new_cmdline, " ");
4163 STRCAT(new_cmdline, p);
4164 }
4165 msg_make(p);
4166
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004167 // 'eap->cmd' is not set here, because it is not used at CMD_make
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004168 vim_free(*cmdlinep);
4169 *cmdlinep = new_cmdline;
4170 p = new_cmdline;
4171 }
4172 return p;
4173}
Bram Moolenaar748bf032005-02-02 23:04:36 +00004174#endif
4175
Bram Moolenaar071d4272004-06-13 20:20:40 +00004176/*
4177 * Expand file name in Ex command argument.
Bram Moolenaar0abb4272019-06-15 16:06:00 +02004178 * When an error is detected, "errormsgp" is set to a non-NULL pointer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004179 * Return FAIL for failure, OK otherwise.
4180 */
4181 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004182expand_filename(
4183 exarg_T *eap,
4184 char_u **cmdlinep,
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004185 char **errormsgp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004186{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004187 int has_wildcards; // need to expand wildcards
Bram Moolenaar071d4272004-06-13 20:20:40 +00004188 char_u *repl;
4189 int srclen;
4190 char_u *p;
4191 int n;
Bram Moolenaar63b92542007-03-27 14:57:09 +00004192 int escaped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004193
Bram Moolenaar748bf032005-02-02 23:04:36 +00004194#ifdef FEAT_QUICKFIX
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004195 // Skip a regexp pattern for ":vimgrep[add] pat file..."
Bram Moolenaar748bf032005-02-02 23:04:36 +00004196 p = skip_grep_pat(eap);
4197#else
4198 p = eap->arg;
4199#endif
4200
Bram Moolenaar071d4272004-06-13 20:20:40 +00004201 /*
4202 * Decide to expand wildcards *before* replacing '%', '#', etc. If
4203 * the file name contains a wildcard it should not cause expanding.
4204 * (it will be expanded anyway if there is a wildcard before replacing).
4205 */
Bram Moolenaar748bf032005-02-02 23:04:36 +00004206 has_wildcards = mch_has_wildcard(p);
4207 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004208 {
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004209#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004210 // Skip over `=expr`, wildcards in it are not expanded.
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004211 if (p[0] == '`' && p[1] == '=')
4212 {
4213 p += 2;
4214 (void)skip_expr(&p);
4215 if (*p == '`')
4216 ++p;
4217 continue;
4218 }
4219#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004220 /*
4221 * Quick check if this cannot be the start of a special string.
4222 * Also removes backslash before '%', '#' and '<'.
4223 */
4224 if (vim_strchr((char_u *)"%#<", *p) == NULL)
4225 {
4226 ++p;
4227 continue;
4228 }
4229
4230 /*
4231 * Try to find a match at this position.
4232 */
Bram Moolenaar63b92542007-03-27 14:57:09 +00004233 repl = eval_vars(p, eap->arg, &srclen, &(eap->do_ecmd_lnum),
4234 errormsgp, &escaped);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004235 if (*errormsgp != NULL) // error detected
Bram Moolenaar071d4272004-06-13 20:20:40 +00004236 return FAIL;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004237 if (repl == NULL) // no match found
Bram Moolenaar071d4272004-06-13 20:20:40 +00004238 {
4239 p += srclen;
4240 continue;
4241 }
4242
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004243 // Wildcards won't be expanded below, the replacement is taken
4244 // literally. But do expand "~/file", "~user/file" and "$HOME/file".
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00004245 if (vim_strchr(repl, '$') != NULL || vim_strchr(repl, '~') != NULL)
4246 {
4247 char_u *l = repl;
4248
4249 repl = expand_env_save(repl);
4250 vim_free(l);
4251 }
4252
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004253 // Need to escape white space et al. with a backslash.
4254 // Don't do this for:
4255 // - replacement that already has been escaped: "##"
4256 // - shell commands (may have to use quotes instead).
4257 // - non-unix systems when there is a single argument (spaces don't
4258 // separate arguments then).
Bram Moolenaar071d4272004-06-13 20:20:40 +00004259 if (!eap->usefilter
Bram Moolenaar63b92542007-03-27 14:57:09 +00004260 && !escaped
Bram Moolenaar071d4272004-06-13 20:20:40 +00004261 && eap->cmdidx != CMD_bang
Bram Moolenaar071d4272004-06-13 20:20:40 +00004262 && eap->cmdidx != CMD_grep
4263 && eap->cmdidx != CMD_grepadd
Bram Moolenaarbf15b8d2017-06-04 20:43:48 +02004264 && eap->cmdidx != CMD_hardcopy
Bram Moolenaar67883b42017-07-27 22:57:00 +02004265 && eap->cmdidx != CMD_lgrep
4266 && eap->cmdidx != CMD_lgrepadd
4267 && eap->cmdidx != CMD_lmake
4268 && eap->cmdidx != CMD_make
4269 && eap->cmdidx != CMD_terminal
Bram Moolenaar071d4272004-06-13 20:20:40 +00004270#ifndef UNIX
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004271 && !(eap->argt & EX_NOSPC)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004272#endif
4273 )
4274 {
4275 char_u *l;
4276#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004277 // Don't escape a backslash here, because rem_backslash() doesn't
4278 // remove it later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004279 static char_u *nobslash = (char_u *)" \t\"|";
4280# define ESCAPE_CHARS nobslash
4281#else
4282# define ESCAPE_CHARS escape_chars
4283#endif
4284
4285 for (l = repl; *l; ++l)
4286 if (vim_strchr(ESCAPE_CHARS, *l) != NULL)
4287 {
4288 l = vim_strsave_escaped(repl, ESCAPE_CHARS);
4289 if (l != NULL)
4290 {
4291 vim_free(repl);
4292 repl = l;
4293 }
4294 break;
4295 }
4296 }
4297
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004298 // For a shell command a '!' must be escaped.
Bram Moolenaar67883b42017-07-27 22:57:00 +02004299 if ((eap->usefilter || eap->cmdidx == CMD_bang
4300 || eap->cmdidx == CMD_terminal)
Bram Moolenaar31b7d382014-04-01 18:54:48 +02004301 && vim_strpbrk(repl, (char_u *)"!") != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004302 {
4303 char_u *l;
4304
Bram Moolenaar31b7d382014-04-01 18:54:48 +02004305 l = vim_strsave_escaped(repl, (char_u *)"!");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004306 if (l != NULL)
4307 {
4308 vim_free(repl);
4309 repl = l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004310 }
4311 }
4312
4313 p = repl_cmdline(eap, p, srclen, repl, cmdlinep);
4314 vim_free(repl);
4315 if (p == NULL)
4316 return FAIL;
4317 }
4318
4319 /*
4320 * One file argument: Expand wildcards.
4321 * Don't do this with ":r !command" or ":w !command".
4322 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004323 if ((eap->argt & EX_NOSPC) && !eap->usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004324 {
4325 /*
4326 * May do this twice:
4327 * 1. Replace environment variables.
4328 * 2. Replace any other wildcards, remove backslashes.
4329 */
4330 for (n = 1; n <= 2; ++n)
4331 {
4332 if (n == 2)
4333 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004334 /*
4335 * Halve the number of backslashes (this is Vi compatible).
4336 * For Unix and OS/2, when wildcards are expanded, this is
4337 * done by ExpandOne() below.
4338 */
Bram Moolenaare7fedb62015-12-31 19:07:19 +01004339#if defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004340 if (!has_wildcards)
4341#endif
4342 backslash_halve(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004343 }
4344
4345 if (has_wildcards)
4346 {
4347 if (n == 1)
4348 {
4349 /*
4350 * First loop: May expand environment variables. This
4351 * can be done much faster with expand_env() than with
4352 * something else (e.g., calling a shell).
4353 * After expanding environment variables, check again
4354 * if there are still wildcards present.
4355 */
4356 if (vim_strchr(eap->arg, '$') != NULL
4357 || vim_strchr(eap->arg, '~') != NULL)
4358 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +00004359 expand_env_esc(eap->arg, NameBuff, MAXPATHL,
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00004360 TRUE, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004361 has_wildcards = mch_has_wildcard(NameBuff);
4362 p = NameBuff;
4363 }
4364 else
4365 p = NULL;
4366 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004367 else // n == 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00004368 {
4369 expand_T xpc;
Bram Moolenaarb40c2572019-10-19 21:01:05 +02004370 int options = WILD_LIST_NOTFOUND
4371 | WILD_NOERROR | WILD_ADD_SLASH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004372
4373 ExpandInit(&xpc);
4374 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar94950a92010-12-02 16:01:29 +01004375 if (p_wic)
4376 options += WILD_ICASE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004377 p = ExpandOne(&xpc, eap->arg, NULL,
Bram Moolenaar94950a92010-12-02 16:01:29 +01004378 options, WILD_EXPAND_FREE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004379 if (p == NULL)
4380 return FAIL;
4381 }
4382 if (p != NULL)
4383 {
4384 (void)repl_cmdline(eap, eap->arg, (int)STRLEN(eap->arg),
4385 p, cmdlinep);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004386 if (n == 2) // p came from ExpandOne()
Bram Moolenaar071d4272004-06-13 20:20:40 +00004387 vim_free(p);
4388 }
4389 }
4390 }
4391 }
4392 return OK;
4393}
4394
4395/*
4396 * Replace part of the command line, keeping eap->cmd, eap->arg and
4397 * eap->nextcmd correct.
4398 * "src" points to the part that is to be replaced, of length "srclen".
4399 * "repl" is the replacement string.
4400 * Returns a pointer to the character after the replaced string.
4401 * Returns NULL for failure.
4402 */
4403 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004404repl_cmdline(
4405 exarg_T *eap,
4406 char_u *src,
4407 int srclen,
4408 char_u *repl,
4409 char_u **cmdlinep)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004410{
4411 int len;
4412 int i;
4413 char_u *new_cmdline;
4414
4415 /*
4416 * The new command line is build in new_cmdline[].
4417 * First allocate it.
4418 * Careful: a "+cmd" argument may have been NUL terminated.
4419 */
4420 len = (int)STRLEN(repl);
4421 i = (int)(src - *cmdlinep) + (int)STRLEN(src + srclen) + len + 3;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004422 if (eap->nextcmd != NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004423 i += (int)STRLEN(eap->nextcmd);// add space for next command
Bram Moolenaar964b3742019-05-24 18:54:09 +02004424 if ((new_cmdline = alloc(i)) == NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004425 return NULL; // out of memory!
Bram Moolenaar071d4272004-06-13 20:20:40 +00004426
4427 /*
4428 * Copy the stuff before the expanded part.
4429 * Copy the expanded stuff.
4430 * Copy what came after the expanded part.
4431 * Copy the next commands, if there are any.
4432 */
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004433 i = (int)(src - *cmdlinep); // length of part before match
Bram Moolenaar071d4272004-06-13 20:20:40 +00004434 mch_memmove(new_cmdline, *cmdlinep, (size_t)i);
Bram Moolenaara3ffd9c2005-07-21 21:03:15 +00004435
Bram Moolenaar071d4272004-06-13 20:20:40 +00004436 mch_memmove(new_cmdline + i, repl, (size_t)len);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004437 i += len; // remember the end of the string
Bram Moolenaar071d4272004-06-13 20:20:40 +00004438 STRCPY(new_cmdline + i, src + srclen);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004439 src = new_cmdline + i; // remember where to continue
Bram Moolenaar071d4272004-06-13 20:20:40 +00004440
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004441 if (eap->nextcmd != NULL) // append next command
Bram Moolenaar071d4272004-06-13 20:20:40 +00004442 {
4443 i = (int)STRLEN(new_cmdline) + 1;
4444 STRCPY(new_cmdline + i, eap->nextcmd);
4445 eap->nextcmd = new_cmdline + i;
4446 }
4447 eap->cmd = new_cmdline + (eap->cmd - *cmdlinep);
4448 eap->arg = new_cmdline + (eap->arg - *cmdlinep);
4449 if (eap->do_ecmd_cmd != NULL && eap->do_ecmd_cmd != dollar_command)
4450 eap->do_ecmd_cmd = new_cmdline + (eap->do_ecmd_cmd - *cmdlinep);
4451 vim_free(*cmdlinep);
4452 *cmdlinep = new_cmdline;
4453
4454 return src;
4455}
4456
4457/*
4458 * Check for '|' to separate commands and '"' to start comments.
4459 */
4460 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004461separate_nextcmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004462{
4463 char_u *p;
4464
Bram Moolenaar86b68352004-12-27 21:59:20 +00004465#ifdef FEAT_QUICKFIX
Bram Moolenaar748bf032005-02-02 23:04:36 +00004466 p = skip_grep_pat(eap);
4467#else
4468 p = eap->arg;
Bram Moolenaar86b68352004-12-27 21:59:20 +00004469#endif
4470
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004471 for ( ; *p; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004472 {
4473 if (*p == Ctrl_V)
4474 {
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004475 if (eap->argt & (EX_CTRLV | EX_XFILE))
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004476 ++p; // skip CTRL-V and next char
Bram Moolenaar071d4272004-06-13 20:20:40 +00004477 else
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004478 // remove CTRL-V and skip next char
Bram Moolenaara7241f52008-06-24 20:39:31 +00004479 STRMOVE(p, p + 1);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004480 if (*p == NUL) // stop at NUL after CTRL-V
Bram Moolenaar071d4272004-06-13 20:20:40 +00004481 break;
4482 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004483
4484#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004485 // Skip over `=expr` when wildcards are expanded.
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004486 else if (p[0] == '`' && p[1] == '=' && (eap->argt & EX_XFILE))
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004487 {
4488 p += 2;
4489 (void)skip_expr(&p);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004490 }
4491#endif
4492
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004493 // Check for '"': start of comment or '|': next command
4494 // :@" and :*" do not start a comment!
4495 // :redir @" doesn't either.
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004496 else if ((*p == '"' && !(eap->argt & EX_NOTRLCOM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004497 && ((eap->cmdidx != CMD_at && eap->cmdidx != CMD_star)
4498 || p != eap->arg)
4499 && (eap->cmdidx != CMD_redir
4500 || p != eap->arg + 1 || p[-1] != '@'))
Bram Moolenaara26b9702020-04-18 19:53:28 +02004501#ifdef FEAT_EVAL
4502 || (*p == '#' && in_vim9script()
4503 && p[1] != '{' && p > eap->cmd && VIM_ISWHITE(p[-1]))
4504#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004505 || *p == '|' || *p == '\n')
4506 {
4507 /*
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004508 * We remove the '\' before the '|', unless EX_CTRLV is used
Bram Moolenaar071d4272004-06-13 20:20:40 +00004509 * AND 'b' is present in 'cpoptions'.
4510 */
4511 if ((vim_strchr(p_cpo, CPO_BAR) == NULL
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004512 || !(eap->argt & EX_CTRLV)) && *(p - 1) == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004513 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004514 STRMOVE(p - 1, p); // remove the '\'
Bram Moolenaar071d4272004-06-13 20:20:40 +00004515 --p;
4516 }
4517 else
4518 {
4519 eap->nextcmd = check_nextcmd(p);
4520 *p = NUL;
4521 break;
4522 }
4523 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004524 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00004525
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004526 if (!(eap->argt & EX_NOTRLCOM)) // remove trailing spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00004527 del_trailing_spaces(eap->arg);
4528}
4529
4530/*
4531 * get + command from ex argument
4532 */
4533 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004534getargcmd(char_u **argp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004535{
4536 char_u *arg = *argp;
4537 char_u *command = NULL;
4538
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004539 if (*arg == '+') // +[command]
Bram Moolenaar071d4272004-06-13 20:20:40 +00004540 {
4541 ++arg;
Bram Moolenaar3e451592014-04-02 14:22:05 +02004542 if (vim_isspace(*arg) || *arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004543 command = dollar_command;
4544 else
4545 {
4546 command = arg;
4547 arg = skip_cmd_arg(command, TRUE);
4548 if (*arg != NUL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004549 *arg++ = NUL; // terminate command with NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004550 }
4551
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004552 arg = skipwhite(arg); // skip over spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00004553 *argp = arg;
4554 }
4555 return command;
4556}
4557
4558/*
4559 * Find end of "+command" argument. Skip over "\ " and "\\".
4560 */
Bram Moolenaard0190392019-08-23 21:17:35 +02004561 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004562skip_cmd_arg(
4563 char_u *p,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004564 int rembs) // TRUE to halve the number of backslashes
Bram Moolenaar071d4272004-06-13 20:20:40 +00004565{
4566 while (*p && !vim_isspace(*p))
4567 {
4568 if (*p == '\\' && p[1] != NUL)
4569 {
4570 if (rembs)
Bram Moolenaara7241f52008-06-24 20:39:31 +00004571 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004572 else
4573 ++p;
4574 }
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004575 MB_PTR_ADV(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004576 }
4577 return p;
4578}
4579
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004580 int
4581get_bad_opt(char_u *p, exarg_T *eap)
4582{
4583 if (STRICMP(p, "keep") == 0)
4584 eap->bad_char = BAD_KEEP;
4585 else if (STRICMP(p, "drop") == 0)
4586 eap->bad_char = BAD_DROP;
4587 else if (MB_BYTE2LEN(*p) == 1 && p[1] == NUL)
4588 eap->bad_char = *p;
Bram Moolenaar75808492018-06-12 12:39:41 +02004589 else
4590 return FAIL;
4591 return OK;
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004592}
4593
Bram Moolenaar071d4272004-06-13 20:20:40 +00004594/*
4595 * Get "++opt=arg" argument.
4596 * Return FAIL or OK.
4597 */
4598 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004599getargopt(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004600{
4601 char_u *arg = eap->arg + 2;
4602 int *pp = NULL;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02004603 int bad_char_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004604 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004605
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004606 // ":edit ++[no]bin[ary] file"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004607 if (STRNCMP(arg, "bin", 3) == 0 || STRNCMP(arg, "nobin", 5) == 0)
4608 {
4609 if (*arg == 'n')
4610 {
4611 arg += 2;
4612 eap->force_bin = FORCE_NOBIN;
4613 }
4614 else
4615 eap->force_bin = FORCE_BIN;
4616 if (!checkforcmd(&arg, "binary", 3))
4617 return FAIL;
4618 eap->arg = skipwhite(arg);
4619 return OK;
4620 }
4621
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004622 // ":read ++edit file"
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004623 if (STRNCMP(arg, "edit", 4) == 0)
4624 {
4625 eap->read_edit = TRUE;
4626 eap->arg = skipwhite(arg + 4);
4627 return OK;
4628 }
4629
Bram Moolenaar071d4272004-06-13 20:20:40 +00004630 if (STRNCMP(arg, "ff", 2) == 0)
4631 {
4632 arg += 2;
4633 pp = &eap->force_ff;
4634 }
4635 else if (STRNCMP(arg, "fileformat", 10) == 0)
4636 {
4637 arg += 10;
4638 pp = &eap->force_ff;
4639 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004640 else if (STRNCMP(arg, "enc", 3) == 0)
4641 {
Bram Moolenaarb38e9ab2011-12-14 14:49:45 +01004642 if (STRNCMP(arg, "encoding", 8) == 0)
4643 arg += 8;
4644 else
4645 arg += 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004646 pp = &eap->force_enc;
4647 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004648 else if (STRNCMP(arg, "bad", 3) == 0)
4649 {
4650 arg += 3;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02004651 pp = &bad_char_idx;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004652 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004653
4654 if (pp == NULL || *arg != '=')
4655 return FAIL;
4656
4657 ++arg;
4658 *pp = (int)(arg - eap->cmd);
4659 arg = skip_cmd_arg(arg, FALSE);
4660 eap->arg = skipwhite(arg);
4661 *arg = NUL;
4662
Bram Moolenaar071d4272004-06-13 20:20:40 +00004663 if (pp == &eap->force_ff)
4664 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004665 if (check_ff_value(eap->cmd + eap->force_ff) == FAIL)
4666 return FAIL;
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004667 eap->force_ff = eap->cmd[eap->force_ff];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004668 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004669 else if (pp == &eap->force_enc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004670 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004671 // Make 'fileencoding' lower case.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004672 for (p = eap->cmd + eap->force_enc; *p != NUL; ++p)
4673 *p = TOLOWER_ASC(*p);
4674 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004675 else
4676 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004677 // Check ++bad= argument. Must be a single-byte character, "keep" or
4678 // "drop".
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004679 if (get_bad_opt(eap->cmd + bad_char_idx, eap) == FAIL)
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004680 return FAIL;
4681 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004682
4683 return OK;
4684}
4685
Bram Moolenaar071d4272004-06-13 20:20:40 +00004686 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004687ex_autocmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004688{
4689 /*
Bram Moolenaar26d4b892018-07-04 22:26:28 +02004690 * Disallow autocommands from .exrc and .vimrc in current
Bram Moolenaar071d4272004-06-13 20:20:40 +00004691 * directory for security reasons.
4692 */
4693 if (secure)
4694 {
4695 secure = 2;
4696 eap->errmsg = e_curdir;
4697 }
4698 else if (eap->cmdidx == CMD_autocmd)
4699 do_autocmd(eap->arg, eap->forceit);
4700 else
4701 do_augroup(eap->arg, eap->forceit);
4702}
4703
4704/*
4705 * ":doautocmd": Apply the automatic commands to the current buffer.
4706 */
4707 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004708ex_doautocmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004709{
Bram Moolenaar60542ac2012-02-12 20:14:01 +01004710 char_u *arg = eap->arg;
4711 int call_do_modelines = check_nomodeline(&arg);
Bram Moolenaar1610d052016-06-09 22:53:01 +02004712 int did_aucmd;
Bram Moolenaar60542ac2012-02-12 20:14:01 +01004713
Bram Moolenaar1610d052016-06-09 22:53:01 +02004714 (void)do_doautocmd(arg, TRUE, &did_aucmd);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004715 // Only when there is no <nomodeline>.
Bram Moolenaar1610d052016-06-09 22:53:01 +02004716 if (call_do_modelines && did_aucmd)
Bram Moolenaar60542ac2012-02-12 20:14:01 +01004717 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004718}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004719
Bram Moolenaar071d4272004-06-13 20:20:40 +00004720/*
4721 * :[N]bunload[!] [N] [bufname] unload buffer
4722 * :[N]bdelete[!] [N] [bufname] delete buffer from buffer list
4723 * :[N]bwipeout[!] [N] [bufname] delete buffer really
4724 */
4725 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004726ex_bunload(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004727{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01004728 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02004729 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004730 eap->errmsg = do_bufdel(
4731 eap->cmdidx == CMD_bdelete ? DOBUF_DEL
4732 : eap->cmdidx == CMD_bwipeout ? DOBUF_WIPE
4733 : DOBUF_UNLOAD, eap->arg,
4734 eap->addr_count, (int)eap->line1, (int)eap->line2, eap->forceit);
4735}
4736
4737/*
4738 * :[N]buffer [N] to buffer N
4739 * :[N]sbuffer [N] to buffer N
4740 */
4741 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004742ex_buffer(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004743{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01004744 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02004745 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004746 if (*eap->arg)
4747 eap->errmsg = e_trailing;
4748 else
4749 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004750 if (eap->addr_count == 0) // default is current buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00004751 goto_buffer(eap, DOBUF_CURRENT, FORWARD, 0);
4752 else
4753 goto_buffer(eap, DOBUF_FIRST, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02004754 if (eap->do_ecmd_cmd != NULL)
4755 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004756 }
4757}
4758
4759/*
4760 * :[N]bmodified [N] to next mod. buffer
4761 * :[N]sbmodified [N] to next mod. buffer
4762 */
4763 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004764ex_bmodified(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004765{
4766 goto_buffer(eap, DOBUF_MOD, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02004767 if (eap->do_ecmd_cmd != NULL)
4768 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004769}
4770
4771/*
4772 * :[N]bnext [N] to next buffer
4773 * :[N]sbnext [N] split and to next buffer
4774 */
4775 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004776ex_bnext(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004777{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01004778 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar3029bcc2020-01-18 15:06:19 +01004779 return;
4780
Bram Moolenaar071d4272004-06-13 20:20:40 +00004781 goto_buffer(eap, DOBUF_CURRENT, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02004782 if (eap->do_ecmd_cmd != NULL)
4783 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004784}
4785
4786/*
4787 * :[N]bNext [N] to previous buffer
4788 * :[N]bprevious [N] to previous buffer
4789 * :[N]sbNext [N] split and to previous buffer
4790 * :[N]sbprevious [N] split and to previous buffer
4791 */
4792 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004793ex_bprevious(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004794{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01004795 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar3029bcc2020-01-18 15:06:19 +01004796 return;
4797
Bram Moolenaar071d4272004-06-13 20:20:40 +00004798 goto_buffer(eap, DOBUF_CURRENT, BACKWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02004799 if (eap->do_ecmd_cmd != NULL)
4800 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004801}
4802
4803/*
4804 * :brewind to first buffer
4805 * :bfirst to first buffer
4806 * :sbrewind split and to first buffer
4807 * :sbfirst split and to first buffer
4808 */
4809 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004810ex_brewind(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004811{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01004812 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar3029bcc2020-01-18 15:06:19 +01004813 return;
4814
Bram Moolenaar071d4272004-06-13 20:20:40 +00004815 goto_buffer(eap, DOBUF_FIRST, FORWARD, 0);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02004816 if (eap->do_ecmd_cmd != NULL)
4817 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004818}
4819
4820/*
4821 * :blast to last buffer
4822 * :sblast split and to last buffer
4823 */
4824 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004825ex_blast(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004826{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01004827 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar3029bcc2020-01-18 15:06:19 +01004828 return;
4829
Bram Moolenaar071d4272004-06-13 20:20:40 +00004830 goto_buffer(eap, DOBUF_LAST, BACKWARD, 0);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02004831 if (eap->do_ecmd_cmd != NULL)
4832 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004833}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004834
Bram Moolenaar7a092242020-04-16 22:10:49 +02004835/*
4836 * Check if "c" ends an Ex command.
Bram Moolenaara494f562020-04-18 17:45:38 +02004837 * In Vim9 script does not check for white space before # or #{.
Bram Moolenaar7a092242020-04-16 22:10:49 +02004838 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004839 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004840ends_excmd(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004841{
Bram Moolenaarfbda69b2020-04-13 15:06:53 +02004842#ifdef FEAT_EVAL
Bram Moolenaar2c330432020-04-13 14:41:35 +02004843 if (c == '#')
Bram Moolenaar7a092242020-04-16 22:10:49 +02004844 return in_vim9script();
4845#endif
4846 return (c == NUL || c == '|' || c == '"' || c == '\n');
4847}
4848
4849/*
4850 * Like ends_excmd() but checks that a # in Vim9 script either has "cmd" equal
4851 * to "cmd_start" or has a white space character before it.
4852 */
4853 int
Bram Moolenaar21cfe502020-04-16 23:01:50 +02004854ends_excmd2(char_u *cmd_start UNUSED, char_u *cmd)
Bram Moolenaar7a092242020-04-16 22:10:49 +02004855{
4856 int c = *cmd;
4857
4858#ifdef FEAT_EVAL
Bram Moolenaara26b9702020-04-18 19:53:28 +02004859 if (c == '#' && cmd[1] != '{' && (cmd == cmd_start || VIM_ISWHITE(cmd[-1])))
Bram Moolenaar2c330432020-04-13 14:41:35 +02004860 return in_vim9script();
Bram Moolenaarfbda69b2020-04-13 15:06:53 +02004861#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004862 return (c == NUL || c == '|' || c == '"' || c == '\n');
4863}
4864
4865#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA) || defined(FEAT_EVAL) \
4866 || defined(PROTO)
4867/*
4868 * Return the next command, after the first '|' or '\n'.
4869 * Return NULL if not found.
4870 */
4871 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004872find_nextcmd(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004873{
4874 while (*p != '|' && *p != '\n')
4875 {
4876 if (*p == NUL)
4877 return NULL;
4878 ++p;
4879 }
4880 return (p + 1);
4881}
4882#endif
4883
4884/*
Bram Moolenaar2256c992016-11-15 21:17:07 +01004885 * Check if *p is a separator between Ex commands, skipping over white space.
4886 * Return NULL if it isn't, the following character if it is.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004887 */
4888 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004889check_nextcmd(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004890{
Bram Moolenaar2256c992016-11-15 21:17:07 +01004891 char_u *s = skipwhite(p);
4892
4893 if (*s == '|' || *s == '\n')
4894 return (s + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004895 else
4896 return NULL;
4897}
4898
4899/*
4900 * - if there are more files to edit
4901 * - and this is the last window
4902 * - and forceit not used
4903 * - and not repeated twice on a row
4904 * return FAIL and give error message if 'message' TRUE
4905 * return OK otherwise
4906 */
4907 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004908check_more(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004909 int message, // when FALSE check only, no messages
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004910 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004911{
4912 int n = ARGCOUNT - curwin->w_arg_idx - 1;
4913
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00004914 if (!forceit && only_one_window()
4915 && ARGCOUNT > 1 && !arg_had_last && n >= 0 && quitmore == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004916 {
4917 if (message)
4918 {
4919#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
4920 if ((p_confirm || cmdmod.confirm) && curbuf->b_fname != NULL)
4921 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02004922 char_u buff[DIALOG_MSG_SIZE];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004923
Bram Moolenaarda6e8912018-08-21 15:12:14 +02004924 vim_snprintf((char *)buff, DIALOG_MSG_SIZE,
4925 NGETTEXT("%d more file to edit. Quit anyway?",
4926 "%d more files to edit. Quit anyway?", n), n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004927 if (vim_dialog_yesno(VIM_QUESTION, NULL, buff, 1) == VIM_YES)
4928 return OK;
4929 return FAIL;
4930 }
4931#endif
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01004932 semsg(NGETTEXT("E173: %d more file to edit",
4933 "E173: %d more files to edit", n), n);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004934 quitmore = 2; // next try to quit is allowed
Bram Moolenaar071d4272004-06-13 20:20:40 +00004935 }
4936 return FAIL;
4937 }
4938 return OK;
4939}
4940
Bram Moolenaar071d4272004-06-13 20:20:40 +00004941/*
4942 * Function given to ExpandGeneric() to obtain the list of command names.
4943 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004944 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004945get_command_name(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004946{
4947 if (idx >= (int)CMD_SIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004948 return get_user_command_name(idx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004949 return cmdnames[idx].cmd_name;
4950}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004951
Bram Moolenaar071d4272004-06-13 20:20:40 +00004952 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004953ex_colorscheme(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004954{
Bram Moolenaare6850792010-05-14 15:28:44 +02004955 if (*eap->arg == NUL)
4956 {
4957#ifdef FEAT_EVAL
4958 char_u *expr = vim_strsave((char_u *)"g:colors_name");
4959 char_u *p = NULL;
4960
4961 if (expr != NULL)
4962 {
4963 ++emsg_off;
Bram Moolenaarb171fb12020-06-24 20:34:03 +02004964 p = eval_to_string(expr, FALSE);
Bram Moolenaare6850792010-05-14 15:28:44 +02004965 --emsg_off;
4966 vim_free(expr);
4967 }
4968 if (p != NULL)
4969 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01004970 msg((char *)p);
Bram Moolenaare6850792010-05-14 15:28:44 +02004971 vim_free(p);
4972 }
4973 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01004974 msg("default");
Bram Moolenaare6850792010-05-14 15:28:44 +02004975#else
Bram Moolenaar32526b32019-01-19 17:43:09 +01004976 msg(_("unknown"));
Bram Moolenaare6850792010-05-14 15:28:44 +02004977#endif
4978 }
4979 else if (load_colors(eap->arg) == FAIL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004980 semsg(_("E185: Cannot find color scheme '%s'"), eap->arg);
Bram Moolenaarf58d81a2019-01-28 20:19:05 +01004981
4982#ifdef FEAT_VTP
4983 else if (has_vtp_working())
4984 {
4985 // background color change requires clear + redraw
4986 update_screen(CLEAR);
4987 redrawcmd();
4988 }
4989#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004990}
4991
4992 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004993ex_highlight(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004994{
4995 if (*eap->arg == NUL && eap->cmd[2] == '!')
Bram Moolenaar32526b32019-01-19 17:43:09 +01004996 msg(_("Greetings, Vim user!"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004997 do_highlight(eap->arg, eap->forceit, FALSE);
4998}
4999
5000
5001/*
5002 * Call this function if we thought we were going to exit, but we won't
5003 * (because of an error). May need to restore the terminal mode.
5004 */
5005 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005006not_exiting(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005007{
5008 exiting = FALSE;
5009 settmode(TMODE_RAW);
5010}
5011
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005012 static int
5013before_quit_autocmds(win_T *wp, int quit_all, int forceit)
5014{
5015 apply_autocmds(EVENT_QUITPRE, NULL, NULL, FALSE, wp->w_buffer);
5016
Bram Moolenaar34ba06b2019-10-20 22:27:10 +02005017 // Bail out when autocommands closed the window.
5018 // Refuse to quit when the buffer in the last window is being closed (can
5019 // only happen in autocommands).
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005020 if (!win_valid(wp)
5021 || curbuf_locked()
5022 || (wp->w_buffer->b_nwindows == 1 && wp->w_buffer->b_locked > 0))
5023 return TRUE;
5024
5025 if (quit_all || (check_more(FALSE, forceit) == OK && only_one_window()))
5026 {
5027 apply_autocmds(EVENT_EXITPRE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar34ba06b2019-10-20 22:27:10 +02005028 // Refuse to quit when locked or when the window was closed or the
5029 // buffer in the last window is being closed (can only happen in
5030 // autocommands).
5031 if (!win_valid(wp) || curbuf_locked()
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005032 || (curbuf->b_nwindows == 1 && curbuf->b_locked > 0))
5033 return TRUE;
5034 }
5035
5036 return FALSE;
5037}
5038
Bram Moolenaar071d4272004-06-13 20:20:40 +00005039/*
Bram Moolenaarf240e182014-11-27 18:33:02 +01005040 * ":quit": quit current window, quit Vim if the last window is closed.
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005041 * ":{nr}quit": quit window {nr}
Bram Moolenaar4d14bac2019-10-20 21:15:15 +02005042 * Also used when closing a terminal window that's the last one.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005043 */
Bram Moolenaar4d14bac2019-10-20 21:15:15 +02005044 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005045ex_quit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005046{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005047 win_T *wp;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005048
Bram Moolenaar071d4272004-06-13 20:20:40 +00005049#ifdef FEAT_CMDWIN
5050 if (cmdwin_type != 0)
5051 {
5052 cmdwin_result = Ctrl_C;
5053 return;
5054 }
5055#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005056 // Don't quit while editing the command line.
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005057 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005058 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005059 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005060 return;
5061 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005062 if (eap->addr_count > 0)
5063 {
Bram Moolenaarf240e182014-11-27 18:33:02 +01005064 int wnr = eap->line2;
5065
5066 for (wp = firstwin; wp->w_next != NULL; wp = wp->w_next)
5067 if (--wnr <= 0)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005068 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005069 }
5070 else
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005071 wp = curwin;
Bram Moolenaarf240e182014-11-27 18:33:02 +01005072
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005073 // Refuse to quit when locked.
Bram Moolenaar87ffb5c2017-10-19 12:37:42 +02005074 if (curbuf_locked())
5075 return;
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005076
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005077 // Trigger QuitPre and maybe ExitPre
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005078 if (before_quit_autocmds(wp, FALSE, eap->forceit))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005079 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005080
5081#ifdef FEAT_NETBEANS_INTG
5082 netbeansForcedQuit = eap->forceit;
5083#endif
5084
5085 /*
5086 * If there are more files or windows we won't exit.
5087 */
5088 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
5089 exiting = TRUE;
Bram Moolenaarff930ca2017-10-19 17:12:10 +02005090 if ((!buf_hide(wp->w_buffer)
5091 && check_changed(wp->w_buffer, (p_awa ? CCGD_AW : 0)
Bram Moolenaar45d3b142013-11-09 03:31:51 +01005092 | (eap->forceit ? CCGD_FORCEIT : 0)
5093 | CCGD_EXCMD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005094 || check_more(TRUE, eap->forceit) == FAIL
Bram Moolenaar027387f2016-01-02 22:25:52 +01005095 || (only_one_window() && check_changed_any(eap->forceit, TRUE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005096 {
5097 not_exiting();
5098 }
5099 else
5100 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005101 // quit last window
5102 // Note: only_one_window() returns true, even so a help window is
5103 // still open. In that case only quit, if no address has been
5104 // specified. Example:
5105 // :h|wincmd w|1q - don't quit
5106 // :h|wincmd w|q - quit
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01005107 if (only_one_window() && (ONE_WINDOW || eap->addr_count == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005108 getout(0);
Bram Moolenaar2c33d7b2017-10-14 16:06:20 +02005109 not_exiting();
Bram Moolenaar4033c552017-09-16 20:54:51 +02005110#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00005111 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005112#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005113 // close window; may free buffer
Bram Moolenaareb44a682017-08-03 22:44:55 +02005114 win_close(wp, !buf_hide(wp->w_buffer) || eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005115 }
5116}
5117
5118/*
5119 * ":cquit".
5120 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005121 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005122ex_cquit(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005123{
Bram Moolenaar1860bde2020-01-06 21:47:21 +01005124 // this does not always pass on the exit code to the Manx compiler. why?
5125 getout(eap->addr_count > 0 ? (int)eap->line2 : EXIT_FAILURE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005126}
5127
5128/*
5129 * ":qall": try to quit all windows
5130 */
5131 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005132ex_quit_all(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005133{
5134# ifdef FEAT_CMDWIN
5135 if (cmdwin_type != 0)
5136 {
5137 if (eap->forceit)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005138 cmdwin_result = K_XF1; // ex_window() takes care of this
Bram Moolenaar071d4272004-06-13 20:20:40 +00005139 else
5140 cmdwin_result = K_XF2;
5141 return;
5142 }
5143# endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005144
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005145 // Don't quit while editing the command line.
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005146 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005147 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005148 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005149 return;
5150 }
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005151
5152 if (before_quit_autocmds(curwin, TRUE, eap->forceit))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005153 return;
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005154
Bram Moolenaar071d4272004-06-13 20:20:40 +00005155 exiting = TRUE;
Bram Moolenaar027387f2016-01-02 22:25:52 +01005156 if (eap->forceit || !check_changed_any(FALSE, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005157 getout(0);
5158 not_exiting();
5159}
5160
Bram Moolenaar071d4272004-06-13 20:20:40 +00005161/*
5162 * ":close": close current window, unless it is the last one
5163 */
5164 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005165ex_close(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005166{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005167 win_T *win;
5168 int winnr = 0;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005169#ifdef FEAT_CMDWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00005170 if (cmdwin_type != 0)
Bram Moolenaar9bd1a7e2011-05-19 14:50:54 +02005171 cmdwin_result = Ctrl_C;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005172 else
Bram Moolenaar4033c552017-09-16 20:54:51 +02005173#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005174 if (!text_locked() && !curbuf_locked())
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005175 {
5176 if (eap->addr_count == 0)
5177 ex_win_close(eap->forceit, curwin, NULL);
Bram Moolenaar6d41c782018-06-06 09:11:12 +02005178 else
5179 {
Bram Moolenaar29323592016-07-24 22:04:11 +02005180 FOR_ALL_WINDOWS(win)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005181 {
5182 winnr++;
5183 if (winnr == eap->line2)
5184 break;
5185 }
5186 if (win == NULL)
5187 win = lastwin;
5188 ex_win_close(eap->forceit, win, NULL);
5189 }
5190 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005191}
5192
Bram Moolenaar4033c552017-09-16 20:54:51 +02005193#ifdef FEAT_QUICKFIX
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005194/*
5195 * ":pclose": Close any preview window.
5196 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005197 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005198ex_pclose(exarg_T *eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005199{
5200 win_T *win;
5201
Bram Moolenaar79648732019-07-18 21:43:07 +02005202 // First close any normal window.
Bram Moolenaar29323592016-07-24 22:04:11 +02005203 FOR_ALL_WINDOWS(win)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005204 if (win->w_p_pvw)
5205 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00005206 ex_win_close(eap->forceit, win, NULL);
Bram Moolenaar79648732019-07-18 21:43:07 +02005207 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005208 }
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01005209# ifdef FEAT_PROP_POPUP
Bram Moolenaar79648732019-07-18 21:43:07 +02005210 // Also when 'previewpopup' is empty, it might have been cleared.
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02005211 popup_close_preview();
Bram Moolenaar79648732019-07-18 21:43:07 +02005212# endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005213}
Bram Moolenaar4033c552017-09-16 20:54:51 +02005214#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005215
Bram Moolenaarf740b292006-02-16 22:11:02 +00005216/*
5217 * Close window "win" and take care of handling closing the last window for a
5218 * modified buffer.
5219 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005220 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005221ex_win_close(
5222 int forceit,
5223 win_T *win,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005224 tabpage_T *tp) // NULL or the tab page "win" is in
Bram Moolenaar071d4272004-06-13 20:20:40 +00005225{
5226 int need_hide;
5227 buf_T *buf = win->w_buffer;
5228
Bram Moolenaarcf844172020-06-26 19:44:06 +02005229 // Never close the autocommand window.
5230 if (win == aucmd_win)
5231 {
5232 emsg(_(e_autocmd_close));
5233 return;
5234 }
5235
Bram Moolenaar071d4272004-06-13 20:20:40 +00005236 need_hide = (bufIsChanged(buf) && buf->b_nwindows <= 1);
Bram Moolenaareb44a682017-08-03 22:44:55 +02005237 if (need_hide && !buf_hide(buf) && !forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005238 {
Bram Moolenaar4033c552017-09-16 20:54:51 +02005239#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005240 if ((p_confirm || cmdmod.confirm) && p_write)
5241 {
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005242 bufref_T bufref;
5243
5244 set_bufref(&bufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005245 dialog_changed(buf, FALSE);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005246 if (bufref_valid(&bufref) && bufIsChanged(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005247 return;
5248 need_hide = FALSE;
5249 }
5250 else
Bram Moolenaar4033c552017-09-16 20:54:51 +02005251#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005252 {
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02005253 no_write_message();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005254 return;
5255 }
5256 }
5257
Bram Moolenaar4033c552017-09-16 20:54:51 +02005258#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00005259 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005260#endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00005261
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005262 // free buffer when not hiding it or when it's a scratch buffer
Bram Moolenaarf740b292006-02-16 22:11:02 +00005263 if (tp == NULL)
Bram Moolenaareb44a682017-08-03 22:44:55 +02005264 win_close(win, !need_hide && !buf_hide(buf));
Bram Moolenaarf740b292006-02-16 22:11:02 +00005265 else
Bram Moolenaareb44a682017-08-03 22:44:55 +02005266 win_close_othertab(win, !need_hide && !buf_hide(buf), tp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005267}
5268
Bram Moolenaar071d4272004-06-13 20:20:40 +00005269/*
Bram Moolenaardea25702017-01-29 15:18:10 +01005270 * Handle the argument for a tabpage related ex command.
5271 * Returns a tabpage number.
5272 * When an error is encountered then eap->errmsg is set.
5273 */
5274 static int
5275get_tabpage_arg(exarg_T *eap)
5276{
5277 int tab_number;
5278 int unaccept_arg0 = (eap->cmdidx == CMD_tabmove) ? 0 : 1;
5279
5280 if (eap->arg && *eap->arg != NUL)
5281 {
5282 char_u *p = eap->arg;
5283 char_u *p_save;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005284 int relative = 0; // argument +N/-N means: go to N places to the
5285 // right/left relative to the current position.
Bram Moolenaardea25702017-01-29 15:18:10 +01005286
5287 if (*p == '-')
5288 {
5289 relative = -1;
5290 p++;
5291 }
5292 else if (*p == '+')
5293 {
5294 relative = 1;
5295 p++;
5296 }
5297
5298 p_save = p;
5299 tab_number = getdigits(&p);
5300
5301 if (relative == 0)
5302 {
5303 if (STRCMP(p, "$") == 0)
5304 tab_number = LAST_TAB_NR;
5305 else if (p == p_save || *p_save == '-' || *p != NUL
5306 || tab_number > LAST_TAB_NR)
5307 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005308 // No numbers as argument.
Bram Moolenaardea25702017-01-29 15:18:10 +01005309 eap->errmsg = e_invarg;
5310 goto theend;
5311 }
5312 }
5313 else
5314 {
5315 if (*p_save == NUL)
5316 tab_number = 1;
5317 else if (p == p_save || *p_save == '-' || *p != NUL
5318 || tab_number == 0)
5319 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005320 // No numbers as argument.
Bram Moolenaardea25702017-01-29 15:18:10 +01005321 eap->errmsg = e_invarg;
5322 goto theend;
5323 }
5324 tab_number = tab_number * relative + tabpage_index(curtab);
5325 if (!unaccept_arg0 && relative == -1)
5326 --tab_number;
5327 }
5328 if (tab_number < unaccept_arg0 || tab_number > LAST_TAB_NR)
5329 eap->errmsg = e_invarg;
5330 }
5331 else if (eap->addr_count > 0)
5332 {
5333 if (unaccept_arg0 && eap->line2 == 0)
Bram Moolenaarc6251552017-01-29 21:42:20 +01005334 {
Bram Moolenaardea25702017-01-29 15:18:10 +01005335 eap->errmsg = e_invrange;
Bram Moolenaarc6251552017-01-29 21:42:20 +01005336 tab_number = 0;
5337 }
Bram Moolenaardea25702017-01-29 15:18:10 +01005338 else
5339 {
5340 tab_number = eap->line2;
Bram Moolenaar82a12462019-01-22 22:41:42 +01005341 if (!unaccept_arg0 && *skipwhite(*eap->cmdlinep) == '-')
Bram Moolenaardea25702017-01-29 15:18:10 +01005342 {
5343 --tab_number;
5344 if (tab_number < unaccept_arg0)
5345 eap->errmsg = e_invarg;
5346 }
5347 }
5348 }
5349 else
5350 {
5351 switch (eap->cmdidx)
5352 {
5353 case CMD_tabnext:
5354 tab_number = tabpage_index(curtab) + 1;
5355 if (tab_number > LAST_TAB_NR)
5356 tab_number = 1;
5357 break;
5358 case CMD_tabmove:
5359 tab_number = LAST_TAB_NR;
5360 break;
5361 default:
5362 tab_number = tabpage_index(curtab);
5363 }
5364 }
5365
5366theend:
5367 return tab_number;
5368}
5369
5370/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00005371 * ":tabclose": close current tab page, unless it is the last one.
5372 * ":tabclose N": close tab page N.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005373 */
5374 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005375ex_tabclose(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005376{
Bram Moolenaarf740b292006-02-16 22:11:02 +00005377 tabpage_T *tp;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005378 int tab_number;
Bram Moolenaarf740b292006-02-16 22:11:02 +00005379
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005380# ifdef FEAT_CMDWIN
5381 if (cmdwin_type != 0)
5382 cmdwin_result = K_IGNORE;
5383 else
5384# endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00005385 if (first_tabpage->tp_next == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005386 emsg(_("E784: Cannot close last tab page"));
Bram Moolenaarf740b292006-02-16 22:11:02 +00005387 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005388 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005389 tab_number = get_tabpage_arg(eap);
5390 if (eap->errmsg == NULL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005391 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005392 tp = find_tabpage(tab_number);
Bram Moolenaarf740b292006-02-16 22:11:02 +00005393 if (tp == NULL)
5394 {
5395 beep_flush();
5396 return;
5397 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005398 if (tp != curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00005399 {
5400 tabpage_close_other(tp, eap->forceit);
5401 return;
5402 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005403 else if (!text_locked() && !curbuf_locked())
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005404 tabpage_close(eap->forceit);
5405 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005406 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005407}
5408
5409/*
5410 * ":tabonly": close all tab pages except the current one
5411 */
5412 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005413ex_tabonly(exarg_T *eap)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005414{
5415 tabpage_T *tp;
5416 int done;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005417 int tab_number;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005418
5419# ifdef FEAT_CMDWIN
5420 if (cmdwin_type != 0)
5421 cmdwin_result = K_IGNORE;
5422 else
5423# endif
5424 if (first_tabpage->tp_next == NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01005425 msg(_("Already only one tab page"));
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005426 else
5427 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005428 tab_number = get_tabpage_arg(eap);
5429 if (eap->errmsg == NULL)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005430 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005431 goto_tabpage(tab_number);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005432 // Repeat this up to a 1000 times, because autocommands may
5433 // mess up the lists.
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005434 for (done = 0; done < 1000; ++done)
5435 {
5436 FOR_ALL_TABPAGES(tp)
5437 if (tp->tp_topframe != topframe)
5438 {
5439 tabpage_close_other(tp, eap->forceit);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005440 // if we failed to close it quit
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005441 if (valid_tabpage(tp))
5442 done = 1000;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005443 // start over, "tp" is now invalid
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005444 break;
5445 }
5446 if (first_tabpage->tp_next == NULL)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005447 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005448 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005449 }
5450 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005451}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005452
5453/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00005454 * Close the current tab page.
5455 */
5456 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005457tabpage_close(int forceit)
Bram Moolenaarf740b292006-02-16 22:11:02 +00005458{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005459 // First close all the windows but the current one. If that worked then
5460 // close the last window in this tab, that will close it.
Bram Moolenaar459ca562016-11-10 18:16:33 +01005461 if (!ONE_WINDOW)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005462 close_others(TRUE, forceit);
Bram Moolenaar459ca562016-11-10 18:16:33 +01005463 if (ONE_WINDOW)
Bram Moolenaarf740b292006-02-16 22:11:02 +00005464 ex_win_close(forceit, curwin, NULL);
5465# ifdef FEAT_GUI
5466 need_mouse_correct = TRUE;
5467# endif
5468}
5469
5470/*
5471 * Close tab page "tp", which is not the current tab page.
5472 * Note that autocommands may make "tp" invalid.
Bram Moolenaar7875acc2006-09-10 13:51:17 +00005473 * Also takes care of the tab pages line disappearing when closing the
5474 * last-but-one tab page.
Bram Moolenaarf740b292006-02-16 22:11:02 +00005475 */
5476 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005477tabpage_close_other(tabpage_T *tp, int forceit)
Bram Moolenaarf740b292006-02-16 22:11:02 +00005478{
5479 int done = 0;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005480 win_T *wp;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00005481 int h = tabline_height();
Bram Moolenaarf740b292006-02-16 22:11:02 +00005482
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005483 // Limit to 1000 windows, autocommands may add a window while we close
5484 // one. OK, so I'm paranoid...
Bram Moolenaarf740b292006-02-16 22:11:02 +00005485 while (++done < 1000)
5486 {
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005487 wp = tp->tp_firstwin;
5488 ex_win_close(forceit, wp, tp);
Bram Moolenaarf740b292006-02-16 22:11:02 +00005489
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005490 // Autocommands may delete the tab page under our fingers and we may
5491 // fail to close a window with a modified buffer.
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005492 if (!valid_tabpage(tp) || tp->tp_firstwin == wp)
Bram Moolenaarf740b292006-02-16 22:11:02 +00005493 break;
5494 }
Bram Moolenaar7875acc2006-09-10 13:51:17 +00005495
Bram Moolenaar29323592016-07-24 22:04:11 +02005496 apply_autocmds(EVENT_TABCLOSED, NULL, NULL, FALSE, curbuf);
Bram Moolenaar29323592016-07-24 22:04:11 +02005497
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005498 redraw_tabline = TRUE;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00005499 if (h != tabline_height())
5500 shell_new_rows();
Bram Moolenaarf740b292006-02-16 22:11:02 +00005501}
5502
5503/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005504 * ":only".
5505 */
5506 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005507ex_only(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005508{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005509 win_T *wp;
5510 int wnr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005511# ifdef FEAT_GUI
5512 need_mouse_correct = TRUE;
5513# endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005514 if (eap->addr_count > 0)
5515 {
5516 wnr = eap->line2;
5517 for (wp = firstwin; --wnr > 0; )
5518 {
5519 if (wp->w_next == NULL)
5520 break;
5521 else
5522 wp = wp->w_next;
5523 }
5524 win_goto(wp);
5525 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005526 close_others(TRUE, eap->forceit);
5527}
5528
Bram Moolenaar071d4272004-06-13 20:20:40 +00005529 static void
Bram Moolenaar5e1e6d22017-01-02 17:26:00 +01005530ex_hide(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005531{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005532 // ":hide" or ":hide | cmd": hide current window
Bram Moolenaar2256c992016-11-15 21:17:07 +01005533 if (!eap->skip)
5534 {
Bram Moolenaar4033c552017-09-16 20:54:51 +02005535#ifdef FEAT_GUI
Bram Moolenaar2256c992016-11-15 21:17:07 +01005536 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005537#endif
Bram Moolenaar2256c992016-11-15 21:17:07 +01005538 if (eap->addr_count == 0)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005539 win_close(curwin, FALSE); // don't free buffer
Bram Moolenaar2256c992016-11-15 21:17:07 +01005540 else
5541 {
5542 int winnr = 0;
5543 win_T *win;
Bram Moolenaarf240e182014-11-27 18:33:02 +01005544
Bram Moolenaar2256c992016-11-15 21:17:07 +01005545 FOR_ALL_WINDOWS(win)
5546 {
5547 winnr++;
5548 if (winnr == eap->line2)
5549 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005550 }
Bram Moolenaar2256c992016-11-15 21:17:07 +01005551 if (win == NULL)
5552 win = lastwin;
5553 win_close(win, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005554 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005555 }
5556}
5557
5558/*
5559 * ":stop" and ":suspend": Suspend Vim.
5560 */
5561 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005562ex_stop(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005563{
5564 /*
5565 * Disallow suspending for "rvim".
5566 */
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005567 if (!check_restricted())
Bram Moolenaar071d4272004-06-13 20:20:40 +00005568 {
5569 if (!eap->forceit)
5570 autowrite_all();
5571 windgoto((int)Rows - 1, 0);
5572 out_char('\n');
5573 out_flush();
5574 stoptermcap();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005575 out_flush(); // needed for SUN to restore xterm buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00005576#ifdef FEAT_TITLE
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005577 mch_restore_title(SAVE_RESTORE_BOTH); // restore window titles
Bram Moolenaar071d4272004-06-13 20:20:40 +00005578#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005579 ui_suspend(); // call machine specific function
Bram Moolenaar071d4272004-06-13 20:20:40 +00005580#ifdef FEAT_TITLE
5581 maketitle();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005582 resettitle(); // force updating the title
Bram Moolenaar071d4272004-06-13 20:20:40 +00005583#endif
5584 starttermcap();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005585 scroll_start(); // scroll screen before redrawing
Bram Moolenaar071d4272004-06-13 20:20:40 +00005586 redraw_later_clear();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005587 shell_resized(); // may have resized window
Bram Moolenaar071d4272004-06-13 20:20:40 +00005588 }
5589}
5590
5591/*
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005592 * ":exit", ":xit" and ":wq": Write file and quite the current window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005593 */
5594 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005595ex_exit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005596{
5597#ifdef FEAT_CMDWIN
5598 if (cmdwin_type != 0)
5599 {
5600 cmdwin_result = Ctrl_C;
5601 return;
5602 }
5603#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005604 // Don't quit while editing the command line.
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005605 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005606 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005607 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005608 return;
5609 }
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005610
5611 if (before_quit_autocmds(curwin, FALSE, eap->forceit))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005612 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005613
5614 /*
5615 * if more files or windows we won't exit
5616 */
5617 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
5618 exiting = TRUE;
5619 if ( ((eap->cmdidx == CMD_wq
5620 || curbufIsChanged())
5621 && do_write(eap) == FAIL)
5622 || check_more(TRUE, eap->forceit) == FAIL
Bram Moolenaar027387f2016-01-02 22:25:52 +01005623 || (only_one_window() && check_changed_any(eap->forceit, FALSE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005624 {
5625 not_exiting();
5626 }
5627 else
5628 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005629 if (only_one_window()) // quit last window, exit Vim
Bram Moolenaar071d4272004-06-13 20:20:40 +00005630 getout(0);
Bram Moolenaar2c33d7b2017-10-14 16:06:20 +02005631 not_exiting();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005632# ifdef FEAT_GUI
5633 need_mouse_correct = TRUE;
5634# endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005635 // Quit current window, may free the buffer.
Bram Moolenaareb44a682017-08-03 22:44:55 +02005636 win_close(curwin, !buf_hide(curwin->w_buffer));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005637 }
5638}
5639
5640/*
5641 * ":print", ":list", ":number".
5642 */
5643 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005644ex_print(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005645{
Bram Moolenaardf177f62005-02-22 08:39:57 +00005646 if (curbuf->b_ml.ml_flags & ML_EMPTY)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005647 emsg(_(e_emptybuf));
Bram Moolenaardf177f62005-02-22 08:39:57 +00005648 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005649 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00005650 for ( ;!got_int; ui_breakcheck())
5651 {
5652 print_line(eap->line1,
5653 (eap->cmdidx == CMD_number || eap->cmdidx == CMD_pound
5654 || (eap->flags & EXFLAG_NR)),
5655 eap->cmdidx == CMD_list || (eap->flags & EXFLAG_LIST));
5656 if (++eap->line1 > eap->line2)
5657 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005658 out_flush(); // show one line at a time
Bram Moolenaardf177f62005-02-22 08:39:57 +00005659 }
5660 setpcmark();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005661 // put cursor at last line
Bram Moolenaardf177f62005-02-22 08:39:57 +00005662 curwin->w_cursor.lnum = eap->line2;
5663 beginline(BL_SOL | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005664 }
5665
Bram Moolenaar071d4272004-06-13 20:20:40 +00005666 ex_no_reprint = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005667}
5668
5669#ifdef FEAT_BYTEOFF
5670 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005671ex_goto(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005672{
5673 goto_byte(eap->line2);
5674}
5675#endif
5676
5677/*
5678 * ":shell".
5679 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005680 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005681ex_shell(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005682{
5683 do_shell(NULL, 0);
5684}
5685
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005686#if defined(HAVE_DROP_FILE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005687
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005688static int drop_busy = FALSE;
5689static int drop_filec;
5690static char_u **drop_filev = NULL;
5691static int drop_split;
5692static void (*drop_callback)(void *);
5693static void *drop_cookie;
5694
5695 static void
5696handle_drop_internal(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005697{
5698 exarg_T ea;
5699 int save_msg_scroll = msg_scroll;
5700
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005701 // Setting the argument list may cause screen updates and being called
5702 // recursively. Avoid that by setting drop_busy.
5703 drop_busy = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005704
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005705 // Check whether the current buffer is changed. If so, we will need
5706 // to split the current window or data could be lost.
5707 // We don't need to check if the 'hidden' option is set, as in this
5708 // case the buffer won't be lost.
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005709 if (!buf_hide(curbuf) && !drop_split)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005710 {
5711 ++emsg_off;
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005712 drop_split = check_changed(curbuf, CCGD_AW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005713 --emsg_off;
5714 }
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005715 if (drop_split)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005716 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005717 if (win_split(0, 0) == FAIL)
5718 return;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02005719 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005720
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005721 // When splitting the window, create a new alist. Otherwise the
5722 // existing one is overwritten.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005723 alist_unlink(curwin->w_alist);
5724 alist_new();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005725 }
5726
5727 /*
5728 * Set up the new argument list.
5729 */
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005730 alist_set(ALIST(curwin), drop_filec, drop_filev, FALSE, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005731
5732 /*
5733 * Move to the first file.
5734 */
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005735 // Fake up a minimal "next" command for do_argfile()
Bram Moolenaara80faa82020-04-12 19:37:17 +02005736 CLEAR_FIELD(ea);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005737 ea.cmd = (char_u *)"next";
5738 do_argfile(&ea, 0);
5739
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005740 // do_ecmd() may set need_start_insertmode, but since we never left Insert
5741 // mode that is not needed here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005742 need_start_insertmode = FALSE;
5743
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005744 // Restore msg_scroll, otherwise a following command may cause scrolling
5745 // unexpectedly. The screen will be redrawn by the caller, thus
5746 // msg_scroll being set by displaying a message is irrelevant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005747 msg_scroll = save_msg_scroll;
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005748
5749 if (drop_callback != NULL)
5750 drop_callback(drop_cookie);
5751
5752 drop_filev = NULL;
5753 drop_busy = FALSE;
5754}
5755
5756/*
5757 * Handle a file drop. The code is here because a drop is *nearly* like an
5758 * :args command, but not quite (we have a list of exact filenames, so we
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01005759 * don't want to (a) parse a command line, or (b) expand wildcards). So the
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005760 * code is very similar to :args and hence needs access to a lot of the static
5761 * functions in this file.
5762 *
5763 * The "filev" list must have been allocated using alloc(), as should each item
5764 * in the list. This function takes over responsibility for freeing the "filev"
5765 * list.
5766 */
5767 void
5768handle_drop(
5769 int filec, // the number of files dropped
5770 char_u **filev, // the list of files dropped
5771 int split, // force splitting the window
5772 void (*callback)(void *), // to be called after setting the argument
5773 // list
5774 void *cookie) // argument for "callback" (allocated)
5775{
5776 // Cannot handle recursive drops, finish the pending one.
5777 if (drop_busy)
5778 {
5779 FreeWild(filec, filev);
5780 vim_free(cookie);
5781 return;
5782 }
5783
5784 // When calling handle_drop() more than once in a row we only use the last
5785 // one.
5786 if (drop_filev != NULL)
5787 {
5788 FreeWild(drop_filec, drop_filev);
5789 vim_free(drop_cookie);
5790 }
5791
5792 drop_filec = filec;
5793 drop_filev = filev;
5794 drop_split = split;
5795 drop_callback = callback;
5796 drop_cookie = cookie;
5797
5798 // Postpone this when:
5799 // - editing the command line
5800 // - not possible to change the current buffer
5801 // - updating the screen
5802 // As it may change buffers and window structures that are in use and cause
5803 // freed memory to be used.
5804 if (text_locked() || curbuf_locked() || updating_screen)
5805 return;
5806
5807 handle_drop_internal();
5808}
5809
5810/*
5811 * To be called when text is unlocked, curbuf is unlocked or updating_screen is
5812 * reset: Handle a postponed drop.
5813 */
5814 void
5815handle_any_postponed_drop(void)
5816{
5817 if (!drop_busy && drop_filev != NULL
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +02005818 && !text_locked() && !curbuf_locked() && !updating_screen)
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005819 handle_drop_internal();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005820}
5821#endif
5822
Bram Moolenaar071d4272004-06-13 20:20:40 +00005823/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005824 * ":preserve".
5825 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005826 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005827ex_preserve(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005828{
Bram Moolenaar4399ef42005-02-12 14:29:27 +00005829 curbuf->b_flags |= BF_PRESERVED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005830 ml_preserve(curbuf, TRUE);
5831}
5832
5833/*
5834 * ":recover".
5835 */
5836 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005837ex_recover(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005838{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005839 // Set recoverymode right away to avoid the ATTENTION prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005840 recoverymode = TRUE;
Bram Moolenaar45d3b142013-11-09 03:31:51 +01005841 if (!check_changed(curbuf, (p_awa ? CCGD_AW : 0)
5842 | CCGD_MULTWIN
5843 | (eap->forceit ? CCGD_FORCEIT : 0)
5844 | CCGD_EXCMD)
5845
Bram Moolenaar071d4272004-06-13 20:20:40 +00005846 && (*eap->arg == NUL
5847 || setfname(curbuf, eap->arg, NULL, TRUE) == OK))
Bram Moolenaar99499b12019-05-23 21:35:48 +02005848 ml_recover(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005849 recoverymode = FALSE;
5850}
5851
5852/*
5853 * Command modifier used in a wrong way.
5854 */
5855 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005856ex_wrongmodifier(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005857{
5858 eap->errmsg = e_invcmd;
5859}
5860
Bram Moolenaar071d4272004-06-13 20:20:40 +00005861/*
5862 * :sview [+command] file split window with new file, read-only
5863 * :split [[+command] file] split window with current or new file
5864 * :vsplit [[+command] file] split window vertically with current or new file
5865 * :new [[+command] file] split window with no or new file
5866 * :vnew [[+command] file] split vertically window with no or new file
5867 * :sfind [+command] file split window with file in 'path'
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005868 *
5869 * :tabedit open new Tab page with empty window
5870 * :tabedit [+command] file open new Tab page and edit "file"
5871 * :tabnew [[+command] file] just like :tabedit
5872 * :tabfind [+command] file open new Tab page and find "file"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005873 */
5874 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005875ex_splitview(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005876{
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005877 win_T *old_curwin = curwin;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005878#if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005879 char_u *fname = NULL;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005880#endif
5881#ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00005882 int browse_flag = cmdmod.browse;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005883#endif
Bram Moolenaar39902a02018-06-21 22:10:08 +02005884 int use_tab = eap->cmdidx == CMD_tabedit
5885 || eap->cmdidx == CMD_tabfind
5886 || eap->cmdidx == CMD_tabnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005887
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01005888 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02005889 return;
5890
Bram Moolenaar4033c552017-09-16 20:54:51 +02005891#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00005892 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005893#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005894
Bram Moolenaar4033c552017-09-16 20:54:51 +02005895#ifdef FEAT_QUICKFIX
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005896 // A ":split" in the quickfix window works like ":new". Don't want two
5897 // quickfix windows. But it's OK when doing ":tab split".
Bram Moolenaar05bb9532008-07-04 09:44:11 +00005898 if (bt_quickfix(curbuf) && cmdmod.tab == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005899 {
5900 if (eap->cmdidx == CMD_split)
5901 eap->cmdidx = CMD_new;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005902 if (eap->cmdidx == CMD_vsplit)
5903 eap->cmdidx = CMD_vnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005904 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02005905#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005906
Bram Moolenaar4033c552017-09-16 20:54:51 +02005907#ifdef FEAT_SEARCHPATH
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005908 if (eap->cmdidx == CMD_sfind || eap->cmdidx == CMD_tabfind)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005909 {
5910 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg),
5911 FNAME_MESS, TRUE, curbuf->b_ffname);
5912 if (fname == NULL)
5913 goto theend;
5914 eap->arg = fname;
5915 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005916# ifdef FEAT_BROWSE
Bram Moolenaar4033c552017-09-16 20:54:51 +02005917 else
5918# endif
5919#endif
5920#ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00005921 if (cmdmod.browse
Bram Moolenaar071d4272004-06-13 20:20:40 +00005922 && eap->cmdidx != CMD_vnew
Bram Moolenaar071d4272004-06-13 20:20:40 +00005923 && eap->cmdidx != CMD_new)
5924 {
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00005925 if (
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005926# ifdef FEAT_GUI
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00005927 !gui.in_use &&
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005928# endif
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00005929 au_has_group((char_u *)"FileExplorer"))
5930 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005931 // No browsing supported but we do have the file explorer:
5932 // Edit the directory.
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00005933 if (*eap->arg == NUL || !mch_isdir(eap->arg))
5934 eap->arg = (char_u *)".";
5935 }
5936 else
5937 {
Bram Moolenaar39902a02018-06-21 22:10:08 +02005938 fname = do_browse(0, (char_u *)(use_tab
5939 ? _("Edit File in new tab page")
5940 : _("Edit File in new window")),
Bram Moolenaar071d4272004-06-13 20:20:40 +00005941 eap->arg, NULL, NULL, NULL, curbuf);
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00005942 if (fname == NULL)
5943 goto theend;
5944 eap->arg = fname;
5945 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005946 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005947 cmdmod.browse = FALSE; // Don't browse again in do_ecmd().
Bram Moolenaar4033c552017-09-16 20:54:51 +02005948#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005949
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005950 /*
5951 * Either open new tab page or split the window.
5952 */
Bram Moolenaar39902a02018-06-21 22:10:08 +02005953 if (use_tab)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005954 {
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005955 if (win_new_tabpage(cmdmod.tab != 0 ? cmdmod.tab
5956 : eap->addr_count == 0 ? 0
5957 : (int)eap->line2 + 1) != FAIL)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005958 {
Bram Moolenaard2b66012008-01-09 19:30:36 +00005959 do_exedit(eap, old_curwin);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005960
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005961 // set the alternate buffer for the window we came from
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005962 if (curwin != old_curwin
5963 && win_valid(old_curwin)
5964 && old_curwin->w_buffer != curbuf
5965 && !cmdmod.keepalt)
5966 old_curwin->w_alt_fnum = curbuf->b_fnum;
5967 }
5968 }
5969 else if (win_split(eap->addr_count > 0 ? (int)eap->line2 : 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005970 *eap->cmd == 'v' ? WSP_VERT : 0) != FAIL)
5971 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005972 // Reset 'scrollbind' when editing another file, but keep it when
5973 // doing ":split" without arguments.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005974 if (*eap->arg != NUL
Bram Moolenaar8a3bb562018-03-04 20:14:14 +01005975# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00005976 || cmdmod.browse
Bram Moolenaar8a3bb562018-03-04 20:14:14 +01005977# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005978 )
Bram Moolenaar3368ea22010-09-21 16:56:35 +02005979 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005980 else
5981 do_check_scrollbind(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005982 do_exedit(eap, old_curwin);
5983 }
5984
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005985# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00005986 cmdmod.browse = browse_flag;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005987# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005988
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005989# if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005990theend:
5991 vim_free(fname);
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
5995/*
Bram Moolenaar80a94a52006-02-23 21:26:58 +00005996 * Open a new tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005997 */
5998 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005999tabpage_new(void)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006000{
6001 exarg_T ea;
6002
Bram Moolenaara80faa82020-04-12 19:37:17 +02006003 CLEAR_FIELD(ea);
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006004 ea.cmdidx = CMD_tabnew;
6005 ea.cmd = (char_u *)"tabn";
6006 ea.arg = (char_u *)"";
6007 ex_splitview(&ea);
6008}
6009
6010/*
6011 * :tabnext command
6012 */
6013 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006014ex_tabnext(exarg_T *eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006015{
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006016 int tab_number;
6017
Bram Moolenaar8cdbd5b2019-06-16 15:50:45 +02006018 if (ERROR_IF_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02006019 return;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006020 switch (eap->cmdidx)
6021 {
6022 case CMD_tabfirst:
6023 case CMD_tabrewind:
6024 goto_tabpage(1);
6025 break;
6026 case CMD_tablast:
6027 goto_tabpage(9999);
6028 break;
6029 case CMD_tabprevious:
6030 case CMD_tabNext:
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006031 if (eap->arg && *eap->arg != NUL)
6032 {
6033 char_u *p = eap->arg;
6034 char_u *p_save = p;
6035
6036 tab_number = getdigits(&p);
6037 if (p == p_save || *p_save == '-' || *p != NUL
6038 || tab_number == 0)
6039 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006040 // No numbers as argument.
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006041 eap->errmsg = e_invarg;
6042 return;
6043 }
6044 }
6045 else
6046 {
6047 if (eap->addr_count == 0)
6048 tab_number = 1;
6049 else
6050 {
6051 tab_number = eap->line2;
6052 if (tab_number < 1)
6053 {
6054 eap->errmsg = e_invrange;
6055 return;
6056 }
6057 }
6058 }
6059 goto_tabpage(-tab_number);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006060 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006061 default: // CMD_tabnext
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006062 tab_number = get_tabpage_arg(eap);
6063 if (eap->errmsg == NULL)
6064 goto_tabpage(tab_number);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006065 break;
6066 }
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006067}
6068
6069/*
6070 * :tabmove command
6071 */
6072 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006073ex_tabmove(exarg_T *eap)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006074{
Bram Moolenaar40ce3a42015-04-21 18:08:39 +02006075 int tab_number;
Bram Moolenaar8cb8dca2012-07-06 18:27:39 +02006076
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006077 tab_number = get_tabpage_arg(eap);
6078 if (eap->errmsg == NULL)
6079 tabpage_move(tab_number);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006080}
6081
6082/*
6083 * :tabs command: List tabs and their contents.
6084 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006085 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006086ex_tabs(exarg_T *eap UNUSED)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006087{
6088 tabpage_T *tp;
6089 win_T *wp;
6090 int tabcount = 1;
6091
6092 msg_start();
6093 msg_scroll = TRUE;
6094 for (tp = first_tabpage; tp != NULL && !got_int; tp = tp->tp_next)
6095 {
6096 msg_putchar('\n');
6097 vim_snprintf((char *)IObuff, IOSIZE, _("Tab page %d"), tabcount++);
Bram Moolenaar8820b482017-03-16 17:23:31 +01006098 msg_outtrans_attr(IObuff, HL_ATTR(HLF_T));
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006099 out_flush(); // output one line at a time
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006100 ui_breakcheck();
6101
Bram Moolenaar030f0df2006-02-21 22:02:53 +00006102 if (tp == curtab)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006103 wp = firstwin;
6104 else
6105 wp = tp->tp_firstwin;
6106 for ( ; wp != NULL && !got_int; wp = wp->w_next)
6107 {
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006108 msg_putchar('\n');
6109 msg_putchar(wp == curwin ? '>' : ' ');
6110 msg_putchar(' ');
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006111 msg_putchar(bufIsChanged(wp->w_buffer) ? '+' : ' ');
6112 msg_putchar(' ');
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006113 if (buf_spname(wp->w_buffer) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02006114 vim_strncpy(IObuff, buf_spname(wp->w_buffer), IOSIZE - 1);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006115 else
6116 home_replace(wp->w_buffer, wp->w_buffer->b_fname,
6117 IObuff, IOSIZE, TRUE);
6118 msg_outtrans(IObuff);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006119 out_flush(); // output one line at a time
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006120 ui_breakcheck();
6121 }
6122 }
6123}
6124
Bram Moolenaar071d4272004-06-13 20:20:40 +00006125/*
6126 * ":mode": Set screen mode.
6127 * If no argument given, just get the screen size and redraw.
6128 */
6129 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006130ex_mode(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006131{
6132 if (*eap->arg == NUL)
6133 shell_resized();
6134 else
Bram Moolenaar3c71aec2020-01-17 19:32:20 +01006135 emsg(_(e_screenmode));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006136}
6137
Bram Moolenaar071d4272004-06-13 20:20:40 +00006138/*
6139 * ":resize".
6140 * set, increment or decrement current window height
6141 */
6142 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006143ex_resize(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006144{
6145 int n;
6146 win_T *wp = curwin;
6147
6148 if (eap->addr_count > 0)
6149 {
6150 n = eap->line2;
6151 for (wp = firstwin; wp->w_next != NULL && --n > 0; wp = wp->w_next)
6152 ;
6153 }
6154
Bram Moolenaar44a2f922016-03-19 22:11:51 +01006155# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00006156 need_mouse_correct = TRUE;
Bram Moolenaar44a2f922016-03-19 22:11:51 +01006157# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006158 n = atol((char *)eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006159 if (cmdmod.split & WSP_VERT)
6160 {
6161 if (*eap->arg == '-' || *eap->arg == '+')
Bram Moolenaar02631462017-09-22 15:20:32 +02006162 n += curwin->w_width;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006163 else if (n == 0 && eap->arg[0] == NUL) // default is very wide
Bram Moolenaar071d4272004-06-13 20:20:40 +00006164 n = 9999;
6165 win_setwidth_win((int)n, wp);
6166 }
6167 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006168 {
6169 if (*eap->arg == '-' || *eap->arg == '+')
6170 n += curwin->w_height;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006171 else if (n == 0 && eap->arg[0] == NUL) // default is very high
Bram Moolenaar071d4272004-06-13 20:20:40 +00006172 n = 9999;
6173 win_setheight_win((int)n, wp);
6174 }
6175}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006176
6177/*
6178 * ":find [+command] <file>" command.
6179 */
6180 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006181ex_find(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006182{
6183#ifdef FEAT_SEARCHPATH
6184 char_u *fname;
6185 int count;
6186
6187 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg), FNAME_MESS,
6188 TRUE, curbuf->b_ffname);
6189 if (eap->addr_count > 0)
6190 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006191 // Repeat finding the file "count" times. This matters when it
6192 // appears several times in the path.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006193 count = eap->line2;
6194 while (fname != NULL && --count > 0)
6195 {
6196 vim_free(fname);
6197 fname = find_file_in_path(NULL, 0, FNAME_MESS,
6198 FALSE, curbuf->b_ffname);
6199 }
6200 }
6201
6202 if (fname != NULL)
6203 {
6204 eap->arg = fname;
6205#endif
6206 do_exedit(eap, NULL);
6207#ifdef FEAT_SEARCHPATH
6208 vim_free(fname);
6209 }
6210#endif
6211}
6212
6213/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00006214 * ":open" simulation: for now just work like ":visual".
6215 */
6216 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006217ex_open(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00006218{
6219 regmatch_T regmatch;
6220 char_u *p;
6221
6222 curwin->w_cursor.lnum = eap->line2;
6223 beginline(BL_SOL | BL_FIX);
6224 if (*eap->arg == '/')
6225 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006226 // ":open /pattern/": put cursor in column found with pattern
Bram Moolenaardf177f62005-02-22 08:39:57 +00006227 ++eap->arg;
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006228 p = skip_regexp(eap->arg, '/', p_magic);
Bram Moolenaardf177f62005-02-22 08:39:57 +00006229 *p = NUL;
6230 regmatch.regprog = vim_regcomp(eap->arg, p_magic ? RE_MAGIC : 0);
6231 if (regmatch.regprog != NULL)
6232 {
6233 regmatch.rm_ic = p_ic;
6234 p = ml_get_curline();
6235 if (vim_regexec(&regmatch, p, (colnr_T)0))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006236 curwin->w_cursor.col = (colnr_T)(regmatch.startp[0] - p);
Bram Moolenaardf177f62005-02-22 08:39:57 +00006237 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006238 emsg(_(e_nomatch));
Bram Moolenaar473de612013-06-08 18:19:48 +02006239 vim_regfree(regmatch.regprog);
Bram Moolenaardf177f62005-02-22 08:39:57 +00006240 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006241 // Move to the NUL, ignore any other arguments.
Bram Moolenaardf177f62005-02-22 08:39:57 +00006242 eap->arg += STRLEN(eap->arg);
6243 }
6244 check_cursor();
6245
6246 eap->cmdidx = CMD_visual;
6247 do_exedit(eap, NULL);
6248}
6249
6250/*
6251 * ":edit", ":badd", ":visual".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006252 */
6253 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006254ex_edit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006255{
6256 do_exedit(eap, NULL);
6257}
6258
6259/*
Bram Moolenaar4b96df52020-01-26 22:00:26 +01006260 * ":edit <file>" command and alike.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006261 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006262 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006263do_exedit(
6264 exarg_T *eap,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006265 win_T *old_curwin) // curwin before doing a split or NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006266{
6267 int n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006268 int need_hide;
Bram Moolenaardf177f62005-02-22 08:39:57 +00006269 int exmode_was = exmode_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006270
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01006271 if ((eap->cmdidx != CMD_pedit && ERROR_IF_POPUP_WINDOW)
6272 || ERROR_IF_TERM_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02006273 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006274 /*
6275 * ":vi" command ends Ex mode.
6276 */
6277 if (exmode_active && (eap->cmdidx == CMD_visual
6278 || eap->cmdidx == CMD_view))
6279 {
6280 exmode_active = FALSE;
Bram Moolenaar158ea172020-06-18 17:28:39 +02006281 ex_pressedreturn = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006282 if (*eap->arg == NUL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00006283 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006284 // Special case: ":global/pat/visual\NLvi-commands"
Bram Moolenaardf177f62005-02-22 08:39:57 +00006285 if (global_busy)
6286 {
6287 int rd = RedrawingDisabled;
6288 int nwr = no_wait_return;
6289 int ms = msg_scroll;
6290#ifdef FEAT_GUI
6291 int he = hold_gui_events;
6292#endif
6293
6294 if (eap->nextcmd != NULL)
6295 {
6296 stuffReadbuff(eap->nextcmd);
6297 eap->nextcmd = NULL;
6298 }
6299
6300 if (exmode_was != EXMODE_VIM)
6301 settmode(TMODE_RAW);
6302 RedrawingDisabled = 0;
6303 no_wait_return = 0;
6304 need_wait_return = FALSE;
6305 msg_scroll = 0;
6306#ifdef FEAT_GUI
6307 hold_gui_events = 0;
6308#endif
6309 must_redraw = CLEAR;
Bram Moolenaar9e2bcb52020-02-18 21:33:00 +01006310 pending_exmode_active = TRUE;
Bram Moolenaardf177f62005-02-22 08:39:57 +00006311
6312 main_loop(FALSE, TRUE);
6313
Bram Moolenaar9e2bcb52020-02-18 21:33:00 +01006314 pending_exmode_active = FALSE;
Bram Moolenaardf177f62005-02-22 08:39:57 +00006315 RedrawingDisabled = rd;
6316 no_wait_return = nwr;
6317 msg_scroll = ms;
6318#ifdef FEAT_GUI
6319 hold_gui_events = he;
6320#endif
6321 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006322 return;
Bram Moolenaardf177f62005-02-22 08:39:57 +00006323 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006324 }
6325
6326 if ((eap->cmdidx == CMD_new
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006327 || eap->cmdidx == CMD_tabnew
6328 || eap->cmdidx == CMD_tabedit
Bram Moolenaar4033c552017-09-16 20:54:51 +02006329 || eap->cmdidx == CMD_vnew) && *eap->arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006330 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006331 // ":new" or ":tabnew" without argument: edit an new empty buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00006332 setpcmark();
6333 (void)do_ecmd(0, NULL, NULL, eap, ECMD_ONE,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00006334 ECMD_HIDE + (eap->forceit ? ECMD_FORCEIT : 0),
6335 old_curwin == NULL ? curwin : NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006336 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02006337 else if ((eap->cmdidx != CMD_split && eap->cmdidx != CMD_vsplit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006338 || *eap->arg != NUL
6339#ifdef FEAT_BROWSE
6340 || cmdmod.browse
6341#endif
6342 )
6343 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006344 // Can't edit another file when "curbuf_lock" is set. Only ":edit"
6345 // can bring us here, others are stopped earlier.
Bram Moolenaar5555acc2006-04-07 21:33:12 +00006346 if (*eap->arg != NUL && curbuf_locked())
6347 return;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006348
Bram Moolenaar071d4272004-06-13 20:20:40 +00006349 n = readonlymode;
6350 if (eap->cmdidx == CMD_view || eap->cmdidx == CMD_sview)
6351 readonlymode = TRUE;
6352 else if (eap->cmdidx == CMD_enew)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006353 readonlymode = FALSE; // 'readonly' doesn't make sense in an
6354 // empty buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00006355 setpcmark();
6356 if (do_ecmd(0, (eap->cmdidx == CMD_enew ? NULL : eap->arg),
6357 NULL, eap,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006358 // ":edit" goes to first line if Vi compatible
Bram Moolenaar071d4272004-06-13 20:20:40 +00006359 (*eap->arg == NUL && eap->do_ecmd_lnum == 0
6360 && vim_strchr(p_cpo, CPO_GOTO1) != NULL)
6361 ? ECMD_ONE : eap->do_ecmd_lnum,
Bram Moolenaareb44a682017-08-03 22:44:55 +02006362 (buf_hide(curbuf) ? ECMD_HIDE : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006363 + (eap->forceit ? ECMD_FORCEIT : 0)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006364 // after a split we can use an existing buffer
Bram Moolenaar7b449342014-03-25 13:03:48 +01006365 + (old_curwin != NULL ? ECMD_OLDBUF : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006366 + (eap->cmdidx == CMD_badd ? ECMD_ADDBUF : 0 )
Bram Moolenaar701f7af2008-11-15 13:12:07 +00006367 , old_curwin == NULL ? curwin : NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006368 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006369 // Editing the file failed. If the window was split, close it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006370 if (old_curwin != NULL)
6371 {
6372 need_hide = (curbufIsChanged() && curbuf->b_nwindows <= 1);
Bram Moolenaareb44a682017-08-03 22:44:55 +02006373 if (!need_hide || buf_hide(curbuf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006374 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006375#if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00006376 cleanup_T cs;
6377
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006378 // Reset the error/interrupt/exception state here so that
6379 // aborting() returns FALSE when closing a window.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00006380 enter_cleanup(&cs);
Bram Moolenaar4033c552017-09-16 20:54:51 +02006381#endif
6382#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00006383 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006384#endif
Bram Moolenaareb44a682017-08-03 22:44:55 +02006385 win_close(curwin, !need_hide && !buf_hide(curbuf));
Bram Moolenaarc0197e22004-09-13 20:26:32 +00006386
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006387#if defined(FEAT_EVAL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006388 // Restore the error/interrupt/exception state if not
6389 // discarded by a new aborting error, interrupt, or
6390 // uncaught exception.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00006391 leave_cleanup(&cs);
Bram Moolenaar4033c552017-09-16 20:54:51 +02006392#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006393 }
6394 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006395 }
6396 else if (readonlymode && curbuf->b_nwindows == 1)
6397 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006398 // When editing an already visited buffer, 'readonly' won't be set
6399 // but the previous value is kept. With ":view" and ":sview" we
6400 // want the file to be readonly, except when another window is
6401 // editing the same buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006402 curbuf->b_p_ro = TRUE;
6403 }
6404 readonlymode = n;
6405 }
6406 else
6407 {
6408 if (eap->do_ecmd_cmd != NULL)
6409 do_cmdline_cmd(eap->do_ecmd_cmd);
6410#ifdef FEAT_TITLE
6411 n = curwin->w_arg_idx_invalid;
6412#endif
6413 check_arg_idx(curwin);
6414#ifdef FEAT_TITLE
6415 if (n != curwin->w_arg_idx_invalid)
6416 maketitle();
6417#endif
6418 }
6419
Bram Moolenaar071d4272004-06-13 20:20:40 +00006420 /*
6421 * if ":split file" worked, set alternate file name in old window to new
6422 * file
6423 */
6424 if (old_curwin != NULL
6425 && *eap->arg != NUL
6426 && curwin != old_curwin
6427 && win_valid(old_curwin)
Bram Moolenaard4755bb2004-09-02 19:12:26 +00006428 && old_curwin->w_buffer != curbuf
6429 && !cmdmod.keepalt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006430 old_curwin->w_alt_fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006431
6432 ex_no_reprint = TRUE;
6433}
6434
6435#ifndef FEAT_GUI
6436/*
6437 * ":gui" and ":gvim" when there is no GUI.
6438 */
6439 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006440ex_nogui(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006441{
6442 eap->errmsg = e_nogvim;
6443}
6444#endif
6445
Bram Moolenaar4f974752019-02-17 17:44:42 +01006446#if defined(FEAT_GUI_MSWIN) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006447 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006448ex_tearoff(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006449{
6450 gui_make_tearoff(eap->arg);
6451}
6452#endif
6453
Bram Moolenaar29a2c082018-03-05 21:06:23 +01006454#if (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK) \
6455 || defined(FEAT_TERM_POPUP_MENU)) && defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006456 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006457ex_popup(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006458{
Bram Moolenaar29a2c082018-03-05 21:06:23 +01006459# if defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK)
6460 if (gui.in_use)
6461 gui_make_popup(eap->arg, eap->forceit);
6462# ifdef FEAT_TERM_POPUP_MENU
6463 else
6464# endif
6465# endif
6466# ifdef FEAT_TERM_POPUP_MENU
6467 pum_make_popup(eap->arg, eap->forceit);
6468# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006469}
6470#endif
6471
Bram Moolenaar071d4272004-06-13 20:20:40 +00006472 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006473ex_swapname(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006474{
6475 if (curbuf->b_ml.ml_mfp == NULL || curbuf->b_ml.ml_mfp->mf_fname == NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01006476 msg(_("No swap file"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006477 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01006478 msg((char *)curbuf->b_ml.ml_mfp->mf_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006479}
6480
6481/*
6482 * ":syncbind" forces all 'scrollbind' windows to have the same relative
6483 * offset.
6484 * (1998-11-02 16:21:01 R. Edward Ralston <eralston@computer.org>)
6485 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006486 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006487ex_syncbind(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006488{
Bram Moolenaar071d4272004-06-13 20:20:40 +00006489 win_T *wp;
Bram Moolenaardedd1b02013-12-14 13:06:17 +01006490 win_T *save_curwin = curwin;
6491 buf_T *save_curbuf = curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006492 long topline;
6493 long y;
6494 linenr_T old_linenr = curwin->w_cursor.lnum;
6495
6496 setpcmark();
6497
6498 /*
6499 * determine max topline
6500 */
6501 if (curwin->w_p_scb)
6502 {
6503 topline = curwin->w_topline;
Bram Moolenaar29323592016-07-24 22:04:11 +02006504 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006505 {
6506 if (wp->w_p_scb && wp->w_buffer)
6507 {
Bram Moolenaar375e3392019-01-31 18:26:10 +01006508 y = wp->w_buffer->b_ml.ml_line_count - get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006509 if (topline > y)
6510 topline = y;
6511 }
6512 }
6513 if (topline < 1)
6514 topline = 1;
6515 }
6516 else
6517 {
6518 topline = 1;
6519 }
6520
6521
6522 /*
Bram Moolenaardedd1b02013-12-14 13:06:17 +01006523 * Set all scrollbind windows to the same topline.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006524 */
Bram Moolenaar29323592016-07-24 22:04:11 +02006525 FOR_ALL_WINDOWS(curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006526 {
6527 if (curwin->w_p_scb)
6528 {
Bram Moolenaardedd1b02013-12-14 13:06:17 +01006529 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006530 y = topline - curwin->w_topline;
6531 if (y > 0)
6532 scrollup(y, TRUE);
6533 else
6534 scrolldown(-y, TRUE);
6535 curwin->w_scbind_pos = topline;
6536 redraw_later(VALID);
6537 cursor_correct();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006538 curwin->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006539 }
6540 }
Bram Moolenaardedd1b02013-12-14 13:06:17 +01006541 curwin = save_curwin;
6542 curbuf = save_curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006543 if (curwin->w_p_scb)
6544 {
6545 did_syncbind = TRUE;
6546 checkpcmark();
6547 if (old_linenr != curwin->w_cursor.lnum)
6548 {
6549 char_u ctrl_o[2];
6550
6551 ctrl_o[0] = Ctrl_O;
6552 ctrl_o[1] = 0;
6553 ins_typebuf(ctrl_o, REMAP_NONE, 0, TRUE, FALSE);
6554 }
6555 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006556}
6557
6558
6559 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006560ex_read(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006561{
Bram Moolenaardf177f62005-02-22 08:39:57 +00006562 int i;
6563 int empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
6564 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006565
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006566 if (eap->usefilter) // :r!cmd
Bram Moolenaar071d4272004-06-13 20:20:40 +00006567 do_bang(1, eap, FALSE, FALSE, TRUE);
6568 else
6569 {
6570 if (u_save(eap->line2, (linenr_T)(eap->line2 + 1)) == FAIL)
6571 return;
6572
6573#ifdef FEAT_BROWSE
6574 if (cmdmod.browse)
6575 {
6576 char_u *browseFile;
6577
Bram Moolenaar7171abe2004-10-11 10:06:20 +00006578 browseFile = do_browse(0, (char_u *)_("Append File"), eap->arg,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006579 NULL, NULL, NULL, curbuf);
6580 if (browseFile != NULL)
6581 {
6582 i = readfile(browseFile, NULL,
6583 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
6584 vim_free(browseFile);
6585 }
6586 else
6587 i = OK;
6588 }
6589 else
6590#endif
6591 if (*eap->arg == NUL)
6592 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006593 if (check_fname() == FAIL) // check for no file name
Bram Moolenaar071d4272004-06-13 20:20:40 +00006594 return;
6595 i = readfile(curbuf->b_ffname, curbuf->b_fname,
6596 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
6597 }
6598 else
6599 {
6600 if (vim_strchr(p_cpo, CPO_ALTREAD) != NULL)
6601 (void)setaltfname(eap->arg, eap->arg, (linenr_T)1);
6602 i = readfile(eap->arg, NULL,
6603 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
6604
6605 }
Bram Moolenaare13b9af2017-01-13 22:01:02 +01006606 if (i != OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006607 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006608#if defined(FEAT_EVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006609 if (!aborting())
6610#endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006611 semsg(_(e_notopen), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006612 }
6613 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00006614 {
6615 if (empty && exmode_active)
6616 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006617 // Delete the empty line that remains. Historically ex does
6618 // this but vi doesn't.
Bram Moolenaardf177f62005-02-22 08:39:57 +00006619 if (eap->line2 == 0)
6620 lnum = curbuf->b_ml.ml_line_count;
6621 else
6622 lnum = 1;
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00006623 if (*ml_get(lnum) == NUL && u_savedel(lnum, 1L) == OK)
Bram Moolenaardf177f62005-02-22 08:39:57 +00006624 {
Bram Moolenaarca70c072020-05-30 20:30:46 +02006625 ml_delete(lnum);
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00006626 if (curwin->w_cursor.lnum > 1
6627 && curwin->w_cursor.lnum >= lnum)
Bram Moolenaardf177f62005-02-22 08:39:57 +00006628 --curwin->w_cursor.lnum;
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00006629 deleted_lines_mark(lnum, 1L);
Bram Moolenaardf177f62005-02-22 08:39:57 +00006630 }
6631 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006632 redraw_curbuf_later(VALID);
Bram Moolenaardf177f62005-02-22 08:39:57 +00006633 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006634 }
6635}
6636
Bram Moolenaarea408852005-06-25 22:49:46 +00006637static char_u *prev_dir = NULL;
6638
6639#if defined(EXITFREE) || defined(PROTO)
6640 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006641free_cd_dir(void)
Bram Moolenaarea408852005-06-25 22:49:46 +00006642{
Bram Moolenaard23a8232018-02-10 18:45:26 +01006643 VIM_CLEAR(prev_dir);
6644 VIM_CLEAR(globaldir);
Bram Moolenaarea408852005-06-25 22:49:46 +00006645}
6646#endif
6647
Bram Moolenaarf4258302013-06-02 18:20:17 +02006648/*
Bram Moolenaar002bc792020-06-05 22:33:42 +02006649 * Get the previous directory for the given chdir scope.
6650 */
6651 static char_u *
6652get_prevdir(cdscope_T scope)
6653{
6654 if (scope == CDSCOPE_WINDOW)
6655 return curwin->w_prevdir;
6656 else if (scope == CDSCOPE_TABPAGE)
6657 return curtab->tp_prevdir;
6658 return prev_dir;
6659}
6660
6661/*
Bram Moolenaarf4258302013-06-02 18:20:17 +02006662 * Deal with the side effects of changing the current directory.
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006663 * When 'scope' is CDSCOPE_TABPAGE then this was after an ":tcd" command.
6664 * When 'scope' is CDSCOPE_WINDOW then this was after an ":lcd" command.
Bram Moolenaarf4258302013-06-02 18:20:17 +02006665 */
6666 void
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006667post_chdir(cdscope_T scope)
Bram Moolenaarf4258302013-06-02 18:20:17 +02006668{
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006669 if (scope != CDSCOPE_WINDOW)
Bram Moolenaar00aa0692019-04-27 20:37:57 +02006670 // Clear tab local directory for both :cd and :tcd
6671 VIM_CLEAR(curtab->tp_localdir);
Bram Moolenaard23a8232018-02-10 18:45:26 +01006672 VIM_CLEAR(curwin->w_localdir);
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006673 if (scope != CDSCOPE_GLOBAL)
Bram Moolenaarf4258302013-06-02 18:20:17 +02006674 {
Bram Moolenaar002bc792020-06-05 22:33:42 +02006675 char_u *pdir = get_prevdir(scope);
6676
6677 // If still in the global directory, need to remember current
6678 // directory as the global directory.
6679 if (globaldir == NULL && pdir != NULL)
6680 globaldir = vim_strsave(pdir);
6681
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006682 // Remember this local directory for the window.
Bram Moolenaarf4258302013-06-02 18:20:17 +02006683 if (mch_dirname(NameBuff, MAXPATHL) == OK)
Bram Moolenaar00aa0692019-04-27 20:37:57 +02006684 {
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006685 if (scope == CDSCOPE_TABPAGE)
Bram Moolenaar00aa0692019-04-27 20:37:57 +02006686 curtab->tp_localdir = vim_strsave(NameBuff);
6687 else
6688 curwin->w_localdir = vim_strsave(NameBuff);
6689 }
Bram Moolenaarf4258302013-06-02 18:20:17 +02006690 }
6691 else
6692 {
Bram Moolenaar002bc792020-06-05 22:33:42 +02006693 // We are now in the global directory, no need to remember its name.
Bram Moolenaard23a8232018-02-10 18:45:26 +01006694 VIM_CLEAR(globaldir);
Bram Moolenaarf4258302013-06-02 18:20:17 +02006695 }
6696
6697 shorten_fnames(TRUE);
6698}
6699
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006700/*
6701 * Change directory function used by :cd/:tcd/:lcd Ex commands and the
Bram Moolenaar95058722020-06-01 16:26:19 +02006702 * chdir() function.
6703 * scope == CDSCOPE_WINDOW: changes the window-local directory
6704 * scope == CDSCOPE_TABPAGE: changes the tab-local directory
6705 * Otherwise: changes the global directory
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006706 * Returns TRUE if the directory is successfully changed.
6707 */
6708 int
6709changedir_func(
6710 char_u *new_dir,
6711 int forceit,
6712 cdscope_T scope)
6713{
6714 char_u *tofree;
Bram Moolenaar002bc792020-06-05 22:33:42 +02006715 char_u *pdir = NULL;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006716 int dir_differs;
6717 int retval = FALSE;
6718
Bram Moolenaar7cc96922020-01-31 22:41:38 +01006719 if (new_dir == NULL || allbuf_locked())
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006720 return FALSE;
6721
6722 if (vim_strchr(p_cpo, CPO_CHDIR) != NULL && curbufIsChanged() && !forceit)
6723 {
6724 emsg(_("E747: Cannot change directory, buffer is modified (add ! to override)"));
6725 return FALSE;
6726 }
6727
6728 // ":cd -": Change to previous directory
6729 if (STRCMP(new_dir, "-") == 0)
6730 {
Bram Moolenaar002bc792020-06-05 22:33:42 +02006731 pdir = get_prevdir(scope);
6732 if (pdir == NULL)
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006733 {
6734 emsg(_("E186: No previous directory"));
6735 return FALSE;
6736 }
Bram Moolenaar002bc792020-06-05 22:33:42 +02006737 new_dir = pdir;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006738 }
6739
Bram Moolenaar002bc792020-06-05 22:33:42 +02006740 // Free the previous directory
6741 tofree = get_prevdir(scope);
6742
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006743 // Save current directory for next ":cd -"
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006744 if (mch_dirname(NameBuff, MAXPATHL) == OK)
Bram Moolenaar002bc792020-06-05 22:33:42 +02006745 pdir = vim_strsave(NameBuff);
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006746 else
Bram Moolenaar002bc792020-06-05 22:33:42 +02006747 pdir = NULL;
6748 if (scope == CDSCOPE_WINDOW)
6749 curwin->w_prevdir = pdir;
6750 else if (scope == CDSCOPE_TABPAGE)
6751 curtab->tp_prevdir = pdir;
6752 else
6753 prev_dir = pdir;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006754
6755#if defined(UNIX) || defined(VMS)
6756 // for UNIX ":cd" means: go to home directory
6757 if (*new_dir == NUL)
6758 {
6759 // use NameBuff for home directory name
6760# ifdef VMS
6761 char_u *p;
6762
6763 p = mch_getenv((char_u *)"SYS$LOGIN");
6764 if (p == NULL || *p == NUL) // empty is the same as not set
6765 NameBuff[0] = NUL;
6766 else
6767 vim_strncpy(NameBuff, p, MAXPATHL - 1);
6768# else
6769 expand_env((char_u *)"$HOME", NameBuff, MAXPATHL);
6770# endif
6771 new_dir = NameBuff;
6772 }
6773#endif
Bram Moolenaar002bc792020-06-05 22:33:42 +02006774 dir_differs = new_dir == NULL || pdir == NULL
6775 || pathcmp((char *)pdir, (char *)new_dir, -1) != 0;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006776 if (new_dir == NULL || (dir_differs && vim_chdir(new_dir)))
6777 emsg(_(e_failed));
6778 else
6779 {
6780 char_u *acmd_fname;
6781
6782 post_chdir(scope);
6783
6784 if (dir_differs)
6785 {
6786 if (scope == CDSCOPE_WINDOW)
6787 acmd_fname = (char_u *)"window";
6788 else if (scope == CDSCOPE_TABPAGE)
6789 acmd_fname = (char_u *)"tabpage";
6790 else
6791 acmd_fname = (char_u *)"global";
6792 apply_autocmds(EVENT_DIRCHANGED, acmd_fname, new_dir, FALSE,
6793 curbuf);
6794 }
6795 retval = TRUE;
6796 }
6797 vim_free(tofree);
6798
6799 return retval;
6800}
Bram Moolenaarea408852005-06-25 22:49:46 +00006801
Bram Moolenaar071d4272004-06-13 20:20:40 +00006802/*
Bram Moolenaar00aa0692019-04-27 20:37:57 +02006803 * ":cd", ":tcd", ":lcd", ":chdir" ":tchdir" and ":lchdir".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006804 */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00006805 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006806ex_cd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006807{
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01006808 char_u *new_dir;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006809
6810 new_dir = eap->arg;
6811#if !defined(UNIX) && !defined(VMS)
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006812 // for non-UNIX ":cd" means: print current directory
Bram Moolenaar071d4272004-06-13 20:20:40 +00006813 if (*new_dir == NUL)
6814 ex_pwd(NULL);
6815 else
6816#endif
6817 {
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006818 cdscope_T scope = CDSCOPE_GLOBAL;
6819
6820 if (eap->cmdidx == CMD_lcd || eap->cmdidx == CMD_lchdir)
6821 scope = CDSCOPE_WINDOW;
6822 else if (eap->cmdidx == CMD_tcd || eap->cmdidx == CMD_tchdir)
6823 scope = CDSCOPE_TABPAGE;
6824
6825 if (changedir_func(new_dir, eap->forceit, scope))
Bram Moolenaardf177f62005-02-22 08:39:57 +00006826 {
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006827 // Echo the new current directory if the command was typed.
Bram Moolenaarfcfbc672009-07-09 18:13:49 +00006828 if (KeyTyped || p_verbose >= 5)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006829 ex_pwd(eap);
6830 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006831 }
6832}
6833
6834/*
6835 * ":pwd".
6836 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006837 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006838ex_pwd(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006839{
6840 if (mch_dirname(NameBuff, MAXPATHL) == OK)
6841 {
6842#ifdef BACKSLASH_IN_FILENAME
6843 slash_adjust(NameBuff);
6844#endif
Bram Moolenaar95058722020-06-01 16:26:19 +02006845 if (p_verbose > 0)
6846 {
6847 char *context = "global";
6848
6849 if (curwin->w_localdir != NULL)
6850 context = "window";
6851 else if (curtab->tp_localdir != NULL)
6852 context = "tabpage";
6853 smsg("[%s] %s", context, (char *)NameBuff);
6854 }
6855 else
6856 msg((char *)NameBuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006857 }
6858 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006859 emsg(_("E187: Unknown"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006860}
6861
6862/*
6863 * ":=".
6864 */
6865 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006866ex_equal(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006867{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006868 smsg("%ld", (long)eap->line2);
Bram Moolenaardf177f62005-02-22 08:39:57 +00006869 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006870}
6871
6872 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006873ex_sleep(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006874{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006875 int n;
6876 long len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006877
6878 if (cursor_valid())
6879 {
6880 n = W_WINROW(curwin) + curwin->w_wrow - msg_scrolled;
6881 if (n >= 0)
Bram Moolenaar53f81742017-09-22 14:35:51 +02006882 windgoto((int)n, curwin->w_wincol + curwin->w_wcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006883 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006884
6885 len = eap->line2;
6886 switch (*eap->arg)
6887 {
6888 case 'm': break;
6889 case NUL: len *= 1000L; break;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006890 default: semsg(_(e_invarg2), eap->arg); return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006891 }
6892 do_sleep(len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006893}
6894
6895/*
6896 * Sleep for "msec" milliseconds, but keep checking for a CTRL-C every second.
6897 */
6898 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006899do_sleep(long msec)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006900{
Bram Moolenaar52953192019-08-16 10:27:13 +02006901 long done = 0;
Bram Moolenaar975b5272016-03-15 23:10:59 +01006902 long wait_now;
Bram Moolenaar52953192019-08-16 10:27:13 +02006903# ifdef ELAPSED_FUNC
6904 elapsed_T start_tv;
6905
6906 // Remember at what time we started, so that we know how much longer we
6907 // should wait after waiting for a bit.
6908 ELAPSED_INIT(start_tv);
6909# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006910
6911 cursor_on();
Bram Moolenaarf45d7472018-09-30 18:22:26 +02006912 out_flush_cursor(FALSE, FALSE);
Bram Moolenaar52953192019-08-16 10:27:13 +02006913 while (!got_int && done < msec)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006914 {
Bram Moolenaar975b5272016-03-15 23:10:59 +01006915 wait_now = msec - done > 1000L ? 1000L : msec - done;
6916#ifdef FEAT_TIMERS
6917 {
6918 long due_time = check_due_timer();
6919
6920 if (due_time > 0 && due_time < wait_now)
6921 wait_now = due_time;
6922 }
6923#endif
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02006924#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar28e67e02019-08-15 23:05:49 +02006925 if (has_any_channel() && wait_now > 20L)
6926 wait_now = 20L;
6927#endif
6928#ifdef FEAT_SOUND
6929 if (has_any_sound_callback() && wait_now > 20L)
6930 wait_now = 20L;
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02006931#endif
Bram Moolenaar975b5272016-03-15 23:10:59 +01006932 ui_delay(wait_now, TRUE);
Bram Moolenaar52953192019-08-16 10:27:13 +02006933
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02006934#ifdef FEAT_JOB_CHANNEL
6935 if (has_any_channel())
6936 ui_breakcheck_force(TRUE);
6937 else
6938#endif
6939 ui_breakcheck();
Bram Moolenaar93c88e02015-09-15 14:12:05 +02006940#ifdef MESSAGE_QUEUE
Bram Moolenaar52953192019-08-16 10:27:13 +02006941 // Process the netbeans and clientserver messages that may have been
6942 // received in the call to ui_breakcheck() when the GUI is in use. This
6943 // may occur when running a test case.
Bram Moolenaar93c88e02015-09-15 14:12:05 +02006944 parse_queued_messages();
Bram Moolenaare3cc6d42011-10-20 21:58:34 +02006945#endif
Bram Moolenaar52953192019-08-16 10:27:13 +02006946
6947# ifdef ELAPSED_FUNC
6948 // actual time passed
6949 done = ELAPSED_FUNC(start_tv);
6950# else
6951 // guestimate time passed (will actually be more)
6952 done += wait_now;
6953# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006954 }
Bram Moolenaar1ebff3d2018-07-07 16:18:13 +02006955
6956 // If CTRL-C was typed to interrupt the sleep, drop the CTRL-C from the
6957 // input buffer, otherwise a following call to input() fails.
6958 if (got_int)
6959 (void)vpeekc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006960}
6961
Bram Moolenaar071d4272004-06-13 20:20:40 +00006962/*
6963 * ":winsize" command (obsolete).
6964 */
6965 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006966ex_winsize(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006967{
6968 int w, h;
6969 char_u *arg = eap->arg;
6970 char_u *p;
6971
6972 w = getdigits(&arg);
6973 arg = skipwhite(arg);
6974 p = arg;
6975 h = getdigits(&arg);
6976 if (*p != NUL && *arg == NUL)
6977 set_shellsize(w, h, TRUE);
6978 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006979 emsg(_("E465: :winsize requires two number arguments"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006980}
6981
Bram Moolenaar071d4272004-06-13 20:20:40 +00006982 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006983ex_wincmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006984{
6985 int xchar = NUL;
6986 char_u *p;
6987
6988 if (*eap->arg == 'g' || *eap->arg == Ctrl_G)
6989 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006990 // CTRL-W g and CTRL-W CTRL-G have an extra command character
Bram Moolenaar071d4272004-06-13 20:20:40 +00006991 if (eap->arg[1] == NUL)
6992 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006993 emsg(_(e_invarg));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006994 return;
6995 }
6996 xchar = eap->arg[1];
6997 p = eap->arg + 2;
6998 }
6999 else
7000 p = eap->arg + 1;
7001
7002 eap->nextcmd = check_nextcmd(p);
7003 p = skipwhite(p);
7004 if (*p != NUL && *p != '"' && eap->nextcmd == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007005 emsg(_(e_invarg));
Bram Moolenaar12bde492011-06-13 01:19:56 +02007006 else if (!eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007007 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007008 // Pass flags on for ":vertical wincmd ]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00007009 postponed_split_flags = cmdmod.split;
Bram Moolenaard326ce82007-03-11 14:48:29 +00007010 postponed_split_tab = cmdmod.tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007011 do_window(*eap->arg, eap->addr_count > 0 ? eap->line2 : 0L, xchar);
7012 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00007013 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007014 }
7015}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007016
Bram Moolenaar843ee412004-06-30 16:16:41 +00007017#if defined(FEAT_GUI) || defined(UNIX) || defined(VMS) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007018/*
7019 * ":winpos".
7020 */
7021 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007022ex_winpos(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007023{
7024 int x, y;
7025 char_u *arg = eap->arg;
7026 char_u *p;
7027
7028 if (*arg == NUL)
7029 {
Bram Moolenaar843ee412004-06-30 16:16:41 +00007030# if defined(FEAT_GUI) || defined(MSWIN)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007031# ifdef VIMDLL
7032 if (gui.in_use ? gui_mch_get_winpos(&x, &y) != FAIL :
7033 mch_get_winpos(&x, &y) != FAIL)
7034# elif defined(FEAT_GUI)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007035 if (gui.in_use && gui_mch_get_winpos(&x, &y) != FAIL)
Bram Moolenaar843ee412004-06-30 16:16:41 +00007036# else
7037 if (mch_get_winpos(&x, &y) != FAIL)
7038# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007039 {
7040 sprintf((char *)IObuff, _("Window position: X %d, Y %d"), x, y);
Bram Moolenaar32526b32019-01-19 17:43:09 +01007041 msg((char *)IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007042 }
7043 else
7044# endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007045 emsg(_("E188: Obtaining window position not implemented for this platform"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007046 }
7047 else
7048 {
7049 x = getdigits(&arg);
7050 arg = skipwhite(arg);
7051 p = arg;
7052 y = getdigits(&arg);
7053 if (*p == NUL || *arg != NUL)
7054 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007055 emsg(_("E466: :winpos requires two number arguments"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007056 return;
7057 }
7058# ifdef FEAT_GUI
7059 if (gui.in_use)
7060 gui_mch_set_winpos(x, y);
7061 else if (gui.starting)
7062 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007063 // Remember the coordinates for when the window is opened.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007064 gui_win_x = x;
7065 gui_win_y = y;
7066 }
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007067# if defined(HAVE_TGETENT) || defined(VIMDLL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007068 else
7069# endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007070# endif
7071# if defined(MSWIN) && (!defined(FEAT_GUI) || defined(VIMDLL))
Bram Moolenaar843ee412004-06-30 16:16:41 +00007072 mch_set_winpos(x, y);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007073# endif
7074# ifdef HAVE_TGETENT
7075 if (*T_CWP)
7076 term_set_winpos(x, y);
7077# endif
7078 }
7079}
7080#endif
7081
7082/*
7083 * Handle command that work like operators: ":delete", ":yank", ":>" and ":<".
7084 */
7085 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007086ex_operators(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007087{
7088 oparg_T oa;
7089
7090 clear_oparg(&oa);
7091 oa.regname = eap->regname;
7092 oa.start.lnum = eap->line1;
7093 oa.end.lnum = eap->line2;
7094 oa.line_count = eap->line2 - eap->line1 + 1;
7095 oa.motion_type = MLINE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007096 virtual_op = FALSE;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007097 if (eap->cmdidx != CMD_yank) // position cursor for undo
Bram Moolenaar071d4272004-06-13 20:20:40 +00007098 {
7099 setpcmark();
7100 curwin->w_cursor.lnum = eap->line1;
7101 beginline(BL_SOL | BL_FIX);
7102 }
7103
Bram Moolenaard07c6e12013-11-21 14:21:40 +01007104 if (VIsual_active)
7105 end_visual_mode();
Bram Moolenaard07c6e12013-11-21 14:21:40 +01007106
Bram Moolenaar071d4272004-06-13 20:20:40 +00007107 switch (eap->cmdidx)
7108 {
7109 case CMD_delete:
7110 oa.op_type = OP_DELETE;
7111 op_delete(&oa);
7112 break;
7113
7114 case CMD_yank:
7115 oa.op_type = OP_YANK;
7116 (void)op_yank(&oa, FALSE, TRUE);
7117 break;
7118
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007119 default: // CMD_rshift or CMD_lshift
Bram Moolenaar6e707362013-06-14 19:15:58 +02007120 if (
Bram Moolenaar071d4272004-06-13 20:20:40 +00007121#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6e707362013-06-14 19:15:58 +02007122 (eap->cmdidx == CMD_rshift) ^ curwin->w_p_rl
7123#else
7124 eap->cmdidx == CMD_rshift
Bram Moolenaar071d4272004-06-13 20:20:40 +00007125#endif
Bram Moolenaar6e707362013-06-14 19:15:58 +02007126 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00007127 oa.op_type = OP_RSHIFT;
7128 else
7129 oa.op_type = OP_LSHIFT;
7130 op_shift(&oa, FALSE, eap->amount);
7131 break;
7132 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007133 virtual_op = MAYBE;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007134 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007135}
7136
7137/*
7138 * ":put".
7139 */
7140 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007141ex_put(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007142{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007143 // ":0put" works like ":1put!".
Bram Moolenaar071d4272004-06-13 20:20:40 +00007144 if (eap->line2 == 0)
7145 {
7146 eap->line2 = 1;
7147 eap->forceit = TRUE;
7148 }
7149 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007150 do_put(eap->regname, eap->forceit ? BACKWARD : FORWARD, 1L,
7151 PUT_LINE|PUT_CURSLINE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007152}
7153
7154/*
7155 * Handle ":copy" and ":move".
7156 */
7157 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007158ex_copymove(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007159{
7160 long n;
7161
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02007162 n = get_address(eap, &eap->arg, eap->addr_type, FALSE, FALSE, FALSE, 1);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007163 if (eap->arg == NULL) // error detected
Bram Moolenaar071d4272004-06-13 20:20:40 +00007164 {
7165 eap->nextcmd = NULL;
7166 return;
7167 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00007168 get_flags(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007169
7170 /*
7171 * move or copy lines from 'eap->line1'-'eap->line2' to below line 'n'
7172 */
7173 if (n == MAXLNUM || n < 0 || n > curbuf->b_ml.ml_line_count)
7174 {
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02007175 emsg(_(e_invrange));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007176 return;
7177 }
7178
7179 if (eap->cmdidx == CMD_move)
7180 {
7181 if (do_move(eap->line1, eap->line2, n) == FAIL)
7182 return;
7183 }
7184 else
7185 ex_copy(eap->line1, eap->line2, n);
7186 u_clearline();
7187 beginline(BL_SOL | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007188 ex_may_print(eap);
7189}
7190
7191/*
7192 * Print the current line if flags were given to the Ex command.
7193 */
Bram Moolenaarfd3fe982014-04-01 17:49:44 +02007194 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007195ex_may_print(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007196{
7197 if (eap->flags != 0)
7198 {
7199 print_line(curwin->w_cursor.lnum, (eap->flags & EXFLAG_NR),
7200 (eap->flags & EXFLAG_LIST));
7201 ex_no_reprint = TRUE;
7202 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007203}
7204
7205/*
7206 * ":smagic" and ":snomagic".
7207 */
7208 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007209ex_submagic(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007210{
7211 int magic_save = p_magic;
7212
7213 p_magic = (eap->cmdidx == CMD_smagic);
7214 do_sub(eap);
7215 p_magic = magic_save;
7216}
7217
7218/*
7219 * ":join".
7220 */
7221 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007222ex_join(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007223{
7224 curwin->w_cursor.lnum = eap->line1;
7225 if (eap->line1 == eap->line2)
7226 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007227 if (eap->addr_count >= 2) // :2,2join does nothing
Bram Moolenaar071d4272004-06-13 20:20:40 +00007228 return;
7229 if (eap->line2 == curbuf->b_ml.ml_line_count)
7230 {
7231 beep_flush();
7232 return;
7233 }
7234 ++eap->line2;
7235 }
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02007236 (void)do_join(eap->line2 - eap->line1 + 1, !eap->forceit, TRUE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007237 beginline(BL_WHITE | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007238 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007239}
7240
7241/*
7242 * ":[addr]@r" or ":[addr]*r": execute register
7243 */
7244 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007245ex_at(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007246{
7247 int c;
Bram Moolenaar60462872009-11-03 11:40:19 +00007248 int prev_len = typebuf.tb_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007249
7250 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaar4930a762016-09-11 14:39:53 +02007251 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007252
7253#ifdef USE_ON_FLY_SCROLL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007254 dont_scroll = TRUE; // disallow scrolling here
Bram Moolenaar071d4272004-06-13 20:20:40 +00007255#endif
7256
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007257 // get the register name. No name means to use the previous one
Bram Moolenaar071d4272004-06-13 20:20:40 +00007258 c = *eap->arg;
7259 if (c == NUL || (c == '*' && *eap->cmd == '*'))
7260 c = '@';
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007261 // Put the register in the typeahead buffer with the "silent" flag.
Bram Moolenaard333d1e2006-11-07 17:43:47 +00007262 if (do_execreg(c, TRUE, vim_strchr(p_cpo, CPO_EXECBUF) != NULL, TRUE)
7263 == FAIL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007264 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007265 beep_flush();
Bram Moolenaardf177f62005-02-22 08:39:57 +00007266 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007267 else
7268 {
7269 int save_efr = exec_from_reg;
7270
7271 exec_from_reg = TRUE;
7272
7273 /*
7274 * Execute from the typeahead buffer.
Bram Moolenaar60462872009-11-03 11:40:19 +00007275 * Continue until the stuff buffer is empty and all added characters
7276 * have been consumed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007277 */
Bram Moolenaar60462872009-11-03 11:40:19 +00007278 while (!stuff_empty() || typebuf.tb_len > prev_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007279 (void)do_cmdline(NULL, getexline, NULL, DOCMD_NOWAIT|DOCMD_VERBOSE);
7280
7281 exec_from_reg = save_efr;
7282 }
7283}
7284
7285/*
7286 * ":!".
7287 */
7288 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007289ex_bang(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007290{
7291 do_bang(eap->addr_count, eap, eap->forceit, TRUE, TRUE);
7292}
7293
7294/*
7295 * ":undo".
7296 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007297 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +01007298ex_undo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007299{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007300 if (eap->addr_count == 1) // :undo 123
Bram Moolenaar730cde92010-06-27 05:18:54 +02007301 undo_time(eap->line2, FALSE, FALSE, TRUE);
Bram Moolenaard3667a22006-03-16 21:35:52 +00007302 else
7303 u_undo(1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007304}
7305
Bram Moolenaar55debbe2010-05-23 23:34:36 +02007306#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar6df6f472010-07-18 18:04:50 +02007307 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007308ex_wundo(exarg_T *eap)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02007309{
7310 char_u hash[UNDO_HASH_SIZE];
7311
7312 u_compute_hash(hash);
7313 u_write_undo(eap->arg, eap->forceit, curbuf, hash);
7314}
7315
Bram Moolenaar6df6f472010-07-18 18:04:50 +02007316 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007317ex_rundo(exarg_T *eap)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02007318{
7319 char_u hash[UNDO_HASH_SIZE];
7320
7321 u_compute_hash(hash);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02007322 u_read_undo(eap->arg, hash, NULL);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02007323}
7324#endif
7325
Bram Moolenaar071d4272004-06-13 20:20:40 +00007326/*
7327 * ":redo".
7328 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007329 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007330ex_redo(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007331{
7332 u_redo(1);
7333}
7334
7335/*
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007336 * ":earlier" and ":later".
7337 */
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007338 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007339ex_later(exarg_T *eap)
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007340{
7341 long count = 0;
7342 int sec = FALSE;
Bram Moolenaar730cde92010-06-27 05:18:54 +02007343 int file = FALSE;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007344 char_u *p = eap->arg;
7345
7346 if (*p == NUL)
7347 count = 1;
7348 else if (isdigit(*p))
7349 {
7350 count = getdigits(&p);
7351 switch (*p)
7352 {
7353 case 's': ++p; sec = TRUE; break;
7354 case 'm': ++p; sec = TRUE; count *= 60; break;
7355 case 'h': ++p; sec = TRUE; count *= 60 * 60; break;
Bram Moolenaar730cde92010-06-27 05:18:54 +02007356 case 'd': ++p; sec = TRUE; count *= 24 * 60 * 60; break;
7357 case 'f': ++p; file = TRUE; break;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007358 }
7359 }
7360
7361 if (*p != NUL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007362 semsg(_(e_invarg2), eap->arg);
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007363 else
Bram Moolenaar730cde92010-06-27 05:18:54 +02007364 undo_time(eap->cmdidx == CMD_earlier ? -count : count,
7365 sec, file, FALSE);
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007366}
7367
7368/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007369 * ":redir": start/stop redirection.
7370 */
7371 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007372ex_redir(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007373{
7374 char *mode;
7375 char_u *fname;
Bram Moolenaarca472992005-01-21 11:46:23 +00007376 char_u *arg = eap->arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007377
Bram Moolenaarba768492016-07-08 15:32:54 +02007378#ifdef FEAT_EVAL
Bram Moolenaar79815f12016-07-09 17:07:29 +02007379 if (redir_execute)
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02007380 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007381 emsg(_("E930: Cannot use :redir inside execute()"));
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02007382 return;
7383 }
Bram Moolenaarba768492016-07-08 15:32:54 +02007384#endif
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02007385
Bram Moolenaar071d4272004-06-13 20:20:40 +00007386 if (STRICMP(eap->arg, "END") == 0)
7387 close_redir();
7388 else
7389 {
Bram Moolenaarca472992005-01-21 11:46:23 +00007390 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00007391 {
Bram Moolenaarca472992005-01-21 11:46:23 +00007392 ++arg;
7393 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00007394 {
Bram Moolenaarca472992005-01-21 11:46:23 +00007395 ++arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007396 mode = "a";
7397 }
7398 else
7399 mode = "w";
Bram Moolenaarca472992005-01-21 11:46:23 +00007400 arg = skipwhite(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007401
7402 close_redir();
7403
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007404 // Expand environment variables and "~/".
Bram Moolenaarca472992005-01-21 11:46:23 +00007405 fname = expand_env_save(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007406 if (fname == NULL)
7407 return;
7408#ifdef FEAT_BROWSE
7409 if (cmdmod.browse)
7410 {
7411 char_u *browseFile;
7412
Bram Moolenaar7171abe2004-10-11 10:06:20 +00007413 browseFile = do_browse(BROWSE_SAVE,
7414 (char_u *)_("Save Redirection"),
Bram Moolenaarc36651b2018-04-29 12:22:56 +02007415 fname, NULL, NULL,
7416 (char_u *)_(BROWSE_FILTER_ALL_FILES), curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007417 if (browseFile == NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007418 return; // operation cancelled
Bram Moolenaar071d4272004-06-13 20:20:40 +00007419 vim_free(fname);
7420 fname = browseFile;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007421 eap->forceit = TRUE; // since dialog already asked
Bram Moolenaar071d4272004-06-13 20:20:40 +00007422 }
7423#endif
7424
7425 redir_fd = open_exfile(fname, eap->forceit, mode);
7426 vim_free(fname);
7427 }
7428#ifdef FEAT_EVAL
Bram Moolenaarca472992005-01-21 11:46:23 +00007429 else if (*arg == '@')
Bram Moolenaar071d4272004-06-13 20:20:40 +00007430 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007431 // redirect to a register a-z (resp. A-Z for appending)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007432 close_redir();
Bram Moolenaarca472992005-01-21 11:46:23 +00007433 ++arg;
7434 if (ASCII_ISALPHA(*arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007435# ifdef FEAT_CLIPBOARD
Bram Moolenaarca472992005-01-21 11:46:23 +00007436 || *arg == '*'
Bram Moolenaar9a51c6e2006-11-14 19:25:02 +00007437 || *arg == '+'
Bram Moolenaar071d4272004-06-13 20:20:40 +00007438# endif
Bram Moolenaarca472992005-01-21 11:46:23 +00007439 || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00007440 {
Bram Moolenaarca472992005-01-21 11:46:23 +00007441 redir_reg = *arg++;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007442 if (*arg == '>' && arg[1] == '>') // append
Bram Moolenaard9d30582005-05-18 22:10:28 +00007443 arg += 2;
Bram Moolenaarc188b882007-10-19 14:20:54 +00007444 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007445 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007446 // Can use both "@a" and "@a>".
Bram Moolenaar2c29bee2005-06-01 21:46:07 +00007447 if (*arg == '>')
7448 arg++;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007449 // Make register empty when not using @A-@Z and the
7450 // command is valid.
Bram Moolenaarc188b882007-10-19 14:20:54 +00007451 if (*arg == NUL && !isupper(redir_reg))
7452 write_reg_contents(redir_reg, (char_u *)"", -1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007453 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00007454 }
7455 if (*arg != NUL)
7456 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00007457 redir_reg = 0;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007458 semsg(_(e_invarg2), eap->arg);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007459 }
7460 }
7461 else if (*arg == '=' && arg[1] == '>')
7462 {
7463 int append;
7464
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007465 // redirect to a variable
Bram Moolenaardf177f62005-02-22 08:39:57 +00007466 close_redir();
7467 arg += 2;
7468
7469 if (*arg == '>')
7470 {
7471 ++arg;
7472 append = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007473 }
7474 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00007475 append = FALSE;
7476
7477 if (var_redir_start(skipwhite(arg), append) == OK)
7478 redir_vname = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007479 }
7480#endif
7481
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007482 // TODO: redirect to a buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00007483
Bram Moolenaar071d4272004-06-13 20:20:40 +00007484 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007485 semsg(_(e_invarg2), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007486 }
Bram Moolenaar29b2d262006-09-10 19:07:28 +00007487
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007488 // Make sure redirection is not off. Can happen for cmdline completion
7489 // that indirectly invokes a command to catch its output.
Bram Moolenaar29b2d262006-09-10 19:07:28 +00007490 if (redir_fd != NULL
7491#ifdef FEAT_EVAL
7492 || redir_reg || redir_vname
7493#endif
7494 )
7495 redir_off = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007496}
7497
7498/*
7499 * ":redraw": force redraw
7500 */
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01007501 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007502ex_redraw(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007503{
7504 int r = RedrawingDisabled;
7505 int p = p_lz;
7506
7507 RedrawingDisabled = 0;
7508 p_lz = FALSE;
Bram Moolenaarabc39ab2017-03-01 18:04:05 +01007509 validate_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007510 update_topline();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007511 update_screen(eap->forceit ? CLEAR : VIsual_active ? INVERTED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007512#ifdef FEAT_TITLE
7513 if (need_maketitle)
7514 maketitle();
7515#endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007516#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
7517# ifdef VIMDLL
7518 if (!gui.in_use)
7519# endif
7520 resize_console_buf();
Bram Moolenaar78d21da2019-02-17 15:00:52 +01007521#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007522 RedrawingDisabled = r;
7523 p_lz = p;
7524
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007525 // Reset msg_didout, so that a message that's there is overwritten.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007526 msg_didout = FALSE;
7527 msg_col = 0;
7528
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007529 // No need to wait after an intentional redraw.
Bram Moolenaar56667a52013-07-17 11:54:28 +02007530 need_wait_return = FALSE;
7531
Bram Moolenaar071d4272004-06-13 20:20:40 +00007532 out_flush();
7533}
7534
7535/*
7536 * ":redrawstatus": force redraw of status line(s)
7537 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007538 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007539ex_redrawstatus(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007540{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007541 int r = RedrawingDisabled;
7542 int p = p_lz;
7543
7544 RedrawingDisabled = 0;
7545 p_lz = FALSE;
7546 if (eap->forceit)
7547 status_redraw_all();
7548 else
7549 status_redraw_curbuf();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007550 update_screen(VIsual_active ? INVERTED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007551 RedrawingDisabled = r;
7552 p_lz = p;
7553 out_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007554}
7555
Bram Moolenaare12bab32019-01-08 22:02:56 +01007556/*
7557 * ":redrawtabline": force redraw of the tabline
7558 */
7559 static void
7560ex_redrawtabline(exarg_T *eap UNUSED)
7561{
7562 int r = RedrawingDisabled;
7563 int p = p_lz;
7564
7565 RedrawingDisabled = 0;
7566 p_lz = FALSE;
7567
7568 draw_tabline();
7569
7570 RedrawingDisabled = r;
7571 p_lz = p;
7572 out_flush();
7573}
7574
Bram Moolenaar071d4272004-06-13 20:20:40 +00007575 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007576close_redir(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007577{
7578 if (redir_fd != NULL)
7579 {
7580 fclose(redir_fd);
7581 redir_fd = NULL;
7582 }
7583#ifdef FEAT_EVAL
7584 redir_reg = 0;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007585 if (redir_vname)
7586 {
7587 var_redir_stop();
7588 redir_vname = 0;
7589 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007590#endif
7591}
7592
Bram Moolenaarf96ae0b2019-07-28 15:21:55 +02007593#if (defined(FEAT_SESSION) || defined(FEAT_EVAL)) || defined(PROTO)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007594 int
Bram Moolenaarbd67aac2019-09-21 23:09:04 +02007595vim_mkdir_emsg(char_u *name, int prot UNUSED)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007596{
7597 if (vim_mkdir(name, prot) != 0)
7598 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007599 semsg(_("E739: Cannot create directory: %s"), name);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007600 return FAIL;
7601 }
7602 return OK;
7603}
7604#endif
7605
Bram Moolenaar071d4272004-06-13 20:20:40 +00007606/*
7607 * Open a file for writing for an Ex command, with some checks.
7608 * Return file descriptor, or NULL on failure.
7609 */
7610 FILE *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007611open_exfile(
7612 char_u *fname,
7613 int forceit,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007614 char *mode) // "w" for create new file or "a" for append
Bram Moolenaar071d4272004-06-13 20:20:40 +00007615{
7616 FILE *fd;
7617
7618#ifdef UNIX
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007619 // with Unix it is possible to open a directory
Bram Moolenaar071d4272004-06-13 20:20:40 +00007620 if (mch_isdir(fname))
7621 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007622 semsg(_(e_isadir2), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007623 return NULL;
7624 }
7625#endif
7626 if (!forceit && *mode != 'a' && vim_fexists(fname))
7627 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007628 semsg(_("E189: \"%s\" exists (add ! to override)"), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007629 return NULL;
7630 }
7631
7632 if ((fd = mch_fopen((char *)fname, mode)) == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007633 semsg(_("E190: Cannot open \"%s\" for writing"), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007634
7635 return fd;
7636}
7637
7638/*
7639 * ":mark" and ":k".
7640 */
7641 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007642ex_mark(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007643{
7644 pos_T pos;
7645
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007646 if (*eap->arg == NUL) // No argument?
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007647 emsg(_(e_argreq));
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007648 else if (eap->arg[1] != NUL) // more than one character?
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007649 emsg(_(e_trailing));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007650 else
7651 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007652 pos = curwin->w_cursor; // save curwin->w_cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00007653 curwin->w_cursor.lnum = eap->line2;
7654 beginline(BL_WHITE | BL_FIX);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007655 if (setmark(*eap->arg) == FAIL) // set mark
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007656 emsg(_("E191: Argument must be a letter or forward/backward quote"));
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007657 curwin->w_cursor = pos; // restore curwin->w_cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00007658 }
7659}
7660
7661/*
7662 * Update w_topline, w_leftcol and the cursor position.
7663 */
7664 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007665update_topline_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007666{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007667 check_cursor(); // put cursor on valid line
Bram Moolenaar071d4272004-06-13 20:20:40 +00007668 update_topline();
7669 if (!curwin->w_p_wrap)
7670 validate_cursor();
7671 update_curswant();
7672}
7673
Bram Moolenaar071d4272004-06-13 20:20:40 +00007674/*
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007675 * Save the current State and go to Normal mode.
7676 * Return TRUE if the typeahead could be saved.
7677 */
7678 int
7679save_current_state(save_state_T *sst)
7680{
7681 sst->save_msg_scroll = msg_scroll;
7682 sst->save_restart_edit = restart_edit;
7683 sst->save_msg_didout = msg_didout;
7684 sst->save_State = State;
7685 sst->save_insertmode = p_im;
7686 sst->save_finish_op = finish_op;
7687 sst->save_opcount = opcount;
Bram Moolenaarcce713d2019-03-04 11:40:12 +01007688 sst->save_reg_executing = reg_executing;
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007689
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007690 msg_scroll = FALSE; // no msg scrolling in Normal mode
7691 restart_edit = 0; // don't go to Insert mode
7692 p_im = FALSE; // don't use 'insertmode'
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007693
7694 /*
7695 * Save the current typeahead. This is required to allow using ":normal"
7696 * from an event handler and makes sure we don't hang when the argument
7697 * ends with half a command.
7698 */
7699 save_typeahead(&sst->tabuf);
7700 return sst->tabuf.typebuf_valid;
7701}
7702
7703 void
7704restore_current_state(save_state_T *sst)
7705{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007706 // Restore the previous typeahead.
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007707 restore_typeahead(&sst->tabuf);
7708
7709 msg_scroll = sst->save_msg_scroll;
7710 restart_edit = sst->save_restart_edit;
7711 p_im = sst->save_insertmode;
7712 finish_op = sst->save_finish_op;
7713 opcount = sst->save_opcount;
Bram Moolenaarcce713d2019-03-04 11:40:12 +01007714 reg_executing = sst->save_reg_executing;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007715 msg_didout |= sst->save_msg_didout; // don't reset msg_didout now
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007716
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007717 // Restore the state (needed when called from a function executed for
7718 // 'indentexpr'). Update the mouse and cursor, they may have changed.
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007719 State = sst->save_State;
7720#ifdef CURSOR_SHAPE
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007721 ui_cursor_shape(); // may show different cursor shape
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007722#endif
7723}
7724
7725/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007726 * ":normal[!] {commands}": Execute normal mode commands.
7727 */
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01007728 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007729ex_normal(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007730{
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007731 save_state_T save_state;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007732 char_u *arg = NULL;
7733 int l;
7734 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007735
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00007736 if (ex_normal_lock > 0)
7737 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007738 emsg(_(e_secure));
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00007739 return;
7740 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007741 if (ex_normal_busy >= p_mmd)
7742 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007743 emsg(_("E192: Recursive use of :normal too deep"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007744 return;
7745 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007746
Bram Moolenaar071d4272004-06-13 20:20:40 +00007747 /*
7748 * vgetc() expects a CSI and K_SPECIAL to have been escaped. Don't do
7749 * this for the K_SPECIAL leading byte, otherwise special keys will not
7750 * work.
7751 */
7752 if (has_mbyte)
7753 {
7754 int len = 0;
7755
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007756 // Count the number of characters to be escaped.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007757 for (p = eap->arg; *p != NUL; ++p)
7758 {
Bram Moolenaar13505972019-01-24 15:04:48 +01007759#ifdef FEAT_GUI
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007760 if (*p == CSI) // leadbyte CSI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007761 len += 2;
Bram Moolenaar13505972019-01-24 15:04:48 +01007762#endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007763 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007764 if (*++p == K_SPECIAL // trailbyte K_SPECIAL or CSI
Bram Moolenaar13505972019-01-24 15:04:48 +01007765#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007766 || *p == CSI
Bram Moolenaar13505972019-01-24 15:04:48 +01007767#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007768 )
7769 len += 2;
7770 }
7771 if (len > 0)
7772 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02007773 arg = alloc(STRLEN(eap->arg) + len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007774 if (arg != NULL)
7775 {
7776 len = 0;
7777 for (p = eap->arg; *p != NUL; ++p)
7778 {
7779 arg[len++] = *p;
Bram Moolenaar13505972019-01-24 15:04:48 +01007780#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007781 if (*p == CSI)
7782 {
7783 arg[len++] = KS_EXTRA;
7784 arg[len++] = (int)KE_CSI;
7785 }
Bram Moolenaar13505972019-01-24 15:04:48 +01007786#endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007787 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007788 {
7789 arg[len++] = *++p;
7790 if (*p == K_SPECIAL)
7791 {
7792 arg[len++] = KS_SPECIAL;
7793 arg[len++] = KE_FILLER;
7794 }
Bram Moolenaar13505972019-01-24 15:04:48 +01007795#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007796 else if (*p == CSI)
7797 {
7798 arg[len++] = KS_EXTRA;
7799 arg[len++] = (int)KE_CSI;
7800 }
Bram Moolenaar13505972019-01-24 15:04:48 +01007801#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007802 }
7803 arg[len] = NUL;
7804 }
7805 }
7806 }
7807 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007808
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007809 ++ex_normal_busy;
7810 if (save_current_state(&save_state))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007811 {
7812 /*
7813 * Repeat the :normal command for each line in the range. When no
7814 * range given, execute it just once, without positioning the cursor
7815 * first.
7816 */
7817 do
7818 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007819 if (eap->addr_count != 0)
7820 {
7821 curwin->w_cursor.lnum = eap->line1++;
7822 curwin->w_cursor.col = 0;
Bram Moolenaard5d37532017-03-27 23:02:07 +02007823 check_cursor_moved(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007824 }
7825
Bram Moolenaar13505972019-01-24 15:04:48 +01007826 exec_normal_cmd(arg != NULL
7827 ? arg
7828 : eap->arg, eap->forceit ? REMAP_NONE : REMAP_YES, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007829 }
7830 while (eap->addr_count > 0 && eap->line1 <= eap->line2 && !got_int);
7831 }
7832
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007833 // Might not return to the main loop when in an event handler.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007834 update_topline_cursor();
7835
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007836 restore_current_state(&save_state);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007837 --ex_normal_busy;
Bram Moolenaareda73602014-11-05 09:53:23 +01007838 setmouse();
Bram Moolenaareda73602014-11-05 09:53:23 +01007839#ifdef CURSOR_SHAPE
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007840 ui_cursor_shape(); // may show different cursor shape
Bram Moolenaareda73602014-11-05 09:53:23 +01007841#endif
7842
Bram Moolenaar071d4272004-06-13 20:20:40 +00007843 vim_free(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007844}
7845
7846/*
Bram Moolenaar64969662005-12-14 21:59:55 +00007847 * ":startinsert", ":startreplace" and ":startgreplace"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007848 */
7849 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007850ex_startinsert(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007851{
Bram Moolenaarfd371682005-01-14 21:42:54 +00007852 if (eap->forceit)
7853 {
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02007854 // cursor line can be zero on startup
Bram Moolenaar09ca9322017-09-26 17:40:45 +02007855 if (!curwin->w_cursor.lnum)
7856 curwin->w_cursor.lnum = 1;
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02007857 set_cursor_for_append_to_line();
Bram Moolenaarfd371682005-01-14 21:42:54 +00007858 }
Bram Moolenaareb58a242020-04-19 18:13:19 +02007859#ifdef FEAT_TERMINAL
7860 // Ignore this when running in an active terminal.
7861 if (term_job_running(curbuf->b_term))
7862 return;
7863#endif
Bram Moolenaarfd371682005-01-14 21:42:54 +00007864
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02007865 // Ignore the command when already in Insert mode. Inserting an
7866 // expression register that invokes a function can do this.
Bram Moolenaara40c5002005-01-09 21:16:21 +00007867 if (State & INSERT)
7868 return;
7869
Bram Moolenaar64969662005-12-14 21:59:55 +00007870 if (eap->cmdidx == CMD_startinsert)
7871 restart_edit = 'a';
7872 else if (eap->cmdidx == CMD_startreplace)
7873 restart_edit = 'R';
Bram Moolenaar071d4272004-06-13 20:20:40 +00007874 else
Bram Moolenaar64969662005-12-14 21:59:55 +00007875 restart_edit = 'V';
7876
7877 if (!eap->forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007878 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00007879 if (eap->cmdidx == CMD_startinsert)
7880 restart_edit = 'i';
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02007881 curwin->w_curswant = 0; // avoid MAXCOL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007882 }
7883}
7884
7885/*
7886 * ":stopinsert"
7887 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007888 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007889ex_stopinsert(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007890{
7891 restart_edit = 0;
7892 stop_insert_mode = TRUE;
Bram Moolenaarfd773e92016-04-02 19:39:16 +02007893 clearmode();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007894}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007895
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00007896/*
7897 * Execute normal mode command "cmd".
7898 * "remap" can be REMAP_NONE or REMAP_YES.
7899 */
7900 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007901exec_normal_cmd(char_u *cmd, int remap, int silent)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00007902{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007903 // Stuff the argument into the typeahead buffer.
Bram Moolenaar25281632016-01-21 23:32:32 +01007904 ins_typebuf(cmd, remap, 0, TRUE, silent);
Bram Moolenaar586c70c2018-10-02 16:23:58 +02007905 exec_normal(FALSE, FALSE, FALSE);
Bram Moolenaar25281632016-01-21 23:32:32 +01007906}
Bram Moolenaar25281632016-01-21 23:32:32 +01007907
Bram Moolenaar25281632016-01-21 23:32:32 +01007908/*
7909 * Execute normal_cmd() until there is no typeahead left.
Bram Moolenaar586c70c2018-10-02 16:23:58 +02007910 * When "use_vpeekc" is TRUE use vpeekc() to check for available chars.
Bram Moolenaar25281632016-01-21 23:32:32 +01007911 */
7912 void
Bram Moolenaar586c70c2018-10-02 16:23:58 +02007913exec_normal(int was_typed, int use_vpeekc, int may_use_terminal_loop UNUSED)
Bram Moolenaar25281632016-01-21 23:32:32 +01007914{
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00007915 oparg_T oa;
Bram Moolenaar905dd902019-04-07 14:21:47 +02007916 int c;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00007917
Bram Moolenaar905dd902019-04-07 14:21:47 +02007918 // When calling vpeekc() from feedkeys() it will return Ctrl_C when there
7919 // is nothing to get, so also check for Ctrl_C.
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00007920 clear_oparg(&oa);
7921 finish_op = FALSE;
Bram Moolenaar586c70c2018-10-02 16:23:58 +02007922 while ((!stuff_empty()
7923 || ((was_typed || !typebuf_typed()) && typebuf.tb_len > 0)
Bram Moolenaar905dd902019-04-07 14:21:47 +02007924 || (use_vpeekc && (c = vpeekc()) != NUL && c != Ctrl_C))
Bram Moolenaar586c70c2018-10-02 16:23:58 +02007925 && !got_int)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00007926 {
7927 update_topline_cursor();
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02007928#ifdef FEAT_TERMINAL
Bram Moolenaar655a82a2018-05-08 22:01:07 +02007929 if (may_use_terminal_loop && term_use_loop()
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02007930 && oa.op_type == OP_NOP && oa.regname == NUL
7931 && !VIsual_active)
7932 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007933 // If terminal_loop() returns OK we got a key that is handled
7934 // in Normal model. With FAIL we first need to position the
7935 // cursor and the screen needs to be redrawn.
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02007936 if (terminal_loop(TRUE) == OK)
7937 normal_cmd(&oa, TRUE);
7938 }
7939 else
7940#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007941 // execute a Normal mode cmd
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02007942 normal_cmd(&oa, TRUE);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00007943 }
7944}
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00007945
Bram Moolenaar071d4272004-06-13 20:20:40 +00007946#ifdef FEAT_FIND_ID
7947 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007948ex_checkpath(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007949{
7950 find_pattern_in_path(NULL, 0, 0, FALSE, FALSE, CHECK_PATH, 1L,
7951 eap->forceit ? ACTION_SHOW_ALL : ACTION_SHOW,
7952 (linenr_T)1, (linenr_T)MAXLNUM);
7953}
7954
Bram Moolenaar4033c552017-09-16 20:54:51 +02007955#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007956/*
7957 * ":psearch"
7958 */
7959 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007960ex_psearch(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007961{
7962 g_do_tagpreview = p_pvh;
7963 ex_findpat(eap);
7964 g_do_tagpreview = 0;
7965}
7966#endif
7967
7968 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007969ex_findpat(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007970{
7971 int whole = TRUE;
7972 long n;
7973 char_u *p;
7974 int action;
7975
7976 switch (cmdnames[eap->cmdidx].cmd_name[2])
7977 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007978 case 'e': // ":psearch", ":isearch" and ":dsearch"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007979 if (cmdnames[eap->cmdidx].cmd_name[0] == 'p')
7980 action = ACTION_GOTO;
7981 else
7982 action = ACTION_SHOW;
7983 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007984 case 'i': // ":ilist" and ":dlist"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007985 action = ACTION_SHOW_ALL;
7986 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007987 case 'u': // ":ijump" and ":djump"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007988 action = ACTION_GOTO;
7989 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007990 default: // ":isplit" and ":dsplit"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007991 action = ACTION_SPLIT;
7992 break;
7993 }
7994
7995 n = 1;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007996 if (vim_isdigit(*eap->arg)) // get count
Bram Moolenaar071d4272004-06-13 20:20:40 +00007997 {
7998 n = getdigits(&eap->arg);
7999 eap->arg = skipwhite(eap->arg);
8000 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008001 if (*eap->arg == '/') // Match regexp, not just whole words
Bram Moolenaar071d4272004-06-13 20:20:40 +00008002 {
8003 whole = FALSE;
8004 ++eap->arg;
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02008005 p = skip_regexp(eap->arg, '/', p_magic);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008006 if (*p)
8007 {
8008 *p++ = NUL;
8009 p = skipwhite(p);
8010
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008011 // Check for trailing illegal characters
Bram Moolenaarfaac4102020-04-20 17:46:14 +02008012 if (!ends_excmd2(eap->arg, p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008013 eap->errmsg = e_trailing;
8014 else
8015 eap->nextcmd = check_nextcmd(p);
8016 }
8017 }
8018 if (!eap->skip)
8019 find_pattern_in_path(eap->arg, 0, (int)STRLEN(eap->arg),
8020 whole, !eap->forceit,
8021 *eap->cmd == 'd' ? FIND_DEFINE : FIND_ANY,
8022 n, action, eap->line1, eap->line2);
8023}
8024#endif
8025
Bram Moolenaar071d4272004-06-13 20:20:40 +00008026
Bram Moolenaar4033c552017-09-16 20:54:51 +02008027#ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00008028/*
8029 * ":ptag", ":ptselect", ":ptjump", ":ptnext", etc.
8030 */
8031 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008032ex_ptag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008033{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008034 g_do_tagpreview = p_pvh; // will be reset to 0 in ex_tag_cmd()
Bram Moolenaar071d4272004-06-13 20:20:40 +00008035 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
8036}
8037
8038/*
8039 * ":pedit"
8040 */
8041 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008042ex_pedit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008043{
8044 win_T *curwin_save = curwin;
8045
Bram Moolenaar3a2505c2020-03-09 16:40:41 +01008046 if (ERROR_IF_ANY_POPUP_WINDOW)
8047 return;
8048
Bram Moolenaar026587b2019-08-17 15:08:00 +02008049 // Open the preview window or popup and make it the current window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008050 g_do_tagpreview = p_pvh;
Bram Moolenaar576a4a62019-08-18 15:25:17 +02008051 prepare_tagpreview(TRUE, TRUE, FALSE);
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02008052
Bram Moolenaar026587b2019-08-17 15:08:00 +02008053 // Edit the file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008054 do_exedit(eap, NULL);
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02008055
Bram Moolenaar071d4272004-06-13 20:20:40 +00008056 if (curwin != curwin_save && win_valid(curwin_save))
8057 {
Bram Moolenaar026587b2019-08-17 15:08:00 +02008058 // Return cursor to where we were
Bram Moolenaar071d4272004-06-13 20:20:40 +00008059 validate_cursor();
8060 redraw_later(VALID);
8061 win_enter(curwin_save, TRUE);
8062 }
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01008063# ifdef FEAT_PROP_POPUP
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02008064 else if (WIN_IS_POPUP(curwin))
8065 {
8066 // can't keep focus in popup window
8067 win_enter(firstwin, TRUE);
8068 }
8069# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008070 g_do_tagpreview = 0;
8071}
Bram Moolenaar4033c552017-09-16 20:54:51 +02008072#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008073
8074/*
8075 * ":stag", ":stselect" and ":stjump".
8076 */
8077 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008078ex_stag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008079{
8080 postponed_split = -1;
8081 postponed_split_flags = cmdmod.split;
Bram Moolenaard326ce82007-03-11 14:48:29 +00008082 postponed_split_tab = cmdmod.tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008083 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
8084 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00008085 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008086}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008087
8088/*
8089 * ":tag", ":tselect", ":tjump", ":tnext", etc.
8090 */
8091 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008092ex_tag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008093{
8094 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name);
8095}
8096
8097 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008098ex_tag_cmd(exarg_T *eap, char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008099{
8100 int cmd;
8101
8102 switch (name[1])
8103 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008104 case 'j': cmd = DT_JUMP; // ":tjump"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008105 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008106 case 's': cmd = DT_SELECT; // ":tselect"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008107 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008108 case 'p': cmd = DT_PREV; // ":tprevious"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008109 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008110 case 'N': cmd = DT_PREV; // ":tNext"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008111 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008112 case 'n': cmd = DT_NEXT; // ":tnext"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008113 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008114 case 'o': cmd = DT_POP; // ":pop"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008115 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008116 case 'f': // ":tfirst"
8117 case 'r': cmd = DT_FIRST; // ":trewind"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008118 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008119 case 'l': cmd = DT_LAST; // ":tlast"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008120 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008121 default: // ":tag"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008122#ifdef FEAT_CSCOPE
Bram Moolenaar7c94c262008-06-20 09:11:34 +00008123 if (p_cst && *eap->arg != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008124 {
Bram Moolenaard4db7712016-11-12 19:16:46 +01008125 ex_cstag(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008126 return;
8127 }
8128#endif
8129 cmd = DT_TAG;
8130 break;
8131 }
8132
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00008133 if (name[0] == 'l')
8134 {
8135#ifndef FEAT_QUICKFIX
8136 ex_ni(eap);
8137 return;
8138#else
8139 cmd = DT_LTAG;
8140#endif
8141 }
8142
Bram Moolenaar071d4272004-06-13 20:20:40 +00008143 do_tag(eap->arg, cmd, eap->addr_count > 0 ? (int)eap->line2 : 1,
8144 eap->forceit, TRUE);
8145}
8146
8147/*
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008148 * Check "str" for starting with a special cmdline variable.
8149 * If found return one of the SPEC_ values and set "*usedlen" to the length of
8150 * the variable. Otherwise return -1 and "*usedlen" is unchanged.
8151 */
8152 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008153find_cmdline_var(char_u *src, int *usedlen)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008154{
8155 int len;
8156 int i;
Bram Moolenaar8f0b2d42009-05-16 14:41:10 +00008157 static char *(spec_str[]) = {
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008158 "%",
8159#define SPEC_PERC 0
8160 "#",
Bram Moolenaar65f08472017-09-10 18:16:20 +02008161#define SPEC_HASH (SPEC_PERC + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008162 "<cword>", // cursor word
Bram Moolenaar65f08472017-09-10 18:16:20 +02008163#define SPEC_CWORD (SPEC_HASH + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008164 "<cWORD>", // cursor WORD
Bram Moolenaar65f08472017-09-10 18:16:20 +02008165#define SPEC_CCWORD (SPEC_CWORD + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008166 "<cexpr>", // expr under cursor
Bram Moolenaar65f08472017-09-10 18:16:20 +02008167#define SPEC_CEXPR (SPEC_CCWORD + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008168 "<cfile>", // cursor path name
Bram Moolenaar65f08472017-09-10 18:16:20 +02008169#define SPEC_CFILE (SPEC_CEXPR + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008170 "<sfile>", // ":so" file name
Bram Moolenaar65f08472017-09-10 18:16:20 +02008171#define SPEC_SFILE (SPEC_CFILE + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008172 "<slnum>", // ":so" file line number
Bram Moolenaar65f08472017-09-10 18:16:20 +02008173#define SPEC_SLNUM (SPEC_SFILE + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008174 "<afile>", // autocommand file name
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008175#define SPEC_AFILE (SPEC_SLNUM + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008176 "<abuf>", // autocommand buffer number
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008177#define SPEC_ABUF (SPEC_AFILE + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008178 "<amatch>", // autocommand match name
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01008179#define SPEC_AMATCH (SPEC_ABUF + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008180 "<sflnum>", // script file line number
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008181#define SPEC_SFLNUM (SPEC_AMATCH + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008182#ifdef FEAT_CLIENTSERVER
8183 "<client>"
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008184# define SPEC_CLIENT (SPEC_SFLNUM + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008185#endif
8186 };
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008187
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00008188 for (i = 0; i < (int)(sizeof(spec_str) / sizeof(char *)); ++i)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008189 {
8190 len = (int)STRLEN(spec_str[i]);
8191 if (STRNCMP(src, spec_str[i], len) == 0)
8192 {
8193 *usedlen = len;
8194 return i;
8195 }
8196 }
8197 return -1;
8198}
8199
8200/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008201 * Evaluate cmdline variables.
8202 *
8203 * change '%' to curbuf->b_ffname
8204 * '#' to curwin->w_altfile
8205 * '<cword>' to word under the cursor
8206 * '<cWORD>' to WORD under the cursor
Bram Moolenaar8071cb22019-07-12 17:58:01 +02008207 * '<cexpr>' to C-expression under the cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00008208 * '<cfile>' to path name under the cursor
8209 * '<sfile>' to sourced file name
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01008210 * '<slnum>' to sourced file line number
Bram Moolenaar071d4272004-06-13 20:20:40 +00008211 * '<afile>' to file name for autocommand
8212 * '<abuf>' to buffer number for autocommand
8213 * '<amatch>' to matching name for autocommand
8214 *
8215 * When an error is detected, "errormsg" is set to a non-NULL pointer (may be
8216 * "" for error without a message) and NULL is returned.
8217 * Returns an allocated string if a valid match was found.
8218 * Returns NULL if no match was found. "usedlen" then still contains the
8219 * number of characters to skip.
8220 */
8221 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008222eval_vars(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008223 char_u *src, // pointer into commandline
8224 char_u *srcstart, // beginning of valid memory for src
8225 int *usedlen, // characters after src that are used
8226 linenr_T *lnump, // line number for :e command, or NULL
8227 char **errormsg, // pointer to error message
8228 int *escaped) // return value has escaped white space (can
8229 // be NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008230{
8231 int i;
8232 char_u *s;
8233 char_u *result;
8234 char_u *resultbuf = NULL;
8235 int resultlen;
8236 buf_T *buf;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008237 int valid = VALID_HEAD + VALID_PATH; // assume valid result
Bram Moolenaar071d4272004-06-13 20:20:40 +00008238 int spec_idx;
Bram Moolenaarfd249462018-07-28 16:14:30 +02008239 int tilde_file = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008240 int skip_mod = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008241 char_u strbuf[30];
Bram Moolenaar071d4272004-06-13 20:20:40 +00008242
8243 *errormsg = NULL;
Bram Moolenaar63b92542007-03-27 14:57:09 +00008244 if (escaped != NULL)
8245 *escaped = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008246
8247 /*
8248 * Check if there is something to do.
8249 */
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008250 spec_idx = find_cmdline_var(src, usedlen);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008251 if (spec_idx < 0) // no match
Bram Moolenaar071d4272004-06-13 20:20:40 +00008252 {
8253 *usedlen = 1;
8254 return NULL;
8255 }
8256
8257 /*
8258 * Skip when preceded with a backslash "\%" and "\#".
8259 * Note: In "\\%" the % is also not recognized!
8260 */
8261 if (src > srcstart && src[-1] == '\\')
8262 {
8263 *usedlen = 0;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008264 STRMOVE(src - 1, src); // remove backslash
Bram Moolenaar071d4272004-06-13 20:20:40 +00008265 return NULL;
8266 }
8267
8268 /*
8269 * word or WORD under cursor
8270 */
Bram Moolenaar65f08472017-09-10 18:16:20 +02008271 if (spec_idx == SPEC_CWORD || spec_idx == SPEC_CCWORD
8272 || spec_idx == SPEC_CEXPR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008273 {
Bram Moolenaar65f08472017-09-10 18:16:20 +02008274 resultlen = find_ident_under_cursor(&result,
8275 spec_idx == SPEC_CWORD ? (FIND_IDENT | FIND_STRING)
8276 : spec_idx == SPEC_CEXPR ? (FIND_IDENT | FIND_STRING | FIND_EVAL)
8277 : FIND_STRING);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008278 if (resultlen == 0)
8279 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008280 *errormsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +00008281 return NULL;
8282 }
8283 }
8284
8285 /*
8286 * '#': Alternate file name
8287 * '%': Current file name
8288 * File name under the cursor
8289 * File name for autocommand
8290 * and following modifiers
8291 */
8292 else
8293 {
8294 switch (spec_idx)
8295 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008296 case SPEC_PERC: // '%': current file
Bram Moolenaar071d4272004-06-13 20:20:40 +00008297 if (curbuf->b_fname == NULL)
8298 {
8299 result = (char_u *)"";
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008300 valid = 0; // Must have ":p:h" to be valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00008301 }
8302 else
Bram Moolenaar00136dc2018-07-25 21:19:13 +02008303 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008304 result = curbuf->b_fname;
Bram Moolenaar00136dc2018-07-25 21:19:13 +02008305 tilde_file = STRCMP(result, "~") == 0;
8306 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008307 break;
8308
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008309 case SPEC_HASH: // '#' or "#99": alternate file
8310 if (src[1] == '#') // "##": the argument list
Bram Moolenaar071d4272004-06-13 20:20:40 +00008311 {
8312 result = arg_all();
8313 resultbuf = result;
8314 *usedlen = 2;
Bram Moolenaar63b92542007-03-27 14:57:09 +00008315 if (escaped != NULL)
8316 *escaped = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008317 skip_mod = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008318 break;
8319 }
8320 s = src + 1;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008321 if (*s == '<') // "#<99" uses v:oldfiles
Bram Moolenaard812df62008-11-09 12:46:09 +00008322 ++s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008323 i = (int)getdigits(&s);
Bram Moolenaarc312b8b2017-10-28 17:53:04 +02008324 if (s == src + 2 && src[1] == '-')
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008325 // just a minus sign, don't skip over it
Bram Moolenaarc312b8b2017-10-28 17:53:04 +02008326 s--;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008327 *usedlen = (int)(s - src); // length of what we expand
Bram Moolenaar071d4272004-06-13 20:20:40 +00008328
Bram Moolenaarc312b8b2017-10-28 17:53:04 +02008329 if (src[1] == '<' && i != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008330 {
Bram Moolenaard812df62008-11-09 12:46:09 +00008331 if (*usedlen < 2)
8332 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008333 // Should we give an error message for #<text?
Bram Moolenaard812df62008-11-09 12:46:09 +00008334 *usedlen = 1;
8335 return NULL;
8336 }
8337#ifdef FEAT_EVAL
8338 result = list_find_str(get_vim_var_list(VV_OLDFILES),
8339 (long)i);
8340 if (result == NULL)
8341 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008342 *errormsg = "";
Bram Moolenaard812df62008-11-09 12:46:09 +00008343 return NULL;
8344 }
8345#else
Bram Moolenaar8e481e82019-01-15 20:07:48 +01008346 *errormsg = _("E809: #< is not available without the +eval feature");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008347 return NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +00008348#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008349 }
8350 else
Bram Moolenaard812df62008-11-09 12:46:09 +00008351 {
Bram Moolenaarc312b8b2017-10-28 17:53:04 +02008352 if (i == 0 && src[1] == '<' && *usedlen > 1)
8353 *usedlen = 1;
Bram Moolenaard812df62008-11-09 12:46:09 +00008354 buf = buflist_findnr(i);
8355 if (buf == NULL)
8356 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008357 *errormsg = _("E194: No alternate file name to substitute for '#'");
Bram Moolenaard812df62008-11-09 12:46:09 +00008358 return NULL;
8359 }
8360 if (lnump != NULL)
8361 *lnump = ECMD_LAST;
8362 if (buf->b_fname == NULL)
8363 {
8364 result = (char_u *)"";
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008365 valid = 0; // Must have ":p:h" to be valid
Bram Moolenaard812df62008-11-09 12:46:09 +00008366 }
8367 else
Bram Moolenaar00136dc2018-07-25 21:19:13 +02008368 {
Bram Moolenaard812df62008-11-09 12:46:09 +00008369 result = buf->b_fname;
Bram Moolenaar00136dc2018-07-25 21:19:13 +02008370 tilde_file = STRCMP(result, "~") == 0;
8371 }
Bram Moolenaard812df62008-11-09 12:46:09 +00008372 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008373 break;
8374
8375#ifdef FEAT_SEARCHPATH
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008376 case SPEC_CFILE: // file name under cursor
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008377 result = file_name_at_cursor(FNAME_MESS|FNAME_HYP, 1L, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008378 if (result == NULL)
8379 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008380 *errormsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +00008381 return NULL;
8382 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008383 resultbuf = result; // remember allocated string
Bram Moolenaar071d4272004-06-13 20:20:40 +00008384 break;
8385#endif
8386
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008387 case SPEC_AFILE: // file name for autocommand
Bram Moolenaar071d4272004-06-13 20:20:40 +00008388 result = autocmd_fname;
Bram Moolenaarf6dad432008-09-18 19:29:58 +00008389 if (result != NULL && !autocmd_fname_full)
8390 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008391 // Still need to turn the fname into a full path. It is
8392 // postponed to avoid a delay when <afile> is not used.
Bram Moolenaarf6dad432008-09-18 19:29:58 +00008393 autocmd_fname_full = TRUE;
8394 result = FullName_save(autocmd_fname, FALSE);
8395 vim_free(autocmd_fname);
8396 autocmd_fname = result;
8397 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008398 if (result == NULL)
8399 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008400 *errormsg = _("E495: no autocommand file name to substitute for \"<afile>\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008401 return NULL;
8402 }
Bram Moolenaara0174af2008-01-02 20:08:25 +00008403 result = shorten_fname1(result);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008404 break;
8405
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008406 case SPEC_ABUF: // buffer number for autocommand
Bram Moolenaar071d4272004-06-13 20:20:40 +00008407 if (autocmd_bufnr <= 0)
8408 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008409 *errormsg = _("E496: no autocommand buffer number to substitute for \"<abuf>\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008410 return NULL;
8411 }
8412 sprintf((char *)strbuf, "%d", autocmd_bufnr);
8413 result = strbuf;
8414 break;
8415
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008416 case SPEC_AMATCH: // match name for autocommand
Bram Moolenaar071d4272004-06-13 20:20:40 +00008417 result = autocmd_match;
8418 if (result == NULL)
8419 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008420 *errormsg = _("E497: no autocommand match name to substitute for \"<amatch>\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008421 return NULL;
8422 }
8423 break;
8424
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008425 case SPEC_SFILE: // file name for ":so" command
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01008426 result = estack_sfile();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008427 if (result == NULL)
8428 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008429 *errormsg = _("E498: no :source file name to substitute for \"<sfile>\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008430 return NULL;
8431 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01008432 resultbuf = result; // remember allocated string
Bram Moolenaar071d4272004-06-13 20:20:40 +00008433 break;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008434
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008435 case SPEC_SLNUM: // line in file for ":so" command
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01008436 if (SOURCING_NAME == NULL || SOURCING_LNUM == 0)
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01008437 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008438 *errormsg = _("E842: no line number to use for \"<slnum>\"");
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01008439 return NULL;
8440 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01008441 sprintf((char *)strbuf, "%ld", SOURCING_LNUM);
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01008442 result = strbuf;
8443 break;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008444
8445#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008446 case SPEC_SFLNUM: // line in script file
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01008447 if (current_sctx.sc_lnum + SOURCING_LNUM == 0)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008448 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008449 *errormsg = _("E961: no line number to use for \"<sflnum>\"");
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008450 return NULL;
8451 }
8452 sprintf((char *)strbuf, "%ld",
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01008453 (long)(current_sctx.sc_lnum + SOURCING_LNUM));
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008454 result = strbuf;
8455 break;
8456#endif
8457
8458#ifdef FEAT_CLIENTSERVER
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008459 case SPEC_CLIENT: // Source of last submitted input
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008460 sprintf((char *)strbuf, PRINTF_HEX_LONG_U,
8461 (long_u)clientWindow);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008462 result = strbuf;
8463 break;
8464#endif
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008465
Bram Moolenaar9e0f6ec2017-05-16 09:36:54 +02008466 default:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008467 result = (char_u *)""; // avoid gcc warning
Bram Moolenaar9e0f6ec2017-05-16 09:36:54 +02008468 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008469 }
8470
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008471 resultlen = (int)STRLEN(result); // length of new string
8472 if (src[*usedlen] == '<') // remove the file name extension
Bram Moolenaar071d4272004-06-13 20:20:40 +00008473 {
8474 ++*usedlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008475 if ((s = vim_strrchr(result, '.')) != NULL && s >= gettail(result))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008476 resultlen = (int)(s - result);
8477 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008478 else if (!skip_mod)
8479 {
Bram Moolenaar00136dc2018-07-25 21:19:13 +02008480 valid |= modify_fname(src, tilde_file, usedlen, &result, &resultbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008481 &resultlen);
8482 if (result == NULL)
8483 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008484 *errormsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +00008485 return NULL;
8486 }
8487 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008488 }
8489
8490 if (resultlen == 0 || valid != VALID_HEAD + VALID_PATH)
8491 {
8492 if (valid != VALID_HEAD + VALID_PATH)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008493 // xgettext:no-c-format
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008494 *errormsg = _("E499: Empty file name for '%' or '#', only works with \":p:h\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008495 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008496 *errormsg = _("E500: Evaluates to an empty string");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008497 result = NULL;
8498 }
8499 else
8500 result = vim_strnsave(result, resultlen);
8501 vim_free(resultbuf);
8502 return result;
8503}
8504
8505/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008506 * Expand the <sfile> string in "arg".
8507 *
8508 * Returns an allocated string, or NULL for any error.
8509 */
8510 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008511expand_sfile(char_u *arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008512{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008513 char *errormsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008514 int len;
8515 char_u *result;
8516 char_u *newres;
8517 char_u *repl;
8518 int srclen;
8519 char_u *p;
8520
8521 result = vim_strsave(arg);
8522 if (result == NULL)
8523 return NULL;
8524
8525 for (p = result; *p; )
8526 {
8527 if (STRNCMP(p, "<sfile>", 7) != 0)
8528 ++p;
8529 else
8530 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008531 // replace "<sfile>" with the sourced file name, and do ":" stuff
Bram Moolenaar63b92542007-03-27 14:57:09 +00008532 repl = eval_vars(p, result, &srclen, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008533 if (errormsg != NULL)
8534 {
8535 if (*errormsg)
8536 emsg(errormsg);
8537 vim_free(result);
8538 return NULL;
8539 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008540 if (repl == NULL) // no match (cannot happen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008541 {
8542 p += srclen;
8543 continue;
8544 }
8545 len = (int)STRLEN(result) - srclen + (int)STRLEN(repl) + 1;
8546 newres = alloc(len);
8547 if (newres == NULL)
8548 {
8549 vim_free(repl);
8550 vim_free(result);
8551 return NULL;
8552 }
8553 mch_memmove(newres, result, (size_t)(p - result));
8554 STRCPY(newres + (p - result), repl);
8555 len = (int)STRLEN(newres);
8556 STRCAT(newres, p + srclen);
8557 vim_free(repl);
8558 vim_free(result);
8559 result = newres;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008560 p = newres + len; // continue after the match
Bram Moolenaar071d4272004-06-13 20:20:40 +00008561 }
8562 }
8563
8564 return result;
8565}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008566
Bram Moolenaar071d4272004-06-13 20:20:40 +00008567#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG) || defined(PROTO)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00008568/*
Bram Moolenaard9462e32011-04-11 21:35:11 +02008569 * Make a dialog message in "buff[DIALOG_MSG_SIZE]".
Bram Moolenaarb765d632005-06-07 21:00:02 +00008570 * "format" must contain "%s".
Bram Moolenaar9c13b352005-05-19 20:53:52 +00008571 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008572 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008573dialog_msg(char_u *buff, char *format, char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008574{
Bram Moolenaar071d4272004-06-13 20:20:40 +00008575 if (fname == NULL)
8576 fname = (char_u *)_("Untitled");
Bram Moolenaard9462e32011-04-11 21:35:11 +02008577 vim_snprintf((char *)buff, DIALOG_MSG_SIZE, format, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008578}
8579#endif
8580
8581/*
8582 * ":behave {mswin,xterm}"
8583 */
8584 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008585ex_behave(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008586{
8587 if (STRCMP(eap->arg, "mswin") == 0)
8588 {
8589 set_option_value((char_u *)"selection", 0L, (char_u *)"exclusive", 0);
8590 set_option_value((char_u *)"selectmode", 0L, (char_u *)"mouse,key", 0);
8591 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"popup", 0);
8592 set_option_value((char_u *)"keymodel", 0L,
8593 (char_u *)"startsel,stopsel", 0);
8594 }
8595 else if (STRCMP(eap->arg, "xterm") == 0)
8596 {
8597 set_option_value((char_u *)"selection", 0L, (char_u *)"inclusive", 0);
8598 set_option_value((char_u *)"selectmode", 0L, (char_u *)"", 0);
8599 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"extend", 0);
8600 set_option_value((char_u *)"keymodel", 0L, (char_u *)"", 0);
8601 }
8602 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008603 semsg(_(e_invarg2), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008604}
8605
Bram Moolenaar071d4272004-06-13 20:20:40 +00008606static int filetype_detect = FALSE;
8607static int filetype_plugin = FALSE;
8608static int filetype_indent = FALSE;
8609
8610/*
8611 * ":filetype [plugin] [indent] {on,off,detect}"
8612 * on: Load the filetype.vim file to install autocommands for file types.
8613 * off: Load the ftoff.vim file to remove all autocommands for file types.
8614 * plugin on: load filetype.vim and ftplugin.vim
8615 * plugin off: load ftplugof.vim
8616 * indent on: load filetype.vim and indent.vim
8617 * indent off: load indoff.vim
8618 */
8619 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008620ex_filetype(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008621{
8622 char_u *arg = eap->arg;
8623 int plugin = FALSE;
8624 int indent = FALSE;
8625
8626 if (*eap->arg == NUL)
8627 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008628 // Print current status.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008629 smsg("filetype detection:%s plugin:%s indent:%s",
Bram Moolenaar071d4272004-06-13 20:20:40 +00008630 filetype_detect ? "ON" : "OFF",
8631 filetype_plugin ? (filetype_detect ? "ON" : "(on)") : "OFF",
8632 filetype_indent ? (filetype_detect ? "ON" : "(on)") : "OFF");
8633 return;
8634 }
8635
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008636 // Accept "plugin" and "indent" in any order.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008637 for (;;)
8638 {
8639 if (STRNCMP(arg, "plugin", 6) == 0)
8640 {
8641 plugin = TRUE;
8642 arg = skipwhite(arg + 6);
8643 continue;
8644 }
8645 if (STRNCMP(arg, "indent", 6) == 0)
8646 {
8647 indent = TRUE;
8648 arg = skipwhite(arg + 6);
8649 continue;
8650 }
8651 break;
8652 }
8653 if (STRCMP(arg, "on") == 0 || STRCMP(arg, "detect") == 0)
8654 {
8655 if (*arg == 'o' || !filetype_detect)
8656 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01008657 source_runtime((char_u *)FILETYPE_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008658 filetype_detect = TRUE;
8659 if (plugin)
8660 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01008661 source_runtime((char_u *)FTPLUGIN_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008662 filetype_plugin = TRUE;
8663 }
8664 if (indent)
8665 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01008666 source_runtime((char_u *)INDENT_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008667 filetype_indent = TRUE;
8668 }
8669 }
8670 if (*arg == 'd')
8671 {
Bram Moolenaar1610d052016-06-09 22:53:01 +02008672 (void)do_doautocmd((char_u *)"filetypedetect BufRead", TRUE, NULL);
Bram Moolenaara3227e22006-03-08 21:32:40 +00008673 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008674 }
8675 }
8676 else if (STRCMP(arg, "off") == 0)
8677 {
8678 if (plugin || indent)
8679 {
8680 if (plugin)
8681 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01008682 source_runtime((char_u *)FTPLUGOF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008683 filetype_plugin = FALSE;
8684 }
8685 if (indent)
8686 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01008687 source_runtime((char_u *)INDOFF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008688 filetype_indent = FALSE;
8689 }
8690 }
8691 else
8692 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01008693 source_runtime((char_u *)FTOFF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008694 filetype_detect = FALSE;
8695 }
8696 }
8697 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008698 semsg(_(e_invarg2), arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008699}
8700
8701/*
Bram Moolenaar3e545692017-06-04 19:00:32 +02008702 * ":setfiletype [FALLBACK] {name}"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008703 */
8704 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008705ex_setfiletype(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008706{
8707 if (!did_filetype)
Bram Moolenaar3e545692017-06-04 19:00:32 +02008708 {
8709 char_u *arg = eap->arg;
8710
8711 if (STRNCMP(arg, "FALLBACK ", 9) == 0)
8712 arg += 9;
8713
8714 set_option_value((char_u *)"filetype", 0L, arg, OPT_LOCAL);
8715 if (arg != eap->arg)
8716 did_filetype = FALSE;
8717 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008718}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008719
Bram Moolenaar071d4272004-06-13 20:20:40 +00008720 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008721ex_digraphs(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008722{
8723#ifdef FEAT_DIGRAPHS
8724 if (*eap->arg != NUL)
8725 putdigraph(eap->arg);
8726 else
Bram Moolenaareae8ae12018-12-14 18:53:02 +01008727 listdigraphs(eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008728#else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008729 emsg(_("E196: No digraphs in this version"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008730#endif
8731}
8732
Bram Moolenaar451fc7b2018-04-27 22:53:07 +02008733#if defined(FEAT_SEARCH_EXTRA) || defined(PROTO)
8734 void
8735set_no_hlsearch(int flag)
8736{
8737 no_hlsearch = flag;
8738# ifdef FEAT_EVAL
8739 set_vim_var_nr(VV_HLSEARCH, !no_hlsearch && p_hls);
8740# endif
8741}
8742
Bram Moolenaar071d4272004-06-13 20:20:40 +00008743/*
8744 * ":nohlsearch"
8745 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008746 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008747ex_nohlsearch(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008748{
Bram Moolenaar451fc7b2018-04-27 22:53:07 +02008749 set_no_hlsearch(TRUE);
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00008750 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008751}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008752#endif
8753
8754#ifdef FEAT_CRYPT
8755/*
8756 * ":X": Get crypt key
8757 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008758 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008759ex_X(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008760{
Bram Moolenaar3a0c9082014-11-12 15:15:42 +01008761 crypt_check_current_method();
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02008762 (void)crypt_get_key(TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008763}
8764#endif
8765
8766#ifdef FEAT_FOLDING
8767 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008768ex_fold(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008769{
8770 if (foldManualAllowed(TRUE))
8771 foldCreate(eap->line1, eap->line2);
8772}
8773
8774 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008775ex_foldopen(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008776{
8777 opFoldRange(eap->line1, eap->line2, eap->cmdidx == CMD_foldopen,
8778 eap->forceit, FALSE);
8779}
8780
8781 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008782ex_folddo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008783{
8784 linenr_T lnum;
8785
Bram Moolenaar4facea32019-10-12 20:17:40 +02008786# ifdef FEAT_CLIPBOARD
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02008787 start_global_changes();
Bram Moolenaar4facea32019-10-12 20:17:40 +02008788# endif
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02008789
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008790 // First set the marks for all lines closed/open.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008791 for (lnum = eap->line1; lnum <= eap->line2; ++lnum)
8792 if (hasFolding(lnum, NULL, NULL) == (eap->cmdidx == CMD_folddoclosed))
8793 ml_setmarked(lnum);
8794
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008795 // Execute the command on the marked lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008796 global_exe(eap->arg);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008797 ml_clearmarked(); // clear rest of the marks
Bram Moolenaar4facea32019-10-12 20:17:40 +02008798# ifdef FEAT_CLIPBOARD
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02008799 end_global_changes();
Bram Moolenaar4facea32019-10-12 20:17:40 +02008800# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008801}
8802#endif
Bram Moolenaarf5291f32017-09-14 22:55:37 +02008803
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008804#if defined(FEAT_QUICKFIX) || defined(PROTO)
Bram Moolenaar39665952018-08-15 20:59:48 +02008805/*
8806 * Returns TRUE if the supplied Ex cmdidx is for a location list command
8807 * instead of a quickfix command.
8808 */
8809 int
8810is_loclist_cmd(int cmdidx)
8811{
Bram Moolenaar74c8be22018-08-23 22:51:40 +02008812 if (cmdidx < 0 || cmdidx >= CMD_SIZE)
Bram Moolenaar39665952018-08-15 20:59:48 +02008813 return FALSE;
8814 return cmdnames[cmdidx].cmd_name[0] == 'l';
8815}
8816#endif
8817
Bram Moolenaar4facea32019-10-12 20:17:40 +02008818#if defined(FEAT_TIMERS) || defined(PROTO)
Bram Moolenaarf5291f32017-09-14 22:55:37 +02008819 int
8820get_pressedreturn(void)
8821{
8822 return ex_pressedreturn;
8823}
8824
8825 void
8826set_pressedreturn(int val)
8827{
8828 ex_pressedreturn = val;
8829}
8830#endif