blob: ed52b464bee7f2b8e970f633ba3ff212b4e47f14 [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
294# define ex_lockvar ex_ni
295# define ex_oldfiles ex_ni
296# define ex_options ex_ni
297# define ex_packadd ex_ni
298# define ex_packloadall ex_ni
299# define ex_return ex_ni
300# define ex_scriptnames ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000301# define ex_throw ex_ni
302# define ex_try ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000303# define ex_unlet ex_ni
Bram Moolenaar65c1b012005-01-31 19:02:28 +0000304# define ex_unlockvar ex_ni
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100305# define ex_vim9script ex_ni
Bram Moolenaare4ce8252019-08-04 15:30:16 +0200306# define ex_while ex_ni
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100307# define ex_import ex_ni
308# define ex_export ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000309#endif
Bram Moolenaar84538072019-07-28 14:15:42 +0200310#ifndef FEAT_SESSION
Bram Moolenaar071d4272004-06-13 20:20:40 +0000311# define ex_loadview ex_ni
312#endif
Bram Moolenaardefa0672019-07-21 19:25:37 +0200313#ifndef FEAT_VIMINFO
Bram Moolenaar071d4272004-06-13 20:20:40 +0000314# define ex_viminfo ex_ni
315#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100316static void ex_behave(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100317static void ex_filetype(exarg_T *eap);
318static void ex_setfiletype(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000319#ifndef FEAT_DIFF
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000320# define ex_diffoff ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000321# define ex_diffpatch ex_ni
322# define ex_diffgetput ex_ni
323# define ex_diffsplit ex_ni
324# define ex_diffthis ex_ni
325# define ex_diffupdate ex_ni
326#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100327static void ex_digraphs(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000328#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100329static void ex_nohlsearch(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000330#else
331# define ex_nohlsearch ex_ni
332# define ex_match ex_ni
333#endif
334#ifdef FEAT_CRYPT
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100335static void ex_X(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000336#else
337# define ex_X ex_ni
338#endif
339#ifdef FEAT_FOLDING
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100340static void ex_fold(exarg_T *eap);
341static void ex_foldopen(exarg_T *eap);
342static void ex_folddo(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000343#else
344# define ex_fold ex_ni
345# define ex_foldopen ex_ni
346# define ex_folddo ex_ni
347#endif
Bram Moolenaar13505972019-01-24 15:04:48 +0100348#if !(defined(HAVE_LOCALE_H) || defined(X_LOCALE))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000349# define ex_language ex_ni
350#endif
351#ifndef FEAT_SIGNS
352# define ex_sign ex_ni
353#endif
Bram Moolenaar009b2592004-10-24 19:18:58 +0000354#ifndef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200355# define ex_nbclose ex_ni
Bram Moolenaar009b2592004-10-24 19:18:58 +0000356# define ex_nbkey ex_ni
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200357# define ex_nbstart ex_ni
Bram Moolenaar009b2592004-10-24 19:18:58 +0000358#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000359
Bram Moolenaar071d4272004-06-13 20:20:40 +0000360#ifndef FEAT_JUMPLIST
361# define ex_jumps ex_ni
Bram Moolenaar2d358992016-06-12 21:20:54 +0200362# define ex_clearjumps ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000363# define ex_changes ex_ni
364#endif
365
Bram Moolenaar05159a02005-02-26 23:04:13 +0000366#ifndef FEAT_PROFILE
367# define ex_profile ex_ni
368#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200369#ifndef FEAT_TERMINAL
370# define ex_terminal ex_ni
371#endif
Bram Moolenaard4aa83a2019-05-09 18:59:31 +0200372#if !defined(FEAT_X11) || !defined(FEAT_XCLIPBOARD)
373# define ex_xrestore ex_ni
374#endif
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +0100375#if !defined(FEAT_PROP_POPUP)
Bram Moolenaar682725c2019-05-25 20:10:37 +0200376# define ex_popupclear ex_ni
377#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +0000378
Bram Moolenaar071d4272004-06-13 20:20:40 +0000379/*
380 * Declare cmdnames[].
381 */
382#define DO_DECLARE_EXCMD
383#include "ex_cmds.h"
Bram Moolenaar6de5e122017-04-20 21:55:44 +0200384#include "ex_cmdidxs.h"
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +0100385
Bram Moolenaar071d4272004-06-13 20:20:40 +0000386static char_u dollar_command[2] = {'$', 0};
387
388
389#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100390// Struct for storing a line inside a while/for loop
Bram Moolenaar071d4272004-06-13 20:20:40 +0000391typedef struct
392{
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100393 char_u *line; // command line
394 linenr_T lnum; // sourcing_lnum of the line
Bram Moolenaar071d4272004-06-13 20:20:40 +0000395} wcmd_T;
396
397/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000398 * Structure used to store info for line position in a while or for loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000399 * This is required, because do_one_cmd() may invoke ex_function(), which
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000400 * reads more lines that may come from the while/for loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000401 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000402struct loop_cookie
Bram Moolenaar071d4272004-06-13 20:20:40 +0000403{
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100404 garray_T *lines_gap; // growarray with line info
405 int current_line; // last read line from growarray
406 int repeating; // TRUE when looping a second time
407 // When "repeating" is FALSE use "getline" and "cookie" to get lines
Bram Moolenaar20b23c62020-08-20 15:25:00 +0200408 char_u *(*getline)(int, void *, int, getline_opt_T);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000409 void *cookie;
410};
411
Bram Moolenaar20b23c62020-08-20 15:25:00 +0200412static char_u *get_loop_line(int c, void *cookie, int indent, getline_opt_T options);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100413static int store_loop_line(garray_T *gap, char_u *line);
414static void free_cmdlines(garray_T *gap);
Bram Moolenaared203462004-06-16 11:19:22 +0000415
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100416// Struct to save a few things while debugging. Used in do_cmdline() only.
Bram Moolenaared203462004-06-16 11:19:22 +0000417struct dbg_stuff
418{
419 int trylevel;
420 int force_abort;
421 except_T *caught_stack;
422 char_u *vv_exception;
423 char_u *vv_throwpoint;
424 int did_emsg;
425 int got_int;
426 int did_throw;
427 int need_rethrow;
428 int check_cstack;
429 except_T *current_exception;
430};
431
Bram Moolenaared203462004-06-16 11:19:22 +0000432 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100433save_dbg_stuff(struct dbg_stuff *dsp)
Bram Moolenaared203462004-06-16 11:19:22 +0000434{
435 dsp->trylevel = trylevel; trylevel = 0;
436 dsp->force_abort = force_abort; force_abort = FALSE;
437 dsp->caught_stack = caught_stack; caught_stack = NULL;
438 dsp->vv_exception = v_exception(NULL);
439 dsp->vv_throwpoint = v_throwpoint(NULL);
440
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100441 // Necessary for debugging an inactive ":catch", ":finally", ":endtry"
Bram Moolenaared203462004-06-16 11:19:22 +0000442 dsp->did_emsg = did_emsg; did_emsg = FALSE;
443 dsp->got_int = got_int; got_int = FALSE;
444 dsp->did_throw = did_throw; did_throw = FALSE;
445 dsp->need_rethrow = need_rethrow; need_rethrow = FALSE;
446 dsp->check_cstack = check_cstack; check_cstack = FALSE;
447 dsp->current_exception = current_exception; current_exception = NULL;
448}
449
450 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100451restore_dbg_stuff(struct dbg_stuff *dsp)
Bram Moolenaared203462004-06-16 11:19:22 +0000452{
453 suppress_errthrow = FALSE;
454 trylevel = dsp->trylevel;
455 force_abort = dsp->force_abort;
456 caught_stack = dsp->caught_stack;
457 (void)v_exception(dsp->vv_exception);
458 (void)v_throwpoint(dsp->vv_throwpoint);
459 did_emsg = dsp->did_emsg;
460 got_int = dsp->got_int;
461 did_throw = dsp->did_throw;
462 need_rethrow = dsp->need_rethrow;
463 check_cstack = dsp->check_cstack;
464 current_exception = dsp->current_exception;
465}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000466#endif
467
Bram Moolenaar071d4272004-06-13 20:20:40 +0000468/*
469 * do_exmode(): Repeatedly get commands for the "Ex" mode, until the ":vi"
470 * command is given.
471 */
472 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100473do_exmode(
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100474 int improved) // TRUE for "improved Ex" mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000475{
476 int save_msg_scroll;
477 int prev_msg_row;
478 linenr_T prev_line;
Bram Moolenaar79518e22017-02-17 16:31:35 +0100479 varnumber_T changedtick;
Bram Moolenaardf177f62005-02-22 08:39:57 +0000480
481 if (improved)
482 exmode_active = EXMODE_VIM;
483 else
484 exmode_active = EXMODE_NORMAL;
485 State = NORMAL;
486
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100487 // When using ":global /pat/ visual" and then "Q" we return to continue
488 // the :global command.
Bram Moolenaardf177f62005-02-22 08:39:57 +0000489 if (global_busy)
490 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000491
492 save_msg_scroll = msg_scroll;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100493 ++RedrawingDisabled; // don't redisplay the window
494 ++no_wait_return; // don't wait for return
Bram Moolenaar071d4272004-06-13 20:20:40 +0000495#ifdef FEAT_GUI
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100496 // Ignore scrollbar and mouse events in Ex mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000497 ++hold_gui_events;
498#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000499
Bram Moolenaar32526b32019-01-19 17:43:09 +0100500 msg(_("Entering Ex mode. Type \"visual\" to go to Normal mode."));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000501 while (exmode_active)
502 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100503 // Check for a ":normal" command and no more characters left.
Bram Moolenaar7c626922005-02-07 22:01:03 +0000504 if (ex_normal_busy > 0 && typebuf.tb_len == 0)
505 {
506 exmode_active = FALSE;
507 break;
508 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000509 msg_scroll = TRUE;
510 need_wait_return = FALSE;
511 ex_pressedreturn = FALSE;
512 ex_no_reprint = FALSE;
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100513 changedtick = CHANGEDTICK(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000514 prev_msg_row = msg_row;
515 prev_line = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000516 if (improved)
517 {
518 cmdline_row = msg_row;
519 do_cmdline(NULL, getexline, NULL, 0);
520 }
521 else
522 do_cmdline(NULL, getexmodeline, NULL, DOCMD_NOWAIT);
523 lines_left = Rows - 1;
524
Bram Moolenaardf177f62005-02-22 08:39:57 +0000525 if ((prev_line != curwin->w_cursor.lnum
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100526 || changedtick != CHANGEDTICK(curbuf)) && !ex_no_reprint)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000527 {
Bram Moolenaardf177f62005-02-22 08:39:57 +0000528 if (curbuf->b_ml.ml_flags & ML_EMPTY)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100529 emsg(_(e_emptybuf));
Bram Moolenaardf177f62005-02-22 08:39:57 +0000530 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000531 {
Bram Moolenaardf177f62005-02-22 08:39:57 +0000532 if (ex_pressedreturn)
533 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100534 // go up one line, to overwrite the ":<CR>" line, so the
535 // output doesn't contain empty lines.
Bram Moolenaardf177f62005-02-22 08:39:57 +0000536 msg_row = prev_msg_row;
537 if (prev_msg_row == Rows - 1)
538 msg_row--;
539 }
540 msg_col = 0;
541 print_line_no_prefix(curwin->w_cursor.lnum, FALSE, FALSE);
542 msg_clr_eos();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000543 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000544 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100545 else if (ex_pressedreturn && !ex_no_reprint) // must be at EOF
Bram Moolenaardf177f62005-02-22 08:39:57 +0000546 {
547 if (curbuf->b_ml.ml_flags & ML_EMPTY)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100548 emsg(_(e_emptybuf));
Bram Moolenaardf177f62005-02-22 08:39:57 +0000549 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100550 emsg(_("E501: At end-of-file"));
Bram Moolenaardf177f62005-02-22 08:39:57 +0000551 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000552 }
553
554#ifdef FEAT_GUI
555 --hold_gui_events;
556#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000557 --RedrawingDisabled;
558 --no_wait_return;
559 update_screen(CLEAR);
560 need_wait_return = FALSE;
561 msg_scroll = save_msg_scroll;
562}
563
564/*
Bram Moolenaar4facea32019-10-12 20:17:40 +0200565 * Print the executed command for when 'verbose' is set.
566 * When "lnum" is 0 only print the command.
567 */
568 static void
569msg_verbose_cmd(linenr_T lnum, char_u *cmd)
570{
571 ++no_wait_return;
572 verbose_enter_scroll();
573
574 if (lnum == 0)
575 smsg(_("Executing: %s"), cmd);
576 else
577 smsg(_("line %ld: %s"), (long)lnum, cmd);
578 if (msg_silent == 0)
579 msg_puts("\n"); // don't overwrite this
580
581 verbose_leave_scroll();
582 --no_wait_return;
583}
584
585/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000586 * Execute a simple command line. Used for translated commands like "*".
587 */
588 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100589do_cmdline_cmd(char_u *cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000590{
591 return do_cmdline(cmd, NULL, NULL,
592 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
593}
594
595/*
596 * do_cmdline(): execute one Ex command line
597 *
598 * 1. Execute "cmdline" when it is not NULL.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100599 * If "cmdline" is NULL, or more lines are needed, fgetline() is used.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000600 * 2. Split up in parts separated with '|'.
601 *
602 * This function can be called recursively!
603 *
604 * flags:
605 * DOCMD_VERBOSE - The command will be included in the error message.
606 * DOCMD_NOWAIT - Don't call wait_return() and friends.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100607 * DOCMD_REPEAT - Repeat execution until fgetline() returns NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000608 * DOCMD_KEYTYPED - Don't reset KeyTyped.
609 * DOCMD_EXCRESET - Reset the exception environment (used for debugging).
610 * DOCMD_KEEPLINE - Store first typed line (for repeating with ".").
611 *
612 * return FAIL if cmdline could not be executed, OK otherwise
613 */
614 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100615do_cmdline(
616 char_u *cmdline,
Bram Moolenaar66250c92020-08-20 15:02:42 +0200617 char_u *(*fgetline)(int, void *, int, getline_opt_T),
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100618 void *cookie, // argument for fgetline()
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100619 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000620{
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100621 char_u *next_cmdline; // next cmd to execute
622 char_u *cmdline_copy = NULL; // copy of cmd line
623 int used_getline = FALSE; // used "fgetline" to obtain command
624 static int recursive = 0; // recursive depth
Bram Moolenaar071d4272004-06-13 20:20:40 +0000625 int msg_didout_before_start = 0;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100626 int count = 0; // line number count
627 int did_inc = FALSE; // incremented RedrawingDisabled
Bram Moolenaar071d4272004-06-13 20:20:40 +0000628 int retval = OK;
629#ifdef FEAT_EVAL
Bram Moolenaarddef1292019-12-16 17:10:33 +0100630 cstack_T cstack; // conditional stack
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100631 garray_T lines_ga; // keep lines for ":while"/":for"
632 int current_line = 0; // active line in lines_ga
Bram Moolenaard5053d02020-06-28 15:51:16 +0200633 int current_line_before = 0;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100634 char_u *fname = NULL; // function or script name
635 linenr_T *breakpoint = NULL; // ptr to breakpoint field in cookie
636 int *dbg_tick = NULL; // ptr to dbg_tick field in cookie
637 struct dbg_stuff debug_saved; // saved things for debug mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000638 int initial_trylevel;
Bram Moolenaar25e0f582020-05-25 22:36:50 +0200639 msglist_T **saved_msg_list = NULL;
640 msglist_T *private_msg_list;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000641
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100642 // "fgetline" and "cookie" passed to do_one_cmd()
Bram Moolenaar66250c92020-08-20 15:02:42 +0200643 char_u *(*cmd_getline)(int, void *, int, getline_opt_T);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000644 void *cmd_cookie;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000645 struct loop_cookie cmd_loop_cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000646 void *real_cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +0000647 int getline_is_func;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000648#else
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100649# define cmd_getline fgetline
Bram Moolenaar071d4272004-06-13 20:20:40 +0000650# define cmd_cookie cookie
651#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100652 static int call_depth = 0; // recursiveness
Bram Moolenaar2196bce2020-04-12 20:01:11 +0200653#ifdef FEAT_EVAL
Bram Moolenaare31ee862020-01-07 20:59:34 +0100654 ESTACK_CHECK_DECLARATION
Bram Moolenaar071d4272004-06-13 20:20:40 +0000655
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100656 // For every pair of do_cmdline()/do_one_cmd() calls, use an extra memory
657 // location for storing error messages to be converted to an exception.
658 // This ensures that the do_errthrow() call in do_one_cmd() does not
659 // combine the messages stored by an earlier invocation of do_one_cmd()
660 // with the command name of the later one. This would happen when
661 // BufWritePost autocommands are executed after a write error.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000662 saved_msg_list = msg_list;
663 msg_list = &private_msg_list;
664 private_msg_list = NULL;
665#endif
666
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100667 // It's possible to create an endless loop with ":execute", catch that
668 // here. The value of 200 allows nested function calls, ":source", etc.
669 // Allow 200 or 'maxfuncdepth', whatever is larger.
Bram Moolenaarb094ff42017-01-02 16:16:39 +0100670 if (call_depth >= 200
671#ifdef FEAT_EVAL
672 && call_depth >= p_mfd
673#endif
674 )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000675 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100676 emsg(_("E169: Command too recursive"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000677#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100678 // When converting to an exception, we do not include the command name
679 // since this is not an error of the specific command.
Bram Moolenaarddef1292019-12-16 17:10:33 +0100680 do_errthrow((cstack_T *)NULL, (char_u *)NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000681 msg_list = saved_msg_list;
682#endif
683 return FAIL;
684 }
685 ++call_depth;
686
687#ifdef FEAT_EVAL
688 cstack.cs_idx = -1;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000689 cstack.cs_looplevel = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000690 cstack.cs_trylevel = 0;
691 cstack.cs_emsg_silent_list = NULL;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000692 cstack.cs_lflags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000693 ga_init2(&lines_ga, (int)sizeof(wcmd_T), 10);
694
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100695 real_cookie = getline_cookie(fgetline, cookie);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000696
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100697 // Inside a function use a higher nesting level.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100698 getline_is_func = getline_equal(fgetline, cookie, get_func_line);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000699 if (getline_is_func && ex_nesting_level == func_level(real_cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000700 ++ex_nesting_level;
701
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100702 // Get the function or script name and the address where the next breakpoint
703 // line and the debug tick for a function or script are stored.
Bram Moolenaar05159a02005-02-26 23:04:13 +0000704 if (getline_is_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000705 {
706 fname = func_name(real_cookie);
707 breakpoint = func_breakpoint(real_cookie);
708 dbg_tick = func_dbg_tick(real_cookie);
709 }
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100710 else if (getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000711 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100712 fname = SOURCING_NAME;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000713 breakpoint = source_breakpoint(real_cookie);
714 dbg_tick = source_dbg_tick(real_cookie);
715 }
716
717 /*
718 * Initialize "force_abort" and "suppress_errthrow" at the top level.
719 */
720 if (!recursive)
721 {
722 force_abort = FALSE;
723 suppress_errthrow = FALSE;
724 }
725
726 /*
727 * If requested, store and reset the global values controlling the
Bram Moolenaar89d40322006-08-29 15:30:07 +0000728 * exception handling (used when debugging). Otherwise clear it to avoid
729 * a bogus compiler warning when the optimizer uses inline functions...
Bram Moolenaar071d4272004-06-13 20:20:40 +0000730 */
Bram Moolenaarb4872942006-05-13 10:32:52 +0000731 if (flags & DOCMD_EXCRESET)
Bram Moolenaared203462004-06-16 11:19:22 +0000732 save_dbg_stuff(&debug_saved);
Bram Moolenaar89d40322006-08-29 15:30:07 +0000733 else
Bram Moolenaara80faa82020-04-12 19:37:17 +0200734 CLEAR_FIELD(debug_saved);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000735
736 initial_trylevel = trylevel;
737
738 /*
739 * "did_throw" will be set to TRUE when an exception is being thrown.
740 */
741 did_throw = FALSE;
742#endif
743 /*
744 * "did_emsg" will be set to TRUE when emsg() is used, in which case we
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000745 * cancel the whole command line, and any if/endif or loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000746 * If force_abort is set, we cancel everything.
747 */
748 did_emsg = FALSE;
749
750 /*
751 * KeyTyped is only set when calling vgetc(). Reset it here when not
752 * calling vgetc() (sourced command lines).
753 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100754 if (!(flags & DOCMD_KEYTYPED)
755 && !getline_equal(fgetline, cookie, getexline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000756 KeyTyped = FALSE;
757
758 /*
759 * Continue executing command lines:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000760 * - when inside an ":if", ":while" or ":for"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000761 * - for multiple commands on one line, separated with '|'
762 * - when repeating until there are no more lines (for ":source")
763 */
764 next_cmdline = cmdline;
765 do
766 {
Bram Moolenaar05159a02005-02-26 23:04:13 +0000767#ifdef FEAT_EVAL
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100768 getline_is_func = getline_equal(fgetline, cookie, get_func_line);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000769#endif
770
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100771 // stop skipping cmds for an error msg after all endif/while/for
Bram Moolenaar071d4272004-06-13 20:20:40 +0000772 if (next_cmdline == NULL
773#ifdef FEAT_EVAL
774 && !force_abort
775 && cstack.cs_idx < 0
Bram Moolenaar05159a02005-02-26 23:04:13 +0000776 && !(getline_is_func && func_has_abort(real_cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000777#endif
778 )
779 did_emsg = FALSE;
780
781 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000782 * 1. If repeating a line in a loop, get a line from lines_ga.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100783 * 2. If no line given: Get an allocated line with fgetline().
Bram Moolenaar071d4272004-06-13 20:20:40 +0000784 * 3. If a line is given: Make a copy, so we can mess with it.
785 */
786
787#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100788 // 1. If repeating, get a previous line from lines_ga.
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000789 if (cstack.cs_looplevel > 0 && current_line < lines_ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000790 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100791 // Each '|' separated command is stored separately in lines_ga, to
792 // be able to jump to it. Don't use next_cmdline now.
Bram Moolenaard23a8232018-02-10 18:45:26 +0100793 VIM_CLEAR(cmdline_copy);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000794
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100795 // Check if a function has returned or, unless it has an unclosed
796 // try conditional, aborted.
Bram Moolenaar05159a02005-02-26 23:04:13 +0000797 if (getline_is_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000798 {
Bram Moolenaar05159a02005-02-26 23:04:13 +0000799# ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000800 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +0000801 func_line_end(real_cookie);
802# endif
803 if (func_has_ended(real_cookie))
804 {
805 retval = FAIL;
806 break;
807 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000808 }
Bram Moolenaar05159a02005-02-26 23:04:13 +0000809#ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000810 else if (do_profiling == PROF_YES
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100811 && getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar05159a02005-02-26 23:04:13 +0000812 script_line_end();
813#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000814
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100815 // Check if a sourced file hit a ":finish" command.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100816 if (source_finished(fgetline, cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000817 {
818 retval = FAIL;
819 break;
820 }
821
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100822 // If breakpoints have been added/deleted need to check for it.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000823 if (breakpoint != NULL && dbg_tick != NULL
824 && *dbg_tick != debug_tick)
825 {
826 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100827 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100828 fname, SOURCING_LNUM);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000829 *dbg_tick = debug_tick;
830 }
831
832 next_cmdline = ((wcmd_T *)(lines_ga.ga_data))[current_line].line;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100833 SOURCING_LNUM = ((wcmd_T *)(lines_ga.ga_data))[current_line].lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000834
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100835 // Did we encounter a breakpoint?
Bram Moolenaar071d4272004-06-13 20:20:40 +0000836 if (breakpoint != NULL && *breakpoint != 0
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100837 && *breakpoint <= SOURCING_LNUM)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000838 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100839 dbg_breakpoint(fname, SOURCING_LNUM);
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100840 // Find next breakpoint.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000841 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100842 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100843 fname, SOURCING_LNUM);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000844 *dbg_tick = debug_tick;
845 }
Bram Moolenaar05159a02005-02-26 23:04:13 +0000846# ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000847 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +0000848 {
849 if (getline_is_func)
850 func_line_start(real_cookie);
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100851 else if (getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar05159a02005-02-26 23:04:13 +0000852 script_line_start();
853 }
854# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000855 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000856#endif
857
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100858 // 2. If no line given, get an allocated line with fgetline().
Bram Moolenaar071d4272004-06-13 20:20:40 +0000859 if (next_cmdline == NULL)
860 {
861 /*
862 * Need to set msg_didout for the first line after an ":if",
863 * otherwise the ":if" will be overwritten.
864 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100865 if (count == 1 && getline_equal(fgetline, cookie, getexline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000866 msg_didout = TRUE;
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100867 if (fgetline == NULL || (next_cmdline = fgetline(':', cookie,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000868#ifdef FEAT_EVAL
869 cstack.cs_idx < 0 ? 0 : (cstack.cs_idx + 1) * 2
870#else
871 0
872#endif
Bram Moolenaare96a2492019-06-25 04:12:16 +0200873 , TRUE)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000874 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100875 // Don't call wait_return for aborted command line. The NULL
876 // returned for the end of a sourced file or executed function
877 // doesn't do this.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000878 if (KeyTyped && !(flags & DOCMD_REPEAT))
879 need_wait_return = FALSE;
880 retval = FAIL;
881 break;
882 }
883 used_getline = TRUE;
884
885 /*
886 * Keep the first typed line. Clear it when more lines are typed.
887 */
888 if (flags & DOCMD_KEEPLINE)
889 {
890 vim_free(repeat_cmdline);
891 if (count == 0)
892 repeat_cmdline = vim_strsave(next_cmdline);
893 else
894 repeat_cmdline = NULL;
895 }
896 }
897
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100898 // 3. Make a copy of the command so we can mess with it.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000899 else if (cmdline_copy == NULL)
900 {
901 next_cmdline = vim_strsave(next_cmdline);
902 if (next_cmdline == NULL)
903 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100904 emsg(_(e_outofmem));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000905 retval = FAIL;
906 break;
907 }
908 }
909 cmdline_copy = next_cmdline;
910
911#ifdef FEAT_EVAL
912 /*
Bram Moolenaard5053d02020-06-28 15:51:16 +0200913 * Inside a while/for loop, and when the command looks like a ":while"
914 * or ":for", the line is stored, because we may need it later when
915 * looping.
916 *
917 * When there is a '|' and another command, it is stored separately,
918 * because we need to be able to jump back to it from an
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000919 * :endwhile/:endfor.
Bram Moolenaard5053d02020-06-28 15:51:16 +0200920 *
921 * Pass a different "fgetline" function to do_one_cmd() below,
922 * that it stores lines in or reads them from "lines_ga". Makes it
923 * possible to define a function inside a while/for loop and handles
924 * line continuation.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000925 */
Bram Moolenaard5053d02020-06-28 15:51:16 +0200926 if ((cstack.cs_looplevel > 0 || has_loop_cmd(next_cmdline)))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000927 {
Bram Moolenaard5053d02020-06-28 15:51:16 +0200928 cmd_getline = get_loop_line;
929 cmd_cookie = (void *)&cmd_loop_cookie;
930 cmd_loop_cookie.lines_gap = &lines_ga;
931 cmd_loop_cookie.current_line = current_line;
932 cmd_loop_cookie.getline = fgetline;
933 cmd_loop_cookie.cookie = cookie;
934 cmd_loop_cookie.repeating = (current_line < lines_ga.ga_len);
935
936 // Save the current line when encountering it the first time.
937 if (current_line == lines_ga.ga_len
938 && store_loop_line(&lines_ga, next_cmdline) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000939 {
940 retval = FAIL;
941 break;
942 }
Bram Moolenaard5053d02020-06-28 15:51:16 +0200943 current_line_before = current_line;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000944 }
Bram Moolenaard5053d02020-06-28 15:51:16 +0200945 else
946 {
947 cmd_getline = fgetline;
948 cmd_cookie = cookie;
949 }
950
Bram Moolenaar071d4272004-06-13 20:20:40 +0000951 did_endif = FALSE;
952#endif
953
954 if (count++ == 0)
955 {
956 /*
957 * All output from the commands is put below each other, without
958 * waiting for a return. Don't do this when executing commands
959 * from a script or when being called recursive (e.g. for ":e
960 * +command file").
961 */
962 if (!(flags & DOCMD_NOWAIT) && !recursive)
963 {
964 msg_didout_before_start = msg_didout;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100965 msg_didany = FALSE; // no output yet
Bram Moolenaar071d4272004-06-13 20:20:40 +0000966 msg_start();
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100967 msg_scroll = TRUE; // put messages below each other
968 ++no_wait_return; // don't wait for return until finished
Bram Moolenaar071d4272004-06-13 20:20:40 +0000969 ++RedrawingDisabled;
970 did_inc = TRUE;
971 }
972 }
973
Bram Moolenaar823654b2020-05-29 23:03:09 +0200974 if ((p_verbose >= 15 && SOURCING_NAME != NULL) || p_verbose >= 16)
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100975 msg_verbose_cmd(SOURCING_LNUM, cmdline_copy);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000976
977 /*
978 * 2. Execute one '|' separated command.
979 * do_one_cmd() will return NULL if there is no trailing '|'.
980 * "cmdline_copy" can change, e.g. for '%' and '#' expansion.
981 */
982 ++recursive;
983 next_cmdline = do_one_cmd(&cmdline_copy, flags & DOCMD_VERBOSE,
984#ifdef FEAT_EVAL
985 &cstack,
986#endif
987 cmd_getline, cmd_cookie);
988 --recursive;
989
990#ifdef FEAT_EVAL
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000991 if (cmd_cookie == (void *)&cmd_loop_cookie)
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100992 // Use "current_line" from "cmd_loop_cookie", it may have been
993 // incremented when defining a function.
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000994 current_line = cmd_loop_cookie.current_line;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000995#endif
996
997 if (next_cmdline == NULL)
998 {
Bram Moolenaard23a8232018-02-10 18:45:26 +0100999 VIM_CLEAR(cmdline_copy);
Bram Moolenaard7663c22019-08-06 21:59:57 +02001000
Bram Moolenaar071d4272004-06-13 20:20:40 +00001001 /*
1002 * If the command was typed, remember it for the ':' register.
1003 * Do this AFTER executing the command to make :@: work.
1004 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001005 if (getline_equal(fgetline, cookie, getexline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001006 && new_last_cmdline != NULL)
1007 {
1008 vim_free(last_cmdline);
1009 last_cmdline = new_last_cmdline;
1010 new_last_cmdline = NULL;
1011 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001012 }
1013 else
1014 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001015 // need to copy the command after the '|' to cmdline_copy, for the
1016 // next do_one_cmd()
Bram Moolenaara7241f52008-06-24 20:39:31 +00001017 STRMOVE(cmdline_copy, next_cmdline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001018 next_cmdline = cmdline_copy;
1019 }
1020
1021
1022#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001023 // reset did_emsg for a function that is not aborted by an error
Bram Moolenaar071d4272004-06-13 20:20:40 +00001024 if (did_emsg && !force_abort
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001025 && getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001026 && !func_has_abort(real_cookie))
1027 did_emsg = FALSE;
1028
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001029 if (cstack.cs_looplevel > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001030 {
1031 ++current_line;
1032
1033 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001034 * An ":endwhile", ":endfor" and ":continue" is handled here.
1035 * If we were executing commands, jump back to the ":while" or
1036 * ":for".
1037 * If we were not executing commands, decrement cs_looplevel.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001038 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001039 if (cstack.cs_lflags & (CSL_HAD_CONT | CSL_HAD_ENDLOOP))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001040 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001041 cstack.cs_lflags &= ~(CSL_HAD_CONT | CSL_HAD_ENDLOOP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001042
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001043 // Jump back to the matching ":while" or ":for". Be careful
1044 // not to use a cs_line[] from an entry that isn't a ":while"
1045 // or ":for": It would make "current_line" invalid and can
1046 // cause a crash.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001047 if (!did_emsg && !got_int && !did_throw
1048 && cstack.cs_idx >= 0
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001049 && (cstack.cs_flags[cstack.cs_idx]
1050 & (CSF_WHILE | CSF_FOR))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001051 && cstack.cs_line[cstack.cs_idx] >= 0
1052 && (cstack.cs_flags[cstack.cs_idx] & CSF_ACTIVE))
1053 {
1054 current_line = cstack.cs_line[cstack.cs_idx];
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001055 // remember we jumped there
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001056 cstack.cs_lflags |= CSL_HAD_LOOP;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001057 line_breakcheck(); // check if CTRL-C typed
Bram Moolenaar071d4272004-06-13 20:20:40 +00001058
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001059 // Check for the next breakpoint at or after the ":while"
1060 // or ":for".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001061 if (breakpoint != NULL)
1062 {
1063 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001064 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001065 fname,
1066 ((wcmd_T *)lines_ga.ga_data)[current_line].lnum-1);
1067 *dbg_tick = debug_tick;
1068 }
1069 }
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001070 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001071 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001072 // can only get here with ":endwhile" or ":endfor"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001073 if (cstack.cs_idx >= 0)
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001074 rewind_conditionals(&cstack, cstack.cs_idx - 1,
1075 CSF_WHILE | CSF_FOR, &cstack.cs_looplevel);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001076 }
1077 }
1078
1079 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001080 * For a ":while" or ":for" we need to remember the line number.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001081 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001082 else if (cstack.cs_lflags & CSL_HAD_LOOP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001083 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001084 cstack.cs_lflags &= ~CSL_HAD_LOOP;
Bram Moolenaard5053d02020-06-28 15:51:16 +02001085 cstack.cs_line[cstack.cs_idx] = current_line_before;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001086 }
1087 }
1088
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001089 // Check for the next breakpoint after a watchexpression
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01001090 if (breakpoint != NULL && has_watchexpr())
1091 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001092 *breakpoint = dbg_find_breakpoint(FALSE, fname, SOURCING_LNUM);
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01001093 *dbg_tick = debug_tick;
1094 }
1095
Bram Moolenaar071d4272004-06-13 20:20:40 +00001096 /*
1097 * When not inside any ":while" loop, clear remembered lines.
1098 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001099 if (cstack.cs_looplevel == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001100 {
1101 if (lines_ga.ga_len > 0)
1102 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001103 SOURCING_LNUM =
Bram Moolenaar071d4272004-06-13 20:20:40 +00001104 ((wcmd_T *)lines_ga.ga_data)[lines_ga.ga_len - 1].lnum;
1105 free_cmdlines(&lines_ga);
1106 }
1107 current_line = 0;
1108 }
1109
1110 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001111 * A ":finally" makes did_emsg, got_int, and did_throw pending for
1112 * being restored at the ":endtry". Reset them here and set the
1113 * ACTIVE and FINALLY flags, so that the finally clause gets executed.
1114 * This includes the case where a missing ":endif", ":endwhile" or
1115 * ":endfor" was detected by the ":finally" itself.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001116 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001117 if (cstack.cs_lflags & CSL_HAD_FINA)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001118 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001119 cstack.cs_lflags &= ~CSL_HAD_FINA;
1120 report_make_pending(cstack.cs_pending[cstack.cs_idx]
1121 & (CSTP_ERROR | CSTP_INTERRUPT | CSTP_THROW),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001122 did_throw ? (void *)current_exception : NULL);
1123 did_emsg = got_int = did_throw = FALSE;
1124 cstack.cs_flags[cstack.cs_idx] |= CSF_ACTIVE | CSF_FINALLY;
1125 }
1126
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001127 // Update global "trylevel" for recursive calls to do_cmdline() from
1128 // within this loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001129 trylevel = initial_trylevel + cstack.cs_trylevel;
1130
1131 /*
Bram Moolenaarc1762cc2007-05-10 16:56:30 +00001132 * If the outermost try conditional (across function calls and sourced
Bram Moolenaar071d4272004-06-13 20:20:40 +00001133 * files) is aborted because of an error, an interrupt, or an uncaught
1134 * exception, cancel everything. If it is left normally, reset
1135 * force_abort to get the non-EH compatible abortion behavior for
1136 * the rest of the script.
1137 */
1138 if (trylevel == 0 && !did_emsg && !got_int && !did_throw)
1139 force_abort = FALSE;
1140
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001141 // Convert an interrupt to an exception if appropriate.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001142 (void)do_intthrow(&cstack);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001143#endif // FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001144
1145 }
1146 /*
1147 * Continue executing command lines when:
1148 * - no CTRL-C typed, no aborting error, no exception thrown or try
1149 * conditionals need to be checked for executing finally clauses or
1150 * catching an interrupt exception
1151 * - didn't get an error message or lines are not typed
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001152 * - there is a command after '|', inside a :if, :while, :for or :try, or
Bram Moolenaar071d4272004-06-13 20:20:40 +00001153 * looping for ":source" command or function call.
1154 */
1155 while (!((got_int
1156#ifdef FEAT_EVAL
1157 || (did_emsg && force_abort) || did_throw
1158#endif
1159 )
1160#ifdef FEAT_EVAL
1161 && cstack.cs_trylevel == 0
1162#endif
1163 )
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001164 && !(did_emsg
1165#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001166 // Keep going when inside try/catch, so that the error can be
1167 // deal with, except when it is a syntax error, it may cause
1168 // the :endtry to be missed.
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001169 && (cstack.cs_trylevel == 0 || did_emsg_syntax)
1170#endif
1171 && used_getline
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001172 && (getline_equal(fgetline, cookie, getexmodeline)
1173 || getline_equal(fgetline, cookie, getexline)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001174 && (next_cmdline != NULL
1175#ifdef FEAT_EVAL
1176 || cstack.cs_idx >= 0
1177#endif
1178 || (flags & DOCMD_REPEAT)));
1179
1180 vim_free(cmdline_copy);
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001181 did_emsg_syntax = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001182#ifdef FEAT_EVAL
1183 free_cmdlines(&lines_ga);
1184 ga_clear(&lines_ga);
1185
1186 if (cstack.cs_idx >= 0)
1187 {
1188 /*
1189 * If a sourced file or executed function ran to its end, report the
1190 * unclosed conditional.
1191 */
1192 if (!got_int && !did_throw
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001193 && ((getline_equal(fgetline, cookie, getsourceline)
1194 && !source_finished(fgetline, cookie))
1195 || (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001196 && !func_has_ended(real_cookie))))
1197 {
1198 if (cstack.cs_flags[cstack.cs_idx] & CSF_TRY)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001199 emsg(_(e_endtry));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001200 else if (cstack.cs_flags[cstack.cs_idx] & CSF_WHILE)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001201 emsg(_(e_endwhile));
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001202 else if (cstack.cs_flags[cstack.cs_idx] & CSF_FOR)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001203 emsg(_(e_endfor));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001204 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001205 emsg(_(e_endif));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001206 }
1207
1208 /*
1209 * Reset "trylevel" in case of a ":finish" or ":return" or a missing
1210 * ":endtry" in a sourced file or executed function. If the try
1211 * conditional is in its finally clause, ignore anything pending.
1212 * If it is in a catch clause, finish the caught exception.
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001213 * Also cleanup any "cs_forinfo" structures.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001214 */
1215 do
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001216 {
1217 int idx = cleanup_conditionals(&cstack, 0, TRUE);
1218
Bram Moolenaar89e5d682005-01-17 22:06:23 +00001219 if (idx >= 0)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001220 --idx; // remove try block not in its finally clause
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001221 rewind_conditionals(&cstack, idx, CSF_WHILE | CSF_FOR,
1222 &cstack.cs_looplevel);
1223 }
1224 while (cstack.cs_idx >= 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001225 trylevel = initial_trylevel;
1226 }
1227
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001228 // If a missing ":endtry", ":endwhile", ":endfor", or ":endif" or a memory
1229 // lack was reported above and the error message is to be converted to an
1230 // exception, do this now after rewinding the cstack.
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001231 do_errthrow(&cstack, getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001232 ? (char_u *)"endfunction" : (char_u *)NULL);
1233
1234 if (trylevel == 0)
1235 {
1236 /*
1237 * When an exception is being thrown out of the outermost try
1238 * conditional, discard the uncaught exception, disable the conversion
1239 * of interrupts or errors to exceptions, and ensure that no more
1240 * commands are executed.
1241 */
1242 if (did_throw)
1243 {
1244 void *p = NULL;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02001245 msglist_T *messages = NULL, *next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001246
1247 /*
1248 * If the uncaught exception is a user exception, report it as an
1249 * error. If it is an error exception, display the saved error
1250 * message now. For an interrupt exception, do nothing; the
1251 * interrupt message is given elsewhere.
1252 */
1253 switch (current_exception->type)
1254 {
1255 case ET_USER:
Bram Moolenaar9c13b352005-05-19 20:53:52 +00001256 vim_snprintf((char *)IObuff, IOSIZE,
1257 _("E605: Exception not caught: %s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001258 current_exception->value);
1259 p = vim_strsave(IObuff);
1260 break;
1261 case ET_ERROR:
1262 messages = current_exception->messages;
1263 current_exception->messages = NULL;
1264 break;
1265 case ET_INTERRUPT:
1266 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001267 }
1268
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001269 estack_push(ETYPE_EXCEPT, current_exception->throw_name,
1270 current_exception->throw_lnum);
Bram Moolenaare31ee862020-01-07 20:59:34 +01001271 ESTACK_CHECK_SETUP
Bram Moolenaar071d4272004-06-13 20:20:40 +00001272 current_exception->throw_name = NULL;
1273
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001274 discard_current_exception(); // uses IObuff if 'verbose'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001275 suppress_errthrow = TRUE;
1276 force_abort = TRUE;
1277
1278 if (messages != NULL)
1279 {
1280 do
1281 {
1282 next = messages->next;
1283 emsg(messages->msg);
1284 vim_free(messages->msg);
Bram Moolenaar5fbf3bc2020-06-01 21:13:11 +02001285 vim_free(messages->sfile);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001286 vim_free(messages);
1287 messages = next;
1288 }
1289 while (messages != NULL);
1290 }
1291 else if (p != NULL)
1292 {
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01001293 emsg(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001294 vim_free(p);
1295 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001296 vim_free(SOURCING_NAME);
Bram Moolenaare31ee862020-01-07 20:59:34 +01001297 ESTACK_CHECK_NOW
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001298 estack_pop();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001299 }
1300
1301 /*
1302 * On an interrupt or an aborting error not converted to an exception,
1303 * disable the conversion of errors to exceptions. (Interrupts are not
Bram Moolenaar2196bce2020-04-12 20:01:11 +02001304 * converted anymore, here.) This enables also the interrupt message
Bram Moolenaar071d4272004-06-13 20:20:40 +00001305 * when force_abort is set and did_emsg unset in case of an interrupt
1306 * from a finally clause after an error.
1307 */
1308 else if (got_int || (did_emsg && force_abort))
1309 suppress_errthrow = TRUE;
1310 }
1311
1312 /*
1313 * The current cstack will be freed when do_cmdline() returns. An uncaught
1314 * exception will have to be rethrown in the previous cstack. If a function
1315 * has just returned or a script file was just finished and the previous
1316 * cstack belongs to the same function or, respectively, script file, it
1317 * will have to be checked for finally clauses to be executed due to the
1318 * ":return" or ":finish". This is done in do_one_cmd().
1319 */
1320 if (did_throw)
1321 need_rethrow = TRUE;
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001322 if ((getline_equal(fgetline, cookie, getsourceline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001323 && ex_nesting_level > source_level(real_cookie))
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001324 || (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001325 && ex_nesting_level > func_level(real_cookie) + 1))
1326 {
1327 if (!did_throw)
1328 check_cstack = TRUE;
1329 }
1330 else
1331 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001332 // When leaving a function, reduce nesting level.
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001333 if (getline_equal(fgetline, cookie, get_func_line))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001334 --ex_nesting_level;
1335 /*
1336 * Go to debug mode when returning from a function in which we are
1337 * single-stepping.
1338 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001339 if ((getline_equal(fgetline, cookie, getsourceline)
1340 || getline_equal(fgetline, cookie, get_func_line))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001341 && ex_nesting_level + 1 <= debug_break_level)
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001342 do_debug(getline_equal(fgetline, cookie, getsourceline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001343 ? (char_u *)_("End of sourced file")
1344 : (char_u *)_("End of function"));
1345 }
1346
1347 /*
1348 * Restore the exception environment (done after returning from the
1349 * debugger).
1350 */
1351 if (flags & DOCMD_EXCRESET)
Bram Moolenaared203462004-06-16 11:19:22 +00001352 restore_dbg_stuff(&debug_saved);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001353
1354 msg_list = saved_msg_list;
Bram Moolenaar292b90d2020-03-18 15:23:16 +01001355
1356 // Cleanup if "cs_emsg_silent_list" remains.
1357 if (cstack.cs_emsg_silent_list != NULL)
1358 {
1359 eslist_T *elem, *temp;
1360
1361 for (elem = cstack.cs_emsg_silent_list; elem != NULL; elem = temp)
1362 {
1363 temp = elem->next;
1364 vim_free(elem);
1365 }
1366 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001367#endif // FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001368
1369 /*
1370 * If there was too much output to fit on the command line, ask the user to
1371 * hit return before redrawing the screen. With the ":global" command we do
1372 * this only once after the command is finished.
1373 */
1374 if (did_inc)
1375 {
1376 --RedrawingDisabled;
1377 --no_wait_return;
1378 msg_scroll = FALSE;
1379
1380 /*
1381 * When just finished an ":if"-":else" which was typed, no need to
1382 * wait for hit-return. Also for an error situation.
1383 */
1384 if (retval == FAIL
1385#ifdef FEAT_EVAL
1386 || (did_endif && KeyTyped && !did_emsg)
1387#endif
1388 )
1389 {
1390 need_wait_return = FALSE;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001391 msg_didany = FALSE; // don't wait when restarting edit
Bram Moolenaar071d4272004-06-13 20:20:40 +00001392 }
1393 else if (need_wait_return)
1394 {
1395 /*
1396 * The msg_start() above clears msg_didout. The wait_return we do
1397 * here should not overwrite the command that may be shown before
1398 * doing that.
1399 */
1400 msg_didout |= msg_didout_before_start;
1401 wait_return(FALSE);
1402 }
1403 }
1404
Bram Moolenaar2a942252012-11-28 23:03:07 +01001405#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001406 did_endif = FALSE; // in case do_cmdline used recursively
Bram Moolenaar2a942252012-11-28 23:03:07 +01001407#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001408 /*
1409 * Reset if_level, in case a sourced script file contains more ":if" than
1410 * ":endif" (could be ":if x | foo | endif").
1411 */
1412 if_level = 0;
Bram Moolenaar2e18a122012-11-28 19:10:54 +01001413#endif
Bram Moolenaard4ad0d42012-11-28 17:34:48 +01001414
Bram Moolenaar071d4272004-06-13 20:20:40 +00001415 --call_depth;
1416 return retval;
1417}
1418
1419#ifdef FEAT_EVAL
1420/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001421 * Obtain a line when inside a ":while" or ":for" loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001422 */
1423 static char_u *
Bram Moolenaar20b23c62020-08-20 15:25:00 +02001424get_loop_line(int c, void *cookie, int indent, getline_opt_T options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001425{
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001426 struct loop_cookie *cp = (struct loop_cookie *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001427 wcmd_T *wp;
1428 char_u *line;
1429
1430 if (cp->current_line + 1 >= cp->lines_gap->ga_len)
1431 {
1432 if (cp->repeating)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001433 return NULL; // trying to read past ":endwhile"/":endfor"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001434
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001435 // First time inside the ":while"/":for": get line normally.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001436 if (cp->getline == NULL)
Bram Moolenaar20b23c62020-08-20 15:25:00 +02001437 line = getcmdline(c, 0L, indent, options);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001438 else
Bram Moolenaar20b23c62020-08-20 15:25:00 +02001439 line = cp->getline(c, cp->cookie, indent, options);
Bram Moolenaard68071d2006-05-02 22:08:30 +00001440 if (line != NULL && store_loop_line(cp->lines_gap, line) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001441 ++cp->current_line;
1442
1443 return line;
1444 }
1445
1446 KeyTyped = FALSE;
1447 ++cp->current_line;
1448 wp = (wcmd_T *)(cp->lines_gap->ga_data) + cp->current_line;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001449 SOURCING_LNUM = wp->lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001450 return vim_strsave(wp->line);
1451}
1452
1453/*
1454 * Store a line in "gap" so that a ":while" loop can execute it again.
1455 */
1456 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001457store_loop_line(garray_T *gap, char_u *line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001458{
1459 if (ga_grow(gap, 1) == FAIL)
1460 return FAIL;
1461 ((wcmd_T *)(gap->ga_data))[gap->ga_len].line = vim_strsave(line);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001462 ((wcmd_T *)(gap->ga_data))[gap->ga_len].lnum = SOURCING_LNUM;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001463 ++gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001464 return OK;
1465}
1466
1467/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001468 * Free the lines stored for a ":while" or ":for" loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001469 */
1470 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001471free_cmdlines(garray_T *gap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001472{
1473 while (gap->ga_len > 0)
1474 {
1475 vim_free(((wcmd_T *)(gap->ga_data))[gap->ga_len - 1].line);
1476 --gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001477 }
1478}
1479#endif
1480
1481/*
Bram Moolenaar89d40322006-08-29 15:30:07 +00001482 * If "fgetline" is get_loop_line(), return TRUE if the getline it uses equals
1483 * "func". * Otherwise return TRUE when "fgetline" equals "func".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001484 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001485 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001486getline_equal(
Bram Moolenaar66250c92020-08-20 15:02:42 +02001487 char_u *(*fgetline)(int, void *, int, getline_opt_T),
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001488 void *cookie UNUSED, // argument for fgetline()
Bram Moolenaar66250c92020-08-20 15:02:42 +02001489 char_u *(*func)(int, void *, int, getline_opt_T))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001490{
1491#ifdef FEAT_EVAL
Bram Moolenaar20b23c62020-08-20 15:25:00 +02001492 char_u *(*gp)(int, void *, int, getline_opt_T);
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001493 struct loop_cookie *cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001494
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001495 // When "fgetline" is "get_loop_line()" use the "cookie" to find the
1496 // function that's originally used to obtain the lines. This may be
1497 // nested several levels.
Bram Moolenaar89d40322006-08-29 15:30:07 +00001498 gp = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001499 cp = (struct loop_cookie *)cookie;
1500 while (gp == get_loop_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001501 {
1502 gp = cp->getline;
1503 cp = cp->cookie;
1504 }
1505 return gp == func;
1506#else
Bram Moolenaar89d40322006-08-29 15:30:07 +00001507 return fgetline == func;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001508#endif
1509}
1510
Bram Moolenaar071d4272004-06-13 20:20:40 +00001511/*
Bram Moolenaar89d40322006-08-29 15:30:07 +00001512 * If "fgetline" is get_loop_line(), return the cookie used by the original
Bram Moolenaar071d4272004-06-13 20:20:40 +00001513 * getline function. Otherwise return "cookie".
1514 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001515 void *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001516getline_cookie(
Bram Moolenaar66250c92020-08-20 15:02:42 +02001517 char_u *(*fgetline)(int, void *, int, getline_opt_T) UNUSED,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001518 void *cookie) // argument for fgetline()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001519{
Bram Moolenaar13505972019-01-24 15:04:48 +01001520#ifdef FEAT_EVAL
Bram Moolenaar20b23c62020-08-20 15:25:00 +02001521 char_u *(*gp)(int, void *, int, getline_opt_T);
Bram Moolenaard5053d02020-06-28 15:51:16 +02001522 struct loop_cookie *cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001523
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001524 // When "fgetline" is "get_loop_line()" use the "cookie" to find the
1525 // cookie that's originally used to obtain the lines. This may be nested
1526 // several levels.
Bram Moolenaar89d40322006-08-29 15:30:07 +00001527 gp = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001528 cp = (struct loop_cookie *)cookie;
1529 while (gp == get_loop_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001530 {
1531 gp = cp->getline;
1532 cp = cp->cookie;
1533 }
1534 return cp;
Bram Moolenaar13505972019-01-24 15:04:48 +01001535#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001536 return cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001537#endif
Bram Moolenaar13505972019-01-24 15:04:48 +01001538}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001539
Bram Moolenaard5053d02020-06-28 15:51:16 +02001540#if defined(FEAT_EVAL) || defined(PROT)
1541/*
1542 * Get the next line source line without advancing.
1543 */
1544 char_u *
1545getline_peek(
Bram Moolenaar66250c92020-08-20 15:02:42 +02001546 char_u *(*fgetline)(int, void *, int, getline_opt_T) UNUSED,
Bram Moolenaard5053d02020-06-28 15:51:16 +02001547 void *cookie) // argument for fgetline()
1548{
Bram Moolenaar20b23c62020-08-20 15:25:00 +02001549 char_u *(*gp)(int, void *, int, getline_opt_T);
Bram Moolenaard5053d02020-06-28 15:51:16 +02001550 struct loop_cookie *cp;
1551 wcmd_T *wp;
1552
1553 // When "fgetline" is "get_loop_line()" use the "cookie" to find the
1554 // cookie that's originally used to obtain the lines. This may be nested
1555 // several levels.
1556 gp = fgetline;
1557 cp = (struct loop_cookie *)cookie;
1558 while (gp == get_loop_line)
1559 {
1560 if (cp->current_line + 1 < cp->lines_gap->ga_len)
1561 {
1562 // executing lines a second time, use the stored copy
1563 wp = (wcmd_T *)(cp->lines_gap->ga_data) + cp->current_line + 1;
1564 return wp->line;
1565 }
1566 gp = cp->getline;
1567 cp = cp->cookie;
1568 }
1569 if (gp == getsourceline)
1570 return source_nextline(cp);
1571 return NULL;
1572}
1573#endif
1574
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001575
1576/*
1577 * Helper function to apply an offset for buffer commands, i.e. ":bdelete",
1578 * ":bwipeout", etc.
1579 * Returns the buffer number.
1580 */
1581 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001582compute_buffer_local_count(int addr_type, int lnum, int offset)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001583{
1584 buf_T *buf;
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001585 buf_T *nextbuf;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001586 int count = offset;
1587
1588 buf = firstbuf;
1589 while (buf->b_next != NULL && buf->b_fnum < lnum)
1590 buf = buf->b_next;
1591 while (count != 0)
1592 {
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001593 count += (offset < 0) ? 1 : -1;
1594 nextbuf = (offset < 0) ? buf->b_prev : buf->b_next;
1595 if (nextbuf == NULL)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001596 break;
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001597 buf = nextbuf;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001598 if (addr_type == ADDR_LOADED_BUFFERS)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001599 // skip over unloaded buffers
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001600 while (buf->b_ml.ml_mfp == NULL)
1601 {
1602 nextbuf = (offset < 0) ? buf->b_prev : buf->b_next;
1603 if (nextbuf == NULL)
1604 break;
1605 buf = nextbuf;
1606 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001607 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001608 // we might have gone too far, last buffer is not loadedd
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001609 if (addr_type == ADDR_LOADED_BUFFERS)
1610 while (buf->b_ml.ml_mfp == NULL)
1611 {
1612 nextbuf = (offset >= 0) ? buf->b_prev : buf->b_next;
1613 if (nextbuf == NULL)
1614 break;
1615 buf = nextbuf;
1616 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001617 return buf->b_fnum;
1618}
1619
Bram Moolenaarb7316892019-05-01 18:08:42 +02001620/*
1621 * Return the window number of "win".
1622 * When "win" is NULL return the number of windows.
1623 */
Bram Moolenaarf240e182014-11-27 18:33:02 +01001624 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001625current_win_nr(win_T *win)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001626{
1627 win_T *wp;
1628 int nr = 0;
1629
Bram Moolenaar29323592016-07-24 22:04:11 +02001630 FOR_ALL_WINDOWS(wp)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001631 {
1632 ++nr;
1633 if (wp == win)
1634 break;
1635 }
1636 return nr;
1637}
1638
1639 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001640current_tab_nr(tabpage_T *tab)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001641{
1642 tabpage_T *tp;
1643 int nr = 0;
1644
Bram Moolenaar29323592016-07-24 22:04:11 +02001645 FOR_ALL_TABPAGES(tp)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001646 {
1647 ++nr;
1648 if (tp == tab)
1649 break;
1650 }
1651 return nr;
1652}
1653
Bram Moolenaar6e36b1c2020-07-17 20:47:51 +02001654 static int
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001655comment_start(char_u *p, int starts_with_colon UNUSED)
1656{
1657#ifdef FEAT_EVAL
1658 if (in_vim9script())
1659 return p[0] == '#' && p[1] != '{' && !starts_with_colon;
1660#endif
1661 return *p == '"';
1662}
1663
Bram Moolenaarf240e182014-11-27 18:33:02 +01001664# define CURRENT_WIN_NR current_win_nr(curwin)
1665# define LAST_WIN_NR current_win_nr(NULL)
1666# define CURRENT_TAB_NR current_tab_nr(curtab)
1667# define LAST_TAB_NR current_tab_nr(NULL)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001668
Bram Moolenaar071d4272004-06-13 20:20:40 +00001669/*
1670 * Execute one Ex command.
1671 *
1672 * If 'sourcing' is TRUE, the command will be included in the error message.
1673 *
1674 * 1. skip comment lines and leading space
1675 * 2. handle command modifiers
Bram Moolenaar1c40a662014-11-27 16:38:11 +01001676 * 3. find the command
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001677 * 4. parse range
Bram Moolenaar1c40a662014-11-27 16:38:11 +01001678 * 5. Parse the command.
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001679 * 6. parse arguments
1680 * 7. switch on command name
Bram Moolenaar071d4272004-06-13 20:20:40 +00001681 *
Bram Moolenaar89d40322006-08-29 15:30:07 +00001682 * Note: "fgetline" can be NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001683 *
1684 * This function may be called recursively!
1685 */
1686#if (_MSC_VER == 1200)
1687/*
Bram Moolenaared203462004-06-16 11:19:22 +00001688 * Avoid optimisation bug in VC++ version 6.0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001689 */
Bram Moolenaar281bdce2005-01-25 21:53:18 +00001690 #pragma optimize( "g", off )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001691#endif
1692 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001693do_one_cmd(
Bram Moolenaarddef1292019-12-16 17:10:33 +01001694 char_u **cmdlinep,
1695 int sourcing,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001696#ifdef FEAT_EVAL
Bram Moolenaarddef1292019-12-16 17:10:33 +01001697 cstack_T *cstack,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001698#endif
Bram Moolenaar20b23c62020-08-20 15:25:00 +02001699 char_u *(*fgetline)(int, void *, int, getline_opt_T),
Bram Moolenaarddef1292019-12-16 17:10:33 +01001700 void *cookie) // argument for fgetline()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001701{
Bram Moolenaarddef1292019-12-16 17:10:33 +01001702 char_u *p;
1703 linenr_T lnum;
1704 long n;
1705 char *errormsg = NULL; // error message
1706 char_u *after_modifier = NULL;
1707 exarg_T ea; // Ex command arguments
1708 int save_msg_scroll = msg_scroll;
1709 cmdmod_T save_cmdmod;
1710 int save_reg_executing = reg_executing;
1711 int ni; // set when Not Implemented
1712 char_u *cmd;
Bram Moolenaar6e36b1c2020-07-17 20:47:51 +02001713 int starts_with_colon = FALSE;
Bram Moolenaar83f37b92020-02-23 14:35:01 +01001714#ifdef FEAT_EVAL
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02001715 int may_have_range;
Bram Moolenaar3d48e252020-07-15 14:15:52 +02001716 int vim9script = in_vim9script();
Bram Moolenaar83f37b92020-02-23 14:35:01 +01001717#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001718
Bram Moolenaara80faa82020-04-12 19:37:17 +02001719 CLEAR_FIELD(ea);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001720 ea.line1 = 1;
1721 ea.line2 = 1;
1722#ifdef FEAT_EVAL
1723 ++ex_nesting_level;
1724#endif
1725
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001726 // When the last file has not been edited :q has to be typed twice.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001727 if (quitmore
1728#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001729 // avoid that a function call in 'statusline' does this
Bram Moolenaar89d40322006-08-29 15:30:07 +00001730 && !getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001731#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001732 // avoid that an autocommand, e.g. QuitPre, does this
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001733 && !getline_equal(fgetline, cookie, getnextac))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001734 --quitmore;
1735
1736 /*
1737 * Reset browse, confirm, etc.. They are restored when returning, for
1738 * recursive calls.
1739 */
1740 save_cmdmod = cmdmod;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001741
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001742 // "#!anything" is handled like a comment.
Bram Moolenaarcbb37ad2006-08-16 15:04:21 +00001743 if ((*cmdlinep)[0] == '#' && (*cmdlinep)[1] == '!')
1744 goto doend;
1745
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02001746/*
1747 * 1. Skip comment lines and leading white space and colons.
1748 * 2. Handle command modifiers.
1749 */
1750 // The "ea" structure holds the arguments that can be used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001751 ea.cmd = *cmdlinep;
Bram Moolenaareffed932018-08-14 13:38:17 +02001752 ea.cmdlinep = cmdlinep;
1753 ea.getline = fgetline;
1754 ea.cookie = cookie;
1755#ifdef FEAT_EVAL
1756 ea.cstack = cstack;
Bram Moolenaar83f37b92020-02-23 14:35:01 +01001757 starts_with_colon = *skipwhite(ea.cmd) == ':';
Bram Moolenaar071d4272004-06-13 20:20:40 +00001758#endif
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02001759 if (parse_command_modifiers(&ea, &errormsg, FALSE) == FAIL)
Bram Moolenaareffed932018-08-14 13:38:17 +02001760 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001761
Bram Moolenaar7b668e82016-08-23 23:51:21 +02001762 after_modifier = ea.cmd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001763
1764#ifdef FEAT_EVAL
1765 ea.skip = did_emsg || got_int || did_throw || (cstack->cs_idx >= 0
1766 && !(cstack->cs_flags[cstack->cs_idx] & CSF_ACTIVE));
1767#else
1768 ea.skip = (if_level > 0);
1769#endif
1770
Bram Moolenaar071d4272004-06-13 20:20:40 +00001771/*
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001772 * 3. Skip over the range to find the command. Let "p" point to after it.
1773 *
1774 * We need the command to know what kind of range it uses.
1775 */
1776 cmd = ea.cmd;
Bram Moolenaar3d48e252020-07-15 14:15:52 +02001777#ifdef FEAT_EVAL
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02001778 // In Vim9 script a colon is required before the range.
1779 may_have_range = !vim9script || starts_with_colon;
1780 if (may_have_range)
Bram Moolenaar3d48e252020-07-15 14:15:52 +02001781#endif
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02001782 ea.cmd = skip_range(ea.cmd, TRUE, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001783
1784#ifdef FEAT_EVAL
Bram Moolenaar3d48e252020-07-15 14:15:52 +02001785 if (vim9script && !starts_with_colon)
Bram Moolenaardf069ee2020-06-22 23:02:51 +02001786 {
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02001787 if (ea.cmd == cmd + 1 && *cmd == '$')
1788 // should be "$VAR = val"
1789 --ea.cmd;
1790 else if (ea.cmd > cmd)
Bram Moolenaardf069ee2020-06-22 23:02:51 +02001791 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02001792 emsg(_(e_colon_required_before_a_range));
Bram Moolenaardf069ee2020-06-22 23:02:51 +02001793 goto doend;
1794 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001795 p = find_ex_command(&ea, NULL, lookup_scriptvar, NULL);
Bram Moolenaardf069ee2020-06-22 23:02:51 +02001796 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001797 else
1798#endif
1799 p = find_ex_command(&ea, NULL, NULL, NULL);
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001800
Bram Moolenaar7feb35e2018-08-21 17:49:54 +02001801#ifdef FEAT_EVAL
1802# ifdef FEAT_PROFILE
1803 // Count this line for profiling if skip is TRUE.
1804 if (do_profiling == PROF_YES
1805 && (!ea.skip || cstack->cs_idx == 0 || (cstack->cs_idx > 0
1806 && (cstack->cs_flags[cstack->cs_idx - 1] & CSF_ACTIVE))))
1807 {
1808 int skip = did_emsg || got_int || did_throw;
1809
1810 if (ea.cmdidx == CMD_catch)
1811 skip = !skip && !(cstack->cs_idx >= 0
1812 && (cstack->cs_flags[cstack->cs_idx] & CSF_THROWN)
1813 && !(cstack->cs_flags[cstack->cs_idx] & CSF_CAUGHT));
1814 else if (ea.cmdidx == CMD_else || ea.cmdidx == CMD_elseif)
1815 skip = skip || !(cstack->cs_idx >= 0
1816 && !(cstack->cs_flags[cstack->cs_idx]
1817 & (CSF_ACTIVE | CSF_TRUE)));
1818 else if (ea.cmdidx == CMD_finally)
1819 skip = FALSE;
1820 else if (ea.cmdidx != CMD_endif
1821 && ea.cmdidx != CMD_endfor
1822 && ea.cmdidx != CMD_endtry
1823 && ea.cmdidx != CMD_endwhile)
1824 skip = ea.skip;
1825
1826 if (!skip)
1827 {
1828 if (getline_equal(fgetline, cookie, get_func_line))
1829 func_line_exec(getline_cookie(fgetline, cookie));
1830 else if (getline_equal(fgetline, cookie, getsourceline))
1831 script_line_exec();
1832 }
1833 }
1834# endif
1835
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001836 // May go to debug mode. If this happens and the ">quit" debug command is
1837 // used, throw an interrupt exception and skip the next command.
Bram Moolenaar7feb35e2018-08-21 17:49:54 +02001838 dbg_check_breakpoint(&ea);
1839 if (!ea.skip && got_int)
1840 {
1841 ea.skip = TRUE;
1842 (void)do_intthrow(cstack);
1843 }
1844#endif
1845
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001846/*
1847 * 4. parse a range specifier of the form: addr [,addr] [;addr] ..
Bram Moolenaar071d4272004-06-13 20:20:40 +00001848 *
1849 * where 'addr' is:
1850 *
1851 * % (entire file)
1852 * $ [+-NUM]
1853 * 'x [+-NUM] (where x denotes a currently defined mark)
1854 * . [+-NUM]
1855 * [+-NUM]..
1856 * NUM
1857 *
1858 * The ea.cmd pointer is updated to point to the first character following the
1859 * range spec. If an initial address is found, but no second, the upper bound
1860 * is equal to the lower.
1861 */
1862
Bram Moolenaar25190db2019-05-04 15:05:28 +02001863 // ea.addr_type for user commands is set by find_ucmd
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01001864 if (!IS_USER_CMDIDX(ea.cmdidx))
1865 {
1866 if (ea.cmdidx != CMD_SIZE)
1867 ea.addr_type = cmdnames[(int)ea.cmdidx].cmd_addr_type;
1868 else
1869 ea.addr_type = ADDR_LINES;
1870
Bram Moolenaar25190db2019-05-04 15:05:28 +02001871 // :wincmd range depends on the argument.
Bram Moolenaar164f3262015-01-14 21:22:01 +01001872 if (ea.cmdidx == CMD_wincmd && p != NULL)
1873 get_wincmd_addr_type(skipwhite(p), &ea);
Bram Moolenaar25190db2019-05-04 15:05:28 +02001874#ifdef FEAT_QUICKFIX
1875 // :.cc in quickfix window uses line number
1876 if ((ea.cmdidx == CMD_cc || ea.cmdidx == CMD_ll) && bt_quickfix(curbuf))
1877 ea.addr_type = ADDR_OTHER;
1878#endif
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01001879 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001880
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01001881 ea.cmd = cmd;
Bram Moolenaar3d48e252020-07-15 14:15:52 +02001882#ifdef FEAT_EVAL
Bram Moolenaarc2af0af2020-08-23 21:06:02 +02001883 if (!may_have_range)
1884 ea.line1 = ea.line2 = default_address(&ea);
1885 else
Bram Moolenaar3d48e252020-07-15 14:15:52 +02001886#endif
1887 if (parse_cmd_address(&ea, &errormsg, FALSE) == FAIL)
1888 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001889
Bram Moolenaar071d4272004-06-13 20:20:40 +00001890/*
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001891 * 5. Parse the command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001892 */
1893
1894 /*
1895 * Skip ':' and any white space
1896 */
1897 ea.cmd = skipwhite(ea.cmd);
1898 while (*ea.cmd == ':')
1899 ea.cmd = skipwhite(ea.cmd + 1);
1900
1901 /*
1902 * If we got a line, but no command, then go to the line.
1903 * If we find a '|' or '\n' we set ea.nextcmd.
1904 */
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001905 if (*ea.cmd == NUL || comment_start(ea.cmd, starts_with_colon)
1906 || (ea.nextcmd = check_nextcmd(ea.cmd)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001907 {
1908 /*
1909 * strange vi behaviour:
1910 * ":3" jumps to line 3
1911 * ":3|..." prints line 3
1912 * ":|" prints current line
1913 */
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001914 if (ea.skip) // skip this if inside :if
Bram Moolenaar071d4272004-06-13 20:20:40 +00001915 goto doend;
Bram Moolenaardf177f62005-02-22 08:39:57 +00001916 if (*ea.cmd == '|' || (exmode_active && ea.line1 != ea.line2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001917 {
1918 ea.cmdidx = CMD_print;
Bram Moolenaar8071cb22019-07-12 17:58:01 +02001919 ea.argt = EX_RANGE+EX_COUNT+EX_TRLBAR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001920 if ((errormsg = invalid_range(&ea)) == NULL)
1921 {
1922 correct_range(&ea);
1923 ex_print(&ea);
1924 }
1925 }
1926 else if (ea.addr_count != 0)
1927 {
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00001928 if (ea.line2 > curbuf->b_ml.ml_line_count)
1929 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001930 // With '-' in 'cpoptions' a line number past the file is an
1931 // error, otherwise put it at the end of the file.
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00001932 if (vim_strchr(p_cpo, CPO_MINUS) != NULL)
1933 ea.line2 = -1;
1934 else
1935 ea.line2 = curbuf->b_ml.ml_line_count;
1936 }
1937
1938 if (ea.line2 < 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001939 errormsg = _(e_invrange);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001940 else
1941 {
1942 if (ea.line2 == 0)
1943 curwin->w_cursor.lnum = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001944 else
1945 curwin->w_cursor.lnum = ea.line2;
1946 beginline(BL_SOL | BL_FIX);
1947 }
1948 }
1949 goto doend;
1950 }
1951
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001952 // If this looks like an undefined user command and there are CmdUndefined
1953 // autocommands defined, trigger the matching autocommands.
Bram Moolenaard5005162014-08-22 23:05:54 +02001954 if (p != NULL && ea.cmdidx == CMD_SIZE && !ea.skip
1955 && ASCII_ISUPPER(*ea.cmd)
1956 && has_cmdundefined())
1957 {
Bram Moolenaard5005162014-08-22 23:05:54 +02001958 int ret;
1959
Bram Moolenaar5a31b462014-08-23 14:16:20 +02001960 p = ea.cmd;
Bram Moolenaard5005162014-08-22 23:05:54 +02001961 while (ASCII_ISALNUM(*p))
1962 ++p;
Bram Moolenaar158ea172020-06-18 17:28:39 +02001963 p = vim_strnsave(ea.cmd, p - ea.cmd);
Bram Moolenaard5005162014-08-22 23:05:54 +02001964 ret = apply_autocmds(EVENT_CMDUNDEFINED, p, p, TRUE, NULL);
1965 vim_free(p);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001966 // If the autocommands did something and didn't cause an error, try
1967 // finding the command again.
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001968 p = (ret
1969#ifdef FEAT_EVAL
1970 && !aborting()
Bram Moolenaard5005162014-08-22 23:05:54 +02001971#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001972 ) ? find_ex_command(&ea, NULL, NULL, NULL) : ea.cmd;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001973 }
Bram Moolenaard5005162014-08-22 23:05:54 +02001974
Bram Moolenaar071d4272004-06-13 20:20:40 +00001975 if (p == NULL)
1976 {
1977 if (!ea.skip)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001978 errormsg = _("E464: Ambiguous use of user-defined command");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001979 goto doend;
1980 }
Bram Moolenaarac9fb182019-04-27 13:04:13 +02001981 // Check for wrong commands.
Bram Moolenaar6f9a4762017-06-22 20:39:17 +02001982 if (*p == '!' && ea.cmd[1] == 0151 && ea.cmd[0] == 78
1983 && !IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001984 {
1985 errormsg = uc_fun_cmd();
1986 goto doend;
1987 }
Bram Moolenaarac9fb182019-04-27 13:04:13 +02001988
Bram Moolenaar071d4272004-06-13 20:20:40 +00001989 if (ea.cmdidx == CMD_SIZE)
1990 {
1991 if (!ea.skip)
1992 {
1993 STRCPY(IObuff, _("E492: Not an editor command"));
1994 if (!sourcing)
Bram Moolenaar7b668e82016-08-23 23:51:21 +02001995 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001996 // If the modifier was parsed OK the error must be in the
1997 // following command
Bram Moolenaar7b668e82016-08-23 23:51:21 +02001998 if (after_modifier != NULL)
1999 append_command(after_modifier);
2000 else
2001 append_command(*cmdlinep);
2002 }
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002003 errormsg = (char *)IObuff;
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02002004 did_emsg_syntax = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002005 }
2006 goto doend;
2007 }
2008
Bram Moolenaar958636c2014-10-21 20:01:58 +02002009 ni = (!IS_USER_CMDIDX(ea.cmdidx)
2010 && (cmdnames[ea.cmdidx].cmd_func == ex_ni
Bram Moolenaar7bb75552007-07-16 18:39:49 +00002011#ifdef HAVE_EX_SCRIPT_NI
2012 || cmdnames[ea.cmdidx].cmd_func == ex_script_ni
2013#endif
2014 ));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002015
2016#ifndef FEAT_EVAL
2017 /*
2018 * When the expression evaluation is disabled, recognize the ":if" and
2019 * ":endif" commands and ignore everything in between it.
2020 */
2021 if (ea.cmdidx == CMD_if)
2022 ++if_level;
2023 if (if_level)
2024 {
2025 if (ea.cmdidx == CMD_endif)
2026 --if_level;
2027 goto doend;
2028 }
2029
2030#endif
2031
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002032 // forced commands
Bram Moolenaar196b3b02008-06-20 16:51:41 +00002033 if (*p == '!' && ea.cmdidx != CMD_substitute
2034 && ea.cmdidx != CMD_smagic && ea.cmdidx != CMD_snomagic)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002035 {
2036 ++p;
2037 ea.forceit = TRUE;
2038 }
2039 else
2040 ea.forceit = FALSE;
2041
2042/*
Bram Moolenaarb7316892019-05-01 18:08:42 +02002043 * 6. Parse arguments. Then check for errors.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002044 */
Bram Moolenaar958636c2014-10-21 20:01:58 +02002045 if (!IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002046 ea.argt = (long)cmdnames[(int)ea.cmdidx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002047
2048 if (!ea.skip)
2049 {
2050#ifdef HAVE_SANDBOX
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002051 if (sandbox != 0 && !(ea.argt & EX_SBOXOK))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002052 {
Bram Moolenaar8c62a082019-02-08 14:34:10 +01002053 // Command not allowed in sandbox.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002054 errormsg = _(e_sandbox);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002055 goto doend;
2056 }
2057#endif
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002058 if (restricted != 0 && (ea.argt & EX_RESTRICT))
Bram Moolenaar8c62a082019-02-08 14:34:10 +01002059 {
2060 errormsg = _("E981: Command not allowed in rvim");
2061 goto doend;
2062 }
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002063 if (!curbuf->b_p_ma && (ea.argt & EX_MODIFY))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002064 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002065 // Command not allowed in non-'modifiable' buffer
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002066 errormsg = _(e_modifiable);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002067 goto doend;
2068 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002069
Bram Moolenaar37394ff2020-07-25 19:38:18 +02002070 if (!IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002071 {
Bram Moolenaar37394ff2020-07-25 19:38:18 +02002072#ifdef FEAT_CMDWIN
2073 if (cmdwin_type != 0 && !(ea.argt & EX_CMDWIN))
2074 {
2075 // Command not allowed in the command line window
2076 errormsg = _(e_cmdwin);
2077 goto doend;
2078 }
2079#endif
2080 if (text_locked() && !(ea.argt & EX_LOCK_OK))
2081 {
2082 // Command not allowed when text is locked
2083 errormsg = _(get_text_locked_msg());
2084 goto doend;
2085 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002086 }
Bram Moolenaar379fb762018-08-30 15:58:28 +02002087
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002088 // Disallow editing another buffer when "curbuf_lock" is set.
2089 // Do allow ":checktime" (it is postponed).
2090 // Do allow ":edit" (check for an argument later).
2091 // Do allow ":file" with no arguments (check for an argument later).
Bram Moolenaar8e1986e2020-08-06 22:11:06 +02002092 if (!(ea.argt & (EX_CMDWIN | EX_LOCK_OK))
Bram Moolenaar5555acc2006-04-07 21:33:12 +00002093 && ea.cmdidx != CMD_checktime
Bram Moolenaar379fb762018-08-30 15:58:28 +02002094 && ea.cmdidx != CMD_edit
2095 && ea.cmdidx != CMD_file
Bram Moolenaar958636c2014-10-21 20:01:58 +02002096 && !IS_USER_CMDIDX(ea.cmdidx)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002097 && curbuf_locked())
2098 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002099
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002100 if (!ni && !(ea.argt & EX_RANGE) && ea.addr_count > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002101 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002102 // no range allowed
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002103 errormsg = _(e_norange);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002104 goto doend;
2105 }
2106 }
2107
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002108 if (!ni && !(ea.argt & EX_BANG) && ea.forceit) // no <!> allowed
Bram Moolenaar071d4272004-06-13 20:20:40 +00002109 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002110 errormsg = _(e_nobang);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002111 goto doend;
2112 }
2113
2114 /*
2115 * Don't complain about the range if it is not used
2116 * (could happen if line_count is accidentally set to 0).
2117 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002118 if (!ea.skip && !ni && (ea.argt & EX_RANGE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002119 {
2120 /*
2121 * If the range is backwards, ask for confirmation and, if given, swap
2122 * ea.line1 & ea.line2 so it's forwards again.
2123 * When global command is busy, don't ask, will fail below.
2124 */
2125 if (!global_busy && ea.line1 > ea.line2)
2126 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002127 if (msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002128 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002129 if (sourcing || exmode_active)
2130 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002131 errormsg = _("E493: Backwards range given");
Bram Moolenaara5792f52005-11-23 21:25:05 +00002132 goto doend;
2133 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002134 if (ask_yesno((char_u *)
2135 _("Backwards range given, OK to swap"), FALSE) != 'y')
Bram Moolenaara5792f52005-11-23 21:25:05 +00002136 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002137 }
2138 lnum = ea.line1;
2139 ea.line1 = ea.line2;
2140 ea.line2 = lnum;
2141 }
2142 if ((errormsg = invalid_range(&ea)) != NULL)
2143 goto doend;
2144 }
2145
Bram Moolenaarb7316892019-05-01 18:08:42 +02002146 if ((ea.addr_type == ADDR_OTHER) && ea.addr_count == 0)
2147 // default is 1, not cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00002148 ea.line2 = 1;
2149
2150 correct_range(&ea);
2151
2152#ifdef FEAT_FOLDING
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002153 if (((ea.argt & EX_WHOLEFOLD) || ea.addr_count >= 2) && !global_busy
Bram Moolenaara3306952016-01-02 21:41:06 +01002154 && ea.addr_type == ADDR_LINES)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002155 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002156 // Put the first line at the start of a closed fold, put the last line
2157 // at the end of a closed fold.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002158 (void)hasFolding(ea.line1, &ea.line1, NULL);
2159 (void)hasFolding(ea.line2, NULL, &ea.line2);
2160 }
2161#endif
2162
2163#ifdef FEAT_QUICKFIX
2164 /*
Bram Moolenaar86b68352004-12-27 21:59:20 +00002165 * For the ":make" and ":grep" commands we insert the 'makeprg'/'grepprg'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002166 * option here, so things like % get expanded.
2167 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002168 p = replace_makeprg(&ea, p, cmdlinep);
2169 if (p == NULL)
2170 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002171#endif
2172
2173 /*
2174 * Skip to start of argument.
2175 * Don't do this for the ":!" command, because ":!! -l" needs the space.
2176 */
2177 if (ea.cmdidx == CMD_bang)
2178 ea.arg = p;
2179 else
2180 ea.arg = skipwhite(p);
2181
Bram Moolenaar379fb762018-08-30 15:58:28 +02002182 // ":file" cannot be run with an argument when "curbuf_lock" is set
2183 if (ea.cmdidx == CMD_file && *ea.arg != NUL && curbuf_locked())
2184 goto doend;
2185
Bram Moolenaar071d4272004-06-13 20:20:40 +00002186 /*
2187 * Check for "++opt=val" argument.
2188 * Must be first, allow ":w ++enc=utf8 !cmd"
2189 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002190 if (ea.argt & EX_ARGOPT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002191 while (ea.arg[0] == '+' && ea.arg[1] == '+')
2192 if (getargopt(&ea) == FAIL && !ni)
2193 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002194 errormsg = _(e_invarg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002195 goto doend;
2196 }
2197
2198 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
2199 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002200 if (*ea.arg == '>') // append
Bram Moolenaar071d4272004-06-13 20:20:40 +00002201 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002202 if (*++ea.arg != '>') // typed wrong
Bram Moolenaar071d4272004-06-13 20:20:40 +00002203 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002204 errormsg = _("E494: Use w or w>>");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002205 goto doend;
2206 }
2207 ea.arg = skipwhite(ea.arg + 1);
2208 ea.append = TRUE;
2209 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002210 else if (*ea.arg == '!' && ea.cmdidx == CMD_write) // :w !filter
Bram Moolenaar071d4272004-06-13 20:20:40 +00002211 {
2212 ++ea.arg;
2213 ea.usefilter = TRUE;
2214 }
2215 }
2216
2217 if (ea.cmdidx == CMD_read)
2218 {
2219 if (ea.forceit)
2220 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002221 ea.usefilter = TRUE; // :r! filter if ea.forceit
Bram Moolenaar071d4272004-06-13 20:20:40 +00002222 ea.forceit = FALSE;
2223 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002224 else if (*ea.arg == '!') // :r !filter
Bram Moolenaar071d4272004-06-13 20:20:40 +00002225 {
2226 ++ea.arg;
2227 ea.usefilter = TRUE;
2228 }
2229 }
2230
2231 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
2232 {
2233 ea.amount = 1;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002234 while (*ea.arg == *ea.cmd) // count number of '>' or '<'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002235 {
2236 ++ea.arg;
2237 ++ea.amount;
2238 }
2239 ea.arg = skipwhite(ea.arg);
2240 }
2241
2242 /*
2243 * Check for "+command" argument, before checking for next command.
2244 * Don't do this for ":read !cmd" and ":write !cmd".
2245 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002246 if ((ea.argt & EX_CMDARG) && !ea.usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002247 ea.do_ecmd_cmd = getargcmd(&ea.arg);
2248
2249 /*
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002250 * Check for '|' to separate commands and '"' or '#' to start comments.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002251 * Don't do this for ":read !cmd" and ":write !cmd".
2252 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002253 if ((ea.argt & EX_TRLBAR) && !ea.usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002254 separate_nextcmd(&ea);
2255
2256 /*
2257 * Check for <newline> to end a shell command.
Bram Moolenaardf177f62005-02-22 08:39:57 +00002258 * Also do this for ":read !cmd", ":write !cmd" and ":global".
2259 * Any others?
Bram Moolenaar071d4272004-06-13 20:20:40 +00002260 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00002261 else if (ea.cmdidx == CMD_bang
Bram Moolenaar67883b42017-07-27 22:57:00 +02002262 || ea.cmdidx == CMD_terminal
Bram Moolenaardf177f62005-02-22 08:39:57 +00002263 || ea.cmdidx == CMD_global
2264 || ea.cmdidx == CMD_vglobal
2265 || ea.usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002266 {
2267 for (p = ea.arg; *p; ++p)
2268 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002269 // Remove one backslash before a newline, so that it's possible to
2270 // pass a newline to the shell and also a newline that is preceded
2271 // with a backslash. This makes it impossible to end a shell
2272 // command in a backslash, but that doesn't appear useful.
2273 // Halving the number of backslashes is incompatible with previous
2274 // versions.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002275 if (*p == '\\' && p[1] == '\n')
Bram Moolenaara7241f52008-06-24 20:39:31 +00002276 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002277 else if (*p == '\n')
2278 {
2279 ea.nextcmd = p + 1;
2280 *p = NUL;
2281 break;
2282 }
2283 }
2284 }
2285
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002286 if ((ea.argt & EX_DFLALL) && ea.addr_count == 0)
Bram Moolenaarc2af0af2020-08-23 21:06:02 +02002287 address_default_all(&ea);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002288
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002289 // accept numbered register only when no count allowed (:put)
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002290 if ( (ea.argt & EX_REGSTR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002291 && *ea.arg != NUL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002292 // Do not allow register = for user commands
Bram Moolenaar958636c2014-10-21 20:01:58 +02002293 && (!IS_USER_CMDIDX(ea.cmdidx) || *ea.arg != '=')
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002294 && !((ea.argt & EX_COUNT) && VIM_ISDIGIT(*ea.arg)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002295 {
Bram Moolenaar85de2062011-05-05 14:26:41 +02002296#ifndef FEAT_CLIPBOARD
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002297 // check these explicitly for a more specific error message
Bram Moolenaar85de2062011-05-05 14:26:41 +02002298 if (*ea.arg == '*' || *ea.arg == '+')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002299 {
Bram Moolenaar99b12722019-01-14 20:16:40 +01002300 errormsg = _(e_invalidreg);
Bram Moolenaar85de2062011-05-05 14:26:41 +02002301 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002302 }
2303#endif
Bram Moolenaar958636c2014-10-21 20:01:58 +02002304 if (valid_yank_reg(*ea.arg, (ea.cmdidx != CMD_put
2305 && !IS_USER_CMDIDX(ea.cmdidx))))
Bram Moolenaar85de2062011-05-05 14:26:41 +02002306 {
2307 ea.regname = *ea.arg++;
2308#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002309 // for '=' register: accept the rest of the line as an expression
Bram Moolenaar85de2062011-05-05 14:26:41 +02002310 if (ea.arg[-1] == '=' && ea.arg[0] != NUL)
2311 {
2312 set_expr_line(vim_strsave(ea.arg));
2313 ea.arg += STRLEN(ea.arg);
2314 }
2315#endif
2316 ea.arg = skipwhite(ea.arg);
2317 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002318 }
2319
2320 /*
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002321 * Check for a count. When accepting a EX_BUFNAME, don't use "123foo" as a
Bram Moolenaar071d4272004-06-13 20:20:40 +00002322 * count, it's a buffer name.
2323 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002324 if ((ea.argt & EX_COUNT) && VIM_ISDIGIT(*ea.arg)
2325 && (!(ea.argt & EX_BUFNAME) || *(p = skipdigits(ea.arg)) == NUL
Bram Moolenaar1c465442017-03-12 20:10:05 +01002326 || VIM_ISWHITE(*p)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002327 {
2328 n = getdigits(&ea.arg);
2329 ea.arg = skipwhite(ea.arg);
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002330 if (n <= 0 && !ni && (ea.argt & EX_ZEROR) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002331 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002332 errormsg = _(e_zerocount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002333 goto doend;
2334 }
Bram Moolenaarb7316892019-05-01 18:08:42 +02002335 if (ea.addr_type != ADDR_LINES) // e.g. :buffer 2, :sleep 3
Bram Moolenaar071d4272004-06-13 20:20:40 +00002336 {
2337 ea.line2 = n;
2338 if (ea.addr_count == 0)
2339 ea.addr_count = 1;
2340 }
2341 else
2342 {
2343 ea.line1 = ea.line2;
2344 ea.line2 += n - 1;
2345 ++ea.addr_count;
2346 /*
2347 * Be vi compatible: no error message for out of range.
2348 */
Bram Moolenaarb7316892019-05-01 18:08:42 +02002349 if (ea.line2 > curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002350 ea.line2 = curbuf->b_ml.ml_line_count;
2351 }
2352 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00002353
2354 /*
2355 * Check for flags: 'l', 'p' and '#'.
2356 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002357 if (ea.argt & EX_FLAGS)
Bram Moolenaardf177f62005-02-22 08:39:57 +00002358 get_flags(&ea);
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002359 if (!ni && !(ea.argt & EX_EXTRA) && *ea.arg != NUL
2360 && *ea.arg != '"' && (*ea.arg != '|' || (ea.argt & EX_TRLBAR) == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002361 {
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002362 // no arguments allowed but there is something
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +02002363 errormsg = ex_errmsg(e_trailing_arg, ea.arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002364 goto doend;
2365 }
2366
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002367 if (!ni && (ea.argt & EX_NEEDARG) && *ea.arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002368 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002369 errormsg = _(e_argreq);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002370 goto doend;
2371 }
2372
2373#ifdef FEAT_EVAL
2374 /*
2375 * Skip the command when it's not going to be executed.
2376 * The commands like :if, :endif, etc. always need to be executed.
2377 * Also make an exception for commands that handle a trailing command
2378 * themselves.
2379 */
2380 if (ea.skip)
2381 {
2382 switch (ea.cmdidx)
2383 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002384 // commands that need evaluation
Bram Moolenaar071d4272004-06-13 20:20:40 +00002385 case CMD_while:
2386 case CMD_endwhile:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00002387 case CMD_for:
2388 case CMD_endfor:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002389 case CMD_if:
2390 case CMD_elseif:
2391 case CMD_else:
2392 case CMD_endif:
2393 case CMD_try:
2394 case CMD_catch:
2395 case CMD_finally:
2396 case CMD_endtry:
2397 case CMD_function:
Bram Moolenaarab55c682020-03-01 19:41:43 +01002398 case CMD_def:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002399 break;
2400
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002401 // Commands that handle '|' themselves. Check: A command should
2402 // either have the EX_TRLBAR flag, appear in this list or appear in
2403 // the list at ":help :bar".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002404 case CMD_aboveleft:
2405 case CMD_and:
2406 case CMD_belowright:
2407 case CMD_botright:
2408 case CMD_browse:
2409 case CMD_call:
2410 case CMD_confirm:
Bram Moolenaar8f76e6b2019-11-26 16:50:30 +01002411 case CMD_const:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002412 case CMD_delfunction:
2413 case CMD_djump:
2414 case CMD_dlist:
2415 case CMD_dsearch:
2416 case CMD_dsplit:
2417 case CMD_echo:
2418 case CMD_echoerr:
2419 case CMD_echomsg:
2420 case CMD_echon:
Bram Moolenaara76b3152020-02-16 16:20:21 +01002421 case CMD_eval:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002422 case CMD_execute:
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002423 case CMD_filter:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002424 case CMD_help:
2425 case CMD_hide:
2426 case CMD_ijump:
2427 case CMD_ilist:
2428 case CMD_isearch:
2429 case CMD_isplit:
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002430 case CMD_keepalt:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002431 case CMD_keepjumps:
2432 case CMD_keepmarks:
Bram Moolenaara939e432013-11-09 05:30:26 +01002433 case CMD_keeppatterns:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002434 case CMD_leftabove:
2435 case CMD_let:
2436 case CMD_lockmarks:
Bram Moolenaarcc4423a2019-11-26 17:05:00 +01002437 case CMD_lockvar:
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002438 case CMD_lua:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002439 case CMD_match:
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002440 case CMD_mzscheme:
Bram Moolenaar5803ae62014-03-23 16:04:02 +01002441 case CMD_noautocmd:
2442 case CMD_noswapfile:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002443 case CMD_perl:
2444 case CMD_psearch:
2445 case CMD_python:
Bram Moolenaar368373e2010-07-19 20:46:22 +02002446 case CMD_py3:
Bram Moolenaarb6590522010-07-21 16:00:43 +02002447 case CMD_python3:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002448 case CMD_return:
2449 case CMD_rightbelow:
2450 case CMD_ruby:
2451 case CMD_silent:
2452 case CMD_smagic:
2453 case CMD_snomagic:
2454 case CMD_substitute:
2455 case CMD_syntax:
Bram Moolenaara226a6d2006-02-26 23:59:20 +00002456 case CMD_tab:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002457 case CMD_tcl:
2458 case CMD_throw:
2459 case CMD_tilde:
2460 case CMD_topleft:
2461 case CMD_unlet:
Bram Moolenaarcc4423a2019-11-26 17:05:00 +01002462 case CMD_unlockvar:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002463 case CMD_verbose:
2464 case CMD_vertical:
Bram Moolenaar12bde492011-06-13 01:19:56 +02002465 case CMD_wincmd:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002466 break;
2467
2468 default: goto doend;
2469 }
2470 }
2471#endif
2472
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002473 if (ea.argt & EX_XFILE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002474 {
2475 if (expand_filename(&ea, cmdlinep, &errormsg) == FAIL)
2476 goto doend;
2477 }
2478
Bram Moolenaar071d4272004-06-13 20:20:40 +00002479 /*
2480 * Accept buffer name. Cannot be used at the same time with a buffer
2481 * number. Don't do this for a user command.
2482 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002483 if ((ea.argt & EX_BUFNAME) && *ea.arg != NUL && ea.addr_count == 0
Bram Moolenaar958636c2014-10-21 20:01:58 +02002484 && !IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002485 {
2486 /*
2487 * :bdelete, :bwipeout and :bunload take several arguments, separated
2488 * by spaces: find next space (skipping over escaped characters).
2489 * The others take one argument: ignore trailing spaces.
2490 */
2491 if (ea.cmdidx == CMD_bdelete || ea.cmdidx == CMD_bwipeout
2492 || ea.cmdidx == CMD_bunload)
2493 p = skiptowhite_esc(ea.arg);
2494 else
2495 {
2496 p = ea.arg + STRLEN(ea.arg);
Bram Moolenaar1c465442017-03-12 20:10:05 +01002497 while (p > ea.arg && VIM_ISWHITE(p[-1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002498 --p;
2499 }
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002500 ea.line2 = buflist_findpat(ea.arg, p, (ea.argt & EX_BUFUNL) != 0,
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002501 FALSE, FALSE);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002502 if (ea.line2 < 0) // failed
Bram Moolenaar071d4272004-06-13 20:20:40 +00002503 goto doend;
2504 ea.addr_count = 1;
2505 ea.arg = skipwhite(p);
2506 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002507
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002508 // The :try command saves the emsg_silent flag, reset it here when
2509 // ":silent! try" was used, it should only apply to :try itself.
Bram Moolenaareffed932018-08-14 13:38:17 +02002510 if (ea.cmdidx == CMD_try && ea.did_esilent > 0)
Bram Moolenaar2be57332018-02-13 18:05:18 +01002511 {
Bram Moolenaareffed932018-08-14 13:38:17 +02002512 emsg_silent -= ea.did_esilent;
Bram Moolenaar2be57332018-02-13 18:05:18 +01002513 if (emsg_silent < 0)
2514 emsg_silent = 0;
Bram Moolenaareffed932018-08-14 13:38:17 +02002515 ea.did_esilent = 0;
Bram Moolenaar2be57332018-02-13 18:05:18 +01002516 }
2517
Bram Moolenaar071d4272004-06-13 20:20:40 +00002518/*
Bram Moolenaar2be57332018-02-13 18:05:18 +01002519 * 7. Execute the command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002520 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002521
Bram Moolenaar958636c2014-10-21 20:01:58 +02002522 if (IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002523 {
2524 /*
2525 * Execute a user-defined command.
2526 */
2527 do_ucmd(&ea);
2528 }
2529 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002530 {
2531 /*
Bram Moolenaarac9fb182019-04-27 13:04:13 +02002532 * Call the function to execute the builtin command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002533 */
2534 ea.errmsg = NULL;
2535 (cmdnames[ea.cmdidx].cmd_func)(&ea);
2536 if (ea.errmsg != NULL)
Bram Moolenaar8930caa2020-07-23 16:37:03 +02002537 errormsg = ea.errmsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002538 }
2539
2540#ifdef FEAT_EVAL
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002541 // Set flag that any command was executed, used by ex_vim9script().
Bram Moolenaar16531552020-02-08 16:00:46 +01002542 if (getline_equal(ea.getline, ea.cookie, getsourceline)
2543 && current_sctx.sc_sid > 0)
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002544 SCRIPT_ITEM(current_sctx.sc_sid)->sn_had_command = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002545
Bram Moolenaar071d4272004-06-13 20:20:40 +00002546 /*
2547 * If the command just executed called do_cmdline(), any throw or ":return"
2548 * or ":finish" encountered there must also check the cstack of the still
2549 * active do_cmdline() that called this do_one_cmd(). Rethrow an uncaught
2550 * exception, or reanimate a returned function or finished script file and
2551 * return or finish it again.
2552 */
2553 if (need_rethrow)
2554 do_throw(cstack);
2555 else if (check_cstack)
2556 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002557 if (source_finished(fgetline, cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002558 do_finish(&ea, TRUE);
Bram Moolenaar89d40322006-08-29 15:30:07 +00002559 else if (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002560 && current_func_returned())
2561 do_return(&ea, TRUE, FALSE, NULL);
2562 }
2563 need_rethrow = check_cstack = FALSE;
2564#endif
2565
2566doend:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002567 if (curwin->w_cursor.lnum == 0) // can happen with zero line number
Bram Moolenaar6de5e122017-04-20 21:55:44 +02002568 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002569 curwin->w_cursor.lnum = 1;
Bram Moolenaar6de5e122017-04-20 21:55:44 +02002570 curwin->w_cursor.col = 0;
2571 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002572
2573 if (errormsg != NULL && *errormsg != NUL && !did_emsg)
2574 {
2575 if (sourcing)
2576 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002577 if (errormsg != (char *)IObuff)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002578 {
2579 STRCPY(IObuff, errormsg);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002580 errormsg = (char *)IObuff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002581 }
Bram Moolenaara6f4d612011-09-21 19:10:46 +02002582 append_command(*cmdlinep);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002583 }
2584 emsg(errormsg);
2585 }
2586#ifdef FEAT_EVAL
2587 do_errthrow(cstack,
Bram Moolenaar958636c2014-10-21 20:01:58 +02002588 (ea.cmdidx != CMD_SIZE && !IS_USER_CMDIDX(ea.cmdidx))
2589 ? cmdnames[(int)ea.cmdidx].cmd_name : (char_u *)NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002590#endif
2591
Bram Moolenaar47e7d702020-07-05 18:18:42 +02002592 undo_cmdmod(&ea, save_msg_scroll);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002593 cmdmod = save_cmdmod;
Bram Moolenaar9a2c0912019-03-30 14:26:18 +01002594 reg_executing = save_reg_executing;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002595
Bram Moolenaar7171abe2004-10-11 10:06:20 +00002596#ifdef HAVE_SANDBOX
Bram Moolenaareffed932018-08-14 13:38:17 +02002597 if (ea.did_sandbox)
Bram Moolenaar7171abe2004-10-11 10:06:20 +00002598 --sandbox;
2599#endif
2600
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002601 if (ea.nextcmd && *ea.nextcmd == NUL) // not really a next command
Bram Moolenaar071d4272004-06-13 20:20:40 +00002602 ea.nextcmd = NULL;
2603
2604#ifdef FEAT_EVAL
2605 --ex_nesting_level;
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002606 vim_free(ea.cmdline_tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002607#endif
2608
2609 return ea.nextcmd;
2610}
2611#if (_MSC_VER == 1200)
Bram Moolenaar281bdce2005-01-25 21:53:18 +00002612 #pragma optimize( "", on )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002613#endif
2614
Bram Moolenaar8930caa2020-07-23 16:37:03 +02002615static char ex_error_buf[MSG_BUF_LEN];
2616
2617/*
2618 * Return an error message with argument included.
2619 * Uses a static buffer, only the last error will be kept.
2620 * "msg" will be translated, caller should use N_().
2621 */
2622 char *
2623ex_errmsg(char *msg, char_u *arg)
2624{
2625 vim_snprintf(ex_error_buf, MSG_BUF_LEN, _(msg), arg);
2626 return ex_error_buf;
2627}
2628
Bram Moolenaar071d4272004-06-13 20:20:40 +00002629/*
Bram Moolenaareffed932018-08-14 13:38:17 +02002630 * Parse and skip over command modifiers:
2631 * - update eap->cmd
2632 * - store flags in "cmdmod".
2633 * - Set ex_pressedreturn for an empty command line.
2634 * - set msg_silent for ":silent"
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002635 * - set 'eventignore' to "all" for ":noautocmd"
Bram Moolenaareffed932018-08-14 13:38:17 +02002636 * - set p_verbose for ":verbose"
2637 * - Increment "sandbox" for ":sandbox"
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002638 * When "skip_only" is TRUE the global variables are not changed, except for
2639 * "cmdmod".
Bram Moolenaareffed932018-08-14 13:38:17 +02002640 * Return FAIL when the command is not to be executed.
2641 * May set "errormsg" to an error message.
2642 */
2643 int
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002644parse_command_modifiers(exarg_T *eap, char **errormsg, int skip_only)
Bram Moolenaareffed932018-08-14 13:38:17 +02002645{
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002646 char_u *p;
2647 int starts_with_colon = FALSE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002648
Bram Moolenaara80faa82020-04-12 19:37:17 +02002649 CLEAR_FIELD(cmdmod);
Bram Moolenaareffed932018-08-14 13:38:17 +02002650 eap->verbose_save = -1;
2651 eap->save_msg_silent = -1;
2652
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002653 // Repeat until no more command modifiers are found.
Bram Moolenaareffed932018-08-14 13:38:17 +02002654 for (;;)
2655 {
Bram Moolenaareffed932018-08-14 13:38:17 +02002656 while (*eap->cmd == ' ' || *eap->cmd == '\t' || *eap->cmd == ':')
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002657 {
2658 if (*eap->cmd == ':')
2659 starts_with_colon = TRUE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002660 ++eap->cmd;
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002661 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002662
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002663 // in ex mode, an empty line works like :+
Bram Moolenaareffed932018-08-14 13:38:17 +02002664 if (*eap->cmd == NUL && exmode_active
2665 && (getline_equal(eap->getline, eap->cookie, getexmodeline)
2666 || getline_equal(eap->getline, eap->cookie, getexline))
2667 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
2668 {
2669 eap->cmd = (char_u *)"+";
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002670 if (!skip_only)
2671 ex_pressedreturn = TRUE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002672 }
2673
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002674 // ignore comment and empty lines
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002675 if (comment_start(eap->cmd, starts_with_colon))
Bram Moolenaareffed932018-08-14 13:38:17 +02002676 return FAIL;
2677 if (*eap->cmd == NUL)
2678 {
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002679 if (!skip_only)
2680 ex_pressedreturn = TRUE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002681 return FAIL;
2682 }
2683
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02002684 p = skip_range(eap->cmd, TRUE, NULL);
Bram Moolenaareffed932018-08-14 13:38:17 +02002685 switch (*p)
2686 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002687 // When adding an entry, also modify cmd_exists().
Bram Moolenaareffed932018-08-14 13:38:17 +02002688 case 'a': if (!checkforcmd(&eap->cmd, "aboveleft", 3))
2689 break;
2690 cmdmod.split |= WSP_ABOVE;
2691 continue;
2692
2693 case 'b': if (checkforcmd(&eap->cmd, "belowright", 3))
2694 {
2695 cmdmod.split |= WSP_BELOW;
2696 continue;
2697 }
2698 if (checkforcmd(&eap->cmd, "browse", 3))
2699 {
2700#ifdef FEAT_BROWSE_CMD
2701 cmdmod.browse = TRUE;
2702#endif
2703 continue;
2704 }
2705 if (!checkforcmd(&eap->cmd, "botright", 2))
2706 break;
2707 cmdmod.split |= WSP_BOT;
2708 continue;
2709
2710 case 'c': if (!checkforcmd(&eap->cmd, "confirm", 4))
2711 break;
2712#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
2713 cmdmod.confirm = TRUE;
2714#endif
2715 continue;
2716
2717 case 'k': if (checkforcmd(&eap->cmd, "keepmarks", 3))
2718 {
2719 cmdmod.keepmarks = TRUE;
2720 continue;
2721 }
2722 if (checkforcmd(&eap->cmd, "keepalt", 5))
2723 {
2724 cmdmod.keepalt = TRUE;
2725 continue;
2726 }
2727 if (checkforcmd(&eap->cmd, "keeppatterns", 5))
2728 {
2729 cmdmod.keeppatterns = TRUE;
2730 continue;
2731 }
2732 if (!checkforcmd(&eap->cmd, "keepjumps", 5))
2733 break;
2734 cmdmod.keepjumps = TRUE;
2735 continue;
2736
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002737 case 'f': // only accept ":filter {pat} cmd"
Bram Moolenaareffed932018-08-14 13:38:17 +02002738 {
2739 char_u *reg_pat;
2740
2741 if (!checkforcmd(&p, "filter", 4)
2742 || *p == NUL || ends_excmd(*p))
2743 break;
2744 if (*p == '!')
2745 {
2746 cmdmod.filter_force = TRUE;
2747 p = skipwhite(p + 1);
2748 if (*p == NUL || ends_excmd(*p))
2749 break;
2750 }
Bram Moolenaar1e624c92020-07-11 14:08:04 +02002751#ifdef FEAT_EVAL
Bram Moolenaarb074e8b2020-07-11 13:40:45 +02002752 // Avoid that "filter(arg)" is recognized.
2753 if (in_vim9script() && !VIM_ISWHITE(*p))
2754 break;
Bram Moolenaar1e624c92020-07-11 14:08:04 +02002755#endif
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002756 if (skip_only)
2757 p = skip_vimgrep_pat(p, NULL, NULL);
2758 else
2759 // NOTE: This puts a NUL after the pattern.
2760 p = skip_vimgrep_pat(p, &reg_pat, NULL);
Bram Moolenaareffed932018-08-14 13:38:17 +02002761 if (p == NULL || *p == NUL)
2762 break;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002763 if (!skip_only)
2764 {
2765 cmdmod.filter_regmatch.regprog =
Bram Moolenaareffed932018-08-14 13:38:17 +02002766 vim_regcomp(reg_pat, RE_MAGIC);
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002767 if (cmdmod.filter_regmatch.regprog == NULL)
2768 break;
2769 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002770 eap->cmd = p;
2771 continue;
2772 }
2773
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002774 // ":hide" and ":hide | cmd" are not modifiers
Bram Moolenaareffed932018-08-14 13:38:17 +02002775 case 'h': if (p != eap->cmd || !checkforcmd(&p, "hide", 3)
2776 || *p == NUL || ends_excmd(*p))
2777 break;
2778 eap->cmd = p;
2779 cmdmod.hide = TRUE;
2780 continue;
2781
2782 case 'l': if (checkforcmd(&eap->cmd, "lockmarks", 3))
2783 {
2784 cmdmod.lockmarks = TRUE;
2785 continue;
2786 }
2787
2788 if (!checkforcmd(&eap->cmd, "leftabove", 5))
2789 break;
2790 cmdmod.split |= WSP_ABOVE;
2791 continue;
2792
2793 case 'n': if (checkforcmd(&eap->cmd, "noautocmd", 3))
2794 {
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002795 if (cmdmod.save_ei == NULL && !skip_only)
Bram Moolenaareffed932018-08-14 13:38:17 +02002796 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002797 // Set 'eventignore' to "all". Restore the
2798 // existing option value later.
Bram Moolenaareffed932018-08-14 13:38:17 +02002799 cmdmod.save_ei = vim_strsave(p_ei);
2800 set_string_option_direct((char_u *)"ei", -1,
2801 (char_u *)"all", OPT_FREE, SID_NONE);
2802 }
2803 continue;
2804 }
2805 if (!checkforcmd(&eap->cmd, "noswapfile", 3))
2806 break;
2807 cmdmod.noswapfile = TRUE;
2808 continue;
2809
2810 case 'r': if (!checkforcmd(&eap->cmd, "rightbelow", 6))
2811 break;
2812 cmdmod.split |= WSP_BELOW;
2813 continue;
2814
2815 case 's': if (checkforcmd(&eap->cmd, "sandbox", 3))
2816 {
2817#ifdef HAVE_SANDBOX
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002818 if (!skip_only)
2819 {
2820 if (!eap->did_sandbox)
2821 ++sandbox;
2822 eap->did_sandbox = TRUE;
2823 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002824#endif
2825 continue;
2826 }
2827 if (!checkforcmd(&eap->cmd, "silent", 3))
2828 break;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002829 if (!skip_only)
2830 {
2831 if (eap->save_msg_silent == -1)
2832 eap->save_msg_silent = msg_silent;
2833 ++msg_silent;
2834 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002835 if (*eap->cmd == '!' && !VIM_ISWHITE(eap->cmd[-1]))
2836 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002837 // ":silent!", but not "silent !cmd"
Bram Moolenaareffed932018-08-14 13:38:17 +02002838 eap->cmd = skipwhite(eap->cmd + 1);
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002839 if (!skip_only)
2840 {
2841 ++emsg_silent;
2842 ++eap->did_esilent;
2843 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002844 }
2845 continue;
2846
2847 case 't': if (checkforcmd(&p, "tab", 3))
2848 {
Bram Moolenaar548e5982018-12-26 21:45:00 +01002849 if (!skip_only)
Bram Moolenaareffed932018-08-14 13:38:17 +02002850 {
Bram Moolenaar548e5982018-12-26 21:45:00 +01002851 long tabnr = get_address(eap, &eap->cmd,
2852 ADDR_TABS, eap->skip,
2853 skip_only, FALSE, 1);
2854 if (tabnr == MAXLNUM)
2855 cmdmod.tab = tabpage_index(curtab) + 1;
2856 else
Bram Moolenaareffed932018-08-14 13:38:17 +02002857 {
Bram Moolenaar548e5982018-12-26 21:45:00 +01002858 if (tabnr < 0 || tabnr > LAST_TAB_NR)
2859 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002860 *errormsg = _(e_invrange);
Bram Moolenaar548e5982018-12-26 21:45:00 +01002861 return FAIL;
2862 }
2863 cmdmod.tab = tabnr + 1;
Bram Moolenaareffed932018-08-14 13:38:17 +02002864 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002865 }
2866 eap->cmd = p;
2867 continue;
2868 }
2869 if (!checkforcmd(&eap->cmd, "topleft", 2))
2870 break;
2871 cmdmod.split |= WSP_TOP;
2872 continue;
2873
2874 case 'u': if (!checkforcmd(&eap->cmd, "unsilent", 3))
2875 break;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002876 if (!skip_only)
2877 {
2878 if (eap->save_msg_silent == -1)
2879 eap->save_msg_silent = msg_silent;
2880 msg_silent = 0;
2881 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002882 continue;
2883
2884 case 'v': if (checkforcmd(&eap->cmd, "vertical", 4))
2885 {
2886 cmdmod.split |= WSP_VERT;
2887 continue;
2888 }
2889 if (!checkforcmd(&p, "verbose", 4))
2890 break;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002891 if (!skip_only)
2892 {
2893 if (eap->verbose_save < 0)
2894 eap->verbose_save = p_verbose;
2895 if (vim_isdigit(*eap->cmd))
2896 p_verbose = atoi((char *)eap->cmd);
2897 else
2898 p_verbose = 1;
2899 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002900 eap->cmd = p;
2901 continue;
2902 }
2903 break;
2904 }
2905
2906 return OK;
2907}
2908
2909/*
Bram Moolenaarb074e8b2020-07-11 13:40:45 +02002910 * Undo and free contents of "cmdmod".
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002911 */
Bram Moolenaar47e7d702020-07-05 18:18:42 +02002912 void
2913undo_cmdmod(exarg_T *eap, int save_msg_scroll)
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002914{
Bram Moolenaar47e7d702020-07-05 18:18:42 +02002915 if (eap->verbose_save >= 0)
2916 p_verbose = eap->verbose_save;
2917
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002918 if (cmdmod.save_ei != NULL)
2919 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002920 // Restore 'eventignore' to the value before ":noautocmd".
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002921 set_string_option_direct((char_u *)"ei", -1, cmdmod.save_ei,
2922 OPT_FREE, SID_NONE);
2923 free_string_option(cmdmod.save_ei);
2924 }
2925
2926 if (cmdmod.filter_regmatch.regprog != NULL)
2927 vim_regfree(cmdmod.filter_regmatch.regprog);
Bram Moolenaar47e7d702020-07-05 18:18:42 +02002928
2929 if (eap->save_msg_silent != -1)
2930 {
2931 // messages could be enabled for a serious error, need to check if the
2932 // counters don't become negative
2933 if (!did_emsg || msg_silent > eap->save_msg_silent)
2934 msg_silent = eap->save_msg_silent;
2935 emsg_silent -= eap->did_esilent;
2936 if (emsg_silent < 0)
2937 emsg_silent = 0;
2938 // Restore msg_scroll, it's set by file I/O commands, even when no
2939 // message is actually displayed.
2940 msg_scroll = save_msg_scroll;
2941
2942 // "silent reg" or "silent echo x" inside "redir" leaves msg_col
2943 // somewhere in the line. Put it back in the first column.
2944 if (redirecting())
2945 msg_col = 0;
2946 }
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002947}
2948
2949/*
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002950 * Parse the address range, if any, in "eap".
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02002951 * May set the last search pattern, unless "silent" is TRUE.
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002952 * Return FAIL and set "errormsg" or return OK.
2953 */
2954 int
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002955parse_cmd_address(exarg_T *eap, char **errormsg, int silent)
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002956{
2957 int address_count = 1;
2958 linenr_T lnum;
2959
2960 // Repeat for all ',' or ';' separated addresses.
2961 for (;;)
2962 {
2963 eap->line1 = eap->line2;
Bram Moolenaarc2af0af2020-08-23 21:06:02 +02002964 eap->line2 = default_address(eap);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002965 eap->cmd = skipwhite(eap->cmd);
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02002966 lnum = get_address(eap, &eap->cmd, eap->addr_type, eap->skip, silent,
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002967 eap->addr_count == 0, address_count++);
2968 if (eap->cmd == NULL) // error detected
2969 return FAIL;
2970 if (lnum == MAXLNUM)
2971 {
2972 if (*eap->cmd == '%') // '%' - all lines
2973 {
2974 ++eap->cmd;
2975 switch (eap->addr_type)
2976 {
2977 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02002978 case ADDR_OTHER:
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002979 eap->line1 = 1;
2980 eap->line2 = curbuf->b_ml.ml_line_count;
2981 break;
2982 case ADDR_LOADED_BUFFERS:
2983 {
2984 buf_T *buf = firstbuf;
2985
2986 while (buf->b_next != NULL
2987 && buf->b_ml.ml_mfp == NULL)
2988 buf = buf->b_next;
2989 eap->line1 = buf->b_fnum;
2990 buf = lastbuf;
2991 while (buf->b_prev != NULL
2992 && buf->b_ml.ml_mfp == NULL)
2993 buf = buf->b_prev;
2994 eap->line2 = buf->b_fnum;
2995 break;
2996 }
2997 case ADDR_BUFFERS:
2998 eap->line1 = firstbuf->b_fnum;
2999 eap->line2 = lastbuf->b_fnum;
3000 break;
3001 case ADDR_WINDOWS:
3002 case ADDR_TABS:
3003 if (IS_USER_CMDIDX(eap->cmdidx))
3004 {
3005 eap->line1 = 1;
3006 eap->line2 = eap->addr_type == ADDR_WINDOWS
3007 ? LAST_WIN_NR : LAST_TAB_NR;
3008 }
3009 else
3010 {
3011 // there is no Vim command which uses '%' and
3012 // ADDR_WINDOWS or ADDR_TABS
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003013 *errormsg = _(e_invrange);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003014 return FAIL;
3015 }
3016 break;
3017 case ADDR_TABS_RELATIVE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02003018 case ADDR_UNSIGNED:
3019 case ADDR_QUICKFIX:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003020 *errormsg = _(e_invrange);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003021 return FAIL;
3022 case ADDR_ARGUMENTS:
3023 if (ARGCOUNT == 0)
3024 eap->line1 = eap->line2 = 0;
3025 else
3026 {
3027 eap->line1 = 1;
3028 eap->line2 = ARGCOUNT;
3029 }
3030 break;
Bram Moolenaar25190db2019-05-04 15:05:28 +02003031 case ADDR_QUICKFIX_VALID:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003032#ifdef FEAT_QUICKFIX
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003033 eap->line1 = 1;
Bram Moolenaar25190db2019-05-04 15:05:28 +02003034 eap->line2 = qf_get_valid_size(eap);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003035 if (eap->line2 == 0)
3036 eap->line2 = 1;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003037#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003038 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02003039 case ADDR_NONE:
3040 // Will give an error later if a range is found.
3041 break;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003042 }
3043 ++eap->addr_count;
3044 }
3045 else if (*eap->cmd == '*' && vim_strchr(p_cpo, CPO_STAR) == NULL)
3046 {
3047 pos_T *fp;
3048
3049 // '*' - visual area
3050 if (eap->addr_type != ADDR_LINES)
3051 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003052 *errormsg = _(e_invrange);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003053 return FAIL;
3054 }
3055
3056 ++eap->cmd;
3057 if (!eap->skip)
3058 {
3059 fp = getmark('<', FALSE);
3060 if (check_mark(fp) == FAIL)
3061 return FAIL;
3062 eap->line1 = fp->lnum;
3063 fp = getmark('>', FALSE);
3064 if (check_mark(fp) == FAIL)
3065 return FAIL;
3066 eap->line2 = fp->lnum;
3067 ++eap->addr_count;
3068 }
3069 }
3070 }
3071 else
3072 eap->line2 = lnum;
3073 eap->addr_count++;
3074
3075 if (*eap->cmd == ';')
3076 {
3077 if (!eap->skip)
3078 {
3079 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaar0e717042020-04-27 19:29:01 +02003080 // Don't leave the cursor on an illegal line or column, but do
3081 // accept zero as address, so 0;/PATTERN/ works correctly.
3082 if (eap->line2 > 0)
3083 check_cursor();
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003084 }
3085 }
3086 else if (*eap->cmd != ',')
3087 break;
3088 ++eap->cmd;
3089 }
3090
3091 // One address given: set start and end lines.
3092 if (eap->addr_count == 1)
3093 {
3094 eap->line1 = eap->line2;
3095 // ... but only implicit: really no address given
3096 if (lnum == MAXLNUM)
3097 eap->addr_count = 0;
3098 }
3099 return OK;
3100}
3101
3102/*
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00003103 * Check for an Ex command with optional tail.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003104 * If there is a match advance "pp" to the argument and return TRUE.
3105 */
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00003106 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003107checkforcmd(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003108 char_u **pp, // start of command
3109 char *cmd, // name of command
3110 int len) // required length
Bram Moolenaar071d4272004-06-13 20:20:40 +00003111{
3112 int i;
3113
3114 for (i = 0; cmd[i] != NUL; ++i)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003115 if (((char_u *)cmd)[i] != (*pp)[i])
Bram Moolenaar071d4272004-06-13 20:20:40 +00003116 break;
3117 if (i >= len && !isalpha((*pp)[i]))
3118 {
3119 *pp = skipwhite(*pp + i);
3120 return TRUE;
3121 }
3122 return FALSE;
3123}
3124
3125/*
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003126 * Append "cmd" to the error message in IObuff.
3127 * Takes care of limiting the length and handling 0xa0, which would be
3128 * invisible otherwise.
3129 */
3130 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003131append_command(char_u *cmd)
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003132{
3133 char_u *s = cmd;
3134 char_u *d;
3135
3136 STRCAT(IObuff, ": ");
3137 d = IObuff + STRLEN(IObuff);
3138 while (*s != NUL && d - IObuff < IOSIZE - 7)
3139 {
Bram Moolenaar13505972019-01-24 15:04:48 +01003140 if (enc_utf8 ? (s[0] == 0xc2 && s[1] == 0xa0) : *s == 0xa0)
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003141 {
Bram Moolenaar13505972019-01-24 15:04:48 +01003142 s += enc_utf8 ? 2 : 1;
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003143 STRCPY(d, "<a0>");
3144 d += 4;
3145 }
3146 else
3147 MB_COPY_CHAR(s, d);
3148 }
3149 *d = NUL;
3150}
3151
3152/*
Bram Moolenaar2e800952020-08-23 19:34:48 +02003153 * If "start" points "&opt", "&l:opt", "&g:opt" or "$ENV" return a pointer to
3154 * the name. Otherwise just return "start".
3155 */
3156 char_u *
3157skip_option_env_lead(char_u *start)
3158{
3159 char_u *name = start;
3160
3161 if (*start == '&')
3162 {
3163 if ((start[1] == 'l' || start[1] == 'g') && start[2] == ':')
3164 name += 3;
3165 else
3166 name += 1;
3167 }
3168 else if (*start == '$')
3169 name += 1;
3170 return name;
3171}
3172
3173/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003174 * Find an Ex command by its name, either built-in or user.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003175 * Start of the name can be found at eap->cmd.
Bram Moolenaar25190db2019-05-04 15:05:28 +02003176 * Sets eap->cmdidx and returns a pointer to char after the command name.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003177 * "full" is set to TRUE if the whole command name matched.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003178 *
3179 * If "lookup" is not NULL recognize expression without "eval" or "call" and
3180 * assignment without "let". Sets eap->cmdidx to the command while returning
3181 * "eap->cmd".
3182 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00003183 * Returns NULL for an ambiguous user command.
3184 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003185 char_u *
3186find_ex_command(
3187 exarg_T *eap,
Bram Moolenaara494f562020-04-18 17:45:38 +02003188 int *full UNUSED,
Bram Moolenaarb84a3812020-05-01 15:44:29 +02003189 void *(*lookup)(char_u *, size_t, cctx_T *) UNUSED,
Bram Moolenaara494f562020-04-18 17:45:38 +02003190 cctx_T *cctx UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003191{
3192 int len;
3193 char_u *p;
Bram Moolenaardf177f62005-02-22 08:39:57 +00003194 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003195
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003196#ifdef FEAT_EVAL
3197 /*
3198 * Recognize a Vim9 script function/method call and assignment:
3199 * "lvar = value", "lvar(arg)", "[1, 2 3]->Func()"
3200 */
Bram Moolenaar0c6ceaf2020-02-22 18:36:32 +01003201 p = eap->cmd;
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003202 if (lookup != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003203 {
Bram Moolenaar2e800952020-08-23 19:34:48 +02003204 char_u *pskip = skip_option_env_lead(eap->cmd);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003205
Bram Moolenaar83144542020-08-02 20:40:43 +02003206 if (vim_strchr((char_u *)"{('[\"@", *p) != NULL
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003207 || ((p = to_name_const_end(pskip)) > eap->cmd && *p != NUL))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003208 {
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003209 int oplen;
3210 int heredoc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003211
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003212 if (
3213 // "(..." is an expression.
3214 // "funcname(" is always a function call.
3215 *p == '('
3216 || (p == eap->cmd
3217 ? (
3218 // "{..." is an dict expression.
3219 *eap->cmd == '{'
3220 // "'string'->func()" is an expression.
3221 || *eap->cmd == '\''
Bram Moolenaar10409562020-07-29 20:00:38 +02003222 // '"string"->func()' is an expression.
3223 || *eap->cmd == '"'
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003224 // "g:varname" is an expression.
3225 || eap->cmd[1] == ':'
3226 )
3227 : (
3228 // "varname[]" is an expression.
3229 *p == '['
3230 // "varname->func()" is an expression.
3231 || (*p == '-' && p[1] == '>')
3232 // "varname.expr" is an expression.
3233 || (*p == '.' && ASCII_ISALPHA(p[1]))
3234 )))
Bram Moolenaarb31be3f2020-07-20 22:37:44 +02003235 {
3236 eap->cmdidx = CMD_eval;
3237 return eap->cmd;
3238 }
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003239
3240 // "[...]->Method()" is a list expression, but "[a, b] = Func()" is
3241 // an assignment.
3242 // If there is no line break inside the "[...]" then "p" is
3243 // advanced to after the "]" by to_name_const_end(): check if a "="
3244 // follows.
3245 // If "[...]" has a line break "p" still points at the "[" and it
3246 // can't be an assignment.
3247 if (*eap->cmd == '[')
Bram Moolenaarb31be3f2020-07-20 22:37:44 +02003248 {
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003249 p = to_name_const_end(eap->cmd);
3250 if (p == eap->cmd || *skipwhite(p) != '=')
3251 {
3252 eap->cmdidx = CMD_eval;
3253 return eap->cmd;
3254 }
3255 if (p > eap->cmd && *skipwhite(p) == '=')
3256 {
3257 eap->cmdidx = CMD_let;
3258 return eap->cmd;
3259 }
3260 }
3261
3262 // Recognize an assignment if we recognize the variable name:
3263 // "g:var = expr"
3264 // "var = expr" where "var" is a local var name.
Bram Moolenaar83144542020-08-02 20:40:43 +02003265 if (*eap->cmd == '@')
3266 p = eap->cmd + 2;
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003267 oplen = assignment_len(skipwhite(p), &heredoc);
3268 if (oplen > 0)
3269 {
3270 if (((p - eap->cmd) > 2 && eap->cmd[1] == ':')
3271 || *eap->cmd == '&'
3272 || *eap->cmd == '$'
3273 || *eap->cmd == '@'
3274 || lookup(eap->cmd, p - eap->cmd, cctx) != NULL)
3275 {
3276 eap->cmdidx = CMD_let;
3277 return eap->cmd;
3278 }
3279 }
3280
3281 // Recognize using a type for a w:, b:, t: or g: variable:
3282 // "w:varname: number = 123".
3283 if (eap->cmd[1] == ':' && *p == ':')
3284 {
3285 eap->cmdidx = CMD_eval;
Bram Moolenaarb31be3f2020-07-20 22:37:44 +02003286 return eap->cmd;
3287 }
Bram Moolenaard2ef6b32020-07-02 21:11:34 +02003288 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003289 }
3290#endif
3291
Bram Moolenaar071d4272004-06-13 20:20:40 +00003292 /*
3293 * Isolate the command and search for it in the command table.
Bram Moolenaar81870892007-11-11 18:17:28 +00003294 * Exceptions:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003295 * - the 'k' command can directly be followed by any character.
3296 * - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003297 * but :sre[wind] is another command, as are :scr[iptnames],
Bram Moolenaar071d4272004-06-13 20:20:40 +00003298 * :scs[cope], :sim[alt], :sig[ns] and :sil[ent].
Bram Moolenaardf177f62005-02-22 08:39:57 +00003299 * - the "d" command can directly be followed by 'l' or 'p' flag.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003300 */
3301 p = eap->cmd;
3302 if (*p == 'k')
3303 {
3304 eap->cmdidx = CMD_k;
3305 ++p;
3306 }
3307 else if (p[0] == 's'
Bram Moolenaar204b93f2015-08-04 22:02:51 +02003308 && ((p[1] == 'c' && (p[2] == NUL || (p[2] != 's' && p[2] != 'r'
3309 && (p[3] == NUL || (p[3] != 'i' && p[4] != 'p')))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003310 || p[1] == 'g'
3311 || (p[1] == 'i' && p[2] != 'm' && p[2] != 'l' && p[2] != 'g')
3312 || p[1] == 'I'
3313 || (p[1] == 'r' && p[2] != 'e')))
3314 {
3315 eap->cmdidx = CMD_substitute;
3316 ++p;
3317 }
3318 else
3319 {
3320 while (ASCII_ISALPHA(*p))
3321 ++p;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003322 // for python 3.x support ":py3", ":python3", ":py3file", etc.
Bram Moolenaar55d5c032010-07-17 23:52:29 +02003323 if (eap->cmd[0] == 'p' && eap->cmd[1] == 'y')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003324 {
Bram Moolenaarb6590522010-07-21 16:00:43 +02003325 while (ASCII_ISALNUM(*p))
3326 ++p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003327 }
3328 else if (*p == '9' && STRNCMP("vim9", eap->cmd, 4) == 0)
3329 {
3330 // include "9" for "vim9script"
3331 ++p;
3332 while (ASCII_ISALPHA(*p))
3333 ++p;
3334 }
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02003335
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003336 // check for non-alpha command
Bram Moolenaar071d4272004-06-13 20:20:40 +00003337 if (p == eap->cmd && vim_strchr((char_u *)"@*!=><&~#", *p) != NULL)
3338 ++p;
3339 len = (int)(p - eap->cmd);
Bram Moolenaardf177f62005-02-22 08:39:57 +00003340 if (*eap->cmd == 'd' && (p[-1] == 'l' || p[-1] == 'p'))
3341 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003342 // Check for ":dl", ":dell", etc. to ":deletel": that's
3343 // :delete with the 'l' flag. Same for 'p'.
Bram Moolenaardf177f62005-02-22 08:39:57 +00003344 for (i = 0; i < len; ++i)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003345 if (eap->cmd[i] != ((char_u *)"delete")[i])
Bram Moolenaardf177f62005-02-22 08:39:57 +00003346 break;
3347 if (i == len - 1)
3348 {
3349 --len;
3350 if (p[-1] == 'l')
3351 eap->flags |= EXFLAG_LIST;
3352 else
3353 eap->flags |= EXFLAG_PRINT;
3354 }
3355 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003356
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003357 if (ASCII_ISLOWER(eap->cmd[0]))
3358 {
Bram Moolenaar6c0c1e82017-03-25 15:07:43 +01003359 int c1 = eap->cmd[0];
Bram Moolenaar94f82cb2019-07-24 22:30:27 +02003360 int c2 = len == 1 ? NUL : eap->cmd[1];
Bram Moolenaar6c0c1e82017-03-25 15:07:43 +01003361
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003362 if (command_count != (int)CMD_SIZE)
3363 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003364 iemsg(_("E943: Command table needs to be updated, run 'make cmdidxs'"));
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003365 getout(1);
3366 }
3367
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003368 // Use a precomputed index for fast look-up in cmdnames[]
3369 // taking into account the first 2 letters of eap->cmd.
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003370 eap->cmdidx = cmdidxs1[CharOrdLow(c1)];
3371 if (ASCII_ISLOWER(c2))
3372 eap->cmdidx += cmdidxs2[CharOrdLow(c1)][CharOrdLow(c2)];
3373 }
Bram Moolenaara494f562020-04-18 17:45:38 +02003374 else if (ASCII_ISUPPER(eap->cmd[0]))
3375 eap->cmdidx = CMD_Next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003376 else
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003377 eap->cmdidx = CMD_bang;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003378
3379 for ( ; (int)eap->cmdidx < (int)CMD_SIZE;
3380 eap->cmdidx = (cmdidx_T)((int)eap->cmdidx + 1))
3381 if (STRNCMP(cmdnames[(int)eap->cmdidx].cmd_name, (char *)eap->cmd,
3382 (size_t)len) == 0)
3383 {
3384#ifdef FEAT_EVAL
3385 if (full != NULL
3386 && cmdnames[(int)eap->cmdidx].cmd_name[len] == NUL)
3387 *full = TRUE;
3388#endif
3389 break;
3390 }
3391
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003392 // Look for a user defined command as a last resort. Let ":Print" be
3393 // overruled by a user defined command.
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01003394 if ((eap->cmdidx == CMD_SIZE || eap->cmdidx == CMD_Print)
3395 && *eap->cmd >= 'A' && *eap->cmd <= 'Z')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003396 {
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003397 // User defined commands may contain digits.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003398 while (ASCII_ISALNUM(*p))
3399 ++p;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003400 p = find_ucmd(eap, p, full, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003401 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003402 if (p == eap->cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003403 eap->cmdidx = CMD_SIZE;
3404 }
3405
3406 return p;
3407}
3408
3409#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003410static struct cmdmod
3411{
3412 char *name;
3413 int minlen;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003414 int has_count; // :123verbose :3tab
Bram Moolenaared53fb92007-11-24 20:50:24 +00003415} cmdmods[] = {
3416 {"aboveleft", 3, FALSE},
3417 {"belowright", 3, FALSE},
3418 {"botright", 2, FALSE},
3419 {"browse", 3, FALSE},
3420 {"confirm", 4, FALSE},
Bram Moolenaar7b668e82016-08-23 23:51:21 +02003421 {"filter", 4, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003422 {"hide", 3, FALSE},
3423 {"keepalt", 5, FALSE},
3424 {"keepjumps", 5, FALSE},
3425 {"keepmarks", 3, FALSE},
Bram Moolenaara939e432013-11-09 05:30:26 +01003426 {"keeppatterns", 5, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003427 {"leftabove", 5, FALSE},
3428 {"lockmarks", 3, FALSE},
Bram Moolenaarca9f9582008-09-18 10:44:28 +00003429 {"noautocmd", 3, FALSE},
Bram Moolenaar5803ae62014-03-23 16:04:02 +01003430 {"noswapfile", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003431 {"rightbelow", 6, FALSE},
3432 {"sandbox", 3, FALSE},
3433 {"silent", 3, FALSE},
3434 {"tab", 3, TRUE},
3435 {"topleft", 2, FALSE},
Bram Moolenaar8e258a42009-07-09 13:55:43 +00003436 {"unsilent", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003437 {"verbose", 4, TRUE},
3438 {"vertical", 4, FALSE},
3439};
3440
3441/*
3442 * Return length of a command modifier (including optional count).
3443 * Return zero when it's not a modifier.
3444 */
3445 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003446modifier_len(char_u *cmd)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003447{
3448 int i, j;
3449 char_u *p = cmd;
3450
3451 if (VIM_ISDIGIT(*cmd))
3452 p = skipwhite(skipdigits(cmd));
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00003453 for (i = 0; i < (int)(sizeof(cmdmods) / sizeof(struct cmdmod)); ++i)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003454 {
3455 for (j = 0; p[j] != NUL; ++j)
3456 if (p[j] != cmdmods[i].name[j])
3457 break;
Bram Moolenaar2d473ab2013-06-12 17:12:24 +02003458 if (!ASCII_ISALPHA(p[j]) && j >= cmdmods[i].minlen
Bram Moolenaared53fb92007-11-24 20:50:24 +00003459 && (p == cmd || cmdmods[i].has_count))
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00003460 return j + (int)(p - cmd);
Bram Moolenaared53fb92007-11-24 20:50:24 +00003461 }
3462 return 0;
3463}
3464
Bram Moolenaar071d4272004-06-13 20:20:40 +00003465/*
3466 * Return > 0 if an Ex command "name" exists.
3467 * Return 2 if there is an exact match.
3468 * Return 3 if there is an ambiguous match.
3469 */
3470 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003471cmd_exists(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003472{
3473 exarg_T ea;
3474 int full = FALSE;
3475 int i;
3476 int j;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003477 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003478
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003479 // Check command modifiers.
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00003480 for (i = 0; i < (int)(sizeof(cmdmods) / sizeof(struct cmdmod)); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003481 {
3482 for (j = 0; name[j] != NUL; ++j)
3483 if (name[j] != cmdmods[i].name[j])
3484 break;
3485 if (name[j] == NUL && j >= cmdmods[i].minlen)
3486 return (cmdmods[i].name[j] == NUL ? 2 : 1);
3487 }
3488
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003489 // Check built-in commands and user defined commands.
3490 // For ":2match" and ":3match" we need to skip the number.
Bram Moolenaara9587612006-05-04 21:47:50 +00003491 ea.cmd = (*name == '2' || *name == '3') ? name + 1 : name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003492 ea.cmdidx = (cmdidx_T)0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003493 p = find_ex_command(&ea, &full, NULL, NULL);
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003494 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003495 return 3;
Bram Moolenaara9587612006-05-04 21:47:50 +00003496 if (vim_isdigit(*name) && ea.cmdidx != CMD_match)
3497 return 0;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003498 if (*skipwhite(p) != NUL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003499 return 0; // trailing garbage
Bram Moolenaar071d4272004-06-13 20:20:40 +00003500 return (ea.cmdidx == CMD_SIZE ? 0 : (full ? 2 : 1));
3501}
3502#endif
3503
Bram Moolenaard0190392019-08-23 21:17:35 +02003504 cmdidx_T
3505excmd_get_cmdidx(char_u *cmd, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003506{
Bram Moolenaard0190392019-08-23 21:17:35 +02003507 cmdidx_T idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003508
Bram Moolenaard0190392019-08-23 21:17:35 +02003509 for (idx = (cmdidx_T)0; (int)idx < (int)CMD_SIZE;
3510 idx = (cmdidx_T)((int)idx + 1))
3511 if (STRNCMP(cmdnames[(int)idx].cmd_name, cmd, (size_t)len) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003512 break;
3513
Bram Moolenaard0190392019-08-23 21:17:35 +02003514 return idx;
3515}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003516
Bram Moolenaard0190392019-08-23 21:17:35 +02003517 long
3518excmd_get_argt(cmdidx_T idx)
3519{
3520 return (long)cmdnames[(int)idx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003521}
3522
3523/*
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003524 * Skip a range specifier of the form: addr [,addr] [;addr] ..
Bram Moolenaar071d4272004-06-13 20:20:40 +00003525 *
3526 * Backslashed delimiters after / or ? will be skipped, and commands will
3527 * not be expanded between /'s and ?'s or after "'".
3528 *
Bram Moolenaardf069ee2020-06-22 23:02:51 +02003529 * Also skip white space and ":" characters after the range.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003530 * Returns the "cmd" pointer advanced to beyond the range.
3531 */
3532 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003533skip_range(
3534 char_u *cmd,
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02003535 int skip_star, // skip "*" used for Visual range
3536 int *ctx) // pointer to xp_context or NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003537{
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003538 unsigned delim;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003539
Bram Moolenaarcbf20fb2017-02-03 21:19:04 +01003540 while (vim_strchr((char_u *)" \t0123456789.$%'/?-+,;\\", *cmd) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003541 {
Bram Moolenaarcbf20fb2017-02-03 21:19:04 +01003542 if (*cmd == '\\')
3543 {
3544 if (cmd[1] == '?' || cmd[1] == '/' || cmd[1] == '&')
3545 ++cmd;
3546 else
3547 break;
3548 }
3549 else if (*cmd == '\'')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003550 {
3551 if (*++cmd == NUL && ctx != NULL)
3552 *ctx = EXPAND_NOTHING;
3553 }
3554 else if (*cmd == '/' || *cmd == '?')
3555 {
3556 delim = *cmd++;
3557 while (*cmd != NUL && *cmd != delim)
3558 if (*cmd++ == '\\' && *cmd != NUL)
3559 ++cmd;
3560 if (*cmd == NUL && ctx != NULL)
3561 *ctx = EXPAND_NOTHING;
3562 }
3563 if (*cmd != NUL)
3564 ++cmd;
3565 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00003566
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003567 // Skip ":" and white space.
Bram Moolenaardf177f62005-02-22 08:39:57 +00003568 while (*cmd == ':')
3569 cmd = skipwhite(cmd + 1);
3570
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02003571 // Skip "*" used for Visual range.
3572 if (skip_star && *cmd == '*' && vim_strchr(p_cpo, CPO_STAR) == NULL)
3573 cmd = skipwhite(cmd + 1);
3574
Bram Moolenaar071d4272004-06-13 20:20:40 +00003575 return cmd;
3576}
3577
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003578 static void
3579addr_error(cmd_addr_T addr_type)
3580{
3581 if (addr_type == ADDR_NONE)
3582 emsg(_(e_norange));
3583 else
3584 emsg(_(e_invrange));
3585}
3586
Bram Moolenaar071d4272004-06-13 20:20:40 +00003587/*
Bram Moolenaarc2af0af2020-08-23 21:06:02 +02003588 * Return the default address for an address type.
3589 */
3590 static linenr_T
3591default_address(exarg_T *eap)
3592{
3593 linenr_T lnum = 0;
3594
3595 switch (eap->addr_type)
3596 {
3597 case ADDR_LINES:
3598 case ADDR_OTHER:
3599 // Default is the cursor line number. Avoid using an invalid
3600 // line number though.
3601 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
3602 lnum = curbuf->b_ml.ml_line_count;
3603 else
3604 lnum = curwin->w_cursor.lnum;
3605 break;
3606 case ADDR_WINDOWS:
3607 lnum = CURRENT_WIN_NR;
3608 break;
3609 case ADDR_ARGUMENTS:
3610 lnum = curwin->w_arg_idx + 1;
3611 if (lnum > ARGCOUNT)
3612 lnum = ARGCOUNT;
3613 break;
3614 case ADDR_LOADED_BUFFERS:
3615 case ADDR_BUFFERS:
3616 lnum = curbuf->b_fnum;
3617 break;
3618 case ADDR_TABS:
3619 lnum = CURRENT_TAB_NR;
3620 break;
3621 case ADDR_TABS_RELATIVE:
3622 case ADDR_UNSIGNED:
3623 lnum = 1;
3624 break;
3625 case ADDR_QUICKFIX:
3626#ifdef FEAT_QUICKFIX
3627 lnum = qf_get_cur_idx(eap);
3628#endif
3629 break;
3630 case ADDR_QUICKFIX_VALID:
3631#ifdef FEAT_QUICKFIX
3632 lnum = qf_get_cur_valid_idx(eap);
3633#endif
3634 break;
3635 case ADDR_NONE:
3636 // Will give an error later if a range is found.
3637 break;
3638 }
3639 return lnum;
3640}
3641
3642/*
Bram Moolenaar198cb662018-09-06 21:44:17 +02003643 * Get a single EX address.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003644 *
3645 * Set ptr to the next character after the part that was interpreted.
3646 * Set ptr to NULL when an error is encountered.
Bram Moolenaar198cb662018-09-06 21:44:17 +02003647 * This may set the last used search pattern.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003648 *
3649 * Return MAXLNUM when no Ex address was found.
3650 */
3651 static linenr_T
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003652get_address(
3653 exarg_T *eap UNUSED,
3654 char_u **ptr,
Bram Moolenaar1b03a192019-12-08 17:08:29 +01003655 cmd_addr_T addr_type,
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02003656 int skip, // only skip the address, don't use it
3657 int silent, // no errors or side effects
3658 int to_other_file, // flag: may jump to other file
3659 int address_count UNUSED) // 1 for first address, >1 after comma
Bram Moolenaar071d4272004-06-13 20:20:40 +00003660{
3661 int c;
3662 int i;
3663 long n;
3664 char_u *cmd;
3665 pos_T pos;
3666 pos_T *fp;
3667 linenr_T lnum;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003668 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003669
3670 cmd = skipwhite(*ptr);
3671 lnum = MAXLNUM;
3672 do
3673 {
3674 switch (*cmd)
3675 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003676 case '.': // '.' - Cursor position
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003677 ++cmd;
3678 switch (addr_type)
3679 {
3680 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02003681 case ADDR_OTHER:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003682 lnum = curwin->w_cursor.lnum;
3683 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003684 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01003685 lnum = CURRENT_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003686 break;
3687 case ADDR_ARGUMENTS:
3688 lnum = curwin->w_arg_idx + 1;
3689 break;
3690 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003691 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003692 lnum = curbuf->b_fnum;
3693 break;
3694 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01003695 lnum = CURRENT_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003696 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02003697 case ADDR_NONE:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003698 case ADDR_TABS_RELATIVE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02003699 case ADDR_UNSIGNED:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003700 addr_error(addr_type);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01003701 cmd = NULL;
3702 goto error;
3703 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003704 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003705#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02003706 lnum = qf_get_cur_idx(eap);
3707#endif
3708 break;
3709 case ADDR_QUICKFIX_VALID:
3710#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003711 lnum = qf_get_cur_valid_idx(eap);
Bram Moolenaare906c502015-09-09 21:10:39 +02003712#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003713 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003714 }
3715 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003716
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003717 case '$': // '$' - last line
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003718 ++cmd;
3719 switch (addr_type)
3720 {
3721 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02003722 case ADDR_OTHER:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003723 lnum = curbuf->b_ml.ml_line_count;
3724 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003725 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01003726 lnum = LAST_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003727 break;
3728 case ADDR_ARGUMENTS:
3729 lnum = ARGCOUNT;
3730 break;
3731 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003732 buf = lastbuf;
3733 while (buf->b_ml.ml_mfp == NULL)
3734 {
3735 if (buf->b_prev == NULL)
3736 break;
3737 buf = buf->b_prev;
3738 }
3739 lnum = buf->b_fnum;
3740 break;
3741 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003742 lnum = lastbuf->b_fnum;
3743 break;
3744 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01003745 lnum = LAST_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003746 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02003747 case ADDR_NONE:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003748 case ADDR_TABS_RELATIVE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02003749 case ADDR_UNSIGNED:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003750 addr_error(addr_type);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01003751 cmd = NULL;
3752 goto error;
3753 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003754 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003755#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003756 lnum = qf_get_size(eap);
3757 if (lnum == 0)
3758 lnum = 1;
Bram Moolenaare906c502015-09-09 21:10:39 +02003759#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003760 break;
Bram Moolenaar25190db2019-05-04 15:05:28 +02003761 case ADDR_QUICKFIX_VALID:
3762#ifdef FEAT_QUICKFIX
3763 lnum = qf_get_valid_size(eap);
3764 if (lnum == 0)
3765 lnum = 1;
3766#endif
3767 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003768 }
3769 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003770
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003771 case '\'': // ''' - mark
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003772 if (*++cmd == NUL)
3773 {
3774 cmd = NULL;
3775 goto error;
3776 }
3777 if (addr_type != ADDR_LINES)
3778 {
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003779 addr_error(addr_type);
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01003780 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003781 goto error;
3782 }
3783 if (skip)
3784 ++cmd;
3785 else
3786 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003787 // Only accept a mark in another file when it is
3788 // used by itself: ":'M".
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003789 fp = getmark(*cmd, to_other_file && cmd[1] == NUL);
3790 ++cmd;
3791 if (fp == (pos_T *)-1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003792 // Jumped to another file.
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003793 lnum = curwin->w_cursor.lnum;
3794 else
3795 {
3796 if (check_mark(fp) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003797 {
3798 cmd = NULL;
3799 goto error;
3800 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003801 lnum = fp->lnum;
3802 }
3803 }
3804 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003805
3806 case '/':
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003807 case '?': // '/' or '?' - search
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003808 c = *cmd++;
3809 if (addr_type != ADDR_LINES)
3810 {
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003811 addr_error(addr_type);
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01003812 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003813 goto error;
3814 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003815 if (skip) // skip "/pat/"
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003816 {
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02003817 cmd = skip_regexp(cmd, c, (int)p_magic);
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003818 if (*cmd == c)
3819 ++cmd;
3820 }
3821 else
3822 {
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02003823 int flags;
3824
3825 pos = curwin->w_cursor; // save curwin->w_cursor
3826
3827 // When '/' or '?' follows another address, start from
3828 // there.
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003829 if (lnum != MAXLNUM)
3830 curwin->w_cursor.lnum = lnum;
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02003831
3832 // Start a forward search at the end of the line (unless
3833 // before the first line).
3834 // Start a backward search at the start of the line.
3835 // This makes sure we never match in the current
3836 // line, and can match anywhere in the
3837 // next/previous line.
Bram Moolenaar8ada6aa2017-12-19 21:23:21 +01003838 if (c == '/' && curwin->w_cursor.lnum > 0)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003839 curwin->w_cursor.col = MAXCOL;
3840 else
3841 curwin->w_cursor.col = 0;
3842 searchcmdlen = 0;
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02003843 flags = silent ? 0 : SEARCH_HIS | SEARCH_MSG;
Bram Moolenaarc036e872020-02-21 21:30:52 +01003844 if (!do_search(NULL, c, c, cmd, 1L, flags, NULL))
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003845 {
3846 curwin->w_cursor = pos;
3847 cmd = NULL;
3848 goto error;
3849 }
3850 lnum = curwin->w_cursor.lnum;
3851 curwin->w_cursor = pos;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003852 // adjust command string pointer
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003853 cmd += searchcmdlen;
3854 }
3855 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003856
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003857 case '\\': // "\?", "\/" or "\&", repeat search
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003858 ++cmd;
3859 if (addr_type != ADDR_LINES)
3860 {
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003861 addr_error(addr_type);
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01003862 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003863 goto error;
3864 }
3865 if (*cmd == '&')
3866 i = RE_SUBST;
3867 else if (*cmd == '?' || *cmd == '/')
3868 i = RE_SEARCH;
3869 else
3870 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003871 emsg(_(e_backslash));
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003872 cmd = NULL;
3873 goto error;
3874 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003875
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003876 if (!skip)
3877 {
3878 /*
3879 * When search follows another address, start from
3880 * there.
3881 */
3882 if (lnum != MAXLNUM)
3883 pos.lnum = lnum;
3884 else
3885 pos.lnum = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003886
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003887 /*
3888 * Start the search just like for the above
3889 * do_search().
3890 */
3891 if (*cmd != '?')
3892 pos.col = MAXCOL;
3893 else
3894 pos.col = 0;
Bram Moolenaarbd8539a2015-08-11 18:53:03 +02003895 pos.coladd = 0;
Bram Moolenaar5d24a222018-12-23 19:10:09 +01003896 if (searchit(curwin, curbuf, &pos, NULL,
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003897 *cmd == '?' ? BACKWARD : FORWARD,
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02003898 (char_u *)"", 1L, SEARCH_MSG, i, NULL) != FAIL)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003899 lnum = pos.lnum;
3900 else
3901 {
3902 cmd = NULL;
3903 goto error;
3904 }
3905 }
3906 ++cmd;
3907 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003908
3909 default:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003910 if (VIM_ISDIGIT(*cmd)) // absolute line number
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003911 lnum = getdigits(&cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003912 }
3913
3914 for (;;)
3915 {
3916 cmd = skipwhite(cmd);
3917 if (*cmd != '-' && *cmd != '+' && !VIM_ISDIGIT(*cmd))
3918 break;
3919
3920 if (lnum == MAXLNUM)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003921 {
3922 switch (addr_type)
3923 {
3924 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02003925 case ADDR_OTHER:
3926 // "+1" is same as ".+1"
Bram Moolenaarf240e182014-11-27 18:33:02 +01003927 lnum = curwin->w_cursor.lnum;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003928 break;
3929 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01003930 lnum = CURRENT_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003931 break;
3932 case ADDR_ARGUMENTS:
3933 lnum = curwin->w_arg_idx + 1;
3934 break;
3935 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003936 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003937 lnum = curbuf->b_fnum;
3938 break;
3939 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01003940 lnum = CURRENT_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003941 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01003942 case ADDR_TABS_RELATIVE:
3943 lnum = 1;
3944 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003945 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003946#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02003947 lnum = qf_get_cur_idx(eap);
3948#endif
3949 break;
3950 case ADDR_QUICKFIX_VALID:
3951#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003952 lnum = qf_get_cur_valid_idx(eap);
Bram Moolenaare906c502015-09-09 21:10:39 +02003953#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003954 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02003955 case ADDR_NONE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02003956 case ADDR_UNSIGNED:
3957 lnum = 0;
Bram Moolenaarb7316892019-05-01 18:08:42 +02003958 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003959 }
3960 }
3961
Bram Moolenaar071d4272004-06-13 20:20:40 +00003962 if (VIM_ISDIGIT(*cmd))
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003963 i = '+'; // "number" is same as "+number"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003964 else
3965 i = *cmd++;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003966 if (!VIM_ISDIGIT(*cmd)) // '+' is '+1', but '+0' is not '+1'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003967 n = 1;
3968 else
3969 n = getdigits(&cmd);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01003970
3971 if (addr_type == ADDR_TABS_RELATIVE)
3972 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003973 emsg(_(e_invrange));
Bram Moolenaar2f72c702017-01-29 14:48:10 +01003974 cmd = NULL;
3975 goto error;
3976 }
3977 else if (addr_type == ADDR_LOADED_BUFFERS
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003978 || addr_type == ADDR_BUFFERS)
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01003979 lnum = compute_buffer_local_count(
3980 addr_type, lnum, (i == '-') ? -1 * n : n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003981 else
Bram Moolenaarded27822017-01-02 14:27:34 +01003982 {
3983#ifdef FEAT_FOLDING
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003984 // Relative line addressing, need to adjust for folded lines
3985 // now, but only do it after the first address.
Bram Moolenaarded27822017-01-02 14:27:34 +01003986 if (addr_type == ADDR_LINES && (i == '-' || i == '+')
3987 && address_count >= 2)
3988 (void)hasFolding(lnum, NULL, &lnum);
3989#endif
3990 if (i == '-')
3991 lnum -= n;
3992 else
3993 lnum += n;
3994 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003995 }
3996 } while (*cmd == '/' || *cmd == '?');
3997
3998error:
3999 *ptr = cmd;
4000 return lnum;
4001}
4002
4003/*
Bram Moolenaarc2af0af2020-08-23 21:06:02 +02004004 * Set eap->line1 and eap->line2 to the whole range.
4005 * Used for commands with the EX_DFLALL flag and no range given.
4006 */
4007 static void
4008address_default_all(exarg_T *eap)
4009{
4010 eap->line1 = 1;
4011 switch (eap->addr_type)
4012 {
4013 case ADDR_LINES:
4014 case ADDR_OTHER:
4015 eap->line2 = curbuf->b_ml.ml_line_count;
4016 break;
4017 case ADDR_LOADED_BUFFERS:
4018 {
4019 buf_T *buf = firstbuf;
4020
4021 while (buf->b_next != NULL && buf->b_ml.ml_mfp == NULL)
4022 buf = buf->b_next;
4023 eap->line1 = buf->b_fnum;
4024 buf = lastbuf;
4025 while (buf->b_prev != NULL && buf->b_ml.ml_mfp == NULL)
4026 buf = buf->b_prev;
4027 eap->line2 = buf->b_fnum;
4028 }
4029 break;
4030 case ADDR_BUFFERS:
4031 eap->line1 = firstbuf->b_fnum;
4032 eap->line2 = lastbuf->b_fnum;
4033 break;
4034 case ADDR_WINDOWS:
4035 eap->line2 = LAST_WIN_NR;
4036 break;
4037 case ADDR_TABS:
4038 eap->line2 = LAST_TAB_NR;
4039 break;
4040 case ADDR_TABS_RELATIVE:
4041 eap->line2 = 1;
4042 break;
4043 case ADDR_ARGUMENTS:
4044 if (ARGCOUNT == 0)
4045 eap->line1 = eap->line2 = 0;
4046 else
4047 eap->line2 = ARGCOUNT;
4048 break;
4049 case ADDR_QUICKFIX_VALID:
4050#ifdef FEAT_QUICKFIX
4051 eap->line2 = qf_get_valid_size(eap);
4052 if (eap->line2 == 0)
4053 eap->line2 = 1;
4054#endif
4055 break;
4056 case ADDR_NONE:
4057 case ADDR_UNSIGNED:
4058 case ADDR_QUICKFIX:
4059 iemsg(_("INTERNAL: Cannot use EX_DFLALL with ADDR_NONE, ADDR_UNSIGNED or ADDR_QUICKFIX"));
4060 break;
4061 }
4062}
4063
4064
4065/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00004066 * Get flags from an Ex command argument.
4067 */
4068 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004069get_flags(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00004070{
4071 while (vim_strchr((char_u *)"lp#", *eap->arg) != NULL)
4072 {
4073 if (*eap->arg == 'l')
4074 eap->flags |= EXFLAG_LIST;
4075 else if (*eap->arg == 'p')
4076 eap->flags |= EXFLAG_PRINT;
4077 else
4078 eap->flags |= EXFLAG_NR;
4079 eap->arg = skipwhite(eap->arg + 1);
4080 }
4081}
4082
4083/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004084 * Function called for command which is Not Implemented. NI!
4085 */
4086 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004087ex_ni(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004088{
4089 if (!eap->skip)
Bram Moolenaar8930caa2020-07-23 16:37:03 +02004090 eap->errmsg =
4091 _("E319: Sorry, the command is not available in this version");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004092}
4093
Bram Moolenaar7bb75552007-07-16 18:39:49 +00004094#ifdef HAVE_EX_SCRIPT_NI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004095/*
4096 * Function called for script command which is Not Implemented. NI!
4097 * Skips over ":perl <<EOF" constructs.
4098 */
4099 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004100ex_script_ni(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004101{
4102 if (!eap->skip)
4103 ex_ni(eap);
4104 else
4105 vim_free(script_get(eap, eap->arg));
4106}
4107#endif
4108
4109/*
4110 * Check range in Ex command for validity.
4111 * Return NULL when valid, error message when invalid.
4112 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004113 static char *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004114invalid_range(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004115{
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004116 buf_T *buf;
Bram Moolenaar25190db2019-05-04 15:05:28 +02004117
Bram Moolenaar071d4272004-06-13 20:20:40 +00004118 if ( eap->line1 < 0
4119 || eap->line2 < 0
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004120 || eap->line1 > eap->line2)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004121 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004122
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004123 if (eap->argt & EX_RANGE)
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004124 {
Bram Moolenaarb7316892019-05-01 18:08:42 +02004125 switch (eap->addr_type)
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004126 {
4127 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004128 if (eap->line2 > curbuf->b_ml.ml_line_count
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004129#ifdef FEAT_DIFF
4130 + (eap->cmdidx == CMD_diffget)
4131#endif
4132 )
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004133 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004134 break;
4135 case ADDR_ARGUMENTS:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004136 // add 1 if ARGCOUNT is 0
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004137 if (eap->line2 > ARGCOUNT + (!ARGCOUNT))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004138 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004139 break;
4140 case ADDR_BUFFERS:
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02004141 // Only a boundary check, not whether the buffers actually
4142 // exist.
4143 if (eap->line1 < 1 || eap->line2 > get_highest_fnum())
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004144 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004145 break;
4146 case ADDR_LOADED_BUFFERS:
4147 buf = firstbuf;
4148 while (buf->b_ml.ml_mfp == NULL)
4149 {
4150 if (buf->b_next == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004151 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004152 buf = buf->b_next;
4153 }
4154 if (eap->line1 < buf->b_fnum)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004155 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004156 buf = lastbuf;
4157 while (buf->b_ml.ml_mfp == NULL)
4158 {
4159 if (buf->b_prev == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004160 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004161 buf = buf->b_prev;
4162 }
4163 if (eap->line2 > buf->b_fnum)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004164 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004165 break;
4166 case ADDR_WINDOWS:
Bram Moolenaar8be63882015-01-14 11:25:05 +01004167 if (eap->line2 > LAST_WIN_NR)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004168 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004169 break;
4170 case ADDR_TABS:
4171 if (eap->line2 > LAST_TAB_NR)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004172 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004173 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004174 case ADDR_TABS_RELATIVE:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004175 case ADDR_OTHER:
4176 // Any range is OK.
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004177 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004178 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004179#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02004180 // No error for value that is too big, will use the last entry.
4181 if (eap->line2 <= 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004182 return _(e_invrange);
Bram Moolenaare906c502015-09-09 21:10:39 +02004183#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004184 break;
Bram Moolenaar25190db2019-05-04 15:05:28 +02004185 case ADDR_QUICKFIX_VALID:
4186#ifdef FEAT_QUICKFIX
4187 if ((eap->line2 != 1 && eap->line2 > qf_get_valid_size(eap))
4188 || eap->line2 < 0)
4189 return _(e_invrange);
4190#endif
4191 break;
4192 case ADDR_UNSIGNED:
4193 if (eap->line2 < 0)
4194 return _(e_invrange);
4195 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02004196 case ADDR_NONE:
4197 // Will give an error elsewhere.
4198 break;
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004199 }
4200 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004201 return NULL;
4202}
4203
4204/*
4205 * Correct the range for zero line number, if required.
4206 */
4207 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004208correct_range(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004209{
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004210 if (!(eap->argt & EX_ZEROR)) // zero in range not allowed
Bram Moolenaar071d4272004-06-13 20:20:40 +00004211 {
4212 if (eap->line1 == 0)
4213 eap->line1 = 1;
4214 if (eap->line2 == 0)
4215 eap->line2 = 1;
4216 }
4217}
4218
Bram Moolenaar748bf032005-02-02 23:04:36 +00004219#ifdef FEAT_QUICKFIX
Bram Moolenaar748bf032005-02-02 23:04:36 +00004220/*
4221 * For a ":vimgrep" or ":vimgrepadd" command return a pointer past the
4222 * pattern. Otherwise return eap->arg.
4223 */
4224 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004225skip_grep_pat(exarg_T *eap)
Bram Moolenaar748bf032005-02-02 23:04:36 +00004226{
4227 char_u *p = eap->arg;
4228
Bram Moolenaara37420f2006-02-04 22:37:47 +00004229 if (*p != NUL && (eap->cmdidx == CMD_vimgrep || eap->cmdidx == CMD_lvimgrep
4230 || eap->cmdidx == CMD_vimgrepadd
4231 || eap->cmdidx == CMD_lvimgrepadd
4232 || grep_internal(eap->cmdidx)))
Bram Moolenaar748bf032005-02-02 23:04:36 +00004233 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00004234 p = skip_vimgrep_pat(p, NULL, NULL);
Bram Moolenaar748bf032005-02-02 23:04:36 +00004235 if (p == NULL)
4236 p = eap->arg;
Bram Moolenaar748bf032005-02-02 23:04:36 +00004237 }
4238 return p;
4239}
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004240
4241/*
4242 * For the ":make" and ":grep" commands insert the 'makeprg'/'grepprg' option
4243 * in the command line, so that things like % get expanded.
4244 */
4245 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004246replace_makeprg(exarg_T *eap, char_u *p, char_u **cmdlinep)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004247{
4248 char_u *new_cmdline;
4249 char_u *program;
4250 char_u *pos;
4251 char_u *ptr;
4252 int len;
4253 int i;
4254
4255 /*
4256 * Don't do it when ":vimgrep" is used for ":grep".
4257 */
Bram Moolenaara37420f2006-02-04 22:37:47 +00004258 if ((eap->cmdidx == CMD_make || eap->cmdidx == CMD_lmake
4259 || eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4260 || eap->cmdidx == CMD_grepadd
4261 || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004262 && !grep_internal(eap->cmdidx))
4263 {
Bram Moolenaara37420f2006-02-04 22:37:47 +00004264 if (eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4265 || eap->cmdidx == CMD_grepadd || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004266 {
4267 if (*curbuf->b_p_gp == NUL)
4268 program = p_gp;
4269 else
4270 program = curbuf->b_p_gp;
4271 }
4272 else
4273 {
4274 if (*curbuf->b_p_mp == NUL)
4275 program = p_mp;
4276 else
4277 program = curbuf->b_p_mp;
4278 }
4279
4280 p = skipwhite(p);
4281
4282 if ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4283 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004284 // replace $* by given arguments
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004285 i = 1;
4286 while ((pos = (char_u *)strstr((char *)pos + 2, "$*")) != NULL)
4287 ++i;
4288 len = (int)STRLEN(p);
Bram Moolenaar51e14382019-05-25 20:21:28 +02004289 new_cmdline = alloc(STRLEN(program) + i * (len - 2) + 1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004290 if (new_cmdline == NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004291 return NULL; // out of memory
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004292 ptr = new_cmdline;
4293 while ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4294 {
4295 i = (int)(pos - program);
4296 STRNCPY(ptr, program, i);
4297 STRCPY(ptr += i, p);
4298 ptr += len;
4299 program = pos + 2;
4300 }
4301 STRCPY(ptr, program);
4302 }
4303 else
4304 {
Bram Moolenaar51e14382019-05-25 20:21:28 +02004305 new_cmdline = alloc(STRLEN(program) + STRLEN(p) + 2);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004306 if (new_cmdline == NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004307 return NULL; // out of memory
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004308 STRCPY(new_cmdline, program);
4309 STRCAT(new_cmdline, " ");
4310 STRCAT(new_cmdline, p);
4311 }
4312 msg_make(p);
4313
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004314 // 'eap->cmd' is not set here, because it is not used at CMD_make
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004315 vim_free(*cmdlinep);
4316 *cmdlinep = new_cmdline;
4317 p = new_cmdline;
4318 }
4319 return p;
4320}
Bram Moolenaar748bf032005-02-02 23:04:36 +00004321#endif
4322
Bram Moolenaar071d4272004-06-13 20:20:40 +00004323/*
4324 * Expand file name in Ex command argument.
Bram Moolenaar0abb4272019-06-15 16:06:00 +02004325 * When an error is detected, "errormsgp" is set to a non-NULL pointer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004326 * Return FAIL for failure, OK otherwise.
4327 */
4328 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004329expand_filename(
4330 exarg_T *eap,
4331 char_u **cmdlinep,
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004332 char **errormsgp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004333{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004334 int has_wildcards; // need to expand wildcards
Bram Moolenaar071d4272004-06-13 20:20:40 +00004335 char_u *repl;
4336 int srclen;
4337 char_u *p;
4338 int n;
Bram Moolenaar63b92542007-03-27 14:57:09 +00004339 int escaped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004340
Bram Moolenaar748bf032005-02-02 23:04:36 +00004341#ifdef FEAT_QUICKFIX
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004342 // Skip a regexp pattern for ":vimgrep[add] pat file..."
Bram Moolenaar748bf032005-02-02 23:04:36 +00004343 p = skip_grep_pat(eap);
4344#else
4345 p = eap->arg;
4346#endif
4347
Bram Moolenaar071d4272004-06-13 20:20:40 +00004348 /*
4349 * Decide to expand wildcards *before* replacing '%', '#', etc. If
4350 * the file name contains a wildcard it should not cause expanding.
4351 * (it will be expanded anyway if there is a wildcard before replacing).
4352 */
Bram Moolenaar748bf032005-02-02 23:04:36 +00004353 has_wildcards = mch_has_wildcard(p);
4354 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004355 {
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004356#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004357 // Skip over `=expr`, wildcards in it are not expanded.
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004358 if (p[0] == '`' && p[1] == '=')
4359 {
4360 p += 2;
4361 (void)skip_expr(&p);
4362 if (*p == '`')
4363 ++p;
4364 continue;
4365 }
4366#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004367 /*
4368 * Quick check if this cannot be the start of a special string.
4369 * Also removes backslash before '%', '#' and '<'.
4370 */
4371 if (vim_strchr((char_u *)"%#<", *p) == NULL)
4372 {
4373 ++p;
4374 continue;
4375 }
4376
4377 /*
4378 * Try to find a match at this position.
4379 */
Bram Moolenaar63b92542007-03-27 14:57:09 +00004380 repl = eval_vars(p, eap->arg, &srclen, &(eap->do_ecmd_lnum),
4381 errormsgp, &escaped);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004382 if (*errormsgp != NULL) // error detected
Bram Moolenaar071d4272004-06-13 20:20:40 +00004383 return FAIL;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004384 if (repl == NULL) // no match found
Bram Moolenaar071d4272004-06-13 20:20:40 +00004385 {
4386 p += srclen;
4387 continue;
4388 }
4389
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004390 // Wildcards won't be expanded below, the replacement is taken
4391 // literally. But do expand "~/file", "~user/file" and "$HOME/file".
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00004392 if (vim_strchr(repl, '$') != NULL || vim_strchr(repl, '~') != NULL)
4393 {
4394 char_u *l = repl;
4395
4396 repl = expand_env_save(repl);
4397 vim_free(l);
4398 }
4399
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004400 // Need to escape white space et al. with a backslash.
4401 // Don't do this for:
4402 // - replacement that already has been escaped: "##"
4403 // - shell commands (may have to use quotes instead).
4404 // - non-unix systems when there is a single argument (spaces don't
4405 // separate arguments then).
Bram Moolenaar071d4272004-06-13 20:20:40 +00004406 if (!eap->usefilter
Bram Moolenaar63b92542007-03-27 14:57:09 +00004407 && !escaped
Bram Moolenaar071d4272004-06-13 20:20:40 +00004408 && eap->cmdidx != CMD_bang
Bram Moolenaar071d4272004-06-13 20:20:40 +00004409 && eap->cmdidx != CMD_grep
4410 && eap->cmdidx != CMD_grepadd
Bram Moolenaarbf15b8d2017-06-04 20:43:48 +02004411 && eap->cmdidx != CMD_hardcopy
Bram Moolenaar67883b42017-07-27 22:57:00 +02004412 && eap->cmdidx != CMD_lgrep
4413 && eap->cmdidx != CMD_lgrepadd
4414 && eap->cmdidx != CMD_lmake
4415 && eap->cmdidx != CMD_make
4416 && eap->cmdidx != CMD_terminal
Bram Moolenaar071d4272004-06-13 20:20:40 +00004417#ifndef UNIX
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004418 && !(eap->argt & EX_NOSPC)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004419#endif
4420 )
4421 {
4422 char_u *l;
4423#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004424 // Don't escape a backslash here, because rem_backslash() doesn't
4425 // remove it later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004426 static char_u *nobslash = (char_u *)" \t\"|";
4427# define ESCAPE_CHARS nobslash
4428#else
4429# define ESCAPE_CHARS escape_chars
4430#endif
4431
4432 for (l = repl; *l; ++l)
4433 if (vim_strchr(ESCAPE_CHARS, *l) != NULL)
4434 {
4435 l = vim_strsave_escaped(repl, ESCAPE_CHARS);
4436 if (l != NULL)
4437 {
4438 vim_free(repl);
4439 repl = l;
4440 }
4441 break;
4442 }
4443 }
4444
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004445 // For a shell command a '!' must be escaped.
Bram Moolenaar67883b42017-07-27 22:57:00 +02004446 if ((eap->usefilter || eap->cmdidx == CMD_bang
4447 || eap->cmdidx == CMD_terminal)
Bram Moolenaar31b7d382014-04-01 18:54:48 +02004448 && vim_strpbrk(repl, (char_u *)"!") != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004449 {
4450 char_u *l;
4451
Bram Moolenaar31b7d382014-04-01 18:54:48 +02004452 l = vim_strsave_escaped(repl, (char_u *)"!");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004453 if (l != NULL)
4454 {
4455 vim_free(repl);
4456 repl = l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004457 }
4458 }
4459
4460 p = repl_cmdline(eap, p, srclen, repl, cmdlinep);
4461 vim_free(repl);
4462 if (p == NULL)
4463 return FAIL;
4464 }
4465
4466 /*
4467 * One file argument: Expand wildcards.
4468 * Don't do this with ":r !command" or ":w !command".
4469 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004470 if ((eap->argt & EX_NOSPC) && !eap->usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004471 {
4472 /*
4473 * May do this twice:
4474 * 1. Replace environment variables.
4475 * 2. Replace any other wildcards, remove backslashes.
4476 */
4477 for (n = 1; n <= 2; ++n)
4478 {
4479 if (n == 2)
4480 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004481 /*
4482 * Halve the number of backslashes (this is Vi compatible).
4483 * For Unix and OS/2, when wildcards are expanded, this is
4484 * done by ExpandOne() below.
4485 */
Bram Moolenaare7fedb62015-12-31 19:07:19 +01004486#if defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004487 if (!has_wildcards)
4488#endif
4489 backslash_halve(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004490 }
4491
4492 if (has_wildcards)
4493 {
4494 if (n == 1)
4495 {
4496 /*
4497 * First loop: May expand environment variables. This
4498 * can be done much faster with expand_env() than with
4499 * something else (e.g., calling a shell).
4500 * After expanding environment variables, check again
4501 * if there are still wildcards present.
4502 */
4503 if (vim_strchr(eap->arg, '$') != NULL
4504 || vim_strchr(eap->arg, '~') != NULL)
4505 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +00004506 expand_env_esc(eap->arg, NameBuff, MAXPATHL,
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00004507 TRUE, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004508 has_wildcards = mch_has_wildcard(NameBuff);
4509 p = NameBuff;
4510 }
4511 else
4512 p = NULL;
4513 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004514 else // n == 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00004515 {
4516 expand_T xpc;
Bram Moolenaarb40c2572019-10-19 21:01:05 +02004517 int options = WILD_LIST_NOTFOUND
4518 | WILD_NOERROR | WILD_ADD_SLASH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004519
4520 ExpandInit(&xpc);
4521 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar94950a92010-12-02 16:01:29 +01004522 if (p_wic)
4523 options += WILD_ICASE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004524 p = ExpandOne(&xpc, eap->arg, NULL,
Bram Moolenaar94950a92010-12-02 16:01:29 +01004525 options, WILD_EXPAND_FREE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004526 if (p == NULL)
4527 return FAIL;
4528 }
4529 if (p != NULL)
4530 {
4531 (void)repl_cmdline(eap, eap->arg, (int)STRLEN(eap->arg),
4532 p, cmdlinep);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004533 if (n == 2) // p came from ExpandOne()
Bram Moolenaar071d4272004-06-13 20:20:40 +00004534 vim_free(p);
4535 }
4536 }
4537 }
4538 }
4539 return OK;
4540}
4541
4542/*
4543 * Replace part of the command line, keeping eap->cmd, eap->arg and
4544 * eap->nextcmd correct.
4545 * "src" points to the part that is to be replaced, of length "srclen".
4546 * "repl" is the replacement string.
4547 * Returns a pointer to the character after the replaced string.
4548 * Returns NULL for failure.
4549 */
4550 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004551repl_cmdline(
4552 exarg_T *eap,
4553 char_u *src,
4554 int srclen,
4555 char_u *repl,
4556 char_u **cmdlinep)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004557{
4558 int len;
4559 int i;
4560 char_u *new_cmdline;
4561
4562 /*
4563 * The new command line is build in new_cmdline[].
4564 * First allocate it.
4565 * Careful: a "+cmd" argument may have been NUL terminated.
4566 */
4567 len = (int)STRLEN(repl);
4568 i = (int)(src - *cmdlinep) + (int)STRLEN(src + srclen) + len + 3;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004569 if (eap->nextcmd != NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004570 i += (int)STRLEN(eap->nextcmd);// add space for next command
Bram Moolenaar964b3742019-05-24 18:54:09 +02004571 if ((new_cmdline = alloc(i)) == NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004572 return NULL; // out of memory!
Bram Moolenaar071d4272004-06-13 20:20:40 +00004573
4574 /*
4575 * Copy the stuff before the expanded part.
4576 * Copy the expanded stuff.
4577 * Copy what came after the expanded part.
4578 * Copy the next commands, if there are any.
4579 */
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004580 i = (int)(src - *cmdlinep); // length of part before match
Bram Moolenaar071d4272004-06-13 20:20:40 +00004581 mch_memmove(new_cmdline, *cmdlinep, (size_t)i);
Bram Moolenaara3ffd9c2005-07-21 21:03:15 +00004582
Bram Moolenaar071d4272004-06-13 20:20:40 +00004583 mch_memmove(new_cmdline + i, repl, (size_t)len);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004584 i += len; // remember the end of the string
Bram Moolenaar071d4272004-06-13 20:20:40 +00004585 STRCPY(new_cmdline + i, src + srclen);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004586 src = new_cmdline + i; // remember where to continue
Bram Moolenaar071d4272004-06-13 20:20:40 +00004587
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004588 if (eap->nextcmd != NULL) // append next command
Bram Moolenaar071d4272004-06-13 20:20:40 +00004589 {
4590 i = (int)STRLEN(new_cmdline) + 1;
4591 STRCPY(new_cmdline + i, eap->nextcmd);
4592 eap->nextcmd = new_cmdline + i;
4593 }
4594 eap->cmd = new_cmdline + (eap->cmd - *cmdlinep);
4595 eap->arg = new_cmdline + (eap->arg - *cmdlinep);
4596 if (eap->do_ecmd_cmd != NULL && eap->do_ecmd_cmd != dollar_command)
4597 eap->do_ecmd_cmd = new_cmdline + (eap->do_ecmd_cmd - *cmdlinep);
4598 vim_free(*cmdlinep);
4599 *cmdlinep = new_cmdline;
4600
4601 return src;
4602}
4603
4604/*
4605 * Check for '|' to separate commands and '"' to start comments.
4606 */
4607 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004608separate_nextcmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004609{
4610 char_u *p;
4611
Bram Moolenaar86b68352004-12-27 21:59:20 +00004612#ifdef FEAT_QUICKFIX
Bram Moolenaar748bf032005-02-02 23:04:36 +00004613 p = skip_grep_pat(eap);
4614#else
4615 p = eap->arg;
Bram Moolenaar86b68352004-12-27 21:59:20 +00004616#endif
4617
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004618 for ( ; *p; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004619 {
4620 if (*p == Ctrl_V)
4621 {
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004622 if (eap->argt & (EX_CTRLV | EX_XFILE))
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004623 ++p; // skip CTRL-V and next char
Bram Moolenaar071d4272004-06-13 20:20:40 +00004624 else
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004625 // remove CTRL-V and skip next char
Bram Moolenaara7241f52008-06-24 20:39:31 +00004626 STRMOVE(p, p + 1);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004627 if (*p == NUL) // stop at NUL after CTRL-V
Bram Moolenaar071d4272004-06-13 20:20:40 +00004628 break;
4629 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004630
4631#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004632 // Skip over `=expr` when wildcards are expanded.
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004633 else if (p[0] == '`' && p[1] == '=' && (eap->argt & EX_XFILE))
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004634 {
4635 p += 2;
4636 (void)skip_expr(&p);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004637 }
4638#endif
4639
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004640 // Check for '"': start of comment or '|': next command
4641 // :@" and :*" do not start a comment!
4642 // :redir @" doesn't either.
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02004643 else if ((*p == '"'
Bram Moolenaara26b9702020-04-18 19:53:28 +02004644#ifdef FEAT_EVAL
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02004645 && !in_vim9script()
4646#endif
4647 && !(eap->argt & EX_NOTRLCOM)
4648 && ((eap->cmdidx != CMD_at && eap->cmdidx != CMD_star)
4649 || p != eap->arg)
4650 && (eap->cmdidx != CMD_redir
4651 || p != eap->arg + 1 || p[-1] != '@'))
4652#ifdef FEAT_EVAL
4653 || (*p == '#'
4654 && in_vim9script()
Bram Moolenaarb8a92962020-08-20 18:02:47 +02004655 && !(eap->argt & EX_NOTRLCOM)
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02004656 && p[1] != '{'
4657 && p > eap->cmd && VIM_ISWHITE(p[-1]))
Bram Moolenaara26b9702020-04-18 19:53:28 +02004658#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004659 || *p == '|' || *p == '\n')
4660 {
4661 /*
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004662 * We remove the '\' before the '|', unless EX_CTRLV is used
Bram Moolenaar071d4272004-06-13 20:20:40 +00004663 * AND 'b' is present in 'cpoptions'.
4664 */
4665 if ((vim_strchr(p_cpo, CPO_BAR) == NULL
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004666 || !(eap->argt & EX_CTRLV)) && *(p - 1) == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004667 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004668 STRMOVE(p - 1, p); // remove the '\'
Bram Moolenaar071d4272004-06-13 20:20:40 +00004669 --p;
4670 }
4671 else
4672 {
4673 eap->nextcmd = check_nextcmd(p);
4674 *p = NUL;
4675 break;
4676 }
4677 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004678 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00004679
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004680 if (!(eap->argt & EX_NOTRLCOM)) // remove trailing spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00004681 del_trailing_spaces(eap->arg);
4682}
4683
4684/*
4685 * get + command from ex argument
4686 */
4687 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004688getargcmd(char_u **argp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004689{
4690 char_u *arg = *argp;
4691 char_u *command = NULL;
4692
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004693 if (*arg == '+') // +[command]
Bram Moolenaar071d4272004-06-13 20:20:40 +00004694 {
4695 ++arg;
Bram Moolenaar3e451592014-04-02 14:22:05 +02004696 if (vim_isspace(*arg) || *arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004697 command = dollar_command;
4698 else
4699 {
4700 command = arg;
4701 arg = skip_cmd_arg(command, TRUE);
4702 if (*arg != NUL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004703 *arg++ = NUL; // terminate command with NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004704 }
4705
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004706 arg = skipwhite(arg); // skip over spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00004707 *argp = arg;
4708 }
4709 return command;
4710}
4711
4712/*
4713 * Find end of "+command" argument. Skip over "\ " and "\\".
4714 */
Bram Moolenaard0190392019-08-23 21:17:35 +02004715 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004716skip_cmd_arg(
4717 char_u *p,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004718 int rembs) // TRUE to halve the number of backslashes
Bram Moolenaar071d4272004-06-13 20:20:40 +00004719{
4720 while (*p && !vim_isspace(*p))
4721 {
4722 if (*p == '\\' && p[1] != NUL)
4723 {
4724 if (rembs)
Bram Moolenaara7241f52008-06-24 20:39:31 +00004725 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004726 else
4727 ++p;
4728 }
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004729 MB_PTR_ADV(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004730 }
4731 return p;
4732}
4733
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004734 int
4735get_bad_opt(char_u *p, exarg_T *eap)
4736{
4737 if (STRICMP(p, "keep") == 0)
4738 eap->bad_char = BAD_KEEP;
4739 else if (STRICMP(p, "drop") == 0)
4740 eap->bad_char = BAD_DROP;
4741 else if (MB_BYTE2LEN(*p) == 1 && p[1] == NUL)
4742 eap->bad_char = *p;
Bram Moolenaar75808492018-06-12 12:39:41 +02004743 else
4744 return FAIL;
4745 return OK;
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004746}
4747
Bram Moolenaar071d4272004-06-13 20:20:40 +00004748/*
4749 * Get "++opt=arg" argument.
4750 * Return FAIL or OK.
4751 */
4752 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004753getargopt(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004754{
4755 char_u *arg = eap->arg + 2;
4756 int *pp = NULL;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02004757 int bad_char_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004758 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004759
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004760 // ":edit ++[no]bin[ary] file"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004761 if (STRNCMP(arg, "bin", 3) == 0 || STRNCMP(arg, "nobin", 5) == 0)
4762 {
4763 if (*arg == 'n')
4764 {
4765 arg += 2;
4766 eap->force_bin = FORCE_NOBIN;
4767 }
4768 else
4769 eap->force_bin = FORCE_BIN;
4770 if (!checkforcmd(&arg, "binary", 3))
4771 return FAIL;
4772 eap->arg = skipwhite(arg);
4773 return OK;
4774 }
4775
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004776 // ":read ++edit file"
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004777 if (STRNCMP(arg, "edit", 4) == 0)
4778 {
4779 eap->read_edit = TRUE;
4780 eap->arg = skipwhite(arg + 4);
4781 return OK;
4782 }
4783
Bram Moolenaar071d4272004-06-13 20:20:40 +00004784 if (STRNCMP(arg, "ff", 2) == 0)
4785 {
4786 arg += 2;
4787 pp = &eap->force_ff;
4788 }
4789 else if (STRNCMP(arg, "fileformat", 10) == 0)
4790 {
4791 arg += 10;
4792 pp = &eap->force_ff;
4793 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004794 else if (STRNCMP(arg, "enc", 3) == 0)
4795 {
Bram Moolenaarb38e9ab2011-12-14 14:49:45 +01004796 if (STRNCMP(arg, "encoding", 8) == 0)
4797 arg += 8;
4798 else
4799 arg += 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004800 pp = &eap->force_enc;
4801 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004802 else if (STRNCMP(arg, "bad", 3) == 0)
4803 {
4804 arg += 3;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02004805 pp = &bad_char_idx;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004806 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004807
4808 if (pp == NULL || *arg != '=')
4809 return FAIL;
4810
4811 ++arg;
4812 *pp = (int)(arg - eap->cmd);
4813 arg = skip_cmd_arg(arg, FALSE);
4814 eap->arg = skipwhite(arg);
4815 *arg = NUL;
4816
Bram Moolenaar071d4272004-06-13 20:20:40 +00004817 if (pp == &eap->force_ff)
4818 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004819 if (check_ff_value(eap->cmd + eap->force_ff) == FAIL)
4820 return FAIL;
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004821 eap->force_ff = eap->cmd[eap->force_ff];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004822 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004823 else if (pp == &eap->force_enc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004824 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004825 // Make 'fileencoding' lower case.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004826 for (p = eap->cmd + eap->force_enc; *p != NUL; ++p)
4827 *p = TOLOWER_ASC(*p);
4828 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004829 else
4830 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004831 // Check ++bad= argument. Must be a single-byte character, "keep" or
4832 // "drop".
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004833 if (get_bad_opt(eap->cmd + bad_char_idx, eap) == FAIL)
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004834 return FAIL;
4835 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004836
4837 return OK;
4838}
4839
Bram Moolenaar071d4272004-06-13 20:20:40 +00004840 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004841ex_autocmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004842{
4843 /*
Bram Moolenaar26d4b892018-07-04 22:26:28 +02004844 * Disallow autocommands from .exrc and .vimrc in current
Bram Moolenaar071d4272004-06-13 20:20:40 +00004845 * directory for security reasons.
4846 */
4847 if (secure)
4848 {
4849 secure = 2;
Bram Moolenaar8930caa2020-07-23 16:37:03 +02004850 eap->errmsg = _(e_curdir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004851 }
4852 else if (eap->cmdidx == CMD_autocmd)
4853 do_autocmd(eap->arg, eap->forceit);
4854 else
4855 do_augroup(eap->arg, eap->forceit);
4856}
4857
4858/*
4859 * ":doautocmd": Apply the automatic commands to the current buffer.
4860 */
4861 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004862ex_doautocmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004863{
Bram Moolenaar60542ac2012-02-12 20:14:01 +01004864 char_u *arg = eap->arg;
4865 int call_do_modelines = check_nomodeline(&arg);
Bram Moolenaar1610d052016-06-09 22:53:01 +02004866 int did_aucmd;
Bram Moolenaar60542ac2012-02-12 20:14:01 +01004867
Bram Moolenaar1610d052016-06-09 22:53:01 +02004868 (void)do_doautocmd(arg, TRUE, &did_aucmd);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004869 // Only when there is no <nomodeline>.
Bram Moolenaar1610d052016-06-09 22:53:01 +02004870 if (call_do_modelines && did_aucmd)
Bram Moolenaar60542ac2012-02-12 20:14:01 +01004871 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004872}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004873
Bram Moolenaar071d4272004-06-13 20:20:40 +00004874/*
4875 * :[N]bunload[!] [N] [bufname] unload buffer
4876 * :[N]bdelete[!] [N] [bufname] delete buffer from buffer list
4877 * :[N]bwipeout[!] [N] [bufname] delete buffer really
4878 */
4879 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004880ex_bunload(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004881{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01004882 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02004883 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004884 eap->errmsg = do_bufdel(
4885 eap->cmdidx == CMD_bdelete ? DOBUF_DEL
4886 : eap->cmdidx == CMD_bwipeout ? DOBUF_WIPE
4887 : DOBUF_UNLOAD, eap->arg,
4888 eap->addr_count, (int)eap->line1, (int)eap->line2, eap->forceit);
4889}
4890
4891/*
4892 * :[N]buffer [N] to buffer N
4893 * :[N]sbuffer [N] to buffer N
4894 */
4895 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004896ex_buffer(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004897{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01004898 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02004899 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004900 if (*eap->arg)
Bram Moolenaar8930caa2020-07-23 16:37:03 +02004901 eap->errmsg = ex_errmsg(e_trailing_arg, eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004902 else
4903 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004904 if (eap->addr_count == 0) // default is current buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00004905 goto_buffer(eap, DOBUF_CURRENT, FORWARD, 0);
4906 else
4907 goto_buffer(eap, DOBUF_FIRST, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02004908 if (eap->do_ecmd_cmd != NULL)
4909 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004910 }
4911}
4912
4913/*
4914 * :[N]bmodified [N] to next mod. buffer
4915 * :[N]sbmodified [N] to next mod. buffer
4916 */
4917 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004918ex_bmodified(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004919{
4920 goto_buffer(eap, DOBUF_MOD, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02004921 if (eap->do_ecmd_cmd != NULL)
4922 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004923}
4924
4925/*
4926 * :[N]bnext [N] to next buffer
4927 * :[N]sbnext [N] split and to next buffer
4928 */
4929 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004930ex_bnext(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004931{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01004932 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar3029bcc2020-01-18 15:06:19 +01004933 return;
4934
Bram Moolenaar071d4272004-06-13 20:20:40 +00004935 goto_buffer(eap, DOBUF_CURRENT, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02004936 if (eap->do_ecmd_cmd != NULL)
4937 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004938}
4939
4940/*
4941 * :[N]bNext [N] to previous buffer
4942 * :[N]bprevious [N] to previous buffer
4943 * :[N]sbNext [N] split and to previous buffer
4944 * :[N]sbprevious [N] split and to previous buffer
4945 */
4946 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004947ex_bprevious(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004948{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01004949 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar3029bcc2020-01-18 15:06:19 +01004950 return;
4951
Bram Moolenaar071d4272004-06-13 20:20:40 +00004952 goto_buffer(eap, DOBUF_CURRENT, BACKWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02004953 if (eap->do_ecmd_cmd != NULL)
4954 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004955}
4956
4957/*
4958 * :brewind to first buffer
4959 * :bfirst to first buffer
4960 * :sbrewind split and to first buffer
4961 * :sbfirst split and to first buffer
4962 */
4963 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004964ex_brewind(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004965{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01004966 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar3029bcc2020-01-18 15:06:19 +01004967 return;
4968
Bram Moolenaar071d4272004-06-13 20:20:40 +00004969 goto_buffer(eap, DOBUF_FIRST, FORWARD, 0);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02004970 if (eap->do_ecmd_cmd != NULL)
4971 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004972}
4973
4974/*
4975 * :blast to last buffer
4976 * :sblast split and to last buffer
4977 */
4978 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004979ex_blast(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004980{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01004981 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar3029bcc2020-01-18 15:06:19 +01004982 return;
4983
Bram Moolenaar071d4272004-06-13 20:20:40 +00004984 goto_buffer(eap, DOBUF_LAST, BACKWARD, 0);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02004985 if (eap->do_ecmd_cmd != NULL)
4986 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004987}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004988
Bram Moolenaar7a092242020-04-16 22:10:49 +02004989/*
4990 * Check if "c" ends an Ex command.
Bram Moolenaara494f562020-04-18 17:45:38 +02004991 * In Vim9 script does not check for white space before # or #{.
Bram Moolenaar7a092242020-04-16 22:10:49 +02004992 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004993 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004994ends_excmd(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004995{
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02004996 int comment_char = '"';
4997
Bram Moolenaarfbda69b2020-04-13 15:06:53 +02004998#ifdef FEAT_EVAL
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02004999 if (in_vim9script())
5000 comment_char = '#';
Bram Moolenaar7a092242020-04-16 22:10:49 +02005001#endif
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005002 return (c == NUL || c == '|' || c == comment_char || c == '\n');
Bram Moolenaar7a092242020-04-16 22:10:49 +02005003}
5004
5005/*
5006 * Like ends_excmd() but checks that a # in Vim9 script either has "cmd" equal
5007 * to "cmd_start" or has a white space character before it.
5008 */
5009 int
Bram Moolenaar21cfe502020-04-16 23:01:50 +02005010ends_excmd2(char_u *cmd_start UNUSED, char_u *cmd)
Bram Moolenaar7a092242020-04-16 22:10:49 +02005011{
5012 int c = *cmd;
5013
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005014 if (c == NUL || c == '|' || c == '\n')
5015 return TRUE;
Bram Moolenaar7a092242020-04-16 22:10:49 +02005016#ifdef FEAT_EVAL
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005017 if (in_vim9script())
5018 return c == '#' && cmd[1] != '{'
5019 && (cmd == cmd_start || VIM_ISWHITE(cmd[-1]));
Bram Moolenaarfbda69b2020-04-13 15:06:53 +02005020#endif
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005021 return c == '"';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005022}
5023
5024#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA) || defined(FEAT_EVAL) \
5025 || defined(PROTO)
5026/*
5027 * Return the next command, after the first '|' or '\n'.
5028 * Return NULL if not found.
5029 */
5030 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005031find_nextcmd(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005032{
5033 while (*p != '|' && *p != '\n')
5034 {
5035 if (*p == NUL)
5036 return NULL;
5037 ++p;
5038 }
5039 return (p + 1);
5040}
5041#endif
5042
5043/*
Bram Moolenaar2256c992016-11-15 21:17:07 +01005044 * Check if *p is a separator between Ex commands, skipping over white space.
5045 * Return NULL if it isn't, the following character if it is.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005046 */
5047 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005048check_nextcmd(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005049{
Bram Moolenaar2256c992016-11-15 21:17:07 +01005050 char_u *s = skipwhite(p);
5051
5052 if (*s == '|' || *s == '\n')
5053 return (s + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005054 else
5055 return NULL;
5056}
5057
5058/*
5059 * - if there are more files to edit
5060 * - and this is the last window
5061 * - and forceit not used
5062 * - and not repeated twice on a row
5063 * return FAIL and give error message if 'message' TRUE
5064 * return OK otherwise
5065 */
5066 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005067check_more(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005068 int message, // when FALSE check only, no messages
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005069 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005070{
5071 int n = ARGCOUNT - curwin->w_arg_idx - 1;
5072
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005073 if (!forceit && only_one_window()
Bram Moolenaar7b221172020-08-17 19:34:10 +02005074 && ARGCOUNT > 1 && !arg_had_last && n > 0 && quitmore == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005075 {
5076 if (message)
5077 {
5078#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
5079 if ((p_confirm || cmdmod.confirm) && curbuf->b_fname != NULL)
5080 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02005081 char_u buff[DIALOG_MSG_SIZE];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005082
Bram Moolenaarda6e8912018-08-21 15:12:14 +02005083 vim_snprintf((char *)buff, DIALOG_MSG_SIZE,
5084 NGETTEXT("%d more file to edit. Quit anyway?",
5085 "%d more files to edit. Quit anyway?", n), n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005086 if (vim_dialog_yesno(VIM_QUESTION, NULL, buff, 1) == VIM_YES)
5087 return OK;
5088 return FAIL;
5089 }
5090#endif
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01005091 semsg(NGETTEXT("E173: %d more file to edit",
5092 "E173: %d more files to edit", n), n);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005093 quitmore = 2; // next try to quit is allowed
Bram Moolenaar071d4272004-06-13 20:20:40 +00005094 }
5095 return FAIL;
5096 }
5097 return OK;
5098}
5099
Bram Moolenaar071d4272004-06-13 20:20:40 +00005100/*
5101 * Function given to ExpandGeneric() to obtain the list of command names.
5102 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005103 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005104get_command_name(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005105{
5106 if (idx >= (int)CMD_SIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005107 return get_user_command_name(idx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005108 return cmdnames[idx].cmd_name;
5109}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005110
Bram Moolenaar071d4272004-06-13 20:20:40 +00005111 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005112ex_colorscheme(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005113{
Bram Moolenaare6850792010-05-14 15:28:44 +02005114 if (*eap->arg == NUL)
5115 {
5116#ifdef FEAT_EVAL
5117 char_u *expr = vim_strsave((char_u *)"g:colors_name");
5118 char_u *p = NULL;
5119
5120 if (expr != NULL)
5121 {
5122 ++emsg_off;
Bram Moolenaarb171fb12020-06-24 20:34:03 +02005123 p = eval_to_string(expr, FALSE);
Bram Moolenaare6850792010-05-14 15:28:44 +02005124 --emsg_off;
5125 vim_free(expr);
5126 }
5127 if (p != NULL)
5128 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01005129 msg((char *)p);
Bram Moolenaare6850792010-05-14 15:28:44 +02005130 vim_free(p);
5131 }
5132 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01005133 msg("default");
Bram Moolenaare6850792010-05-14 15:28:44 +02005134#else
Bram Moolenaar32526b32019-01-19 17:43:09 +01005135 msg(_("unknown"));
Bram Moolenaare6850792010-05-14 15:28:44 +02005136#endif
5137 }
5138 else if (load_colors(eap->arg) == FAIL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005139 semsg(_("E185: Cannot find color scheme '%s'"), eap->arg);
Bram Moolenaarf58d81a2019-01-28 20:19:05 +01005140
5141#ifdef FEAT_VTP
5142 else if (has_vtp_working())
5143 {
5144 // background color change requires clear + redraw
5145 update_screen(CLEAR);
5146 redrawcmd();
5147 }
5148#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005149}
5150
5151 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005152ex_highlight(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005153{
5154 if (*eap->arg == NUL && eap->cmd[2] == '!')
Bram Moolenaar32526b32019-01-19 17:43:09 +01005155 msg(_("Greetings, Vim user!"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005156 do_highlight(eap->arg, eap->forceit, FALSE);
5157}
5158
5159
5160/*
5161 * Call this function if we thought we were going to exit, but we won't
5162 * (because of an error). May need to restore the terminal mode.
5163 */
5164 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005165not_exiting(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005166{
5167 exiting = FALSE;
5168 settmode(TMODE_RAW);
5169}
5170
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005171 static int
5172before_quit_autocmds(win_T *wp, int quit_all, int forceit)
5173{
5174 apply_autocmds(EVENT_QUITPRE, NULL, NULL, FALSE, wp->w_buffer);
5175
Bram Moolenaar34ba06b2019-10-20 22:27:10 +02005176 // Bail out when autocommands closed the window.
5177 // Refuse to quit when the buffer in the last window is being closed (can
5178 // only happen in autocommands).
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005179 if (!win_valid(wp)
5180 || curbuf_locked()
5181 || (wp->w_buffer->b_nwindows == 1 && wp->w_buffer->b_locked > 0))
5182 return TRUE;
5183
5184 if (quit_all || (check_more(FALSE, forceit) == OK && only_one_window()))
5185 {
5186 apply_autocmds(EVENT_EXITPRE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar34ba06b2019-10-20 22:27:10 +02005187 // Refuse to quit when locked or when the window was closed or the
5188 // buffer in the last window is being closed (can only happen in
5189 // autocommands).
5190 if (!win_valid(wp) || curbuf_locked()
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005191 || (curbuf->b_nwindows == 1 && curbuf->b_locked > 0))
5192 return TRUE;
5193 }
5194
5195 return FALSE;
5196}
5197
Bram Moolenaar071d4272004-06-13 20:20:40 +00005198/*
Bram Moolenaarf240e182014-11-27 18:33:02 +01005199 * ":quit": quit current window, quit Vim if the last window is closed.
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005200 * ":{nr}quit": quit window {nr}
Bram Moolenaar4d14bac2019-10-20 21:15:15 +02005201 * Also used when closing a terminal window that's the last one.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005202 */
Bram Moolenaar4d14bac2019-10-20 21:15:15 +02005203 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005204ex_quit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005205{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005206 win_T *wp;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005207
Bram Moolenaar071d4272004-06-13 20:20:40 +00005208#ifdef FEAT_CMDWIN
5209 if (cmdwin_type != 0)
5210 {
5211 cmdwin_result = Ctrl_C;
5212 return;
5213 }
5214#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005215 // Don't quit while editing the command line.
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005216 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005217 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005218 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005219 return;
5220 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005221 if (eap->addr_count > 0)
5222 {
Bram Moolenaarf240e182014-11-27 18:33:02 +01005223 int wnr = eap->line2;
5224
5225 for (wp = firstwin; wp->w_next != NULL; wp = wp->w_next)
5226 if (--wnr <= 0)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005227 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005228 }
5229 else
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005230 wp = curwin;
Bram Moolenaarf240e182014-11-27 18:33:02 +01005231
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005232 // Refuse to quit when locked.
Bram Moolenaar87ffb5c2017-10-19 12:37:42 +02005233 if (curbuf_locked())
5234 return;
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005235
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005236 // Trigger QuitPre and maybe ExitPre
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005237 if (before_quit_autocmds(wp, FALSE, eap->forceit))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005238 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005239
5240#ifdef FEAT_NETBEANS_INTG
5241 netbeansForcedQuit = eap->forceit;
5242#endif
5243
5244 /*
5245 * If there are more files or windows we won't exit.
5246 */
5247 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
5248 exiting = TRUE;
Bram Moolenaarff930ca2017-10-19 17:12:10 +02005249 if ((!buf_hide(wp->w_buffer)
5250 && check_changed(wp->w_buffer, (p_awa ? CCGD_AW : 0)
Bram Moolenaar45d3b142013-11-09 03:31:51 +01005251 | (eap->forceit ? CCGD_FORCEIT : 0)
5252 | CCGD_EXCMD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005253 || check_more(TRUE, eap->forceit) == FAIL
Bram Moolenaar027387f2016-01-02 22:25:52 +01005254 || (only_one_window() && check_changed_any(eap->forceit, TRUE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005255 {
5256 not_exiting();
5257 }
5258 else
5259 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005260 // quit last window
5261 // Note: only_one_window() returns true, even so a help window is
5262 // still open. In that case only quit, if no address has been
5263 // specified. Example:
5264 // :h|wincmd w|1q - don't quit
5265 // :h|wincmd w|q - quit
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01005266 if (only_one_window() && (ONE_WINDOW || eap->addr_count == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005267 getout(0);
Bram Moolenaar2c33d7b2017-10-14 16:06:20 +02005268 not_exiting();
Bram Moolenaar4033c552017-09-16 20:54:51 +02005269#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00005270 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005271#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005272 // close window; may free buffer
Bram Moolenaareb44a682017-08-03 22:44:55 +02005273 win_close(wp, !buf_hide(wp->w_buffer) || eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005274 }
5275}
5276
5277/*
5278 * ":cquit".
5279 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005280 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005281ex_cquit(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005282{
Bram Moolenaar1860bde2020-01-06 21:47:21 +01005283 // this does not always pass on the exit code to the Manx compiler. why?
5284 getout(eap->addr_count > 0 ? (int)eap->line2 : EXIT_FAILURE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005285}
5286
5287/*
5288 * ":qall": try to quit all windows
5289 */
5290 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005291ex_quit_all(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005292{
5293# ifdef FEAT_CMDWIN
5294 if (cmdwin_type != 0)
5295 {
5296 if (eap->forceit)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005297 cmdwin_result = K_XF1; // ex_window() takes care of this
Bram Moolenaar071d4272004-06-13 20:20:40 +00005298 else
5299 cmdwin_result = K_XF2;
5300 return;
5301 }
5302# endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005303
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005304 // Don't quit while editing the command line.
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005305 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005306 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005307 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005308 return;
5309 }
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005310
5311 if (before_quit_autocmds(curwin, TRUE, eap->forceit))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005312 return;
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005313
Bram Moolenaar071d4272004-06-13 20:20:40 +00005314 exiting = TRUE;
Bram Moolenaar027387f2016-01-02 22:25:52 +01005315 if (eap->forceit || !check_changed_any(FALSE, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005316 getout(0);
5317 not_exiting();
5318}
5319
Bram Moolenaar071d4272004-06-13 20:20:40 +00005320/*
5321 * ":close": close current window, unless it is the last one
5322 */
5323 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005324ex_close(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005325{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005326 win_T *win;
5327 int winnr = 0;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005328#ifdef FEAT_CMDWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00005329 if (cmdwin_type != 0)
Bram Moolenaar9bd1a7e2011-05-19 14:50:54 +02005330 cmdwin_result = Ctrl_C;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005331 else
Bram Moolenaar4033c552017-09-16 20:54:51 +02005332#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005333 if (!text_locked() && !curbuf_locked())
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005334 {
5335 if (eap->addr_count == 0)
5336 ex_win_close(eap->forceit, curwin, NULL);
Bram Moolenaar6d41c782018-06-06 09:11:12 +02005337 else
5338 {
Bram Moolenaar29323592016-07-24 22:04:11 +02005339 FOR_ALL_WINDOWS(win)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005340 {
5341 winnr++;
5342 if (winnr == eap->line2)
5343 break;
5344 }
5345 if (win == NULL)
5346 win = lastwin;
5347 ex_win_close(eap->forceit, win, NULL);
5348 }
5349 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005350}
5351
Bram Moolenaar4033c552017-09-16 20:54:51 +02005352#ifdef FEAT_QUICKFIX
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005353/*
5354 * ":pclose": Close any preview window.
5355 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005356 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005357ex_pclose(exarg_T *eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005358{
5359 win_T *win;
5360
Bram Moolenaar79648732019-07-18 21:43:07 +02005361 // First close any normal window.
Bram Moolenaar29323592016-07-24 22:04:11 +02005362 FOR_ALL_WINDOWS(win)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005363 if (win->w_p_pvw)
5364 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00005365 ex_win_close(eap->forceit, win, NULL);
Bram Moolenaar79648732019-07-18 21:43:07 +02005366 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005367 }
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01005368# ifdef FEAT_PROP_POPUP
Bram Moolenaar79648732019-07-18 21:43:07 +02005369 // Also when 'previewpopup' is empty, it might have been cleared.
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02005370 popup_close_preview();
Bram Moolenaar79648732019-07-18 21:43:07 +02005371# endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005372}
Bram Moolenaar4033c552017-09-16 20:54:51 +02005373#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005374
Bram Moolenaarf740b292006-02-16 22:11:02 +00005375/*
5376 * Close window "win" and take care of handling closing the last window for a
5377 * modified buffer.
5378 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005379 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005380ex_win_close(
5381 int forceit,
5382 win_T *win,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005383 tabpage_T *tp) // NULL or the tab page "win" is in
Bram Moolenaar071d4272004-06-13 20:20:40 +00005384{
5385 int need_hide;
5386 buf_T *buf = win->w_buffer;
5387
Bram Moolenaarcf844172020-06-26 19:44:06 +02005388 // Never close the autocommand window.
5389 if (win == aucmd_win)
5390 {
5391 emsg(_(e_autocmd_close));
5392 return;
5393 }
5394
Bram Moolenaar071d4272004-06-13 20:20:40 +00005395 need_hide = (bufIsChanged(buf) && buf->b_nwindows <= 1);
Bram Moolenaareb44a682017-08-03 22:44:55 +02005396 if (need_hide && !buf_hide(buf) && !forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005397 {
Bram Moolenaar4033c552017-09-16 20:54:51 +02005398#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005399 if ((p_confirm || cmdmod.confirm) && p_write)
5400 {
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005401 bufref_T bufref;
5402
5403 set_bufref(&bufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005404 dialog_changed(buf, FALSE);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005405 if (bufref_valid(&bufref) && bufIsChanged(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005406 return;
5407 need_hide = FALSE;
5408 }
5409 else
Bram Moolenaar4033c552017-09-16 20:54:51 +02005410#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005411 {
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02005412 no_write_message();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005413 return;
5414 }
5415 }
5416
Bram Moolenaar4033c552017-09-16 20:54:51 +02005417#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00005418 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005419#endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00005420
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005421 // free buffer when not hiding it or when it's a scratch buffer
Bram Moolenaarf740b292006-02-16 22:11:02 +00005422 if (tp == NULL)
Bram Moolenaareb44a682017-08-03 22:44:55 +02005423 win_close(win, !need_hide && !buf_hide(buf));
Bram Moolenaarf740b292006-02-16 22:11:02 +00005424 else
Bram Moolenaareb44a682017-08-03 22:44:55 +02005425 win_close_othertab(win, !need_hide && !buf_hide(buf), tp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005426}
5427
Bram Moolenaar071d4272004-06-13 20:20:40 +00005428/*
Bram Moolenaardea25702017-01-29 15:18:10 +01005429 * Handle the argument for a tabpage related ex command.
5430 * Returns a tabpage number.
5431 * When an error is encountered then eap->errmsg is set.
5432 */
5433 static int
5434get_tabpage_arg(exarg_T *eap)
5435{
5436 int tab_number;
5437 int unaccept_arg0 = (eap->cmdidx == CMD_tabmove) ? 0 : 1;
5438
5439 if (eap->arg && *eap->arg != NUL)
5440 {
5441 char_u *p = eap->arg;
5442 char_u *p_save;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005443 int relative = 0; // argument +N/-N means: go to N places to the
5444 // right/left relative to the current position.
Bram Moolenaardea25702017-01-29 15:18:10 +01005445
5446 if (*p == '-')
5447 {
5448 relative = -1;
5449 p++;
5450 }
5451 else if (*p == '+')
5452 {
5453 relative = 1;
5454 p++;
5455 }
5456
5457 p_save = p;
5458 tab_number = getdigits(&p);
5459
5460 if (relative == 0)
5461 {
5462 if (STRCMP(p, "$") == 0)
5463 tab_number = LAST_TAB_NR;
Bram Moolenaar94f4ffa2020-08-10 19:21:15 +02005464 else if (STRCMP(p, "#") == 0)
5465 if (valid_tabpage(lastused_tabpage))
5466 tab_number = tabpage_index(lastused_tabpage);
5467 else
5468 {
5469 eap->errmsg = ex_errmsg(e_invargval, eap->arg);
5470 tab_number = 0;
5471 goto theend;
5472 }
Bram Moolenaardea25702017-01-29 15:18:10 +01005473 else if (p == p_save || *p_save == '-' || *p != NUL
5474 || tab_number > LAST_TAB_NR)
5475 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005476 // No numbers as argument.
Bram Moolenaar8930caa2020-07-23 16:37:03 +02005477 eap->errmsg = ex_errmsg(e_invarg2, eap->arg);
Bram Moolenaardea25702017-01-29 15:18:10 +01005478 goto theend;
5479 }
5480 }
5481 else
5482 {
5483 if (*p_save == NUL)
5484 tab_number = 1;
5485 else if (p == p_save || *p_save == '-' || *p != NUL
5486 || tab_number == 0)
5487 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005488 // No numbers as argument.
Bram Moolenaar8930caa2020-07-23 16:37:03 +02005489 eap->errmsg = ex_errmsg(e_invarg2, eap->arg);
Bram Moolenaardea25702017-01-29 15:18:10 +01005490 goto theend;
5491 }
5492 tab_number = tab_number * relative + tabpage_index(curtab);
5493 if (!unaccept_arg0 && relative == -1)
5494 --tab_number;
5495 }
5496 if (tab_number < unaccept_arg0 || tab_number > LAST_TAB_NR)
Bram Moolenaar8930caa2020-07-23 16:37:03 +02005497 eap->errmsg = ex_errmsg(e_invarg2, eap->arg);
Bram Moolenaardea25702017-01-29 15:18:10 +01005498 }
5499 else if (eap->addr_count > 0)
5500 {
5501 if (unaccept_arg0 && eap->line2 == 0)
Bram Moolenaarc6251552017-01-29 21:42:20 +01005502 {
Bram Moolenaar8930caa2020-07-23 16:37:03 +02005503 eap->errmsg = _(e_invrange);
Bram Moolenaarc6251552017-01-29 21:42:20 +01005504 tab_number = 0;
5505 }
Bram Moolenaardea25702017-01-29 15:18:10 +01005506 else
5507 {
5508 tab_number = eap->line2;
Bram Moolenaar82a12462019-01-22 22:41:42 +01005509 if (!unaccept_arg0 && *skipwhite(*eap->cmdlinep) == '-')
Bram Moolenaardea25702017-01-29 15:18:10 +01005510 {
5511 --tab_number;
5512 if (tab_number < unaccept_arg0)
Bram Moolenaar8930caa2020-07-23 16:37:03 +02005513 eap->errmsg = _(e_invrange);
Bram Moolenaardea25702017-01-29 15:18:10 +01005514 }
5515 }
5516 }
5517 else
5518 {
5519 switch (eap->cmdidx)
5520 {
5521 case CMD_tabnext:
5522 tab_number = tabpage_index(curtab) + 1;
5523 if (tab_number > LAST_TAB_NR)
5524 tab_number = 1;
5525 break;
5526 case CMD_tabmove:
5527 tab_number = LAST_TAB_NR;
5528 break;
5529 default:
5530 tab_number = tabpage_index(curtab);
5531 }
5532 }
5533
5534theend:
5535 return tab_number;
5536}
5537
5538/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00005539 * ":tabclose": close current tab page, unless it is the last one.
5540 * ":tabclose N": close tab page N.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005541 */
5542 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005543ex_tabclose(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005544{
Bram Moolenaarf740b292006-02-16 22:11:02 +00005545 tabpage_T *tp;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005546 int tab_number;
Bram Moolenaarf740b292006-02-16 22:11:02 +00005547
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005548# ifdef FEAT_CMDWIN
5549 if (cmdwin_type != 0)
5550 cmdwin_result = K_IGNORE;
5551 else
5552# endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00005553 if (first_tabpage->tp_next == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005554 emsg(_("E784: Cannot close last tab page"));
Bram Moolenaarf740b292006-02-16 22:11:02 +00005555 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005556 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005557 tab_number = get_tabpage_arg(eap);
5558 if (eap->errmsg == NULL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005559 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005560 tp = find_tabpage(tab_number);
Bram Moolenaarf740b292006-02-16 22:11:02 +00005561 if (tp == NULL)
5562 {
5563 beep_flush();
5564 return;
5565 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005566 if (tp != curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00005567 {
5568 tabpage_close_other(tp, eap->forceit);
5569 return;
5570 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005571 else if (!text_locked() && !curbuf_locked())
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005572 tabpage_close(eap->forceit);
5573 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005574 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005575}
5576
5577/*
5578 * ":tabonly": close all tab pages except the current one
5579 */
5580 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005581ex_tabonly(exarg_T *eap)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005582{
5583 tabpage_T *tp;
5584 int done;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005585 int tab_number;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005586
5587# ifdef FEAT_CMDWIN
5588 if (cmdwin_type != 0)
5589 cmdwin_result = K_IGNORE;
5590 else
5591# endif
5592 if (first_tabpage->tp_next == NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01005593 msg(_("Already only one tab page"));
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005594 else
5595 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005596 tab_number = get_tabpage_arg(eap);
5597 if (eap->errmsg == NULL)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005598 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005599 goto_tabpage(tab_number);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005600 // Repeat this up to a 1000 times, because autocommands may
5601 // mess up the lists.
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005602 for (done = 0; done < 1000; ++done)
5603 {
5604 FOR_ALL_TABPAGES(tp)
5605 if (tp->tp_topframe != topframe)
5606 {
5607 tabpage_close_other(tp, eap->forceit);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005608 // if we failed to close it quit
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005609 if (valid_tabpage(tp))
5610 done = 1000;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005611 // start over, "tp" is now invalid
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005612 break;
5613 }
5614 if (first_tabpage->tp_next == NULL)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005615 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005616 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005617 }
5618 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005619}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005620
5621/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00005622 * Close the current tab page.
5623 */
5624 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005625tabpage_close(int forceit)
Bram Moolenaarf740b292006-02-16 22:11:02 +00005626{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005627 // First close all the windows but the current one. If that worked then
5628 // close the last window in this tab, that will close it.
Bram Moolenaar459ca562016-11-10 18:16:33 +01005629 if (!ONE_WINDOW)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005630 close_others(TRUE, forceit);
Bram Moolenaar459ca562016-11-10 18:16:33 +01005631 if (ONE_WINDOW)
Bram Moolenaarf740b292006-02-16 22:11:02 +00005632 ex_win_close(forceit, curwin, NULL);
5633# ifdef FEAT_GUI
5634 need_mouse_correct = TRUE;
5635# endif
5636}
5637
5638/*
5639 * Close tab page "tp", which is not the current tab page.
5640 * Note that autocommands may make "tp" invalid.
Bram Moolenaar7875acc2006-09-10 13:51:17 +00005641 * Also takes care of the tab pages line disappearing when closing the
5642 * last-but-one tab page.
Bram Moolenaarf740b292006-02-16 22:11:02 +00005643 */
5644 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005645tabpage_close_other(tabpage_T *tp, int forceit)
Bram Moolenaarf740b292006-02-16 22:11:02 +00005646{
5647 int done = 0;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005648 win_T *wp;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00005649 int h = tabline_height();
Bram Moolenaarf740b292006-02-16 22:11:02 +00005650
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005651 // Limit to 1000 windows, autocommands may add a window while we close
5652 // one. OK, so I'm paranoid...
Bram Moolenaarf740b292006-02-16 22:11:02 +00005653 while (++done < 1000)
5654 {
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005655 wp = tp->tp_firstwin;
5656 ex_win_close(forceit, wp, tp);
Bram Moolenaarf740b292006-02-16 22:11:02 +00005657
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005658 // Autocommands may delete the tab page under our fingers and we may
5659 // fail to close a window with a modified buffer.
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005660 if (!valid_tabpage(tp) || tp->tp_firstwin == wp)
Bram Moolenaarf740b292006-02-16 22:11:02 +00005661 break;
5662 }
Bram Moolenaar7875acc2006-09-10 13:51:17 +00005663
Bram Moolenaar29323592016-07-24 22:04:11 +02005664 apply_autocmds(EVENT_TABCLOSED, NULL, NULL, FALSE, curbuf);
Bram Moolenaar29323592016-07-24 22:04:11 +02005665
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005666 redraw_tabline = TRUE;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00005667 if (h != tabline_height())
5668 shell_new_rows();
Bram Moolenaarf740b292006-02-16 22:11:02 +00005669}
5670
5671/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005672 * ":only".
5673 */
5674 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005675ex_only(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005676{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005677 win_T *wp;
5678 int wnr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005679# ifdef FEAT_GUI
5680 need_mouse_correct = TRUE;
5681# endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005682 if (eap->addr_count > 0)
5683 {
5684 wnr = eap->line2;
5685 for (wp = firstwin; --wnr > 0; )
5686 {
5687 if (wp->w_next == NULL)
5688 break;
5689 else
5690 wp = wp->w_next;
5691 }
5692 win_goto(wp);
5693 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005694 close_others(TRUE, eap->forceit);
5695}
5696
Bram Moolenaar071d4272004-06-13 20:20:40 +00005697 static void
Bram Moolenaar5e1e6d22017-01-02 17:26:00 +01005698ex_hide(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005699{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005700 // ":hide" or ":hide | cmd": hide current window
Bram Moolenaar2256c992016-11-15 21:17:07 +01005701 if (!eap->skip)
5702 {
Bram Moolenaar4033c552017-09-16 20:54:51 +02005703#ifdef FEAT_GUI
Bram Moolenaar2256c992016-11-15 21:17:07 +01005704 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005705#endif
Bram Moolenaar2256c992016-11-15 21:17:07 +01005706 if (eap->addr_count == 0)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005707 win_close(curwin, FALSE); // don't free buffer
Bram Moolenaar2256c992016-11-15 21:17:07 +01005708 else
5709 {
5710 int winnr = 0;
5711 win_T *win;
Bram Moolenaarf240e182014-11-27 18:33:02 +01005712
Bram Moolenaar2256c992016-11-15 21:17:07 +01005713 FOR_ALL_WINDOWS(win)
5714 {
5715 winnr++;
5716 if (winnr == eap->line2)
5717 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005718 }
Bram Moolenaar2256c992016-11-15 21:17:07 +01005719 if (win == NULL)
5720 win = lastwin;
5721 win_close(win, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005722 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005723 }
5724}
5725
5726/*
5727 * ":stop" and ":suspend": Suspend Vim.
5728 */
5729 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005730ex_stop(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005731{
5732 /*
5733 * Disallow suspending for "rvim".
5734 */
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005735 if (!check_restricted())
Bram Moolenaar071d4272004-06-13 20:20:40 +00005736 {
5737 if (!eap->forceit)
5738 autowrite_all();
5739 windgoto((int)Rows - 1, 0);
5740 out_char('\n');
5741 out_flush();
5742 stoptermcap();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005743 out_flush(); // needed for SUN to restore xterm buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00005744#ifdef FEAT_TITLE
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005745 mch_restore_title(SAVE_RESTORE_BOTH); // restore window titles
Bram Moolenaar071d4272004-06-13 20:20:40 +00005746#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005747 ui_suspend(); // call machine specific function
Bram Moolenaar071d4272004-06-13 20:20:40 +00005748#ifdef FEAT_TITLE
5749 maketitle();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005750 resettitle(); // force updating the title
Bram Moolenaar071d4272004-06-13 20:20:40 +00005751#endif
5752 starttermcap();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005753 scroll_start(); // scroll screen before redrawing
Bram Moolenaar071d4272004-06-13 20:20:40 +00005754 redraw_later_clear();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005755 shell_resized(); // may have resized window
Bram Moolenaar071d4272004-06-13 20:20:40 +00005756 }
5757}
5758
5759/*
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005760 * ":exit", ":xit" and ":wq": Write file and quite the current window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005761 */
5762 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005763ex_exit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005764{
Bram Moolenaar461f2122020-07-28 20:25:47 +02005765#ifdef FEAT_EVAL
Bram Moolenaarae616492020-07-28 20:07:27 +02005766 if (not_in_vim9(eap) == FAIL)
5767 return;
Bram Moolenaar461f2122020-07-28 20:25:47 +02005768#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005769#ifdef FEAT_CMDWIN
5770 if (cmdwin_type != 0)
5771 {
5772 cmdwin_result = Ctrl_C;
5773 return;
5774 }
5775#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005776 // Don't quit while editing the command line.
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005777 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005778 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005779 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005780 return;
5781 }
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005782
5783 if (before_quit_autocmds(curwin, FALSE, eap->forceit))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005784 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005785
5786 /*
5787 * if more files or windows we won't exit
5788 */
5789 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
5790 exiting = TRUE;
5791 if ( ((eap->cmdidx == CMD_wq
5792 || curbufIsChanged())
5793 && do_write(eap) == FAIL)
5794 || check_more(TRUE, eap->forceit) == FAIL
Bram Moolenaar027387f2016-01-02 22:25:52 +01005795 || (only_one_window() && check_changed_any(eap->forceit, FALSE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005796 {
5797 not_exiting();
5798 }
5799 else
5800 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005801 if (only_one_window()) // quit last window, exit Vim
Bram Moolenaar071d4272004-06-13 20:20:40 +00005802 getout(0);
Bram Moolenaar2c33d7b2017-10-14 16:06:20 +02005803 not_exiting();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005804# ifdef FEAT_GUI
5805 need_mouse_correct = TRUE;
5806# endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005807 // Quit current window, may free the buffer.
Bram Moolenaareb44a682017-08-03 22:44:55 +02005808 win_close(curwin, !buf_hide(curwin->w_buffer));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005809 }
5810}
5811
5812/*
5813 * ":print", ":list", ":number".
5814 */
5815 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005816ex_print(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005817{
Bram Moolenaardf177f62005-02-22 08:39:57 +00005818 if (curbuf->b_ml.ml_flags & ML_EMPTY)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005819 emsg(_(e_emptybuf));
Bram Moolenaardf177f62005-02-22 08:39:57 +00005820 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005821 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00005822 for ( ;!got_int; ui_breakcheck())
5823 {
5824 print_line(eap->line1,
5825 (eap->cmdidx == CMD_number || eap->cmdidx == CMD_pound
5826 || (eap->flags & EXFLAG_NR)),
5827 eap->cmdidx == CMD_list || (eap->flags & EXFLAG_LIST));
5828 if (++eap->line1 > eap->line2)
5829 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005830 out_flush(); // show one line at a time
Bram Moolenaardf177f62005-02-22 08:39:57 +00005831 }
5832 setpcmark();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005833 // put cursor at last line
Bram Moolenaardf177f62005-02-22 08:39:57 +00005834 curwin->w_cursor.lnum = eap->line2;
5835 beginline(BL_SOL | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005836 }
5837
Bram Moolenaar071d4272004-06-13 20:20:40 +00005838 ex_no_reprint = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005839}
5840
5841#ifdef FEAT_BYTEOFF
5842 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005843ex_goto(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005844{
5845 goto_byte(eap->line2);
5846}
5847#endif
5848
5849/*
5850 * ":shell".
5851 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005852 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005853ex_shell(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005854{
5855 do_shell(NULL, 0);
5856}
5857
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005858#if defined(HAVE_DROP_FILE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005859
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005860static int drop_busy = FALSE;
5861static int drop_filec;
5862static char_u **drop_filev = NULL;
5863static int drop_split;
5864static void (*drop_callback)(void *);
5865static void *drop_cookie;
5866
5867 static void
5868handle_drop_internal(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005869{
5870 exarg_T ea;
5871 int save_msg_scroll = msg_scroll;
5872
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005873 // Setting the argument list may cause screen updates and being called
5874 // recursively. Avoid that by setting drop_busy.
5875 drop_busy = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005876
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005877 // Check whether the current buffer is changed. If so, we will need
5878 // to split the current window or data could be lost.
5879 // We don't need to check if the 'hidden' option is set, as in this
5880 // case the buffer won't be lost.
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005881 if (!buf_hide(curbuf) && !drop_split)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005882 {
5883 ++emsg_off;
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005884 drop_split = check_changed(curbuf, CCGD_AW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005885 --emsg_off;
5886 }
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005887 if (drop_split)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005888 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005889 if (win_split(0, 0) == FAIL)
5890 return;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02005891 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005892
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005893 // When splitting the window, create a new alist. Otherwise the
5894 // existing one is overwritten.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005895 alist_unlink(curwin->w_alist);
5896 alist_new();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005897 }
5898
5899 /*
5900 * Set up the new argument list.
5901 */
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005902 alist_set(ALIST(curwin), drop_filec, drop_filev, FALSE, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005903
5904 /*
5905 * Move to the first file.
5906 */
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005907 // Fake up a minimal "next" command for do_argfile()
Bram Moolenaara80faa82020-04-12 19:37:17 +02005908 CLEAR_FIELD(ea);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005909 ea.cmd = (char_u *)"next";
5910 do_argfile(&ea, 0);
5911
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005912 // do_ecmd() may set need_start_insertmode, but since we never left Insert
5913 // mode that is not needed here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005914 need_start_insertmode = FALSE;
5915
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005916 // Restore msg_scroll, otherwise a following command may cause scrolling
5917 // unexpectedly. The screen will be redrawn by the caller, thus
5918 // msg_scroll being set by displaying a message is irrelevant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005919 msg_scroll = save_msg_scroll;
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005920
5921 if (drop_callback != NULL)
5922 drop_callback(drop_cookie);
5923
5924 drop_filev = NULL;
5925 drop_busy = FALSE;
5926}
5927
5928/*
5929 * Handle a file drop. The code is here because a drop is *nearly* like an
5930 * :args command, but not quite (we have a list of exact filenames, so we
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01005931 * don't want to (a) parse a command line, or (b) expand wildcards). So the
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005932 * code is very similar to :args and hence needs access to a lot of the static
5933 * functions in this file.
5934 *
5935 * The "filev" list must have been allocated using alloc(), as should each item
5936 * in the list. This function takes over responsibility for freeing the "filev"
5937 * list.
5938 */
5939 void
5940handle_drop(
5941 int filec, // the number of files dropped
5942 char_u **filev, // the list of files dropped
5943 int split, // force splitting the window
5944 void (*callback)(void *), // to be called after setting the argument
5945 // list
5946 void *cookie) // argument for "callback" (allocated)
5947{
5948 // Cannot handle recursive drops, finish the pending one.
5949 if (drop_busy)
5950 {
5951 FreeWild(filec, filev);
5952 vim_free(cookie);
5953 return;
5954 }
5955
5956 // When calling handle_drop() more than once in a row we only use the last
5957 // one.
5958 if (drop_filev != NULL)
5959 {
5960 FreeWild(drop_filec, drop_filev);
5961 vim_free(drop_cookie);
5962 }
5963
5964 drop_filec = filec;
5965 drop_filev = filev;
5966 drop_split = split;
5967 drop_callback = callback;
5968 drop_cookie = cookie;
5969
5970 // Postpone this when:
5971 // - editing the command line
5972 // - not possible to change the current buffer
5973 // - updating the screen
5974 // As it may change buffers and window structures that are in use and cause
5975 // freed memory to be used.
5976 if (text_locked() || curbuf_locked() || updating_screen)
5977 return;
5978
5979 handle_drop_internal();
5980}
5981
5982/*
5983 * To be called when text is unlocked, curbuf is unlocked or updating_screen is
5984 * reset: Handle a postponed drop.
5985 */
5986 void
5987handle_any_postponed_drop(void)
5988{
5989 if (!drop_busy && drop_filev != NULL
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +02005990 && !text_locked() && !curbuf_locked() && !updating_screen)
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005991 handle_drop_internal();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005992}
5993#endif
5994
Bram Moolenaar071d4272004-06-13 20:20:40 +00005995/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005996 * ":preserve".
5997 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005998 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005999ex_preserve(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006000{
Bram Moolenaar4399ef42005-02-12 14:29:27 +00006001 curbuf->b_flags |= BF_PRESERVED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006002 ml_preserve(curbuf, TRUE);
6003}
6004
6005/*
6006 * ":recover".
6007 */
6008 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006009ex_recover(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006010{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006011 // Set recoverymode right away to avoid the ATTENTION prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006012 recoverymode = TRUE;
Bram Moolenaar45d3b142013-11-09 03:31:51 +01006013 if (!check_changed(curbuf, (p_awa ? CCGD_AW : 0)
6014 | CCGD_MULTWIN
6015 | (eap->forceit ? CCGD_FORCEIT : 0)
6016 | CCGD_EXCMD)
6017
Bram Moolenaar071d4272004-06-13 20:20:40 +00006018 && (*eap->arg == NUL
6019 || setfname(curbuf, eap->arg, NULL, TRUE) == OK))
Bram Moolenaar99499b12019-05-23 21:35:48 +02006020 ml_recover(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006021 recoverymode = FALSE;
6022}
6023
6024/*
6025 * Command modifier used in a wrong way.
6026 */
6027 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006028ex_wrongmodifier(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006029{
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006030 eap->errmsg = _(e_invalid_command);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006031}
6032
Bram Moolenaar071d4272004-06-13 20:20:40 +00006033/*
6034 * :sview [+command] file split window with new file, read-only
6035 * :split [[+command] file] split window with current or new file
6036 * :vsplit [[+command] file] split window vertically with current or new file
6037 * :new [[+command] file] split window with no or new file
6038 * :vnew [[+command] file] split vertically window with no or new file
6039 * :sfind [+command] file split window with file in 'path'
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006040 *
6041 * :tabedit open new Tab page with empty window
6042 * :tabedit [+command] file open new Tab page and edit "file"
6043 * :tabnew [[+command] file] just like :tabedit
6044 * :tabfind [+command] file open new Tab page and find "file"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006045 */
6046 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006047ex_splitview(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006048{
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006049 win_T *old_curwin = curwin;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006050#if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006051 char_u *fname = NULL;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006052#endif
6053#ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00006054 int browse_flag = cmdmod.browse;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006055#endif
Bram Moolenaar39902a02018-06-21 22:10:08 +02006056 int use_tab = eap->cmdidx == CMD_tabedit
6057 || eap->cmdidx == CMD_tabfind
6058 || eap->cmdidx == CMD_tabnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006059
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01006060 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02006061 return;
6062
Bram Moolenaar4033c552017-09-16 20:54:51 +02006063#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00006064 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006065#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006066
Bram Moolenaar4033c552017-09-16 20:54:51 +02006067#ifdef FEAT_QUICKFIX
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006068 // A ":split" in the quickfix window works like ":new". Don't want two
6069 // quickfix windows. But it's OK when doing ":tab split".
Bram Moolenaar05bb9532008-07-04 09:44:11 +00006070 if (bt_quickfix(curbuf) && cmdmod.tab == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006071 {
6072 if (eap->cmdidx == CMD_split)
6073 eap->cmdidx = CMD_new;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006074 if (eap->cmdidx == CMD_vsplit)
6075 eap->cmdidx = CMD_vnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006076 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02006077#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006078
Bram Moolenaar4033c552017-09-16 20:54:51 +02006079#ifdef FEAT_SEARCHPATH
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006080 if (eap->cmdidx == CMD_sfind || eap->cmdidx == CMD_tabfind)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006081 {
6082 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg),
6083 FNAME_MESS, TRUE, curbuf->b_ffname);
6084 if (fname == NULL)
6085 goto theend;
6086 eap->arg = fname;
6087 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006088# ifdef FEAT_BROWSE
Bram Moolenaar4033c552017-09-16 20:54:51 +02006089 else
6090# endif
6091#endif
6092#ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00006093 if (cmdmod.browse
Bram Moolenaar071d4272004-06-13 20:20:40 +00006094 && eap->cmdidx != CMD_vnew
Bram Moolenaar071d4272004-06-13 20:20:40 +00006095 && eap->cmdidx != CMD_new)
6096 {
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006097 if (
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006098# ifdef FEAT_GUI
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006099 !gui.in_use &&
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006100# endif
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006101 au_has_group((char_u *)"FileExplorer"))
6102 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006103 // No browsing supported but we do have the file explorer:
6104 // Edit the directory.
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006105 if (*eap->arg == NUL || !mch_isdir(eap->arg))
6106 eap->arg = (char_u *)".";
6107 }
6108 else
6109 {
Bram Moolenaar39902a02018-06-21 22:10:08 +02006110 fname = do_browse(0, (char_u *)(use_tab
6111 ? _("Edit File in new tab page")
6112 : _("Edit File in new window")),
Bram Moolenaar071d4272004-06-13 20:20:40 +00006113 eap->arg, NULL, NULL, NULL, curbuf);
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006114 if (fname == NULL)
6115 goto theend;
6116 eap->arg = fname;
6117 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006118 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006119 cmdmod.browse = FALSE; // Don't browse again in do_ecmd().
Bram Moolenaar4033c552017-09-16 20:54:51 +02006120#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006121
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006122 /*
6123 * Either open new tab page or split the window.
6124 */
Bram Moolenaar39902a02018-06-21 22:10:08 +02006125 if (use_tab)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006126 {
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006127 if (win_new_tabpage(cmdmod.tab != 0 ? cmdmod.tab
6128 : eap->addr_count == 0 ? 0
6129 : (int)eap->line2 + 1) != FAIL)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006130 {
Bram Moolenaard2b66012008-01-09 19:30:36 +00006131 do_exedit(eap, old_curwin);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006132
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006133 // set the alternate buffer for the window we came from
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006134 if (curwin != old_curwin
6135 && win_valid(old_curwin)
6136 && old_curwin->w_buffer != curbuf
6137 && !cmdmod.keepalt)
6138 old_curwin->w_alt_fnum = curbuf->b_fnum;
6139 }
6140 }
6141 else if (win_split(eap->addr_count > 0 ? (int)eap->line2 : 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006142 *eap->cmd == 'v' ? WSP_VERT : 0) != FAIL)
6143 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006144 // Reset 'scrollbind' when editing another file, but keep it when
6145 // doing ":split" without arguments.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006146 if (*eap->arg != NUL
Bram Moolenaar8a3bb562018-03-04 20:14:14 +01006147# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00006148 || cmdmod.browse
Bram Moolenaar8a3bb562018-03-04 20:14:14 +01006149# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006150 )
Bram Moolenaar3368ea22010-09-21 16:56:35 +02006151 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006152 else
6153 do_check_scrollbind(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006154 do_exedit(eap, old_curwin);
6155 }
6156
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006157# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00006158 cmdmod.browse = browse_flag;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006159# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006160
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006161# if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006162theend:
6163 vim_free(fname);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006164# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006165}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006166
6167/*
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006168 * Open a new tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006169 */
6170 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006171tabpage_new(void)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006172{
6173 exarg_T ea;
6174
Bram Moolenaara80faa82020-04-12 19:37:17 +02006175 CLEAR_FIELD(ea);
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006176 ea.cmdidx = CMD_tabnew;
6177 ea.cmd = (char_u *)"tabn";
6178 ea.arg = (char_u *)"";
6179 ex_splitview(&ea);
6180}
6181
6182/*
6183 * :tabnext command
6184 */
6185 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006186ex_tabnext(exarg_T *eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006187{
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006188 int tab_number;
6189
Bram Moolenaar8cdbd5b2019-06-16 15:50:45 +02006190 if (ERROR_IF_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02006191 return;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006192 switch (eap->cmdidx)
6193 {
6194 case CMD_tabfirst:
6195 case CMD_tabrewind:
6196 goto_tabpage(1);
6197 break;
6198 case CMD_tablast:
6199 goto_tabpage(9999);
6200 break;
6201 case CMD_tabprevious:
6202 case CMD_tabNext:
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006203 if (eap->arg && *eap->arg != NUL)
6204 {
6205 char_u *p = eap->arg;
6206 char_u *p_save = p;
6207
6208 tab_number = getdigits(&p);
6209 if (p == p_save || *p_save == '-' || *p != NUL
6210 || tab_number == 0)
6211 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006212 // No numbers as argument.
Bram Moolenaar8930caa2020-07-23 16:37:03 +02006213 eap->errmsg = ex_errmsg(e_invarg2, eap->arg);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006214 return;
6215 }
6216 }
6217 else
6218 {
6219 if (eap->addr_count == 0)
6220 tab_number = 1;
6221 else
6222 {
6223 tab_number = eap->line2;
6224 if (tab_number < 1)
6225 {
Bram Moolenaar8930caa2020-07-23 16:37:03 +02006226 eap->errmsg = _(e_invrange);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006227 return;
6228 }
6229 }
6230 }
6231 goto_tabpage(-tab_number);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006232 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006233 default: // CMD_tabnext
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006234 tab_number = get_tabpage_arg(eap);
6235 if (eap->errmsg == NULL)
6236 goto_tabpage(tab_number);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006237 break;
6238 }
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006239}
6240
6241/*
6242 * :tabmove command
6243 */
6244 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006245ex_tabmove(exarg_T *eap)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006246{
Bram Moolenaar40ce3a42015-04-21 18:08:39 +02006247 int tab_number;
Bram Moolenaar8cb8dca2012-07-06 18:27:39 +02006248
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006249 tab_number = get_tabpage_arg(eap);
6250 if (eap->errmsg == NULL)
6251 tabpage_move(tab_number);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006252}
6253
6254/*
6255 * :tabs command: List tabs and their contents.
6256 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006257 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006258ex_tabs(exarg_T *eap UNUSED)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006259{
6260 tabpage_T *tp;
6261 win_T *wp;
6262 int tabcount = 1;
6263
6264 msg_start();
6265 msg_scroll = TRUE;
6266 for (tp = first_tabpage; tp != NULL && !got_int; tp = tp->tp_next)
6267 {
6268 msg_putchar('\n');
6269 vim_snprintf((char *)IObuff, IOSIZE, _("Tab page %d"), tabcount++);
Bram Moolenaar8820b482017-03-16 17:23:31 +01006270 msg_outtrans_attr(IObuff, HL_ATTR(HLF_T));
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006271 out_flush(); // output one line at a time
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006272 ui_breakcheck();
6273
Bram Moolenaar030f0df2006-02-21 22:02:53 +00006274 if (tp == curtab)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006275 wp = firstwin;
6276 else
6277 wp = tp->tp_firstwin;
6278 for ( ; wp != NULL && !got_int; wp = wp->w_next)
6279 {
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006280 msg_putchar('\n');
6281 msg_putchar(wp == curwin ? '>' : ' ');
6282 msg_putchar(' ');
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006283 msg_putchar(bufIsChanged(wp->w_buffer) ? '+' : ' ');
6284 msg_putchar(' ');
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006285 if (buf_spname(wp->w_buffer) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02006286 vim_strncpy(IObuff, buf_spname(wp->w_buffer), IOSIZE - 1);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006287 else
6288 home_replace(wp->w_buffer, wp->w_buffer->b_fname,
6289 IObuff, IOSIZE, TRUE);
6290 msg_outtrans(IObuff);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006291 out_flush(); // output one line at a time
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006292 ui_breakcheck();
6293 }
6294 }
6295}
6296
Bram Moolenaar071d4272004-06-13 20:20:40 +00006297/*
6298 * ":mode": Set screen mode.
6299 * If no argument given, just get the screen size and redraw.
6300 */
6301 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006302ex_mode(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006303{
6304 if (*eap->arg == NUL)
6305 shell_resized();
6306 else
Bram Moolenaar3c71aec2020-01-17 19:32:20 +01006307 emsg(_(e_screenmode));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006308}
6309
Bram Moolenaar071d4272004-06-13 20:20:40 +00006310/*
6311 * ":resize".
6312 * set, increment or decrement current window height
6313 */
6314 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006315ex_resize(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006316{
6317 int n;
6318 win_T *wp = curwin;
6319
6320 if (eap->addr_count > 0)
6321 {
6322 n = eap->line2;
6323 for (wp = firstwin; wp->w_next != NULL && --n > 0; wp = wp->w_next)
6324 ;
6325 }
6326
Bram Moolenaar44a2f922016-03-19 22:11:51 +01006327# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00006328 need_mouse_correct = TRUE;
Bram Moolenaar44a2f922016-03-19 22:11:51 +01006329# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006330 n = atol((char *)eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006331 if (cmdmod.split & WSP_VERT)
6332 {
6333 if (*eap->arg == '-' || *eap->arg == '+')
Bram Moolenaar02631462017-09-22 15:20:32 +02006334 n += curwin->w_width;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006335 else if (n == 0 && eap->arg[0] == NUL) // default is very wide
Bram Moolenaar071d4272004-06-13 20:20:40 +00006336 n = 9999;
6337 win_setwidth_win((int)n, wp);
6338 }
6339 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006340 {
6341 if (*eap->arg == '-' || *eap->arg == '+')
6342 n += curwin->w_height;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006343 else if (n == 0 && eap->arg[0] == NUL) // default is very high
Bram Moolenaar071d4272004-06-13 20:20:40 +00006344 n = 9999;
6345 win_setheight_win((int)n, wp);
6346 }
6347}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006348
6349/*
6350 * ":find [+command] <file>" command.
6351 */
6352 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006353ex_find(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006354{
6355#ifdef FEAT_SEARCHPATH
6356 char_u *fname;
6357 int count;
6358
6359 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg), FNAME_MESS,
6360 TRUE, curbuf->b_ffname);
6361 if (eap->addr_count > 0)
6362 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006363 // Repeat finding the file "count" times. This matters when it
6364 // appears several times in the path.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006365 count = eap->line2;
6366 while (fname != NULL && --count > 0)
6367 {
6368 vim_free(fname);
6369 fname = find_file_in_path(NULL, 0, FNAME_MESS,
6370 FALSE, curbuf->b_ffname);
6371 }
6372 }
6373
6374 if (fname != NULL)
6375 {
6376 eap->arg = fname;
6377#endif
6378 do_exedit(eap, NULL);
6379#ifdef FEAT_SEARCHPATH
6380 vim_free(fname);
6381 }
6382#endif
6383}
6384
6385/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00006386 * ":open" simulation: for now just work like ":visual".
6387 */
6388 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006389ex_open(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00006390{
6391 regmatch_T regmatch;
6392 char_u *p;
6393
6394 curwin->w_cursor.lnum = eap->line2;
6395 beginline(BL_SOL | BL_FIX);
6396 if (*eap->arg == '/')
6397 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006398 // ":open /pattern/": put cursor in column found with pattern
Bram Moolenaardf177f62005-02-22 08:39:57 +00006399 ++eap->arg;
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006400 p = skip_regexp(eap->arg, '/', p_magic);
Bram Moolenaardf177f62005-02-22 08:39:57 +00006401 *p = NUL;
6402 regmatch.regprog = vim_regcomp(eap->arg, p_magic ? RE_MAGIC : 0);
6403 if (regmatch.regprog != NULL)
6404 {
6405 regmatch.rm_ic = p_ic;
6406 p = ml_get_curline();
6407 if (vim_regexec(&regmatch, p, (colnr_T)0))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006408 curwin->w_cursor.col = (colnr_T)(regmatch.startp[0] - p);
Bram Moolenaardf177f62005-02-22 08:39:57 +00006409 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006410 emsg(_(e_nomatch));
Bram Moolenaar473de612013-06-08 18:19:48 +02006411 vim_regfree(regmatch.regprog);
Bram Moolenaardf177f62005-02-22 08:39:57 +00006412 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006413 // Move to the NUL, ignore any other arguments.
Bram Moolenaardf177f62005-02-22 08:39:57 +00006414 eap->arg += STRLEN(eap->arg);
6415 }
6416 check_cursor();
6417
6418 eap->cmdidx = CMD_visual;
6419 do_exedit(eap, NULL);
6420}
6421
6422/*
6423 * ":edit", ":badd", ":visual".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006424 */
6425 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006426ex_edit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006427{
6428 do_exedit(eap, NULL);
6429}
6430
6431/*
Bram Moolenaar4b96df52020-01-26 22:00:26 +01006432 * ":edit <file>" command and alike.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006433 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006434 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006435do_exedit(
6436 exarg_T *eap,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006437 win_T *old_curwin) // curwin before doing a split or NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006438{
6439 int n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006440 int need_hide;
Bram Moolenaardf177f62005-02-22 08:39:57 +00006441 int exmode_was = exmode_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006442
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01006443 if ((eap->cmdidx != CMD_pedit && ERROR_IF_POPUP_WINDOW)
6444 || ERROR_IF_TERM_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02006445 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006446 /*
6447 * ":vi" command ends Ex mode.
6448 */
6449 if (exmode_active && (eap->cmdidx == CMD_visual
6450 || eap->cmdidx == CMD_view))
6451 {
6452 exmode_active = FALSE;
Bram Moolenaar158ea172020-06-18 17:28:39 +02006453 ex_pressedreturn = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006454 if (*eap->arg == NUL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00006455 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006456 // Special case: ":global/pat/visual\NLvi-commands"
Bram Moolenaardf177f62005-02-22 08:39:57 +00006457 if (global_busy)
6458 {
6459 int rd = RedrawingDisabled;
6460 int nwr = no_wait_return;
6461 int ms = msg_scroll;
6462#ifdef FEAT_GUI
6463 int he = hold_gui_events;
6464#endif
6465
6466 if (eap->nextcmd != NULL)
6467 {
6468 stuffReadbuff(eap->nextcmd);
6469 eap->nextcmd = NULL;
6470 }
6471
6472 if (exmode_was != EXMODE_VIM)
6473 settmode(TMODE_RAW);
6474 RedrawingDisabled = 0;
6475 no_wait_return = 0;
6476 need_wait_return = FALSE;
6477 msg_scroll = 0;
6478#ifdef FEAT_GUI
6479 hold_gui_events = 0;
6480#endif
6481 must_redraw = CLEAR;
Bram Moolenaar9e2bcb52020-02-18 21:33:00 +01006482 pending_exmode_active = TRUE;
Bram Moolenaardf177f62005-02-22 08:39:57 +00006483
6484 main_loop(FALSE, TRUE);
6485
Bram Moolenaar9e2bcb52020-02-18 21:33:00 +01006486 pending_exmode_active = FALSE;
Bram Moolenaardf177f62005-02-22 08:39:57 +00006487 RedrawingDisabled = rd;
6488 no_wait_return = nwr;
6489 msg_scroll = ms;
6490#ifdef FEAT_GUI
6491 hold_gui_events = he;
6492#endif
6493 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006494 return;
Bram Moolenaardf177f62005-02-22 08:39:57 +00006495 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006496 }
6497
6498 if ((eap->cmdidx == CMD_new
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006499 || eap->cmdidx == CMD_tabnew
6500 || eap->cmdidx == CMD_tabedit
Bram Moolenaar4033c552017-09-16 20:54:51 +02006501 || eap->cmdidx == CMD_vnew) && *eap->arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006502 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006503 // ":new" or ":tabnew" without argument: edit an new empty buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00006504 setpcmark();
6505 (void)do_ecmd(0, NULL, NULL, eap, ECMD_ONE,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00006506 ECMD_HIDE + (eap->forceit ? ECMD_FORCEIT : 0),
6507 old_curwin == NULL ? curwin : NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006508 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02006509 else if ((eap->cmdidx != CMD_split && eap->cmdidx != CMD_vsplit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006510 || *eap->arg != NUL
6511#ifdef FEAT_BROWSE
6512 || cmdmod.browse
6513#endif
6514 )
6515 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006516 // Can't edit another file when "curbuf_lock" is set. Only ":edit"
6517 // can bring us here, others are stopped earlier.
Bram Moolenaar5555acc2006-04-07 21:33:12 +00006518 if (*eap->arg != NUL && curbuf_locked())
6519 return;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006520
Bram Moolenaar071d4272004-06-13 20:20:40 +00006521 n = readonlymode;
6522 if (eap->cmdidx == CMD_view || eap->cmdidx == CMD_sview)
6523 readonlymode = TRUE;
6524 else if (eap->cmdidx == CMD_enew)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006525 readonlymode = FALSE; // 'readonly' doesn't make sense in an
6526 // empty buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00006527 setpcmark();
6528 if (do_ecmd(0, (eap->cmdidx == CMD_enew ? NULL : eap->arg),
6529 NULL, eap,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006530 // ":edit" goes to first line if Vi compatible
Bram Moolenaar071d4272004-06-13 20:20:40 +00006531 (*eap->arg == NUL && eap->do_ecmd_lnum == 0
6532 && vim_strchr(p_cpo, CPO_GOTO1) != NULL)
6533 ? ECMD_ONE : eap->do_ecmd_lnum,
Bram Moolenaareb44a682017-08-03 22:44:55 +02006534 (buf_hide(curbuf) ? ECMD_HIDE : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006535 + (eap->forceit ? ECMD_FORCEIT : 0)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006536 // after a split we can use an existing buffer
Bram Moolenaar7b449342014-03-25 13:03:48 +01006537 + (old_curwin != NULL ? ECMD_OLDBUF : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006538 + (eap->cmdidx == CMD_badd ? ECMD_ADDBUF : 0 )
Bram Moolenaar701f7af2008-11-15 13:12:07 +00006539 , old_curwin == NULL ? curwin : NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006540 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006541 // Editing the file failed. If the window was split, close it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006542 if (old_curwin != NULL)
6543 {
6544 need_hide = (curbufIsChanged() && curbuf->b_nwindows <= 1);
Bram Moolenaareb44a682017-08-03 22:44:55 +02006545 if (!need_hide || buf_hide(curbuf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006546 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006547#if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00006548 cleanup_T cs;
6549
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006550 // Reset the error/interrupt/exception state here so that
6551 // aborting() returns FALSE when closing a window.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00006552 enter_cleanup(&cs);
Bram Moolenaar4033c552017-09-16 20:54:51 +02006553#endif
6554#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00006555 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006556#endif
Bram Moolenaareb44a682017-08-03 22:44:55 +02006557 win_close(curwin, !need_hide && !buf_hide(curbuf));
Bram Moolenaarc0197e22004-09-13 20:26:32 +00006558
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006559#if defined(FEAT_EVAL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006560 // Restore the error/interrupt/exception state if not
6561 // discarded by a new aborting error, interrupt, or
6562 // uncaught exception.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00006563 leave_cleanup(&cs);
Bram Moolenaar4033c552017-09-16 20:54:51 +02006564#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006565 }
6566 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006567 }
6568 else if (readonlymode && curbuf->b_nwindows == 1)
6569 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006570 // When editing an already visited buffer, 'readonly' won't be set
6571 // but the previous value is kept. With ":view" and ":sview" we
6572 // want the file to be readonly, except when another window is
6573 // editing the same buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006574 curbuf->b_p_ro = TRUE;
6575 }
6576 readonlymode = n;
6577 }
6578 else
6579 {
6580 if (eap->do_ecmd_cmd != NULL)
6581 do_cmdline_cmd(eap->do_ecmd_cmd);
6582#ifdef FEAT_TITLE
6583 n = curwin->w_arg_idx_invalid;
6584#endif
6585 check_arg_idx(curwin);
6586#ifdef FEAT_TITLE
6587 if (n != curwin->w_arg_idx_invalid)
6588 maketitle();
6589#endif
6590 }
6591
Bram Moolenaar071d4272004-06-13 20:20:40 +00006592 /*
6593 * if ":split file" worked, set alternate file name in old window to new
6594 * file
6595 */
6596 if (old_curwin != NULL
6597 && *eap->arg != NUL
6598 && curwin != old_curwin
6599 && win_valid(old_curwin)
Bram Moolenaard4755bb2004-09-02 19:12:26 +00006600 && old_curwin->w_buffer != curbuf
6601 && !cmdmod.keepalt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006602 old_curwin->w_alt_fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006603
6604 ex_no_reprint = TRUE;
6605}
6606
6607#ifndef FEAT_GUI
6608/*
6609 * ":gui" and ":gvim" when there is no GUI.
6610 */
6611 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006612ex_nogui(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006613{
Bram Moolenaar8930caa2020-07-23 16:37:03 +02006614 eap->errmsg = _(e_nogvim);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006615}
6616#endif
6617
Bram Moolenaar4f974752019-02-17 17:44:42 +01006618#if defined(FEAT_GUI_MSWIN) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006619 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006620ex_tearoff(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006621{
6622 gui_make_tearoff(eap->arg);
6623}
6624#endif
6625
Bram Moolenaar29a2c082018-03-05 21:06:23 +01006626#if (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK) \
6627 || defined(FEAT_TERM_POPUP_MENU)) && defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006628 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006629ex_popup(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006630{
Bram Moolenaar29a2c082018-03-05 21:06:23 +01006631# if defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK)
6632 if (gui.in_use)
6633 gui_make_popup(eap->arg, eap->forceit);
6634# ifdef FEAT_TERM_POPUP_MENU
6635 else
6636# endif
6637# endif
6638# ifdef FEAT_TERM_POPUP_MENU
6639 pum_make_popup(eap->arg, eap->forceit);
6640# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006641}
6642#endif
6643
Bram Moolenaar071d4272004-06-13 20:20:40 +00006644 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006645ex_swapname(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006646{
6647 if (curbuf->b_ml.ml_mfp == NULL || curbuf->b_ml.ml_mfp->mf_fname == NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01006648 msg(_("No swap file"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006649 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01006650 msg((char *)curbuf->b_ml.ml_mfp->mf_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006651}
6652
6653/*
6654 * ":syncbind" forces all 'scrollbind' windows to have the same relative
6655 * offset.
6656 * (1998-11-02 16:21:01 R. Edward Ralston <eralston@computer.org>)
6657 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006658 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006659ex_syncbind(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006660{
Bram Moolenaar071d4272004-06-13 20:20:40 +00006661 win_T *wp;
Bram Moolenaardedd1b02013-12-14 13:06:17 +01006662 win_T *save_curwin = curwin;
6663 buf_T *save_curbuf = curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006664 long topline;
6665 long y;
6666 linenr_T old_linenr = curwin->w_cursor.lnum;
6667
6668 setpcmark();
6669
6670 /*
6671 * determine max topline
6672 */
6673 if (curwin->w_p_scb)
6674 {
6675 topline = curwin->w_topline;
Bram Moolenaar29323592016-07-24 22:04:11 +02006676 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006677 {
6678 if (wp->w_p_scb && wp->w_buffer)
6679 {
Bram Moolenaar375e3392019-01-31 18:26:10 +01006680 y = wp->w_buffer->b_ml.ml_line_count - get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006681 if (topline > y)
6682 topline = y;
6683 }
6684 }
6685 if (topline < 1)
6686 topline = 1;
6687 }
6688 else
6689 {
6690 topline = 1;
6691 }
6692
6693
6694 /*
Bram Moolenaardedd1b02013-12-14 13:06:17 +01006695 * Set all scrollbind windows to the same topline.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006696 */
Bram Moolenaar29323592016-07-24 22:04:11 +02006697 FOR_ALL_WINDOWS(curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006698 {
6699 if (curwin->w_p_scb)
6700 {
Bram Moolenaardedd1b02013-12-14 13:06:17 +01006701 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006702 y = topline - curwin->w_topline;
6703 if (y > 0)
6704 scrollup(y, TRUE);
6705 else
6706 scrolldown(-y, TRUE);
6707 curwin->w_scbind_pos = topline;
6708 redraw_later(VALID);
6709 cursor_correct();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006710 curwin->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006711 }
6712 }
Bram Moolenaardedd1b02013-12-14 13:06:17 +01006713 curwin = save_curwin;
6714 curbuf = save_curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006715 if (curwin->w_p_scb)
6716 {
6717 did_syncbind = TRUE;
6718 checkpcmark();
6719 if (old_linenr != curwin->w_cursor.lnum)
6720 {
6721 char_u ctrl_o[2];
6722
6723 ctrl_o[0] = Ctrl_O;
6724 ctrl_o[1] = 0;
6725 ins_typebuf(ctrl_o, REMAP_NONE, 0, TRUE, FALSE);
6726 }
6727 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006728}
6729
6730
6731 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006732ex_read(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006733{
Bram Moolenaardf177f62005-02-22 08:39:57 +00006734 int i;
6735 int empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
6736 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006737
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006738 if (eap->usefilter) // :r!cmd
Bram Moolenaar071d4272004-06-13 20:20:40 +00006739 do_bang(1, eap, FALSE, FALSE, TRUE);
6740 else
6741 {
6742 if (u_save(eap->line2, (linenr_T)(eap->line2 + 1)) == FAIL)
6743 return;
6744
6745#ifdef FEAT_BROWSE
6746 if (cmdmod.browse)
6747 {
6748 char_u *browseFile;
6749
Bram Moolenaar7171abe2004-10-11 10:06:20 +00006750 browseFile = do_browse(0, (char_u *)_("Append File"), eap->arg,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006751 NULL, NULL, NULL, curbuf);
6752 if (browseFile != NULL)
6753 {
6754 i = readfile(browseFile, NULL,
6755 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
6756 vim_free(browseFile);
6757 }
6758 else
6759 i = OK;
6760 }
6761 else
6762#endif
6763 if (*eap->arg == NUL)
6764 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006765 if (check_fname() == FAIL) // check for no file name
Bram Moolenaar071d4272004-06-13 20:20:40 +00006766 return;
6767 i = readfile(curbuf->b_ffname, curbuf->b_fname,
6768 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
6769 }
6770 else
6771 {
6772 if (vim_strchr(p_cpo, CPO_ALTREAD) != NULL)
6773 (void)setaltfname(eap->arg, eap->arg, (linenr_T)1);
6774 i = readfile(eap->arg, NULL,
6775 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
6776
6777 }
Bram Moolenaare13b9af2017-01-13 22:01:02 +01006778 if (i != OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006779 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006780#if defined(FEAT_EVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006781 if (!aborting())
6782#endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006783 semsg(_(e_notopen), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006784 }
6785 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00006786 {
6787 if (empty && exmode_active)
6788 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006789 // Delete the empty line that remains. Historically ex does
6790 // this but vi doesn't.
Bram Moolenaardf177f62005-02-22 08:39:57 +00006791 if (eap->line2 == 0)
6792 lnum = curbuf->b_ml.ml_line_count;
6793 else
6794 lnum = 1;
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00006795 if (*ml_get(lnum) == NUL && u_savedel(lnum, 1L) == OK)
Bram Moolenaardf177f62005-02-22 08:39:57 +00006796 {
Bram Moolenaarca70c072020-05-30 20:30:46 +02006797 ml_delete(lnum);
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00006798 if (curwin->w_cursor.lnum > 1
6799 && curwin->w_cursor.lnum >= lnum)
Bram Moolenaardf177f62005-02-22 08:39:57 +00006800 --curwin->w_cursor.lnum;
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00006801 deleted_lines_mark(lnum, 1L);
Bram Moolenaardf177f62005-02-22 08:39:57 +00006802 }
6803 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006804 redraw_curbuf_later(VALID);
Bram Moolenaardf177f62005-02-22 08:39:57 +00006805 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006806 }
6807}
6808
Bram Moolenaarea408852005-06-25 22:49:46 +00006809static char_u *prev_dir = NULL;
6810
6811#if defined(EXITFREE) || defined(PROTO)
6812 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006813free_cd_dir(void)
Bram Moolenaarea408852005-06-25 22:49:46 +00006814{
Bram Moolenaard23a8232018-02-10 18:45:26 +01006815 VIM_CLEAR(prev_dir);
6816 VIM_CLEAR(globaldir);
Bram Moolenaarea408852005-06-25 22:49:46 +00006817}
6818#endif
6819
Bram Moolenaarf4258302013-06-02 18:20:17 +02006820/*
Bram Moolenaar002bc792020-06-05 22:33:42 +02006821 * Get the previous directory for the given chdir scope.
6822 */
6823 static char_u *
6824get_prevdir(cdscope_T scope)
6825{
6826 if (scope == CDSCOPE_WINDOW)
6827 return curwin->w_prevdir;
6828 else if (scope == CDSCOPE_TABPAGE)
6829 return curtab->tp_prevdir;
6830 return prev_dir;
6831}
6832
6833/*
Bram Moolenaarf4258302013-06-02 18:20:17 +02006834 * Deal with the side effects of changing the current directory.
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006835 * When 'scope' is CDSCOPE_TABPAGE then this was after an ":tcd" command.
6836 * When 'scope' is CDSCOPE_WINDOW then this was after an ":lcd" command.
Bram Moolenaarf4258302013-06-02 18:20:17 +02006837 */
6838 void
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006839post_chdir(cdscope_T scope)
Bram Moolenaarf4258302013-06-02 18:20:17 +02006840{
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006841 if (scope != CDSCOPE_WINDOW)
Bram Moolenaar00aa0692019-04-27 20:37:57 +02006842 // Clear tab local directory for both :cd and :tcd
6843 VIM_CLEAR(curtab->tp_localdir);
Bram Moolenaard23a8232018-02-10 18:45:26 +01006844 VIM_CLEAR(curwin->w_localdir);
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006845 if (scope != CDSCOPE_GLOBAL)
Bram Moolenaarf4258302013-06-02 18:20:17 +02006846 {
Bram Moolenaar002bc792020-06-05 22:33:42 +02006847 char_u *pdir = get_prevdir(scope);
6848
6849 // If still in the global directory, need to remember current
6850 // directory as the global directory.
6851 if (globaldir == NULL && pdir != NULL)
6852 globaldir = vim_strsave(pdir);
6853
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006854 // Remember this local directory for the window.
Bram Moolenaarf4258302013-06-02 18:20:17 +02006855 if (mch_dirname(NameBuff, MAXPATHL) == OK)
Bram Moolenaar00aa0692019-04-27 20:37:57 +02006856 {
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006857 if (scope == CDSCOPE_TABPAGE)
Bram Moolenaar00aa0692019-04-27 20:37:57 +02006858 curtab->tp_localdir = vim_strsave(NameBuff);
6859 else
6860 curwin->w_localdir = vim_strsave(NameBuff);
6861 }
Bram Moolenaarf4258302013-06-02 18:20:17 +02006862 }
6863 else
6864 {
Bram Moolenaar002bc792020-06-05 22:33:42 +02006865 // We are now in the global directory, no need to remember its name.
Bram Moolenaard23a8232018-02-10 18:45:26 +01006866 VIM_CLEAR(globaldir);
Bram Moolenaarf4258302013-06-02 18:20:17 +02006867 }
6868
6869 shorten_fnames(TRUE);
6870}
6871
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006872/*
6873 * Change directory function used by :cd/:tcd/:lcd Ex commands and the
Bram Moolenaar95058722020-06-01 16:26:19 +02006874 * chdir() function.
6875 * scope == CDSCOPE_WINDOW: changes the window-local directory
6876 * scope == CDSCOPE_TABPAGE: changes the tab-local directory
6877 * Otherwise: changes the global directory
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006878 * Returns TRUE if the directory is successfully changed.
6879 */
6880 int
6881changedir_func(
6882 char_u *new_dir,
6883 int forceit,
6884 cdscope_T scope)
6885{
6886 char_u *tofree;
Bram Moolenaar002bc792020-06-05 22:33:42 +02006887 char_u *pdir = NULL;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006888 int dir_differs;
6889 int retval = FALSE;
6890
Bram Moolenaar7cc96922020-01-31 22:41:38 +01006891 if (new_dir == NULL || allbuf_locked())
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006892 return FALSE;
6893
6894 if (vim_strchr(p_cpo, CPO_CHDIR) != NULL && curbufIsChanged() && !forceit)
6895 {
6896 emsg(_("E747: Cannot change directory, buffer is modified (add ! to override)"));
6897 return FALSE;
6898 }
6899
6900 // ":cd -": Change to previous directory
6901 if (STRCMP(new_dir, "-") == 0)
6902 {
Bram Moolenaar002bc792020-06-05 22:33:42 +02006903 pdir = get_prevdir(scope);
6904 if (pdir == NULL)
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006905 {
6906 emsg(_("E186: No previous directory"));
6907 return FALSE;
6908 }
Bram Moolenaar002bc792020-06-05 22:33:42 +02006909 new_dir = pdir;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006910 }
6911
Bram Moolenaar002bc792020-06-05 22:33:42 +02006912 // Free the previous directory
6913 tofree = get_prevdir(scope);
6914
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006915 // Save current directory for next ":cd -"
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006916 if (mch_dirname(NameBuff, MAXPATHL) == OK)
Bram Moolenaar002bc792020-06-05 22:33:42 +02006917 pdir = vim_strsave(NameBuff);
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006918 else
Bram Moolenaar002bc792020-06-05 22:33:42 +02006919 pdir = NULL;
6920 if (scope == CDSCOPE_WINDOW)
6921 curwin->w_prevdir = pdir;
6922 else if (scope == CDSCOPE_TABPAGE)
6923 curtab->tp_prevdir = pdir;
6924 else
6925 prev_dir = pdir;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006926
6927#if defined(UNIX) || defined(VMS)
6928 // for UNIX ":cd" means: go to home directory
6929 if (*new_dir == NUL)
6930 {
6931 // use NameBuff for home directory name
6932# ifdef VMS
6933 char_u *p;
6934
6935 p = mch_getenv((char_u *)"SYS$LOGIN");
6936 if (p == NULL || *p == NUL) // empty is the same as not set
6937 NameBuff[0] = NUL;
6938 else
6939 vim_strncpy(NameBuff, p, MAXPATHL - 1);
6940# else
6941 expand_env((char_u *)"$HOME", NameBuff, MAXPATHL);
6942# endif
6943 new_dir = NameBuff;
6944 }
6945#endif
Bram Moolenaar002bc792020-06-05 22:33:42 +02006946 dir_differs = new_dir == NULL || pdir == NULL
6947 || pathcmp((char *)pdir, (char *)new_dir, -1) != 0;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006948 if (new_dir == NULL || (dir_differs && vim_chdir(new_dir)))
6949 emsg(_(e_failed));
6950 else
6951 {
6952 char_u *acmd_fname;
6953
6954 post_chdir(scope);
6955
6956 if (dir_differs)
6957 {
6958 if (scope == CDSCOPE_WINDOW)
6959 acmd_fname = (char_u *)"window";
6960 else if (scope == CDSCOPE_TABPAGE)
6961 acmd_fname = (char_u *)"tabpage";
6962 else
6963 acmd_fname = (char_u *)"global";
6964 apply_autocmds(EVENT_DIRCHANGED, acmd_fname, new_dir, FALSE,
6965 curbuf);
6966 }
6967 retval = TRUE;
6968 }
6969 vim_free(tofree);
6970
6971 return retval;
6972}
Bram Moolenaarea408852005-06-25 22:49:46 +00006973
Bram Moolenaar071d4272004-06-13 20:20:40 +00006974/*
Bram Moolenaar00aa0692019-04-27 20:37:57 +02006975 * ":cd", ":tcd", ":lcd", ":chdir" ":tchdir" and ":lchdir".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006976 */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00006977 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006978ex_cd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006979{
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01006980 char_u *new_dir;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006981
6982 new_dir = eap->arg;
6983#if !defined(UNIX) && !defined(VMS)
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006984 // for non-UNIX ":cd" means: print current directory
Bram Moolenaar071d4272004-06-13 20:20:40 +00006985 if (*new_dir == NUL)
6986 ex_pwd(NULL);
6987 else
6988#endif
6989 {
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006990 cdscope_T scope = CDSCOPE_GLOBAL;
6991
6992 if (eap->cmdidx == CMD_lcd || eap->cmdidx == CMD_lchdir)
6993 scope = CDSCOPE_WINDOW;
6994 else if (eap->cmdidx == CMD_tcd || eap->cmdidx == CMD_tchdir)
6995 scope = CDSCOPE_TABPAGE;
6996
6997 if (changedir_func(new_dir, eap->forceit, scope))
Bram Moolenaardf177f62005-02-22 08:39:57 +00006998 {
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006999 // Echo the new current directory if the command was typed.
Bram Moolenaarfcfbc672009-07-09 18:13:49 +00007000 if (KeyTyped || p_verbose >= 5)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007001 ex_pwd(eap);
7002 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007003 }
7004}
7005
7006/*
7007 * ":pwd".
7008 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007009 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007010ex_pwd(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007011{
7012 if (mch_dirname(NameBuff, MAXPATHL) == OK)
7013 {
7014#ifdef BACKSLASH_IN_FILENAME
7015 slash_adjust(NameBuff);
7016#endif
Bram Moolenaar95058722020-06-01 16:26:19 +02007017 if (p_verbose > 0)
7018 {
7019 char *context = "global";
7020
7021 if (curwin->w_localdir != NULL)
7022 context = "window";
7023 else if (curtab->tp_localdir != NULL)
7024 context = "tabpage";
7025 smsg("[%s] %s", context, (char *)NameBuff);
7026 }
7027 else
7028 msg((char *)NameBuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007029 }
7030 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007031 emsg(_("E187: Unknown"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007032}
7033
7034/*
7035 * ":=".
7036 */
7037 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007038ex_equal(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007039{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007040 smsg("%ld", (long)eap->line2);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007041 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007042}
7043
7044 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007045ex_sleep(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007046{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007047 int n;
7048 long len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007049
7050 if (cursor_valid())
7051 {
7052 n = W_WINROW(curwin) + curwin->w_wrow - msg_scrolled;
7053 if (n >= 0)
Bram Moolenaar53f81742017-09-22 14:35:51 +02007054 windgoto((int)n, curwin->w_wincol + curwin->w_wcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007055 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007056
7057 len = eap->line2;
7058 switch (*eap->arg)
7059 {
7060 case 'm': break;
7061 case NUL: len *= 1000L; break;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007062 default: semsg(_(e_invarg2), eap->arg); return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007063 }
7064 do_sleep(len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007065}
7066
7067/*
7068 * Sleep for "msec" milliseconds, but keep checking for a CTRL-C every second.
7069 */
7070 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007071do_sleep(long msec)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007072{
Bram Moolenaar52953192019-08-16 10:27:13 +02007073 long done = 0;
Bram Moolenaar975b5272016-03-15 23:10:59 +01007074 long wait_now;
Bram Moolenaar52953192019-08-16 10:27:13 +02007075# ifdef ELAPSED_FUNC
7076 elapsed_T start_tv;
7077
7078 // Remember at what time we started, so that we know how much longer we
7079 // should wait after waiting for a bit.
7080 ELAPSED_INIT(start_tv);
7081# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007082
7083 cursor_on();
Bram Moolenaarf45d7472018-09-30 18:22:26 +02007084 out_flush_cursor(FALSE, FALSE);
Bram Moolenaar52953192019-08-16 10:27:13 +02007085 while (!got_int && done < msec)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007086 {
Bram Moolenaar975b5272016-03-15 23:10:59 +01007087 wait_now = msec - done > 1000L ? 1000L : msec - done;
7088#ifdef FEAT_TIMERS
7089 {
7090 long due_time = check_due_timer();
7091
7092 if (due_time > 0 && due_time < wait_now)
7093 wait_now = due_time;
7094 }
7095#endif
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02007096#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar28e67e02019-08-15 23:05:49 +02007097 if (has_any_channel() && wait_now > 20L)
7098 wait_now = 20L;
7099#endif
7100#ifdef FEAT_SOUND
7101 if (has_any_sound_callback() && wait_now > 20L)
7102 wait_now = 20L;
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02007103#endif
Bram Moolenaar975b5272016-03-15 23:10:59 +01007104 ui_delay(wait_now, TRUE);
Bram Moolenaar52953192019-08-16 10:27:13 +02007105
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02007106#ifdef FEAT_JOB_CHANNEL
7107 if (has_any_channel())
7108 ui_breakcheck_force(TRUE);
7109 else
7110#endif
7111 ui_breakcheck();
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007112#ifdef MESSAGE_QUEUE
Bram Moolenaar52953192019-08-16 10:27:13 +02007113 // Process the netbeans and clientserver messages that may have been
7114 // received in the call to ui_breakcheck() when the GUI is in use. This
7115 // may occur when running a test case.
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007116 parse_queued_messages();
Bram Moolenaare3cc6d42011-10-20 21:58:34 +02007117#endif
Bram Moolenaar52953192019-08-16 10:27:13 +02007118
7119# ifdef ELAPSED_FUNC
7120 // actual time passed
7121 done = ELAPSED_FUNC(start_tv);
7122# else
7123 // guestimate time passed (will actually be more)
7124 done += wait_now;
7125# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007126 }
Bram Moolenaar1ebff3d2018-07-07 16:18:13 +02007127
7128 // If CTRL-C was typed to interrupt the sleep, drop the CTRL-C from the
7129 // input buffer, otherwise a following call to input() fails.
7130 if (got_int)
7131 (void)vpeekc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007132}
7133
Bram Moolenaar071d4272004-06-13 20:20:40 +00007134/*
7135 * ":winsize" command (obsolete).
7136 */
7137 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007138ex_winsize(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007139{
7140 int w, h;
7141 char_u *arg = eap->arg;
7142 char_u *p;
7143
7144 w = getdigits(&arg);
7145 arg = skipwhite(arg);
7146 p = arg;
7147 h = getdigits(&arg);
7148 if (*p != NUL && *arg == NUL)
7149 set_shellsize(w, h, TRUE);
7150 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007151 emsg(_("E465: :winsize requires two number arguments"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007152}
7153
Bram Moolenaar071d4272004-06-13 20:20:40 +00007154 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007155ex_wincmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007156{
7157 int xchar = NUL;
7158 char_u *p;
7159
7160 if (*eap->arg == 'g' || *eap->arg == Ctrl_G)
7161 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007162 // CTRL-W g and CTRL-W CTRL-G have an extra command character
Bram Moolenaar071d4272004-06-13 20:20:40 +00007163 if (eap->arg[1] == NUL)
7164 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007165 emsg(_(e_invarg));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007166 return;
7167 }
7168 xchar = eap->arg[1];
7169 p = eap->arg + 2;
7170 }
7171 else
7172 p = eap->arg + 1;
7173
7174 eap->nextcmd = check_nextcmd(p);
7175 p = skipwhite(p);
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02007176 if (*p != NUL && *p != (
7177#ifdef FEAT_EVAL
7178 in_vim9script() ? '#' :
7179#endif
7180 '"')
7181 && eap->nextcmd == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007182 emsg(_(e_invarg));
Bram Moolenaar12bde492011-06-13 01:19:56 +02007183 else if (!eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007184 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007185 // Pass flags on for ":vertical wincmd ]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00007186 postponed_split_flags = cmdmod.split;
Bram Moolenaard326ce82007-03-11 14:48:29 +00007187 postponed_split_tab = cmdmod.tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007188 do_window(*eap->arg, eap->addr_count > 0 ? eap->line2 : 0L, xchar);
7189 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00007190 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007191 }
7192}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007193
Bram Moolenaar843ee412004-06-30 16:16:41 +00007194#if defined(FEAT_GUI) || defined(UNIX) || defined(VMS) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007195/*
7196 * ":winpos".
7197 */
7198 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007199ex_winpos(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007200{
7201 int x, y;
7202 char_u *arg = eap->arg;
7203 char_u *p;
7204
7205 if (*arg == NUL)
7206 {
Bram Moolenaar843ee412004-06-30 16:16:41 +00007207# if defined(FEAT_GUI) || defined(MSWIN)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007208# ifdef VIMDLL
7209 if (gui.in_use ? gui_mch_get_winpos(&x, &y) != FAIL :
7210 mch_get_winpos(&x, &y) != FAIL)
7211# elif defined(FEAT_GUI)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007212 if (gui.in_use && gui_mch_get_winpos(&x, &y) != FAIL)
Bram Moolenaar843ee412004-06-30 16:16:41 +00007213# else
7214 if (mch_get_winpos(&x, &y) != FAIL)
7215# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007216 {
7217 sprintf((char *)IObuff, _("Window position: X %d, Y %d"), x, y);
Bram Moolenaar32526b32019-01-19 17:43:09 +01007218 msg((char *)IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007219 }
7220 else
7221# endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007222 emsg(_("E188: Obtaining window position not implemented for this platform"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007223 }
7224 else
7225 {
7226 x = getdigits(&arg);
7227 arg = skipwhite(arg);
7228 p = arg;
7229 y = getdigits(&arg);
7230 if (*p == NUL || *arg != NUL)
7231 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007232 emsg(_("E466: :winpos requires two number arguments"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007233 return;
7234 }
7235# ifdef FEAT_GUI
7236 if (gui.in_use)
7237 gui_mch_set_winpos(x, y);
7238 else if (gui.starting)
7239 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007240 // Remember the coordinates for when the window is opened.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007241 gui_win_x = x;
7242 gui_win_y = y;
7243 }
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007244# if defined(HAVE_TGETENT) || defined(VIMDLL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007245 else
7246# endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007247# endif
7248# if defined(MSWIN) && (!defined(FEAT_GUI) || defined(VIMDLL))
Bram Moolenaar843ee412004-06-30 16:16:41 +00007249 mch_set_winpos(x, y);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007250# endif
7251# ifdef HAVE_TGETENT
7252 if (*T_CWP)
7253 term_set_winpos(x, y);
7254# endif
7255 }
7256}
7257#endif
7258
7259/*
7260 * Handle command that work like operators: ":delete", ":yank", ":>" and ":<".
7261 */
7262 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007263ex_operators(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007264{
7265 oparg_T oa;
7266
7267 clear_oparg(&oa);
7268 oa.regname = eap->regname;
7269 oa.start.lnum = eap->line1;
7270 oa.end.lnum = eap->line2;
7271 oa.line_count = eap->line2 - eap->line1 + 1;
7272 oa.motion_type = MLINE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007273 virtual_op = FALSE;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007274 if (eap->cmdidx != CMD_yank) // position cursor for undo
Bram Moolenaar071d4272004-06-13 20:20:40 +00007275 {
7276 setpcmark();
7277 curwin->w_cursor.lnum = eap->line1;
7278 beginline(BL_SOL | BL_FIX);
7279 }
7280
Bram Moolenaard07c6e12013-11-21 14:21:40 +01007281 if (VIsual_active)
7282 end_visual_mode();
Bram Moolenaard07c6e12013-11-21 14:21:40 +01007283
Bram Moolenaar071d4272004-06-13 20:20:40 +00007284 switch (eap->cmdidx)
7285 {
7286 case CMD_delete:
7287 oa.op_type = OP_DELETE;
7288 op_delete(&oa);
7289 break;
7290
7291 case CMD_yank:
7292 oa.op_type = OP_YANK;
7293 (void)op_yank(&oa, FALSE, TRUE);
7294 break;
7295
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007296 default: // CMD_rshift or CMD_lshift
Bram Moolenaar6e707362013-06-14 19:15:58 +02007297 if (
Bram Moolenaar071d4272004-06-13 20:20:40 +00007298#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6e707362013-06-14 19:15:58 +02007299 (eap->cmdidx == CMD_rshift) ^ curwin->w_p_rl
7300#else
7301 eap->cmdidx == CMD_rshift
Bram Moolenaar071d4272004-06-13 20:20:40 +00007302#endif
Bram Moolenaar6e707362013-06-14 19:15:58 +02007303 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00007304 oa.op_type = OP_RSHIFT;
7305 else
7306 oa.op_type = OP_LSHIFT;
7307 op_shift(&oa, FALSE, eap->amount);
7308 break;
7309 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007310 virtual_op = MAYBE;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007311 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007312}
7313
7314/*
7315 * ":put".
7316 */
7317 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007318ex_put(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007319{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007320 // ":0put" works like ":1put!".
Bram Moolenaar071d4272004-06-13 20:20:40 +00007321 if (eap->line2 == 0)
7322 {
7323 eap->line2 = 1;
7324 eap->forceit = TRUE;
7325 }
7326 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaarc3516f72020-09-08 22:45:35 +02007327 do_put(eap->regname, NULL, eap->forceit ? BACKWARD : FORWARD, 1L,
Bram Moolenaardf177f62005-02-22 08:39:57 +00007328 PUT_LINE|PUT_CURSLINE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007329}
7330
7331/*
7332 * Handle ":copy" and ":move".
7333 */
7334 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007335ex_copymove(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007336{
7337 long n;
7338
Bram Moolenaar491799b2020-08-01 19:23:43 +02007339#ifdef FEAT_EVAL
Bram Moolenaarf5a48012020-08-01 17:00:03 +02007340 if (not_in_vim9(eap) == FAIL)
7341 return;
Bram Moolenaar491799b2020-08-01 19:23:43 +02007342#endif
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02007343 n = get_address(eap, &eap->arg, eap->addr_type, FALSE, FALSE, FALSE, 1);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007344 if (eap->arg == NULL) // error detected
Bram Moolenaar071d4272004-06-13 20:20:40 +00007345 {
7346 eap->nextcmd = NULL;
7347 return;
7348 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00007349 get_flags(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007350
7351 /*
7352 * move or copy lines from 'eap->line1'-'eap->line2' to below line 'n'
7353 */
7354 if (n == MAXLNUM || n < 0 || n > curbuf->b_ml.ml_line_count)
7355 {
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02007356 emsg(_(e_invrange));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007357 return;
7358 }
7359
7360 if (eap->cmdidx == CMD_move)
7361 {
7362 if (do_move(eap->line1, eap->line2, n) == FAIL)
7363 return;
7364 }
7365 else
7366 ex_copy(eap->line1, eap->line2, n);
7367 u_clearline();
7368 beginline(BL_SOL | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007369 ex_may_print(eap);
7370}
7371
7372/*
7373 * Print the current line if flags were given to the Ex command.
7374 */
Bram Moolenaarfd3fe982014-04-01 17:49:44 +02007375 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007376ex_may_print(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007377{
7378 if (eap->flags != 0)
7379 {
7380 print_line(curwin->w_cursor.lnum, (eap->flags & EXFLAG_NR),
7381 (eap->flags & EXFLAG_LIST));
7382 ex_no_reprint = TRUE;
7383 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007384}
7385
7386/*
7387 * ":smagic" and ":snomagic".
7388 */
7389 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007390ex_submagic(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007391{
7392 int magic_save = p_magic;
7393
7394 p_magic = (eap->cmdidx == CMD_smagic);
Bram Moolenaar66e00142020-08-12 21:58:12 +02007395 ex_substitute(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007396 p_magic = magic_save;
7397}
7398
7399/*
7400 * ":join".
7401 */
7402 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007403ex_join(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007404{
7405 curwin->w_cursor.lnum = eap->line1;
7406 if (eap->line1 == eap->line2)
7407 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007408 if (eap->addr_count >= 2) // :2,2join does nothing
Bram Moolenaar071d4272004-06-13 20:20:40 +00007409 return;
7410 if (eap->line2 == curbuf->b_ml.ml_line_count)
7411 {
7412 beep_flush();
7413 return;
7414 }
7415 ++eap->line2;
7416 }
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02007417 (void)do_join(eap->line2 - eap->line1 + 1, !eap->forceit, TRUE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007418 beginline(BL_WHITE | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007419 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007420}
7421
7422/*
7423 * ":[addr]@r" or ":[addr]*r": execute register
7424 */
7425 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007426ex_at(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007427{
7428 int c;
Bram Moolenaar60462872009-11-03 11:40:19 +00007429 int prev_len = typebuf.tb_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007430
7431 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaar4930a762016-09-11 14:39:53 +02007432 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007433
7434#ifdef USE_ON_FLY_SCROLL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007435 dont_scroll = TRUE; // disallow scrolling here
Bram Moolenaar071d4272004-06-13 20:20:40 +00007436#endif
7437
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007438 // get the register name. No name means to use the previous one
Bram Moolenaar071d4272004-06-13 20:20:40 +00007439 c = *eap->arg;
7440 if (c == NUL || (c == '*' && *eap->cmd == '*'))
7441 c = '@';
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007442 // Put the register in the typeahead buffer with the "silent" flag.
Bram Moolenaard333d1e2006-11-07 17:43:47 +00007443 if (do_execreg(c, TRUE, vim_strchr(p_cpo, CPO_EXECBUF) != NULL, TRUE)
7444 == FAIL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007445 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007446 beep_flush();
Bram Moolenaardf177f62005-02-22 08:39:57 +00007447 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007448 else
7449 {
7450 int save_efr = exec_from_reg;
7451
7452 exec_from_reg = TRUE;
7453
7454 /*
7455 * Execute from the typeahead buffer.
Bram Moolenaar60462872009-11-03 11:40:19 +00007456 * Continue until the stuff buffer is empty and all added characters
7457 * have been consumed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007458 */
Bram Moolenaar60462872009-11-03 11:40:19 +00007459 while (!stuff_empty() || typebuf.tb_len > prev_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007460 (void)do_cmdline(NULL, getexline, NULL, DOCMD_NOWAIT|DOCMD_VERBOSE);
7461
7462 exec_from_reg = save_efr;
7463 }
7464}
7465
7466/*
7467 * ":!".
7468 */
7469 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007470ex_bang(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007471{
7472 do_bang(eap->addr_count, eap, eap->forceit, TRUE, TRUE);
7473}
7474
7475/*
7476 * ":undo".
7477 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007478 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +01007479ex_undo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007480{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007481 if (eap->addr_count == 1) // :undo 123
Bram Moolenaar730cde92010-06-27 05:18:54 +02007482 undo_time(eap->line2, FALSE, FALSE, TRUE);
Bram Moolenaard3667a22006-03-16 21:35:52 +00007483 else
7484 u_undo(1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007485}
7486
Bram Moolenaar55debbe2010-05-23 23:34:36 +02007487#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar6df6f472010-07-18 18:04:50 +02007488 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007489ex_wundo(exarg_T *eap)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02007490{
7491 char_u hash[UNDO_HASH_SIZE];
7492
7493 u_compute_hash(hash);
7494 u_write_undo(eap->arg, eap->forceit, curbuf, hash);
7495}
7496
Bram Moolenaar6df6f472010-07-18 18:04:50 +02007497 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007498ex_rundo(exarg_T *eap)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02007499{
7500 char_u hash[UNDO_HASH_SIZE];
7501
7502 u_compute_hash(hash);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02007503 u_read_undo(eap->arg, hash, NULL);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02007504}
7505#endif
7506
Bram Moolenaar071d4272004-06-13 20:20:40 +00007507/*
7508 * ":redo".
7509 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007510 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007511ex_redo(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007512{
7513 u_redo(1);
7514}
7515
7516/*
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007517 * ":earlier" and ":later".
7518 */
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007519 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007520ex_later(exarg_T *eap)
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007521{
7522 long count = 0;
7523 int sec = FALSE;
Bram Moolenaar730cde92010-06-27 05:18:54 +02007524 int file = FALSE;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007525 char_u *p = eap->arg;
7526
7527 if (*p == NUL)
7528 count = 1;
7529 else if (isdigit(*p))
7530 {
7531 count = getdigits(&p);
7532 switch (*p)
7533 {
7534 case 's': ++p; sec = TRUE; break;
7535 case 'm': ++p; sec = TRUE; count *= 60; break;
7536 case 'h': ++p; sec = TRUE; count *= 60 * 60; break;
Bram Moolenaar730cde92010-06-27 05:18:54 +02007537 case 'd': ++p; sec = TRUE; count *= 24 * 60 * 60; break;
7538 case 'f': ++p; file = TRUE; break;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007539 }
7540 }
7541
7542 if (*p != NUL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007543 semsg(_(e_invarg2), eap->arg);
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007544 else
Bram Moolenaar730cde92010-06-27 05:18:54 +02007545 undo_time(eap->cmdidx == CMD_earlier ? -count : count,
7546 sec, file, FALSE);
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007547}
7548
7549/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007550 * ":redir": start/stop redirection.
7551 */
7552 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007553ex_redir(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007554{
7555 char *mode;
7556 char_u *fname;
Bram Moolenaarca472992005-01-21 11:46:23 +00007557 char_u *arg = eap->arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007558
Bram Moolenaarba768492016-07-08 15:32:54 +02007559#ifdef FEAT_EVAL
Bram Moolenaar79815f12016-07-09 17:07:29 +02007560 if (redir_execute)
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02007561 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007562 emsg(_("E930: Cannot use :redir inside execute()"));
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02007563 return;
7564 }
Bram Moolenaarba768492016-07-08 15:32:54 +02007565#endif
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02007566
Bram Moolenaar071d4272004-06-13 20:20:40 +00007567 if (STRICMP(eap->arg, "END") == 0)
7568 close_redir();
7569 else
7570 {
Bram Moolenaarca472992005-01-21 11:46:23 +00007571 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00007572 {
Bram Moolenaarca472992005-01-21 11:46:23 +00007573 ++arg;
7574 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00007575 {
Bram Moolenaarca472992005-01-21 11:46:23 +00007576 ++arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007577 mode = "a";
7578 }
7579 else
7580 mode = "w";
Bram Moolenaarca472992005-01-21 11:46:23 +00007581 arg = skipwhite(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007582
7583 close_redir();
7584
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007585 // Expand environment variables and "~/".
Bram Moolenaarca472992005-01-21 11:46:23 +00007586 fname = expand_env_save(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007587 if (fname == NULL)
7588 return;
7589#ifdef FEAT_BROWSE
7590 if (cmdmod.browse)
7591 {
7592 char_u *browseFile;
7593
Bram Moolenaar7171abe2004-10-11 10:06:20 +00007594 browseFile = do_browse(BROWSE_SAVE,
7595 (char_u *)_("Save Redirection"),
Bram Moolenaarc36651b2018-04-29 12:22:56 +02007596 fname, NULL, NULL,
7597 (char_u *)_(BROWSE_FILTER_ALL_FILES), curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007598 if (browseFile == NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007599 return; // operation cancelled
Bram Moolenaar071d4272004-06-13 20:20:40 +00007600 vim_free(fname);
7601 fname = browseFile;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007602 eap->forceit = TRUE; // since dialog already asked
Bram Moolenaar071d4272004-06-13 20:20:40 +00007603 }
7604#endif
7605
7606 redir_fd = open_exfile(fname, eap->forceit, mode);
7607 vim_free(fname);
7608 }
7609#ifdef FEAT_EVAL
Bram Moolenaarca472992005-01-21 11:46:23 +00007610 else if (*arg == '@')
Bram Moolenaar071d4272004-06-13 20:20:40 +00007611 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007612 // redirect to a register a-z (resp. A-Z for appending)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007613 close_redir();
Bram Moolenaarca472992005-01-21 11:46:23 +00007614 ++arg;
7615 if (ASCII_ISALPHA(*arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007616# ifdef FEAT_CLIPBOARD
Bram Moolenaarca472992005-01-21 11:46:23 +00007617 || *arg == '*'
Bram Moolenaar9a51c6e2006-11-14 19:25:02 +00007618 || *arg == '+'
Bram Moolenaar071d4272004-06-13 20:20:40 +00007619# endif
Bram Moolenaarca472992005-01-21 11:46:23 +00007620 || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00007621 {
Bram Moolenaarca472992005-01-21 11:46:23 +00007622 redir_reg = *arg++;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007623 if (*arg == '>' && arg[1] == '>') // append
Bram Moolenaard9d30582005-05-18 22:10:28 +00007624 arg += 2;
Bram Moolenaarc188b882007-10-19 14:20:54 +00007625 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007626 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007627 // Can use both "@a" and "@a>".
Bram Moolenaar2c29bee2005-06-01 21:46:07 +00007628 if (*arg == '>')
7629 arg++;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007630 // Make register empty when not using @A-@Z and the
7631 // command is valid.
Bram Moolenaarc188b882007-10-19 14:20:54 +00007632 if (*arg == NUL && !isupper(redir_reg))
7633 write_reg_contents(redir_reg, (char_u *)"", -1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007634 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00007635 }
7636 if (*arg != NUL)
7637 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00007638 redir_reg = 0;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007639 semsg(_(e_invarg2), eap->arg);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007640 }
7641 }
7642 else if (*arg == '=' && arg[1] == '>')
7643 {
7644 int append;
7645
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007646 // redirect to a variable
Bram Moolenaardf177f62005-02-22 08:39:57 +00007647 close_redir();
7648 arg += 2;
7649
7650 if (*arg == '>')
7651 {
7652 ++arg;
7653 append = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007654 }
7655 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00007656 append = FALSE;
7657
7658 if (var_redir_start(skipwhite(arg), append) == OK)
7659 redir_vname = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007660 }
7661#endif
7662
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007663 // TODO: redirect to a buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00007664
Bram Moolenaar071d4272004-06-13 20:20:40 +00007665 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007666 semsg(_(e_invarg2), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007667 }
Bram Moolenaar29b2d262006-09-10 19:07:28 +00007668
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007669 // Make sure redirection is not off. Can happen for cmdline completion
7670 // that indirectly invokes a command to catch its output.
Bram Moolenaar29b2d262006-09-10 19:07:28 +00007671 if (redir_fd != NULL
7672#ifdef FEAT_EVAL
7673 || redir_reg || redir_vname
7674#endif
7675 )
7676 redir_off = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007677}
7678
7679/*
7680 * ":redraw": force redraw
7681 */
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01007682 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007683ex_redraw(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007684{
7685 int r = RedrawingDisabled;
7686 int p = p_lz;
7687
7688 RedrawingDisabled = 0;
7689 p_lz = FALSE;
Bram Moolenaarabc39ab2017-03-01 18:04:05 +01007690 validate_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007691 update_topline();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007692 update_screen(eap->forceit ? CLEAR : VIsual_active ? INVERTED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007693#ifdef FEAT_TITLE
7694 if (need_maketitle)
7695 maketitle();
7696#endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007697#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
7698# ifdef VIMDLL
7699 if (!gui.in_use)
7700# endif
7701 resize_console_buf();
Bram Moolenaar78d21da2019-02-17 15:00:52 +01007702#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007703 RedrawingDisabled = r;
7704 p_lz = p;
7705
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007706 // Reset msg_didout, so that a message that's there is overwritten.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007707 msg_didout = FALSE;
7708 msg_col = 0;
7709
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007710 // No need to wait after an intentional redraw.
Bram Moolenaar56667a52013-07-17 11:54:28 +02007711 need_wait_return = FALSE;
7712
Bram Moolenaar071d4272004-06-13 20:20:40 +00007713 out_flush();
7714}
7715
7716/*
7717 * ":redrawstatus": force redraw of status line(s)
7718 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007719 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007720ex_redrawstatus(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007721{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007722 int r = RedrawingDisabled;
7723 int p = p_lz;
7724
7725 RedrawingDisabled = 0;
7726 p_lz = FALSE;
7727 if (eap->forceit)
7728 status_redraw_all();
7729 else
7730 status_redraw_curbuf();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007731 update_screen(VIsual_active ? INVERTED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007732 RedrawingDisabled = r;
7733 p_lz = p;
7734 out_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007735}
7736
Bram Moolenaare12bab32019-01-08 22:02:56 +01007737/*
7738 * ":redrawtabline": force redraw of the tabline
7739 */
7740 static void
7741ex_redrawtabline(exarg_T *eap UNUSED)
7742{
7743 int r = RedrawingDisabled;
7744 int p = p_lz;
7745
7746 RedrawingDisabled = 0;
7747 p_lz = FALSE;
7748
7749 draw_tabline();
7750
7751 RedrawingDisabled = r;
7752 p_lz = p;
7753 out_flush();
7754}
7755
Bram Moolenaar071d4272004-06-13 20:20:40 +00007756 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007757close_redir(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007758{
7759 if (redir_fd != NULL)
7760 {
7761 fclose(redir_fd);
7762 redir_fd = NULL;
7763 }
7764#ifdef FEAT_EVAL
7765 redir_reg = 0;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007766 if (redir_vname)
7767 {
7768 var_redir_stop();
7769 redir_vname = 0;
7770 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007771#endif
7772}
7773
Bram Moolenaarf96ae0b2019-07-28 15:21:55 +02007774#if (defined(FEAT_SESSION) || defined(FEAT_EVAL)) || defined(PROTO)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007775 int
Bram Moolenaarbd67aac2019-09-21 23:09:04 +02007776vim_mkdir_emsg(char_u *name, int prot UNUSED)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007777{
7778 if (vim_mkdir(name, prot) != 0)
7779 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007780 semsg(_("E739: Cannot create directory: %s"), name);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007781 return FAIL;
7782 }
7783 return OK;
7784}
7785#endif
7786
Bram Moolenaar071d4272004-06-13 20:20:40 +00007787/*
7788 * Open a file for writing for an Ex command, with some checks.
7789 * Return file descriptor, or NULL on failure.
7790 */
7791 FILE *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007792open_exfile(
7793 char_u *fname,
7794 int forceit,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007795 char *mode) // "w" for create new file or "a" for append
Bram Moolenaar071d4272004-06-13 20:20:40 +00007796{
7797 FILE *fd;
7798
7799#ifdef UNIX
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007800 // with Unix it is possible to open a directory
Bram Moolenaar071d4272004-06-13 20:20:40 +00007801 if (mch_isdir(fname))
7802 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007803 semsg(_(e_isadir2), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007804 return NULL;
7805 }
7806#endif
7807 if (!forceit && *mode != 'a' && vim_fexists(fname))
7808 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007809 semsg(_("E189: \"%s\" exists (add ! to override)"), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007810 return NULL;
7811 }
7812
7813 if ((fd = mch_fopen((char *)fname, mode)) == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007814 semsg(_("E190: Cannot open \"%s\" for writing"), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007815
7816 return fd;
7817}
7818
7819/*
7820 * ":mark" and ":k".
7821 */
7822 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007823ex_mark(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007824{
7825 pos_T pos;
7826
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007827 if (*eap->arg == NUL) // No argument?
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007828 emsg(_(e_argreq));
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007829 else if (eap->arg[1] != NUL) // more than one character?
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +02007830 semsg(_(e_trailing_arg), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007831 else
7832 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007833 pos = curwin->w_cursor; // save curwin->w_cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00007834 curwin->w_cursor.lnum = eap->line2;
7835 beginline(BL_WHITE | BL_FIX);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007836 if (setmark(*eap->arg) == FAIL) // set mark
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007837 emsg(_("E191: Argument must be a letter or forward/backward quote"));
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007838 curwin->w_cursor = pos; // restore curwin->w_cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00007839 }
7840}
7841
7842/*
7843 * Update w_topline, w_leftcol and the cursor position.
7844 */
7845 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007846update_topline_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007847{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007848 check_cursor(); // put cursor on valid line
Bram Moolenaar071d4272004-06-13 20:20:40 +00007849 update_topline();
7850 if (!curwin->w_p_wrap)
7851 validate_cursor();
7852 update_curswant();
7853}
7854
Bram Moolenaar071d4272004-06-13 20:20:40 +00007855/*
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007856 * Save the current State and go to Normal mode.
7857 * Return TRUE if the typeahead could be saved.
7858 */
7859 int
7860save_current_state(save_state_T *sst)
7861{
7862 sst->save_msg_scroll = msg_scroll;
7863 sst->save_restart_edit = restart_edit;
7864 sst->save_msg_didout = msg_didout;
7865 sst->save_State = State;
7866 sst->save_insertmode = p_im;
7867 sst->save_finish_op = finish_op;
7868 sst->save_opcount = opcount;
Bram Moolenaarcce713d2019-03-04 11:40:12 +01007869 sst->save_reg_executing = reg_executing;
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007870
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007871 msg_scroll = FALSE; // no msg scrolling in Normal mode
7872 restart_edit = 0; // don't go to Insert mode
7873 p_im = FALSE; // don't use 'insertmode'
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007874
7875 /*
7876 * Save the current typeahead. This is required to allow using ":normal"
7877 * from an event handler and makes sure we don't hang when the argument
7878 * ends with half a command.
7879 */
7880 save_typeahead(&sst->tabuf);
7881 return sst->tabuf.typebuf_valid;
7882}
7883
7884 void
7885restore_current_state(save_state_T *sst)
7886{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007887 // Restore the previous typeahead.
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007888 restore_typeahead(&sst->tabuf);
7889
7890 msg_scroll = sst->save_msg_scroll;
7891 restart_edit = sst->save_restart_edit;
7892 p_im = sst->save_insertmode;
7893 finish_op = sst->save_finish_op;
7894 opcount = sst->save_opcount;
Bram Moolenaarcce713d2019-03-04 11:40:12 +01007895 reg_executing = sst->save_reg_executing;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007896 msg_didout |= sst->save_msg_didout; // don't reset msg_didout now
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007897
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007898 // Restore the state (needed when called from a function executed for
7899 // 'indentexpr'). Update the mouse and cursor, they may have changed.
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007900 State = sst->save_State;
7901#ifdef CURSOR_SHAPE
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007902 ui_cursor_shape(); // may show different cursor shape
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007903#endif
7904}
7905
7906/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007907 * ":normal[!] {commands}": Execute normal mode commands.
7908 */
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01007909 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007910ex_normal(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007911{
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007912 save_state_T save_state;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007913 char_u *arg = NULL;
7914 int l;
7915 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007916
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00007917 if (ex_normal_lock > 0)
7918 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007919 emsg(_(e_secure));
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00007920 return;
7921 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007922 if (ex_normal_busy >= p_mmd)
7923 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007924 emsg(_("E192: Recursive use of :normal too deep"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007925 return;
7926 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007927
Bram Moolenaar071d4272004-06-13 20:20:40 +00007928 /*
7929 * vgetc() expects a CSI and K_SPECIAL to have been escaped. Don't do
7930 * this for the K_SPECIAL leading byte, otherwise special keys will not
7931 * work.
7932 */
7933 if (has_mbyte)
7934 {
7935 int len = 0;
7936
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007937 // Count the number of characters to be escaped.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007938 for (p = eap->arg; *p != NUL; ++p)
7939 {
Bram Moolenaar13505972019-01-24 15:04:48 +01007940#ifdef FEAT_GUI
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007941 if (*p == CSI) // leadbyte CSI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007942 len += 2;
Bram Moolenaar13505972019-01-24 15:04:48 +01007943#endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007944 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007945 if (*++p == K_SPECIAL // trailbyte K_SPECIAL or CSI
Bram Moolenaar13505972019-01-24 15:04:48 +01007946#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007947 || *p == CSI
Bram Moolenaar13505972019-01-24 15:04:48 +01007948#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007949 )
7950 len += 2;
7951 }
7952 if (len > 0)
7953 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02007954 arg = alloc(STRLEN(eap->arg) + len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007955 if (arg != NULL)
7956 {
7957 len = 0;
7958 for (p = eap->arg; *p != NUL; ++p)
7959 {
7960 arg[len++] = *p;
Bram Moolenaar13505972019-01-24 15:04:48 +01007961#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007962 if (*p == CSI)
7963 {
7964 arg[len++] = KS_EXTRA;
7965 arg[len++] = (int)KE_CSI;
7966 }
Bram Moolenaar13505972019-01-24 15:04:48 +01007967#endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007968 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007969 {
7970 arg[len++] = *++p;
7971 if (*p == K_SPECIAL)
7972 {
7973 arg[len++] = KS_SPECIAL;
7974 arg[len++] = KE_FILLER;
7975 }
Bram Moolenaar13505972019-01-24 15:04:48 +01007976#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007977 else if (*p == CSI)
7978 {
7979 arg[len++] = KS_EXTRA;
7980 arg[len++] = (int)KE_CSI;
7981 }
Bram Moolenaar13505972019-01-24 15:04:48 +01007982#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007983 }
7984 arg[len] = NUL;
7985 }
7986 }
7987 }
7988 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007989
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007990 ++ex_normal_busy;
7991 if (save_current_state(&save_state))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007992 {
7993 /*
7994 * Repeat the :normal command for each line in the range. When no
7995 * range given, execute it just once, without positioning the cursor
7996 * first.
7997 */
7998 do
7999 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008000 if (eap->addr_count != 0)
8001 {
8002 curwin->w_cursor.lnum = eap->line1++;
8003 curwin->w_cursor.col = 0;
Bram Moolenaard5d37532017-03-27 23:02:07 +02008004 check_cursor_moved(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008005 }
8006
Bram Moolenaar13505972019-01-24 15:04:48 +01008007 exec_normal_cmd(arg != NULL
8008 ? arg
8009 : eap->arg, eap->forceit ? REMAP_NONE : REMAP_YES, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008010 }
8011 while (eap->addr_count > 0 && eap->line1 <= eap->line2 && !got_int);
8012 }
8013
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008014 // Might not return to the main loop when in an event handler.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008015 update_topline_cursor();
8016
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008017 restore_current_state(&save_state);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008018 --ex_normal_busy;
Bram Moolenaareda73602014-11-05 09:53:23 +01008019 setmouse();
Bram Moolenaareda73602014-11-05 09:53:23 +01008020#ifdef CURSOR_SHAPE
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008021 ui_cursor_shape(); // may show different cursor shape
Bram Moolenaareda73602014-11-05 09:53:23 +01008022#endif
8023
Bram Moolenaar071d4272004-06-13 20:20:40 +00008024 vim_free(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008025}
8026
8027/*
Bram Moolenaar64969662005-12-14 21:59:55 +00008028 * ":startinsert", ":startreplace" and ":startgreplace"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008029 */
8030 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008031ex_startinsert(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008032{
Bram Moolenaarfd371682005-01-14 21:42:54 +00008033 if (eap->forceit)
8034 {
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02008035 // cursor line can be zero on startup
Bram Moolenaar09ca9322017-09-26 17:40:45 +02008036 if (!curwin->w_cursor.lnum)
8037 curwin->w_cursor.lnum = 1;
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02008038 set_cursor_for_append_to_line();
Bram Moolenaarfd371682005-01-14 21:42:54 +00008039 }
Bram Moolenaareb58a242020-04-19 18:13:19 +02008040#ifdef FEAT_TERMINAL
8041 // Ignore this when running in an active terminal.
8042 if (term_job_running(curbuf->b_term))
8043 return;
8044#endif
Bram Moolenaarfd371682005-01-14 21:42:54 +00008045
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02008046 // Ignore the command when already in Insert mode. Inserting an
8047 // expression register that invokes a function can do this.
Bram Moolenaara40c5002005-01-09 21:16:21 +00008048 if (State & INSERT)
8049 return;
8050
Bram Moolenaar64969662005-12-14 21:59:55 +00008051 if (eap->cmdidx == CMD_startinsert)
8052 restart_edit = 'a';
8053 else if (eap->cmdidx == CMD_startreplace)
8054 restart_edit = 'R';
Bram Moolenaar071d4272004-06-13 20:20:40 +00008055 else
Bram Moolenaar64969662005-12-14 21:59:55 +00008056 restart_edit = 'V';
8057
8058 if (!eap->forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008059 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00008060 if (eap->cmdidx == CMD_startinsert)
8061 restart_edit = 'i';
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02008062 curwin->w_curswant = 0; // avoid MAXCOL
Bram Moolenaar071d4272004-06-13 20:20:40 +00008063 }
8064}
8065
8066/*
8067 * ":stopinsert"
8068 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008069 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008070ex_stopinsert(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008071{
8072 restart_edit = 0;
8073 stop_insert_mode = TRUE;
Bram Moolenaarfd773e92016-04-02 19:39:16 +02008074 clearmode();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008075}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008076
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008077/*
8078 * Execute normal mode command "cmd".
8079 * "remap" can be REMAP_NONE or REMAP_YES.
8080 */
8081 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008082exec_normal_cmd(char_u *cmd, int remap, int silent)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008083{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008084 // Stuff the argument into the typeahead buffer.
Bram Moolenaar25281632016-01-21 23:32:32 +01008085 ins_typebuf(cmd, remap, 0, TRUE, silent);
Bram Moolenaar586c70c2018-10-02 16:23:58 +02008086 exec_normal(FALSE, FALSE, FALSE);
Bram Moolenaar25281632016-01-21 23:32:32 +01008087}
Bram Moolenaar25281632016-01-21 23:32:32 +01008088
Bram Moolenaar25281632016-01-21 23:32:32 +01008089/*
8090 * Execute normal_cmd() until there is no typeahead left.
Bram Moolenaar586c70c2018-10-02 16:23:58 +02008091 * When "use_vpeekc" is TRUE use vpeekc() to check for available chars.
Bram Moolenaar25281632016-01-21 23:32:32 +01008092 */
8093 void
Bram Moolenaar586c70c2018-10-02 16:23:58 +02008094exec_normal(int was_typed, int use_vpeekc, int may_use_terminal_loop UNUSED)
Bram Moolenaar25281632016-01-21 23:32:32 +01008095{
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008096 oparg_T oa;
Bram Moolenaar905dd902019-04-07 14:21:47 +02008097 int c;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008098
Bram Moolenaar905dd902019-04-07 14:21:47 +02008099 // When calling vpeekc() from feedkeys() it will return Ctrl_C when there
8100 // is nothing to get, so also check for Ctrl_C.
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008101 clear_oparg(&oa);
8102 finish_op = FALSE;
Bram Moolenaar586c70c2018-10-02 16:23:58 +02008103 while ((!stuff_empty()
8104 || ((was_typed || !typebuf_typed()) && typebuf.tb_len > 0)
Bram Moolenaar905dd902019-04-07 14:21:47 +02008105 || (use_vpeekc && (c = vpeekc()) != NUL && c != Ctrl_C))
Bram Moolenaar586c70c2018-10-02 16:23:58 +02008106 && !got_int)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008107 {
8108 update_topline_cursor();
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02008109#ifdef FEAT_TERMINAL
Bram Moolenaar655a82a2018-05-08 22:01:07 +02008110 if (may_use_terminal_loop && term_use_loop()
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02008111 && oa.op_type == OP_NOP && oa.regname == NUL
8112 && !VIsual_active)
8113 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008114 // If terminal_loop() returns OK we got a key that is handled
8115 // in Normal model. With FAIL we first need to position the
8116 // cursor and the screen needs to be redrawn.
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02008117 if (terminal_loop(TRUE) == OK)
8118 normal_cmd(&oa, TRUE);
8119 }
8120 else
8121#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008122 // execute a Normal mode cmd
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02008123 normal_cmd(&oa, TRUE);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008124 }
8125}
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008126
Bram Moolenaar071d4272004-06-13 20:20:40 +00008127#ifdef FEAT_FIND_ID
8128 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008129ex_checkpath(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008130{
8131 find_pattern_in_path(NULL, 0, 0, FALSE, FALSE, CHECK_PATH, 1L,
8132 eap->forceit ? ACTION_SHOW_ALL : ACTION_SHOW,
8133 (linenr_T)1, (linenr_T)MAXLNUM);
8134}
8135
Bram Moolenaar4033c552017-09-16 20:54:51 +02008136#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008137/*
8138 * ":psearch"
8139 */
8140 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008141ex_psearch(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008142{
8143 g_do_tagpreview = p_pvh;
8144 ex_findpat(eap);
8145 g_do_tagpreview = 0;
8146}
8147#endif
8148
8149 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008150ex_findpat(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008151{
8152 int whole = TRUE;
8153 long n;
8154 char_u *p;
8155 int action;
8156
8157 switch (cmdnames[eap->cmdidx].cmd_name[2])
8158 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008159 case 'e': // ":psearch", ":isearch" and ":dsearch"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008160 if (cmdnames[eap->cmdidx].cmd_name[0] == 'p')
8161 action = ACTION_GOTO;
8162 else
8163 action = ACTION_SHOW;
8164 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008165 case 'i': // ":ilist" and ":dlist"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008166 action = ACTION_SHOW_ALL;
8167 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008168 case 'u': // ":ijump" and ":djump"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008169 action = ACTION_GOTO;
8170 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008171 default: // ":isplit" and ":dsplit"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008172 action = ACTION_SPLIT;
8173 break;
8174 }
8175
8176 n = 1;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008177 if (vim_isdigit(*eap->arg)) // get count
Bram Moolenaar071d4272004-06-13 20:20:40 +00008178 {
8179 n = getdigits(&eap->arg);
8180 eap->arg = skipwhite(eap->arg);
8181 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008182 if (*eap->arg == '/') // Match regexp, not just whole words
Bram Moolenaar071d4272004-06-13 20:20:40 +00008183 {
8184 whole = FALSE;
8185 ++eap->arg;
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02008186 p = skip_regexp(eap->arg, '/', p_magic);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008187 if (*p)
8188 {
8189 *p++ = NUL;
8190 p = skipwhite(p);
8191
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008192 // Check for trailing illegal characters
Bram Moolenaarfaac4102020-04-20 17:46:14 +02008193 if (!ends_excmd2(eap->arg, p))
Bram Moolenaar8930caa2020-07-23 16:37:03 +02008194 eap->errmsg = ex_errmsg(e_trailing_arg, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008195 else
8196 eap->nextcmd = check_nextcmd(p);
8197 }
8198 }
8199 if (!eap->skip)
8200 find_pattern_in_path(eap->arg, 0, (int)STRLEN(eap->arg),
8201 whole, !eap->forceit,
8202 *eap->cmd == 'd' ? FIND_DEFINE : FIND_ANY,
8203 n, action, eap->line1, eap->line2);
8204}
8205#endif
8206
Bram Moolenaar071d4272004-06-13 20:20:40 +00008207
Bram Moolenaar4033c552017-09-16 20:54:51 +02008208#ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00008209/*
8210 * ":ptag", ":ptselect", ":ptjump", ":ptnext", etc.
8211 */
8212 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008213ex_ptag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008214{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008215 g_do_tagpreview = p_pvh; // will be reset to 0 in ex_tag_cmd()
Bram Moolenaar071d4272004-06-13 20:20:40 +00008216 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
8217}
8218
8219/*
8220 * ":pedit"
8221 */
8222 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008223ex_pedit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008224{
8225 win_T *curwin_save = curwin;
8226
Bram Moolenaar3a2505c2020-03-09 16:40:41 +01008227 if (ERROR_IF_ANY_POPUP_WINDOW)
8228 return;
8229
Bram Moolenaar026587b2019-08-17 15:08:00 +02008230 // Open the preview window or popup and make it the current window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008231 g_do_tagpreview = p_pvh;
Bram Moolenaar576a4a62019-08-18 15:25:17 +02008232 prepare_tagpreview(TRUE, TRUE, FALSE);
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02008233
Bram Moolenaar026587b2019-08-17 15:08:00 +02008234 // Edit the file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008235 do_exedit(eap, NULL);
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02008236
Bram Moolenaar071d4272004-06-13 20:20:40 +00008237 if (curwin != curwin_save && win_valid(curwin_save))
8238 {
Bram Moolenaar026587b2019-08-17 15:08:00 +02008239 // Return cursor to where we were
Bram Moolenaar071d4272004-06-13 20:20:40 +00008240 validate_cursor();
8241 redraw_later(VALID);
8242 win_enter(curwin_save, TRUE);
8243 }
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01008244# ifdef FEAT_PROP_POPUP
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02008245 else if (WIN_IS_POPUP(curwin))
8246 {
8247 // can't keep focus in popup window
8248 win_enter(firstwin, TRUE);
8249 }
8250# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008251 g_do_tagpreview = 0;
8252}
Bram Moolenaar4033c552017-09-16 20:54:51 +02008253#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008254
8255/*
8256 * ":stag", ":stselect" and ":stjump".
8257 */
8258 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008259ex_stag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008260{
8261 postponed_split = -1;
8262 postponed_split_flags = cmdmod.split;
Bram Moolenaard326ce82007-03-11 14:48:29 +00008263 postponed_split_tab = cmdmod.tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008264 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
8265 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00008266 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008267}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008268
8269/*
8270 * ":tag", ":tselect", ":tjump", ":tnext", etc.
8271 */
8272 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008273ex_tag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008274{
8275 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name);
8276}
8277
8278 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008279ex_tag_cmd(exarg_T *eap, char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008280{
8281 int cmd;
8282
8283 switch (name[1])
8284 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008285 case 'j': cmd = DT_JUMP; // ":tjump"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008286 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008287 case 's': cmd = DT_SELECT; // ":tselect"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008288 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008289 case 'p': cmd = DT_PREV; // ":tprevious"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008290 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008291 case 'N': cmd = DT_PREV; // ":tNext"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008292 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008293 case 'n': cmd = DT_NEXT; // ":tnext"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008294 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008295 case 'o': cmd = DT_POP; // ":pop"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008296 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008297 case 'f': // ":tfirst"
8298 case 'r': cmd = DT_FIRST; // ":trewind"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008299 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008300 case 'l': cmd = DT_LAST; // ":tlast"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008301 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008302 default: // ":tag"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008303#ifdef FEAT_CSCOPE
Bram Moolenaar7c94c262008-06-20 09:11:34 +00008304 if (p_cst && *eap->arg != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008305 {
Bram Moolenaard4db7712016-11-12 19:16:46 +01008306 ex_cstag(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008307 return;
8308 }
8309#endif
8310 cmd = DT_TAG;
8311 break;
8312 }
8313
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00008314 if (name[0] == 'l')
8315 {
8316#ifndef FEAT_QUICKFIX
8317 ex_ni(eap);
8318 return;
8319#else
8320 cmd = DT_LTAG;
8321#endif
8322 }
8323
Bram Moolenaar071d4272004-06-13 20:20:40 +00008324 do_tag(eap->arg, cmd, eap->addr_count > 0 ? (int)eap->line2 : 1,
8325 eap->forceit, TRUE);
8326}
8327
8328/*
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008329 * Check "str" for starting with a special cmdline variable.
8330 * If found return one of the SPEC_ values and set "*usedlen" to the length of
8331 * the variable. Otherwise return -1 and "*usedlen" is unchanged.
8332 */
8333 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008334find_cmdline_var(char_u *src, int *usedlen)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008335{
8336 int len;
8337 int i;
Bram Moolenaar8f0b2d42009-05-16 14:41:10 +00008338 static char *(spec_str[]) = {
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008339 "%",
8340#define SPEC_PERC 0
8341 "#",
Bram Moolenaar65f08472017-09-10 18:16:20 +02008342#define SPEC_HASH (SPEC_PERC + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008343 "<cword>", // cursor word
Bram Moolenaar65f08472017-09-10 18:16:20 +02008344#define SPEC_CWORD (SPEC_HASH + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008345 "<cWORD>", // cursor WORD
Bram Moolenaar65f08472017-09-10 18:16:20 +02008346#define SPEC_CCWORD (SPEC_CWORD + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008347 "<cexpr>", // expr under cursor
Bram Moolenaar65f08472017-09-10 18:16:20 +02008348#define SPEC_CEXPR (SPEC_CCWORD + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008349 "<cfile>", // cursor path name
Bram Moolenaar65f08472017-09-10 18:16:20 +02008350#define SPEC_CFILE (SPEC_CEXPR + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008351 "<sfile>", // ":so" file name
Bram Moolenaar65f08472017-09-10 18:16:20 +02008352#define SPEC_SFILE (SPEC_CFILE + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008353 "<slnum>", // ":so" file line number
Bram Moolenaar65f08472017-09-10 18:16:20 +02008354#define SPEC_SLNUM (SPEC_SFILE + 1)
Bram Moolenaara5d04232020-07-26 15:37:02 +02008355 "<stack>", // call stack
8356#define SPEC_STACK (SPEC_SLNUM + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008357 "<afile>", // autocommand file name
Bram Moolenaara5d04232020-07-26 15:37:02 +02008358#define SPEC_AFILE (SPEC_STACK + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008359 "<abuf>", // autocommand buffer number
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008360#define SPEC_ABUF (SPEC_AFILE + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008361 "<amatch>", // autocommand match name
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01008362#define SPEC_AMATCH (SPEC_ABUF + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008363 "<sflnum>", // script file line number
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008364#define SPEC_SFLNUM (SPEC_AMATCH + 1)
Bram Moolenaar90944302020-08-01 20:45:11 +02008365 "<SID>", // script ID: <SNR>123_
8366#define SPEC_SID (SPEC_SFLNUM + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008367#ifdef FEAT_CLIENTSERVER
8368 "<client>"
Bram Moolenaar90944302020-08-01 20:45:11 +02008369# define SPEC_CLIENT (SPEC_SID + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008370#endif
8371 };
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008372
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00008373 for (i = 0; i < (int)(sizeof(spec_str) / sizeof(char *)); ++i)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008374 {
8375 len = (int)STRLEN(spec_str[i]);
8376 if (STRNCMP(src, spec_str[i], len) == 0)
8377 {
8378 *usedlen = len;
8379 return i;
8380 }
8381 }
8382 return -1;
8383}
8384
8385/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008386 * Evaluate cmdline variables.
8387 *
8388 * change '%' to curbuf->b_ffname
8389 * '#' to curwin->w_altfile
8390 * '<cword>' to word under the cursor
8391 * '<cWORD>' to WORD under the cursor
Bram Moolenaar8071cb22019-07-12 17:58:01 +02008392 * '<cexpr>' to C-expression under the cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00008393 * '<cfile>' to path name under the cursor
8394 * '<sfile>' to sourced file name
Bram Moolenaar4f25b1a2020-09-10 19:25:05 +02008395 * '<stack>' to call stack
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01008396 * '<slnum>' to sourced file line number
Bram Moolenaar071d4272004-06-13 20:20:40 +00008397 * '<afile>' to file name for autocommand
8398 * '<abuf>' to buffer number for autocommand
8399 * '<amatch>' to matching name for autocommand
8400 *
8401 * When an error is detected, "errormsg" is set to a non-NULL pointer (may be
8402 * "" for error without a message) and NULL is returned.
8403 * Returns an allocated string if a valid match was found.
8404 * Returns NULL if no match was found. "usedlen" then still contains the
8405 * number of characters to skip.
8406 */
8407 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008408eval_vars(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008409 char_u *src, // pointer into commandline
8410 char_u *srcstart, // beginning of valid memory for src
8411 int *usedlen, // characters after src that are used
8412 linenr_T *lnump, // line number for :e command, or NULL
8413 char **errormsg, // pointer to error message
8414 int *escaped) // return value has escaped white space (can
8415 // be NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008416{
8417 int i;
8418 char_u *s;
8419 char_u *result;
8420 char_u *resultbuf = NULL;
8421 int resultlen;
8422 buf_T *buf;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008423 int valid = VALID_HEAD + VALID_PATH; // assume valid result
Bram Moolenaar071d4272004-06-13 20:20:40 +00008424 int spec_idx;
Bram Moolenaarfd249462018-07-28 16:14:30 +02008425 int tilde_file = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008426 int skip_mod = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008427 char_u strbuf[30];
Bram Moolenaar071d4272004-06-13 20:20:40 +00008428
8429 *errormsg = NULL;
Bram Moolenaar63b92542007-03-27 14:57:09 +00008430 if (escaped != NULL)
8431 *escaped = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008432
8433 /*
8434 * Check if there is something to do.
8435 */
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008436 spec_idx = find_cmdline_var(src, usedlen);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008437 if (spec_idx < 0) // no match
Bram Moolenaar071d4272004-06-13 20:20:40 +00008438 {
8439 *usedlen = 1;
8440 return NULL;
8441 }
8442
8443 /*
8444 * Skip when preceded with a backslash "\%" and "\#".
8445 * Note: In "\\%" the % is also not recognized!
8446 */
8447 if (src > srcstart && src[-1] == '\\')
8448 {
8449 *usedlen = 0;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008450 STRMOVE(src - 1, src); // remove backslash
Bram Moolenaar071d4272004-06-13 20:20:40 +00008451 return NULL;
8452 }
8453
8454 /*
8455 * word or WORD under cursor
8456 */
Bram Moolenaar65f08472017-09-10 18:16:20 +02008457 if (spec_idx == SPEC_CWORD || spec_idx == SPEC_CCWORD
8458 || spec_idx == SPEC_CEXPR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008459 {
Bram Moolenaar65f08472017-09-10 18:16:20 +02008460 resultlen = find_ident_under_cursor(&result,
8461 spec_idx == SPEC_CWORD ? (FIND_IDENT | FIND_STRING)
8462 : spec_idx == SPEC_CEXPR ? (FIND_IDENT | FIND_STRING | FIND_EVAL)
8463 : FIND_STRING);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008464 if (resultlen == 0)
8465 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008466 *errormsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +00008467 return NULL;
8468 }
8469 }
8470
8471 /*
8472 * '#': Alternate file name
8473 * '%': Current file name
8474 * File name under the cursor
8475 * File name for autocommand
8476 * and following modifiers
8477 */
8478 else
8479 {
8480 switch (spec_idx)
8481 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008482 case SPEC_PERC: // '%': current file
Bram Moolenaar071d4272004-06-13 20:20:40 +00008483 if (curbuf->b_fname == NULL)
8484 {
8485 result = (char_u *)"";
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008486 valid = 0; // Must have ":p:h" to be valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00008487 }
8488 else
Bram Moolenaar00136dc2018-07-25 21:19:13 +02008489 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008490 result = curbuf->b_fname;
Bram Moolenaar00136dc2018-07-25 21:19:13 +02008491 tilde_file = STRCMP(result, "~") == 0;
8492 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008493 break;
8494
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008495 case SPEC_HASH: // '#' or "#99": alternate file
8496 if (src[1] == '#') // "##": the argument list
Bram Moolenaar071d4272004-06-13 20:20:40 +00008497 {
8498 result = arg_all();
8499 resultbuf = result;
8500 *usedlen = 2;
Bram Moolenaar63b92542007-03-27 14:57:09 +00008501 if (escaped != NULL)
8502 *escaped = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008503 skip_mod = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008504 break;
8505 }
8506 s = src + 1;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008507 if (*s == '<') // "#<99" uses v:oldfiles
Bram Moolenaard812df62008-11-09 12:46:09 +00008508 ++s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008509 i = (int)getdigits(&s);
Bram Moolenaarc312b8b2017-10-28 17:53:04 +02008510 if (s == src + 2 && src[1] == '-')
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008511 // just a minus sign, don't skip over it
Bram Moolenaarc312b8b2017-10-28 17:53:04 +02008512 s--;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008513 *usedlen = (int)(s - src); // length of what we expand
Bram Moolenaar071d4272004-06-13 20:20:40 +00008514
Bram Moolenaarc312b8b2017-10-28 17:53:04 +02008515 if (src[1] == '<' && i != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008516 {
Bram Moolenaard812df62008-11-09 12:46:09 +00008517 if (*usedlen < 2)
8518 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008519 // Should we give an error message for #<text?
Bram Moolenaard812df62008-11-09 12:46:09 +00008520 *usedlen = 1;
8521 return NULL;
8522 }
8523#ifdef FEAT_EVAL
8524 result = list_find_str(get_vim_var_list(VV_OLDFILES),
8525 (long)i);
8526 if (result == NULL)
8527 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008528 *errormsg = "";
Bram Moolenaard812df62008-11-09 12:46:09 +00008529 return NULL;
8530 }
8531#else
Bram Moolenaar8e481e82019-01-15 20:07:48 +01008532 *errormsg = _("E809: #< is not available without the +eval feature");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008533 return NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +00008534#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008535 }
8536 else
Bram Moolenaard812df62008-11-09 12:46:09 +00008537 {
Bram Moolenaarc312b8b2017-10-28 17:53:04 +02008538 if (i == 0 && src[1] == '<' && *usedlen > 1)
8539 *usedlen = 1;
Bram Moolenaard812df62008-11-09 12:46:09 +00008540 buf = buflist_findnr(i);
8541 if (buf == NULL)
8542 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008543 *errormsg = _("E194: No alternate file name to substitute for '#'");
Bram Moolenaard812df62008-11-09 12:46:09 +00008544 return NULL;
8545 }
8546 if (lnump != NULL)
8547 *lnump = ECMD_LAST;
8548 if (buf->b_fname == NULL)
8549 {
8550 result = (char_u *)"";
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008551 valid = 0; // Must have ":p:h" to be valid
Bram Moolenaard812df62008-11-09 12:46:09 +00008552 }
8553 else
Bram Moolenaar00136dc2018-07-25 21:19:13 +02008554 {
Bram Moolenaard812df62008-11-09 12:46:09 +00008555 result = buf->b_fname;
Bram Moolenaar00136dc2018-07-25 21:19:13 +02008556 tilde_file = STRCMP(result, "~") == 0;
8557 }
Bram Moolenaard812df62008-11-09 12:46:09 +00008558 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008559 break;
8560
8561#ifdef FEAT_SEARCHPATH
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008562 case SPEC_CFILE: // file name under cursor
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008563 result = file_name_at_cursor(FNAME_MESS|FNAME_HYP, 1L, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008564 if (result == NULL)
8565 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008566 *errormsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +00008567 return NULL;
8568 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008569 resultbuf = result; // remember allocated string
Bram Moolenaar071d4272004-06-13 20:20:40 +00008570 break;
8571#endif
8572
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008573 case SPEC_AFILE: // file name for autocommand
Bram Moolenaar071d4272004-06-13 20:20:40 +00008574 result = autocmd_fname;
Bram Moolenaarf6dad432008-09-18 19:29:58 +00008575 if (result != NULL && !autocmd_fname_full)
8576 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008577 // Still need to turn the fname into a full path. It is
8578 // postponed to avoid a delay when <afile> is not used.
Bram Moolenaarf6dad432008-09-18 19:29:58 +00008579 autocmd_fname_full = TRUE;
8580 result = FullName_save(autocmd_fname, FALSE);
8581 vim_free(autocmd_fname);
8582 autocmd_fname = result;
8583 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008584 if (result == NULL)
8585 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008586 *errormsg = _("E495: no autocommand file name to substitute for \"<afile>\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008587 return NULL;
8588 }
Bram Moolenaara0174af2008-01-02 20:08:25 +00008589 result = shorten_fname1(result);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008590 break;
8591
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008592 case SPEC_ABUF: // buffer number for autocommand
Bram Moolenaar071d4272004-06-13 20:20:40 +00008593 if (autocmd_bufnr <= 0)
8594 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008595 *errormsg = _("E496: no autocommand buffer number to substitute for \"<abuf>\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008596 return NULL;
8597 }
8598 sprintf((char *)strbuf, "%d", autocmd_bufnr);
8599 result = strbuf;
8600 break;
8601
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008602 case SPEC_AMATCH: // match name for autocommand
Bram Moolenaar071d4272004-06-13 20:20:40 +00008603 result = autocmd_match;
8604 if (result == NULL)
8605 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008606 *errormsg = _("E497: no autocommand match name to substitute for \"<amatch>\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008607 return NULL;
8608 }
8609 break;
8610
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008611 case SPEC_SFILE: // file name for ":so" command
Bram Moolenaara5d04232020-07-26 15:37:02 +02008612 case SPEC_STACK: // call stack
Bram Moolenaar4f25b1a2020-09-10 19:25:05 +02008613 result = estack_sfile(spec_idx == SPEC_SFILE
8614 ? ESTACK_SFILE : ESTACK_STACK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008615 if (result == NULL)
8616 {
Bram Moolenaara5d04232020-07-26 15:37:02 +02008617 *errormsg = spec_idx == SPEC_SFILE
8618 ? _("E498: no :source file name to substitute for \"<sfile>\"")
8619 : _("E489: no call stack to substitute for \"<stack>\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008620 return NULL;
8621 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01008622 resultbuf = result; // remember allocated string
Bram Moolenaar071d4272004-06-13 20:20:40 +00008623 break;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008624
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008625 case SPEC_SLNUM: // line in file for ":so" command
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01008626 if (SOURCING_NAME == NULL || SOURCING_LNUM == 0)
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01008627 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008628 *errormsg = _("E842: no line number to use for \"<slnum>\"");
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01008629 return NULL;
8630 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01008631 sprintf((char *)strbuf, "%ld", SOURCING_LNUM);
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01008632 result = strbuf;
8633 break;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008634
8635#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008636 case SPEC_SFLNUM: // line in script file
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01008637 if (current_sctx.sc_lnum + SOURCING_LNUM == 0)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008638 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008639 *errormsg = _("E961: no line number to use for \"<sflnum>\"");
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008640 return NULL;
8641 }
8642 sprintf((char *)strbuf, "%ld",
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01008643 (long)(current_sctx.sc_lnum + SOURCING_LNUM));
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008644 result = strbuf;
8645 break;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008646
Bram Moolenaar90944302020-08-01 20:45:11 +02008647 case SPEC_SID:
8648 if (current_sctx.sc_sid <= 0)
8649 {
8650 *errormsg = _(e_usingsid);
8651 return NULL;
8652 }
8653 sprintf((char *)strbuf, "<SNR>%d_", current_sctx.sc_sid);
8654 result = strbuf;
8655 break;
Bram Moolenaare4218b92020-08-01 21:11:38 +02008656#endif
Bram Moolenaar90944302020-08-01 20:45:11 +02008657
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008658#ifdef FEAT_CLIENTSERVER
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008659 case SPEC_CLIENT: // Source of last submitted input
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008660 sprintf((char *)strbuf, PRINTF_HEX_LONG_U,
8661 (long_u)clientWindow);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008662 result = strbuf;
8663 break;
8664#endif
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008665
Bram Moolenaar9e0f6ec2017-05-16 09:36:54 +02008666 default:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008667 result = (char_u *)""; // avoid gcc warning
Bram Moolenaar9e0f6ec2017-05-16 09:36:54 +02008668 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008669 }
8670
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008671 resultlen = (int)STRLEN(result); // length of new string
8672 if (src[*usedlen] == '<') // remove the file name extension
Bram Moolenaar071d4272004-06-13 20:20:40 +00008673 {
8674 ++*usedlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008675 if ((s = vim_strrchr(result, '.')) != NULL && s >= gettail(result))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008676 resultlen = (int)(s - result);
8677 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008678 else if (!skip_mod)
8679 {
Bram Moolenaar00136dc2018-07-25 21:19:13 +02008680 valid |= modify_fname(src, tilde_file, usedlen, &result, &resultbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008681 &resultlen);
8682 if (result == NULL)
8683 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008684 *errormsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +00008685 return NULL;
8686 }
8687 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008688 }
8689
8690 if (resultlen == 0 || valid != VALID_HEAD + VALID_PATH)
8691 {
8692 if (valid != VALID_HEAD + VALID_PATH)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008693 // xgettext:no-c-format
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008694 *errormsg = _("E499: Empty file name for '%' or '#', only works with \":p:h\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008695 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008696 *errormsg = _("E500: Evaluates to an empty string");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008697 result = NULL;
8698 }
8699 else
8700 result = vim_strnsave(result, resultlen);
8701 vim_free(resultbuf);
8702 return result;
8703}
8704
8705/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008706 * Expand the <sfile> string in "arg".
8707 *
8708 * Returns an allocated string, or NULL for any error.
8709 */
8710 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008711expand_sfile(char_u *arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008712{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008713 char *errormsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008714 int len;
8715 char_u *result;
8716 char_u *newres;
8717 char_u *repl;
8718 int srclen;
8719 char_u *p;
8720
8721 result = vim_strsave(arg);
8722 if (result == NULL)
8723 return NULL;
8724
8725 for (p = result; *p; )
8726 {
8727 if (STRNCMP(p, "<sfile>", 7) != 0)
8728 ++p;
8729 else
8730 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008731 // replace "<sfile>" with the sourced file name, and do ":" stuff
Bram Moolenaar63b92542007-03-27 14:57:09 +00008732 repl = eval_vars(p, result, &srclen, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008733 if (errormsg != NULL)
8734 {
8735 if (*errormsg)
8736 emsg(errormsg);
8737 vim_free(result);
8738 return NULL;
8739 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008740 if (repl == NULL) // no match (cannot happen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008741 {
8742 p += srclen;
8743 continue;
8744 }
8745 len = (int)STRLEN(result) - srclen + (int)STRLEN(repl) + 1;
8746 newres = alloc(len);
8747 if (newres == NULL)
8748 {
8749 vim_free(repl);
8750 vim_free(result);
8751 return NULL;
8752 }
8753 mch_memmove(newres, result, (size_t)(p - result));
8754 STRCPY(newres + (p - result), repl);
8755 len = (int)STRLEN(newres);
8756 STRCAT(newres, p + srclen);
8757 vim_free(repl);
8758 vim_free(result);
8759 result = newres;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008760 p = newres + len; // continue after the match
Bram Moolenaar071d4272004-06-13 20:20:40 +00008761 }
8762 }
8763
8764 return result;
8765}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008766
Bram Moolenaar071d4272004-06-13 20:20:40 +00008767#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG) || defined(PROTO)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00008768/*
Bram Moolenaard9462e32011-04-11 21:35:11 +02008769 * Make a dialog message in "buff[DIALOG_MSG_SIZE]".
Bram Moolenaarb765d632005-06-07 21:00:02 +00008770 * "format" must contain "%s".
Bram Moolenaar9c13b352005-05-19 20:53:52 +00008771 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008772 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008773dialog_msg(char_u *buff, char *format, char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008774{
Bram Moolenaar071d4272004-06-13 20:20:40 +00008775 if (fname == NULL)
8776 fname = (char_u *)_("Untitled");
Bram Moolenaard9462e32011-04-11 21:35:11 +02008777 vim_snprintf((char *)buff, DIALOG_MSG_SIZE, format, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008778}
8779#endif
8780
8781/*
8782 * ":behave {mswin,xterm}"
8783 */
8784 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008785ex_behave(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008786{
8787 if (STRCMP(eap->arg, "mswin") == 0)
8788 {
8789 set_option_value((char_u *)"selection", 0L, (char_u *)"exclusive", 0);
8790 set_option_value((char_u *)"selectmode", 0L, (char_u *)"mouse,key", 0);
8791 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"popup", 0);
8792 set_option_value((char_u *)"keymodel", 0L,
8793 (char_u *)"startsel,stopsel", 0);
8794 }
8795 else if (STRCMP(eap->arg, "xterm") == 0)
8796 {
8797 set_option_value((char_u *)"selection", 0L, (char_u *)"inclusive", 0);
8798 set_option_value((char_u *)"selectmode", 0L, (char_u *)"", 0);
8799 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"extend", 0);
8800 set_option_value((char_u *)"keymodel", 0L, (char_u *)"", 0);
8801 }
8802 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008803 semsg(_(e_invarg2), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008804}
8805
Bram Moolenaar071d4272004-06-13 20:20:40 +00008806static int filetype_detect = FALSE;
8807static int filetype_plugin = FALSE;
8808static int filetype_indent = FALSE;
8809
8810/*
8811 * ":filetype [plugin] [indent] {on,off,detect}"
8812 * on: Load the filetype.vim file to install autocommands for file types.
8813 * off: Load the ftoff.vim file to remove all autocommands for file types.
8814 * plugin on: load filetype.vim and ftplugin.vim
8815 * plugin off: load ftplugof.vim
8816 * indent on: load filetype.vim and indent.vim
8817 * indent off: load indoff.vim
8818 */
8819 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008820ex_filetype(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008821{
8822 char_u *arg = eap->arg;
8823 int plugin = FALSE;
8824 int indent = FALSE;
8825
8826 if (*eap->arg == NUL)
8827 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008828 // Print current status.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008829 smsg("filetype detection:%s plugin:%s indent:%s",
Bram Moolenaar071d4272004-06-13 20:20:40 +00008830 filetype_detect ? "ON" : "OFF",
8831 filetype_plugin ? (filetype_detect ? "ON" : "(on)") : "OFF",
8832 filetype_indent ? (filetype_detect ? "ON" : "(on)") : "OFF");
8833 return;
8834 }
8835
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008836 // Accept "plugin" and "indent" in any order.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008837 for (;;)
8838 {
8839 if (STRNCMP(arg, "plugin", 6) == 0)
8840 {
8841 plugin = TRUE;
8842 arg = skipwhite(arg + 6);
8843 continue;
8844 }
8845 if (STRNCMP(arg, "indent", 6) == 0)
8846 {
8847 indent = TRUE;
8848 arg = skipwhite(arg + 6);
8849 continue;
8850 }
8851 break;
8852 }
8853 if (STRCMP(arg, "on") == 0 || STRCMP(arg, "detect") == 0)
8854 {
8855 if (*arg == 'o' || !filetype_detect)
8856 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01008857 source_runtime((char_u *)FILETYPE_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008858 filetype_detect = TRUE;
8859 if (plugin)
8860 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01008861 source_runtime((char_u *)FTPLUGIN_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008862 filetype_plugin = TRUE;
8863 }
8864 if (indent)
8865 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01008866 source_runtime((char_u *)INDENT_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008867 filetype_indent = TRUE;
8868 }
8869 }
8870 if (*arg == 'd')
8871 {
Bram Moolenaar1610d052016-06-09 22:53:01 +02008872 (void)do_doautocmd((char_u *)"filetypedetect BufRead", TRUE, NULL);
Bram Moolenaara3227e22006-03-08 21:32:40 +00008873 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008874 }
8875 }
8876 else if (STRCMP(arg, "off") == 0)
8877 {
8878 if (plugin || indent)
8879 {
8880 if (plugin)
8881 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01008882 source_runtime((char_u *)FTPLUGOF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008883 filetype_plugin = FALSE;
8884 }
8885 if (indent)
8886 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01008887 source_runtime((char_u *)INDOFF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008888 filetype_indent = FALSE;
8889 }
8890 }
8891 else
8892 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01008893 source_runtime((char_u *)FTOFF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008894 filetype_detect = FALSE;
8895 }
8896 }
8897 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008898 semsg(_(e_invarg2), arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008899}
8900
8901/*
Bram Moolenaar3e545692017-06-04 19:00:32 +02008902 * ":setfiletype [FALLBACK] {name}"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008903 */
8904 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008905ex_setfiletype(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008906{
8907 if (!did_filetype)
Bram Moolenaar3e545692017-06-04 19:00:32 +02008908 {
8909 char_u *arg = eap->arg;
8910
8911 if (STRNCMP(arg, "FALLBACK ", 9) == 0)
8912 arg += 9;
8913
8914 set_option_value((char_u *)"filetype", 0L, arg, OPT_LOCAL);
8915 if (arg != eap->arg)
8916 did_filetype = FALSE;
8917 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008918}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008919
Bram Moolenaar071d4272004-06-13 20:20:40 +00008920 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008921ex_digraphs(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008922{
8923#ifdef FEAT_DIGRAPHS
8924 if (*eap->arg != NUL)
8925 putdigraph(eap->arg);
8926 else
Bram Moolenaareae8ae12018-12-14 18:53:02 +01008927 listdigraphs(eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008928#else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008929 emsg(_("E196: No digraphs in this version"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008930#endif
8931}
8932
Bram Moolenaar451fc7b2018-04-27 22:53:07 +02008933#if defined(FEAT_SEARCH_EXTRA) || defined(PROTO)
8934 void
8935set_no_hlsearch(int flag)
8936{
8937 no_hlsearch = flag;
8938# ifdef FEAT_EVAL
8939 set_vim_var_nr(VV_HLSEARCH, !no_hlsearch && p_hls);
8940# endif
8941}
8942
Bram Moolenaar071d4272004-06-13 20:20:40 +00008943/*
8944 * ":nohlsearch"
8945 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008946 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008947ex_nohlsearch(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008948{
Bram Moolenaar451fc7b2018-04-27 22:53:07 +02008949 set_no_hlsearch(TRUE);
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00008950 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008951}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008952#endif
8953
8954#ifdef FEAT_CRYPT
8955/*
8956 * ":X": Get crypt key
8957 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008958 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008959ex_X(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008960{
Bram Moolenaar3a0c9082014-11-12 15:15:42 +01008961 crypt_check_current_method();
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02008962 (void)crypt_get_key(TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008963}
8964#endif
8965
8966#ifdef FEAT_FOLDING
8967 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008968ex_fold(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008969{
8970 if (foldManualAllowed(TRUE))
8971 foldCreate(eap->line1, eap->line2);
8972}
8973
8974 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008975ex_foldopen(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008976{
8977 opFoldRange(eap->line1, eap->line2, eap->cmdidx == CMD_foldopen,
8978 eap->forceit, FALSE);
8979}
8980
8981 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008982ex_folddo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008983{
8984 linenr_T lnum;
8985
Bram Moolenaar4facea32019-10-12 20:17:40 +02008986# ifdef FEAT_CLIPBOARD
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02008987 start_global_changes();
Bram Moolenaar4facea32019-10-12 20:17:40 +02008988# endif
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02008989
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008990 // First set the marks for all lines closed/open.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008991 for (lnum = eap->line1; lnum <= eap->line2; ++lnum)
8992 if (hasFolding(lnum, NULL, NULL) == (eap->cmdidx == CMD_folddoclosed))
8993 ml_setmarked(lnum);
8994
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008995 // Execute the command on the marked lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008996 global_exe(eap->arg);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008997 ml_clearmarked(); // clear rest of the marks
Bram Moolenaar4facea32019-10-12 20:17:40 +02008998# ifdef FEAT_CLIPBOARD
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02008999 end_global_changes();
Bram Moolenaar4facea32019-10-12 20:17:40 +02009000# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009001}
9002#endif
Bram Moolenaarf5291f32017-09-14 22:55:37 +02009003
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009004#if defined(FEAT_QUICKFIX) || defined(PROTO)
Bram Moolenaar39665952018-08-15 20:59:48 +02009005/*
9006 * Returns TRUE if the supplied Ex cmdidx is for a location list command
9007 * instead of a quickfix command.
9008 */
9009 int
9010is_loclist_cmd(int cmdidx)
9011{
Bram Moolenaar74c8be22018-08-23 22:51:40 +02009012 if (cmdidx < 0 || cmdidx >= CMD_SIZE)
Bram Moolenaar39665952018-08-15 20:59:48 +02009013 return FALSE;
9014 return cmdnames[cmdidx].cmd_name[0] == 'l';
9015}
9016#endif
9017
Bram Moolenaar4facea32019-10-12 20:17:40 +02009018#if defined(FEAT_TIMERS) || defined(PROTO)
Bram Moolenaarf5291f32017-09-14 22:55:37 +02009019 int
9020get_pressedreturn(void)
9021{
9022 return ex_pressedreturn;
9023}
9024
9025 void
9026set_pressedreturn(int val)
9027{
9028 ex_pressedreturn = val;
9029}
9030#endif