blob: bc01b69c7759af1cde94cc621a135a181361cfad [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * ex_docmd.c: functions for executing an Ex command line.
12 */
13
14#include "vim.h"
15
Bram Moolenaar071d4272004-06-13 20:20:40 +000016static int quitmore = 0;
17static int ex_pressedreturn = FALSE;
18#ifndef FEAT_PRINTER
19# define ex_hardcopy ex_ni
20#endif
21
Bram Moolenaar071d4272004-06-13 20:20:40 +000022#ifdef FEAT_EVAL
Bram Moolenaar20b23c62020-08-20 15:25:00 +020023static char_u *do_one_cmd(char_u **, int, cstack_T *, char_u *(*fgetline)(int, void *, int, getline_opt_T), void *cookie);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024#else
Bram Moolenaar20b23c62020-08-20 15:25:00 +020025static char_u *do_one_cmd(char_u **, int, char_u *(*fgetline)(int, void *, int, getline_opt_T), void *cookie);
Bram Moolenaar217e1b82019-12-01 21:41:28 +010026static int if_level = 0; // depth in :if
Bram Moolenaar071d4272004-06-13 20:20:40 +000027#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010028static void append_command(char_u *cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000029
Bram Moolenaar071d4272004-06-13 20:20:40 +000030#ifndef FEAT_MENU
31# define ex_emenu ex_ni
32# define ex_menu ex_ni
33# define ex_menutranslate ex_ni
34#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010035static void ex_autocmd(exarg_T *eap);
36static void ex_doautocmd(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010037static void ex_bunload(exarg_T *eap);
38static void ex_buffer(exarg_T *eap);
39static void ex_bmodified(exarg_T *eap);
40static void ex_bnext(exarg_T *eap);
41static void ex_bprevious(exarg_T *eap);
42static void ex_brewind(exarg_T *eap);
43static void ex_blast(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010044static char_u *getargcmd(char_u **);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010045static int getargopt(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +000046#ifndef FEAT_QUICKFIX
47# define ex_make ex_ni
Bram Moolenaar86b68352004-12-27 21:59:20 +000048# define ex_cbuffer ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +000049# define ex_cc ex_ni
50# define ex_cnext ex_ni
Bram Moolenaar3ff33112019-05-03 21:56:35 +020051# define ex_cbelow ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +000052# define ex_cfile ex_ni
53# define qf_list ex_ni
54# define qf_age ex_ni
Bram Moolenaarf6acffb2016-07-16 16:54:24 +020055# define qf_history ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +000056# define ex_helpgrep ex_ni
Bram Moolenaar86b68352004-12-27 21:59:20 +000057# define ex_vimgrep ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +000058#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +020059#if !defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +000060# define ex_cclose ex_ni
61# define ex_copen ex_ni
62# define ex_cwindow ex_ni
Bram Moolenaardcb17002016-07-07 18:58:59 +020063# define ex_cbottom ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +000064#endif
Bram Moolenaar1e015462005-09-25 22:16:38 +000065#if !defined(FEAT_QUICKFIX) || !defined(FEAT_EVAL)
66# define ex_cexpr ex_ni
67#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000068
Bram Moolenaarc2af0af2020-08-23 21:06:02 +020069static linenr_T default_address(exarg_T *eap);
Bram Moolenaarb7316892019-05-01 18:08:42 +020070static linenr_T get_address(exarg_T *, char_u **, cmd_addr_T addr_type, int skip, int silent, int to_other_file, int address_count);
Bram Moolenaarc2af0af2020-08-23 21:06:02 +020071static void address_default_all(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010072static void get_flags(exarg_T *eap);
Bram Moolenaar85363ab2010-07-18 13:58:26 +020073#if !defined(FEAT_PERL) \
74 || !defined(FEAT_PYTHON) || !defined(FEAT_PYTHON3) \
75 || !defined(FEAT_TCL) \
76 || !defined(FEAT_RUBY) \
77 || !defined(FEAT_LUA) \
78 || !defined(FEAT_MZSCHEME)
Bram Moolenaar7bb75552007-07-16 18:39:49 +000079# define HAVE_EX_SCRIPT_NI
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010080static void ex_script_ni(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +000081#endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010082static char *invalid_range(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010083static void correct_range(exarg_T *eap);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000084#ifdef FEAT_QUICKFIX
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010085static char_u *replace_makeprg(exarg_T *eap, char_u *p, char_u **cmdlinep);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000086#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010087static char_u *repl_cmdline(exarg_T *eap, char_u *src, int srclen, char_u *repl, char_u **cmdlinep);
88static void ex_highlight(exarg_T *eap);
89static void ex_colorscheme(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010090static void ex_cquit(exarg_T *eap);
91static void ex_quit_all(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010092static void ex_close(exarg_T *eap);
93static void ex_win_close(int forceit, win_T *win, tabpage_T *tp);
94static void ex_only(exarg_T *eap);
95static void ex_resize(exarg_T *eap);
96static void ex_stag(exarg_T *eap);
97static void ex_tabclose(exarg_T *eap);
98static void ex_tabonly(exarg_T *eap);
99static void ex_tabnext(exarg_T *eap);
100static void ex_tabmove(exarg_T *eap);
101static void ex_tabs(exarg_T *eap);
Bram Moolenaar4033c552017-09-16 20:54:51 +0200102#if defined(FEAT_QUICKFIX)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100103static void ex_pclose(exarg_T *eap);
104static void ex_ptag(exarg_T *eap);
105static void ex_pedit(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000106#else
107# define ex_pclose ex_ni
108# define ex_ptag ex_ni
109# define ex_pedit ex_ni
110#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100111static void ex_hide(exarg_T *eap);
112static void ex_stop(exarg_T *eap);
113static void ex_exit(exarg_T *eap);
114static void ex_print(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000115#ifdef FEAT_BYTEOFF
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100116static void ex_goto(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000117#else
118# define ex_goto ex_ni
119#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100120static void ex_shell(exarg_T *eap);
121static void ex_preserve(exarg_T *eap);
122static void ex_recover(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100123static void ex_mode(exarg_T *eap);
124static void ex_wrongmodifier(exarg_T *eap);
125static void ex_find(exarg_T *eap);
126static void ex_open(exarg_T *eap);
127static void ex_edit(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000128#ifndef FEAT_GUI
129# define ex_gui ex_nogui
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100130static void ex_nogui(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000131#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +0100132#if defined(FEAT_GUI_MSWIN) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100133static void ex_tearoff(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000134#else
135# define ex_tearoff ex_ni
136#endif
Bram Moolenaar29a2c082018-03-05 21:06:23 +0100137#if (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK) \
138 || defined(FEAT_TERM_POPUP_MENU)) && defined(FEAT_MENU)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100139static void ex_popup(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000140#else
141# define ex_popup ex_ni
142#endif
143#ifndef FEAT_GUI_MSWIN
144# define ex_simalt ex_ni
145#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +0000146#if !defined(FEAT_GUI_MSWIN) && !defined(FEAT_GUI_GTK) && !defined(FEAT_GUI_MOTIF)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000147# define gui_mch_find_dialog ex_ni
148# define gui_mch_replace_dialog ex_ni
149#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +0000150#if !defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000151# define ex_helpfind ex_ni
152#endif
153#ifndef FEAT_CSCOPE
Bram Moolenaard4db7712016-11-12 19:16:46 +0100154# define ex_cscope ex_ni
155# define ex_scscope ex_ni
156# define ex_cstag ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000157#endif
158#ifndef FEAT_SYN_HL
159# define ex_syntax ex_ni
Bram Moolenaar860cae12010-06-05 23:22:07 +0200160# define ex_ownsyntax ex_ni
Bram Moolenaarf71a3db2006-03-12 21:50:18 +0000161#endif
Bram Moolenaarf7512552013-06-06 14:55:19 +0200162#if !defined(FEAT_SYN_HL) || !defined(FEAT_PROFILE)
Bram Moolenaar8a7f5a22013-06-06 14:01:46 +0200163# define ex_syntime ex_ni
164#endif
Bram Moolenaarf71a3db2006-03-12 21:50:18 +0000165#ifndef FEAT_SPELL
Bram Moolenaarb765d632005-06-07 21:00:02 +0000166# define ex_spell ex_ni
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000167# define ex_mkspell ex_ni
Bram Moolenaarf417f2b2005-06-23 22:29:21 +0000168# define ex_spelldump ex_ni
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000169# define ex_spellinfo ex_ni
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000170# define ex_spellrepall ex_ni
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000171#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200172#ifndef FEAT_PERSISTENT_UNDO
173# define ex_rundo ex_ni
174# define ex_wundo ex_ni
175#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200176#ifndef FEAT_LUA
177# define ex_lua ex_script_ni
178# define ex_luado ex_ni
179# define ex_luafile ex_ni
180#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000181#ifndef FEAT_MZSCHEME
182# define ex_mzscheme ex_script_ni
183# define ex_mzfile ex_ni
184#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000185#ifndef FEAT_PERL
186# define ex_perl ex_script_ni
187# define ex_perldo ex_ni
188#endif
189#ifndef FEAT_PYTHON
190# define ex_python ex_script_ni
Bram Moolenaard620aa92013-05-17 16:40:06 +0200191# define ex_pydo ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000192# define ex_pyfile ex_ni
193#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200194#ifndef FEAT_PYTHON3
Bram Moolenaar368373e2010-07-19 20:46:22 +0200195# define ex_py3 ex_script_ni
Bram Moolenaar3dab2802013-05-15 18:28:13 +0200196# define ex_py3do ex_ni
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200197# define ex_py3file ex_ni
198#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +0100199#if !defined(FEAT_PYTHON) && !defined(FEAT_PYTHON3)
200# define ex_pyx ex_script_ni
201# define ex_pyxdo ex_ni
202# define ex_pyxfile ex_ni
203#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000204#ifndef FEAT_TCL
205# define ex_tcl ex_script_ni
206# define ex_tcldo ex_ni
207# define ex_tclfile ex_ni
208#endif
209#ifndef FEAT_RUBY
210# define ex_ruby ex_script_ni
211# define ex_rubydo ex_ni
212# define ex_rubyfile ex_ni
213#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000214#ifndef FEAT_KEYMAP
215# define ex_loadkeymap ex_ni
216#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100217static void ex_swapname(exarg_T *eap);
218static void ex_syncbind(exarg_T *eap);
219static void ex_read(exarg_T *eap);
220static void ex_pwd(exarg_T *eap);
221static void ex_equal(exarg_T *eap);
222static void ex_sleep(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100223static void ex_winsize(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100224static void ex_wincmd(exarg_T *eap);
Bram Moolenaar843ee412004-06-30 16:16:41 +0000225#if defined(FEAT_GUI) || defined(UNIX) || defined(VMS) || defined(MSWIN)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100226static void ex_winpos(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000227#else
228# define ex_winpos ex_ni
229#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100230static void ex_operators(exarg_T *eap);
231static void ex_put(exarg_T *eap);
232static void ex_copymove(exarg_T *eap);
233static void ex_submagic(exarg_T *eap);
234static void ex_join(exarg_T *eap);
235static void ex_at(exarg_T *eap);
236static void ex_bang(exarg_T *eap);
237static void ex_undo(exarg_T *eap);
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200238#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100239static void ex_wundo(exarg_T *eap);
240static void ex_rundo(exarg_T *eap);
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200241#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100242static void ex_redo(exarg_T *eap);
243static void ex_later(exarg_T *eap);
244static void ex_redir(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100245static void ex_redrawstatus(exarg_T *eap);
Bram Moolenaare12bab32019-01-08 22:02:56 +0100246static void ex_redrawtabline(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100247static void close_redir(void);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100248static void ex_mark(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100249static void ex_startinsert(exarg_T *eap);
250static void ex_stopinsert(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000251#ifdef FEAT_FIND_ID
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100252static void ex_checkpath(exarg_T *eap);
253static void ex_findpat(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000254#else
255# define ex_findpat ex_ni
256# define ex_checkpath ex_ni
257#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +0200258#if defined(FEAT_FIND_ID) && defined(FEAT_QUICKFIX)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100259static void ex_psearch(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000260#else
261# define ex_psearch ex_ni
262#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100263static void ex_tag(exarg_T *eap);
264static void ex_tag_cmd(exarg_T *eap, char_u *name);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000265#ifndef FEAT_EVAL
Bram Moolenaare4ce8252019-08-04 15:30:16 +0200266# define ex_break ex_ni
267# define ex_breakadd ex_ni
268# define ex_breakdel ex_ni
269# define ex_breaklist ex_ni
270# define ex_call ex_ni
271# define ex_catch ex_ni
272# define ex_compiler ex_ni
Bram Moolenaare4ce8252019-08-04 15:30:16 +0200273# define ex_continue ex_ni
274# define ex_debug ex_ni
275# define ex_debuggreedy ex_ni
Bram Moolenaar822ba242020-05-24 23:00:18 +0200276# define ex_def ex_ni
277# define ex_defcompile ex_ni
Bram Moolenaare4ce8252019-08-04 15:30:16 +0200278# define ex_delfunction ex_ni
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100279# define ex_disassemble ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000280# define ex_echo ex_ni
281# define ex_echohl ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000282# define ex_else ex_ni
Bram Moolenaare4ce8252019-08-04 15:30:16 +0200283# define ex_endfunction ex_ni
284# define ex_endif ex_ni
285# define ex_endtry ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000286# define ex_endwhile ex_ni
Bram Moolenaare4ce8252019-08-04 15:30:16 +0200287# define ex_eval ex_ni
288# define ex_execute ex_ni
289# define ex_finally ex_ni
290# define ex_finish ex_ni
291# define ex_function ex_ni
292# define ex_if ex_ni
293# define ex_let ex_ni
294# define ex_lockvar ex_ni
295# define ex_oldfiles ex_ni
296# define ex_options ex_ni
297# define ex_packadd ex_ni
298# define ex_packloadall ex_ni
299# define ex_return ex_ni
300# define ex_scriptnames ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000301# define ex_throw ex_ni
302# define ex_try ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000303# define ex_unlet ex_ni
Bram Moolenaar65c1b012005-01-31 19:02:28 +0000304# define ex_unlockvar ex_ni
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100305# define ex_vim9script ex_ni
Bram Moolenaare4ce8252019-08-04 15:30:16 +0200306# define ex_while ex_ni
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100307# define ex_import ex_ni
308# define ex_export ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000309#endif
Bram Moolenaar84538072019-07-28 14:15:42 +0200310#ifndef FEAT_SESSION
Bram Moolenaar071d4272004-06-13 20:20:40 +0000311# define ex_loadview ex_ni
312#endif
Bram Moolenaardefa0672019-07-21 19:25:37 +0200313#ifndef FEAT_VIMINFO
Bram Moolenaar071d4272004-06-13 20:20:40 +0000314# define ex_viminfo ex_ni
315#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100316static void ex_behave(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100317static void ex_filetype(exarg_T *eap);
318static void ex_setfiletype(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000319#ifndef FEAT_DIFF
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000320# define ex_diffoff ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000321# define ex_diffpatch ex_ni
322# define ex_diffgetput ex_ni
323# define ex_diffsplit ex_ni
324# define ex_diffthis ex_ni
325# define ex_diffupdate ex_ni
326#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100327static void ex_digraphs(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000328#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100329static void ex_nohlsearch(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000330#else
331# define ex_nohlsearch ex_ni
332# define ex_match ex_ni
333#endif
334#ifdef FEAT_CRYPT
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100335static void ex_X(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000336#else
337# define ex_X ex_ni
338#endif
339#ifdef FEAT_FOLDING
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100340static void ex_fold(exarg_T *eap);
341static void ex_foldopen(exarg_T *eap);
342static void ex_folddo(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000343#else
344# define ex_fold ex_ni
345# define ex_foldopen ex_ni
346# define ex_folddo ex_ni
347#endif
Bram Moolenaar13505972019-01-24 15:04:48 +0100348#if !(defined(HAVE_LOCALE_H) || defined(X_LOCALE))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000349# define ex_language ex_ni
350#endif
351#ifndef FEAT_SIGNS
352# define ex_sign ex_ni
353#endif
Bram Moolenaar009b2592004-10-24 19:18:58 +0000354#ifndef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200355# define ex_nbclose ex_ni
Bram Moolenaar009b2592004-10-24 19:18:58 +0000356# define ex_nbkey ex_ni
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200357# define ex_nbstart ex_ni
Bram Moolenaar009b2592004-10-24 19:18:58 +0000358#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000359
Bram Moolenaar071d4272004-06-13 20:20:40 +0000360#ifndef FEAT_JUMPLIST
361# define ex_jumps ex_ni
Bram Moolenaar2d358992016-06-12 21:20:54 +0200362# define ex_clearjumps ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000363# define ex_changes ex_ni
364#endif
365
Bram Moolenaar05159a02005-02-26 23:04:13 +0000366#ifndef FEAT_PROFILE
367# define ex_profile ex_ni
368#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200369#ifndef FEAT_TERMINAL
370# define ex_terminal ex_ni
371#endif
Bram Moolenaard4aa83a2019-05-09 18:59:31 +0200372#if !defined(FEAT_X11) || !defined(FEAT_XCLIPBOARD)
373# define ex_xrestore ex_ni
374#endif
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +0100375#if !defined(FEAT_PROP_POPUP)
Bram Moolenaar682725c2019-05-25 20:10:37 +0200376# define ex_popupclear ex_ni
377#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +0000378
Bram Moolenaar071d4272004-06-13 20:20:40 +0000379/*
380 * Declare cmdnames[].
381 */
382#define DO_DECLARE_EXCMD
383#include "ex_cmds.h"
Bram Moolenaar6de5e122017-04-20 21:55:44 +0200384#include "ex_cmdidxs.h"
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +0100385
Bram Moolenaar071d4272004-06-13 20:20:40 +0000386static char_u dollar_command[2] = {'$', 0};
387
388
389#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100390// Struct for storing a line inside a while/for loop
Bram Moolenaar071d4272004-06-13 20:20:40 +0000391typedef struct
392{
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100393 char_u *line; // command line
394 linenr_T lnum; // sourcing_lnum of the line
Bram Moolenaar071d4272004-06-13 20:20:40 +0000395} wcmd_T;
396
397/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000398 * Structure used to store info for line position in a while or for loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000399 * This is required, because do_one_cmd() may invoke ex_function(), which
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000400 * reads more lines that may come from the while/for loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000401 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000402struct loop_cookie
Bram Moolenaar071d4272004-06-13 20:20:40 +0000403{
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100404 garray_T *lines_gap; // growarray with line info
405 int current_line; // last read line from growarray
406 int repeating; // TRUE when looping a second time
407 // When "repeating" is FALSE use "getline" and "cookie" to get lines
Bram Moolenaar20b23c62020-08-20 15:25:00 +0200408 char_u *(*getline)(int, void *, int, getline_opt_T);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000409 void *cookie;
410};
411
Bram Moolenaar20b23c62020-08-20 15:25:00 +0200412static char_u *get_loop_line(int c, void *cookie, int indent, getline_opt_T options);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100413static int store_loop_line(garray_T *gap, char_u *line);
414static void free_cmdlines(garray_T *gap);
Bram Moolenaared203462004-06-16 11:19:22 +0000415
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100416// Struct to save a few things while debugging. Used in do_cmdline() only.
Bram Moolenaared203462004-06-16 11:19:22 +0000417struct dbg_stuff
418{
419 int trylevel;
420 int force_abort;
421 except_T *caught_stack;
422 char_u *vv_exception;
423 char_u *vv_throwpoint;
424 int did_emsg;
425 int got_int;
426 int did_throw;
427 int need_rethrow;
428 int check_cstack;
429 except_T *current_exception;
430};
431
Bram Moolenaared203462004-06-16 11:19:22 +0000432 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100433save_dbg_stuff(struct dbg_stuff *dsp)
Bram Moolenaared203462004-06-16 11:19:22 +0000434{
435 dsp->trylevel = trylevel; trylevel = 0;
436 dsp->force_abort = force_abort; force_abort = FALSE;
437 dsp->caught_stack = caught_stack; caught_stack = NULL;
438 dsp->vv_exception = v_exception(NULL);
439 dsp->vv_throwpoint = v_throwpoint(NULL);
440
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100441 // Necessary for debugging an inactive ":catch", ":finally", ":endtry"
Bram Moolenaared203462004-06-16 11:19:22 +0000442 dsp->did_emsg = did_emsg; did_emsg = FALSE;
443 dsp->got_int = got_int; got_int = FALSE;
444 dsp->did_throw = did_throw; did_throw = FALSE;
445 dsp->need_rethrow = need_rethrow; need_rethrow = FALSE;
446 dsp->check_cstack = check_cstack; check_cstack = FALSE;
447 dsp->current_exception = current_exception; current_exception = NULL;
448}
449
450 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100451restore_dbg_stuff(struct dbg_stuff *dsp)
Bram Moolenaared203462004-06-16 11:19:22 +0000452{
453 suppress_errthrow = FALSE;
454 trylevel = dsp->trylevel;
455 force_abort = dsp->force_abort;
456 caught_stack = dsp->caught_stack;
457 (void)v_exception(dsp->vv_exception);
458 (void)v_throwpoint(dsp->vv_throwpoint);
459 did_emsg = dsp->did_emsg;
460 got_int = dsp->got_int;
461 did_throw = dsp->did_throw;
462 need_rethrow = dsp->need_rethrow;
463 check_cstack = dsp->check_cstack;
464 current_exception = dsp->current_exception;
465}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000466#endif
467
Bram Moolenaar071d4272004-06-13 20:20:40 +0000468/*
469 * do_exmode(): Repeatedly get commands for the "Ex" mode, until the ":vi"
470 * command is given.
471 */
472 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100473do_exmode(
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100474 int improved) // TRUE for "improved Ex" mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000475{
476 int save_msg_scroll;
477 int prev_msg_row;
478 linenr_T prev_line;
Bram Moolenaar79518e22017-02-17 16:31:35 +0100479 varnumber_T changedtick;
Bram Moolenaardf177f62005-02-22 08:39:57 +0000480
481 if (improved)
482 exmode_active = EXMODE_VIM;
483 else
484 exmode_active = EXMODE_NORMAL;
485 State = NORMAL;
486
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100487 // When using ":global /pat/ visual" and then "Q" we return to continue
488 // the :global command.
Bram Moolenaardf177f62005-02-22 08:39:57 +0000489 if (global_busy)
490 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000491
492 save_msg_scroll = msg_scroll;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100493 ++RedrawingDisabled; // don't redisplay the window
494 ++no_wait_return; // don't wait for return
Bram Moolenaar071d4272004-06-13 20:20:40 +0000495#ifdef FEAT_GUI
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100496 // Ignore scrollbar and mouse events in Ex mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000497 ++hold_gui_events;
498#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000499
Bram Moolenaar32526b32019-01-19 17:43:09 +0100500 msg(_("Entering Ex mode. Type \"visual\" to go to Normal mode."));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000501 while (exmode_active)
502 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100503 // Check for a ":normal" command and no more characters left.
Bram Moolenaar7c626922005-02-07 22:01:03 +0000504 if (ex_normal_busy > 0 && typebuf.tb_len == 0)
505 {
506 exmode_active = FALSE;
507 break;
508 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000509 msg_scroll = TRUE;
510 need_wait_return = FALSE;
511 ex_pressedreturn = FALSE;
512 ex_no_reprint = FALSE;
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100513 changedtick = CHANGEDTICK(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000514 prev_msg_row = msg_row;
515 prev_line = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000516 if (improved)
517 {
518 cmdline_row = msg_row;
519 do_cmdline(NULL, getexline, NULL, 0);
520 }
521 else
522 do_cmdline(NULL, getexmodeline, NULL, DOCMD_NOWAIT);
523 lines_left = Rows - 1;
524
Bram Moolenaardf177f62005-02-22 08:39:57 +0000525 if ((prev_line != curwin->w_cursor.lnum
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100526 || changedtick != CHANGEDTICK(curbuf)) && !ex_no_reprint)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000527 {
Bram Moolenaardf177f62005-02-22 08:39:57 +0000528 if (curbuf->b_ml.ml_flags & ML_EMPTY)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100529 emsg(_(e_emptybuf));
Bram Moolenaardf177f62005-02-22 08:39:57 +0000530 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000531 {
Bram Moolenaardf177f62005-02-22 08:39:57 +0000532 if (ex_pressedreturn)
533 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100534 // go up one line, to overwrite the ":<CR>" line, so the
535 // output doesn't contain empty lines.
Bram Moolenaardf177f62005-02-22 08:39:57 +0000536 msg_row = prev_msg_row;
537 if (prev_msg_row == Rows - 1)
538 msg_row--;
539 }
540 msg_col = 0;
541 print_line_no_prefix(curwin->w_cursor.lnum, FALSE, FALSE);
542 msg_clr_eos();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000543 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000544 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100545 else if (ex_pressedreturn && !ex_no_reprint) // must be at EOF
Bram Moolenaardf177f62005-02-22 08:39:57 +0000546 {
547 if (curbuf->b_ml.ml_flags & ML_EMPTY)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100548 emsg(_(e_emptybuf));
Bram Moolenaardf177f62005-02-22 08:39:57 +0000549 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100550 emsg(_("E501: At end-of-file"));
Bram Moolenaardf177f62005-02-22 08:39:57 +0000551 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000552 }
553
554#ifdef FEAT_GUI
555 --hold_gui_events;
556#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000557 --RedrawingDisabled;
558 --no_wait_return;
559 update_screen(CLEAR);
560 need_wait_return = FALSE;
561 msg_scroll = save_msg_scroll;
562}
563
564/*
Bram Moolenaar4facea32019-10-12 20:17:40 +0200565 * Print the executed command for when 'verbose' is set.
566 * When "lnum" is 0 only print the command.
567 */
568 static void
569msg_verbose_cmd(linenr_T lnum, char_u *cmd)
570{
571 ++no_wait_return;
572 verbose_enter_scroll();
573
574 if (lnum == 0)
575 smsg(_("Executing: %s"), cmd);
576 else
577 smsg(_("line %ld: %s"), (long)lnum, cmd);
578 if (msg_silent == 0)
579 msg_puts("\n"); // don't overwrite this
580
581 verbose_leave_scroll();
582 --no_wait_return;
583}
584
585/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000586 * Execute a simple command line. Used for translated commands like "*".
587 */
588 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100589do_cmdline_cmd(char_u *cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000590{
591 return do_cmdline(cmd, NULL, NULL,
592 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
593}
594
595/*
596 * do_cmdline(): execute one Ex command line
597 *
598 * 1. Execute "cmdline" when it is not NULL.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100599 * If "cmdline" is NULL, or more lines are needed, fgetline() is used.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000600 * 2. Split up in parts separated with '|'.
601 *
602 * This function can be called recursively!
603 *
604 * flags:
605 * DOCMD_VERBOSE - The command will be included in the error message.
606 * DOCMD_NOWAIT - Don't call wait_return() and friends.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100607 * DOCMD_REPEAT - Repeat execution until fgetline() returns NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000608 * DOCMD_KEYTYPED - Don't reset KeyTyped.
609 * DOCMD_EXCRESET - Reset the exception environment (used for debugging).
610 * DOCMD_KEEPLINE - Store first typed line (for repeating with ".").
611 *
612 * return FAIL if cmdline could not be executed, OK otherwise
613 */
614 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100615do_cmdline(
616 char_u *cmdline,
Bram Moolenaar66250c92020-08-20 15:02:42 +0200617 char_u *(*fgetline)(int, void *, int, getline_opt_T),
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100618 void *cookie, // argument for fgetline()
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100619 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000620{
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100621 char_u *next_cmdline; // next cmd to execute
622 char_u *cmdline_copy = NULL; // copy of cmd line
623 int used_getline = FALSE; // used "fgetline" to obtain command
624 static int recursive = 0; // recursive depth
Bram Moolenaar071d4272004-06-13 20:20:40 +0000625 int msg_didout_before_start = 0;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100626 int count = 0; // line number count
627 int did_inc = FALSE; // incremented RedrawingDisabled
Bram Moolenaar071d4272004-06-13 20:20:40 +0000628 int retval = OK;
629#ifdef FEAT_EVAL
Bram Moolenaarddef1292019-12-16 17:10:33 +0100630 cstack_T cstack; // conditional stack
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100631 garray_T lines_ga; // keep lines for ":while"/":for"
632 int current_line = 0; // active line in lines_ga
Bram Moolenaard5053d02020-06-28 15:51:16 +0200633 int current_line_before = 0;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100634 char_u *fname = NULL; // function or script name
635 linenr_T *breakpoint = NULL; // ptr to breakpoint field in cookie
636 int *dbg_tick = NULL; // ptr to dbg_tick field in cookie
637 struct dbg_stuff debug_saved; // saved things for debug mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000638 int initial_trylevel;
Bram Moolenaar25e0f582020-05-25 22:36:50 +0200639 msglist_T **saved_msg_list = NULL;
640 msglist_T *private_msg_list;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000641
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100642 // "fgetline" and "cookie" passed to do_one_cmd()
Bram Moolenaar66250c92020-08-20 15:02:42 +0200643 char_u *(*cmd_getline)(int, void *, int, getline_opt_T);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000644 void *cmd_cookie;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000645 struct loop_cookie cmd_loop_cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000646 void *real_cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +0000647 int getline_is_func;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000648#else
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100649# define cmd_getline fgetline
Bram Moolenaar071d4272004-06-13 20:20:40 +0000650# define cmd_cookie cookie
651#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100652 static int call_depth = 0; // recursiveness
Bram Moolenaar2196bce2020-04-12 20:01:11 +0200653#ifdef FEAT_EVAL
Bram Moolenaare31ee862020-01-07 20:59:34 +0100654 ESTACK_CHECK_DECLARATION
Bram Moolenaar071d4272004-06-13 20:20:40 +0000655
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100656 // For every pair of do_cmdline()/do_one_cmd() calls, use an extra memory
657 // location for storing error messages to be converted to an exception.
658 // This ensures that the do_errthrow() call in do_one_cmd() does not
659 // combine the messages stored by an earlier invocation of do_one_cmd()
660 // with the command name of the later one. This would happen when
661 // BufWritePost autocommands are executed after a write error.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000662 saved_msg_list = msg_list;
663 msg_list = &private_msg_list;
664 private_msg_list = NULL;
665#endif
666
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100667 // It's possible to create an endless loop with ":execute", catch that
668 // here. The value of 200 allows nested function calls, ":source", etc.
669 // Allow 200 or 'maxfuncdepth', whatever is larger.
Bram Moolenaarb094ff42017-01-02 16:16:39 +0100670 if (call_depth >= 200
671#ifdef FEAT_EVAL
672 && call_depth >= p_mfd
673#endif
674 )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000675 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100676 emsg(_("E169: Command too recursive"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000677#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100678 // When converting to an exception, we do not include the command name
679 // since this is not an error of the specific command.
Bram Moolenaarddef1292019-12-16 17:10:33 +0100680 do_errthrow((cstack_T *)NULL, (char_u *)NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000681 msg_list = saved_msg_list;
682#endif
683 return FAIL;
684 }
685 ++call_depth;
686
687#ifdef FEAT_EVAL
688 cstack.cs_idx = -1;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000689 cstack.cs_looplevel = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000690 cstack.cs_trylevel = 0;
691 cstack.cs_emsg_silent_list = NULL;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000692 cstack.cs_lflags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000693 ga_init2(&lines_ga, (int)sizeof(wcmd_T), 10);
694
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100695 real_cookie = getline_cookie(fgetline, cookie);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000696
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100697 // Inside a function use a higher nesting level.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100698 getline_is_func = getline_equal(fgetline, cookie, get_func_line);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000699 if (getline_is_func && ex_nesting_level == func_level(real_cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000700 ++ex_nesting_level;
701
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100702 // Get the function or script name and the address where the next breakpoint
703 // line and the debug tick for a function or script are stored.
Bram Moolenaar05159a02005-02-26 23:04:13 +0000704 if (getline_is_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000705 {
706 fname = func_name(real_cookie);
707 breakpoint = func_breakpoint(real_cookie);
708 dbg_tick = func_dbg_tick(real_cookie);
709 }
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100710 else if (getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000711 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100712 fname = SOURCING_NAME;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000713 breakpoint = source_breakpoint(real_cookie);
714 dbg_tick = source_dbg_tick(real_cookie);
715 }
716
717 /*
718 * Initialize "force_abort" and "suppress_errthrow" at the top level.
719 */
720 if (!recursive)
721 {
722 force_abort = FALSE;
723 suppress_errthrow = FALSE;
724 }
725
726 /*
727 * If requested, store and reset the global values controlling the
Bram Moolenaar89d40322006-08-29 15:30:07 +0000728 * exception handling (used when debugging). Otherwise clear it to avoid
729 * a bogus compiler warning when the optimizer uses inline functions...
Bram Moolenaar071d4272004-06-13 20:20:40 +0000730 */
Bram Moolenaarb4872942006-05-13 10:32:52 +0000731 if (flags & DOCMD_EXCRESET)
Bram Moolenaared203462004-06-16 11:19:22 +0000732 save_dbg_stuff(&debug_saved);
Bram Moolenaar89d40322006-08-29 15:30:07 +0000733 else
Bram Moolenaara80faa82020-04-12 19:37:17 +0200734 CLEAR_FIELD(debug_saved);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000735
736 initial_trylevel = trylevel;
737
738 /*
739 * "did_throw" will be set to TRUE when an exception is being thrown.
740 */
741 did_throw = FALSE;
742#endif
743 /*
744 * "did_emsg" will be set to TRUE when emsg() is used, in which case we
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000745 * cancel the whole command line, and any if/endif or loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000746 * If force_abort is set, we cancel everything.
747 */
748 did_emsg = FALSE;
749
750 /*
751 * KeyTyped is only set when calling vgetc(). Reset it here when not
752 * calling vgetc() (sourced command lines).
753 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100754 if (!(flags & DOCMD_KEYTYPED)
755 && !getline_equal(fgetline, cookie, getexline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000756 KeyTyped = FALSE;
757
758 /*
759 * Continue executing command lines:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000760 * - when inside an ":if", ":while" or ":for"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000761 * - for multiple commands on one line, separated with '|'
762 * - when repeating until there are no more lines (for ":source")
763 */
764 next_cmdline = cmdline;
765 do
766 {
Bram Moolenaar05159a02005-02-26 23:04:13 +0000767#ifdef FEAT_EVAL
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100768 getline_is_func = getline_equal(fgetline, cookie, get_func_line);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000769#endif
770
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100771 // stop skipping cmds for an error msg after all endif/while/for
Bram Moolenaar071d4272004-06-13 20:20:40 +0000772 if (next_cmdline == NULL
773#ifdef FEAT_EVAL
774 && !force_abort
775 && cstack.cs_idx < 0
Bram Moolenaar05159a02005-02-26 23:04:13 +0000776 && !(getline_is_func && func_has_abort(real_cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000777#endif
778 )
779 did_emsg = FALSE;
780
781 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000782 * 1. If repeating a line in a loop, get a line from lines_ga.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100783 * 2. If no line given: Get an allocated line with fgetline().
Bram Moolenaar071d4272004-06-13 20:20:40 +0000784 * 3. If a line is given: Make a copy, so we can mess with it.
785 */
786
787#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100788 // 1. If repeating, get a previous line from lines_ga.
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000789 if (cstack.cs_looplevel > 0 && current_line < lines_ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000790 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100791 // Each '|' separated command is stored separately in lines_ga, to
792 // be able to jump to it. Don't use next_cmdline now.
Bram Moolenaard23a8232018-02-10 18:45:26 +0100793 VIM_CLEAR(cmdline_copy);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000794
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100795 // Check if a function has returned or, unless it has an unclosed
796 // try conditional, aborted.
Bram Moolenaar05159a02005-02-26 23:04:13 +0000797 if (getline_is_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000798 {
Bram Moolenaar05159a02005-02-26 23:04:13 +0000799# ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000800 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +0000801 func_line_end(real_cookie);
802# endif
803 if (func_has_ended(real_cookie))
804 {
805 retval = FAIL;
806 break;
807 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000808 }
Bram Moolenaar05159a02005-02-26 23:04:13 +0000809#ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000810 else if (do_profiling == PROF_YES
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100811 && getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar05159a02005-02-26 23:04:13 +0000812 script_line_end();
813#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000814
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100815 // Check if a sourced file hit a ":finish" command.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100816 if (source_finished(fgetline, cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000817 {
818 retval = FAIL;
819 break;
820 }
821
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100822 // If breakpoints have been added/deleted need to check for it.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000823 if (breakpoint != NULL && dbg_tick != NULL
824 && *dbg_tick != debug_tick)
825 {
826 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100827 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100828 fname, SOURCING_LNUM);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000829 *dbg_tick = debug_tick;
830 }
831
832 next_cmdline = ((wcmd_T *)(lines_ga.ga_data))[current_line].line;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100833 SOURCING_LNUM = ((wcmd_T *)(lines_ga.ga_data))[current_line].lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000834
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100835 // Did we encounter a breakpoint?
Bram Moolenaar071d4272004-06-13 20:20:40 +0000836 if (breakpoint != NULL && *breakpoint != 0
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100837 && *breakpoint <= SOURCING_LNUM)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000838 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100839 dbg_breakpoint(fname, SOURCING_LNUM);
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100840 // Find next breakpoint.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000841 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100842 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100843 fname, SOURCING_LNUM);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000844 *dbg_tick = debug_tick;
845 }
Bram Moolenaar05159a02005-02-26 23:04:13 +0000846# ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000847 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +0000848 {
849 if (getline_is_func)
850 func_line_start(real_cookie);
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100851 else if (getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar05159a02005-02-26 23:04:13 +0000852 script_line_start();
853 }
854# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000855 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000856#endif
857
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100858 // 2. If no line given, get an allocated line with fgetline().
Bram Moolenaar071d4272004-06-13 20:20:40 +0000859 if (next_cmdline == NULL)
860 {
861 /*
862 * Need to set msg_didout for the first line after an ":if",
863 * otherwise the ":if" will be overwritten.
864 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100865 if (count == 1 && getline_equal(fgetline, cookie, getexline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000866 msg_didout = TRUE;
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100867 if (fgetline == NULL || (next_cmdline = fgetline(':', cookie,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000868#ifdef FEAT_EVAL
869 cstack.cs_idx < 0 ? 0 : (cstack.cs_idx + 1) * 2
870#else
871 0
872#endif
Bram Moolenaare96a2492019-06-25 04:12:16 +0200873 , TRUE)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000874 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100875 // Don't call wait_return for aborted command line. The NULL
876 // returned for the end of a sourced file or executed function
877 // doesn't do this.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000878 if (KeyTyped && !(flags & DOCMD_REPEAT))
879 need_wait_return = FALSE;
880 retval = FAIL;
881 break;
882 }
883 used_getline = TRUE;
884
885 /*
886 * Keep the first typed line. Clear it when more lines are typed.
887 */
888 if (flags & DOCMD_KEEPLINE)
889 {
890 vim_free(repeat_cmdline);
891 if (count == 0)
892 repeat_cmdline = vim_strsave(next_cmdline);
893 else
894 repeat_cmdline = NULL;
895 }
896 }
897
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100898 // 3. Make a copy of the command so we can mess with it.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000899 else if (cmdline_copy == NULL)
900 {
901 next_cmdline = vim_strsave(next_cmdline);
902 if (next_cmdline == NULL)
903 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100904 emsg(_(e_outofmem));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000905 retval = FAIL;
906 break;
907 }
908 }
909 cmdline_copy = next_cmdline;
910
911#ifdef FEAT_EVAL
912 /*
Bram Moolenaard5053d02020-06-28 15:51:16 +0200913 * Inside a while/for loop, and when the command looks like a ":while"
914 * or ":for", the line is stored, because we may need it later when
915 * looping.
916 *
917 * When there is a '|' and another command, it is stored separately,
918 * because we need to be able to jump back to it from an
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000919 * :endwhile/:endfor.
Bram Moolenaard5053d02020-06-28 15:51:16 +0200920 *
921 * Pass a different "fgetline" function to do_one_cmd() below,
922 * that it stores lines in or reads them from "lines_ga". Makes it
923 * possible to define a function inside a while/for loop and handles
924 * line continuation.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000925 */
Bram Moolenaard5053d02020-06-28 15:51:16 +0200926 if ((cstack.cs_looplevel > 0 || has_loop_cmd(next_cmdline)))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000927 {
Bram Moolenaard5053d02020-06-28 15:51:16 +0200928 cmd_getline = get_loop_line;
929 cmd_cookie = (void *)&cmd_loop_cookie;
930 cmd_loop_cookie.lines_gap = &lines_ga;
931 cmd_loop_cookie.current_line = current_line;
932 cmd_loop_cookie.getline = fgetline;
933 cmd_loop_cookie.cookie = cookie;
934 cmd_loop_cookie.repeating = (current_line < lines_ga.ga_len);
935
936 // Save the current line when encountering it the first time.
937 if (current_line == lines_ga.ga_len
938 && store_loop_line(&lines_ga, next_cmdline) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000939 {
940 retval = FAIL;
941 break;
942 }
Bram Moolenaard5053d02020-06-28 15:51:16 +0200943 current_line_before = current_line;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000944 }
Bram Moolenaard5053d02020-06-28 15:51:16 +0200945 else
946 {
947 cmd_getline = fgetline;
948 cmd_cookie = cookie;
949 }
950
Bram Moolenaar071d4272004-06-13 20:20:40 +0000951 did_endif = FALSE;
952#endif
953
954 if (count++ == 0)
955 {
956 /*
957 * All output from the commands is put below each other, without
958 * waiting for a return. Don't do this when executing commands
959 * from a script or when being called recursive (e.g. for ":e
960 * +command file").
961 */
962 if (!(flags & DOCMD_NOWAIT) && !recursive)
963 {
964 msg_didout_before_start = msg_didout;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100965 msg_didany = FALSE; // no output yet
Bram Moolenaar071d4272004-06-13 20:20:40 +0000966 msg_start();
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100967 msg_scroll = TRUE; // put messages below each other
968 ++no_wait_return; // don't wait for return until finished
Bram Moolenaar071d4272004-06-13 20:20:40 +0000969 ++RedrawingDisabled;
970 did_inc = TRUE;
971 }
972 }
973
Bram Moolenaar823654b2020-05-29 23:03:09 +0200974 if ((p_verbose >= 15 && SOURCING_NAME != NULL) || p_verbose >= 16)
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100975 msg_verbose_cmd(SOURCING_LNUM, cmdline_copy);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000976
977 /*
978 * 2. Execute one '|' separated command.
979 * do_one_cmd() will return NULL if there is no trailing '|'.
980 * "cmdline_copy" can change, e.g. for '%' and '#' expansion.
981 */
982 ++recursive;
983 next_cmdline = do_one_cmd(&cmdline_copy, flags & DOCMD_VERBOSE,
984#ifdef FEAT_EVAL
985 &cstack,
986#endif
987 cmd_getline, cmd_cookie);
988 --recursive;
989
990#ifdef FEAT_EVAL
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000991 if (cmd_cookie == (void *)&cmd_loop_cookie)
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100992 // Use "current_line" from "cmd_loop_cookie", it may have been
993 // incremented when defining a function.
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000994 current_line = cmd_loop_cookie.current_line;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000995#endif
996
997 if (next_cmdline == NULL)
998 {
Bram Moolenaard23a8232018-02-10 18:45:26 +0100999 VIM_CLEAR(cmdline_copy);
Bram Moolenaard7663c22019-08-06 21:59:57 +02001000
Bram Moolenaar071d4272004-06-13 20:20:40 +00001001 /*
1002 * If the command was typed, remember it for the ':' register.
1003 * Do this AFTER executing the command to make :@: work.
1004 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001005 if (getline_equal(fgetline, cookie, getexline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001006 && new_last_cmdline != NULL)
1007 {
1008 vim_free(last_cmdline);
1009 last_cmdline = new_last_cmdline;
1010 new_last_cmdline = NULL;
1011 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001012 }
1013 else
1014 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001015 // need to copy the command after the '|' to cmdline_copy, for the
1016 // next do_one_cmd()
Bram Moolenaara7241f52008-06-24 20:39:31 +00001017 STRMOVE(cmdline_copy, next_cmdline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001018 next_cmdline = cmdline_copy;
1019 }
1020
1021
1022#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001023 // reset did_emsg for a function that is not aborted by an error
Bram Moolenaar071d4272004-06-13 20:20:40 +00001024 if (did_emsg && !force_abort
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001025 && getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001026 && !func_has_abort(real_cookie))
1027 did_emsg = FALSE;
1028
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001029 if (cstack.cs_looplevel > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001030 {
1031 ++current_line;
1032
1033 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001034 * An ":endwhile", ":endfor" and ":continue" is handled here.
1035 * If we were executing commands, jump back to the ":while" or
1036 * ":for".
1037 * If we were not executing commands, decrement cs_looplevel.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001038 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001039 if (cstack.cs_lflags & (CSL_HAD_CONT | CSL_HAD_ENDLOOP))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001040 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001041 cstack.cs_lflags &= ~(CSL_HAD_CONT | CSL_HAD_ENDLOOP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001042
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001043 // Jump back to the matching ":while" or ":for". Be careful
1044 // not to use a cs_line[] from an entry that isn't a ":while"
1045 // or ":for": It would make "current_line" invalid and can
1046 // cause a crash.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001047 if (!did_emsg && !got_int && !did_throw
1048 && cstack.cs_idx >= 0
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001049 && (cstack.cs_flags[cstack.cs_idx]
1050 & (CSF_WHILE | CSF_FOR))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001051 && cstack.cs_line[cstack.cs_idx] >= 0
1052 && (cstack.cs_flags[cstack.cs_idx] & CSF_ACTIVE))
1053 {
1054 current_line = cstack.cs_line[cstack.cs_idx];
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001055 // remember we jumped there
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001056 cstack.cs_lflags |= CSL_HAD_LOOP;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001057 line_breakcheck(); // check if CTRL-C typed
Bram Moolenaar071d4272004-06-13 20:20:40 +00001058
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001059 // Check for the next breakpoint at or after the ":while"
1060 // or ":for".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001061 if (breakpoint != NULL)
1062 {
1063 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001064 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001065 fname,
1066 ((wcmd_T *)lines_ga.ga_data)[current_line].lnum-1);
1067 *dbg_tick = debug_tick;
1068 }
1069 }
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001070 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001071 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001072 // can only get here with ":endwhile" or ":endfor"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001073 if (cstack.cs_idx >= 0)
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001074 rewind_conditionals(&cstack, cstack.cs_idx - 1,
1075 CSF_WHILE | CSF_FOR, &cstack.cs_looplevel);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001076 }
1077 }
1078
1079 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001080 * For a ":while" or ":for" we need to remember the line number.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001081 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001082 else if (cstack.cs_lflags & CSL_HAD_LOOP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001083 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001084 cstack.cs_lflags &= ~CSL_HAD_LOOP;
Bram Moolenaard5053d02020-06-28 15:51:16 +02001085 cstack.cs_line[cstack.cs_idx] = current_line_before;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001086 }
1087 }
1088
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001089 // Check for the next breakpoint after a watchexpression
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01001090 if (breakpoint != NULL && has_watchexpr())
1091 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001092 *breakpoint = dbg_find_breakpoint(FALSE, fname, SOURCING_LNUM);
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01001093 *dbg_tick = debug_tick;
1094 }
1095
Bram Moolenaar071d4272004-06-13 20:20:40 +00001096 /*
1097 * When not inside any ":while" loop, clear remembered lines.
1098 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001099 if (cstack.cs_looplevel == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001100 {
1101 if (lines_ga.ga_len > 0)
1102 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001103 SOURCING_LNUM =
Bram Moolenaar071d4272004-06-13 20:20:40 +00001104 ((wcmd_T *)lines_ga.ga_data)[lines_ga.ga_len - 1].lnum;
1105 free_cmdlines(&lines_ga);
1106 }
1107 current_line = 0;
1108 }
1109
1110 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001111 * A ":finally" makes did_emsg, got_int, and did_throw pending for
1112 * being restored at the ":endtry". Reset them here and set the
1113 * ACTIVE and FINALLY flags, so that the finally clause gets executed.
1114 * This includes the case where a missing ":endif", ":endwhile" or
1115 * ":endfor" was detected by the ":finally" itself.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001116 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001117 if (cstack.cs_lflags & CSL_HAD_FINA)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001118 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001119 cstack.cs_lflags &= ~CSL_HAD_FINA;
1120 report_make_pending(cstack.cs_pending[cstack.cs_idx]
1121 & (CSTP_ERROR | CSTP_INTERRUPT | CSTP_THROW),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001122 did_throw ? (void *)current_exception : NULL);
1123 did_emsg = got_int = did_throw = FALSE;
1124 cstack.cs_flags[cstack.cs_idx] |= CSF_ACTIVE | CSF_FINALLY;
1125 }
1126
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001127 // Update global "trylevel" for recursive calls to do_cmdline() from
1128 // within this loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001129 trylevel = initial_trylevel + cstack.cs_trylevel;
1130
1131 /*
Bram Moolenaarc1762cc2007-05-10 16:56:30 +00001132 * If the outermost try conditional (across function calls and sourced
Bram Moolenaar071d4272004-06-13 20:20:40 +00001133 * files) is aborted because of an error, an interrupt, or an uncaught
1134 * exception, cancel everything. If it is left normally, reset
1135 * force_abort to get the non-EH compatible abortion behavior for
1136 * the rest of the script.
1137 */
1138 if (trylevel == 0 && !did_emsg && !got_int && !did_throw)
1139 force_abort = FALSE;
1140
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001141 // Convert an interrupt to an exception if appropriate.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001142 (void)do_intthrow(&cstack);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001143#endif // FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001144
1145 }
1146 /*
1147 * Continue executing command lines when:
1148 * - no CTRL-C typed, no aborting error, no exception thrown or try
1149 * conditionals need to be checked for executing finally clauses or
1150 * catching an interrupt exception
1151 * - didn't get an error message or lines are not typed
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001152 * - there is a command after '|', inside a :if, :while, :for or :try, or
Bram Moolenaar071d4272004-06-13 20:20:40 +00001153 * looping for ":source" command or function call.
1154 */
1155 while (!((got_int
1156#ifdef FEAT_EVAL
1157 || (did_emsg && force_abort) || did_throw
1158#endif
1159 )
1160#ifdef FEAT_EVAL
1161 && cstack.cs_trylevel == 0
1162#endif
1163 )
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001164 && !(did_emsg
1165#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001166 // Keep going when inside try/catch, so that the error can be
1167 // deal with, except when it is a syntax error, it may cause
1168 // the :endtry to be missed.
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001169 && (cstack.cs_trylevel == 0 || did_emsg_syntax)
1170#endif
1171 && used_getline
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001172 && (getline_equal(fgetline, cookie, getexmodeline)
1173 || getline_equal(fgetline, cookie, getexline)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001174 && (next_cmdline != NULL
1175#ifdef FEAT_EVAL
1176 || cstack.cs_idx >= 0
1177#endif
1178 || (flags & DOCMD_REPEAT)));
1179
1180 vim_free(cmdline_copy);
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001181 did_emsg_syntax = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001182#ifdef FEAT_EVAL
1183 free_cmdlines(&lines_ga);
1184 ga_clear(&lines_ga);
1185
1186 if (cstack.cs_idx >= 0)
1187 {
1188 /*
1189 * If a sourced file or executed function ran to its end, report the
1190 * unclosed conditional.
1191 */
1192 if (!got_int && !did_throw
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001193 && ((getline_equal(fgetline, cookie, getsourceline)
1194 && !source_finished(fgetline, cookie))
1195 || (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001196 && !func_has_ended(real_cookie))))
1197 {
1198 if (cstack.cs_flags[cstack.cs_idx] & CSF_TRY)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001199 emsg(_(e_endtry));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001200 else if (cstack.cs_flags[cstack.cs_idx] & CSF_WHILE)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001201 emsg(_(e_endwhile));
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001202 else if (cstack.cs_flags[cstack.cs_idx] & CSF_FOR)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001203 emsg(_(e_endfor));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001204 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001205 emsg(_(e_endif));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001206 }
1207
1208 /*
1209 * Reset "trylevel" in case of a ":finish" or ":return" or a missing
1210 * ":endtry" in a sourced file or executed function. If the try
1211 * conditional is in its finally clause, ignore anything pending.
1212 * If it is in a catch clause, finish the caught exception.
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001213 * Also cleanup any "cs_forinfo" structures.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001214 */
1215 do
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001216 {
1217 int idx = cleanup_conditionals(&cstack, 0, TRUE);
1218
Bram Moolenaar89e5d682005-01-17 22:06:23 +00001219 if (idx >= 0)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001220 --idx; // remove try block not in its finally clause
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001221 rewind_conditionals(&cstack, idx, CSF_WHILE | CSF_FOR,
1222 &cstack.cs_looplevel);
1223 }
1224 while (cstack.cs_idx >= 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001225 trylevel = initial_trylevel;
1226 }
1227
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001228 // If a missing ":endtry", ":endwhile", ":endfor", or ":endif" or a memory
1229 // lack was reported above and the error message is to be converted to an
1230 // exception, do this now after rewinding the cstack.
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001231 do_errthrow(&cstack, getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001232 ? (char_u *)"endfunction" : (char_u *)NULL);
1233
1234 if (trylevel == 0)
1235 {
1236 /*
1237 * When an exception is being thrown out of the outermost try
1238 * conditional, discard the uncaught exception, disable the conversion
1239 * of interrupts or errors to exceptions, and ensure that no more
1240 * commands are executed.
1241 */
1242 if (did_throw)
1243 {
1244 void *p = NULL;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02001245 msglist_T *messages = NULL, *next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001246
1247 /*
1248 * If the uncaught exception is a user exception, report it as an
1249 * error. If it is an error exception, display the saved error
1250 * message now. For an interrupt exception, do nothing; the
1251 * interrupt message is given elsewhere.
1252 */
1253 switch (current_exception->type)
1254 {
1255 case ET_USER:
Bram Moolenaar9c13b352005-05-19 20:53:52 +00001256 vim_snprintf((char *)IObuff, IOSIZE,
1257 _("E605: Exception not caught: %s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001258 current_exception->value);
1259 p = vim_strsave(IObuff);
1260 break;
1261 case ET_ERROR:
1262 messages = current_exception->messages;
1263 current_exception->messages = NULL;
1264 break;
1265 case ET_INTERRUPT:
1266 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001267 }
1268
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001269 estack_push(ETYPE_EXCEPT, current_exception->throw_name,
1270 current_exception->throw_lnum);
Bram Moolenaare31ee862020-01-07 20:59:34 +01001271 ESTACK_CHECK_SETUP
Bram Moolenaar071d4272004-06-13 20:20:40 +00001272 current_exception->throw_name = NULL;
1273
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001274 discard_current_exception(); // uses IObuff if 'verbose'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001275 suppress_errthrow = TRUE;
1276 force_abort = TRUE;
1277
1278 if (messages != NULL)
1279 {
1280 do
1281 {
1282 next = messages->next;
1283 emsg(messages->msg);
1284 vim_free(messages->msg);
Bram Moolenaar5fbf3bc2020-06-01 21:13:11 +02001285 vim_free(messages->sfile);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001286 vim_free(messages);
1287 messages = next;
1288 }
1289 while (messages != NULL);
1290 }
1291 else if (p != NULL)
1292 {
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01001293 emsg(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001294 vim_free(p);
1295 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001296 vim_free(SOURCING_NAME);
Bram Moolenaare31ee862020-01-07 20:59:34 +01001297 ESTACK_CHECK_NOW
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001298 estack_pop();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001299 }
1300
1301 /*
1302 * On an interrupt or an aborting error not converted to an exception,
1303 * disable the conversion of errors to exceptions. (Interrupts are not
Bram Moolenaar2196bce2020-04-12 20:01:11 +02001304 * converted anymore, here.) This enables also the interrupt message
Bram Moolenaar071d4272004-06-13 20:20:40 +00001305 * when force_abort is set and did_emsg unset in case of an interrupt
1306 * from a finally clause after an error.
1307 */
1308 else if (got_int || (did_emsg && force_abort))
1309 suppress_errthrow = TRUE;
1310 }
1311
1312 /*
1313 * The current cstack will be freed when do_cmdline() returns. An uncaught
1314 * exception will have to be rethrown in the previous cstack. If a function
1315 * has just returned or a script file was just finished and the previous
1316 * cstack belongs to the same function or, respectively, script file, it
1317 * will have to be checked for finally clauses to be executed due to the
1318 * ":return" or ":finish". This is done in do_one_cmd().
1319 */
1320 if (did_throw)
1321 need_rethrow = TRUE;
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001322 if ((getline_equal(fgetline, cookie, getsourceline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001323 && ex_nesting_level > source_level(real_cookie))
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001324 || (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001325 && ex_nesting_level > func_level(real_cookie) + 1))
1326 {
1327 if (!did_throw)
1328 check_cstack = TRUE;
1329 }
1330 else
1331 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001332 // When leaving a function, reduce nesting level.
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001333 if (getline_equal(fgetline, cookie, get_func_line))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001334 --ex_nesting_level;
1335 /*
1336 * Go to debug mode when returning from a function in which we are
1337 * single-stepping.
1338 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001339 if ((getline_equal(fgetline, cookie, getsourceline)
1340 || getline_equal(fgetline, cookie, get_func_line))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001341 && ex_nesting_level + 1 <= debug_break_level)
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001342 do_debug(getline_equal(fgetline, cookie, getsourceline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001343 ? (char_u *)_("End of sourced file")
1344 : (char_u *)_("End of function"));
1345 }
1346
1347 /*
1348 * Restore the exception environment (done after returning from the
1349 * debugger).
1350 */
1351 if (flags & DOCMD_EXCRESET)
Bram Moolenaared203462004-06-16 11:19:22 +00001352 restore_dbg_stuff(&debug_saved);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001353
1354 msg_list = saved_msg_list;
Bram Moolenaar292b90d2020-03-18 15:23:16 +01001355
1356 // Cleanup if "cs_emsg_silent_list" remains.
1357 if (cstack.cs_emsg_silent_list != NULL)
1358 {
1359 eslist_T *elem, *temp;
1360
1361 for (elem = cstack.cs_emsg_silent_list; elem != NULL; elem = temp)
1362 {
1363 temp = elem->next;
1364 vim_free(elem);
1365 }
1366 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001367#endif // FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001368
1369 /*
1370 * If there was too much output to fit on the command line, ask the user to
1371 * hit return before redrawing the screen. With the ":global" command we do
1372 * this only once after the command is finished.
1373 */
1374 if (did_inc)
1375 {
1376 --RedrawingDisabled;
1377 --no_wait_return;
1378 msg_scroll = FALSE;
1379
1380 /*
1381 * When just finished an ":if"-":else" which was typed, no need to
1382 * wait for hit-return. Also for an error situation.
1383 */
1384 if (retval == FAIL
1385#ifdef FEAT_EVAL
1386 || (did_endif && KeyTyped && !did_emsg)
1387#endif
1388 )
1389 {
1390 need_wait_return = FALSE;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001391 msg_didany = FALSE; // don't wait when restarting edit
Bram Moolenaar071d4272004-06-13 20:20:40 +00001392 }
1393 else if (need_wait_return)
1394 {
1395 /*
1396 * The msg_start() above clears msg_didout. The wait_return we do
1397 * here should not overwrite the command that may be shown before
1398 * doing that.
1399 */
1400 msg_didout |= msg_didout_before_start;
1401 wait_return(FALSE);
1402 }
1403 }
1404
Bram Moolenaar2a942252012-11-28 23:03:07 +01001405#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001406 did_endif = FALSE; // in case do_cmdline used recursively
Bram Moolenaar2a942252012-11-28 23:03:07 +01001407#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001408 /*
1409 * Reset if_level, in case a sourced script file contains more ":if" than
1410 * ":endif" (could be ":if x | foo | endif").
1411 */
1412 if_level = 0;
Bram Moolenaar2e18a122012-11-28 19:10:54 +01001413#endif
Bram Moolenaard4ad0d42012-11-28 17:34:48 +01001414
Bram Moolenaar071d4272004-06-13 20:20:40 +00001415 --call_depth;
1416 return retval;
1417}
1418
1419#ifdef FEAT_EVAL
1420/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001421 * Obtain a line when inside a ":while" or ":for" loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001422 */
1423 static char_u *
Bram Moolenaar20b23c62020-08-20 15:25:00 +02001424get_loop_line(int c, void *cookie, int indent, getline_opt_T options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001425{
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001426 struct loop_cookie *cp = (struct loop_cookie *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001427 wcmd_T *wp;
1428 char_u *line;
1429
1430 if (cp->current_line + 1 >= cp->lines_gap->ga_len)
1431 {
1432 if (cp->repeating)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001433 return NULL; // trying to read past ":endwhile"/":endfor"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001434
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001435 // First time inside the ":while"/":for": get line normally.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001436 if (cp->getline == NULL)
Bram Moolenaar20b23c62020-08-20 15:25:00 +02001437 line = getcmdline(c, 0L, indent, options);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001438 else
Bram Moolenaar20b23c62020-08-20 15:25:00 +02001439 line = cp->getline(c, cp->cookie, indent, options);
Bram Moolenaard68071d2006-05-02 22:08:30 +00001440 if (line != NULL && store_loop_line(cp->lines_gap, line) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001441 ++cp->current_line;
1442
1443 return line;
1444 }
1445
1446 KeyTyped = FALSE;
1447 ++cp->current_line;
1448 wp = (wcmd_T *)(cp->lines_gap->ga_data) + cp->current_line;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001449 SOURCING_LNUM = wp->lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001450 return vim_strsave(wp->line);
1451}
1452
1453/*
1454 * Store a line in "gap" so that a ":while" loop can execute it again.
1455 */
1456 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001457store_loop_line(garray_T *gap, char_u *line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001458{
1459 if (ga_grow(gap, 1) == FAIL)
1460 return FAIL;
1461 ((wcmd_T *)(gap->ga_data))[gap->ga_len].line = vim_strsave(line);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001462 ((wcmd_T *)(gap->ga_data))[gap->ga_len].lnum = SOURCING_LNUM;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001463 ++gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001464 return OK;
1465}
1466
1467/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001468 * Free the lines stored for a ":while" or ":for" loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001469 */
1470 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001471free_cmdlines(garray_T *gap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001472{
1473 while (gap->ga_len > 0)
1474 {
1475 vim_free(((wcmd_T *)(gap->ga_data))[gap->ga_len - 1].line);
1476 --gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001477 }
1478}
1479#endif
1480
1481/*
Bram Moolenaar89d40322006-08-29 15:30:07 +00001482 * If "fgetline" is get_loop_line(), return TRUE if the getline it uses equals
1483 * "func". * Otherwise return TRUE when "fgetline" equals "func".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001484 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001485 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001486getline_equal(
Bram Moolenaar66250c92020-08-20 15:02:42 +02001487 char_u *(*fgetline)(int, void *, int, getline_opt_T),
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001488 void *cookie UNUSED, // argument for fgetline()
Bram Moolenaar66250c92020-08-20 15:02:42 +02001489 char_u *(*func)(int, void *, int, getline_opt_T))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001490{
1491#ifdef FEAT_EVAL
Bram Moolenaar20b23c62020-08-20 15:25:00 +02001492 char_u *(*gp)(int, void *, int, getline_opt_T);
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001493 struct loop_cookie *cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001494
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001495 // When "fgetline" is "get_loop_line()" use the "cookie" to find the
1496 // function that's originally used to obtain the lines. This may be
1497 // nested several levels.
Bram Moolenaar89d40322006-08-29 15:30:07 +00001498 gp = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001499 cp = (struct loop_cookie *)cookie;
1500 while (gp == get_loop_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001501 {
1502 gp = cp->getline;
1503 cp = cp->cookie;
1504 }
1505 return gp == func;
1506#else
Bram Moolenaar89d40322006-08-29 15:30:07 +00001507 return fgetline == func;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001508#endif
1509}
1510
Bram Moolenaar071d4272004-06-13 20:20:40 +00001511/*
Bram Moolenaar89d40322006-08-29 15:30:07 +00001512 * If "fgetline" is get_loop_line(), return the cookie used by the original
Bram Moolenaar071d4272004-06-13 20:20:40 +00001513 * getline function. Otherwise return "cookie".
1514 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001515 void *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001516getline_cookie(
Bram Moolenaar66250c92020-08-20 15:02:42 +02001517 char_u *(*fgetline)(int, void *, int, getline_opt_T) UNUSED,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001518 void *cookie) // argument for fgetline()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001519{
Bram Moolenaar13505972019-01-24 15:04:48 +01001520#ifdef FEAT_EVAL
Bram Moolenaar20b23c62020-08-20 15:25:00 +02001521 char_u *(*gp)(int, void *, int, getline_opt_T);
Bram Moolenaard5053d02020-06-28 15:51:16 +02001522 struct loop_cookie *cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001523
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001524 // When "fgetline" is "get_loop_line()" use the "cookie" to find the
1525 // cookie that's originally used to obtain the lines. This may be nested
1526 // several levels.
Bram Moolenaar89d40322006-08-29 15:30:07 +00001527 gp = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001528 cp = (struct loop_cookie *)cookie;
1529 while (gp == get_loop_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001530 {
1531 gp = cp->getline;
1532 cp = cp->cookie;
1533 }
1534 return cp;
Bram Moolenaar13505972019-01-24 15:04:48 +01001535#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001536 return cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001537#endif
Bram Moolenaar13505972019-01-24 15:04:48 +01001538}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001539
Bram Moolenaard5053d02020-06-28 15:51:16 +02001540#if defined(FEAT_EVAL) || defined(PROT)
1541/*
1542 * Get the next line source line without advancing.
1543 */
1544 char_u *
1545getline_peek(
Bram Moolenaar66250c92020-08-20 15:02:42 +02001546 char_u *(*fgetline)(int, void *, int, getline_opt_T) UNUSED,
Bram Moolenaard5053d02020-06-28 15:51:16 +02001547 void *cookie) // argument for fgetline()
1548{
Bram Moolenaar20b23c62020-08-20 15:25:00 +02001549 char_u *(*gp)(int, void *, int, getline_opt_T);
Bram Moolenaard5053d02020-06-28 15:51:16 +02001550 struct loop_cookie *cp;
1551 wcmd_T *wp;
1552
1553 // When "fgetline" is "get_loop_line()" use the "cookie" to find the
1554 // cookie that's originally used to obtain the lines. This may be nested
1555 // several levels.
1556 gp = fgetline;
1557 cp = (struct loop_cookie *)cookie;
1558 while (gp == get_loop_line)
1559 {
1560 if (cp->current_line + 1 < cp->lines_gap->ga_len)
1561 {
1562 // executing lines a second time, use the stored copy
1563 wp = (wcmd_T *)(cp->lines_gap->ga_data) + cp->current_line + 1;
1564 return wp->line;
1565 }
1566 gp = cp->getline;
1567 cp = cp->cookie;
1568 }
1569 if (gp == getsourceline)
1570 return source_nextline(cp);
1571 return NULL;
1572}
1573#endif
1574
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001575
1576/*
1577 * Helper function to apply an offset for buffer commands, i.e. ":bdelete",
1578 * ":bwipeout", etc.
1579 * Returns the buffer number.
1580 */
1581 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001582compute_buffer_local_count(int addr_type, int lnum, int offset)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001583{
1584 buf_T *buf;
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001585 buf_T *nextbuf;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001586 int count = offset;
1587
1588 buf = firstbuf;
1589 while (buf->b_next != NULL && buf->b_fnum < lnum)
1590 buf = buf->b_next;
1591 while (count != 0)
1592 {
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001593 count += (offset < 0) ? 1 : -1;
1594 nextbuf = (offset < 0) ? buf->b_prev : buf->b_next;
1595 if (nextbuf == NULL)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001596 break;
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001597 buf = nextbuf;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001598 if (addr_type == ADDR_LOADED_BUFFERS)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001599 // skip over unloaded buffers
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001600 while (buf->b_ml.ml_mfp == NULL)
1601 {
1602 nextbuf = (offset < 0) ? buf->b_prev : buf->b_next;
1603 if (nextbuf == NULL)
1604 break;
1605 buf = nextbuf;
1606 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001607 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001608 // we might have gone too far, last buffer is not loadedd
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001609 if (addr_type == ADDR_LOADED_BUFFERS)
1610 while (buf->b_ml.ml_mfp == NULL)
1611 {
1612 nextbuf = (offset >= 0) ? buf->b_prev : buf->b_next;
1613 if (nextbuf == NULL)
1614 break;
1615 buf = nextbuf;
1616 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001617 return buf->b_fnum;
1618}
1619
Bram Moolenaarb7316892019-05-01 18:08:42 +02001620/*
1621 * Return the window number of "win".
1622 * When "win" is NULL return the number of windows.
1623 */
Bram Moolenaarf240e182014-11-27 18:33:02 +01001624 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001625current_win_nr(win_T *win)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001626{
1627 win_T *wp;
1628 int nr = 0;
1629
Bram Moolenaar29323592016-07-24 22:04:11 +02001630 FOR_ALL_WINDOWS(wp)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001631 {
1632 ++nr;
1633 if (wp == win)
1634 break;
1635 }
1636 return nr;
1637}
1638
1639 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001640current_tab_nr(tabpage_T *tab)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001641{
1642 tabpage_T *tp;
1643 int nr = 0;
1644
Bram Moolenaar29323592016-07-24 22:04:11 +02001645 FOR_ALL_TABPAGES(tp)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001646 {
1647 ++nr;
1648 if (tp == tab)
1649 break;
1650 }
1651 return nr;
1652}
1653
Bram Moolenaar6e36b1c2020-07-17 20:47:51 +02001654 static int
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001655comment_start(char_u *p, int starts_with_colon UNUSED)
1656{
1657#ifdef FEAT_EVAL
1658 if (in_vim9script())
1659 return p[0] == '#' && p[1] != '{' && !starts_with_colon;
1660#endif
1661 return *p == '"';
1662}
1663
Bram Moolenaarf240e182014-11-27 18:33:02 +01001664# define CURRENT_WIN_NR current_win_nr(curwin)
1665# define LAST_WIN_NR current_win_nr(NULL)
1666# define CURRENT_TAB_NR current_tab_nr(curtab)
1667# define LAST_TAB_NR current_tab_nr(NULL)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001668
Bram Moolenaar071d4272004-06-13 20:20:40 +00001669/*
1670 * Execute one Ex command.
1671 *
1672 * If 'sourcing' is TRUE, the command will be included in the error message.
1673 *
1674 * 1. skip comment lines and leading space
1675 * 2. handle command modifiers
Bram Moolenaar1c40a662014-11-27 16:38:11 +01001676 * 3. find the command
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001677 * 4. parse range
Bram Moolenaar1c40a662014-11-27 16:38:11 +01001678 * 5. Parse the command.
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001679 * 6. parse arguments
1680 * 7. switch on command name
Bram Moolenaar071d4272004-06-13 20:20:40 +00001681 *
Bram Moolenaar89d40322006-08-29 15:30:07 +00001682 * Note: "fgetline" can be NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001683 *
1684 * This function may be called recursively!
1685 */
1686#if (_MSC_VER == 1200)
1687/*
Bram Moolenaared203462004-06-16 11:19:22 +00001688 * Avoid optimisation bug in VC++ version 6.0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001689 */
Bram Moolenaar281bdce2005-01-25 21:53:18 +00001690 #pragma optimize( "g", off )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001691#endif
1692 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001693do_one_cmd(
Bram Moolenaarddef1292019-12-16 17:10:33 +01001694 char_u **cmdlinep,
1695 int sourcing,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001696#ifdef FEAT_EVAL
Bram Moolenaarddef1292019-12-16 17:10:33 +01001697 cstack_T *cstack,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001698#endif
Bram Moolenaar20b23c62020-08-20 15:25:00 +02001699 char_u *(*fgetline)(int, void *, int, getline_opt_T),
Bram Moolenaarddef1292019-12-16 17:10:33 +01001700 void *cookie) // argument for fgetline()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001701{
Bram Moolenaarddef1292019-12-16 17:10:33 +01001702 char_u *p;
1703 linenr_T lnum;
1704 long n;
1705 char *errormsg = NULL; // error message
1706 char_u *after_modifier = NULL;
1707 exarg_T ea; // Ex command arguments
1708 int save_msg_scroll = msg_scroll;
1709 cmdmod_T save_cmdmod;
1710 int save_reg_executing = reg_executing;
1711 int ni; // set when Not Implemented
1712 char_u *cmd;
Bram Moolenaar6e36b1c2020-07-17 20:47:51 +02001713 int starts_with_colon = FALSE;
Bram Moolenaar83f37b92020-02-23 14:35:01 +01001714#ifdef FEAT_EVAL
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02001715 int may_have_range;
Bram Moolenaar3d48e252020-07-15 14:15:52 +02001716 int vim9script = in_vim9script();
Bram Moolenaar83f37b92020-02-23 14:35:01 +01001717#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001718
Bram Moolenaara80faa82020-04-12 19:37:17 +02001719 CLEAR_FIELD(ea);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001720 ea.line1 = 1;
1721 ea.line2 = 1;
1722#ifdef FEAT_EVAL
1723 ++ex_nesting_level;
1724#endif
1725
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001726 // When the last file has not been edited :q has to be typed twice.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001727 if (quitmore
1728#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001729 // avoid that a function call in 'statusline' does this
Bram Moolenaar89d40322006-08-29 15:30:07 +00001730 && !getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001731#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001732 // avoid that an autocommand, e.g. QuitPre, does this
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001733 && !getline_equal(fgetline, cookie, getnextac))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001734 --quitmore;
1735
1736 /*
1737 * Reset browse, confirm, etc.. They are restored when returning, for
1738 * recursive calls.
1739 */
1740 save_cmdmod = cmdmod;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001741
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001742 // "#!anything" is handled like a comment.
Bram Moolenaarcbb37ad2006-08-16 15:04:21 +00001743 if ((*cmdlinep)[0] == '#' && (*cmdlinep)[1] == '!')
1744 goto doend;
1745
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02001746/*
1747 * 1. Skip comment lines and leading white space and colons.
1748 * 2. Handle command modifiers.
1749 */
1750 // The "ea" structure holds the arguments that can be used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001751 ea.cmd = *cmdlinep;
Bram Moolenaareffed932018-08-14 13:38:17 +02001752 ea.cmdlinep = cmdlinep;
1753 ea.getline = fgetline;
1754 ea.cookie = cookie;
1755#ifdef FEAT_EVAL
1756 ea.cstack = cstack;
Bram Moolenaar83f37b92020-02-23 14:35:01 +01001757 starts_with_colon = *skipwhite(ea.cmd) == ':';
Bram Moolenaar071d4272004-06-13 20:20:40 +00001758#endif
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02001759 if (parse_command_modifiers(&ea, &errormsg, FALSE) == FAIL)
Bram Moolenaareffed932018-08-14 13:38:17 +02001760 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001761
Bram Moolenaar7b668e82016-08-23 23:51:21 +02001762 after_modifier = ea.cmd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001763
1764#ifdef FEAT_EVAL
1765 ea.skip = did_emsg || got_int || did_throw || (cstack->cs_idx >= 0
1766 && !(cstack->cs_flags[cstack->cs_idx] & CSF_ACTIVE));
1767#else
1768 ea.skip = (if_level > 0);
1769#endif
1770
Bram Moolenaar071d4272004-06-13 20:20:40 +00001771/*
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001772 * 3. Skip over the range to find the command. Let "p" point to after it.
1773 *
1774 * We need the command to know what kind of range it uses.
1775 */
1776 cmd = ea.cmd;
Bram Moolenaar3d48e252020-07-15 14:15:52 +02001777#ifdef FEAT_EVAL
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02001778 // In Vim9 script a colon is required before the range.
1779 may_have_range = !vim9script || starts_with_colon;
1780 if (may_have_range)
Bram Moolenaar3d48e252020-07-15 14:15:52 +02001781#endif
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02001782 ea.cmd = skip_range(ea.cmd, TRUE, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001783
1784#ifdef FEAT_EVAL
Bram Moolenaar3d48e252020-07-15 14:15:52 +02001785 if (vim9script && !starts_with_colon)
Bram Moolenaardf069ee2020-06-22 23:02:51 +02001786 {
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02001787 if (ea.cmd == cmd + 1 && *cmd == '$')
1788 // should be "$VAR = val"
1789 --ea.cmd;
1790 else if (ea.cmd > cmd)
Bram Moolenaardf069ee2020-06-22 23:02:51 +02001791 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02001792 emsg(_(e_colon_required_before_a_range));
Bram Moolenaardf069ee2020-06-22 23:02:51 +02001793 goto doend;
1794 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001795 p = find_ex_command(&ea, NULL, lookup_scriptvar, NULL);
Bram Moolenaardf069ee2020-06-22 23:02:51 +02001796 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001797 else
1798#endif
1799 p = find_ex_command(&ea, NULL, NULL, NULL);
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001800
Bram Moolenaar7feb35e2018-08-21 17:49:54 +02001801#ifdef FEAT_EVAL
1802# ifdef FEAT_PROFILE
1803 // Count this line for profiling if skip is TRUE.
1804 if (do_profiling == PROF_YES
1805 && (!ea.skip || cstack->cs_idx == 0 || (cstack->cs_idx > 0
1806 && (cstack->cs_flags[cstack->cs_idx - 1] & CSF_ACTIVE))))
1807 {
1808 int skip = did_emsg || got_int || did_throw;
1809
1810 if (ea.cmdidx == CMD_catch)
1811 skip = !skip && !(cstack->cs_idx >= 0
1812 && (cstack->cs_flags[cstack->cs_idx] & CSF_THROWN)
1813 && !(cstack->cs_flags[cstack->cs_idx] & CSF_CAUGHT));
1814 else if (ea.cmdidx == CMD_else || ea.cmdidx == CMD_elseif)
1815 skip = skip || !(cstack->cs_idx >= 0
1816 && !(cstack->cs_flags[cstack->cs_idx]
1817 & (CSF_ACTIVE | CSF_TRUE)));
1818 else if (ea.cmdidx == CMD_finally)
1819 skip = FALSE;
1820 else if (ea.cmdidx != CMD_endif
1821 && ea.cmdidx != CMD_endfor
1822 && ea.cmdidx != CMD_endtry
1823 && ea.cmdidx != CMD_endwhile)
1824 skip = ea.skip;
1825
1826 if (!skip)
1827 {
1828 if (getline_equal(fgetline, cookie, get_func_line))
1829 func_line_exec(getline_cookie(fgetline, cookie));
1830 else if (getline_equal(fgetline, cookie, getsourceline))
1831 script_line_exec();
1832 }
1833 }
1834# endif
1835
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001836 // May go to debug mode. If this happens and the ">quit" debug command is
1837 // used, throw an interrupt exception and skip the next command.
Bram Moolenaar7feb35e2018-08-21 17:49:54 +02001838 dbg_check_breakpoint(&ea);
1839 if (!ea.skip && got_int)
1840 {
1841 ea.skip = TRUE;
1842 (void)do_intthrow(cstack);
1843 }
1844#endif
1845
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001846/*
1847 * 4. parse a range specifier of the form: addr [,addr] [;addr] ..
Bram Moolenaar071d4272004-06-13 20:20:40 +00001848 *
1849 * where 'addr' is:
1850 *
1851 * % (entire file)
1852 * $ [+-NUM]
1853 * 'x [+-NUM] (where x denotes a currently defined mark)
1854 * . [+-NUM]
1855 * [+-NUM]..
1856 * NUM
1857 *
1858 * The ea.cmd pointer is updated to point to the first character following the
1859 * range spec. If an initial address is found, but no second, the upper bound
1860 * is equal to the lower.
1861 */
1862
Bram Moolenaar25190db2019-05-04 15:05:28 +02001863 // ea.addr_type for user commands is set by find_ucmd
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01001864 if (!IS_USER_CMDIDX(ea.cmdidx))
1865 {
1866 if (ea.cmdidx != CMD_SIZE)
1867 ea.addr_type = cmdnames[(int)ea.cmdidx].cmd_addr_type;
1868 else
1869 ea.addr_type = ADDR_LINES;
1870
Bram Moolenaar25190db2019-05-04 15:05:28 +02001871 // :wincmd range depends on the argument.
Bram Moolenaar164f3262015-01-14 21:22:01 +01001872 if (ea.cmdidx == CMD_wincmd && p != NULL)
1873 get_wincmd_addr_type(skipwhite(p), &ea);
Bram Moolenaar25190db2019-05-04 15:05:28 +02001874#ifdef FEAT_QUICKFIX
1875 // :.cc in quickfix window uses line number
1876 if ((ea.cmdidx == CMD_cc || ea.cmdidx == CMD_ll) && bt_quickfix(curbuf))
1877 ea.addr_type = ADDR_OTHER;
1878#endif
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01001879 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001880
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01001881 ea.cmd = cmd;
Bram Moolenaar3d48e252020-07-15 14:15:52 +02001882#ifdef FEAT_EVAL
Bram Moolenaarc2af0af2020-08-23 21:06:02 +02001883 if (!may_have_range)
1884 ea.line1 = ea.line2 = default_address(&ea);
1885 else
Bram Moolenaar3d48e252020-07-15 14:15:52 +02001886#endif
1887 if (parse_cmd_address(&ea, &errormsg, FALSE) == FAIL)
1888 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001889
Bram Moolenaar071d4272004-06-13 20:20:40 +00001890/*
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001891 * 5. Parse the command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001892 */
1893
1894 /*
1895 * Skip ':' and any white space
1896 */
1897 ea.cmd = skipwhite(ea.cmd);
1898 while (*ea.cmd == ':')
1899 ea.cmd = skipwhite(ea.cmd + 1);
1900
1901 /*
1902 * If we got a line, but no command, then go to the line.
1903 * If we find a '|' or '\n' we set ea.nextcmd.
1904 */
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001905 if (*ea.cmd == NUL || comment_start(ea.cmd, starts_with_colon)
1906 || (ea.nextcmd = check_nextcmd(ea.cmd)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001907 {
1908 /*
1909 * strange vi behaviour:
1910 * ":3" jumps to line 3
1911 * ":3|..." prints line 3
1912 * ":|" prints current line
1913 */
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001914 if (ea.skip) // skip this if inside :if
Bram Moolenaar071d4272004-06-13 20:20:40 +00001915 goto doend;
Bram Moolenaardf177f62005-02-22 08:39:57 +00001916 if (*ea.cmd == '|' || (exmode_active && ea.line1 != ea.line2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001917 {
1918 ea.cmdidx = CMD_print;
Bram Moolenaar8071cb22019-07-12 17:58:01 +02001919 ea.argt = EX_RANGE+EX_COUNT+EX_TRLBAR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001920 if ((errormsg = invalid_range(&ea)) == NULL)
1921 {
1922 correct_range(&ea);
1923 ex_print(&ea);
1924 }
1925 }
1926 else if (ea.addr_count != 0)
1927 {
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00001928 if (ea.line2 > curbuf->b_ml.ml_line_count)
1929 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001930 // With '-' in 'cpoptions' a line number past the file is an
1931 // error, otherwise put it at the end of the file.
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00001932 if (vim_strchr(p_cpo, CPO_MINUS) != NULL)
1933 ea.line2 = -1;
1934 else
1935 ea.line2 = curbuf->b_ml.ml_line_count;
1936 }
1937
1938 if (ea.line2 < 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001939 errormsg = _(e_invrange);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001940 else
1941 {
1942 if (ea.line2 == 0)
1943 curwin->w_cursor.lnum = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001944 else
1945 curwin->w_cursor.lnum = ea.line2;
1946 beginline(BL_SOL | BL_FIX);
1947 }
1948 }
1949 goto doend;
1950 }
1951
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001952 // If this looks like an undefined user command and there are CmdUndefined
1953 // autocommands defined, trigger the matching autocommands.
Bram Moolenaard5005162014-08-22 23:05:54 +02001954 if (p != NULL && ea.cmdidx == CMD_SIZE && !ea.skip
1955 && ASCII_ISUPPER(*ea.cmd)
1956 && has_cmdundefined())
1957 {
Bram Moolenaard5005162014-08-22 23:05:54 +02001958 int ret;
1959
Bram Moolenaar5a31b462014-08-23 14:16:20 +02001960 p = ea.cmd;
Bram Moolenaard5005162014-08-22 23:05:54 +02001961 while (ASCII_ISALNUM(*p))
1962 ++p;
Bram Moolenaar158ea172020-06-18 17:28:39 +02001963 p = vim_strnsave(ea.cmd, p - ea.cmd);
Bram Moolenaard5005162014-08-22 23:05:54 +02001964 ret = apply_autocmds(EVENT_CMDUNDEFINED, p, p, TRUE, NULL);
1965 vim_free(p);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001966 // If the autocommands did something and didn't cause an error, try
1967 // finding the command again.
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001968 p = (ret
1969#ifdef FEAT_EVAL
1970 && !aborting()
Bram Moolenaard5005162014-08-22 23:05:54 +02001971#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001972 ) ? find_ex_command(&ea, NULL, NULL, NULL) : ea.cmd;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001973 }
Bram Moolenaard5005162014-08-22 23:05:54 +02001974
Bram Moolenaar071d4272004-06-13 20:20:40 +00001975 if (p == NULL)
1976 {
1977 if (!ea.skip)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001978 errormsg = _("E464: Ambiguous use of user-defined command");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001979 goto doend;
1980 }
Bram Moolenaarac9fb182019-04-27 13:04:13 +02001981 // Check for wrong commands.
Bram Moolenaar6f9a4762017-06-22 20:39:17 +02001982 if (*p == '!' && ea.cmd[1] == 0151 && ea.cmd[0] == 78
1983 && !IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001984 {
1985 errormsg = uc_fun_cmd();
1986 goto doend;
1987 }
Bram Moolenaarac9fb182019-04-27 13:04:13 +02001988
Bram Moolenaar071d4272004-06-13 20:20:40 +00001989 if (ea.cmdidx == CMD_SIZE)
1990 {
1991 if (!ea.skip)
1992 {
1993 STRCPY(IObuff, _("E492: Not an editor command"));
1994 if (!sourcing)
Bram Moolenaar7b668e82016-08-23 23:51:21 +02001995 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001996 // If the modifier was parsed OK the error must be in the
1997 // following command
Bram Moolenaar7b668e82016-08-23 23:51:21 +02001998 if (after_modifier != NULL)
1999 append_command(after_modifier);
2000 else
2001 append_command(*cmdlinep);
2002 }
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002003 errormsg = (char *)IObuff;
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02002004 did_emsg_syntax = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002005 }
2006 goto doend;
2007 }
2008
Bram Moolenaar958636c2014-10-21 20:01:58 +02002009 ni = (!IS_USER_CMDIDX(ea.cmdidx)
2010 && (cmdnames[ea.cmdidx].cmd_func == ex_ni
Bram Moolenaar7bb75552007-07-16 18:39:49 +00002011#ifdef HAVE_EX_SCRIPT_NI
2012 || cmdnames[ea.cmdidx].cmd_func == ex_script_ni
2013#endif
2014 ));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002015
2016#ifndef FEAT_EVAL
2017 /*
2018 * When the expression evaluation is disabled, recognize the ":if" and
2019 * ":endif" commands and ignore everything in between it.
2020 */
2021 if (ea.cmdidx == CMD_if)
2022 ++if_level;
2023 if (if_level)
2024 {
2025 if (ea.cmdidx == CMD_endif)
2026 --if_level;
2027 goto doend;
2028 }
2029
2030#endif
2031
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002032 // forced commands
Bram Moolenaar196b3b02008-06-20 16:51:41 +00002033 if (*p == '!' && ea.cmdidx != CMD_substitute
2034 && ea.cmdidx != CMD_smagic && ea.cmdidx != CMD_snomagic)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002035 {
2036 ++p;
2037 ea.forceit = TRUE;
2038 }
2039 else
2040 ea.forceit = FALSE;
2041
2042/*
Bram Moolenaarb7316892019-05-01 18:08:42 +02002043 * 6. Parse arguments. Then check for errors.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002044 */
Bram Moolenaar958636c2014-10-21 20:01:58 +02002045 if (!IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002046 ea.argt = (long)cmdnames[(int)ea.cmdidx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002047
2048 if (!ea.skip)
2049 {
2050#ifdef HAVE_SANDBOX
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002051 if (sandbox != 0 && !(ea.argt & EX_SBOXOK))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002052 {
Bram Moolenaar8c62a082019-02-08 14:34:10 +01002053 // Command not allowed in sandbox.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002054 errormsg = _(e_sandbox);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002055 goto doend;
2056 }
2057#endif
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002058 if (restricted != 0 && (ea.argt & EX_RESTRICT))
Bram Moolenaar8c62a082019-02-08 14:34:10 +01002059 {
2060 errormsg = _("E981: Command not allowed in rvim");
2061 goto doend;
2062 }
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002063 if (!curbuf->b_p_ma && (ea.argt & EX_MODIFY))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002064 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002065 // Command not allowed in non-'modifiable' buffer
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002066 errormsg = _(e_modifiable);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002067 goto doend;
2068 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002069
Bram Moolenaar37394ff2020-07-25 19:38:18 +02002070 if (!IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002071 {
Bram Moolenaar37394ff2020-07-25 19:38:18 +02002072#ifdef FEAT_CMDWIN
2073 if (cmdwin_type != 0 && !(ea.argt & EX_CMDWIN))
2074 {
2075 // Command not allowed in the command line window
2076 errormsg = _(e_cmdwin);
2077 goto doend;
2078 }
2079#endif
2080 if (text_locked() && !(ea.argt & EX_LOCK_OK))
2081 {
2082 // Command not allowed when text is locked
2083 errormsg = _(get_text_locked_msg());
2084 goto doend;
2085 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002086 }
Bram Moolenaar379fb762018-08-30 15:58:28 +02002087
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002088 // Disallow editing another buffer when "curbuf_lock" is set.
2089 // Do allow ":checktime" (it is postponed).
2090 // Do allow ":edit" (check for an argument later).
2091 // Do allow ":file" with no arguments (check for an argument later).
Bram Moolenaar8e1986e2020-08-06 22:11:06 +02002092 if (!(ea.argt & (EX_CMDWIN | EX_LOCK_OK))
Bram Moolenaar5555acc2006-04-07 21:33:12 +00002093 && ea.cmdidx != CMD_checktime
Bram Moolenaar379fb762018-08-30 15:58:28 +02002094 && ea.cmdidx != CMD_edit
2095 && ea.cmdidx != CMD_file
Bram Moolenaar958636c2014-10-21 20:01:58 +02002096 && !IS_USER_CMDIDX(ea.cmdidx)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002097 && curbuf_locked())
2098 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002099
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002100 if (!ni && !(ea.argt & EX_RANGE) && ea.addr_count > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002101 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002102 // no range allowed
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002103 errormsg = _(e_norange);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002104 goto doend;
2105 }
2106 }
2107
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002108 if (!ni && !(ea.argt & EX_BANG) && ea.forceit) // no <!> allowed
Bram Moolenaar071d4272004-06-13 20:20:40 +00002109 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002110 errormsg = _(e_nobang);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002111 goto doend;
2112 }
2113
2114 /*
2115 * Don't complain about the range if it is not used
2116 * (could happen if line_count is accidentally set to 0).
2117 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002118 if (!ea.skip && !ni && (ea.argt & EX_RANGE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002119 {
2120 /*
2121 * If the range is backwards, ask for confirmation and, if given, swap
2122 * ea.line1 & ea.line2 so it's forwards again.
2123 * When global command is busy, don't ask, will fail below.
2124 */
2125 if (!global_busy && ea.line1 > ea.line2)
2126 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002127 if (msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002128 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002129 if (sourcing || exmode_active)
2130 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002131 errormsg = _("E493: Backwards range given");
Bram Moolenaara5792f52005-11-23 21:25:05 +00002132 goto doend;
2133 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002134 if (ask_yesno((char_u *)
2135 _("Backwards range given, OK to swap"), FALSE) != 'y')
Bram Moolenaara5792f52005-11-23 21:25:05 +00002136 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002137 }
2138 lnum = ea.line1;
2139 ea.line1 = ea.line2;
2140 ea.line2 = lnum;
2141 }
2142 if ((errormsg = invalid_range(&ea)) != NULL)
2143 goto doend;
2144 }
2145
Bram Moolenaarb7316892019-05-01 18:08:42 +02002146 if ((ea.addr_type == ADDR_OTHER) && ea.addr_count == 0)
2147 // default is 1, not cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00002148 ea.line2 = 1;
2149
2150 correct_range(&ea);
2151
2152#ifdef FEAT_FOLDING
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002153 if (((ea.argt & EX_WHOLEFOLD) || ea.addr_count >= 2) && !global_busy
Bram Moolenaara3306952016-01-02 21:41:06 +01002154 && ea.addr_type == ADDR_LINES)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002155 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002156 // Put the first line at the start of a closed fold, put the last line
2157 // at the end of a closed fold.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002158 (void)hasFolding(ea.line1, &ea.line1, NULL);
2159 (void)hasFolding(ea.line2, NULL, &ea.line2);
2160 }
2161#endif
2162
2163#ifdef FEAT_QUICKFIX
2164 /*
Bram Moolenaar86b68352004-12-27 21:59:20 +00002165 * For the ":make" and ":grep" commands we insert the 'makeprg'/'grepprg'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002166 * option here, so things like % get expanded.
2167 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002168 p = replace_makeprg(&ea, p, cmdlinep);
2169 if (p == NULL)
2170 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002171#endif
2172
2173 /*
2174 * Skip to start of argument.
2175 * Don't do this for the ":!" command, because ":!! -l" needs the space.
2176 */
2177 if (ea.cmdidx == CMD_bang)
2178 ea.arg = p;
2179 else
2180 ea.arg = skipwhite(p);
2181
Bram Moolenaar379fb762018-08-30 15:58:28 +02002182 // ":file" cannot be run with an argument when "curbuf_lock" is set
2183 if (ea.cmdidx == CMD_file && *ea.arg != NUL && curbuf_locked())
2184 goto doend;
2185
Bram Moolenaar071d4272004-06-13 20:20:40 +00002186 /*
2187 * Check for "++opt=val" argument.
2188 * Must be first, allow ":w ++enc=utf8 !cmd"
2189 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002190 if (ea.argt & EX_ARGOPT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002191 while (ea.arg[0] == '+' && ea.arg[1] == '+')
2192 if (getargopt(&ea) == FAIL && !ni)
2193 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002194 errormsg = _(e_invarg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002195 goto doend;
2196 }
2197
2198 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
2199 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002200 if (*ea.arg == '>') // append
Bram Moolenaar071d4272004-06-13 20:20:40 +00002201 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002202 if (*++ea.arg != '>') // typed wrong
Bram Moolenaar071d4272004-06-13 20:20:40 +00002203 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002204 errormsg = _("E494: Use w or w>>");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002205 goto doend;
2206 }
2207 ea.arg = skipwhite(ea.arg + 1);
2208 ea.append = TRUE;
2209 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002210 else if (*ea.arg == '!' && ea.cmdidx == CMD_write) // :w !filter
Bram Moolenaar071d4272004-06-13 20:20:40 +00002211 {
2212 ++ea.arg;
2213 ea.usefilter = TRUE;
2214 }
2215 }
2216
2217 if (ea.cmdidx == CMD_read)
2218 {
2219 if (ea.forceit)
2220 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002221 ea.usefilter = TRUE; // :r! filter if ea.forceit
Bram Moolenaar071d4272004-06-13 20:20:40 +00002222 ea.forceit = FALSE;
2223 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002224 else if (*ea.arg == '!') // :r !filter
Bram Moolenaar071d4272004-06-13 20:20:40 +00002225 {
2226 ++ea.arg;
2227 ea.usefilter = TRUE;
2228 }
2229 }
2230
2231 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
2232 {
2233 ea.amount = 1;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002234 while (*ea.arg == *ea.cmd) // count number of '>' or '<'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002235 {
2236 ++ea.arg;
2237 ++ea.amount;
2238 }
2239 ea.arg = skipwhite(ea.arg);
2240 }
2241
2242 /*
2243 * Check for "+command" argument, before checking for next command.
2244 * Don't do this for ":read !cmd" and ":write !cmd".
2245 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002246 if ((ea.argt & EX_CMDARG) && !ea.usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002247 ea.do_ecmd_cmd = getargcmd(&ea.arg);
2248
2249 /*
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002250 * Check for '|' to separate commands and '"' or '#' to start comments.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002251 * Don't do this for ":read !cmd" and ":write !cmd".
2252 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002253 if ((ea.argt & EX_TRLBAR) && !ea.usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002254 separate_nextcmd(&ea);
2255
2256 /*
2257 * Check for <newline> to end a shell command.
Bram Moolenaardf177f62005-02-22 08:39:57 +00002258 * Also do this for ":read !cmd", ":write !cmd" and ":global".
2259 * Any others?
Bram Moolenaar071d4272004-06-13 20:20:40 +00002260 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00002261 else if (ea.cmdidx == CMD_bang
Bram Moolenaar67883b42017-07-27 22:57:00 +02002262 || ea.cmdidx == CMD_terminal
Bram Moolenaardf177f62005-02-22 08:39:57 +00002263 || ea.cmdidx == CMD_global
2264 || ea.cmdidx == CMD_vglobal
2265 || ea.usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002266 {
2267 for (p = ea.arg; *p; ++p)
2268 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002269 // Remove one backslash before a newline, so that it's possible to
2270 // pass a newline to the shell and also a newline that is preceded
2271 // with a backslash. This makes it impossible to end a shell
2272 // command in a backslash, but that doesn't appear useful.
2273 // Halving the number of backslashes is incompatible with previous
2274 // versions.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002275 if (*p == '\\' && p[1] == '\n')
Bram Moolenaara7241f52008-06-24 20:39:31 +00002276 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002277 else if (*p == '\n')
2278 {
2279 ea.nextcmd = p + 1;
2280 *p = NUL;
2281 break;
2282 }
2283 }
2284 }
2285
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002286 if ((ea.argt & EX_DFLALL) && ea.addr_count == 0)
Bram Moolenaarc2af0af2020-08-23 21:06:02 +02002287 address_default_all(&ea);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002288
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002289 // accept numbered register only when no count allowed (:put)
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002290 if ( (ea.argt & EX_REGSTR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002291 && *ea.arg != NUL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002292 // Do not allow register = for user commands
Bram Moolenaar958636c2014-10-21 20:01:58 +02002293 && (!IS_USER_CMDIDX(ea.cmdidx) || *ea.arg != '=')
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002294 && !((ea.argt & EX_COUNT) && VIM_ISDIGIT(*ea.arg)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002295 {
Bram Moolenaar85de2062011-05-05 14:26:41 +02002296#ifndef FEAT_CLIPBOARD
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002297 // check these explicitly for a more specific error message
Bram Moolenaar85de2062011-05-05 14:26:41 +02002298 if (*ea.arg == '*' || *ea.arg == '+')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002299 {
Bram Moolenaar99b12722019-01-14 20:16:40 +01002300 errormsg = _(e_invalidreg);
Bram Moolenaar85de2062011-05-05 14:26:41 +02002301 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002302 }
2303#endif
Bram Moolenaar958636c2014-10-21 20:01:58 +02002304 if (valid_yank_reg(*ea.arg, (ea.cmdidx != CMD_put
2305 && !IS_USER_CMDIDX(ea.cmdidx))))
Bram Moolenaar85de2062011-05-05 14:26:41 +02002306 {
2307 ea.regname = *ea.arg++;
2308#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002309 // for '=' register: accept the rest of the line as an expression
Bram Moolenaar85de2062011-05-05 14:26:41 +02002310 if (ea.arg[-1] == '=' && ea.arg[0] != NUL)
2311 {
2312 set_expr_line(vim_strsave(ea.arg));
2313 ea.arg += STRLEN(ea.arg);
2314 }
2315#endif
2316 ea.arg = skipwhite(ea.arg);
2317 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002318 }
2319
2320 /*
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002321 * Check for a count. When accepting a EX_BUFNAME, don't use "123foo" as a
Bram Moolenaar071d4272004-06-13 20:20:40 +00002322 * count, it's a buffer name.
2323 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002324 if ((ea.argt & EX_COUNT) && VIM_ISDIGIT(*ea.arg)
2325 && (!(ea.argt & EX_BUFNAME) || *(p = skipdigits(ea.arg)) == NUL
Bram Moolenaar1c465442017-03-12 20:10:05 +01002326 || VIM_ISWHITE(*p)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002327 {
2328 n = getdigits(&ea.arg);
2329 ea.arg = skipwhite(ea.arg);
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002330 if (n <= 0 && !ni && (ea.argt & EX_ZEROR) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002331 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002332 errormsg = _(e_zerocount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002333 goto doend;
2334 }
Bram Moolenaarb7316892019-05-01 18:08:42 +02002335 if (ea.addr_type != ADDR_LINES) // e.g. :buffer 2, :sleep 3
Bram Moolenaar071d4272004-06-13 20:20:40 +00002336 {
2337 ea.line2 = n;
2338 if (ea.addr_count == 0)
2339 ea.addr_count = 1;
2340 }
2341 else
2342 {
2343 ea.line1 = ea.line2;
2344 ea.line2 += n - 1;
2345 ++ea.addr_count;
2346 /*
2347 * Be vi compatible: no error message for out of range.
2348 */
Bram Moolenaarb7316892019-05-01 18:08:42 +02002349 if (ea.line2 > curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002350 ea.line2 = curbuf->b_ml.ml_line_count;
2351 }
2352 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00002353
2354 /*
2355 * Check for flags: 'l', 'p' and '#'.
2356 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002357 if (ea.argt & EX_FLAGS)
Bram Moolenaardf177f62005-02-22 08:39:57 +00002358 get_flags(&ea);
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002359 if (!ni && !(ea.argt & EX_EXTRA) && *ea.arg != NUL
2360 && *ea.arg != '"' && (*ea.arg != '|' || (ea.argt & EX_TRLBAR) == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002361 {
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002362 // no arguments allowed but there is something
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +02002363 errormsg = ex_errmsg(e_trailing_arg, ea.arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002364 goto doend;
2365 }
2366
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002367 if (!ni && (ea.argt & EX_NEEDARG) && *ea.arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002368 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002369 errormsg = _(e_argreq);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002370 goto doend;
2371 }
2372
2373#ifdef FEAT_EVAL
2374 /*
2375 * Skip the command when it's not going to be executed.
2376 * The commands like :if, :endif, etc. always need to be executed.
2377 * Also make an exception for commands that handle a trailing command
2378 * themselves.
2379 */
2380 if (ea.skip)
2381 {
2382 switch (ea.cmdidx)
2383 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002384 // commands that need evaluation
Bram Moolenaar071d4272004-06-13 20:20:40 +00002385 case CMD_while:
2386 case CMD_endwhile:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00002387 case CMD_for:
2388 case CMD_endfor:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002389 case CMD_if:
2390 case CMD_elseif:
2391 case CMD_else:
2392 case CMD_endif:
2393 case CMD_try:
2394 case CMD_catch:
2395 case CMD_finally:
2396 case CMD_endtry:
2397 case CMD_function:
Bram Moolenaarab55c682020-03-01 19:41:43 +01002398 case CMD_def:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002399 break;
2400
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002401 // Commands that handle '|' themselves. Check: A command should
2402 // either have the EX_TRLBAR flag, appear in this list or appear in
2403 // the list at ":help :bar".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002404 case CMD_aboveleft:
2405 case CMD_and:
2406 case CMD_belowright:
2407 case CMD_botright:
2408 case CMD_browse:
2409 case CMD_call:
2410 case CMD_confirm:
Bram Moolenaar8f76e6b2019-11-26 16:50:30 +01002411 case CMD_const:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002412 case CMD_delfunction:
2413 case CMD_djump:
2414 case CMD_dlist:
2415 case CMD_dsearch:
2416 case CMD_dsplit:
2417 case CMD_echo:
2418 case CMD_echoerr:
2419 case CMD_echomsg:
2420 case CMD_echon:
Bram Moolenaara76b3152020-02-16 16:20:21 +01002421 case CMD_eval:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002422 case CMD_execute:
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002423 case CMD_filter:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002424 case CMD_help:
2425 case CMD_hide:
2426 case CMD_ijump:
2427 case CMD_ilist:
2428 case CMD_isearch:
2429 case CMD_isplit:
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002430 case CMD_keepalt:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002431 case CMD_keepjumps:
2432 case CMD_keepmarks:
Bram Moolenaara939e432013-11-09 05:30:26 +01002433 case CMD_keeppatterns:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002434 case CMD_leftabove:
2435 case CMD_let:
2436 case CMD_lockmarks:
Bram Moolenaarcc4423a2019-11-26 17:05:00 +01002437 case CMD_lockvar:
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002438 case CMD_lua:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002439 case CMD_match:
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002440 case CMD_mzscheme:
Bram Moolenaar5803ae62014-03-23 16:04:02 +01002441 case CMD_noautocmd:
2442 case CMD_noswapfile:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002443 case CMD_perl:
2444 case CMD_psearch:
2445 case CMD_python:
Bram Moolenaar368373e2010-07-19 20:46:22 +02002446 case CMD_py3:
Bram Moolenaarb6590522010-07-21 16:00:43 +02002447 case CMD_python3:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002448 case CMD_return:
2449 case CMD_rightbelow:
2450 case CMD_ruby:
2451 case CMD_silent:
2452 case CMD_smagic:
2453 case CMD_snomagic:
2454 case CMD_substitute:
2455 case CMD_syntax:
Bram Moolenaara226a6d2006-02-26 23:59:20 +00002456 case CMD_tab:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002457 case CMD_tcl:
2458 case CMD_throw:
2459 case CMD_tilde:
2460 case CMD_topleft:
2461 case CMD_unlet:
Bram Moolenaarcc4423a2019-11-26 17:05:00 +01002462 case CMD_unlockvar:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002463 case CMD_verbose:
2464 case CMD_vertical:
Bram Moolenaar12bde492011-06-13 01:19:56 +02002465 case CMD_wincmd:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002466 break;
2467
2468 default: goto doend;
2469 }
2470 }
2471#endif
2472
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002473 if (ea.argt & EX_XFILE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002474 {
2475 if (expand_filename(&ea, cmdlinep, &errormsg) == FAIL)
2476 goto doend;
2477 }
2478
Bram Moolenaar071d4272004-06-13 20:20:40 +00002479 /*
2480 * Accept buffer name. Cannot be used at the same time with a buffer
2481 * number. Don't do this for a user command.
2482 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002483 if ((ea.argt & EX_BUFNAME) && *ea.arg != NUL && ea.addr_count == 0
Bram Moolenaar958636c2014-10-21 20:01:58 +02002484 && !IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002485 {
2486 /*
2487 * :bdelete, :bwipeout and :bunload take several arguments, separated
2488 * by spaces: find next space (skipping over escaped characters).
2489 * The others take one argument: ignore trailing spaces.
2490 */
2491 if (ea.cmdidx == CMD_bdelete || ea.cmdidx == CMD_bwipeout
2492 || ea.cmdidx == CMD_bunload)
2493 p = skiptowhite_esc(ea.arg);
2494 else
2495 {
2496 p = ea.arg + STRLEN(ea.arg);
Bram Moolenaar1c465442017-03-12 20:10:05 +01002497 while (p > ea.arg && VIM_ISWHITE(p[-1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002498 --p;
2499 }
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002500 ea.line2 = buflist_findpat(ea.arg, p, (ea.argt & EX_BUFUNL) != 0,
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002501 FALSE, FALSE);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002502 if (ea.line2 < 0) // failed
Bram Moolenaar071d4272004-06-13 20:20:40 +00002503 goto doend;
2504 ea.addr_count = 1;
2505 ea.arg = skipwhite(p);
2506 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002507
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002508 // The :try command saves the emsg_silent flag, reset it here when
2509 // ":silent! try" was used, it should only apply to :try itself.
Bram Moolenaareffed932018-08-14 13:38:17 +02002510 if (ea.cmdidx == CMD_try && ea.did_esilent > 0)
Bram Moolenaar2be57332018-02-13 18:05:18 +01002511 {
Bram Moolenaareffed932018-08-14 13:38:17 +02002512 emsg_silent -= ea.did_esilent;
Bram Moolenaar2be57332018-02-13 18:05:18 +01002513 if (emsg_silent < 0)
2514 emsg_silent = 0;
Bram Moolenaareffed932018-08-14 13:38:17 +02002515 ea.did_esilent = 0;
Bram Moolenaar2be57332018-02-13 18:05:18 +01002516 }
2517
Bram Moolenaar071d4272004-06-13 20:20:40 +00002518/*
Bram Moolenaar2be57332018-02-13 18:05:18 +01002519 * 7. Execute the command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002520 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002521
Bram Moolenaar958636c2014-10-21 20:01:58 +02002522 if (IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002523 {
2524 /*
2525 * Execute a user-defined command.
2526 */
2527 do_ucmd(&ea);
2528 }
2529 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002530 {
2531 /*
Bram Moolenaarac9fb182019-04-27 13:04:13 +02002532 * Call the function to execute the builtin command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002533 */
2534 ea.errmsg = NULL;
2535 (cmdnames[ea.cmdidx].cmd_func)(&ea);
2536 if (ea.errmsg != NULL)
Bram Moolenaar8930caa2020-07-23 16:37:03 +02002537 errormsg = ea.errmsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002538 }
2539
2540#ifdef FEAT_EVAL
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002541 // Set flag that any command was executed, used by ex_vim9script().
Bram Moolenaar16531552020-02-08 16:00:46 +01002542 if (getline_equal(ea.getline, ea.cookie, getsourceline)
2543 && current_sctx.sc_sid > 0)
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002544 SCRIPT_ITEM(current_sctx.sc_sid)->sn_had_command = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002545
Bram Moolenaar071d4272004-06-13 20:20:40 +00002546 /*
2547 * If the command just executed called do_cmdline(), any throw or ":return"
2548 * or ":finish" encountered there must also check the cstack of the still
2549 * active do_cmdline() that called this do_one_cmd(). Rethrow an uncaught
2550 * exception, or reanimate a returned function or finished script file and
2551 * return or finish it again.
2552 */
2553 if (need_rethrow)
2554 do_throw(cstack);
2555 else if (check_cstack)
2556 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002557 if (source_finished(fgetline, cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002558 do_finish(&ea, TRUE);
Bram Moolenaar89d40322006-08-29 15:30:07 +00002559 else if (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002560 && current_func_returned())
2561 do_return(&ea, TRUE, FALSE, NULL);
2562 }
2563 need_rethrow = check_cstack = FALSE;
2564#endif
2565
2566doend:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002567 if (curwin->w_cursor.lnum == 0) // can happen with zero line number
Bram Moolenaar6de5e122017-04-20 21:55:44 +02002568 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002569 curwin->w_cursor.lnum = 1;
Bram Moolenaar6de5e122017-04-20 21:55:44 +02002570 curwin->w_cursor.col = 0;
2571 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002572
2573 if (errormsg != NULL && *errormsg != NUL && !did_emsg)
2574 {
2575 if (sourcing)
2576 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002577 if (errormsg != (char *)IObuff)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002578 {
2579 STRCPY(IObuff, errormsg);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002580 errormsg = (char *)IObuff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002581 }
Bram Moolenaara6f4d612011-09-21 19:10:46 +02002582 append_command(*cmdlinep);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002583 }
2584 emsg(errormsg);
2585 }
2586#ifdef FEAT_EVAL
2587 do_errthrow(cstack,
Bram Moolenaar958636c2014-10-21 20:01:58 +02002588 (ea.cmdidx != CMD_SIZE && !IS_USER_CMDIDX(ea.cmdidx))
2589 ? cmdnames[(int)ea.cmdidx].cmd_name : (char_u *)NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002590#endif
2591
Bram Moolenaar47e7d702020-07-05 18:18:42 +02002592 undo_cmdmod(&ea, save_msg_scroll);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002593 cmdmod = save_cmdmod;
Bram Moolenaar9a2c0912019-03-30 14:26:18 +01002594 reg_executing = save_reg_executing;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002595
Bram Moolenaar7171abe2004-10-11 10:06:20 +00002596#ifdef HAVE_SANDBOX
Bram Moolenaareffed932018-08-14 13:38:17 +02002597 if (ea.did_sandbox)
Bram Moolenaar7171abe2004-10-11 10:06:20 +00002598 --sandbox;
2599#endif
2600
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002601 if (ea.nextcmd && *ea.nextcmd == NUL) // not really a next command
Bram Moolenaar071d4272004-06-13 20:20:40 +00002602 ea.nextcmd = NULL;
2603
2604#ifdef FEAT_EVAL
2605 --ex_nesting_level;
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002606 vim_free(ea.cmdline_tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002607#endif
2608
2609 return ea.nextcmd;
2610}
2611#if (_MSC_VER == 1200)
Bram Moolenaar281bdce2005-01-25 21:53:18 +00002612 #pragma optimize( "", on )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002613#endif
2614
Bram Moolenaar8930caa2020-07-23 16:37:03 +02002615static char ex_error_buf[MSG_BUF_LEN];
2616
2617/*
2618 * Return an error message with argument included.
2619 * Uses a static buffer, only the last error will be kept.
2620 * "msg" will be translated, caller should use N_().
2621 */
2622 char *
2623ex_errmsg(char *msg, char_u *arg)
2624{
2625 vim_snprintf(ex_error_buf, MSG_BUF_LEN, _(msg), arg);
2626 return ex_error_buf;
2627}
2628
Bram Moolenaar071d4272004-06-13 20:20:40 +00002629/*
Bram Moolenaareffed932018-08-14 13:38:17 +02002630 * Parse and skip over command modifiers:
2631 * - update eap->cmd
2632 * - store flags in "cmdmod".
2633 * - Set ex_pressedreturn for an empty command line.
2634 * - set msg_silent for ":silent"
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002635 * - set 'eventignore' to "all" for ":noautocmd"
Bram Moolenaareffed932018-08-14 13:38:17 +02002636 * - set p_verbose for ":verbose"
2637 * - Increment "sandbox" for ":sandbox"
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002638 * When "skip_only" is TRUE the global variables are not changed, except for
2639 * "cmdmod".
Bram Moolenaareffed932018-08-14 13:38:17 +02002640 * Return FAIL when the command is not to be executed.
2641 * May set "errormsg" to an error message.
2642 */
2643 int
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002644parse_command_modifiers(exarg_T *eap, char **errormsg, int skip_only)
Bram Moolenaareffed932018-08-14 13:38:17 +02002645{
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002646 char_u *p;
2647 int starts_with_colon = FALSE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002648
Bram Moolenaara80faa82020-04-12 19:37:17 +02002649 CLEAR_FIELD(cmdmod);
Bram Moolenaareffed932018-08-14 13:38:17 +02002650 eap->verbose_save = -1;
2651 eap->save_msg_silent = -1;
2652
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002653 // Repeat until no more command modifiers are found.
Bram Moolenaareffed932018-08-14 13:38:17 +02002654 for (;;)
2655 {
Bram Moolenaareffed932018-08-14 13:38:17 +02002656 while (*eap->cmd == ' ' || *eap->cmd == '\t' || *eap->cmd == ':')
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002657 {
2658 if (*eap->cmd == ':')
2659 starts_with_colon = TRUE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002660 ++eap->cmd;
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002661 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002662
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002663 // in ex mode, an empty line works like :+
Bram Moolenaareffed932018-08-14 13:38:17 +02002664 if (*eap->cmd == NUL && exmode_active
2665 && (getline_equal(eap->getline, eap->cookie, getexmodeline)
2666 || getline_equal(eap->getline, eap->cookie, getexline))
2667 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
2668 {
2669 eap->cmd = (char_u *)"+";
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002670 if (!skip_only)
2671 ex_pressedreturn = TRUE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002672 }
2673
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002674 // ignore comment and empty lines
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002675 if (comment_start(eap->cmd, starts_with_colon))
Bram Moolenaareffed932018-08-14 13:38:17 +02002676 return FAIL;
2677 if (*eap->cmd == NUL)
2678 {
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002679 if (!skip_only)
2680 ex_pressedreturn = TRUE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002681 return FAIL;
2682 }
2683
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02002684 p = skip_range(eap->cmd, TRUE, NULL);
Bram Moolenaareffed932018-08-14 13:38:17 +02002685 switch (*p)
2686 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002687 // When adding an entry, also modify cmd_exists().
Bram Moolenaareffed932018-08-14 13:38:17 +02002688 case 'a': if (!checkforcmd(&eap->cmd, "aboveleft", 3))
2689 break;
2690 cmdmod.split |= WSP_ABOVE;
2691 continue;
2692
2693 case 'b': if (checkforcmd(&eap->cmd, "belowright", 3))
2694 {
2695 cmdmod.split |= WSP_BELOW;
2696 continue;
2697 }
2698 if (checkforcmd(&eap->cmd, "browse", 3))
2699 {
2700#ifdef FEAT_BROWSE_CMD
2701 cmdmod.browse = TRUE;
2702#endif
2703 continue;
2704 }
2705 if (!checkforcmd(&eap->cmd, "botright", 2))
2706 break;
2707 cmdmod.split |= WSP_BOT;
2708 continue;
2709
2710 case 'c': if (!checkforcmd(&eap->cmd, "confirm", 4))
2711 break;
2712#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
2713 cmdmod.confirm = TRUE;
2714#endif
2715 continue;
2716
2717 case 'k': if (checkforcmd(&eap->cmd, "keepmarks", 3))
2718 {
2719 cmdmod.keepmarks = TRUE;
2720 continue;
2721 }
2722 if (checkforcmd(&eap->cmd, "keepalt", 5))
2723 {
2724 cmdmod.keepalt = TRUE;
2725 continue;
2726 }
2727 if (checkforcmd(&eap->cmd, "keeppatterns", 5))
2728 {
2729 cmdmod.keeppatterns = TRUE;
2730 continue;
2731 }
2732 if (!checkforcmd(&eap->cmd, "keepjumps", 5))
2733 break;
2734 cmdmod.keepjumps = TRUE;
2735 continue;
2736
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002737 case 'f': // only accept ":filter {pat} cmd"
Bram Moolenaareffed932018-08-14 13:38:17 +02002738 {
2739 char_u *reg_pat;
2740
2741 if (!checkforcmd(&p, "filter", 4)
2742 || *p == NUL || ends_excmd(*p))
2743 break;
2744 if (*p == '!')
2745 {
2746 cmdmod.filter_force = TRUE;
2747 p = skipwhite(p + 1);
2748 if (*p == NUL || ends_excmd(*p))
2749 break;
2750 }
Bram Moolenaar1e624c92020-07-11 14:08:04 +02002751#ifdef FEAT_EVAL
Bram Moolenaarb074e8b2020-07-11 13:40:45 +02002752 // Avoid that "filter(arg)" is recognized.
2753 if (in_vim9script() && !VIM_ISWHITE(*p))
2754 break;
Bram Moolenaar1e624c92020-07-11 14:08:04 +02002755#endif
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002756 if (skip_only)
2757 p = skip_vimgrep_pat(p, NULL, NULL);
2758 else
2759 // NOTE: This puts a NUL after the pattern.
2760 p = skip_vimgrep_pat(p, &reg_pat, NULL);
Bram Moolenaareffed932018-08-14 13:38:17 +02002761 if (p == NULL || *p == NUL)
2762 break;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002763 if (!skip_only)
2764 {
2765 cmdmod.filter_regmatch.regprog =
Bram Moolenaareffed932018-08-14 13:38:17 +02002766 vim_regcomp(reg_pat, RE_MAGIC);
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002767 if (cmdmod.filter_regmatch.regprog == NULL)
2768 break;
2769 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002770 eap->cmd = p;
2771 continue;
2772 }
2773
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002774 // ":hide" and ":hide | cmd" are not modifiers
Bram Moolenaareffed932018-08-14 13:38:17 +02002775 case 'h': if (p != eap->cmd || !checkforcmd(&p, "hide", 3)
2776 || *p == NUL || ends_excmd(*p))
2777 break;
2778 eap->cmd = p;
2779 cmdmod.hide = TRUE;
2780 continue;
2781
2782 case 'l': if (checkforcmd(&eap->cmd, "lockmarks", 3))
2783 {
2784 cmdmod.lockmarks = TRUE;
2785 continue;
2786 }
2787
2788 if (!checkforcmd(&eap->cmd, "leftabove", 5))
2789 break;
2790 cmdmod.split |= WSP_ABOVE;
2791 continue;
2792
2793 case 'n': if (checkforcmd(&eap->cmd, "noautocmd", 3))
2794 {
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002795 if (cmdmod.save_ei == NULL && !skip_only)
Bram Moolenaareffed932018-08-14 13:38:17 +02002796 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002797 // Set 'eventignore' to "all". Restore the
2798 // existing option value later.
Bram Moolenaareffed932018-08-14 13:38:17 +02002799 cmdmod.save_ei = vim_strsave(p_ei);
2800 set_string_option_direct((char_u *)"ei", -1,
2801 (char_u *)"all", OPT_FREE, SID_NONE);
2802 }
2803 continue;
2804 }
2805 if (!checkforcmd(&eap->cmd, "noswapfile", 3))
2806 break;
2807 cmdmod.noswapfile = TRUE;
2808 continue;
2809
2810 case 'r': if (!checkforcmd(&eap->cmd, "rightbelow", 6))
2811 break;
2812 cmdmod.split |= WSP_BELOW;
2813 continue;
2814
2815 case 's': if (checkforcmd(&eap->cmd, "sandbox", 3))
2816 {
2817#ifdef HAVE_SANDBOX
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002818 if (!skip_only)
2819 {
2820 if (!eap->did_sandbox)
2821 ++sandbox;
2822 eap->did_sandbox = TRUE;
2823 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002824#endif
2825 continue;
2826 }
2827 if (!checkforcmd(&eap->cmd, "silent", 3))
2828 break;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002829 if (!skip_only)
2830 {
2831 if (eap->save_msg_silent == -1)
2832 eap->save_msg_silent = msg_silent;
2833 ++msg_silent;
2834 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002835 if (*eap->cmd == '!' && !VIM_ISWHITE(eap->cmd[-1]))
2836 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002837 // ":silent!", but not "silent !cmd"
Bram Moolenaareffed932018-08-14 13:38:17 +02002838 eap->cmd = skipwhite(eap->cmd + 1);
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002839 if (!skip_only)
2840 {
2841 ++emsg_silent;
2842 ++eap->did_esilent;
2843 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002844 }
2845 continue;
2846
2847 case 't': if (checkforcmd(&p, "tab", 3))
2848 {
Bram Moolenaar548e5982018-12-26 21:45:00 +01002849 if (!skip_only)
Bram Moolenaareffed932018-08-14 13:38:17 +02002850 {
Bram Moolenaar548e5982018-12-26 21:45:00 +01002851 long tabnr = get_address(eap, &eap->cmd,
2852 ADDR_TABS, eap->skip,
2853 skip_only, FALSE, 1);
2854 if (tabnr == MAXLNUM)
2855 cmdmod.tab = tabpage_index(curtab) + 1;
2856 else
Bram Moolenaareffed932018-08-14 13:38:17 +02002857 {
Bram Moolenaar548e5982018-12-26 21:45:00 +01002858 if (tabnr < 0 || tabnr > LAST_TAB_NR)
2859 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002860 *errormsg = _(e_invrange);
Bram Moolenaar548e5982018-12-26 21:45:00 +01002861 return FAIL;
2862 }
2863 cmdmod.tab = tabnr + 1;
Bram Moolenaareffed932018-08-14 13:38:17 +02002864 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002865 }
2866 eap->cmd = p;
2867 continue;
2868 }
2869 if (!checkforcmd(&eap->cmd, "topleft", 2))
2870 break;
2871 cmdmod.split |= WSP_TOP;
2872 continue;
2873
2874 case 'u': if (!checkforcmd(&eap->cmd, "unsilent", 3))
2875 break;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002876 if (!skip_only)
2877 {
2878 if (eap->save_msg_silent == -1)
2879 eap->save_msg_silent = msg_silent;
2880 msg_silent = 0;
2881 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002882 continue;
2883
2884 case 'v': if (checkforcmd(&eap->cmd, "vertical", 4))
2885 {
2886 cmdmod.split |= WSP_VERT;
2887 continue;
2888 }
2889 if (!checkforcmd(&p, "verbose", 4))
2890 break;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002891 if (!skip_only)
2892 {
2893 if (eap->verbose_save < 0)
2894 eap->verbose_save = p_verbose;
2895 if (vim_isdigit(*eap->cmd))
2896 p_verbose = atoi((char *)eap->cmd);
2897 else
2898 p_verbose = 1;
2899 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002900 eap->cmd = p;
2901 continue;
2902 }
2903 break;
2904 }
2905
2906 return OK;
2907}
2908
2909/*
Bram Moolenaarb074e8b2020-07-11 13:40:45 +02002910 * Undo and free contents of "cmdmod".
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002911 */
Bram Moolenaar47e7d702020-07-05 18:18:42 +02002912 void
2913undo_cmdmod(exarg_T *eap, int save_msg_scroll)
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002914{
Bram Moolenaar47e7d702020-07-05 18:18:42 +02002915 if (eap->verbose_save >= 0)
2916 p_verbose = eap->verbose_save;
2917
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002918 if (cmdmod.save_ei != NULL)
2919 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002920 // Restore 'eventignore' to the value before ":noautocmd".
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002921 set_string_option_direct((char_u *)"ei", -1, cmdmod.save_ei,
2922 OPT_FREE, SID_NONE);
2923 free_string_option(cmdmod.save_ei);
2924 }
2925
2926 if (cmdmod.filter_regmatch.regprog != NULL)
2927 vim_regfree(cmdmod.filter_regmatch.regprog);
Bram Moolenaar47e7d702020-07-05 18:18:42 +02002928
2929 if (eap->save_msg_silent != -1)
2930 {
2931 // messages could be enabled for a serious error, need to check if the
2932 // counters don't become negative
2933 if (!did_emsg || msg_silent > eap->save_msg_silent)
2934 msg_silent = eap->save_msg_silent;
2935 emsg_silent -= eap->did_esilent;
2936 if (emsg_silent < 0)
2937 emsg_silent = 0;
2938 // Restore msg_scroll, it's set by file I/O commands, even when no
2939 // message is actually displayed.
2940 msg_scroll = save_msg_scroll;
2941
2942 // "silent reg" or "silent echo x" inside "redir" leaves msg_col
2943 // somewhere in the line. Put it back in the first column.
2944 if (redirecting())
2945 msg_col = 0;
2946 }
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002947}
2948
2949/*
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002950 * Parse the address range, if any, in "eap".
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02002951 * May set the last search pattern, unless "silent" is TRUE.
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002952 * Return FAIL and set "errormsg" or return OK.
2953 */
2954 int
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002955parse_cmd_address(exarg_T *eap, char **errormsg, int silent)
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002956{
2957 int address_count = 1;
2958 linenr_T lnum;
2959
2960 // Repeat for all ',' or ';' separated addresses.
2961 for (;;)
2962 {
2963 eap->line1 = eap->line2;
Bram Moolenaarc2af0af2020-08-23 21:06:02 +02002964 eap->line2 = default_address(eap);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002965 eap->cmd = skipwhite(eap->cmd);
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02002966 lnum = get_address(eap, &eap->cmd, eap->addr_type, eap->skip, silent,
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002967 eap->addr_count == 0, address_count++);
2968 if (eap->cmd == NULL) // error detected
2969 return FAIL;
2970 if (lnum == MAXLNUM)
2971 {
2972 if (*eap->cmd == '%') // '%' - all lines
2973 {
2974 ++eap->cmd;
2975 switch (eap->addr_type)
2976 {
2977 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02002978 case ADDR_OTHER:
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002979 eap->line1 = 1;
2980 eap->line2 = curbuf->b_ml.ml_line_count;
2981 break;
2982 case ADDR_LOADED_BUFFERS:
2983 {
2984 buf_T *buf = firstbuf;
2985
2986 while (buf->b_next != NULL
2987 && buf->b_ml.ml_mfp == NULL)
2988 buf = buf->b_next;
2989 eap->line1 = buf->b_fnum;
2990 buf = lastbuf;
2991 while (buf->b_prev != NULL
2992 && buf->b_ml.ml_mfp == NULL)
2993 buf = buf->b_prev;
2994 eap->line2 = buf->b_fnum;
2995 break;
2996 }
2997 case ADDR_BUFFERS:
2998 eap->line1 = firstbuf->b_fnum;
2999 eap->line2 = lastbuf->b_fnum;
3000 break;
3001 case ADDR_WINDOWS:
3002 case ADDR_TABS:
3003 if (IS_USER_CMDIDX(eap->cmdidx))
3004 {
3005 eap->line1 = 1;
3006 eap->line2 = eap->addr_type == ADDR_WINDOWS
3007 ? LAST_WIN_NR : LAST_TAB_NR;
3008 }
3009 else
3010 {
3011 // there is no Vim command which uses '%' and
3012 // ADDR_WINDOWS or ADDR_TABS
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003013 *errormsg = _(e_invrange);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003014 return FAIL;
3015 }
3016 break;
3017 case ADDR_TABS_RELATIVE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02003018 case ADDR_UNSIGNED:
3019 case ADDR_QUICKFIX:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003020 *errormsg = _(e_invrange);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003021 return FAIL;
3022 case ADDR_ARGUMENTS:
3023 if (ARGCOUNT == 0)
3024 eap->line1 = eap->line2 = 0;
3025 else
3026 {
3027 eap->line1 = 1;
3028 eap->line2 = ARGCOUNT;
3029 }
3030 break;
Bram Moolenaar25190db2019-05-04 15:05:28 +02003031 case ADDR_QUICKFIX_VALID:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003032#ifdef FEAT_QUICKFIX
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003033 eap->line1 = 1;
Bram Moolenaar25190db2019-05-04 15:05:28 +02003034 eap->line2 = qf_get_valid_size(eap);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003035 if (eap->line2 == 0)
3036 eap->line2 = 1;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003037#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003038 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02003039 case ADDR_NONE:
3040 // Will give an error later if a range is found.
3041 break;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003042 }
3043 ++eap->addr_count;
3044 }
3045 else if (*eap->cmd == '*' && vim_strchr(p_cpo, CPO_STAR) == NULL)
3046 {
3047 pos_T *fp;
3048
3049 // '*' - visual area
3050 if (eap->addr_type != ADDR_LINES)
3051 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003052 *errormsg = _(e_invrange);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003053 return FAIL;
3054 }
3055
3056 ++eap->cmd;
3057 if (!eap->skip)
3058 {
3059 fp = getmark('<', FALSE);
3060 if (check_mark(fp) == FAIL)
3061 return FAIL;
3062 eap->line1 = fp->lnum;
3063 fp = getmark('>', FALSE);
3064 if (check_mark(fp) == FAIL)
3065 return FAIL;
3066 eap->line2 = fp->lnum;
3067 ++eap->addr_count;
3068 }
3069 }
3070 }
3071 else
3072 eap->line2 = lnum;
3073 eap->addr_count++;
3074
3075 if (*eap->cmd == ';')
3076 {
3077 if (!eap->skip)
3078 {
3079 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaar0e717042020-04-27 19:29:01 +02003080 // Don't leave the cursor on an illegal line or column, but do
3081 // accept zero as address, so 0;/PATTERN/ works correctly.
3082 if (eap->line2 > 0)
3083 check_cursor();
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003084 }
3085 }
3086 else if (*eap->cmd != ',')
3087 break;
3088 ++eap->cmd;
3089 }
3090
3091 // One address given: set start and end lines.
3092 if (eap->addr_count == 1)
3093 {
3094 eap->line1 = eap->line2;
3095 // ... but only implicit: really no address given
3096 if (lnum == MAXLNUM)
3097 eap->addr_count = 0;
3098 }
3099 return OK;
3100}
3101
3102/*
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00003103 * Check for an Ex command with optional tail.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003104 * If there is a match advance "pp" to the argument and return TRUE.
3105 */
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00003106 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003107checkforcmd(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003108 char_u **pp, // start of command
3109 char *cmd, // name of command
3110 int len) // required length
Bram Moolenaar071d4272004-06-13 20:20:40 +00003111{
3112 int i;
3113
3114 for (i = 0; cmd[i] != NUL; ++i)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003115 if (((char_u *)cmd)[i] != (*pp)[i])
Bram Moolenaar071d4272004-06-13 20:20:40 +00003116 break;
3117 if (i >= len && !isalpha((*pp)[i]))
3118 {
3119 *pp = skipwhite(*pp + i);
3120 return TRUE;
3121 }
3122 return FALSE;
3123}
3124
3125/*
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003126 * Append "cmd" to the error message in IObuff.
3127 * Takes care of limiting the length and handling 0xa0, which would be
3128 * invisible otherwise.
3129 */
3130 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003131append_command(char_u *cmd)
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003132{
3133 char_u *s = cmd;
3134 char_u *d;
3135
3136 STRCAT(IObuff, ": ");
3137 d = IObuff + STRLEN(IObuff);
3138 while (*s != NUL && d - IObuff < IOSIZE - 7)
3139 {
Bram Moolenaar13505972019-01-24 15:04:48 +01003140 if (enc_utf8 ? (s[0] == 0xc2 && s[1] == 0xa0) : *s == 0xa0)
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003141 {
Bram Moolenaar13505972019-01-24 15:04:48 +01003142 s += enc_utf8 ? 2 : 1;
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003143 STRCPY(d, "<a0>");
3144 d += 4;
3145 }
3146 else
3147 MB_COPY_CHAR(s, d);
3148 }
3149 *d = NUL;
3150}
3151
3152/*
Bram Moolenaar2e800952020-08-23 19:34:48 +02003153 * If "start" points "&opt", "&l:opt", "&g:opt" or "$ENV" return a pointer to
3154 * the name. Otherwise just return "start".
3155 */
3156 char_u *
3157skip_option_env_lead(char_u *start)
3158{
3159 char_u *name = start;
3160
3161 if (*start == '&')
3162 {
3163 if ((start[1] == 'l' || start[1] == 'g') && start[2] == ':')
3164 name += 3;
3165 else
3166 name += 1;
3167 }
3168 else if (*start == '$')
3169 name += 1;
3170 return name;
3171}
3172
3173/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003174 * Find an Ex command by its name, either built-in or user.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003175 * Start of the name can be found at eap->cmd.
Bram Moolenaar25190db2019-05-04 15:05:28 +02003176 * Sets eap->cmdidx and returns a pointer to char after the command name.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003177 * "full" is set to TRUE if the whole command name matched.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003178 *
3179 * If "lookup" is not NULL recognize expression without "eval" or "call" and
3180 * assignment without "let". Sets eap->cmdidx to the command while returning
3181 * "eap->cmd".
3182 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00003183 * Returns NULL for an ambiguous user command.
3184 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003185 char_u *
3186find_ex_command(
3187 exarg_T *eap,
Bram Moolenaara494f562020-04-18 17:45:38 +02003188 int *full UNUSED,
Bram Moolenaarb84a3812020-05-01 15:44:29 +02003189 void *(*lookup)(char_u *, size_t, cctx_T *) UNUSED,
Bram Moolenaara494f562020-04-18 17:45:38 +02003190 cctx_T *cctx UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003191{
3192 int len;
3193 char_u *p;
Bram Moolenaardf177f62005-02-22 08:39:57 +00003194 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003195
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003196#ifdef FEAT_EVAL
3197 /*
3198 * Recognize a Vim9 script function/method call and assignment:
3199 * "lvar = value", "lvar(arg)", "[1, 2 3]->Func()"
3200 */
Bram Moolenaar0c6ceaf2020-02-22 18:36:32 +01003201 p = eap->cmd;
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003202 if (lookup != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003203 {
Bram Moolenaar2e800952020-08-23 19:34:48 +02003204 char_u *pskip = skip_option_env_lead(eap->cmd);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003205
Bram Moolenaar83144542020-08-02 20:40:43 +02003206 if (vim_strchr((char_u *)"{('[\"@", *p) != NULL
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003207 || ((p = to_name_const_end(pskip)) > eap->cmd && *p != NUL))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003208 {
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003209 int oplen;
3210 int heredoc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003211
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003212 if (
3213 // "(..." is an expression.
3214 // "funcname(" is always a function call.
3215 *p == '('
3216 || (p == eap->cmd
3217 ? (
3218 // "{..." is an dict expression.
3219 *eap->cmd == '{'
3220 // "'string'->func()" is an expression.
3221 || *eap->cmd == '\''
Bram Moolenaar10409562020-07-29 20:00:38 +02003222 // '"string"->func()' is an expression.
3223 || *eap->cmd == '"'
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003224 // "g:varname" is an expression.
3225 || eap->cmd[1] == ':'
3226 )
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003227 // "varname->func()" is an expression.
Bram Moolenaar2b22b112020-09-14 18:35:18 +02003228 : (*p == '-' && p[1] == '>')))
Bram Moolenaarb31be3f2020-07-20 22:37:44 +02003229 {
3230 eap->cmdidx = CMD_eval;
3231 return eap->cmd;
3232 }
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003233
Bram Moolenaar2b22b112020-09-14 18:35:18 +02003234 if (p != eap->cmd && (
3235 // "varname[]" is an expression.
3236 *p == '['
3237 // "varname.key" is an expression.
3238 || (*p == '.' && ASCII_ISALPHA(p[1]))))
3239 {
3240 char_u *after = p;
3241
3242 // When followed by "=" or "+=" then it is an assignment.
3243 ++emsg_silent;
3244 if (skip_expr(&after) == OK
3245 && (*after == '='
3246 || (*after != NUL && after[1] == '=')))
3247 eap->cmdidx = CMD_let;
3248 else
3249 eap->cmdidx = CMD_eval;
3250 --emsg_silent;
3251 return eap->cmd;
3252 }
3253
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003254 // "[...]->Method()" is a list expression, but "[a, b] = Func()" is
3255 // an assignment.
3256 // If there is no line break inside the "[...]" then "p" is
3257 // advanced to after the "]" by to_name_const_end(): check if a "="
3258 // follows.
3259 // If "[...]" has a line break "p" still points at the "[" and it
3260 // can't be an assignment.
3261 if (*eap->cmd == '[')
Bram Moolenaarb31be3f2020-07-20 22:37:44 +02003262 {
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003263 p = to_name_const_end(eap->cmd);
3264 if (p == eap->cmd || *skipwhite(p) != '=')
3265 {
3266 eap->cmdidx = CMD_eval;
3267 return eap->cmd;
3268 }
3269 if (p > eap->cmd && *skipwhite(p) == '=')
3270 {
3271 eap->cmdidx = CMD_let;
3272 return eap->cmd;
3273 }
3274 }
3275
3276 // Recognize an assignment if we recognize the variable name:
3277 // "g:var = expr"
3278 // "var = expr" where "var" is a local var name.
Bram Moolenaar83144542020-08-02 20:40:43 +02003279 if (*eap->cmd == '@')
3280 p = eap->cmd + 2;
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003281 oplen = assignment_len(skipwhite(p), &heredoc);
3282 if (oplen > 0)
3283 {
3284 if (((p - eap->cmd) > 2 && eap->cmd[1] == ':')
3285 || *eap->cmd == '&'
3286 || *eap->cmd == '$'
3287 || *eap->cmd == '@'
3288 || lookup(eap->cmd, p - eap->cmd, cctx) != NULL)
3289 {
3290 eap->cmdidx = CMD_let;
3291 return eap->cmd;
3292 }
3293 }
3294
3295 // Recognize using a type for a w:, b:, t: or g: variable:
3296 // "w:varname: number = 123".
3297 if (eap->cmd[1] == ':' && *p == ':')
3298 {
3299 eap->cmdidx = CMD_eval;
Bram Moolenaarb31be3f2020-07-20 22:37:44 +02003300 return eap->cmd;
3301 }
Bram Moolenaard2ef6b32020-07-02 21:11:34 +02003302 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003303 }
3304#endif
3305
Bram Moolenaar071d4272004-06-13 20:20:40 +00003306 /*
3307 * Isolate the command and search for it in the command table.
Bram Moolenaar81870892007-11-11 18:17:28 +00003308 * Exceptions:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003309 * - the 'k' command can directly be followed by any character.
3310 * - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003311 * but :sre[wind] is another command, as are :scr[iptnames],
Bram Moolenaar071d4272004-06-13 20:20:40 +00003312 * :scs[cope], :sim[alt], :sig[ns] and :sil[ent].
Bram Moolenaardf177f62005-02-22 08:39:57 +00003313 * - the "d" command can directly be followed by 'l' or 'p' flag.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003314 */
3315 p = eap->cmd;
3316 if (*p == 'k')
3317 {
3318 eap->cmdidx = CMD_k;
3319 ++p;
3320 }
3321 else if (p[0] == 's'
Bram Moolenaar204b93f2015-08-04 22:02:51 +02003322 && ((p[1] == 'c' && (p[2] == NUL || (p[2] != 's' && p[2] != 'r'
3323 && (p[3] == NUL || (p[3] != 'i' && p[4] != 'p')))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003324 || p[1] == 'g'
3325 || (p[1] == 'i' && p[2] != 'm' && p[2] != 'l' && p[2] != 'g')
3326 || p[1] == 'I'
3327 || (p[1] == 'r' && p[2] != 'e')))
3328 {
3329 eap->cmdidx = CMD_substitute;
3330 ++p;
3331 }
3332 else
3333 {
3334 while (ASCII_ISALPHA(*p))
3335 ++p;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003336 // for python 3.x support ":py3", ":python3", ":py3file", etc.
Bram Moolenaar55d5c032010-07-17 23:52:29 +02003337 if (eap->cmd[0] == 'p' && eap->cmd[1] == 'y')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003338 {
Bram Moolenaarb6590522010-07-21 16:00:43 +02003339 while (ASCII_ISALNUM(*p))
3340 ++p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003341 }
3342 else if (*p == '9' && STRNCMP("vim9", eap->cmd, 4) == 0)
3343 {
3344 // include "9" for "vim9script"
3345 ++p;
3346 while (ASCII_ISALPHA(*p))
3347 ++p;
3348 }
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02003349
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003350 // check for non-alpha command
Bram Moolenaar071d4272004-06-13 20:20:40 +00003351 if (p == eap->cmd && vim_strchr((char_u *)"@*!=><&~#", *p) != NULL)
3352 ++p;
3353 len = (int)(p - eap->cmd);
Bram Moolenaardf177f62005-02-22 08:39:57 +00003354 if (*eap->cmd == 'd' && (p[-1] == 'l' || p[-1] == 'p'))
3355 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003356 // Check for ":dl", ":dell", etc. to ":deletel": that's
3357 // :delete with the 'l' flag. Same for 'p'.
Bram Moolenaardf177f62005-02-22 08:39:57 +00003358 for (i = 0; i < len; ++i)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003359 if (eap->cmd[i] != ((char_u *)"delete")[i])
Bram Moolenaardf177f62005-02-22 08:39:57 +00003360 break;
3361 if (i == len - 1)
3362 {
3363 --len;
3364 if (p[-1] == 'l')
3365 eap->flags |= EXFLAG_LIST;
3366 else
3367 eap->flags |= EXFLAG_PRINT;
3368 }
3369 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003370
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003371 if (ASCII_ISLOWER(eap->cmd[0]))
3372 {
Bram Moolenaar6c0c1e82017-03-25 15:07:43 +01003373 int c1 = eap->cmd[0];
Bram Moolenaar94f82cb2019-07-24 22:30:27 +02003374 int c2 = len == 1 ? NUL : eap->cmd[1];
Bram Moolenaar6c0c1e82017-03-25 15:07:43 +01003375
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003376 if (command_count != (int)CMD_SIZE)
3377 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003378 iemsg(_("E943: Command table needs to be updated, run 'make cmdidxs'"));
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003379 getout(1);
3380 }
3381
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003382 // Use a precomputed index for fast look-up in cmdnames[]
3383 // taking into account the first 2 letters of eap->cmd.
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003384 eap->cmdidx = cmdidxs1[CharOrdLow(c1)];
3385 if (ASCII_ISLOWER(c2))
3386 eap->cmdidx += cmdidxs2[CharOrdLow(c1)][CharOrdLow(c2)];
3387 }
Bram Moolenaara494f562020-04-18 17:45:38 +02003388 else if (ASCII_ISUPPER(eap->cmd[0]))
3389 eap->cmdidx = CMD_Next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003390 else
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003391 eap->cmdidx = CMD_bang;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003392
3393 for ( ; (int)eap->cmdidx < (int)CMD_SIZE;
3394 eap->cmdidx = (cmdidx_T)((int)eap->cmdidx + 1))
3395 if (STRNCMP(cmdnames[(int)eap->cmdidx].cmd_name, (char *)eap->cmd,
3396 (size_t)len) == 0)
3397 {
3398#ifdef FEAT_EVAL
3399 if (full != NULL
3400 && cmdnames[(int)eap->cmdidx].cmd_name[len] == NUL)
3401 *full = TRUE;
3402#endif
3403 break;
3404 }
3405
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003406 // Look for a user defined command as a last resort. Let ":Print" be
3407 // overruled by a user defined command.
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01003408 if ((eap->cmdidx == CMD_SIZE || eap->cmdidx == CMD_Print)
3409 && *eap->cmd >= 'A' && *eap->cmd <= 'Z')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003410 {
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003411 // User defined commands may contain digits.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003412 while (ASCII_ISALNUM(*p))
3413 ++p;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003414 p = find_ucmd(eap, p, full, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003415 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003416 if (p == eap->cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003417 eap->cmdidx = CMD_SIZE;
3418 }
3419
3420 return p;
3421}
3422
3423#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003424static struct cmdmod
3425{
3426 char *name;
3427 int minlen;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003428 int has_count; // :123verbose :3tab
Bram Moolenaared53fb92007-11-24 20:50:24 +00003429} cmdmods[] = {
3430 {"aboveleft", 3, FALSE},
3431 {"belowright", 3, FALSE},
3432 {"botright", 2, FALSE},
3433 {"browse", 3, FALSE},
3434 {"confirm", 4, FALSE},
Bram Moolenaar7b668e82016-08-23 23:51:21 +02003435 {"filter", 4, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003436 {"hide", 3, FALSE},
3437 {"keepalt", 5, FALSE},
3438 {"keepjumps", 5, FALSE},
3439 {"keepmarks", 3, FALSE},
Bram Moolenaara939e432013-11-09 05:30:26 +01003440 {"keeppatterns", 5, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003441 {"leftabove", 5, FALSE},
3442 {"lockmarks", 3, FALSE},
Bram Moolenaarca9f9582008-09-18 10:44:28 +00003443 {"noautocmd", 3, FALSE},
Bram Moolenaar5803ae62014-03-23 16:04:02 +01003444 {"noswapfile", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003445 {"rightbelow", 6, FALSE},
3446 {"sandbox", 3, FALSE},
3447 {"silent", 3, FALSE},
3448 {"tab", 3, TRUE},
3449 {"topleft", 2, FALSE},
Bram Moolenaar8e258a42009-07-09 13:55:43 +00003450 {"unsilent", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003451 {"verbose", 4, TRUE},
3452 {"vertical", 4, FALSE},
3453};
3454
3455/*
3456 * Return length of a command modifier (including optional count).
3457 * Return zero when it's not a modifier.
3458 */
3459 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003460modifier_len(char_u *cmd)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003461{
3462 int i, j;
3463 char_u *p = cmd;
3464
3465 if (VIM_ISDIGIT(*cmd))
3466 p = skipwhite(skipdigits(cmd));
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00003467 for (i = 0; i < (int)(sizeof(cmdmods) / sizeof(struct cmdmod)); ++i)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003468 {
3469 for (j = 0; p[j] != NUL; ++j)
3470 if (p[j] != cmdmods[i].name[j])
3471 break;
Bram Moolenaar2d473ab2013-06-12 17:12:24 +02003472 if (!ASCII_ISALPHA(p[j]) && j >= cmdmods[i].minlen
Bram Moolenaared53fb92007-11-24 20:50:24 +00003473 && (p == cmd || cmdmods[i].has_count))
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00003474 return j + (int)(p - cmd);
Bram Moolenaared53fb92007-11-24 20:50:24 +00003475 }
3476 return 0;
3477}
3478
Bram Moolenaar071d4272004-06-13 20:20:40 +00003479/*
3480 * Return > 0 if an Ex command "name" exists.
3481 * Return 2 if there is an exact match.
3482 * Return 3 if there is an ambiguous match.
3483 */
3484 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003485cmd_exists(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003486{
3487 exarg_T ea;
3488 int full = FALSE;
3489 int i;
3490 int j;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003491 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003492
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003493 // Check command modifiers.
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00003494 for (i = 0; i < (int)(sizeof(cmdmods) / sizeof(struct cmdmod)); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003495 {
3496 for (j = 0; name[j] != NUL; ++j)
3497 if (name[j] != cmdmods[i].name[j])
3498 break;
3499 if (name[j] == NUL && j >= cmdmods[i].minlen)
3500 return (cmdmods[i].name[j] == NUL ? 2 : 1);
3501 }
3502
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003503 // Check built-in commands and user defined commands.
3504 // For ":2match" and ":3match" we need to skip the number.
Bram Moolenaara9587612006-05-04 21:47:50 +00003505 ea.cmd = (*name == '2' || *name == '3') ? name + 1 : name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003506 ea.cmdidx = (cmdidx_T)0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003507 p = find_ex_command(&ea, &full, NULL, NULL);
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003508 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003509 return 3;
Bram Moolenaara9587612006-05-04 21:47:50 +00003510 if (vim_isdigit(*name) && ea.cmdidx != CMD_match)
3511 return 0;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003512 if (*skipwhite(p) != NUL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003513 return 0; // trailing garbage
Bram Moolenaar071d4272004-06-13 20:20:40 +00003514 return (ea.cmdidx == CMD_SIZE ? 0 : (full ? 2 : 1));
3515}
3516#endif
3517
Bram Moolenaard0190392019-08-23 21:17:35 +02003518 cmdidx_T
3519excmd_get_cmdidx(char_u *cmd, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003520{
Bram Moolenaard0190392019-08-23 21:17:35 +02003521 cmdidx_T idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003522
Bram Moolenaard0190392019-08-23 21:17:35 +02003523 for (idx = (cmdidx_T)0; (int)idx < (int)CMD_SIZE;
3524 idx = (cmdidx_T)((int)idx + 1))
3525 if (STRNCMP(cmdnames[(int)idx].cmd_name, cmd, (size_t)len) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003526 break;
3527
Bram Moolenaard0190392019-08-23 21:17:35 +02003528 return idx;
3529}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003530
Bram Moolenaard0190392019-08-23 21:17:35 +02003531 long
3532excmd_get_argt(cmdidx_T idx)
3533{
3534 return (long)cmdnames[(int)idx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003535}
3536
3537/*
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003538 * Skip a range specifier of the form: addr [,addr] [;addr] ..
Bram Moolenaar071d4272004-06-13 20:20:40 +00003539 *
3540 * Backslashed delimiters after / or ? will be skipped, and commands will
3541 * not be expanded between /'s and ?'s or after "'".
3542 *
Bram Moolenaardf069ee2020-06-22 23:02:51 +02003543 * Also skip white space and ":" characters after the range.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003544 * Returns the "cmd" pointer advanced to beyond the range.
3545 */
3546 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003547skip_range(
3548 char_u *cmd,
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02003549 int skip_star, // skip "*" used for Visual range
3550 int *ctx) // pointer to xp_context or NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003551{
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003552 unsigned delim;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003553
Bram Moolenaarcbf20fb2017-02-03 21:19:04 +01003554 while (vim_strchr((char_u *)" \t0123456789.$%'/?-+,;\\", *cmd) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003555 {
Bram Moolenaarcbf20fb2017-02-03 21:19:04 +01003556 if (*cmd == '\\')
3557 {
3558 if (cmd[1] == '?' || cmd[1] == '/' || cmd[1] == '&')
3559 ++cmd;
3560 else
3561 break;
3562 }
3563 else if (*cmd == '\'')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003564 {
3565 if (*++cmd == NUL && ctx != NULL)
3566 *ctx = EXPAND_NOTHING;
3567 }
3568 else if (*cmd == '/' || *cmd == '?')
3569 {
3570 delim = *cmd++;
3571 while (*cmd != NUL && *cmd != delim)
3572 if (*cmd++ == '\\' && *cmd != NUL)
3573 ++cmd;
3574 if (*cmd == NUL && ctx != NULL)
3575 *ctx = EXPAND_NOTHING;
3576 }
3577 if (*cmd != NUL)
3578 ++cmd;
3579 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00003580
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003581 // Skip ":" and white space.
Bram Moolenaardf177f62005-02-22 08:39:57 +00003582 while (*cmd == ':')
3583 cmd = skipwhite(cmd + 1);
3584
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02003585 // Skip "*" used for Visual range.
3586 if (skip_star && *cmd == '*' && vim_strchr(p_cpo, CPO_STAR) == NULL)
3587 cmd = skipwhite(cmd + 1);
3588
Bram Moolenaar071d4272004-06-13 20:20:40 +00003589 return cmd;
3590}
3591
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003592 static void
3593addr_error(cmd_addr_T addr_type)
3594{
3595 if (addr_type == ADDR_NONE)
3596 emsg(_(e_norange));
3597 else
3598 emsg(_(e_invrange));
3599}
3600
Bram Moolenaar071d4272004-06-13 20:20:40 +00003601/*
Bram Moolenaarc2af0af2020-08-23 21:06:02 +02003602 * Return the default address for an address type.
3603 */
3604 static linenr_T
3605default_address(exarg_T *eap)
3606{
3607 linenr_T lnum = 0;
3608
3609 switch (eap->addr_type)
3610 {
3611 case ADDR_LINES:
3612 case ADDR_OTHER:
3613 // Default is the cursor line number. Avoid using an invalid
3614 // line number though.
3615 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
3616 lnum = curbuf->b_ml.ml_line_count;
3617 else
3618 lnum = curwin->w_cursor.lnum;
3619 break;
3620 case ADDR_WINDOWS:
3621 lnum = CURRENT_WIN_NR;
3622 break;
3623 case ADDR_ARGUMENTS:
3624 lnum = curwin->w_arg_idx + 1;
3625 if (lnum > ARGCOUNT)
3626 lnum = ARGCOUNT;
3627 break;
3628 case ADDR_LOADED_BUFFERS:
3629 case ADDR_BUFFERS:
3630 lnum = curbuf->b_fnum;
3631 break;
3632 case ADDR_TABS:
3633 lnum = CURRENT_TAB_NR;
3634 break;
3635 case ADDR_TABS_RELATIVE:
3636 case ADDR_UNSIGNED:
3637 lnum = 1;
3638 break;
3639 case ADDR_QUICKFIX:
3640#ifdef FEAT_QUICKFIX
3641 lnum = qf_get_cur_idx(eap);
3642#endif
3643 break;
3644 case ADDR_QUICKFIX_VALID:
3645#ifdef FEAT_QUICKFIX
3646 lnum = qf_get_cur_valid_idx(eap);
3647#endif
3648 break;
3649 case ADDR_NONE:
3650 // Will give an error later if a range is found.
3651 break;
3652 }
3653 return lnum;
3654}
3655
3656/*
Bram Moolenaar198cb662018-09-06 21:44:17 +02003657 * Get a single EX address.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003658 *
3659 * Set ptr to the next character after the part that was interpreted.
3660 * Set ptr to NULL when an error is encountered.
Bram Moolenaar198cb662018-09-06 21:44:17 +02003661 * This may set the last used search pattern.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003662 *
3663 * Return MAXLNUM when no Ex address was found.
3664 */
3665 static linenr_T
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003666get_address(
3667 exarg_T *eap UNUSED,
3668 char_u **ptr,
Bram Moolenaar1b03a192019-12-08 17:08:29 +01003669 cmd_addr_T addr_type,
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02003670 int skip, // only skip the address, don't use it
3671 int silent, // no errors or side effects
3672 int to_other_file, // flag: may jump to other file
3673 int address_count UNUSED) // 1 for first address, >1 after comma
Bram Moolenaar071d4272004-06-13 20:20:40 +00003674{
3675 int c;
3676 int i;
3677 long n;
3678 char_u *cmd;
3679 pos_T pos;
3680 pos_T *fp;
3681 linenr_T lnum;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003682 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003683
3684 cmd = skipwhite(*ptr);
3685 lnum = MAXLNUM;
3686 do
3687 {
3688 switch (*cmd)
3689 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003690 case '.': // '.' - Cursor position
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003691 ++cmd;
3692 switch (addr_type)
3693 {
3694 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02003695 case ADDR_OTHER:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003696 lnum = curwin->w_cursor.lnum;
3697 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003698 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01003699 lnum = CURRENT_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003700 break;
3701 case ADDR_ARGUMENTS:
3702 lnum = curwin->w_arg_idx + 1;
3703 break;
3704 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003705 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003706 lnum = curbuf->b_fnum;
3707 break;
3708 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01003709 lnum = CURRENT_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003710 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02003711 case ADDR_NONE:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003712 case ADDR_TABS_RELATIVE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02003713 case ADDR_UNSIGNED:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003714 addr_error(addr_type);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01003715 cmd = NULL;
3716 goto error;
3717 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003718 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003719#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02003720 lnum = qf_get_cur_idx(eap);
3721#endif
3722 break;
3723 case ADDR_QUICKFIX_VALID:
3724#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003725 lnum = qf_get_cur_valid_idx(eap);
Bram Moolenaare906c502015-09-09 21:10:39 +02003726#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003727 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003728 }
3729 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003730
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003731 case '$': // '$' - last line
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003732 ++cmd;
3733 switch (addr_type)
3734 {
3735 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02003736 case ADDR_OTHER:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003737 lnum = curbuf->b_ml.ml_line_count;
3738 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003739 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01003740 lnum = LAST_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003741 break;
3742 case ADDR_ARGUMENTS:
3743 lnum = ARGCOUNT;
3744 break;
3745 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003746 buf = lastbuf;
3747 while (buf->b_ml.ml_mfp == NULL)
3748 {
3749 if (buf->b_prev == NULL)
3750 break;
3751 buf = buf->b_prev;
3752 }
3753 lnum = buf->b_fnum;
3754 break;
3755 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003756 lnum = lastbuf->b_fnum;
3757 break;
3758 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01003759 lnum = LAST_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003760 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02003761 case ADDR_NONE:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003762 case ADDR_TABS_RELATIVE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02003763 case ADDR_UNSIGNED:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003764 addr_error(addr_type);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01003765 cmd = NULL;
3766 goto error;
3767 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003768 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003769#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003770 lnum = qf_get_size(eap);
3771 if (lnum == 0)
3772 lnum = 1;
Bram Moolenaare906c502015-09-09 21:10:39 +02003773#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003774 break;
Bram Moolenaar25190db2019-05-04 15:05:28 +02003775 case ADDR_QUICKFIX_VALID:
3776#ifdef FEAT_QUICKFIX
3777 lnum = qf_get_valid_size(eap);
3778 if (lnum == 0)
3779 lnum = 1;
3780#endif
3781 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003782 }
3783 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003784
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003785 case '\'': // ''' - mark
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003786 if (*++cmd == NUL)
3787 {
3788 cmd = NULL;
3789 goto error;
3790 }
3791 if (addr_type != ADDR_LINES)
3792 {
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003793 addr_error(addr_type);
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01003794 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003795 goto error;
3796 }
3797 if (skip)
3798 ++cmd;
3799 else
3800 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003801 // Only accept a mark in another file when it is
3802 // used by itself: ":'M".
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003803 fp = getmark(*cmd, to_other_file && cmd[1] == NUL);
3804 ++cmd;
3805 if (fp == (pos_T *)-1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003806 // Jumped to another file.
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003807 lnum = curwin->w_cursor.lnum;
3808 else
3809 {
3810 if (check_mark(fp) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003811 {
3812 cmd = NULL;
3813 goto error;
3814 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003815 lnum = fp->lnum;
3816 }
3817 }
3818 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003819
3820 case '/':
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003821 case '?': // '/' or '?' - search
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003822 c = *cmd++;
3823 if (addr_type != ADDR_LINES)
3824 {
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003825 addr_error(addr_type);
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01003826 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003827 goto error;
3828 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003829 if (skip) // skip "/pat/"
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003830 {
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02003831 cmd = skip_regexp(cmd, c, (int)p_magic);
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003832 if (*cmd == c)
3833 ++cmd;
3834 }
3835 else
3836 {
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02003837 int flags;
3838
3839 pos = curwin->w_cursor; // save curwin->w_cursor
3840
3841 // When '/' or '?' follows another address, start from
3842 // there.
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003843 if (lnum != MAXLNUM)
3844 curwin->w_cursor.lnum = lnum;
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02003845
3846 // Start a forward search at the end of the line (unless
3847 // before the first line).
3848 // Start a backward search at the start of the line.
3849 // This makes sure we never match in the current
3850 // line, and can match anywhere in the
3851 // next/previous line.
Bram Moolenaar8ada6aa2017-12-19 21:23:21 +01003852 if (c == '/' && curwin->w_cursor.lnum > 0)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003853 curwin->w_cursor.col = MAXCOL;
3854 else
3855 curwin->w_cursor.col = 0;
3856 searchcmdlen = 0;
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02003857 flags = silent ? 0 : SEARCH_HIS | SEARCH_MSG;
Bram Moolenaarc036e872020-02-21 21:30:52 +01003858 if (!do_search(NULL, c, c, cmd, 1L, flags, NULL))
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003859 {
3860 curwin->w_cursor = pos;
3861 cmd = NULL;
3862 goto error;
3863 }
3864 lnum = curwin->w_cursor.lnum;
3865 curwin->w_cursor = pos;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003866 // adjust command string pointer
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003867 cmd += searchcmdlen;
3868 }
3869 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003870
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003871 case '\\': // "\?", "\/" or "\&", repeat search
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003872 ++cmd;
3873 if (addr_type != ADDR_LINES)
3874 {
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003875 addr_error(addr_type);
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01003876 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003877 goto error;
3878 }
3879 if (*cmd == '&')
3880 i = RE_SUBST;
3881 else if (*cmd == '?' || *cmd == '/')
3882 i = RE_SEARCH;
3883 else
3884 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003885 emsg(_(e_backslash));
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003886 cmd = NULL;
3887 goto error;
3888 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003889
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003890 if (!skip)
3891 {
3892 /*
3893 * When search follows another address, start from
3894 * there.
3895 */
3896 if (lnum != MAXLNUM)
3897 pos.lnum = lnum;
3898 else
3899 pos.lnum = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003900
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003901 /*
3902 * Start the search just like for the above
3903 * do_search().
3904 */
3905 if (*cmd != '?')
3906 pos.col = MAXCOL;
3907 else
3908 pos.col = 0;
Bram Moolenaarbd8539a2015-08-11 18:53:03 +02003909 pos.coladd = 0;
Bram Moolenaar5d24a222018-12-23 19:10:09 +01003910 if (searchit(curwin, curbuf, &pos, NULL,
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003911 *cmd == '?' ? BACKWARD : FORWARD,
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02003912 (char_u *)"", 1L, SEARCH_MSG, i, NULL) != FAIL)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003913 lnum = pos.lnum;
3914 else
3915 {
3916 cmd = NULL;
3917 goto error;
3918 }
3919 }
3920 ++cmd;
3921 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003922
3923 default:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003924 if (VIM_ISDIGIT(*cmd)) // absolute line number
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003925 lnum = getdigits(&cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003926 }
3927
3928 for (;;)
3929 {
3930 cmd = skipwhite(cmd);
3931 if (*cmd != '-' && *cmd != '+' && !VIM_ISDIGIT(*cmd))
3932 break;
3933
3934 if (lnum == MAXLNUM)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003935 {
3936 switch (addr_type)
3937 {
3938 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02003939 case ADDR_OTHER:
3940 // "+1" is same as ".+1"
Bram Moolenaarf240e182014-11-27 18:33:02 +01003941 lnum = curwin->w_cursor.lnum;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003942 break;
3943 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01003944 lnum = CURRENT_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003945 break;
3946 case ADDR_ARGUMENTS:
3947 lnum = curwin->w_arg_idx + 1;
3948 break;
3949 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003950 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003951 lnum = curbuf->b_fnum;
3952 break;
3953 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01003954 lnum = CURRENT_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003955 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01003956 case ADDR_TABS_RELATIVE:
3957 lnum = 1;
3958 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003959 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003960#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02003961 lnum = qf_get_cur_idx(eap);
3962#endif
3963 break;
3964 case ADDR_QUICKFIX_VALID:
3965#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003966 lnum = qf_get_cur_valid_idx(eap);
Bram Moolenaare906c502015-09-09 21:10:39 +02003967#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003968 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02003969 case ADDR_NONE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02003970 case ADDR_UNSIGNED:
3971 lnum = 0;
Bram Moolenaarb7316892019-05-01 18:08:42 +02003972 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003973 }
3974 }
3975
Bram Moolenaar071d4272004-06-13 20:20:40 +00003976 if (VIM_ISDIGIT(*cmd))
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003977 i = '+'; // "number" is same as "+number"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003978 else
3979 i = *cmd++;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003980 if (!VIM_ISDIGIT(*cmd)) // '+' is '+1', but '+0' is not '+1'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003981 n = 1;
3982 else
3983 n = getdigits(&cmd);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01003984
3985 if (addr_type == ADDR_TABS_RELATIVE)
3986 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003987 emsg(_(e_invrange));
Bram Moolenaar2f72c702017-01-29 14:48:10 +01003988 cmd = NULL;
3989 goto error;
3990 }
3991 else if (addr_type == ADDR_LOADED_BUFFERS
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003992 || addr_type == ADDR_BUFFERS)
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01003993 lnum = compute_buffer_local_count(
3994 addr_type, lnum, (i == '-') ? -1 * n : n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003995 else
Bram Moolenaarded27822017-01-02 14:27:34 +01003996 {
3997#ifdef FEAT_FOLDING
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003998 // Relative line addressing, need to adjust for folded lines
3999 // now, but only do it after the first address.
Bram Moolenaarded27822017-01-02 14:27:34 +01004000 if (addr_type == ADDR_LINES && (i == '-' || i == '+')
4001 && address_count >= 2)
4002 (void)hasFolding(lnum, NULL, &lnum);
4003#endif
4004 if (i == '-')
4005 lnum -= n;
4006 else
4007 lnum += n;
4008 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004009 }
4010 } while (*cmd == '/' || *cmd == '?');
4011
4012error:
4013 *ptr = cmd;
4014 return lnum;
4015}
4016
4017/*
Bram Moolenaarc2af0af2020-08-23 21:06:02 +02004018 * Set eap->line1 and eap->line2 to the whole range.
4019 * Used for commands with the EX_DFLALL flag and no range given.
4020 */
4021 static void
4022address_default_all(exarg_T *eap)
4023{
4024 eap->line1 = 1;
4025 switch (eap->addr_type)
4026 {
4027 case ADDR_LINES:
4028 case ADDR_OTHER:
4029 eap->line2 = curbuf->b_ml.ml_line_count;
4030 break;
4031 case ADDR_LOADED_BUFFERS:
4032 {
4033 buf_T *buf = firstbuf;
4034
4035 while (buf->b_next != NULL && buf->b_ml.ml_mfp == NULL)
4036 buf = buf->b_next;
4037 eap->line1 = buf->b_fnum;
4038 buf = lastbuf;
4039 while (buf->b_prev != NULL && buf->b_ml.ml_mfp == NULL)
4040 buf = buf->b_prev;
4041 eap->line2 = buf->b_fnum;
4042 }
4043 break;
4044 case ADDR_BUFFERS:
4045 eap->line1 = firstbuf->b_fnum;
4046 eap->line2 = lastbuf->b_fnum;
4047 break;
4048 case ADDR_WINDOWS:
4049 eap->line2 = LAST_WIN_NR;
4050 break;
4051 case ADDR_TABS:
4052 eap->line2 = LAST_TAB_NR;
4053 break;
4054 case ADDR_TABS_RELATIVE:
4055 eap->line2 = 1;
4056 break;
4057 case ADDR_ARGUMENTS:
4058 if (ARGCOUNT == 0)
4059 eap->line1 = eap->line2 = 0;
4060 else
4061 eap->line2 = ARGCOUNT;
4062 break;
4063 case ADDR_QUICKFIX_VALID:
4064#ifdef FEAT_QUICKFIX
4065 eap->line2 = qf_get_valid_size(eap);
4066 if (eap->line2 == 0)
4067 eap->line2 = 1;
4068#endif
4069 break;
4070 case ADDR_NONE:
4071 case ADDR_UNSIGNED:
4072 case ADDR_QUICKFIX:
4073 iemsg(_("INTERNAL: Cannot use EX_DFLALL with ADDR_NONE, ADDR_UNSIGNED or ADDR_QUICKFIX"));
4074 break;
4075 }
4076}
4077
4078
4079/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00004080 * Get flags from an Ex command argument.
4081 */
4082 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004083get_flags(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00004084{
4085 while (vim_strchr((char_u *)"lp#", *eap->arg) != NULL)
4086 {
4087 if (*eap->arg == 'l')
4088 eap->flags |= EXFLAG_LIST;
4089 else if (*eap->arg == 'p')
4090 eap->flags |= EXFLAG_PRINT;
4091 else
4092 eap->flags |= EXFLAG_NR;
4093 eap->arg = skipwhite(eap->arg + 1);
4094 }
4095}
4096
4097/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004098 * Function called for command which is Not Implemented. NI!
4099 */
4100 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004101ex_ni(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004102{
4103 if (!eap->skip)
Bram Moolenaar8930caa2020-07-23 16:37:03 +02004104 eap->errmsg =
4105 _("E319: Sorry, the command is not available in this version");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004106}
4107
Bram Moolenaar7bb75552007-07-16 18:39:49 +00004108#ifdef HAVE_EX_SCRIPT_NI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004109/*
4110 * Function called for script command which is Not Implemented. NI!
4111 * Skips over ":perl <<EOF" constructs.
4112 */
4113 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004114ex_script_ni(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004115{
4116 if (!eap->skip)
4117 ex_ni(eap);
4118 else
4119 vim_free(script_get(eap, eap->arg));
4120}
4121#endif
4122
4123/*
4124 * Check range in Ex command for validity.
4125 * Return NULL when valid, error message when invalid.
4126 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004127 static char *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004128invalid_range(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004129{
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004130 buf_T *buf;
Bram Moolenaar25190db2019-05-04 15:05:28 +02004131
Bram Moolenaar071d4272004-06-13 20:20:40 +00004132 if ( eap->line1 < 0
4133 || eap->line2 < 0
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004134 || eap->line1 > eap->line2)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004135 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004136
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004137 if (eap->argt & EX_RANGE)
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004138 {
Bram Moolenaarb7316892019-05-01 18:08:42 +02004139 switch (eap->addr_type)
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004140 {
4141 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004142 if (eap->line2 > curbuf->b_ml.ml_line_count
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004143#ifdef FEAT_DIFF
4144 + (eap->cmdidx == CMD_diffget)
4145#endif
4146 )
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004147 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004148 break;
4149 case ADDR_ARGUMENTS:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004150 // add 1 if ARGCOUNT is 0
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004151 if (eap->line2 > ARGCOUNT + (!ARGCOUNT))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004152 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004153 break;
4154 case ADDR_BUFFERS:
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02004155 // Only a boundary check, not whether the buffers actually
4156 // exist.
4157 if (eap->line1 < 1 || eap->line2 > get_highest_fnum())
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004158 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004159 break;
4160 case ADDR_LOADED_BUFFERS:
4161 buf = firstbuf;
4162 while (buf->b_ml.ml_mfp == NULL)
4163 {
4164 if (buf->b_next == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004165 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004166 buf = buf->b_next;
4167 }
4168 if (eap->line1 < buf->b_fnum)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004169 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004170 buf = lastbuf;
4171 while (buf->b_ml.ml_mfp == NULL)
4172 {
4173 if (buf->b_prev == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004174 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004175 buf = buf->b_prev;
4176 }
4177 if (eap->line2 > buf->b_fnum)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004178 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004179 break;
4180 case ADDR_WINDOWS:
Bram Moolenaar8be63882015-01-14 11:25:05 +01004181 if (eap->line2 > LAST_WIN_NR)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004182 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004183 break;
4184 case ADDR_TABS:
4185 if (eap->line2 > LAST_TAB_NR)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004186 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004187 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004188 case ADDR_TABS_RELATIVE:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004189 case ADDR_OTHER:
4190 // Any range is OK.
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004191 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004192 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004193#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02004194 // No error for value that is too big, will use the last entry.
4195 if (eap->line2 <= 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004196 return _(e_invrange);
Bram Moolenaare906c502015-09-09 21:10:39 +02004197#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004198 break;
Bram Moolenaar25190db2019-05-04 15:05:28 +02004199 case ADDR_QUICKFIX_VALID:
4200#ifdef FEAT_QUICKFIX
4201 if ((eap->line2 != 1 && eap->line2 > qf_get_valid_size(eap))
4202 || eap->line2 < 0)
4203 return _(e_invrange);
4204#endif
4205 break;
4206 case ADDR_UNSIGNED:
4207 if (eap->line2 < 0)
4208 return _(e_invrange);
4209 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02004210 case ADDR_NONE:
4211 // Will give an error elsewhere.
4212 break;
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004213 }
4214 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004215 return NULL;
4216}
4217
4218/*
4219 * Correct the range for zero line number, if required.
4220 */
4221 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004222correct_range(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004223{
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004224 if (!(eap->argt & EX_ZEROR)) // zero in range not allowed
Bram Moolenaar071d4272004-06-13 20:20:40 +00004225 {
4226 if (eap->line1 == 0)
4227 eap->line1 = 1;
4228 if (eap->line2 == 0)
4229 eap->line2 = 1;
4230 }
4231}
4232
Bram Moolenaar748bf032005-02-02 23:04:36 +00004233#ifdef FEAT_QUICKFIX
Bram Moolenaar748bf032005-02-02 23:04:36 +00004234/*
4235 * For a ":vimgrep" or ":vimgrepadd" command return a pointer past the
4236 * pattern. Otherwise return eap->arg.
4237 */
4238 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004239skip_grep_pat(exarg_T *eap)
Bram Moolenaar748bf032005-02-02 23:04:36 +00004240{
4241 char_u *p = eap->arg;
4242
Bram Moolenaara37420f2006-02-04 22:37:47 +00004243 if (*p != NUL && (eap->cmdidx == CMD_vimgrep || eap->cmdidx == CMD_lvimgrep
4244 || eap->cmdidx == CMD_vimgrepadd
4245 || eap->cmdidx == CMD_lvimgrepadd
4246 || grep_internal(eap->cmdidx)))
Bram Moolenaar748bf032005-02-02 23:04:36 +00004247 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00004248 p = skip_vimgrep_pat(p, NULL, NULL);
Bram Moolenaar748bf032005-02-02 23:04:36 +00004249 if (p == NULL)
4250 p = eap->arg;
Bram Moolenaar748bf032005-02-02 23:04:36 +00004251 }
4252 return p;
4253}
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004254
4255/*
4256 * For the ":make" and ":grep" commands insert the 'makeprg'/'grepprg' option
4257 * in the command line, so that things like % get expanded.
4258 */
4259 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004260replace_makeprg(exarg_T *eap, char_u *p, char_u **cmdlinep)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004261{
4262 char_u *new_cmdline;
4263 char_u *program;
4264 char_u *pos;
4265 char_u *ptr;
4266 int len;
4267 int i;
4268
4269 /*
4270 * Don't do it when ":vimgrep" is used for ":grep".
4271 */
Bram Moolenaara37420f2006-02-04 22:37:47 +00004272 if ((eap->cmdidx == CMD_make || eap->cmdidx == CMD_lmake
4273 || eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4274 || eap->cmdidx == CMD_grepadd
4275 || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004276 && !grep_internal(eap->cmdidx))
4277 {
Bram Moolenaara37420f2006-02-04 22:37:47 +00004278 if (eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4279 || eap->cmdidx == CMD_grepadd || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004280 {
4281 if (*curbuf->b_p_gp == NUL)
4282 program = p_gp;
4283 else
4284 program = curbuf->b_p_gp;
4285 }
4286 else
4287 {
4288 if (*curbuf->b_p_mp == NUL)
4289 program = p_mp;
4290 else
4291 program = curbuf->b_p_mp;
4292 }
4293
4294 p = skipwhite(p);
4295
4296 if ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4297 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004298 // replace $* by given arguments
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004299 i = 1;
4300 while ((pos = (char_u *)strstr((char *)pos + 2, "$*")) != NULL)
4301 ++i;
4302 len = (int)STRLEN(p);
Bram Moolenaar51e14382019-05-25 20:21:28 +02004303 new_cmdline = alloc(STRLEN(program) + i * (len - 2) + 1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004304 if (new_cmdline == NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004305 return NULL; // out of memory
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004306 ptr = new_cmdline;
4307 while ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4308 {
4309 i = (int)(pos - program);
4310 STRNCPY(ptr, program, i);
4311 STRCPY(ptr += i, p);
4312 ptr += len;
4313 program = pos + 2;
4314 }
4315 STRCPY(ptr, program);
4316 }
4317 else
4318 {
Bram Moolenaar51e14382019-05-25 20:21:28 +02004319 new_cmdline = alloc(STRLEN(program) + STRLEN(p) + 2);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004320 if (new_cmdline == NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004321 return NULL; // out of memory
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004322 STRCPY(new_cmdline, program);
4323 STRCAT(new_cmdline, " ");
4324 STRCAT(new_cmdline, p);
4325 }
4326 msg_make(p);
4327
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004328 // 'eap->cmd' is not set here, because it is not used at CMD_make
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004329 vim_free(*cmdlinep);
4330 *cmdlinep = new_cmdline;
4331 p = new_cmdline;
4332 }
4333 return p;
4334}
Bram Moolenaar748bf032005-02-02 23:04:36 +00004335#endif
4336
Bram Moolenaar071d4272004-06-13 20:20:40 +00004337/*
4338 * Expand file name in Ex command argument.
Bram Moolenaar0abb4272019-06-15 16:06:00 +02004339 * When an error is detected, "errormsgp" is set to a non-NULL pointer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004340 * Return FAIL for failure, OK otherwise.
4341 */
4342 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004343expand_filename(
4344 exarg_T *eap,
4345 char_u **cmdlinep,
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004346 char **errormsgp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004347{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004348 int has_wildcards; // need to expand wildcards
Bram Moolenaar071d4272004-06-13 20:20:40 +00004349 char_u *repl;
4350 int srclen;
4351 char_u *p;
4352 int n;
Bram Moolenaar63b92542007-03-27 14:57:09 +00004353 int escaped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004354
Bram Moolenaar748bf032005-02-02 23:04:36 +00004355#ifdef FEAT_QUICKFIX
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004356 // Skip a regexp pattern for ":vimgrep[add] pat file..."
Bram Moolenaar748bf032005-02-02 23:04:36 +00004357 p = skip_grep_pat(eap);
4358#else
4359 p = eap->arg;
4360#endif
4361
Bram Moolenaar071d4272004-06-13 20:20:40 +00004362 /*
4363 * Decide to expand wildcards *before* replacing '%', '#', etc. If
4364 * the file name contains a wildcard it should not cause expanding.
4365 * (it will be expanded anyway if there is a wildcard before replacing).
4366 */
Bram Moolenaar748bf032005-02-02 23:04:36 +00004367 has_wildcards = mch_has_wildcard(p);
4368 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004369 {
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004370#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004371 // Skip over `=expr`, wildcards in it are not expanded.
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004372 if (p[0] == '`' && p[1] == '=')
4373 {
4374 p += 2;
4375 (void)skip_expr(&p);
4376 if (*p == '`')
4377 ++p;
4378 continue;
4379 }
4380#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004381 /*
4382 * Quick check if this cannot be the start of a special string.
4383 * Also removes backslash before '%', '#' and '<'.
4384 */
4385 if (vim_strchr((char_u *)"%#<", *p) == NULL)
4386 {
4387 ++p;
4388 continue;
4389 }
4390
4391 /*
4392 * Try to find a match at this position.
4393 */
Bram Moolenaar63b92542007-03-27 14:57:09 +00004394 repl = eval_vars(p, eap->arg, &srclen, &(eap->do_ecmd_lnum),
4395 errormsgp, &escaped);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004396 if (*errormsgp != NULL) // error detected
Bram Moolenaar071d4272004-06-13 20:20:40 +00004397 return FAIL;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004398 if (repl == NULL) // no match found
Bram Moolenaar071d4272004-06-13 20:20:40 +00004399 {
4400 p += srclen;
4401 continue;
4402 }
4403
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004404 // Wildcards won't be expanded below, the replacement is taken
4405 // literally. But do expand "~/file", "~user/file" and "$HOME/file".
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00004406 if (vim_strchr(repl, '$') != NULL || vim_strchr(repl, '~') != NULL)
4407 {
4408 char_u *l = repl;
4409
4410 repl = expand_env_save(repl);
4411 vim_free(l);
4412 }
4413
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004414 // Need to escape white space et al. with a backslash.
4415 // Don't do this for:
4416 // - replacement that already has been escaped: "##"
4417 // - shell commands (may have to use quotes instead).
4418 // - non-unix systems when there is a single argument (spaces don't
4419 // separate arguments then).
Bram Moolenaar071d4272004-06-13 20:20:40 +00004420 if (!eap->usefilter
Bram Moolenaar63b92542007-03-27 14:57:09 +00004421 && !escaped
Bram Moolenaar071d4272004-06-13 20:20:40 +00004422 && eap->cmdidx != CMD_bang
Bram Moolenaar071d4272004-06-13 20:20:40 +00004423 && eap->cmdidx != CMD_grep
4424 && eap->cmdidx != CMD_grepadd
Bram Moolenaarbf15b8d2017-06-04 20:43:48 +02004425 && eap->cmdidx != CMD_hardcopy
Bram Moolenaar67883b42017-07-27 22:57:00 +02004426 && eap->cmdidx != CMD_lgrep
4427 && eap->cmdidx != CMD_lgrepadd
4428 && eap->cmdidx != CMD_lmake
4429 && eap->cmdidx != CMD_make
4430 && eap->cmdidx != CMD_terminal
Bram Moolenaar071d4272004-06-13 20:20:40 +00004431#ifndef UNIX
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004432 && !(eap->argt & EX_NOSPC)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004433#endif
4434 )
4435 {
4436 char_u *l;
4437#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004438 // Don't escape a backslash here, because rem_backslash() doesn't
4439 // remove it later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004440 static char_u *nobslash = (char_u *)" \t\"|";
4441# define ESCAPE_CHARS nobslash
4442#else
4443# define ESCAPE_CHARS escape_chars
4444#endif
4445
4446 for (l = repl; *l; ++l)
4447 if (vim_strchr(ESCAPE_CHARS, *l) != NULL)
4448 {
4449 l = vim_strsave_escaped(repl, ESCAPE_CHARS);
4450 if (l != NULL)
4451 {
4452 vim_free(repl);
4453 repl = l;
4454 }
4455 break;
4456 }
4457 }
4458
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004459 // For a shell command a '!' must be escaped.
Bram Moolenaar67883b42017-07-27 22:57:00 +02004460 if ((eap->usefilter || eap->cmdidx == CMD_bang
4461 || eap->cmdidx == CMD_terminal)
Bram Moolenaar31b7d382014-04-01 18:54:48 +02004462 && vim_strpbrk(repl, (char_u *)"!") != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004463 {
4464 char_u *l;
4465
Bram Moolenaar31b7d382014-04-01 18:54:48 +02004466 l = vim_strsave_escaped(repl, (char_u *)"!");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004467 if (l != NULL)
4468 {
4469 vim_free(repl);
4470 repl = l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004471 }
4472 }
4473
4474 p = repl_cmdline(eap, p, srclen, repl, cmdlinep);
4475 vim_free(repl);
4476 if (p == NULL)
4477 return FAIL;
4478 }
4479
4480 /*
4481 * One file argument: Expand wildcards.
4482 * Don't do this with ":r !command" or ":w !command".
4483 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004484 if ((eap->argt & EX_NOSPC) && !eap->usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004485 {
4486 /*
4487 * May do this twice:
4488 * 1. Replace environment variables.
4489 * 2. Replace any other wildcards, remove backslashes.
4490 */
4491 for (n = 1; n <= 2; ++n)
4492 {
4493 if (n == 2)
4494 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004495 /*
4496 * Halve the number of backslashes (this is Vi compatible).
4497 * For Unix and OS/2, when wildcards are expanded, this is
4498 * done by ExpandOne() below.
4499 */
Bram Moolenaare7fedb62015-12-31 19:07:19 +01004500#if defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004501 if (!has_wildcards)
4502#endif
4503 backslash_halve(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004504 }
4505
4506 if (has_wildcards)
4507 {
4508 if (n == 1)
4509 {
4510 /*
4511 * First loop: May expand environment variables. This
4512 * can be done much faster with expand_env() than with
4513 * something else (e.g., calling a shell).
4514 * After expanding environment variables, check again
4515 * if there are still wildcards present.
4516 */
4517 if (vim_strchr(eap->arg, '$') != NULL
4518 || vim_strchr(eap->arg, '~') != NULL)
4519 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +00004520 expand_env_esc(eap->arg, NameBuff, MAXPATHL,
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00004521 TRUE, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004522 has_wildcards = mch_has_wildcard(NameBuff);
4523 p = NameBuff;
4524 }
4525 else
4526 p = NULL;
4527 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004528 else // n == 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00004529 {
4530 expand_T xpc;
Bram Moolenaarb40c2572019-10-19 21:01:05 +02004531 int options = WILD_LIST_NOTFOUND
4532 | WILD_NOERROR | WILD_ADD_SLASH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004533
4534 ExpandInit(&xpc);
4535 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar94950a92010-12-02 16:01:29 +01004536 if (p_wic)
4537 options += WILD_ICASE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004538 p = ExpandOne(&xpc, eap->arg, NULL,
Bram Moolenaar94950a92010-12-02 16:01:29 +01004539 options, WILD_EXPAND_FREE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004540 if (p == NULL)
4541 return FAIL;
4542 }
4543 if (p != NULL)
4544 {
4545 (void)repl_cmdline(eap, eap->arg, (int)STRLEN(eap->arg),
4546 p, cmdlinep);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004547 if (n == 2) // p came from ExpandOne()
Bram Moolenaar071d4272004-06-13 20:20:40 +00004548 vim_free(p);
4549 }
4550 }
4551 }
4552 }
4553 return OK;
4554}
4555
4556/*
4557 * Replace part of the command line, keeping eap->cmd, eap->arg and
4558 * eap->nextcmd correct.
4559 * "src" points to the part that is to be replaced, of length "srclen".
4560 * "repl" is the replacement string.
4561 * Returns a pointer to the character after the replaced string.
4562 * Returns NULL for failure.
4563 */
4564 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004565repl_cmdline(
4566 exarg_T *eap,
4567 char_u *src,
4568 int srclen,
4569 char_u *repl,
4570 char_u **cmdlinep)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004571{
4572 int len;
4573 int i;
4574 char_u *new_cmdline;
4575
4576 /*
4577 * The new command line is build in new_cmdline[].
4578 * First allocate it.
4579 * Careful: a "+cmd" argument may have been NUL terminated.
4580 */
4581 len = (int)STRLEN(repl);
4582 i = (int)(src - *cmdlinep) + (int)STRLEN(src + srclen) + len + 3;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004583 if (eap->nextcmd != NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004584 i += (int)STRLEN(eap->nextcmd);// add space for next command
Bram Moolenaar964b3742019-05-24 18:54:09 +02004585 if ((new_cmdline = alloc(i)) == NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004586 return NULL; // out of memory!
Bram Moolenaar071d4272004-06-13 20:20:40 +00004587
4588 /*
4589 * Copy the stuff before the expanded part.
4590 * Copy the expanded stuff.
4591 * Copy what came after the expanded part.
4592 * Copy the next commands, if there are any.
4593 */
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004594 i = (int)(src - *cmdlinep); // length of part before match
Bram Moolenaar071d4272004-06-13 20:20:40 +00004595 mch_memmove(new_cmdline, *cmdlinep, (size_t)i);
Bram Moolenaara3ffd9c2005-07-21 21:03:15 +00004596
Bram Moolenaar071d4272004-06-13 20:20:40 +00004597 mch_memmove(new_cmdline + i, repl, (size_t)len);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004598 i += len; // remember the end of the string
Bram Moolenaar071d4272004-06-13 20:20:40 +00004599 STRCPY(new_cmdline + i, src + srclen);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004600 src = new_cmdline + i; // remember where to continue
Bram Moolenaar071d4272004-06-13 20:20:40 +00004601
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004602 if (eap->nextcmd != NULL) // append next command
Bram Moolenaar071d4272004-06-13 20:20:40 +00004603 {
4604 i = (int)STRLEN(new_cmdline) + 1;
4605 STRCPY(new_cmdline + i, eap->nextcmd);
4606 eap->nextcmd = new_cmdline + i;
4607 }
4608 eap->cmd = new_cmdline + (eap->cmd - *cmdlinep);
4609 eap->arg = new_cmdline + (eap->arg - *cmdlinep);
4610 if (eap->do_ecmd_cmd != NULL && eap->do_ecmd_cmd != dollar_command)
4611 eap->do_ecmd_cmd = new_cmdline + (eap->do_ecmd_cmd - *cmdlinep);
4612 vim_free(*cmdlinep);
4613 *cmdlinep = new_cmdline;
4614
4615 return src;
4616}
4617
4618/*
4619 * Check for '|' to separate commands and '"' to start comments.
4620 */
4621 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004622separate_nextcmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004623{
4624 char_u *p;
4625
Bram Moolenaar86b68352004-12-27 21:59:20 +00004626#ifdef FEAT_QUICKFIX
Bram Moolenaar748bf032005-02-02 23:04:36 +00004627 p = skip_grep_pat(eap);
4628#else
4629 p = eap->arg;
Bram Moolenaar86b68352004-12-27 21:59:20 +00004630#endif
4631
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004632 for ( ; *p; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004633 {
4634 if (*p == Ctrl_V)
4635 {
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004636 if (eap->argt & (EX_CTRLV | EX_XFILE))
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004637 ++p; // skip CTRL-V and next char
Bram Moolenaar071d4272004-06-13 20:20:40 +00004638 else
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004639 // remove CTRL-V and skip next char
Bram Moolenaara7241f52008-06-24 20:39:31 +00004640 STRMOVE(p, p + 1);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004641 if (*p == NUL) // stop at NUL after CTRL-V
Bram Moolenaar071d4272004-06-13 20:20:40 +00004642 break;
4643 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004644
4645#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004646 // Skip over `=expr` when wildcards are expanded.
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004647 else if (p[0] == '`' && p[1] == '=' && (eap->argt & EX_XFILE))
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004648 {
4649 p += 2;
4650 (void)skip_expr(&p);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004651 }
4652#endif
4653
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004654 // Check for '"': start of comment or '|': next command
4655 // :@" and :*" do not start a comment!
4656 // :redir @" doesn't either.
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02004657 else if ((*p == '"'
Bram Moolenaara26b9702020-04-18 19:53:28 +02004658#ifdef FEAT_EVAL
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02004659 && !in_vim9script()
4660#endif
4661 && !(eap->argt & EX_NOTRLCOM)
4662 && ((eap->cmdidx != CMD_at && eap->cmdidx != CMD_star)
4663 || p != eap->arg)
4664 && (eap->cmdidx != CMD_redir
4665 || p != eap->arg + 1 || p[-1] != '@'))
4666#ifdef FEAT_EVAL
4667 || (*p == '#'
4668 && in_vim9script()
Bram Moolenaarb8a92962020-08-20 18:02:47 +02004669 && !(eap->argt & EX_NOTRLCOM)
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02004670 && p[1] != '{'
4671 && p > eap->cmd && VIM_ISWHITE(p[-1]))
Bram Moolenaara26b9702020-04-18 19:53:28 +02004672#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004673 || *p == '|' || *p == '\n')
4674 {
4675 /*
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004676 * We remove the '\' before the '|', unless EX_CTRLV is used
Bram Moolenaar071d4272004-06-13 20:20:40 +00004677 * AND 'b' is present in 'cpoptions'.
4678 */
4679 if ((vim_strchr(p_cpo, CPO_BAR) == NULL
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004680 || !(eap->argt & EX_CTRLV)) && *(p - 1) == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004681 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004682 STRMOVE(p - 1, p); // remove the '\'
Bram Moolenaar071d4272004-06-13 20:20:40 +00004683 --p;
4684 }
4685 else
4686 {
4687 eap->nextcmd = check_nextcmd(p);
4688 *p = NUL;
4689 break;
4690 }
4691 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004692 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00004693
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004694 if (!(eap->argt & EX_NOTRLCOM)) // remove trailing spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00004695 del_trailing_spaces(eap->arg);
4696}
4697
4698/*
4699 * get + command from ex argument
4700 */
4701 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004702getargcmd(char_u **argp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004703{
4704 char_u *arg = *argp;
4705 char_u *command = NULL;
4706
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004707 if (*arg == '+') // +[command]
Bram Moolenaar071d4272004-06-13 20:20:40 +00004708 {
4709 ++arg;
Bram Moolenaar3e451592014-04-02 14:22:05 +02004710 if (vim_isspace(*arg) || *arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004711 command = dollar_command;
4712 else
4713 {
4714 command = arg;
4715 arg = skip_cmd_arg(command, TRUE);
4716 if (*arg != NUL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004717 *arg++ = NUL; // terminate command with NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004718 }
4719
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004720 arg = skipwhite(arg); // skip over spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00004721 *argp = arg;
4722 }
4723 return command;
4724}
4725
4726/*
4727 * Find end of "+command" argument. Skip over "\ " and "\\".
4728 */
Bram Moolenaard0190392019-08-23 21:17:35 +02004729 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004730skip_cmd_arg(
4731 char_u *p,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004732 int rembs) // TRUE to halve the number of backslashes
Bram Moolenaar071d4272004-06-13 20:20:40 +00004733{
4734 while (*p && !vim_isspace(*p))
4735 {
4736 if (*p == '\\' && p[1] != NUL)
4737 {
4738 if (rembs)
Bram Moolenaara7241f52008-06-24 20:39:31 +00004739 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004740 else
4741 ++p;
4742 }
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004743 MB_PTR_ADV(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004744 }
4745 return p;
4746}
4747
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004748 int
4749get_bad_opt(char_u *p, exarg_T *eap)
4750{
4751 if (STRICMP(p, "keep") == 0)
4752 eap->bad_char = BAD_KEEP;
4753 else if (STRICMP(p, "drop") == 0)
4754 eap->bad_char = BAD_DROP;
4755 else if (MB_BYTE2LEN(*p) == 1 && p[1] == NUL)
4756 eap->bad_char = *p;
Bram Moolenaar75808492018-06-12 12:39:41 +02004757 else
4758 return FAIL;
4759 return OK;
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004760}
4761
Bram Moolenaar071d4272004-06-13 20:20:40 +00004762/*
4763 * Get "++opt=arg" argument.
4764 * Return FAIL or OK.
4765 */
4766 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004767getargopt(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004768{
4769 char_u *arg = eap->arg + 2;
4770 int *pp = NULL;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02004771 int bad_char_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004772 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004773
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004774 // ":edit ++[no]bin[ary] file"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004775 if (STRNCMP(arg, "bin", 3) == 0 || STRNCMP(arg, "nobin", 5) == 0)
4776 {
4777 if (*arg == 'n')
4778 {
4779 arg += 2;
4780 eap->force_bin = FORCE_NOBIN;
4781 }
4782 else
4783 eap->force_bin = FORCE_BIN;
4784 if (!checkforcmd(&arg, "binary", 3))
4785 return FAIL;
4786 eap->arg = skipwhite(arg);
4787 return OK;
4788 }
4789
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004790 // ":read ++edit file"
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004791 if (STRNCMP(arg, "edit", 4) == 0)
4792 {
4793 eap->read_edit = TRUE;
4794 eap->arg = skipwhite(arg + 4);
4795 return OK;
4796 }
4797
Bram Moolenaar071d4272004-06-13 20:20:40 +00004798 if (STRNCMP(arg, "ff", 2) == 0)
4799 {
4800 arg += 2;
4801 pp = &eap->force_ff;
4802 }
4803 else if (STRNCMP(arg, "fileformat", 10) == 0)
4804 {
4805 arg += 10;
4806 pp = &eap->force_ff;
4807 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004808 else if (STRNCMP(arg, "enc", 3) == 0)
4809 {
Bram Moolenaarb38e9ab2011-12-14 14:49:45 +01004810 if (STRNCMP(arg, "encoding", 8) == 0)
4811 arg += 8;
4812 else
4813 arg += 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004814 pp = &eap->force_enc;
4815 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004816 else if (STRNCMP(arg, "bad", 3) == 0)
4817 {
4818 arg += 3;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02004819 pp = &bad_char_idx;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004820 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004821
4822 if (pp == NULL || *arg != '=')
4823 return FAIL;
4824
4825 ++arg;
4826 *pp = (int)(arg - eap->cmd);
4827 arg = skip_cmd_arg(arg, FALSE);
4828 eap->arg = skipwhite(arg);
4829 *arg = NUL;
4830
Bram Moolenaar071d4272004-06-13 20:20:40 +00004831 if (pp == &eap->force_ff)
4832 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004833 if (check_ff_value(eap->cmd + eap->force_ff) == FAIL)
4834 return FAIL;
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004835 eap->force_ff = eap->cmd[eap->force_ff];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004836 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004837 else if (pp == &eap->force_enc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004838 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004839 // Make 'fileencoding' lower case.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004840 for (p = eap->cmd + eap->force_enc; *p != NUL; ++p)
4841 *p = TOLOWER_ASC(*p);
4842 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004843 else
4844 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004845 // Check ++bad= argument. Must be a single-byte character, "keep" or
4846 // "drop".
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004847 if (get_bad_opt(eap->cmd + bad_char_idx, eap) == FAIL)
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004848 return FAIL;
4849 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004850
4851 return OK;
4852}
4853
Bram Moolenaar071d4272004-06-13 20:20:40 +00004854 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004855ex_autocmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004856{
4857 /*
Bram Moolenaar26d4b892018-07-04 22:26:28 +02004858 * Disallow autocommands from .exrc and .vimrc in current
Bram Moolenaar071d4272004-06-13 20:20:40 +00004859 * directory for security reasons.
4860 */
4861 if (secure)
4862 {
4863 secure = 2;
Bram Moolenaar8930caa2020-07-23 16:37:03 +02004864 eap->errmsg = _(e_curdir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004865 }
4866 else if (eap->cmdidx == CMD_autocmd)
4867 do_autocmd(eap->arg, eap->forceit);
4868 else
4869 do_augroup(eap->arg, eap->forceit);
4870}
4871
4872/*
4873 * ":doautocmd": Apply the automatic commands to the current buffer.
4874 */
4875 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004876ex_doautocmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004877{
Bram Moolenaar60542ac2012-02-12 20:14:01 +01004878 char_u *arg = eap->arg;
4879 int call_do_modelines = check_nomodeline(&arg);
Bram Moolenaar1610d052016-06-09 22:53:01 +02004880 int did_aucmd;
Bram Moolenaar60542ac2012-02-12 20:14:01 +01004881
Bram Moolenaar1610d052016-06-09 22:53:01 +02004882 (void)do_doautocmd(arg, TRUE, &did_aucmd);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004883 // Only when there is no <nomodeline>.
Bram Moolenaar1610d052016-06-09 22:53:01 +02004884 if (call_do_modelines && did_aucmd)
Bram Moolenaar60542ac2012-02-12 20:14:01 +01004885 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004886}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004887
Bram Moolenaar071d4272004-06-13 20:20:40 +00004888/*
4889 * :[N]bunload[!] [N] [bufname] unload buffer
4890 * :[N]bdelete[!] [N] [bufname] delete buffer from buffer list
4891 * :[N]bwipeout[!] [N] [bufname] delete buffer really
4892 */
4893 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004894ex_bunload(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004895{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01004896 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02004897 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004898 eap->errmsg = do_bufdel(
4899 eap->cmdidx == CMD_bdelete ? DOBUF_DEL
4900 : eap->cmdidx == CMD_bwipeout ? DOBUF_WIPE
4901 : DOBUF_UNLOAD, eap->arg,
4902 eap->addr_count, (int)eap->line1, (int)eap->line2, eap->forceit);
4903}
4904
4905/*
4906 * :[N]buffer [N] to buffer N
4907 * :[N]sbuffer [N] to buffer N
4908 */
4909 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004910ex_buffer(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004911{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01004912 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02004913 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004914 if (*eap->arg)
Bram Moolenaar8930caa2020-07-23 16:37:03 +02004915 eap->errmsg = ex_errmsg(e_trailing_arg, eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004916 else
4917 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004918 if (eap->addr_count == 0) // default is current buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00004919 goto_buffer(eap, DOBUF_CURRENT, FORWARD, 0);
4920 else
4921 goto_buffer(eap, DOBUF_FIRST, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02004922 if (eap->do_ecmd_cmd != NULL)
4923 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004924 }
4925}
4926
4927/*
4928 * :[N]bmodified [N] to next mod. buffer
4929 * :[N]sbmodified [N] to next mod. buffer
4930 */
4931 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004932ex_bmodified(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004933{
4934 goto_buffer(eap, DOBUF_MOD, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02004935 if (eap->do_ecmd_cmd != NULL)
4936 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004937}
4938
4939/*
4940 * :[N]bnext [N] to next buffer
4941 * :[N]sbnext [N] split and to next buffer
4942 */
4943 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004944ex_bnext(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, FORWARD, (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 * :[N]bNext [N] to previous buffer
4956 * :[N]bprevious [N] to previous buffer
4957 * :[N]sbNext [N] split and to previous buffer
4958 * :[N]sbprevious [N] split and to previous buffer
4959 */
4960 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004961ex_bprevious(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004962{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01004963 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar3029bcc2020-01-18 15:06:19 +01004964 return;
4965
Bram Moolenaar071d4272004-06-13 20:20:40 +00004966 goto_buffer(eap, DOBUF_CURRENT, BACKWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02004967 if (eap->do_ecmd_cmd != NULL)
4968 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004969}
4970
4971/*
4972 * :brewind to first buffer
4973 * :bfirst to first buffer
4974 * :sbrewind split and to first buffer
4975 * :sbfirst split and to first buffer
4976 */
4977 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004978ex_brewind(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004979{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01004980 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar3029bcc2020-01-18 15:06:19 +01004981 return;
4982
Bram Moolenaar071d4272004-06-13 20:20:40 +00004983 goto_buffer(eap, DOBUF_FIRST, FORWARD, 0);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02004984 if (eap->do_ecmd_cmd != NULL)
4985 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004986}
4987
4988/*
4989 * :blast to last buffer
4990 * :sblast split and to last buffer
4991 */
4992 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004993ex_blast(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004994{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01004995 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar3029bcc2020-01-18 15:06:19 +01004996 return;
4997
Bram Moolenaar071d4272004-06-13 20:20:40 +00004998 goto_buffer(eap, DOBUF_LAST, BACKWARD, 0);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02004999 if (eap->do_ecmd_cmd != NULL)
5000 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005001}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005002
Bram Moolenaar7a092242020-04-16 22:10:49 +02005003/*
5004 * Check if "c" ends an Ex command.
Bram Moolenaara494f562020-04-18 17:45:38 +02005005 * In Vim9 script does not check for white space before # or #{.
Bram Moolenaar7a092242020-04-16 22:10:49 +02005006 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005007 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005008ends_excmd(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005009{
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005010 int comment_char = '"';
5011
Bram Moolenaarfbda69b2020-04-13 15:06:53 +02005012#ifdef FEAT_EVAL
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005013 if (in_vim9script())
5014 comment_char = '#';
Bram Moolenaar7a092242020-04-16 22:10:49 +02005015#endif
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005016 return (c == NUL || c == '|' || c == comment_char || c == '\n');
Bram Moolenaar7a092242020-04-16 22:10:49 +02005017}
5018
5019/*
5020 * Like ends_excmd() but checks that a # in Vim9 script either has "cmd" equal
5021 * to "cmd_start" or has a white space character before it.
5022 */
5023 int
Bram Moolenaar21cfe502020-04-16 23:01:50 +02005024ends_excmd2(char_u *cmd_start UNUSED, char_u *cmd)
Bram Moolenaar7a092242020-04-16 22:10:49 +02005025{
5026 int c = *cmd;
5027
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005028 if (c == NUL || c == '|' || c == '\n')
5029 return TRUE;
Bram Moolenaar7a092242020-04-16 22:10:49 +02005030#ifdef FEAT_EVAL
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005031 if (in_vim9script())
5032 return c == '#' && cmd[1] != '{'
5033 && (cmd == cmd_start || VIM_ISWHITE(cmd[-1]));
Bram Moolenaarfbda69b2020-04-13 15:06:53 +02005034#endif
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005035 return c == '"';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005036}
5037
5038#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA) || defined(FEAT_EVAL) \
5039 || defined(PROTO)
5040/*
5041 * Return the next command, after the first '|' or '\n'.
5042 * Return NULL if not found.
5043 */
5044 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005045find_nextcmd(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005046{
5047 while (*p != '|' && *p != '\n')
5048 {
5049 if (*p == NUL)
5050 return NULL;
5051 ++p;
5052 }
5053 return (p + 1);
5054}
5055#endif
5056
5057/*
Bram Moolenaar2256c992016-11-15 21:17:07 +01005058 * Check if *p is a separator between Ex commands, skipping over white space.
5059 * Return NULL if it isn't, the following character if it is.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005060 */
5061 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005062check_nextcmd(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005063{
Bram Moolenaar2256c992016-11-15 21:17:07 +01005064 char_u *s = skipwhite(p);
5065
5066 if (*s == '|' || *s == '\n')
5067 return (s + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005068 else
5069 return NULL;
5070}
5071
5072/*
5073 * - if there are more files to edit
5074 * - and this is the last window
5075 * - and forceit not used
5076 * - and not repeated twice on a row
5077 * return FAIL and give error message if 'message' TRUE
5078 * return OK otherwise
5079 */
5080 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005081check_more(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005082 int message, // when FALSE check only, no messages
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005083 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005084{
5085 int n = ARGCOUNT - curwin->w_arg_idx - 1;
5086
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005087 if (!forceit && only_one_window()
Bram Moolenaar7b221172020-08-17 19:34:10 +02005088 && ARGCOUNT > 1 && !arg_had_last && n > 0 && quitmore == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005089 {
5090 if (message)
5091 {
5092#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
5093 if ((p_confirm || cmdmod.confirm) && curbuf->b_fname != NULL)
5094 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02005095 char_u buff[DIALOG_MSG_SIZE];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005096
Bram Moolenaarda6e8912018-08-21 15:12:14 +02005097 vim_snprintf((char *)buff, DIALOG_MSG_SIZE,
5098 NGETTEXT("%d more file to edit. Quit anyway?",
5099 "%d more files to edit. Quit anyway?", n), n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005100 if (vim_dialog_yesno(VIM_QUESTION, NULL, buff, 1) == VIM_YES)
5101 return OK;
5102 return FAIL;
5103 }
5104#endif
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01005105 semsg(NGETTEXT("E173: %d more file to edit",
5106 "E173: %d more files to edit", n), n);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005107 quitmore = 2; // next try to quit is allowed
Bram Moolenaar071d4272004-06-13 20:20:40 +00005108 }
5109 return FAIL;
5110 }
5111 return OK;
5112}
5113
Bram Moolenaar071d4272004-06-13 20:20:40 +00005114/*
5115 * Function given to ExpandGeneric() to obtain the list of command names.
5116 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005117 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005118get_command_name(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005119{
5120 if (idx >= (int)CMD_SIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005121 return get_user_command_name(idx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005122 return cmdnames[idx].cmd_name;
5123}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005124
Bram Moolenaar071d4272004-06-13 20:20:40 +00005125 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005126ex_colorscheme(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005127{
Bram Moolenaare6850792010-05-14 15:28:44 +02005128 if (*eap->arg == NUL)
5129 {
5130#ifdef FEAT_EVAL
5131 char_u *expr = vim_strsave((char_u *)"g:colors_name");
5132 char_u *p = NULL;
5133
5134 if (expr != NULL)
5135 {
5136 ++emsg_off;
Bram Moolenaarb171fb12020-06-24 20:34:03 +02005137 p = eval_to_string(expr, FALSE);
Bram Moolenaare6850792010-05-14 15:28:44 +02005138 --emsg_off;
5139 vim_free(expr);
5140 }
5141 if (p != NULL)
5142 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01005143 msg((char *)p);
Bram Moolenaare6850792010-05-14 15:28:44 +02005144 vim_free(p);
5145 }
5146 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01005147 msg("default");
Bram Moolenaare6850792010-05-14 15:28:44 +02005148#else
Bram Moolenaar32526b32019-01-19 17:43:09 +01005149 msg(_("unknown"));
Bram Moolenaare6850792010-05-14 15:28:44 +02005150#endif
5151 }
5152 else if (load_colors(eap->arg) == FAIL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005153 semsg(_("E185: Cannot find color scheme '%s'"), eap->arg);
Bram Moolenaarf58d81a2019-01-28 20:19:05 +01005154
5155#ifdef FEAT_VTP
5156 else if (has_vtp_working())
5157 {
5158 // background color change requires clear + redraw
5159 update_screen(CLEAR);
5160 redrawcmd();
5161 }
5162#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005163}
5164
5165 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005166ex_highlight(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005167{
5168 if (*eap->arg == NUL && eap->cmd[2] == '!')
Bram Moolenaar32526b32019-01-19 17:43:09 +01005169 msg(_("Greetings, Vim user!"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005170 do_highlight(eap->arg, eap->forceit, FALSE);
5171}
5172
5173
5174/*
5175 * Call this function if we thought we were going to exit, but we won't
5176 * (because of an error). May need to restore the terminal mode.
5177 */
5178 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005179not_exiting(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005180{
5181 exiting = FALSE;
5182 settmode(TMODE_RAW);
5183}
5184
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005185 static int
5186before_quit_autocmds(win_T *wp, int quit_all, int forceit)
5187{
5188 apply_autocmds(EVENT_QUITPRE, NULL, NULL, FALSE, wp->w_buffer);
5189
Bram Moolenaar34ba06b2019-10-20 22:27:10 +02005190 // Bail out when autocommands closed the window.
5191 // Refuse to quit when the buffer in the last window is being closed (can
5192 // only happen in autocommands).
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005193 if (!win_valid(wp)
5194 || curbuf_locked()
5195 || (wp->w_buffer->b_nwindows == 1 && wp->w_buffer->b_locked > 0))
5196 return TRUE;
5197
5198 if (quit_all || (check_more(FALSE, forceit) == OK && only_one_window()))
5199 {
5200 apply_autocmds(EVENT_EXITPRE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar34ba06b2019-10-20 22:27:10 +02005201 // Refuse to quit when locked or when the window was closed or the
5202 // buffer in the last window is being closed (can only happen in
5203 // autocommands).
5204 if (!win_valid(wp) || curbuf_locked()
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005205 || (curbuf->b_nwindows == 1 && curbuf->b_locked > 0))
5206 return TRUE;
5207 }
5208
5209 return FALSE;
5210}
5211
Bram Moolenaar071d4272004-06-13 20:20:40 +00005212/*
Bram Moolenaarf240e182014-11-27 18:33:02 +01005213 * ":quit": quit current window, quit Vim if the last window is closed.
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005214 * ":{nr}quit": quit window {nr}
Bram Moolenaar4d14bac2019-10-20 21:15:15 +02005215 * Also used when closing a terminal window that's the last one.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005216 */
Bram Moolenaar4d14bac2019-10-20 21:15:15 +02005217 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005218ex_quit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005219{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005220 win_T *wp;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005221
Bram Moolenaar071d4272004-06-13 20:20:40 +00005222#ifdef FEAT_CMDWIN
5223 if (cmdwin_type != 0)
5224 {
5225 cmdwin_result = Ctrl_C;
5226 return;
5227 }
5228#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005229 // Don't quit while editing the command line.
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005230 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005231 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005232 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005233 return;
5234 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005235 if (eap->addr_count > 0)
5236 {
Bram Moolenaarf240e182014-11-27 18:33:02 +01005237 int wnr = eap->line2;
5238
5239 for (wp = firstwin; wp->w_next != NULL; wp = wp->w_next)
5240 if (--wnr <= 0)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005241 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005242 }
5243 else
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005244 wp = curwin;
Bram Moolenaarf240e182014-11-27 18:33:02 +01005245
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005246 // Refuse to quit when locked.
Bram Moolenaar87ffb5c2017-10-19 12:37:42 +02005247 if (curbuf_locked())
5248 return;
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005249
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005250 // Trigger QuitPre and maybe ExitPre
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005251 if (before_quit_autocmds(wp, FALSE, eap->forceit))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005252 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005253
5254#ifdef FEAT_NETBEANS_INTG
5255 netbeansForcedQuit = eap->forceit;
5256#endif
5257
5258 /*
5259 * If there are more files or windows we won't exit.
5260 */
5261 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
5262 exiting = TRUE;
Bram Moolenaarff930ca2017-10-19 17:12:10 +02005263 if ((!buf_hide(wp->w_buffer)
5264 && check_changed(wp->w_buffer, (p_awa ? CCGD_AW : 0)
Bram Moolenaar45d3b142013-11-09 03:31:51 +01005265 | (eap->forceit ? CCGD_FORCEIT : 0)
5266 | CCGD_EXCMD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005267 || check_more(TRUE, eap->forceit) == FAIL
Bram Moolenaar027387f2016-01-02 22:25:52 +01005268 || (only_one_window() && check_changed_any(eap->forceit, TRUE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005269 {
5270 not_exiting();
5271 }
5272 else
5273 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005274 // quit last window
5275 // Note: only_one_window() returns true, even so a help window is
5276 // still open. In that case only quit, if no address has been
5277 // specified. Example:
5278 // :h|wincmd w|1q - don't quit
5279 // :h|wincmd w|q - quit
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01005280 if (only_one_window() && (ONE_WINDOW || eap->addr_count == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005281 getout(0);
Bram Moolenaar2c33d7b2017-10-14 16:06:20 +02005282 not_exiting();
Bram Moolenaar4033c552017-09-16 20:54:51 +02005283#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00005284 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005285#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005286 // close window; may free buffer
Bram Moolenaareb44a682017-08-03 22:44:55 +02005287 win_close(wp, !buf_hide(wp->w_buffer) || eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005288 }
5289}
5290
5291/*
5292 * ":cquit".
5293 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005294 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005295ex_cquit(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005296{
Bram Moolenaar1860bde2020-01-06 21:47:21 +01005297 // this does not always pass on the exit code to the Manx compiler. why?
5298 getout(eap->addr_count > 0 ? (int)eap->line2 : EXIT_FAILURE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005299}
5300
5301/*
5302 * ":qall": try to quit all windows
5303 */
5304 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005305ex_quit_all(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005306{
5307# ifdef FEAT_CMDWIN
5308 if (cmdwin_type != 0)
5309 {
5310 if (eap->forceit)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005311 cmdwin_result = K_XF1; // ex_window() takes care of this
Bram Moolenaar071d4272004-06-13 20:20:40 +00005312 else
5313 cmdwin_result = K_XF2;
5314 return;
5315 }
5316# endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005317
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005318 // Don't quit while editing the command line.
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005319 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005320 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005321 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005322 return;
5323 }
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005324
5325 if (before_quit_autocmds(curwin, TRUE, eap->forceit))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005326 return;
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005327
Bram Moolenaar071d4272004-06-13 20:20:40 +00005328 exiting = TRUE;
Bram Moolenaar027387f2016-01-02 22:25:52 +01005329 if (eap->forceit || !check_changed_any(FALSE, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005330 getout(0);
5331 not_exiting();
5332}
5333
Bram Moolenaar071d4272004-06-13 20:20:40 +00005334/*
5335 * ":close": close current window, unless it is the last one
5336 */
5337 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005338ex_close(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005339{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005340 win_T *win;
5341 int winnr = 0;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005342#ifdef FEAT_CMDWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00005343 if (cmdwin_type != 0)
Bram Moolenaar9bd1a7e2011-05-19 14:50:54 +02005344 cmdwin_result = Ctrl_C;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005345 else
Bram Moolenaar4033c552017-09-16 20:54:51 +02005346#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005347 if (!text_locked() && !curbuf_locked())
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005348 {
5349 if (eap->addr_count == 0)
5350 ex_win_close(eap->forceit, curwin, NULL);
Bram Moolenaar6d41c782018-06-06 09:11:12 +02005351 else
5352 {
Bram Moolenaar29323592016-07-24 22:04:11 +02005353 FOR_ALL_WINDOWS(win)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005354 {
5355 winnr++;
5356 if (winnr == eap->line2)
5357 break;
5358 }
5359 if (win == NULL)
5360 win = lastwin;
5361 ex_win_close(eap->forceit, win, NULL);
5362 }
5363 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005364}
5365
Bram Moolenaar4033c552017-09-16 20:54:51 +02005366#ifdef FEAT_QUICKFIX
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005367/*
5368 * ":pclose": Close any preview window.
5369 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005370 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005371ex_pclose(exarg_T *eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005372{
5373 win_T *win;
5374
Bram Moolenaar79648732019-07-18 21:43:07 +02005375 // First close any normal window.
Bram Moolenaar29323592016-07-24 22:04:11 +02005376 FOR_ALL_WINDOWS(win)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005377 if (win->w_p_pvw)
5378 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00005379 ex_win_close(eap->forceit, win, NULL);
Bram Moolenaar79648732019-07-18 21:43:07 +02005380 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005381 }
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01005382# ifdef FEAT_PROP_POPUP
Bram Moolenaar79648732019-07-18 21:43:07 +02005383 // Also when 'previewpopup' is empty, it might have been cleared.
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02005384 popup_close_preview();
Bram Moolenaar79648732019-07-18 21:43:07 +02005385# endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005386}
Bram Moolenaar4033c552017-09-16 20:54:51 +02005387#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005388
Bram Moolenaarf740b292006-02-16 22:11:02 +00005389/*
5390 * Close window "win" and take care of handling closing the last window for a
5391 * modified buffer.
5392 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005393 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005394ex_win_close(
5395 int forceit,
5396 win_T *win,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005397 tabpage_T *tp) // NULL or the tab page "win" is in
Bram Moolenaar071d4272004-06-13 20:20:40 +00005398{
5399 int need_hide;
5400 buf_T *buf = win->w_buffer;
5401
Bram Moolenaarcf844172020-06-26 19:44:06 +02005402 // Never close the autocommand window.
5403 if (win == aucmd_win)
5404 {
5405 emsg(_(e_autocmd_close));
5406 return;
5407 }
5408
Bram Moolenaar071d4272004-06-13 20:20:40 +00005409 need_hide = (bufIsChanged(buf) && buf->b_nwindows <= 1);
Bram Moolenaareb44a682017-08-03 22:44:55 +02005410 if (need_hide && !buf_hide(buf) && !forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005411 {
Bram Moolenaar4033c552017-09-16 20:54:51 +02005412#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005413 if ((p_confirm || cmdmod.confirm) && p_write)
5414 {
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005415 bufref_T bufref;
5416
5417 set_bufref(&bufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005418 dialog_changed(buf, FALSE);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005419 if (bufref_valid(&bufref) && bufIsChanged(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005420 return;
5421 need_hide = FALSE;
5422 }
5423 else
Bram Moolenaar4033c552017-09-16 20:54:51 +02005424#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005425 {
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02005426 no_write_message();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005427 return;
5428 }
5429 }
5430
Bram Moolenaar4033c552017-09-16 20:54:51 +02005431#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00005432 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005433#endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00005434
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005435 // free buffer when not hiding it or when it's a scratch buffer
Bram Moolenaarf740b292006-02-16 22:11:02 +00005436 if (tp == NULL)
Bram Moolenaareb44a682017-08-03 22:44:55 +02005437 win_close(win, !need_hide && !buf_hide(buf));
Bram Moolenaarf740b292006-02-16 22:11:02 +00005438 else
Bram Moolenaareb44a682017-08-03 22:44:55 +02005439 win_close_othertab(win, !need_hide && !buf_hide(buf), tp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005440}
5441
Bram Moolenaar071d4272004-06-13 20:20:40 +00005442/*
Bram Moolenaardea25702017-01-29 15:18:10 +01005443 * Handle the argument for a tabpage related ex command.
5444 * Returns a tabpage number.
5445 * When an error is encountered then eap->errmsg is set.
5446 */
5447 static int
5448get_tabpage_arg(exarg_T *eap)
5449{
5450 int tab_number;
5451 int unaccept_arg0 = (eap->cmdidx == CMD_tabmove) ? 0 : 1;
5452
5453 if (eap->arg && *eap->arg != NUL)
5454 {
5455 char_u *p = eap->arg;
5456 char_u *p_save;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005457 int relative = 0; // argument +N/-N means: go to N places to the
5458 // right/left relative to the current position.
Bram Moolenaardea25702017-01-29 15:18:10 +01005459
5460 if (*p == '-')
5461 {
5462 relative = -1;
5463 p++;
5464 }
5465 else if (*p == '+')
5466 {
5467 relative = 1;
5468 p++;
5469 }
5470
5471 p_save = p;
5472 tab_number = getdigits(&p);
5473
5474 if (relative == 0)
5475 {
5476 if (STRCMP(p, "$") == 0)
5477 tab_number = LAST_TAB_NR;
Bram Moolenaar94f4ffa2020-08-10 19:21:15 +02005478 else if (STRCMP(p, "#") == 0)
5479 if (valid_tabpage(lastused_tabpage))
5480 tab_number = tabpage_index(lastused_tabpage);
5481 else
5482 {
5483 eap->errmsg = ex_errmsg(e_invargval, eap->arg);
5484 tab_number = 0;
5485 goto theend;
5486 }
Bram Moolenaardea25702017-01-29 15:18:10 +01005487 else if (p == p_save || *p_save == '-' || *p != NUL
5488 || tab_number > LAST_TAB_NR)
5489 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005490 // No numbers as argument.
Bram Moolenaar8930caa2020-07-23 16:37:03 +02005491 eap->errmsg = ex_errmsg(e_invarg2, eap->arg);
Bram Moolenaardea25702017-01-29 15:18:10 +01005492 goto theend;
5493 }
5494 }
5495 else
5496 {
5497 if (*p_save == NUL)
5498 tab_number = 1;
5499 else if (p == p_save || *p_save == '-' || *p != NUL
5500 || tab_number == 0)
5501 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005502 // No numbers as argument.
Bram Moolenaar8930caa2020-07-23 16:37:03 +02005503 eap->errmsg = ex_errmsg(e_invarg2, eap->arg);
Bram Moolenaardea25702017-01-29 15:18:10 +01005504 goto theend;
5505 }
5506 tab_number = tab_number * relative + tabpage_index(curtab);
5507 if (!unaccept_arg0 && relative == -1)
5508 --tab_number;
5509 }
5510 if (tab_number < unaccept_arg0 || tab_number > LAST_TAB_NR)
Bram Moolenaar8930caa2020-07-23 16:37:03 +02005511 eap->errmsg = ex_errmsg(e_invarg2, eap->arg);
Bram Moolenaardea25702017-01-29 15:18:10 +01005512 }
5513 else if (eap->addr_count > 0)
5514 {
5515 if (unaccept_arg0 && eap->line2 == 0)
Bram Moolenaarc6251552017-01-29 21:42:20 +01005516 {
Bram Moolenaar8930caa2020-07-23 16:37:03 +02005517 eap->errmsg = _(e_invrange);
Bram Moolenaarc6251552017-01-29 21:42:20 +01005518 tab_number = 0;
5519 }
Bram Moolenaardea25702017-01-29 15:18:10 +01005520 else
5521 {
5522 tab_number = eap->line2;
Bram Moolenaar82a12462019-01-22 22:41:42 +01005523 if (!unaccept_arg0 && *skipwhite(*eap->cmdlinep) == '-')
Bram Moolenaardea25702017-01-29 15:18:10 +01005524 {
5525 --tab_number;
5526 if (tab_number < unaccept_arg0)
Bram Moolenaar8930caa2020-07-23 16:37:03 +02005527 eap->errmsg = _(e_invrange);
Bram Moolenaardea25702017-01-29 15:18:10 +01005528 }
5529 }
5530 }
5531 else
5532 {
5533 switch (eap->cmdidx)
5534 {
5535 case CMD_tabnext:
5536 tab_number = tabpage_index(curtab) + 1;
5537 if (tab_number > LAST_TAB_NR)
5538 tab_number = 1;
5539 break;
5540 case CMD_tabmove:
5541 tab_number = LAST_TAB_NR;
5542 break;
5543 default:
5544 tab_number = tabpage_index(curtab);
5545 }
5546 }
5547
5548theend:
5549 return tab_number;
5550}
5551
5552/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00005553 * ":tabclose": close current tab page, unless it is the last one.
5554 * ":tabclose N": close tab page N.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005555 */
5556 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005557ex_tabclose(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005558{
Bram Moolenaarf740b292006-02-16 22:11:02 +00005559 tabpage_T *tp;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005560 int tab_number;
Bram Moolenaarf740b292006-02-16 22:11:02 +00005561
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005562# ifdef FEAT_CMDWIN
5563 if (cmdwin_type != 0)
5564 cmdwin_result = K_IGNORE;
5565 else
5566# endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00005567 if (first_tabpage->tp_next == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005568 emsg(_("E784: Cannot close last tab page"));
Bram Moolenaarf740b292006-02-16 22:11:02 +00005569 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005570 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005571 tab_number = get_tabpage_arg(eap);
5572 if (eap->errmsg == NULL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005573 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005574 tp = find_tabpage(tab_number);
Bram Moolenaarf740b292006-02-16 22:11:02 +00005575 if (tp == NULL)
5576 {
5577 beep_flush();
5578 return;
5579 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005580 if (tp != curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00005581 {
5582 tabpage_close_other(tp, eap->forceit);
5583 return;
5584 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005585 else if (!text_locked() && !curbuf_locked())
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005586 tabpage_close(eap->forceit);
5587 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005588 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005589}
5590
5591/*
5592 * ":tabonly": close all tab pages except the current one
5593 */
5594 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005595ex_tabonly(exarg_T *eap)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005596{
5597 tabpage_T *tp;
5598 int done;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005599 int tab_number;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005600
5601# ifdef FEAT_CMDWIN
5602 if (cmdwin_type != 0)
5603 cmdwin_result = K_IGNORE;
5604 else
5605# endif
5606 if (first_tabpage->tp_next == NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01005607 msg(_("Already only one tab page"));
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005608 else
5609 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005610 tab_number = get_tabpage_arg(eap);
5611 if (eap->errmsg == NULL)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005612 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005613 goto_tabpage(tab_number);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005614 // Repeat this up to a 1000 times, because autocommands may
5615 // mess up the lists.
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005616 for (done = 0; done < 1000; ++done)
5617 {
5618 FOR_ALL_TABPAGES(tp)
5619 if (tp->tp_topframe != topframe)
5620 {
5621 tabpage_close_other(tp, eap->forceit);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005622 // if we failed to close it quit
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005623 if (valid_tabpage(tp))
5624 done = 1000;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005625 // start over, "tp" is now invalid
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005626 break;
5627 }
5628 if (first_tabpage->tp_next == NULL)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005629 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005630 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005631 }
5632 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005633}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005634
5635/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00005636 * Close the current tab page.
5637 */
5638 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005639tabpage_close(int forceit)
Bram Moolenaarf740b292006-02-16 22:11:02 +00005640{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005641 // First close all the windows but the current one. If that worked then
5642 // close the last window in this tab, that will close it.
Bram Moolenaar459ca562016-11-10 18:16:33 +01005643 if (!ONE_WINDOW)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005644 close_others(TRUE, forceit);
Bram Moolenaar459ca562016-11-10 18:16:33 +01005645 if (ONE_WINDOW)
Bram Moolenaarf740b292006-02-16 22:11:02 +00005646 ex_win_close(forceit, curwin, NULL);
5647# ifdef FEAT_GUI
5648 need_mouse_correct = TRUE;
5649# endif
5650}
5651
5652/*
5653 * Close tab page "tp", which is not the current tab page.
5654 * Note that autocommands may make "tp" invalid.
Bram Moolenaar7875acc2006-09-10 13:51:17 +00005655 * Also takes care of the tab pages line disappearing when closing the
5656 * last-but-one tab page.
Bram Moolenaarf740b292006-02-16 22:11:02 +00005657 */
5658 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005659tabpage_close_other(tabpage_T *tp, int forceit)
Bram Moolenaarf740b292006-02-16 22:11:02 +00005660{
5661 int done = 0;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005662 win_T *wp;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00005663 int h = tabline_height();
Bram Moolenaarf740b292006-02-16 22:11:02 +00005664
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005665 // Limit to 1000 windows, autocommands may add a window while we close
5666 // one. OK, so I'm paranoid...
Bram Moolenaarf740b292006-02-16 22:11:02 +00005667 while (++done < 1000)
5668 {
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005669 wp = tp->tp_firstwin;
5670 ex_win_close(forceit, wp, tp);
Bram Moolenaarf740b292006-02-16 22:11:02 +00005671
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005672 // Autocommands may delete the tab page under our fingers and we may
5673 // fail to close a window with a modified buffer.
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005674 if (!valid_tabpage(tp) || tp->tp_firstwin == wp)
Bram Moolenaarf740b292006-02-16 22:11:02 +00005675 break;
5676 }
Bram Moolenaar7875acc2006-09-10 13:51:17 +00005677
Bram Moolenaar29323592016-07-24 22:04:11 +02005678 apply_autocmds(EVENT_TABCLOSED, NULL, NULL, FALSE, curbuf);
Bram Moolenaar29323592016-07-24 22:04:11 +02005679
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005680 redraw_tabline = TRUE;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00005681 if (h != tabline_height())
5682 shell_new_rows();
Bram Moolenaarf740b292006-02-16 22:11:02 +00005683}
5684
5685/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005686 * ":only".
5687 */
5688 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005689ex_only(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005690{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005691 win_T *wp;
5692 int wnr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005693# ifdef FEAT_GUI
5694 need_mouse_correct = TRUE;
5695# endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005696 if (eap->addr_count > 0)
5697 {
5698 wnr = eap->line2;
5699 for (wp = firstwin; --wnr > 0; )
5700 {
5701 if (wp->w_next == NULL)
5702 break;
5703 else
5704 wp = wp->w_next;
5705 }
5706 win_goto(wp);
5707 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005708 close_others(TRUE, eap->forceit);
5709}
5710
Bram Moolenaar071d4272004-06-13 20:20:40 +00005711 static void
Bram Moolenaar5e1e6d22017-01-02 17:26:00 +01005712ex_hide(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005713{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005714 // ":hide" or ":hide | cmd": hide current window
Bram Moolenaar2256c992016-11-15 21:17:07 +01005715 if (!eap->skip)
5716 {
Bram Moolenaar4033c552017-09-16 20:54:51 +02005717#ifdef FEAT_GUI
Bram Moolenaar2256c992016-11-15 21:17:07 +01005718 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005719#endif
Bram Moolenaar2256c992016-11-15 21:17:07 +01005720 if (eap->addr_count == 0)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005721 win_close(curwin, FALSE); // don't free buffer
Bram Moolenaar2256c992016-11-15 21:17:07 +01005722 else
5723 {
5724 int winnr = 0;
5725 win_T *win;
Bram Moolenaarf240e182014-11-27 18:33:02 +01005726
Bram Moolenaar2256c992016-11-15 21:17:07 +01005727 FOR_ALL_WINDOWS(win)
5728 {
5729 winnr++;
5730 if (winnr == eap->line2)
5731 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005732 }
Bram Moolenaar2256c992016-11-15 21:17:07 +01005733 if (win == NULL)
5734 win = lastwin;
5735 win_close(win, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005736 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005737 }
5738}
5739
5740/*
5741 * ":stop" and ":suspend": Suspend Vim.
5742 */
5743 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005744ex_stop(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005745{
5746 /*
5747 * Disallow suspending for "rvim".
5748 */
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005749 if (!check_restricted())
Bram Moolenaar071d4272004-06-13 20:20:40 +00005750 {
5751 if (!eap->forceit)
5752 autowrite_all();
5753 windgoto((int)Rows - 1, 0);
5754 out_char('\n');
5755 out_flush();
5756 stoptermcap();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005757 out_flush(); // needed for SUN to restore xterm buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00005758#ifdef FEAT_TITLE
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005759 mch_restore_title(SAVE_RESTORE_BOTH); // restore window titles
Bram Moolenaar071d4272004-06-13 20:20:40 +00005760#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005761 ui_suspend(); // call machine specific function
Bram Moolenaar071d4272004-06-13 20:20:40 +00005762#ifdef FEAT_TITLE
5763 maketitle();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005764 resettitle(); // force updating the title
Bram Moolenaar071d4272004-06-13 20:20:40 +00005765#endif
5766 starttermcap();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005767 scroll_start(); // scroll screen before redrawing
Bram Moolenaar071d4272004-06-13 20:20:40 +00005768 redraw_later_clear();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005769 shell_resized(); // may have resized window
Bram Moolenaar071d4272004-06-13 20:20:40 +00005770 }
5771}
5772
5773/*
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005774 * ":exit", ":xit" and ":wq": Write file and quite the current window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005775 */
5776 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005777ex_exit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005778{
Bram Moolenaar461f2122020-07-28 20:25:47 +02005779#ifdef FEAT_EVAL
Bram Moolenaarae616492020-07-28 20:07:27 +02005780 if (not_in_vim9(eap) == FAIL)
5781 return;
Bram Moolenaar461f2122020-07-28 20:25:47 +02005782#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005783#ifdef FEAT_CMDWIN
5784 if (cmdwin_type != 0)
5785 {
5786 cmdwin_result = Ctrl_C;
5787 return;
5788 }
5789#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005790 // Don't quit while editing the command line.
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005791 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005792 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005793 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005794 return;
5795 }
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005796
5797 if (before_quit_autocmds(curwin, FALSE, eap->forceit))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005798 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005799
5800 /*
5801 * if more files or windows we won't exit
5802 */
5803 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
5804 exiting = TRUE;
5805 if ( ((eap->cmdidx == CMD_wq
5806 || curbufIsChanged())
5807 && do_write(eap) == FAIL)
5808 || check_more(TRUE, eap->forceit) == FAIL
Bram Moolenaar027387f2016-01-02 22:25:52 +01005809 || (only_one_window() && check_changed_any(eap->forceit, FALSE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005810 {
5811 not_exiting();
5812 }
5813 else
5814 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005815 if (only_one_window()) // quit last window, exit Vim
Bram Moolenaar071d4272004-06-13 20:20:40 +00005816 getout(0);
Bram Moolenaar2c33d7b2017-10-14 16:06:20 +02005817 not_exiting();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005818# ifdef FEAT_GUI
5819 need_mouse_correct = TRUE;
5820# endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005821 // Quit current window, may free the buffer.
Bram Moolenaareb44a682017-08-03 22:44:55 +02005822 win_close(curwin, !buf_hide(curwin->w_buffer));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005823 }
5824}
5825
5826/*
5827 * ":print", ":list", ":number".
5828 */
5829 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005830ex_print(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005831{
Bram Moolenaardf177f62005-02-22 08:39:57 +00005832 if (curbuf->b_ml.ml_flags & ML_EMPTY)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005833 emsg(_(e_emptybuf));
Bram Moolenaardf177f62005-02-22 08:39:57 +00005834 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005835 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00005836 for ( ;!got_int; ui_breakcheck())
5837 {
5838 print_line(eap->line1,
5839 (eap->cmdidx == CMD_number || eap->cmdidx == CMD_pound
5840 || (eap->flags & EXFLAG_NR)),
5841 eap->cmdidx == CMD_list || (eap->flags & EXFLAG_LIST));
5842 if (++eap->line1 > eap->line2)
5843 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005844 out_flush(); // show one line at a time
Bram Moolenaardf177f62005-02-22 08:39:57 +00005845 }
5846 setpcmark();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005847 // put cursor at last line
Bram Moolenaardf177f62005-02-22 08:39:57 +00005848 curwin->w_cursor.lnum = eap->line2;
5849 beginline(BL_SOL | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005850 }
5851
Bram Moolenaar071d4272004-06-13 20:20:40 +00005852 ex_no_reprint = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005853}
5854
5855#ifdef FEAT_BYTEOFF
5856 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005857ex_goto(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005858{
5859 goto_byte(eap->line2);
5860}
5861#endif
5862
5863/*
5864 * ":shell".
5865 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005866 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005867ex_shell(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005868{
5869 do_shell(NULL, 0);
5870}
5871
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005872#if defined(HAVE_DROP_FILE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005873
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005874static int drop_busy = FALSE;
5875static int drop_filec;
5876static char_u **drop_filev = NULL;
5877static int drop_split;
5878static void (*drop_callback)(void *);
5879static void *drop_cookie;
5880
5881 static void
5882handle_drop_internal(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005883{
5884 exarg_T ea;
5885 int save_msg_scroll = msg_scroll;
5886
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005887 // Setting the argument list may cause screen updates and being called
5888 // recursively. Avoid that by setting drop_busy.
5889 drop_busy = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005890
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005891 // Check whether the current buffer is changed. If so, we will need
5892 // to split the current window or data could be lost.
5893 // We don't need to check if the 'hidden' option is set, as in this
5894 // case the buffer won't be lost.
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005895 if (!buf_hide(curbuf) && !drop_split)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005896 {
5897 ++emsg_off;
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005898 drop_split = check_changed(curbuf, CCGD_AW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005899 --emsg_off;
5900 }
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005901 if (drop_split)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005902 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005903 if (win_split(0, 0) == FAIL)
5904 return;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02005905 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005906
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005907 // When splitting the window, create a new alist. Otherwise the
5908 // existing one is overwritten.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005909 alist_unlink(curwin->w_alist);
5910 alist_new();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005911 }
5912
5913 /*
5914 * Set up the new argument list.
5915 */
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005916 alist_set(ALIST(curwin), drop_filec, drop_filev, FALSE, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005917
5918 /*
5919 * Move to the first file.
5920 */
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005921 // Fake up a minimal "next" command for do_argfile()
Bram Moolenaara80faa82020-04-12 19:37:17 +02005922 CLEAR_FIELD(ea);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005923 ea.cmd = (char_u *)"next";
5924 do_argfile(&ea, 0);
5925
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005926 // do_ecmd() may set need_start_insertmode, but since we never left Insert
5927 // mode that is not needed here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005928 need_start_insertmode = FALSE;
5929
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005930 // Restore msg_scroll, otherwise a following command may cause scrolling
5931 // unexpectedly. The screen will be redrawn by the caller, thus
5932 // msg_scroll being set by displaying a message is irrelevant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005933 msg_scroll = save_msg_scroll;
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005934
5935 if (drop_callback != NULL)
5936 drop_callback(drop_cookie);
5937
5938 drop_filev = NULL;
5939 drop_busy = FALSE;
5940}
5941
5942/*
5943 * Handle a file drop. The code is here because a drop is *nearly* like an
5944 * :args command, but not quite (we have a list of exact filenames, so we
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01005945 * don't want to (a) parse a command line, or (b) expand wildcards). So the
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005946 * code is very similar to :args and hence needs access to a lot of the static
5947 * functions in this file.
5948 *
5949 * The "filev" list must have been allocated using alloc(), as should each item
5950 * in the list. This function takes over responsibility for freeing the "filev"
5951 * list.
5952 */
5953 void
5954handle_drop(
5955 int filec, // the number of files dropped
5956 char_u **filev, // the list of files dropped
5957 int split, // force splitting the window
5958 void (*callback)(void *), // to be called after setting the argument
5959 // list
5960 void *cookie) // argument for "callback" (allocated)
5961{
5962 // Cannot handle recursive drops, finish the pending one.
5963 if (drop_busy)
5964 {
5965 FreeWild(filec, filev);
5966 vim_free(cookie);
5967 return;
5968 }
5969
5970 // When calling handle_drop() more than once in a row we only use the last
5971 // one.
5972 if (drop_filev != NULL)
5973 {
5974 FreeWild(drop_filec, drop_filev);
5975 vim_free(drop_cookie);
5976 }
5977
5978 drop_filec = filec;
5979 drop_filev = filev;
5980 drop_split = split;
5981 drop_callback = callback;
5982 drop_cookie = cookie;
5983
5984 // Postpone this when:
5985 // - editing the command line
5986 // - not possible to change the current buffer
5987 // - updating the screen
5988 // As it may change buffers and window structures that are in use and cause
5989 // freed memory to be used.
5990 if (text_locked() || curbuf_locked() || updating_screen)
5991 return;
5992
5993 handle_drop_internal();
5994}
5995
5996/*
5997 * To be called when text is unlocked, curbuf is unlocked or updating_screen is
5998 * reset: Handle a postponed drop.
5999 */
6000 void
6001handle_any_postponed_drop(void)
6002{
6003 if (!drop_busy && drop_filev != NULL
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +02006004 && !text_locked() && !curbuf_locked() && !updating_screen)
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006005 handle_drop_internal();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006006}
6007#endif
6008
Bram Moolenaar071d4272004-06-13 20:20:40 +00006009/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006010 * ":preserve".
6011 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006012 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006013ex_preserve(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006014{
Bram Moolenaar4399ef42005-02-12 14:29:27 +00006015 curbuf->b_flags |= BF_PRESERVED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006016 ml_preserve(curbuf, TRUE);
6017}
6018
6019/*
6020 * ":recover".
6021 */
6022 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006023ex_recover(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006024{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006025 // Set recoverymode right away to avoid the ATTENTION prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006026 recoverymode = TRUE;
Bram Moolenaar45d3b142013-11-09 03:31:51 +01006027 if (!check_changed(curbuf, (p_awa ? CCGD_AW : 0)
6028 | CCGD_MULTWIN
6029 | (eap->forceit ? CCGD_FORCEIT : 0)
6030 | CCGD_EXCMD)
6031
Bram Moolenaar071d4272004-06-13 20:20:40 +00006032 && (*eap->arg == NUL
6033 || setfname(curbuf, eap->arg, NULL, TRUE) == OK))
Bram Moolenaar99499b12019-05-23 21:35:48 +02006034 ml_recover(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006035 recoverymode = FALSE;
6036}
6037
6038/*
6039 * Command modifier used in a wrong way.
6040 */
6041 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006042ex_wrongmodifier(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006043{
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006044 eap->errmsg = _(e_invalid_command);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006045}
6046
Bram Moolenaar071d4272004-06-13 20:20:40 +00006047/*
6048 * :sview [+command] file split window with new file, read-only
6049 * :split [[+command] file] split window with current or new file
6050 * :vsplit [[+command] file] split window vertically with current or new file
6051 * :new [[+command] file] split window with no or new file
6052 * :vnew [[+command] file] split vertically window with no or new file
6053 * :sfind [+command] file split window with file in 'path'
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006054 *
6055 * :tabedit open new Tab page with empty window
6056 * :tabedit [+command] file open new Tab page and edit "file"
6057 * :tabnew [[+command] file] just like :tabedit
6058 * :tabfind [+command] file open new Tab page and find "file"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006059 */
6060 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006061ex_splitview(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006062{
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006063 win_T *old_curwin = curwin;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006064#if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006065 char_u *fname = NULL;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006066#endif
6067#ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00006068 int browse_flag = cmdmod.browse;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006069#endif
Bram Moolenaar39902a02018-06-21 22:10:08 +02006070 int use_tab = eap->cmdidx == CMD_tabedit
6071 || eap->cmdidx == CMD_tabfind
6072 || eap->cmdidx == CMD_tabnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006073
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01006074 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02006075 return;
6076
Bram Moolenaar4033c552017-09-16 20:54:51 +02006077#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00006078 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006079#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006080
Bram Moolenaar4033c552017-09-16 20:54:51 +02006081#ifdef FEAT_QUICKFIX
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006082 // A ":split" in the quickfix window works like ":new". Don't want two
6083 // quickfix windows. But it's OK when doing ":tab split".
Bram Moolenaar05bb9532008-07-04 09:44:11 +00006084 if (bt_quickfix(curbuf) && cmdmod.tab == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006085 {
6086 if (eap->cmdidx == CMD_split)
6087 eap->cmdidx = CMD_new;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006088 if (eap->cmdidx == CMD_vsplit)
6089 eap->cmdidx = CMD_vnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006090 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02006091#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006092
Bram Moolenaar4033c552017-09-16 20:54:51 +02006093#ifdef FEAT_SEARCHPATH
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006094 if (eap->cmdidx == CMD_sfind || eap->cmdidx == CMD_tabfind)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006095 {
6096 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg),
6097 FNAME_MESS, TRUE, curbuf->b_ffname);
6098 if (fname == NULL)
6099 goto theend;
6100 eap->arg = fname;
6101 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006102# ifdef FEAT_BROWSE
Bram Moolenaar4033c552017-09-16 20:54:51 +02006103 else
6104# endif
6105#endif
6106#ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00006107 if (cmdmod.browse
Bram Moolenaar071d4272004-06-13 20:20:40 +00006108 && eap->cmdidx != CMD_vnew
Bram Moolenaar071d4272004-06-13 20:20:40 +00006109 && eap->cmdidx != CMD_new)
6110 {
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006111 if (
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006112# ifdef FEAT_GUI
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006113 !gui.in_use &&
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006114# endif
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006115 au_has_group((char_u *)"FileExplorer"))
6116 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006117 // No browsing supported but we do have the file explorer:
6118 // Edit the directory.
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006119 if (*eap->arg == NUL || !mch_isdir(eap->arg))
6120 eap->arg = (char_u *)".";
6121 }
6122 else
6123 {
Bram Moolenaar39902a02018-06-21 22:10:08 +02006124 fname = do_browse(0, (char_u *)(use_tab
6125 ? _("Edit File in new tab page")
6126 : _("Edit File in new window")),
Bram Moolenaar071d4272004-06-13 20:20:40 +00006127 eap->arg, NULL, NULL, NULL, curbuf);
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006128 if (fname == NULL)
6129 goto theend;
6130 eap->arg = fname;
6131 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006132 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006133 cmdmod.browse = FALSE; // Don't browse again in do_ecmd().
Bram Moolenaar4033c552017-09-16 20:54:51 +02006134#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006135
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006136 /*
6137 * Either open new tab page or split the window.
6138 */
Bram Moolenaar39902a02018-06-21 22:10:08 +02006139 if (use_tab)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006140 {
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006141 if (win_new_tabpage(cmdmod.tab != 0 ? cmdmod.tab
6142 : eap->addr_count == 0 ? 0
6143 : (int)eap->line2 + 1) != FAIL)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006144 {
Bram Moolenaard2b66012008-01-09 19:30:36 +00006145 do_exedit(eap, old_curwin);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006146
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006147 // set the alternate buffer for the window we came from
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006148 if (curwin != old_curwin
6149 && win_valid(old_curwin)
6150 && old_curwin->w_buffer != curbuf
6151 && !cmdmod.keepalt)
6152 old_curwin->w_alt_fnum = curbuf->b_fnum;
6153 }
6154 }
6155 else if (win_split(eap->addr_count > 0 ? (int)eap->line2 : 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006156 *eap->cmd == 'v' ? WSP_VERT : 0) != FAIL)
6157 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006158 // Reset 'scrollbind' when editing another file, but keep it when
6159 // doing ":split" without arguments.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006160 if (*eap->arg != NUL
Bram Moolenaar8a3bb562018-03-04 20:14:14 +01006161# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00006162 || cmdmod.browse
Bram Moolenaar8a3bb562018-03-04 20:14:14 +01006163# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006164 )
Bram Moolenaar3368ea22010-09-21 16:56:35 +02006165 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006166 else
6167 do_check_scrollbind(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006168 do_exedit(eap, old_curwin);
6169 }
6170
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006171# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00006172 cmdmod.browse = browse_flag;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006173# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006174
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006175# if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006176theend:
6177 vim_free(fname);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006178# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006179}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006180
6181/*
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006182 * Open a new tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006183 */
6184 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006185tabpage_new(void)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006186{
6187 exarg_T ea;
6188
Bram Moolenaara80faa82020-04-12 19:37:17 +02006189 CLEAR_FIELD(ea);
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006190 ea.cmdidx = CMD_tabnew;
6191 ea.cmd = (char_u *)"tabn";
6192 ea.arg = (char_u *)"";
6193 ex_splitview(&ea);
6194}
6195
6196/*
6197 * :tabnext command
6198 */
6199 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006200ex_tabnext(exarg_T *eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006201{
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006202 int tab_number;
6203
Bram Moolenaar8cdbd5b2019-06-16 15:50:45 +02006204 if (ERROR_IF_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02006205 return;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006206 switch (eap->cmdidx)
6207 {
6208 case CMD_tabfirst:
6209 case CMD_tabrewind:
6210 goto_tabpage(1);
6211 break;
6212 case CMD_tablast:
6213 goto_tabpage(9999);
6214 break;
6215 case CMD_tabprevious:
6216 case CMD_tabNext:
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006217 if (eap->arg && *eap->arg != NUL)
6218 {
6219 char_u *p = eap->arg;
6220 char_u *p_save = p;
6221
6222 tab_number = getdigits(&p);
6223 if (p == p_save || *p_save == '-' || *p != NUL
6224 || tab_number == 0)
6225 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006226 // No numbers as argument.
Bram Moolenaar8930caa2020-07-23 16:37:03 +02006227 eap->errmsg = ex_errmsg(e_invarg2, eap->arg);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006228 return;
6229 }
6230 }
6231 else
6232 {
6233 if (eap->addr_count == 0)
6234 tab_number = 1;
6235 else
6236 {
6237 tab_number = eap->line2;
6238 if (tab_number < 1)
6239 {
Bram Moolenaar8930caa2020-07-23 16:37:03 +02006240 eap->errmsg = _(e_invrange);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006241 return;
6242 }
6243 }
6244 }
6245 goto_tabpage(-tab_number);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006246 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006247 default: // CMD_tabnext
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006248 tab_number = get_tabpage_arg(eap);
6249 if (eap->errmsg == NULL)
6250 goto_tabpage(tab_number);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006251 break;
6252 }
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006253}
6254
6255/*
6256 * :tabmove command
6257 */
6258 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006259ex_tabmove(exarg_T *eap)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006260{
Bram Moolenaar40ce3a42015-04-21 18:08:39 +02006261 int tab_number;
Bram Moolenaar8cb8dca2012-07-06 18:27:39 +02006262
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006263 tab_number = get_tabpage_arg(eap);
6264 if (eap->errmsg == NULL)
6265 tabpage_move(tab_number);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006266}
6267
6268/*
6269 * :tabs command: List tabs and their contents.
6270 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006271 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006272ex_tabs(exarg_T *eap UNUSED)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006273{
6274 tabpage_T *tp;
6275 win_T *wp;
6276 int tabcount = 1;
6277
6278 msg_start();
6279 msg_scroll = TRUE;
6280 for (tp = first_tabpage; tp != NULL && !got_int; tp = tp->tp_next)
6281 {
6282 msg_putchar('\n');
6283 vim_snprintf((char *)IObuff, IOSIZE, _("Tab page %d"), tabcount++);
Bram Moolenaar8820b482017-03-16 17:23:31 +01006284 msg_outtrans_attr(IObuff, HL_ATTR(HLF_T));
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006285 out_flush(); // output one line at a time
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006286 ui_breakcheck();
6287
Bram Moolenaar030f0df2006-02-21 22:02:53 +00006288 if (tp == curtab)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006289 wp = firstwin;
6290 else
6291 wp = tp->tp_firstwin;
6292 for ( ; wp != NULL && !got_int; wp = wp->w_next)
6293 {
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006294 msg_putchar('\n');
6295 msg_putchar(wp == curwin ? '>' : ' ');
6296 msg_putchar(' ');
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006297 msg_putchar(bufIsChanged(wp->w_buffer) ? '+' : ' ');
6298 msg_putchar(' ');
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006299 if (buf_spname(wp->w_buffer) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02006300 vim_strncpy(IObuff, buf_spname(wp->w_buffer), IOSIZE - 1);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006301 else
6302 home_replace(wp->w_buffer, wp->w_buffer->b_fname,
6303 IObuff, IOSIZE, TRUE);
6304 msg_outtrans(IObuff);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006305 out_flush(); // output one line at a time
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006306 ui_breakcheck();
6307 }
6308 }
6309}
6310
Bram Moolenaar071d4272004-06-13 20:20:40 +00006311/*
6312 * ":mode": Set screen mode.
6313 * If no argument given, just get the screen size and redraw.
6314 */
6315 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006316ex_mode(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006317{
6318 if (*eap->arg == NUL)
6319 shell_resized();
6320 else
Bram Moolenaar3c71aec2020-01-17 19:32:20 +01006321 emsg(_(e_screenmode));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006322}
6323
Bram Moolenaar071d4272004-06-13 20:20:40 +00006324/*
6325 * ":resize".
6326 * set, increment or decrement current window height
6327 */
6328 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006329ex_resize(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006330{
6331 int n;
6332 win_T *wp = curwin;
6333
6334 if (eap->addr_count > 0)
6335 {
6336 n = eap->line2;
6337 for (wp = firstwin; wp->w_next != NULL && --n > 0; wp = wp->w_next)
6338 ;
6339 }
6340
Bram Moolenaar44a2f922016-03-19 22:11:51 +01006341# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00006342 need_mouse_correct = TRUE;
Bram Moolenaar44a2f922016-03-19 22:11:51 +01006343# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006344 n = atol((char *)eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006345 if (cmdmod.split & WSP_VERT)
6346 {
6347 if (*eap->arg == '-' || *eap->arg == '+')
Bram Moolenaar02631462017-09-22 15:20:32 +02006348 n += curwin->w_width;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006349 else if (n == 0 && eap->arg[0] == NUL) // default is very wide
Bram Moolenaar071d4272004-06-13 20:20:40 +00006350 n = 9999;
6351 win_setwidth_win((int)n, wp);
6352 }
6353 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006354 {
6355 if (*eap->arg == '-' || *eap->arg == '+')
6356 n += curwin->w_height;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006357 else if (n == 0 && eap->arg[0] == NUL) // default is very high
Bram Moolenaar071d4272004-06-13 20:20:40 +00006358 n = 9999;
6359 win_setheight_win((int)n, wp);
6360 }
6361}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006362
6363/*
6364 * ":find [+command] <file>" command.
6365 */
6366 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006367ex_find(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006368{
6369#ifdef FEAT_SEARCHPATH
6370 char_u *fname;
6371 int count;
6372
6373 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg), FNAME_MESS,
6374 TRUE, curbuf->b_ffname);
6375 if (eap->addr_count > 0)
6376 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006377 // Repeat finding the file "count" times. This matters when it
6378 // appears several times in the path.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006379 count = eap->line2;
6380 while (fname != NULL && --count > 0)
6381 {
6382 vim_free(fname);
6383 fname = find_file_in_path(NULL, 0, FNAME_MESS,
6384 FALSE, curbuf->b_ffname);
6385 }
6386 }
6387
6388 if (fname != NULL)
6389 {
6390 eap->arg = fname;
6391#endif
6392 do_exedit(eap, NULL);
6393#ifdef FEAT_SEARCHPATH
6394 vim_free(fname);
6395 }
6396#endif
6397}
6398
6399/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00006400 * ":open" simulation: for now just work like ":visual".
6401 */
6402 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006403ex_open(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00006404{
6405 regmatch_T regmatch;
6406 char_u *p;
6407
6408 curwin->w_cursor.lnum = eap->line2;
6409 beginline(BL_SOL | BL_FIX);
6410 if (*eap->arg == '/')
6411 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006412 // ":open /pattern/": put cursor in column found with pattern
Bram Moolenaardf177f62005-02-22 08:39:57 +00006413 ++eap->arg;
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006414 p = skip_regexp(eap->arg, '/', p_magic);
Bram Moolenaardf177f62005-02-22 08:39:57 +00006415 *p = NUL;
6416 regmatch.regprog = vim_regcomp(eap->arg, p_magic ? RE_MAGIC : 0);
6417 if (regmatch.regprog != NULL)
6418 {
6419 regmatch.rm_ic = p_ic;
6420 p = ml_get_curline();
6421 if (vim_regexec(&regmatch, p, (colnr_T)0))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006422 curwin->w_cursor.col = (colnr_T)(regmatch.startp[0] - p);
Bram Moolenaardf177f62005-02-22 08:39:57 +00006423 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006424 emsg(_(e_nomatch));
Bram Moolenaar473de612013-06-08 18:19:48 +02006425 vim_regfree(regmatch.regprog);
Bram Moolenaardf177f62005-02-22 08:39:57 +00006426 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006427 // Move to the NUL, ignore any other arguments.
Bram Moolenaardf177f62005-02-22 08:39:57 +00006428 eap->arg += STRLEN(eap->arg);
6429 }
6430 check_cursor();
6431
6432 eap->cmdidx = CMD_visual;
6433 do_exedit(eap, NULL);
6434}
6435
6436/*
6437 * ":edit", ":badd", ":visual".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006438 */
6439 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006440ex_edit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006441{
6442 do_exedit(eap, NULL);
6443}
6444
6445/*
Bram Moolenaar4b96df52020-01-26 22:00:26 +01006446 * ":edit <file>" command and alike.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006447 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006448 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006449do_exedit(
6450 exarg_T *eap,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006451 win_T *old_curwin) // curwin before doing a split or NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006452{
6453 int n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006454 int need_hide;
Bram Moolenaardf177f62005-02-22 08:39:57 +00006455 int exmode_was = exmode_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006456
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01006457 if ((eap->cmdidx != CMD_pedit && ERROR_IF_POPUP_WINDOW)
6458 || ERROR_IF_TERM_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02006459 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006460 /*
6461 * ":vi" command ends Ex mode.
6462 */
6463 if (exmode_active && (eap->cmdidx == CMD_visual
6464 || eap->cmdidx == CMD_view))
6465 {
6466 exmode_active = FALSE;
Bram Moolenaar158ea172020-06-18 17:28:39 +02006467 ex_pressedreturn = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006468 if (*eap->arg == NUL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00006469 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006470 // Special case: ":global/pat/visual\NLvi-commands"
Bram Moolenaardf177f62005-02-22 08:39:57 +00006471 if (global_busy)
6472 {
6473 int rd = RedrawingDisabled;
6474 int nwr = no_wait_return;
6475 int ms = msg_scroll;
6476#ifdef FEAT_GUI
6477 int he = hold_gui_events;
6478#endif
6479
6480 if (eap->nextcmd != NULL)
6481 {
6482 stuffReadbuff(eap->nextcmd);
6483 eap->nextcmd = NULL;
6484 }
6485
6486 if (exmode_was != EXMODE_VIM)
6487 settmode(TMODE_RAW);
6488 RedrawingDisabled = 0;
6489 no_wait_return = 0;
6490 need_wait_return = FALSE;
6491 msg_scroll = 0;
6492#ifdef FEAT_GUI
6493 hold_gui_events = 0;
6494#endif
6495 must_redraw = CLEAR;
Bram Moolenaar9e2bcb52020-02-18 21:33:00 +01006496 pending_exmode_active = TRUE;
Bram Moolenaardf177f62005-02-22 08:39:57 +00006497
6498 main_loop(FALSE, TRUE);
6499
Bram Moolenaar9e2bcb52020-02-18 21:33:00 +01006500 pending_exmode_active = FALSE;
Bram Moolenaardf177f62005-02-22 08:39:57 +00006501 RedrawingDisabled = rd;
6502 no_wait_return = nwr;
6503 msg_scroll = ms;
6504#ifdef FEAT_GUI
6505 hold_gui_events = he;
6506#endif
6507 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006508 return;
Bram Moolenaardf177f62005-02-22 08:39:57 +00006509 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006510 }
6511
6512 if ((eap->cmdidx == CMD_new
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006513 || eap->cmdidx == CMD_tabnew
6514 || eap->cmdidx == CMD_tabedit
Bram Moolenaar4033c552017-09-16 20:54:51 +02006515 || eap->cmdidx == CMD_vnew) && *eap->arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006516 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006517 // ":new" or ":tabnew" without argument: edit an new empty buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00006518 setpcmark();
6519 (void)do_ecmd(0, NULL, NULL, eap, ECMD_ONE,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00006520 ECMD_HIDE + (eap->forceit ? ECMD_FORCEIT : 0),
6521 old_curwin == NULL ? curwin : NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006522 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02006523 else if ((eap->cmdidx != CMD_split && eap->cmdidx != CMD_vsplit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006524 || *eap->arg != NUL
6525#ifdef FEAT_BROWSE
6526 || cmdmod.browse
6527#endif
6528 )
6529 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006530 // Can't edit another file when "curbuf_lock" is set. Only ":edit"
6531 // can bring us here, others are stopped earlier.
Bram Moolenaar5555acc2006-04-07 21:33:12 +00006532 if (*eap->arg != NUL && curbuf_locked())
6533 return;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006534
Bram Moolenaar071d4272004-06-13 20:20:40 +00006535 n = readonlymode;
6536 if (eap->cmdidx == CMD_view || eap->cmdidx == CMD_sview)
6537 readonlymode = TRUE;
6538 else if (eap->cmdidx == CMD_enew)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006539 readonlymode = FALSE; // 'readonly' doesn't make sense in an
6540 // empty buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00006541 setpcmark();
6542 if (do_ecmd(0, (eap->cmdidx == CMD_enew ? NULL : eap->arg),
6543 NULL, eap,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006544 // ":edit" goes to first line if Vi compatible
Bram Moolenaar071d4272004-06-13 20:20:40 +00006545 (*eap->arg == NUL && eap->do_ecmd_lnum == 0
6546 && vim_strchr(p_cpo, CPO_GOTO1) != NULL)
6547 ? ECMD_ONE : eap->do_ecmd_lnum,
Bram Moolenaareb44a682017-08-03 22:44:55 +02006548 (buf_hide(curbuf) ? ECMD_HIDE : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006549 + (eap->forceit ? ECMD_FORCEIT : 0)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006550 // after a split we can use an existing buffer
Bram Moolenaar7b449342014-03-25 13:03:48 +01006551 + (old_curwin != NULL ? ECMD_OLDBUF : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006552 + (eap->cmdidx == CMD_badd ? ECMD_ADDBUF : 0 )
Bram Moolenaar701f7af2008-11-15 13:12:07 +00006553 , old_curwin == NULL ? curwin : NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006554 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006555 // Editing the file failed. If the window was split, close it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006556 if (old_curwin != NULL)
6557 {
6558 need_hide = (curbufIsChanged() && curbuf->b_nwindows <= 1);
Bram Moolenaareb44a682017-08-03 22:44:55 +02006559 if (!need_hide || buf_hide(curbuf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006560 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006561#if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00006562 cleanup_T cs;
6563
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006564 // Reset the error/interrupt/exception state here so that
6565 // aborting() returns FALSE when closing a window.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00006566 enter_cleanup(&cs);
Bram Moolenaar4033c552017-09-16 20:54:51 +02006567#endif
6568#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00006569 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006570#endif
Bram Moolenaareb44a682017-08-03 22:44:55 +02006571 win_close(curwin, !need_hide && !buf_hide(curbuf));
Bram Moolenaarc0197e22004-09-13 20:26:32 +00006572
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006573#if defined(FEAT_EVAL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006574 // Restore the error/interrupt/exception state if not
6575 // discarded by a new aborting error, interrupt, or
6576 // uncaught exception.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00006577 leave_cleanup(&cs);
Bram Moolenaar4033c552017-09-16 20:54:51 +02006578#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006579 }
6580 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006581 }
6582 else if (readonlymode && curbuf->b_nwindows == 1)
6583 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006584 // When editing an already visited buffer, 'readonly' won't be set
6585 // but the previous value is kept. With ":view" and ":sview" we
6586 // want the file to be readonly, except when another window is
6587 // editing the same buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006588 curbuf->b_p_ro = TRUE;
6589 }
6590 readonlymode = n;
6591 }
6592 else
6593 {
6594 if (eap->do_ecmd_cmd != NULL)
6595 do_cmdline_cmd(eap->do_ecmd_cmd);
6596#ifdef FEAT_TITLE
6597 n = curwin->w_arg_idx_invalid;
6598#endif
6599 check_arg_idx(curwin);
6600#ifdef FEAT_TITLE
6601 if (n != curwin->w_arg_idx_invalid)
6602 maketitle();
6603#endif
6604 }
6605
Bram Moolenaar071d4272004-06-13 20:20:40 +00006606 /*
6607 * if ":split file" worked, set alternate file name in old window to new
6608 * file
6609 */
6610 if (old_curwin != NULL
6611 && *eap->arg != NUL
6612 && curwin != old_curwin
6613 && win_valid(old_curwin)
Bram Moolenaard4755bb2004-09-02 19:12:26 +00006614 && old_curwin->w_buffer != curbuf
6615 && !cmdmod.keepalt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006616 old_curwin->w_alt_fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006617
6618 ex_no_reprint = TRUE;
6619}
6620
6621#ifndef FEAT_GUI
6622/*
6623 * ":gui" and ":gvim" when there is no GUI.
6624 */
6625 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006626ex_nogui(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006627{
Bram Moolenaar8930caa2020-07-23 16:37:03 +02006628 eap->errmsg = _(e_nogvim);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006629}
6630#endif
6631
Bram Moolenaar4f974752019-02-17 17:44:42 +01006632#if defined(FEAT_GUI_MSWIN) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006633 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006634ex_tearoff(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006635{
6636 gui_make_tearoff(eap->arg);
6637}
6638#endif
6639
Bram Moolenaar29a2c082018-03-05 21:06:23 +01006640#if (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK) \
6641 || defined(FEAT_TERM_POPUP_MENU)) && defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006642 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006643ex_popup(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006644{
Bram Moolenaar29a2c082018-03-05 21:06:23 +01006645# if defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK)
6646 if (gui.in_use)
6647 gui_make_popup(eap->arg, eap->forceit);
6648# ifdef FEAT_TERM_POPUP_MENU
6649 else
6650# endif
6651# endif
6652# ifdef FEAT_TERM_POPUP_MENU
6653 pum_make_popup(eap->arg, eap->forceit);
6654# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006655}
6656#endif
6657
Bram Moolenaar071d4272004-06-13 20:20:40 +00006658 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006659ex_swapname(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006660{
6661 if (curbuf->b_ml.ml_mfp == NULL || curbuf->b_ml.ml_mfp->mf_fname == NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01006662 msg(_("No swap file"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006663 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01006664 msg((char *)curbuf->b_ml.ml_mfp->mf_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006665}
6666
6667/*
6668 * ":syncbind" forces all 'scrollbind' windows to have the same relative
6669 * offset.
6670 * (1998-11-02 16:21:01 R. Edward Ralston <eralston@computer.org>)
6671 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006672 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006673ex_syncbind(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006674{
Bram Moolenaar071d4272004-06-13 20:20:40 +00006675 win_T *wp;
Bram Moolenaardedd1b02013-12-14 13:06:17 +01006676 win_T *save_curwin = curwin;
6677 buf_T *save_curbuf = curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006678 long topline;
6679 long y;
6680 linenr_T old_linenr = curwin->w_cursor.lnum;
6681
6682 setpcmark();
6683
6684 /*
6685 * determine max topline
6686 */
6687 if (curwin->w_p_scb)
6688 {
6689 topline = curwin->w_topline;
Bram Moolenaar29323592016-07-24 22:04:11 +02006690 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006691 {
6692 if (wp->w_p_scb && wp->w_buffer)
6693 {
Bram Moolenaar375e3392019-01-31 18:26:10 +01006694 y = wp->w_buffer->b_ml.ml_line_count - get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006695 if (topline > y)
6696 topline = y;
6697 }
6698 }
6699 if (topline < 1)
6700 topline = 1;
6701 }
6702 else
6703 {
6704 topline = 1;
6705 }
6706
6707
6708 /*
Bram Moolenaardedd1b02013-12-14 13:06:17 +01006709 * Set all scrollbind windows to the same topline.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006710 */
Bram Moolenaar29323592016-07-24 22:04:11 +02006711 FOR_ALL_WINDOWS(curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006712 {
6713 if (curwin->w_p_scb)
6714 {
Bram Moolenaardedd1b02013-12-14 13:06:17 +01006715 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006716 y = topline - curwin->w_topline;
6717 if (y > 0)
6718 scrollup(y, TRUE);
6719 else
6720 scrolldown(-y, TRUE);
6721 curwin->w_scbind_pos = topline;
6722 redraw_later(VALID);
6723 cursor_correct();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006724 curwin->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006725 }
6726 }
Bram Moolenaardedd1b02013-12-14 13:06:17 +01006727 curwin = save_curwin;
6728 curbuf = save_curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006729 if (curwin->w_p_scb)
6730 {
6731 did_syncbind = TRUE;
6732 checkpcmark();
6733 if (old_linenr != curwin->w_cursor.lnum)
6734 {
6735 char_u ctrl_o[2];
6736
6737 ctrl_o[0] = Ctrl_O;
6738 ctrl_o[1] = 0;
6739 ins_typebuf(ctrl_o, REMAP_NONE, 0, TRUE, FALSE);
6740 }
6741 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006742}
6743
6744
6745 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006746ex_read(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006747{
Bram Moolenaardf177f62005-02-22 08:39:57 +00006748 int i;
6749 int empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
6750 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006751
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006752 if (eap->usefilter) // :r!cmd
Bram Moolenaar071d4272004-06-13 20:20:40 +00006753 do_bang(1, eap, FALSE, FALSE, TRUE);
6754 else
6755 {
6756 if (u_save(eap->line2, (linenr_T)(eap->line2 + 1)) == FAIL)
6757 return;
6758
6759#ifdef FEAT_BROWSE
6760 if (cmdmod.browse)
6761 {
6762 char_u *browseFile;
6763
Bram Moolenaar7171abe2004-10-11 10:06:20 +00006764 browseFile = do_browse(0, (char_u *)_("Append File"), eap->arg,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006765 NULL, NULL, NULL, curbuf);
6766 if (browseFile != NULL)
6767 {
6768 i = readfile(browseFile, NULL,
6769 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
6770 vim_free(browseFile);
6771 }
6772 else
6773 i = OK;
6774 }
6775 else
6776#endif
6777 if (*eap->arg == NUL)
6778 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006779 if (check_fname() == FAIL) // check for no file name
Bram Moolenaar071d4272004-06-13 20:20:40 +00006780 return;
6781 i = readfile(curbuf->b_ffname, curbuf->b_fname,
6782 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
6783 }
6784 else
6785 {
6786 if (vim_strchr(p_cpo, CPO_ALTREAD) != NULL)
6787 (void)setaltfname(eap->arg, eap->arg, (linenr_T)1);
6788 i = readfile(eap->arg, NULL,
6789 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
6790
6791 }
Bram Moolenaare13b9af2017-01-13 22:01:02 +01006792 if (i != OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006793 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006794#if defined(FEAT_EVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006795 if (!aborting())
6796#endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006797 semsg(_(e_notopen), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006798 }
6799 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00006800 {
6801 if (empty && exmode_active)
6802 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006803 // Delete the empty line that remains. Historically ex does
6804 // this but vi doesn't.
Bram Moolenaardf177f62005-02-22 08:39:57 +00006805 if (eap->line2 == 0)
6806 lnum = curbuf->b_ml.ml_line_count;
6807 else
6808 lnum = 1;
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00006809 if (*ml_get(lnum) == NUL && u_savedel(lnum, 1L) == OK)
Bram Moolenaardf177f62005-02-22 08:39:57 +00006810 {
Bram Moolenaarca70c072020-05-30 20:30:46 +02006811 ml_delete(lnum);
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00006812 if (curwin->w_cursor.lnum > 1
6813 && curwin->w_cursor.lnum >= lnum)
Bram Moolenaardf177f62005-02-22 08:39:57 +00006814 --curwin->w_cursor.lnum;
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00006815 deleted_lines_mark(lnum, 1L);
Bram Moolenaardf177f62005-02-22 08:39:57 +00006816 }
6817 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006818 redraw_curbuf_later(VALID);
Bram Moolenaardf177f62005-02-22 08:39:57 +00006819 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006820 }
6821}
6822
Bram Moolenaarea408852005-06-25 22:49:46 +00006823static char_u *prev_dir = NULL;
6824
6825#if defined(EXITFREE) || defined(PROTO)
6826 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006827free_cd_dir(void)
Bram Moolenaarea408852005-06-25 22:49:46 +00006828{
Bram Moolenaard23a8232018-02-10 18:45:26 +01006829 VIM_CLEAR(prev_dir);
6830 VIM_CLEAR(globaldir);
Bram Moolenaarea408852005-06-25 22:49:46 +00006831}
6832#endif
6833
Bram Moolenaarf4258302013-06-02 18:20:17 +02006834/*
Bram Moolenaar002bc792020-06-05 22:33:42 +02006835 * Get the previous directory for the given chdir scope.
6836 */
6837 static char_u *
6838get_prevdir(cdscope_T scope)
6839{
6840 if (scope == CDSCOPE_WINDOW)
6841 return curwin->w_prevdir;
6842 else if (scope == CDSCOPE_TABPAGE)
6843 return curtab->tp_prevdir;
6844 return prev_dir;
6845}
6846
6847/*
Bram Moolenaarf4258302013-06-02 18:20:17 +02006848 * Deal with the side effects of changing the current directory.
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006849 * When 'scope' is CDSCOPE_TABPAGE then this was after an ":tcd" command.
6850 * When 'scope' is CDSCOPE_WINDOW then this was after an ":lcd" command.
Bram Moolenaarf4258302013-06-02 18:20:17 +02006851 */
6852 void
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006853post_chdir(cdscope_T scope)
Bram Moolenaarf4258302013-06-02 18:20:17 +02006854{
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006855 if (scope != CDSCOPE_WINDOW)
Bram Moolenaar00aa0692019-04-27 20:37:57 +02006856 // Clear tab local directory for both :cd and :tcd
6857 VIM_CLEAR(curtab->tp_localdir);
Bram Moolenaard23a8232018-02-10 18:45:26 +01006858 VIM_CLEAR(curwin->w_localdir);
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006859 if (scope != CDSCOPE_GLOBAL)
Bram Moolenaarf4258302013-06-02 18:20:17 +02006860 {
Bram Moolenaar002bc792020-06-05 22:33:42 +02006861 char_u *pdir = get_prevdir(scope);
6862
6863 // If still in the global directory, need to remember current
6864 // directory as the global directory.
6865 if (globaldir == NULL && pdir != NULL)
6866 globaldir = vim_strsave(pdir);
6867
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006868 // Remember this local directory for the window.
Bram Moolenaarf4258302013-06-02 18:20:17 +02006869 if (mch_dirname(NameBuff, MAXPATHL) == OK)
Bram Moolenaar00aa0692019-04-27 20:37:57 +02006870 {
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006871 if (scope == CDSCOPE_TABPAGE)
Bram Moolenaar00aa0692019-04-27 20:37:57 +02006872 curtab->tp_localdir = vim_strsave(NameBuff);
6873 else
6874 curwin->w_localdir = vim_strsave(NameBuff);
6875 }
Bram Moolenaarf4258302013-06-02 18:20:17 +02006876 }
6877 else
6878 {
Bram Moolenaar002bc792020-06-05 22:33:42 +02006879 // We are now in the global directory, no need to remember its name.
Bram Moolenaard23a8232018-02-10 18:45:26 +01006880 VIM_CLEAR(globaldir);
Bram Moolenaarf4258302013-06-02 18:20:17 +02006881 }
6882
6883 shorten_fnames(TRUE);
6884}
6885
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006886/*
6887 * Change directory function used by :cd/:tcd/:lcd Ex commands and the
Bram Moolenaar95058722020-06-01 16:26:19 +02006888 * chdir() function.
6889 * scope == CDSCOPE_WINDOW: changes the window-local directory
6890 * scope == CDSCOPE_TABPAGE: changes the tab-local directory
6891 * Otherwise: changes the global directory
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006892 * Returns TRUE if the directory is successfully changed.
6893 */
6894 int
6895changedir_func(
6896 char_u *new_dir,
6897 int forceit,
6898 cdscope_T scope)
6899{
6900 char_u *tofree;
Bram Moolenaar002bc792020-06-05 22:33:42 +02006901 char_u *pdir = NULL;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006902 int dir_differs;
6903 int retval = FALSE;
6904
Bram Moolenaar7cc96922020-01-31 22:41:38 +01006905 if (new_dir == NULL || allbuf_locked())
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006906 return FALSE;
6907
6908 if (vim_strchr(p_cpo, CPO_CHDIR) != NULL && curbufIsChanged() && !forceit)
6909 {
6910 emsg(_("E747: Cannot change directory, buffer is modified (add ! to override)"));
6911 return FALSE;
6912 }
6913
6914 // ":cd -": Change to previous directory
6915 if (STRCMP(new_dir, "-") == 0)
6916 {
Bram Moolenaar002bc792020-06-05 22:33:42 +02006917 pdir = get_prevdir(scope);
6918 if (pdir == NULL)
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006919 {
6920 emsg(_("E186: No previous directory"));
6921 return FALSE;
6922 }
Bram Moolenaar002bc792020-06-05 22:33:42 +02006923 new_dir = pdir;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006924 }
6925
Bram Moolenaar002bc792020-06-05 22:33:42 +02006926 // Free the previous directory
6927 tofree = get_prevdir(scope);
6928
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006929 // Save current directory for next ":cd -"
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006930 if (mch_dirname(NameBuff, MAXPATHL) == OK)
Bram Moolenaar002bc792020-06-05 22:33:42 +02006931 pdir = vim_strsave(NameBuff);
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006932 else
Bram Moolenaar002bc792020-06-05 22:33:42 +02006933 pdir = NULL;
6934 if (scope == CDSCOPE_WINDOW)
6935 curwin->w_prevdir = pdir;
6936 else if (scope == CDSCOPE_TABPAGE)
6937 curtab->tp_prevdir = pdir;
6938 else
6939 prev_dir = pdir;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006940
6941#if defined(UNIX) || defined(VMS)
6942 // for UNIX ":cd" means: go to home directory
6943 if (*new_dir == NUL)
6944 {
6945 // use NameBuff for home directory name
6946# ifdef VMS
6947 char_u *p;
6948
6949 p = mch_getenv((char_u *)"SYS$LOGIN");
6950 if (p == NULL || *p == NUL) // empty is the same as not set
6951 NameBuff[0] = NUL;
6952 else
6953 vim_strncpy(NameBuff, p, MAXPATHL - 1);
6954# else
6955 expand_env((char_u *)"$HOME", NameBuff, MAXPATHL);
6956# endif
6957 new_dir = NameBuff;
6958 }
6959#endif
Bram Moolenaar002bc792020-06-05 22:33:42 +02006960 dir_differs = new_dir == NULL || pdir == NULL
6961 || pathcmp((char *)pdir, (char *)new_dir, -1) != 0;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006962 if (new_dir == NULL || (dir_differs && vim_chdir(new_dir)))
6963 emsg(_(e_failed));
6964 else
6965 {
6966 char_u *acmd_fname;
6967
6968 post_chdir(scope);
6969
6970 if (dir_differs)
6971 {
6972 if (scope == CDSCOPE_WINDOW)
6973 acmd_fname = (char_u *)"window";
6974 else if (scope == CDSCOPE_TABPAGE)
6975 acmd_fname = (char_u *)"tabpage";
6976 else
6977 acmd_fname = (char_u *)"global";
6978 apply_autocmds(EVENT_DIRCHANGED, acmd_fname, new_dir, FALSE,
6979 curbuf);
6980 }
6981 retval = TRUE;
6982 }
6983 vim_free(tofree);
6984
6985 return retval;
6986}
Bram Moolenaarea408852005-06-25 22:49:46 +00006987
Bram Moolenaar071d4272004-06-13 20:20:40 +00006988/*
Bram Moolenaar00aa0692019-04-27 20:37:57 +02006989 * ":cd", ":tcd", ":lcd", ":chdir" ":tchdir" and ":lchdir".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006990 */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00006991 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006992ex_cd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006993{
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01006994 char_u *new_dir;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006995
6996 new_dir = eap->arg;
6997#if !defined(UNIX) && !defined(VMS)
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006998 // for non-UNIX ":cd" means: print current directory
Bram Moolenaar071d4272004-06-13 20:20:40 +00006999 if (*new_dir == NUL)
7000 ex_pwd(NULL);
7001 else
7002#endif
7003 {
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007004 cdscope_T scope = CDSCOPE_GLOBAL;
7005
7006 if (eap->cmdidx == CMD_lcd || eap->cmdidx == CMD_lchdir)
7007 scope = CDSCOPE_WINDOW;
7008 else if (eap->cmdidx == CMD_tcd || eap->cmdidx == CMD_tchdir)
7009 scope = CDSCOPE_TABPAGE;
7010
7011 if (changedir_func(new_dir, eap->forceit, scope))
Bram Moolenaardf177f62005-02-22 08:39:57 +00007012 {
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007013 // Echo the new current directory if the command was typed.
Bram Moolenaarfcfbc672009-07-09 18:13:49 +00007014 if (KeyTyped || p_verbose >= 5)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007015 ex_pwd(eap);
7016 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007017 }
7018}
7019
7020/*
7021 * ":pwd".
7022 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007023 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007024ex_pwd(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007025{
7026 if (mch_dirname(NameBuff, MAXPATHL) == OK)
7027 {
7028#ifdef BACKSLASH_IN_FILENAME
7029 slash_adjust(NameBuff);
7030#endif
Bram Moolenaar95058722020-06-01 16:26:19 +02007031 if (p_verbose > 0)
7032 {
7033 char *context = "global";
7034
7035 if (curwin->w_localdir != NULL)
7036 context = "window";
7037 else if (curtab->tp_localdir != NULL)
7038 context = "tabpage";
7039 smsg("[%s] %s", context, (char *)NameBuff);
7040 }
7041 else
7042 msg((char *)NameBuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007043 }
7044 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007045 emsg(_("E187: Unknown"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007046}
7047
7048/*
7049 * ":=".
7050 */
7051 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007052ex_equal(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007053{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007054 smsg("%ld", (long)eap->line2);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007055 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007056}
7057
7058 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007059ex_sleep(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007060{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007061 int n;
7062 long len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007063
7064 if (cursor_valid())
7065 {
7066 n = W_WINROW(curwin) + curwin->w_wrow - msg_scrolled;
7067 if (n >= 0)
Bram Moolenaar53f81742017-09-22 14:35:51 +02007068 windgoto((int)n, curwin->w_wincol + curwin->w_wcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007069 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007070
7071 len = eap->line2;
7072 switch (*eap->arg)
7073 {
7074 case 'm': break;
7075 case NUL: len *= 1000L; break;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007076 default: semsg(_(e_invarg2), eap->arg); return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007077 }
7078 do_sleep(len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007079}
7080
7081/*
7082 * Sleep for "msec" milliseconds, but keep checking for a CTRL-C every second.
7083 */
7084 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007085do_sleep(long msec)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007086{
Bram Moolenaar52953192019-08-16 10:27:13 +02007087 long done = 0;
Bram Moolenaar975b5272016-03-15 23:10:59 +01007088 long wait_now;
Bram Moolenaar52953192019-08-16 10:27:13 +02007089# ifdef ELAPSED_FUNC
7090 elapsed_T start_tv;
7091
7092 // Remember at what time we started, so that we know how much longer we
7093 // should wait after waiting for a bit.
7094 ELAPSED_INIT(start_tv);
7095# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007096
7097 cursor_on();
Bram Moolenaarf45d7472018-09-30 18:22:26 +02007098 out_flush_cursor(FALSE, FALSE);
Bram Moolenaar52953192019-08-16 10:27:13 +02007099 while (!got_int && done < msec)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007100 {
Bram Moolenaar975b5272016-03-15 23:10:59 +01007101 wait_now = msec - done > 1000L ? 1000L : msec - done;
7102#ifdef FEAT_TIMERS
7103 {
7104 long due_time = check_due_timer();
7105
7106 if (due_time > 0 && due_time < wait_now)
7107 wait_now = due_time;
7108 }
7109#endif
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02007110#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar28e67e02019-08-15 23:05:49 +02007111 if (has_any_channel() && wait_now > 20L)
7112 wait_now = 20L;
7113#endif
7114#ifdef FEAT_SOUND
7115 if (has_any_sound_callback() && wait_now > 20L)
7116 wait_now = 20L;
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02007117#endif
Bram Moolenaar975b5272016-03-15 23:10:59 +01007118 ui_delay(wait_now, TRUE);
Bram Moolenaar52953192019-08-16 10:27:13 +02007119
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02007120#ifdef FEAT_JOB_CHANNEL
7121 if (has_any_channel())
7122 ui_breakcheck_force(TRUE);
7123 else
7124#endif
7125 ui_breakcheck();
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007126#ifdef MESSAGE_QUEUE
Bram Moolenaar52953192019-08-16 10:27:13 +02007127 // Process the netbeans and clientserver messages that may have been
7128 // received in the call to ui_breakcheck() when the GUI is in use. This
7129 // may occur when running a test case.
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007130 parse_queued_messages();
Bram Moolenaare3cc6d42011-10-20 21:58:34 +02007131#endif
Bram Moolenaar52953192019-08-16 10:27:13 +02007132
7133# ifdef ELAPSED_FUNC
7134 // actual time passed
7135 done = ELAPSED_FUNC(start_tv);
7136# else
7137 // guestimate time passed (will actually be more)
7138 done += wait_now;
7139# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007140 }
Bram Moolenaar1ebff3d2018-07-07 16:18:13 +02007141
7142 // If CTRL-C was typed to interrupt the sleep, drop the CTRL-C from the
7143 // input buffer, otherwise a following call to input() fails.
7144 if (got_int)
7145 (void)vpeekc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007146}
7147
Bram Moolenaar071d4272004-06-13 20:20:40 +00007148/*
7149 * ":winsize" command (obsolete).
7150 */
7151 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007152ex_winsize(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007153{
7154 int w, h;
7155 char_u *arg = eap->arg;
7156 char_u *p;
7157
7158 w = getdigits(&arg);
7159 arg = skipwhite(arg);
7160 p = arg;
7161 h = getdigits(&arg);
7162 if (*p != NUL && *arg == NUL)
7163 set_shellsize(w, h, TRUE);
7164 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007165 emsg(_("E465: :winsize requires two number arguments"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007166}
7167
Bram Moolenaar071d4272004-06-13 20:20:40 +00007168 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007169ex_wincmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007170{
7171 int xchar = NUL;
7172 char_u *p;
7173
7174 if (*eap->arg == 'g' || *eap->arg == Ctrl_G)
7175 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007176 // CTRL-W g and CTRL-W CTRL-G have an extra command character
Bram Moolenaar071d4272004-06-13 20:20:40 +00007177 if (eap->arg[1] == NUL)
7178 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007179 emsg(_(e_invarg));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007180 return;
7181 }
7182 xchar = eap->arg[1];
7183 p = eap->arg + 2;
7184 }
7185 else
7186 p = eap->arg + 1;
7187
7188 eap->nextcmd = check_nextcmd(p);
7189 p = skipwhite(p);
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02007190 if (*p != NUL && *p != (
7191#ifdef FEAT_EVAL
7192 in_vim9script() ? '#' :
7193#endif
7194 '"')
7195 && eap->nextcmd == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007196 emsg(_(e_invarg));
Bram Moolenaar12bde492011-06-13 01:19:56 +02007197 else if (!eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007198 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007199 // Pass flags on for ":vertical wincmd ]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00007200 postponed_split_flags = cmdmod.split;
Bram Moolenaard326ce82007-03-11 14:48:29 +00007201 postponed_split_tab = cmdmod.tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007202 do_window(*eap->arg, eap->addr_count > 0 ? eap->line2 : 0L, xchar);
7203 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00007204 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007205 }
7206}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007207
Bram Moolenaar843ee412004-06-30 16:16:41 +00007208#if defined(FEAT_GUI) || defined(UNIX) || defined(VMS) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007209/*
7210 * ":winpos".
7211 */
7212 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007213ex_winpos(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007214{
7215 int x, y;
7216 char_u *arg = eap->arg;
7217 char_u *p;
7218
7219 if (*arg == NUL)
7220 {
Bram Moolenaar843ee412004-06-30 16:16:41 +00007221# if defined(FEAT_GUI) || defined(MSWIN)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007222# ifdef VIMDLL
7223 if (gui.in_use ? gui_mch_get_winpos(&x, &y) != FAIL :
7224 mch_get_winpos(&x, &y) != FAIL)
7225# elif defined(FEAT_GUI)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007226 if (gui.in_use && gui_mch_get_winpos(&x, &y) != FAIL)
Bram Moolenaar843ee412004-06-30 16:16:41 +00007227# else
7228 if (mch_get_winpos(&x, &y) != FAIL)
7229# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007230 {
7231 sprintf((char *)IObuff, _("Window position: X %d, Y %d"), x, y);
Bram Moolenaar32526b32019-01-19 17:43:09 +01007232 msg((char *)IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007233 }
7234 else
7235# endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007236 emsg(_("E188: Obtaining window position not implemented for this platform"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007237 }
7238 else
7239 {
7240 x = getdigits(&arg);
7241 arg = skipwhite(arg);
7242 p = arg;
7243 y = getdigits(&arg);
7244 if (*p == NUL || *arg != NUL)
7245 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007246 emsg(_("E466: :winpos requires two number arguments"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007247 return;
7248 }
7249# ifdef FEAT_GUI
7250 if (gui.in_use)
7251 gui_mch_set_winpos(x, y);
7252 else if (gui.starting)
7253 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007254 // Remember the coordinates for when the window is opened.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007255 gui_win_x = x;
7256 gui_win_y = y;
7257 }
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007258# if defined(HAVE_TGETENT) || defined(VIMDLL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007259 else
7260# endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007261# endif
7262# if defined(MSWIN) && (!defined(FEAT_GUI) || defined(VIMDLL))
Bram Moolenaar843ee412004-06-30 16:16:41 +00007263 mch_set_winpos(x, y);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007264# endif
7265# ifdef HAVE_TGETENT
7266 if (*T_CWP)
7267 term_set_winpos(x, y);
7268# endif
7269 }
7270}
7271#endif
7272
7273/*
7274 * Handle command that work like operators: ":delete", ":yank", ":>" and ":<".
7275 */
7276 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007277ex_operators(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007278{
7279 oparg_T oa;
7280
7281 clear_oparg(&oa);
7282 oa.regname = eap->regname;
7283 oa.start.lnum = eap->line1;
7284 oa.end.lnum = eap->line2;
7285 oa.line_count = eap->line2 - eap->line1 + 1;
7286 oa.motion_type = MLINE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007287 virtual_op = FALSE;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007288 if (eap->cmdidx != CMD_yank) // position cursor for undo
Bram Moolenaar071d4272004-06-13 20:20:40 +00007289 {
7290 setpcmark();
7291 curwin->w_cursor.lnum = eap->line1;
7292 beginline(BL_SOL | BL_FIX);
7293 }
7294
Bram Moolenaard07c6e12013-11-21 14:21:40 +01007295 if (VIsual_active)
7296 end_visual_mode();
Bram Moolenaard07c6e12013-11-21 14:21:40 +01007297
Bram Moolenaar071d4272004-06-13 20:20:40 +00007298 switch (eap->cmdidx)
7299 {
7300 case CMD_delete:
7301 oa.op_type = OP_DELETE;
7302 op_delete(&oa);
7303 break;
7304
7305 case CMD_yank:
7306 oa.op_type = OP_YANK;
7307 (void)op_yank(&oa, FALSE, TRUE);
7308 break;
7309
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007310 default: // CMD_rshift or CMD_lshift
Bram Moolenaar6e707362013-06-14 19:15:58 +02007311 if (
Bram Moolenaar071d4272004-06-13 20:20:40 +00007312#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6e707362013-06-14 19:15:58 +02007313 (eap->cmdidx == CMD_rshift) ^ curwin->w_p_rl
7314#else
7315 eap->cmdidx == CMD_rshift
Bram Moolenaar071d4272004-06-13 20:20:40 +00007316#endif
Bram Moolenaar6e707362013-06-14 19:15:58 +02007317 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00007318 oa.op_type = OP_RSHIFT;
7319 else
7320 oa.op_type = OP_LSHIFT;
7321 op_shift(&oa, FALSE, eap->amount);
7322 break;
7323 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007324 virtual_op = MAYBE;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007325 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007326}
7327
7328/*
7329 * ":put".
7330 */
7331 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007332ex_put(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007333{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007334 // ":0put" works like ":1put!".
Bram Moolenaar071d4272004-06-13 20:20:40 +00007335 if (eap->line2 == 0)
7336 {
7337 eap->line2 = 1;
7338 eap->forceit = TRUE;
7339 }
7340 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaarc3516f72020-09-08 22:45:35 +02007341 do_put(eap->regname, NULL, eap->forceit ? BACKWARD : FORWARD, 1L,
Bram Moolenaardf177f62005-02-22 08:39:57 +00007342 PUT_LINE|PUT_CURSLINE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007343}
7344
7345/*
7346 * Handle ":copy" and ":move".
7347 */
7348 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007349ex_copymove(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007350{
7351 long n;
7352
Bram Moolenaar491799b2020-08-01 19:23:43 +02007353#ifdef FEAT_EVAL
Bram Moolenaarf5a48012020-08-01 17:00:03 +02007354 if (not_in_vim9(eap) == FAIL)
7355 return;
Bram Moolenaar491799b2020-08-01 19:23:43 +02007356#endif
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02007357 n = get_address(eap, &eap->arg, eap->addr_type, FALSE, FALSE, FALSE, 1);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007358 if (eap->arg == NULL) // error detected
Bram Moolenaar071d4272004-06-13 20:20:40 +00007359 {
7360 eap->nextcmd = NULL;
7361 return;
7362 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00007363 get_flags(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007364
7365 /*
7366 * move or copy lines from 'eap->line1'-'eap->line2' to below line 'n'
7367 */
7368 if (n == MAXLNUM || n < 0 || n > curbuf->b_ml.ml_line_count)
7369 {
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02007370 emsg(_(e_invrange));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007371 return;
7372 }
7373
7374 if (eap->cmdidx == CMD_move)
7375 {
7376 if (do_move(eap->line1, eap->line2, n) == FAIL)
7377 return;
7378 }
7379 else
7380 ex_copy(eap->line1, eap->line2, n);
7381 u_clearline();
7382 beginline(BL_SOL | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007383 ex_may_print(eap);
7384}
7385
7386/*
7387 * Print the current line if flags were given to the Ex command.
7388 */
Bram Moolenaarfd3fe982014-04-01 17:49:44 +02007389 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007390ex_may_print(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007391{
7392 if (eap->flags != 0)
7393 {
7394 print_line(curwin->w_cursor.lnum, (eap->flags & EXFLAG_NR),
7395 (eap->flags & EXFLAG_LIST));
7396 ex_no_reprint = TRUE;
7397 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007398}
7399
7400/*
7401 * ":smagic" and ":snomagic".
7402 */
7403 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007404ex_submagic(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007405{
7406 int magic_save = p_magic;
7407
7408 p_magic = (eap->cmdidx == CMD_smagic);
Bram Moolenaar66e00142020-08-12 21:58:12 +02007409 ex_substitute(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007410 p_magic = magic_save;
7411}
7412
7413/*
7414 * ":join".
7415 */
7416 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007417ex_join(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007418{
7419 curwin->w_cursor.lnum = eap->line1;
7420 if (eap->line1 == eap->line2)
7421 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007422 if (eap->addr_count >= 2) // :2,2join does nothing
Bram Moolenaar071d4272004-06-13 20:20:40 +00007423 return;
7424 if (eap->line2 == curbuf->b_ml.ml_line_count)
7425 {
7426 beep_flush();
7427 return;
7428 }
7429 ++eap->line2;
7430 }
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02007431 (void)do_join(eap->line2 - eap->line1 + 1, !eap->forceit, TRUE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007432 beginline(BL_WHITE | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007433 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007434}
7435
7436/*
7437 * ":[addr]@r" or ":[addr]*r": execute register
7438 */
7439 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007440ex_at(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007441{
7442 int c;
Bram Moolenaar60462872009-11-03 11:40:19 +00007443 int prev_len = typebuf.tb_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007444
7445 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaar4930a762016-09-11 14:39:53 +02007446 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007447
7448#ifdef USE_ON_FLY_SCROLL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007449 dont_scroll = TRUE; // disallow scrolling here
Bram Moolenaar071d4272004-06-13 20:20:40 +00007450#endif
7451
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007452 // get the register name. No name means to use the previous one
Bram Moolenaar071d4272004-06-13 20:20:40 +00007453 c = *eap->arg;
7454 if (c == NUL || (c == '*' && *eap->cmd == '*'))
7455 c = '@';
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007456 // Put the register in the typeahead buffer with the "silent" flag.
Bram Moolenaard333d1e2006-11-07 17:43:47 +00007457 if (do_execreg(c, TRUE, vim_strchr(p_cpo, CPO_EXECBUF) != NULL, TRUE)
7458 == FAIL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007459 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007460 beep_flush();
Bram Moolenaardf177f62005-02-22 08:39:57 +00007461 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007462 else
7463 {
7464 int save_efr = exec_from_reg;
7465
7466 exec_from_reg = TRUE;
7467
7468 /*
7469 * Execute from the typeahead buffer.
Bram Moolenaar60462872009-11-03 11:40:19 +00007470 * Continue until the stuff buffer is empty and all added characters
7471 * have been consumed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007472 */
Bram Moolenaar60462872009-11-03 11:40:19 +00007473 while (!stuff_empty() || typebuf.tb_len > prev_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007474 (void)do_cmdline(NULL, getexline, NULL, DOCMD_NOWAIT|DOCMD_VERBOSE);
7475
7476 exec_from_reg = save_efr;
7477 }
7478}
7479
7480/*
7481 * ":!".
7482 */
7483 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007484ex_bang(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007485{
7486 do_bang(eap->addr_count, eap, eap->forceit, TRUE, TRUE);
7487}
7488
7489/*
7490 * ":undo".
7491 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007492 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +01007493ex_undo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007494{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007495 if (eap->addr_count == 1) // :undo 123
Bram Moolenaar730cde92010-06-27 05:18:54 +02007496 undo_time(eap->line2, FALSE, FALSE, TRUE);
Bram Moolenaard3667a22006-03-16 21:35:52 +00007497 else
7498 u_undo(1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007499}
7500
Bram Moolenaar55debbe2010-05-23 23:34:36 +02007501#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar6df6f472010-07-18 18:04:50 +02007502 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007503ex_wundo(exarg_T *eap)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02007504{
7505 char_u hash[UNDO_HASH_SIZE];
7506
7507 u_compute_hash(hash);
7508 u_write_undo(eap->arg, eap->forceit, curbuf, hash);
7509}
7510
Bram Moolenaar6df6f472010-07-18 18:04:50 +02007511 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007512ex_rundo(exarg_T *eap)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02007513{
7514 char_u hash[UNDO_HASH_SIZE];
7515
7516 u_compute_hash(hash);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02007517 u_read_undo(eap->arg, hash, NULL);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02007518}
7519#endif
7520
Bram Moolenaar071d4272004-06-13 20:20:40 +00007521/*
7522 * ":redo".
7523 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007524 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007525ex_redo(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007526{
7527 u_redo(1);
7528}
7529
7530/*
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007531 * ":earlier" and ":later".
7532 */
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007533 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007534ex_later(exarg_T *eap)
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007535{
7536 long count = 0;
7537 int sec = FALSE;
Bram Moolenaar730cde92010-06-27 05:18:54 +02007538 int file = FALSE;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007539 char_u *p = eap->arg;
7540
7541 if (*p == NUL)
7542 count = 1;
7543 else if (isdigit(*p))
7544 {
7545 count = getdigits(&p);
7546 switch (*p)
7547 {
7548 case 's': ++p; sec = TRUE; break;
7549 case 'm': ++p; sec = TRUE; count *= 60; break;
7550 case 'h': ++p; sec = TRUE; count *= 60 * 60; break;
Bram Moolenaar730cde92010-06-27 05:18:54 +02007551 case 'd': ++p; sec = TRUE; count *= 24 * 60 * 60; break;
7552 case 'f': ++p; file = TRUE; break;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007553 }
7554 }
7555
7556 if (*p != NUL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007557 semsg(_(e_invarg2), eap->arg);
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007558 else
Bram Moolenaar730cde92010-06-27 05:18:54 +02007559 undo_time(eap->cmdidx == CMD_earlier ? -count : count,
7560 sec, file, FALSE);
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007561}
7562
7563/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007564 * ":redir": start/stop redirection.
7565 */
7566 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007567ex_redir(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007568{
7569 char *mode;
7570 char_u *fname;
Bram Moolenaarca472992005-01-21 11:46:23 +00007571 char_u *arg = eap->arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007572
Bram Moolenaarba768492016-07-08 15:32:54 +02007573#ifdef FEAT_EVAL
Bram Moolenaar79815f12016-07-09 17:07:29 +02007574 if (redir_execute)
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02007575 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007576 emsg(_("E930: Cannot use :redir inside execute()"));
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02007577 return;
7578 }
Bram Moolenaarba768492016-07-08 15:32:54 +02007579#endif
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02007580
Bram Moolenaar071d4272004-06-13 20:20:40 +00007581 if (STRICMP(eap->arg, "END") == 0)
7582 close_redir();
7583 else
7584 {
Bram Moolenaarca472992005-01-21 11:46:23 +00007585 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00007586 {
Bram Moolenaarca472992005-01-21 11:46:23 +00007587 ++arg;
7588 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00007589 {
Bram Moolenaarca472992005-01-21 11:46:23 +00007590 ++arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007591 mode = "a";
7592 }
7593 else
7594 mode = "w";
Bram Moolenaarca472992005-01-21 11:46:23 +00007595 arg = skipwhite(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007596
7597 close_redir();
7598
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007599 // Expand environment variables and "~/".
Bram Moolenaarca472992005-01-21 11:46:23 +00007600 fname = expand_env_save(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007601 if (fname == NULL)
7602 return;
7603#ifdef FEAT_BROWSE
7604 if (cmdmod.browse)
7605 {
7606 char_u *browseFile;
7607
Bram Moolenaar7171abe2004-10-11 10:06:20 +00007608 browseFile = do_browse(BROWSE_SAVE,
7609 (char_u *)_("Save Redirection"),
Bram Moolenaarc36651b2018-04-29 12:22:56 +02007610 fname, NULL, NULL,
7611 (char_u *)_(BROWSE_FILTER_ALL_FILES), curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007612 if (browseFile == NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007613 return; // operation cancelled
Bram Moolenaar071d4272004-06-13 20:20:40 +00007614 vim_free(fname);
7615 fname = browseFile;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007616 eap->forceit = TRUE; // since dialog already asked
Bram Moolenaar071d4272004-06-13 20:20:40 +00007617 }
7618#endif
7619
7620 redir_fd = open_exfile(fname, eap->forceit, mode);
7621 vim_free(fname);
7622 }
7623#ifdef FEAT_EVAL
Bram Moolenaarca472992005-01-21 11:46:23 +00007624 else if (*arg == '@')
Bram Moolenaar071d4272004-06-13 20:20:40 +00007625 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007626 // redirect to a register a-z (resp. A-Z for appending)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007627 close_redir();
Bram Moolenaarca472992005-01-21 11:46:23 +00007628 ++arg;
7629 if (ASCII_ISALPHA(*arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007630# ifdef FEAT_CLIPBOARD
Bram Moolenaarca472992005-01-21 11:46:23 +00007631 || *arg == '*'
Bram Moolenaar9a51c6e2006-11-14 19:25:02 +00007632 || *arg == '+'
Bram Moolenaar071d4272004-06-13 20:20:40 +00007633# endif
Bram Moolenaarca472992005-01-21 11:46:23 +00007634 || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00007635 {
Bram Moolenaarca472992005-01-21 11:46:23 +00007636 redir_reg = *arg++;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007637 if (*arg == '>' && arg[1] == '>') // append
Bram Moolenaard9d30582005-05-18 22:10:28 +00007638 arg += 2;
Bram Moolenaarc188b882007-10-19 14:20:54 +00007639 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007640 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007641 // Can use both "@a" and "@a>".
Bram Moolenaar2c29bee2005-06-01 21:46:07 +00007642 if (*arg == '>')
7643 arg++;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007644 // Make register empty when not using @A-@Z and the
7645 // command is valid.
Bram Moolenaarc188b882007-10-19 14:20:54 +00007646 if (*arg == NUL && !isupper(redir_reg))
7647 write_reg_contents(redir_reg, (char_u *)"", -1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007648 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00007649 }
7650 if (*arg != NUL)
7651 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00007652 redir_reg = 0;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007653 semsg(_(e_invarg2), eap->arg);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007654 }
7655 }
7656 else if (*arg == '=' && arg[1] == '>')
7657 {
7658 int append;
7659
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007660 // redirect to a variable
Bram Moolenaardf177f62005-02-22 08:39:57 +00007661 close_redir();
7662 arg += 2;
7663
7664 if (*arg == '>')
7665 {
7666 ++arg;
7667 append = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007668 }
7669 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00007670 append = FALSE;
7671
7672 if (var_redir_start(skipwhite(arg), append) == OK)
7673 redir_vname = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007674 }
7675#endif
7676
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007677 // TODO: redirect to a buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00007678
Bram Moolenaar071d4272004-06-13 20:20:40 +00007679 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007680 semsg(_(e_invarg2), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007681 }
Bram Moolenaar29b2d262006-09-10 19:07:28 +00007682
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007683 // Make sure redirection is not off. Can happen for cmdline completion
7684 // that indirectly invokes a command to catch its output.
Bram Moolenaar29b2d262006-09-10 19:07:28 +00007685 if (redir_fd != NULL
7686#ifdef FEAT_EVAL
7687 || redir_reg || redir_vname
7688#endif
7689 )
7690 redir_off = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007691}
7692
7693/*
7694 * ":redraw": force redraw
7695 */
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01007696 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007697ex_redraw(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007698{
7699 int r = RedrawingDisabled;
7700 int p = p_lz;
7701
7702 RedrawingDisabled = 0;
7703 p_lz = FALSE;
Bram Moolenaarabc39ab2017-03-01 18:04:05 +01007704 validate_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007705 update_topline();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007706 update_screen(eap->forceit ? CLEAR : VIsual_active ? INVERTED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007707#ifdef FEAT_TITLE
7708 if (need_maketitle)
7709 maketitle();
7710#endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007711#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
7712# ifdef VIMDLL
7713 if (!gui.in_use)
7714# endif
7715 resize_console_buf();
Bram Moolenaar78d21da2019-02-17 15:00:52 +01007716#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007717 RedrawingDisabled = r;
7718 p_lz = p;
7719
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007720 // Reset msg_didout, so that a message that's there is overwritten.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007721 msg_didout = FALSE;
7722 msg_col = 0;
7723
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007724 // No need to wait after an intentional redraw.
Bram Moolenaar56667a52013-07-17 11:54:28 +02007725 need_wait_return = FALSE;
7726
Bram Moolenaar071d4272004-06-13 20:20:40 +00007727 out_flush();
7728}
7729
7730/*
7731 * ":redrawstatus": force redraw of status line(s)
7732 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007733 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007734ex_redrawstatus(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007735{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007736 int r = RedrawingDisabled;
7737 int p = p_lz;
7738
7739 RedrawingDisabled = 0;
7740 p_lz = FALSE;
7741 if (eap->forceit)
7742 status_redraw_all();
7743 else
7744 status_redraw_curbuf();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007745 update_screen(VIsual_active ? INVERTED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007746 RedrawingDisabled = r;
7747 p_lz = p;
7748 out_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007749}
7750
Bram Moolenaare12bab32019-01-08 22:02:56 +01007751/*
7752 * ":redrawtabline": force redraw of the tabline
7753 */
7754 static void
7755ex_redrawtabline(exarg_T *eap UNUSED)
7756{
7757 int r = RedrawingDisabled;
7758 int p = p_lz;
7759
7760 RedrawingDisabled = 0;
7761 p_lz = FALSE;
7762
7763 draw_tabline();
7764
7765 RedrawingDisabled = r;
7766 p_lz = p;
7767 out_flush();
7768}
7769
Bram Moolenaar071d4272004-06-13 20:20:40 +00007770 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007771close_redir(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007772{
7773 if (redir_fd != NULL)
7774 {
7775 fclose(redir_fd);
7776 redir_fd = NULL;
7777 }
7778#ifdef FEAT_EVAL
7779 redir_reg = 0;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007780 if (redir_vname)
7781 {
7782 var_redir_stop();
7783 redir_vname = 0;
7784 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007785#endif
7786}
7787
Bram Moolenaarf96ae0b2019-07-28 15:21:55 +02007788#if (defined(FEAT_SESSION) || defined(FEAT_EVAL)) || defined(PROTO)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007789 int
Bram Moolenaarbd67aac2019-09-21 23:09:04 +02007790vim_mkdir_emsg(char_u *name, int prot UNUSED)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007791{
7792 if (vim_mkdir(name, prot) != 0)
7793 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007794 semsg(_("E739: Cannot create directory: %s"), name);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007795 return FAIL;
7796 }
7797 return OK;
7798}
7799#endif
7800
Bram Moolenaar071d4272004-06-13 20:20:40 +00007801/*
7802 * Open a file for writing for an Ex command, with some checks.
7803 * Return file descriptor, or NULL on failure.
7804 */
7805 FILE *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007806open_exfile(
7807 char_u *fname,
7808 int forceit,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007809 char *mode) // "w" for create new file or "a" for append
Bram Moolenaar071d4272004-06-13 20:20:40 +00007810{
7811 FILE *fd;
7812
7813#ifdef UNIX
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007814 // with Unix it is possible to open a directory
Bram Moolenaar071d4272004-06-13 20:20:40 +00007815 if (mch_isdir(fname))
7816 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007817 semsg(_(e_isadir2), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007818 return NULL;
7819 }
7820#endif
7821 if (!forceit && *mode != 'a' && vim_fexists(fname))
7822 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007823 semsg(_("E189: \"%s\" exists (add ! to override)"), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007824 return NULL;
7825 }
7826
7827 if ((fd = mch_fopen((char *)fname, mode)) == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007828 semsg(_("E190: Cannot open \"%s\" for writing"), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007829
7830 return fd;
7831}
7832
7833/*
7834 * ":mark" and ":k".
7835 */
7836 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007837ex_mark(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007838{
7839 pos_T pos;
7840
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007841 if (*eap->arg == NUL) // No argument?
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007842 emsg(_(e_argreq));
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007843 else if (eap->arg[1] != NUL) // more than one character?
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +02007844 semsg(_(e_trailing_arg), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007845 else
7846 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007847 pos = curwin->w_cursor; // save curwin->w_cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00007848 curwin->w_cursor.lnum = eap->line2;
7849 beginline(BL_WHITE | BL_FIX);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007850 if (setmark(*eap->arg) == FAIL) // set mark
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007851 emsg(_("E191: Argument must be a letter or forward/backward quote"));
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007852 curwin->w_cursor = pos; // restore curwin->w_cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00007853 }
7854}
7855
7856/*
7857 * Update w_topline, w_leftcol and the cursor position.
7858 */
7859 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007860update_topline_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007861{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007862 check_cursor(); // put cursor on valid line
Bram Moolenaar071d4272004-06-13 20:20:40 +00007863 update_topline();
7864 if (!curwin->w_p_wrap)
7865 validate_cursor();
7866 update_curswant();
7867}
7868
Bram Moolenaar071d4272004-06-13 20:20:40 +00007869/*
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007870 * Save the current State and go to Normal mode.
7871 * Return TRUE if the typeahead could be saved.
7872 */
7873 int
7874save_current_state(save_state_T *sst)
7875{
7876 sst->save_msg_scroll = msg_scroll;
7877 sst->save_restart_edit = restart_edit;
7878 sst->save_msg_didout = msg_didout;
7879 sst->save_State = State;
7880 sst->save_insertmode = p_im;
7881 sst->save_finish_op = finish_op;
7882 sst->save_opcount = opcount;
Bram Moolenaarcce713d2019-03-04 11:40:12 +01007883 sst->save_reg_executing = reg_executing;
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007884
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007885 msg_scroll = FALSE; // no msg scrolling in Normal mode
7886 restart_edit = 0; // don't go to Insert mode
7887 p_im = FALSE; // don't use 'insertmode'
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007888
7889 /*
7890 * Save the current typeahead. This is required to allow using ":normal"
7891 * from an event handler and makes sure we don't hang when the argument
7892 * ends with half a command.
7893 */
7894 save_typeahead(&sst->tabuf);
7895 return sst->tabuf.typebuf_valid;
7896}
7897
7898 void
7899restore_current_state(save_state_T *sst)
7900{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007901 // Restore the previous typeahead.
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007902 restore_typeahead(&sst->tabuf);
7903
7904 msg_scroll = sst->save_msg_scroll;
7905 restart_edit = sst->save_restart_edit;
7906 p_im = sst->save_insertmode;
7907 finish_op = sst->save_finish_op;
7908 opcount = sst->save_opcount;
Bram Moolenaarcce713d2019-03-04 11:40:12 +01007909 reg_executing = sst->save_reg_executing;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007910 msg_didout |= sst->save_msg_didout; // don't reset msg_didout now
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007911
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007912 // Restore the state (needed when called from a function executed for
7913 // 'indentexpr'). Update the mouse and cursor, they may have changed.
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007914 State = sst->save_State;
7915#ifdef CURSOR_SHAPE
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007916 ui_cursor_shape(); // may show different cursor shape
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007917#endif
7918}
7919
7920/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007921 * ":normal[!] {commands}": Execute normal mode commands.
7922 */
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01007923 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007924ex_normal(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007925{
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007926 save_state_T save_state;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007927 char_u *arg = NULL;
7928 int l;
7929 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007930
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00007931 if (ex_normal_lock > 0)
7932 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007933 emsg(_(e_secure));
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00007934 return;
7935 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007936 if (ex_normal_busy >= p_mmd)
7937 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007938 emsg(_("E192: Recursive use of :normal too deep"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007939 return;
7940 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007941
Bram Moolenaar071d4272004-06-13 20:20:40 +00007942 /*
7943 * vgetc() expects a CSI and K_SPECIAL to have been escaped. Don't do
7944 * this for the K_SPECIAL leading byte, otherwise special keys will not
7945 * work.
7946 */
7947 if (has_mbyte)
7948 {
7949 int len = 0;
7950
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007951 // Count the number of characters to be escaped.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007952 for (p = eap->arg; *p != NUL; ++p)
7953 {
Bram Moolenaar13505972019-01-24 15:04:48 +01007954#ifdef FEAT_GUI
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007955 if (*p == CSI) // leadbyte CSI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007956 len += 2;
Bram Moolenaar13505972019-01-24 15:04:48 +01007957#endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007958 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007959 if (*++p == K_SPECIAL // trailbyte K_SPECIAL or CSI
Bram Moolenaar13505972019-01-24 15:04:48 +01007960#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007961 || *p == CSI
Bram Moolenaar13505972019-01-24 15:04:48 +01007962#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007963 )
7964 len += 2;
7965 }
7966 if (len > 0)
7967 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02007968 arg = alloc(STRLEN(eap->arg) + len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007969 if (arg != NULL)
7970 {
7971 len = 0;
7972 for (p = eap->arg; *p != NUL; ++p)
7973 {
7974 arg[len++] = *p;
Bram Moolenaar13505972019-01-24 15:04:48 +01007975#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007976 if (*p == CSI)
7977 {
7978 arg[len++] = KS_EXTRA;
7979 arg[len++] = (int)KE_CSI;
7980 }
Bram Moolenaar13505972019-01-24 15:04:48 +01007981#endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007982 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007983 {
7984 arg[len++] = *++p;
7985 if (*p == K_SPECIAL)
7986 {
7987 arg[len++] = KS_SPECIAL;
7988 arg[len++] = KE_FILLER;
7989 }
Bram Moolenaar13505972019-01-24 15:04:48 +01007990#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007991 else if (*p == CSI)
7992 {
7993 arg[len++] = KS_EXTRA;
7994 arg[len++] = (int)KE_CSI;
7995 }
Bram Moolenaar13505972019-01-24 15:04:48 +01007996#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007997 }
7998 arg[len] = NUL;
7999 }
8000 }
8001 }
8002 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008003
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008004 ++ex_normal_busy;
8005 if (save_current_state(&save_state))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008006 {
8007 /*
8008 * Repeat the :normal command for each line in the range. When no
8009 * range given, execute it just once, without positioning the cursor
8010 * first.
8011 */
8012 do
8013 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008014 if (eap->addr_count != 0)
8015 {
8016 curwin->w_cursor.lnum = eap->line1++;
8017 curwin->w_cursor.col = 0;
Bram Moolenaard5d37532017-03-27 23:02:07 +02008018 check_cursor_moved(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008019 }
8020
Bram Moolenaar13505972019-01-24 15:04:48 +01008021 exec_normal_cmd(arg != NULL
8022 ? arg
8023 : eap->arg, eap->forceit ? REMAP_NONE : REMAP_YES, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008024 }
8025 while (eap->addr_count > 0 && eap->line1 <= eap->line2 && !got_int);
8026 }
8027
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008028 // Might not return to the main loop when in an event handler.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008029 update_topline_cursor();
8030
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008031 restore_current_state(&save_state);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008032 --ex_normal_busy;
Bram Moolenaareda73602014-11-05 09:53:23 +01008033 setmouse();
Bram Moolenaareda73602014-11-05 09:53:23 +01008034#ifdef CURSOR_SHAPE
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008035 ui_cursor_shape(); // may show different cursor shape
Bram Moolenaareda73602014-11-05 09:53:23 +01008036#endif
8037
Bram Moolenaar071d4272004-06-13 20:20:40 +00008038 vim_free(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008039}
8040
8041/*
Bram Moolenaar64969662005-12-14 21:59:55 +00008042 * ":startinsert", ":startreplace" and ":startgreplace"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008043 */
8044 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008045ex_startinsert(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008046{
Bram Moolenaarfd371682005-01-14 21:42:54 +00008047 if (eap->forceit)
8048 {
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02008049 // cursor line can be zero on startup
Bram Moolenaar09ca9322017-09-26 17:40:45 +02008050 if (!curwin->w_cursor.lnum)
8051 curwin->w_cursor.lnum = 1;
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02008052 set_cursor_for_append_to_line();
Bram Moolenaarfd371682005-01-14 21:42:54 +00008053 }
Bram Moolenaareb58a242020-04-19 18:13:19 +02008054#ifdef FEAT_TERMINAL
8055 // Ignore this when running in an active terminal.
8056 if (term_job_running(curbuf->b_term))
8057 return;
8058#endif
Bram Moolenaarfd371682005-01-14 21:42:54 +00008059
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02008060 // Ignore the command when already in Insert mode. Inserting an
8061 // expression register that invokes a function can do this.
Bram Moolenaara40c5002005-01-09 21:16:21 +00008062 if (State & INSERT)
8063 return;
8064
Bram Moolenaar64969662005-12-14 21:59:55 +00008065 if (eap->cmdidx == CMD_startinsert)
8066 restart_edit = 'a';
8067 else if (eap->cmdidx == CMD_startreplace)
8068 restart_edit = 'R';
Bram Moolenaar071d4272004-06-13 20:20:40 +00008069 else
Bram Moolenaar64969662005-12-14 21:59:55 +00008070 restart_edit = 'V';
8071
8072 if (!eap->forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008073 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00008074 if (eap->cmdidx == CMD_startinsert)
8075 restart_edit = 'i';
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02008076 curwin->w_curswant = 0; // avoid MAXCOL
Bram Moolenaar071d4272004-06-13 20:20:40 +00008077 }
8078}
8079
8080/*
8081 * ":stopinsert"
8082 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008083 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008084ex_stopinsert(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008085{
8086 restart_edit = 0;
8087 stop_insert_mode = TRUE;
Bram Moolenaarfd773e92016-04-02 19:39:16 +02008088 clearmode();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008089}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008090
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008091/*
8092 * Execute normal mode command "cmd".
8093 * "remap" can be REMAP_NONE or REMAP_YES.
8094 */
8095 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008096exec_normal_cmd(char_u *cmd, int remap, int silent)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008097{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008098 // Stuff the argument into the typeahead buffer.
Bram Moolenaar25281632016-01-21 23:32:32 +01008099 ins_typebuf(cmd, remap, 0, TRUE, silent);
Bram Moolenaar586c70c2018-10-02 16:23:58 +02008100 exec_normal(FALSE, FALSE, FALSE);
Bram Moolenaar25281632016-01-21 23:32:32 +01008101}
Bram Moolenaar25281632016-01-21 23:32:32 +01008102
Bram Moolenaar25281632016-01-21 23:32:32 +01008103/*
8104 * Execute normal_cmd() until there is no typeahead left.
Bram Moolenaar586c70c2018-10-02 16:23:58 +02008105 * When "use_vpeekc" is TRUE use vpeekc() to check for available chars.
Bram Moolenaar25281632016-01-21 23:32:32 +01008106 */
8107 void
Bram Moolenaar586c70c2018-10-02 16:23:58 +02008108exec_normal(int was_typed, int use_vpeekc, int may_use_terminal_loop UNUSED)
Bram Moolenaar25281632016-01-21 23:32:32 +01008109{
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008110 oparg_T oa;
Bram Moolenaar905dd902019-04-07 14:21:47 +02008111 int c;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008112
Bram Moolenaar905dd902019-04-07 14:21:47 +02008113 // When calling vpeekc() from feedkeys() it will return Ctrl_C when there
8114 // is nothing to get, so also check for Ctrl_C.
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008115 clear_oparg(&oa);
8116 finish_op = FALSE;
Bram Moolenaar586c70c2018-10-02 16:23:58 +02008117 while ((!stuff_empty()
8118 || ((was_typed || !typebuf_typed()) && typebuf.tb_len > 0)
Bram Moolenaar905dd902019-04-07 14:21:47 +02008119 || (use_vpeekc && (c = vpeekc()) != NUL && c != Ctrl_C))
Bram Moolenaar586c70c2018-10-02 16:23:58 +02008120 && !got_int)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008121 {
8122 update_topline_cursor();
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02008123#ifdef FEAT_TERMINAL
Bram Moolenaar655a82a2018-05-08 22:01:07 +02008124 if (may_use_terminal_loop && term_use_loop()
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02008125 && oa.op_type == OP_NOP && oa.regname == NUL
8126 && !VIsual_active)
8127 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008128 // If terminal_loop() returns OK we got a key that is handled
8129 // in Normal model. With FAIL we first need to position the
8130 // cursor and the screen needs to be redrawn.
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02008131 if (terminal_loop(TRUE) == OK)
8132 normal_cmd(&oa, TRUE);
8133 }
8134 else
8135#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008136 // execute a Normal mode cmd
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02008137 normal_cmd(&oa, TRUE);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008138 }
8139}
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008140
Bram Moolenaar071d4272004-06-13 20:20:40 +00008141#ifdef FEAT_FIND_ID
8142 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008143ex_checkpath(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008144{
8145 find_pattern_in_path(NULL, 0, 0, FALSE, FALSE, CHECK_PATH, 1L,
8146 eap->forceit ? ACTION_SHOW_ALL : ACTION_SHOW,
8147 (linenr_T)1, (linenr_T)MAXLNUM);
8148}
8149
Bram Moolenaar4033c552017-09-16 20:54:51 +02008150#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008151/*
8152 * ":psearch"
8153 */
8154 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008155ex_psearch(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008156{
8157 g_do_tagpreview = p_pvh;
8158 ex_findpat(eap);
8159 g_do_tagpreview = 0;
8160}
8161#endif
8162
8163 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008164ex_findpat(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008165{
8166 int whole = TRUE;
8167 long n;
8168 char_u *p;
8169 int action;
8170
8171 switch (cmdnames[eap->cmdidx].cmd_name[2])
8172 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008173 case 'e': // ":psearch", ":isearch" and ":dsearch"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008174 if (cmdnames[eap->cmdidx].cmd_name[0] == 'p')
8175 action = ACTION_GOTO;
8176 else
8177 action = ACTION_SHOW;
8178 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008179 case 'i': // ":ilist" and ":dlist"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008180 action = ACTION_SHOW_ALL;
8181 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008182 case 'u': // ":ijump" and ":djump"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008183 action = ACTION_GOTO;
8184 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008185 default: // ":isplit" and ":dsplit"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008186 action = ACTION_SPLIT;
8187 break;
8188 }
8189
8190 n = 1;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008191 if (vim_isdigit(*eap->arg)) // get count
Bram Moolenaar071d4272004-06-13 20:20:40 +00008192 {
8193 n = getdigits(&eap->arg);
8194 eap->arg = skipwhite(eap->arg);
8195 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008196 if (*eap->arg == '/') // Match regexp, not just whole words
Bram Moolenaar071d4272004-06-13 20:20:40 +00008197 {
8198 whole = FALSE;
8199 ++eap->arg;
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02008200 p = skip_regexp(eap->arg, '/', p_magic);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008201 if (*p)
8202 {
8203 *p++ = NUL;
8204 p = skipwhite(p);
8205
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008206 // Check for trailing illegal characters
Bram Moolenaarfaac4102020-04-20 17:46:14 +02008207 if (!ends_excmd2(eap->arg, p))
Bram Moolenaar8930caa2020-07-23 16:37:03 +02008208 eap->errmsg = ex_errmsg(e_trailing_arg, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008209 else
8210 eap->nextcmd = check_nextcmd(p);
8211 }
8212 }
8213 if (!eap->skip)
8214 find_pattern_in_path(eap->arg, 0, (int)STRLEN(eap->arg),
8215 whole, !eap->forceit,
8216 *eap->cmd == 'd' ? FIND_DEFINE : FIND_ANY,
8217 n, action, eap->line1, eap->line2);
8218}
8219#endif
8220
Bram Moolenaar071d4272004-06-13 20:20:40 +00008221
Bram Moolenaar4033c552017-09-16 20:54:51 +02008222#ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00008223/*
8224 * ":ptag", ":ptselect", ":ptjump", ":ptnext", etc.
8225 */
8226 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008227ex_ptag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008228{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008229 g_do_tagpreview = p_pvh; // will be reset to 0 in ex_tag_cmd()
Bram Moolenaar071d4272004-06-13 20:20:40 +00008230 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
8231}
8232
8233/*
8234 * ":pedit"
8235 */
8236 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008237ex_pedit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008238{
8239 win_T *curwin_save = curwin;
8240
Bram Moolenaar3a2505c2020-03-09 16:40:41 +01008241 if (ERROR_IF_ANY_POPUP_WINDOW)
8242 return;
8243
Bram Moolenaar026587b2019-08-17 15:08:00 +02008244 // Open the preview window or popup and make it the current window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008245 g_do_tagpreview = p_pvh;
Bram Moolenaar576a4a62019-08-18 15:25:17 +02008246 prepare_tagpreview(TRUE, TRUE, FALSE);
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02008247
Bram Moolenaar026587b2019-08-17 15:08:00 +02008248 // Edit the file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008249 do_exedit(eap, NULL);
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02008250
Bram Moolenaar071d4272004-06-13 20:20:40 +00008251 if (curwin != curwin_save && win_valid(curwin_save))
8252 {
Bram Moolenaar026587b2019-08-17 15:08:00 +02008253 // Return cursor to where we were
Bram Moolenaar071d4272004-06-13 20:20:40 +00008254 validate_cursor();
8255 redraw_later(VALID);
8256 win_enter(curwin_save, TRUE);
8257 }
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01008258# ifdef FEAT_PROP_POPUP
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02008259 else if (WIN_IS_POPUP(curwin))
8260 {
8261 // can't keep focus in popup window
8262 win_enter(firstwin, TRUE);
8263 }
8264# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008265 g_do_tagpreview = 0;
8266}
Bram Moolenaar4033c552017-09-16 20:54:51 +02008267#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008268
8269/*
8270 * ":stag", ":stselect" and ":stjump".
8271 */
8272 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008273ex_stag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008274{
8275 postponed_split = -1;
8276 postponed_split_flags = cmdmod.split;
Bram Moolenaard326ce82007-03-11 14:48:29 +00008277 postponed_split_tab = cmdmod.tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008278 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
8279 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00008280 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008281}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008282
8283/*
8284 * ":tag", ":tselect", ":tjump", ":tnext", etc.
8285 */
8286 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008287ex_tag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008288{
8289 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name);
8290}
8291
8292 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008293ex_tag_cmd(exarg_T *eap, char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008294{
8295 int cmd;
8296
8297 switch (name[1])
8298 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008299 case 'j': cmd = DT_JUMP; // ":tjump"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008300 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008301 case 's': cmd = DT_SELECT; // ":tselect"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008302 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008303 case 'p': cmd = DT_PREV; // ":tprevious"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008304 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008305 case 'N': cmd = DT_PREV; // ":tNext"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008306 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008307 case 'n': cmd = DT_NEXT; // ":tnext"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008308 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008309 case 'o': cmd = DT_POP; // ":pop"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008310 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008311 case 'f': // ":tfirst"
8312 case 'r': cmd = DT_FIRST; // ":trewind"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008313 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008314 case 'l': cmd = DT_LAST; // ":tlast"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008315 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008316 default: // ":tag"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008317#ifdef FEAT_CSCOPE
Bram Moolenaar7c94c262008-06-20 09:11:34 +00008318 if (p_cst && *eap->arg != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008319 {
Bram Moolenaard4db7712016-11-12 19:16:46 +01008320 ex_cstag(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008321 return;
8322 }
8323#endif
8324 cmd = DT_TAG;
8325 break;
8326 }
8327
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00008328 if (name[0] == 'l')
8329 {
8330#ifndef FEAT_QUICKFIX
8331 ex_ni(eap);
8332 return;
8333#else
8334 cmd = DT_LTAG;
8335#endif
8336 }
8337
Bram Moolenaar071d4272004-06-13 20:20:40 +00008338 do_tag(eap->arg, cmd, eap->addr_count > 0 ? (int)eap->line2 : 1,
8339 eap->forceit, TRUE);
8340}
8341
8342/*
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008343 * Check "str" for starting with a special cmdline variable.
8344 * If found return one of the SPEC_ values and set "*usedlen" to the length of
8345 * the variable. Otherwise return -1 and "*usedlen" is unchanged.
8346 */
8347 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008348find_cmdline_var(char_u *src, int *usedlen)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008349{
8350 int len;
8351 int i;
Bram Moolenaar8f0b2d42009-05-16 14:41:10 +00008352 static char *(spec_str[]) = {
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008353 "%",
8354#define SPEC_PERC 0
8355 "#",
Bram Moolenaar65f08472017-09-10 18:16:20 +02008356#define SPEC_HASH (SPEC_PERC + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008357 "<cword>", // cursor word
Bram Moolenaar65f08472017-09-10 18:16:20 +02008358#define SPEC_CWORD (SPEC_HASH + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008359 "<cWORD>", // cursor WORD
Bram Moolenaar65f08472017-09-10 18:16:20 +02008360#define SPEC_CCWORD (SPEC_CWORD + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008361 "<cexpr>", // expr under cursor
Bram Moolenaar65f08472017-09-10 18:16:20 +02008362#define SPEC_CEXPR (SPEC_CCWORD + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008363 "<cfile>", // cursor path name
Bram Moolenaar65f08472017-09-10 18:16:20 +02008364#define SPEC_CFILE (SPEC_CEXPR + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008365 "<sfile>", // ":so" file name
Bram Moolenaar65f08472017-09-10 18:16:20 +02008366#define SPEC_SFILE (SPEC_CFILE + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008367 "<slnum>", // ":so" file line number
Bram Moolenaar65f08472017-09-10 18:16:20 +02008368#define SPEC_SLNUM (SPEC_SFILE + 1)
Bram Moolenaara5d04232020-07-26 15:37:02 +02008369 "<stack>", // call stack
8370#define SPEC_STACK (SPEC_SLNUM + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008371 "<afile>", // autocommand file name
Bram Moolenaara5d04232020-07-26 15:37:02 +02008372#define SPEC_AFILE (SPEC_STACK + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008373 "<abuf>", // autocommand buffer number
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008374#define SPEC_ABUF (SPEC_AFILE + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008375 "<amatch>", // autocommand match name
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01008376#define SPEC_AMATCH (SPEC_ABUF + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008377 "<sflnum>", // script file line number
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008378#define SPEC_SFLNUM (SPEC_AMATCH + 1)
Bram Moolenaar90944302020-08-01 20:45:11 +02008379 "<SID>", // script ID: <SNR>123_
8380#define SPEC_SID (SPEC_SFLNUM + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008381#ifdef FEAT_CLIENTSERVER
8382 "<client>"
Bram Moolenaar90944302020-08-01 20:45:11 +02008383# define SPEC_CLIENT (SPEC_SID + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008384#endif
8385 };
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008386
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00008387 for (i = 0; i < (int)(sizeof(spec_str) / sizeof(char *)); ++i)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008388 {
8389 len = (int)STRLEN(spec_str[i]);
8390 if (STRNCMP(src, spec_str[i], len) == 0)
8391 {
8392 *usedlen = len;
8393 return i;
8394 }
8395 }
8396 return -1;
8397}
8398
8399/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008400 * Evaluate cmdline variables.
8401 *
8402 * change '%' to curbuf->b_ffname
8403 * '#' to curwin->w_altfile
8404 * '<cword>' to word under the cursor
8405 * '<cWORD>' to WORD under the cursor
Bram Moolenaar8071cb22019-07-12 17:58:01 +02008406 * '<cexpr>' to C-expression under the cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00008407 * '<cfile>' to path name under the cursor
8408 * '<sfile>' to sourced file name
Bram Moolenaar4f25b1a2020-09-10 19:25:05 +02008409 * '<stack>' to call stack
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01008410 * '<slnum>' to sourced file line number
Bram Moolenaar071d4272004-06-13 20:20:40 +00008411 * '<afile>' to file name for autocommand
8412 * '<abuf>' to buffer number for autocommand
8413 * '<amatch>' to matching name for autocommand
8414 *
8415 * When an error is detected, "errormsg" is set to a non-NULL pointer (may be
8416 * "" for error without a message) and NULL is returned.
8417 * Returns an allocated string if a valid match was found.
8418 * Returns NULL if no match was found. "usedlen" then still contains the
8419 * number of characters to skip.
8420 */
8421 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008422eval_vars(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008423 char_u *src, // pointer into commandline
8424 char_u *srcstart, // beginning of valid memory for src
8425 int *usedlen, // characters after src that are used
8426 linenr_T *lnump, // line number for :e command, or NULL
8427 char **errormsg, // pointer to error message
8428 int *escaped) // return value has escaped white space (can
8429 // be NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008430{
8431 int i;
8432 char_u *s;
8433 char_u *result;
8434 char_u *resultbuf = NULL;
8435 int resultlen;
8436 buf_T *buf;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008437 int valid = VALID_HEAD + VALID_PATH; // assume valid result
Bram Moolenaar071d4272004-06-13 20:20:40 +00008438 int spec_idx;
Bram Moolenaarfd249462018-07-28 16:14:30 +02008439 int tilde_file = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008440 int skip_mod = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008441 char_u strbuf[30];
Bram Moolenaar071d4272004-06-13 20:20:40 +00008442
8443 *errormsg = NULL;
Bram Moolenaar63b92542007-03-27 14:57:09 +00008444 if (escaped != NULL)
8445 *escaped = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008446
8447 /*
8448 * Check if there is something to do.
8449 */
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008450 spec_idx = find_cmdline_var(src, usedlen);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008451 if (spec_idx < 0) // no match
Bram Moolenaar071d4272004-06-13 20:20:40 +00008452 {
8453 *usedlen = 1;
8454 return NULL;
8455 }
8456
8457 /*
8458 * Skip when preceded with a backslash "\%" and "\#".
8459 * Note: In "\\%" the % is also not recognized!
8460 */
8461 if (src > srcstart && src[-1] == '\\')
8462 {
8463 *usedlen = 0;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008464 STRMOVE(src - 1, src); // remove backslash
Bram Moolenaar071d4272004-06-13 20:20:40 +00008465 return NULL;
8466 }
8467
8468 /*
8469 * word or WORD under cursor
8470 */
Bram Moolenaar65f08472017-09-10 18:16:20 +02008471 if (spec_idx == SPEC_CWORD || spec_idx == SPEC_CCWORD
8472 || spec_idx == SPEC_CEXPR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008473 {
Bram Moolenaar65f08472017-09-10 18:16:20 +02008474 resultlen = find_ident_under_cursor(&result,
8475 spec_idx == SPEC_CWORD ? (FIND_IDENT | FIND_STRING)
8476 : spec_idx == SPEC_CEXPR ? (FIND_IDENT | FIND_STRING | FIND_EVAL)
8477 : FIND_STRING);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008478 if (resultlen == 0)
8479 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008480 *errormsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +00008481 return NULL;
8482 }
8483 }
8484
8485 /*
8486 * '#': Alternate file name
8487 * '%': Current file name
8488 * File name under the cursor
8489 * File name for autocommand
8490 * and following modifiers
8491 */
8492 else
8493 {
8494 switch (spec_idx)
8495 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008496 case SPEC_PERC: // '%': current file
Bram Moolenaar071d4272004-06-13 20:20:40 +00008497 if (curbuf->b_fname == NULL)
8498 {
8499 result = (char_u *)"";
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008500 valid = 0; // Must have ":p:h" to be valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00008501 }
8502 else
Bram Moolenaar00136dc2018-07-25 21:19:13 +02008503 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008504 result = curbuf->b_fname;
Bram Moolenaar00136dc2018-07-25 21:19:13 +02008505 tilde_file = STRCMP(result, "~") == 0;
8506 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008507 break;
8508
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008509 case SPEC_HASH: // '#' or "#99": alternate file
8510 if (src[1] == '#') // "##": the argument list
Bram Moolenaar071d4272004-06-13 20:20:40 +00008511 {
8512 result = arg_all();
8513 resultbuf = result;
8514 *usedlen = 2;
Bram Moolenaar63b92542007-03-27 14:57:09 +00008515 if (escaped != NULL)
8516 *escaped = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008517 skip_mod = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008518 break;
8519 }
8520 s = src + 1;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008521 if (*s == '<') // "#<99" uses v:oldfiles
Bram Moolenaard812df62008-11-09 12:46:09 +00008522 ++s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008523 i = (int)getdigits(&s);
Bram Moolenaarc312b8b2017-10-28 17:53:04 +02008524 if (s == src + 2 && src[1] == '-')
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008525 // just a minus sign, don't skip over it
Bram Moolenaarc312b8b2017-10-28 17:53:04 +02008526 s--;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008527 *usedlen = (int)(s - src); // length of what we expand
Bram Moolenaar071d4272004-06-13 20:20:40 +00008528
Bram Moolenaarc312b8b2017-10-28 17:53:04 +02008529 if (src[1] == '<' && i != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008530 {
Bram Moolenaard812df62008-11-09 12:46:09 +00008531 if (*usedlen < 2)
8532 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008533 // Should we give an error message for #<text?
Bram Moolenaard812df62008-11-09 12:46:09 +00008534 *usedlen = 1;
8535 return NULL;
8536 }
8537#ifdef FEAT_EVAL
8538 result = list_find_str(get_vim_var_list(VV_OLDFILES),
8539 (long)i);
8540 if (result == NULL)
8541 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008542 *errormsg = "";
Bram Moolenaard812df62008-11-09 12:46:09 +00008543 return NULL;
8544 }
8545#else
Bram Moolenaar8e481e82019-01-15 20:07:48 +01008546 *errormsg = _("E809: #< is not available without the +eval feature");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008547 return NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +00008548#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008549 }
8550 else
Bram Moolenaard812df62008-11-09 12:46:09 +00008551 {
Bram Moolenaarc312b8b2017-10-28 17:53:04 +02008552 if (i == 0 && src[1] == '<' && *usedlen > 1)
8553 *usedlen = 1;
Bram Moolenaard812df62008-11-09 12:46:09 +00008554 buf = buflist_findnr(i);
8555 if (buf == NULL)
8556 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008557 *errormsg = _("E194: No alternate file name to substitute for '#'");
Bram Moolenaard812df62008-11-09 12:46:09 +00008558 return NULL;
8559 }
8560 if (lnump != NULL)
8561 *lnump = ECMD_LAST;
8562 if (buf->b_fname == NULL)
8563 {
8564 result = (char_u *)"";
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008565 valid = 0; // Must have ":p:h" to be valid
Bram Moolenaard812df62008-11-09 12:46:09 +00008566 }
8567 else
Bram Moolenaar00136dc2018-07-25 21:19:13 +02008568 {
Bram Moolenaard812df62008-11-09 12:46:09 +00008569 result = buf->b_fname;
Bram Moolenaar00136dc2018-07-25 21:19:13 +02008570 tilde_file = STRCMP(result, "~") == 0;
8571 }
Bram Moolenaard812df62008-11-09 12:46:09 +00008572 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008573 break;
8574
8575#ifdef FEAT_SEARCHPATH
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008576 case SPEC_CFILE: // file name under cursor
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008577 result = file_name_at_cursor(FNAME_MESS|FNAME_HYP, 1L, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008578 if (result == NULL)
8579 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008580 *errormsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +00008581 return NULL;
8582 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008583 resultbuf = result; // remember allocated string
Bram Moolenaar071d4272004-06-13 20:20:40 +00008584 break;
8585#endif
8586
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008587 case SPEC_AFILE: // file name for autocommand
Bram Moolenaar071d4272004-06-13 20:20:40 +00008588 result = autocmd_fname;
Bram Moolenaarf6dad432008-09-18 19:29:58 +00008589 if (result != NULL && !autocmd_fname_full)
8590 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008591 // Still need to turn the fname into a full path. It is
8592 // postponed to avoid a delay when <afile> is not used.
Bram Moolenaarf6dad432008-09-18 19:29:58 +00008593 autocmd_fname_full = TRUE;
8594 result = FullName_save(autocmd_fname, FALSE);
8595 vim_free(autocmd_fname);
8596 autocmd_fname = result;
8597 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008598 if (result == NULL)
8599 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008600 *errormsg = _("E495: no autocommand file name to substitute for \"<afile>\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008601 return NULL;
8602 }
Bram Moolenaara0174af2008-01-02 20:08:25 +00008603 result = shorten_fname1(result);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008604 break;
8605
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008606 case SPEC_ABUF: // buffer number for autocommand
Bram Moolenaar071d4272004-06-13 20:20:40 +00008607 if (autocmd_bufnr <= 0)
8608 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008609 *errormsg = _("E496: no autocommand buffer number to substitute for \"<abuf>\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008610 return NULL;
8611 }
8612 sprintf((char *)strbuf, "%d", autocmd_bufnr);
8613 result = strbuf;
8614 break;
8615
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008616 case SPEC_AMATCH: // match name for autocommand
Bram Moolenaar071d4272004-06-13 20:20:40 +00008617 result = autocmd_match;
8618 if (result == NULL)
8619 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008620 *errormsg = _("E497: no autocommand match name to substitute for \"<amatch>\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008621 return NULL;
8622 }
8623 break;
8624
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008625 case SPEC_SFILE: // file name for ":so" command
Bram Moolenaara5d04232020-07-26 15:37:02 +02008626 case SPEC_STACK: // call stack
Bram Moolenaar4f25b1a2020-09-10 19:25:05 +02008627 result = estack_sfile(spec_idx == SPEC_SFILE
8628 ? ESTACK_SFILE : ESTACK_STACK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008629 if (result == NULL)
8630 {
Bram Moolenaara5d04232020-07-26 15:37:02 +02008631 *errormsg = spec_idx == SPEC_SFILE
8632 ? _("E498: no :source file name to substitute for \"<sfile>\"")
8633 : _("E489: no call stack to substitute for \"<stack>\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008634 return NULL;
8635 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01008636 resultbuf = result; // remember allocated string
Bram Moolenaar071d4272004-06-13 20:20:40 +00008637 break;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008638
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008639 case SPEC_SLNUM: // line in file for ":so" command
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01008640 if (SOURCING_NAME == NULL || SOURCING_LNUM == 0)
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01008641 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008642 *errormsg = _("E842: no line number to use for \"<slnum>\"");
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01008643 return NULL;
8644 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01008645 sprintf((char *)strbuf, "%ld", SOURCING_LNUM);
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01008646 result = strbuf;
8647 break;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008648
8649#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008650 case SPEC_SFLNUM: // line in script file
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01008651 if (current_sctx.sc_lnum + SOURCING_LNUM == 0)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008652 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008653 *errormsg = _("E961: no line number to use for \"<sflnum>\"");
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008654 return NULL;
8655 }
8656 sprintf((char *)strbuf, "%ld",
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01008657 (long)(current_sctx.sc_lnum + SOURCING_LNUM));
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008658 result = strbuf;
8659 break;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008660
Bram Moolenaar90944302020-08-01 20:45:11 +02008661 case SPEC_SID:
8662 if (current_sctx.sc_sid <= 0)
8663 {
8664 *errormsg = _(e_usingsid);
8665 return NULL;
8666 }
8667 sprintf((char *)strbuf, "<SNR>%d_", current_sctx.sc_sid);
8668 result = strbuf;
8669 break;
Bram Moolenaare4218b92020-08-01 21:11:38 +02008670#endif
Bram Moolenaar90944302020-08-01 20:45:11 +02008671
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008672#ifdef FEAT_CLIENTSERVER
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008673 case SPEC_CLIENT: // Source of last submitted input
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008674 sprintf((char *)strbuf, PRINTF_HEX_LONG_U,
8675 (long_u)clientWindow);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008676 result = strbuf;
8677 break;
8678#endif
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008679
Bram Moolenaar9e0f6ec2017-05-16 09:36:54 +02008680 default:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008681 result = (char_u *)""; // avoid gcc warning
Bram Moolenaar9e0f6ec2017-05-16 09:36:54 +02008682 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008683 }
8684
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008685 resultlen = (int)STRLEN(result); // length of new string
8686 if (src[*usedlen] == '<') // remove the file name extension
Bram Moolenaar071d4272004-06-13 20:20:40 +00008687 {
8688 ++*usedlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008689 if ((s = vim_strrchr(result, '.')) != NULL && s >= gettail(result))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008690 resultlen = (int)(s - result);
8691 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008692 else if (!skip_mod)
8693 {
Bram Moolenaar00136dc2018-07-25 21:19:13 +02008694 valid |= modify_fname(src, tilde_file, usedlen, &result, &resultbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008695 &resultlen);
8696 if (result == NULL)
8697 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008698 *errormsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +00008699 return NULL;
8700 }
8701 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008702 }
8703
8704 if (resultlen == 0 || valid != VALID_HEAD + VALID_PATH)
8705 {
8706 if (valid != VALID_HEAD + VALID_PATH)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008707 // xgettext:no-c-format
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008708 *errormsg = _("E499: Empty file name for '%' or '#', only works with \":p:h\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008709 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008710 *errormsg = _("E500: Evaluates to an empty string");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008711 result = NULL;
8712 }
8713 else
8714 result = vim_strnsave(result, resultlen);
8715 vim_free(resultbuf);
8716 return result;
8717}
8718
8719/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008720 * Expand the <sfile> string in "arg".
8721 *
8722 * Returns an allocated string, or NULL for any error.
8723 */
8724 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008725expand_sfile(char_u *arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008726{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008727 char *errormsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008728 int len;
8729 char_u *result;
8730 char_u *newres;
8731 char_u *repl;
8732 int srclen;
8733 char_u *p;
8734
8735 result = vim_strsave(arg);
8736 if (result == NULL)
8737 return NULL;
8738
8739 for (p = result; *p; )
8740 {
8741 if (STRNCMP(p, "<sfile>", 7) != 0)
8742 ++p;
8743 else
8744 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008745 // replace "<sfile>" with the sourced file name, and do ":" stuff
Bram Moolenaar63b92542007-03-27 14:57:09 +00008746 repl = eval_vars(p, result, &srclen, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008747 if (errormsg != NULL)
8748 {
8749 if (*errormsg)
8750 emsg(errormsg);
8751 vim_free(result);
8752 return NULL;
8753 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008754 if (repl == NULL) // no match (cannot happen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008755 {
8756 p += srclen;
8757 continue;
8758 }
8759 len = (int)STRLEN(result) - srclen + (int)STRLEN(repl) + 1;
8760 newres = alloc(len);
8761 if (newres == NULL)
8762 {
8763 vim_free(repl);
8764 vim_free(result);
8765 return NULL;
8766 }
8767 mch_memmove(newres, result, (size_t)(p - result));
8768 STRCPY(newres + (p - result), repl);
8769 len = (int)STRLEN(newres);
8770 STRCAT(newres, p + srclen);
8771 vim_free(repl);
8772 vim_free(result);
8773 result = newres;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008774 p = newres + len; // continue after the match
Bram Moolenaar071d4272004-06-13 20:20:40 +00008775 }
8776 }
8777
8778 return result;
8779}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008780
Bram Moolenaar071d4272004-06-13 20:20:40 +00008781#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG) || defined(PROTO)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00008782/*
Bram Moolenaard9462e32011-04-11 21:35:11 +02008783 * Make a dialog message in "buff[DIALOG_MSG_SIZE]".
Bram Moolenaarb765d632005-06-07 21:00:02 +00008784 * "format" must contain "%s".
Bram Moolenaar9c13b352005-05-19 20:53:52 +00008785 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008786 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008787dialog_msg(char_u *buff, char *format, char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008788{
Bram Moolenaar071d4272004-06-13 20:20:40 +00008789 if (fname == NULL)
8790 fname = (char_u *)_("Untitled");
Bram Moolenaard9462e32011-04-11 21:35:11 +02008791 vim_snprintf((char *)buff, DIALOG_MSG_SIZE, format, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008792}
8793#endif
8794
8795/*
8796 * ":behave {mswin,xterm}"
8797 */
8798 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008799ex_behave(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008800{
8801 if (STRCMP(eap->arg, "mswin") == 0)
8802 {
8803 set_option_value((char_u *)"selection", 0L, (char_u *)"exclusive", 0);
8804 set_option_value((char_u *)"selectmode", 0L, (char_u *)"mouse,key", 0);
8805 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"popup", 0);
8806 set_option_value((char_u *)"keymodel", 0L,
8807 (char_u *)"startsel,stopsel", 0);
8808 }
8809 else if (STRCMP(eap->arg, "xterm") == 0)
8810 {
8811 set_option_value((char_u *)"selection", 0L, (char_u *)"inclusive", 0);
8812 set_option_value((char_u *)"selectmode", 0L, (char_u *)"", 0);
8813 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"extend", 0);
8814 set_option_value((char_u *)"keymodel", 0L, (char_u *)"", 0);
8815 }
8816 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008817 semsg(_(e_invarg2), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008818}
8819
Bram Moolenaar071d4272004-06-13 20:20:40 +00008820static int filetype_detect = FALSE;
8821static int filetype_plugin = FALSE;
8822static int filetype_indent = FALSE;
8823
8824/*
8825 * ":filetype [plugin] [indent] {on,off,detect}"
8826 * on: Load the filetype.vim file to install autocommands for file types.
8827 * off: Load the ftoff.vim file to remove all autocommands for file types.
8828 * plugin on: load filetype.vim and ftplugin.vim
8829 * plugin off: load ftplugof.vim
8830 * indent on: load filetype.vim and indent.vim
8831 * indent off: load indoff.vim
8832 */
8833 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008834ex_filetype(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008835{
8836 char_u *arg = eap->arg;
8837 int plugin = FALSE;
8838 int indent = FALSE;
8839
8840 if (*eap->arg == NUL)
8841 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008842 // Print current status.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008843 smsg("filetype detection:%s plugin:%s indent:%s",
Bram Moolenaar071d4272004-06-13 20:20:40 +00008844 filetype_detect ? "ON" : "OFF",
8845 filetype_plugin ? (filetype_detect ? "ON" : "(on)") : "OFF",
8846 filetype_indent ? (filetype_detect ? "ON" : "(on)") : "OFF");
8847 return;
8848 }
8849
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008850 // Accept "plugin" and "indent" in any order.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008851 for (;;)
8852 {
8853 if (STRNCMP(arg, "plugin", 6) == 0)
8854 {
8855 plugin = TRUE;
8856 arg = skipwhite(arg + 6);
8857 continue;
8858 }
8859 if (STRNCMP(arg, "indent", 6) == 0)
8860 {
8861 indent = TRUE;
8862 arg = skipwhite(arg + 6);
8863 continue;
8864 }
8865 break;
8866 }
8867 if (STRCMP(arg, "on") == 0 || STRCMP(arg, "detect") == 0)
8868 {
8869 if (*arg == 'o' || !filetype_detect)
8870 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01008871 source_runtime((char_u *)FILETYPE_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008872 filetype_detect = TRUE;
8873 if (plugin)
8874 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01008875 source_runtime((char_u *)FTPLUGIN_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008876 filetype_plugin = TRUE;
8877 }
8878 if (indent)
8879 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01008880 source_runtime((char_u *)INDENT_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008881 filetype_indent = TRUE;
8882 }
8883 }
8884 if (*arg == 'd')
8885 {
Bram Moolenaar1610d052016-06-09 22:53:01 +02008886 (void)do_doautocmd((char_u *)"filetypedetect BufRead", TRUE, NULL);
Bram Moolenaara3227e22006-03-08 21:32:40 +00008887 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008888 }
8889 }
8890 else if (STRCMP(arg, "off") == 0)
8891 {
8892 if (plugin || indent)
8893 {
8894 if (plugin)
8895 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01008896 source_runtime((char_u *)FTPLUGOF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008897 filetype_plugin = FALSE;
8898 }
8899 if (indent)
8900 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01008901 source_runtime((char_u *)INDOFF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008902 filetype_indent = FALSE;
8903 }
8904 }
8905 else
8906 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01008907 source_runtime((char_u *)FTOFF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008908 filetype_detect = FALSE;
8909 }
8910 }
8911 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008912 semsg(_(e_invarg2), arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008913}
8914
8915/*
Bram Moolenaar3e545692017-06-04 19:00:32 +02008916 * ":setfiletype [FALLBACK] {name}"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008917 */
8918 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008919ex_setfiletype(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008920{
8921 if (!did_filetype)
Bram Moolenaar3e545692017-06-04 19:00:32 +02008922 {
8923 char_u *arg = eap->arg;
8924
8925 if (STRNCMP(arg, "FALLBACK ", 9) == 0)
8926 arg += 9;
8927
8928 set_option_value((char_u *)"filetype", 0L, arg, OPT_LOCAL);
8929 if (arg != eap->arg)
8930 did_filetype = FALSE;
8931 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008932}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008933
Bram Moolenaar071d4272004-06-13 20:20:40 +00008934 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008935ex_digraphs(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008936{
8937#ifdef FEAT_DIGRAPHS
8938 if (*eap->arg != NUL)
8939 putdigraph(eap->arg);
8940 else
Bram Moolenaareae8ae12018-12-14 18:53:02 +01008941 listdigraphs(eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008942#else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008943 emsg(_("E196: No digraphs in this version"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008944#endif
8945}
8946
Bram Moolenaar451fc7b2018-04-27 22:53:07 +02008947#if defined(FEAT_SEARCH_EXTRA) || defined(PROTO)
8948 void
8949set_no_hlsearch(int flag)
8950{
8951 no_hlsearch = flag;
8952# ifdef FEAT_EVAL
8953 set_vim_var_nr(VV_HLSEARCH, !no_hlsearch && p_hls);
8954# endif
8955}
8956
Bram Moolenaar071d4272004-06-13 20:20:40 +00008957/*
8958 * ":nohlsearch"
8959 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008960 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008961ex_nohlsearch(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008962{
Bram Moolenaar451fc7b2018-04-27 22:53:07 +02008963 set_no_hlsearch(TRUE);
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00008964 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008965}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008966#endif
8967
8968#ifdef FEAT_CRYPT
8969/*
8970 * ":X": Get crypt key
8971 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008972 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008973ex_X(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008974{
Bram Moolenaar3a0c9082014-11-12 15:15:42 +01008975 crypt_check_current_method();
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02008976 (void)crypt_get_key(TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008977}
8978#endif
8979
8980#ifdef FEAT_FOLDING
8981 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008982ex_fold(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008983{
8984 if (foldManualAllowed(TRUE))
8985 foldCreate(eap->line1, eap->line2);
8986}
8987
8988 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008989ex_foldopen(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008990{
8991 opFoldRange(eap->line1, eap->line2, eap->cmdidx == CMD_foldopen,
8992 eap->forceit, FALSE);
8993}
8994
8995 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008996ex_folddo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008997{
8998 linenr_T lnum;
8999
Bram Moolenaar4facea32019-10-12 20:17:40 +02009000# ifdef FEAT_CLIPBOARD
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02009001 start_global_changes();
Bram Moolenaar4facea32019-10-12 20:17:40 +02009002# endif
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02009003
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009004 // First set the marks for all lines closed/open.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009005 for (lnum = eap->line1; lnum <= eap->line2; ++lnum)
9006 if (hasFolding(lnum, NULL, NULL) == (eap->cmdidx == CMD_folddoclosed))
9007 ml_setmarked(lnum);
9008
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009009 // Execute the command on the marked lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009010 global_exe(eap->arg);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009011 ml_clearmarked(); // clear rest of the marks
Bram Moolenaar4facea32019-10-12 20:17:40 +02009012# ifdef FEAT_CLIPBOARD
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02009013 end_global_changes();
Bram Moolenaar4facea32019-10-12 20:17:40 +02009014# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009015}
9016#endif
Bram Moolenaarf5291f32017-09-14 22:55:37 +02009017
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009018#if defined(FEAT_QUICKFIX) || defined(PROTO)
Bram Moolenaar39665952018-08-15 20:59:48 +02009019/*
9020 * Returns TRUE if the supplied Ex cmdidx is for a location list command
9021 * instead of a quickfix command.
9022 */
9023 int
9024is_loclist_cmd(int cmdidx)
9025{
Bram Moolenaar74c8be22018-08-23 22:51:40 +02009026 if (cmdidx < 0 || cmdidx >= CMD_SIZE)
Bram Moolenaar39665952018-08-15 20:59:48 +02009027 return FALSE;
9028 return cmdnames[cmdidx].cmd_name[0] == 'l';
9029}
9030#endif
9031
Bram Moolenaar4facea32019-10-12 20:17:40 +02009032#if defined(FEAT_TIMERS) || defined(PROTO)
Bram Moolenaarf5291f32017-09-14 22:55:37 +02009033 int
9034get_pressedreturn(void)
9035{
9036 return ex_pressedreturn;
9037}
9038
9039 void
9040set_pressedreturn(int val)
9041{
9042 ex_pressedreturn = val;
9043}
9044#endif