blob: 4a71989ddbda80221317a64baa2362a96745ffa0 [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 ? (
Bram Moolenaar9becdf22020-10-10 21:33:48 +02003225 // "{..." is a dict expression or block start.
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003226 *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 {
Bram Moolenaar9becdf22020-10-10 21:33:48 +02003237 if (*eap->cmd == '{' && ends_excmd(*skipwhite(eap->cmd + 1)))
3238 {
3239 // "{" by itself is the start of a block.
3240 eap->cmdidx = CMD_block;
3241 return eap->cmd + 1;
3242 }
Bram Moolenaarb31be3f2020-07-20 22:37:44 +02003243 eap->cmdidx = CMD_eval;
3244 return eap->cmd;
3245 }
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003246
Bram Moolenaar2b22b112020-09-14 18:35:18 +02003247 if (p != eap->cmd && (
3248 // "varname[]" is an expression.
3249 *p == '['
3250 // "varname.key" is an expression.
3251 || (*p == '.' && ASCII_ISALPHA(p[1]))))
3252 {
3253 char_u *after = p;
3254
3255 // When followed by "=" or "+=" then it is an assignment.
3256 ++emsg_silent;
3257 if (skip_expr(&after) == OK
3258 && (*after == '='
3259 || (*after != NUL && after[1] == '=')))
Bram Moolenaar30fd8202020-09-26 15:09:30 +02003260 eap->cmdidx = CMD_var;
Bram Moolenaar2b22b112020-09-14 18:35:18 +02003261 else
3262 eap->cmdidx = CMD_eval;
3263 --emsg_silent;
3264 return eap->cmd;
3265 }
3266
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003267 // "[...]->Method()" is a list expression, but "[a, b] = Func()" is
3268 // an assignment.
3269 // If there is no line break inside the "[...]" then "p" is
3270 // advanced to after the "]" by to_name_const_end(): check if a "="
3271 // follows.
3272 // If "[...]" has a line break "p" still points at the "[" and it
3273 // can't be an assignment.
3274 if (*eap->cmd == '[')
Bram Moolenaarb31be3f2020-07-20 22:37:44 +02003275 {
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003276 p = to_name_const_end(eap->cmd);
3277 if (p == eap->cmd || *skipwhite(p) != '=')
3278 {
3279 eap->cmdidx = CMD_eval;
3280 return eap->cmd;
3281 }
3282 if (p > eap->cmd && *skipwhite(p) == '=')
3283 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02003284 eap->cmdidx = CMD_var;
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003285 return eap->cmd;
3286 }
3287 }
3288
3289 // Recognize an assignment if we recognize the variable name:
3290 // "g:var = expr"
3291 // "var = expr" where "var" is a local var name.
Bram Moolenaar83144542020-08-02 20:40:43 +02003292 if (*eap->cmd == '@')
3293 p = eap->cmd + 2;
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003294 oplen = assignment_len(skipwhite(p), &heredoc);
3295 if (oplen > 0)
3296 {
3297 if (((p - eap->cmd) > 2 && eap->cmd[1] == ':')
3298 || *eap->cmd == '&'
3299 || *eap->cmd == '$'
3300 || *eap->cmd == '@'
3301 || lookup(eap->cmd, p - eap->cmd, cctx) != NULL)
3302 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02003303 eap->cmdidx = CMD_var;
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003304 return eap->cmd;
3305 }
3306 }
3307
3308 // Recognize using a type for a w:, b:, t: or g: variable:
3309 // "w:varname: number = 123".
3310 if (eap->cmd[1] == ':' && *p == ':')
3311 {
3312 eap->cmdidx = CMD_eval;
Bram Moolenaarb31be3f2020-07-20 22:37:44 +02003313 return eap->cmd;
3314 }
Bram Moolenaard2ef6b32020-07-02 21:11:34 +02003315 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003316 }
3317#endif
3318
Bram Moolenaar071d4272004-06-13 20:20:40 +00003319 /*
3320 * Isolate the command and search for it in the command table.
Bram Moolenaar81870892007-11-11 18:17:28 +00003321 * Exceptions:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003322 * - the 'k' command can directly be followed by any character.
3323 * - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003324 * but :sre[wind] is another command, as are :scr[iptnames],
Bram Moolenaar071d4272004-06-13 20:20:40 +00003325 * :scs[cope], :sim[alt], :sig[ns] and :sil[ent].
Bram Moolenaardf177f62005-02-22 08:39:57 +00003326 * - the "d" command can directly be followed by 'l' or 'p' flag.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003327 */
3328 p = eap->cmd;
3329 if (*p == 'k')
3330 {
3331 eap->cmdidx = CMD_k;
3332 ++p;
3333 }
3334 else if (p[0] == 's'
Bram Moolenaar204b93f2015-08-04 22:02:51 +02003335 && ((p[1] == 'c' && (p[2] == NUL || (p[2] != 's' && p[2] != 'r'
3336 && (p[3] == NUL || (p[3] != 'i' && p[4] != 'p')))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003337 || p[1] == 'g'
3338 || (p[1] == 'i' && p[2] != 'm' && p[2] != 'l' && p[2] != 'g')
3339 || p[1] == 'I'
3340 || (p[1] == 'r' && p[2] != 'e')))
3341 {
3342 eap->cmdidx = CMD_substitute;
3343 ++p;
3344 }
3345 else
3346 {
3347 while (ASCII_ISALPHA(*p))
3348 ++p;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003349 // for python 3.x support ":py3", ":python3", ":py3file", etc.
Bram Moolenaar55d5c032010-07-17 23:52:29 +02003350 if (eap->cmd[0] == 'p' && eap->cmd[1] == 'y')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003351 {
Bram Moolenaarb6590522010-07-21 16:00:43 +02003352 while (ASCII_ISALNUM(*p))
3353 ++p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003354 }
3355 else if (*p == '9' && STRNCMP("vim9", eap->cmd, 4) == 0)
3356 {
3357 // include "9" for "vim9script"
3358 ++p;
3359 while (ASCII_ISALPHA(*p))
3360 ++p;
3361 }
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02003362
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003363 // check for non-alpha command
Bram Moolenaar9becdf22020-10-10 21:33:48 +02003364 if (p == eap->cmd && vim_strchr((char_u *)"@*!=><&~#}", *p) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003365 ++p;
3366 len = (int)(p - eap->cmd);
Bram Moolenaardf177f62005-02-22 08:39:57 +00003367 if (*eap->cmd == 'd' && (p[-1] == 'l' || p[-1] == 'p'))
3368 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003369 // Check for ":dl", ":dell", etc. to ":deletel": that's
3370 // :delete with the 'l' flag. Same for 'p'.
Bram Moolenaardf177f62005-02-22 08:39:57 +00003371 for (i = 0; i < len; ++i)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003372 if (eap->cmd[i] != ((char_u *)"delete")[i])
Bram Moolenaardf177f62005-02-22 08:39:57 +00003373 break;
3374 if (i == len - 1)
3375 {
3376 --len;
3377 if (p[-1] == 'l')
3378 eap->flags |= EXFLAG_LIST;
3379 else
3380 eap->flags |= EXFLAG_PRINT;
3381 }
3382 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003383
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003384 if (ASCII_ISLOWER(eap->cmd[0]))
3385 {
Bram Moolenaar6c0c1e82017-03-25 15:07:43 +01003386 int c1 = eap->cmd[0];
Bram Moolenaar94f82cb2019-07-24 22:30:27 +02003387 int c2 = len == 1 ? NUL : eap->cmd[1];
Bram Moolenaar6c0c1e82017-03-25 15:07:43 +01003388
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003389 if (command_count != (int)CMD_SIZE)
3390 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003391 iemsg(_("E943: Command table needs to be updated, run 'make cmdidxs'"));
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003392 getout(1);
3393 }
3394
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003395 // Use a precomputed index for fast look-up in cmdnames[]
3396 // taking into account the first 2 letters of eap->cmd.
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003397 eap->cmdidx = cmdidxs1[CharOrdLow(c1)];
3398 if (ASCII_ISLOWER(c2))
3399 eap->cmdidx += cmdidxs2[CharOrdLow(c1)][CharOrdLow(c2)];
3400 }
Bram Moolenaara494f562020-04-18 17:45:38 +02003401 else if (ASCII_ISUPPER(eap->cmd[0]))
3402 eap->cmdidx = CMD_Next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003403 else
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003404 eap->cmdidx = CMD_bang;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003405
3406 for ( ; (int)eap->cmdidx < (int)CMD_SIZE;
3407 eap->cmdidx = (cmdidx_T)((int)eap->cmdidx + 1))
3408 if (STRNCMP(cmdnames[(int)eap->cmdidx].cmd_name, (char *)eap->cmd,
3409 (size_t)len) == 0)
3410 {
3411#ifdef FEAT_EVAL
3412 if (full != NULL
3413 && cmdnames[(int)eap->cmdidx].cmd_name[len] == NUL)
3414 *full = TRUE;
3415#endif
3416 break;
3417 }
3418
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003419 // Look for a user defined command as a last resort. Let ":Print" be
3420 // overruled by a user defined command.
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01003421 if ((eap->cmdidx == CMD_SIZE || eap->cmdidx == CMD_Print)
3422 && *eap->cmd >= 'A' && *eap->cmd <= 'Z')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003423 {
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003424 // User defined commands may contain digits.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003425 while (ASCII_ISALNUM(*p))
3426 ++p;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003427 p = find_ucmd(eap, p, full, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003428 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003429 if (p == eap->cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003430 eap->cmdidx = CMD_SIZE;
3431 }
3432
Bram Moolenaar373863e2020-09-26 17:20:53 +02003433 // ":fina" means ":finally" for backwards compatibility.
3434 if (eap->cmdidx == CMD_final && p - eap->cmd == 4)
3435 eap->cmdidx = CMD_finally;
3436
Bram Moolenaar071d4272004-06-13 20:20:40 +00003437 return p;
3438}
3439
3440#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003441static struct cmdmod
3442{
3443 char *name;
3444 int minlen;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003445 int has_count; // :123verbose :3tab
Bram Moolenaared53fb92007-11-24 20:50:24 +00003446} cmdmods[] = {
3447 {"aboveleft", 3, FALSE},
3448 {"belowright", 3, FALSE},
3449 {"botright", 2, FALSE},
3450 {"browse", 3, FALSE},
3451 {"confirm", 4, FALSE},
Bram Moolenaar7b668e82016-08-23 23:51:21 +02003452 {"filter", 4, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003453 {"hide", 3, FALSE},
3454 {"keepalt", 5, FALSE},
3455 {"keepjumps", 5, FALSE},
3456 {"keepmarks", 3, FALSE},
Bram Moolenaara939e432013-11-09 05:30:26 +01003457 {"keeppatterns", 5, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003458 {"leftabove", 5, FALSE},
3459 {"lockmarks", 3, FALSE},
Bram Moolenaarca9f9582008-09-18 10:44:28 +00003460 {"noautocmd", 3, FALSE},
Bram Moolenaar5803ae62014-03-23 16:04:02 +01003461 {"noswapfile", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003462 {"rightbelow", 6, FALSE},
3463 {"sandbox", 3, FALSE},
3464 {"silent", 3, FALSE},
3465 {"tab", 3, TRUE},
3466 {"topleft", 2, FALSE},
Bram Moolenaar8e258a42009-07-09 13:55:43 +00003467 {"unsilent", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003468 {"verbose", 4, TRUE},
3469 {"vertical", 4, FALSE},
3470};
3471
3472/*
3473 * Return length of a command modifier (including optional count).
3474 * Return zero when it's not a modifier.
3475 */
3476 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003477modifier_len(char_u *cmd)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003478{
3479 int i, j;
3480 char_u *p = cmd;
3481
3482 if (VIM_ISDIGIT(*cmd))
3483 p = skipwhite(skipdigits(cmd));
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00003484 for (i = 0; i < (int)(sizeof(cmdmods) / sizeof(struct cmdmod)); ++i)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003485 {
3486 for (j = 0; p[j] != NUL; ++j)
3487 if (p[j] != cmdmods[i].name[j])
3488 break;
Bram Moolenaar2d473ab2013-06-12 17:12:24 +02003489 if (!ASCII_ISALPHA(p[j]) && j >= cmdmods[i].minlen
Bram Moolenaared53fb92007-11-24 20:50:24 +00003490 && (p == cmd || cmdmods[i].has_count))
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00003491 return j + (int)(p - cmd);
Bram Moolenaared53fb92007-11-24 20:50:24 +00003492 }
3493 return 0;
3494}
3495
Bram Moolenaar071d4272004-06-13 20:20:40 +00003496/*
3497 * Return > 0 if an Ex command "name" exists.
3498 * Return 2 if there is an exact match.
3499 * Return 3 if there is an ambiguous match.
3500 */
3501 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003502cmd_exists(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003503{
3504 exarg_T ea;
3505 int full = FALSE;
3506 int i;
3507 int j;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003508 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003509
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003510 // Check command modifiers.
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00003511 for (i = 0; i < (int)(sizeof(cmdmods) / sizeof(struct cmdmod)); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003512 {
3513 for (j = 0; name[j] != NUL; ++j)
3514 if (name[j] != cmdmods[i].name[j])
3515 break;
3516 if (name[j] == NUL && j >= cmdmods[i].minlen)
3517 return (cmdmods[i].name[j] == NUL ? 2 : 1);
3518 }
3519
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003520 // Check built-in commands and user defined commands.
3521 // For ":2match" and ":3match" we need to skip the number.
Bram Moolenaara9587612006-05-04 21:47:50 +00003522 ea.cmd = (*name == '2' || *name == '3') ? name + 1 : name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003523 ea.cmdidx = (cmdidx_T)0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003524 p = find_ex_command(&ea, &full, NULL, NULL);
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003525 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003526 return 3;
Bram Moolenaara9587612006-05-04 21:47:50 +00003527 if (vim_isdigit(*name) && ea.cmdidx != CMD_match)
3528 return 0;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003529 if (*skipwhite(p) != NUL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003530 return 0; // trailing garbage
Bram Moolenaar071d4272004-06-13 20:20:40 +00003531 return (ea.cmdidx == CMD_SIZE ? 0 : (full ? 2 : 1));
3532}
3533#endif
3534
Bram Moolenaard0190392019-08-23 21:17:35 +02003535 cmdidx_T
3536excmd_get_cmdidx(char_u *cmd, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003537{
Bram Moolenaard0190392019-08-23 21:17:35 +02003538 cmdidx_T idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003539
Bram Moolenaard0190392019-08-23 21:17:35 +02003540 for (idx = (cmdidx_T)0; (int)idx < (int)CMD_SIZE;
3541 idx = (cmdidx_T)((int)idx + 1))
3542 if (STRNCMP(cmdnames[(int)idx].cmd_name, cmd, (size_t)len) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003543 break;
3544
Bram Moolenaard0190392019-08-23 21:17:35 +02003545 return idx;
3546}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003547
Bram Moolenaard0190392019-08-23 21:17:35 +02003548 long
3549excmd_get_argt(cmdidx_T idx)
3550{
3551 return (long)cmdnames[(int)idx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003552}
3553
3554/*
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003555 * Skip a range specifier of the form: addr [,addr] [;addr] ..
Bram Moolenaar071d4272004-06-13 20:20:40 +00003556 *
3557 * Backslashed delimiters after / or ? will be skipped, and commands will
3558 * not be expanded between /'s and ?'s or after "'".
3559 *
Bram Moolenaardf069ee2020-06-22 23:02:51 +02003560 * Also skip white space and ":" characters after the range.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003561 * Returns the "cmd" pointer advanced to beyond the range.
3562 */
3563 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003564skip_range(
3565 char_u *cmd,
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02003566 int skip_star, // skip "*" used for Visual range
3567 int *ctx) // pointer to xp_context or NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003568{
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003569 unsigned delim;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003570
Bram Moolenaarcbf20fb2017-02-03 21:19:04 +01003571 while (vim_strchr((char_u *)" \t0123456789.$%'/?-+,;\\", *cmd) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003572 {
Bram Moolenaarcbf20fb2017-02-03 21:19:04 +01003573 if (*cmd == '\\')
3574 {
3575 if (cmd[1] == '?' || cmd[1] == '/' || cmd[1] == '&')
3576 ++cmd;
3577 else
3578 break;
3579 }
3580 else if (*cmd == '\'')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003581 {
3582 if (*++cmd == NUL && ctx != NULL)
3583 *ctx = EXPAND_NOTHING;
3584 }
3585 else if (*cmd == '/' || *cmd == '?')
3586 {
3587 delim = *cmd++;
3588 while (*cmd != NUL && *cmd != delim)
3589 if (*cmd++ == '\\' && *cmd != NUL)
3590 ++cmd;
3591 if (*cmd == NUL && ctx != NULL)
3592 *ctx = EXPAND_NOTHING;
3593 }
3594 if (*cmd != NUL)
3595 ++cmd;
3596 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00003597
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003598 // Skip ":" and white space.
Bram Moolenaardf177f62005-02-22 08:39:57 +00003599 while (*cmd == ':')
3600 cmd = skipwhite(cmd + 1);
3601
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02003602 // Skip "*" used for Visual range.
3603 if (skip_star && *cmd == '*' && vim_strchr(p_cpo, CPO_STAR) == NULL)
3604 cmd = skipwhite(cmd + 1);
3605
Bram Moolenaar071d4272004-06-13 20:20:40 +00003606 return cmd;
3607}
3608
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003609 static void
3610addr_error(cmd_addr_T addr_type)
3611{
3612 if (addr_type == ADDR_NONE)
3613 emsg(_(e_norange));
3614 else
3615 emsg(_(e_invrange));
3616}
3617
Bram Moolenaar071d4272004-06-13 20:20:40 +00003618/*
Bram Moolenaarc2af0af2020-08-23 21:06:02 +02003619 * Return the default address for an address type.
3620 */
3621 static linenr_T
3622default_address(exarg_T *eap)
3623{
3624 linenr_T lnum = 0;
3625
3626 switch (eap->addr_type)
3627 {
3628 case ADDR_LINES:
3629 case ADDR_OTHER:
3630 // Default is the cursor line number. Avoid using an invalid
3631 // line number though.
3632 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
3633 lnum = curbuf->b_ml.ml_line_count;
3634 else
3635 lnum = curwin->w_cursor.lnum;
3636 break;
3637 case ADDR_WINDOWS:
3638 lnum = CURRENT_WIN_NR;
3639 break;
3640 case ADDR_ARGUMENTS:
3641 lnum = curwin->w_arg_idx + 1;
3642 if (lnum > ARGCOUNT)
3643 lnum = ARGCOUNT;
3644 break;
3645 case ADDR_LOADED_BUFFERS:
3646 case ADDR_BUFFERS:
3647 lnum = curbuf->b_fnum;
3648 break;
3649 case ADDR_TABS:
3650 lnum = CURRENT_TAB_NR;
3651 break;
3652 case ADDR_TABS_RELATIVE:
3653 case ADDR_UNSIGNED:
3654 lnum = 1;
3655 break;
3656 case ADDR_QUICKFIX:
3657#ifdef FEAT_QUICKFIX
3658 lnum = qf_get_cur_idx(eap);
3659#endif
3660 break;
3661 case ADDR_QUICKFIX_VALID:
3662#ifdef FEAT_QUICKFIX
3663 lnum = qf_get_cur_valid_idx(eap);
3664#endif
3665 break;
3666 case ADDR_NONE:
3667 // Will give an error later if a range is found.
3668 break;
3669 }
3670 return lnum;
3671}
3672
3673/*
Bram Moolenaar198cb662018-09-06 21:44:17 +02003674 * Get a single EX address.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003675 *
3676 * Set ptr to the next character after the part that was interpreted.
3677 * Set ptr to NULL when an error is encountered.
Bram Moolenaar198cb662018-09-06 21:44:17 +02003678 * This may set the last used search pattern.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003679 *
3680 * Return MAXLNUM when no Ex address was found.
3681 */
3682 static linenr_T
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003683get_address(
3684 exarg_T *eap UNUSED,
3685 char_u **ptr,
Bram Moolenaar1b03a192019-12-08 17:08:29 +01003686 cmd_addr_T addr_type,
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02003687 int skip, // only skip the address, don't use it
3688 int silent, // no errors or side effects
3689 int to_other_file, // flag: may jump to other file
3690 int address_count UNUSED) // 1 for first address, >1 after comma
Bram Moolenaar071d4272004-06-13 20:20:40 +00003691{
3692 int c;
3693 int i;
3694 long n;
3695 char_u *cmd;
3696 pos_T pos;
3697 pos_T *fp;
3698 linenr_T lnum;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003699 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003700
3701 cmd = skipwhite(*ptr);
3702 lnum = MAXLNUM;
3703 do
3704 {
3705 switch (*cmd)
3706 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003707 case '.': // '.' - Cursor position
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003708 ++cmd;
3709 switch (addr_type)
3710 {
3711 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02003712 case ADDR_OTHER:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003713 lnum = curwin->w_cursor.lnum;
3714 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003715 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01003716 lnum = CURRENT_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003717 break;
3718 case ADDR_ARGUMENTS:
3719 lnum = curwin->w_arg_idx + 1;
3720 break;
3721 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003722 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003723 lnum = curbuf->b_fnum;
3724 break;
3725 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01003726 lnum = CURRENT_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003727 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02003728 case ADDR_NONE:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003729 case ADDR_TABS_RELATIVE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02003730 case ADDR_UNSIGNED:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003731 addr_error(addr_type);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01003732 cmd = NULL;
3733 goto error;
3734 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003735 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003736#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02003737 lnum = qf_get_cur_idx(eap);
3738#endif
3739 break;
3740 case ADDR_QUICKFIX_VALID:
3741#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003742 lnum = qf_get_cur_valid_idx(eap);
Bram Moolenaare906c502015-09-09 21:10:39 +02003743#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003744 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003745 }
3746 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003747
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003748 case '$': // '$' - last line
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003749 ++cmd;
3750 switch (addr_type)
3751 {
3752 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02003753 case ADDR_OTHER:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003754 lnum = curbuf->b_ml.ml_line_count;
3755 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003756 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01003757 lnum = LAST_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003758 break;
3759 case ADDR_ARGUMENTS:
3760 lnum = ARGCOUNT;
3761 break;
3762 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003763 buf = lastbuf;
3764 while (buf->b_ml.ml_mfp == NULL)
3765 {
3766 if (buf->b_prev == NULL)
3767 break;
3768 buf = buf->b_prev;
3769 }
3770 lnum = buf->b_fnum;
3771 break;
3772 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003773 lnum = lastbuf->b_fnum;
3774 break;
3775 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01003776 lnum = LAST_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003777 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02003778 case ADDR_NONE:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003779 case ADDR_TABS_RELATIVE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02003780 case ADDR_UNSIGNED:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003781 addr_error(addr_type);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01003782 cmd = NULL;
3783 goto error;
3784 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003785 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003786#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003787 lnum = qf_get_size(eap);
3788 if (lnum == 0)
3789 lnum = 1;
Bram Moolenaare906c502015-09-09 21:10:39 +02003790#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003791 break;
Bram Moolenaar25190db2019-05-04 15:05:28 +02003792 case ADDR_QUICKFIX_VALID:
3793#ifdef FEAT_QUICKFIX
3794 lnum = qf_get_valid_size(eap);
3795 if (lnum == 0)
3796 lnum = 1;
3797#endif
3798 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003799 }
3800 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003801
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003802 case '\'': // ''' - mark
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003803 if (*++cmd == NUL)
3804 {
3805 cmd = NULL;
3806 goto error;
3807 }
3808 if (addr_type != ADDR_LINES)
3809 {
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003810 addr_error(addr_type);
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01003811 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003812 goto error;
3813 }
3814 if (skip)
3815 ++cmd;
3816 else
3817 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003818 // Only accept a mark in another file when it is
3819 // used by itself: ":'M".
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003820 fp = getmark(*cmd, to_other_file && cmd[1] == NUL);
3821 ++cmd;
3822 if (fp == (pos_T *)-1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003823 // Jumped to another file.
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003824 lnum = curwin->w_cursor.lnum;
3825 else
3826 {
3827 if (check_mark(fp) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003828 {
3829 cmd = NULL;
3830 goto error;
3831 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003832 lnum = fp->lnum;
3833 }
3834 }
3835 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003836
3837 case '/':
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003838 case '?': // '/' or '?' - search
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003839 c = *cmd++;
3840 if (addr_type != ADDR_LINES)
3841 {
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003842 addr_error(addr_type);
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01003843 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003844 goto error;
3845 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003846 if (skip) // skip "/pat/"
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003847 {
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02003848 cmd = skip_regexp(cmd, c, (int)p_magic);
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003849 if (*cmd == c)
3850 ++cmd;
3851 }
3852 else
3853 {
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02003854 int flags;
3855
3856 pos = curwin->w_cursor; // save curwin->w_cursor
3857
3858 // When '/' or '?' follows another address, start from
3859 // there.
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003860 if (lnum != MAXLNUM)
3861 curwin->w_cursor.lnum = lnum;
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02003862
3863 // Start a forward search at the end of the line (unless
3864 // before the first line).
3865 // Start a backward search at the start of the line.
3866 // This makes sure we never match in the current
3867 // line, and can match anywhere in the
3868 // next/previous line.
Bram Moolenaar8ada6aa2017-12-19 21:23:21 +01003869 if (c == '/' && curwin->w_cursor.lnum > 0)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003870 curwin->w_cursor.col = MAXCOL;
3871 else
3872 curwin->w_cursor.col = 0;
3873 searchcmdlen = 0;
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02003874 flags = silent ? 0 : SEARCH_HIS | SEARCH_MSG;
Bram Moolenaarc036e872020-02-21 21:30:52 +01003875 if (!do_search(NULL, c, c, cmd, 1L, flags, NULL))
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003876 {
3877 curwin->w_cursor = pos;
3878 cmd = NULL;
3879 goto error;
3880 }
3881 lnum = curwin->w_cursor.lnum;
3882 curwin->w_cursor = pos;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003883 // adjust command string pointer
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003884 cmd += searchcmdlen;
3885 }
3886 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003887
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003888 case '\\': // "\?", "\/" or "\&", repeat search
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003889 ++cmd;
3890 if (addr_type != ADDR_LINES)
3891 {
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003892 addr_error(addr_type);
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01003893 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003894 goto error;
3895 }
3896 if (*cmd == '&')
3897 i = RE_SUBST;
3898 else if (*cmd == '?' || *cmd == '/')
3899 i = RE_SEARCH;
3900 else
3901 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003902 emsg(_(e_backslash));
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003903 cmd = NULL;
3904 goto error;
3905 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003906
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003907 if (!skip)
3908 {
3909 /*
3910 * When search follows another address, start from
3911 * there.
3912 */
3913 if (lnum != MAXLNUM)
3914 pos.lnum = lnum;
3915 else
3916 pos.lnum = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003917
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003918 /*
3919 * Start the search just like for the above
3920 * do_search().
3921 */
3922 if (*cmd != '?')
3923 pos.col = MAXCOL;
3924 else
3925 pos.col = 0;
Bram Moolenaarbd8539a2015-08-11 18:53:03 +02003926 pos.coladd = 0;
Bram Moolenaar5d24a222018-12-23 19:10:09 +01003927 if (searchit(curwin, curbuf, &pos, NULL,
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003928 *cmd == '?' ? BACKWARD : FORWARD,
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02003929 (char_u *)"", 1L, SEARCH_MSG, i, NULL) != FAIL)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003930 lnum = pos.lnum;
3931 else
3932 {
3933 cmd = NULL;
3934 goto error;
3935 }
3936 }
3937 ++cmd;
3938 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003939
3940 default:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003941 if (VIM_ISDIGIT(*cmd)) // absolute line number
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003942 lnum = getdigits(&cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003943 }
3944
3945 for (;;)
3946 {
3947 cmd = skipwhite(cmd);
3948 if (*cmd != '-' && *cmd != '+' && !VIM_ISDIGIT(*cmd))
3949 break;
3950
3951 if (lnum == MAXLNUM)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003952 {
3953 switch (addr_type)
3954 {
3955 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02003956 case ADDR_OTHER:
3957 // "+1" is same as ".+1"
Bram Moolenaarf240e182014-11-27 18:33:02 +01003958 lnum = curwin->w_cursor.lnum;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003959 break;
3960 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01003961 lnum = CURRENT_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003962 break;
3963 case ADDR_ARGUMENTS:
3964 lnum = curwin->w_arg_idx + 1;
3965 break;
3966 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003967 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003968 lnum = curbuf->b_fnum;
3969 break;
3970 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01003971 lnum = CURRENT_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003972 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01003973 case ADDR_TABS_RELATIVE:
3974 lnum = 1;
3975 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003976 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003977#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02003978 lnum = qf_get_cur_idx(eap);
3979#endif
3980 break;
3981 case ADDR_QUICKFIX_VALID:
3982#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003983 lnum = qf_get_cur_valid_idx(eap);
Bram Moolenaare906c502015-09-09 21:10:39 +02003984#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003985 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02003986 case ADDR_NONE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02003987 case ADDR_UNSIGNED:
3988 lnum = 0;
Bram Moolenaarb7316892019-05-01 18:08:42 +02003989 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003990 }
3991 }
3992
Bram Moolenaar071d4272004-06-13 20:20:40 +00003993 if (VIM_ISDIGIT(*cmd))
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003994 i = '+'; // "number" is same as "+number"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003995 else
3996 i = *cmd++;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003997 if (!VIM_ISDIGIT(*cmd)) // '+' is '+1', but '+0' is not '+1'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003998 n = 1;
3999 else
4000 n = getdigits(&cmd);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004001
4002 if (addr_type == ADDR_TABS_RELATIVE)
4003 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004004 emsg(_(e_invrange));
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004005 cmd = NULL;
4006 goto error;
4007 }
4008 else if (addr_type == ADDR_LOADED_BUFFERS
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004009 || addr_type == ADDR_BUFFERS)
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004010 lnum = compute_buffer_local_count(
4011 addr_type, lnum, (i == '-') ? -1 * n : n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004012 else
Bram Moolenaarded27822017-01-02 14:27:34 +01004013 {
4014#ifdef FEAT_FOLDING
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004015 // Relative line addressing, need to adjust for folded lines
4016 // now, but only do it after the first address.
Bram Moolenaarded27822017-01-02 14:27:34 +01004017 if (addr_type == ADDR_LINES && (i == '-' || i == '+')
4018 && address_count >= 2)
4019 (void)hasFolding(lnum, NULL, &lnum);
4020#endif
4021 if (i == '-')
4022 lnum -= n;
4023 else
4024 lnum += n;
4025 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004026 }
4027 } while (*cmd == '/' || *cmd == '?');
4028
4029error:
4030 *ptr = cmd;
4031 return lnum;
4032}
4033
4034/*
Bram Moolenaarc2af0af2020-08-23 21:06:02 +02004035 * Set eap->line1 and eap->line2 to the whole range.
4036 * Used for commands with the EX_DFLALL flag and no range given.
4037 */
4038 static void
4039address_default_all(exarg_T *eap)
4040{
4041 eap->line1 = 1;
4042 switch (eap->addr_type)
4043 {
4044 case ADDR_LINES:
4045 case ADDR_OTHER:
4046 eap->line2 = curbuf->b_ml.ml_line_count;
4047 break;
4048 case ADDR_LOADED_BUFFERS:
4049 {
4050 buf_T *buf = firstbuf;
4051
4052 while (buf->b_next != NULL && buf->b_ml.ml_mfp == NULL)
4053 buf = buf->b_next;
4054 eap->line1 = buf->b_fnum;
4055 buf = lastbuf;
4056 while (buf->b_prev != NULL && buf->b_ml.ml_mfp == NULL)
4057 buf = buf->b_prev;
4058 eap->line2 = buf->b_fnum;
4059 }
4060 break;
4061 case ADDR_BUFFERS:
4062 eap->line1 = firstbuf->b_fnum;
4063 eap->line2 = lastbuf->b_fnum;
4064 break;
4065 case ADDR_WINDOWS:
4066 eap->line2 = LAST_WIN_NR;
4067 break;
4068 case ADDR_TABS:
4069 eap->line2 = LAST_TAB_NR;
4070 break;
4071 case ADDR_TABS_RELATIVE:
4072 eap->line2 = 1;
4073 break;
4074 case ADDR_ARGUMENTS:
4075 if (ARGCOUNT == 0)
4076 eap->line1 = eap->line2 = 0;
4077 else
4078 eap->line2 = ARGCOUNT;
4079 break;
4080 case ADDR_QUICKFIX_VALID:
4081#ifdef FEAT_QUICKFIX
4082 eap->line2 = qf_get_valid_size(eap);
4083 if (eap->line2 == 0)
4084 eap->line2 = 1;
4085#endif
4086 break;
4087 case ADDR_NONE:
4088 case ADDR_UNSIGNED:
4089 case ADDR_QUICKFIX:
4090 iemsg(_("INTERNAL: Cannot use EX_DFLALL with ADDR_NONE, ADDR_UNSIGNED or ADDR_QUICKFIX"));
4091 break;
4092 }
4093}
4094
4095
4096/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00004097 * Get flags from an Ex command argument.
4098 */
4099 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004100get_flags(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00004101{
4102 while (vim_strchr((char_u *)"lp#", *eap->arg) != NULL)
4103 {
4104 if (*eap->arg == 'l')
4105 eap->flags |= EXFLAG_LIST;
4106 else if (*eap->arg == 'p')
4107 eap->flags |= EXFLAG_PRINT;
4108 else
4109 eap->flags |= EXFLAG_NR;
4110 eap->arg = skipwhite(eap->arg + 1);
4111 }
4112}
4113
4114/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004115 * Function called for command which is Not Implemented. NI!
4116 */
4117 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004118ex_ni(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004119{
4120 if (!eap->skip)
Bram Moolenaar8930caa2020-07-23 16:37:03 +02004121 eap->errmsg =
4122 _("E319: Sorry, the command is not available in this version");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004123}
4124
Bram Moolenaar7bb75552007-07-16 18:39:49 +00004125#ifdef HAVE_EX_SCRIPT_NI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004126/*
4127 * Function called for script command which is Not Implemented. NI!
4128 * Skips over ":perl <<EOF" constructs.
4129 */
4130 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004131ex_script_ni(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004132{
4133 if (!eap->skip)
4134 ex_ni(eap);
4135 else
4136 vim_free(script_get(eap, eap->arg));
4137}
4138#endif
4139
4140/*
4141 * Check range in Ex command for validity.
4142 * Return NULL when valid, error message when invalid.
4143 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004144 static char *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004145invalid_range(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004146{
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004147 buf_T *buf;
Bram Moolenaar25190db2019-05-04 15:05:28 +02004148
Bram Moolenaar071d4272004-06-13 20:20:40 +00004149 if ( eap->line1 < 0
4150 || eap->line2 < 0
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004151 || eap->line1 > eap->line2)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004152 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004153
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004154 if (eap->argt & EX_RANGE)
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004155 {
Bram Moolenaarb7316892019-05-01 18:08:42 +02004156 switch (eap->addr_type)
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004157 {
4158 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004159 if (eap->line2 > curbuf->b_ml.ml_line_count
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004160#ifdef FEAT_DIFF
4161 + (eap->cmdidx == CMD_diffget)
4162#endif
4163 )
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004164 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004165 break;
4166 case ADDR_ARGUMENTS:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004167 // add 1 if ARGCOUNT is 0
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004168 if (eap->line2 > ARGCOUNT + (!ARGCOUNT))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004169 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004170 break;
4171 case ADDR_BUFFERS:
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02004172 // Only a boundary check, not whether the buffers actually
4173 // exist.
4174 if (eap->line1 < 1 || eap->line2 > get_highest_fnum())
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004175 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004176 break;
4177 case ADDR_LOADED_BUFFERS:
4178 buf = firstbuf;
4179 while (buf->b_ml.ml_mfp == NULL)
4180 {
4181 if (buf->b_next == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004182 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004183 buf = buf->b_next;
4184 }
4185 if (eap->line1 < buf->b_fnum)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004186 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004187 buf = lastbuf;
4188 while (buf->b_ml.ml_mfp == NULL)
4189 {
4190 if (buf->b_prev == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004191 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004192 buf = buf->b_prev;
4193 }
4194 if (eap->line2 > buf->b_fnum)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004195 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004196 break;
4197 case ADDR_WINDOWS:
Bram Moolenaar8be63882015-01-14 11:25:05 +01004198 if (eap->line2 > LAST_WIN_NR)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004199 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004200 break;
4201 case ADDR_TABS:
4202 if (eap->line2 > LAST_TAB_NR)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004203 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004204 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004205 case ADDR_TABS_RELATIVE:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004206 case ADDR_OTHER:
4207 // Any range is OK.
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004208 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004209 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004210#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02004211 // No error for value that is too big, will use the last entry.
4212 if (eap->line2 <= 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004213 return _(e_invrange);
Bram Moolenaare906c502015-09-09 21:10:39 +02004214#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004215 break;
Bram Moolenaar25190db2019-05-04 15:05:28 +02004216 case ADDR_QUICKFIX_VALID:
4217#ifdef FEAT_QUICKFIX
4218 if ((eap->line2 != 1 && eap->line2 > qf_get_valid_size(eap))
4219 || eap->line2 < 0)
4220 return _(e_invrange);
4221#endif
4222 break;
4223 case ADDR_UNSIGNED:
4224 if (eap->line2 < 0)
4225 return _(e_invrange);
4226 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02004227 case ADDR_NONE:
4228 // Will give an error elsewhere.
4229 break;
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004230 }
4231 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004232 return NULL;
4233}
4234
4235/*
4236 * Correct the range for zero line number, if required.
4237 */
4238 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004239correct_range(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004240{
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004241 if (!(eap->argt & EX_ZEROR)) // zero in range not allowed
Bram Moolenaar071d4272004-06-13 20:20:40 +00004242 {
4243 if (eap->line1 == 0)
4244 eap->line1 = 1;
4245 if (eap->line2 == 0)
4246 eap->line2 = 1;
4247 }
4248}
4249
Bram Moolenaar748bf032005-02-02 23:04:36 +00004250#ifdef FEAT_QUICKFIX
Bram Moolenaar748bf032005-02-02 23:04:36 +00004251/*
4252 * For a ":vimgrep" or ":vimgrepadd" command return a pointer past the
4253 * pattern. Otherwise return eap->arg.
4254 */
4255 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004256skip_grep_pat(exarg_T *eap)
Bram Moolenaar748bf032005-02-02 23:04:36 +00004257{
4258 char_u *p = eap->arg;
4259
Bram Moolenaara37420f2006-02-04 22:37:47 +00004260 if (*p != NUL && (eap->cmdidx == CMD_vimgrep || eap->cmdidx == CMD_lvimgrep
4261 || eap->cmdidx == CMD_vimgrepadd
4262 || eap->cmdidx == CMD_lvimgrepadd
4263 || grep_internal(eap->cmdidx)))
Bram Moolenaar748bf032005-02-02 23:04:36 +00004264 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00004265 p = skip_vimgrep_pat(p, NULL, NULL);
Bram Moolenaar748bf032005-02-02 23:04:36 +00004266 if (p == NULL)
4267 p = eap->arg;
Bram Moolenaar748bf032005-02-02 23:04:36 +00004268 }
4269 return p;
4270}
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004271
4272/*
4273 * For the ":make" and ":grep" commands insert the 'makeprg'/'grepprg' option
4274 * in the command line, so that things like % get expanded.
4275 */
4276 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004277replace_makeprg(exarg_T *eap, char_u *p, char_u **cmdlinep)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004278{
4279 char_u *new_cmdline;
4280 char_u *program;
4281 char_u *pos;
4282 char_u *ptr;
4283 int len;
4284 int i;
4285
4286 /*
4287 * Don't do it when ":vimgrep" is used for ":grep".
4288 */
Bram Moolenaara37420f2006-02-04 22:37:47 +00004289 if ((eap->cmdidx == CMD_make || eap->cmdidx == CMD_lmake
4290 || eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4291 || eap->cmdidx == CMD_grepadd
4292 || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004293 && !grep_internal(eap->cmdidx))
4294 {
Bram Moolenaara37420f2006-02-04 22:37:47 +00004295 if (eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4296 || eap->cmdidx == CMD_grepadd || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004297 {
4298 if (*curbuf->b_p_gp == NUL)
4299 program = p_gp;
4300 else
4301 program = curbuf->b_p_gp;
4302 }
4303 else
4304 {
4305 if (*curbuf->b_p_mp == NUL)
4306 program = p_mp;
4307 else
4308 program = curbuf->b_p_mp;
4309 }
4310
4311 p = skipwhite(p);
4312
4313 if ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4314 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004315 // replace $* by given arguments
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004316 i = 1;
4317 while ((pos = (char_u *)strstr((char *)pos + 2, "$*")) != NULL)
4318 ++i;
4319 len = (int)STRLEN(p);
Bram Moolenaar51e14382019-05-25 20:21:28 +02004320 new_cmdline = alloc(STRLEN(program) + i * (len - 2) + 1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004321 if (new_cmdline == NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004322 return NULL; // out of memory
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004323 ptr = new_cmdline;
4324 while ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4325 {
4326 i = (int)(pos - program);
4327 STRNCPY(ptr, program, i);
4328 STRCPY(ptr += i, p);
4329 ptr += len;
4330 program = pos + 2;
4331 }
4332 STRCPY(ptr, program);
4333 }
4334 else
4335 {
Bram Moolenaar51e14382019-05-25 20:21:28 +02004336 new_cmdline = alloc(STRLEN(program) + STRLEN(p) + 2);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004337 if (new_cmdline == NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004338 return NULL; // out of memory
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004339 STRCPY(new_cmdline, program);
4340 STRCAT(new_cmdline, " ");
4341 STRCAT(new_cmdline, p);
4342 }
4343 msg_make(p);
4344
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004345 // 'eap->cmd' is not set here, because it is not used at CMD_make
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004346 vim_free(*cmdlinep);
4347 *cmdlinep = new_cmdline;
4348 p = new_cmdline;
4349 }
4350 return p;
4351}
Bram Moolenaar748bf032005-02-02 23:04:36 +00004352#endif
4353
Bram Moolenaar071d4272004-06-13 20:20:40 +00004354/*
4355 * Expand file name in Ex command argument.
Bram Moolenaar0abb4272019-06-15 16:06:00 +02004356 * When an error is detected, "errormsgp" is set to a non-NULL pointer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004357 * Return FAIL for failure, OK otherwise.
4358 */
4359 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004360expand_filename(
4361 exarg_T *eap,
4362 char_u **cmdlinep,
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004363 char **errormsgp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004364{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004365 int has_wildcards; // need to expand wildcards
Bram Moolenaar071d4272004-06-13 20:20:40 +00004366 char_u *repl;
4367 int srclen;
4368 char_u *p;
4369 int n;
Bram Moolenaar63b92542007-03-27 14:57:09 +00004370 int escaped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004371
Bram Moolenaar748bf032005-02-02 23:04:36 +00004372#ifdef FEAT_QUICKFIX
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004373 // Skip a regexp pattern for ":vimgrep[add] pat file..."
Bram Moolenaar748bf032005-02-02 23:04:36 +00004374 p = skip_grep_pat(eap);
4375#else
4376 p = eap->arg;
4377#endif
4378
Bram Moolenaar071d4272004-06-13 20:20:40 +00004379 /*
4380 * Decide to expand wildcards *before* replacing '%', '#', etc. If
4381 * the file name contains a wildcard it should not cause expanding.
4382 * (it will be expanded anyway if there is a wildcard before replacing).
4383 */
Bram Moolenaar748bf032005-02-02 23:04:36 +00004384 has_wildcards = mch_has_wildcard(p);
4385 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004386 {
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004387#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004388 // Skip over `=expr`, wildcards in it are not expanded.
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004389 if (p[0] == '`' && p[1] == '=')
4390 {
4391 p += 2;
4392 (void)skip_expr(&p);
4393 if (*p == '`')
4394 ++p;
4395 continue;
4396 }
4397#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004398 /*
4399 * Quick check if this cannot be the start of a special string.
4400 * Also removes backslash before '%', '#' and '<'.
4401 */
4402 if (vim_strchr((char_u *)"%#<", *p) == NULL)
4403 {
4404 ++p;
4405 continue;
4406 }
4407
4408 /*
4409 * Try to find a match at this position.
4410 */
Bram Moolenaar63b92542007-03-27 14:57:09 +00004411 repl = eval_vars(p, eap->arg, &srclen, &(eap->do_ecmd_lnum),
4412 errormsgp, &escaped);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004413 if (*errormsgp != NULL) // error detected
Bram Moolenaar071d4272004-06-13 20:20:40 +00004414 return FAIL;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004415 if (repl == NULL) // no match found
Bram Moolenaar071d4272004-06-13 20:20:40 +00004416 {
4417 p += srclen;
4418 continue;
4419 }
4420
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004421 // Wildcards won't be expanded below, the replacement is taken
4422 // literally. But do expand "~/file", "~user/file" and "$HOME/file".
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00004423 if (vim_strchr(repl, '$') != NULL || vim_strchr(repl, '~') != NULL)
4424 {
4425 char_u *l = repl;
4426
4427 repl = expand_env_save(repl);
4428 vim_free(l);
4429 }
4430
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004431 // Need to escape white space et al. with a backslash.
4432 // Don't do this for:
4433 // - replacement that already has been escaped: "##"
4434 // - shell commands (may have to use quotes instead).
4435 // - non-unix systems when there is a single argument (spaces don't
4436 // separate arguments then).
Bram Moolenaar071d4272004-06-13 20:20:40 +00004437 if (!eap->usefilter
Bram Moolenaar63b92542007-03-27 14:57:09 +00004438 && !escaped
Bram Moolenaar071d4272004-06-13 20:20:40 +00004439 && eap->cmdidx != CMD_bang
Bram Moolenaar071d4272004-06-13 20:20:40 +00004440 && eap->cmdidx != CMD_grep
4441 && eap->cmdidx != CMD_grepadd
Bram Moolenaarbf15b8d2017-06-04 20:43:48 +02004442 && eap->cmdidx != CMD_hardcopy
Bram Moolenaar67883b42017-07-27 22:57:00 +02004443 && eap->cmdidx != CMD_lgrep
4444 && eap->cmdidx != CMD_lgrepadd
4445 && eap->cmdidx != CMD_lmake
4446 && eap->cmdidx != CMD_make
4447 && eap->cmdidx != CMD_terminal
Bram Moolenaar071d4272004-06-13 20:20:40 +00004448#ifndef UNIX
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004449 && !(eap->argt & EX_NOSPC)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004450#endif
4451 )
4452 {
4453 char_u *l;
4454#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004455 // Don't escape a backslash here, because rem_backslash() doesn't
4456 // remove it later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004457 static char_u *nobslash = (char_u *)" \t\"|";
4458# define ESCAPE_CHARS nobslash
4459#else
4460# define ESCAPE_CHARS escape_chars
4461#endif
4462
4463 for (l = repl; *l; ++l)
4464 if (vim_strchr(ESCAPE_CHARS, *l) != NULL)
4465 {
4466 l = vim_strsave_escaped(repl, ESCAPE_CHARS);
4467 if (l != NULL)
4468 {
4469 vim_free(repl);
4470 repl = l;
4471 }
4472 break;
4473 }
4474 }
4475
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004476 // For a shell command a '!' must be escaped.
Bram Moolenaar67883b42017-07-27 22:57:00 +02004477 if ((eap->usefilter || eap->cmdidx == CMD_bang
4478 || eap->cmdidx == CMD_terminal)
Bram Moolenaar31b7d382014-04-01 18:54:48 +02004479 && vim_strpbrk(repl, (char_u *)"!") != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004480 {
4481 char_u *l;
4482
Bram Moolenaar31b7d382014-04-01 18:54:48 +02004483 l = vim_strsave_escaped(repl, (char_u *)"!");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004484 if (l != NULL)
4485 {
4486 vim_free(repl);
4487 repl = l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004488 }
4489 }
4490
4491 p = repl_cmdline(eap, p, srclen, repl, cmdlinep);
4492 vim_free(repl);
4493 if (p == NULL)
4494 return FAIL;
4495 }
4496
4497 /*
4498 * One file argument: Expand wildcards.
4499 * Don't do this with ":r !command" or ":w !command".
4500 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004501 if ((eap->argt & EX_NOSPC) && !eap->usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004502 {
4503 /*
4504 * May do this twice:
4505 * 1. Replace environment variables.
4506 * 2. Replace any other wildcards, remove backslashes.
4507 */
4508 for (n = 1; n <= 2; ++n)
4509 {
4510 if (n == 2)
4511 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004512 /*
4513 * Halve the number of backslashes (this is Vi compatible).
4514 * For Unix and OS/2, when wildcards are expanded, this is
4515 * done by ExpandOne() below.
4516 */
Bram Moolenaare7fedb62015-12-31 19:07:19 +01004517#if defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004518 if (!has_wildcards)
4519#endif
4520 backslash_halve(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004521 }
4522
4523 if (has_wildcards)
4524 {
4525 if (n == 1)
4526 {
4527 /*
4528 * First loop: May expand environment variables. This
4529 * can be done much faster with expand_env() than with
4530 * something else (e.g., calling a shell).
4531 * After expanding environment variables, check again
4532 * if there are still wildcards present.
4533 */
4534 if (vim_strchr(eap->arg, '$') != NULL
4535 || vim_strchr(eap->arg, '~') != NULL)
4536 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +00004537 expand_env_esc(eap->arg, NameBuff, MAXPATHL,
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00004538 TRUE, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004539 has_wildcards = mch_has_wildcard(NameBuff);
4540 p = NameBuff;
4541 }
4542 else
4543 p = NULL;
4544 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004545 else // n == 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00004546 {
4547 expand_T xpc;
Bram Moolenaarb40c2572019-10-19 21:01:05 +02004548 int options = WILD_LIST_NOTFOUND
4549 | WILD_NOERROR | WILD_ADD_SLASH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004550
4551 ExpandInit(&xpc);
4552 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar94950a92010-12-02 16:01:29 +01004553 if (p_wic)
4554 options += WILD_ICASE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004555 p = ExpandOne(&xpc, eap->arg, NULL,
Bram Moolenaar94950a92010-12-02 16:01:29 +01004556 options, WILD_EXPAND_FREE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004557 if (p == NULL)
4558 return FAIL;
4559 }
4560 if (p != NULL)
4561 {
4562 (void)repl_cmdline(eap, eap->arg, (int)STRLEN(eap->arg),
4563 p, cmdlinep);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004564 if (n == 2) // p came from ExpandOne()
Bram Moolenaar071d4272004-06-13 20:20:40 +00004565 vim_free(p);
4566 }
4567 }
4568 }
4569 }
4570 return OK;
4571}
4572
4573/*
4574 * Replace part of the command line, keeping eap->cmd, eap->arg and
4575 * eap->nextcmd correct.
4576 * "src" points to the part that is to be replaced, of length "srclen".
4577 * "repl" is the replacement string.
4578 * Returns a pointer to the character after the replaced string.
4579 * Returns NULL for failure.
4580 */
4581 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004582repl_cmdline(
4583 exarg_T *eap,
4584 char_u *src,
4585 int srclen,
4586 char_u *repl,
4587 char_u **cmdlinep)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004588{
4589 int len;
4590 int i;
4591 char_u *new_cmdline;
4592
4593 /*
4594 * The new command line is build in new_cmdline[].
4595 * First allocate it.
4596 * Careful: a "+cmd" argument may have been NUL terminated.
4597 */
4598 len = (int)STRLEN(repl);
4599 i = (int)(src - *cmdlinep) + (int)STRLEN(src + srclen) + len + 3;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004600 if (eap->nextcmd != NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004601 i += (int)STRLEN(eap->nextcmd);// add space for next command
Bram Moolenaar964b3742019-05-24 18:54:09 +02004602 if ((new_cmdline = alloc(i)) == NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004603 return NULL; // out of memory!
Bram Moolenaar071d4272004-06-13 20:20:40 +00004604
4605 /*
4606 * Copy the stuff before the expanded part.
4607 * Copy the expanded stuff.
4608 * Copy what came after the expanded part.
4609 * Copy the next commands, if there are any.
4610 */
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004611 i = (int)(src - *cmdlinep); // length of part before match
Bram Moolenaar071d4272004-06-13 20:20:40 +00004612 mch_memmove(new_cmdline, *cmdlinep, (size_t)i);
Bram Moolenaara3ffd9c2005-07-21 21:03:15 +00004613
Bram Moolenaar071d4272004-06-13 20:20:40 +00004614 mch_memmove(new_cmdline + i, repl, (size_t)len);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004615 i += len; // remember the end of the string
Bram Moolenaar071d4272004-06-13 20:20:40 +00004616 STRCPY(new_cmdline + i, src + srclen);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004617 src = new_cmdline + i; // remember where to continue
Bram Moolenaar071d4272004-06-13 20:20:40 +00004618
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004619 if (eap->nextcmd != NULL) // append next command
Bram Moolenaar071d4272004-06-13 20:20:40 +00004620 {
4621 i = (int)STRLEN(new_cmdline) + 1;
4622 STRCPY(new_cmdline + i, eap->nextcmd);
4623 eap->nextcmd = new_cmdline + i;
4624 }
4625 eap->cmd = new_cmdline + (eap->cmd - *cmdlinep);
4626 eap->arg = new_cmdline + (eap->arg - *cmdlinep);
4627 if (eap->do_ecmd_cmd != NULL && eap->do_ecmd_cmd != dollar_command)
4628 eap->do_ecmd_cmd = new_cmdline + (eap->do_ecmd_cmd - *cmdlinep);
4629 vim_free(*cmdlinep);
4630 *cmdlinep = new_cmdline;
4631
4632 return src;
4633}
4634
4635/*
4636 * Check for '|' to separate commands and '"' to start comments.
4637 */
4638 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004639separate_nextcmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004640{
4641 char_u *p;
4642
Bram Moolenaar86b68352004-12-27 21:59:20 +00004643#ifdef FEAT_QUICKFIX
Bram Moolenaar748bf032005-02-02 23:04:36 +00004644 p = skip_grep_pat(eap);
4645#else
4646 p = eap->arg;
Bram Moolenaar86b68352004-12-27 21:59:20 +00004647#endif
4648
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004649 for ( ; *p; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004650 {
4651 if (*p == Ctrl_V)
4652 {
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004653 if (eap->argt & (EX_CTRLV | EX_XFILE))
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004654 ++p; // skip CTRL-V and next char
Bram Moolenaar071d4272004-06-13 20:20:40 +00004655 else
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004656 // remove CTRL-V and skip next char
Bram Moolenaara7241f52008-06-24 20:39:31 +00004657 STRMOVE(p, p + 1);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004658 if (*p == NUL) // stop at NUL after CTRL-V
Bram Moolenaar071d4272004-06-13 20:20:40 +00004659 break;
4660 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004661
4662#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004663 // Skip over `=expr` when wildcards are expanded.
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004664 else if (p[0] == '`' && p[1] == '=' && (eap->argt & EX_XFILE))
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004665 {
4666 p += 2;
4667 (void)skip_expr(&p);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004668 }
4669#endif
4670
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004671 // Check for '"': start of comment or '|': next command
4672 // :@" and :*" do not start a comment!
4673 // :redir @" doesn't either.
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02004674 else if ((*p == '"'
Bram Moolenaara26b9702020-04-18 19:53:28 +02004675#ifdef FEAT_EVAL
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02004676 && !in_vim9script()
4677#endif
4678 && !(eap->argt & EX_NOTRLCOM)
4679 && ((eap->cmdidx != CMD_at && eap->cmdidx != CMD_star)
4680 || p != eap->arg)
4681 && (eap->cmdidx != CMD_redir
4682 || p != eap->arg + 1 || p[-1] != '@'))
4683#ifdef FEAT_EVAL
4684 || (*p == '#'
4685 && in_vim9script()
Bram Moolenaarb8a92962020-08-20 18:02:47 +02004686 && !(eap->argt & EX_NOTRLCOM)
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02004687 && p[1] != '{'
4688 && p > eap->cmd && VIM_ISWHITE(p[-1]))
Bram Moolenaara26b9702020-04-18 19:53:28 +02004689#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004690 || *p == '|' || *p == '\n')
4691 {
4692 /*
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004693 * We remove the '\' before the '|', unless EX_CTRLV is used
Bram Moolenaar071d4272004-06-13 20:20:40 +00004694 * AND 'b' is present in 'cpoptions'.
4695 */
4696 if ((vim_strchr(p_cpo, CPO_BAR) == NULL
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004697 || !(eap->argt & EX_CTRLV)) && *(p - 1) == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004698 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004699 STRMOVE(p - 1, p); // remove the '\'
Bram Moolenaar071d4272004-06-13 20:20:40 +00004700 --p;
4701 }
4702 else
4703 {
4704 eap->nextcmd = check_nextcmd(p);
4705 *p = NUL;
4706 break;
4707 }
4708 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004709 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00004710
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004711 if (!(eap->argt & EX_NOTRLCOM)) // remove trailing spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00004712 del_trailing_spaces(eap->arg);
4713}
4714
4715/*
4716 * get + command from ex argument
4717 */
4718 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004719getargcmd(char_u **argp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004720{
4721 char_u *arg = *argp;
4722 char_u *command = NULL;
4723
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004724 if (*arg == '+') // +[command]
Bram Moolenaar071d4272004-06-13 20:20:40 +00004725 {
4726 ++arg;
Bram Moolenaar3e451592014-04-02 14:22:05 +02004727 if (vim_isspace(*arg) || *arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004728 command = dollar_command;
4729 else
4730 {
4731 command = arg;
4732 arg = skip_cmd_arg(command, TRUE);
4733 if (*arg != NUL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004734 *arg++ = NUL; // terminate command with NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004735 }
4736
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004737 arg = skipwhite(arg); // skip over spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00004738 *argp = arg;
4739 }
4740 return command;
4741}
4742
4743/*
4744 * Find end of "+command" argument. Skip over "\ " and "\\".
4745 */
Bram Moolenaard0190392019-08-23 21:17:35 +02004746 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004747skip_cmd_arg(
4748 char_u *p,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004749 int rembs) // TRUE to halve the number of backslashes
Bram Moolenaar071d4272004-06-13 20:20:40 +00004750{
4751 while (*p && !vim_isspace(*p))
4752 {
4753 if (*p == '\\' && p[1] != NUL)
4754 {
4755 if (rembs)
Bram Moolenaara7241f52008-06-24 20:39:31 +00004756 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004757 else
4758 ++p;
4759 }
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004760 MB_PTR_ADV(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004761 }
4762 return p;
4763}
4764
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004765 int
4766get_bad_opt(char_u *p, exarg_T *eap)
4767{
4768 if (STRICMP(p, "keep") == 0)
4769 eap->bad_char = BAD_KEEP;
4770 else if (STRICMP(p, "drop") == 0)
4771 eap->bad_char = BAD_DROP;
4772 else if (MB_BYTE2LEN(*p) == 1 && p[1] == NUL)
4773 eap->bad_char = *p;
Bram Moolenaar75808492018-06-12 12:39:41 +02004774 else
4775 return FAIL;
4776 return OK;
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004777}
4778
Bram Moolenaar071d4272004-06-13 20:20:40 +00004779/*
4780 * Get "++opt=arg" argument.
4781 * Return FAIL or OK.
4782 */
4783 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004784getargopt(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004785{
4786 char_u *arg = eap->arg + 2;
4787 int *pp = NULL;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02004788 int bad_char_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004789 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004790
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004791 // ":edit ++[no]bin[ary] file"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004792 if (STRNCMP(arg, "bin", 3) == 0 || STRNCMP(arg, "nobin", 5) == 0)
4793 {
4794 if (*arg == 'n')
4795 {
4796 arg += 2;
4797 eap->force_bin = FORCE_NOBIN;
4798 }
4799 else
4800 eap->force_bin = FORCE_BIN;
4801 if (!checkforcmd(&arg, "binary", 3))
4802 return FAIL;
4803 eap->arg = skipwhite(arg);
4804 return OK;
4805 }
4806
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004807 // ":read ++edit file"
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004808 if (STRNCMP(arg, "edit", 4) == 0)
4809 {
4810 eap->read_edit = TRUE;
4811 eap->arg = skipwhite(arg + 4);
4812 return OK;
4813 }
4814
Bram Moolenaar071d4272004-06-13 20:20:40 +00004815 if (STRNCMP(arg, "ff", 2) == 0)
4816 {
4817 arg += 2;
4818 pp = &eap->force_ff;
4819 }
4820 else if (STRNCMP(arg, "fileformat", 10) == 0)
4821 {
4822 arg += 10;
4823 pp = &eap->force_ff;
4824 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004825 else if (STRNCMP(arg, "enc", 3) == 0)
4826 {
Bram Moolenaarb38e9ab2011-12-14 14:49:45 +01004827 if (STRNCMP(arg, "encoding", 8) == 0)
4828 arg += 8;
4829 else
4830 arg += 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004831 pp = &eap->force_enc;
4832 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004833 else if (STRNCMP(arg, "bad", 3) == 0)
4834 {
4835 arg += 3;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02004836 pp = &bad_char_idx;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004837 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004838
4839 if (pp == NULL || *arg != '=')
4840 return FAIL;
4841
4842 ++arg;
4843 *pp = (int)(arg - eap->cmd);
4844 arg = skip_cmd_arg(arg, FALSE);
4845 eap->arg = skipwhite(arg);
4846 *arg = NUL;
4847
Bram Moolenaar071d4272004-06-13 20:20:40 +00004848 if (pp == &eap->force_ff)
4849 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004850 if (check_ff_value(eap->cmd + eap->force_ff) == FAIL)
4851 return FAIL;
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004852 eap->force_ff = eap->cmd[eap->force_ff];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004853 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004854 else if (pp == &eap->force_enc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004855 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004856 // Make 'fileencoding' lower case.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004857 for (p = eap->cmd + eap->force_enc; *p != NUL; ++p)
4858 *p = TOLOWER_ASC(*p);
4859 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004860 else
4861 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004862 // Check ++bad= argument. Must be a single-byte character, "keep" or
4863 // "drop".
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004864 if (get_bad_opt(eap->cmd + bad_char_idx, eap) == FAIL)
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004865 return FAIL;
4866 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004867
4868 return OK;
4869}
4870
Bram Moolenaar071d4272004-06-13 20:20:40 +00004871 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004872ex_autocmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004873{
4874 /*
Bram Moolenaar26d4b892018-07-04 22:26:28 +02004875 * Disallow autocommands from .exrc and .vimrc in current
Bram Moolenaar071d4272004-06-13 20:20:40 +00004876 * directory for security reasons.
4877 */
4878 if (secure)
4879 {
4880 secure = 2;
Bram Moolenaar8930caa2020-07-23 16:37:03 +02004881 eap->errmsg = _(e_curdir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004882 }
4883 else if (eap->cmdidx == CMD_autocmd)
4884 do_autocmd(eap->arg, eap->forceit);
4885 else
4886 do_augroup(eap->arg, eap->forceit);
4887}
4888
4889/*
4890 * ":doautocmd": Apply the automatic commands to the current buffer.
4891 */
4892 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004893ex_doautocmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004894{
Bram Moolenaar60542ac2012-02-12 20:14:01 +01004895 char_u *arg = eap->arg;
4896 int call_do_modelines = check_nomodeline(&arg);
Bram Moolenaar1610d052016-06-09 22:53:01 +02004897 int did_aucmd;
Bram Moolenaar60542ac2012-02-12 20:14:01 +01004898
Bram Moolenaar1610d052016-06-09 22:53:01 +02004899 (void)do_doautocmd(arg, TRUE, &did_aucmd);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004900 // Only when there is no <nomodeline>.
Bram Moolenaar1610d052016-06-09 22:53:01 +02004901 if (call_do_modelines && did_aucmd)
Bram Moolenaar60542ac2012-02-12 20:14:01 +01004902 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004903}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004904
Bram Moolenaar071d4272004-06-13 20:20:40 +00004905/*
4906 * :[N]bunload[!] [N] [bufname] unload buffer
4907 * :[N]bdelete[!] [N] [bufname] delete buffer from buffer list
4908 * :[N]bwipeout[!] [N] [bufname] delete buffer really
4909 */
4910 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004911ex_bunload(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004912{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01004913 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02004914 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004915 eap->errmsg = do_bufdel(
4916 eap->cmdidx == CMD_bdelete ? DOBUF_DEL
4917 : eap->cmdidx == CMD_bwipeout ? DOBUF_WIPE
4918 : DOBUF_UNLOAD, eap->arg,
4919 eap->addr_count, (int)eap->line1, (int)eap->line2, eap->forceit);
4920}
4921
4922/*
4923 * :[N]buffer [N] to buffer N
4924 * :[N]sbuffer [N] to buffer N
4925 */
4926 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004927ex_buffer(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004928{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01004929 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02004930 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004931 if (*eap->arg)
Bram Moolenaar8930caa2020-07-23 16:37:03 +02004932 eap->errmsg = ex_errmsg(e_trailing_arg, eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004933 else
4934 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004935 if (eap->addr_count == 0) // default is current buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00004936 goto_buffer(eap, DOBUF_CURRENT, FORWARD, 0);
4937 else
4938 goto_buffer(eap, DOBUF_FIRST, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02004939 if (eap->do_ecmd_cmd != NULL)
4940 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004941 }
4942}
4943
4944/*
4945 * :[N]bmodified [N] to next mod. buffer
4946 * :[N]sbmodified [N] to next mod. buffer
4947 */
4948 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004949ex_bmodified(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004950{
4951 goto_buffer(eap, DOBUF_MOD, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02004952 if (eap->do_ecmd_cmd != NULL)
4953 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004954}
4955
4956/*
4957 * :[N]bnext [N] to next buffer
4958 * :[N]sbnext [N] split and to next buffer
4959 */
4960 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004961ex_bnext(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004962{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01004963 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar3029bcc2020-01-18 15:06:19 +01004964 return;
4965
Bram Moolenaar071d4272004-06-13 20:20:40 +00004966 goto_buffer(eap, DOBUF_CURRENT, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02004967 if (eap->do_ecmd_cmd != NULL)
4968 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004969}
4970
4971/*
4972 * :[N]bNext [N] to previous buffer
4973 * :[N]bprevious [N] to previous buffer
4974 * :[N]sbNext [N] split and to previous buffer
4975 * :[N]sbprevious [N] split and to previous buffer
4976 */
4977 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004978ex_bprevious(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004979{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01004980 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar3029bcc2020-01-18 15:06:19 +01004981 return;
4982
Bram Moolenaar071d4272004-06-13 20:20:40 +00004983 goto_buffer(eap, DOBUF_CURRENT, BACKWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02004984 if (eap->do_ecmd_cmd != NULL)
4985 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004986}
4987
4988/*
4989 * :brewind to first buffer
4990 * :bfirst to first buffer
4991 * :sbrewind split and to first buffer
4992 * :sbfirst split and to first buffer
4993 */
4994 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004995ex_brewind(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004996{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01004997 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar3029bcc2020-01-18 15:06:19 +01004998 return;
4999
Bram Moolenaar071d4272004-06-13 20:20:40 +00005000 goto_buffer(eap, DOBUF_FIRST, FORWARD, 0);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005001 if (eap->do_ecmd_cmd != NULL)
5002 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005003}
5004
5005/*
5006 * :blast to last buffer
5007 * :sblast split and to last buffer
5008 */
5009 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005010ex_blast(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005011{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01005012 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar3029bcc2020-01-18 15:06:19 +01005013 return;
5014
Bram Moolenaar071d4272004-06-13 20:20:40 +00005015 goto_buffer(eap, DOBUF_LAST, BACKWARD, 0);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005016 if (eap->do_ecmd_cmd != NULL)
5017 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005018}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005019
Bram Moolenaar7a092242020-04-16 22:10:49 +02005020/*
5021 * Check if "c" ends an Ex command.
Bram Moolenaara494f562020-04-18 17:45:38 +02005022 * In Vim9 script does not check for white space before # or #{.
Bram Moolenaar7a092242020-04-16 22:10:49 +02005023 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005024 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005025ends_excmd(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005026{
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005027 int comment_char = '"';
5028
Bram Moolenaarfbda69b2020-04-13 15:06:53 +02005029#ifdef FEAT_EVAL
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005030 if (in_vim9script())
5031 comment_char = '#';
Bram Moolenaar7a092242020-04-16 22:10:49 +02005032#endif
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005033 return (c == NUL || c == '|' || c == comment_char || c == '\n');
Bram Moolenaar7a092242020-04-16 22:10:49 +02005034}
5035
5036/*
5037 * Like ends_excmd() but checks that a # in Vim9 script either has "cmd" equal
5038 * to "cmd_start" or has a white space character before it.
5039 */
5040 int
Bram Moolenaar21cfe502020-04-16 23:01:50 +02005041ends_excmd2(char_u *cmd_start UNUSED, char_u *cmd)
Bram Moolenaar7a092242020-04-16 22:10:49 +02005042{
5043 int c = *cmd;
5044
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005045 if (c == NUL || c == '|' || c == '\n')
5046 return TRUE;
Bram Moolenaar7a092242020-04-16 22:10:49 +02005047#ifdef FEAT_EVAL
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005048 if (in_vim9script())
5049 return c == '#' && cmd[1] != '{'
5050 && (cmd == cmd_start || VIM_ISWHITE(cmd[-1]));
Bram Moolenaarfbda69b2020-04-13 15:06:53 +02005051#endif
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005052 return c == '"';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005053}
5054
5055#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA) || defined(FEAT_EVAL) \
5056 || defined(PROTO)
5057/*
5058 * Return the next command, after the first '|' or '\n'.
5059 * Return NULL if not found.
5060 */
5061 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005062find_nextcmd(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005063{
5064 while (*p != '|' && *p != '\n')
5065 {
5066 if (*p == NUL)
5067 return NULL;
5068 ++p;
5069 }
5070 return (p + 1);
5071}
5072#endif
5073
5074/*
Bram Moolenaar2256c992016-11-15 21:17:07 +01005075 * Check if *p is a separator between Ex commands, skipping over white space.
5076 * Return NULL if it isn't, the following character if it is.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005077 */
5078 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005079check_nextcmd(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005080{
Bram Moolenaar2256c992016-11-15 21:17:07 +01005081 char_u *s = skipwhite(p);
5082
5083 if (*s == '|' || *s == '\n')
5084 return (s + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005085 else
5086 return NULL;
5087}
5088
5089/*
5090 * - if there are more files to edit
5091 * - and this is the last window
5092 * - and forceit not used
5093 * - and not repeated twice on a row
5094 * return FAIL and give error message if 'message' TRUE
5095 * return OK otherwise
5096 */
5097 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005098check_more(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005099 int message, // when FALSE check only, no messages
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005100 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005101{
5102 int n = ARGCOUNT - curwin->w_arg_idx - 1;
5103
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005104 if (!forceit && only_one_window()
Bram Moolenaar7b221172020-08-17 19:34:10 +02005105 && ARGCOUNT > 1 && !arg_had_last && n > 0 && quitmore == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005106 {
5107 if (message)
5108 {
5109#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
5110 if ((p_confirm || cmdmod.confirm) && curbuf->b_fname != NULL)
5111 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02005112 char_u buff[DIALOG_MSG_SIZE];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005113
Bram Moolenaarda6e8912018-08-21 15:12:14 +02005114 vim_snprintf((char *)buff, DIALOG_MSG_SIZE,
5115 NGETTEXT("%d more file to edit. Quit anyway?",
5116 "%d more files to edit. Quit anyway?", n), n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005117 if (vim_dialog_yesno(VIM_QUESTION, NULL, buff, 1) == VIM_YES)
5118 return OK;
5119 return FAIL;
5120 }
5121#endif
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01005122 semsg(NGETTEXT("E173: %d more file to edit",
5123 "E173: %d more files to edit", n), n);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005124 quitmore = 2; // next try to quit is allowed
Bram Moolenaar071d4272004-06-13 20:20:40 +00005125 }
5126 return FAIL;
5127 }
5128 return OK;
5129}
5130
Bram Moolenaar071d4272004-06-13 20:20:40 +00005131/*
5132 * Function given to ExpandGeneric() to obtain the list of command names.
5133 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005134 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005135get_command_name(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005136{
5137 if (idx >= (int)CMD_SIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005138 return get_user_command_name(idx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005139 return cmdnames[idx].cmd_name;
5140}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005141
Bram Moolenaar071d4272004-06-13 20:20:40 +00005142 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005143ex_colorscheme(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005144{
Bram Moolenaare6850792010-05-14 15:28:44 +02005145 if (*eap->arg == NUL)
5146 {
5147#ifdef FEAT_EVAL
5148 char_u *expr = vim_strsave((char_u *)"g:colors_name");
5149 char_u *p = NULL;
5150
5151 if (expr != NULL)
5152 {
5153 ++emsg_off;
Bram Moolenaarb171fb12020-06-24 20:34:03 +02005154 p = eval_to_string(expr, FALSE);
Bram Moolenaare6850792010-05-14 15:28:44 +02005155 --emsg_off;
5156 vim_free(expr);
5157 }
5158 if (p != NULL)
5159 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01005160 msg((char *)p);
Bram Moolenaare6850792010-05-14 15:28:44 +02005161 vim_free(p);
5162 }
5163 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01005164 msg("default");
Bram Moolenaare6850792010-05-14 15:28:44 +02005165#else
Bram Moolenaar32526b32019-01-19 17:43:09 +01005166 msg(_("unknown"));
Bram Moolenaare6850792010-05-14 15:28:44 +02005167#endif
5168 }
5169 else if (load_colors(eap->arg) == FAIL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005170 semsg(_("E185: Cannot find color scheme '%s'"), eap->arg);
Bram Moolenaarf58d81a2019-01-28 20:19:05 +01005171
5172#ifdef FEAT_VTP
5173 else if (has_vtp_working())
5174 {
5175 // background color change requires clear + redraw
5176 update_screen(CLEAR);
5177 redrawcmd();
5178 }
5179#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005180}
5181
5182 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005183ex_highlight(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005184{
5185 if (*eap->arg == NUL && eap->cmd[2] == '!')
Bram Moolenaar32526b32019-01-19 17:43:09 +01005186 msg(_("Greetings, Vim user!"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005187 do_highlight(eap->arg, eap->forceit, FALSE);
5188}
5189
5190
5191/*
5192 * Call this function if we thought we were going to exit, but we won't
5193 * (because of an error). May need to restore the terminal mode.
5194 */
5195 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005196not_exiting(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005197{
5198 exiting = FALSE;
5199 settmode(TMODE_RAW);
5200}
5201
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005202 static int
5203before_quit_autocmds(win_T *wp, int quit_all, int forceit)
5204{
5205 apply_autocmds(EVENT_QUITPRE, NULL, NULL, FALSE, wp->w_buffer);
5206
Bram Moolenaar34ba06b2019-10-20 22:27:10 +02005207 // Bail out when autocommands closed the window.
5208 // Refuse to quit when the buffer in the last window is being closed (can
5209 // only happen in autocommands).
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005210 if (!win_valid(wp)
5211 || curbuf_locked()
5212 || (wp->w_buffer->b_nwindows == 1 && wp->w_buffer->b_locked > 0))
5213 return TRUE;
5214
5215 if (quit_all || (check_more(FALSE, forceit) == OK && only_one_window()))
5216 {
5217 apply_autocmds(EVENT_EXITPRE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar34ba06b2019-10-20 22:27:10 +02005218 // Refuse to quit when locked or when the window was closed or the
5219 // buffer in the last window is being closed (can only happen in
5220 // autocommands).
5221 if (!win_valid(wp) || curbuf_locked()
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005222 || (curbuf->b_nwindows == 1 && curbuf->b_locked > 0))
5223 return TRUE;
5224 }
5225
5226 return FALSE;
5227}
5228
Bram Moolenaar071d4272004-06-13 20:20:40 +00005229/*
Bram Moolenaarf240e182014-11-27 18:33:02 +01005230 * ":quit": quit current window, quit Vim if the last window is closed.
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005231 * ":{nr}quit": quit window {nr}
Bram Moolenaar4d14bac2019-10-20 21:15:15 +02005232 * Also used when closing a terminal window that's the last one.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005233 */
Bram Moolenaar4d14bac2019-10-20 21:15:15 +02005234 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005235ex_quit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005236{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005237 win_T *wp;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005238
Bram Moolenaar071d4272004-06-13 20:20:40 +00005239#ifdef FEAT_CMDWIN
5240 if (cmdwin_type != 0)
5241 {
5242 cmdwin_result = Ctrl_C;
5243 return;
5244 }
5245#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005246 // Don't quit while editing the command line.
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005247 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005248 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005249 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005250 return;
5251 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005252 if (eap->addr_count > 0)
5253 {
Bram Moolenaarf240e182014-11-27 18:33:02 +01005254 int wnr = eap->line2;
5255
5256 for (wp = firstwin; wp->w_next != NULL; wp = wp->w_next)
5257 if (--wnr <= 0)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005258 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005259 }
5260 else
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005261 wp = curwin;
Bram Moolenaarf240e182014-11-27 18:33:02 +01005262
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005263 // Refuse to quit when locked.
Bram Moolenaar87ffb5c2017-10-19 12:37:42 +02005264 if (curbuf_locked())
5265 return;
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005266
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005267 // Trigger QuitPre and maybe ExitPre
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005268 if (before_quit_autocmds(wp, FALSE, eap->forceit))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005269 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005270
5271#ifdef FEAT_NETBEANS_INTG
5272 netbeansForcedQuit = eap->forceit;
5273#endif
5274
5275 /*
5276 * If there are more files or windows we won't exit.
5277 */
5278 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
5279 exiting = TRUE;
Bram Moolenaarff930ca2017-10-19 17:12:10 +02005280 if ((!buf_hide(wp->w_buffer)
5281 && check_changed(wp->w_buffer, (p_awa ? CCGD_AW : 0)
Bram Moolenaar45d3b142013-11-09 03:31:51 +01005282 | (eap->forceit ? CCGD_FORCEIT : 0)
5283 | CCGD_EXCMD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005284 || check_more(TRUE, eap->forceit) == FAIL
Bram Moolenaar027387f2016-01-02 22:25:52 +01005285 || (only_one_window() && check_changed_any(eap->forceit, TRUE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005286 {
5287 not_exiting();
5288 }
5289 else
5290 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005291 // quit last window
5292 // Note: only_one_window() returns true, even so a help window is
5293 // still open. In that case only quit, if no address has been
5294 // specified. Example:
5295 // :h|wincmd w|1q - don't quit
5296 // :h|wincmd w|q - quit
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01005297 if (only_one_window() && (ONE_WINDOW || eap->addr_count == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005298 getout(0);
Bram Moolenaar2c33d7b2017-10-14 16:06:20 +02005299 not_exiting();
Bram Moolenaar4033c552017-09-16 20:54:51 +02005300#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00005301 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005302#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005303 // close window; may free buffer
Bram Moolenaareb44a682017-08-03 22:44:55 +02005304 win_close(wp, !buf_hide(wp->w_buffer) || eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005305 }
5306}
5307
5308/*
5309 * ":cquit".
5310 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005311 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005312ex_cquit(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005313{
Bram Moolenaar1860bde2020-01-06 21:47:21 +01005314 // this does not always pass on the exit code to the Manx compiler. why?
5315 getout(eap->addr_count > 0 ? (int)eap->line2 : EXIT_FAILURE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005316}
5317
5318/*
5319 * ":qall": try to quit all windows
5320 */
5321 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005322ex_quit_all(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005323{
5324# ifdef FEAT_CMDWIN
5325 if (cmdwin_type != 0)
5326 {
5327 if (eap->forceit)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005328 cmdwin_result = K_XF1; // ex_window() takes care of this
Bram Moolenaar071d4272004-06-13 20:20:40 +00005329 else
5330 cmdwin_result = K_XF2;
5331 return;
5332 }
5333# endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005334
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005335 // Don't quit while editing the command line.
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005336 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005337 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005338 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005339 return;
5340 }
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005341
5342 if (before_quit_autocmds(curwin, TRUE, eap->forceit))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005343 return;
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005344
Bram Moolenaar071d4272004-06-13 20:20:40 +00005345 exiting = TRUE;
Bram Moolenaar027387f2016-01-02 22:25:52 +01005346 if (eap->forceit || !check_changed_any(FALSE, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005347 getout(0);
5348 not_exiting();
5349}
5350
Bram Moolenaar071d4272004-06-13 20:20:40 +00005351/*
5352 * ":close": close current window, unless it is the last one
5353 */
5354 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005355ex_close(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005356{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005357 win_T *win;
5358 int winnr = 0;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005359#ifdef FEAT_CMDWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00005360 if (cmdwin_type != 0)
Bram Moolenaar9bd1a7e2011-05-19 14:50:54 +02005361 cmdwin_result = Ctrl_C;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005362 else
Bram Moolenaar4033c552017-09-16 20:54:51 +02005363#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005364 if (!text_locked() && !curbuf_locked())
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005365 {
5366 if (eap->addr_count == 0)
5367 ex_win_close(eap->forceit, curwin, NULL);
Bram Moolenaar6d41c782018-06-06 09:11:12 +02005368 else
5369 {
Bram Moolenaar29323592016-07-24 22:04:11 +02005370 FOR_ALL_WINDOWS(win)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005371 {
5372 winnr++;
5373 if (winnr == eap->line2)
5374 break;
5375 }
5376 if (win == NULL)
5377 win = lastwin;
5378 ex_win_close(eap->forceit, win, NULL);
5379 }
5380 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005381}
5382
Bram Moolenaar4033c552017-09-16 20:54:51 +02005383#ifdef FEAT_QUICKFIX
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005384/*
5385 * ":pclose": Close any preview window.
5386 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005387 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005388ex_pclose(exarg_T *eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005389{
5390 win_T *win;
5391
Bram Moolenaar79648732019-07-18 21:43:07 +02005392 // First close any normal window.
Bram Moolenaar29323592016-07-24 22:04:11 +02005393 FOR_ALL_WINDOWS(win)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005394 if (win->w_p_pvw)
5395 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00005396 ex_win_close(eap->forceit, win, NULL);
Bram Moolenaar79648732019-07-18 21:43:07 +02005397 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005398 }
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01005399# ifdef FEAT_PROP_POPUP
Bram Moolenaar79648732019-07-18 21:43:07 +02005400 // Also when 'previewpopup' is empty, it might have been cleared.
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02005401 popup_close_preview();
Bram Moolenaar79648732019-07-18 21:43:07 +02005402# endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005403}
Bram Moolenaar4033c552017-09-16 20:54:51 +02005404#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005405
Bram Moolenaarf740b292006-02-16 22:11:02 +00005406/*
5407 * Close window "win" and take care of handling closing the last window for a
5408 * modified buffer.
5409 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005410 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005411ex_win_close(
5412 int forceit,
5413 win_T *win,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005414 tabpage_T *tp) // NULL or the tab page "win" is in
Bram Moolenaar071d4272004-06-13 20:20:40 +00005415{
5416 int need_hide;
5417 buf_T *buf = win->w_buffer;
5418
Bram Moolenaarcf844172020-06-26 19:44:06 +02005419 // Never close the autocommand window.
5420 if (win == aucmd_win)
5421 {
5422 emsg(_(e_autocmd_close));
5423 return;
5424 }
5425
Bram Moolenaar071d4272004-06-13 20:20:40 +00005426 need_hide = (bufIsChanged(buf) && buf->b_nwindows <= 1);
Bram Moolenaareb44a682017-08-03 22:44:55 +02005427 if (need_hide && !buf_hide(buf) && !forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005428 {
Bram Moolenaar4033c552017-09-16 20:54:51 +02005429#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005430 if ((p_confirm || cmdmod.confirm) && p_write)
5431 {
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005432 bufref_T bufref;
5433
5434 set_bufref(&bufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005435 dialog_changed(buf, FALSE);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005436 if (bufref_valid(&bufref) && bufIsChanged(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005437 return;
5438 need_hide = FALSE;
5439 }
5440 else
Bram Moolenaar4033c552017-09-16 20:54:51 +02005441#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005442 {
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02005443 no_write_message();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005444 return;
5445 }
5446 }
5447
Bram Moolenaar4033c552017-09-16 20:54:51 +02005448#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00005449 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005450#endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00005451
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005452 // free buffer when not hiding it or when it's a scratch buffer
Bram Moolenaarf740b292006-02-16 22:11:02 +00005453 if (tp == NULL)
Bram Moolenaareb44a682017-08-03 22:44:55 +02005454 win_close(win, !need_hide && !buf_hide(buf));
Bram Moolenaarf740b292006-02-16 22:11:02 +00005455 else
Bram Moolenaareb44a682017-08-03 22:44:55 +02005456 win_close_othertab(win, !need_hide && !buf_hide(buf), tp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005457}
5458
Bram Moolenaar071d4272004-06-13 20:20:40 +00005459/*
Bram Moolenaardea25702017-01-29 15:18:10 +01005460 * Handle the argument for a tabpage related ex command.
5461 * Returns a tabpage number.
5462 * When an error is encountered then eap->errmsg is set.
5463 */
5464 static int
5465get_tabpage_arg(exarg_T *eap)
5466{
5467 int tab_number;
5468 int unaccept_arg0 = (eap->cmdidx == CMD_tabmove) ? 0 : 1;
5469
5470 if (eap->arg && *eap->arg != NUL)
5471 {
5472 char_u *p = eap->arg;
5473 char_u *p_save;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005474 int relative = 0; // argument +N/-N means: go to N places to the
5475 // right/left relative to the current position.
Bram Moolenaardea25702017-01-29 15:18:10 +01005476
5477 if (*p == '-')
5478 {
5479 relative = -1;
5480 p++;
5481 }
5482 else if (*p == '+')
5483 {
5484 relative = 1;
5485 p++;
5486 }
5487
5488 p_save = p;
5489 tab_number = getdigits(&p);
5490
5491 if (relative == 0)
5492 {
5493 if (STRCMP(p, "$") == 0)
5494 tab_number = LAST_TAB_NR;
Bram Moolenaar94f4ffa2020-08-10 19:21:15 +02005495 else if (STRCMP(p, "#") == 0)
5496 if (valid_tabpage(lastused_tabpage))
5497 tab_number = tabpage_index(lastused_tabpage);
5498 else
5499 {
5500 eap->errmsg = ex_errmsg(e_invargval, eap->arg);
5501 tab_number = 0;
5502 goto theend;
5503 }
Bram Moolenaardea25702017-01-29 15:18:10 +01005504 else if (p == p_save || *p_save == '-' || *p != NUL
5505 || tab_number > LAST_TAB_NR)
5506 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005507 // No numbers as argument.
Bram Moolenaar8930caa2020-07-23 16:37:03 +02005508 eap->errmsg = ex_errmsg(e_invarg2, eap->arg);
Bram Moolenaardea25702017-01-29 15:18:10 +01005509 goto theend;
5510 }
5511 }
5512 else
5513 {
5514 if (*p_save == NUL)
5515 tab_number = 1;
5516 else if (p == p_save || *p_save == '-' || *p != NUL
5517 || tab_number == 0)
5518 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005519 // No numbers as argument.
Bram Moolenaar8930caa2020-07-23 16:37:03 +02005520 eap->errmsg = ex_errmsg(e_invarg2, eap->arg);
Bram Moolenaardea25702017-01-29 15:18:10 +01005521 goto theend;
5522 }
5523 tab_number = tab_number * relative + tabpage_index(curtab);
5524 if (!unaccept_arg0 && relative == -1)
5525 --tab_number;
5526 }
5527 if (tab_number < unaccept_arg0 || tab_number > LAST_TAB_NR)
Bram Moolenaar8930caa2020-07-23 16:37:03 +02005528 eap->errmsg = ex_errmsg(e_invarg2, eap->arg);
Bram Moolenaardea25702017-01-29 15:18:10 +01005529 }
5530 else if (eap->addr_count > 0)
5531 {
5532 if (unaccept_arg0 && eap->line2 == 0)
Bram Moolenaarc6251552017-01-29 21:42:20 +01005533 {
Bram Moolenaar8930caa2020-07-23 16:37:03 +02005534 eap->errmsg = _(e_invrange);
Bram Moolenaarc6251552017-01-29 21:42:20 +01005535 tab_number = 0;
5536 }
Bram Moolenaardea25702017-01-29 15:18:10 +01005537 else
5538 {
5539 tab_number = eap->line2;
Bram Moolenaar82a12462019-01-22 22:41:42 +01005540 if (!unaccept_arg0 && *skipwhite(*eap->cmdlinep) == '-')
Bram Moolenaardea25702017-01-29 15:18:10 +01005541 {
5542 --tab_number;
5543 if (tab_number < unaccept_arg0)
Bram Moolenaar8930caa2020-07-23 16:37:03 +02005544 eap->errmsg = _(e_invrange);
Bram Moolenaardea25702017-01-29 15:18:10 +01005545 }
5546 }
5547 }
5548 else
5549 {
5550 switch (eap->cmdidx)
5551 {
5552 case CMD_tabnext:
5553 tab_number = tabpage_index(curtab) + 1;
5554 if (tab_number > LAST_TAB_NR)
5555 tab_number = 1;
5556 break;
5557 case CMD_tabmove:
5558 tab_number = LAST_TAB_NR;
5559 break;
5560 default:
5561 tab_number = tabpage_index(curtab);
5562 }
5563 }
5564
5565theend:
5566 return tab_number;
5567}
5568
5569/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00005570 * ":tabclose": close current tab page, unless it is the last one.
5571 * ":tabclose N": close tab page N.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005572 */
5573 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005574ex_tabclose(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005575{
Bram Moolenaarf740b292006-02-16 22:11:02 +00005576 tabpage_T *tp;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005577 int tab_number;
Bram Moolenaarf740b292006-02-16 22:11:02 +00005578
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005579# ifdef FEAT_CMDWIN
5580 if (cmdwin_type != 0)
5581 cmdwin_result = K_IGNORE;
5582 else
5583# endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00005584 if (first_tabpage->tp_next == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005585 emsg(_("E784: Cannot close last tab page"));
Bram Moolenaarf740b292006-02-16 22:11:02 +00005586 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005587 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005588 tab_number = get_tabpage_arg(eap);
5589 if (eap->errmsg == NULL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005590 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005591 tp = find_tabpage(tab_number);
Bram Moolenaarf740b292006-02-16 22:11:02 +00005592 if (tp == NULL)
5593 {
5594 beep_flush();
5595 return;
5596 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005597 if (tp != curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00005598 {
5599 tabpage_close_other(tp, eap->forceit);
5600 return;
5601 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005602 else if (!text_locked() && !curbuf_locked())
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005603 tabpage_close(eap->forceit);
5604 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005605 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005606}
5607
5608/*
5609 * ":tabonly": close all tab pages except the current one
5610 */
5611 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005612ex_tabonly(exarg_T *eap)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005613{
5614 tabpage_T *tp;
5615 int done;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005616 int tab_number;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005617
5618# ifdef FEAT_CMDWIN
5619 if (cmdwin_type != 0)
5620 cmdwin_result = K_IGNORE;
5621 else
5622# endif
5623 if (first_tabpage->tp_next == NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01005624 msg(_("Already only one tab page"));
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005625 else
5626 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005627 tab_number = get_tabpage_arg(eap);
5628 if (eap->errmsg == NULL)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005629 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005630 goto_tabpage(tab_number);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005631 // Repeat this up to a 1000 times, because autocommands may
5632 // mess up the lists.
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005633 for (done = 0; done < 1000; ++done)
5634 {
5635 FOR_ALL_TABPAGES(tp)
5636 if (tp->tp_topframe != topframe)
5637 {
5638 tabpage_close_other(tp, eap->forceit);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005639 // if we failed to close it quit
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005640 if (valid_tabpage(tp))
5641 done = 1000;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005642 // start over, "tp" is now invalid
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005643 break;
5644 }
5645 if (first_tabpage->tp_next == NULL)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005646 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005647 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005648 }
5649 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005650}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005651
5652/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00005653 * Close the current tab page.
5654 */
5655 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005656tabpage_close(int forceit)
Bram Moolenaarf740b292006-02-16 22:11:02 +00005657{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005658 // First close all the windows but the current one. If that worked then
5659 // close the last window in this tab, that will close it.
Bram Moolenaar459ca562016-11-10 18:16:33 +01005660 if (!ONE_WINDOW)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005661 close_others(TRUE, forceit);
Bram Moolenaar459ca562016-11-10 18:16:33 +01005662 if (ONE_WINDOW)
Bram Moolenaarf740b292006-02-16 22:11:02 +00005663 ex_win_close(forceit, curwin, NULL);
5664# ifdef FEAT_GUI
5665 need_mouse_correct = TRUE;
5666# endif
5667}
5668
5669/*
5670 * Close tab page "tp", which is not the current tab page.
5671 * Note that autocommands may make "tp" invalid.
Bram Moolenaar7875acc2006-09-10 13:51:17 +00005672 * Also takes care of the tab pages line disappearing when closing the
5673 * last-but-one tab page.
Bram Moolenaarf740b292006-02-16 22:11:02 +00005674 */
5675 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005676tabpage_close_other(tabpage_T *tp, int forceit)
Bram Moolenaarf740b292006-02-16 22:11:02 +00005677{
5678 int done = 0;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005679 win_T *wp;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00005680 int h = tabline_height();
Bram Moolenaarf740b292006-02-16 22:11:02 +00005681
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005682 // Limit to 1000 windows, autocommands may add a window while we close
5683 // one. OK, so I'm paranoid...
Bram Moolenaarf740b292006-02-16 22:11:02 +00005684 while (++done < 1000)
5685 {
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005686 wp = tp->tp_firstwin;
5687 ex_win_close(forceit, wp, tp);
Bram Moolenaarf740b292006-02-16 22:11:02 +00005688
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005689 // Autocommands may delete the tab page under our fingers and we may
5690 // fail to close a window with a modified buffer.
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005691 if (!valid_tabpage(tp) || tp->tp_firstwin == wp)
Bram Moolenaarf740b292006-02-16 22:11:02 +00005692 break;
5693 }
Bram Moolenaar7875acc2006-09-10 13:51:17 +00005694
Bram Moolenaar29323592016-07-24 22:04:11 +02005695 apply_autocmds(EVENT_TABCLOSED, NULL, NULL, FALSE, curbuf);
Bram Moolenaar29323592016-07-24 22:04:11 +02005696
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005697 redraw_tabline = TRUE;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00005698 if (h != tabline_height())
5699 shell_new_rows();
Bram Moolenaarf740b292006-02-16 22:11:02 +00005700}
5701
5702/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005703 * ":only".
5704 */
5705 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005706ex_only(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005707{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005708 win_T *wp;
5709 int wnr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005710# ifdef FEAT_GUI
5711 need_mouse_correct = TRUE;
5712# endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005713 if (eap->addr_count > 0)
5714 {
5715 wnr = eap->line2;
5716 for (wp = firstwin; --wnr > 0; )
5717 {
5718 if (wp->w_next == NULL)
5719 break;
5720 else
5721 wp = wp->w_next;
5722 }
5723 win_goto(wp);
5724 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005725 close_others(TRUE, eap->forceit);
5726}
5727
Bram Moolenaar071d4272004-06-13 20:20:40 +00005728 static void
Bram Moolenaar5e1e6d22017-01-02 17:26:00 +01005729ex_hide(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005730{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005731 // ":hide" or ":hide | cmd": hide current window
Bram Moolenaar2256c992016-11-15 21:17:07 +01005732 if (!eap->skip)
5733 {
Bram Moolenaar4033c552017-09-16 20:54:51 +02005734#ifdef FEAT_GUI
Bram Moolenaar2256c992016-11-15 21:17:07 +01005735 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005736#endif
Bram Moolenaar2256c992016-11-15 21:17:07 +01005737 if (eap->addr_count == 0)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005738 win_close(curwin, FALSE); // don't free buffer
Bram Moolenaar2256c992016-11-15 21:17:07 +01005739 else
5740 {
5741 int winnr = 0;
5742 win_T *win;
Bram Moolenaarf240e182014-11-27 18:33:02 +01005743
Bram Moolenaar2256c992016-11-15 21:17:07 +01005744 FOR_ALL_WINDOWS(win)
5745 {
5746 winnr++;
5747 if (winnr == eap->line2)
5748 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005749 }
Bram Moolenaar2256c992016-11-15 21:17:07 +01005750 if (win == NULL)
5751 win = lastwin;
5752 win_close(win, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005753 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005754 }
5755}
5756
5757/*
5758 * ":stop" and ":suspend": Suspend Vim.
5759 */
5760 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005761ex_stop(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005762{
5763 /*
5764 * Disallow suspending for "rvim".
5765 */
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005766 if (!check_restricted())
Bram Moolenaar071d4272004-06-13 20:20:40 +00005767 {
5768 if (!eap->forceit)
5769 autowrite_all();
5770 windgoto((int)Rows - 1, 0);
5771 out_char('\n');
5772 out_flush();
5773 stoptermcap();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005774 out_flush(); // needed for SUN to restore xterm buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00005775#ifdef FEAT_TITLE
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005776 mch_restore_title(SAVE_RESTORE_BOTH); // restore window titles
Bram Moolenaar071d4272004-06-13 20:20:40 +00005777#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005778 ui_suspend(); // call machine specific function
Bram Moolenaar071d4272004-06-13 20:20:40 +00005779#ifdef FEAT_TITLE
5780 maketitle();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005781 resettitle(); // force updating the title
Bram Moolenaar071d4272004-06-13 20:20:40 +00005782#endif
5783 starttermcap();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005784 scroll_start(); // scroll screen before redrawing
Bram Moolenaar071d4272004-06-13 20:20:40 +00005785 redraw_later_clear();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005786 shell_resized(); // may have resized window
Bram Moolenaar071d4272004-06-13 20:20:40 +00005787 }
5788}
5789
5790/*
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005791 * ":exit", ":xit" and ":wq": Write file and quite the current window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005792 */
5793 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005794ex_exit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005795{
Bram Moolenaar461f2122020-07-28 20:25:47 +02005796#ifdef FEAT_EVAL
Bram Moolenaarae616492020-07-28 20:07:27 +02005797 if (not_in_vim9(eap) == FAIL)
5798 return;
Bram Moolenaar461f2122020-07-28 20:25:47 +02005799#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005800#ifdef FEAT_CMDWIN
5801 if (cmdwin_type != 0)
5802 {
5803 cmdwin_result = Ctrl_C;
5804 return;
5805 }
5806#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005807 // Don't quit while editing the command line.
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005808 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005809 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005810 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005811 return;
5812 }
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005813
5814 if (before_quit_autocmds(curwin, FALSE, eap->forceit))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005815 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005816
5817 /*
5818 * if more files or windows we won't exit
5819 */
5820 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
5821 exiting = TRUE;
5822 if ( ((eap->cmdidx == CMD_wq
5823 || curbufIsChanged())
5824 && do_write(eap) == FAIL)
5825 || check_more(TRUE, eap->forceit) == FAIL
Bram Moolenaar027387f2016-01-02 22:25:52 +01005826 || (only_one_window() && check_changed_any(eap->forceit, FALSE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005827 {
5828 not_exiting();
5829 }
5830 else
5831 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005832 if (only_one_window()) // quit last window, exit Vim
Bram Moolenaar071d4272004-06-13 20:20:40 +00005833 getout(0);
Bram Moolenaar2c33d7b2017-10-14 16:06:20 +02005834 not_exiting();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005835# ifdef FEAT_GUI
5836 need_mouse_correct = TRUE;
5837# endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005838 // Quit current window, may free the buffer.
Bram Moolenaareb44a682017-08-03 22:44:55 +02005839 win_close(curwin, !buf_hide(curwin->w_buffer));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005840 }
5841}
5842
5843/*
5844 * ":print", ":list", ":number".
5845 */
5846 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005847ex_print(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005848{
Bram Moolenaardf177f62005-02-22 08:39:57 +00005849 if (curbuf->b_ml.ml_flags & ML_EMPTY)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005850 emsg(_(e_emptybuf));
Bram Moolenaardf177f62005-02-22 08:39:57 +00005851 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005852 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00005853 for ( ;!got_int; ui_breakcheck())
5854 {
5855 print_line(eap->line1,
5856 (eap->cmdidx == CMD_number || eap->cmdidx == CMD_pound
5857 || (eap->flags & EXFLAG_NR)),
5858 eap->cmdidx == CMD_list || (eap->flags & EXFLAG_LIST));
5859 if (++eap->line1 > eap->line2)
5860 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005861 out_flush(); // show one line at a time
Bram Moolenaardf177f62005-02-22 08:39:57 +00005862 }
5863 setpcmark();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005864 // put cursor at last line
Bram Moolenaardf177f62005-02-22 08:39:57 +00005865 curwin->w_cursor.lnum = eap->line2;
5866 beginline(BL_SOL | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005867 }
5868
Bram Moolenaar071d4272004-06-13 20:20:40 +00005869 ex_no_reprint = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005870}
5871
5872#ifdef FEAT_BYTEOFF
5873 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005874ex_goto(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005875{
5876 goto_byte(eap->line2);
5877}
5878#endif
5879
5880/*
5881 * ":shell".
5882 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005883 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005884ex_shell(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005885{
5886 do_shell(NULL, 0);
5887}
5888
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005889#if defined(HAVE_DROP_FILE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005890
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005891static int drop_busy = FALSE;
5892static int drop_filec;
5893static char_u **drop_filev = NULL;
5894static int drop_split;
5895static void (*drop_callback)(void *);
5896static void *drop_cookie;
5897
5898 static void
5899handle_drop_internal(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005900{
5901 exarg_T ea;
5902 int save_msg_scroll = msg_scroll;
5903
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005904 // Setting the argument list may cause screen updates and being called
5905 // recursively. Avoid that by setting drop_busy.
5906 drop_busy = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005907
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005908 // Check whether the current buffer is changed. If so, we will need
5909 // to split the current window or data could be lost.
5910 // We don't need to check if the 'hidden' option is set, as in this
5911 // case the buffer won't be lost.
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005912 if (!buf_hide(curbuf) && !drop_split)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005913 {
5914 ++emsg_off;
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005915 drop_split = check_changed(curbuf, CCGD_AW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005916 --emsg_off;
5917 }
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005918 if (drop_split)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005919 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005920 if (win_split(0, 0) == FAIL)
5921 return;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02005922 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005923
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005924 // When splitting the window, create a new alist. Otherwise the
5925 // existing one is overwritten.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005926 alist_unlink(curwin->w_alist);
5927 alist_new();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005928 }
5929
5930 /*
5931 * Set up the new argument list.
5932 */
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005933 alist_set(ALIST(curwin), drop_filec, drop_filev, FALSE, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005934
5935 /*
5936 * Move to the first file.
5937 */
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005938 // Fake up a minimal "next" command for do_argfile()
Bram Moolenaara80faa82020-04-12 19:37:17 +02005939 CLEAR_FIELD(ea);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005940 ea.cmd = (char_u *)"next";
5941 do_argfile(&ea, 0);
5942
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005943 // do_ecmd() may set need_start_insertmode, but since we never left Insert
5944 // mode that is not needed here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005945 need_start_insertmode = FALSE;
5946
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005947 // Restore msg_scroll, otherwise a following command may cause scrolling
5948 // unexpectedly. The screen will be redrawn by the caller, thus
5949 // msg_scroll being set by displaying a message is irrelevant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005950 msg_scroll = save_msg_scroll;
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005951
5952 if (drop_callback != NULL)
5953 drop_callback(drop_cookie);
5954
5955 drop_filev = NULL;
5956 drop_busy = FALSE;
5957}
5958
5959/*
5960 * Handle a file drop. The code is here because a drop is *nearly* like an
5961 * :args command, but not quite (we have a list of exact filenames, so we
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01005962 * don't want to (a) parse a command line, or (b) expand wildcards). So the
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005963 * code is very similar to :args and hence needs access to a lot of the static
5964 * functions in this file.
5965 *
5966 * The "filev" list must have been allocated using alloc(), as should each item
5967 * in the list. This function takes over responsibility for freeing the "filev"
5968 * list.
5969 */
5970 void
5971handle_drop(
5972 int filec, // the number of files dropped
5973 char_u **filev, // the list of files dropped
5974 int split, // force splitting the window
5975 void (*callback)(void *), // to be called after setting the argument
5976 // list
5977 void *cookie) // argument for "callback" (allocated)
5978{
5979 // Cannot handle recursive drops, finish the pending one.
5980 if (drop_busy)
5981 {
5982 FreeWild(filec, filev);
5983 vim_free(cookie);
5984 return;
5985 }
5986
5987 // When calling handle_drop() more than once in a row we only use the last
5988 // one.
5989 if (drop_filev != NULL)
5990 {
5991 FreeWild(drop_filec, drop_filev);
5992 vim_free(drop_cookie);
5993 }
5994
5995 drop_filec = filec;
5996 drop_filev = filev;
5997 drop_split = split;
5998 drop_callback = callback;
5999 drop_cookie = cookie;
6000
6001 // Postpone this when:
6002 // - editing the command line
6003 // - not possible to change the current buffer
6004 // - updating the screen
6005 // As it may change buffers and window structures that are in use and cause
6006 // freed memory to be used.
6007 if (text_locked() || curbuf_locked() || updating_screen)
6008 return;
6009
6010 handle_drop_internal();
6011}
6012
6013/*
6014 * To be called when text is unlocked, curbuf is unlocked or updating_screen is
6015 * reset: Handle a postponed drop.
6016 */
6017 void
6018handle_any_postponed_drop(void)
6019{
6020 if (!drop_busy && drop_filev != NULL
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +02006021 && !text_locked() && !curbuf_locked() && !updating_screen)
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006022 handle_drop_internal();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006023}
6024#endif
6025
Bram Moolenaar071d4272004-06-13 20:20:40 +00006026/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006027 * ":preserve".
6028 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006029 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006030ex_preserve(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006031{
Bram Moolenaar4399ef42005-02-12 14:29:27 +00006032 curbuf->b_flags |= BF_PRESERVED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006033 ml_preserve(curbuf, TRUE);
6034}
6035
6036/*
6037 * ":recover".
6038 */
6039 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006040ex_recover(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006041{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006042 // Set recoverymode right away to avoid the ATTENTION prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006043 recoverymode = TRUE;
Bram Moolenaar45d3b142013-11-09 03:31:51 +01006044 if (!check_changed(curbuf, (p_awa ? CCGD_AW : 0)
6045 | CCGD_MULTWIN
6046 | (eap->forceit ? CCGD_FORCEIT : 0)
6047 | CCGD_EXCMD)
6048
Bram Moolenaar071d4272004-06-13 20:20:40 +00006049 && (*eap->arg == NUL
6050 || setfname(curbuf, eap->arg, NULL, TRUE) == OK))
Bram Moolenaar99499b12019-05-23 21:35:48 +02006051 ml_recover(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006052 recoverymode = FALSE;
6053}
6054
6055/*
6056 * Command modifier used in a wrong way.
6057 */
6058 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006059ex_wrongmodifier(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006060{
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006061 eap->errmsg = _(e_invalid_command);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006062}
6063
Bram Moolenaar071d4272004-06-13 20:20:40 +00006064/*
6065 * :sview [+command] file split window with new file, read-only
6066 * :split [[+command] file] split window with current or new file
6067 * :vsplit [[+command] file] split window vertically with current or new file
6068 * :new [[+command] file] split window with no or new file
6069 * :vnew [[+command] file] split vertically window with no or new file
6070 * :sfind [+command] file split window with file in 'path'
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006071 *
6072 * :tabedit open new Tab page with empty window
6073 * :tabedit [+command] file open new Tab page and edit "file"
6074 * :tabnew [[+command] file] just like :tabedit
6075 * :tabfind [+command] file open new Tab page and find "file"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006076 */
6077 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006078ex_splitview(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006079{
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006080 win_T *old_curwin = curwin;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006081#if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006082 char_u *fname = NULL;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006083#endif
6084#ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00006085 int browse_flag = cmdmod.browse;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006086#endif
Bram Moolenaar39902a02018-06-21 22:10:08 +02006087 int use_tab = eap->cmdidx == CMD_tabedit
6088 || eap->cmdidx == CMD_tabfind
6089 || eap->cmdidx == CMD_tabnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006090
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01006091 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02006092 return;
6093
Bram Moolenaar4033c552017-09-16 20:54:51 +02006094#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00006095 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006096#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006097
Bram Moolenaar4033c552017-09-16 20:54:51 +02006098#ifdef FEAT_QUICKFIX
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006099 // A ":split" in the quickfix window works like ":new". Don't want two
6100 // quickfix windows. But it's OK when doing ":tab split".
Bram Moolenaar05bb9532008-07-04 09:44:11 +00006101 if (bt_quickfix(curbuf) && cmdmod.tab == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006102 {
6103 if (eap->cmdidx == CMD_split)
6104 eap->cmdidx = CMD_new;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006105 if (eap->cmdidx == CMD_vsplit)
6106 eap->cmdidx = CMD_vnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006107 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02006108#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006109
Bram Moolenaar4033c552017-09-16 20:54:51 +02006110#ifdef FEAT_SEARCHPATH
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006111 if (eap->cmdidx == CMD_sfind || eap->cmdidx == CMD_tabfind)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006112 {
6113 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg),
6114 FNAME_MESS, TRUE, curbuf->b_ffname);
6115 if (fname == NULL)
6116 goto theend;
6117 eap->arg = fname;
6118 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006119# ifdef FEAT_BROWSE
Bram Moolenaar4033c552017-09-16 20:54:51 +02006120 else
6121# endif
6122#endif
6123#ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00006124 if (cmdmod.browse
Bram Moolenaar071d4272004-06-13 20:20:40 +00006125 && eap->cmdidx != CMD_vnew
Bram Moolenaar071d4272004-06-13 20:20:40 +00006126 && eap->cmdidx != CMD_new)
6127 {
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006128 if (
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006129# ifdef FEAT_GUI
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006130 !gui.in_use &&
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006131# endif
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006132 au_has_group((char_u *)"FileExplorer"))
6133 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006134 // No browsing supported but we do have the file explorer:
6135 // Edit the directory.
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006136 if (*eap->arg == NUL || !mch_isdir(eap->arg))
6137 eap->arg = (char_u *)".";
6138 }
6139 else
6140 {
Bram Moolenaar39902a02018-06-21 22:10:08 +02006141 fname = do_browse(0, (char_u *)(use_tab
6142 ? _("Edit File in new tab page")
6143 : _("Edit File in new window")),
Bram Moolenaar071d4272004-06-13 20:20:40 +00006144 eap->arg, NULL, NULL, NULL, curbuf);
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006145 if (fname == NULL)
6146 goto theend;
6147 eap->arg = fname;
6148 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006149 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006150 cmdmod.browse = FALSE; // Don't browse again in do_ecmd().
Bram Moolenaar4033c552017-09-16 20:54:51 +02006151#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006152
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006153 /*
6154 * Either open new tab page or split the window.
6155 */
Bram Moolenaar39902a02018-06-21 22:10:08 +02006156 if (use_tab)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006157 {
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006158 if (win_new_tabpage(cmdmod.tab != 0 ? cmdmod.tab
6159 : eap->addr_count == 0 ? 0
6160 : (int)eap->line2 + 1) != FAIL)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006161 {
Bram Moolenaard2b66012008-01-09 19:30:36 +00006162 do_exedit(eap, old_curwin);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006163
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006164 // set the alternate buffer for the window we came from
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006165 if (curwin != old_curwin
6166 && win_valid(old_curwin)
6167 && old_curwin->w_buffer != curbuf
6168 && !cmdmod.keepalt)
6169 old_curwin->w_alt_fnum = curbuf->b_fnum;
6170 }
6171 }
6172 else if (win_split(eap->addr_count > 0 ? (int)eap->line2 : 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006173 *eap->cmd == 'v' ? WSP_VERT : 0) != FAIL)
6174 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006175 // Reset 'scrollbind' when editing another file, but keep it when
6176 // doing ":split" without arguments.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006177 if (*eap->arg != NUL
Bram Moolenaar8a3bb562018-03-04 20:14:14 +01006178# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00006179 || cmdmod.browse
Bram Moolenaar8a3bb562018-03-04 20:14:14 +01006180# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006181 )
Bram Moolenaar3368ea22010-09-21 16:56:35 +02006182 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006183 else
6184 do_check_scrollbind(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006185 do_exedit(eap, old_curwin);
6186 }
6187
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006188# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00006189 cmdmod.browse = browse_flag;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006190# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006191
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006192# if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006193theend:
6194 vim_free(fname);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006195# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006196}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006197
6198/*
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006199 * Open a new tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006200 */
6201 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006202tabpage_new(void)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006203{
6204 exarg_T ea;
6205
Bram Moolenaara80faa82020-04-12 19:37:17 +02006206 CLEAR_FIELD(ea);
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006207 ea.cmdidx = CMD_tabnew;
6208 ea.cmd = (char_u *)"tabn";
6209 ea.arg = (char_u *)"";
6210 ex_splitview(&ea);
6211}
6212
6213/*
6214 * :tabnext command
6215 */
6216 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006217ex_tabnext(exarg_T *eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006218{
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006219 int tab_number;
6220
Bram Moolenaar8cdbd5b2019-06-16 15:50:45 +02006221 if (ERROR_IF_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02006222 return;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006223 switch (eap->cmdidx)
6224 {
6225 case CMD_tabfirst:
6226 case CMD_tabrewind:
6227 goto_tabpage(1);
6228 break;
6229 case CMD_tablast:
6230 goto_tabpage(9999);
6231 break;
6232 case CMD_tabprevious:
6233 case CMD_tabNext:
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006234 if (eap->arg && *eap->arg != NUL)
6235 {
6236 char_u *p = eap->arg;
6237 char_u *p_save = p;
6238
6239 tab_number = getdigits(&p);
6240 if (p == p_save || *p_save == '-' || *p != NUL
6241 || tab_number == 0)
6242 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006243 // No numbers as argument.
Bram Moolenaar8930caa2020-07-23 16:37:03 +02006244 eap->errmsg = ex_errmsg(e_invarg2, eap->arg);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006245 return;
6246 }
6247 }
6248 else
6249 {
6250 if (eap->addr_count == 0)
6251 tab_number = 1;
6252 else
6253 {
6254 tab_number = eap->line2;
6255 if (tab_number < 1)
6256 {
Bram Moolenaar8930caa2020-07-23 16:37:03 +02006257 eap->errmsg = _(e_invrange);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006258 return;
6259 }
6260 }
6261 }
6262 goto_tabpage(-tab_number);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006263 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006264 default: // CMD_tabnext
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006265 tab_number = get_tabpage_arg(eap);
6266 if (eap->errmsg == NULL)
6267 goto_tabpage(tab_number);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006268 break;
6269 }
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006270}
6271
6272/*
6273 * :tabmove command
6274 */
6275 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006276ex_tabmove(exarg_T *eap)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006277{
Bram Moolenaar40ce3a42015-04-21 18:08:39 +02006278 int tab_number;
Bram Moolenaar8cb8dca2012-07-06 18:27:39 +02006279
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006280 tab_number = get_tabpage_arg(eap);
6281 if (eap->errmsg == NULL)
6282 tabpage_move(tab_number);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006283}
6284
6285/*
6286 * :tabs command: List tabs and their contents.
6287 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006288 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006289ex_tabs(exarg_T *eap UNUSED)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006290{
6291 tabpage_T *tp;
6292 win_T *wp;
6293 int tabcount = 1;
6294
6295 msg_start();
6296 msg_scroll = TRUE;
6297 for (tp = first_tabpage; tp != NULL && !got_int; tp = tp->tp_next)
6298 {
6299 msg_putchar('\n');
6300 vim_snprintf((char *)IObuff, IOSIZE, _("Tab page %d"), tabcount++);
Bram Moolenaar8820b482017-03-16 17:23:31 +01006301 msg_outtrans_attr(IObuff, HL_ATTR(HLF_T));
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006302 out_flush(); // output one line at a time
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006303 ui_breakcheck();
6304
Bram Moolenaar030f0df2006-02-21 22:02:53 +00006305 if (tp == curtab)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006306 wp = firstwin;
6307 else
6308 wp = tp->tp_firstwin;
6309 for ( ; wp != NULL && !got_int; wp = wp->w_next)
6310 {
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006311 msg_putchar('\n');
6312 msg_putchar(wp == curwin ? '>' : ' ');
6313 msg_putchar(' ');
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006314 msg_putchar(bufIsChanged(wp->w_buffer) ? '+' : ' ');
6315 msg_putchar(' ');
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006316 if (buf_spname(wp->w_buffer) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02006317 vim_strncpy(IObuff, buf_spname(wp->w_buffer), IOSIZE - 1);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006318 else
6319 home_replace(wp->w_buffer, wp->w_buffer->b_fname,
6320 IObuff, IOSIZE, TRUE);
6321 msg_outtrans(IObuff);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006322 out_flush(); // output one line at a time
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006323 ui_breakcheck();
6324 }
6325 }
6326}
6327
Bram Moolenaar071d4272004-06-13 20:20:40 +00006328/*
6329 * ":mode": Set screen mode.
6330 * If no argument given, just get the screen size and redraw.
6331 */
6332 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006333ex_mode(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006334{
6335 if (*eap->arg == NUL)
6336 shell_resized();
6337 else
Bram Moolenaar3c71aec2020-01-17 19:32:20 +01006338 emsg(_(e_screenmode));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006339}
6340
Bram Moolenaar071d4272004-06-13 20:20:40 +00006341/*
6342 * ":resize".
6343 * set, increment or decrement current window height
6344 */
6345 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006346ex_resize(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006347{
6348 int n;
6349 win_T *wp = curwin;
6350
6351 if (eap->addr_count > 0)
6352 {
6353 n = eap->line2;
6354 for (wp = firstwin; wp->w_next != NULL && --n > 0; wp = wp->w_next)
6355 ;
6356 }
6357
Bram Moolenaar44a2f922016-03-19 22:11:51 +01006358# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00006359 need_mouse_correct = TRUE;
Bram Moolenaar44a2f922016-03-19 22:11:51 +01006360# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006361 n = atol((char *)eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006362 if (cmdmod.split & WSP_VERT)
6363 {
6364 if (*eap->arg == '-' || *eap->arg == '+')
Bram Moolenaar02631462017-09-22 15:20:32 +02006365 n += curwin->w_width;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006366 else if (n == 0 && eap->arg[0] == NUL) // default is very wide
Bram Moolenaar071d4272004-06-13 20:20:40 +00006367 n = 9999;
6368 win_setwidth_win((int)n, wp);
6369 }
6370 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006371 {
6372 if (*eap->arg == '-' || *eap->arg == '+')
6373 n += curwin->w_height;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006374 else if (n == 0 && eap->arg[0] == NUL) // default is very high
Bram Moolenaar071d4272004-06-13 20:20:40 +00006375 n = 9999;
6376 win_setheight_win((int)n, wp);
6377 }
6378}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006379
6380/*
6381 * ":find [+command] <file>" command.
6382 */
6383 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006384ex_find(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006385{
6386#ifdef FEAT_SEARCHPATH
6387 char_u *fname;
6388 int count;
6389
6390 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg), FNAME_MESS,
6391 TRUE, curbuf->b_ffname);
6392 if (eap->addr_count > 0)
6393 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006394 // Repeat finding the file "count" times. This matters when it
6395 // appears several times in the path.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006396 count = eap->line2;
6397 while (fname != NULL && --count > 0)
6398 {
6399 vim_free(fname);
6400 fname = find_file_in_path(NULL, 0, FNAME_MESS,
6401 FALSE, curbuf->b_ffname);
6402 }
6403 }
6404
6405 if (fname != NULL)
6406 {
6407 eap->arg = fname;
6408#endif
6409 do_exedit(eap, NULL);
6410#ifdef FEAT_SEARCHPATH
6411 vim_free(fname);
6412 }
6413#endif
6414}
6415
6416/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00006417 * ":open" simulation: for now just work like ":visual".
6418 */
6419 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006420ex_open(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00006421{
6422 regmatch_T regmatch;
6423 char_u *p;
6424
6425 curwin->w_cursor.lnum = eap->line2;
6426 beginline(BL_SOL | BL_FIX);
6427 if (*eap->arg == '/')
6428 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006429 // ":open /pattern/": put cursor in column found with pattern
Bram Moolenaardf177f62005-02-22 08:39:57 +00006430 ++eap->arg;
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006431 p = skip_regexp(eap->arg, '/', p_magic);
Bram Moolenaardf177f62005-02-22 08:39:57 +00006432 *p = NUL;
6433 regmatch.regprog = vim_regcomp(eap->arg, p_magic ? RE_MAGIC : 0);
6434 if (regmatch.regprog != NULL)
6435 {
6436 regmatch.rm_ic = p_ic;
6437 p = ml_get_curline();
6438 if (vim_regexec(&regmatch, p, (colnr_T)0))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006439 curwin->w_cursor.col = (colnr_T)(regmatch.startp[0] - p);
Bram Moolenaardf177f62005-02-22 08:39:57 +00006440 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006441 emsg(_(e_nomatch));
Bram Moolenaar473de612013-06-08 18:19:48 +02006442 vim_regfree(regmatch.regprog);
Bram Moolenaardf177f62005-02-22 08:39:57 +00006443 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006444 // Move to the NUL, ignore any other arguments.
Bram Moolenaardf177f62005-02-22 08:39:57 +00006445 eap->arg += STRLEN(eap->arg);
6446 }
6447 check_cursor();
6448
6449 eap->cmdidx = CMD_visual;
6450 do_exedit(eap, NULL);
6451}
6452
6453/*
6454 * ":edit", ":badd", ":visual".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006455 */
6456 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006457ex_edit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006458{
6459 do_exedit(eap, NULL);
6460}
6461
6462/*
Bram Moolenaar4b96df52020-01-26 22:00:26 +01006463 * ":edit <file>" command and alike.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006464 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006465 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006466do_exedit(
6467 exarg_T *eap,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006468 win_T *old_curwin) // curwin before doing a split or NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006469{
6470 int n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006471 int need_hide;
Bram Moolenaardf177f62005-02-22 08:39:57 +00006472 int exmode_was = exmode_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006473
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01006474 if ((eap->cmdidx != CMD_pedit && ERROR_IF_POPUP_WINDOW)
6475 || ERROR_IF_TERM_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02006476 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006477 /*
6478 * ":vi" command ends Ex mode.
6479 */
6480 if (exmode_active && (eap->cmdidx == CMD_visual
6481 || eap->cmdidx == CMD_view))
6482 {
6483 exmode_active = FALSE;
Bram Moolenaar158ea172020-06-18 17:28:39 +02006484 ex_pressedreturn = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006485 if (*eap->arg == NUL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00006486 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006487 // Special case: ":global/pat/visual\NLvi-commands"
Bram Moolenaardf177f62005-02-22 08:39:57 +00006488 if (global_busy)
6489 {
6490 int rd = RedrawingDisabled;
6491 int nwr = no_wait_return;
6492 int ms = msg_scroll;
6493#ifdef FEAT_GUI
6494 int he = hold_gui_events;
6495#endif
6496
6497 if (eap->nextcmd != NULL)
6498 {
6499 stuffReadbuff(eap->nextcmd);
6500 eap->nextcmd = NULL;
6501 }
6502
6503 if (exmode_was != EXMODE_VIM)
6504 settmode(TMODE_RAW);
6505 RedrawingDisabled = 0;
6506 no_wait_return = 0;
6507 need_wait_return = FALSE;
6508 msg_scroll = 0;
6509#ifdef FEAT_GUI
6510 hold_gui_events = 0;
6511#endif
6512 must_redraw = CLEAR;
Bram Moolenaar9e2bcb52020-02-18 21:33:00 +01006513 pending_exmode_active = TRUE;
Bram Moolenaardf177f62005-02-22 08:39:57 +00006514
6515 main_loop(FALSE, TRUE);
6516
Bram Moolenaar9e2bcb52020-02-18 21:33:00 +01006517 pending_exmode_active = FALSE;
Bram Moolenaardf177f62005-02-22 08:39:57 +00006518 RedrawingDisabled = rd;
6519 no_wait_return = nwr;
6520 msg_scroll = ms;
6521#ifdef FEAT_GUI
6522 hold_gui_events = he;
6523#endif
6524 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006525 return;
Bram Moolenaardf177f62005-02-22 08:39:57 +00006526 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006527 }
6528
6529 if ((eap->cmdidx == CMD_new
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006530 || eap->cmdidx == CMD_tabnew
6531 || eap->cmdidx == CMD_tabedit
Bram Moolenaar4033c552017-09-16 20:54:51 +02006532 || eap->cmdidx == CMD_vnew) && *eap->arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006533 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006534 // ":new" or ":tabnew" without argument: edit an new empty buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00006535 setpcmark();
6536 (void)do_ecmd(0, NULL, NULL, eap, ECMD_ONE,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00006537 ECMD_HIDE + (eap->forceit ? ECMD_FORCEIT : 0),
6538 old_curwin == NULL ? curwin : NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006539 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02006540 else if ((eap->cmdidx != CMD_split && eap->cmdidx != CMD_vsplit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006541 || *eap->arg != NUL
6542#ifdef FEAT_BROWSE
6543 || cmdmod.browse
6544#endif
6545 )
6546 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006547 // Can't edit another file when "curbuf_lock" is set. Only ":edit"
6548 // can bring us here, others are stopped earlier.
Bram Moolenaar5555acc2006-04-07 21:33:12 +00006549 if (*eap->arg != NUL && curbuf_locked())
6550 return;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006551
Bram Moolenaar071d4272004-06-13 20:20:40 +00006552 n = readonlymode;
6553 if (eap->cmdidx == CMD_view || eap->cmdidx == CMD_sview)
6554 readonlymode = TRUE;
6555 else if (eap->cmdidx == CMD_enew)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006556 readonlymode = FALSE; // 'readonly' doesn't make sense in an
6557 // empty buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00006558 setpcmark();
6559 if (do_ecmd(0, (eap->cmdidx == CMD_enew ? NULL : eap->arg),
6560 NULL, eap,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006561 // ":edit" goes to first line if Vi compatible
Bram Moolenaar071d4272004-06-13 20:20:40 +00006562 (*eap->arg == NUL && eap->do_ecmd_lnum == 0
6563 && vim_strchr(p_cpo, CPO_GOTO1) != NULL)
6564 ? ECMD_ONE : eap->do_ecmd_lnum,
Bram Moolenaareb44a682017-08-03 22:44:55 +02006565 (buf_hide(curbuf) ? ECMD_HIDE : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006566 + (eap->forceit ? ECMD_FORCEIT : 0)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006567 // after a split we can use an existing buffer
Bram Moolenaar7b449342014-03-25 13:03:48 +01006568 + (old_curwin != NULL ? ECMD_OLDBUF : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006569 + (eap->cmdidx == CMD_badd ? ECMD_ADDBUF : 0 )
Bram Moolenaar701f7af2008-11-15 13:12:07 +00006570 , old_curwin == NULL ? curwin : NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006571 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006572 // Editing the file failed. If the window was split, close it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006573 if (old_curwin != NULL)
6574 {
6575 need_hide = (curbufIsChanged() && curbuf->b_nwindows <= 1);
Bram Moolenaareb44a682017-08-03 22:44:55 +02006576 if (!need_hide || buf_hide(curbuf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006577 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006578#if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00006579 cleanup_T cs;
6580
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006581 // Reset the error/interrupt/exception state here so that
6582 // aborting() returns FALSE when closing a window.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00006583 enter_cleanup(&cs);
Bram Moolenaar4033c552017-09-16 20:54:51 +02006584#endif
6585#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00006586 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006587#endif
Bram Moolenaareb44a682017-08-03 22:44:55 +02006588 win_close(curwin, !need_hide && !buf_hide(curbuf));
Bram Moolenaarc0197e22004-09-13 20:26:32 +00006589
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006590#if defined(FEAT_EVAL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006591 // Restore the error/interrupt/exception state if not
6592 // discarded by a new aborting error, interrupt, or
6593 // uncaught exception.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00006594 leave_cleanup(&cs);
Bram Moolenaar4033c552017-09-16 20:54:51 +02006595#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006596 }
6597 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006598 }
6599 else if (readonlymode && curbuf->b_nwindows == 1)
6600 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006601 // When editing an already visited buffer, 'readonly' won't be set
6602 // but the previous value is kept. With ":view" and ":sview" we
6603 // want the file to be readonly, except when another window is
6604 // editing the same buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006605 curbuf->b_p_ro = TRUE;
6606 }
6607 readonlymode = n;
6608 }
6609 else
6610 {
6611 if (eap->do_ecmd_cmd != NULL)
6612 do_cmdline_cmd(eap->do_ecmd_cmd);
6613#ifdef FEAT_TITLE
6614 n = curwin->w_arg_idx_invalid;
6615#endif
6616 check_arg_idx(curwin);
6617#ifdef FEAT_TITLE
6618 if (n != curwin->w_arg_idx_invalid)
6619 maketitle();
6620#endif
6621 }
6622
Bram Moolenaar071d4272004-06-13 20:20:40 +00006623 /*
6624 * if ":split file" worked, set alternate file name in old window to new
6625 * file
6626 */
6627 if (old_curwin != NULL
6628 && *eap->arg != NUL
6629 && curwin != old_curwin
6630 && win_valid(old_curwin)
Bram Moolenaard4755bb2004-09-02 19:12:26 +00006631 && old_curwin->w_buffer != curbuf
6632 && !cmdmod.keepalt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006633 old_curwin->w_alt_fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006634
6635 ex_no_reprint = TRUE;
6636}
6637
6638#ifndef FEAT_GUI
6639/*
6640 * ":gui" and ":gvim" when there is no GUI.
6641 */
6642 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006643ex_nogui(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006644{
Bram Moolenaar8930caa2020-07-23 16:37:03 +02006645 eap->errmsg = _(e_nogvim);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006646}
6647#endif
6648
Bram Moolenaar4f974752019-02-17 17:44:42 +01006649#if defined(FEAT_GUI_MSWIN) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006650 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006651ex_tearoff(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006652{
6653 gui_make_tearoff(eap->arg);
6654}
6655#endif
6656
Bram Moolenaar29a2c082018-03-05 21:06:23 +01006657#if (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK) \
6658 || defined(FEAT_TERM_POPUP_MENU)) && defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006659 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006660ex_popup(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006661{
Bram Moolenaar29a2c082018-03-05 21:06:23 +01006662# if defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK)
6663 if (gui.in_use)
6664 gui_make_popup(eap->arg, eap->forceit);
6665# ifdef FEAT_TERM_POPUP_MENU
6666 else
6667# endif
6668# endif
6669# ifdef FEAT_TERM_POPUP_MENU
6670 pum_make_popup(eap->arg, eap->forceit);
6671# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006672}
6673#endif
6674
Bram Moolenaar071d4272004-06-13 20:20:40 +00006675 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006676ex_swapname(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006677{
6678 if (curbuf->b_ml.ml_mfp == NULL || curbuf->b_ml.ml_mfp->mf_fname == NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01006679 msg(_("No swap file"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006680 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01006681 msg((char *)curbuf->b_ml.ml_mfp->mf_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006682}
6683
6684/*
6685 * ":syncbind" forces all 'scrollbind' windows to have the same relative
6686 * offset.
6687 * (1998-11-02 16:21:01 R. Edward Ralston <eralston@computer.org>)
6688 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006689 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006690ex_syncbind(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006691{
Bram Moolenaar071d4272004-06-13 20:20:40 +00006692 win_T *wp;
Bram Moolenaardedd1b02013-12-14 13:06:17 +01006693 win_T *save_curwin = curwin;
6694 buf_T *save_curbuf = curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006695 long topline;
6696 long y;
6697 linenr_T old_linenr = curwin->w_cursor.lnum;
6698
6699 setpcmark();
6700
6701 /*
6702 * determine max topline
6703 */
6704 if (curwin->w_p_scb)
6705 {
6706 topline = curwin->w_topline;
Bram Moolenaar29323592016-07-24 22:04:11 +02006707 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006708 {
6709 if (wp->w_p_scb && wp->w_buffer)
6710 {
Bram Moolenaar375e3392019-01-31 18:26:10 +01006711 y = wp->w_buffer->b_ml.ml_line_count - get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006712 if (topline > y)
6713 topline = y;
6714 }
6715 }
6716 if (topline < 1)
6717 topline = 1;
6718 }
6719 else
6720 {
6721 topline = 1;
6722 }
6723
6724
6725 /*
Bram Moolenaardedd1b02013-12-14 13:06:17 +01006726 * Set all scrollbind windows to the same topline.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006727 */
Bram Moolenaar29323592016-07-24 22:04:11 +02006728 FOR_ALL_WINDOWS(curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006729 {
6730 if (curwin->w_p_scb)
6731 {
Bram Moolenaardedd1b02013-12-14 13:06:17 +01006732 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006733 y = topline - curwin->w_topline;
6734 if (y > 0)
6735 scrollup(y, TRUE);
6736 else
6737 scrolldown(-y, TRUE);
6738 curwin->w_scbind_pos = topline;
6739 redraw_later(VALID);
6740 cursor_correct();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006741 curwin->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006742 }
6743 }
Bram Moolenaardedd1b02013-12-14 13:06:17 +01006744 curwin = save_curwin;
6745 curbuf = save_curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006746 if (curwin->w_p_scb)
6747 {
6748 did_syncbind = TRUE;
6749 checkpcmark();
6750 if (old_linenr != curwin->w_cursor.lnum)
6751 {
6752 char_u ctrl_o[2];
6753
6754 ctrl_o[0] = Ctrl_O;
6755 ctrl_o[1] = 0;
6756 ins_typebuf(ctrl_o, REMAP_NONE, 0, TRUE, FALSE);
6757 }
6758 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006759}
6760
6761
6762 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006763ex_read(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006764{
Bram Moolenaardf177f62005-02-22 08:39:57 +00006765 int i;
6766 int empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
6767 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006768
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006769 if (eap->usefilter) // :r!cmd
Bram Moolenaar071d4272004-06-13 20:20:40 +00006770 do_bang(1, eap, FALSE, FALSE, TRUE);
6771 else
6772 {
6773 if (u_save(eap->line2, (linenr_T)(eap->line2 + 1)) == FAIL)
6774 return;
6775
6776#ifdef FEAT_BROWSE
6777 if (cmdmod.browse)
6778 {
6779 char_u *browseFile;
6780
Bram Moolenaar7171abe2004-10-11 10:06:20 +00006781 browseFile = do_browse(0, (char_u *)_("Append File"), eap->arg,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006782 NULL, NULL, NULL, curbuf);
6783 if (browseFile != NULL)
6784 {
6785 i = readfile(browseFile, NULL,
6786 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
6787 vim_free(browseFile);
6788 }
6789 else
6790 i = OK;
6791 }
6792 else
6793#endif
6794 if (*eap->arg == NUL)
6795 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006796 if (check_fname() == FAIL) // check for no file name
Bram Moolenaar071d4272004-06-13 20:20:40 +00006797 return;
6798 i = readfile(curbuf->b_ffname, curbuf->b_fname,
6799 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
6800 }
6801 else
6802 {
6803 if (vim_strchr(p_cpo, CPO_ALTREAD) != NULL)
6804 (void)setaltfname(eap->arg, eap->arg, (linenr_T)1);
6805 i = readfile(eap->arg, NULL,
6806 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
6807
6808 }
Bram Moolenaare13b9af2017-01-13 22:01:02 +01006809 if (i != OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006810 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006811#if defined(FEAT_EVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006812 if (!aborting())
6813#endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006814 semsg(_(e_notopen), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006815 }
6816 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00006817 {
6818 if (empty && exmode_active)
6819 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006820 // Delete the empty line that remains. Historically ex does
6821 // this but vi doesn't.
Bram Moolenaardf177f62005-02-22 08:39:57 +00006822 if (eap->line2 == 0)
6823 lnum = curbuf->b_ml.ml_line_count;
6824 else
6825 lnum = 1;
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00006826 if (*ml_get(lnum) == NUL && u_savedel(lnum, 1L) == OK)
Bram Moolenaardf177f62005-02-22 08:39:57 +00006827 {
Bram Moolenaarca70c072020-05-30 20:30:46 +02006828 ml_delete(lnum);
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00006829 if (curwin->w_cursor.lnum > 1
6830 && curwin->w_cursor.lnum >= lnum)
Bram Moolenaardf177f62005-02-22 08:39:57 +00006831 --curwin->w_cursor.lnum;
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00006832 deleted_lines_mark(lnum, 1L);
Bram Moolenaardf177f62005-02-22 08:39:57 +00006833 }
6834 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006835 redraw_curbuf_later(VALID);
Bram Moolenaardf177f62005-02-22 08:39:57 +00006836 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006837 }
6838}
6839
Bram Moolenaarea408852005-06-25 22:49:46 +00006840static char_u *prev_dir = NULL;
6841
6842#if defined(EXITFREE) || defined(PROTO)
6843 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006844free_cd_dir(void)
Bram Moolenaarea408852005-06-25 22:49:46 +00006845{
Bram Moolenaard23a8232018-02-10 18:45:26 +01006846 VIM_CLEAR(prev_dir);
6847 VIM_CLEAR(globaldir);
Bram Moolenaarea408852005-06-25 22:49:46 +00006848}
6849#endif
6850
Bram Moolenaarf4258302013-06-02 18:20:17 +02006851/*
Bram Moolenaar002bc792020-06-05 22:33:42 +02006852 * Get the previous directory for the given chdir scope.
6853 */
6854 static char_u *
6855get_prevdir(cdscope_T scope)
6856{
6857 if (scope == CDSCOPE_WINDOW)
6858 return curwin->w_prevdir;
6859 else if (scope == CDSCOPE_TABPAGE)
6860 return curtab->tp_prevdir;
6861 return prev_dir;
6862}
6863
6864/*
Bram Moolenaarf4258302013-06-02 18:20:17 +02006865 * Deal with the side effects of changing the current directory.
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006866 * When 'scope' is CDSCOPE_TABPAGE then this was after an ":tcd" command.
6867 * When 'scope' is CDSCOPE_WINDOW then this was after an ":lcd" command.
Bram Moolenaarf4258302013-06-02 18:20:17 +02006868 */
6869 void
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006870post_chdir(cdscope_T scope)
Bram Moolenaarf4258302013-06-02 18:20:17 +02006871{
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006872 if (scope != CDSCOPE_WINDOW)
Bram Moolenaar00aa0692019-04-27 20:37:57 +02006873 // Clear tab local directory for both :cd and :tcd
6874 VIM_CLEAR(curtab->tp_localdir);
Bram Moolenaard23a8232018-02-10 18:45:26 +01006875 VIM_CLEAR(curwin->w_localdir);
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006876 if (scope != CDSCOPE_GLOBAL)
Bram Moolenaarf4258302013-06-02 18:20:17 +02006877 {
Bram Moolenaar002bc792020-06-05 22:33:42 +02006878 char_u *pdir = get_prevdir(scope);
6879
6880 // If still in the global directory, need to remember current
6881 // directory as the global directory.
6882 if (globaldir == NULL && pdir != NULL)
6883 globaldir = vim_strsave(pdir);
6884
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006885 // Remember this local directory for the window.
Bram Moolenaarf4258302013-06-02 18:20:17 +02006886 if (mch_dirname(NameBuff, MAXPATHL) == OK)
Bram Moolenaar00aa0692019-04-27 20:37:57 +02006887 {
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006888 if (scope == CDSCOPE_TABPAGE)
Bram Moolenaar00aa0692019-04-27 20:37:57 +02006889 curtab->tp_localdir = vim_strsave(NameBuff);
6890 else
6891 curwin->w_localdir = vim_strsave(NameBuff);
6892 }
Bram Moolenaarf4258302013-06-02 18:20:17 +02006893 }
6894 else
6895 {
Bram Moolenaar002bc792020-06-05 22:33:42 +02006896 // We are now in the global directory, no need to remember its name.
Bram Moolenaard23a8232018-02-10 18:45:26 +01006897 VIM_CLEAR(globaldir);
Bram Moolenaarf4258302013-06-02 18:20:17 +02006898 }
6899
6900 shorten_fnames(TRUE);
6901}
6902
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006903/*
6904 * Change directory function used by :cd/:tcd/:lcd Ex commands and the
Bram Moolenaar95058722020-06-01 16:26:19 +02006905 * chdir() function.
6906 * scope == CDSCOPE_WINDOW: changes the window-local directory
6907 * scope == CDSCOPE_TABPAGE: changes the tab-local directory
6908 * Otherwise: changes the global directory
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006909 * Returns TRUE if the directory is successfully changed.
6910 */
6911 int
6912changedir_func(
6913 char_u *new_dir,
6914 int forceit,
6915 cdscope_T scope)
6916{
6917 char_u *tofree;
Bram Moolenaar002bc792020-06-05 22:33:42 +02006918 char_u *pdir = NULL;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006919 int dir_differs;
6920 int retval = FALSE;
6921
Bram Moolenaar7cc96922020-01-31 22:41:38 +01006922 if (new_dir == NULL || allbuf_locked())
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006923 return FALSE;
6924
6925 if (vim_strchr(p_cpo, CPO_CHDIR) != NULL && curbufIsChanged() && !forceit)
6926 {
6927 emsg(_("E747: Cannot change directory, buffer is modified (add ! to override)"));
6928 return FALSE;
6929 }
6930
6931 // ":cd -": Change to previous directory
6932 if (STRCMP(new_dir, "-") == 0)
6933 {
Bram Moolenaar002bc792020-06-05 22:33:42 +02006934 pdir = get_prevdir(scope);
6935 if (pdir == NULL)
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006936 {
6937 emsg(_("E186: No previous directory"));
6938 return FALSE;
6939 }
Bram Moolenaar002bc792020-06-05 22:33:42 +02006940 new_dir = pdir;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006941 }
6942
Bram Moolenaar002bc792020-06-05 22:33:42 +02006943 // Free the previous directory
6944 tofree = get_prevdir(scope);
6945
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006946 // Save current directory for next ":cd -"
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006947 if (mch_dirname(NameBuff, MAXPATHL) == OK)
Bram Moolenaar002bc792020-06-05 22:33:42 +02006948 pdir = vim_strsave(NameBuff);
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006949 else
Bram Moolenaar002bc792020-06-05 22:33:42 +02006950 pdir = NULL;
6951 if (scope == CDSCOPE_WINDOW)
6952 curwin->w_prevdir = pdir;
6953 else if (scope == CDSCOPE_TABPAGE)
6954 curtab->tp_prevdir = pdir;
6955 else
6956 prev_dir = pdir;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006957
6958#if defined(UNIX) || defined(VMS)
6959 // for UNIX ":cd" means: go to home directory
6960 if (*new_dir == NUL)
6961 {
6962 // use NameBuff for home directory name
6963# ifdef VMS
6964 char_u *p;
6965
6966 p = mch_getenv((char_u *)"SYS$LOGIN");
6967 if (p == NULL || *p == NUL) // empty is the same as not set
6968 NameBuff[0] = NUL;
6969 else
6970 vim_strncpy(NameBuff, p, MAXPATHL - 1);
6971# else
6972 expand_env((char_u *)"$HOME", NameBuff, MAXPATHL);
6973# endif
6974 new_dir = NameBuff;
6975 }
6976#endif
Bram Moolenaar002bc792020-06-05 22:33:42 +02006977 dir_differs = new_dir == NULL || pdir == NULL
6978 || pathcmp((char *)pdir, (char *)new_dir, -1) != 0;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006979 if (new_dir == NULL || (dir_differs && vim_chdir(new_dir)))
6980 emsg(_(e_failed));
6981 else
6982 {
6983 char_u *acmd_fname;
6984
6985 post_chdir(scope);
6986
6987 if (dir_differs)
6988 {
6989 if (scope == CDSCOPE_WINDOW)
6990 acmd_fname = (char_u *)"window";
6991 else if (scope == CDSCOPE_TABPAGE)
6992 acmd_fname = (char_u *)"tabpage";
6993 else
6994 acmd_fname = (char_u *)"global";
6995 apply_autocmds(EVENT_DIRCHANGED, acmd_fname, new_dir, FALSE,
6996 curbuf);
6997 }
6998 retval = TRUE;
6999 }
7000 vim_free(tofree);
7001
7002 return retval;
7003}
Bram Moolenaarea408852005-06-25 22:49:46 +00007004
Bram Moolenaar071d4272004-06-13 20:20:40 +00007005/*
Bram Moolenaar00aa0692019-04-27 20:37:57 +02007006 * ":cd", ":tcd", ":lcd", ":chdir" ":tchdir" and ":lchdir".
Bram Moolenaar071d4272004-06-13 20:20:40 +00007007 */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00007008 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007009ex_cd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007010{
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01007011 char_u *new_dir;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007012
7013 new_dir = eap->arg;
7014#if !defined(UNIX) && !defined(VMS)
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007015 // for non-UNIX ":cd" means: print current directory
Bram Moolenaar071d4272004-06-13 20:20:40 +00007016 if (*new_dir == NUL)
7017 ex_pwd(NULL);
7018 else
7019#endif
7020 {
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007021 cdscope_T scope = CDSCOPE_GLOBAL;
7022
7023 if (eap->cmdidx == CMD_lcd || eap->cmdidx == CMD_lchdir)
7024 scope = CDSCOPE_WINDOW;
7025 else if (eap->cmdidx == CMD_tcd || eap->cmdidx == CMD_tchdir)
7026 scope = CDSCOPE_TABPAGE;
7027
7028 if (changedir_func(new_dir, eap->forceit, scope))
Bram Moolenaardf177f62005-02-22 08:39:57 +00007029 {
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007030 // Echo the new current directory if the command was typed.
Bram Moolenaarfcfbc672009-07-09 18:13:49 +00007031 if (KeyTyped || p_verbose >= 5)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007032 ex_pwd(eap);
7033 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007034 }
7035}
7036
7037/*
7038 * ":pwd".
7039 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007040 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007041ex_pwd(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007042{
7043 if (mch_dirname(NameBuff, MAXPATHL) == OK)
7044 {
7045#ifdef BACKSLASH_IN_FILENAME
7046 slash_adjust(NameBuff);
7047#endif
Bram Moolenaar95058722020-06-01 16:26:19 +02007048 if (p_verbose > 0)
7049 {
7050 char *context = "global";
7051
7052 if (curwin->w_localdir != NULL)
7053 context = "window";
7054 else if (curtab->tp_localdir != NULL)
7055 context = "tabpage";
7056 smsg("[%s] %s", context, (char *)NameBuff);
7057 }
7058 else
7059 msg((char *)NameBuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007060 }
7061 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007062 emsg(_("E187: Unknown"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007063}
7064
7065/*
7066 * ":=".
7067 */
7068 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007069ex_equal(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007070{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007071 smsg("%ld", (long)eap->line2);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007072 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007073}
7074
7075 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007076ex_sleep(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007077{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007078 int n;
7079 long len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007080
7081 if (cursor_valid())
7082 {
7083 n = W_WINROW(curwin) + curwin->w_wrow - msg_scrolled;
7084 if (n >= 0)
Bram Moolenaar53f81742017-09-22 14:35:51 +02007085 windgoto((int)n, curwin->w_wincol + curwin->w_wcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007086 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007087
7088 len = eap->line2;
7089 switch (*eap->arg)
7090 {
7091 case 'm': break;
7092 case NUL: len *= 1000L; break;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007093 default: semsg(_(e_invarg2), eap->arg); return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007094 }
7095 do_sleep(len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007096}
7097
7098/*
7099 * Sleep for "msec" milliseconds, but keep checking for a CTRL-C every second.
7100 */
7101 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007102do_sleep(long msec)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007103{
Bram Moolenaar52953192019-08-16 10:27:13 +02007104 long done = 0;
Bram Moolenaar975b5272016-03-15 23:10:59 +01007105 long wait_now;
Bram Moolenaar52953192019-08-16 10:27:13 +02007106# ifdef ELAPSED_FUNC
7107 elapsed_T start_tv;
7108
7109 // Remember at what time we started, so that we know how much longer we
7110 // should wait after waiting for a bit.
7111 ELAPSED_INIT(start_tv);
7112# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007113
7114 cursor_on();
Bram Moolenaarf45d7472018-09-30 18:22:26 +02007115 out_flush_cursor(FALSE, FALSE);
Bram Moolenaar52953192019-08-16 10:27:13 +02007116 while (!got_int && done < msec)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007117 {
Bram Moolenaar975b5272016-03-15 23:10:59 +01007118 wait_now = msec - done > 1000L ? 1000L : msec - done;
7119#ifdef FEAT_TIMERS
7120 {
7121 long due_time = check_due_timer();
7122
7123 if (due_time > 0 && due_time < wait_now)
7124 wait_now = due_time;
7125 }
7126#endif
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02007127#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar28e67e02019-08-15 23:05:49 +02007128 if (has_any_channel() && wait_now > 20L)
7129 wait_now = 20L;
7130#endif
7131#ifdef FEAT_SOUND
7132 if (has_any_sound_callback() && wait_now > 20L)
7133 wait_now = 20L;
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02007134#endif
Bram Moolenaar975b5272016-03-15 23:10:59 +01007135 ui_delay(wait_now, TRUE);
Bram Moolenaar52953192019-08-16 10:27:13 +02007136
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02007137#ifdef FEAT_JOB_CHANNEL
7138 if (has_any_channel())
7139 ui_breakcheck_force(TRUE);
7140 else
7141#endif
7142 ui_breakcheck();
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007143#ifdef MESSAGE_QUEUE
Bram Moolenaar52953192019-08-16 10:27:13 +02007144 // Process the netbeans and clientserver messages that may have been
7145 // received in the call to ui_breakcheck() when the GUI is in use. This
7146 // may occur when running a test case.
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007147 parse_queued_messages();
Bram Moolenaare3cc6d42011-10-20 21:58:34 +02007148#endif
Bram Moolenaar52953192019-08-16 10:27:13 +02007149
7150# ifdef ELAPSED_FUNC
7151 // actual time passed
7152 done = ELAPSED_FUNC(start_tv);
7153# else
7154 // guestimate time passed (will actually be more)
7155 done += wait_now;
7156# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007157 }
Bram Moolenaar1ebff3d2018-07-07 16:18:13 +02007158
7159 // If CTRL-C was typed to interrupt the sleep, drop the CTRL-C from the
7160 // input buffer, otherwise a following call to input() fails.
7161 if (got_int)
7162 (void)vpeekc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007163}
7164
Bram Moolenaar071d4272004-06-13 20:20:40 +00007165/*
7166 * ":winsize" command (obsolete).
7167 */
7168 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007169ex_winsize(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007170{
7171 int w, h;
7172 char_u *arg = eap->arg;
7173 char_u *p;
7174
7175 w = getdigits(&arg);
7176 arg = skipwhite(arg);
7177 p = arg;
7178 h = getdigits(&arg);
7179 if (*p != NUL && *arg == NUL)
7180 set_shellsize(w, h, TRUE);
7181 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007182 emsg(_("E465: :winsize requires two number arguments"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007183}
7184
Bram Moolenaar071d4272004-06-13 20:20:40 +00007185 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007186ex_wincmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007187{
7188 int xchar = NUL;
7189 char_u *p;
7190
7191 if (*eap->arg == 'g' || *eap->arg == Ctrl_G)
7192 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007193 // CTRL-W g and CTRL-W CTRL-G have an extra command character
Bram Moolenaar071d4272004-06-13 20:20:40 +00007194 if (eap->arg[1] == NUL)
7195 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007196 emsg(_(e_invarg));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007197 return;
7198 }
7199 xchar = eap->arg[1];
7200 p = eap->arg + 2;
7201 }
7202 else
7203 p = eap->arg + 1;
7204
7205 eap->nextcmd = check_nextcmd(p);
7206 p = skipwhite(p);
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02007207 if (*p != NUL && *p != (
7208#ifdef FEAT_EVAL
7209 in_vim9script() ? '#' :
7210#endif
7211 '"')
7212 && eap->nextcmd == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007213 emsg(_(e_invarg));
Bram Moolenaar12bde492011-06-13 01:19:56 +02007214 else if (!eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007215 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007216 // Pass flags on for ":vertical wincmd ]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00007217 postponed_split_flags = cmdmod.split;
Bram Moolenaard326ce82007-03-11 14:48:29 +00007218 postponed_split_tab = cmdmod.tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007219 do_window(*eap->arg, eap->addr_count > 0 ? eap->line2 : 0L, xchar);
7220 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00007221 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007222 }
7223}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007224
Bram Moolenaar843ee412004-06-30 16:16:41 +00007225#if defined(FEAT_GUI) || defined(UNIX) || defined(VMS) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007226/*
7227 * ":winpos".
7228 */
7229 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007230ex_winpos(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007231{
7232 int x, y;
7233 char_u *arg = eap->arg;
7234 char_u *p;
7235
7236 if (*arg == NUL)
7237 {
Bram Moolenaar843ee412004-06-30 16:16:41 +00007238# if defined(FEAT_GUI) || defined(MSWIN)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007239# ifdef VIMDLL
7240 if (gui.in_use ? gui_mch_get_winpos(&x, &y) != FAIL :
7241 mch_get_winpos(&x, &y) != FAIL)
7242# elif defined(FEAT_GUI)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007243 if (gui.in_use && gui_mch_get_winpos(&x, &y) != FAIL)
Bram Moolenaar843ee412004-06-30 16:16:41 +00007244# else
7245 if (mch_get_winpos(&x, &y) != FAIL)
7246# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007247 {
7248 sprintf((char *)IObuff, _("Window position: X %d, Y %d"), x, y);
Bram Moolenaar32526b32019-01-19 17:43:09 +01007249 msg((char *)IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007250 }
7251 else
7252# endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007253 emsg(_("E188: Obtaining window position not implemented for this platform"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007254 }
7255 else
7256 {
7257 x = getdigits(&arg);
7258 arg = skipwhite(arg);
7259 p = arg;
7260 y = getdigits(&arg);
7261 if (*p == NUL || *arg != NUL)
7262 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007263 emsg(_("E466: :winpos requires two number arguments"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007264 return;
7265 }
7266# ifdef FEAT_GUI
7267 if (gui.in_use)
7268 gui_mch_set_winpos(x, y);
7269 else if (gui.starting)
7270 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007271 // Remember the coordinates for when the window is opened.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007272 gui_win_x = x;
7273 gui_win_y = y;
7274 }
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007275# if defined(HAVE_TGETENT) || defined(VIMDLL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007276 else
7277# endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007278# endif
7279# if defined(MSWIN) && (!defined(FEAT_GUI) || defined(VIMDLL))
Bram Moolenaar843ee412004-06-30 16:16:41 +00007280 mch_set_winpos(x, y);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007281# endif
7282# ifdef HAVE_TGETENT
7283 if (*T_CWP)
7284 term_set_winpos(x, y);
7285# endif
7286 }
7287}
7288#endif
7289
7290/*
7291 * Handle command that work like operators: ":delete", ":yank", ":>" and ":<".
7292 */
7293 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007294ex_operators(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007295{
7296 oparg_T oa;
7297
7298 clear_oparg(&oa);
7299 oa.regname = eap->regname;
7300 oa.start.lnum = eap->line1;
7301 oa.end.lnum = eap->line2;
7302 oa.line_count = eap->line2 - eap->line1 + 1;
7303 oa.motion_type = MLINE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007304 virtual_op = FALSE;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007305 if (eap->cmdidx != CMD_yank) // position cursor for undo
Bram Moolenaar071d4272004-06-13 20:20:40 +00007306 {
7307 setpcmark();
7308 curwin->w_cursor.lnum = eap->line1;
7309 beginline(BL_SOL | BL_FIX);
7310 }
7311
Bram Moolenaard07c6e12013-11-21 14:21:40 +01007312 if (VIsual_active)
7313 end_visual_mode();
Bram Moolenaard07c6e12013-11-21 14:21:40 +01007314
Bram Moolenaar071d4272004-06-13 20:20:40 +00007315 switch (eap->cmdidx)
7316 {
7317 case CMD_delete:
7318 oa.op_type = OP_DELETE;
7319 op_delete(&oa);
7320 break;
7321
7322 case CMD_yank:
7323 oa.op_type = OP_YANK;
7324 (void)op_yank(&oa, FALSE, TRUE);
7325 break;
7326
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007327 default: // CMD_rshift or CMD_lshift
Bram Moolenaar6e707362013-06-14 19:15:58 +02007328 if (
Bram Moolenaar071d4272004-06-13 20:20:40 +00007329#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6e707362013-06-14 19:15:58 +02007330 (eap->cmdidx == CMD_rshift) ^ curwin->w_p_rl
7331#else
7332 eap->cmdidx == CMD_rshift
Bram Moolenaar071d4272004-06-13 20:20:40 +00007333#endif
Bram Moolenaar6e707362013-06-14 19:15:58 +02007334 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00007335 oa.op_type = OP_RSHIFT;
7336 else
7337 oa.op_type = OP_LSHIFT;
7338 op_shift(&oa, FALSE, eap->amount);
7339 break;
7340 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007341 virtual_op = MAYBE;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007342 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007343}
7344
7345/*
7346 * ":put".
7347 */
7348 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007349ex_put(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007350{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007351 // ":0put" works like ":1put!".
Bram Moolenaar071d4272004-06-13 20:20:40 +00007352 if (eap->line2 == 0)
7353 {
7354 eap->line2 = 1;
7355 eap->forceit = TRUE;
7356 }
7357 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaarc3516f72020-09-08 22:45:35 +02007358 do_put(eap->regname, NULL, eap->forceit ? BACKWARD : FORWARD, 1L,
Bram Moolenaardf177f62005-02-22 08:39:57 +00007359 PUT_LINE|PUT_CURSLINE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007360}
7361
7362/*
7363 * Handle ":copy" and ":move".
7364 */
7365 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007366ex_copymove(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007367{
7368 long n;
7369
Bram Moolenaar491799b2020-08-01 19:23:43 +02007370#ifdef FEAT_EVAL
Bram Moolenaarf5a48012020-08-01 17:00:03 +02007371 if (not_in_vim9(eap) == FAIL)
7372 return;
Bram Moolenaar491799b2020-08-01 19:23:43 +02007373#endif
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02007374 n = get_address(eap, &eap->arg, eap->addr_type, FALSE, FALSE, FALSE, 1);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007375 if (eap->arg == NULL) // error detected
Bram Moolenaar071d4272004-06-13 20:20:40 +00007376 {
7377 eap->nextcmd = NULL;
7378 return;
7379 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00007380 get_flags(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007381
7382 /*
7383 * move or copy lines from 'eap->line1'-'eap->line2' to below line 'n'
7384 */
7385 if (n == MAXLNUM || n < 0 || n > curbuf->b_ml.ml_line_count)
7386 {
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02007387 emsg(_(e_invrange));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007388 return;
7389 }
7390
7391 if (eap->cmdidx == CMD_move)
7392 {
7393 if (do_move(eap->line1, eap->line2, n) == FAIL)
7394 return;
7395 }
7396 else
7397 ex_copy(eap->line1, eap->line2, n);
7398 u_clearline();
7399 beginline(BL_SOL | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007400 ex_may_print(eap);
7401}
7402
7403/*
7404 * Print the current line if flags were given to the Ex command.
7405 */
Bram Moolenaarfd3fe982014-04-01 17:49:44 +02007406 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007407ex_may_print(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007408{
7409 if (eap->flags != 0)
7410 {
7411 print_line(curwin->w_cursor.lnum, (eap->flags & EXFLAG_NR),
7412 (eap->flags & EXFLAG_LIST));
7413 ex_no_reprint = TRUE;
7414 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007415}
7416
7417/*
7418 * ":smagic" and ":snomagic".
7419 */
7420 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007421ex_submagic(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007422{
7423 int magic_save = p_magic;
7424
7425 p_magic = (eap->cmdidx == CMD_smagic);
Bram Moolenaar66e00142020-08-12 21:58:12 +02007426 ex_substitute(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007427 p_magic = magic_save;
7428}
7429
7430/*
7431 * ":join".
7432 */
7433 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007434ex_join(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007435{
7436 curwin->w_cursor.lnum = eap->line1;
7437 if (eap->line1 == eap->line2)
7438 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007439 if (eap->addr_count >= 2) // :2,2join does nothing
Bram Moolenaar071d4272004-06-13 20:20:40 +00007440 return;
7441 if (eap->line2 == curbuf->b_ml.ml_line_count)
7442 {
7443 beep_flush();
7444 return;
7445 }
7446 ++eap->line2;
7447 }
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02007448 (void)do_join(eap->line2 - eap->line1 + 1, !eap->forceit, TRUE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007449 beginline(BL_WHITE | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007450 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007451}
7452
7453/*
7454 * ":[addr]@r" or ":[addr]*r": execute register
7455 */
7456 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007457ex_at(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007458{
7459 int c;
Bram Moolenaar60462872009-11-03 11:40:19 +00007460 int prev_len = typebuf.tb_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007461
7462 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaar4930a762016-09-11 14:39:53 +02007463 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007464
7465#ifdef USE_ON_FLY_SCROLL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007466 dont_scroll = TRUE; // disallow scrolling here
Bram Moolenaar071d4272004-06-13 20:20:40 +00007467#endif
7468
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007469 // get the register name. No name means to use the previous one
Bram Moolenaar071d4272004-06-13 20:20:40 +00007470 c = *eap->arg;
7471 if (c == NUL || (c == '*' && *eap->cmd == '*'))
7472 c = '@';
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007473 // Put the register in the typeahead buffer with the "silent" flag.
Bram Moolenaard333d1e2006-11-07 17:43:47 +00007474 if (do_execreg(c, TRUE, vim_strchr(p_cpo, CPO_EXECBUF) != NULL, TRUE)
7475 == FAIL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007476 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007477 beep_flush();
Bram Moolenaardf177f62005-02-22 08:39:57 +00007478 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007479 else
7480 {
7481 int save_efr = exec_from_reg;
7482
7483 exec_from_reg = TRUE;
7484
7485 /*
7486 * Execute from the typeahead buffer.
Bram Moolenaar60462872009-11-03 11:40:19 +00007487 * Continue until the stuff buffer is empty and all added characters
7488 * have been consumed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007489 */
Bram Moolenaar60462872009-11-03 11:40:19 +00007490 while (!stuff_empty() || typebuf.tb_len > prev_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007491 (void)do_cmdline(NULL, getexline, NULL, DOCMD_NOWAIT|DOCMD_VERBOSE);
7492
7493 exec_from_reg = save_efr;
7494 }
7495}
7496
7497/*
7498 * ":!".
7499 */
7500 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007501ex_bang(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007502{
7503 do_bang(eap->addr_count, eap, eap->forceit, TRUE, TRUE);
7504}
7505
7506/*
7507 * ":undo".
7508 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007509 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +01007510ex_undo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007511{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007512 if (eap->addr_count == 1) // :undo 123
Bram Moolenaar730cde92010-06-27 05:18:54 +02007513 undo_time(eap->line2, FALSE, FALSE, TRUE);
Bram Moolenaard3667a22006-03-16 21:35:52 +00007514 else
7515 u_undo(1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007516}
7517
Bram Moolenaar55debbe2010-05-23 23:34:36 +02007518#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar6df6f472010-07-18 18:04:50 +02007519 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007520ex_wundo(exarg_T *eap)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02007521{
7522 char_u hash[UNDO_HASH_SIZE];
7523
7524 u_compute_hash(hash);
7525 u_write_undo(eap->arg, eap->forceit, curbuf, hash);
7526}
7527
Bram Moolenaar6df6f472010-07-18 18:04:50 +02007528 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007529ex_rundo(exarg_T *eap)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02007530{
7531 char_u hash[UNDO_HASH_SIZE];
7532
7533 u_compute_hash(hash);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02007534 u_read_undo(eap->arg, hash, NULL);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02007535}
7536#endif
7537
Bram Moolenaar071d4272004-06-13 20:20:40 +00007538/*
7539 * ":redo".
7540 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007541 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007542ex_redo(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007543{
7544 u_redo(1);
7545}
7546
7547/*
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007548 * ":earlier" and ":later".
7549 */
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007550 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007551ex_later(exarg_T *eap)
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007552{
7553 long count = 0;
7554 int sec = FALSE;
Bram Moolenaar730cde92010-06-27 05:18:54 +02007555 int file = FALSE;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007556 char_u *p = eap->arg;
7557
7558 if (*p == NUL)
7559 count = 1;
7560 else if (isdigit(*p))
7561 {
7562 count = getdigits(&p);
7563 switch (*p)
7564 {
7565 case 's': ++p; sec = TRUE; break;
7566 case 'm': ++p; sec = TRUE; count *= 60; break;
7567 case 'h': ++p; sec = TRUE; count *= 60 * 60; break;
Bram Moolenaar730cde92010-06-27 05:18:54 +02007568 case 'd': ++p; sec = TRUE; count *= 24 * 60 * 60; break;
7569 case 'f': ++p; file = TRUE; break;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007570 }
7571 }
7572
7573 if (*p != NUL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007574 semsg(_(e_invarg2), eap->arg);
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007575 else
Bram Moolenaar730cde92010-06-27 05:18:54 +02007576 undo_time(eap->cmdidx == CMD_earlier ? -count : count,
7577 sec, file, FALSE);
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007578}
7579
7580/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007581 * ":redir": start/stop redirection.
7582 */
7583 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007584ex_redir(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007585{
7586 char *mode;
7587 char_u *fname;
Bram Moolenaarca472992005-01-21 11:46:23 +00007588 char_u *arg = eap->arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007589
Bram Moolenaarba768492016-07-08 15:32:54 +02007590#ifdef FEAT_EVAL
Bram Moolenaar79815f12016-07-09 17:07:29 +02007591 if (redir_execute)
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02007592 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007593 emsg(_("E930: Cannot use :redir inside execute()"));
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02007594 return;
7595 }
Bram Moolenaarba768492016-07-08 15:32:54 +02007596#endif
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02007597
Bram Moolenaar071d4272004-06-13 20:20:40 +00007598 if (STRICMP(eap->arg, "END") == 0)
7599 close_redir();
7600 else
7601 {
Bram Moolenaarca472992005-01-21 11:46:23 +00007602 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00007603 {
Bram Moolenaarca472992005-01-21 11:46:23 +00007604 ++arg;
7605 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00007606 {
Bram Moolenaarca472992005-01-21 11:46:23 +00007607 ++arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007608 mode = "a";
7609 }
7610 else
7611 mode = "w";
Bram Moolenaarca472992005-01-21 11:46:23 +00007612 arg = skipwhite(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007613
7614 close_redir();
7615
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007616 // Expand environment variables and "~/".
Bram Moolenaarca472992005-01-21 11:46:23 +00007617 fname = expand_env_save(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007618 if (fname == NULL)
7619 return;
7620#ifdef FEAT_BROWSE
7621 if (cmdmod.browse)
7622 {
7623 char_u *browseFile;
7624
Bram Moolenaar7171abe2004-10-11 10:06:20 +00007625 browseFile = do_browse(BROWSE_SAVE,
7626 (char_u *)_("Save Redirection"),
Bram Moolenaarc36651b2018-04-29 12:22:56 +02007627 fname, NULL, NULL,
7628 (char_u *)_(BROWSE_FILTER_ALL_FILES), curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007629 if (browseFile == NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007630 return; // operation cancelled
Bram Moolenaar071d4272004-06-13 20:20:40 +00007631 vim_free(fname);
7632 fname = browseFile;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007633 eap->forceit = TRUE; // since dialog already asked
Bram Moolenaar071d4272004-06-13 20:20:40 +00007634 }
7635#endif
7636
7637 redir_fd = open_exfile(fname, eap->forceit, mode);
7638 vim_free(fname);
7639 }
7640#ifdef FEAT_EVAL
Bram Moolenaarca472992005-01-21 11:46:23 +00007641 else if (*arg == '@')
Bram Moolenaar071d4272004-06-13 20:20:40 +00007642 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007643 // redirect to a register a-z (resp. A-Z for appending)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007644 close_redir();
Bram Moolenaarca472992005-01-21 11:46:23 +00007645 ++arg;
7646 if (ASCII_ISALPHA(*arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007647# ifdef FEAT_CLIPBOARD
Bram Moolenaarca472992005-01-21 11:46:23 +00007648 || *arg == '*'
Bram Moolenaar9a51c6e2006-11-14 19:25:02 +00007649 || *arg == '+'
Bram Moolenaar071d4272004-06-13 20:20:40 +00007650# endif
Bram Moolenaarca472992005-01-21 11:46:23 +00007651 || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00007652 {
Bram Moolenaarca472992005-01-21 11:46:23 +00007653 redir_reg = *arg++;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007654 if (*arg == '>' && arg[1] == '>') // append
Bram Moolenaard9d30582005-05-18 22:10:28 +00007655 arg += 2;
Bram Moolenaarc188b882007-10-19 14:20:54 +00007656 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007657 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007658 // Can use both "@a" and "@a>".
Bram Moolenaar2c29bee2005-06-01 21:46:07 +00007659 if (*arg == '>')
7660 arg++;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007661 // Make register empty when not using @A-@Z and the
7662 // command is valid.
Bram Moolenaarc188b882007-10-19 14:20:54 +00007663 if (*arg == NUL && !isupper(redir_reg))
7664 write_reg_contents(redir_reg, (char_u *)"", -1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007665 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00007666 }
7667 if (*arg != NUL)
7668 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00007669 redir_reg = 0;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007670 semsg(_(e_invarg2), eap->arg);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007671 }
7672 }
7673 else if (*arg == '=' && arg[1] == '>')
7674 {
7675 int append;
7676
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007677 // redirect to a variable
Bram Moolenaardf177f62005-02-22 08:39:57 +00007678 close_redir();
7679 arg += 2;
7680
7681 if (*arg == '>')
7682 {
7683 ++arg;
7684 append = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007685 }
7686 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00007687 append = FALSE;
7688
7689 if (var_redir_start(skipwhite(arg), append) == OK)
7690 redir_vname = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007691 }
7692#endif
7693
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007694 // TODO: redirect to a buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00007695
Bram Moolenaar071d4272004-06-13 20:20:40 +00007696 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007697 semsg(_(e_invarg2), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007698 }
Bram Moolenaar29b2d262006-09-10 19:07:28 +00007699
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007700 // Make sure redirection is not off. Can happen for cmdline completion
7701 // that indirectly invokes a command to catch its output.
Bram Moolenaar29b2d262006-09-10 19:07:28 +00007702 if (redir_fd != NULL
7703#ifdef FEAT_EVAL
7704 || redir_reg || redir_vname
7705#endif
7706 )
7707 redir_off = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007708}
7709
7710/*
7711 * ":redraw": force redraw
7712 */
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01007713 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007714ex_redraw(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007715{
7716 int r = RedrawingDisabled;
7717 int p = p_lz;
7718
7719 RedrawingDisabled = 0;
7720 p_lz = FALSE;
Bram Moolenaarabc39ab2017-03-01 18:04:05 +01007721 validate_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007722 update_topline();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007723 update_screen(eap->forceit ? CLEAR : VIsual_active ? INVERTED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007724#ifdef FEAT_TITLE
7725 if (need_maketitle)
7726 maketitle();
7727#endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007728#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
7729# ifdef VIMDLL
7730 if (!gui.in_use)
7731# endif
7732 resize_console_buf();
Bram Moolenaar78d21da2019-02-17 15:00:52 +01007733#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007734 RedrawingDisabled = r;
7735 p_lz = p;
7736
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007737 // Reset msg_didout, so that a message that's there is overwritten.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007738 msg_didout = FALSE;
7739 msg_col = 0;
7740
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007741 // No need to wait after an intentional redraw.
Bram Moolenaar56667a52013-07-17 11:54:28 +02007742 need_wait_return = FALSE;
7743
Bram Moolenaar071d4272004-06-13 20:20:40 +00007744 out_flush();
7745}
7746
7747/*
7748 * ":redrawstatus": force redraw of status line(s)
7749 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007750 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007751ex_redrawstatus(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007752{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007753 int r = RedrawingDisabled;
7754 int p = p_lz;
7755
7756 RedrawingDisabled = 0;
7757 p_lz = FALSE;
7758 if (eap->forceit)
7759 status_redraw_all();
7760 else
7761 status_redraw_curbuf();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007762 update_screen(VIsual_active ? INVERTED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007763 RedrawingDisabled = r;
7764 p_lz = p;
7765 out_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007766}
7767
Bram Moolenaare12bab32019-01-08 22:02:56 +01007768/*
7769 * ":redrawtabline": force redraw of the tabline
7770 */
7771 static void
7772ex_redrawtabline(exarg_T *eap UNUSED)
7773{
7774 int r = RedrawingDisabled;
7775 int p = p_lz;
7776
7777 RedrawingDisabled = 0;
7778 p_lz = FALSE;
7779
7780 draw_tabline();
7781
7782 RedrawingDisabled = r;
7783 p_lz = p;
7784 out_flush();
7785}
7786
Bram Moolenaar071d4272004-06-13 20:20:40 +00007787 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007788close_redir(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007789{
7790 if (redir_fd != NULL)
7791 {
7792 fclose(redir_fd);
7793 redir_fd = NULL;
7794 }
7795#ifdef FEAT_EVAL
7796 redir_reg = 0;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007797 if (redir_vname)
7798 {
7799 var_redir_stop();
7800 redir_vname = 0;
7801 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007802#endif
7803}
7804
Bram Moolenaarf96ae0b2019-07-28 15:21:55 +02007805#if (defined(FEAT_SESSION) || defined(FEAT_EVAL)) || defined(PROTO)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007806 int
Bram Moolenaarbd67aac2019-09-21 23:09:04 +02007807vim_mkdir_emsg(char_u *name, int prot UNUSED)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007808{
7809 if (vim_mkdir(name, prot) != 0)
7810 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007811 semsg(_("E739: Cannot create directory: %s"), name);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007812 return FAIL;
7813 }
7814 return OK;
7815}
7816#endif
7817
Bram Moolenaar071d4272004-06-13 20:20:40 +00007818/*
7819 * Open a file for writing for an Ex command, with some checks.
7820 * Return file descriptor, or NULL on failure.
7821 */
7822 FILE *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007823open_exfile(
7824 char_u *fname,
7825 int forceit,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007826 char *mode) // "w" for create new file or "a" for append
Bram Moolenaar071d4272004-06-13 20:20:40 +00007827{
7828 FILE *fd;
7829
7830#ifdef UNIX
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007831 // with Unix it is possible to open a directory
Bram Moolenaar071d4272004-06-13 20:20:40 +00007832 if (mch_isdir(fname))
7833 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007834 semsg(_(e_isadir2), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007835 return NULL;
7836 }
7837#endif
7838 if (!forceit && *mode != 'a' && vim_fexists(fname))
7839 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007840 semsg(_("E189: \"%s\" exists (add ! to override)"), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007841 return NULL;
7842 }
7843
7844 if ((fd = mch_fopen((char *)fname, mode)) == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007845 semsg(_("E190: Cannot open \"%s\" for writing"), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007846
7847 return fd;
7848}
7849
7850/*
7851 * ":mark" and ":k".
7852 */
7853 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007854ex_mark(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007855{
7856 pos_T pos;
7857
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007858 if (*eap->arg == NUL) // No argument?
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007859 emsg(_(e_argreq));
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007860 else if (eap->arg[1] != NUL) // more than one character?
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +02007861 semsg(_(e_trailing_arg), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007862 else
7863 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007864 pos = curwin->w_cursor; // save curwin->w_cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00007865 curwin->w_cursor.lnum = eap->line2;
7866 beginline(BL_WHITE | BL_FIX);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007867 if (setmark(*eap->arg) == FAIL) // set mark
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007868 emsg(_("E191: Argument must be a letter or forward/backward quote"));
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007869 curwin->w_cursor = pos; // restore curwin->w_cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00007870 }
7871}
7872
7873/*
7874 * Update w_topline, w_leftcol and the cursor position.
7875 */
7876 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007877update_topline_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007878{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007879 check_cursor(); // put cursor on valid line
Bram Moolenaar071d4272004-06-13 20:20:40 +00007880 update_topline();
7881 if (!curwin->w_p_wrap)
7882 validate_cursor();
7883 update_curswant();
7884}
7885
Bram Moolenaar071d4272004-06-13 20:20:40 +00007886/*
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007887 * Save the current State and go to Normal mode.
7888 * Return TRUE if the typeahead could be saved.
7889 */
7890 int
7891save_current_state(save_state_T *sst)
7892{
7893 sst->save_msg_scroll = msg_scroll;
7894 sst->save_restart_edit = restart_edit;
7895 sst->save_msg_didout = msg_didout;
7896 sst->save_State = State;
7897 sst->save_insertmode = p_im;
7898 sst->save_finish_op = finish_op;
7899 sst->save_opcount = opcount;
Bram Moolenaarcce713d2019-03-04 11:40:12 +01007900 sst->save_reg_executing = reg_executing;
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007901
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007902 msg_scroll = FALSE; // no msg scrolling in Normal mode
7903 restart_edit = 0; // don't go to Insert mode
7904 p_im = FALSE; // don't use 'insertmode'
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007905
7906 /*
7907 * Save the current typeahead. This is required to allow using ":normal"
7908 * from an event handler and makes sure we don't hang when the argument
7909 * ends with half a command.
7910 */
7911 save_typeahead(&sst->tabuf);
7912 return sst->tabuf.typebuf_valid;
7913}
7914
7915 void
7916restore_current_state(save_state_T *sst)
7917{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007918 // Restore the previous typeahead.
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007919 restore_typeahead(&sst->tabuf);
7920
7921 msg_scroll = sst->save_msg_scroll;
7922 restart_edit = sst->save_restart_edit;
7923 p_im = sst->save_insertmode;
7924 finish_op = sst->save_finish_op;
7925 opcount = sst->save_opcount;
Bram Moolenaarcce713d2019-03-04 11:40:12 +01007926 reg_executing = sst->save_reg_executing;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007927 msg_didout |= sst->save_msg_didout; // don't reset msg_didout now
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007928
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007929 // Restore the state (needed when called from a function executed for
7930 // 'indentexpr'). Update the mouse and cursor, they may have changed.
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007931 State = sst->save_State;
7932#ifdef CURSOR_SHAPE
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007933 ui_cursor_shape(); // may show different cursor shape
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007934#endif
7935}
7936
7937/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007938 * ":normal[!] {commands}": Execute normal mode commands.
7939 */
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01007940 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007941ex_normal(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007942{
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007943 save_state_T save_state;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007944 char_u *arg = NULL;
7945 int l;
7946 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007947
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00007948 if (ex_normal_lock > 0)
7949 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007950 emsg(_(e_secure));
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00007951 return;
7952 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007953 if (ex_normal_busy >= p_mmd)
7954 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007955 emsg(_("E192: Recursive use of :normal too deep"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007956 return;
7957 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007958
Bram Moolenaar071d4272004-06-13 20:20:40 +00007959 /*
7960 * vgetc() expects a CSI and K_SPECIAL to have been escaped. Don't do
7961 * this for the K_SPECIAL leading byte, otherwise special keys will not
7962 * work.
7963 */
7964 if (has_mbyte)
7965 {
7966 int len = 0;
7967
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007968 // Count the number of characters to be escaped.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007969 for (p = eap->arg; *p != NUL; ++p)
7970 {
Bram Moolenaar13505972019-01-24 15:04:48 +01007971#ifdef FEAT_GUI
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007972 if (*p == CSI) // leadbyte CSI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007973 len += 2;
Bram Moolenaar13505972019-01-24 15:04:48 +01007974#endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007975 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007976 if (*++p == K_SPECIAL // trailbyte K_SPECIAL or CSI
Bram Moolenaar13505972019-01-24 15:04:48 +01007977#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007978 || *p == CSI
Bram Moolenaar13505972019-01-24 15:04:48 +01007979#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007980 )
7981 len += 2;
7982 }
7983 if (len > 0)
7984 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02007985 arg = alloc(STRLEN(eap->arg) + len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007986 if (arg != NULL)
7987 {
7988 len = 0;
7989 for (p = eap->arg; *p != NUL; ++p)
7990 {
7991 arg[len++] = *p;
Bram Moolenaar13505972019-01-24 15:04:48 +01007992#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007993 if (*p == CSI)
7994 {
7995 arg[len++] = KS_EXTRA;
7996 arg[len++] = (int)KE_CSI;
7997 }
Bram Moolenaar13505972019-01-24 15:04:48 +01007998#endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007999 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008000 {
8001 arg[len++] = *++p;
8002 if (*p == K_SPECIAL)
8003 {
8004 arg[len++] = KS_SPECIAL;
8005 arg[len++] = KE_FILLER;
8006 }
Bram Moolenaar13505972019-01-24 15:04:48 +01008007#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008008 else if (*p == CSI)
8009 {
8010 arg[len++] = KS_EXTRA;
8011 arg[len++] = (int)KE_CSI;
8012 }
Bram Moolenaar13505972019-01-24 15:04:48 +01008013#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008014 }
8015 arg[len] = NUL;
8016 }
8017 }
8018 }
8019 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008020
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008021 ++ex_normal_busy;
8022 if (save_current_state(&save_state))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008023 {
8024 /*
8025 * Repeat the :normal command for each line in the range. When no
8026 * range given, execute it just once, without positioning the cursor
8027 * first.
8028 */
8029 do
8030 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008031 if (eap->addr_count != 0)
8032 {
8033 curwin->w_cursor.lnum = eap->line1++;
8034 curwin->w_cursor.col = 0;
Bram Moolenaard5d37532017-03-27 23:02:07 +02008035 check_cursor_moved(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008036 }
8037
Bram Moolenaar13505972019-01-24 15:04:48 +01008038 exec_normal_cmd(arg != NULL
8039 ? arg
8040 : eap->arg, eap->forceit ? REMAP_NONE : REMAP_YES, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008041 }
8042 while (eap->addr_count > 0 && eap->line1 <= eap->line2 && !got_int);
8043 }
8044
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008045 // Might not return to the main loop when in an event handler.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008046 update_topline_cursor();
8047
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008048 restore_current_state(&save_state);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008049 --ex_normal_busy;
Bram Moolenaareda73602014-11-05 09:53:23 +01008050 setmouse();
Bram Moolenaareda73602014-11-05 09:53:23 +01008051#ifdef CURSOR_SHAPE
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008052 ui_cursor_shape(); // may show different cursor shape
Bram Moolenaareda73602014-11-05 09:53:23 +01008053#endif
8054
Bram Moolenaar071d4272004-06-13 20:20:40 +00008055 vim_free(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008056}
8057
8058/*
Bram Moolenaar64969662005-12-14 21:59:55 +00008059 * ":startinsert", ":startreplace" and ":startgreplace"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008060 */
8061 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008062ex_startinsert(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008063{
Bram Moolenaarfd371682005-01-14 21:42:54 +00008064 if (eap->forceit)
8065 {
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02008066 // cursor line can be zero on startup
Bram Moolenaar09ca9322017-09-26 17:40:45 +02008067 if (!curwin->w_cursor.lnum)
8068 curwin->w_cursor.lnum = 1;
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02008069 set_cursor_for_append_to_line();
Bram Moolenaarfd371682005-01-14 21:42:54 +00008070 }
Bram Moolenaareb58a242020-04-19 18:13:19 +02008071#ifdef FEAT_TERMINAL
8072 // Ignore this when running in an active terminal.
8073 if (term_job_running(curbuf->b_term))
8074 return;
8075#endif
Bram Moolenaarfd371682005-01-14 21:42:54 +00008076
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02008077 // Ignore the command when already in Insert mode. Inserting an
8078 // expression register that invokes a function can do this.
Bram Moolenaara40c5002005-01-09 21:16:21 +00008079 if (State & INSERT)
8080 return;
8081
Bram Moolenaar64969662005-12-14 21:59:55 +00008082 if (eap->cmdidx == CMD_startinsert)
8083 restart_edit = 'a';
8084 else if (eap->cmdidx == CMD_startreplace)
8085 restart_edit = 'R';
Bram Moolenaar071d4272004-06-13 20:20:40 +00008086 else
Bram Moolenaar64969662005-12-14 21:59:55 +00008087 restart_edit = 'V';
8088
8089 if (!eap->forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008090 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00008091 if (eap->cmdidx == CMD_startinsert)
8092 restart_edit = 'i';
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02008093 curwin->w_curswant = 0; // avoid MAXCOL
Bram Moolenaar071d4272004-06-13 20:20:40 +00008094 }
8095}
8096
8097/*
8098 * ":stopinsert"
8099 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008100 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008101ex_stopinsert(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008102{
8103 restart_edit = 0;
8104 stop_insert_mode = TRUE;
Bram Moolenaarfd773e92016-04-02 19:39:16 +02008105 clearmode();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008106}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008107
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008108/*
8109 * Execute normal mode command "cmd".
8110 * "remap" can be REMAP_NONE or REMAP_YES.
8111 */
8112 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008113exec_normal_cmd(char_u *cmd, int remap, int silent)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008114{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008115 // Stuff the argument into the typeahead buffer.
Bram Moolenaar25281632016-01-21 23:32:32 +01008116 ins_typebuf(cmd, remap, 0, TRUE, silent);
Bram Moolenaar586c70c2018-10-02 16:23:58 +02008117 exec_normal(FALSE, FALSE, FALSE);
Bram Moolenaar25281632016-01-21 23:32:32 +01008118}
Bram Moolenaar25281632016-01-21 23:32:32 +01008119
Bram Moolenaar25281632016-01-21 23:32:32 +01008120/*
8121 * Execute normal_cmd() until there is no typeahead left.
Bram Moolenaar586c70c2018-10-02 16:23:58 +02008122 * When "use_vpeekc" is TRUE use vpeekc() to check for available chars.
Bram Moolenaar25281632016-01-21 23:32:32 +01008123 */
8124 void
Bram Moolenaar586c70c2018-10-02 16:23:58 +02008125exec_normal(int was_typed, int use_vpeekc, int may_use_terminal_loop UNUSED)
Bram Moolenaar25281632016-01-21 23:32:32 +01008126{
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008127 oparg_T oa;
Bram Moolenaar905dd902019-04-07 14:21:47 +02008128 int c;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008129
Bram Moolenaar905dd902019-04-07 14:21:47 +02008130 // When calling vpeekc() from feedkeys() it will return Ctrl_C when there
8131 // is nothing to get, so also check for Ctrl_C.
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008132 clear_oparg(&oa);
8133 finish_op = FALSE;
Bram Moolenaar586c70c2018-10-02 16:23:58 +02008134 while ((!stuff_empty()
8135 || ((was_typed || !typebuf_typed()) && typebuf.tb_len > 0)
Bram Moolenaar905dd902019-04-07 14:21:47 +02008136 || (use_vpeekc && (c = vpeekc()) != NUL && c != Ctrl_C))
Bram Moolenaar586c70c2018-10-02 16:23:58 +02008137 && !got_int)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008138 {
8139 update_topline_cursor();
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02008140#ifdef FEAT_TERMINAL
Bram Moolenaar655a82a2018-05-08 22:01:07 +02008141 if (may_use_terminal_loop && term_use_loop()
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02008142 && oa.op_type == OP_NOP && oa.regname == NUL
8143 && !VIsual_active)
8144 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008145 // If terminal_loop() returns OK we got a key that is handled
8146 // in Normal model. With FAIL we first need to position the
8147 // cursor and the screen needs to be redrawn.
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02008148 if (terminal_loop(TRUE) == OK)
8149 normal_cmd(&oa, TRUE);
8150 }
8151 else
8152#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008153 // execute a Normal mode cmd
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02008154 normal_cmd(&oa, TRUE);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008155 }
8156}
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008157
Bram Moolenaar071d4272004-06-13 20:20:40 +00008158#ifdef FEAT_FIND_ID
8159 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008160ex_checkpath(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008161{
8162 find_pattern_in_path(NULL, 0, 0, FALSE, FALSE, CHECK_PATH, 1L,
8163 eap->forceit ? ACTION_SHOW_ALL : ACTION_SHOW,
8164 (linenr_T)1, (linenr_T)MAXLNUM);
8165}
8166
Bram Moolenaar4033c552017-09-16 20:54:51 +02008167#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008168/*
8169 * ":psearch"
8170 */
8171 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008172ex_psearch(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008173{
8174 g_do_tagpreview = p_pvh;
8175 ex_findpat(eap);
8176 g_do_tagpreview = 0;
8177}
8178#endif
8179
8180 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008181ex_findpat(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008182{
8183 int whole = TRUE;
8184 long n;
8185 char_u *p;
8186 int action;
8187
8188 switch (cmdnames[eap->cmdidx].cmd_name[2])
8189 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008190 case 'e': // ":psearch", ":isearch" and ":dsearch"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008191 if (cmdnames[eap->cmdidx].cmd_name[0] == 'p')
8192 action = ACTION_GOTO;
8193 else
8194 action = ACTION_SHOW;
8195 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008196 case 'i': // ":ilist" and ":dlist"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008197 action = ACTION_SHOW_ALL;
8198 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008199 case 'u': // ":ijump" and ":djump"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008200 action = ACTION_GOTO;
8201 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008202 default: // ":isplit" and ":dsplit"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008203 action = ACTION_SPLIT;
8204 break;
8205 }
8206
8207 n = 1;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008208 if (vim_isdigit(*eap->arg)) // get count
Bram Moolenaar071d4272004-06-13 20:20:40 +00008209 {
8210 n = getdigits(&eap->arg);
8211 eap->arg = skipwhite(eap->arg);
8212 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008213 if (*eap->arg == '/') // Match regexp, not just whole words
Bram Moolenaar071d4272004-06-13 20:20:40 +00008214 {
8215 whole = FALSE;
8216 ++eap->arg;
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02008217 p = skip_regexp(eap->arg, '/', p_magic);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008218 if (*p)
8219 {
8220 *p++ = NUL;
8221 p = skipwhite(p);
8222
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008223 // Check for trailing illegal characters
Bram Moolenaarfaac4102020-04-20 17:46:14 +02008224 if (!ends_excmd2(eap->arg, p))
Bram Moolenaar8930caa2020-07-23 16:37:03 +02008225 eap->errmsg = ex_errmsg(e_trailing_arg, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008226 else
8227 eap->nextcmd = check_nextcmd(p);
8228 }
8229 }
8230 if (!eap->skip)
8231 find_pattern_in_path(eap->arg, 0, (int)STRLEN(eap->arg),
8232 whole, !eap->forceit,
8233 *eap->cmd == 'd' ? FIND_DEFINE : FIND_ANY,
8234 n, action, eap->line1, eap->line2);
8235}
8236#endif
8237
Bram Moolenaar071d4272004-06-13 20:20:40 +00008238
Bram Moolenaar4033c552017-09-16 20:54:51 +02008239#ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00008240/*
8241 * ":ptag", ":ptselect", ":ptjump", ":ptnext", etc.
8242 */
8243 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008244ex_ptag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008245{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008246 g_do_tagpreview = p_pvh; // will be reset to 0 in ex_tag_cmd()
Bram Moolenaar071d4272004-06-13 20:20:40 +00008247 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
8248}
8249
8250/*
8251 * ":pedit"
8252 */
8253 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008254ex_pedit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008255{
8256 win_T *curwin_save = curwin;
8257
Bram Moolenaar3a2505c2020-03-09 16:40:41 +01008258 if (ERROR_IF_ANY_POPUP_WINDOW)
8259 return;
8260
Bram Moolenaar026587b2019-08-17 15:08:00 +02008261 // Open the preview window or popup and make it the current window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008262 g_do_tagpreview = p_pvh;
Bram Moolenaar576a4a62019-08-18 15:25:17 +02008263 prepare_tagpreview(TRUE, TRUE, FALSE);
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02008264
Bram Moolenaar026587b2019-08-17 15:08:00 +02008265 // Edit the file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008266 do_exedit(eap, NULL);
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02008267
Bram Moolenaar071d4272004-06-13 20:20:40 +00008268 if (curwin != curwin_save && win_valid(curwin_save))
8269 {
Bram Moolenaar026587b2019-08-17 15:08:00 +02008270 // Return cursor to where we were
Bram Moolenaar071d4272004-06-13 20:20:40 +00008271 validate_cursor();
8272 redraw_later(VALID);
8273 win_enter(curwin_save, TRUE);
8274 }
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01008275# ifdef FEAT_PROP_POPUP
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02008276 else if (WIN_IS_POPUP(curwin))
8277 {
8278 // can't keep focus in popup window
8279 win_enter(firstwin, TRUE);
8280 }
8281# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008282 g_do_tagpreview = 0;
8283}
Bram Moolenaar4033c552017-09-16 20:54:51 +02008284#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008285
8286/*
8287 * ":stag", ":stselect" and ":stjump".
8288 */
8289 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008290ex_stag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008291{
8292 postponed_split = -1;
8293 postponed_split_flags = cmdmod.split;
Bram Moolenaard326ce82007-03-11 14:48:29 +00008294 postponed_split_tab = cmdmod.tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008295 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
8296 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00008297 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008298}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008299
8300/*
8301 * ":tag", ":tselect", ":tjump", ":tnext", etc.
8302 */
8303 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008304ex_tag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008305{
8306 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name);
8307}
8308
8309 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008310ex_tag_cmd(exarg_T *eap, char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008311{
8312 int cmd;
8313
8314 switch (name[1])
8315 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008316 case 'j': cmd = DT_JUMP; // ":tjump"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008317 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008318 case 's': cmd = DT_SELECT; // ":tselect"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008319 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008320 case 'p': cmd = DT_PREV; // ":tprevious"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008321 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008322 case 'N': cmd = DT_PREV; // ":tNext"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008323 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008324 case 'n': cmd = DT_NEXT; // ":tnext"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008325 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008326 case 'o': cmd = DT_POP; // ":pop"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008327 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008328 case 'f': // ":tfirst"
8329 case 'r': cmd = DT_FIRST; // ":trewind"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008330 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008331 case 'l': cmd = DT_LAST; // ":tlast"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008332 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008333 default: // ":tag"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008334#ifdef FEAT_CSCOPE
Bram Moolenaar7c94c262008-06-20 09:11:34 +00008335 if (p_cst && *eap->arg != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008336 {
Bram Moolenaard4db7712016-11-12 19:16:46 +01008337 ex_cstag(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008338 return;
8339 }
8340#endif
8341 cmd = DT_TAG;
8342 break;
8343 }
8344
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00008345 if (name[0] == 'l')
8346 {
8347#ifndef FEAT_QUICKFIX
8348 ex_ni(eap);
8349 return;
8350#else
8351 cmd = DT_LTAG;
8352#endif
8353 }
8354
Bram Moolenaar071d4272004-06-13 20:20:40 +00008355 do_tag(eap->arg, cmd, eap->addr_count > 0 ? (int)eap->line2 : 1,
8356 eap->forceit, TRUE);
8357}
8358
8359/*
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008360 * Check "str" for starting with a special cmdline variable.
8361 * If found return one of the SPEC_ values and set "*usedlen" to the length of
8362 * the variable. Otherwise return -1 and "*usedlen" is unchanged.
8363 */
8364 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008365find_cmdline_var(char_u *src, int *usedlen)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008366{
8367 int len;
8368 int i;
Bram Moolenaar8f0b2d42009-05-16 14:41:10 +00008369 static char *(spec_str[]) = {
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008370 "%",
8371#define SPEC_PERC 0
8372 "#",
Bram Moolenaar65f08472017-09-10 18:16:20 +02008373#define SPEC_HASH (SPEC_PERC + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008374 "<cword>", // cursor word
Bram Moolenaar65f08472017-09-10 18:16:20 +02008375#define SPEC_CWORD (SPEC_HASH + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008376 "<cWORD>", // cursor WORD
Bram Moolenaar65f08472017-09-10 18:16:20 +02008377#define SPEC_CCWORD (SPEC_CWORD + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008378 "<cexpr>", // expr under cursor
Bram Moolenaar65f08472017-09-10 18:16:20 +02008379#define SPEC_CEXPR (SPEC_CCWORD + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008380 "<cfile>", // cursor path name
Bram Moolenaar65f08472017-09-10 18:16:20 +02008381#define SPEC_CFILE (SPEC_CEXPR + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008382 "<sfile>", // ":so" file name
Bram Moolenaar65f08472017-09-10 18:16:20 +02008383#define SPEC_SFILE (SPEC_CFILE + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008384 "<slnum>", // ":so" file line number
Bram Moolenaar65f08472017-09-10 18:16:20 +02008385#define SPEC_SLNUM (SPEC_SFILE + 1)
Bram Moolenaara5d04232020-07-26 15:37:02 +02008386 "<stack>", // call stack
8387#define SPEC_STACK (SPEC_SLNUM + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008388 "<afile>", // autocommand file name
Bram Moolenaara5d04232020-07-26 15:37:02 +02008389#define SPEC_AFILE (SPEC_STACK + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008390 "<abuf>", // autocommand buffer number
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008391#define SPEC_ABUF (SPEC_AFILE + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008392 "<amatch>", // autocommand match name
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01008393#define SPEC_AMATCH (SPEC_ABUF + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008394 "<sflnum>", // script file line number
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008395#define SPEC_SFLNUM (SPEC_AMATCH + 1)
Bram Moolenaar90944302020-08-01 20:45:11 +02008396 "<SID>", // script ID: <SNR>123_
8397#define SPEC_SID (SPEC_SFLNUM + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008398#ifdef FEAT_CLIENTSERVER
8399 "<client>"
Bram Moolenaar90944302020-08-01 20:45:11 +02008400# define SPEC_CLIENT (SPEC_SID + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008401#endif
8402 };
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008403
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00008404 for (i = 0; i < (int)(sizeof(spec_str) / sizeof(char *)); ++i)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008405 {
8406 len = (int)STRLEN(spec_str[i]);
8407 if (STRNCMP(src, spec_str[i], len) == 0)
8408 {
8409 *usedlen = len;
8410 return i;
8411 }
8412 }
8413 return -1;
8414}
8415
8416/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008417 * Evaluate cmdline variables.
8418 *
8419 * change '%' to curbuf->b_ffname
8420 * '#' to curwin->w_altfile
8421 * '<cword>' to word under the cursor
8422 * '<cWORD>' to WORD under the cursor
Bram Moolenaar8071cb22019-07-12 17:58:01 +02008423 * '<cexpr>' to C-expression under the cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00008424 * '<cfile>' to path name under the cursor
8425 * '<sfile>' to sourced file name
Bram Moolenaar4f25b1a2020-09-10 19:25:05 +02008426 * '<stack>' to call stack
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01008427 * '<slnum>' to sourced file line number
Bram Moolenaar071d4272004-06-13 20:20:40 +00008428 * '<afile>' to file name for autocommand
8429 * '<abuf>' to buffer number for autocommand
8430 * '<amatch>' to matching name for autocommand
8431 *
8432 * When an error is detected, "errormsg" is set to a non-NULL pointer (may be
8433 * "" for error without a message) and NULL is returned.
8434 * Returns an allocated string if a valid match was found.
8435 * Returns NULL if no match was found. "usedlen" then still contains the
8436 * number of characters to skip.
8437 */
8438 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008439eval_vars(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008440 char_u *src, // pointer into commandline
8441 char_u *srcstart, // beginning of valid memory for src
8442 int *usedlen, // characters after src that are used
8443 linenr_T *lnump, // line number for :e command, or NULL
8444 char **errormsg, // pointer to error message
8445 int *escaped) // return value has escaped white space (can
8446 // be NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008447{
8448 int i;
8449 char_u *s;
8450 char_u *result;
8451 char_u *resultbuf = NULL;
8452 int resultlen;
8453 buf_T *buf;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008454 int valid = VALID_HEAD + VALID_PATH; // assume valid result
Bram Moolenaar071d4272004-06-13 20:20:40 +00008455 int spec_idx;
Bram Moolenaarfd249462018-07-28 16:14:30 +02008456 int tilde_file = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008457 int skip_mod = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008458 char_u strbuf[30];
Bram Moolenaar071d4272004-06-13 20:20:40 +00008459
8460 *errormsg = NULL;
Bram Moolenaar63b92542007-03-27 14:57:09 +00008461 if (escaped != NULL)
8462 *escaped = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008463
8464 /*
8465 * Check if there is something to do.
8466 */
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008467 spec_idx = find_cmdline_var(src, usedlen);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008468 if (spec_idx < 0) // no match
Bram Moolenaar071d4272004-06-13 20:20:40 +00008469 {
8470 *usedlen = 1;
8471 return NULL;
8472 }
8473
8474 /*
8475 * Skip when preceded with a backslash "\%" and "\#".
8476 * Note: In "\\%" the % is also not recognized!
8477 */
8478 if (src > srcstart && src[-1] == '\\')
8479 {
8480 *usedlen = 0;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008481 STRMOVE(src - 1, src); // remove backslash
Bram Moolenaar071d4272004-06-13 20:20:40 +00008482 return NULL;
8483 }
8484
8485 /*
8486 * word or WORD under cursor
8487 */
Bram Moolenaar65f08472017-09-10 18:16:20 +02008488 if (spec_idx == SPEC_CWORD || spec_idx == SPEC_CCWORD
8489 || spec_idx == SPEC_CEXPR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008490 {
Bram Moolenaar65f08472017-09-10 18:16:20 +02008491 resultlen = find_ident_under_cursor(&result,
8492 spec_idx == SPEC_CWORD ? (FIND_IDENT | FIND_STRING)
8493 : spec_idx == SPEC_CEXPR ? (FIND_IDENT | FIND_STRING | FIND_EVAL)
8494 : FIND_STRING);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008495 if (resultlen == 0)
8496 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008497 *errormsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +00008498 return NULL;
8499 }
8500 }
8501
8502 /*
8503 * '#': Alternate file name
8504 * '%': Current file name
8505 * File name under the cursor
8506 * File name for autocommand
8507 * and following modifiers
8508 */
8509 else
8510 {
8511 switch (spec_idx)
8512 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008513 case SPEC_PERC: // '%': current file
Bram Moolenaar071d4272004-06-13 20:20:40 +00008514 if (curbuf->b_fname == NULL)
8515 {
8516 result = (char_u *)"";
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008517 valid = 0; // Must have ":p:h" to be valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00008518 }
8519 else
Bram Moolenaar00136dc2018-07-25 21:19:13 +02008520 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008521 result = curbuf->b_fname;
Bram Moolenaar00136dc2018-07-25 21:19:13 +02008522 tilde_file = STRCMP(result, "~") == 0;
8523 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008524 break;
8525
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008526 case SPEC_HASH: // '#' or "#99": alternate file
8527 if (src[1] == '#') // "##": the argument list
Bram Moolenaar071d4272004-06-13 20:20:40 +00008528 {
8529 result = arg_all();
8530 resultbuf = result;
8531 *usedlen = 2;
Bram Moolenaar63b92542007-03-27 14:57:09 +00008532 if (escaped != NULL)
8533 *escaped = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008534 skip_mod = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008535 break;
8536 }
8537 s = src + 1;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008538 if (*s == '<') // "#<99" uses v:oldfiles
Bram Moolenaard812df62008-11-09 12:46:09 +00008539 ++s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008540 i = (int)getdigits(&s);
Bram Moolenaarc312b8b2017-10-28 17:53:04 +02008541 if (s == src + 2 && src[1] == '-')
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008542 // just a minus sign, don't skip over it
Bram Moolenaarc312b8b2017-10-28 17:53:04 +02008543 s--;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008544 *usedlen = (int)(s - src); // length of what we expand
Bram Moolenaar071d4272004-06-13 20:20:40 +00008545
Bram Moolenaarc312b8b2017-10-28 17:53:04 +02008546 if (src[1] == '<' && i != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008547 {
Bram Moolenaard812df62008-11-09 12:46:09 +00008548 if (*usedlen < 2)
8549 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008550 // Should we give an error message for #<text?
Bram Moolenaard812df62008-11-09 12:46:09 +00008551 *usedlen = 1;
8552 return NULL;
8553 }
8554#ifdef FEAT_EVAL
8555 result = list_find_str(get_vim_var_list(VV_OLDFILES),
8556 (long)i);
8557 if (result == NULL)
8558 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008559 *errormsg = "";
Bram Moolenaard812df62008-11-09 12:46:09 +00008560 return NULL;
8561 }
8562#else
Bram Moolenaar8e481e82019-01-15 20:07:48 +01008563 *errormsg = _("E809: #< is not available without the +eval feature");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008564 return NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +00008565#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008566 }
8567 else
Bram Moolenaard812df62008-11-09 12:46:09 +00008568 {
Bram Moolenaarc312b8b2017-10-28 17:53:04 +02008569 if (i == 0 && src[1] == '<' && *usedlen > 1)
8570 *usedlen = 1;
Bram Moolenaard812df62008-11-09 12:46:09 +00008571 buf = buflist_findnr(i);
8572 if (buf == NULL)
8573 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008574 *errormsg = _("E194: No alternate file name to substitute for '#'");
Bram Moolenaard812df62008-11-09 12:46:09 +00008575 return NULL;
8576 }
8577 if (lnump != NULL)
8578 *lnump = ECMD_LAST;
8579 if (buf->b_fname == NULL)
8580 {
8581 result = (char_u *)"";
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008582 valid = 0; // Must have ":p:h" to be valid
Bram Moolenaard812df62008-11-09 12:46:09 +00008583 }
8584 else
Bram Moolenaar00136dc2018-07-25 21:19:13 +02008585 {
Bram Moolenaard812df62008-11-09 12:46:09 +00008586 result = buf->b_fname;
Bram Moolenaar00136dc2018-07-25 21:19:13 +02008587 tilde_file = STRCMP(result, "~") == 0;
8588 }
Bram Moolenaard812df62008-11-09 12:46:09 +00008589 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008590 break;
8591
8592#ifdef FEAT_SEARCHPATH
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008593 case SPEC_CFILE: // file name under cursor
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008594 result = file_name_at_cursor(FNAME_MESS|FNAME_HYP, 1L, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008595 if (result == NULL)
8596 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008597 *errormsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +00008598 return NULL;
8599 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008600 resultbuf = result; // remember allocated string
Bram Moolenaar071d4272004-06-13 20:20:40 +00008601 break;
8602#endif
8603
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008604 case SPEC_AFILE: // file name for autocommand
Bram Moolenaar071d4272004-06-13 20:20:40 +00008605 result = autocmd_fname;
Bram Moolenaarf6dad432008-09-18 19:29:58 +00008606 if (result != NULL && !autocmd_fname_full)
8607 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008608 // Still need to turn the fname into a full path. It is
8609 // postponed to avoid a delay when <afile> is not used.
Bram Moolenaarf6dad432008-09-18 19:29:58 +00008610 autocmd_fname_full = TRUE;
8611 result = FullName_save(autocmd_fname, FALSE);
8612 vim_free(autocmd_fname);
8613 autocmd_fname = result;
8614 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008615 if (result == NULL)
8616 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008617 *errormsg = _("E495: no autocommand file name to substitute for \"<afile>\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008618 return NULL;
8619 }
Bram Moolenaara0174af2008-01-02 20:08:25 +00008620 result = shorten_fname1(result);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008621 break;
8622
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008623 case SPEC_ABUF: // buffer number for autocommand
Bram Moolenaar071d4272004-06-13 20:20:40 +00008624 if (autocmd_bufnr <= 0)
8625 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008626 *errormsg = _("E496: no autocommand buffer number to substitute for \"<abuf>\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008627 return NULL;
8628 }
8629 sprintf((char *)strbuf, "%d", autocmd_bufnr);
8630 result = strbuf;
8631 break;
8632
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008633 case SPEC_AMATCH: // match name for autocommand
Bram Moolenaar071d4272004-06-13 20:20:40 +00008634 result = autocmd_match;
8635 if (result == NULL)
8636 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008637 *errormsg = _("E497: no autocommand match name to substitute for \"<amatch>\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008638 return NULL;
8639 }
8640 break;
8641
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008642 case SPEC_SFILE: // file name for ":so" command
Bram Moolenaara5d04232020-07-26 15:37:02 +02008643 case SPEC_STACK: // call stack
Bram Moolenaar4f25b1a2020-09-10 19:25:05 +02008644 result = estack_sfile(spec_idx == SPEC_SFILE
8645 ? ESTACK_SFILE : ESTACK_STACK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008646 if (result == NULL)
8647 {
Bram Moolenaara5d04232020-07-26 15:37:02 +02008648 *errormsg = spec_idx == SPEC_SFILE
8649 ? _("E498: no :source file name to substitute for \"<sfile>\"")
8650 : _("E489: no call stack to substitute for \"<stack>\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008651 return NULL;
8652 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01008653 resultbuf = result; // remember allocated string
Bram Moolenaar071d4272004-06-13 20:20:40 +00008654 break;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008655
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008656 case SPEC_SLNUM: // line in file for ":so" command
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01008657 if (SOURCING_NAME == NULL || SOURCING_LNUM == 0)
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01008658 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008659 *errormsg = _("E842: no line number to use for \"<slnum>\"");
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01008660 return NULL;
8661 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01008662 sprintf((char *)strbuf, "%ld", SOURCING_LNUM);
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01008663 result = strbuf;
8664 break;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008665
8666#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008667 case SPEC_SFLNUM: // line in script file
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01008668 if (current_sctx.sc_lnum + SOURCING_LNUM == 0)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008669 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008670 *errormsg = _("E961: no line number to use for \"<sflnum>\"");
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008671 return NULL;
8672 }
8673 sprintf((char *)strbuf, "%ld",
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01008674 (long)(current_sctx.sc_lnum + SOURCING_LNUM));
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008675 result = strbuf;
8676 break;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008677
Bram Moolenaar90944302020-08-01 20:45:11 +02008678 case SPEC_SID:
8679 if (current_sctx.sc_sid <= 0)
8680 {
8681 *errormsg = _(e_usingsid);
8682 return NULL;
8683 }
8684 sprintf((char *)strbuf, "<SNR>%d_", current_sctx.sc_sid);
8685 result = strbuf;
8686 break;
Bram Moolenaare4218b92020-08-01 21:11:38 +02008687#endif
Bram Moolenaar90944302020-08-01 20:45:11 +02008688
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008689#ifdef FEAT_CLIENTSERVER
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008690 case SPEC_CLIENT: // Source of last submitted input
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008691 sprintf((char *)strbuf, PRINTF_HEX_LONG_U,
8692 (long_u)clientWindow);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008693 result = strbuf;
8694 break;
8695#endif
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008696
Bram Moolenaar9e0f6ec2017-05-16 09:36:54 +02008697 default:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008698 result = (char_u *)""; // avoid gcc warning
Bram Moolenaar9e0f6ec2017-05-16 09:36:54 +02008699 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008700 }
8701
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008702 resultlen = (int)STRLEN(result); // length of new string
8703 if (src[*usedlen] == '<') // remove the file name extension
Bram Moolenaar071d4272004-06-13 20:20:40 +00008704 {
8705 ++*usedlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008706 if ((s = vim_strrchr(result, '.')) != NULL && s >= gettail(result))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008707 resultlen = (int)(s - result);
8708 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008709 else if (!skip_mod)
8710 {
Bram Moolenaar00136dc2018-07-25 21:19:13 +02008711 valid |= modify_fname(src, tilde_file, usedlen, &result, &resultbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008712 &resultlen);
8713 if (result == NULL)
8714 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008715 *errormsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +00008716 return NULL;
8717 }
8718 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008719 }
8720
8721 if (resultlen == 0 || valid != VALID_HEAD + VALID_PATH)
8722 {
8723 if (valid != VALID_HEAD + VALID_PATH)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008724 // xgettext:no-c-format
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008725 *errormsg = _("E499: Empty file name for '%' or '#', only works with \":p:h\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008726 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008727 *errormsg = _("E500: Evaluates to an empty string");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008728 result = NULL;
8729 }
8730 else
8731 result = vim_strnsave(result, resultlen);
8732 vim_free(resultbuf);
8733 return result;
8734}
8735
8736/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008737 * Expand the <sfile> string in "arg".
8738 *
8739 * Returns an allocated string, or NULL for any error.
8740 */
8741 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008742expand_sfile(char_u *arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008743{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008744 char *errormsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008745 int len;
8746 char_u *result;
8747 char_u *newres;
8748 char_u *repl;
8749 int srclen;
8750 char_u *p;
8751
8752 result = vim_strsave(arg);
8753 if (result == NULL)
8754 return NULL;
8755
8756 for (p = result; *p; )
8757 {
8758 if (STRNCMP(p, "<sfile>", 7) != 0)
8759 ++p;
8760 else
8761 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008762 // replace "<sfile>" with the sourced file name, and do ":" stuff
Bram Moolenaar63b92542007-03-27 14:57:09 +00008763 repl = eval_vars(p, result, &srclen, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008764 if (errormsg != NULL)
8765 {
8766 if (*errormsg)
8767 emsg(errormsg);
8768 vim_free(result);
8769 return NULL;
8770 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008771 if (repl == NULL) // no match (cannot happen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008772 {
8773 p += srclen;
8774 continue;
8775 }
8776 len = (int)STRLEN(result) - srclen + (int)STRLEN(repl) + 1;
8777 newres = alloc(len);
8778 if (newres == NULL)
8779 {
8780 vim_free(repl);
8781 vim_free(result);
8782 return NULL;
8783 }
8784 mch_memmove(newres, result, (size_t)(p - result));
8785 STRCPY(newres + (p - result), repl);
8786 len = (int)STRLEN(newres);
8787 STRCAT(newres, p + srclen);
8788 vim_free(repl);
8789 vim_free(result);
8790 result = newres;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008791 p = newres + len; // continue after the match
Bram Moolenaar071d4272004-06-13 20:20:40 +00008792 }
8793 }
8794
8795 return result;
8796}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008797
Bram Moolenaar071d4272004-06-13 20:20:40 +00008798#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG) || defined(PROTO)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00008799/*
Bram Moolenaard9462e32011-04-11 21:35:11 +02008800 * Make a dialog message in "buff[DIALOG_MSG_SIZE]".
Bram Moolenaarb765d632005-06-07 21:00:02 +00008801 * "format" must contain "%s".
Bram Moolenaar9c13b352005-05-19 20:53:52 +00008802 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008803 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008804dialog_msg(char_u *buff, char *format, char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008805{
Bram Moolenaar071d4272004-06-13 20:20:40 +00008806 if (fname == NULL)
8807 fname = (char_u *)_("Untitled");
Bram Moolenaard9462e32011-04-11 21:35:11 +02008808 vim_snprintf((char *)buff, DIALOG_MSG_SIZE, format, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008809}
8810#endif
8811
8812/*
8813 * ":behave {mswin,xterm}"
8814 */
8815 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008816ex_behave(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008817{
8818 if (STRCMP(eap->arg, "mswin") == 0)
8819 {
8820 set_option_value((char_u *)"selection", 0L, (char_u *)"exclusive", 0);
8821 set_option_value((char_u *)"selectmode", 0L, (char_u *)"mouse,key", 0);
8822 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"popup", 0);
8823 set_option_value((char_u *)"keymodel", 0L,
8824 (char_u *)"startsel,stopsel", 0);
8825 }
8826 else if (STRCMP(eap->arg, "xterm") == 0)
8827 {
8828 set_option_value((char_u *)"selection", 0L, (char_u *)"inclusive", 0);
8829 set_option_value((char_u *)"selectmode", 0L, (char_u *)"", 0);
8830 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"extend", 0);
8831 set_option_value((char_u *)"keymodel", 0L, (char_u *)"", 0);
8832 }
8833 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008834 semsg(_(e_invarg2), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008835}
8836
Bram Moolenaar071d4272004-06-13 20:20:40 +00008837static int filetype_detect = FALSE;
8838static int filetype_plugin = FALSE;
8839static int filetype_indent = FALSE;
8840
8841/*
8842 * ":filetype [plugin] [indent] {on,off,detect}"
8843 * on: Load the filetype.vim file to install autocommands for file types.
8844 * off: Load the ftoff.vim file to remove all autocommands for file types.
8845 * plugin on: load filetype.vim and ftplugin.vim
8846 * plugin off: load ftplugof.vim
8847 * indent on: load filetype.vim and indent.vim
8848 * indent off: load indoff.vim
8849 */
8850 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008851ex_filetype(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008852{
8853 char_u *arg = eap->arg;
8854 int plugin = FALSE;
8855 int indent = FALSE;
8856
8857 if (*eap->arg == NUL)
8858 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008859 // Print current status.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008860 smsg("filetype detection:%s plugin:%s indent:%s",
Bram Moolenaar071d4272004-06-13 20:20:40 +00008861 filetype_detect ? "ON" : "OFF",
8862 filetype_plugin ? (filetype_detect ? "ON" : "(on)") : "OFF",
8863 filetype_indent ? (filetype_detect ? "ON" : "(on)") : "OFF");
8864 return;
8865 }
8866
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008867 // Accept "plugin" and "indent" in any order.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008868 for (;;)
8869 {
8870 if (STRNCMP(arg, "plugin", 6) == 0)
8871 {
8872 plugin = TRUE;
8873 arg = skipwhite(arg + 6);
8874 continue;
8875 }
8876 if (STRNCMP(arg, "indent", 6) == 0)
8877 {
8878 indent = TRUE;
8879 arg = skipwhite(arg + 6);
8880 continue;
8881 }
8882 break;
8883 }
8884 if (STRCMP(arg, "on") == 0 || STRCMP(arg, "detect") == 0)
8885 {
8886 if (*arg == 'o' || !filetype_detect)
8887 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01008888 source_runtime((char_u *)FILETYPE_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008889 filetype_detect = TRUE;
8890 if (plugin)
8891 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01008892 source_runtime((char_u *)FTPLUGIN_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008893 filetype_plugin = TRUE;
8894 }
8895 if (indent)
8896 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01008897 source_runtime((char_u *)INDENT_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008898 filetype_indent = TRUE;
8899 }
8900 }
8901 if (*arg == 'd')
8902 {
Bram Moolenaar1610d052016-06-09 22:53:01 +02008903 (void)do_doautocmd((char_u *)"filetypedetect BufRead", TRUE, NULL);
Bram Moolenaara3227e22006-03-08 21:32:40 +00008904 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008905 }
8906 }
8907 else if (STRCMP(arg, "off") == 0)
8908 {
8909 if (plugin || indent)
8910 {
8911 if (plugin)
8912 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01008913 source_runtime((char_u *)FTPLUGOF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008914 filetype_plugin = FALSE;
8915 }
8916 if (indent)
8917 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01008918 source_runtime((char_u *)INDOFF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008919 filetype_indent = FALSE;
8920 }
8921 }
8922 else
8923 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01008924 source_runtime((char_u *)FTOFF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008925 filetype_detect = FALSE;
8926 }
8927 }
8928 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008929 semsg(_(e_invarg2), arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008930}
8931
8932/*
Bram Moolenaar3e545692017-06-04 19:00:32 +02008933 * ":setfiletype [FALLBACK] {name}"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008934 */
8935 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008936ex_setfiletype(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008937{
8938 if (!did_filetype)
Bram Moolenaar3e545692017-06-04 19:00:32 +02008939 {
8940 char_u *arg = eap->arg;
8941
8942 if (STRNCMP(arg, "FALLBACK ", 9) == 0)
8943 arg += 9;
8944
8945 set_option_value((char_u *)"filetype", 0L, arg, OPT_LOCAL);
8946 if (arg != eap->arg)
8947 did_filetype = FALSE;
8948 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008949}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008950
Bram Moolenaar071d4272004-06-13 20:20:40 +00008951 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008952ex_digraphs(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008953{
8954#ifdef FEAT_DIGRAPHS
8955 if (*eap->arg != NUL)
8956 putdigraph(eap->arg);
8957 else
Bram Moolenaareae8ae12018-12-14 18:53:02 +01008958 listdigraphs(eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008959#else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008960 emsg(_("E196: No digraphs in this version"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008961#endif
8962}
8963
Bram Moolenaar451fc7b2018-04-27 22:53:07 +02008964#if defined(FEAT_SEARCH_EXTRA) || defined(PROTO)
8965 void
8966set_no_hlsearch(int flag)
8967{
8968 no_hlsearch = flag;
8969# ifdef FEAT_EVAL
8970 set_vim_var_nr(VV_HLSEARCH, !no_hlsearch && p_hls);
8971# endif
8972}
8973
Bram Moolenaar071d4272004-06-13 20:20:40 +00008974/*
8975 * ":nohlsearch"
8976 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008977 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008978ex_nohlsearch(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008979{
Bram Moolenaar451fc7b2018-04-27 22:53:07 +02008980 set_no_hlsearch(TRUE);
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00008981 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008982}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008983#endif
8984
8985#ifdef FEAT_CRYPT
8986/*
8987 * ":X": Get crypt key
8988 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008989 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008990ex_X(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008991{
Bram Moolenaar3a0c9082014-11-12 15:15:42 +01008992 crypt_check_current_method();
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02008993 (void)crypt_get_key(TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008994}
8995#endif
8996
8997#ifdef FEAT_FOLDING
8998 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008999ex_fold(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009000{
9001 if (foldManualAllowed(TRUE))
9002 foldCreate(eap->line1, eap->line2);
9003}
9004
9005 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009006ex_foldopen(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009007{
9008 opFoldRange(eap->line1, eap->line2, eap->cmdidx == CMD_foldopen,
9009 eap->forceit, FALSE);
9010}
9011
9012 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009013ex_folddo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009014{
9015 linenr_T lnum;
9016
Bram Moolenaar4facea32019-10-12 20:17:40 +02009017# ifdef FEAT_CLIPBOARD
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02009018 start_global_changes();
Bram Moolenaar4facea32019-10-12 20:17:40 +02009019# endif
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02009020
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009021 // First set the marks for all lines closed/open.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009022 for (lnum = eap->line1; lnum <= eap->line2; ++lnum)
9023 if (hasFolding(lnum, NULL, NULL) == (eap->cmdidx == CMD_folddoclosed))
9024 ml_setmarked(lnum);
9025
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009026 // Execute the command on the marked lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009027 global_exe(eap->arg);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009028 ml_clearmarked(); // clear rest of the marks
Bram Moolenaar4facea32019-10-12 20:17:40 +02009029# ifdef FEAT_CLIPBOARD
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02009030 end_global_changes();
Bram Moolenaar4facea32019-10-12 20:17:40 +02009031# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009032}
9033#endif
Bram Moolenaarf5291f32017-09-14 22:55:37 +02009034
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009035#if defined(FEAT_QUICKFIX) || defined(PROTO)
Bram Moolenaar39665952018-08-15 20:59:48 +02009036/*
9037 * Returns TRUE if the supplied Ex cmdidx is for a location list command
9038 * instead of a quickfix command.
9039 */
9040 int
9041is_loclist_cmd(int cmdidx)
9042{
Bram Moolenaar74c8be22018-08-23 22:51:40 +02009043 if (cmdidx < 0 || cmdidx >= CMD_SIZE)
Bram Moolenaar39665952018-08-15 20:59:48 +02009044 return FALSE;
9045 return cmdnames[cmdidx].cmd_name[0] == 'l';
9046}
9047#endif
9048
Bram Moolenaar4facea32019-10-12 20:17:40 +02009049#if defined(FEAT_TIMERS) || defined(PROTO)
Bram Moolenaarf5291f32017-09-14 22:55:37 +02009050 int
9051get_pressedreturn(void)
9052{
9053 return ex_pressedreturn;
9054}
9055
9056 void
9057set_pressedreturn(int val)
9058{
9059 ex_pressedreturn = val;
9060}
9061#endif