blob: 1d3cfcdcd078e5f8b5c98a0887b8397cd3ee4009 [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
1782 ea.cmd = skip_range(ea.cmd, NULL);
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001783 if (*ea.cmd == '*' && vim_strchr(p_cpo, CPO_STAR) == NULL)
1784 ea.cmd = skipwhite(ea.cmd + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001785
1786#ifdef FEAT_EVAL
Bram Moolenaar3d48e252020-07-15 14:15:52 +02001787 if (vim9script && !starts_with_colon)
Bram Moolenaardf069ee2020-06-22 23:02:51 +02001788 {
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02001789 if (ea.cmd == cmd + 1 && *cmd == '$')
1790 // should be "$VAR = val"
1791 --ea.cmd;
1792 else if (ea.cmd > cmd)
Bram Moolenaardf069ee2020-06-22 23:02:51 +02001793 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02001794 emsg(_(e_colon_required_before_a_range));
Bram Moolenaardf069ee2020-06-22 23:02:51 +02001795 goto doend;
1796 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001797 p = find_ex_command(&ea, NULL, lookup_scriptvar, NULL);
Bram Moolenaardf069ee2020-06-22 23:02:51 +02001798 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001799 else
1800#endif
1801 p = find_ex_command(&ea, NULL, NULL, NULL);
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001802
Bram Moolenaar7feb35e2018-08-21 17:49:54 +02001803#ifdef FEAT_EVAL
1804# ifdef FEAT_PROFILE
1805 // Count this line for profiling if skip is TRUE.
1806 if (do_profiling == PROF_YES
1807 && (!ea.skip || cstack->cs_idx == 0 || (cstack->cs_idx > 0
1808 && (cstack->cs_flags[cstack->cs_idx - 1] & CSF_ACTIVE))))
1809 {
1810 int skip = did_emsg || got_int || did_throw;
1811
1812 if (ea.cmdidx == CMD_catch)
1813 skip = !skip && !(cstack->cs_idx >= 0
1814 && (cstack->cs_flags[cstack->cs_idx] & CSF_THROWN)
1815 && !(cstack->cs_flags[cstack->cs_idx] & CSF_CAUGHT));
1816 else if (ea.cmdidx == CMD_else || ea.cmdidx == CMD_elseif)
1817 skip = skip || !(cstack->cs_idx >= 0
1818 && !(cstack->cs_flags[cstack->cs_idx]
1819 & (CSF_ACTIVE | CSF_TRUE)));
1820 else if (ea.cmdidx == CMD_finally)
1821 skip = FALSE;
1822 else if (ea.cmdidx != CMD_endif
1823 && ea.cmdidx != CMD_endfor
1824 && ea.cmdidx != CMD_endtry
1825 && ea.cmdidx != CMD_endwhile)
1826 skip = ea.skip;
1827
1828 if (!skip)
1829 {
1830 if (getline_equal(fgetline, cookie, get_func_line))
1831 func_line_exec(getline_cookie(fgetline, cookie));
1832 else if (getline_equal(fgetline, cookie, getsourceline))
1833 script_line_exec();
1834 }
1835 }
1836# endif
1837
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001838 // May go to debug mode. If this happens and the ">quit" debug command is
1839 // used, throw an interrupt exception and skip the next command.
Bram Moolenaar7feb35e2018-08-21 17:49:54 +02001840 dbg_check_breakpoint(&ea);
1841 if (!ea.skip && got_int)
1842 {
1843 ea.skip = TRUE;
1844 (void)do_intthrow(cstack);
1845 }
1846#endif
1847
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001848/*
1849 * 4. parse a range specifier of the form: addr [,addr] [;addr] ..
Bram Moolenaar071d4272004-06-13 20:20:40 +00001850 *
1851 * where 'addr' is:
1852 *
1853 * % (entire file)
1854 * $ [+-NUM]
1855 * 'x [+-NUM] (where x denotes a currently defined mark)
1856 * . [+-NUM]
1857 * [+-NUM]..
1858 * NUM
1859 *
1860 * The ea.cmd pointer is updated to point to the first character following the
1861 * range spec. If an initial address is found, but no second, the upper bound
1862 * is equal to the lower.
1863 */
1864
Bram Moolenaar25190db2019-05-04 15:05:28 +02001865 // ea.addr_type for user commands is set by find_ucmd
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01001866 if (!IS_USER_CMDIDX(ea.cmdidx))
1867 {
1868 if (ea.cmdidx != CMD_SIZE)
1869 ea.addr_type = cmdnames[(int)ea.cmdidx].cmd_addr_type;
1870 else
1871 ea.addr_type = ADDR_LINES;
1872
Bram Moolenaar25190db2019-05-04 15:05:28 +02001873 // :wincmd range depends on the argument.
Bram Moolenaar164f3262015-01-14 21:22:01 +01001874 if (ea.cmdidx == CMD_wincmd && p != NULL)
1875 get_wincmd_addr_type(skipwhite(p), &ea);
Bram Moolenaar25190db2019-05-04 15:05:28 +02001876#ifdef FEAT_QUICKFIX
1877 // :.cc in quickfix window uses line number
1878 if ((ea.cmdidx == CMD_cc || ea.cmdidx == CMD_ll) && bt_quickfix(curbuf))
1879 ea.addr_type = ADDR_OTHER;
1880#endif
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01001881 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001882
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01001883 ea.cmd = cmd;
Bram Moolenaar3d48e252020-07-15 14:15:52 +02001884#ifdef FEAT_EVAL
Bram Moolenaarc2af0af2020-08-23 21:06:02 +02001885 if (!may_have_range)
1886 ea.line1 = ea.line2 = default_address(&ea);
1887 else
Bram Moolenaar3d48e252020-07-15 14:15:52 +02001888#endif
1889 if (parse_cmd_address(&ea, &errormsg, FALSE) == FAIL)
1890 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001891
Bram Moolenaar071d4272004-06-13 20:20:40 +00001892/*
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001893 * 5. Parse the command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001894 */
1895
1896 /*
1897 * Skip ':' and any white space
1898 */
1899 ea.cmd = skipwhite(ea.cmd);
1900 while (*ea.cmd == ':')
1901 ea.cmd = skipwhite(ea.cmd + 1);
1902
1903 /*
1904 * If we got a line, but no command, then go to the line.
1905 * If we find a '|' or '\n' we set ea.nextcmd.
1906 */
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001907 if (*ea.cmd == NUL || comment_start(ea.cmd, starts_with_colon)
1908 || (ea.nextcmd = check_nextcmd(ea.cmd)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001909 {
1910 /*
1911 * strange vi behaviour:
1912 * ":3" jumps to line 3
1913 * ":3|..." prints line 3
1914 * ":|" prints current line
1915 */
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001916 if (ea.skip) // skip this if inside :if
Bram Moolenaar071d4272004-06-13 20:20:40 +00001917 goto doend;
Bram Moolenaardf177f62005-02-22 08:39:57 +00001918 if (*ea.cmd == '|' || (exmode_active && ea.line1 != ea.line2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001919 {
1920 ea.cmdidx = CMD_print;
Bram Moolenaar8071cb22019-07-12 17:58:01 +02001921 ea.argt = EX_RANGE+EX_COUNT+EX_TRLBAR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001922 if ((errormsg = invalid_range(&ea)) == NULL)
1923 {
1924 correct_range(&ea);
1925 ex_print(&ea);
1926 }
1927 }
1928 else if (ea.addr_count != 0)
1929 {
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00001930 if (ea.line2 > curbuf->b_ml.ml_line_count)
1931 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001932 // With '-' in 'cpoptions' a line number past the file is an
1933 // error, otherwise put it at the end of the file.
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00001934 if (vim_strchr(p_cpo, CPO_MINUS) != NULL)
1935 ea.line2 = -1;
1936 else
1937 ea.line2 = curbuf->b_ml.ml_line_count;
1938 }
1939
1940 if (ea.line2 < 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001941 errormsg = _(e_invrange);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001942 else
1943 {
1944 if (ea.line2 == 0)
1945 curwin->w_cursor.lnum = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001946 else
1947 curwin->w_cursor.lnum = ea.line2;
1948 beginline(BL_SOL | BL_FIX);
1949 }
1950 }
1951 goto doend;
1952 }
1953
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001954 // If this looks like an undefined user command and there are CmdUndefined
1955 // autocommands defined, trigger the matching autocommands.
Bram Moolenaard5005162014-08-22 23:05:54 +02001956 if (p != NULL && ea.cmdidx == CMD_SIZE && !ea.skip
1957 && ASCII_ISUPPER(*ea.cmd)
1958 && has_cmdundefined())
1959 {
Bram Moolenaard5005162014-08-22 23:05:54 +02001960 int ret;
1961
Bram Moolenaar5a31b462014-08-23 14:16:20 +02001962 p = ea.cmd;
Bram Moolenaard5005162014-08-22 23:05:54 +02001963 while (ASCII_ISALNUM(*p))
1964 ++p;
Bram Moolenaar158ea172020-06-18 17:28:39 +02001965 p = vim_strnsave(ea.cmd, p - ea.cmd);
Bram Moolenaard5005162014-08-22 23:05:54 +02001966 ret = apply_autocmds(EVENT_CMDUNDEFINED, p, p, TRUE, NULL);
1967 vim_free(p);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001968 // If the autocommands did something and didn't cause an error, try
1969 // finding the command again.
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001970 p = (ret
1971#ifdef FEAT_EVAL
1972 && !aborting()
Bram Moolenaard5005162014-08-22 23:05:54 +02001973#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001974 ) ? find_ex_command(&ea, NULL, NULL, NULL) : ea.cmd;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001975 }
Bram Moolenaard5005162014-08-22 23:05:54 +02001976
Bram Moolenaar071d4272004-06-13 20:20:40 +00001977 if (p == NULL)
1978 {
1979 if (!ea.skip)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001980 errormsg = _("E464: Ambiguous use of user-defined command");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001981 goto doend;
1982 }
Bram Moolenaarac9fb182019-04-27 13:04:13 +02001983 // Check for wrong commands.
Bram Moolenaar6f9a4762017-06-22 20:39:17 +02001984 if (*p == '!' && ea.cmd[1] == 0151 && ea.cmd[0] == 78
1985 && !IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001986 {
1987 errormsg = uc_fun_cmd();
1988 goto doend;
1989 }
Bram Moolenaarac9fb182019-04-27 13:04:13 +02001990
Bram Moolenaar071d4272004-06-13 20:20:40 +00001991 if (ea.cmdidx == CMD_SIZE)
1992 {
1993 if (!ea.skip)
1994 {
1995 STRCPY(IObuff, _("E492: Not an editor command"));
1996 if (!sourcing)
Bram Moolenaar7b668e82016-08-23 23:51:21 +02001997 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001998 // If the modifier was parsed OK the error must be in the
1999 // following command
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002000 if (after_modifier != NULL)
2001 append_command(after_modifier);
2002 else
2003 append_command(*cmdlinep);
2004 }
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002005 errormsg = (char *)IObuff;
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02002006 did_emsg_syntax = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002007 }
2008 goto doend;
2009 }
2010
Bram Moolenaar958636c2014-10-21 20:01:58 +02002011 ni = (!IS_USER_CMDIDX(ea.cmdidx)
2012 && (cmdnames[ea.cmdidx].cmd_func == ex_ni
Bram Moolenaar7bb75552007-07-16 18:39:49 +00002013#ifdef HAVE_EX_SCRIPT_NI
2014 || cmdnames[ea.cmdidx].cmd_func == ex_script_ni
2015#endif
2016 ));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002017
2018#ifndef FEAT_EVAL
2019 /*
2020 * When the expression evaluation is disabled, recognize the ":if" and
2021 * ":endif" commands and ignore everything in between it.
2022 */
2023 if (ea.cmdidx == CMD_if)
2024 ++if_level;
2025 if (if_level)
2026 {
2027 if (ea.cmdidx == CMD_endif)
2028 --if_level;
2029 goto doend;
2030 }
2031
2032#endif
2033
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002034 // forced commands
Bram Moolenaar196b3b02008-06-20 16:51:41 +00002035 if (*p == '!' && ea.cmdidx != CMD_substitute
2036 && ea.cmdidx != CMD_smagic && ea.cmdidx != CMD_snomagic)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002037 {
2038 ++p;
2039 ea.forceit = TRUE;
2040 }
2041 else
2042 ea.forceit = FALSE;
2043
2044/*
Bram Moolenaarb7316892019-05-01 18:08:42 +02002045 * 6. Parse arguments. Then check for errors.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002046 */
Bram Moolenaar958636c2014-10-21 20:01:58 +02002047 if (!IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002048 ea.argt = (long)cmdnames[(int)ea.cmdidx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002049
2050 if (!ea.skip)
2051 {
2052#ifdef HAVE_SANDBOX
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002053 if (sandbox != 0 && !(ea.argt & EX_SBOXOK))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002054 {
Bram Moolenaar8c62a082019-02-08 14:34:10 +01002055 // Command not allowed in sandbox.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002056 errormsg = _(e_sandbox);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002057 goto doend;
2058 }
2059#endif
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002060 if (restricted != 0 && (ea.argt & EX_RESTRICT))
Bram Moolenaar8c62a082019-02-08 14:34:10 +01002061 {
2062 errormsg = _("E981: Command not allowed in rvim");
2063 goto doend;
2064 }
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002065 if (!curbuf->b_p_ma && (ea.argt & EX_MODIFY))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002066 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002067 // Command not allowed in non-'modifiable' buffer
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002068 errormsg = _(e_modifiable);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002069 goto doend;
2070 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002071
Bram Moolenaar37394ff2020-07-25 19:38:18 +02002072 if (!IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002073 {
Bram Moolenaar37394ff2020-07-25 19:38:18 +02002074#ifdef FEAT_CMDWIN
2075 if (cmdwin_type != 0 && !(ea.argt & EX_CMDWIN))
2076 {
2077 // Command not allowed in the command line window
2078 errormsg = _(e_cmdwin);
2079 goto doend;
2080 }
2081#endif
2082 if (text_locked() && !(ea.argt & EX_LOCK_OK))
2083 {
2084 // Command not allowed when text is locked
2085 errormsg = _(get_text_locked_msg());
2086 goto doend;
2087 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002088 }
Bram Moolenaar379fb762018-08-30 15:58:28 +02002089
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002090 // Disallow editing another buffer when "curbuf_lock" is set.
2091 // Do allow ":checktime" (it is postponed).
2092 // Do allow ":edit" (check for an argument later).
2093 // Do allow ":file" with no arguments (check for an argument later).
Bram Moolenaar8e1986e2020-08-06 22:11:06 +02002094 if (!(ea.argt & (EX_CMDWIN | EX_LOCK_OK))
Bram Moolenaar5555acc2006-04-07 21:33:12 +00002095 && ea.cmdidx != CMD_checktime
Bram Moolenaar379fb762018-08-30 15:58:28 +02002096 && ea.cmdidx != CMD_edit
2097 && ea.cmdidx != CMD_file
Bram Moolenaar958636c2014-10-21 20:01:58 +02002098 && !IS_USER_CMDIDX(ea.cmdidx)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002099 && curbuf_locked())
2100 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002101
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002102 if (!ni && !(ea.argt & EX_RANGE) && ea.addr_count > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002103 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002104 // no range allowed
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002105 errormsg = _(e_norange);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002106 goto doend;
2107 }
2108 }
2109
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002110 if (!ni && !(ea.argt & EX_BANG) && ea.forceit) // no <!> allowed
Bram Moolenaar071d4272004-06-13 20:20:40 +00002111 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002112 errormsg = _(e_nobang);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002113 goto doend;
2114 }
2115
2116 /*
2117 * Don't complain about the range if it is not used
2118 * (could happen if line_count is accidentally set to 0).
2119 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002120 if (!ea.skip && !ni && (ea.argt & EX_RANGE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002121 {
2122 /*
2123 * If the range is backwards, ask for confirmation and, if given, swap
2124 * ea.line1 & ea.line2 so it's forwards again.
2125 * When global command is busy, don't ask, will fail below.
2126 */
2127 if (!global_busy && ea.line1 > ea.line2)
2128 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002129 if (msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002130 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002131 if (sourcing || exmode_active)
2132 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002133 errormsg = _("E493: Backwards range given");
Bram Moolenaara5792f52005-11-23 21:25:05 +00002134 goto doend;
2135 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002136 if (ask_yesno((char_u *)
2137 _("Backwards range given, OK to swap"), FALSE) != 'y')
Bram Moolenaara5792f52005-11-23 21:25:05 +00002138 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002139 }
2140 lnum = ea.line1;
2141 ea.line1 = ea.line2;
2142 ea.line2 = lnum;
2143 }
2144 if ((errormsg = invalid_range(&ea)) != NULL)
2145 goto doend;
2146 }
2147
Bram Moolenaarb7316892019-05-01 18:08:42 +02002148 if ((ea.addr_type == ADDR_OTHER) && ea.addr_count == 0)
2149 // default is 1, not cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00002150 ea.line2 = 1;
2151
2152 correct_range(&ea);
2153
2154#ifdef FEAT_FOLDING
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002155 if (((ea.argt & EX_WHOLEFOLD) || ea.addr_count >= 2) && !global_busy
Bram Moolenaara3306952016-01-02 21:41:06 +01002156 && ea.addr_type == ADDR_LINES)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002157 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002158 // Put the first line at the start of a closed fold, put the last line
2159 // at the end of a closed fold.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002160 (void)hasFolding(ea.line1, &ea.line1, NULL);
2161 (void)hasFolding(ea.line2, NULL, &ea.line2);
2162 }
2163#endif
2164
2165#ifdef FEAT_QUICKFIX
2166 /*
Bram Moolenaar86b68352004-12-27 21:59:20 +00002167 * For the ":make" and ":grep" commands we insert the 'makeprg'/'grepprg'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002168 * option here, so things like % get expanded.
2169 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002170 p = replace_makeprg(&ea, p, cmdlinep);
2171 if (p == NULL)
2172 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002173#endif
2174
2175 /*
2176 * Skip to start of argument.
2177 * Don't do this for the ":!" command, because ":!! -l" needs the space.
2178 */
2179 if (ea.cmdidx == CMD_bang)
2180 ea.arg = p;
2181 else
2182 ea.arg = skipwhite(p);
2183
Bram Moolenaar379fb762018-08-30 15:58:28 +02002184 // ":file" cannot be run with an argument when "curbuf_lock" is set
2185 if (ea.cmdidx == CMD_file && *ea.arg != NUL && curbuf_locked())
2186 goto doend;
2187
Bram Moolenaar071d4272004-06-13 20:20:40 +00002188 /*
2189 * Check for "++opt=val" argument.
2190 * Must be first, allow ":w ++enc=utf8 !cmd"
2191 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002192 if (ea.argt & EX_ARGOPT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002193 while (ea.arg[0] == '+' && ea.arg[1] == '+')
2194 if (getargopt(&ea) == FAIL && !ni)
2195 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002196 errormsg = _(e_invarg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002197 goto doend;
2198 }
2199
2200 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
2201 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002202 if (*ea.arg == '>') // append
Bram Moolenaar071d4272004-06-13 20:20:40 +00002203 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002204 if (*++ea.arg != '>') // typed wrong
Bram Moolenaar071d4272004-06-13 20:20:40 +00002205 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002206 errormsg = _("E494: Use w or w>>");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002207 goto doend;
2208 }
2209 ea.arg = skipwhite(ea.arg + 1);
2210 ea.append = TRUE;
2211 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002212 else if (*ea.arg == '!' && ea.cmdidx == CMD_write) // :w !filter
Bram Moolenaar071d4272004-06-13 20:20:40 +00002213 {
2214 ++ea.arg;
2215 ea.usefilter = TRUE;
2216 }
2217 }
2218
2219 if (ea.cmdidx == CMD_read)
2220 {
2221 if (ea.forceit)
2222 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002223 ea.usefilter = TRUE; // :r! filter if ea.forceit
Bram Moolenaar071d4272004-06-13 20:20:40 +00002224 ea.forceit = FALSE;
2225 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002226 else if (*ea.arg == '!') // :r !filter
Bram Moolenaar071d4272004-06-13 20:20:40 +00002227 {
2228 ++ea.arg;
2229 ea.usefilter = TRUE;
2230 }
2231 }
2232
2233 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
2234 {
2235 ea.amount = 1;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002236 while (*ea.arg == *ea.cmd) // count number of '>' or '<'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002237 {
2238 ++ea.arg;
2239 ++ea.amount;
2240 }
2241 ea.arg = skipwhite(ea.arg);
2242 }
2243
2244 /*
2245 * Check for "+command" argument, before checking for next command.
2246 * Don't do this for ":read !cmd" and ":write !cmd".
2247 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002248 if ((ea.argt & EX_CMDARG) && !ea.usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002249 ea.do_ecmd_cmd = getargcmd(&ea.arg);
2250
2251 /*
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002252 * Check for '|' to separate commands and '"' or '#' to start comments.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002253 * Don't do this for ":read !cmd" and ":write !cmd".
2254 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002255 if ((ea.argt & EX_TRLBAR) && !ea.usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002256 separate_nextcmd(&ea);
2257
2258 /*
2259 * Check for <newline> to end a shell command.
Bram Moolenaardf177f62005-02-22 08:39:57 +00002260 * Also do this for ":read !cmd", ":write !cmd" and ":global".
2261 * Any others?
Bram Moolenaar071d4272004-06-13 20:20:40 +00002262 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00002263 else if (ea.cmdidx == CMD_bang
Bram Moolenaar67883b42017-07-27 22:57:00 +02002264 || ea.cmdidx == CMD_terminal
Bram Moolenaardf177f62005-02-22 08:39:57 +00002265 || ea.cmdidx == CMD_global
2266 || ea.cmdidx == CMD_vglobal
2267 || ea.usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002268 {
2269 for (p = ea.arg; *p; ++p)
2270 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002271 // Remove one backslash before a newline, so that it's possible to
2272 // pass a newline to the shell and also a newline that is preceded
2273 // with a backslash. This makes it impossible to end a shell
2274 // command in a backslash, but that doesn't appear useful.
2275 // Halving the number of backslashes is incompatible with previous
2276 // versions.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002277 if (*p == '\\' && p[1] == '\n')
Bram Moolenaara7241f52008-06-24 20:39:31 +00002278 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002279 else if (*p == '\n')
2280 {
2281 ea.nextcmd = p + 1;
2282 *p = NUL;
2283 break;
2284 }
2285 }
2286 }
2287
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002288 if ((ea.argt & EX_DFLALL) && ea.addr_count == 0)
Bram Moolenaarc2af0af2020-08-23 21:06:02 +02002289 address_default_all(&ea);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002290
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002291 // accept numbered register only when no count allowed (:put)
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002292 if ( (ea.argt & EX_REGSTR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002293 && *ea.arg != NUL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002294 // Do not allow register = for user commands
Bram Moolenaar958636c2014-10-21 20:01:58 +02002295 && (!IS_USER_CMDIDX(ea.cmdidx) || *ea.arg != '=')
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002296 && !((ea.argt & EX_COUNT) && VIM_ISDIGIT(*ea.arg)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002297 {
Bram Moolenaar85de2062011-05-05 14:26:41 +02002298#ifndef FEAT_CLIPBOARD
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002299 // check these explicitly for a more specific error message
Bram Moolenaar85de2062011-05-05 14:26:41 +02002300 if (*ea.arg == '*' || *ea.arg == '+')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002301 {
Bram Moolenaar99b12722019-01-14 20:16:40 +01002302 errormsg = _(e_invalidreg);
Bram Moolenaar85de2062011-05-05 14:26:41 +02002303 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002304 }
2305#endif
Bram Moolenaar958636c2014-10-21 20:01:58 +02002306 if (valid_yank_reg(*ea.arg, (ea.cmdidx != CMD_put
2307 && !IS_USER_CMDIDX(ea.cmdidx))))
Bram Moolenaar85de2062011-05-05 14:26:41 +02002308 {
2309 ea.regname = *ea.arg++;
2310#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002311 // for '=' register: accept the rest of the line as an expression
Bram Moolenaar85de2062011-05-05 14:26:41 +02002312 if (ea.arg[-1] == '=' && ea.arg[0] != NUL)
2313 {
2314 set_expr_line(vim_strsave(ea.arg));
2315 ea.arg += STRLEN(ea.arg);
2316 }
2317#endif
2318 ea.arg = skipwhite(ea.arg);
2319 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002320 }
2321
2322 /*
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002323 * Check for a count. When accepting a EX_BUFNAME, don't use "123foo" as a
Bram Moolenaar071d4272004-06-13 20:20:40 +00002324 * count, it's a buffer name.
2325 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002326 if ((ea.argt & EX_COUNT) && VIM_ISDIGIT(*ea.arg)
2327 && (!(ea.argt & EX_BUFNAME) || *(p = skipdigits(ea.arg)) == NUL
Bram Moolenaar1c465442017-03-12 20:10:05 +01002328 || VIM_ISWHITE(*p)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002329 {
2330 n = getdigits(&ea.arg);
2331 ea.arg = skipwhite(ea.arg);
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002332 if (n <= 0 && !ni && (ea.argt & EX_ZEROR) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002333 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002334 errormsg = _(e_zerocount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002335 goto doend;
2336 }
Bram Moolenaarb7316892019-05-01 18:08:42 +02002337 if (ea.addr_type != ADDR_LINES) // e.g. :buffer 2, :sleep 3
Bram Moolenaar071d4272004-06-13 20:20:40 +00002338 {
2339 ea.line2 = n;
2340 if (ea.addr_count == 0)
2341 ea.addr_count = 1;
2342 }
2343 else
2344 {
2345 ea.line1 = ea.line2;
2346 ea.line2 += n - 1;
2347 ++ea.addr_count;
2348 /*
2349 * Be vi compatible: no error message for out of range.
2350 */
Bram Moolenaarb7316892019-05-01 18:08:42 +02002351 if (ea.line2 > curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002352 ea.line2 = curbuf->b_ml.ml_line_count;
2353 }
2354 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00002355
2356 /*
2357 * Check for flags: 'l', 'p' and '#'.
2358 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002359 if (ea.argt & EX_FLAGS)
Bram Moolenaardf177f62005-02-22 08:39:57 +00002360 get_flags(&ea);
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002361 if (!ni && !(ea.argt & EX_EXTRA) && *ea.arg != NUL
2362 && *ea.arg != '"' && (*ea.arg != '|' || (ea.argt & EX_TRLBAR) == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002363 {
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002364 // no arguments allowed but there is something
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +02002365 errormsg = ex_errmsg(e_trailing_arg, ea.arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002366 goto doend;
2367 }
2368
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002369 if (!ni && (ea.argt & EX_NEEDARG) && *ea.arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002370 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002371 errormsg = _(e_argreq);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002372 goto doend;
2373 }
2374
2375#ifdef FEAT_EVAL
2376 /*
2377 * Skip the command when it's not going to be executed.
2378 * The commands like :if, :endif, etc. always need to be executed.
2379 * Also make an exception for commands that handle a trailing command
2380 * themselves.
2381 */
2382 if (ea.skip)
2383 {
2384 switch (ea.cmdidx)
2385 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002386 // commands that need evaluation
Bram Moolenaar071d4272004-06-13 20:20:40 +00002387 case CMD_while:
2388 case CMD_endwhile:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00002389 case CMD_for:
2390 case CMD_endfor:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002391 case CMD_if:
2392 case CMD_elseif:
2393 case CMD_else:
2394 case CMD_endif:
2395 case CMD_try:
2396 case CMD_catch:
2397 case CMD_finally:
2398 case CMD_endtry:
2399 case CMD_function:
Bram Moolenaarab55c682020-03-01 19:41:43 +01002400 case CMD_def:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002401 break;
2402
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002403 // Commands that handle '|' themselves. Check: A command should
2404 // either have the EX_TRLBAR flag, appear in this list or appear in
2405 // the list at ":help :bar".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002406 case CMD_aboveleft:
2407 case CMD_and:
2408 case CMD_belowright:
2409 case CMD_botright:
2410 case CMD_browse:
2411 case CMD_call:
2412 case CMD_confirm:
Bram Moolenaar8f76e6b2019-11-26 16:50:30 +01002413 case CMD_const:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002414 case CMD_delfunction:
2415 case CMD_djump:
2416 case CMD_dlist:
2417 case CMD_dsearch:
2418 case CMD_dsplit:
2419 case CMD_echo:
2420 case CMD_echoerr:
2421 case CMD_echomsg:
2422 case CMD_echon:
Bram Moolenaara76b3152020-02-16 16:20:21 +01002423 case CMD_eval:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002424 case CMD_execute:
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002425 case CMD_filter:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002426 case CMD_help:
2427 case CMD_hide:
2428 case CMD_ijump:
2429 case CMD_ilist:
2430 case CMD_isearch:
2431 case CMD_isplit:
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002432 case CMD_keepalt:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002433 case CMD_keepjumps:
2434 case CMD_keepmarks:
Bram Moolenaara939e432013-11-09 05:30:26 +01002435 case CMD_keeppatterns:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002436 case CMD_leftabove:
2437 case CMD_let:
2438 case CMD_lockmarks:
Bram Moolenaarcc4423a2019-11-26 17:05:00 +01002439 case CMD_lockvar:
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002440 case CMD_lua:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002441 case CMD_match:
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002442 case CMD_mzscheme:
Bram Moolenaar5803ae62014-03-23 16:04:02 +01002443 case CMD_noautocmd:
2444 case CMD_noswapfile:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002445 case CMD_perl:
2446 case CMD_psearch:
2447 case CMD_python:
Bram Moolenaar368373e2010-07-19 20:46:22 +02002448 case CMD_py3:
Bram Moolenaarb6590522010-07-21 16:00:43 +02002449 case CMD_python3:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002450 case CMD_return:
2451 case CMD_rightbelow:
2452 case CMD_ruby:
2453 case CMD_silent:
2454 case CMD_smagic:
2455 case CMD_snomagic:
2456 case CMD_substitute:
2457 case CMD_syntax:
Bram Moolenaara226a6d2006-02-26 23:59:20 +00002458 case CMD_tab:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002459 case CMD_tcl:
2460 case CMD_throw:
2461 case CMD_tilde:
2462 case CMD_topleft:
2463 case CMD_unlet:
Bram Moolenaarcc4423a2019-11-26 17:05:00 +01002464 case CMD_unlockvar:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002465 case CMD_verbose:
2466 case CMD_vertical:
Bram Moolenaar12bde492011-06-13 01:19:56 +02002467 case CMD_wincmd:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002468 break;
2469
2470 default: goto doend;
2471 }
2472 }
2473#endif
2474
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002475 if (ea.argt & EX_XFILE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002476 {
2477 if (expand_filename(&ea, cmdlinep, &errormsg) == FAIL)
2478 goto doend;
2479 }
2480
Bram Moolenaar071d4272004-06-13 20:20:40 +00002481 /*
2482 * Accept buffer name. Cannot be used at the same time with a buffer
2483 * number. Don't do this for a user command.
2484 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002485 if ((ea.argt & EX_BUFNAME) && *ea.arg != NUL && ea.addr_count == 0
Bram Moolenaar958636c2014-10-21 20:01:58 +02002486 && !IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002487 {
2488 /*
2489 * :bdelete, :bwipeout and :bunload take several arguments, separated
2490 * by spaces: find next space (skipping over escaped characters).
2491 * The others take one argument: ignore trailing spaces.
2492 */
2493 if (ea.cmdidx == CMD_bdelete || ea.cmdidx == CMD_bwipeout
2494 || ea.cmdidx == CMD_bunload)
2495 p = skiptowhite_esc(ea.arg);
2496 else
2497 {
2498 p = ea.arg + STRLEN(ea.arg);
Bram Moolenaar1c465442017-03-12 20:10:05 +01002499 while (p > ea.arg && VIM_ISWHITE(p[-1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002500 --p;
2501 }
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002502 ea.line2 = buflist_findpat(ea.arg, p, (ea.argt & EX_BUFUNL) != 0,
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002503 FALSE, FALSE);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002504 if (ea.line2 < 0) // failed
Bram Moolenaar071d4272004-06-13 20:20:40 +00002505 goto doend;
2506 ea.addr_count = 1;
2507 ea.arg = skipwhite(p);
2508 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002509
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002510 // The :try command saves the emsg_silent flag, reset it here when
2511 // ":silent! try" was used, it should only apply to :try itself.
Bram Moolenaareffed932018-08-14 13:38:17 +02002512 if (ea.cmdidx == CMD_try && ea.did_esilent > 0)
Bram Moolenaar2be57332018-02-13 18:05:18 +01002513 {
Bram Moolenaareffed932018-08-14 13:38:17 +02002514 emsg_silent -= ea.did_esilent;
Bram Moolenaar2be57332018-02-13 18:05:18 +01002515 if (emsg_silent < 0)
2516 emsg_silent = 0;
Bram Moolenaareffed932018-08-14 13:38:17 +02002517 ea.did_esilent = 0;
Bram Moolenaar2be57332018-02-13 18:05:18 +01002518 }
2519
Bram Moolenaar071d4272004-06-13 20:20:40 +00002520/*
Bram Moolenaar2be57332018-02-13 18:05:18 +01002521 * 7. Execute the command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002522 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002523
Bram Moolenaar958636c2014-10-21 20:01:58 +02002524 if (IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002525 {
2526 /*
2527 * Execute a user-defined command.
2528 */
2529 do_ucmd(&ea);
2530 }
2531 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002532 {
2533 /*
Bram Moolenaarac9fb182019-04-27 13:04:13 +02002534 * Call the function to execute the builtin command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002535 */
2536 ea.errmsg = NULL;
2537 (cmdnames[ea.cmdidx].cmd_func)(&ea);
2538 if (ea.errmsg != NULL)
Bram Moolenaar8930caa2020-07-23 16:37:03 +02002539 errormsg = ea.errmsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002540 }
2541
2542#ifdef FEAT_EVAL
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002543 // Set flag that any command was executed, used by ex_vim9script().
Bram Moolenaar16531552020-02-08 16:00:46 +01002544 if (getline_equal(ea.getline, ea.cookie, getsourceline)
2545 && current_sctx.sc_sid > 0)
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002546 SCRIPT_ITEM(current_sctx.sc_sid)->sn_had_command = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002547
Bram Moolenaar071d4272004-06-13 20:20:40 +00002548 /*
2549 * If the command just executed called do_cmdline(), any throw or ":return"
2550 * or ":finish" encountered there must also check the cstack of the still
2551 * active do_cmdline() that called this do_one_cmd(). Rethrow an uncaught
2552 * exception, or reanimate a returned function or finished script file and
2553 * return or finish it again.
2554 */
2555 if (need_rethrow)
2556 do_throw(cstack);
2557 else if (check_cstack)
2558 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002559 if (source_finished(fgetline, cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002560 do_finish(&ea, TRUE);
Bram Moolenaar89d40322006-08-29 15:30:07 +00002561 else if (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002562 && current_func_returned())
2563 do_return(&ea, TRUE, FALSE, NULL);
2564 }
2565 need_rethrow = check_cstack = FALSE;
2566#endif
2567
2568doend:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002569 if (curwin->w_cursor.lnum == 0) // can happen with zero line number
Bram Moolenaar6de5e122017-04-20 21:55:44 +02002570 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002571 curwin->w_cursor.lnum = 1;
Bram Moolenaar6de5e122017-04-20 21:55:44 +02002572 curwin->w_cursor.col = 0;
2573 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002574
2575 if (errormsg != NULL && *errormsg != NUL && !did_emsg)
2576 {
2577 if (sourcing)
2578 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002579 if (errormsg != (char *)IObuff)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002580 {
2581 STRCPY(IObuff, errormsg);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002582 errormsg = (char *)IObuff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002583 }
Bram Moolenaara6f4d612011-09-21 19:10:46 +02002584 append_command(*cmdlinep);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002585 }
2586 emsg(errormsg);
2587 }
2588#ifdef FEAT_EVAL
2589 do_errthrow(cstack,
Bram Moolenaar958636c2014-10-21 20:01:58 +02002590 (ea.cmdidx != CMD_SIZE && !IS_USER_CMDIDX(ea.cmdidx))
2591 ? cmdnames[(int)ea.cmdidx].cmd_name : (char_u *)NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002592#endif
2593
Bram Moolenaar47e7d702020-07-05 18:18:42 +02002594 undo_cmdmod(&ea, save_msg_scroll);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002595 cmdmod = save_cmdmod;
Bram Moolenaar9a2c0912019-03-30 14:26:18 +01002596 reg_executing = save_reg_executing;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002597
Bram Moolenaar7171abe2004-10-11 10:06:20 +00002598#ifdef HAVE_SANDBOX
Bram Moolenaareffed932018-08-14 13:38:17 +02002599 if (ea.did_sandbox)
Bram Moolenaar7171abe2004-10-11 10:06:20 +00002600 --sandbox;
2601#endif
2602
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002603 if (ea.nextcmd && *ea.nextcmd == NUL) // not really a next command
Bram Moolenaar071d4272004-06-13 20:20:40 +00002604 ea.nextcmd = NULL;
2605
2606#ifdef FEAT_EVAL
2607 --ex_nesting_level;
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002608 vim_free(ea.cmdline_tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002609#endif
2610
2611 return ea.nextcmd;
2612}
2613#if (_MSC_VER == 1200)
Bram Moolenaar281bdce2005-01-25 21:53:18 +00002614 #pragma optimize( "", on )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002615#endif
2616
Bram Moolenaar8930caa2020-07-23 16:37:03 +02002617static char ex_error_buf[MSG_BUF_LEN];
2618
2619/*
2620 * Return an error message with argument included.
2621 * Uses a static buffer, only the last error will be kept.
2622 * "msg" will be translated, caller should use N_().
2623 */
2624 char *
2625ex_errmsg(char *msg, char_u *arg)
2626{
2627 vim_snprintf(ex_error_buf, MSG_BUF_LEN, _(msg), arg);
2628 return ex_error_buf;
2629}
2630
Bram Moolenaar071d4272004-06-13 20:20:40 +00002631/*
Bram Moolenaareffed932018-08-14 13:38:17 +02002632 * Parse and skip over command modifiers:
2633 * - update eap->cmd
2634 * - store flags in "cmdmod".
2635 * - Set ex_pressedreturn for an empty command line.
2636 * - set msg_silent for ":silent"
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002637 * - set 'eventignore' to "all" for ":noautocmd"
Bram Moolenaareffed932018-08-14 13:38:17 +02002638 * - set p_verbose for ":verbose"
2639 * - Increment "sandbox" for ":sandbox"
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002640 * When "skip_only" is TRUE the global variables are not changed, except for
2641 * "cmdmod".
Bram Moolenaareffed932018-08-14 13:38:17 +02002642 * Return FAIL when the command is not to be executed.
2643 * May set "errormsg" to an error message.
2644 */
2645 int
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002646parse_command_modifiers(exarg_T *eap, char **errormsg, int skip_only)
Bram Moolenaareffed932018-08-14 13:38:17 +02002647{
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002648 char_u *p;
2649 int starts_with_colon = FALSE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002650
Bram Moolenaara80faa82020-04-12 19:37:17 +02002651 CLEAR_FIELD(cmdmod);
Bram Moolenaareffed932018-08-14 13:38:17 +02002652 eap->verbose_save = -1;
2653 eap->save_msg_silent = -1;
2654
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002655 // Repeat until no more command modifiers are found.
Bram Moolenaareffed932018-08-14 13:38:17 +02002656 for (;;)
2657 {
Bram Moolenaareffed932018-08-14 13:38:17 +02002658 while (*eap->cmd == ' ' || *eap->cmd == '\t' || *eap->cmd == ':')
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002659 {
2660 if (*eap->cmd == ':')
2661 starts_with_colon = TRUE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002662 ++eap->cmd;
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002663 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002664
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002665 // in ex mode, an empty line works like :+
Bram Moolenaareffed932018-08-14 13:38:17 +02002666 if (*eap->cmd == NUL && exmode_active
2667 && (getline_equal(eap->getline, eap->cookie, getexmodeline)
2668 || getline_equal(eap->getline, eap->cookie, getexline))
2669 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
2670 {
2671 eap->cmd = (char_u *)"+";
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002672 if (!skip_only)
2673 ex_pressedreturn = TRUE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002674 }
2675
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002676 // ignore comment and empty lines
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002677 if (comment_start(eap->cmd, starts_with_colon))
Bram Moolenaareffed932018-08-14 13:38:17 +02002678 return FAIL;
2679 if (*eap->cmd == NUL)
2680 {
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002681 if (!skip_only)
2682 ex_pressedreturn = TRUE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002683 return FAIL;
2684 }
2685
Bram Moolenaareffed932018-08-14 13:38:17 +02002686 p = skip_range(eap->cmd, NULL);
2687 switch (*p)
2688 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002689 // When adding an entry, also modify cmd_exists().
Bram Moolenaareffed932018-08-14 13:38:17 +02002690 case 'a': if (!checkforcmd(&eap->cmd, "aboveleft", 3))
2691 break;
2692 cmdmod.split |= WSP_ABOVE;
2693 continue;
2694
2695 case 'b': if (checkforcmd(&eap->cmd, "belowright", 3))
2696 {
2697 cmdmod.split |= WSP_BELOW;
2698 continue;
2699 }
2700 if (checkforcmd(&eap->cmd, "browse", 3))
2701 {
2702#ifdef FEAT_BROWSE_CMD
2703 cmdmod.browse = TRUE;
2704#endif
2705 continue;
2706 }
2707 if (!checkforcmd(&eap->cmd, "botright", 2))
2708 break;
2709 cmdmod.split |= WSP_BOT;
2710 continue;
2711
2712 case 'c': if (!checkforcmd(&eap->cmd, "confirm", 4))
2713 break;
2714#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
2715 cmdmod.confirm = TRUE;
2716#endif
2717 continue;
2718
2719 case 'k': if (checkforcmd(&eap->cmd, "keepmarks", 3))
2720 {
2721 cmdmod.keepmarks = TRUE;
2722 continue;
2723 }
2724 if (checkforcmd(&eap->cmd, "keepalt", 5))
2725 {
2726 cmdmod.keepalt = TRUE;
2727 continue;
2728 }
2729 if (checkforcmd(&eap->cmd, "keeppatterns", 5))
2730 {
2731 cmdmod.keeppatterns = TRUE;
2732 continue;
2733 }
2734 if (!checkforcmd(&eap->cmd, "keepjumps", 5))
2735 break;
2736 cmdmod.keepjumps = TRUE;
2737 continue;
2738
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002739 case 'f': // only accept ":filter {pat} cmd"
Bram Moolenaareffed932018-08-14 13:38:17 +02002740 {
2741 char_u *reg_pat;
2742
2743 if (!checkforcmd(&p, "filter", 4)
2744 || *p == NUL || ends_excmd(*p))
2745 break;
2746 if (*p == '!')
2747 {
2748 cmdmod.filter_force = TRUE;
2749 p = skipwhite(p + 1);
2750 if (*p == NUL || ends_excmd(*p))
2751 break;
2752 }
Bram Moolenaar1e624c92020-07-11 14:08:04 +02002753#ifdef FEAT_EVAL
Bram Moolenaarb074e8b2020-07-11 13:40:45 +02002754 // Avoid that "filter(arg)" is recognized.
2755 if (in_vim9script() && !VIM_ISWHITE(*p))
2756 break;
Bram Moolenaar1e624c92020-07-11 14:08:04 +02002757#endif
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002758 if (skip_only)
2759 p = skip_vimgrep_pat(p, NULL, NULL);
2760 else
2761 // NOTE: This puts a NUL after the pattern.
2762 p = skip_vimgrep_pat(p, &reg_pat, NULL);
Bram Moolenaareffed932018-08-14 13:38:17 +02002763 if (p == NULL || *p == NUL)
2764 break;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002765 if (!skip_only)
2766 {
2767 cmdmod.filter_regmatch.regprog =
Bram Moolenaareffed932018-08-14 13:38:17 +02002768 vim_regcomp(reg_pat, RE_MAGIC);
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002769 if (cmdmod.filter_regmatch.regprog == NULL)
2770 break;
2771 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002772 eap->cmd = p;
2773 continue;
2774 }
2775
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002776 // ":hide" and ":hide | cmd" are not modifiers
Bram Moolenaareffed932018-08-14 13:38:17 +02002777 case 'h': if (p != eap->cmd || !checkforcmd(&p, "hide", 3)
2778 || *p == NUL || ends_excmd(*p))
2779 break;
2780 eap->cmd = p;
2781 cmdmod.hide = TRUE;
2782 continue;
2783
2784 case 'l': if (checkforcmd(&eap->cmd, "lockmarks", 3))
2785 {
2786 cmdmod.lockmarks = TRUE;
2787 continue;
2788 }
2789
2790 if (!checkforcmd(&eap->cmd, "leftabove", 5))
2791 break;
2792 cmdmod.split |= WSP_ABOVE;
2793 continue;
2794
2795 case 'n': if (checkforcmd(&eap->cmd, "noautocmd", 3))
2796 {
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002797 if (cmdmod.save_ei == NULL && !skip_only)
Bram Moolenaareffed932018-08-14 13:38:17 +02002798 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002799 // Set 'eventignore' to "all". Restore the
2800 // existing option value later.
Bram Moolenaareffed932018-08-14 13:38:17 +02002801 cmdmod.save_ei = vim_strsave(p_ei);
2802 set_string_option_direct((char_u *)"ei", -1,
2803 (char_u *)"all", OPT_FREE, SID_NONE);
2804 }
2805 continue;
2806 }
2807 if (!checkforcmd(&eap->cmd, "noswapfile", 3))
2808 break;
2809 cmdmod.noswapfile = TRUE;
2810 continue;
2811
2812 case 'r': if (!checkforcmd(&eap->cmd, "rightbelow", 6))
2813 break;
2814 cmdmod.split |= WSP_BELOW;
2815 continue;
2816
2817 case 's': if (checkforcmd(&eap->cmd, "sandbox", 3))
2818 {
2819#ifdef HAVE_SANDBOX
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002820 if (!skip_only)
2821 {
2822 if (!eap->did_sandbox)
2823 ++sandbox;
2824 eap->did_sandbox = TRUE;
2825 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002826#endif
2827 continue;
2828 }
2829 if (!checkforcmd(&eap->cmd, "silent", 3))
2830 break;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002831 if (!skip_only)
2832 {
2833 if (eap->save_msg_silent == -1)
2834 eap->save_msg_silent = msg_silent;
2835 ++msg_silent;
2836 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002837 if (*eap->cmd == '!' && !VIM_ISWHITE(eap->cmd[-1]))
2838 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002839 // ":silent!", but not "silent !cmd"
Bram Moolenaareffed932018-08-14 13:38:17 +02002840 eap->cmd = skipwhite(eap->cmd + 1);
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002841 if (!skip_only)
2842 {
2843 ++emsg_silent;
2844 ++eap->did_esilent;
2845 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002846 }
2847 continue;
2848
2849 case 't': if (checkforcmd(&p, "tab", 3))
2850 {
Bram Moolenaar548e5982018-12-26 21:45:00 +01002851 if (!skip_only)
Bram Moolenaareffed932018-08-14 13:38:17 +02002852 {
Bram Moolenaar548e5982018-12-26 21:45:00 +01002853 long tabnr = get_address(eap, &eap->cmd,
2854 ADDR_TABS, eap->skip,
2855 skip_only, FALSE, 1);
2856 if (tabnr == MAXLNUM)
2857 cmdmod.tab = tabpage_index(curtab) + 1;
2858 else
Bram Moolenaareffed932018-08-14 13:38:17 +02002859 {
Bram Moolenaar548e5982018-12-26 21:45:00 +01002860 if (tabnr < 0 || tabnr > LAST_TAB_NR)
2861 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002862 *errormsg = _(e_invrange);
Bram Moolenaar548e5982018-12-26 21:45:00 +01002863 return FAIL;
2864 }
2865 cmdmod.tab = tabnr + 1;
Bram Moolenaareffed932018-08-14 13:38:17 +02002866 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002867 }
2868 eap->cmd = p;
2869 continue;
2870 }
2871 if (!checkforcmd(&eap->cmd, "topleft", 2))
2872 break;
2873 cmdmod.split |= WSP_TOP;
2874 continue;
2875
2876 case 'u': if (!checkforcmd(&eap->cmd, "unsilent", 3))
2877 break;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002878 if (!skip_only)
2879 {
2880 if (eap->save_msg_silent == -1)
2881 eap->save_msg_silent = msg_silent;
2882 msg_silent = 0;
2883 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002884 continue;
2885
2886 case 'v': if (checkforcmd(&eap->cmd, "vertical", 4))
2887 {
2888 cmdmod.split |= WSP_VERT;
2889 continue;
2890 }
2891 if (!checkforcmd(&p, "verbose", 4))
2892 break;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002893 if (!skip_only)
2894 {
2895 if (eap->verbose_save < 0)
2896 eap->verbose_save = p_verbose;
2897 if (vim_isdigit(*eap->cmd))
2898 p_verbose = atoi((char *)eap->cmd);
2899 else
2900 p_verbose = 1;
2901 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002902 eap->cmd = p;
2903 continue;
2904 }
2905 break;
2906 }
2907
2908 return OK;
2909}
2910
2911/*
Bram Moolenaarb074e8b2020-07-11 13:40:45 +02002912 * Undo and free contents of "cmdmod".
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002913 */
Bram Moolenaar47e7d702020-07-05 18:18:42 +02002914 void
2915undo_cmdmod(exarg_T *eap, int save_msg_scroll)
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002916{
Bram Moolenaar47e7d702020-07-05 18:18:42 +02002917 if (eap->verbose_save >= 0)
2918 p_verbose = eap->verbose_save;
2919
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002920 if (cmdmod.save_ei != NULL)
2921 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002922 // Restore 'eventignore' to the value before ":noautocmd".
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002923 set_string_option_direct((char_u *)"ei", -1, cmdmod.save_ei,
2924 OPT_FREE, SID_NONE);
2925 free_string_option(cmdmod.save_ei);
2926 }
2927
2928 if (cmdmod.filter_regmatch.regprog != NULL)
2929 vim_regfree(cmdmod.filter_regmatch.regprog);
Bram Moolenaar47e7d702020-07-05 18:18:42 +02002930
2931 if (eap->save_msg_silent != -1)
2932 {
2933 // messages could be enabled for a serious error, need to check if the
2934 // counters don't become negative
2935 if (!did_emsg || msg_silent > eap->save_msg_silent)
2936 msg_silent = eap->save_msg_silent;
2937 emsg_silent -= eap->did_esilent;
2938 if (emsg_silent < 0)
2939 emsg_silent = 0;
2940 // Restore msg_scroll, it's set by file I/O commands, even when no
2941 // message is actually displayed.
2942 msg_scroll = save_msg_scroll;
2943
2944 // "silent reg" or "silent echo x" inside "redir" leaves msg_col
2945 // somewhere in the line. Put it back in the first column.
2946 if (redirecting())
2947 msg_col = 0;
2948 }
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002949}
2950
2951/*
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002952 * Parse the address range, if any, in "eap".
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02002953 * May set the last search pattern, unless "silent" is TRUE.
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002954 * Return FAIL and set "errormsg" or return OK.
2955 */
2956 int
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002957parse_cmd_address(exarg_T *eap, char **errormsg, int silent)
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002958{
2959 int address_count = 1;
2960 linenr_T lnum;
2961
2962 // Repeat for all ',' or ';' separated addresses.
2963 for (;;)
2964 {
2965 eap->line1 = eap->line2;
Bram Moolenaarc2af0af2020-08-23 21:06:02 +02002966 eap->line2 = default_address(eap);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002967 eap->cmd = skipwhite(eap->cmd);
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02002968 lnum = get_address(eap, &eap->cmd, eap->addr_type, eap->skip, silent,
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002969 eap->addr_count == 0, address_count++);
2970 if (eap->cmd == NULL) // error detected
2971 return FAIL;
2972 if (lnum == MAXLNUM)
2973 {
2974 if (*eap->cmd == '%') // '%' - all lines
2975 {
2976 ++eap->cmd;
2977 switch (eap->addr_type)
2978 {
2979 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02002980 case ADDR_OTHER:
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002981 eap->line1 = 1;
2982 eap->line2 = curbuf->b_ml.ml_line_count;
2983 break;
2984 case ADDR_LOADED_BUFFERS:
2985 {
2986 buf_T *buf = firstbuf;
2987
2988 while (buf->b_next != NULL
2989 && buf->b_ml.ml_mfp == NULL)
2990 buf = buf->b_next;
2991 eap->line1 = buf->b_fnum;
2992 buf = lastbuf;
2993 while (buf->b_prev != NULL
2994 && buf->b_ml.ml_mfp == NULL)
2995 buf = buf->b_prev;
2996 eap->line2 = buf->b_fnum;
2997 break;
2998 }
2999 case ADDR_BUFFERS:
3000 eap->line1 = firstbuf->b_fnum;
3001 eap->line2 = lastbuf->b_fnum;
3002 break;
3003 case ADDR_WINDOWS:
3004 case ADDR_TABS:
3005 if (IS_USER_CMDIDX(eap->cmdidx))
3006 {
3007 eap->line1 = 1;
3008 eap->line2 = eap->addr_type == ADDR_WINDOWS
3009 ? LAST_WIN_NR : LAST_TAB_NR;
3010 }
3011 else
3012 {
3013 // there is no Vim command which uses '%' and
3014 // ADDR_WINDOWS or ADDR_TABS
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003015 *errormsg = _(e_invrange);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003016 return FAIL;
3017 }
3018 break;
3019 case ADDR_TABS_RELATIVE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02003020 case ADDR_UNSIGNED:
3021 case ADDR_QUICKFIX:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003022 *errormsg = _(e_invrange);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003023 return FAIL;
3024 case ADDR_ARGUMENTS:
3025 if (ARGCOUNT == 0)
3026 eap->line1 = eap->line2 = 0;
3027 else
3028 {
3029 eap->line1 = 1;
3030 eap->line2 = ARGCOUNT;
3031 }
3032 break;
Bram Moolenaar25190db2019-05-04 15:05:28 +02003033 case ADDR_QUICKFIX_VALID:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003034#ifdef FEAT_QUICKFIX
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003035 eap->line1 = 1;
Bram Moolenaar25190db2019-05-04 15:05:28 +02003036 eap->line2 = qf_get_valid_size(eap);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003037 if (eap->line2 == 0)
3038 eap->line2 = 1;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003039#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003040 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02003041 case ADDR_NONE:
3042 // Will give an error later if a range is found.
3043 break;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003044 }
3045 ++eap->addr_count;
3046 }
3047 else if (*eap->cmd == '*' && vim_strchr(p_cpo, CPO_STAR) == NULL)
3048 {
3049 pos_T *fp;
3050
3051 // '*' - visual area
3052 if (eap->addr_type != ADDR_LINES)
3053 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003054 *errormsg = _(e_invrange);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003055 return FAIL;
3056 }
3057
3058 ++eap->cmd;
3059 if (!eap->skip)
3060 {
3061 fp = getmark('<', FALSE);
3062 if (check_mark(fp) == FAIL)
3063 return FAIL;
3064 eap->line1 = fp->lnum;
3065 fp = getmark('>', FALSE);
3066 if (check_mark(fp) == FAIL)
3067 return FAIL;
3068 eap->line2 = fp->lnum;
3069 ++eap->addr_count;
3070 }
3071 }
3072 }
3073 else
3074 eap->line2 = lnum;
3075 eap->addr_count++;
3076
3077 if (*eap->cmd == ';')
3078 {
3079 if (!eap->skip)
3080 {
3081 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaar0e717042020-04-27 19:29:01 +02003082 // Don't leave the cursor on an illegal line or column, but do
3083 // accept zero as address, so 0;/PATTERN/ works correctly.
3084 if (eap->line2 > 0)
3085 check_cursor();
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003086 }
3087 }
3088 else if (*eap->cmd != ',')
3089 break;
3090 ++eap->cmd;
3091 }
3092
3093 // One address given: set start and end lines.
3094 if (eap->addr_count == 1)
3095 {
3096 eap->line1 = eap->line2;
3097 // ... but only implicit: really no address given
3098 if (lnum == MAXLNUM)
3099 eap->addr_count = 0;
3100 }
3101 return OK;
3102}
3103
3104/*
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00003105 * Check for an Ex command with optional tail.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003106 * If there is a match advance "pp" to the argument and return TRUE.
3107 */
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00003108 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003109checkforcmd(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003110 char_u **pp, // start of command
3111 char *cmd, // name of command
3112 int len) // required length
Bram Moolenaar071d4272004-06-13 20:20:40 +00003113{
3114 int i;
3115
3116 for (i = 0; cmd[i] != NUL; ++i)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003117 if (((char_u *)cmd)[i] != (*pp)[i])
Bram Moolenaar071d4272004-06-13 20:20:40 +00003118 break;
3119 if (i >= len && !isalpha((*pp)[i]))
3120 {
3121 *pp = skipwhite(*pp + i);
3122 return TRUE;
3123 }
3124 return FALSE;
3125}
3126
3127/*
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003128 * Append "cmd" to the error message in IObuff.
3129 * Takes care of limiting the length and handling 0xa0, which would be
3130 * invisible otherwise.
3131 */
3132 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003133append_command(char_u *cmd)
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003134{
3135 char_u *s = cmd;
3136 char_u *d;
3137
3138 STRCAT(IObuff, ": ");
3139 d = IObuff + STRLEN(IObuff);
3140 while (*s != NUL && d - IObuff < IOSIZE - 7)
3141 {
Bram Moolenaar13505972019-01-24 15:04:48 +01003142 if (enc_utf8 ? (s[0] == 0xc2 && s[1] == 0xa0) : *s == 0xa0)
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003143 {
Bram Moolenaar13505972019-01-24 15:04:48 +01003144 s += enc_utf8 ? 2 : 1;
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003145 STRCPY(d, "<a0>");
3146 d += 4;
3147 }
3148 else
3149 MB_COPY_CHAR(s, d);
3150 }
3151 *d = NUL;
3152}
3153
3154/*
Bram Moolenaar2e800952020-08-23 19:34:48 +02003155 * If "start" points "&opt", "&l:opt", "&g:opt" or "$ENV" return a pointer to
3156 * the name. Otherwise just return "start".
3157 */
3158 char_u *
3159skip_option_env_lead(char_u *start)
3160{
3161 char_u *name = start;
3162
3163 if (*start == '&')
3164 {
3165 if ((start[1] == 'l' || start[1] == 'g') && start[2] == ':')
3166 name += 3;
3167 else
3168 name += 1;
3169 }
3170 else if (*start == '$')
3171 name += 1;
3172 return name;
3173}
3174
3175/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003176 * Find an Ex command by its name, either built-in or user.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003177 * Start of the name can be found at eap->cmd.
Bram Moolenaar25190db2019-05-04 15:05:28 +02003178 * Sets eap->cmdidx and returns a pointer to char after the command name.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003179 * "full" is set to TRUE if the whole command name matched.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003180 *
3181 * If "lookup" is not NULL recognize expression without "eval" or "call" and
3182 * assignment without "let". Sets eap->cmdidx to the command while returning
3183 * "eap->cmd".
3184 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00003185 * Returns NULL for an ambiguous user command.
3186 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003187 char_u *
3188find_ex_command(
3189 exarg_T *eap,
Bram Moolenaara494f562020-04-18 17:45:38 +02003190 int *full UNUSED,
Bram Moolenaarb84a3812020-05-01 15:44:29 +02003191 void *(*lookup)(char_u *, size_t, cctx_T *) UNUSED,
Bram Moolenaara494f562020-04-18 17:45:38 +02003192 cctx_T *cctx UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003193{
3194 int len;
3195 char_u *p;
Bram Moolenaardf177f62005-02-22 08:39:57 +00003196 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003197
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003198#ifdef FEAT_EVAL
3199 /*
3200 * Recognize a Vim9 script function/method call and assignment:
3201 * "lvar = value", "lvar(arg)", "[1, 2 3]->Func()"
3202 */
Bram Moolenaar0c6ceaf2020-02-22 18:36:32 +01003203 p = eap->cmd;
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003204 if (lookup != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003205 {
Bram Moolenaar2e800952020-08-23 19:34:48 +02003206 char_u *pskip = skip_option_env_lead(eap->cmd);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003207
Bram Moolenaar83144542020-08-02 20:40:43 +02003208 if (vim_strchr((char_u *)"{('[\"@", *p) != NULL
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003209 || ((p = to_name_const_end(pskip)) > eap->cmd && *p != NUL))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003210 {
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003211 int oplen;
3212 int heredoc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003213
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003214 if (
3215 // "(..." is an expression.
3216 // "funcname(" is always a function call.
3217 *p == '('
3218 || (p == eap->cmd
3219 ? (
3220 // "{..." is an dict expression.
3221 *eap->cmd == '{'
3222 // "'string'->func()" is an expression.
3223 || *eap->cmd == '\''
Bram Moolenaar10409562020-07-29 20:00:38 +02003224 // '"string"->func()' is an expression.
3225 || *eap->cmd == '"'
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003226 // "g:varname" is an expression.
3227 || eap->cmd[1] == ':'
3228 )
3229 : (
3230 // "varname[]" is an expression.
3231 *p == '['
3232 // "varname->func()" is an expression.
3233 || (*p == '-' && p[1] == '>')
3234 // "varname.expr" is an expression.
3235 || (*p == '.' && ASCII_ISALPHA(p[1]))
3236 )))
Bram Moolenaarb31be3f2020-07-20 22:37:44 +02003237 {
3238 eap->cmdidx = CMD_eval;
3239 return eap->cmd;
3240 }
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003241
3242 // "[...]->Method()" is a list expression, but "[a, b] = Func()" is
3243 // an assignment.
3244 // If there is no line break inside the "[...]" then "p" is
3245 // advanced to after the "]" by to_name_const_end(): check if a "="
3246 // follows.
3247 // If "[...]" has a line break "p" still points at the "[" and it
3248 // can't be an assignment.
3249 if (*eap->cmd == '[')
Bram Moolenaarb31be3f2020-07-20 22:37:44 +02003250 {
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003251 p = to_name_const_end(eap->cmd);
3252 if (p == eap->cmd || *skipwhite(p) != '=')
3253 {
3254 eap->cmdidx = CMD_eval;
3255 return eap->cmd;
3256 }
3257 if (p > eap->cmd && *skipwhite(p) == '=')
3258 {
3259 eap->cmdidx = CMD_let;
3260 return eap->cmd;
3261 }
3262 }
3263
3264 // Recognize an assignment if we recognize the variable name:
3265 // "g:var = expr"
3266 // "var = expr" where "var" is a local var name.
Bram Moolenaar83144542020-08-02 20:40:43 +02003267 if (*eap->cmd == '@')
3268 p = eap->cmd + 2;
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003269 oplen = assignment_len(skipwhite(p), &heredoc);
3270 if (oplen > 0)
3271 {
3272 if (((p - eap->cmd) > 2 && eap->cmd[1] == ':')
3273 || *eap->cmd == '&'
3274 || *eap->cmd == '$'
3275 || *eap->cmd == '@'
3276 || lookup(eap->cmd, p - eap->cmd, cctx) != NULL)
3277 {
3278 eap->cmdidx = CMD_let;
3279 return eap->cmd;
3280 }
3281 }
3282
3283 // Recognize using a type for a w:, b:, t: or g: variable:
3284 // "w:varname: number = 123".
3285 if (eap->cmd[1] == ':' && *p == ':')
3286 {
3287 eap->cmdidx = CMD_eval;
Bram Moolenaarb31be3f2020-07-20 22:37:44 +02003288 return eap->cmd;
3289 }
Bram Moolenaard2ef6b32020-07-02 21:11:34 +02003290 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003291 }
3292#endif
3293
Bram Moolenaar071d4272004-06-13 20:20:40 +00003294 /*
3295 * Isolate the command and search for it in the command table.
Bram Moolenaar81870892007-11-11 18:17:28 +00003296 * Exceptions:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003297 * - the 'k' command can directly be followed by any character.
3298 * - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003299 * but :sre[wind] is another command, as are :scr[iptnames],
Bram Moolenaar071d4272004-06-13 20:20:40 +00003300 * :scs[cope], :sim[alt], :sig[ns] and :sil[ent].
Bram Moolenaardf177f62005-02-22 08:39:57 +00003301 * - the "d" command can directly be followed by 'l' or 'p' flag.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003302 */
3303 p = eap->cmd;
3304 if (*p == 'k')
3305 {
3306 eap->cmdidx = CMD_k;
3307 ++p;
3308 }
3309 else if (p[0] == 's'
Bram Moolenaar204b93f2015-08-04 22:02:51 +02003310 && ((p[1] == 'c' && (p[2] == NUL || (p[2] != 's' && p[2] != 'r'
3311 && (p[3] == NUL || (p[3] != 'i' && p[4] != 'p')))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003312 || p[1] == 'g'
3313 || (p[1] == 'i' && p[2] != 'm' && p[2] != 'l' && p[2] != 'g')
3314 || p[1] == 'I'
3315 || (p[1] == 'r' && p[2] != 'e')))
3316 {
3317 eap->cmdidx = CMD_substitute;
3318 ++p;
3319 }
3320 else
3321 {
3322 while (ASCII_ISALPHA(*p))
3323 ++p;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003324 // for python 3.x support ":py3", ":python3", ":py3file", etc.
Bram Moolenaar55d5c032010-07-17 23:52:29 +02003325 if (eap->cmd[0] == 'p' && eap->cmd[1] == 'y')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003326 {
Bram Moolenaarb6590522010-07-21 16:00:43 +02003327 while (ASCII_ISALNUM(*p))
3328 ++p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003329 }
3330 else if (*p == '9' && STRNCMP("vim9", eap->cmd, 4) == 0)
3331 {
3332 // include "9" for "vim9script"
3333 ++p;
3334 while (ASCII_ISALPHA(*p))
3335 ++p;
3336 }
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02003337
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003338 // check for non-alpha command
Bram Moolenaar071d4272004-06-13 20:20:40 +00003339 if (p == eap->cmd && vim_strchr((char_u *)"@*!=><&~#", *p) != NULL)
3340 ++p;
3341 len = (int)(p - eap->cmd);
Bram Moolenaardf177f62005-02-22 08:39:57 +00003342 if (*eap->cmd == 'd' && (p[-1] == 'l' || p[-1] == 'p'))
3343 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003344 // Check for ":dl", ":dell", etc. to ":deletel": that's
3345 // :delete with the 'l' flag. Same for 'p'.
Bram Moolenaardf177f62005-02-22 08:39:57 +00003346 for (i = 0; i < len; ++i)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003347 if (eap->cmd[i] != ((char_u *)"delete")[i])
Bram Moolenaardf177f62005-02-22 08:39:57 +00003348 break;
3349 if (i == len - 1)
3350 {
3351 --len;
3352 if (p[-1] == 'l')
3353 eap->flags |= EXFLAG_LIST;
3354 else
3355 eap->flags |= EXFLAG_PRINT;
3356 }
3357 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003358
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003359 if (ASCII_ISLOWER(eap->cmd[0]))
3360 {
Bram Moolenaar6c0c1e82017-03-25 15:07:43 +01003361 int c1 = eap->cmd[0];
Bram Moolenaar94f82cb2019-07-24 22:30:27 +02003362 int c2 = len == 1 ? NUL : eap->cmd[1];
Bram Moolenaar6c0c1e82017-03-25 15:07:43 +01003363
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003364 if (command_count != (int)CMD_SIZE)
3365 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003366 iemsg(_("E943: Command table needs to be updated, run 'make cmdidxs'"));
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003367 getout(1);
3368 }
3369
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003370 // Use a precomputed index for fast look-up in cmdnames[]
3371 // taking into account the first 2 letters of eap->cmd.
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003372 eap->cmdidx = cmdidxs1[CharOrdLow(c1)];
3373 if (ASCII_ISLOWER(c2))
3374 eap->cmdidx += cmdidxs2[CharOrdLow(c1)][CharOrdLow(c2)];
3375 }
Bram Moolenaara494f562020-04-18 17:45:38 +02003376 else if (ASCII_ISUPPER(eap->cmd[0]))
3377 eap->cmdidx = CMD_Next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003378 else
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003379 eap->cmdidx = CMD_bang;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003380
3381 for ( ; (int)eap->cmdidx < (int)CMD_SIZE;
3382 eap->cmdidx = (cmdidx_T)((int)eap->cmdidx + 1))
3383 if (STRNCMP(cmdnames[(int)eap->cmdidx].cmd_name, (char *)eap->cmd,
3384 (size_t)len) == 0)
3385 {
3386#ifdef FEAT_EVAL
3387 if (full != NULL
3388 && cmdnames[(int)eap->cmdidx].cmd_name[len] == NUL)
3389 *full = TRUE;
3390#endif
3391 break;
3392 }
3393
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003394 // Look for a user defined command as a last resort. Let ":Print" be
3395 // overruled by a user defined command.
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01003396 if ((eap->cmdidx == CMD_SIZE || eap->cmdidx == CMD_Print)
3397 && *eap->cmd >= 'A' && *eap->cmd <= 'Z')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003398 {
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003399 // User defined commands may contain digits.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003400 while (ASCII_ISALNUM(*p))
3401 ++p;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003402 p = find_ucmd(eap, p, full, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003403 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003404 if (p == eap->cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003405 eap->cmdidx = CMD_SIZE;
3406 }
3407
3408 return p;
3409}
3410
3411#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003412static struct cmdmod
3413{
3414 char *name;
3415 int minlen;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003416 int has_count; // :123verbose :3tab
Bram Moolenaared53fb92007-11-24 20:50:24 +00003417} cmdmods[] = {
3418 {"aboveleft", 3, FALSE},
3419 {"belowright", 3, FALSE},
3420 {"botright", 2, FALSE},
3421 {"browse", 3, FALSE},
3422 {"confirm", 4, FALSE},
Bram Moolenaar7b668e82016-08-23 23:51:21 +02003423 {"filter", 4, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003424 {"hide", 3, FALSE},
3425 {"keepalt", 5, FALSE},
3426 {"keepjumps", 5, FALSE},
3427 {"keepmarks", 3, FALSE},
Bram Moolenaara939e432013-11-09 05:30:26 +01003428 {"keeppatterns", 5, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003429 {"leftabove", 5, FALSE},
3430 {"lockmarks", 3, FALSE},
Bram Moolenaarca9f9582008-09-18 10:44:28 +00003431 {"noautocmd", 3, FALSE},
Bram Moolenaar5803ae62014-03-23 16:04:02 +01003432 {"noswapfile", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003433 {"rightbelow", 6, FALSE},
3434 {"sandbox", 3, FALSE},
3435 {"silent", 3, FALSE},
3436 {"tab", 3, TRUE},
3437 {"topleft", 2, FALSE},
Bram Moolenaar8e258a42009-07-09 13:55:43 +00003438 {"unsilent", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003439 {"verbose", 4, TRUE},
3440 {"vertical", 4, FALSE},
3441};
3442
3443/*
3444 * Return length of a command modifier (including optional count).
3445 * Return zero when it's not a modifier.
3446 */
3447 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003448modifier_len(char_u *cmd)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003449{
3450 int i, j;
3451 char_u *p = cmd;
3452
3453 if (VIM_ISDIGIT(*cmd))
3454 p = skipwhite(skipdigits(cmd));
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00003455 for (i = 0; i < (int)(sizeof(cmdmods) / sizeof(struct cmdmod)); ++i)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003456 {
3457 for (j = 0; p[j] != NUL; ++j)
3458 if (p[j] != cmdmods[i].name[j])
3459 break;
Bram Moolenaar2d473ab2013-06-12 17:12:24 +02003460 if (!ASCII_ISALPHA(p[j]) && j >= cmdmods[i].minlen
Bram Moolenaared53fb92007-11-24 20:50:24 +00003461 && (p == cmd || cmdmods[i].has_count))
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00003462 return j + (int)(p - cmd);
Bram Moolenaared53fb92007-11-24 20:50:24 +00003463 }
3464 return 0;
3465}
3466
Bram Moolenaar071d4272004-06-13 20:20:40 +00003467/*
3468 * Return > 0 if an Ex command "name" exists.
3469 * Return 2 if there is an exact match.
3470 * Return 3 if there is an ambiguous match.
3471 */
3472 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003473cmd_exists(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003474{
3475 exarg_T ea;
3476 int full = FALSE;
3477 int i;
3478 int j;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003479 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003480
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003481 // Check command modifiers.
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00003482 for (i = 0; i < (int)(sizeof(cmdmods) / sizeof(struct cmdmod)); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003483 {
3484 for (j = 0; name[j] != NUL; ++j)
3485 if (name[j] != cmdmods[i].name[j])
3486 break;
3487 if (name[j] == NUL && j >= cmdmods[i].minlen)
3488 return (cmdmods[i].name[j] == NUL ? 2 : 1);
3489 }
3490
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003491 // Check built-in commands and user defined commands.
3492 // For ":2match" and ":3match" we need to skip the number.
Bram Moolenaara9587612006-05-04 21:47:50 +00003493 ea.cmd = (*name == '2' || *name == '3') ? name + 1 : name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003494 ea.cmdidx = (cmdidx_T)0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003495 p = find_ex_command(&ea, &full, NULL, NULL);
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003496 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003497 return 3;
Bram Moolenaara9587612006-05-04 21:47:50 +00003498 if (vim_isdigit(*name) && ea.cmdidx != CMD_match)
3499 return 0;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003500 if (*skipwhite(p) != NUL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003501 return 0; // trailing garbage
Bram Moolenaar071d4272004-06-13 20:20:40 +00003502 return (ea.cmdidx == CMD_SIZE ? 0 : (full ? 2 : 1));
3503}
3504#endif
3505
Bram Moolenaard0190392019-08-23 21:17:35 +02003506 cmdidx_T
3507excmd_get_cmdidx(char_u *cmd, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003508{
Bram Moolenaard0190392019-08-23 21:17:35 +02003509 cmdidx_T idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003510
Bram Moolenaard0190392019-08-23 21:17:35 +02003511 for (idx = (cmdidx_T)0; (int)idx < (int)CMD_SIZE;
3512 idx = (cmdidx_T)((int)idx + 1))
3513 if (STRNCMP(cmdnames[(int)idx].cmd_name, cmd, (size_t)len) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003514 break;
3515
Bram Moolenaard0190392019-08-23 21:17:35 +02003516 return idx;
3517}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003518
Bram Moolenaard0190392019-08-23 21:17:35 +02003519 long
3520excmd_get_argt(cmdidx_T idx)
3521{
3522 return (long)cmdnames[(int)idx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003523}
3524
3525/*
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003526 * Skip a range specifier of the form: addr [,addr] [;addr] ..
Bram Moolenaar071d4272004-06-13 20:20:40 +00003527 *
3528 * Backslashed delimiters after / or ? will be skipped, and commands will
3529 * not be expanded between /'s and ?'s or after "'".
3530 *
Bram Moolenaardf069ee2020-06-22 23:02:51 +02003531 * Also skip white space and ":" characters after the range.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003532 * Returns the "cmd" pointer advanced to beyond the range.
3533 */
3534 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003535skip_range(
3536 char_u *cmd,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003537 int *ctx) // pointer to xp_context or NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003538{
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003539 unsigned delim;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003540
Bram Moolenaarcbf20fb2017-02-03 21:19:04 +01003541 while (vim_strchr((char_u *)" \t0123456789.$%'/?-+,;\\", *cmd) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003542 {
Bram Moolenaarcbf20fb2017-02-03 21:19:04 +01003543 if (*cmd == '\\')
3544 {
3545 if (cmd[1] == '?' || cmd[1] == '/' || cmd[1] == '&')
3546 ++cmd;
3547 else
3548 break;
3549 }
3550 else if (*cmd == '\'')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003551 {
3552 if (*++cmd == NUL && ctx != NULL)
3553 *ctx = EXPAND_NOTHING;
3554 }
3555 else if (*cmd == '/' || *cmd == '?')
3556 {
3557 delim = *cmd++;
3558 while (*cmd != NUL && *cmd != delim)
3559 if (*cmd++ == '\\' && *cmd != NUL)
3560 ++cmd;
3561 if (*cmd == NUL && ctx != NULL)
3562 *ctx = EXPAND_NOTHING;
3563 }
3564 if (*cmd != NUL)
3565 ++cmd;
3566 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00003567
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003568 // Skip ":" and white space.
Bram Moolenaardf177f62005-02-22 08:39:57 +00003569 while (*cmd == ':')
3570 cmd = skipwhite(cmd + 1);
3571
Bram Moolenaar071d4272004-06-13 20:20:40 +00003572 return cmd;
3573}
3574
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003575 static void
3576addr_error(cmd_addr_T addr_type)
3577{
3578 if (addr_type == ADDR_NONE)
3579 emsg(_(e_norange));
3580 else
3581 emsg(_(e_invrange));
3582}
3583
Bram Moolenaar071d4272004-06-13 20:20:40 +00003584/*
Bram Moolenaarc2af0af2020-08-23 21:06:02 +02003585 * Return the default address for an address type.
3586 */
3587 static linenr_T
3588default_address(exarg_T *eap)
3589{
3590 linenr_T lnum = 0;
3591
3592 switch (eap->addr_type)
3593 {
3594 case ADDR_LINES:
3595 case ADDR_OTHER:
3596 // Default is the cursor line number. Avoid using an invalid
3597 // line number though.
3598 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
3599 lnum = curbuf->b_ml.ml_line_count;
3600 else
3601 lnum = curwin->w_cursor.lnum;
3602 break;
3603 case ADDR_WINDOWS:
3604 lnum = CURRENT_WIN_NR;
3605 break;
3606 case ADDR_ARGUMENTS:
3607 lnum = curwin->w_arg_idx + 1;
3608 if (lnum > ARGCOUNT)
3609 lnum = ARGCOUNT;
3610 break;
3611 case ADDR_LOADED_BUFFERS:
3612 case ADDR_BUFFERS:
3613 lnum = curbuf->b_fnum;
3614 break;
3615 case ADDR_TABS:
3616 lnum = CURRENT_TAB_NR;
3617 break;
3618 case ADDR_TABS_RELATIVE:
3619 case ADDR_UNSIGNED:
3620 lnum = 1;
3621 break;
3622 case ADDR_QUICKFIX:
3623#ifdef FEAT_QUICKFIX
3624 lnum = qf_get_cur_idx(eap);
3625#endif
3626 break;
3627 case ADDR_QUICKFIX_VALID:
3628#ifdef FEAT_QUICKFIX
3629 lnum = qf_get_cur_valid_idx(eap);
3630#endif
3631 break;
3632 case ADDR_NONE:
3633 // Will give an error later if a range is found.
3634 break;
3635 }
3636 return lnum;
3637}
3638
3639/*
Bram Moolenaar198cb662018-09-06 21:44:17 +02003640 * Get a single EX address.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003641 *
3642 * Set ptr to the next character after the part that was interpreted.
3643 * Set ptr to NULL when an error is encountered.
Bram Moolenaar198cb662018-09-06 21:44:17 +02003644 * This may set the last used search pattern.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003645 *
3646 * Return MAXLNUM when no Ex address was found.
3647 */
3648 static linenr_T
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003649get_address(
3650 exarg_T *eap UNUSED,
3651 char_u **ptr,
Bram Moolenaar1b03a192019-12-08 17:08:29 +01003652 cmd_addr_T addr_type,
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02003653 int skip, // only skip the address, don't use it
3654 int silent, // no errors or side effects
3655 int to_other_file, // flag: may jump to other file
3656 int address_count UNUSED) // 1 for first address, >1 after comma
Bram Moolenaar071d4272004-06-13 20:20:40 +00003657{
3658 int c;
3659 int i;
3660 long n;
3661 char_u *cmd;
3662 pos_T pos;
3663 pos_T *fp;
3664 linenr_T lnum;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003665 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003666
3667 cmd = skipwhite(*ptr);
3668 lnum = MAXLNUM;
3669 do
3670 {
3671 switch (*cmd)
3672 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003673 case '.': // '.' - Cursor position
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003674 ++cmd;
3675 switch (addr_type)
3676 {
3677 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02003678 case ADDR_OTHER:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003679 lnum = curwin->w_cursor.lnum;
3680 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003681 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01003682 lnum = CURRENT_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003683 break;
3684 case ADDR_ARGUMENTS:
3685 lnum = curwin->w_arg_idx + 1;
3686 break;
3687 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003688 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003689 lnum = curbuf->b_fnum;
3690 break;
3691 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01003692 lnum = CURRENT_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003693 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02003694 case ADDR_NONE:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003695 case ADDR_TABS_RELATIVE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02003696 case ADDR_UNSIGNED:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003697 addr_error(addr_type);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01003698 cmd = NULL;
3699 goto error;
3700 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003701 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003702#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02003703 lnum = qf_get_cur_idx(eap);
3704#endif
3705 break;
3706 case ADDR_QUICKFIX_VALID:
3707#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003708 lnum = qf_get_cur_valid_idx(eap);
Bram Moolenaare906c502015-09-09 21:10:39 +02003709#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003710 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003711 }
3712 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003713
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003714 case '$': // '$' - last line
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003715 ++cmd;
3716 switch (addr_type)
3717 {
3718 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02003719 case ADDR_OTHER:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003720 lnum = curbuf->b_ml.ml_line_count;
3721 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003722 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01003723 lnum = LAST_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003724 break;
3725 case ADDR_ARGUMENTS:
3726 lnum = ARGCOUNT;
3727 break;
3728 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003729 buf = lastbuf;
3730 while (buf->b_ml.ml_mfp == NULL)
3731 {
3732 if (buf->b_prev == NULL)
3733 break;
3734 buf = buf->b_prev;
3735 }
3736 lnum = buf->b_fnum;
3737 break;
3738 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003739 lnum = lastbuf->b_fnum;
3740 break;
3741 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01003742 lnum = LAST_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003743 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02003744 case ADDR_NONE:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003745 case ADDR_TABS_RELATIVE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02003746 case ADDR_UNSIGNED:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003747 addr_error(addr_type);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01003748 cmd = NULL;
3749 goto error;
3750 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003751 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003752#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003753 lnum = qf_get_size(eap);
3754 if (lnum == 0)
3755 lnum = 1;
Bram Moolenaare906c502015-09-09 21:10:39 +02003756#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003757 break;
Bram Moolenaar25190db2019-05-04 15:05:28 +02003758 case ADDR_QUICKFIX_VALID:
3759#ifdef FEAT_QUICKFIX
3760 lnum = qf_get_valid_size(eap);
3761 if (lnum == 0)
3762 lnum = 1;
3763#endif
3764 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003765 }
3766 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003767
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003768 case '\'': // ''' - mark
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003769 if (*++cmd == NUL)
3770 {
3771 cmd = NULL;
3772 goto error;
3773 }
3774 if (addr_type != ADDR_LINES)
3775 {
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003776 addr_error(addr_type);
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01003777 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003778 goto error;
3779 }
3780 if (skip)
3781 ++cmd;
3782 else
3783 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003784 // Only accept a mark in another file when it is
3785 // used by itself: ":'M".
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003786 fp = getmark(*cmd, to_other_file && cmd[1] == NUL);
3787 ++cmd;
3788 if (fp == (pos_T *)-1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003789 // Jumped to another file.
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003790 lnum = curwin->w_cursor.lnum;
3791 else
3792 {
3793 if (check_mark(fp) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003794 {
3795 cmd = NULL;
3796 goto error;
3797 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003798 lnum = fp->lnum;
3799 }
3800 }
3801 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003802
3803 case '/':
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003804 case '?': // '/' or '?' - search
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003805 c = *cmd++;
3806 if (addr_type != ADDR_LINES)
3807 {
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003808 addr_error(addr_type);
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01003809 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003810 goto error;
3811 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003812 if (skip) // skip "/pat/"
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003813 {
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02003814 cmd = skip_regexp(cmd, c, (int)p_magic);
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003815 if (*cmd == c)
3816 ++cmd;
3817 }
3818 else
3819 {
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02003820 int flags;
3821
3822 pos = curwin->w_cursor; // save curwin->w_cursor
3823
3824 // When '/' or '?' follows another address, start from
3825 // there.
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003826 if (lnum != MAXLNUM)
3827 curwin->w_cursor.lnum = lnum;
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02003828
3829 // Start a forward search at the end of the line (unless
3830 // before the first line).
3831 // Start a backward search at the start of the line.
3832 // This makes sure we never match in the current
3833 // line, and can match anywhere in the
3834 // next/previous line.
Bram Moolenaar8ada6aa2017-12-19 21:23:21 +01003835 if (c == '/' && curwin->w_cursor.lnum > 0)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003836 curwin->w_cursor.col = MAXCOL;
3837 else
3838 curwin->w_cursor.col = 0;
3839 searchcmdlen = 0;
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02003840 flags = silent ? 0 : SEARCH_HIS | SEARCH_MSG;
Bram Moolenaarc036e872020-02-21 21:30:52 +01003841 if (!do_search(NULL, c, c, cmd, 1L, flags, NULL))
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003842 {
3843 curwin->w_cursor = pos;
3844 cmd = NULL;
3845 goto error;
3846 }
3847 lnum = curwin->w_cursor.lnum;
3848 curwin->w_cursor = pos;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003849 // adjust command string pointer
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003850 cmd += searchcmdlen;
3851 }
3852 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003853
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003854 case '\\': // "\?", "\/" or "\&", repeat search
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003855 ++cmd;
3856 if (addr_type != ADDR_LINES)
3857 {
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003858 addr_error(addr_type);
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01003859 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003860 goto error;
3861 }
3862 if (*cmd == '&')
3863 i = RE_SUBST;
3864 else if (*cmd == '?' || *cmd == '/')
3865 i = RE_SEARCH;
3866 else
3867 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003868 emsg(_(e_backslash));
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003869 cmd = NULL;
3870 goto error;
3871 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003872
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003873 if (!skip)
3874 {
3875 /*
3876 * When search follows another address, start from
3877 * there.
3878 */
3879 if (lnum != MAXLNUM)
3880 pos.lnum = lnum;
3881 else
3882 pos.lnum = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003883
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003884 /*
3885 * Start the search just like for the above
3886 * do_search().
3887 */
3888 if (*cmd != '?')
3889 pos.col = MAXCOL;
3890 else
3891 pos.col = 0;
Bram Moolenaarbd8539a2015-08-11 18:53:03 +02003892 pos.coladd = 0;
Bram Moolenaar5d24a222018-12-23 19:10:09 +01003893 if (searchit(curwin, curbuf, &pos, NULL,
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003894 *cmd == '?' ? BACKWARD : FORWARD,
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02003895 (char_u *)"", 1L, SEARCH_MSG, i, NULL) != FAIL)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003896 lnum = pos.lnum;
3897 else
3898 {
3899 cmd = NULL;
3900 goto error;
3901 }
3902 }
3903 ++cmd;
3904 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003905
3906 default:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003907 if (VIM_ISDIGIT(*cmd)) // absolute line number
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003908 lnum = getdigits(&cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003909 }
3910
3911 for (;;)
3912 {
3913 cmd = skipwhite(cmd);
3914 if (*cmd != '-' && *cmd != '+' && !VIM_ISDIGIT(*cmd))
3915 break;
3916
3917 if (lnum == MAXLNUM)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003918 {
3919 switch (addr_type)
3920 {
3921 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02003922 case ADDR_OTHER:
3923 // "+1" is same as ".+1"
Bram Moolenaarf240e182014-11-27 18:33:02 +01003924 lnum = curwin->w_cursor.lnum;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003925 break;
3926 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01003927 lnum = CURRENT_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003928 break;
3929 case ADDR_ARGUMENTS:
3930 lnum = curwin->w_arg_idx + 1;
3931 break;
3932 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003933 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003934 lnum = curbuf->b_fnum;
3935 break;
3936 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01003937 lnum = CURRENT_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003938 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01003939 case ADDR_TABS_RELATIVE:
3940 lnum = 1;
3941 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003942 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003943#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02003944 lnum = qf_get_cur_idx(eap);
3945#endif
3946 break;
3947 case ADDR_QUICKFIX_VALID:
3948#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003949 lnum = qf_get_cur_valid_idx(eap);
Bram Moolenaare906c502015-09-09 21:10:39 +02003950#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003951 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02003952 case ADDR_NONE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02003953 case ADDR_UNSIGNED:
3954 lnum = 0;
Bram Moolenaarb7316892019-05-01 18:08:42 +02003955 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003956 }
3957 }
3958
Bram Moolenaar071d4272004-06-13 20:20:40 +00003959 if (VIM_ISDIGIT(*cmd))
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003960 i = '+'; // "number" is same as "+number"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003961 else
3962 i = *cmd++;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003963 if (!VIM_ISDIGIT(*cmd)) // '+' is '+1', but '+0' is not '+1'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003964 n = 1;
3965 else
3966 n = getdigits(&cmd);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01003967
3968 if (addr_type == ADDR_TABS_RELATIVE)
3969 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003970 emsg(_(e_invrange));
Bram Moolenaar2f72c702017-01-29 14:48:10 +01003971 cmd = NULL;
3972 goto error;
3973 }
3974 else if (addr_type == ADDR_LOADED_BUFFERS
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003975 || addr_type == ADDR_BUFFERS)
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01003976 lnum = compute_buffer_local_count(
3977 addr_type, lnum, (i == '-') ? -1 * n : n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003978 else
Bram Moolenaarded27822017-01-02 14:27:34 +01003979 {
3980#ifdef FEAT_FOLDING
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003981 // Relative line addressing, need to adjust for folded lines
3982 // now, but only do it after the first address.
Bram Moolenaarded27822017-01-02 14:27:34 +01003983 if (addr_type == ADDR_LINES && (i == '-' || i == '+')
3984 && address_count >= 2)
3985 (void)hasFolding(lnum, NULL, &lnum);
3986#endif
3987 if (i == '-')
3988 lnum -= n;
3989 else
3990 lnum += n;
3991 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003992 }
3993 } while (*cmd == '/' || *cmd == '?');
3994
3995error:
3996 *ptr = cmd;
3997 return lnum;
3998}
3999
4000/*
Bram Moolenaarc2af0af2020-08-23 21:06:02 +02004001 * Set eap->line1 and eap->line2 to the whole range.
4002 * Used for commands with the EX_DFLALL flag and no range given.
4003 */
4004 static void
4005address_default_all(exarg_T *eap)
4006{
4007 eap->line1 = 1;
4008 switch (eap->addr_type)
4009 {
4010 case ADDR_LINES:
4011 case ADDR_OTHER:
4012 eap->line2 = curbuf->b_ml.ml_line_count;
4013 break;
4014 case ADDR_LOADED_BUFFERS:
4015 {
4016 buf_T *buf = firstbuf;
4017
4018 while (buf->b_next != NULL && buf->b_ml.ml_mfp == NULL)
4019 buf = buf->b_next;
4020 eap->line1 = buf->b_fnum;
4021 buf = lastbuf;
4022 while (buf->b_prev != NULL && buf->b_ml.ml_mfp == NULL)
4023 buf = buf->b_prev;
4024 eap->line2 = buf->b_fnum;
4025 }
4026 break;
4027 case ADDR_BUFFERS:
4028 eap->line1 = firstbuf->b_fnum;
4029 eap->line2 = lastbuf->b_fnum;
4030 break;
4031 case ADDR_WINDOWS:
4032 eap->line2 = LAST_WIN_NR;
4033 break;
4034 case ADDR_TABS:
4035 eap->line2 = LAST_TAB_NR;
4036 break;
4037 case ADDR_TABS_RELATIVE:
4038 eap->line2 = 1;
4039 break;
4040 case ADDR_ARGUMENTS:
4041 if (ARGCOUNT == 0)
4042 eap->line1 = eap->line2 = 0;
4043 else
4044 eap->line2 = ARGCOUNT;
4045 break;
4046 case ADDR_QUICKFIX_VALID:
4047#ifdef FEAT_QUICKFIX
4048 eap->line2 = qf_get_valid_size(eap);
4049 if (eap->line2 == 0)
4050 eap->line2 = 1;
4051#endif
4052 break;
4053 case ADDR_NONE:
4054 case ADDR_UNSIGNED:
4055 case ADDR_QUICKFIX:
4056 iemsg(_("INTERNAL: Cannot use EX_DFLALL with ADDR_NONE, ADDR_UNSIGNED or ADDR_QUICKFIX"));
4057 break;
4058 }
4059}
4060
4061
4062/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00004063 * Get flags from an Ex command argument.
4064 */
4065 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004066get_flags(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00004067{
4068 while (vim_strchr((char_u *)"lp#", *eap->arg) != NULL)
4069 {
4070 if (*eap->arg == 'l')
4071 eap->flags |= EXFLAG_LIST;
4072 else if (*eap->arg == 'p')
4073 eap->flags |= EXFLAG_PRINT;
4074 else
4075 eap->flags |= EXFLAG_NR;
4076 eap->arg = skipwhite(eap->arg + 1);
4077 }
4078}
4079
4080/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004081 * Function called for command which is Not Implemented. NI!
4082 */
4083 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004084ex_ni(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004085{
4086 if (!eap->skip)
Bram Moolenaar8930caa2020-07-23 16:37:03 +02004087 eap->errmsg =
4088 _("E319: Sorry, the command is not available in this version");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004089}
4090
Bram Moolenaar7bb75552007-07-16 18:39:49 +00004091#ifdef HAVE_EX_SCRIPT_NI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004092/*
4093 * Function called for script command which is Not Implemented. NI!
4094 * Skips over ":perl <<EOF" constructs.
4095 */
4096 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004097ex_script_ni(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004098{
4099 if (!eap->skip)
4100 ex_ni(eap);
4101 else
4102 vim_free(script_get(eap, eap->arg));
4103}
4104#endif
4105
4106/*
4107 * Check range in Ex command for validity.
4108 * Return NULL when valid, error message when invalid.
4109 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004110 static char *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004111invalid_range(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004112{
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004113 buf_T *buf;
Bram Moolenaar25190db2019-05-04 15:05:28 +02004114
Bram Moolenaar071d4272004-06-13 20:20:40 +00004115 if ( eap->line1 < 0
4116 || eap->line2 < 0
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004117 || eap->line1 > eap->line2)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004118 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004119
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004120 if (eap->argt & EX_RANGE)
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004121 {
Bram Moolenaarb7316892019-05-01 18:08:42 +02004122 switch (eap->addr_type)
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004123 {
4124 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004125 if (eap->line2 > curbuf->b_ml.ml_line_count
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004126#ifdef FEAT_DIFF
4127 + (eap->cmdidx == CMD_diffget)
4128#endif
4129 )
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004130 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004131 break;
4132 case ADDR_ARGUMENTS:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004133 // add 1 if ARGCOUNT is 0
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004134 if (eap->line2 > ARGCOUNT + (!ARGCOUNT))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004135 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004136 break;
4137 case ADDR_BUFFERS:
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02004138 // Only a boundary check, not whether the buffers actually
4139 // exist.
4140 if (eap->line1 < 1 || eap->line2 > get_highest_fnum())
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004141 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004142 break;
4143 case ADDR_LOADED_BUFFERS:
4144 buf = firstbuf;
4145 while (buf->b_ml.ml_mfp == NULL)
4146 {
4147 if (buf->b_next == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004148 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004149 buf = buf->b_next;
4150 }
4151 if (eap->line1 < buf->b_fnum)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004152 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004153 buf = lastbuf;
4154 while (buf->b_ml.ml_mfp == NULL)
4155 {
4156 if (buf->b_prev == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004157 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004158 buf = buf->b_prev;
4159 }
4160 if (eap->line2 > buf->b_fnum)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004161 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004162 break;
4163 case ADDR_WINDOWS:
Bram Moolenaar8be63882015-01-14 11:25:05 +01004164 if (eap->line2 > LAST_WIN_NR)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004165 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004166 break;
4167 case ADDR_TABS:
4168 if (eap->line2 > LAST_TAB_NR)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004169 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004170 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004171 case ADDR_TABS_RELATIVE:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004172 case ADDR_OTHER:
4173 // Any range is OK.
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004174 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004175 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004176#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02004177 // No error for value that is too big, will use the last entry.
4178 if (eap->line2 <= 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004179 return _(e_invrange);
Bram Moolenaare906c502015-09-09 21:10:39 +02004180#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004181 break;
Bram Moolenaar25190db2019-05-04 15:05:28 +02004182 case ADDR_QUICKFIX_VALID:
4183#ifdef FEAT_QUICKFIX
4184 if ((eap->line2 != 1 && eap->line2 > qf_get_valid_size(eap))
4185 || eap->line2 < 0)
4186 return _(e_invrange);
4187#endif
4188 break;
4189 case ADDR_UNSIGNED:
4190 if (eap->line2 < 0)
4191 return _(e_invrange);
4192 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02004193 case ADDR_NONE:
4194 // Will give an error elsewhere.
4195 break;
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004196 }
4197 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004198 return NULL;
4199}
4200
4201/*
4202 * Correct the range for zero line number, if required.
4203 */
4204 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004205correct_range(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004206{
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004207 if (!(eap->argt & EX_ZEROR)) // zero in range not allowed
Bram Moolenaar071d4272004-06-13 20:20:40 +00004208 {
4209 if (eap->line1 == 0)
4210 eap->line1 = 1;
4211 if (eap->line2 == 0)
4212 eap->line2 = 1;
4213 }
4214}
4215
Bram Moolenaar748bf032005-02-02 23:04:36 +00004216#ifdef FEAT_QUICKFIX
Bram Moolenaar748bf032005-02-02 23:04:36 +00004217/*
4218 * For a ":vimgrep" or ":vimgrepadd" command return a pointer past the
4219 * pattern. Otherwise return eap->arg.
4220 */
4221 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004222skip_grep_pat(exarg_T *eap)
Bram Moolenaar748bf032005-02-02 23:04:36 +00004223{
4224 char_u *p = eap->arg;
4225
Bram Moolenaara37420f2006-02-04 22:37:47 +00004226 if (*p != NUL && (eap->cmdidx == CMD_vimgrep || eap->cmdidx == CMD_lvimgrep
4227 || eap->cmdidx == CMD_vimgrepadd
4228 || eap->cmdidx == CMD_lvimgrepadd
4229 || grep_internal(eap->cmdidx)))
Bram Moolenaar748bf032005-02-02 23:04:36 +00004230 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00004231 p = skip_vimgrep_pat(p, NULL, NULL);
Bram Moolenaar748bf032005-02-02 23:04:36 +00004232 if (p == NULL)
4233 p = eap->arg;
Bram Moolenaar748bf032005-02-02 23:04:36 +00004234 }
4235 return p;
4236}
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004237
4238/*
4239 * For the ":make" and ":grep" commands insert the 'makeprg'/'grepprg' option
4240 * in the command line, so that things like % get expanded.
4241 */
4242 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004243replace_makeprg(exarg_T *eap, char_u *p, char_u **cmdlinep)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004244{
4245 char_u *new_cmdline;
4246 char_u *program;
4247 char_u *pos;
4248 char_u *ptr;
4249 int len;
4250 int i;
4251
4252 /*
4253 * Don't do it when ":vimgrep" is used for ":grep".
4254 */
Bram Moolenaara37420f2006-02-04 22:37:47 +00004255 if ((eap->cmdidx == CMD_make || eap->cmdidx == CMD_lmake
4256 || eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4257 || eap->cmdidx == CMD_grepadd
4258 || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004259 && !grep_internal(eap->cmdidx))
4260 {
Bram Moolenaara37420f2006-02-04 22:37:47 +00004261 if (eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4262 || eap->cmdidx == CMD_grepadd || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004263 {
4264 if (*curbuf->b_p_gp == NUL)
4265 program = p_gp;
4266 else
4267 program = curbuf->b_p_gp;
4268 }
4269 else
4270 {
4271 if (*curbuf->b_p_mp == NUL)
4272 program = p_mp;
4273 else
4274 program = curbuf->b_p_mp;
4275 }
4276
4277 p = skipwhite(p);
4278
4279 if ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4280 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004281 // replace $* by given arguments
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004282 i = 1;
4283 while ((pos = (char_u *)strstr((char *)pos + 2, "$*")) != NULL)
4284 ++i;
4285 len = (int)STRLEN(p);
Bram Moolenaar51e14382019-05-25 20:21:28 +02004286 new_cmdline = alloc(STRLEN(program) + i * (len - 2) + 1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004287 if (new_cmdline == NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004288 return NULL; // out of memory
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004289 ptr = new_cmdline;
4290 while ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4291 {
4292 i = (int)(pos - program);
4293 STRNCPY(ptr, program, i);
4294 STRCPY(ptr += i, p);
4295 ptr += len;
4296 program = pos + 2;
4297 }
4298 STRCPY(ptr, program);
4299 }
4300 else
4301 {
Bram Moolenaar51e14382019-05-25 20:21:28 +02004302 new_cmdline = alloc(STRLEN(program) + STRLEN(p) + 2);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004303 if (new_cmdline == NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004304 return NULL; // out of memory
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004305 STRCPY(new_cmdline, program);
4306 STRCAT(new_cmdline, " ");
4307 STRCAT(new_cmdline, p);
4308 }
4309 msg_make(p);
4310
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004311 // 'eap->cmd' is not set here, because it is not used at CMD_make
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004312 vim_free(*cmdlinep);
4313 *cmdlinep = new_cmdline;
4314 p = new_cmdline;
4315 }
4316 return p;
4317}
Bram Moolenaar748bf032005-02-02 23:04:36 +00004318#endif
4319
Bram Moolenaar071d4272004-06-13 20:20:40 +00004320/*
4321 * Expand file name in Ex command argument.
Bram Moolenaar0abb4272019-06-15 16:06:00 +02004322 * When an error is detected, "errormsgp" is set to a non-NULL pointer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004323 * Return FAIL for failure, OK otherwise.
4324 */
4325 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004326expand_filename(
4327 exarg_T *eap,
4328 char_u **cmdlinep,
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004329 char **errormsgp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004330{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004331 int has_wildcards; // need to expand wildcards
Bram Moolenaar071d4272004-06-13 20:20:40 +00004332 char_u *repl;
4333 int srclen;
4334 char_u *p;
4335 int n;
Bram Moolenaar63b92542007-03-27 14:57:09 +00004336 int escaped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004337
Bram Moolenaar748bf032005-02-02 23:04:36 +00004338#ifdef FEAT_QUICKFIX
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004339 // Skip a regexp pattern for ":vimgrep[add] pat file..."
Bram Moolenaar748bf032005-02-02 23:04:36 +00004340 p = skip_grep_pat(eap);
4341#else
4342 p = eap->arg;
4343#endif
4344
Bram Moolenaar071d4272004-06-13 20:20:40 +00004345 /*
4346 * Decide to expand wildcards *before* replacing '%', '#', etc. If
4347 * the file name contains a wildcard it should not cause expanding.
4348 * (it will be expanded anyway if there is a wildcard before replacing).
4349 */
Bram Moolenaar748bf032005-02-02 23:04:36 +00004350 has_wildcards = mch_has_wildcard(p);
4351 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004352 {
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004353#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004354 // Skip over `=expr`, wildcards in it are not expanded.
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004355 if (p[0] == '`' && p[1] == '=')
4356 {
4357 p += 2;
4358 (void)skip_expr(&p);
4359 if (*p == '`')
4360 ++p;
4361 continue;
4362 }
4363#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004364 /*
4365 * Quick check if this cannot be the start of a special string.
4366 * Also removes backslash before '%', '#' and '<'.
4367 */
4368 if (vim_strchr((char_u *)"%#<", *p) == NULL)
4369 {
4370 ++p;
4371 continue;
4372 }
4373
4374 /*
4375 * Try to find a match at this position.
4376 */
Bram Moolenaar63b92542007-03-27 14:57:09 +00004377 repl = eval_vars(p, eap->arg, &srclen, &(eap->do_ecmd_lnum),
4378 errormsgp, &escaped);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004379 if (*errormsgp != NULL) // error detected
Bram Moolenaar071d4272004-06-13 20:20:40 +00004380 return FAIL;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004381 if (repl == NULL) // no match found
Bram Moolenaar071d4272004-06-13 20:20:40 +00004382 {
4383 p += srclen;
4384 continue;
4385 }
4386
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004387 // Wildcards won't be expanded below, the replacement is taken
4388 // literally. But do expand "~/file", "~user/file" and "$HOME/file".
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00004389 if (vim_strchr(repl, '$') != NULL || vim_strchr(repl, '~') != NULL)
4390 {
4391 char_u *l = repl;
4392
4393 repl = expand_env_save(repl);
4394 vim_free(l);
4395 }
4396
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004397 // Need to escape white space et al. with a backslash.
4398 // Don't do this for:
4399 // - replacement that already has been escaped: "##"
4400 // - shell commands (may have to use quotes instead).
4401 // - non-unix systems when there is a single argument (spaces don't
4402 // separate arguments then).
Bram Moolenaar071d4272004-06-13 20:20:40 +00004403 if (!eap->usefilter
Bram Moolenaar63b92542007-03-27 14:57:09 +00004404 && !escaped
Bram Moolenaar071d4272004-06-13 20:20:40 +00004405 && eap->cmdidx != CMD_bang
Bram Moolenaar071d4272004-06-13 20:20:40 +00004406 && eap->cmdidx != CMD_grep
4407 && eap->cmdidx != CMD_grepadd
Bram Moolenaarbf15b8d2017-06-04 20:43:48 +02004408 && eap->cmdidx != CMD_hardcopy
Bram Moolenaar67883b42017-07-27 22:57:00 +02004409 && eap->cmdidx != CMD_lgrep
4410 && eap->cmdidx != CMD_lgrepadd
4411 && eap->cmdidx != CMD_lmake
4412 && eap->cmdidx != CMD_make
4413 && eap->cmdidx != CMD_terminal
Bram Moolenaar071d4272004-06-13 20:20:40 +00004414#ifndef UNIX
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004415 && !(eap->argt & EX_NOSPC)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004416#endif
4417 )
4418 {
4419 char_u *l;
4420#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004421 // Don't escape a backslash here, because rem_backslash() doesn't
4422 // remove it later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004423 static char_u *nobslash = (char_u *)" \t\"|";
4424# define ESCAPE_CHARS nobslash
4425#else
4426# define ESCAPE_CHARS escape_chars
4427#endif
4428
4429 for (l = repl; *l; ++l)
4430 if (vim_strchr(ESCAPE_CHARS, *l) != NULL)
4431 {
4432 l = vim_strsave_escaped(repl, ESCAPE_CHARS);
4433 if (l != NULL)
4434 {
4435 vim_free(repl);
4436 repl = l;
4437 }
4438 break;
4439 }
4440 }
4441
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004442 // For a shell command a '!' must be escaped.
Bram Moolenaar67883b42017-07-27 22:57:00 +02004443 if ((eap->usefilter || eap->cmdidx == CMD_bang
4444 || eap->cmdidx == CMD_terminal)
Bram Moolenaar31b7d382014-04-01 18:54:48 +02004445 && vim_strpbrk(repl, (char_u *)"!") != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004446 {
4447 char_u *l;
4448
Bram Moolenaar31b7d382014-04-01 18:54:48 +02004449 l = vim_strsave_escaped(repl, (char_u *)"!");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004450 if (l != NULL)
4451 {
4452 vim_free(repl);
4453 repl = l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004454 }
4455 }
4456
4457 p = repl_cmdline(eap, p, srclen, repl, cmdlinep);
4458 vim_free(repl);
4459 if (p == NULL)
4460 return FAIL;
4461 }
4462
4463 /*
4464 * One file argument: Expand wildcards.
4465 * Don't do this with ":r !command" or ":w !command".
4466 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004467 if ((eap->argt & EX_NOSPC) && !eap->usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004468 {
4469 /*
4470 * May do this twice:
4471 * 1. Replace environment variables.
4472 * 2. Replace any other wildcards, remove backslashes.
4473 */
4474 for (n = 1; n <= 2; ++n)
4475 {
4476 if (n == 2)
4477 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004478 /*
4479 * Halve the number of backslashes (this is Vi compatible).
4480 * For Unix and OS/2, when wildcards are expanded, this is
4481 * done by ExpandOne() below.
4482 */
Bram Moolenaare7fedb62015-12-31 19:07:19 +01004483#if defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004484 if (!has_wildcards)
4485#endif
4486 backslash_halve(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004487 }
4488
4489 if (has_wildcards)
4490 {
4491 if (n == 1)
4492 {
4493 /*
4494 * First loop: May expand environment variables. This
4495 * can be done much faster with expand_env() than with
4496 * something else (e.g., calling a shell).
4497 * After expanding environment variables, check again
4498 * if there are still wildcards present.
4499 */
4500 if (vim_strchr(eap->arg, '$') != NULL
4501 || vim_strchr(eap->arg, '~') != NULL)
4502 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +00004503 expand_env_esc(eap->arg, NameBuff, MAXPATHL,
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00004504 TRUE, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004505 has_wildcards = mch_has_wildcard(NameBuff);
4506 p = NameBuff;
4507 }
4508 else
4509 p = NULL;
4510 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004511 else // n == 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00004512 {
4513 expand_T xpc;
Bram Moolenaarb40c2572019-10-19 21:01:05 +02004514 int options = WILD_LIST_NOTFOUND
4515 | WILD_NOERROR | WILD_ADD_SLASH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004516
4517 ExpandInit(&xpc);
4518 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar94950a92010-12-02 16:01:29 +01004519 if (p_wic)
4520 options += WILD_ICASE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004521 p = ExpandOne(&xpc, eap->arg, NULL,
Bram Moolenaar94950a92010-12-02 16:01:29 +01004522 options, WILD_EXPAND_FREE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004523 if (p == NULL)
4524 return FAIL;
4525 }
4526 if (p != NULL)
4527 {
4528 (void)repl_cmdline(eap, eap->arg, (int)STRLEN(eap->arg),
4529 p, cmdlinep);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004530 if (n == 2) // p came from ExpandOne()
Bram Moolenaar071d4272004-06-13 20:20:40 +00004531 vim_free(p);
4532 }
4533 }
4534 }
4535 }
4536 return OK;
4537}
4538
4539/*
4540 * Replace part of the command line, keeping eap->cmd, eap->arg and
4541 * eap->nextcmd correct.
4542 * "src" points to the part that is to be replaced, of length "srclen".
4543 * "repl" is the replacement string.
4544 * Returns a pointer to the character after the replaced string.
4545 * Returns NULL for failure.
4546 */
4547 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004548repl_cmdline(
4549 exarg_T *eap,
4550 char_u *src,
4551 int srclen,
4552 char_u *repl,
4553 char_u **cmdlinep)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004554{
4555 int len;
4556 int i;
4557 char_u *new_cmdline;
4558
4559 /*
4560 * The new command line is build in new_cmdline[].
4561 * First allocate it.
4562 * Careful: a "+cmd" argument may have been NUL terminated.
4563 */
4564 len = (int)STRLEN(repl);
4565 i = (int)(src - *cmdlinep) + (int)STRLEN(src + srclen) + len + 3;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004566 if (eap->nextcmd != NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004567 i += (int)STRLEN(eap->nextcmd);// add space for next command
Bram Moolenaar964b3742019-05-24 18:54:09 +02004568 if ((new_cmdline = alloc(i)) == NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004569 return NULL; // out of memory!
Bram Moolenaar071d4272004-06-13 20:20:40 +00004570
4571 /*
4572 * Copy the stuff before the expanded part.
4573 * Copy the expanded stuff.
4574 * Copy what came after the expanded part.
4575 * Copy the next commands, if there are any.
4576 */
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004577 i = (int)(src - *cmdlinep); // length of part before match
Bram Moolenaar071d4272004-06-13 20:20:40 +00004578 mch_memmove(new_cmdline, *cmdlinep, (size_t)i);
Bram Moolenaara3ffd9c2005-07-21 21:03:15 +00004579
Bram Moolenaar071d4272004-06-13 20:20:40 +00004580 mch_memmove(new_cmdline + i, repl, (size_t)len);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004581 i += len; // remember the end of the string
Bram Moolenaar071d4272004-06-13 20:20:40 +00004582 STRCPY(new_cmdline + i, src + srclen);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004583 src = new_cmdline + i; // remember where to continue
Bram Moolenaar071d4272004-06-13 20:20:40 +00004584
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004585 if (eap->nextcmd != NULL) // append next command
Bram Moolenaar071d4272004-06-13 20:20:40 +00004586 {
4587 i = (int)STRLEN(new_cmdline) + 1;
4588 STRCPY(new_cmdline + i, eap->nextcmd);
4589 eap->nextcmd = new_cmdline + i;
4590 }
4591 eap->cmd = new_cmdline + (eap->cmd - *cmdlinep);
4592 eap->arg = new_cmdline + (eap->arg - *cmdlinep);
4593 if (eap->do_ecmd_cmd != NULL && eap->do_ecmd_cmd != dollar_command)
4594 eap->do_ecmd_cmd = new_cmdline + (eap->do_ecmd_cmd - *cmdlinep);
4595 vim_free(*cmdlinep);
4596 *cmdlinep = new_cmdline;
4597
4598 return src;
4599}
4600
4601/*
4602 * Check for '|' to separate commands and '"' to start comments.
4603 */
4604 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004605separate_nextcmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004606{
4607 char_u *p;
4608
Bram Moolenaar86b68352004-12-27 21:59:20 +00004609#ifdef FEAT_QUICKFIX
Bram Moolenaar748bf032005-02-02 23:04:36 +00004610 p = skip_grep_pat(eap);
4611#else
4612 p = eap->arg;
Bram Moolenaar86b68352004-12-27 21:59:20 +00004613#endif
4614
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004615 for ( ; *p; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004616 {
4617 if (*p == Ctrl_V)
4618 {
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004619 if (eap->argt & (EX_CTRLV | EX_XFILE))
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004620 ++p; // skip CTRL-V and next char
Bram Moolenaar071d4272004-06-13 20:20:40 +00004621 else
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004622 // remove CTRL-V and skip next char
Bram Moolenaara7241f52008-06-24 20:39:31 +00004623 STRMOVE(p, p + 1);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004624 if (*p == NUL) // stop at NUL after CTRL-V
Bram Moolenaar071d4272004-06-13 20:20:40 +00004625 break;
4626 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004627
4628#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004629 // Skip over `=expr` when wildcards are expanded.
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004630 else if (p[0] == '`' && p[1] == '=' && (eap->argt & EX_XFILE))
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004631 {
4632 p += 2;
4633 (void)skip_expr(&p);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004634 }
4635#endif
4636
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004637 // Check for '"': start of comment or '|': next command
4638 // :@" and :*" do not start a comment!
4639 // :redir @" doesn't either.
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02004640 else if ((*p == '"'
Bram Moolenaara26b9702020-04-18 19:53:28 +02004641#ifdef FEAT_EVAL
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02004642 && !in_vim9script()
4643#endif
4644 && !(eap->argt & EX_NOTRLCOM)
4645 && ((eap->cmdidx != CMD_at && eap->cmdidx != CMD_star)
4646 || p != eap->arg)
4647 && (eap->cmdidx != CMD_redir
4648 || p != eap->arg + 1 || p[-1] != '@'))
4649#ifdef FEAT_EVAL
4650 || (*p == '#'
4651 && in_vim9script()
Bram Moolenaarb8a92962020-08-20 18:02:47 +02004652 && !(eap->argt & EX_NOTRLCOM)
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02004653 && p[1] != '{'
4654 && p > eap->cmd && VIM_ISWHITE(p[-1]))
Bram Moolenaara26b9702020-04-18 19:53:28 +02004655#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004656 || *p == '|' || *p == '\n')
4657 {
4658 /*
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004659 * We remove the '\' before the '|', unless EX_CTRLV is used
Bram Moolenaar071d4272004-06-13 20:20:40 +00004660 * AND 'b' is present in 'cpoptions'.
4661 */
4662 if ((vim_strchr(p_cpo, CPO_BAR) == NULL
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004663 || !(eap->argt & EX_CTRLV)) && *(p - 1) == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004664 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004665 STRMOVE(p - 1, p); // remove the '\'
Bram Moolenaar071d4272004-06-13 20:20:40 +00004666 --p;
4667 }
4668 else
4669 {
4670 eap->nextcmd = check_nextcmd(p);
4671 *p = NUL;
4672 break;
4673 }
4674 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004675 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00004676
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004677 if (!(eap->argt & EX_NOTRLCOM)) // remove trailing spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00004678 del_trailing_spaces(eap->arg);
4679}
4680
4681/*
4682 * get + command from ex argument
4683 */
4684 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004685getargcmd(char_u **argp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004686{
4687 char_u *arg = *argp;
4688 char_u *command = NULL;
4689
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004690 if (*arg == '+') // +[command]
Bram Moolenaar071d4272004-06-13 20:20:40 +00004691 {
4692 ++arg;
Bram Moolenaar3e451592014-04-02 14:22:05 +02004693 if (vim_isspace(*arg) || *arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004694 command = dollar_command;
4695 else
4696 {
4697 command = arg;
4698 arg = skip_cmd_arg(command, TRUE);
4699 if (*arg != NUL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004700 *arg++ = NUL; // terminate command with NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004701 }
4702
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004703 arg = skipwhite(arg); // skip over spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00004704 *argp = arg;
4705 }
4706 return command;
4707}
4708
4709/*
4710 * Find end of "+command" argument. Skip over "\ " and "\\".
4711 */
Bram Moolenaard0190392019-08-23 21:17:35 +02004712 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004713skip_cmd_arg(
4714 char_u *p,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004715 int rembs) // TRUE to halve the number of backslashes
Bram Moolenaar071d4272004-06-13 20:20:40 +00004716{
4717 while (*p && !vim_isspace(*p))
4718 {
4719 if (*p == '\\' && p[1] != NUL)
4720 {
4721 if (rembs)
Bram Moolenaara7241f52008-06-24 20:39:31 +00004722 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004723 else
4724 ++p;
4725 }
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004726 MB_PTR_ADV(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004727 }
4728 return p;
4729}
4730
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004731 int
4732get_bad_opt(char_u *p, exarg_T *eap)
4733{
4734 if (STRICMP(p, "keep") == 0)
4735 eap->bad_char = BAD_KEEP;
4736 else if (STRICMP(p, "drop") == 0)
4737 eap->bad_char = BAD_DROP;
4738 else if (MB_BYTE2LEN(*p) == 1 && p[1] == NUL)
4739 eap->bad_char = *p;
Bram Moolenaar75808492018-06-12 12:39:41 +02004740 else
4741 return FAIL;
4742 return OK;
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004743}
4744
Bram Moolenaar071d4272004-06-13 20:20:40 +00004745/*
4746 * Get "++opt=arg" argument.
4747 * Return FAIL or OK.
4748 */
4749 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004750getargopt(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004751{
4752 char_u *arg = eap->arg + 2;
4753 int *pp = NULL;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02004754 int bad_char_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004755 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004756
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004757 // ":edit ++[no]bin[ary] file"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004758 if (STRNCMP(arg, "bin", 3) == 0 || STRNCMP(arg, "nobin", 5) == 0)
4759 {
4760 if (*arg == 'n')
4761 {
4762 arg += 2;
4763 eap->force_bin = FORCE_NOBIN;
4764 }
4765 else
4766 eap->force_bin = FORCE_BIN;
4767 if (!checkforcmd(&arg, "binary", 3))
4768 return FAIL;
4769 eap->arg = skipwhite(arg);
4770 return OK;
4771 }
4772
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004773 // ":read ++edit file"
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004774 if (STRNCMP(arg, "edit", 4) == 0)
4775 {
4776 eap->read_edit = TRUE;
4777 eap->arg = skipwhite(arg + 4);
4778 return OK;
4779 }
4780
Bram Moolenaar071d4272004-06-13 20:20:40 +00004781 if (STRNCMP(arg, "ff", 2) == 0)
4782 {
4783 arg += 2;
4784 pp = &eap->force_ff;
4785 }
4786 else if (STRNCMP(arg, "fileformat", 10) == 0)
4787 {
4788 arg += 10;
4789 pp = &eap->force_ff;
4790 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004791 else if (STRNCMP(arg, "enc", 3) == 0)
4792 {
Bram Moolenaarb38e9ab2011-12-14 14:49:45 +01004793 if (STRNCMP(arg, "encoding", 8) == 0)
4794 arg += 8;
4795 else
4796 arg += 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004797 pp = &eap->force_enc;
4798 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004799 else if (STRNCMP(arg, "bad", 3) == 0)
4800 {
4801 arg += 3;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02004802 pp = &bad_char_idx;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004803 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004804
4805 if (pp == NULL || *arg != '=')
4806 return FAIL;
4807
4808 ++arg;
4809 *pp = (int)(arg - eap->cmd);
4810 arg = skip_cmd_arg(arg, FALSE);
4811 eap->arg = skipwhite(arg);
4812 *arg = NUL;
4813
Bram Moolenaar071d4272004-06-13 20:20:40 +00004814 if (pp == &eap->force_ff)
4815 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004816 if (check_ff_value(eap->cmd + eap->force_ff) == FAIL)
4817 return FAIL;
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004818 eap->force_ff = eap->cmd[eap->force_ff];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004819 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004820 else if (pp == &eap->force_enc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004821 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004822 // Make 'fileencoding' lower case.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004823 for (p = eap->cmd + eap->force_enc; *p != NUL; ++p)
4824 *p = TOLOWER_ASC(*p);
4825 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004826 else
4827 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004828 // Check ++bad= argument. Must be a single-byte character, "keep" or
4829 // "drop".
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004830 if (get_bad_opt(eap->cmd + bad_char_idx, eap) == FAIL)
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004831 return FAIL;
4832 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004833
4834 return OK;
4835}
4836
Bram Moolenaar071d4272004-06-13 20:20:40 +00004837 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004838ex_autocmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004839{
4840 /*
Bram Moolenaar26d4b892018-07-04 22:26:28 +02004841 * Disallow autocommands from .exrc and .vimrc in current
Bram Moolenaar071d4272004-06-13 20:20:40 +00004842 * directory for security reasons.
4843 */
4844 if (secure)
4845 {
4846 secure = 2;
Bram Moolenaar8930caa2020-07-23 16:37:03 +02004847 eap->errmsg = _(e_curdir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004848 }
4849 else if (eap->cmdidx == CMD_autocmd)
4850 do_autocmd(eap->arg, eap->forceit);
4851 else
4852 do_augroup(eap->arg, eap->forceit);
4853}
4854
4855/*
4856 * ":doautocmd": Apply the automatic commands to the current buffer.
4857 */
4858 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004859ex_doautocmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004860{
Bram Moolenaar60542ac2012-02-12 20:14:01 +01004861 char_u *arg = eap->arg;
4862 int call_do_modelines = check_nomodeline(&arg);
Bram Moolenaar1610d052016-06-09 22:53:01 +02004863 int did_aucmd;
Bram Moolenaar60542ac2012-02-12 20:14:01 +01004864
Bram Moolenaar1610d052016-06-09 22:53:01 +02004865 (void)do_doautocmd(arg, TRUE, &did_aucmd);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004866 // Only when there is no <nomodeline>.
Bram Moolenaar1610d052016-06-09 22:53:01 +02004867 if (call_do_modelines && did_aucmd)
Bram Moolenaar60542ac2012-02-12 20:14:01 +01004868 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004869}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004870
Bram Moolenaar071d4272004-06-13 20:20:40 +00004871/*
4872 * :[N]bunload[!] [N] [bufname] unload buffer
4873 * :[N]bdelete[!] [N] [bufname] delete buffer from buffer list
4874 * :[N]bwipeout[!] [N] [bufname] delete buffer really
4875 */
4876 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004877ex_bunload(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004878{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01004879 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02004880 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004881 eap->errmsg = do_bufdel(
4882 eap->cmdidx == CMD_bdelete ? DOBUF_DEL
4883 : eap->cmdidx == CMD_bwipeout ? DOBUF_WIPE
4884 : DOBUF_UNLOAD, eap->arg,
4885 eap->addr_count, (int)eap->line1, (int)eap->line2, eap->forceit);
4886}
4887
4888/*
4889 * :[N]buffer [N] to buffer N
4890 * :[N]sbuffer [N] to buffer N
4891 */
4892 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004893ex_buffer(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004894{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01004895 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02004896 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004897 if (*eap->arg)
Bram Moolenaar8930caa2020-07-23 16:37:03 +02004898 eap->errmsg = ex_errmsg(e_trailing_arg, eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004899 else
4900 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004901 if (eap->addr_count == 0) // default is current buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00004902 goto_buffer(eap, DOBUF_CURRENT, FORWARD, 0);
4903 else
4904 goto_buffer(eap, DOBUF_FIRST, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02004905 if (eap->do_ecmd_cmd != NULL)
4906 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004907 }
4908}
4909
4910/*
4911 * :[N]bmodified [N] to next mod. buffer
4912 * :[N]sbmodified [N] to next mod. buffer
4913 */
4914 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004915ex_bmodified(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004916{
4917 goto_buffer(eap, DOBUF_MOD, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02004918 if (eap->do_ecmd_cmd != NULL)
4919 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004920}
4921
4922/*
4923 * :[N]bnext [N] to next buffer
4924 * :[N]sbnext [N] split and to next buffer
4925 */
4926 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004927ex_bnext(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004928{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01004929 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar3029bcc2020-01-18 15:06:19 +01004930 return;
4931
Bram Moolenaar071d4272004-06-13 20:20:40 +00004932 goto_buffer(eap, DOBUF_CURRENT, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02004933 if (eap->do_ecmd_cmd != NULL)
4934 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004935}
4936
4937/*
4938 * :[N]bNext [N] to previous buffer
4939 * :[N]bprevious [N] to previous buffer
4940 * :[N]sbNext [N] split and to previous buffer
4941 * :[N]sbprevious [N] split and to previous buffer
4942 */
4943 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004944ex_bprevious(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004945{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01004946 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar3029bcc2020-01-18 15:06:19 +01004947 return;
4948
Bram Moolenaar071d4272004-06-13 20:20:40 +00004949 goto_buffer(eap, DOBUF_CURRENT, BACKWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02004950 if (eap->do_ecmd_cmd != NULL)
4951 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004952}
4953
4954/*
4955 * :brewind to first buffer
4956 * :bfirst to first buffer
4957 * :sbrewind split and to first buffer
4958 * :sbfirst split and to first buffer
4959 */
4960 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004961ex_brewind(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004962{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01004963 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar3029bcc2020-01-18 15:06:19 +01004964 return;
4965
Bram Moolenaar071d4272004-06-13 20:20:40 +00004966 goto_buffer(eap, DOBUF_FIRST, FORWARD, 0);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02004967 if (eap->do_ecmd_cmd != NULL)
4968 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004969}
4970
4971/*
4972 * :blast to last buffer
4973 * :sblast split and to last buffer
4974 */
4975 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004976ex_blast(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004977{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01004978 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar3029bcc2020-01-18 15:06:19 +01004979 return;
4980
Bram Moolenaar071d4272004-06-13 20:20:40 +00004981 goto_buffer(eap, DOBUF_LAST, BACKWARD, 0);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02004982 if (eap->do_ecmd_cmd != NULL)
4983 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004984}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004985
Bram Moolenaar7a092242020-04-16 22:10:49 +02004986/*
4987 * Check if "c" ends an Ex command.
Bram Moolenaara494f562020-04-18 17:45:38 +02004988 * In Vim9 script does not check for white space before # or #{.
Bram Moolenaar7a092242020-04-16 22:10:49 +02004989 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004990 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004991ends_excmd(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004992{
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02004993 int comment_char = '"';
4994
Bram Moolenaarfbda69b2020-04-13 15:06:53 +02004995#ifdef FEAT_EVAL
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02004996 if (in_vim9script())
4997 comment_char = '#';
Bram Moolenaar7a092242020-04-16 22:10:49 +02004998#endif
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02004999 return (c == NUL || c == '|' || c == comment_char || c == '\n');
Bram Moolenaar7a092242020-04-16 22:10:49 +02005000}
5001
5002/*
5003 * Like ends_excmd() but checks that a # in Vim9 script either has "cmd" equal
5004 * to "cmd_start" or has a white space character before it.
5005 */
5006 int
Bram Moolenaar21cfe502020-04-16 23:01:50 +02005007ends_excmd2(char_u *cmd_start UNUSED, char_u *cmd)
Bram Moolenaar7a092242020-04-16 22:10:49 +02005008{
5009 int c = *cmd;
5010
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005011 if (c == NUL || c == '|' || c == '\n')
5012 return TRUE;
Bram Moolenaar7a092242020-04-16 22:10:49 +02005013#ifdef FEAT_EVAL
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005014 if (in_vim9script())
5015 return c == '#' && cmd[1] != '{'
5016 && (cmd == cmd_start || VIM_ISWHITE(cmd[-1]));
Bram Moolenaarfbda69b2020-04-13 15:06:53 +02005017#endif
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005018 return c == '"';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005019}
5020
5021#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA) || defined(FEAT_EVAL) \
5022 || defined(PROTO)
5023/*
5024 * Return the next command, after the first '|' or '\n'.
5025 * Return NULL if not found.
5026 */
5027 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005028find_nextcmd(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005029{
5030 while (*p != '|' && *p != '\n')
5031 {
5032 if (*p == NUL)
5033 return NULL;
5034 ++p;
5035 }
5036 return (p + 1);
5037}
5038#endif
5039
5040/*
Bram Moolenaar2256c992016-11-15 21:17:07 +01005041 * Check if *p is a separator between Ex commands, skipping over white space.
5042 * Return NULL if it isn't, the following character if it is.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005043 */
5044 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005045check_nextcmd(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005046{
Bram Moolenaar2256c992016-11-15 21:17:07 +01005047 char_u *s = skipwhite(p);
5048
5049 if (*s == '|' || *s == '\n')
5050 return (s + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005051 else
5052 return NULL;
5053}
5054
5055/*
5056 * - if there are more files to edit
5057 * - and this is the last window
5058 * - and forceit not used
5059 * - and not repeated twice on a row
5060 * return FAIL and give error message if 'message' TRUE
5061 * return OK otherwise
5062 */
5063 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005064check_more(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005065 int message, // when FALSE check only, no messages
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005066 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005067{
5068 int n = ARGCOUNT - curwin->w_arg_idx - 1;
5069
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005070 if (!forceit && only_one_window()
Bram Moolenaar7b221172020-08-17 19:34:10 +02005071 && ARGCOUNT > 1 && !arg_had_last && n > 0 && quitmore == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005072 {
5073 if (message)
5074 {
5075#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
5076 if ((p_confirm || cmdmod.confirm) && curbuf->b_fname != NULL)
5077 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02005078 char_u buff[DIALOG_MSG_SIZE];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005079
Bram Moolenaarda6e8912018-08-21 15:12:14 +02005080 vim_snprintf((char *)buff, DIALOG_MSG_SIZE,
5081 NGETTEXT("%d more file to edit. Quit anyway?",
5082 "%d more files to edit. Quit anyway?", n), n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005083 if (vim_dialog_yesno(VIM_QUESTION, NULL, buff, 1) == VIM_YES)
5084 return OK;
5085 return FAIL;
5086 }
5087#endif
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01005088 semsg(NGETTEXT("E173: %d more file to edit",
5089 "E173: %d more files to edit", n), n);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005090 quitmore = 2; // next try to quit is allowed
Bram Moolenaar071d4272004-06-13 20:20:40 +00005091 }
5092 return FAIL;
5093 }
5094 return OK;
5095}
5096
Bram Moolenaar071d4272004-06-13 20:20:40 +00005097/*
5098 * Function given to ExpandGeneric() to obtain the list of command names.
5099 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005100 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005101get_command_name(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005102{
5103 if (idx >= (int)CMD_SIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005104 return get_user_command_name(idx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005105 return cmdnames[idx].cmd_name;
5106}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005107
Bram Moolenaar071d4272004-06-13 20:20:40 +00005108 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005109ex_colorscheme(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005110{
Bram Moolenaare6850792010-05-14 15:28:44 +02005111 if (*eap->arg == NUL)
5112 {
5113#ifdef FEAT_EVAL
5114 char_u *expr = vim_strsave((char_u *)"g:colors_name");
5115 char_u *p = NULL;
5116
5117 if (expr != NULL)
5118 {
5119 ++emsg_off;
Bram Moolenaarb171fb12020-06-24 20:34:03 +02005120 p = eval_to_string(expr, FALSE);
Bram Moolenaare6850792010-05-14 15:28:44 +02005121 --emsg_off;
5122 vim_free(expr);
5123 }
5124 if (p != NULL)
5125 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01005126 msg((char *)p);
Bram Moolenaare6850792010-05-14 15:28:44 +02005127 vim_free(p);
5128 }
5129 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01005130 msg("default");
Bram Moolenaare6850792010-05-14 15:28:44 +02005131#else
Bram Moolenaar32526b32019-01-19 17:43:09 +01005132 msg(_("unknown"));
Bram Moolenaare6850792010-05-14 15:28:44 +02005133#endif
5134 }
5135 else if (load_colors(eap->arg) == FAIL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005136 semsg(_("E185: Cannot find color scheme '%s'"), eap->arg);
Bram Moolenaarf58d81a2019-01-28 20:19:05 +01005137
5138#ifdef FEAT_VTP
5139 else if (has_vtp_working())
5140 {
5141 // background color change requires clear + redraw
5142 update_screen(CLEAR);
5143 redrawcmd();
5144 }
5145#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005146}
5147
5148 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005149ex_highlight(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005150{
5151 if (*eap->arg == NUL && eap->cmd[2] == '!')
Bram Moolenaar32526b32019-01-19 17:43:09 +01005152 msg(_("Greetings, Vim user!"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005153 do_highlight(eap->arg, eap->forceit, FALSE);
5154}
5155
5156
5157/*
5158 * Call this function if we thought we were going to exit, but we won't
5159 * (because of an error). May need to restore the terminal mode.
5160 */
5161 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005162not_exiting(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005163{
5164 exiting = FALSE;
5165 settmode(TMODE_RAW);
5166}
5167
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005168 static int
5169before_quit_autocmds(win_T *wp, int quit_all, int forceit)
5170{
5171 apply_autocmds(EVENT_QUITPRE, NULL, NULL, FALSE, wp->w_buffer);
5172
Bram Moolenaar34ba06b2019-10-20 22:27:10 +02005173 // Bail out when autocommands closed the window.
5174 // Refuse to quit when the buffer in the last window is being closed (can
5175 // only happen in autocommands).
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005176 if (!win_valid(wp)
5177 || curbuf_locked()
5178 || (wp->w_buffer->b_nwindows == 1 && wp->w_buffer->b_locked > 0))
5179 return TRUE;
5180
5181 if (quit_all || (check_more(FALSE, forceit) == OK && only_one_window()))
5182 {
5183 apply_autocmds(EVENT_EXITPRE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar34ba06b2019-10-20 22:27:10 +02005184 // Refuse to quit when locked or when the window was closed or the
5185 // buffer in the last window is being closed (can only happen in
5186 // autocommands).
5187 if (!win_valid(wp) || curbuf_locked()
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005188 || (curbuf->b_nwindows == 1 && curbuf->b_locked > 0))
5189 return TRUE;
5190 }
5191
5192 return FALSE;
5193}
5194
Bram Moolenaar071d4272004-06-13 20:20:40 +00005195/*
Bram Moolenaarf240e182014-11-27 18:33:02 +01005196 * ":quit": quit current window, quit Vim if the last window is closed.
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005197 * ":{nr}quit": quit window {nr}
Bram Moolenaar4d14bac2019-10-20 21:15:15 +02005198 * Also used when closing a terminal window that's the last one.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005199 */
Bram Moolenaar4d14bac2019-10-20 21:15:15 +02005200 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005201ex_quit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005202{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005203 win_T *wp;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005204
Bram Moolenaar071d4272004-06-13 20:20:40 +00005205#ifdef FEAT_CMDWIN
5206 if (cmdwin_type != 0)
5207 {
5208 cmdwin_result = Ctrl_C;
5209 return;
5210 }
5211#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005212 // Don't quit while editing the command line.
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005213 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005214 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005215 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005216 return;
5217 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005218 if (eap->addr_count > 0)
5219 {
Bram Moolenaarf240e182014-11-27 18:33:02 +01005220 int wnr = eap->line2;
5221
5222 for (wp = firstwin; wp->w_next != NULL; wp = wp->w_next)
5223 if (--wnr <= 0)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005224 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005225 }
5226 else
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005227 wp = curwin;
Bram Moolenaarf240e182014-11-27 18:33:02 +01005228
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005229 // Refuse to quit when locked.
Bram Moolenaar87ffb5c2017-10-19 12:37:42 +02005230 if (curbuf_locked())
5231 return;
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005232
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005233 // Trigger QuitPre and maybe ExitPre
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005234 if (before_quit_autocmds(wp, FALSE, eap->forceit))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005235 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005236
5237#ifdef FEAT_NETBEANS_INTG
5238 netbeansForcedQuit = eap->forceit;
5239#endif
5240
5241 /*
5242 * If there are more files or windows we won't exit.
5243 */
5244 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
5245 exiting = TRUE;
Bram Moolenaarff930ca2017-10-19 17:12:10 +02005246 if ((!buf_hide(wp->w_buffer)
5247 && check_changed(wp->w_buffer, (p_awa ? CCGD_AW : 0)
Bram Moolenaar45d3b142013-11-09 03:31:51 +01005248 | (eap->forceit ? CCGD_FORCEIT : 0)
5249 | CCGD_EXCMD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005250 || check_more(TRUE, eap->forceit) == FAIL
Bram Moolenaar027387f2016-01-02 22:25:52 +01005251 || (only_one_window() && check_changed_any(eap->forceit, TRUE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005252 {
5253 not_exiting();
5254 }
5255 else
5256 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005257 // quit last window
5258 // Note: only_one_window() returns true, even so a help window is
5259 // still open. In that case only quit, if no address has been
5260 // specified. Example:
5261 // :h|wincmd w|1q - don't quit
5262 // :h|wincmd w|q - quit
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01005263 if (only_one_window() && (ONE_WINDOW || eap->addr_count == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005264 getout(0);
Bram Moolenaar2c33d7b2017-10-14 16:06:20 +02005265 not_exiting();
Bram Moolenaar4033c552017-09-16 20:54:51 +02005266#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00005267 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005268#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005269 // close window; may free buffer
Bram Moolenaareb44a682017-08-03 22:44:55 +02005270 win_close(wp, !buf_hide(wp->w_buffer) || eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005271 }
5272}
5273
5274/*
5275 * ":cquit".
5276 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005277 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005278ex_cquit(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005279{
Bram Moolenaar1860bde2020-01-06 21:47:21 +01005280 // this does not always pass on the exit code to the Manx compiler. why?
5281 getout(eap->addr_count > 0 ? (int)eap->line2 : EXIT_FAILURE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005282}
5283
5284/*
5285 * ":qall": try to quit all windows
5286 */
5287 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005288ex_quit_all(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005289{
5290# ifdef FEAT_CMDWIN
5291 if (cmdwin_type != 0)
5292 {
5293 if (eap->forceit)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005294 cmdwin_result = K_XF1; // ex_window() takes care of this
Bram Moolenaar071d4272004-06-13 20:20:40 +00005295 else
5296 cmdwin_result = K_XF2;
5297 return;
5298 }
5299# endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005300
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005301 // Don't quit while editing the command line.
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005302 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005303 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005304 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005305 return;
5306 }
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005307
5308 if (before_quit_autocmds(curwin, TRUE, eap->forceit))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005309 return;
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005310
Bram Moolenaar071d4272004-06-13 20:20:40 +00005311 exiting = TRUE;
Bram Moolenaar027387f2016-01-02 22:25:52 +01005312 if (eap->forceit || !check_changed_any(FALSE, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005313 getout(0);
5314 not_exiting();
5315}
5316
Bram Moolenaar071d4272004-06-13 20:20:40 +00005317/*
5318 * ":close": close current window, unless it is the last one
5319 */
5320 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005321ex_close(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005322{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005323 win_T *win;
5324 int winnr = 0;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005325#ifdef FEAT_CMDWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00005326 if (cmdwin_type != 0)
Bram Moolenaar9bd1a7e2011-05-19 14:50:54 +02005327 cmdwin_result = Ctrl_C;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005328 else
Bram Moolenaar4033c552017-09-16 20:54:51 +02005329#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005330 if (!text_locked() && !curbuf_locked())
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005331 {
5332 if (eap->addr_count == 0)
5333 ex_win_close(eap->forceit, curwin, NULL);
Bram Moolenaar6d41c782018-06-06 09:11:12 +02005334 else
5335 {
Bram Moolenaar29323592016-07-24 22:04:11 +02005336 FOR_ALL_WINDOWS(win)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005337 {
5338 winnr++;
5339 if (winnr == eap->line2)
5340 break;
5341 }
5342 if (win == NULL)
5343 win = lastwin;
5344 ex_win_close(eap->forceit, win, NULL);
5345 }
5346 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005347}
5348
Bram Moolenaar4033c552017-09-16 20:54:51 +02005349#ifdef FEAT_QUICKFIX
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005350/*
5351 * ":pclose": Close any preview window.
5352 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005353 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005354ex_pclose(exarg_T *eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005355{
5356 win_T *win;
5357
Bram Moolenaar79648732019-07-18 21:43:07 +02005358 // First close any normal window.
Bram Moolenaar29323592016-07-24 22:04:11 +02005359 FOR_ALL_WINDOWS(win)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005360 if (win->w_p_pvw)
5361 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00005362 ex_win_close(eap->forceit, win, NULL);
Bram Moolenaar79648732019-07-18 21:43:07 +02005363 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005364 }
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01005365# ifdef FEAT_PROP_POPUP
Bram Moolenaar79648732019-07-18 21:43:07 +02005366 // Also when 'previewpopup' is empty, it might have been cleared.
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02005367 popup_close_preview();
Bram Moolenaar79648732019-07-18 21:43:07 +02005368# endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005369}
Bram Moolenaar4033c552017-09-16 20:54:51 +02005370#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005371
Bram Moolenaarf740b292006-02-16 22:11:02 +00005372/*
5373 * Close window "win" and take care of handling closing the last window for a
5374 * modified buffer.
5375 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005376 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005377ex_win_close(
5378 int forceit,
5379 win_T *win,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005380 tabpage_T *tp) // NULL or the tab page "win" is in
Bram Moolenaar071d4272004-06-13 20:20:40 +00005381{
5382 int need_hide;
5383 buf_T *buf = win->w_buffer;
5384
Bram Moolenaarcf844172020-06-26 19:44:06 +02005385 // Never close the autocommand window.
5386 if (win == aucmd_win)
5387 {
5388 emsg(_(e_autocmd_close));
5389 return;
5390 }
5391
Bram Moolenaar071d4272004-06-13 20:20:40 +00005392 need_hide = (bufIsChanged(buf) && buf->b_nwindows <= 1);
Bram Moolenaareb44a682017-08-03 22:44:55 +02005393 if (need_hide && !buf_hide(buf) && !forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005394 {
Bram Moolenaar4033c552017-09-16 20:54:51 +02005395#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005396 if ((p_confirm || cmdmod.confirm) && p_write)
5397 {
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005398 bufref_T bufref;
5399
5400 set_bufref(&bufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005401 dialog_changed(buf, FALSE);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005402 if (bufref_valid(&bufref) && bufIsChanged(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005403 return;
5404 need_hide = FALSE;
5405 }
5406 else
Bram Moolenaar4033c552017-09-16 20:54:51 +02005407#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005408 {
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02005409 no_write_message();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005410 return;
5411 }
5412 }
5413
Bram Moolenaar4033c552017-09-16 20:54:51 +02005414#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00005415 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005416#endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00005417
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005418 // free buffer when not hiding it or when it's a scratch buffer
Bram Moolenaarf740b292006-02-16 22:11:02 +00005419 if (tp == NULL)
Bram Moolenaareb44a682017-08-03 22:44:55 +02005420 win_close(win, !need_hide && !buf_hide(buf));
Bram Moolenaarf740b292006-02-16 22:11:02 +00005421 else
Bram Moolenaareb44a682017-08-03 22:44:55 +02005422 win_close_othertab(win, !need_hide && !buf_hide(buf), tp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005423}
5424
Bram Moolenaar071d4272004-06-13 20:20:40 +00005425/*
Bram Moolenaardea25702017-01-29 15:18:10 +01005426 * Handle the argument for a tabpage related ex command.
5427 * Returns a tabpage number.
5428 * When an error is encountered then eap->errmsg is set.
5429 */
5430 static int
5431get_tabpage_arg(exarg_T *eap)
5432{
5433 int tab_number;
5434 int unaccept_arg0 = (eap->cmdidx == CMD_tabmove) ? 0 : 1;
5435
5436 if (eap->arg && *eap->arg != NUL)
5437 {
5438 char_u *p = eap->arg;
5439 char_u *p_save;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005440 int relative = 0; // argument +N/-N means: go to N places to the
5441 // right/left relative to the current position.
Bram Moolenaardea25702017-01-29 15:18:10 +01005442
5443 if (*p == '-')
5444 {
5445 relative = -1;
5446 p++;
5447 }
5448 else if (*p == '+')
5449 {
5450 relative = 1;
5451 p++;
5452 }
5453
5454 p_save = p;
5455 tab_number = getdigits(&p);
5456
5457 if (relative == 0)
5458 {
5459 if (STRCMP(p, "$") == 0)
5460 tab_number = LAST_TAB_NR;
Bram Moolenaar94f4ffa2020-08-10 19:21:15 +02005461 else if (STRCMP(p, "#") == 0)
5462 if (valid_tabpage(lastused_tabpage))
5463 tab_number = tabpage_index(lastused_tabpage);
5464 else
5465 {
5466 eap->errmsg = ex_errmsg(e_invargval, eap->arg);
5467 tab_number = 0;
5468 goto theend;
5469 }
Bram Moolenaardea25702017-01-29 15:18:10 +01005470 else if (p == p_save || *p_save == '-' || *p != NUL
5471 || tab_number > LAST_TAB_NR)
5472 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005473 // No numbers as argument.
Bram Moolenaar8930caa2020-07-23 16:37:03 +02005474 eap->errmsg = ex_errmsg(e_invarg2, eap->arg);
Bram Moolenaardea25702017-01-29 15:18:10 +01005475 goto theend;
5476 }
5477 }
5478 else
5479 {
5480 if (*p_save == NUL)
5481 tab_number = 1;
5482 else if (p == p_save || *p_save == '-' || *p != NUL
5483 || tab_number == 0)
5484 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005485 // No numbers as argument.
Bram Moolenaar8930caa2020-07-23 16:37:03 +02005486 eap->errmsg = ex_errmsg(e_invarg2, eap->arg);
Bram Moolenaardea25702017-01-29 15:18:10 +01005487 goto theend;
5488 }
5489 tab_number = tab_number * relative + tabpage_index(curtab);
5490 if (!unaccept_arg0 && relative == -1)
5491 --tab_number;
5492 }
5493 if (tab_number < unaccept_arg0 || tab_number > LAST_TAB_NR)
Bram Moolenaar8930caa2020-07-23 16:37:03 +02005494 eap->errmsg = ex_errmsg(e_invarg2, eap->arg);
Bram Moolenaardea25702017-01-29 15:18:10 +01005495 }
5496 else if (eap->addr_count > 0)
5497 {
5498 if (unaccept_arg0 && eap->line2 == 0)
Bram Moolenaarc6251552017-01-29 21:42:20 +01005499 {
Bram Moolenaar8930caa2020-07-23 16:37:03 +02005500 eap->errmsg = _(e_invrange);
Bram Moolenaarc6251552017-01-29 21:42:20 +01005501 tab_number = 0;
5502 }
Bram Moolenaardea25702017-01-29 15:18:10 +01005503 else
5504 {
5505 tab_number = eap->line2;
Bram Moolenaar82a12462019-01-22 22:41:42 +01005506 if (!unaccept_arg0 && *skipwhite(*eap->cmdlinep) == '-')
Bram Moolenaardea25702017-01-29 15:18:10 +01005507 {
5508 --tab_number;
5509 if (tab_number < unaccept_arg0)
Bram Moolenaar8930caa2020-07-23 16:37:03 +02005510 eap->errmsg = _(e_invrange);
Bram Moolenaardea25702017-01-29 15:18:10 +01005511 }
5512 }
5513 }
5514 else
5515 {
5516 switch (eap->cmdidx)
5517 {
5518 case CMD_tabnext:
5519 tab_number = tabpage_index(curtab) + 1;
5520 if (tab_number > LAST_TAB_NR)
5521 tab_number = 1;
5522 break;
5523 case CMD_tabmove:
5524 tab_number = LAST_TAB_NR;
5525 break;
5526 default:
5527 tab_number = tabpage_index(curtab);
5528 }
5529 }
5530
5531theend:
5532 return tab_number;
5533}
5534
5535/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00005536 * ":tabclose": close current tab page, unless it is the last one.
5537 * ":tabclose N": close tab page N.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005538 */
5539 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005540ex_tabclose(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005541{
Bram Moolenaarf740b292006-02-16 22:11:02 +00005542 tabpage_T *tp;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005543 int tab_number;
Bram Moolenaarf740b292006-02-16 22:11:02 +00005544
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005545# ifdef FEAT_CMDWIN
5546 if (cmdwin_type != 0)
5547 cmdwin_result = K_IGNORE;
5548 else
5549# endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00005550 if (first_tabpage->tp_next == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005551 emsg(_("E784: Cannot close last tab page"));
Bram Moolenaarf740b292006-02-16 22:11:02 +00005552 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005553 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005554 tab_number = get_tabpage_arg(eap);
5555 if (eap->errmsg == NULL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005556 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005557 tp = find_tabpage(tab_number);
Bram Moolenaarf740b292006-02-16 22:11:02 +00005558 if (tp == NULL)
5559 {
5560 beep_flush();
5561 return;
5562 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005563 if (tp != curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00005564 {
5565 tabpage_close_other(tp, eap->forceit);
5566 return;
5567 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005568 else if (!text_locked() && !curbuf_locked())
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005569 tabpage_close(eap->forceit);
5570 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005571 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005572}
5573
5574/*
5575 * ":tabonly": close all tab pages except the current one
5576 */
5577 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005578ex_tabonly(exarg_T *eap)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005579{
5580 tabpage_T *tp;
5581 int done;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005582 int tab_number;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005583
5584# ifdef FEAT_CMDWIN
5585 if (cmdwin_type != 0)
5586 cmdwin_result = K_IGNORE;
5587 else
5588# endif
5589 if (first_tabpage->tp_next == NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01005590 msg(_("Already only one tab page"));
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005591 else
5592 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005593 tab_number = get_tabpage_arg(eap);
5594 if (eap->errmsg == NULL)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005595 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005596 goto_tabpage(tab_number);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005597 // Repeat this up to a 1000 times, because autocommands may
5598 // mess up the lists.
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005599 for (done = 0; done < 1000; ++done)
5600 {
5601 FOR_ALL_TABPAGES(tp)
5602 if (tp->tp_topframe != topframe)
5603 {
5604 tabpage_close_other(tp, eap->forceit);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005605 // if we failed to close it quit
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005606 if (valid_tabpage(tp))
5607 done = 1000;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005608 // start over, "tp" is now invalid
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005609 break;
5610 }
5611 if (first_tabpage->tp_next == NULL)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005612 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005613 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005614 }
5615 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005616}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005617
5618/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00005619 * Close the current tab page.
5620 */
5621 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005622tabpage_close(int forceit)
Bram Moolenaarf740b292006-02-16 22:11:02 +00005623{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005624 // First close all the windows but the current one. If that worked then
5625 // close the last window in this tab, that will close it.
Bram Moolenaar459ca562016-11-10 18:16:33 +01005626 if (!ONE_WINDOW)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005627 close_others(TRUE, forceit);
Bram Moolenaar459ca562016-11-10 18:16:33 +01005628 if (ONE_WINDOW)
Bram Moolenaarf740b292006-02-16 22:11:02 +00005629 ex_win_close(forceit, curwin, NULL);
5630# ifdef FEAT_GUI
5631 need_mouse_correct = TRUE;
5632# endif
5633}
5634
5635/*
5636 * Close tab page "tp", which is not the current tab page.
5637 * Note that autocommands may make "tp" invalid.
Bram Moolenaar7875acc2006-09-10 13:51:17 +00005638 * Also takes care of the tab pages line disappearing when closing the
5639 * last-but-one tab page.
Bram Moolenaarf740b292006-02-16 22:11:02 +00005640 */
5641 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005642tabpage_close_other(tabpage_T *tp, int forceit)
Bram Moolenaarf740b292006-02-16 22:11:02 +00005643{
5644 int done = 0;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005645 win_T *wp;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00005646 int h = tabline_height();
Bram Moolenaarf740b292006-02-16 22:11:02 +00005647
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005648 // Limit to 1000 windows, autocommands may add a window while we close
5649 // one. OK, so I'm paranoid...
Bram Moolenaarf740b292006-02-16 22:11:02 +00005650 while (++done < 1000)
5651 {
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005652 wp = tp->tp_firstwin;
5653 ex_win_close(forceit, wp, tp);
Bram Moolenaarf740b292006-02-16 22:11:02 +00005654
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005655 // Autocommands may delete the tab page under our fingers and we may
5656 // fail to close a window with a modified buffer.
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005657 if (!valid_tabpage(tp) || tp->tp_firstwin == wp)
Bram Moolenaarf740b292006-02-16 22:11:02 +00005658 break;
5659 }
Bram Moolenaar7875acc2006-09-10 13:51:17 +00005660
Bram Moolenaar29323592016-07-24 22:04:11 +02005661 apply_autocmds(EVENT_TABCLOSED, NULL, NULL, FALSE, curbuf);
Bram Moolenaar29323592016-07-24 22:04:11 +02005662
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005663 redraw_tabline = TRUE;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00005664 if (h != tabline_height())
5665 shell_new_rows();
Bram Moolenaarf740b292006-02-16 22:11:02 +00005666}
5667
5668/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005669 * ":only".
5670 */
5671 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005672ex_only(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005673{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005674 win_T *wp;
5675 int wnr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005676# ifdef FEAT_GUI
5677 need_mouse_correct = TRUE;
5678# endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005679 if (eap->addr_count > 0)
5680 {
5681 wnr = eap->line2;
5682 for (wp = firstwin; --wnr > 0; )
5683 {
5684 if (wp->w_next == NULL)
5685 break;
5686 else
5687 wp = wp->w_next;
5688 }
5689 win_goto(wp);
5690 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005691 close_others(TRUE, eap->forceit);
5692}
5693
Bram Moolenaar071d4272004-06-13 20:20:40 +00005694 static void
Bram Moolenaar5e1e6d22017-01-02 17:26:00 +01005695ex_hide(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005696{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005697 // ":hide" or ":hide | cmd": hide current window
Bram Moolenaar2256c992016-11-15 21:17:07 +01005698 if (!eap->skip)
5699 {
Bram Moolenaar4033c552017-09-16 20:54:51 +02005700#ifdef FEAT_GUI
Bram Moolenaar2256c992016-11-15 21:17:07 +01005701 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005702#endif
Bram Moolenaar2256c992016-11-15 21:17:07 +01005703 if (eap->addr_count == 0)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005704 win_close(curwin, FALSE); // don't free buffer
Bram Moolenaar2256c992016-11-15 21:17:07 +01005705 else
5706 {
5707 int winnr = 0;
5708 win_T *win;
Bram Moolenaarf240e182014-11-27 18:33:02 +01005709
Bram Moolenaar2256c992016-11-15 21:17:07 +01005710 FOR_ALL_WINDOWS(win)
5711 {
5712 winnr++;
5713 if (winnr == eap->line2)
5714 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005715 }
Bram Moolenaar2256c992016-11-15 21:17:07 +01005716 if (win == NULL)
5717 win = lastwin;
5718 win_close(win, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005719 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005720 }
5721}
5722
5723/*
5724 * ":stop" and ":suspend": Suspend Vim.
5725 */
5726 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005727ex_stop(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005728{
5729 /*
5730 * Disallow suspending for "rvim".
5731 */
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005732 if (!check_restricted())
Bram Moolenaar071d4272004-06-13 20:20:40 +00005733 {
5734 if (!eap->forceit)
5735 autowrite_all();
5736 windgoto((int)Rows - 1, 0);
5737 out_char('\n');
5738 out_flush();
5739 stoptermcap();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005740 out_flush(); // needed for SUN to restore xterm buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00005741#ifdef FEAT_TITLE
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005742 mch_restore_title(SAVE_RESTORE_BOTH); // restore window titles
Bram Moolenaar071d4272004-06-13 20:20:40 +00005743#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005744 ui_suspend(); // call machine specific function
Bram Moolenaar071d4272004-06-13 20:20:40 +00005745#ifdef FEAT_TITLE
5746 maketitle();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005747 resettitle(); // force updating the title
Bram Moolenaar071d4272004-06-13 20:20:40 +00005748#endif
5749 starttermcap();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005750 scroll_start(); // scroll screen before redrawing
Bram Moolenaar071d4272004-06-13 20:20:40 +00005751 redraw_later_clear();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005752 shell_resized(); // may have resized window
Bram Moolenaar071d4272004-06-13 20:20:40 +00005753 }
5754}
5755
5756/*
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005757 * ":exit", ":xit" and ":wq": Write file and quite the current window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005758 */
5759 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005760ex_exit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005761{
Bram Moolenaar461f2122020-07-28 20:25:47 +02005762#ifdef FEAT_EVAL
Bram Moolenaarae616492020-07-28 20:07:27 +02005763 if (not_in_vim9(eap) == FAIL)
5764 return;
Bram Moolenaar461f2122020-07-28 20:25:47 +02005765#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005766#ifdef FEAT_CMDWIN
5767 if (cmdwin_type != 0)
5768 {
5769 cmdwin_result = Ctrl_C;
5770 return;
5771 }
5772#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005773 // Don't quit while editing the command line.
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005774 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005775 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005776 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005777 return;
5778 }
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005779
5780 if (before_quit_autocmds(curwin, FALSE, eap->forceit))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005781 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005782
5783 /*
5784 * if more files or windows we won't exit
5785 */
5786 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
5787 exiting = TRUE;
5788 if ( ((eap->cmdidx == CMD_wq
5789 || curbufIsChanged())
5790 && do_write(eap) == FAIL)
5791 || check_more(TRUE, eap->forceit) == FAIL
Bram Moolenaar027387f2016-01-02 22:25:52 +01005792 || (only_one_window() && check_changed_any(eap->forceit, FALSE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005793 {
5794 not_exiting();
5795 }
5796 else
5797 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005798 if (only_one_window()) // quit last window, exit Vim
Bram Moolenaar071d4272004-06-13 20:20:40 +00005799 getout(0);
Bram Moolenaar2c33d7b2017-10-14 16:06:20 +02005800 not_exiting();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005801# ifdef FEAT_GUI
5802 need_mouse_correct = TRUE;
5803# endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005804 // Quit current window, may free the buffer.
Bram Moolenaareb44a682017-08-03 22:44:55 +02005805 win_close(curwin, !buf_hide(curwin->w_buffer));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005806 }
5807}
5808
5809/*
5810 * ":print", ":list", ":number".
5811 */
5812 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005813ex_print(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005814{
Bram Moolenaardf177f62005-02-22 08:39:57 +00005815 if (curbuf->b_ml.ml_flags & ML_EMPTY)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005816 emsg(_(e_emptybuf));
Bram Moolenaardf177f62005-02-22 08:39:57 +00005817 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005818 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00005819 for ( ;!got_int; ui_breakcheck())
5820 {
5821 print_line(eap->line1,
5822 (eap->cmdidx == CMD_number || eap->cmdidx == CMD_pound
5823 || (eap->flags & EXFLAG_NR)),
5824 eap->cmdidx == CMD_list || (eap->flags & EXFLAG_LIST));
5825 if (++eap->line1 > eap->line2)
5826 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005827 out_flush(); // show one line at a time
Bram Moolenaardf177f62005-02-22 08:39:57 +00005828 }
5829 setpcmark();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005830 // put cursor at last line
Bram Moolenaardf177f62005-02-22 08:39:57 +00005831 curwin->w_cursor.lnum = eap->line2;
5832 beginline(BL_SOL | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005833 }
5834
Bram Moolenaar071d4272004-06-13 20:20:40 +00005835 ex_no_reprint = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005836}
5837
5838#ifdef FEAT_BYTEOFF
5839 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005840ex_goto(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005841{
5842 goto_byte(eap->line2);
5843}
5844#endif
5845
5846/*
5847 * ":shell".
5848 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005849 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005850ex_shell(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005851{
5852 do_shell(NULL, 0);
5853}
5854
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005855#if defined(HAVE_DROP_FILE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005856
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005857static int drop_busy = FALSE;
5858static int drop_filec;
5859static char_u **drop_filev = NULL;
5860static int drop_split;
5861static void (*drop_callback)(void *);
5862static void *drop_cookie;
5863
5864 static void
5865handle_drop_internal(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005866{
5867 exarg_T ea;
5868 int save_msg_scroll = msg_scroll;
5869
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005870 // Setting the argument list may cause screen updates and being called
5871 // recursively. Avoid that by setting drop_busy.
5872 drop_busy = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005873
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005874 // Check whether the current buffer is changed. If so, we will need
5875 // to split the current window or data could be lost.
5876 // We don't need to check if the 'hidden' option is set, as in this
5877 // case the buffer won't be lost.
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005878 if (!buf_hide(curbuf) && !drop_split)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005879 {
5880 ++emsg_off;
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005881 drop_split = check_changed(curbuf, CCGD_AW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005882 --emsg_off;
5883 }
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005884 if (drop_split)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005885 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005886 if (win_split(0, 0) == FAIL)
5887 return;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02005888 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005889
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005890 // When splitting the window, create a new alist. Otherwise the
5891 // existing one is overwritten.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005892 alist_unlink(curwin->w_alist);
5893 alist_new();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005894 }
5895
5896 /*
5897 * Set up the new argument list.
5898 */
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005899 alist_set(ALIST(curwin), drop_filec, drop_filev, FALSE, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005900
5901 /*
5902 * Move to the first file.
5903 */
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005904 // Fake up a minimal "next" command for do_argfile()
Bram Moolenaara80faa82020-04-12 19:37:17 +02005905 CLEAR_FIELD(ea);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005906 ea.cmd = (char_u *)"next";
5907 do_argfile(&ea, 0);
5908
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005909 // do_ecmd() may set need_start_insertmode, but since we never left Insert
5910 // mode that is not needed here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005911 need_start_insertmode = FALSE;
5912
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005913 // Restore msg_scroll, otherwise a following command may cause scrolling
5914 // unexpectedly. The screen will be redrawn by the caller, thus
5915 // msg_scroll being set by displaying a message is irrelevant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005916 msg_scroll = save_msg_scroll;
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005917
5918 if (drop_callback != NULL)
5919 drop_callback(drop_cookie);
5920
5921 drop_filev = NULL;
5922 drop_busy = FALSE;
5923}
5924
5925/*
5926 * Handle a file drop. The code is here because a drop is *nearly* like an
5927 * :args command, but not quite (we have a list of exact filenames, so we
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01005928 * don't want to (a) parse a command line, or (b) expand wildcards). So the
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005929 * code is very similar to :args and hence needs access to a lot of the static
5930 * functions in this file.
5931 *
5932 * The "filev" list must have been allocated using alloc(), as should each item
5933 * in the list. This function takes over responsibility for freeing the "filev"
5934 * list.
5935 */
5936 void
5937handle_drop(
5938 int filec, // the number of files dropped
5939 char_u **filev, // the list of files dropped
5940 int split, // force splitting the window
5941 void (*callback)(void *), // to be called after setting the argument
5942 // list
5943 void *cookie) // argument for "callback" (allocated)
5944{
5945 // Cannot handle recursive drops, finish the pending one.
5946 if (drop_busy)
5947 {
5948 FreeWild(filec, filev);
5949 vim_free(cookie);
5950 return;
5951 }
5952
5953 // When calling handle_drop() more than once in a row we only use the last
5954 // one.
5955 if (drop_filev != NULL)
5956 {
5957 FreeWild(drop_filec, drop_filev);
5958 vim_free(drop_cookie);
5959 }
5960
5961 drop_filec = filec;
5962 drop_filev = filev;
5963 drop_split = split;
5964 drop_callback = callback;
5965 drop_cookie = cookie;
5966
5967 // Postpone this when:
5968 // - editing the command line
5969 // - not possible to change the current buffer
5970 // - updating the screen
5971 // As it may change buffers and window structures that are in use and cause
5972 // freed memory to be used.
5973 if (text_locked() || curbuf_locked() || updating_screen)
5974 return;
5975
5976 handle_drop_internal();
5977}
5978
5979/*
5980 * To be called when text is unlocked, curbuf is unlocked or updating_screen is
5981 * reset: Handle a postponed drop.
5982 */
5983 void
5984handle_any_postponed_drop(void)
5985{
5986 if (!drop_busy && drop_filev != NULL
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +02005987 && !text_locked() && !curbuf_locked() && !updating_screen)
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005988 handle_drop_internal();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005989}
5990#endif
5991
Bram Moolenaar071d4272004-06-13 20:20:40 +00005992/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005993 * ":preserve".
5994 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005995 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005996ex_preserve(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005997{
Bram Moolenaar4399ef42005-02-12 14:29:27 +00005998 curbuf->b_flags |= BF_PRESERVED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005999 ml_preserve(curbuf, TRUE);
6000}
6001
6002/*
6003 * ":recover".
6004 */
6005 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006006ex_recover(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006007{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006008 // Set recoverymode right away to avoid the ATTENTION prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006009 recoverymode = TRUE;
Bram Moolenaar45d3b142013-11-09 03:31:51 +01006010 if (!check_changed(curbuf, (p_awa ? CCGD_AW : 0)
6011 | CCGD_MULTWIN
6012 | (eap->forceit ? CCGD_FORCEIT : 0)
6013 | CCGD_EXCMD)
6014
Bram Moolenaar071d4272004-06-13 20:20:40 +00006015 && (*eap->arg == NUL
6016 || setfname(curbuf, eap->arg, NULL, TRUE) == OK))
Bram Moolenaar99499b12019-05-23 21:35:48 +02006017 ml_recover(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006018 recoverymode = FALSE;
6019}
6020
6021/*
6022 * Command modifier used in a wrong way.
6023 */
6024 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006025ex_wrongmodifier(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006026{
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006027 eap->errmsg = _(e_invalid_command);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006028}
6029
Bram Moolenaar071d4272004-06-13 20:20:40 +00006030/*
6031 * :sview [+command] file split window with new file, read-only
6032 * :split [[+command] file] split window with current or new file
6033 * :vsplit [[+command] file] split window vertically with current or new file
6034 * :new [[+command] file] split window with no or new file
6035 * :vnew [[+command] file] split vertically window with no or new file
6036 * :sfind [+command] file split window with file in 'path'
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006037 *
6038 * :tabedit open new Tab page with empty window
6039 * :tabedit [+command] file open new Tab page and edit "file"
6040 * :tabnew [[+command] file] just like :tabedit
6041 * :tabfind [+command] file open new Tab page and find "file"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006042 */
6043 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006044ex_splitview(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006045{
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006046 win_T *old_curwin = curwin;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006047#if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006048 char_u *fname = NULL;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006049#endif
6050#ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00006051 int browse_flag = cmdmod.browse;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006052#endif
Bram Moolenaar39902a02018-06-21 22:10:08 +02006053 int use_tab = eap->cmdidx == CMD_tabedit
6054 || eap->cmdidx == CMD_tabfind
6055 || eap->cmdidx == CMD_tabnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006056
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01006057 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02006058 return;
6059
Bram Moolenaar4033c552017-09-16 20:54:51 +02006060#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00006061 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006062#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006063
Bram Moolenaar4033c552017-09-16 20:54:51 +02006064#ifdef FEAT_QUICKFIX
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006065 // A ":split" in the quickfix window works like ":new". Don't want two
6066 // quickfix windows. But it's OK when doing ":tab split".
Bram Moolenaar05bb9532008-07-04 09:44:11 +00006067 if (bt_quickfix(curbuf) && cmdmod.tab == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006068 {
6069 if (eap->cmdidx == CMD_split)
6070 eap->cmdidx = CMD_new;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006071 if (eap->cmdidx == CMD_vsplit)
6072 eap->cmdidx = CMD_vnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006073 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02006074#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006075
Bram Moolenaar4033c552017-09-16 20:54:51 +02006076#ifdef FEAT_SEARCHPATH
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006077 if (eap->cmdidx == CMD_sfind || eap->cmdidx == CMD_tabfind)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006078 {
6079 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg),
6080 FNAME_MESS, TRUE, curbuf->b_ffname);
6081 if (fname == NULL)
6082 goto theend;
6083 eap->arg = fname;
6084 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006085# ifdef FEAT_BROWSE
Bram Moolenaar4033c552017-09-16 20:54:51 +02006086 else
6087# endif
6088#endif
6089#ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00006090 if (cmdmod.browse
Bram Moolenaar071d4272004-06-13 20:20:40 +00006091 && eap->cmdidx != CMD_vnew
Bram Moolenaar071d4272004-06-13 20:20:40 +00006092 && eap->cmdidx != CMD_new)
6093 {
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006094 if (
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006095# ifdef FEAT_GUI
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006096 !gui.in_use &&
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006097# endif
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006098 au_has_group((char_u *)"FileExplorer"))
6099 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006100 // No browsing supported but we do have the file explorer:
6101 // Edit the directory.
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006102 if (*eap->arg == NUL || !mch_isdir(eap->arg))
6103 eap->arg = (char_u *)".";
6104 }
6105 else
6106 {
Bram Moolenaar39902a02018-06-21 22:10:08 +02006107 fname = do_browse(0, (char_u *)(use_tab
6108 ? _("Edit File in new tab page")
6109 : _("Edit File in new window")),
Bram Moolenaar071d4272004-06-13 20:20:40 +00006110 eap->arg, NULL, NULL, NULL, curbuf);
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006111 if (fname == NULL)
6112 goto theend;
6113 eap->arg = fname;
6114 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006115 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006116 cmdmod.browse = FALSE; // Don't browse again in do_ecmd().
Bram Moolenaar4033c552017-09-16 20:54:51 +02006117#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006118
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006119 /*
6120 * Either open new tab page or split the window.
6121 */
Bram Moolenaar39902a02018-06-21 22:10:08 +02006122 if (use_tab)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006123 {
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006124 if (win_new_tabpage(cmdmod.tab != 0 ? cmdmod.tab
6125 : eap->addr_count == 0 ? 0
6126 : (int)eap->line2 + 1) != FAIL)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006127 {
Bram Moolenaard2b66012008-01-09 19:30:36 +00006128 do_exedit(eap, old_curwin);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006129
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006130 // set the alternate buffer for the window we came from
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006131 if (curwin != old_curwin
6132 && win_valid(old_curwin)
6133 && old_curwin->w_buffer != curbuf
6134 && !cmdmod.keepalt)
6135 old_curwin->w_alt_fnum = curbuf->b_fnum;
6136 }
6137 }
6138 else if (win_split(eap->addr_count > 0 ? (int)eap->line2 : 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006139 *eap->cmd == 'v' ? WSP_VERT : 0) != FAIL)
6140 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006141 // Reset 'scrollbind' when editing another file, but keep it when
6142 // doing ":split" without arguments.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006143 if (*eap->arg != NUL
Bram Moolenaar8a3bb562018-03-04 20:14:14 +01006144# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00006145 || cmdmod.browse
Bram Moolenaar8a3bb562018-03-04 20:14:14 +01006146# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006147 )
Bram Moolenaar3368ea22010-09-21 16:56:35 +02006148 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006149 else
6150 do_check_scrollbind(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006151 do_exedit(eap, old_curwin);
6152 }
6153
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006154# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00006155 cmdmod.browse = browse_flag;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006156# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006157
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006158# if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006159theend:
6160 vim_free(fname);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006161# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006162}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006163
6164/*
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006165 * Open a new tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006166 */
6167 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006168tabpage_new(void)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006169{
6170 exarg_T ea;
6171
Bram Moolenaara80faa82020-04-12 19:37:17 +02006172 CLEAR_FIELD(ea);
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006173 ea.cmdidx = CMD_tabnew;
6174 ea.cmd = (char_u *)"tabn";
6175 ea.arg = (char_u *)"";
6176 ex_splitview(&ea);
6177}
6178
6179/*
6180 * :tabnext command
6181 */
6182 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006183ex_tabnext(exarg_T *eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006184{
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006185 int tab_number;
6186
Bram Moolenaar8cdbd5b2019-06-16 15:50:45 +02006187 if (ERROR_IF_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02006188 return;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006189 switch (eap->cmdidx)
6190 {
6191 case CMD_tabfirst:
6192 case CMD_tabrewind:
6193 goto_tabpage(1);
6194 break;
6195 case CMD_tablast:
6196 goto_tabpage(9999);
6197 break;
6198 case CMD_tabprevious:
6199 case CMD_tabNext:
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006200 if (eap->arg && *eap->arg != NUL)
6201 {
6202 char_u *p = eap->arg;
6203 char_u *p_save = p;
6204
6205 tab_number = getdigits(&p);
6206 if (p == p_save || *p_save == '-' || *p != NUL
6207 || tab_number == 0)
6208 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006209 // No numbers as argument.
Bram Moolenaar8930caa2020-07-23 16:37:03 +02006210 eap->errmsg = ex_errmsg(e_invarg2, eap->arg);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006211 return;
6212 }
6213 }
6214 else
6215 {
6216 if (eap->addr_count == 0)
6217 tab_number = 1;
6218 else
6219 {
6220 tab_number = eap->line2;
6221 if (tab_number < 1)
6222 {
Bram Moolenaar8930caa2020-07-23 16:37:03 +02006223 eap->errmsg = _(e_invrange);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006224 return;
6225 }
6226 }
6227 }
6228 goto_tabpage(-tab_number);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006229 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006230 default: // CMD_tabnext
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006231 tab_number = get_tabpage_arg(eap);
6232 if (eap->errmsg == NULL)
6233 goto_tabpage(tab_number);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006234 break;
6235 }
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006236}
6237
6238/*
6239 * :tabmove command
6240 */
6241 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006242ex_tabmove(exarg_T *eap)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006243{
Bram Moolenaar40ce3a42015-04-21 18:08:39 +02006244 int tab_number;
Bram Moolenaar8cb8dca2012-07-06 18:27:39 +02006245
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006246 tab_number = get_tabpage_arg(eap);
6247 if (eap->errmsg == NULL)
6248 tabpage_move(tab_number);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006249}
6250
6251/*
6252 * :tabs command: List tabs and their contents.
6253 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006254 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006255ex_tabs(exarg_T *eap UNUSED)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006256{
6257 tabpage_T *tp;
6258 win_T *wp;
6259 int tabcount = 1;
6260
6261 msg_start();
6262 msg_scroll = TRUE;
6263 for (tp = first_tabpage; tp != NULL && !got_int; tp = tp->tp_next)
6264 {
6265 msg_putchar('\n');
6266 vim_snprintf((char *)IObuff, IOSIZE, _("Tab page %d"), tabcount++);
Bram Moolenaar8820b482017-03-16 17:23:31 +01006267 msg_outtrans_attr(IObuff, HL_ATTR(HLF_T));
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006268 out_flush(); // output one line at a time
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006269 ui_breakcheck();
6270
Bram Moolenaar030f0df2006-02-21 22:02:53 +00006271 if (tp == curtab)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006272 wp = firstwin;
6273 else
6274 wp = tp->tp_firstwin;
6275 for ( ; wp != NULL && !got_int; wp = wp->w_next)
6276 {
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006277 msg_putchar('\n');
6278 msg_putchar(wp == curwin ? '>' : ' ');
6279 msg_putchar(' ');
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006280 msg_putchar(bufIsChanged(wp->w_buffer) ? '+' : ' ');
6281 msg_putchar(' ');
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006282 if (buf_spname(wp->w_buffer) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02006283 vim_strncpy(IObuff, buf_spname(wp->w_buffer), IOSIZE - 1);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006284 else
6285 home_replace(wp->w_buffer, wp->w_buffer->b_fname,
6286 IObuff, IOSIZE, TRUE);
6287 msg_outtrans(IObuff);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006288 out_flush(); // output one line at a time
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006289 ui_breakcheck();
6290 }
6291 }
6292}
6293
Bram Moolenaar071d4272004-06-13 20:20:40 +00006294/*
6295 * ":mode": Set screen mode.
6296 * If no argument given, just get the screen size and redraw.
6297 */
6298 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006299ex_mode(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006300{
6301 if (*eap->arg == NUL)
6302 shell_resized();
6303 else
Bram Moolenaar3c71aec2020-01-17 19:32:20 +01006304 emsg(_(e_screenmode));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006305}
6306
Bram Moolenaar071d4272004-06-13 20:20:40 +00006307/*
6308 * ":resize".
6309 * set, increment or decrement current window height
6310 */
6311 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006312ex_resize(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006313{
6314 int n;
6315 win_T *wp = curwin;
6316
6317 if (eap->addr_count > 0)
6318 {
6319 n = eap->line2;
6320 for (wp = firstwin; wp->w_next != NULL && --n > 0; wp = wp->w_next)
6321 ;
6322 }
6323
Bram Moolenaar44a2f922016-03-19 22:11:51 +01006324# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00006325 need_mouse_correct = TRUE;
Bram Moolenaar44a2f922016-03-19 22:11:51 +01006326# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006327 n = atol((char *)eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006328 if (cmdmod.split & WSP_VERT)
6329 {
6330 if (*eap->arg == '-' || *eap->arg == '+')
Bram Moolenaar02631462017-09-22 15:20:32 +02006331 n += curwin->w_width;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006332 else if (n == 0 && eap->arg[0] == NUL) // default is very wide
Bram Moolenaar071d4272004-06-13 20:20:40 +00006333 n = 9999;
6334 win_setwidth_win((int)n, wp);
6335 }
6336 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006337 {
6338 if (*eap->arg == '-' || *eap->arg == '+')
6339 n += curwin->w_height;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006340 else if (n == 0 && eap->arg[0] == NUL) // default is very high
Bram Moolenaar071d4272004-06-13 20:20:40 +00006341 n = 9999;
6342 win_setheight_win((int)n, wp);
6343 }
6344}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006345
6346/*
6347 * ":find [+command] <file>" command.
6348 */
6349 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006350ex_find(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006351{
6352#ifdef FEAT_SEARCHPATH
6353 char_u *fname;
6354 int count;
6355
6356 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg), FNAME_MESS,
6357 TRUE, curbuf->b_ffname);
6358 if (eap->addr_count > 0)
6359 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006360 // Repeat finding the file "count" times. This matters when it
6361 // appears several times in the path.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006362 count = eap->line2;
6363 while (fname != NULL && --count > 0)
6364 {
6365 vim_free(fname);
6366 fname = find_file_in_path(NULL, 0, FNAME_MESS,
6367 FALSE, curbuf->b_ffname);
6368 }
6369 }
6370
6371 if (fname != NULL)
6372 {
6373 eap->arg = fname;
6374#endif
6375 do_exedit(eap, NULL);
6376#ifdef FEAT_SEARCHPATH
6377 vim_free(fname);
6378 }
6379#endif
6380}
6381
6382/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00006383 * ":open" simulation: for now just work like ":visual".
6384 */
6385 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006386ex_open(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00006387{
6388 regmatch_T regmatch;
6389 char_u *p;
6390
6391 curwin->w_cursor.lnum = eap->line2;
6392 beginline(BL_SOL | BL_FIX);
6393 if (*eap->arg == '/')
6394 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006395 // ":open /pattern/": put cursor in column found with pattern
Bram Moolenaardf177f62005-02-22 08:39:57 +00006396 ++eap->arg;
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006397 p = skip_regexp(eap->arg, '/', p_magic);
Bram Moolenaardf177f62005-02-22 08:39:57 +00006398 *p = NUL;
6399 regmatch.regprog = vim_regcomp(eap->arg, p_magic ? RE_MAGIC : 0);
6400 if (regmatch.regprog != NULL)
6401 {
6402 regmatch.rm_ic = p_ic;
6403 p = ml_get_curline();
6404 if (vim_regexec(&regmatch, p, (colnr_T)0))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006405 curwin->w_cursor.col = (colnr_T)(regmatch.startp[0] - p);
Bram Moolenaardf177f62005-02-22 08:39:57 +00006406 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006407 emsg(_(e_nomatch));
Bram Moolenaar473de612013-06-08 18:19:48 +02006408 vim_regfree(regmatch.regprog);
Bram Moolenaardf177f62005-02-22 08:39:57 +00006409 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006410 // Move to the NUL, ignore any other arguments.
Bram Moolenaardf177f62005-02-22 08:39:57 +00006411 eap->arg += STRLEN(eap->arg);
6412 }
6413 check_cursor();
6414
6415 eap->cmdidx = CMD_visual;
6416 do_exedit(eap, NULL);
6417}
6418
6419/*
6420 * ":edit", ":badd", ":visual".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006421 */
6422 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006423ex_edit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006424{
6425 do_exedit(eap, NULL);
6426}
6427
6428/*
Bram Moolenaar4b96df52020-01-26 22:00:26 +01006429 * ":edit <file>" command and alike.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006430 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006431 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006432do_exedit(
6433 exarg_T *eap,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006434 win_T *old_curwin) // curwin before doing a split or NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006435{
6436 int n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006437 int need_hide;
Bram Moolenaardf177f62005-02-22 08:39:57 +00006438 int exmode_was = exmode_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006439
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01006440 if ((eap->cmdidx != CMD_pedit && ERROR_IF_POPUP_WINDOW)
6441 || ERROR_IF_TERM_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02006442 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006443 /*
6444 * ":vi" command ends Ex mode.
6445 */
6446 if (exmode_active && (eap->cmdidx == CMD_visual
6447 || eap->cmdidx == CMD_view))
6448 {
6449 exmode_active = FALSE;
Bram Moolenaar158ea172020-06-18 17:28:39 +02006450 ex_pressedreturn = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006451 if (*eap->arg == NUL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00006452 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006453 // Special case: ":global/pat/visual\NLvi-commands"
Bram Moolenaardf177f62005-02-22 08:39:57 +00006454 if (global_busy)
6455 {
6456 int rd = RedrawingDisabled;
6457 int nwr = no_wait_return;
6458 int ms = msg_scroll;
6459#ifdef FEAT_GUI
6460 int he = hold_gui_events;
6461#endif
6462
6463 if (eap->nextcmd != NULL)
6464 {
6465 stuffReadbuff(eap->nextcmd);
6466 eap->nextcmd = NULL;
6467 }
6468
6469 if (exmode_was != EXMODE_VIM)
6470 settmode(TMODE_RAW);
6471 RedrawingDisabled = 0;
6472 no_wait_return = 0;
6473 need_wait_return = FALSE;
6474 msg_scroll = 0;
6475#ifdef FEAT_GUI
6476 hold_gui_events = 0;
6477#endif
6478 must_redraw = CLEAR;
Bram Moolenaar9e2bcb52020-02-18 21:33:00 +01006479 pending_exmode_active = TRUE;
Bram Moolenaardf177f62005-02-22 08:39:57 +00006480
6481 main_loop(FALSE, TRUE);
6482
Bram Moolenaar9e2bcb52020-02-18 21:33:00 +01006483 pending_exmode_active = FALSE;
Bram Moolenaardf177f62005-02-22 08:39:57 +00006484 RedrawingDisabled = rd;
6485 no_wait_return = nwr;
6486 msg_scroll = ms;
6487#ifdef FEAT_GUI
6488 hold_gui_events = he;
6489#endif
6490 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006491 return;
Bram Moolenaardf177f62005-02-22 08:39:57 +00006492 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006493 }
6494
6495 if ((eap->cmdidx == CMD_new
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006496 || eap->cmdidx == CMD_tabnew
6497 || eap->cmdidx == CMD_tabedit
Bram Moolenaar4033c552017-09-16 20:54:51 +02006498 || eap->cmdidx == CMD_vnew) && *eap->arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006499 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006500 // ":new" or ":tabnew" without argument: edit an new empty buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00006501 setpcmark();
6502 (void)do_ecmd(0, NULL, NULL, eap, ECMD_ONE,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00006503 ECMD_HIDE + (eap->forceit ? ECMD_FORCEIT : 0),
6504 old_curwin == NULL ? curwin : NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006505 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02006506 else if ((eap->cmdidx != CMD_split && eap->cmdidx != CMD_vsplit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006507 || *eap->arg != NUL
6508#ifdef FEAT_BROWSE
6509 || cmdmod.browse
6510#endif
6511 )
6512 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006513 // Can't edit another file when "curbuf_lock" is set. Only ":edit"
6514 // can bring us here, others are stopped earlier.
Bram Moolenaar5555acc2006-04-07 21:33:12 +00006515 if (*eap->arg != NUL && curbuf_locked())
6516 return;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006517
Bram Moolenaar071d4272004-06-13 20:20:40 +00006518 n = readonlymode;
6519 if (eap->cmdidx == CMD_view || eap->cmdidx == CMD_sview)
6520 readonlymode = TRUE;
6521 else if (eap->cmdidx == CMD_enew)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006522 readonlymode = FALSE; // 'readonly' doesn't make sense in an
6523 // empty buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00006524 setpcmark();
6525 if (do_ecmd(0, (eap->cmdidx == CMD_enew ? NULL : eap->arg),
6526 NULL, eap,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006527 // ":edit" goes to first line if Vi compatible
Bram Moolenaar071d4272004-06-13 20:20:40 +00006528 (*eap->arg == NUL && eap->do_ecmd_lnum == 0
6529 && vim_strchr(p_cpo, CPO_GOTO1) != NULL)
6530 ? ECMD_ONE : eap->do_ecmd_lnum,
Bram Moolenaareb44a682017-08-03 22:44:55 +02006531 (buf_hide(curbuf) ? ECMD_HIDE : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006532 + (eap->forceit ? ECMD_FORCEIT : 0)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006533 // after a split we can use an existing buffer
Bram Moolenaar7b449342014-03-25 13:03:48 +01006534 + (old_curwin != NULL ? ECMD_OLDBUF : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006535 + (eap->cmdidx == CMD_badd ? ECMD_ADDBUF : 0 )
Bram Moolenaar701f7af2008-11-15 13:12:07 +00006536 , old_curwin == NULL ? curwin : NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006537 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006538 // Editing the file failed. If the window was split, close it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006539 if (old_curwin != NULL)
6540 {
6541 need_hide = (curbufIsChanged() && curbuf->b_nwindows <= 1);
Bram Moolenaareb44a682017-08-03 22:44:55 +02006542 if (!need_hide || buf_hide(curbuf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006543 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006544#if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00006545 cleanup_T cs;
6546
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006547 // Reset the error/interrupt/exception state here so that
6548 // aborting() returns FALSE when closing a window.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00006549 enter_cleanup(&cs);
Bram Moolenaar4033c552017-09-16 20:54:51 +02006550#endif
6551#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00006552 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006553#endif
Bram Moolenaareb44a682017-08-03 22:44:55 +02006554 win_close(curwin, !need_hide && !buf_hide(curbuf));
Bram Moolenaarc0197e22004-09-13 20:26:32 +00006555
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006556#if defined(FEAT_EVAL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006557 // Restore the error/interrupt/exception state if not
6558 // discarded by a new aborting error, interrupt, or
6559 // uncaught exception.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00006560 leave_cleanup(&cs);
Bram Moolenaar4033c552017-09-16 20:54:51 +02006561#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006562 }
6563 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006564 }
6565 else if (readonlymode && curbuf->b_nwindows == 1)
6566 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006567 // When editing an already visited buffer, 'readonly' won't be set
6568 // but the previous value is kept. With ":view" and ":sview" we
6569 // want the file to be readonly, except when another window is
6570 // editing the same buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006571 curbuf->b_p_ro = TRUE;
6572 }
6573 readonlymode = n;
6574 }
6575 else
6576 {
6577 if (eap->do_ecmd_cmd != NULL)
6578 do_cmdline_cmd(eap->do_ecmd_cmd);
6579#ifdef FEAT_TITLE
6580 n = curwin->w_arg_idx_invalid;
6581#endif
6582 check_arg_idx(curwin);
6583#ifdef FEAT_TITLE
6584 if (n != curwin->w_arg_idx_invalid)
6585 maketitle();
6586#endif
6587 }
6588
Bram Moolenaar071d4272004-06-13 20:20:40 +00006589 /*
6590 * if ":split file" worked, set alternate file name in old window to new
6591 * file
6592 */
6593 if (old_curwin != NULL
6594 && *eap->arg != NUL
6595 && curwin != old_curwin
6596 && win_valid(old_curwin)
Bram Moolenaard4755bb2004-09-02 19:12:26 +00006597 && old_curwin->w_buffer != curbuf
6598 && !cmdmod.keepalt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006599 old_curwin->w_alt_fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006600
6601 ex_no_reprint = TRUE;
6602}
6603
6604#ifndef FEAT_GUI
6605/*
6606 * ":gui" and ":gvim" when there is no GUI.
6607 */
6608 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006609ex_nogui(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006610{
Bram Moolenaar8930caa2020-07-23 16:37:03 +02006611 eap->errmsg = _(e_nogvim);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006612}
6613#endif
6614
Bram Moolenaar4f974752019-02-17 17:44:42 +01006615#if defined(FEAT_GUI_MSWIN) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006616 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006617ex_tearoff(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006618{
6619 gui_make_tearoff(eap->arg);
6620}
6621#endif
6622
Bram Moolenaar29a2c082018-03-05 21:06:23 +01006623#if (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK) \
6624 || defined(FEAT_TERM_POPUP_MENU)) && defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006625 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006626ex_popup(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006627{
Bram Moolenaar29a2c082018-03-05 21:06:23 +01006628# if defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK)
6629 if (gui.in_use)
6630 gui_make_popup(eap->arg, eap->forceit);
6631# ifdef FEAT_TERM_POPUP_MENU
6632 else
6633# endif
6634# endif
6635# ifdef FEAT_TERM_POPUP_MENU
6636 pum_make_popup(eap->arg, eap->forceit);
6637# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006638}
6639#endif
6640
Bram Moolenaar071d4272004-06-13 20:20:40 +00006641 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006642ex_swapname(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006643{
6644 if (curbuf->b_ml.ml_mfp == NULL || curbuf->b_ml.ml_mfp->mf_fname == NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01006645 msg(_("No swap file"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006646 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01006647 msg((char *)curbuf->b_ml.ml_mfp->mf_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006648}
6649
6650/*
6651 * ":syncbind" forces all 'scrollbind' windows to have the same relative
6652 * offset.
6653 * (1998-11-02 16:21:01 R. Edward Ralston <eralston@computer.org>)
6654 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006655 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006656ex_syncbind(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006657{
Bram Moolenaar071d4272004-06-13 20:20:40 +00006658 win_T *wp;
Bram Moolenaardedd1b02013-12-14 13:06:17 +01006659 win_T *save_curwin = curwin;
6660 buf_T *save_curbuf = curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006661 long topline;
6662 long y;
6663 linenr_T old_linenr = curwin->w_cursor.lnum;
6664
6665 setpcmark();
6666
6667 /*
6668 * determine max topline
6669 */
6670 if (curwin->w_p_scb)
6671 {
6672 topline = curwin->w_topline;
Bram Moolenaar29323592016-07-24 22:04:11 +02006673 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006674 {
6675 if (wp->w_p_scb && wp->w_buffer)
6676 {
Bram Moolenaar375e3392019-01-31 18:26:10 +01006677 y = wp->w_buffer->b_ml.ml_line_count - get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006678 if (topline > y)
6679 topline = y;
6680 }
6681 }
6682 if (topline < 1)
6683 topline = 1;
6684 }
6685 else
6686 {
6687 topline = 1;
6688 }
6689
6690
6691 /*
Bram Moolenaardedd1b02013-12-14 13:06:17 +01006692 * Set all scrollbind windows to the same topline.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006693 */
Bram Moolenaar29323592016-07-24 22:04:11 +02006694 FOR_ALL_WINDOWS(curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006695 {
6696 if (curwin->w_p_scb)
6697 {
Bram Moolenaardedd1b02013-12-14 13:06:17 +01006698 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006699 y = topline - curwin->w_topline;
6700 if (y > 0)
6701 scrollup(y, TRUE);
6702 else
6703 scrolldown(-y, TRUE);
6704 curwin->w_scbind_pos = topline;
6705 redraw_later(VALID);
6706 cursor_correct();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006707 curwin->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006708 }
6709 }
Bram Moolenaardedd1b02013-12-14 13:06:17 +01006710 curwin = save_curwin;
6711 curbuf = save_curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006712 if (curwin->w_p_scb)
6713 {
6714 did_syncbind = TRUE;
6715 checkpcmark();
6716 if (old_linenr != curwin->w_cursor.lnum)
6717 {
6718 char_u ctrl_o[2];
6719
6720 ctrl_o[0] = Ctrl_O;
6721 ctrl_o[1] = 0;
6722 ins_typebuf(ctrl_o, REMAP_NONE, 0, TRUE, FALSE);
6723 }
6724 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006725}
6726
6727
6728 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006729ex_read(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006730{
Bram Moolenaardf177f62005-02-22 08:39:57 +00006731 int i;
6732 int empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
6733 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006734
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006735 if (eap->usefilter) // :r!cmd
Bram Moolenaar071d4272004-06-13 20:20:40 +00006736 do_bang(1, eap, FALSE, FALSE, TRUE);
6737 else
6738 {
6739 if (u_save(eap->line2, (linenr_T)(eap->line2 + 1)) == FAIL)
6740 return;
6741
6742#ifdef FEAT_BROWSE
6743 if (cmdmod.browse)
6744 {
6745 char_u *browseFile;
6746
Bram Moolenaar7171abe2004-10-11 10:06:20 +00006747 browseFile = do_browse(0, (char_u *)_("Append File"), eap->arg,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006748 NULL, NULL, NULL, curbuf);
6749 if (browseFile != NULL)
6750 {
6751 i = readfile(browseFile, NULL,
6752 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
6753 vim_free(browseFile);
6754 }
6755 else
6756 i = OK;
6757 }
6758 else
6759#endif
6760 if (*eap->arg == NUL)
6761 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006762 if (check_fname() == FAIL) // check for no file name
Bram Moolenaar071d4272004-06-13 20:20:40 +00006763 return;
6764 i = readfile(curbuf->b_ffname, curbuf->b_fname,
6765 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
6766 }
6767 else
6768 {
6769 if (vim_strchr(p_cpo, CPO_ALTREAD) != NULL)
6770 (void)setaltfname(eap->arg, eap->arg, (linenr_T)1);
6771 i = readfile(eap->arg, NULL,
6772 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
6773
6774 }
Bram Moolenaare13b9af2017-01-13 22:01:02 +01006775 if (i != OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006776 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006777#if defined(FEAT_EVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006778 if (!aborting())
6779#endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006780 semsg(_(e_notopen), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006781 }
6782 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00006783 {
6784 if (empty && exmode_active)
6785 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006786 // Delete the empty line that remains. Historically ex does
6787 // this but vi doesn't.
Bram Moolenaardf177f62005-02-22 08:39:57 +00006788 if (eap->line2 == 0)
6789 lnum = curbuf->b_ml.ml_line_count;
6790 else
6791 lnum = 1;
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00006792 if (*ml_get(lnum) == NUL && u_savedel(lnum, 1L) == OK)
Bram Moolenaardf177f62005-02-22 08:39:57 +00006793 {
Bram Moolenaarca70c072020-05-30 20:30:46 +02006794 ml_delete(lnum);
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00006795 if (curwin->w_cursor.lnum > 1
6796 && curwin->w_cursor.lnum >= lnum)
Bram Moolenaardf177f62005-02-22 08:39:57 +00006797 --curwin->w_cursor.lnum;
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00006798 deleted_lines_mark(lnum, 1L);
Bram Moolenaardf177f62005-02-22 08:39:57 +00006799 }
6800 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006801 redraw_curbuf_later(VALID);
Bram Moolenaardf177f62005-02-22 08:39:57 +00006802 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006803 }
6804}
6805
Bram Moolenaarea408852005-06-25 22:49:46 +00006806static char_u *prev_dir = NULL;
6807
6808#if defined(EXITFREE) || defined(PROTO)
6809 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006810free_cd_dir(void)
Bram Moolenaarea408852005-06-25 22:49:46 +00006811{
Bram Moolenaard23a8232018-02-10 18:45:26 +01006812 VIM_CLEAR(prev_dir);
6813 VIM_CLEAR(globaldir);
Bram Moolenaarea408852005-06-25 22:49:46 +00006814}
6815#endif
6816
Bram Moolenaarf4258302013-06-02 18:20:17 +02006817/*
Bram Moolenaar002bc792020-06-05 22:33:42 +02006818 * Get the previous directory for the given chdir scope.
6819 */
6820 static char_u *
6821get_prevdir(cdscope_T scope)
6822{
6823 if (scope == CDSCOPE_WINDOW)
6824 return curwin->w_prevdir;
6825 else if (scope == CDSCOPE_TABPAGE)
6826 return curtab->tp_prevdir;
6827 return prev_dir;
6828}
6829
6830/*
Bram Moolenaarf4258302013-06-02 18:20:17 +02006831 * Deal with the side effects of changing the current directory.
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006832 * When 'scope' is CDSCOPE_TABPAGE then this was after an ":tcd" command.
6833 * When 'scope' is CDSCOPE_WINDOW then this was after an ":lcd" command.
Bram Moolenaarf4258302013-06-02 18:20:17 +02006834 */
6835 void
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006836post_chdir(cdscope_T scope)
Bram Moolenaarf4258302013-06-02 18:20:17 +02006837{
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006838 if (scope != CDSCOPE_WINDOW)
Bram Moolenaar00aa0692019-04-27 20:37:57 +02006839 // Clear tab local directory for both :cd and :tcd
6840 VIM_CLEAR(curtab->tp_localdir);
Bram Moolenaard23a8232018-02-10 18:45:26 +01006841 VIM_CLEAR(curwin->w_localdir);
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006842 if (scope != CDSCOPE_GLOBAL)
Bram Moolenaarf4258302013-06-02 18:20:17 +02006843 {
Bram Moolenaar002bc792020-06-05 22:33:42 +02006844 char_u *pdir = get_prevdir(scope);
6845
6846 // If still in the global directory, need to remember current
6847 // directory as the global directory.
6848 if (globaldir == NULL && pdir != NULL)
6849 globaldir = vim_strsave(pdir);
6850
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006851 // Remember this local directory for the window.
Bram Moolenaarf4258302013-06-02 18:20:17 +02006852 if (mch_dirname(NameBuff, MAXPATHL) == OK)
Bram Moolenaar00aa0692019-04-27 20:37:57 +02006853 {
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006854 if (scope == CDSCOPE_TABPAGE)
Bram Moolenaar00aa0692019-04-27 20:37:57 +02006855 curtab->tp_localdir = vim_strsave(NameBuff);
6856 else
6857 curwin->w_localdir = vim_strsave(NameBuff);
6858 }
Bram Moolenaarf4258302013-06-02 18:20:17 +02006859 }
6860 else
6861 {
Bram Moolenaar002bc792020-06-05 22:33:42 +02006862 // We are now in the global directory, no need to remember its name.
Bram Moolenaard23a8232018-02-10 18:45:26 +01006863 VIM_CLEAR(globaldir);
Bram Moolenaarf4258302013-06-02 18:20:17 +02006864 }
6865
6866 shorten_fnames(TRUE);
6867}
6868
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006869/*
6870 * Change directory function used by :cd/:tcd/:lcd Ex commands and the
Bram Moolenaar95058722020-06-01 16:26:19 +02006871 * chdir() function.
6872 * scope == CDSCOPE_WINDOW: changes the window-local directory
6873 * scope == CDSCOPE_TABPAGE: changes the tab-local directory
6874 * Otherwise: changes the global directory
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006875 * Returns TRUE if the directory is successfully changed.
6876 */
6877 int
6878changedir_func(
6879 char_u *new_dir,
6880 int forceit,
6881 cdscope_T scope)
6882{
6883 char_u *tofree;
Bram Moolenaar002bc792020-06-05 22:33:42 +02006884 char_u *pdir = NULL;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006885 int dir_differs;
6886 int retval = FALSE;
6887
Bram Moolenaar7cc96922020-01-31 22:41:38 +01006888 if (new_dir == NULL || allbuf_locked())
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006889 return FALSE;
6890
6891 if (vim_strchr(p_cpo, CPO_CHDIR) != NULL && curbufIsChanged() && !forceit)
6892 {
6893 emsg(_("E747: Cannot change directory, buffer is modified (add ! to override)"));
6894 return FALSE;
6895 }
6896
6897 // ":cd -": Change to previous directory
6898 if (STRCMP(new_dir, "-") == 0)
6899 {
Bram Moolenaar002bc792020-06-05 22:33:42 +02006900 pdir = get_prevdir(scope);
6901 if (pdir == NULL)
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006902 {
6903 emsg(_("E186: No previous directory"));
6904 return FALSE;
6905 }
Bram Moolenaar002bc792020-06-05 22:33:42 +02006906 new_dir = pdir;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006907 }
6908
Bram Moolenaar002bc792020-06-05 22:33:42 +02006909 // Free the previous directory
6910 tofree = get_prevdir(scope);
6911
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006912 // Save current directory for next ":cd -"
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006913 if (mch_dirname(NameBuff, MAXPATHL) == OK)
Bram Moolenaar002bc792020-06-05 22:33:42 +02006914 pdir = vim_strsave(NameBuff);
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006915 else
Bram Moolenaar002bc792020-06-05 22:33:42 +02006916 pdir = NULL;
6917 if (scope == CDSCOPE_WINDOW)
6918 curwin->w_prevdir = pdir;
6919 else if (scope == CDSCOPE_TABPAGE)
6920 curtab->tp_prevdir = pdir;
6921 else
6922 prev_dir = pdir;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006923
6924#if defined(UNIX) || defined(VMS)
6925 // for UNIX ":cd" means: go to home directory
6926 if (*new_dir == NUL)
6927 {
6928 // use NameBuff for home directory name
6929# ifdef VMS
6930 char_u *p;
6931
6932 p = mch_getenv((char_u *)"SYS$LOGIN");
6933 if (p == NULL || *p == NUL) // empty is the same as not set
6934 NameBuff[0] = NUL;
6935 else
6936 vim_strncpy(NameBuff, p, MAXPATHL - 1);
6937# else
6938 expand_env((char_u *)"$HOME", NameBuff, MAXPATHL);
6939# endif
6940 new_dir = NameBuff;
6941 }
6942#endif
Bram Moolenaar002bc792020-06-05 22:33:42 +02006943 dir_differs = new_dir == NULL || pdir == NULL
6944 || pathcmp((char *)pdir, (char *)new_dir, -1) != 0;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006945 if (new_dir == NULL || (dir_differs && vim_chdir(new_dir)))
6946 emsg(_(e_failed));
6947 else
6948 {
6949 char_u *acmd_fname;
6950
6951 post_chdir(scope);
6952
6953 if (dir_differs)
6954 {
6955 if (scope == CDSCOPE_WINDOW)
6956 acmd_fname = (char_u *)"window";
6957 else if (scope == CDSCOPE_TABPAGE)
6958 acmd_fname = (char_u *)"tabpage";
6959 else
6960 acmd_fname = (char_u *)"global";
6961 apply_autocmds(EVENT_DIRCHANGED, acmd_fname, new_dir, FALSE,
6962 curbuf);
6963 }
6964 retval = TRUE;
6965 }
6966 vim_free(tofree);
6967
6968 return retval;
6969}
Bram Moolenaarea408852005-06-25 22:49:46 +00006970
Bram Moolenaar071d4272004-06-13 20:20:40 +00006971/*
Bram Moolenaar00aa0692019-04-27 20:37:57 +02006972 * ":cd", ":tcd", ":lcd", ":chdir" ":tchdir" and ":lchdir".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006973 */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00006974 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006975ex_cd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006976{
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01006977 char_u *new_dir;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006978
6979 new_dir = eap->arg;
6980#if !defined(UNIX) && !defined(VMS)
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006981 // for non-UNIX ":cd" means: print current directory
Bram Moolenaar071d4272004-06-13 20:20:40 +00006982 if (*new_dir == NUL)
6983 ex_pwd(NULL);
6984 else
6985#endif
6986 {
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006987 cdscope_T scope = CDSCOPE_GLOBAL;
6988
6989 if (eap->cmdidx == CMD_lcd || eap->cmdidx == CMD_lchdir)
6990 scope = CDSCOPE_WINDOW;
6991 else if (eap->cmdidx == CMD_tcd || eap->cmdidx == CMD_tchdir)
6992 scope = CDSCOPE_TABPAGE;
6993
6994 if (changedir_func(new_dir, eap->forceit, scope))
Bram Moolenaardf177f62005-02-22 08:39:57 +00006995 {
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006996 // Echo the new current directory if the command was typed.
Bram Moolenaarfcfbc672009-07-09 18:13:49 +00006997 if (KeyTyped || p_verbose >= 5)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006998 ex_pwd(eap);
6999 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007000 }
7001}
7002
7003/*
7004 * ":pwd".
7005 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007006 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007007ex_pwd(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007008{
7009 if (mch_dirname(NameBuff, MAXPATHL) == OK)
7010 {
7011#ifdef BACKSLASH_IN_FILENAME
7012 slash_adjust(NameBuff);
7013#endif
Bram Moolenaar95058722020-06-01 16:26:19 +02007014 if (p_verbose > 0)
7015 {
7016 char *context = "global";
7017
7018 if (curwin->w_localdir != NULL)
7019 context = "window";
7020 else if (curtab->tp_localdir != NULL)
7021 context = "tabpage";
7022 smsg("[%s] %s", context, (char *)NameBuff);
7023 }
7024 else
7025 msg((char *)NameBuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007026 }
7027 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007028 emsg(_("E187: Unknown"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007029}
7030
7031/*
7032 * ":=".
7033 */
7034 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007035ex_equal(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007036{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007037 smsg("%ld", (long)eap->line2);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007038 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007039}
7040
7041 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007042ex_sleep(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007043{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007044 int n;
7045 long len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007046
7047 if (cursor_valid())
7048 {
7049 n = W_WINROW(curwin) + curwin->w_wrow - msg_scrolled;
7050 if (n >= 0)
Bram Moolenaar53f81742017-09-22 14:35:51 +02007051 windgoto((int)n, curwin->w_wincol + curwin->w_wcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007052 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007053
7054 len = eap->line2;
7055 switch (*eap->arg)
7056 {
7057 case 'm': break;
7058 case NUL: len *= 1000L; break;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007059 default: semsg(_(e_invarg2), eap->arg); return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007060 }
7061 do_sleep(len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007062}
7063
7064/*
7065 * Sleep for "msec" milliseconds, but keep checking for a CTRL-C every second.
7066 */
7067 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007068do_sleep(long msec)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007069{
Bram Moolenaar52953192019-08-16 10:27:13 +02007070 long done = 0;
Bram Moolenaar975b5272016-03-15 23:10:59 +01007071 long wait_now;
Bram Moolenaar52953192019-08-16 10:27:13 +02007072# ifdef ELAPSED_FUNC
7073 elapsed_T start_tv;
7074
7075 // Remember at what time we started, so that we know how much longer we
7076 // should wait after waiting for a bit.
7077 ELAPSED_INIT(start_tv);
7078# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007079
7080 cursor_on();
Bram Moolenaarf45d7472018-09-30 18:22:26 +02007081 out_flush_cursor(FALSE, FALSE);
Bram Moolenaar52953192019-08-16 10:27:13 +02007082 while (!got_int && done < msec)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007083 {
Bram Moolenaar975b5272016-03-15 23:10:59 +01007084 wait_now = msec - done > 1000L ? 1000L : msec - done;
7085#ifdef FEAT_TIMERS
7086 {
7087 long due_time = check_due_timer();
7088
7089 if (due_time > 0 && due_time < wait_now)
7090 wait_now = due_time;
7091 }
7092#endif
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02007093#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar28e67e02019-08-15 23:05:49 +02007094 if (has_any_channel() && wait_now > 20L)
7095 wait_now = 20L;
7096#endif
7097#ifdef FEAT_SOUND
7098 if (has_any_sound_callback() && wait_now > 20L)
7099 wait_now = 20L;
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02007100#endif
Bram Moolenaar975b5272016-03-15 23:10:59 +01007101 ui_delay(wait_now, TRUE);
Bram Moolenaar52953192019-08-16 10:27:13 +02007102
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02007103#ifdef FEAT_JOB_CHANNEL
7104 if (has_any_channel())
7105 ui_breakcheck_force(TRUE);
7106 else
7107#endif
7108 ui_breakcheck();
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007109#ifdef MESSAGE_QUEUE
Bram Moolenaar52953192019-08-16 10:27:13 +02007110 // Process the netbeans and clientserver messages that may have been
7111 // received in the call to ui_breakcheck() when the GUI is in use. This
7112 // may occur when running a test case.
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007113 parse_queued_messages();
Bram Moolenaare3cc6d42011-10-20 21:58:34 +02007114#endif
Bram Moolenaar52953192019-08-16 10:27:13 +02007115
7116# ifdef ELAPSED_FUNC
7117 // actual time passed
7118 done = ELAPSED_FUNC(start_tv);
7119# else
7120 // guestimate time passed (will actually be more)
7121 done += wait_now;
7122# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007123 }
Bram Moolenaar1ebff3d2018-07-07 16:18:13 +02007124
7125 // If CTRL-C was typed to interrupt the sleep, drop the CTRL-C from the
7126 // input buffer, otherwise a following call to input() fails.
7127 if (got_int)
7128 (void)vpeekc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007129}
7130
Bram Moolenaar071d4272004-06-13 20:20:40 +00007131/*
7132 * ":winsize" command (obsolete).
7133 */
7134 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007135ex_winsize(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007136{
7137 int w, h;
7138 char_u *arg = eap->arg;
7139 char_u *p;
7140
7141 w = getdigits(&arg);
7142 arg = skipwhite(arg);
7143 p = arg;
7144 h = getdigits(&arg);
7145 if (*p != NUL && *arg == NUL)
7146 set_shellsize(w, h, TRUE);
7147 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007148 emsg(_("E465: :winsize requires two number arguments"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007149}
7150
Bram Moolenaar071d4272004-06-13 20:20:40 +00007151 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007152ex_wincmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007153{
7154 int xchar = NUL;
7155 char_u *p;
7156
7157 if (*eap->arg == 'g' || *eap->arg == Ctrl_G)
7158 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007159 // CTRL-W g and CTRL-W CTRL-G have an extra command character
Bram Moolenaar071d4272004-06-13 20:20:40 +00007160 if (eap->arg[1] == NUL)
7161 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007162 emsg(_(e_invarg));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007163 return;
7164 }
7165 xchar = eap->arg[1];
7166 p = eap->arg + 2;
7167 }
7168 else
7169 p = eap->arg + 1;
7170
7171 eap->nextcmd = check_nextcmd(p);
7172 p = skipwhite(p);
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02007173 if (*p != NUL && *p != (
7174#ifdef FEAT_EVAL
7175 in_vim9script() ? '#' :
7176#endif
7177 '"')
7178 && eap->nextcmd == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007179 emsg(_(e_invarg));
Bram Moolenaar12bde492011-06-13 01:19:56 +02007180 else if (!eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007181 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007182 // Pass flags on for ":vertical wincmd ]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00007183 postponed_split_flags = cmdmod.split;
Bram Moolenaard326ce82007-03-11 14:48:29 +00007184 postponed_split_tab = cmdmod.tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007185 do_window(*eap->arg, eap->addr_count > 0 ? eap->line2 : 0L, xchar);
7186 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00007187 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007188 }
7189}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007190
Bram Moolenaar843ee412004-06-30 16:16:41 +00007191#if defined(FEAT_GUI) || defined(UNIX) || defined(VMS) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007192/*
7193 * ":winpos".
7194 */
7195 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007196ex_winpos(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007197{
7198 int x, y;
7199 char_u *arg = eap->arg;
7200 char_u *p;
7201
7202 if (*arg == NUL)
7203 {
Bram Moolenaar843ee412004-06-30 16:16:41 +00007204# if defined(FEAT_GUI) || defined(MSWIN)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007205# ifdef VIMDLL
7206 if (gui.in_use ? gui_mch_get_winpos(&x, &y) != FAIL :
7207 mch_get_winpos(&x, &y) != FAIL)
7208# elif defined(FEAT_GUI)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007209 if (gui.in_use && gui_mch_get_winpos(&x, &y) != FAIL)
Bram Moolenaar843ee412004-06-30 16:16:41 +00007210# else
7211 if (mch_get_winpos(&x, &y) != FAIL)
7212# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007213 {
7214 sprintf((char *)IObuff, _("Window position: X %d, Y %d"), x, y);
Bram Moolenaar32526b32019-01-19 17:43:09 +01007215 msg((char *)IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007216 }
7217 else
7218# endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007219 emsg(_("E188: Obtaining window position not implemented for this platform"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007220 }
7221 else
7222 {
7223 x = getdigits(&arg);
7224 arg = skipwhite(arg);
7225 p = arg;
7226 y = getdigits(&arg);
7227 if (*p == NUL || *arg != NUL)
7228 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007229 emsg(_("E466: :winpos requires two number arguments"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007230 return;
7231 }
7232# ifdef FEAT_GUI
7233 if (gui.in_use)
7234 gui_mch_set_winpos(x, y);
7235 else if (gui.starting)
7236 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007237 // Remember the coordinates for when the window is opened.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007238 gui_win_x = x;
7239 gui_win_y = y;
7240 }
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007241# if defined(HAVE_TGETENT) || defined(VIMDLL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007242 else
7243# endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007244# endif
7245# if defined(MSWIN) && (!defined(FEAT_GUI) || defined(VIMDLL))
Bram Moolenaar843ee412004-06-30 16:16:41 +00007246 mch_set_winpos(x, y);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007247# endif
7248# ifdef HAVE_TGETENT
7249 if (*T_CWP)
7250 term_set_winpos(x, y);
7251# endif
7252 }
7253}
7254#endif
7255
7256/*
7257 * Handle command that work like operators: ":delete", ":yank", ":>" and ":<".
7258 */
7259 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007260ex_operators(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007261{
7262 oparg_T oa;
7263
7264 clear_oparg(&oa);
7265 oa.regname = eap->regname;
7266 oa.start.lnum = eap->line1;
7267 oa.end.lnum = eap->line2;
7268 oa.line_count = eap->line2 - eap->line1 + 1;
7269 oa.motion_type = MLINE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007270 virtual_op = FALSE;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007271 if (eap->cmdidx != CMD_yank) // position cursor for undo
Bram Moolenaar071d4272004-06-13 20:20:40 +00007272 {
7273 setpcmark();
7274 curwin->w_cursor.lnum = eap->line1;
7275 beginline(BL_SOL | BL_FIX);
7276 }
7277
Bram Moolenaard07c6e12013-11-21 14:21:40 +01007278 if (VIsual_active)
7279 end_visual_mode();
Bram Moolenaard07c6e12013-11-21 14:21:40 +01007280
Bram Moolenaar071d4272004-06-13 20:20:40 +00007281 switch (eap->cmdidx)
7282 {
7283 case CMD_delete:
7284 oa.op_type = OP_DELETE;
7285 op_delete(&oa);
7286 break;
7287
7288 case CMD_yank:
7289 oa.op_type = OP_YANK;
7290 (void)op_yank(&oa, FALSE, TRUE);
7291 break;
7292
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007293 default: // CMD_rshift or CMD_lshift
Bram Moolenaar6e707362013-06-14 19:15:58 +02007294 if (
Bram Moolenaar071d4272004-06-13 20:20:40 +00007295#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6e707362013-06-14 19:15:58 +02007296 (eap->cmdidx == CMD_rshift) ^ curwin->w_p_rl
7297#else
7298 eap->cmdidx == CMD_rshift
Bram Moolenaar071d4272004-06-13 20:20:40 +00007299#endif
Bram Moolenaar6e707362013-06-14 19:15:58 +02007300 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00007301 oa.op_type = OP_RSHIFT;
7302 else
7303 oa.op_type = OP_LSHIFT;
7304 op_shift(&oa, FALSE, eap->amount);
7305 break;
7306 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007307 virtual_op = MAYBE;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007308 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007309}
7310
7311/*
7312 * ":put".
7313 */
7314 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007315ex_put(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007316{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007317 // ":0put" works like ":1put!".
Bram Moolenaar071d4272004-06-13 20:20:40 +00007318 if (eap->line2 == 0)
7319 {
7320 eap->line2 = 1;
7321 eap->forceit = TRUE;
7322 }
7323 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaarc3516f72020-09-08 22:45:35 +02007324 do_put(eap->regname, NULL, eap->forceit ? BACKWARD : FORWARD, 1L,
Bram Moolenaardf177f62005-02-22 08:39:57 +00007325 PUT_LINE|PUT_CURSLINE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007326}
7327
7328/*
7329 * Handle ":copy" and ":move".
7330 */
7331 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007332ex_copymove(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007333{
7334 long n;
7335
Bram Moolenaar491799b2020-08-01 19:23:43 +02007336#ifdef FEAT_EVAL
Bram Moolenaarf5a48012020-08-01 17:00:03 +02007337 if (not_in_vim9(eap) == FAIL)
7338 return;
Bram Moolenaar491799b2020-08-01 19:23:43 +02007339#endif
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02007340 n = get_address(eap, &eap->arg, eap->addr_type, FALSE, FALSE, FALSE, 1);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007341 if (eap->arg == NULL) // error detected
Bram Moolenaar071d4272004-06-13 20:20:40 +00007342 {
7343 eap->nextcmd = NULL;
7344 return;
7345 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00007346 get_flags(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007347
7348 /*
7349 * move or copy lines from 'eap->line1'-'eap->line2' to below line 'n'
7350 */
7351 if (n == MAXLNUM || n < 0 || n > curbuf->b_ml.ml_line_count)
7352 {
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02007353 emsg(_(e_invrange));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007354 return;
7355 }
7356
7357 if (eap->cmdidx == CMD_move)
7358 {
7359 if (do_move(eap->line1, eap->line2, n) == FAIL)
7360 return;
7361 }
7362 else
7363 ex_copy(eap->line1, eap->line2, n);
7364 u_clearline();
7365 beginline(BL_SOL | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007366 ex_may_print(eap);
7367}
7368
7369/*
7370 * Print the current line if flags were given to the Ex command.
7371 */
Bram Moolenaarfd3fe982014-04-01 17:49:44 +02007372 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007373ex_may_print(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007374{
7375 if (eap->flags != 0)
7376 {
7377 print_line(curwin->w_cursor.lnum, (eap->flags & EXFLAG_NR),
7378 (eap->flags & EXFLAG_LIST));
7379 ex_no_reprint = TRUE;
7380 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007381}
7382
7383/*
7384 * ":smagic" and ":snomagic".
7385 */
7386 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007387ex_submagic(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007388{
7389 int magic_save = p_magic;
7390
7391 p_magic = (eap->cmdidx == CMD_smagic);
Bram Moolenaar66e00142020-08-12 21:58:12 +02007392 ex_substitute(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007393 p_magic = magic_save;
7394}
7395
7396/*
7397 * ":join".
7398 */
7399 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007400ex_join(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007401{
7402 curwin->w_cursor.lnum = eap->line1;
7403 if (eap->line1 == eap->line2)
7404 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007405 if (eap->addr_count >= 2) // :2,2join does nothing
Bram Moolenaar071d4272004-06-13 20:20:40 +00007406 return;
7407 if (eap->line2 == curbuf->b_ml.ml_line_count)
7408 {
7409 beep_flush();
7410 return;
7411 }
7412 ++eap->line2;
7413 }
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02007414 (void)do_join(eap->line2 - eap->line1 + 1, !eap->forceit, TRUE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007415 beginline(BL_WHITE | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007416 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007417}
7418
7419/*
7420 * ":[addr]@r" or ":[addr]*r": execute register
7421 */
7422 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007423ex_at(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007424{
7425 int c;
Bram Moolenaar60462872009-11-03 11:40:19 +00007426 int prev_len = typebuf.tb_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007427
7428 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaar4930a762016-09-11 14:39:53 +02007429 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007430
7431#ifdef USE_ON_FLY_SCROLL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007432 dont_scroll = TRUE; // disallow scrolling here
Bram Moolenaar071d4272004-06-13 20:20:40 +00007433#endif
7434
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007435 // get the register name. No name means to use the previous one
Bram Moolenaar071d4272004-06-13 20:20:40 +00007436 c = *eap->arg;
7437 if (c == NUL || (c == '*' && *eap->cmd == '*'))
7438 c = '@';
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007439 // Put the register in the typeahead buffer with the "silent" flag.
Bram Moolenaard333d1e2006-11-07 17:43:47 +00007440 if (do_execreg(c, TRUE, vim_strchr(p_cpo, CPO_EXECBUF) != NULL, TRUE)
7441 == FAIL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007442 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007443 beep_flush();
Bram Moolenaardf177f62005-02-22 08:39:57 +00007444 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007445 else
7446 {
7447 int save_efr = exec_from_reg;
7448
7449 exec_from_reg = TRUE;
7450
7451 /*
7452 * Execute from the typeahead buffer.
Bram Moolenaar60462872009-11-03 11:40:19 +00007453 * Continue until the stuff buffer is empty and all added characters
7454 * have been consumed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007455 */
Bram Moolenaar60462872009-11-03 11:40:19 +00007456 while (!stuff_empty() || typebuf.tb_len > prev_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007457 (void)do_cmdline(NULL, getexline, NULL, DOCMD_NOWAIT|DOCMD_VERBOSE);
7458
7459 exec_from_reg = save_efr;
7460 }
7461}
7462
7463/*
7464 * ":!".
7465 */
7466 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007467ex_bang(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007468{
7469 do_bang(eap->addr_count, eap, eap->forceit, TRUE, TRUE);
7470}
7471
7472/*
7473 * ":undo".
7474 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007475 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +01007476ex_undo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007477{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007478 if (eap->addr_count == 1) // :undo 123
Bram Moolenaar730cde92010-06-27 05:18:54 +02007479 undo_time(eap->line2, FALSE, FALSE, TRUE);
Bram Moolenaard3667a22006-03-16 21:35:52 +00007480 else
7481 u_undo(1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007482}
7483
Bram Moolenaar55debbe2010-05-23 23:34:36 +02007484#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar6df6f472010-07-18 18:04:50 +02007485 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007486ex_wundo(exarg_T *eap)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02007487{
7488 char_u hash[UNDO_HASH_SIZE];
7489
7490 u_compute_hash(hash);
7491 u_write_undo(eap->arg, eap->forceit, curbuf, hash);
7492}
7493
Bram Moolenaar6df6f472010-07-18 18:04:50 +02007494 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007495ex_rundo(exarg_T *eap)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02007496{
7497 char_u hash[UNDO_HASH_SIZE];
7498
7499 u_compute_hash(hash);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02007500 u_read_undo(eap->arg, hash, NULL);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02007501}
7502#endif
7503
Bram Moolenaar071d4272004-06-13 20:20:40 +00007504/*
7505 * ":redo".
7506 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007507 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007508ex_redo(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007509{
7510 u_redo(1);
7511}
7512
7513/*
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007514 * ":earlier" and ":later".
7515 */
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007516 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007517ex_later(exarg_T *eap)
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007518{
7519 long count = 0;
7520 int sec = FALSE;
Bram Moolenaar730cde92010-06-27 05:18:54 +02007521 int file = FALSE;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007522 char_u *p = eap->arg;
7523
7524 if (*p == NUL)
7525 count = 1;
7526 else if (isdigit(*p))
7527 {
7528 count = getdigits(&p);
7529 switch (*p)
7530 {
7531 case 's': ++p; sec = TRUE; break;
7532 case 'm': ++p; sec = TRUE; count *= 60; break;
7533 case 'h': ++p; sec = TRUE; count *= 60 * 60; break;
Bram Moolenaar730cde92010-06-27 05:18:54 +02007534 case 'd': ++p; sec = TRUE; count *= 24 * 60 * 60; break;
7535 case 'f': ++p; file = TRUE; break;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007536 }
7537 }
7538
7539 if (*p != NUL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007540 semsg(_(e_invarg2), eap->arg);
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007541 else
Bram Moolenaar730cde92010-06-27 05:18:54 +02007542 undo_time(eap->cmdidx == CMD_earlier ? -count : count,
7543 sec, file, FALSE);
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007544}
7545
7546/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007547 * ":redir": start/stop redirection.
7548 */
7549 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007550ex_redir(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007551{
7552 char *mode;
7553 char_u *fname;
Bram Moolenaarca472992005-01-21 11:46:23 +00007554 char_u *arg = eap->arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007555
Bram Moolenaarba768492016-07-08 15:32:54 +02007556#ifdef FEAT_EVAL
Bram Moolenaar79815f12016-07-09 17:07:29 +02007557 if (redir_execute)
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02007558 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007559 emsg(_("E930: Cannot use :redir inside execute()"));
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02007560 return;
7561 }
Bram Moolenaarba768492016-07-08 15:32:54 +02007562#endif
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02007563
Bram Moolenaar071d4272004-06-13 20:20:40 +00007564 if (STRICMP(eap->arg, "END") == 0)
7565 close_redir();
7566 else
7567 {
Bram Moolenaarca472992005-01-21 11:46:23 +00007568 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00007569 {
Bram Moolenaarca472992005-01-21 11:46:23 +00007570 ++arg;
7571 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00007572 {
Bram Moolenaarca472992005-01-21 11:46:23 +00007573 ++arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007574 mode = "a";
7575 }
7576 else
7577 mode = "w";
Bram Moolenaarca472992005-01-21 11:46:23 +00007578 arg = skipwhite(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007579
7580 close_redir();
7581
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007582 // Expand environment variables and "~/".
Bram Moolenaarca472992005-01-21 11:46:23 +00007583 fname = expand_env_save(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007584 if (fname == NULL)
7585 return;
7586#ifdef FEAT_BROWSE
7587 if (cmdmod.browse)
7588 {
7589 char_u *browseFile;
7590
Bram Moolenaar7171abe2004-10-11 10:06:20 +00007591 browseFile = do_browse(BROWSE_SAVE,
7592 (char_u *)_("Save Redirection"),
Bram Moolenaarc36651b2018-04-29 12:22:56 +02007593 fname, NULL, NULL,
7594 (char_u *)_(BROWSE_FILTER_ALL_FILES), curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007595 if (browseFile == NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007596 return; // operation cancelled
Bram Moolenaar071d4272004-06-13 20:20:40 +00007597 vim_free(fname);
7598 fname = browseFile;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007599 eap->forceit = TRUE; // since dialog already asked
Bram Moolenaar071d4272004-06-13 20:20:40 +00007600 }
7601#endif
7602
7603 redir_fd = open_exfile(fname, eap->forceit, mode);
7604 vim_free(fname);
7605 }
7606#ifdef FEAT_EVAL
Bram Moolenaarca472992005-01-21 11:46:23 +00007607 else if (*arg == '@')
Bram Moolenaar071d4272004-06-13 20:20:40 +00007608 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007609 // redirect to a register a-z (resp. A-Z for appending)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007610 close_redir();
Bram Moolenaarca472992005-01-21 11:46:23 +00007611 ++arg;
7612 if (ASCII_ISALPHA(*arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007613# ifdef FEAT_CLIPBOARD
Bram Moolenaarca472992005-01-21 11:46:23 +00007614 || *arg == '*'
Bram Moolenaar9a51c6e2006-11-14 19:25:02 +00007615 || *arg == '+'
Bram Moolenaar071d4272004-06-13 20:20:40 +00007616# endif
Bram Moolenaarca472992005-01-21 11:46:23 +00007617 || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00007618 {
Bram Moolenaarca472992005-01-21 11:46:23 +00007619 redir_reg = *arg++;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007620 if (*arg == '>' && arg[1] == '>') // append
Bram Moolenaard9d30582005-05-18 22:10:28 +00007621 arg += 2;
Bram Moolenaarc188b882007-10-19 14:20:54 +00007622 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007623 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007624 // Can use both "@a" and "@a>".
Bram Moolenaar2c29bee2005-06-01 21:46:07 +00007625 if (*arg == '>')
7626 arg++;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007627 // Make register empty when not using @A-@Z and the
7628 // command is valid.
Bram Moolenaarc188b882007-10-19 14:20:54 +00007629 if (*arg == NUL && !isupper(redir_reg))
7630 write_reg_contents(redir_reg, (char_u *)"", -1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007631 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00007632 }
7633 if (*arg != NUL)
7634 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00007635 redir_reg = 0;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007636 semsg(_(e_invarg2), eap->arg);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007637 }
7638 }
7639 else if (*arg == '=' && arg[1] == '>')
7640 {
7641 int append;
7642
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007643 // redirect to a variable
Bram Moolenaardf177f62005-02-22 08:39:57 +00007644 close_redir();
7645 arg += 2;
7646
7647 if (*arg == '>')
7648 {
7649 ++arg;
7650 append = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007651 }
7652 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00007653 append = FALSE;
7654
7655 if (var_redir_start(skipwhite(arg), append) == OK)
7656 redir_vname = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007657 }
7658#endif
7659
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007660 // TODO: redirect to a buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00007661
Bram Moolenaar071d4272004-06-13 20:20:40 +00007662 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007663 semsg(_(e_invarg2), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007664 }
Bram Moolenaar29b2d262006-09-10 19:07:28 +00007665
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007666 // Make sure redirection is not off. Can happen for cmdline completion
7667 // that indirectly invokes a command to catch its output.
Bram Moolenaar29b2d262006-09-10 19:07:28 +00007668 if (redir_fd != NULL
7669#ifdef FEAT_EVAL
7670 || redir_reg || redir_vname
7671#endif
7672 )
7673 redir_off = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007674}
7675
7676/*
7677 * ":redraw": force redraw
7678 */
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01007679 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007680ex_redraw(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007681{
7682 int r = RedrawingDisabled;
7683 int p = p_lz;
7684
7685 RedrawingDisabled = 0;
7686 p_lz = FALSE;
Bram Moolenaarabc39ab2017-03-01 18:04:05 +01007687 validate_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007688 update_topline();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007689 update_screen(eap->forceit ? CLEAR : VIsual_active ? INVERTED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007690#ifdef FEAT_TITLE
7691 if (need_maketitle)
7692 maketitle();
7693#endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007694#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
7695# ifdef VIMDLL
7696 if (!gui.in_use)
7697# endif
7698 resize_console_buf();
Bram Moolenaar78d21da2019-02-17 15:00:52 +01007699#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007700 RedrawingDisabled = r;
7701 p_lz = p;
7702
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007703 // Reset msg_didout, so that a message that's there is overwritten.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007704 msg_didout = FALSE;
7705 msg_col = 0;
7706
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007707 // No need to wait after an intentional redraw.
Bram Moolenaar56667a52013-07-17 11:54:28 +02007708 need_wait_return = FALSE;
7709
Bram Moolenaar071d4272004-06-13 20:20:40 +00007710 out_flush();
7711}
7712
7713/*
7714 * ":redrawstatus": force redraw of status line(s)
7715 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007716 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007717ex_redrawstatus(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007718{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007719 int r = RedrawingDisabled;
7720 int p = p_lz;
7721
7722 RedrawingDisabled = 0;
7723 p_lz = FALSE;
7724 if (eap->forceit)
7725 status_redraw_all();
7726 else
7727 status_redraw_curbuf();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007728 update_screen(VIsual_active ? INVERTED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007729 RedrawingDisabled = r;
7730 p_lz = p;
7731 out_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007732}
7733
Bram Moolenaare12bab32019-01-08 22:02:56 +01007734/*
7735 * ":redrawtabline": force redraw of the tabline
7736 */
7737 static void
7738ex_redrawtabline(exarg_T *eap UNUSED)
7739{
7740 int r = RedrawingDisabled;
7741 int p = p_lz;
7742
7743 RedrawingDisabled = 0;
7744 p_lz = FALSE;
7745
7746 draw_tabline();
7747
7748 RedrawingDisabled = r;
7749 p_lz = p;
7750 out_flush();
7751}
7752
Bram Moolenaar071d4272004-06-13 20:20:40 +00007753 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007754close_redir(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007755{
7756 if (redir_fd != NULL)
7757 {
7758 fclose(redir_fd);
7759 redir_fd = NULL;
7760 }
7761#ifdef FEAT_EVAL
7762 redir_reg = 0;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007763 if (redir_vname)
7764 {
7765 var_redir_stop();
7766 redir_vname = 0;
7767 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007768#endif
7769}
7770
Bram Moolenaarf96ae0b2019-07-28 15:21:55 +02007771#if (defined(FEAT_SESSION) || defined(FEAT_EVAL)) || defined(PROTO)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007772 int
Bram Moolenaarbd67aac2019-09-21 23:09:04 +02007773vim_mkdir_emsg(char_u *name, int prot UNUSED)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007774{
7775 if (vim_mkdir(name, prot) != 0)
7776 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007777 semsg(_("E739: Cannot create directory: %s"), name);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007778 return FAIL;
7779 }
7780 return OK;
7781}
7782#endif
7783
Bram Moolenaar071d4272004-06-13 20:20:40 +00007784/*
7785 * Open a file for writing for an Ex command, with some checks.
7786 * Return file descriptor, or NULL on failure.
7787 */
7788 FILE *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007789open_exfile(
7790 char_u *fname,
7791 int forceit,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007792 char *mode) // "w" for create new file or "a" for append
Bram Moolenaar071d4272004-06-13 20:20:40 +00007793{
7794 FILE *fd;
7795
7796#ifdef UNIX
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007797 // with Unix it is possible to open a directory
Bram Moolenaar071d4272004-06-13 20:20:40 +00007798 if (mch_isdir(fname))
7799 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007800 semsg(_(e_isadir2), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007801 return NULL;
7802 }
7803#endif
7804 if (!forceit && *mode != 'a' && vim_fexists(fname))
7805 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007806 semsg(_("E189: \"%s\" exists (add ! to override)"), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007807 return NULL;
7808 }
7809
7810 if ((fd = mch_fopen((char *)fname, mode)) == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007811 semsg(_("E190: Cannot open \"%s\" for writing"), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007812
7813 return fd;
7814}
7815
7816/*
7817 * ":mark" and ":k".
7818 */
7819 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007820ex_mark(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007821{
7822 pos_T pos;
7823
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007824 if (*eap->arg == NUL) // No argument?
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007825 emsg(_(e_argreq));
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007826 else if (eap->arg[1] != NUL) // more than one character?
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +02007827 semsg(_(e_trailing_arg), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007828 else
7829 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007830 pos = curwin->w_cursor; // save curwin->w_cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00007831 curwin->w_cursor.lnum = eap->line2;
7832 beginline(BL_WHITE | BL_FIX);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007833 if (setmark(*eap->arg) == FAIL) // set mark
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007834 emsg(_("E191: Argument must be a letter or forward/backward quote"));
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007835 curwin->w_cursor = pos; // restore curwin->w_cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00007836 }
7837}
7838
7839/*
7840 * Update w_topline, w_leftcol and the cursor position.
7841 */
7842 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007843update_topline_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007844{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007845 check_cursor(); // put cursor on valid line
Bram Moolenaar071d4272004-06-13 20:20:40 +00007846 update_topline();
7847 if (!curwin->w_p_wrap)
7848 validate_cursor();
7849 update_curswant();
7850}
7851
Bram Moolenaar071d4272004-06-13 20:20:40 +00007852/*
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007853 * Save the current State and go to Normal mode.
7854 * Return TRUE if the typeahead could be saved.
7855 */
7856 int
7857save_current_state(save_state_T *sst)
7858{
7859 sst->save_msg_scroll = msg_scroll;
7860 sst->save_restart_edit = restart_edit;
7861 sst->save_msg_didout = msg_didout;
7862 sst->save_State = State;
7863 sst->save_insertmode = p_im;
7864 sst->save_finish_op = finish_op;
7865 sst->save_opcount = opcount;
Bram Moolenaarcce713d2019-03-04 11:40:12 +01007866 sst->save_reg_executing = reg_executing;
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007867
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007868 msg_scroll = FALSE; // no msg scrolling in Normal mode
7869 restart_edit = 0; // don't go to Insert mode
7870 p_im = FALSE; // don't use 'insertmode'
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007871
7872 /*
7873 * Save the current typeahead. This is required to allow using ":normal"
7874 * from an event handler and makes sure we don't hang when the argument
7875 * ends with half a command.
7876 */
7877 save_typeahead(&sst->tabuf);
7878 return sst->tabuf.typebuf_valid;
7879}
7880
7881 void
7882restore_current_state(save_state_T *sst)
7883{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007884 // Restore the previous typeahead.
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007885 restore_typeahead(&sst->tabuf);
7886
7887 msg_scroll = sst->save_msg_scroll;
7888 restart_edit = sst->save_restart_edit;
7889 p_im = sst->save_insertmode;
7890 finish_op = sst->save_finish_op;
7891 opcount = sst->save_opcount;
Bram Moolenaarcce713d2019-03-04 11:40:12 +01007892 reg_executing = sst->save_reg_executing;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007893 msg_didout |= sst->save_msg_didout; // don't reset msg_didout now
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007894
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007895 // Restore the state (needed when called from a function executed for
7896 // 'indentexpr'). Update the mouse and cursor, they may have changed.
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007897 State = sst->save_State;
7898#ifdef CURSOR_SHAPE
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007899 ui_cursor_shape(); // may show different cursor shape
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007900#endif
7901}
7902
7903/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007904 * ":normal[!] {commands}": Execute normal mode commands.
7905 */
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01007906 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007907ex_normal(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007908{
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007909 save_state_T save_state;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007910 char_u *arg = NULL;
7911 int l;
7912 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007913
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00007914 if (ex_normal_lock > 0)
7915 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007916 emsg(_(e_secure));
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00007917 return;
7918 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007919 if (ex_normal_busy >= p_mmd)
7920 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007921 emsg(_("E192: Recursive use of :normal too deep"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007922 return;
7923 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007924
Bram Moolenaar071d4272004-06-13 20:20:40 +00007925 /*
7926 * vgetc() expects a CSI and K_SPECIAL to have been escaped. Don't do
7927 * this for the K_SPECIAL leading byte, otherwise special keys will not
7928 * work.
7929 */
7930 if (has_mbyte)
7931 {
7932 int len = 0;
7933
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007934 // Count the number of characters to be escaped.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007935 for (p = eap->arg; *p != NUL; ++p)
7936 {
Bram Moolenaar13505972019-01-24 15:04:48 +01007937#ifdef FEAT_GUI
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007938 if (*p == CSI) // leadbyte CSI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007939 len += 2;
Bram Moolenaar13505972019-01-24 15:04:48 +01007940#endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007941 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007942 if (*++p == K_SPECIAL // trailbyte K_SPECIAL or CSI
Bram Moolenaar13505972019-01-24 15:04:48 +01007943#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007944 || *p == CSI
Bram Moolenaar13505972019-01-24 15:04:48 +01007945#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007946 )
7947 len += 2;
7948 }
7949 if (len > 0)
7950 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02007951 arg = alloc(STRLEN(eap->arg) + len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007952 if (arg != NULL)
7953 {
7954 len = 0;
7955 for (p = eap->arg; *p != NUL; ++p)
7956 {
7957 arg[len++] = *p;
Bram Moolenaar13505972019-01-24 15:04:48 +01007958#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007959 if (*p == CSI)
7960 {
7961 arg[len++] = KS_EXTRA;
7962 arg[len++] = (int)KE_CSI;
7963 }
Bram Moolenaar13505972019-01-24 15:04:48 +01007964#endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007965 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007966 {
7967 arg[len++] = *++p;
7968 if (*p == K_SPECIAL)
7969 {
7970 arg[len++] = KS_SPECIAL;
7971 arg[len++] = KE_FILLER;
7972 }
Bram Moolenaar13505972019-01-24 15:04:48 +01007973#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007974 else if (*p == CSI)
7975 {
7976 arg[len++] = KS_EXTRA;
7977 arg[len++] = (int)KE_CSI;
7978 }
Bram Moolenaar13505972019-01-24 15:04:48 +01007979#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007980 }
7981 arg[len] = NUL;
7982 }
7983 }
7984 }
7985 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007986
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007987 ++ex_normal_busy;
7988 if (save_current_state(&save_state))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007989 {
7990 /*
7991 * Repeat the :normal command for each line in the range. When no
7992 * range given, execute it just once, without positioning the cursor
7993 * first.
7994 */
7995 do
7996 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007997 if (eap->addr_count != 0)
7998 {
7999 curwin->w_cursor.lnum = eap->line1++;
8000 curwin->w_cursor.col = 0;
Bram Moolenaard5d37532017-03-27 23:02:07 +02008001 check_cursor_moved(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008002 }
8003
Bram Moolenaar13505972019-01-24 15:04:48 +01008004 exec_normal_cmd(arg != NULL
8005 ? arg
8006 : eap->arg, eap->forceit ? REMAP_NONE : REMAP_YES, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008007 }
8008 while (eap->addr_count > 0 && eap->line1 <= eap->line2 && !got_int);
8009 }
8010
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008011 // Might not return to the main loop when in an event handler.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008012 update_topline_cursor();
8013
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008014 restore_current_state(&save_state);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008015 --ex_normal_busy;
Bram Moolenaareda73602014-11-05 09:53:23 +01008016 setmouse();
Bram Moolenaareda73602014-11-05 09:53:23 +01008017#ifdef CURSOR_SHAPE
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008018 ui_cursor_shape(); // may show different cursor shape
Bram Moolenaareda73602014-11-05 09:53:23 +01008019#endif
8020
Bram Moolenaar071d4272004-06-13 20:20:40 +00008021 vim_free(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008022}
8023
8024/*
Bram Moolenaar64969662005-12-14 21:59:55 +00008025 * ":startinsert", ":startreplace" and ":startgreplace"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008026 */
8027 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008028ex_startinsert(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008029{
Bram Moolenaarfd371682005-01-14 21:42:54 +00008030 if (eap->forceit)
8031 {
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02008032 // cursor line can be zero on startup
Bram Moolenaar09ca9322017-09-26 17:40:45 +02008033 if (!curwin->w_cursor.lnum)
8034 curwin->w_cursor.lnum = 1;
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02008035 set_cursor_for_append_to_line();
Bram Moolenaarfd371682005-01-14 21:42:54 +00008036 }
Bram Moolenaareb58a242020-04-19 18:13:19 +02008037#ifdef FEAT_TERMINAL
8038 // Ignore this when running in an active terminal.
8039 if (term_job_running(curbuf->b_term))
8040 return;
8041#endif
Bram Moolenaarfd371682005-01-14 21:42:54 +00008042
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02008043 // Ignore the command when already in Insert mode. Inserting an
8044 // expression register that invokes a function can do this.
Bram Moolenaara40c5002005-01-09 21:16:21 +00008045 if (State & INSERT)
8046 return;
8047
Bram Moolenaar64969662005-12-14 21:59:55 +00008048 if (eap->cmdidx == CMD_startinsert)
8049 restart_edit = 'a';
8050 else if (eap->cmdidx == CMD_startreplace)
8051 restart_edit = 'R';
Bram Moolenaar071d4272004-06-13 20:20:40 +00008052 else
Bram Moolenaar64969662005-12-14 21:59:55 +00008053 restart_edit = 'V';
8054
8055 if (!eap->forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008056 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00008057 if (eap->cmdidx == CMD_startinsert)
8058 restart_edit = 'i';
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02008059 curwin->w_curswant = 0; // avoid MAXCOL
Bram Moolenaar071d4272004-06-13 20:20:40 +00008060 }
8061}
8062
8063/*
8064 * ":stopinsert"
8065 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008066 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008067ex_stopinsert(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008068{
8069 restart_edit = 0;
8070 stop_insert_mode = TRUE;
Bram Moolenaarfd773e92016-04-02 19:39:16 +02008071 clearmode();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008072}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008073
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008074/*
8075 * Execute normal mode command "cmd".
8076 * "remap" can be REMAP_NONE or REMAP_YES.
8077 */
8078 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008079exec_normal_cmd(char_u *cmd, int remap, int silent)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008080{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008081 // Stuff the argument into the typeahead buffer.
Bram Moolenaar25281632016-01-21 23:32:32 +01008082 ins_typebuf(cmd, remap, 0, TRUE, silent);
Bram Moolenaar586c70c2018-10-02 16:23:58 +02008083 exec_normal(FALSE, FALSE, FALSE);
Bram Moolenaar25281632016-01-21 23:32:32 +01008084}
Bram Moolenaar25281632016-01-21 23:32:32 +01008085
Bram Moolenaar25281632016-01-21 23:32:32 +01008086/*
8087 * Execute normal_cmd() until there is no typeahead left.
Bram Moolenaar586c70c2018-10-02 16:23:58 +02008088 * When "use_vpeekc" is TRUE use vpeekc() to check for available chars.
Bram Moolenaar25281632016-01-21 23:32:32 +01008089 */
8090 void
Bram Moolenaar586c70c2018-10-02 16:23:58 +02008091exec_normal(int was_typed, int use_vpeekc, int may_use_terminal_loop UNUSED)
Bram Moolenaar25281632016-01-21 23:32:32 +01008092{
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008093 oparg_T oa;
Bram Moolenaar905dd902019-04-07 14:21:47 +02008094 int c;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008095
Bram Moolenaar905dd902019-04-07 14:21:47 +02008096 // When calling vpeekc() from feedkeys() it will return Ctrl_C when there
8097 // is nothing to get, so also check for Ctrl_C.
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008098 clear_oparg(&oa);
8099 finish_op = FALSE;
Bram Moolenaar586c70c2018-10-02 16:23:58 +02008100 while ((!stuff_empty()
8101 || ((was_typed || !typebuf_typed()) && typebuf.tb_len > 0)
Bram Moolenaar905dd902019-04-07 14:21:47 +02008102 || (use_vpeekc && (c = vpeekc()) != NUL && c != Ctrl_C))
Bram Moolenaar586c70c2018-10-02 16:23:58 +02008103 && !got_int)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008104 {
8105 update_topline_cursor();
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02008106#ifdef FEAT_TERMINAL
Bram Moolenaar655a82a2018-05-08 22:01:07 +02008107 if (may_use_terminal_loop && term_use_loop()
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02008108 && oa.op_type == OP_NOP && oa.regname == NUL
8109 && !VIsual_active)
8110 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008111 // If terminal_loop() returns OK we got a key that is handled
8112 // in Normal model. With FAIL we first need to position the
8113 // cursor and the screen needs to be redrawn.
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02008114 if (terminal_loop(TRUE) == OK)
8115 normal_cmd(&oa, TRUE);
8116 }
8117 else
8118#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008119 // execute a Normal mode cmd
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02008120 normal_cmd(&oa, TRUE);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008121 }
8122}
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008123
Bram Moolenaar071d4272004-06-13 20:20:40 +00008124#ifdef FEAT_FIND_ID
8125 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008126ex_checkpath(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008127{
8128 find_pattern_in_path(NULL, 0, 0, FALSE, FALSE, CHECK_PATH, 1L,
8129 eap->forceit ? ACTION_SHOW_ALL : ACTION_SHOW,
8130 (linenr_T)1, (linenr_T)MAXLNUM);
8131}
8132
Bram Moolenaar4033c552017-09-16 20:54:51 +02008133#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008134/*
8135 * ":psearch"
8136 */
8137 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008138ex_psearch(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008139{
8140 g_do_tagpreview = p_pvh;
8141 ex_findpat(eap);
8142 g_do_tagpreview = 0;
8143}
8144#endif
8145
8146 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008147ex_findpat(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008148{
8149 int whole = TRUE;
8150 long n;
8151 char_u *p;
8152 int action;
8153
8154 switch (cmdnames[eap->cmdidx].cmd_name[2])
8155 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008156 case 'e': // ":psearch", ":isearch" and ":dsearch"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008157 if (cmdnames[eap->cmdidx].cmd_name[0] == 'p')
8158 action = ACTION_GOTO;
8159 else
8160 action = ACTION_SHOW;
8161 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008162 case 'i': // ":ilist" and ":dlist"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008163 action = ACTION_SHOW_ALL;
8164 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008165 case 'u': // ":ijump" and ":djump"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008166 action = ACTION_GOTO;
8167 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008168 default: // ":isplit" and ":dsplit"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008169 action = ACTION_SPLIT;
8170 break;
8171 }
8172
8173 n = 1;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008174 if (vim_isdigit(*eap->arg)) // get count
Bram Moolenaar071d4272004-06-13 20:20:40 +00008175 {
8176 n = getdigits(&eap->arg);
8177 eap->arg = skipwhite(eap->arg);
8178 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008179 if (*eap->arg == '/') // Match regexp, not just whole words
Bram Moolenaar071d4272004-06-13 20:20:40 +00008180 {
8181 whole = FALSE;
8182 ++eap->arg;
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02008183 p = skip_regexp(eap->arg, '/', p_magic);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008184 if (*p)
8185 {
8186 *p++ = NUL;
8187 p = skipwhite(p);
8188
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008189 // Check for trailing illegal characters
Bram Moolenaarfaac4102020-04-20 17:46:14 +02008190 if (!ends_excmd2(eap->arg, p))
Bram Moolenaar8930caa2020-07-23 16:37:03 +02008191 eap->errmsg = ex_errmsg(e_trailing_arg, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008192 else
8193 eap->nextcmd = check_nextcmd(p);
8194 }
8195 }
8196 if (!eap->skip)
8197 find_pattern_in_path(eap->arg, 0, (int)STRLEN(eap->arg),
8198 whole, !eap->forceit,
8199 *eap->cmd == 'd' ? FIND_DEFINE : FIND_ANY,
8200 n, action, eap->line1, eap->line2);
8201}
8202#endif
8203
Bram Moolenaar071d4272004-06-13 20:20:40 +00008204
Bram Moolenaar4033c552017-09-16 20:54:51 +02008205#ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00008206/*
8207 * ":ptag", ":ptselect", ":ptjump", ":ptnext", etc.
8208 */
8209 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008210ex_ptag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008211{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008212 g_do_tagpreview = p_pvh; // will be reset to 0 in ex_tag_cmd()
Bram Moolenaar071d4272004-06-13 20:20:40 +00008213 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
8214}
8215
8216/*
8217 * ":pedit"
8218 */
8219 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008220ex_pedit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008221{
8222 win_T *curwin_save = curwin;
8223
Bram Moolenaar3a2505c2020-03-09 16:40:41 +01008224 if (ERROR_IF_ANY_POPUP_WINDOW)
8225 return;
8226
Bram Moolenaar026587b2019-08-17 15:08:00 +02008227 // Open the preview window or popup and make it the current window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008228 g_do_tagpreview = p_pvh;
Bram Moolenaar576a4a62019-08-18 15:25:17 +02008229 prepare_tagpreview(TRUE, TRUE, FALSE);
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02008230
Bram Moolenaar026587b2019-08-17 15:08:00 +02008231 // Edit the file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008232 do_exedit(eap, NULL);
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02008233
Bram Moolenaar071d4272004-06-13 20:20:40 +00008234 if (curwin != curwin_save && win_valid(curwin_save))
8235 {
Bram Moolenaar026587b2019-08-17 15:08:00 +02008236 // Return cursor to where we were
Bram Moolenaar071d4272004-06-13 20:20:40 +00008237 validate_cursor();
8238 redraw_later(VALID);
8239 win_enter(curwin_save, TRUE);
8240 }
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01008241# ifdef FEAT_PROP_POPUP
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02008242 else if (WIN_IS_POPUP(curwin))
8243 {
8244 // can't keep focus in popup window
8245 win_enter(firstwin, TRUE);
8246 }
8247# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008248 g_do_tagpreview = 0;
8249}
Bram Moolenaar4033c552017-09-16 20:54:51 +02008250#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008251
8252/*
8253 * ":stag", ":stselect" and ":stjump".
8254 */
8255 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008256ex_stag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008257{
8258 postponed_split = -1;
8259 postponed_split_flags = cmdmod.split;
Bram Moolenaard326ce82007-03-11 14:48:29 +00008260 postponed_split_tab = cmdmod.tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008261 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
8262 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00008263 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008264}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008265
8266/*
8267 * ":tag", ":tselect", ":tjump", ":tnext", etc.
8268 */
8269 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008270ex_tag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008271{
8272 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name);
8273}
8274
8275 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008276ex_tag_cmd(exarg_T *eap, char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008277{
8278 int cmd;
8279
8280 switch (name[1])
8281 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008282 case 'j': cmd = DT_JUMP; // ":tjump"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008283 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008284 case 's': cmd = DT_SELECT; // ":tselect"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008285 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008286 case 'p': cmd = DT_PREV; // ":tprevious"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008287 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008288 case 'N': cmd = DT_PREV; // ":tNext"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008289 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008290 case 'n': cmd = DT_NEXT; // ":tnext"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008291 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008292 case 'o': cmd = DT_POP; // ":pop"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008293 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008294 case 'f': // ":tfirst"
8295 case 'r': cmd = DT_FIRST; // ":trewind"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008296 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008297 case 'l': cmd = DT_LAST; // ":tlast"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008298 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008299 default: // ":tag"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008300#ifdef FEAT_CSCOPE
Bram Moolenaar7c94c262008-06-20 09:11:34 +00008301 if (p_cst && *eap->arg != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008302 {
Bram Moolenaard4db7712016-11-12 19:16:46 +01008303 ex_cstag(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008304 return;
8305 }
8306#endif
8307 cmd = DT_TAG;
8308 break;
8309 }
8310
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00008311 if (name[0] == 'l')
8312 {
8313#ifndef FEAT_QUICKFIX
8314 ex_ni(eap);
8315 return;
8316#else
8317 cmd = DT_LTAG;
8318#endif
8319 }
8320
Bram Moolenaar071d4272004-06-13 20:20:40 +00008321 do_tag(eap->arg, cmd, eap->addr_count > 0 ? (int)eap->line2 : 1,
8322 eap->forceit, TRUE);
8323}
8324
8325/*
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008326 * Check "str" for starting with a special cmdline variable.
8327 * If found return one of the SPEC_ values and set "*usedlen" to the length of
8328 * the variable. Otherwise return -1 and "*usedlen" is unchanged.
8329 */
8330 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008331find_cmdline_var(char_u *src, int *usedlen)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008332{
8333 int len;
8334 int i;
Bram Moolenaar8f0b2d42009-05-16 14:41:10 +00008335 static char *(spec_str[]) = {
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008336 "%",
8337#define SPEC_PERC 0
8338 "#",
Bram Moolenaar65f08472017-09-10 18:16:20 +02008339#define SPEC_HASH (SPEC_PERC + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008340 "<cword>", // cursor word
Bram Moolenaar65f08472017-09-10 18:16:20 +02008341#define SPEC_CWORD (SPEC_HASH + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008342 "<cWORD>", // cursor WORD
Bram Moolenaar65f08472017-09-10 18:16:20 +02008343#define SPEC_CCWORD (SPEC_CWORD + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008344 "<cexpr>", // expr under cursor
Bram Moolenaar65f08472017-09-10 18:16:20 +02008345#define SPEC_CEXPR (SPEC_CCWORD + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008346 "<cfile>", // cursor path name
Bram Moolenaar65f08472017-09-10 18:16:20 +02008347#define SPEC_CFILE (SPEC_CEXPR + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008348 "<sfile>", // ":so" file name
Bram Moolenaar65f08472017-09-10 18:16:20 +02008349#define SPEC_SFILE (SPEC_CFILE + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008350 "<slnum>", // ":so" file line number
Bram Moolenaar65f08472017-09-10 18:16:20 +02008351#define SPEC_SLNUM (SPEC_SFILE + 1)
Bram Moolenaara5d04232020-07-26 15:37:02 +02008352 "<stack>", // call stack
8353#define SPEC_STACK (SPEC_SLNUM + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008354 "<afile>", // autocommand file name
Bram Moolenaara5d04232020-07-26 15:37:02 +02008355#define SPEC_AFILE (SPEC_STACK + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008356 "<abuf>", // autocommand buffer number
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008357#define SPEC_ABUF (SPEC_AFILE + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008358 "<amatch>", // autocommand match name
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01008359#define SPEC_AMATCH (SPEC_ABUF + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008360 "<sflnum>", // script file line number
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008361#define SPEC_SFLNUM (SPEC_AMATCH + 1)
Bram Moolenaar90944302020-08-01 20:45:11 +02008362 "<SID>", // script ID: <SNR>123_
8363#define SPEC_SID (SPEC_SFLNUM + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008364#ifdef FEAT_CLIENTSERVER
8365 "<client>"
Bram Moolenaar90944302020-08-01 20:45:11 +02008366# define SPEC_CLIENT (SPEC_SID + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008367#endif
8368 };
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008369
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00008370 for (i = 0; i < (int)(sizeof(spec_str) / sizeof(char *)); ++i)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008371 {
8372 len = (int)STRLEN(spec_str[i]);
8373 if (STRNCMP(src, spec_str[i], len) == 0)
8374 {
8375 *usedlen = len;
8376 return i;
8377 }
8378 }
8379 return -1;
8380}
8381
8382/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008383 * Evaluate cmdline variables.
8384 *
8385 * change '%' to curbuf->b_ffname
8386 * '#' to curwin->w_altfile
8387 * '<cword>' to word under the cursor
8388 * '<cWORD>' to WORD under the cursor
Bram Moolenaar8071cb22019-07-12 17:58:01 +02008389 * '<cexpr>' to C-expression under the cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00008390 * '<cfile>' to path name under the cursor
8391 * '<sfile>' to sourced file name
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01008392 * '<slnum>' to sourced file line number
Bram Moolenaar071d4272004-06-13 20:20:40 +00008393 * '<afile>' to file name for autocommand
8394 * '<abuf>' to buffer number for autocommand
8395 * '<amatch>' to matching name for autocommand
8396 *
8397 * When an error is detected, "errormsg" is set to a non-NULL pointer (may be
8398 * "" for error without a message) and NULL is returned.
8399 * Returns an allocated string if a valid match was found.
8400 * Returns NULL if no match was found. "usedlen" then still contains the
8401 * number of characters to skip.
8402 */
8403 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008404eval_vars(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008405 char_u *src, // pointer into commandline
8406 char_u *srcstart, // beginning of valid memory for src
8407 int *usedlen, // characters after src that are used
8408 linenr_T *lnump, // line number for :e command, or NULL
8409 char **errormsg, // pointer to error message
8410 int *escaped) // return value has escaped white space (can
8411 // be NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008412{
8413 int i;
8414 char_u *s;
8415 char_u *result;
8416 char_u *resultbuf = NULL;
8417 int resultlen;
8418 buf_T *buf;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008419 int valid = VALID_HEAD + VALID_PATH; // assume valid result
Bram Moolenaar071d4272004-06-13 20:20:40 +00008420 int spec_idx;
Bram Moolenaarfd249462018-07-28 16:14:30 +02008421 int tilde_file = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008422 int skip_mod = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008423 char_u strbuf[30];
Bram Moolenaar071d4272004-06-13 20:20:40 +00008424
8425 *errormsg = NULL;
Bram Moolenaar63b92542007-03-27 14:57:09 +00008426 if (escaped != NULL)
8427 *escaped = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008428
8429 /*
8430 * Check if there is something to do.
8431 */
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008432 spec_idx = find_cmdline_var(src, usedlen);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008433 if (spec_idx < 0) // no match
Bram Moolenaar071d4272004-06-13 20:20:40 +00008434 {
8435 *usedlen = 1;
8436 return NULL;
8437 }
8438
8439 /*
8440 * Skip when preceded with a backslash "\%" and "\#".
8441 * Note: In "\\%" the % is also not recognized!
8442 */
8443 if (src > srcstart && src[-1] == '\\')
8444 {
8445 *usedlen = 0;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008446 STRMOVE(src - 1, src); // remove backslash
Bram Moolenaar071d4272004-06-13 20:20:40 +00008447 return NULL;
8448 }
8449
8450 /*
8451 * word or WORD under cursor
8452 */
Bram Moolenaar65f08472017-09-10 18:16:20 +02008453 if (spec_idx == SPEC_CWORD || spec_idx == SPEC_CCWORD
8454 || spec_idx == SPEC_CEXPR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008455 {
Bram Moolenaar65f08472017-09-10 18:16:20 +02008456 resultlen = find_ident_under_cursor(&result,
8457 spec_idx == SPEC_CWORD ? (FIND_IDENT | FIND_STRING)
8458 : spec_idx == SPEC_CEXPR ? (FIND_IDENT | FIND_STRING | FIND_EVAL)
8459 : FIND_STRING);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008460 if (resultlen == 0)
8461 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008462 *errormsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +00008463 return NULL;
8464 }
8465 }
8466
8467 /*
8468 * '#': Alternate file name
8469 * '%': Current file name
8470 * File name under the cursor
8471 * File name for autocommand
8472 * and following modifiers
8473 */
8474 else
8475 {
8476 switch (spec_idx)
8477 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008478 case SPEC_PERC: // '%': current file
Bram Moolenaar071d4272004-06-13 20:20:40 +00008479 if (curbuf->b_fname == NULL)
8480 {
8481 result = (char_u *)"";
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008482 valid = 0; // Must have ":p:h" to be valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00008483 }
8484 else
Bram Moolenaar00136dc2018-07-25 21:19:13 +02008485 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008486 result = curbuf->b_fname;
Bram Moolenaar00136dc2018-07-25 21:19:13 +02008487 tilde_file = STRCMP(result, "~") == 0;
8488 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008489 break;
8490
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008491 case SPEC_HASH: // '#' or "#99": alternate file
8492 if (src[1] == '#') // "##": the argument list
Bram Moolenaar071d4272004-06-13 20:20:40 +00008493 {
8494 result = arg_all();
8495 resultbuf = result;
8496 *usedlen = 2;
Bram Moolenaar63b92542007-03-27 14:57:09 +00008497 if (escaped != NULL)
8498 *escaped = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008499 skip_mod = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008500 break;
8501 }
8502 s = src + 1;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008503 if (*s == '<') // "#<99" uses v:oldfiles
Bram Moolenaard812df62008-11-09 12:46:09 +00008504 ++s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008505 i = (int)getdigits(&s);
Bram Moolenaarc312b8b2017-10-28 17:53:04 +02008506 if (s == src + 2 && src[1] == '-')
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008507 // just a minus sign, don't skip over it
Bram Moolenaarc312b8b2017-10-28 17:53:04 +02008508 s--;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008509 *usedlen = (int)(s - src); // length of what we expand
Bram Moolenaar071d4272004-06-13 20:20:40 +00008510
Bram Moolenaarc312b8b2017-10-28 17:53:04 +02008511 if (src[1] == '<' && i != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008512 {
Bram Moolenaard812df62008-11-09 12:46:09 +00008513 if (*usedlen < 2)
8514 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008515 // Should we give an error message for #<text?
Bram Moolenaard812df62008-11-09 12:46:09 +00008516 *usedlen = 1;
8517 return NULL;
8518 }
8519#ifdef FEAT_EVAL
8520 result = list_find_str(get_vim_var_list(VV_OLDFILES),
8521 (long)i);
8522 if (result == NULL)
8523 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008524 *errormsg = "";
Bram Moolenaard812df62008-11-09 12:46:09 +00008525 return NULL;
8526 }
8527#else
Bram Moolenaar8e481e82019-01-15 20:07:48 +01008528 *errormsg = _("E809: #< is not available without the +eval feature");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008529 return NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +00008530#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008531 }
8532 else
Bram Moolenaard812df62008-11-09 12:46:09 +00008533 {
Bram Moolenaarc312b8b2017-10-28 17:53:04 +02008534 if (i == 0 && src[1] == '<' && *usedlen > 1)
8535 *usedlen = 1;
Bram Moolenaard812df62008-11-09 12:46:09 +00008536 buf = buflist_findnr(i);
8537 if (buf == NULL)
8538 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008539 *errormsg = _("E194: No alternate file name to substitute for '#'");
Bram Moolenaard812df62008-11-09 12:46:09 +00008540 return NULL;
8541 }
8542 if (lnump != NULL)
8543 *lnump = ECMD_LAST;
8544 if (buf->b_fname == NULL)
8545 {
8546 result = (char_u *)"";
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008547 valid = 0; // Must have ":p:h" to be valid
Bram Moolenaard812df62008-11-09 12:46:09 +00008548 }
8549 else
Bram Moolenaar00136dc2018-07-25 21:19:13 +02008550 {
Bram Moolenaard812df62008-11-09 12:46:09 +00008551 result = buf->b_fname;
Bram Moolenaar00136dc2018-07-25 21:19:13 +02008552 tilde_file = STRCMP(result, "~") == 0;
8553 }
Bram Moolenaard812df62008-11-09 12:46:09 +00008554 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008555 break;
8556
8557#ifdef FEAT_SEARCHPATH
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008558 case SPEC_CFILE: // file name under cursor
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008559 result = file_name_at_cursor(FNAME_MESS|FNAME_HYP, 1L, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008560 if (result == NULL)
8561 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008562 *errormsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +00008563 return NULL;
8564 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008565 resultbuf = result; // remember allocated string
Bram Moolenaar071d4272004-06-13 20:20:40 +00008566 break;
8567#endif
8568
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008569 case SPEC_AFILE: // file name for autocommand
Bram Moolenaar071d4272004-06-13 20:20:40 +00008570 result = autocmd_fname;
Bram Moolenaarf6dad432008-09-18 19:29:58 +00008571 if (result != NULL && !autocmd_fname_full)
8572 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008573 // Still need to turn the fname into a full path. It is
8574 // postponed to avoid a delay when <afile> is not used.
Bram Moolenaarf6dad432008-09-18 19:29:58 +00008575 autocmd_fname_full = TRUE;
8576 result = FullName_save(autocmd_fname, FALSE);
8577 vim_free(autocmd_fname);
8578 autocmd_fname = result;
8579 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008580 if (result == NULL)
8581 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008582 *errormsg = _("E495: no autocommand file name to substitute for \"<afile>\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008583 return NULL;
8584 }
Bram Moolenaara0174af2008-01-02 20:08:25 +00008585 result = shorten_fname1(result);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008586 break;
8587
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008588 case SPEC_ABUF: // buffer number for autocommand
Bram Moolenaar071d4272004-06-13 20:20:40 +00008589 if (autocmd_bufnr <= 0)
8590 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008591 *errormsg = _("E496: no autocommand buffer number to substitute for \"<abuf>\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008592 return NULL;
8593 }
8594 sprintf((char *)strbuf, "%d", autocmd_bufnr);
8595 result = strbuf;
8596 break;
8597
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008598 case SPEC_AMATCH: // match name for autocommand
Bram Moolenaar071d4272004-06-13 20:20:40 +00008599 result = autocmd_match;
8600 if (result == NULL)
8601 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008602 *errormsg = _("E497: no autocommand match name to substitute for \"<amatch>\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008603 return NULL;
8604 }
8605 break;
8606
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008607 case SPEC_SFILE: // file name for ":so" command
Bram Moolenaara5d04232020-07-26 15:37:02 +02008608 case SPEC_STACK: // call stack
8609 result = estack_sfile(spec_idx == SPEC_SFILE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008610 if (result == NULL)
8611 {
Bram Moolenaara5d04232020-07-26 15:37:02 +02008612 *errormsg = spec_idx == SPEC_SFILE
8613 ? _("E498: no :source file name to substitute for \"<sfile>\"")
8614 : _("E489: no call stack to substitute for \"<stack>\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008615 return NULL;
8616 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01008617 resultbuf = result; // remember allocated string
Bram Moolenaar071d4272004-06-13 20:20:40 +00008618 break;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008619
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008620 case SPEC_SLNUM: // line in file for ":so" command
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01008621 if (SOURCING_NAME == NULL || SOURCING_LNUM == 0)
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01008622 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008623 *errormsg = _("E842: no line number to use for \"<slnum>\"");
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01008624 return NULL;
8625 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01008626 sprintf((char *)strbuf, "%ld", SOURCING_LNUM);
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01008627 result = strbuf;
8628 break;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008629
8630#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008631 case SPEC_SFLNUM: // line in script file
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01008632 if (current_sctx.sc_lnum + SOURCING_LNUM == 0)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008633 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008634 *errormsg = _("E961: no line number to use for \"<sflnum>\"");
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008635 return NULL;
8636 }
8637 sprintf((char *)strbuf, "%ld",
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01008638 (long)(current_sctx.sc_lnum + SOURCING_LNUM));
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008639 result = strbuf;
8640 break;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008641
Bram Moolenaar90944302020-08-01 20:45:11 +02008642 case SPEC_SID:
8643 if (current_sctx.sc_sid <= 0)
8644 {
8645 *errormsg = _(e_usingsid);
8646 return NULL;
8647 }
8648 sprintf((char *)strbuf, "<SNR>%d_", current_sctx.sc_sid);
8649 result = strbuf;
8650 break;
Bram Moolenaare4218b92020-08-01 21:11:38 +02008651#endif
Bram Moolenaar90944302020-08-01 20:45:11 +02008652
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008653#ifdef FEAT_CLIENTSERVER
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008654 case SPEC_CLIENT: // Source of last submitted input
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008655 sprintf((char *)strbuf, PRINTF_HEX_LONG_U,
8656 (long_u)clientWindow);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008657 result = strbuf;
8658 break;
8659#endif
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008660
Bram Moolenaar9e0f6ec2017-05-16 09:36:54 +02008661 default:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008662 result = (char_u *)""; // avoid gcc warning
Bram Moolenaar9e0f6ec2017-05-16 09:36:54 +02008663 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008664 }
8665
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008666 resultlen = (int)STRLEN(result); // length of new string
8667 if (src[*usedlen] == '<') // remove the file name extension
Bram Moolenaar071d4272004-06-13 20:20:40 +00008668 {
8669 ++*usedlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008670 if ((s = vim_strrchr(result, '.')) != NULL && s >= gettail(result))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008671 resultlen = (int)(s - result);
8672 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008673 else if (!skip_mod)
8674 {
Bram Moolenaar00136dc2018-07-25 21:19:13 +02008675 valid |= modify_fname(src, tilde_file, usedlen, &result, &resultbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008676 &resultlen);
8677 if (result == NULL)
8678 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008679 *errormsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +00008680 return NULL;
8681 }
8682 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008683 }
8684
8685 if (resultlen == 0 || valid != VALID_HEAD + VALID_PATH)
8686 {
8687 if (valid != VALID_HEAD + VALID_PATH)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008688 // xgettext:no-c-format
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008689 *errormsg = _("E499: Empty file name for '%' or '#', only works with \":p:h\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008690 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008691 *errormsg = _("E500: Evaluates to an empty string");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008692 result = NULL;
8693 }
8694 else
8695 result = vim_strnsave(result, resultlen);
8696 vim_free(resultbuf);
8697 return result;
8698}
8699
8700/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008701 * Expand the <sfile> string in "arg".
8702 *
8703 * Returns an allocated string, or NULL for any error.
8704 */
8705 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008706expand_sfile(char_u *arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008707{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008708 char *errormsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008709 int len;
8710 char_u *result;
8711 char_u *newres;
8712 char_u *repl;
8713 int srclen;
8714 char_u *p;
8715
8716 result = vim_strsave(arg);
8717 if (result == NULL)
8718 return NULL;
8719
8720 for (p = result; *p; )
8721 {
8722 if (STRNCMP(p, "<sfile>", 7) != 0)
8723 ++p;
8724 else
8725 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008726 // replace "<sfile>" with the sourced file name, and do ":" stuff
Bram Moolenaar63b92542007-03-27 14:57:09 +00008727 repl = eval_vars(p, result, &srclen, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008728 if (errormsg != NULL)
8729 {
8730 if (*errormsg)
8731 emsg(errormsg);
8732 vim_free(result);
8733 return NULL;
8734 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008735 if (repl == NULL) // no match (cannot happen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008736 {
8737 p += srclen;
8738 continue;
8739 }
8740 len = (int)STRLEN(result) - srclen + (int)STRLEN(repl) + 1;
8741 newres = alloc(len);
8742 if (newres == NULL)
8743 {
8744 vim_free(repl);
8745 vim_free(result);
8746 return NULL;
8747 }
8748 mch_memmove(newres, result, (size_t)(p - result));
8749 STRCPY(newres + (p - result), repl);
8750 len = (int)STRLEN(newres);
8751 STRCAT(newres, p + srclen);
8752 vim_free(repl);
8753 vim_free(result);
8754 result = newres;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008755 p = newres + len; // continue after the match
Bram Moolenaar071d4272004-06-13 20:20:40 +00008756 }
8757 }
8758
8759 return result;
8760}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008761
Bram Moolenaar071d4272004-06-13 20:20:40 +00008762#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG) || defined(PROTO)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00008763/*
Bram Moolenaard9462e32011-04-11 21:35:11 +02008764 * Make a dialog message in "buff[DIALOG_MSG_SIZE]".
Bram Moolenaarb765d632005-06-07 21:00:02 +00008765 * "format" must contain "%s".
Bram Moolenaar9c13b352005-05-19 20:53:52 +00008766 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008767 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008768dialog_msg(char_u *buff, char *format, char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008769{
Bram Moolenaar071d4272004-06-13 20:20:40 +00008770 if (fname == NULL)
8771 fname = (char_u *)_("Untitled");
Bram Moolenaard9462e32011-04-11 21:35:11 +02008772 vim_snprintf((char *)buff, DIALOG_MSG_SIZE, format, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008773}
8774#endif
8775
8776/*
8777 * ":behave {mswin,xterm}"
8778 */
8779 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008780ex_behave(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008781{
8782 if (STRCMP(eap->arg, "mswin") == 0)
8783 {
8784 set_option_value((char_u *)"selection", 0L, (char_u *)"exclusive", 0);
8785 set_option_value((char_u *)"selectmode", 0L, (char_u *)"mouse,key", 0);
8786 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"popup", 0);
8787 set_option_value((char_u *)"keymodel", 0L,
8788 (char_u *)"startsel,stopsel", 0);
8789 }
8790 else if (STRCMP(eap->arg, "xterm") == 0)
8791 {
8792 set_option_value((char_u *)"selection", 0L, (char_u *)"inclusive", 0);
8793 set_option_value((char_u *)"selectmode", 0L, (char_u *)"", 0);
8794 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"extend", 0);
8795 set_option_value((char_u *)"keymodel", 0L, (char_u *)"", 0);
8796 }
8797 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008798 semsg(_(e_invarg2), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008799}
8800
Bram Moolenaar071d4272004-06-13 20:20:40 +00008801static int filetype_detect = FALSE;
8802static int filetype_plugin = FALSE;
8803static int filetype_indent = FALSE;
8804
8805/*
8806 * ":filetype [plugin] [indent] {on,off,detect}"
8807 * on: Load the filetype.vim file to install autocommands for file types.
8808 * off: Load the ftoff.vim file to remove all autocommands for file types.
8809 * plugin on: load filetype.vim and ftplugin.vim
8810 * plugin off: load ftplugof.vim
8811 * indent on: load filetype.vim and indent.vim
8812 * indent off: load indoff.vim
8813 */
8814 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008815ex_filetype(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008816{
8817 char_u *arg = eap->arg;
8818 int plugin = FALSE;
8819 int indent = FALSE;
8820
8821 if (*eap->arg == NUL)
8822 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008823 // Print current status.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008824 smsg("filetype detection:%s plugin:%s indent:%s",
Bram Moolenaar071d4272004-06-13 20:20:40 +00008825 filetype_detect ? "ON" : "OFF",
8826 filetype_plugin ? (filetype_detect ? "ON" : "(on)") : "OFF",
8827 filetype_indent ? (filetype_detect ? "ON" : "(on)") : "OFF");
8828 return;
8829 }
8830
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008831 // Accept "plugin" and "indent" in any order.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008832 for (;;)
8833 {
8834 if (STRNCMP(arg, "plugin", 6) == 0)
8835 {
8836 plugin = TRUE;
8837 arg = skipwhite(arg + 6);
8838 continue;
8839 }
8840 if (STRNCMP(arg, "indent", 6) == 0)
8841 {
8842 indent = TRUE;
8843 arg = skipwhite(arg + 6);
8844 continue;
8845 }
8846 break;
8847 }
8848 if (STRCMP(arg, "on") == 0 || STRCMP(arg, "detect") == 0)
8849 {
8850 if (*arg == 'o' || !filetype_detect)
8851 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01008852 source_runtime((char_u *)FILETYPE_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008853 filetype_detect = TRUE;
8854 if (plugin)
8855 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01008856 source_runtime((char_u *)FTPLUGIN_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008857 filetype_plugin = TRUE;
8858 }
8859 if (indent)
8860 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01008861 source_runtime((char_u *)INDENT_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008862 filetype_indent = TRUE;
8863 }
8864 }
8865 if (*arg == 'd')
8866 {
Bram Moolenaar1610d052016-06-09 22:53:01 +02008867 (void)do_doautocmd((char_u *)"filetypedetect BufRead", TRUE, NULL);
Bram Moolenaara3227e22006-03-08 21:32:40 +00008868 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008869 }
8870 }
8871 else if (STRCMP(arg, "off") == 0)
8872 {
8873 if (plugin || indent)
8874 {
8875 if (plugin)
8876 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01008877 source_runtime((char_u *)FTPLUGOF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008878 filetype_plugin = FALSE;
8879 }
8880 if (indent)
8881 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01008882 source_runtime((char_u *)INDOFF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008883 filetype_indent = FALSE;
8884 }
8885 }
8886 else
8887 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01008888 source_runtime((char_u *)FTOFF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008889 filetype_detect = FALSE;
8890 }
8891 }
8892 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008893 semsg(_(e_invarg2), arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008894}
8895
8896/*
Bram Moolenaar3e545692017-06-04 19:00:32 +02008897 * ":setfiletype [FALLBACK] {name}"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008898 */
8899 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008900ex_setfiletype(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008901{
8902 if (!did_filetype)
Bram Moolenaar3e545692017-06-04 19:00:32 +02008903 {
8904 char_u *arg = eap->arg;
8905
8906 if (STRNCMP(arg, "FALLBACK ", 9) == 0)
8907 arg += 9;
8908
8909 set_option_value((char_u *)"filetype", 0L, arg, OPT_LOCAL);
8910 if (arg != eap->arg)
8911 did_filetype = FALSE;
8912 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008913}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008914
Bram Moolenaar071d4272004-06-13 20:20:40 +00008915 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008916ex_digraphs(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008917{
8918#ifdef FEAT_DIGRAPHS
8919 if (*eap->arg != NUL)
8920 putdigraph(eap->arg);
8921 else
Bram Moolenaareae8ae12018-12-14 18:53:02 +01008922 listdigraphs(eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008923#else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008924 emsg(_("E196: No digraphs in this version"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008925#endif
8926}
8927
Bram Moolenaar451fc7b2018-04-27 22:53:07 +02008928#if defined(FEAT_SEARCH_EXTRA) || defined(PROTO)
8929 void
8930set_no_hlsearch(int flag)
8931{
8932 no_hlsearch = flag;
8933# ifdef FEAT_EVAL
8934 set_vim_var_nr(VV_HLSEARCH, !no_hlsearch && p_hls);
8935# endif
8936}
8937
Bram Moolenaar071d4272004-06-13 20:20:40 +00008938/*
8939 * ":nohlsearch"
8940 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008941 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008942ex_nohlsearch(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008943{
Bram Moolenaar451fc7b2018-04-27 22:53:07 +02008944 set_no_hlsearch(TRUE);
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00008945 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008946}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008947#endif
8948
8949#ifdef FEAT_CRYPT
8950/*
8951 * ":X": Get crypt key
8952 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008953 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008954ex_X(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008955{
Bram Moolenaar3a0c9082014-11-12 15:15:42 +01008956 crypt_check_current_method();
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02008957 (void)crypt_get_key(TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008958}
8959#endif
8960
8961#ifdef FEAT_FOLDING
8962 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008963ex_fold(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008964{
8965 if (foldManualAllowed(TRUE))
8966 foldCreate(eap->line1, eap->line2);
8967}
8968
8969 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008970ex_foldopen(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008971{
8972 opFoldRange(eap->line1, eap->line2, eap->cmdidx == CMD_foldopen,
8973 eap->forceit, FALSE);
8974}
8975
8976 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008977ex_folddo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008978{
8979 linenr_T lnum;
8980
Bram Moolenaar4facea32019-10-12 20:17:40 +02008981# ifdef FEAT_CLIPBOARD
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02008982 start_global_changes();
Bram Moolenaar4facea32019-10-12 20:17:40 +02008983# endif
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02008984
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008985 // First set the marks for all lines closed/open.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008986 for (lnum = eap->line1; lnum <= eap->line2; ++lnum)
8987 if (hasFolding(lnum, NULL, NULL) == (eap->cmdidx == CMD_folddoclosed))
8988 ml_setmarked(lnum);
8989
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008990 // Execute the command on the marked lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008991 global_exe(eap->arg);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008992 ml_clearmarked(); // clear rest of the marks
Bram Moolenaar4facea32019-10-12 20:17:40 +02008993# ifdef FEAT_CLIPBOARD
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02008994 end_global_changes();
Bram Moolenaar4facea32019-10-12 20:17:40 +02008995# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008996}
8997#endif
Bram Moolenaarf5291f32017-09-14 22:55:37 +02008998
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01008999#if defined(FEAT_QUICKFIX) || defined(PROTO)
Bram Moolenaar39665952018-08-15 20:59:48 +02009000/*
9001 * Returns TRUE if the supplied Ex cmdidx is for a location list command
9002 * instead of a quickfix command.
9003 */
9004 int
9005is_loclist_cmd(int cmdidx)
9006{
Bram Moolenaar74c8be22018-08-23 22:51:40 +02009007 if (cmdidx < 0 || cmdidx >= CMD_SIZE)
Bram Moolenaar39665952018-08-15 20:59:48 +02009008 return FALSE;
9009 return cmdnames[cmdidx].cmd_name[0] == 'l';
9010}
9011#endif
9012
Bram Moolenaar4facea32019-10-12 20:17:40 +02009013#if defined(FEAT_TIMERS) || defined(PROTO)
Bram Moolenaarf5291f32017-09-14 22:55:37 +02009014 int
9015get_pressedreturn(void)
9016{
9017 return ex_pressedreturn;
9018}
9019
9020 void
9021set_pressedreturn(int val)
9022{
9023 ex_pressedreturn = val;
9024}
9025#endif