blob: 91d56d71ffdfbb8cccdbc3a56036f5f303b67d53 [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 Moolenaar1b1e9df2020-10-10 22:26:52 +0200266# define ex_block ex_ni
Bram Moolenaare4ce8252019-08-04 15:30:16 +0200267# define ex_break ex_ni
268# define ex_breakadd ex_ni
269# define ex_breakdel ex_ni
270# define ex_breaklist ex_ni
271# define ex_call ex_ni
272# define ex_catch ex_ni
273# define ex_compiler ex_ni
Bram Moolenaare4ce8252019-08-04 15:30:16 +0200274# define ex_continue ex_ni
275# define ex_debug ex_ni
276# define ex_debuggreedy ex_ni
Bram Moolenaar822ba242020-05-24 23:00:18 +0200277# 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 Moolenaar1b1e9df2020-10-10 22:26:52 +0200283# define ex_endblock ex_ni
Bram Moolenaare4ce8252019-08-04 15:30:16 +0200284# define ex_endfunction ex_ni
285# define ex_endif ex_ni
286# define ex_endtry ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000287# define ex_endwhile ex_ni
Bram Moolenaare4ce8252019-08-04 15:30:16 +0200288# define ex_eval ex_ni
289# define ex_execute ex_ni
290# define ex_finally ex_ni
291# define ex_finish ex_ni
292# define ex_function ex_ni
293# define ex_if ex_ni
294# define ex_let ex_ni
Bram Moolenaard47f50b2020-09-26 15:20:42 +0200295# define ex_var ex_ni
Bram Moolenaare4ce8252019-08-04 15:30:16 +0200296# define ex_lockvar ex_ni
297# define ex_oldfiles ex_ni
298# define ex_options ex_ni
299# define ex_packadd ex_ni
300# define ex_packloadall ex_ni
301# define ex_return ex_ni
302# define ex_scriptnames ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000303# define ex_throw ex_ni
304# define ex_try ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000305# define ex_unlet ex_ni
Bram Moolenaar65c1b012005-01-31 19:02:28 +0000306# define ex_unlockvar ex_ni
Bram Moolenaare4ce8252019-08-04 15:30:16 +0200307# define ex_while ex_ni
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100308# define ex_import ex_ni
309# define ex_export ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000310#endif
Bram Moolenaar84538072019-07-28 14:15:42 +0200311#ifndef FEAT_SESSION
Bram Moolenaar071d4272004-06-13 20:20:40 +0000312# define ex_loadview ex_ni
313#endif
Bram Moolenaardefa0672019-07-21 19:25:37 +0200314#ifndef FEAT_VIMINFO
Bram Moolenaar071d4272004-06-13 20:20:40 +0000315# define ex_viminfo ex_ni
316#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100317static void ex_behave(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100318static void ex_filetype(exarg_T *eap);
319static void ex_setfiletype(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000320#ifndef FEAT_DIFF
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000321# define ex_diffoff ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000322# define ex_diffpatch ex_ni
323# define ex_diffgetput ex_ni
324# define ex_diffsplit ex_ni
325# define ex_diffthis ex_ni
326# define ex_diffupdate ex_ni
327#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100328static void ex_digraphs(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000329#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100330static void ex_nohlsearch(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000331#else
332# define ex_nohlsearch ex_ni
333# define ex_match ex_ni
334#endif
335#ifdef FEAT_CRYPT
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100336static void ex_X(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000337#else
338# define ex_X ex_ni
339#endif
340#ifdef FEAT_FOLDING
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100341static void ex_fold(exarg_T *eap);
342static void ex_foldopen(exarg_T *eap);
343static void ex_folddo(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000344#else
345# define ex_fold ex_ni
346# define ex_foldopen ex_ni
347# define ex_folddo ex_ni
348#endif
Bram Moolenaar13505972019-01-24 15:04:48 +0100349#if !(defined(HAVE_LOCALE_H) || defined(X_LOCALE))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000350# define ex_language ex_ni
351#endif
352#ifndef FEAT_SIGNS
353# define ex_sign ex_ni
354#endif
Bram Moolenaar009b2592004-10-24 19:18:58 +0000355#ifndef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200356# define ex_nbclose ex_ni
Bram Moolenaar009b2592004-10-24 19:18:58 +0000357# define ex_nbkey ex_ni
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200358# define ex_nbstart ex_ni
Bram Moolenaar009b2592004-10-24 19:18:58 +0000359#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000360
Bram Moolenaar071d4272004-06-13 20:20:40 +0000361#ifndef FEAT_JUMPLIST
362# define ex_jumps ex_ni
Bram Moolenaar2d358992016-06-12 21:20:54 +0200363# define ex_clearjumps ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000364# define ex_changes ex_ni
365#endif
366
Bram Moolenaar05159a02005-02-26 23:04:13 +0000367#ifndef FEAT_PROFILE
368# define ex_profile ex_ni
369#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200370#ifndef FEAT_TERMINAL
371# define ex_terminal ex_ni
372#endif
Bram Moolenaard4aa83a2019-05-09 18:59:31 +0200373#if !defined(FEAT_X11) || !defined(FEAT_XCLIPBOARD)
374# define ex_xrestore ex_ni
375#endif
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +0100376#if !defined(FEAT_PROP_POPUP)
Bram Moolenaar682725c2019-05-25 20:10:37 +0200377# define ex_popupclear ex_ni
378#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +0000379
Bram Moolenaar071d4272004-06-13 20:20:40 +0000380/*
381 * Declare cmdnames[].
382 */
383#define DO_DECLARE_EXCMD
384#include "ex_cmds.h"
Bram Moolenaar6de5e122017-04-20 21:55:44 +0200385#include "ex_cmdidxs.h"
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +0100386
Bram Moolenaar071d4272004-06-13 20:20:40 +0000387static char_u dollar_command[2] = {'$', 0};
388
389
390#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100391// Struct for storing a line inside a while/for loop
Bram Moolenaar071d4272004-06-13 20:20:40 +0000392typedef struct
393{
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100394 char_u *line; // command line
395 linenr_T lnum; // sourcing_lnum of the line
Bram Moolenaar071d4272004-06-13 20:20:40 +0000396} wcmd_T;
397
398/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000399 * Structure used to store info for line position in a while or for loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000400 * This is required, because do_one_cmd() may invoke ex_function(), which
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000401 * reads more lines that may come from the while/for loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000402 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000403struct loop_cookie
Bram Moolenaar071d4272004-06-13 20:20:40 +0000404{
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100405 garray_T *lines_gap; // growarray with line info
406 int current_line; // last read line from growarray
407 int repeating; // TRUE when looping a second time
408 // When "repeating" is FALSE use "getline" and "cookie" to get lines
Bram Moolenaar20b23c62020-08-20 15:25:00 +0200409 char_u *(*getline)(int, void *, int, getline_opt_T);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000410 void *cookie;
411};
412
Bram Moolenaar20b23c62020-08-20 15:25:00 +0200413static char_u *get_loop_line(int c, void *cookie, int indent, getline_opt_T options);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100414static int store_loop_line(garray_T *gap, char_u *line);
415static void free_cmdlines(garray_T *gap);
Bram Moolenaared203462004-06-16 11:19:22 +0000416
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100417// Struct to save a few things while debugging. Used in do_cmdline() only.
Bram Moolenaared203462004-06-16 11:19:22 +0000418struct dbg_stuff
419{
420 int trylevel;
421 int force_abort;
422 except_T *caught_stack;
423 char_u *vv_exception;
424 char_u *vv_throwpoint;
425 int did_emsg;
426 int got_int;
427 int did_throw;
428 int need_rethrow;
429 int check_cstack;
430 except_T *current_exception;
431};
432
Bram Moolenaared203462004-06-16 11:19:22 +0000433 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100434save_dbg_stuff(struct dbg_stuff *dsp)
Bram Moolenaared203462004-06-16 11:19:22 +0000435{
436 dsp->trylevel = trylevel; trylevel = 0;
437 dsp->force_abort = force_abort; force_abort = FALSE;
438 dsp->caught_stack = caught_stack; caught_stack = NULL;
439 dsp->vv_exception = v_exception(NULL);
440 dsp->vv_throwpoint = v_throwpoint(NULL);
441
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100442 // Necessary for debugging an inactive ":catch", ":finally", ":endtry"
Bram Moolenaared203462004-06-16 11:19:22 +0000443 dsp->did_emsg = did_emsg; did_emsg = FALSE;
444 dsp->got_int = got_int; got_int = FALSE;
445 dsp->did_throw = did_throw; did_throw = FALSE;
446 dsp->need_rethrow = need_rethrow; need_rethrow = FALSE;
447 dsp->check_cstack = check_cstack; check_cstack = FALSE;
448 dsp->current_exception = current_exception; current_exception = NULL;
449}
450
451 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100452restore_dbg_stuff(struct dbg_stuff *dsp)
Bram Moolenaared203462004-06-16 11:19:22 +0000453{
454 suppress_errthrow = FALSE;
455 trylevel = dsp->trylevel;
456 force_abort = dsp->force_abort;
457 caught_stack = dsp->caught_stack;
458 (void)v_exception(dsp->vv_exception);
459 (void)v_throwpoint(dsp->vv_throwpoint);
460 did_emsg = dsp->did_emsg;
461 got_int = dsp->got_int;
462 did_throw = dsp->did_throw;
463 need_rethrow = dsp->need_rethrow;
464 check_cstack = dsp->check_cstack;
465 current_exception = dsp->current_exception;
466}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000467#endif
468
Bram Moolenaar071d4272004-06-13 20:20:40 +0000469/*
470 * do_exmode(): Repeatedly get commands for the "Ex" mode, until the ":vi"
471 * command is given.
472 */
473 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100474do_exmode(
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100475 int improved) // TRUE for "improved Ex" mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000476{
477 int save_msg_scroll;
478 int prev_msg_row;
479 linenr_T prev_line;
Bram Moolenaar79518e22017-02-17 16:31:35 +0100480 varnumber_T changedtick;
Bram Moolenaardf177f62005-02-22 08:39:57 +0000481
482 if (improved)
483 exmode_active = EXMODE_VIM;
484 else
485 exmode_active = EXMODE_NORMAL;
486 State = NORMAL;
487
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100488 // When using ":global /pat/ visual" and then "Q" we return to continue
489 // the :global command.
Bram Moolenaardf177f62005-02-22 08:39:57 +0000490 if (global_busy)
491 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000492
493 save_msg_scroll = msg_scroll;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100494 ++RedrawingDisabled; // don't redisplay the window
495 ++no_wait_return; // don't wait for return
Bram Moolenaar071d4272004-06-13 20:20:40 +0000496#ifdef FEAT_GUI
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100497 // Ignore scrollbar and mouse events in Ex mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000498 ++hold_gui_events;
499#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000500
Bram Moolenaar32526b32019-01-19 17:43:09 +0100501 msg(_("Entering Ex mode. Type \"visual\" to go to Normal mode."));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000502 while (exmode_active)
503 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100504 // Check for a ":normal" command and no more characters left.
Bram Moolenaar7c626922005-02-07 22:01:03 +0000505 if (ex_normal_busy > 0 && typebuf.tb_len == 0)
506 {
507 exmode_active = FALSE;
508 break;
509 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000510 msg_scroll = TRUE;
511 need_wait_return = FALSE;
512 ex_pressedreturn = FALSE;
513 ex_no_reprint = FALSE;
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100514 changedtick = CHANGEDTICK(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000515 prev_msg_row = msg_row;
516 prev_line = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000517 if (improved)
518 {
519 cmdline_row = msg_row;
520 do_cmdline(NULL, getexline, NULL, 0);
521 }
522 else
523 do_cmdline(NULL, getexmodeline, NULL, DOCMD_NOWAIT);
524 lines_left = Rows - 1;
525
Bram Moolenaardf177f62005-02-22 08:39:57 +0000526 if ((prev_line != curwin->w_cursor.lnum
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100527 || changedtick != CHANGEDTICK(curbuf)) && !ex_no_reprint)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000528 {
Bram Moolenaardf177f62005-02-22 08:39:57 +0000529 if (curbuf->b_ml.ml_flags & ML_EMPTY)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100530 emsg(_(e_emptybuf));
Bram Moolenaardf177f62005-02-22 08:39:57 +0000531 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000532 {
Bram Moolenaardf177f62005-02-22 08:39:57 +0000533 if (ex_pressedreturn)
534 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100535 // go up one line, to overwrite the ":<CR>" line, so the
536 // output doesn't contain empty lines.
Bram Moolenaardf177f62005-02-22 08:39:57 +0000537 msg_row = prev_msg_row;
538 if (prev_msg_row == Rows - 1)
539 msg_row--;
540 }
541 msg_col = 0;
542 print_line_no_prefix(curwin->w_cursor.lnum, FALSE, FALSE);
543 msg_clr_eos();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000544 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000545 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100546 else if (ex_pressedreturn && !ex_no_reprint) // must be at EOF
Bram Moolenaardf177f62005-02-22 08:39:57 +0000547 {
548 if (curbuf->b_ml.ml_flags & ML_EMPTY)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100549 emsg(_(e_emptybuf));
Bram Moolenaardf177f62005-02-22 08:39:57 +0000550 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100551 emsg(_("E501: At end-of-file"));
Bram Moolenaardf177f62005-02-22 08:39:57 +0000552 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000553 }
554
555#ifdef FEAT_GUI
556 --hold_gui_events;
557#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000558 --RedrawingDisabled;
559 --no_wait_return;
560 update_screen(CLEAR);
561 need_wait_return = FALSE;
562 msg_scroll = save_msg_scroll;
563}
564
565/*
Bram Moolenaar4facea32019-10-12 20:17:40 +0200566 * Print the executed command for when 'verbose' is set.
567 * When "lnum" is 0 only print the command.
568 */
569 static void
570msg_verbose_cmd(linenr_T lnum, char_u *cmd)
571{
572 ++no_wait_return;
573 verbose_enter_scroll();
574
575 if (lnum == 0)
576 smsg(_("Executing: %s"), cmd);
577 else
578 smsg(_("line %ld: %s"), (long)lnum, cmd);
579 if (msg_silent == 0)
580 msg_puts("\n"); // don't overwrite this
581
582 verbose_leave_scroll();
583 --no_wait_return;
584}
585
586/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000587 * Execute a simple command line. Used for translated commands like "*".
588 */
589 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100590do_cmdline_cmd(char_u *cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000591{
592 return do_cmdline(cmd, NULL, NULL,
593 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
594}
595
596/*
Bram Moolenaar47a2abf2020-11-25 20:12:11 +0100597 * Execute the "+cmd" argument of "edit +cmd fname" and the like.
598 * This allows for using a range without ":" in Vim9 script.
599 */
600 int
601do_cmd_argument(char_u *cmd)
602{
603 return do_cmdline(cmd, NULL, NULL,
604 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED|DOCMD_RANGEOK);
605}
606
607/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000608 * do_cmdline(): execute one Ex command line
609 *
610 * 1. Execute "cmdline" when it is not NULL.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100611 * If "cmdline" is NULL, or more lines are needed, fgetline() is used.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000612 * 2. Split up in parts separated with '|'.
613 *
614 * This function can be called recursively!
615 *
616 * flags:
617 * DOCMD_VERBOSE - The command will be included in the error message.
618 * DOCMD_NOWAIT - Don't call wait_return() and friends.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100619 * DOCMD_REPEAT - Repeat execution until fgetline() returns NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000620 * DOCMD_KEYTYPED - Don't reset KeyTyped.
621 * DOCMD_EXCRESET - Reset the exception environment (used for debugging).
622 * DOCMD_KEEPLINE - Store first typed line (for repeating with ".").
623 *
624 * return FAIL if cmdline could not be executed, OK otherwise
625 */
626 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100627do_cmdline(
628 char_u *cmdline,
Bram Moolenaar66250c92020-08-20 15:02:42 +0200629 char_u *(*fgetline)(int, void *, int, getline_opt_T),
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100630 void *cookie, // argument for fgetline()
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100631 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000632{
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100633 char_u *next_cmdline; // next cmd to execute
634 char_u *cmdline_copy = NULL; // copy of cmd line
635 int used_getline = FALSE; // used "fgetline" to obtain command
636 static int recursive = 0; // recursive depth
Bram Moolenaar071d4272004-06-13 20:20:40 +0000637 int msg_didout_before_start = 0;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100638 int count = 0; // line number count
639 int did_inc = FALSE; // incremented RedrawingDisabled
Bram Moolenaar071d4272004-06-13 20:20:40 +0000640 int retval = OK;
641#ifdef FEAT_EVAL
Bram Moolenaarddef1292019-12-16 17:10:33 +0100642 cstack_T cstack; // conditional stack
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100643 garray_T lines_ga; // keep lines for ":while"/":for"
644 int current_line = 0; // active line in lines_ga
Bram Moolenaard5053d02020-06-28 15:51:16 +0200645 int current_line_before = 0;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100646 char_u *fname = NULL; // function or script name
647 linenr_T *breakpoint = NULL; // ptr to breakpoint field in cookie
648 int *dbg_tick = NULL; // ptr to dbg_tick field in cookie
649 struct dbg_stuff debug_saved; // saved things for debug mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000650 int initial_trylevel;
Bram Moolenaar25e0f582020-05-25 22:36:50 +0200651 msglist_T **saved_msg_list = NULL;
Bram Moolenaar683581e2020-10-22 21:22:58 +0200652 msglist_T *private_msg_list = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000653
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100654 // "fgetline" and "cookie" passed to do_one_cmd()
Bram Moolenaar66250c92020-08-20 15:02:42 +0200655 char_u *(*cmd_getline)(int, void *, int, getline_opt_T);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000656 void *cmd_cookie;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000657 struct loop_cookie cmd_loop_cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000658 void *real_cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +0000659 int getline_is_func;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000660#else
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100661# define cmd_getline fgetline
Bram Moolenaar071d4272004-06-13 20:20:40 +0000662# define cmd_cookie cookie
663#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100664 static int call_depth = 0; // recursiveness
Bram Moolenaar2196bce2020-04-12 20:01:11 +0200665#ifdef FEAT_EVAL
Bram Moolenaare31ee862020-01-07 20:59:34 +0100666 ESTACK_CHECK_DECLARATION
Bram Moolenaar071d4272004-06-13 20:20:40 +0000667
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100668 // For every pair of do_cmdline()/do_one_cmd() calls, use an extra memory
669 // location for storing error messages to be converted to an exception.
670 // This ensures that the do_errthrow() call in do_one_cmd() does not
671 // combine the messages stored by an earlier invocation of do_one_cmd()
672 // with the command name of the later one. This would happen when
673 // BufWritePost autocommands are executed after a write error.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000674 saved_msg_list = msg_list;
675 msg_list = &private_msg_list;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000676#endif
677
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100678 // It's possible to create an endless loop with ":execute", catch that
679 // here. The value of 200 allows nested function calls, ":source", etc.
680 // Allow 200 or 'maxfuncdepth', whatever is larger.
Bram Moolenaarb094ff42017-01-02 16:16:39 +0100681 if (call_depth >= 200
682#ifdef FEAT_EVAL
683 && call_depth >= p_mfd
684#endif
685 )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000686 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100687 emsg(_("E169: Command too recursive"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000688#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100689 // When converting to an exception, we do not include the command name
690 // since this is not an error of the specific command.
Bram Moolenaarddef1292019-12-16 17:10:33 +0100691 do_errthrow((cstack_T *)NULL, (char_u *)NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000692 msg_list = saved_msg_list;
693#endif
694 return FAIL;
695 }
696 ++call_depth;
697
698#ifdef FEAT_EVAL
699 cstack.cs_idx = -1;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000700 cstack.cs_looplevel = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000701 cstack.cs_trylevel = 0;
702 cstack.cs_emsg_silent_list = NULL;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000703 cstack.cs_lflags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000704 ga_init2(&lines_ga, (int)sizeof(wcmd_T), 10);
705
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100706 real_cookie = getline_cookie(fgetline, cookie);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000707
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100708 // Inside a function use a higher nesting level.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100709 getline_is_func = getline_equal(fgetline, cookie, get_func_line);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000710 if (getline_is_func && ex_nesting_level == func_level(real_cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000711 ++ex_nesting_level;
712
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100713 // Get the function or script name and the address where the next breakpoint
714 // line and the debug tick for a function or script are stored.
Bram Moolenaar05159a02005-02-26 23:04:13 +0000715 if (getline_is_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000716 {
717 fname = func_name(real_cookie);
718 breakpoint = func_breakpoint(real_cookie);
719 dbg_tick = func_dbg_tick(real_cookie);
720 }
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100721 else if (getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000722 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100723 fname = SOURCING_NAME;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000724 breakpoint = source_breakpoint(real_cookie);
725 dbg_tick = source_dbg_tick(real_cookie);
726 }
727
728 /*
729 * Initialize "force_abort" and "suppress_errthrow" at the top level.
730 */
731 if (!recursive)
732 {
733 force_abort = FALSE;
734 suppress_errthrow = FALSE;
735 }
736
737 /*
738 * If requested, store and reset the global values controlling the
Bram Moolenaar89d40322006-08-29 15:30:07 +0000739 * exception handling (used when debugging). Otherwise clear it to avoid
740 * a bogus compiler warning when the optimizer uses inline functions...
Bram Moolenaar071d4272004-06-13 20:20:40 +0000741 */
Bram Moolenaarb4872942006-05-13 10:32:52 +0000742 if (flags & DOCMD_EXCRESET)
Bram Moolenaared203462004-06-16 11:19:22 +0000743 save_dbg_stuff(&debug_saved);
Bram Moolenaar89d40322006-08-29 15:30:07 +0000744 else
Bram Moolenaara80faa82020-04-12 19:37:17 +0200745 CLEAR_FIELD(debug_saved);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000746
747 initial_trylevel = trylevel;
748
749 /*
750 * "did_throw" will be set to TRUE when an exception is being thrown.
751 */
752 did_throw = FALSE;
753#endif
754 /*
755 * "did_emsg" will be set to TRUE when emsg() is used, in which case we
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000756 * cancel the whole command line, and any if/endif or loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000757 * If force_abort is set, we cancel everything.
758 */
Bram Moolenaareeece9e2020-11-20 19:26:48 +0100759#ifdef FEAT_EVAL
760 did_emsg_cumul += did_emsg;
761#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000762 did_emsg = FALSE;
763
764 /*
765 * KeyTyped is only set when calling vgetc(). Reset it here when not
766 * calling vgetc() (sourced command lines).
767 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100768 if (!(flags & DOCMD_KEYTYPED)
769 && !getline_equal(fgetline, cookie, getexline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000770 KeyTyped = FALSE;
771
772 /*
773 * Continue executing command lines:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000774 * - when inside an ":if", ":while" or ":for"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000775 * - for multiple commands on one line, separated with '|'
776 * - when repeating until there are no more lines (for ":source")
777 */
778 next_cmdline = cmdline;
779 do
780 {
Bram Moolenaar05159a02005-02-26 23:04:13 +0000781#ifdef FEAT_EVAL
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100782 getline_is_func = getline_equal(fgetline, cookie, get_func_line);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000783#endif
784
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100785 // stop skipping cmds for an error msg after all endif/while/for
Bram Moolenaar071d4272004-06-13 20:20:40 +0000786 if (next_cmdline == NULL
787#ifdef FEAT_EVAL
788 && !force_abort
789 && cstack.cs_idx < 0
Bram Moolenaar05159a02005-02-26 23:04:13 +0000790 && !(getline_is_func && func_has_abort(real_cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000791#endif
792 )
Bram Moolenaareeece9e2020-11-20 19:26:48 +0100793 {
794#ifdef FEAT_EVAL
795 did_emsg_cumul += did_emsg;
796#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000797 did_emsg = FALSE;
Bram Moolenaareeece9e2020-11-20 19:26:48 +0100798 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000799
800 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000801 * 1. If repeating a line in a loop, get a line from lines_ga.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100802 * 2. If no line given: Get an allocated line with fgetline().
Bram Moolenaar071d4272004-06-13 20:20:40 +0000803 * 3. If a line is given: Make a copy, so we can mess with it.
804 */
805
806#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100807 // 1. If repeating, get a previous line from lines_ga.
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000808 if (cstack.cs_looplevel > 0 && current_line < lines_ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000809 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100810 // Each '|' separated command is stored separately in lines_ga, to
811 // be able to jump to it. Don't use next_cmdline now.
Bram Moolenaard23a8232018-02-10 18:45:26 +0100812 VIM_CLEAR(cmdline_copy);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000813
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100814 // Check if a function has returned or, unless it has an unclosed
815 // try conditional, aborted.
Bram Moolenaar05159a02005-02-26 23:04:13 +0000816 if (getline_is_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000817 {
Bram Moolenaar05159a02005-02-26 23:04:13 +0000818# ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000819 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +0000820 func_line_end(real_cookie);
821# endif
822 if (func_has_ended(real_cookie))
823 {
824 retval = FAIL;
825 break;
826 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000827 }
Bram Moolenaar05159a02005-02-26 23:04:13 +0000828#ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000829 else if (do_profiling == PROF_YES
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100830 && getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar05159a02005-02-26 23:04:13 +0000831 script_line_end();
832#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000833
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100834 // Check if a sourced file hit a ":finish" command.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100835 if (source_finished(fgetline, cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000836 {
837 retval = FAIL;
838 break;
839 }
840
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100841 // If breakpoints have been added/deleted need to check for it.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000842 if (breakpoint != NULL && dbg_tick != NULL
843 && *dbg_tick != debug_tick)
844 {
845 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100846 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100847 fname, SOURCING_LNUM);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000848 *dbg_tick = debug_tick;
849 }
850
851 next_cmdline = ((wcmd_T *)(lines_ga.ga_data))[current_line].line;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100852 SOURCING_LNUM = ((wcmd_T *)(lines_ga.ga_data))[current_line].lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000853
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100854 // Did we encounter a breakpoint?
Bram Moolenaar071d4272004-06-13 20:20:40 +0000855 if (breakpoint != NULL && *breakpoint != 0
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100856 && *breakpoint <= SOURCING_LNUM)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000857 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100858 dbg_breakpoint(fname, SOURCING_LNUM);
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100859 // Find next breakpoint.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000860 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100861 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100862 fname, SOURCING_LNUM);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000863 *dbg_tick = debug_tick;
864 }
Bram Moolenaar05159a02005-02-26 23:04:13 +0000865# ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000866 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +0000867 {
868 if (getline_is_func)
869 func_line_start(real_cookie);
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100870 else if (getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar05159a02005-02-26 23:04:13 +0000871 script_line_start();
872 }
873# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000874 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000875#endif
876
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100877 // 2. If no line given, get an allocated line with fgetline().
Bram Moolenaar071d4272004-06-13 20:20:40 +0000878 if (next_cmdline == NULL)
879 {
880 /*
881 * Need to set msg_didout for the first line after an ":if",
882 * otherwise the ":if" will be overwritten.
883 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100884 if (count == 1 && getline_equal(fgetline, cookie, getexline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000885 msg_didout = TRUE;
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100886 if (fgetline == NULL || (next_cmdline = fgetline(':', cookie,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000887#ifdef FEAT_EVAL
888 cstack.cs_idx < 0 ? 0 : (cstack.cs_idx + 1) * 2
889#else
890 0
891#endif
Bram Moolenaare96a2492019-06-25 04:12:16 +0200892 , TRUE)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000893 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100894 // Don't call wait_return for aborted command line. The NULL
895 // returned for the end of a sourced file or executed function
896 // doesn't do this.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000897 if (KeyTyped && !(flags & DOCMD_REPEAT))
898 need_wait_return = FALSE;
899 retval = FAIL;
900 break;
901 }
902 used_getline = TRUE;
903
904 /*
905 * Keep the first typed line. Clear it when more lines are typed.
906 */
907 if (flags & DOCMD_KEEPLINE)
908 {
909 vim_free(repeat_cmdline);
910 if (count == 0)
911 repeat_cmdline = vim_strsave(next_cmdline);
912 else
913 repeat_cmdline = NULL;
914 }
915 }
916
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100917 // 3. Make a copy of the command so we can mess with it.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000918 else if (cmdline_copy == NULL)
919 {
920 next_cmdline = vim_strsave(next_cmdline);
921 if (next_cmdline == NULL)
922 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100923 emsg(_(e_outofmem));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000924 retval = FAIL;
925 break;
926 }
927 }
928 cmdline_copy = next_cmdline;
929
930#ifdef FEAT_EVAL
931 /*
Bram Moolenaard5053d02020-06-28 15:51:16 +0200932 * Inside a while/for loop, and when the command looks like a ":while"
933 * or ":for", the line is stored, because we may need it later when
934 * looping.
935 *
936 * When there is a '|' and another command, it is stored separately,
937 * because we need to be able to jump back to it from an
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000938 * :endwhile/:endfor.
Bram Moolenaard5053d02020-06-28 15:51:16 +0200939 *
940 * Pass a different "fgetline" function to do_one_cmd() below,
941 * that it stores lines in or reads them from "lines_ga". Makes it
942 * possible to define a function inside a while/for loop and handles
943 * line continuation.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000944 */
Bram Moolenaard5053d02020-06-28 15:51:16 +0200945 if ((cstack.cs_looplevel > 0 || has_loop_cmd(next_cmdline)))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000946 {
Bram Moolenaard5053d02020-06-28 15:51:16 +0200947 cmd_getline = get_loop_line;
948 cmd_cookie = (void *)&cmd_loop_cookie;
949 cmd_loop_cookie.lines_gap = &lines_ga;
950 cmd_loop_cookie.current_line = current_line;
951 cmd_loop_cookie.getline = fgetline;
952 cmd_loop_cookie.cookie = cookie;
953 cmd_loop_cookie.repeating = (current_line < lines_ga.ga_len);
954
955 // Save the current line when encountering it the first time.
956 if (current_line == lines_ga.ga_len
957 && store_loop_line(&lines_ga, next_cmdline) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000958 {
959 retval = FAIL;
960 break;
961 }
Bram Moolenaard5053d02020-06-28 15:51:16 +0200962 current_line_before = current_line;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000963 }
Bram Moolenaard5053d02020-06-28 15:51:16 +0200964 else
965 {
966 cmd_getline = fgetline;
967 cmd_cookie = cookie;
968 }
969
Bram Moolenaar071d4272004-06-13 20:20:40 +0000970 did_endif = FALSE;
971#endif
972
973 if (count++ == 0)
974 {
975 /*
976 * All output from the commands is put below each other, without
977 * waiting for a return. Don't do this when executing commands
978 * from a script or when being called recursive (e.g. for ":e
979 * +command file").
980 */
981 if (!(flags & DOCMD_NOWAIT) && !recursive)
982 {
983 msg_didout_before_start = msg_didout;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100984 msg_didany = FALSE; // no output yet
Bram Moolenaar071d4272004-06-13 20:20:40 +0000985 msg_start();
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100986 msg_scroll = TRUE; // put messages below each other
987 ++no_wait_return; // don't wait for return until finished
Bram Moolenaar071d4272004-06-13 20:20:40 +0000988 ++RedrawingDisabled;
989 did_inc = TRUE;
990 }
991 }
992
Bram Moolenaar823654b2020-05-29 23:03:09 +0200993 if ((p_verbose >= 15 && SOURCING_NAME != NULL) || p_verbose >= 16)
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100994 msg_verbose_cmd(SOURCING_LNUM, cmdline_copy);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000995
996 /*
997 * 2. Execute one '|' separated command.
998 * do_one_cmd() will return NULL if there is no trailing '|'.
999 * "cmdline_copy" can change, e.g. for '%' and '#' expansion.
1000 */
1001 ++recursive;
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01001002 next_cmdline = do_one_cmd(&cmdline_copy, flags,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001003#ifdef FEAT_EVAL
1004 &cstack,
1005#endif
1006 cmd_getline, cmd_cookie);
1007 --recursive;
1008
1009#ifdef FEAT_EVAL
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001010 if (cmd_cookie == (void *)&cmd_loop_cookie)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001011 // Use "current_line" from "cmd_loop_cookie", it may have been
1012 // incremented when defining a function.
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001013 current_line = cmd_loop_cookie.current_line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001014#endif
1015
1016 if (next_cmdline == NULL)
1017 {
Bram Moolenaard23a8232018-02-10 18:45:26 +01001018 VIM_CLEAR(cmdline_copy);
Bram Moolenaard7663c22019-08-06 21:59:57 +02001019
Bram Moolenaar071d4272004-06-13 20:20:40 +00001020 /*
1021 * If the command was typed, remember it for the ':' register.
1022 * Do this AFTER executing the command to make :@: work.
1023 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001024 if (getline_equal(fgetline, cookie, getexline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001025 && new_last_cmdline != NULL)
1026 {
1027 vim_free(last_cmdline);
1028 last_cmdline = new_last_cmdline;
1029 new_last_cmdline = NULL;
1030 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001031 }
1032 else
1033 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001034 // need to copy the command after the '|' to cmdline_copy, for the
1035 // next do_one_cmd()
Bram Moolenaara7241f52008-06-24 20:39:31 +00001036 STRMOVE(cmdline_copy, next_cmdline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001037 next_cmdline = cmdline_copy;
1038 }
1039
1040
1041#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001042 // reset did_emsg for a function that is not aborted by an error
Bram Moolenaar071d4272004-06-13 20:20:40 +00001043 if (did_emsg && !force_abort
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001044 && getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001045 && !func_has_abort(real_cookie))
Bram Moolenaareeece9e2020-11-20 19:26:48 +01001046 {
1047 // did_emsg_cumul is not set here
Bram Moolenaar071d4272004-06-13 20:20:40 +00001048 did_emsg = FALSE;
Bram Moolenaareeece9e2020-11-20 19:26:48 +01001049 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001050
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001051 if (cstack.cs_looplevel > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001052 {
1053 ++current_line;
1054
1055 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001056 * An ":endwhile", ":endfor" and ":continue" is handled here.
1057 * If we were executing commands, jump back to the ":while" or
1058 * ":for".
1059 * If we were not executing commands, decrement cs_looplevel.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001060 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001061 if (cstack.cs_lflags & (CSL_HAD_CONT | CSL_HAD_ENDLOOP))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001062 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001063 cstack.cs_lflags &= ~(CSL_HAD_CONT | CSL_HAD_ENDLOOP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001064
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001065 // Jump back to the matching ":while" or ":for". Be careful
1066 // not to use a cs_line[] from an entry that isn't a ":while"
1067 // or ":for": It would make "current_line" invalid and can
1068 // cause a crash.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001069 if (!did_emsg && !got_int && !did_throw
1070 && cstack.cs_idx >= 0
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001071 && (cstack.cs_flags[cstack.cs_idx]
1072 & (CSF_WHILE | CSF_FOR))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001073 && cstack.cs_line[cstack.cs_idx] >= 0
1074 && (cstack.cs_flags[cstack.cs_idx] & CSF_ACTIVE))
1075 {
1076 current_line = cstack.cs_line[cstack.cs_idx];
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001077 // remember we jumped there
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001078 cstack.cs_lflags |= CSL_HAD_LOOP;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001079 line_breakcheck(); // check if CTRL-C typed
Bram Moolenaar071d4272004-06-13 20:20:40 +00001080
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001081 // Check for the next breakpoint at or after the ":while"
1082 // or ":for".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001083 if (breakpoint != NULL)
1084 {
1085 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001086 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001087 fname,
1088 ((wcmd_T *)lines_ga.ga_data)[current_line].lnum-1);
1089 *dbg_tick = debug_tick;
1090 }
1091 }
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001092 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001093 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001094 // can only get here with ":endwhile" or ":endfor"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001095 if (cstack.cs_idx >= 0)
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001096 rewind_conditionals(&cstack, cstack.cs_idx - 1,
1097 CSF_WHILE | CSF_FOR, &cstack.cs_looplevel);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001098 }
1099 }
1100
1101 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001102 * For a ":while" or ":for" we need to remember the line number.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001103 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001104 else if (cstack.cs_lflags & CSL_HAD_LOOP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001105 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001106 cstack.cs_lflags &= ~CSL_HAD_LOOP;
Bram Moolenaard5053d02020-06-28 15:51:16 +02001107 cstack.cs_line[cstack.cs_idx] = current_line_before;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001108 }
1109 }
1110
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001111 // Check for the next breakpoint after a watchexpression
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01001112 if (breakpoint != NULL && has_watchexpr())
1113 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001114 *breakpoint = dbg_find_breakpoint(FALSE, fname, SOURCING_LNUM);
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01001115 *dbg_tick = debug_tick;
1116 }
1117
Bram Moolenaar071d4272004-06-13 20:20:40 +00001118 /*
1119 * When not inside any ":while" loop, clear remembered lines.
1120 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001121 if (cstack.cs_looplevel == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001122 {
1123 if (lines_ga.ga_len > 0)
1124 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001125 SOURCING_LNUM =
Bram Moolenaar071d4272004-06-13 20:20:40 +00001126 ((wcmd_T *)lines_ga.ga_data)[lines_ga.ga_len - 1].lnum;
1127 free_cmdlines(&lines_ga);
1128 }
1129 current_line = 0;
1130 }
1131
1132 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001133 * A ":finally" makes did_emsg, got_int, and did_throw pending for
1134 * being restored at the ":endtry". Reset them here and set the
1135 * ACTIVE and FINALLY flags, so that the finally clause gets executed.
1136 * This includes the case where a missing ":endif", ":endwhile" or
1137 * ":endfor" was detected by the ":finally" itself.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001138 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001139 if (cstack.cs_lflags & CSL_HAD_FINA)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001140 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001141 cstack.cs_lflags &= ~CSL_HAD_FINA;
1142 report_make_pending(cstack.cs_pending[cstack.cs_idx]
1143 & (CSTP_ERROR | CSTP_INTERRUPT | CSTP_THROW),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001144 did_throw ? (void *)current_exception : NULL);
1145 did_emsg = got_int = did_throw = FALSE;
1146 cstack.cs_flags[cstack.cs_idx] |= CSF_ACTIVE | CSF_FINALLY;
1147 }
1148
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001149 // Update global "trylevel" for recursive calls to do_cmdline() from
1150 // within this loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001151 trylevel = initial_trylevel + cstack.cs_trylevel;
1152
1153 /*
Bram Moolenaarc1762cc2007-05-10 16:56:30 +00001154 * If the outermost try conditional (across function calls and sourced
Bram Moolenaar071d4272004-06-13 20:20:40 +00001155 * files) is aborted because of an error, an interrupt, or an uncaught
1156 * exception, cancel everything. If it is left normally, reset
1157 * force_abort to get the non-EH compatible abortion behavior for
1158 * the rest of the script.
1159 */
1160 if (trylevel == 0 && !did_emsg && !got_int && !did_throw)
1161 force_abort = FALSE;
1162
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001163 // Convert an interrupt to an exception if appropriate.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001164 (void)do_intthrow(&cstack);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001165#endif // FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001166
1167 }
1168 /*
1169 * Continue executing command lines when:
1170 * - no CTRL-C typed, no aborting error, no exception thrown or try
1171 * conditionals need to be checked for executing finally clauses or
1172 * catching an interrupt exception
1173 * - didn't get an error message or lines are not typed
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001174 * - there is a command after '|', inside a :if, :while, :for or :try, or
Bram Moolenaar071d4272004-06-13 20:20:40 +00001175 * looping for ":source" command or function call.
1176 */
1177 while (!((got_int
1178#ifdef FEAT_EVAL
1179 || (did_emsg && force_abort) || did_throw
1180#endif
1181 )
1182#ifdef FEAT_EVAL
1183 && cstack.cs_trylevel == 0
1184#endif
1185 )
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001186 && !(did_emsg
1187#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001188 // Keep going when inside try/catch, so that the error can be
1189 // deal with, except when it is a syntax error, it may cause
1190 // the :endtry to be missed.
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001191 && (cstack.cs_trylevel == 0 || did_emsg_syntax)
1192#endif
1193 && used_getline
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001194 && (getline_equal(fgetline, cookie, getexmodeline)
1195 || getline_equal(fgetline, cookie, getexline)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001196 && (next_cmdline != NULL
1197#ifdef FEAT_EVAL
1198 || cstack.cs_idx >= 0
1199#endif
1200 || (flags & DOCMD_REPEAT)));
1201
1202 vim_free(cmdline_copy);
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001203 did_emsg_syntax = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001204#ifdef FEAT_EVAL
1205 free_cmdlines(&lines_ga);
1206 ga_clear(&lines_ga);
1207
1208 if (cstack.cs_idx >= 0)
1209 {
1210 /*
1211 * If a sourced file or executed function ran to its end, report the
1212 * unclosed conditional.
1213 */
1214 if (!got_int && !did_throw
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001215 && ((getline_equal(fgetline, cookie, getsourceline)
1216 && !source_finished(fgetline, cookie))
1217 || (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001218 && !func_has_ended(real_cookie))))
1219 {
1220 if (cstack.cs_flags[cstack.cs_idx] & CSF_TRY)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001221 emsg(_(e_endtry));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001222 else if (cstack.cs_flags[cstack.cs_idx] & CSF_WHILE)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001223 emsg(_(e_endwhile));
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001224 else if (cstack.cs_flags[cstack.cs_idx] & CSF_FOR)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001225 emsg(_(e_endfor));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001226 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001227 emsg(_(e_endif));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001228 }
1229
1230 /*
1231 * Reset "trylevel" in case of a ":finish" or ":return" or a missing
1232 * ":endtry" in a sourced file or executed function. If the try
1233 * conditional is in its finally clause, ignore anything pending.
1234 * If it is in a catch clause, finish the caught exception.
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001235 * Also cleanup any "cs_forinfo" structures.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001236 */
1237 do
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001238 {
1239 int idx = cleanup_conditionals(&cstack, 0, TRUE);
1240
Bram Moolenaar89e5d682005-01-17 22:06:23 +00001241 if (idx >= 0)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001242 --idx; // remove try block not in its finally clause
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001243 rewind_conditionals(&cstack, idx, CSF_WHILE | CSF_FOR,
1244 &cstack.cs_looplevel);
1245 }
1246 while (cstack.cs_idx >= 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001247 trylevel = initial_trylevel;
1248 }
1249
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001250 // If a missing ":endtry", ":endwhile", ":endfor", or ":endif" or a memory
1251 // lack was reported above and the error message is to be converted to an
1252 // exception, do this now after rewinding the cstack.
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001253 do_errthrow(&cstack, getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001254 ? (char_u *)"endfunction" : (char_u *)NULL);
1255
1256 if (trylevel == 0)
1257 {
Bram Moolenaar820d55a2020-10-10 15:05:23 +02001258 // Just in case did_throw got set but current_exception wasn't.
1259 if (current_exception == NULL)
1260 did_throw = FALSE;
1261
Bram Moolenaar071d4272004-06-13 20:20:40 +00001262 /*
1263 * When an exception is being thrown out of the outermost try
1264 * conditional, discard the uncaught exception, disable the conversion
1265 * of interrupts or errors to exceptions, and ensure that no more
1266 * commands are executed.
1267 */
1268 if (did_throw)
1269 {
1270 void *p = NULL;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02001271 msglist_T *messages = NULL, *next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001272
1273 /*
1274 * If the uncaught exception is a user exception, report it as an
1275 * error. If it is an error exception, display the saved error
1276 * message now. For an interrupt exception, do nothing; the
1277 * interrupt message is given elsewhere.
1278 */
1279 switch (current_exception->type)
1280 {
1281 case ET_USER:
Bram Moolenaar9c13b352005-05-19 20:53:52 +00001282 vim_snprintf((char *)IObuff, IOSIZE,
1283 _("E605: Exception not caught: %s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001284 current_exception->value);
1285 p = vim_strsave(IObuff);
1286 break;
1287 case ET_ERROR:
1288 messages = current_exception->messages;
1289 current_exception->messages = NULL;
1290 break;
1291 case ET_INTERRUPT:
1292 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001293 }
1294
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001295 estack_push(ETYPE_EXCEPT, current_exception->throw_name,
1296 current_exception->throw_lnum);
Bram Moolenaare31ee862020-01-07 20:59:34 +01001297 ESTACK_CHECK_SETUP
Bram Moolenaar071d4272004-06-13 20:20:40 +00001298 current_exception->throw_name = NULL;
1299
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001300 discard_current_exception(); // uses IObuff if 'verbose'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001301 suppress_errthrow = TRUE;
1302 force_abort = TRUE;
1303
1304 if (messages != NULL)
1305 {
1306 do
1307 {
1308 next = messages->next;
1309 emsg(messages->msg);
1310 vim_free(messages->msg);
Bram Moolenaar5fbf3bc2020-06-01 21:13:11 +02001311 vim_free(messages->sfile);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001312 vim_free(messages);
1313 messages = next;
1314 }
1315 while (messages != NULL);
1316 }
1317 else if (p != NULL)
1318 {
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01001319 emsg(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001320 vim_free(p);
1321 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001322 vim_free(SOURCING_NAME);
Bram Moolenaare31ee862020-01-07 20:59:34 +01001323 ESTACK_CHECK_NOW
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001324 estack_pop();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001325 }
1326
1327 /*
1328 * On an interrupt or an aborting error not converted to an exception,
1329 * disable the conversion of errors to exceptions. (Interrupts are not
Bram Moolenaar2196bce2020-04-12 20:01:11 +02001330 * converted anymore, here.) This enables also the interrupt message
Bram Moolenaar071d4272004-06-13 20:20:40 +00001331 * when force_abort is set and did_emsg unset in case of an interrupt
1332 * from a finally clause after an error.
1333 */
1334 else if (got_int || (did_emsg && force_abort))
1335 suppress_errthrow = TRUE;
1336 }
1337
1338 /*
1339 * The current cstack will be freed when do_cmdline() returns. An uncaught
1340 * exception will have to be rethrown in the previous cstack. If a function
1341 * has just returned or a script file was just finished and the previous
1342 * cstack belongs to the same function or, respectively, script file, it
1343 * will have to be checked for finally clauses to be executed due to the
1344 * ":return" or ":finish". This is done in do_one_cmd().
1345 */
1346 if (did_throw)
1347 need_rethrow = TRUE;
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001348 if ((getline_equal(fgetline, cookie, getsourceline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001349 && ex_nesting_level > source_level(real_cookie))
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001350 || (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001351 && ex_nesting_level > func_level(real_cookie) + 1))
1352 {
1353 if (!did_throw)
1354 check_cstack = TRUE;
1355 }
1356 else
1357 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001358 // When leaving a function, reduce nesting level.
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001359 if (getline_equal(fgetline, cookie, get_func_line))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001360 --ex_nesting_level;
1361 /*
1362 * Go to debug mode when returning from a function in which we are
1363 * single-stepping.
1364 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001365 if ((getline_equal(fgetline, cookie, getsourceline)
1366 || getline_equal(fgetline, cookie, get_func_line))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001367 && ex_nesting_level + 1 <= debug_break_level)
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001368 do_debug(getline_equal(fgetline, cookie, getsourceline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001369 ? (char_u *)_("End of sourced file")
1370 : (char_u *)_("End of function"));
1371 }
1372
1373 /*
1374 * Restore the exception environment (done after returning from the
1375 * debugger).
1376 */
1377 if (flags & DOCMD_EXCRESET)
Bram Moolenaared203462004-06-16 11:19:22 +00001378 restore_dbg_stuff(&debug_saved);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001379
1380 msg_list = saved_msg_list;
Bram Moolenaar292b90d2020-03-18 15:23:16 +01001381
1382 // Cleanup if "cs_emsg_silent_list" remains.
1383 if (cstack.cs_emsg_silent_list != NULL)
1384 {
1385 eslist_T *elem, *temp;
1386
1387 for (elem = cstack.cs_emsg_silent_list; elem != NULL; elem = temp)
1388 {
1389 temp = elem->next;
1390 vim_free(elem);
1391 }
1392 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001393#endif // FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001394
1395 /*
1396 * If there was too much output to fit on the command line, ask the user to
1397 * hit return before redrawing the screen. With the ":global" command we do
1398 * this only once after the command is finished.
1399 */
1400 if (did_inc)
1401 {
1402 --RedrawingDisabled;
1403 --no_wait_return;
1404 msg_scroll = FALSE;
1405
1406 /*
1407 * When just finished an ":if"-":else" which was typed, no need to
1408 * wait for hit-return. Also for an error situation.
1409 */
1410 if (retval == FAIL
1411#ifdef FEAT_EVAL
1412 || (did_endif && KeyTyped && !did_emsg)
1413#endif
1414 )
1415 {
1416 need_wait_return = FALSE;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001417 msg_didany = FALSE; // don't wait when restarting edit
Bram Moolenaar071d4272004-06-13 20:20:40 +00001418 }
1419 else if (need_wait_return)
1420 {
1421 /*
1422 * The msg_start() above clears msg_didout. The wait_return we do
1423 * here should not overwrite the command that may be shown before
1424 * doing that.
1425 */
1426 msg_didout |= msg_didout_before_start;
1427 wait_return(FALSE);
1428 }
1429 }
1430
Bram Moolenaar2a942252012-11-28 23:03:07 +01001431#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001432 did_endif = FALSE; // in case do_cmdline used recursively
Bram Moolenaar2a942252012-11-28 23:03:07 +01001433#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001434 /*
1435 * Reset if_level, in case a sourced script file contains more ":if" than
1436 * ":endif" (could be ":if x | foo | endif").
1437 */
1438 if_level = 0;
Bram Moolenaar2e18a122012-11-28 19:10:54 +01001439#endif
Bram Moolenaard4ad0d42012-11-28 17:34:48 +01001440
Bram Moolenaar071d4272004-06-13 20:20:40 +00001441 --call_depth;
1442 return retval;
1443}
1444
1445#ifdef FEAT_EVAL
1446/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001447 * Obtain a line when inside a ":while" or ":for" loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001448 */
1449 static char_u *
Bram Moolenaar20b23c62020-08-20 15:25:00 +02001450get_loop_line(int c, void *cookie, int indent, getline_opt_T options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001451{
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001452 struct loop_cookie *cp = (struct loop_cookie *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001453 wcmd_T *wp;
1454 char_u *line;
1455
1456 if (cp->current_line + 1 >= cp->lines_gap->ga_len)
1457 {
1458 if (cp->repeating)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001459 return NULL; // trying to read past ":endwhile"/":endfor"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001460
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001461 // First time inside the ":while"/":for": get line normally.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001462 if (cp->getline == NULL)
Bram Moolenaar20b23c62020-08-20 15:25:00 +02001463 line = getcmdline(c, 0L, indent, options);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001464 else
Bram Moolenaar20b23c62020-08-20 15:25:00 +02001465 line = cp->getline(c, cp->cookie, indent, options);
Bram Moolenaard68071d2006-05-02 22:08:30 +00001466 if (line != NULL && store_loop_line(cp->lines_gap, line) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001467 ++cp->current_line;
1468
1469 return line;
1470 }
1471
1472 KeyTyped = FALSE;
1473 ++cp->current_line;
1474 wp = (wcmd_T *)(cp->lines_gap->ga_data) + cp->current_line;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001475 SOURCING_LNUM = wp->lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001476 return vim_strsave(wp->line);
1477}
1478
1479/*
1480 * Store a line in "gap" so that a ":while" loop can execute it again.
1481 */
1482 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001483store_loop_line(garray_T *gap, char_u *line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001484{
1485 if (ga_grow(gap, 1) == FAIL)
1486 return FAIL;
1487 ((wcmd_T *)(gap->ga_data))[gap->ga_len].line = vim_strsave(line);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001488 ((wcmd_T *)(gap->ga_data))[gap->ga_len].lnum = SOURCING_LNUM;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001489 ++gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001490 return OK;
1491}
1492
1493/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001494 * Free the lines stored for a ":while" or ":for" loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001495 */
1496 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001497free_cmdlines(garray_T *gap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001498{
1499 while (gap->ga_len > 0)
1500 {
1501 vim_free(((wcmd_T *)(gap->ga_data))[gap->ga_len - 1].line);
1502 --gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001503 }
1504}
1505#endif
1506
1507/*
Bram Moolenaar89d40322006-08-29 15:30:07 +00001508 * If "fgetline" is get_loop_line(), return TRUE if the getline it uses equals
1509 * "func". * Otherwise return TRUE when "fgetline" equals "func".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001510 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001511 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001512getline_equal(
Bram Moolenaar66250c92020-08-20 15:02:42 +02001513 char_u *(*fgetline)(int, void *, int, getline_opt_T),
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001514 void *cookie UNUSED, // argument for fgetline()
Bram Moolenaar66250c92020-08-20 15:02:42 +02001515 char_u *(*func)(int, void *, int, getline_opt_T))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001516{
1517#ifdef FEAT_EVAL
Bram Moolenaar20b23c62020-08-20 15:25:00 +02001518 char_u *(*gp)(int, void *, int, getline_opt_T);
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001519 struct loop_cookie *cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001520
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001521 // When "fgetline" is "get_loop_line()" use the "cookie" to find the
1522 // function that's originally used to obtain the lines. This may be
1523 // nested several levels.
Bram Moolenaar89d40322006-08-29 15:30:07 +00001524 gp = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001525 cp = (struct loop_cookie *)cookie;
1526 while (gp == get_loop_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001527 {
1528 gp = cp->getline;
1529 cp = cp->cookie;
1530 }
1531 return gp == func;
1532#else
Bram Moolenaar89d40322006-08-29 15:30:07 +00001533 return fgetline == func;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001534#endif
1535}
1536
Bram Moolenaar071d4272004-06-13 20:20:40 +00001537/*
Bram Moolenaar89d40322006-08-29 15:30:07 +00001538 * If "fgetline" is get_loop_line(), return the cookie used by the original
Bram Moolenaar071d4272004-06-13 20:20:40 +00001539 * getline function. Otherwise return "cookie".
1540 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001541 void *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001542getline_cookie(
Bram Moolenaar66250c92020-08-20 15:02:42 +02001543 char_u *(*fgetline)(int, void *, int, getline_opt_T) UNUSED,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001544 void *cookie) // argument for fgetline()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001545{
Bram Moolenaar13505972019-01-24 15:04:48 +01001546#ifdef FEAT_EVAL
Bram Moolenaar20b23c62020-08-20 15:25:00 +02001547 char_u *(*gp)(int, void *, int, getline_opt_T);
Bram Moolenaard5053d02020-06-28 15:51:16 +02001548 struct loop_cookie *cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001549
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001550 // When "fgetline" is "get_loop_line()" use the "cookie" to find the
1551 // cookie that's originally used to obtain the lines. This may be nested
1552 // several levels.
Bram Moolenaar89d40322006-08-29 15:30:07 +00001553 gp = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001554 cp = (struct loop_cookie *)cookie;
1555 while (gp == get_loop_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001556 {
1557 gp = cp->getline;
1558 cp = cp->cookie;
1559 }
1560 return cp;
Bram Moolenaar13505972019-01-24 15:04:48 +01001561#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001562 return cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001563#endif
Bram Moolenaar13505972019-01-24 15:04:48 +01001564}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001565
Bram Moolenaard5053d02020-06-28 15:51:16 +02001566#if defined(FEAT_EVAL) || defined(PROT)
1567/*
1568 * Get the next line source line without advancing.
1569 */
1570 char_u *
1571getline_peek(
Bram Moolenaar66250c92020-08-20 15:02:42 +02001572 char_u *(*fgetline)(int, void *, int, getline_opt_T) UNUSED,
Bram Moolenaard5053d02020-06-28 15:51:16 +02001573 void *cookie) // argument for fgetline()
1574{
Bram Moolenaar20b23c62020-08-20 15:25:00 +02001575 char_u *(*gp)(int, void *, int, getline_opt_T);
Bram Moolenaard5053d02020-06-28 15:51:16 +02001576 struct loop_cookie *cp;
1577 wcmd_T *wp;
1578
1579 // When "fgetline" is "get_loop_line()" use the "cookie" to find the
1580 // cookie that's originally used to obtain the lines. This may be nested
1581 // several levels.
1582 gp = fgetline;
1583 cp = (struct loop_cookie *)cookie;
1584 while (gp == get_loop_line)
1585 {
1586 if (cp->current_line + 1 < cp->lines_gap->ga_len)
1587 {
1588 // executing lines a second time, use the stored copy
1589 wp = (wcmd_T *)(cp->lines_gap->ga_data) + cp->current_line + 1;
1590 return wp->line;
1591 }
1592 gp = cp->getline;
1593 cp = cp->cookie;
1594 }
1595 if (gp == getsourceline)
1596 return source_nextline(cp);
1597 return NULL;
1598}
1599#endif
1600
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001601
1602/*
1603 * Helper function to apply an offset for buffer commands, i.e. ":bdelete",
1604 * ":bwipeout", etc.
1605 * Returns the buffer number.
1606 */
1607 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001608compute_buffer_local_count(int addr_type, int lnum, int offset)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001609{
1610 buf_T *buf;
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001611 buf_T *nextbuf;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001612 int count = offset;
1613
1614 buf = firstbuf;
1615 while (buf->b_next != NULL && buf->b_fnum < lnum)
1616 buf = buf->b_next;
1617 while (count != 0)
1618 {
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001619 count += (offset < 0) ? 1 : -1;
1620 nextbuf = (offset < 0) ? buf->b_prev : buf->b_next;
1621 if (nextbuf == NULL)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001622 break;
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001623 buf = nextbuf;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001624 if (addr_type == ADDR_LOADED_BUFFERS)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001625 // skip over unloaded buffers
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001626 while (buf->b_ml.ml_mfp == NULL)
1627 {
1628 nextbuf = (offset < 0) ? buf->b_prev : buf->b_next;
1629 if (nextbuf == NULL)
1630 break;
1631 buf = nextbuf;
1632 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001633 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001634 // we might have gone too far, last buffer is not loadedd
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001635 if (addr_type == ADDR_LOADED_BUFFERS)
1636 while (buf->b_ml.ml_mfp == NULL)
1637 {
1638 nextbuf = (offset >= 0) ? buf->b_prev : buf->b_next;
1639 if (nextbuf == NULL)
1640 break;
1641 buf = nextbuf;
1642 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001643 return buf->b_fnum;
1644}
1645
Bram Moolenaarb7316892019-05-01 18:08:42 +02001646/*
1647 * Return the window number of "win".
1648 * When "win" is NULL return the number of windows.
1649 */
Bram Moolenaarf240e182014-11-27 18:33:02 +01001650 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001651current_win_nr(win_T *win)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001652{
1653 win_T *wp;
1654 int nr = 0;
1655
Bram Moolenaar29323592016-07-24 22:04:11 +02001656 FOR_ALL_WINDOWS(wp)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001657 {
1658 ++nr;
1659 if (wp == win)
1660 break;
1661 }
1662 return nr;
1663}
1664
1665 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001666current_tab_nr(tabpage_T *tab)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001667{
1668 tabpage_T *tp;
1669 int nr = 0;
1670
Bram Moolenaar29323592016-07-24 22:04:11 +02001671 FOR_ALL_TABPAGES(tp)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001672 {
1673 ++nr;
1674 if (tp == tab)
1675 break;
1676 }
1677 return nr;
1678}
1679
Bram Moolenaar6e36b1c2020-07-17 20:47:51 +02001680 static int
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001681comment_start(char_u *p, int starts_with_colon UNUSED)
1682{
1683#ifdef FEAT_EVAL
1684 if (in_vim9script())
Bram Moolenaar93f82cb2020-12-12 21:25:56 +01001685 return p[0] == '#' && !starts_with_colon;
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001686#endif
1687 return *p == '"';
1688}
1689
Bram Moolenaarf240e182014-11-27 18:33:02 +01001690# define CURRENT_WIN_NR current_win_nr(curwin)
1691# define LAST_WIN_NR current_win_nr(NULL)
1692# define CURRENT_TAB_NR current_tab_nr(curtab)
1693# define LAST_TAB_NR current_tab_nr(NULL)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001694
Bram Moolenaar071d4272004-06-13 20:20:40 +00001695/*
1696 * Execute one Ex command.
1697 *
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01001698 * If "flags" has DOCMD_VERBOSE, the command will be included in the error
1699 * message.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001700 *
1701 * 1. skip comment lines and leading space
1702 * 2. handle command modifiers
Bram Moolenaar1c40a662014-11-27 16:38:11 +01001703 * 3. find the command
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001704 * 4. parse range
Bram Moolenaar1c40a662014-11-27 16:38:11 +01001705 * 5. Parse the command.
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001706 * 6. parse arguments
1707 * 7. switch on command name
Bram Moolenaar071d4272004-06-13 20:20:40 +00001708 *
Bram Moolenaar89d40322006-08-29 15:30:07 +00001709 * Note: "fgetline" can be NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001710 *
1711 * This function may be called recursively!
1712 */
1713#if (_MSC_VER == 1200)
1714/*
Bram Moolenaared203462004-06-16 11:19:22 +00001715 * Avoid optimisation bug in VC++ version 6.0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001716 */
Bram Moolenaar281bdce2005-01-25 21:53:18 +00001717 #pragma optimize( "g", off )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001718#endif
1719 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001720do_one_cmd(
Bram Moolenaarddef1292019-12-16 17:10:33 +01001721 char_u **cmdlinep,
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01001722 int flags,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001723#ifdef FEAT_EVAL
Bram Moolenaarddef1292019-12-16 17:10:33 +01001724 cstack_T *cstack,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001725#endif
Bram Moolenaar20b23c62020-08-20 15:25:00 +02001726 char_u *(*fgetline)(int, void *, int, getline_opt_T),
Bram Moolenaarddef1292019-12-16 17:10:33 +01001727 void *cookie) // argument for fgetline()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001728{
Bram Moolenaarddef1292019-12-16 17:10:33 +01001729 char_u *p;
1730 linenr_T lnum;
1731 long n;
1732 char *errormsg = NULL; // error message
1733 char_u *after_modifier = NULL;
1734 exarg_T ea; // Ex command arguments
Bram Moolenaarddef1292019-12-16 17:10:33 +01001735 cmdmod_T save_cmdmod;
1736 int save_reg_executing = reg_executing;
1737 int ni; // set when Not Implemented
1738 char_u *cmd;
Bram Moolenaar6e36b1c2020-07-17 20:47:51 +02001739 int starts_with_colon = FALSE;
Bram Moolenaar83f37b92020-02-23 14:35:01 +01001740#ifdef FEAT_EVAL
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02001741 int may_have_range;
Bram Moolenaar3d48e252020-07-15 14:15:52 +02001742 int vim9script = in_vim9script();
Bram Moolenaarb4bcea42020-10-28 13:53:50 +01001743 int did_set_expr_line = FALSE;
Bram Moolenaar83f37b92020-02-23 14:35:01 +01001744#endif
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01001745 int sourcing = flags & DOCMD_VERBOSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001746
Bram Moolenaara80faa82020-04-12 19:37:17 +02001747 CLEAR_FIELD(ea);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001748 ea.line1 = 1;
1749 ea.line2 = 1;
1750#ifdef FEAT_EVAL
1751 ++ex_nesting_level;
1752#endif
1753
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001754 // When the last file has not been edited :q has to be typed twice.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001755 if (quitmore
1756#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001757 // avoid that a function call in 'statusline' does this
Bram Moolenaar89d40322006-08-29 15:30:07 +00001758 && !getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001759#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001760 // avoid that an autocommand, e.g. QuitPre, does this
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001761 && !getline_equal(fgetline, cookie, getnextac))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001762 --quitmore;
1763
1764 /*
1765 * Reset browse, confirm, etc.. They are restored when returning, for
1766 * recursive calls.
1767 */
1768 save_cmdmod = cmdmod;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001769
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001770 // "#!anything" is handled like a comment.
Bram Moolenaarcbb37ad2006-08-16 15:04:21 +00001771 if ((*cmdlinep)[0] == '#' && (*cmdlinep)[1] == '!')
1772 goto doend;
1773
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02001774/*
1775 * 1. Skip comment lines and leading white space and colons.
1776 * 2. Handle command modifiers.
1777 */
1778 // The "ea" structure holds the arguments that can be used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001779 ea.cmd = *cmdlinep;
Bram Moolenaareffed932018-08-14 13:38:17 +02001780 ea.cmdlinep = cmdlinep;
1781 ea.getline = fgetline;
1782 ea.cookie = cookie;
1783#ifdef FEAT_EVAL
1784 ea.cstack = cstack;
Bram Moolenaar83f37b92020-02-23 14:35:01 +01001785 starts_with_colon = *skipwhite(ea.cmd) == ':';
Bram Moolenaar071d4272004-06-13 20:20:40 +00001786#endif
Bram Moolenaare1004402020-10-24 20:49:43 +02001787 if (parse_command_modifiers(&ea, &errormsg, &cmdmod, FALSE) == FAIL)
Bram Moolenaareffed932018-08-14 13:38:17 +02001788 goto doend;
Bram Moolenaar5661ed62020-10-24 17:19:16 +02001789 apply_cmdmod(&cmdmod);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001790
Bram Moolenaar7b668e82016-08-23 23:51:21 +02001791 after_modifier = ea.cmd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001792
1793#ifdef FEAT_EVAL
1794 ea.skip = did_emsg || got_int || did_throw || (cstack->cs_idx >= 0
1795 && !(cstack->cs_flags[cstack->cs_idx] & CSF_ACTIVE));
1796#else
1797 ea.skip = (if_level > 0);
1798#endif
1799
Bram Moolenaar071d4272004-06-13 20:20:40 +00001800/*
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001801 * 3. Skip over the range to find the command. Let "p" point to after it.
1802 *
1803 * We need the command to know what kind of range it uses.
1804 */
1805 cmd = ea.cmd;
Bram Moolenaar3d48e252020-07-15 14:15:52 +02001806#ifdef FEAT_EVAL
Bram Moolenaare88c8e82020-11-01 17:03:37 +01001807 // In Vim9 script a colon is required before the range. This may also be
1808 // after command modifiers.
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01001809 if (vim9script && (flags & DOCMD_RANGEOK) == 0)
Bram Moolenaare88c8e82020-11-01 17:03:37 +01001810 {
1811 may_have_range = FALSE;
1812 for (p = ea.cmd; p >= *cmdlinep; --p)
1813 {
1814 if (*p == ':')
1815 may_have_range = TRUE;
1816 if (p < ea.cmd && !VIM_ISWHITE(*p))
1817 break;
1818 }
1819 }
1820 else
1821 may_have_range = TRUE;
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02001822 if (may_have_range)
Bram Moolenaar3d48e252020-07-15 14:15:52 +02001823#endif
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02001824 ea.cmd = skip_range(ea.cmd, TRUE, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001825
1826#ifdef FEAT_EVAL
Bram Moolenaare88c8e82020-11-01 17:03:37 +01001827 if (vim9script && !may_have_range)
Bram Moolenaardf069ee2020-06-22 23:02:51 +02001828 {
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02001829 if (ea.cmd == cmd + 1 && *cmd == '$')
1830 // should be "$VAR = val"
1831 --ea.cmd;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001832 p = find_ex_command(&ea, NULL, lookup_scriptvar, NULL);
Bram Moolenaar36113e42020-11-02 21:08:47 +01001833 if (ea.cmdidx == CMD_SIZE)
1834 {
1835 char_u *ar = skip_range(ea.cmd, TRUE, NULL);
1836
1837 // If a ':' before the range is missing, give a clearer error
1838 // message.
1839 if (ar > ea.cmd)
1840 {
Bram Moolenaar6e2c2c52020-12-25 19:25:45 +01001841 semsg(_(e_colon_required_before_range_str), ea.cmd);
Bram Moolenaar36113e42020-11-02 21:08:47 +01001842 goto doend;
1843 }
1844 }
Bram Moolenaardf069ee2020-06-22 23:02:51 +02001845 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001846 else
1847#endif
1848 p = find_ex_command(&ea, NULL, NULL, NULL);
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001849
Bram Moolenaar7feb35e2018-08-21 17:49:54 +02001850#ifdef FEAT_EVAL
1851# ifdef FEAT_PROFILE
1852 // Count this line for profiling if skip is TRUE.
1853 if (do_profiling == PROF_YES
1854 && (!ea.skip || cstack->cs_idx == 0 || (cstack->cs_idx > 0
1855 && (cstack->cs_flags[cstack->cs_idx - 1] & CSF_ACTIVE))))
1856 {
1857 int skip = did_emsg || got_int || did_throw;
1858
1859 if (ea.cmdidx == CMD_catch)
1860 skip = !skip && !(cstack->cs_idx >= 0
1861 && (cstack->cs_flags[cstack->cs_idx] & CSF_THROWN)
1862 && !(cstack->cs_flags[cstack->cs_idx] & CSF_CAUGHT));
1863 else if (ea.cmdidx == CMD_else || ea.cmdidx == CMD_elseif)
1864 skip = skip || !(cstack->cs_idx >= 0
1865 && !(cstack->cs_flags[cstack->cs_idx]
1866 & (CSF_ACTIVE | CSF_TRUE)));
1867 else if (ea.cmdidx == CMD_finally)
1868 skip = FALSE;
1869 else if (ea.cmdidx != CMD_endif
1870 && ea.cmdidx != CMD_endfor
1871 && ea.cmdidx != CMD_endtry
1872 && ea.cmdidx != CMD_endwhile)
1873 skip = ea.skip;
1874
1875 if (!skip)
1876 {
1877 if (getline_equal(fgetline, cookie, get_func_line))
1878 func_line_exec(getline_cookie(fgetline, cookie));
1879 else if (getline_equal(fgetline, cookie, getsourceline))
1880 script_line_exec();
1881 }
1882 }
1883# endif
1884
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001885 // May go to debug mode. If this happens and the ">quit" debug command is
1886 // used, throw an interrupt exception and skip the next command.
Bram Moolenaar7feb35e2018-08-21 17:49:54 +02001887 dbg_check_breakpoint(&ea);
1888 if (!ea.skip && got_int)
1889 {
1890 ea.skip = TRUE;
1891 (void)do_intthrow(cstack);
1892 }
1893#endif
1894
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001895/*
1896 * 4. parse a range specifier of the form: addr [,addr] [;addr] ..
Bram Moolenaar071d4272004-06-13 20:20:40 +00001897 *
1898 * where 'addr' is:
1899 *
1900 * % (entire file)
1901 * $ [+-NUM]
1902 * 'x [+-NUM] (where x denotes a currently defined mark)
1903 * . [+-NUM]
1904 * [+-NUM]..
1905 * NUM
1906 *
1907 * The ea.cmd pointer is updated to point to the first character following the
1908 * range spec. If an initial address is found, but no second, the upper bound
1909 * is equal to the lower.
1910 */
1911
Bram Moolenaar25190db2019-05-04 15:05:28 +02001912 // ea.addr_type for user commands is set by find_ucmd
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01001913 if (!IS_USER_CMDIDX(ea.cmdidx))
1914 {
1915 if (ea.cmdidx != CMD_SIZE)
1916 ea.addr_type = cmdnames[(int)ea.cmdidx].cmd_addr_type;
1917 else
1918 ea.addr_type = ADDR_LINES;
1919
Bram Moolenaar25190db2019-05-04 15:05:28 +02001920 // :wincmd range depends on the argument.
Bram Moolenaar164f3262015-01-14 21:22:01 +01001921 if (ea.cmdidx == CMD_wincmd && p != NULL)
1922 get_wincmd_addr_type(skipwhite(p), &ea);
Bram Moolenaar25190db2019-05-04 15:05:28 +02001923#ifdef FEAT_QUICKFIX
1924 // :.cc in quickfix window uses line number
1925 if ((ea.cmdidx == CMD_cc || ea.cmdidx == CMD_ll) && bt_quickfix(curbuf))
1926 ea.addr_type = ADDR_OTHER;
1927#endif
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01001928 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001929
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01001930 ea.cmd = cmd;
Bram Moolenaar3d48e252020-07-15 14:15:52 +02001931#ifdef FEAT_EVAL
Bram Moolenaarc2af0af2020-08-23 21:06:02 +02001932 if (!may_have_range)
1933 ea.line1 = ea.line2 = default_address(&ea);
1934 else
Bram Moolenaar3d48e252020-07-15 14:15:52 +02001935#endif
1936 if (parse_cmd_address(&ea, &errormsg, FALSE) == FAIL)
1937 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001938
Bram Moolenaar071d4272004-06-13 20:20:40 +00001939/*
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001940 * 5. Parse the command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001941 */
1942
1943 /*
1944 * Skip ':' and any white space
1945 */
1946 ea.cmd = skipwhite(ea.cmd);
1947 while (*ea.cmd == ':')
1948 ea.cmd = skipwhite(ea.cmd + 1);
1949
1950 /*
1951 * If we got a line, but no command, then go to the line.
1952 * If we find a '|' or '\n' we set ea.nextcmd.
1953 */
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001954 if (*ea.cmd == NUL || comment_start(ea.cmd, starts_with_colon)
1955 || (ea.nextcmd = check_nextcmd(ea.cmd)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001956 {
1957 /*
1958 * strange vi behaviour:
1959 * ":3" jumps to line 3
1960 * ":3|..." prints line 3
1961 * ":|" prints current line
1962 */
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001963 if (ea.skip) // skip this if inside :if
Bram Moolenaar071d4272004-06-13 20:20:40 +00001964 goto doend;
Bram Moolenaardf177f62005-02-22 08:39:57 +00001965 if (*ea.cmd == '|' || (exmode_active && ea.line1 != ea.line2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001966 {
1967 ea.cmdidx = CMD_print;
Bram Moolenaar8071cb22019-07-12 17:58:01 +02001968 ea.argt = EX_RANGE+EX_COUNT+EX_TRLBAR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001969 if ((errormsg = invalid_range(&ea)) == NULL)
1970 {
1971 correct_range(&ea);
1972 ex_print(&ea);
1973 }
1974 }
1975 else if (ea.addr_count != 0)
1976 {
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00001977 if (ea.line2 > curbuf->b_ml.ml_line_count)
1978 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001979 // With '-' in 'cpoptions' a line number past the file is an
1980 // error, otherwise put it at the end of the file.
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00001981 if (vim_strchr(p_cpo, CPO_MINUS) != NULL)
1982 ea.line2 = -1;
1983 else
1984 ea.line2 = curbuf->b_ml.ml_line_count;
1985 }
1986
1987 if (ea.line2 < 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001988 errormsg = _(e_invrange);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001989 else
1990 {
1991 if (ea.line2 == 0)
1992 curwin->w_cursor.lnum = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001993 else
1994 curwin->w_cursor.lnum = ea.line2;
1995 beginline(BL_SOL | BL_FIX);
1996 }
1997 }
1998 goto doend;
1999 }
2000
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002001 // If this looks like an undefined user command and there are CmdUndefined
2002 // autocommands defined, trigger the matching autocommands.
Bram Moolenaard5005162014-08-22 23:05:54 +02002003 if (p != NULL && ea.cmdidx == CMD_SIZE && !ea.skip
2004 && ASCII_ISUPPER(*ea.cmd)
2005 && has_cmdundefined())
2006 {
Bram Moolenaard5005162014-08-22 23:05:54 +02002007 int ret;
2008
Bram Moolenaar5a31b462014-08-23 14:16:20 +02002009 p = ea.cmd;
Bram Moolenaard5005162014-08-22 23:05:54 +02002010 while (ASCII_ISALNUM(*p))
2011 ++p;
Bram Moolenaar158ea172020-06-18 17:28:39 +02002012 p = vim_strnsave(ea.cmd, p - ea.cmd);
Bram Moolenaard5005162014-08-22 23:05:54 +02002013 ret = apply_autocmds(EVENT_CMDUNDEFINED, p, p, TRUE, NULL);
2014 vim_free(p);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002015 // If the autocommands did something and didn't cause an error, try
2016 // finding the command again.
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002017 p = (ret
2018#ifdef FEAT_EVAL
2019 && !aborting()
Bram Moolenaard5005162014-08-22 23:05:54 +02002020#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002021 ) ? find_ex_command(&ea, NULL, NULL, NULL) : ea.cmd;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002022 }
Bram Moolenaard5005162014-08-22 23:05:54 +02002023
Bram Moolenaar071d4272004-06-13 20:20:40 +00002024 if (p == NULL)
2025 {
2026 if (!ea.skip)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002027 errormsg = _("E464: Ambiguous use of user-defined command");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002028 goto doend;
2029 }
Bram Moolenaarac9fb182019-04-27 13:04:13 +02002030 // Check for wrong commands.
Bram Moolenaar6f9a4762017-06-22 20:39:17 +02002031 if (*p == '!' && ea.cmd[1] == 0151 && ea.cmd[0] == 78
2032 && !IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002033 {
2034 errormsg = uc_fun_cmd();
2035 goto doend;
2036 }
Bram Moolenaarac9fb182019-04-27 13:04:13 +02002037
Bram Moolenaar071d4272004-06-13 20:20:40 +00002038 if (ea.cmdidx == CMD_SIZE)
2039 {
2040 if (!ea.skip)
2041 {
2042 STRCPY(IObuff, _("E492: Not an editor command"));
2043 if (!sourcing)
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002044 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002045 // If the modifier was parsed OK the error must be in the
2046 // following command
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002047 if (after_modifier != NULL)
2048 append_command(after_modifier);
2049 else
2050 append_command(*cmdlinep);
2051 }
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002052 errormsg = (char *)IObuff;
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02002053 did_emsg_syntax = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002054 }
2055 goto doend;
2056 }
2057
Bram Moolenaar958636c2014-10-21 20:01:58 +02002058 ni = (!IS_USER_CMDIDX(ea.cmdidx)
2059 && (cmdnames[ea.cmdidx].cmd_func == ex_ni
Bram Moolenaar7bb75552007-07-16 18:39:49 +00002060#ifdef HAVE_EX_SCRIPT_NI
2061 || cmdnames[ea.cmdidx].cmd_func == ex_script_ni
2062#endif
2063 ));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002064
2065#ifndef FEAT_EVAL
2066 /*
2067 * When the expression evaluation is disabled, recognize the ":if" and
2068 * ":endif" commands and ignore everything in between it.
2069 */
2070 if (ea.cmdidx == CMD_if)
2071 ++if_level;
2072 if (if_level)
2073 {
2074 if (ea.cmdidx == CMD_endif)
2075 --if_level;
2076 goto doend;
2077 }
2078
2079#endif
2080
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002081 // forced commands
Bram Moolenaar196b3b02008-06-20 16:51:41 +00002082 if (*p == '!' && ea.cmdidx != CMD_substitute
2083 && ea.cmdidx != CMD_smagic && ea.cmdidx != CMD_snomagic)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002084 {
2085 ++p;
2086 ea.forceit = TRUE;
2087 }
2088 else
2089 ea.forceit = FALSE;
2090
2091/*
Bram Moolenaarb7316892019-05-01 18:08:42 +02002092 * 6. Parse arguments. Then check for errors.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002093 */
Bram Moolenaar958636c2014-10-21 20:01:58 +02002094 if (!IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002095 ea.argt = (long)cmdnames[(int)ea.cmdidx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002096
2097 if (!ea.skip)
2098 {
2099#ifdef HAVE_SANDBOX
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002100 if (sandbox != 0 && !(ea.argt & EX_SBOXOK))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002101 {
Bram Moolenaar8c62a082019-02-08 14:34:10 +01002102 // Command not allowed in sandbox.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002103 errormsg = _(e_sandbox);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002104 goto doend;
2105 }
2106#endif
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002107 if (restricted != 0 && (ea.argt & EX_RESTRICT))
Bram Moolenaar8c62a082019-02-08 14:34:10 +01002108 {
2109 errormsg = _("E981: Command not allowed in rvim");
2110 goto doend;
2111 }
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002112 if (!curbuf->b_p_ma && (ea.argt & EX_MODIFY))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002113 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002114 // Command not allowed in non-'modifiable' buffer
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002115 errormsg = _(e_modifiable);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002116 goto doend;
2117 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002118
Bram Moolenaar37394ff2020-07-25 19:38:18 +02002119 if (!IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002120 {
Bram Moolenaar37394ff2020-07-25 19:38:18 +02002121#ifdef FEAT_CMDWIN
2122 if (cmdwin_type != 0 && !(ea.argt & EX_CMDWIN))
2123 {
2124 // Command not allowed in the command line window
2125 errormsg = _(e_cmdwin);
2126 goto doend;
2127 }
2128#endif
2129 if (text_locked() && !(ea.argt & EX_LOCK_OK))
2130 {
2131 // Command not allowed when text is locked
2132 errormsg = _(get_text_locked_msg());
2133 goto doend;
2134 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002135 }
Bram Moolenaar379fb762018-08-30 15:58:28 +02002136
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002137 // Disallow editing another buffer when "curbuf_lock" is set.
2138 // Do allow ":checktime" (it is postponed).
2139 // Do allow ":edit" (check for an argument later).
2140 // Do allow ":file" with no arguments (check for an argument later).
Bram Moolenaar8e1986e2020-08-06 22:11:06 +02002141 if (!(ea.argt & (EX_CMDWIN | EX_LOCK_OK))
Bram Moolenaar5555acc2006-04-07 21:33:12 +00002142 && ea.cmdidx != CMD_checktime
Bram Moolenaar379fb762018-08-30 15:58:28 +02002143 && ea.cmdidx != CMD_edit
2144 && ea.cmdidx != CMD_file
Bram Moolenaar958636c2014-10-21 20:01:58 +02002145 && !IS_USER_CMDIDX(ea.cmdidx)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002146 && curbuf_locked())
2147 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002148
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002149 if (!ni && !(ea.argt & EX_RANGE) && ea.addr_count > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002150 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002151 // no range allowed
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002152 errormsg = _(e_norange);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002153 goto doend;
2154 }
2155 }
2156
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002157 if (!ni && !(ea.argt & EX_BANG) && ea.forceit) // no <!> allowed
Bram Moolenaar071d4272004-06-13 20:20:40 +00002158 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002159 errormsg = _(e_nobang);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002160 goto doend;
2161 }
2162
2163 /*
2164 * Don't complain about the range if it is not used
2165 * (could happen if line_count is accidentally set to 0).
2166 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002167 if (!ea.skip && !ni && (ea.argt & EX_RANGE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002168 {
2169 /*
2170 * If the range is backwards, ask for confirmation and, if given, swap
2171 * ea.line1 & ea.line2 so it's forwards again.
2172 * When global command is busy, don't ask, will fail below.
2173 */
2174 if (!global_busy && ea.line1 > ea.line2)
2175 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002176 if (msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002177 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002178 if (sourcing || exmode_active)
2179 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002180 errormsg = _("E493: Backwards range given");
Bram Moolenaara5792f52005-11-23 21:25:05 +00002181 goto doend;
2182 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002183 if (ask_yesno((char_u *)
2184 _("Backwards range given, OK to swap"), FALSE) != 'y')
Bram Moolenaara5792f52005-11-23 21:25:05 +00002185 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002186 }
2187 lnum = ea.line1;
2188 ea.line1 = ea.line2;
2189 ea.line2 = lnum;
2190 }
2191 if ((errormsg = invalid_range(&ea)) != NULL)
2192 goto doend;
2193 }
2194
Bram Moolenaarb7316892019-05-01 18:08:42 +02002195 if ((ea.addr_type == ADDR_OTHER) && ea.addr_count == 0)
2196 // default is 1, not cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00002197 ea.line2 = 1;
2198
2199 correct_range(&ea);
2200
2201#ifdef FEAT_FOLDING
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002202 if (((ea.argt & EX_WHOLEFOLD) || ea.addr_count >= 2) && !global_busy
Bram Moolenaara3306952016-01-02 21:41:06 +01002203 && ea.addr_type == ADDR_LINES)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002204 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002205 // Put the first line at the start of a closed fold, put the last line
2206 // at the end of a closed fold.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002207 (void)hasFolding(ea.line1, &ea.line1, NULL);
2208 (void)hasFolding(ea.line2, NULL, &ea.line2);
2209 }
2210#endif
2211
2212#ifdef FEAT_QUICKFIX
2213 /*
Bram Moolenaar86b68352004-12-27 21:59:20 +00002214 * For the ":make" and ":grep" commands we insert the 'makeprg'/'grepprg'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002215 * option here, so things like % get expanded.
2216 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002217 p = replace_makeprg(&ea, p, cmdlinep);
2218 if (p == NULL)
2219 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002220#endif
2221
2222 /*
2223 * Skip to start of argument.
2224 * Don't do this for the ":!" command, because ":!! -l" needs the space.
2225 */
2226 if (ea.cmdidx == CMD_bang)
2227 ea.arg = p;
2228 else
2229 ea.arg = skipwhite(p);
2230
Bram Moolenaar379fb762018-08-30 15:58:28 +02002231 // ":file" cannot be run with an argument when "curbuf_lock" is set
2232 if (ea.cmdidx == CMD_file && *ea.arg != NUL && curbuf_locked())
2233 goto doend;
2234
Bram Moolenaar071d4272004-06-13 20:20:40 +00002235 /*
2236 * Check for "++opt=val" argument.
2237 * Must be first, allow ":w ++enc=utf8 !cmd"
2238 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002239 if (ea.argt & EX_ARGOPT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002240 while (ea.arg[0] == '+' && ea.arg[1] == '+')
2241 if (getargopt(&ea) == FAIL && !ni)
2242 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002243 errormsg = _(e_invarg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002244 goto doend;
2245 }
2246
2247 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
2248 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002249 if (*ea.arg == '>') // append
Bram Moolenaar071d4272004-06-13 20:20:40 +00002250 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002251 if (*++ea.arg != '>') // typed wrong
Bram Moolenaar071d4272004-06-13 20:20:40 +00002252 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002253 errormsg = _("E494: Use w or w>>");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002254 goto doend;
2255 }
2256 ea.arg = skipwhite(ea.arg + 1);
2257 ea.append = TRUE;
2258 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002259 else if (*ea.arg == '!' && ea.cmdidx == CMD_write) // :w !filter
Bram Moolenaar071d4272004-06-13 20:20:40 +00002260 {
2261 ++ea.arg;
2262 ea.usefilter = TRUE;
2263 }
2264 }
2265
2266 if (ea.cmdidx == CMD_read)
2267 {
2268 if (ea.forceit)
2269 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002270 ea.usefilter = TRUE; // :r! filter if ea.forceit
Bram Moolenaar071d4272004-06-13 20:20:40 +00002271 ea.forceit = FALSE;
2272 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002273 else if (*ea.arg == '!') // :r !filter
Bram Moolenaar071d4272004-06-13 20:20:40 +00002274 {
2275 ++ea.arg;
2276 ea.usefilter = TRUE;
2277 }
2278 }
2279
2280 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
2281 {
2282 ea.amount = 1;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002283 while (*ea.arg == *ea.cmd) // count number of '>' or '<'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002284 {
2285 ++ea.arg;
2286 ++ea.amount;
2287 }
2288 ea.arg = skipwhite(ea.arg);
2289 }
2290
2291 /*
2292 * Check for "+command" argument, before checking for next command.
2293 * Don't do this for ":read !cmd" and ":write !cmd".
2294 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002295 if ((ea.argt & EX_CMDARG) && !ea.usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002296 ea.do_ecmd_cmd = getargcmd(&ea.arg);
2297
2298 /*
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002299 * Check for '|' to separate commands and '"' or '#' to start comments.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002300 * Don't do this for ":read !cmd" and ":write !cmd".
2301 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002302 if ((ea.argt & EX_TRLBAR) && !ea.usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002303 separate_nextcmd(&ea);
2304
2305 /*
2306 * Check for <newline> to end a shell command.
Bram Moolenaardf177f62005-02-22 08:39:57 +00002307 * Also do this for ":read !cmd", ":write !cmd" and ":global".
2308 * Any others?
Bram Moolenaar071d4272004-06-13 20:20:40 +00002309 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00002310 else if (ea.cmdidx == CMD_bang
Bram Moolenaar67883b42017-07-27 22:57:00 +02002311 || ea.cmdidx == CMD_terminal
Bram Moolenaardf177f62005-02-22 08:39:57 +00002312 || ea.cmdidx == CMD_global
2313 || ea.cmdidx == CMD_vglobal
2314 || ea.usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002315 {
2316 for (p = ea.arg; *p; ++p)
2317 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002318 // Remove one backslash before a newline, so that it's possible to
2319 // pass a newline to the shell and also a newline that is preceded
2320 // with a backslash. This makes it impossible to end a shell
2321 // command in a backslash, but that doesn't appear useful.
2322 // Halving the number of backslashes is incompatible with previous
2323 // versions.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002324 if (*p == '\\' && p[1] == '\n')
Bram Moolenaara7241f52008-06-24 20:39:31 +00002325 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002326 else if (*p == '\n')
2327 {
2328 ea.nextcmd = p + 1;
2329 *p = NUL;
2330 break;
2331 }
2332 }
2333 }
2334
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002335 if ((ea.argt & EX_DFLALL) && ea.addr_count == 0)
Bram Moolenaarc2af0af2020-08-23 21:06:02 +02002336 address_default_all(&ea);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002337
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002338 // accept numbered register only when no count allowed (:put)
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002339 if ( (ea.argt & EX_REGSTR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002340 && *ea.arg != NUL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002341 // Do not allow register = for user commands
Bram Moolenaar958636c2014-10-21 20:01:58 +02002342 && (!IS_USER_CMDIDX(ea.cmdidx) || *ea.arg != '=')
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002343 && !((ea.argt & EX_COUNT) && VIM_ISDIGIT(*ea.arg)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002344 {
Bram Moolenaar85de2062011-05-05 14:26:41 +02002345#ifndef FEAT_CLIPBOARD
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002346 // check these explicitly for a more specific error message
Bram Moolenaar85de2062011-05-05 14:26:41 +02002347 if (*ea.arg == '*' || *ea.arg == '+')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002348 {
Bram Moolenaar99b12722019-01-14 20:16:40 +01002349 errormsg = _(e_invalidreg);
Bram Moolenaar85de2062011-05-05 14:26:41 +02002350 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002351 }
2352#endif
Bram Moolenaar958636c2014-10-21 20:01:58 +02002353 if (valid_yank_reg(*ea.arg, (ea.cmdidx != CMD_put
2354 && !IS_USER_CMDIDX(ea.cmdidx))))
Bram Moolenaar85de2062011-05-05 14:26:41 +02002355 {
2356 ea.regname = *ea.arg++;
2357#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002358 // for '=' register: accept the rest of the line as an expression
Bram Moolenaar85de2062011-05-05 14:26:41 +02002359 if (ea.arg[-1] == '=' && ea.arg[0] != NUL)
2360 {
Bram Moolenaarb4bcea42020-10-28 13:53:50 +01002361 set_expr_line(vim_strsave(ea.arg), &ea);
Bram Moolenaar85de2062011-05-05 14:26:41 +02002362 ea.arg += STRLEN(ea.arg);
Bram Moolenaarb4bcea42020-10-28 13:53:50 +01002363 did_set_expr_line = TRUE;
Bram Moolenaar85de2062011-05-05 14:26:41 +02002364 }
2365#endif
2366 ea.arg = skipwhite(ea.arg);
2367 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002368 }
2369
2370 /*
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002371 * Check for a count. When accepting a EX_BUFNAME, don't use "123foo" as a
Bram Moolenaar071d4272004-06-13 20:20:40 +00002372 * count, it's a buffer name.
2373 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002374 if ((ea.argt & EX_COUNT) && VIM_ISDIGIT(*ea.arg)
2375 && (!(ea.argt & EX_BUFNAME) || *(p = skipdigits(ea.arg)) == NUL
Bram Moolenaar1c465442017-03-12 20:10:05 +01002376 || VIM_ISWHITE(*p)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002377 {
2378 n = getdigits(&ea.arg);
2379 ea.arg = skipwhite(ea.arg);
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002380 if (n <= 0 && !ni && (ea.argt & EX_ZEROR) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002381 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002382 errormsg = _(e_zerocount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002383 goto doend;
2384 }
Bram Moolenaarb7316892019-05-01 18:08:42 +02002385 if (ea.addr_type != ADDR_LINES) // e.g. :buffer 2, :sleep 3
Bram Moolenaar071d4272004-06-13 20:20:40 +00002386 {
2387 ea.line2 = n;
2388 if (ea.addr_count == 0)
2389 ea.addr_count = 1;
2390 }
2391 else
2392 {
2393 ea.line1 = ea.line2;
2394 ea.line2 += n - 1;
2395 ++ea.addr_count;
2396 /*
2397 * Be vi compatible: no error message for out of range.
2398 */
Bram Moolenaarb7316892019-05-01 18:08:42 +02002399 if (ea.line2 > curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002400 ea.line2 = curbuf->b_ml.ml_line_count;
2401 }
2402 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00002403
2404 /*
2405 * Check for flags: 'l', 'p' and '#'.
2406 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002407 if (ea.argt & EX_FLAGS)
Bram Moolenaardf177f62005-02-22 08:39:57 +00002408 get_flags(&ea);
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002409 if (!ni && !(ea.argt & EX_EXTRA) && *ea.arg != NUL
2410 && *ea.arg != '"' && (*ea.arg != '|' || (ea.argt & EX_TRLBAR) == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002411 {
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002412 // no arguments allowed but there is something
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +02002413 errormsg = ex_errmsg(e_trailing_arg, ea.arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002414 goto doend;
2415 }
2416
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002417 if (!ni && (ea.argt & EX_NEEDARG) && *ea.arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002418 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002419 errormsg = _(e_argreq);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002420 goto doend;
2421 }
2422
2423#ifdef FEAT_EVAL
2424 /*
2425 * Skip the command when it's not going to be executed.
2426 * The commands like :if, :endif, etc. always need to be executed.
2427 * Also make an exception for commands that handle a trailing command
2428 * themselves.
2429 */
2430 if (ea.skip)
2431 {
2432 switch (ea.cmdidx)
2433 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002434 // commands that need evaluation
Bram Moolenaar071d4272004-06-13 20:20:40 +00002435 case CMD_while:
2436 case CMD_endwhile:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00002437 case CMD_for:
2438 case CMD_endfor:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002439 case CMD_if:
2440 case CMD_elseif:
2441 case CMD_else:
2442 case CMD_endif:
2443 case CMD_try:
2444 case CMD_catch:
2445 case CMD_finally:
2446 case CMD_endtry:
2447 case CMD_function:
Bram Moolenaarab55c682020-03-01 19:41:43 +01002448 case CMD_def:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002449 break;
2450
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002451 // Commands that handle '|' themselves. Check: A command should
2452 // either have the EX_TRLBAR flag, appear in this list or appear in
2453 // the list at ":help :bar".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002454 case CMD_aboveleft:
2455 case CMD_and:
2456 case CMD_belowright:
2457 case CMD_botright:
2458 case CMD_browse:
2459 case CMD_call:
2460 case CMD_confirm:
Bram Moolenaar8f76e6b2019-11-26 16:50:30 +01002461 case CMD_const:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002462 case CMD_delfunction:
2463 case CMD_djump:
2464 case CMD_dlist:
2465 case CMD_dsearch:
2466 case CMD_dsplit:
2467 case CMD_echo:
2468 case CMD_echoerr:
2469 case CMD_echomsg:
2470 case CMD_echon:
Bram Moolenaara76b3152020-02-16 16:20:21 +01002471 case CMD_eval:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002472 case CMD_execute:
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002473 case CMD_filter:
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002474 case CMD_final:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002475 case CMD_help:
2476 case CMD_hide:
2477 case CMD_ijump:
2478 case CMD_ilist:
2479 case CMD_isearch:
2480 case CMD_isplit:
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002481 case CMD_keepalt:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002482 case CMD_keepjumps:
2483 case CMD_keepmarks:
Bram Moolenaara939e432013-11-09 05:30:26 +01002484 case CMD_keeppatterns:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002485 case CMD_leftabove:
2486 case CMD_let:
2487 case CMD_lockmarks:
Bram Moolenaarcc4423a2019-11-26 17:05:00 +01002488 case CMD_lockvar:
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002489 case CMD_lua:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002490 case CMD_match:
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002491 case CMD_mzscheme:
Bram Moolenaar5803ae62014-03-23 16:04:02 +01002492 case CMD_noautocmd:
2493 case CMD_noswapfile:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002494 case CMD_perl:
2495 case CMD_psearch:
Bram Moolenaar368373e2010-07-19 20:46:22 +02002496 case CMD_py3:
Bram Moolenaarb6590522010-07-21 16:00:43 +02002497 case CMD_python3:
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002498 case CMD_python:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002499 case CMD_return:
2500 case CMD_rightbelow:
2501 case CMD_ruby:
2502 case CMD_silent:
2503 case CMD_smagic:
2504 case CMD_snomagic:
2505 case CMD_substitute:
2506 case CMD_syntax:
Bram Moolenaara226a6d2006-02-26 23:59:20 +00002507 case CMD_tab:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002508 case CMD_tcl:
2509 case CMD_throw:
2510 case CMD_tilde:
2511 case CMD_topleft:
2512 case CMD_unlet:
Bram Moolenaarcc4423a2019-11-26 17:05:00 +01002513 case CMD_unlockvar:
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002514 case CMD_var:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002515 case CMD_verbose:
2516 case CMD_vertical:
Bram Moolenaar12bde492011-06-13 01:19:56 +02002517 case CMD_wincmd:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002518 break;
2519
2520 default: goto doend;
2521 }
2522 }
2523#endif
2524
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002525 if (ea.argt & EX_XFILE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002526 {
2527 if (expand_filename(&ea, cmdlinep, &errormsg) == FAIL)
2528 goto doend;
2529 }
2530
Bram Moolenaar071d4272004-06-13 20:20:40 +00002531 /*
2532 * Accept buffer name. Cannot be used at the same time with a buffer
2533 * number. Don't do this for a user command.
2534 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002535 if ((ea.argt & EX_BUFNAME) && *ea.arg != NUL && ea.addr_count == 0
Bram Moolenaar958636c2014-10-21 20:01:58 +02002536 && !IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002537 {
2538 /*
2539 * :bdelete, :bwipeout and :bunload take several arguments, separated
2540 * by spaces: find next space (skipping over escaped characters).
2541 * The others take one argument: ignore trailing spaces.
2542 */
2543 if (ea.cmdidx == CMD_bdelete || ea.cmdidx == CMD_bwipeout
2544 || ea.cmdidx == CMD_bunload)
2545 p = skiptowhite_esc(ea.arg);
2546 else
2547 {
2548 p = ea.arg + STRLEN(ea.arg);
Bram Moolenaar1c465442017-03-12 20:10:05 +01002549 while (p > ea.arg && VIM_ISWHITE(p[-1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002550 --p;
2551 }
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002552 ea.line2 = buflist_findpat(ea.arg, p, (ea.argt & EX_BUFUNL) != 0,
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002553 FALSE, FALSE);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002554 if (ea.line2 < 0) // failed
Bram Moolenaar071d4272004-06-13 20:20:40 +00002555 goto doend;
2556 ea.addr_count = 1;
2557 ea.arg = skipwhite(p);
2558 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002559
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002560 // The :try command saves the emsg_silent flag, reset it here when
2561 // ":silent! try" was used, it should only apply to :try itself.
Bram Moolenaar5661ed62020-10-24 17:19:16 +02002562 if (ea.cmdidx == CMD_try && cmdmod.cmod_did_esilent > 0)
Bram Moolenaar2be57332018-02-13 18:05:18 +01002563 {
Bram Moolenaar5661ed62020-10-24 17:19:16 +02002564 emsg_silent -= cmdmod.cmod_did_esilent;
Bram Moolenaar2be57332018-02-13 18:05:18 +01002565 if (emsg_silent < 0)
2566 emsg_silent = 0;
Bram Moolenaar5661ed62020-10-24 17:19:16 +02002567 cmdmod.cmod_did_esilent = 0;
Bram Moolenaar2be57332018-02-13 18:05:18 +01002568 }
2569
Bram Moolenaar071d4272004-06-13 20:20:40 +00002570/*
Bram Moolenaar2be57332018-02-13 18:05:18 +01002571 * 7. Execute the command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002572 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002573
Bram Moolenaar958636c2014-10-21 20:01:58 +02002574 if (IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002575 {
2576 /*
2577 * Execute a user-defined command.
2578 */
2579 do_ucmd(&ea);
2580 }
2581 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002582 {
2583 /*
Bram Moolenaarac9fb182019-04-27 13:04:13 +02002584 * Call the function to execute the builtin command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002585 */
2586 ea.errmsg = NULL;
2587 (cmdnames[ea.cmdidx].cmd_func)(&ea);
2588 if (ea.errmsg != NULL)
Bram Moolenaar8930caa2020-07-23 16:37:03 +02002589 errormsg = ea.errmsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002590 }
2591
2592#ifdef FEAT_EVAL
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002593 // Set flag that any command was executed, used by ex_vim9script().
Bram Moolenaar16531552020-02-08 16:00:46 +01002594 if (getline_equal(ea.getline, ea.cookie, getsourceline)
2595 && current_sctx.sc_sid > 0)
Bram Moolenaar2b327002020-12-26 15:39:31 +01002596 SCRIPT_ITEM(current_sctx.sc_sid)->sn_state = SN_STATE_HAD_COMMAND;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002597
Bram Moolenaar071d4272004-06-13 20:20:40 +00002598 /*
2599 * If the command just executed called do_cmdline(), any throw or ":return"
2600 * or ":finish" encountered there must also check the cstack of the still
2601 * active do_cmdline() that called this do_one_cmd(). Rethrow an uncaught
2602 * exception, or reanimate a returned function or finished script file and
2603 * return or finish it again.
2604 */
2605 if (need_rethrow)
2606 do_throw(cstack);
2607 else if (check_cstack)
2608 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002609 if (source_finished(fgetline, cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002610 do_finish(&ea, TRUE);
Bram Moolenaar89d40322006-08-29 15:30:07 +00002611 else if (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002612 && current_func_returned())
2613 do_return(&ea, TRUE, FALSE, NULL);
2614 }
2615 need_rethrow = check_cstack = FALSE;
2616#endif
2617
2618doend:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002619 if (curwin->w_cursor.lnum == 0) // can happen with zero line number
Bram Moolenaar6de5e122017-04-20 21:55:44 +02002620 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002621 curwin->w_cursor.lnum = 1;
Bram Moolenaar6de5e122017-04-20 21:55:44 +02002622 curwin->w_cursor.col = 0;
2623 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002624
2625 if (errormsg != NULL && *errormsg != NUL && !did_emsg)
2626 {
2627 if (sourcing)
2628 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002629 if (errormsg != (char *)IObuff)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002630 {
2631 STRCPY(IObuff, errormsg);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002632 errormsg = (char *)IObuff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002633 }
Bram Moolenaara6f4d612011-09-21 19:10:46 +02002634 append_command(*cmdlinep);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002635 }
2636 emsg(errormsg);
2637 }
2638#ifdef FEAT_EVAL
2639 do_errthrow(cstack,
Bram Moolenaar958636c2014-10-21 20:01:58 +02002640 (ea.cmdidx != CMD_SIZE && !IS_USER_CMDIDX(ea.cmdidx))
2641 ? cmdnames[(int)ea.cmdidx].cmd_name : (char_u *)NULL);
Bram Moolenaarb4bcea42020-10-28 13:53:50 +01002642
2643 if (did_set_expr_line)
2644 set_expr_line(NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002645#endif
2646
Bram Moolenaare1004402020-10-24 20:49:43 +02002647 undo_cmdmod(&cmdmod);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002648 cmdmod = save_cmdmod;
Bram Moolenaar9a2c0912019-03-30 14:26:18 +01002649 reg_executing = save_reg_executing;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002650
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002651 if (ea.nextcmd && *ea.nextcmd == NUL) // not really a next command
Bram Moolenaar071d4272004-06-13 20:20:40 +00002652 ea.nextcmd = NULL;
2653
2654#ifdef FEAT_EVAL
2655 --ex_nesting_level;
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002656 vim_free(ea.cmdline_tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002657#endif
2658
2659 return ea.nextcmd;
2660}
2661#if (_MSC_VER == 1200)
Bram Moolenaar281bdce2005-01-25 21:53:18 +00002662 #pragma optimize( "", on )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002663#endif
2664
Bram Moolenaar8930caa2020-07-23 16:37:03 +02002665static char ex_error_buf[MSG_BUF_LEN];
2666
2667/*
2668 * Return an error message with argument included.
2669 * Uses a static buffer, only the last error will be kept.
2670 * "msg" will be translated, caller should use N_().
2671 */
2672 char *
2673ex_errmsg(char *msg, char_u *arg)
2674{
2675 vim_snprintf(ex_error_buf, MSG_BUF_LEN, _(msg), arg);
2676 return ex_error_buf;
2677}
2678
Bram Moolenaar071d4272004-06-13 20:20:40 +00002679/*
Bram Moolenaareffed932018-08-14 13:38:17 +02002680 * Parse and skip over command modifiers:
2681 * - update eap->cmd
Bram Moolenaare1004402020-10-24 20:49:43 +02002682 * - store flags in "cmod".
Bram Moolenaareffed932018-08-14 13:38:17 +02002683 * - Set ex_pressedreturn for an empty command line.
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002684 * When "skip_only" is TRUE the global variables are not changed, except for
2685 * "cmdmod".
Bram Moolenaare1004402020-10-24 20:49:43 +02002686 * When "skip_only" is FALSE then undo_cmdmod() must be called later to free
2687 * any cmod_filter_regmatch.regprog.
Bram Moolenaar5661ed62020-10-24 17:19:16 +02002688 * Call apply_cmdmod() to get the side effects of the modifiers:
2689 * - Increment "sandbox" for ":sandbox"
2690 * - set p_verbose for ":verbose"
Bram Moolenaare1004402020-10-24 20:49:43 +02002691 * - set msg_silent for ":silent"
2692 * - set 'eventignore' to "all" for ":noautocmd"
Bram Moolenaareffed932018-08-14 13:38:17 +02002693 * Return FAIL when the command is not to be executed.
2694 * May set "errormsg" to an error message.
2695 */
2696 int
Bram Moolenaare1004402020-10-24 20:49:43 +02002697parse_command_modifiers(
2698 exarg_T *eap,
2699 char **errormsg,
2700 cmdmod_T *cmod,
2701 int skip_only)
Bram Moolenaareffed932018-08-14 13:38:17 +02002702{
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002703 char_u *p;
2704 int starts_with_colon = FALSE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002705
Bram Moolenaare1004402020-10-24 20:49:43 +02002706 CLEAR_POINTER(cmod);
Bram Moolenaareffed932018-08-14 13:38:17 +02002707
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002708 // Repeat until no more command modifiers are found.
Bram Moolenaareffed932018-08-14 13:38:17 +02002709 for (;;)
2710 {
Bram Moolenaareffed932018-08-14 13:38:17 +02002711 while (*eap->cmd == ' ' || *eap->cmd == '\t' || *eap->cmd == ':')
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002712 {
2713 if (*eap->cmd == ':')
2714 starts_with_colon = TRUE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002715 ++eap->cmd;
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002716 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002717
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002718 // in ex mode, an empty line works like :+
Bram Moolenaareffed932018-08-14 13:38:17 +02002719 if (*eap->cmd == NUL && exmode_active
2720 && (getline_equal(eap->getline, eap->cookie, getexmodeline)
2721 || getline_equal(eap->getline, eap->cookie, getexline))
2722 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
2723 {
2724 eap->cmd = (char_u *)"+";
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002725 if (!skip_only)
2726 ex_pressedreturn = TRUE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002727 }
2728
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002729 // ignore comment and empty lines
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002730 if (comment_start(eap->cmd, starts_with_colon))
Bram Moolenaareffed932018-08-14 13:38:17 +02002731 return FAIL;
2732 if (*eap->cmd == NUL)
2733 {
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002734 if (!skip_only)
2735 ex_pressedreturn = TRUE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002736 return FAIL;
2737 }
2738
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02002739 p = skip_range(eap->cmd, TRUE, NULL);
Bram Moolenaareffed932018-08-14 13:38:17 +02002740 switch (*p)
2741 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002742 // When adding an entry, also modify cmd_exists().
Bram Moolenaareffed932018-08-14 13:38:17 +02002743 case 'a': if (!checkforcmd(&eap->cmd, "aboveleft", 3))
2744 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02002745 cmod->cmod_split |= WSP_ABOVE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002746 continue;
2747
2748 case 'b': if (checkforcmd(&eap->cmd, "belowright", 3))
2749 {
Bram Moolenaare1004402020-10-24 20:49:43 +02002750 cmod->cmod_split |= WSP_BELOW;
Bram Moolenaareffed932018-08-14 13:38:17 +02002751 continue;
2752 }
2753 if (checkforcmd(&eap->cmd, "browse", 3))
2754 {
2755#ifdef FEAT_BROWSE_CMD
Bram Moolenaare1004402020-10-24 20:49:43 +02002756 cmod->cmod_flags |= CMOD_BROWSE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002757#endif
2758 continue;
2759 }
2760 if (!checkforcmd(&eap->cmd, "botright", 2))
2761 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02002762 cmod->cmod_split |= WSP_BOT;
Bram Moolenaareffed932018-08-14 13:38:17 +02002763 continue;
2764
2765 case 'c': if (!checkforcmd(&eap->cmd, "confirm", 4))
2766 break;
2767#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaare1004402020-10-24 20:49:43 +02002768 cmod->cmod_flags |= CMOD_CONFIRM;
Bram Moolenaareffed932018-08-14 13:38:17 +02002769#endif
2770 continue;
2771
2772 case 'k': if (checkforcmd(&eap->cmd, "keepmarks", 3))
2773 {
Bram Moolenaare1004402020-10-24 20:49:43 +02002774 cmod->cmod_flags |= CMOD_KEEPMARKS;
Bram Moolenaareffed932018-08-14 13:38:17 +02002775 continue;
2776 }
2777 if (checkforcmd(&eap->cmd, "keepalt", 5))
2778 {
Bram Moolenaare1004402020-10-24 20:49:43 +02002779 cmod->cmod_flags |= CMOD_KEEPALT;
Bram Moolenaareffed932018-08-14 13:38:17 +02002780 continue;
2781 }
2782 if (checkforcmd(&eap->cmd, "keeppatterns", 5))
2783 {
Bram Moolenaare1004402020-10-24 20:49:43 +02002784 cmod->cmod_flags |= CMOD_KEEPPATTERNS;
Bram Moolenaareffed932018-08-14 13:38:17 +02002785 continue;
2786 }
2787 if (!checkforcmd(&eap->cmd, "keepjumps", 5))
2788 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02002789 cmod->cmod_flags |= CMOD_KEEPJUMPS;
Bram Moolenaareffed932018-08-14 13:38:17 +02002790 continue;
2791
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002792 case 'f': // only accept ":filter {pat} cmd"
Bram Moolenaareffed932018-08-14 13:38:17 +02002793 {
2794 char_u *reg_pat;
2795
2796 if (!checkforcmd(&p, "filter", 4)
2797 || *p == NUL || ends_excmd(*p))
2798 break;
2799 if (*p == '!')
2800 {
Bram Moolenaare1004402020-10-24 20:49:43 +02002801 cmod->cmod_filter_force = TRUE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002802 p = skipwhite(p + 1);
2803 if (*p == NUL || ends_excmd(*p))
2804 break;
2805 }
Bram Moolenaar1e624c92020-07-11 14:08:04 +02002806#ifdef FEAT_EVAL
Bram Moolenaarb074e8b2020-07-11 13:40:45 +02002807 // Avoid that "filter(arg)" is recognized.
Bram Moolenaar4f6b6ed2020-10-29 20:24:34 +01002808 if (in_vim9script() && !VIM_ISWHITE(p[-1]))
Bram Moolenaarb074e8b2020-07-11 13:40:45 +02002809 break;
Bram Moolenaar1e624c92020-07-11 14:08:04 +02002810#endif
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002811 if (skip_only)
2812 p = skip_vimgrep_pat(p, NULL, NULL);
2813 else
2814 // NOTE: This puts a NUL after the pattern.
2815 p = skip_vimgrep_pat(p, &reg_pat, NULL);
Bram Moolenaareffed932018-08-14 13:38:17 +02002816 if (p == NULL || *p == NUL)
2817 break;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002818 if (!skip_only)
2819 {
Bram Moolenaare1004402020-10-24 20:49:43 +02002820 cmod->cmod_filter_regmatch.regprog =
Bram Moolenaareffed932018-08-14 13:38:17 +02002821 vim_regcomp(reg_pat, RE_MAGIC);
Bram Moolenaare1004402020-10-24 20:49:43 +02002822 if (cmod->cmod_filter_regmatch.regprog == NULL)
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002823 break;
2824 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002825 eap->cmd = p;
2826 continue;
2827 }
2828
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002829 // ":hide" and ":hide | cmd" are not modifiers
Bram Moolenaareffed932018-08-14 13:38:17 +02002830 case 'h': if (p != eap->cmd || !checkforcmd(&p, "hide", 3)
2831 || *p == NUL || ends_excmd(*p))
2832 break;
2833 eap->cmd = p;
Bram Moolenaare1004402020-10-24 20:49:43 +02002834 cmod->cmod_flags |= CMOD_HIDE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002835 continue;
2836
2837 case 'l': if (checkforcmd(&eap->cmd, "lockmarks", 3))
2838 {
Bram Moolenaare1004402020-10-24 20:49:43 +02002839 cmod->cmod_flags |= CMOD_LOCKMARKS;
Bram Moolenaareffed932018-08-14 13:38:17 +02002840 continue;
2841 }
2842
2843 if (!checkforcmd(&eap->cmd, "leftabove", 5))
2844 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02002845 cmod->cmod_split |= WSP_ABOVE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002846 continue;
2847
2848 case 'n': if (checkforcmd(&eap->cmd, "noautocmd", 3))
2849 {
Bram Moolenaare1004402020-10-24 20:49:43 +02002850 cmod->cmod_flags |= CMOD_NOAUTOCMD;
Bram Moolenaareffed932018-08-14 13:38:17 +02002851 continue;
2852 }
2853 if (!checkforcmd(&eap->cmd, "noswapfile", 3))
2854 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02002855 cmod->cmod_flags |= CMOD_NOSWAPFILE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002856 continue;
2857
2858 case 'r': if (!checkforcmd(&eap->cmd, "rightbelow", 6))
2859 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02002860 cmod->cmod_split |= WSP_BELOW;
Bram Moolenaareffed932018-08-14 13:38:17 +02002861 continue;
2862
2863 case 's': if (checkforcmd(&eap->cmd, "sandbox", 3))
2864 {
Bram Moolenaare1004402020-10-24 20:49:43 +02002865 cmod->cmod_flags |= CMOD_SANDBOX;
Bram Moolenaareffed932018-08-14 13:38:17 +02002866 continue;
2867 }
2868 if (!checkforcmd(&eap->cmd, "silent", 3))
2869 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02002870 cmod->cmod_flags |= CMOD_SILENT;
Bram Moolenaareffed932018-08-14 13:38:17 +02002871 if (*eap->cmd == '!' && !VIM_ISWHITE(eap->cmd[-1]))
2872 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002873 // ":silent!", but not "silent !cmd"
Bram Moolenaareffed932018-08-14 13:38:17 +02002874 eap->cmd = skipwhite(eap->cmd + 1);
Bram Moolenaare1004402020-10-24 20:49:43 +02002875 cmod->cmod_flags |= CMOD_ERRSILENT;
Bram Moolenaareffed932018-08-14 13:38:17 +02002876 }
2877 continue;
2878
2879 case 't': if (checkforcmd(&p, "tab", 3))
2880 {
Bram Moolenaar548e5982018-12-26 21:45:00 +01002881 if (!skip_only)
Bram Moolenaareffed932018-08-14 13:38:17 +02002882 {
Bram Moolenaar548e5982018-12-26 21:45:00 +01002883 long tabnr = get_address(eap, &eap->cmd,
2884 ADDR_TABS, eap->skip,
2885 skip_only, FALSE, 1);
2886 if (tabnr == MAXLNUM)
Bram Moolenaare1004402020-10-24 20:49:43 +02002887 cmod->cmod_tab = tabpage_index(curtab) + 1;
Bram Moolenaar548e5982018-12-26 21:45:00 +01002888 else
Bram Moolenaareffed932018-08-14 13:38:17 +02002889 {
Bram Moolenaar548e5982018-12-26 21:45:00 +01002890 if (tabnr < 0 || tabnr > LAST_TAB_NR)
2891 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002892 *errormsg = _(e_invrange);
Bram Moolenaar548e5982018-12-26 21:45:00 +01002893 return FAIL;
2894 }
Bram Moolenaare1004402020-10-24 20:49:43 +02002895 cmod->cmod_tab = tabnr + 1;
Bram Moolenaareffed932018-08-14 13:38:17 +02002896 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002897 }
2898 eap->cmd = p;
2899 continue;
2900 }
2901 if (!checkforcmd(&eap->cmd, "topleft", 2))
2902 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02002903 cmod->cmod_split |= WSP_TOP;
Bram Moolenaareffed932018-08-14 13:38:17 +02002904 continue;
2905
2906 case 'u': if (!checkforcmd(&eap->cmd, "unsilent", 3))
2907 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02002908 cmod->cmod_flags |= CMOD_UNSILENT;
Bram Moolenaareffed932018-08-14 13:38:17 +02002909 continue;
2910
2911 case 'v': if (checkforcmd(&eap->cmd, "vertical", 4))
2912 {
Bram Moolenaare1004402020-10-24 20:49:43 +02002913 cmod->cmod_split |= WSP_VERT;
Bram Moolenaareffed932018-08-14 13:38:17 +02002914 continue;
2915 }
2916 if (!checkforcmd(&p, "verbose", 4))
2917 break;
Bram Moolenaar5661ed62020-10-24 17:19:16 +02002918 if (vim_isdigit(*eap->cmd))
Bram Moolenaare1004402020-10-24 20:49:43 +02002919 cmod->cmod_verbose = atoi((char *)eap->cmd);
Bram Moolenaar5661ed62020-10-24 17:19:16 +02002920 else
Bram Moolenaare1004402020-10-24 20:49:43 +02002921 cmod->cmod_verbose = 1;
Bram Moolenaareffed932018-08-14 13:38:17 +02002922 eap->cmd = p;
2923 continue;
2924 }
2925 break;
2926 }
2927
2928 return OK;
2929}
2930
2931/*
Bram Moolenaar5661ed62020-10-24 17:19:16 +02002932 * Apply the command modifiers. Saves current state in "cmdmod", call
2933 * undo_cmdmod() later.
2934 */
2935 void
2936apply_cmdmod(cmdmod_T *cmod)
2937{
2938#ifdef HAVE_SANDBOX
2939 if ((cmod->cmod_flags & CMOD_SANDBOX) && !cmod->cmod_did_sandbox)
2940 {
2941 ++sandbox;
2942 cmod->cmod_did_sandbox = TRUE;
2943 }
2944#endif
2945 if (cmod->cmod_verbose > 0)
2946 {
2947 if (cmod->cmod_verbose_save == 0)
2948 cmod->cmod_verbose_save = p_verbose + 1;
2949 p_verbose = cmod->cmod_verbose;
2950 }
2951
2952 if ((cmod->cmod_flags & (CMOD_SILENT | CMOD_UNSILENT))
2953 && cmod->cmod_save_msg_silent == 0)
Bram Moolenaare1004402020-10-24 20:49:43 +02002954 {
Bram Moolenaar5661ed62020-10-24 17:19:16 +02002955 cmod->cmod_save_msg_silent = msg_silent + 1;
Bram Moolenaare1004402020-10-24 20:49:43 +02002956 cmod->cmod_save_msg_scroll = msg_scroll;
2957 }
Bram Moolenaar5661ed62020-10-24 17:19:16 +02002958 if (cmod->cmod_flags & CMOD_SILENT)
2959 ++msg_silent;
2960 if (cmod->cmod_flags & CMOD_UNSILENT)
2961 msg_silent = 0;
2962
2963 if (cmod->cmod_flags & CMOD_ERRSILENT)
2964 {
2965 ++emsg_silent;
2966 ++cmod->cmod_did_esilent;
2967 }
2968
Bram Moolenaare1004402020-10-24 20:49:43 +02002969 if ((cmod->cmod_flags & CMOD_NOAUTOCMD) && cmod->cmod_save_ei == NULL)
Bram Moolenaar5661ed62020-10-24 17:19:16 +02002970 {
2971 // Set 'eventignore' to "all".
2972 // First save the existing option value for restoring it later.
Bram Moolenaare1004402020-10-24 20:49:43 +02002973 cmod->cmod_save_ei = vim_strsave(p_ei);
Bram Moolenaar5661ed62020-10-24 17:19:16 +02002974 set_string_option_direct((char_u *)"ei", -1,
2975 (char_u *)"all", OPT_FREE, SID_NONE);
2976 }
2977}
2978
2979/*
Bram Moolenaare1004402020-10-24 20:49:43 +02002980 * Undo and free contents of "cmod".
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002981 */
Bram Moolenaar47e7d702020-07-05 18:18:42 +02002982 void
Bram Moolenaare1004402020-10-24 20:49:43 +02002983undo_cmdmod(cmdmod_T *cmod)
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002984{
Bram Moolenaare1004402020-10-24 20:49:43 +02002985 if (cmod->cmod_verbose_save > 0)
Bram Moolenaar5661ed62020-10-24 17:19:16 +02002986 {
Bram Moolenaare1004402020-10-24 20:49:43 +02002987 p_verbose = cmod->cmod_verbose_save - 1;
2988 cmod->cmod_verbose_save = 0;
Bram Moolenaar5661ed62020-10-24 17:19:16 +02002989 }
Bram Moolenaar47e7d702020-07-05 18:18:42 +02002990
Bram Moolenaar5661ed62020-10-24 17:19:16 +02002991#ifdef HAVE_SANDBOX
Bram Moolenaare1004402020-10-24 20:49:43 +02002992 if (cmod->cmod_did_sandbox)
Bram Moolenaar5661ed62020-10-24 17:19:16 +02002993 {
2994 --sandbox;
Bram Moolenaare1004402020-10-24 20:49:43 +02002995 cmod->cmod_did_sandbox = FALSE;
Bram Moolenaar5661ed62020-10-24 17:19:16 +02002996 }
2997#endif
2998
Bram Moolenaare1004402020-10-24 20:49:43 +02002999 if (cmod->cmod_save_ei != NULL)
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02003000 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003001 // Restore 'eventignore' to the value before ":noautocmd".
Bram Moolenaare1004402020-10-24 20:49:43 +02003002 set_string_option_direct((char_u *)"ei", -1, cmod->cmod_save_ei,
3003 OPT_FREE, SID_NONE);
3004 free_string_option(cmod->cmod_save_ei);
3005 cmod->cmod_save_ei = NULL;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02003006 }
3007
Bram Moolenaarcaf73dc2020-10-26 21:39:13 +01003008 vim_regfree(cmod->cmod_filter_regmatch.regprog);
Bram Moolenaar47e7d702020-07-05 18:18:42 +02003009
Bram Moolenaare1004402020-10-24 20:49:43 +02003010 if (cmod->cmod_save_msg_silent > 0)
Bram Moolenaar47e7d702020-07-05 18:18:42 +02003011 {
3012 // messages could be enabled for a serious error, need to check if the
3013 // counters don't become negative
Bram Moolenaare1004402020-10-24 20:49:43 +02003014 if (!did_emsg || msg_silent > cmod->cmod_save_msg_silent - 1)
3015 msg_silent = cmod->cmod_save_msg_silent - 1;
3016 emsg_silent -= cmod->cmod_did_esilent;
Bram Moolenaar47e7d702020-07-05 18:18:42 +02003017 if (emsg_silent < 0)
3018 emsg_silent = 0;
3019 // Restore msg_scroll, it's set by file I/O commands, even when no
3020 // message is actually displayed.
Bram Moolenaare1004402020-10-24 20:49:43 +02003021 msg_scroll = cmod->cmod_save_msg_scroll;
Bram Moolenaar47e7d702020-07-05 18:18:42 +02003022
3023 // "silent reg" or "silent echo x" inside "redir" leaves msg_col
3024 // somewhere in the line. Put it back in the first column.
3025 if (redirecting())
3026 msg_col = 0;
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003027
Bram Moolenaare1004402020-10-24 20:49:43 +02003028 cmod->cmod_save_msg_silent = 0;
3029 cmod->cmod_did_esilent = 0;
Bram Moolenaar47e7d702020-07-05 18:18:42 +02003030 }
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02003031}
3032
3033/*
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003034 * Parse the address range, if any, in "eap".
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02003035 * May set the last search pattern, unless "silent" is TRUE.
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003036 * Return FAIL and set "errormsg" or return OK.
3037 */
3038 int
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003039parse_cmd_address(exarg_T *eap, char **errormsg, int silent)
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003040{
3041 int address_count = 1;
3042 linenr_T lnum;
3043
3044 // Repeat for all ',' or ';' separated addresses.
3045 for (;;)
3046 {
3047 eap->line1 = eap->line2;
Bram Moolenaarc2af0af2020-08-23 21:06:02 +02003048 eap->line2 = default_address(eap);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003049 eap->cmd = skipwhite(eap->cmd);
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02003050 lnum = get_address(eap, &eap->cmd, eap->addr_type, eap->skip, silent,
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003051 eap->addr_count == 0, address_count++);
3052 if (eap->cmd == NULL) // error detected
3053 return FAIL;
3054 if (lnum == MAXLNUM)
3055 {
3056 if (*eap->cmd == '%') // '%' - all lines
3057 {
3058 ++eap->cmd;
3059 switch (eap->addr_type)
3060 {
3061 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02003062 case ADDR_OTHER:
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003063 eap->line1 = 1;
3064 eap->line2 = curbuf->b_ml.ml_line_count;
3065 break;
3066 case ADDR_LOADED_BUFFERS:
3067 {
3068 buf_T *buf = firstbuf;
3069
3070 while (buf->b_next != NULL
3071 && buf->b_ml.ml_mfp == NULL)
3072 buf = buf->b_next;
3073 eap->line1 = buf->b_fnum;
3074 buf = lastbuf;
3075 while (buf->b_prev != NULL
3076 && buf->b_ml.ml_mfp == NULL)
3077 buf = buf->b_prev;
3078 eap->line2 = buf->b_fnum;
3079 break;
3080 }
3081 case ADDR_BUFFERS:
3082 eap->line1 = firstbuf->b_fnum;
3083 eap->line2 = lastbuf->b_fnum;
3084 break;
3085 case ADDR_WINDOWS:
3086 case ADDR_TABS:
3087 if (IS_USER_CMDIDX(eap->cmdidx))
3088 {
3089 eap->line1 = 1;
3090 eap->line2 = eap->addr_type == ADDR_WINDOWS
3091 ? LAST_WIN_NR : LAST_TAB_NR;
3092 }
3093 else
3094 {
3095 // there is no Vim command which uses '%' and
3096 // ADDR_WINDOWS or ADDR_TABS
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003097 *errormsg = _(e_invrange);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003098 return FAIL;
3099 }
3100 break;
3101 case ADDR_TABS_RELATIVE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02003102 case ADDR_UNSIGNED:
3103 case ADDR_QUICKFIX:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003104 *errormsg = _(e_invrange);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003105 return FAIL;
3106 case ADDR_ARGUMENTS:
3107 if (ARGCOUNT == 0)
3108 eap->line1 = eap->line2 = 0;
3109 else
3110 {
3111 eap->line1 = 1;
3112 eap->line2 = ARGCOUNT;
3113 }
3114 break;
Bram Moolenaar25190db2019-05-04 15:05:28 +02003115 case ADDR_QUICKFIX_VALID:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003116#ifdef FEAT_QUICKFIX
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003117 eap->line1 = 1;
Bram Moolenaar25190db2019-05-04 15:05:28 +02003118 eap->line2 = qf_get_valid_size(eap);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003119 if (eap->line2 == 0)
3120 eap->line2 = 1;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003121#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003122 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02003123 case ADDR_NONE:
3124 // Will give an error later if a range is found.
3125 break;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003126 }
3127 ++eap->addr_count;
3128 }
3129 else if (*eap->cmd == '*' && vim_strchr(p_cpo, CPO_STAR) == NULL)
3130 {
3131 pos_T *fp;
3132
3133 // '*' - visual area
3134 if (eap->addr_type != ADDR_LINES)
3135 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003136 *errormsg = _(e_invrange);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003137 return FAIL;
3138 }
3139
3140 ++eap->cmd;
3141 if (!eap->skip)
3142 {
3143 fp = getmark('<', FALSE);
3144 if (check_mark(fp) == FAIL)
3145 return FAIL;
3146 eap->line1 = fp->lnum;
3147 fp = getmark('>', FALSE);
3148 if (check_mark(fp) == FAIL)
3149 return FAIL;
3150 eap->line2 = fp->lnum;
3151 ++eap->addr_count;
3152 }
3153 }
3154 }
3155 else
3156 eap->line2 = lnum;
3157 eap->addr_count++;
3158
3159 if (*eap->cmd == ';')
3160 {
3161 if (!eap->skip)
3162 {
3163 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaar0e717042020-04-27 19:29:01 +02003164 // Don't leave the cursor on an illegal line or column, but do
3165 // accept zero as address, so 0;/PATTERN/ works correctly.
3166 if (eap->line2 > 0)
3167 check_cursor();
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003168 }
3169 }
3170 else if (*eap->cmd != ',')
3171 break;
3172 ++eap->cmd;
3173 }
3174
3175 // One address given: set start and end lines.
3176 if (eap->addr_count == 1)
3177 {
3178 eap->line1 = eap->line2;
3179 // ... but only implicit: really no address given
3180 if (lnum == MAXLNUM)
3181 eap->addr_count = 0;
3182 }
3183 return OK;
3184}
3185
3186/*
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00003187 * Check for an Ex command with optional tail.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003188 * If there is a match advance "pp" to the argument and return TRUE.
3189 */
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00003190 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003191checkforcmd(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003192 char_u **pp, // start of command
3193 char *cmd, // name of command
3194 int len) // required length
Bram Moolenaar071d4272004-06-13 20:20:40 +00003195{
3196 int i;
3197
3198 for (i = 0; cmd[i] != NUL; ++i)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003199 if (((char_u *)cmd)[i] != (*pp)[i])
Bram Moolenaar071d4272004-06-13 20:20:40 +00003200 break;
Bram Moolenaar7a66a172020-10-16 19:56:12 +02003201 if (i >= len && !isalpha((*pp)[i]) && (*pp)[i] != '_')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003202 {
3203 *pp = skipwhite(*pp + i);
3204 return TRUE;
3205 }
3206 return FALSE;
3207}
3208
3209/*
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003210 * Append "cmd" to the error message in IObuff.
3211 * Takes care of limiting the length and handling 0xa0, which would be
3212 * invisible otherwise.
3213 */
3214 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003215append_command(char_u *cmd)
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003216{
3217 char_u *s = cmd;
3218 char_u *d;
3219
3220 STRCAT(IObuff, ": ");
3221 d = IObuff + STRLEN(IObuff);
3222 while (*s != NUL && d - IObuff < IOSIZE - 7)
3223 {
Bram Moolenaar13505972019-01-24 15:04:48 +01003224 if (enc_utf8 ? (s[0] == 0xc2 && s[1] == 0xa0) : *s == 0xa0)
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003225 {
Bram Moolenaar13505972019-01-24 15:04:48 +01003226 s += enc_utf8 ? 2 : 1;
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003227 STRCPY(d, "<a0>");
3228 d += 4;
3229 }
3230 else
3231 MB_COPY_CHAR(s, d);
3232 }
3233 *d = NUL;
3234}
3235
3236/*
Bram Moolenaar2e800952020-08-23 19:34:48 +02003237 * If "start" points "&opt", "&l:opt", "&g:opt" or "$ENV" return a pointer to
3238 * the name. Otherwise just return "start".
3239 */
3240 char_u *
3241skip_option_env_lead(char_u *start)
3242{
3243 char_u *name = start;
3244
3245 if (*start == '&')
3246 {
3247 if ((start[1] == 'l' || start[1] == 'g') && start[2] == ':')
3248 name += 3;
3249 else
3250 name += 1;
3251 }
3252 else if (*start == '$')
3253 name += 1;
3254 return name;
3255}
3256
3257/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003258 * Find an Ex command by its name, either built-in or user.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003259 * Start of the name can be found at eap->cmd.
Bram Moolenaar25190db2019-05-04 15:05:28 +02003260 * Sets eap->cmdidx and returns a pointer to char after the command name.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003261 * "full" is set to TRUE if the whole command name matched.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003262 *
3263 * If "lookup" is not NULL recognize expression without "eval" or "call" and
3264 * assignment without "let". Sets eap->cmdidx to the command while returning
3265 * "eap->cmd".
3266 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00003267 * Returns NULL for an ambiguous user command.
3268 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003269 char_u *
3270find_ex_command(
3271 exarg_T *eap,
Bram Moolenaara494f562020-04-18 17:45:38 +02003272 int *full UNUSED,
Bram Moolenaar709664c2020-12-12 14:33:41 +01003273 int (*lookup)(char_u *, size_t, void *, cctx_T *) UNUSED,
Bram Moolenaara494f562020-04-18 17:45:38 +02003274 cctx_T *cctx UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003275{
3276 int len;
3277 char_u *p;
Bram Moolenaardf177f62005-02-22 08:39:57 +00003278 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003279
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003280#ifdef FEAT_EVAL
3281 /*
3282 * Recognize a Vim9 script function/method call and assignment:
3283 * "lvar = value", "lvar(arg)", "[1, 2 3]->Func()"
3284 */
Bram Moolenaar0c6ceaf2020-02-22 18:36:32 +01003285 p = eap->cmd;
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003286 if (lookup != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003287 {
Bram Moolenaar2e800952020-08-23 19:34:48 +02003288 char_u *pskip = skip_option_env_lead(eap->cmd);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003289
Bram Moolenaar83144542020-08-02 20:40:43 +02003290 if (vim_strchr((char_u *)"{('[\"@", *p) != NULL
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003291 || ((p = to_name_const_end(pskip)) > eap->cmd && *p != NUL))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003292 {
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003293 int oplen;
3294 int heredoc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003295
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003296 if (
3297 // "(..." is an expression.
3298 // "funcname(" is always a function call.
3299 *p == '('
3300 || (p == eap->cmd
3301 ? (
Bram Moolenaar9becdf22020-10-10 21:33:48 +02003302 // "{..." is a dict expression or block start.
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003303 *eap->cmd == '{'
3304 // "'string'->func()" is an expression.
3305 || *eap->cmd == '\''
Bram Moolenaar10409562020-07-29 20:00:38 +02003306 // '"string"->func()' is an expression.
3307 || *eap->cmd == '"'
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003308 // "g:varname" is an expression.
3309 || eap->cmd[1] == ':'
3310 )
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003311 // "varname->func()" is an expression.
Bram Moolenaar2b22b112020-09-14 18:35:18 +02003312 : (*p == '-' && p[1] == '>')))
Bram Moolenaarb31be3f2020-07-20 22:37:44 +02003313 {
Bram Moolenaar9becdf22020-10-10 21:33:48 +02003314 if (*eap->cmd == '{' && ends_excmd(*skipwhite(eap->cmd + 1)))
3315 {
3316 // "{" by itself is the start of a block.
3317 eap->cmdidx = CMD_block;
3318 return eap->cmd + 1;
3319 }
Bram Moolenaarb31be3f2020-07-20 22:37:44 +02003320 eap->cmdidx = CMD_eval;
3321 return eap->cmd;
3322 }
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003323
Bram Moolenaar2b22b112020-09-14 18:35:18 +02003324 if (p != eap->cmd && (
3325 // "varname[]" is an expression.
3326 *p == '['
3327 // "varname.key" is an expression.
3328 || (*p == '.' && ASCII_ISALPHA(p[1]))))
3329 {
3330 char_u *after = p;
3331
3332 // When followed by "=" or "+=" then it is an assignment.
3333 ++emsg_silent;
Bram Moolenaar8f22f5c2020-12-19 22:10:13 +01003334 if (*after == '.')
3335 after = skipwhite(after + 1);
Bram Moolenaarce2c5442020-11-28 21:21:17 +01003336 if (skip_expr(&after, NULL) == OK)
3337 after = skipwhite(after);
3338 else
3339 after = (char_u *)"";
3340 if (*after == '=' || (*after != NUL && after[1] == '=')
3341 || (after[0] == '.' && after[1] == '.'
3342 && after[2] == '='))
Bram Moolenaar30fd8202020-09-26 15:09:30 +02003343 eap->cmdidx = CMD_var;
Bram Moolenaar2b22b112020-09-14 18:35:18 +02003344 else
3345 eap->cmdidx = CMD_eval;
3346 --emsg_silent;
3347 return eap->cmd;
3348 }
3349
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003350 // "[...]->Method()" is a list expression, but "[a, b] = Func()" is
3351 // an assignment.
3352 // If there is no line break inside the "[...]" then "p" is
3353 // advanced to after the "]" by to_name_const_end(): check if a "="
3354 // follows.
3355 // If "[...]" has a line break "p" still points at the "[" and it
3356 // can't be an assignment.
3357 if (*eap->cmd == '[')
Bram Moolenaarb31be3f2020-07-20 22:37:44 +02003358 {
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003359 p = to_name_const_end(eap->cmd);
Bram Moolenaarda7c20c2020-11-30 21:12:19 +01003360 if (p == eap->cmd && *p == '[')
3361 {
3362 int count = 0;
3363 int semicolon = FALSE;
3364
3365 p = skip_var_list(eap->cmd, TRUE, &count, &semicolon, TRUE);
3366 }
3367 if (p == NULL || p == eap->cmd || *skipwhite(p) != '=')
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003368 {
3369 eap->cmdidx = CMD_eval;
3370 return eap->cmd;
3371 }
3372 if (p > eap->cmd && *skipwhite(p) == '=')
3373 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02003374 eap->cmdidx = CMD_var;
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003375 return eap->cmd;
3376 }
3377 }
3378
3379 // Recognize an assignment if we recognize the variable name:
3380 // "g:var = expr"
3381 // "var = expr" where "var" is a local var name.
Bram Moolenaar83144542020-08-02 20:40:43 +02003382 if (*eap->cmd == '@')
3383 p = eap->cmd + 2;
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003384 oplen = assignment_len(skipwhite(p), &heredoc);
3385 if (oplen > 0)
3386 {
3387 if (((p - eap->cmd) > 2 && eap->cmd[1] == ':')
3388 || *eap->cmd == '&'
3389 || *eap->cmd == '$'
3390 || *eap->cmd == '@'
Bram Moolenaar709664c2020-12-12 14:33:41 +01003391 || lookup(eap->cmd, p - eap->cmd, NULL, cctx) == OK)
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003392 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02003393 eap->cmdidx = CMD_var;
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003394 return eap->cmd;
3395 }
3396 }
3397
3398 // Recognize using a type for a w:, b:, t: or g: variable:
3399 // "w:varname: number = 123".
3400 if (eap->cmd[1] == ':' && *p == ':')
3401 {
3402 eap->cmdidx = CMD_eval;
Bram Moolenaarb31be3f2020-07-20 22:37:44 +02003403 return eap->cmd;
3404 }
Bram Moolenaard2ef6b32020-07-02 21:11:34 +02003405 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003406 }
3407#endif
3408
Bram Moolenaar071d4272004-06-13 20:20:40 +00003409 /*
3410 * Isolate the command and search for it in the command table.
Bram Moolenaar81870892007-11-11 18:17:28 +00003411 * Exceptions:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003412 * - the 'k' command can directly be followed by any character.
3413 * - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003414 * but :sre[wind] is another command, as are :scr[iptnames],
Bram Moolenaar071d4272004-06-13 20:20:40 +00003415 * :scs[cope], :sim[alt], :sig[ns] and :sil[ent].
Bram Moolenaardf177f62005-02-22 08:39:57 +00003416 * - the "d" command can directly be followed by 'l' or 'p' flag.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003417 */
3418 p = eap->cmd;
3419 if (*p == 'k')
3420 {
3421 eap->cmdidx = CMD_k;
3422 ++p;
3423 }
3424 else if (p[0] == 's'
Bram Moolenaar204b93f2015-08-04 22:02:51 +02003425 && ((p[1] == 'c' && (p[2] == NUL || (p[2] != 's' && p[2] != 'r'
3426 && (p[3] == NUL || (p[3] != 'i' && p[4] != 'p')))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003427 || p[1] == 'g'
3428 || (p[1] == 'i' && p[2] != 'm' && p[2] != 'l' && p[2] != 'g')
3429 || p[1] == 'I'
3430 || (p[1] == 'r' && p[2] != 'e')))
3431 {
3432 eap->cmdidx = CMD_substitute;
3433 ++p;
3434 }
3435 else
3436 {
3437 while (ASCII_ISALPHA(*p))
3438 ++p;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003439 // for python 3.x support ":py3", ":python3", ":py3file", etc.
Bram Moolenaar55d5c032010-07-17 23:52:29 +02003440 if (eap->cmd[0] == 'p' && eap->cmd[1] == 'y')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003441 {
Bram Moolenaarb6590522010-07-21 16:00:43 +02003442 while (ASCII_ISALNUM(*p))
3443 ++p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003444 }
3445 else if (*p == '9' && STRNCMP("vim9", eap->cmd, 4) == 0)
3446 {
3447 // include "9" for "vim9script"
3448 ++p;
3449 while (ASCII_ISALPHA(*p))
3450 ++p;
3451 }
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02003452
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003453 // check for non-alpha command
Bram Moolenaar9becdf22020-10-10 21:33:48 +02003454 if (p == eap->cmd && vim_strchr((char_u *)"@*!=><&~#}", *p) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003455 ++p;
3456 len = (int)(p - eap->cmd);
Bram Moolenaardf177f62005-02-22 08:39:57 +00003457 if (*eap->cmd == 'd' && (p[-1] == 'l' || p[-1] == 'p'))
3458 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003459 // Check for ":dl", ":dell", etc. to ":deletel": that's
3460 // :delete with the 'l' flag. Same for 'p'.
Bram Moolenaardf177f62005-02-22 08:39:57 +00003461 for (i = 0; i < len; ++i)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003462 if (eap->cmd[i] != ((char_u *)"delete")[i])
Bram Moolenaardf177f62005-02-22 08:39:57 +00003463 break;
3464 if (i == len - 1)
3465 {
3466 --len;
3467 if (p[-1] == 'l')
3468 eap->flags |= EXFLAG_LIST;
3469 else
3470 eap->flags |= EXFLAG_PRINT;
3471 }
3472 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003473
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003474 if (ASCII_ISLOWER(eap->cmd[0]))
3475 {
Bram Moolenaar6c0c1e82017-03-25 15:07:43 +01003476 int c1 = eap->cmd[0];
Bram Moolenaar94f82cb2019-07-24 22:30:27 +02003477 int c2 = len == 1 ? NUL : eap->cmd[1];
Bram Moolenaar6c0c1e82017-03-25 15:07:43 +01003478
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003479 if (command_count != (int)CMD_SIZE)
3480 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003481 iemsg(_("E943: Command table needs to be updated, run 'make cmdidxs'"));
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003482 getout(1);
3483 }
3484
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003485 // Use a precomputed index for fast look-up in cmdnames[]
3486 // taking into account the first 2 letters of eap->cmd.
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003487 eap->cmdidx = cmdidxs1[CharOrdLow(c1)];
3488 if (ASCII_ISLOWER(c2))
3489 eap->cmdidx += cmdidxs2[CharOrdLow(c1)][CharOrdLow(c2)];
3490 }
Bram Moolenaara494f562020-04-18 17:45:38 +02003491 else if (ASCII_ISUPPER(eap->cmd[0]))
3492 eap->cmdidx = CMD_Next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003493 else
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003494 eap->cmdidx = CMD_bang;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003495
3496 for ( ; (int)eap->cmdidx < (int)CMD_SIZE;
3497 eap->cmdidx = (cmdidx_T)((int)eap->cmdidx + 1))
3498 if (STRNCMP(cmdnames[(int)eap->cmdidx].cmd_name, (char *)eap->cmd,
3499 (size_t)len) == 0)
3500 {
3501#ifdef FEAT_EVAL
3502 if (full != NULL
3503 && cmdnames[(int)eap->cmdidx].cmd_name[len] == NUL)
3504 *full = TRUE;
3505#endif
3506 break;
3507 }
3508
Bram Moolenaar95388e32020-11-20 21:07:00 +01003509 // Not not recognize ":*" as the star command unless '*' is in
3510 // 'cpoptions'.
3511 if (eap->cmdidx == CMD_star && vim_strchr(p_cpo, CPO_STAR) == NULL)
3512 p = eap->cmd;
3513
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003514 // Look for a user defined command as a last resort. Let ":Print" be
3515 // overruled by a user defined command.
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01003516 if ((eap->cmdidx == CMD_SIZE || eap->cmdidx == CMD_Print)
3517 && *eap->cmd >= 'A' && *eap->cmd <= 'Z')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003518 {
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003519 // User defined commands may contain digits.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003520 while (ASCII_ISALNUM(*p))
3521 ++p;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003522 p = find_ucmd(eap, p, full, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003523 }
Bram Moolenaar1c0aa972020-12-16 21:43:54 +01003524 if (p == NULL || p == eap->cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003525 eap->cmdidx = CMD_SIZE;
3526 }
3527
Bram Moolenaar373863e2020-09-26 17:20:53 +02003528 // ":fina" means ":finally" for backwards compatibility.
3529 if (eap->cmdidx == CMD_final && p - eap->cmd == 4)
3530 eap->cmdidx = CMD_finally;
3531
Bram Moolenaar2a3cd3a2020-12-13 19:22:27 +01003532#ifdef FEAT_EVAL
Bram Moolenaarb5b94802020-12-13 17:50:20 +01003533 if (eap->cmdidx != CMD_SIZE && in_vim9script()
Bram Moolenaarf8103f22020-12-25 17:36:27 +01003534 && !IS_WHITE_OR_NUL(*p) && *p != '\n' && *p != '!'
Bram Moolenaarb5b94802020-12-13 17:50:20 +01003535 && (cmdnames[eap->cmdidx].cmd_argt & EX_NONWHITE_OK) == 0)
3536 {
3537 semsg(_(e_command_not_followed_by_white_space_str), eap->cmd);
3538 eap->cmdidx = CMD_SIZE;
3539 }
Bram Moolenaar2a3cd3a2020-12-13 19:22:27 +01003540#endif
Bram Moolenaarb5b94802020-12-13 17:50:20 +01003541
Bram Moolenaar071d4272004-06-13 20:20:40 +00003542 return p;
3543}
3544
3545#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003546static struct cmdmod
3547{
3548 char *name;
3549 int minlen;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003550 int has_count; // :123verbose :3tab
Bram Moolenaared53fb92007-11-24 20:50:24 +00003551} cmdmods[] = {
3552 {"aboveleft", 3, FALSE},
3553 {"belowright", 3, FALSE},
3554 {"botright", 2, FALSE},
3555 {"browse", 3, FALSE},
3556 {"confirm", 4, FALSE},
Bram Moolenaar7b668e82016-08-23 23:51:21 +02003557 {"filter", 4, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003558 {"hide", 3, FALSE},
3559 {"keepalt", 5, FALSE},
3560 {"keepjumps", 5, FALSE},
3561 {"keepmarks", 3, FALSE},
Bram Moolenaara939e432013-11-09 05:30:26 +01003562 {"keeppatterns", 5, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003563 {"leftabove", 5, FALSE},
3564 {"lockmarks", 3, FALSE},
Bram Moolenaarca9f9582008-09-18 10:44:28 +00003565 {"noautocmd", 3, FALSE},
Bram Moolenaar5803ae62014-03-23 16:04:02 +01003566 {"noswapfile", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003567 {"rightbelow", 6, FALSE},
3568 {"sandbox", 3, FALSE},
3569 {"silent", 3, FALSE},
3570 {"tab", 3, TRUE},
3571 {"topleft", 2, FALSE},
Bram Moolenaar8e258a42009-07-09 13:55:43 +00003572 {"unsilent", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003573 {"verbose", 4, TRUE},
3574 {"vertical", 4, FALSE},
3575};
3576
3577/*
3578 * Return length of a command modifier (including optional count).
3579 * Return zero when it's not a modifier.
3580 */
3581 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003582modifier_len(char_u *cmd)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003583{
3584 int i, j;
3585 char_u *p = cmd;
3586
3587 if (VIM_ISDIGIT(*cmd))
3588 p = skipwhite(skipdigits(cmd));
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00003589 for (i = 0; i < (int)(sizeof(cmdmods) / sizeof(struct cmdmod)); ++i)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003590 {
3591 for (j = 0; p[j] != NUL; ++j)
3592 if (p[j] != cmdmods[i].name[j])
3593 break;
Bram Moolenaar2d473ab2013-06-12 17:12:24 +02003594 if (!ASCII_ISALPHA(p[j]) && j >= cmdmods[i].minlen
Bram Moolenaared53fb92007-11-24 20:50:24 +00003595 && (p == cmd || cmdmods[i].has_count))
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00003596 return j + (int)(p - cmd);
Bram Moolenaared53fb92007-11-24 20:50:24 +00003597 }
3598 return 0;
3599}
3600
Bram Moolenaar071d4272004-06-13 20:20:40 +00003601/*
3602 * Return > 0 if an Ex command "name" exists.
3603 * Return 2 if there is an exact match.
3604 * Return 3 if there is an ambiguous match.
3605 */
3606 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003607cmd_exists(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003608{
3609 exarg_T ea;
3610 int full = FALSE;
3611 int i;
3612 int j;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003613 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003614
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003615 // Check command modifiers.
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00003616 for (i = 0; i < (int)(sizeof(cmdmods) / sizeof(struct cmdmod)); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003617 {
3618 for (j = 0; name[j] != NUL; ++j)
3619 if (name[j] != cmdmods[i].name[j])
3620 break;
3621 if (name[j] == NUL && j >= cmdmods[i].minlen)
3622 return (cmdmods[i].name[j] == NUL ? 2 : 1);
3623 }
3624
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003625 // Check built-in commands and user defined commands.
3626 // For ":2match" and ":3match" we need to skip the number.
Bram Moolenaara9587612006-05-04 21:47:50 +00003627 ea.cmd = (*name == '2' || *name == '3') ? name + 1 : name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003628 ea.cmdidx = (cmdidx_T)0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003629 p = find_ex_command(&ea, &full, NULL, NULL);
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003630 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003631 return 3;
Bram Moolenaara9587612006-05-04 21:47:50 +00003632 if (vim_isdigit(*name) && ea.cmdidx != CMD_match)
3633 return 0;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003634 if (*skipwhite(p) != NUL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003635 return 0; // trailing garbage
Bram Moolenaar071d4272004-06-13 20:20:40 +00003636 return (ea.cmdidx == CMD_SIZE ? 0 : (full ? 2 : 1));
3637}
3638#endif
3639
Bram Moolenaard0190392019-08-23 21:17:35 +02003640 cmdidx_T
3641excmd_get_cmdidx(char_u *cmd, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003642{
Bram Moolenaard0190392019-08-23 21:17:35 +02003643 cmdidx_T idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003644
Bram Moolenaard0190392019-08-23 21:17:35 +02003645 for (idx = (cmdidx_T)0; (int)idx < (int)CMD_SIZE;
3646 idx = (cmdidx_T)((int)idx + 1))
3647 if (STRNCMP(cmdnames[(int)idx].cmd_name, cmd, (size_t)len) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003648 break;
3649
Bram Moolenaard0190392019-08-23 21:17:35 +02003650 return idx;
3651}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003652
Bram Moolenaard0190392019-08-23 21:17:35 +02003653 long
3654excmd_get_argt(cmdidx_T idx)
3655{
3656 return (long)cmdnames[(int)idx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003657}
3658
3659/*
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003660 * Skip a range specifier of the form: addr [,addr] [;addr] ..
Bram Moolenaar071d4272004-06-13 20:20:40 +00003661 *
3662 * Backslashed delimiters after / or ? will be skipped, and commands will
3663 * not be expanded between /'s and ?'s or after "'".
3664 *
Bram Moolenaardf069ee2020-06-22 23:02:51 +02003665 * Also skip white space and ":" characters after the range.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003666 * Returns the "cmd" pointer advanced to beyond the range.
3667 */
3668 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003669skip_range(
3670 char_u *cmd,
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02003671 int skip_star, // skip "*" used for Visual range
3672 int *ctx) // pointer to xp_context or NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003673{
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003674 unsigned delim;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003675
Bram Moolenaarcbf20fb2017-02-03 21:19:04 +01003676 while (vim_strchr((char_u *)" \t0123456789.$%'/?-+,;\\", *cmd) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003677 {
Bram Moolenaarcbf20fb2017-02-03 21:19:04 +01003678 if (*cmd == '\\')
3679 {
3680 if (cmd[1] == '?' || cmd[1] == '/' || cmd[1] == '&')
3681 ++cmd;
3682 else
3683 break;
3684 }
3685 else if (*cmd == '\'')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003686 {
3687 if (*++cmd == NUL && ctx != NULL)
3688 *ctx = EXPAND_NOTHING;
3689 }
3690 else if (*cmd == '/' || *cmd == '?')
3691 {
3692 delim = *cmd++;
3693 while (*cmd != NUL && *cmd != delim)
3694 if (*cmd++ == '\\' && *cmd != NUL)
3695 ++cmd;
3696 if (*cmd == NUL && ctx != NULL)
3697 *ctx = EXPAND_NOTHING;
3698 }
3699 if (*cmd != NUL)
3700 ++cmd;
3701 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00003702
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003703 // Skip ":" and white space.
Bram Moolenaardf177f62005-02-22 08:39:57 +00003704 while (*cmd == ':')
3705 cmd = skipwhite(cmd + 1);
3706
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02003707 // Skip "*" used for Visual range.
3708 if (skip_star && *cmd == '*' && vim_strchr(p_cpo, CPO_STAR) == NULL)
3709 cmd = skipwhite(cmd + 1);
3710
Bram Moolenaar071d4272004-06-13 20:20:40 +00003711 return cmd;
3712}
3713
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003714 static void
3715addr_error(cmd_addr_T addr_type)
3716{
3717 if (addr_type == ADDR_NONE)
3718 emsg(_(e_norange));
3719 else
3720 emsg(_(e_invrange));
3721}
3722
Bram Moolenaar071d4272004-06-13 20:20:40 +00003723/*
Bram Moolenaarc2af0af2020-08-23 21:06:02 +02003724 * Return the default address for an address type.
3725 */
3726 static linenr_T
3727default_address(exarg_T *eap)
3728{
3729 linenr_T lnum = 0;
3730
3731 switch (eap->addr_type)
3732 {
3733 case ADDR_LINES:
3734 case ADDR_OTHER:
3735 // Default is the cursor line number. Avoid using an invalid
3736 // line number though.
3737 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
3738 lnum = curbuf->b_ml.ml_line_count;
3739 else
3740 lnum = curwin->w_cursor.lnum;
3741 break;
3742 case ADDR_WINDOWS:
3743 lnum = CURRENT_WIN_NR;
3744 break;
3745 case ADDR_ARGUMENTS:
3746 lnum = curwin->w_arg_idx + 1;
3747 if (lnum > ARGCOUNT)
3748 lnum = ARGCOUNT;
3749 break;
3750 case ADDR_LOADED_BUFFERS:
3751 case ADDR_BUFFERS:
3752 lnum = curbuf->b_fnum;
3753 break;
3754 case ADDR_TABS:
3755 lnum = CURRENT_TAB_NR;
3756 break;
3757 case ADDR_TABS_RELATIVE:
3758 case ADDR_UNSIGNED:
3759 lnum = 1;
3760 break;
3761 case ADDR_QUICKFIX:
3762#ifdef FEAT_QUICKFIX
3763 lnum = qf_get_cur_idx(eap);
3764#endif
3765 break;
3766 case ADDR_QUICKFIX_VALID:
3767#ifdef FEAT_QUICKFIX
3768 lnum = qf_get_cur_valid_idx(eap);
3769#endif
3770 break;
3771 case ADDR_NONE:
3772 // Will give an error later if a range is found.
3773 break;
3774 }
3775 return lnum;
3776}
3777
3778/*
Bram Moolenaar198cb662018-09-06 21:44:17 +02003779 * Get a single EX address.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003780 *
3781 * Set ptr to the next character after the part that was interpreted.
3782 * Set ptr to NULL when an error is encountered.
Bram Moolenaar198cb662018-09-06 21:44:17 +02003783 * This may set the last used search pattern.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003784 *
3785 * Return MAXLNUM when no Ex address was found.
3786 */
3787 static linenr_T
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003788get_address(
3789 exarg_T *eap UNUSED,
3790 char_u **ptr,
Bram Moolenaar1b03a192019-12-08 17:08:29 +01003791 cmd_addr_T addr_type,
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02003792 int skip, // only skip the address, don't use it
3793 int silent, // no errors or side effects
3794 int to_other_file, // flag: may jump to other file
3795 int address_count UNUSED) // 1 for first address, >1 after comma
Bram Moolenaar071d4272004-06-13 20:20:40 +00003796{
3797 int c;
3798 int i;
3799 long n;
3800 char_u *cmd;
3801 pos_T pos;
3802 pos_T *fp;
3803 linenr_T lnum;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003804 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003805
3806 cmd = skipwhite(*ptr);
3807 lnum = MAXLNUM;
3808 do
3809 {
3810 switch (*cmd)
3811 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003812 case '.': // '.' - Cursor position
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003813 ++cmd;
3814 switch (addr_type)
3815 {
3816 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02003817 case ADDR_OTHER:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003818 lnum = curwin->w_cursor.lnum;
3819 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003820 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01003821 lnum = CURRENT_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003822 break;
3823 case ADDR_ARGUMENTS:
3824 lnum = curwin->w_arg_idx + 1;
3825 break;
3826 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003827 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003828 lnum = curbuf->b_fnum;
3829 break;
3830 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01003831 lnum = CURRENT_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003832 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02003833 case ADDR_NONE:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003834 case ADDR_TABS_RELATIVE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02003835 case ADDR_UNSIGNED:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003836 addr_error(addr_type);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01003837 cmd = NULL;
3838 goto error;
3839 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003840 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003841#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02003842 lnum = qf_get_cur_idx(eap);
3843#endif
3844 break;
3845 case ADDR_QUICKFIX_VALID:
3846#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003847 lnum = qf_get_cur_valid_idx(eap);
Bram Moolenaare906c502015-09-09 21:10:39 +02003848#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003849 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003850 }
3851 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003852
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003853 case '$': // '$' - last line
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003854 ++cmd;
3855 switch (addr_type)
3856 {
3857 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02003858 case ADDR_OTHER:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003859 lnum = curbuf->b_ml.ml_line_count;
3860 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003861 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01003862 lnum = LAST_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003863 break;
3864 case ADDR_ARGUMENTS:
3865 lnum = ARGCOUNT;
3866 break;
3867 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003868 buf = lastbuf;
3869 while (buf->b_ml.ml_mfp == NULL)
3870 {
3871 if (buf->b_prev == NULL)
3872 break;
3873 buf = buf->b_prev;
3874 }
3875 lnum = buf->b_fnum;
3876 break;
3877 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003878 lnum = lastbuf->b_fnum;
3879 break;
3880 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01003881 lnum = LAST_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003882 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02003883 case ADDR_NONE:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003884 case ADDR_TABS_RELATIVE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02003885 case ADDR_UNSIGNED:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003886 addr_error(addr_type);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01003887 cmd = NULL;
3888 goto error;
3889 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003890 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003891#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003892 lnum = qf_get_size(eap);
3893 if (lnum == 0)
3894 lnum = 1;
Bram Moolenaare906c502015-09-09 21:10:39 +02003895#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003896 break;
Bram Moolenaar25190db2019-05-04 15:05:28 +02003897 case ADDR_QUICKFIX_VALID:
3898#ifdef FEAT_QUICKFIX
3899 lnum = qf_get_valid_size(eap);
3900 if (lnum == 0)
3901 lnum = 1;
3902#endif
3903 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003904 }
3905 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003906
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003907 case '\'': // ''' - mark
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003908 if (*++cmd == NUL)
3909 {
3910 cmd = NULL;
3911 goto error;
3912 }
3913 if (addr_type != ADDR_LINES)
3914 {
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003915 addr_error(addr_type);
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01003916 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003917 goto error;
3918 }
3919 if (skip)
3920 ++cmd;
3921 else
3922 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003923 // Only accept a mark in another file when it is
3924 // used by itself: ":'M".
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003925 fp = getmark(*cmd, to_other_file && cmd[1] == NUL);
3926 ++cmd;
3927 if (fp == (pos_T *)-1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003928 // Jumped to another file.
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003929 lnum = curwin->w_cursor.lnum;
3930 else
3931 {
3932 if (check_mark(fp) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003933 {
3934 cmd = NULL;
3935 goto error;
3936 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003937 lnum = fp->lnum;
3938 }
3939 }
3940 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003941
3942 case '/':
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003943 case '?': // '/' or '?' - search
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003944 c = *cmd++;
3945 if (addr_type != ADDR_LINES)
3946 {
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003947 addr_error(addr_type);
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01003948 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003949 goto error;
3950 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003951 if (skip) // skip "/pat/"
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003952 {
Bram Moolenaarf4e20992020-12-21 19:59:08 +01003953 cmd = skip_regexp(cmd, c, magic_isset());
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003954 if (*cmd == c)
3955 ++cmd;
3956 }
3957 else
3958 {
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02003959 int flags;
3960
3961 pos = curwin->w_cursor; // save curwin->w_cursor
3962
3963 // When '/' or '?' follows another address, start from
3964 // there.
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003965 if (lnum != MAXLNUM)
3966 curwin->w_cursor.lnum = lnum;
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02003967
3968 // Start a forward search at the end of the line (unless
3969 // before the first line).
3970 // Start a backward search at the start of the line.
3971 // This makes sure we never match in the current
3972 // line, and can match anywhere in the
3973 // next/previous line.
Bram Moolenaar8ada6aa2017-12-19 21:23:21 +01003974 if (c == '/' && curwin->w_cursor.lnum > 0)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003975 curwin->w_cursor.col = MAXCOL;
3976 else
3977 curwin->w_cursor.col = 0;
3978 searchcmdlen = 0;
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02003979 flags = silent ? 0 : SEARCH_HIS | SEARCH_MSG;
Bram Moolenaarc036e872020-02-21 21:30:52 +01003980 if (!do_search(NULL, c, c, cmd, 1L, flags, NULL))
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003981 {
3982 curwin->w_cursor = pos;
3983 cmd = NULL;
3984 goto error;
3985 }
3986 lnum = curwin->w_cursor.lnum;
3987 curwin->w_cursor = pos;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003988 // adjust command string pointer
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003989 cmd += searchcmdlen;
3990 }
3991 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003992
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003993 case '\\': // "\?", "\/" or "\&", repeat search
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003994 ++cmd;
3995 if (addr_type != ADDR_LINES)
3996 {
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003997 addr_error(addr_type);
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01003998 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003999 goto error;
4000 }
4001 if (*cmd == '&')
4002 i = RE_SUBST;
4003 else if (*cmd == '?' || *cmd == '/')
4004 i = RE_SEARCH;
4005 else
4006 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004007 emsg(_(e_backslash));
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004008 cmd = NULL;
4009 goto error;
4010 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004011
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004012 if (!skip)
4013 {
4014 /*
4015 * When search follows another address, start from
4016 * there.
4017 */
4018 if (lnum != MAXLNUM)
4019 pos.lnum = lnum;
4020 else
4021 pos.lnum = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004022
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004023 /*
4024 * Start the search just like for the above
4025 * do_search().
4026 */
4027 if (*cmd != '?')
4028 pos.col = MAXCOL;
4029 else
4030 pos.col = 0;
Bram Moolenaarbd8539a2015-08-11 18:53:03 +02004031 pos.coladd = 0;
Bram Moolenaar5d24a222018-12-23 19:10:09 +01004032 if (searchit(curwin, curbuf, &pos, NULL,
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004033 *cmd == '?' ? BACKWARD : FORWARD,
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004034 (char_u *)"", 1L, SEARCH_MSG, i, NULL) != FAIL)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004035 lnum = pos.lnum;
4036 else
4037 {
4038 cmd = NULL;
4039 goto error;
4040 }
4041 }
4042 ++cmd;
4043 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004044
4045 default:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004046 if (VIM_ISDIGIT(*cmd)) // absolute line number
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004047 lnum = getdigits(&cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004048 }
4049
4050 for (;;)
4051 {
4052 cmd = skipwhite(cmd);
4053 if (*cmd != '-' && *cmd != '+' && !VIM_ISDIGIT(*cmd))
4054 break;
4055
4056 if (lnum == MAXLNUM)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004057 {
4058 switch (addr_type)
4059 {
4060 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004061 case ADDR_OTHER:
4062 // "+1" is same as ".+1"
Bram Moolenaarf240e182014-11-27 18:33:02 +01004063 lnum = curwin->w_cursor.lnum;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004064 break;
4065 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004066 lnum = CURRENT_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004067 break;
4068 case ADDR_ARGUMENTS:
4069 lnum = curwin->w_arg_idx + 1;
4070 break;
4071 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004072 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004073 lnum = curbuf->b_fnum;
4074 break;
4075 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004076 lnum = CURRENT_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004077 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004078 case ADDR_TABS_RELATIVE:
4079 lnum = 1;
4080 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004081 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004082#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02004083 lnum = qf_get_cur_idx(eap);
4084#endif
4085 break;
4086 case ADDR_QUICKFIX_VALID:
4087#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004088 lnum = qf_get_cur_valid_idx(eap);
Bram Moolenaare906c502015-09-09 21:10:39 +02004089#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004090 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02004091 case ADDR_NONE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02004092 case ADDR_UNSIGNED:
4093 lnum = 0;
Bram Moolenaarb7316892019-05-01 18:08:42 +02004094 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004095 }
4096 }
4097
Bram Moolenaar071d4272004-06-13 20:20:40 +00004098 if (VIM_ISDIGIT(*cmd))
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004099 i = '+'; // "number" is same as "+number"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004100 else
4101 i = *cmd++;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004102 if (!VIM_ISDIGIT(*cmd)) // '+' is '+1', but '+0' is not '+1'
Bram Moolenaar071d4272004-06-13 20:20:40 +00004103 n = 1;
4104 else
4105 n = getdigits(&cmd);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004106
4107 if (addr_type == ADDR_TABS_RELATIVE)
4108 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004109 emsg(_(e_invrange));
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004110 cmd = NULL;
4111 goto error;
4112 }
4113 else if (addr_type == ADDR_LOADED_BUFFERS
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004114 || addr_type == ADDR_BUFFERS)
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004115 lnum = compute_buffer_local_count(
4116 addr_type, lnum, (i == '-') ? -1 * n : n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004117 else
Bram Moolenaarded27822017-01-02 14:27:34 +01004118 {
4119#ifdef FEAT_FOLDING
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004120 // Relative line addressing, need to adjust for folded lines
4121 // now, but only do it after the first address.
Bram Moolenaarded27822017-01-02 14:27:34 +01004122 if (addr_type == ADDR_LINES && (i == '-' || i == '+')
4123 && address_count >= 2)
4124 (void)hasFolding(lnum, NULL, &lnum);
4125#endif
4126 if (i == '-')
4127 lnum -= n;
4128 else
4129 lnum += n;
4130 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004131 }
4132 } while (*cmd == '/' || *cmd == '?');
4133
4134error:
4135 *ptr = cmd;
4136 return lnum;
4137}
4138
4139/*
Bram Moolenaarc2af0af2020-08-23 21:06:02 +02004140 * Set eap->line1 and eap->line2 to the whole range.
4141 * Used for commands with the EX_DFLALL flag and no range given.
4142 */
4143 static void
4144address_default_all(exarg_T *eap)
4145{
4146 eap->line1 = 1;
4147 switch (eap->addr_type)
4148 {
4149 case ADDR_LINES:
4150 case ADDR_OTHER:
4151 eap->line2 = curbuf->b_ml.ml_line_count;
4152 break;
4153 case ADDR_LOADED_BUFFERS:
4154 {
4155 buf_T *buf = firstbuf;
4156
4157 while (buf->b_next != NULL && buf->b_ml.ml_mfp == NULL)
4158 buf = buf->b_next;
4159 eap->line1 = buf->b_fnum;
4160 buf = lastbuf;
4161 while (buf->b_prev != NULL && buf->b_ml.ml_mfp == NULL)
4162 buf = buf->b_prev;
4163 eap->line2 = buf->b_fnum;
4164 }
4165 break;
4166 case ADDR_BUFFERS:
4167 eap->line1 = firstbuf->b_fnum;
4168 eap->line2 = lastbuf->b_fnum;
4169 break;
4170 case ADDR_WINDOWS:
4171 eap->line2 = LAST_WIN_NR;
4172 break;
4173 case ADDR_TABS:
4174 eap->line2 = LAST_TAB_NR;
4175 break;
4176 case ADDR_TABS_RELATIVE:
4177 eap->line2 = 1;
4178 break;
4179 case ADDR_ARGUMENTS:
4180 if (ARGCOUNT == 0)
4181 eap->line1 = eap->line2 = 0;
4182 else
4183 eap->line2 = ARGCOUNT;
4184 break;
4185 case ADDR_QUICKFIX_VALID:
4186#ifdef FEAT_QUICKFIX
4187 eap->line2 = qf_get_valid_size(eap);
4188 if (eap->line2 == 0)
4189 eap->line2 = 1;
4190#endif
4191 break;
4192 case ADDR_NONE:
4193 case ADDR_UNSIGNED:
4194 case ADDR_QUICKFIX:
4195 iemsg(_("INTERNAL: Cannot use EX_DFLALL with ADDR_NONE, ADDR_UNSIGNED or ADDR_QUICKFIX"));
4196 break;
4197 }
4198}
4199
4200
4201/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00004202 * Get flags from an Ex command argument.
4203 */
4204 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004205get_flags(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00004206{
4207 while (vim_strchr((char_u *)"lp#", *eap->arg) != NULL)
4208 {
4209 if (*eap->arg == 'l')
4210 eap->flags |= EXFLAG_LIST;
4211 else if (*eap->arg == 'p')
4212 eap->flags |= EXFLAG_PRINT;
4213 else
4214 eap->flags |= EXFLAG_NR;
4215 eap->arg = skipwhite(eap->arg + 1);
4216 }
4217}
4218
4219/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004220 * Function called for command which is Not Implemented. NI!
4221 */
4222 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004223ex_ni(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004224{
4225 if (!eap->skip)
Bram Moolenaar8930caa2020-07-23 16:37:03 +02004226 eap->errmsg =
4227 _("E319: Sorry, the command is not available in this version");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004228}
4229
Bram Moolenaar7bb75552007-07-16 18:39:49 +00004230#ifdef HAVE_EX_SCRIPT_NI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004231/*
4232 * Function called for script command which is Not Implemented. NI!
4233 * Skips over ":perl <<EOF" constructs.
4234 */
4235 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004236ex_script_ni(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004237{
4238 if (!eap->skip)
4239 ex_ni(eap);
4240 else
4241 vim_free(script_get(eap, eap->arg));
4242}
4243#endif
4244
4245/*
4246 * Check range in Ex command for validity.
4247 * Return NULL when valid, error message when invalid.
4248 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004249 static char *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004250invalid_range(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004251{
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004252 buf_T *buf;
Bram Moolenaar25190db2019-05-04 15:05:28 +02004253
Bram Moolenaar071d4272004-06-13 20:20:40 +00004254 if ( eap->line1 < 0
4255 || eap->line2 < 0
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004256 || eap->line1 > eap->line2)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004257 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004258
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004259 if (eap->argt & EX_RANGE)
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004260 {
Bram Moolenaarb7316892019-05-01 18:08:42 +02004261 switch (eap->addr_type)
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004262 {
4263 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004264 if (eap->line2 > curbuf->b_ml.ml_line_count
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004265#ifdef FEAT_DIFF
4266 + (eap->cmdidx == CMD_diffget)
4267#endif
4268 )
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004269 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004270 break;
4271 case ADDR_ARGUMENTS:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004272 // add 1 if ARGCOUNT is 0
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004273 if (eap->line2 > ARGCOUNT + (!ARGCOUNT))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004274 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004275 break;
4276 case ADDR_BUFFERS:
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02004277 // Only a boundary check, not whether the buffers actually
4278 // exist.
4279 if (eap->line1 < 1 || eap->line2 > get_highest_fnum())
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004280 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004281 break;
4282 case ADDR_LOADED_BUFFERS:
4283 buf = firstbuf;
4284 while (buf->b_ml.ml_mfp == NULL)
4285 {
4286 if (buf->b_next == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004287 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004288 buf = buf->b_next;
4289 }
4290 if (eap->line1 < buf->b_fnum)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004291 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004292 buf = lastbuf;
4293 while (buf->b_ml.ml_mfp == NULL)
4294 {
4295 if (buf->b_prev == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004296 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004297 buf = buf->b_prev;
4298 }
4299 if (eap->line2 > buf->b_fnum)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004300 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004301 break;
4302 case ADDR_WINDOWS:
Bram Moolenaar8be63882015-01-14 11:25:05 +01004303 if (eap->line2 > LAST_WIN_NR)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004304 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004305 break;
4306 case ADDR_TABS:
4307 if (eap->line2 > LAST_TAB_NR)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004308 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004309 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004310 case ADDR_TABS_RELATIVE:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004311 case ADDR_OTHER:
4312 // Any range is OK.
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004313 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004314 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004315#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02004316 // No error for value that is too big, will use the last entry.
4317 if (eap->line2 <= 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004318 return _(e_invrange);
Bram Moolenaare906c502015-09-09 21:10:39 +02004319#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004320 break;
Bram Moolenaar25190db2019-05-04 15:05:28 +02004321 case ADDR_QUICKFIX_VALID:
4322#ifdef FEAT_QUICKFIX
4323 if ((eap->line2 != 1 && eap->line2 > qf_get_valid_size(eap))
4324 || eap->line2 < 0)
4325 return _(e_invrange);
4326#endif
4327 break;
4328 case ADDR_UNSIGNED:
4329 if (eap->line2 < 0)
4330 return _(e_invrange);
4331 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02004332 case ADDR_NONE:
4333 // Will give an error elsewhere.
4334 break;
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004335 }
4336 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004337 return NULL;
4338}
4339
4340/*
4341 * Correct the range for zero line number, if required.
4342 */
4343 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004344correct_range(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004345{
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004346 if (!(eap->argt & EX_ZEROR)) // zero in range not allowed
Bram Moolenaar071d4272004-06-13 20:20:40 +00004347 {
4348 if (eap->line1 == 0)
4349 eap->line1 = 1;
4350 if (eap->line2 == 0)
4351 eap->line2 = 1;
4352 }
4353}
4354
Bram Moolenaar748bf032005-02-02 23:04:36 +00004355#ifdef FEAT_QUICKFIX
Bram Moolenaar748bf032005-02-02 23:04:36 +00004356/*
4357 * For a ":vimgrep" or ":vimgrepadd" command return a pointer past the
4358 * pattern. Otherwise return eap->arg.
4359 */
4360 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004361skip_grep_pat(exarg_T *eap)
Bram Moolenaar748bf032005-02-02 23:04:36 +00004362{
4363 char_u *p = eap->arg;
4364
Bram Moolenaara37420f2006-02-04 22:37:47 +00004365 if (*p != NUL && (eap->cmdidx == CMD_vimgrep || eap->cmdidx == CMD_lvimgrep
4366 || eap->cmdidx == CMD_vimgrepadd
4367 || eap->cmdidx == CMD_lvimgrepadd
4368 || grep_internal(eap->cmdidx)))
Bram Moolenaar748bf032005-02-02 23:04:36 +00004369 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00004370 p = skip_vimgrep_pat(p, NULL, NULL);
Bram Moolenaar748bf032005-02-02 23:04:36 +00004371 if (p == NULL)
4372 p = eap->arg;
Bram Moolenaar748bf032005-02-02 23:04:36 +00004373 }
4374 return p;
4375}
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004376
4377/*
4378 * For the ":make" and ":grep" commands insert the 'makeprg'/'grepprg' option
4379 * in the command line, so that things like % get expanded.
4380 */
4381 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004382replace_makeprg(exarg_T *eap, char_u *p, char_u **cmdlinep)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004383{
4384 char_u *new_cmdline;
4385 char_u *program;
4386 char_u *pos;
4387 char_u *ptr;
4388 int len;
4389 int i;
4390
4391 /*
4392 * Don't do it when ":vimgrep" is used for ":grep".
4393 */
Bram Moolenaara37420f2006-02-04 22:37:47 +00004394 if ((eap->cmdidx == CMD_make || eap->cmdidx == CMD_lmake
4395 || eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4396 || eap->cmdidx == CMD_grepadd
4397 || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004398 && !grep_internal(eap->cmdidx))
4399 {
Bram Moolenaara37420f2006-02-04 22:37:47 +00004400 if (eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4401 || eap->cmdidx == CMD_grepadd || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004402 {
4403 if (*curbuf->b_p_gp == NUL)
4404 program = p_gp;
4405 else
4406 program = curbuf->b_p_gp;
4407 }
4408 else
4409 {
4410 if (*curbuf->b_p_mp == NUL)
4411 program = p_mp;
4412 else
4413 program = curbuf->b_p_mp;
4414 }
4415
4416 p = skipwhite(p);
4417
4418 if ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4419 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004420 // replace $* by given arguments
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004421 i = 1;
4422 while ((pos = (char_u *)strstr((char *)pos + 2, "$*")) != NULL)
4423 ++i;
4424 len = (int)STRLEN(p);
Bram Moolenaar51e14382019-05-25 20:21:28 +02004425 new_cmdline = alloc(STRLEN(program) + i * (len - 2) + 1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004426 if (new_cmdline == NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004427 return NULL; // out of memory
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004428 ptr = new_cmdline;
4429 while ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4430 {
4431 i = (int)(pos - program);
4432 STRNCPY(ptr, program, i);
4433 STRCPY(ptr += i, p);
4434 ptr += len;
4435 program = pos + 2;
4436 }
4437 STRCPY(ptr, program);
4438 }
4439 else
4440 {
Bram Moolenaar51e14382019-05-25 20:21:28 +02004441 new_cmdline = alloc(STRLEN(program) + STRLEN(p) + 2);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004442 if (new_cmdline == NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004443 return NULL; // out of memory
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004444 STRCPY(new_cmdline, program);
4445 STRCAT(new_cmdline, " ");
4446 STRCAT(new_cmdline, p);
4447 }
4448 msg_make(p);
4449
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004450 // 'eap->cmd' is not set here, because it is not used at CMD_make
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004451 vim_free(*cmdlinep);
4452 *cmdlinep = new_cmdline;
4453 p = new_cmdline;
4454 }
4455 return p;
4456}
Bram Moolenaar748bf032005-02-02 23:04:36 +00004457#endif
4458
Bram Moolenaar071d4272004-06-13 20:20:40 +00004459/*
4460 * Expand file name in Ex command argument.
Bram Moolenaar0abb4272019-06-15 16:06:00 +02004461 * When an error is detected, "errormsgp" is set to a non-NULL pointer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004462 * Return FAIL for failure, OK otherwise.
4463 */
4464 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004465expand_filename(
4466 exarg_T *eap,
4467 char_u **cmdlinep,
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004468 char **errormsgp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004469{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004470 int has_wildcards; // need to expand wildcards
Bram Moolenaar071d4272004-06-13 20:20:40 +00004471 char_u *repl;
4472 int srclen;
4473 char_u *p;
4474 int n;
Bram Moolenaar63b92542007-03-27 14:57:09 +00004475 int escaped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004476
Bram Moolenaar748bf032005-02-02 23:04:36 +00004477#ifdef FEAT_QUICKFIX
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004478 // Skip a regexp pattern for ":vimgrep[add] pat file..."
Bram Moolenaar748bf032005-02-02 23:04:36 +00004479 p = skip_grep_pat(eap);
4480#else
4481 p = eap->arg;
4482#endif
4483
Bram Moolenaar071d4272004-06-13 20:20:40 +00004484 /*
4485 * Decide to expand wildcards *before* replacing '%', '#', etc. If
4486 * the file name contains a wildcard it should not cause expanding.
4487 * (it will be expanded anyway if there is a wildcard before replacing).
4488 */
Bram Moolenaar748bf032005-02-02 23:04:36 +00004489 has_wildcards = mch_has_wildcard(p);
4490 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004491 {
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004492#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004493 // Skip over `=expr`, wildcards in it are not expanded.
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004494 if (p[0] == '`' && p[1] == '=')
4495 {
4496 p += 2;
Bram Moolenaar683581e2020-10-22 21:22:58 +02004497 (void)skip_expr(&p, NULL);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004498 if (*p == '`')
4499 ++p;
4500 continue;
4501 }
4502#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004503 /*
4504 * Quick check if this cannot be the start of a special string.
4505 * Also removes backslash before '%', '#' and '<'.
4506 */
4507 if (vim_strchr((char_u *)"%#<", *p) == NULL)
4508 {
4509 ++p;
4510 continue;
4511 }
4512
4513 /*
4514 * Try to find a match at this position.
4515 */
Bram Moolenaar63b92542007-03-27 14:57:09 +00004516 repl = eval_vars(p, eap->arg, &srclen, &(eap->do_ecmd_lnum),
4517 errormsgp, &escaped);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004518 if (*errormsgp != NULL) // error detected
Bram Moolenaar071d4272004-06-13 20:20:40 +00004519 return FAIL;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004520 if (repl == NULL) // no match found
Bram Moolenaar071d4272004-06-13 20:20:40 +00004521 {
4522 p += srclen;
4523 continue;
4524 }
4525
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004526 // Wildcards won't be expanded below, the replacement is taken
4527 // literally. But do expand "~/file", "~user/file" and "$HOME/file".
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00004528 if (vim_strchr(repl, '$') != NULL || vim_strchr(repl, '~') != NULL)
4529 {
4530 char_u *l = repl;
4531
4532 repl = expand_env_save(repl);
4533 vim_free(l);
4534 }
4535
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004536 // Need to escape white space et al. with a backslash.
4537 // Don't do this for:
4538 // - replacement that already has been escaped: "##"
4539 // - shell commands (may have to use quotes instead).
4540 // - non-unix systems when there is a single argument (spaces don't
4541 // separate arguments then).
Bram Moolenaar071d4272004-06-13 20:20:40 +00004542 if (!eap->usefilter
Bram Moolenaar63b92542007-03-27 14:57:09 +00004543 && !escaped
Bram Moolenaar071d4272004-06-13 20:20:40 +00004544 && eap->cmdidx != CMD_bang
Bram Moolenaar071d4272004-06-13 20:20:40 +00004545 && eap->cmdidx != CMD_grep
4546 && eap->cmdidx != CMD_grepadd
Bram Moolenaarbf15b8d2017-06-04 20:43:48 +02004547 && eap->cmdidx != CMD_hardcopy
Bram Moolenaar67883b42017-07-27 22:57:00 +02004548 && eap->cmdidx != CMD_lgrep
4549 && eap->cmdidx != CMD_lgrepadd
4550 && eap->cmdidx != CMD_lmake
4551 && eap->cmdidx != CMD_make
4552 && eap->cmdidx != CMD_terminal
Bram Moolenaar071d4272004-06-13 20:20:40 +00004553#ifndef UNIX
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004554 && !(eap->argt & EX_NOSPC)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004555#endif
4556 )
4557 {
4558 char_u *l;
4559#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004560 // Don't escape a backslash here, because rem_backslash() doesn't
4561 // remove it later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004562 static char_u *nobslash = (char_u *)" \t\"|";
4563# define ESCAPE_CHARS nobslash
4564#else
4565# define ESCAPE_CHARS escape_chars
4566#endif
4567
4568 for (l = repl; *l; ++l)
4569 if (vim_strchr(ESCAPE_CHARS, *l) != NULL)
4570 {
4571 l = vim_strsave_escaped(repl, ESCAPE_CHARS);
4572 if (l != NULL)
4573 {
4574 vim_free(repl);
4575 repl = l;
4576 }
4577 break;
4578 }
4579 }
4580
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004581 // For a shell command a '!' must be escaped.
Bram Moolenaar67883b42017-07-27 22:57:00 +02004582 if ((eap->usefilter || eap->cmdidx == CMD_bang
4583 || eap->cmdidx == CMD_terminal)
Bram Moolenaar31b7d382014-04-01 18:54:48 +02004584 && vim_strpbrk(repl, (char_u *)"!") != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004585 {
4586 char_u *l;
4587
Bram Moolenaar31b7d382014-04-01 18:54:48 +02004588 l = vim_strsave_escaped(repl, (char_u *)"!");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004589 if (l != NULL)
4590 {
4591 vim_free(repl);
4592 repl = l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004593 }
4594 }
4595
4596 p = repl_cmdline(eap, p, srclen, repl, cmdlinep);
4597 vim_free(repl);
4598 if (p == NULL)
4599 return FAIL;
4600 }
4601
4602 /*
4603 * One file argument: Expand wildcards.
4604 * Don't do this with ":r !command" or ":w !command".
4605 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004606 if ((eap->argt & EX_NOSPC) && !eap->usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004607 {
4608 /*
4609 * May do this twice:
4610 * 1. Replace environment variables.
4611 * 2. Replace any other wildcards, remove backslashes.
4612 */
4613 for (n = 1; n <= 2; ++n)
4614 {
4615 if (n == 2)
4616 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004617 /*
4618 * Halve the number of backslashes (this is Vi compatible).
4619 * For Unix and OS/2, when wildcards are expanded, this is
4620 * done by ExpandOne() below.
4621 */
Bram Moolenaare7fedb62015-12-31 19:07:19 +01004622#if defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004623 if (!has_wildcards)
4624#endif
4625 backslash_halve(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004626 }
4627
4628 if (has_wildcards)
4629 {
4630 if (n == 1)
4631 {
4632 /*
4633 * First loop: May expand environment variables. This
4634 * can be done much faster with expand_env() than with
4635 * something else (e.g., calling a shell).
4636 * After expanding environment variables, check again
4637 * if there are still wildcards present.
4638 */
4639 if (vim_strchr(eap->arg, '$') != NULL
4640 || vim_strchr(eap->arg, '~') != NULL)
4641 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +00004642 expand_env_esc(eap->arg, NameBuff, MAXPATHL,
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00004643 TRUE, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004644 has_wildcards = mch_has_wildcard(NameBuff);
4645 p = NameBuff;
4646 }
4647 else
4648 p = NULL;
4649 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004650 else // n == 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00004651 {
4652 expand_T xpc;
Bram Moolenaarb40c2572019-10-19 21:01:05 +02004653 int options = WILD_LIST_NOTFOUND
4654 | WILD_NOERROR | WILD_ADD_SLASH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004655
4656 ExpandInit(&xpc);
4657 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar94950a92010-12-02 16:01:29 +01004658 if (p_wic)
4659 options += WILD_ICASE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004660 p = ExpandOne(&xpc, eap->arg, NULL,
Bram Moolenaar94950a92010-12-02 16:01:29 +01004661 options, WILD_EXPAND_FREE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004662 if (p == NULL)
4663 return FAIL;
4664 }
4665 if (p != NULL)
4666 {
4667 (void)repl_cmdline(eap, eap->arg, (int)STRLEN(eap->arg),
4668 p, cmdlinep);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004669 if (n == 2) // p came from ExpandOne()
Bram Moolenaar071d4272004-06-13 20:20:40 +00004670 vim_free(p);
4671 }
4672 }
4673 }
4674 }
4675 return OK;
4676}
4677
4678/*
4679 * Replace part of the command line, keeping eap->cmd, eap->arg and
4680 * eap->nextcmd correct.
4681 * "src" points to the part that is to be replaced, of length "srclen".
4682 * "repl" is the replacement string.
4683 * Returns a pointer to the character after the replaced string.
4684 * Returns NULL for failure.
4685 */
4686 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004687repl_cmdline(
4688 exarg_T *eap,
4689 char_u *src,
4690 int srclen,
4691 char_u *repl,
4692 char_u **cmdlinep)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004693{
4694 int len;
4695 int i;
4696 char_u *new_cmdline;
4697
4698 /*
4699 * The new command line is build in new_cmdline[].
4700 * First allocate it.
4701 * Careful: a "+cmd" argument may have been NUL terminated.
4702 */
4703 len = (int)STRLEN(repl);
4704 i = (int)(src - *cmdlinep) + (int)STRLEN(src + srclen) + len + 3;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004705 if (eap->nextcmd != NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004706 i += (int)STRLEN(eap->nextcmd);// add space for next command
Bram Moolenaar964b3742019-05-24 18:54:09 +02004707 if ((new_cmdline = alloc(i)) == NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004708 return NULL; // out of memory!
Bram Moolenaar071d4272004-06-13 20:20:40 +00004709
4710 /*
4711 * Copy the stuff before the expanded part.
4712 * Copy the expanded stuff.
4713 * Copy what came after the expanded part.
4714 * Copy the next commands, if there are any.
4715 */
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004716 i = (int)(src - *cmdlinep); // length of part before match
Bram Moolenaar071d4272004-06-13 20:20:40 +00004717 mch_memmove(new_cmdline, *cmdlinep, (size_t)i);
Bram Moolenaara3ffd9c2005-07-21 21:03:15 +00004718
Bram Moolenaar071d4272004-06-13 20:20:40 +00004719 mch_memmove(new_cmdline + i, repl, (size_t)len);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004720 i += len; // remember the end of the string
Bram Moolenaar071d4272004-06-13 20:20:40 +00004721 STRCPY(new_cmdline + i, src + srclen);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004722 src = new_cmdline + i; // remember where to continue
Bram Moolenaar071d4272004-06-13 20:20:40 +00004723
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004724 if (eap->nextcmd != NULL) // append next command
Bram Moolenaar071d4272004-06-13 20:20:40 +00004725 {
4726 i = (int)STRLEN(new_cmdline) + 1;
4727 STRCPY(new_cmdline + i, eap->nextcmd);
4728 eap->nextcmd = new_cmdline + i;
4729 }
4730 eap->cmd = new_cmdline + (eap->cmd - *cmdlinep);
4731 eap->arg = new_cmdline + (eap->arg - *cmdlinep);
4732 if (eap->do_ecmd_cmd != NULL && eap->do_ecmd_cmd != dollar_command)
4733 eap->do_ecmd_cmd = new_cmdline + (eap->do_ecmd_cmd - *cmdlinep);
4734 vim_free(*cmdlinep);
4735 *cmdlinep = new_cmdline;
4736
4737 return src;
4738}
4739
4740/*
4741 * Check for '|' to separate commands and '"' to start comments.
4742 */
4743 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004744separate_nextcmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004745{
4746 char_u *p;
4747
Bram Moolenaar86b68352004-12-27 21:59:20 +00004748#ifdef FEAT_QUICKFIX
Bram Moolenaar748bf032005-02-02 23:04:36 +00004749 p = skip_grep_pat(eap);
4750#else
4751 p = eap->arg;
Bram Moolenaar86b68352004-12-27 21:59:20 +00004752#endif
4753
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004754 for ( ; *p; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004755 {
4756 if (*p == Ctrl_V)
4757 {
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004758 if (eap->argt & (EX_CTRLV | EX_XFILE))
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004759 ++p; // skip CTRL-V and next char
Bram Moolenaar071d4272004-06-13 20:20:40 +00004760 else
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004761 // remove CTRL-V and skip next char
Bram Moolenaara7241f52008-06-24 20:39:31 +00004762 STRMOVE(p, p + 1);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004763 if (*p == NUL) // stop at NUL after CTRL-V
Bram Moolenaar071d4272004-06-13 20:20:40 +00004764 break;
4765 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004766
4767#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004768 // Skip over `=expr` when wildcards are expanded.
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004769 else if (p[0] == '`' && p[1] == '=' && (eap->argt & EX_XFILE))
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004770 {
4771 p += 2;
Bram Moolenaar683581e2020-10-22 21:22:58 +02004772 (void)skip_expr(&p, NULL);
Bram Moolenaarcaf73dc2020-10-26 21:39:13 +01004773 if (*p == NUL) // stop at NUL after CTRL-V
4774 break;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004775 }
4776#endif
4777
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004778 // Check for '"': start of comment or '|': next command
4779 // :@" and :*" do not start a comment!
4780 // :redir @" doesn't either.
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02004781 else if ((*p == '"'
Bram Moolenaara26b9702020-04-18 19:53:28 +02004782#ifdef FEAT_EVAL
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02004783 && !in_vim9script()
4784#endif
4785 && !(eap->argt & EX_NOTRLCOM)
4786 && ((eap->cmdidx != CMD_at && eap->cmdidx != CMD_star)
4787 || p != eap->arg)
4788 && (eap->cmdidx != CMD_redir
4789 || p != eap->arg + 1 || p[-1] != '@'))
4790#ifdef FEAT_EVAL
4791 || (*p == '#'
4792 && in_vim9script()
Bram Moolenaarb8a92962020-08-20 18:02:47 +02004793 && !(eap->argt & EX_NOTRLCOM)
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02004794 && p > eap->cmd && VIM_ISWHITE(p[-1]))
Bram Moolenaara26b9702020-04-18 19:53:28 +02004795#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004796 || *p == '|' || *p == '\n')
4797 {
4798 /*
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004799 * We remove the '\' before the '|', unless EX_CTRLV is used
Bram Moolenaar071d4272004-06-13 20:20:40 +00004800 * AND 'b' is present in 'cpoptions'.
4801 */
4802 if ((vim_strchr(p_cpo, CPO_BAR) == NULL
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004803 || !(eap->argt & EX_CTRLV)) && *(p - 1) == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004804 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004805 STRMOVE(p - 1, p); // remove the '\'
Bram Moolenaar071d4272004-06-13 20:20:40 +00004806 --p;
4807 }
4808 else
4809 {
4810 eap->nextcmd = check_nextcmd(p);
4811 *p = NUL;
4812 break;
4813 }
4814 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004815 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00004816
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004817 if (!(eap->argt & EX_NOTRLCOM)) // remove trailing spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00004818 del_trailing_spaces(eap->arg);
4819}
4820
4821/*
4822 * get + command from ex argument
4823 */
4824 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004825getargcmd(char_u **argp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004826{
4827 char_u *arg = *argp;
4828 char_u *command = NULL;
4829
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004830 if (*arg == '+') // +[command]
Bram Moolenaar071d4272004-06-13 20:20:40 +00004831 {
4832 ++arg;
Bram Moolenaar3e451592014-04-02 14:22:05 +02004833 if (vim_isspace(*arg) || *arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004834 command = dollar_command;
4835 else
4836 {
4837 command = arg;
4838 arg = skip_cmd_arg(command, TRUE);
4839 if (*arg != NUL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004840 *arg++ = NUL; // terminate command with NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004841 }
4842
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004843 arg = skipwhite(arg); // skip over spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00004844 *argp = arg;
4845 }
4846 return command;
4847}
4848
4849/*
4850 * Find end of "+command" argument. Skip over "\ " and "\\".
4851 */
Bram Moolenaard0190392019-08-23 21:17:35 +02004852 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004853skip_cmd_arg(
4854 char_u *p,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004855 int rembs) // TRUE to halve the number of backslashes
Bram Moolenaar071d4272004-06-13 20:20:40 +00004856{
4857 while (*p && !vim_isspace(*p))
4858 {
4859 if (*p == '\\' && p[1] != NUL)
4860 {
4861 if (rembs)
Bram Moolenaara7241f52008-06-24 20:39:31 +00004862 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004863 else
4864 ++p;
4865 }
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004866 MB_PTR_ADV(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004867 }
4868 return p;
4869}
4870
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004871 int
4872get_bad_opt(char_u *p, exarg_T *eap)
4873{
4874 if (STRICMP(p, "keep") == 0)
4875 eap->bad_char = BAD_KEEP;
4876 else if (STRICMP(p, "drop") == 0)
4877 eap->bad_char = BAD_DROP;
4878 else if (MB_BYTE2LEN(*p) == 1 && p[1] == NUL)
4879 eap->bad_char = *p;
Bram Moolenaar75808492018-06-12 12:39:41 +02004880 else
4881 return FAIL;
4882 return OK;
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004883}
4884
Bram Moolenaar071d4272004-06-13 20:20:40 +00004885/*
4886 * Get "++opt=arg" argument.
4887 * Return FAIL or OK.
4888 */
4889 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004890getargopt(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004891{
4892 char_u *arg = eap->arg + 2;
4893 int *pp = NULL;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02004894 int bad_char_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004895 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004896
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004897 // ":edit ++[no]bin[ary] file"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004898 if (STRNCMP(arg, "bin", 3) == 0 || STRNCMP(arg, "nobin", 5) == 0)
4899 {
4900 if (*arg == 'n')
4901 {
4902 arg += 2;
4903 eap->force_bin = FORCE_NOBIN;
4904 }
4905 else
4906 eap->force_bin = FORCE_BIN;
4907 if (!checkforcmd(&arg, "binary", 3))
4908 return FAIL;
4909 eap->arg = skipwhite(arg);
4910 return OK;
4911 }
4912
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004913 // ":read ++edit file"
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004914 if (STRNCMP(arg, "edit", 4) == 0)
4915 {
4916 eap->read_edit = TRUE;
4917 eap->arg = skipwhite(arg + 4);
4918 return OK;
4919 }
4920
Bram Moolenaar071d4272004-06-13 20:20:40 +00004921 if (STRNCMP(arg, "ff", 2) == 0)
4922 {
4923 arg += 2;
4924 pp = &eap->force_ff;
4925 }
4926 else if (STRNCMP(arg, "fileformat", 10) == 0)
4927 {
4928 arg += 10;
4929 pp = &eap->force_ff;
4930 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004931 else if (STRNCMP(arg, "enc", 3) == 0)
4932 {
Bram Moolenaarb38e9ab2011-12-14 14:49:45 +01004933 if (STRNCMP(arg, "encoding", 8) == 0)
4934 arg += 8;
4935 else
4936 arg += 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004937 pp = &eap->force_enc;
4938 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004939 else if (STRNCMP(arg, "bad", 3) == 0)
4940 {
4941 arg += 3;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02004942 pp = &bad_char_idx;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004943 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004944
4945 if (pp == NULL || *arg != '=')
4946 return FAIL;
4947
4948 ++arg;
4949 *pp = (int)(arg - eap->cmd);
4950 arg = skip_cmd_arg(arg, FALSE);
4951 eap->arg = skipwhite(arg);
4952 *arg = NUL;
4953
Bram Moolenaar071d4272004-06-13 20:20:40 +00004954 if (pp == &eap->force_ff)
4955 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004956 if (check_ff_value(eap->cmd + eap->force_ff) == FAIL)
4957 return FAIL;
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004958 eap->force_ff = eap->cmd[eap->force_ff];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004959 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004960 else if (pp == &eap->force_enc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004961 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004962 // Make 'fileencoding' lower case.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004963 for (p = eap->cmd + eap->force_enc; *p != NUL; ++p)
4964 *p = TOLOWER_ASC(*p);
4965 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004966 else
4967 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004968 // Check ++bad= argument. Must be a single-byte character, "keep" or
4969 // "drop".
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004970 if (get_bad_opt(eap->cmd + bad_char_idx, eap) == FAIL)
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004971 return FAIL;
4972 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004973
4974 return OK;
4975}
4976
Bram Moolenaar071d4272004-06-13 20:20:40 +00004977 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004978ex_autocmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004979{
4980 /*
Bram Moolenaar26d4b892018-07-04 22:26:28 +02004981 * Disallow autocommands from .exrc and .vimrc in current
Bram Moolenaar071d4272004-06-13 20:20:40 +00004982 * directory for security reasons.
4983 */
4984 if (secure)
4985 {
4986 secure = 2;
Bram Moolenaar8930caa2020-07-23 16:37:03 +02004987 eap->errmsg = _(e_curdir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004988 }
4989 else if (eap->cmdidx == CMD_autocmd)
4990 do_autocmd(eap->arg, eap->forceit);
4991 else
4992 do_augroup(eap->arg, eap->forceit);
4993}
4994
4995/*
4996 * ":doautocmd": Apply the automatic commands to the current buffer.
4997 */
4998 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004999ex_doautocmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005000{
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005001 char_u *arg = eap->arg;
5002 int call_do_modelines = check_nomodeline(&arg);
Bram Moolenaar1610d052016-06-09 22:53:01 +02005003 int did_aucmd;
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005004
Bram Moolenaar1610d052016-06-09 22:53:01 +02005005 (void)do_doautocmd(arg, TRUE, &did_aucmd);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005006 // Only when there is no <nomodeline>.
Bram Moolenaar1610d052016-06-09 22:53:01 +02005007 if (call_do_modelines && did_aucmd)
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005008 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005009}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005010
Bram Moolenaar071d4272004-06-13 20:20:40 +00005011/*
5012 * :[N]bunload[!] [N] [bufname] unload buffer
5013 * :[N]bdelete[!] [N] [bufname] delete buffer from buffer list
5014 * :[N]bwipeout[!] [N] [bufname] delete buffer really
5015 */
5016 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005017ex_bunload(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005018{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01005019 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02005020 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005021 eap->errmsg = do_bufdel(
5022 eap->cmdidx == CMD_bdelete ? DOBUF_DEL
5023 : eap->cmdidx == CMD_bwipeout ? DOBUF_WIPE
5024 : DOBUF_UNLOAD, eap->arg,
5025 eap->addr_count, (int)eap->line1, (int)eap->line2, eap->forceit);
5026}
5027
5028/*
5029 * :[N]buffer [N] to buffer N
5030 * :[N]sbuffer [N] to buffer N
5031 */
5032 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005033ex_buffer(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005034{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01005035 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02005036 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005037 if (*eap->arg)
Bram Moolenaar8930caa2020-07-23 16:37:03 +02005038 eap->errmsg = ex_errmsg(e_trailing_arg, eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005039 else
5040 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005041 if (eap->addr_count == 0) // default is current buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00005042 goto_buffer(eap, DOBUF_CURRENT, FORWARD, 0);
5043 else
5044 goto_buffer(eap, DOBUF_FIRST, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005045 if (eap->do_ecmd_cmd != NULL)
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01005046 do_cmd_argument(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005047 }
5048}
5049
5050/*
5051 * :[N]bmodified [N] to next mod. buffer
5052 * :[N]sbmodified [N] to next mod. buffer
5053 */
5054 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005055ex_bmodified(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005056{
5057 goto_buffer(eap, DOBUF_MOD, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005058 if (eap->do_ecmd_cmd != NULL)
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01005059 do_cmd_argument(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005060}
5061
5062/*
5063 * :[N]bnext [N] to next buffer
5064 * :[N]sbnext [N] split and to next buffer
5065 */
5066 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005067ex_bnext(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005068{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01005069 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar3029bcc2020-01-18 15:06:19 +01005070 return;
5071
Bram Moolenaar071d4272004-06-13 20:20:40 +00005072 goto_buffer(eap, DOBUF_CURRENT, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005073 if (eap->do_ecmd_cmd != NULL)
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01005074 do_cmd_argument(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005075}
5076
5077/*
5078 * :[N]bNext [N] to previous buffer
5079 * :[N]bprevious [N] to previous buffer
5080 * :[N]sbNext [N] split and to previous buffer
5081 * :[N]sbprevious [N] split and to previous buffer
5082 */
5083 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005084ex_bprevious(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005085{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01005086 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar3029bcc2020-01-18 15:06:19 +01005087 return;
5088
Bram Moolenaar071d4272004-06-13 20:20:40 +00005089 goto_buffer(eap, DOBUF_CURRENT, BACKWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005090 if (eap->do_ecmd_cmd != NULL)
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01005091 do_cmd_argument(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005092}
5093
5094/*
5095 * :brewind to first buffer
5096 * :bfirst to first buffer
5097 * :sbrewind split and to first buffer
5098 * :sbfirst split and to first buffer
5099 */
5100 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005101ex_brewind(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005102{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01005103 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar3029bcc2020-01-18 15:06:19 +01005104 return;
5105
Bram Moolenaar071d4272004-06-13 20:20:40 +00005106 goto_buffer(eap, DOBUF_FIRST, FORWARD, 0);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005107 if (eap->do_ecmd_cmd != NULL)
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01005108 do_cmd_argument(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005109}
5110
5111/*
5112 * :blast to last buffer
5113 * :sblast split and to last buffer
5114 */
5115 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005116ex_blast(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005117{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01005118 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar3029bcc2020-01-18 15:06:19 +01005119 return;
5120
Bram Moolenaar071d4272004-06-13 20:20:40 +00005121 goto_buffer(eap, DOBUF_LAST, BACKWARD, 0);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005122 if (eap->do_ecmd_cmd != NULL)
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01005123 do_cmd_argument(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005124}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005125
Bram Moolenaar7a092242020-04-16 22:10:49 +02005126/*
5127 * Check if "c" ends an Ex command.
Bram Moolenaarb5b94802020-12-13 17:50:20 +01005128 * In Vim9 script does not check for white space before #.
Bram Moolenaar7a092242020-04-16 22:10:49 +02005129 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005130 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005131ends_excmd(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005132{
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005133 int comment_char = '"';
5134
Bram Moolenaarfbda69b2020-04-13 15:06:53 +02005135#ifdef FEAT_EVAL
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005136 if (in_vim9script())
5137 comment_char = '#';
Bram Moolenaar7a092242020-04-16 22:10:49 +02005138#endif
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005139 return (c == NUL || c == '|' || c == comment_char || c == '\n');
Bram Moolenaar7a092242020-04-16 22:10:49 +02005140}
5141
5142/*
5143 * Like ends_excmd() but checks that a # in Vim9 script either has "cmd" equal
5144 * to "cmd_start" or has a white space character before it.
5145 */
5146 int
Bram Moolenaar21cfe502020-04-16 23:01:50 +02005147ends_excmd2(char_u *cmd_start UNUSED, char_u *cmd)
Bram Moolenaar7a092242020-04-16 22:10:49 +02005148{
5149 int c = *cmd;
5150
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005151 if (c == NUL || c == '|' || c == '\n')
5152 return TRUE;
Bram Moolenaar7a092242020-04-16 22:10:49 +02005153#ifdef FEAT_EVAL
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005154 if (in_vim9script())
5155 return c == '#' && cmd[1] != '{'
5156 && (cmd == cmd_start || VIM_ISWHITE(cmd[-1]));
Bram Moolenaarfbda69b2020-04-13 15:06:53 +02005157#endif
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005158 return c == '"';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005159}
5160
5161#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA) || defined(FEAT_EVAL) \
5162 || defined(PROTO)
5163/*
5164 * Return the next command, after the first '|' or '\n'.
5165 * Return NULL if not found.
5166 */
5167 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005168find_nextcmd(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005169{
5170 while (*p != '|' && *p != '\n')
5171 {
5172 if (*p == NUL)
5173 return NULL;
5174 ++p;
5175 }
5176 return (p + 1);
5177}
5178#endif
5179
5180/*
Bram Moolenaar2256c992016-11-15 21:17:07 +01005181 * Check if *p is a separator between Ex commands, skipping over white space.
5182 * Return NULL if it isn't, the following character if it is.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005183 */
5184 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005185check_nextcmd(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005186{
Bram Moolenaar2256c992016-11-15 21:17:07 +01005187 char_u *s = skipwhite(p);
5188
5189 if (*s == '|' || *s == '\n')
5190 return (s + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005191 else
5192 return NULL;
5193}
5194
5195/*
5196 * - if there are more files to edit
5197 * - and this is the last window
5198 * - and forceit not used
5199 * - and not repeated twice on a row
5200 * return FAIL and give error message if 'message' TRUE
5201 * return OK otherwise
5202 */
5203 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005204check_more(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005205 int message, // when FALSE check only, no messages
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005206 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005207{
5208 int n = ARGCOUNT - curwin->w_arg_idx - 1;
5209
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005210 if (!forceit && only_one_window()
Bram Moolenaar7b221172020-08-17 19:34:10 +02005211 && ARGCOUNT > 1 && !arg_had_last && n > 0 && quitmore == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005212 {
5213 if (message)
5214 {
5215#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaare1004402020-10-24 20:49:43 +02005216 if ((p_confirm || (cmdmod.cmod_flags & CMOD_CONFIRM))
5217 && curbuf->b_fname != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005218 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02005219 char_u buff[DIALOG_MSG_SIZE];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005220
Bram Moolenaarda6e8912018-08-21 15:12:14 +02005221 vim_snprintf((char *)buff, DIALOG_MSG_SIZE,
5222 NGETTEXT("%d more file to edit. Quit anyway?",
5223 "%d more files to edit. Quit anyway?", n), n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005224 if (vim_dialog_yesno(VIM_QUESTION, NULL, buff, 1) == VIM_YES)
5225 return OK;
5226 return FAIL;
5227 }
5228#endif
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01005229 semsg(NGETTEXT("E173: %d more file to edit",
5230 "E173: %d more files to edit", n), n);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005231 quitmore = 2; // next try to quit is allowed
Bram Moolenaar071d4272004-06-13 20:20:40 +00005232 }
5233 return FAIL;
5234 }
5235 return OK;
5236}
5237
Bram Moolenaar071d4272004-06-13 20:20:40 +00005238/*
5239 * Function given to ExpandGeneric() to obtain the list of command names.
5240 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005241 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005242get_command_name(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005243{
5244 if (idx >= (int)CMD_SIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005245 return get_user_command_name(idx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005246 return cmdnames[idx].cmd_name;
5247}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005248
Bram Moolenaar071d4272004-06-13 20:20:40 +00005249 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005250ex_colorscheme(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005251{
Bram Moolenaare6850792010-05-14 15:28:44 +02005252 if (*eap->arg == NUL)
5253 {
5254#ifdef FEAT_EVAL
5255 char_u *expr = vim_strsave((char_u *)"g:colors_name");
5256 char_u *p = NULL;
5257
5258 if (expr != NULL)
5259 {
5260 ++emsg_off;
Bram Moolenaarb171fb12020-06-24 20:34:03 +02005261 p = eval_to_string(expr, FALSE);
Bram Moolenaare6850792010-05-14 15:28:44 +02005262 --emsg_off;
5263 vim_free(expr);
5264 }
5265 if (p != NULL)
5266 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01005267 msg((char *)p);
Bram Moolenaare6850792010-05-14 15:28:44 +02005268 vim_free(p);
5269 }
5270 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01005271 msg("default");
Bram Moolenaare6850792010-05-14 15:28:44 +02005272#else
Bram Moolenaar32526b32019-01-19 17:43:09 +01005273 msg(_("unknown"));
Bram Moolenaare6850792010-05-14 15:28:44 +02005274#endif
5275 }
5276 else if (load_colors(eap->arg) == FAIL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005277 semsg(_("E185: Cannot find color scheme '%s'"), eap->arg);
Bram Moolenaarf58d81a2019-01-28 20:19:05 +01005278
5279#ifdef FEAT_VTP
5280 else if (has_vtp_working())
5281 {
5282 // background color change requires clear + redraw
5283 update_screen(CLEAR);
5284 redrawcmd();
5285 }
5286#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005287}
5288
5289 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005290ex_highlight(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005291{
5292 if (*eap->arg == NUL && eap->cmd[2] == '!')
Bram Moolenaar32526b32019-01-19 17:43:09 +01005293 msg(_("Greetings, Vim user!"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005294 do_highlight(eap->arg, eap->forceit, FALSE);
5295}
5296
5297
5298/*
5299 * Call this function if we thought we were going to exit, but we won't
5300 * (because of an error). May need to restore the terminal mode.
5301 */
5302 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005303not_exiting(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005304{
5305 exiting = FALSE;
5306 settmode(TMODE_RAW);
5307}
5308
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005309 static int
5310before_quit_autocmds(win_T *wp, int quit_all, int forceit)
5311{
5312 apply_autocmds(EVENT_QUITPRE, NULL, NULL, FALSE, wp->w_buffer);
5313
Bram Moolenaar34ba06b2019-10-20 22:27:10 +02005314 // Bail out when autocommands closed the window.
5315 // Refuse to quit when the buffer in the last window is being closed (can
5316 // only happen in autocommands).
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005317 if (!win_valid(wp)
5318 || curbuf_locked()
5319 || (wp->w_buffer->b_nwindows == 1 && wp->w_buffer->b_locked > 0))
5320 return TRUE;
5321
5322 if (quit_all || (check_more(FALSE, forceit) == OK && only_one_window()))
5323 {
5324 apply_autocmds(EVENT_EXITPRE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar34ba06b2019-10-20 22:27:10 +02005325 // Refuse to quit when locked or when the window was closed or the
5326 // buffer in the last window is being closed (can only happen in
5327 // autocommands).
5328 if (!win_valid(wp) || curbuf_locked()
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005329 || (curbuf->b_nwindows == 1 && curbuf->b_locked > 0))
5330 return TRUE;
5331 }
5332
5333 return FALSE;
5334}
5335
Bram Moolenaar071d4272004-06-13 20:20:40 +00005336/*
Bram Moolenaarf240e182014-11-27 18:33:02 +01005337 * ":quit": quit current window, quit Vim if the last window is closed.
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005338 * ":{nr}quit": quit window {nr}
Bram Moolenaar4d14bac2019-10-20 21:15:15 +02005339 * Also used when closing a terminal window that's the last one.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005340 */
Bram Moolenaar4d14bac2019-10-20 21:15:15 +02005341 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005342ex_quit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005343{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005344 win_T *wp;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005345
Bram Moolenaar071d4272004-06-13 20:20:40 +00005346#ifdef FEAT_CMDWIN
5347 if (cmdwin_type != 0)
5348 {
5349 cmdwin_result = Ctrl_C;
5350 return;
5351 }
5352#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005353 // Don't quit while editing the command line.
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005354 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005355 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005356 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005357 return;
5358 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005359 if (eap->addr_count > 0)
5360 {
Bram Moolenaarf240e182014-11-27 18:33:02 +01005361 int wnr = eap->line2;
5362
5363 for (wp = firstwin; wp->w_next != NULL; wp = wp->w_next)
5364 if (--wnr <= 0)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005365 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005366 }
5367 else
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005368 wp = curwin;
Bram Moolenaarf240e182014-11-27 18:33:02 +01005369
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005370 // Refuse to quit when locked.
Bram Moolenaar87ffb5c2017-10-19 12:37:42 +02005371 if (curbuf_locked())
5372 return;
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005373
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005374 // Trigger QuitPre and maybe ExitPre
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005375 if (before_quit_autocmds(wp, FALSE, eap->forceit))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005376 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005377
5378#ifdef FEAT_NETBEANS_INTG
5379 netbeansForcedQuit = eap->forceit;
5380#endif
5381
5382 /*
5383 * If there are more files or windows we won't exit.
5384 */
5385 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
5386 exiting = TRUE;
Bram Moolenaarff930ca2017-10-19 17:12:10 +02005387 if ((!buf_hide(wp->w_buffer)
5388 && check_changed(wp->w_buffer, (p_awa ? CCGD_AW : 0)
Bram Moolenaar45d3b142013-11-09 03:31:51 +01005389 | (eap->forceit ? CCGD_FORCEIT : 0)
5390 | CCGD_EXCMD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005391 || check_more(TRUE, eap->forceit) == FAIL
Bram Moolenaar027387f2016-01-02 22:25:52 +01005392 || (only_one_window() && check_changed_any(eap->forceit, TRUE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005393 {
5394 not_exiting();
5395 }
5396 else
5397 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005398 // quit last window
5399 // Note: only_one_window() returns true, even so a help window is
5400 // still open. In that case only quit, if no address has been
5401 // specified. Example:
5402 // :h|wincmd w|1q - don't quit
5403 // :h|wincmd w|q - quit
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01005404 if (only_one_window() && (ONE_WINDOW || eap->addr_count == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005405 getout(0);
Bram Moolenaar2c33d7b2017-10-14 16:06:20 +02005406 not_exiting();
Bram Moolenaar4033c552017-09-16 20:54:51 +02005407#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00005408 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005409#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005410 // close window; may free buffer
Bram Moolenaareb44a682017-08-03 22:44:55 +02005411 win_close(wp, !buf_hide(wp->w_buffer) || eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005412 }
5413}
5414
5415/*
5416 * ":cquit".
5417 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005418 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005419ex_cquit(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005420{
Bram Moolenaar1860bde2020-01-06 21:47:21 +01005421 // this does not always pass on the exit code to the Manx compiler. why?
5422 getout(eap->addr_count > 0 ? (int)eap->line2 : EXIT_FAILURE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005423}
5424
5425/*
5426 * ":qall": try to quit all windows
5427 */
5428 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005429ex_quit_all(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005430{
5431# ifdef FEAT_CMDWIN
5432 if (cmdwin_type != 0)
5433 {
5434 if (eap->forceit)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005435 cmdwin_result = K_XF1; // ex_window() takes care of this
Bram Moolenaar071d4272004-06-13 20:20:40 +00005436 else
5437 cmdwin_result = K_XF2;
5438 return;
5439 }
5440# endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005441
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005442 // Don't quit while editing the command line.
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005443 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005444 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005445 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005446 return;
5447 }
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005448
5449 if (before_quit_autocmds(curwin, TRUE, eap->forceit))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005450 return;
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005451
Bram Moolenaar071d4272004-06-13 20:20:40 +00005452 exiting = TRUE;
Bram Moolenaar027387f2016-01-02 22:25:52 +01005453 if (eap->forceit || !check_changed_any(FALSE, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005454 getout(0);
5455 not_exiting();
5456}
5457
Bram Moolenaar071d4272004-06-13 20:20:40 +00005458/*
5459 * ":close": close current window, unless it is the last one
5460 */
5461 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005462ex_close(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005463{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005464 win_T *win;
5465 int winnr = 0;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005466#ifdef FEAT_CMDWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00005467 if (cmdwin_type != 0)
Bram Moolenaar9bd1a7e2011-05-19 14:50:54 +02005468 cmdwin_result = Ctrl_C;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005469 else
Bram Moolenaar4033c552017-09-16 20:54:51 +02005470#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005471 if (!text_locked() && !curbuf_locked())
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005472 {
5473 if (eap->addr_count == 0)
5474 ex_win_close(eap->forceit, curwin, NULL);
Bram Moolenaar6d41c782018-06-06 09:11:12 +02005475 else
5476 {
Bram Moolenaar29323592016-07-24 22:04:11 +02005477 FOR_ALL_WINDOWS(win)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005478 {
5479 winnr++;
5480 if (winnr == eap->line2)
5481 break;
5482 }
5483 if (win == NULL)
5484 win = lastwin;
5485 ex_win_close(eap->forceit, win, NULL);
5486 }
5487 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005488}
5489
Bram Moolenaar4033c552017-09-16 20:54:51 +02005490#ifdef FEAT_QUICKFIX
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005491/*
5492 * ":pclose": Close any preview window.
5493 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005494 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005495ex_pclose(exarg_T *eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005496{
5497 win_T *win;
5498
Bram Moolenaar79648732019-07-18 21:43:07 +02005499 // First close any normal window.
Bram Moolenaar29323592016-07-24 22:04:11 +02005500 FOR_ALL_WINDOWS(win)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005501 if (win->w_p_pvw)
5502 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00005503 ex_win_close(eap->forceit, win, NULL);
Bram Moolenaar79648732019-07-18 21:43:07 +02005504 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005505 }
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01005506# ifdef FEAT_PROP_POPUP
Bram Moolenaar79648732019-07-18 21:43:07 +02005507 // Also when 'previewpopup' is empty, it might have been cleared.
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02005508 popup_close_preview();
Bram Moolenaar79648732019-07-18 21:43:07 +02005509# endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005510}
Bram Moolenaar4033c552017-09-16 20:54:51 +02005511#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005512
Bram Moolenaarf740b292006-02-16 22:11:02 +00005513/*
5514 * Close window "win" and take care of handling closing the last window for a
5515 * modified buffer.
5516 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005517 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005518ex_win_close(
5519 int forceit,
5520 win_T *win,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005521 tabpage_T *tp) // NULL or the tab page "win" is in
Bram Moolenaar071d4272004-06-13 20:20:40 +00005522{
5523 int need_hide;
5524 buf_T *buf = win->w_buffer;
5525
Bram Moolenaarcf844172020-06-26 19:44:06 +02005526 // Never close the autocommand window.
5527 if (win == aucmd_win)
5528 {
5529 emsg(_(e_autocmd_close));
5530 return;
5531 }
5532
Bram Moolenaar071d4272004-06-13 20:20:40 +00005533 need_hide = (bufIsChanged(buf) && buf->b_nwindows <= 1);
Bram Moolenaareb44a682017-08-03 22:44:55 +02005534 if (need_hide && !buf_hide(buf) && !forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005535 {
Bram Moolenaar4033c552017-09-16 20:54:51 +02005536#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaare1004402020-10-24 20:49:43 +02005537 if ((p_confirm || (cmdmod.cmod_flags & CMOD_CONFIRM)) && p_write)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005538 {
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005539 bufref_T bufref;
5540
5541 set_bufref(&bufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005542 dialog_changed(buf, FALSE);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005543 if (bufref_valid(&bufref) && bufIsChanged(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005544 return;
5545 need_hide = FALSE;
5546 }
5547 else
Bram Moolenaar4033c552017-09-16 20:54:51 +02005548#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005549 {
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02005550 no_write_message();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005551 return;
5552 }
5553 }
5554
Bram Moolenaar4033c552017-09-16 20:54:51 +02005555#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00005556 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005557#endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00005558
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005559 // free buffer when not hiding it or when it's a scratch buffer
Bram Moolenaarf740b292006-02-16 22:11:02 +00005560 if (tp == NULL)
Bram Moolenaareb44a682017-08-03 22:44:55 +02005561 win_close(win, !need_hide && !buf_hide(buf));
Bram Moolenaarf740b292006-02-16 22:11:02 +00005562 else
Bram Moolenaareb44a682017-08-03 22:44:55 +02005563 win_close_othertab(win, !need_hide && !buf_hide(buf), tp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005564}
5565
Bram Moolenaar071d4272004-06-13 20:20:40 +00005566/*
Bram Moolenaardea25702017-01-29 15:18:10 +01005567 * Handle the argument for a tabpage related ex command.
5568 * Returns a tabpage number.
5569 * When an error is encountered then eap->errmsg is set.
5570 */
5571 static int
5572get_tabpage_arg(exarg_T *eap)
5573{
5574 int tab_number;
5575 int unaccept_arg0 = (eap->cmdidx == CMD_tabmove) ? 0 : 1;
5576
5577 if (eap->arg && *eap->arg != NUL)
5578 {
5579 char_u *p = eap->arg;
5580 char_u *p_save;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005581 int relative = 0; // argument +N/-N means: go to N places to the
5582 // right/left relative to the current position.
Bram Moolenaardea25702017-01-29 15:18:10 +01005583
5584 if (*p == '-')
5585 {
5586 relative = -1;
5587 p++;
5588 }
5589 else if (*p == '+')
5590 {
5591 relative = 1;
5592 p++;
5593 }
5594
5595 p_save = p;
5596 tab_number = getdigits(&p);
5597
5598 if (relative == 0)
5599 {
5600 if (STRCMP(p, "$") == 0)
5601 tab_number = LAST_TAB_NR;
Bram Moolenaar94f4ffa2020-08-10 19:21:15 +02005602 else if (STRCMP(p, "#") == 0)
5603 if (valid_tabpage(lastused_tabpage))
5604 tab_number = tabpage_index(lastused_tabpage);
5605 else
5606 {
5607 eap->errmsg = ex_errmsg(e_invargval, eap->arg);
5608 tab_number = 0;
5609 goto theend;
5610 }
Bram Moolenaardea25702017-01-29 15:18:10 +01005611 else if (p == p_save || *p_save == '-' || *p != NUL
5612 || tab_number > LAST_TAB_NR)
5613 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005614 // No numbers as argument.
Bram Moolenaar8930caa2020-07-23 16:37:03 +02005615 eap->errmsg = ex_errmsg(e_invarg2, eap->arg);
Bram Moolenaardea25702017-01-29 15:18:10 +01005616 goto theend;
5617 }
5618 }
5619 else
5620 {
5621 if (*p_save == NUL)
5622 tab_number = 1;
5623 else if (p == p_save || *p_save == '-' || *p != NUL
5624 || tab_number == 0)
5625 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005626 // No numbers as argument.
Bram Moolenaar8930caa2020-07-23 16:37:03 +02005627 eap->errmsg = ex_errmsg(e_invarg2, eap->arg);
Bram Moolenaardea25702017-01-29 15:18:10 +01005628 goto theend;
5629 }
5630 tab_number = tab_number * relative + tabpage_index(curtab);
5631 if (!unaccept_arg0 && relative == -1)
5632 --tab_number;
5633 }
5634 if (tab_number < unaccept_arg0 || tab_number > LAST_TAB_NR)
Bram Moolenaar8930caa2020-07-23 16:37:03 +02005635 eap->errmsg = ex_errmsg(e_invarg2, eap->arg);
Bram Moolenaardea25702017-01-29 15:18:10 +01005636 }
5637 else if (eap->addr_count > 0)
5638 {
5639 if (unaccept_arg0 && eap->line2 == 0)
Bram Moolenaarc6251552017-01-29 21:42:20 +01005640 {
Bram Moolenaar8930caa2020-07-23 16:37:03 +02005641 eap->errmsg = _(e_invrange);
Bram Moolenaarc6251552017-01-29 21:42:20 +01005642 tab_number = 0;
5643 }
Bram Moolenaardea25702017-01-29 15:18:10 +01005644 else
5645 {
5646 tab_number = eap->line2;
Bram Moolenaar82a12462019-01-22 22:41:42 +01005647 if (!unaccept_arg0 && *skipwhite(*eap->cmdlinep) == '-')
Bram Moolenaardea25702017-01-29 15:18:10 +01005648 {
5649 --tab_number;
5650 if (tab_number < unaccept_arg0)
Bram Moolenaar8930caa2020-07-23 16:37:03 +02005651 eap->errmsg = _(e_invrange);
Bram Moolenaardea25702017-01-29 15:18:10 +01005652 }
5653 }
5654 }
5655 else
5656 {
5657 switch (eap->cmdidx)
5658 {
5659 case CMD_tabnext:
5660 tab_number = tabpage_index(curtab) + 1;
5661 if (tab_number > LAST_TAB_NR)
5662 tab_number = 1;
5663 break;
5664 case CMD_tabmove:
5665 tab_number = LAST_TAB_NR;
5666 break;
5667 default:
5668 tab_number = tabpage_index(curtab);
5669 }
5670 }
5671
5672theend:
5673 return tab_number;
5674}
5675
5676/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00005677 * ":tabclose": close current tab page, unless it is the last one.
5678 * ":tabclose N": close tab page N.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005679 */
5680 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005681ex_tabclose(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005682{
Bram Moolenaarf740b292006-02-16 22:11:02 +00005683 tabpage_T *tp;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005684 int tab_number;
Bram Moolenaarf740b292006-02-16 22:11:02 +00005685
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005686# ifdef FEAT_CMDWIN
5687 if (cmdwin_type != 0)
5688 cmdwin_result = K_IGNORE;
5689 else
5690# endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00005691 if (first_tabpage->tp_next == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005692 emsg(_("E784: Cannot close last tab page"));
Bram Moolenaarf740b292006-02-16 22:11:02 +00005693 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005694 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005695 tab_number = get_tabpage_arg(eap);
5696 if (eap->errmsg == NULL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005697 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005698 tp = find_tabpage(tab_number);
Bram Moolenaarf740b292006-02-16 22:11:02 +00005699 if (tp == NULL)
5700 {
5701 beep_flush();
5702 return;
5703 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005704 if (tp != curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00005705 {
5706 tabpage_close_other(tp, eap->forceit);
5707 return;
5708 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005709 else if (!text_locked() && !curbuf_locked())
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005710 tabpage_close(eap->forceit);
5711 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005712 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005713}
5714
5715/*
5716 * ":tabonly": close all tab pages except the current one
5717 */
5718 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005719ex_tabonly(exarg_T *eap)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005720{
5721 tabpage_T *tp;
5722 int done;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005723 int tab_number;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005724
5725# ifdef FEAT_CMDWIN
5726 if (cmdwin_type != 0)
5727 cmdwin_result = K_IGNORE;
5728 else
5729# endif
5730 if (first_tabpage->tp_next == NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01005731 msg(_("Already only one tab page"));
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005732 else
5733 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005734 tab_number = get_tabpage_arg(eap);
5735 if (eap->errmsg == NULL)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005736 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005737 goto_tabpage(tab_number);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005738 // Repeat this up to a 1000 times, because autocommands may
5739 // mess up the lists.
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005740 for (done = 0; done < 1000; ++done)
5741 {
5742 FOR_ALL_TABPAGES(tp)
5743 if (tp->tp_topframe != topframe)
5744 {
5745 tabpage_close_other(tp, eap->forceit);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005746 // if we failed to close it quit
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005747 if (valid_tabpage(tp))
5748 done = 1000;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005749 // start over, "tp" is now invalid
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005750 break;
5751 }
5752 if (first_tabpage->tp_next == NULL)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005753 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005754 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005755 }
5756 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005757}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005758
5759/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00005760 * Close the current tab page.
5761 */
5762 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005763tabpage_close(int forceit)
Bram Moolenaarf740b292006-02-16 22:11:02 +00005764{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005765 // First close all the windows but the current one. If that worked then
5766 // close the last window in this tab, that will close it.
Bram Moolenaar459ca562016-11-10 18:16:33 +01005767 if (!ONE_WINDOW)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005768 close_others(TRUE, forceit);
Bram Moolenaar459ca562016-11-10 18:16:33 +01005769 if (ONE_WINDOW)
Bram Moolenaarf740b292006-02-16 22:11:02 +00005770 ex_win_close(forceit, curwin, NULL);
5771# ifdef FEAT_GUI
5772 need_mouse_correct = TRUE;
5773# endif
5774}
5775
5776/*
5777 * Close tab page "tp", which is not the current tab page.
5778 * Note that autocommands may make "tp" invalid.
Bram Moolenaar7875acc2006-09-10 13:51:17 +00005779 * Also takes care of the tab pages line disappearing when closing the
5780 * last-but-one tab page.
Bram Moolenaarf740b292006-02-16 22:11:02 +00005781 */
5782 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005783tabpage_close_other(tabpage_T *tp, int forceit)
Bram Moolenaarf740b292006-02-16 22:11:02 +00005784{
5785 int done = 0;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005786 win_T *wp;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00005787 int h = tabline_height();
Bram Moolenaarf740b292006-02-16 22:11:02 +00005788
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005789 // Limit to 1000 windows, autocommands may add a window while we close
5790 // one. OK, so I'm paranoid...
Bram Moolenaarf740b292006-02-16 22:11:02 +00005791 while (++done < 1000)
5792 {
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005793 wp = tp->tp_firstwin;
5794 ex_win_close(forceit, wp, tp);
Bram Moolenaarf740b292006-02-16 22:11:02 +00005795
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005796 // Autocommands may delete the tab page under our fingers and we may
5797 // fail to close a window with a modified buffer.
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005798 if (!valid_tabpage(tp) || tp->tp_firstwin == wp)
Bram Moolenaarf740b292006-02-16 22:11:02 +00005799 break;
5800 }
Bram Moolenaar7875acc2006-09-10 13:51:17 +00005801
Bram Moolenaar29323592016-07-24 22:04:11 +02005802 apply_autocmds(EVENT_TABCLOSED, NULL, NULL, FALSE, curbuf);
Bram Moolenaar29323592016-07-24 22:04:11 +02005803
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005804 redraw_tabline = TRUE;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00005805 if (h != tabline_height())
5806 shell_new_rows();
Bram Moolenaarf740b292006-02-16 22:11:02 +00005807}
5808
5809/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005810 * ":only".
5811 */
5812 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005813ex_only(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005814{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005815 win_T *wp;
5816 int wnr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005817# ifdef FEAT_GUI
5818 need_mouse_correct = TRUE;
5819# endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005820 if (eap->addr_count > 0)
5821 {
5822 wnr = eap->line2;
5823 for (wp = firstwin; --wnr > 0; )
5824 {
5825 if (wp->w_next == NULL)
5826 break;
5827 else
5828 wp = wp->w_next;
5829 }
5830 win_goto(wp);
5831 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005832 close_others(TRUE, eap->forceit);
5833}
5834
Bram Moolenaar071d4272004-06-13 20:20:40 +00005835 static void
Bram Moolenaar5e1e6d22017-01-02 17:26:00 +01005836ex_hide(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005837{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005838 // ":hide" or ":hide | cmd": hide current window
Bram Moolenaar2256c992016-11-15 21:17:07 +01005839 if (!eap->skip)
5840 {
Bram Moolenaar4033c552017-09-16 20:54:51 +02005841#ifdef FEAT_GUI
Bram Moolenaar2256c992016-11-15 21:17:07 +01005842 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005843#endif
Bram Moolenaar2256c992016-11-15 21:17:07 +01005844 if (eap->addr_count == 0)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005845 win_close(curwin, FALSE); // don't free buffer
Bram Moolenaar2256c992016-11-15 21:17:07 +01005846 else
5847 {
5848 int winnr = 0;
5849 win_T *win;
Bram Moolenaarf240e182014-11-27 18:33:02 +01005850
Bram Moolenaar2256c992016-11-15 21:17:07 +01005851 FOR_ALL_WINDOWS(win)
5852 {
5853 winnr++;
5854 if (winnr == eap->line2)
5855 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005856 }
Bram Moolenaar2256c992016-11-15 21:17:07 +01005857 if (win == NULL)
5858 win = lastwin;
5859 win_close(win, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005860 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005861 }
5862}
5863
5864/*
5865 * ":stop" and ":suspend": Suspend Vim.
5866 */
5867 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005868ex_stop(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005869{
5870 /*
5871 * Disallow suspending for "rvim".
5872 */
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005873 if (!check_restricted())
Bram Moolenaar071d4272004-06-13 20:20:40 +00005874 {
5875 if (!eap->forceit)
5876 autowrite_all();
Bram Moolenaar100118c2020-12-11 19:30:34 +01005877 apply_autocmds(EVENT_VIMSUSPEND, NULL, NULL, FALSE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005878 windgoto((int)Rows - 1, 0);
5879 out_char('\n');
5880 out_flush();
5881 stoptermcap();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005882 out_flush(); // needed for SUN to restore xterm buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00005883#ifdef FEAT_TITLE
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005884 mch_restore_title(SAVE_RESTORE_BOTH); // restore window titles
Bram Moolenaar071d4272004-06-13 20:20:40 +00005885#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005886 ui_suspend(); // call machine specific function
Bram Moolenaar071d4272004-06-13 20:20:40 +00005887#ifdef FEAT_TITLE
5888 maketitle();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005889 resettitle(); // force updating the title
Bram Moolenaar071d4272004-06-13 20:20:40 +00005890#endif
5891 starttermcap();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005892 scroll_start(); // scroll screen before redrawing
Bram Moolenaar071d4272004-06-13 20:20:40 +00005893 redraw_later_clear();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005894 shell_resized(); // may have resized window
Bram Moolenaar100118c2020-12-11 19:30:34 +01005895 apply_autocmds(EVENT_VIMRESUME, NULL, NULL, FALSE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005896 }
5897}
5898
5899/*
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005900 * ":exit", ":xit" and ":wq": Write file and quite the current window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005901 */
5902 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005903ex_exit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005904{
Bram Moolenaar461f2122020-07-28 20:25:47 +02005905#ifdef FEAT_EVAL
Bram Moolenaarae616492020-07-28 20:07:27 +02005906 if (not_in_vim9(eap) == FAIL)
5907 return;
Bram Moolenaar461f2122020-07-28 20:25:47 +02005908#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005909#ifdef FEAT_CMDWIN
5910 if (cmdwin_type != 0)
5911 {
5912 cmdwin_result = Ctrl_C;
5913 return;
5914 }
5915#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005916 // Don't quit while editing the command line.
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005917 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005918 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005919 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005920 return;
5921 }
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005922
5923 if (before_quit_autocmds(curwin, FALSE, eap->forceit))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005924 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005925
5926 /*
5927 * if more files or windows we won't exit
5928 */
5929 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
5930 exiting = TRUE;
5931 if ( ((eap->cmdidx == CMD_wq
5932 || curbufIsChanged())
5933 && do_write(eap) == FAIL)
5934 || check_more(TRUE, eap->forceit) == FAIL
Bram Moolenaar027387f2016-01-02 22:25:52 +01005935 || (only_one_window() && check_changed_any(eap->forceit, FALSE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005936 {
5937 not_exiting();
5938 }
5939 else
5940 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005941 if (only_one_window()) // quit last window, exit Vim
Bram Moolenaar071d4272004-06-13 20:20:40 +00005942 getout(0);
Bram Moolenaar2c33d7b2017-10-14 16:06:20 +02005943 not_exiting();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005944# ifdef FEAT_GUI
5945 need_mouse_correct = TRUE;
5946# endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005947 // Quit current window, may free the buffer.
Bram Moolenaareb44a682017-08-03 22:44:55 +02005948 win_close(curwin, !buf_hide(curwin->w_buffer));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005949 }
5950}
5951
5952/*
5953 * ":print", ":list", ":number".
5954 */
5955 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005956ex_print(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005957{
Bram Moolenaardf177f62005-02-22 08:39:57 +00005958 if (curbuf->b_ml.ml_flags & ML_EMPTY)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005959 emsg(_(e_emptybuf));
Bram Moolenaardf177f62005-02-22 08:39:57 +00005960 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005961 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00005962 for ( ;!got_int; ui_breakcheck())
5963 {
5964 print_line(eap->line1,
5965 (eap->cmdidx == CMD_number || eap->cmdidx == CMD_pound
5966 || (eap->flags & EXFLAG_NR)),
5967 eap->cmdidx == CMD_list || (eap->flags & EXFLAG_LIST));
5968 if (++eap->line1 > eap->line2)
5969 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005970 out_flush(); // show one line at a time
Bram Moolenaardf177f62005-02-22 08:39:57 +00005971 }
5972 setpcmark();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005973 // put cursor at last line
Bram Moolenaardf177f62005-02-22 08:39:57 +00005974 curwin->w_cursor.lnum = eap->line2;
5975 beginline(BL_SOL | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005976 }
5977
Bram Moolenaar071d4272004-06-13 20:20:40 +00005978 ex_no_reprint = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005979}
5980
5981#ifdef FEAT_BYTEOFF
5982 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005983ex_goto(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005984{
5985 goto_byte(eap->line2);
5986}
5987#endif
5988
5989/*
5990 * ":shell".
5991 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005992 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005993ex_shell(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005994{
5995 do_shell(NULL, 0);
5996}
5997
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005998#if defined(HAVE_DROP_FILE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005999
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006000static int drop_busy = FALSE;
6001static int drop_filec;
6002static char_u **drop_filev = NULL;
6003static int drop_split;
6004static void (*drop_callback)(void *);
6005static void *drop_cookie;
6006
6007 static void
6008handle_drop_internal(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006009{
6010 exarg_T ea;
6011 int save_msg_scroll = msg_scroll;
6012
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006013 // Setting the argument list may cause screen updates and being called
6014 // recursively. Avoid that by setting drop_busy.
6015 drop_busy = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006016
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006017 // Check whether the current buffer is changed. If so, we will need
6018 // to split the current window or data could be lost.
6019 // We don't need to check if the 'hidden' option is set, as in this
6020 // case the buffer won't be lost.
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006021 if (!buf_hide(curbuf) && !drop_split)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006022 {
6023 ++emsg_off;
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006024 drop_split = check_changed(curbuf, CCGD_AW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006025 --emsg_off;
6026 }
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006027 if (drop_split)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006028 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006029 if (win_split(0, 0) == FAIL)
6030 return;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02006031 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006032
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006033 // When splitting the window, create a new alist. Otherwise the
6034 // existing one is overwritten.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006035 alist_unlink(curwin->w_alist);
6036 alist_new();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006037 }
6038
6039 /*
6040 * Set up the new argument list.
6041 */
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006042 alist_set(ALIST(curwin), drop_filec, drop_filev, FALSE, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006043
6044 /*
6045 * Move to the first file.
6046 */
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006047 // Fake up a minimal "next" command for do_argfile()
Bram Moolenaara80faa82020-04-12 19:37:17 +02006048 CLEAR_FIELD(ea);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006049 ea.cmd = (char_u *)"next";
6050 do_argfile(&ea, 0);
6051
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006052 // do_ecmd() may set need_start_insertmode, but since we never left Insert
6053 // mode that is not needed here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006054 need_start_insertmode = FALSE;
6055
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006056 // Restore msg_scroll, otherwise a following command may cause scrolling
6057 // unexpectedly. The screen will be redrawn by the caller, thus
6058 // msg_scroll being set by displaying a message is irrelevant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006059 msg_scroll = save_msg_scroll;
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006060
6061 if (drop_callback != NULL)
6062 drop_callback(drop_cookie);
6063
6064 drop_filev = NULL;
6065 drop_busy = FALSE;
6066}
6067
6068/*
6069 * Handle a file drop. The code is here because a drop is *nearly* like an
6070 * :args command, but not quite (we have a list of exact filenames, so we
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01006071 * don't want to (a) parse a command line, or (b) expand wildcards). So the
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006072 * code is very similar to :args and hence needs access to a lot of the static
6073 * functions in this file.
6074 *
6075 * The "filev" list must have been allocated using alloc(), as should each item
6076 * in the list. This function takes over responsibility for freeing the "filev"
6077 * list.
6078 */
6079 void
6080handle_drop(
6081 int filec, // the number of files dropped
6082 char_u **filev, // the list of files dropped
6083 int split, // force splitting the window
6084 void (*callback)(void *), // to be called after setting the argument
6085 // list
6086 void *cookie) // argument for "callback" (allocated)
6087{
6088 // Cannot handle recursive drops, finish the pending one.
6089 if (drop_busy)
6090 {
6091 FreeWild(filec, filev);
6092 vim_free(cookie);
6093 return;
6094 }
6095
6096 // When calling handle_drop() more than once in a row we only use the last
6097 // one.
6098 if (drop_filev != NULL)
6099 {
6100 FreeWild(drop_filec, drop_filev);
6101 vim_free(drop_cookie);
6102 }
6103
6104 drop_filec = filec;
6105 drop_filev = filev;
6106 drop_split = split;
6107 drop_callback = callback;
6108 drop_cookie = cookie;
6109
6110 // Postpone this when:
6111 // - editing the command line
6112 // - not possible to change the current buffer
6113 // - updating the screen
6114 // As it may change buffers and window structures that are in use and cause
6115 // freed memory to be used.
6116 if (text_locked() || curbuf_locked() || updating_screen)
6117 return;
6118
6119 handle_drop_internal();
6120}
6121
6122/*
6123 * To be called when text is unlocked, curbuf is unlocked or updating_screen is
6124 * reset: Handle a postponed drop.
6125 */
6126 void
6127handle_any_postponed_drop(void)
6128{
6129 if (!drop_busy && drop_filev != NULL
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +02006130 && !text_locked() && !curbuf_locked() && !updating_screen)
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006131 handle_drop_internal();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006132}
6133#endif
6134
Bram Moolenaar071d4272004-06-13 20:20:40 +00006135/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006136 * ":preserve".
6137 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006138 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006139ex_preserve(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006140{
Bram Moolenaar4399ef42005-02-12 14:29:27 +00006141 curbuf->b_flags |= BF_PRESERVED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006142 ml_preserve(curbuf, TRUE);
6143}
6144
6145/*
6146 * ":recover".
6147 */
6148 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006149ex_recover(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006150{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006151 // Set recoverymode right away to avoid the ATTENTION prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006152 recoverymode = TRUE;
Bram Moolenaar45d3b142013-11-09 03:31:51 +01006153 if (!check_changed(curbuf, (p_awa ? CCGD_AW : 0)
6154 | CCGD_MULTWIN
6155 | (eap->forceit ? CCGD_FORCEIT : 0)
6156 | CCGD_EXCMD)
6157
Bram Moolenaar071d4272004-06-13 20:20:40 +00006158 && (*eap->arg == NUL
6159 || setfname(curbuf, eap->arg, NULL, TRUE) == OK))
Bram Moolenaar99499b12019-05-23 21:35:48 +02006160 ml_recover(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006161 recoverymode = FALSE;
6162}
6163
6164/*
6165 * Command modifier used in a wrong way.
6166 */
6167 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006168ex_wrongmodifier(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006169{
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006170 eap->errmsg = _(e_invalid_command);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006171}
6172
Bram Moolenaar071d4272004-06-13 20:20:40 +00006173/*
6174 * :sview [+command] file split window with new file, read-only
6175 * :split [[+command] file] split window with current or new file
6176 * :vsplit [[+command] file] split window vertically with current or new file
6177 * :new [[+command] file] split window with no or new file
6178 * :vnew [[+command] file] split vertically window with no or new file
6179 * :sfind [+command] file split window with file in 'path'
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006180 *
6181 * :tabedit open new Tab page with empty window
6182 * :tabedit [+command] file open new Tab page and edit "file"
6183 * :tabnew [[+command] file] just like :tabedit
6184 * :tabfind [+command] file open new Tab page and find "file"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006185 */
6186 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006187ex_splitview(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006188{
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006189 win_T *old_curwin = curwin;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006190#if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006191 char_u *fname = NULL;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006192#endif
6193#ifdef FEAT_BROWSE
Bram Moolenaar21cbe172020-10-13 19:08:24 +02006194 char_u dot_path[] = ".";
Bram Moolenaare1004402020-10-24 20:49:43 +02006195 int save_cmod_flags = cmdmod.cmod_flags;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006196#endif
Bram Moolenaar39902a02018-06-21 22:10:08 +02006197 int use_tab = eap->cmdidx == CMD_tabedit
6198 || eap->cmdidx == CMD_tabfind
6199 || eap->cmdidx == CMD_tabnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006200
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01006201 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02006202 return;
6203
Bram Moolenaar4033c552017-09-16 20:54:51 +02006204#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00006205 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006206#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006207
Bram Moolenaar4033c552017-09-16 20:54:51 +02006208#ifdef FEAT_QUICKFIX
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006209 // A ":split" in the quickfix window works like ":new". Don't want two
6210 // quickfix windows. But it's OK when doing ":tab split".
Bram Moolenaare1004402020-10-24 20:49:43 +02006211 if (bt_quickfix(curbuf) && cmdmod.cmod_tab == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006212 {
6213 if (eap->cmdidx == CMD_split)
6214 eap->cmdidx = CMD_new;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006215 if (eap->cmdidx == CMD_vsplit)
6216 eap->cmdidx = CMD_vnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006217 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02006218#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006219
Bram Moolenaar4033c552017-09-16 20:54:51 +02006220#ifdef FEAT_SEARCHPATH
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006221 if (eap->cmdidx == CMD_sfind || eap->cmdidx == CMD_tabfind)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006222 {
6223 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg),
6224 FNAME_MESS, TRUE, curbuf->b_ffname);
6225 if (fname == NULL)
6226 goto theend;
6227 eap->arg = fname;
6228 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006229# ifdef FEAT_BROWSE
Bram Moolenaar4033c552017-09-16 20:54:51 +02006230 else
6231# endif
6232#endif
6233#ifdef FEAT_BROWSE
Bram Moolenaare1004402020-10-24 20:49:43 +02006234 if ((cmdmod.cmod_flags & CMOD_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006235 && eap->cmdidx != CMD_vnew
Bram Moolenaar071d4272004-06-13 20:20:40 +00006236 && eap->cmdidx != CMD_new)
6237 {
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006238 if (
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006239# ifdef FEAT_GUI
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006240 !gui.in_use &&
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006241# endif
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006242 au_has_group((char_u *)"FileExplorer"))
6243 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006244 // No browsing supported but we do have the file explorer:
6245 // Edit the directory.
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006246 if (*eap->arg == NUL || !mch_isdir(eap->arg))
Bram Moolenaar21cbe172020-10-13 19:08:24 +02006247 eap->arg = dot_path;
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006248 }
6249 else
6250 {
Bram Moolenaar39902a02018-06-21 22:10:08 +02006251 fname = do_browse(0, (char_u *)(use_tab
6252 ? _("Edit File in new tab page")
6253 : _("Edit File in new window")),
Bram Moolenaar071d4272004-06-13 20:20:40 +00006254 eap->arg, NULL, NULL, NULL, curbuf);
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006255 if (fname == NULL)
6256 goto theend;
6257 eap->arg = fname;
6258 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006259 }
Bram Moolenaare1004402020-10-24 20:49:43 +02006260 cmdmod.cmod_flags &= ~CMOD_BROWSE; // Don't browse again in do_ecmd().
Bram Moolenaar4033c552017-09-16 20:54:51 +02006261#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006262
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006263 /*
6264 * Either open new tab page or split the window.
6265 */
Bram Moolenaar39902a02018-06-21 22:10:08 +02006266 if (use_tab)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006267 {
Bram Moolenaare1004402020-10-24 20:49:43 +02006268 if (win_new_tabpage(cmdmod.cmod_tab != 0 ? cmdmod.cmod_tab
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006269 : eap->addr_count == 0 ? 0
6270 : (int)eap->line2 + 1) != FAIL)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006271 {
Bram Moolenaard2b66012008-01-09 19:30:36 +00006272 do_exedit(eap, old_curwin);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006273
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006274 // set the alternate buffer for the window we came from
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006275 if (curwin != old_curwin
6276 && win_valid(old_curwin)
6277 && old_curwin->w_buffer != curbuf
Bram Moolenaare1004402020-10-24 20:49:43 +02006278 && (cmdmod.cmod_flags & CMOD_KEEPALT) == 0)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006279 old_curwin->w_alt_fnum = curbuf->b_fnum;
6280 }
6281 }
6282 else if (win_split(eap->addr_count > 0 ? (int)eap->line2 : 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006283 *eap->cmd == 'v' ? WSP_VERT : 0) != FAIL)
6284 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006285 // Reset 'scrollbind' when editing another file, but keep it when
6286 // doing ":split" without arguments.
Bram Moolenaare1004402020-10-24 20:49:43 +02006287 if (*eap->arg != NUL)
Bram Moolenaar3368ea22010-09-21 16:56:35 +02006288 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006289 else
6290 do_check_scrollbind(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006291 do_exedit(eap, old_curwin);
6292 }
6293
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006294# ifdef FEAT_BROWSE
Bram Moolenaare1004402020-10-24 20:49:43 +02006295 cmdmod.cmod_flags = save_cmod_flags;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006296# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006297
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006298# if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006299theend:
6300 vim_free(fname);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006301# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006302}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006303
6304/*
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006305 * Open a new tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006306 */
6307 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006308tabpage_new(void)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006309{
6310 exarg_T ea;
6311
Bram Moolenaara80faa82020-04-12 19:37:17 +02006312 CLEAR_FIELD(ea);
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006313 ea.cmdidx = CMD_tabnew;
6314 ea.cmd = (char_u *)"tabn";
6315 ea.arg = (char_u *)"";
6316 ex_splitview(&ea);
6317}
6318
6319/*
6320 * :tabnext command
6321 */
6322 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006323ex_tabnext(exarg_T *eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006324{
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006325 int tab_number;
6326
Bram Moolenaar8cdbd5b2019-06-16 15:50:45 +02006327 if (ERROR_IF_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02006328 return;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006329 switch (eap->cmdidx)
6330 {
6331 case CMD_tabfirst:
6332 case CMD_tabrewind:
6333 goto_tabpage(1);
6334 break;
6335 case CMD_tablast:
6336 goto_tabpage(9999);
6337 break;
6338 case CMD_tabprevious:
6339 case CMD_tabNext:
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006340 if (eap->arg && *eap->arg != NUL)
6341 {
6342 char_u *p = eap->arg;
6343 char_u *p_save = p;
6344
6345 tab_number = getdigits(&p);
6346 if (p == p_save || *p_save == '-' || *p != NUL
6347 || tab_number == 0)
6348 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006349 // No numbers as argument.
Bram Moolenaar8930caa2020-07-23 16:37:03 +02006350 eap->errmsg = ex_errmsg(e_invarg2, eap->arg);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006351 return;
6352 }
6353 }
6354 else
6355 {
6356 if (eap->addr_count == 0)
6357 tab_number = 1;
6358 else
6359 {
6360 tab_number = eap->line2;
6361 if (tab_number < 1)
6362 {
Bram Moolenaar8930caa2020-07-23 16:37:03 +02006363 eap->errmsg = _(e_invrange);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006364 return;
6365 }
6366 }
6367 }
6368 goto_tabpage(-tab_number);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006369 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006370 default: // CMD_tabnext
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006371 tab_number = get_tabpage_arg(eap);
6372 if (eap->errmsg == NULL)
6373 goto_tabpage(tab_number);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006374 break;
6375 }
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006376}
6377
6378/*
6379 * :tabmove command
6380 */
6381 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006382ex_tabmove(exarg_T *eap)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006383{
Bram Moolenaar40ce3a42015-04-21 18:08:39 +02006384 int tab_number;
Bram Moolenaar8cb8dca2012-07-06 18:27:39 +02006385
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006386 tab_number = get_tabpage_arg(eap);
6387 if (eap->errmsg == NULL)
6388 tabpage_move(tab_number);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006389}
6390
6391/*
6392 * :tabs command: List tabs and their contents.
6393 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006394 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006395ex_tabs(exarg_T *eap UNUSED)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006396{
6397 tabpage_T *tp;
6398 win_T *wp;
6399 int tabcount = 1;
6400
6401 msg_start();
6402 msg_scroll = TRUE;
6403 for (tp = first_tabpage; tp != NULL && !got_int; tp = tp->tp_next)
6404 {
6405 msg_putchar('\n');
6406 vim_snprintf((char *)IObuff, IOSIZE, _("Tab page %d"), tabcount++);
Bram Moolenaar8820b482017-03-16 17:23:31 +01006407 msg_outtrans_attr(IObuff, HL_ATTR(HLF_T));
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006408 out_flush(); // output one line at a time
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006409 ui_breakcheck();
6410
Bram Moolenaar030f0df2006-02-21 22:02:53 +00006411 if (tp == curtab)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006412 wp = firstwin;
6413 else
6414 wp = tp->tp_firstwin;
6415 for ( ; wp != NULL && !got_int; wp = wp->w_next)
6416 {
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006417 msg_putchar('\n');
6418 msg_putchar(wp == curwin ? '>' : ' ');
6419 msg_putchar(' ');
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006420 msg_putchar(bufIsChanged(wp->w_buffer) ? '+' : ' ');
6421 msg_putchar(' ');
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006422 if (buf_spname(wp->w_buffer) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02006423 vim_strncpy(IObuff, buf_spname(wp->w_buffer), IOSIZE - 1);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006424 else
6425 home_replace(wp->w_buffer, wp->w_buffer->b_fname,
6426 IObuff, IOSIZE, TRUE);
6427 msg_outtrans(IObuff);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006428 out_flush(); // output one line at a time
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006429 ui_breakcheck();
6430 }
6431 }
6432}
6433
Bram Moolenaar071d4272004-06-13 20:20:40 +00006434/*
6435 * ":mode": Set screen mode.
6436 * If no argument given, just get the screen size and redraw.
6437 */
6438 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006439ex_mode(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006440{
6441 if (*eap->arg == NUL)
6442 shell_resized();
6443 else
Bram Moolenaar3c71aec2020-01-17 19:32:20 +01006444 emsg(_(e_screenmode));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006445}
6446
Bram Moolenaar071d4272004-06-13 20:20:40 +00006447/*
6448 * ":resize".
6449 * set, increment or decrement current window height
6450 */
6451 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006452ex_resize(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006453{
6454 int n;
6455 win_T *wp = curwin;
6456
6457 if (eap->addr_count > 0)
6458 {
6459 n = eap->line2;
6460 for (wp = firstwin; wp->w_next != NULL && --n > 0; wp = wp->w_next)
6461 ;
6462 }
6463
Bram Moolenaar44a2f922016-03-19 22:11:51 +01006464# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00006465 need_mouse_correct = TRUE;
Bram Moolenaar44a2f922016-03-19 22:11:51 +01006466# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006467 n = atol((char *)eap->arg);
Bram Moolenaare1004402020-10-24 20:49:43 +02006468 if (cmdmod.cmod_split & WSP_VERT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006469 {
6470 if (*eap->arg == '-' || *eap->arg == '+')
Bram Moolenaar9668cc52020-10-17 17:39:55 +02006471 n += wp->w_width;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006472 else if (n == 0 && eap->arg[0] == NUL) // default is very wide
Bram Moolenaar071d4272004-06-13 20:20:40 +00006473 n = 9999;
6474 win_setwidth_win((int)n, wp);
6475 }
6476 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006477 {
6478 if (*eap->arg == '-' || *eap->arg == '+')
Bram Moolenaar9668cc52020-10-17 17:39:55 +02006479 n += wp->w_height;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006480 else if (n == 0 && eap->arg[0] == NUL) // default is very high
Bram Moolenaar071d4272004-06-13 20:20:40 +00006481 n = 9999;
6482 win_setheight_win((int)n, wp);
6483 }
6484}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006485
6486/*
6487 * ":find [+command] <file>" command.
6488 */
6489 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006490ex_find(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006491{
6492#ifdef FEAT_SEARCHPATH
6493 char_u *fname;
6494 int count;
6495
6496 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg), FNAME_MESS,
6497 TRUE, curbuf->b_ffname);
6498 if (eap->addr_count > 0)
6499 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006500 // Repeat finding the file "count" times. This matters when it
6501 // appears several times in the path.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006502 count = eap->line2;
6503 while (fname != NULL && --count > 0)
6504 {
6505 vim_free(fname);
6506 fname = find_file_in_path(NULL, 0, FNAME_MESS,
6507 FALSE, curbuf->b_ffname);
6508 }
6509 }
6510
6511 if (fname != NULL)
6512 {
6513 eap->arg = fname;
6514#endif
6515 do_exedit(eap, NULL);
6516#ifdef FEAT_SEARCHPATH
6517 vim_free(fname);
6518 }
6519#endif
6520}
6521
6522/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00006523 * ":open" simulation: for now just work like ":visual".
6524 */
6525 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006526ex_open(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00006527{
6528 regmatch_T regmatch;
6529 char_u *p;
6530
6531 curwin->w_cursor.lnum = eap->line2;
6532 beginline(BL_SOL | BL_FIX);
6533 if (*eap->arg == '/')
6534 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006535 // ":open /pattern/": put cursor in column found with pattern
Bram Moolenaardf177f62005-02-22 08:39:57 +00006536 ++eap->arg;
Bram Moolenaarf4e20992020-12-21 19:59:08 +01006537 p = skip_regexp(eap->arg, '/', magic_isset());
Bram Moolenaardf177f62005-02-22 08:39:57 +00006538 *p = NUL;
Bram Moolenaarf4e20992020-12-21 19:59:08 +01006539 regmatch.regprog = vim_regcomp(eap->arg, magic_isset() ? RE_MAGIC : 0);
Bram Moolenaardf177f62005-02-22 08:39:57 +00006540 if (regmatch.regprog != NULL)
6541 {
6542 regmatch.rm_ic = p_ic;
6543 p = ml_get_curline();
6544 if (vim_regexec(&regmatch, p, (colnr_T)0))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006545 curwin->w_cursor.col = (colnr_T)(regmatch.startp[0] - p);
Bram Moolenaardf177f62005-02-22 08:39:57 +00006546 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006547 emsg(_(e_nomatch));
Bram Moolenaar473de612013-06-08 18:19:48 +02006548 vim_regfree(regmatch.regprog);
Bram Moolenaardf177f62005-02-22 08:39:57 +00006549 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006550 // Move to the NUL, ignore any other arguments.
Bram Moolenaardf177f62005-02-22 08:39:57 +00006551 eap->arg += STRLEN(eap->arg);
6552 }
6553 check_cursor();
6554
6555 eap->cmdidx = CMD_visual;
6556 do_exedit(eap, NULL);
6557}
6558
6559/*
Bram Moolenaar59d8e562020-11-07 18:41:10 +01006560 * ":edit", ":badd", ":balt", ":visual".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006561 */
6562 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006563ex_edit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006564{
6565 do_exedit(eap, NULL);
6566}
6567
6568/*
Bram Moolenaar4b96df52020-01-26 22:00:26 +01006569 * ":edit <file>" command and alike.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006570 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006571 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006572do_exedit(
6573 exarg_T *eap,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006574 win_T *old_curwin) // curwin before doing a split or NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006575{
6576 int n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006577 int need_hide;
Bram Moolenaardf177f62005-02-22 08:39:57 +00006578 int exmode_was = exmode_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006579
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01006580 if ((eap->cmdidx != CMD_pedit && ERROR_IF_POPUP_WINDOW)
6581 || ERROR_IF_TERM_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02006582 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006583 /*
6584 * ":vi" command ends Ex mode.
6585 */
6586 if (exmode_active && (eap->cmdidx == CMD_visual
6587 || eap->cmdidx == CMD_view))
6588 {
6589 exmode_active = FALSE;
Bram Moolenaar158ea172020-06-18 17:28:39 +02006590 ex_pressedreturn = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006591 if (*eap->arg == NUL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00006592 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006593 // Special case: ":global/pat/visual\NLvi-commands"
Bram Moolenaardf177f62005-02-22 08:39:57 +00006594 if (global_busy)
6595 {
6596 int rd = RedrawingDisabled;
6597 int nwr = no_wait_return;
6598 int ms = msg_scroll;
6599#ifdef FEAT_GUI
6600 int he = hold_gui_events;
6601#endif
6602
6603 if (eap->nextcmd != NULL)
6604 {
6605 stuffReadbuff(eap->nextcmd);
6606 eap->nextcmd = NULL;
6607 }
6608
6609 if (exmode_was != EXMODE_VIM)
6610 settmode(TMODE_RAW);
6611 RedrawingDisabled = 0;
6612 no_wait_return = 0;
6613 need_wait_return = FALSE;
6614 msg_scroll = 0;
6615#ifdef FEAT_GUI
6616 hold_gui_events = 0;
6617#endif
6618 must_redraw = CLEAR;
Bram Moolenaar9e2bcb52020-02-18 21:33:00 +01006619 pending_exmode_active = TRUE;
Bram Moolenaardf177f62005-02-22 08:39:57 +00006620
6621 main_loop(FALSE, TRUE);
6622
Bram Moolenaar9e2bcb52020-02-18 21:33:00 +01006623 pending_exmode_active = FALSE;
Bram Moolenaardf177f62005-02-22 08:39:57 +00006624 RedrawingDisabled = rd;
6625 no_wait_return = nwr;
6626 msg_scroll = ms;
6627#ifdef FEAT_GUI
6628 hold_gui_events = he;
6629#endif
6630 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006631 return;
Bram Moolenaardf177f62005-02-22 08:39:57 +00006632 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006633 }
6634
6635 if ((eap->cmdidx == CMD_new
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006636 || eap->cmdidx == CMD_tabnew
6637 || eap->cmdidx == CMD_tabedit
Bram Moolenaar4033c552017-09-16 20:54:51 +02006638 || eap->cmdidx == CMD_vnew) && *eap->arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006639 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006640 // ":new" or ":tabnew" without argument: edit an new empty buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00006641 setpcmark();
6642 (void)do_ecmd(0, NULL, NULL, eap, ECMD_ONE,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00006643 ECMD_HIDE + (eap->forceit ? ECMD_FORCEIT : 0),
6644 old_curwin == NULL ? curwin : NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006645 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02006646 else if ((eap->cmdidx != CMD_split && eap->cmdidx != CMD_vsplit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006647 || *eap->arg != NUL
6648#ifdef FEAT_BROWSE
Bram Moolenaare1004402020-10-24 20:49:43 +02006649 || (cmdmod.cmod_flags & CMOD_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006650#endif
6651 )
6652 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006653 // Can't edit another file when "curbuf_lock" is set. Only ":edit"
6654 // can bring us here, others are stopped earlier.
Bram Moolenaar5555acc2006-04-07 21:33:12 +00006655 if (*eap->arg != NUL && curbuf_locked())
6656 return;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006657
Bram Moolenaar071d4272004-06-13 20:20:40 +00006658 n = readonlymode;
6659 if (eap->cmdidx == CMD_view || eap->cmdidx == CMD_sview)
6660 readonlymode = TRUE;
6661 else if (eap->cmdidx == CMD_enew)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006662 readonlymode = FALSE; // 'readonly' doesn't make sense in an
6663 // empty buffer
Bram Moolenaar3482be62020-11-27 11:00:38 +01006664 if (eap->cmdidx != CMD_balt && eap->cmdidx != CMD_badd)
6665 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006666 if (do_ecmd(0, (eap->cmdidx == CMD_enew ? NULL : eap->arg),
6667 NULL, eap,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006668 // ":edit" goes to first line if Vi compatible
Bram Moolenaar071d4272004-06-13 20:20:40 +00006669 (*eap->arg == NUL && eap->do_ecmd_lnum == 0
6670 && vim_strchr(p_cpo, CPO_GOTO1) != NULL)
6671 ? ECMD_ONE : eap->do_ecmd_lnum,
Bram Moolenaareb44a682017-08-03 22:44:55 +02006672 (buf_hide(curbuf) ? ECMD_HIDE : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006673 + (eap->forceit ? ECMD_FORCEIT : 0)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006674 // after a split we can use an existing buffer
Bram Moolenaar7b449342014-03-25 13:03:48 +01006675 + (old_curwin != NULL ? ECMD_OLDBUF : 0)
Bram Moolenaar59d8e562020-11-07 18:41:10 +01006676 + (eap->cmdidx == CMD_badd ? ECMD_ADDBUF : 0)
6677 + (eap->cmdidx == CMD_balt ? ECMD_ALTBUF : 0)
Bram Moolenaar701f7af2008-11-15 13:12:07 +00006678 , old_curwin == NULL ? curwin : NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006679 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006680 // Editing the file failed. If the window was split, close it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006681 if (old_curwin != NULL)
6682 {
6683 need_hide = (curbufIsChanged() && curbuf->b_nwindows <= 1);
Bram Moolenaareb44a682017-08-03 22:44:55 +02006684 if (!need_hide || buf_hide(curbuf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006685 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006686#if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00006687 cleanup_T cs;
6688
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006689 // Reset the error/interrupt/exception state here so that
6690 // aborting() returns FALSE when closing a window.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00006691 enter_cleanup(&cs);
Bram Moolenaar4033c552017-09-16 20:54:51 +02006692#endif
6693#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00006694 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006695#endif
Bram Moolenaareb44a682017-08-03 22:44:55 +02006696 win_close(curwin, !need_hide && !buf_hide(curbuf));
Bram Moolenaarc0197e22004-09-13 20:26:32 +00006697
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006698#if defined(FEAT_EVAL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006699 // Restore the error/interrupt/exception state if not
6700 // discarded by a new aborting error, interrupt, or
6701 // uncaught exception.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00006702 leave_cleanup(&cs);
Bram Moolenaar4033c552017-09-16 20:54:51 +02006703#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006704 }
6705 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006706 }
6707 else if (readonlymode && curbuf->b_nwindows == 1)
6708 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006709 // When editing an already visited buffer, 'readonly' won't be set
6710 // but the previous value is kept. With ":view" and ":sview" we
6711 // want the file to be readonly, except when another window is
6712 // editing the same buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006713 curbuf->b_p_ro = TRUE;
6714 }
6715 readonlymode = n;
6716 }
6717 else
6718 {
6719 if (eap->do_ecmd_cmd != NULL)
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01006720 do_cmd_argument(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006721#ifdef FEAT_TITLE
6722 n = curwin->w_arg_idx_invalid;
6723#endif
6724 check_arg_idx(curwin);
6725#ifdef FEAT_TITLE
6726 if (n != curwin->w_arg_idx_invalid)
6727 maketitle();
6728#endif
6729 }
6730
Bram Moolenaar071d4272004-06-13 20:20:40 +00006731 /*
6732 * if ":split file" worked, set alternate file name in old window to new
6733 * file
6734 */
6735 if (old_curwin != NULL
6736 && *eap->arg != NUL
6737 && curwin != old_curwin
6738 && win_valid(old_curwin)
Bram Moolenaard4755bb2004-09-02 19:12:26 +00006739 && old_curwin->w_buffer != curbuf
Bram Moolenaare1004402020-10-24 20:49:43 +02006740 && (cmdmod.cmod_flags & CMOD_KEEPALT) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006741 old_curwin->w_alt_fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006742
6743 ex_no_reprint = TRUE;
6744}
6745
6746#ifndef FEAT_GUI
6747/*
6748 * ":gui" and ":gvim" when there is no GUI.
6749 */
6750 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006751ex_nogui(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006752{
Bram Moolenaar8930caa2020-07-23 16:37:03 +02006753 eap->errmsg = _(e_nogvim);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006754}
6755#endif
6756
Bram Moolenaar4f974752019-02-17 17:44:42 +01006757#if defined(FEAT_GUI_MSWIN) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006758 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006759ex_tearoff(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006760{
6761 gui_make_tearoff(eap->arg);
6762}
6763#endif
6764
Bram Moolenaar29a2c082018-03-05 21:06:23 +01006765#if (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK) \
6766 || defined(FEAT_TERM_POPUP_MENU)) && defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006767 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006768ex_popup(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006769{
Bram Moolenaar29a2c082018-03-05 21:06:23 +01006770# if defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK)
6771 if (gui.in_use)
6772 gui_make_popup(eap->arg, eap->forceit);
6773# ifdef FEAT_TERM_POPUP_MENU
6774 else
6775# endif
6776# endif
6777# ifdef FEAT_TERM_POPUP_MENU
6778 pum_make_popup(eap->arg, eap->forceit);
6779# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006780}
6781#endif
6782
Bram Moolenaar071d4272004-06-13 20:20:40 +00006783 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006784ex_swapname(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006785{
6786 if (curbuf->b_ml.ml_mfp == NULL || curbuf->b_ml.ml_mfp->mf_fname == NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01006787 msg(_("No swap file"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006788 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01006789 msg((char *)curbuf->b_ml.ml_mfp->mf_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006790}
6791
6792/*
6793 * ":syncbind" forces all 'scrollbind' windows to have the same relative
6794 * offset.
6795 * (1998-11-02 16:21:01 R. Edward Ralston <eralston@computer.org>)
6796 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006797 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006798ex_syncbind(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006799{
Bram Moolenaar071d4272004-06-13 20:20:40 +00006800 win_T *wp;
Bram Moolenaardedd1b02013-12-14 13:06:17 +01006801 win_T *save_curwin = curwin;
6802 buf_T *save_curbuf = curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006803 long topline;
6804 long y;
6805 linenr_T old_linenr = curwin->w_cursor.lnum;
6806
6807 setpcmark();
6808
6809 /*
6810 * determine max topline
6811 */
6812 if (curwin->w_p_scb)
6813 {
6814 topline = curwin->w_topline;
Bram Moolenaar29323592016-07-24 22:04:11 +02006815 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006816 {
6817 if (wp->w_p_scb && wp->w_buffer)
6818 {
Bram Moolenaar375e3392019-01-31 18:26:10 +01006819 y = wp->w_buffer->b_ml.ml_line_count - get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006820 if (topline > y)
6821 topline = y;
6822 }
6823 }
6824 if (topline < 1)
6825 topline = 1;
6826 }
6827 else
6828 {
6829 topline = 1;
6830 }
6831
6832
6833 /*
Bram Moolenaardedd1b02013-12-14 13:06:17 +01006834 * Set all scrollbind windows to the same topline.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006835 */
Bram Moolenaar29323592016-07-24 22:04:11 +02006836 FOR_ALL_WINDOWS(curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006837 {
6838 if (curwin->w_p_scb)
6839 {
Bram Moolenaardedd1b02013-12-14 13:06:17 +01006840 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006841 y = topline - curwin->w_topline;
6842 if (y > 0)
6843 scrollup(y, TRUE);
6844 else
6845 scrolldown(-y, TRUE);
6846 curwin->w_scbind_pos = topline;
6847 redraw_later(VALID);
6848 cursor_correct();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006849 curwin->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006850 }
6851 }
Bram Moolenaardedd1b02013-12-14 13:06:17 +01006852 curwin = save_curwin;
6853 curbuf = save_curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006854 if (curwin->w_p_scb)
6855 {
6856 did_syncbind = TRUE;
6857 checkpcmark();
6858 if (old_linenr != curwin->w_cursor.lnum)
6859 {
6860 char_u ctrl_o[2];
6861
6862 ctrl_o[0] = Ctrl_O;
6863 ctrl_o[1] = 0;
6864 ins_typebuf(ctrl_o, REMAP_NONE, 0, TRUE, FALSE);
6865 }
6866 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006867}
6868
6869
6870 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006871ex_read(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006872{
Bram Moolenaardf177f62005-02-22 08:39:57 +00006873 int i;
6874 int empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
6875 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006876
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006877 if (eap->usefilter) // :r!cmd
Bram Moolenaar071d4272004-06-13 20:20:40 +00006878 do_bang(1, eap, FALSE, FALSE, TRUE);
6879 else
6880 {
6881 if (u_save(eap->line2, (linenr_T)(eap->line2 + 1)) == FAIL)
6882 return;
6883
6884#ifdef FEAT_BROWSE
Bram Moolenaare1004402020-10-24 20:49:43 +02006885 if (cmdmod.cmod_flags & CMOD_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006886 {
6887 char_u *browseFile;
6888
Bram Moolenaar7171abe2004-10-11 10:06:20 +00006889 browseFile = do_browse(0, (char_u *)_("Append File"), eap->arg,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006890 NULL, NULL, NULL, curbuf);
6891 if (browseFile != NULL)
6892 {
6893 i = readfile(browseFile, NULL,
6894 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
6895 vim_free(browseFile);
6896 }
6897 else
6898 i = OK;
6899 }
6900 else
6901#endif
6902 if (*eap->arg == NUL)
6903 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006904 if (check_fname() == FAIL) // check for no file name
Bram Moolenaar071d4272004-06-13 20:20:40 +00006905 return;
6906 i = readfile(curbuf->b_ffname, curbuf->b_fname,
6907 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
6908 }
6909 else
6910 {
6911 if (vim_strchr(p_cpo, CPO_ALTREAD) != NULL)
6912 (void)setaltfname(eap->arg, eap->arg, (linenr_T)1);
6913 i = readfile(eap->arg, NULL,
6914 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
6915
6916 }
Bram Moolenaare13b9af2017-01-13 22:01:02 +01006917 if (i != OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006918 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006919#if defined(FEAT_EVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006920 if (!aborting())
6921#endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006922 semsg(_(e_notopen), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006923 }
6924 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00006925 {
6926 if (empty && exmode_active)
6927 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006928 // Delete the empty line that remains. Historically ex does
6929 // this but vi doesn't.
Bram Moolenaardf177f62005-02-22 08:39:57 +00006930 if (eap->line2 == 0)
6931 lnum = curbuf->b_ml.ml_line_count;
6932 else
6933 lnum = 1;
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00006934 if (*ml_get(lnum) == NUL && u_savedel(lnum, 1L) == OK)
Bram Moolenaardf177f62005-02-22 08:39:57 +00006935 {
Bram Moolenaarca70c072020-05-30 20:30:46 +02006936 ml_delete(lnum);
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00006937 if (curwin->w_cursor.lnum > 1
6938 && curwin->w_cursor.lnum >= lnum)
Bram Moolenaardf177f62005-02-22 08:39:57 +00006939 --curwin->w_cursor.lnum;
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00006940 deleted_lines_mark(lnum, 1L);
Bram Moolenaardf177f62005-02-22 08:39:57 +00006941 }
6942 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006943 redraw_curbuf_later(VALID);
Bram Moolenaardf177f62005-02-22 08:39:57 +00006944 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006945 }
6946}
6947
Bram Moolenaarea408852005-06-25 22:49:46 +00006948static char_u *prev_dir = NULL;
6949
6950#if defined(EXITFREE) || defined(PROTO)
6951 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006952free_cd_dir(void)
Bram Moolenaarea408852005-06-25 22:49:46 +00006953{
Bram Moolenaard23a8232018-02-10 18:45:26 +01006954 VIM_CLEAR(prev_dir);
6955 VIM_CLEAR(globaldir);
Bram Moolenaarea408852005-06-25 22:49:46 +00006956}
6957#endif
6958
Bram Moolenaarf4258302013-06-02 18:20:17 +02006959/*
Bram Moolenaar002bc792020-06-05 22:33:42 +02006960 * Get the previous directory for the given chdir scope.
6961 */
6962 static char_u *
6963get_prevdir(cdscope_T scope)
6964{
6965 if (scope == CDSCOPE_WINDOW)
6966 return curwin->w_prevdir;
6967 else if (scope == CDSCOPE_TABPAGE)
6968 return curtab->tp_prevdir;
6969 return prev_dir;
6970}
6971
6972/*
Bram Moolenaarf4258302013-06-02 18:20:17 +02006973 * Deal with the side effects of changing the current directory.
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006974 * When 'scope' is CDSCOPE_TABPAGE then this was after an ":tcd" command.
6975 * When 'scope' is CDSCOPE_WINDOW then this was after an ":lcd" command.
Bram Moolenaarf4258302013-06-02 18:20:17 +02006976 */
6977 void
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006978post_chdir(cdscope_T scope)
Bram Moolenaarf4258302013-06-02 18:20:17 +02006979{
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006980 if (scope != CDSCOPE_WINDOW)
Bram Moolenaar00aa0692019-04-27 20:37:57 +02006981 // Clear tab local directory for both :cd and :tcd
6982 VIM_CLEAR(curtab->tp_localdir);
Bram Moolenaard23a8232018-02-10 18:45:26 +01006983 VIM_CLEAR(curwin->w_localdir);
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006984 if (scope != CDSCOPE_GLOBAL)
Bram Moolenaarf4258302013-06-02 18:20:17 +02006985 {
Bram Moolenaar002bc792020-06-05 22:33:42 +02006986 char_u *pdir = get_prevdir(scope);
6987
6988 // If still in the global directory, need to remember current
6989 // directory as the global directory.
6990 if (globaldir == NULL && pdir != NULL)
6991 globaldir = vim_strsave(pdir);
6992
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006993 // Remember this local directory for the window.
Bram Moolenaarf4258302013-06-02 18:20:17 +02006994 if (mch_dirname(NameBuff, MAXPATHL) == OK)
Bram Moolenaar00aa0692019-04-27 20:37:57 +02006995 {
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006996 if (scope == CDSCOPE_TABPAGE)
Bram Moolenaar00aa0692019-04-27 20:37:57 +02006997 curtab->tp_localdir = vim_strsave(NameBuff);
6998 else
6999 curwin->w_localdir = vim_strsave(NameBuff);
7000 }
Bram Moolenaarf4258302013-06-02 18:20:17 +02007001 }
7002 else
7003 {
Bram Moolenaar002bc792020-06-05 22:33:42 +02007004 // We are now in the global directory, no need to remember its name.
Bram Moolenaard23a8232018-02-10 18:45:26 +01007005 VIM_CLEAR(globaldir);
Bram Moolenaarf4258302013-06-02 18:20:17 +02007006 }
7007
7008 shorten_fnames(TRUE);
7009}
7010
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007011/*
7012 * Change directory function used by :cd/:tcd/:lcd Ex commands and the
Bram Moolenaar95058722020-06-01 16:26:19 +02007013 * chdir() function.
7014 * scope == CDSCOPE_WINDOW: changes the window-local directory
7015 * scope == CDSCOPE_TABPAGE: changes the tab-local directory
7016 * Otherwise: changes the global directory
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007017 * Returns TRUE if the directory is successfully changed.
7018 */
7019 int
7020changedir_func(
7021 char_u *new_dir,
7022 int forceit,
7023 cdscope_T scope)
7024{
7025 char_u *tofree;
Bram Moolenaar002bc792020-06-05 22:33:42 +02007026 char_u *pdir = NULL;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007027 int dir_differs;
7028 int retval = FALSE;
7029
Bram Moolenaar7cc96922020-01-31 22:41:38 +01007030 if (new_dir == NULL || allbuf_locked())
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007031 return FALSE;
7032
7033 if (vim_strchr(p_cpo, CPO_CHDIR) != NULL && curbufIsChanged() && !forceit)
7034 {
7035 emsg(_("E747: Cannot change directory, buffer is modified (add ! to override)"));
7036 return FALSE;
7037 }
7038
7039 // ":cd -": Change to previous directory
7040 if (STRCMP(new_dir, "-") == 0)
7041 {
Bram Moolenaar002bc792020-06-05 22:33:42 +02007042 pdir = get_prevdir(scope);
7043 if (pdir == NULL)
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007044 {
7045 emsg(_("E186: No previous directory"));
7046 return FALSE;
7047 }
Bram Moolenaar002bc792020-06-05 22:33:42 +02007048 new_dir = pdir;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007049 }
7050
Bram Moolenaar002bc792020-06-05 22:33:42 +02007051 // Free the previous directory
7052 tofree = get_prevdir(scope);
7053
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007054 // Save current directory for next ":cd -"
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007055 if (mch_dirname(NameBuff, MAXPATHL) == OK)
Bram Moolenaar002bc792020-06-05 22:33:42 +02007056 pdir = vim_strsave(NameBuff);
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007057 else
Bram Moolenaar002bc792020-06-05 22:33:42 +02007058 pdir = NULL;
7059 if (scope == CDSCOPE_WINDOW)
7060 curwin->w_prevdir = pdir;
7061 else if (scope == CDSCOPE_TABPAGE)
7062 curtab->tp_prevdir = pdir;
7063 else
7064 prev_dir = pdir;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007065
7066#if defined(UNIX) || defined(VMS)
7067 // for UNIX ":cd" means: go to home directory
7068 if (*new_dir == NUL)
7069 {
7070 // use NameBuff for home directory name
7071# ifdef VMS
7072 char_u *p;
7073
7074 p = mch_getenv((char_u *)"SYS$LOGIN");
7075 if (p == NULL || *p == NUL) // empty is the same as not set
7076 NameBuff[0] = NUL;
7077 else
7078 vim_strncpy(NameBuff, p, MAXPATHL - 1);
7079# else
7080 expand_env((char_u *)"$HOME", NameBuff, MAXPATHL);
7081# endif
7082 new_dir = NameBuff;
7083 }
7084#endif
Bram Moolenaar002bc792020-06-05 22:33:42 +02007085 dir_differs = new_dir == NULL || pdir == NULL
7086 || pathcmp((char *)pdir, (char *)new_dir, -1) != 0;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007087 if (new_dir == NULL || (dir_differs && vim_chdir(new_dir)))
7088 emsg(_(e_failed));
7089 else
7090 {
7091 char_u *acmd_fname;
7092
7093 post_chdir(scope);
7094
7095 if (dir_differs)
7096 {
7097 if (scope == CDSCOPE_WINDOW)
7098 acmd_fname = (char_u *)"window";
7099 else if (scope == CDSCOPE_TABPAGE)
7100 acmd_fname = (char_u *)"tabpage";
7101 else
7102 acmd_fname = (char_u *)"global";
7103 apply_autocmds(EVENT_DIRCHANGED, acmd_fname, new_dir, FALSE,
7104 curbuf);
7105 }
7106 retval = TRUE;
7107 }
7108 vim_free(tofree);
7109
7110 return retval;
7111}
Bram Moolenaarea408852005-06-25 22:49:46 +00007112
Bram Moolenaar071d4272004-06-13 20:20:40 +00007113/*
Bram Moolenaar00aa0692019-04-27 20:37:57 +02007114 * ":cd", ":tcd", ":lcd", ":chdir" ":tchdir" and ":lchdir".
Bram Moolenaar071d4272004-06-13 20:20:40 +00007115 */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00007116 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007117ex_cd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007118{
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01007119 char_u *new_dir;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007120
7121 new_dir = eap->arg;
7122#if !defined(UNIX) && !defined(VMS)
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007123 // for non-UNIX ":cd" means: print current directory
Bram Moolenaar071d4272004-06-13 20:20:40 +00007124 if (*new_dir == NUL)
7125 ex_pwd(NULL);
7126 else
7127#endif
7128 {
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007129 cdscope_T scope = CDSCOPE_GLOBAL;
7130
7131 if (eap->cmdidx == CMD_lcd || eap->cmdidx == CMD_lchdir)
7132 scope = CDSCOPE_WINDOW;
7133 else if (eap->cmdidx == CMD_tcd || eap->cmdidx == CMD_tchdir)
7134 scope = CDSCOPE_TABPAGE;
7135
7136 if (changedir_func(new_dir, eap->forceit, scope))
Bram Moolenaardf177f62005-02-22 08:39:57 +00007137 {
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007138 // Echo the new current directory if the command was typed.
Bram Moolenaarfcfbc672009-07-09 18:13:49 +00007139 if (KeyTyped || p_verbose >= 5)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007140 ex_pwd(eap);
7141 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007142 }
7143}
7144
7145/*
7146 * ":pwd".
7147 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007148 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007149ex_pwd(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007150{
7151 if (mch_dirname(NameBuff, MAXPATHL) == OK)
7152 {
7153#ifdef BACKSLASH_IN_FILENAME
7154 slash_adjust(NameBuff);
7155#endif
Bram Moolenaar95058722020-06-01 16:26:19 +02007156 if (p_verbose > 0)
7157 {
7158 char *context = "global";
7159
7160 if (curwin->w_localdir != NULL)
7161 context = "window";
7162 else if (curtab->tp_localdir != NULL)
7163 context = "tabpage";
7164 smsg("[%s] %s", context, (char *)NameBuff);
7165 }
7166 else
7167 msg((char *)NameBuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007168 }
7169 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007170 emsg(_("E187: Unknown"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007171}
7172
7173/*
7174 * ":=".
7175 */
7176 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007177ex_equal(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007178{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007179 smsg("%ld", (long)eap->line2);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007180 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007181}
7182
7183 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007184ex_sleep(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007185{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007186 int n;
7187 long len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007188
7189 if (cursor_valid())
7190 {
7191 n = W_WINROW(curwin) + curwin->w_wrow - msg_scrolled;
7192 if (n >= 0)
Bram Moolenaar53f81742017-09-22 14:35:51 +02007193 windgoto((int)n, curwin->w_wincol + curwin->w_wcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007194 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007195
7196 len = eap->line2;
7197 switch (*eap->arg)
7198 {
7199 case 'm': break;
7200 case NUL: len *= 1000L; break;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007201 default: semsg(_(e_invarg2), eap->arg); return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007202 }
7203 do_sleep(len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007204}
7205
7206/*
7207 * Sleep for "msec" milliseconds, but keep checking for a CTRL-C every second.
7208 */
7209 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007210do_sleep(long msec)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007211{
Bram Moolenaar52953192019-08-16 10:27:13 +02007212 long done = 0;
Bram Moolenaar975b5272016-03-15 23:10:59 +01007213 long wait_now;
Bram Moolenaar52953192019-08-16 10:27:13 +02007214# ifdef ELAPSED_FUNC
7215 elapsed_T start_tv;
7216
7217 // Remember at what time we started, so that we know how much longer we
7218 // should wait after waiting for a bit.
7219 ELAPSED_INIT(start_tv);
7220# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007221
7222 cursor_on();
Bram Moolenaarf45d7472018-09-30 18:22:26 +02007223 out_flush_cursor(FALSE, FALSE);
Bram Moolenaar52953192019-08-16 10:27:13 +02007224 while (!got_int && done < msec)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007225 {
Bram Moolenaar975b5272016-03-15 23:10:59 +01007226 wait_now = msec - done > 1000L ? 1000L : msec - done;
7227#ifdef FEAT_TIMERS
7228 {
7229 long due_time = check_due_timer();
7230
7231 if (due_time > 0 && due_time < wait_now)
7232 wait_now = due_time;
7233 }
7234#endif
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02007235#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar28e67e02019-08-15 23:05:49 +02007236 if (has_any_channel() && wait_now > 20L)
7237 wait_now = 20L;
7238#endif
7239#ifdef FEAT_SOUND
7240 if (has_any_sound_callback() && wait_now > 20L)
7241 wait_now = 20L;
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02007242#endif
Bram Moolenaar975b5272016-03-15 23:10:59 +01007243 ui_delay(wait_now, TRUE);
Bram Moolenaar52953192019-08-16 10:27:13 +02007244
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02007245#ifdef FEAT_JOB_CHANNEL
7246 if (has_any_channel())
7247 ui_breakcheck_force(TRUE);
7248 else
7249#endif
7250 ui_breakcheck();
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007251#ifdef MESSAGE_QUEUE
Bram Moolenaar52953192019-08-16 10:27:13 +02007252 // Process the netbeans and clientserver messages that may have been
7253 // received in the call to ui_breakcheck() when the GUI is in use. This
7254 // may occur when running a test case.
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007255 parse_queued_messages();
Bram Moolenaare3cc6d42011-10-20 21:58:34 +02007256#endif
Bram Moolenaar52953192019-08-16 10:27:13 +02007257
7258# ifdef ELAPSED_FUNC
7259 // actual time passed
7260 done = ELAPSED_FUNC(start_tv);
7261# else
7262 // guestimate time passed (will actually be more)
7263 done += wait_now;
7264# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007265 }
Bram Moolenaar1ebff3d2018-07-07 16:18:13 +02007266
7267 // If CTRL-C was typed to interrupt the sleep, drop the CTRL-C from the
7268 // input buffer, otherwise a following call to input() fails.
7269 if (got_int)
7270 (void)vpeekc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007271}
7272
Bram Moolenaar071d4272004-06-13 20:20:40 +00007273/*
7274 * ":winsize" command (obsolete).
7275 */
7276 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007277ex_winsize(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007278{
7279 int w, h;
7280 char_u *arg = eap->arg;
7281 char_u *p;
7282
7283 w = getdigits(&arg);
7284 arg = skipwhite(arg);
7285 p = arg;
7286 h = getdigits(&arg);
7287 if (*p != NUL && *arg == NUL)
7288 set_shellsize(w, h, TRUE);
7289 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007290 emsg(_("E465: :winsize requires two number arguments"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007291}
7292
Bram Moolenaar071d4272004-06-13 20:20:40 +00007293 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007294ex_wincmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007295{
7296 int xchar = NUL;
7297 char_u *p;
7298
7299 if (*eap->arg == 'g' || *eap->arg == Ctrl_G)
7300 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007301 // CTRL-W g and CTRL-W CTRL-G have an extra command character
Bram Moolenaar071d4272004-06-13 20:20:40 +00007302 if (eap->arg[1] == NUL)
7303 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007304 emsg(_(e_invarg));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007305 return;
7306 }
7307 xchar = eap->arg[1];
7308 p = eap->arg + 2;
7309 }
7310 else
7311 p = eap->arg + 1;
7312
7313 eap->nextcmd = check_nextcmd(p);
7314 p = skipwhite(p);
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02007315 if (*p != NUL && *p != (
7316#ifdef FEAT_EVAL
7317 in_vim9script() ? '#' :
7318#endif
7319 '"')
7320 && eap->nextcmd == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007321 emsg(_(e_invarg));
Bram Moolenaar12bde492011-06-13 01:19:56 +02007322 else if (!eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007323 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007324 // Pass flags on for ":vertical wincmd ]".
Bram Moolenaare1004402020-10-24 20:49:43 +02007325 postponed_split_flags = cmdmod.cmod_split;
7326 postponed_split_tab = cmdmod.cmod_tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007327 do_window(*eap->arg, eap->addr_count > 0 ? eap->line2 : 0L, xchar);
7328 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00007329 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007330 }
7331}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007332
Bram Moolenaar843ee412004-06-30 16:16:41 +00007333#if defined(FEAT_GUI) || defined(UNIX) || defined(VMS) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007334/*
7335 * ":winpos".
7336 */
7337 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007338ex_winpos(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007339{
7340 int x, y;
7341 char_u *arg = eap->arg;
7342 char_u *p;
7343
7344 if (*arg == NUL)
7345 {
Bram Moolenaar843ee412004-06-30 16:16:41 +00007346# if defined(FEAT_GUI) || defined(MSWIN)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007347# ifdef VIMDLL
7348 if (gui.in_use ? gui_mch_get_winpos(&x, &y) != FAIL :
7349 mch_get_winpos(&x, &y) != FAIL)
7350# elif defined(FEAT_GUI)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007351 if (gui.in_use && gui_mch_get_winpos(&x, &y) != FAIL)
Bram Moolenaar843ee412004-06-30 16:16:41 +00007352# else
7353 if (mch_get_winpos(&x, &y) != FAIL)
7354# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007355 {
7356 sprintf((char *)IObuff, _("Window position: X %d, Y %d"), x, y);
Bram Moolenaar32526b32019-01-19 17:43:09 +01007357 msg((char *)IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007358 }
7359 else
7360# endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007361 emsg(_("E188: Obtaining window position not implemented for this platform"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007362 }
7363 else
7364 {
7365 x = getdigits(&arg);
7366 arg = skipwhite(arg);
7367 p = arg;
7368 y = getdigits(&arg);
7369 if (*p == NUL || *arg != NUL)
7370 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007371 emsg(_("E466: :winpos requires two number arguments"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007372 return;
7373 }
7374# ifdef FEAT_GUI
7375 if (gui.in_use)
7376 gui_mch_set_winpos(x, y);
7377 else if (gui.starting)
7378 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007379 // Remember the coordinates for when the window is opened.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007380 gui_win_x = x;
7381 gui_win_y = y;
7382 }
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007383# if defined(HAVE_TGETENT) || defined(VIMDLL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007384 else
7385# endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007386# endif
7387# if defined(MSWIN) && (!defined(FEAT_GUI) || defined(VIMDLL))
Bram Moolenaar843ee412004-06-30 16:16:41 +00007388 mch_set_winpos(x, y);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007389# endif
7390# ifdef HAVE_TGETENT
7391 if (*T_CWP)
7392 term_set_winpos(x, y);
7393# endif
7394 }
7395}
7396#endif
7397
7398/*
7399 * Handle command that work like operators: ":delete", ":yank", ":>" and ":<".
7400 */
7401 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007402ex_operators(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007403{
7404 oparg_T oa;
7405
7406 clear_oparg(&oa);
7407 oa.regname = eap->regname;
7408 oa.start.lnum = eap->line1;
7409 oa.end.lnum = eap->line2;
7410 oa.line_count = eap->line2 - eap->line1 + 1;
7411 oa.motion_type = MLINE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007412 virtual_op = FALSE;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007413 if (eap->cmdidx != CMD_yank) // position cursor for undo
Bram Moolenaar071d4272004-06-13 20:20:40 +00007414 {
7415 setpcmark();
7416 curwin->w_cursor.lnum = eap->line1;
7417 beginline(BL_SOL | BL_FIX);
7418 }
7419
Bram Moolenaard07c6e12013-11-21 14:21:40 +01007420 if (VIsual_active)
7421 end_visual_mode();
Bram Moolenaard07c6e12013-11-21 14:21:40 +01007422
Bram Moolenaar071d4272004-06-13 20:20:40 +00007423 switch (eap->cmdidx)
7424 {
7425 case CMD_delete:
7426 oa.op_type = OP_DELETE;
7427 op_delete(&oa);
7428 break;
7429
7430 case CMD_yank:
7431 oa.op_type = OP_YANK;
7432 (void)op_yank(&oa, FALSE, TRUE);
7433 break;
7434
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007435 default: // CMD_rshift or CMD_lshift
Bram Moolenaar6e707362013-06-14 19:15:58 +02007436 if (
Bram Moolenaar071d4272004-06-13 20:20:40 +00007437#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6e707362013-06-14 19:15:58 +02007438 (eap->cmdidx == CMD_rshift) ^ curwin->w_p_rl
7439#else
7440 eap->cmdidx == CMD_rshift
Bram Moolenaar071d4272004-06-13 20:20:40 +00007441#endif
Bram Moolenaar6e707362013-06-14 19:15:58 +02007442 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00007443 oa.op_type = OP_RSHIFT;
7444 else
7445 oa.op_type = OP_LSHIFT;
7446 op_shift(&oa, FALSE, eap->amount);
7447 break;
7448 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007449 virtual_op = MAYBE;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007450 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007451}
7452
7453/*
7454 * ":put".
7455 */
7456 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007457ex_put(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007458{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007459 // ":0put" works like ":1put!".
Bram Moolenaar071d4272004-06-13 20:20:40 +00007460 if (eap->line2 == 0)
7461 {
7462 eap->line2 = 1;
7463 eap->forceit = TRUE;
7464 }
7465 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaarc3516f72020-09-08 22:45:35 +02007466 do_put(eap->regname, NULL, eap->forceit ? BACKWARD : FORWARD, 1L,
Bram Moolenaardf177f62005-02-22 08:39:57 +00007467 PUT_LINE|PUT_CURSLINE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007468}
7469
7470/*
7471 * Handle ":copy" and ":move".
7472 */
7473 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007474ex_copymove(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007475{
7476 long n;
7477
Bram Moolenaar491799b2020-08-01 19:23:43 +02007478#ifdef FEAT_EVAL
Bram Moolenaarf5a48012020-08-01 17:00:03 +02007479 if (not_in_vim9(eap) == FAIL)
7480 return;
Bram Moolenaar491799b2020-08-01 19:23:43 +02007481#endif
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02007482 n = get_address(eap, &eap->arg, eap->addr_type, FALSE, FALSE, FALSE, 1);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007483 if (eap->arg == NULL) // error detected
Bram Moolenaar071d4272004-06-13 20:20:40 +00007484 {
7485 eap->nextcmd = NULL;
7486 return;
7487 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00007488 get_flags(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007489
7490 /*
7491 * move or copy lines from 'eap->line1'-'eap->line2' to below line 'n'
7492 */
7493 if (n == MAXLNUM || n < 0 || n > curbuf->b_ml.ml_line_count)
7494 {
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02007495 emsg(_(e_invrange));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007496 return;
7497 }
7498
7499 if (eap->cmdidx == CMD_move)
7500 {
7501 if (do_move(eap->line1, eap->line2, n) == FAIL)
7502 return;
7503 }
7504 else
7505 ex_copy(eap->line1, eap->line2, n);
7506 u_clearline();
7507 beginline(BL_SOL | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007508 ex_may_print(eap);
7509}
7510
7511/*
7512 * Print the current line if flags were given to the Ex command.
7513 */
Bram Moolenaarfd3fe982014-04-01 17:49:44 +02007514 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007515ex_may_print(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007516{
7517 if (eap->flags != 0)
7518 {
7519 print_line(curwin->w_cursor.lnum, (eap->flags & EXFLAG_NR),
7520 (eap->flags & EXFLAG_LIST));
7521 ex_no_reprint = TRUE;
7522 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007523}
7524
7525/*
7526 * ":smagic" and ":snomagic".
7527 */
7528 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007529ex_submagic(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007530{
Bram Moolenaarf4e20992020-12-21 19:59:08 +01007531 magic_T saved = magic_overruled;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007532
Bram Moolenaarf4e20992020-12-21 19:59:08 +01007533 magic_overruled = eap->cmdidx == CMD_smagic ? MAGIC_ON : MAGIC_OFF;
Bram Moolenaar66e00142020-08-12 21:58:12 +02007534 ex_substitute(eap);
Bram Moolenaarf4e20992020-12-21 19:59:08 +01007535 magic_overruled = saved;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007536}
7537
7538/*
7539 * ":join".
7540 */
7541 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007542ex_join(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007543{
7544 curwin->w_cursor.lnum = eap->line1;
7545 if (eap->line1 == eap->line2)
7546 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007547 if (eap->addr_count >= 2) // :2,2join does nothing
Bram Moolenaar071d4272004-06-13 20:20:40 +00007548 return;
7549 if (eap->line2 == curbuf->b_ml.ml_line_count)
7550 {
7551 beep_flush();
7552 return;
7553 }
7554 ++eap->line2;
7555 }
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02007556 (void)do_join(eap->line2 - eap->line1 + 1, !eap->forceit, TRUE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007557 beginline(BL_WHITE | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007558 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007559}
7560
7561/*
7562 * ":[addr]@r" or ":[addr]*r": execute register
7563 */
7564 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007565ex_at(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007566{
7567 int c;
Bram Moolenaar60462872009-11-03 11:40:19 +00007568 int prev_len = typebuf.tb_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007569
7570 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaar4930a762016-09-11 14:39:53 +02007571 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007572
7573#ifdef USE_ON_FLY_SCROLL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007574 dont_scroll = TRUE; // disallow scrolling here
Bram Moolenaar071d4272004-06-13 20:20:40 +00007575#endif
7576
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007577 // get the register name. No name means to use the previous one
Bram Moolenaar071d4272004-06-13 20:20:40 +00007578 c = *eap->arg;
7579 if (c == NUL || (c == '*' && *eap->cmd == '*'))
7580 c = '@';
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007581 // Put the register in the typeahead buffer with the "silent" flag.
Bram Moolenaard333d1e2006-11-07 17:43:47 +00007582 if (do_execreg(c, TRUE, vim_strchr(p_cpo, CPO_EXECBUF) != NULL, TRUE)
7583 == FAIL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007584 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007585 beep_flush();
Bram Moolenaardf177f62005-02-22 08:39:57 +00007586 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007587 else
7588 {
7589 int save_efr = exec_from_reg;
7590
7591 exec_from_reg = TRUE;
7592
7593 /*
7594 * Execute from the typeahead buffer.
Bram Moolenaar60462872009-11-03 11:40:19 +00007595 * Continue until the stuff buffer is empty and all added characters
7596 * have been consumed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007597 */
Bram Moolenaar60462872009-11-03 11:40:19 +00007598 while (!stuff_empty() || typebuf.tb_len > prev_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007599 (void)do_cmdline(NULL, getexline, NULL, DOCMD_NOWAIT|DOCMD_VERBOSE);
7600
7601 exec_from_reg = save_efr;
7602 }
7603}
7604
7605/*
7606 * ":!".
7607 */
7608 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007609ex_bang(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007610{
7611 do_bang(eap->addr_count, eap, eap->forceit, TRUE, TRUE);
7612}
7613
7614/*
7615 * ":undo".
7616 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007617 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +01007618ex_undo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007619{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007620 if (eap->addr_count == 1) // :undo 123
Bram Moolenaar730cde92010-06-27 05:18:54 +02007621 undo_time(eap->line2, FALSE, FALSE, TRUE);
Bram Moolenaard3667a22006-03-16 21:35:52 +00007622 else
7623 u_undo(1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007624}
7625
Bram Moolenaar55debbe2010-05-23 23:34:36 +02007626#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar6df6f472010-07-18 18:04:50 +02007627 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007628ex_wundo(exarg_T *eap)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02007629{
7630 char_u hash[UNDO_HASH_SIZE];
7631
7632 u_compute_hash(hash);
7633 u_write_undo(eap->arg, eap->forceit, curbuf, hash);
7634}
7635
Bram Moolenaar6df6f472010-07-18 18:04:50 +02007636 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007637ex_rundo(exarg_T *eap)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02007638{
7639 char_u hash[UNDO_HASH_SIZE];
7640
7641 u_compute_hash(hash);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02007642 u_read_undo(eap->arg, hash, NULL);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02007643}
7644#endif
7645
Bram Moolenaar071d4272004-06-13 20:20:40 +00007646/*
7647 * ":redo".
7648 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007649 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007650ex_redo(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007651{
7652 u_redo(1);
7653}
7654
7655/*
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007656 * ":earlier" and ":later".
7657 */
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007658 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007659ex_later(exarg_T *eap)
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007660{
7661 long count = 0;
7662 int sec = FALSE;
Bram Moolenaar730cde92010-06-27 05:18:54 +02007663 int file = FALSE;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007664 char_u *p = eap->arg;
7665
7666 if (*p == NUL)
7667 count = 1;
7668 else if (isdigit(*p))
7669 {
7670 count = getdigits(&p);
7671 switch (*p)
7672 {
7673 case 's': ++p; sec = TRUE; break;
7674 case 'm': ++p; sec = TRUE; count *= 60; break;
7675 case 'h': ++p; sec = TRUE; count *= 60 * 60; break;
Bram Moolenaar730cde92010-06-27 05:18:54 +02007676 case 'd': ++p; sec = TRUE; count *= 24 * 60 * 60; break;
7677 case 'f': ++p; file = TRUE; break;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007678 }
7679 }
7680
7681 if (*p != NUL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007682 semsg(_(e_invarg2), eap->arg);
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007683 else
Bram Moolenaar730cde92010-06-27 05:18:54 +02007684 undo_time(eap->cmdidx == CMD_earlier ? -count : count,
7685 sec, file, FALSE);
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007686}
7687
7688/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007689 * ":redir": start/stop redirection.
7690 */
7691 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007692ex_redir(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007693{
7694 char *mode;
7695 char_u *fname;
Bram Moolenaarca472992005-01-21 11:46:23 +00007696 char_u *arg = eap->arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007697
Bram Moolenaarba768492016-07-08 15:32:54 +02007698#ifdef FEAT_EVAL
Bram Moolenaar79815f12016-07-09 17:07:29 +02007699 if (redir_execute)
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02007700 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007701 emsg(_("E930: Cannot use :redir inside execute()"));
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02007702 return;
7703 }
Bram Moolenaarba768492016-07-08 15:32:54 +02007704#endif
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02007705
Bram Moolenaar071d4272004-06-13 20:20:40 +00007706 if (STRICMP(eap->arg, "END") == 0)
7707 close_redir();
7708 else
7709 {
Bram Moolenaarca472992005-01-21 11:46:23 +00007710 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00007711 {
Bram Moolenaarca472992005-01-21 11:46:23 +00007712 ++arg;
7713 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00007714 {
Bram Moolenaarca472992005-01-21 11:46:23 +00007715 ++arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007716 mode = "a";
7717 }
7718 else
7719 mode = "w";
Bram Moolenaarca472992005-01-21 11:46:23 +00007720 arg = skipwhite(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007721
7722 close_redir();
7723
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007724 // Expand environment variables and "~/".
Bram Moolenaarca472992005-01-21 11:46:23 +00007725 fname = expand_env_save(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007726 if (fname == NULL)
7727 return;
7728#ifdef FEAT_BROWSE
Bram Moolenaare1004402020-10-24 20:49:43 +02007729 if (cmdmod.cmod_flags & CMOD_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007730 {
7731 char_u *browseFile;
7732
Bram Moolenaar7171abe2004-10-11 10:06:20 +00007733 browseFile = do_browse(BROWSE_SAVE,
7734 (char_u *)_("Save Redirection"),
Bram Moolenaarc36651b2018-04-29 12:22:56 +02007735 fname, NULL, NULL,
7736 (char_u *)_(BROWSE_FILTER_ALL_FILES), curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007737 if (browseFile == NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007738 return; // operation cancelled
Bram Moolenaar071d4272004-06-13 20:20:40 +00007739 vim_free(fname);
7740 fname = browseFile;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007741 eap->forceit = TRUE; // since dialog already asked
Bram Moolenaar071d4272004-06-13 20:20:40 +00007742 }
7743#endif
7744
7745 redir_fd = open_exfile(fname, eap->forceit, mode);
7746 vim_free(fname);
7747 }
7748#ifdef FEAT_EVAL
Bram Moolenaarca472992005-01-21 11:46:23 +00007749 else if (*arg == '@')
Bram Moolenaar071d4272004-06-13 20:20:40 +00007750 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007751 // redirect to a register a-z (resp. A-Z for appending)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007752 close_redir();
Bram Moolenaarca472992005-01-21 11:46:23 +00007753 ++arg;
7754 if (ASCII_ISALPHA(*arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007755# ifdef FEAT_CLIPBOARD
Bram Moolenaarca472992005-01-21 11:46:23 +00007756 || *arg == '*'
Bram Moolenaar9a51c6e2006-11-14 19:25:02 +00007757 || *arg == '+'
Bram Moolenaar071d4272004-06-13 20:20:40 +00007758# endif
Bram Moolenaarca472992005-01-21 11:46:23 +00007759 || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00007760 {
Bram Moolenaarca472992005-01-21 11:46:23 +00007761 redir_reg = *arg++;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007762 if (*arg == '>' && arg[1] == '>') // append
Bram Moolenaard9d30582005-05-18 22:10:28 +00007763 arg += 2;
Bram Moolenaarc188b882007-10-19 14:20:54 +00007764 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007765 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007766 // Can use both "@a" and "@a>".
Bram Moolenaar2c29bee2005-06-01 21:46:07 +00007767 if (*arg == '>')
7768 arg++;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007769 // Make register empty when not using @A-@Z and the
7770 // command is valid.
Bram Moolenaarc188b882007-10-19 14:20:54 +00007771 if (*arg == NUL && !isupper(redir_reg))
7772 write_reg_contents(redir_reg, (char_u *)"", -1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007773 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00007774 }
7775 if (*arg != NUL)
7776 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00007777 redir_reg = 0;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007778 semsg(_(e_invarg2), eap->arg);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007779 }
7780 }
7781 else if (*arg == '=' && arg[1] == '>')
7782 {
7783 int append;
7784
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007785 // redirect to a variable
Bram Moolenaardf177f62005-02-22 08:39:57 +00007786 close_redir();
7787 arg += 2;
7788
7789 if (*arg == '>')
7790 {
7791 ++arg;
7792 append = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007793 }
7794 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00007795 append = FALSE;
7796
7797 if (var_redir_start(skipwhite(arg), append) == OK)
7798 redir_vname = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007799 }
7800#endif
7801
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007802 // TODO: redirect to a buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00007803
Bram Moolenaar071d4272004-06-13 20:20:40 +00007804 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007805 semsg(_(e_invarg2), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007806 }
Bram Moolenaar29b2d262006-09-10 19:07:28 +00007807
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007808 // Make sure redirection is not off. Can happen for cmdline completion
7809 // that indirectly invokes a command to catch its output.
Bram Moolenaar29b2d262006-09-10 19:07:28 +00007810 if (redir_fd != NULL
7811#ifdef FEAT_EVAL
7812 || redir_reg || redir_vname
7813#endif
7814 )
7815 redir_off = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007816}
7817
7818/*
7819 * ":redraw": force redraw
7820 */
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01007821 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007822ex_redraw(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007823{
7824 int r = RedrawingDisabled;
7825 int p = p_lz;
7826
7827 RedrawingDisabled = 0;
7828 p_lz = FALSE;
Bram Moolenaarabc39ab2017-03-01 18:04:05 +01007829 validate_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007830 update_topline();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007831 update_screen(eap->forceit ? CLEAR : VIsual_active ? INVERTED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007832#ifdef FEAT_TITLE
7833 if (need_maketitle)
7834 maketitle();
7835#endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007836#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
7837# ifdef VIMDLL
7838 if (!gui.in_use)
7839# endif
7840 resize_console_buf();
Bram Moolenaar78d21da2019-02-17 15:00:52 +01007841#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007842 RedrawingDisabled = r;
7843 p_lz = p;
7844
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007845 // Reset msg_didout, so that a message that's there is overwritten.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007846 msg_didout = FALSE;
7847 msg_col = 0;
7848
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007849 // No need to wait after an intentional redraw.
Bram Moolenaar56667a52013-07-17 11:54:28 +02007850 need_wait_return = FALSE;
7851
Bram Moolenaar071d4272004-06-13 20:20:40 +00007852 out_flush();
7853}
7854
7855/*
7856 * ":redrawstatus": force redraw of status line(s)
7857 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007858 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007859ex_redrawstatus(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007860{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007861 int r = RedrawingDisabled;
7862 int p = p_lz;
7863
7864 RedrawingDisabled = 0;
7865 p_lz = FALSE;
7866 if (eap->forceit)
7867 status_redraw_all();
7868 else
7869 status_redraw_curbuf();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007870 update_screen(VIsual_active ? INVERTED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007871 RedrawingDisabled = r;
7872 p_lz = p;
7873 out_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007874}
7875
Bram Moolenaare12bab32019-01-08 22:02:56 +01007876/*
7877 * ":redrawtabline": force redraw of the tabline
7878 */
7879 static void
7880ex_redrawtabline(exarg_T *eap UNUSED)
7881{
7882 int r = RedrawingDisabled;
7883 int p = p_lz;
7884
7885 RedrawingDisabled = 0;
7886 p_lz = FALSE;
7887
7888 draw_tabline();
7889
7890 RedrawingDisabled = r;
7891 p_lz = p;
7892 out_flush();
7893}
7894
Bram Moolenaar071d4272004-06-13 20:20:40 +00007895 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007896close_redir(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007897{
7898 if (redir_fd != NULL)
7899 {
7900 fclose(redir_fd);
7901 redir_fd = NULL;
7902 }
7903#ifdef FEAT_EVAL
7904 redir_reg = 0;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007905 if (redir_vname)
7906 {
7907 var_redir_stop();
7908 redir_vname = 0;
7909 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007910#endif
7911}
7912
Bram Moolenaarf96ae0b2019-07-28 15:21:55 +02007913#if (defined(FEAT_SESSION) || defined(FEAT_EVAL)) || defined(PROTO)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007914 int
Bram Moolenaarbd67aac2019-09-21 23:09:04 +02007915vim_mkdir_emsg(char_u *name, int prot UNUSED)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007916{
7917 if (vim_mkdir(name, prot) != 0)
7918 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007919 semsg(_("E739: Cannot create directory: %s"), name);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007920 return FAIL;
7921 }
7922 return OK;
7923}
7924#endif
7925
Bram Moolenaar071d4272004-06-13 20:20:40 +00007926/*
7927 * Open a file for writing for an Ex command, with some checks.
7928 * Return file descriptor, or NULL on failure.
7929 */
7930 FILE *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007931open_exfile(
7932 char_u *fname,
7933 int forceit,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007934 char *mode) // "w" for create new file or "a" for append
Bram Moolenaar071d4272004-06-13 20:20:40 +00007935{
7936 FILE *fd;
7937
7938#ifdef UNIX
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007939 // with Unix it is possible to open a directory
Bram Moolenaar071d4272004-06-13 20:20:40 +00007940 if (mch_isdir(fname))
7941 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007942 semsg(_(e_isadir2), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007943 return NULL;
7944 }
7945#endif
7946 if (!forceit && *mode != 'a' && vim_fexists(fname))
7947 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007948 semsg(_("E189: \"%s\" exists (add ! to override)"), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007949 return NULL;
7950 }
7951
7952 if ((fd = mch_fopen((char *)fname, mode)) == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007953 semsg(_("E190: Cannot open \"%s\" for writing"), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007954
7955 return fd;
7956}
7957
7958/*
7959 * ":mark" and ":k".
7960 */
7961 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007962ex_mark(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007963{
7964 pos_T pos;
7965
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007966 if (*eap->arg == NUL) // No argument?
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007967 emsg(_(e_argreq));
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007968 else if (eap->arg[1] != NUL) // more than one character?
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +02007969 semsg(_(e_trailing_arg), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007970 else
7971 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007972 pos = curwin->w_cursor; // save curwin->w_cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00007973 curwin->w_cursor.lnum = eap->line2;
7974 beginline(BL_WHITE | BL_FIX);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007975 if (setmark(*eap->arg) == FAIL) // set mark
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007976 emsg(_("E191: Argument must be a letter or forward/backward quote"));
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007977 curwin->w_cursor = pos; // restore curwin->w_cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00007978 }
7979}
7980
7981/*
7982 * Update w_topline, w_leftcol and the cursor position.
7983 */
7984 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007985update_topline_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007986{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007987 check_cursor(); // put cursor on valid line
Bram Moolenaar071d4272004-06-13 20:20:40 +00007988 update_topline();
7989 if (!curwin->w_p_wrap)
7990 validate_cursor();
7991 update_curswant();
7992}
7993
Bram Moolenaar071d4272004-06-13 20:20:40 +00007994/*
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007995 * Save the current State and go to Normal mode.
7996 * Return TRUE if the typeahead could be saved.
7997 */
7998 int
7999save_current_state(save_state_T *sst)
8000{
8001 sst->save_msg_scroll = msg_scroll;
8002 sst->save_restart_edit = restart_edit;
8003 sst->save_msg_didout = msg_didout;
8004 sst->save_State = State;
8005 sst->save_insertmode = p_im;
8006 sst->save_finish_op = finish_op;
8007 sst->save_opcount = opcount;
Bram Moolenaarcce713d2019-03-04 11:40:12 +01008008 sst->save_reg_executing = reg_executing;
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008009
Bram Moolenaar4324d872020-12-01 20:12:24 +01008010 msg_scroll = FALSE; // no msg scrolling in Normal mode
8011 restart_edit = 0; // don't go to Insert mode
8012 p_im = FALSE; // don't use 'insertmode'
Bram Moolenaar9b8d6222020-12-28 18:26:00 +01008013
Bram Moolenaara452b802020-12-01 21:47:59 +01008014 sst->save_script_version = current_sctx.sc_version;
Bram Moolenaar4324d872020-12-01 20:12:24 +01008015 current_sctx.sc_version = 1; // not in Vim9 script
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008016
8017 /*
8018 * Save the current typeahead. This is required to allow using ":normal"
8019 * from an event handler and makes sure we don't hang when the argument
8020 * ends with half a command.
8021 */
8022 save_typeahead(&sst->tabuf);
8023 return sst->tabuf.typebuf_valid;
8024}
8025
8026 void
8027restore_current_state(save_state_T *sst)
8028{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008029 // Restore the previous typeahead.
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008030 restore_typeahead(&sst->tabuf);
8031
8032 msg_scroll = sst->save_msg_scroll;
8033 restart_edit = sst->save_restart_edit;
8034 p_im = sst->save_insertmode;
8035 finish_op = sst->save_finish_op;
8036 opcount = sst->save_opcount;
Bram Moolenaarcce713d2019-03-04 11:40:12 +01008037 reg_executing = sst->save_reg_executing;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008038 msg_didout |= sst->save_msg_didout; // don't reset msg_didout now
Bram Moolenaar4324d872020-12-01 20:12:24 +01008039 current_sctx.sc_version = sst->save_script_version;
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008040
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008041 // Restore the state (needed when called from a function executed for
8042 // 'indentexpr'). Update the mouse and cursor, they may have changed.
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008043 State = sst->save_State;
8044#ifdef CURSOR_SHAPE
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008045 ui_cursor_shape(); // may show different cursor shape
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008046#endif
8047}
8048
8049/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008050 * ":normal[!] {commands}": Execute normal mode commands.
8051 */
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01008052 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008053ex_normal(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008054{
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008055 save_state_T save_state;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008056 char_u *arg = NULL;
8057 int l;
8058 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008059
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008060 if (ex_normal_lock > 0)
8061 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008062 emsg(_(e_secure));
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008063 return;
8064 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008065 if (ex_normal_busy >= p_mmd)
8066 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008067 emsg(_("E192: Recursive use of :normal too deep"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008068 return;
8069 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008070
Bram Moolenaar071d4272004-06-13 20:20:40 +00008071 /*
8072 * vgetc() expects a CSI and K_SPECIAL to have been escaped. Don't do
8073 * this for the K_SPECIAL leading byte, otherwise special keys will not
8074 * work.
8075 */
8076 if (has_mbyte)
8077 {
8078 int len = 0;
8079
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008080 // Count the number of characters to be escaped.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008081 for (p = eap->arg; *p != NUL; ++p)
8082 {
Bram Moolenaar13505972019-01-24 15:04:48 +01008083#ifdef FEAT_GUI
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008084 if (*p == CSI) // leadbyte CSI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008085 len += 2;
Bram Moolenaar13505972019-01-24 15:04:48 +01008086#endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008087 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008088 if (*++p == K_SPECIAL // trailbyte K_SPECIAL or CSI
Bram Moolenaar13505972019-01-24 15:04:48 +01008089#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008090 || *p == CSI
Bram Moolenaar13505972019-01-24 15:04:48 +01008091#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008092 )
8093 len += 2;
8094 }
8095 if (len > 0)
8096 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02008097 arg = alloc(STRLEN(eap->arg) + len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008098 if (arg != NULL)
8099 {
8100 len = 0;
8101 for (p = eap->arg; *p != NUL; ++p)
8102 {
8103 arg[len++] = *p;
Bram Moolenaar13505972019-01-24 15:04:48 +01008104#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008105 if (*p == CSI)
8106 {
8107 arg[len++] = KS_EXTRA;
8108 arg[len++] = (int)KE_CSI;
8109 }
Bram Moolenaar13505972019-01-24 15:04:48 +01008110#endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008111 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008112 {
8113 arg[len++] = *++p;
8114 if (*p == K_SPECIAL)
8115 {
8116 arg[len++] = KS_SPECIAL;
8117 arg[len++] = KE_FILLER;
8118 }
Bram Moolenaar13505972019-01-24 15:04:48 +01008119#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008120 else if (*p == CSI)
8121 {
8122 arg[len++] = KS_EXTRA;
8123 arg[len++] = (int)KE_CSI;
8124 }
Bram Moolenaar13505972019-01-24 15:04:48 +01008125#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008126 }
8127 arg[len] = NUL;
8128 }
8129 }
8130 }
8131 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008132
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008133 ++ex_normal_busy;
8134 if (save_current_state(&save_state))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008135 {
8136 /*
8137 * Repeat the :normal command for each line in the range. When no
8138 * range given, execute it just once, without positioning the cursor
8139 * first.
8140 */
8141 do
8142 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008143 if (eap->addr_count != 0)
8144 {
8145 curwin->w_cursor.lnum = eap->line1++;
8146 curwin->w_cursor.col = 0;
Bram Moolenaard5d37532017-03-27 23:02:07 +02008147 check_cursor_moved(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008148 }
8149
Bram Moolenaar13505972019-01-24 15:04:48 +01008150 exec_normal_cmd(arg != NULL
8151 ? arg
8152 : eap->arg, eap->forceit ? REMAP_NONE : REMAP_YES, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008153 }
8154 while (eap->addr_count > 0 && eap->line1 <= eap->line2 && !got_int);
8155 }
8156
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008157 // Might not return to the main loop when in an event handler.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008158 update_topline_cursor();
8159
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008160 restore_current_state(&save_state);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008161 --ex_normal_busy;
Bram Moolenaareda73602014-11-05 09:53:23 +01008162 setmouse();
Bram Moolenaareda73602014-11-05 09:53:23 +01008163#ifdef CURSOR_SHAPE
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008164 ui_cursor_shape(); // may show different cursor shape
Bram Moolenaareda73602014-11-05 09:53:23 +01008165#endif
8166
Bram Moolenaar071d4272004-06-13 20:20:40 +00008167 vim_free(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008168}
8169
8170/*
Bram Moolenaar64969662005-12-14 21:59:55 +00008171 * ":startinsert", ":startreplace" and ":startgreplace"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008172 */
8173 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008174ex_startinsert(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008175{
Bram Moolenaarfd371682005-01-14 21:42:54 +00008176 if (eap->forceit)
8177 {
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02008178 // cursor line can be zero on startup
Bram Moolenaar09ca9322017-09-26 17:40:45 +02008179 if (!curwin->w_cursor.lnum)
8180 curwin->w_cursor.lnum = 1;
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02008181 set_cursor_for_append_to_line();
Bram Moolenaarfd371682005-01-14 21:42:54 +00008182 }
Bram Moolenaareb58a242020-04-19 18:13:19 +02008183#ifdef FEAT_TERMINAL
8184 // Ignore this when running in an active terminal.
8185 if (term_job_running(curbuf->b_term))
8186 return;
8187#endif
Bram Moolenaarfd371682005-01-14 21:42:54 +00008188
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02008189 // Ignore the command when already in Insert mode. Inserting an
8190 // expression register that invokes a function can do this.
Bram Moolenaara40c5002005-01-09 21:16:21 +00008191 if (State & INSERT)
8192 return;
8193
Bram Moolenaar64969662005-12-14 21:59:55 +00008194 if (eap->cmdidx == CMD_startinsert)
8195 restart_edit = 'a';
8196 else if (eap->cmdidx == CMD_startreplace)
8197 restart_edit = 'R';
Bram Moolenaar071d4272004-06-13 20:20:40 +00008198 else
Bram Moolenaar64969662005-12-14 21:59:55 +00008199 restart_edit = 'V';
8200
8201 if (!eap->forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008202 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00008203 if (eap->cmdidx == CMD_startinsert)
8204 restart_edit = 'i';
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02008205 curwin->w_curswant = 0; // avoid MAXCOL
Bram Moolenaar071d4272004-06-13 20:20:40 +00008206 }
Bram Moolenaar957cf672020-11-12 14:21:06 +01008207
8208 if (VIsual_active)
8209 showmode();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008210}
8211
8212/*
8213 * ":stopinsert"
8214 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008215 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008216ex_stopinsert(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008217{
8218 restart_edit = 0;
8219 stop_insert_mode = TRUE;
Bram Moolenaarfd773e92016-04-02 19:39:16 +02008220 clearmode();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008221}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008222
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008223/*
8224 * Execute normal mode command "cmd".
8225 * "remap" can be REMAP_NONE or REMAP_YES.
8226 */
8227 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008228exec_normal_cmd(char_u *cmd, int remap, int silent)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008229{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008230 // Stuff the argument into the typeahead buffer.
Bram Moolenaar25281632016-01-21 23:32:32 +01008231 ins_typebuf(cmd, remap, 0, TRUE, silent);
Bram Moolenaar586c70c2018-10-02 16:23:58 +02008232 exec_normal(FALSE, FALSE, FALSE);
Bram Moolenaar25281632016-01-21 23:32:32 +01008233}
Bram Moolenaar25281632016-01-21 23:32:32 +01008234
Bram Moolenaar25281632016-01-21 23:32:32 +01008235/*
8236 * Execute normal_cmd() until there is no typeahead left.
Bram Moolenaar586c70c2018-10-02 16:23:58 +02008237 * When "use_vpeekc" is TRUE use vpeekc() to check for available chars.
Bram Moolenaar25281632016-01-21 23:32:32 +01008238 */
8239 void
Bram Moolenaar586c70c2018-10-02 16:23:58 +02008240exec_normal(int was_typed, int use_vpeekc, int may_use_terminal_loop UNUSED)
Bram Moolenaar25281632016-01-21 23:32:32 +01008241{
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008242 oparg_T oa;
Bram Moolenaar905dd902019-04-07 14:21:47 +02008243 int c;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008244
Bram Moolenaar905dd902019-04-07 14:21:47 +02008245 // When calling vpeekc() from feedkeys() it will return Ctrl_C when there
8246 // is nothing to get, so also check for Ctrl_C.
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008247 clear_oparg(&oa);
8248 finish_op = FALSE;
Bram Moolenaar586c70c2018-10-02 16:23:58 +02008249 while ((!stuff_empty()
8250 || ((was_typed || !typebuf_typed()) && typebuf.tb_len > 0)
Bram Moolenaar905dd902019-04-07 14:21:47 +02008251 || (use_vpeekc && (c = vpeekc()) != NUL && c != Ctrl_C))
Bram Moolenaar586c70c2018-10-02 16:23:58 +02008252 && !got_int)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008253 {
8254 update_topline_cursor();
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02008255#ifdef FEAT_TERMINAL
Bram Moolenaar655a82a2018-05-08 22:01:07 +02008256 if (may_use_terminal_loop && term_use_loop()
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02008257 && oa.op_type == OP_NOP && oa.regname == NUL
8258 && !VIsual_active)
8259 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008260 // If terminal_loop() returns OK we got a key that is handled
8261 // in Normal model. With FAIL we first need to position the
8262 // cursor and the screen needs to be redrawn.
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02008263 if (terminal_loop(TRUE) == OK)
8264 normal_cmd(&oa, TRUE);
8265 }
8266 else
8267#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008268 // execute a Normal mode cmd
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02008269 normal_cmd(&oa, TRUE);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008270 }
8271}
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008272
Bram Moolenaar071d4272004-06-13 20:20:40 +00008273#ifdef FEAT_FIND_ID
8274 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008275ex_checkpath(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008276{
8277 find_pattern_in_path(NULL, 0, 0, FALSE, FALSE, CHECK_PATH, 1L,
8278 eap->forceit ? ACTION_SHOW_ALL : ACTION_SHOW,
8279 (linenr_T)1, (linenr_T)MAXLNUM);
8280}
8281
Bram Moolenaar4033c552017-09-16 20:54:51 +02008282#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008283/*
8284 * ":psearch"
8285 */
8286 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008287ex_psearch(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008288{
8289 g_do_tagpreview = p_pvh;
8290 ex_findpat(eap);
8291 g_do_tagpreview = 0;
8292}
8293#endif
8294
8295 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008296ex_findpat(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008297{
8298 int whole = TRUE;
8299 long n;
8300 char_u *p;
8301 int action;
8302
8303 switch (cmdnames[eap->cmdidx].cmd_name[2])
8304 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008305 case 'e': // ":psearch", ":isearch" and ":dsearch"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008306 if (cmdnames[eap->cmdidx].cmd_name[0] == 'p')
8307 action = ACTION_GOTO;
8308 else
8309 action = ACTION_SHOW;
8310 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008311 case 'i': // ":ilist" and ":dlist"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008312 action = ACTION_SHOW_ALL;
8313 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008314 case 'u': // ":ijump" and ":djump"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008315 action = ACTION_GOTO;
8316 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008317 default: // ":isplit" and ":dsplit"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008318 action = ACTION_SPLIT;
8319 break;
8320 }
8321
8322 n = 1;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008323 if (vim_isdigit(*eap->arg)) // get count
Bram Moolenaar071d4272004-06-13 20:20:40 +00008324 {
8325 n = getdigits(&eap->arg);
8326 eap->arg = skipwhite(eap->arg);
8327 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008328 if (*eap->arg == '/') // Match regexp, not just whole words
Bram Moolenaar071d4272004-06-13 20:20:40 +00008329 {
8330 whole = FALSE;
8331 ++eap->arg;
Bram Moolenaarf4e20992020-12-21 19:59:08 +01008332 p = skip_regexp(eap->arg, '/', magic_isset());
Bram Moolenaar071d4272004-06-13 20:20:40 +00008333 if (*p)
8334 {
8335 *p++ = NUL;
8336 p = skipwhite(p);
8337
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008338 // Check for trailing illegal characters
Bram Moolenaarfaac4102020-04-20 17:46:14 +02008339 if (!ends_excmd2(eap->arg, p))
Bram Moolenaar8930caa2020-07-23 16:37:03 +02008340 eap->errmsg = ex_errmsg(e_trailing_arg, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008341 else
8342 eap->nextcmd = check_nextcmd(p);
8343 }
8344 }
8345 if (!eap->skip)
8346 find_pattern_in_path(eap->arg, 0, (int)STRLEN(eap->arg),
8347 whole, !eap->forceit,
8348 *eap->cmd == 'd' ? FIND_DEFINE : FIND_ANY,
8349 n, action, eap->line1, eap->line2);
8350}
8351#endif
8352
Bram Moolenaar071d4272004-06-13 20:20:40 +00008353
Bram Moolenaar4033c552017-09-16 20:54:51 +02008354#ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00008355/*
8356 * ":ptag", ":ptselect", ":ptjump", ":ptnext", etc.
8357 */
8358 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008359ex_ptag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008360{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008361 g_do_tagpreview = p_pvh; // will be reset to 0 in ex_tag_cmd()
Bram Moolenaar071d4272004-06-13 20:20:40 +00008362 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
8363}
8364
8365/*
8366 * ":pedit"
8367 */
8368 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008369ex_pedit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008370{
8371 win_T *curwin_save = curwin;
8372
Bram Moolenaar3a2505c2020-03-09 16:40:41 +01008373 if (ERROR_IF_ANY_POPUP_WINDOW)
8374 return;
8375
Bram Moolenaar026587b2019-08-17 15:08:00 +02008376 // Open the preview window or popup and make it the current window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008377 g_do_tagpreview = p_pvh;
Bram Moolenaar576a4a62019-08-18 15:25:17 +02008378 prepare_tagpreview(TRUE, TRUE, FALSE);
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02008379
Bram Moolenaar026587b2019-08-17 15:08:00 +02008380 // Edit the file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008381 do_exedit(eap, NULL);
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02008382
Bram Moolenaar071d4272004-06-13 20:20:40 +00008383 if (curwin != curwin_save && win_valid(curwin_save))
8384 {
Bram Moolenaar026587b2019-08-17 15:08:00 +02008385 // Return cursor to where we were
Bram Moolenaar071d4272004-06-13 20:20:40 +00008386 validate_cursor();
8387 redraw_later(VALID);
8388 win_enter(curwin_save, TRUE);
8389 }
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01008390# ifdef FEAT_PROP_POPUP
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02008391 else if (WIN_IS_POPUP(curwin))
8392 {
8393 // can't keep focus in popup window
8394 win_enter(firstwin, TRUE);
8395 }
8396# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008397 g_do_tagpreview = 0;
8398}
Bram Moolenaar4033c552017-09-16 20:54:51 +02008399#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008400
8401/*
8402 * ":stag", ":stselect" and ":stjump".
8403 */
8404 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008405ex_stag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008406{
8407 postponed_split = -1;
Bram Moolenaare1004402020-10-24 20:49:43 +02008408 postponed_split_flags = cmdmod.cmod_split;
8409 postponed_split_tab = cmdmod.cmod_tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008410 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
8411 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00008412 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008413}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008414
8415/*
8416 * ":tag", ":tselect", ":tjump", ":tnext", etc.
8417 */
8418 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008419ex_tag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008420{
8421 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name);
8422}
8423
8424 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008425ex_tag_cmd(exarg_T *eap, char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008426{
8427 int cmd;
8428
8429 switch (name[1])
8430 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008431 case 'j': cmd = DT_JUMP; // ":tjump"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008432 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008433 case 's': cmd = DT_SELECT; // ":tselect"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008434 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008435 case 'p': cmd = DT_PREV; // ":tprevious"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008436 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008437 case 'N': cmd = DT_PREV; // ":tNext"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008438 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008439 case 'n': cmd = DT_NEXT; // ":tnext"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008440 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008441 case 'o': cmd = DT_POP; // ":pop"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008442 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008443 case 'f': // ":tfirst"
8444 case 'r': cmd = DT_FIRST; // ":trewind"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008445 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008446 case 'l': cmd = DT_LAST; // ":tlast"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008447 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008448 default: // ":tag"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008449#ifdef FEAT_CSCOPE
Bram Moolenaar7c94c262008-06-20 09:11:34 +00008450 if (p_cst && *eap->arg != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008451 {
Bram Moolenaard4db7712016-11-12 19:16:46 +01008452 ex_cstag(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008453 return;
8454 }
8455#endif
8456 cmd = DT_TAG;
8457 break;
8458 }
8459
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00008460 if (name[0] == 'l')
8461 {
8462#ifndef FEAT_QUICKFIX
8463 ex_ni(eap);
8464 return;
8465#else
8466 cmd = DT_LTAG;
8467#endif
8468 }
8469
Bram Moolenaar071d4272004-06-13 20:20:40 +00008470 do_tag(eap->arg, cmd, eap->addr_count > 0 ? (int)eap->line2 : 1,
8471 eap->forceit, TRUE);
8472}
8473
8474/*
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008475 * Check "str" for starting with a special cmdline variable.
8476 * If found return one of the SPEC_ values and set "*usedlen" to the length of
8477 * the variable. Otherwise return -1 and "*usedlen" is unchanged.
8478 */
8479 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008480find_cmdline_var(char_u *src, int *usedlen)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008481{
8482 int len;
8483 int i;
Bram Moolenaar8f0b2d42009-05-16 14:41:10 +00008484 static char *(spec_str[]) = {
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008485 "%",
8486#define SPEC_PERC 0
8487 "#",
Bram Moolenaar65f08472017-09-10 18:16:20 +02008488#define SPEC_HASH (SPEC_PERC + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008489 "<cword>", // cursor word
Bram Moolenaar65f08472017-09-10 18:16:20 +02008490#define SPEC_CWORD (SPEC_HASH + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008491 "<cWORD>", // cursor WORD
Bram Moolenaar65f08472017-09-10 18:16:20 +02008492#define SPEC_CCWORD (SPEC_CWORD + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008493 "<cexpr>", // expr under cursor
Bram Moolenaar65f08472017-09-10 18:16:20 +02008494#define SPEC_CEXPR (SPEC_CCWORD + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008495 "<cfile>", // cursor path name
Bram Moolenaar65f08472017-09-10 18:16:20 +02008496#define SPEC_CFILE (SPEC_CEXPR + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008497 "<sfile>", // ":so" file name
Bram Moolenaar65f08472017-09-10 18:16:20 +02008498#define SPEC_SFILE (SPEC_CFILE + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008499 "<slnum>", // ":so" file line number
Bram Moolenaar65f08472017-09-10 18:16:20 +02008500#define SPEC_SLNUM (SPEC_SFILE + 1)
Bram Moolenaara5d04232020-07-26 15:37:02 +02008501 "<stack>", // call stack
8502#define SPEC_STACK (SPEC_SLNUM + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008503 "<afile>", // autocommand file name
Bram Moolenaara5d04232020-07-26 15:37:02 +02008504#define SPEC_AFILE (SPEC_STACK + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008505 "<abuf>", // autocommand buffer number
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008506#define SPEC_ABUF (SPEC_AFILE + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008507 "<amatch>", // autocommand match name
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01008508#define SPEC_AMATCH (SPEC_ABUF + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008509 "<sflnum>", // script file line number
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008510#define SPEC_SFLNUM (SPEC_AMATCH + 1)
Bram Moolenaar90944302020-08-01 20:45:11 +02008511 "<SID>", // script ID: <SNR>123_
8512#define SPEC_SID (SPEC_SFLNUM + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008513#ifdef FEAT_CLIENTSERVER
8514 "<client>"
Bram Moolenaar90944302020-08-01 20:45:11 +02008515# define SPEC_CLIENT (SPEC_SID + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008516#endif
8517 };
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008518
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00008519 for (i = 0; i < (int)(sizeof(spec_str) / sizeof(char *)); ++i)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008520 {
8521 len = (int)STRLEN(spec_str[i]);
8522 if (STRNCMP(src, spec_str[i], len) == 0)
8523 {
8524 *usedlen = len;
8525 return i;
8526 }
8527 }
8528 return -1;
8529}
8530
8531/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008532 * Evaluate cmdline variables.
8533 *
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01008534 * change "%" to curbuf->b_ffname
8535 * "#" to curwin->w_alt_fnum
8536 * "%%" to curwin->w_alt_fnum in Vim9 script
8537 * "<cword>" to word under the cursor
8538 * "<cWORD>" to WORD under the cursor
8539 * "<cexpr>" to C-expression under the cursor
8540 * "<cfile>" to path name under the cursor
8541 * "<sfile>" to sourced file name
8542 * "<stack>" to call stack
8543 * "<slnum>" to sourced file line number
8544 * "<afile>" to file name for autocommand
8545 * "<abuf>" to buffer number for autocommand
8546 * "<amatch>" to matching name for autocommand
Bram Moolenaar071d4272004-06-13 20:20:40 +00008547 *
8548 * When an error is detected, "errormsg" is set to a non-NULL pointer (may be
8549 * "" for error without a message) and NULL is returned.
8550 * Returns an allocated string if a valid match was found.
8551 * Returns NULL if no match was found. "usedlen" then still contains the
8552 * number of characters to skip.
8553 */
8554 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008555eval_vars(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008556 char_u *src, // pointer into commandline
8557 char_u *srcstart, // beginning of valid memory for src
8558 int *usedlen, // characters after src that are used
8559 linenr_T *lnump, // line number for :e command, or NULL
8560 char **errormsg, // pointer to error message
8561 int *escaped) // return value has escaped white space (can
8562 // be NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008563{
8564 int i;
8565 char_u *s;
8566 char_u *result;
8567 char_u *resultbuf = NULL;
8568 int resultlen;
8569 buf_T *buf;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008570 int valid = VALID_HEAD + VALID_PATH; // assume valid result
Bram Moolenaar071d4272004-06-13 20:20:40 +00008571 int spec_idx;
Bram Moolenaarfd249462018-07-28 16:14:30 +02008572 int tilde_file = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008573 int skip_mod = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008574 char_u strbuf[30];
Bram Moolenaar071d4272004-06-13 20:20:40 +00008575
8576 *errormsg = NULL;
Bram Moolenaar63b92542007-03-27 14:57:09 +00008577 if (escaped != NULL)
8578 *escaped = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008579
8580 /*
8581 * Check if there is something to do.
8582 */
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008583 spec_idx = find_cmdline_var(src, usedlen);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008584 if (spec_idx < 0) // no match
Bram Moolenaar071d4272004-06-13 20:20:40 +00008585 {
8586 *usedlen = 1;
8587 return NULL;
8588 }
8589
8590 /*
8591 * Skip when preceded with a backslash "\%" and "\#".
8592 * Note: In "\\%" the % is also not recognized!
8593 */
8594 if (src > srcstart && src[-1] == '\\')
8595 {
8596 *usedlen = 0;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008597 STRMOVE(src - 1, src); // remove backslash
Bram Moolenaar071d4272004-06-13 20:20:40 +00008598 return NULL;
8599 }
8600
8601 /*
8602 * word or WORD under cursor
8603 */
Bram Moolenaar65f08472017-09-10 18:16:20 +02008604 if (spec_idx == SPEC_CWORD || spec_idx == SPEC_CCWORD
8605 || spec_idx == SPEC_CEXPR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008606 {
Bram Moolenaar65f08472017-09-10 18:16:20 +02008607 resultlen = find_ident_under_cursor(&result,
8608 spec_idx == SPEC_CWORD ? (FIND_IDENT | FIND_STRING)
8609 : spec_idx == SPEC_CEXPR ? (FIND_IDENT | FIND_STRING | FIND_EVAL)
8610 : FIND_STRING);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008611 if (resultlen == 0)
8612 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008613 *errormsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +00008614 return NULL;
8615 }
8616 }
8617
8618 /*
8619 * '#': Alternate file name
8620 * '%': Current file name
8621 * File name under the cursor
8622 * File name for autocommand
8623 * and following modifiers
8624 */
8625 else
8626 {
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01008627 int off = 0;
8628
Bram Moolenaar071d4272004-06-13 20:20:40 +00008629 switch (spec_idx)
8630 {
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01008631 case SPEC_PERC:
Bram Moolenaar39cb2da2020-12-27 17:35:18 +01008632#ifdef FEAT_EVAL
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01008633 if (!in_vim9script() || src[1] != '%')
Bram Moolenaar39cb2da2020-12-27 17:35:18 +01008634#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008635 {
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01008636 // '%': current file
8637 if (curbuf->b_fname == NULL)
8638 {
8639 result = (char_u *)"";
8640 valid = 0; // Must have ":p:h" to be valid
8641 }
8642 else
8643 {
8644 result = curbuf->b_fname;
8645 tilde_file = STRCMP(result, "~") == 0;
8646 }
8647 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008648 }
Bram Moolenaar39cb2da2020-12-27 17:35:18 +01008649#ifdef FEAT_EVAL
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01008650 // "%%" alternate file
8651 off = 1;
Bram Moolenaar39cb2da2020-12-27 17:35:18 +01008652#endif
Bram Moolenaar9618a252020-12-27 19:18:03 +01008653 // FALLTHROUGH
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008654 case SPEC_HASH: // '#' or "#99": alternate file
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01008655 if (off == 0 ? src[1] == '#' : src[2] == '%')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008656 {
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01008657 // "##" or "%%%": the argument list
Bram Moolenaar071d4272004-06-13 20:20:40 +00008658 result = arg_all();
8659 resultbuf = result;
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01008660 *usedlen = off + 2;
Bram Moolenaar63b92542007-03-27 14:57:09 +00008661 if (escaped != NULL)
8662 *escaped = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008663 skip_mod = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008664 break;
8665 }
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01008666 s = src + off + 1;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008667 if (*s == '<') // "#<99" uses v:oldfiles
Bram Moolenaard812df62008-11-09 12:46:09 +00008668 ++s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008669 i = (int)getdigits(&s);
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01008670 if (s == src + off + 2 && src[off + 1] == '-')
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008671 // just a minus sign, don't skip over it
Bram Moolenaarc312b8b2017-10-28 17:53:04 +02008672 s--;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008673 *usedlen = (int)(s - src); // length of what we expand
Bram Moolenaar071d4272004-06-13 20:20:40 +00008674
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01008675 if (src[off + 1] == '<' && i != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008676 {
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01008677 if (*usedlen < off + 2)
Bram Moolenaard812df62008-11-09 12:46:09 +00008678 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008679 // Should we give an error message for #<text?
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01008680 *usedlen = off + 1;
Bram Moolenaard812df62008-11-09 12:46:09 +00008681 return NULL;
8682 }
8683#ifdef FEAT_EVAL
8684 result = list_find_str(get_vim_var_list(VV_OLDFILES),
8685 (long)i);
8686 if (result == NULL)
8687 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008688 *errormsg = "";
Bram Moolenaard812df62008-11-09 12:46:09 +00008689 return NULL;
8690 }
8691#else
Bram Moolenaar8e481e82019-01-15 20:07:48 +01008692 *errormsg = _("E809: #< is not available without the +eval feature");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008693 return NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +00008694#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008695 }
8696 else
Bram Moolenaard812df62008-11-09 12:46:09 +00008697 {
Bram Moolenaar4389f9c2020-12-27 16:55:11 +01008698 if (i == 0 && src[off + 1] == '<' && *usedlen > off + 1)
8699 *usedlen = off + 1;
Bram Moolenaard812df62008-11-09 12:46:09 +00008700 buf = buflist_findnr(i);
8701 if (buf == NULL)
8702 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008703 *errormsg = _("E194: No alternate file name to substitute for '#'");
Bram Moolenaard812df62008-11-09 12:46:09 +00008704 return NULL;
8705 }
8706 if (lnump != NULL)
8707 *lnump = ECMD_LAST;
8708 if (buf->b_fname == NULL)
8709 {
8710 result = (char_u *)"";
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008711 valid = 0; // Must have ":p:h" to be valid
Bram Moolenaard812df62008-11-09 12:46:09 +00008712 }
8713 else
Bram Moolenaar00136dc2018-07-25 21:19:13 +02008714 {
Bram Moolenaard812df62008-11-09 12:46:09 +00008715 result = buf->b_fname;
Bram Moolenaar00136dc2018-07-25 21:19:13 +02008716 tilde_file = STRCMP(result, "~") == 0;
8717 }
Bram Moolenaard812df62008-11-09 12:46:09 +00008718 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008719 break;
8720
8721#ifdef FEAT_SEARCHPATH
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008722 case SPEC_CFILE: // file name under cursor
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008723 result = file_name_at_cursor(FNAME_MESS|FNAME_HYP, 1L, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008724 if (result == NULL)
8725 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008726 *errormsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +00008727 return NULL;
8728 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008729 resultbuf = result; // remember allocated string
Bram Moolenaar071d4272004-06-13 20:20:40 +00008730 break;
8731#endif
8732
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008733 case SPEC_AFILE: // file name for autocommand
Bram Moolenaar071d4272004-06-13 20:20:40 +00008734 result = autocmd_fname;
Bram Moolenaarf6dad432008-09-18 19:29:58 +00008735 if (result != NULL && !autocmd_fname_full)
8736 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008737 // Still need to turn the fname into a full path. It is
8738 // postponed to avoid a delay when <afile> is not used.
Bram Moolenaarf6dad432008-09-18 19:29:58 +00008739 autocmd_fname_full = TRUE;
8740 result = FullName_save(autocmd_fname, FALSE);
8741 vim_free(autocmd_fname);
8742 autocmd_fname = result;
8743 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008744 if (result == NULL)
8745 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008746 *errormsg = _("E495: no autocommand file name to substitute for \"<afile>\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008747 return NULL;
8748 }
Bram Moolenaara0174af2008-01-02 20:08:25 +00008749 result = shorten_fname1(result);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008750 break;
8751
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008752 case SPEC_ABUF: // buffer number for autocommand
Bram Moolenaar071d4272004-06-13 20:20:40 +00008753 if (autocmd_bufnr <= 0)
8754 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008755 *errormsg = _("E496: no autocommand buffer number to substitute for \"<abuf>\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008756 return NULL;
8757 }
8758 sprintf((char *)strbuf, "%d", autocmd_bufnr);
8759 result = strbuf;
8760 break;
8761
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008762 case SPEC_AMATCH: // match name for autocommand
Bram Moolenaar071d4272004-06-13 20:20:40 +00008763 result = autocmd_match;
8764 if (result == NULL)
8765 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008766 *errormsg = _("E497: no autocommand match name to substitute for \"<amatch>\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008767 return NULL;
8768 }
8769 break;
8770
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008771 case SPEC_SFILE: // file name for ":so" command
Bram Moolenaara5d04232020-07-26 15:37:02 +02008772 case SPEC_STACK: // call stack
Bram Moolenaar4f25b1a2020-09-10 19:25:05 +02008773 result = estack_sfile(spec_idx == SPEC_SFILE
8774 ? ESTACK_SFILE : ESTACK_STACK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008775 if (result == NULL)
8776 {
Bram Moolenaara5d04232020-07-26 15:37:02 +02008777 *errormsg = spec_idx == SPEC_SFILE
8778 ? _("E498: no :source file name to substitute for \"<sfile>\"")
8779 : _("E489: no call stack to substitute for \"<stack>\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008780 return NULL;
8781 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01008782 resultbuf = result; // remember allocated string
Bram Moolenaar071d4272004-06-13 20:20:40 +00008783 break;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008784
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008785 case SPEC_SLNUM: // line in file for ":so" command
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01008786 if (SOURCING_NAME == NULL || SOURCING_LNUM == 0)
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01008787 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008788 *errormsg = _("E842: no line number to use for \"<slnum>\"");
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01008789 return NULL;
8790 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01008791 sprintf((char *)strbuf, "%ld", SOURCING_LNUM);
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01008792 result = strbuf;
8793 break;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008794
8795#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008796 case SPEC_SFLNUM: // line in script file
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01008797 if (current_sctx.sc_lnum + SOURCING_LNUM == 0)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008798 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008799 *errormsg = _("E961: no line number to use for \"<sflnum>\"");
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008800 return NULL;
8801 }
8802 sprintf((char *)strbuf, "%ld",
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01008803 (long)(current_sctx.sc_lnum + SOURCING_LNUM));
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008804 result = strbuf;
8805 break;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008806
Bram Moolenaar90944302020-08-01 20:45:11 +02008807 case SPEC_SID:
8808 if (current_sctx.sc_sid <= 0)
8809 {
8810 *errormsg = _(e_usingsid);
8811 return NULL;
8812 }
8813 sprintf((char *)strbuf, "<SNR>%d_", current_sctx.sc_sid);
8814 result = strbuf;
8815 break;
Bram Moolenaare4218b92020-08-01 21:11:38 +02008816#endif
Bram Moolenaar90944302020-08-01 20:45:11 +02008817
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008818#ifdef FEAT_CLIENTSERVER
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008819 case SPEC_CLIENT: // Source of last submitted input
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008820 sprintf((char *)strbuf, PRINTF_HEX_LONG_U,
8821 (long_u)clientWindow);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008822 result = strbuf;
8823 break;
8824#endif
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008825
Bram Moolenaar9e0f6ec2017-05-16 09:36:54 +02008826 default:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008827 result = (char_u *)""; // avoid gcc warning
Bram Moolenaar9e0f6ec2017-05-16 09:36:54 +02008828 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008829 }
8830
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008831 resultlen = (int)STRLEN(result); // length of new string
8832 if (src[*usedlen] == '<') // remove the file name extension
Bram Moolenaar071d4272004-06-13 20:20:40 +00008833 {
8834 ++*usedlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008835 if ((s = vim_strrchr(result, '.')) != NULL && s >= gettail(result))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008836 resultlen = (int)(s - result);
8837 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008838 else if (!skip_mod)
8839 {
Bram Moolenaar00136dc2018-07-25 21:19:13 +02008840 valid |= modify_fname(src, tilde_file, usedlen, &result, &resultbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008841 &resultlen);
8842 if (result == NULL)
8843 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008844 *errormsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +00008845 return NULL;
8846 }
8847 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008848 }
8849
8850 if (resultlen == 0 || valid != VALID_HEAD + VALID_PATH)
8851 {
8852 if (valid != VALID_HEAD + VALID_PATH)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008853 // xgettext:no-c-format
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008854 *errormsg = _("E499: Empty file name for '%' or '#', only works with \":p:h\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008855 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008856 *errormsg = _("E500: Evaluates to an empty string");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008857 result = NULL;
8858 }
8859 else
8860 result = vim_strnsave(result, resultlen);
8861 vim_free(resultbuf);
8862 return result;
8863}
8864
8865/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008866 * Expand the <sfile> string in "arg".
8867 *
8868 * Returns an allocated string, or NULL for any error.
8869 */
8870 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008871expand_sfile(char_u *arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008872{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008873 char *errormsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008874 int len;
8875 char_u *result;
8876 char_u *newres;
8877 char_u *repl;
8878 int srclen;
8879 char_u *p;
8880
8881 result = vim_strsave(arg);
8882 if (result == NULL)
8883 return NULL;
8884
8885 for (p = result; *p; )
8886 {
8887 if (STRNCMP(p, "<sfile>", 7) != 0)
8888 ++p;
8889 else
8890 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008891 // replace "<sfile>" with the sourced file name, and do ":" stuff
Bram Moolenaar63b92542007-03-27 14:57:09 +00008892 repl = eval_vars(p, result, &srclen, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008893 if (errormsg != NULL)
8894 {
8895 if (*errormsg)
8896 emsg(errormsg);
8897 vim_free(result);
8898 return NULL;
8899 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008900 if (repl == NULL) // no match (cannot happen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008901 {
8902 p += srclen;
8903 continue;
8904 }
8905 len = (int)STRLEN(result) - srclen + (int)STRLEN(repl) + 1;
8906 newres = alloc(len);
8907 if (newres == NULL)
8908 {
8909 vim_free(repl);
8910 vim_free(result);
8911 return NULL;
8912 }
8913 mch_memmove(newres, result, (size_t)(p - result));
8914 STRCPY(newres + (p - result), repl);
8915 len = (int)STRLEN(newres);
8916 STRCAT(newres, p + srclen);
8917 vim_free(repl);
8918 vim_free(result);
8919 result = newres;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008920 p = newres + len; // continue after the match
Bram Moolenaar071d4272004-06-13 20:20:40 +00008921 }
8922 }
8923
8924 return result;
8925}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008926
Bram Moolenaar071d4272004-06-13 20:20:40 +00008927#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG) || defined(PROTO)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00008928/*
Bram Moolenaard9462e32011-04-11 21:35:11 +02008929 * Make a dialog message in "buff[DIALOG_MSG_SIZE]".
Bram Moolenaarb765d632005-06-07 21:00:02 +00008930 * "format" must contain "%s".
Bram Moolenaar9c13b352005-05-19 20:53:52 +00008931 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008932 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008933dialog_msg(char_u *buff, char *format, char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008934{
Bram Moolenaar071d4272004-06-13 20:20:40 +00008935 if (fname == NULL)
8936 fname = (char_u *)_("Untitled");
Bram Moolenaard9462e32011-04-11 21:35:11 +02008937 vim_snprintf((char *)buff, DIALOG_MSG_SIZE, format, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008938}
8939#endif
8940
8941/*
8942 * ":behave {mswin,xterm}"
8943 */
8944 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008945ex_behave(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008946{
8947 if (STRCMP(eap->arg, "mswin") == 0)
8948 {
8949 set_option_value((char_u *)"selection", 0L, (char_u *)"exclusive", 0);
8950 set_option_value((char_u *)"selectmode", 0L, (char_u *)"mouse,key", 0);
8951 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"popup", 0);
8952 set_option_value((char_u *)"keymodel", 0L,
8953 (char_u *)"startsel,stopsel", 0);
8954 }
8955 else if (STRCMP(eap->arg, "xterm") == 0)
8956 {
8957 set_option_value((char_u *)"selection", 0L, (char_u *)"inclusive", 0);
8958 set_option_value((char_u *)"selectmode", 0L, (char_u *)"", 0);
8959 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"extend", 0);
8960 set_option_value((char_u *)"keymodel", 0L, (char_u *)"", 0);
8961 }
8962 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008963 semsg(_(e_invarg2), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008964}
8965
Bram Moolenaar071d4272004-06-13 20:20:40 +00008966static int filetype_detect = FALSE;
8967static int filetype_plugin = FALSE;
8968static int filetype_indent = FALSE;
8969
8970/*
8971 * ":filetype [plugin] [indent] {on,off,detect}"
8972 * on: Load the filetype.vim file to install autocommands for file types.
8973 * off: Load the ftoff.vim file to remove all autocommands for file types.
8974 * plugin on: load filetype.vim and ftplugin.vim
8975 * plugin off: load ftplugof.vim
8976 * indent on: load filetype.vim and indent.vim
8977 * indent off: load indoff.vim
8978 */
8979 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008980ex_filetype(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008981{
8982 char_u *arg = eap->arg;
8983 int plugin = FALSE;
8984 int indent = FALSE;
8985
8986 if (*eap->arg == NUL)
8987 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008988 // Print current status.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008989 smsg("filetype detection:%s plugin:%s indent:%s",
Bram Moolenaar071d4272004-06-13 20:20:40 +00008990 filetype_detect ? "ON" : "OFF",
8991 filetype_plugin ? (filetype_detect ? "ON" : "(on)") : "OFF",
8992 filetype_indent ? (filetype_detect ? "ON" : "(on)") : "OFF");
8993 return;
8994 }
8995
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008996 // Accept "plugin" and "indent" in any order.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008997 for (;;)
8998 {
8999 if (STRNCMP(arg, "plugin", 6) == 0)
9000 {
9001 plugin = TRUE;
9002 arg = skipwhite(arg + 6);
9003 continue;
9004 }
9005 if (STRNCMP(arg, "indent", 6) == 0)
9006 {
9007 indent = TRUE;
9008 arg = skipwhite(arg + 6);
9009 continue;
9010 }
9011 break;
9012 }
9013 if (STRCMP(arg, "on") == 0 || STRCMP(arg, "detect") == 0)
9014 {
9015 if (*arg == 'o' || !filetype_detect)
9016 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01009017 source_runtime((char_u *)FILETYPE_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009018 filetype_detect = TRUE;
9019 if (plugin)
9020 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01009021 source_runtime((char_u *)FTPLUGIN_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009022 filetype_plugin = TRUE;
9023 }
9024 if (indent)
9025 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01009026 source_runtime((char_u *)INDENT_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009027 filetype_indent = TRUE;
9028 }
9029 }
9030 if (*arg == 'd')
9031 {
Bram Moolenaar1610d052016-06-09 22:53:01 +02009032 (void)do_doautocmd((char_u *)"filetypedetect BufRead", TRUE, NULL);
Bram Moolenaara3227e22006-03-08 21:32:40 +00009033 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009034 }
9035 }
9036 else if (STRCMP(arg, "off") == 0)
9037 {
9038 if (plugin || indent)
9039 {
9040 if (plugin)
9041 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01009042 source_runtime((char_u *)FTPLUGOF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009043 filetype_plugin = FALSE;
9044 }
9045 if (indent)
9046 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01009047 source_runtime((char_u *)INDOFF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009048 filetype_indent = FALSE;
9049 }
9050 }
9051 else
9052 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01009053 source_runtime((char_u *)FTOFF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009054 filetype_detect = FALSE;
9055 }
9056 }
9057 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009058 semsg(_(e_invarg2), arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009059}
9060
9061/*
Bram Moolenaar3e545692017-06-04 19:00:32 +02009062 * ":setfiletype [FALLBACK] {name}"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009063 */
9064 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009065ex_setfiletype(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009066{
9067 if (!did_filetype)
Bram Moolenaar3e545692017-06-04 19:00:32 +02009068 {
9069 char_u *arg = eap->arg;
9070
9071 if (STRNCMP(arg, "FALLBACK ", 9) == 0)
9072 arg += 9;
9073
9074 set_option_value((char_u *)"filetype", 0L, arg, OPT_LOCAL);
9075 if (arg != eap->arg)
9076 did_filetype = FALSE;
9077 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009078}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009079
Bram Moolenaar071d4272004-06-13 20:20:40 +00009080 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009081ex_digraphs(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009082{
9083#ifdef FEAT_DIGRAPHS
9084 if (*eap->arg != NUL)
9085 putdigraph(eap->arg);
9086 else
Bram Moolenaareae8ae12018-12-14 18:53:02 +01009087 listdigraphs(eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009088#else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009089 emsg(_("E196: No digraphs in this version"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009090#endif
9091}
9092
Bram Moolenaar451fc7b2018-04-27 22:53:07 +02009093#if defined(FEAT_SEARCH_EXTRA) || defined(PROTO)
9094 void
9095set_no_hlsearch(int flag)
9096{
9097 no_hlsearch = flag;
9098# ifdef FEAT_EVAL
9099 set_vim_var_nr(VV_HLSEARCH, !no_hlsearch && p_hls);
9100# endif
9101}
9102
Bram Moolenaar071d4272004-06-13 20:20:40 +00009103/*
9104 * ":nohlsearch"
9105 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009106 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009107ex_nohlsearch(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009108{
Bram Moolenaar451fc7b2018-04-27 22:53:07 +02009109 set_no_hlsearch(TRUE);
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00009110 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009111}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009112#endif
9113
9114#ifdef FEAT_CRYPT
9115/*
9116 * ":X": Get crypt key
9117 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009118 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009119ex_X(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009120{
Bram Moolenaar3a0c9082014-11-12 15:15:42 +01009121 crypt_check_current_method();
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02009122 (void)crypt_get_key(TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009123}
9124#endif
9125
9126#ifdef FEAT_FOLDING
9127 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009128ex_fold(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009129{
9130 if (foldManualAllowed(TRUE))
9131 foldCreate(eap->line1, eap->line2);
9132}
9133
9134 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009135ex_foldopen(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009136{
9137 opFoldRange(eap->line1, eap->line2, eap->cmdidx == CMD_foldopen,
9138 eap->forceit, FALSE);
9139}
9140
9141 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009142ex_folddo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009143{
9144 linenr_T lnum;
9145
Bram Moolenaar4facea32019-10-12 20:17:40 +02009146# ifdef FEAT_CLIPBOARD
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02009147 start_global_changes();
Bram Moolenaar4facea32019-10-12 20:17:40 +02009148# endif
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02009149
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009150 // First set the marks for all lines closed/open.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009151 for (lnum = eap->line1; lnum <= eap->line2; ++lnum)
9152 if (hasFolding(lnum, NULL, NULL) == (eap->cmdidx == CMD_folddoclosed))
9153 ml_setmarked(lnum);
9154
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009155 // Execute the command on the marked lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009156 global_exe(eap->arg);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009157 ml_clearmarked(); // clear rest of the marks
Bram Moolenaar4facea32019-10-12 20:17:40 +02009158# ifdef FEAT_CLIPBOARD
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02009159 end_global_changes();
Bram Moolenaar4facea32019-10-12 20:17:40 +02009160# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009161}
9162#endif
Bram Moolenaarf5291f32017-09-14 22:55:37 +02009163
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009164#if defined(FEAT_QUICKFIX) || defined(PROTO)
Bram Moolenaar39665952018-08-15 20:59:48 +02009165/*
9166 * Returns TRUE if the supplied Ex cmdidx is for a location list command
9167 * instead of a quickfix command.
9168 */
9169 int
9170is_loclist_cmd(int cmdidx)
9171{
Bram Moolenaar74c8be22018-08-23 22:51:40 +02009172 if (cmdidx < 0 || cmdidx >= CMD_SIZE)
Bram Moolenaar39665952018-08-15 20:59:48 +02009173 return FALSE;
9174 return cmdnames[cmdidx].cmd_name[0] == 'l';
9175}
9176#endif
9177
Bram Moolenaar4facea32019-10-12 20:17:40 +02009178#if defined(FEAT_TIMERS) || defined(PROTO)
Bram Moolenaarf5291f32017-09-14 22:55:37 +02009179 int
9180get_pressedreturn(void)
9181{
9182 return ex_pressedreturn;
9183}
9184
9185 void
9186set_pressedreturn(int val)
9187{
9188 ex_pressedreturn = val;
9189}
9190#endif