blob: b08482232bf1cbcf98bbb31e19090c6d9fd78fc1 [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 Moolenaar8a7d6542020-01-26 15:56:19 +0100307# define ex_vim9script ex_ni
Bram Moolenaare4ce8252019-08-04 15:30:16 +0200308# define ex_while ex_ni
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100309# define ex_import ex_ni
310# define ex_export ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000311#endif
Bram Moolenaar84538072019-07-28 14:15:42 +0200312#ifndef FEAT_SESSION
Bram Moolenaar071d4272004-06-13 20:20:40 +0000313# define ex_loadview ex_ni
314#endif
Bram Moolenaardefa0672019-07-21 19:25:37 +0200315#ifndef FEAT_VIMINFO
Bram Moolenaar071d4272004-06-13 20:20:40 +0000316# define ex_viminfo ex_ni
317#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100318static void ex_behave(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100319static void ex_filetype(exarg_T *eap);
320static void ex_setfiletype(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000321#ifndef FEAT_DIFF
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000322# define ex_diffoff ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000323# define ex_diffpatch ex_ni
324# define ex_diffgetput ex_ni
325# define ex_diffsplit ex_ni
326# define ex_diffthis ex_ni
327# define ex_diffupdate ex_ni
328#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100329static void ex_digraphs(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000330#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100331static void ex_nohlsearch(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000332#else
333# define ex_nohlsearch ex_ni
334# define ex_match ex_ni
335#endif
336#ifdef FEAT_CRYPT
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100337static void ex_X(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000338#else
339# define ex_X ex_ni
340#endif
341#ifdef FEAT_FOLDING
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100342static void ex_fold(exarg_T *eap);
343static void ex_foldopen(exarg_T *eap);
344static void ex_folddo(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000345#else
346# define ex_fold ex_ni
347# define ex_foldopen ex_ni
348# define ex_folddo ex_ni
349#endif
Bram Moolenaar13505972019-01-24 15:04:48 +0100350#if !(defined(HAVE_LOCALE_H) || defined(X_LOCALE))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000351# define ex_language ex_ni
352#endif
353#ifndef FEAT_SIGNS
354# define ex_sign ex_ni
355#endif
Bram Moolenaar009b2592004-10-24 19:18:58 +0000356#ifndef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200357# define ex_nbclose ex_ni
Bram Moolenaar009b2592004-10-24 19:18:58 +0000358# define ex_nbkey ex_ni
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200359# define ex_nbstart ex_ni
Bram Moolenaar009b2592004-10-24 19:18:58 +0000360#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000361
Bram Moolenaar071d4272004-06-13 20:20:40 +0000362#ifndef FEAT_JUMPLIST
363# define ex_jumps ex_ni
Bram Moolenaar2d358992016-06-12 21:20:54 +0200364# define ex_clearjumps ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000365# define ex_changes ex_ni
366#endif
367
Bram Moolenaar05159a02005-02-26 23:04:13 +0000368#ifndef FEAT_PROFILE
369# define ex_profile ex_ni
370#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200371#ifndef FEAT_TERMINAL
372# define ex_terminal ex_ni
373#endif
Bram Moolenaard4aa83a2019-05-09 18:59:31 +0200374#if !defined(FEAT_X11) || !defined(FEAT_XCLIPBOARD)
375# define ex_xrestore ex_ni
376#endif
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +0100377#if !defined(FEAT_PROP_POPUP)
Bram Moolenaar682725c2019-05-25 20:10:37 +0200378# define ex_popupclear ex_ni
379#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +0000380
Bram Moolenaar071d4272004-06-13 20:20:40 +0000381/*
382 * Declare cmdnames[].
383 */
384#define DO_DECLARE_EXCMD
385#include "ex_cmds.h"
Bram Moolenaar6de5e122017-04-20 21:55:44 +0200386#include "ex_cmdidxs.h"
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +0100387
Bram Moolenaar071d4272004-06-13 20:20:40 +0000388static char_u dollar_command[2] = {'$', 0};
389
390
391#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100392// Struct for storing a line inside a while/for loop
Bram Moolenaar071d4272004-06-13 20:20:40 +0000393typedef struct
394{
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100395 char_u *line; // command line
396 linenr_T lnum; // sourcing_lnum of the line
Bram Moolenaar071d4272004-06-13 20:20:40 +0000397} wcmd_T;
398
399/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000400 * Structure used to store info for line position in a while or for loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000401 * This is required, because do_one_cmd() may invoke ex_function(), which
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000402 * reads more lines that may come from the while/for loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000403 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000404struct loop_cookie
Bram Moolenaar071d4272004-06-13 20:20:40 +0000405{
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100406 garray_T *lines_gap; // growarray with line info
407 int current_line; // last read line from growarray
408 int repeating; // TRUE when looping a second time
409 // When "repeating" is FALSE use "getline" and "cookie" to get lines
Bram Moolenaar20b23c62020-08-20 15:25:00 +0200410 char_u *(*getline)(int, void *, int, getline_opt_T);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000411 void *cookie;
412};
413
Bram Moolenaar20b23c62020-08-20 15:25:00 +0200414static char_u *get_loop_line(int c, void *cookie, int indent, getline_opt_T options);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100415static int store_loop_line(garray_T *gap, char_u *line);
416static void free_cmdlines(garray_T *gap);
Bram Moolenaared203462004-06-16 11:19:22 +0000417
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100418// Struct to save a few things while debugging. Used in do_cmdline() only.
Bram Moolenaared203462004-06-16 11:19:22 +0000419struct dbg_stuff
420{
421 int trylevel;
422 int force_abort;
423 except_T *caught_stack;
424 char_u *vv_exception;
425 char_u *vv_throwpoint;
426 int did_emsg;
427 int got_int;
428 int did_throw;
429 int need_rethrow;
430 int check_cstack;
431 except_T *current_exception;
432};
433
Bram Moolenaared203462004-06-16 11:19:22 +0000434 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100435save_dbg_stuff(struct dbg_stuff *dsp)
Bram Moolenaared203462004-06-16 11:19:22 +0000436{
437 dsp->trylevel = trylevel; trylevel = 0;
438 dsp->force_abort = force_abort; force_abort = FALSE;
439 dsp->caught_stack = caught_stack; caught_stack = NULL;
440 dsp->vv_exception = v_exception(NULL);
441 dsp->vv_throwpoint = v_throwpoint(NULL);
442
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100443 // Necessary for debugging an inactive ":catch", ":finally", ":endtry"
Bram Moolenaared203462004-06-16 11:19:22 +0000444 dsp->did_emsg = did_emsg; did_emsg = FALSE;
445 dsp->got_int = got_int; got_int = FALSE;
446 dsp->did_throw = did_throw; did_throw = FALSE;
447 dsp->need_rethrow = need_rethrow; need_rethrow = FALSE;
448 dsp->check_cstack = check_cstack; check_cstack = FALSE;
449 dsp->current_exception = current_exception; current_exception = NULL;
450}
451
452 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100453restore_dbg_stuff(struct dbg_stuff *dsp)
Bram Moolenaared203462004-06-16 11:19:22 +0000454{
455 suppress_errthrow = FALSE;
456 trylevel = dsp->trylevel;
457 force_abort = dsp->force_abort;
458 caught_stack = dsp->caught_stack;
459 (void)v_exception(dsp->vv_exception);
460 (void)v_throwpoint(dsp->vv_throwpoint);
461 did_emsg = dsp->did_emsg;
462 got_int = dsp->got_int;
463 did_throw = dsp->did_throw;
464 need_rethrow = dsp->need_rethrow;
465 check_cstack = dsp->check_cstack;
466 current_exception = dsp->current_exception;
467}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000468#endif
469
Bram Moolenaar071d4272004-06-13 20:20:40 +0000470/*
471 * do_exmode(): Repeatedly get commands for the "Ex" mode, until the ":vi"
472 * command is given.
473 */
474 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100475do_exmode(
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100476 int improved) // TRUE for "improved Ex" mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000477{
478 int save_msg_scroll;
479 int prev_msg_row;
480 linenr_T prev_line;
Bram Moolenaar79518e22017-02-17 16:31:35 +0100481 varnumber_T changedtick;
Bram Moolenaardf177f62005-02-22 08:39:57 +0000482
483 if (improved)
484 exmode_active = EXMODE_VIM;
485 else
486 exmode_active = EXMODE_NORMAL;
487 State = NORMAL;
488
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100489 // When using ":global /pat/ visual" and then "Q" we return to continue
490 // the :global command.
Bram Moolenaardf177f62005-02-22 08:39:57 +0000491 if (global_busy)
492 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000493
494 save_msg_scroll = msg_scroll;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100495 ++RedrawingDisabled; // don't redisplay the window
496 ++no_wait_return; // don't wait for return
Bram Moolenaar071d4272004-06-13 20:20:40 +0000497#ifdef FEAT_GUI
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100498 // Ignore scrollbar and mouse events in Ex mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000499 ++hold_gui_events;
500#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000501
Bram Moolenaar32526b32019-01-19 17:43:09 +0100502 msg(_("Entering Ex mode. Type \"visual\" to go to Normal mode."));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000503 while (exmode_active)
504 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100505 // Check for a ":normal" command and no more characters left.
Bram Moolenaar7c626922005-02-07 22:01:03 +0000506 if (ex_normal_busy > 0 && typebuf.tb_len == 0)
507 {
508 exmode_active = FALSE;
509 break;
510 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000511 msg_scroll = TRUE;
512 need_wait_return = FALSE;
513 ex_pressedreturn = FALSE;
514 ex_no_reprint = FALSE;
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100515 changedtick = CHANGEDTICK(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000516 prev_msg_row = msg_row;
517 prev_line = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000518 if (improved)
519 {
520 cmdline_row = msg_row;
521 do_cmdline(NULL, getexline, NULL, 0);
522 }
523 else
524 do_cmdline(NULL, getexmodeline, NULL, DOCMD_NOWAIT);
525 lines_left = Rows - 1;
526
Bram Moolenaardf177f62005-02-22 08:39:57 +0000527 if ((prev_line != curwin->w_cursor.lnum
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100528 || changedtick != CHANGEDTICK(curbuf)) && !ex_no_reprint)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000529 {
Bram Moolenaardf177f62005-02-22 08:39:57 +0000530 if (curbuf->b_ml.ml_flags & ML_EMPTY)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100531 emsg(_(e_emptybuf));
Bram Moolenaardf177f62005-02-22 08:39:57 +0000532 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000533 {
Bram Moolenaardf177f62005-02-22 08:39:57 +0000534 if (ex_pressedreturn)
535 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100536 // go up one line, to overwrite the ":<CR>" line, so the
537 // output doesn't contain empty lines.
Bram Moolenaardf177f62005-02-22 08:39:57 +0000538 msg_row = prev_msg_row;
539 if (prev_msg_row == Rows - 1)
540 msg_row--;
541 }
542 msg_col = 0;
543 print_line_no_prefix(curwin->w_cursor.lnum, FALSE, FALSE);
544 msg_clr_eos();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000545 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000546 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100547 else if (ex_pressedreturn && !ex_no_reprint) // must be at EOF
Bram Moolenaardf177f62005-02-22 08:39:57 +0000548 {
549 if (curbuf->b_ml.ml_flags & ML_EMPTY)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100550 emsg(_(e_emptybuf));
Bram Moolenaardf177f62005-02-22 08:39:57 +0000551 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100552 emsg(_("E501: At end-of-file"));
Bram Moolenaardf177f62005-02-22 08:39:57 +0000553 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000554 }
555
556#ifdef FEAT_GUI
557 --hold_gui_events;
558#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000559 --RedrawingDisabled;
560 --no_wait_return;
561 update_screen(CLEAR);
562 need_wait_return = FALSE;
563 msg_scroll = save_msg_scroll;
564}
565
566/*
Bram Moolenaar4facea32019-10-12 20:17:40 +0200567 * Print the executed command for when 'verbose' is set.
568 * When "lnum" is 0 only print the command.
569 */
570 static void
571msg_verbose_cmd(linenr_T lnum, char_u *cmd)
572{
573 ++no_wait_return;
574 verbose_enter_scroll();
575
576 if (lnum == 0)
577 smsg(_("Executing: %s"), cmd);
578 else
579 smsg(_("line %ld: %s"), (long)lnum, cmd);
580 if (msg_silent == 0)
581 msg_puts("\n"); // don't overwrite this
582
583 verbose_leave_scroll();
584 --no_wait_return;
585}
586
587/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000588 * Execute a simple command line. Used for translated commands like "*".
589 */
590 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100591do_cmdline_cmd(char_u *cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000592{
593 return do_cmdline(cmd, NULL, NULL,
594 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
595}
596
597/*
Bram Moolenaar47a2abf2020-11-25 20:12:11 +0100598 * Execute the "+cmd" argument of "edit +cmd fname" and the like.
599 * This allows for using a range without ":" in Vim9 script.
600 */
601 int
602do_cmd_argument(char_u *cmd)
603{
604 return do_cmdline(cmd, NULL, NULL,
605 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED|DOCMD_RANGEOK);
606}
607
608/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000609 * do_cmdline(): execute one Ex command line
610 *
611 * 1. Execute "cmdline" when it is not NULL.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100612 * If "cmdline" is NULL, or more lines are needed, fgetline() is used.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000613 * 2. Split up in parts separated with '|'.
614 *
615 * This function can be called recursively!
616 *
617 * flags:
618 * DOCMD_VERBOSE - The command will be included in the error message.
619 * DOCMD_NOWAIT - Don't call wait_return() and friends.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100620 * DOCMD_REPEAT - Repeat execution until fgetline() returns NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000621 * DOCMD_KEYTYPED - Don't reset KeyTyped.
622 * DOCMD_EXCRESET - Reset the exception environment (used for debugging).
623 * DOCMD_KEEPLINE - Store first typed line (for repeating with ".").
624 *
625 * return FAIL if cmdline could not be executed, OK otherwise
626 */
627 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100628do_cmdline(
629 char_u *cmdline,
Bram Moolenaar66250c92020-08-20 15:02:42 +0200630 char_u *(*fgetline)(int, void *, int, getline_opt_T),
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100631 void *cookie, // argument for fgetline()
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100632 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000633{
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100634 char_u *next_cmdline; // next cmd to execute
635 char_u *cmdline_copy = NULL; // copy of cmd line
636 int used_getline = FALSE; // used "fgetline" to obtain command
637 static int recursive = 0; // recursive depth
Bram Moolenaar071d4272004-06-13 20:20:40 +0000638 int msg_didout_before_start = 0;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100639 int count = 0; // line number count
640 int did_inc = FALSE; // incremented RedrawingDisabled
Bram Moolenaar071d4272004-06-13 20:20:40 +0000641 int retval = OK;
642#ifdef FEAT_EVAL
Bram Moolenaarddef1292019-12-16 17:10:33 +0100643 cstack_T cstack; // conditional stack
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100644 garray_T lines_ga; // keep lines for ":while"/":for"
645 int current_line = 0; // active line in lines_ga
Bram Moolenaard5053d02020-06-28 15:51:16 +0200646 int current_line_before = 0;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100647 char_u *fname = NULL; // function or script name
648 linenr_T *breakpoint = NULL; // ptr to breakpoint field in cookie
649 int *dbg_tick = NULL; // ptr to dbg_tick field in cookie
650 struct dbg_stuff debug_saved; // saved things for debug mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000651 int initial_trylevel;
Bram Moolenaar25e0f582020-05-25 22:36:50 +0200652 msglist_T **saved_msg_list = NULL;
Bram Moolenaar683581e2020-10-22 21:22:58 +0200653 msglist_T *private_msg_list = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000654
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100655 // "fgetline" and "cookie" passed to do_one_cmd()
Bram Moolenaar66250c92020-08-20 15:02:42 +0200656 char_u *(*cmd_getline)(int, void *, int, getline_opt_T);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000657 void *cmd_cookie;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000658 struct loop_cookie cmd_loop_cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000659 void *real_cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +0000660 int getline_is_func;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000661#else
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100662# define cmd_getline fgetline
Bram Moolenaar071d4272004-06-13 20:20:40 +0000663# define cmd_cookie cookie
664#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100665 static int call_depth = 0; // recursiveness
Bram Moolenaar2196bce2020-04-12 20:01:11 +0200666#ifdef FEAT_EVAL
Bram Moolenaare31ee862020-01-07 20:59:34 +0100667 ESTACK_CHECK_DECLARATION
Bram Moolenaar071d4272004-06-13 20:20:40 +0000668
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100669 // For every pair of do_cmdline()/do_one_cmd() calls, use an extra memory
670 // location for storing error messages to be converted to an exception.
671 // This ensures that the do_errthrow() call in do_one_cmd() does not
672 // combine the messages stored by an earlier invocation of do_one_cmd()
673 // with the command name of the later one. This would happen when
674 // BufWritePost autocommands are executed after a write error.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000675 saved_msg_list = msg_list;
676 msg_list = &private_msg_list;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000677#endif
678
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100679 // It's possible to create an endless loop with ":execute", catch that
680 // here. The value of 200 allows nested function calls, ":source", etc.
681 // Allow 200 or 'maxfuncdepth', whatever is larger.
Bram Moolenaarb094ff42017-01-02 16:16:39 +0100682 if (call_depth >= 200
683#ifdef FEAT_EVAL
684 && call_depth >= p_mfd
685#endif
686 )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000687 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100688 emsg(_("E169: Command too recursive"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000689#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100690 // When converting to an exception, we do not include the command name
691 // since this is not an error of the specific command.
Bram Moolenaarddef1292019-12-16 17:10:33 +0100692 do_errthrow((cstack_T *)NULL, (char_u *)NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000693 msg_list = saved_msg_list;
694#endif
695 return FAIL;
696 }
697 ++call_depth;
698
699#ifdef FEAT_EVAL
700 cstack.cs_idx = -1;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000701 cstack.cs_looplevel = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000702 cstack.cs_trylevel = 0;
703 cstack.cs_emsg_silent_list = NULL;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000704 cstack.cs_lflags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000705 ga_init2(&lines_ga, (int)sizeof(wcmd_T), 10);
706
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100707 real_cookie = getline_cookie(fgetline, cookie);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000708
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100709 // Inside a function use a higher nesting level.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100710 getline_is_func = getline_equal(fgetline, cookie, get_func_line);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000711 if (getline_is_func && ex_nesting_level == func_level(real_cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000712 ++ex_nesting_level;
713
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100714 // Get the function or script name and the address where the next breakpoint
715 // line and the debug tick for a function or script are stored.
Bram Moolenaar05159a02005-02-26 23:04:13 +0000716 if (getline_is_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000717 {
718 fname = func_name(real_cookie);
719 breakpoint = func_breakpoint(real_cookie);
720 dbg_tick = func_dbg_tick(real_cookie);
721 }
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100722 else if (getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000723 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100724 fname = SOURCING_NAME;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000725 breakpoint = source_breakpoint(real_cookie);
726 dbg_tick = source_dbg_tick(real_cookie);
727 }
728
729 /*
730 * Initialize "force_abort" and "suppress_errthrow" at the top level.
731 */
732 if (!recursive)
733 {
734 force_abort = FALSE;
735 suppress_errthrow = FALSE;
736 }
737
738 /*
739 * If requested, store and reset the global values controlling the
Bram Moolenaar89d40322006-08-29 15:30:07 +0000740 * exception handling (used when debugging). Otherwise clear it to avoid
741 * a bogus compiler warning when the optimizer uses inline functions...
Bram Moolenaar071d4272004-06-13 20:20:40 +0000742 */
Bram Moolenaarb4872942006-05-13 10:32:52 +0000743 if (flags & DOCMD_EXCRESET)
Bram Moolenaared203462004-06-16 11:19:22 +0000744 save_dbg_stuff(&debug_saved);
Bram Moolenaar89d40322006-08-29 15:30:07 +0000745 else
Bram Moolenaara80faa82020-04-12 19:37:17 +0200746 CLEAR_FIELD(debug_saved);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000747
748 initial_trylevel = trylevel;
749
750 /*
751 * "did_throw" will be set to TRUE when an exception is being thrown.
752 */
753 did_throw = FALSE;
754#endif
755 /*
756 * "did_emsg" will be set to TRUE when emsg() is used, in which case we
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000757 * cancel the whole command line, and any if/endif or loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000758 * If force_abort is set, we cancel everything.
759 */
Bram Moolenaareeece9e2020-11-20 19:26:48 +0100760#ifdef FEAT_EVAL
761 did_emsg_cumul += did_emsg;
762#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000763 did_emsg = FALSE;
764
765 /*
766 * KeyTyped is only set when calling vgetc(). Reset it here when not
767 * calling vgetc() (sourced command lines).
768 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100769 if (!(flags & DOCMD_KEYTYPED)
770 && !getline_equal(fgetline, cookie, getexline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000771 KeyTyped = FALSE;
772
773 /*
774 * Continue executing command lines:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000775 * - when inside an ":if", ":while" or ":for"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000776 * - for multiple commands on one line, separated with '|'
777 * - when repeating until there are no more lines (for ":source")
778 */
779 next_cmdline = cmdline;
780 do
781 {
Bram Moolenaar05159a02005-02-26 23:04:13 +0000782#ifdef FEAT_EVAL
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100783 getline_is_func = getline_equal(fgetline, cookie, get_func_line);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000784#endif
785
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100786 // stop skipping cmds for an error msg after all endif/while/for
Bram Moolenaar071d4272004-06-13 20:20:40 +0000787 if (next_cmdline == NULL
788#ifdef FEAT_EVAL
789 && !force_abort
790 && cstack.cs_idx < 0
Bram Moolenaar05159a02005-02-26 23:04:13 +0000791 && !(getline_is_func && func_has_abort(real_cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000792#endif
793 )
Bram Moolenaareeece9e2020-11-20 19:26:48 +0100794 {
795#ifdef FEAT_EVAL
796 did_emsg_cumul += did_emsg;
797#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000798 did_emsg = FALSE;
Bram Moolenaareeece9e2020-11-20 19:26:48 +0100799 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000800
801 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000802 * 1. If repeating a line in a loop, get a line from lines_ga.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100803 * 2. If no line given: Get an allocated line with fgetline().
Bram Moolenaar071d4272004-06-13 20:20:40 +0000804 * 3. If a line is given: Make a copy, so we can mess with it.
805 */
806
807#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100808 // 1. If repeating, get a previous line from lines_ga.
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000809 if (cstack.cs_looplevel > 0 && current_line < lines_ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000810 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100811 // Each '|' separated command is stored separately in lines_ga, to
812 // be able to jump to it. Don't use next_cmdline now.
Bram Moolenaard23a8232018-02-10 18:45:26 +0100813 VIM_CLEAR(cmdline_copy);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000814
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100815 // Check if a function has returned or, unless it has an unclosed
816 // try conditional, aborted.
Bram Moolenaar05159a02005-02-26 23:04:13 +0000817 if (getline_is_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000818 {
Bram Moolenaar05159a02005-02-26 23:04:13 +0000819# ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000820 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +0000821 func_line_end(real_cookie);
822# endif
823 if (func_has_ended(real_cookie))
824 {
825 retval = FAIL;
826 break;
827 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000828 }
Bram Moolenaar05159a02005-02-26 23:04:13 +0000829#ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000830 else if (do_profiling == PROF_YES
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100831 && getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar05159a02005-02-26 23:04:13 +0000832 script_line_end();
833#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000834
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100835 // Check if a sourced file hit a ":finish" command.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100836 if (source_finished(fgetline, cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000837 {
838 retval = FAIL;
839 break;
840 }
841
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100842 // If breakpoints have been added/deleted need to check for it.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000843 if (breakpoint != NULL && dbg_tick != NULL
844 && *dbg_tick != debug_tick)
845 {
846 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100847 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100848 fname, SOURCING_LNUM);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000849 *dbg_tick = debug_tick;
850 }
851
852 next_cmdline = ((wcmd_T *)(lines_ga.ga_data))[current_line].line;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100853 SOURCING_LNUM = ((wcmd_T *)(lines_ga.ga_data))[current_line].lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000854
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100855 // Did we encounter a breakpoint?
Bram Moolenaar071d4272004-06-13 20:20:40 +0000856 if (breakpoint != NULL && *breakpoint != 0
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100857 && *breakpoint <= SOURCING_LNUM)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000858 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100859 dbg_breakpoint(fname, SOURCING_LNUM);
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100860 // Find next breakpoint.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000861 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100862 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100863 fname, SOURCING_LNUM);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000864 *dbg_tick = debug_tick;
865 }
Bram Moolenaar05159a02005-02-26 23:04:13 +0000866# ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000867 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +0000868 {
869 if (getline_is_func)
870 func_line_start(real_cookie);
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100871 else if (getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar05159a02005-02-26 23:04:13 +0000872 script_line_start();
873 }
874# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000875 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000876#endif
877
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100878 // 2. If no line given, get an allocated line with fgetline().
Bram Moolenaar071d4272004-06-13 20:20:40 +0000879 if (next_cmdline == NULL)
880 {
881 /*
882 * Need to set msg_didout for the first line after an ":if",
883 * otherwise the ":if" will be overwritten.
884 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100885 if (count == 1 && getline_equal(fgetline, cookie, getexline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000886 msg_didout = TRUE;
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100887 if (fgetline == NULL || (next_cmdline = fgetline(':', cookie,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000888#ifdef FEAT_EVAL
889 cstack.cs_idx < 0 ? 0 : (cstack.cs_idx + 1) * 2
890#else
891 0
892#endif
Bram Moolenaare96a2492019-06-25 04:12:16 +0200893 , TRUE)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000894 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100895 // Don't call wait_return for aborted command line. The NULL
896 // returned for the end of a sourced file or executed function
897 // doesn't do this.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000898 if (KeyTyped && !(flags & DOCMD_REPEAT))
899 need_wait_return = FALSE;
900 retval = FAIL;
901 break;
902 }
903 used_getline = TRUE;
904
905 /*
906 * Keep the first typed line. Clear it when more lines are typed.
907 */
908 if (flags & DOCMD_KEEPLINE)
909 {
910 vim_free(repeat_cmdline);
911 if (count == 0)
912 repeat_cmdline = vim_strsave(next_cmdline);
913 else
914 repeat_cmdline = NULL;
915 }
916 }
917
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100918 // 3. Make a copy of the command so we can mess with it.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000919 else if (cmdline_copy == NULL)
920 {
921 next_cmdline = vim_strsave(next_cmdline);
922 if (next_cmdline == NULL)
923 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100924 emsg(_(e_outofmem));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000925 retval = FAIL;
926 break;
927 }
928 }
929 cmdline_copy = next_cmdline;
930
931#ifdef FEAT_EVAL
932 /*
Bram Moolenaard5053d02020-06-28 15:51:16 +0200933 * Inside a while/for loop, and when the command looks like a ":while"
934 * or ":for", the line is stored, because we may need it later when
935 * looping.
936 *
937 * When there is a '|' and another command, it is stored separately,
938 * because we need to be able to jump back to it from an
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000939 * :endwhile/:endfor.
Bram Moolenaard5053d02020-06-28 15:51:16 +0200940 *
941 * Pass a different "fgetline" function to do_one_cmd() below,
942 * that it stores lines in or reads them from "lines_ga". Makes it
943 * possible to define a function inside a while/for loop and handles
944 * line continuation.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000945 */
Bram Moolenaard5053d02020-06-28 15:51:16 +0200946 if ((cstack.cs_looplevel > 0 || has_loop_cmd(next_cmdline)))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000947 {
Bram Moolenaard5053d02020-06-28 15:51:16 +0200948 cmd_getline = get_loop_line;
949 cmd_cookie = (void *)&cmd_loop_cookie;
950 cmd_loop_cookie.lines_gap = &lines_ga;
951 cmd_loop_cookie.current_line = current_line;
952 cmd_loop_cookie.getline = fgetline;
953 cmd_loop_cookie.cookie = cookie;
954 cmd_loop_cookie.repeating = (current_line < lines_ga.ga_len);
955
956 // Save the current line when encountering it the first time.
957 if (current_line == lines_ga.ga_len
958 && store_loop_line(&lines_ga, next_cmdline) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000959 {
960 retval = FAIL;
961 break;
962 }
Bram Moolenaard5053d02020-06-28 15:51:16 +0200963 current_line_before = current_line;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000964 }
Bram Moolenaard5053d02020-06-28 15:51:16 +0200965 else
966 {
967 cmd_getline = fgetline;
968 cmd_cookie = cookie;
969 }
970
Bram Moolenaar071d4272004-06-13 20:20:40 +0000971 did_endif = FALSE;
972#endif
973
974 if (count++ == 0)
975 {
976 /*
977 * All output from the commands is put below each other, without
978 * waiting for a return. Don't do this when executing commands
979 * from a script or when being called recursive (e.g. for ":e
980 * +command file").
981 */
982 if (!(flags & DOCMD_NOWAIT) && !recursive)
983 {
984 msg_didout_before_start = msg_didout;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100985 msg_didany = FALSE; // no output yet
Bram Moolenaar071d4272004-06-13 20:20:40 +0000986 msg_start();
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100987 msg_scroll = TRUE; // put messages below each other
988 ++no_wait_return; // don't wait for return until finished
Bram Moolenaar071d4272004-06-13 20:20:40 +0000989 ++RedrawingDisabled;
990 did_inc = TRUE;
991 }
992 }
993
Bram Moolenaar823654b2020-05-29 23:03:09 +0200994 if ((p_verbose >= 15 && SOURCING_NAME != NULL) || p_verbose >= 16)
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100995 msg_verbose_cmd(SOURCING_LNUM, cmdline_copy);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000996
997 /*
998 * 2. Execute one '|' separated command.
999 * do_one_cmd() will return NULL if there is no trailing '|'.
1000 * "cmdline_copy" can change, e.g. for '%' and '#' expansion.
1001 */
1002 ++recursive;
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01001003 next_cmdline = do_one_cmd(&cmdline_copy, flags,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001004#ifdef FEAT_EVAL
1005 &cstack,
1006#endif
1007 cmd_getline, cmd_cookie);
1008 --recursive;
1009
1010#ifdef FEAT_EVAL
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001011 if (cmd_cookie == (void *)&cmd_loop_cookie)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001012 // Use "current_line" from "cmd_loop_cookie", it may have been
1013 // incremented when defining a function.
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001014 current_line = cmd_loop_cookie.current_line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001015#endif
1016
1017 if (next_cmdline == NULL)
1018 {
Bram Moolenaard23a8232018-02-10 18:45:26 +01001019 VIM_CLEAR(cmdline_copy);
Bram Moolenaard7663c22019-08-06 21:59:57 +02001020
Bram Moolenaar071d4272004-06-13 20:20:40 +00001021 /*
1022 * If the command was typed, remember it for the ':' register.
1023 * Do this AFTER executing the command to make :@: work.
1024 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001025 if (getline_equal(fgetline, cookie, getexline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001026 && new_last_cmdline != NULL)
1027 {
1028 vim_free(last_cmdline);
1029 last_cmdline = new_last_cmdline;
1030 new_last_cmdline = NULL;
1031 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001032 }
1033 else
1034 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001035 // need to copy the command after the '|' to cmdline_copy, for the
1036 // next do_one_cmd()
Bram Moolenaara7241f52008-06-24 20:39:31 +00001037 STRMOVE(cmdline_copy, next_cmdline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001038 next_cmdline = cmdline_copy;
1039 }
1040
1041
1042#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001043 // reset did_emsg for a function that is not aborted by an error
Bram Moolenaar071d4272004-06-13 20:20:40 +00001044 if (did_emsg && !force_abort
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001045 && getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001046 && !func_has_abort(real_cookie))
Bram Moolenaareeece9e2020-11-20 19:26:48 +01001047 {
1048 // did_emsg_cumul is not set here
Bram Moolenaar071d4272004-06-13 20:20:40 +00001049 did_emsg = FALSE;
Bram Moolenaareeece9e2020-11-20 19:26:48 +01001050 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001051
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001052 if (cstack.cs_looplevel > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001053 {
1054 ++current_line;
1055
1056 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001057 * An ":endwhile", ":endfor" and ":continue" is handled here.
1058 * If we were executing commands, jump back to the ":while" or
1059 * ":for".
1060 * If we were not executing commands, decrement cs_looplevel.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001061 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001062 if (cstack.cs_lflags & (CSL_HAD_CONT | CSL_HAD_ENDLOOP))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001063 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001064 cstack.cs_lflags &= ~(CSL_HAD_CONT | CSL_HAD_ENDLOOP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001065
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001066 // Jump back to the matching ":while" or ":for". Be careful
1067 // not to use a cs_line[] from an entry that isn't a ":while"
1068 // or ":for": It would make "current_line" invalid and can
1069 // cause a crash.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001070 if (!did_emsg && !got_int && !did_throw
1071 && cstack.cs_idx >= 0
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001072 && (cstack.cs_flags[cstack.cs_idx]
1073 & (CSF_WHILE | CSF_FOR))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001074 && cstack.cs_line[cstack.cs_idx] >= 0
1075 && (cstack.cs_flags[cstack.cs_idx] & CSF_ACTIVE))
1076 {
1077 current_line = cstack.cs_line[cstack.cs_idx];
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001078 // remember we jumped there
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001079 cstack.cs_lflags |= CSL_HAD_LOOP;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001080 line_breakcheck(); // check if CTRL-C typed
Bram Moolenaar071d4272004-06-13 20:20:40 +00001081
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001082 // Check for the next breakpoint at or after the ":while"
1083 // or ":for".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001084 if (breakpoint != NULL)
1085 {
1086 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001087 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001088 fname,
1089 ((wcmd_T *)lines_ga.ga_data)[current_line].lnum-1);
1090 *dbg_tick = debug_tick;
1091 }
1092 }
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001093 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001094 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001095 // can only get here with ":endwhile" or ":endfor"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001096 if (cstack.cs_idx >= 0)
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001097 rewind_conditionals(&cstack, cstack.cs_idx - 1,
1098 CSF_WHILE | CSF_FOR, &cstack.cs_looplevel);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001099 }
1100 }
1101
1102 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001103 * For a ":while" or ":for" we need to remember the line number.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001104 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001105 else if (cstack.cs_lflags & CSL_HAD_LOOP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001106 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001107 cstack.cs_lflags &= ~CSL_HAD_LOOP;
Bram Moolenaard5053d02020-06-28 15:51:16 +02001108 cstack.cs_line[cstack.cs_idx] = current_line_before;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001109 }
1110 }
1111
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001112 // Check for the next breakpoint after a watchexpression
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01001113 if (breakpoint != NULL && has_watchexpr())
1114 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001115 *breakpoint = dbg_find_breakpoint(FALSE, fname, SOURCING_LNUM);
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01001116 *dbg_tick = debug_tick;
1117 }
1118
Bram Moolenaar071d4272004-06-13 20:20:40 +00001119 /*
1120 * When not inside any ":while" loop, clear remembered lines.
1121 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001122 if (cstack.cs_looplevel == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001123 {
1124 if (lines_ga.ga_len > 0)
1125 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001126 SOURCING_LNUM =
Bram Moolenaar071d4272004-06-13 20:20:40 +00001127 ((wcmd_T *)lines_ga.ga_data)[lines_ga.ga_len - 1].lnum;
1128 free_cmdlines(&lines_ga);
1129 }
1130 current_line = 0;
1131 }
1132
1133 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001134 * A ":finally" makes did_emsg, got_int, and did_throw pending for
1135 * being restored at the ":endtry". Reset them here and set the
1136 * ACTIVE and FINALLY flags, so that the finally clause gets executed.
1137 * This includes the case where a missing ":endif", ":endwhile" or
1138 * ":endfor" was detected by the ":finally" itself.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001139 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001140 if (cstack.cs_lflags & CSL_HAD_FINA)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001141 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001142 cstack.cs_lflags &= ~CSL_HAD_FINA;
1143 report_make_pending(cstack.cs_pending[cstack.cs_idx]
1144 & (CSTP_ERROR | CSTP_INTERRUPT | CSTP_THROW),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001145 did_throw ? (void *)current_exception : NULL);
1146 did_emsg = got_int = did_throw = FALSE;
1147 cstack.cs_flags[cstack.cs_idx] |= CSF_ACTIVE | CSF_FINALLY;
1148 }
1149
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001150 // Update global "trylevel" for recursive calls to do_cmdline() from
1151 // within this loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001152 trylevel = initial_trylevel + cstack.cs_trylevel;
1153
1154 /*
Bram Moolenaarc1762cc2007-05-10 16:56:30 +00001155 * If the outermost try conditional (across function calls and sourced
Bram Moolenaar071d4272004-06-13 20:20:40 +00001156 * files) is aborted because of an error, an interrupt, or an uncaught
1157 * exception, cancel everything. If it is left normally, reset
1158 * force_abort to get the non-EH compatible abortion behavior for
1159 * the rest of the script.
1160 */
1161 if (trylevel == 0 && !did_emsg && !got_int && !did_throw)
1162 force_abort = FALSE;
1163
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001164 // Convert an interrupt to an exception if appropriate.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001165 (void)do_intthrow(&cstack);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001166#endif // FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001167
1168 }
1169 /*
1170 * Continue executing command lines when:
1171 * - no CTRL-C typed, no aborting error, no exception thrown or try
1172 * conditionals need to be checked for executing finally clauses or
1173 * catching an interrupt exception
1174 * - didn't get an error message or lines are not typed
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001175 * - there is a command after '|', inside a :if, :while, :for or :try, or
Bram Moolenaar071d4272004-06-13 20:20:40 +00001176 * looping for ":source" command or function call.
1177 */
1178 while (!((got_int
1179#ifdef FEAT_EVAL
1180 || (did_emsg && force_abort) || did_throw
1181#endif
1182 )
1183#ifdef FEAT_EVAL
1184 && cstack.cs_trylevel == 0
1185#endif
1186 )
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001187 && !(did_emsg
1188#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001189 // Keep going when inside try/catch, so that the error can be
1190 // deal with, except when it is a syntax error, it may cause
1191 // the :endtry to be missed.
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001192 && (cstack.cs_trylevel == 0 || did_emsg_syntax)
1193#endif
1194 && used_getline
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001195 && (getline_equal(fgetline, cookie, getexmodeline)
1196 || getline_equal(fgetline, cookie, getexline)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001197 && (next_cmdline != NULL
1198#ifdef FEAT_EVAL
1199 || cstack.cs_idx >= 0
1200#endif
1201 || (flags & DOCMD_REPEAT)));
1202
1203 vim_free(cmdline_copy);
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001204 did_emsg_syntax = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001205#ifdef FEAT_EVAL
1206 free_cmdlines(&lines_ga);
1207 ga_clear(&lines_ga);
1208
1209 if (cstack.cs_idx >= 0)
1210 {
1211 /*
1212 * If a sourced file or executed function ran to its end, report the
1213 * unclosed conditional.
1214 */
1215 if (!got_int && !did_throw
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001216 && ((getline_equal(fgetline, cookie, getsourceline)
1217 && !source_finished(fgetline, cookie))
1218 || (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001219 && !func_has_ended(real_cookie))))
1220 {
1221 if (cstack.cs_flags[cstack.cs_idx] & CSF_TRY)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001222 emsg(_(e_endtry));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001223 else if (cstack.cs_flags[cstack.cs_idx] & CSF_WHILE)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001224 emsg(_(e_endwhile));
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001225 else if (cstack.cs_flags[cstack.cs_idx] & CSF_FOR)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001226 emsg(_(e_endfor));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001227 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001228 emsg(_(e_endif));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001229 }
1230
1231 /*
1232 * Reset "trylevel" in case of a ":finish" or ":return" or a missing
1233 * ":endtry" in a sourced file or executed function. If the try
1234 * conditional is in its finally clause, ignore anything pending.
1235 * If it is in a catch clause, finish the caught exception.
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001236 * Also cleanup any "cs_forinfo" structures.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001237 */
1238 do
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001239 {
1240 int idx = cleanup_conditionals(&cstack, 0, TRUE);
1241
Bram Moolenaar89e5d682005-01-17 22:06:23 +00001242 if (idx >= 0)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001243 --idx; // remove try block not in its finally clause
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001244 rewind_conditionals(&cstack, idx, CSF_WHILE | CSF_FOR,
1245 &cstack.cs_looplevel);
1246 }
1247 while (cstack.cs_idx >= 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001248 trylevel = initial_trylevel;
1249 }
1250
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001251 // If a missing ":endtry", ":endwhile", ":endfor", or ":endif" or a memory
1252 // lack was reported above and the error message is to be converted to an
1253 // exception, do this now after rewinding the cstack.
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001254 do_errthrow(&cstack, getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001255 ? (char_u *)"endfunction" : (char_u *)NULL);
1256
1257 if (trylevel == 0)
1258 {
Bram Moolenaar820d55a2020-10-10 15:05:23 +02001259 // Just in case did_throw got set but current_exception wasn't.
1260 if (current_exception == NULL)
1261 did_throw = FALSE;
1262
Bram Moolenaar071d4272004-06-13 20:20:40 +00001263 /*
1264 * When an exception is being thrown out of the outermost try
1265 * conditional, discard the uncaught exception, disable the conversion
1266 * of interrupts or errors to exceptions, and ensure that no more
1267 * commands are executed.
1268 */
1269 if (did_throw)
1270 {
1271 void *p = NULL;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02001272 msglist_T *messages = NULL, *next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001273
1274 /*
1275 * If the uncaught exception is a user exception, report it as an
1276 * error. If it is an error exception, display the saved error
1277 * message now. For an interrupt exception, do nothing; the
1278 * interrupt message is given elsewhere.
1279 */
1280 switch (current_exception->type)
1281 {
1282 case ET_USER:
Bram Moolenaar9c13b352005-05-19 20:53:52 +00001283 vim_snprintf((char *)IObuff, IOSIZE,
1284 _("E605: Exception not caught: %s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001285 current_exception->value);
1286 p = vim_strsave(IObuff);
1287 break;
1288 case ET_ERROR:
1289 messages = current_exception->messages;
1290 current_exception->messages = NULL;
1291 break;
1292 case ET_INTERRUPT:
1293 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001294 }
1295
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001296 estack_push(ETYPE_EXCEPT, current_exception->throw_name,
1297 current_exception->throw_lnum);
Bram Moolenaare31ee862020-01-07 20:59:34 +01001298 ESTACK_CHECK_SETUP
Bram Moolenaar071d4272004-06-13 20:20:40 +00001299 current_exception->throw_name = NULL;
1300
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001301 discard_current_exception(); // uses IObuff if 'verbose'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001302 suppress_errthrow = TRUE;
1303 force_abort = TRUE;
1304
1305 if (messages != NULL)
1306 {
1307 do
1308 {
1309 next = messages->next;
1310 emsg(messages->msg);
1311 vim_free(messages->msg);
Bram Moolenaar5fbf3bc2020-06-01 21:13:11 +02001312 vim_free(messages->sfile);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001313 vim_free(messages);
1314 messages = next;
1315 }
1316 while (messages != NULL);
1317 }
1318 else if (p != NULL)
1319 {
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01001320 emsg(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001321 vim_free(p);
1322 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001323 vim_free(SOURCING_NAME);
Bram Moolenaare31ee862020-01-07 20:59:34 +01001324 ESTACK_CHECK_NOW
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001325 estack_pop();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001326 }
1327
1328 /*
1329 * On an interrupt or an aborting error not converted to an exception,
1330 * disable the conversion of errors to exceptions. (Interrupts are not
Bram Moolenaar2196bce2020-04-12 20:01:11 +02001331 * converted anymore, here.) This enables also the interrupt message
Bram Moolenaar071d4272004-06-13 20:20:40 +00001332 * when force_abort is set and did_emsg unset in case of an interrupt
1333 * from a finally clause after an error.
1334 */
1335 else if (got_int || (did_emsg && force_abort))
1336 suppress_errthrow = TRUE;
1337 }
1338
1339 /*
1340 * The current cstack will be freed when do_cmdline() returns. An uncaught
1341 * exception will have to be rethrown in the previous cstack. If a function
1342 * has just returned or a script file was just finished and the previous
1343 * cstack belongs to the same function or, respectively, script file, it
1344 * will have to be checked for finally clauses to be executed due to the
1345 * ":return" or ":finish". This is done in do_one_cmd().
1346 */
1347 if (did_throw)
1348 need_rethrow = TRUE;
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001349 if ((getline_equal(fgetline, cookie, getsourceline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001350 && ex_nesting_level > source_level(real_cookie))
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001351 || (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001352 && ex_nesting_level > func_level(real_cookie) + 1))
1353 {
1354 if (!did_throw)
1355 check_cstack = TRUE;
1356 }
1357 else
1358 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001359 // When leaving a function, reduce nesting level.
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001360 if (getline_equal(fgetline, cookie, get_func_line))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001361 --ex_nesting_level;
1362 /*
1363 * Go to debug mode when returning from a function in which we are
1364 * single-stepping.
1365 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001366 if ((getline_equal(fgetline, cookie, getsourceline)
1367 || getline_equal(fgetline, cookie, get_func_line))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001368 && ex_nesting_level + 1 <= debug_break_level)
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001369 do_debug(getline_equal(fgetline, cookie, getsourceline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001370 ? (char_u *)_("End of sourced file")
1371 : (char_u *)_("End of function"));
1372 }
1373
1374 /*
1375 * Restore the exception environment (done after returning from the
1376 * debugger).
1377 */
1378 if (flags & DOCMD_EXCRESET)
Bram Moolenaared203462004-06-16 11:19:22 +00001379 restore_dbg_stuff(&debug_saved);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001380
1381 msg_list = saved_msg_list;
Bram Moolenaar292b90d2020-03-18 15:23:16 +01001382
1383 // Cleanup if "cs_emsg_silent_list" remains.
1384 if (cstack.cs_emsg_silent_list != NULL)
1385 {
1386 eslist_T *elem, *temp;
1387
1388 for (elem = cstack.cs_emsg_silent_list; elem != NULL; elem = temp)
1389 {
1390 temp = elem->next;
1391 vim_free(elem);
1392 }
1393 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001394#endif // FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001395
1396 /*
1397 * If there was too much output to fit on the command line, ask the user to
1398 * hit return before redrawing the screen. With the ":global" command we do
1399 * this only once after the command is finished.
1400 */
1401 if (did_inc)
1402 {
1403 --RedrawingDisabled;
1404 --no_wait_return;
1405 msg_scroll = FALSE;
1406
1407 /*
1408 * When just finished an ":if"-":else" which was typed, no need to
1409 * wait for hit-return. Also for an error situation.
1410 */
1411 if (retval == FAIL
1412#ifdef FEAT_EVAL
1413 || (did_endif && KeyTyped && !did_emsg)
1414#endif
1415 )
1416 {
1417 need_wait_return = FALSE;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001418 msg_didany = FALSE; // don't wait when restarting edit
Bram Moolenaar071d4272004-06-13 20:20:40 +00001419 }
1420 else if (need_wait_return)
1421 {
1422 /*
1423 * The msg_start() above clears msg_didout. The wait_return we do
1424 * here should not overwrite the command that may be shown before
1425 * doing that.
1426 */
1427 msg_didout |= msg_didout_before_start;
1428 wait_return(FALSE);
1429 }
1430 }
1431
Bram Moolenaar2a942252012-11-28 23:03:07 +01001432#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001433 did_endif = FALSE; // in case do_cmdline used recursively
Bram Moolenaar2a942252012-11-28 23:03:07 +01001434#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001435 /*
1436 * Reset if_level, in case a sourced script file contains more ":if" than
1437 * ":endif" (could be ":if x | foo | endif").
1438 */
1439 if_level = 0;
Bram Moolenaar2e18a122012-11-28 19:10:54 +01001440#endif
Bram Moolenaard4ad0d42012-11-28 17:34:48 +01001441
Bram Moolenaar071d4272004-06-13 20:20:40 +00001442 --call_depth;
1443 return retval;
1444}
1445
1446#ifdef FEAT_EVAL
1447/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001448 * Obtain a line when inside a ":while" or ":for" loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001449 */
1450 static char_u *
Bram Moolenaar20b23c62020-08-20 15:25:00 +02001451get_loop_line(int c, void *cookie, int indent, getline_opt_T options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001452{
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001453 struct loop_cookie *cp = (struct loop_cookie *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001454 wcmd_T *wp;
1455 char_u *line;
1456
1457 if (cp->current_line + 1 >= cp->lines_gap->ga_len)
1458 {
1459 if (cp->repeating)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001460 return NULL; // trying to read past ":endwhile"/":endfor"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001461
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001462 // First time inside the ":while"/":for": get line normally.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001463 if (cp->getline == NULL)
Bram Moolenaar20b23c62020-08-20 15:25:00 +02001464 line = getcmdline(c, 0L, indent, options);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001465 else
Bram Moolenaar20b23c62020-08-20 15:25:00 +02001466 line = cp->getline(c, cp->cookie, indent, options);
Bram Moolenaard68071d2006-05-02 22:08:30 +00001467 if (line != NULL && store_loop_line(cp->lines_gap, line) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001468 ++cp->current_line;
1469
1470 return line;
1471 }
1472
1473 KeyTyped = FALSE;
1474 ++cp->current_line;
1475 wp = (wcmd_T *)(cp->lines_gap->ga_data) + cp->current_line;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001476 SOURCING_LNUM = wp->lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001477 return vim_strsave(wp->line);
1478}
1479
1480/*
1481 * Store a line in "gap" so that a ":while" loop can execute it again.
1482 */
1483 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001484store_loop_line(garray_T *gap, char_u *line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001485{
1486 if (ga_grow(gap, 1) == FAIL)
1487 return FAIL;
1488 ((wcmd_T *)(gap->ga_data))[gap->ga_len].line = vim_strsave(line);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001489 ((wcmd_T *)(gap->ga_data))[gap->ga_len].lnum = SOURCING_LNUM;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001490 ++gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001491 return OK;
1492}
1493
1494/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001495 * Free the lines stored for a ":while" or ":for" loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001496 */
1497 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001498free_cmdlines(garray_T *gap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001499{
1500 while (gap->ga_len > 0)
1501 {
1502 vim_free(((wcmd_T *)(gap->ga_data))[gap->ga_len - 1].line);
1503 --gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001504 }
1505}
1506#endif
1507
1508/*
Bram Moolenaar89d40322006-08-29 15:30:07 +00001509 * If "fgetline" is get_loop_line(), return TRUE if the getline it uses equals
1510 * "func". * Otherwise return TRUE when "fgetline" equals "func".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001511 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001512 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001513getline_equal(
Bram Moolenaar66250c92020-08-20 15:02:42 +02001514 char_u *(*fgetline)(int, void *, int, getline_opt_T),
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001515 void *cookie UNUSED, // argument for fgetline()
Bram Moolenaar66250c92020-08-20 15:02:42 +02001516 char_u *(*func)(int, void *, int, getline_opt_T))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001517{
1518#ifdef FEAT_EVAL
Bram Moolenaar20b23c62020-08-20 15:25:00 +02001519 char_u *(*gp)(int, void *, int, getline_opt_T);
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001520 struct loop_cookie *cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001521
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001522 // When "fgetline" is "get_loop_line()" use the "cookie" to find the
1523 // function that's originally used to obtain the lines. This may be
1524 // nested several levels.
Bram Moolenaar89d40322006-08-29 15:30:07 +00001525 gp = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001526 cp = (struct loop_cookie *)cookie;
1527 while (gp == get_loop_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001528 {
1529 gp = cp->getline;
1530 cp = cp->cookie;
1531 }
1532 return gp == func;
1533#else
Bram Moolenaar89d40322006-08-29 15:30:07 +00001534 return fgetline == func;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001535#endif
1536}
1537
Bram Moolenaar071d4272004-06-13 20:20:40 +00001538/*
Bram Moolenaar89d40322006-08-29 15:30:07 +00001539 * If "fgetline" is get_loop_line(), return the cookie used by the original
Bram Moolenaar071d4272004-06-13 20:20:40 +00001540 * getline function. Otherwise return "cookie".
1541 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001542 void *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001543getline_cookie(
Bram Moolenaar66250c92020-08-20 15:02:42 +02001544 char_u *(*fgetline)(int, void *, int, getline_opt_T) UNUSED,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001545 void *cookie) // argument for fgetline()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001546{
Bram Moolenaar13505972019-01-24 15:04:48 +01001547#ifdef FEAT_EVAL
Bram Moolenaar20b23c62020-08-20 15:25:00 +02001548 char_u *(*gp)(int, void *, int, getline_opt_T);
Bram Moolenaard5053d02020-06-28 15:51:16 +02001549 struct loop_cookie *cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001550
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001551 // When "fgetline" is "get_loop_line()" use the "cookie" to find the
1552 // cookie that's originally used to obtain the lines. This may be nested
1553 // several levels.
Bram Moolenaar89d40322006-08-29 15:30:07 +00001554 gp = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001555 cp = (struct loop_cookie *)cookie;
1556 while (gp == get_loop_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001557 {
1558 gp = cp->getline;
1559 cp = cp->cookie;
1560 }
1561 return cp;
Bram Moolenaar13505972019-01-24 15:04:48 +01001562#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001563 return cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001564#endif
Bram Moolenaar13505972019-01-24 15:04:48 +01001565}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001566
Bram Moolenaard5053d02020-06-28 15:51:16 +02001567#if defined(FEAT_EVAL) || defined(PROT)
1568/*
1569 * Get the next line source line without advancing.
1570 */
1571 char_u *
1572getline_peek(
Bram Moolenaar66250c92020-08-20 15:02:42 +02001573 char_u *(*fgetline)(int, void *, int, getline_opt_T) UNUSED,
Bram Moolenaard5053d02020-06-28 15:51:16 +02001574 void *cookie) // argument for fgetline()
1575{
Bram Moolenaar20b23c62020-08-20 15:25:00 +02001576 char_u *(*gp)(int, void *, int, getline_opt_T);
Bram Moolenaard5053d02020-06-28 15:51:16 +02001577 struct loop_cookie *cp;
1578 wcmd_T *wp;
1579
1580 // When "fgetline" is "get_loop_line()" use the "cookie" to find the
1581 // cookie that's originally used to obtain the lines. This may be nested
1582 // several levels.
1583 gp = fgetline;
1584 cp = (struct loop_cookie *)cookie;
1585 while (gp == get_loop_line)
1586 {
1587 if (cp->current_line + 1 < cp->lines_gap->ga_len)
1588 {
1589 // executing lines a second time, use the stored copy
1590 wp = (wcmd_T *)(cp->lines_gap->ga_data) + cp->current_line + 1;
1591 return wp->line;
1592 }
1593 gp = cp->getline;
1594 cp = cp->cookie;
1595 }
1596 if (gp == getsourceline)
1597 return source_nextline(cp);
1598 return NULL;
1599}
1600#endif
1601
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001602
1603/*
1604 * Helper function to apply an offset for buffer commands, i.e. ":bdelete",
1605 * ":bwipeout", etc.
1606 * Returns the buffer number.
1607 */
1608 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001609compute_buffer_local_count(int addr_type, int lnum, int offset)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001610{
1611 buf_T *buf;
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001612 buf_T *nextbuf;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001613 int count = offset;
1614
1615 buf = firstbuf;
1616 while (buf->b_next != NULL && buf->b_fnum < lnum)
1617 buf = buf->b_next;
1618 while (count != 0)
1619 {
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001620 count += (offset < 0) ? 1 : -1;
1621 nextbuf = (offset < 0) ? buf->b_prev : buf->b_next;
1622 if (nextbuf == NULL)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001623 break;
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001624 buf = nextbuf;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001625 if (addr_type == ADDR_LOADED_BUFFERS)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001626 // skip over unloaded buffers
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001627 while (buf->b_ml.ml_mfp == NULL)
1628 {
1629 nextbuf = (offset < 0) ? buf->b_prev : buf->b_next;
1630 if (nextbuf == NULL)
1631 break;
1632 buf = nextbuf;
1633 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001634 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001635 // we might have gone too far, last buffer is not loadedd
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001636 if (addr_type == ADDR_LOADED_BUFFERS)
1637 while (buf->b_ml.ml_mfp == NULL)
1638 {
1639 nextbuf = (offset >= 0) ? buf->b_prev : buf->b_next;
1640 if (nextbuf == NULL)
1641 break;
1642 buf = nextbuf;
1643 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001644 return buf->b_fnum;
1645}
1646
Bram Moolenaarb7316892019-05-01 18:08:42 +02001647/*
1648 * Return the window number of "win".
1649 * When "win" is NULL return the number of windows.
1650 */
Bram Moolenaarf240e182014-11-27 18:33:02 +01001651 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001652current_win_nr(win_T *win)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001653{
1654 win_T *wp;
1655 int nr = 0;
1656
Bram Moolenaar29323592016-07-24 22:04:11 +02001657 FOR_ALL_WINDOWS(wp)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001658 {
1659 ++nr;
1660 if (wp == win)
1661 break;
1662 }
1663 return nr;
1664}
1665
1666 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001667current_tab_nr(tabpage_T *tab)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001668{
1669 tabpage_T *tp;
1670 int nr = 0;
1671
Bram Moolenaar29323592016-07-24 22:04:11 +02001672 FOR_ALL_TABPAGES(tp)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001673 {
1674 ++nr;
1675 if (tp == tab)
1676 break;
1677 }
1678 return nr;
1679}
1680
Bram Moolenaar6e36b1c2020-07-17 20:47:51 +02001681 static int
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001682comment_start(char_u *p, int starts_with_colon UNUSED)
1683{
1684#ifdef FEAT_EVAL
1685 if (in_vim9script())
Bram Moolenaar93f82cb2020-12-12 21:25:56 +01001686 return p[0] == '#' && !starts_with_colon;
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001687#endif
1688 return *p == '"';
1689}
1690
Bram Moolenaarf240e182014-11-27 18:33:02 +01001691# define CURRENT_WIN_NR current_win_nr(curwin)
1692# define LAST_WIN_NR current_win_nr(NULL)
1693# define CURRENT_TAB_NR current_tab_nr(curtab)
1694# define LAST_TAB_NR current_tab_nr(NULL)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001695
Bram Moolenaar071d4272004-06-13 20:20:40 +00001696/*
1697 * Execute one Ex command.
1698 *
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01001699 * If "flags" has DOCMD_VERBOSE, the command will be included in the error
1700 * message.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001701 *
1702 * 1. skip comment lines and leading space
1703 * 2. handle command modifiers
Bram Moolenaar1c40a662014-11-27 16:38:11 +01001704 * 3. find the command
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001705 * 4. parse range
Bram Moolenaar1c40a662014-11-27 16:38:11 +01001706 * 5. Parse the command.
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001707 * 6. parse arguments
1708 * 7. switch on command name
Bram Moolenaar071d4272004-06-13 20:20:40 +00001709 *
Bram Moolenaar89d40322006-08-29 15:30:07 +00001710 * Note: "fgetline" can be NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001711 *
1712 * This function may be called recursively!
1713 */
1714#if (_MSC_VER == 1200)
1715/*
Bram Moolenaared203462004-06-16 11:19:22 +00001716 * Avoid optimisation bug in VC++ version 6.0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001717 */
Bram Moolenaar281bdce2005-01-25 21:53:18 +00001718 #pragma optimize( "g", off )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001719#endif
1720 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001721do_one_cmd(
Bram Moolenaarddef1292019-12-16 17:10:33 +01001722 char_u **cmdlinep,
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01001723 int flags,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001724#ifdef FEAT_EVAL
Bram Moolenaarddef1292019-12-16 17:10:33 +01001725 cstack_T *cstack,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001726#endif
Bram Moolenaar20b23c62020-08-20 15:25:00 +02001727 char_u *(*fgetline)(int, void *, int, getline_opt_T),
Bram Moolenaarddef1292019-12-16 17:10:33 +01001728 void *cookie) // argument for fgetline()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001729{
Bram Moolenaarddef1292019-12-16 17:10:33 +01001730 char_u *p;
1731 linenr_T lnum;
1732 long n;
1733 char *errormsg = NULL; // error message
1734 char_u *after_modifier = NULL;
1735 exarg_T ea; // Ex command arguments
Bram Moolenaarddef1292019-12-16 17:10:33 +01001736 cmdmod_T save_cmdmod;
1737 int save_reg_executing = reg_executing;
1738 int ni; // set when Not Implemented
1739 char_u *cmd;
Bram Moolenaar6e36b1c2020-07-17 20:47:51 +02001740 int starts_with_colon = FALSE;
Bram Moolenaar83f37b92020-02-23 14:35:01 +01001741#ifdef FEAT_EVAL
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02001742 int may_have_range;
Bram Moolenaar3d48e252020-07-15 14:15:52 +02001743 int vim9script = in_vim9script();
Bram Moolenaarb4bcea42020-10-28 13:53:50 +01001744 int did_set_expr_line = FALSE;
Bram Moolenaar83f37b92020-02-23 14:35:01 +01001745#endif
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01001746 int sourcing = flags & DOCMD_VERBOSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001747
Bram Moolenaara80faa82020-04-12 19:37:17 +02001748 CLEAR_FIELD(ea);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001749 ea.line1 = 1;
1750 ea.line2 = 1;
1751#ifdef FEAT_EVAL
1752 ++ex_nesting_level;
1753#endif
1754
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001755 // When the last file has not been edited :q has to be typed twice.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001756 if (quitmore
1757#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001758 // avoid that a function call in 'statusline' does this
Bram Moolenaar89d40322006-08-29 15:30:07 +00001759 && !getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001760#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001761 // avoid that an autocommand, e.g. QuitPre, does this
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001762 && !getline_equal(fgetline, cookie, getnextac))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001763 --quitmore;
1764
1765 /*
1766 * Reset browse, confirm, etc.. They are restored when returning, for
1767 * recursive calls.
1768 */
1769 save_cmdmod = cmdmod;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001770
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001771 // "#!anything" is handled like a comment.
Bram Moolenaarcbb37ad2006-08-16 15:04:21 +00001772 if ((*cmdlinep)[0] == '#' && (*cmdlinep)[1] == '!')
1773 goto doend;
1774
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02001775/*
1776 * 1. Skip comment lines and leading white space and colons.
1777 * 2. Handle command modifiers.
1778 */
1779 // The "ea" structure holds the arguments that can be used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001780 ea.cmd = *cmdlinep;
Bram Moolenaareffed932018-08-14 13:38:17 +02001781 ea.cmdlinep = cmdlinep;
1782 ea.getline = fgetline;
1783 ea.cookie = cookie;
1784#ifdef FEAT_EVAL
1785 ea.cstack = cstack;
Bram Moolenaar83f37b92020-02-23 14:35:01 +01001786 starts_with_colon = *skipwhite(ea.cmd) == ':';
Bram Moolenaar071d4272004-06-13 20:20:40 +00001787#endif
Bram Moolenaare1004402020-10-24 20:49:43 +02001788 if (parse_command_modifiers(&ea, &errormsg, &cmdmod, FALSE) == FAIL)
Bram Moolenaareffed932018-08-14 13:38:17 +02001789 goto doend;
Bram Moolenaar5661ed62020-10-24 17:19:16 +02001790 apply_cmdmod(&cmdmod);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001791
Bram Moolenaar7b668e82016-08-23 23:51:21 +02001792 after_modifier = ea.cmd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001793
1794#ifdef FEAT_EVAL
1795 ea.skip = did_emsg || got_int || did_throw || (cstack->cs_idx >= 0
1796 && !(cstack->cs_flags[cstack->cs_idx] & CSF_ACTIVE));
1797#else
1798 ea.skip = (if_level > 0);
1799#endif
1800
Bram Moolenaar071d4272004-06-13 20:20:40 +00001801/*
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001802 * 3. Skip over the range to find the command. Let "p" point to after it.
1803 *
1804 * We need the command to know what kind of range it uses.
1805 */
1806 cmd = ea.cmd;
Bram Moolenaar3d48e252020-07-15 14:15:52 +02001807#ifdef FEAT_EVAL
Bram Moolenaare88c8e82020-11-01 17:03:37 +01001808 // In Vim9 script a colon is required before the range. This may also be
1809 // after command modifiers.
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01001810 if (vim9script && (flags & DOCMD_RANGEOK) == 0)
Bram Moolenaare88c8e82020-11-01 17:03:37 +01001811 {
1812 may_have_range = FALSE;
1813 for (p = ea.cmd; p >= *cmdlinep; --p)
1814 {
1815 if (*p == ':')
1816 may_have_range = TRUE;
1817 if (p < ea.cmd && !VIM_ISWHITE(*p))
1818 break;
1819 }
1820 }
1821 else
1822 may_have_range = TRUE;
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02001823 if (may_have_range)
Bram Moolenaar3d48e252020-07-15 14:15:52 +02001824#endif
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02001825 ea.cmd = skip_range(ea.cmd, TRUE, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001826
1827#ifdef FEAT_EVAL
Bram Moolenaare88c8e82020-11-01 17:03:37 +01001828 if (vim9script && !may_have_range)
Bram Moolenaardf069ee2020-06-22 23:02:51 +02001829 {
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02001830 if (ea.cmd == cmd + 1 && *cmd == '$')
1831 // should be "$VAR = val"
1832 --ea.cmd;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001833 p = find_ex_command(&ea, NULL, lookup_scriptvar, NULL);
Bram Moolenaar36113e42020-11-02 21:08:47 +01001834 if (ea.cmdidx == CMD_SIZE)
1835 {
1836 char_u *ar = skip_range(ea.cmd, TRUE, NULL);
1837
1838 // If a ':' before the range is missing, give a clearer error
1839 // message.
1840 if (ar > ea.cmd)
1841 {
1842 emsg(_(e_colon_required_before_a_range));
1843 goto doend;
1844 }
1845 }
Bram Moolenaardf069ee2020-06-22 23:02:51 +02001846 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001847 else
1848#endif
1849 p = find_ex_command(&ea, NULL, NULL, NULL);
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001850
Bram Moolenaar7feb35e2018-08-21 17:49:54 +02001851#ifdef FEAT_EVAL
1852# ifdef FEAT_PROFILE
1853 // Count this line for profiling if skip is TRUE.
1854 if (do_profiling == PROF_YES
1855 && (!ea.skip || cstack->cs_idx == 0 || (cstack->cs_idx > 0
1856 && (cstack->cs_flags[cstack->cs_idx - 1] & CSF_ACTIVE))))
1857 {
1858 int skip = did_emsg || got_int || did_throw;
1859
1860 if (ea.cmdidx == CMD_catch)
1861 skip = !skip && !(cstack->cs_idx >= 0
1862 && (cstack->cs_flags[cstack->cs_idx] & CSF_THROWN)
1863 && !(cstack->cs_flags[cstack->cs_idx] & CSF_CAUGHT));
1864 else if (ea.cmdidx == CMD_else || ea.cmdidx == CMD_elseif)
1865 skip = skip || !(cstack->cs_idx >= 0
1866 && !(cstack->cs_flags[cstack->cs_idx]
1867 & (CSF_ACTIVE | CSF_TRUE)));
1868 else if (ea.cmdidx == CMD_finally)
1869 skip = FALSE;
1870 else if (ea.cmdidx != CMD_endif
1871 && ea.cmdidx != CMD_endfor
1872 && ea.cmdidx != CMD_endtry
1873 && ea.cmdidx != CMD_endwhile)
1874 skip = ea.skip;
1875
1876 if (!skip)
1877 {
1878 if (getline_equal(fgetline, cookie, get_func_line))
1879 func_line_exec(getline_cookie(fgetline, cookie));
1880 else if (getline_equal(fgetline, cookie, getsourceline))
1881 script_line_exec();
1882 }
1883 }
1884# endif
1885
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001886 // May go to debug mode. If this happens and the ">quit" debug command is
1887 // used, throw an interrupt exception and skip the next command.
Bram Moolenaar7feb35e2018-08-21 17:49:54 +02001888 dbg_check_breakpoint(&ea);
1889 if (!ea.skip && got_int)
1890 {
1891 ea.skip = TRUE;
1892 (void)do_intthrow(cstack);
1893 }
1894#endif
1895
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001896/*
1897 * 4. parse a range specifier of the form: addr [,addr] [;addr] ..
Bram Moolenaar071d4272004-06-13 20:20:40 +00001898 *
1899 * where 'addr' is:
1900 *
1901 * % (entire file)
1902 * $ [+-NUM]
1903 * 'x [+-NUM] (where x denotes a currently defined mark)
1904 * . [+-NUM]
1905 * [+-NUM]..
1906 * NUM
1907 *
1908 * The ea.cmd pointer is updated to point to the first character following the
1909 * range spec. If an initial address is found, but no second, the upper bound
1910 * is equal to the lower.
1911 */
1912
Bram Moolenaar25190db2019-05-04 15:05:28 +02001913 // ea.addr_type for user commands is set by find_ucmd
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01001914 if (!IS_USER_CMDIDX(ea.cmdidx))
1915 {
1916 if (ea.cmdidx != CMD_SIZE)
1917 ea.addr_type = cmdnames[(int)ea.cmdidx].cmd_addr_type;
1918 else
1919 ea.addr_type = ADDR_LINES;
1920
Bram Moolenaar25190db2019-05-04 15:05:28 +02001921 // :wincmd range depends on the argument.
Bram Moolenaar164f3262015-01-14 21:22:01 +01001922 if (ea.cmdidx == CMD_wincmd && p != NULL)
1923 get_wincmd_addr_type(skipwhite(p), &ea);
Bram Moolenaar25190db2019-05-04 15:05:28 +02001924#ifdef FEAT_QUICKFIX
1925 // :.cc in quickfix window uses line number
1926 if ((ea.cmdidx == CMD_cc || ea.cmdidx == CMD_ll) && bt_quickfix(curbuf))
1927 ea.addr_type = ADDR_OTHER;
1928#endif
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01001929 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001930
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01001931 ea.cmd = cmd;
Bram Moolenaar3d48e252020-07-15 14:15:52 +02001932#ifdef FEAT_EVAL
Bram Moolenaarc2af0af2020-08-23 21:06:02 +02001933 if (!may_have_range)
1934 ea.line1 = ea.line2 = default_address(&ea);
1935 else
Bram Moolenaar3d48e252020-07-15 14:15:52 +02001936#endif
1937 if (parse_cmd_address(&ea, &errormsg, FALSE) == FAIL)
1938 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001939
Bram Moolenaar071d4272004-06-13 20:20:40 +00001940/*
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001941 * 5. Parse the command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001942 */
1943
1944 /*
1945 * Skip ':' and any white space
1946 */
1947 ea.cmd = skipwhite(ea.cmd);
1948 while (*ea.cmd == ':')
1949 ea.cmd = skipwhite(ea.cmd + 1);
1950
1951 /*
1952 * If we got a line, but no command, then go to the line.
1953 * If we find a '|' or '\n' we set ea.nextcmd.
1954 */
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001955 if (*ea.cmd == NUL || comment_start(ea.cmd, starts_with_colon)
1956 || (ea.nextcmd = check_nextcmd(ea.cmd)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001957 {
1958 /*
1959 * strange vi behaviour:
1960 * ":3" jumps to line 3
1961 * ":3|..." prints line 3
1962 * ":|" prints current line
1963 */
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001964 if (ea.skip) // skip this if inside :if
Bram Moolenaar071d4272004-06-13 20:20:40 +00001965 goto doend;
Bram Moolenaardf177f62005-02-22 08:39:57 +00001966 if (*ea.cmd == '|' || (exmode_active && ea.line1 != ea.line2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001967 {
1968 ea.cmdidx = CMD_print;
Bram Moolenaar8071cb22019-07-12 17:58:01 +02001969 ea.argt = EX_RANGE+EX_COUNT+EX_TRLBAR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001970 if ((errormsg = invalid_range(&ea)) == NULL)
1971 {
1972 correct_range(&ea);
1973 ex_print(&ea);
1974 }
1975 }
1976 else if (ea.addr_count != 0)
1977 {
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00001978 if (ea.line2 > curbuf->b_ml.ml_line_count)
1979 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001980 // With '-' in 'cpoptions' a line number past the file is an
1981 // error, otherwise put it at the end of the file.
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00001982 if (vim_strchr(p_cpo, CPO_MINUS) != NULL)
1983 ea.line2 = -1;
1984 else
1985 ea.line2 = curbuf->b_ml.ml_line_count;
1986 }
1987
1988 if (ea.line2 < 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001989 errormsg = _(e_invrange);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001990 else
1991 {
1992 if (ea.line2 == 0)
1993 curwin->w_cursor.lnum = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001994 else
1995 curwin->w_cursor.lnum = ea.line2;
1996 beginline(BL_SOL | BL_FIX);
1997 }
1998 }
1999 goto doend;
2000 }
2001
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002002 // If this looks like an undefined user command and there are CmdUndefined
2003 // autocommands defined, trigger the matching autocommands.
Bram Moolenaard5005162014-08-22 23:05:54 +02002004 if (p != NULL && ea.cmdidx == CMD_SIZE && !ea.skip
2005 && ASCII_ISUPPER(*ea.cmd)
2006 && has_cmdundefined())
2007 {
Bram Moolenaard5005162014-08-22 23:05:54 +02002008 int ret;
2009
Bram Moolenaar5a31b462014-08-23 14:16:20 +02002010 p = ea.cmd;
Bram Moolenaard5005162014-08-22 23:05:54 +02002011 while (ASCII_ISALNUM(*p))
2012 ++p;
Bram Moolenaar158ea172020-06-18 17:28:39 +02002013 p = vim_strnsave(ea.cmd, p - ea.cmd);
Bram Moolenaard5005162014-08-22 23:05:54 +02002014 ret = apply_autocmds(EVENT_CMDUNDEFINED, p, p, TRUE, NULL);
2015 vim_free(p);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002016 // If the autocommands did something and didn't cause an error, try
2017 // finding the command again.
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002018 p = (ret
2019#ifdef FEAT_EVAL
2020 && !aborting()
Bram Moolenaard5005162014-08-22 23:05:54 +02002021#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002022 ) ? find_ex_command(&ea, NULL, NULL, NULL) : ea.cmd;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002023 }
Bram Moolenaard5005162014-08-22 23:05:54 +02002024
Bram Moolenaar071d4272004-06-13 20:20:40 +00002025 if (p == NULL)
2026 {
2027 if (!ea.skip)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002028 errormsg = _("E464: Ambiguous use of user-defined command");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002029 goto doend;
2030 }
Bram Moolenaarac9fb182019-04-27 13:04:13 +02002031 // Check for wrong commands.
Bram Moolenaar6f9a4762017-06-22 20:39:17 +02002032 if (*p == '!' && ea.cmd[1] == 0151 && ea.cmd[0] == 78
2033 && !IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002034 {
2035 errormsg = uc_fun_cmd();
2036 goto doend;
2037 }
Bram Moolenaarac9fb182019-04-27 13:04:13 +02002038
Bram Moolenaar071d4272004-06-13 20:20:40 +00002039 if (ea.cmdidx == CMD_SIZE)
2040 {
2041 if (!ea.skip)
2042 {
2043 STRCPY(IObuff, _("E492: Not an editor command"));
2044 if (!sourcing)
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002045 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002046 // If the modifier was parsed OK the error must be in the
2047 // following command
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002048 if (after_modifier != NULL)
2049 append_command(after_modifier);
2050 else
2051 append_command(*cmdlinep);
2052 }
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002053 errormsg = (char *)IObuff;
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02002054 did_emsg_syntax = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002055 }
2056 goto doend;
2057 }
2058
Bram Moolenaar958636c2014-10-21 20:01:58 +02002059 ni = (!IS_USER_CMDIDX(ea.cmdidx)
2060 && (cmdnames[ea.cmdidx].cmd_func == ex_ni
Bram Moolenaar7bb75552007-07-16 18:39:49 +00002061#ifdef HAVE_EX_SCRIPT_NI
2062 || cmdnames[ea.cmdidx].cmd_func == ex_script_ni
2063#endif
2064 ));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002065
2066#ifndef FEAT_EVAL
2067 /*
2068 * When the expression evaluation is disabled, recognize the ":if" and
2069 * ":endif" commands and ignore everything in between it.
2070 */
2071 if (ea.cmdidx == CMD_if)
2072 ++if_level;
2073 if (if_level)
2074 {
2075 if (ea.cmdidx == CMD_endif)
2076 --if_level;
2077 goto doend;
2078 }
2079
2080#endif
2081
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002082 // forced commands
Bram Moolenaar196b3b02008-06-20 16:51:41 +00002083 if (*p == '!' && ea.cmdidx != CMD_substitute
2084 && ea.cmdidx != CMD_smagic && ea.cmdidx != CMD_snomagic)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002085 {
2086 ++p;
2087 ea.forceit = TRUE;
2088 }
2089 else
2090 ea.forceit = FALSE;
2091
2092/*
Bram Moolenaarb7316892019-05-01 18:08:42 +02002093 * 6. Parse arguments. Then check for errors.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002094 */
Bram Moolenaar958636c2014-10-21 20:01:58 +02002095 if (!IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002096 ea.argt = (long)cmdnames[(int)ea.cmdidx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002097
2098 if (!ea.skip)
2099 {
2100#ifdef HAVE_SANDBOX
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002101 if (sandbox != 0 && !(ea.argt & EX_SBOXOK))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002102 {
Bram Moolenaar8c62a082019-02-08 14:34:10 +01002103 // Command not allowed in sandbox.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002104 errormsg = _(e_sandbox);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002105 goto doend;
2106 }
2107#endif
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002108 if (restricted != 0 && (ea.argt & EX_RESTRICT))
Bram Moolenaar8c62a082019-02-08 14:34:10 +01002109 {
2110 errormsg = _("E981: Command not allowed in rvim");
2111 goto doend;
2112 }
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002113 if (!curbuf->b_p_ma && (ea.argt & EX_MODIFY))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002114 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002115 // Command not allowed in non-'modifiable' buffer
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002116 errormsg = _(e_modifiable);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002117 goto doend;
2118 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002119
Bram Moolenaar37394ff2020-07-25 19:38:18 +02002120 if (!IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002121 {
Bram Moolenaar37394ff2020-07-25 19:38:18 +02002122#ifdef FEAT_CMDWIN
2123 if (cmdwin_type != 0 && !(ea.argt & EX_CMDWIN))
2124 {
2125 // Command not allowed in the command line window
2126 errormsg = _(e_cmdwin);
2127 goto doend;
2128 }
2129#endif
2130 if (text_locked() && !(ea.argt & EX_LOCK_OK))
2131 {
2132 // Command not allowed when text is locked
2133 errormsg = _(get_text_locked_msg());
2134 goto doend;
2135 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002136 }
Bram Moolenaar379fb762018-08-30 15:58:28 +02002137
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002138 // Disallow editing another buffer when "curbuf_lock" is set.
2139 // Do allow ":checktime" (it is postponed).
2140 // Do allow ":edit" (check for an argument later).
2141 // Do allow ":file" with no arguments (check for an argument later).
Bram Moolenaar8e1986e2020-08-06 22:11:06 +02002142 if (!(ea.argt & (EX_CMDWIN | EX_LOCK_OK))
Bram Moolenaar5555acc2006-04-07 21:33:12 +00002143 && ea.cmdidx != CMD_checktime
Bram Moolenaar379fb762018-08-30 15:58:28 +02002144 && ea.cmdidx != CMD_edit
2145 && ea.cmdidx != CMD_file
Bram Moolenaar958636c2014-10-21 20:01:58 +02002146 && !IS_USER_CMDIDX(ea.cmdidx)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002147 && curbuf_locked())
2148 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002149
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002150 if (!ni && !(ea.argt & EX_RANGE) && ea.addr_count > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002151 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002152 // no range allowed
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002153 errormsg = _(e_norange);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002154 goto doend;
2155 }
2156 }
2157
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002158 if (!ni && !(ea.argt & EX_BANG) && ea.forceit) // no <!> allowed
Bram Moolenaar071d4272004-06-13 20:20:40 +00002159 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002160 errormsg = _(e_nobang);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002161 goto doend;
2162 }
2163
2164 /*
2165 * Don't complain about the range if it is not used
2166 * (could happen if line_count is accidentally set to 0).
2167 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002168 if (!ea.skip && !ni && (ea.argt & EX_RANGE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002169 {
2170 /*
2171 * If the range is backwards, ask for confirmation and, if given, swap
2172 * ea.line1 & ea.line2 so it's forwards again.
2173 * When global command is busy, don't ask, will fail below.
2174 */
2175 if (!global_busy && ea.line1 > ea.line2)
2176 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002177 if (msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002178 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002179 if (sourcing || exmode_active)
2180 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002181 errormsg = _("E493: Backwards range given");
Bram Moolenaara5792f52005-11-23 21:25:05 +00002182 goto doend;
2183 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002184 if (ask_yesno((char_u *)
2185 _("Backwards range given, OK to swap"), FALSE) != 'y')
Bram Moolenaara5792f52005-11-23 21:25:05 +00002186 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002187 }
2188 lnum = ea.line1;
2189 ea.line1 = ea.line2;
2190 ea.line2 = lnum;
2191 }
2192 if ((errormsg = invalid_range(&ea)) != NULL)
2193 goto doend;
2194 }
2195
Bram Moolenaarb7316892019-05-01 18:08:42 +02002196 if ((ea.addr_type == ADDR_OTHER) && ea.addr_count == 0)
2197 // default is 1, not cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00002198 ea.line2 = 1;
2199
2200 correct_range(&ea);
2201
2202#ifdef FEAT_FOLDING
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002203 if (((ea.argt & EX_WHOLEFOLD) || ea.addr_count >= 2) && !global_busy
Bram Moolenaara3306952016-01-02 21:41:06 +01002204 && ea.addr_type == ADDR_LINES)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002205 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002206 // Put the first line at the start of a closed fold, put the last line
2207 // at the end of a closed fold.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002208 (void)hasFolding(ea.line1, &ea.line1, NULL);
2209 (void)hasFolding(ea.line2, NULL, &ea.line2);
2210 }
2211#endif
2212
2213#ifdef FEAT_QUICKFIX
2214 /*
Bram Moolenaar86b68352004-12-27 21:59:20 +00002215 * For the ":make" and ":grep" commands we insert the 'makeprg'/'grepprg'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002216 * option here, so things like % get expanded.
2217 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002218 p = replace_makeprg(&ea, p, cmdlinep);
2219 if (p == NULL)
2220 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002221#endif
2222
2223 /*
2224 * Skip to start of argument.
2225 * Don't do this for the ":!" command, because ":!! -l" needs the space.
2226 */
2227 if (ea.cmdidx == CMD_bang)
2228 ea.arg = p;
2229 else
2230 ea.arg = skipwhite(p);
2231
Bram Moolenaar379fb762018-08-30 15:58:28 +02002232 // ":file" cannot be run with an argument when "curbuf_lock" is set
2233 if (ea.cmdidx == CMD_file && *ea.arg != NUL && curbuf_locked())
2234 goto doend;
2235
Bram Moolenaar071d4272004-06-13 20:20:40 +00002236 /*
2237 * Check for "++opt=val" argument.
2238 * Must be first, allow ":w ++enc=utf8 !cmd"
2239 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002240 if (ea.argt & EX_ARGOPT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002241 while (ea.arg[0] == '+' && ea.arg[1] == '+')
2242 if (getargopt(&ea) == FAIL && !ni)
2243 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002244 errormsg = _(e_invarg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002245 goto doend;
2246 }
2247
2248 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
2249 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002250 if (*ea.arg == '>') // append
Bram Moolenaar071d4272004-06-13 20:20:40 +00002251 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002252 if (*++ea.arg != '>') // typed wrong
Bram Moolenaar071d4272004-06-13 20:20:40 +00002253 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002254 errormsg = _("E494: Use w or w>>");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002255 goto doend;
2256 }
2257 ea.arg = skipwhite(ea.arg + 1);
2258 ea.append = TRUE;
2259 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002260 else if (*ea.arg == '!' && ea.cmdidx == CMD_write) // :w !filter
Bram Moolenaar071d4272004-06-13 20:20:40 +00002261 {
2262 ++ea.arg;
2263 ea.usefilter = TRUE;
2264 }
2265 }
2266
2267 if (ea.cmdidx == CMD_read)
2268 {
2269 if (ea.forceit)
2270 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002271 ea.usefilter = TRUE; // :r! filter if ea.forceit
Bram Moolenaar071d4272004-06-13 20:20:40 +00002272 ea.forceit = FALSE;
2273 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002274 else if (*ea.arg == '!') // :r !filter
Bram Moolenaar071d4272004-06-13 20:20:40 +00002275 {
2276 ++ea.arg;
2277 ea.usefilter = TRUE;
2278 }
2279 }
2280
2281 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
2282 {
2283 ea.amount = 1;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002284 while (*ea.arg == *ea.cmd) // count number of '>' or '<'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002285 {
2286 ++ea.arg;
2287 ++ea.amount;
2288 }
2289 ea.arg = skipwhite(ea.arg);
2290 }
2291
2292 /*
2293 * Check for "+command" argument, before checking for next command.
2294 * Don't do this for ":read !cmd" and ":write !cmd".
2295 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002296 if ((ea.argt & EX_CMDARG) && !ea.usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002297 ea.do_ecmd_cmd = getargcmd(&ea.arg);
2298
2299 /*
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002300 * Check for '|' to separate commands and '"' or '#' to start comments.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002301 * Don't do this for ":read !cmd" and ":write !cmd".
2302 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002303 if ((ea.argt & EX_TRLBAR) && !ea.usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002304 separate_nextcmd(&ea);
2305
2306 /*
2307 * Check for <newline> to end a shell command.
Bram Moolenaardf177f62005-02-22 08:39:57 +00002308 * Also do this for ":read !cmd", ":write !cmd" and ":global".
2309 * Any others?
Bram Moolenaar071d4272004-06-13 20:20:40 +00002310 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00002311 else if (ea.cmdidx == CMD_bang
Bram Moolenaar67883b42017-07-27 22:57:00 +02002312 || ea.cmdidx == CMD_terminal
Bram Moolenaardf177f62005-02-22 08:39:57 +00002313 || ea.cmdidx == CMD_global
2314 || ea.cmdidx == CMD_vglobal
2315 || ea.usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002316 {
2317 for (p = ea.arg; *p; ++p)
2318 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002319 // Remove one backslash before a newline, so that it's possible to
2320 // pass a newline to the shell and also a newline that is preceded
2321 // with a backslash. This makes it impossible to end a shell
2322 // command in a backslash, but that doesn't appear useful.
2323 // Halving the number of backslashes is incompatible with previous
2324 // versions.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002325 if (*p == '\\' && p[1] == '\n')
Bram Moolenaara7241f52008-06-24 20:39:31 +00002326 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002327 else if (*p == '\n')
2328 {
2329 ea.nextcmd = p + 1;
2330 *p = NUL;
2331 break;
2332 }
2333 }
2334 }
2335
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002336 if ((ea.argt & EX_DFLALL) && ea.addr_count == 0)
Bram Moolenaarc2af0af2020-08-23 21:06:02 +02002337 address_default_all(&ea);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002338
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002339 // accept numbered register only when no count allowed (:put)
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002340 if ( (ea.argt & EX_REGSTR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002341 && *ea.arg != NUL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002342 // Do not allow register = for user commands
Bram Moolenaar958636c2014-10-21 20:01:58 +02002343 && (!IS_USER_CMDIDX(ea.cmdidx) || *ea.arg != '=')
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002344 && !((ea.argt & EX_COUNT) && VIM_ISDIGIT(*ea.arg)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002345 {
Bram Moolenaar85de2062011-05-05 14:26:41 +02002346#ifndef FEAT_CLIPBOARD
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002347 // check these explicitly for a more specific error message
Bram Moolenaar85de2062011-05-05 14:26:41 +02002348 if (*ea.arg == '*' || *ea.arg == '+')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002349 {
Bram Moolenaar99b12722019-01-14 20:16:40 +01002350 errormsg = _(e_invalidreg);
Bram Moolenaar85de2062011-05-05 14:26:41 +02002351 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002352 }
2353#endif
Bram Moolenaar958636c2014-10-21 20:01:58 +02002354 if (valid_yank_reg(*ea.arg, (ea.cmdidx != CMD_put
2355 && !IS_USER_CMDIDX(ea.cmdidx))))
Bram Moolenaar85de2062011-05-05 14:26:41 +02002356 {
2357 ea.regname = *ea.arg++;
2358#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002359 // for '=' register: accept the rest of the line as an expression
Bram Moolenaar85de2062011-05-05 14:26:41 +02002360 if (ea.arg[-1] == '=' && ea.arg[0] != NUL)
2361 {
Bram Moolenaarb4bcea42020-10-28 13:53:50 +01002362 set_expr_line(vim_strsave(ea.arg), &ea);
Bram Moolenaar85de2062011-05-05 14:26:41 +02002363 ea.arg += STRLEN(ea.arg);
Bram Moolenaarb4bcea42020-10-28 13:53:50 +01002364 did_set_expr_line = TRUE;
Bram Moolenaar85de2062011-05-05 14:26:41 +02002365 }
2366#endif
2367 ea.arg = skipwhite(ea.arg);
2368 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002369 }
2370
2371 /*
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002372 * Check for a count. When accepting a EX_BUFNAME, don't use "123foo" as a
Bram Moolenaar071d4272004-06-13 20:20:40 +00002373 * count, it's a buffer name.
2374 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002375 if ((ea.argt & EX_COUNT) && VIM_ISDIGIT(*ea.arg)
2376 && (!(ea.argt & EX_BUFNAME) || *(p = skipdigits(ea.arg)) == NUL
Bram Moolenaar1c465442017-03-12 20:10:05 +01002377 || VIM_ISWHITE(*p)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002378 {
2379 n = getdigits(&ea.arg);
2380 ea.arg = skipwhite(ea.arg);
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002381 if (n <= 0 && !ni && (ea.argt & EX_ZEROR) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002382 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002383 errormsg = _(e_zerocount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002384 goto doend;
2385 }
Bram Moolenaarb7316892019-05-01 18:08:42 +02002386 if (ea.addr_type != ADDR_LINES) // e.g. :buffer 2, :sleep 3
Bram Moolenaar071d4272004-06-13 20:20:40 +00002387 {
2388 ea.line2 = n;
2389 if (ea.addr_count == 0)
2390 ea.addr_count = 1;
2391 }
2392 else
2393 {
2394 ea.line1 = ea.line2;
2395 ea.line2 += n - 1;
2396 ++ea.addr_count;
2397 /*
2398 * Be vi compatible: no error message for out of range.
2399 */
Bram Moolenaarb7316892019-05-01 18:08:42 +02002400 if (ea.line2 > curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002401 ea.line2 = curbuf->b_ml.ml_line_count;
2402 }
2403 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00002404
2405 /*
2406 * Check for flags: 'l', 'p' and '#'.
2407 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002408 if (ea.argt & EX_FLAGS)
Bram Moolenaardf177f62005-02-22 08:39:57 +00002409 get_flags(&ea);
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002410 if (!ni && !(ea.argt & EX_EXTRA) && *ea.arg != NUL
2411 && *ea.arg != '"' && (*ea.arg != '|' || (ea.argt & EX_TRLBAR) == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002412 {
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002413 // no arguments allowed but there is something
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +02002414 errormsg = ex_errmsg(e_trailing_arg, ea.arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002415 goto doend;
2416 }
2417
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002418 if (!ni && (ea.argt & EX_NEEDARG) && *ea.arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002419 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002420 errormsg = _(e_argreq);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002421 goto doend;
2422 }
2423
2424#ifdef FEAT_EVAL
2425 /*
2426 * Skip the command when it's not going to be executed.
2427 * The commands like :if, :endif, etc. always need to be executed.
2428 * Also make an exception for commands that handle a trailing command
2429 * themselves.
2430 */
2431 if (ea.skip)
2432 {
2433 switch (ea.cmdidx)
2434 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002435 // commands that need evaluation
Bram Moolenaar071d4272004-06-13 20:20:40 +00002436 case CMD_while:
2437 case CMD_endwhile:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00002438 case CMD_for:
2439 case CMD_endfor:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002440 case CMD_if:
2441 case CMD_elseif:
2442 case CMD_else:
2443 case CMD_endif:
2444 case CMD_try:
2445 case CMD_catch:
2446 case CMD_finally:
2447 case CMD_endtry:
2448 case CMD_function:
Bram Moolenaarab55c682020-03-01 19:41:43 +01002449 case CMD_def:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002450 break;
2451
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002452 // Commands that handle '|' themselves. Check: A command should
2453 // either have the EX_TRLBAR flag, appear in this list or appear in
2454 // the list at ":help :bar".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002455 case CMD_aboveleft:
2456 case CMD_and:
2457 case CMD_belowright:
2458 case CMD_botright:
2459 case CMD_browse:
2460 case CMD_call:
2461 case CMD_confirm:
Bram Moolenaar8f76e6b2019-11-26 16:50:30 +01002462 case CMD_const:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002463 case CMD_delfunction:
2464 case CMD_djump:
2465 case CMD_dlist:
2466 case CMD_dsearch:
2467 case CMD_dsplit:
2468 case CMD_echo:
2469 case CMD_echoerr:
2470 case CMD_echomsg:
2471 case CMD_echon:
Bram Moolenaara76b3152020-02-16 16:20:21 +01002472 case CMD_eval:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002473 case CMD_execute:
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002474 case CMD_filter:
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002475 case CMD_final:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002476 case CMD_help:
2477 case CMD_hide:
2478 case CMD_ijump:
2479 case CMD_ilist:
2480 case CMD_isearch:
2481 case CMD_isplit:
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002482 case CMD_keepalt:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002483 case CMD_keepjumps:
2484 case CMD_keepmarks:
Bram Moolenaara939e432013-11-09 05:30:26 +01002485 case CMD_keeppatterns:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002486 case CMD_leftabove:
2487 case CMD_let:
2488 case CMD_lockmarks:
Bram Moolenaarcc4423a2019-11-26 17:05:00 +01002489 case CMD_lockvar:
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002490 case CMD_lua:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002491 case CMD_match:
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002492 case CMD_mzscheme:
Bram Moolenaar5803ae62014-03-23 16:04:02 +01002493 case CMD_noautocmd:
2494 case CMD_noswapfile:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002495 case CMD_perl:
2496 case CMD_psearch:
Bram Moolenaar368373e2010-07-19 20:46:22 +02002497 case CMD_py3:
Bram Moolenaarb6590522010-07-21 16:00:43 +02002498 case CMD_python3:
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002499 case CMD_python:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002500 case CMD_return:
2501 case CMD_rightbelow:
2502 case CMD_ruby:
2503 case CMD_silent:
2504 case CMD_smagic:
2505 case CMD_snomagic:
2506 case CMD_substitute:
2507 case CMD_syntax:
Bram Moolenaara226a6d2006-02-26 23:59:20 +00002508 case CMD_tab:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002509 case CMD_tcl:
2510 case CMD_throw:
2511 case CMD_tilde:
2512 case CMD_topleft:
2513 case CMD_unlet:
Bram Moolenaarcc4423a2019-11-26 17:05:00 +01002514 case CMD_unlockvar:
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002515 case CMD_var:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002516 case CMD_verbose:
2517 case CMD_vertical:
Bram Moolenaar12bde492011-06-13 01:19:56 +02002518 case CMD_wincmd:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002519 break;
2520
2521 default: goto doend;
2522 }
2523 }
2524#endif
2525
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002526 if (ea.argt & EX_XFILE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002527 {
2528 if (expand_filename(&ea, cmdlinep, &errormsg) == FAIL)
2529 goto doend;
2530 }
2531
Bram Moolenaar071d4272004-06-13 20:20:40 +00002532 /*
2533 * Accept buffer name. Cannot be used at the same time with a buffer
2534 * number. Don't do this for a user command.
2535 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002536 if ((ea.argt & EX_BUFNAME) && *ea.arg != NUL && ea.addr_count == 0
Bram Moolenaar958636c2014-10-21 20:01:58 +02002537 && !IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002538 {
2539 /*
2540 * :bdelete, :bwipeout and :bunload take several arguments, separated
2541 * by spaces: find next space (skipping over escaped characters).
2542 * The others take one argument: ignore trailing spaces.
2543 */
2544 if (ea.cmdidx == CMD_bdelete || ea.cmdidx == CMD_bwipeout
2545 || ea.cmdidx == CMD_bunload)
2546 p = skiptowhite_esc(ea.arg);
2547 else
2548 {
2549 p = ea.arg + STRLEN(ea.arg);
Bram Moolenaar1c465442017-03-12 20:10:05 +01002550 while (p > ea.arg && VIM_ISWHITE(p[-1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002551 --p;
2552 }
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002553 ea.line2 = buflist_findpat(ea.arg, p, (ea.argt & EX_BUFUNL) != 0,
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002554 FALSE, FALSE);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002555 if (ea.line2 < 0) // failed
Bram Moolenaar071d4272004-06-13 20:20:40 +00002556 goto doend;
2557 ea.addr_count = 1;
2558 ea.arg = skipwhite(p);
2559 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002560
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002561 // The :try command saves the emsg_silent flag, reset it here when
2562 // ":silent! try" was used, it should only apply to :try itself.
Bram Moolenaar5661ed62020-10-24 17:19:16 +02002563 if (ea.cmdidx == CMD_try && cmdmod.cmod_did_esilent > 0)
Bram Moolenaar2be57332018-02-13 18:05:18 +01002564 {
Bram Moolenaar5661ed62020-10-24 17:19:16 +02002565 emsg_silent -= cmdmod.cmod_did_esilent;
Bram Moolenaar2be57332018-02-13 18:05:18 +01002566 if (emsg_silent < 0)
2567 emsg_silent = 0;
Bram Moolenaar5661ed62020-10-24 17:19:16 +02002568 cmdmod.cmod_did_esilent = 0;
Bram Moolenaar2be57332018-02-13 18:05:18 +01002569 }
2570
Bram Moolenaar071d4272004-06-13 20:20:40 +00002571/*
Bram Moolenaar2be57332018-02-13 18:05:18 +01002572 * 7. Execute the command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002573 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002574
Bram Moolenaar958636c2014-10-21 20:01:58 +02002575 if (IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002576 {
2577 /*
2578 * Execute a user-defined command.
2579 */
2580 do_ucmd(&ea);
2581 }
2582 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002583 {
2584 /*
Bram Moolenaarac9fb182019-04-27 13:04:13 +02002585 * Call the function to execute the builtin command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002586 */
2587 ea.errmsg = NULL;
2588 (cmdnames[ea.cmdidx].cmd_func)(&ea);
2589 if (ea.errmsg != NULL)
Bram Moolenaar8930caa2020-07-23 16:37:03 +02002590 errormsg = ea.errmsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002591 }
2592
2593#ifdef FEAT_EVAL
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002594 // Set flag that any command was executed, used by ex_vim9script().
Bram Moolenaar16531552020-02-08 16:00:46 +01002595 if (getline_equal(ea.getline, ea.cookie, getsourceline)
2596 && current_sctx.sc_sid > 0)
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002597 SCRIPT_ITEM(current_sctx.sc_sid)->sn_had_command = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002598
Bram Moolenaar071d4272004-06-13 20:20:40 +00002599 /*
2600 * If the command just executed called do_cmdline(), any throw or ":return"
2601 * or ":finish" encountered there must also check the cstack of the still
2602 * active do_cmdline() that called this do_one_cmd(). Rethrow an uncaught
2603 * exception, or reanimate a returned function or finished script file and
2604 * return or finish it again.
2605 */
2606 if (need_rethrow)
2607 do_throw(cstack);
2608 else if (check_cstack)
2609 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002610 if (source_finished(fgetline, cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002611 do_finish(&ea, TRUE);
Bram Moolenaar89d40322006-08-29 15:30:07 +00002612 else if (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002613 && current_func_returned())
2614 do_return(&ea, TRUE, FALSE, NULL);
2615 }
2616 need_rethrow = check_cstack = FALSE;
2617#endif
2618
2619doend:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002620 if (curwin->w_cursor.lnum == 0) // can happen with zero line number
Bram Moolenaar6de5e122017-04-20 21:55:44 +02002621 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002622 curwin->w_cursor.lnum = 1;
Bram Moolenaar6de5e122017-04-20 21:55:44 +02002623 curwin->w_cursor.col = 0;
2624 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002625
2626 if (errormsg != NULL && *errormsg != NUL && !did_emsg)
2627 {
2628 if (sourcing)
2629 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002630 if (errormsg != (char *)IObuff)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002631 {
2632 STRCPY(IObuff, errormsg);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002633 errormsg = (char *)IObuff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002634 }
Bram Moolenaara6f4d612011-09-21 19:10:46 +02002635 append_command(*cmdlinep);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002636 }
2637 emsg(errormsg);
2638 }
2639#ifdef FEAT_EVAL
2640 do_errthrow(cstack,
Bram Moolenaar958636c2014-10-21 20:01:58 +02002641 (ea.cmdidx != CMD_SIZE && !IS_USER_CMDIDX(ea.cmdidx))
2642 ? cmdnames[(int)ea.cmdidx].cmd_name : (char_u *)NULL);
Bram Moolenaarb4bcea42020-10-28 13:53:50 +01002643
2644 if (did_set_expr_line)
2645 set_expr_line(NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002646#endif
2647
Bram Moolenaare1004402020-10-24 20:49:43 +02002648 undo_cmdmod(&cmdmod);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002649 cmdmod = save_cmdmod;
Bram Moolenaar9a2c0912019-03-30 14:26:18 +01002650 reg_executing = save_reg_executing;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002651
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002652 if (ea.nextcmd && *ea.nextcmd == NUL) // not really a next command
Bram Moolenaar071d4272004-06-13 20:20:40 +00002653 ea.nextcmd = NULL;
2654
2655#ifdef FEAT_EVAL
2656 --ex_nesting_level;
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002657 vim_free(ea.cmdline_tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002658#endif
2659
2660 return ea.nextcmd;
2661}
2662#if (_MSC_VER == 1200)
Bram Moolenaar281bdce2005-01-25 21:53:18 +00002663 #pragma optimize( "", on )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002664#endif
2665
Bram Moolenaar8930caa2020-07-23 16:37:03 +02002666static char ex_error_buf[MSG_BUF_LEN];
2667
2668/*
2669 * Return an error message with argument included.
2670 * Uses a static buffer, only the last error will be kept.
2671 * "msg" will be translated, caller should use N_().
2672 */
2673 char *
2674ex_errmsg(char *msg, char_u *arg)
2675{
2676 vim_snprintf(ex_error_buf, MSG_BUF_LEN, _(msg), arg);
2677 return ex_error_buf;
2678}
2679
Bram Moolenaar071d4272004-06-13 20:20:40 +00002680/*
Bram Moolenaareffed932018-08-14 13:38:17 +02002681 * Parse and skip over command modifiers:
2682 * - update eap->cmd
Bram Moolenaare1004402020-10-24 20:49:43 +02002683 * - store flags in "cmod".
Bram Moolenaareffed932018-08-14 13:38:17 +02002684 * - Set ex_pressedreturn for an empty command line.
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002685 * When "skip_only" is TRUE the global variables are not changed, except for
2686 * "cmdmod".
Bram Moolenaare1004402020-10-24 20:49:43 +02002687 * When "skip_only" is FALSE then undo_cmdmod() must be called later to free
2688 * any cmod_filter_regmatch.regprog.
Bram Moolenaar5661ed62020-10-24 17:19:16 +02002689 * Call apply_cmdmod() to get the side effects of the modifiers:
2690 * - Increment "sandbox" for ":sandbox"
2691 * - set p_verbose for ":verbose"
Bram Moolenaare1004402020-10-24 20:49:43 +02002692 * - set msg_silent for ":silent"
2693 * - set 'eventignore' to "all" for ":noautocmd"
Bram Moolenaareffed932018-08-14 13:38:17 +02002694 * Return FAIL when the command is not to be executed.
2695 * May set "errormsg" to an error message.
2696 */
2697 int
Bram Moolenaare1004402020-10-24 20:49:43 +02002698parse_command_modifiers(
2699 exarg_T *eap,
2700 char **errormsg,
2701 cmdmod_T *cmod,
2702 int skip_only)
Bram Moolenaareffed932018-08-14 13:38:17 +02002703{
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002704 char_u *p;
2705 int starts_with_colon = FALSE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002706
Bram Moolenaare1004402020-10-24 20:49:43 +02002707 CLEAR_POINTER(cmod);
Bram Moolenaareffed932018-08-14 13:38:17 +02002708
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002709 // Repeat until no more command modifiers are found.
Bram Moolenaareffed932018-08-14 13:38:17 +02002710 for (;;)
2711 {
Bram Moolenaareffed932018-08-14 13:38:17 +02002712 while (*eap->cmd == ' ' || *eap->cmd == '\t' || *eap->cmd == ':')
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002713 {
2714 if (*eap->cmd == ':')
2715 starts_with_colon = TRUE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002716 ++eap->cmd;
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002717 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002718
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002719 // in ex mode, an empty line works like :+
Bram Moolenaareffed932018-08-14 13:38:17 +02002720 if (*eap->cmd == NUL && exmode_active
2721 && (getline_equal(eap->getline, eap->cookie, getexmodeline)
2722 || getline_equal(eap->getline, eap->cookie, getexline))
2723 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
2724 {
2725 eap->cmd = (char_u *)"+";
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002726 if (!skip_only)
2727 ex_pressedreturn = TRUE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002728 }
2729
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002730 // ignore comment and empty lines
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002731 if (comment_start(eap->cmd, starts_with_colon))
Bram Moolenaareffed932018-08-14 13:38:17 +02002732 return FAIL;
2733 if (*eap->cmd == NUL)
2734 {
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002735 if (!skip_only)
2736 ex_pressedreturn = TRUE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002737 return FAIL;
2738 }
2739
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02002740 p = skip_range(eap->cmd, TRUE, NULL);
Bram Moolenaareffed932018-08-14 13:38:17 +02002741 switch (*p)
2742 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002743 // When adding an entry, also modify cmd_exists().
Bram Moolenaareffed932018-08-14 13:38:17 +02002744 case 'a': if (!checkforcmd(&eap->cmd, "aboveleft", 3))
2745 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02002746 cmod->cmod_split |= WSP_ABOVE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002747 continue;
2748
2749 case 'b': if (checkforcmd(&eap->cmd, "belowright", 3))
2750 {
Bram Moolenaare1004402020-10-24 20:49:43 +02002751 cmod->cmod_split |= WSP_BELOW;
Bram Moolenaareffed932018-08-14 13:38:17 +02002752 continue;
2753 }
2754 if (checkforcmd(&eap->cmd, "browse", 3))
2755 {
2756#ifdef FEAT_BROWSE_CMD
Bram Moolenaare1004402020-10-24 20:49:43 +02002757 cmod->cmod_flags |= CMOD_BROWSE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002758#endif
2759 continue;
2760 }
2761 if (!checkforcmd(&eap->cmd, "botright", 2))
2762 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02002763 cmod->cmod_split |= WSP_BOT;
Bram Moolenaareffed932018-08-14 13:38:17 +02002764 continue;
2765
2766 case 'c': if (!checkforcmd(&eap->cmd, "confirm", 4))
2767 break;
2768#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaare1004402020-10-24 20:49:43 +02002769 cmod->cmod_flags |= CMOD_CONFIRM;
Bram Moolenaareffed932018-08-14 13:38:17 +02002770#endif
2771 continue;
2772
2773 case 'k': if (checkforcmd(&eap->cmd, "keepmarks", 3))
2774 {
Bram Moolenaare1004402020-10-24 20:49:43 +02002775 cmod->cmod_flags |= CMOD_KEEPMARKS;
Bram Moolenaareffed932018-08-14 13:38:17 +02002776 continue;
2777 }
2778 if (checkforcmd(&eap->cmd, "keepalt", 5))
2779 {
Bram Moolenaare1004402020-10-24 20:49:43 +02002780 cmod->cmod_flags |= CMOD_KEEPALT;
Bram Moolenaareffed932018-08-14 13:38:17 +02002781 continue;
2782 }
2783 if (checkforcmd(&eap->cmd, "keeppatterns", 5))
2784 {
Bram Moolenaare1004402020-10-24 20:49:43 +02002785 cmod->cmod_flags |= CMOD_KEEPPATTERNS;
Bram Moolenaareffed932018-08-14 13:38:17 +02002786 continue;
2787 }
2788 if (!checkforcmd(&eap->cmd, "keepjumps", 5))
2789 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02002790 cmod->cmod_flags |= CMOD_KEEPJUMPS;
Bram Moolenaareffed932018-08-14 13:38:17 +02002791 continue;
2792
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002793 case 'f': // only accept ":filter {pat} cmd"
Bram Moolenaareffed932018-08-14 13:38:17 +02002794 {
2795 char_u *reg_pat;
2796
2797 if (!checkforcmd(&p, "filter", 4)
2798 || *p == NUL || ends_excmd(*p))
2799 break;
2800 if (*p == '!')
2801 {
Bram Moolenaare1004402020-10-24 20:49:43 +02002802 cmod->cmod_filter_force = TRUE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002803 p = skipwhite(p + 1);
2804 if (*p == NUL || ends_excmd(*p))
2805 break;
2806 }
Bram Moolenaar1e624c92020-07-11 14:08:04 +02002807#ifdef FEAT_EVAL
Bram Moolenaarb074e8b2020-07-11 13:40:45 +02002808 // Avoid that "filter(arg)" is recognized.
Bram Moolenaar4f6b6ed2020-10-29 20:24:34 +01002809 if (in_vim9script() && !VIM_ISWHITE(p[-1]))
Bram Moolenaarb074e8b2020-07-11 13:40:45 +02002810 break;
Bram Moolenaar1e624c92020-07-11 14:08:04 +02002811#endif
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002812 if (skip_only)
2813 p = skip_vimgrep_pat(p, NULL, NULL);
2814 else
2815 // NOTE: This puts a NUL after the pattern.
2816 p = skip_vimgrep_pat(p, &reg_pat, NULL);
Bram Moolenaareffed932018-08-14 13:38:17 +02002817 if (p == NULL || *p == NUL)
2818 break;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002819 if (!skip_only)
2820 {
Bram Moolenaare1004402020-10-24 20:49:43 +02002821 cmod->cmod_filter_regmatch.regprog =
Bram Moolenaareffed932018-08-14 13:38:17 +02002822 vim_regcomp(reg_pat, RE_MAGIC);
Bram Moolenaare1004402020-10-24 20:49:43 +02002823 if (cmod->cmod_filter_regmatch.regprog == NULL)
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002824 break;
2825 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002826 eap->cmd = p;
2827 continue;
2828 }
2829
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002830 // ":hide" and ":hide | cmd" are not modifiers
Bram Moolenaareffed932018-08-14 13:38:17 +02002831 case 'h': if (p != eap->cmd || !checkforcmd(&p, "hide", 3)
2832 || *p == NUL || ends_excmd(*p))
2833 break;
2834 eap->cmd = p;
Bram Moolenaare1004402020-10-24 20:49:43 +02002835 cmod->cmod_flags |= CMOD_HIDE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002836 continue;
2837
2838 case 'l': if (checkforcmd(&eap->cmd, "lockmarks", 3))
2839 {
Bram Moolenaare1004402020-10-24 20:49:43 +02002840 cmod->cmod_flags |= CMOD_LOCKMARKS;
Bram Moolenaareffed932018-08-14 13:38:17 +02002841 continue;
2842 }
2843
2844 if (!checkforcmd(&eap->cmd, "leftabove", 5))
2845 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02002846 cmod->cmod_split |= WSP_ABOVE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002847 continue;
2848
2849 case 'n': if (checkforcmd(&eap->cmd, "noautocmd", 3))
2850 {
Bram Moolenaare1004402020-10-24 20:49:43 +02002851 cmod->cmod_flags |= CMOD_NOAUTOCMD;
Bram Moolenaareffed932018-08-14 13:38:17 +02002852 continue;
2853 }
2854 if (!checkforcmd(&eap->cmd, "noswapfile", 3))
2855 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02002856 cmod->cmod_flags |= CMOD_NOSWAPFILE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002857 continue;
2858
2859 case 'r': if (!checkforcmd(&eap->cmd, "rightbelow", 6))
2860 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02002861 cmod->cmod_split |= WSP_BELOW;
Bram Moolenaareffed932018-08-14 13:38:17 +02002862 continue;
2863
2864 case 's': if (checkforcmd(&eap->cmd, "sandbox", 3))
2865 {
Bram Moolenaare1004402020-10-24 20:49:43 +02002866 cmod->cmod_flags |= CMOD_SANDBOX;
Bram Moolenaareffed932018-08-14 13:38:17 +02002867 continue;
2868 }
2869 if (!checkforcmd(&eap->cmd, "silent", 3))
2870 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02002871 cmod->cmod_flags |= CMOD_SILENT;
Bram Moolenaareffed932018-08-14 13:38:17 +02002872 if (*eap->cmd == '!' && !VIM_ISWHITE(eap->cmd[-1]))
2873 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002874 // ":silent!", but not "silent !cmd"
Bram Moolenaareffed932018-08-14 13:38:17 +02002875 eap->cmd = skipwhite(eap->cmd + 1);
Bram Moolenaare1004402020-10-24 20:49:43 +02002876 cmod->cmod_flags |= CMOD_ERRSILENT;
Bram Moolenaareffed932018-08-14 13:38:17 +02002877 }
2878 continue;
2879
2880 case 't': if (checkforcmd(&p, "tab", 3))
2881 {
Bram Moolenaar548e5982018-12-26 21:45:00 +01002882 if (!skip_only)
Bram Moolenaareffed932018-08-14 13:38:17 +02002883 {
Bram Moolenaar548e5982018-12-26 21:45:00 +01002884 long tabnr = get_address(eap, &eap->cmd,
2885 ADDR_TABS, eap->skip,
2886 skip_only, FALSE, 1);
2887 if (tabnr == MAXLNUM)
Bram Moolenaare1004402020-10-24 20:49:43 +02002888 cmod->cmod_tab = tabpage_index(curtab) + 1;
Bram Moolenaar548e5982018-12-26 21:45:00 +01002889 else
Bram Moolenaareffed932018-08-14 13:38:17 +02002890 {
Bram Moolenaar548e5982018-12-26 21:45:00 +01002891 if (tabnr < 0 || tabnr > LAST_TAB_NR)
2892 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002893 *errormsg = _(e_invrange);
Bram Moolenaar548e5982018-12-26 21:45:00 +01002894 return FAIL;
2895 }
Bram Moolenaare1004402020-10-24 20:49:43 +02002896 cmod->cmod_tab = tabnr + 1;
Bram Moolenaareffed932018-08-14 13:38:17 +02002897 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002898 }
2899 eap->cmd = p;
2900 continue;
2901 }
2902 if (!checkforcmd(&eap->cmd, "topleft", 2))
2903 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02002904 cmod->cmod_split |= WSP_TOP;
Bram Moolenaareffed932018-08-14 13:38:17 +02002905 continue;
2906
2907 case 'u': if (!checkforcmd(&eap->cmd, "unsilent", 3))
2908 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02002909 cmod->cmod_flags |= CMOD_UNSILENT;
Bram Moolenaareffed932018-08-14 13:38:17 +02002910 continue;
2911
2912 case 'v': if (checkforcmd(&eap->cmd, "vertical", 4))
2913 {
Bram Moolenaare1004402020-10-24 20:49:43 +02002914 cmod->cmod_split |= WSP_VERT;
Bram Moolenaareffed932018-08-14 13:38:17 +02002915 continue;
2916 }
2917 if (!checkforcmd(&p, "verbose", 4))
2918 break;
Bram Moolenaar5661ed62020-10-24 17:19:16 +02002919 if (vim_isdigit(*eap->cmd))
Bram Moolenaare1004402020-10-24 20:49:43 +02002920 cmod->cmod_verbose = atoi((char *)eap->cmd);
Bram Moolenaar5661ed62020-10-24 17:19:16 +02002921 else
Bram Moolenaare1004402020-10-24 20:49:43 +02002922 cmod->cmod_verbose = 1;
Bram Moolenaareffed932018-08-14 13:38:17 +02002923 eap->cmd = p;
2924 continue;
2925 }
2926 break;
2927 }
2928
2929 return OK;
2930}
2931
2932/*
Bram Moolenaar5661ed62020-10-24 17:19:16 +02002933 * Apply the command modifiers. Saves current state in "cmdmod", call
2934 * undo_cmdmod() later.
2935 */
2936 void
2937apply_cmdmod(cmdmod_T *cmod)
2938{
2939#ifdef HAVE_SANDBOX
2940 if ((cmod->cmod_flags & CMOD_SANDBOX) && !cmod->cmod_did_sandbox)
2941 {
2942 ++sandbox;
2943 cmod->cmod_did_sandbox = TRUE;
2944 }
2945#endif
2946 if (cmod->cmod_verbose > 0)
2947 {
2948 if (cmod->cmod_verbose_save == 0)
2949 cmod->cmod_verbose_save = p_verbose + 1;
2950 p_verbose = cmod->cmod_verbose;
2951 }
2952
2953 if ((cmod->cmod_flags & (CMOD_SILENT | CMOD_UNSILENT))
2954 && cmod->cmod_save_msg_silent == 0)
Bram Moolenaare1004402020-10-24 20:49:43 +02002955 {
Bram Moolenaar5661ed62020-10-24 17:19:16 +02002956 cmod->cmod_save_msg_silent = msg_silent + 1;
Bram Moolenaare1004402020-10-24 20:49:43 +02002957 cmod->cmod_save_msg_scroll = msg_scroll;
2958 }
Bram Moolenaar5661ed62020-10-24 17:19:16 +02002959 if (cmod->cmod_flags & CMOD_SILENT)
2960 ++msg_silent;
2961 if (cmod->cmod_flags & CMOD_UNSILENT)
2962 msg_silent = 0;
2963
2964 if (cmod->cmod_flags & CMOD_ERRSILENT)
2965 {
2966 ++emsg_silent;
2967 ++cmod->cmod_did_esilent;
2968 }
2969
Bram Moolenaare1004402020-10-24 20:49:43 +02002970 if ((cmod->cmod_flags & CMOD_NOAUTOCMD) && cmod->cmod_save_ei == NULL)
Bram Moolenaar5661ed62020-10-24 17:19:16 +02002971 {
2972 // Set 'eventignore' to "all".
2973 // First save the existing option value for restoring it later.
Bram Moolenaare1004402020-10-24 20:49:43 +02002974 cmod->cmod_save_ei = vim_strsave(p_ei);
Bram Moolenaar5661ed62020-10-24 17:19:16 +02002975 set_string_option_direct((char_u *)"ei", -1,
2976 (char_u *)"all", OPT_FREE, SID_NONE);
2977 }
2978}
2979
2980/*
Bram Moolenaare1004402020-10-24 20:49:43 +02002981 * Undo and free contents of "cmod".
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002982 */
Bram Moolenaar47e7d702020-07-05 18:18:42 +02002983 void
Bram Moolenaare1004402020-10-24 20:49:43 +02002984undo_cmdmod(cmdmod_T *cmod)
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002985{
Bram Moolenaare1004402020-10-24 20:49:43 +02002986 if (cmod->cmod_verbose_save > 0)
Bram Moolenaar5661ed62020-10-24 17:19:16 +02002987 {
Bram Moolenaare1004402020-10-24 20:49:43 +02002988 p_verbose = cmod->cmod_verbose_save - 1;
2989 cmod->cmod_verbose_save = 0;
Bram Moolenaar5661ed62020-10-24 17:19:16 +02002990 }
Bram Moolenaar47e7d702020-07-05 18:18:42 +02002991
Bram Moolenaar5661ed62020-10-24 17:19:16 +02002992#ifdef HAVE_SANDBOX
Bram Moolenaare1004402020-10-24 20:49:43 +02002993 if (cmod->cmod_did_sandbox)
Bram Moolenaar5661ed62020-10-24 17:19:16 +02002994 {
2995 --sandbox;
Bram Moolenaare1004402020-10-24 20:49:43 +02002996 cmod->cmod_did_sandbox = FALSE;
Bram Moolenaar5661ed62020-10-24 17:19:16 +02002997 }
2998#endif
2999
Bram Moolenaare1004402020-10-24 20:49:43 +02003000 if (cmod->cmod_save_ei != NULL)
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02003001 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003002 // Restore 'eventignore' to the value before ":noautocmd".
Bram Moolenaare1004402020-10-24 20:49:43 +02003003 set_string_option_direct((char_u *)"ei", -1, cmod->cmod_save_ei,
3004 OPT_FREE, SID_NONE);
3005 free_string_option(cmod->cmod_save_ei);
3006 cmod->cmod_save_ei = NULL;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02003007 }
3008
Bram Moolenaarcaf73dc2020-10-26 21:39:13 +01003009 vim_regfree(cmod->cmod_filter_regmatch.regprog);
Bram Moolenaar47e7d702020-07-05 18:18:42 +02003010
Bram Moolenaare1004402020-10-24 20:49:43 +02003011 if (cmod->cmod_save_msg_silent > 0)
Bram Moolenaar47e7d702020-07-05 18:18:42 +02003012 {
3013 // messages could be enabled for a serious error, need to check if the
3014 // counters don't become negative
Bram Moolenaare1004402020-10-24 20:49:43 +02003015 if (!did_emsg || msg_silent > cmod->cmod_save_msg_silent - 1)
3016 msg_silent = cmod->cmod_save_msg_silent - 1;
3017 emsg_silent -= cmod->cmod_did_esilent;
Bram Moolenaar47e7d702020-07-05 18:18:42 +02003018 if (emsg_silent < 0)
3019 emsg_silent = 0;
3020 // Restore msg_scroll, it's set by file I/O commands, even when no
3021 // message is actually displayed.
Bram Moolenaare1004402020-10-24 20:49:43 +02003022 msg_scroll = cmod->cmod_save_msg_scroll;
Bram Moolenaar47e7d702020-07-05 18:18:42 +02003023
3024 // "silent reg" or "silent echo x" inside "redir" leaves msg_col
3025 // somewhere in the line. Put it back in the first column.
3026 if (redirecting())
3027 msg_col = 0;
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003028
Bram Moolenaare1004402020-10-24 20:49:43 +02003029 cmod->cmod_save_msg_silent = 0;
3030 cmod->cmod_did_esilent = 0;
Bram Moolenaar47e7d702020-07-05 18:18:42 +02003031 }
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02003032}
3033
3034/*
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003035 * Parse the address range, if any, in "eap".
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02003036 * May set the last search pattern, unless "silent" is TRUE.
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003037 * Return FAIL and set "errormsg" or return OK.
3038 */
3039 int
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003040parse_cmd_address(exarg_T *eap, char **errormsg, int silent)
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003041{
3042 int address_count = 1;
3043 linenr_T lnum;
3044
3045 // Repeat for all ',' or ';' separated addresses.
3046 for (;;)
3047 {
3048 eap->line1 = eap->line2;
Bram Moolenaarc2af0af2020-08-23 21:06:02 +02003049 eap->line2 = default_address(eap);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003050 eap->cmd = skipwhite(eap->cmd);
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02003051 lnum = get_address(eap, &eap->cmd, eap->addr_type, eap->skip, silent,
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003052 eap->addr_count == 0, address_count++);
3053 if (eap->cmd == NULL) // error detected
3054 return FAIL;
3055 if (lnum == MAXLNUM)
3056 {
3057 if (*eap->cmd == '%') // '%' - all lines
3058 {
3059 ++eap->cmd;
3060 switch (eap->addr_type)
3061 {
3062 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02003063 case ADDR_OTHER:
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003064 eap->line1 = 1;
3065 eap->line2 = curbuf->b_ml.ml_line_count;
3066 break;
3067 case ADDR_LOADED_BUFFERS:
3068 {
3069 buf_T *buf = firstbuf;
3070
3071 while (buf->b_next != NULL
3072 && buf->b_ml.ml_mfp == NULL)
3073 buf = buf->b_next;
3074 eap->line1 = buf->b_fnum;
3075 buf = lastbuf;
3076 while (buf->b_prev != NULL
3077 && buf->b_ml.ml_mfp == NULL)
3078 buf = buf->b_prev;
3079 eap->line2 = buf->b_fnum;
3080 break;
3081 }
3082 case ADDR_BUFFERS:
3083 eap->line1 = firstbuf->b_fnum;
3084 eap->line2 = lastbuf->b_fnum;
3085 break;
3086 case ADDR_WINDOWS:
3087 case ADDR_TABS:
3088 if (IS_USER_CMDIDX(eap->cmdidx))
3089 {
3090 eap->line1 = 1;
3091 eap->line2 = eap->addr_type == ADDR_WINDOWS
3092 ? LAST_WIN_NR : LAST_TAB_NR;
3093 }
3094 else
3095 {
3096 // there is no Vim command which uses '%' and
3097 // ADDR_WINDOWS or ADDR_TABS
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003098 *errormsg = _(e_invrange);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003099 return FAIL;
3100 }
3101 break;
3102 case ADDR_TABS_RELATIVE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02003103 case ADDR_UNSIGNED:
3104 case ADDR_QUICKFIX:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003105 *errormsg = _(e_invrange);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003106 return FAIL;
3107 case ADDR_ARGUMENTS:
3108 if (ARGCOUNT == 0)
3109 eap->line1 = eap->line2 = 0;
3110 else
3111 {
3112 eap->line1 = 1;
3113 eap->line2 = ARGCOUNT;
3114 }
3115 break;
Bram Moolenaar25190db2019-05-04 15:05:28 +02003116 case ADDR_QUICKFIX_VALID:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003117#ifdef FEAT_QUICKFIX
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003118 eap->line1 = 1;
Bram Moolenaar25190db2019-05-04 15:05:28 +02003119 eap->line2 = qf_get_valid_size(eap);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003120 if (eap->line2 == 0)
3121 eap->line2 = 1;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003122#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003123 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02003124 case ADDR_NONE:
3125 // Will give an error later if a range is found.
3126 break;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003127 }
3128 ++eap->addr_count;
3129 }
3130 else if (*eap->cmd == '*' && vim_strchr(p_cpo, CPO_STAR) == NULL)
3131 {
3132 pos_T *fp;
3133
3134 // '*' - visual area
3135 if (eap->addr_type != ADDR_LINES)
3136 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003137 *errormsg = _(e_invrange);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003138 return FAIL;
3139 }
3140
3141 ++eap->cmd;
3142 if (!eap->skip)
3143 {
3144 fp = getmark('<', FALSE);
3145 if (check_mark(fp) == FAIL)
3146 return FAIL;
3147 eap->line1 = fp->lnum;
3148 fp = getmark('>', FALSE);
3149 if (check_mark(fp) == FAIL)
3150 return FAIL;
3151 eap->line2 = fp->lnum;
3152 ++eap->addr_count;
3153 }
3154 }
3155 }
3156 else
3157 eap->line2 = lnum;
3158 eap->addr_count++;
3159
3160 if (*eap->cmd == ';')
3161 {
3162 if (!eap->skip)
3163 {
3164 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaar0e717042020-04-27 19:29:01 +02003165 // Don't leave the cursor on an illegal line or column, but do
3166 // accept zero as address, so 0;/PATTERN/ works correctly.
3167 if (eap->line2 > 0)
3168 check_cursor();
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003169 }
3170 }
3171 else if (*eap->cmd != ',')
3172 break;
3173 ++eap->cmd;
3174 }
3175
3176 // One address given: set start and end lines.
3177 if (eap->addr_count == 1)
3178 {
3179 eap->line1 = eap->line2;
3180 // ... but only implicit: really no address given
3181 if (lnum == MAXLNUM)
3182 eap->addr_count = 0;
3183 }
3184 return OK;
3185}
3186
3187/*
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00003188 * Check for an Ex command with optional tail.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003189 * If there is a match advance "pp" to the argument and return TRUE.
3190 */
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00003191 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003192checkforcmd(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003193 char_u **pp, // start of command
3194 char *cmd, // name of command
3195 int len) // required length
Bram Moolenaar071d4272004-06-13 20:20:40 +00003196{
3197 int i;
3198
3199 for (i = 0; cmd[i] != NUL; ++i)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003200 if (((char_u *)cmd)[i] != (*pp)[i])
Bram Moolenaar071d4272004-06-13 20:20:40 +00003201 break;
Bram Moolenaar7a66a172020-10-16 19:56:12 +02003202 if (i >= len && !isalpha((*pp)[i]) && (*pp)[i] != '_')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003203 {
3204 *pp = skipwhite(*pp + i);
3205 return TRUE;
3206 }
3207 return FALSE;
3208}
3209
3210/*
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003211 * Append "cmd" to the error message in IObuff.
3212 * Takes care of limiting the length and handling 0xa0, which would be
3213 * invisible otherwise.
3214 */
3215 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003216append_command(char_u *cmd)
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003217{
3218 char_u *s = cmd;
3219 char_u *d;
3220
3221 STRCAT(IObuff, ": ");
3222 d = IObuff + STRLEN(IObuff);
3223 while (*s != NUL && d - IObuff < IOSIZE - 7)
3224 {
Bram Moolenaar13505972019-01-24 15:04:48 +01003225 if (enc_utf8 ? (s[0] == 0xc2 && s[1] == 0xa0) : *s == 0xa0)
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003226 {
Bram Moolenaar13505972019-01-24 15:04:48 +01003227 s += enc_utf8 ? 2 : 1;
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003228 STRCPY(d, "<a0>");
3229 d += 4;
3230 }
3231 else
3232 MB_COPY_CHAR(s, d);
3233 }
3234 *d = NUL;
3235}
3236
3237/*
Bram Moolenaar2e800952020-08-23 19:34:48 +02003238 * If "start" points "&opt", "&l:opt", "&g:opt" or "$ENV" return a pointer to
3239 * the name. Otherwise just return "start".
3240 */
3241 char_u *
3242skip_option_env_lead(char_u *start)
3243{
3244 char_u *name = start;
3245
3246 if (*start == '&')
3247 {
3248 if ((start[1] == 'l' || start[1] == 'g') && start[2] == ':')
3249 name += 3;
3250 else
3251 name += 1;
3252 }
3253 else if (*start == '$')
3254 name += 1;
3255 return name;
3256}
3257
3258/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003259 * Find an Ex command by its name, either built-in or user.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003260 * Start of the name can be found at eap->cmd.
Bram Moolenaar25190db2019-05-04 15:05:28 +02003261 * Sets eap->cmdidx and returns a pointer to char after the command name.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003262 * "full" is set to TRUE if the whole command name matched.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003263 *
3264 * If "lookup" is not NULL recognize expression without "eval" or "call" and
3265 * assignment without "let". Sets eap->cmdidx to the command while returning
3266 * "eap->cmd".
3267 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00003268 * Returns NULL for an ambiguous user command.
3269 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003270 char_u *
3271find_ex_command(
3272 exarg_T *eap,
Bram Moolenaara494f562020-04-18 17:45:38 +02003273 int *full UNUSED,
Bram Moolenaar709664c2020-12-12 14:33:41 +01003274 int (*lookup)(char_u *, size_t, void *, cctx_T *) UNUSED,
Bram Moolenaara494f562020-04-18 17:45:38 +02003275 cctx_T *cctx UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003276{
3277 int len;
3278 char_u *p;
Bram Moolenaardf177f62005-02-22 08:39:57 +00003279 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003280
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003281#ifdef FEAT_EVAL
3282 /*
3283 * Recognize a Vim9 script function/method call and assignment:
3284 * "lvar = value", "lvar(arg)", "[1, 2 3]->Func()"
3285 */
Bram Moolenaar0c6ceaf2020-02-22 18:36:32 +01003286 p = eap->cmd;
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003287 if (lookup != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003288 {
Bram Moolenaar2e800952020-08-23 19:34:48 +02003289 char_u *pskip = skip_option_env_lead(eap->cmd);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003290
Bram Moolenaar83144542020-08-02 20:40:43 +02003291 if (vim_strchr((char_u *)"{('[\"@", *p) != NULL
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003292 || ((p = to_name_const_end(pskip)) > eap->cmd && *p != NUL))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003293 {
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003294 int oplen;
3295 int heredoc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003296
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003297 if (
3298 // "(..." is an expression.
3299 // "funcname(" is always a function call.
3300 *p == '('
3301 || (p == eap->cmd
3302 ? (
Bram Moolenaar9becdf22020-10-10 21:33:48 +02003303 // "{..." is a dict expression or block start.
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003304 *eap->cmd == '{'
3305 // "'string'->func()" is an expression.
3306 || *eap->cmd == '\''
Bram Moolenaar10409562020-07-29 20:00:38 +02003307 // '"string"->func()' is an expression.
3308 || *eap->cmd == '"'
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003309 // "g:varname" is an expression.
3310 || eap->cmd[1] == ':'
3311 )
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003312 // "varname->func()" is an expression.
Bram Moolenaar2b22b112020-09-14 18:35:18 +02003313 : (*p == '-' && p[1] == '>')))
Bram Moolenaarb31be3f2020-07-20 22:37:44 +02003314 {
Bram Moolenaar9becdf22020-10-10 21:33:48 +02003315 if (*eap->cmd == '{' && ends_excmd(*skipwhite(eap->cmd + 1)))
3316 {
3317 // "{" by itself is the start of a block.
3318 eap->cmdidx = CMD_block;
3319 return eap->cmd + 1;
3320 }
Bram Moolenaarb31be3f2020-07-20 22:37:44 +02003321 eap->cmdidx = CMD_eval;
3322 return eap->cmd;
3323 }
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003324
Bram Moolenaar2b22b112020-09-14 18:35:18 +02003325 if (p != eap->cmd && (
3326 // "varname[]" is an expression.
3327 *p == '['
3328 // "varname.key" is an expression.
3329 || (*p == '.' && ASCII_ISALPHA(p[1]))))
3330 {
3331 char_u *after = p;
3332
3333 // When followed by "=" or "+=" then it is an assignment.
3334 ++emsg_silent;
Bram Moolenaarce2c5442020-11-28 21:21:17 +01003335 if (skip_expr(&after, NULL) == OK)
3336 after = skipwhite(after);
3337 else
3338 after = (char_u *)"";
3339 if (*after == '=' || (*after != NUL && after[1] == '=')
3340 || (after[0] == '.' && after[1] == '.'
3341 && after[2] == '='))
Bram Moolenaar30fd8202020-09-26 15:09:30 +02003342 eap->cmdidx = CMD_var;
Bram Moolenaar2b22b112020-09-14 18:35:18 +02003343 else
3344 eap->cmdidx = CMD_eval;
3345 --emsg_silent;
3346 return eap->cmd;
3347 }
3348
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003349 // "[...]->Method()" is a list expression, but "[a, b] = Func()" is
3350 // an assignment.
3351 // If there is no line break inside the "[...]" then "p" is
3352 // advanced to after the "]" by to_name_const_end(): check if a "="
3353 // follows.
3354 // If "[...]" has a line break "p" still points at the "[" and it
3355 // can't be an assignment.
3356 if (*eap->cmd == '[')
Bram Moolenaarb31be3f2020-07-20 22:37:44 +02003357 {
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003358 p = to_name_const_end(eap->cmd);
Bram Moolenaarda7c20c2020-11-30 21:12:19 +01003359 if (p == eap->cmd && *p == '[')
3360 {
3361 int count = 0;
3362 int semicolon = FALSE;
3363
3364 p = skip_var_list(eap->cmd, TRUE, &count, &semicolon, TRUE);
3365 }
3366 if (p == NULL || p == eap->cmd || *skipwhite(p) != '=')
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003367 {
3368 eap->cmdidx = CMD_eval;
3369 return eap->cmd;
3370 }
3371 if (p > eap->cmd && *skipwhite(p) == '=')
3372 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02003373 eap->cmdidx = CMD_var;
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003374 return eap->cmd;
3375 }
3376 }
3377
3378 // Recognize an assignment if we recognize the variable name:
3379 // "g:var = expr"
3380 // "var = expr" where "var" is a local var name.
Bram Moolenaar83144542020-08-02 20:40:43 +02003381 if (*eap->cmd == '@')
3382 p = eap->cmd + 2;
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003383 oplen = assignment_len(skipwhite(p), &heredoc);
3384 if (oplen > 0)
3385 {
3386 if (((p - eap->cmd) > 2 && eap->cmd[1] == ':')
3387 || *eap->cmd == '&'
3388 || *eap->cmd == '$'
3389 || *eap->cmd == '@'
Bram Moolenaar709664c2020-12-12 14:33:41 +01003390 || lookup(eap->cmd, p - eap->cmd, NULL, cctx) == OK)
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003391 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02003392 eap->cmdidx = CMD_var;
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003393 return eap->cmd;
3394 }
3395 }
3396
3397 // Recognize using a type for a w:, b:, t: or g: variable:
3398 // "w:varname: number = 123".
3399 if (eap->cmd[1] == ':' && *p == ':')
3400 {
3401 eap->cmdidx = CMD_eval;
Bram Moolenaarb31be3f2020-07-20 22:37:44 +02003402 return eap->cmd;
3403 }
Bram Moolenaard2ef6b32020-07-02 21:11:34 +02003404 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003405 }
3406#endif
3407
Bram Moolenaar071d4272004-06-13 20:20:40 +00003408 /*
3409 * Isolate the command and search for it in the command table.
Bram Moolenaar81870892007-11-11 18:17:28 +00003410 * Exceptions:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003411 * - the 'k' command can directly be followed by any character.
3412 * - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003413 * but :sre[wind] is another command, as are :scr[iptnames],
Bram Moolenaar071d4272004-06-13 20:20:40 +00003414 * :scs[cope], :sim[alt], :sig[ns] and :sil[ent].
Bram Moolenaardf177f62005-02-22 08:39:57 +00003415 * - the "d" command can directly be followed by 'l' or 'p' flag.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003416 */
3417 p = eap->cmd;
3418 if (*p == 'k')
3419 {
3420 eap->cmdidx = CMD_k;
3421 ++p;
3422 }
3423 else if (p[0] == 's'
Bram Moolenaar204b93f2015-08-04 22:02:51 +02003424 && ((p[1] == 'c' && (p[2] == NUL || (p[2] != 's' && p[2] != 'r'
3425 && (p[3] == NUL || (p[3] != 'i' && p[4] != 'p')))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003426 || p[1] == 'g'
3427 || (p[1] == 'i' && p[2] != 'm' && p[2] != 'l' && p[2] != 'g')
3428 || p[1] == 'I'
3429 || (p[1] == 'r' && p[2] != 'e')))
3430 {
3431 eap->cmdidx = CMD_substitute;
3432 ++p;
3433 }
3434 else
3435 {
3436 while (ASCII_ISALPHA(*p))
3437 ++p;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003438 // for python 3.x support ":py3", ":python3", ":py3file", etc.
Bram Moolenaar55d5c032010-07-17 23:52:29 +02003439 if (eap->cmd[0] == 'p' && eap->cmd[1] == 'y')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003440 {
Bram Moolenaarb6590522010-07-21 16:00:43 +02003441 while (ASCII_ISALNUM(*p))
3442 ++p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003443 }
3444 else if (*p == '9' && STRNCMP("vim9", eap->cmd, 4) == 0)
3445 {
3446 // include "9" for "vim9script"
3447 ++p;
3448 while (ASCII_ISALPHA(*p))
3449 ++p;
3450 }
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02003451
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003452 // check for non-alpha command
Bram Moolenaar9becdf22020-10-10 21:33:48 +02003453 if (p == eap->cmd && vim_strchr((char_u *)"@*!=><&~#}", *p) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003454 ++p;
3455 len = (int)(p - eap->cmd);
Bram Moolenaardf177f62005-02-22 08:39:57 +00003456 if (*eap->cmd == 'd' && (p[-1] == 'l' || p[-1] == 'p'))
3457 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003458 // Check for ":dl", ":dell", etc. to ":deletel": that's
3459 // :delete with the 'l' flag. Same for 'p'.
Bram Moolenaardf177f62005-02-22 08:39:57 +00003460 for (i = 0; i < len; ++i)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003461 if (eap->cmd[i] != ((char_u *)"delete")[i])
Bram Moolenaardf177f62005-02-22 08:39:57 +00003462 break;
3463 if (i == len - 1)
3464 {
3465 --len;
3466 if (p[-1] == 'l')
3467 eap->flags |= EXFLAG_LIST;
3468 else
3469 eap->flags |= EXFLAG_PRINT;
3470 }
3471 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003472
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003473 if (ASCII_ISLOWER(eap->cmd[0]))
3474 {
Bram Moolenaar6c0c1e82017-03-25 15:07:43 +01003475 int c1 = eap->cmd[0];
Bram Moolenaar94f82cb2019-07-24 22:30:27 +02003476 int c2 = len == 1 ? NUL : eap->cmd[1];
Bram Moolenaar6c0c1e82017-03-25 15:07:43 +01003477
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003478 if (command_count != (int)CMD_SIZE)
3479 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003480 iemsg(_("E943: Command table needs to be updated, run 'make cmdidxs'"));
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003481 getout(1);
3482 }
3483
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003484 // Use a precomputed index for fast look-up in cmdnames[]
3485 // taking into account the first 2 letters of eap->cmd.
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003486 eap->cmdidx = cmdidxs1[CharOrdLow(c1)];
3487 if (ASCII_ISLOWER(c2))
3488 eap->cmdidx += cmdidxs2[CharOrdLow(c1)][CharOrdLow(c2)];
3489 }
Bram Moolenaara494f562020-04-18 17:45:38 +02003490 else if (ASCII_ISUPPER(eap->cmd[0]))
3491 eap->cmdidx = CMD_Next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003492 else
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003493 eap->cmdidx = CMD_bang;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003494
3495 for ( ; (int)eap->cmdidx < (int)CMD_SIZE;
3496 eap->cmdidx = (cmdidx_T)((int)eap->cmdidx + 1))
3497 if (STRNCMP(cmdnames[(int)eap->cmdidx].cmd_name, (char *)eap->cmd,
3498 (size_t)len) == 0)
3499 {
3500#ifdef FEAT_EVAL
3501 if (full != NULL
3502 && cmdnames[(int)eap->cmdidx].cmd_name[len] == NUL)
3503 *full = TRUE;
3504#endif
3505 break;
3506 }
3507
Bram Moolenaar95388e32020-11-20 21:07:00 +01003508 // Not not recognize ":*" as the star command unless '*' is in
3509 // 'cpoptions'.
3510 if (eap->cmdidx == CMD_star && vim_strchr(p_cpo, CPO_STAR) == NULL)
3511 p = eap->cmd;
3512
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003513 // Look for a user defined command as a last resort. Let ":Print" be
3514 // overruled by a user defined command.
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01003515 if ((eap->cmdidx == CMD_SIZE || eap->cmdidx == CMD_Print)
3516 && *eap->cmd >= 'A' && *eap->cmd <= 'Z')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003517 {
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003518 // User defined commands may contain digits.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003519 while (ASCII_ISALNUM(*p))
3520 ++p;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003521 p = find_ucmd(eap, p, full, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003522 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003523 if (p == eap->cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003524 eap->cmdidx = CMD_SIZE;
3525 }
3526
Bram Moolenaar373863e2020-09-26 17:20:53 +02003527 // ":fina" means ":finally" for backwards compatibility.
3528 if (eap->cmdidx == CMD_final && p - eap->cmd == 4)
3529 eap->cmdidx = CMD_finally;
3530
Bram Moolenaar2a3cd3a2020-12-13 19:22:27 +01003531#ifdef FEAT_EVAL
Bram Moolenaarb5b94802020-12-13 17:50:20 +01003532 if (eap->cmdidx != CMD_SIZE && in_vim9script()
3533 && !IS_WHITE_OR_NUL(*p) && !ends_excmd(*p) && *p != '!'
3534 && (cmdnames[eap->cmdidx].cmd_argt & EX_NONWHITE_OK) == 0)
3535 {
3536 semsg(_(e_command_not_followed_by_white_space_str), eap->cmd);
3537 eap->cmdidx = CMD_SIZE;
3538 }
Bram Moolenaar2a3cd3a2020-12-13 19:22:27 +01003539#endif
Bram Moolenaarb5b94802020-12-13 17:50:20 +01003540
Bram Moolenaar071d4272004-06-13 20:20:40 +00003541 return p;
3542}
3543
3544#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003545static struct cmdmod
3546{
3547 char *name;
3548 int minlen;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003549 int has_count; // :123verbose :3tab
Bram Moolenaared53fb92007-11-24 20:50:24 +00003550} cmdmods[] = {
3551 {"aboveleft", 3, FALSE},
3552 {"belowright", 3, FALSE},
3553 {"botright", 2, FALSE},
3554 {"browse", 3, FALSE},
3555 {"confirm", 4, FALSE},
Bram Moolenaar7b668e82016-08-23 23:51:21 +02003556 {"filter", 4, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003557 {"hide", 3, FALSE},
3558 {"keepalt", 5, FALSE},
3559 {"keepjumps", 5, FALSE},
3560 {"keepmarks", 3, FALSE},
Bram Moolenaara939e432013-11-09 05:30:26 +01003561 {"keeppatterns", 5, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003562 {"leftabove", 5, FALSE},
3563 {"lockmarks", 3, FALSE},
Bram Moolenaarca9f9582008-09-18 10:44:28 +00003564 {"noautocmd", 3, FALSE},
Bram Moolenaar5803ae62014-03-23 16:04:02 +01003565 {"noswapfile", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003566 {"rightbelow", 6, FALSE},
3567 {"sandbox", 3, FALSE},
3568 {"silent", 3, FALSE},
3569 {"tab", 3, TRUE},
3570 {"topleft", 2, FALSE},
Bram Moolenaar8e258a42009-07-09 13:55:43 +00003571 {"unsilent", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003572 {"verbose", 4, TRUE},
3573 {"vertical", 4, FALSE},
3574};
3575
3576/*
3577 * Return length of a command modifier (including optional count).
3578 * Return zero when it's not a modifier.
3579 */
3580 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003581modifier_len(char_u *cmd)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003582{
3583 int i, j;
3584 char_u *p = cmd;
3585
3586 if (VIM_ISDIGIT(*cmd))
3587 p = skipwhite(skipdigits(cmd));
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00003588 for (i = 0; i < (int)(sizeof(cmdmods) / sizeof(struct cmdmod)); ++i)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003589 {
3590 for (j = 0; p[j] != NUL; ++j)
3591 if (p[j] != cmdmods[i].name[j])
3592 break;
Bram Moolenaar2d473ab2013-06-12 17:12:24 +02003593 if (!ASCII_ISALPHA(p[j]) && j >= cmdmods[i].minlen
Bram Moolenaared53fb92007-11-24 20:50:24 +00003594 && (p == cmd || cmdmods[i].has_count))
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00003595 return j + (int)(p - cmd);
Bram Moolenaared53fb92007-11-24 20:50:24 +00003596 }
3597 return 0;
3598}
3599
Bram Moolenaar071d4272004-06-13 20:20:40 +00003600/*
3601 * Return > 0 if an Ex command "name" exists.
3602 * Return 2 if there is an exact match.
3603 * Return 3 if there is an ambiguous match.
3604 */
3605 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003606cmd_exists(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003607{
3608 exarg_T ea;
3609 int full = FALSE;
3610 int i;
3611 int j;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003612 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003613
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003614 // Check command modifiers.
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00003615 for (i = 0; i < (int)(sizeof(cmdmods) / sizeof(struct cmdmod)); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003616 {
3617 for (j = 0; name[j] != NUL; ++j)
3618 if (name[j] != cmdmods[i].name[j])
3619 break;
3620 if (name[j] == NUL && j >= cmdmods[i].minlen)
3621 return (cmdmods[i].name[j] == NUL ? 2 : 1);
3622 }
3623
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003624 // Check built-in commands and user defined commands.
3625 // For ":2match" and ":3match" we need to skip the number.
Bram Moolenaara9587612006-05-04 21:47:50 +00003626 ea.cmd = (*name == '2' || *name == '3') ? name + 1 : name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003627 ea.cmdidx = (cmdidx_T)0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003628 p = find_ex_command(&ea, &full, NULL, NULL);
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003629 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003630 return 3;
Bram Moolenaara9587612006-05-04 21:47:50 +00003631 if (vim_isdigit(*name) && ea.cmdidx != CMD_match)
3632 return 0;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003633 if (*skipwhite(p) != NUL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003634 return 0; // trailing garbage
Bram Moolenaar071d4272004-06-13 20:20:40 +00003635 return (ea.cmdidx == CMD_SIZE ? 0 : (full ? 2 : 1));
3636}
3637#endif
3638
Bram Moolenaard0190392019-08-23 21:17:35 +02003639 cmdidx_T
3640excmd_get_cmdidx(char_u *cmd, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003641{
Bram Moolenaard0190392019-08-23 21:17:35 +02003642 cmdidx_T idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003643
Bram Moolenaard0190392019-08-23 21:17:35 +02003644 for (idx = (cmdidx_T)0; (int)idx < (int)CMD_SIZE;
3645 idx = (cmdidx_T)((int)idx + 1))
3646 if (STRNCMP(cmdnames[(int)idx].cmd_name, cmd, (size_t)len) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003647 break;
3648
Bram Moolenaard0190392019-08-23 21:17:35 +02003649 return idx;
3650}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003651
Bram Moolenaard0190392019-08-23 21:17:35 +02003652 long
3653excmd_get_argt(cmdidx_T idx)
3654{
3655 return (long)cmdnames[(int)idx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003656}
3657
3658/*
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003659 * Skip a range specifier of the form: addr [,addr] [;addr] ..
Bram Moolenaar071d4272004-06-13 20:20:40 +00003660 *
3661 * Backslashed delimiters after / or ? will be skipped, and commands will
3662 * not be expanded between /'s and ?'s or after "'".
3663 *
Bram Moolenaardf069ee2020-06-22 23:02:51 +02003664 * Also skip white space and ":" characters after the range.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003665 * Returns the "cmd" pointer advanced to beyond the range.
3666 */
3667 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003668skip_range(
3669 char_u *cmd,
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02003670 int skip_star, // skip "*" used for Visual range
3671 int *ctx) // pointer to xp_context or NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003672{
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003673 unsigned delim;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003674
Bram Moolenaarcbf20fb2017-02-03 21:19:04 +01003675 while (vim_strchr((char_u *)" \t0123456789.$%'/?-+,;\\", *cmd) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003676 {
Bram Moolenaarcbf20fb2017-02-03 21:19:04 +01003677 if (*cmd == '\\')
3678 {
3679 if (cmd[1] == '?' || cmd[1] == '/' || cmd[1] == '&')
3680 ++cmd;
3681 else
3682 break;
3683 }
3684 else if (*cmd == '\'')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003685 {
3686 if (*++cmd == NUL && ctx != NULL)
3687 *ctx = EXPAND_NOTHING;
3688 }
3689 else if (*cmd == '/' || *cmd == '?')
3690 {
3691 delim = *cmd++;
3692 while (*cmd != NUL && *cmd != delim)
3693 if (*cmd++ == '\\' && *cmd != NUL)
3694 ++cmd;
3695 if (*cmd == NUL && ctx != NULL)
3696 *ctx = EXPAND_NOTHING;
3697 }
3698 if (*cmd != NUL)
3699 ++cmd;
3700 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00003701
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003702 // Skip ":" and white space.
Bram Moolenaardf177f62005-02-22 08:39:57 +00003703 while (*cmd == ':')
3704 cmd = skipwhite(cmd + 1);
3705
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02003706 // Skip "*" used for Visual range.
3707 if (skip_star && *cmd == '*' && vim_strchr(p_cpo, CPO_STAR) == NULL)
3708 cmd = skipwhite(cmd + 1);
3709
Bram Moolenaar071d4272004-06-13 20:20:40 +00003710 return cmd;
3711}
3712
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003713 static void
3714addr_error(cmd_addr_T addr_type)
3715{
3716 if (addr_type == ADDR_NONE)
3717 emsg(_(e_norange));
3718 else
3719 emsg(_(e_invrange));
3720}
3721
Bram Moolenaar071d4272004-06-13 20:20:40 +00003722/*
Bram Moolenaarc2af0af2020-08-23 21:06:02 +02003723 * Return the default address for an address type.
3724 */
3725 static linenr_T
3726default_address(exarg_T *eap)
3727{
3728 linenr_T lnum = 0;
3729
3730 switch (eap->addr_type)
3731 {
3732 case ADDR_LINES:
3733 case ADDR_OTHER:
3734 // Default is the cursor line number. Avoid using an invalid
3735 // line number though.
3736 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
3737 lnum = curbuf->b_ml.ml_line_count;
3738 else
3739 lnum = curwin->w_cursor.lnum;
3740 break;
3741 case ADDR_WINDOWS:
3742 lnum = CURRENT_WIN_NR;
3743 break;
3744 case ADDR_ARGUMENTS:
3745 lnum = curwin->w_arg_idx + 1;
3746 if (lnum > ARGCOUNT)
3747 lnum = ARGCOUNT;
3748 break;
3749 case ADDR_LOADED_BUFFERS:
3750 case ADDR_BUFFERS:
3751 lnum = curbuf->b_fnum;
3752 break;
3753 case ADDR_TABS:
3754 lnum = CURRENT_TAB_NR;
3755 break;
3756 case ADDR_TABS_RELATIVE:
3757 case ADDR_UNSIGNED:
3758 lnum = 1;
3759 break;
3760 case ADDR_QUICKFIX:
3761#ifdef FEAT_QUICKFIX
3762 lnum = qf_get_cur_idx(eap);
3763#endif
3764 break;
3765 case ADDR_QUICKFIX_VALID:
3766#ifdef FEAT_QUICKFIX
3767 lnum = qf_get_cur_valid_idx(eap);
3768#endif
3769 break;
3770 case ADDR_NONE:
3771 // Will give an error later if a range is found.
3772 break;
3773 }
3774 return lnum;
3775}
3776
3777/*
Bram Moolenaar198cb662018-09-06 21:44:17 +02003778 * Get a single EX address.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003779 *
3780 * Set ptr to the next character after the part that was interpreted.
3781 * Set ptr to NULL when an error is encountered.
Bram Moolenaar198cb662018-09-06 21:44:17 +02003782 * This may set the last used search pattern.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003783 *
3784 * Return MAXLNUM when no Ex address was found.
3785 */
3786 static linenr_T
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003787get_address(
3788 exarg_T *eap UNUSED,
3789 char_u **ptr,
Bram Moolenaar1b03a192019-12-08 17:08:29 +01003790 cmd_addr_T addr_type,
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02003791 int skip, // only skip the address, don't use it
3792 int silent, // no errors or side effects
3793 int to_other_file, // flag: may jump to other file
3794 int address_count UNUSED) // 1 for first address, >1 after comma
Bram Moolenaar071d4272004-06-13 20:20:40 +00003795{
3796 int c;
3797 int i;
3798 long n;
3799 char_u *cmd;
3800 pos_T pos;
3801 pos_T *fp;
3802 linenr_T lnum;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003803 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003804
3805 cmd = skipwhite(*ptr);
3806 lnum = MAXLNUM;
3807 do
3808 {
3809 switch (*cmd)
3810 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003811 case '.': // '.' - Cursor position
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003812 ++cmd;
3813 switch (addr_type)
3814 {
3815 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02003816 case ADDR_OTHER:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003817 lnum = curwin->w_cursor.lnum;
3818 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003819 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01003820 lnum = CURRENT_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003821 break;
3822 case ADDR_ARGUMENTS:
3823 lnum = curwin->w_arg_idx + 1;
3824 break;
3825 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003826 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003827 lnum = curbuf->b_fnum;
3828 break;
3829 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01003830 lnum = CURRENT_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003831 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02003832 case ADDR_NONE:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003833 case ADDR_TABS_RELATIVE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02003834 case ADDR_UNSIGNED:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003835 addr_error(addr_type);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01003836 cmd = NULL;
3837 goto error;
3838 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003839 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003840#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02003841 lnum = qf_get_cur_idx(eap);
3842#endif
3843 break;
3844 case ADDR_QUICKFIX_VALID:
3845#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003846 lnum = qf_get_cur_valid_idx(eap);
Bram Moolenaare906c502015-09-09 21:10:39 +02003847#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003848 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003849 }
3850 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003851
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003852 case '$': // '$' - last line
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003853 ++cmd;
3854 switch (addr_type)
3855 {
3856 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02003857 case ADDR_OTHER:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003858 lnum = curbuf->b_ml.ml_line_count;
3859 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003860 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01003861 lnum = LAST_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003862 break;
3863 case ADDR_ARGUMENTS:
3864 lnum = ARGCOUNT;
3865 break;
3866 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003867 buf = lastbuf;
3868 while (buf->b_ml.ml_mfp == NULL)
3869 {
3870 if (buf->b_prev == NULL)
3871 break;
3872 buf = buf->b_prev;
3873 }
3874 lnum = buf->b_fnum;
3875 break;
3876 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003877 lnum = lastbuf->b_fnum;
3878 break;
3879 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01003880 lnum = LAST_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003881 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02003882 case ADDR_NONE:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003883 case ADDR_TABS_RELATIVE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02003884 case ADDR_UNSIGNED:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003885 addr_error(addr_type);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01003886 cmd = NULL;
3887 goto error;
3888 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003889 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003890#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003891 lnum = qf_get_size(eap);
3892 if (lnum == 0)
3893 lnum = 1;
Bram Moolenaare906c502015-09-09 21:10:39 +02003894#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003895 break;
Bram Moolenaar25190db2019-05-04 15:05:28 +02003896 case ADDR_QUICKFIX_VALID:
3897#ifdef FEAT_QUICKFIX
3898 lnum = qf_get_valid_size(eap);
3899 if (lnum == 0)
3900 lnum = 1;
3901#endif
3902 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003903 }
3904 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003905
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003906 case '\'': // ''' - mark
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003907 if (*++cmd == NUL)
3908 {
3909 cmd = NULL;
3910 goto error;
3911 }
3912 if (addr_type != ADDR_LINES)
3913 {
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003914 addr_error(addr_type);
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01003915 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003916 goto error;
3917 }
3918 if (skip)
3919 ++cmd;
3920 else
3921 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003922 // Only accept a mark in another file when it is
3923 // used by itself: ":'M".
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003924 fp = getmark(*cmd, to_other_file && cmd[1] == NUL);
3925 ++cmd;
3926 if (fp == (pos_T *)-1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003927 // Jumped to another file.
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003928 lnum = curwin->w_cursor.lnum;
3929 else
3930 {
3931 if (check_mark(fp) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003932 {
3933 cmd = NULL;
3934 goto error;
3935 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003936 lnum = fp->lnum;
3937 }
3938 }
3939 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003940
3941 case '/':
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003942 case '?': // '/' or '?' - search
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003943 c = *cmd++;
3944 if (addr_type != ADDR_LINES)
3945 {
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003946 addr_error(addr_type);
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01003947 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003948 goto error;
3949 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003950 if (skip) // skip "/pat/"
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003951 {
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02003952 cmd = skip_regexp(cmd, c, (int)p_magic);
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003953 if (*cmd == c)
3954 ++cmd;
3955 }
3956 else
3957 {
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02003958 int flags;
3959
3960 pos = curwin->w_cursor; // save curwin->w_cursor
3961
3962 // When '/' or '?' follows another address, start from
3963 // there.
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003964 if (lnum != MAXLNUM)
3965 curwin->w_cursor.lnum = lnum;
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02003966
3967 // Start a forward search at the end of the line (unless
3968 // before the first line).
3969 // Start a backward search at the start of the line.
3970 // This makes sure we never match in the current
3971 // line, and can match anywhere in the
3972 // next/previous line.
Bram Moolenaar8ada6aa2017-12-19 21:23:21 +01003973 if (c == '/' && curwin->w_cursor.lnum > 0)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003974 curwin->w_cursor.col = MAXCOL;
3975 else
3976 curwin->w_cursor.col = 0;
3977 searchcmdlen = 0;
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02003978 flags = silent ? 0 : SEARCH_HIS | SEARCH_MSG;
Bram Moolenaarc036e872020-02-21 21:30:52 +01003979 if (!do_search(NULL, c, c, cmd, 1L, flags, NULL))
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003980 {
3981 curwin->w_cursor = pos;
3982 cmd = NULL;
3983 goto error;
3984 }
3985 lnum = curwin->w_cursor.lnum;
3986 curwin->w_cursor = pos;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003987 // adjust command string pointer
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003988 cmd += searchcmdlen;
3989 }
3990 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003991
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003992 case '\\': // "\?", "\/" or "\&", repeat search
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003993 ++cmd;
3994 if (addr_type != ADDR_LINES)
3995 {
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003996 addr_error(addr_type);
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01003997 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003998 goto error;
3999 }
4000 if (*cmd == '&')
4001 i = RE_SUBST;
4002 else if (*cmd == '?' || *cmd == '/')
4003 i = RE_SEARCH;
4004 else
4005 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004006 emsg(_(e_backslash));
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004007 cmd = NULL;
4008 goto error;
4009 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004010
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004011 if (!skip)
4012 {
4013 /*
4014 * When search follows another address, start from
4015 * there.
4016 */
4017 if (lnum != MAXLNUM)
4018 pos.lnum = lnum;
4019 else
4020 pos.lnum = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004021
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004022 /*
4023 * Start the search just like for the above
4024 * do_search().
4025 */
4026 if (*cmd != '?')
4027 pos.col = MAXCOL;
4028 else
4029 pos.col = 0;
Bram Moolenaarbd8539a2015-08-11 18:53:03 +02004030 pos.coladd = 0;
Bram Moolenaar5d24a222018-12-23 19:10:09 +01004031 if (searchit(curwin, curbuf, &pos, NULL,
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004032 *cmd == '?' ? BACKWARD : FORWARD,
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02004033 (char_u *)"", 1L, SEARCH_MSG, i, NULL) != FAIL)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004034 lnum = pos.lnum;
4035 else
4036 {
4037 cmd = NULL;
4038 goto error;
4039 }
4040 }
4041 ++cmd;
4042 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004043
4044 default:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004045 if (VIM_ISDIGIT(*cmd)) // absolute line number
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004046 lnum = getdigits(&cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004047 }
4048
4049 for (;;)
4050 {
4051 cmd = skipwhite(cmd);
4052 if (*cmd != '-' && *cmd != '+' && !VIM_ISDIGIT(*cmd))
4053 break;
4054
4055 if (lnum == MAXLNUM)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004056 {
4057 switch (addr_type)
4058 {
4059 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004060 case ADDR_OTHER:
4061 // "+1" is same as ".+1"
Bram Moolenaarf240e182014-11-27 18:33:02 +01004062 lnum = curwin->w_cursor.lnum;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004063 break;
4064 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004065 lnum = CURRENT_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004066 break;
4067 case ADDR_ARGUMENTS:
4068 lnum = curwin->w_arg_idx + 1;
4069 break;
4070 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004071 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004072 lnum = curbuf->b_fnum;
4073 break;
4074 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004075 lnum = CURRENT_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004076 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004077 case ADDR_TABS_RELATIVE:
4078 lnum = 1;
4079 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004080 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004081#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02004082 lnum = qf_get_cur_idx(eap);
4083#endif
4084 break;
4085 case ADDR_QUICKFIX_VALID:
4086#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004087 lnum = qf_get_cur_valid_idx(eap);
Bram Moolenaare906c502015-09-09 21:10:39 +02004088#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004089 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02004090 case ADDR_NONE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02004091 case ADDR_UNSIGNED:
4092 lnum = 0;
Bram Moolenaarb7316892019-05-01 18:08:42 +02004093 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004094 }
4095 }
4096
Bram Moolenaar071d4272004-06-13 20:20:40 +00004097 if (VIM_ISDIGIT(*cmd))
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004098 i = '+'; // "number" is same as "+number"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004099 else
4100 i = *cmd++;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004101 if (!VIM_ISDIGIT(*cmd)) // '+' is '+1', but '+0' is not '+1'
Bram Moolenaar071d4272004-06-13 20:20:40 +00004102 n = 1;
4103 else
4104 n = getdigits(&cmd);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004105
4106 if (addr_type == ADDR_TABS_RELATIVE)
4107 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004108 emsg(_(e_invrange));
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004109 cmd = NULL;
4110 goto error;
4111 }
4112 else if (addr_type == ADDR_LOADED_BUFFERS
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004113 || addr_type == ADDR_BUFFERS)
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004114 lnum = compute_buffer_local_count(
4115 addr_type, lnum, (i == '-') ? -1 * n : n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004116 else
Bram Moolenaarded27822017-01-02 14:27:34 +01004117 {
4118#ifdef FEAT_FOLDING
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004119 // Relative line addressing, need to adjust for folded lines
4120 // now, but only do it after the first address.
Bram Moolenaarded27822017-01-02 14:27:34 +01004121 if (addr_type == ADDR_LINES && (i == '-' || i == '+')
4122 && address_count >= 2)
4123 (void)hasFolding(lnum, NULL, &lnum);
4124#endif
4125 if (i == '-')
4126 lnum -= n;
4127 else
4128 lnum += n;
4129 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004130 }
4131 } while (*cmd == '/' || *cmd == '?');
4132
4133error:
4134 *ptr = cmd;
4135 return lnum;
4136}
4137
4138/*
Bram Moolenaarc2af0af2020-08-23 21:06:02 +02004139 * Set eap->line1 and eap->line2 to the whole range.
4140 * Used for commands with the EX_DFLALL flag and no range given.
4141 */
4142 static void
4143address_default_all(exarg_T *eap)
4144{
4145 eap->line1 = 1;
4146 switch (eap->addr_type)
4147 {
4148 case ADDR_LINES:
4149 case ADDR_OTHER:
4150 eap->line2 = curbuf->b_ml.ml_line_count;
4151 break;
4152 case ADDR_LOADED_BUFFERS:
4153 {
4154 buf_T *buf = firstbuf;
4155
4156 while (buf->b_next != NULL && buf->b_ml.ml_mfp == NULL)
4157 buf = buf->b_next;
4158 eap->line1 = buf->b_fnum;
4159 buf = lastbuf;
4160 while (buf->b_prev != NULL && buf->b_ml.ml_mfp == NULL)
4161 buf = buf->b_prev;
4162 eap->line2 = buf->b_fnum;
4163 }
4164 break;
4165 case ADDR_BUFFERS:
4166 eap->line1 = firstbuf->b_fnum;
4167 eap->line2 = lastbuf->b_fnum;
4168 break;
4169 case ADDR_WINDOWS:
4170 eap->line2 = LAST_WIN_NR;
4171 break;
4172 case ADDR_TABS:
4173 eap->line2 = LAST_TAB_NR;
4174 break;
4175 case ADDR_TABS_RELATIVE:
4176 eap->line2 = 1;
4177 break;
4178 case ADDR_ARGUMENTS:
4179 if (ARGCOUNT == 0)
4180 eap->line1 = eap->line2 = 0;
4181 else
4182 eap->line2 = ARGCOUNT;
4183 break;
4184 case ADDR_QUICKFIX_VALID:
4185#ifdef FEAT_QUICKFIX
4186 eap->line2 = qf_get_valid_size(eap);
4187 if (eap->line2 == 0)
4188 eap->line2 = 1;
4189#endif
4190 break;
4191 case ADDR_NONE:
4192 case ADDR_UNSIGNED:
4193 case ADDR_QUICKFIX:
4194 iemsg(_("INTERNAL: Cannot use EX_DFLALL with ADDR_NONE, ADDR_UNSIGNED or ADDR_QUICKFIX"));
4195 break;
4196 }
4197}
4198
4199
4200/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00004201 * Get flags from an Ex command argument.
4202 */
4203 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004204get_flags(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00004205{
4206 while (vim_strchr((char_u *)"lp#", *eap->arg) != NULL)
4207 {
4208 if (*eap->arg == 'l')
4209 eap->flags |= EXFLAG_LIST;
4210 else if (*eap->arg == 'p')
4211 eap->flags |= EXFLAG_PRINT;
4212 else
4213 eap->flags |= EXFLAG_NR;
4214 eap->arg = skipwhite(eap->arg + 1);
4215 }
4216}
4217
4218/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004219 * Function called for command which is Not Implemented. NI!
4220 */
4221 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004222ex_ni(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004223{
4224 if (!eap->skip)
Bram Moolenaar8930caa2020-07-23 16:37:03 +02004225 eap->errmsg =
4226 _("E319: Sorry, the command is not available in this version");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004227}
4228
Bram Moolenaar7bb75552007-07-16 18:39:49 +00004229#ifdef HAVE_EX_SCRIPT_NI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004230/*
4231 * Function called for script command which is Not Implemented. NI!
4232 * Skips over ":perl <<EOF" constructs.
4233 */
4234 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004235ex_script_ni(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004236{
4237 if (!eap->skip)
4238 ex_ni(eap);
4239 else
4240 vim_free(script_get(eap, eap->arg));
4241}
4242#endif
4243
4244/*
4245 * Check range in Ex command for validity.
4246 * Return NULL when valid, error message when invalid.
4247 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004248 static char *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004249invalid_range(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004250{
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004251 buf_T *buf;
Bram Moolenaar25190db2019-05-04 15:05:28 +02004252
Bram Moolenaar071d4272004-06-13 20:20:40 +00004253 if ( eap->line1 < 0
4254 || eap->line2 < 0
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004255 || eap->line1 > eap->line2)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004256 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004257
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004258 if (eap->argt & EX_RANGE)
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004259 {
Bram Moolenaarb7316892019-05-01 18:08:42 +02004260 switch (eap->addr_type)
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004261 {
4262 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004263 if (eap->line2 > curbuf->b_ml.ml_line_count
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004264#ifdef FEAT_DIFF
4265 + (eap->cmdidx == CMD_diffget)
4266#endif
4267 )
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004268 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004269 break;
4270 case ADDR_ARGUMENTS:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004271 // add 1 if ARGCOUNT is 0
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004272 if (eap->line2 > ARGCOUNT + (!ARGCOUNT))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004273 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004274 break;
4275 case ADDR_BUFFERS:
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02004276 // Only a boundary check, not whether the buffers actually
4277 // exist.
4278 if (eap->line1 < 1 || eap->line2 > get_highest_fnum())
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004279 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004280 break;
4281 case ADDR_LOADED_BUFFERS:
4282 buf = firstbuf;
4283 while (buf->b_ml.ml_mfp == NULL)
4284 {
4285 if (buf->b_next == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004286 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004287 buf = buf->b_next;
4288 }
4289 if (eap->line1 < buf->b_fnum)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004290 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004291 buf = lastbuf;
4292 while (buf->b_ml.ml_mfp == NULL)
4293 {
4294 if (buf->b_prev == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004295 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004296 buf = buf->b_prev;
4297 }
4298 if (eap->line2 > buf->b_fnum)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004299 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004300 break;
4301 case ADDR_WINDOWS:
Bram Moolenaar8be63882015-01-14 11:25:05 +01004302 if (eap->line2 > LAST_WIN_NR)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004303 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004304 break;
4305 case ADDR_TABS:
4306 if (eap->line2 > LAST_TAB_NR)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004307 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004308 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004309 case ADDR_TABS_RELATIVE:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004310 case ADDR_OTHER:
4311 // Any range is OK.
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004312 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004313 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004314#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02004315 // No error for value that is too big, will use the last entry.
4316 if (eap->line2 <= 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004317 return _(e_invrange);
Bram Moolenaare906c502015-09-09 21:10:39 +02004318#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004319 break;
Bram Moolenaar25190db2019-05-04 15:05:28 +02004320 case ADDR_QUICKFIX_VALID:
4321#ifdef FEAT_QUICKFIX
4322 if ((eap->line2 != 1 && eap->line2 > qf_get_valid_size(eap))
4323 || eap->line2 < 0)
4324 return _(e_invrange);
4325#endif
4326 break;
4327 case ADDR_UNSIGNED:
4328 if (eap->line2 < 0)
4329 return _(e_invrange);
4330 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02004331 case ADDR_NONE:
4332 // Will give an error elsewhere.
4333 break;
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004334 }
4335 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004336 return NULL;
4337}
4338
4339/*
4340 * Correct the range for zero line number, if required.
4341 */
4342 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004343correct_range(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004344{
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004345 if (!(eap->argt & EX_ZEROR)) // zero in range not allowed
Bram Moolenaar071d4272004-06-13 20:20:40 +00004346 {
4347 if (eap->line1 == 0)
4348 eap->line1 = 1;
4349 if (eap->line2 == 0)
4350 eap->line2 = 1;
4351 }
4352}
4353
Bram Moolenaar748bf032005-02-02 23:04:36 +00004354#ifdef FEAT_QUICKFIX
Bram Moolenaar748bf032005-02-02 23:04:36 +00004355/*
4356 * For a ":vimgrep" or ":vimgrepadd" command return a pointer past the
4357 * pattern. Otherwise return eap->arg.
4358 */
4359 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004360skip_grep_pat(exarg_T *eap)
Bram Moolenaar748bf032005-02-02 23:04:36 +00004361{
4362 char_u *p = eap->arg;
4363
Bram Moolenaara37420f2006-02-04 22:37:47 +00004364 if (*p != NUL && (eap->cmdidx == CMD_vimgrep || eap->cmdidx == CMD_lvimgrep
4365 || eap->cmdidx == CMD_vimgrepadd
4366 || eap->cmdidx == CMD_lvimgrepadd
4367 || grep_internal(eap->cmdidx)))
Bram Moolenaar748bf032005-02-02 23:04:36 +00004368 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00004369 p = skip_vimgrep_pat(p, NULL, NULL);
Bram Moolenaar748bf032005-02-02 23:04:36 +00004370 if (p == NULL)
4371 p = eap->arg;
Bram Moolenaar748bf032005-02-02 23:04:36 +00004372 }
4373 return p;
4374}
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004375
4376/*
4377 * For the ":make" and ":grep" commands insert the 'makeprg'/'grepprg' option
4378 * in the command line, so that things like % get expanded.
4379 */
4380 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004381replace_makeprg(exarg_T *eap, char_u *p, char_u **cmdlinep)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004382{
4383 char_u *new_cmdline;
4384 char_u *program;
4385 char_u *pos;
4386 char_u *ptr;
4387 int len;
4388 int i;
4389
4390 /*
4391 * Don't do it when ":vimgrep" is used for ":grep".
4392 */
Bram Moolenaara37420f2006-02-04 22:37:47 +00004393 if ((eap->cmdidx == CMD_make || eap->cmdidx == CMD_lmake
4394 || eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4395 || eap->cmdidx == CMD_grepadd
4396 || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004397 && !grep_internal(eap->cmdidx))
4398 {
Bram Moolenaara37420f2006-02-04 22:37:47 +00004399 if (eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4400 || eap->cmdidx == CMD_grepadd || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004401 {
4402 if (*curbuf->b_p_gp == NUL)
4403 program = p_gp;
4404 else
4405 program = curbuf->b_p_gp;
4406 }
4407 else
4408 {
4409 if (*curbuf->b_p_mp == NUL)
4410 program = p_mp;
4411 else
4412 program = curbuf->b_p_mp;
4413 }
4414
4415 p = skipwhite(p);
4416
4417 if ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4418 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004419 // replace $* by given arguments
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004420 i = 1;
4421 while ((pos = (char_u *)strstr((char *)pos + 2, "$*")) != NULL)
4422 ++i;
4423 len = (int)STRLEN(p);
Bram Moolenaar51e14382019-05-25 20:21:28 +02004424 new_cmdline = alloc(STRLEN(program) + i * (len - 2) + 1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004425 if (new_cmdline == NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004426 return NULL; // out of memory
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004427 ptr = new_cmdline;
4428 while ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4429 {
4430 i = (int)(pos - program);
4431 STRNCPY(ptr, program, i);
4432 STRCPY(ptr += i, p);
4433 ptr += len;
4434 program = pos + 2;
4435 }
4436 STRCPY(ptr, program);
4437 }
4438 else
4439 {
Bram Moolenaar51e14382019-05-25 20:21:28 +02004440 new_cmdline = alloc(STRLEN(program) + STRLEN(p) + 2);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004441 if (new_cmdline == NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004442 return NULL; // out of memory
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004443 STRCPY(new_cmdline, program);
4444 STRCAT(new_cmdline, " ");
4445 STRCAT(new_cmdline, p);
4446 }
4447 msg_make(p);
4448
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004449 // 'eap->cmd' is not set here, because it is not used at CMD_make
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004450 vim_free(*cmdlinep);
4451 *cmdlinep = new_cmdline;
4452 p = new_cmdline;
4453 }
4454 return p;
4455}
Bram Moolenaar748bf032005-02-02 23:04:36 +00004456#endif
4457
Bram Moolenaar071d4272004-06-13 20:20:40 +00004458/*
4459 * Expand file name in Ex command argument.
Bram Moolenaar0abb4272019-06-15 16:06:00 +02004460 * When an error is detected, "errormsgp" is set to a non-NULL pointer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004461 * Return FAIL for failure, OK otherwise.
4462 */
4463 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004464expand_filename(
4465 exarg_T *eap,
4466 char_u **cmdlinep,
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004467 char **errormsgp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004468{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004469 int has_wildcards; // need to expand wildcards
Bram Moolenaar071d4272004-06-13 20:20:40 +00004470 char_u *repl;
4471 int srclen;
4472 char_u *p;
4473 int n;
Bram Moolenaar63b92542007-03-27 14:57:09 +00004474 int escaped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004475
Bram Moolenaar748bf032005-02-02 23:04:36 +00004476#ifdef FEAT_QUICKFIX
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004477 // Skip a regexp pattern for ":vimgrep[add] pat file..."
Bram Moolenaar748bf032005-02-02 23:04:36 +00004478 p = skip_grep_pat(eap);
4479#else
4480 p = eap->arg;
4481#endif
4482
Bram Moolenaar071d4272004-06-13 20:20:40 +00004483 /*
4484 * Decide to expand wildcards *before* replacing '%', '#', etc. If
4485 * the file name contains a wildcard it should not cause expanding.
4486 * (it will be expanded anyway if there is a wildcard before replacing).
4487 */
Bram Moolenaar748bf032005-02-02 23:04:36 +00004488 has_wildcards = mch_has_wildcard(p);
4489 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004490 {
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004491#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004492 // Skip over `=expr`, wildcards in it are not expanded.
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004493 if (p[0] == '`' && p[1] == '=')
4494 {
4495 p += 2;
Bram Moolenaar683581e2020-10-22 21:22:58 +02004496 (void)skip_expr(&p, NULL);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004497 if (*p == '`')
4498 ++p;
4499 continue;
4500 }
4501#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004502 /*
4503 * Quick check if this cannot be the start of a special string.
4504 * Also removes backslash before '%', '#' and '<'.
4505 */
4506 if (vim_strchr((char_u *)"%#<", *p) == NULL)
4507 {
4508 ++p;
4509 continue;
4510 }
4511
4512 /*
4513 * Try to find a match at this position.
4514 */
Bram Moolenaar63b92542007-03-27 14:57:09 +00004515 repl = eval_vars(p, eap->arg, &srclen, &(eap->do_ecmd_lnum),
4516 errormsgp, &escaped);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004517 if (*errormsgp != NULL) // error detected
Bram Moolenaar071d4272004-06-13 20:20:40 +00004518 return FAIL;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004519 if (repl == NULL) // no match found
Bram Moolenaar071d4272004-06-13 20:20:40 +00004520 {
4521 p += srclen;
4522 continue;
4523 }
4524
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004525 // Wildcards won't be expanded below, the replacement is taken
4526 // literally. But do expand "~/file", "~user/file" and "$HOME/file".
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00004527 if (vim_strchr(repl, '$') != NULL || vim_strchr(repl, '~') != NULL)
4528 {
4529 char_u *l = repl;
4530
4531 repl = expand_env_save(repl);
4532 vim_free(l);
4533 }
4534
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004535 // Need to escape white space et al. with a backslash.
4536 // Don't do this for:
4537 // - replacement that already has been escaped: "##"
4538 // - shell commands (may have to use quotes instead).
4539 // - non-unix systems when there is a single argument (spaces don't
4540 // separate arguments then).
Bram Moolenaar071d4272004-06-13 20:20:40 +00004541 if (!eap->usefilter
Bram Moolenaar63b92542007-03-27 14:57:09 +00004542 && !escaped
Bram Moolenaar071d4272004-06-13 20:20:40 +00004543 && eap->cmdidx != CMD_bang
Bram Moolenaar071d4272004-06-13 20:20:40 +00004544 && eap->cmdidx != CMD_grep
4545 && eap->cmdidx != CMD_grepadd
Bram Moolenaarbf15b8d2017-06-04 20:43:48 +02004546 && eap->cmdidx != CMD_hardcopy
Bram Moolenaar67883b42017-07-27 22:57:00 +02004547 && eap->cmdidx != CMD_lgrep
4548 && eap->cmdidx != CMD_lgrepadd
4549 && eap->cmdidx != CMD_lmake
4550 && eap->cmdidx != CMD_make
4551 && eap->cmdidx != CMD_terminal
Bram Moolenaar071d4272004-06-13 20:20:40 +00004552#ifndef UNIX
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004553 && !(eap->argt & EX_NOSPC)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004554#endif
4555 )
4556 {
4557 char_u *l;
4558#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004559 // Don't escape a backslash here, because rem_backslash() doesn't
4560 // remove it later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004561 static char_u *nobslash = (char_u *)" \t\"|";
4562# define ESCAPE_CHARS nobslash
4563#else
4564# define ESCAPE_CHARS escape_chars
4565#endif
4566
4567 for (l = repl; *l; ++l)
4568 if (vim_strchr(ESCAPE_CHARS, *l) != NULL)
4569 {
4570 l = vim_strsave_escaped(repl, ESCAPE_CHARS);
4571 if (l != NULL)
4572 {
4573 vim_free(repl);
4574 repl = l;
4575 }
4576 break;
4577 }
4578 }
4579
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004580 // For a shell command a '!' must be escaped.
Bram Moolenaar67883b42017-07-27 22:57:00 +02004581 if ((eap->usefilter || eap->cmdidx == CMD_bang
4582 || eap->cmdidx == CMD_terminal)
Bram Moolenaar31b7d382014-04-01 18:54:48 +02004583 && vim_strpbrk(repl, (char_u *)"!") != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004584 {
4585 char_u *l;
4586
Bram Moolenaar31b7d382014-04-01 18:54:48 +02004587 l = vim_strsave_escaped(repl, (char_u *)"!");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004588 if (l != NULL)
4589 {
4590 vim_free(repl);
4591 repl = l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004592 }
4593 }
4594
4595 p = repl_cmdline(eap, p, srclen, repl, cmdlinep);
4596 vim_free(repl);
4597 if (p == NULL)
4598 return FAIL;
4599 }
4600
4601 /*
4602 * One file argument: Expand wildcards.
4603 * Don't do this with ":r !command" or ":w !command".
4604 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004605 if ((eap->argt & EX_NOSPC) && !eap->usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004606 {
4607 /*
4608 * May do this twice:
4609 * 1. Replace environment variables.
4610 * 2. Replace any other wildcards, remove backslashes.
4611 */
4612 for (n = 1; n <= 2; ++n)
4613 {
4614 if (n == 2)
4615 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004616 /*
4617 * Halve the number of backslashes (this is Vi compatible).
4618 * For Unix and OS/2, when wildcards are expanded, this is
4619 * done by ExpandOne() below.
4620 */
Bram Moolenaare7fedb62015-12-31 19:07:19 +01004621#if defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004622 if (!has_wildcards)
4623#endif
4624 backslash_halve(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004625 }
4626
4627 if (has_wildcards)
4628 {
4629 if (n == 1)
4630 {
4631 /*
4632 * First loop: May expand environment variables. This
4633 * can be done much faster with expand_env() than with
4634 * something else (e.g., calling a shell).
4635 * After expanding environment variables, check again
4636 * if there are still wildcards present.
4637 */
4638 if (vim_strchr(eap->arg, '$') != NULL
4639 || vim_strchr(eap->arg, '~') != NULL)
4640 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +00004641 expand_env_esc(eap->arg, NameBuff, MAXPATHL,
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00004642 TRUE, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004643 has_wildcards = mch_has_wildcard(NameBuff);
4644 p = NameBuff;
4645 }
4646 else
4647 p = NULL;
4648 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004649 else // n == 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00004650 {
4651 expand_T xpc;
Bram Moolenaarb40c2572019-10-19 21:01:05 +02004652 int options = WILD_LIST_NOTFOUND
4653 | WILD_NOERROR | WILD_ADD_SLASH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004654
4655 ExpandInit(&xpc);
4656 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar94950a92010-12-02 16:01:29 +01004657 if (p_wic)
4658 options += WILD_ICASE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004659 p = ExpandOne(&xpc, eap->arg, NULL,
Bram Moolenaar94950a92010-12-02 16:01:29 +01004660 options, WILD_EXPAND_FREE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004661 if (p == NULL)
4662 return FAIL;
4663 }
4664 if (p != NULL)
4665 {
4666 (void)repl_cmdline(eap, eap->arg, (int)STRLEN(eap->arg),
4667 p, cmdlinep);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004668 if (n == 2) // p came from ExpandOne()
Bram Moolenaar071d4272004-06-13 20:20:40 +00004669 vim_free(p);
4670 }
4671 }
4672 }
4673 }
4674 return OK;
4675}
4676
4677/*
4678 * Replace part of the command line, keeping eap->cmd, eap->arg and
4679 * eap->nextcmd correct.
4680 * "src" points to the part that is to be replaced, of length "srclen".
4681 * "repl" is the replacement string.
4682 * Returns a pointer to the character after the replaced string.
4683 * Returns NULL for failure.
4684 */
4685 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004686repl_cmdline(
4687 exarg_T *eap,
4688 char_u *src,
4689 int srclen,
4690 char_u *repl,
4691 char_u **cmdlinep)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004692{
4693 int len;
4694 int i;
4695 char_u *new_cmdline;
4696
4697 /*
4698 * The new command line is build in new_cmdline[].
4699 * First allocate it.
4700 * Careful: a "+cmd" argument may have been NUL terminated.
4701 */
4702 len = (int)STRLEN(repl);
4703 i = (int)(src - *cmdlinep) + (int)STRLEN(src + srclen) + len + 3;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004704 if (eap->nextcmd != NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004705 i += (int)STRLEN(eap->nextcmd);// add space for next command
Bram Moolenaar964b3742019-05-24 18:54:09 +02004706 if ((new_cmdline = alloc(i)) == NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004707 return NULL; // out of memory!
Bram Moolenaar071d4272004-06-13 20:20:40 +00004708
4709 /*
4710 * Copy the stuff before the expanded part.
4711 * Copy the expanded stuff.
4712 * Copy what came after the expanded part.
4713 * Copy the next commands, if there are any.
4714 */
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004715 i = (int)(src - *cmdlinep); // length of part before match
Bram Moolenaar071d4272004-06-13 20:20:40 +00004716 mch_memmove(new_cmdline, *cmdlinep, (size_t)i);
Bram Moolenaara3ffd9c2005-07-21 21:03:15 +00004717
Bram Moolenaar071d4272004-06-13 20:20:40 +00004718 mch_memmove(new_cmdline + i, repl, (size_t)len);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004719 i += len; // remember the end of the string
Bram Moolenaar071d4272004-06-13 20:20:40 +00004720 STRCPY(new_cmdline + i, src + srclen);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004721 src = new_cmdline + i; // remember where to continue
Bram Moolenaar071d4272004-06-13 20:20:40 +00004722
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004723 if (eap->nextcmd != NULL) // append next command
Bram Moolenaar071d4272004-06-13 20:20:40 +00004724 {
4725 i = (int)STRLEN(new_cmdline) + 1;
4726 STRCPY(new_cmdline + i, eap->nextcmd);
4727 eap->nextcmd = new_cmdline + i;
4728 }
4729 eap->cmd = new_cmdline + (eap->cmd - *cmdlinep);
4730 eap->arg = new_cmdline + (eap->arg - *cmdlinep);
4731 if (eap->do_ecmd_cmd != NULL && eap->do_ecmd_cmd != dollar_command)
4732 eap->do_ecmd_cmd = new_cmdline + (eap->do_ecmd_cmd - *cmdlinep);
4733 vim_free(*cmdlinep);
4734 *cmdlinep = new_cmdline;
4735
4736 return src;
4737}
4738
4739/*
4740 * Check for '|' to separate commands and '"' to start comments.
4741 */
4742 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004743separate_nextcmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004744{
4745 char_u *p;
4746
Bram Moolenaar86b68352004-12-27 21:59:20 +00004747#ifdef FEAT_QUICKFIX
Bram Moolenaar748bf032005-02-02 23:04:36 +00004748 p = skip_grep_pat(eap);
4749#else
4750 p = eap->arg;
Bram Moolenaar86b68352004-12-27 21:59:20 +00004751#endif
4752
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004753 for ( ; *p; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004754 {
4755 if (*p == Ctrl_V)
4756 {
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004757 if (eap->argt & (EX_CTRLV | EX_XFILE))
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004758 ++p; // skip CTRL-V and next char
Bram Moolenaar071d4272004-06-13 20:20:40 +00004759 else
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004760 // remove CTRL-V and skip next char
Bram Moolenaara7241f52008-06-24 20:39:31 +00004761 STRMOVE(p, p + 1);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004762 if (*p == NUL) // stop at NUL after CTRL-V
Bram Moolenaar071d4272004-06-13 20:20:40 +00004763 break;
4764 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004765
4766#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004767 // Skip over `=expr` when wildcards are expanded.
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004768 else if (p[0] == '`' && p[1] == '=' && (eap->argt & EX_XFILE))
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004769 {
4770 p += 2;
Bram Moolenaar683581e2020-10-22 21:22:58 +02004771 (void)skip_expr(&p, NULL);
Bram Moolenaarcaf73dc2020-10-26 21:39:13 +01004772 if (*p == NUL) // stop at NUL after CTRL-V
4773 break;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004774 }
4775#endif
4776
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004777 // Check for '"': start of comment or '|': next command
4778 // :@" and :*" do not start a comment!
4779 // :redir @" doesn't either.
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02004780 else if ((*p == '"'
Bram Moolenaara26b9702020-04-18 19:53:28 +02004781#ifdef FEAT_EVAL
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02004782 && !in_vim9script()
4783#endif
4784 && !(eap->argt & EX_NOTRLCOM)
4785 && ((eap->cmdidx != CMD_at && eap->cmdidx != CMD_star)
4786 || p != eap->arg)
4787 && (eap->cmdidx != CMD_redir
4788 || p != eap->arg + 1 || p[-1] != '@'))
4789#ifdef FEAT_EVAL
4790 || (*p == '#'
4791 && in_vim9script()
Bram Moolenaarb8a92962020-08-20 18:02:47 +02004792 && !(eap->argt & EX_NOTRLCOM)
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02004793 && p > eap->cmd && VIM_ISWHITE(p[-1]))
Bram Moolenaara26b9702020-04-18 19:53:28 +02004794#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004795 || *p == '|' || *p == '\n')
4796 {
4797 /*
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004798 * We remove the '\' before the '|', unless EX_CTRLV is used
Bram Moolenaar071d4272004-06-13 20:20:40 +00004799 * AND 'b' is present in 'cpoptions'.
4800 */
4801 if ((vim_strchr(p_cpo, CPO_BAR) == NULL
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004802 || !(eap->argt & EX_CTRLV)) && *(p - 1) == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004803 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004804 STRMOVE(p - 1, p); // remove the '\'
Bram Moolenaar071d4272004-06-13 20:20:40 +00004805 --p;
4806 }
4807 else
4808 {
4809 eap->nextcmd = check_nextcmd(p);
4810 *p = NUL;
4811 break;
4812 }
4813 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004814 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00004815
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004816 if (!(eap->argt & EX_NOTRLCOM)) // remove trailing spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00004817 del_trailing_spaces(eap->arg);
4818}
4819
4820/*
4821 * get + command from ex argument
4822 */
4823 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004824getargcmd(char_u **argp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004825{
4826 char_u *arg = *argp;
4827 char_u *command = NULL;
4828
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004829 if (*arg == '+') // +[command]
Bram Moolenaar071d4272004-06-13 20:20:40 +00004830 {
4831 ++arg;
Bram Moolenaar3e451592014-04-02 14:22:05 +02004832 if (vim_isspace(*arg) || *arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004833 command = dollar_command;
4834 else
4835 {
4836 command = arg;
4837 arg = skip_cmd_arg(command, TRUE);
4838 if (*arg != NUL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004839 *arg++ = NUL; // terminate command with NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004840 }
4841
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004842 arg = skipwhite(arg); // skip over spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00004843 *argp = arg;
4844 }
4845 return command;
4846}
4847
4848/*
4849 * Find end of "+command" argument. Skip over "\ " and "\\".
4850 */
Bram Moolenaard0190392019-08-23 21:17:35 +02004851 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004852skip_cmd_arg(
4853 char_u *p,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004854 int rembs) // TRUE to halve the number of backslashes
Bram Moolenaar071d4272004-06-13 20:20:40 +00004855{
4856 while (*p && !vim_isspace(*p))
4857 {
4858 if (*p == '\\' && p[1] != NUL)
4859 {
4860 if (rembs)
Bram Moolenaara7241f52008-06-24 20:39:31 +00004861 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004862 else
4863 ++p;
4864 }
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004865 MB_PTR_ADV(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004866 }
4867 return p;
4868}
4869
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004870 int
4871get_bad_opt(char_u *p, exarg_T *eap)
4872{
4873 if (STRICMP(p, "keep") == 0)
4874 eap->bad_char = BAD_KEEP;
4875 else if (STRICMP(p, "drop") == 0)
4876 eap->bad_char = BAD_DROP;
4877 else if (MB_BYTE2LEN(*p) == 1 && p[1] == NUL)
4878 eap->bad_char = *p;
Bram Moolenaar75808492018-06-12 12:39:41 +02004879 else
4880 return FAIL;
4881 return OK;
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004882}
4883
Bram Moolenaar071d4272004-06-13 20:20:40 +00004884/*
4885 * Get "++opt=arg" argument.
4886 * Return FAIL or OK.
4887 */
4888 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004889getargopt(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004890{
4891 char_u *arg = eap->arg + 2;
4892 int *pp = NULL;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02004893 int bad_char_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004894 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004895
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004896 // ":edit ++[no]bin[ary] file"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004897 if (STRNCMP(arg, "bin", 3) == 0 || STRNCMP(arg, "nobin", 5) == 0)
4898 {
4899 if (*arg == 'n')
4900 {
4901 arg += 2;
4902 eap->force_bin = FORCE_NOBIN;
4903 }
4904 else
4905 eap->force_bin = FORCE_BIN;
4906 if (!checkforcmd(&arg, "binary", 3))
4907 return FAIL;
4908 eap->arg = skipwhite(arg);
4909 return OK;
4910 }
4911
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004912 // ":read ++edit file"
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004913 if (STRNCMP(arg, "edit", 4) == 0)
4914 {
4915 eap->read_edit = TRUE;
4916 eap->arg = skipwhite(arg + 4);
4917 return OK;
4918 }
4919
Bram Moolenaar071d4272004-06-13 20:20:40 +00004920 if (STRNCMP(arg, "ff", 2) == 0)
4921 {
4922 arg += 2;
4923 pp = &eap->force_ff;
4924 }
4925 else if (STRNCMP(arg, "fileformat", 10) == 0)
4926 {
4927 arg += 10;
4928 pp = &eap->force_ff;
4929 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004930 else if (STRNCMP(arg, "enc", 3) == 0)
4931 {
Bram Moolenaarb38e9ab2011-12-14 14:49:45 +01004932 if (STRNCMP(arg, "encoding", 8) == 0)
4933 arg += 8;
4934 else
4935 arg += 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004936 pp = &eap->force_enc;
4937 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004938 else if (STRNCMP(arg, "bad", 3) == 0)
4939 {
4940 arg += 3;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02004941 pp = &bad_char_idx;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004942 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004943
4944 if (pp == NULL || *arg != '=')
4945 return FAIL;
4946
4947 ++arg;
4948 *pp = (int)(arg - eap->cmd);
4949 arg = skip_cmd_arg(arg, FALSE);
4950 eap->arg = skipwhite(arg);
4951 *arg = NUL;
4952
Bram Moolenaar071d4272004-06-13 20:20:40 +00004953 if (pp == &eap->force_ff)
4954 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004955 if (check_ff_value(eap->cmd + eap->force_ff) == FAIL)
4956 return FAIL;
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004957 eap->force_ff = eap->cmd[eap->force_ff];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004958 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004959 else if (pp == &eap->force_enc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004960 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004961 // Make 'fileencoding' lower case.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004962 for (p = eap->cmd + eap->force_enc; *p != NUL; ++p)
4963 *p = TOLOWER_ASC(*p);
4964 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004965 else
4966 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004967 // Check ++bad= argument. Must be a single-byte character, "keep" or
4968 // "drop".
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004969 if (get_bad_opt(eap->cmd + bad_char_idx, eap) == FAIL)
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004970 return FAIL;
4971 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004972
4973 return OK;
4974}
4975
Bram Moolenaar071d4272004-06-13 20:20:40 +00004976 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004977ex_autocmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004978{
4979 /*
Bram Moolenaar26d4b892018-07-04 22:26:28 +02004980 * Disallow autocommands from .exrc and .vimrc in current
Bram Moolenaar071d4272004-06-13 20:20:40 +00004981 * directory for security reasons.
4982 */
4983 if (secure)
4984 {
4985 secure = 2;
Bram Moolenaar8930caa2020-07-23 16:37:03 +02004986 eap->errmsg = _(e_curdir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004987 }
4988 else if (eap->cmdidx == CMD_autocmd)
4989 do_autocmd(eap->arg, eap->forceit);
4990 else
4991 do_augroup(eap->arg, eap->forceit);
4992}
4993
4994/*
4995 * ":doautocmd": Apply the automatic commands to the current buffer.
4996 */
4997 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004998ex_doautocmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004999{
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005000 char_u *arg = eap->arg;
5001 int call_do_modelines = check_nomodeline(&arg);
Bram Moolenaar1610d052016-06-09 22:53:01 +02005002 int did_aucmd;
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005003
Bram Moolenaar1610d052016-06-09 22:53:01 +02005004 (void)do_doautocmd(arg, TRUE, &did_aucmd);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005005 // Only when there is no <nomodeline>.
Bram Moolenaar1610d052016-06-09 22:53:01 +02005006 if (call_do_modelines && did_aucmd)
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005007 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005008}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005009
Bram Moolenaar071d4272004-06-13 20:20:40 +00005010/*
5011 * :[N]bunload[!] [N] [bufname] unload buffer
5012 * :[N]bdelete[!] [N] [bufname] delete buffer from buffer list
5013 * :[N]bwipeout[!] [N] [bufname] delete buffer really
5014 */
5015 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005016ex_bunload(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005017{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01005018 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02005019 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005020 eap->errmsg = do_bufdel(
5021 eap->cmdidx == CMD_bdelete ? DOBUF_DEL
5022 : eap->cmdidx == CMD_bwipeout ? DOBUF_WIPE
5023 : DOBUF_UNLOAD, eap->arg,
5024 eap->addr_count, (int)eap->line1, (int)eap->line2, eap->forceit);
5025}
5026
5027/*
5028 * :[N]buffer [N] to buffer N
5029 * :[N]sbuffer [N] to buffer N
5030 */
5031 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005032ex_buffer(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005033{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01005034 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02005035 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005036 if (*eap->arg)
Bram Moolenaar8930caa2020-07-23 16:37:03 +02005037 eap->errmsg = ex_errmsg(e_trailing_arg, eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005038 else
5039 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005040 if (eap->addr_count == 0) // default is current buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00005041 goto_buffer(eap, DOBUF_CURRENT, FORWARD, 0);
5042 else
5043 goto_buffer(eap, DOBUF_FIRST, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005044 if (eap->do_ecmd_cmd != NULL)
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01005045 do_cmd_argument(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005046 }
5047}
5048
5049/*
5050 * :[N]bmodified [N] to next mod. buffer
5051 * :[N]sbmodified [N] to next mod. buffer
5052 */
5053 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005054ex_bmodified(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005055{
5056 goto_buffer(eap, DOBUF_MOD, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005057 if (eap->do_ecmd_cmd != NULL)
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01005058 do_cmd_argument(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005059}
5060
5061/*
5062 * :[N]bnext [N] to next buffer
5063 * :[N]sbnext [N] split and to next buffer
5064 */
5065 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005066ex_bnext(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005067{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01005068 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar3029bcc2020-01-18 15:06:19 +01005069 return;
5070
Bram Moolenaar071d4272004-06-13 20:20:40 +00005071 goto_buffer(eap, DOBUF_CURRENT, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005072 if (eap->do_ecmd_cmd != NULL)
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01005073 do_cmd_argument(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005074}
5075
5076/*
5077 * :[N]bNext [N] to previous buffer
5078 * :[N]bprevious [N] to previous buffer
5079 * :[N]sbNext [N] split and to previous buffer
5080 * :[N]sbprevious [N] split and to previous buffer
5081 */
5082 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005083ex_bprevious(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005084{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01005085 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar3029bcc2020-01-18 15:06:19 +01005086 return;
5087
Bram Moolenaar071d4272004-06-13 20:20:40 +00005088 goto_buffer(eap, DOBUF_CURRENT, BACKWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005089 if (eap->do_ecmd_cmd != NULL)
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01005090 do_cmd_argument(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005091}
5092
5093/*
5094 * :brewind to first buffer
5095 * :bfirst to first buffer
5096 * :sbrewind split and to first buffer
5097 * :sbfirst split and to first buffer
5098 */
5099 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005100ex_brewind(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005101{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01005102 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar3029bcc2020-01-18 15:06:19 +01005103 return;
5104
Bram Moolenaar071d4272004-06-13 20:20:40 +00005105 goto_buffer(eap, DOBUF_FIRST, FORWARD, 0);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005106 if (eap->do_ecmd_cmd != NULL)
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01005107 do_cmd_argument(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005108}
5109
5110/*
5111 * :blast to last buffer
5112 * :sblast split and to last buffer
5113 */
5114 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005115ex_blast(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005116{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01005117 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar3029bcc2020-01-18 15:06:19 +01005118 return;
5119
Bram Moolenaar071d4272004-06-13 20:20:40 +00005120 goto_buffer(eap, DOBUF_LAST, BACKWARD, 0);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005121 if (eap->do_ecmd_cmd != NULL)
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01005122 do_cmd_argument(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005123}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005124
Bram Moolenaar7a092242020-04-16 22:10:49 +02005125/*
5126 * Check if "c" ends an Ex command.
Bram Moolenaarb5b94802020-12-13 17:50:20 +01005127 * In Vim9 script does not check for white space before #.
Bram Moolenaar7a092242020-04-16 22:10:49 +02005128 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005129 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005130ends_excmd(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005131{
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005132 int comment_char = '"';
5133
Bram Moolenaarfbda69b2020-04-13 15:06:53 +02005134#ifdef FEAT_EVAL
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005135 if (in_vim9script())
5136 comment_char = '#';
Bram Moolenaar7a092242020-04-16 22:10:49 +02005137#endif
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005138 return (c == NUL || c == '|' || c == comment_char || c == '\n');
Bram Moolenaar7a092242020-04-16 22:10:49 +02005139}
5140
5141/*
5142 * Like ends_excmd() but checks that a # in Vim9 script either has "cmd" equal
5143 * to "cmd_start" or has a white space character before it.
5144 */
5145 int
Bram Moolenaar21cfe502020-04-16 23:01:50 +02005146ends_excmd2(char_u *cmd_start UNUSED, char_u *cmd)
Bram Moolenaar7a092242020-04-16 22:10:49 +02005147{
5148 int c = *cmd;
5149
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005150 if (c == NUL || c == '|' || c == '\n')
5151 return TRUE;
Bram Moolenaar7a092242020-04-16 22:10:49 +02005152#ifdef FEAT_EVAL
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005153 if (in_vim9script())
5154 return c == '#' && cmd[1] != '{'
5155 && (cmd == cmd_start || VIM_ISWHITE(cmd[-1]));
Bram Moolenaarfbda69b2020-04-13 15:06:53 +02005156#endif
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005157 return c == '"';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005158}
5159
5160#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA) || defined(FEAT_EVAL) \
5161 || defined(PROTO)
5162/*
5163 * Return the next command, after the first '|' or '\n'.
5164 * Return NULL if not found.
5165 */
5166 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005167find_nextcmd(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005168{
5169 while (*p != '|' && *p != '\n')
5170 {
5171 if (*p == NUL)
5172 return NULL;
5173 ++p;
5174 }
5175 return (p + 1);
5176}
5177#endif
5178
5179/*
Bram Moolenaar2256c992016-11-15 21:17:07 +01005180 * Check if *p is a separator between Ex commands, skipping over white space.
5181 * Return NULL if it isn't, the following character if it is.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005182 */
5183 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005184check_nextcmd(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005185{
Bram Moolenaar2256c992016-11-15 21:17:07 +01005186 char_u *s = skipwhite(p);
5187
5188 if (*s == '|' || *s == '\n')
5189 return (s + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005190 else
5191 return NULL;
5192}
5193
5194/*
5195 * - if there are more files to edit
5196 * - and this is the last window
5197 * - and forceit not used
5198 * - and not repeated twice on a row
5199 * return FAIL and give error message if 'message' TRUE
5200 * return OK otherwise
5201 */
5202 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005203check_more(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005204 int message, // when FALSE check only, no messages
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005205 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005206{
5207 int n = ARGCOUNT - curwin->w_arg_idx - 1;
5208
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005209 if (!forceit && only_one_window()
Bram Moolenaar7b221172020-08-17 19:34:10 +02005210 && ARGCOUNT > 1 && !arg_had_last && n > 0 && quitmore == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005211 {
5212 if (message)
5213 {
5214#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaare1004402020-10-24 20:49:43 +02005215 if ((p_confirm || (cmdmod.cmod_flags & CMOD_CONFIRM))
5216 && curbuf->b_fname != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005217 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02005218 char_u buff[DIALOG_MSG_SIZE];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005219
Bram Moolenaarda6e8912018-08-21 15:12:14 +02005220 vim_snprintf((char *)buff, DIALOG_MSG_SIZE,
5221 NGETTEXT("%d more file to edit. Quit anyway?",
5222 "%d more files to edit. Quit anyway?", n), n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005223 if (vim_dialog_yesno(VIM_QUESTION, NULL, buff, 1) == VIM_YES)
5224 return OK;
5225 return FAIL;
5226 }
5227#endif
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01005228 semsg(NGETTEXT("E173: %d more file to edit",
5229 "E173: %d more files to edit", n), n);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005230 quitmore = 2; // next try to quit is allowed
Bram Moolenaar071d4272004-06-13 20:20:40 +00005231 }
5232 return FAIL;
5233 }
5234 return OK;
5235}
5236
Bram Moolenaar071d4272004-06-13 20:20:40 +00005237/*
5238 * Function given to ExpandGeneric() to obtain the list of command names.
5239 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005240 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005241get_command_name(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005242{
5243 if (idx >= (int)CMD_SIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005244 return get_user_command_name(idx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005245 return cmdnames[idx].cmd_name;
5246}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005247
Bram Moolenaar071d4272004-06-13 20:20:40 +00005248 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005249ex_colorscheme(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005250{
Bram Moolenaare6850792010-05-14 15:28:44 +02005251 if (*eap->arg == NUL)
5252 {
5253#ifdef FEAT_EVAL
5254 char_u *expr = vim_strsave((char_u *)"g:colors_name");
5255 char_u *p = NULL;
5256
5257 if (expr != NULL)
5258 {
5259 ++emsg_off;
Bram Moolenaarb171fb12020-06-24 20:34:03 +02005260 p = eval_to_string(expr, FALSE);
Bram Moolenaare6850792010-05-14 15:28:44 +02005261 --emsg_off;
5262 vim_free(expr);
5263 }
5264 if (p != NULL)
5265 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01005266 msg((char *)p);
Bram Moolenaare6850792010-05-14 15:28:44 +02005267 vim_free(p);
5268 }
5269 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01005270 msg("default");
Bram Moolenaare6850792010-05-14 15:28:44 +02005271#else
Bram Moolenaar32526b32019-01-19 17:43:09 +01005272 msg(_("unknown"));
Bram Moolenaare6850792010-05-14 15:28:44 +02005273#endif
5274 }
5275 else if (load_colors(eap->arg) == FAIL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005276 semsg(_("E185: Cannot find color scheme '%s'"), eap->arg);
Bram Moolenaarf58d81a2019-01-28 20:19:05 +01005277
5278#ifdef FEAT_VTP
5279 else if (has_vtp_working())
5280 {
5281 // background color change requires clear + redraw
5282 update_screen(CLEAR);
5283 redrawcmd();
5284 }
5285#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005286}
5287
5288 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005289ex_highlight(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005290{
5291 if (*eap->arg == NUL && eap->cmd[2] == '!')
Bram Moolenaar32526b32019-01-19 17:43:09 +01005292 msg(_("Greetings, Vim user!"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005293 do_highlight(eap->arg, eap->forceit, FALSE);
5294}
5295
5296
5297/*
5298 * Call this function if we thought we were going to exit, but we won't
5299 * (because of an error). May need to restore the terminal mode.
5300 */
5301 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005302not_exiting(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005303{
5304 exiting = FALSE;
5305 settmode(TMODE_RAW);
5306}
5307
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005308 static int
5309before_quit_autocmds(win_T *wp, int quit_all, int forceit)
5310{
5311 apply_autocmds(EVENT_QUITPRE, NULL, NULL, FALSE, wp->w_buffer);
5312
Bram Moolenaar34ba06b2019-10-20 22:27:10 +02005313 // Bail out when autocommands closed the window.
5314 // Refuse to quit when the buffer in the last window is being closed (can
5315 // only happen in autocommands).
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005316 if (!win_valid(wp)
5317 || curbuf_locked()
5318 || (wp->w_buffer->b_nwindows == 1 && wp->w_buffer->b_locked > 0))
5319 return TRUE;
5320
5321 if (quit_all || (check_more(FALSE, forceit) == OK && only_one_window()))
5322 {
5323 apply_autocmds(EVENT_EXITPRE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar34ba06b2019-10-20 22:27:10 +02005324 // Refuse to quit when locked or when the window was closed or the
5325 // buffer in the last window is being closed (can only happen in
5326 // autocommands).
5327 if (!win_valid(wp) || curbuf_locked()
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005328 || (curbuf->b_nwindows == 1 && curbuf->b_locked > 0))
5329 return TRUE;
5330 }
5331
5332 return FALSE;
5333}
5334
Bram Moolenaar071d4272004-06-13 20:20:40 +00005335/*
Bram Moolenaarf240e182014-11-27 18:33:02 +01005336 * ":quit": quit current window, quit Vim if the last window is closed.
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005337 * ":{nr}quit": quit window {nr}
Bram Moolenaar4d14bac2019-10-20 21:15:15 +02005338 * Also used when closing a terminal window that's the last one.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005339 */
Bram Moolenaar4d14bac2019-10-20 21:15:15 +02005340 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005341ex_quit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005342{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005343 win_T *wp;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005344
Bram Moolenaar071d4272004-06-13 20:20:40 +00005345#ifdef FEAT_CMDWIN
5346 if (cmdwin_type != 0)
5347 {
5348 cmdwin_result = Ctrl_C;
5349 return;
5350 }
5351#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005352 // Don't quit while editing the command line.
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005353 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005354 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005355 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005356 return;
5357 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005358 if (eap->addr_count > 0)
5359 {
Bram Moolenaarf240e182014-11-27 18:33:02 +01005360 int wnr = eap->line2;
5361
5362 for (wp = firstwin; wp->w_next != NULL; wp = wp->w_next)
5363 if (--wnr <= 0)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005364 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005365 }
5366 else
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005367 wp = curwin;
Bram Moolenaarf240e182014-11-27 18:33:02 +01005368
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005369 // Refuse to quit when locked.
Bram Moolenaar87ffb5c2017-10-19 12:37:42 +02005370 if (curbuf_locked())
5371 return;
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005372
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005373 // Trigger QuitPre and maybe ExitPre
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005374 if (before_quit_autocmds(wp, FALSE, eap->forceit))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005375 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005376
5377#ifdef FEAT_NETBEANS_INTG
5378 netbeansForcedQuit = eap->forceit;
5379#endif
5380
5381 /*
5382 * If there are more files or windows we won't exit.
5383 */
5384 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
5385 exiting = TRUE;
Bram Moolenaarff930ca2017-10-19 17:12:10 +02005386 if ((!buf_hide(wp->w_buffer)
5387 && check_changed(wp->w_buffer, (p_awa ? CCGD_AW : 0)
Bram Moolenaar45d3b142013-11-09 03:31:51 +01005388 | (eap->forceit ? CCGD_FORCEIT : 0)
5389 | CCGD_EXCMD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005390 || check_more(TRUE, eap->forceit) == FAIL
Bram Moolenaar027387f2016-01-02 22:25:52 +01005391 || (only_one_window() && check_changed_any(eap->forceit, TRUE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005392 {
5393 not_exiting();
5394 }
5395 else
5396 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005397 // quit last window
5398 // Note: only_one_window() returns true, even so a help window is
5399 // still open. In that case only quit, if no address has been
5400 // specified. Example:
5401 // :h|wincmd w|1q - don't quit
5402 // :h|wincmd w|q - quit
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01005403 if (only_one_window() && (ONE_WINDOW || eap->addr_count == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005404 getout(0);
Bram Moolenaar2c33d7b2017-10-14 16:06:20 +02005405 not_exiting();
Bram Moolenaar4033c552017-09-16 20:54:51 +02005406#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00005407 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005408#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005409 // close window; may free buffer
Bram Moolenaareb44a682017-08-03 22:44:55 +02005410 win_close(wp, !buf_hide(wp->w_buffer) || eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005411 }
5412}
5413
5414/*
5415 * ":cquit".
5416 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005417 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005418ex_cquit(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005419{
Bram Moolenaar1860bde2020-01-06 21:47:21 +01005420 // this does not always pass on the exit code to the Manx compiler. why?
5421 getout(eap->addr_count > 0 ? (int)eap->line2 : EXIT_FAILURE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005422}
5423
5424/*
5425 * ":qall": try to quit all windows
5426 */
5427 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005428ex_quit_all(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005429{
5430# ifdef FEAT_CMDWIN
5431 if (cmdwin_type != 0)
5432 {
5433 if (eap->forceit)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005434 cmdwin_result = K_XF1; // ex_window() takes care of this
Bram Moolenaar071d4272004-06-13 20:20:40 +00005435 else
5436 cmdwin_result = K_XF2;
5437 return;
5438 }
5439# endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005440
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005441 // Don't quit while editing the command line.
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005442 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005443 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005444 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005445 return;
5446 }
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005447
5448 if (before_quit_autocmds(curwin, TRUE, eap->forceit))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005449 return;
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005450
Bram Moolenaar071d4272004-06-13 20:20:40 +00005451 exiting = TRUE;
Bram Moolenaar027387f2016-01-02 22:25:52 +01005452 if (eap->forceit || !check_changed_any(FALSE, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005453 getout(0);
5454 not_exiting();
5455}
5456
Bram Moolenaar071d4272004-06-13 20:20:40 +00005457/*
5458 * ":close": close current window, unless it is the last one
5459 */
5460 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005461ex_close(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005462{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005463 win_T *win;
5464 int winnr = 0;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005465#ifdef FEAT_CMDWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00005466 if (cmdwin_type != 0)
Bram Moolenaar9bd1a7e2011-05-19 14:50:54 +02005467 cmdwin_result = Ctrl_C;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005468 else
Bram Moolenaar4033c552017-09-16 20:54:51 +02005469#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005470 if (!text_locked() && !curbuf_locked())
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005471 {
5472 if (eap->addr_count == 0)
5473 ex_win_close(eap->forceit, curwin, NULL);
Bram Moolenaar6d41c782018-06-06 09:11:12 +02005474 else
5475 {
Bram Moolenaar29323592016-07-24 22:04:11 +02005476 FOR_ALL_WINDOWS(win)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005477 {
5478 winnr++;
5479 if (winnr == eap->line2)
5480 break;
5481 }
5482 if (win == NULL)
5483 win = lastwin;
5484 ex_win_close(eap->forceit, win, NULL);
5485 }
5486 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005487}
5488
Bram Moolenaar4033c552017-09-16 20:54:51 +02005489#ifdef FEAT_QUICKFIX
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005490/*
5491 * ":pclose": Close any preview window.
5492 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005493 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005494ex_pclose(exarg_T *eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005495{
5496 win_T *win;
5497
Bram Moolenaar79648732019-07-18 21:43:07 +02005498 // First close any normal window.
Bram Moolenaar29323592016-07-24 22:04:11 +02005499 FOR_ALL_WINDOWS(win)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005500 if (win->w_p_pvw)
5501 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00005502 ex_win_close(eap->forceit, win, NULL);
Bram Moolenaar79648732019-07-18 21:43:07 +02005503 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005504 }
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01005505# ifdef FEAT_PROP_POPUP
Bram Moolenaar79648732019-07-18 21:43:07 +02005506 // Also when 'previewpopup' is empty, it might have been cleared.
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02005507 popup_close_preview();
Bram Moolenaar79648732019-07-18 21:43:07 +02005508# endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005509}
Bram Moolenaar4033c552017-09-16 20:54:51 +02005510#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005511
Bram Moolenaarf740b292006-02-16 22:11:02 +00005512/*
5513 * Close window "win" and take care of handling closing the last window for a
5514 * modified buffer.
5515 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005516 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005517ex_win_close(
5518 int forceit,
5519 win_T *win,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005520 tabpage_T *tp) // NULL or the tab page "win" is in
Bram Moolenaar071d4272004-06-13 20:20:40 +00005521{
5522 int need_hide;
5523 buf_T *buf = win->w_buffer;
5524
Bram Moolenaarcf844172020-06-26 19:44:06 +02005525 // Never close the autocommand window.
5526 if (win == aucmd_win)
5527 {
5528 emsg(_(e_autocmd_close));
5529 return;
5530 }
5531
Bram Moolenaar071d4272004-06-13 20:20:40 +00005532 need_hide = (bufIsChanged(buf) && buf->b_nwindows <= 1);
Bram Moolenaareb44a682017-08-03 22:44:55 +02005533 if (need_hide && !buf_hide(buf) && !forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005534 {
Bram Moolenaar4033c552017-09-16 20:54:51 +02005535#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaare1004402020-10-24 20:49:43 +02005536 if ((p_confirm || (cmdmod.cmod_flags & CMOD_CONFIRM)) && p_write)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005537 {
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005538 bufref_T bufref;
5539
5540 set_bufref(&bufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005541 dialog_changed(buf, FALSE);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005542 if (bufref_valid(&bufref) && bufIsChanged(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005543 return;
5544 need_hide = FALSE;
5545 }
5546 else
Bram Moolenaar4033c552017-09-16 20:54:51 +02005547#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005548 {
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02005549 no_write_message();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005550 return;
5551 }
5552 }
5553
Bram Moolenaar4033c552017-09-16 20:54:51 +02005554#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00005555 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005556#endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00005557
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005558 // free buffer when not hiding it or when it's a scratch buffer
Bram Moolenaarf740b292006-02-16 22:11:02 +00005559 if (tp == NULL)
Bram Moolenaareb44a682017-08-03 22:44:55 +02005560 win_close(win, !need_hide && !buf_hide(buf));
Bram Moolenaarf740b292006-02-16 22:11:02 +00005561 else
Bram Moolenaareb44a682017-08-03 22:44:55 +02005562 win_close_othertab(win, !need_hide && !buf_hide(buf), tp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005563}
5564
Bram Moolenaar071d4272004-06-13 20:20:40 +00005565/*
Bram Moolenaardea25702017-01-29 15:18:10 +01005566 * Handle the argument for a tabpage related ex command.
5567 * Returns a tabpage number.
5568 * When an error is encountered then eap->errmsg is set.
5569 */
5570 static int
5571get_tabpage_arg(exarg_T *eap)
5572{
5573 int tab_number;
5574 int unaccept_arg0 = (eap->cmdidx == CMD_tabmove) ? 0 : 1;
5575
5576 if (eap->arg && *eap->arg != NUL)
5577 {
5578 char_u *p = eap->arg;
5579 char_u *p_save;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005580 int relative = 0; // argument +N/-N means: go to N places to the
5581 // right/left relative to the current position.
Bram Moolenaardea25702017-01-29 15:18:10 +01005582
5583 if (*p == '-')
5584 {
5585 relative = -1;
5586 p++;
5587 }
5588 else if (*p == '+')
5589 {
5590 relative = 1;
5591 p++;
5592 }
5593
5594 p_save = p;
5595 tab_number = getdigits(&p);
5596
5597 if (relative == 0)
5598 {
5599 if (STRCMP(p, "$") == 0)
5600 tab_number = LAST_TAB_NR;
Bram Moolenaar94f4ffa2020-08-10 19:21:15 +02005601 else if (STRCMP(p, "#") == 0)
5602 if (valid_tabpage(lastused_tabpage))
5603 tab_number = tabpage_index(lastused_tabpage);
5604 else
5605 {
5606 eap->errmsg = ex_errmsg(e_invargval, eap->arg);
5607 tab_number = 0;
5608 goto theend;
5609 }
Bram Moolenaardea25702017-01-29 15:18:10 +01005610 else if (p == p_save || *p_save == '-' || *p != NUL
5611 || tab_number > LAST_TAB_NR)
5612 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005613 // No numbers as argument.
Bram Moolenaar8930caa2020-07-23 16:37:03 +02005614 eap->errmsg = ex_errmsg(e_invarg2, eap->arg);
Bram Moolenaardea25702017-01-29 15:18:10 +01005615 goto theend;
5616 }
5617 }
5618 else
5619 {
5620 if (*p_save == NUL)
5621 tab_number = 1;
5622 else if (p == p_save || *p_save == '-' || *p != NUL
5623 || tab_number == 0)
5624 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005625 // No numbers as argument.
Bram Moolenaar8930caa2020-07-23 16:37:03 +02005626 eap->errmsg = ex_errmsg(e_invarg2, eap->arg);
Bram Moolenaardea25702017-01-29 15:18:10 +01005627 goto theend;
5628 }
5629 tab_number = tab_number * relative + tabpage_index(curtab);
5630 if (!unaccept_arg0 && relative == -1)
5631 --tab_number;
5632 }
5633 if (tab_number < unaccept_arg0 || tab_number > LAST_TAB_NR)
Bram Moolenaar8930caa2020-07-23 16:37:03 +02005634 eap->errmsg = ex_errmsg(e_invarg2, eap->arg);
Bram Moolenaardea25702017-01-29 15:18:10 +01005635 }
5636 else if (eap->addr_count > 0)
5637 {
5638 if (unaccept_arg0 && eap->line2 == 0)
Bram Moolenaarc6251552017-01-29 21:42:20 +01005639 {
Bram Moolenaar8930caa2020-07-23 16:37:03 +02005640 eap->errmsg = _(e_invrange);
Bram Moolenaarc6251552017-01-29 21:42:20 +01005641 tab_number = 0;
5642 }
Bram Moolenaardea25702017-01-29 15:18:10 +01005643 else
5644 {
5645 tab_number = eap->line2;
Bram Moolenaar82a12462019-01-22 22:41:42 +01005646 if (!unaccept_arg0 && *skipwhite(*eap->cmdlinep) == '-')
Bram Moolenaardea25702017-01-29 15:18:10 +01005647 {
5648 --tab_number;
5649 if (tab_number < unaccept_arg0)
Bram Moolenaar8930caa2020-07-23 16:37:03 +02005650 eap->errmsg = _(e_invrange);
Bram Moolenaardea25702017-01-29 15:18:10 +01005651 }
5652 }
5653 }
5654 else
5655 {
5656 switch (eap->cmdidx)
5657 {
5658 case CMD_tabnext:
5659 tab_number = tabpage_index(curtab) + 1;
5660 if (tab_number > LAST_TAB_NR)
5661 tab_number = 1;
5662 break;
5663 case CMD_tabmove:
5664 tab_number = LAST_TAB_NR;
5665 break;
5666 default:
5667 tab_number = tabpage_index(curtab);
5668 }
5669 }
5670
5671theend:
5672 return tab_number;
5673}
5674
5675/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00005676 * ":tabclose": close current tab page, unless it is the last one.
5677 * ":tabclose N": close tab page N.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005678 */
5679 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005680ex_tabclose(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005681{
Bram Moolenaarf740b292006-02-16 22:11:02 +00005682 tabpage_T *tp;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005683 int tab_number;
Bram Moolenaarf740b292006-02-16 22:11:02 +00005684
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005685# ifdef FEAT_CMDWIN
5686 if (cmdwin_type != 0)
5687 cmdwin_result = K_IGNORE;
5688 else
5689# endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00005690 if (first_tabpage->tp_next == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005691 emsg(_("E784: Cannot close last tab page"));
Bram Moolenaarf740b292006-02-16 22:11:02 +00005692 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005693 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005694 tab_number = get_tabpage_arg(eap);
5695 if (eap->errmsg == NULL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005696 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005697 tp = find_tabpage(tab_number);
Bram Moolenaarf740b292006-02-16 22:11:02 +00005698 if (tp == NULL)
5699 {
5700 beep_flush();
5701 return;
5702 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005703 if (tp != curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00005704 {
5705 tabpage_close_other(tp, eap->forceit);
5706 return;
5707 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005708 else if (!text_locked() && !curbuf_locked())
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005709 tabpage_close(eap->forceit);
5710 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005711 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005712}
5713
5714/*
5715 * ":tabonly": close all tab pages except the current one
5716 */
5717 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005718ex_tabonly(exarg_T *eap)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005719{
5720 tabpage_T *tp;
5721 int done;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005722 int tab_number;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005723
5724# ifdef FEAT_CMDWIN
5725 if (cmdwin_type != 0)
5726 cmdwin_result = K_IGNORE;
5727 else
5728# endif
5729 if (first_tabpage->tp_next == NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01005730 msg(_("Already only one tab page"));
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005731 else
5732 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005733 tab_number = get_tabpage_arg(eap);
5734 if (eap->errmsg == NULL)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005735 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005736 goto_tabpage(tab_number);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005737 // Repeat this up to a 1000 times, because autocommands may
5738 // mess up the lists.
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005739 for (done = 0; done < 1000; ++done)
5740 {
5741 FOR_ALL_TABPAGES(tp)
5742 if (tp->tp_topframe != topframe)
5743 {
5744 tabpage_close_other(tp, eap->forceit);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005745 // if we failed to close it quit
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005746 if (valid_tabpage(tp))
5747 done = 1000;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005748 // start over, "tp" is now invalid
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005749 break;
5750 }
5751 if (first_tabpage->tp_next == NULL)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005752 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005753 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005754 }
5755 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005756}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005757
5758/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00005759 * Close the current tab page.
5760 */
5761 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005762tabpage_close(int forceit)
Bram Moolenaarf740b292006-02-16 22:11:02 +00005763{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005764 // First close all the windows but the current one. If that worked then
5765 // close the last window in this tab, that will close it.
Bram Moolenaar459ca562016-11-10 18:16:33 +01005766 if (!ONE_WINDOW)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005767 close_others(TRUE, forceit);
Bram Moolenaar459ca562016-11-10 18:16:33 +01005768 if (ONE_WINDOW)
Bram Moolenaarf740b292006-02-16 22:11:02 +00005769 ex_win_close(forceit, curwin, NULL);
5770# ifdef FEAT_GUI
5771 need_mouse_correct = TRUE;
5772# endif
5773}
5774
5775/*
5776 * Close tab page "tp", which is not the current tab page.
5777 * Note that autocommands may make "tp" invalid.
Bram Moolenaar7875acc2006-09-10 13:51:17 +00005778 * Also takes care of the tab pages line disappearing when closing the
5779 * last-but-one tab page.
Bram Moolenaarf740b292006-02-16 22:11:02 +00005780 */
5781 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005782tabpage_close_other(tabpage_T *tp, int forceit)
Bram Moolenaarf740b292006-02-16 22:11:02 +00005783{
5784 int done = 0;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005785 win_T *wp;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00005786 int h = tabline_height();
Bram Moolenaarf740b292006-02-16 22:11:02 +00005787
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005788 // Limit to 1000 windows, autocommands may add a window while we close
5789 // one. OK, so I'm paranoid...
Bram Moolenaarf740b292006-02-16 22:11:02 +00005790 while (++done < 1000)
5791 {
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005792 wp = tp->tp_firstwin;
5793 ex_win_close(forceit, wp, tp);
Bram Moolenaarf740b292006-02-16 22:11:02 +00005794
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005795 // Autocommands may delete the tab page under our fingers and we may
5796 // fail to close a window with a modified buffer.
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005797 if (!valid_tabpage(tp) || tp->tp_firstwin == wp)
Bram Moolenaarf740b292006-02-16 22:11:02 +00005798 break;
5799 }
Bram Moolenaar7875acc2006-09-10 13:51:17 +00005800
Bram Moolenaar29323592016-07-24 22:04:11 +02005801 apply_autocmds(EVENT_TABCLOSED, NULL, NULL, FALSE, curbuf);
Bram Moolenaar29323592016-07-24 22:04:11 +02005802
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005803 redraw_tabline = TRUE;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00005804 if (h != tabline_height())
5805 shell_new_rows();
Bram Moolenaarf740b292006-02-16 22:11:02 +00005806}
5807
5808/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005809 * ":only".
5810 */
5811 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005812ex_only(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005813{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005814 win_T *wp;
5815 int wnr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005816# ifdef FEAT_GUI
5817 need_mouse_correct = TRUE;
5818# endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005819 if (eap->addr_count > 0)
5820 {
5821 wnr = eap->line2;
5822 for (wp = firstwin; --wnr > 0; )
5823 {
5824 if (wp->w_next == NULL)
5825 break;
5826 else
5827 wp = wp->w_next;
5828 }
5829 win_goto(wp);
5830 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005831 close_others(TRUE, eap->forceit);
5832}
5833
Bram Moolenaar071d4272004-06-13 20:20:40 +00005834 static void
Bram Moolenaar5e1e6d22017-01-02 17:26:00 +01005835ex_hide(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005836{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005837 // ":hide" or ":hide | cmd": hide current window
Bram Moolenaar2256c992016-11-15 21:17:07 +01005838 if (!eap->skip)
5839 {
Bram Moolenaar4033c552017-09-16 20:54:51 +02005840#ifdef FEAT_GUI
Bram Moolenaar2256c992016-11-15 21:17:07 +01005841 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005842#endif
Bram Moolenaar2256c992016-11-15 21:17:07 +01005843 if (eap->addr_count == 0)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005844 win_close(curwin, FALSE); // don't free buffer
Bram Moolenaar2256c992016-11-15 21:17:07 +01005845 else
5846 {
5847 int winnr = 0;
5848 win_T *win;
Bram Moolenaarf240e182014-11-27 18:33:02 +01005849
Bram Moolenaar2256c992016-11-15 21:17:07 +01005850 FOR_ALL_WINDOWS(win)
5851 {
5852 winnr++;
5853 if (winnr == eap->line2)
5854 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005855 }
Bram Moolenaar2256c992016-11-15 21:17:07 +01005856 if (win == NULL)
5857 win = lastwin;
5858 win_close(win, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005859 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005860 }
5861}
5862
5863/*
5864 * ":stop" and ":suspend": Suspend Vim.
5865 */
5866 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005867ex_stop(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005868{
5869 /*
5870 * Disallow suspending for "rvim".
5871 */
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005872 if (!check_restricted())
Bram Moolenaar071d4272004-06-13 20:20:40 +00005873 {
5874 if (!eap->forceit)
5875 autowrite_all();
Bram Moolenaar100118c2020-12-11 19:30:34 +01005876 apply_autocmds(EVENT_VIMSUSPEND, NULL, NULL, FALSE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005877 windgoto((int)Rows - 1, 0);
5878 out_char('\n');
5879 out_flush();
5880 stoptermcap();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005881 out_flush(); // needed for SUN to restore xterm buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00005882#ifdef FEAT_TITLE
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005883 mch_restore_title(SAVE_RESTORE_BOTH); // restore window titles
Bram Moolenaar071d4272004-06-13 20:20:40 +00005884#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005885 ui_suspend(); // call machine specific function
Bram Moolenaar071d4272004-06-13 20:20:40 +00005886#ifdef FEAT_TITLE
5887 maketitle();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005888 resettitle(); // force updating the title
Bram Moolenaar071d4272004-06-13 20:20:40 +00005889#endif
5890 starttermcap();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005891 scroll_start(); // scroll screen before redrawing
Bram Moolenaar071d4272004-06-13 20:20:40 +00005892 redraw_later_clear();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005893 shell_resized(); // may have resized window
Bram Moolenaar100118c2020-12-11 19:30:34 +01005894 apply_autocmds(EVENT_VIMRESUME, NULL, NULL, FALSE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005895 }
5896}
5897
5898/*
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005899 * ":exit", ":xit" and ":wq": Write file and quite the current window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005900 */
5901 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005902ex_exit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005903{
Bram Moolenaar461f2122020-07-28 20:25:47 +02005904#ifdef FEAT_EVAL
Bram Moolenaarae616492020-07-28 20:07:27 +02005905 if (not_in_vim9(eap) == FAIL)
5906 return;
Bram Moolenaar461f2122020-07-28 20:25:47 +02005907#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005908#ifdef FEAT_CMDWIN
5909 if (cmdwin_type != 0)
5910 {
5911 cmdwin_result = Ctrl_C;
5912 return;
5913 }
5914#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005915 // Don't quit while editing the command line.
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005916 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005917 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005918 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005919 return;
5920 }
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005921
5922 if (before_quit_autocmds(curwin, FALSE, eap->forceit))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005923 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005924
5925 /*
5926 * if more files or windows we won't exit
5927 */
5928 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
5929 exiting = TRUE;
5930 if ( ((eap->cmdidx == CMD_wq
5931 || curbufIsChanged())
5932 && do_write(eap) == FAIL)
5933 || check_more(TRUE, eap->forceit) == FAIL
Bram Moolenaar027387f2016-01-02 22:25:52 +01005934 || (only_one_window() && check_changed_any(eap->forceit, FALSE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005935 {
5936 not_exiting();
5937 }
5938 else
5939 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005940 if (only_one_window()) // quit last window, exit Vim
Bram Moolenaar071d4272004-06-13 20:20:40 +00005941 getout(0);
Bram Moolenaar2c33d7b2017-10-14 16:06:20 +02005942 not_exiting();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005943# ifdef FEAT_GUI
5944 need_mouse_correct = TRUE;
5945# endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005946 // Quit current window, may free the buffer.
Bram Moolenaareb44a682017-08-03 22:44:55 +02005947 win_close(curwin, !buf_hide(curwin->w_buffer));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005948 }
5949}
5950
5951/*
5952 * ":print", ":list", ":number".
5953 */
5954 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005955ex_print(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005956{
Bram Moolenaardf177f62005-02-22 08:39:57 +00005957 if (curbuf->b_ml.ml_flags & ML_EMPTY)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005958 emsg(_(e_emptybuf));
Bram Moolenaardf177f62005-02-22 08:39:57 +00005959 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005960 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00005961 for ( ;!got_int; ui_breakcheck())
5962 {
5963 print_line(eap->line1,
5964 (eap->cmdidx == CMD_number || eap->cmdidx == CMD_pound
5965 || (eap->flags & EXFLAG_NR)),
5966 eap->cmdidx == CMD_list || (eap->flags & EXFLAG_LIST));
5967 if (++eap->line1 > eap->line2)
5968 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005969 out_flush(); // show one line at a time
Bram Moolenaardf177f62005-02-22 08:39:57 +00005970 }
5971 setpcmark();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005972 // put cursor at last line
Bram Moolenaardf177f62005-02-22 08:39:57 +00005973 curwin->w_cursor.lnum = eap->line2;
5974 beginline(BL_SOL | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005975 }
5976
Bram Moolenaar071d4272004-06-13 20:20:40 +00005977 ex_no_reprint = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005978}
5979
5980#ifdef FEAT_BYTEOFF
5981 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005982ex_goto(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005983{
5984 goto_byte(eap->line2);
5985}
5986#endif
5987
5988/*
5989 * ":shell".
5990 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005991 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005992ex_shell(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005993{
5994 do_shell(NULL, 0);
5995}
5996
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005997#if defined(HAVE_DROP_FILE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005998
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005999static int drop_busy = FALSE;
6000static int drop_filec;
6001static char_u **drop_filev = NULL;
6002static int drop_split;
6003static void (*drop_callback)(void *);
6004static void *drop_cookie;
6005
6006 static void
6007handle_drop_internal(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006008{
6009 exarg_T ea;
6010 int save_msg_scroll = msg_scroll;
6011
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006012 // Setting the argument list may cause screen updates and being called
6013 // recursively. Avoid that by setting drop_busy.
6014 drop_busy = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006015
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006016 // Check whether the current buffer is changed. If so, we will need
6017 // to split the current window or data could be lost.
6018 // We don't need to check if the 'hidden' option is set, as in this
6019 // case the buffer won't be lost.
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006020 if (!buf_hide(curbuf) && !drop_split)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006021 {
6022 ++emsg_off;
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006023 drop_split = check_changed(curbuf, CCGD_AW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006024 --emsg_off;
6025 }
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006026 if (drop_split)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006027 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006028 if (win_split(0, 0) == FAIL)
6029 return;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02006030 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006031
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006032 // When splitting the window, create a new alist. Otherwise the
6033 // existing one is overwritten.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006034 alist_unlink(curwin->w_alist);
6035 alist_new();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006036 }
6037
6038 /*
6039 * Set up the new argument list.
6040 */
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006041 alist_set(ALIST(curwin), drop_filec, drop_filev, FALSE, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006042
6043 /*
6044 * Move to the first file.
6045 */
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006046 // Fake up a minimal "next" command for do_argfile()
Bram Moolenaara80faa82020-04-12 19:37:17 +02006047 CLEAR_FIELD(ea);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006048 ea.cmd = (char_u *)"next";
6049 do_argfile(&ea, 0);
6050
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006051 // do_ecmd() may set need_start_insertmode, but since we never left Insert
6052 // mode that is not needed here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006053 need_start_insertmode = FALSE;
6054
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006055 // Restore msg_scroll, otherwise a following command may cause scrolling
6056 // unexpectedly. The screen will be redrawn by the caller, thus
6057 // msg_scroll being set by displaying a message is irrelevant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006058 msg_scroll = save_msg_scroll;
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006059
6060 if (drop_callback != NULL)
6061 drop_callback(drop_cookie);
6062
6063 drop_filev = NULL;
6064 drop_busy = FALSE;
6065}
6066
6067/*
6068 * Handle a file drop. The code is here because a drop is *nearly* like an
6069 * :args command, but not quite (we have a list of exact filenames, so we
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01006070 * don't want to (a) parse a command line, or (b) expand wildcards). So the
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006071 * code is very similar to :args and hence needs access to a lot of the static
6072 * functions in this file.
6073 *
6074 * The "filev" list must have been allocated using alloc(), as should each item
6075 * in the list. This function takes over responsibility for freeing the "filev"
6076 * list.
6077 */
6078 void
6079handle_drop(
6080 int filec, // the number of files dropped
6081 char_u **filev, // the list of files dropped
6082 int split, // force splitting the window
6083 void (*callback)(void *), // to be called after setting the argument
6084 // list
6085 void *cookie) // argument for "callback" (allocated)
6086{
6087 // Cannot handle recursive drops, finish the pending one.
6088 if (drop_busy)
6089 {
6090 FreeWild(filec, filev);
6091 vim_free(cookie);
6092 return;
6093 }
6094
6095 // When calling handle_drop() more than once in a row we only use the last
6096 // one.
6097 if (drop_filev != NULL)
6098 {
6099 FreeWild(drop_filec, drop_filev);
6100 vim_free(drop_cookie);
6101 }
6102
6103 drop_filec = filec;
6104 drop_filev = filev;
6105 drop_split = split;
6106 drop_callback = callback;
6107 drop_cookie = cookie;
6108
6109 // Postpone this when:
6110 // - editing the command line
6111 // - not possible to change the current buffer
6112 // - updating the screen
6113 // As it may change buffers and window structures that are in use and cause
6114 // freed memory to be used.
6115 if (text_locked() || curbuf_locked() || updating_screen)
6116 return;
6117
6118 handle_drop_internal();
6119}
6120
6121/*
6122 * To be called when text is unlocked, curbuf is unlocked or updating_screen is
6123 * reset: Handle a postponed drop.
6124 */
6125 void
6126handle_any_postponed_drop(void)
6127{
6128 if (!drop_busy && drop_filev != NULL
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +02006129 && !text_locked() && !curbuf_locked() && !updating_screen)
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006130 handle_drop_internal();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006131}
6132#endif
6133
Bram Moolenaar071d4272004-06-13 20:20:40 +00006134/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006135 * ":preserve".
6136 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006137 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006138ex_preserve(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006139{
Bram Moolenaar4399ef42005-02-12 14:29:27 +00006140 curbuf->b_flags |= BF_PRESERVED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006141 ml_preserve(curbuf, TRUE);
6142}
6143
6144/*
6145 * ":recover".
6146 */
6147 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006148ex_recover(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006149{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006150 // Set recoverymode right away to avoid the ATTENTION prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006151 recoverymode = TRUE;
Bram Moolenaar45d3b142013-11-09 03:31:51 +01006152 if (!check_changed(curbuf, (p_awa ? CCGD_AW : 0)
6153 | CCGD_MULTWIN
6154 | (eap->forceit ? CCGD_FORCEIT : 0)
6155 | CCGD_EXCMD)
6156
Bram Moolenaar071d4272004-06-13 20:20:40 +00006157 && (*eap->arg == NUL
6158 || setfname(curbuf, eap->arg, NULL, TRUE) == OK))
Bram Moolenaar99499b12019-05-23 21:35:48 +02006159 ml_recover(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006160 recoverymode = FALSE;
6161}
6162
6163/*
6164 * Command modifier used in a wrong way.
6165 */
6166 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006167ex_wrongmodifier(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006168{
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006169 eap->errmsg = _(e_invalid_command);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006170}
6171
Bram Moolenaar071d4272004-06-13 20:20:40 +00006172/*
6173 * :sview [+command] file split window with new file, read-only
6174 * :split [[+command] file] split window with current or new file
6175 * :vsplit [[+command] file] split window vertically with current or new file
6176 * :new [[+command] file] split window with no or new file
6177 * :vnew [[+command] file] split vertically window with no or new file
6178 * :sfind [+command] file split window with file in 'path'
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006179 *
6180 * :tabedit open new Tab page with empty window
6181 * :tabedit [+command] file open new Tab page and edit "file"
6182 * :tabnew [[+command] file] just like :tabedit
6183 * :tabfind [+command] file open new Tab page and find "file"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006184 */
6185 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006186ex_splitview(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006187{
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006188 win_T *old_curwin = curwin;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006189#if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006190 char_u *fname = NULL;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006191#endif
6192#ifdef FEAT_BROWSE
Bram Moolenaar21cbe172020-10-13 19:08:24 +02006193 char_u dot_path[] = ".";
Bram Moolenaare1004402020-10-24 20:49:43 +02006194 int save_cmod_flags = cmdmod.cmod_flags;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006195#endif
Bram Moolenaar39902a02018-06-21 22:10:08 +02006196 int use_tab = eap->cmdidx == CMD_tabedit
6197 || eap->cmdidx == CMD_tabfind
6198 || eap->cmdidx == CMD_tabnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006199
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01006200 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02006201 return;
6202
Bram Moolenaar4033c552017-09-16 20:54:51 +02006203#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00006204 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006205#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006206
Bram Moolenaar4033c552017-09-16 20:54:51 +02006207#ifdef FEAT_QUICKFIX
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006208 // A ":split" in the quickfix window works like ":new". Don't want two
6209 // quickfix windows. But it's OK when doing ":tab split".
Bram Moolenaare1004402020-10-24 20:49:43 +02006210 if (bt_quickfix(curbuf) && cmdmod.cmod_tab == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006211 {
6212 if (eap->cmdidx == CMD_split)
6213 eap->cmdidx = CMD_new;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006214 if (eap->cmdidx == CMD_vsplit)
6215 eap->cmdidx = CMD_vnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006216 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02006217#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006218
Bram Moolenaar4033c552017-09-16 20:54:51 +02006219#ifdef FEAT_SEARCHPATH
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006220 if (eap->cmdidx == CMD_sfind || eap->cmdidx == CMD_tabfind)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006221 {
6222 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg),
6223 FNAME_MESS, TRUE, curbuf->b_ffname);
6224 if (fname == NULL)
6225 goto theend;
6226 eap->arg = fname;
6227 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006228# ifdef FEAT_BROWSE
Bram Moolenaar4033c552017-09-16 20:54:51 +02006229 else
6230# endif
6231#endif
6232#ifdef FEAT_BROWSE
Bram Moolenaare1004402020-10-24 20:49:43 +02006233 if ((cmdmod.cmod_flags & CMOD_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006234 && eap->cmdidx != CMD_vnew
Bram Moolenaar071d4272004-06-13 20:20:40 +00006235 && eap->cmdidx != CMD_new)
6236 {
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006237 if (
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006238# ifdef FEAT_GUI
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006239 !gui.in_use &&
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006240# endif
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006241 au_has_group((char_u *)"FileExplorer"))
6242 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006243 // No browsing supported but we do have the file explorer:
6244 // Edit the directory.
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006245 if (*eap->arg == NUL || !mch_isdir(eap->arg))
Bram Moolenaar21cbe172020-10-13 19:08:24 +02006246 eap->arg = dot_path;
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006247 }
6248 else
6249 {
Bram Moolenaar39902a02018-06-21 22:10:08 +02006250 fname = do_browse(0, (char_u *)(use_tab
6251 ? _("Edit File in new tab page")
6252 : _("Edit File in new window")),
Bram Moolenaar071d4272004-06-13 20:20:40 +00006253 eap->arg, NULL, NULL, NULL, curbuf);
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006254 if (fname == NULL)
6255 goto theend;
6256 eap->arg = fname;
6257 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006258 }
Bram Moolenaare1004402020-10-24 20:49:43 +02006259 cmdmod.cmod_flags &= ~CMOD_BROWSE; // Don't browse again in do_ecmd().
Bram Moolenaar4033c552017-09-16 20:54:51 +02006260#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006261
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006262 /*
6263 * Either open new tab page or split the window.
6264 */
Bram Moolenaar39902a02018-06-21 22:10:08 +02006265 if (use_tab)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006266 {
Bram Moolenaare1004402020-10-24 20:49:43 +02006267 if (win_new_tabpage(cmdmod.cmod_tab != 0 ? cmdmod.cmod_tab
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006268 : eap->addr_count == 0 ? 0
6269 : (int)eap->line2 + 1) != FAIL)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006270 {
Bram Moolenaard2b66012008-01-09 19:30:36 +00006271 do_exedit(eap, old_curwin);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006272
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006273 // set the alternate buffer for the window we came from
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006274 if (curwin != old_curwin
6275 && win_valid(old_curwin)
6276 && old_curwin->w_buffer != curbuf
Bram Moolenaare1004402020-10-24 20:49:43 +02006277 && (cmdmod.cmod_flags & CMOD_KEEPALT) == 0)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006278 old_curwin->w_alt_fnum = curbuf->b_fnum;
6279 }
6280 }
6281 else if (win_split(eap->addr_count > 0 ? (int)eap->line2 : 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006282 *eap->cmd == 'v' ? WSP_VERT : 0) != FAIL)
6283 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006284 // Reset 'scrollbind' when editing another file, but keep it when
6285 // doing ":split" without arguments.
Bram Moolenaare1004402020-10-24 20:49:43 +02006286 if (*eap->arg != NUL)
Bram Moolenaar3368ea22010-09-21 16:56:35 +02006287 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006288 else
6289 do_check_scrollbind(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006290 do_exedit(eap, old_curwin);
6291 }
6292
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006293# ifdef FEAT_BROWSE
Bram Moolenaare1004402020-10-24 20:49:43 +02006294 cmdmod.cmod_flags = save_cmod_flags;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006295# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006296
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006297# if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006298theend:
6299 vim_free(fname);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006300# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006301}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006302
6303/*
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006304 * Open a new tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006305 */
6306 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006307tabpage_new(void)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006308{
6309 exarg_T ea;
6310
Bram Moolenaara80faa82020-04-12 19:37:17 +02006311 CLEAR_FIELD(ea);
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006312 ea.cmdidx = CMD_tabnew;
6313 ea.cmd = (char_u *)"tabn";
6314 ea.arg = (char_u *)"";
6315 ex_splitview(&ea);
6316}
6317
6318/*
6319 * :tabnext command
6320 */
6321 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006322ex_tabnext(exarg_T *eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006323{
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006324 int tab_number;
6325
Bram Moolenaar8cdbd5b2019-06-16 15:50:45 +02006326 if (ERROR_IF_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02006327 return;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006328 switch (eap->cmdidx)
6329 {
6330 case CMD_tabfirst:
6331 case CMD_tabrewind:
6332 goto_tabpage(1);
6333 break;
6334 case CMD_tablast:
6335 goto_tabpage(9999);
6336 break;
6337 case CMD_tabprevious:
6338 case CMD_tabNext:
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006339 if (eap->arg && *eap->arg != NUL)
6340 {
6341 char_u *p = eap->arg;
6342 char_u *p_save = p;
6343
6344 tab_number = getdigits(&p);
6345 if (p == p_save || *p_save == '-' || *p != NUL
6346 || tab_number == 0)
6347 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006348 // No numbers as argument.
Bram Moolenaar8930caa2020-07-23 16:37:03 +02006349 eap->errmsg = ex_errmsg(e_invarg2, eap->arg);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006350 return;
6351 }
6352 }
6353 else
6354 {
6355 if (eap->addr_count == 0)
6356 tab_number = 1;
6357 else
6358 {
6359 tab_number = eap->line2;
6360 if (tab_number < 1)
6361 {
Bram Moolenaar8930caa2020-07-23 16:37:03 +02006362 eap->errmsg = _(e_invrange);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006363 return;
6364 }
6365 }
6366 }
6367 goto_tabpage(-tab_number);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006368 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006369 default: // CMD_tabnext
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006370 tab_number = get_tabpage_arg(eap);
6371 if (eap->errmsg == NULL)
6372 goto_tabpage(tab_number);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006373 break;
6374 }
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006375}
6376
6377/*
6378 * :tabmove command
6379 */
6380 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006381ex_tabmove(exarg_T *eap)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006382{
Bram Moolenaar40ce3a42015-04-21 18:08:39 +02006383 int tab_number;
Bram Moolenaar8cb8dca2012-07-06 18:27:39 +02006384
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006385 tab_number = get_tabpage_arg(eap);
6386 if (eap->errmsg == NULL)
6387 tabpage_move(tab_number);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006388}
6389
6390/*
6391 * :tabs command: List tabs and their contents.
6392 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006393 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006394ex_tabs(exarg_T *eap UNUSED)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006395{
6396 tabpage_T *tp;
6397 win_T *wp;
6398 int tabcount = 1;
6399
6400 msg_start();
6401 msg_scroll = TRUE;
6402 for (tp = first_tabpage; tp != NULL && !got_int; tp = tp->tp_next)
6403 {
6404 msg_putchar('\n');
6405 vim_snprintf((char *)IObuff, IOSIZE, _("Tab page %d"), tabcount++);
Bram Moolenaar8820b482017-03-16 17:23:31 +01006406 msg_outtrans_attr(IObuff, HL_ATTR(HLF_T));
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006407 out_flush(); // output one line at a time
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006408 ui_breakcheck();
6409
Bram Moolenaar030f0df2006-02-21 22:02:53 +00006410 if (tp == curtab)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006411 wp = firstwin;
6412 else
6413 wp = tp->tp_firstwin;
6414 for ( ; wp != NULL && !got_int; wp = wp->w_next)
6415 {
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006416 msg_putchar('\n');
6417 msg_putchar(wp == curwin ? '>' : ' ');
6418 msg_putchar(' ');
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006419 msg_putchar(bufIsChanged(wp->w_buffer) ? '+' : ' ');
6420 msg_putchar(' ');
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006421 if (buf_spname(wp->w_buffer) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02006422 vim_strncpy(IObuff, buf_spname(wp->w_buffer), IOSIZE - 1);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006423 else
6424 home_replace(wp->w_buffer, wp->w_buffer->b_fname,
6425 IObuff, IOSIZE, TRUE);
6426 msg_outtrans(IObuff);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006427 out_flush(); // output one line at a time
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006428 ui_breakcheck();
6429 }
6430 }
6431}
6432
Bram Moolenaar071d4272004-06-13 20:20:40 +00006433/*
6434 * ":mode": Set screen mode.
6435 * If no argument given, just get the screen size and redraw.
6436 */
6437 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006438ex_mode(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006439{
6440 if (*eap->arg == NUL)
6441 shell_resized();
6442 else
Bram Moolenaar3c71aec2020-01-17 19:32:20 +01006443 emsg(_(e_screenmode));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006444}
6445
Bram Moolenaar071d4272004-06-13 20:20:40 +00006446/*
6447 * ":resize".
6448 * set, increment or decrement current window height
6449 */
6450 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006451ex_resize(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006452{
6453 int n;
6454 win_T *wp = curwin;
6455
6456 if (eap->addr_count > 0)
6457 {
6458 n = eap->line2;
6459 for (wp = firstwin; wp->w_next != NULL && --n > 0; wp = wp->w_next)
6460 ;
6461 }
6462
Bram Moolenaar44a2f922016-03-19 22:11:51 +01006463# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00006464 need_mouse_correct = TRUE;
Bram Moolenaar44a2f922016-03-19 22:11:51 +01006465# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006466 n = atol((char *)eap->arg);
Bram Moolenaare1004402020-10-24 20:49:43 +02006467 if (cmdmod.cmod_split & WSP_VERT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006468 {
6469 if (*eap->arg == '-' || *eap->arg == '+')
Bram Moolenaar9668cc52020-10-17 17:39:55 +02006470 n += wp->w_width;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006471 else if (n == 0 && eap->arg[0] == NUL) // default is very wide
Bram Moolenaar071d4272004-06-13 20:20:40 +00006472 n = 9999;
6473 win_setwidth_win((int)n, wp);
6474 }
6475 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006476 {
6477 if (*eap->arg == '-' || *eap->arg == '+')
Bram Moolenaar9668cc52020-10-17 17:39:55 +02006478 n += wp->w_height;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006479 else if (n == 0 && eap->arg[0] == NUL) // default is very high
Bram Moolenaar071d4272004-06-13 20:20:40 +00006480 n = 9999;
6481 win_setheight_win((int)n, wp);
6482 }
6483}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006484
6485/*
6486 * ":find [+command] <file>" command.
6487 */
6488 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006489ex_find(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006490{
6491#ifdef FEAT_SEARCHPATH
6492 char_u *fname;
6493 int count;
6494
6495 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg), FNAME_MESS,
6496 TRUE, curbuf->b_ffname);
6497 if (eap->addr_count > 0)
6498 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006499 // Repeat finding the file "count" times. This matters when it
6500 // appears several times in the path.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006501 count = eap->line2;
6502 while (fname != NULL && --count > 0)
6503 {
6504 vim_free(fname);
6505 fname = find_file_in_path(NULL, 0, FNAME_MESS,
6506 FALSE, curbuf->b_ffname);
6507 }
6508 }
6509
6510 if (fname != NULL)
6511 {
6512 eap->arg = fname;
6513#endif
6514 do_exedit(eap, NULL);
6515#ifdef FEAT_SEARCHPATH
6516 vim_free(fname);
6517 }
6518#endif
6519}
6520
6521/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00006522 * ":open" simulation: for now just work like ":visual".
6523 */
6524 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006525ex_open(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00006526{
6527 regmatch_T regmatch;
6528 char_u *p;
6529
6530 curwin->w_cursor.lnum = eap->line2;
6531 beginline(BL_SOL | BL_FIX);
6532 if (*eap->arg == '/')
6533 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006534 // ":open /pattern/": put cursor in column found with pattern
Bram Moolenaardf177f62005-02-22 08:39:57 +00006535 ++eap->arg;
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006536 p = skip_regexp(eap->arg, '/', p_magic);
Bram Moolenaardf177f62005-02-22 08:39:57 +00006537 *p = NUL;
6538 regmatch.regprog = vim_regcomp(eap->arg, p_magic ? RE_MAGIC : 0);
6539 if (regmatch.regprog != NULL)
6540 {
6541 regmatch.rm_ic = p_ic;
6542 p = ml_get_curline();
6543 if (vim_regexec(&regmatch, p, (colnr_T)0))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006544 curwin->w_cursor.col = (colnr_T)(regmatch.startp[0] - p);
Bram Moolenaardf177f62005-02-22 08:39:57 +00006545 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006546 emsg(_(e_nomatch));
Bram Moolenaar473de612013-06-08 18:19:48 +02006547 vim_regfree(regmatch.regprog);
Bram Moolenaardf177f62005-02-22 08:39:57 +00006548 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006549 // Move to the NUL, ignore any other arguments.
Bram Moolenaardf177f62005-02-22 08:39:57 +00006550 eap->arg += STRLEN(eap->arg);
6551 }
6552 check_cursor();
6553
6554 eap->cmdidx = CMD_visual;
6555 do_exedit(eap, NULL);
6556}
6557
6558/*
Bram Moolenaar59d8e562020-11-07 18:41:10 +01006559 * ":edit", ":badd", ":balt", ":visual".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006560 */
6561 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006562ex_edit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006563{
6564 do_exedit(eap, NULL);
6565}
6566
6567/*
Bram Moolenaar4b96df52020-01-26 22:00:26 +01006568 * ":edit <file>" command and alike.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006569 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006570 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006571do_exedit(
6572 exarg_T *eap,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006573 win_T *old_curwin) // curwin before doing a split or NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006574{
6575 int n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006576 int need_hide;
Bram Moolenaardf177f62005-02-22 08:39:57 +00006577 int exmode_was = exmode_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006578
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01006579 if ((eap->cmdidx != CMD_pedit && ERROR_IF_POPUP_WINDOW)
6580 || ERROR_IF_TERM_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02006581 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006582 /*
6583 * ":vi" command ends Ex mode.
6584 */
6585 if (exmode_active && (eap->cmdidx == CMD_visual
6586 || eap->cmdidx == CMD_view))
6587 {
6588 exmode_active = FALSE;
Bram Moolenaar158ea172020-06-18 17:28:39 +02006589 ex_pressedreturn = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006590 if (*eap->arg == NUL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00006591 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006592 // Special case: ":global/pat/visual\NLvi-commands"
Bram Moolenaardf177f62005-02-22 08:39:57 +00006593 if (global_busy)
6594 {
6595 int rd = RedrawingDisabled;
6596 int nwr = no_wait_return;
6597 int ms = msg_scroll;
6598#ifdef FEAT_GUI
6599 int he = hold_gui_events;
6600#endif
6601
6602 if (eap->nextcmd != NULL)
6603 {
6604 stuffReadbuff(eap->nextcmd);
6605 eap->nextcmd = NULL;
6606 }
6607
6608 if (exmode_was != EXMODE_VIM)
6609 settmode(TMODE_RAW);
6610 RedrawingDisabled = 0;
6611 no_wait_return = 0;
6612 need_wait_return = FALSE;
6613 msg_scroll = 0;
6614#ifdef FEAT_GUI
6615 hold_gui_events = 0;
6616#endif
6617 must_redraw = CLEAR;
Bram Moolenaar9e2bcb52020-02-18 21:33:00 +01006618 pending_exmode_active = TRUE;
Bram Moolenaardf177f62005-02-22 08:39:57 +00006619
6620 main_loop(FALSE, TRUE);
6621
Bram Moolenaar9e2bcb52020-02-18 21:33:00 +01006622 pending_exmode_active = FALSE;
Bram Moolenaardf177f62005-02-22 08:39:57 +00006623 RedrawingDisabled = rd;
6624 no_wait_return = nwr;
6625 msg_scroll = ms;
6626#ifdef FEAT_GUI
6627 hold_gui_events = he;
6628#endif
6629 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006630 return;
Bram Moolenaardf177f62005-02-22 08:39:57 +00006631 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006632 }
6633
6634 if ((eap->cmdidx == CMD_new
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006635 || eap->cmdidx == CMD_tabnew
6636 || eap->cmdidx == CMD_tabedit
Bram Moolenaar4033c552017-09-16 20:54:51 +02006637 || eap->cmdidx == CMD_vnew) && *eap->arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006638 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006639 // ":new" or ":tabnew" without argument: edit an new empty buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00006640 setpcmark();
6641 (void)do_ecmd(0, NULL, NULL, eap, ECMD_ONE,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00006642 ECMD_HIDE + (eap->forceit ? ECMD_FORCEIT : 0),
6643 old_curwin == NULL ? curwin : NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006644 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02006645 else if ((eap->cmdidx != CMD_split && eap->cmdidx != CMD_vsplit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006646 || *eap->arg != NUL
6647#ifdef FEAT_BROWSE
Bram Moolenaare1004402020-10-24 20:49:43 +02006648 || (cmdmod.cmod_flags & CMOD_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006649#endif
6650 )
6651 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006652 // Can't edit another file when "curbuf_lock" is set. Only ":edit"
6653 // can bring us here, others are stopped earlier.
Bram Moolenaar5555acc2006-04-07 21:33:12 +00006654 if (*eap->arg != NUL && curbuf_locked())
6655 return;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006656
Bram Moolenaar071d4272004-06-13 20:20:40 +00006657 n = readonlymode;
6658 if (eap->cmdidx == CMD_view || eap->cmdidx == CMD_sview)
6659 readonlymode = TRUE;
6660 else if (eap->cmdidx == CMD_enew)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006661 readonlymode = FALSE; // 'readonly' doesn't make sense in an
6662 // empty buffer
Bram Moolenaar3482be62020-11-27 11:00:38 +01006663 if (eap->cmdidx != CMD_balt && eap->cmdidx != CMD_badd)
6664 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006665 if (do_ecmd(0, (eap->cmdidx == CMD_enew ? NULL : eap->arg),
6666 NULL, eap,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006667 // ":edit" goes to first line if Vi compatible
Bram Moolenaar071d4272004-06-13 20:20:40 +00006668 (*eap->arg == NUL && eap->do_ecmd_lnum == 0
6669 && vim_strchr(p_cpo, CPO_GOTO1) != NULL)
6670 ? ECMD_ONE : eap->do_ecmd_lnum,
Bram Moolenaareb44a682017-08-03 22:44:55 +02006671 (buf_hide(curbuf) ? ECMD_HIDE : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006672 + (eap->forceit ? ECMD_FORCEIT : 0)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006673 // after a split we can use an existing buffer
Bram Moolenaar7b449342014-03-25 13:03:48 +01006674 + (old_curwin != NULL ? ECMD_OLDBUF : 0)
Bram Moolenaar59d8e562020-11-07 18:41:10 +01006675 + (eap->cmdidx == CMD_badd ? ECMD_ADDBUF : 0)
6676 + (eap->cmdidx == CMD_balt ? ECMD_ALTBUF : 0)
Bram Moolenaar701f7af2008-11-15 13:12:07 +00006677 , old_curwin == NULL ? curwin : NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006678 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006679 // Editing the file failed. If the window was split, close it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006680 if (old_curwin != NULL)
6681 {
6682 need_hide = (curbufIsChanged() && curbuf->b_nwindows <= 1);
Bram Moolenaareb44a682017-08-03 22:44:55 +02006683 if (!need_hide || buf_hide(curbuf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006684 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006685#if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00006686 cleanup_T cs;
6687
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006688 // Reset the error/interrupt/exception state here so that
6689 // aborting() returns FALSE when closing a window.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00006690 enter_cleanup(&cs);
Bram Moolenaar4033c552017-09-16 20:54:51 +02006691#endif
6692#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00006693 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006694#endif
Bram Moolenaareb44a682017-08-03 22:44:55 +02006695 win_close(curwin, !need_hide && !buf_hide(curbuf));
Bram Moolenaarc0197e22004-09-13 20:26:32 +00006696
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006697#if defined(FEAT_EVAL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006698 // Restore the error/interrupt/exception state if not
6699 // discarded by a new aborting error, interrupt, or
6700 // uncaught exception.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00006701 leave_cleanup(&cs);
Bram Moolenaar4033c552017-09-16 20:54:51 +02006702#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006703 }
6704 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006705 }
6706 else if (readonlymode && curbuf->b_nwindows == 1)
6707 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006708 // When editing an already visited buffer, 'readonly' won't be set
6709 // but the previous value is kept. With ":view" and ":sview" we
6710 // want the file to be readonly, except when another window is
6711 // editing the same buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006712 curbuf->b_p_ro = TRUE;
6713 }
6714 readonlymode = n;
6715 }
6716 else
6717 {
6718 if (eap->do_ecmd_cmd != NULL)
Bram Moolenaar47a2abf2020-11-25 20:12:11 +01006719 do_cmd_argument(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006720#ifdef FEAT_TITLE
6721 n = curwin->w_arg_idx_invalid;
6722#endif
6723 check_arg_idx(curwin);
6724#ifdef FEAT_TITLE
6725 if (n != curwin->w_arg_idx_invalid)
6726 maketitle();
6727#endif
6728 }
6729
Bram Moolenaar071d4272004-06-13 20:20:40 +00006730 /*
6731 * if ":split file" worked, set alternate file name in old window to new
6732 * file
6733 */
6734 if (old_curwin != NULL
6735 && *eap->arg != NUL
6736 && curwin != old_curwin
6737 && win_valid(old_curwin)
Bram Moolenaard4755bb2004-09-02 19:12:26 +00006738 && old_curwin->w_buffer != curbuf
Bram Moolenaare1004402020-10-24 20:49:43 +02006739 && (cmdmod.cmod_flags & CMOD_KEEPALT) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006740 old_curwin->w_alt_fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006741
6742 ex_no_reprint = TRUE;
6743}
6744
6745#ifndef FEAT_GUI
6746/*
6747 * ":gui" and ":gvim" when there is no GUI.
6748 */
6749 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006750ex_nogui(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006751{
Bram Moolenaar8930caa2020-07-23 16:37:03 +02006752 eap->errmsg = _(e_nogvim);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006753}
6754#endif
6755
Bram Moolenaar4f974752019-02-17 17:44:42 +01006756#if defined(FEAT_GUI_MSWIN) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006757 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006758ex_tearoff(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006759{
6760 gui_make_tearoff(eap->arg);
6761}
6762#endif
6763
Bram Moolenaar29a2c082018-03-05 21:06:23 +01006764#if (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK) \
6765 || defined(FEAT_TERM_POPUP_MENU)) && defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006766 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006767ex_popup(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006768{
Bram Moolenaar29a2c082018-03-05 21:06:23 +01006769# if defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK)
6770 if (gui.in_use)
6771 gui_make_popup(eap->arg, eap->forceit);
6772# ifdef FEAT_TERM_POPUP_MENU
6773 else
6774# endif
6775# endif
6776# ifdef FEAT_TERM_POPUP_MENU
6777 pum_make_popup(eap->arg, eap->forceit);
6778# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006779}
6780#endif
6781
Bram Moolenaar071d4272004-06-13 20:20:40 +00006782 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006783ex_swapname(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006784{
6785 if (curbuf->b_ml.ml_mfp == NULL || curbuf->b_ml.ml_mfp->mf_fname == NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01006786 msg(_("No swap file"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006787 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01006788 msg((char *)curbuf->b_ml.ml_mfp->mf_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006789}
6790
6791/*
6792 * ":syncbind" forces all 'scrollbind' windows to have the same relative
6793 * offset.
6794 * (1998-11-02 16:21:01 R. Edward Ralston <eralston@computer.org>)
6795 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006796 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006797ex_syncbind(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006798{
Bram Moolenaar071d4272004-06-13 20:20:40 +00006799 win_T *wp;
Bram Moolenaardedd1b02013-12-14 13:06:17 +01006800 win_T *save_curwin = curwin;
6801 buf_T *save_curbuf = curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006802 long topline;
6803 long y;
6804 linenr_T old_linenr = curwin->w_cursor.lnum;
6805
6806 setpcmark();
6807
6808 /*
6809 * determine max topline
6810 */
6811 if (curwin->w_p_scb)
6812 {
6813 topline = curwin->w_topline;
Bram Moolenaar29323592016-07-24 22:04:11 +02006814 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006815 {
6816 if (wp->w_p_scb && wp->w_buffer)
6817 {
Bram Moolenaar375e3392019-01-31 18:26:10 +01006818 y = wp->w_buffer->b_ml.ml_line_count - get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006819 if (topline > y)
6820 topline = y;
6821 }
6822 }
6823 if (topline < 1)
6824 topline = 1;
6825 }
6826 else
6827 {
6828 topline = 1;
6829 }
6830
6831
6832 /*
Bram Moolenaardedd1b02013-12-14 13:06:17 +01006833 * Set all scrollbind windows to the same topline.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006834 */
Bram Moolenaar29323592016-07-24 22:04:11 +02006835 FOR_ALL_WINDOWS(curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006836 {
6837 if (curwin->w_p_scb)
6838 {
Bram Moolenaardedd1b02013-12-14 13:06:17 +01006839 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006840 y = topline - curwin->w_topline;
6841 if (y > 0)
6842 scrollup(y, TRUE);
6843 else
6844 scrolldown(-y, TRUE);
6845 curwin->w_scbind_pos = topline;
6846 redraw_later(VALID);
6847 cursor_correct();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006848 curwin->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006849 }
6850 }
Bram Moolenaardedd1b02013-12-14 13:06:17 +01006851 curwin = save_curwin;
6852 curbuf = save_curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006853 if (curwin->w_p_scb)
6854 {
6855 did_syncbind = TRUE;
6856 checkpcmark();
6857 if (old_linenr != curwin->w_cursor.lnum)
6858 {
6859 char_u ctrl_o[2];
6860
6861 ctrl_o[0] = Ctrl_O;
6862 ctrl_o[1] = 0;
6863 ins_typebuf(ctrl_o, REMAP_NONE, 0, TRUE, FALSE);
6864 }
6865 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006866}
6867
6868
6869 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006870ex_read(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006871{
Bram Moolenaardf177f62005-02-22 08:39:57 +00006872 int i;
6873 int empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
6874 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006875
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006876 if (eap->usefilter) // :r!cmd
Bram Moolenaar071d4272004-06-13 20:20:40 +00006877 do_bang(1, eap, FALSE, FALSE, TRUE);
6878 else
6879 {
6880 if (u_save(eap->line2, (linenr_T)(eap->line2 + 1)) == FAIL)
6881 return;
6882
6883#ifdef FEAT_BROWSE
Bram Moolenaare1004402020-10-24 20:49:43 +02006884 if (cmdmod.cmod_flags & CMOD_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006885 {
6886 char_u *browseFile;
6887
Bram Moolenaar7171abe2004-10-11 10:06:20 +00006888 browseFile = do_browse(0, (char_u *)_("Append File"), eap->arg,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006889 NULL, NULL, NULL, curbuf);
6890 if (browseFile != NULL)
6891 {
6892 i = readfile(browseFile, NULL,
6893 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
6894 vim_free(browseFile);
6895 }
6896 else
6897 i = OK;
6898 }
6899 else
6900#endif
6901 if (*eap->arg == NUL)
6902 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006903 if (check_fname() == FAIL) // check for no file name
Bram Moolenaar071d4272004-06-13 20:20:40 +00006904 return;
6905 i = readfile(curbuf->b_ffname, curbuf->b_fname,
6906 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
6907 }
6908 else
6909 {
6910 if (vim_strchr(p_cpo, CPO_ALTREAD) != NULL)
6911 (void)setaltfname(eap->arg, eap->arg, (linenr_T)1);
6912 i = readfile(eap->arg, NULL,
6913 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
6914
6915 }
Bram Moolenaare13b9af2017-01-13 22:01:02 +01006916 if (i != OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006917 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006918#if defined(FEAT_EVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006919 if (!aborting())
6920#endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006921 semsg(_(e_notopen), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006922 }
6923 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00006924 {
6925 if (empty && exmode_active)
6926 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006927 // Delete the empty line that remains. Historically ex does
6928 // this but vi doesn't.
Bram Moolenaardf177f62005-02-22 08:39:57 +00006929 if (eap->line2 == 0)
6930 lnum = curbuf->b_ml.ml_line_count;
6931 else
6932 lnum = 1;
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00006933 if (*ml_get(lnum) == NUL && u_savedel(lnum, 1L) == OK)
Bram Moolenaardf177f62005-02-22 08:39:57 +00006934 {
Bram Moolenaarca70c072020-05-30 20:30:46 +02006935 ml_delete(lnum);
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00006936 if (curwin->w_cursor.lnum > 1
6937 && curwin->w_cursor.lnum >= lnum)
Bram Moolenaardf177f62005-02-22 08:39:57 +00006938 --curwin->w_cursor.lnum;
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00006939 deleted_lines_mark(lnum, 1L);
Bram Moolenaardf177f62005-02-22 08:39:57 +00006940 }
6941 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006942 redraw_curbuf_later(VALID);
Bram Moolenaardf177f62005-02-22 08:39:57 +00006943 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006944 }
6945}
6946
Bram Moolenaarea408852005-06-25 22:49:46 +00006947static char_u *prev_dir = NULL;
6948
6949#if defined(EXITFREE) || defined(PROTO)
6950 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006951free_cd_dir(void)
Bram Moolenaarea408852005-06-25 22:49:46 +00006952{
Bram Moolenaard23a8232018-02-10 18:45:26 +01006953 VIM_CLEAR(prev_dir);
6954 VIM_CLEAR(globaldir);
Bram Moolenaarea408852005-06-25 22:49:46 +00006955}
6956#endif
6957
Bram Moolenaarf4258302013-06-02 18:20:17 +02006958/*
Bram Moolenaar002bc792020-06-05 22:33:42 +02006959 * Get the previous directory for the given chdir scope.
6960 */
6961 static char_u *
6962get_prevdir(cdscope_T scope)
6963{
6964 if (scope == CDSCOPE_WINDOW)
6965 return curwin->w_prevdir;
6966 else if (scope == CDSCOPE_TABPAGE)
6967 return curtab->tp_prevdir;
6968 return prev_dir;
6969}
6970
6971/*
Bram Moolenaarf4258302013-06-02 18:20:17 +02006972 * Deal with the side effects of changing the current directory.
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006973 * When 'scope' is CDSCOPE_TABPAGE then this was after an ":tcd" command.
6974 * When 'scope' is CDSCOPE_WINDOW then this was after an ":lcd" command.
Bram Moolenaarf4258302013-06-02 18:20:17 +02006975 */
6976 void
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006977post_chdir(cdscope_T scope)
Bram Moolenaarf4258302013-06-02 18:20:17 +02006978{
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006979 if (scope != CDSCOPE_WINDOW)
Bram Moolenaar00aa0692019-04-27 20:37:57 +02006980 // Clear tab local directory for both :cd and :tcd
6981 VIM_CLEAR(curtab->tp_localdir);
Bram Moolenaard23a8232018-02-10 18:45:26 +01006982 VIM_CLEAR(curwin->w_localdir);
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006983 if (scope != CDSCOPE_GLOBAL)
Bram Moolenaarf4258302013-06-02 18:20:17 +02006984 {
Bram Moolenaar002bc792020-06-05 22:33:42 +02006985 char_u *pdir = get_prevdir(scope);
6986
6987 // If still in the global directory, need to remember current
6988 // directory as the global directory.
6989 if (globaldir == NULL && pdir != NULL)
6990 globaldir = vim_strsave(pdir);
6991
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006992 // Remember this local directory for the window.
Bram Moolenaarf4258302013-06-02 18:20:17 +02006993 if (mch_dirname(NameBuff, MAXPATHL) == OK)
Bram Moolenaar00aa0692019-04-27 20:37:57 +02006994 {
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006995 if (scope == CDSCOPE_TABPAGE)
Bram Moolenaar00aa0692019-04-27 20:37:57 +02006996 curtab->tp_localdir = vim_strsave(NameBuff);
6997 else
6998 curwin->w_localdir = vim_strsave(NameBuff);
6999 }
Bram Moolenaarf4258302013-06-02 18:20:17 +02007000 }
7001 else
7002 {
Bram Moolenaar002bc792020-06-05 22:33:42 +02007003 // We are now in the global directory, no need to remember its name.
Bram Moolenaard23a8232018-02-10 18:45:26 +01007004 VIM_CLEAR(globaldir);
Bram Moolenaarf4258302013-06-02 18:20:17 +02007005 }
7006
7007 shorten_fnames(TRUE);
7008}
7009
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007010/*
7011 * Change directory function used by :cd/:tcd/:lcd Ex commands and the
Bram Moolenaar95058722020-06-01 16:26:19 +02007012 * chdir() function.
7013 * scope == CDSCOPE_WINDOW: changes the window-local directory
7014 * scope == CDSCOPE_TABPAGE: changes the tab-local directory
7015 * Otherwise: changes the global directory
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007016 * Returns TRUE if the directory is successfully changed.
7017 */
7018 int
7019changedir_func(
7020 char_u *new_dir,
7021 int forceit,
7022 cdscope_T scope)
7023{
7024 char_u *tofree;
Bram Moolenaar002bc792020-06-05 22:33:42 +02007025 char_u *pdir = NULL;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007026 int dir_differs;
7027 int retval = FALSE;
7028
Bram Moolenaar7cc96922020-01-31 22:41:38 +01007029 if (new_dir == NULL || allbuf_locked())
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007030 return FALSE;
7031
7032 if (vim_strchr(p_cpo, CPO_CHDIR) != NULL && curbufIsChanged() && !forceit)
7033 {
7034 emsg(_("E747: Cannot change directory, buffer is modified (add ! to override)"));
7035 return FALSE;
7036 }
7037
7038 // ":cd -": Change to previous directory
7039 if (STRCMP(new_dir, "-") == 0)
7040 {
Bram Moolenaar002bc792020-06-05 22:33:42 +02007041 pdir = get_prevdir(scope);
7042 if (pdir == NULL)
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007043 {
7044 emsg(_("E186: No previous directory"));
7045 return FALSE;
7046 }
Bram Moolenaar002bc792020-06-05 22:33:42 +02007047 new_dir = pdir;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007048 }
7049
Bram Moolenaar002bc792020-06-05 22:33:42 +02007050 // Free the previous directory
7051 tofree = get_prevdir(scope);
7052
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007053 // Save current directory for next ":cd -"
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007054 if (mch_dirname(NameBuff, MAXPATHL) == OK)
Bram Moolenaar002bc792020-06-05 22:33:42 +02007055 pdir = vim_strsave(NameBuff);
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007056 else
Bram Moolenaar002bc792020-06-05 22:33:42 +02007057 pdir = NULL;
7058 if (scope == CDSCOPE_WINDOW)
7059 curwin->w_prevdir = pdir;
7060 else if (scope == CDSCOPE_TABPAGE)
7061 curtab->tp_prevdir = pdir;
7062 else
7063 prev_dir = pdir;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007064
7065#if defined(UNIX) || defined(VMS)
7066 // for UNIX ":cd" means: go to home directory
7067 if (*new_dir == NUL)
7068 {
7069 // use NameBuff for home directory name
7070# ifdef VMS
7071 char_u *p;
7072
7073 p = mch_getenv((char_u *)"SYS$LOGIN");
7074 if (p == NULL || *p == NUL) // empty is the same as not set
7075 NameBuff[0] = NUL;
7076 else
7077 vim_strncpy(NameBuff, p, MAXPATHL - 1);
7078# else
7079 expand_env((char_u *)"$HOME", NameBuff, MAXPATHL);
7080# endif
7081 new_dir = NameBuff;
7082 }
7083#endif
Bram Moolenaar002bc792020-06-05 22:33:42 +02007084 dir_differs = new_dir == NULL || pdir == NULL
7085 || pathcmp((char *)pdir, (char *)new_dir, -1) != 0;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007086 if (new_dir == NULL || (dir_differs && vim_chdir(new_dir)))
7087 emsg(_(e_failed));
7088 else
7089 {
7090 char_u *acmd_fname;
7091
7092 post_chdir(scope);
7093
7094 if (dir_differs)
7095 {
7096 if (scope == CDSCOPE_WINDOW)
7097 acmd_fname = (char_u *)"window";
7098 else if (scope == CDSCOPE_TABPAGE)
7099 acmd_fname = (char_u *)"tabpage";
7100 else
7101 acmd_fname = (char_u *)"global";
7102 apply_autocmds(EVENT_DIRCHANGED, acmd_fname, new_dir, FALSE,
7103 curbuf);
7104 }
7105 retval = TRUE;
7106 }
7107 vim_free(tofree);
7108
7109 return retval;
7110}
Bram Moolenaarea408852005-06-25 22:49:46 +00007111
Bram Moolenaar071d4272004-06-13 20:20:40 +00007112/*
Bram Moolenaar00aa0692019-04-27 20:37:57 +02007113 * ":cd", ":tcd", ":lcd", ":chdir" ":tchdir" and ":lchdir".
Bram Moolenaar071d4272004-06-13 20:20:40 +00007114 */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00007115 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007116ex_cd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007117{
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01007118 char_u *new_dir;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007119
7120 new_dir = eap->arg;
7121#if !defined(UNIX) && !defined(VMS)
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007122 // for non-UNIX ":cd" means: print current directory
Bram Moolenaar071d4272004-06-13 20:20:40 +00007123 if (*new_dir == NUL)
7124 ex_pwd(NULL);
7125 else
7126#endif
7127 {
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007128 cdscope_T scope = CDSCOPE_GLOBAL;
7129
7130 if (eap->cmdidx == CMD_lcd || eap->cmdidx == CMD_lchdir)
7131 scope = CDSCOPE_WINDOW;
7132 else if (eap->cmdidx == CMD_tcd || eap->cmdidx == CMD_tchdir)
7133 scope = CDSCOPE_TABPAGE;
7134
7135 if (changedir_func(new_dir, eap->forceit, scope))
Bram Moolenaardf177f62005-02-22 08:39:57 +00007136 {
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007137 // Echo the new current directory if the command was typed.
Bram Moolenaarfcfbc672009-07-09 18:13:49 +00007138 if (KeyTyped || p_verbose >= 5)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007139 ex_pwd(eap);
7140 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007141 }
7142}
7143
7144/*
7145 * ":pwd".
7146 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007147 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007148ex_pwd(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007149{
7150 if (mch_dirname(NameBuff, MAXPATHL) == OK)
7151 {
7152#ifdef BACKSLASH_IN_FILENAME
7153 slash_adjust(NameBuff);
7154#endif
Bram Moolenaar95058722020-06-01 16:26:19 +02007155 if (p_verbose > 0)
7156 {
7157 char *context = "global";
7158
7159 if (curwin->w_localdir != NULL)
7160 context = "window";
7161 else if (curtab->tp_localdir != NULL)
7162 context = "tabpage";
7163 smsg("[%s] %s", context, (char *)NameBuff);
7164 }
7165 else
7166 msg((char *)NameBuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007167 }
7168 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007169 emsg(_("E187: Unknown"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007170}
7171
7172/*
7173 * ":=".
7174 */
7175 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007176ex_equal(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007177{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007178 smsg("%ld", (long)eap->line2);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007179 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007180}
7181
7182 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007183ex_sleep(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007184{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007185 int n;
7186 long len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007187
7188 if (cursor_valid())
7189 {
7190 n = W_WINROW(curwin) + curwin->w_wrow - msg_scrolled;
7191 if (n >= 0)
Bram Moolenaar53f81742017-09-22 14:35:51 +02007192 windgoto((int)n, curwin->w_wincol + curwin->w_wcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007193 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007194
7195 len = eap->line2;
7196 switch (*eap->arg)
7197 {
7198 case 'm': break;
7199 case NUL: len *= 1000L; break;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007200 default: semsg(_(e_invarg2), eap->arg); return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007201 }
7202 do_sleep(len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007203}
7204
7205/*
7206 * Sleep for "msec" milliseconds, but keep checking for a CTRL-C every second.
7207 */
7208 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007209do_sleep(long msec)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007210{
Bram Moolenaar52953192019-08-16 10:27:13 +02007211 long done = 0;
Bram Moolenaar975b5272016-03-15 23:10:59 +01007212 long wait_now;
Bram Moolenaar52953192019-08-16 10:27:13 +02007213# ifdef ELAPSED_FUNC
7214 elapsed_T start_tv;
7215
7216 // Remember at what time we started, so that we know how much longer we
7217 // should wait after waiting for a bit.
7218 ELAPSED_INIT(start_tv);
7219# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007220
7221 cursor_on();
Bram Moolenaarf45d7472018-09-30 18:22:26 +02007222 out_flush_cursor(FALSE, FALSE);
Bram Moolenaar52953192019-08-16 10:27:13 +02007223 while (!got_int && done < msec)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007224 {
Bram Moolenaar975b5272016-03-15 23:10:59 +01007225 wait_now = msec - done > 1000L ? 1000L : msec - done;
7226#ifdef FEAT_TIMERS
7227 {
7228 long due_time = check_due_timer();
7229
7230 if (due_time > 0 && due_time < wait_now)
7231 wait_now = due_time;
7232 }
7233#endif
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02007234#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar28e67e02019-08-15 23:05:49 +02007235 if (has_any_channel() && wait_now > 20L)
7236 wait_now = 20L;
7237#endif
7238#ifdef FEAT_SOUND
7239 if (has_any_sound_callback() && wait_now > 20L)
7240 wait_now = 20L;
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02007241#endif
Bram Moolenaar975b5272016-03-15 23:10:59 +01007242 ui_delay(wait_now, TRUE);
Bram Moolenaar52953192019-08-16 10:27:13 +02007243
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02007244#ifdef FEAT_JOB_CHANNEL
7245 if (has_any_channel())
7246 ui_breakcheck_force(TRUE);
7247 else
7248#endif
7249 ui_breakcheck();
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007250#ifdef MESSAGE_QUEUE
Bram Moolenaar52953192019-08-16 10:27:13 +02007251 // Process the netbeans and clientserver messages that may have been
7252 // received in the call to ui_breakcheck() when the GUI is in use. This
7253 // may occur when running a test case.
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007254 parse_queued_messages();
Bram Moolenaare3cc6d42011-10-20 21:58:34 +02007255#endif
Bram Moolenaar52953192019-08-16 10:27:13 +02007256
7257# ifdef ELAPSED_FUNC
7258 // actual time passed
7259 done = ELAPSED_FUNC(start_tv);
7260# else
7261 // guestimate time passed (will actually be more)
7262 done += wait_now;
7263# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007264 }
Bram Moolenaar1ebff3d2018-07-07 16:18:13 +02007265
7266 // If CTRL-C was typed to interrupt the sleep, drop the CTRL-C from the
7267 // input buffer, otherwise a following call to input() fails.
7268 if (got_int)
7269 (void)vpeekc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007270}
7271
Bram Moolenaar071d4272004-06-13 20:20:40 +00007272/*
7273 * ":winsize" command (obsolete).
7274 */
7275 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007276ex_winsize(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007277{
7278 int w, h;
7279 char_u *arg = eap->arg;
7280 char_u *p;
7281
7282 w = getdigits(&arg);
7283 arg = skipwhite(arg);
7284 p = arg;
7285 h = getdigits(&arg);
7286 if (*p != NUL && *arg == NUL)
7287 set_shellsize(w, h, TRUE);
7288 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007289 emsg(_("E465: :winsize requires two number arguments"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007290}
7291
Bram Moolenaar071d4272004-06-13 20:20:40 +00007292 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007293ex_wincmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007294{
7295 int xchar = NUL;
7296 char_u *p;
7297
7298 if (*eap->arg == 'g' || *eap->arg == Ctrl_G)
7299 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007300 // CTRL-W g and CTRL-W CTRL-G have an extra command character
Bram Moolenaar071d4272004-06-13 20:20:40 +00007301 if (eap->arg[1] == NUL)
7302 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007303 emsg(_(e_invarg));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007304 return;
7305 }
7306 xchar = eap->arg[1];
7307 p = eap->arg + 2;
7308 }
7309 else
7310 p = eap->arg + 1;
7311
7312 eap->nextcmd = check_nextcmd(p);
7313 p = skipwhite(p);
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02007314 if (*p != NUL && *p != (
7315#ifdef FEAT_EVAL
7316 in_vim9script() ? '#' :
7317#endif
7318 '"')
7319 && eap->nextcmd == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007320 emsg(_(e_invarg));
Bram Moolenaar12bde492011-06-13 01:19:56 +02007321 else if (!eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007322 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007323 // Pass flags on for ":vertical wincmd ]".
Bram Moolenaare1004402020-10-24 20:49:43 +02007324 postponed_split_flags = cmdmod.cmod_split;
7325 postponed_split_tab = cmdmod.cmod_tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007326 do_window(*eap->arg, eap->addr_count > 0 ? eap->line2 : 0L, xchar);
7327 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00007328 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007329 }
7330}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007331
Bram Moolenaar843ee412004-06-30 16:16:41 +00007332#if defined(FEAT_GUI) || defined(UNIX) || defined(VMS) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007333/*
7334 * ":winpos".
7335 */
7336 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007337ex_winpos(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007338{
7339 int x, y;
7340 char_u *arg = eap->arg;
7341 char_u *p;
7342
7343 if (*arg == NUL)
7344 {
Bram Moolenaar843ee412004-06-30 16:16:41 +00007345# if defined(FEAT_GUI) || defined(MSWIN)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007346# ifdef VIMDLL
7347 if (gui.in_use ? gui_mch_get_winpos(&x, &y) != FAIL :
7348 mch_get_winpos(&x, &y) != FAIL)
7349# elif defined(FEAT_GUI)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007350 if (gui.in_use && gui_mch_get_winpos(&x, &y) != FAIL)
Bram Moolenaar843ee412004-06-30 16:16:41 +00007351# else
7352 if (mch_get_winpos(&x, &y) != FAIL)
7353# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007354 {
7355 sprintf((char *)IObuff, _("Window position: X %d, Y %d"), x, y);
Bram Moolenaar32526b32019-01-19 17:43:09 +01007356 msg((char *)IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007357 }
7358 else
7359# endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007360 emsg(_("E188: Obtaining window position not implemented for this platform"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007361 }
7362 else
7363 {
7364 x = getdigits(&arg);
7365 arg = skipwhite(arg);
7366 p = arg;
7367 y = getdigits(&arg);
7368 if (*p == NUL || *arg != NUL)
7369 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007370 emsg(_("E466: :winpos requires two number arguments"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007371 return;
7372 }
7373# ifdef FEAT_GUI
7374 if (gui.in_use)
7375 gui_mch_set_winpos(x, y);
7376 else if (gui.starting)
7377 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007378 // Remember the coordinates for when the window is opened.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007379 gui_win_x = x;
7380 gui_win_y = y;
7381 }
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007382# if defined(HAVE_TGETENT) || defined(VIMDLL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007383 else
7384# endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007385# endif
7386# if defined(MSWIN) && (!defined(FEAT_GUI) || defined(VIMDLL))
Bram Moolenaar843ee412004-06-30 16:16:41 +00007387 mch_set_winpos(x, y);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007388# endif
7389# ifdef HAVE_TGETENT
7390 if (*T_CWP)
7391 term_set_winpos(x, y);
7392# endif
7393 }
7394}
7395#endif
7396
7397/*
7398 * Handle command that work like operators: ":delete", ":yank", ":>" and ":<".
7399 */
7400 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007401ex_operators(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007402{
7403 oparg_T oa;
7404
7405 clear_oparg(&oa);
7406 oa.regname = eap->regname;
7407 oa.start.lnum = eap->line1;
7408 oa.end.lnum = eap->line2;
7409 oa.line_count = eap->line2 - eap->line1 + 1;
7410 oa.motion_type = MLINE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007411 virtual_op = FALSE;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007412 if (eap->cmdidx != CMD_yank) // position cursor for undo
Bram Moolenaar071d4272004-06-13 20:20:40 +00007413 {
7414 setpcmark();
7415 curwin->w_cursor.lnum = eap->line1;
7416 beginline(BL_SOL | BL_FIX);
7417 }
7418
Bram Moolenaard07c6e12013-11-21 14:21:40 +01007419 if (VIsual_active)
7420 end_visual_mode();
Bram Moolenaard07c6e12013-11-21 14:21:40 +01007421
Bram Moolenaar071d4272004-06-13 20:20:40 +00007422 switch (eap->cmdidx)
7423 {
7424 case CMD_delete:
7425 oa.op_type = OP_DELETE;
7426 op_delete(&oa);
7427 break;
7428
7429 case CMD_yank:
7430 oa.op_type = OP_YANK;
7431 (void)op_yank(&oa, FALSE, TRUE);
7432 break;
7433
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007434 default: // CMD_rshift or CMD_lshift
Bram Moolenaar6e707362013-06-14 19:15:58 +02007435 if (
Bram Moolenaar071d4272004-06-13 20:20:40 +00007436#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6e707362013-06-14 19:15:58 +02007437 (eap->cmdidx == CMD_rshift) ^ curwin->w_p_rl
7438#else
7439 eap->cmdidx == CMD_rshift
Bram Moolenaar071d4272004-06-13 20:20:40 +00007440#endif
Bram Moolenaar6e707362013-06-14 19:15:58 +02007441 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00007442 oa.op_type = OP_RSHIFT;
7443 else
7444 oa.op_type = OP_LSHIFT;
7445 op_shift(&oa, FALSE, eap->amount);
7446 break;
7447 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007448 virtual_op = MAYBE;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007449 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007450}
7451
7452/*
7453 * ":put".
7454 */
7455 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007456ex_put(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007457{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007458 // ":0put" works like ":1put!".
Bram Moolenaar071d4272004-06-13 20:20:40 +00007459 if (eap->line2 == 0)
7460 {
7461 eap->line2 = 1;
7462 eap->forceit = TRUE;
7463 }
7464 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaarc3516f72020-09-08 22:45:35 +02007465 do_put(eap->regname, NULL, eap->forceit ? BACKWARD : FORWARD, 1L,
Bram Moolenaardf177f62005-02-22 08:39:57 +00007466 PUT_LINE|PUT_CURSLINE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007467}
7468
7469/*
7470 * Handle ":copy" and ":move".
7471 */
7472 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007473ex_copymove(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007474{
7475 long n;
7476
Bram Moolenaar491799b2020-08-01 19:23:43 +02007477#ifdef FEAT_EVAL
Bram Moolenaarf5a48012020-08-01 17:00:03 +02007478 if (not_in_vim9(eap) == FAIL)
7479 return;
Bram Moolenaar491799b2020-08-01 19:23:43 +02007480#endif
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02007481 n = get_address(eap, &eap->arg, eap->addr_type, FALSE, FALSE, FALSE, 1);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007482 if (eap->arg == NULL) // error detected
Bram Moolenaar071d4272004-06-13 20:20:40 +00007483 {
7484 eap->nextcmd = NULL;
7485 return;
7486 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00007487 get_flags(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007488
7489 /*
7490 * move or copy lines from 'eap->line1'-'eap->line2' to below line 'n'
7491 */
7492 if (n == MAXLNUM || n < 0 || n > curbuf->b_ml.ml_line_count)
7493 {
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02007494 emsg(_(e_invrange));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007495 return;
7496 }
7497
7498 if (eap->cmdidx == CMD_move)
7499 {
7500 if (do_move(eap->line1, eap->line2, n) == FAIL)
7501 return;
7502 }
7503 else
7504 ex_copy(eap->line1, eap->line2, n);
7505 u_clearline();
7506 beginline(BL_SOL | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007507 ex_may_print(eap);
7508}
7509
7510/*
7511 * Print the current line if flags were given to the Ex command.
7512 */
Bram Moolenaarfd3fe982014-04-01 17:49:44 +02007513 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007514ex_may_print(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007515{
7516 if (eap->flags != 0)
7517 {
7518 print_line(curwin->w_cursor.lnum, (eap->flags & EXFLAG_NR),
7519 (eap->flags & EXFLAG_LIST));
7520 ex_no_reprint = TRUE;
7521 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007522}
7523
7524/*
7525 * ":smagic" and ":snomagic".
7526 */
7527 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007528ex_submagic(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007529{
7530 int magic_save = p_magic;
7531
7532 p_magic = (eap->cmdidx == CMD_smagic);
Bram Moolenaar66e00142020-08-12 21:58:12 +02007533 ex_substitute(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007534 p_magic = magic_save;
7535}
7536
7537/*
7538 * ":join".
7539 */
7540 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007541ex_join(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007542{
7543 curwin->w_cursor.lnum = eap->line1;
7544 if (eap->line1 == eap->line2)
7545 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007546 if (eap->addr_count >= 2) // :2,2join does nothing
Bram Moolenaar071d4272004-06-13 20:20:40 +00007547 return;
7548 if (eap->line2 == curbuf->b_ml.ml_line_count)
7549 {
7550 beep_flush();
7551 return;
7552 }
7553 ++eap->line2;
7554 }
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02007555 (void)do_join(eap->line2 - eap->line1 + 1, !eap->forceit, TRUE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007556 beginline(BL_WHITE | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007557 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007558}
7559
7560/*
7561 * ":[addr]@r" or ":[addr]*r": execute register
7562 */
7563 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007564ex_at(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007565{
7566 int c;
Bram Moolenaar60462872009-11-03 11:40:19 +00007567 int prev_len = typebuf.tb_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007568
7569 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaar4930a762016-09-11 14:39:53 +02007570 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007571
7572#ifdef USE_ON_FLY_SCROLL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007573 dont_scroll = TRUE; // disallow scrolling here
Bram Moolenaar071d4272004-06-13 20:20:40 +00007574#endif
7575
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007576 // get the register name. No name means to use the previous one
Bram Moolenaar071d4272004-06-13 20:20:40 +00007577 c = *eap->arg;
7578 if (c == NUL || (c == '*' && *eap->cmd == '*'))
7579 c = '@';
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007580 // Put the register in the typeahead buffer with the "silent" flag.
Bram Moolenaard333d1e2006-11-07 17:43:47 +00007581 if (do_execreg(c, TRUE, vim_strchr(p_cpo, CPO_EXECBUF) != NULL, TRUE)
7582 == FAIL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007583 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007584 beep_flush();
Bram Moolenaardf177f62005-02-22 08:39:57 +00007585 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007586 else
7587 {
7588 int save_efr = exec_from_reg;
7589
7590 exec_from_reg = TRUE;
7591
7592 /*
7593 * Execute from the typeahead buffer.
Bram Moolenaar60462872009-11-03 11:40:19 +00007594 * Continue until the stuff buffer is empty and all added characters
7595 * have been consumed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007596 */
Bram Moolenaar60462872009-11-03 11:40:19 +00007597 while (!stuff_empty() || typebuf.tb_len > prev_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007598 (void)do_cmdline(NULL, getexline, NULL, DOCMD_NOWAIT|DOCMD_VERBOSE);
7599
7600 exec_from_reg = save_efr;
7601 }
7602}
7603
7604/*
7605 * ":!".
7606 */
7607 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007608ex_bang(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007609{
7610 do_bang(eap->addr_count, eap, eap->forceit, TRUE, TRUE);
7611}
7612
7613/*
7614 * ":undo".
7615 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007616 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +01007617ex_undo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007618{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007619 if (eap->addr_count == 1) // :undo 123
Bram Moolenaar730cde92010-06-27 05:18:54 +02007620 undo_time(eap->line2, FALSE, FALSE, TRUE);
Bram Moolenaard3667a22006-03-16 21:35:52 +00007621 else
7622 u_undo(1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007623}
7624
Bram Moolenaar55debbe2010-05-23 23:34:36 +02007625#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar6df6f472010-07-18 18:04:50 +02007626 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007627ex_wundo(exarg_T *eap)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02007628{
7629 char_u hash[UNDO_HASH_SIZE];
7630
7631 u_compute_hash(hash);
7632 u_write_undo(eap->arg, eap->forceit, curbuf, hash);
7633}
7634
Bram Moolenaar6df6f472010-07-18 18:04:50 +02007635 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007636ex_rundo(exarg_T *eap)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02007637{
7638 char_u hash[UNDO_HASH_SIZE];
7639
7640 u_compute_hash(hash);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02007641 u_read_undo(eap->arg, hash, NULL);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02007642}
7643#endif
7644
Bram Moolenaar071d4272004-06-13 20:20:40 +00007645/*
7646 * ":redo".
7647 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007648 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007649ex_redo(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007650{
7651 u_redo(1);
7652}
7653
7654/*
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007655 * ":earlier" and ":later".
7656 */
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007657 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007658ex_later(exarg_T *eap)
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007659{
7660 long count = 0;
7661 int sec = FALSE;
Bram Moolenaar730cde92010-06-27 05:18:54 +02007662 int file = FALSE;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007663 char_u *p = eap->arg;
7664
7665 if (*p == NUL)
7666 count = 1;
7667 else if (isdigit(*p))
7668 {
7669 count = getdigits(&p);
7670 switch (*p)
7671 {
7672 case 's': ++p; sec = TRUE; break;
7673 case 'm': ++p; sec = TRUE; count *= 60; break;
7674 case 'h': ++p; sec = TRUE; count *= 60 * 60; break;
Bram Moolenaar730cde92010-06-27 05:18:54 +02007675 case 'd': ++p; sec = TRUE; count *= 24 * 60 * 60; break;
7676 case 'f': ++p; file = TRUE; break;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007677 }
7678 }
7679
7680 if (*p != NUL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007681 semsg(_(e_invarg2), eap->arg);
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007682 else
Bram Moolenaar730cde92010-06-27 05:18:54 +02007683 undo_time(eap->cmdidx == CMD_earlier ? -count : count,
7684 sec, file, FALSE);
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007685}
7686
7687/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007688 * ":redir": start/stop redirection.
7689 */
7690 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007691ex_redir(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007692{
7693 char *mode;
7694 char_u *fname;
Bram Moolenaarca472992005-01-21 11:46:23 +00007695 char_u *arg = eap->arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007696
Bram Moolenaarba768492016-07-08 15:32:54 +02007697#ifdef FEAT_EVAL
Bram Moolenaar79815f12016-07-09 17:07:29 +02007698 if (redir_execute)
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02007699 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007700 emsg(_("E930: Cannot use :redir inside execute()"));
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02007701 return;
7702 }
Bram Moolenaarba768492016-07-08 15:32:54 +02007703#endif
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02007704
Bram Moolenaar071d4272004-06-13 20:20:40 +00007705 if (STRICMP(eap->arg, "END") == 0)
7706 close_redir();
7707 else
7708 {
Bram Moolenaarca472992005-01-21 11:46:23 +00007709 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00007710 {
Bram Moolenaarca472992005-01-21 11:46:23 +00007711 ++arg;
7712 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00007713 {
Bram Moolenaarca472992005-01-21 11:46:23 +00007714 ++arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007715 mode = "a";
7716 }
7717 else
7718 mode = "w";
Bram Moolenaarca472992005-01-21 11:46:23 +00007719 arg = skipwhite(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007720
7721 close_redir();
7722
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007723 // Expand environment variables and "~/".
Bram Moolenaarca472992005-01-21 11:46:23 +00007724 fname = expand_env_save(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007725 if (fname == NULL)
7726 return;
7727#ifdef FEAT_BROWSE
Bram Moolenaare1004402020-10-24 20:49:43 +02007728 if (cmdmod.cmod_flags & CMOD_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007729 {
7730 char_u *browseFile;
7731
Bram Moolenaar7171abe2004-10-11 10:06:20 +00007732 browseFile = do_browse(BROWSE_SAVE,
7733 (char_u *)_("Save Redirection"),
Bram Moolenaarc36651b2018-04-29 12:22:56 +02007734 fname, NULL, NULL,
7735 (char_u *)_(BROWSE_FILTER_ALL_FILES), curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007736 if (browseFile == NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007737 return; // operation cancelled
Bram Moolenaar071d4272004-06-13 20:20:40 +00007738 vim_free(fname);
7739 fname = browseFile;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007740 eap->forceit = TRUE; // since dialog already asked
Bram Moolenaar071d4272004-06-13 20:20:40 +00007741 }
7742#endif
7743
7744 redir_fd = open_exfile(fname, eap->forceit, mode);
7745 vim_free(fname);
7746 }
7747#ifdef FEAT_EVAL
Bram Moolenaarca472992005-01-21 11:46:23 +00007748 else if (*arg == '@')
Bram Moolenaar071d4272004-06-13 20:20:40 +00007749 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007750 // redirect to a register a-z (resp. A-Z for appending)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007751 close_redir();
Bram Moolenaarca472992005-01-21 11:46:23 +00007752 ++arg;
7753 if (ASCII_ISALPHA(*arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007754# ifdef FEAT_CLIPBOARD
Bram Moolenaarca472992005-01-21 11:46:23 +00007755 || *arg == '*'
Bram Moolenaar9a51c6e2006-11-14 19:25:02 +00007756 || *arg == '+'
Bram Moolenaar071d4272004-06-13 20:20:40 +00007757# endif
Bram Moolenaarca472992005-01-21 11:46:23 +00007758 || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00007759 {
Bram Moolenaarca472992005-01-21 11:46:23 +00007760 redir_reg = *arg++;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007761 if (*arg == '>' && arg[1] == '>') // append
Bram Moolenaard9d30582005-05-18 22:10:28 +00007762 arg += 2;
Bram Moolenaarc188b882007-10-19 14:20:54 +00007763 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007764 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007765 // Can use both "@a" and "@a>".
Bram Moolenaar2c29bee2005-06-01 21:46:07 +00007766 if (*arg == '>')
7767 arg++;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007768 // Make register empty when not using @A-@Z and the
7769 // command is valid.
Bram Moolenaarc188b882007-10-19 14:20:54 +00007770 if (*arg == NUL && !isupper(redir_reg))
7771 write_reg_contents(redir_reg, (char_u *)"", -1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007772 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00007773 }
7774 if (*arg != NUL)
7775 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00007776 redir_reg = 0;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007777 semsg(_(e_invarg2), eap->arg);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007778 }
7779 }
7780 else if (*arg == '=' && arg[1] == '>')
7781 {
7782 int append;
7783
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007784 // redirect to a variable
Bram Moolenaardf177f62005-02-22 08:39:57 +00007785 close_redir();
7786 arg += 2;
7787
7788 if (*arg == '>')
7789 {
7790 ++arg;
7791 append = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007792 }
7793 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00007794 append = FALSE;
7795
7796 if (var_redir_start(skipwhite(arg), append) == OK)
7797 redir_vname = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007798 }
7799#endif
7800
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007801 // TODO: redirect to a buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00007802
Bram Moolenaar071d4272004-06-13 20:20:40 +00007803 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007804 semsg(_(e_invarg2), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007805 }
Bram Moolenaar29b2d262006-09-10 19:07:28 +00007806
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007807 // Make sure redirection is not off. Can happen for cmdline completion
7808 // that indirectly invokes a command to catch its output.
Bram Moolenaar29b2d262006-09-10 19:07:28 +00007809 if (redir_fd != NULL
7810#ifdef FEAT_EVAL
7811 || redir_reg || redir_vname
7812#endif
7813 )
7814 redir_off = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007815}
7816
7817/*
7818 * ":redraw": force redraw
7819 */
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01007820 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007821ex_redraw(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007822{
7823 int r = RedrawingDisabled;
7824 int p = p_lz;
7825
7826 RedrawingDisabled = 0;
7827 p_lz = FALSE;
Bram Moolenaarabc39ab2017-03-01 18:04:05 +01007828 validate_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007829 update_topline();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007830 update_screen(eap->forceit ? CLEAR : VIsual_active ? INVERTED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007831#ifdef FEAT_TITLE
7832 if (need_maketitle)
7833 maketitle();
7834#endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007835#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
7836# ifdef VIMDLL
7837 if (!gui.in_use)
7838# endif
7839 resize_console_buf();
Bram Moolenaar78d21da2019-02-17 15:00:52 +01007840#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007841 RedrawingDisabled = r;
7842 p_lz = p;
7843
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007844 // Reset msg_didout, so that a message that's there is overwritten.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007845 msg_didout = FALSE;
7846 msg_col = 0;
7847
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007848 // No need to wait after an intentional redraw.
Bram Moolenaar56667a52013-07-17 11:54:28 +02007849 need_wait_return = FALSE;
7850
Bram Moolenaar071d4272004-06-13 20:20:40 +00007851 out_flush();
7852}
7853
7854/*
7855 * ":redrawstatus": force redraw of status line(s)
7856 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007857 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007858ex_redrawstatus(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007859{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007860 int r = RedrawingDisabled;
7861 int p = p_lz;
7862
7863 RedrawingDisabled = 0;
7864 p_lz = FALSE;
7865 if (eap->forceit)
7866 status_redraw_all();
7867 else
7868 status_redraw_curbuf();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007869 update_screen(VIsual_active ? INVERTED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007870 RedrawingDisabled = r;
7871 p_lz = p;
7872 out_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007873}
7874
Bram Moolenaare12bab32019-01-08 22:02:56 +01007875/*
7876 * ":redrawtabline": force redraw of the tabline
7877 */
7878 static void
7879ex_redrawtabline(exarg_T *eap UNUSED)
7880{
7881 int r = RedrawingDisabled;
7882 int p = p_lz;
7883
7884 RedrawingDisabled = 0;
7885 p_lz = FALSE;
7886
7887 draw_tabline();
7888
7889 RedrawingDisabled = r;
7890 p_lz = p;
7891 out_flush();
7892}
7893
Bram Moolenaar071d4272004-06-13 20:20:40 +00007894 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007895close_redir(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007896{
7897 if (redir_fd != NULL)
7898 {
7899 fclose(redir_fd);
7900 redir_fd = NULL;
7901 }
7902#ifdef FEAT_EVAL
7903 redir_reg = 0;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007904 if (redir_vname)
7905 {
7906 var_redir_stop();
7907 redir_vname = 0;
7908 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007909#endif
7910}
7911
Bram Moolenaarf96ae0b2019-07-28 15:21:55 +02007912#if (defined(FEAT_SESSION) || defined(FEAT_EVAL)) || defined(PROTO)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007913 int
Bram Moolenaarbd67aac2019-09-21 23:09:04 +02007914vim_mkdir_emsg(char_u *name, int prot UNUSED)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007915{
7916 if (vim_mkdir(name, prot) != 0)
7917 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007918 semsg(_("E739: Cannot create directory: %s"), name);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007919 return FAIL;
7920 }
7921 return OK;
7922}
7923#endif
7924
Bram Moolenaar071d4272004-06-13 20:20:40 +00007925/*
7926 * Open a file for writing for an Ex command, with some checks.
7927 * Return file descriptor, or NULL on failure.
7928 */
7929 FILE *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007930open_exfile(
7931 char_u *fname,
7932 int forceit,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007933 char *mode) // "w" for create new file or "a" for append
Bram Moolenaar071d4272004-06-13 20:20:40 +00007934{
7935 FILE *fd;
7936
7937#ifdef UNIX
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007938 // with Unix it is possible to open a directory
Bram Moolenaar071d4272004-06-13 20:20:40 +00007939 if (mch_isdir(fname))
7940 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007941 semsg(_(e_isadir2), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007942 return NULL;
7943 }
7944#endif
7945 if (!forceit && *mode != 'a' && vim_fexists(fname))
7946 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007947 semsg(_("E189: \"%s\" exists (add ! to override)"), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007948 return NULL;
7949 }
7950
7951 if ((fd = mch_fopen((char *)fname, mode)) == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007952 semsg(_("E190: Cannot open \"%s\" for writing"), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007953
7954 return fd;
7955}
7956
7957/*
7958 * ":mark" and ":k".
7959 */
7960 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007961ex_mark(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007962{
7963 pos_T pos;
7964
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007965 if (*eap->arg == NUL) // No argument?
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007966 emsg(_(e_argreq));
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007967 else if (eap->arg[1] != NUL) // more than one character?
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +02007968 semsg(_(e_trailing_arg), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007969 else
7970 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007971 pos = curwin->w_cursor; // save curwin->w_cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00007972 curwin->w_cursor.lnum = eap->line2;
7973 beginline(BL_WHITE | BL_FIX);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007974 if (setmark(*eap->arg) == FAIL) // set mark
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007975 emsg(_("E191: Argument must be a letter or forward/backward quote"));
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007976 curwin->w_cursor = pos; // restore curwin->w_cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00007977 }
7978}
7979
7980/*
7981 * Update w_topline, w_leftcol and the cursor position.
7982 */
7983 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007984update_topline_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007985{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007986 check_cursor(); // put cursor on valid line
Bram Moolenaar071d4272004-06-13 20:20:40 +00007987 update_topline();
7988 if (!curwin->w_p_wrap)
7989 validate_cursor();
7990 update_curswant();
7991}
7992
Bram Moolenaar071d4272004-06-13 20:20:40 +00007993/*
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007994 * Save the current State and go to Normal mode.
7995 * Return TRUE if the typeahead could be saved.
7996 */
7997 int
7998save_current_state(save_state_T *sst)
7999{
8000 sst->save_msg_scroll = msg_scroll;
8001 sst->save_restart_edit = restart_edit;
8002 sst->save_msg_didout = msg_didout;
8003 sst->save_State = State;
8004 sst->save_insertmode = p_im;
8005 sst->save_finish_op = finish_op;
8006 sst->save_opcount = opcount;
Bram Moolenaarcce713d2019-03-04 11:40:12 +01008007 sst->save_reg_executing = reg_executing;
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008008
Bram Moolenaar4324d872020-12-01 20:12:24 +01008009 msg_scroll = FALSE; // no msg scrolling in Normal mode
8010 restart_edit = 0; // don't go to Insert mode
8011 p_im = FALSE; // don't use 'insertmode'
Bram Moolenaara452b802020-12-01 21:47:59 +01008012#ifdef FEAT_EVAL
8013 sst->save_script_version = current_sctx.sc_version;
Bram Moolenaar4324d872020-12-01 20:12:24 +01008014 current_sctx.sc_version = 1; // not in Vim9 script
Bram Moolenaara452b802020-12-01 21:47:59 +01008015#endif
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 Moolenaara452b802020-12-01 21:47:59 +01008039#ifdef FEAT_EVAL
Bram Moolenaar4324d872020-12-01 20:12:24 +01008040 current_sctx.sc_version = sst->save_script_version;
Bram Moolenaara452b802020-12-01 21:47:59 +01008041#endif
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008042
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008043 // Restore the state (needed when called from a function executed for
8044 // 'indentexpr'). Update the mouse and cursor, they may have changed.
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008045 State = sst->save_State;
8046#ifdef CURSOR_SHAPE
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008047 ui_cursor_shape(); // may show different cursor shape
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008048#endif
8049}
8050
8051/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008052 * ":normal[!] {commands}": Execute normal mode commands.
8053 */
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01008054 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008055ex_normal(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008056{
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008057 save_state_T save_state;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008058 char_u *arg = NULL;
8059 int l;
8060 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008061
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008062 if (ex_normal_lock > 0)
8063 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008064 emsg(_(e_secure));
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008065 return;
8066 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008067 if (ex_normal_busy >= p_mmd)
8068 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008069 emsg(_("E192: Recursive use of :normal too deep"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008070 return;
8071 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008072
Bram Moolenaar071d4272004-06-13 20:20:40 +00008073 /*
8074 * vgetc() expects a CSI and K_SPECIAL to have been escaped. Don't do
8075 * this for the K_SPECIAL leading byte, otherwise special keys will not
8076 * work.
8077 */
8078 if (has_mbyte)
8079 {
8080 int len = 0;
8081
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008082 // Count the number of characters to be escaped.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008083 for (p = eap->arg; *p != NUL; ++p)
8084 {
Bram Moolenaar13505972019-01-24 15:04:48 +01008085#ifdef FEAT_GUI
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008086 if (*p == CSI) // leadbyte CSI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008087 len += 2;
Bram Moolenaar13505972019-01-24 15:04:48 +01008088#endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008089 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008090 if (*++p == K_SPECIAL // trailbyte K_SPECIAL or CSI
Bram Moolenaar13505972019-01-24 15:04:48 +01008091#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008092 || *p == CSI
Bram Moolenaar13505972019-01-24 15:04:48 +01008093#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008094 )
8095 len += 2;
8096 }
8097 if (len > 0)
8098 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02008099 arg = alloc(STRLEN(eap->arg) + len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008100 if (arg != NULL)
8101 {
8102 len = 0;
8103 for (p = eap->arg; *p != NUL; ++p)
8104 {
8105 arg[len++] = *p;
Bram Moolenaar13505972019-01-24 15:04:48 +01008106#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008107 if (*p == CSI)
8108 {
8109 arg[len++] = KS_EXTRA;
8110 arg[len++] = (int)KE_CSI;
8111 }
Bram Moolenaar13505972019-01-24 15:04:48 +01008112#endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008113 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008114 {
8115 arg[len++] = *++p;
8116 if (*p == K_SPECIAL)
8117 {
8118 arg[len++] = KS_SPECIAL;
8119 arg[len++] = KE_FILLER;
8120 }
Bram Moolenaar13505972019-01-24 15:04:48 +01008121#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008122 else if (*p == CSI)
8123 {
8124 arg[len++] = KS_EXTRA;
8125 arg[len++] = (int)KE_CSI;
8126 }
Bram Moolenaar13505972019-01-24 15:04:48 +01008127#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008128 }
8129 arg[len] = NUL;
8130 }
8131 }
8132 }
8133 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008134
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008135 ++ex_normal_busy;
8136 if (save_current_state(&save_state))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008137 {
8138 /*
8139 * Repeat the :normal command for each line in the range. When no
8140 * range given, execute it just once, without positioning the cursor
8141 * first.
8142 */
8143 do
8144 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008145 if (eap->addr_count != 0)
8146 {
8147 curwin->w_cursor.lnum = eap->line1++;
8148 curwin->w_cursor.col = 0;
Bram Moolenaard5d37532017-03-27 23:02:07 +02008149 check_cursor_moved(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008150 }
8151
Bram Moolenaar13505972019-01-24 15:04:48 +01008152 exec_normal_cmd(arg != NULL
8153 ? arg
8154 : eap->arg, eap->forceit ? REMAP_NONE : REMAP_YES, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008155 }
8156 while (eap->addr_count > 0 && eap->line1 <= eap->line2 && !got_int);
8157 }
8158
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008159 // Might not return to the main loop when in an event handler.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008160 update_topline_cursor();
8161
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008162 restore_current_state(&save_state);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008163 --ex_normal_busy;
Bram Moolenaareda73602014-11-05 09:53:23 +01008164 setmouse();
Bram Moolenaareda73602014-11-05 09:53:23 +01008165#ifdef CURSOR_SHAPE
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008166 ui_cursor_shape(); // may show different cursor shape
Bram Moolenaareda73602014-11-05 09:53:23 +01008167#endif
8168
Bram Moolenaar071d4272004-06-13 20:20:40 +00008169 vim_free(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008170}
8171
8172/*
Bram Moolenaar64969662005-12-14 21:59:55 +00008173 * ":startinsert", ":startreplace" and ":startgreplace"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008174 */
8175 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008176ex_startinsert(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008177{
Bram Moolenaarfd371682005-01-14 21:42:54 +00008178 if (eap->forceit)
8179 {
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02008180 // cursor line can be zero on startup
Bram Moolenaar09ca9322017-09-26 17:40:45 +02008181 if (!curwin->w_cursor.lnum)
8182 curwin->w_cursor.lnum = 1;
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02008183 set_cursor_for_append_to_line();
Bram Moolenaarfd371682005-01-14 21:42:54 +00008184 }
Bram Moolenaareb58a242020-04-19 18:13:19 +02008185#ifdef FEAT_TERMINAL
8186 // Ignore this when running in an active terminal.
8187 if (term_job_running(curbuf->b_term))
8188 return;
8189#endif
Bram Moolenaarfd371682005-01-14 21:42:54 +00008190
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02008191 // Ignore the command when already in Insert mode. Inserting an
8192 // expression register that invokes a function can do this.
Bram Moolenaara40c5002005-01-09 21:16:21 +00008193 if (State & INSERT)
8194 return;
8195
Bram Moolenaar64969662005-12-14 21:59:55 +00008196 if (eap->cmdidx == CMD_startinsert)
8197 restart_edit = 'a';
8198 else if (eap->cmdidx == CMD_startreplace)
8199 restart_edit = 'R';
Bram Moolenaar071d4272004-06-13 20:20:40 +00008200 else
Bram Moolenaar64969662005-12-14 21:59:55 +00008201 restart_edit = 'V';
8202
8203 if (!eap->forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008204 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00008205 if (eap->cmdidx == CMD_startinsert)
8206 restart_edit = 'i';
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02008207 curwin->w_curswant = 0; // avoid MAXCOL
Bram Moolenaar071d4272004-06-13 20:20:40 +00008208 }
Bram Moolenaar957cf672020-11-12 14:21:06 +01008209
8210 if (VIsual_active)
8211 showmode();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008212}
8213
8214/*
8215 * ":stopinsert"
8216 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008217 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008218ex_stopinsert(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008219{
8220 restart_edit = 0;
8221 stop_insert_mode = TRUE;
Bram Moolenaarfd773e92016-04-02 19:39:16 +02008222 clearmode();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008223}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008224
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008225/*
8226 * Execute normal mode command "cmd".
8227 * "remap" can be REMAP_NONE or REMAP_YES.
8228 */
8229 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008230exec_normal_cmd(char_u *cmd, int remap, int silent)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008231{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008232 // Stuff the argument into the typeahead buffer.
Bram Moolenaar25281632016-01-21 23:32:32 +01008233 ins_typebuf(cmd, remap, 0, TRUE, silent);
Bram Moolenaar586c70c2018-10-02 16:23:58 +02008234 exec_normal(FALSE, FALSE, FALSE);
Bram Moolenaar25281632016-01-21 23:32:32 +01008235}
Bram Moolenaar25281632016-01-21 23:32:32 +01008236
Bram Moolenaar25281632016-01-21 23:32:32 +01008237/*
8238 * Execute normal_cmd() until there is no typeahead left.
Bram Moolenaar586c70c2018-10-02 16:23:58 +02008239 * When "use_vpeekc" is TRUE use vpeekc() to check for available chars.
Bram Moolenaar25281632016-01-21 23:32:32 +01008240 */
8241 void
Bram Moolenaar586c70c2018-10-02 16:23:58 +02008242exec_normal(int was_typed, int use_vpeekc, int may_use_terminal_loop UNUSED)
Bram Moolenaar25281632016-01-21 23:32:32 +01008243{
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008244 oparg_T oa;
Bram Moolenaar905dd902019-04-07 14:21:47 +02008245 int c;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008246
Bram Moolenaar905dd902019-04-07 14:21:47 +02008247 // When calling vpeekc() from feedkeys() it will return Ctrl_C when there
8248 // is nothing to get, so also check for Ctrl_C.
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008249 clear_oparg(&oa);
8250 finish_op = FALSE;
Bram Moolenaar586c70c2018-10-02 16:23:58 +02008251 while ((!stuff_empty()
8252 || ((was_typed || !typebuf_typed()) && typebuf.tb_len > 0)
Bram Moolenaar905dd902019-04-07 14:21:47 +02008253 || (use_vpeekc && (c = vpeekc()) != NUL && c != Ctrl_C))
Bram Moolenaar586c70c2018-10-02 16:23:58 +02008254 && !got_int)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008255 {
8256 update_topline_cursor();
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02008257#ifdef FEAT_TERMINAL
Bram Moolenaar655a82a2018-05-08 22:01:07 +02008258 if (may_use_terminal_loop && term_use_loop()
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02008259 && oa.op_type == OP_NOP && oa.regname == NUL
8260 && !VIsual_active)
8261 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008262 // If terminal_loop() returns OK we got a key that is handled
8263 // in Normal model. With FAIL we first need to position the
8264 // cursor and the screen needs to be redrawn.
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02008265 if (terminal_loop(TRUE) == OK)
8266 normal_cmd(&oa, TRUE);
8267 }
8268 else
8269#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008270 // execute a Normal mode cmd
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02008271 normal_cmd(&oa, TRUE);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008272 }
8273}
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008274
Bram Moolenaar071d4272004-06-13 20:20:40 +00008275#ifdef FEAT_FIND_ID
8276 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008277ex_checkpath(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008278{
8279 find_pattern_in_path(NULL, 0, 0, FALSE, FALSE, CHECK_PATH, 1L,
8280 eap->forceit ? ACTION_SHOW_ALL : ACTION_SHOW,
8281 (linenr_T)1, (linenr_T)MAXLNUM);
8282}
8283
Bram Moolenaar4033c552017-09-16 20:54:51 +02008284#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008285/*
8286 * ":psearch"
8287 */
8288 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008289ex_psearch(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008290{
8291 g_do_tagpreview = p_pvh;
8292 ex_findpat(eap);
8293 g_do_tagpreview = 0;
8294}
8295#endif
8296
8297 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008298ex_findpat(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008299{
8300 int whole = TRUE;
8301 long n;
8302 char_u *p;
8303 int action;
8304
8305 switch (cmdnames[eap->cmdidx].cmd_name[2])
8306 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008307 case 'e': // ":psearch", ":isearch" and ":dsearch"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008308 if (cmdnames[eap->cmdidx].cmd_name[0] == 'p')
8309 action = ACTION_GOTO;
8310 else
8311 action = ACTION_SHOW;
8312 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008313 case 'i': // ":ilist" and ":dlist"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008314 action = ACTION_SHOW_ALL;
8315 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008316 case 'u': // ":ijump" and ":djump"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008317 action = ACTION_GOTO;
8318 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008319 default: // ":isplit" and ":dsplit"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008320 action = ACTION_SPLIT;
8321 break;
8322 }
8323
8324 n = 1;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008325 if (vim_isdigit(*eap->arg)) // get count
Bram Moolenaar071d4272004-06-13 20:20:40 +00008326 {
8327 n = getdigits(&eap->arg);
8328 eap->arg = skipwhite(eap->arg);
8329 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008330 if (*eap->arg == '/') // Match regexp, not just whole words
Bram Moolenaar071d4272004-06-13 20:20:40 +00008331 {
8332 whole = FALSE;
8333 ++eap->arg;
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02008334 p = skip_regexp(eap->arg, '/', p_magic);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008335 if (*p)
8336 {
8337 *p++ = NUL;
8338 p = skipwhite(p);
8339
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008340 // Check for trailing illegal characters
Bram Moolenaarfaac4102020-04-20 17:46:14 +02008341 if (!ends_excmd2(eap->arg, p))
Bram Moolenaar8930caa2020-07-23 16:37:03 +02008342 eap->errmsg = ex_errmsg(e_trailing_arg, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008343 else
8344 eap->nextcmd = check_nextcmd(p);
8345 }
8346 }
8347 if (!eap->skip)
8348 find_pattern_in_path(eap->arg, 0, (int)STRLEN(eap->arg),
8349 whole, !eap->forceit,
8350 *eap->cmd == 'd' ? FIND_DEFINE : FIND_ANY,
8351 n, action, eap->line1, eap->line2);
8352}
8353#endif
8354
Bram Moolenaar071d4272004-06-13 20:20:40 +00008355
Bram Moolenaar4033c552017-09-16 20:54:51 +02008356#ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00008357/*
8358 * ":ptag", ":ptselect", ":ptjump", ":ptnext", etc.
8359 */
8360 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008361ex_ptag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008362{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008363 g_do_tagpreview = p_pvh; // will be reset to 0 in ex_tag_cmd()
Bram Moolenaar071d4272004-06-13 20:20:40 +00008364 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
8365}
8366
8367/*
8368 * ":pedit"
8369 */
8370 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008371ex_pedit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008372{
8373 win_T *curwin_save = curwin;
8374
Bram Moolenaar3a2505c2020-03-09 16:40:41 +01008375 if (ERROR_IF_ANY_POPUP_WINDOW)
8376 return;
8377
Bram Moolenaar026587b2019-08-17 15:08:00 +02008378 // Open the preview window or popup and make it the current window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008379 g_do_tagpreview = p_pvh;
Bram Moolenaar576a4a62019-08-18 15:25:17 +02008380 prepare_tagpreview(TRUE, TRUE, FALSE);
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02008381
Bram Moolenaar026587b2019-08-17 15:08:00 +02008382 // Edit the file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008383 do_exedit(eap, NULL);
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02008384
Bram Moolenaar071d4272004-06-13 20:20:40 +00008385 if (curwin != curwin_save && win_valid(curwin_save))
8386 {
Bram Moolenaar026587b2019-08-17 15:08:00 +02008387 // Return cursor to where we were
Bram Moolenaar071d4272004-06-13 20:20:40 +00008388 validate_cursor();
8389 redraw_later(VALID);
8390 win_enter(curwin_save, TRUE);
8391 }
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01008392# ifdef FEAT_PROP_POPUP
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02008393 else if (WIN_IS_POPUP(curwin))
8394 {
8395 // can't keep focus in popup window
8396 win_enter(firstwin, TRUE);
8397 }
8398# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008399 g_do_tagpreview = 0;
8400}
Bram Moolenaar4033c552017-09-16 20:54:51 +02008401#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008402
8403/*
8404 * ":stag", ":stselect" and ":stjump".
8405 */
8406 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008407ex_stag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008408{
8409 postponed_split = -1;
Bram Moolenaare1004402020-10-24 20:49:43 +02008410 postponed_split_flags = cmdmod.cmod_split;
8411 postponed_split_tab = cmdmod.cmod_tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008412 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
8413 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00008414 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008415}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008416
8417/*
8418 * ":tag", ":tselect", ":tjump", ":tnext", etc.
8419 */
8420 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008421ex_tag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008422{
8423 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name);
8424}
8425
8426 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008427ex_tag_cmd(exarg_T *eap, char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008428{
8429 int cmd;
8430
8431 switch (name[1])
8432 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008433 case 'j': cmd = DT_JUMP; // ":tjump"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008434 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008435 case 's': cmd = DT_SELECT; // ":tselect"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008436 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008437 case 'p': cmd = DT_PREV; // ":tprevious"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008438 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008439 case 'N': cmd = DT_PREV; // ":tNext"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008440 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008441 case 'n': cmd = DT_NEXT; // ":tnext"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008442 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008443 case 'o': cmd = DT_POP; // ":pop"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008444 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008445 case 'f': // ":tfirst"
8446 case 'r': cmd = DT_FIRST; // ":trewind"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008447 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008448 case 'l': cmd = DT_LAST; // ":tlast"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008449 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008450 default: // ":tag"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008451#ifdef FEAT_CSCOPE
Bram Moolenaar7c94c262008-06-20 09:11:34 +00008452 if (p_cst && *eap->arg != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008453 {
Bram Moolenaard4db7712016-11-12 19:16:46 +01008454 ex_cstag(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008455 return;
8456 }
8457#endif
8458 cmd = DT_TAG;
8459 break;
8460 }
8461
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00008462 if (name[0] == 'l')
8463 {
8464#ifndef FEAT_QUICKFIX
8465 ex_ni(eap);
8466 return;
8467#else
8468 cmd = DT_LTAG;
8469#endif
8470 }
8471
Bram Moolenaar071d4272004-06-13 20:20:40 +00008472 do_tag(eap->arg, cmd, eap->addr_count > 0 ? (int)eap->line2 : 1,
8473 eap->forceit, TRUE);
8474}
8475
8476/*
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008477 * Check "str" for starting with a special cmdline variable.
8478 * If found return one of the SPEC_ values and set "*usedlen" to the length of
8479 * the variable. Otherwise return -1 and "*usedlen" is unchanged.
8480 */
8481 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008482find_cmdline_var(char_u *src, int *usedlen)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008483{
8484 int len;
8485 int i;
Bram Moolenaar8f0b2d42009-05-16 14:41:10 +00008486 static char *(spec_str[]) = {
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008487 "%",
8488#define SPEC_PERC 0
8489 "#",
Bram Moolenaar65f08472017-09-10 18:16:20 +02008490#define SPEC_HASH (SPEC_PERC + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008491 "<cword>", // cursor word
Bram Moolenaar65f08472017-09-10 18:16:20 +02008492#define SPEC_CWORD (SPEC_HASH + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008493 "<cWORD>", // cursor WORD
Bram Moolenaar65f08472017-09-10 18:16:20 +02008494#define SPEC_CCWORD (SPEC_CWORD + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008495 "<cexpr>", // expr under cursor
Bram Moolenaar65f08472017-09-10 18:16:20 +02008496#define SPEC_CEXPR (SPEC_CCWORD + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008497 "<cfile>", // cursor path name
Bram Moolenaar65f08472017-09-10 18:16:20 +02008498#define SPEC_CFILE (SPEC_CEXPR + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008499 "<sfile>", // ":so" file name
Bram Moolenaar65f08472017-09-10 18:16:20 +02008500#define SPEC_SFILE (SPEC_CFILE + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008501 "<slnum>", // ":so" file line number
Bram Moolenaar65f08472017-09-10 18:16:20 +02008502#define SPEC_SLNUM (SPEC_SFILE + 1)
Bram Moolenaara5d04232020-07-26 15:37:02 +02008503 "<stack>", // call stack
8504#define SPEC_STACK (SPEC_SLNUM + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008505 "<afile>", // autocommand file name
Bram Moolenaara5d04232020-07-26 15:37:02 +02008506#define SPEC_AFILE (SPEC_STACK + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008507 "<abuf>", // autocommand buffer number
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008508#define SPEC_ABUF (SPEC_AFILE + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008509 "<amatch>", // autocommand match name
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01008510#define SPEC_AMATCH (SPEC_ABUF + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008511 "<sflnum>", // script file line number
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008512#define SPEC_SFLNUM (SPEC_AMATCH + 1)
Bram Moolenaar90944302020-08-01 20:45:11 +02008513 "<SID>", // script ID: <SNR>123_
8514#define SPEC_SID (SPEC_SFLNUM + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008515#ifdef FEAT_CLIENTSERVER
8516 "<client>"
Bram Moolenaar90944302020-08-01 20:45:11 +02008517# define SPEC_CLIENT (SPEC_SID + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008518#endif
8519 };
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008520
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00008521 for (i = 0; i < (int)(sizeof(spec_str) / sizeof(char *)); ++i)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008522 {
8523 len = (int)STRLEN(spec_str[i]);
8524 if (STRNCMP(src, spec_str[i], len) == 0)
8525 {
8526 *usedlen = len;
8527 return i;
8528 }
8529 }
8530 return -1;
8531}
8532
8533/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008534 * Evaluate cmdline variables.
8535 *
8536 * change '%' to curbuf->b_ffname
Bram Moolenaar59d8e562020-11-07 18:41:10 +01008537 * '#' to curwin->w_alt_fnum
Bram Moolenaar071d4272004-06-13 20:20:40 +00008538 * '<cword>' to word under the cursor
8539 * '<cWORD>' to WORD under the cursor
Bram Moolenaar8071cb22019-07-12 17:58:01 +02008540 * '<cexpr>' to C-expression under the cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00008541 * '<cfile>' to path name under the cursor
8542 * '<sfile>' to sourced file name
Bram Moolenaar4f25b1a2020-09-10 19:25:05 +02008543 * '<stack>' to call stack
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01008544 * '<slnum>' to sourced file line number
Bram Moolenaar071d4272004-06-13 20:20:40 +00008545 * '<afile>' to file name for autocommand
8546 * '<abuf>' to buffer number for autocommand
8547 * '<amatch>' to matching name for autocommand
8548 *
8549 * When an error is detected, "errormsg" is set to a non-NULL pointer (may be
8550 * "" for error without a message) and NULL is returned.
8551 * Returns an allocated string if a valid match was found.
8552 * Returns NULL if no match was found. "usedlen" then still contains the
8553 * number of characters to skip.
8554 */
8555 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008556eval_vars(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008557 char_u *src, // pointer into commandline
8558 char_u *srcstart, // beginning of valid memory for src
8559 int *usedlen, // characters after src that are used
8560 linenr_T *lnump, // line number for :e command, or NULL
8561 char **errormsg, // pointer to error message
8562 int *escaped) // return value has escaped white space (can
8563 // be NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008564{
8565 int i;
8566 char_u *s;
8567 char_u *result;
8568 char_u *resultbuf = NULL;
8569 int resultlen;
8570 buf_T *buf;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008571 int valid = VALID_HEAD + VALID_PATH; // assume valid result
Bram Moolenaar071d4272004-06-13 20:20:40 +00008572 int spec_idx;
Bram Moolenaarfd249462018-07-28 16:14:30 +02008573 int tilde_file = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008574 int skip_mod = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008575 char_u strbuf[30];
Bram Moolenaar071d4272004-06-13 20:20:40 +00008576
8577 *errormsg = NULL;
Bram Moolenaar63b92542007-03-27 14:57:09 +00008578 if (escaped != NULL)
8579 *escaped = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008580
8581 /*
8582 * Check if there is something to do.
8583 */
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008584 spec_idx = find_cmdline_var(src, usedlen);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008585 if (spec_idx < 0) // no match
Bram Moolenaar071d4272004-06-13 20:20:40 +00008586 {
8587 *usedlen = 1;
8588 return NULL;
8589 }
8590
8591 /*
8592 * Skip when preceded with a backslash "\%" and "\#".
8593 * Note: In "\\%" the % is also not recognized!
8594 */
8595 if (src > srcstart && src[-1] == '\\')
8596 {
8597 *usedlen = 0;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008598 STRMOVE(src - 1, src); // remove backslash
Bram Moolenaar071d4272004-06-13 20:20:40 +00008599 return NULL;
8600 }
8601
8602 /*
8603 * word or WORD under cursor
8604 */
Bram Moolenaar65f08472017-09-10 18:16:20 +02008605 if (spec_idx == SPEC_CWORD || spec_idx == SPEC_CCWORD
8606 || spec_idx == SPEC_CEXPR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008607 {
Bram Moolenaar65f08472017-09-10 18:16:20 +02008608 resultlen = find_ident_under_cursor(&result,
8609 spec_idx == SPEC_CWORD ? (FIND_IDENT | FIND_STRING)
8610 : spec_idx == SPEC_CEXPR ? (FIND_IDENT | FIND_STRING | FIND_EVAL)
8611 : FIND_STRING);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008612 if (resultlen == 0)
8613 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008614 *errormsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +00008615 return NULL;
8616 }
8617 }
8618
8619 /*
8620 * '#': Alternate file name
8621 * '%': Current file name
8622 * File name under the cursor
8623 * File name for autocommand
8624 * and following modifiers
8625 */
8626 else
8627 {
8628 switch (spec_idx)
8629 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008630 case SPEC_PERC: // '%': current file
Bram Moolenaar071d4272004-06-13 20:20:40 +00008631 if (curbuf->b_fname == NULL)
8632 {
8633 result = (char_u *)"";
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008634 valid = 0; // Must have ":p:h" to be valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00008635 }
8636 else
Bram Moolenaar00136dc2018-07-25 21:19:13 +02008637 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008638 result = curbuf->b_fname;
Bram Moolenaar00136dc2018-07-25 21:19:13 +02008639 tilde_file = STRCMP(result, "~") == 0;
8640 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008641 break;
8642
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008643 case SPEC_HASH: // '#' or "#99": alternate file
8644 if (src[1] == '#') // "##": the argument list
Bram Moolenaar071d4272004-06-13 20:20:40 +00008645 {
8646 result = arg_all();
8647 resultbuf = result;
8648 *usedlen = 2;
Bram Moolenaar63b92542007-03-27 14:57:09 +00008649 if (escaped != NULL)
8650 *escaped = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008651 skip_mod = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008652 break;
8653 }
8654 s = src + 1;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008655 if (*s == '<') // "#<99" uses v:oldfiles
Bram Moolenaard812df62008-11-09 12:46:09 +00008656 ++s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008657 i = (int)getdigits(&s);
Bram Moolenaarc312b8b2017-10-28 17:53:04 +02008658 if (s == src + 2 && src[1] == '-')
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008659 // just a minus sign, don't skip over it
Bram Moolenaarc312b8b2017-10-28 17:53:04 +02008660 s--;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008661 *usedlen = (int)(s - src); // length of what we expand
Bram Moolenaar071d4272004-06-13 20:20:40 +00008662
Bram Moolenaarc312b8b2017-10-28 17:53:04 +02008663 if (src[1] == '<' && i != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008664 {
Bram Moolenaard812df62008-11-09 12:46:09 +00008665 if (*usedlen < 2)
8666 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008667 // Should we give an error message for #<text?
Bram Moolenaard812df62008-11-09 12:46:09 +00008668 *usedlen = 1;
8669 return NULL;
8670 }
8671#ifdef FEAT_EVAL
8672 result = list_find_str(get_vim_var_list(VV_OLDFILES),
8673 (long)i);
8674 if (result == NULL)
8675 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008676 *errormsg = "";
Bram Moolenaard812df62008-11-09 12:46:09 +00008677 return NULL;
8678 }
8679#else
Bram Moolenaar8e481e82019-01-15 20:07:48 +01008680 *errormsg = _("E809: #< is not available without the +eval feature");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008681 return NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +00008682#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008683 }
8684 else
Bram Moolenaard812df62008-11-09 12:46:09 +00008685 {
Bram Moolenaarc312b8b2017-10-28 17:53:04 +02008686 if (i == 0 && src[1] == '<' && *usedlen > 1)
8687 *usedlen = 1;
Bram Moolenaard812df62008-11-09 12:46:09 +00008688 buf = buflist_findnr(i);
8689 if (buf == NULL)
8690 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008691 *errormsg = _("E194: No alternate file name to substitute for '#'");
Bram Moolenaard812df62008-11-09 12:46:09 +00008692 return NULL;
8693 }
8694 if (lnump != NULL)
8695 *lnump = ECMD_LAST;
8696 if (buf->b_fname == NULL)
8697 {
8698 result = (char_u *)"";
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008699 valid = 0; // Must have ":p:h" to be valid
Bram Moolenaard812df62008-11-09 12:46:09 +00008700 }
8701 else
Bram Moolenaar00136dc2018-07-25 21:19:13 +02008702 {
Bram Moolenaard812df62008-11-09 12:46:09 +00008703 result = buf->b_fname;
Bram Moolenaar00136dc2018-07-25 21:19:13 +02008704 tilde_file = STRCMP(result, "~") == 0;
8705 }
Bram Moolenaard812df62008-11-09 12:46:09 +00008706 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008707 break;
8708
8709#ifdef FEAT_SEARCHPATH
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008710 case SPEC_CFILE: // file name under cursor
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008711 result = file_name_at_cursor(FNAME_MESS|FNAME_HYP, 1L, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008712 if (result == NULL)
8713 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008714 *errormsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +00008715 return NULL;
8716 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008717 resultbuf = result; // remember allocated string
Bram Moolenaar071d4272004-06-13 20:20:40 +00008718 break;
8719#endif
8720
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008721 case SPEC_AFILE: // file name for autocommand
Bram Moolenaar071d4272004-06-13 20:20:40 +00008722 result = autocmd_fname;
Bram Moolenaarf6dad432008-09-18 19:29:58 +00008723 if (result != NULL && !autocmd_fname_full)
8724 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008725 // Still need to turn the fname into a full path. It is
8726 // postponed to avoid a delay when <afile> is not used.
Bram Moolenaarf6dad432008-09-18 19:29:58 +00008727 autocmd_fname_full = TRUE;
8728 result = FullName_save(autocmd_fname, FALSE);
8729 vim_free(autocmd_fname);
8730 autocmd_fname = result;
8731 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008732 if (result == NULL)
8733 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008734 *errormsg = _("E495: no autocommand file name to substitute for \"<afile>\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008735 return NULL;
8736 }
Bram Moolenaara0174af2008-01-02 20:08:25 +00008737 result = shorten_fname1(result);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008738 break;
8739
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008740 case SPEC_ABUF: // buffer number for autocommand
Bram Moolenaar071d4272004-06-13 20:20:40 +00008741 if (autocmd_bufnr <= 0)
8742 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008743 *errormsg = _("E496: no autocommand buffer number to substitute for \"<abuf>\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008744 return NULL;
8745 }
8746 sprintf((char *)strbuf, "%d", autocmd_bufnr);
8747 result = strbuf;
8748 break;
8749
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008750 case SPEC_AMATCH: // match name for autocommand
Bram Moolenaar071d4272004-06-13 20:20:40 +00008751 result = autocmd_match;
8752 if (result == NULL)
8753 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008754 *errormsg = _("E497: no autocommand match name to substitute for \"<amatch>\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008755 return NULL;
8756 }
8757 break;
8758
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008759 case SPEC_SFILE: // file name for ":so" command
Bram Moolenaara5d04232020-07-26 15:37:02 +02008760 case SPEC_STACK: // call stack
Bram Moolenaar4f25b1a2020-09-10 19:25:05 +02008761 result = estack_sfile(spec_idx == SPEC_SFILE
8762 ? ESTACK_SFILE : ESTACK_STACK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008763 if (result == NULL)
8764 {
Bram Moolenaara5d04232020-07-26 15:37:02 +02008765 *errormsg = spec_idx == SPEC_SFILE
8766 ? _("E498: no :source file name to substitute for \"<sfile>\"")
8767 : _("E489: no call stack to substitute for \"<stack>\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008768 return NULL;
8769 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01008770 resultbuf = result; // remember allocated string
Bram Moolenaar071d4272004-06-13 20:20:40 +00008771 break;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008772
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008773 case SPEC_SLNUM: // line in file for ":so" command
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01008774 if (SOURCING_NAME == NULL || SOURCING_LNUM == 0)
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01008775 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008776 *errormsg = _("E842: no line number to use for \"<slnum>\"");
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01008777 return NULL;
8778 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01008779 sprintf((char *)strbuf, "%ld", SOURCING_LNUM);
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01008780 result = strbuf;
8781 break;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008782
8783#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008784 case SPEC_SFLNUM: // line in script file
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01008785 if (current_sctx.sc_lnum + SOURCING_LNUM == 0)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008786 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008787 *errormsg = _("E961: no line number to use for \"<sflnum>\"");
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008788 return NULL;
8789 }
8790 sprintf((char *)strbuf, "%ld",
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01008791 (long)(current_sctx.sc_lnum + SOURCING_LNUM));
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008792 result = strbuf;
8793 break;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008794
Bram Moolenaar90944302020-08-01 20:45:11 +02008795 case SPEC_SID:
8796 if (current_sctx.sc_sid <= 0)
8797 {
8798 *errormsg = _(e_usingsid);
8799 return NULL;
8800 }
8801 sprintf((char *)strbuf, "<SNR>%d_", current_sctx.sc_sid);
8802 result = strbuf;
8803 break;
Bram Moolenaare4218b92020-08-01 21:11:38 +02008804#endif
Bram Moolenaar90944302020-08-01 20:45:11 +02008805
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008806#ifdef FEAT_CLIENTSERVER
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008807 case SPEC_CLIENT: // Source of last submitted input
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008808 sprintf((char *)strbuf, PRINTF_HEX_LONG_U,
8809 (long_u)clientWindow);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008810 result = strbuf;
8811 break;
8812#endif
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008813
Bram Moolenaar9e0f6ec2017-05-16 09:36:54 +02008814 default:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008815 result = (char_u *)""; // avoid gcc warning
Bram Moolenaar9e0f6ec2017-05-16 09:36:54 +02008816 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008817 }
8818
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008819 resultlen = (int)STRLEN(result); // length of new string
8820 if (src[*usedlen] == '<') // remove the file name extension
Bram Moolenaar071d4272004-06-13 20:20:40 +00008821 {
8822 ++*usedlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008823 if ((s = vim_strrchr(result, '.')) != NULL && s >= gettail(result))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008824 resultlen = (int)(s - result);
8825 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008826 else if (!skip_mod)
8827 {
Bram Moolenaar00136dc2018-07-25 21:19:13 +02008828 valid |= modify_fname(src, tilde_file, usedlen, &result, &resultbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008829 &resultlen);
8830 if (result == NULL)
8831 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008832 *errormsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +00008833 return NULL;
8834 }
8835 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008836 }
8837
8838 if (resultlen == 0 || valid != VALID_HEAD + VALID_PATH)
8839 {
8840 if (valid != VALID_HEAD + VALID_PATH)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008841 // xgettext:no-c-format
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008842 *errormsg = _("E499: Empty file name for '%' or '#', only works with \":p:h\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008843 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008844 *errormsg = _("E500: Evaluates to an empty string");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008845 result = NULL;
8846 }
8847 else
8848 result = vim_strnsave(result, resultlen);
8849 vim_free(resultbuf);
8850 return result;
8851}
8852
8853/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008854 * Expand the <sfile> string in "arg".
8855 *
8856 * Returns an allocated string, or NULL for any error.
8857 */
8858 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008859expand_sfile(char_u *arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008860{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008861 char *errormsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008862 int len;
8863 char_u *result;
8864 char_u *newres;
8865 char_u *repl;
8866 int srclen;
8867 char_u *p;
8868
8869 result = vim_strsave(arg);
8870 if (result == NULL)
8871 return NULL;
8872
8873 for (p = result; *p; )
8874 {
8875 if (STRNCMP(p, "<sfile>", 7) != 0)
8876 ++p;
8877 else
8878 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008879 // replace "<sfile>" with the sourced file name, and do ":" stuff
Bram Moolenaar63b92542007-03-27 14:57:09 +00008880 repl = eval_vars(p, result, &srclen, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008881 if (errormsg != NULL)
8882 {
8883 if (*errormsg)
8884 emsg(errormsg);
8885 vim_free(result);
8886 return NULL;
8887 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008888 if (repl == NULL) // no match (cannot happen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008889 {
8890 p += srclen;
8891 continue;
8892 }
8893 len = (int)STRLEN(result) - srclen + (int)STRLEN(repl) + 1;
8894 newres = alloc(len);
8895 if (newres == NULL)
8896 {
8897 vim_free(repl);
8898 vim_free(result);
8899 return NULL;
8900 }
8901 mch_memmove(newres, result, (size_t)(p - result));
8902 STRCPY(newres + (p - result), repl);
8903 len = (int)STRLEN(newres);
8904 STRCAT(newres, p + srclen);
8905 vim_free(repl);
8906 vim_free(result);
8907 result = newres;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008908 p = newres + len; // continue after the match
Bram Moolenaar071d4272004-06-13 20:20:40 +00008909 }
8910 }
8911
8912 return result;
8913}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008914
Bram Moolenaar071d4272004-06-13 20:20:40 +00008915#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG) || defined(PROTO)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00008916/*
Bram Moolenaard9462e32011-04-11 21:35:11 +02008917 * Make a dialog message in "buff[DIALOG_MSG_SIZE]".
Bram Moolenaarb765d632005-06-07 21:00:02 +00008918 * "format" must contain "%s".
Bram Moolenaar9c13b352005-05-19 20:53:52 +00008919 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008920 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008921dialog_msg(char_u *buff, char *format, char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008922{
Bram Moolenaar071d4272004-06-13 20:20:40 +00008923 if (fname == NULL)
8924 fname = (char_u *)_("Untitled");
Bram Moolenaard9462e32011-04-11 21:35:11 +02008925 vim_snprintf((char *)buff, DIALOG_MSG_SIZE, format, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008926}
8927#endif
8928
8929/*
8930 * ":behave {mswin,xterm}"
8931 */
8932 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008933ex_behave(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008934{
8935 if (STRCMP(eap->arg, "mswin") == 0)
8936 {
8937 set_option_value((char_u *)"selection", 0L, (char_u *)"exclusive", 0);
8938 set_option_value((char_u *)"selectmode", 0L, (char_u *)"mouse,key", 0);
8939 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"popup", 0);
8940 set_option_value((char_u *)"keymodel", 0L,
8941 (char_u *)"startsel,stopsel", 0);
8942 }
8943 else if (STRCMP(eap->arg, "xterm") == 0)
8944 {
8945 set_option_value((char_u *)"selection", 0L, (char_u *)"inclusive", 0);
8946 set_option_value((char_u *)"selectmode", 0L, (char_u *)"", 0);
8947 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"extend", 0);
8948 set_option_value((char_u *)"keymodel", 0L, (char_u *)"", 0);
8949 }
8950 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008951 semsg(_(e_invarg2), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008952}
8953
Bram Moolenaar071d4272004-06-13 20:20:40 +00008954static int filetype_detect = FALSE;
8955static int filetype_plugin = FALSE;
8956static int filetype_indent = FALSE;
8957
8958/*
8959 * ":filetype [plugin] [indent] {on,off,detect}"
8960 * on: Load the filetype.vim file to install autocommands for file types.
8961 * off: Load the ftoff.vim file to remove all autocommands for file types.
8962 * plugin on: load filetype.vim and ftplugin.vim
8963 * plugin off: load ftplugof.vim
8964 * indent on: load filetype.vim and indent.vim
8965 * indent off: load indoff.vim
8966 */
8967 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008968ex_filetype(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008969{
8970 char_u *arg = eap->arg;
8971 int plugin = FALSE;
8972 int indent = FALSE;
8973
8974 if (*eap->arg == NUL)
8975 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008976 // Print current status.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008977 smsg("filetype detection:%s plugin:%s indent:%s",
Bram Moolenaar071d4272004-06-13 20:20:40 +00008978 filetype_detect ? "ON" : "OFF",
8979 filetype_plugin ? (filetype_detect ? "ON" : "(on)") : "OFF",
8980 filetype_indent ? (filetype_detect ? "ON" : "(on)") : "OFF");
8981 return;
8982 }
8983
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008984 // Accept "plugin" and "indent" in any order.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008985 for (;;)
8986 {
8987 if (STRNCMP(arg, "plugin", 6) == 0)
8988 {
8989 plugin = TRUE;
8990 arg = skipwhite(arg + 6);
8991 continue;
8992 }
8993 if (STRNCMP(arg, "indent", 6) == 0)
8994 {
8995 indent = TRUE;
8996 arg = skipwhite(arg + 6);
8997 continue;
8998 }
8999 break;
9000 }
9001 if (STRCMP(arg, "on") == 0 || STRCMP(arg, "detect") == 0)
9002 {
9003 if (*arg == 'o' || !filetype_detect)
9004 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01009005 source_runtime((char_u *)FILETYPE_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009006 filetype_detect = TRUE;
9007 if (plugin)
9008 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01009009 source_runtime((char_u *)FTPLUGIN_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009010 filetype_plugin = TRUE;
9011 }
9012 if (indent)
9013 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01009014 source_runtime((char_u *)INDENT_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009015 filetype_indent = TRUE;
9016 }
9017 }
9018 if (*arg == 'd')
9019 {
Bram Moolenaar1610d052016-06-09 22:53:01 +02009020 (void)do_doautocmd((char_u *)"filetypedetect BufRead", TRUE, NULL);
Bram Moolenaara3227e22006-03-08 21:32:40 +00009021 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009022 }
9023 }
9024 else if (STRCMP(arg, "off") == 0)
9025 {
9026 if (plugin || indent)
9027 {
9028 if (plugin)
9029 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01009030 source_runtime((char_u *)FTPLUGOF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009031 filetype_plugin = FALSE;
9032 }
9033 if (indent)
9034 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01009035 source_runtime((char_u *)INDOFF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009036 filetype_indent = FALSE;
9037 }
9038 }
9039 else
9040 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01009041 source_runtime((char_u *)FTOFF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009042 filetype_detect = FALSE;
9043 }
9044 }
9045 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009046 semsg(_(e_invarg2), arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009047}
9048
9049/*
Bram Moolenaar3e545692017-06-04 19:00:32 +02009050 * ":setfiletype [FALLBACK] {name}"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009051 */
9052 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009053ex_setfiletype(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009054{
9055 if (!did_filetype)
Bram Moolenaar3e545692017-06-04 19:00:32 +02009056 {
9057 char_u *arg = eap->arg;
9058
9059 if (STRNCMP(arg, "FALLBACK ", 9) == 0)
9060 arg += 9;
9061
9062 set_option_value((char_u *)"filetype", 0L, arg, OPT_LOCAL);
9063 if (arg != eap->arg)
9064 did_filetype = FALSE;
9065 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009066}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009067
Bram Moolenaar071d4272004-06-13 20:20:40 +00009068 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009069ex_digraphs(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009070{
9071#ifdef FEAT_DIGRAPHS
9072 if (*eap->arg != NUL)
9073 putdigraph(eap->arg);
9074 else
Bram Moolenaareae8ae12018-12-14 18:53:02 +01009075 listdigraphs(eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009076#else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009077 emsg(_("E196: No digraphs in this version"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009078#endif
9079}
9080
Bram Moolenaar451fc7b2018-04-27 22:53:07 +02009081#if defined(FEAT_SEARCH_EXTRA) || defined(PROTO)
9082 void
9083set_no_hlsearch(int flag)
9084{
9085 no_hlsearch = flag;
9086# ifdef FEAT_EVAL
9087 set_vim_var_nr(VV_HLSEARCH, !no_hlsearch && p_hls);
9088# endif
9089}
9090
Bram Moolenaar071d4272004-06-13 20:20:40 +00009091/*
9092 * ":nohlsearch"
9093 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009094 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009095ex_nohlsearch(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009096{
Bram Moolenaar451fc7b2018-04-27 22:53:07 +02009097 set_no_hlsearch(TRUE);
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00009098 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009099}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009100#endif
9101
9102#ifdef FEAT_CRYPT
9103/*
9104 * ":X": Get crypt key
9105 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009106 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009107ex_X(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009108{
Bram Moolenaar3a0c9082014-11-12 15:15:42 +01009109 crypt_check_current_method();
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02009110 (void)crypt_get_key(TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009111}
9112#endif
9113
9114#ifdef FEAT_FOLDING
9115 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009116ex_fold(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009117{
9118 if (foldManualAllowed(TRUE))
9119 foldCreate(eap->line1, eap->line2);
9120}
9121
9122 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009123ex_foldopen(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009124{
9125 opFoldRange(eap->line1, eap->line2, eap->cmdidx == CMD_foldopen,
9126 eap->forceit, FALSE);
9127}
9128
9129 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009130ex_folddo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009131{
9132 linenr_T lnum;
9133
Bram Moolenaar4facea32019-10-12 20:17:40 +02009134# ifdef FEAT_CLIPBOARD
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02009135 start_global_changes();
Bram Moolenaar4facea32019-10-12 20:17:40 +02009136# endif
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02009137
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009138 // First set the marks for all lines closed/open.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009139 for (lnum = eap->line1; lnum <= eap->line2; ++lnum)
9140 if (hasFolding(lnum, NULL, NULL) == (eap->cmdidx == CMD_folddoclosed))
9141 ml_setmarked(lnum);
9142
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009143 // Execute the command on the marked lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009144 global_exe(eap->arg);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009145 ml_clearmarked(); // clear rest of the marks
Bram Moolenaar4facea32019-10-12 20:17:40 +02009146# ifdef FEAT_CLIPBOARD
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02009147 end_global_changes();
Bram Moolenaar4facea32019-10-12 20:17:40 +02009148# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009149}
9150#endif
Bram Moolenaarf5291f32017-09-14 22:55:37 +02009151
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009152#if defined(FEAT_QUICKFIX) || defined(PROTO)
Bram Moolenaar39665952018-08-15 20:59:48 +02009153/*
9154 * Returns TRUE if the supplied Ex cmdidx is for a location list command
9155 * instead of a quickfix command.
9156 */
9157 int
9158is_loclist_cmd(int cmdidx)
9159{
Bram Moolenaar74c8be22018-08-23 22:51:40 +02009160 if (cmdidx < 0 || cmdidx >= CMD_SIZE)
Bram Moolenaar39665952018-08-15 20:59:48 +02009161 return FALSE;
9162 return cmdnames[cmdidx].cmd_name[0] == 'l';
9163}
9164#endif
9165
Bram Moolenaar4facea32019-10-12 20:17:40 +02009166#if defined(FEAT_TIMERS) || defined(PROTO)
Bram Moolenaarf5291f32017-09-14 22:55:37 +02009167 int
9168get_pressedreturn(void)
9169{
9170 return ex_pressedreturn;
9171}
9172
9173 void
9174set_pressedreturn(int val)
9175{
9176 ex_pressedreturn = val;
9177}
9178#endif