blob: 310934ed71efe274caf007675d65fe33bca1c39f [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 Moolenaar20b23c62020-08-20 15:25:00 +020023static char_u *do_one_cmd(char_u **, int, cstack_T *, char_u *(*fgetline)(int, void *, int, getline_opt_T), void *cookie);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024#else
Bram Moolenaar20b23c62020-08-20 15:25:00 +020025static char_u *do_one_cmd(char_u **, int, char_u *(*fgetline)(int, void *, int, getline_opt_T), 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 Moolenaarf28dbce2016-01-29 22:03:47 +010028static void append_command(char_u *cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000029
Bram Moolenaar071d4272004-06-13 20:20:40 +000030#ifndef FEAT_MENU
31# define ex_emenu ex_ni
32# define ex_menu ex_ni
33# define ex_menutranslate ex_ni
34#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010035static void ex_autocmd(exarg_T *eap);
36static void ex_doautocmd(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010037static void ex_bunload(exarg_T *eap);
38static void ex_buffer(exarg_T *eap);
39static void ex_bmodified(exarg_T *eap);
40static void ex_bnext(exarg_T *eap);
41static void ex_bprevious(exarg_T *eap);
42static void ex_brewind(exarg_T *eap);
43static void ex_blast(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010044static char_u *getargcmd(char_u **);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010045static int getargopt(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +000046#ifndef FEAT_QUICKFIX
47# define ex_make ex_ni
Bram Moolenaar86b68352004-12-27 21:59:20 +000048# define ex_cbuffer ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +000049# define ex_cc ex_ni
50# define ex_cnext ex_ni
Bram Moolenaar3ff33112019-05-03 21:56:35 +020051# define ex_cbelow ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +000052# define ex_cfile ex_ni
53# define qf_list ex_ni
54# define qf_age ex_ni
Bram Moolenaarf6acffb2016-07-16 16:54:24 +020055# define qf_history ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +000056# define ex_helpgrep ex_ni
Bram Moolenaar86b68352004-12-27 21:59:20 +000057# define ex_vimgrep ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +000058#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +020059#if !defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +000060# define ex_cclose ex_ni
61# define ex_copen ex_ni
62# define ex_cwindow ex_ni
Bram Moolenaardcb17002016-07-07 18:58:59 +020063# define ex_cbottom ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +000064#endif
Bram Moolenaar1e015462005-09-25 22:16:38 +000065#if !defined(FEAT_QUICKFIX) || !defined(FEAT_EVAL)
66# define ex_cexpr ex_ni
67#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000068
Bram Moolenaarc2af0af2020-08-23 21:06:02 +020069static linenr_T default_address(exarg_T *eap);
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 Moolenaarc2af0af2020-08-23 21:06:02 +020071static void address_default_all(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010072static void get_flags(exarg_T *eap);
Bram Moolenaar85363ab2010-07-18 13:58:26 +020073#if !defined(FEAT_PERL) \
74 || !defined(FEAT_PYTHON) || !defined(FEAT_PYTHON3) \
75 || !defined(FEAT_TCL) \
76 || !defined(FEAT_RUBY) \
77 || !defined(FEAT_LUA) \
78 || !defined(FEAT_MZSCHEME)
Bram Moolenaar7bb75552007-07-16 18:39:49 +000079# define HAVE_EX_SCRIPT_NI
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010080static void ex_script_ni(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +000081#endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010082static char *invalid_range(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010083static void correct_range(exarg_T *eap);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000084#ifdef FEAT_QUICKFIX
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010085static char_u *replace_makeprg(exarg_T *eap, char_u *p, char_u **cmdlinep);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000086#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010087static char_u *repl_cmdline(exarg_T *eap, char_u *src, int srclen, char_u *repl, char_u **cmdlinep);
88static void ex_highlight(exarg_T *eap);
89static void ex_colorscheme(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010090static void ex_cquit(exarg_T *eap);
91static void ex_quit_all(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010092static void ex_close(exarg_T *eap);
93static void ex_win_close(int forceit, win_T *win, tabpage_T *tp);
94static void ex_only(exarg_T *eap);
95static void ex_resize(exarg_T *eap);
96static void ex_stag(exarg_T *eap);
97static void ex_tabclose(exarg_T *eap);
98static void ex_tabonly(exarg_T *eap);
99static void ex_tabnext(exarg_T *eap);
100static void ex_tabmove(exarg_T *eap);
101static void ex_tabs(exarg_T *eap);
Bram Moolenaar4033c552017-09-16 20:54:51 +0200102#if defined(FEAT_QUICKFIX)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100103static void ex_pclose(exarg_T *eap);
104static void ex_ptag(exarg_T *eap);
105static void ex_pedit(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000106#else
107# define ex_pclose ex_ni
108# define ex_ptag ex_ni
109# define ex_pedit ex_ni
110#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100111static void ex_hide(exarg_T *eap);
112static void ex_stop(exarg_T *eap);
113static void ex_exit(exarg_T *eap);
114static void ex_print(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000115#ifdef FEAT_BYTEOFF
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100116static void ex_goto(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000117#else
118# define ex_goto ex_ni
119#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100120static void ex_shell(exarg_T *eap);
121static void ex_preserve(exarg_T *eap);
122static void ex_recover(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100123static void ex_mode(exarg_T *eap);
124static void ex_wrongmodifier(exarg_T *eap);
125static void ex_find(exarg_T *eap);
126static void ex_open(exarg_T *eap);
127static void ex_edit(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000128#ifndef FEAT_GUI
129# define ex_gui ex_nogui
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100130static void ex_nogui(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000131#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +0100132#if defined(FEAT_GUI_MSWIN) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100133static void ex_tearoff(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000134#else
135# define ex_tearoff ex_ni
136#endif
Bram Moolenaar29a2c082018-03-05 21:06:23 +0100137#if (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK) \
138 || defined(FEAT_TERM_POPUP_MENU)) && defined(FEAT_MENU)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100139static void ex_popup(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000140#else
141# define ex_popup ex_ni
142#endif
143#ifndef FEAT_GUI_MSWIN
144# define ex_simalt ex_ni
145#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +0000146#if !defined(FEAT_GUI_MSWIN) && !defined(FEAT_GUI_GTK) && !defined(FEAT_GUI_MOTIF)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000147# define gui_mch_find_dialog ex_ni
148# define gui_mch_replace_dialog ex_ni
149#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +0000150#if !defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000151# define ex_helpfind ex_ni
152#endif
153#ifndef FEAT_CSCOPE
Bram Moolenaard4db7712016-11-12 19:16:46 +0100154# define ex_cscope ex_ni
155# define ex_scscope ex_ni
156# define ex_cstag ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000157#endif
158#ifndef FEAT_SYN_HL
159# define ex_syntax ex_ni
Bram Moolenaar860cae12010-06-05 23:22:07 +0200160# define ex_ownsyntax ex_ni
Bram Moolenaarf71a3db2006-03-12 21:50:18 +0000161#endif
Bram Moolenaarf7512552013-06-06 14:55:19 +0200162#if !defined(FEAT_SYN_HL) || !defined(FEAT_PROFILE)
Bram Moolenaar8a7f5a22013-06-06 14:01:46 +0200163# define ex_syntime ex_ni
164#endif
Bram Moolenaarf71a3db2006-03-12 21:50:18 +0000165#ifndef FEAT_SPELL
Bram Moolenaarb765d632005-06-07 21:00:02 +0000166# define ex_spell ex_ni
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000167# define ex_mkspell ex_ni
Bram Moolenaarf417f2b2005-06-23 22:29:21 +0000168# define ex_spelldump ex_ni
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000169# define ex_spellinfo ex_ni
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000170# define ex_spellrepall ex_ni
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000171#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200172#ifndef FEAT_PERSISTENT_UNDO
173# define ex_rundo ex_ni
174# define ex_wundo ex_ni
175#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200176#ifndef FEAT_LUA
177# define ex_lua ex_script_ni
178# define ex_luado ex_ni
179# define ex_luafile ex_ni
180#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000181#ifndef FEAT_MZSCHEME
182# define ex_mzscheme ex_script_ni
183# define ex_mzfile ex_ni
184#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000185#ifndef FEAT_PERL
186# define ex_perl ex_script_ni
187# define ex_perldo ex_ni
188#endif
189#ifndef FEAT_PYTHON
190# define ex_python ex_script_ni
Bram Moolenaard620aa92013-05-17 16:40:06 +0200191# define ex_pydo ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000192# define ex_pyfile ex_ni
193#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200194#ifndef FEAT_PYTHON3
Bram Moolenaar368373e2010-07-19 20:46:22 +0200195# define ex_py3 ex_script_ni
Bram Moolenaar3dab2802013-05-15 18:28:13 +0200196# define ex_py3do ex_ni
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200197# define ex_py3file ex_ni
198#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +0100199#if !defined(FEAT_PYTHON) && !defined(FEAT_PYTHON3)
200# define ex_pyx ex_script_ni
201# define ex_pyxdo ex_ni
202# define ex_pyxfile ex_ni
203#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000204#ifndef FEAT_TCL
205# define ex_tcl ex_script_ni
206# define ex_tcldo ex_ni
207# define ex_tclfile ex_ni
208#endif
209#ifndef FEAT_RUBY
210# define ex_ruby ex_script_ni
211# define ex_rubydo ex_ni
212# define ex_rubyfile ex_ni
213#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000214#ifndef FEAT_KEYMAP
215# define ex_loadkeymap ex_ni
216#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100217static void ex_swapname(exarg_T *eap);
218static void ex_syncbind(exarg_T *eap);
219static void ex_read(exarg_T *eap);
220static void ex_pwd(exarg_T *eap);
221static void ex_equal(exarg_T *eap);
222static void ex_sleep(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100223static void ex_winsize(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100224static void ex_wincmd(exarg_T *eap);
Bram Moolenaar843ee412004-06-30 16:16:41 +0000225#if defined(FEAT_GUI) || defined(UNIX) || defined(VMS) || defined(MSWIN)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100226static void ex_winpos(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000227#else
228# define ex_winpos ex_ni
229#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100230static void ex_operators(exarg_T *eap);
231static void ex_put(exarg_T *eap);
232static void ex_copymove(exarg_T *eap);
233static void ex_submagic(exarg_T *eap);
234static void ex_join(exarg_T *eap);
235static void ex_at(exarg_T *eap);
236static void ex_bang(exarg_T *eap);
237static void ex_undo(exarg_T *eap);
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200238#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100239static void ex_wundo(exarg_T *eap);
240static void ex_rundo(exarg_T *eap);
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200241#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100242static void ex_redo(exarg_T *eap);
243static void ex_later(exarg_T *eap);
244static void ex_redir(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100245static void ex_redrawstatus(exarg_T *eap);
Bram Moolenaare12bab32019-01-08 22:02:56 +0100246static void ex_redrawtabline(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100247static void close_redir(void);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100248static void ex_mark(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100249static void ex_startinsert(exarg_T *eap);
250static void ex_stopinsert(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000251#ifdef FEAT_FIND_ID
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100252static void ex_checkpath(exarg_T *eap);
253static void ex_findpat(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000254#else
255# define ex_findpat ex_ni
256# define ex_checkpath ex_ni
257#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +0200258#if defined(FEAT_FIND_ID) && defined(FEAT_QUICKFIX)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100259static void ex_psearch(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000260#else
261# define ex_psearch ex_ni
262#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100263static void ex_tag(exarg_T *eap);
264static void ex_tag_cmd(exarg_T *eap, char_u *name);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000265#ifndef FEAT_EVAL
Bram Moolenaare4ce8252019-08-04 15:30:16 +0200266# define ex_break ex_ni
267# define ex_breakadd ex_ni
268# define ex_breakdel ex_ni
269# define ex_breaklist ex_ni
270# define ex_call ex_ni
271# define ex_catch ex_ni
272# define ex_compiler ex_ni
Bram Moolenaare4ce8252019-08-04 15:30:16 +0200273# define ex_continue ex_ni
274# define ex_debug ex_ni
275# define ex_debuggreedy ex_ni
Bram Moolenaar822ba242020-05-24 23:00:18 +0200276# define ex_def ex_ni
277# define ex_defcompile ex_ni
Bram Moolenaare4ce8252019-08-04 15:30:16 +0200278# define ex_delfunction ex_ni
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100279# define ex_disassemble ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000280# define ex_echo ex_ni
281# define ex_echohl ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000282# define ex_else ex_ni
Bram Moolenaare4ce8252019-08-04 15:30:16 +0200283# define ex_endfunction ex_ni
284# define ex_endif ex_ni
285# define ex_endtry ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000286# define ex_endwhile ex_ni
Bram Moolenaare4ce8252019-08-04 15:30:16 +0200287# define ex_eval ex_ni
288# define ex_execute ex_ni
289# define ex_finally ex_ni
290# define ex_finish ex_ni
291# define ex_function ex_ni
292# define ex_if ex_ni
293# define ex_let ex_ni
Bram Moolenaard47f50b2020-09-26 15:20:42 +0200294# define ex_var ex_ni
Bram Moolenaare4ce8252019-08-04 15:30:16 +0200295# define ex_lockvar ex_ni
296# define ex_oldfiles ex_ni
297# define ex_options ex_ni
298# define ex_packadd ex_ni
299# define ex_packloadall ex_ni
300# define ex_return ex_ni
301# define ex_scriptnames ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000302# define ex_throw ex_ni
303# define ex_try ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000304# define ex_unlet ex_ni
Bram Moolenaar65c1b012005-01-31 19:02:28 +0000305# define ex_unlockvar ex_ni
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100306# define ex_vim9script ex_ni
Bram Moolenaare4ce8252019-08-04 15:30:16 +0200307# define ex_while ex_ni
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100308# define ex_import ex_ni
309# define ex_export ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000310#endif
Bram Moolenaar84538072019-07-28 14:15:42 +0200311#ifndef FEAT_SESSION
Bram Moolenaar071d4272004-06-13 20:20:40 +0000312# define ex_loadview ex_ni
313#endif
Bram Moolenaardefa0672019-07-21 19:25:37 +0200314#ifndef FEAT_VIMINFO
Bram Moolenaar071d4272004-06-13 20:20:40 +0000315# define ex_viminfo ex_ni
316#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100317static void ex_behave(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100318static void ex_filetype(exarg_T *eap);
319static void ex_setfiletype(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000320#ifndef FEAT_DIFF
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000321# define ex_diffoff ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000322# define ex_diffpatch ex_ni
323# define ex_diffgetput ex_ni
324# define ex_diffsplit ex_ni
325# define ex_diffthis ex_ni
326# define ex_diffupdate ex_ni
327#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100328static void ex_digraphs(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000329#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100330static void ex_nohlsearch(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000331#else
332# define ex_nohlsearch ex_ni
333# define ex_match ex_ni
334#endif
335#ifdef FEAT_CRYPT
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100336static void ex_X(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000337#else
338# define ex_X ex_ni
339#endif
340#ifdef FEAT_FOLDING
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100341static void ex_fold(exarg_T *eap);
342static void ex_foldopen(exarg_T *eap);
343static void ex_folddo(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000344#else
345# define ex_fold ex_ni
346# define ex_foldopen ex_ni
347# define ex_folddo ex_ni
348#endif
Bram Moolenaar13505972019-01-24 15:04:48 +0100349#if !(defined(HAVE_LOCALE_H) || defined(X_LOCALE))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000350# define ex_language ex_ni
351#endif
352#ifndef FEAT_SIGNS
353# define ex_sign ex_ni
354#endif
Bram Moolenaar009b2592004-10-24 19:18:58 +0000355#ifndef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200356# define ex_nbclose ex_ni
Bram Moolenaar009b2592004-10-24 19:18:58 +0000357# define ex_nbkey ex_ni
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200358# define ex_nbstart ex_ni
Bram Moolenaar009b2592004-10-24 19:18:58 +0000359#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000360
Bram Moolenaar071d4272004-06-13 20:20:40 +0000361#ifndef FEAT_JUMPLIST
362# define ex_jumps ex_ni
Bram Moolenaar2d358992016-06-12 21:20:54 +0200363# define ex_clearjumps ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000364# define ex_changes ex_ni
365#endif
366
Bram Moolenaar05159a02005-02-26 23:04:13 +0000367#ifndef FEAT_PROFILE
368# define ex_profile ex_ni
369#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200370#ifndef FEAT_TERMINAL
371# define ex_terminal ex_ni
372#endif
Bram Moolenaard4aa83a2019-05-09 18:59:31 +0200373#if !defined(FEAT_X11) || !defined(FEAT_XCLIPBOARD)
374# define ex_xrestore ex_ni
375#endif
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +0100376#if !defined(FEAT_PROP_POPUP)
Bram Moolenaar682725c2019-05-25 20:10:37 +0200377# define ex_popupclear ex_ni
378#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +0000379
Bram Moolenaar071d4272004-06-13 20:20:40 +0000380/*
381 * Declare cmdnames[].
382 */
383#define DO_DECLARE_EXCMD
384#include "ex_cmds.h"
Bram Moolenaar6de5e122017-04-20 21:55:44 +0200385#include "ex_cmdidxs.h"
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +0100386
Bram Moolenaar071d4272004-06-13 20:20:40 +0000387static char_u dollar_command[2] = {'$', 0};
388
389
390#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100391// Struct for storing a line inside a while/for loop
Bram Moolenaar071d4272004-06-13 20:20:40 +0000392typedef struct
393{
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100394 char_u *line; // command line
395 linenr_T lnum; // sourcing_lnum of the line
Bram Moolenaar071d4272004-06-13 20:20:40 +0000396} wcmd_T;
397
398/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000399 * Structure used to store info for line position in a while or for loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000400 * This is required, because do_one_cmd() may invoke ex_function(), which
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000401 * reads more lines that may come from the while/for loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000402 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000403struct loop_cookie
Bram Moolenaar071d4272004-06-13 20:20:40 +0000404{
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100405 garray_T *lines_gap; // growarray with line info
406 int current_line; // last read line from growarray
407 int repeating; // TRUE when looping a second time
408 // When "repeating" is FALSE use "getline" and "cookie" to get lines
Bram Moolenaar20b23c62020-08-20 15:25:00 +0200409 char_u *(*getline)(int, void *, int, getline_opt_T);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000410 void *cookie;
411};
412
Bram Moolenaar20b23c62020-08-20 15:25:00 +0200413static char_u *get_loop_line(int c, void *cookie, int indent, getline_opt_T options);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100414static int store_loop_line(garray_T *gap, char_u *line);
415static void free_cmdlines(garray_T *gap);
Bram Moolenaared203462004-06-16 11:19:22 +0000416
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100417// Struct to save a few things while debugging. Used in do_cmdline() only.
Bram Moolenaared203462004-06-16 11:19:22 +0000418struct dbg_stuff
419{
420 int trylevel;
421 int force_abort;
422 except_T *caught_stack;
423 char_u *vv_exception;
424 char_u *vv_throwpoint;
425 int did_emsg;
426 int got_int;
427 int did_throw;
428 int need_rethrow;
429 int check_cstack;
430 except_T *current_exception;
431};
432
Bram Moolenaared203462004-06-16 11:19:22 +0000433 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100434save_dbg_stuff(struct dbg_stuff *dsp)
Bram Moolenaared203462004-06-16 11:19:22 +0000435{
436 dsp->trylevel = trylevel; trylevel = 0;
437 dsp->force_abort = force_abort; force_abort = FALSE;
438 dsp->caught_stack = caught_stack; caught_stack = NULL;
439 dsp->vv_exception = v_exception(NULL);
440 dsp->vv_throwpoint = v_throwpoint(NULL);
441
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100442 // Necessary for debugging an inactive ":catch", ":finally", ":endtry"
Bram Moolenaared203462004-06-16 11:19:22 +0000443 dsp->did_emsg = did_emsg; did_emsg = FALSE;
444 dsp->got_int = got_int; got_int = FALSE;
445 dsp->did_throw = did_throw; did_throw = FALSE;
446 dsp->need_rethrow = need_rethrow; need_rethrow = FALSE;
447 dsp->check_cstack = check_cstack; check_cstack = FALSE;
448 dsp->current_exception = current_exception; current_exception = NULL;
449}
450
451 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100452restore_dbg_stuff(struct dbg_stuff *dsp)
Bram Moolenaared203462004-06-16 11:19:22 +0000453{
454 suppress_errthrow = FALSE;
455 trylevel = dsp->trylevel;
456 force_abort = dsp->force_abort;
457 caught_stack = dsp->caught_stack;
458 (void)v_exception(dsp->vv_exception);
459 (void)v_throwpoint(dsp->vv_throwpoint);
460 did_emsg = dsp->did_emsg;
461 got_int = dsp->got_int;
462 did_throw = dsp->did_throw;
463 need_rethrow = dsp->need_rethrow;
464 check_cstack = dsp->check_cstack;
465 current_exception = dsp->current_exception;
466}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000467#endif
468
Bram Moolenaar071d4272004-06-13 20:20:40 +0000469/*
470 * do_exmode(): Repeatedly get commands for the "Ex" mode, until the ":vi"
471 * command is given.
472 */
473 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100474do_exmode(
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100475 int improved) // TRUE for "improved Ex" mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000476{
477 int save_msg_scroll;
478 int prev_msg_row;
479 linenr_T prev_line;
Bram Moolenaar79518e22017-02-17 16:31:35 +0100480 varnumber_T changedtick;
Bram Moolenaardf177f62005-02-22 08:39:57 +0000481
482 if (improved)
483 exmode_active = EXMODE_VIM;
484 else
485 exmode_active = EXMODE_NORMAL;
486 State = NORMAL;
487
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100488 // When using ":global /pat/ visual" and then "Q" we return to continue
489 // the :global command.
Bram Moolenaardf177f62005-02-22 08:39:57 +0000490 if (global_busy)
491 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000492
493 save_msg_scroll = msg_scroll;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100494 ++RedrawingDisabled; // don't redisplay the window
495 ++no_wait_return; // don't wait for return
Bram Moolenaar071d4272004-06-13 20:20:40 +0000496#ifdef FEAT_GUI
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100497 // Ignore scrollbar and mouse events in Ex mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000498 ++hold_gui_events;
499#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000500
Bram Moolenaar32526b32019-01-19 17:43:09 +0100501 msg(_("Entering Ex mode. Type \"visual\" to go to Normal mode."));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000502 while (exmode_active)
503 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100504 // Check for a ":normal" command and no more characters left.
Bram Moolenaar7c626922005-02-07 22:01:03 +0000505 if (ex_normal_busy > 0 && typebuf.tb_len == 0)
506 {
507 exmode_active = FALSE;
508 break;
509 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000510 msg_scroll = TRUE;
511 need_wait_return = FALSE;
512 ex_pressedreturn = FALSE;
513 ex_no_reprint = FALSE;
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100514 changedtick = CHANGEDTICK(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000515 prev_msg_row = msg_row;
516 prev_line = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000517 if (improved)
518 {
519 cmdline_row = msg_row;
520 do_cmdline(NULL, getexline, NULL, 0);
521 }
522 else
523 do_cmdline(NULL, getexmodeline, NULL, DOCMD_NOWAIT);
524 lines_left = Rows - 1;
525
Bram Moolenaardf177f62005-02-22 08:39:57 +0000526 if ((prev_line != curwin->w_cursor.lnum
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100527 || changedtick != CHANGEDTICK(curbuf)) && !ex_no_reprint)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000528 {
Bram Moolenaardf177f62005-02-22 08:39:57 +0000529 if (curbuf->b_ml.ml_flags & ML_EMPTY)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100530 emsg(_(e_emptybuf));
Bram Moolenaardf177f62005-02-22 08:39:57 +0000531 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000532 {
Bram Moolenaardf177f62005-02-22 08:39:57 +0000533 if (ex_pressedreturn)
534 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100535 // go up one line, to overwrite the ":<CR>" line, so the
536 // output doesn't contain empty lines.
Bram Moolenaardf177f62005-02-22 08:39:57 +0000537 msg_row = prev_msg_row;
538 if (prev_msg_row == Rows - 1)
539 msg_row--;
540 }
541 msg_col = 0;
542 print_line_no_prefix(curwin->w_cursor.lnum, FALSE, FALSE);
543 msg_clr_eos();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000544 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000545 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100546 else if (ex_pressedreturn && !ex_no_reprint) // must be at EOF
Bram Moolenaardf177f62005-02-22 08:39:57 +0000547 {
548 if (curbuf->b_ml.ml_flags & ML_EMPTY)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100549 emsg(_(e_emptybuf));
Bram Moolenaardf177f62005-02-22 08:39:57 +0000550 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100551 emsg(_("E501: At end-of-file"));
Bram Moolenaardf177f62005-02-22 08:39:57 +0000552 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000553 }
554
555#ifdef FEAT_GUI
556 --hold_gui_events;
557#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000558 --RedrawingDisabled;
559 --no_wait_return;
560 update_screen(CLEAR);
561 need_wait_return = FALSE;
562 msg_scroll = save_msg_scroll;
563}
564
565/*
Bram Moolenaar4facea32019-10-12 20:17:40 +0200566 * Print the executed command for when 'verbose' is set.
567 * When "lnum" is 0 only print the command.
568 */
569 static void
570msg_verbose_cmd(linenr_T lnum, char_u *cmd)
571{
572 ++no_wait_return;
573 verbose_enter_scroll();
574
575 if (lnum == 0)
576 smsg(_("Executing: %s"), cmd);
577 else
578 smsg(_("line %ld: %s"), (long)lnum, cmd);
579 if (msg_silent == 0)
580 msg_puts("\n"); // don't overwrite this
581
582 verbose_leave_scroll();
583 --no_wait_return;
584}
585
586/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000587 * Execute a simple command line. Used for translated commands like "*".
588 */
589 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100590do_cmdline_cmd(char_u *cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000591{
592 return do_cmdline(cmd, NULL, NULL,
593 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
594}
595
596/*
597 * do_cmdline(): execute one Ex command line
598 *
599 * 1. Execute "cmdline" when it is not NULL.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100600 * If "cmdline" is NULL, or more lines are needed, fgetline() is used.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000601 * 2. Split up in parts separated with '|'.
602 *
603 * This function can be called recursively!
604 *
605 * flags:
606 * DOCMD_VERBOSE - The command will be included in the error message.
607 * DOCMD_NOWAIT - Don't call wait_return() and friends.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100608 * DOCMD_REPEAT - Repeat execution until fgetline() returns NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000609 * DOCMD_KEYTYPED - Don't reset KeyTyped.
610 * DOCMD_EXCRESET - Reset the exception environment (used for debugging).
611 * DOCMD_KEEPLINE - Store first typed line (for repeating with ".").
612 *
613 * return FAIL if cmdline could not be executed, OK otherwise
614 */
615 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100616do_cmdline(
617 char_u *cmdline,
Bram Moolenaar66250c92020-08-20 15:02:42 +0200618 char_u *(*fgetline)(int, void *, int, getline_opt_T),
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100619 void *cookie, // argument for fgetline()
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100620 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000621{
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100622 char_u *next_cmdline; // next cmd to execute
623 char_u *cmdline_copy = NULL; // copy of cmd line
624 int used_getline = FALSE; // used "fgetline" to obtain command
625 static int recursive = 0; // recursive depth
Bram Moolenaar071d4272004-06-13 20:20:40 +0000626 int msg_didout_before_start = 0;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100627 int count = 0; // line number count
628 int did_inc = FALSE; // incremented RedrawingDisabled
Bram Moolenaar071d4272004-06-13 20:20:40 +0000629 int retval = OK;
630#ifdef FEAT_EVAL
Bram Moolenaarddef1292019-12-16 17:10:33 +0100631 cstack_T cstack; // conditional stack
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100632 garray_T lines_ga; // keep lines for ":while"/":for"
633 int current_line = 0; // active line in lines_ga
Bram Moolenaard5053d02020-06-28 15:51:16 +0200634 int current_line_before = 0;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100635 char_u *fname = NULL; // function or script name
636 linenr_T *breakpoint = NULL; // ptr to breakpoint field in cookie
637 int *dbg_tick = NULL; // ptr to dbg_tick field in cookie
638 struct dbg_stuff debug_saved; // saved things for debug mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000639 int initial_trylevel;
Bram Moolenaar25e0f582020-05-25 22:36:50 +0200640 msglist_T **saved_msg_list = NULL;
641 msglist_T *private_msg_list;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000642
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100643 // "fgetline" and "cookie" passed to do_one_cmd()
Bram Moolenaar66250c92020-08-20 15:02:42 +0200644 char_u *(*cmd_getline)(int, void *, int, getline_opt_T);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000645 void *cmd_cookie;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000646 struct loop_cookie cmd_loop_cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000647 void *real_cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +0000648 int getline_is_func;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000649#else
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100650# define cmd_getline fgetline
Bram Moolenaar071d4272004-06-13 20:20:40 +0000651# define cmd_cookie cookie
652#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100653 static int call_depth = 0; // recursiveness
Bram Moolenaar2196bce2020-04-12 20:01:11 +0200654#ifdef FEAT_EVAL
Bram Moolenaare31ee862020-01-07 20:59:34 +0100655 ESTACK_CHECK_DECLARATION
Bram Moolenaar071d4272004-06-13 20:20:40 +0000656
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100657 // For every pair of do_cmdline()/do_one_cmd() calls, use an extra memory
658 // location for storing error messages to be converted to an exception.
659 // This ensures that the do_errthrow() call in do_one_cmd() does not
660 // combine the messages stored by an earlier invocation of do_one_cmd()
661 // with the command name of the later one. This would happen when
662 // BufWritePost autocommands are executed after a write error.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000663 saved_msg_list = msg_list;
664 msg_list = &private_msg_list;
665 private_msg_list = NULL;
666#endif
667
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100668 // It's possible to create an endless loop with ":execute", catch that
669 // here. The value of 200 allows nested function calls, ":source", etc.
670 // Allow 200 or 'maxfuncdepth', whatever is larger.
Bram Moolenaarb094ff42017-01-02 16:16:39 +0100671 if (call_depth >= 200
672#ifdef FEAT_EVAL
673 && call_depth >= p_mfd
674#endif
675 )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000676 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100677 emsg(_("E169: Command too recursive"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000678#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100679 // When converting to an exception, we do not include the command name
680 // since this is not an error of the specific command.
Bram Moolenaarddef1292019-12-16 17:10:33 +0100681 do_errthrow((cstack_T *)NULL, (char_u *)NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000682 msg_list = saved_msg_list;
683#endif
684 return FAIL;
685 }
686 ++call_depth;
687
688#ifdef FEAT_EVAL
689 cstack.cs_idx = -1;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000690 cstack.cs_looplevel = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000691 cstack.cs_trylevel = 0;
692 cstack.cs_emsg_silent_list = NULL;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000693 cstack.cs_lflags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000694 ga_init2(&lines_ga, (int)sizeof(wcmd_T), 10);
695
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100696 real_cookie = getline_cookie(fgetline, cookie);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000697
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100698 // Inside a function use a higher nesting level.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100699 getline_is_func = getline_equal(fgetline, cookie, get_func_line);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000700 if (getline_is_func && ex_nesting_level == func_level(real_cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000701 ++ex_nesting_level;
702
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100703 // Get the function or script name and the address where the next breakpoint
704 // line and the debug tick for a function or script are stored.
Bram Moolenaar05159a02005-02-26 23:04:13 +0000705 if (getline_is_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000706 {
707 fname = func_name(real_cookie);
708 breakpoint = func_breakpoint(real_cookie);
709 dbg_tick = func_dbg_tick(real_cookie);
710 }
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100711 else if (getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000712 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100713 fname = SOURCING_NAME;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000714 breakpoint = source_breakpoint(real_cookie);
715 dbg_tick = source_dbg_tick(real_cookie);
716 }
717
718 /*
719 * Initialize "force_abort" and "suppress_errthrow" at the top level.
720 */
721 if (!recursive)
722 {
723 force_abort = FALSE;
724 suppress_errthrow = FALSE;
725 }
726
727 /*
728 * If requested, store and reset the global values controlling the
Bram Moolenaar89d40322006-08-29 15:30:07 +0000729 * exception handling (used when debugging). Otherwise clear it to avoid
730 * a bogus compiler warning when the optimizer uses inline functions...
Bram Moolenaar071d4272004-06-13 20:20:40 +0000731 */
Bram Moolenaarb4872942006-05-13 10:32:52 +0000732 if (flags & DOCMD_EXCRESET)
Bram Moolenaared203462004-06-16 11:19:22 +0000733 save_dbg_stuff(&debug_saved);
Bram Moolenaar89d40322006-08-29 15:30:07 +0000734 else
Bram Moolenaara80faa82020-04-12 19:37:17 +0200735 CLEAR_FIELD(debug_saved);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000736
737 initial_trylevel = trylevel;
738
739 /*
740 * "did_throw" will be set to TRUE when an exception is being thrown.
741 */
742 did_throw = FALSE;
743#endif
744 /*
745 * "did_emsg" will be set to TRUE when emsg() is used, in which case we
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000746 * cancel the whole command line, and any if/endif or loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000747 * If force_abort is set, we cancel everything.
748 */
749 did_emsg = FALSE;
750
751 /*
752 * KeyTyped is only set when calling vgetc(). Reset it here when not
753 * calling vgetc() (sourced command lines).
754 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100755 if (!(flags & DOCMD_KEYTYPED)
756 && !getline_equal(fgetline, cookie, getexline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000757 KeyTyped = FALSE;
758
759 /*
760 * Continue executing command lines:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000761 * - when inside an ":if", ":while" or ":for"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000762 * - for multiple commands on one line, separated with '|'
763 * - when repeating until there are no more lines (for ":source")
764 */
765 next_cmdline = cmdline;
766 do
767 {
Bram Moolenaar05159a02005-02-26 23:04:13 +0000768#ifdef FEAT_EVAL
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100769 getline_is_func = getline_equal(fgetline, cookie, get_func_line);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000770#endif
771
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100772 // stop skipping cmds for an error msg after all endif/while/for
Bram Moolenaar071d4272004-06-13 20:20:40 +0000773 if (next_cmdline == NULL
774#ifdef FEAT_EVAL
775 && !force_abort
776 && cstack.cs_idx < 0
Bram Moolenaar05159a02005-02-26 23:04:13 +0000777 && !(getline_is_func && func_has_abort(real_cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000778#endif
779 )
780 did_emsg = FALSE;
781
782 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000783 * 1. If repeating a line in a loop, get a line from lines_ga.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100784 * 2. If no line given: Get an allocated line with fgetline().
Bram Moolenaar071d4272004-06-13 20:20:40 +0000785 * 3. If a line is given: Make a copy, so we can mess with it.
786 */
787
788#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100789 // 1. If repeating, get a previous line from lines_ga.
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000790 if (cstack.cs_looplevel > 0 && current_line < lines_ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000791 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100792 // Each '|' separated command is stored separately in lines_ga, to
793 // be able to jump to it. Don't use next_cmdline now.
Bram Moolenaard23a8232018-02-10 18:45:26 +0100794 VIM_CLEAR(cmdline_copy);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000795
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100796 // Check if a function has returned or, unless it has an unclosed
797 // try conditional, aborted.
Bram Moolenaar05159a02005-02-26 23:04:13 +0000798 if (getline_is_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000799 {
Bram Moolenaar05159a02005-02-26 23:04:13 +0000800# ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000801 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +0000802 func_line_end(real_cookie);
803# endif
804 if (func_has_ended(real_cookie))
805 {
806 retval = FAIL;
807 break;
808 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000809 }
Bram Moolenaar05159a02005-02-26 23:04:13 +0000810#ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000811 else if (do_profiling == PROF_YES
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100812 && getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar05159a02005-02-26 23:04:13 +0000813 script_line_end();
814#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000815
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100816 // Check if a sourced file hit a ":finish" command.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100817 if (source_finished(fgetline, cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000818 {
819 retval = FAIL;
820 break;
821 }
822
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100823 // If breakpoints have been added/deleted need to check for it.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000824 if (breakpoint != NULL && dbg_tick != NULL
825 && *dbg_tick != debug_tick)
826 {
827 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100828 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100829 fname, SOURCING_LNUM);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000830 *dbg_tick = debug_tick;
831 }
832
833 next_cmdline = ((wcmd_T *)(lines_ga.ga_data))[current_line].line;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100834 SOURCING_LNUM = ((wcmd_T *)(lines_ga.ga_data))[current_line].lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000835
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100836 // Did we encounter a breakpoint?
Bram Moolenaar071d4272004-06-13 20:20:40 +0000837 if (breakpoint != NULL && *breakpoint != 0
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100838 && *breakpoint <= SOURCING_LNUM)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000839 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100840 dbg_breakpoint(fname, SOURCING_LNUM);
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100841 // Find next breakpoint.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000842 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100843 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100844 fname, SOURCING_LNUM);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000845 *dbg_tick = debug_tick;
846 }
Bram Moolenaar05159a02005-02-26 23:04:13 +0000847# ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000848 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +0000849 {
850 if (getline_is_func)
851 func_line_start(real_cookie);
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100852 else if (getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar05159a02005-02-26 23:04:13 +0000853 script_line_start();
854 }
855# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000856 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000857#endif
858
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100859 // 2. If no line given, get an allocated line with fgetline().
Bram Moolenaar071d4272004-06-13 20:20:40 +0000860 if (next_cmdline == NULL)
861 {
862 /*
863 * Need to set msg_didout for the first line after an ":if",
864 * otherwise the ":if" will be overwritten.
865 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100866 if (count == 1 && getline_equal(fgetline, cookie, getexline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000867 msg_didout = TRUE;
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100868 if (fgetline == NULL || (next_cmdline = fgetline(':', cookie,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000869#ifdef FEAT_EVAL
870 cstack.cs_idx < 0 ? 0 : (cstack.cs_idx + 1) * 2
871#else
872 0
873#endif
Bram Moolenaare96a2492019-06-25 04:12:16 +0200874 , TRUE)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000875 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100876 // Don't call wait_return for aborted command line. The NULL
877 // returned for the end of a sourced file or executed function
878 // doesn't do this.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000879 if (KeyTyped && !(flags & DOCMD_REPEAT))
880 need_wait_return = FALSE;
881 retval = FAIL;
882 break;
883 }
884 used_getline = TRUE;
885
886 /*
887 * Keep the first typed line. Clear it when more lines are typed.
888 */
889 if (flags & DOCMD_KEEPLINE)
890 {
891 vim_free(repeat_cmdline);
892 if (count == 0)
893 repeat_cmdline = vim_strsave(next_cmdline);
894 else
895 repeat_cmdline = NULL;
896 }
897 }
898
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100899 // 3. Make a copy of the command so we can mess with it.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000900 else if (cmdline_copy == NULL)
901 {
902 next_cmdline = vim_strsave(next_cmdline);
903 if (next_cmdline == NULL)
904 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100905 emsg(_(e_outofmem));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000906 retval = FAIL;
907 break;
908 }
909 }
910 cmdline_copy = next_cmdline;
911
912#ifdef FEAT_EVAL
913 /*
Bram Moolenaard5053d02020-06-28 15:51:16 +0200914 * Inside a while/for loop, and when the command looks like a ":while"
915 * or ":for", the line is stored, because we may need it later when
916 * looping.
917 *
918 * When there is a '|' and another command, it is stored separately,
919 * because we need to be able to jump back to it from an
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000920 * :endwhile/:endfor.
Bram Moolenaard5053d02020-06-28 15:51:16 +0200921 *
922 * Pass a different "fgetline" function to do_one_cmd() below,
923 * that it stores lines in or reads them from "lines_ga". Makes it
924 * possible to define a function inside a while/for loop and handles
925 * line continuation.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000926 */
Bram Moolenaard5053d02020-06-28 15:51:16 +0200927 if ((cstack.cs_looplevel > 0 || has_loop_cmd(next_cmdline)))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000928 {
Bram Moolenaard5053d02020-06-28 15:51:16 +0200929 cmd_getline = get_loop_line;
930 cmd_cookie = (void *)&cmd_loop_cookie;
931 cmd_loop_cookie.lines_gap = &lines_ga;
932 cmd_loop_cookie.current_line = current_line;
933 cmd_loop_cookie.getline = fgetline;
934 cmd_loop_cookie.cookie = cookie;
935 cmd_loop_cookie.repeating = (current_line < lines_ga.ga_len);
936
937 // Save the current line when encountering it the first time.
938 if (current_line == lines_ga.ga_len
939 && store_loop_line(&lines_ga, next_cmdline) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000940 {
941 retval = FAIL;
942 break;
943 }
Bram Moolenaard5053d02020-06-28 15:51:16 +0200944 current_line_before = current_line;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000945 }
Bram Moolenaard5053d02020-06-28 15:51:16 +0200946 else
947 {
948 cmd_getline = fgetline;
949 cmd_cookie = cookie;
950 }
951
Bram Moolenaar071d4272004-06-13 20:20:40 +0000952 did_endif = FALSE;
953#endif
954
955 if (count++ == 0)
956 {
957 /*
958 * All output from the commands is put below each other, without
959 * waiting for a return. Don't do this when executing commands
960 * from a script or when being called recursive (e.g. for ":e
961 * +command file").
962 */
963 if (!(flags & DOCMD_NOWAIT) && !recursive)
964 {
965 msg_didout_before_start = msg_didout;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100966 msg_didany = FALSE; // no output yet
Bram Moolenaar071d4272004-06-13 20:20:40 +0000967 msg_start();
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100968 msg_scroll = TRUE; // put messages below each other
969 ++no_wait_return; // don't wait for return until finished
Bram Moolenaar071d4272004-06-13 20:20:40 +0000970 ++RedrawingDisabled;
971 did_inc = TRUE;
972 }
973 }
974
Bram Moolenaar823654b2020-05-29 23:03:09 +0200975 if ((p_verbose >= 15 && SOURCING_NAME != NULL) || p_verbose >= 16)
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100976 msg_verbose_cmd(SOURCING_LNUM, cmdline_copy);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000977
978 /*
979 * 2. Execute one '|' separated command.
980 * do_one_cmd() will return NULL if there is no trailing '|'.
981 * "cmdline_copy" can change, e.g. for '%' and '#' expansion.
982 */
983 ++recursive;
984 next_cmdline = do_one_cmd(&cmdline_copy, flags & DOCMD_VERBOSE,
985#ifdef FEAT_EVAL
986 &cstack,
987#endif
988 cmd_getline, cmd_cookie);
989 --recursive;
990
991#ifdef FEAT_EVAL
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000992 if (cmd_cookie == (void *)&cmd_loop_cookie)
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100993 // Use "current_line" from "cmd_loop_cookie", it may have been
994 // incremented when defining a function.
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000995 current_line = cmd_loop_cookie.current_line;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000996#endif
997
998 if (next_cmdline == NULL)
999 {
Bram Moolenaard23a8232018-02-10 18:45:26 +01001000 VIM_CLEAR(cmdline_copy);
Bram Moolenaard7663c22019-08-06 21:59:57 +02001001
Bram Moolenaar071d4272004-06-13 20:20:40 +00001002 /*
1003 * If the command was typed, remember it for the ':' register.
1004 * Do this AFTER executing the command to make :@: work.
1005 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001006 if (getline_equal(fgetline, cookie, getexline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001007 && new_last_cmdline != NULL)
1008 {
1009 vim_free(last_cmdline);
1010 last_cmdline = new_last_cmdline;
1011 new_last_cmdline = NULL;
1012 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001013 }
1014 else
1015 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001016 // need to copy the command after the '|' to cmdline_copy, for the
1017 // next do_one_cmd()
Bram Moolenaara7241f52008-06-24 20:39:31 +00001018 STRMOVE(cmdline_copy, next_cmdline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001019 next_cmdline = cmdline_copy;
1020 }
1021
1022
1023#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001024 // reset did_emsg for a function that is not aborted by an error
Bram Moolenaar071d4272004-06-13 20:20:40 +00001025 if (did_emsg && !force_abort
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001026 && getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001027 && !func_has_abort(real_cookie))
1028 did_emsg = FALSE;
1029
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001030 if (cstack.cs_looplevel > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001031 {
1032 ++current_line;
1033
1034 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001035 * An ":endwhile", ":endfor" and ":continue" is handled here.
1036 * If we were executing commands, jump back to the ":while" or
1037 * ":for".
1038 * If we were not executing commands, decrement cs_looplevel.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001039 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001040 if (cstack.cs_lflags & (CSL_HAD_CONT | CSL_HAD_ENDLOOP))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001041 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001042 cstack.cs_lflags &= ~(CSL_HAD_CONT | CSL_HAD_ENDLOOP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001043
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001044 // Jump back to the matching ":while" or ":for". Be careful
1045 // not to use a cs_line[] from an entry that isn't a ":while"
1046 // or ":for": It would make "current_line" invalid and can
1047 // cause a crash.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001048 if (!did_emsg && !got_int && !did_throw
1049 && cstack.cs_idx >= 0
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001050 && (cstack.cs_flags[cstack.cs_idx]
1051 & (CSF_WHILE | CSF_FOR))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001052 && cstack.cs_line[cstack.cs_idx] >= 0
1053 && (cstack.cs_flags[cstack.cs_idx] & CSF_ACTIVE))
1054 {
1055 current_line = cstack.cs_line[cstack.cs_idx];
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001056 // remember we jumped there
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001057 cstack.cs_lflags |= CSL_HAD_LOOP;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001058 line_breakcheck(); // check if CTRL-C typed
Bram Moolenaar071d4272004-06-13 20:20:40 +00001059
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001060 // Check for the next breakpoint at or after the ":while"
1061 // or ":for".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001062 if (breakpoint != NULL)
1063 {
1064 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001065 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001066 fname,
1067 ((wcmd_T *)lines_ga.ga_data)[current_line].lnum-1);
1068 *dbg_tick = debug_tick;
1069 }
1070 }
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001071 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001072 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001073 // can only get here with ":endwhile" or ":endfor"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001074 if (cstack.cs_idx >= 0)
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001075 rewind_conditionals(&cstack, cstack.cs_idx - 1,
1076 CSF_WHILE | CSF_FOR, &cstack.cs_looplevel);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001077 }
1078 }
1079
1080 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001081 * For a ":while" or ":for" we need to remember the line number.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001082 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001083 else if (cstack.cs_lflags & CSL_HAD_LOOP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001084 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001085 cstack.cs_lflags &= ~CSL_HAD_LOOP;
Bram Moolenaard5053d02020-06-28 15:51:16 +02001086 cstack.cs_line[cstack.cs_idx] = current_line_before;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001087 }
1088 }
1089
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001090 // Check for the next breakpoint after a watchexpression
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01001091 if (breakpoint != NULL && has_watchexpr())
1092 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001093 *breakpoint = dbg_find_breakpoint(FALSE, fname, SOURCING_LNUM);
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01001094 *dbg_tick = debug_tick;
1095 }
1096
Bram Moolenaar071d4272004-06-13 20:20:40 +00001097 /*
1098 * When not inside any ":while" loop, clear remembered lines.
1099 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001100 if (cstack.cs_looplevel == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001101 {
1102 if (lines_ga.ga_len > 0)
1103 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001104 SOURCING_LNUM =
Bram Moolenaar071d4272004-06-13 20:20:40 +00001105 ((wcmd_T *)lines_ga.ga_data)[lines_ga.ga_len - 1].lnum;
1106 free_cmdlines(&lines_ga);
1107 }
1108 current_line = 0;
1109 }
1110
1111 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001112 * A ":finally" makes did_emsg, got_int, and did_throw pending for
1113 * being restored at the ":endtry". Reset them here and set the
1114 * ACTIVE and FINALLY flags, so that the finally clause gets executed.
1115 * This includes the case where a missing ":endif", ":endwhile" or
1116 * ":endfor" was detected by the ":finally" itself.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001117 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001118 if (cstack.cs_lflags & CSL_HAD_FINA)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001119 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001120 cstack.cs_lflags &= ~CSL_HAD_FINA;
1121 report_make_pending(cstack.cs_pending[cstack.cs_idx]
1122 & (CSTP_ERROR | CSTP_INTERRUPT | CSTP_THROW),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001123 did_throw ? (void *)current_exception : NULL);
1124 did_emsg = got_int = did_throw = FALSE;
1125 cstack.cs_flags[cstack.cs_idx] |= CSF_ACTIVE | CSF_FINALLY;
1126 }
1127
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001128 // Update global "trylevel" for recursive calls to do_cmdline() from
1129 // within this loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001130 trylevel = initial_trylevel + cstack.cs_trylevel;
1131
1132 /*
Bram Moolenaarc1762cc2007-05-10 16:56:30 +00001133 * If the outermost try conditional (across function calls and sourced
Bram Moolenaar071d4272004-06-13 20:20:40 +00001134 * files) is aborted because of an error, an interrupt, or an uncaught
1135 * exception, cancel everything. If it is left normally, reset
1136 * force_abort to get the non-EH compatible abortion behavior for
1137 * the rest of the script.
1138 */
1139 if (trylevel == 0 && !did_emsg && !got_int && !did_throw)
1140 force_abort = FALSE;
1141
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001142 // Convert an interrupt to an exception if appropriate.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001143 (void)do_intthrow(&cstack);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001144#endif // FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001145
1146 }
1147 /*
1148 * Continue executing command lines when:
1149 * - no CTRL-C typed, no aborting error, no exception thrown or try
1150 * conditionals need to be checked for executing finally clauses or
1151 * catching an interrupt exception
1152 * - didn't get an error message or lines are not typed
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001153 * - there is a command after '|', inside a :if, :while, :for or :try, or
Bram Moolenaar071d4272004-06-13 20:20:40 +00001154 * looping for ":source" command or function call.
1155 */
1156 while (!((got_int
1157#ifdef FEAT_EVAL
1158 || (did_emsg && force_abort) || did_throw
1159#endif
1160 )
1161#ifdef FEAT_EVAL
1162 && cstack.cs_trylevel == 0
1163#endif
1164 )
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001165 && !(did_emsg
1166#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001167 // Keep going when inside try/catch, so that the error can be
1168 // deal with, except when it is a syntax error, it may cause
1169 // the :endtry to be missed.
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001170 && (cstack.cs_trylevel == 0 || did_emsg_syntax)
1171#endif
1172 && used_getline
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001173 && (getline_equal(fgetline, cookie, getexmodeline)
1174 || getline_equal(fgetline, cookie, getexline)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001175 && (next_cmdline != NULL
1176#ifdef FEAT_EVAL
1177 || cstack.cs_idx >= 0
1178#endif
1179 || (flags & DOCMD_REPEAT)));
1180
1181 vim_free(cmdline_copy);
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001182 did_emsg_syntax = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001183#ifdef FEAT_EVAL
1184 free_cmdlines(&lines_ga);
1185 ga_clear(&lines_ga);
1186
1187 if (cstack.cs_idx >= 0)
1188 {
1189 /*
1190 * If a sourced file or executed function ran to its end, report the
1191 * unclosed conditional.
1192 */
1193 if (!got_int && !did_throw
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001194 && ((getline_equal(fgetline, cookie, getsourceline)
1195 && !source_finished(fgetline, cookie))
1196 || (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001197 && !func_has_ended(real_cookie))))
1198 {
1199 if (cstack.cs_flags[cstack.cs_idx] & CSF_TRY)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001200 emsg(_(e_endtry));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001201 else if (cstack.cs_flags[cstack.cs_idx] & CSF_WHILE)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001202 emsg(_(e_endwhile));
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001203 else if (cstack.cs_flags[cstack.cs_idx] & CSF_FOR)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001204 emsg(_(e_endfor));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001205 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001206 emsg(_(e_endif));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001207 }
1208
1209 /*
1210 * Reset "trylevel" in case of a ":finish" or ":return" or a missing
1211 * ":endtry" in a sourced file or executed function. If the try
1212 * conditional is in its finally clause, ignore anything pending.
1213 * If it is in a catch clause, finish the caught exception.
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001214 * Also cleanup any "cs_forinfo" structures.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001215 */
1216 do
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001217 {
1218 int idx = cleanup_conditionals(&cstack, 0, TRUE);
1219
Bram Moolenaar89e5d682005-01-17 22:06:23 +00001220 if (idx >= 0)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001221 --idx; // remove try block not in its finally clause
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001222 rewind_conditionals(&cstack, idx, CSF_WHILE | CSF_FOR,
1223 &cstack.cs_looplevel);
1224 }
1225 while (cstack.cs_idx >= 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001226 trylevel = initial_trylevel;
1227 }
1228
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001229 // If a missing ":endtry", ":endwhile", ":endfor", or ":endif" or a memory
1230 // lack was reported above and the error message is to be converted to an
1231 // exception, do this now after rewinding the cstack.
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001232 do_errthrow(&cstack, getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001233 ? (char_u *)"endfunction" : (char_u *)NULL);
1234
1235 if (trylevel == 0)
1236 {
Bram Moolenaar820d55a2020-10-10 15:05:23 +02001237 // Just in case did_throw got set but current_exception wasn't.
1238 if (current_exception == NULL)
1239 did_throw = FALSE;
1240
Bram Moolenaar071d4272004-06-13 20:20:40 +00001241 /*
1242 * When an exception is being thrown out of the outermost try
1243 * conditional, discard the uncaught exception, disable the conversion
1244 * of interrupts or errors to exceptions, and ensure that no more
1245 * commands are executed.
1246 */
1247 if (did_throw)
1248 {
1249 void *p = NULL;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02001250 msglist_T *messages = NULL, *next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001251
1252 /*
1253 * If the uncaught exception is a user exception, report it as an
1254 * error. If it is an error exception, display the saved error
1255 * message now. For an interrupt exception, do nothing; the
1256 * interrupt message is given elsewhere.
1257 */
1258 switch (current_exception->type)
1259 {
1260 case ET_USER:
Bram Moolenaar9c13b352005-05-19 20:53:52 +00001261 vim_snprintf((char *)IObuff, IOSIZE,
1262 _("E605: Exception not caught: %s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001263 current_exception->value);
1264 p = vim_strsave(IObuff);
1265 break;
1266 case ET_ERROR:
1267 messages = current_exception->messages;
1268 current_exception->messages = NULL;
1269 break;
1270 case ET_INTERRUPT:
1271 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001272 }
1273
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001274 estack_push(ETYPE_EXCEPT, current_exception->throw_name,
1275 current_exception->throw_lnum);
Bram Moolenaare31ee862020-01-07 20:59:34 +01001276 ESTACK_CHECK_SETUP
Bram Moolenaar071d4272004-06-13 20:20:40 +00001277 current_exception->throw_name = NULL;
1278
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001279 discard_current_exception(); // uses IObuff if 'verbose'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001280 suppress_errthrow = TRUE;
1281 force_abort = TRUE;
1282
1283 if (messages != NULL)
1284 {
1285 do
1286 {
1287 next = messages->next;
1288 emsg(messages->msg);
1289 vim_free(messages->msg);
Bram Moolenaar5fbf3bc2020-06-01 21:13:11 +02001290 vim_free(messages->sfile);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001291 vim_free(messages);
1292 messages = next;
1293 }
1294 while (messages != NULL);
1295 }
1296 else if (p != NULL)
1297 {
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01001298 emsg(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001299 vim_free(p);
1300 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001301 vim_free(SOURCING_NAME);
Bram Moolenaare31ee862020-01-07 20:59:34 +01001302 ESTACK_CHECK_NOW
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001303 estack_pop();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001304 }
1305
1306 /*
1307 * On an interrupt or an aborting error not converted to an exception,
1308 * disable the conversion of errors to exceptions. (Interrupts are not
Bram Moolenaar2196bce2020-04-12 20:01:11 +02001309 * converted anymore, here.) This enables also the interrupt message
Bram Moolenaar071d4272004-06-13 20:20:40 +00001310 * when force_abort is set and did_emsg unset in case of an interrupt
1311 * from a finally clause after an error.
1312 */
1313 else if (got_int || (did_emsg && force_abort))
1314 suppress_errthrow = TRUE;
1315 }
1316
1317 /*
1318 * The current cstack will be freed when do_cmdline() returns. An uncaught
1319 * exception will have to be rethrown in the previous cstack. If a function
1320 * has just returned or a script file was just finished and the previous
1321 * cstack belongs to the same function or, respectively, script file, it
1322 * will have to be checked for finally clauses to be executed due to the
1323 * ":return" or ":finish". This is done in do_one_cmd().
1324 */
1325 if (did_throw)
1326 need_rethrow = TRUE;
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001327 if ((getline_equal(fgetline, cookie, getsourceline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001328 && ex_nesting_level > source_level(real_cookie))
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001329 || (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001330 && ex_nesting_level > func_level(real_cookie) + 1))
1331 {
1332 if (!did_throw)
1333 check_cstack = TRUE;
1334 }
1335 else
1336 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001337 // When leaving a function, reduce nesting level.
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001338 if (getline_equal(fgetline, cookie, get_func_line))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001339 --ex_nesting_level;
1340 /*
1341 * Go to debug mode when returning from a function in which we are
1342 * single-stepping.
1343 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001344 if ((getline_equal(fgetline, cookie, getsourceline)
1345 || getline_equal(fgetline, cookie, get_func_line))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001346 && ex_nesting_level + 1 <= debug_break_level)
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001347 do_debug(getline_equal(fgetline, cookie, getsourceline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001348 ? (char_u *)_("End of sourced file")
1349 : (char_u *)_("End of function"));
1350 }
1351
1352 /*
1353 * Restore the exception environment (done after returning from the
1354 * debugger).
1355 */
1356 if (flags & DOCMD_EXCRESET)
Bram Moolenaared203462004-06-16 11:19:22 +00001357 restore_dbg_stuff(&debug_saved);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001358
1359 msg_list = saved_msg_list;
Bram Moolenaar292b90d2020-03-18 15:23:16 +01001360
1361 // Cleanup if "cs_emsg_silent_list" remains.
1362 if (cstack.cs_emsg_silent_list != NULL)
1363 {
1364 eslist_T *elem, *temp;
1365
1366 for (elem = cstack.cs_emsg_silent_list; elem != NULL; elem = temp)
1367 {
1368 temp = elem->next;
1369 vim_free(elem);
1370 }
1371 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001372#endif // FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001373
1374 /*
1375 * If there was too much output to fit on the command line, ask the user to
1376 * hit return before redrawing the screen. With the ":global" command we do
1377 * this only once after the command is finished.
1378 */
1379 if (did_inc)
1380 {
1381 --RedrawingDisabled;
1382 --no_wait_return;
1383 msg_scroll = FALSE;
1384
1385 /*
1386 * When just finished an ":if"-":else" which was typed, no need to
1387 * wait for hit-return. Also for an error situation.
1388 */
1389 if (retval == FAIL
1390#ifdef FEAT_EVAL
1391 || (did_endif && KeyTyped && !did_emsg)
1392#endif
1393 )
1394 {
1395 need_wait_return = FALSE;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001396 msg_didany = FALSE; // don't wait when restarting edit
Bram Moolenaar071d4272004-06-13 20:20:40 +00001397 }
1398 else if (need_wait_return)
1399 {
1400 /*
1401 * The msg_start() above clears msg_didout. The wait_return we do
1402 * here should not overwrite the command that may be shown before
1403 * doing that.
1404 */
1405 msg_didout |= msg_didout_before_start;
1406 wait_return(FALSE);
1407 }
1408 }
1409
Bram Moolenaar2a942252012-11-28 23:03:07 +01001410#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001411 did_endif = FALSE; // in case do_cmdline used recursively
Bram Moolenaar2a942252012-11-28 23:03:07 +01001412#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001413 /*
1414 * Reset if_level, in case a sourced script file contains more ":if" than
1415 * ":endif" (could be ":if x | foo | endif").
1416 */
1417 if_level = 0;
Bram Moolenaar2e18a122012-11-28 19:10:54 +01001418#endif
Bram Moolenaard4ad0d42012-11-28 17:34:48 +01001419
Bram Moolenaar071d4272004-06-13 20:20:40 +00001420 --call_depth;
1421 return retval;
1422}
1423
1424#ifdef FEAT_EVAL
1425/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001426 * Obtain a line when inside a ":while" or ":for" loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001427 */
1428 static char_u *
Bram Moolenaar20b23c62020-08-20 15:25:00 +02001429get_loop_line(int c, void *cookie, int indent, getline_opt_T options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001430{
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001431 struct loop_cookie *cp = (struct loop_cookie *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001432 wcmd_T *wp;
1433 char_u *line;
1434
1435 if (cp->current_line + 1 >= cp->lines_gap->ga_len)
1436 {
1437 if (cp->repeating)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001438 return NULL; // trying to read past ":endwhile"/":endfor"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001439
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001440 // First time inside the ":while"/":for": get line normally.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001441 if (cp->getline == NULL)
Bram Moolenaar20b23c62020-08-20 15:25:00 +02001442 line = getcmdline(c, 0L, indent, options);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001443 else
Bram Moolenaar20b23c62020-08-20 15:25:00 +02001444 line = cp->getline(c, cp->cookie, indent, options);
Bram Moolenaard68071d2006-05-02 22:08:30 +00001445 if (line != NULL && store_loop_line(cp->lines_gap, line) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001446 ++cp->current_line;
1447
1448 return line;
1449 }
1450
1451 KeyTyped = FALSE;
1452 ++cp->current_line;
1453 wp = (wcmd_T *)(cp->lines_gap->ga_data) + cp->current_line;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001454 SOURCING_LNUM = wp->lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001455 return vim_strsave(wp->line);
1456}
1457
1458/*
1459 * Store a line in "gap" so that a ":while" loop can execute it again.
1460 */
1461 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001462store_loop_line(garray_T *gap, char_u *line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001463{
1464 if (ga_grow(gap, 1) == FAIL)
1465 return FAIL;
1466 ((wcmd_T *)(gap->ga_data))[gap->ga_len].line = vim_strsave(line);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001467 ((wcmd_T *)(gap->ga_data))[gap->ga_len].lnum = SOURCING_LNUM;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001468 ++gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001469 return OK;
1470}
1471
1472/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001473 * Free the lines stored for a ":while" or ":for" loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001474 */
1475 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001476free_cmdlines(garray_T *gap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001477{
1478 while (gap->ga_len > 0)
1479 {
1480 vim_free(((wcmd_T *)(gap->ga_data))[gap->ga_len - 1].line);
1481 --gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001482 }
1483}
1484#endif
1485
1486/*
Bram Moolenaar89d40322006-08-29 15:30:07 +00001487 * If "fgetline" is get_loop_line(), return TRUE if the getline it uses equals
1488 * "func". * Otherwise return TRUE when "fgetline" equals "func".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001489 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001490 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001491getline_equal(
Bram Moolenaar66250c92020-08-20 15:02:42 +02001492 char_u *(*fgetline)(int, void *, int, getline_opt_T),
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001493 void *cookie UNUSED, // argument for fgetline()
Bram Moolenaar66250c92020-08-20 15:02:42 +02001494 char_u *(*func)(int, void *, int, getline_opt_T))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001495{
1496#ifdef FEAT_EVAL
Bram Moolenaar20b23c62020-08-20 15:25:00 +02001497 char_u *(*gp)(int, void *, int, getline_opt_T);
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001498 struct loop_cookie *cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001499
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001500 // When "fgetline" is "get_loop_line()" use the "cookie" to find the
1501 // function that's originally used to obtain the lines. This may be
1502 // nested several levels.
Bram Moolenaar89d40322006-08-29 15:30:07 +00001503 gp = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001504 cp = (struct loop_cookie *)cookie;
1505 while (gp == get_loop_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001506 {
1507 gp = cp->getline;
1508 cp = cp->cookie;
1509 }
1510 return gp == func;
1511#else
Bram Moolenaar89d40322006-08-29 15:30:07 +00001512 return fgetline == func;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001513#endif
1514}
1515
Bram Moolenaar071d4272004-06-13 20:20:40 +00001516/*
Bram Moolenaar89d40322006-08-29 15:30:07 +00001517 * If "fgetline" is get_loop_line(), return the cookie used by the original
Bram Moolenaar071d4272004-06-13 20:20:40 +00001518 * getline function. Otherwise return "cookie".
1519 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001520 void *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001521getline_cookie(
Bram Moolenaar66250c92020-08-20 15:02:42 +02001522 char_u *(*fgetline)(int, void *, int, getline_opt_T) UNUSED,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001523 void *cookie) // argument for fgetline()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001524{
Bram Moolenaar13505972019-01-24 15:04:48 +01001525#ifdef FEAT_EVAL
Bram Moolenaar20b23c62020-08-20 15:25:00 +02001526 char_u *(*gp)(int, void *, int, getline_opt_T);
Bram Moolenaard5053d02020-06-28 15:51:16 +02001527 struct loop_cookie *cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001528
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001529 // When "fgetline" is "get_loop_line()" use the "cookie" to find the
1530 // cookie that's originally used to obtain the lines. This may be nested
1531 // several levels.
Bram Moolenaar89d40322006-08-29 15:30:07 +00001532 gp = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001533 cp = (struct loop_cookie *)cookie;
1534 while (gp == get_loop_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001535 {
1536 gp = cp->getline;
1537 cp = cp->cookie;
1538 }
1539 return cp;
Bram Moolenaar13505972019-01-24 15:04:48 +01001540#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001541 return cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001542#endif
Bram Moolenaar13505972019-01-24 15:04:48 +01001543}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001544
Bram Moolenaard5053d02020-06-28 15:51:16 +02001545#if defined(FEAT_EVAL) || defined(PROT)
1546/*
1547 * Get the next line source line without advancing.
1548 */
1549 char_u *
1550getline_peek(
Bram Moolenaar66250c92020-08-20 15:02:42 +02001551 char_u *(*fgetline)(int, void *, int, getline_opt_T) UNUSED,
Bram Moolenaard5053d02020-06-28 15:51:16 +02001552 void *cookie) // argument for fgetline()
1553{
Bram Moolenaar20b23c62020-08-20 15:25:00 +02001554 char_u *(*gp)(int, void *, int, getline_opt_T);
Bram Moolenaard5053d02020-06-28 15:51:16 +02001555 struct loop_cookie *cp;
1556 wcmd_T *wp;
1557
1558 // When "fgetline" is "get_loop_line()" use the "cookie" to find the
1559 // cookie that's originally used to obtain the lines. This may be nested
1560 // several levels.
1561 gp = fgetline;
1562 cp = (struct loop_cookie *)cookie;
1563 while (gp == get_loop_line)
1564 {
1565 if (cp->current_line + 1 < cp->lines_gap->ga_len)
1566 {
1567 // executing lines a second time, use the stored copy
1568 wp = (wcmd_T *)(cp->lines_gap->ga_data) + cp->current_line + 1;
1569 return wp->line;
1570 }
1571 gp = cp->getline;
1572 cp = cp->cookie;
1573 }
1574 if (gp == getsourceline)
1575 return source_nextline(cp);
1576 return NULL;
1577}
1578#endif
1579
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001580
1581/*
1582 * Helper function to apply an offset for buffer commands, i.e. ":bdelete",
1583 * ":bwipeout", etc.
1584 * Returns the buffer number.
1585 */
1586 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001587compute_buffer_local_count(int addr_type, int lnum, int offset)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001588{
1589 buf_T *buf;
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001590 buf_T *nextbuf;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001591 int count = offset;
1592
1593 buf = firstbuf;
1594 while (buf->b_next != NULL && buf->b_fnum < lnum)
1595 buf = buf->b_next;
1596 while (count != 0)
1597 {
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001598 count += (offset < 0) ? 1 : -1;
1599 nextbuf = (offset < 0) ? buf->b_prev : buf->b_next;
1600 if (nextbuf == NULL)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001601 break;
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001602 buf = nextbuf;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001603 if (addr_type == ADDR_LOADED_BUFFERS)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001604 // skip over unloaded buffers
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001605 while (buf->b_ml.ml_mfp == NULL)
1606 {
1607 nextbuf = (offset < 0) ? buf->b_prev : buf->b_next;
1608 if (nextbuf == NULL)
1609 break;
1610 buf = nextbuf;
1611 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001612 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001613 // we might have gone too far, last buffer is not loadedd
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001614 if (addr_type == ADDR_LOADED_BUFFERS)
1615 while (buf->b_ml.ml_mfp == NULL)
1616 {
1617 nextbuf = (offset >= 0) ? buf->b_prev : buf->b_next;
1618 if (nextbuf == NULL)
1619 break;
1620 buf = nextbuf;
1621 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001622 return buf->b_fnum;
1623}
1624
Bram Moolenaarb7316892019-05-01 18:08:42 +02001625/*
1626 * Return the window number of "win".
1627 * When "win" is NULL return the number of windows.
1628 */
Bram Moolenaarf240e182014-11-27 18:33:02 +01001629 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001630current_win_nr(win_T *win)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001631{
1632 win_T *wp;
1633 int nr = 0;
1634
Bram Moolenaar29323592016-07-24 22:04:11 +02001635 FOR_ALL_WINDOWS(wp)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001636 {
1637 ++nr;
1638 if (wp == win)
1639 break;
1640 }
1641 return nr;
1642}
1643
1644 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001645current_tab_nr(tabpage_T *tab)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001646{
1647 tabpage_T *tp;
1648 int nr = 0;
1649
Bram Moolenaar29323592016-07-24 22:04:11 +02001650 FOR_ALL_TABPAGES(tp)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001651 {
1652 ++nr;
1653 if (tp == tab)
1654 break;
1655 }
1656 return nr;
1657}
1658
Bram Moolenaar6e36b1c2020-07-17 20:47:51 +02001659 static int
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001660comment_start(char_u *p, int starts_with_colon UNUSED)
1661{
1662#ifdef FEAT_EVAL
1663 if (in_vim9script())
1664 return p[0] == '#' && p[1] != '{' && !starts_with_colon;
1665#endif
1666 return *p == '"';
1667}
1668
Bram Moolenaarf240e182014-11-27 18:33:02 +01001669# define CURRENT_WIN_NR current_win_nr(curwin)
1670# define LAST_WIN_NR current_win_nr(NULL)
1671# define CURRENT_TAB_NR current_tab_nr(curtab)
1672# define LAST_TAB_NR current_tab_nr(NULL)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001673
Bram Moolenaar071d4272004-06-13 20:20:40 +00001674/*
1675 * Execute one Ex command.
1676 *
1677 * If 'sourcing' is TRUE, the command will be included in the error message.
1678 *
1679 * 1. skip comment lines and leading space
1680 * 2. handle command modifiers
Bram Moolenaar1c40a662014-11-27 16:38:11 +01001681 * 3. find the command
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001682 * 4. parse range
Bram Moolenaar1c40a662014-11-27 16:38:11 +01001683 * 5. Parse the command.
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001684 * 6. parse arguments
1685 * 7. switch on command name
Bram Moolenaar071d4272004-06-13 20:20:40 +00001686 *
Bram Moolenaar89d40322006-08-29 15:30:07 +00001687 * Note: "fgetline" can be NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001688 *
1689 * This function may be called recursively!
1690 */
1691#if (_MSC_VER == 1200)
1692/*
Bram Moolenaared203462004-06-16 11:19:22 +00001693 * Avoid optimisation bug in VC++ version 6.0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001694 */
Bram Moolenaar281bdce2005-01-25 21:53:18 +00001695 #pragma optimize( "g", off )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001696#endif
1697 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001698do_one_cmd(
Bram Moolenaarddef1292019-12-16 17:10:33 +01001699 char_u **cmdlinep,
1700 int sourcing,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001701#ifdef FEAT_EVAL
Bram Moolenaarddef1292019-12-16 17:10:33 +01001702 cstack_T *cstack,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001703#endif
Bram Moolenaar20b23c62020-08-20 15:25:00 +02001704 char_u *(*fgetline)(int, void *, int, getline_opt_T),
Bram Moolenaarddef1292019-12-16 17:10:33 +01001705 void *cookie) // argument for fgetline()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001706{
Bram Moolenaarddef1292019-12-16 17:10:33 +01001707 char_u *p;
1708 linenr_T lnum;
1709 long n;
1710 char *errormsg = NULL; // error message
1711 char_u *after_modifier = NULL;
1712 exarg_T ea; // Ex command arguments
1713 int save_msg_scroll = msg_scroll;
1714 cmdmod_T save_cmdmod;
1715 int save_reg_executing = reg_executing;
1716 int ni; // set when Not Implemented
1717 char_u *cmd;
Bram Moolenaar6e36b1c2020-07-17 20:47:51 +02001718 int starts_with_colon = FALSE;
Bram Moolenaar83f37b92020-02-23 14:35:01 +01001719#ifdef FEAT_EVAL
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02001720 int may_have_range;
Bram Moolenaar3d48e252020-07-15 14:15:52 +02001721 int vim9script = in_vim9script();
Bram Moolenaar83f37b92020-02-23 14:35:01 +01001722#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001723
Bram Moolenaara80faa82020-04-12 19:37:17 +02001724 CLEAR_FIELD(ea);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001725 ea.line1 = 1;
1726 ea.line2 = 1;
1727#ifdef FEAT_EVAL
1728 ++ex_nesting_level;
1729#endif
1730
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001731 // When the last file has not been edited :q has to be typed twice.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001732 if (quitmore
1733#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001734 // avoid that a function call in 'statusline' does this
Bram Moolenaar89d40322006-08-29 15:30:07 +00001735 && !getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001736#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001737 // avoid that an autocommand, e.g. QuitPre, does this
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001738 && !getline_equal(fgetline, cookie, getnextac))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001739 --quitmore;
1740
1741 /*
1742 * Reset browse, confirm, etc.. They are restored when returning, for
1743 * recursive calls.
1744 */
1745 save_cmdmod = cmdmod;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001746
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001747 // "#!anything" is handled like a comment.
Bram Moolenaarcbb37ad2006-08-16 15:04:21 +00001748 if ((*cmdlinep)[0] == '#' && (*cmdlinep)[1] == '!')
1749 goto doend;
1750
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02001751/*
1752 * 1. Skip comment lines and leading white space and colons.
1753 * 2. Handle command modifiers.
1754 */
1755 // The "ea" structure holds the arguments that can be used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001756 ea.cmd = *cmdlinep;
Bram Moolenaareffed932018-08-14 13:38:17 +02001757 ea.cmdlinep = cmdlinep;
1758 ea.getline = fgetline;
1759 ea.cookie = cookie;
1760#ifdef FEAT_EVAL
1761 ea.cstack = cstack;
Bram Moolenaar83f37b92020-02-23 14:35:01 +01001762 starts_with_colon = *skipwhite(ea.cmd) == ':';
Bram Moolenaar071d4272004-06-13 20:20:40 +00001763#endif
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02001764 if (parse_command_modifiers(&ea, &errormsg, FALSE) == FAIL)
Bram Moolenaareffed932018-08-14 13:38:17 +02001765 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001766
Bram Moolenaar7b668e82016-08-23 23:51:21 +02001767 after_modifier = ea.cmd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001768
1769#ifdef FEAT_EVAL
1770 ea.skip = did_emsg || got_int || did_throw || (cstack->cs_idx >= 0
1771 && !(cstack->cs_flags[cstack->cs_idx] & CSF_ACTIVE));
1772#else
1773 ea.skip = (if_level > 0);
1774#endif
1775
Bram Moolenaar071d4272004-06-13 20:20:40 +00001776/*
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001777 * 3. Skip over the range to find the command. Let "p" point to after it.
1778 *
1779 * We need the command to know what kind of range it uses.
1780 */
1781 cmd = ea.cmd;
Bram Moolenaar3d48e252020-07-15 14:15:52 +02001782#ifdef FEAT_EVAL
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02001783 // In Vim9 script a colon is required before the range.
1784 may_have_range = !vim9script || starts_with_colon;
1785 if (may_have_range)
Bram Moolenaar3d48e252020-07-15 14:15:52 +02001786#endif
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02001787 ea.cmd = skip_range(ea.cmd, TRUE, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001788
1789#ifdef FEAT_EVAL
Bram Moolenaar3d48e252020-07-15 14:15:52 +02001790 if (vim9script && !starts_with_colon)
Bram Moolenaardf069ee2020-06-22 23:02:51 +02001791 {
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02001792 if (ea.cmd == cmd + 1 && *cmd == '$')
1793 // should be "$VAR = val"
1794 --ea.cmd;
1795 else if (ea.cmd > cmd)
Bram Moolenaardf069ee2020-06-22 23:02:51 +02001796 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02001797 emsg(_(e_colon_required_before_a_range));
Bram Moolenaardf069ee2020-06-22 23:02:51 +02001798 goto doend;
1799 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001800 p = find_ex_command(&ea, NULL, lookup_scriptvar, NULL);
Bram Moolenaardf069ee2020-06-22 23:02:51 +02001801 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001802 else
1803#endif
1804 p = find_ex_command(&ea, NULL, NULL, NULL);
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001805
Bram Moolenaar7feb35e2018-08-21 17:49:54 +02001806#ifdef FEAT_EVAL
1807# ifdef FEAT_PROFILE
1808 // Count this line for profiling if skip is TRUE.
1809 if (do_profiling == PROF_YES
1810 && (!ea.skip || cstack->cs_idx == 0 || (cstack->cs_idx > 0
1811 && (cstack->cs_flags[cstack->cs_idx - 1] & CSF_ACTIVE))))
1812 {
1813 int skip = did_emsg || got_int || did_throw;
1814
1815 if (ea.cmdidx == CMD_catch)
1816 skip = !skip && !(cstack->cs_idx >= 0
1817 && (cstack->cs_flags[cstack->cs_idx] & CSF_THROWN)
1818 && !(cstack->cs_flags[cstack->cs_idx] & CSF_CAUGHT));
1819 else if (ea.cmdidx == CMD_else || ea.cmdidx == CMD_elseif)
1820 skip = skip || !(cstack->cs_idx >= 0
1821 && !(cstack->cs_flags[cstack->cs_idx]
1822 & (CSF_ACTIVE | CSF_TRUE)));
1823 else if (ea.cmdidx == CMD_finally)
1824 skip = FALSE;
1825 else if (ea.cmdidx != CMD_endif
1826 && ea.cmdidx != CMD_endfor
1827 && ea.cmdidx != CMD_endtry
1828 && ea.cmdidx != CMD_endwhile)
1829 skip = ea.skip;
1830
1831 if (!skip)
1832 {
1833 if (getline_equal(fgetline, cookie, get_func_line))
1834 func_line_exec(getline_cookie(fgetline, cookie));
1835 else if (getline_equal(fgetline, cookie, getsourceline))
1836 script_line_exec();
1837 }
1838 }
1839# endif
1840
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001841 // May go to debug mode. If this happens and the ">quit" debug command is
1842 // used, throw an interrupt exception and skip the next command.
Bram Moolenaar7feb35e2018-08-21 17:49:54 +02001843 dbg_check_breakpoint(&ea);
1844 if (!ea.skip && got_int)
1845 {
1846 ea.skip = TRUE;
1847 (void)do_intthrow(cstack);
1848 }
1849#endif
1850
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001851/*
1852 * 4. parse a range specifier of the form: addr [,addr] [;addr] ..
Bram Moolenaar071d4272004-06-13 20:20:40 +00001853 *
1854 * where 'addr' is:
1855 *
1856 * % (entire file)
1857 * $ [+-NUM]
1858 * 'x [+-NUM] (where x denotes a currently defined mark)
1859 * . [+-NUM]
1860 * [+-NUM]..
1861 * NUM
1862 *
1863 * The ea.cmd pointer is updated to point to the first character following the
1864 * range spec. If an initial address is found, but no second, the upper bound
1865 * is equal to the lower.
1866 */
1867
Bram Moolenaar25190db2019-05-04 15:05:28 +02001868 // ea.addr_type for user commands is set by find_ucmd
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01001869 if (!IS_USER_CMDIDX(ea.cmdidx))
1870 {
1871 if (ea.cmdidx != CMD_SIZE)
1872 ea.addr_type = cmdnames[(int)ea.cmdidx].cmd_addr_type;
1873 else
1874 ea.addr_type = ADDR_LINES;
1875
Bram Moolenaar25190db2019-05-04 15:05:28 +02001876 // :wincmd range depends on the argument.
Bram Moolenaar164f3262015-01-14 21:22:01 +01001877 if (ea.cmdidx == CMD_wincmd && p != NULL)
1878 get_wincmd_addr_type(skipwhite(p), &ea);
Bram Moolenaar25190db2019-05-04 15:05:28 +02001879#ifdef FEAT_QUICKFIX
1880 // :.cc in quickfix window uses line number
1881 if ((ea.cmdidx == CMD_cc || ea.cmdidx == CMD_ll) && bt_quickfix(curbuf))
1882 ea.addr_type = ADDR_OTHER;
1883#endif
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01001884 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001885
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01001886 ea.cmd = cmd;
Bram Moolenaar3d48e252020-07-15 14:15:52 +02001887#ifdef FEAT_EVAL
Bram Moolenaarc2af0af2020-08-23 21:06:02 +02001888 if (!may_have_range)
1889 ea.line1 = ea.line2 = default_address(&ea);
1890 else
Bram Moolenaar3d48e252020-07-15 14:15:52 +02001891#endif
1892 if (parse_cmd_address(&ea, &errormsg, FALSE) == FAIL)
1893 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001894
Bram Moolenaar071d4272004-06-13 20:20:40 +00001895/*
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001896 * 5. Parse the command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001897 */
1898
1899 /*
1900 * Skip ':' and any white space
1901 */
1902 ea.cmd = skipwhite(ea.cmd);
1903 while (*ea.cmd == ':')
1904 ea.cmd = skipwhite(ea.cmd + 1);
1905
1906 /*
1907 * If we got a line, but no command, then go to the line.
1908 * If we find a '|' or '\n' we set ea.nextcmd.
1909 */
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001910 if (*ea.cmd == NUL || comment_start(ea.cmd, starts_with_colon)
1911 || (ea.nextcmd = check_nextcmd(ea.cmd)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001912 {
1913 /*
1914 * strange vi behaviour:
1915 * ":3" jumps to line 3
1916 * ":3|..." prints line 3
1917 * ":|" prints current line
1918 */
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001919 if (ea.skip) // skip this if inside :if
Bram Moolenaar071d4272004-06-13 20:20:40 +00001920 goto doend;
Bram Moolenaardf177f62005-02-22 08:39:57 +00001921 if (*ea.cmd == '|' || (exmode_active && ea.line1 != ea.line2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001922 {
1923 ea.cmdidx = CMD_print;
Bram Moolenaar8071cb22019-07-12 17:58:01 +02001924 ea.argt = EX_RANGE+EX_COUNT+EX_TRLBAR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001925 if ((errormsg = invalid_range(&ea)) == NULL)
1926 {
1927 correct_range(&ea);
1928 ex_print(&ea);
1929 }
1930 }
1931 else if (ea.addr_count != 0)
1932 {
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00001933 if (ea.line2 > curbuf->b_ml.ml_line_count)
1934 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001935 // With '-' in 'cpoptions' a line number past the file is an
1936 // error, otherwise put it at the end of the file.
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00001937 if (vim_strchr(p_cpo, CPO_MINUS) != NULL)
1938 ea.line2 = -1;
1939 else
1940 ea.line2 = curbuf->b_ml.ml_line_count;
1941 }
1942
1943 if (ea.line2 < 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001944 errormsg = _(e_invrange);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001945 else
1946 {
1947 if (ea.line2 == 0)
1948 curwin->w_cursor.lnum = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001949 else
1950 curwin->w_cursor.lnum = ea.line2;
1951 beginline(BL_SOL | BL_FIX);
1952 }
1953 }
1954 goto doend;
1955 }
1956
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001957 // If this looks like an undefined user command and there are CmdUndefined
1958 // autocommands defined, trigger the matching autocommands.
Bram Moolenaard5005162014-08-22 23:05:54 +02001959 if (p != NULL && ea.cmdidx == CMD_SIZE && !ea.skip
1960 && ASCII_ISUPPER(*ea.cmd)
1961 && has_cmdundefined())
1962 {
Bram Moolenaard5005162014-08-22 23:05:54 +02001963 int ret;
1964
Bram Moolenaar5a31b462014-08-23 14:16:20 +02001965 p = ea.cmd;
Bram Moolenaard5005162014-08-22 23:05:54 +02001966 while (ASCII_ISALNUM(*p))
1967 ++p;
Bram Moolenaar158ea172020-06-18 17:28:39 +02001968 p = vim_strnsave(ea.cmd, p - ea.cmd);
Bram Moolenaard5005162014-08-22 23:05:54 +02001969 ret = apply_autocmds(EVENT_CMDUNDEFINED, p, p, TRUE, NULL);
1970 vim_free(p);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001971 // If the autocommands did something and didn't cause an error, try
1972 // finding the command again.
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001973 p = (ret
1974#ifdef FEAT_EVAL
1975 && !aborting()
Bram Moolenaard5005162014-08-22 23:05:54 +02001976#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001977 ) ? find_ex_command(&ea, NULL, NULL, NULL) : ea.cmd;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001978 }
Bram Moolenaard5005162014-08-22 23:05:54 +02001979
Bram Moolenaar071d4272004-06-13 20:20:40 +00001980 if (p == NULL)
1981 {
1982 if (!ea.skip)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001983 errormsg = _("E464: Ambiguous use of user-defined command");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001984 goto doend;
1985 }
Bram Moolenaarac9fb182019-04-27 13:04:13 +02001986 // Check for wrong commands.
Bram Moolenaar6f9a4762017-06-22 20:39:17 +02001987 if (*p == '!' && ea.cmd[1] == 0151 && ea.cmd[0] == 78
1988 && !IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001989 {
1990 errormsg = uc_fun_cmd();
1991 goto doend;
1992 }
Bram Moolenaarac9fb182019-04-27 13:04:13 +02001993
Bram Moolenaar071d4272004-06-13 20:20:40 +00001994 if (ea.cmdidx == CMD_SIZE)
1995 {
1996 if (!ea.skip)
1997 {
1998 STRCPY(IObuff, _("E492: Not an editor command"));
1999 if (!sourcing)
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002000 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002001 // If the modifier was parsed OK the error must be in the
2002 // following command
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002003 if (after_modifier != NULL)
2004 append_command(after_modifier);
2005 else
2006 append_command(*cmdlinep);
2007 }
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002008 errormsg = (char *)IObuff;
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02002009 did_emsg_syntax = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002010 }
2011 goto doend;
2012 }
2013
Bram Moolenaar958636c2014-10-21 20:01:58 +02002014 ni = (!IS_USER_CMDIDX(ea.cmdidx)
2015 && (cmdnames[ea.cmdidx].cmd_func == ex_ni
Bram Moolenaar7bb75552007-07-16 18:39:49 +00002016#ifdef HAVE_EX_SCRIPT_NI
2017 || cmdnames[ea.cmdidx].cmd_func == ex_script_ni
2018#endif
2019 ));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002020
2021#ifndef FEAT_EVAL
2022 /*
2023 * When the expression evaluation is disabled, recognize the ":if" and
2024 * ":endif" commands and ignore everything in between it.
2025 */
2026 if (ea.cmdidx == CMD_if)
2027 ++if_level;
2028 if (if_level)
2029 {
2030 if (ea.cmdidx == CMD_endif)
2031 --if_level;
2032 goto doend;
2033 }
2034
2035#endif
2036
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002037 // forced commands
Bram Moolenaar196b3b02008-06-20 16:51:41 +00002038 if (*p == '!' && ea.cmdidx != CMD_substitute
2039 && ea.cmdidx != CMD_smagic && ea.cmdidx != CMD_snomagic)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002040 {
2041 ++p;
2042 ea.forceit = TRUE;
2043 }
2044 else
2045 ea.forceit = FALSE;
2046
2047/*
Bram Moolenaarb7316892019-05-01 18:08:42 +02002048 * 6. Parse arguments. Then check for errors.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002049 */
Bram Moolenaar958636c2014-10-21 20:01:58 +02002050 if (!IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002051 ea.argt = (long)cmdnames[(int)ea.cmdidx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002052
2053 if (!ea.skip)
2054 {
2055#ifdef HAVE_SANDBOX
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002056 if (sandbox != 0 && !(ea.argt & EX_SBOXOK))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002057 {
Bram Moolenaar8c62a082019-02-08 14:34:10 +01002058 // Command not allowed in sandbox.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002059 errormsg = _(e_sandbox);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002060 goto doend;
2061 }
2062#endif
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002063 if (restricted != 0 && (ea.argt & EX_RESTRICT))
Bram Moolenaar8c62a082019-02-08 14:34:10 +01002064 {
2065 errormsg = _("E981: Command not allowed in rvim");
2066 goto doend;
2067 }
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002068 if (!curbuf->b_p_ma && (ea.argt & EX_MODIFY))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002069 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002070 // Command not allowed in non-'modifiable' buffer
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002071 errormsg = _(e_modifiable);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002072 goto doend;
2073 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002074
Bram Moolenaar37394ff2020-07-25 19:38:18 +02002075 if (!IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002076 {
Bram Moolenaar37394ff2020-07-25 19:38:18 +02002077#ifdef FEAT_CMDWIN
2078 if (cmdwin_type != 0 && !(ea.argt & EX_CMDWIN))
2079 {
2080 // Command not allowed in the command line window
2081 errormsg = _(e_cmdwin);
2082 goto doend;
2083 }
2084#endif
2085 if (text_locked() && !(ea.argt & EX_LOCK_OK))
2086 {
2087 // Command not allowed when text is locked
2088 errormsg = _(get_text_locked_msg());
2089 goto doend;
2090 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002091 }
Bram Moolenaar379fb762018-08-30 15:58:28 +02002092
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002093 // Disallow editing another buffer when "curbuf_lock" is set.
2094 // Do allow ":checktime" (it is postponed).
2095 // Do allow ":edit" (check for an argument later).
2096 // Do allow ":file" with no arguments (check for an argument later).
Bram Moolenaar8e1986e2020-08-06 22:11:06 +02002097 if (!(ea.argt & (EX_CMDWIN | EX_LOCK_OK))
Bram Moolenaar5555acc2006-04-07 21:33:12 +00002098 && ea.cmdidx != CMD_checktime
Bram Moolenaar379fb762018-08-30 15:58:28 +02002099 && ea.cmdidx != CMD_edit
2100 && ea.cmdidx != CMD_file
Bram Moolenaar958636c2014-10-21 20:01:58 +02002101 && !IS_USER_CMDIDX(ea.cmdidx)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002102 && curbuf_locked())
2103 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002104
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002105 if (!ni && !(ea.argt & EX_RANGE) && ea.addr_count > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002106 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002107 // no range allowed
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002108 errormsg = _(e_norange);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002109 goto doend;
2110 }
2111 }
2112
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002113 if (!ni && !(ea.argt & EX_BANG) && ea.forceit) // no <!> allowed
Bram Moolenaar071d4272004-06-13 20:20:40 +00002114 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002115 errormsg = _(e_nobang);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002116 goto doend;
2117 }
2118
2119 /*
2120 * Don't complain about the range if it is not used
2121 * (could happen if line_count is accidentally set to 0).
2122 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002123 if (!ea.skip && !ni && (ea.argt & EX_RANGE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002124 {
2125 /*
2126 * If the range is backwards, ask for confirmation and, if given, swap
2127 * ea.line1 & ea.line2 so it's forwards again.
2128 * When global command is busy, don't ask, will fail below.
2129 */
2130 if (!global_busy && ea.line1 > ea.line2)
2131 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002132 if (msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002133 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002134 if (sourcing || exmode_active)
2135 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002136 errormsg = _("E493: Backwards range given");
Bram Moolenaara5792f52005-11-23 21:25:05 +00002137 goto doend;
2138 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002139 if (ask_yesno((char_u *)
2140 _("Backwards range given, OK to swap"), FALSE) != 'y')
Bram Moolenaara5792f52005-11-23 21:25:05 +00002141 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002142 }
2143 lnum = ea.line1;
2144 ea.line1 = ea.line2;
2145 ea.line2 = lnum;
2146 }
2147 if ((errormsg = invalid_range(&ea)) != NULL)
2148 goto doend;
2149 }
2150
Bram Moolenaarb7316892019-05-01 18:08:42 +02002151 if ((ea.addr_type == ADDR_OTHER) && ea.addr_count == 0)
2152 // default is 1, not cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00002153 ea.line2 = 1;
2154
2155 correct_range(&ea);
2156
2157#ifdef FEAT_FOLDING
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002158 if (((ea.argt & EX_WHOLEFOLD) || ea.addr_count >= 2) && !global_busy
Bram Moolenaara3306952016-01-02 21:41:06 +01002159 && ea.addr_type == ADDR_LINES)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002160 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002161 // Put the first line at the start of a closed fold, put the last line
2162 // at the end of a closed fold.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002163 (void)hasFolding(ea.line1, &ea.line1, NULL);
2164 (void)hasFolding(ea.line2, NULL, &ea.line2);
2165 }
2166#endif
2167
2168#ifdef FEAT_QUICKFIX
2169 /*
Bram Moolenaar86b68352004-12-27 21:59:20 +00002170 * For the ":make" and ":grep" commands we insert the 'makeprg'/'grepprg'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002171 * option here, so things like % get expanded.
2172 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002173 p = replace_makeprg(&ea, p, cmdlinep);
2174 if (p == NULL)
2175 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002176#endif
2177
2178 /*
2179 * Skip to start of argument.
2180 * Don't do this for the ":!" command, because ":!! -l" needs the space.
2181 */
2182 if (ea.cmdidx == CMD_bang)
2183 ea.arg = p;
2184 else
2185 ea.arg = skipwhite(p);
2186
Bram Moolenaar379fb762018-08-30 15:58:28 +02002187 // ":file" cannot be run with an argument when "curbuf_lock" is set
2188 if (ea.cmdidx == CMD_file && *ea.arg != NUL && curbuf_locked())
2189 goto doend;
2190
Bram Moolenaar071d4272004-06-13 20:20:40 +00002191 /*
2192 * Check for "++opt=val" argument.
2193 * Must be first, allow ":w ++enc=utf8 !cmd"
2194 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002195 if (ea.argt & EX_ARGOPT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002196 while (ea.arg[0] == '+' && ea.arg[1] == '+')
2197 if (getargopt(&ea) == FAIL && !ni)
2198 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002199 errormsg = _(e_invarg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002200 goto doend;
2201 }
2202
2203 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
2204 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002205 if (*ea.arg == '>') // append
Bram Moolenaar071d4272004-06-13 20:20:40 +00002206 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002207 if (*++ea.arg != '>') // typed wrong
Bram Moolenaar071d4272004-06-13 20:20:40 +00002208 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002209 errormsg = _("E494: Use w or w>>");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002210 goto doend;
2211 }
2212 ea.arg = skipwhite(ea.arg + 1);
2213 ea.append = TRUE;
2214 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002215 else if (*ea.arg == '!' && ea.cmdidx == CMD_write) // :w !filter
Bram Moolenaar071d4272004-06-13 20:20:40 +00002216 {
2217 ++ea.arg;
2218 ea.usefilter = TRUE;
2219 }
2220 }
2221
2222 if (ea.cmdidx == CMD_read)
2223 {
2224 if (ea.forceit)
2225 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002226 ea.usefilter = TRUE; // :r! filter if ea.forceit
Bram Moolenaar071d4272004-06-13 20:20:40 +00002227 ea.forceit = FALSE;
2228 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002229 else if (*ea.arg == '!') // :r !filter
Bram Moolenaar071d4272004-06-13 20:20:40 +00002230 {
2231 ++ea.arg;
2232 ea.usefilter = TRUE;
2233 }
2234 }
2235
2236 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
2237 {
2238 ea.amount = 1;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002239 while (*ea.arg == *ea.cmd) // count number of '>' or '<'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002240 {
2241 ++ea.arg;
2242 ++ea.amount;
2243 }
2244 ea.arg = skipwhite(ea.arg);
2245 }
2246
2247 /*
2248 * Check for "+command" argument, before checking for next command.
2249 * Don't do this for ":read !cmd" and ":write !cmd".
2250 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002251 if ((ea.argt & EX_CMDARG) && !ea.usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002252 ea.do_ecmd_cmd = getargcmd(&ea.arg);
2253
2254 /*
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002255 * Check for '|' to separate commands and '"' or '#' to start comments.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002256 * Don't do this for ":read !cmd" and ":write !cmd".
2257 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002258 if ((ea.argt & EX_TRLBAR) && !ea.usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002259 separate_nextcmd(&ea);
2260
2261 /*
2262 * Check for <newline> to end a shell command.
Bram Moolenaardf177f62005-02-22 08:39:57 +00002263 * Also do this for ":read !cmd", ":write !cmd" and ":global".
2264 * Any others?
Bram Moolenaar071d4272004-06-13 20:20:40 +00002265 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00002266 else if (ea.cmdidx == CMD_bang
Bram Moolenaar67883b42017-07-27 22:57:00 +02002267 || ea.cmdidx == CMD_terminal
Bram Moolenaardf177f62005-02-22 08:39:57 +00002268 || ea.cmdidx == CMD_global
2269 || ea.cmdidx == CMD_vglobal
2270 || ea.usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002271 {
2272 for (p = ea.arg; *p; ++p)
2273 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002274 // Remove one backslash before a newline, so that it's possible to
2275 // pass a newline to the shell and also a newline that is preceded
2276 // with a backslash. This makes it impossible to end a shell
2277 // command in a backslash, but that doesn't appear useful.
2278 // Halving the number of backslashes is incompatible with previous
2279 // versions.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002280 if (*p == '\\' && p[1] == '\n')
Bram Moolenaara7241f52008-06-24 20:39:31 +00002281 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002282 else if (*p == '\n')
2283 {
2284 ea.nextcmd = p + 1;
2285 *p = NUL;
2286 break;
2287 }
2288 }
2289 }
2290
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002291 if ((ea.argt & EX_DFLALL) && ea.addr_count == 0)
Bram Moolenaarc2af0af2020-08-23 21:06:02 +02002292 address_default_all(&ea);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002293
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002294 // accept numbered register only when no count allowed (:put)
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002295 if ( (ea.argt & EX_REGSTR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002296 && *ea.arg != NUL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002297 // Do not allow register = for user commands
Bram Moolenaar958636c2014-10-21 20:01:58 +02002298 && (!IS_USER_CMDIDX(ea.cmdidx) || *ea.arg != '=')
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002299 && !((ea.argt & EX_COUNT) && VIM_ISDIGIT(*ea.arg)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002300 {
Bram Moolenaar85de2062011-05-05 14:26:41 +02002301#ifndef FEAT_CLIPBOARD
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002302 // check these explicitly for a more specific error message
Bram Moolenaar85de2062011-05-05 14:26:41 +02002303 if (*ea.arg == '*' || *ea.arg == '+')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002304 {
Bram Moolenaar99b12722019-01-14 20:16:40 +01002305 errormsg = _(e_invalidreg);
Bram Moolenaar85de2062011-05-05 14:26:41 +02002306 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002307 }
2308#endif
Bram Moolenaar958636c2014-10-21 20:01:58 +02002309 if (valid_yank_reg(*ea.arg, (ea.cmdidx != CMD_put
2310 && !IS_USER_CMDIDX(ea.cmdidx))))
Bram Moolenaar85de2062011-05-05 14:26:41 +02002311 {
2312 ea.regname = *ea.arg++;
2313#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002314 // for '=' register: accept the rest of the line as an expression
Bram Moolenaar85de2062011-05-05 14:26:41 +02002315 if (ea.arg[-1] == '=' && ea.arg[0] != NUL)
2316 {
2317 set_expr_line(vim_strsave(ea.arg));
2318 ea.arg += STRLEN(ea.arg);
2319 }
2320#endif
2321 ea.arg = skipwhite(ea.arg);
2322 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002323 }
2324
2325 /*
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002326 * Check for a count. When accepting a EX_BUFNAME, don't use "123foo" as a
Bram Moolenaar071d4272004-06-13 20:20:40 +00002327 * count, it's a buffer name.
2328 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002329 if ((ea.argt & EX_COUNT) && VIM_ISDIGIT(*ea.arg)
2330 && (!(ea.argt & EX_BUFNAME) || *(p = skipdigits(ea.arg)) == NUL
Bram Moolenaar1c465442017-03-12 20:10:05 +01002331 || VIM_ISWHITE(*p)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002332 {
2333 n = getdigits(&ea.arg);
2334 ea.arg = skipwhite(ea.arg);
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002335 if (n <= 0 && !ni && (ea.argt & EX_ZEROR) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002336 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002337 errormsg = _(e_zerocount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002338 goto doend;
2339 }
Bram Moolenaarb7316892019-05-01 18:08:42 +02002340 if (ea.addr_type != ADDR_LINES) // e.g. :buffer 2, :sleep 3
Bram Moolenaar071d4272004-06-13 20:20:40 +00002341 {
2342 ea.line2 = n;
2343 if (ea.addr_count == 0)
2344 ea.addr_count = 1;
2345 }
2346 else
2347 {
2348 ea.line1 = ea.line2;
2349 ea.line2 += n - 1;
2350 ++ea.addr_count;
2351 /*
2352 * Be vi compatible: no error message for out of range.
2353 */
Bram Moolenaarb7316892019-05-01 18:08:42 +02002354 if (ea.line2 > curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002355 ea.line2 = curbuf->b_ml.ml_line_count;
2356 }
2357 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00002358
2359 /*
2360 * Check for flags: 'l', 'p' and '#'.
2361 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002362 if (ea.argt & EX_FLAGS)
Bram Moolenaardf177f62005-02-22 08:39:57 +00002363 get_flags(&ea);
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002364 if (!ni && !(ea.argt & EX_EXTRA) && *ea.arg != NUL
2365 && *ea.arg != '"' && (*ea.arg != '|' || (ea.argt & EX_TRLBAR) == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002366 {
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002367 // no arguments allowed but there is something
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +02002368 errormsg = ex_errmsg(e_trailing_arg, ea.arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002369 goto doend;
2370 }
2371
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002372 if (!ni && (ea.argt & EX_NEEDARG) && *ea.arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002373 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002374 errormsg = _(e_argreq);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002375 goto doend;
2376 }
2377
2378#ifdef FEAT_EVAL
2379 /*
2380 * Skip the command when it's not going to be executed.
2381 * The commands like :if, :endif, etc. always need to be executed.
2382 * Also make an exception for commands that handle a trailing command
2383 * themselves.
2384 */
2385 if (ea.skip)
2386 {
2387 switch (ea.cmdidx)
2388 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002389 // commands that need evaluation
Bram Moolenaar071d4272004-06-13 20:20:40 +00002390 case CMD_while:
2391 case CMD_endwhile:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00002392 case CMD_for:
2393 case CMD_endfor:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002394 case CMD_if:
2395 case CMD_elseif:
2396 case CMD_else:
2397 case CMD_endif:
2398 case CMD_try:
2399 case CMD_catch:
2400 case CMD_finally:
2401 case CMD_endtry:
2402 case CMD_function:
Bram Moolenaarab55c682020-03-01 19:41:43 +01002403 case CMD_def:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002404 break;
2405
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002406 // Commands that handle '|' themselves. Check: A command should
2407 // either have the EX_TRLBAR flag, appear in this list or appear in
2408 // the list at ":help :bar".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002409 case CMD_aboveleft:
2410 case CMD_and:
2411 case CMD_belowright:
2412 case CMD_botright:
2413 case CMD_browse:
2414 case CMD_call:
2415 case CMD_confirm:
Bram Moolenaar8f76e6b2019-11-26 16:50:30 +01002416 case CMD_const:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002417 case CMD_delfunction:
2418 case CMD_djump:
2419 case CMD_dlist:
2420 case CMD_dsearch:
2421 case CMD_dsplit:
2422 case CMD_echo:
2423 case CMD_echoerr:
2424 case CMD_echomsg:
2425 case CMD_echon:
Bram Moolenaara76b3152020-02-16 16:20:21 +01002426 case CMD_eval:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002427 case CMD_execute:
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002428 case CMD_filter:
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002429 case CMD_final:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002430 case CMD_help:
2431 case CMD_hide:
2432 case CMD_ijump:
2433 case CMD_ilist:
2434 case CMD_isearch:
2435 case CMD_isplit:
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002436 case CMD_keepalt:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002437 case CMD_keepjumps:
2438 case CMD_keepmarks:
Bram Moolenaara939e432013-11-09 05:30:26 +01002439 case CMD_keeppatterns:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002440 case CMD_leftabove:
2441 case CMD_let:
2442 case CMD_lockmarks:
Bram Moolenaarcc4423a2019-11-26 17:05:00 +01002443 case CMD_lockvar:
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002444 case CMD_lua:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002445 case CMD_match:
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002446 case CMD_mzscheme:
Bram Moolenaar5803ae62014-03-23 16:04:02 +01002447 case CMD_noautocmd:
2448 case CMD_noswapfile:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002449 case CMD_perl:
2450 case CMD_psearch:
Bram Moolenaar368373e2010-07-19 20:46:22 +02002451 case CMD_py3:
Bram Moolenaarb6590522010-07-21 16:00:43 +02002452 case CMD_python3:
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002453 case CMD_python:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002454 case CMD_return:
2455 case CMD_rightbelow:
2456 case CMD_ruby:
2457 case CMD_silent:
2458 case CMD_smagic:
2459 case CMD_snomagic:
2460 case CMD_substitute:
2461 case CMD_syntax:
Bram Moolenaara226a6d2006-02-26 23:59:20 +00002462 case CMD_tab:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002463 case CMD_tcl:
2464 case CMD_throw:
2465 case CMD_tilde:
2466 case CMD_topleft:
2467 case CMD_unlet:
Bram Moolenaarcc4423a2019-11-26 17:05:00 +01002468 case CMD_unlockvar:
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002469 case CMD_var:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002470 case CMD_verbose:
2471 case CMD_vertical:
Bram Moolenaar12bde492011-06-13 01:19:56 +02002472 case CMD_wincmd:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002473 break;
2474
2475 default: goto doend;
2476 }
2477 }
2478#endif
2479
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002480 if (ea.argt & EX_XFILE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002481 {
2482 if (expand_filename(&ea, cmdlinep, &errormsg) == FAIL)
2483 goto doend;
2484 }
2485
Bram Moolenaar071d4272004-06-13 20:20:40 +00002486 /*
2487 * Accept buffer name. Cannot be used at the same time with a buffer
2488 * number. Don't do this for a user command.
2489 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002490 if ((ea.argt & EX_BUFNAME) && *ea.arg != NUL && ea.addr_count == 0
Bram Moolenaar958636c2014-10-21 20:01:58 +02002491 && !IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002492 {
2493 /*
2494 * :bdelete, :bwipeout and :bunload take several arguments, separated
2495 * by spaces: find next space (skipping over escaped characters).
2496 * The others take one argument: ignore trailing spaces.
2497 */
2498 if (ea.cmdidx == CMD_bdelete || ea.cmdidx == CMD_bwipeout
2499 || ea.cmdidx == CMD_bunload)
2500 p = skiptowhite_esc(ea.arg);
2501 else
2502 {
2503 p = ea.arg + STRLEN(ea.arg);
Bram Moolenaar1c465442017-03-12 20:10:05 +01002504 while (p > ea.arg && VIM_ISWHITE(p[-1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002505 --p;
2506 }
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002507 ea.line2 = buflist_findpat(ea.arg, p, (ea.argt & EX_BUFUNL) != 0,
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002508 FALSE, FALSE);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002509 if (ea.line2 < 0) // failed
Bram Moolenaar071d4272004-06-13 20:20:40 +00002510 goto doend;
2511 ea.addr_count = 1;
2512 ea.arg = skipwhite(p);
2513 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002514
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002515 // The :try command saves the emsg_silent flag, reset it here when
2516 // ":silent! try" was used, it should only apply to :try itself.
Bram Moolenaareffed932018-08-14 13:38:17 +02002517 if (ea.cmdidx == CMD_try && ea.did_esilent > 0)
Bram Moolenaar2be57332018-02-13 18:05:18 +01002518 {
Bram Moolenaareffed932018-08-14 13:38:17 +02002519 emsg_silent -= ea.did_esilent;
Bram Moolenaar2be57332018-02-13 18:05:18 +01002520 if (emsg_silent < 0)
2521 emsg_silent = 0;
Bram Moolenaareffed932018-08-14 13:38:17 +02002522 ea.did_esilent = 0;
Bram Moolenaar2be57332018-02-13 18:05:18 +01002523 }
2524
Bram Moolenaar071d4272004-06-13 20:20:40 +00002525/*
Bram Moolenaar2be57332018-02-13 18:05:18 +01002526 * 7. Execute the command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002527 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002528
Bram Moolenaar958636c2014-10-21 20:01:58 +02002529 if (IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002530 {
2531 /*
2532 * Execute a user-defined command.
2533 */
2534 do_ucmd(&ea);
2535 }
2536 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002537 {
2538 /*
Bram Moolenaarac9fb182019-04-27 13:04:13 +02002539 * Call the function to execute the builtin command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002540 */
2541 ea.errmsg = NULL;
2542 (cmdnames[ea.cmdidx].cmd_func)(&ea);
2543 if (ea.errmsg != NULL)
Bram Moolenaar8930caa2020-07-23 16:37:03 +02002544 errormsg = ea.errmsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002545 }
2546
2547#ifdef FEAT_EVAL
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002548 // Set flag that any command was executed, used by ex_vim9script().
Bram Moolenaar16531552020-02-08 16:00:46 +01002549 if (getline_equal(ea.getline, ea.cookie, getsourceline)
2550 && current_sctx.sc_sid > 0)
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002551 SCRIPT_ITEM(current_sctx.sc_sid)->sn_had_command = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002552
Bram Moolenaar071d4272004-06-13 20:20:40 +00002553 /*
2554 * If the command just executed called do_cmdline(), any throw or ":return"
2555 * or ":finish" encountered there must also check the cstack of the still
2556 * active do_cmdline() that called this do_one_cmd(). Rethrow an uncaught
2557 * exception, or reanimate a returned function or finished script file and
2558 * return or finish it again.
2559 */
2560 if (need_rethrow)
2561 do_throw(cstack);
2562 else if (check_cstack)
2563 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002564 if (source_finished(fgetline, cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002565 do_finish(&ea, TRUE);
Bram Moolenaar89d40322006-08-29 15:30:07 +00002566 else if (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002567 && current_func_returned())
2568 do_return(&ea, TRUE, FALSE, NULL);
2569 }
2570 need_rethrow = check_cstack = FALSE;
2571#endif
2572
2573doend:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002574 if (curwin->w_cursor.lnum == 0) // can happen with zero line number
Bram Moolenaar6de5e122017-04-20 21:55:44 +02002575 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002576 curwin->w_cursor.lnum = 1;
Bram Moolenaar6de5e122017-04-20 21:55:44 +02002577 curwin->w_cursor.col = 0;
2578 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002579
2580 if (errormsg != NULL && *errormsg != NUL && !did_emsg)
2581 {
2582 if (sourcing)
2583 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002584 if (errormsg != (char *)IObuff)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002585 {
2586 STRCPY(IObuff, errormsg);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002587 errormsg = (char *)IObuff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002588 }
Bram Moolenaara6f4d612011-09-21 19:10:46 +02002589 append_command(*cmdlinep);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002590 }
2591 emsg(errormsg);
2592 }
2593#ifdef FEAT_EVAL
2594 do_errthrow(cstack,
Bram Moolenaar958636c2014-10-21 20:01:58 +02002595 (ea.cmdidx != CMD_SIZE && !IS_USER_CMDIDX(ea.cmdidx))
2596 ? cmdnames[(int)ea.cmdidx].cmd_name : (char_u *)NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002597#endif
2598
Bram Moolenaar47e7d702020-07-05 18:18:42 +02002599 undo_cmdmod(&ea, save_msg_scroll);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002600 cmdmod = save_cmdmod;
Bram Moolenaar9a2c0912019-03-30 14:26:18 +01002601 reg_executing = save_reg_executing;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002602
Bram Moolenaar7171abe2004-10-11 10:06:20 +00002603#ifdef HAVE_SANDBOX
Bram Moolenaareffed932018-08-14 13:38:17 +02002604 if (ea.did_sandbox)
Bram Moolenaar7171abe2004-10-11 10:06:20 +00002605 --sandbox;
2606#endif
2607
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002608 if (ea.nextcmd && *ea.nextcmd == NUL) // not really a next command
Bram Moolenaar071d4272004-06-13 20:20:40 +00002609 ea.nextcmd = NULL;
2610
2611#ifdef FEAT_EVAL
2612 --ex_nesting_level;
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002613 vim_free(ea.cmdline_tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002614#endif
2615
2616 return ea.nextcmd;
2617}
2618#if (_MSC_VER == 1200)
Bram Moolenaar281bdce2005-01-25 21:53:18 +00002619 #pragma optimize( "", on )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002620#endif
2621
Bram Moolenaar8930caa2020-07-23 16:37:03 +02002622static char ex_error_buf[MSG_BUF_LEN];
2623
2624/*
2625 * Return an error message with argument included.
2626 * Uses a static buffer, only the last error will be kept.
2627 * "msg" will be translated, caller should use N_().
2628 */
2629 char *
2630ex_errmsg(char *msg, char_u *arg)
2631{
2632 vim_snprintf(ex_error_buf, MSG_BUF_LEN, _(msg), arg);
2633 return ex_error_buf;
2634}
2635
Bram Moolenaar071d4272004-06-13 20:20:40 +00002636/*
Bram Moolenaareffed932018-08-14 13:38:17 +02002637 * Parse and skip over command modifiers:
2638 * - update eap->cmd
2639 * - store flags in "cmdmod".
2640 * - Set ex_pressedreturn for an empty command line.
2641 * - set msg_silent for ":silent"
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002642 * - set 'eventignore' to "all" for ":noautocmd"
Bram Moolenaareffed932018-08-14 13:38:17 +02002643 * - set p_verbose for ":verbose"
2644 * - Increment "sandbox" for ":sandbox"
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002645 * When "skip_only" is TRUE the global variables are not changed, except for
2646 * "cmdmod".
Bram Moolenaareffed932018-08-14 13:38:17 +02002647 * Return FAIL when the command is not to be executed.
2648 * May set "errormsg" to an error message.
2649 */
2650 int
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002651parse_command_modifiers(exarg_T *eap, char **errormsg, int skip_only)
Bram Moolenaareffed932018-08-14 13:38:17 +02002652{
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002653 char_u *p;
2654 int starts_with_colon = FALSE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002655
Bram Moolenaara80faa82020-04-12 19:37:17 +02002656 CLEAR_FIELD(cmdmod);
Bram Moolenaareffed932018-08-14 13:38:17 +02002657 eap->verbose_save = -1;
2658 eap->save_msg_silent = -1;
2659
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002660 // Repeat until no more command modifiers are found.
Bram Moolenaareffed932018-08-14 13:38:17 +02002661 for (;;)
2662 {
Bram Moolenaareffed932018-08-14 13:38:17 +02002663 while (*eap->cmd == ' ' || *eap->cmd == '\t' || *eap->cmd == ':')
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002664 {
2665 if (*eap->cmd == ':')
2666 starts_with_colon = TRUE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002667 ++eap->cmd;
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002668 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002669
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002670 // in ex mode, an empty line works like :+
Bram Moolenaareffed932018-08-14 13:38:17 +02002671 if (*eap->cmd == NUL && exmode_active
2672 && (getline_equal(eap->getline, eap->cookie, getexmodeline)
2673 || getline_equal(eap->getline, eap->cookie, getexline))
2674 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
2675 {
2676 eap->cmd = (char_u *)"+";
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002677 if (!skip_only)
2678 ex_pressedreturn = TRUE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002679 }
2680
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002681 // ignore comment and empty lines
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002682 if (comment_start(eap->cmd, starts_with_colon))
Bram Moolenaareffed932018-08-14 13:38:17 +02002683 return FAIL;
2684 if (*eap->cmd == NUL)
2685 {
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002686 if (!skip_only)
2687 ex_pressedreturn = TRUE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002688 return FAIL;
2689 }
2690
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02002691 p = skip_range(eap->cmd, TRUE, NULL);
Bram Moolenaareffed932018-08-14 13:38:17 +02002692 switch (*p)
2693 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002694 // When adding an entry, also modify cmd_exists().
Bram Moolenaareffed932018-08-14 13:38:17 +02002695 case 'a': if (!checkforcmd(&eap->cmd, "aboveleft", 3))
2696 break;
2697 cmdmod.split |= WSP_ABOVE;
2698 continue;
2699
2700 case 'b': if (checkforcmd(&eap->cmd, "belowright", 3))
2701 {
2702 cmdmod.split |= WSP_BELOW;
2703 continue;
2704 }
2705 if (checkforcmd(&eap->cmd, "browse", 3))
2706 {
2707#ifdef FEAT_BROWSE_CMD
2708 cmdmod.browse = TRUE;
2709#endif
2710 continue;
2711 }
2712 if (!checkforcmd(&eap->cmd, "botright", 2))
2713 break;
2714 cmdmod.split |= WSP_BOT;
2715 continue;
2716
2717 case 'c': if (!checkforcmd(&eap->cmd, "confirm", 4))
2718 break;
2719#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
2720 cmdmod.confirm = TRUE;
2721#endif
2722 continue;
2723
2724 case 'k': if (checkforcmd(&eap->cmd, "keepmarks", 3))
2725 {
2726 cmdmod.keepmarks = TRUE;
2727 continue;
2728 }
2729 if (checkforcmd(&eap->cmd, "keepalt", 5))
2730 {
2731 cmdmod.keepalt = TRUE;
2732 continue;
2733 }
2734 if (checkforcmd(&eap->cmd, "keeppatterns", 5))
2735 {
2736 cmdmod.keeppatterns = TRUE;
2737 continue;
2738 }
2739 if (!checkforcmd(&eap->cmd, "keepjumps", 5))
2740 break;
2741 cmdmod.keepjumps = TRUE;
2742 continue;
2743
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002744 case 'f': // only accept ":filter {pat} cmd"
Bram Moolenaareffed932018-08-14 13:38:17 +02002745 {
2746 char_u *reg_pat;
2747
2748 if (!checkforcmd(&p, "filter", 4)
2749 || *p == NUL || ends_excmd(*p))
2750 break;
2751 if (*p == '!')
2752 {
2753 cmdmod.filter_force = TRUE;
2754 p = skipwhite(p + 1);
2755 if (*p == NUL || ends_excmd(*p))
2756 break;
2757 }
Bram Moolenaar1e624c92020-07-11 14:08:04 +02002758#ifdef FEAT_EVAL
Bram Moolenaarb074e8b2020-07-11 13:40:45 +02002759 // Avoid that "filter(arg)" is recognized.
2760 if (in_vim9script() && !VIM_ISWHITE(*p))
2761 break;
Bram Moolenaar1e624c92020-07-11 14:08:04 +02002762#endif
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002763 if (skip_only)
2764 p = skip_vimgrep_pat(p, NULL, NULL);
2765 else
2766 // NOTE: This puts a NUL after the pattern.
2767 p = skip_vimgrep_pat(p, &reg_pat, NULL);
Bram Moolenaareffed932018-08-14 13:38:17 +02002768 if (p == NULL || *p == NUL)
2769 break;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002770 if (!skip_only)
2771 {
2772 cmdmod.filter_regmatch.regprog =
Bram Moolenaareffed932018-08-14 13:38:17 +02002773 vim_regcomp(reg_pat, RE_MAGIC);
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002774 if (cmdmod.filter_regmatch.regprog == NULL)
2775 break;
2776 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002777 eap->cmd = p;
2778 continue;
2779 }
2780
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002781 // ":hide" and ":hide | cmd" are not modifiers
Bram Moolenaareffed932018-08-14 13:38:17 +02002782 case 'h': if (p != eap->cmd || !checkforcmd(&p, "hide", 3)
2783 || *p == NUL || ends_excmd(*p))
2784 break;
2785 eap->cmd = p;
2786 cmdmod.hide = TRUE;
2787 continue;
2788
2789 case 'l': if (checkforcmd(&eap->cmd, "lockmarks", 3))
2790 {
2791 cmdmod.lockmarks = TRUE;
2792 continue;
2793 }
2794
2795 if (!checkforcmd(&eap->cmd, "leftabove", 5))
2796 break;
2797 cmdmod.split |= WSP_ABOVE;
2798 continue;
2799
2800 case 'n': if (checkforcmd(&eap->cmd, "noautocmd", 3))
2801 {
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002802 if (cmdmod.save_ei == NULL && !skip_only)
Bram Moolenaareffed932018-08-14 13:38:17 +02002803 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002804 // Set 'eventignore' to "all". Restore the
2805 // existing option value later.
Bram Moolenaareffed932018-08-14 13:38:17 +02002806 cmdmod.save_ei = vim_strsave(p_ei);
2807 set_string_option_direct((char_u *)"ei", -1,
2808 (char_u *)"all", OPT_FREE, SID_NONE);
2809 }
2810 continue;
2811 }
2812 if (!checkforcmd(&eap->cmd, "noswapfile", 3))
2813 break;
2814 cmdmod.noswapfile = TRUE;
2815 continue;
2816
2817 case 'r': if (!checkforcmd(&eap->cmd, "rightbelow", 6))
2818 break;
2819 cmdmod.split |= WSP_BELOW;
2820 continue;
2821
2822 case 's': if (checkforcmd(&eap->cmd, "sandbox", 3))
2823 {
2824#ifdef HAVE_SANDBOX
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002825 if (!skip_only)
2826 {
2827 if (!eap->did_sandbox)
2828 ++sandbox;
2829 eap->did_sandbox = TRUE;
2830 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002831#endif
2832 continue;
2833 }
2834 if (!checkforcmd(&eap->cmd, "silent", 3))
2835 break;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002836 if (!skip_only)
2837 {
2838 if (eap->save_msg_silent == -1)
2839 eap->save_msg_silent = msg_silent;
2840 ++msg_silent;
2841 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002842 if (*eap->cmd == '!' && !VIM_ISWHITE(eap->cmd[-1]))
2843 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002844 // ":silent!", but not "silent !cmd"
Bram Moolenaareffed932018-08-14 13:38:17 +02002845 eap->cmd = skipwhite(eap->cmd + 1);
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002846 if (!skip_only)
2847 {
2848 ++emsg_silent;
2849 ++eap->did_esilent;
2850 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002851 }
2852 continue;
2853
2854 case 't': if (checkforcmd(&p, "tab", 3))
2855 {
Bram Moolenaar548e5982018-12-26 21:45:00 +01002856 if (!skip_only)
Bram Moolenaareffed932018-08-14 13:38:17 +02002857 {
Bram Moolenaar548e5982018-12-26 21:45:00 +01002858 long tabnr = get_address(eap, &eap->cmd,
2859 ADDR_TABS, eap->skip,
2860 skip_only, FALSE, 1);
2861 if (tabnr == MAXLNUM)
2862 cmdmod.tab = tabpage_index(curtab) + 1;
2863 else
Bram Moolenaareffed932018-08-14 13:38:17 +02002864 {
Bram Moolenaar548e5982018-12-26 21:45:00 +01002865 if (tabnr < 0 || tabnr > LAST_TAB_NR)
2866 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002867 *errormsg = _(e_invrange);
Bram Moolenaar548e5982018-12-26 21:45:00 +01002868 return FAIL;
2869 }
2870 cmdmod.tab = tabnr + 1;
Bram Moolenaareffed932018-08-14 13:38:17 +02002871 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002872 }
2873 eap->cmd = p;
2874 continue;
2875 }
2876 if (!checkforcmd(&eap->cmd, "topleft", 2))
2877 break;
2878 cmdmod.split |= WSP_TOP;
2879 continue;
2880
2881 case 'u': if (!checkforcmd(&eap->cmd, "unsilent", 3))
2882 break;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002883 if (!skip_only)
2884 {
2885 if (eap->save_msg_silent == -1)
2886 eap->save_msg_silent = msg_silent;
2887 msg_silent = 0;
2888 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002889 continue;
2890
2891 case 'v': if (checkforcmd(&eap->cmd, "vertical", 4))
2892 {
2893 cmdmod.split |= WSP_VERT;
2894 continue;
2895 }
2896 if (!checkforcmd(&p, "verbose", 4))
2897 break;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002898 if (!skip_only)
2899 {
2900 if (eap->verbose_save < 0)
2901 eap->verbose_save = p_verbose;
2902 if (vim_isdigit(*eap->cmd))
2903 p_verbose = atoi((char *)eap->cmd);
2904 else
2905 p_verbose = 1;
2906 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002907 eap->cmd = p;
2908 continue;
2909 }
2910 break;
2911 }
2912
2913 return OK;
2914}
2915
2916/*
Bram Moolenaarb074e8b2020-07-11 13:40:45 +02002917 * Undo and free contents of "cmdmod".
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002918 */
Bram Moolenaar47e7d702020-07-05 18:18:42 +02002919 void
2920undo_cmdmod(exarg_T *eap, int save_msg_scroll)
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002921{
Bram Moolenaar47e7d702020-07-05 18:18:42 +02002922 if (eap->verbose_save >= 0)
2923 p_verbose = eap->verbose_save;
2924
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002925 if (cmdmod.save_ei != NULL)
2926 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002927 // Restore 'eventignore' to the value before ":noautocmd".
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002928 set_string_option_direct((char_u *)"ei", -1, cmdmod.save_ei,
2929 OPT_FREE, SID_NONE);
2930 free_string_option(cmdmod.save_ei);
2931 }
2932
2933 if (cmdmod.filter_regmatch.regprog != NULL)
2934 vim_regfree(cmdmod.filter_regmatch.regprog);
Bram Moolenaar47e7d702020-07-05 18:18:42 +02002935
2936 if (eap->save_msg_silent != -1)
2937 {
2938 // messages could be enabled for a serious error, need to check if the
2939 // counters don't become negative
2940 if (!did_emsg || msg_silent > eap->save_msg_silent)
2941 msg_silent = eap->save_msg_silent;
2942 emsg_silent -= eap->did_esilent;
2943 if (emsg_silent < 0)
2944 emsg_silent = 0;
2945 // Restore msg_scroll, it's set by file I/O commands, even when no
2946 // message is actually displayed.
2947 msg_scroll = save_msg_scroll;
2948
2949 // "silent reg" or "silent echo x" inside "redir" leaves msg_col
2950 // somewhere in the line. Put it back in the first column.
2951 if (redirecting())
2952 msg_col = 0;
2953 }
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002954}
2955
2956/*
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002957 * Parse the address range, if any, in "eap".
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02002958 * May set the last search pattern, unless "silent" is TRUE.
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002959 * Return FAIL and set "errormsg" or return OK.
2960 */
2961 int
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002962parse_cmd_address(exarg_T *eap, char **errormsg, int silent)
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002963{
2964 int address_count = 1;
2965 linenr_T lnum;
2966
2967 // Repeat for all ',' or ';' separated addresses.
2968 for (;;)
2969 {
2970 eap->line1 = eap->line2;
Bram Moolenaarc2af0af2020-08-23 21:06:02 +02002971 eap->line2 = default_address(eap);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002972 eap->cmd = skipwhite(eap->cmd);
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02002973 lnum = get_address(eap, &eap->cmd, eap->addr_type, eap->skip, silent,
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002974 eap->addr_count == 0, address_count++);
2975 if (eap->cmd == NULL) // error detected
2976 return FAIL;
2977 if (lnum == MAXLNUM)
2978 {
2979 if (*eap->cmd == '%') // '%' - all lines
2980 {
2981 ++eap->cmd;
2982 switch (eap->addr_type)
2983 {
2984 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02002985 case ADDR_OTHER:
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002986 eap->line1 = 1;
2987 eap->line2 = curbuf->b_ml.ml_line_count;
2988 break;
2989 case ADDR_LOADED_BUFFERS:
2990 {
2991 buf_T *buf = firstbuf;
2992
2993 while (buf->b_next != NULL
2994 && buf->b_ml.ml_mfp == NULL)
2995 buf = buf->b_next;
2996 eap->line1 = buf->b_fnum;
2997 buf = lastbuf;
2998 while (buf->b_prev != NULL
2999 && buf->b_ml.ml_mfp == NULL)
3000 buf = buf->b_prev;
3001 eap->line2 = buf->b_fnum;
3002 break;
3003 }
3004 case ADDR_BUFFERS:
3005 eap->line1 = firstbuf->b_fnum;
3006 eap->line2 = lastbuf->b_fnum;
3007 break;
3008 case ADDR_WINDOWS:
3009 case ADDR_TABS:
3010 if (IS_USER_CMDIDX(eap->cmdidx))
3011 {
3012 eap->line1 = 1;
3013 eap->line2 = eap->addr_type == ADDR_WINDOWS
3014 ? LAST_WIN_NR : LAST_TAB_NR;
3015 }
3016 else
3017 {
3018 // there is no Vim command which uses '%' and
3019 // ADDR_WINDOWS or ADDR_TABS
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003020 *errormsg = _(e_invrange);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003021 return FAIL;
3022 }
3023 break;
3024 case ADDR_TABS_RELATIVE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02003025 case ADDR_UNSIGNED:
3026 case ADDR_QUICKFIX:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003027 *errormsg = _(e_invrange);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003028 return FAIL;
3029 case ADDR_ARGUMENTS:
3030 if (ARGCOUNT == 0)
3031 eap->line1 = eap->line2 = 0;
3032 else
3033 {
3034 eap->line1 = 1;
3035 eap->line2 = ARGCOUNT;
3036 }
3037 break;
Bram Moolenaar25190db2019-05-04 15:05:28 +02003038 case ADDR_QUICKFIX_VALID:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003039#ifdef FEAT_QUICKFIX
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003040 eap->line1 = 1;
Bram Moolenaar25190db2019-05-04 15:05:28 +02003041 eap->line2 = qf_get_valid_size(eap);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003042 if (eap->line2 == 0)
3043 eap->line2 = 1;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003044#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003045 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02003046 case ADDR_NONE:
3047 // Will give an error later if a range is found.
3048 break;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003049 }
3050 ++eap->addr_count;
3051 }
3052 else if (*eap->cmd == '*' && vim_strchr(p_cpo, CPO_STAR) == NULL)
3053 {
3054 pos_T *fp;
3055
3056 // '*' - visual area
3057 if (eap->addr_type != ADDR_LINES)
3058 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003059 *errormsg = _(e_invrange);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003060 return FAIL;
3061 }
3062
3063 ++eap->cmd;
3064 if (!eap->skip)
3065 {
3066 fp = getmark('<', FALSE);
3067 if (check_mark(fp) == FAIL)
3068 return FAIL;
3069 eap->line1 = fp->lnum;
3070 fp = getmark('>', FALSE);
3071 if (check_mark(fp) == FAIL)
3072 return FAIL;
3073 eap->line2 = fp->lnum;
3074 ++eap->addr_count;
3075 }
3076 }
3077 }
3078 else
3079 eap->line2 = lnum;
3080 eap->addr_count++;
3081
3082 if (*eap->cmd == ';')
3083 {
3084 if (!eap->skip)
3085 {
3086 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaar0e717042020-04-27 19:29:01 +02003087 // Don't leave the cursor on an illegal line or column, but do
3088 // accept zero as address, so 0;/PATTERN/ works correctly.
3089 if (eap->line2 > 0)
3090 check_cursor();
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003091 }
3092 }
3093 else if (*eap->cmd != ',')
3094 break;
3095 ++eap->cmd;
3096 }
3097
3098 // One address given: set start and end lines.
3099 if (eap->addr_count == 1)
3100 {
3101 eap->line1 = eap->line2;
3102 // ... but only implicit: really no address given
3103 if (lnum == MAXLNUM)
3104 eap->addr_count = 0;
3105 }
3106 return OK;
3107}
3108
3109/*
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00003110 * Check for an Ex command with optional tail.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003111 * If there is a match advance "pp" to the argument and return TRUE.
3112 */
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00003113 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003114checkforcmd(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003115 char_u **pp, // start of command
3116 char *cmd, // name of command
3117 int len) // required length
Bram Moolenaar071d4272004-06-13 20:20:40 +00003118{
3119 int i;
3120
3121 for (i = 0; cmd[i] != NUL; ++i)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003122 if (((char_u *)cmd)[i] != (*pp)[i])
Bram Moolenaar071d4272004-06-13 20:20:40 +00003123 break;
3124 if (i >= len && !isalpha((*pp)[i]))
3125 {
3126 *pp = skipwhite(*pp + i);
3127 return TRUE;
3128 }
3129 return FALSE;
3130}
3131
3132/*
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003133 * Append "cmd" to the error message in IObuff.
3134 * Takes care of limiting the length and handling 0xa0, which would be
3135 * invisible otherwise.
3136 */
3137 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003138append_command(char_u *cmd)
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003139{
3140 char_u *s = cmd;
3141 char_u *d;
3142
3143 STRCAT(IObuff, ": ");
3144 d = IObuff + STRLEN(IObuff);
3145 while (*s != NUL && d - IObuff < IOSIZE - 7)
3146 {
Bram Moolenaar13505972019-01-24 15:04:48 +01003147 if (enc_utf8 ? (s[0] == 0xc2 && s[1] == 0xa0) : *s == 0xa0)
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003148 {
Bram Moolenaar13505972019-01-24 15:04:48 +01003149 s += enc_utf8 ? 2 : 1;
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003150 STRCPY(d, "<a0>");
3151 d += 4;
3152 }
3153 else
3154 MB_COPY_CHAR(s, d);
3155 }
3156 *d = NUL;
3157}
3158
3159/*
Bram Moolenaar2e800952020-08-23 19:34:48 +02003160 * If "start" points "&opt", "&l:opt", "&g:opt" or "$ENV" return a pointer to
3161 * the name. Otherwise just return "start".
3162 */
3163 char_u *
3164skip_option_env_lead(char_u *start)
3165{
3166 char_u *name = start;
3167
3168 if (*start == '&')
3169 {
3170 if ((start[1] == 'l' || start[1] == 'g') && start[2] == ':')
3171 name += 3;
3172 else
3173 name += 1;
3174 }
3175 else if (*start == '$')
3176 name += 1;
3177 return name;
3178}
3179
3180/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003181 * Find an Ex command by its name, either built-in or user.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003182 * Start of the name can be found at eap->cmd.
Bram Moolenaar25190db2019-05-04 15:05:28 +02003183 * Sets eap->cmdidx and returns a pointer to char after the command name.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003184 * "full" is set to TRUE if the whole command name matched.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003185 *
3186 * If "lookup" is not NULL recognize expression without "eval" or "call" and
3187 * assignment without "let". Sets eap->cmdidx to the command while returning
3188 * "eap->cmd".
3189 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00003190 * Returns NULL for an ambiguous user command.
3191 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003192 char_u *
3193find_ex_command(
3194 exarg_T *eap,
Bram Moolenaara494f562020-04-18 17:45:38 +02003195 int *full UNUSED,
Bram Moolenaarb84a3812020-05-01 15:44:29 +02003196 void *(*lookup)(char_u *, size_t, cctx_T *) UNUSED,
Bram Moolenaara494f562020-04-18 17:45:38 +02003197 cctx_T *cctx UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003198{
3199 int len;
3200 char_u *p;
Bram Moolenaardf177f62005-02-22 08:39:57 +00003201 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003202
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003203#ifdef FEAT_EVAL
3204 /*
3205 * Recognize a Vim9 script function/method call and assignment:
3206 * "lvar = value", "lvar(arg)", "[1, 2 3]->Func()"
3207 */
Bram Moolenaar0c6ceaf2020-02-22 18:36:32 +01003208 p = eap->cmd;
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003209 if (lookup != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003210 {
Bram Moolenaar2e800952020-08-23 19:34:48 +02003211 char_u *pskip = skip_option_env_lead(eap->cmd);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003212
Bram Moolenaar83144542020-08-02 20:40:43 +02003213 if (vim_strchr((char_u *)"{('[\"@", *p) != NULL
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003214 || ((p = to_name_const_end(pskip)) > eap->cmd && *p != NUL))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003215 {
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003216 int oplen;
3217 int heredoc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003218
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003219 if (
3220 // "(..." is an expression.
3221 // "funcname(" is always a function call.
3222 *p == '('
3223 || (p == eap->cmd
3224 ? (
3225 // "{..." is an dict expression.
3226 *eap->cmd == '{'
3227 // "'string'->func()" is an expression.
3228 || *eap->cmd == '\''
Bram Moolenaar10409562020-07-29 20:00:38 +02003229 // '"string"->func()' is an expression.
3230 || *eap->cmd == '"'
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003231 // "g:varname" is an expression.
3232 || eap->cmd[1] == ':'
3233 )
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003234 // "varname->func()" is an expression.
Bram Moolenaar2b22b112020-09-14 18:35:18 +02003235 : (*p == '-' && p[1] == '>')))
Bram Moolenaarb31be3f2020-07-20 22:37:44 +02003236 {
3237 eap->cmdidx = CMD_eval;
3238 return eap->cmd;
3239 }
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003240
Bram Moolenaar2b22b112020-09-14 18:35:18 +02003241 if (p != eap->cmd && (
3242 // "varname[]" is an expression.
3243 *p == '['
3244 // "varname.key" is an expression.
3245 || (*p == '.' && ASCII_ISALPHA(p[1]))))
3246 {
3247 char_u *after = p;
3248
3249 // When followed by "=" or "+=" then it is an assignment.
3250 ++emsg_silent;
3251 if (skip_expr(&after) == OK
3252 && (*after == '='
3253 || (*after != NUL && after[1] == '=')))
Bram Moolenaar30fd8202020-09-26 15:09:30 +02003254 eap->cmdidx = CMD_var;
Bram Moolenaar2b22b112020-09-14 18:35:18 +02003255 else
3256 eap->cmdidx = CMD_eval;
3257 --emsg_silent;
3258 return eap->cmd;
3259 }
3260
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003261 // "[...]->Method()" is a list expression, but "[a, b] = Func()" is
3262 // an assignment.
3263 // If there is no line break inside the "[...]" then "p" is
3264 // advanced to after the "]" by to_name_const_end(): check if a "="
3265 // follows.
3266 // If "[...]" has a line break "p" still points at the "[" and it
3267 // can't be an assignment.
3268 if (*eap->cmd == '[')
Bram Moolenaarb31be3f2020-07-20 22:37:44 +02003269 {
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003270 p = to_name_const_end(eap->cmd);
3271 if (p == eap->cmd || *skipwhite(p) != '=')
3272 {
3273 eap->cmdidx = CMD_eval;
3274 return eap->cmd;
3275 }
3276 if (p > eap->cmd && *skipwhite(p) == '=')
3277 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02003278 eap->cmdidx = CMD_var;
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003279 return eap->cmd;
3280 }
3281 }
3282
3283 // Recognize an assignment if we recognize the variable name:
3284 // "g:var = expr"
3285 // "var = expr" where "var" is a local var name.
Bram Moolenaar83144542020-08-02 20:40:43 +02003286 if (*eap->cmd == '@')
3287 p = eap->cmd + 2;
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003288 oplen = assignment_len(skipwhite(p), &heredoc);
3289 if (oplen > 0)
3290 {
3291 if (((p - eap->cmd) > 2 && eap->cmd[1] == ':')
3292 || *eap->cmd == '&'
3293 || *eap->cmd == '$'
3294 || *eap->cmd == '@'
3295 || lookup(eap->cmd, p - eap->cmd, cctx) != NULL)
3296 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02003297 eap->cmdidx = CMD_var;
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003298 return eap->cmd;
3299 }
3300 }
3301
3302 // Recognize using a type for a w:, b:, t: or g: variable:
3303 // "w:varname: number = 123".
3304 if (eap->cmd[1] == ':' && *p == ':')
3305 {
3306 eap->cmdidx = CMD_eval;
Bram Moolenaarb31be3f2020-07-20 22:37:44 +02003307 return eap->cmd;
3308 }
Bram Moolenaard2ef6b32020-07-02 21:11:34 +02003309 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003310 }
3311#endif
3312
Bram Moolenaar071d4272004-06-13 20:20:40 +00003313 /*
3314 * Isolate the command and search for it in the command table.
Bram Moolenaar81870892007-11-11 18:17:28 +00003315 * Exceptions:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003316 * - the 'k' command can directly be followed by any character.
3317 * - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003318 * but :sre[wind] is another command, as are :scr[iptnames],
Bram Moolenaar071d4272004-06-13 20:20:40 +00003319 * :scs[cope], :sim[alt], :sig[ns] and :sil[ent].
Bram Moolenaardf177f62005-02-22 08:39:57 +00003320 * - the "d" command can directly be followed by 'l' or 'p' flag.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003321 */
3322 p = eap->cmd;
3323 if (*p == 'k')
3324 {
3325 eap->cmdidx = CMD_k;
3326 ++p;
3327 }
3328 else if (p[0] == 's'
Bram Moolenaar204b93f2015-08-04 22:02:51 +02003329 && ((p[1] == 'c' && (p[2] == NUL || (p[2] != 's' && p[2] != 'r'
3330 && (p[3] == NUL || (p[3] != 'i' && p[4] != 'p')))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003331 || p[1] == 'g'
3332 || (p[1] == 'i' && p[2] != 'm' && p[2] != 'l' && p[2] != 'g')
3333 || p[1] == 'I'
3334 || (p[1] == 'r' && p[2] != 'e')))
3335 {
3336 eap->cmdidx = CMD_substitute;
3337 ++p;
3338 }
3339 else
3340 {
3341 while (ASCII_ISALPHA(*p))
3342 ++p;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003343 // for python 3.x support ":py3", ":python3", ":py3file", etc.
Bram Moolenaar55d5c032010-07-17 23:52:29 +02003344 if (eap->cmd[0] == 'p' && eap->cmd[1] == 'y')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003345 {
Bram Moolenaarb6590522010-07-21 16:00:43 +02003346 while (ASCII_ISALNUM(*p))
3347 ++p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003348 }
3349 else if (*p == '9' && STRNCMP("vim9", eap->cmd, 4) == 0)
3350 {
3351 // include "9" for "vim9script"
3352 ++p;
3353 while (ASCII_ISALPHA(*p))
3354 ++p;
3355 }
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02003356
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003357 // check for non-alpha command
Bram Moolenaar071d4272004-06-13 20:20:40 +00003358 if (p == eap->cmd && vim_strchr((char_u *)"@*!=><&~#", *p) != NULL)
3359 ++p;
3360 len = (int)(p - eap->cmd);
Bram Moolenaardf177f62005-02-22 08:39:57 +00003361 if (*eap->cmd == 'd' && (p[-1] == 'l' || p[-1] == 'p'))
3362 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003363 // Check for ":dl", ":dell", etc. to ":deletel": that's
3364 // :delete with the 'l' flag. Same for 'p'.
Bram Moolenaardf177f62005-02-22 08:39:57 +00003365 for (i = 0; i < len; ++i)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003366 if (eap->cmd[i] != ((char_u *)"delete")[i])
Bram Moolenaardf177f62005-02-22 08:39:57 +00003367 break;
3368 if (i == len - 1)
3369 {
3370 --len;
3371 if (p[-1] == 'l')
3372 eap->flags |= EXFLAG_LIST;
3373 else
3374 eap->flags |= EXFLAG_PRINT;
3375 }
3376 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003377
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003378 if (ASCII_ISLOWER(eap->cmd[0]))
3379 {
Bram Moolenaar6c0c1e82017-03-25 15:07:43 +01003380 int c1 = eap->cmd[0];
Bram Moolenaar94f82cb2019-07-24 22:30:27 +02003381 int c2 = len == 1 ? NUL : eap->cmd[1];
Bram Moolenaar6c0c1e82017-03-25 15:07:43 +01003382
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003383 if (command_count != (int)CMD_SIZE)
3384 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003385 iemsg(_("E943: Command table needs to be updated, run 'make cmdidxs'"));
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003386 getout(1);
3387 }
3388
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003389 // Use a precomputed index for fast look-up in cmdnames[]
3390 // taking into account the first 2 letters of eap->cmd.
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003391 eap->cmdidx = cmdidxs1[CharOrdLow(c1)];
3392 if (ASCII_ISLOWER(c2))
3393 eap->cmdidx += cmdidxs2[CharOrdLow(c1)][CharOrdLow(c2)];
3394 }
Bram Moolenaara494f562020-04-18 17:45:38 +02003395 else if (ASCII_ISUPPER(eap->cmd[0]))
3396 eap->cmdidx = CMD_Next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003397 else
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003398 eap->cmdidx = CMD_bang;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003399
3400 for ( ; (int)eap->cmdidx < (int)CMD_SIZE;
3401 eap->cmdidx = (cmdidx_T)((int)eap->cmdidx + 1))
3402 if (STRNCMP(cmdnames[(int)eap->cmdidx].cmd_name, (char *)eap->cmd,
3403 (size_t)len) == 0)
3404 {
3405#ifdef FEAT_EVAL
3406 if (full != NULL
3407 && cmdnames[(int)eap->cmdidx].cmd_name[len] == NUL)
3408 *full = TRUE;
3409#endif
3410 break;
3411 }
3412
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003413 // Look for a user defined command as a last resort. Let ":Print" be
3414 // overruled by a user defined command.
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01003415 if ((eap->cmdidx == CMD_SIZE || eap->cmdidx == CMD_Print)
3416 && *eap->cmd >= 'A' && *eap->cmd <= 'Z')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003417 {
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003418 // User defined commands may contain digits.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003419 while (ASCII_ISALNUM(*p))
3420 ++p;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003421 p = find_ucmd(eap, p, full, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003422 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003423 if (p == eap->cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003424 eap->cmdidx = CMD_SIZE;
3425 }
3426
Bram Moolenaar373863e2020-09-26 17:20:53 +02003427 // ":fina" means ":finally" for backwards compatibility.
3428 if (eap->cmdidx == CMD_final && p - eap->cmd == 4)
3429 eap->cmdidx = CMD_finally;
3430
Bram Moolenaar071d4272004-06-13 20:20:40 +00003431 return p;
3432}
3433
3434#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003435static struct cmdmod
3436{
3437 char *name;
3438 int minlen;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003439 int has_count; // :123verbose :3tab
Bram Moolenaared53fb92007-11-24 20:50:24 +00003440} cmdmods[] = {
3441 {"aboveleft", 3, FALSE},
3442 {"belowright", 3, FALSE},
3443 {"botright", 2, FALSE},
3444 {"browse", 3, FALSE},
3445 {"confirm", 4, FALSE},
Bram Moolenaar7b668e82016-08-23 23:51:21 +02003446 {"filter", 4, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003447 {"hide", 3, FALSE},
3448 {"keepalt", 5, FALSE},
3449 {"keepjumps", 5, FALSE},
3450 {"keepmarks", 3, FALSE},
Bram Moolenaara939e432013-11-09 05:30:26 +01003451 {"keeppatterns", 5, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003452 {"leftabove", 5, FALSE},
3453 {"lockmarks", 3, FALSE},
Bram Moolenaarca9f9582008-09-18 10:44:28 +00003454 {"noautocmd", 3, FALSE},
Bram Moolenaar5803ae62014-03-23 16:04:02 +01003455 {"noswapfile", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003456 {"rightbelow", 6, FALSE},
3457 {"sandbox", 3, FALSE},
3458 {"silent", 3, FALSE},
3459 {"tab", 3, TRUE},
3460 {"topleft", 2, FALSE},
Bram Moolenaar8e258a42009-07-09 13:55:43 +00003461 {"unsilent", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003462 {"verbose", 4, TRUE},
3463 {"vertical", 4, FALSE},
3464};
3465
3466/*
3467 * Return length of a command modifier (including optional count).
3468 * Return zero when it's not a modifier.
3469 */
3470 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003471modifier_len(char_u *cmd)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003472{
3473 int i, j;
3474 char_u *p = cmd;
3475
3476 if (VIM_ISDIGIT(*cmd))
3477 p = skipwhite(skipdigits(cmd));
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00003478 for (i = 0; i < (int)(sizeof(cmdmods) / sizeof(struct cmdmod)); ++i)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003479 {
3480 for (j = 0; p[j] != NUL; ++j)
3481 if (p[j] != cmdmods[i].name[j])
3482 break;
Bram Moolenaar2d473ab2013-06-12 17:12:24 +02003483 if (!ASCII_ISALPHA(p[j]) && j >= cmdmods[i].minlen
Bram Moolenaared53fb92007-11-24 20:50:24 +00003484 && (p == cmd || cmdmods[i].has_count))
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00003485 return j + (int)(p - cmd);
Bram Moolenaared53fb92007-11-24 20:50:24 +00003486 }
3487 return 0;
3488}
3489
Bram Moolenaar071d4272004-06-13 20:20:40 +00003490/*
3491 * Return > 0 if an Ex command "name" exists.
3492 * Return 2 if there is an exact match.
3493 * Return 3 if there is an ambiguous match.
3494 */
3495 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003496cmd_exists(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003497{
3498 exarg_T ea;
3499 int full = FALSE;
3500 int i;
3501 int j;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003502 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003503
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003504 // Check command modifiers.
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00003505 for (i = 0; i < (int)(sizeof(cmdmods) / sizeof(struct cmdmod)); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003506 {
3507 for (j = 0; name[j] != NUL; ++j)
3508 if (name[j] != cmdmods[i].name[j])
3509 break;
3510 if (name[j] == NUL && j >= cmdmods[i].minlen)
3511 return (cmdmods[i].name[j] == NUL ? 2 : 1);
3512 }
3513
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003514 // Check built-in commands and user defined commands.
3515 // For ":2match" and ":3match" we need to skip the number.
Bram Moolenaara9587612006-05-04 21:47:50 +00003516 ea.cmd = (*name == '2' || *name == '3') ? name + 1 : name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003517 ea.cmdidx = (cmdidx_T)0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003518 p = find_ex_command(&ea, &full, NULL, NULL);
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003519 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003520 return 3;
Bram Moolenaara9587612006-05-04 21:47:50 +00003521 if (vim_isdigit(*name) && ea.cmdidx != CMD_match)
3522 return 0;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003523 if (*skipwhite(p) != NUL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003524 return 0; // trailing garbage
Bram Moolenaar071d4272004-06-13 20:20:40 +00003525 return (ea.cmdidx == CMD_SIZE ? 0 : (full ? 2 : 1));
3526}
3527#endif
3528
Bram Moolenaard0190392019-08-23 21:17:35 +02003529 cmdidx_T
3530excmd_get_cmdidx(char_u *cmd, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003531{
Bram Moolenaard0190392019-08-23 21:17:35 +02003532 cmdidx_T idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003533
Bram Moolenaard0190392019-08-23 21:17:35 +02003534 for (idx = (cmdidx_T)0; (int)idx < (int)CMD_SIZE;
3535 idx = (cmdidx_T)((int)idx + 1))
3536 if (STRNCMP(cmdnames[(int)idx].cmd_name, cmd, (size_t)len) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003537 break;
3538
Bram Moolenaard0190392019-08-23 21:17:35 +02003539 return idx;
3540}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003541
Bram Moolenaard0190392019-08-23 21:17:35 +02003542 long
3543excmd_get_argt(cmdidx_T idx)
3544{
3545 return (long)cmdnames[(int)idx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003546}
3547
3548/*
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003549 * Skip a range specifier of the form: addr [,addr] [;addr] ..
Bram Moolenaar071d4272004-06-13 20:20:40 +00003550 *
3551 * Backslashed delimiters after / or ? will be skipped, and commands will
3552 * not be expanded between /'s and ?'s or after "'".
3553 *
Bram Moolenaardf069ee2020-06-22 23:02:51 +02003554 * Also skip white space and ":" characters after the range.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003555 * Returns the "cmd" pointer advanced to beyond the range.
3556 */
3557 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003558skip_range(
3559 char_u *cmd,
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02003560 int skip_star, // skip "*" used for Visual range
3561 int *ctx) // pointer to xp_context or NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003562{
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003563 unsigned delim;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003564
Bram Moolenaarcbf20fb2017-02-03 21:19:04 +01003565 while (vim_strchr((char_u *)" \t0123456789.$%'/?-+,;\\", *cmd) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003566 {
Bram Moolenaarcbf20fb2017-02-03 21:19:04 +01003567 if (*cmd == '\\')
3568 {
3569 if (cmd[1] == '?' || cmd[1] == '/' || cmd[1] == '&')
3570 ++cmd;
3571 else
3572 break;
3573 }
3574 else if (*cmd == '\'')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003575 {
3576 if (*++cmd == NUL && ctx != NULL)
3577 *ctx = EXPAND_NOTHING;
3578 }
3579 else if (*cmd == '/' || *cmd == '?')
3580 {
3581 delim = *cmd++;
3582 while (*cmd != NUL && *cmd != delim)
3583 if (*cmd++ == '\\' && *cmd != NUL)
3584 ++cmd;
3585 if (*cmd == NUL && ctx != NULL)
3586 *ctx = EXPAND_NOTHING;
3587 }
3588 if (*cmd != NUL)
3589 ++cmd;
3590 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00003591
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003592 // Skip ":" and white space.
Bram Moolenaardf177f62005-02-22 08:39:57 +00003593 while (*cmd == ':')
3594 cmd = skipwhite(cmd + 1);
3595
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02003596 // Skip "*" used for Visual range.
3597 if (skip_star && *cmd == '*' && vim_strchr(p_cpo, CPO_STAR) == NULL)
3598 cmd = skipwhite(cmd + 1);
3599
Bram Moolenaar071d4272004-06-13 20:20:40 +00003600 return cmd;
3601}
3602
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003603 static void
3604addr_error(cmd_addr_T addr_type)
3605{
3606 if (addr_type == ADDR_NONE)
3607 emsg(_(e_norange));
3608 else
3609 emsg(_(e_invrange));
3610}
3611
Bram Moolenaar071d4272004-06-13 20:20:40 +00003612/*
Bram Moolenaarc2af0af2020-08-23 21:06:02 +02003613 * Return the default address for an address type.
3614 */
3615 static linenr_T
3616default_address(exarg_T *eap)
3617{
3618 linenr_T lnum = 0;
3619
3620 switch (eap->addr_type)
3621 {
3622 case ADDR_LINES:
3623 case ADDR_OTHER:
3624 // Default is the cursor line number. Avoid using an invalid
3625 // line number though.
3626 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
3627 lnum = curbuf->b_ml.ml_line_count;
3628 else
3629 lnum = curwin->w_cursor.lnum;
3630 break;
3631 case ADDR_WINDOWS:
3632 lnum = CURRENT_WIN_NR;
3633 break;
3634 case ADDR_ARGUMENTS:
3635 lnum = curwin->w_arg_idx + 1;
3636 if (lnum > ARGCOUNT)
3637 lnum = ARGCOUNT;
3638 break;
3639 case ADDR_LOADED_BUFFERS:
3640 case ADDR_BUFFERS:
3641 lnum = curbuf->b_fnum;
3642 break;
3643 case ADDR_TABS:
3644 lnum = CURRENT_TAB_NR;
3645 break;
3646 case ADDR_TABS_RELATIVE:
3647 case ADDR_UNSIGNED:
3648 lnum = 1;
3649 break;
3650 case ADDR_QUICKFIX:
3651#ifdef FEAT_QUICKFIX
3652 lnum = qf_get_cur_idx(eap);
3653#endif
3654 break;
3655 case ADDR_QUICKFIX_VALID:
3656#ifdef FEAT_QUICKFIX
3657 lnum = qf_get_cur_valid_idx(eap);
3658#endif
3659 break;
3660 case ADDR_NONE:
3661 // Will give an error later if a range is found.
3662 break;
3663 }
3664 return lnum;
3665}
3666
3667/*
Bram Moolenaar198cb662018-09-06 21:44:17 +02003668 * Get a single EX address.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003669 *
3670 * Set ptr to the next character after the part that was interpreted.
3671 * Set ptr to NULL when an error is encountered.
Bram Moolenaar198cb662018-09-06 21:44:17 +02003672 * This may set the last used search pattern.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003673 *
3674 * Return MAXLNUM when no Ex address was found.
3675 */
3676 static linenr_T
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003677get_address(
3678 exarg_T *eap UNUSED,
3679 char_u **ptr,
Bram Moolenaar1b03a192019-12-08 17:08:29 +01003680 cmd_addr_T addr_type,
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02003681 int skip, // only skip the address, don't use it
3682 int silent, // no errors or side effects
3683 int to_other_file, // flag: may jump to other file
3684 int address_count UNUSED) // 1 for first address, >1 after comma
Bram Moolenaar071d4272004-06-13 20:20:40 +00003685{
3686 int c;
3687 int i;
3688 long n;
3689 char_u *cmd;
3690 pos_T pos;
3691 pos_T *fp;
3692 linenr_T lnum;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003693 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003694
3695 cmd = skipwhite(*ptr);
3696 lnum = MAXLNUM;
3697 do
3698 {
3699 switch (*cmd)
3700 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003701 case '.': // '.' - Cursor position
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003702 ++cmd;
3703 switch (addr_type)
3704 {
3705 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02003706 case ADDR_OTHER:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003707 lnum = curwin->w_cursor.lnum;
3708 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003709 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01003710 lnum = CURRENT_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003711 break;
3712 case ADDR_ARGUMENTS:
3713 lnum = curwin->w_arg_idx + 1;
3714 break;
3715 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003716 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003717 lnum = curbuf->b_fnum;
3718 break;
3719 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01003720 lnum = CURRENT_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003721 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02003722 case ADDR_NONE:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003723 case ADDR_TABS_RELATIVE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02003724 case ADDR_UNSIGNED:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003725 addr_error(addr_type);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01003726 cmd = NULL;
3727 goto error;
3728 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003729 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003730#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02003731 lnum = qf_get_cur_idx(eap);
3732#endif
3733 break;
3734 case ADDR_QUICKFIX_VALID:
3735#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003736 lnum = qf_get_cur_valid_idx(eap);
Bram Moolenaare906c502015-09-09 21:10:39 +02003737#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003738 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003739 }
3740 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003741
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003742 case '$': // '$' - last line
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003743 ++cmd;
3744 switch (addr_type)
3745 {
3746 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02003747 case ADDR_OTHER:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003748 lnum = curbuf->b_ml.ml_line_count;
3749 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003750 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01003751 lnum = LAST_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003752 break;
3753 case ADDR_ARGUMENTS:
3754 lnum = ARGCOUNT;
3755 break;
3756 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003757 buf = lastbuf;
3758 while (buf->b_ml.ml_mfp == NULL)
3759 {
3760 if (buf->b_prev == NULL)
3761 break;
3762 buf = buf->b_prev;
3763 }
3764 lnum = buf->b_fnum;
3765 break;
3766 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003767 lnum = lastbuf->b_fnum;
3768 break;
3769 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01003770 lnum = LAST_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003771 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02003772 case ADDR_NONE:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003773 case ADDR_TABS_RELATIVE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02003774 case ADDR_UNSIGNED:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003775 addr_error(addr_type);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01003776 cmd = NULL;
3777 goto error;
3778 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003779 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003780#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003781 lnum = qf_get_size(eap);
3782 if (lnum == 0)
3783 lnum = 1;
Bram Moolenaare906c502015-09-09 21:10:39 +02003784#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003785 break;
Bram Moolenaar25190db2019-05-04 15:05:28 +02003786 case ADDR_QUICKFIX_VALID:
3787#ifdef FEAT_QUICKFIX
3788 lnum = qf_get_valid_size(eap);
3789 if (lnum == 0)
3790 lnum = 1;
3791#endif
3792 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003793 }
3794 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003795
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003796 case '\'': // ''' - mark
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003797 if (*++cmd == NUL)
3798 {
3799 cmd = NULL;
3800 goto error;
3801 }
3802 if (addr_type != ADDR_LINES)
3803 {
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003804 addr_error(addr_type);
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01003805 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003806 goto error;
3807 }
3808 if (skip)
3809 ++cmd;
3810 else
3811 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003812 // Only accept a mark in another file when it is
3813 // used by itself: ":'M".
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003814 fp = getmark(*cmd, to_other_file && cmd[1] == NUL);
3815 ++cmd;
3816 if (fp == (pos_T *)-1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003817 // Jumped to another file.
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003818 lnum = curwin->w_cursor.lnum;
3819 else
3820 {
3821 if (check_mark(fp) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003822 {
3823 cmd = NULL;
3824 goto error;
3825 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003826 lnum = fp->lnum;
3827 }
3828 }
3829 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003830
3831 case '/':
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003832 case '?': // '/' or '?' - search
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003833 c = *cmd++;
3834 if (addr_type != ADDR_LINES)
3835 {
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003836 addr_error(addr_type);
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01003837 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003838 goto error;
3839 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003840 if (skip) // skip "/pat/"
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003841 {
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02003842 cmd = skip_regexp(cmd, c, (int)p_magic);
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003843 if (*cmd == c)
3844 ++cmd;
3845 }
3846 else
3847 {
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02003848 int flags;
3849
3850 pos = curwin->w_cursor; // save curwin->w_cursor
3851
3852 // When '/' or '?' follows another address, start from
3853 // there.
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003854 if (lnum != MAXLNUM)
3855 curwin->w_cursor.lnum = lnum;
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02003856
3857 // Start a forward search at the end of the line (unless
3858 // before the first line).
3859 // Start a backward search at the start of the line.
3860 // This makes sure we never match in the current
3861 // line, and can match anywhere in the
3862 // next/previous line.
Bram Moolenaar8ada6aa2017-12-19 21:23:21 +01003863 if (c == '/' && curwin->w_cursor.lnum > 0)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003864 curwin->w_cursor.col = MAXCOL;
3865 else
3866 curwin->w_cursor.col = 0;
3867 searchcmdlen = 0;
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02003868 flags = silent ? 0 : SEARCH_HIS | SEARCH_MSG;
Bram Moolenaarc036e872020-02-21 21:30:52 +01003869 if (!do_search(NULL, c, c, cmd, 1L, flags, NULL))
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003870 {
3871 curwin->w_cursor = pos;
3872 cmd = NULL;
3873 goto error;
3874 }
3875 lnum = curwin->w_cursor.lnum;
3876 curwin->w_cursor = pos;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003877 // adjust command string pointer
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003878 cmd += searchcmdlen;
3879 }
3880 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003881
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003882 case '\\': // "\?", "\/" or "\&", repeat search
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003883 ++cmd;
3884 if (addr_type != ADDR_LINES)
3885 {
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003886 addr_error(addr_type);
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01003887 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003888 goto error;
3889 }
3890 if (*cmd == '&')
3891 i = RE_SUBST;
3892 else if (*cmd == '?' || *cmd == '/')
3893 i = RE_SEARCH;
3894 else
3895 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003896 emsg(_(e_backslash));
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003897 cmd = NULL;
3898 goto error;
3899 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003900
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003901 if (!skip)
3902 {
3903 /*
3904 * When search follows another address, start from
3905 * there.
3906 */
3907 if (lnum != MAXLNUM)
3908 pos.lnum = lnum;
3909 else
3910 pos.lnum = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003911
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003912 /*
3913 * Start the search just like for the above
3914 * do_search().
3915 */
3916 if (*cmd != '?')
3917 pos.col = MAXCOL;
3918 else
3919 pos.col = 0;
Bram Moolenaarbd8539a2015-08-11 18:53:03 +02003920 pos.coladd = 0;
Bram Moolenaar5d24a222018-12-23 19:10:09 +01003921 if (searchit(curwin, curbuf, &pos, NULL,
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003922 *cmd == '?' ? BACKWARD : FORWARD,
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02003923 (char_u *)"", 1L, SEARCH_MSG, i, NULL) != FAIL)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003924 lnum = pos.lnum;
3925 else
3926 {
3927 cmd = NULL;
3928 goto error;
3929 }
3930 }
3931 ++cmd;
3932 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003933
3934 default:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003935 if (VIM_ISDIGIT(*cmd)) // absolute line number
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003936 lnum = getdigits(&cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003937 }
3938
3939 for (;;)
3940 {
3941 cmd = skipwhite(cmd);
3942 if (*cmd != '-' && *cmd != '+' && !VIM_ISDIGIT(*cmd))
3943 break;
3944
3945 if (lnum == MAXLNUM)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003946 {
3947 switch (addr_type)
3948 {
3949 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02003950 case ADDR_OTHER:
3951 // "+1" is same as ".+1"
Bram Moolenaarf240e182014-11-27 18:33:02 +01003952 lnum = curwin->w_cursor.lnum;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003953 break;
3954 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01003955 lnum = CURRENT_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003956 break;
3957 case ADDR_ARGUMENTS:
3958 lnum = curwin->w_arg_idx + 1;
3959 break;
3960 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003961 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003962 lnum = curbuf->b_fnum;
3963 break;
3964 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01003965 lnum = CURRENT_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003966 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01003967 case ADDR_TABS_RELATIVE:
3968 lnum = 1;
3969 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003970 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003971#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02003972 lnum = qf_get_cur_idx(eap);
3973#endif
3974 break;
3975 case ADDR_QUICKFIX_VALID:
3976#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003977 lnum = qf_get_cur_valid_idx(eap);
Bram Moolenaare906c502015-09-09 21:10:39 +02003978#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003979 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02003980 case ADDR_NONE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02003981 case ADDR_UNSIGNED:
3982 lnum = 0;
Bram Moolenaarb7316892019-05-01 18:08:42 +02003983 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003984 }
3985 }
3986
Bram Moolenaar071d4272004-06-13 20:20:40 +00003987 if (VIM_ISDIGIT(*cmd))
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003988 i = '+'; // "number" is same as "+number"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003989 else
3990 i = *cmd++;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003991 if (!VIM_ISDIGIT(*cmd)) // '+' is '+1', but '+0' is not '+1'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003992 n = 1;
3993 else
3994 n = getdigits(&cmd);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01003995
3996 if (addr_type == ADDR_TABS_RELATIVE)
3997 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003998 emsg(_(e_invrange));
Bram Moolenaar2f72c702017-01-29 14:48:10 +01003999 cmd = NULL;
4000 goto error;
4001 }
4002 else if (addr_type == ADDR_LOADED_BUFFERS
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004003 || addr_type == ADDR_BUFFERS)
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004004 lnum = compute_buffer_local_count(
4005 addr_type, lnum, (i == '-') ? -1 * n : n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004006 else
Bram Moolenaarded27822017-01-02 14:27:34 +01004007 {
4008#ifdef FEAT_FOLDING
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004009 // Relative line addressing, need to adjust for folded lines
4010 // now, but only do it after the first address.
Bram Moolenaarded27822017-01-02 14:27:34 +01004011 if (addr_type == ADDR_LINES && (i == '-' || i == '+')
4012 && address_count >= 2)
4013 (void)hasFolding(lnum, NULL, &lnum);
4014#endif
4015 if (i == '-')
4016 lnum -= n;
4017 else
4018 lnum += n;
4019 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004020 }
4021 } while (*cmd == '/' || *cmd == '?');
4022
4023error:
4024 *ptr = cmd;
4025 return lnum;
4026}
4027
4028/*
Bram Moolenaarc2af0af2020-08-23 21:06:02 +02004029 * Set eap->line1 and eap->line2 to the whole range.
4030 * Used for commands with the EX_DFLALL flag and no range given.
4031 */
4032 static void
4033address_default_all(exarg_T *eap)
4034{
4035 eap->line1 = 1;
4036 switch (eap->addr_type)
4037 {
4038 case ADDR_LINES:
4039 case ADDR_OTHER:
4040 eap->line2 = curbuf->b_ml.ml_line_count;
4041 break;
4042 case ADDR_LOADED_BUFFERS:
4043 {
4044 buf_T *buf = firstbuf;
4045
4046 while (buf->b_next != NULL && buf->b_ml.ml_mfp == NULL)
4047 buf = buf->b_next;
4048 eap->line1 = buf->b_fnum;
4049 buf = lastbuf;
4050 while (buf->b_prev != NULL && buf->b_ml.ml_mfp == NULL)
4051 buf = buf->b_prev;
4052 eap->line2 = buf->b_fnum;
4053 }
4054 break;
4055 case ADDR_BUFFERS:
4056 eap->line1 = firstbuf->b_fnum;
4057 eap->line2 = lastbuf->b_fnum;
4058 break;
4059 case ADDR_WINDOWS:
4060 eap->line2 = LAST_WIN_NR;
4061 break;
4062 case ADDR_TABS:
4063 eap->line2 = LAST_TAB_NR;
4064 break;
4065 case ADDR_TABS_RELATIVE:
4066 eap->line2 = 1;
4067 break;
4068 case ADDR_ARGUMENTS:
4069 if (ARGCOUNT == 0)
4070 eap->line1 = eap->line2 = 0;
4071 else
4072 eap->line2 = ARGCOUNT;
4073 break;
4074 case ADDR_QUICKFIX_VALID:
4075#ifdef FEAT_QUICKFIX
4076 eap->line2 = qf_get_valid_size(eap);
4077 if (eap->line2 == 0)
4078 eap->line2 = 1;
4079#endif
4080 break;
4081 case ADDR_NONE:
4082 case ADDR_UNSIGNED:
4083 case ADDR_QUICKFIX:
4084 iemsg(_("INTERNAL: Cannot use EX_DFLALL with ADDR_NONE, ADDR_UNSIGNED or ADDR_QUICKFIX"));
4085 break;
4086 }
4087}
4088
4089
4090/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00004091 * Get flags from an Ex command argument.
4092 */
4093 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004094get_flags(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00004095{
4096 while (vim_strchr((char_u *)"lp#", *eap->arg) != NULL)
4097 {
4098 if (*eap->arg == 'l')
4099 eap->flags |= EXFLAG_LIST;
4100 else if (*eap->arg == 'p')
4101 eap->flags |= EXFLAG_PRINT;
4102 else
4103 eap->flags |= EXFLAG_NR;
4104 eap->arg = skipwhite(eap->arg + 1);
4105 }
4106}
4107
4108/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004109 * Function called for command which is Not Implemented. NI!
4110 */
4111 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004112ex_ni(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004113{
4114 if (!eap->skip)
Bram Moolenaar8930caa2020-07-23 16:37:03 +02004115 eap->errmsg =
4116 _("E319: Sorry, the command is not available in this version");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004117}
4118
Bram Moolenaar7bb75552007-07-16 18:39:49 +00004119#ifdef HAVE_EX_SCRIPT_NI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004120/*
4121 * Function called for script command which is Not Implemented. NI!
4122 * Skips over ":perl <<EOF" constructs.
4123 */
4124 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004125ex_script_ni(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004126{
4127 if (!eap->skip)
4128 ex_ni(eap);
4129 else
4130 vim_free(script_get(eap, eap->arg));
4131}
4132#endif
4133
4134/*
4135 * Check range in Ex command for validity.
4136 * Return NULL when valid, error message when invalid.
4137 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004138 static char *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004139invalid_range(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004140{
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004141 buf_T *buf;
Bram Moolenaar25190db2019-05-04 15:05:28 +02004142
Bram Moolenaar071d4272004-06-13 20:20:40 +00004143 if ( eap->line1 < 0
4144 || eap->line2 < 0
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004145 || eap->line1 > eap->line2)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004146 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004147
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004148 if (eap->argt & EX_RANGE)
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004149 {
Bram Moolenaarb7316892019-05-01 18:08:42 +02004150 switch (eap->addr_type)
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004151 {
4152 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004153 if (eap->line2 > curbuf->b_ml.ml_line_count
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004154#ifdef FEAT_DIFF
4155 + (eap->cmdidx == CMD_diffget)
4156#endif
4157 )
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004158 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004159 break;
4160 case ADDR_ARGUMENTS:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004161 // add 1 if ARGCOUNT is 0
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004162 if (eap->line2 > ARGCOUNT + (!ARGCOUNT))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004163 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004164 break;
4165 case ADDR_BUFFERS:
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02004166 // Only a boundary check, not whether the buffers actually
4167 // exist.
4168 if (eap->line1 < 1 || eap->line2 > get_highest_fnum())
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004169 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004170 break;
4171 case ADDR_LOADED_BUFFERS:
4172 buf = firstbuf;
4173 while (buf->b_ml.ml_mfp == NULL)
4174 {
4175 if (buf->b_next == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004176 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004177 buf = buf->b_next;
4178 }
4179 if (eap->line1 < buf->b_fnum)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004180 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004181 buf = lastbuf;
4182 while (buf->b_ml.ml_mfp == NULL)
4183 {
4184 if (buf->b_prev == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004185 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004186 buf = buf->b_prev;
4187 }
4188 if (eap->line2 > buf->b_fnum)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004189 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004190 break;
4191 case ADDR_WINDOWS:
Bram Moolenaar8be63882015-01-14 11:25:05 +01004192 if (eap->line2 > LAST_WIN_NR)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004193 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004194 break;
4195 case ADDR_TABS:
4196 if (eap->line2 > LAST_TAB_NR)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004197 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004198 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004199 case ADDR_TABS_RELATIVE:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004200 case ADDR_OTHER:
4201 // Any range is OK.
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004202 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004203 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004204#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02004205 // No error for value that is too big, will use the last entry.
4206 if (eap->line2 <= 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004207 return _(e_invrange);
Bram Moolenaare906c502015-09-09 21:10:39 +02004208#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004209 break;
Bram Moolenaar25190db2019-05-04 15:05:28 +02004210 case ADDR_QUICKFIX_VALID:
4211#ifdef FEAT_QUICKFIX
4212 if ((eap->line2 != 1 && eap->line2 > qf_get_valid_size(eap))
4213 || eap->line2 < 0)
4214 return _(e_invrange);
4215#endif
4216 break;
4217 case ADDR_UNSIGNED:
4218 if (eap->line2 < 0)
4219 return _(e_invrange);
4220 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02004221 case ADDR_NONE:
4222 // Will give an error elsewhere.
4223 break;
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004224 }
4225 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004226 return NULL;
4227}
4228
4229/*
4230 * Correct the range for zero line number, if required.
4231 */
4232 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004233correct_range(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004234{
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004235 if (!(eap->argt & EX_ZEROR)) // zero in range not allowed
Bram Moolenaar071d4272004-06-13 20:20:40 +00004236 {
4237 if (eap->line1 == 0)
4238 eap->line1 = 1;
4239 if (eap->line2 == 0)
4240 eap->line2 = 1;
4241 }
4242}
4243
Bram Moolenaar748bf032005-02-02 23:04:36 +00004244#ifdef FEAT_QUICKFIX
Bram Moolenaar748bf032005-02-02 23:04:36 +00004245/*
4246 * For a ":vimgrep" or ":vimgrepadd" command return a pointer past the
4247 * pattern. Otherwise return eap->arg.
4248 */
4249 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004250skip_grep_pat(exarg_T *eap)
Bram Moolenaar748bf032005-02-02 23:04:36 +00004251{
4252 char_u *p = eap->arg;
4253
Bram Moolenaara37420f2006-02-04 22:37:47 +00004254 if (*p != NUL && (eap->cmdidx == CMD_vimgrep || eap->cmdidx == CMD_lvimgrep
4255 || eap->cmdidx == CMD_vimgrepadd
4256 || eap->cmdidx == CMD_lvimgrepadd
4257 || grep_internal(eap->cmdidx)))
Bram Moolenaar748bf032005-02-02 23:04:36 +00004258 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00004259 p = skip_vimgrep_pat(p, NULL, NULL);
Bram Moolenaar748bf032005-02-02 23:04:36 +00004260 if (p == NULL)
4261 p = eap->arg;
Bram Moolenaar748bf032005-02-02 23:04:36 +00004262 }
4263 return p;
4264}
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004265
4266/*
4267 * For the ":make" and ":grep" commands insert the 'makeprg'/'grepprg' option
4268 * in the command line, so that things like % get expanded.
4269 */
4270 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004271replace_makeprg(exarg_T *eap, char_u *p, char_u **cmdlinep)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004272{
4273 char_u *new_cmdline;
4274 char_u *program;
4275 char_u *pos;
4276 char_u *ptr;
4277 int len;
4278 int i;
4279
4280 /*
4281 * Don't do it when ":vimgrep" is used for ":grep".
4282 */
Bram Moolenaara37420f2006-02-04 22:37:47 +00004283 if ((eap->cmdidx == CMD_make || eap->cmdidx == CMD_lmake
4284 || eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4285 || eap->cmdidx == CMD_grepadd
4286 || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004287 && !grep_internal(eap->cmdidx))
4288 {
Bram Moolenaara37420f2006-02-04 22:37:47 +00004289 if (eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4290 || eap->cmdidx == CMD_grepadd || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004291 {
4292 if (*curbuf->b_p_gp == NUL)
4293 program = p_gp;
4294 else
4295 program = curbuf->b_p_gp;
4296 }
4297 else
4298 {
4299 if (*curbuf->b_p_mp == NUL)
4300 program = p_mp;
4301 else
4302 program = curbuf->b_p_mp;
4303 }
4304
4305 p = skipwhite(p);
4306
4307 if ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4308 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004309 // replace $* by given arguments
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004310 i = 1;
4311 while ((pos = (char_u *)strstr((char *)pos + 2, "$*")) != NULL)
4312 ++i;
4313 len = (int)STRLEN(p);
Bram Moolenaar51e14382019-05-25 20:21:28 +02004314 new_cmdline = alloc(STRLEN(program) + i * (len - 2) + 1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004315 if (new_cmdline == NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004316 return NULL; // out of memory
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004317 ptr = new_cmdline;
4318 while ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4319 {
4320 i = (int)(pos - program);
4321 STRNCPY(ptr, program, i);
4322 STRCPY(ptr += i, p);
4323 ptr += len;
4324 program = pos + 2;
4325 }
4326 STRCPY(ptr, program);
4327 }
4328 else
4329 {
Bram Moolenaar51e14382019-05-25 20:21:28 +02004330 new_cmdline = alloc(STRLEN(program) + STRLEN(p) + 2);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004331 if (new_cmdline == NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004332 return NULL; // out of memory
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004333 STRCPY(new_cmdline, program);
4334 STRCAT(new_cmdline, " ");
4335 STRCAT(new_cmdline, p);
4336 }
4337 msg_make(p);
4338
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004339 // 'eap->cmd' is not set here, because it is not used at CMD_make
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004340 vim_free(*cmdlinep);
4341 *cmdlinep = new_cmdline;
4342 p = new_cmdline;
4343 }
4344 return p;
4345}
Bram Moolenaar748bf032005-02-02 23:04:36 +00004346#endif
4347
Bram Moolenaar071d4272004-06-13 20:20:40 +00004348/*
4349 * Expand file name in Ex command argument.
Bram Moolenaar0abb4272019-06-15 16:06:00 +02004350 * When an error is detected, "errormsgp" is set to a non-NULL pointer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004351 * Return FAIL for failure, OK otherwise.
4352 */
4353 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004354expand_filename(
4355 exarg_T *eap,
4356 char_u **cmdlinep,
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004357 char **errormsgp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004358{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004359 int has_wildcards; // need to expand wildcards
Bram Moolenaar071d4272004-06-13 20:20:40 +00004360 char_u *repl;
4361 int srclen;
4362 char_u *p;
4363 int n;
Bram Moolenaar63b92542007-03-27 14:57:09 +00004364 int escaped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004365
Bram Moolenaar748bf032005-02-02 23:04:36 +00004366#ifdef FEAT_QUICKFIX
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004367 // Skip a regexp pattern for ":vimgrep[add] pat file..."
Bram Moolenaar748bf032005-02-02 23:04:36 +00004368 p = skip_grep_pat(eap);
4369#else
4370 p = eap->arg;
4371#endif
4372
Bram Moolenaar071d4272004-06-13 20:20:40 +00004373 /*
4374 * Decide to expand wildcards *before* replacing '%', '#', etc. If
4375 * the file name contains a wildcard it should not cause expanding.
4376 * (it will be expanded anyway if there is a wildcard before replacing).
4377 */
Bram Moolenaar748bf032005-02-02 23:04:36 +00004378 has_wildcards = mch_has_wildcard(p);
4379 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004380 {
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004381#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004382 // Skip over `=expr`, wildcards in it are not expanded.
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004383 if (p[0] == '`' && p[1] == '=')
4384 {
4385 p += 2;
4386 (void)skip_expr(&p);
4387 if (*p == '`')
4388 ++p;
4389 continue;
4390 }
4391#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004392 /*
4393 * Quick check if this cannot be the start of a special string.
4394 * Also removes backslash before '%', '#' and '<'.
4395 */
4396 if (vim_strchr((char_u *)"%#<", *p) == NULL)
4397 {
4398 ++p;
4399 continue;
4400 }
4401
4402 /*
4403 * Try to find a match at this position.
4404 */
Bram Moolenaar63b92542007-03-27 14:57:09 +00004405 repl = eval_vars(p, eap->arg, &srclen, &(eap->do_ecmd_lnum),
4406 errormsgp, &escaped);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004407 if (*errormsgp != NULL) // error detected
Bram Moolenaar071d4272004-06-13 20:20:40 +00004408 return FAIL;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004409 if (repl == NULL) // no match found
Bram Moolenaar071d4272004-06-13 20:20:40 +00004410 {
4411 p += srclen;
4412 continue;
4413 }
4414
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004415 // Wildcards won't be expanded below, the replacement is taken
4416 // literally. But do expand "~/file", "~user/file" and "$HOME/file".
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00004417 if (vim_strchr(repl, '$') != NULL || vim_strchr(repl, '~') != NULL)
4418 {
4419 char_u *l = repl;
4420
4421 repl = expand_env_save(repl);
4422 vim_free(l);
4423 }
4424
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004425 // Need to escape white space et al. with a backslash.
4426 // Don't do this for:
4427 // - replacement that already has been escaped: "##"
4428 // - shell commands (may have to use quotes instead).
4429 // - non-unix systems when there is a single argument (spaces don't
4430 // separate arguments then).
Bram Moolenaar071d4272004-06-13 20:20:40 +00004431 if (!eap->usefilter
Bram Moolenaar63b92542007-03-27 14:57:09 +00004432 && !escaped
Bram Moolenaar071d4272004-06-13 20:20:40 +00004433 && eap->cmdidx != CMD_bang
Bram Moolenaar071d4272004-06-13 20:20:40 +00004434 && eap->cmdidx != CMD_grep
4435 && eap->cmdidx != CMD_grepadd
Bram Moolenaarbf15b8d2017-06-04 20:43:48 +02004436 && eap->cmdidx != CMD_hardcopy
Bram Moolenaar67883b42017-07-27 22:57:00 +02004437 && eap->cmdidx != CMD_lgrep
4438 && eap->cmdidx != CMD_lgrepadd
4439 && eap->cmdidx != CMD_lmake
4440 && eap->cmdidx != CMD_make
4441 && eap->cmdidx != CMD_terminal
Bram Moolenaar071d4272004-06-13 20:20:40 +00004442#ifndef UNIX
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004443 && !(eap->argt & EX_NOSPC)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004444#endif
4445 )
4446 {
4447 char_u *l;
4448#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004449 // Don't escape a backslash here, because rem_backslash() doesn't
4450 // remove it later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004451 static char_u *nobslash = (char_u *)" \t\"|";
4452# define ESCAPE_CHARS nobslash
4453#else
4454# define ESCAPE_CHARS escape_chars
4455#endif
4456
4457 for (l = repl; *l; ++l)
4458 if (vim_strchr(ESCAPE_CHARS, *l) != NULL)
4459 {
4460 l = vim_strsave_escaped(repl, ESCAPE_CHARS);
4461 if (l != NULL)
4462 {
4463 vim_free(repl);
4464 repl = l;
4465 }
4466 break;
4467 }
4468 }
4469
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004470 // For a shell command a '!' must be escaped.
Bram Moolenaar67883b42017-07-27 22:57:00 +02004471 if ((eap->usefilter || eap->cmdidx == CMD_bang
4472 || eap->cmdidx == CMD_terminal)
Bram Moolenaar31b7d382014-04-01 18:54:48 +02004473 && vim_strpbrk(repl, (char_u *)"!") != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004474 {
4475 char_u *l;
4476
Bram Moolenaar31b7d382014-04-01 18:54:48 +02004477 l = vim_strsave_escaped(repl, (char_u *)"!");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004478 if (l != NULL)
4479 {
4480 vim_free(repl);
4481 repl = l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004482 }
4483 }
4484
4485 p = repl_cmdline(eap, p, srclen, repl, cmdlinep);
4486 vim_free(repl);
4487 if (p == NULL)
4488 return FAIL;
4489 }
4490
4491 /*
4492 * One file argument: Expand wildcards.
4493 * Don't do this with ":r !command" or ":w !command".
4494 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004495 if ((eap->argt & EX_NOSPC) && !eap->usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004496 {
4497 /*
4498 * May do this twice:
4499 * 1. Replace environment variables.
4500 * 2. Replace any other wildcards, remove backslashes.
4501 */
4502 for (n = 1; n <= 2; ++n)
4503 {
4504 if (n == 2)
4505 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004506 /*
4507 * Halve the number of backslashes (this is Vi compatible).
4508 * For Unix and OS/2, when wildcards are expanded, this is
4509 * done by ExpandOne() below.
4510 */
Bram Moolenaare7fedb62015-12-31 19:07:19 +01004511#if defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004512 if (!has_wildcards)
4513#endif
4514 backslash_halve(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004515 }
4516
4517 if (has_wildcards)
4518 {
4519 if (n == 1)
4520 {
4521 /*
4522 * First loop: May expand environment variables. This
4523 * can be done much faster with expand_env() than with
4524 * something else (e.g., calling a shell).
4525 * After expanding environment variables, check again
4526 * if there are still wildcards present.
4527 */
4528 if (vim_strchr(eap->arg, '$') != NULL
4529 || vim_strchr(eap->arg, '~') != NULL)
4530 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +00004531 expand_env_esc(eap->arg, NameBuff, MAXPATHL,
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00004532 TRUE, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004533 has_wildcards = mch_has_wildcard(NameBuff);
4534 p = NameBuff;
4535 }
4536 else
4537 p = NULL;
4538 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004539 else // n == 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00004540 {
4541 expand_T xpc;
Bram Moolenaarb40c2572019-10-19 21:01:05 +02004542 int options = WILD_LIST_NOTFOUND
4543 | WILD_NOERROR | WILD_ADD_SLASH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004544
4545 ExpandInit(&xpc);
4546 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar94950a92010-12-02 16:01:29 +01004547 if (p_wic)
4548 options += WILD_ICASE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004549 p = ExpandOne(&xpc, eap->arg, NULL,
Bram Moolenaar94950a92010-12-02 16:01:29 +01004550 options, WILD_EXPAND_FREE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004551 if (p == NULL)
4552 return FAIL;
4553 }
4554 if (p != NULL)
4555 {
4556 (void)repl_cmdline(eap, eap->arg, (int)STRLEN(eap->arg),
4557 p, cmdlinep);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004558 if (n == 2) // p came from ExpandOne()
Bram Moolenaar071d4272004-06-13 20:20:40 +00004559 vim_free(p);
4560 }
4561 }
4562 }
4563 }
4564 return OK;
4565}
4566
4567/*
4568 * Replace part of the command line, keeping eap->cmd, eap->arg and
4569 * eap->nextcmd correct.
4570 * "src" points to the part that is to be replaced, of length "srclen".
4571 * "repl" is the replacement string.
4572 * Returns a pointer to the character after the replaced string.
4573 * Returns NULL for failure.
4574 */
4575 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004576repl_cmdline(
4577 exarg_T *eap,
4578 char_u *src,
4579 int srclen,
4580 char_u *repl,
4581 char_u **cmdlinep)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004582{
4583 int len;
4584 int i;
4585 char_u *new_cmdline;
4586
4587 /*
4588 * The new command line is build in new_cmdline[].
4589 * First allocate it.
4590 * Careful: a "+cmd" argument may have been NUL terminated.
4591 */
4592 len = (int)STRLEN(repl);
4593 i = (int)(src - *cmdlinep) + (int)STRLEN(src + srclen) + len + 3;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004594 if (eap->nextcmd != NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004595 i += (int)STRLEN(eap->nextcmd);// add space for next command
Bram Moolenaar964b3742019-05-24 18:54:09 +02004596 if ((new_cmdline = alloc(i)) == NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004597 return NULL; // out of memory!
Bram Moolenaar071d4272004-06-13 20:20:40 +00004598
4599 /*
4600 * Copy the stuff before the expanded part.
4601 * Copy the expanded stuff.
4602 * Copy what came after the expanded part.
4603 * Copy the next commands, if there are any.
4604 */
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004605 i = (int)(src - *cmdlinep); // length of part before match
Bram Moolenaar071d4272004-06-13 20:20:40 +00004606 mch_memmove(new_cmdline, *cmdlinep, (size_t)i);
Bram Moolenaara3ffd9c2005-07-21 21:03:15 +00004607
Bram Moolenaar071d4272004-06-13 20:20:40 +00004608 mch_memmove(new_cmdline + i, repl, (size_t)len);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004609 i += len; // remember the end of the string
Bram Moolenaar071d4272004-06-13 20:20:40 +00004610 STRCPY(new_cmdline + i, src + srclen);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004611 src = new_cmdline + i; // remember where to continue
Bram Moolenaar071d4272004-06-13 20:20:40 +00004612
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004613 if (eap->nextcmd != NULL) // append next command
Bram Moolenaar071d4272004-06-13 20:20:40 +00004614 {
4615 i = (int)STRLEN(new_cmdline) + 1;
4616 STRCPY(new_cmdline + i, eap->nextcmd);
4617 eap->nextcmd = new_cmdline + i;
4618 }
4619 eap->cmd = new_cmdline + (eap->cmd - *cmdlinep);
4620 eap->arg = new_cmdline + (eap->arg - *cmdlinep);
4621 if (eap->do_ecmd_cmd != NULL && eap->do_ecmd_cmd != dollar_command)
4622 eap->do_ecmd_cmd = new_cmdline + (eap->do_ecmd_cmd - *cmdlinep);
4623 vim_free(*cmdlinep);
4624 *cmdlinep = new_cmdline;
4625
4626 return src;
4627}
4628
4629/*
4630 * Check for '|' to separate commands and '"' to start comments.
4631 */
4632 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004633separate_nextcmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004634{
4635 char_u *p;
4636
Bram Moolenaar86b68352004-12-27 21:59:20 +00004637#ifdef FEAT_QUICKFIX
Bram Moolenaar748bf032005-02-02 23:04:36 +00004638 p = skip_grep_pat(eap);
4639#else
4640 p = eap->arg;
Bram Moolenaar86b68352004-12-27 21:59:20 +00004641#endif
4642
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004643 for ( ; *p; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004644 {
4645 if (*p == Ctrl_V)
4646 {
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004647 if (eap->argt & (EX_CTRLV | EX_XFILE))
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004648 ++p; // skip CTRL-V and next char
Bram Moolenaar071d4272004-06-13 20:20:40 +00004649 else
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004650 // remove CTRL-V and skip next char
Bram Moolenaara7241f52008-06-24 20:39:31 +00004651 STRMOVE(p, p + 1);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004652 if (*p == NUL) // stop at NUL after CTRL-V
Bram Moolenaar071d4272004-06-13 20:20:40 +00004653 break;
4654 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004655
4656#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004657 // Skip over `=expr` when wildcards are expanded.
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004658 else if (p[0] == '`' && p[1] == '=' && (eap->argt & EX_XFILE))
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004659 {
4660 p += 2;
4661 (void)skip_expr(&p);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004662 }
4663#endif
4664
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004665 // Check for '"': start of comment or '|': next command
4666 // :@" and :*" do not start a comment!
4667 // :redir @" doesn't either.
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02004668 else if ((*p == '"'
Bram Moolenaara26b9702020-04-18 19:53:28 +02004669#ifdef FEAT_EVAL
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02004670 && !in_vim9script()
4671#endif
4672 && !(eap->argt & EX_NOTRLCOM)
4673 && ((eap->cmdidx != CMD_at && eap->cmdidx != CMD_star)
4674 || p != eap->arg)
4675 && (eap->cmdidx != CMD_redir
4676 || p != eap->arg + 1 || p[-1] != '@'))
4677#ifdef FEAT_EVAL
4678 || (*p == '#'
4679 && in_vim9script()
Bram Moolenaarb8a92962020-08-20 18:02:47 +02004680 && !(eap->argt & EX_NOTRLCOM)
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02004681 && p[1] != '{'
4682 && p > eap->cmd && VIM_ISWHITE(p[-1]))
Bram Moolenaara26b9702020-04-18 19:53:28 +02004683#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004684 || *p == '|' || *p == '\n')
4685 {
4686 /*
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004687 * We remove the '\' before the '|', unless EX_CTRLV is used
Bram Moolenaar071d4272004-06-13 20:20:40 +00004688 * AND 'b' is present in 'cpoptions'.
4689 */
4690 if ((vim_strchr(p_cpo, CPO_BAR) == NULL
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004691 || !(eap->argt & EX_CTRLV)) && *(p - 1) == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004692 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004693 STRMOVE(p - 1, p); // remove the '\'
Bram Moolenaar071d4272004-06-13 20:20:40 +00004694 --p;
4695 }
4696 else
4697 {
4698 eap->nextcmd = check_nextcmd(p);
4699 *p = NUL;
4700 break;
4701 }
4702 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004703 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00004704
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004705 if (!(eap->argt & EX_NOTRLCOM)) // remove trailing spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00004706 del_trailing_spaces(eap->arg);
4707}
4708
4709/*
4710 * get + command from ex argument
4711 */
4712 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004713getargcmd(char_u **argp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004714{
4715 char_u *arg = *argp;
4716 char_u *command = NULL;
4717
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004718 if (*arg == '+') // +[command]
Bram Moolenaar071d4272004-06-13 20:20:40 +00004719 {
4720 ++arg;
Bram Moolenaar3e451592014-04-02 14:22:05 +02004721 if (vim_isspace(*arg) || *arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004722 command = dollar_command;
4723 else
4724 {
4725 command = arg;
4726 arg = skip_cmd_arg(command, TRUE);
4727 if (*arg != NUL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004728 *arg++ = NUL; // terminate command with NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004729 }
4730
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004731 arg = skipwhite(arg); // skip over spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00004732 *argp = arg;
4733 }
4734 return command;
4735}
4736
4737/*
4738 * Find end of "+command" argument. Skip over "\ " and "\\".
4739 */
Bram Moolenaard0190392019-08-23 21:17:35 +02004740 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004741skip_cmd_arg(
4742 char_u *p,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004743 int rembs) // TRUE to halve the number of backslashes
Bram Moolenaar071d4272004-06-13 20:20:40 +00004744{
4745 while (*p && !vim_isspace(*p))
4746 {
4747 if (*p == '\\' && p[1] != NUL)
4748 {
4749 if (rembs)
Bram Moolenaara7241f52008-06-24 20:39:31 +00004750 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004751 else
4752 ++p;
4753 }
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004754 MB_PTR_ADV(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004755 }
4756 return p;
4757}
4758
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004759 int
4760get_bad_opt(char_u *p, exarg_T *eap)
4761{
4762 if (STRICMP(p, "keep") == 0)
4763 eap->bad_char = BAD_KEEP;
4764 else if (STRICMP(p, "drop") == 0)
4765 eap->bad_char = BAD_DROP;
4766 else if (MB_BYTE2LEN(*p) == 1 && p[1] == NUL)
4767 eap->bad_char = *p;
Bram Moolenaar75808492018-06-12 12:39:41 +02004768 else
4769 return FAIL;
4770 return OK;
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004771}
4772
Bram Moolenaar071d4272004-06-13 20:20:40 +00004773/*
4774 * Get "++opt=arg" argument.
4775 * Return FAIL or OK.
4776 */
4777 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004778getargopt(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004779{
4780 char_u *arg = eap->arg + 2;
4781 int *pp = NULL;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02004782 int bad_char_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004783 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004784
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004785 // ":edit ++[no]bin[ary] file"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004786 if (STRNCMP(arg, "bin", 3) == 0 || STRNCMP(arg, "nobin", 5) == 0)
4787 {
4788 if (*arg == 'n')
4789 {
4790 arg += 2;
4791 eap->force_bin = FORCE_NOBIN;
4792 }
4793 else
4794 eap->force_bin = FORCE_BIN;
4795 if (!checkforcmd(&arg, "binary", 3))
4796 return FAIL;
4797 eap->arg = skipwhite(arg);
4798 return OK;
4799 }
4800
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004801 // ":read ++edit file"
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004802 if (STRNCMP(arg, "edit", 4) == 0)
4803 {
4804 eap->read_edit = TRUE;
4805 eap->arg = skipwhite(arg + 4);
4806 return OK;
4807 }
4808
Bram Moolenaar071d4272004-06-13 20:20:40 +00004809 if (STRNCMP(arg, "ff", 2) == 0)
4810 {
4811 arg += 2;
4812 pp = &eap->force_ff;
4813 }
4814 else if (STRNCMP(arg, "fileformat", 10) == 0)
4815 {
4816 arg += 10;
4817 pp = &eap->force_ff;
4818 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004819 else if (STRNCMP(arg, "enc", 3) == 0)
4820 {
Bram Moolenaarb38e9ab2011-12-14 14:49:45 +01004821 if (STRNCMP(arg, "encoding", 8) == 0)
4822 arg += 8;
4823 else
4824 arg += 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004825 pp = &eap->force_enc;
4826 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004827 else if (STRNCMP(arg, "bad", 3) == 0)
4828 {
4829 arg += 3;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02004830 pp = &bad_char_idx;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004831 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004832
4833 if (pp == NULL || *arg != '=')
4834 return FAIL;
4835
4836 ++arg;
4837 *pp = (int)(arg - eap->cmd);
4838 arg = skip_cmd_arg(arg, FALSE);
4839 eap->arg = skipwhite(arg);
4840 *arg = NUL;
4841
Bram Moolenaar071d4272004-06-13 20:20:40 +00004842 if (pp == &eap->force_ff)
4843 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004844 if (check_ff_value(eap->cmd + eap->force_ff) == FAIL)
4845 return FAIL;
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004846 eap->force_ff = eap->cmd[eap->force_ff];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004847 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004848 else if (pp == &eap->force_enc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004849 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004850 // Make 'fileencoding' lower case.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004851 for (p = eap->cmd + eap->force_enc; *p != NUL; ++p)
4852 *p = TOLOWER_ASC(*p);
4853 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004854 else
4855 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004856 // Check ++bad= argument. Must be a single-byte character, "keep" or
4857 // "drop".
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004858 if (get_bad_opt(eap->cmd + bad_char_idx, eap) == FAIL)
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004859 return FAIL;
4860 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004861
4862 return OK;
4863}
4864
Bram Moolenaar071d4272004-06-13 20:20:40 +00004865 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004866ex_autocmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004867{
4868 /*
Bram Moolenaar26d4b892018-07-04 22:26:28 +02004869 * Disallow autocommands from .exrc and .vimrc in current
Bram Moolenaar071d4272004-06-13 20:20:40 +00004870 * directory for security reasons.
4871 */
4872 if (secure)
4873 {
4874 secure = 2;
Bram Moolenaar8930caa2020-07-23 16:37:03 +02004875 eap->errmsg = _(e_curdir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004876 }
4877 else if (eap->cmdidx == CMD_autocmd)
4878 do_autocmd(eap->arg, eap->forceit);
4879 else
4880 do_augroup(eap->arg, eap->forceit);
4881}
4882
4883/*
4884 * ":doautocmd": Apply the automatic commands to the current buffer.
4885 */
4886 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004887ex_doautocmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004888{
Bram Moolenaar60542ac2012-02-12 20:14:01 +01004889 char_u *arg = eap->arg;
4890 int call_do_modelines = check_nomodeline(&arg);
Bram Moolenaar1610d052016-06-09 22:53:01 +02004891 int did_aucmd;
Bram Moolenaar60542ac2012-02-12 20:14:01 +01004892
Bram Moolenaar1610d052016-06-09 22:53:01 +02004893 (void)do_doautocmd(arg, TRUE, &did_aucmd);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004894 // Only when there is no <nomodeline>.
Bram Moolenaar1610d052016-06-09 22:53:01 +02004895 if (call_do_modelines && did_aucmd)
Bram Moolenaar60542ac2012-02-12 20:14:01 +01004896 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004897}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004898
Bram Moolenaar071d4272004-06-13 20:20:40 +00004899/*
4900 * :[N]bunload[!] [N] [bufname] unload buffer
4901 * :[N]bdelete[!] [N] [bufname] delete buffer from buffer list
4902 * :[N]bwipeout[!] [N] [bufname] delete buffer really
4903 */
4904 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004905ex_bunload(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004906{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01004907 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02004908 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004909 eap->errmsg = do_bufdel(
4910 eap->cmdidx == CMD_bdelete ? DOBUF_DEL
4911 : eap->cmdidx == CMD_bwipeout ? DOBUF_WIPE
4912 : DOBUF_UNLOAD, eap->arg,
4913 eap->addr_count, (int)eap->line1, (int)eap->line2, eap->forceit);
4914}
4915
4916/*
4917 * :[N]buffer [N] to buffer N
4918 * :[N]sbuffer [N] to buffer N
4919 */
4920 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004921ex_buffer(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004922{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01004923 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02004924 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004925 if (*eap->arg)
Bram Moolenaar8930caa2020-07-23 16:37:03 +02004926 eap->errmsg = ex_errmsg(e_trailing_arg, eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004927 else
4928 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004929 if (eap->addr_count == 0) // default is current buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00004930 goto_buffer(eap, DOBUF_CURRENT, FORWARD, 0);
4931 else
4932 goto_buffer(eap, DOBUF_FIRST, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02004933 if (eap->do_ecmd_cmd != NULL)
4934 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004935 }
4936}
4937
4938/*
4939 * :[N]bmodified [N] to next mod. buffer
4940 * :[N]sbmodified [N] to next mod. buffer
4941 */
4942 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004943ex_bmodified(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004944{
4945 goto_buffer(eap, DOBUF_MOD, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02004946 if (eap->do_ecmd_cmd != NULL)
4947 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004948}
4949
4950/*
4951 * :[N]bnext [N] to next buffer
4952 * :[N]sbnext [N] split and to next buffer
4953 */
4954 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004955ex_bnext(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004956{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01004957 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar3029bcc2020-01-18 15:06:19 +01004958 return;
4959
Bram Moolenaar071d4272004-06-13 20:20:40 +00004960 goto_buffer(eap, DOBUF_CURRENT, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02004961 if (eap->do_ecmd_cmd != NULL)
4962 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004963}
4964
4965/*
4966 * :[N]bNext [N] to previous buffer
4967 * :[N]bprevious [N] to previous buffer
4968 * :[N]sbNext [N] split and to previous buffer
4969 * :[N]sbprevious [N] split and to previous buffer
4970 */
4971 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004972ex_bprevious(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004973{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01004974 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar3029bcc2020-01-18 15:06:19 +01004975 return;
4976
Bram Moolenaar071d4272004-06-13 20:20:40 +00004977 goto_buffer(eap, DOBUF_CURRENT, BACKWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02004978 if (eap->do_ecmd_cmd != NULL)
4979 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004980}
4981
4982/*
4983 * :brewind to first buffer
4984 * :bfirst to first buffer
4985 * :sbrewind split and to first buffer
4986 * :sbfirst split and to first buffer
4987 */
4988 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004989ex_brewind(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004990{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01004991 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar3029bcc2020-01-18 15:06:19 +01004992 return;
4993
Bram Moolenaar071d4272004-06-13 20:20:40 +00004994 goto_buffer(eap, DOBUF_FIRST, FORWARD, 0);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02004995 if (eap->do_ecmd_cmd != NULL)
4996 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004997}
4998
4999/*
5000 * :blast to last buffer
5001 * :sblast split and to last buffer
5002 */
5003 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005004ex_blast(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005005{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01005006 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar3029bcc2020-01-18 15:06:19 +01005007 return;
5008
Bram Moolenaar071d4272004-06-13 20:20:40 +00005009 goto_buffer(eap, DOBUF_LAST, BACKWARD, 0);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005010 if (eap->do_ecmd_cmd != NULL)
5011 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005012}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005013
Bram Moolenaar7a092242020-04-16 22:10:49 +02005014/*
5015 * Check if "c" ends an Ex command.
Bram Moolenaara494f562020-04-18 17:45:38 +02005016 * In Vim9 script does not check for white space before # or #{.
Bram Moolenaar7a092242020-04-16 22:10:49 +02005017 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005018 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005019ends_excmd(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005020{
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005021 int comment_char = '"';
5022
Bram Moolenaarfbda69b2020-04-13 15:06:53 +02005023#ifdef FEAT_EVAL
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005024 if (in_vim9script())
5025 comment_char = '#';
Bram Moolenaar7a092242020-04-16 22:10:49 +02005026#endif
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005027 return (c == NUL || c == '|' || c == comment_char || c == '\n');
Bram Moolenaar7a092242020-04-16 22:10:49 +02005028}
5029
5030/*
5031 * Like ends_excmd() but checks that a # in Vim9 script either has "cmd" equal
5032 * to "cmd_start" or has a white space character before it.
5033 */
5034 int
Bram Moolenaar21cfe502020-04-16 23:01:50 +02005035ends_excmd2(char_u *cmd_start UNUSED, char_u *cmd)
Bram Moolenaar7a092242020-04-16 22:10:49 +02005036{
5037 int c = *cmd;
5038
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005039 if (c == NUL || c == '|' || c == '\n')
5040 return TRUE;
Bram Moolenaar7a092242020-04-16 22:10:49 +02005041#ifdef FEAT_EVAL
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005042 if (in_vim9script())
5043 return c == '#' && cmd[1] != '{'
5044 && (cmd == cmd_start || VIM_ISWHITE(cmd[-1]));
Bram Moolenaarfbda69b2020-04-13 15:06:53 +02005045#endif
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005046 return c == '"';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005047}
5048
5049#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA) || defined(FEAT_EVAL) \
5050 || defined(PROTO)
5051/*
5052 * Return the next command, after the first '|' or '\n'.
5053 * Return NULL if not found.
5054 */
5055 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005056find_nextcmd(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005057{
5058 while (*p != '|' && *p != '\n')
5059 {
5060 if (*p == NUL)
5061 return NULL;
5062 ++p;
5063 }
5064 return (p + 1);
5065}
5066#endif
5067
5068/*
Bram Moolenaar2256c992016-11-15 21:17:07 +01005069 * Check if *p is a separator between Ex commands, skipping over white space.
5070 * Return NULL if it isn't, the following character if it is.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005071 */
5072 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005073check_nextcmd(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005074{
Bram Moolenaar2256c992016-11-15 21:17:07 +01005075 char_u *s = skipwhite(p);
5076
5077 if (*s == '|' || *s == '\n')
5078 return (s + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005079 else
5080 return NULL;
5081}
5082
5083/*
5084 * - if there are more files to edit
5085 * - and this is the last window
5086 * - and forceit not used
5087 * - and not repeated twice on a row
5088 * return FAIL and give error message if 'message' TRUE
5089 * return OK otherwise
5090 */
5091 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005092check_more(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005093 int message, // when FALSE check only, no messages
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005094 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005095{
5096 int n = ARGCOUNT - curwin->w_arg_idx - 1;
5097
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005098 if (!forceit && only_one_window()
Bram Moolenaar7b221172020-08-17 19:34:10 +02005099 && ARGCOUNT > 1 && !arg_had_last && n > 0 && quitmore == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005100 {
5101 if (message)
5102 {
5103#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
5104 if ((p_confirm || cmdmod.confirm) && curbuf->b_fname != NULL)
5105 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02005106 char_u buff[DIALOG_MSG_SIZE];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005107
Bram Moolenaarda6e8912018-08-21 15:12:14 +02005108 vim_snprintf((char *)buff, DIALOG_MSG_SIZE,
5109 NGETTEXT("%d more file to edit. Quit anyway?",
5110 "%d more files to edit. Quit anyway?", n), n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005111 if (vim_dialog_yesno(VIM_QUESTION, NULL, buff, 1) == VIM_YES)
5112 return OK;
5113 return FAIL;
5114 }
5115#endif
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01005116 semsg(NGETTEXT("E173: %d more file to edit",
5117 "E173: %d more files to edit", n), n);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005118 quitmore = 2; // next try to quit is allowed
Bram Moolenaar071d4272004-06-13 20:20:40 +00005119 }
5120 return FAIL;
5121 }
5122 return OK;
5123}
5124
Bram Moolenaar071d4272004-06-13 20:20:40 +00005125/*
5126 * Function given to ExpandGeneric() to obtain the list of command names.
5127 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005128 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005129get_command_name(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005130{
5131 if (idx >= (int)CMD_SIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005132 return get_user_command_name(idx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005133 return cmdnames[idx].cmd_name;
5134}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005135
Bram Moolenaar071d4272004-06-13 20:20:40 +00005136 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005137ex_colorscheme(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005138{
Bram Moolenaare6850792010-05-14 15:28:44 +02005139 if (*eap->arg == NUL)
5140 {
5141#ifdef FEAT_EVAL
5142 char_u *expr = vim_strsave((char_u *)"g:colors_name");
5143 char_u *p = NULL;
5144
5145 if (expr != NULL)
5146 {
5147 ++emsg_off;
Bram Moolenaarb171fb12020-06-24 20:34:03 +02005148 p = eval_to_string(expr, FALSE);
Bram Moolenaare6850792010-05-14 15:28:44 +02005149 --emsg_off;
5150 vim_free(expr);
5151 }
5152 if (p != NULL)
5153 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01005154 msg((char *)p);
Bram Moolenaare6850792010-05-14 15:28:44 +02005155 vim_free(p);
5156 }
5157 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01005158 msg("default");
Bram Moolenaare6850792010-05-14 15:28:44 +02005159#else
Bram Moolenaar32526b32019-01-19 17:43:09 +01005160 msg(_("unknown"));
Bram Moolenaare6850792010-05-14 15:28:44 +02005161#endif
5162 }
5163 else if (load_colors(eap->arg) == FAIL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005164 semsg(_("E185: Cannot find color scheme '%s'"), eap->arg);
Bram Moolenaarf58d81a2019-01-28 20:19:05 +01005165
5166#ifdef FEAT_VTP
5167 else if (has_vtp_working())
5168 {
5169 // background color change requires clear + redraw
5170 update_screen(CLEAR);
5171 redrawcmd();
5172 }
5173#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005174}
5175
5176 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005177ex_highlight(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005178{
5179 if (*eap->arg == NUL && eap->cmd[2] == '!')
Bram Moolenaar32526b32019-01-19 17:43:09 +01005180 msg(_("Greetings, Vim user!"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005181 do_highlight(eap->arg, eap->forceit, FALSE);
5182}
5183
5184
5185/*
5186 * Call this function if we thought we were going to exit, but we won't
5187 * (because of an error). May need to restore the terminal mode.
5188 */
5189 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005190not_exiting(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005191{
5192 exiting = FALSE;
5193 settmode(TMODE_RAW);
5194}
5195
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005196 static int
5197before_quit_autocmds(win_T *wp, int quit_all, int forceit)
5198{
5199 apply_autocmds(EVENT_QUITPRE, NULL, NULL, FALSE, wp->w_buffer);
5200
Bram Moolenaar34ba06b2019-10-20 22:27:10 +02005201 // Bail out when autocommands closed the window.
5202 // Refuse to quit when the buffer in the last window is being closed (can
5203 // only happen in autocommands).
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005204 if (!win_valid(wp)
5205 || curbuf_locked()
5206 || (wp->w_buffer->b_nwindows == 1 && wp->w_buffer->b_locked > 0))
5207 return TRUE;
5208
5209 if (quit_all || (check_more(FALSE, forceit) == OK && only_one_window()))
5210 {
5211 apply_autocmds(EVENT_EXITPRE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar34ba06b2019-10-20 22:27:10 +02005212 // Refuse to quit when locked or when the window was closed or the
5213 // buffer in the last window is being closed (can only happen in
5214 // autocommands).
5215 if (!win_valid(wp) || curbuf_locked()
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005216 || (curbuf->b_nwindows == 1 && curbuf->b_locked > 0))
5217 return TRUE;
5218 }
5219
5220 return FALSE;
5221}
5222
Bram Moolenaar071d4272004-06-13 20:20:40 +00005223/*
Bram Moolenaarf240e182014-11-27 18:33:02 +01005224 * ":quit": quit current window, quit Vim if the last window is closed.
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005225 * ":{nr}quit": quit window {nr}
Bram Moolenaar4d14bac2019-10-20 21:15:15 +02005226 * Also used when closing a terminal window that's the last one.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005227 */
Bram Moolenaar4d14bac2019-10-20 21:15:15 +02005228 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005229ex_quit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005230{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005231 win_T *wp;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005232
Bram Moolenaar071d4272004-06-13 20:20:40 +00005233#ifdef FEAT_CMDWIN
5234 if (cmdwin_type != 0)
5235 {
5236 cmdwin_result = Ctrl_C;
5237 return;
5238 }
5239#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005240 // Don't quit while editing the command line.
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005241 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005242 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005243 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005244 return;
5245 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005246 if (eap->addr_count > 0)
5247 {
Bram Moolenaarf240e182014-11-27 18:33:02 +01005248 int wnr = eap->line2;
5249
5250 for (wp = firstwin; wp->w_next != NULL; wp = wp->w_next)
5251 if (--wnr <= 0)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005252 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005253 }
5254 else
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005255 wp = curwin;
Bram Moolenaarf240e182014-11-27 18:33:02 +01005256
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005257 // Refuse to quit when locked.
Bram Moolenaar87ffb5c2017-10-19 12:37:42 +02005258 if (curbuf_locked())
5259 return;
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005260
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005261 // Trigger QuitPre and maybe ExitPre
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005262 if (before_quit_autocmds(wp, FALSE, eap->forceit))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005263 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005264
5265#ifdef FEAT_NETBEANS_INTG
5266 netbeansForcedQuit = eap->forceit;
5267#endif
5268
5269 /*
5270 * If there are more files or windows we won't exit.
5271 */
5272 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
5273 exiting = TRUE;
Bram Moolenaarff930ca2017-10-19 17:12:10 +02005274 if ((!buf_hide(wp->w_buffer)
5275 && check_changed(wp->w_buffer, (p_awa ? CCGD_AW : 0)
Bram Moolenaar45d3b142013-11-09 03:31:51 +01005276 | (eap->forceit ? CCGD_FORCEIT : 0)
5277 | CCGD_EXCMD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005278 || check_more(TRUE, eap->forceit) == FAIL
Bram Moolenaar027387f2016-01-02 22:25:52 +01005279 || (only_one_window() && check_changed_any(eap->forceit, TRUE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005280 {
5281 not_exiting();
5282 }
5283 else
5284 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005285 // quit last window
5286 // Note: only_one_window() returns true, even so a help window is
5287 // still open. In that case only quit, if no address has been
5288 // specified. Example:
5289 // :h|wincmd w|1q - don't quit
5290 // :h|wincmd w|q - quit
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01005291 if (only_one_window() && (ONE_WINDOW || eap->addr_count == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005292 getout(0);
Bram Moolenaar2c33d7b2017-10-14 16:06:20 +02005293 not_exiting();
Bram Moolenaar4033c552017-09-16 20:54:51 +02005294#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00005295 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005296#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005297 // close window; may free buffer
Bram Moolenaareb44a682017-08-03 22:44:55 +02005298 win_close(wp, !buf_hide(wp->w_buffer) || eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005299 }
5300}
5301
5302/*
5303 * ":cquit".
5304 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005305 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005306ex_cquit(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005307{
Bram Moolenaar1860bde2020-01-06 21:47:21 +01005308 // this does not always pass on the exit code to the Manx compiler. why?
5309 getout(eap->addr_count > 0 ? (int)eap->line2 : EXIT_FAILURE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005310}
5311
5312/*
5313 * ":qall": try to quit all windows
5314 */
5315 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005316ex_quit_all(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005317{
5318# ifdef FEAT_CMDWIN
5319 if (cmdwin_type != 0)
5320 {
5321 if (eap->forceit)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005322 cmdwin_result = K_XF1; // ex_window() takes care of this
Bram Moolenaar071d4272004-06-13 20:20:40 +00005323 else
5324 cmdwin_result = K_XF2;
5325 return;
5326 }
5327# endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005328
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005329 // Don't quit while editing the command line.
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005330 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005331 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005332 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005333 return;
5334 }
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005335
5336 if (before_quit_autocmds(curwin, TRUE, eap->forceit))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005337 return;
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005338
Bram Moolenaar071d4272004-06-13 20:20:40 +00005339 exiting = TRUE;
Bram Moolenaar027387f2016-01-02 22:25:52 +01005340 if (eap->forceit || !check_changed_any(FALSE, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005341 getout(0);
5342 not_exiting();
5343}
5344
Bram Moolenaar071d4272004-06-13 20:20:40 +00005345/*
5346 * ":close": close current window, unless it is the last one
5347 */
5348 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005349ex_close(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005350{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005351 win_T *win;
5352 int winnr = 0;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005353#ifdef FEAT_CMDWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00005354 if (cmdwin_type != 0)
Bram Moolenaar9bd1a7e2011-05-19 14:50:54 +02005355 cmdwin_result = Ctrl_C;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005356 else
Bram Moolenaar4033c552017-09-16 20:54:51 +02005357#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005358 if (!text_locked() && !curbuf_locked())
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005359 {
5360 if (eap->addr_count == 0)
5361 ex_win_close(eap->forceit, curwin, NULL);
Bram Moolenaar6d41c782018-06-06 09:11:12 +02005362 else
5363 {
Bram Moolenaar29323592016-07-24 22:04:11 +02005364 FOR_ALL_WINDOWS(win)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005365 {
5366 winnr++;
5367 if (winnr == eap->line2)
5368 break;
5369 }
5370 if (win == NULL)
5371 win = lastwin;
5372 ex_win_close(eap->forceit, win, NULL);
5373 }
5374 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005375}
5376
Bram Moolenaar4033c552017-09-16 20:54:51 +02005377#ifdef FEAT_QUICKFIX
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005378/*
5379 * ":pclose": Close any preview window.
5380 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005381 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005382ex_pclose(exarg_T *eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005383{
5384 win_T *win;
5385
Bram Moolenaar79648732019-07-18 21:43:07 +02005386 // First close any normal window.
Bram Moolenaar29323592016-07-24 22:04:11 +02005387 FOR_ALL_WINDOWS(win)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005388 if (win->w_p_pvw)
5389 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00005390 ex_win_close(eap->forceit, win, NULL);
Bram Moolenaar79648732019-07-18 21:43:07 +02005391 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005392 }
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01005393# ifdef FEAT_PROP_POPUP
Bram Moolenaar79648732019-07-18 21:43:07 +02005394 // Also when 'previewpopup' is empty, it might have been cleared.
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02005395 popup_close_preview();
Bram Moolenaar79648732019-07-18 21:43:07 +02005396# endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005397}
Bram Moolenaar4033c552017-09-16 20:54:51 +02005398#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005399
Bram Moolenaarf740b292006-02-16 22:11:02 +00005400/*
5401 * Close window "win" and take care of handling closing the last window for a
5402 * modified buffer.
5403 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005404 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005405ex_win_close(
5406 int forceit,
5407 win_T *win,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005408 tabpage_T *tp) // NULL or the tab page "win" is in
Bram Moolenaar071d4272004-06-13 20:20:40 +00005409{
5410 int need_hide;
5411 buf_T *buf = win->w_buffer;
5412
Bram Moolenaarcf844172020-06-26 19:44:06 +02005413 // Never close the autocommand window.
5414 if (win == aucmd_win)
5415 {
5416 emsg(_(e_autocmd_close));
5417 return;
5418 }
5419
Bram Moolenaar071d4272004-06-13 20:20:40 +00005420 need_hide = (bufIsChanged(buf) && buf->b_nwindows <= 1);
Bram Moolenaareb44a682017-08-03 22:44:55 +02005421 if (need_hide && !buf_hide(buf) && !forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005422 {
Bram Moolenaar4033c552017-09-16 20:54:51 +02005423#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005424 if ((p_confirm || cmdmod.confirm) && p_write)
5425 {
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005426 bufref_T bufref;
5427
5428 set_bufref(&bufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005429 dialog_changed(buf, FALSE);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005430 if (bufref_valid(&bufref) && bufIsChanged(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005431 return;
5432 need_hide = FALSE;
5433 }
5434 else
Bram Moolenaar4033c552017-09-16 20:54:51 +02005435#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005436 {
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02005437 no_write_message();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005438 return;
5439 }
5440 }
5441
Bram Moolenaar4033c552017-09-16 20:54:51 +02005442#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00005443 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005444#endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00005445
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005446 // free buffer when not hiding it or when it's a scratch buffer
Bram Moolenaarf740b292006-02-16 22:11:02 +00005447 if (tp == NULL)
Bram Moolenaareb44a682017-08-03 22:44:55 +02005448 win_close(win, !need_hide && !buf_hide(buf));
Bram Moolenaarf740b292006-02-16 22:11:02 +00005449 else
Bram Moolenaareb44a682017-08-03 22:44:55 +02005450 win_close_othertab(win, !need_hide && !buf_hide(buf), tp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005451}
5452
Bram Moolenaar071d4272004-06-13 20:20:40 +00005453/*
Bram Moolenaardea25702017-01-29 15:18:10 +01005454 * Handle the argument for a tabpage related ex command.
5455 * Returns a tabpage number.
5456 * When an error is encountered then eap->errmsg is set.
5457 */
5458 static int
5459get_tabpage_arg(exarg_T *eap)
5460{
5461 int tab_number;
5462 int unaccept_arg0 = (eap->cmdidx == CMD_tabmove) ? 0 : 1;
5463
5464 if (eap->arg && *eap->arg != NUL)
5465 {
5466 char_u *p = eap->arg;
5467 char_u *p_save;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005468 int relative = 0; // argument +N/-N means: go to N places to the
5469 // right/left relative to the current position.
Bram Moolenaardea25702017-01-29 15:18:10 +01005470
5471 if (*p == '-')
5472 {
5473 relative = -1;
5474 p++;
5475 }
5476 else if (*p == '+')
5477 {
5478 relative = 1;
5479 p++;
5480 }
5481
5482 p_save = p;
5483 tab_number = getdigits(&p);
5484
5485 if (relative == 0)
5486 {
5487 if (STRCMP(p, "$") == 0)
5488 tab_number = LAST_TAB_NR;
Bram Moolenaar94f4ffa2020-08-10 19:21:15 +02005489 else if (STRCMP(p, "#") == 0)
5490 if (valid_tabpage(lastused_tabpage))
5491 tab_number = tabpage_index(lastused_tabpage);
5492 else
5493 {
5494 eap->errmsg = ex_errmsg(e_invargval, eap->arg);
5495 tab_number = 0;
5496 goto theend;
5497 }
Bram Moolenaardea25702017-01-29 15:18:10 +01005498 else if (p == p_save || *p_save == '-' || *p != NUL
5499 || tab_number > LAST_TAB_NR)
5500 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005501 // No numbers as argument.
Bram Moolenaar8930caa2020-07-23 16:37:03 +02005502 eap->errmsg = ex_errmsg(e_invarg2, eap->arg);
Bram Moolenaardea25702017-01-29 15:18:10 +01005503 goto theend;
5504 }
5505 }
5506 else
5507 {
5508 if (*p_save == NUL)
5509 tab_number = 1;
5510 else if (p == p_save || *p_save == '-' || *p != NUL
5511 || tab_number == 0)
5512 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005513 // No numbers as argument.
Bram Moolenaar8930caa2020-07-23 16:37:03 +02005514 eap->errmsg = ex_errmsg(e_invarg2, eap->arg);
Bram Moolenaardea25702017-01-29 15:18:10 +01005515 goto theend;
5516 }
5517 tab_number = tab_number * relative + tabpage_index(curtab);
5518 if (!unaccept_arg0 && relative == -1)
5519 --tab_number;
5520 }
5521 if (tab_number < unaccept_arg0 || tab_number > LAST_TAB_NR)
Bram Moolenaar8930caa2020-07-23 16:37:03 +02005522 eap->errmsg = ex_errmsg(e_invarg2, eap->arg);
Bram Moolenaardea25702017-01-29 15:18:10 +01005523 }
5524 else if (eap->addr_count > 0)
5525 {
5526 if (unaccept_arg0 && eap->line2 == 0)
Bram Moolenaarc6251552017-01-29 21:42:20 +01005527 {
Bram Moolenaar8930caa2020-07-23 16:37:03 +02005528 eap->errmsg = _(e_invrange);
Bram Moolenaarc6251552017-01-29 21:42:20 +01005529 tab_number = 0;
5530 }
Bram Moolenaardea25702017-01-29 15:18:10 +01005531 else
5532 {
5533 tab_number = eap->line2;
Bram Moolenaar82a12462019-01-22 22:41:42 +01005534 if (!unaccept_arg0 && *skipwhite(*eap->cmdlinep) == '-')
Bram Moolenaardea25702017-01-29 15:18:10 +01005535 {
5536 --tab_number;
5537 if (tab_number < unaccept_arg0)
Bram Moolenaar8930caa2020-07-23 16:37:03 +02005538 eap->errmsg = _(e_invrange);
Bram Moolenaardea25702017-01-29 15:18:10 +01005539 }
5540 }
5541 }
5542 else
5543 {
5544 switch (eap->cmdidx)
5545 {
5546 case CMD_tabnext:
5547 tab_number = tabpage_index(curtab) + 1;
5548 if (tab_number > LAST_TAB_NR)
5549 tab_number = 1;
5550 break;
5551 case CMD_tabmove:
5552 tab_number = LAST_TAB_NR;
5553 break;
5554 default:
5555 tab_number = tabpage_index(curtab);
5556 }
5557 }
5558
5559theend:
5560 return tab_number;
5561}
5562
5563/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00005564 * ":tabclose": close current tab page, unless it is the last one.
5565 * ":tabclose N": close tab page N.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005566 */
5567 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005568ex_tabclose(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005569{
Bram Moolenaarf740b292006-02-16 22:11:02 +00005570 tabpage_T *tp;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005571 int tab_number;
Bram Moolenaarf740b292006-02-16 22:11:02 +00005572
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005573# ifdef FEAT_CMDWIN
5574 if (cmdwin_type != 0)
5575 cmdwin_result = K_IGNORE;
5576 else
5577# endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00005578 if (first_tabpage->tp_next == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005579 emsg(_("E784: Cannot close last tab page"));
Bram Moolenaarf740b292006-02-16 22:11:02 +00005580 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005581 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005582 tab_number = get_tabpage_arg(eap);
5583 if (eap->errmsg == NULL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005584 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005585 tp = find_tabpage(tab_number);
Bram Moolenaarf740b292006-02-16 22:11:02 +00005586 if (tp == NULL)
5587 {
5588 beep_flush();
5589 return;
5590 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005591 if (tp != curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00005592 {
5593 tabpage_close_other(tp, eap->forceit);
5594 return;
5595 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005596 else if (!text_locked() && !curbuf_locked())
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005597 tabpage_close(eap->forceit);
5598 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005599 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005600}
5601
5602/*
5603 * ":tabonly": close all tab pages except the current one
5604 */
5605 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005606ex_tabonly(exarg_T *eap)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005607{
5608 tabpage_T *tp;
5609 int done;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005610 int tab_number;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005611
5612# ifdef FEAT_CMDWIN
5613 if (cmdwin_type != 0)
5614 cmdwin_result = K_IGNORE;
5615 else
5616# endif
5617 if (first_tabpage->tp_next == NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01005618 msg(_("Already only one tab page"));
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005619 else
5620 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005621 tab_number = get_tabpage_arg(eap);
5622 if (eap->errmsg == NULL)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005623 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005624 goto_tabpage(tab_number);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005625 // Repeat this up to a 1000 times, because autocommands may
5626 // mess up the lists.
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005627 for (done = 0; done < 1000; ++done)
5628 {
5629 FOR_ALL_TABPAGES(tp)
5630 if (tp->tp_topframe != topframe)
5631 {
5632 tabpage_close_other(tp, eap->forceit);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005633 // if we failed to close it quit
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005634 if (valid_tabpage(tp))
5635 done = 1000;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005636 // start over, "tp" is now invalid
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005637 break;
5638 }
5639 if (first_tabpage->tp_next == NULL)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005640 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005641 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005642 }
5643 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005644}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005645
5646/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00005647 * Close the current tab page.
5648 */
5649 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005650tabpage_close(int forceit)
Bram Moolenaarf740b292006-02-16 22:11:02 +00005651{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005652 // First close all the windows but the current one. If that worked then
5653 // close the last window in this tab, that will close it.
Bram Moolenaar459ca562016-11-10 18:16:33 +01005654 if (!ONE_WINDOW)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005655 close_others(TRUE, forceit);
Bram Moolenaar459ca562016-11-10 18:16:33 +01005656 if (ONE_WINDOW)
Bram Moolenaarf740b292006-02-16 22:11:02 +00005657 ex_win_close(forceit, curwin, NULL);
5658# ifdef FEAT_GUI
5659 need_mouse_correct = TRUE;
5660# endif
5661}
5662
5663/*
5664 * Close tab page "tp", which is not the current tab page.
5665 * Note that autocommands may make "tp" invalid.
Bram Moolenaar7875acc2006-09-10 13:51:17 +00005666 * Also takes care of the tab pages line disappearing when closing the
5667 * last-but-one tab page.
Bram Moolenaarf740b292006-02-16 22:11:02 +00005668 */
5669 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005670tabpage_close_other(tabpage_T *tp, int forceit)
Bram Moolenaarf740b292006-02-16 22:11:02 +00005671{
5672 int done = 0;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005673 win_T *wp;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00005674 int h = tabline_height();
Bram Moolenaarf740b292006-02-16 22:11:02 +00005675
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005676 // Limit to 1000 windows, autocommands may add a window while we close
5677 // one. OK, so I'm paranoid...
Bram Moolenaarf740b292006-02-16 22:11:02 +00005678 while (++done < 1000)
5679 {
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005680 wp = tp->tp_firstwin;
5681 ex_win_close(forceit, wp, tp);
Bram Moolenaarf740b292006-02-16 22:11:02 +00005682
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005683 // Autocommands may delete the tab page under our fingers and we may
5684 // fail to close a window with a modified buffer.
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005685 if (!valid_tabpage(tp) || tp->tp_firstwin == wp)
Bram Moolenaarf740b292006-02-16 22:11:02 +00005686 break;
5687 }
Bram Moolenaar7875acc2006-09-10 13:51:17 +00005688
Bram Moolenaar29323592016-07-24 22:04:11 +02005689 apply_autocmds(EVENT_TABCLOSED, NULL, NULL, FALSE, curbuf);
Bram Moolenaar29323592016-07-24 22:04:11 +02005690
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005691 redraw_tabline = TRUE;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00005692 if (h != tabline_height())
5693 shell_new_rows();
Bram Moolenaarf740b292006-02-16 22:11:02 +00005694}
5695
5696/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005697 * ":only".
5698 */
5699 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005700ex_only(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005701{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005702 win_T *wp;
5703 int wnr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005704# ifdef FEAT_GUI
5705 need_mouse_correct = TRUE;
5706# endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005707 if (eap->addr_count > 0)
5708 {
5709 wnr = eap->line2;
5710 for (wp = firstwin; --wnr > 0; )
5711 {
5712 if (wp->w_next == NULL)
5713 break;
5714 else
5715 wp = wp->w_next;
5716 }
5717 win_goto(wp);
5718 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005719 close_others(TRUE, eap->forceit);
5720}
5721
Bram Moolenaar071d4272004-06-13 20:20:40 +00005722 static void
Bram Moolenaar5e1e6d22017-01-02 17:26:00 +01005723ex_hide(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005724{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005725 // ":hide" or ":hide | cmd": hide current window
Bram Moolenaar2256c992016-11-15 21:17:07 +01005726 if (!eap->skip)
5727 {
Bram Moolenaar4033c552017-09-16 20:54:51 +02005728#ifdef FEAT_GUI
Bram Moolenaar2256c992016-11-15 21:17:07 +01005729 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005730#endif
Bram Moolenaar2256c992016-11-15 21:17:07 +01005731 if (eap->addr_count == 0)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005732 win_close(curwin, FALSE); // don't free buffer
Bram Moolenaar2256c992016-11-15 21:17:07 +01005733 else
5734 {
5735 int winnr = 0;
5736 win_T *win;
Bram Moolenaarf240e182014-11-27 18:33:02 +01005737
Bram Moolenaar2256c992016-11-15 21:17:07 +01005738 FOR_ALL_WINDOWS(win)
5739 {
5740 winnr++;
5741 if (winnr == eap->line2)
5742 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005743 }
Bram Moolenaar2256c992016-11-15 21:17:07 +01005744 if (win == NULL)
5745 win = lastwin;
5746 win_close(win, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005747 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005748 }
5749}
5750
5751/*
5752 * ":stop" and ":suspend": Suspend Vim.
5753 */
5754 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005755ex_stop(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005756{
5757 /*
5758 * Disallow suspending for "rvim".
5759 */
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005760 if (!check_restricted())
Bram Moolenaar071d4272004-06-13 20:20:40 +00005761 {
5762 if (!eap->forceit)
5763 autowrite_all();
5764 windgoto((int)Rows - 1, 0);
5765 out_char('\n');
5766 out_flush();
5767 stoptermcap();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005768 out_flush(); // needed for SUN to restore xterm buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00005769#ifdef FEAT_TITLE
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005770 mch_restore_title(SAVE_RESTORE_BOTH); // restore window titles
Bram Moolenaar071d4272004-06-13 20:20:40 +00005771#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005772 ui_suspend(); // call machine specific function
Bram Moolenaar071d4272004-06-13 20:20:40 +00005773#ifdef FEAT_TITLE
5774 maketitle();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005775 resettitle(); // force updating the title
Bram Moolenaar071d4272004-06-13 20:20:40 +00005776#endif
5777 starttermcap();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005778 scroll_start(); // scroll screen before redrawing
Bram Moolenaar071d4272004-06-13 20:20:40 +00005779 redraw_later_clear();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005780 shell_resized(); // may have resized window
Bram Moolenaar071d4272004-06-13 20:20:40 +00005781 }
5782}
5783
5784/*
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005785 * ":exit", ":xit" and ":wq": Write file and quite the current window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005786 */
5787 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005788ex_exit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005789{
Bram Moolenaar461f2122020-07-28 20:25:47 +02005790#ifdef FEAT_EVAL
Bram Moolenaarae616492020-07-28 20:07:27 +02005791 if (not_in_vim9(eap) == FAIL)
5792 return;
Bram Moolenaar461f2122020-07-28 20:25:47 +02005793#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005794#ifdef FEAT_CMDWIN
5795 if (cmdwin_type != 0)
5796 {
5797 cmdwin_result = Ctrl_C;
5798 return;
5799 }
5800#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005801 // Don't quit while editing the command line.
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005802 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005803 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005804 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005805 return;
5806 }
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005807
5808 if (before_quit_autocmds(curwin, FALSE, eap->forceit))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005809 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005810
5811 /*
5812 * if more files or windows we won't exit
5813 */
5814 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
5815 exiting = TRUE;
5816 if ( ((eap->cmdidx == CMD_wq
5817 || curbufIsChanged())
5818 && do_write(eap) == FAIL)
5819 || check_more(TRUE, eap->forceit) == FAIL
Bram Moolenaar027387f2016-01-02 22:25:52 +01005820 || (only_one_window() && check_changed_any(eap->forceit, FALSE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005821 {
5822 not_exiting();
5823 }
5824 else
5825 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005826 if (only_one_window()) // quit last window, exit Vim
Bram Moolenaar071d4272004-06-13 20:20:40 +00005827 getout(0);
Bram Moolenaar2c33d7b2017-10-14 16:06:20 +02005828 not_exiting();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005829# ifdef FEAT_GUI
5830 need_mouse_correct = TRUE;
5831# endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005832 // Quit current window, may free the buffer.
Bram Moolenaareb44a682017-08-03 22:44:55 +02005833 win_close(curwin, !buf_hide(curwin->w_buffer));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005834 }
5835}
5836
5837/*
5838 * ":print", ":list", ":number".
5839 */
5840 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005841ex_print(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005842{
Bram Moolenaardf177f62005-02-22 08:39:57 +00005843 if (curbuf->b_ml.ml_flags & ML_EMPTY)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005844 emsg(_(e_emptybuf));
Bram Moolenaardf177f62005-02-22 08:39:57 +00005845 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005846 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00005847 for ( ;!got_int; ui_breakcheck())
5848 {
5849 print_line(eap->line1,
5850 (eap->cmdidx == CMD_number || eap->cmdidx == CMD_pound
5851 || (eap->flags & EXFLAG_NR)),
5852 eap->cmdidx == CMD_list || (eap->flags & EXFLAG_LIST));
5853 if (++eap->line1 > eap->line2)
5854 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005855 out_flush(); // show one line at a time
Bram Moolenaardf177f62005-02-22 08:39:57 +00005856 }
5857 setpcmark();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005858 // put cursor at last line
Bram Moolenaardf177f62005-02-22 08:39:57 +00005859 curwin->w_cursor.lnum = eap->line2;
5860 beginline(BL_SOL | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005861 }
5862
Bram Moolenaar071d4272004-06-13 20:20:40 +00005863 ex_no_reprint = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005864}
5865
5866#ifdef FEAT_BYTEOFF
5867 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005868ex_goto(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005869{
5870 goto_byte(eap->line2);
5871}
5872#endif
5873
5874/*
5875 * ":shell".
5876 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005877 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005878ex_shell(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005879{
5880 do_shell(NULL, 0);
5881}
5882
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005883#if defined(HAVE_DROP_FILE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005884
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005885static int drop_busy = FALSE;
5886static int drop_filec;
5887static char_u **drop_filev = NULL;
5888static int drop_split;
5889static void (*drop_callback)(void *);
5890static void *drop_cookie;
5891
5892 static void
5893handle_drop_internal(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005894{
5895 exarg_T ea;
5896 int save_msg_scroll = msg_scroll;
5897
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005898 // Setting the argument list may cause screen updates and being called
5899 // recursively. Avoid that by setting drop_busy.
5900 drop_busy = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005901
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005902 // Check whether the current buffer is changed. If so, we will need
5903 // to split the current window or data could be lost.
5904 // We don't need to check if the 'hidden' option is set, as in this
5905 // case the buffer won't be lost.
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005906 if (!buf_hide(curbuf) && !drop_split)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005907 {
5908 ++emsg_off;
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005909 drop_split = check_changed(curbuf, CCGD_AW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005910 --emsg_off;
5911 }
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005912 if (drop_split)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005913 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005914 if (win_split(0, 0) == FAIL)
5915 return;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02005916 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005917
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005918 // When splitting the window, create a new alist. Otherwise the
5919 // existing one is overwritten.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005920 alist_unlink(curwin->w_alist);
5921 alist_new();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005922 }
5923
5924 /*
5925 * Set up the new argument list.
5926 */
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005927 alist_set(ALIST(curwin), drop_filec, drop_filev, FALSE, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005928
5929 /*
5930 * Move to the first file.
5931 */
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005932 // Fake up a minimal "next" command for do_argfile()
Bram Moolenaara80faa82020-04-12 19:37:17 +02005933 CLEAR_FIELD(ea);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005934 ea.cmd = (char_u *)"next";
5935 do_argfile(&ea, 0);
5936
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005937 // do_ecmd() may set need_start_insertmode, but since we never left Insert
5938 // mode that is not needed here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005939 need_start_insertmode = FALSE;
5940
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005941 // Restore msg_scroll, otherwise a following command may cause scrolling
5942 // unexpectedly. The screen will be redrawn by the caller, thus
5943 // msg_scroll being set by displaying a message is irrelevant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005944 msg_scroll = save_msg_scroll;
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005945
5946 if (drop_callback != NULL)
5947 drop_callback(drop_cookie);
5948
5949 drop_filev = NULL;
5950 drop_busy = FALSE;
5951}
5952
5953/*
5954 * Handle a file drop. The code is here because a drop is *nearly* like an
5955 * :args command, but not quite (we have a list of exact filenames, so we
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01005956 * don't want to (a) parse a command line, or (b) expand wildcards). So the
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005957 * code is very similar to :args and hence needs access to a lot of the static
5958 * functions in this file.
5959 *
5960 * The "filev" list must have been allocated using alloc(), as should each item
5961 * in the list. This function takes over responsibility for freeing the "filev"
5962 * list.
5963 */
5964 void
5965handle_drop(
5966 int filec, // the number of files dropped
5967 char_u **filev, // the list of files dropped
5968 int split, // force splitting the window
5969 void (*callback)(void *), // to be called after setting the argument
5970 // list
5971 void *cookie) // argument for "callback" (allocated)
5972{
5973 // Cannot handle recursive drops, finish the pending one.
5974 if (drop_busy)
5975 {
5976 FreeWild(filec, filev);
5977 vim_free(cookie);
5978 return;
5979 }
5980
5981 // When calling handle_drop() more than once in a row we only use the last
5982 // one.
5983 if (drop_filev != NULL)
5984 {
5985 FreeWild(drop_filec, drop_filev);
5986 vim_free(drop_cookie);
5987 }
5988
5989 drop_filec = filec;
5990 drop_filev = filev;
5991 drop_split = split;
5992 drop_callback = callback;
5993 drop_cookie = cookie;
5994
5995 // Postpone this when:
5996 // - editing the command line
5997 // - not possible to change the current buffer
5998 // - updating the screen
5999 // As it may change buffers and window structures that are in use and cause
6000 // freed memory to be used.
6001 if (text_locked() || curbuf_locked() || updating_screen)
6002 return;
6003
6004 handle_drop_internal();
6005}
6006
6007/*
6008 * To be called when text is unlocked, curbuf is unlocked or updating_screen is
6009 * reset: Handle a postponed drop.
6010 */
6011 void
6012handle_any_postponed_drop(void)
6013{
6014 if (!drop_busy && drop_filev != NULL
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +02006015 && !text_locked() && !curbuf_locked() && !updating_screen)
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006016 handle_drop_internal();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006017}
6018#endif
6019
Bram Moolenaar071d4272004-06-13 20:20:40 +00006020/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006021 * ":preserve".
6022 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006023 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006024ex_preserve(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006025{
Bram Moolenaar4399ef42005-02-12 14:29:27 +00006026 curbuf->b_flags |= BF_PRESERVED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006027 ml_preserve(curbuf, TRUE);
6028}
6029
6030/*
6031 * ":recover".
6032 */
6033 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006034ex_recover(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006035{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006036 // Set recoverymode right away to avoid the ATTENTION prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006037 recoverymode = TRUE;
Bram Moolenaar45d3b142013-11-09 03:31:51 +01006038 if (!check_changed(curbuf, (p_awa ? CCGD_AW : 0)
6039 | CCGD_MULTWIN
6040 | (eap->forceit ? CCGD_FORCEIT : 0)
6041 | CCGD_EXCMD)
6042
Bram Moolenaar071d4272004-06-13 20:20:40 +00006043 && (*eap->arg == NUL
6044 || setfname(curbuf, eap->arg, NULL, TRUE) == OK))
Bram Moolenaar99499b12019-05-23 21:35:48 +02006045 ml_recover(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006046 recoverymode = FALSE;
6047}
6048
6049/*
6050 * Command modifier used in a wrong way.
6051 */
6052 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006053ex_wrongmodifier(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006054{
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006055 eap->errmsg = _(e_invalid_command);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006056}
6057
Bram Moolenaar071d4272004-06-13 20:20:40 +00006058/*
6059 * :sview [+command] file split window with new file, read-only
6060 * :split [[+command] file] split window with current or new file
6061 * :vsplit [[+command] file] split window vertically with current or new file
6062 * :new [[+command] file] split window with no or new file
6063 * :vnew [[+command] file] split vertically window with no or new file
6064 * :sfind [+command] file split window with file in 'path'
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006065 *
6066 * :tabedit open new Tab page with empty window
6067 * :tabedit [+command] file open new Tab page and edit "file"
6068 * :tabnew [[+command] file] just like :tabedit
6069 * :tabfind [+command] file open new Tab page and find "file"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006070 */
6071 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006072ex_splitview(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006073{
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006074 win_T *old_curwin = curwin;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006075#if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006076 char_u *fname = NULL;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006077#endif
6078#ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00006079 int browse_flag = cmdmod.browse;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006080#endif
Bram Moolenaar39902a02018-06-21 22:10:08 +02006081 int use_tab = eap->cmdidx == CMD_tabedit
6082 || eap->cmdidx == CMD_tabfind
6083 || eap->cmdidx == CMD_tabnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006084
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01006085 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02006086 return;
6087
Bram Moolenaar4033c552017-09-16 20:54:51 +02006088#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00006089 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006090#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006091
Bram Moolenaar4033c552017-09-16 20:54:51 +02006092#ifdef FEAT_QUICKFIX
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006093 // A ":split" in the quickfix window works like ":new". Don't want two
6094 // quickfix windows. But it's OK when doing ":tab split".
Bram Moolenaar05bb9532008-07-04 09:44:11 +00006095 if (bt_quickfix(curbuf) && cmdmod.tab == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006096 {
6097 if (eap->cmdidx == CMD_split)
6098 eap->cmdidx = CMD_new;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006099 if (eap->cmdidx == CMD_vsplit)
6100 eap->cmdidx = CMD_vnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006101 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02006102#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006103
Bram Moolenaar4033c552017-09-16 20:54:51 +02006104#ifdef FEAT_SEARCHPATH
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006105 if (eap->cmdidx == CMD_sfind || eap->cmdidx == CMD_tabfind)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006106 {
6107 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg),
6108 FNAME_MESS, TRUE, curbuf->b_ffname);
6109 if (fname == NULL)
6110 goto theend;
6111 eap->arg = fname;
6112 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006113# ifdef FEAT_BROWSE
Bram Moolenaar4033c552017-09-16 20:54:51 +02006114 else
6115# endif
6116#endif
6117#ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00006118 if (cmdmod.browse
Bram Moolenaar071d4272004-06-13 20:20:40 +00006119 && eap->cmdidx != CMD_vnew
Bram Moolenaar071d4272004-06-13 20:20:40 +00006120 && eap->cmdidx != CMD_new)
6121 {
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006122 if (
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006123# ifdef FEAT_GUI
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006124 !gui.in_use &&
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006125# endif
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006126 au_has_group((char_u *)"FileExplorer"))
6127 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006128 // No browsing supported but we do have the file explorer:
6129 // Edit the directory.
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006130 if (*eap->arg == NUL || !mch_isdir(eap->arg))
6131 eap->arg = (char_u *)".";
6132 }
6133 else
6134 {
Bram Moolenaar39902a02018-06-21 22:10:08 +02006135 fname = do_browse(0, (char_u *)(use_tab
6136 ? _("Edit File in new tab page")
6137 : _("Edit File in new window")),
Bram Moolenaar071d4272004-06-13 20:20:40 +00006138 eap->arg, NULL, NULL, NULL, curbuf);
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006139 if (fname == NULL)
6140 goto theend;
6141 eap->arg = fname;
6142 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006143 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006144 cmdmod.browse = FALSE; // Don't browse again in do_ecmd().
Bram Moolenaar4033c552017-09-16 20:54:51 +02006145#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006146
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006147 /*
6148 * Either open new tab page or split the window.
6149 */
Bram Moolenaar39902a02018-06-21 22:10:08 +02006150 if (use_tab)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006151 {
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006152 if (win_new_tabpage(cmdmod.tab != 0 ? cmdmod.tab
6153 : eap->addr_count == 0 ? 0
6154 : (int)eap->line2 + 1) != FAIL)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006155 {
Bram Moolenaard2b66012008-01-09 19:30:36 +00006156 do_exedit(eap, old_curwin);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006157
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006158 // set the alternate buffer for the window we came from
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006159 if (curwin != old_curwin
6160 && win_valid(old_curwin)
6161 && old_curwin->w_buffer != curbuf
6162 && !cmdmod.keepalt)
6163 old_curwin->w_alt_fnum = curbuf->b_fnum;
6164 }
6165 }
6166 else if (win_split(eap->addr_count > 0 ? (int)eap->line2 : 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006167 *eap->cmd == 'v' ? WSP_VERT : 0) != FAIL)
6168 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006169 // Reset 'scrollbind' when editing another file, but keep it when
6170 // doing ":split" without arguments.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006171 if (*eap->arg != NUL
Bram Moolenaar8a3bb562018-03-04 20:14:14 +01006172# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00006173 || cmdmod.browse
Bram Moolenaar8a3bb562018-03-04 20:14:14 +01006174# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006175 )
Bram Moolenaar3368ea22010-09-21 16:56:35 +02006176 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006177 else
6178 do_check_scrollbind(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006179 do_exedit(eap, old_curwin);
6180 }
6181
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006182# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00006183 cmdmod.browse = browse_flag;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006184# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006185
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006186# if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006187theend:
6188 vim_free(fname);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006189# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006190}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006191
6192/*
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006193 * Open a new tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006194 */
6195 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006196tabpage_new(void)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006197{
6198 exarg_T ea;
6199
Bram Moolenaara80faa82020-04-12 19:37:17 +02006200 CLEAR_FIELD(ea);
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006201 ea.cmdidx = CMD_tabnew;
6202 ea.cmd = (char_u *)"tabn";
6203 ea.arg = (char_u *)"";
6204 ex_splitview(&ea);
6205}
6206
6207/*
6208 * :tabnext command
6209 */
6210 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006211ex_tabnext(exarg_T *eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006212{
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006213 int tab_number;
6214
Bram Moolenaar8cdbd5b2019-06-16 15:50:45 +02006215 if (ERROR_IF_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02006216 return;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006217 switch (eap->cmdidx)
6218 {
6219 case CMD_tabfirst:
6220 case CMD_tabrewind:
6221 goto_tabpage(1);
6222 break;
6223 case CMD_tablast:
6224 goto_tabpage(9999);
6225 break;
6226 case CMD_tabprevious:
6227 case CMD_tabNext:
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006228 if (eap->arg && *eap->arg != NUL)
6229 {
6230 char_u *p = eap->arg;
6231 char_u *p_save = p;
6232
6233 tab_number = getdigits(&p);
6234 if (p == p_save || *p_save == '-' || *p != NUL
6235 || tab_number == 0)
6236 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006237 // No numbers as argument.
Bram Moolenaar8930caa2020-07-23 16:37:03 +02006238 eap->errmsg = ex_errmsg(e_invarg2, eap->arg);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006239 return;
6240 }
6241 }
6242 else
6243 {
6244 if (eap->addr_count == 0)
6245 tab_number = 1;
6246 else
6247 {
6248 tab_number = eap->line2;
6249 if (tab_number < 1)
6250 {
Bram Moolenaar8930caa2020-07-23 16:37:03 +02006251 eap->errmsg = _(e_invrange);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006252 return;
6253 }
6254 }
6255 }
6256 goto_tabpage(-tab_number);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006257 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006258 default: // CMD_tabnext
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006259 tab_number = get_tabpage_arg(eap);
6260 if (eap->errmsg == NULL)
6261 goto_tabpage(tab_number);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006262 break;
6263 }
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006264}
6265
6266/*
6267 * :tabmove command
6268 */
6269 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006270ex_tabmove(exarg_T *eap)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006271{
Bram Moolenaar40ce3a42015-04-21 18:08:39 +02006272 int tab_number;
Bram Moolenaar8cb8dca2012-07-06 18:27:39 +02006273
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006274 tab_number = get_tabpage_arg(eap);
6275 if (eap->errmsg == NULL)
6276 tabpage_move(tab_number);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006277}
6278
6279/*
6280 * :tabs command: List tabs and their contents.
6281 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006282 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006283ex_tabs(exarg_T *eap UNUSED)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006284{
6285 tabpage_T *tp;
6286 win_T *wp;
6287 int tabcount = 1;
6288
6289 msg_start();
6290 msg_scroll = TRUE;
6291 for (tp = first_tabpage; tp != NULL && !got_int; tp = tp->tp_next)
6292 {
6293 msg_putchar('\n');
6294 vim_snprintf((char *)IObuff, IOSIZE, _("Tab page %d"), tabcount++);
Bram Moolenaar8820b482017-03-16 17:23:31 +01006295 msg_outtrans_attr(IObuff, HL_ATTR(HLF_T));
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006296 out_flush(); // output one line at a time
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006297 ui_breakcheck();
6298
Bram Moolenaar030f0df2006-02-21 22:02:53 +00006299 if (tp == curtab)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006300 wp = firstwin;
6301 else
6302 wp = tp->tp_firstwin;
6303 for ( ; wp != NULL && !got_int; wp = wp->w_next)
6304 {
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006305 msg_putchar('\n');
6306 msg_putchar(wp == curwin ? '>' : ' ');
6307 msg_putchar(' ');
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006308 msg_putchar(bufIsChanged(wp->w_buffer) ? '+' : ' ');
6309 msg_putchar(' ');
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006310 if (buf_spname(wp->w_buffer) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02006311 vim_strncpy(IObuff, buf_spname(wp->w_buffer), IOSIZE - 1);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006312 else
6313 home_replace(wp->w_buffer, wp->w_buffer->b_fname,
6314 IObuff, IOSIZE, TRUE);
6315 msg_outtrans(IObuff);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006316 out_flush(); // output one line at a time
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006317 ui_breakcheck();
6318 }
6319 }
6320}
6321
Bram Moolenaar071d4272004-06-13 20:20:40 +00006322/*
6323 * ":mode": Set screen mode.
6324 * If no argument given, just get the screen size and redraw.
6325 */
6326 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006327ex_mode(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006328{
6329 if (*eap->arg == NUL)
6330 shell_resized();
6331 else
Bram Moolenaar3c71aec2020-01-17 19:32:20 +01006332 emsg(_(e_screenmode));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006333}
6334
Bram Moolenaar071d4272004-06-13 20:20:40 +00006335/*
6336 * ":resize".
6337 * set, increment or decrement current window height
6338 */
6339 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006340ex_resize(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006341{
6342 int n;
6343 win_T *wp = curwin;
6344
6345 if (eap->addr_count > 0)
6346 {
6347 n = eap->line2;
6348 for (wp = firstwin; wp->w_next != NULL && --n > 0; wp = wp->w_next)
6349 ;
6350 }
6351
Bram Moolenaar44a2f922016-03-19 22:11:51 +01006352# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00006353 need_mouse_correct = TRUE;
Bram Moolenaar44a2f922016-03-19 22:11:51 +01006354# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006355 n = atol((char *)eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006356 if (cmdmod.split & WSP_VERT)
6357 {
6358 if (*eap->arg == '-' || *eap->arg == '+')
Bram Moolenaar02631462017-09-22 15:20:32 +02006359 n += curwin->w_width;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006360 else if (n == 0 && eap->arg[0] == NUL) // default is very wide
Bram Moolenaar071d4272004-06-13 20:20:40 +00006361 n = 9999;
6362 win_setwidth_win((int)n, wp);
6363 }
6364 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006365 {
6366 if (*eap->arg == '-' || *eap->arg == '+')
6367 n += curwin->w_height;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006368 else if (n == 0 && eap->arg[0] == NUL) // default is very high
Bram Moolenaar071d4272004-06-13 20:20:40 +00006369 n = 9999;
6370 win_setheight_win((int)n, wp);
6371 }
6372}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006373
6374/*
6375 * ":find [+command] <file>" command.
6376 */
6377 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006378ex_find(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006379{
6380#ifdef FEAT_SEARCHPATH
6381 char_u *fname;
6382 int count;
6383
6384 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg), FNAME_MESS,
6385 TRUE, curbuf->b_ffname);
6386 if (eap->addr_count > 0)
6387 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006388 // Repeat finding the file "count" times. This matters when it
6389 // appears several times in the path.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006390 count = eap->line2;
6391 while (fname != NULL && --count > 0)
6392 {
6393 vim_free(fname);
6394 fname = find_file_in_path(NULL, 0, FNAME_MESS,
6395 FALSE, curbuf->b_ffname);
6396 }
6397 }
6398
6399 if (fname != NULL)
6400 {
6401 eap->arg = fname;
6402#endif
6403 do_exedit(eap, NULL);
6404#ifdef FEAT_SEARCHPATH
6405 vim_free(fname);
6406 }
6407#endif
6408}
6409
6410/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00006411 * ":open" simulation: for now just work like ":visual".
6412 */
6413 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006414ex_open(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00006415{
6416 regmatch_T regmatch;
6417 char_u *p;
6418
6419 curwin->w_cursor.lnum = eap->line2;
6420 beginline(BL_SOL | BL_FIX);
6421 if (*eap->arg == '/')
6422 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006423 // ":open /pattern/": put cursor in column found with pattern
Bram Moolenaardf177f62005-02-22 08:39:57 +00006424 ++eap->arg;
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006425 p = skip_regexp(eap->arg, '/', p_magic);
Bram Moolenaardf177f62005-02-22 08:39:57 +00006426 *p = NUL;
6427 regmatch.regprog = vim_regcomp(eap->arg, p_magic ? RE_MAGIC : 0);
6428 if (regmatch.regprog != NULL)
6429 {
6430 regmatch.rm_ic = p_ic;
6431 p = ml_get_curline();
6432 if (vim_regexec(&regmatch, p, (colnr_T)0))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006433 curwin->w_cursor.col = (colnr_T)(regmatch.startp[0] - p);
Bram Moolenaardf177f62005-02-22 08:39:57 +00006434 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006435 emsg(_(e_nomatch));
Bram Moolenaar473de612013-06-08 18:19:48 +02006436 vim_regfree(regmatch.regprog);
Bram Moolenaardf177f62005-02-22 08:39:57 +00006437 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006438 // Move to the NUL, ignore any other arguments.
Bram Moolenaardf177f62005-02-22 08:39:57 +00006439 eap->arg += STRLEN(eap->arg);
6440 }
6441 check_cursor();
6442
6443 eap->cmdidx = CMD_visual;
6444 do_exedit(eap, NULL);
6445}
6446
6447/*
6448 * ":edit", ":badd", ":visual".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006449 */
6450 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006451ex_edit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006452{
6453 do_exedit(eap, NULL);
6454}
6455
6456/*
Bram Moolenaar4b96df52020-01-26 22:00:26 +01006457 * ":edit <file>" command and alike.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006458 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006459 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006460do_exedit(
6461 exarg_T *eap,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006462 win_T *old_curwin) // curwin before doing a split or NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006463{
6464 int n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006465 int need_hide;
Bram Moolenaardf177f62005-02-22 08:39:57 +00006466 int exmode_was = exmode_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006467
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01006468 if ((eap->cmdidx != CMD_pedit && ERROR_IF_POPUP_WINDOW)
6469 || ERROR_IF_TERM_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02006470 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006471 /*
6472 * ":vi" command ends Ex mode.
6473 */
6474 if (exmode_active && (eap->cmdidx == CMD_visual
6475 || eap->cmdidx == CMD_view))
6476 {
6477 exmode_active = FALSE;
Bram Moolenaar158ea172020-06-18 17:28:39 +02006478 ex_pressedreturn = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006479 if (*eap->arg == NUL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00006480 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006481 // Special case: ":global/pat/visual\NLvi-commands"
Bram Moolenaardf177f62005-02-22 08:39:57 +00006482 if (global_busy)
6483 {
6484 int rd = RedrawingDisabled;
6485 int nwr = no_wait_return;
6486 int ms = msg_scroll;
6487#ifdef FEAT_GUI
6488 int he = hold_gui_events;
6489#endif
6490
6491 if (eap->nextcmd != NULL)
6492 {
6493 stuffReadbuff(eap->nextcmd);
6494 eap->nextcmd = NULL;
6495 }
6496
6497 if (exmode_was != EXMODE_VIM)
6498 settmode(TMODE_RAW);
6499 RedrawingDisabled = 0;
6500 no_wait_return = 0;
6501 need_wait_return = FALSE;
6502 msg_scroll = 0;
6503#ifdef FEAT_GUI
6504 hold_gui_events = 0;
6505#endif
6506 must_redraw = CLEAR;
Bram Moolenaar9e2bcb52020-02-18 21:33:00 +01006507 pending_exmode_active = TRUE;
Bram Moolenaardf177f62005-02-22 08:39:57 +00006508
6509 main_loop(FALSE, TRUE);
6510
Bram Moolenaar9e2bcb52020-02-18 21:33:00 +01006511 pending_exmode_active = FALSE;
Bram Moolenaardf177f62005-02-22 08:39:57 +00006512 RedrawingDisabled = rd;
6513 no_wait_return = nwr;
6514 msg_scroll = ms;
6515#ifdef FEAT_GUI
6516 hold_gui_events = he;
6517#endif
6518 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006519 return;
Bram Moolenaardf177f62005-02-22 08:39:57 +00006520 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006521 }
6522
6523 if ((eap->cmdidx == CMD_new
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006524 || eap->cmdidx == CMD_tabnew
6525 || eap->cmdidx == CMD_tabedit
Bram Moolenaar4033c552017-09-16 20:54:51 +02006526 || eap->cmdidx == CMD_vnew) && *eap->arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006527 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006528 // ":new" or ":tabnew" without argument: edit an new empty buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00006529 setpcmark();
6530 (void)do_ecmd(0, NULL, NULL, eap, ECMD_ONE,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00006531 ECMD_HIDE + (eap->forceit ? ECMD_FORCEIT : 0),
6532 old_curwin == NULL ? curwin : NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006533 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02006534 else if ((eap->cmdidx != CMD_split && eap->cmdidx != CMD_vsplit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006535 || *eap->arg != NUL
6536#ifdef FEAT_BROWSE
6537 || cmdmod.browse
6538#endif
6539 )
6540 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006541 // Can't edit another file when "curbuf_lock" is set. Only ":edit"
6542 // can bring us here, others are stopped earlier.
Bram Moolenaar5555acc2006-04-07 21:33:12 +00006543 if (*eap->arg != NUL && curbuf_locked())
6544 return;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006545
Bram Moolenaar071d4272004-06-13 20:20:40 +00006546 n = readonlymode;
6547 if (eap->cmdidx == CMD_view || eap->cmdidx == CMD_sview)
6548 readonlymode = TRUE;
6549 else if (eap->cmdidx == CMD_enew)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006550 readonlymode = FALSE; // 'readonly' doesn't make sense in an
6551 // empty buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00006552 setpcmark();
6553 if (do_ecmd(0, (eap->cmdidx == CMD_enew ? NULL : eap->arg),
6554 NULL, eap,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006555 // ":edit" goes to first line if Vi compatible
Bram Moolenaar071d4272004-06-13 20:20:40 +00006556 (*eap->arg == NUL && eap->do_ecmd_lnum == 0
6557 && vim_strchr(p_cpo, CPO_GOTO1) != NULL)
6558 ? ECMD_ONE : eap->do_ecmd_lnum,
Bram Moolenaareb44a682017-08-03 22:44:55 +02006559 (buf_hide(curbuf) ? ECMD_HIDE : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006560 + (eap->forceit ? ECMD_FORCEIT : 0)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006561 // after a split we can use an existing buffer
Bram Moolenaar7b449342014-03-25 13:03:48 +01006562 + (old_curwin != NULL ? ECMD_OLDBUF : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006563 + (eap->cmdidx == CMD_badd ? ECMD_ADDBUF : 0 )
Bram Moolenaar701f7af2008-11-15 13:12:07 +00006564 , old_curwin == NULL ? curwin : NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006565 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006566 // Editing the file failed. If the window was split, close it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006567 if (old_curwin != NULL)
6568 {
6569 need_hide = (curbufIsChanged() && curbuf->b_nwindows <= 1);
Bram Moolenaareb44a682017-08-03 22:44:55 +02006570 if (!need_hide || buf_hide(curbuf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006571 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006572#if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00006573 cleanup_T cs;
6574
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006575 // Reset the error/interrupt/exception state here so that
6576 // aborting() returns FALSE when closing a window.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00006577 enter_cleanup(&cs);
Bram Moolenaar4033c552017-09-16 20:54:51 +02006578#endif
6579#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00006580 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006581#endif
Bram Moolenaareb44a682017-08-03 22:44:55 +02006582 win_close(curwin, !need_hide && !buf_hide(curbuf));
Bram Moolenaarc0197e22004-09-13 20:26:32 +00006583
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006584#if defined(FEAT_EVAL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006585 // Restore the error/interrupt/exception state if not
6586 // discarded by a new aborting error, interrupt, or
6587 // uncaught exception.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00006588 leave_cleanup(&cs);
Bram Moolenaar4033c552017-09-16 20:54:51 +02006589#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006590 }
6591 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006592 }
6593 else if (readonlymode && curbuf->b_nwindows == 1)
6594 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006595 // When editing an already visited buffer, 'readonly' won't be set
6596 // but the previous value is kept. With ":view" and ":sview" we
6597 // want the file to be readonly, except when another window is
6598 // editing the same buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006599 curbuf->b_p_ro = TRUE;
6600 }
6601 readonlymode = n;
6602 }
6603 else
6604 {
6605 if (eap->do_ecmd_cmd != NULL)
6606 do_cmdline_cmd(eap->do_ecmd_cmd);
6607#ifdef FEAT_TITLE
6608 n = curwin->w_arg_idx_invalid;
6609#endif
6610 check_arg_idx(curwin);
6611#ifdef FEAT_TITLE
6612 if (n != curwin->w_arg_idx_invalid)
6613 maketitle();
6614#endif
6615 }
6616
Bram Moolenaar071d4272004-06-13 20:20:40 +00006617 /*
6618 * if ":split file" worked, set alternate file name in old window to new
6619 * file
6620 */
6621 if (old_curwin != NULL
6622 && *eap->arg != NUL
6623 && curwin != old_curwin
6624 && win_valid(old_curwin)
Bram Moolenaard4755bb2004-09-02 19:12:26 +00006625 && old_curwin->w_buffer != curbuf
6626 && !cmdmod.keepalt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006627 old_curwin->w_alt_fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006628
6629 ex_no_reprint = TRUE;
6630}
6631
6632#ifndef FEAT_GUI
6633/*
6634 * ":gui" and ":gvim" when there is no GUI.
6635 */
6636 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006637ex_nogui(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006638{
Bram Moolenaar8930caa2020-07-23 16:37:03 +02006639 eap->errmsg = _(e_nogvim);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006640}
6641#endif
6642
Bram Moolenaar4f974752019-02-17 17:44:42 +01006643#if defined(FEAT_GUI_MSWIN) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006644 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006645ex_tearoff(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006646{
6647 gui_make_tearoff(eap->arg);
6648}
6649#endif
6650
Bram Moolenaar29a2c082018-03-05 21:06:23 +01006651#if (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK) \
6652 || defined(FEAT_TERM_POPUP_MENU)) && defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006653 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006654ex_popup(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006655{
Bram Moolenaar29a2c082018-03-05 21:06:23 +01006656# if defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK)
6657 if (gui.in_use)
6658 gui_make_popup(eap->arg, eap->forceit);
6659# ifdef FEAT_TERM_POPUP_MENU
6660 else
6661# endif
6662# endif
6663# ifdef FEAT_TERM_POPUP_MENU
6664 pum_make_popup(eap->arg, eap->forceit);
6665# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006666}
6667#endif
6668
Bram Moolenaar071d4272004-06-13 20:20:40 +00006669 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006670ex_swapname(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006671{
6672 if (curbuf->b_ml.ml_mfp == NULL || curbuf->b_ml.ml_mfp->mf_fname == NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01006673 msg(_("No swap file"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006674 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01006675 msg((char *)curbuf->b_ml.ml_mfp->mf_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006676}
6677
6678/*
6679 * ":syncbind" forces all 'scrollbind' windows to have the same relative
6680 * offset.
6681 * (1998-11-02 16:21:01 R. Edward Ralston <eralston@computer.org>)
6682 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006683 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006684ex_syncbind(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006685{
Bram Moolenaar071d4272004-06-13 20:20:40 +00006686 win_T *wp;
Bram Moolenaardedd1b02013-12-14 13:06:17 +01006687 win_T *save_curwin = curwin;
6688 buf_T *save_curbuf = curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006689 long topline;
6690 long y;
6691 linenr_T old_linenr = curwin->w_cursor.lnum;
6692
6693 setpcmark();
6694
6695 /*
6696 * determine max topline
6697 */
6698 if (curwin->w_p_scb)
6699 {
6700 topline = curwin->w_topline;
Bram Moolenaar29323592016-07-24 22:04:11 +02006701 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006702 {
6703 if (wp->w_p_scb && wp->w_buffer)
6704 {
Bram Moolenaar375e3392019-01-31 18:26:10 +01006705 y = wp->w_buffer->b_ml.ml_line_count - get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006706 if (topline > y)
6707 topline = y;
6708 }
6709 }
6710 if (topline < 1)
6711 topline = 1;
6712 }
6713 else
6714 {
6715 topline = 1;
6716 }
6717
6718
6719 /*
Bram Moolenaardedd1b02013-12-14 13:06:17 +01006720 * Set all scrollbind windows to the same topline.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006721 */
Bram Moolenaar29323592016-07-24 22:04:11 +02006722 FOR_ALL_WINDOWS(curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006723 {
6724 if (curwin->w_p_scb)
6725 {
Bram Moolenaardedd1b02013-12-14 13:06:17 +01006726 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006727 y = topline - curwin->w_topline;
6728 if (y > 0)
6729 scrollup(y, TRUE);
6730 else
6731 scrolldown(-y, TRUE);
6732 curwin->w_scbind_pos = topline;
6733 redraw_later(VALID);
6734 cursor_correct();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006735 curwin->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006736 }
6737 }
Bram Moolenaardedd1b02013-12-14 13:06:17 +01006738 curwin = save_curwin;
6739 curbuf = save_curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006740 if (curwin->w_p_scb)
6741 {
6742 did_syncbind = TRUE;
6743 checkpcmark();
6744 if (old_linenr != curwin->w_cursor.lnum)
6745 {
6746 char_u ctrl_o[2];
6747
6748 ctrl_o[0] = Ctrl_O;
6749 ctrl_o[1] = 0;
6750 ins_typebuf(ctrl_o, REMAP_NONE, 0, TRUE, FALSE);
6751 }
6752 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006753}
6754
6755
6756 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006757ex_read(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006758{
Bram Moolenaardf177f62005-02-22 08:39:57 +00006759 int i;
6760 int empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
6761 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006762
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006763 if (eap->usefilter) // :r!cmd
Bram Moolenaar071d4272004-06-13 20:20:40 +00006764 do_bang(1, eap, FALSE, FALSE, TRUE);
6765 else
6766 {
6767 if (u_save(eap->line2, (linenr_T)(eap->line2 + 1)) == FAIL)
6768 return;
6769
6770#ifdef FEAT_BROWSE
6771 if (cmdmod.browse)
6772 {
6773 char_u *browseFile;
6774
Bram Moolenaar7171abe2004-10-11 10:06:20 +00006775 browseFile = do_browse(0, (char_u *)_("Append File"), eap->arg,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006776 NULL, NULL, NULL, curbuf);
6777 if (browseFile != NULL)
6778 {
6779 i = readfile(browseFile, NULL,
6780 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
6781 vim_free(browseFile);
6782 }
6783 else
6784 i = OK;
6785 }
6786 else
6787#endif
6788 if (*eap->arg == NUL)
6789 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006790 if (check_fname() == FAIL) // check for no file name
Bram Moolenaar071d4272004-06-13 20:20:40 +00006791 return;
6792 i = readfile(curbuf->b_ffname, curbuf->b_fname,
6793 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
6794 }
6795 else
6796 {
6797 if (vim_strchr(p_cpo, CPO_ALTREAD) != NULL)
6798 (void)setaltfname(eap->arg, eap->arg, (linenr_T)1);
6799 i = readfile(eap->arg, NULL,
6800 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
6801
6802 }
Bram Moolenaare13b9af2017-01-13 22:01:02 +01006803 if (i != OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006804 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006805#if defined(FEAT_EVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006806 if (!aborting())
6807#endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006808 semsg(_(e_notopen), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006809 }
6810 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00006811 {
6812 if (empty && exmode_active)
6813 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006814 // Delete the empty line that remains. Historically ex does
6815 // this but vi doesn't.
Bram Moolenaardf177f62005-02-22 08:39:57 +00006816 if (eap->line2 == 0)
6817 lnum = curbuf->b_ml.ml_line_count;
6818 else
6819 lnum = 1;
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00006820 if (*ml_get(lnum) == NUL && u_savedel(lnum, 1L) == OK)
Bram Moolenaardf177f62005-02-22 08:39:57 +00006821 {
Bram Moolenaarca70c072020-05-30 20:30:46 +02006822 ml_delete(lnum);
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00006823 if (curwin->w_cursor.lnum > 1
6824 && curwin->w_cursor.lnum >= lnum)
Bram Moolenaardf177f62005-02-22 08:39:57 +00006825 --curwin->w_cursor.lnum;
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00006826 deleted_lines_mark(lnum, 1L);
Bram Moolenaardf177f62005-02-22 08:39:57 +00006827 }
6828 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006829 redraw_curbuf_later(VALID);
Bram Moolenaardf177f62005-02-22 08:39:57 +00006830 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006831 }
6832}
6833
Bram Moolenaarea408852005-06-25 22:49:46 +00006834static char_u *prev_dir = NULL;
6835
6836#if defined(EXITFREE) || defined(PROTO)
6837 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006838free_cd_dir(void)
Bram Moolenaarea408852005-06-25 22:49:46 +00006839{
Bram Moolenaard23a8232018-02-10 18:45:26 +01006840 VIM_CLEAR(prev_dir);
6841 VIM_CLEAR(globaldir);
Bram Moolenaarea408852005-06-25 22:49:46 +00006842}
6843#endif
6844
Bram Moolenaarf4258302013-06-02 18:20:17 +02006845/*
Bram Moolenaar002bc792020-06-05 22:33:42 +02006846 * Get the previous directory for the given chdir scope.
6847 */
6848 static char_u *
6849get_prevdir(cdscope_T scope)
6850{
6851 if (scope == CDSCOPE_WINDOW)
6852 return curwin->w_prevdir;
6853 else if (scope == CDSCOPE_TABPAGE)
6854 return curtab->tp_prevdir;
6855 return prev_dir;
6856}
6857
6858/*
Bram Moolenaarf4258302013-06-02 18:20:17 +02006859 * Deal with the side effects of changing the current directory.
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006860 * When 'scope' is CDSCOPE_TABPAGE then this was after an ":tcd" command.
6861 * When 'scope' is CDSCOPE_WINDOW then this was after an ":lcd" command.
Bram Moolenaarf4258302013-06-02 18:20:17 +02006862 */
6863 void
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006864post_chdir(cdscope_T scope)
Bram Moolenaarf4258302013-06-02 18:20:17 +02006865{
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006866 if (scope != CDSCOPE_WINDOW)
Bram Moolenaar00aa0692019-04-27 20:37:57 +02006867 // Clear tab local directory for both :cd and :tcd
6868 VIM_CLEAR(curtab->tp_localdir);
Bram Moolenaard23a8232018-02-10 18:45:26 +01006869 VIM_CLEAR(curwin->w_localdir);
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006870 if (scope != CDSCOPE_GLOBAL)
Bram Moolenaarf4258302013-06-02 18:20:17 +02006871 {
Bram Moolenaar002bc792020-06-05 22:33:42 +02006872 char_u *pdir = get_prevdir(scope);
6873
6874 // If still in the global directory, need to remember current
6875 // directory as the global directory.
6876 if (globaldir == NULL && pdir != NULL)
6877 globaldir = vim_strsave(pdir);
6878
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006879 // Remember this local directory for the window.
Bram Moolenaarf4258302013-06-02 18:20:17 +02006880 if (mch_dirname(NameBuff, MAXPATHL) == OK)
Bram Moolenaar00aa0692019-04-27 20:37:57 +02006881 {
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006882 if (scope == CDSCOPE_TABPAGE)
Bram Moolenaar00aa0692019-04-27 20:37:57 +02006883 curtab->tp_localdir = vim_strsave(NameBuff);
6884 else
6885 curwin->w_localdir = vim_strsave(NameBuff);
6886 }
Bram Moolenaarf4258302013-06-02 18:20:17 +02006887 }
6888 else
6889 {
Bram Moolenaar002bc792020-06-05 22:33:42 +02006890 // We are now in the global directory, no need to remember its name.
Bram Moolenaard23a8232018-02-10 18:45:26 +01006891 VIM_CLEAR(globaldir);
Bram Moolenaarf4258302013-06-02 18:20:17 +02006892 }
6893
6894 shorten_fnames(TRUE);
6895}
6896
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006897/*
6898 * Change directory function used by :cd/:tcd/:lcd Ex commands and the
Bram Moolenaar95058722020-06-01 16:26:19 +02006899 * chdir() function.
6900 * scope == CDSCOPE_WINDOW: changes the window-local directory
6901 * scope == CDSCOPE_TABPAGE: changes the tab-local directory
6902 * Otherwise: changes the global directory
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006903 * Returns TRUE if the directory is successfully changed.
6904 */
6905 int
6906changedir_func(
6907 char_u *new_dir,
6908 int forceit,
6909 cdscope_T scope)
6910{
6911 char_u *tofree;
Bram Moolenaar002bc792020-06-05 22:33:42 +02006912 char_u *pdir = NULL;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006913 int dir_differs;
6914 int retval = FALSE;
6915
Bram Moolenaar7cc96922020-01-31 22:41:38 +01006916 if (new_dir == NULL || allbuf_locked())
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006917 return FALSE;
6918
6919 if (vim_strchr(p_cpo, CPO_CHDIR) != NULL && curbufIsChanged() && !forceit)
6920 {
6921 emsg(_("E747: Cannot change directory, buffer is modified (add ! to override)"));
6922 return FALSE;
6923 }
6924
6925 // ":cd -": Change to previous directory
6926 if (STRCMP(new_dir, "-") == 0)
6927 {
Bram Moolenaar002bc792020-06-05 22:33:42 +02006928 pdir = get_prevdir(scope);
6929 if (pdir == NULL)
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006930 {
6931 emsg(_("E186: No previous directory"));
6932 return FALSE;
6933 }
Bram Moolenaar002bc792020-06-05 22:33:42 +02006934 new_dir = pdir;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006935 }
6936
Bram Moolenaar002bc792020-06-05 22:33:42 +02006937 // Free the previous directory
6938 tofree = get_prevdir(scope);
6939
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006940 // Save current directory for next ":cd -"
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006941 if (mch_dirname(NameBuff, MAXPATHL) == OK)
Bram Moolenaar002bc792020-06-05 22:33:42 +02006942 pdir = vim_strsave(NameBuff);
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006943 else
Bram Moolenaar002bc792020-06-05 22:33:42 +02006944 pdir = NULL;
6945 if (scope == CDSCOPE_WINDOW)
6946 curwin->w_prevdir = pdir;
6947 else if (scope == CDSCOPE_TABPAGE)
6948 curtab->tp_prevdir = pdir;
6949 else
6950 prev_dir = pdir;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006951
6952#if defined(UNIX) || defined(VMS)
6953 // for UNIX ":cd" means: go to home directory
6954 if (*new_dir == NUL)
6955 {
6956 // use NameBuff for home directory name
6957# ifdef VMS
6958 char_u *p;
6959
6960 p = mch_getenv((char_u *)"SYS$LOGIN");
6961 if (p == NULL || *p == NUL) // empty is the same as not set
6962 NameBuff[0] = NUL;
6963 else
6964 vim_strncpy(NameBuff, p, MAXPATHL - 1);
6965# else
6966 expand_env((char_u *)"$HOME", NameBuff, MAXPATHL);
6967# endif
6968 new_dir = NameBuff;
6969 }
6970#endif
Bram Moolenaar002bc792020-06-05 22:33:42 +02006971 dir_differs = new_dir == NULL || pdir == NULL
6972 || pathcmp((char *)pdir, (char *)new_dir, -1) != 0;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006973 if (new_dir == NULL || (dir_differs && vim_chdir(new_dir)))
6974 emsg(_(e_failed));
6975 else
6976 {
6977 char_u *acmd_fname;
6978
6979 post_chdir(scope);
6980
6981 if (dir_differs)
6982 {
6983 if (scope == CDSCOPE_WINDOW)
6984 acmd_fname = (char_u *)"window";
6985 else if (scope == CDSCOPE_TABPAGE)
6986 acmd_fname = (char_u *)"tabpage";
6987 else
6988 acmd_fname = (char_u *)"global";
6989 apply_autocmds(EVENT_DIRCHANGED, acmd_fname, new_dir, FALSE,
6990 curbuf);
6991 }
6992 retval = TRUE;
6993 }
6994 vim_free(tofree);
6995
6996 return retval;
6997}
Bram Moolenaarea408852005-06-25 22:49:46 +00006998
Bram Moolenaar071d4272004-06-13 20:20:40 +00006999/*
Bram Moolenaar00aa0692019-04-27 20:37:57 +02007000 * ":cd", ":tcd", ":lcd", ":chdir" ":tchdir" and ":lchdir".
Bram Moolenaar071d4272004-06-13 20:20:40 +00007001 */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00007002 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007003ex_cd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007004{
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01007005 char_u *new_dir;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007006
7007 new_dir = eap->arg;
7008#if !defined(UNIX) && !defined(VMS)
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007009 // for non-UNIX ":cd" means: print current directory
Bram Moolenaar071d4272004-06-13 20:20:40 +00007010 if (*new_dir == NUL)
7011 ex_pwd(NULL);
7012 else
7013#endif
7014 {
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007015 cdscope_T scope = CDSCOPE_GLOBAL;
7016
7017 if (eap->cmdidx == CMD_lcd || eap->cmdidx == CMD_lchdir)
7018 scope = CDSCOPE_WINDOW;
7019 else if (eap->cmdidx == CMD_tcd || eap->cmdidx == CMD_tchdir)
7020 scope = CDSCOPE_TABPAGE;
7021
7022 if (changedir_func(new_dir, eap->forceit, scope))
Bram Moolenaardf177f62005-02-22 08:39:57 +00007023 {
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007024 // Echo the new current directory if the command was typed.
Bram Moolenaarfcfbc672009-07-09 18:13:49 +00007025 if (KeyTyped || p_verbose >= 5)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007026 ex_pwd(eap);
7027 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007028 }
7029}
7030
7031/*
7032 * ":pwd".
7033 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007034 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007035ex_pwd(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007036{
7037 if (mch_dirname(NameBuff, MAXPATHL) == OK)
7038 {
7039#ifdef BACKSLASH_IN_FILENAME
7040 slash_adjust(NameBuff);
7041#endif
Bram Moolenaar95058722020-06-01 16:26:19 +02007042 if (p_verbose > 0)
7043 {
7044 char *context = "global";
7045
7046 if (curwin->w_localdir != NULL)
7047 context = "window";
7048 else if (curtab->tp_localdir != NULL)
7049 context = "tabpage";
7050 smsg("[%s] %s", context, (char *)NameBuff);
7051 }
7052 else
7053 msg((char *)NameBuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007054 }
7055 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007056 emsg(_("E187: Unknown"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007057}
7058
7059/*
7060 * ":=".
7061 */
7062 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007063ex_equal(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007064{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007065 smsg("%ld", (long)eap->line2);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007066 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007067}
7068
7069 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007070ex_sleep(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007071{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007072 int n;
7073 long len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007074
7075 if (cursor_valid())
7076 {
7077 n = W_WINROW(curwin) + curwin->w_wrow - msg_scrolled;
7078 if (n >= 0)
Bram Moolenaar53f81742017-09-22 14:35:51 +02007079 windgoto((int)n, curwin->w_wincol + curwin->w_wcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007080 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007081
7082 len = eap->line2;
7083 switch (*eap->arg)
7084 {
7085 case 'm': break;
7086 case NUL: len *= 1000L; break;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007087 default: semsg(_(e_invarg2), eap->arg); return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007088 }
7089 do_sleep(len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007090}
7091
7092/*
7093 * Sleep for "msec" milliseconds, but keep checking for a CTRL-C every second.
7094 */
7095 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007096do_sleep(long msec)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007097{
Bram Moolenaar52953192019-08-16 10:27:13 +02007098 long done = 0;
Bram Moolenaar975b5272016-03-15 23:10:59 +01007099 long wait_now;
Bram Moolenaar52953192019-08-16 10:27:13 +02007100# ifdef ELAPSED_FUNC
7101 elapsed_T start_tv;
7102
7103 // Remember at what time we started, so that we know how much longer we
7104 // should wait after waiting for a bit.
7105 ELAPSED_INIT(start_tv);
7106# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007107
7108 cursor_on();
Bram Moolenaarf45d7472018-09-30 18:22:26 +02007109 out_flush_cursor(FALSE, FALSE);
Bram Moolenaar52953192019-08-16 10:27:13 +02007110 while (!got_int && done < msec)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007111 {
Bram Moolenaar975b5272016-03-15 23:10:59 +01007112 wait_now = msec - done > 1000L ? 1000L : msec - done;
7113#ifdef FEAT_TIMERS
7114 {
7115 long due_time = check_due_timer();
7116
7117 if (due_time > 0 && due_time < wait_now)
7118 wait_now = due_time;
7119 }
7120#endif
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02007121#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar28e67e02019-08-15 23:05:49 +02007122 if (has_any_channel() && wait_now > 20L)
7123 wait_now = 20L;
7124#endif
7125#ifdef FEAT_SOUND
7126 if (has_any_sound_callback() && wait_now > 20L)
7127 wait_now = 20L;
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02007128#endif
Bram Moolenaar975b5272016-03-15 23:10:59 +01007129 ui_delay(wait_now, TRUE);
Bram Moolenaar52953192019-08-16 10:27:13 +02007130
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02007131#ifdef FEAT_JOB_CHANNEL
7132 if (has_any_channel())
7133 ui_breakcheck_force(TRUE);
7134 else
7135#endif
7136 ui_breakcheck();
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007137#ifdef MESSAGE_QUEUE
Bram Moolenaar52953192019-08-16 10:27:13 +02007138 // Process the netbeans and clientserver messages that may have been
7139 // received in the call to ui_breakcheck() when the GUI is in use. This
7140 // may occur when running a test case.
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007141 parse_queued_messages();
Bram Moolenaare3cc6d42011-10-20 21:58:34 +02007142#endif
Bram Moolenaar52953192019-08-16 10:27:13 +02007143
7144# ifdef ELAPSED_FUNC
7145 // actual time passed
7146 done = ELAPSED_FUNC(start_tv);
7147# else
7148 // guestimate time passed (will actually be more)
7149 done += wait_now;
7150# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007151 }
Bram Moolenaar1ebff3d2018-07-07 16:18:13 +02007152
7153 // If CTRL-C was typed to interrupt the sleep, drop the CTRL-C from the
7154 // input buffer, otherwise a following call to input() fails.
7155 if (got_int)
7156 (void)vpeekc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007157}
7158
Bram Moolenaar071d4272004-06-13 20:20:40 +00007159/*
7160 * ":winsize" command (obsolete).
7161 */
7162 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007163ex_winsize(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007164{
7165 int w, h;
7166 char_u *arg = eap->arg;
7167 char_u *p;
7168
7169 w = getdigits(&arg);
7170 arg = skipwhite(arg);
7171 p = arg;
7172 h = getdigits(&arg);
7173 if (*p != NUL && *arg == NUL)
7174 set_shellsize(w, h, TRUE);
7175 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007176 emsg(_("E465: :winsize requires two number arguments"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007177}
7178
Bram Moolenaar071d4272004-06-13 20:20:40 +00007179 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007180ex_wincmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007181{
7182 int xchar = NUL;
7183 char_u *p;
7184
7185 if (*eap->arg == 'g' || *eap->arg == Ctrl_G)
7186 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007187 // CTRL-W g and CTRL-W CTRL-G have an extra command character
Bram Moolenaar071d4272004-06-13 20:20:40 +00007188 if (eap->arg[1] == NUL)
7189 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007190 emsg(_(e_invarg));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007191 return;
7192 }
7193 xchar = eap->arg[1];
7194 p = eap->arg + 2;
7195 }
7196 else
7197 p = eap->arg + 1;
7198
7199 eap->nextcmd = check_nextcmd(p);
7200 p = skipwhite(p);
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02007201 if (*p != NUL && *p != (
7202#ifdef FEAT_EVAL
7203 in_vim9script() ? '#' :
7204#endif
7205 '"')
7206 && eap->nextcmd == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007207 emsg(_(e_invarg));
Bram Moolenaar12bde492011-06-13 01:19:56 +02007208 else if (!eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007209 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007210 // Pass flags on for ":vertical wincmd ]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00007211 postponed_split_flags = cmdmod.split;
Bram Moolenaard326ce82007-03-11 14:48:29 +00007212 postponed_split_tab = cmdmod.tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007213 do_window(*eap->arg, eap->addr_count > 0 ? eap->line2 : 0L, xchar);
7214 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00007215 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007216 }
7217}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007218
Bram Moolenaar843ee412004-06-30 16:16:41 +00007219#if defined(FEAT_GUI) || defined(UNIX) || defined(VMS) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007220/*
7221 * ":winpos".
7222 */
7223 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007224ex_winpos(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007225{
7226 int x, y;
7227 char_u *arg = eap->arg;
7228 char_u *p;
7229
7230 if (*arg == NUL)
7231 {
Bram Moolenaar843ee412004-06-30 16:16:41 +00007232# if defined(FEAT_GUI) || defined(MSWIN)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007233# ifdef VIMDLL
7234 if (gui.in_use ? gui_mch_get_winpos(&x, &y) != FAIL :
7235 mch_get_winpos(&x, &y) != FAIL)
7236# elif defined(FEAT_GUI)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007237 if (gui.in_use && gui_mch_get_winpos(&x, &y) != FAIL)
Bram Moolenaar843ee412004-06-30 16:16:41 +00007238# else
7239 if (mch_get_winpos(&x, &y) != FAIL)
7240# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007241 {
7242 sprintf((char *)IObuff, _("Window position: X %d, Y %d"), x, y);
Bram Moolenaar32526b32019-01-19 17:43:09 +01007243 msg((char *)IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007244 }
7245 else
7246# endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007247 emsg(_("E188: Obtaining window position not implemented for this platform"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007248 }
7249 else
7250 {
7251 x = getdigits(&arg);
7252 arg = skipwhite(arg);
7253 p = arg;
7254 y = getdigits(&arg);
7255 if (*p == NUL || *arg != NUL)
7256 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007257 emsg(_("E466: :winpos requires two number arguments"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007258 return;
7259 }
7260# ifdef FEAT_GUI
7261 if (gui.in_use)
7262 gui_mch_set_winpos(x, y);
7263 else if (gui.starting)
7264 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007265 // Remember the coordinates for when the window is opened.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007266 gui_win_x = x;
7267 gui_win_y = y;
7268 }
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007269# if defined(HAVE_TGETENT) || defined(VIMDLL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007270 else
7271# endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007272# endif
7273# if defined(MSWIN) && (!defined(FEAT_GUI) || defined(VIMDLL))
Bram Moolenaar843ee412004-06-30 16:16:41 +00007274 mch_set_winpos(x, y);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007275# endif
7276# ifdef HAVE_TGETENT
7277 if (*T_CWP)
7278 term_set_winpos(x, y);
7279# endif
7280 }
7281}
7282#endif
7283
7284/*
7285 * Handle command that work like operators: ":delete", ":yank", ":>" and ":<".
7286 */
7287 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007288ex_operators(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007289{
7290 oparg_T oa;
7291
7292 clear_oparg(&oa);
7293 oa.regname = eap->regname;
7294 oa.start.lnum = eap->line1;
7295 oa.end.lnum = eap->line2;
7296 oa.line_count = eap->line2 - eap->line1 + 1;
7297 oa.motion_type = MLINE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007298 virtual_op = FALSE;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007299 if (eap->cmdidx != CMD_yank) // position cursor for undo
Bram Moolenaar071d4272004-06-13 20:20:40 +00007300 {
7301 setpcmark();
7302 curwin->w_cursor.lnum = eap->line1;
7303 beginline(BL_SOL | BL_FIX);
7304 }
7305
Bram Moolenaard07c6e12013-11-21 14:21:40 +01007306 if (VIsual_active)
7307 end_visual_mode();
Bram Moolenaard07c6e12013-11-21 14:21:40 +01007308
Bram Moolenaar071d4272004-06-13 20:20:40 +00007309 switch (eap->cmdidx)
7310 {
7311 case CMD_delete:
7312 oa.op_type = OP_DELETE;
7313 op_delete(&oa);
7314 break;
7315
7316 case CMD_yank:
7317 oa.op_type = OP_YANK;
7318 (void)op_yank(&oa, FALSE, TRUE);
7319 break;
7320
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007321 default: // CMD_rshift or CMD_lshift
Bram Moolenaar6e707362013-06-14 19:15:58 +02007322 if (
Bram Moolenaar071d4272004-06-13 20:20:40 +00007323#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6e707362013-06-14 19:15:58 +02007324 (eap->cmdidx == CMD_rshift) ^ curwin->w_p_rl
7325#else
7326 eap->cmdidx == CMD_rshift
Bram Moolenaar071d4272004-06-13 20:20:40 +00007327#endif
Bram Moolenaar6e707362013-06-14 19:15:58 +02007328 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00007329 oa.op_type = OP_RSHIFT;
7330 else
7331 oa.op_type = OP_LSHIFT;
7332 op_shift(&oa, FALSE, eap->amount);
7333 break;
7334 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007335 virtual_op = MAYBE;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007336 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007337}
7338
7339/*
7340 * ":put".
7341 */
7342 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007343ex_put(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007344{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007345 // ":0put" works like ":1put!".
Bram Moolenaar071d4272004-06-13 20:20:40 +00007346 if (eap->line2 == 0)
7347 {
7348 eap->line2 = 1;
7349 eap->forceit = TRUE;
7350 }
7351 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaarc3516f72020-09-08 22:45:35 +02007352 do_put(eap->regname, NULL, eap->forceit ? BACKWARD : FORWARD, 1L,
Bram Moolenaardf177f62005-02-22 08:39:57 +00007353 PUT_LINE|PUT_CURSLINE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007354}
7355
7356/*
7357 * Handle ":copy" and ":move".
7358 */
7359 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007360ex_copymove(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007361{
7362 long n;
7363
Bram Moolenaar491799b2020-08-01 19:23:43 +02007364#ifdef FEAT_EVAL
Bram Moolenaarf5a48012020-08-01 17:00:03 +02007365 if (not_in_vim9(eap) == FAIL)
7366 return;
Bram Moolenaar491799b2020-08-01 19:23:43 +02007367#endif
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02007368 n = get_address(eap, &eap->arg, eap->addr_type, FALSE, FALSE, FALSE, 1);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007369 if (eap->arg == NULL) // error detected
Bram Moolenaar071d4272004-06-13 20:20:40 +00007370 {
7371 eap->nextcmd = NULL;
7372 return;
7373 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00007374 get_flags(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007375
7376 /*
7377 * move or copy lines from 'eap->line1'-'eap->line2' to below line 'n'
7378 */
7379 if (n == MAXLNUM || n < 0 || n > curbuf->b_ml.ml_line_count)
7380 {
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02007381 emsg(_(e_invrange));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007382 return;
7383 }
7384
7385 if (eap->cmdidx == CMD_move)
7386 {
7387 if (do_move(eap->line1, eap->line2, n) == FAIL)
7388 return;
7389 }
7390 else
7391 ex_copy(eap->line1, eap->line2, n);
7392 u_clearline();
7393 beginline(BL_SOL | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007394 ex_may_print(eap);
7395}
7396
7397/*
7398 * Print the current line if flags were given to the Ex command.
7399 */
Bram Moolenaarfd3fe982014-04-01 17:49:44 +02007400 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007401ex_may_print(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007402{
7403 if (eap->flags != 0)
7404 {
7405 print_line(curwin->w_cursor.lnum, (eap->flags & EXFLAG_NR),
7406 (eap->flags & EXFLAG_LIST));
7407 ex_no_reprint = TRUE;
7408 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007409}
7410
7411/*
7412 * ":smagic" and ":snomagic".
7413 */
7414 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007415ex_submagic(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007416{
7417 int magic_save = p_magic;
7418
7419 p_magic = (eap->cmdidx == CMD_smagic);
Bram Moolenaar66e00142020-08-12 21:58:12 +02007420 ex_substitute(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007421 p_magic = magic_save;
7422}
7423
7424/*
7425 * ":join".
7426 */
7427 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007428ex_join(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007429{
7430 curwin->w_cursor.lnum = eap->line1;
7431 if (eap->line1 == eap->line2)
7432 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007433 if (eap->addr_count >= 2) // :2,2join does nothing
Bram Moolenaar071d4272004-06-13 20:20:40 +00007434 return;
7435 if (eap->line2 == curbuf->b_ml.ml_line_count)
7436 {
7437 beep_flush();
7438 return;
7439 }
7440 ++eap->line2;
7441 }
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02007442 (void)do_join(eap->line2 - eap->line1 + 1, !eap->forceit, TRUE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007443 beginline(BL_WHITE | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007444 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007445}
7446
7447/*
7448 * ":[addr]@r" or ":[addr]*r": execute register
7449 */
7450 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007451ex_at(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007452{
7453 int c;
Bram Moolenaar60462872009-11-03 11:40:19 +00007454 int prev_len = typebuf.tb_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007455
7456 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaar4930a762016-09-11 14:39:53 +02007457 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007458
7459#ifdef USE_ON_FLY_SCROLL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007460 dont_scroll = TRUE; // disallow scrolling here
Bram Moolenaar071d4272004-06-13 20:20:40 +00007461#endif
7462
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007463 // get the register name. No name means to use the previous one
Bram Moolenaar071d4272004-06-13 20:20:40 +00007464 c = *eap->arg;
7465 if (c == NUL || (c == '*' && *eap->cmd == '*'))
7466 c = '@';
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007467 // Put the register in the typeahead buffer with the "silent" flag.
Bram Moolenaard333d1e2006-11-07 17:43:47 +00007468 if (do_execreg(c, TRUE, vim_strchr(p_cpo, CPO_EXECBUF) != NULL, TRUE)
7469 == FAIL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007470 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007471 beep_flush();
Bram Moolenaardf177f62005-02-22 08:39:57 +00007472 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007473 else
7474 {
7475 int save_efr = exec_from_reg;
7476
7477 exec_from_reg = TRUE;
7478
7479 /*
7480 * Execute from the typeahead buffer.
Bram Moolenaar60462872009-11-03 11:40:19 +00007481 * Continue until the stuff buffer is empty and all added characters
7482 * have been consumed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007483 */
Bram Moolenaar60462872009-11-03 11:40:19 +00007484 while (!stuff_empty() || typebuf.tb_len > prev_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007485 (void)do_cmdline(NULL, getexline, NULL, DOCMD_NOWAIT|DOCMD_VERBOSE);
7486
7487 exec_from_reg = save_efr;
7488 }
7489}
7490
7491/*
7492 * ":!".
7493 */
7494 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007495ex_bang(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007496{
7497 do_bang(eap->addr_count, eap, eap->forceit, TRUE, TRUE);
7498}
7499
7500/*
7501 * ":undo".
7502 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007503 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +01007504ex_undo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007505{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007506 if (eap->addr_count == 1) // :undo 123
Bram Moolenaar730cde92010-06-27 05:18:54 +02007507 undo_time(eap->line2, FALSE, FALSE, TRUE);
Bram Moolenaard3667a22006-03-16 21:35:52 +00007508 else
7509 u_undo(1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007510}
7511
Bram Moolenaar55debbe2010-05-23 23:34:36 +02007512#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar6df6f472010-07-18 18:04:50 +02007513 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007514ex_wundo(exarg_T *eap)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02007515{
7516 char_u hash[UNDO_HASH_SIZE];
7517
7518 u_compute_hash(hash);
7519 u_write_undo(eap->arg, eap->forceit, curbuf, hash);
7520}
7521
Bram Moolenaar6df6f472010-07-18 18:04:50 +02007522 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007523ex_rundo(exarg_T *eap)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02007524{
7525 char_u hash[UNDO_HASH_SIZE];
7526
7527 u_compute_hash(hash);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02007528 u_read_undo(eap->arg, hash, NULL);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02007529}
7530#endif
7531
Bram Moolenaar071d4272004-06-13 20:20:40 +00007532/*
7533 * ":redo".
7534 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007535 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007536ex_redo(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007537{
7538 u_redo(1);
7539}
7540
7541/*
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007542 * ":earlier" and ":later".
7543 */
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007544 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007545ex_later(exarg_T *eap)
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007546{
7547 long count = 0;
7548 int sec = FALSE;
Bram Moolenaar730cde92010-06-27 05:18:54 +02007549 int file = FALSE;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007550 char_u *p = eap->arg;
7551
7552 if (*p == NUL)
7553 count = 1;
7554 else if (isdigit(*p))
7555 {
7556 count = getdigits(&p);
7557 switch (*p)
7558 {
7559 case 's': ++p; sec = TRUE; break;
7560 case 'm': ++p; sec = TRUE; count *= 60; break;
7561 case 'h': ++p; sec = TRUE; count *= 60 * 60; break;
Bram Moolenaar730cde92010-06-27 05:18:54 +02007562 case 'd': ++p; sec = TRUE; count *= 24 * 60 * 60; break;
7563 case 'f': ++p; file = TRUE; break;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007564 }
7565 }
7566
7567 if (*p != NUL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007568 semsg(_(e_invarg2), eap->arg);
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007569 else
Bram Moolenaar730cde92010-06-27 05:18:54 +02007570 undo_time(eap->cmdidx == CMD_earlier ? -count : count,
7571 sec, file, FALSE);
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007572}
7573
7574/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007575 * ":redir": start/stop redirection.
7576 */
7577 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007578ex_redir(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007579{
7580 char *mode;
7581 char_u *fname;
Bram Moolenaarca472992005-01-21 11:46:23 +00007582 char_u *arg = eap->arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007583
Bram Moolenaarba768492016-07-08 15:32:54 +02007584#ifdef FEAT_EVAL
Bram Moolenaar79815f12016-07-09 17:07:29 +02007585 if (redir_execute)
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02007586 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007587 emsg(_("E930: Cannot use :redir inside execute()"));
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02007588 return;
7589 }
Bram Moolenaarba768492016-07-08 15:32:54 +02007590#endif
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02007591
Bram Moolenaar071d4272004-06-13 20:20:40 +00007592 if (STRICMP(eap->arg, "END") == 0)
7593 close_redir();
7594 else
7595 {
Bram Moolenaarca472992005-01-21 11:46:23 +00007596 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00007597 {
Bram Moolenaarca472992005-01-21 11:46:23 +00007598 ++arg;
7599 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00007600 {
Bram Moolenaarca472992005-01-21 11:46:23 +00007601 ++arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007602 mode = "a";
7603 }
7604 else
7605 mode = "w";
Bram Moolenaarca472992005-01-21 11:46:23 +00007606 arg = skipwhite(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007607
7608 close_redir();
7609
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007610 // Expand environment variables and "~/".
Bram Moolenaarca472992005-01-21 11:46:23 +00007611 fname = expand_env_save(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007612 if (fname == NULL)
7613 return;
7614#ifdef FEAT_BROWSE
7615 if (cmdmod.browse)
7616 {
7617 char_u *browseFile;
7618
Bram Moolenaar7171abe2004-10-11 10:06:20 +00007619 browseFile = do_browse(BROWSE_SAVE,
7620 (char_u *)_("Save Redirection"),
Bram Moolenaarc36651b2018-04-29 12:22:56 +02007621 fname, NULL, NULL,
7622 (char_u *)_(BROWSE_FILTER_ALL_FILES), curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007623 if (browseFile == NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007624 return; // operation cancelled
Bram Moolenaar071d4272004-06-13 20:20:40 +00007625 vim_free(fname);
7626 fname = browseFile;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007627 eap->forceit = TRUE; // since dialog already asked
Bram Moolenaar071d4272004-06-13 20:20:40 +00007628 }
7629#endif
7630
7631 redir_fd = open_exfile(fname, eap->forceit, mode);
7632 vim_free(fname);
7633 }
7634#ifdef FEAT_EVAL
Bram Moolenaarca472992005-01-21 11:46:23 +00007635 else if (*arg == '@')
Bram Moolenaar071d4272004-06-13 20:20:40 +00007636 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007637 // redirect to a register a-z (resp. A-Z for appending)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007638 close_redir();
Bram Moolenaarca472992005-01-21 11:46:23 +00007639 ++arg;
7640 if (ASCII_ISALPHA(*arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007641# ifdef FEAT_CLIPBOARD
Bram Moolenaarca472992005-01-21 11:46:23 +00007642 || *arg == '*'
Bram Moolenaar9a51c6e2006-11-14 19:25:02 +00007643 || *arg == '+'
Bram Moolenaar071d4272004-06-13 20:20:40 +00007644# endif
Bram Moolenaarca472992005-01-21 11:46:23 +00007645 || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00007646 {
Bram Moolenaarca472992005-01-21 11:46:23 +00007647 redir_reg = *arg++;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007648 if (*arg == '>' && arg[1] == '>') // append
Bram Moolenaard9d30582005-05-18 22:10:28 +00007649 arg += 2;
Bram Moolenaarc188b882007-10-19 14:20:54 +00007650 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007651 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007652 // Can use both "@a" and "@a>".
Bram Moolenaar2c29bee2005-06-01 21:46:07 +00007653 if (*arg == '>')
7654 arg++;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007655 // Make register empty when not using @A-@Z and the
7656 // command is valid.
Bram Moolenaarc188b882007-10-19 14:20:54 +00007657 if (*arg == NUL && !isupper(redir_reg))
7658 write_reg_contents(redir_reg, (char_u *)"", -1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007659 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00007660 }
7661 if (*arg != NUL)
7662 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00007663 redir_reg = 0;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007664 semsg(_(e_invarg2), eap->arg);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007665 }
7666 }
7667 else if (*arg == '=' && arg[1] == '>')
7668 {
7669 int append;
7670
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007671 // redirect to a variable
Bram Moolenaardf177f62005-02-22 08:39:57 +00007672 close_redir();
7673 arg += 2;
7674
7675 if (*arg == '>')
7676 {
7677 ++arg;
7678 append = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007679 }
7680 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00007681 append = FALSE;
7682
7683 if (var_redir_start(skipwhite(arg), append) == OK)
7684 redir_vname = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007685 }
7686#endif
7687
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007688 // TODO: redirect to a buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00007689
Bram Moolenaar071d4272004-06-13 20:20:40 +00007690 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007691 semsg(_(e_invarg2), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007692 }
Bram Moolenaar29b2d262006-09-10 19:07:28 +00007693
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007694 // Make sure redirection is not off. Can happen for cmdline completion
7695 // that indirectly invokes a command to catch its output.
Bram Moolenaar29b2d262006-09-10 19:07:28 +00007696 if (redir_fd != NULL
7697#ifdef FEAT_EVAL
7698 || redir_reg || redir_vname
7699#endif
7700 )
7701 redir_off = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007702}
7703
7704/*
7705 * ":redraw": force redraw
7706 */
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01007707 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007708ex_redraw(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007709{
7710 int r = RedrawingDisabled;
7711 int p = p_lz;
7712
7713 RedrawingDisabled = 0;
7714 p_lz = FALSE;
Bram Moolenaarabc39ab2017-03-01 18:04:05 +01007715 validate_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007716 update_topline();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007717 update_screen(eap->forceit ? CLEAR : VIsual_active ? INVERTED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007718#ifdef FEAT_TITLE
7719 if (need_maketitle)
7720 maketitle();
7721#endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007722#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
7723# ifdef VIMDLL
7724 if (!gui.in_use)
7725# endif
7726 resize_console_buf();
Bram Moolenaar78d21da2019-02-17 15:00:52 +01007727#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007728 RedrawingDisabled = r;
7729 p_lz = p;
7730
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007731 // Reset msg_didout, so that a message that's there is overwritten.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007732 msg_didout = FALSE;
7733 msg_col = 0;
7734
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007735 // No need to wait after an intentional redraw.
Bram Moolenaar56667a52013-07-17 11:54:28 +02007736 need_wait_return = FALSE;
7737
Bram Moolenaar071d4272004-06-13 20:20:40 +00007738 out_flush();
7739}
7740
7741/*
7742 * ":redrawstatus": force redraw of status line(s)
7743 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007744 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007745ex_redrawstatus(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007746{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007747 int r = RedrawingDisabled;
7748 int p = p_lz;
7749
7750 RedrawingDisabled = 0;
7751 p_lz = FALSE;
7752 if (eap->forceit)
7753 status_redraw_all();
7754 else
7755 status_redraw_curbuf();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007756 update_screen(VIsual_active ? INVERTED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007757 RedrawingDisabled = r;
7758 p_lz = p;
7759 out_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007760}
7761
Bram Moolenaare12bab32019-01-08 22:02:56 +01007762/*
7763 * ":redrawtabline": force redraw of the tabline
7764 */
7765 static void
7766ex_redrawtabline(exarg_T *eap UNUSED)
7767{
7768 int r = RedrawingDisabled;
7769 int p = p_lz;
7770
7771 RedrawingDisabled = 0;
7772 p_lz = FALSE;
7773
7774 draw_tabline();
7775
7776 RedrawingDisabled = r;
7777 p_lz = p;
7778 out_flush();
7779}
7780
Bram Moolenaar071d4272004-06-13 20:20:40 +00007781 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007782close_redir(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007783{
7784 if (redir_fd != NULL)
7785 {
7786 fclose(redir_fd);
7787 redir_fd = NULL;
7788 }
7789#ifdef FEAT_EVAL
7790 redir_reg = 0;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007791 if (redir_vname)
7792 {
7793 var_redir_stop();
7794 redir_vname = 0;
7795 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007796#endif
7797}
7798
Bram Moolenaarf96ae0b2019-07-28 15:21:55 +02007799#if (defined(FEAT_SESSION) || defined(FEAT_EVAL)) || defined(PROTO)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007800 int
Bram Moolenaarbd67aac2019-09-21 23:09:04 +02007801vim_mkdir_emsg(char_u *name, int prot UNUSED)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007802{
7803 if (vim_mkdir(name, prot) != 0)
7804 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007805 semsg(_("E739: Cannot create directory: %s"), name);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007806 return FAIL;
7807 }
7808 return OK;
7809}
7810#endif
7811
Bram Moolenaar071d4272004-06-13 20:20:40 +00007812/*
7813 * Open a file for writing for an Ex command, with some checks.
7814 * Return file descriptor, or NULL on failure.
7815 */
7816 FILE *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007817open_exfile(
7818 char_u *fname,
7819 int forceit,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007820 char *mode) // "w" for create new file or "a" for append
Bram Moolenaar071d4272004-06-13 20:20:40 +00007821{
7822 FILE *fd;
7823
7824#ifdef UNIX
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007825 // with Unix it is possible to open a directory
Bram Moolenaar071d4272004-06-13 20:20:40 +00007826 if (mch_isdir(fname))
7827 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007828 semsg(_(e_isadir2), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007829 return NULL;
7830 }
7831#endif
7832 if (!forceit && *mode != 'a' && vim_fexists(fname))
7833 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007834 semsg(_("E189: \"%s\" exists (add ! to override)"), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007835 return NULL;
7836 }
7837
7838 if ((fd = mch_fopen((char *)fname, mode)) == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007839 semsg(_("E190: Cannot open \"%s\" for writing"), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007840
7841 return fd;
7842}
7843
7844/*
7845 * ":mark" and ":k".
7846 */
7847 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007848ex_mark(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007849{
7850 pos_T pos;
7851
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007852 if (*eap->arg == NUL) // No argument?
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007853 emsg(_(e_argreq));
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007854 else if (eap->arg[1] != NUL) // more than one character?
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +02007855 semsg(_(e_trailing_arg), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007856 else
7857 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007858 pos = curwin->w_cursor; // save curwin->w_cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00007859 curwin->w_cursor.lnum = eap->line2;
7860 beginline(BL_WHITE | BL_FIX);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007861 if (setmark(*eap->arg) == FAIL) // set mark
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007862 emsg(_("E191: Argument must be a letter or forward/backward quote"));
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007863 curwin->w_cursor = pos; // restore curwin->w_cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00007864 }
7865}
7866
7867/*
7868 * Update w_topline, w_leftcol and the cursor position.
7869 */
7870 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007871update_topline_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007872{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007873 check_cursor(); // put cursor on valid line
Bram Moolenaar071d4272004-06-13 20:20:40 +00007874 update_topline();
7875 if (!curwin->w_p_wrap)
7876 validate_cursor();
7877 update_curswant();
7878}
7879
Bram Moolenaar071d4272004-06-13 20:20:40 +00007880/*
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007881 * Save the current State and go to Normal mode.
7882 * Return TRUE if the typeahead could be saved.
7883 */
7884 int
7885save_current_state(save_state_T *sst)
7886{
7887 sst->save_msg_scroll = msg_scroll;
7888 sst->save_restart_edit = restart_edit;
7889 sst->save_msg_didout = msg_didout;
7890 sst->save_State = State;
7891 sst->save_insertmode = p_im;
7892 sst->save_finish_op = finish_op;
7893 sst->save_opcount = opcount;
Bram Moolenaarcce713d2019-03-04 11:40:12 +01007894 sst->save_reg_executing = reg_executing;
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007895
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007896 msg_scroll = FALSE; // no msg scrolling in Normal mode
7897 restart_edit = 0; // don't go to Insert mode
7898 p_im = FALSE; // don't use 'insertmode'
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007899
7900 /*
7901 * Save the current typeahead. This is required to allow using ":normal"
7902 * from an event handler and makes sure we don't hang when the argument
7903 * ends with half a command.
7904 */
7905 save_typeahead(&sst->tabuf);
7906 return sst->tabuf.typebuf_valid;
7907}
7908
7909 void
7910restore_current_state(save_state_T *sst)
7911{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007912 // Restore the previous typeahead.
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007913 restore_typeahead(&sst->tabuf);
7914
7915 msg_scroll = sst->save_msg_scroll;
7916 restart_edit = sst->save_restart_edit;
7917 p_im = sst->save_insertmode;
7918 finish_op = sst->save_finish_op;
7919 opcount = sst->save_opcount;
Bram Moolenaarcce713d2019-03-04 11:40:12 +01007920 reg_executing = sst->save_reg_executing;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007921 msg_didout |= sst->save_msg_didout; // don't reset msg_didout now
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007922
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007923 // Restore the state (needed when called from a function executed for
7924 // 'indentexpr'). Update the mouse and cursor, they may have changed.
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007925 State = sst->save_State;
7926#ifdef CURSOR_SHAPE
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007927 ui_cursor_shape(); // may show different cursor shape
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007928#endif
7929}
7930
7931/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007932 * ":normal[!] {commands}": Execute normal mode commands.
7933 */
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01007934 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007935ex_normal(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007936{
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007937 save_state_T save_state;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007938 char_u *arg = NULL;
7939 int l;
7940 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007941
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00007942 if (ex_normal_lock > 0)
7943 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007944 emsg(_(e_secure));
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00007945 return;
7946 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007947 if (ex_normal_busy >= p_mmd)
7948 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007949 emsg(_("E192: Recursive use of :normal too deep"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007950 return;
7951 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007952
Bram Moolenaar071d4272004-06-13 20:20:40 +00007953 /*
7954 * vgetc() expects a CSI and K_SPECIAL to have been escaped. Don't do
7955 * this for the K_SPECIAL leading byte, otherwise special keys will not
7956 * work.
7957 */
7958 if (has_mbyte)
7959 {
7960 int len = 0;
7961
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007962 // Count the number of characters to be escaped.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007963 for (p = eap->arg; *p != NUL; ++p)
7964 {
Bram Moolenaar13505972019-01-24 15:04:48 +01007965#ifdef FEAT_GUI
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007966 if (*p == CSI) // leadbyte CSI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007967 len += 2;
Bram Moolenaar13505972019-01-24 15:04:48 +01007968#endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007969 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007970 if (*++p == K_SPECIAL // trailbyte K_SPECIAL or CSI
Bram Moolenaar13505972019-01-24 15:04:48 +01007971#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007972 || *p == CSI
Bram Moolenaar13505972019-01-24 15:04:48 +01007973#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007974 )
7975 len += 2;
7976 }
7977 if (len > 0)
7978 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02007979 arg = alloc(STRLEN(eap->arg) + len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007980 if (arg != NULL)
7981 {
7982 len = 0;
7983 for (p = eap->arg; *p != NUL; ++p)
7984 {
7985 arg[len++] = *p;
Bram Moolenaar13505972019-01-24 15:04:48 +01007986#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007987 if (*p == CSI)
7988 {
7989 arg[len++] = KS_EXTRA;
7990 arg[len++] = (int)KE_CSI;
7991 }
Bram Moolenaar13505972019-01-24 15:04:48 +01007992#endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007993 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007994 {
7995 arg[len++] = *++p;
7996 if (*p == K_SPECIAL)
7997 {
7998 arg[len++] = KS_SPECIAL;
7999 arg[len++] = KE_FILLER;
8000 }
Bram Moolenaar13505972019-01-24 15:04:48 +01008001#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008002 else if (*p == CSI)
8003 {
8004 arg[len++] = KS_EXTRA;
8005 arg[len++] = (int)KE_CSI;
8006 }
Bram Moolenaar13505972019-01-24 15:04:48 +01008007#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008008 }
8009 arg[len] = NUL;
8010 }
8011 }
8012 }
8013 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008014
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008015 ++ex_normal_busy;
8016 if (save_current_state(&save_state))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008017 {
8018 /*
8019 * Repeat the :normal command for each line in the range. When no
8020 * range given, execute it just once, without positioning the cursor
8021 * first.
8022 */
8023 do
8024 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008025 if (eap->addr_count != 0)
8026 {
8027 curwin->w_cursor.lnum = eap->line1++;
8028 curwin->w_cursor.col = 0;
Bram Moolenaard5d37532017-03-27 23:02:07 +02008029 check_cursor_moved(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008030 }
8031
Bram Moolenaar13505972019-01-24 15:04:48 +01008032 exec_normal_cmd(arg != NULL
8033 ? arg
8034 : eap->arg, eap->forceit ? REMAP_NONE : REMAP_YES, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008035 }
8036 while (eap->addr_count > 0 && eap->line1 <= eap->line2 && !got_int);
8037 }
8038
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008039 // Might not return to the main loop when in an event handler.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008040 update_topline_cursor();
8041
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008042 restore_current_state(&save_state);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008043 --ex_normal_busy;
Bram Moolenaareda73602014-11-05 09:53:23 +01008044 setmouse();
Bram Moolenaareda73602014-11-05 09:53:23 +01008045#ifdef CURSOR_SHAPE
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008046 ui_cursor_shape(); // may show different cursor shape
Bram Moolenaareda73602014-11-05 09:53:23 +01008047#endif
8048
Bram Moolenaar071d4272004-06-13 20:20:40 +00008049 vim_free(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008050}
8051
8052/*
Bram Moolenaar64969662005-12-14 21:59:55 +00008053 * ":startinsert", ":startreplace" and ":startgreplace"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008054 */
8055 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008056ex_startinsert(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008057{
Bram Moolenaarfd371682005-01-14 21:42:54 +00008058 if (eap->forceit)
8059 {
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02008060 // cursor line can be zero on startup
Bram Moolenaar09ca9322017-09-26 17:40:45 +02008061 if (!curwin->w_cursor.lnum)
8062 curwin->w_cursor.lnum = 1;
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02008063 set_cursor_for_append_to_line();
Bram Moolenaarfd371682005-01-14 21:42:54 +00008064 }
Bram Moolenaareb58a242020-04-19 18:13:19 +02008065#ifdef FEAT_TERMINAL
8066 // Ignore this when running in an active terminal.
8067 if (term_job_running(curbuf->b_term))
8068 return;
8069#endif
Bram Moolenaarfd371682005-01-14 21:42:54 +00008070
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02008071 // Ignore the command when already in Insert mode. Inserting an
8072 // expression register that invokes a function can do this.
Bram Moolenaara40c5002005-01-09 21:16:21 +00008073 if (State & INSERT)
8074 return;
8075
Bram Moolenaar64969662005-12-14 21:59:55 +00008076 if (eap->cmdidx == CMD_startinsert)
8077 restart_edit = 'a';
8078 else if (eap->cmdidx == CMD_startreplace)
8079 restart_edit = 'R';
Bram Moolenaar071d4272004-06-13 20:20:40 +00008080 else
Bram Moolenaar64969662005-12-14 21:59:55 +00008081 restart_edit = 'V';
8082
8083 if (!eap->forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008084 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00008085 if (eap->cmdidx == CMD_startinsert)
8086 restart_edit = 'i';
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02008087 curwin->w_curswant = 0; // avoid MAXCOL
Bram Moolenaar071d4272004-06-13 20:20:40 +00008088 }
8089}
8090
8091/*
8092 * ":stopinsert"
8093 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008094 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008095ex_stopinsert(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008096{
8097 restart_edit = 0;
8098 stop_insert_mode = TRUE;
Bram Moolenaarfd773e92016-04-02 19:39:16 +02008099 clearmode();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008100}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008101
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008102/*
8103 * Execute normal mode command "cmd".
8104 * "remap" can be REMAP_NONE or REMAP_YES.
8105 */
8106 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008107exec_normal_cmd(char_u *cmd, int remap, int silent)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008108{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008109 // Stuff the argument into the typeahead buffer.
Bram Moolenaar25281632016-01-21 23:32:32 +01008110 ins_typebuf(cmd, remap, 0, TRUE, silent);
Bram Moolenaar586c70c2018-10-02 16:23:58 +02008111 exec_normal(FALSE, FALSE, FALSE);
Bram Moolenaar25281632016-01-21 23:32:32 +01008112}
Bram Moolenaar25281632016-01-21 23:32:32 +01008113
Bram Moolenaar25281632016-01-21 23:32:32 +01008114/*
8115 * Execute normal_cmd() until there is no typeahead left.
Bram Moolenaar586c70c2018-10-02 16:23:58 +02008116 * When "use_vpeekc" is TRUE use vpeekc() to check for available chars.
Bram Moolenaar25281632016-01-21 23:32:32 +01008117 */
8118 void
Bram Moolenaar586c70c2018-10-02 16:23:58 +02008119exec_normal(int was_typed, int use_vpeekc, int may_use_terminal_loop UNUSED)
Bram Moolenaar25281632016-01-21 23:32:32 +01008120{
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008121 oparg_T oa;
Bram Moolenaar905dd902019-04-07 14:21:47 +02008122 int c;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008123
Bram Moolenaar905dd902019-04-07 14:21:47 +02008124 // When calling vpeekc() from feedkeys() it will return Ctrl_C when there
8125 // is nothing to get, so also check for Ctrl_C.
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008126 clear_oparg(&oa);
8127 finish_op = FALSE;
Bram Moolenaar586c70c2018-10-02 16:23:58 +02008128 while ((!stuff_empty()
8129 || ((was_typed || !typebuf_typed()) && typebuf.tb_len > 0)
Bram Moolenaar905dd902019-04-07 14:21:47 +02008130 || (use_vpeekc && (c = vpeekc()) != NUL && c != Ctrl_C))
Bram Moolenaar586c70c2018-10-02 16:23:58 +02008131 && !got_int)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008132 {
8133 update_topline_cursor();
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02008134#ifdef FEAT_TERMINAL
Bram Moolenaar655a82a2018-05-08 22:01:07 +02008135 if (may_use_terminal_loop && term_use_loop()
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02008136 && oa.op_type == OP_NOP && oa.regname == NUL
8137 && !VIsual_active)
8138 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008139 // If terminal_loop() returns OK we got a key that is handled
8140 // in Normal model. With FAIL we first need to position the
8141 // cursor and the screen needs to be redrawn.
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02008142 if (terminal_loop(TRUE) == OK)
8143 normal_cmd(&oa, TRUE);
8144 }
8145 else
8146#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008147 // execute a Normal mode cmd
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02008148 normal_cmd(&oa, TRUE);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008149 }
8150}
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008151
Bram Moolenaar071d4272004-06-13 20:20:40 +00008152#ifdef FEAT_FIND_ID
8153 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008154ex_checkpath(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008155{
8156 find_pattern_in_path(NULL, 0, 0, FALSE, FALSE, CHECK_PATH, 1L,
8157 eap->forceit ? ACTION_SHOW_ALL : ACTION_SHOW,
8158 (linenr_T)1, (linenr_T)MAXLNUM);
8159}
8160
Bram Moolenaar4033c552017-09-16 20:54:51 +02008161#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008162/*
8163 * ":psearch"
8164 */
8165 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008166ex_psearch(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008167{
8168 g_do_tagpreview = p_pvh;
8169 ex_findpat(eap);
8170 g_do_tagpreview = 0;
8171}
8172#endif
8173
8174 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008175ex_findpat(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008176{
8177 int whole = TRUE;
8178 long n;
8179 char_u *p;
8180 int action;
8181
8182 switch (cmdnames[eap->cmdidx].cmd_name[2])
8183 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008184 case 'e': // ":psearch", ":isearch" and ":dsearch"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008185 if (cmdnames[eap->cmdidx].cmd_name[0] == 'p')
8186 action = ACTION_GOTO;
8187 else
8188 action = ACTION_SHOW;
8189 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008190 case 'i': // ":ilist" and ":dlist"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008191 action = ACTION_SHOW_ALL;
8192 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008193 case 'u': // ":ijump" and ":djump"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008194 action = ACTION_GOTO;
8195 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008196 default: // ":isplit" and ":dsplit"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008197 action = ACTION_SPLIT;
8198 break;
8199 }
8200
8201 n = 1;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008202 if (vim_isdigit(*eap->arg)) // get count
Bram Moolenaar071d4272004-06-13 20:20:40 +00008203 {
8204 n = getdigits(&eap->arg);
8205 eap->arg = skipwhite(eap->arg);
8206 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008207 if (*eap->arg == '/') // Match regexp, not just whole words
Bram Moolenaar071d4272004-06-13 20:20:40 +00008208 {
8209 whole = FALSE;
8210 ++eap->arg;
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02008211 p = skip_regexp(eap->arg, '/', p_magic);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008212 if (*p)
8213 {
8214 *p++ = NUL;
8215 p = skipwhite(p);
8216
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008217 // Check for trailing illegal characters
Bram Moolenaarfaac4102020-04-20 17:46:14 +02008218 if (!ends_excmd2(eap->arg, p))
Bram Moolenaar8930caa2020-07-23 16:37:03 +02008219 eap->errmsg = ex_errmsg(e_trailing_arg, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008220 else
8221 eap->nextcmd = check_nextcmd(p);
8222 }
8223 }
8224 if (!eap->skip)
8225 find_pattern_in_path(eap->arg, 0, (int)STRLEN(eap->arg),
8226 whole, !eap->forceit,
8227 *eap->cmd == 'd' ? FIND_DEFINE : FIND_ANY,
8228 n, action, eap->line1, eap->line2);
8229}
8230#endif
8231
Bram Moolenaar071d4272004-06-13 20:20:40 +00008232
Bram Moolenaar4033c552017-09-16 20:54:51 +02008233#ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00008234/*
8235 * ":ptag", ":ptselect", ":ptjump", ":ptnext", etc.
8236 */
8237 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008238ex_ptag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008239{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008240 g_do_tagpreview = p_pvh; // will be reset to 0 in ex_tag_cmd()
Bram Moolenaar071d4272004-06-13 20:20:40 +00008241 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
8242}
8243
8244/*
8245 * ":pedit"
8246 */
8247 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008248ex_pedit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008249{
8250 win_T *curwin_save = curwin;
8251
Bram Moolenaar3a2505c2020-03-09 16:40:41 +01008252 if (ERROR_IF_ANY_POPUP_WINDOW)
8253 return;
8254
Bram Moolenaar026587b2019-08-17 15:08:00 +02008255 // Open the preview window or popup and make it the current window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008256 g_do_tagpreview = p_pvh;
Bram Moolenaar576a4a62019-08-18 15:25:17 +02008257 prepare_tagpreview(TRUE, TRUE, FALSE);
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02008258
Bram Moolenaar026587b2019-08-17 15:08:00 +02008259 // Edit the file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008260 do_exedit(eap, NULL);
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02008261
Bram Moolenaar071d4272004-06-13 20:20:40 +00008262 if (curwin != curwin_save && win_valid(curwin_save))
8263 {
Bram Moolenaar026587b2019-08-17 15:08:00 +02008264 // Return cursor to where we were
Bram Moolenaar071d4272004-06-13 20:20:40 +00008265 validate_cursor();
8266 redraw_later(VALID);
8267 win_enter(curwin_save, TRUE);
8268 }
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01008269# ifdef FEAT_PROP_POPUP
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02008270 else if (WIN_IS_POPUP(curwin))
8271 {
8272 // can't keep focus in popup window
8273 win_enter(firstwin, TRUE);
8274 }
8275# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008276 g_do_tagpreview = 0;
8277}
Bram Moolenaar4033c552017-09-16 20:54:51 +02008278#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008279
8280/*
8281 * ":stag", ":stselect" and ":stjump".
8282 */
8283 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008284ex_stag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008285{
8286 postponed_split = -1;
8287 postponed_split_flags = cmdmod.split;
Bram Moolenaard326ce82007-03-11 14:48:29 +00008288 postponed_split_tab = cmdmod.tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008289 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
8290 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00008291 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008292}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008293
8294/*
8295 * ":tag", ":tselect", ":tjump", ":tnext", etc.
8296 */
8297 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008298ex_tag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008299{
8300 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name);
8301}
8302
8303 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008304ex_tag_cmd(exarg_T *eap, char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008305{
8306 int cmd;
8307
8308 switch (name[1])
8309 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008310 case 'j': cmd = DT_JUMP; // ":tjump"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008311 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008312 case 's': cmd = DT_SELECT; // ":tselect"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008313 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008314 case 'p': cmd = DT_PREV; // ":tprevious"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008315 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008316 case 'N': cmd = DT_PREV; // ":tNext"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008317 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008318 case 'n': cmd = DT_NEXT; // ":tnext"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008319 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008320 case 'o': cmd = DT_POP; // ":pop"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008321 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008322 case 'f': // ":tfirst"
8323 case 'r': cmd = DT_FIRST; // ":trewind"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008324 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008325 case 'l': cmd = DT_LAST; // ":tlast"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008326 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008327 default: // ":tag"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008328#ifdef FEAT_CSCOPE
Bram Moolenaar7c94c262008-06-20 09:11:34 +00008329 if (p_cst && *eap->arg != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008330 {
Bram Moolenaard4db7712016-11-12 19:16:46 +01008331 ex_cstag(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008332 return;
8333 }
8334#endif
8335 cmd = DT_TAG;
8336 break;
8337 }
8338
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00008339 if (name[0] == 'l')
8340 {
8341#ifndef FEAT_QUICKFIX
8342 ex_ni(eap);
8343 return;
8344#else
8345 cmd = DT_LTAG;
8346#endif
8347 }
8348
Bram Moolenaar071d4272004-06-13 20:20:40 +00008349 do_tag(eap->arg, cmd, eap->addr_count > 0 ? (int)eap->line2 : 1,
8350 eap->forceit, TRUE);
8351}
8352
8353/*
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008354 * Check "str" for starting with a special cmdline variable.
8355 * If found return one of the SPEC_ values and set "*usedlen" to the length of
8356 * the variable. Otherwise return -1 and "*usedlen" is unchanged.
8357 */
8358 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008359find_cmdline_var(char_u *src, int *usedlen)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008360{
8361 int len;
8362 int i;
Bram Moolenaar8f0b2d42009-05-16 14:41:10 +00008363 static char *(spec_str[]) = {
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008364 "%",
8365#define SPEC_PERC 0
8366 "#",
Bram Moolenaar65f08472017-09-10 18:16:20 +02008367#define SPEC_HASH (SPEC_PERC + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008368 "<cword>", // cursor word
Bram Moolenaar65f08472017-09-10 18:16:20 +02008369#define SPEC_CWORD (SPEC_HASH + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008370 "<cWORD>", // cursor WORD
Bram Moolenaar65f08472017-09-10 18:16:20 +02008371#define SPEC_CCWORD (SPEC_CWORD + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008372 "<cexpr>", // expr under cursor
Bram Moolenaar65f08472017-09-10 18:16:20 +02008373#define SPEC_CEXPR (SPEC_CCWORD + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008374 "<cfile>", // cursor path name
Bram Moolenaar65f08472017-09-10 18:16:20 +02008375#define SPEC_CFILE (SPEC_CEXPR + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008376 "<sfile>", // ":so" file name
Bram Moolenaar65f08472017-09-10 18:16:20 +02008377#define SPEC_SFILE (SPEC_CFILE + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008378 "<slnum>", // ":so" file line number
Bram Moolenaar65f08472017-09-10 18:16:20 +02008379#define SPEC_SLNUM (SPEC_SFILE + 1)
Bram Moolenaara5d04232020-07-26 15:37:02 +02008380 "<stack>", // call stack
8381#define SPEC_STACK (SPEC_SLNUM + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008382 "<afile>", // autocommand file name
Bram Moolenaara5d04232020-07-26 15:37:02 +02008383#define SPEC_AFILE (SPEC_STACK + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008384 "<abuf>", // autocommand buffer number
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008385#define SPEC_ABUF (SPEC_AFILE + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008386 "<amatch>", // autocommand match name
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01008387#define SPEC_AMATCH (SPEC_ABUF + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008388 "<sflnum>", // script file line number
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008389#define SPEC_SFLNUM (SPEC_AMATCH + 1)
Bram Moolenaar90944302020-08-01 20:45:11 +02008390 "<SID>", // script ID: <SNR>123_
8391#define SPEC_SID (SPEC_SFLNUM + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008392#ifdef FEAT_CLIENTSERVER
8393 "<client>"
Bram Moolenaar90944302020-08-01 20:45:11 +02008394# define SPEC_CLIENT (SPEC_SID + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008395#endif
8396 };
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008397
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00008398 for (i = 0; i < (int)(sizeof(spec_str) / sizeof(char *)); ++i)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008399 {
8400 len = (int)STRLEN(spec_str[i]);
8401 if (STRNCMP(src, spec_str[i], len) == 0)
8402 {
8403 *usedlen = len;
8404 return i;
8405 }
8406 }
8407 return -1;
8408}
8409
8410/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008411 * Evaluate cmdline variables.
8412 *
8413 * change '%' to curbuf->b_ffname
8414 * '#' to curwin->w_altfile
8415 * '<cword>' to word under the cursor
8416 * '<cWORD>' to WORD under the cursor
Bram Moolenaar8071cb22019-07-12 17:58:01 +02008417 * '<cexpr>' to C-expression under the cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00008418 * '<cfile>' to path name under the cursor
8419 * '<sfile>' to sourced file name
Bram Moolenaar4f25b1a2020-09-10 19:25:05 +02008420 * '<stack>' to call stack
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01008421 * '<slnum>' to sourced file line number
Bram Moolenaar071d4272004-06-13 20:20:40 +00008422 * '<afile>' to file name for autocommand
8423 * '<abuf>' to buffer number for autocommand
8424 * '<amatch>' to matching name for autocommand
8425 *
8426 * When an error is detected, "errormsg" is set to a non-NULL pointer (may be
8427 * "" for error without a message) and NULL is returned.
8428 * Returns an allocated string if a valid match was found.
8429 * Returns NULL if no match was found. "usedlen" then still contains the
8430 * number of characters to skip.
8431 */
8432 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008433eval_vars(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008434 char_u *src, // pointer into commandline
8435 char_u *srcstart, // beginning of valid memory for src
8436 int *usedlen, // characters after src that are used
8437 linenr_T *lnump, // line number for :e command, or NULL
8438 char **errormsg, // pointer to error message
8439 int *escaped) // return value has escaped white space (can
8440 // be NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008441{
8442 int i;
8443 char_u *s;
8444 char_u *result;
8445 char_u *resultbuf = NULL;
8446 int resultlen;
8447 buf_T *buf;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008448 int valid = VALID_HEAD + VALID_PATH; // assume valid result
Bram Moolenaar071d4272004-06-13 20:20:40 +00008449 int spec_idx;
Bram Moolenaarfd249462018-07-28 16:14:30 +02008450 int tilde_file = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008451 int skip_mod = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008452 char_u strbuf[30];
Bram Moolenaar071d4272004-06-13 20:20:40 +00008453
8454 *errormsg = NULL;
Bram Moolenaar63b92542007-03-27 14:57:09 +00008455 if (escaped != NULL)
8456 *escaped = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008457
8458 /*
8459 * Check if there is something to do.
8460 */
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008461 spec_idx = find_cmdline_var(src, usedlen);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008462 if (spec_idx < 0) // no match
Bram Moolenaar071d4272004-06-13 20:20:40 +00008463 {
8464 *usedlen = 1;
8465 return NULL;
8466 }
8467
8468 /*
8469 * Skip when preceded with a backslash "\%" and "\#".
8470 * Note: In "\\%" the % is also not recognized!
8471 */
8472 if (src > srcstart && src[-1] == '\\')
8473 {
8474 *usedlen = 0;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008475 STRMOVE(src - 1, src); // remove backslash
Bram Moolenaar071d4272004-06-13 20:20:40 +00008476 return NULL;
8477 }
8478
8479 /*
8480 * word or WORD under cursor
8481 */
Bram Moolenaar65f08472017-09-10 18:16:20 +02008482 if (spec_idx == SPEC_CWORD || spec_idx == SPEC_CCWORD
8483 || spec_idx == SPEC_CEXPR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008484 {
Bram Moolenaar65f08472017-09-10 18:16:20 +02008485 resultlen = find_ident_under_cursor(&result,
8486 spec_idx == SPEC_CWORD ? (FIND_IDENT | FIND_STRING)
8487 : spec_idx == SPEC_CEXPR ? (FIND_IDENT | FIND_STRING | FIND_EVAL)
8488 : FIND_STRING);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008489 if (resultlen == 0)
8490 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008491 *errormsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +00008492 return NULL;
8493 }
8494 }
8495
8496 /*
8497 * '#': Alternate file name
8498 * '%': Current file name
8499 * File name under the cursor
8500 * File name for autocommand
8501 * and following modifiers
8502 */
8503 else
8504 {
8505 switch (spec_idx)
8506 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008507 case SPEC_PERC: // '%': current file
Bram Moolenaar071d4272004-06-13 20:20:40 +00008508 if (curbuf->b_fname == NULL)
8509 {
8510 result = (char_u *)"";
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008511 valid = 0; // Must have ":p:h" to be valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00008512 }
8513 else
Bram Moolenaar00136dc2018-07-25 21:19:13 +02008514 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008515 result = curbuf->b_fname;
Bram Moolenaar00136dc2018-07-25 21:19:13 +02008516 tilde_file = STRCMP(result, "~") == 0;
8517 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008518 break;
8519
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008520 case SPEC_HASH: // '#' or "#99": alternate file
8521 if (src[1] == '#') // "##": the argument list
Bram Moolenaar071d4272004-06-13 20:20:40 +00008522 {
8523 result = arg_all();
8524 resultbuf = result;
8525 *usedlen = 2;
Bram Moolenaar63b92542007-03-27 14:57:09 +00008526 if (escaped != NULL)
8527 *escaped = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008528 skip_mod = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008529 break;
8530 }
8531 s = src + 1;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008532 if (*s == '<') // "#<99" uses v:oldfiles
Bram Moolenaard812df62008-11-09 12:46:09 +00008533 ++s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008534 i = (int)getdigits(&s);
Bram Moolenaarc312b8b2017-10-28 17:53:04 +02008535 if (s == src + 2 && src[1] == '-')
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008536 // just a minus sign, don't skip over it
Bram Moolenaarc312b8b2017-10-28 17:53:04 +02008537 s--;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008538 *usedlen = (int)(s - src); // length of what we expand
Bram Moolenaar071d4272004-06-13 20:20:40 +00008539
Bram Moolenaarc312b8b2017-10-28 17:53:04 +02008540 if (src[1] == '<' && i != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008541 {
Bram Moolenaard812df62008-11-09 12:46:09 +00008542 if (*usedlen < 2)
8543 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008544 // Should we give an error message for #<text?
Bram Moolenaard812df62008-11-09 12:46:09 +00008545 *usedlen = 1;
8546 return NULL;
8547 }
8548#ifdef FEAT_EVAL
8549 result = list_find_str(get_vim_var_list(VV_OLDFILES),
8550 (long)i);
8551 if (result == NULL)
8552 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008553 *errormsg = "";
Bram Moolenaard812df62008-11-09 12:46:09 +00008554 return NULL;
8555 }
8556#else
Bram Moolenaar8e481e82019-01-15 20:07:48 +01008557 *errormsg = _("E809: #< is not available without the +eval feature");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008558 return NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +00008559#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008560 }
8561 else
Bram Moolenaard812df62008-11-09 12:46:09 +00008562 {
Bram Moolenaarc312b8b2017-10-28 17:53:04 +02008563 if (i == 0 && src[1] == '<' && *usedlen > 1)
8564 *usedlen = 1;
Bram Moolenaard812df62008-11-09 12:46:09 +00008565 buf = buflist_findnr(i);
8566 if (buf == NULL)
8567 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008568 *errormsg = _("E194: No alternate file name to substitute for '#'");
Bram Moolenaard812df62008-11-09 12:46:09 +00008569 return NULL;
8570 }
8571 if (lnump != NULL)
8572 *lnump = ECMD_LAST;
8573 if (buf->b_fname == NULL)
8574 {
8575 result = (char_u *)"";
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008576 valid = 0; // Must have ":p:h" to be valid
Bram Moolenaard812df62008-11-09 12:46:09 +00008577 }
8578 else
Bram Moolenaar00136dc2018-07-25 21:19:13 +02008579 {
Bram Moolenaard812df62008-11-09 12:46:09 +00008580 result = buf->b_fname;
Bram Moolenaar00136dc2018-07-25 21:19:13 +02008581 tilde_file = STRCMP(result, "~") == 0;
8582 }
Bram Moolenaard812df62008-11-09 12:46:09 +00008583 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008584 break;
8585
8586#ifdef FEAT_SEARCHPATH
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008587 case SPEC_CFILE: // file name under cursor
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008588 result = file_name_at_cursor(FNAME_MESS|FNAME_HYP, 1L, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008589 if (result == NULL)
8590 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008591 *errormsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +00008592 return NULL;
8593 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008594 resultbuf = result; // remember allocated string
Bram Moolenaar071d4272004-06-13 20:20:40 +00008595 break;
8596#endif
8597
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008598 case SPEC_AFILE: // file name for autocommand
Bram Moolenaar071d4272004-06-13 20:20:40 +00008599 result = autocmd_fname;
Bram Moolenaarf6dad432008-09-18 19:29:58 +00008600 if (result != NULL && !autocmd_fname_full)
8601 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008602 // Still need to turn the fname into a full path. It is
8603 // postponed to avoid a delay when <afile> is not used.
Bram Moolenaarf6dad432008-09-18 19:29:58 +00008604 autocmd_fname_full = TRUE;
8605 result = FullName_save(autocmd_fname, FALSE);
8606 vim_free(autocmd_fname);
8607 autocmd_fname = result;
8608 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008609 if (result == NULL)
8610 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008611 *errormsg = _("E495: no autocommand file name to substitute for \"<afile>\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008612 return NULL;
8613 }
Bram Moolenaara0174af2008-01-02 20:08:25 +00008614 result = shorten_fname1(result);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008615 break;
8616
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008617 case SPEC_ABUF: // buffer number for autocommand
Bram Moolenaar071d4272004-06-13 20:20:40 +00008618 if (autocmd_bufnr <= 0)
8619 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008620 *errormsg = _("E496: no autocommand buffer number to substitute for \"<abuf>\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008621 return NULL;
8622 }
8623 sprintf((char *)strbuf, "%d", autocmd_bufnr);
8624 result = strbuf;
8625 break;
8626
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008627 case SPEC_AMATCH: // match name for autocommand
Bram Moolenaar071d4272004-06-13 20:20:40 +00008628 result = autocmd_match;
8629 if (result == NULL)
8630 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008631 *errormsg = _("E497: no autocommand match name to substitute for \"<amatch>\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008632 return NULL;
8633 }
8634 break;
8635
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008636 case SPEC_SFILE: // file name for ":so" command
Bram Moolenaara5d04232020-07-26 15:37:02 +02008637 case SPEC_STACK: // call stack
Bram Moolenaar4f25b1a2020-09-10 19:25:05 +02008638 result = estack_sfile(spec_idx == SPEC_SFILE
8639 ? ESTACK_SFILE : ESTACK_STACK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008640 if (result == NULL)
8641 {
Bram Moolenaara5d04232020-07-26 15:37:02 +02008642 *errormsg = spec_idx == SPEC_SFILE
8643 ? _("E498: no :source file name to substitute for \"<sfile>\"")
8644 : _("E489: no call stack to substitute for \"<stack>\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008645 return NULL;
8646 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01008647 resultbuf = result; // remember allocated string
Bram Moolenaar071d4272004-06-13 20:20:40 +00008648 break;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008649
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008650 case SPEC_SLNUM: // line in file for ":so" command
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01008651 if (SOURCING_NAME == NULL || SOURCING_LNUM == 0)
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01008652 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008653 *errormsg = _("E842: no line number to use for \"<slnum>\"");
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01008654 return NULL;
8655 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01008656 sprintf((char *)strbuf, "%ld", SOURCING_LNUM);
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01008657 result = strbuf;
8658 break;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008659
8660#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008661 case SPEC_SFLNUM: // line in script file
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01008662 if (current_sctx.sc_lnum + SOURCING_LNUM == 0)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008663 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008664 *errormsg = _("E961: no line number to use for \"<sflnum>\"");
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008665 return NULL;
8666 }
8667 sprintf((char *)strbuf, "%ld",
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01008668 (long)(current_sctx.sc_lnum + SOURCING_LNUM));
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008669 result = strbuf;
8670 break;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008671
Bram Moolenaar90944302020-08-01 20:45:11 +02008672 case SPEC_SID:
8673 if (current_sctx.sc_sid <= 0)
8674 {
8675 *errormsg = _(e_usingsid);
8676 return NULL;
8677 }
8678 sprintf((char *)strbuf, "<SNR>%d_", current_sctx.sc_sid);
8679 result = strbuf;
8680 break;
Bram Moolenaare4218b92020-08-01 21:11:38 +02008681#endif
Bram Moolenaar90944302020-08-01 20:45:11 +02008682
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008683#ifdef FEAT_CLIENTSERVER
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008684 case SPEC_CLIENT: // Source of last submitted input
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008685 sprintf((char *)strbuf, PRINTF_HEX_LONG_U,
8686 (long_u)clientWindow);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008687 result = strbuf;
8688 break;
8689#endif
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008690
Bram Moolenaar9e0f6ec2017-05-16 09:36:54 +02008691 default:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008692 result = (char_u *)""; // avoid gcc warning
Bram Moolenaar9e0f6ec2017-05-16 09:36:54 +02008693 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008694 }
8695
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008696 resultlen = (int)STRLEN(result); // length of new string
8697 if (src[*usedlen] == '<') // remove the file name extension
Bram Moolenaar071d4272004-06-13 20:20:40 +00008698 {
8699 ++*usedlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008700 if ((s = vim_strrchr(result, '.')) != NULL && s >= gettail(result))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008701 resultlen = (int)(s - result);
8702 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008703 else if (!skip_mod)
8704 {
Bram Moolenaar00136dc2018-07-25 21:19:13 +02008705 valid |= modify_fname(src, tilde_file, usedlen, &result, &resultbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008706 &resultlen);
8707 if (result == NULL)
8708 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008709 *errormsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +00008710 return NULL;
8711 }
8712 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008713 }
8714
8715 if (resultlen == 0 || valid != VALID_HEAD + VALID_PATH)
8716 {
8717 if (valid != VALID_HEAD + VALID_PATH)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008718 // xgettext:no-c-format
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008719 *errormsg = _("E499: Empty file name for '%' or '#', only works with \":p:h\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008720 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008721 *errormsg = _("E500: Evaluates to an empty string");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008722 result = NULL;
8723 }
8724 else
8725 result = vim_strnsave(result, resultlen);
8726 vim_free(resultbuf);
8727 return result;
8728}
8729
8730/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008731 * Expand the <sfile> string in "arg".
8732 *
8733 * Returns an allocated string, or NULL for any error.
8734 */
8735 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008736expand_sfile(char_u *arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008737{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008738 char *errormsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008739 int len;
8740 char_u *result;
8741 char_u *newres;
8742 char_u *repl;
8743 int srclen;
8744 char_u *p;
8745
8746 result = vim_strsave(arg);
8747 if (result == NULL)
8748 return NULL;
8749
8750 for (p = result; *p; )
8751 {
8752 if (STRNCMP(p, "<sfile>", 7) != 0)
8753 ++p;
8754 else
8755 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008756 // replace "<sfile>" with the sourced file name, and do ":" stuff
Bram Moolenaar63b92542007-03-27 14:57:09 +00008757 repl = eval_vars(p, result, &srclen, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008758 if (errormsg != NULL)
8759 {
8760 if (*errormsg)
8761 emsg(errormsg);
8762 vim_free(result);
8763 return NULL;
8764 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008765 if (repl == NULL) // no match (cannot happen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008766 {
8767 p += srclen;
8768 continue;
8769 }
8770 len = (int)STRLEN(result) - srclen + (int)STRLEN(repl) + 1;
8771 newres = alloc(len);
8772 if (newres == NULL)
8773 {
8774 vim_free(repl);
8775 vim_free(result);
8776 return NULL;
8777 }
8778 mch_memmove(newres, result, (size_t)(p - result));
8779 STRCPY(newres + (p - result), repl);
8780 len = (int)STRLEN(newres);
8781 STRCAT(newres, p + srclen);
8782 vim_free(repl);
8783 vim_free(result);
8784 result = newres;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008785 p = newres + len; // continue after the match
Bram Moolenaar071d4272004-06-13 20:20:40 +00008786 }
8787 }
8788
8789 return result;
8790}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008791
Bram Moolenaar071d4272004-06-13 20:20:40 +00008792#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG) || defined(PROTO)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00008793/*
Bram Moolenaard9462e32011-04-11 21:35:11 +02008794 * Make a dialog message in "buff[DIALOG_MSG_SIZE]".
Bram Moolenaarb765d632005-06-07 21:00:02 +00008795 * "format" must contain "%s".
Bram Moolenaar9c13b352005-05-19 20:53:52 +00008796 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008797 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008798dialog_msg(char_u *buff, char *format, char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008799{
Bram Moolenaar071d4272004-06-13 20:20:40 +00008800 if (fname == NULL)
8801 fname = (char_u *)_("Untitled");
Bram Moolenaard9462e32011-04-11 21:35:11 +02008802 vim_snprintf((char *)buff, DIALOG_MSG_SIZE, format, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008803}
8804#endif
8805
8806/*
8807 * ":behave {mswin,xterm}"
8808 */
8809 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008810ex_behave(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008811{
8812 if (STRCMP(eap->arg, "mswin") == 0)
8813 {
8814 set_option_value((char_u *)"selection", 0L, (char_u *)"exclusive", 0);
8815 set_option_value((char_u *)"selectmode", 0L, (char_u *)"mouse,key", 0);
8816 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"popup", 0);
8817 set_option_value((char_u *)"keymodel", 0L,
8818 (char_u *)"startsel,stopsel", 0);
8819 }
8820 else if (STRCMP(eap->arg, "xterm") == 0)
8821 {
8822 set_option_value((char_u *)"selection", 0L, (char_u *)"inclusive", 0);
8823 set_option_value((char_u *)"selectmode", 0L, (char_u *)"", 0);
8824 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"extend", 0);
8825 set_option_value((char_u *)"keymodel", 0L, (char_u *)"", 0);
8826 }
8827 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008828 semsg(_(e_invarg2), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008829}
8830
Bram Moolenaar071d4272004-06-13 20:20:40 +00008831static int filetype_detect = FALSE;
8832static int filetype_plugin = FALSE;
8833static int filetype_indent = FALSE;
8834
8835/*
8836 * ":filetype [plugin] [indent] {on,off,detect}"
8837 * on: Load the filetype.vim file to install autocommands for file types.
8838 * off: Load the ftoff.vim file to remove all autocommands for file types.
8839 * plugin on: load filetype.vim and ftplugin.vim
8840 * plugin off: load ftplugof.vim
8841 * indent on: load filetype.vim and indent.vim
8842 * indent off: load indoff.vim
8843 */
8844 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008845ex_filetype(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008846{
8847 char_u *arg = eap->arg;
8848 int plugin = FALSE;
8849 int indent = FALSE;
8850
8851 if (*eap->arg == NUL)
8852 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008853 // Print current status.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008854 smsg("filetype detection:%s plugin:%s indent:%s",
Bram Moolenaar071d4272004-06-13 20:20:40 +00008855 filetype_detect ? "ON" : "OFF",
8856 filetype_plugin ? (filetype_detect ? "ON" : "(on)") : "OFF",
8857 filetype_indent ? (filetype_detect ? "ON" : "(on)") : "OFF");
8858 return;
8859 }
8860
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008861 // Accept "plugin" and "indent" in any order.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008862 for (;;)
8863 {
8864 if (STRNCMP(arg, "plugin", 6) == 0)
8865 {
8866 plugin = TRUE;
8867 arg = skipwhite(arg + 6);
8868 continue;
8869 }
8870 if (STRNCMP(arg, "indent", 6) == 0)
8871 {
8872 indent = TRUE;
8873 arg = skipwhite(arg + 6);
8874 continue;
8875 }
8876 break;
8877 }
8878 if (STRCMP(arg, "on") == 0 || STRCMP(arg, "detect") == 0)
8879 {
8880 if (*arg == 'o' || !filetype_detect)
8881 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01008882 source_runtime((char_u *)FILETYPE_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008883 filetype_detect = TRUE;
8884 if (plugin)
8885 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01008886 source_runtime((char_u *)FTPLUGIN_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008887 filetype_plugin = TRUE;
8888 }
8889 if (indent)
8890 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01008891 source_runtime((char_u *)INDENT_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008892 filetype_indent = TRUE;
8893 }
8894 }
8895 if (*arg == 'd')
8896 {
Bram Moolenaar1610d052016-06-09 22:53:01 +02008897 (void)do_doautocmd((char_u *)"filetypedetect BufRead", TRUE, NULL);
Bram Moolenaara3227e22006-03-08 21:32:40 +00008898 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008899 }
8900 }
8901 else if (STRCMP(arg, "off") == 0)
8902 {
8903 if (plugin || indent)
8904 {
8905 if (plugin)
8906 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01008907 source_runtime((char_u *)FTPLUGOF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008908 filetype_plugin = FALSE;
8909 }
8910 if (indent)
8911 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01008912 source_runtime((char_u *)INDOFF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008913 filetype_indent = FALSE;
8914 }
8915 }
8916 else
8917 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01008918 source_runtime((char_u *)FTOFF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008919 filetype_detect = FALSE;
8920 }
8921 }
8922 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008923 semsg(_(e_invarg2), arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008924}
8925
8926/*
Bram Moolenaar3e545692017-06-04 19:00:32 +02008927 * ":setfiletype [FALLBACK] {name}"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008928 */
8929 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008930ex_setfiletype(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008931{
8932 if (!did_filetype)
Bram Moolenaar3e545692017-06-04 19:00:32 +02008933 {
8934 char_u *arg = eap->arg;
8935
8936 if (STRNCMP(arg, "FALLBACK ", 9) == 0)
8937 arg += 9;
8938
8939 set_option_value((char_u *)"filetype", 0L, arg, OPT_LOCAL);
8940 if (arg != eap->arg)
8941 did_filetype = FALSE;
8942 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008943}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008944
Bram Moolenaar071d4272004-06-13 20:20:40 +00008945 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008946ex_digraphs(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008947{
8948#ifdef FEAT_DIGRAPHS
8949 if (*eap->arg != NUL)
8950 putdigraph(eap->arg);
8951 else
Bram Moolenaareae8ae12018-12-14 18:53:02 +01008952 listdigraphs(eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008953#else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008954 emsg(_("E196: No digraphs in this version"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008955#endif
8956}
8957
Bram Moolenaar451fc7b2018-04-27 22:53:07 +02008958#if defined(FEAT_SEARCH_EXTRA) || defined(PROTO)
8959 void
8960set_no_hlsearch(int flag)
8961{
8962 no_hlsearch = flag;
8963# ifdef FEAT_EVAL
8964 set_vim_var_nr(VV_HLSEARCH, !no_hlsearch && p_hls);
8965# endif
8966}
8967
Bram Moolenaar071d4272004-06-13 20:20:40 +00008968/*
8969 * ":nohlsearch"
8970 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008971 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008972ex_nohlsearch(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008973{
Bram Moolenaar451fc7b2018-04-27 22:53:07 +02008974 set_no_hlsearch(TRUE);
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00008975 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008976}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008977#endif
8978
8979#ifdef FEAT_CRYPT
8980/*
8981 * ":X": Get crypt key
8982 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008983 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008984ex_X(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008985{
Bram Moolenaar3a0c9082014-11-12 15:15:42 +01008986 crypt_check_current_method();
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02008987 (void)crypt_get_key(TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008988}
8989#endif
8990
8991#ifdef FEAT_FOLDING
8992 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008993ex_fold(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008994{
8995 if (foldManualAllowed(TRUE))
8996 foldCreate(eap->line1, eap->line2);
8997}
8998
8999 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009000ex_foldopen(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009001{
9002 opFoldRange(eap->line1, eap->line2, eap->cmdidx == CMD_foldopen,
9003 eap->forceit, FALSE);
9004}
9005
9006 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009007ex_folddo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009008{
9009 linenr_T lnum;
9010
Bram Moolenaar4facea32019-10-12 20:17:40 +02009011# ifdef FEAT_CLIPBOARD
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02009012 start_global_changes();
Bram Moolenaar4facea32019-10-12 20:17:40 +02009013# endif
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02009014
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009015 // First set the marks for all lines closed/open.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009016 for (lnum = eap->line1; lnum <= eap->line2; ++lnum)
9017 if (hasFolding(lnum, NULL, NULL) == (eap->cmdidx == CMD_folddoclosed))
9018 ml_setmarked(lnum);
9019
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009020 // Execute the command on the marked lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009021 global_exe(eap->arg);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009022 ml_clearmarked(); // clear rest of the marks
Bram Moolenaar4facea32019-10-12 20:17:40 +02009023# ifdef FEAT_CLIPBOARD
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02009024 end_global_changes();
Bram Moolenaar4facea32019-10-12 20:17:40 +02009025# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009026}
9027#endif
Bram Moolenaarf5291f32017-09-14 22:55:37 +02009028
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009029#if defined(FEAT_QUICKFIX) || defined(PROTO)
Bram Moolenaar39665952018-08-15 20:59:48 +02009030/*
9031 * Returns TRUE if the supplied Ex cmdidx is for a location list command
9032 * instead of a quickfix command.
9033 */
9034 int
9035is_loclist_cmd(int cmdidx)
9036{
Bram Moolenaar74c8be22018-08-23 22:51:40 +02009037 if (cmdidx < 0 || cmdidx >= CMD_SIZE)
Bram Moolenaar39665952018-08-15 20:59:48 +02009038 return FALSE;
9039 return cmdnames[cmdidx].cmd_name[0] == 'l';
9040}
9041#endif
9042
Bram Moolenaar4facea32019-10-12 20:17:40 +02009043#if defined(FEAT_TIMERS) || defined(PROTO)
Bram Moolenaarf5291f32017-09-14 22:55:37 +02009044 int
9045get_pressedreturn(void)
9046{
9047 return ex_pressedreturn;
9048}
9049
9050 void
9051set_pressedreturn(int val)
9052{
9053 ex_pressedreturn = val;
9054}
9055#endif