blob: 44e113d53b79aab250e405a10591cf007973afd8 [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/*
598 * do_cmdline(): execute one Ex command line
599 *
600 * 1. Execute "cmdline" when it is not NULL.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100601 * If "cmdline" is NULL, or more lines are needed, fgetline() is used.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000602 * 2. Split up in parts separated with '|'.
603 *
604 * This function can be called recursively!
605 *
606 * flags:
607 * DOCMD_VERBOSE - The command will be included in the error message.
608 * DOCMD_NOWAIT - Don't call wait_return() and friends.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100609 * DOCMD_REPEAT - Repeat execution until fgetline() returns NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000610 * DOCMD_KEYTYPED - Don't reset KeyTyped.
611 * DOCMD_EXCRESET - Reset the exception environment (used for debugging).
612 * DOCMD_KEEPLINE - Store first typed line (for repeating with ".").
613 *
614 * return FAIL if cmdline could not be executed, OK otherwise
615 */
616 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100617do_cmdline(
618 char_u *cmdline,
Bram Moolenaar66250c92020-08-20 15:02:42 +0200619 char_u *(*fgetline)(int, void *, int, getline_opt_T),
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100620 void *cookie, // argument for fgetline()
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100621 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000622{
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100623 char_u *next_cmdline; // next cmd to execute
624 char_u *cmdline_copy = NULL; // copy of cmd line
625 int used_getline = FALSE; // used "fgetline" to obtain command
626 static int recursive = 0; // recursive depth
Bram Moolenaar071d4272004-06-13 20:20:40 +0000627 int msg_didout_before_start = 0;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100628 int count = 0; // line number count
629 int did_inc = FALSE; // incremented RedrawingDisabled
Bram Moolenaar071d4272004-06-13 20:20:40 +0000630 int retval = OK;
631#ifdef FEAT_EVAL
Bram Moolenaarddef1292019-12-16 17:10:33 +0100632 cstack_T cstack; // conditional stack
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100633 garray_T lines_ga; // keep lines for ":while"/":for"
634 int current_line = 0; // active line in lines_ga
Bram Moolenaard5053d02020-06-28 15:51:16 +0200635 int current_line_before = 0;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100636 char_u *fname = NULL; // function or script name
637 linenr_T *breakpoint = NULL; // ptr to breakpoint field in cookie
638 int *dbg_tick = NULL; // ptr to dbg_tick field in cookie
639 struct dbg_stuff debug_saved; // saved things for debug mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000640 int initial_trylevel;
Bram Moolenaar25e0f582020-05-25 22:36:50 +0200641 msglist_T **saved_msg_list = NULL;
Bram Moolenaar683581e2020-10-22 21:22:58 +0200642 msglist_T *private_msg_list = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000643
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100644 // "fgetline" and "cookie" passed to do_one_cmd()
Bram Moolenaar66250c92020-08-20 15:02:42 +0200645 char_u *(*cmd_getline)(int, void *, int, getline_opt_T);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000646 void *cmd_cookie;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000647 struct loop_cookie cmd_loop_cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000648 void *real_cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +0000649 int getline_is_func;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000650#else
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100651# define cmd_getline fgetline
Bram Moolenaar071d4272004-06-13 20:20:40 +0000652# define cmd_cookie cookie
653#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100654 static int call_depth = 0; // recursiveness
Bram Moolenaar2196bce2020-04-12 20:01:11 +0200655#ifdef FEAT_EVAL
Bram Moolenaare31ee862020-01-07 20:59:34 +0100656 ESTACK_CHECK_DECLARATION
Bram Moolenaar071d4272004-06-13 20:20:40 +0000657
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100658 // For every pair of do_cmdline()/do_one_cmd() calls, use an extra memory
659 // location for storing error messages to be converted to an exception.
660 // This ensures that the do_errthrow() call in do_one_cmd() does not
661 // combine the messages stored by an earlier invocation of do_one_cmd()
662 // with the command name of the later one. This would happen when
663 // BufWritePost autocommands are executed after a write error.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000664 saved_msg_list = msg_list;
665 msg_list = &private_msg_list;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000666#endif
667
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100668 // It's possible to create an endless loop with ":execute", catch that
669 // here. The value of 200 allows nested function calls, ":source", etc.
670 // Allow 200 or 'maxfuncdepth', whatever is larger.
Bram Moolenaarb094ff42017-01-02 16:16:39 +0100671 if (call_depth >= 200
672#ifdef FEAT_EVAL
673 && call_depth >= p_mfd
674#endif
675 )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000676 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100677 emsg(_("E169: Command too recursive"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000678#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100679 // When converting to an exception, we do not include the command name
680 // since this is not an error of the specific command.
Bram Moolenaarddef1292019-12-16 17:10:33 +0100681 do_errthrow((cstack_T *)NULL, (char_u *)NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000682 msg_list = saved_msg_list;
683#endif
684 return FAIL;
685 }
686 ++call_depth;
687
688#ifdef FEAT_EVAL
689 cstack.cs_idx = -1;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000690 cstack.cs_looplevel = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000691 cstack.cs_trylevel = 0;
692 cstack.cs_emsg_silent_list = NULL;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000693 cstack.cs_lflags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000694 ga_init2(&lines_ga, (int)sizeof(wcmd_T), 10);
695
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100696 real_cookie = getline_cookie(fgetline, cookie);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000697
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100698 // Inside a function use a higher nesting level.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100699 getline_is_func = getline_equal(fgetline, cookie, get_func_line);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000700 if (getline_is_func && ex_nesting_level == func_level(real_cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000701 ++ex_nesting_level;
702
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100703 // Get the function or script name and the address where the next breakpoint
704 // line and the debug tick for a function or script are stored.
Bram Moolenaar05159a02005-02-26 23:04:13 +0000705 if (getline_is_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000706 {
707 fname = func_name(real_cookie);
708 breakpoint = func_breakpoint(real_cookie);
709 dbg_tick = func_dbg_tick(real_cookie);
710 }
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100711 else if (getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000712 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100713 fname = SOURCING_NAME;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000714 breakpoint = source_breakpoint(real_cookie);
715 dbg_tick = source_dbg_tick(real_cookie);
716 }
717
718 /*
719 * Initialize "force_abort" and "suppress_errthrow" at the top level.
720 */
721 if (!recursive)
722 {
723 force_abort = FALSE;
724 suppress_errthrow = FALSE;
725 }
726
727 /*
728 * If requested, store and reset the global values controlling the
Bram Moolenaar89d40322006-08-29 15:30:07 +0000729 * exception handling (used when debugging). Otherwise clear it to avoid
730 * a bogus compiler warning when the optimizer uses inline functions...
Bram Moolenaar071d4272004-06-13 20:20:40 +0000731 */
Bram Moolenaarb4872942006-05-13 10:32:52 +0000732 if (flags & DOCMD_EXCRESET)
Bram Moolenaared203462004-06-16 11:19:22 +0000733 save_dbg_stuff(&debug_saved);
Bram Moolenaar89d40322006-08-29 15:30:07 +0000734 else
Bram Moolenaara80faa82020-04-12 19:37:17 +0200735 CLEAR_FIELD(debug_saved);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000736
737 initial_trylevel = trylevel;
738
739 /*
740 * "did_throw" will be set to TRUE when an exception is being thrown.
741 */
742 did_throw = FALSE;
743#endif
744 /*
745 * "did_emsg" will be set to TRUE when emsg() is used, in which case we
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000746 * cancel the whole command line, and any if/endif or loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000747 * If force_abort is set, we cancel everything.
748 */
Bram Moolenaareeece9e2020-11-20 19:26:48 +0100749#ifdef FEAT_EVAL
750 did_emsg_cumul += did_emsg;
751#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000752 did_emsg = FALSE;
753
754 /*
755 * KeyTyped is only set when calling vgetc(). Reset it here when not
756 * calling vgetc() (sourced command lines).
757 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100758 if (!(flags & DOCMD_KEYTYPED)
759 && !getline_equal(fgetline, cookie, getexline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000760 KeyTyped = FALSE;
761
762 /*
763 * Continue executing command lines:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000764 * - when inside an ":if", ":while" or ":for"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000765 * - for multiple commands on one line, separated with '|'
766 * - when repeating until there are no more lines (for ":source")
767 */
768 next_cmdline = cmdline;
769 do
770 {
Bram Moolenaar05159a02005-02-26 23:04:13 +0000771#ifdef FEAT_EVAL
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100772 getline_is_func = getline_equal(fgetline, cookie, get_func_line);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000773#endif
774
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100775 // stop skipping cmds for an error msg after all endif/while/for
Bram Moolenaar071d4272004-06-13 20:20:40 +0000776 if (next_cmdline == NULL
777#ifdef FEAT_EVAL
778 && !force_abort
779 && cstack.cs_idx < 0
Bram Moolenaar05159a02005-02-26 23:04:13 +0000780 && !(getline_is_func && func_has_abort(real_cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000781#endif
782 )
Bram Moolenaareeece9e2020-11-20 19:26:48 +0100783 {
784#ifdef FEAT_EVAL
785 did_emsg_cumul += did_emsg;
786#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000787 did_emsg = FALSE;
Bram Moolenaareeece9e2020-11-20 19:26:48 +0100788 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000789
790 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000791 * 1. If repeating a line in a loop, get a line from lines_ga.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100792 * 2. If no line given: Get an allocated line with fgetline().
Bram Moolenaar071d4272004-06-13 20:20:40 +0000793 * 3. If a line is given: Make a copy, so we can mess with it.
794 */
795
796#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100797 // 1. If repeating, get a previous line from lines_ga.
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000798 if (cstack.cs_looplevel > 0 && current_line < lines_ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000799 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100800 // Each '|' separated command is stored separately in lines_ga, to
801 // be able to jump to it. Don't use next_cmdline now.
Bram Moolenaard23a8232018-02-10 18:45:26 +0100802 VIM_CLEAR(cmdline_copy);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000803
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100804 // Check if a function has returned or, unless it has an unclosed
805 // try conditional, aborted.
Bram Moolenaar05159a02005-02-26 23:04:13 +0000806 if (getline_is_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000807 {
Bram Moolenaar05159a02005-02-26 23:04:13 +0000808# ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000809 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +0000810 func_line_end(real_cookie);
811# endif
812 if (func_has_ended(real_cookie))
813 {
814 retval = FAIL;
815 break;
816 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000817 }
Bram Moolenaar05159a02005-02-26 23:04:13 +0000818#ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000819 else if (do_profiling == PROF_YES
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100820 && getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar05159a02005-02-26 23:04:13 +0000821 script_line_end();
822#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000823
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100824 // Check if a sourced file hit a ":finish" command.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100825 if (source_finished(fgetline, cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000826 {
827 retval = FAIL;
828 break;
829 }
830
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100831 // If breakpoints have been added/deleted need to check for it.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000832 if (breakpoint != NULL && dbg_tick != NULL
833 && *dbg_tick != debug_tick)
834 {
835 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100836 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100837 fname, SOURCING_LNUM);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000838 *dbg_tick = debug_tick;
839 }
840
841 next_cmdline = ((wcmd_T *)(lines_ga.ga_data))[current_line].line;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100842 SOURCING_LNUM = ((wcmd_T *)(lines_ga.ga_data))[current_line].lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000843
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100844 // Did we encounter a breakpoint?
Bram Moolenaar071d4272004-06-13 20:20:40 +0000845 if (breakpoint != NULL && *breakpoint != 0
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100846 && *breakpoint <= SOURCING_LNUM)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000847 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100848 dbg_breakpoint(fname, SOURCING_LNUM);
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100849 // Find next breakpoint.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000850 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100851 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100852 fname, SOURCING_LNUM);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000853 *dbg_tick = debug_tick;
854 }
Bram Moolenaar05159a02005-02-26 23:04:13 +0000855# ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000856 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +0000857 {
858 if (getline_is_func)
859 func_line_start(real_cookie);
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100860 else if (getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar05159a02005-02-26 23:04:13 +0000861 script_line_start();
862 }
863# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000864 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000865#endif
866
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100867 // 2. If no line given, get an allocated line with fgetline().
Bram Moolenaar071d4272004-06-13 20:20:40 +0000868 if (next_cmdline == NULL)
869 {
870 /*
871 * Need to set msg_didout for the first line after an ":if",
872 * otherwise the ":if" will be overwritten.
873 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100874 if (count == 1 && getline_equal(fgetline, cookie, getexline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000875 msg_didout = TRUE;
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100876 if (fgetline == NULL || (next_cmdline = fgetline(':', cookie,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000877#ifdef FEAT_EVAL
878 cstack.cs_idx < 0 ? 0 : (cstack.cs_idx + 1) * 2
879#else
880 0
881#endif
Bram Moolenaare96a2492019-06-25 04:12:16 +0200882 , TRUE)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000883 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100884 // Don't call wait_return for aborted command line. The NULL
885 // returned for the end of a sourced file or executed function
886 // doesn't do this.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000887 if (KeyTyped && !(flags & DOCMD_REPEAT))
888 need_wait_return = FALSE;
889 retval = FAIL;
890 break;
891 }
892 used_getline = TRUE;
893
894 /*
895 * Keep the first typed line. Clear it when more lines are typed.
896 */
897 if (flags & DOCMD_KEEPLINE)
898 {
899 vim_free(repeat_cmdline);
900 if (count == 0)
901 repeat_cmdline = vim_strsave(next_cmdline);
902 else
903 repeat_cmdline = NULL;
904 }
905 }
906
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100907 // 3. Make a copy of the command so we can mess with it.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000908 else if (cmdline_copy == NULL)
909 {
910 next_cmdline = vim_strsave(next_cmdline);
911 if (next_cmdline == NULL)
912 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100913 emsg(_(e_outofmem));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000914 retval = FAIL;
915 break;
916 }
917 }
918 cmdline_copy = next_cmdline;
919
920#ifdef FEAT_EVAL
921 /*
Bram Moolenaard5053d02020-06-28 15:51:16 +0200922 * Inside a while/for loop, and when the command looks like a ":while"
923 * or ":for", the line is stored, because we may need it later when
924 * looping.
925 *
926 * When there is a '|' and another command, it is stored separately,
927 * because we need to be able to jump back to it from an
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000928 * :endwhile/:endfor.
Bram Moolenaard5053d02020-06-28 15:51:16 +0200929 *
930 * Pass a different "fgetline" function to do_one_cmd() below,
931 * that it stores lines in or reads them from "lines_ga". Makes it
932 * possible to define a function inside a while/for loop and handles
933 * line continuation.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000934 */
Bram Moolenaard5053d02020-06-28 15:51:16 +0200935 if ((cstack.cs_looplevel > 0 || has_loop_cmd(next_cmdline)))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000936 {
Bram Moolenaard5053d02020-06-28 15:51:16 +0200937 cmd_getline = get_loop_line;
938 cmd_cookie = (void *)&cmd_loop_cookie;
939 cmd_loop_cookie.lines_gap = &lines_ga;
940 cmd_loop_cookie.current_line = current_line;
941 cmd_loop_cookie.getline = fgetline;
942 cmd_loop_cookie.cookie = cookie;
943 cmd_loop_cookie.repeating = (current_line < lines_ga.ga_len);
944
945 // Save the current line when encountering it the first time.
946 if (current_line == lines_ga.ga_len
947 && store_loop_line(&lines_ga, next_cmdline) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000948 {
949 retval = FAIL;
950 break;
951 }
Bram Moolenaard5053d02020-06-28 15:51:16 +0200952 current_line_before = current_line;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000953 }
Bram Moolenaard5053d02020-06-28 15:51:16 +0200954 else
955 {
956 cmd_getline = fgetline;
957 cmd_cookie = cookie;
958 }
959
Bram Moolenaar071d4272004-06-13 20:20:40 +0000960 did_endif = FALSE;
961#endif
962
963 if (count++ == 0)
964 {
965 /*
966 * All output from the commands is put below each other, without
967 * waiting for a return. Don't do this when executing commands
968 * from a script or when being called recursive (e.g. for ":e
969 * +command file").
970 */
971 if (!(flags & DOCMD_NOWAIT) && !recursive)
972 {
973 msg_didout_before_start = msg_didout;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100974 msg_didany = FALSE; // no output yet
Bram Moolenaar071d4272004-06-13 20:20:40 +0000975 msg_start();
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100976 msg_scroll = TRUE; // put messages below each other
977 ++no_wait_return; // don't wait for return until finished
Bram Moolenaar071d4272004-06-13 20:20:40 +0000978 ++RedrawingDisabled;
979 did_inc = TRUE;
980 }
981 }
982
Bram Moolenaar823654b2020-05-29 23:03:09 +0200983 if ((p_verbose >= 15 && SOURCING_NAME != NULL) || p_verbose >= 16)
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100984 msg_verbose_cmd(SOURCING_LNUM, cmdline_copy);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000985
986 /*
987 * 2. Execute one '|' separated command.
988 * do_one_cmd() will return NULL if there is no trailing '|'.
989 * "cmdline_copy" can change, e.g. for '%' and '#' expansion.
990 */
991 ++recursive;
992 next_cmdline = do_one_cmd(&cmdline_copy, flags & DOCMD_VERBOSE,
993#ifdef FEAT_EVAL
994 &cstack,
995#endif
996 cmd_getline, cmd_cookie);
997 --recursive;
998
999#ifdef FEAT_EVAL
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001000 if (cmd_cookie == (void *)&cmd_loop_cookie)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001001 // Use "current_line" from "cmd_loop_cookie", it may have been
1002 // incremented when defining a function.
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001003 current_line = cmd_loop_cookie.current_line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001004#endif
1005
1006 if (next_cmdline == NULL)
1007 {
Bram Moolenaard23a8232018-02-10 18:45:26 +01001008 VIM_CLEAR(cmdline_copy);
Bram Moolenaard7663c22019-08-06 21:59:57 +02001009
Bram Moolenaar071d4272004-06-13 20:20:40 +00001010 /*
1011 * If the command was typed, remember it for the ':' register.
1012 * Do this AFTER executing the command to make :@: work.
1013 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001014 if (getline_equal(fgetline, cookie, getexline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001015 && new_last_cmdline != NULL)
1016 {
1017 vim_free(last_cmdline);
1018 last_cmdline = new_last_cmdline;
1019 new_last_cmdline = NULL;
1020 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001021 }
1022 else
1023 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001024 // need to copy the command after the '|' to cmdline_copy, for the
1025 // next do_one_cmd()
Bram Moolenaara7241f52008-06-24 20:39:31 +00001026 STRMOVE(cmdline_copy, next_cmdline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001027 next_cmdline = cmdline_copy;
1028 }
1029
1030
1031#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001032 // reset did_emsg for a function that is not aborted by an error
Bram Moolenaar071d4272004-06-13 20:20:40 +00001033 if (did_emsg && !force_abort
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001034 && getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001035 && !func_has_abort(real_cookie))
Bram Moolenaareeece9e2020-11-20 19:26:48 +01001036 {
1037 // did_emsg_cumul is not set here
Bram Moolenaar071d4272004-06-13 20:20:40 +00001038 did_emsg = FALSE;
Bram Moolenaareeece9e2020-11-20 19:26:48 +01001039 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001040
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001041 if (cstack.cs_looplevel > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001042 {
1043 ++current_line;
1044
1045 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001046 * An ":endwhile", ":endfor" and ":continue" is handled here.
1047 * If we were executing commands, jump back to the ":while" or
1048 * ":for".
1049 * If we were not executing commands, decrement cs_looplevel.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001050 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001051 if (cstack.cs_lflags & (CSL_HAD_CONT | CSL_HAD_ENDLOOP))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001052 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001053 cstack.cs_lflags &= ~(CSL_HAD_CONT | CSL_HAD_ENDLOOP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001054
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001055 // Jump back to the matching ":while" or ":for". Be careful
1056 // not to use a cs_line[] from an entry that isn't a ":while"
1057 // or ":for": It would make "current_line" invalid and can
1058 // cause a crash.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001059 if (!did_emsg && !got_int && !did_throw
1060 && cstack.cs_idx >= 0
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001061 && (cstack.cs_flags[cstack.cs_idx]
1062 & (CSF_WHILE | CSF_FOR))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001063 && cstack.cs_line[cstack.cs_idx] >= 0
1064 && (cstack.cs_flags[cstack.cs_idx] & CSF_ACTIVE))
1065 {
1066 current_line = cstack.cs_line[cstack.cs_idx];
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001067 // remember we jumped there
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001068 cstack.cs_lflags |= CSL_HAD_LOOP;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001069 line_breakcheck(); // check if CTRL-C typed
Bram Moolenaar071d4272004-06-13 20:20:40 +00001070
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001071 // Check for the next breakpoint at or after the ":while"
1072 // or ":for".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001073 if (breakpoint != NULL)
1074 {
1075 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001076 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001077 fname,
1078 ((wcmd_T *)lines_ga.ga_data)[current_line].lnum-1);
1079 *dbg_tick = debug_tick;
1080 }
1081 }
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001082 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001083 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001084 // can only get here with ":endwhile" or ":endfor"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001085 if (cstack.cs_idx >= 0)
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001086 rewind_conditionals(&cstack, cstack.cs_idx - 1,
1087 CSF_WHILE | CSF_FOR, &cstack.cs_looplevel);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001088 }
1089 }
1090
1091 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001092 * For a ":while" or ":for" we need to remember the line number.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001093 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001094 else if (cstack.cs_lflags & CSL_HAD_LOOP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001095 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001096 cstack.cs_lflags &= ~CSL_HAD_LOOP;
Bram Moolenaard5053d02020-06-28 15:51:16 +02001097 cstack.cs_line[cstack.cs_idx] = current_line_before;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001098 }
1099 }
1100
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001101 // Check for the next breakpoint after a watchexpression
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01001102 if (breakpoint != NULL && has_watchexpr())
1103 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001104 *breakpoint = dbg_find_breakpoint(FALSE, fname, SOURCING_LNUM);
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01001105 *dbg_tick = debug_tick;
1106 }
1107
Bram Moolenaar071d4272004-06-13 20:20:40 +00001108 /*
1109 * When not inside any ":while" loop, clear remembered lines.
1110 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001111 if (cstack.cs_looplevel == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001112 {
1113 if (lines_ga.ga_len > 0)
1114 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001115 SOURCING_LNUM =
Bram Moolenaar071d4272004-06-13 20:20:40 +00001116 ((wcmd_T *)lines_ga.ga_data)[lines_ga.ga_len - 1].lnum;
1117 free_cmdlines(&lines_ga);
1118 }
1119 current_line = 0;
1120 }
1121
1122 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001123 * A ":finally" makes did_emsg, got_int, and did_throw pending for
1124 * being restored at the ":endtry". Reset them here and set the
1125 * ACTIVE and FINALLY flags, so that the finally clause gets executed.
1126 * This includes the case where a missing ":endif", ":endwhile" or
1127 * ":endfor" was detected by the ":finally" itself.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001128 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001129 if (cstack.cs_lflags & CSL_HAD_FINA)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001130 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001131 cstack.cs_lflags &= ~CSL_HAD_FINA;
1132 report_make_pending(cstack.cs_pending[cstack.cs_idx]
1133 & (CSTP_ERROR | CSTP_INTERRUPT | CSTP_THROW),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001134 did_throw ? (void *)current_exception : NULL);
1135 did_emsg = got_int = did_throw = FALSE;
1136 cstack.cs_flags[cstack.cs_idx] |= CSF_ACTIVE | CSF_FINALLY;
1137 }
1138
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001139 // Update global "trylevel" for recursive calls to do_cmdline() from
1140 // within this loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001141 trylevel = initial_trylevel + cstack.cs_trylevel;
1142
1143 /*
Bram Moolenaarc1762cc2007-05-10 16:56:30 +00001144 * If the outermost try conditional (across function calls and sourced
Bram Moolenaar071d4272004-06-13 20:20:40 +00001145 * files) is aborted because of an error, an interrupt, or an uncaught
1146 * exception, cancel everything. If it is left normally, reset
1147 * force_abort to get the non-EH compatible abortion behavior for
1148 * the rest of the script.
1149 */
1150 if (trylevel == 0 && !did_emsg && !got_int && !did_throw)
1151 force_abort = FALSE;
1152
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001153 // Convert an interrupt to an exception if appropriate.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001154 (void)do_intthrow(&cstack);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001155#endif // FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001156
1157 }
1158 /*
1159 * Continue executing command lines when:
1160 * - no CTRL-C typed, no aborting error, no exception thrown or try
1161 * conditionals need to be checked for executing finally clauses or
1162 * catching an interrupt exception
1163 * - didn't get an error message or lines are not typed
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001164 * - there is a command after '|', inside a :if, :while, :for or :try, or
Bram Moolenaar071d4272004-06-13 20:20:40 +00001165 * looping for ":source" command or function call.
1166 */
1167 while (!((got_int
1168#ifdef FEAT_EVAL
1169 || (did_emsg && force_abort) || did_throw
1170#endif
1171 )
1172#ifdef FEAT_EVAL
1173 && cstack.cs_trylevel == 0
1174#endif
1175 )
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001176 && !(did_emsg
1177#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001178 // Keep going when inside try/catch, so that the error can be
1179 // deal with, except when it is a syntax error, it may cause
1180 // the :endtry to be missed.
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001181 && (cstack.cs_trylevel == 0 || did_emsg_syntax)
1182#endif
1183 && used_getline
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001184 && (getline_equal(fgetline, cookie, getexmodeline)
1185 || getline_equal(fgetline, cookie, getexline)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001186 && (next_cmdline != NULL
1187#ifdef FEAT_EVAL
1188 || cstack.cs_idx >= 0
1189#endif
1190 || (flags & DOCMD_REPEAT)));
1191
1192 vim_free(cmdline_copy);
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001193 did_emsg_syntax = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001194#ifdef FEAT_EVAL
1195 free_cmdlines(&lines_ga);
1196 ga_clear(&lines_ga);
1197
1198 if (cstack.cs_idx >= 0)
1199 {
1200 /*
1201 * If a sourced file or executed function ran to its end, report the
1202 * unclosed conditional.
1203 */
1204 if (!got_int && !did_throw
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001205 && ((getline_equal(fgetline, cookie, getsourceline)
1206 && !source_finished(fgetline, cookie))
1207 || (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001208 && !func_has_ended(real_cookie))))
1209 {
1210 if (cstack.cs_flags[cstack.cs_idx] & CSF_TRY)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001211 emsg(_(e_endtry));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001212 else if (cstack.cs_flags[cstack.cs_idx] & CSF_WHILE)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001213 emsg(_(e_endwhile));
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001214 else if (cstack.cs_flags[cstack.cs_idx] & CSF_FOR)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001215 emsg(_(e_endfor));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001216 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001217 emsg(_(e_endif));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001218 }
1219
1220 /*
1221 * Reset "trylevel" in case of a ":finish" or ":return" or a missing
1222 * ":endtry" in a sourced file or executed function. If the try
1223 * conditional is in its finally clause, ignore anything pending.
1224 * If it is in a catch clause, finish the caught exception.
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001225 * Also cleanup any "cs_forinfo" structures.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001226 */
1227 do
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001228 {
1229 int idx = cleanup_conditionals(&cstack, 0, TRUE);
1230
Bram Moolenaar89e5d682005-01-17 22:06:23 +00001231 if (idx >= 0)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001232 --idx; // remove try block not in its finally clause
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001233 rewind_conditionals(&cstack, idx, CSF_WHILE | CSF_FOR,
1234 &cstack.cs_looplevel);
1235 }
1236 while (cstack.cs_idx >= 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001237 trylevel = initial_trylevel;
1238 }
1239
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001240 // If a missing ":endtry", ":endwhile", ":endfor", or ":endif" or a memory
1241 // lack was reported above and the error message is to be converted to an
1242 // exception, do this now after rewinding the cstack.
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001243 do_errthrow(&cstack, getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001244 ? (char_u *)"endfunction" : (char_u *)NULL);
1245
1246 if (trylevel == 0)
1247 {
Bram Moolenaar820d55a2020-10-10 15:05:23 +02001248 // Just in case did_throw got set but current_exception wasn't.
1249 if (current_exception == NULL)
1250 did_throw = FALSE;
1251
Bram Moolenaar071d4272004-06-13 20:20:40 +00001252 /*
1253 * When an exception is being thrown out of the outermost try
1254 * conditional, discard the uncaught exception, disable the conversion
1255 * of interrupts or errors to exceptions, and ensure that no more
1256 * commands are executed.
1257 */
1258 if (did_throw)
1259 {
1260 void *p = NULL;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02001261 msglist_T *messages = NULL, *next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001262
1263 /*
1264 * If the uncaught exception is a user exception, report it as an
1265 * error. If it is an error exception, display the saved error
1266 * message now. For an interrupt exception, do nothing; the
1267 * interrupt message is given elsewhere.
1268 */
1269 switch (current_exception->type)
1270 {
1271 case ET_USER:
Bram Moolenaar9c13b352005-05-19 20:53:52 +00001272 vim_snprintf((char *)IObuff, IOSIZE,
1273 _("E605: Exception not caught: %s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001274 current_exception->value);
1275 p = vim_strsave(IObuff);
1276 break;
1277 case ET_ERROR:
1278 messages = current_exception->messages;
1279 current_exception->messages = NULL;
1280 break;
1281 case ET_INTERRUPT:
1282 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001283 }
1284
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001285 estack_push(ETYPE_EXCEPT, current_exception->throw_name,
1286 current_exception->throw_lnum);
Bram Moolenaare31ee862020-01-07 20:59:34 +01001287 ESTACK_CHECK_SETUP
Bram Moolenaar071d4272004-06-13 20:20:40 +00001288 current_exception->throw_name = NULL;
1289
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001290 discard_current_exception(); // uses IObuff if 'verbose'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001291 suppress_errthrow = TRUE;
1292 force_abort = TRUE;
1293
1294 if (messages != NULL)
1295 {
1296 do
1297 {
1298 next = messages->next;
1299 emsg(messages->msg);
1300 vim_free(messages->msg);
Bram Moolenaar5fbf3bc2020-06-01 21:13:11 +02001301 vim_free(messages->sfile);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001302 vim_free(messages);
1303 messages = next;
1304 }
1305 while (messages != NULL);
1306 }
1307 else if (p != NULL)
1308 {
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01001309 emsg(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001310 vim_free(p);
1311 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001312 vim_free(SOURCING_NAME);
Bram Moolenaare31ee862020-01-07 20:59:34 +01001313 ESTACK_CHECK_NOW
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001314 estack_pop();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001315 }
1316
1317 /*
1318 * On an interrupt or an aborting error not converted to an exception,
1319 * disable the conversion of errors to exceptions. (Interrupts are not
Bram Moolenaar2196bce2020-04-12 20:01:11 +02001320 * converted anymore, here.) This enables also the interrupt message
Bram Moolenaar071d4272004-06-13 20:20:40 +00001321 * when force_abort is set and did_emsg unset in case of an interrupt
1322 * from a finally clause after an error.
1323 */
1324 else if (got_int || (did_emsg && force_abort))
1325 suppress_errthrow = TRUE;
1326 }
1327
1328 /*
1329 * The current cstack will be freed when do_cmdline() returns. An uncaught
1330 * exception will have to be rethrown in the previous cstack. If a function
1331 * has just returned or a script file was just finished and the previous
1332 * cstack belongs to the same function or, respectively, script file, it
1333 * will have to be checked for finally clauses to be executed due to the
1334 * ":return" or ":finish". This is done in do_one_cmd().
1335 */
1336 if (did_throw)
1337 need_rethrow = TRUE;
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001338 if ((getline_equal(fgetline, cookie, getsourceline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001339 && ex_nesting_level > source_level(real_cookie))
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001340 || (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001341 && ex_nesting_level > func_level(real_cookie) + 1))
1342 {
1343 if (!did_throw)
1344 check_cstack = TRUE;
1345 }
1346 else
1347 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001348 // When leaving a function, reduce nesting level.
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001349 if (getline_equal(fgetline, cookie, get_func_line))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001350 --ex_nesting_level;
1351 /*
1352 * Go to debug mode when returning from a function in which we are
1353 * single-stepping.
1354 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001355 if ((getline_equal(fgetline, cookie, getsourceline)
1356 || getline_equal(fgetline, cookie, get_func_line))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001357 && ex_nesting_level + 1 <= debug_break_level)
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001358 do_debug(getline_equal(fgetline, cookie, getsourceline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001359 ? (char_u *)_("End of sourced file")
1360 : (char_u *)_("End of function"));
1361 }
1362
1363 /*
1364 * Restore the exception environment (done after returning from the
1365 * debugger).
1366 */
1367 if (flags & DOCMD_EXCRESET)
Bram Moolenaared203462004-06-16 11:19:22 +00001368 restore_dbg_stuff(&debug_saved);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001369
1370 msg_list = saved_msg_list;
Bram Moolenaar292b90d2020-03-18 15:23:16 +01001371
1372 // Cleanup if "cs_emsg_silent_list" remains.
1373 if (cstack.cs_emsg_silent_list != NULL)
1374 {
1375 eslist_T *elem, *temp;
1376
1377 for (elem = cstack.cs_emsg_silent_list; elem != NULL; elem = temp)
1378 {
1379 temp = elem->next;
1380 vim_free(elem);
1381 }
1382 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001383#endif // FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001384
1385 /*
1386 * If there was too much output to fit on the command line, ask the user to
1387 * hit return before redrawing the screen. With the ":global" command we do
1388 * this only once after the command is finished.
1389 */
1390 if (did_inc)
1391 {
1392 --RedrawingDisabled;
1393 --no_wait_return;
1394 msg_scroll = FALSE;
1395
1396 /*
1397 * When just finished an ":if"-":else" which was typed, no need to
1398 * wait for hit-return. Also for an error situation.
1399 */
1400 if (retval == FAIL
1401#ifdef FEAT_EVAL
1402 || (did_endif && KeyTyped && !did_emsg)
1403#endif
1404 )
1405 {
1406 need_wait_return = FALSE;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001407 msg_didany = FALSE; // don't wait when restarting edit
Bram Moolenaar071d4272004-06-13 20:20:40 +00001408 }
1409 else if (need_wait_return)
1410 {
1411 /*
1412 * The msg_start() above clears msg_didout. The wait_return we do
1413 * here should not overwrite the command that may be shown before
1414 * doing that.
1415 */
1416 msg_didout |= msg_didout_before_start;
1417 wait_return(FALSE);
1418 }
1419 }
1420
Bram Moolenaar2a942252012-11-28 23:03:07 +01001421#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001422 did_endif = FALSE; // in case do_cmdline used recursively
Bram Moolenaar2a942252012-11-28 23:03:07 +01001423#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001424 /*
1425 * Reset if_level, in case a sourced script file contains more ":if" than
1426 * ":endif" (could be ":if x | foo | endif").
1427 */
1428 if_level = 0;
Bram Moolenaar2e18a122012-11-28 19:10:54 +01001429#endif
Bram Moolenaard4ad0d42012-11-28 17:34:48 +01001430
Bram Moolenaar071d4272004-06-13 20:20:40 +00001431 --call_depth;
1432 return retval;
1433}
1434
1435#ifdef FEAT_EVAL
1436/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001437 * Obtain a line when inside a ":while" or ":for" loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001438 */
1439 static char_u *
Bram Moolenaar20b23c62020-08-20 15:25:00 +02001440get_loop_line(int c, void *cookie, int indent, getline_opt_T options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001441{
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001442 struct loop_cookie *cp = (struct loop_cookie *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001443 wcmd_T *wp;
1444 char_u *line;
1445
1446 if (cp->current_line + 1 >= cp->lines_gap->ga_len)
1447 {
1448 if (cp->repeating)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001449 return NULL; // trying to read past ":endwhile"/":endfor"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001450
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001451 // First time inside the ":while"/":for": get line normally.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001452 if (cp->getline == NULL)
Bram Moolenaar20b23c62020-08-20 15:25:00 +02001453 line = getcmdline(c, 0L, indent, options);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001454 else
Bram Moolenaar20b23c62020-08-20 15:25:00 +02001455 line = cp->getline(c, cp->cookie, indent, options);
Bram Moolenaard68071d2006-05-02 22:08:30 +00001456 if (line != NULL && store_loop_line(cp->lines_gap, line) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001457 ++cp->current_line;
1458
1459 return line;
1460 }
1461
1462 KeyTyped = FALSE;
1463 ++cp->current_line;
1464 wp = (wcmd_T *)(cp->lines_gap->ga_data) + cp->current_line;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001465 SOURCING_LNUM = wp->lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001466 return vim_strsave(wp->line);
1467}
1468
1469/*
1470 * Store a line in "gap" so that a ":while" loop can execute it again.
1471 */
1472 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001473store_loop_line(garray_T *gap, char_u *line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001474{
1475 if (ga_grow(gap, 1) == FAIL)
1476 return FAIL;
1477 ((wcmd_T *)(gap->ga_data))[gap->ga_len].line = vim_strsave(line);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001478 ((wcmd_T *)(gap->ga_data))[gap->ga_len].lnum = SOURCING_LNUM;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001479 ++gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001480 return OK;
1481}
1482
1483/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001484 * Free the lines stored for a ":while" or ":for" loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001485 */
1486 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001487free_cmdlines(garray_T *gap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001488{
1489 while (gap->ga_len > 0)
1490 {
1491 vim_free(((wcmd_T *)(gap->ga_data))[gap->ga_len - 1].line);
1492 --gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001493 }
1494}
1495#endif
1496
1497/*
Bram Moolenaar89d40322006-08-29 15:30:07 +00001498 * If "fgetline" is get_loop_line(), return TRUE if the getline it uses equals
1499 * "func". * Otherwise return TRUE when "fgetline" equals "func".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001500 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001501 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001502getline_equal(
Bram Moolenaar66250c92020-08-20 15:02:42 +02001503 char_u *(*fgetline)(int, void *, int, getline_opt_T),
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001504 void *cookie UNUSED, // argument for fgetline()
Bram Moolenaar66250c92020-08-20 15:02:42 +02001505 char_u *(*func)(int, void *, int, getline_opt_T))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001506{
1507#ifdef FEAT_EVAL
Bram Moolenaar20b23c62020-08-20 15:25:00 +02001508 char_u *(*gp)(int, void *, int, getline_opt_T);
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001509 struct loop_cookie *cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001510
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001511 // When "fgetline" is "get_loop_line()" use the "cookie" to find the
1512 // function that's originally used to obtain the lines. This may be
1513 // nested several levels.
Bram Moolenaar89d40322006-08-29 15:30:07 +00001514 gp = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001515 cp = (struct loop_cookie *)cookie;
1516 while (gp == get_loop_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001517 {
1518 gp = cp->getline;
1519 cp = cp->cookie;
1520 }
1521 return gp == func;
1522#else
Bram Moolenaar89d40322006-08-29 15:30:07 +00001523 return fgetline == func;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001524#endif
1525}
1526
Bram Moolenaar071d4272004-06-13 20:20:40 +00001527/*
Bram Moolenaar89d40322006-08-29 15:30:07 +00001528 * If "fgetline" is get_loop_line(), return the cookie used by the original
Bram Moolenaar071d4272004-06-13 20:20:40 +00001529 * getline function. Otherwise return "cookie".
1530 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001531 void *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001532getline_cookie(
Bram Moolenaar66250c92020-08-20 15:02:42 +02001533 char_u *(*fgetline)(int, void *, int, getline_opt_T) UNUSED,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001534 void *cookie) // argument for fgetline()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001535{
Bram Moolenaar13505972019-01-24 15:04:48 +01001536#ifdef FEAT_EVAL
Bram Moolenaar20b23c62020-08-20 15:25:00 +02001537 char_u *(*gp)(int, void *, int, getline_opt_T);
Bram Moolenaard5053d02020-06-28 15:51:16 +02001538 struct loop_cookie *cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001539
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001540 // When "fgetline" is "get_loop_line()" use the "cookie" to find the
1541 // cookie that's originally used to obtain the lines. This may be nested
1542 // several levels.
Bram Moolenaar89d40322006-08-29 15:30:07 +00001543 gp = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001544 cp = (struct loop_cookie *)cookie;
1545 while (gp == get_loop_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001546 {
1547 gp = cp->getline;
1548 cp = cp->cookie;
1549 }
1550 return cp;
Bram Moolenaar13505972019-01-24 15:04:48 +01001551#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001552 return cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001553#endif
Bram Moolenaar13505972019-01-24 15:04:48 +01001554}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001555
Bram Moolenaard5053d02020-06-28 15:51:16 +02001556#if defined(FEAT_EVAL) || defined(PROT)
1557/*
1558 * Get the next line source line without advancing.
1559 */
1560 char_u *
1561getline_peek(
Bram Moolenaar66250c92020-08-20 15:02:42 +02001562 char_u *(*fgetline)(int, void *, int, getline_opt_T) UNUSED,
Bram Moolenaard5053d02020-06-28 15:51:16 +02001563 void *cookie) // argument for fgetline()
1564{
Bram Moolenaar20b23c62020-08-20 15:25:00 +02001565 char_u *(*gp)(int, void *, int, getline_opt_T);
Bram Moolenaard5053d02020-06-28 15:51:16 +02001566 struct loop_cookie *cp;
1567 wcmd_T *wp;
1568
1569 // When "fgetline" is "get_loop_line()" use the "cookie" to find the
1570 // cookie that's originally used to obtain the lines. This may be nested
1571 // several levels.
1572 gp = fgetline;
1573 cp = (struct loop_cookie *)cookie;
1574 while (gp == get_loop_line)
1575 {
1576 if (cp->current_line + 1 < cp->lines_gap->ga_len)
1577 {
1578 // executing lines a second time, use the stored copy
1579 wp = (wcmd_T *)(cp->lines_gap->ga_data) + cp->current_line + 1;
1580 return wp->line;
1581 }
1582 gp = cp->getline;
1583 cp = cp->cookie;
1584 }
1585 if (gp == getsourceline)
1586 return source_nextline(cp);
1587 return NULL;
1588}
1589#endif
1590
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001591
1592/*
1593 * Helper function to apply an offset for buffer commands, i.e. ":bdelete",
1594 * ":bwipeout", etc.
1595 * Returns the buffer number.
1596 */
1597 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001598compute_buffer_local_count(int addr_type, int lnum, int offset)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001599{
1600 buf_T *buf;
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001601 buf_T *nextbuf;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001602 int count = offset;
1603
1604 buf = firstbuf;
1605 while (buf->b_next != NULL && buf->b_fnum < lnum)
1606 buf = buf->b_next;
1607 while (count != 0)
1608 {
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001609 count += (offset < 0) ? 1 : -1;
1610 nextbuf = (offset < 0) ? buf->b_prev : buf->b_next;
1611 if (nextbuf == NULL)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001612 break;
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001613 buf = nextbuf;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001614 if (addr_type == ADDR_LOADED_BUFFERS)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001615 // skip over unloaded buffers
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001616 while (buf->b_ml.ml_mfp == NULL)
1617 {
1618 nextbuf = (offset < 0) ? buf->b_prev : buf->b_next;
1619 if (nextbuf == NULL)
1620 break;
1621 buf = nextbuf;
1622 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001623 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001624 // we might have gone too far, last buffer is not loadedd
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001625 if (addr_type == ADDR_LOADED_BUFFERS)
1626 while (buf->b_ml.ml_mfp == NULL)
1627 {
1628 nextbuf = (offset >= 0) ? buf->b_prev : buf->b_next;
1629 if (nextbuf == NULL)
1630 break;
1631 buf = nextbuf;
1632 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001633 return buf->b_fnum;
1634}
1635
Bram Moolenaarb7316892019-05-01 18:08:42 +02001636/*
1637 * Return the window number of "win".
1638 * When "win" is NULL return the number of windows.
1639 */
Bram Moolenaarf240e182014-11-27 18:33:02 +01001640 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001641current_win_nr(win_T *win)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001642{
1643 win_T *wp;
1644 int nr = 0;
1645
Bram Moolenaar29323592016-07-24 22:04:11 +02001646 FOR_ALL_WINDOWS(wp)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001647 {
1648 ++nr;
1649 if (wp == win)
1650 break;
1651 }
1652 return nr;
1653}
1654
1655 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001656current_tab_nr(tabpage_T *tab)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001657{
1658 tabpage_T *tp;
1659 int nr = 0;
1660
Bram Moolenaar29323592016-07-24 22:04:11 +02001661 FOR_ALL_TABPAGES(tp)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001662 {
1663 ++nr;
1664 if (tp == tab)
1665 break;
1666 }
1667 return nr;
1668}
1669
Bram Moolenaar6e36b1c2020-07-17 20:47:51 +02001670 static int
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001671comment_start(char_u *p, int starts_with_colon UNUSED)
1672{
1673#ifdef FEAT_EVAL
1674 if (in_vim9script())
1675 return p[0] == '#' && p[1] != '{' && !starts_with_colon;
1676#endif
1677 return *p == '"';
1678}
1679
Bram Moolenaarf240e182014-11-27 18:33:02 +01001680# define CURRENT_WIN_NR current_win_nr(curwin)
1681# define LAST_WIN_NR current_win_nr(NULL)
1682# define CURRENT_TAB_NR current_tab_nr(curtab)
1683# define LAST_TAB_NR current_tab_nr(NULL)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001684
Bram Moolenaar071d4272004-06-13 20:20:40 +00001685/*
1686 * Execute one Ex command.
1687 *
1688 * If 'sourcing' is TRUE, the command will be included in the error message.
1689 *
1690 * 1. skip comment lines and leading space
1691 * 2. handle command modifiers
Bram Moolenaar1c40a662014-11-27 16:38:11 +01001692 * 3. find the command
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001693 * 4. parse range
Bram Moolenaar1c40a662014-11-27 16:38:11 +01001694 * 5. Parse the command.
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001695 * 6. parse arguments
1696 * 7. switch on command name
Bram Moolenaar071d4272004-06-13 20:20:40 +00001697 *
Bram Moolenaar89d40322006-08-29 15:30:07 +00001698 * Note: "fgetline" can be NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001699 *
1700 * This function may be called recursively!
1701 */
1702#if (_MSC_VER == 1200)
1703/*
Bram Moolenaared203462004-06-16 11:19:22 +00001704 * Avoid optimisation bug in VC++ version 6.0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001705 */
Bram Moolenaar281bdce2005-01-25 21:53:18 +00001706 #pragma optimize( "g", off )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001707#endif
1708 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001709do_one_cmd(
Bram Moolenaarddef1292019-12-16 17:10:33 +01001710 char_u **cmdlinep,
1711 int sourcing,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001712#ifdef FEAT_EVAL
Bram Moolenaarddef1292019-12-16 17:10:33 +01001713 cstack_T *cstack,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001714#endif
Bram Moolenaar20b23c62020-08-20 15:25:00 +02001715 char_u *(*fgetline)(int, void *, int, getline_opt_T),
Bram Moolenaarddef1292019-12-16 17:10:33 +01001716 void *cookie) // argument for fgetline()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001717{
Bram Moolenaarddef1292019-12-16 17:10:33 +01001718 char_u *p;
1719 linenr_T lnum;
1720 long n;
1721 char *errormsg = NULL; // error message
1722 char_u *after_modifier = NULL;
1723 exarg_T ea; // Ex command arguments
Bram Moolenaarddef1292019-12-16 17:10:33 +01001724 cmdmod_T save_cmdmod;
1725 int save_reg_executing = reg_executing;
1726 int ni; // set when Not Implemented
1727 char_u *cmd;
Bram Moolenaar6e36b1c2020-07-17 20:47:51 +02001728 int starts_with_colon = FALSE;
Bram Moolenaar83f37b92020-02-23 14:35:01 +01001729#ifdef FEAT_EVAL
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02001730 int may_have_range;
Bram Moolenaar3d48e252020-07-15 14:15:52 +02001731 int vim9script = in_vim9script();
Bram Moolenaarb4bcea42020-10-28 13:53:50 +01001732 int did_set_expr_line = FALSE;
Bram Moolenaar83f37b92020-02-23 14:35:01 +01001733#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001734
Bram Moolenaara80faa82020-04-12 19:37:17 +02001735 CLEAR_FIELD(ea);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001736 ea.line1 = 1;
1737 ea.line2 = 1;
1738#ifdef FEAT_EVAL
1739 ++ex_nesting_level;
1740#endif
1741
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001742 // When the last file has not been edited :q has to be typed twice.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001743 if (quitmore
1744#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001745 // avoid that a function call in 'statusline' does this
Bram Moolenaar89d40322006-08-29 15:30:07 +00001746 && !getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001747#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001748 // avoid that an autocommand, e.g. QuitPre, does this
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001749 && !getline_equal(fgetline, cookie, getnextac))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001750 --quitmore;
1751
1752 /*
1753 * Reset browse, confirm, etc.. They are restored when returning, for
1754 * recursive calls.
1755 */
1756 save_cmdmod = cmdmod;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001757
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001758 // "#!anything" is handled like a comment.
Bram Moolenaarcbb37ad2006-08-16 15:04:21 +00001759 if ((*cmdlinep)[0] == '#' && (*cmdlinep)[1] == '!')
1760 goto doend;
1761
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02001762/*
1763 * 1. Skip comment lines and leading white space and colons.
1764 * 2. Handle command modifiers.
1765 */
1766 // The "ea" structure holds the arguments that can be used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001767 ea.cmd = *cmdlinep;
Bram Moolenaareffed932018-08-14 13:38:17 +02001768 ea.cmdlinep = cmdlinep;
1769 ea.getline = fgetline;
1770 ea.cookie = cookie;
1771#ifdef FEAT_EVAL
1772 ea.cstack = cstack;
Bram Moolenaar83f37b92020-02-23 14:35:01 +01001773 starts_with_colon = *skipwhite(ea.cmd) == ':';
Bram Moolenaar071d4272004-06-13 20:20:40 +00001774#endif
Bram Moolenaare1004402020-10-24 20:49:43 +02001775 if (parse_command_modifiers(&ea, &errormsg, &cmdmod, FALSE) == FAIL)
Bram Moolenaareffed932018-08-14 13:38:17 +02001776 goto doend;
Bram Moolenaar5661ed62020-10-24 17:19:16 +02001777 apply_cmdmod(&cmdmod);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001778
Bram Moolenaar7b668e82016-08-23 23:51:21 +02001779 after_modifier = ea.cmd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001780
1781#ifdef FEAT_EVAL
1782 ea.skip = did_emsg || got_int || did_throw || (cstack->cs_idx >= 0
1783 && !(cstack->cs_flags[cstack->cs_idx] & CSF_ACTIVE));
1784#else
1785 ea.skip = (if_level > 0);
1786#endif
1787
Bram Moolenaar071d4272004-06-13 20:20:40 +00001788/*
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001789 * 3. Skip over the range to find the command. Let "p" point to after it.
1790 *
1791 * We need the command to know what kind of range it uses.
1792 */
1793 cmd = ea.cmd;
Bram Moolenaar3d48e252020-07-15 14:15:52 +02001794#ifdef FEAT_EVAL
Bram Moolenaare88c8e82020-11-01 17:03:37 +01001795 // In Vim9 script a colon is required before the range. This may also be
1796 // after command modifiers.
1797 if (vim9script)
1798 {
1799 may_have_range = FALSE;
1800 for (p = ea.cmd; p >= *cmdlinep; --p)
1801 {
1802 if (*p == ':')
1803 may_have_range = TRUE;
1804 if (p < ea.cmd && !VIM_ISWHITE(*p))
1805 break;
1806 }
1807 }
1808 else
1809 may_have_range = TRUE;
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02001810 if (may_have_range)
Bram Moolenaar3d48e252020-07-15 14:15:52 +02001811#endif
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02001812 ea.cmd = skip_range(ea.cmd, TRUE, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001813
1814#ifdef FEAT_EVAL
Bram Moolenaare88c8e82020-11-01 17:03:37 +01001815 if (vim9script && !may_have_range)
Bram Moolenaardf069ee2020-06-22 23:02:51 +02001816 {
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02001817 if (ea.cmd == cmd + 1 && *cmd == '$')
1818 // should be "$VAR = val"
1819 --ea.cmd;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001820 p = find_ex_command(&ea, NULL, lookup_scriptvar, NULL);
Bram Moolenaar36113e42020-11-02 21:08:47 +01001821 if (ea.cmdidx == CMD_SIZE)
1822 {
1823 char_u *ar = skip_range(ea.cmd, TRUE, NULL);
1824
1825 // If a ':' before the range is missing, give a clearer error
1826 // message.
1827 if (ar > ea.cmd)
1828 {
1829 emsg(_(e_colon_required_before_a_range));
1830 goto doend;
1831 }
1832 }
Bram Moolenaardf069ee2020-06-22 23:02:51 +02001833 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001834 else
1835#endif
1836 p = find_ex_command(&ea, NULL, NULL, NULL);
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001837
Bram Moolenaar7feb35e2018-08-21 17:49:54 +02001838#ifdef FEAT_EVAL
1839# ifdef FEAT_PROFILE
1840 // Count this line for profiling if skip is TRUE.
1841 if (do_profiling == PROF_YES
1842 && (!ea.skip || cstack->cs_idx == 0 || (cstack->cs_idx > 0
1843 && (cstack->cs_flags[cstack->cs_idx - 1] & CSF_ACTIVE))))
1844 {
1845 int skip = did_emsg || got_int || did_throw;
1846
1847 if (ea.cmdidx == CMD_catch)
1848 skip = !skip && !(cstack->cs_idx >= 0
1849 && (cstack->cs_flags[cstack->cs_idx] & CSF_THROWN)
1850 && !(cstack->cs_flags[cstack->cs_idx] & CSF_CAUGHT));
1851 else if (ea.cmdidx == CMD_else || ea.cmdidx == CMD_elseif)
1852 skip = skip || !(cstack->cs_idx >= 0
1853 && !(cstack->cs_flags[cstack->cs_idx]
1854 & (CSF_ACTIVE | CSF_TRUE)));
1855 else if (ea.cmdidx == CMD_finally)
1856 skip = FALSE;
1857 else if (ea.cmdidx != CMD_endif
1858 && ea.cmdidx != CMD_endfor
1859 && ea.cmdidx != CMD_endtry
1860 && ea.cmdidx != CMD_endwhile)
1861 skip = ea.skip;
1862
1863 if (!skip)
1864 {
1865 if (getline_equal(fgetline, cookie, get_func_line))
1866 func_line_exec(getline_cookie(fgetline, cookie));
1867 else if (getline_equal(fgetline, cookie, getsourceline))
1868 script_line_exec();
1869 }
1870 }
1871# endif
1872
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001873 // May go to debug mode. If this happens and the ">quit" debug command is
1874 // used, throw an interrupt exception and skip the next command.
Bram Moolenaar7feb35e2018-08-21 17:49:54 +02001875 dbg_check_breakpoint(&ea);
1876 if (!ea.skip && got_int)
1877 {
1878 ea.skip = TRUE;
1879 (void)do_intthrow(cstack);
1880 }
1881#endif
1882
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001883/*
1884 * 4. parse a range specifier of the form: addr [,addr] [;addr] ..
Bram Moolenaar071d4272004-06-13 20:20:40 +00001885 *
1886 * where 'addr' is:
1887 *
1888 * % (entire file)
1889 * $ [+-NUM]
1890 * 'x [+-NUM] (where x denotes a currently defined mark)
1891 * . [+-NUM]
1892 * [+-NUM]..
1893 * NUM
1894 *
1895 * The ea.cmd pointer is updated to point to the first character following the
1896 * range spec. If an initial address is found, but no second, the upper bound
1897 * is equal to the lower.
1898 */
1899
Bram Moolenaar25190db2019-05-04 15:05:28 +02001900 // ea.addr_type for user commands is set by find_ucmd
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01001901 if (!IS_USER_CMDIDX(ea.cmdidx))
1902 {
1903 if (ea.cmdidx != CMD_SIZE)
1904 ea.addr_type = cmdnames[(int)ea.cmdidx].cmd_addr_type;
1905 else
1906 ea.addr_type = ADDR_LINES;
1907
Bram Moolenaar25190db2019-05-04 15:05:28 +02001908 // :wincmd range depends on the argument.
Bram Moolenaar164f3262015-01-14 21:22:01 +01001909 if (ea.cmdidx == CMD_wincmd && p != NULL)
1910 get_wincmd_addr_type(skipwhite(p), &ea);
Bram Moolenaar25190db2019-05-04 15:05:28 +02001911#ifdef FEAT_QUICKFIX
1912 // :.cc in quickfix window uses line number
1913 if ((ea.cmdidx == CMD_cc || ea.cmdidx == CMD_ll) && bt_quickfix(curbuf))
1914 ea.addr_type = ADDR_OTHER;
1915#endif
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01001916 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001917
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01001918 ea.cmd = cmd;
Bram Moolenaar3d48e252020-07-15 14:15:52 +02001919#ifdef FEAT_EVAL
Bram Moolenaarc2af0af2020-08-23 21:06:02 +02001920 if (!may_have_range)
1921 ea.line1 = ea.line2 = default_address(&ea);
1922 else
Bram Moolenaar3d48e252020-07-15 14:15:52 +02001923#endif
1924 if (parse_cmd_address(&ea, &errormsg, FALSE) == FAIL)
1925 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001926
Bram Moolenaar071d4272004-06-13 20:20:40 +00001927/*
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001928 * 5. Parse the command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001929 */
1930
1931 /*
1932 * Skip ':' and any white space
1933 */
1934 ea.cmd = skipwhite(ea.cmd);
1935 while (*ea.cmd == ':')
1936 ea.cmd = skipwhite(ea.cmd + 1);
1937
1938 /*
1939 * If we got a line, but no command, then go to the line.
1940 * If we find a '|' or '\n' we set ea.nextcmd.
1941 */
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02001942 if (*ea.cmd == NUL || comment_start(ea.cmd, starts_with_colon)
1943 || (ea.nextcmd = check_nextcmd(ea.cmd)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001944 {
1945 /*
1946 * strange vi behaviour:
1947 * ":3" jumps to line 3
1948 * ":3|..." prints line 3
1949 * ":|" prints current line
1950 */
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001951 if (ea.skip) // skip this if inside :if
Bram Moolenaar071d4272004-06-13 20:20:40 +00001952 goto doend;
Bram Moolenaardf177f62005-02-22 08:39:57 +00001953 if (*ea.cmd == '|' || (exmode_active && ea.line1 != ea.line2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001954 {
1955 ea.cmdidx = CMD_print;
Bram Moolenaar8071cb22019-07-12 17:58:01 +02001956 ea.argt = EX_RANGE+EX_COUNT+EX_TRLBAR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001957 if ((errormsg = invalid_range(&ea)) == NULL)
1958 {
1959 correct_range(&ea);
1960 ex_print(&ea);
1961 }
1962 }
1963 else if (ea.addr_count != 0)
1964 {
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00001965 if (ea.line2 > curbuf->b_ml.ml_line_count)
1966 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001967 // With '-' in 'cpoptions' a line number past the file is an
1968 // error, otherwise put it at the end of the file.
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00001969 if (vim_strchr(p_cpo, CPO_MINUS) != NULL)
1970 ea.line2 = -1;
1971 else
1972 ea.line2 = curbuf->b_ml.ml_line_count;
1973 }
1974
1975 if (ea.line2 < 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001976 errormsg = _(e_invrange);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001977 else
1978 {
1979 if (ea.line2 == 0)
1980 curwin->w_cursor.lnum = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001981 else
1982 curwin->w_cursor.lnum = ea.line2;
1983 beginline(BL_SOL | BL_FIX);
1984 }
1985 }
1986 goto doend;
1987 }
1988
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001989 // If this looks like an undefined user command and there are CmdUndefined
1990 // autocommands defined, trigger the matching autocommands.
Bram Moolenaard5005162014-08-22 23:05:54 +02001991 if (p != NULL && ea.cmdidx == CMD_SIZE && !ea.skip
1992 && ASCII_ISUPPER(*ea.cmd)
1993 && has_cmdundefined())
1994 {
Bram Moolenaard5005162014-08-22 23:05:54 +02001995 int ret;
1996
Bram Moolenaar5a31b462014-08-23 14:16:20 +02001997 p = ea.cmd;
Bram Moolenaard5005162014-08-22 23:05:54 +02001998 while (ASCII_ISALNUM(*p))
1999 ++p;
Bram Moolenaar158ea172020-06-18 17:28:39 +02002000 p = vim_strnsave(ea.cmd, p - ea.cmd);
Bram Moolenaard5005162014-08-22 23:05:54 +02002001 ret = apply_autocmds(EVENT_CMDUNDEFINED, p, p, TRUE, NULL);
2002 vim_free(p);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002003 // If the autocommands did something and didn't cause an error, try
2004 // finding the command again.
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002005 p = (ret
2006#ifdef FEAT_EVAL
2007 && !aborting()
Bram Moolenaard5005162014-08-22 23:05:54 +02002008#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002009 ) ? find_ex_command(&ea, NULL, NULL, NULL) : ea.cmd;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002010 }
Bram Moolenaard5005162014-08-22 23:05:54 +02002011
Bram Moolenaar071d4272004-06-13 20:20:40 +00002012 if (p == NULL)
2013 {
2014 if (!ea.skip)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002015 errormsg = _("E464: Ambiguous use of user-defined command");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002016 goto doend;
2017 }
Bram Moolenaarac9fb182019-04-27 13:04:13 +02002018 // Check for wrong commands.
Bram Moolenaar6f9a4762017-06-22 20:39:17 +02002019 if (*p == '!' && ea.cmd[1] == 0151 && ea.cmd[0] == 78
2020 && !IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002021 {
2022 errormsg = uc_fun_cmd();
2023 goto doend;
2024 }
Bram Moolenaarac9fb182019-04-27 13:04:13 +02002025
Bram Moolenaar071d4272004-06-13 20:20:40 +00002026 if (ea.cmdidx == CMD_SIZE)
2027 {
2028 if (!ea.skip)
2029 {
2030 STRCPY(IObuff, _("E492: Not an editor command"));
2031 if (!sourcing)
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002032 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002033 // If the modifier was parsed OK the error must be in the
2034 // following command
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002035 if (after_modifier != NULL)
2036 append_command(after_modifier);
2037 else
2038 append_command(*cmdlinep);
2039 }
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002040 errormsg = (char *)IObuff;
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02002041 did_emsg_syntax = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002042 }
2043 goto doend;
2044 }
2045
Bram Moolenaar958636c2014-10-21 20:01:58 +02002046 ni = (!IS_USER_CMDIDX(ea.cmdidx)
2047 && (cmdnames[ea.cmdidx].cmd_func == ex_ni
Bram Moolenaar7bb75552007-07-16 18:39:49 +00002048#ifdef HAVE_EX_SCRIPT_NI
2049 || cmdnames[ea.cmdidx].cmd_func == ex_script_ni
2050#endif
2051 ));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002052
2053#ifndef FEAT_EVAL
2054 /*
2055 * When the expression evaluation is disabled, recognize the ":if" and
2056 * ":endif" commands and ignore everything in between it.
2057 */
2058 if (ea.cmdidx == CMD_if)
2059 ++if_level;
2060 if (if_level)
2061 {
2062 if (ea.cmdidx == CMD_endif)
2063 --if_level;
2064 goto doend;
2065 }
2066
2067#endif
2068
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002069 // forced commands
Bram Moolenaar196b3b02008-06-20 16:51:41 +00002070 if (*p == '!' && ea.cmdidx != CMD_substitute
2071 && ea.cmdidx != CMD_smagic && ea.cmdidx != CMD_snomagic)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002072 {
2073 ++p;
2074 ea.forceit = TRUE;
2075 }
2076 else
2077 ea.forceit = FALSE;
2078
2079/*
Bram Moolenaarb7316892019-05-01 18:08:42 +02002080 * 6. Parse arguments. Then check for errors.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002081 */
Bram Moolenaar958636c2014-10-21 20:01:58 +02002082 if (!IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002083 ea.argt = (long)cmdnames[(int)ea.cmdidx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002084
2085 if (!ea.skip)
2086 {
2087#ifdef HAVE_SANDBOX
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002088 if (sandbox != 0 && !(ea.argt & EX_SBOXOK))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002089 {
Bram Moolenaar8c62a082019-02-08 14:34:10 +01002090 // Command not allowed in sandbox.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002091 errormsg = _(e_sandbox);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002092 goto doend;
2093 }
2094#endif
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002095 if (restricted != 0 && (ea.argt & EX_RESTRICT))
Bram Moolenaar8c62a082019-02-08 14:34:10 +01002096 {
2097 errormsg = _("E981: Command not allowed in rvim");
2098 goto doend;
2099 }
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002100 if (!curbuf->b_p_ma && (ea.argt & EX_MODIFY))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002101 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002102 // Command not allowed in non-'modifiable' buffer
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002103 errormsg = _(e_modifiable);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002104 goto doend;
2105 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002106
Bram Moolenaar37394ff2020-07-25 19:38:18 +02002107 if (!IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002108 {
Bram Moolenaar37394ff2020-07-25 19:38:18 +02002109#ifdef FEAT_CMDWIN
2110 if (cmdwin_type != 0 && !(ea.argt & EX_CMDWIN))
2111 {
2112 // Command not allowed in the command line window
2113 errormsg = _(e_cmdwin);
2114 goto doend;
2115 }
2116#endif
2117 if (text_locked() && !(ea.argt & EX_LOCK_OK))
2118 {
2119 // Command not allowed when text is locked
2120 errormsg = _(get_text_locked_msg());
2121 goto doend;
2122 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002123 }
Bram Moolenaar379fb762018-08-30 15:58:28 +02002124
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002125 // Disallow editing another buffer when "curbuf_lock" is set.
2126 // Do allow ":checktime" (it is postponed).
2127 // Do allow ":edit" (check for an argument later).
2128 // Do allow ":file" with no arguments (check for an argument later).
Bram Moolenaar8e1986e2020-08-06 22:11:06 +02002129 if (!(ea.argt & (EX_CMDWIN | EX_LOCK_OK))
Bram Moolenaar5555acc2006-04-07 21:33:12 +00002130 && ea.cmdidx != CMD_checktime
Bram Moolenaar379fb762018-08-30 15:58:28 +02002131 && ea.cmdidx != CMD_edit
2132 && ea.cmdidx != CMD_file
Bram Moolenaar958636c2014-10-21 20:01:58 +02002133 && !IS_USER_CMDIDX(ea.cmdidx)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002134 && curbuf_locked())
2135 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002136
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002137 if (!ni && !(ea.argt & EX_RANGE) && ea.addr_count > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002138 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002139 // no range allowed
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002140 errormsg = _(e_norange);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002141 goto doend;
2142 }
2143 }
2144
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002145 if (!ni && !(ea.argt & EX_BANG) && ea.forceit) // no <!> allowed
Bram Moolenaar071d4272004-06-13 20:20:40 +00002146 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002147 errormsg = _(e_nobang);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002148 goto doend;
2149 }
2150
2151 /*
2152 * Don't complain about the range if it is not used
2153 * (could happen if line_count is accidentally set to 0).
2154 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002155 if (!ea.skip && !ni && (ea.argt & EX_RANGE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002156 {
2157 /*
2158 * If the range is backwards, ask for confirmation and, if given, swap
2159 * ea.line1 & ea.line2 so it's forwards again.
2160 * When global command is busy, don't ask, will fail below.
2161 */
2162 if (!global_busy && ea.line1 > ea.line2)
2163 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002164 if (msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002165 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002166 if (sourcing || exmode_active)
2167 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002168 errormsg = _("E493: Backwards range given");
Bram Moolenaara5792f52005-11-23 21:25:05 +00002169 goto doend;
2170 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002171 if (ask_yesno((char_u *)
2172 _("Backwards range given, OK to swap"), FALSE) != 'y')
Bram Moolenaara5792f52005-11-23 21:25:05 +00002173 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002174 }
2175 lnum = ea.line1;
2176 ea.line1 = ea.line2;
2177 ea.line2 = lnum;
2178 }
2179 if ((errormsg = invalid_range(&ea)) != NULL)
2180 goto doend;
2181 }
2182
Bram Moolenaarb7316892019-05-01 18:08:42 +02002183 if ((ea.addr_type == ADDR_OTHER) && ea.addr_count == 0)
2184 // default is 1, not cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00002185 ea.line2 = 1;
2186
2187 correct_range(&ea);
2188
2189#ifdef FEAT_FOLDING
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002190 if (((ea.argt & EX_WHOLEFOLD) || ea.addr_count >= 2) && !global_busy
Bram Moolenaara3306952016-01-02 21:41:06 +01002191 && ea.addr_type == ADDR_LINES)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002192 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002193 // Put the first line at the start of a closed fold, put the last line
2194 // at the end of a closed fold.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002195 (void)hasFolding(ea.line1, &ea.line1, NULL);
2196 (void)hasFolding(ea.line2, NULL, &ea.line2);
2197 }
2198#endif
2199
2200#ifdef FEAT_QUICKFIX
2201 /*
Bram Moolenaar86b68352004-12-27 21:59:20 +00002202 * For the ":make" and ":grep" commands we insert the 'makeprg'/'grepprg'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002203 * option here, so things like % get expanded.
2204 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002205 p = replace_makeprg(&ea, p, cmdlinep);
2206 if (p == NULL)
2207 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002208#endif
2209
2210 /*
2211 * Skip to start of argument.
2212 * Don't do this for the ":!" command, because ":!! -l" needs the space.
2213 */
2214 if (ea.cmdidx == CMD_bang)
2215 ea.arg = p;
2216 else
2217 ea.arg = skipwhite(p);
2218
Bram Moolenaar379fb762018-08-30 15:58:28 +02002219 // ":file" cannot be run with an argument when "curbuf_lock" is set
2220 if (ea.cmdidx == CMD_file && *ea.arg != NUL && curbuf_locked())
2221 goto doend;
2222
Bram Moolenaar071d4272004-06-13 20:20:40 +00002223 /*
2224 * Check for "++opt=val" argument.
2225 * Must be first, allow ":w ++enc=utf8 !cmd"
2226 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002227 if (ea.argt & EX_ARGOPT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002228 while (ea.arg[0] == '+' && ea.arg[1] == '+')
2229 if (getargopt(&ea) == FAIL && !ni)
2230 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002231 errormsg = _(e_invarg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002232 goto doend;
2233 }
2234
2235 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
2236 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002237 if (*ea.arg == '>') // append
Bram Moolenaar071d4272004-06-13 20:20:40 +00002238 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002239 if (*++ea.arg != '>') // typed wrong
Bram Moolenaar071d4272004-06-13 20:20:40 +00002240 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002241 errormsg = _("E494: Use w or w>>");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002242 goto doend;
2243 }
2244 ea.arg = skipwhite(ea.arg + 1);
2245 ea.append = TRUE;
2246 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002247 else if (*ea.arg == '!' && ea.cmdidx == CMD_write) // :w !filter
Bram Moolenaar071d4272004-06-13 20:20:40 +00002248 {
2249 ++ea.arg;
2250 ea.usefilter = TRUE;
2251 }
2252 }
2253
2254 if (ea.cmdidx == CMD_read)
2255 {
2256 if (ea.forceit)
2257 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002258 ea.usefilter = TRUE; // :r! filter if ea.forceit
Bram Moolenaar071d4272004-06-13 20:20:40 +00002259 ea.forceit = FALSE;
2260 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002261 else if (*ea.arg == '!') // :r !filter
Bram Moolenaar071d4272004-06-13 20:20:40 +00002262 {
2263 ++ea.arg;
2264 ea.usefilter = TRUE;
2265 }
2266 }
2267
2268 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
2269 {
2270 ea.amount = 1;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002271 while (*ea.arg == *ea.cmd) // count number of '>' or '<'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002272 {
2273 ++ea.arg;
2274 ++ea.amount;
2275 }
2276 ea.arg = skipwhite(ea.arg);
2277 }
2278
2279 /*
2280 * Check for "+command" argument, before checking for next command.
2281 * Don't do this for ":read !cmd" and ":write !cmd".
2282 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002283 if ((ea.argt & EX_CMDARG) && !ea.usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002284 ea.do_ecmd_cmd = getargcmd(&ea.arg);
2285
2286 /*
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002287 * Check for '|' to separate commands and '"' or '#' to start comments.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002288 * Don't do this for ":read !cmd" and ":write !cmd".
2289 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002290 if ((ea.argt & EX_TRLBAR) && !ea.usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002291 separate_nextcmd(&ea);
2292
2293 /*
2294 * Check for <newline> to end a shell command.
Bram Moolenaardf177f62005-02-22 08:39:57 +00002295 * Also do this for ":read !cmd", ":write !cmd" and ":global".
2296 * Any others?
Bram Moolenaar071d4272004-06-13 20:20:40 +00002297 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00002298 else if (ea.cmdidx == CMD_bang
Bram Moolenaar67883b42017-07-27 22:57:00 +02002299 || ea.cmdidx == CMD_terminal
Bram Moolenaardf177f62005-02-22 08:39:57 +00002300 || ea.cmdidx == CMD_global
2301 || ea.cmdidx == CMD_vglobal
2302 || ea.usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002303 {
2304 for (p = ea.arg; *p; ++p)
2305 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002306 // Remove one backslash before a newline, so that it's possible to
2307 // pass a newline to the shell and also a newline that is preceded
2308 // with a backslash. This makes it impossible to end a shell
2309 // command in a backslash, but that doesn't appear useful.
2310 // Halving the number of backslashes is incompatible with previous
2311 // versions.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002312 if (*p == '\\' && p[1] == '\n')
Bram Moolenaara7241f52008-06-24 20:39:31 +00002313 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002314 else if (*p == '\n')
2315 {
2316 ea.nextcmd = p + 1;
2317 *p = NUL;
2318 break;
2319 }
2320 }
2321 }
2322
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002323 if ((ea.argt & EX_DFLALL) && ea.addr_count == 0)
Bram Moolenaarc2af0af2020-08-23 21:06:02 +02002324 address_default_all(&ea);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002325
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002326 // accept numbered register only when no count allowed (:put)
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002327 if ( (ea.argt & EX_REGSTR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002328 && *ea.arg != NUL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002329 // Do not allow register = for user commands
Bram Moolenaar958636c2014-10-21 20:01:58 +02002330 && (!IS_USER_CMDIDX(ea.cmdidx) || *ea.arg != '=')
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002331 && !((ea.argt & EX_COUNT) && VIM_ISDIGIT(*ea.arg)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002332 {
Bram Moolenaar85de2062011-05-05 14:26:41 +02002333#ifndef FEAT_CLIPBOARD
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002334 // check these explicitly for a more specific error message
Bram Moolenaar85de2062011-05-05 14:26:41 +02002335 if (*ea.arg == '*' || *ea.arg == '+')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002336 {
Bram Moolenaar99b12722019-01-14 20:16:40 +01002337 errormsg = _(e_invalidreg);
Bram Moolenaar85de2062011-05-05 14:26:41 +02002338 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002339 }
2340#endif
Bram Moolenaar958636c2014-10-21 20:01:58 +02002341 if (valid_yank_reg(*ea.arg, (ea.cmdidx != CMD_put
2342 && !IS_USER_CMDIDX(ea.cmdidx))))
Bram Moolenaar85de2062011-05-05 14:26:41 +02002343 {
2344 ea.regname = *ea.arg++;
2345#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002346 // for '=' register: accept the rest of the line as an expression
Bram Moolenaar85de2062011-05-05 14:26:41 +02002347 if (ea.arg[-1] == '=' && ea.arg[0] != NUL)
2348 {
Bram Moolenaarb4bcea42020-10-28 13:53:50 +01002349 set_expr_line(vim_strsave(ea.arg), &ea);
Bram Moolenaar85de2062011-05-05 14:26:41 +02002350 ea.arg += STRLEN(ea.arg);
Bram Moolenaarb4bcea42020-10-28 13:53:50 +01002351 did_set_expr_line = TRUE;
Bram Moolenaar85de2062011-05-05 14:26:41 +02002352 }
2353#endif
2354 ea.arg = skipwhite(ea.arg);
2355 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002356 }
2357
2358 /*
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002359 * Check for a count. When accepting a EX_BUFNAME, don't use "123foo" as a
Bram Moolenaar071d4272004-06-13 20:20:40 +00002360 * count, it's a buffer name.
2361 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002362 if ((ea.argt & EX_COUNT) && VIM_ISDIGIT(*ea.arg)
2363 && (!(ea.argt & EX_BUFNAME) || *(p = skipdigits(ea.arg)) == NUL
Bram Moolenaar1c465442017-03-12 20:10:05 +01002364 || VIM_ISWHITE(*p)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002365 {
2366 n = getdigits(&ea.arg);
2367 ea.arg = skipwhite(ea.arg);
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002368 if (n <= 0 && !ni && (ea.argt & EX_ZEROR) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002369 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002370 errormsg = _(e_zerocount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002371 goto doend;
2372 }
Bram Moolenaarb7316892019-05-01 18:08:42 +02002373 if (ea.addr_type != ADDR_LINES) // e.g. :buffer 2, :sleep 3
Bram Moolenaar071d4272004-06-13 20:20:40 +00002374 {
2375 ea.line2 = n;
2376 if (ea.addr_count == 0)
2377 ea.addr_count = 1;
2378 }
2379 else
2380 {
2381 ea.line1 = ea.line2;
2382 ea.line2 += n - 1;
2383 ++ea.addr_count;
2384 /*
2385 * Be vi compatible: no error message for out of range.
2386 */
Bram Moolenaarb7316892019-05-01 18:08:42 +02002387 if (ea.line2 > curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002388 ea.line2 = curbuf->b_ml.ml_line_count;
2389 }
2390 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00002391
2392 /*
2393 * Check for flags: 'l', 'p' and '#'.
2394 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002395 if (ea.argt & EX_FLAGS)
Bram Moolenaardf177f62005-02-22 08:39:57 +00002396 get_flags(&ea);
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002397 if (!ni && !(ea.argt & EX_EXTRA) && *ea.arg != NUL
2398 && *ea.arg != '"' && (*ea.arg != '|' || (ea.argt & EX_TRLBAR) == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002399 {
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002400 // no arguments allowed but there is something
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +02002401 errormsg = ex_errmsg(e_trailing_arg, ea.arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002402 goto doend;
2403 }
2404
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002405 if (!ni && (ea.argt & EX_NEEDARG) && *ea.arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002406 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002407 errormsg = _(e_argreq);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002408 goto doend;
2409 }
2410
2411#ifdef FEAT_EVAL
2412 /*
2413 * Skip the command when it's not going to be executed.
2414 * The commands like :if, :endif, etc. always need to be executed.
2415 * Also make an exception for commands that handle a trailing command
2416 * themselves.
2417 */
2418 if (ea.skip)
2419 {
2420 switch (ea.cmdidx)
2421 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002422 // commands that need evaluation
Bram Moolenaar071d4272004-06-13 20:20:40 +00002423 case CMD_while:
2424 case CMD_endwhile:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00002425 case CMD_for:
2426 case CMD_endfor:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002427 case CMD_if:
2428 case CMD_elseif:
2429 case CMD_else:
2430 case CMD_endif:
2431 case CMD_try:
2432 case CMD_catch:
2433 case CMD_finally:
2434 case CMD_endtry:
2435 case CMD_function:
Bram Moolenaarab55c682020-03-01 19:41:43 +01002436 case CMD_def:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002437 break;
2438
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002439 // Commands that handle '|' themselves. Check: A command should
2440 // either have the EX_TRLBAR flag, appear in this list or appear in
2441 // the list at ":help :bar".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002442 case CMD_aboveleft:
2443 case CMD_and:
2444 case CMD_belowright:
2445 case CMD_botright:
2446 case CMD_browse:
2447 case CMD_call:
2448 case CMD_confirm:
Bram Moolenaar8f76e6b2019-11-26 16:50:30 +01002449 case CMD_const:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002450 case CMD_delfunction:
2451 case CMD_djump:
2452 case CMD_dlist:
2453 case CMD_dsearch:
2454 case CMD_dsplit:
2455 case CMD_echo:
2456 case CMD_echoerr:
2457 case CMD_echomsg:
2458 case CMD_echon:
Bram Moolenaara76b3152020-02-16 16:20:21 +01002459 case CMD_eval:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002460 case CMD_execute:
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002461 case CMD_filter:
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002462 case CMD_final:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002463 case CMD_help:
2464 case CMD_hide:
2465 case CMD_ijump:
2466 case CMD_ilist:
2467 case CMD_isearch:
2468 case CMD_isplit:
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002469 case CMD_keepalt:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002470 case CMD_keepjumps:
2471 case CMD_keepmarks:
Bram Moolenaara939e432013-11-09 05:30:26 +01002472 case CMD_keeppatterns:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002473 case CMD_leftabove:
2474 case CMD_let:
2475 case CMD_lockmarks:
Bram Moolenaarcc4423a2019-11-26 17:05:00 +01002476 case CMD_lockvar:
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002477 case CMD_lua:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002478 case CMD_match:
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002479 case CMD_mzscheme:
Bram Moolenaar5803ae62014-03-23 16:04:02 +01002480 case CMD_noautocmd:
2481 case CMD_noswapfile:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002482 case CMD_perl:
2483 case CMD_psearch:
Bram Moolenaar368373e2010-07-19 20:46:22 +02002484 case CMD_py3:
Bram Moolenaarb6590522010-07-21 16:00:43 +02002485 case CMD_python3:
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002486 case CMD_python:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002487 case CMD_return:
2488 case CMD_rightbelow:
2489 case CMD_ruby:
2490 case CMD_silent:
2491 case CMD_smagic:
2492 case CMD_snomagic:
2493 case CMD_substitute:
2494 case CMD_syntax:
Bram Moolenaara226a6d2006-02-26 23:59:20 +00002495 case CMD_tab:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002496 case CMD_tcl:
2497 case CMD_throw:
2498 case CMD_tilde:
2499 case CMD_topleft:
2500 case CMD_unlet:
Bram Moolenaarcc4423a2019-11-26 17:05:00 +01002501 case CMD_unlockvar:
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002502 case CMD_var:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002503 case CMD_verbose:
2504 case CMD_vertical:
Bram Moolenaar12bde492011-06-13 01:19:56 +02002505 case CMD_wincmd:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002506 break;
2507
2508 default: goto doend;
2509 }
2510 }
2511#endif
2512
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002513 if (ea.argt & EX_XFILE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002514 {
2515 if (expand_filename(&ea, cmdlinep, &errormsg) == FAIL)
2516 goto doend;
2517 }
2518
Bram Moolenaar071d4272004-06-13 20:20:40 +00002519 /*
2520 * Accept buffer name. Cannot be used at the same time with a buffer
2521 * number. Don't do this for a user command.
2522 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002523 if ((ea.argt & EX_BUFNAME) && *ea.arg != NUL && ea.addr_count == 0
Bram Moolenaar958636c2014-10-21 20:01:58 +02002524 && !IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002525 {
2526 /*
2527 * :bdelete, :bwipeout and :bunload take several arguments, separated
2528 * by spaces: find next space (skipping over escaped characters).
2529 * The others take one argument: ignore trailing spaces.
2530 */
2531 if (ea.cmdidx == CMD_bdelete || ea.cmdidx == CMD_bwipeout
2532 || ea.cmdidx == CMD_bunload)
2533 p = skiptowhite_esc(ea.arg);
2534 else
2535 {
2536 p = ea.arg + STRLEN(ea.arg);
Bram Moolenaar1c465442017-03-12 20:10:05 +01002537 while (p > ea.arg && VIM_ISWHITE(p[-1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002538 --p;
2539 }
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002540 ea.line2 = buflist_findpat(ea.arg, p, (ea.argt & EX_BUFUNL) != 0,
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002541 FALSE, FALSE);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002542 if (ea.line2 < 0) // failed
Bram Moolenaar071d4272004-06-13 20:20:40 +00002543 goto doend;
2544 ea.addr_count = 1;
2545 ea.arg = skipwhite(p);
2546 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002547
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002548 // The :try command saves the emsg_silent flag, reset it here when
2549 // ":silent! try" was used, it should only apply to :try itself.
Bram Moolenaar5661ed62020-10-24 17:19:16 +02002550 if (ea.cmdidx == CMD_try && cmdmod.cmod_did_esilent > 0)
Bram Moolenaar2be57332018-02-13 18:05:18 +01002551 {
Bram Moolenaar5661ed62020-10-24 17:19:16 +02002552 emsg_silent -= cmdmod.cmod_did_esilent;
Bram Moolenaar2be57332018-02-13 18:05:18 +01002553 if (emsg_silent < 0)
2554 emsg_silent = 0;
Bram Moolenaar5661ed62020-10-24 17:19:16 +02002555 cmdmod.cmod_did_esilent = 0;
Bram Moolenaar2be57332018-02-13 18:05:18 +01002556 }
2557
Bram Moolenaar071d4272004-06-13 20:20:40 +00002558/*
Bram Moolenaar2be57332018-02-13 18:05:18 +01002559 * 7. Execute the command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002560 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002561
Bram Moolenaar958636c2014-10-21 20:01:58 +02002562 if (IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002563 {
2564 /*
2565 * Execute a user-defined command.
2566 */
2567 do_ucmd(&ea);
2568 }
2569 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002570 {
2571 /*
Bram Moolenaarac9fb182019-04-27 13:04:13 +02002572 * Call the function to execute the builtin command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002573 */
2574 ea.errmsg = NULL;
2575 (cmdnames[ea.cmdidx].cmd_func)(&ea);
2576 if (ea.errmsg != NULL)
Bram Moolenaar8930caa2020-07-23 16:37:03 +02002577 errormsg = ea.errmsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002578 }
2579
2580#ifdef FEAT_EVAL
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002581 // Set flag that any command was executed, used by ex_vim9script().
Bram Moolenaar16531552020-02-08 16:00:46 +01002582 if (getline_equal(ea.getline, ea.cookie, getsourceline)
2583 && current_sctx.sc_sid > 0)
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002584 SCRIPT_ITEM(current_sctx.sc_sid)->sn_had_command = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002585
Bram Moolenaar071d4272004-06-13 20:20:40 +00002586 /*
2587 * If the command just executed called do_cmdline(), any throw or ":return"
2588 * or ":finish" encountered there must also check the cstack of the still
2589 * active do_cmdline() that called this do_one_cmd(). Rethrow an uncaught
2590 * exception, or reanimate a returned function or finished script file and
2591 * return or finish it again.
2592 */
2593 if (need_rethrow)
2594 do_throw(cstack);
2595 else if (check_cstack)
2596 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002597 if (source_finished(fgetline, cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002598 do_finish(&ea, TRUE);
Bram Moolenaar89d40322006-08-29 15:30:07 +00002599 else if (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002600 && current_func_returned())
2601 do_return(&ea, TRUE, FALSE, NULL);
2602 }
2603 need_rethrow = check_cstack = FALSE;
2604#endif
2605
2606doend:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002607 if (curwin->w_cursor.lnum == 0) // can happen with zero line number
Bram Moolenaar6de5e122017-04-20 21:55:44 +02002608 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002609 curwin->w_cursor.lnum = 1;
Bram Moolenaar6de5e122017-04-20 21:55:44 +02002610 curwin->w_cursor.col = 0;
2611 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002612
2613 if (errormsg != NULL && *errormsg != NUL && !did_emsg)
2614 {
2615 if (sourcing)
2616 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002617 if (errormsg != (char *)IObuff)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002618 {
2619 STRCPY(IObuff, errormsg);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002620 errormsg = (char *)IObuff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002621 }
Bram Moolenaara6f4d612011-09-21 19:10:46 +02002622 append_command(*cmdlinep);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002623 }
2624 emsg(errormsg);
2625 }
2626#ifdef FEAT_EVAL
2627 do_errthrow(cstack,
Bram Moolenaar958636c2014-10-21 20:01:58 +02002628 (ea.cmdidx != CMD_SIZE && !IS_USER_CMDIDX(ea.cmdidx))
2629 ? cmdnames[(int)ea.cmdidx].cmd_name : (char_u *)NULL);
Bram Moolenaarb4bcea42020-10-28 13:53:50 +01002630
2631 if (did_set_expr_line)
2632 set_expr_line(NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002633#endif
2634
Bram Moolenaare1004402020-10-24 20:49:43 +02002635 undo_cmdmod(&cmdmod);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002636 cmdmod = save_cmdmod;
Bram Moolenaar9a2c0912019-03-30 14:26:18 +01002637 reg_executing = save_reg_executing;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002638
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002639 if (ea.nextcmd && *ea.nextcmd == NUL) // not really a next command
Bram Moolenaar071d4272004-06-13 20:20:40 +00002640 ea.nextcmd = NULL;
2641
2642#ifdef FEAT_EVAL
2643 --ex_nesting_level;
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002644 vim_free(ea.cmdline_tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002645#endif
2646
2647 return ea.nextcmd;
2648}
2649#if (_MSC_VER == 1200)
Bram Moolenaar281bdce2005-01-25 21:53:18 +00002650 #pragma optimize( "", on )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002651#endif
2652
Bram Moolenaar8930caa2020-07-23 16:37:03 +02002653static char ex_error_buf[MSG_BUF_LEN];
2654
2655/*
2656 * Return an error message with argument included.
2657 * Uses a static buffer, only the last error will be kept.
2658 * "msg" will be translated, caller should use N_().
2659 */
2660 char *
2661ex_errmsg(char *msg, char_u *arg)
2662{
2663 vim_snprintf(ex_error_buf, MSG_BUF_LEN, _(msg), arg);
2664 return ex_error_buf;
2665}
2666
Bram Moolenaar071d4272004-06-13 20:20:40 +00002667/*
Bram Moolenaareffed932018-08-14 13:38:17 +02002668 * Parse and skip over command modifiers:
2669 * - update eap->cmd
Bram Moolenaare1004402020-10-24 20:49:43 +02002670 * - store flags in "cmod".
Bram Moolenaareffed932018-08-14 13:38:17 +02002671 * - Set ex_pressedreturn for an empty command line.
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002672 * When "skip_only" is TRUE the global variables are not changed, except for
2673 * "cmdmod".
Bram Moolenaare1004402020-10-24 20:49:43 +02002674 * When "skip_only" is FALSE then undo_cmdmod() must be called later to free
2675 * any cmod_filter_regmatch.regprog.
Bram Moolenaar5661ed62020-10-24 17:19:16 +02002676 * Call apply_cmdmod() to get the side effects of the modifiers:
2677 * - Increment "sandbox" for ":sandbox"
2678 * - set p_verbose for ":verbose"
Bram Moolenaare1004402020-10-24 20:49:43 +02002679 * - set msg_silent for ":silent"
2680 * - set 'eventignore' to "all" for ":noautocmd"
Bram Moolenaareffed932018-08-14 13:38:17 +02002681 * Return FAIL when the command is not to be executed.
2682 * May set "errormsg" to an error message.
2683 */
2684 int
Bram Moolenaare1004402020-10-24 20:49:43 +02002685parse_command_modifiers(
2686 exarg_T *eap,
2687 char **errormsg,
2688 cmdmod_T *cmod,
2689 int skip_only)
Bram Moolenaareffed932018-08-14 13:38:17 +02002690{
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002691 char_u *p;
2692 int starts_with_colon = FALSE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002693
Bram Moolenaare1004402020-10-24 20:49:43 +02002694 CLEAR_POINTER(cmod);
Bram Moolenaareffed932018-08-14 13:38:17 +02002695
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002696 // Repeat until no more command modifiers are found.
Bram Moolenaareffed932018-08-14 13:38:17 +02002697 for (;;)
2698 {
Bram Moolenaareffed932018-08-14 13:38:17 +02002699 while (*eap->cmd == ' ' || *eap->cmd == '\t' || *eap->cmd == ':')
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002700 {
2701 if (*eap->cmd == ':')
2702 starts_with_colon = TRUE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002703 ++eap->cmd;
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002704 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002705
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002706 // in ex mode, an empty line works like :+
Bram Moolenaareffed932018-08-14 13:38:17 +02002707 if (*eap->cmd == NUL && exmode_active
2708 && (getline_equal(eap->getline, eap->cookie, getexmodeline)
2709 || getline_equal(eap->getline, eap->cookie, getexline))
2710 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
2711 {
2712 eap->cmd = (char_u *)"+";
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002713 if (!skip_only)
2714 ex_pressedreturn = TRUE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002715 }
2716
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002717 // ignore comment and empty lines
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02002718 if (comment_start(eap->cmd, starts_with_colon))
Bram Moolenaareffed932018-08-14 13:38:17 +02002719 return FAIL;
2720 if (*eap->cmd == NUL)
2721 {
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002722 if (!skip_only)
2723 ex_pressedreturn = TRUE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002724 return FAIL;
2725 }
2726
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02002727 p = skip_range(eap->cmd, TRUE, NULL);
Bram Moolenaareffed932018-08-14 13:38:17 +02002728 switch (*p)
2729 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002730 // When adding an entry, also modify cmd_exists().
Bram Moolenaareffed932018-08-14 13:38:17 +02002731 case 'a': if (!checkforcmd(&eap->cmd, "aboveleft", 3))
2732 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02002733 cmod->cmod_split |= WSP_ABOVE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002734 continue;
2735
2736 case 'b': if (checkforcmd(&eap->cmd, "belowright", 3))
2737 {
Bram Moolenaare1004402020-10-24 20:49:43 +02002738 cmod->cmod_split |= WSP_BELOW;
Bram Moolenaareffed932018-08-14 13:38:17 +02002739 continue;
2740 }
2741 if (checkforcmd(&eap->cmd, "browse", 3))
2742 {
2743#ifdef FEAT_BROWSE_CMD
Bram Moolenaare1004402020-10-24 20:49:43 +02002744 cmod->cmod_flags |= CMOD_BROWSE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002745#endif
2746 continue;
2747 }
2748 if (!checkforcmd(&eap->cmd, "botright", 2))
2749 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02002750 cmod->cmod_split |= WSP_BOT;
Bram Moolenaareffed932018-08-14 13:38:17 +02002751 continue;
2752
2753 case 'c': if (!checkforcmd(&eap->cmd, "confirm", 4))
2754 break;
2755#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaare1004402020-10-24 20:49:43 +02002756 cmod->cmod_flags |= CMOD_CONFIRM;
Bram Moolenaareffed932018-08-14 13:38:17 +02002757#endif
2758 continue;
2759
2760 case 'k': if (checkforcmd(&eap->cmd, "keepmarks", 3))
2761 {
Bram Moolenaare1004402020-10-24 20:49:43 +02002762 cmod->cmod_flags |= CMOD_KEEPMARKS;
Bram Moolenaareffed932018-08-14 13:38:17 +02002763 continue;
2764 }
2765 if (checkforcmd(&eap->cmd, "keepalt", 5))
2766 {
Bram Moolenaare1004402020-10-24 20:49:43 +02002767 cmod->cmod_flags |= CMOD_KEEPALT;
Bram Moolenaareffed932018-08-14 13:38:17 +02002768 continue;
2769 }
2770 if (checkforcmd(&eap->cmd, "keeppatterns", 5))
2771 {
Bram Moolenaare1004402020-10-24 20:49:43 +02002772 cmod->cmod_flags |= CMOD_KEEPPATTERNS;
Bram Moolenaareffed932018-08-14 13:38:17 +02002773 continue;
2774 }
2775 if (!checkforcmd(&eap->cmd, "keepjumps", 5))
2776 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02002777 cmod->cmod_flags |= CMOD_KEEPJUMPS;
Bram Moolenaareffed932018-08-14 13:38:17 +02002778 continue;
2779
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002780 case 'f': // only accept ":filter {pat} cmd"
Bram Moolenaareffed932018-08-14 13:38:17 +02002781 {
2782 char_u *reg_pat;
2783
2784 if (!checkforcmd(&p, "filter", 4)
2785 || *p == NUL || ends_excmd(*p))
2786 break;
2787 if (*p == '!')
2788 {
Bram Moolenaare1004402020-10-24 20:49:43 +02002789 cmod->cmod_filter_force = TRUE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002790 p = skipwhite(p + 1);
2791 if (*p == NUL || ends_excmd(*p))
2792 break;
2793 }
Bram Moolenaar1e624c92020-07-11 14:08:04 +02002794#ifdef FEAT_EVAL
Bram Moolenaarb074e8b2020-07-11 13:40:45 +02002795 // Avoid that "filter(arg)" is recognized.
Bram Moolenaar4f6b6ed2020-10-29 20:24:34 +01002796 if (in_vim9script() && !VIM_ISWHITE(p[-1]))
Bram Moolenaarb074e8b2020-07-11 13:40:45 +02002797 break;
Bram Moolenaar1e624c92020-07-11 14:08:04 +02002798#endif
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002799 if (skip_only)
2800 p = skip_vimgrep_pat(p, NULL, NULL);
2801 else
2802 // NOTE: This puts a NUL after the pattern.
2803 p = skip_vimgrep_pat(p, &reg_pat, NULL);
Bram Moolenaareffed932018-08-14 13:38:17 +02002804 if (p == NULL || *p == NUL)
2805 break;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002806 if (!skip_only)
2807 {
Bram Moolenaare1004402020-10-24 20:49:43 +02002808 cmod->cmod_filter_regmatch.regprog =
Bram Moolenaareffed932018-08-14 13:38:17 +02002809 vim_regcomp(reg_pat, RE_MAGIC);
Bram Moolenaare1004402020-10-24 20:49:43 +02002810 if (cmod->cmod_filter_regmatch.regprog == NULL)
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002811 break;
2812 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002813 eap->cmd = p;
2814 continue;
2815 }
2816
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002817 // ":hide" and ":hide | cmd" are not modifiers
Bram Moolenaareffed932018-08-14 13:38:17 +02002818 case 'h': if (p != eap->cmd || !checkforcmd(&p, "hide", 3)
2819 || *p == NUL || ends_excmd(*p))
2820 break;
2821 eap->cmd = p;
Bram Moolenaare1004402020-10-24 20:49:43 +02002822 cmod->cmod_flags |= CMOD_HIDE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002823 continue;
2824
2825 case 'l': if (checkforcmd(&eap->cmd, "lockmarks", 3))
2826 {
Bram Moolenaare1004402020-10-24 20:49:43 +02002827 cmod->cmod_flags |= CMOD_LOCKMARKS;
Bram Moolenaareffed932018-08-14 13:38:17 +02002828 continue;
2829 }
2830
2831 if (!checkforcmd(&eap->cmd, "leftabove", 5))
2832 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02002833 cmod->cmod_split |= WSP_ABOVE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002834 continue;
2835
2836 case 'n': if (checkforcmd(&eap->cmd, "noautocmd", 3))
2837 {
Bram Moolenaare1004402020-10-24 20:49:43 +02002838 cmod->cmod_flags |= CMOD_NOAUTOCMD;
Bram Moolenaareffed932018-08-14 13:38:17 +02002839 continue;
2840 }
2841 if (!checkforcmd(&eap->cmd, "noswapfile", 3))
2842 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02002843 cmod->cmod_flags |= CMOD_NOSWAPFILE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002844 continue;
2845
2846 case 'r': if (!checkforcmd(&eap->cmd, "rightbelow", 6))
2847 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02002848 cmod->cmod_split |= WSP_BELOW;
Bram Moolenaareffed932018-08-14 13:38:17 +02002849 continue;
2850
2851 case 's': if (checkforcmd(&eap->cmd, "sandbox", 3))
2852 {
Bram Moolenaare1004402020-10-24 20:49:43 +02002853 cmod->cmod_flags |= CMOD_SANDBOX;
Bram Moolenaareffed932018-08-14 13:38:17 +02002854 continue;
2855 }
2856 if (!checkforcmd(&eap->cmd, "silent", 3))
2857 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02002858 cmod->cmod_flags |= CMOD_SILENT;
Bram Moolenaareffed932018-08-14 13:38:17 +02002859 if (*eap->cmd == '!' && !VIM_ISWHITE(eap->cmd[-1]))
2860 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002861 // ":silent!", but not "silent !cmd"
Bram Moolenaareffed932018-08-14 13:38:17 +02002862 eap->cmd = skipwhite(eap->cmd + 1);
Bram Moolenaare1004402020-10-24 20:49:43 +02002863 cmod->cmod_flags |= CMOD_ERRSILENT;
Bram Moolenaareffed932018-08-14 13:38:17 +02002864 }
2865 continue;
2866
2867 case 't': if (checkforcmd(&p, "tab", 3))
2868 {
Bram Moolenaar548e5982018-12-26 21:45:00 +01002869 if (!skip_only)
Bram Moolenaareffed932018-08-14 13:38:17 +02002870 {
Bram Moolenaar548e5982018-12-26 21:45:00 +01002871 long tabnr = get_address(eap, &eap->cmd,
2872 ADDR_TABS, eap->skip,
2873 skip_only, FALSE, 1);
2874 if (tabnr == MAXLNUM)
Bram Moolenaare1004402020-10-24 20:49:43 +02002875 cmod->cmod_tab = tabpage_index(curtab) + 1;
Bram Moolenaar548e5982018-12-26 21:45:00 +01002876 else
Bram Moolenaareffed932018-08-14 13:38:17 +02002877 {
Bram Moolenaar548e5982018-12-26 21:45:00 +01002878 if (tabnr < 0 || tabnr > LAST_TAB_NR)
2879 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002880 *errormsg = _(e_invrange);
Bram Moolenaar548e5982018-12-26 21:45:00 +01002881 return FAIL;
2882 }
Bram Moolenaare1004402020-10-24 20:49:43 +02002883 cmod->cmod_tab = tabnr + 1;
Bram Moolenaareffed932018-08-14 13:38:17 +02002884 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002885 }
2886 eap->cmd = p;
2887 continue;
2888 }
2889 if (!checkforcmd(&eap->cmd, "topleft", 2))
2890 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02002891 cmod->cmod_split |= WSP_TOP;
Bram Moolenaareffed932018-08-14 13:38:17 +02002892 continue;
2893
2894 case 'u': if (!checkforcmd(&eap->cmd, "unsilent", 3))
2895 break;
Bram Moolenaare1004402020-10-24 20:49:43 +02002896 cmod->cmod_flags |= CMOD_UNSILENT;
Bram Moolenaareffed932018-08-14 13:38:17 +02002897 continue;
2898
2899 case 'v': if (checkforcmd(&eap->cmd, "vertical", 4))
2900 {
Bram Moolenaare1004402020-10-24 20:49:43 +02002901 cmod->cmod_split |= WSP_VERT;
Bram Moolenaareffed932018-08-14 13:38:17 +02002902 continue;
2903 }
2904 if (!checkforcmd(&p, "verbose", 4))
2905 break;
Bram Moolenaar5661ed62020-10-24 17:19:16 +02002906 if (vim_isdigit(*eap->cmd))
Bram Moolenaare1004402020-10-24 20:49:43 +02002907 cmod->cmod_verbose = atoi((char *)eap->cmd);
Bram Moolenaar5661ed62020-10-24 17:19:16 +02002908 else
Bram Moolenaare1004402020-10-24 20:49:43 +02002909 cmod->cmod_verbose = 1;
Bram Moolenaareffed932018-08-14 13:38:17 +02002910 eap->cmd = p;
2911 continue;
2912 }
2913 break;
2914 }
2915
2916 return OK;
2917}
2918
2919/*
Bram Moolenaar5661ed62020-10-24 17:19:16 +02002920 * Apply the command modifiers. Saves current state in "cmdmod", call
2921 * undo_cmdmod() later.
2922 */
2923 void
2924apply_cmdmod(cmdmod_T *cmod)
2925{
2926#ifdef HAVE_SANDBOX
2927 if ((cmod->cmod_flags & CMOD_SANDBOX) && !cmod->cmod_did_sandbox)
2928 {
2929 ++sandbox;
2930 cmod->cmod_did_sandbox = TRUE;
2931 }
2932#endif
2933 if (cmod->cmod_verbose > 0)
2934 {
2935 if (cmod->cmod_verbose_save == 0)
2936 cmod->cmod_verbose_save = p_verbose + 1;
2937 p_verbose = cmod->cmod_verbose;
2938 }
2939
2940 if ((cmod->cmod_flags & (CMOD_SILENT | CMOD_UNSILENT))
2941 && cmod->cmod_save_msg_silent == 0)
Bram Moolenaare1004402020-10-24 20:49:43 +02002942 {
Bram Moolenaar5661ed62020-10-24 17:19:16 +02002943 cmod->cmod_save_msg_silent = msg_silent + 1;
Bram Moolenaare1004402020-10-24 20:49:43 +02002944 cmod->cmod_save_msg_scroll = msg_scroll;
2945 }
Bram Moolenaar5661ed62020-10-24 17:19:16 +02002946 if (cmod->cmod_flags & CMOD_SILENT)
2947 ++msg_silent;
2948 if (cmod->cmod_flags & CMOD_UNSILENT)
2949 msg_silent = 0;
2950
2951 if (cmod->cmod_flags & CMOD_ERRSILENT)
2952 {
2953 ++emsg_silent;
2954 ++cmod->cmod_did_esilent;
2955 }
2956
Bram Moolenaare1004402020-10-24 20:49:43 +02002957 if ((cmod->cmod_flags & CMOD_NOAUTOCMD) && cmod->cmod_save_ei == NULL)
Bram Moolenaar5661ed62020-10-24 17:19:16 +02002958 {
2959 // Set 'eventignore' to "all".
2960 // First save the existing option value for restoring it later.
Bram Moolenaare1004402020-10-24 20:49:43 +02002961 cmod->cmod_save_ei = vim_strsave(p_ei);
Bram Moolenaar5661ed62020-10-24 17:19:16 +02002962 set_string_option_direct((char_u *)"ei", -1,
2963 (char_u *)"all", OPT_FREE, SID_NONE);
2964 }
2965}
2966
2967/*
Bram Moolenaare1004402020-10-24 20:49:43 +02002968 * Undo and free contents of "cmod".
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002969 */
Bram Moolenaar47e7d702020-07-05 18:18:42 +02002970 void
Bram Moolenaare1004402020-10-24 20:49:43 +02002971undo_cmdmod(cmdmod_T *cmod)
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002972{
Bram Moolenaare1004402020-10-24 20:49:43 +02002973 if (cmod->cmod_verbose_save > 0)
Bram Moolenaar5661ed62020-10-24 17:19:16 +02002974 {
Bram Moolenaare1004402020-10-24 20:49:43 +02002975 p_verbose = cmod->cmod_verbose_save - 1;
2976 cmod->cmod_verbose_save = 0;
Bram Moolenaar5661ed62020-10-24 17:19:16 +02002977 }
Bram Moolenaar47e7d702020-07-05 18:18:42 +02002978
Bram Moolenaar5661ed62020-10-24 17:19:16 +02002979#ifdef HAVE_SANDBOX
Bram Moolenaare1004402020-10-24 20:49:43 +02002980 if (cmod->cmod_did_sandbox)
Bram Moolenaar5661ed62020-10-24 17:19:16 +02002981 {
2982 --sandbox;
Bram Moolenaare1004402020-10-24 20:49:43 +02002983 cmod->cmod_did_sandbox = FALSE;
Bram Moolenaar5661ed62020-10-24 17:19:16 +02002984 }
2985#endif
2986
Bram Moolenaare1004402020-10-24 20:49:43 +02002987 if (cmod->cmod_save_ei != NULL)
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002988 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002989 // Restore 'eventignore' to the value before ":noautocmd".
Bram Moolenaare1004402020-10-24 20:49:43 +02002990 set_string_option_direct((char_u *)"ei", -1, cmod->cmod_save_ei,
2991 OPT_FREE, SID_NONE);
2992 free_string_option(cmod->cmod_save_ei);
2993 cmod->cmod_save_ei = NULL;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002994 }
2995
Bram Moolenaarcaf73dc2020-10-26 21:39:13 +01002996 vim_regfree(cmod->cmod_filter_regmatch.regprog);
Bram Moolenaar47e7d702020-07-05 18:18:42 +02002997
Bram Moolenaare1004402020-10-24 20:49:43 +02002998 if (cmod->cmod_save_msg_silent > 0)
Bram Moolenaar47e7d702020-07-05 18:18:42 +02002999 {
3000 // messages could be enabled for a serious error, need to check if the
3001 // counters don't become negative
Bram Moolenaare1004402020-10-24 20:49:43 +02003002 if (!did_emsg || msg_silent > cmod->cmod_save_msg_silent - 1)
3003 msg_silent = cmod->cmod_save_msg_silent - 1;
3004 emsg_silent -= cmod->cmod_did_esilent;
Bram Moolenaar47e7d702020-07-05 18:18:42 +02003005 if (emsg_silent < 0)
3006 emsg_silent = 0;
3007 // Restore msg_scroll, it's set by file I/O commands, even when no
3008 // message is actually displayed.
Bram Moolenaare1004402020-10-24 20:49:43 +02003009 msg_scroll = cmod->cmod_save_msg_scroll;
Bram Moolenaar47e7d702020-07-05 18:18:42 +02003010
3011 // "silent reg" or "silent echo x" inside "redir" leaves msg_col
3012 // somewhere in the line. Put it back in the first column.
3013 if (redirecting())
3014 msg_col = 0;
Bram Moolenaar5661ed62020-10-24 17:19:16 +02003015
Bram Moolenaare1004402020-10-24 20:49:43 +02003016 cmod->cmod_save_msg_silent = 0;
3017 cmod->cmod_did_esilent = 0;
Bram Moolenaar47e7d702020-07-05 18:18:42 +02003018 }
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02003019}
3020
3021/*
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003022 * Parse the address range, if any, in "eap".
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02003023 * May set the last search pattern, unless "silent" is TRUE.
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003024 * Return FAIL and set "errormsg" or return OK.
3025 */
3026 int
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003027parse_cmd_address(exarg_T *eap, char **errormsg, int silent)
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003028{
3029 int address_count = 1;
3030 linenr_T lnum;
3031
3032 // Repeat for all ',' or ';' separated addresses.
3033 for (;;)
3034 {
3035 eap->line1 = eap->line2;
Bram Moolenaarc2af0af2020-08-23 21:06:02 +02003036 eap->line2 = default_address(eap);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003037 eap->cmd = skipwhite(eap->cmd);
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02003038 lnum = get_address(eap, &eap->cmd, eap->addr_type, eap->skip, silent,
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003039 eap->addr_count == 0, address_count++);
3040 if (eap->cmd == NULL) // error detected
3041 return FAIL;
3042 if (lnum == MAXLNUM)
3043 {
3044 if (*eap->cmd == '%') // '%' - all lines
3045 {
3046 ++eap->cmd;
3047 switch (eap->addr_type)
3048 {
3049 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02003050 case ADDR_OTHER:
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003051 eap->line1 = 1;
3052 eap->line2 = curbuf->b_ml.ml_line_count;
3053 break;
3054 case ADDR_LOADED_BUFFERS:
3055 {
3056 buf_T *buf = firstbuf;
3057
3058 while (buf->b_next != NULL
3059 && buf->b_ml.ml_mfp == NULL)
3060 buf = buf->b_next;
3061 eap->line1 = buf->b_fnum;
3062 buf = lastbuf;
3063 while (buf->b_prev != NULL
3064 && buf->b_ml.ml_mfp == NULL)
3065 buf = buf->b_prev;
3066 eap->line2 = buf->b_fnum;
3067 break;
3068 }
3069 case ADDR_BUFFERS:
3070 eap->line1 = firstbuf->b_fnum;
3071 eap->line2 = lastbuf->b_fnum;
3072 break;
3073 case ADDR_WINDOWS:
3074 case ADDR_TABS:
3075 if (IS_USER_CMDIDX(eap->cmdidx))
3076 {
3077 eap->line1 = 1;
3078 eap->line2 = eap->addr_type == ADDR_WINDOWS
3079 ? LAST_WIN_NR : LAST_TAB_NR;
3080 }
3081 else
3082 {
3083 // there is no Vim command which uses '%' and
3084 // ADDR_WINDOWS or ADDR_TABS
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003085 *errormsg = _(e_invrange);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003086 return FAIL;
3087 }
3088 break;
3089 case ADDR_TABS_RELATIVE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02003090 case ADDR_UNSIGNED:
3091 case ADDR_QUICKFIX:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003092 *errormsg = _(e_invrange);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003093 return FAIL;
3094 case ADDR_ARGUMENTS:
3095 if (ARGCOUNT == 0)
3096 eap->line1 = eap->line2 = 0;
3097 else
3098 {
3099 eap->line1 = 1;
3100 eap->line2 = ARGCOUNT;
3101 }
3102 break;
Bram Moolenaar25190db2019-05-04 15:05:28 +02003103 case ADDR_QUICKFIX_VALID:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003104#ifdef FEAT_QUICKFIX
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003105 eap->line1 = 1;
Bram Moolenaar25190db2019-05-04 15:05:28 +02003106 eap->line2 = qf_get_valid_size(eap);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003107 if (eap->line2 == 0)
3108 eap->line2 = 1;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003109#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003110 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02003111 case ADDR_NONE:
3112 // Will give an error later if a range is found.
3113 break;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003114 }
3115 ++eap->addr_count;
3116 }
3117 else if (*eap->cmd == '*' && vim_strchr(p_cpo, CPO_STAR) == NULL)
3118 {
3119 pos_T *fp;
3120
3121 // '*' - visual area
3122 if (eap->addr_type != ADDR_LINES)
3123 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003124 *errormsg = _(e_invrange);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003125 return FAIL;
3126 }
3127
3128 ++eap->cmd;
3129 if (!eap->skip)
3130 {
3131 fp = getmark('<', FALSE);
3132 if (check_mark(fp) == FAIL)
3133 return FAIL;
3134 eap->line1 = fp->lnum;
3135 fp = getmark('>', FALSE);
3136 if (check_mark(fp) == FAIL)
3137 return FAIL;
3138 eap->line2 = fp->lnum;
3139 ++eap->addr_count;
3140 }
3141 }
3142 }
3143 else
3144 eap->line2 = lnum;
3145 eap->addr_count++;
3146
3147 if (*eap->cmd == ';')
3148 {
3149 if (!eap->skip)
3150 {
3151 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaar0e717042020-04-27 19:29:01 +02003152 // Don't leave the cursor on an illegal line or column, but do
3153 // accept zero as address, so 0;/PATTERN/ works correctly.
3154 if (eap->line2 > 0)
3155 check_cursor();
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003156 }
3157 }
3158 else if (*eap->cmd != ',')
3159 break;
3160 ++eap->cmd;
3161 }
3162
3163 // One address given: set start and end lines.
3164 if (eap->addr_count == 1)
3165 {
3166 eap->line1 = eap->line2;
3167 // ... but only implicit: really no address given
3168 if (lnum == MAXLNUM)
3169 eap->addr_count = 0;
3170 }
3171 return OK;
3172}
3173
3174/*
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00003175 * Check for an Ex command with optional tail.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003176 * If there is a match advance "pp" to the argument and return TRUE.
3177 */
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00003178 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003179checkforcmd(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003180 char_u **pp, // start of command
3181 char *cmd, // name of command
3182 int len) // required length
Bram Moolenaar071d4272004-06-13 20:20:40 +00003183{
3184 int i;
3185
3186 for (i = 0; cmd[i] != NUL; ++i)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003187 if (((char_u *)cmd)[i] != (*pp)[i])
Bram Moolenaar071d4272004-06-13 20:20:40 +00003188 break;
Bram Moolenaar7a66a172020-10-16 19:56:12 +02003189 if (i >= len && !isalpha((*pp)[i]) && (*pp)[i] != '_')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003190 {
3191 *pp = skipwhite(*pp + i);
3192 return TRUE;
3193 }
3194 return FALSE;
3195}
3196
3197/*
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003198 * Append "cmd" to the error message in IObuff.
3199 * Takes care of limiting the length and handling 0xa0, which would be
3200 * invisible otherwise.
3201 */
3202 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003203append_command(char_u *cmd)
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003204{
3205 char_u *s = cmd;
3206 char_u *d;
3207
3208 STRCAT(IObuff, ": ");
3209 d = IObuff + STRLEN(IObuff);
3210 while (*s != NUL && d - IObuff < IOSIZE - 7)
3211 {
Bram Moolenaar13505972019-01-24 15:04:48 +01003212 if (enc_utf8 ? (s[0] == 0xc2 && s[1] == 0xa0) : *s == 0xa0)
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003213 {
Bram Moolenaar13505972019-01-24 15:04:48 +01003214 s += enc_utf8 ? 2 : 1;
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003215 STRCPY(d, "<a0>");
3216 d += 4;
3217 }
3218 else
3219 MB_COPY_CHAR(s, d);
3220 }
3221 *d = NUL;
3222}
3223
3224/*
Bram Moolenaar2e800952020-08-23 19:34:48 +02003225 * If "start" points "&opt", "&l:opt", "&g:opt" or "$ENV" return a pointer to
3226 * the name. Otherwise just return "start".
3227 */
3228 char_u *
3229skip_option_env_lead(char_u *start)
3230{
3231 char_u *name = start;
3232
3233 if (*start == '&')
3234 {
3235 if ((start[1] == 'l' || start[1] == 'g') && start[2] == ':')
3236 name += 3;
3237 else
3238 name += 1;
3239 }
3240 else if (*start == '$')
3241 name += 1;
3242 return name;
3243}
3244
3245/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003246 * Find an Ex command by its name, either built-in or user.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003247 * Start of the name can be found at eap->cmd.
Bram Moolenaar25190db2019-05-04 15:05:28 +02003248 * Sets eap->cmdidx and returns a pointer to char after the command name.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003249 * "full" is set to TRUE if the whole command name matched.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003250 *
3251 * If "lookup" is not NULL recognize expression without "eval" or "call" and
3252 * assignment without "let". Sets eap->cmdidx to the command while returning
3253 * "eap->cmd".
3254 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00003255 * Returns NULL for an ambiguous user command.
3256 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003257 char_u *
3258find_ex_command(
3259 exarg_T *eap,
Bram Moolenaara494f562020-04-18 17:45:38 +02003260 int *full UNUSED,
Bram Moolenaarb84a3812020-05-01 15:44:29 +02003261 void *(*lookup)(char_u *, size_t, cctx_T *) UNUSED,
Bram Moolenaara494f562020-04-18 17:45:38 +02003262 cctx_T *cctx UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003263{
3264 int len;
3265 char_u *p;
Bram Moolenaardf177f62005-02-22 08:39:57 +00003266 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003267
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003268#ifdef FEAT_EVAL
3269 /*
3270 * Recognize a Vim9 script function/method call and assignment:
3271 * "lvar = value", "lvar(arg)", "[1, 2 3]->Func()"
3272 */
Bram Moolenaar0c6ceaf2020-02-22 18:36:32 +01003273 p = eap->cmd;
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003274 if (lookup != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003275 {
Bram Moolenaar2e800952020-08-23 19:34:48 +02003276 char_u *pskip = skip_option_env_lead(eap->cmd);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003277
Bram Moolenaar83144542020-08-02 20:40:43 +02003278 if (vim_strchr((char_u *)"{('[\"@", *p) != NULL
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003279 || ((p = to_name_const_end(pskip)) > eap->cmd && *p != NUL))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003280 {
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003281 int oplen;
3282 int heredoc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003283
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003284 if (
3285 // "(..." is an expression.
3286 // "funcname(" is always a function call.
3287 *p == '('
3288 || (p == eap->cmd
3289 ? (
Bram Moolenaar9becdf22020-10-10 21:33:48 +02003290 // "{..." is a dict expression or block start.
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003291 *eap->cmd == '{'
3292 // "'string'->func()" is an expression.
3293 || *eap->cmd == '\''
Bram Moolenaar10409562020-07-29 20:00:38 +02003294 // '"string"->func()' is an expression.
3295 || *eap->cmd == '"'
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003296 // "g:varname" is an expression.
3297 || eap->cmd[1] == ':'
3298 )
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003299 // "varname->func()" is an expression.
Bram Moolenaar2b22b112020-09-14 18:35:18 +02003300 : (*p == '-' && p[1] == '>')))
Bram Moolenaarb31be3f2020-07-20 22:37:44 +02003301 {
Bram Moolenaar9becdf22020-10-10 21:33:48 +02003302 if (*eap->cmd == '{' && ends_excmd(*skipwhite(eap->cmd + 1)))
3303 {
3304 // "{" by itself is the start of a block.
3305 eap->cmdidx = CMD_block;
3306 return eap->cmd + 1;
3307 }
Bram Moolenaarb31be3f2020-07-20 22:37:44 +02003308 eap->cmdidx = CMD_eval;
3309 return eap->cmd;
3310 }
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003311
Bram Moolenaar2b22b112020-09-14 18:35:18 +02003312 if (p != eap->cmd && (
3313 // "varname[]" is an expression.
3314 *p == '['
3315 // "varname.key" is an expression.
3316 || (*p == '.' && ASCII_ISALPHA(p[1]))))
3317 {
3318 char_u *after = p;
3319
3320 // When followed by "=" or "+=" then it is an assignment.
3321 ++emsg_silent;
Bram Moolenaar683581e2020-10-22 21:22:58 +02003322 if (skip_expr(&after, NULL) == OK
Bram Moolenaar2b22b112020-09-14 18:35:18 +02003323 && (*after == '='
3324 || (*after != NUL && after[1] == '=')))
Bram Moolenaar30fd8202020-09-26 15:09:30 +02003325 eap->cmdidx = CMD_var;
Bram Moolenaar2b22b112020-09-14 18:35:18 +02003326 else
3327 eap->cmdidx = CMD_eval;
3328 --emsg_silent;
3329 return eap->cmd;
3330 }
3331
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003332 // "[...]->Method()" is a list expression, but "[a, b] = Func()" is
3333 // an assignment.
3334 // If there is no line break inside the "[...]" then "p" is
3335 // advanced to after the "]" by to_name_const_end(): check if a "="
3336 // follows.
3337 // If "[...]" has a line break "p" still points at the "[" and it
3338 // can't be an assignment.
3339 if (*eap->cmd == '[')
Bram Moolenaarb31be3f2020-07-20 22:37:44 +02003340 {
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003341 p = to_name_const_end(eap->cmd);
3342 if (p == eap->cmd || *skipwhite(p) != '=')
3343 {
3344 eap->cmdidx = CMD_eval;
3345 return eap->cmd;
3346 }
3347 if (p > eap->cmd && *skipwhite(p) == '=')
3348 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02003349 eap->cmdidx = CMD_var;
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003350 return eap->cmd;
3351 }
3352 }
3353
3354 // Recognize an assignment if we recognize the variable name:
3355 // "g:var = expr"
3356 // "var = expr" where "var" is a local var name.
Bram Moolenaar83144542020-08-02 20:40:43 +02003357 if (*eap->cmd == '@')
3358 p = eap->cmd + 2;
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003359 oplen = assignment_len(skipwhite(p), &heredoc);
3360 if (oplen > 0)
3361 {
3362 if (((p - eap->cmd) > 2 && eap->cmd[1] == ':')
3363 || *eap->cmd == '&'
3364 || *eap->cmd == '$'
3365 || *eap->cmd == '@'
3366 || lookup(eap->cmd, p - eap->cmd, cctx) != NULL)
3367 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02003368 eap->cmdidx = CMD_var;
Bram Moolenaarb5ed2662020-07-28 22:38:37 +02003369 return eap->cmd;
3370 }
3371 }
3372
3373 // Recognize using a type for a w:, b:, t: or g: variable:
3374 // "w:varname: number = 123".
3375 if (eap->cmd[1] == ':' && *p == ':')
3376 {
3377 eap->cmdidx = CMD_eval;
Bram Moolenaarb31be3f2020-07-20 22:37:44 +02003378 return eap->cmd;
3379 }
Bram Moolenaard2ef6b32020-07-02 21:11:34 +02003380 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003381 }
3382#endif
3383
Bram Moolenaar071d4272004-06-13 20:20:40 +00003384 /*
3385 * Isolate the command and search for it in the command table.
Bram Moolenaar81870892007-11-11 18:17:28 +00003386 * Exceptions:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003387 * - the 'k' command can directly be followed by any character.
3388 * - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003389 * but :sre[wind] is another command, as are :scr[iptnames],
Bram Moolenaar071d4272004-06-13 20:20:40 +00003390 * :scs[cope], :sim[alt], :sig[ns] and :sil[ent].
Bram Moolenaardf177f62005-02-22 08:39:57 +00003391 * - the "d" command can directly be followed by 'l' or 'p' flag.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003392 */
3393 p = eap->cmd;
3394 if (*p == 'k')
3395 {
3396 eap->cmdidx = CMD_k;
3397 ++p;
3398 }
3399 else if (p[0] == 's'
Bram Moolenaar204b93f2015-08-04 22:02:51 +02003400 && ((p[1] == 'c' && (p[2] == NUL || (p[2] != 's' && p[2] != 'r'
3401 && (p[3] == NUL || (p[3] != 'i' && p[4] != 'p')))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003402 || p[1] == 'g'
3403 || (p[1] == 'i' && p[2] != 'm' && p[2] != 'l' && p[2] != 'g')
3404 || p[1] == 'I'
3405 || (p[1] == 'r' && p[2] != 'e')))
3406 {
3407 eap->cmdidx = CMD_substitute;
3408 ++p;
3409 }
3410 else
3411 {
3412 while (ASCII_ISALPHA(*p))
3413 ++p;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003414 // for python 3.x support ":py3", ":python3", ":py3file", etc.
Bram Moolenaar55d5c032010-07-17 23:52:29 +02003415 if (eap->cmd[0] == 'p' && eap->cmd[1] == 'y')
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003416 {
Bram Moolenaarb6590522010-07-21 16:00:43 +02003417 while (ASCII_ISALNUM(*p))
3418 ++p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003419 }
3420 else if (*p == '9' && STRNCMP("vim9", eap->cmd, 4) == 0)
3421 {
3422 // include "9" for "vim9script"
3423 ++p;
3424 while (ASCII_ISALPHA(*p))
3425 ++p;
3426 }
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02003427
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003428 // check for non-alpha command
Bram Moolenaar9becdf22020-10-10 21:33:48 +02003429 if (p == eap->cmd && vim_strchr((char_u *)"@*!=><&~#}", *p) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003430 ++p;
3431 len = (int)(p - eap->cmd);
Bram Moolenaardf177f62005-02-22 08:39:57 +00003432 if (*eap->cmd == 'd' && (p[-1] == 'l' || p[-1] == 'p'))
3433 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003434 // Check for ":dl", ":dell", etc. to ":deletel": that's
3435 // :delete with the 'l' flag. Same for 'p'.
Bram Moolenaardf177f62005-02-22 08:39:57 +00003436 for (i = 0; i < len; ++i)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003437 if (eap->cmd[i] != ((char_u *)"delete")[i])
Bram Moolenaardf177f62005-02-22 08:39:57 +00003438 break;
3439 if (i == len - 1)
3440 {
3441 --len;
3442 if (p[-1] == 'l')
3443 eap->flags |= EXFLAG_LIST;
3444 else
3445 eap->flags |= EXFLAG_PRINT;
3446 }
3447 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003448
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003449 if (ASCII_ISLOWER(eap->cmd[0]))
3450 {
Bram Moolenaar6c0c1e82017-03-25 15:07:43 +01003451 int c1 = eap->cmd[0];
Bram Moolenaar94f82cb2019-07-24 22:30:27 +02003452 int c2 = len == 1 ? NUL : eap->cmd[1];
Bram Moolenaar6c0c1e82017-03-25 15:07:43 +01003453
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003454 if (command_count != (int)CMD_SIZE)
3455 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003456 iemsg(_("E943: Command table needs to be updated, run 'make cmdidxs'"));
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003457 getout(1);
3458 }
3459
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003460 // Use a precomputed index for fast look-up in cmdnames[]
3461 // taking into account the first 2 letters of eap->cmd.
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003462 eap->cmdidx = cmdidxs1[CharOrdLow(c1)];
3463 if (ASCII_ISLOWER(c2))
3464 eap->cmdidx += cmdidxs2[CharOrdLow(c1)][CharOrdLow(c2)];
3465 }
Bram Moolenaara494f562020-04-18 17:45:38 +02003466 else if (ASCII_ISUPPER(eap->cmd[0]))
3467 eap->cmdidx = CMD_Next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003468 else
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003469 eap->cmdidx = CMD_bang;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003470
3471 for ( ; (int)eap->cmdidx < (int)CMD_SIZE;
3472 eap->cmdidx = (cmdidx_T)((int)eap->cmdidx + 1))
3473 if (STRNCMP(cmdnames[(int)eap->cmdidx].cmd_name, (char *)eap->cmd,
3474 (size_t)len) == 0)
3475 {
3476#ifdef FEAT_EVAL
3477 if (full != NULL
3478 && cmdnames[(int)eap->cmdidx].cmd_name[len] == NUL)
3479 *full = TRUE;
3480#endif
3481 break;
3482 }
3483
Bram Moolenaar95388e32020-11-20 21:07:00 +01003484 // Not not recognize ":*" as the star command unless '*' is in
3485 // 'cpoptions'.
3486 if (eap->cmdidx == CMD_star && vim_strchr(p_cpo, CPO_STAR) == NULL)
3487 p = eap->cmd;
3488
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003489 // Look for a user defined command as a last resort. Let ":Print" be
3490 // overruled by a user defined command.
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01003491 if ((eap->cmdidx == CMD_SIZE || eap->cmdidx == CMD_Print)
3492 && *eap->cmd >= 'A' && *eap->cmd <= 'Z')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003493 {
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003494 // User defined commands may contain digits.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003495 while (ASCII_ISALNUM(*p))
3496 ++p;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003497 p = find_ucmd(eap, p, full, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003498 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003499 if (p == eap->cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003500 eap->cmdidx = CMD_SIZE;
3501 }
3502
Bram Moolenaar373863e2020-09-26 17:20:53 +02003503 // ":fina" means ":finally" for backwards compatibility.
3504 if (eap->cmdidx == CMD_final && p - eap->cmd == 4)
3505 eap->cmdidx = CMD_finally;
3506
Bram Moolenaar071d4272004-06-13 20:20:40 +00003507 return p;
3508}
3509
3510#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003511static struct cmdmod
3512{
3513 char *name;
3514 int minlen;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003515 int has_count; // :123verbose :3tab
Bram Moolenaared53fb92007-11-24 20:50:24 +00003516} cmdmods[] = {
3517 {"aboveleft", 3, FALSE},
3518 {"belowright", 3, FALSE},
3519 {"botright", 2, FALSE},
3520 {"browse", 3, FALSE},
3521 {"confirm", 4, FALSE},
Bram Moolenaar7b668e82016-08-23 23:51:21 +02003522 {"filter", 4, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003523 {"hide", 3, FALSE},
3524 {"keepalt", 5, FALSE},
3525 {"keepjumps", 5, FALSE},
3526 {"keepmarks", 3, FALSE},
Bram Moolenaara939e432013-11-09 05:30:26 +01003527 {"keeppatterns", 5, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003528 {"leftabove", 5, FALSE},
3529 {"lockmarks", 3, FALSE},
Bram Moolenaarca9f9582008-09-18 10:44:28 +00003530 {"noautocmd", 3, FALSE},
Bram Moolenaar5803ae62014-03-23 16:04:02 +01003531 {"noswapfile", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003532 {"rightbelow", 6, FALSE},
3533 {"sandbox", 3, FALSE},
3534 {"silent", 3, FALSE},
3535 {"tab", 3, TRUE},
3536 {"topleft", 2, FALSE},
Bram Moolenaar8e258a42009-07-09 13:55:43 +00003537 {"unsilent", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003538 {"verbose", 4, TRUE},
3539 {"vertical", 4, FALSE},
3540};
3541
3542/*
3543 * Return length of a command modifier (including optional count).
3544 * Return zero when it's not a modifier.
3545 */
3546 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003547modifier_len(char_u *cmd)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003548{
3549 int i, j;
3550 char_u *p = cmd;
3551
3552 if (VIM_ISDIGIT(*cmd))
3553 p = skipwhite(skipdigits(cmd));
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00003554 for (i = 0; i < (int)(sizeof(cmdmods) / sizeof(struct cmdmod)); ++i)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003555 {
3556 for (j = 0; p[j] != NUL; ++j)
3557 if (p[j] != cmdmods[i].name[j])
3558 break;
Bram Moolenaar2d473ab2013-06-12 17:12:24 +02003559 if (!ASCII_ISALPHA(p[j]) && j >= cmdmods[i].minlen
Bram Moolenaared53fb92007-11-24 20:50:24 +00003560 && (p == cmd || cmdmods[i].has_count))
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00003561 return j + (int)(p - cmd);
Bram Moolenaared53fb92007-11-24 20:50:24 +00003562 }
3563 return 0;
3564}
3565
Bram Moolenaar071d4272004-06-13 20:20:40 +00003566/*
3567 * Return > 0 if an Ex command "name" exists.
3568 * Return 2 if there is an exact match.
3569 * Return 3 if there is an ambiguous match.
3570 */
3571 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003572cmd_exists(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003573{
3574 exarg_T ea;
3575 int full = FALSE;
3576 int i;
3577 int j;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003578 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003579
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003580 // Check command modifiers.
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00003581 for (i = 0; i < (int)(sizeof(cmdmods) / sizeof(struct cmdmod)); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003582 {
3583 for (j = 0; name[j] != NUL; ++j)
3584 if (name[j] != cmdmods[i].name[j])
3585 break;
3586 if (name[j] == NUL && j >= cmdmods[i].minlen)
3587 return (cmdmods[i].name[j] == NUL ? 2 : 1);
3588 }
3589
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003590 // Check built-in commands and user defined commands.
3591 // For ":2match" and ":3match" we need to skip the number.
Bram Moolenaara9587612006-05-04 21:47:50 +00003592 ea.cmd = (*name == '2' || *name == '3') ? name + 1 : name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003593 ea.cmdidx = (cmdidx_T)0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003594 p = find_ex_command(&ea, &full, NULL, NULL);
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003595 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003596 return 3;
Bram Moolenaara9587612006-05-04 21:47:50 +00003597 if (vim_isdigit(*name) && ea.cmdidx != CMD_match)
3598 return 0;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003599 if (*skipwhite(p) != NUL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003600 return 0; // trailing garbage
Bram Moolenaar071d4272004-06-13 20:20:40 +00003601 return (ea.cmdidx == CMD_SIZE ? 0 : (full ? 2 : 1));
3602}
3603#endif
3604
Bram Moolenaard0190392019-08-23 21:17:35 +02003605 cmdidx_T
3606excmd_get_cmdidx(char_u *cmd, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003607{
Bram Moolenaard0190392019-08-23 21:17:35 +02003608 cmdidx_T idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003609
Bram Moolenaard0190392019-08-23 21:17:35 +02003610 for (idx = (cmdidx_T)0; (int)idx < (int)CMD_SIZE;
3611 idx = (cmdidx_T)((int)idx + 1))
3612 if (STRNCMP(cmdnames[(int)idx].cmd_name, cmd, (size_t)len) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003613 break;
3614
Bram Moolenaard0190392019-08-23 21:17:35 +02003615 return idx;
3616}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003617
Bram Moolenaard0190392019-08-23 21:17:35 +02003618 long
3619excmd_get_argt(cmdidx_T idx)
3620{
3621 return (long)cmdnames[(int)idx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003622}
3623
3624/*
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003625 * Skip a range specifier of the form: addr [,addr] [;addr] ..
Bram Moolenaar071d4272004-06-13 20:20:40 +00003626 *
3627 * Backslashed delimiters after / or ? will be skipped, and commands will
3628 * not be expanded between /'s and ?'s or after "'".
3629 *
Bram Moolenaardf069ee2020-06-22 23:02:51 +02003630 * Also skip white space and ":" characters after the range.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003631 * Returns the "cmd" pointer advanced to beyond the range.
3632 */
3633 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003634skip_range(
3635 char_u *cmd,
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02003636 int skip_star, // skip "*" used for Visual range
3637 int *ctx) // pointer to xp_context or NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003638{
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003639 unsigned delim;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003640
Bram Moolenaarcbf20fb2017-02-03 21:19:04 +01003641 while (vim_strchr((char_u *)" \t0123456789.$%'/?-+,;\\", *cmd) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003642 {
Bram Moolenaarcbf20fb2017-02-03 21:19:04 +01003643 if (*cmd == '\\')
3644 {
3645 if (cmd[1] == '?' || cmd[1] == '/' || cmd[1] == '&')
3646 ++cmd;
3647 else
3648 break;
3649 }
3650 else if (*cmd == '\'')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003651 {
3652 if (*++cmd == NUL && ctx != NULL)
3653 *ctx = EXPAND_NOTHING;
3654 }
3655 else if (*cmd == '/' || *cmd == '?')
3656 {
3657 delim = *cmd++;
3658 while (*cmd != NUL && *cmd != delim)
3659 if (*cmd++ == '\\' && *cmd != NUL)
3660 ++cmd;
3661 if (*cmd == NUL && ctx != NULL)
3662 *ctx = EXPAND_NOTHING;
3663 }
3664 if (*cmd != NUL)
3665 ++cmd;
3666 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00003667
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003668 // Skip ":" and white space.
Bram Moolenaardf177f62005-02-22 08:39:57 +00003669 while (*cmd == ':')
3670 cmd = skipwhite(cmd + 1);
3671
Bram Moolenaar3bd8de42020-09-14 16:37:34 +02003672 // Skip "*" used for Visual range.
3673 if (skip_star && *cmd == '*' && vim_strchr(p_cpo, CPO_STAR) == NULL)
3674 cmd = skipwhite(cmd + 1);
3675
Bram Moolenaar071d4272004-06-13 20:20:40 +00003676 return cmd;
3677}
3678
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003679 static void
3680addr_error(cmd_addr_T addr_type)
3681{
3682 if (addr_type == ADDR_NONE)
3683 emsg(_(e_norange));
3684 else
3685 emsg(_(e_invrange));
3686}
3687
Bram Moolenaar071d4272004-06-13 20:20:40 +00003688/*
Bram Moolenaarc2af0af2020-08-23 21:06:02 +02003689 * Return the default address for an address type.
3690 */
3691 static linenr_T
3692default_address(exarg_T *eap)
3693{
3694 linenr_T lnum = 0;
3695
3696 switch (eap->addr_type)
3697 {
3698 case ADDR_LINES:
3699 case ADDR_OTHER:
3700 // Default is the cursor line number. Avoid using an invalid
3701 // line number though.
3702 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
3703 lnum = curbuf->b_ml.ml_line_count;
3704 else
3705 lnum = curwin->w_cursor.lnum;
3706 break;
3707 case ADDR_WINDOWS:
3708 lnum = CURRENT_WIN_NR;
3709 break;
3710 case ADDR_ARGUMENTS:
3711 lnum = curwin->w_arg_idx + 1;
3712 if (lnum > ARGCOUNT)
3713 lnum = ARGCOUNT;
3714 break;
3715 case ADDR_LOADED_BUFFERS:
3716 case ADDR_BUFFERS:
3717 lnum = curbuf->b_fnum;
3718 break;
3719 case ADDR_TABS:
3720 lnum = CURRENT_TAB_NR;
3721 break;
3722 case ADDR_TABS_RELATIVE:
3723 case ADDR_UNSIGNED:
3724 lnum = 1;
3725 break;
3726 case ADDR_QUICKFIX:
3727#ifdef FEAT_QUICKFIX
3728 lnum = qf_get_cur_idx(eap);
3729#endif
3730 break;
3731 case ADDR_QUICKFIX_VALID:
3732#ifdef FEAT_QUICKFIX
3733 lnum = qf_get_cur_valid_idx(eap);
3734#endif
3735 break;
3736 case ADDR_NONE:
3737 // Will give an error later if a range is found.
3738 break;
3739 }
3740 return lnum;
3741}
3742
3743/*
Bram Moolenaar198cb662018-09-06 21:44:17 +02003744 * Get a single EX address.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003745 *
3746 * Set ptr to the next character after the part that was interpreted.
3747 * Set ptr to NULL when an error is encountered.
Bram Moolenaar198cb662018-09-06 21:44:17 +02003748 * This may set the last used search pattern.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003749 *
3750 * Return MAXLNUM when no Ex address was found.
3751 */
3752 static linenr_T
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003753get_address(
3754 exarg_T *eap UNUSED,
3755 char_u **ptr,
Bram Moolenaar1b03a192019-12-08 17:08:29 +01003756 cmd_addr_T addr_type,
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02003757 int skip, // only skip the address, don't use it
3758 int silent, // no errors or side effects
3759 int to_other_file, // flag: may jump to other file
3760 int address_count UNUSED) // 1 for first address, >1 after comma
Bram Moolenaar071d4272004-06-13 20:20:40 +00003761{
3762 int c;
3763 int i;
3764 long n;
3765 char_u *cmd;
3766 pos_T pos;
3767 pos_T *fp;
3768 linenr_T lnum;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003769 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003770
3771 cmd = skipwhite(*ptr);
3772 lnum = MAXLNUM;
3773 do
3774 {
3775 switch (*cmd)
3776 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003777 case '.': // '.' - Cursor position
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003778 ++cmd;
3779 switch (addr_type)
3780 {
3781 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02003782 case ADDR_OTHER:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003783 lnum = curwin->w_cursor.lnum;
3784 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003785 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01003786 lnum = CURRENT_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003787 break;
3788 case ADDR_ARGUMENTS:
3789 lnum = curwin->w_arg_idx + 1;
3790 break;
3791 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003792 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003793 lnum = curbuf->b_fnum;
3794 break;
3795 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01003796 lnum = CURRENT_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003797 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02003798 case ADDR_NONE:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003799 case ADDR_TABS_RELATIVE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02003800 case ADDR_UNSIGNED:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003801 addr_error(addr_type);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01003802 cmd = NULL;
3803 goto error;
3804 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003805 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003806#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02003807 lnum = qf_get_cur_idx(eap);
3808#endif
3809 break;
3810 case ADDR_QUICKFIX_VALID:
3811#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003812 lnum = qf_get_cur_valid_idx(eap);
Bram Moolenaare906c502015-09-09 21:10:39 +02003813#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003814 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003815 }
3816 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003817
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003818 case '$': // '$' - last line
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003819 ++cmd;
3820 switch (addr_type)
3821 {
3822 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02003823 case ADDR_OTHER:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003824 lnum = curbuf->b_ml.ml_line_count;
3825 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003826 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01003827 lnum = LAST_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003828 break;
3829 case ADDR_ARGUMENTS:
3830 lnum = ARGCOUNT;
3831 break;
3832 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003833 buf = lastbuf;
3834 while (buf->b_ml.ml_mfp == NULL)
3835 {
3836 if (buf->b_prev == NULL)
3837 break;
3838 buf = buf->b_prev;
3839 }
3840 lnum = buf->b_fnum;
3841 break;
3842 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003843 lnum = lastbuf->b_fnum;
3844 break;
3845 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01003846 lnum = LAST_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003847 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02003848 case ADDR_NONE:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003849 case ADDR_TABS_RELATIVE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02003850 case ADDR_UNSIGNED:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003851 addr_error(addr_type);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01003852 cmd = NULL;
3853 goto error;
3854 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003855 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003856#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003857 lnum = qf_get_size(eap);
3858 if (lnum == 0)
3859 lnum = 1;
Bram Moolenaare906c502015-09-09 21:10:39 +02003860#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003861 break;
Bram Moolenaar25190db2019-05-04 15:05:28 +02003862 case ADDR_QUICKFIX_VALID:
3863#ifdef FEAT_QUICKFIX
3864 lnum = qf_get_valid_size(eap);
3865 if (lnum == 0)
3866 lnum = 1;
3867#endif
3868 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003869 }
3870 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003871
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003872 case '\'': // ''' - mark
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003873 if (*++cmd == NUL)
3874 {
3875 cmd = NULL;
3876 goto error;
3877 }
3878 if (addr_type != ADDR_LINES)
3879 {
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003880 addr_error(addr_type);
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01003881 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003882 goto error;
3883 }
3884 if (skip)
3885 ++cmd;
3886 else
3887 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003888 // Only accept a mark in another file when it is
3889 // used by itself: ":'M".
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003890 fp = getmark(*cmd, to_other_file && cmd[1] == NUL);
3891 ++cmd;
3892 if (fp == (pos_T *)-1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003893 // Jumped to another file.
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003894 lnum = curwin->w_cursor.lnum;
3895 else
3896 {
3897 if (check_mark(fp) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003898 {
3899 cmd = NULL;
3900 goto error;
3901 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003902 lnum = fp->lnum;
3903 }
3904 }
3905 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003906
3907 case '/':
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003908 case '?': // '/' or '?' - search
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003909 c = *cmd++;
3910 if (addr_type != ADDR_LINES)
3911 {
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003912 addr_error(addr_type);
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01003913 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003914 goto error;
3915 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003916 if (skip) // skip "/pat/"
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003917 {
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02003918 cmd = skip_regexp(cmd, c, (int)p_magic);
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003919 if (*cmd == c)
3920 ++cmd;
3921 }
3922 else
3923 {
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02003924 int flags;
3925
3926 pos = curwin->w_cursor; // save curwin->w_cursor
3927
3928 // When '/' or '?' follows another address, start from
3929 // there.
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003930 if (lnum != MAXLNUM)
3931 curwin->w_cursor.lnum = lnum;
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02003932
3933 // Start a forward search at the end of the line (unless
3934 // before the first line).
3935 // Start a backward search at the start of the line.
3936 // This makes sure we never match in the current
3937 // line, and can match anywhere in the
3938 // next/previous line.
Bram Moolenaar8ada6aa2017-12-19 21:23:21 +01003939 if (c == '/' && curwin->w_cursor.lnum > 0)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003940 curwin->w_cursor.col = MAXCOL;
3941 else
3942 curwin->w_cursor.col = 0;
3943 searchcmdlen = 0;
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02003944 flags = silent ? 0 : SEARCH_HIS | SEARCH_MSG;
Bram Moolenaarc036e872020-02-21 21:30:52 +01003945 if (!do_search(NULL, c, c, cmd, 1L, flags, NULL))
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003946 {
3947 curwin->w_cursor = pos;
3948 cmd = NULL;
3949 goto error;
3950 }
3951 lnum = curwin->w_cursor.lnum;
3952 curwin->w_cursor = pos;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003953 // adjust command string pointer
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003954 cmd += searchcmdlen;
3955 }
3956 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003957
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003958 case '\\': // "\?", "\/" or "\&", repeat search
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003959 ++cmd;
3960 if (addr_type != ADDR_LINES)
3961 {
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003962 addr_error(addr_type);
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01003963 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003964 goto error;
3965 }
3966 if (*cmd == '&')
3967 i = RE_SUBST;
3968 else if (*cmd == '?' || *cmd == '/')
3969 i = RE_SEARCH;
3970 else
3971 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003972 emsg(_(e_backslash));
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003973 cmd = NULL;
3974 goto error;
3975 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003976
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003977 if (!skip)
3978 {
3979 /*
3980 * When search follows another address, start from
3981 * there.
3982 */
3983 if (lnum != MAXLNUM)
3984 pos.lnum = lnum;
3985 else
3986 pos.lnum = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003987
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003988 /*
3989 * Start the search just like for the above
3990 * do_search().
3991 */
3992 if (*cmd != '?')
3993 pos.col = MAXCOL;
3994 else
3995 pos.col = 0;
Bram Moolenaarbd8539a2015-08-11 18:53:03 +02003996 pos.coladd = 0;
Bram Moolenaar5d24a222018-12-23 19:10:09 +01003997 if (searchit(curwin, curbuf, &pos, NULL,
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003998 *cmd == '?' ? BACKWARD : FORWARD,
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02003999 (char_u *)"", 1L, SEARCH_MSG, i, NULL) != FAIL)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004000 lnum = pos.lnum;
4001 else
4002 {
4003 cmd = NULL;
4004 goto error;
4005 }
4006 }
4007 ++cmd;
4008 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004009
4010 default:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004011 if (VIM_ISDIGIT(*cmd)) // absolute line number
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004012 lnum = getdigits(&cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004013 }
4014
4015 for (;;)
4016 {
4017 cmd = skipwhite(cmd);
4018 if (*cmd != '-' && *cmd != '+' && !VIM_ISDIGIT(*cmd))
4019 break;
4020
4021 if (lnum == MAXLNUM)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004022 {
4023 switch (addr_type)
4024 {
4025 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004026 case ADDR_OTHER:
4027 // "+1" is same as ".+1"
Bram Moolenaarf240e182014-11-27 18:33:02 +01004028 lnum = curwin->w_cursor.lnum;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004029 break;
4030 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004031 lnum = CURRENT_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004032 break;
4033 case ADDR_ARGUMENTS:
4034 lnum = curwin->w_arg_idx + 1;
4035 break;
4036 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004037 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004038 lnum = curbuf->b_fnum;
4039 break;
4040 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004041 lnum = CURRENT_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004042 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004043 case ADDR_TABS_RELATIVE:
4044 lnum = 1;
4045 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004046 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004047#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02004048 lnum = qf_get_cur_idx(eap);
4049#endif
4050 break;
4051 case ADDR_QUICKFIX_VALID:
4052#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004053 lnum = qf_get_cur_valid_idx(eap);
Bram Moolenaare906c502015-09-09 21:10:39 +02004054#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004055 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02004056 case ADDR_NONE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02004057 case ADDR_UNSIGNED:
4058 lnum = 0;
Bram Moolenaarb7316892019-05-01 18:08:42 +02004059 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004060 }
4061 }
4062
Bram Moolenaar071d4272004-06-13 20:20:40 +00004063 if (VIM_ISDIGIT(*cmd))
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004064 i = '+'; // "number" is same as "+number"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004065 else
4066 i = *cmd++;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004067 if (!VIM_ISDIGIT(*cmd)) // '+' is '+1', but '+0' is not '+1'
Bram Moolenaar071d4272004-06-13 20:20:40 +00004068 n = 1;
4069 else
4070 n = getdigits(&cmd);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004071
4072 if (addr_type == ADDR_TABS_RELATIVE)
4073 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004074 emsg(_(e_invrange));
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004075 cmd = NULL;
4076 goto error;
4077 }
4078 else if (addr_type == ADDR_LOADED_BUFFERS
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004079 || addr_type == ADDR_BUFFERS)
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004080 lnum = compute_buffer_local_count(
4081 addr_type, lnum, (i == '-') ? -1 * n : n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004082 else
Bram Moolenaarded27822017-01-02 14:27:34 +01004083 {
4084#ifdef FEAT_FOLDING
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004085 // Relative line addressing, need to adjust for folded lines
4086 // now, but only do it after the first address.
Bram Moolenaarded27822017-01-02 14:27:34 +01004087 if (addr_type == ADDR_LINES && (i == '-' || i == '+')
4088 && address_count >= 2)
4089 (void)hasFolding(lnum, NULL, &lnum);
4090#endif
4091 if (i == '-')
4092 lnum -= n;
4093 else
4094 lnum += n;
4095 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004096 }
4097 } while (*cmd == '/' || *cmd == '?');
4098
4099error:
4100 *ptr = cmd;
4101 return lnum;
4102}
4103
4104/*
Bram Moolenaarc2af0af2020-08-23 21:06:02 +02004105 * Set eap->line1 and eap->line2 to the whole range.
4106 * Used for commands with the EX_DFLALL flag and no range given.
4107 */
4108 static void
4109address_default_all(exarg_T *eap)
4110{
4111 eap->line1 = 1;
4112 switch (eap->addr_type)
4113 {
4114 case ADDR_LINES:
4115 case ADDR_OTHER:
4116 eap->line2 = curbuf->b_ml.ml_line_count;
4117 break;
4118 case ADDR_LOADED_BUFFERS:
4119 {
4120 buf_T *buf = firstbuf;
4121
4122 while (buf->b_next != NULL && buf->b_ml.ml_mfp == NULL)
4123 buf = buf->b_next;
4124 eap->line1 = buf->b_fnum;
4125 buf = lastbuf;
4126 while (buf->b_prev != NULL && buf->b_ml.ml_mfp == NULL)
4127 buf = buf->b_prev;
4128 eap->line2 = buf->b_fnum;
4129 }
4130 break;
4131 case ADDR_BUFFERS:
4132 eap->line1 = firstbuf->b_fnum;
4133 eap->line2 = lastbuf->b_fnum;
4134 break;
4135 case ADDR_WINDOWS:
4136 eap->line2 = LAST_WIN_NR;
4137 break;
4138 case ADDR_TABS:
4139 eap->line2 = LAST_TAB_NR;
4140 break;
4141 case ADDR_TABS_RELATIVE:
4142 eap->line2 = 1;
4143 break;
4144 case ADDR_ARGUMENTS:
4145 if (ARGCOUNT == 0)
4146 eap->line1 = eap->line2 = 0;
4147 else
4148 eap->line2 = ARGCOUNT;
4149 break;
4150 case ADDR_QUICKFIX_VALID:
4151#ifdef FEAT_QUICKFIX
4152 eap->line2 = qf_get_valid_size(eap);
4153 if (eap->line2 == 0)
4154 eap->line2 = 1;
4155#endif
4156 break;
4157 case ADDR_NONE:
4158 case ADDR_UNSIGNED:
4159 case ADDR_QUICKFIX:
4160 iemsg(_("INTERNAL: Cannot use EX_DFLALL with ADDR_NONE, ADDR_UNSIGNED or ADDR_QUICKFIX"));
4161 break;
4162 }
4163}
4164
4165
4166/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00004167 * Get flags from an Ex command argument.
4168 */
4169 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004170get_flags(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00004171{
4172 while (vim_strchr((char_u *)"lp#", *eap->arg) != NULL)
4173 {
4174 if (*eap->arg == 'l')
4175 eap->flags |= EXFLAG_LIST;
4176 else if (*eap->arg == 'p')
4177 eap->flags |= EXFLAG_PRINT;
4178 else
4179 eap->flags |= EXFLAG_NR;
4180 eap->arg = skipwhite(eap->arg + 1);
4181 }
4182}
4183
4184/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004185 * Function called for command which is Not Implemented. NI!
4186 */
4187 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004188ex_ni(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004189{
4190 if (!eap->skip)
Bram Moolenaar8930caa2020-07-23 16:37:03 +02004191 eap->errmsg =
4192 _("E319: Sorry, the command is not available in this version");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004193}
4194
Bram Moolenaar7bb75552007-07-16 18:39:49 +00004195#ifdef HAVE_EX_SCRIPT_NI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004196/*
4197 * Function called for script command which is Not Implemented. NI!
4198 * Skips over ":perl <<EOF" constructs.
4199 */
4200 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004201ex_script_ni(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004202{
4203 if (!eap->skip)
4204 ex_ni(eap);
4205 else
4206 vim_free(script_get(eap, eap->arg));
4207}
4208#endif
4209
4210/*
4211 * Check range in Ex command for validity.
4212 * Return NULL when valid, error message when invalid.
4213 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004214 static char *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004215invalid_range(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004216{
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004217 buf_T *buf;
Bram Moolenaar25190db2019-05-04 15:05:28 +02004218
Bram Moolenaar071d4272004-06-13 20:20:40 +00004219 if ( eap->line1 < 0
4220 || eap->line2 < 0
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004221 || eap->line1 > eap->line2)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004222 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004223
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004224 if (eap->argt & EX_RANGE)
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004225 {
Bram Moolenaarb7316892019-05-01 18:08:42 +02004226 switch (eap->addr_type)
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004227 {
4228 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004229 if (eap->line2 > curbuf->b_ml.ml_line_count
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004230#ifdef FEAT_DIFF
4231 + (eap->cmdidx == CMD_diffget)
4232#endif
4233 )
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004234 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004235 break;
4236 case ADDR_ARGUMENTS:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004237 // add 1 if ARGCOUNT is 0
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004238 if (eap->line2 > ARGCOUNT + (!ARGCOUNT))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004239 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004240 break;
4241 case ADDR_BUFFERS:
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02004242 // Only a boundary check, not whether the buffers actually
4243 // exist.
4244 if (eap->line1 < 1 || eap->line2 > get_highest_fnum())
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004245 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004246 break;
4247 case ADDR_LOADED_BUFFERS:
4248 buf = firstbuf;
4249 while (buf->b_ml.ml_mfp == NULL)
4250 {
4251 if (buf->b_next == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004252 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004253 buf = buf->b_next;
4254 }
4255 if (eap->line1 < buf->b_fnum)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004256 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004257 buf = lastbuf;
4258 while (buf->b_ml.ml_mfp == NULL)
4259 {
4260 if (buf->b_prev == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004261 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004262 buf = buf->b_prev;
4263 }
4264 if (eap->line2 > buf->b_fnum)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004265 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004266 break;
4267 case ADDR_WINDOWS:
Bram Moolenaar8be63882015-01-14 11:25:05 +01004268 if (eap->line2 > LAST_WIN_NR)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004269 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004270 break;
4271 case ADDR_TABS:
4272 if (eap->line2 > LAST_TAB_NR)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004273 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004274 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004275 case ADDR_TABS_RELATIVE:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004276 case ADDR_OTHER:
4277 // Any range is OK.
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004278 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004279 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004280#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02004281 // No error for value that is too big, will use the last entry.
4282 if (eap->line2 <= 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004283 return _(e_invrange);
Bram Moolenaare906c502015-09-09 21:10:39 +02004284#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004285 break;
Bram Moolenaar25190db2019-05-04 15:05:28 +02004286 case ADDR_QUICKFIX_VALID:
4287#ifdef FEAT_QUICKFIX
4288 if ((eap->line2 != 1 && eap->line2 > qf_get_valid_size(eap))
4289 || eap->line2 < 0)
4290 return _(e_invrange);
4291#endif
4292 break;
4293 case ADDR_UNSIGNED:
4294 if (eap->line2 < 0)
4295 return _(e_invrange);
4296 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02004297 case ADDR_NONE:
4298 // Will give an error elsewhere.
4299 break;
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004300 }
4301 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004302 return NULL;
4303}
4304
4305/*
4306 * Correct the range for zero line number, if required.
4307 */
4308 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004309correct_range(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004310{
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004311 if (!(eap->argt & EX_ZEROR)) // zero in range not allowed
Bram Moolenaar071d4272004-06-13 20:20:40 +00004312 {
4313 if (eap->line1 == 0)
4314 eap->line1 = 1;
4315 if (eap->line2 == 0)
4316 eap->line2 = 1;
4317 }
4318}
4319
Bram Moolenaar748bf032005-02-02 23:04:36 +00004320#ifdef FEAT_QUICKFIX
Bram Moolenaar748bf032005-02-02 23:04:36 +00004321/*
4322 * For a ":vimgrep" or ":vimgrepadd" command return a pointer past the
4323 * pattern. Otherwise return eap->arg.
4324 */
4325 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004326skip_grep_pat(exarg_T *eap)
Bram Moolenaar748bf032005-02-02 23:04:36 +00004327{
4328 char_u *p = eap->arg;
4329
Bram Moolenaara37420f2006-02-04 22:37:47 +00004330 if (*p != NUL && (eap->cmdidx == CMD_vimgrep || eap->cmdidx == CMD_lvimgrep
4331 || eap->cmdidx == CMD_vimgrepadd
4332 || eap->cmdidx == CMD_lvimgrepadd
4333 || grep_internal(eap->cmdidx)))
Bram Moolenaar748bf032005-02-02 23:04:36 +00004334 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00004335 p = skip_vimgrep_pat(p, NULL, NULL);
Bram Moolenaar748bf032005-02-02 23:04:36 +00004336 if (p == NULL)
4337 p = eap->arg;
Bram Moolenaar748bf032005-02-02 23:04:36 +00004338 }
4339 return p;
4340}
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004341
4342/*
4343 * For the ":make" and ":grep" commands insert the 'makeprg'/'grepprg' option
4344 * in the command line, so that things like % get expanded.
4345 */
4346 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004347replace_makeprg(exarg_T *eap, char_u *p, char_u **cmdlinep)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004348{
4349 char_u *new_cmdline;
4350 char_u *program;
4351 char_u *pos;
4352 char_u *ptr;
4353 int len;
4354 int i;
4355
4356 /*
4357 * Don't do it when ":vimgrep" is used for ":grep".
4358 */
Bram Moolenaara37420f2006-02-04 22:37:47 +00004359 if ((eap->cmdidx == CMD_make || eap->cmdidx == CMD_lmake
4360 || eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4361 || eap->cmdidx == CMD_grepadd
4362 || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004363 && !grep_internal(eap->cmdidx))
4364 {
Bram Moolenaara37420f2006-02-04 22:37:47 +00004365 if (eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4366 || eap->cmdidx == CMD_grepadd || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004367 {
4368 if (*curbuf->b_p_gp == NUL)
4369 program = p_gp;
4370 else
4371 program = curbuf->b_p_gp;
4372 }
4373 else
4374 {
4375 if (*curbuf->b_p_mp == NUL)
4376 program = p_mp;
4377 else
4378 program = curbuf->b_p_mp;
4379 }
4380
4381 p = skipwhite(p);
4382
4383 if ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4384 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004385 // replace $* by given arguments
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004386 i = 1;
4387 while ((pos = (char_u *)strstr((char *)pos + 2, "$*")) != NULL)
4388 ++i;
4389 len = (int)STRLEN(p);
Bram Moolenaar51e14382019-05-25 20:21:28 +02004390 new_cmdline = alloc(STRLEN(program) + i * (len - 2) + 1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004391 if (new_cmdline == NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004392 return NULL; // out of memory
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004393 ptr = new_cmdline;
4394 while ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4395 {
4396 i = (int)(pos - program);
4397 STRNCPY(ptr, program, i);
4398 STRCPY(ptr += i, p);
4399 ptr += len;
4400 program = pos + 2;
4401 }
4402 STRCPY(ptr, program);
4403 }
4404 else
4405 {
Bram Moolenaar51e14382019-05-25 20:21:28 +02004406 new_cmdline = alloc(STRLEN(program) + STRLEN(p) + 2);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004407 if (new_cmdline == NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004408 return NULL; // out of memory
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004409 STRCPY(new_cmdline, program);
4410 STRCAT(new_cmdline, " ");
4411 STRCAT(new_cmdline, p);
4412 }
4413 msg_make(p);
4414
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004415 // 'eap->cmd' is not set here, because it is not used at CMD_make
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004416 vim_free(*cmdlinep);
4417 *cmdlinep = new_cmdline;
4418 p = new_cmdline;
4419 }
4420 return p;
4421}
Bram Moolenaar748bf032005-02-02 23:04:36 +00004422#endif
4423
Bram Moolenaar071d4272004-06-13 20:20:40 +00004424/*
4425 * Expand file name in Ex command argument.
Bram Moolenaar0abb4272019-06-15 16:06:00 +02004426 * When an error is detected, "errormsgp" is set to a non-NULL pointer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004427 * Return FAIL for failure, OK otherwise.
4428 */
4429 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004430expand_filename(
4431 exarg_T *eap,
4432 char_u **cmdlinep,
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004433 char **errormsgp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004434{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004435 int has_wildcards; // need to expand wildcards
Bram Moolenaar071d4272004-06-13 20:20:40 +00004436 char_u *repl;
4437 int srclen;
4438 char_u *p;
4439 int n;
Bram Moolenaar63b92542007-03-27 14:57:09 +00004440 int escaped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004441
Bram Moolenaar748bf032005-02-02 23:04:36 +00004442#ifdef FEAT_QUICKFIX
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004443 // Skip a regexp pattern for ":vimgrep[add] pat file..."
Bram Moolenaar748bf032005-02-02 23:04:36 +00004444 p = skip_grep_pat(eap);
4445#else
4446 p = eap->arg;
4447#endif
4448
Bram Moolenaar071d4272004-06-13 20:20:40 +00004449 /*
4450 * Decide to expand wildcards *before* replacing '%', '#', etc. If
4451 * the file name contains a wildcard it should not cause expanding.
4452 * (it will be expanded anyway if there is a wildcard before replacing).
4453 */
Bram Moolenaar748bf032005-02-02 23:04:36 +00004454 has_wildcards = mch_has_wildcard(p);
4455 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004456 {
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004457#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004458 // Skip over `=expr`, wildcards in it are not expanded.
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004459 if (p[0] == '`' && p[1] == '=')
4460 {
4461 p += 2;
Bram Moolenaar683581e2020-10-22 21:22:58 +02004462 (void)skip_expr(&p, NULL);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004463 if (*p == '`')
4464 ++p;
4465 continue;
4466 }
4467#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004468 /*
4469 * Quick check if this cannot be the start of a special string.
4470 * Also removes backslash before '%', '#' and '<'.
4471 */
4472 if (vim_strchr((char_u *)"%#<", *p) == NULL)
4473 {
4474 ++p;
4475 continue;
4476 }
4477
4478 /*
4479 * Try to find a match at this position.
4480 */
Bram Moolenaar63b92542007-03-27 14:57:09 +00004481 repl = eval_vars(p, eap->arg, &srclen, &(eap->do_ecmd_lnum),
4482 errormsgp, &escaped);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004483 if (*errormsgp != NULL) // error detected
Bram Moolenaar071d4272004-06-13 20:20:40 +00004484 return FAIL;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004485 if (repl == NULL) // no match found
Bram Moolenaar071d4272004-06-13 20:20:40 +00004486 {
4487 p += srclen;
4488 continue;
4489 }
4490
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004491 // Wildcards won't be expanded below, the replacement is taken
4492 // literally. But do expand "~/file", "~user/file" and "$HOME/file".
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00004493 if (vim_strchr(repl, '$') != NULL || vim_strchr(repl, '~') != NULL)
4494 {
4495 char_u *l = repl;
4496
4497 repl = expand_env_save(repl);
4498 vim_free(l);
4499 }
4500
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004501 // Need to escape white space et al. with a backslash.
4502 // Don't do this for:
4503 // - replacement that already has been escaped: "##"
4504 // - shell commands (may have to use quotes instead).
4505 // - non-unix systems when there is a single argument (spaces don't
4506 // separate arguments then).
Bram Moolenaar071d4272004-06-13 20:20:40 +00004507 if (!eap->usefilter
Bram Moolenaar63b92542007-03-27 14:57:09 +00004508 && !escaped
Bram Moolenaar071d4272004-06-13 20:20:40 +00004509 && eap->cmdidx != CMD_bang
Bram Moolenaar071d4272004-06-13 20:20:40 +00004510 && eap->cmdidx != CMD_grep
4511 && eap->cmdidx != CMD_grepadd
Bram Moolenaarbf15b8d2017-06-04 20:43:48 +02004512 && eap->cmdidx != CMD_hardcopy
Bram Moolenaar67883b42017-07-27 22:57:00 +02004513 && eap->cmdidx != CMD_lgrep
4514 && eap->cmdidx != CMD_lgrepadd
4515 && eap->cmdidx != CMD_lmake
4516 && eap->cmdidx != CMD_make
4517 && eap->cmdidx != CMD_terminal
Bram Moolenaar071d4272004-06-13 20:20:40 +00004518#ifndef UNIX
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004519 && !(eap->argt & EX_NOSPC)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004520#endif
4521 )
4522 {
4523 char_u *l;
4524#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004525 // Don't escape a backslash here, because rem_backslash() doesn't
4526 // remove it later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004527 static char_u *nobslash = (char_u *)" \t\"|";
4528# define ESCAPE_CHARS nobslash
4529#else
4530# define ESCAPE_CHARS escape_chars
4531#endif
4532
4533 for (l = repl; *l; ++l)
4534 if (vim_strchr(ESCAPE_CHARS, *l) != NULL)
4535 {
4536 l = vim_strsave_escaped(repl, ESCAPE_CHARS);
4537 if (l != NULL)
4538 {
4539 vim_free(repl);
4540 repl = l;
4541 }
4542 break;
4543 }
4544 }
4545
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004546 // For a shell command a '!' must be escaped.
Bram Moolenaar67883b42017-07-27 22:57:00 +02004547 if ((eap->usefilter || eap->cmdidx == CMD_bang
4548 || eap->cmdidx == CMD_terminal)
Bram Moolenaar31b7d382014-04-01 18:54:48 +02004549 && vim_strpbrk(repl, (char_u *)"!") != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004550 {
4551 char_u *l;
4552
Bram Moolenaar31b7d382014-04-01 18:54:48 +02004553 l = vim_strsave_escaped(repl, (char_u *)"!");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004554 if (l != NULL)
4555 {
4556 vim_free(repl);
4557 repl = l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004558 }
4559 }
4560
4561 p = repl_cmdline(eap, p, srclen, repl, cmdlinep);
4562 vim_free(repl);
4563 if (p == NULL)
4564 return FAIL;
4565 }
4566
4567 /*
4568 * One file argument: Expand wildcards.
4569 * Don't do this with ":r !command" or ":w !command".
4570 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004571 if ((eap->argt & EX_NOSPC) && !eap->usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004572 {
4573 /*
4574 * May do this twice:
4575 * 1. Replace environment variables.
4576 * 2. Replace any other wildcards, remove backslashes.
4577 */
4578 for (n = 1; n <= 2; ++n)
4579 {
4580 if (n == 2)
4581 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004582 /*
4583 * Halve the number of backslashes (this is Vi compatible).
4584 * For Unix and OS/2, when wildcards are expanded, this is
4585 * done by ExpandOne() below.
4586 */
Bram Moolenaare7fedb62015-12-31 19:07:19 +01004587#if defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004588 if (!has_wildcards)
4589#endif
4590 backslash_halve(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004591 }
4592
4593 if (has_wildcards)
4594 {
4595 if (n == 1)
4596 {
4597 /*
4598 * First loop: May expand environment variables. This
4599 * can be done much faster with expand_env() than with
4600 * something else (e.g., calling a shell).
4601 * After expanding environment variables, check again
4602 * if there are still wildcards present.
4603 */
4604 if (vim_strchr(eap->arg, '$') != NULL
4605 || vim_strchr(eap->arg, '~') != NULL)
4606 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +00004607 expand_env_esc(eap->arg, NameBuff, MAXPATHL,
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00004608 TRUE, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004609 has_wildcards = mch_has_wildcard(NameBuff);
4610 p = NameBuff;
4611 }
4612 else
4613 p = NULL;
4614 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004615 else // n == 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00004616 {
4617 expand_T xpc;
Bram Moolenaarb40c2572019-10-19 21:01:05 +02004618 int options = WILD_LIST_NOTFOUND
4619 | WILD_NOERROR | WILD_ADD_SLASH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004620
4621 ExpandInit(&xpc);
4622 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar94950a92010-12-02 16:01:29 +01004623 if (p_wic)
4624 options += WILD_ICASE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004625 p = ExpandOne(&xpc, eap->arg, NULL,
Bram Moolenaar94950a92010-12-02 16:01:29 +01004626 options, WILD_EXPAND_FREE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004627 if (p == NULL)
4628 return FAIL;
4629 }
4630 if (p != NULL)
4631 {
4632 (void)repl_cmdline(eap, eap->arg, (int)STRLEN(eap->arg),
4633 p, cmdlinep);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004634 if (n == 2) // p came from ExpandOne()
Bram Moolenaar071d4272004-06-13 20:20:40 +00004635 vim_free(p);
4636 }
4637 }
4638 }
4639 }
4640 return OK;
4641}
4642
4643/*
4644 * Replace part of the command line, keeping eap->cmd, eap->arg and
4645 * eap->nextcmd correct.
4646 * "src" points to the part that is to be replaced, of length "srclen".
4647 * "repl" is the replacement string.
4648 * Returns a pointer to the character after the replaced string.
4649 * Returns NULL for failure.
4650 */
4651 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004652repl_cmdline(
4653 exarg_T *eap,
4654 char_u *src,
4655 int srclen,
4656 char_u *repl,
4657 char_u **cmdlinep)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004658{
4659 int len;
4660 int i;
4661 char_u *new_cmdline;
4662
4663 /*
4664 * The new command line is build in new_cmdline[].
4665 * First allocate it.
4666 * Careful: a "+cmd" argument may have been NUL terminated.
4667 */
4668 len = (int)STRLEN(repl);
4669 i = (int)(src - *cmdlinep) + (int)STRLEN(src + srclen) + len + 3;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004670 if (eap->nextcmd != NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004671 i += (int)STRLEN(eap->nextcmd);// add space for next command
Bram Moolenaar964b3742019-05-24 18:54:09 +02004672 if ((new_cmdline = alloc(i)) == NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004673 return NULL; // out of memory!
Bram Moolenaar071d4272004-06-13 20:20:40 +00004674
4675 /*
4676 * Copy the stuff before the expanded part.
4677 * Copy the expanded stuff.
4678 * Copy what came after the expanded part.
4679 * Copy the next commands, if there are any.
4680 */
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004681 i = (int)(src - *cmdlinep); // length of part before match
Bram Moolenaar071d4272004-06-13 20:20:40 +00004682 mch_memmove(new_cmdline, *cmdlinep, (size_t)i);
Bram Moolenaara3ffd9c2005-07-21 21:03:15 +00004683
Bram Moolenaar071d4272004-06-13 20:20:40 +00004684 mch_memmove(new_cmdline + i, repl, (size_t)len);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004685 i += len; // remember the end of the string
Bram Moolenaar071d4272004-06-13 20:20:40 +00004686 STRCPY(new_cmdline + i, src + srclen);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004687 src = new_cmdline + i; // remember where to continue
Bram Moolenaar071d4272004-06-13 20:20:40 +00004688
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004689 if (eap->nextcmd != NULL) // append next command
Bram Moolenaar071d4272004-06-13 20:20:40 +00004690 {
4691 i = (int)STRLEN(new_cmdline) + 1;
4692 STRCPY(new_cmdline + i, eap->nextcmd);
4693 eap->nextcmd = new_cmdline + i;
4694 }
4695 eap->cmd = new_cmdline + (eap->cmd - *cmdlinep);
4696 eap->arg = new_cmdline + (eap->arg - *cmdlinep);
4697 if (eap->do_ecmd_cmd != NULL && eap->do_ecmd_cmd != dollar_command)
4698 eap->do_ecmd_cmd = new_cmdline + (eap->do_ecmd_cmd - *cmdlinep);
4699 vim_free(*cmdlinep);
4700 *cmdlinep = new_cmdline;
4701
4702 return src;
4703}
4704
4705/*
4706 * Check for '|' to separate commands and '"' to start comments.
4707 */
4708 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004709separate_nextcmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004710{
4711 char_u *p;
4712
Bram Moolenaar86b68352004-12-27 21:59:20 +00004713#ifdef FEAT_QUICKFIX
Bram Moolenaar748bf032005-02-02 23:04:36 +00004714 p = skip_grep_pat(eap);
4715#else
4716 p = eap->arg;
Bram Moolenaar86b68352004-12-27 21:59:20 +00004717#endif
4718
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004719 for ( ; *p; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004720 {
4721 if (*p == Ctrl_V)
4722 {
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004723 if (eap->argt & (EX_CTRLV | EX_XFILE))
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004724 ++p; // skip CTRL-V and next char
Bram Moolenaar071d4272004-06-13 20:20:40 +00004725 else
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004726 // remove CTRL-V and skip next char
Bram Moolenaara7241f52008-06-24 20:39:31 +00004727 STRMOVE(p, p + 1);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004728 if (*p == NUL) // stop at NUL after CTRL-V
Bram Moolenaar071d4272004-06-13 20:20:40 +00004729 break;
4730 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004731
4732#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004733 // Skip over `=expr` when wildcards are expanded.
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004734 else if (p[0] == '`' && p[1] == '=' && (eap->argt & EX_XFILE))
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004735 {
4736 p += 2;
Bram Moolenaar683581e2020-10-22 21:22:58 +02004737 (void)skip_expr(&p, NULL);
Bram Moolenaarcaf73dc2020-10-26 21:39:13 +01004738 if (*p == NUL) // stop at NUL after CTRL-V
4739 break;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004740 }
4741#endif
4742
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004743 // Check for '"': start of comment or '|': next command
4744 // :@" and :*" do not start a comment!
4745 // :redir @" doesn't either.
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02004746 else if ((*p == '"'
Bram Moolenaara26b9702020-04-18 19:53:28 +02004747#ifdef FEAT_EVAL
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02004748 && !in_vim9script()
4749#endif
4750 && !(eap->argt & EX_NOTRLCOM)
4751 && ((eap->cmdidx != CMD_at && eap->cmdidx != CMD_star)
4752 || p != eap->arg)
4753 && (eap->cmdidx != CMD_redir
4754 || p != eap->arg + 1 || p[-1] != '@'))
4755#ifdef FEAT_EVAL
4756 || (*p == '#'
4757 && in_vim9script()
Bram Moolenaarb8a92962020-08-20 18:02:47 +02004758 && !(eap->argt & EX_NOTRLCOM)
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02004759 && p[1] != '{'
4760 && p > eap->cmd && VIM_ISWHITE(p[-1]))
Bram Moolenaara26b9702020-04-18 19:53:28 +02004761#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004762 || *p == '|' || *p == '\n')
4763 {
4764 /*
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004765 * We remove the '\' before the '|', unless EX_CTRLV is used
Bram Moolenaar071d4272004-06-13 20:20:40 +00004766 * AND 'b' is present in 'cpoptions'.
4767 */
4768 if ((vim_strchr(p_cpo, CPO_BAR) == NULL
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004769 || !(eap->argt & EX_CTRLV)) && *(p - 1) == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004770 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004771 STRMOVE(p - 1, p); // remove the '\'
Bram Moolenaar071d4272004-06-13 20:20:40 +00004772 --p;
4773 }
4774 else
4775 {
4776 eap->nextcmd = check_nextcmd(p);
4777 *p = NUL;
4778 break;
4779 }
4780 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004781 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00004782
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004783 if (!(eap->argt & EX_NOTRLCOM)) // remove trailing spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00004784 del_trailing_spaces(eap->arg);
4785}
4786
4787/*
4788 * get + command from ex argument
4789 */
4790 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004791getargcmd(char_u **argp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004792{
4793 char_u *arg = *argp;
4794 char_u *command = NULL;
4795
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004796 if (*arg == '+') // +[command]
Bram Moolenaar071d4272004-06-13 20:20:40 +00004797 {
4798 ++arg;
Bram Moolenaar3e451592014-04-02 14:22:05 +02004799 if (vim_isspace(*arg) || *arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004800 command = dollar_command;
4801 else
4802 {
4803 command = arg;
4804 arg = skip_cmd_arg(command, TRUE);
4805 if (*arg != NUL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004806 *arg++ = NUL; // terminate command with NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004807 }
4808
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004809 arg = skipwhite(arg); // skip over spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00004810 *argp = arg;
4811 }
4812 return command;
4813}
4814
4815/*
4816 * Find end of "+command" argument. Skip over "\ " and "\\".
4817 */
Bram Moolenaard0190392019-08-23 21:17:35 +02004818 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004819skip_cmd_arg(
4820 char_u *p,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004821 int rembs) // TRUE to halve the number of backslashes
Bram Moolenaar071d4272004-06-13 20:20:40 +00004822{
4823 while (*p && !vim_isspace(*p))
4824 {
4825 if (*p == '\\' && p[1] != NUL)
4826 {
4827 if (rembs)
Bram Moolenaara7241f52008-06-24 20:39:31 +00004828 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004829 else
4830 ++p;
4831 }
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004832 MB_PTR_ADV(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004833 }
4834 return p;
4835}
4836
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004837 int
4838get_bad_opt(char_u *p, exarg_T *eap)
4839{
4840 if (STRICMP(p, "keep") == 0)
4841 eap->bad_char = BAD_KEEP;
4842 else if (STRICMP(p, "drop") == 0)
4843 eap->bad_char = BAD_DROP;
4844 else if (MB_BYTE2LEN(*p) == 1 && p[1] == NUL)
4845 eap->bad_char = *p;
Bram Moolenaar75808492018-06-12 12:39:41 +02004846 else
4847 return FAIL;
4848 return OK;
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004849}
4850
Bram Moolenaar071d4272004-06-13 20:20:40 +00004851/*
4852 * Get "++opt=arg" argument.
4853 * Return FAIL or OK.
4854 */
4855 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004856getargopt(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004857{
4858 char_u *arg = eap->arg + 2;
4859 int *pp = NULL;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02004860 int bad_char_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004861 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004862
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004863 // ":edit ++[no]bin[ary] file"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004864 if (STRNCMP(arg, "bin", 3) == 0 || STRNCMP(arg, "nobin", 5) == 0)
4865 {
4866 if (*arg == 'n')
4867 {
4868 arg += 2;
4869 eap->force_bin = FORCE_NOBIN;
4870 }
4871 else
4872 eap->force_bin = FORCE_BIN;
4873 if (!checkforcmd(&arg, "binary", 3))
4874 return FAIL;
4875 eap->arg = skipwhite(arg);
4876 return OK;
4877 }
4878
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004879 // ":read ++edit file"
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004880 if (STRNCMP(arg, "edit", 4) == 0)
4881 {
4882 eap->read_edit = TRUE;
4883 eap->arg = skipwhite(arg + 4);
4884 return OK;
4885 }
4886
Bram Moolenaar071d4272004-06-13 20:20:40 +00004887 if (STRNCMP(arg, "ff", 2) == 0)
4888 {
4889 arg += 2;
4890 pp = &eap->force_ff;
4891 }
4892 else if (STRNCMP(arg, "fileformat", 10) == 0)
4893 {
4894 arg += 10;
4895 pp = &eap->force_ff;
4896 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004897 else if (STRNCMP(arg, "enc", 3) == 0)
4898 {
Bram Moolenaarb38e9ab2011-12-14 14:49:45 +01004899 if (STRNCMP(arg, "encoding", 8) == 0)
4900 arg += 8;
4901 else
4902 arg += 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004903 pp = &eap->force_enc;
4904 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004905 else if (STRNCMP(arg, "bad", 3) == 0)
4906 {
4907 arg += 3;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02004908 pp = &bad_char_idx;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004909 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004910
4911 if (pp == NULL || *arg != '=')
4912 return FAIL;
4913
4914 ++arg;
4915 *pp = (int)(arg - eap->cmd);
4916 arg = skip_cmd_arg(arg, FALSE);
4917 eap->arg = skipwhite(arg);
4918 *arg = NUL;
4919
Bram Moolenaar071d4272004-06-13 20:20:40 +00004920 if (pp == &eap->force_ff)
4921 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004922 if (check_ff_value(eap->cmd + eap->force_ff) == FAIL)
4923 return FAIL;
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004924 eap->force_ff = eap->cmd[eap->force_ff];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004925 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004926 else if (pp == &eap->force_enc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004927 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004928 // Make 'fileencoding' lower case.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004929 for (p = eap->cmd + eap->force_enc; *p != NUL; ++p)
4930 *p = TOLOWER_ASC(*p);
4931 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004932 else
4933 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004934 // Check ++bad= argument. Must be a single-byte character, "keep" or
4935 // "drop".
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004936 if (get_bad_opt(eap->cmd + bad_char_idx, eap) == FAIL)
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004937 return FAIL;
4938 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004939
4940 return OK;
4941}
4942
Bram Moolenaar071d4272004-06-13 20:20:40 +00004943 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004944ex_autocmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004945{
4946 /*
Bram Moolenaar26d4b892018-07-04 22:26:28 +02004947 * Disallow autocommands from .exrc and .vimrc in current
Bram Moolenaar071d4272004-06-13 20:20:40 +00004948 * directory for security reasons.
4949 */
4950 if (secure)
4951 {
4952 secure = 2;
Bram Moolenaar8930caa2020-07-23 16:37:03 +02004953 eap->errmsg = _(e_curdir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004954 }
4955 else if (eap->cmdidx == CMD_autocmd)
4956 do_autocmd(eap->arg, eap->forceit);
4957 else
4958 do_augroup(eap->arg, eap->forceit);
4959}
4960
4961/*
4962 * ":doautocmd": Apply the automatic commands to the current buffer.
4963 */
4964 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004965ex_doautocmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004966{
Bram Moolenaar60542ac2012-02-12 20:14:01 +01004967 char_u *arg = eap->arg;
4968 int call_do_modelines = check_nomodeline(&arg);
Bram Moolenaar1610d052016-06-09 22:53:01 +02004969 int did_aucmd;
Bram Moolenaar60542ac2012-02-12 20:14:01 +01004970
Bram Moolenaar1610d052016-06-09 22:53:01 +02004971 (void)do_doautocmd(arg, TRUE, &did_aucmd);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004972 // Only when there is no <nomodeline>.
Bram Moolenaar1610d052016-06-09 22:53:01 +02004973 if (call_do_modelines && did_aucmd)
Bram Moolenaar60542ac2012-02-12 20:14:01 +01004974 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004975}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004976
Bram Moolenaar071d4272004-06-13 20:20:40 +00004977/*
4978 * :[N]bunload[!] [N] [bufname] unload buffer
4979 * :[N]bdelete[!] [N] [bufname] delete buffer from buffer list
4980 * :[N]bwipeout[!] [N] [bufname] delete buffer really
4981 */
4982 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004983ex_bunload(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004984{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01004985 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02004986 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004987 eap->errmsg = do_bufdel(
4988 eap->cmdidx == CMD_bdelete ? DOBUF_DEL
4989 : eap->cmdidx == CMD_bwipeout ? DOBUF_WIPE
4990 : DOBUF_UNLOAD, eap->arg,
4991 eap->addr_count, (int)eap->line1, (int)eap->line2, eap->forceit);
4992}
4993
4994/*
4995 * :[N]buffer [N] to buffer N
4996 * :[N]sbuffer [N] to buffer N
4997 */
4998 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004999ex_buffer(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005000{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01005001 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02005002 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005003 if (*eap->arg)
Bram Moolenaar8930caa2020-07-23 16:37:03 +02005004 eap->errmsg = ex_errmsg(e_trailing_arg, eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005005 else
5006 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005007 if (eap->addr_count == 0) // default is current buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00005008 goto_buffer(eap, DOBUF_CURRENT, FORWARD, 0);
5009 else
5010 goto_buffer(eap, DOBUF_FIRST, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005011 if (eap->do_ecmd_cmd != NULL)
5012 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005013 }
5014}
5015
5016/*
5017 * :[N]bmodified [N] to next mod. buffer
5018 * :[N]sbmodified [N] to next mod. buffer
5019 */
5020 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005021ex_bmodified(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005022{
5023 goto_buffer(eap, DOBUF_MOD, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005024 if (eap->do_ecmd_cmd != NULL)
5025 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005026}
5027
5028/*
5029 * :[N]bnext [N] to next buffer
5030 * :[N]sbnext [N] split and to next buffer
5031 */
5032 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005033ex_bnext(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005034{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01005035 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar3029bcc2020-01-18 15:06:19 +01005036 return;
5037
Bram Moolenaar071d4272004-06-13 20:20:40 +00005038 goto_buffer(eap, DOBUF_CURRENT, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005039 if (eap->do_ecmd_cmd != NULL)
5040 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005041}
5042
5043/*
5044 * :[N]bNext [N] to previous buffer
5045 * :[N]bprevious [N] to previous buffer
5046 * :[N]sbNext [N] split and to previous buffer
5047 * :[N]sbprevious [N] split and to previous buffer
5048 */
5049 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005050ex_bprevious(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005051{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01005052 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar3029bcc2020-01-18 15:06:19 +01005053 return;
5054
Bram Moolenaar071d4272004-06-13 20:20:40 +00005055 goto_buffer(eap, DOBUF_CURRENT, BACKWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005056 if (eap->do_ecmd_cmd != NULL)
5057 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005058}
5059
5060/*
5061 * :brewind to first buffer
5062 * :bfirst to first buffer
5063 * :sbrewind split and to first buffer
5064 * :sbfirst split and to first buffer
5065 */
5066 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005067ex_brewind(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005068{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01005069 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar3029bcc2020-01-18 15:06:19 +01005070 return;
5071
Bram Moolenaar071d4272004-06-13 20:20:40 +00005072 goto_buffer(eap, DOBUF_FIRST, FORWARD, 0);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005073 if (eap->do_ecmd_cmd != NULL)
5074 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005075}
5076
5077/*
5078 * :blast to last buffer
5079 * :sblast split and to last buffer
5080 */
5081 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005082ex_blast(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005083{
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01005084 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar3029bcc2020-01-18 15:06:19 +01005085 return;
5086
Bram Moolenaar071d4272004-06-13 20:20:40 +00005087 goto_buffer(eap, DOBUF_LAST, BACKWARD, 0);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005088 if (eap->do_ecmd_cmd != NULL)
5089 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005090}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005091
Bram Moolenaar7a092242020-04-16 22:10:49 +02005092/*
5093 * Check if "c" ends an Ex command.
Bram Moolenaara494f562020-04-18 17:45:38 +02005094 * In Vim9 script does not check for white space before # or #{.
Bram Moolenaar7a092242020-04-16 22:10:49 +02005095 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005096 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005097ends_excmd(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005098{
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005099 int comment_char = '"';
5100
Bram Moolenaarfbda69b2020-04-13 15:06:53 +02005101#ifdef FEAT_EVAL
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005102 if (in_vim9script())
5103 comment_char = '#';
Bram Moolenaar7a092242020-04-16 22:10:49 +02005104#endif
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005105 return (c == NUL || c == '|' || c == comment_char || c == '\n');
Bram Moolenaar7a092242020-04-16 22:10:49 +02005106}
5107
5108/*
5109 * Like ends_excmd() but checks that a # in Vim9 script either has "cmd" equal
5110 * to "cmd_start" or has a white space character before it.
5111 */
5112 int
Bram Moolenaar21cfe502020-04-16 23:01:50 +02005113ends_excmd2(char_u *cmd_start UNUSED, char_u *cmd)
Bram Moolenaar7a092242020-04-16 22:10:49 +02005114{
5115 int c = *cmd;
5116
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005117 if (c == NUL || c == '|' || c == '\n')
5118 return TRUE;
Bram Moolenaar7a092242020-04-16 22:10:49 +02005119#ifdef FEAT_EVAL
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005120 if (in_vim9script())
5121 return c == '#' && cmd[1] != '{'
5122 && (cmd == cmd_start || VIM_ISWHITE(cmd[-1]));
Bram Moolenaarfbda69b2020-04-13 15:06:53 +02005123#endif
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02005124 return c == '"';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005125}
5126
5127#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA) || defined(FEAT_EVAL) \
5128 || defined(PROTO)
5129/*
5130 * Return the next command, after the first '|' or '\n'.
5131 * Return NULL if not found.
5132 */
5133 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005134find_nextcmd(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005135{
5136 while (*p != '|' && *p != '\n')
5137 {
5138 if (*p == NUL)
5139 return NULL;
5140 ++p;
5141 }
5142 return (p + 1);
5143}
5144#endif
5145
5146/*
Bram Moolenaar2256c992016-11-15 21:17:07 +01005147 * Check if *p is a separator between Ex commands, skipping over white space.
5148 * Return NULL if it isn't, the following character if it is.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005149 */
5150 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005151check_nextcmd(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005152{
Bram Moolenaar2256c992016-11-15 21:17:07 +01005153 char_u *s = skipwhite(p);
5154
5155 if (*s == '|' || *s == '\n')
5156 return (s + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005157 else
5158 return NULL;
5159}
5160
5161/*
5162 * - if there are more files to edit
5163 * - and this is the last window
5164 * - and forceit not used
5165 * - and not repeated twice on a row
5166 * return FAIL and give error message if 'message' TRUE
5167 * return OK otherwise
5168 */
5169 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005170check_more(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005171 int message, // when FALSE check only, no messages
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005172 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005173{
5174 int n = ARGCOUNT - curwin->w_arg_idx - 1;
5175
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005176 if (!forceit && only_one_window()
Bram Moolenaar7b221172020-08-17 19:34:10 +02005177 && ARGCOUNT > 1 && !arg_had_last && n > 0 && quitmore == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005178 {
5179 if (message)
5180 {
5181#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaare1004402020-10-24 20:49:43 +02005182 if ((p_confirm || (cmdmod.cmod_flags & CMOD_CONFIRM))
5183 && curbuf->b_fname != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005184 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02005185 char_u buff[DIALOG_MSG_SIZE];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005186
Bram Moolenaarda6e8912018-08-21 15:12:14 +02005187 vim_snprintf((char *)buff, DIALOG_MSG_SIZE,
5188 NGETTEXT("%d more file to edit. Quit anyway?",
5189 "%d more files to edit. Quit anyway?", n), n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005190 if (vim_dialog_yesno(VIM_QUESTION, NULL, buff, 1) == VIM_YES)
5191 return OK;
5192 return FAIL;
5193 }
5194#endif
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01005195 semsg(NGETTEXT("E173: %d more file to edit",
5196 "E173: %d more files to edit", n), n);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005197 quitmore = 2; // next try to quit is allowed
Bram Moolenaar071d4272004-06-13 20:20:40 +00005198 }
5199 return FAIL;
5200 }
5201 return OK;
5202}
5203
Bram Moolenaar071d4272004-06-13 20:20:40 +00005204/*
5205 * Function given to ExpandGeneric() to obtain the list of command names.
5206 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005207 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005208get_command_name(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005209{
5210 if (idx >= (int)CMD_SIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005211 return get_user_command_name(idx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005212 return cmdnames[idx].cmd_name;
5213}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005214
Bram Moolenaar071d4272004-06-13 20:20:40 +00005215 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005216ex_colorscheme(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005217{
Bram Moolenaare6850792010-05-14 15:28:44 +02005218 if (*eap->arg == NUL)
5219 {
5220#ifdef FEAT_EVAL
5221 char_u *expr = vim_strsave((char_u *)"g:colors_name");
5222 char_u *p = NULL;
5223
5224 if (expr != NULL)
5225 {
5226 ++emsg_off;
Bram Moolenaarb171fb12020-06-24 20:34:03 +02005227 p = eval_to_string(expr, FALSE);
Bram Moolenaare6850792010-05-14 15:28:44 +02005228 --emsg_off;
5229 vim_free(expr);
5230 }
5231 if (p != NULL)
5232 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01005233 msg((char *)p);
Bram Moolenaare6850792010-05-14 15:28:44 +02005234 vim_free(p);
5235 }
5236 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01005237 msg("default");
Bram Moolenaare6850792010-05-14 15:28:44 +02005238#else
Bram Moolenaar32526b32019-01-19 17:43:09 +01005239 msg(_("unknown"));
Bram Moolenaare6850792010-05-14 15:28:44 +02005240#endif
5241 }
5242 else if (load_colors(eap->arg) == FAIL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005243 semsg(_("E185: Cannot find color scheme '%s'"), eap->arg);
Bram Moolenaarf58d81a2019-01-28 20:19:05 +01005244
5245#ifdef FEAT_VTP
5246 else if (has_vtp_working())
5247 {
5248 // background color change requires clear + redraw
5249 update_screen(CLEAR);
5250 redrawcmd();
5251 }
5252#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005253}
5254
5255 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005256ex_highlight(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005257{
5258 if (*eap->arg == NUL && eap->cmd[2] == '!')
Bram Moolenaar32526b32019-01-19 17:43:09 +01005259 msg(_("Greetings, Vim user!"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005260 do_highlight(eap->arg, eap->forceit, FALSE);
5261}
5262
5263
5264/*
5265 * Call this function if we thought we were going to exit, but we won't
5266 * (because of an error). May need to restore the terminal mode.
5267 */
5268 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005269not_exiting(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005270{
5271 exiting = FALSE;
5272 settmode(TMODE_RAW);
5273}
5274
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005275 static int
5276before_quit_autocmds(win_T *wp, int quit_all, int forceit)
5277{
5278 apply_autocmds(EVENT_QUITPRE, NULL, NULL, FALSE, wp->w_buffer);
5279
Bram Moolenaar34ba06b2019-10-20 22:27:10 +02005280 // Bail out when autocommands closed the window.
5281 // Refuse to quit when the buffer in the last window is being closed (can
5282 // only happen in autocommands).
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005283 if (!win_valid(wp)
5284 || curbuf_locked()
5285 || (wp->w_buffer->b_nwindows == 1 && wp->w_buffer->b_locked > 0))
5286 return TRUE;
5287
5288 if (quit_all || (check_more(FALSE, forceit) == OK && only_one_window()))
5289 {
5290 apply_autocmds(EVENT_EXITPRE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar34ba06b2019-10-20 22:27:10 +02005291 // Refuse to quit when locked or when the window was closed or the
5292 // buffer in the last window is being closed (can only happen in
5293 // autocommands).
5294 if (!win_valid(wp) || curbuf_locked()
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005295 || (curbuf->b_nwindows == 1 && curbuf->b_locked > 0))
5296 return TRUE;
5297 }
5298
5299 return FALSE;
5300}
5301
Bram Moolenaar071d4272004-06-13 20:20:40 +00005302/*
Bram Moolenaarf240e182014-11-27 18:33:02 +01005303 * ":quit": quit current window, quit Vim if the last window is closed.
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005304 * ":{nr}quit": quit window {nr}
Bram Moolenaar4d14bac2019-10-20 21:15:15 +02005305 * Also used when closing a terminal window that's the last one.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005306 */
Bram Moolenaar4d14bac2019-10-20 21:15:15 +02005307 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005308ex_quit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005309{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005310 win_T *wp;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005311
Bram Moolenaar071d4272004-06-13 20:20:40 +00005312#ifdef FEAT_CMDWIN
5313 if (cmdwin_type != 0)
5314 {
5315 cmdwin_result = Ctrl_C;
5316 return;
5317 }
5318#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005319 // Don't quit while editing the command line.
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005320 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005321 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005322 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005323 return;
5324 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005325 if (eap->addr_count > 0)
5326 {
Bram Moolenaarf240e182014-11-27 18:33:02 +01005327 int wnr = eap->line2;
5328
5329 for (wp = firstwin; wp->w_next != NULL; wp = wp->w_next)
5330 if (--wnr <= 0)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005331 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005332 }
5333 else
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005334 wp = curwin;
Bram Moolenaarf240e182014-11-27 18:33:02 +01005335
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005336 // Refuse to quit when locked.
Bram Moolenaar87ffb5c2017-10-19 12:37:42 +02005337 if (curbuf_locked())
5338 return;
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005339
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005340 // Trigger QuitPre and maybe ExitPre
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005341 if (before_quit_autocmds(wp, FALSE, eap->forceit))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005342 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005343
5344#ifdef FEAT_NETBEANS_INTG
5345 netbeansForcedQuit = eap->forceit;
5346#endif
5347
5348 /*
5349 * If there are more files or windows we won't exit.
5350 */
5351 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
5352 exiting = TRUE;
Bram Moolenaarff930ca2017-10-19 17:12:10 +02005353 if ((!buf_hide(wp->w_buffer)
5354 && check_changed(wp->w_buffer, (p_awa ? CCGD_AW : 0)
Bram Moolenaar45d3b142013-11-09 03:31:51 +01005355 | (eap->forceit ? CCGD_FORCEIT : 0)
5356 | CCGD_EXCMD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005357 || check_more(TRUE, eap->forceit) == FAIL
Bram Moolenaar027387f2016-01-02 22:25:52 +01005358 || (only_one_window() && check_changed_any(eap->forceit, TRUE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005359 {
5360 not_exiting();
5361 }
5362 else
5363 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005364 // quit last window
5365 // Note: only_one_window() returns true, even so a help window is
5366 // still open. In that case only quit, if no address has been
5367 // specified. Example:
5368 // :h|wincmd w|1q - don't quit
5369 // :h|wincmd w|q - quit
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01005370 if (only_one_window() && (ONE_WINDOW || eap->addr_count == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005371 getout(0);
Bram Moolenaar2c33d7b2017-10-14 16:06:20 +02005372 not_exiting();
Bram Moolenaar4033c552017-09-16 20:54:51 +02005373#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00005374 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005375#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005376 // close window; may free buffer
Bram Moolenaareb44a682017-08-03 22:44:55 +02005377 win_close(wp, !buf_hide(wp->w_buffer) || eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005378 }
5379}
5380
5381/*
5382 * ":cquit".
5383 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005384 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005385ex_cquit(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005386{
Bram Moolenaar1860bde2020-01-06 21:47:21 +01005387 // this does not always pass on the exit code to the Manx compiler. why?
5388 getout(eap->addr_count > 0 ? (int)eap->line2 : EXIT_FAILURE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005389}
5390
5391/*
5392 * ":qall": try to quit all windows
5393 */
5394 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005395ex_quit_all(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005396{
5397# ifdef FEAT_CMDWIN
5398 if (cmdwin_type != 0)
5399 {
5400 if (eap->forceit)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005401 cmdwin_result = K_XF1; // ex_window() takes care of this
Bram Moolenaar071d4272004-06-13 20:20:40 +00005402 else
5403 cmdwin_result = K_XF2;
5404 return;
5405 }
5406# endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005407
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005408 // Don't quit while editing the command line.
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005409 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005410 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005411 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005412 return;
5413 }
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005414
5415 if (before_quit_autocmds(curwin, TRUE, eap->forceit))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005416 return;
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005417
Bram Moolenaar071d4272004-06-13 20:20:40 +00005418 exiting = TRUE;
Bram Moolenaar027387f2016-01-02 22:25:52 +01005419 if (eap->forceit || !check_changed_any(FALSE, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005420 getout(0);
5421 not_exiting();
5422}
5423
Bram Moolenaar071d4272004-06-13 20:20:40 +00005424/*
5425 * ":close": close current window, unless it is the last one
5426 */
5427 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005428ex_close(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005429{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005430 win_T *win;
5431 int winnr = 0;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005432#ifdef FEAT_CMDWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00005433 if (cmdwin_type != 0)
Bram Moolenaar9bd1a7e2011-05-19 14:50:54 +02005434 cmdwin_result = Ctrl_C;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005435 else
Bram Moolenaar4033c552017-09-16 20:54:51 +02005436#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005437 if (!text_locked() && !curbuf_locked())
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005438 {
5439 if (eap->addr_count == 0)
5440 ex_win_close(eap->forceit, curwin, NULL);
Bram Moolenaar6d41c782018-06-06 09:11:12 +02005441 else
5442 {
Bram Moolenaar29323592016-07-24 22:04:11 +02005443 FOR_ALL_WINDOWS(win)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005444 {
5445 winnr++;
5446 if (winnr == eap->line2)
5447 break;
5448 }
5449 if (win == NULL)
5450 win = lastwin;
5451 ex_win_close(eap->forceit, win, NULL);
5452 }
5453 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005454}
5455
Bram Moolenaar4033c552017-09-16 20:54:51 +02005456#ifdef FEAT_QUICKFIX
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005457/*
5458 * ":pclose": Close any preview window.
5459 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005460 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005461ex_pclose(exarg_T *eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005462{
5463 win_T *win;
5464
Bram Moolenaar79648732019-07-18 21:43:07 +02005465 // First close any normal window.
Bram Moolenaar29323592016-07-24 22:04:11 +02005466 FOR_ALL_WINDOWS(win)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005467 if (win->w_p_pvw)
5468 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00005469 ex_win_close(eap->forceit, win, NULL);
Bram Moolenaar79648732019-07-18 21:43:07 +02005470 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005471 }
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01005472# ifdef FEAT_PROP_POPUP
Bram Moolenaar79648732019-07-18 21:43:07 +02005473 // Also when 'previewpopup' is empty, it might have been cleared.
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02005474 popup_close_preview();
Bram Moolenaar79648732019-07-18 21:43:07 +02005475# endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005476}
Bram Moolenaar4033c552017-09-16 20:54:51 +02005477#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005478
Bram Moolenaarf740b292006-02-16 22:11:02 +00005479/*
5480 * Close window "win" and take care of handling closing the last window for a
5481 * modified buffer.
5482 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005483 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005484ex_win_close(
5485 int forceit,
5486 win_T *win,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005487 tabpage_T *tp) // NULL or the tab page "win" is in
Bram Moolenaar071d4272004-06-13 20:20:40 +00005488{
5489 int need_hide;
5490 buf_T *buf = win->w_buffer;
5491
Bram Moolenaarcf844172020-06-26 19:44:06 +02005492 // Never close the autocommand window.
5493 if (win == aucmd_win)
5494 {
5495 emsg(_(e_autocmd_close));
5496 return;
5497 }
5498
Bram Moolenaar071d4272004-06-13 20:20:40 +00005499 need_hide = (bufIsChanged(buf) && buf->b_nwindows <= 1);
Bram Moolenaareb44a682017-08-03 22:44:55 +02005500 if (need_hide && !buf_hide(buf) && !forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005501 {
Bram Moolenaar4033c552017-09-16 20:54:51 +02005502#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaare1004402020-10-24 20:49:43 +02005503 if ((p_confirm || (cmdmod.cmod_flags & CMOD_CONFIRM)) && p_write)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005504 {
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005505 bufref_T bufref;
5506
5507 set_bufref(&bufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005508 dialog_changed(buf, FALSE);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005509 if (bufref_valid(&bufref) && bufIsChanged(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005510 return;
5511 need_hide = FALSE;
5512 }
5513 else
Bram Moolenaar4033c552017-09-16 20:54:51 +02005514#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005515 {
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02005516 no_write_message();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005517 return;
5518 }
5519 }
5520
Bram Moolenaar4033c552017-09-16 20:54:51 +02005521#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00005522 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005523#endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00005524
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005525 // free buffer when not hiding it or when it's a scratch buffer
Bram Moolenaarf740b292006-02-16 22:11:02 +00005526 if (tp == NULL)
Bram Moolenaareb44a682017-08-03 22:44:55 +02005527 win_close(win, !need_hide && !buf_hide(buf));
Bram Moolenaarf740b292006-02-16 22:11:02 +00005528 else
Bram Moolenaareb44a682017-08-03 22:44:55 +02005529 win_close_othertab(win, !need_hide && !buf_hide(buf), tp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005530}
5531
Bram Moolenaar071d4272004-06-13 20:20:40 +00005532/*
Bram Moolenaardea25702017-01-29 15:18:10 +01005533 * Handle the argument for a tabpage related ex command.
5534 * Returns a tabpage number.
5535 * When an error is encountered then eap->errmsg is set.
5536 */
5537 static int
5538get_tabpage_arg(exarg_T *eap)
5539{
5540 int tab_number;
5541 int unaccept_arg0 = (eap->cmdidx == CMD_tabmove) ? 0 : 1;
5542
5543 if (eap->arg && *eap->arg != NUL)
5544 {
5545 char_u *p = eap->arg;
5546 char_u *p_save;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005547 int relative = 0; // argument +N/-N means: go to N places to the
5548 // right/left relative to the current position.
Bram Moolenaardea25702017-01-29 15:18:10 +01005549
5550 if (*p == '-')
5551 {
5552 relative = -1;
5553 p++;
5554 }
5555 else if (*p == '+')
5556 {
5557 relative = 1;
5558 p++;
5559 }
5560
5561 p_save = p;
5562 tab_number = getdigits(&p);
5563
5564 if (relative == 0)
5565 {
5566 if (STRCMP(p, "$") == 0)
5567 tab_number = LAST_TAB_NR;
Bram Moolenaar94f4ffa2020-08-10 19:21:15 +02005568 else if (STRCMP(p, "#") == 0)
5569 if (valid_tabpage(lastused_tabpage))
5570 tab_number = tabpage_index(lastused_tabpage);
5571 else
5572 {
5573 eap->errmsg = ex_errmsg(e_invargval, eap->arg);
5574 tab_number = 0;
5575 goto theend;
5576 }
Bram Moolenaardea25702017-01-29 15:18:10 +01005577 else if (p == p_save || *p_save == '-' || *p != NUL
5578 || tab_number > LAST_TAB_NR)
5579 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005580 // No numbers as argument.
Bram Moolenaar8930caa2020-07-23 16:37:03 +02005581 eap->errmsg = ex_errmsg(e_invarg2, eap->arg);
Bram Moolenaardea25702017-01-29 15:18:10 +01005582 goto theend;
5583 }
5584 }
5585 else
5586 {
5587 if (*p_save == NUL)
5588 tab_number = 1;
5589 else if (p == p_save || *p_save == '-' || *p != NUL
5590 || tab_number == 0)
5591 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005592 // No numbers as argument.
Bram Moolenaar8930caa2020-07-23 16:37:03 +02005593 eap->errmsg = ex_errmsg(e_invarg2, eap->arg);
Bram Moolenaardea25702017-01-29 15:18:10 +01005594 goto theend;
5595 }
5596 tab_number = tab_number * relative + tabpage_index(curtab);
5597 if (!unaccept_arg0 && relative == -1)
5598 --tab_number;
5599 }
5600 if (tab_number < unaccept_arg0 || tab_number > LAST_TAB_NR)
Bram Moolenaar8930caa2020-07-23 16:37:03 +02005601 eap->errmsg = ex_errmsg(e_invarg2, eap->arg);
Bram Moolenaardea25702017-01-29 15:18:10 +01005602 }
5603 else if (eap->addr_count > 0)
5604 {
5605 if (unaccept_arg0 && eap->line2 == 0)
Bram Moolenaarc6251552017-01-29 21:42:20 +01005606 {
Bram Moolenaar8930caa2020-07-23 16:37:03 +02005607 eap->errmsg = _(e_invrange);
Bram Moolenaarc6251552017-01-29 21:42:20 +01005608 tab_number = 0;
5609 }
Bram Moolenaardea25702017-01-29 15:18:10 +01005610 else
5611 {
5612 tab_number = eap->line2;
Bram Moolenaar82a12462019-01-22 22:41:42 +01005613 if (!unaccept_arg0 && *skipwhite(*eap->cmdlinep) == '-')
Bram Moolenaardea25702017-01-29 15:18:10 +01005614 {
5615 --tab_number;
5616 if (tab_number < unaccept_arg0)
Bram Moolenaar8930caa2020-07-23 16:37:03 +02005617 eap->errmsg = _(e_invrange);
Bram Moolenaardea25702017-01-29 15:18:10 +01005618 }
5619 }
5620 }
5621 else
5622 {
5623 switch (eap->cmdidx)
5624 {
5625 case CMD_tabnext:
5626 tab_number = tabpage_index(curtab) + 1;
5627 if (tab_number > LAST_TAB_NR)
5628 tab_number = 1;
5629 break;
5630 case CMD_tabmove:
5631 tab_number = LAST_TAB_NR;
5632 break;
5633 default:
5634 tab_number = tabpage_index(curtab);
5635 }
5636 }
5637
5638theend:
5639 return tab_number;
5640}
5641
5642/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00005643 * ":tabclose": close current tab page, unless it is the last one.
5644 * ":tabclose N": close tab page N.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005645 */
5646 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005647ex_tabclose(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005648{
Bram Moolenaarf740b292006-02-16 22:11:02 +00005649 tabpage_T *tp;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005650 int tab_number;
Bram Moolenaarf740b292006-02-16 22:11:02 +00005651
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005652# ifdef FEAT_CMDWIN
5653 if (cmdwin_type != 0)
5654 cmdwin_result = K_IGNORE;
5655 else
5656# endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00005657 if (first_tabpage->tp_next == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005658 emsg(_("E784: Cannot close last tab page"));
Bram Moolenaarf740b292006-02-16 22:11:02 +00005659 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005660 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005661 tab_number = get_tabpage_arg(eap);
5662 if (eap->errmsg == NULL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005663 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005664 tp = find_tabpage(tab_number);
Bram Moolenaarf740b292006-02-16 22:11:02 +00005665 if (tp == NULL)
5666 {
5667 beep_flush();
5668 return;
5669 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005670 if (tp != curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00005671 {
5672 tabpage_close_other(tp, eap->forceit);
5673 return;
5674 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005675 else if (!text_locked() && !curbuf_locked())
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005676 tabpage_close(eap->forceit);
5677 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005678 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005679}
5680
5681/*
5682 * ":tabonly": close all tab pages except the current one
5683 */
5684 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005685ex_tabonly(exarg_T *eap)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005686{
5687 tabpage_T *tp;
5688 int done;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005689 int tab_number;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005690
5691# ifdef FEAT_CMDWIN
5692 if (cmdwin_type != 0)
5693 cmdwin_result = K_IGNORE;
5694 else
5695# endif
5696 if (first_tabpage->tp_next == NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01005697 msg(_("Already only one tab page"));
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005698 else
5699 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005700 tab_number = get_tabpage_arg(eap);
5701 if (eap->errmsg == NULL)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005702 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005703 goto_tabpage(tab_number);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005704 // Repeat this up to a 1000 times, because autocommands may
5705 // mess up the lists.
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005706 for (done = 0; done < 1000; ++done)
5707 {
5708 FOR_ALL_TABPAGES(tp)
5709 if (tp->tp_topframe != topframe)
5710 {
5711 tabpage_close_other(tp, eap->forceit);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005712 // if we failed to close it quit
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005713 if (valid_tabpage(tp))
5714 done = 1000;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005715 // start over, "tp" is now invalid
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005716 break;
5717 }
5718 if (first_tabpage->tp_next == NULL)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005719 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005720 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005721 }
5722 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005723}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005724
5725/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00005726 * Close the current tab page.
5727 */
5728 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005729tabpage_close(int forceit)
Bram Moolenaarf740b292006-02-16 22:11:02 +00005730{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005731 // First close all the windows but the current one. If that worked then
5732 // close the last window in this tab, that will close it.
Bram Moolenaar459ca562016-11-10 18:16:33 +01005733 if (!ONE_WINDOW)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005734 close_others(TRUE, forceit);
Bram Moolenaar459ca562016-11-10 18:16:33 +01005735 if (ONE_WINDOW)
Bram Moolenaarf740b292006-02-16 22:11:02 +00005736 ex_win_close(forceit, curwin, NULL);
5737# ifdef FEAT_GUI
5738 need_mouse_correct = TRUE;
5739# endif
5740}
5741
5742/*
5743 * Close tab page "tp", which is not the current tab page.
5744 * Note that autocommands may make "tp" invalid.
Bram Moolenaar7875acc2006-09-10 13:51:17 +00005745 * Also takes care of the tab pages line disappearing when closing the
5746 * last-but-one tab page.
Bram Moolenaarf740b292006-02-16 22:11:02 +00005747 */
5748 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005749tabpage_close_other(tabpage_T *tp, int forceit)
Bram Moolenaarf740b292006-02-16 22:11:02 +00005750{
5751 int done = 0;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005752 win_T *wp;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00005753 int h = tabline_height();
Bram Moolenaarf740b292006-02-16 22:11:02 +00005754
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005755 // Limit to 1000 windows, autocommands may add a window while we close
5756 // one. OK, so I'm paranoid...
Bram Moolenaarf740b292006-02-16 22:11:02 +00005757 while (++done < 1000)
5758 {
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005759 wp = tp->tp_firstwin;
5760 ex_win_close(forceit, wp, tp);
Bram Moolenaarf740b292006-02-16 22:11:02 +00005761
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005762 // Autocommands may delete the tab page under our fingers and we may
5763 // fail to close a window with a modified buffer.
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005764 if (!valid_tabpage(tp) || tp->tp_firstwin == wp)
Bram Moolenaarf740b292006-02-16 22:11:02 +00005765 break;
5766 }
Bram Moolenaar7875acc2006-09-10 13:51:17 +00005767
Bram Moolenaar29323592016-07-24 22:04:11 +02005768 apply_autocmds(EVENT_TABCLOSED, NULL, NULL, FALSE, curbuf);
Bram Moolenaar29323592016-07-24 22:04:11 +02005769
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005770 redraw_tabline = TRUE;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00005771 if (h != tabline_height())
5772 shell_new_rows();
Bram Moolenaarf740b292006-02-16 22:11:02 +00005773}
5774
5775/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005776 * ":only".
5777 */
5778 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005779ex_only(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005780{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005781 win_T *wp;
5782 int wnr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005783# ifdef FEAT_GUI
5784 need_mouse_correct = TRUE;
5785# endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005786 if (eap->addr_count > 0)
5787 {
5788 wnr = eap->line2;
5789 for (wp = firstwin; --wnr > 0; )
5790 {
5791 if (wp->w_next == NULL)
5792 break;
5793 else
5794 wp = wp->w_next;
5795 }
5796 win_goto(wp);
5797 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005798 close_others(TRUE, eap->forceit);
5799}
5800
Bram Moolenaar071d4272004-06-13 20:20:40 +00005801 static void
Bram Moolenaar5e1e6d22017-01-02 17:26:00 +01005802ex_hide(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005803{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005804 // ":hide" or ":hide | cmd": hide current window
Bram Moolenaar2256c992016-11-15 21:17:07 +01005805 if (!eap->skip)
5806 {
Bram Moolenaar4033c552017-09-16 20:54:51 +02005807#ifdef FEAT_GUI
Bram Moolenaar2256c992016-11-15 21:17:07 +01005808 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005809#endif
Bram Moolenaar2256c992016-11-15 21:17:07 +01005810 if (eap->addr_count == 0)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005811 win_close(curwin, FALSE); // don't free buffer
Bram Moolenaar2256c992016-11-15 21:17:07 +01005812 else
5813 {
5814 int winnr = 0;
5815 win_T *win;
Bram Moolenaarf240e182014-11-27 18:33:02 +01005816
Bram Moolenaar2256c992016-11-15 21:17:07 +01005817 FOR_ALL_WINDOWS(win)
5818 {
5819 winnr++;
5820 if (winnr == eap->line2)
5821 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005822 }
Bram Moolenaar2256c992016-11-15 21:17:07 +01005823 if (win == NULL)
5824 win = lastwin;
5825 win_close(win, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005826 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005827 }
5828}
5829
5830/*
5831 * ":stop" and ":suspend": Suspend Vim.
5832 */
5833 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005834ex_stop(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005835{
5836 /*
5837 * Disallow suspending for "rvim".
5838 */
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005839 if (!check_restricted())
Bram Moolenaar071d4272004-06-13 20:20:40 +00005840 {
5841 if (!eap->forceit)
5842 autowrite_all();
5843 windgoto((int)Rows - 1, 0);
5844 out_char('\n');
5845 out_flush();
5846 stoptermcap();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005847 out_flush(); // needed for SUN to restore xterm buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00005848#ifdef FEAT_TITLE
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005849 mch_restore_title(SAVE_RESTORE_BOTH); // restore window titles
Bram Moolenaar071d4272004-06-13 20:20:40 +00005850#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005851 ui_suspend(); // call machine specific function
Bram Moolenaar071d4272004-06-13 20:20:40 +00005852#ifdef FEAT_TITLE
5853 maketitle();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005854 resettitle(); // force updating the title
Bram Moolenaar071d4272004-06-13 20:20:40 +00005855#endif
5856 starttermcap();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005857 scroll_start(); // scroll screen before redrawing
Bram Moolenaar071d4272004-06-13 20:20:40 +00005858 redraw_later_clear();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005859 shell_resized(); // may have resized window
Bram Moolenaar071d4272004-06-13 20:20:40 +00005860 }
5861}
5862
5863/*
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005864 * ":exit", ":xit" and ":wq": Write file and quite the current window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005865 */
5866 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005867ex_exit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005868{
Bram Moolenaar461f2122020-07-28 20:25:47 +02005869#ifdef FEAT_EVAL
Bram Moolenaarae616492020-07-28 20:07:27 +02005870 if (not_in_vim9(eap) == FAIL)
5871 return;
Bram Moolenaar461f2122020-07-28 20:25:47 +02005872#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005873#ifdef FEAT_CMDWIN
5874 if (cmdwin_type != 0)
5875 {
5876 cmdwin_result = Ctrl_C;
5877 return;
5878 }
5879#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005880 // Don't quit while editing the command line.
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005881 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005882 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005883 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005884 return;
5885 }
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005886
5887 if (before_quit_autocmds(curwin, FALSE, eap->forceit))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005888 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005889
5890 /*
5891 * if more files or windows we won't exit
5892 */
5893 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
5894 exiting = TRUE;
5895 if ( ((eap->cmdidx == CMD_wq
5896 || curbufIsChanged())
5897 && do_write(eap) == FAIL)
5898 || check_more(TRUE, eap->forceit) == FAIL
Bram Moolenaar027387f2016-01-02 22:25:52 +01005899 || (only_one_window() && check_changed_any(eap->forceit, FALSE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005900 {
5901 not_exiting();
5902 }
5903 else
5904 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005905 if (only_one_window()) // quit last window, exit Vim
Bram Moolenaar071d4272004-06-13 20:20:40 +00005906 getout(0);
Bram Moolenaar2c33d7b2017-10-14 16:06:20 +02005907 not_exiting();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005908# ifdef FEAT_GUI
5909 need_mouse_correct = TRUE;
5910# endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005911 // Quit current window, may free the buffer.
Bram Moolenaareb44a682017-08-03 22:44:55 +02005912 win_close(curwin, !buf_hide(curwin->w_buffer));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005913 }
5914}
5915
5916/*
5917 * ":print", ":list", ":number".
5918 */
5919 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005920ex_print(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005921{
Bram Moolenaardf177f62005-02-22 08:39:57 +00005922 if (curbuf->b_ml.ml_flags & ML_EMPTY)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005923 emsg(_(e_emptybuf));
Bram Moolenaardf177f62005-02-22 08:39:57 +00005924 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005925 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00005926 for ( ;!got_int; ui_breakcheck())
5927 {
5928 print_line(eap->line1,
5929 (eap->cmdidx == CMD_number || eap->cmdidx == CMD_pound
5930 || (eap->flags & EXFLAG_NR)),
5931 eap->cmdidx == CMD_list || (eap->flags & EXFLAG_LIST));
5932 if (++eap->line1 > eap->line2)
5933 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005934 out_flush(); // show one line at a time
Bram Moolenaardf177f62005-02-22 08:39:57 +00005935 }
5936 setpcmark();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005937 // put cursor at last line
Bram Moolenaardf177f62005-02-22 08:39:57 +00005938 curwin->w_cursor.lnum = eap->line2;
5939 beginline(BL_SOL | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005940 }
5941
Bram Moolenaar071d4272004-06-13 20:20:40 +00005942 ex_no_reprint = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005943}
5944
5945#ifdef FEAT_BYTEOFF
5946 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005947ex_goto(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005948{
5949 goto_byte(eap->line2);
5950}
5951#endif
5952
5953/*
5954 * ":shell".
5955 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005956 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005957ex_shell(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005958{
5959 do_shell(NULL, 0);
5960}
5961
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005962#if defined(HAVE_DROP_FILE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005963
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005964static int drop_busy = FALSE;
5965static int drop_filec;
5966static char_u **drop_filev = NULL;
5967static int drop_split;
5968static void (*drop_callback)(void *);
5969static void *drop_cookie;
5970
5971 static void
5972handle_drop_internal(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005973{
5974 exarg_T ea;
5975 int save_msg_scroll = msg_scroll;
5976
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005977 // Setting the argument list may cause screen updates and being called
5978 // recursively. Avoid that by setting drop_busy.
5979 drop_busy = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005980
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005981 // Check whether the current buffer is changed. If so, we will need
5982 // to split the current window or data could be lost.
5983 // We don't need to check if the 'hidden' option is set, as in this
5984 // case the buffer won't be lost.
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005985 if (!buf_hide(curbuf) && !drop_split)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005986 {
5987 ++emsg_off;
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005988 drop_split = check_changed(curbuf, CCGD_AW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005989 --emsg_off;
5990 }
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005991 if (drop_split)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005992 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005993 if (win_split(0, 0) == FAIL)
5994 return;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02005995 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005996
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005997 // When splitting the window, create a new alist. Otherwise the
5998 // existing one is overwritten.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005999 alist_unlink(curwin->w_alist);
6000 alist_new();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006001 }
6002
6003 /*
6004 * Set up the new argument list.
6005 */
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006006 alist_set(ALIST(curwin), drop_filec, drop_filev, FALSE, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006007
6008 /*
6009 * Move to the first file.
6010 */
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006011 // Fake up a minimal "next" command for do_argfile()
Bram Moolenaara80faa82020-04-12 19:37:17 +02006012 CLEAR_FIELD(ea);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006013 ea.cmd = (char_u *)"next";
6014 do_argfile(&ea, 0);
6015
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006016 // do_ecmd() may set need_start_insertmode, but since we never left Insert
6017 // mode that is not needed here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006018 need_start_insertmode = FALSE;
6019
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006020 // Restore msg_scroll, otherwise a following command may cause scrolling
6021 // unexpectedly. The screen will be redrawn by the caller, thus
6022 // msg_scroll being set by displaying a message is irrelevant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006023 msg_scroll = save_msg_scroll;
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006024
6025 if (drop_callback != NULL)
6026 drop_callback(drop_cookie);
6027
6028 drop_filev = NULL;
6029 drop_busy = FALSE;
6030}
6031
6032/*
6033 * Handle a file drop. The code is here because a drop is *nearly* like an
6034 * :args command, but not quite (we have a list of exact filenames, so we
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01006035 * don't want to (a) parse a command line, or (b) expand wildcards). So the
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006036 * code is very similar to :args and hence needs access to a lot of the static
6037 * functions in this file.
6038 *
6039 * The "filev" list must have been allocated using alloc(), as should each item
6040 * in the list. This function takes over responsibility for freeing the "filev"
6041 * list.
6042 */
6043 void
6044handle_drop(
6045 int filec, // the number of files dropped
6046 char_u **filev, // the list of files dropped
6047 int split, // force splitting the window
6048 void (*callback)(void *), // to be called after setting the argument
6049 // list
6050 void *cookie) // argument for "callback" (allocated)
6051{
6052 // Cannot handle recursive drops, finish the pending one.
6053 if (drop_busy)
6054 {
6055 FreeWild(filec, filev);
6056 vim_free(cookie);
6057 return;
6058 }
6059
6060 // When calling handle_drop() more than once in a row we only use the last
6061 // one.
6062 if (drop_filev != NULL)
6063 {
6064 FreeWild(drop_filec, drop_filev);
6065 vim_free(drop_cookie);
6066 }
6067
6068 drop_filec = filec;
6069 drop_filev = filev;
6070 drop_split = split;
6071 drop_callback = callback;
6072 drop_cookie = cookie;
6073
6074 // Postpone this when:
6075 // - editing the command line
6076 // - not possible to change the current buffer
6077 // - updating the screen
6078 // As it may change buffers and window structures that are in use and cause
6079 // freed memory to be used.
6080 if (text_locked() || curbuf_locked() || updating_screen)
6081 return;
6082
6083 handle_drop_internal();
6084}
6085
6086/*
6087 * To be called when text is unlocked, curbuf is unlocked or updating_screen is
6088 * reset: Handle a postponed drop.
6089 */
6090 void
6091handle_any_postponed_drop(void)
6092{
6093 if (!drop_busy && drop_filev != NULL
Bram Moolenaar6adb9ea2020-04-30 22:31:18 +02006094 && !text_locked() && !curbuf_locked() && !updating_screen)
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006095 handle_drop_internal();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006096}
6097#endif
6098
Bram Moolenaar071d4272004-06-13 20:20:40 +00006099/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006100 * ":preserve".
6101 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006102 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006103ex_preserve(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006104{
Bram Moolenaar4399ef42005-02-12 14:29:27 +00006105 curbuf->b_flags |= BF_PRESERVED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006106 ml_preserve(curbuf, TRUE);
6107}
6108
6109/*
6110 * ":recover".
6111 */
6112 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006113ex_recover(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006114{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006115 // Set recoverymode right away to avoid the ATTENTION prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006116 recoverymode = TRUE;
Bram Moolenaar45d3b142013-11-09 03:31:51 +01006117 if (!check_changed(curbuf, (p_awa ? CCGD_AW : 0)
6118 | CCGD_MULTWIN
6119 | (eap->forceit ? CCGD_FORCEIT : 0)
6120 | CCGD_EXCMD)
6121
Bram Moolenaar071d4272004-06-13 20:20:40 +00006122 && (*eap->arg == NUL
6123 || setfname(curbuf, eap->arg, NULL, TRUE) == OK))
Bram Moolenaar99499b12019-05-23 21:35:48 +02006124 ml_recover(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006125 recoverymode = FALSE;
6126}
6127
6128/*
6129 * Command modifier used in a wrong way.
6130 */
6131 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006132ex_wrongmodifier(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006133{
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006134 eap->errmsg = _(e_invalid_command);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006135}
6136
Bram Moolenaar071d4272004-06-13 20:20:40 +00006137/*
6138 * :sview [+command] file split window with new file, read-only
6139 * :split [[+command] file] split window with current or new file
6140 * :vsplit [[+command] file] split window vertically with current or new file
6141 * :new [[+command] file] split window with no or new file
6142 * :vnew [[+command] file] split vertically window with no or new file
6143 * :sfind [+command] file split window with file in 'path'
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006144 *
6145 * :tabedit open new Tab page with empty window
6146 * :tabedit [+command] file open new Tab page and edit "file"
6147 * :tabnew [[+command] file] just like :tabedit
6148 * :tabfind [+command] file open new Tab page and find "file"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006149 */
6150 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006151ex_splitview(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006152{
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006153 win_T *old_curwin = curwin;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006154#if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006155 char_u *fname = NULL;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006156#endif
6157#ifdef FEAT_BROWSE
Bram Moolenaar21cbe172020-10-13 19:08:24 +02006158 char_u dot_path[] = ".";
Bram Moolenaare1004402020-10-24 20:49:43 +02006159 int save_cmod_flags = cmdmod.cmod_flags;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006160#endif
Bram Moolenaar39902a02018-06-21 22:10:08 +02006161 int use_tab = eap->cmdidx == CMD_tabedit
6162 || eap->cmdidx == CMD_tabfind
6163 || eap->cmdidx == CMD_tabnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006164
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01006165 if (ERROR_IF_ANY_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02006166 return;
6167
Bram Moolenaar4033c552017-09-16 20:54:51 +02006168#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00006169 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006170#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006171
Bram Moolenaar4033c552017-09-16 20:54:51 +02006172#ifdef FEAT_QUICKFIX
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006173 // A ":split" in the quickfix window works like ":new". Don't want two
6174 // quickfix windows. But it's OK when doing ":tab split".
Bram Moolenaare1004402020-10-24 20:49:43 +02006175 if (bt_quickfix(curbuf) && cmdmod.cmod_tab == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006176 {
6177 if (eap->cmdidx == CMD_split)
6178 eap->cmdidx = CMD_new;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006179 if (eap->cmdidx == CMD_vsplit)
6180 eap->cmdidx = CMD_vnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006181 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02006182#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006183
Bram Moolenaar4033c552017-09-16 20:54:51 +02006184#ifdef FEAT_SEARCHPATH
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006185 if (eap->cmdidx == CMD_sfind || eap->cmdidx == CMD_tabfind)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006186 {
6187 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg),
6188 FNAME_MESS, TRUE, curbuf->b_ffname);
6189 if (fname == NULL)
6190 goto theend;
6191 eap->arg = fname;
6192 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006193# ifdef FEAT_BROWSE
Bram Moolenaar4033c552017-09-16 20:54:51 +02006194 else
6195# endif
6196#endif
6197#ifdef FEAT_BROWSE
Bram Moolenaare1004402020-10-24 20:49:43 +02006198 if ((cmdmod.cmod_flags & CMOD_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006199 && eap->cmdidx != CMD_vnew
Bram Moolenaar071d4272004-06-13 20:20:40 +00006200 && eap->cmdidx != CMD_new)
6201 {
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006202 if (
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006203# ifdef FEAT_GUI
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006204 !gui.in_use &&
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006205# endif
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006206 au_has_group((char_u *)"FileExplorer"))
6207 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006208 // No browsing supported but we do have the file explorer:
6209 // Edit the directory.
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006210 if (*eap->arg == NUL || !mch_isdir(eap->arg))
Bram Moolenaar21cbe172020-10-13 19:08:24 +02006211 eap->arg = dot_path;
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006212 }
6213 else
6214 {
Bram Moolenaar39902a02018-06-21 22:10:08 +02006215 fname = do_browse(0, (char_u *)(use_tab
6216 ? _("Edit File in new tab page")
6217 : _("Edit File in new window")),
Bram Moolenaar071d4272004-06-13 20:20:40 +00006218 eap->arg, NULL, NULL, NULL, curbuf);
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006219 if (fname == NULL)
6220 goto theend;
6221 eap->arg = fname;
6222 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006223 }
Bram Moolenaare1004402020-10-24 20:49:43 +02006224 cmdmod.cmod_flags &= ~CMOD_BROWSE; // Don't browse again in do_ecmd().
Bram Moolenaar4033c552017-09-16 20:54:51 +02006225#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006226
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006227 /*
6228 * Either open new tab page or split the window.
6229 */
Bram Moolenaar39902a02018-06-21 22:10:08 +02006230 if (use_tab)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006231 {
Bram Moolenaare1004402020-10-24 20:49:43 +02006232 if (win_new_tabpage(cmdmod.cmod_tab != 0 ? cmdmod.cmod_tab
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006233 : eap->addr_count == 0 ? 0
6234 : (int)eap->line2 + 1) != FAIL)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006235 {
Bram Moolenaard2b66012008-01-09 19:30:36 +00006236 do_exedit(eap, old_curwin);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006237
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006238 // set the alternate buffer for the window we came from
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006239 if (curwin != old_curwin
6240 && win_valid(old_curwin)
6241 && old_curwin->w_buffer != curbuf
Bram Moolenaare1004402020-10-24 20:49:43 +02006242 && (cmdmod.cmod_flags & CMOD_KEEPALT) == 0)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006243 old_curwin->w_alt_fnum = curbuf->b_fnum;
6244 }
6245 }
6246 else if (win_split(eap->addr_count > 0 ? (int)eap->line2 : 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006247 *eap->cmd == 'v' ? WSP_VERT : 0) != FAIL)
6248 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006249 // Reset 'scrollbind' when editing another file, but keep it when
6250 // doing ":split" without arguments.
Bram Moolenaare1004402020-10-24 20:49:43 +02006251 if (*eap->arg != NUL)
Bram Moolenaar3368ea22010-09-21 16:56:35 +02006252 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006253 else
6254 do_check_scrollbind(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006255 do_exedit(eap, old_curwin);
6256 }
6257
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006258# ifdef FEAT_BROWSE
Bram Moolenaare1004402020-10-24 20:49:43 +02006259 cmdmod.cmod_flags = save_cmod_flags;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006260# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006261
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006262# if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006263theend:
6264 vim_free(fname);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006265# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006266}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006267
6268/*
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006269 * Open a new tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006270 */
6271 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006272tabpage_new(void)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006273{
6274 exarg_T ea;
6275
Bram Moolenaara80faa82020-04-12 19:37:17 +02006276 CLEAR_FIELD(ea);
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006277 ea.cmdidx = CMD_tabnew;
6278 ea.cmd = (char_u *)"tabn";
6279 ea.arg = (char_u *)"";
6280 ex_splitview(&ea);
6281}
6282
6283/*
6284 * :tabnext command
6285 */
6286 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006287ex_tabnext(exarg_T *eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006288{
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006289 int tab_number;
6290
Bram Moolenaar8cdbd5b2019-06-16 15:50:45 +02006291 if (ERROR_IF_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02006292 return;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006293 switch (eap->cmdidx)
6294 {
6295 case CMD_tabfirst:
6296 case CMD_tabrewind:
6297 goto_tabpage(1);
6298 break;
6299 case CMD_tablast:
6300 goto_tabpage(9999);
6301 break;
6302 case CMD_tabprevious:
6303 case CMD_tabNext:
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006304 if (eap->arg && *eap->arg != NUL)
6305 {
6306 char_u *p = eap->arg;
6307 char_u *p_save = p;
6308
6309 tab_number = getdigits(&p);
6310 if (p == p_save || *p_save == '-' || *p != NUL
6311 || tab_number == 0)
6312 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006313 // No numbers as argument.
Bram Moolenaar8930caa2020-07-23 16:37:03 +02006314 eap->errmsg = ex_errmsg(e_invarg2, eap->arg);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006315 return;
6316 }
6317 }
6318 else
6319 {
6320 if (eap->addr_count == 0)
6321 tab_number = 1;
6322 else
6323 {
6324 tab_number = eap->line2;
6325 if (tab_number < 1)
6326 {
Bram Moolenaar8930caa2020-07-23 16:37:03 +02006327 eap->errmsg = _(e_invrange);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006328 return;
6329 }
6330 }
6331 }
6332 goto_tabpage(-tab_number);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006333 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006334 default: // CMD_tabnext
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006335 tab_number = get_tabpage_arg(eap);
6336 if (eap->errmsg == NULL)
6337 goto_tabpage(tab_number);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006338 break;
6339 }
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006340}
6341
6342/*
6343 * :tabmove command
6344 */
6345 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006346ex_tabmove(exarg_T *eap)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006347{
Bram Moolenaar40ce3a42015-04-21 18:08:39 +02006348 int tab_number;
Bram Moolenaar8cb8dca2012-07-06 18:27:39 +02006349
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006350 tab_number = get_tabpage_arg(eap);
6351 if (eap->errmsg == NULL)
6352 tabpage_move(tab_number);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006353}
6354
6355/*
6356 * :tabs command: List tabs and their contents.
6357 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006358 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006359ex_tabs(exarg_T *eap UNUSED)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006360{
6361 tabpage_T *tp;
6362 win_T *wp;
6363 int tabcount = 1;
6364
6365 msg_start();
6366 msg_scroll = TRUE;
6367 for (tp = first_tabpage; tp != NULL && !got_int; tp = tp->tp_next)
6368 {
6369 msg_putchar('\n');
6370 vim_snprintf((char *)IObuff, IOSIZE, _("Tab page %d"), tabcount++);
Bram Moolenaar8820b482017-03-16 17:23:31 +01006371 msg_outtrans_attr(IObuff, HL_ATTR(HLF_T));
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006372 out_flush(); // output one line at a time
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006373 ui_breakcheck();
6374
Bram Moolenaar030f0df2006-02-21 22:02:53 +00006375 if (tp == curtab)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006376 wp = firstwin;
6377 else
6378 wp = tp->tp_firstwin;
6379 for ( ; wp != NULL && !got_int; wp = wp->w_next)
6380 {
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006381 msg_putchar('\n');
6382 msg_putchar(wp == curwin ? '>' : ' ');
6383 msg_putchar(' ');
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006384 msg_putchar(bufIsChanged(wp->w_buffer) ? '+' : ' ');
6385 msg_putchar(' ');
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006386 if (buf_spname(wp->w_buffer) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02006387 vim_strncpy(IObuff, buf_spname(wp->w_buffer), IOSIZE - 1);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006388 else
6389 home_replace(wp->w_buffer, wp->w_buffer->b_fname,
6390 IObuff, IOSIZE, TRUE);
6391 msg_outtrans(IObuff);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006392 out_flush(); // output one line at a time
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006393 ui_breakcheck();
6394 }
6395 }
6396}
6397
Bram Moolenaar071d4272004-06-13 20:20:40 +00006398/*
6399 * ":mode": Set screen mode.
6400 * If no argument given, just get the screen size and redraw.
6401 */
6402 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006403ex_mode(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006404{
6405 if (*eap->arg == NUL)
6406 shell_resized();
6407 else
Bram Moolenaar3c71aec2020-01-17 19:32:20 +01006408 emsg(_(e_screenmode));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006409}
6410
Bram Moolenaar071d4272004-06-13 20:20:40 +00006411/*
6412 * ":resize".
6413 * set, increment or decrement current window height
6414 */
6415 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006416ex_resize(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006417{
6418 int n;
6419 win_T *wp = curwin;
6420
6421 if (eap->addr_count > 0)
6422 {
6423 n = eap->line2;
6424 for (wp = firstwin; wp->w_next != NULL && --n > 0; wp = wp->w_next)
6425 ;
6426 }
6427
Bram Moolenaar44a2f922016-03-19 22:11:51 +01006428# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00006429 need_mouse_correct = TRUE;
Bram Moolenaar44a2f922016-03-19 22:11:51 +01006430# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006431 n = atol((char *)eap->arg);
Bram Moolenaare1004402020-10-24 20:49:43 +02006432 if (cmdmod.cmod_split & WSP_VERT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006433 {
6434 if (*eap->arg == '-' || *eap->arg == '+')
Bram Moolenaar9668cc52020-10-17 17:39:55 +02006435 n += wp->w_width;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006436 else if (n == 0 && eap->arg[0] == NUL) // default is very wide
Bram Moolenaar071d4272004-06-13 20:20:40 +00006437 n = 9999;
6438 win_setwidth_win((int)n, wp);
6439 }
6440 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006441 {
6442 if (*eap->arg == '-' || *eap->arg == '+')
Bram Moolenaar9668cc52020-10-17 17:39:55 +02006443 n += wp->w_height;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006444 else if (n == 0 && eap->arg[0] == NUL) // default is very high
Bram Moolenaar071d4272004-06-13 20:20:40 +00006445 n = 9999;
6446 win_setheight_win((int)n, wp);
6447 }
6448}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006449
6450/*
6451 * ":find [+command] <file>" command.
6452 */
6453 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006454ex_find(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006455{
6456#ifdef FEAT_SEARCHPATH
6457 char_u *fname;
6458 int count;
6459
6460 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg), FNAME_MESS,
6461 TRUE, curbuf->b_ffname);
6462 if (eap->addr_count > 0)
6463 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006464 // Repeat finding the file "count" times. This matters when it
6465 // appears several times in the path.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006466 count = eap->line2;
6467 while (fname != NULL && --count > 0)
6468 {
6469 vim_free(fname);
6470 fname = find_file_in_path(NULL, 0, FNAME_MESS,
6471 FALSE, curbuf->b_ffname);
6472 }
6473 }
6474
6475 if (fname != NULL)
6476 {
6477 eap->arg = fname;
6478#endif
6479 do_exedit(eap, NULL);
6480#ifdef FEAT_SEARCHPATH
6481 vim_free(fname);
6482 }
6483#endif
6484}
6485
6486/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00006487 * ":open" simulation: for now just work like ":visual".
6488 */
6489 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006490ex_open(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00006491{
6492 regmatch_T regmatch;
6493 char_u *p;
6494
6495 curwin->w_cursor.lnum = eap->line2;
6496 beginline(BL_SOL | BL_FIX);
6497 if (*eap->arg == '/')
6498 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006499 // ":open /pattern/": put cursor in column found with pattern
Bram Moolenaardf177f62005-02-22 08:39:57 +00006500 ++eap->arg;
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02006501 p = skip_regexp(eap->arg, '/', p_magic);
Bram Moolenaardf177f62005-02-22 08:39:57 +00006502 *p = NUL;
6503 regmatch.regprog = vim_regcomp(eap->arg, p_magic ? RE_MAGIC : 0);
6504 if (regmatch.regprog != NULL)
6505 {
6506 regmatch.rm_ic = p_ic;
6507 p = ml_get_curline();
6508 if (vim_regexec(&regmatch, p, (colnr_T)0))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006509 curwin->w_cursor.col = (colnr_T)(regmatch.startp[0] - p);
Bram Moolenaardf177f62005-02-22 08:39:57 +00006510 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006511 emsg(_(e_nomatch));
Bram Moolenaar473de612013-06-08 18:19:48 +02006512 vim_regfree(regmatch.regprog);
Bram Moolenaardf177f62005-02-22 08:39:57 +00006513 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006514 // Move to the NUL, ignore any other arguments.
Bram Moolenaardf177f62005-02-22 08:39:57 +00006515 eap->arg += STRLEN(eap->arg);
6516 }
6517 check_cursor();
6518
6519 eap->cmdidx = CMD_visual;
6520 do_exedit(eap, NULL);
6521}
6522
6523/*
Bram Moolenaar59d8e562020-11-07 18:41:10 +01006524 * ":edit", ":badd", ":balt", ":visual".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006525 */
6526 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006527ex_edit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006528{
6529 do_exedit(eap, NULL);
6530}
6531
6532/*
Bram Moolenaar4b96df52020-01-26 22:00:26 +01006533 * ":edit <file>" command and alike.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006534 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006535 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006536do_exedit(
6537 exarg_T *eap,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006538 win_T *old_curwin) // curwin before doing a split or NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006539{
6540 int n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006541 int need_hide;
Bram Moolenaardf177f62005-02-22 08:39:57 +00006542 int exmode_was = exmode_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006543
Bram Moolenaar3c01c4a2020-02-01 23:04:24 +01006544 if ((eap->cmdidx != CMD_pedit && ERROR_IF_POPUP_WINDOW)
6545 || ERROR_IF_TERM_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02006546 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006547 /*
6548 * ":vi" command ends Ex mode.
6549 */
6550 if (exmode_active && (eap->cmdidx == CMD_visual
6551 || eap->cmdidx == CMD_view))
6552 {
6553 exmode_active = FALSE;
Bram Moolenaar158ea172020-06-18 17:28:39 +02006554 ex_pressedreturn = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006555 if (*eap->arg == NUL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00006556 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006557 // Special case: ":global/pat/visual\NLvi-commands"
Bram Moolenaardf177f62005-02-22 08:39:57 +00006558 if (global_busy)
6559 {
6560 int rd = RedrawingDisabled;
6561 int nwr = no_wait_return;
6562 int ms = msg_scroll;
6563#ifdef FEAT_GUI
6564 int he = hold_gui_events;
6565#endif
6566
6567 if (eap->nextcmd != NULL)
6568 {
6569 stuffReadbuff(eap->nextcmd);
6570 eap->nextcmd = NULL;
6571 }
6572
6573 if (exmode_was != EXMODE_VIM)
6574 settmode(TMODE_RAW);
6575 RedrawingDisabled = 0;
6576 no_wait_return = 0;
6577 need_wait_return = FALSE;
6578 msg_scroll = 0;
6579#ifdef FEAT_GUI
6580 hold_gui_events = 0;
6581#endif
6582 must_redraw = CLEAR;
Bram Moolenaar9e2bcb52020-02-18 21:33:00 +01006583 pending_exmode_active = TRUE;
Bram Moolenaardf177f62005-02-22 08:39:57 +00006584
6585 main_loop(FALSE, TRUE);
6586
Bram Moolenaar9e2bcb52020-02-18 21:33:00 +01006587 pending_exmode_active = FALSE;
Bram Moolenaardf177f62005-02-22 08:39:57 +00006588 RedrawingDisabled = rd;
6589 no_wait_return = nwr;
6590 msg_scroll = ms;
6591#ifdef FEAT_GUI
6592 hold_gui_events = he;
6593#endif
6594 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006595 return;
Bram Moolenaardf177f62005-02-22 08:39:57 +00006596 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006597 }
6598
6599 if ((eap->cmdidx == CMD_new
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006600 || eap->cmdidx == CMD_tabnew
6601 || eap->cmdidx == CMD_tabedit
Bram Moolenaar4033c552017-09-16 20:54:51 +02006602 || eap->cmdidx == CMD_vnew) && *eap->arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006603 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006604 // ":new" or ":tabnew" without argument: edit an new empty buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00006605 setpcmark();
6606 (void)do_ecmd(0, NULL, NULL, eap, ECMD_ONE,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00006607 ECMD_HIDE + (eap->forceit ? ECMD_FORCEIT : 0),
6608 old_curwin == NULL ? curwin : NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006609 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02006610 else if ((eap->cmdidx != CMD_split && eap->cmdidx != CMD_vsplit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006611 || *eap->arg != NUL
6612#ifdef FEAT_BROWSE
Bram Moolenaare1004402020-10-24 20:49:43 +02006613 || (cmdmod.cmod_flags & CMOD_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006614#endif
6615 )
6616 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006617 // Can't edit another file when "curbuf_lock" is set. Only ":edit"
6618 // can bring us here, others are stopped earlier.
Bram Moolenaar5555acc2006-04-07 21:33:12 +00006619 if (*eap->arg != NUL && curbuf_locked())
6620 return;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006621
Bram Moolenaar071d4272004-06-13 20:20:40 +00006622 n = readonlymode;
6623 if (eap->cmdidx == CMD_view || eap->cmdidx == CMD_sview)
6624 readonlymode = TRUE;
6625 else if (eap->cmdidx == CMD_enew)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006626 readonlymode = FALSE; // 'readonly' doesn't make sense in an
6627 // empty buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00006628 setpcmark();
6629 if (do_ecmd(0, (eap->cmdidx == CMD_enew ? NULL : eap->arg),
6630 NULL, eap,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006631 // ":edit" goes to first line if Vi compatible
Bram Moolenaar071d4272004-06-13 20:20:40 +00006632 (*eap->arg == NUL && eap->do_ecmd_lnum == 0
6633 && vim_strchr(p_cpo, CPO_GOTO1) != NULL)
6634 ? ECMD_ONE : eap->do_ecmd_lnum,
Bram Moolenaareb44a682017-08-03 22:44:55 +02006635 (buf_hide(curbuf) ? ECMD_HIDE : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006636 + (eap->forceit ? ECMD_FORCEIT : 0)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006637 // after a split we can use an existing buffer
Bram Moolenaar7b449342014-03-25 13:03:48 +01006638 + (old_curwin != NULL ? ECMD_OLDBUF : 0)
Bram Moolenaar59d8e562020-11-07 18:41:10 +01006639 + (eap->cmdidx == CMD_badd ? ECMD_ADDBUF : 0)
6640 + (eap->cmdidx == CMD_balt ? ECMD_ALTBUF : 0)
Bram Moolenaar701f7af2008-11-15 13:12:07 +00006641 , old_curwin == NULL ? curwin : NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006642 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006643 // Editing the file failed. If the window was split, close it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006644 if (old_curwin != NULL)
6645 {
6646 need_hide = (curbufIsChanged() && curbuf->b_nwindows <= 1);
Bram Moolenaareb44a682017-08-03 22:44:55 +02006647 if (!need_hide || buf_hide(curbuf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006648 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006649#if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00006650 cleanup_T cs;
6651
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006652 // Reset the error/interrupt/exception state here so that
6653 // aborting() returns FALSE when closing a window.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00006654 enter_cleanup(&cs);
Bram Moolenaar4033c552017-09-16 20:54:51 +02006655#endif
6656#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00006657 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006658#endif
Bram Moolenaareb44a682017-08-03 22:44:55 +02006659 win_close(curwin, !need_hide && !buf_hide(curbuf));
Bram Moolenaarc0197e22004-09-13 20:26:32 +00006660
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006661#if defined(FEAT_EVAL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006662 // Restore the error/interrupt/exception state if not
6663 // discarded by a new aborting error, interrupt, or
6664 // uncaught exception.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00006665 leave_cleanup(&cs);
Bram Moolenaar4033c552017-09-16 20:54:51 +02006666#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006667 }
6668 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006669 }
6670 else if (readonlymode && curbuf->b_nwindows == 1)
6671 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006672 // When editing an already visited buffer, 'readonly' won't be set
6673 // but the previous value is kept. With ":view" and ":sview" we
6674 // want the file to be readonly, except when another window is
6675 // editing the same buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006676 curbuf->b_p_ro = TRUE;
6677 }
6678 readonlymode = n;
6679 }
6680 else
6681 {
6682 if (eap->do_ecmd_cmd != NULL)
6683 do_cmdline_cmd(eap->do_ecmd_cmd);
6684#ifdef FEAT_TITLE
6685 n = curwin->w_arg_idx_invalid;
6686#endif
6687 check_arg_idx(curwin);
6688#ifdef FEAT_TITLE
6689 if (n != curwin->w_arg_idx_invalid)
6690 maketitle();
6691#endif
6692 }
6693
Bram Moolenaar071d4272004-06-13 20:20:40 +00006694 /*
6695 * if ":split file" worked, set alternate file name in old window to new
6696 * file
6697 */
6698 if (old_curwin != NULL
6699 && *eap->arg != NUL
6700 && curwin != old_curwin
6701 && win_valid(old_curwin)
Bram Moolenaard4755bb2004-09-02 19:12:26 +00006702 && old_curwin->w_buffer != curbuf
Bram Moolenaare1004402020-10-24 20:49:43 +02006703 && (cmdmod.cmod_flags & CMOD_KEEPALT) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006704 old_curwin->w_alt_fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006705
6706 ex_no_reprint = TRUE;
6707}
6708
6709#ifndef FEAT_GUI
6710/*
6711 * ":gui" and ":gvim" when there is no GUI.
6712 */
6713 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006714ex_nogui(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006715{
Bram Moolenaar8930caa2020-07-23 16:37:03 +02006716 eap->errmsg = _(e_nogvim);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006717}
6718#endif
6719
Bram Moolenaar4f974752019-02-17 17:44:42 +01006720#if defined(FEAT_GUI_MSWIN) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006721 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006722ex_tearoff(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006723{
6724 gui_make_tearoff(eap->arg);
6725}
6726#endif
6727
Bram Moolenaar29a2c082018-03-05 21:06:23 +01006728#if (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK) \
6729 || defined(FEAT_TERM_POPUP_MENU)) && defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006730 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006731ex_popup(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006732{
Bram Moolenaar29a2c082018-03-05 21:06:23 +01006733# if defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK)
6734 if (gui.in_use)
6735 gui_make_popup(eap->arg, eap->forceit);
6736# ifdef FEAT_TERM_POPUP_MENU
6737 else
6738# endif
6739# endif
6740# ifdef FEAT_TERM_POPUP_MENU
6741 pum_make_popup(eap->arg, eap->forceit);
6742# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006743}
6744#endif
6745
Bram Moolenaar071d4272004-06-13 20:20:40 +00006746 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006747ex_swapname(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006748{
6749 if (curbuf->b_ml.ml_mfp == NULL || curbuf->b_ml.ml_mfp->mf_fname == NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01006750 msg(_("No swap file"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006751 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01006752 msg((char *)curbuf->b_ml.ml_mfp->mf_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006753}
6754
6755/*
6756 * ":syncbind" forces all 'scrollbind' windows to have the same relative
6757 * offset.
6758 * (1998-11-02 16:21:01 R. Edward Ralston <eralston@computer.org>)
6759 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006760 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006761ex_syncbind(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006762{
Bram Moolenaar071d4272004-06-13 20:20:40 +00006763 win_T *wp;
Bram Moolenaardedd1b02013-12-14 13:06:17 +01006764 win_T *save_curwin = curwin;
6765 buf_T *save_curbuf = curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006766 long topline;
6767 long y;
6768 linenr_T old_linenr = curwin->w_cursor.lnum;
6769
6770 setpcmark();
6771
6772 /*
6773 * determine max topline
6774 */
6775 if (curwin->w_p_scb)
6776 {
6777 topline = curwin->w_topline;
Bram Moolenaar29323592016-07-24 22:04:11 +02006778 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006779 {
6780 if (wp->w_p_scb && wp->w_buffer)
6781 {
Bram Moolenaar375e3392019-01-31 18:26:10 +01006782 y = wp->w_buffer->b_ml.ml_line_count - get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006783 if (topline > y)
6784 topline = y;
6785 }
6786 }
6787 if (topline < 1)
6788 topline = 1;
6789 }
6790 else
6791 {
6792 topline = 1;
6793 }
6794
6795
6796 /*
Bram Moolenaardedd1b02013-12-14 13:06:17 +01006797 * Set all scrollbind windows to the same topline.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006798 */
Bram Moolenaar29323592016-07-24 22:04:11 +02006799 FOR_ALL_WINDOWS(curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006800 {
6801 if (curwin->w_p_scb)
6802 {
Bram Moolenaardedd1b02013-12-14 13:06:17 +01006803 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006804 y = topline - curwin->w_topline;
6805 if (y > 0)
6806 scrollup(y, TRUE);
6807 else
6808 scrolldown(-y, TRUE);
6809 curwin->w_scbind_pos = topline;
6810 redraw_later(VALID);
6811 cursor_correct();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006812 curwin->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006813 }
6814 }
Bram Moolenaardedd1b02013-12-14 13:06:17 +01006815 curwin = save_curwin;
6816 curbuf = save_curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006817 if (curwin->w_p_scb)
6818 {
6819 did_syncbind = TRUE;
6820 checkpcmark();
6821 if (old_linenr != curwin->w_cursor.lnum)
6822 {
6823 char_u ctrl_o[2];
6824
6825 ctrl_o[0] = Ctrl_O;
6826 ctrl_o[1] = 0;
6827 ins_typebuf(ctrl_o, REMAP_NONE, 0, TRUE, FALSE);
6828 }
6829 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006830}
6831
6832
6833 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006834ex_read(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006835{
Bram Moolenaardf177f62005-02-22 08:39:57 +00006836 int i;
6837 int empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
6838 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006839
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006840 if (eap->usefilter) // :r!cmd
Bram Moolenaar071d4272004-06-13 20:20:40 +00006841 do_bang(1, eap, FALSE, FALSE, TRUE);
6842 else
6843 {
6844 if (u_save(eap->line2, (linenr_T)(eap->line2 + 1)) == FAIL)
6845 return;
6846
6847#ifdef FEAT_BROWSE
Bram Moolenaare1004402020-10-24 20:49:43 +02006848 if (cmdmod.cmod_flags & CMOD_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006849 {
6850 char_u *browseFile;
6851
Bram Moolenaar7171abe2004-10-11 10:06:20 +00006852 browseFile = do_browse(0, (char_u *)_("Append File"), eap->arg,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006853 NULL, NULL, NULL, curbuf);
6854 if (browseFile != NULL)
6855 {
6856 i = readfile(browseFile, NULL,
6857 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
6858 vim_free(browseFile);
6859 }
6860 else
6861 i = OK;
6862 }
6863 else
6864#endif
6865 if (*eap->arg == NUL)
6866 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006867 if (check_fname() == FAIL) // check for no file name
Bram Moolenaar071d4272004-06-13 20:20:40 +00006868 return;
6869 i = readfile(curbuf->b_ffname, curbuf->b_fname,
6870 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
6871 }
6872 else
6873 {
6874 if (vim_strchr(p_cpo, CPO_ALTREAD) != NULL)
6875 (void)setaltfname(eap->arg, eap->arg, (linenr_T)1);
6876 i = readfile(eap->arg, NULL,
6877 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
6878
6879 }
Bram Moolenaare13b9af2017-01-13 22:01:02 +01006880 if (i != OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006881 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006882#if defined(FEAT_EVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006883 if (!aborting())
6884#endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006885 semsg(_(e_notopen), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006886 }
6887 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00006888 {
6889 if (empty && exmode_active)
6890 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006891 // Delete the empty line that remains. Historically ex does
6892 // this but vi doesn't.
Bram Moolenaardf177f62005-02-22 08:39:57 +00006893 if (eap->line2 == 0)
6894 lnum = curbuf->b_ml.ml_line_count;
6895 else
6896 lnum = 1;
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00006897 if (*ml_get(lnum) == NUL && u_savedel(lnum, 1L) == OK)
Bram Moolenaardf177f62005-02-22 08:39:57 +00006898 {
Bram Moolenaarca70c072020-05-30 20:30:46 +02006899 ml_delete(lnum);
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00006900 if (curwin->w_cursor.lnum > 1
6901 && curwin->w_cursor.lnum >= lnum)
Bram Moolenaardf177f62005-02-22 08:39:57 +00006902 --curwin->w_cursor.lnum;
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00006903 deleted_lines_mark(lnum, 1L);
Bram Moolenaardf177f62005-02-22 08:39:57 +00006904 }
6905 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006906 redraw_curbuf_later(VALID);
Bram Moolenaardf177f62005-02-22 08:39:57 +00006907 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006908 }
6909}
6910
Bram Moolenaarea408852005-06-25 22:49:46 +00006911static char_u *prev_dir = NULL;
6912
6913#if defined(EXITFREE) || defined(PROTO)
6914 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006915free_cd_dir(void)
Bram Moolenaarea408852005-06-25 22:49:46 +00006916{
Bram Moolenaard23a8232018-02-10 18:45:26 +01006917 VIM_CLEAR(prev_dir);
6918 VIM_CLEAR(globaldir);
Bram Moolenaarea408852005-06-25 22:49:46 +00006919}
6920#endif
6921
Bram Moolenaarf4258302013-06-02 18:20:17 +02006922/*
Bram Moolenaar002bc792020-06-05 22:33:42 +02006923 * Get the previous directory for the given chdir scope.
6924 */
6925 static char_u *
6926get_prevdir(cdscope_T scope)
6927{
6928 if (scope == CDSCOPE_WINDOW)
6929 return curwin->w_prevdir;
6930 else if (scope == CDSCOPE_TABPAGE)
6931 return curtab->tp_prevdir;
6932 return prev_dir;
6933}
6934
6935/*
Bram Moolenaarf4258302013-06-02 18:20:17 +02006936 * Deal with the side effects of changing the current directory.
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006937 * When 'scope' is CDSCOPE_TABPAGE then this was after an ":tcd" command.
6938 * When 'scope' is CDSCOPE_WINDOW then this was after an ":lcd" command.
Bram Moolenaarf4258302013-06-02 18:20:17 +02006939 */
6940 void
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006941post_chdir(cdscope_T scope)
Bram Moolenaarf4258302013-06-02 18:20:17 +02006942{
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006943 if (scope != CDSCOPE_WINDOW)
Bram Moolenaar00aa0692019-04-27 20:37:57 +02006944 // Clear tab local directory for both :cd and :tcd
6945 VIM_CLEAR(curtab->tp_localdir);
Bram Moolenaard23a8232018-02-10 18:45:26 +01006946 VIM_CLEAR(curwin->w_localdir);
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006947 if (scope != CDSCOPE_GLOBAL)
Bram Moolenaarf4258302013-06-02 18:20:17 +02006948 {
Bram Moolenaar002bc792020-06-05 22:33:42 +02006949 char_u *pdir = get_prevdir(scope);
6950
6951 // If still in the global directory, need to remember current
6952 // directory as the global directory.
6953 if (globaldir == NULL && pdir != NULL)
6954 globaldir = vim_strsave(pdir);
6955
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006956 // Remember this local directory for the window.
Bram Moolenaarf4258302013-06-02 18:20:17 +02006957 if (mch_dirname(NameBuff, MAXPATHL) == OK)
Bram Moolenaar00aa0692019-04-27 20:37:57 +02006958 {
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006959 if (scope == CDSCOPE_TABPAGE)
Bram Moolenaar00aa0692019-04-27 20:37:57 +02006960 curtab->tp_localdir = vim_strsave(NameBuff);
6961 else
6962 curwin->w_localdir = vim_strsave(NameBuff);
6963 }
Bram Moolenaarf4258302013-06-02 18:20:17 +02006964 }
6965 else
6966 {
Bram Moolenaar002bc792020-06-05 22:33:42 +02006967 // We are now in the global directory, no need to remember its name.
Bram Moolenaard23a8232018-02-10 18:45:26 +01006968 VIM_CLEAR(globaldir);
Bram Moolenaarf4258302013-06-02 18:20:17 +02006969 }
6970
6971 shorten_fnames(TRUE);
6972}
6973
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006974/*
6975 * Change directory function used by :cd/:tcd/:lcd Ex commands and the
Bram Moolenaar95058722020-06-01 16:26:19 +02006976 * chdir() function.
6977 * scope == CDSCOPE_WINDOW: changes the window-local directory
6978 * scope == CDSCOPE_TABPAGE: changes the tab-local directory
6979 * Otherwise: changes the global directory
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006980 * Returns TRUE if the directory is successfully changed.
6981 */
6982 int
6983changedir_func(
6984 char_u *new_dir,
6985 int forceit,
6986 cdscope_T scope)
6987{
6988 char_u *tofree;
Bram Moolenaar002bc792020-06-05 22:33:42 +02006989 char_u *pdir = NULL;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006990 int dir_differs;
6991 int retval = FALSE;
6992
Bram Moolenaar7cc96922020-01-31 22:41:38 +01006993 if (new_dir == NULL || allbuf_locked())
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006994 return FALSE;
6995
6996 if (vim_strchr(p_cpo, CPO_CHDIR) != NULL && curbufIsChanged() && !forceit)
6997 {
6998 emsg(_("E747: Cannot change directory, buffer is modified (add ! to override)"));
6999 return FALSE;
7000 }
7001
7002 // ":cd -": Change to previous directory
7003 if (STRCMP(new_dir, "-") == 0)
7004 {
Bram Moolenaar002bc792020-06-05 22:33:42 +02007005 pdir = get_prevdir(scope);
7006 if (pdir == NULL)
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007007 {
7008 emsg(_("E186: No previous directory"));
7009 return FALSE;
7010 }
Bram Moolenaar002bc792020-06-05 22:33:42 +02007011 new_dir = pdir;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007012 }
7013
Bram Moolenaar002bc792020-06-05 22:33:42 +02007014 // Free the previous directory
7015 tofree = get_prevdir(scope);
7016
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007017 // Save current directory for next ":cd -"
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007018 if (mch_dirname(NameBuff, MAXPATHL) == OK)
Bram Moolenaar002bc792020-06-05 22:33:42 +02007019 pdir = vim_strsave(NameBuff);
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007020 else
Bram Moolenaar002bc792020-06-05 22:33:42 +02007021 pdir = NULL;
7022 if (scope == CDSCOPE_WINDOW)
7023 curwin->w_prevdir = pdir;
7024 else if (scope == CDSCOPE_TABPAGE)
7025 curtab->tp_prevdir = pdir;
7026 else
7027 prev_dir = pdir;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007028
7029#if defined(UNIX) || defined(VMS)
7030 // for UNIX ":cd" means: go to home directory
7031 if (*new_dir == NUL)
7032 {
7033 // use NameBuff for home directory name
7034# ifdef VMS
7035 char_u *p;
7036
7037 p = mch_getenv((char_u *)"SYS$LOGIN");
7038 if (p == NULL || *p == NUL) // empty is the same as not set
7039 NameBuff[0] = NUL;
7040 else
7041 vim_strncpy(NameBuff, p, MAXPATHL - 1);
7042# else
7043 expand_env((char_u *)"$HOME", NameBuff, MAXPATHL);
7044# endif
7045 new_dir = NameBuff;
7046 }
7047#endif
Bram Moolenaar002bc792020-06-05 22:33:42 +02007048 dir_differs = new_dir == NULL || pdir == NULL
7049 || pathcmp((char *)pdir, (char *)new_dir, -1) != 0;
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007050 if (new_dir == NULL || (dir_differs && vim_chdir(new_dir)))
7051 emsg(_(e_failed));
7052 else
7053 {
7054 char_u *acmd_fname;
7055
7056 post_chdir(scope);
7057
7058 if (dir_differs)
7059 {
7060 if (scope == CDSCOPE_WINDOW)
7061 acmd_fname = (char_u *)"window";
7062 else if (scope == CDSCOPE_TABPAGE)
7063 acmd_fname = (char_u *)"tabpage";
7064 else
7065 acmd_fname = (char_u *)"global";
7066 apply_autocmds(EVENT_DIRCHANGED, acmd_fname, new_dir, FALSE,
7067 curbuf);
7068 }
7069 retval = TRUE;
7070 }
7071 vim_free(tofree);
7072
7073 return retval;
7074}
Bram Moolenaarea408852005-06-25 22:49:46 +00007075
Bram Moolenaar071d4272004-06-13 20:20:40 +00007076/*
Bram Moolenaar00aa0692019-04-27 20:37:57 +02007077 * ":cd", ":tcd", ":lcd", ":chdir" ":tchdir" and ":lchdir".
Bram Moolenaar071d4272004-06-13 20:20:40 +00007078 */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00007079 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007080ex_cd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007081{
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01007082 char_u *new_dir;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007083
7084 new_dir = eap->arg;
7085#if !defined(UNIX) && !defined(VMS)
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007086 // for non-UNIX ":cd" means: print current directory
Bram Moolenaar071d4272004-06-13 20:20:40 +00007087 if (*new_dir == NUL)
7088 ex_pwd(NULL);
7089 else
7090#endif
7091 {
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007092 cdscope_T scope = CDSCOPE_GLOBAL;
7093
7094 if (eap->cmdidx == CMD_lcd || eap->cmdidx == CMD_lchdir)
7095 scope = CDSCOPE_WINDOW;
7096 else if (eap->cmdidx == CMD_tcd || eap->cmdidx == CMD_tchdir)
7097 scope = CDSCOPE_TABPAGE;
7098
7099 if (changedir_func(new_dir, eap->forceit, scope))
Bram Moolenaardf177f62005-02-22 08:39:57 +00007100 {
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007101 // Echo the new current directory if the command was typed.
Bram Moolenaarfcfbc672009-07-09 18:13:49 +00007102 if (KeyTyped || p_verbose >= 5)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007103 ex_pwd(eap);
7104 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007105 }
7106}
7107
7108/*
7109 * ":pwd".
7110 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007111 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007112ex_pwd(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007113{
7114 if (mch_dirname(NameBuff, MAXPATHL) == OK)
7115 {
7116#ifdef BACKSLASH_IN_FILENAME
7117 slash_adjust(NameBuff);
7118#endif
Bram Moolenaar95058722020-06-01 16:26:19 +02007119 if (p_verbose > 0)
7120 {
7121 char *context = "global";
7122
7123 if (curwin->w_localdir != NULL)
7124 context = "window";
7125 else if (curtab->tp_localdir != NULL)
7126 context = "tabpage";
7127 smsg("[%s] %s", context, (char *)NameBuff);
7128 }
7129 else
7130 msg((char *)NameBuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007131 }
7132 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007133 emsg(_("E187: Unknown"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007134}
7135
7136/*
7137 * ":=".
7138 */
7139 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007140ex_equal(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007141{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007142 smsg("%ld", (long)eap->line2);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007143 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007144}
7145
7146 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007147ex_sleep(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007148{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007149 int n;
7150 long len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007151
7152 if (cursor_valid())
7153 {
7154 n = W_WINROW(curwin) + curwin->w_wrow - msg_scrolled;
7155 if (n >= 0)
Bram Moolenaar53f81742017-09-22 14:35:51 +02007156 windgoto((int)n, curwin->w_wincol + curwin->w_wcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007157 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007158
7159 len = eap->line2;
7160 switch (*eap->arg)
7161 {
7162 case 'm': break;
7163 case NUL: len *= 1000L; break;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007164 default: semsg(_(e_invarg2), eap->arg); return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007165 }
7166 do_sleep(len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007167}
7168
7169/*
7170 * Sleep for "msec" milliseconds, but keep checking for a CTRL-C every second.
7171 */
7172 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007173do_sleep(long msec)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007174{
Bram Moolenaar52953192019-08-16 10:27:13 +02007175 long done = 0;
Bram Moolenaar975b5272016-03-15 23:10:59 +01007176 long wait_now;
Bram Moolenaar52953192019-08-16 10:27:13 +02007177# ifdef ELAPSED_FUNC
7178 elapsed_T start_tv;
7179
7180 // Remember at what time we started, so that we know how much longer we
7181 // should wait after waiting for a bit.
7182 ELAPSED_INIT(start_tv);
7183# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007184
7185 cursor_on();
Bram Moolenaarf45d7472018-09-30 18:22:26 +02007186 out_flush_cursor(FALSE, FALSE);
Bram Moolenaar52953192019-08-16 10:27:13 +02007187 while (!got_int && done < msec)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007188 {
Bram Moolenaar975b5272016-03-15 23:10:59 +01007189 wait_now = msec - done > 1000L ? 1000L : msec - done;
7190#ifdef FEAT_TIMERS
7191 {
7192 long due_time = check_due_timer();
7193
7194 if (due_time > 0 && due_time < wait_now)
7195 wait_now = due_time;
7196 }
7197#endif
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02007198#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar28e67e02019-08-15 23:05:49 +02007199 if (has_any_channel() && wait_now > 20L)
7200 wait_now = 20L;
7201#endif
7202#ifdef FEAT_SOUND
7203 if (has_any_sound_callback() && wait_now > 20L)
7204 wait_now = 20L;
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02007205#endif
Bram Moolenaar975b5272016-03-15 23:10:59 +01007206 ui_delay(wait_now, TRUE);
Bram Moolenaar52953192019-08-16 10:27:13 +02007207
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02007208#ifdef FEAT_JOB_CHANNEL
7209 if (has_any_channel())
7210 ui_breakcheck_force(TRUE);
7211 else
7212#endif
7213 ui_breakcheck();
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007214#ifdef MESSAGE_QUEUE
Bram Moolenaar52953192019-08-16 10:27:13 +02007215 // Process the netbeans and clientserver messages that may have been
7216 // received in the call to ui_breakcheck() when the GUI is in use. This
7217 // may occur when running a test case.
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007218 parse_queued_messages();
Bram Moolenaare3cc6d42011-10-20 21:58:34 +02007219#endif
Bram Moolenaar52953192019-08-16 10:27:13 +02007220
7221# ifdef ELAPSED_FUNC
7222 // actual time passed
7223 done = ELAPSED_FUNC(start_tv);
7224# else
7225 // guestimate time passed (will actually be more)
7226 done += wait_now;
7227# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007228 }
Bram Moolenaar1ebff3d2018-07-07 16:18:13 +02007229
7230 // If CTRL-C was typed to interrupt the sleep, drop the CTRL-C from the
7231 // input buffer, otherwise a following call to input() fails.
7232 if (got_int)
7233 (void)vpeekc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007234}
7235
Bram Moolenaar071d4272004-06-13 20:20:40 +00007236/*
7237 * ":winsize" command (obsolete).
7238 */
7239 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007240ex_winsize(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007241{
7242 int w, h;
7243 char_u *arg = eap->arg;
7244 char_u *p;
7245
7246 w = getdigits(&arg);
7247 arg = skipwhite(arg);
7248 p = arg;
7249 h = getdigits(&arg);
7250 if (*p != NUL && *arg == NUL)
7251 set_shellsize(w, h, TRUE);
7252 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007253 emsg(_("E465: :winsize requires two number arguments"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007254}
7255
Bram Moolenaar071d4272004-06-13 20:20:40 +00007256 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007257ex_wincmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007258{
7259 int xchar = NUL;
7260 char_u *p;
7261
7262 if (*eap->arg == 'g' || *eap->arg == Ctrl_G)
7263 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007264 // CTRL-W g and CTRL-W CTRL-G have an extra command character
Bram Moolenaar071d4272004-06-13 20:20:40 +00007265 if (eap->arg[1] == NUL)
7266 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007267 emsg(_(e_invarg));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007268 return;
7269 }
7270 xchar = eap->arg[1];
7271 p = eap->arg + 2;
7272 }
7273 else
7274 p = eap->arg + 1;
7275
7276 eap->nextcmd = check_nextcmd(p);
7277 p = skipwhite(p);
Bram Moolenaarf5be8cd2020-07-17 20:36:00 +02007278 if (*p != NUL && *p != (
7279#ifdef FEAT_EVAL
7280 in_vim9script() ? '#' :
7281#endif
7282 '"')
7283 && eap->nextcmd == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007284 emsg(_(e_invarg));
Bram Moolenaar12bde492011-06-13 01:19:56 +02007285 else if (!eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007286 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007287 // Pass flags on for ":vertical wincmd ]".
Bram Moolenaare1004402020-10-24 20:49:43 +02007288 postponed_split_flags = cmdmod.cmod_split;
7289 postponed_split_tab = cmdmod.cmod_tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007290 do_window(*eap->arg, eap->addr_count > 0 ? eap->line2 : 0L, xchar);
7291 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00007292 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007293 }
7294}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007295
Bram Moolenaar843ee412004-06-30 16:16:41 +00007296#if defined(FEAT_GUI) || defined(UNIX) || defined(VMS) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007297/*
7298 * ":winpos".
7299 */
7300 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007301ex_winpos(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007302{
7303 int x, y;
7304 char_u *arg = eap->arg;
7305 char_u *p;
7306
7307 if (*arg == NUL)
7308 {
Bram Moolenaar843ee412004-06-30 16:16:41 +00007309# if defined(FEAT_GUI) || defined(MSWIN)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007310# ifdef VIMDLL
7311 if (gui.in_use ? gui_mch_get_winpos(&x, &y) != FAIL :
7312 mch_get_winpos(&x, &y) != FAIL)
7313# elif defined(FEAT_GUI)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007314 if (gui.in_use && gui_mch_get_winpos(&x, &y) != FAIL)
Bram Moolenaar843ee412004-06-30 16:16:41 +00007315# else
7316 if (mch_get_winpos(&x, &y) != FAIL)
7317# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007318 {
7319 sprintf((char *)IObuff, _("Window position: X %d, Y %d"), x, y);
Bram Moolenaar32526b32019-01-19 17:43:09 +01007320 msg((char *)IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007321 }
7322 else
7323# endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007324 emsg(_("E188: Obtaining window position not implemented for this platform"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007325 }
7326 else
7327 {
7328 x = getdigits(&arg);
7329 arg = skipwhite(arg);
7330 p = arg;
7331 y = getdigits(&arg);
7332 if (*p == NUL || *arg != NUL)
7333 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007334 emsg(_("E466: :winpos requires two number arguments"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007335 return;
7336 }
7337# ifdef FEAT_GUI
7338 if (gui.in_use)
7339 gui_mch_set_winpos(x, y);
7340 else if (gui.starting)
7341 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007342 // Remember the coordinates for when the window is opened.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007343 gui_win_x = x;
7344 gui_win_y = y;
7345 }
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007346# if defined(HAVE_TGETENT) || defined(VIMDLL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007347 else
7348# endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007349# endif
7350# if defined(MSWIN) && (!defined(FEAT_GUI) || defined(VIMDLL))
Bram Moolenaar843ee412004-06-30 16:16:41 +00007351 mch_set_winpos(x, y);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007352# endif
7353# ifdef HAVE_TGETENT
7354 if (*T_CWP)
7355 term_set_winpos(x, y);
7356# endif
7357 }
7358}
7359#endif
7360
7361/*
7362 * Handle command that work like operators: ":delete", ":yank", ":>" and ":<".
7363 */
7364 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007365ex_operators(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007366{
7367 oparg_T oa;
7368
7369 clear_oparg(&oa);
7370 oa.regname = eap->regname;
7371 oa.start.lnum = eap->line1;
7372 oa.end.lnum = eap->line2;
7373 oa.line_count = eap->line2 - eap->line1 + 1;
7374 oa.motion_type = MLINE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007375 virtual_op = FALSE;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007376 if (eap->cmdidx != CMD_yank) // position cursor for undo
Bram Moolenaar071d4272004-06-13 20:20:40 +00007377 {
7378 setpcmark();
7379 curwin->w_cursor.lnum = eap->line1;
7380 beginline(BL_SOL | BL_FIX);
7381 }
7382
Bram Moolenaard07c6e12013-11-21 14:21:40 +01007383 if (VIsual_active)
7384 end_visual_mode();
Bram Moolenaard07c6e12013-11-21 14:21:40 +01007385
Bram Moolenaar071d4272004-06-13 20:20:40 +00007386 switch (eap->cmdidx)
7387 {
7388 case CMD_delete:
7389 oa.op_type = OP_DELETE;
7390 op_delete(&oa);
7391 break;
7392
7393 case CMD_yank:
7394 oa.op_type = OP_YANK;
7395 (void)op_yank(&oa, FALSE, TRUE);
7396 break;
7397
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007398 default: // CMD_rshift or CMD_lshift
Bram Moolenaar6e707362013-06-14 19:15:58 +02007399 if (
Bram Moolenaar071d4272004-06-13 20:20:40 +00007400#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6e707362013-06-14 19:15:58 +02007401 (eap->cmdidx == CMD_rshift) ^ curwin->w_p_rl
7402#else
7403 eap->cmdidx == CMD_rshift
Bram Moolenaar071d4272004-06-13 20:20:40 +00007404#endif
Bram Moolenaar6e707362013-06-14 19:15:58 +02007405 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00007406 oa.op_type = OP_RSHIFT;
7407 else
7408 oa.op_type = OP_LSHIFT;
7409 op_shift(&oa, FALSE, eap->amount);
7410 break;
7411 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007412 virtual_op = MAYBE;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007413 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007414}
7415
7416/*
7417 * ":put".
7418 */
7419 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007420ex_put(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007421{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007422 // ":0put" works like ":1put!".
Bram Moolenaar071d4272004-06-13 20:20:40 +00007423 if (eap->line2 == 0)
7424 {
7425 eap->line2 = 1;
7426 eap->forceit = TRUE;
7427 }
7428 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaarc3516f72020-09-08 22:45:35 +02007429 do_put(eap->regname, NULL, eap->forceit ? BACKWARD : FORWARD, 1L,
Bram Moolenaardf177f62005-02-22 08:39:57 +00007430 PUT_LINE|PUT_CURSLINE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007431}
7432
7433/*
7434 * Handle ":copy" and ":move".
7435 */
7436 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007437ex_copymove(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007438{
7439 long n;
7440
Bram Moolenaar491799b2020-08-01 19:23:43 +02007441#ifdef FEAT_EVAL
Bram Moolenaarf5a48012020-08-01 17:00:03 +02007442 if (not_in_vim9(eap) == FAIL)
7443 return;
Bram Moolenaar491799b2020-08-01 19:23:43 +02007444#endif
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02007445 n = get_address(eap, &eap->arg, eap->addr_type, FALSE, FALSE, FALSE, 1);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007446 if (eap->arg == NULL) // error detected
Bram Moolenaar071d4272004-06-13 20:20:40 +00007447 {
7448 eap->nextcmd = NULL;
7449 return;
7450 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00007451 get_flags(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007452
7453 /*
7454 * move or copy lines from 'eap->line1'-'eap->line2' to below line 'n'
7455 */
7456 if (n == MAXLNUM || n < 0 || n > curbuf->b_ml.ml_line_count)
7457 {
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02007458 emsg(_(e_invrange));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007459 return;
7460 }
7461
7462 if (eap->cmdidx == CMD_move)
7463 {
7464 if (do_move(eap->line1, eap->line2, n) == FAIL)
7465 return;
7466 }
7467 else
7468 ex_copy(eap->line1, eap->line2, n);
7469 u_clearline();
7470 beginline(BL_SOL | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007471 ex_may_print(eap);
7472}
7473
7474/*
7475 * Print the current line if flags were given to the Ex command.
7476 */
Bram Moolenaarfd3fe982014-04-01 17:49:44 +02007477 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007478ex_may_print(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007479{
7480 if (eap->flags != 0)
7481 {
7482 print_line(curwin->w_cursor.lnum, (eap->flags & EXFLAG_NR),
7483 (eap->flags & EXFLAG_LIST));
7484 ex_no_reprint = TRUE;
7485 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007486}
7487
7488/*
7489 * ":smagic" and ":snomagic".
7490 */
7491 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007492ex_submagic(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007493{
7494 int magic_save = p_magic;
7495
7496 p_magic = (eap->cmdidx == CMD_smagic);
Bram Moolenaar66e00142020-08-12 21:58:12 +02007497 ex_substitute(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007498 p_magic = magic_save;
7499}
7500
7501/*
7502 * ":join".
7503 */
7504 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007505ex_join(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007506{
7507 curwin->w_cursor.lnum = eap->line1;
7508 if (eap->line1 == eap->line2)
7509 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007510 if (eap->addr_count >= 2) // :2,2join does nothing
Bram Moolenaar071d4272004-06-13 20:20:40 +00007511 return;
7512 if (eap->line2 == curbuf->b_ml.ml_line_count)
7513 {
7514 beep_flush();
7515 return;
7516 }
7517 ++eap->line2;
7518 }
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02007519 (void)do_join(eap->line2 - eap->line1 + 1, !eap->forceit, TRUE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007520 beginline(BL_WHITE | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007521 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007522}
7523
7524/*
7525 * ":[addr]@r" or ":[addr]*r": execute register
7526 */
7527 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007528ex_at(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007529{
7530 int c;
Bram Moolenaar60462872009-11-03 11:40:19 +00007531 int prev_len = typebuf.tb_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007532
7533 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaar4930a762016-09-11 14:39:53 +02007534 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007535
7536#ifdef USE_ON_FLY_SCROLL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007537 dont_scroll = TRUE; // disallow scrolling here
Bram Moolenaar071d4272004-06-13 20:20:40 +00007538#endif
7539
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007540 // get the register name. No name means to use the previous one
Bram Moolenaar071d4272004-06-13 20:20:40 +00007541 c = *eap->arg;
7542 if (c == NUL || (c == '*' && *eap->cmd == '*'))
7543 c = '@';
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007544 // Put the register in the typeahead buffer with the "silent" flag.
Bram Moolenaard333d1e2006-11-07 17:43:47 +00007545 if (do_execreg(c, TRUE, vim_strchr(p_cpo, CPO_EXECBUF) != NULL, TRUE)
7546 == FAIL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007547 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007548 beep_flush();
Bram Moolenaardf177f62005-02-22 08:39:57 +00007549 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007550 else
7551 {
7552 int save_efr = exec_from_reg;
7553
7554 exec_from_reg = TRUE;
7555
7556 /*
7557 * Execute from the typeahead buffer.
Bram Moolenaar60462872009-11-03 11:40:19 +00007558 * Continue until the stuff buffer is empty and all added characters
7559 * have been consumed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007560 */
Bram Moolenaar60462872009-11-03 11:40:19 +00007561 while (!stuff_empty() || typebuf.tb_len > prev_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007562 (void)do_cmdline(NULL, getexline, NULL, DOCMD_NOWAIT|DOCMD_VERBOSE);
7563
7564 exec_from_reg = save_efr;
7565 }
7566}
7567
7568/*
7569 * ":!".
7570 */
7571 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007572ex_bang(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007573{
7574 do_bang(eap->addr_count, eap, eap->forceit, TRUE, TRUE);
7575}
7576
7577/*
7578 * ":undo".
7579 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007580 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +01007581ex_undo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007582{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007583 if (eap->addr_count == 1) // :undo 123
Bram Moolenaar730cde92010-06-27 05:18:54 +02007584 undo_time(eap->line2, FALSE, FALSE, TRUE);
Bram Moolenaard3667a22006-03-16 21:35:52 +00007585 else
7586 u_undo(1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007587}
7588
Bram Moolenaar55debbe2010-05-23 23:34:36 +02007589#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar6df6f472010-07-18 18:04:50 +02007590 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007591ex_wundo(exarg_T *eap)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02007592{
7593 char_u hash[UNDO_HASH_SIZE];
7594
7595 u_compute_hash(hash);
7596 u_write_undo(eap->arg, eap->forceit, curbuf, hash);
7597}
7598
Bram Moolenaar6df6f472010-07-18 18:04:50 +02007599 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007600ex_rundo(exarg_T *eap)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02007601{
7602 char_u hash[UNDO_HASH_SIZE];
7603
7604 u_compute_hash(hash);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02007605 u_read_undo(eap->arg, hash, NULL);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02007606}
7607#endif
7608
Bram Moolenaar071d4272004-06-13 20:20:40 +00007609/*
7610 * ":redo".
7611 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007612 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007613ex_redo(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007614{
7615 u_redo(1);
7616}
7617
7618/*
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007619 * ":earlier" and ":later".
7620 */
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007621 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007622ex_later(exarg_T *eap)
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007623{
7624 long count = 0;
7625 int sec = FALSE;
Bram Moolenaar730cde92010-06-27 05:18:54 +02007626 int file = FALSE;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007627 char_u *p = eap->arg;
7628
7629 if (*p == NUL)
7630 count = 1;
7631 else if (isdigit(*p))
7632 {
7633 count = getdigits(&p);
7634 switch (*p)
7635 {
7636 case 's': ++p; sec = TRUE; break;
7637 case 'm': ++p; sec = TRUE; count *= 60; break;
7638 case 'h': ++p; sec = TRUE; count *= 60 * 60; break;
Bram Moolenaar730cde92010-06-27 05:18:54 +02007639 case 'd': ++p; sec = TRUE; count *= 24 * 60 * 60; break;
7640 case 'f': ++p; file = TRUE; break;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007641 }
7642 }
7643
7644 if (*p != NUL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007645 semsg(_(e_invarg2), eap->arg);
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007646 else
Bram Moolenaar730cde92010-06-27 05:18:54 +02007647 undo_time(eap->cmdidx == CMD_earlier ? -count : count,
7648 sec, file, FALSE);
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007649}
7650
7651/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007652 * ":redir": start/stop redirection.
7653 */
7654 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007655ex_redir(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007656{
7657 char *mode;
7658 char_u *fname;
Bram Moolenaarca472992005-01-21 11:46:23 +00007659 char_u *arg = eap->arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007660
Bram Moolenaarba768492016-07-08 15:32:54 +02007661#ifdef FEAT_EVAL
Bram Moolenaar79815f12016-07-09 17:07:29 +02007662 if (redir_execute)
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02007663 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007664 emsg(_("E930: Cannot use :redir inside execute()"));
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02007665 return;
7666 }
Bram Moolenaarba768492016-07-08 15:32:54 +02007667#endif
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02007668
Bram Moolenaar071d4272004-06-13 20:20:40 +00007669 if (STRICMP(eap->arg, "END") == 0)
7670 close_redir();
7671 else
7672 {
Bram Moolenaarca472992005-01-21 11:46:23 +00007673 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00007674 {
Bram Moolenaarca472992005-01-21 11:46:23 +00007675 ++arg;
7676 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00007677 {
Bram Moolenaarca472992005-01-21 11:46:23 +00007678 ++arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007679 mode = "a";
7680 }
7681 else
7682 mode = "w";
Bram Moolenaarca472992005-01-21 11:46:23 +00007683 arg = skipwhite(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007684
7685 close_redir();
7686
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007687 // Expand environment variables and "~/".
Bram Moolenaarca472992005-01-21 11:46:23 +00007688 fname = expand_env_save(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007689 if (fname == NULL)
7690 return;
7691#ifdef FEAT_BROWSE
Bram Moolenaare1004402020-10-24 20:49:43 +02007692 if (cmdmod.cmod_flags & CMOD_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007693 {
7694 char_u *browseFile;
7695
Bram Moolenaar7171abe2004-10-11 10:06:20 +00007696 browseFile = do_browse(BROWSE_SAVE,
7697 (char_u *)_("Save Redirection"),
Bram Moolenaarc36651b2018-04-29 12:22:56 +02007698 fname, NULL, NULL,
7699 (char_u *)_(BROWSE_FILTER_ALL_FILES), curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007700 if (browseFile == NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007701 return; // operation cancelled
Bram Moolenaar071d4272004-06-13 20:20:40 +00007702 vim_free(fname);
7703 fname = browseFile;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007704 eap->forceit = TRUE; // since dialog already asked
Bram Moolenaar071d4272004-06-13 20:20:40 +00007705 }
7706#endif
7707
7708 redir_fd = open_exfile(fname, eap->forceit, mode);
7709 vim_free(fname);
7710 }
7711#ifdef FEAT_EVAL
Bram Moolenaarca472992005-01-21 11:46:23 +00007712 else if (*arg == '@')
Bram Moolenaar071d4272004-06-13 20:20:40 +00007713 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007714 // redirect to a register a-z (resp. A-Z for appending)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007715 close_redir();
Bram Moolenaarca472992005-01-21 11:46:23 +00007716 ++arg;
7717 if (ASCII_ISALPHA(*arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007718# ifdef FEAT_CLIPBOARD
Bram Moolenaarca472992005-01-21 11:46:23 +00007719 || *arg == '*'
Bram Moolenaar9a51c6e2006-11-14 19:25:02 +00007720 || *arg == '+'
Bram Moolenaar071d4272004-06-13 20:20:40 +00007721# endif
Bram Moolenaarca472992005-01-21 11:46:23 +00007722 || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00007723 {
Bram Moolenaarca472992005-01-21 11:46:23 +00007724 redir_reg = *arg++;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007725 if (*arg == '>' && arg[1] == '>') // append
Bram Moolenaard9d30582005-05-18 22:10:28 +00007726 arg += 2;
Bram Moolenaarc188b882007-10-19 14:20:54 +00007727 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007728 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007729 // Can use both "@a" and "@a>".
Bram Moolenaar2c29bee2005-06-01 21:46:07 +00007730 if (*arg == '>')
7731 arg++;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007732 // Make register empty when not using @A-@Z and the
7733 // command is valid.
Bram Moolenaarc188b882007-10-19 14:20:54 +00007734 if (*arg == NUL && !isupper(redir_reg))
7735 write_reg_contents(redir_reg, (char_u *)"", -1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007736 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00007737 }
7738 if (*arg != NUL)
7739 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00007740 redir_reg = 0;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007741 semsg(_(e_invarg2), eap->arg);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007742 }
7743 }
7744 else if (*arg == '=' && arg[1] == '>')
7745 {
7746 int append;
7747
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007748 // redirect to a variable
Bram Moolenaardf177f62005-02-22 08:39:57 +00007749 close_redir();
7750 arg += 2;
7751
7752 if (*arg == '>')
7753 {
7754 ++arg;
7755 append = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007756 }
7757 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00007758 append = FALSE;
7759
7760 if (var_redir_start(skipwhite(arg), append) == OK)
7761 redir_vname = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007762 }
7763#endif
7764
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007765 // TODO: redirect to a buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00007766
Bram Moolenaar071d4272004-06-13 20:20:40 +00007767 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007768 semsg(_(e_invarg2), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007769 }
Bram Moolenaar29b2d262006-09-10 19:07:28 +00007770
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007771 // Make sure redirection is not off. Can happen for cmdline completion
7772 // that indirectly invokes a command to catch its output.
Bram Moolenaar29b2d262006-09-10 19:07:28 +00007773 if (redir_fd != NULL
7774#ifdef FEAT_EVAL
7775 || redir_reg || redir_vname
7776#endif
7777 )
7778 redir_off = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007779}
7780
7781/*
7782 * ":redraw": force redraw
7783 */
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01007784 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007785ex_redraw(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007786{
7787 int r = RedrawingDisabled;
7788 int p = p_lz;
7789
7790 RedrawingDisabled = 0;
7791 p_lz = FALSE;
Bram Moolenaarabc39ab2017-03-01 18:04:05 +01007792 validate_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007793 update_topline();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007794 update_screen(eap->forceit ? CLEAR : VIsual_active ? INVERTED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007795#ifdef FEAT_TITLE
7796 if (need_maketitle)
7797 maketitle();
7798#endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007799#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
7800# ifdef VIMDLL
7801 if (!gui.in_use)
7802# endif
7803 resize_console_buf();
Bram Moolenaar78d21da2019-02-17 15:00:52 +01007804#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007805 RedrawingDisabled = r;
7806 p_lz = p;
7807
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007808 // Reset msg_didout, so that a message that's there is overwritten.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007809 msg_didout = FALSE;
7810 msg_col = 0;
7811
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007812 // No need to wait after an intentional redraw.
Bram Moolenaar56667a52013-07-17 11:54:28 +02007813 need_wait_return = FALSE;
7814
Bram Moolenaar071d4272004-06-13 20:20:40 +00007815 out_flush();
7816}
7817
7818/*
7819 * ":redrawstatus": force redraw of status line(s)
7820 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007821 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007822ex_redrawstatus(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007823{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007824 int r = RedrawingDisabled;
7825 int p = p_lz;
7826
7827 RedrawingDisabled = 0;
7828 p_lz = FALSE;
7829 if (eap->forceit)
7830 status_redraw_all();
7831 else
7832 status_redraw_curbuf();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007833 update_screen(VIsual_active ? INVERTED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007834 RedrawingDisabled = r;
7835 p_lz = p;
7836 out_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007837}
7838
Bram Moolenaare12bab32019-01-08 22:02:56 +01007839/*
7840 * ":redrawtabline": force redraw of the tabline
7841 */
7842 static void
7843ex_redrawtabline(exarg_T *eap UNUSED)
7844{
7845 int r = RedrawingDisabled;
7846 int p = p_lz;
7847
7848 RedrawingDisabled = 0;
7849 p_lz = FALSE;
7850
7851 draw_tabline();
7852
7853 RedrawingDisabled = r;
7854 p_lz = p;
7855 out_flush();
7856}
7857
Bram Moolenaar071d4272004-06-13 20:20:40 +00007858 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007859close_redir(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007860{
7861 if (redir_fd != NULL)
7862 {
7863 fclose(redir_fd);
7864 redir_fd = NULL;
7865 }
7866#ifdef FEAT_EVAL
7867 redir_reg = 0;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007868 if (redir_vname)
7869 {
7870 var_redir_stop();
7871 redir_vname = 0;
7872 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007873#endif
7874}
7875
Bram Moolenaarf96ae0b2019-07-28 15:21:55 +02007876#if (defined(FEAT_SESSION) || defined(FEAT_EVAL)) || defined(PROTO)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007877 int
Bram Moolenaarbd67aac2019-09-21 23:09:04 +02007878vim_mkdir_emsg(char_u *name, int prot UNUSED)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007879{
7880 if (vim_mkdir(name, prot) != 0)
7881 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007882 semsg(_("E739: Cannot create directory: %s"), name);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007883 return FAIL;
7884 }
7885 return OK;
7886}
7887#endif
7888
Bram Moolenaar071d4272004-06-13 20:20:40 +00007889/*
7890 * Open a file for writing for an Ex command, with some checks.
7891 * Return file descriptor, or NULL on failure.
7892 */
7893 FILE *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007894open_exfile(
7895 char_u *fname,
7896 int forceit,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007897 char *mode) // "w" for create new file or "a" for append
Bram Moolenaar071d4272004-06-13 20:20:40 +00007898{
7899 FILE *fd;
7900
7901#ifdef UNIX
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007902 // with Unix it is possible to open a directory
Bram Moolenaar071d4272004-06-13 20:20:40 +00007903 if (mch_isdir(fname))
7904 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007905 semsg(_(e_isadir2), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007906 return NULL;
7907 }
7908#endif
7909 if (!forceit && *mode != 'a' && vim_fexists(fname))
7910 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007911 semsg(_("E189: \"%s\" exists (add ! to override)"), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007912 return NULL;
7913 }
7914
7915 if ((fd = mch_fopen((char *)fname, mode)) == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007916 semsg(_("E190: Cannot open \"%s\" for writing"), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007917
7918 return fd;
7919}
7920
7921/*
7922 * ":mark" and ":k".
7923 */
7924 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007925ex_mark(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007926{
7927 pos_T pos;
7928
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007929 if (*eap->arg == NUL) // No argument?
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007930 emsg(_(e_argreq));
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007931 else if (eap->arg[1] != NUL) // more than one character?
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +02007932 semsg(_(e_trailing_arg), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007933 else
7934 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007935 pos = curwin->w_cursor; // save curwin->w_cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00007936 curwin->w_cursor.lnum = eap->line2;
7937 beginline(BL_WHITE | BL_FIX);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007938 if (setmark(*eap->arg) == FAIL) // set mark
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007939 emsg(_("E191: Argument must be a letter or forward/backward quote"));
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007940 curwin->w_cursor = pos; // restore curwin->w_cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00007941 }
7942}
7943
7944/*
7945 * Update w_topline, w_leftcol and the cursor position.
7946 */
7947 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007948update_topline_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007949{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007950 check_cursor(); // put cursor on valid line
Bram Moolenaar071d4272004-06-13 20:20:40 +00007951 update_topline();
7952 if (!curwin->w_p_wrap)
7953 validate_cursor();
7954 update_curswant();
7955}
7956
Bram Moolenaar071d4272004-06-13 20:20:40 +00007957/*
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007958 * Save the current State and go to Normal mode.
7959 * Return TRUE if the typeahead could be saved.
7960 */
7961 int
7962save_current_state(save_state_T *sst)
7963{
7964 sst->save_msg_scroll = msg_scroll;
7965 sst->save_restart_edit = restart_edit;
7966 sst->save_msg_didout = msg_didout;
7967 sst->save_State = State;
7968 sst->save_insertmode = p_im;
7969 sst->save_finish_op = finish_op;
7970 sst->save_opcount = opcount;
Bram Moolenaarcce713d2019-03-04 11:40:12 +01007971 sst->save_reg_executing = reg_executing;
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007972
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007973 msg_scroll = FALSE; // no msg scrolling in Normal mode
7974 restart_edit = 0; // don't go to Insert mode
7975 p_im = FALSE; // don't use 'insertmode'
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007976
7977 /*
7978 * Save the current typeahead. This is required to allow using ":normal"
7979 * from an event handler and makes sure we don't hang when the argument
7980 * ends with half a command.
7981 */
7982 save_typeahead(&sst->tabuf);
7983 return sst->tabuf.typebuf_valid;
7984}
7985
7986 void
7987restore_current_state(save_state_T *sst)
7988{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007989 // Restore the previous typeahead.
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007990 restore_typeahead(&sst->tabuf);
7991
7992 msg_scroll = sst->save_msg_scroll;
7993 restart_edit = sst->save_restart_edit;
7994 p_im = sst->save_insertmode;
7995 finish_op = sst->save_finish_op;
7996 opcount = sst->save_opcount;
Bram Moolenaarcce713d2019-03-04 11:40:12 +01007997 reg_executing = sst->save_reg_executing;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007998 msg_didout |= sst->save_msg_didout; // don't reset msg_didout now
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007999
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008000 // Restore the state (needed when called from a function executed for
8001 // 'indentexpr'). Update the mouse and cursor, they may have changed.
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008002 State = sst->save_State;
8003#ifdef CURSOR_SHAPE
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008004 ui_cursor_shape(); // may show different cursor shape
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008005#endif
8006}
8007
8008/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008009 * ":normal[!] {commands}": Execute normal mode commands.
8010 */
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01008011 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008012ex_normal(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008013{
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008014 save_state_T save_state;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008015 char_u *arg = NULL;
8016 int l;
8017 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008018
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008019 if (ex_normal_lock > 0)
8020 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008021 emsg(_(e_secure));
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008022 return;
8023 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008024 if (ex_normal_busy >= p_mmd)
8025 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008026 emsg(_("E192: Recursive use of :normal too deep"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008027 return;
8028 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008029
Bram Moolenaar071d4272004-06-13 20:20:40 +00008030 /*
8031 * vgetc() expects a CSI and K_SPECIAL to have been escaped. Don't do
8032 * this for the K_SPECIAL leading byte, otherwise special keys will not
8033 * work.
8034 */
8035 if (has_mbyte)
8036 {
8037 int len = 0;
8038
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008039 // Count the number of characters to be escaped.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008040 for (p = eap->arg; *p != NUL; ++p)
8041 {
Bram Moolenaar13505972019-01-24 15:04:48 +01008042#ifdef FEAT_GUI
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008043 if (*p == CSI) // leadbyte CSI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008044 len += 2;
Bram Moolenaar13505972019-01-24 15:04:48 +01008045#endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008046 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008047 if (*++p == K_SPECIAL // trailbyte K_SPECIAL or CSI
Bram Moolenaar13505972019-01-24 15:04:48 +01008048#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008049 || *p == CSI
Bram Moolenaar13505972019-01-24 15:04:48 +01008050#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008051 )
8052 len += 2;
8053 }
8054 if (len > 0)
8055 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02008056 arg = alloc(STRLEN(eap->arg) + len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008057 if (arg != NULL)
8058 {
8059 len = 0;
8060 for (p = eap->arg; *p != NUL; ++p)
8061 {
8062 arg[len++] = *p;
Bram Moolenaar13505972019-01-24 15:04:48 +01008063#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008064 if (*p == CSI)
8065 {
8066 arg[len++] = KS_EXTRA;
8067 arg[len++] = (int)KE_CSI;
8068 }
Bram Moolenaar13505972019-01-24 15:04:48 +01008069#endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008070 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008071 {
8072 arg[len++] = *++p;
8073 if (*p == K_SPECIAL)
8074 {
8075 arg[len++] = KS_SPECIAL;
8076 arg[len++] = KE_FILLER;
8077 }
Bram Moolenaar13505972019-01-24 15:04:48 +01008078#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008079 else if (*p == CSI)
8080 {
8081 arg[len++] = KS_EXTRA;
8082 arg[len++] = (int)KE_CSI;
8083 }
Bram Moolenaar13505972019-01-24 15:04:48 +01008084#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008085 }
8086 arg[len] = NUL;
8087 }
8088 }
8089 }
8090 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008091
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008092 ++ex_normal_busy;
8093 if (save_current_state(&save_state))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008094 {
8095 /*
8096 * Repeat the :normal command for each line in the range. When no
8097 * range given, execute it just once, without positioning the cursor
8098 * first.
8099 */
8100 do
8101 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008102 if (eap->addr_count != 0)
8103 {
8104 curwin->w_cursor.lnum = eap->line1++;
8105 curwin->w_cursor.col = 0;
Bram Moolenaard5d37532017-03-27 23:02:07 +02008106 check_cursor_moved(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008107 }
8108
Bram Moolenaar13505972019-01-24 15:04:48 +01008109 exec_normal_cmd(arg != NULL
8110 ? arg
8111 : eap->arg, eap->forceit ? REMAP_NONE : REMAP_YES, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008112 }
8113 while (eap->addr_count > 0 && eap->line1 <= eap->line2 && !got_int);
8114 }
8115
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008116 // Might not return to the main loop when in an event handler.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008117 update_topline_cursor();
8118
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008119 restore_current_state(&save_state);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008120 --ex_normal_busy;
Bram Moolenaareda73602014-11-05 09:53:23 +01008121 setmouse();
Bram Moolenaareda73602014-11-05 09:53:23 +01008122#ifdef CURSOR_SHAPE
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008123 ui_cursor_shape(); // may show different cursor shape
Bram Moolenaareda73602014-11-05 09:53:23 +01008124#endif
8125
Bram Moolenaar071d4272004-06-13 20:20:40 +00008126 vim_free(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008127}
8128
8129/*
Bram Moolenaar64969662005-12-14 21:59:55 +00008130 * ":startinsert", ":startreplace" and ":startgreplace"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008131 */
8132 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008133ex_startinsert(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008134{
Bram Moolenaarfd371682005-01-14 21:42:54 +00008135 if (eap->forceit)
8136 {
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02008137 // cursor line can be zero on startup
Bram Moolenaar09ca9322017-09-26 17:40:45 +02008138 if (!curwin->w_cursor.lnum)
8139 curwin->w_cursor.lnum = 1;
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02008140 set_cursor_for_append_to_line();
Bram Moolenaarfd371682005-01-14 21:42:54 +00008141 }
Bram Moolenaareb58a242020-04-19 18:13:19 +02008142#ifdef FEAT_TERMINAL
8143 // Ignore this when running in an active terminal.
8144 if (term_job_running(curbuf->b_term))
8145 return;
8146#endif
Bram Moolenaarfd371682005-01-14 21:42:54 +00008147
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02008148 // Ignore the command when already in Insert mode. Inserting an
8149 // expression register that invokes a function can do this.
Bram Moolenaara40c5002005-01-09 21:16:21 +00008150 if (State & INSERT)
8151 return;
8152
Bram Moolenaar64969662005-12-14 21:59:55 +00008153 if (eap->cmdidx == CMD_startinsert)
8154 restart_edit = 'a';
8155 else if (eap->cmdidx == CMD_startreplace)
8156 restart_edit = 'R';
Bram Moolenaar071d4272004-06-13 20:20:40 +00008157 else
Bram Moolenaar64969662005-12-14 21:59:55 +00008158 restart_edit = 'V';
8159
8160 if (!eap->forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008161 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00008162 if (eap->cmdidx == CMD_startinsert)
8163 restart_edit = 'i';
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02008164 curwin->w_curswant = 0; // avoid MAXCOL
Bram Moolenaar071d4272004-06-13 20:20:40 +00008165 }
Bram Moolenaar957cf672020-11-12 14:21:06 +01008166
8167 if (VIsual_active)
8168 showmode();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008169}
8170
8171/*
8172 * ":stopinsert"
8173 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008174 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008175ex_stopinsert(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008176{
8177 restart_edit = 0;
8178 stop_insert_mode = TRUE;
Bram Moolenaarfd773e92016-04-02 19:39:16 +02008179 clearmode();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008180}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008181
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008182/*
8183 * Execute normal mode command "cmd".
8184 * "remap" can be REMAP_NONE or REMAP_YES.
8185 */
8186 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008187exec_normal_cmd(char_u *cmd, int remap, int silent)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008188{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008189 // Stuff the argument into the typeahead buffer.
Bram Moolenaar25281632016-01-21 23:32:32 +01008190 ins_typebuf(cmd, remap, 0, TRUE, silent);
Bram Moolenaar586c70c2018-10-02 16:23:58 +02008191 exec_normal(FALSE, FALSE, FALSE);
Bram Moolenaar25281632016-01-21 23:32:32 +01008192}
Bram Moolenaar25281632016-01-21 23:32:32 +01008193
Bram Moolenaar25281632016-01-21 23:32:32 +01008194/*
8195 * Execute normal_cmd() until there is no typeahead left.
Bram Moolenaar586c70c2018-10-02 16:23:58 +02008196 * When "use_vpeekc" is TRUE use vpeekc() to check for available chars.
Bram Moolenaar25281632016-01-21 23:32:32 +01008197 */
8198 void
Bram Moolenaar586c70c2018-10-02 16:23:58 +02008199exec_normal(int was_typed, int use_vpeekc, int may_use_terminal_loop UNUSED)
Bram Moolenaar25281632016-01-21 23:32:32 +01008200{
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008201 oparg_T oa;
Bram Moolenaar905dd902019-04-07 14:21:47 +02008202 int c;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008203
Bram Moolenaar905dd902019-04-07 14:21:47 +02008204 // When calling vpeekc() from feedkeys() it will return Ctrl_C when there
8205 // is nothing to get, so also check for Ctrl_C.
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008206 clear_oparg(&oa);
8207 finish_op = FALSE;
Bram Moolenaar586c70c2018-10-02 16:23:58 +02008208 while ((!stuff_empty()
8209 || ((was_typed || !typebuf_typed()) && typebuf.tb_len > 0)
Bram Moolenaar905dd902019-04-07 14:21:47 +02008210 || (use_vpeekc && (c = vpeekc()) != NUL && c != Ctrl_C))
Bram Moolenaar586c70c2018-10-02 16:23:58 +02008211 && !got_int)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008212 {
8213 update_topline_cursor();
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02008214#ifdef FEAT_TERMINAL
Bram Moolenaar655a82a2018-05-08 22:01:07 +02008215 if (may_use_terminal_loop && term_use_loop()
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02008216 && oa.op_type == OP_NOP && oa.regname == NUL
8217 && !VIsual_active)
8218 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008219 // If terminal_loop() returns OK we got a key that is handled
8220 // in Normal model. With FAIL we first need to position the
8221 // cursor and the screen needs to be redrawn.
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02008222 if (terminal_loop(TRUE) == OK)
8223 normal_cmd(&oa, TRUE);
8224 }
8225 else
8226#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008227 // execute a Normal mode cmd
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02008228 normal_cmd(&oa, TRUE);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008229 }
8230}
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008231
Bram Moolenaar071d4272004-06-13 20:20:40 +00008232#ifdef FEAT_FIND_ID
8233 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008234ex_checkpath(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008235{
8236 find_pattern_in_path(NULL, 0, 0, FALSE, FALSE, CHECK_PATH, 1L,
8237 eap->forceit ? ACTION_SHOW_ALL : ACTION_SHOW,
8238 (linenr_T)1, (linenr_T)MAXLNUM);
8239}
8240
Bram Moolenaar4033c552017-09-16 20:54:51 +02008241#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008242/*
8243 * ":psearch"
8244 */
8245 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008246ex_psearch(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008247{
8248 g_do_tagpreview = p_pvh;
8249 ex_findpat(eap);
8250 g_do_tagpreview = 0;
8251}
8252#endif
8253
8254 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008255ex_findpat(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008256{
8257 int whole = TRUE;
8258 long n;
8259 char_u *p;
8260 int action;
8261
8262 switch (cmdnames[eap->cmdidx].cmd_name[2])
8263 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008264 case 'e': // ":psearch", ":isearch" and ":dsearch"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008265 if (cmdnames[eap->cmdidx].cmd_name[0] == 'p')
8266 action = ACTION_GOTO;
8267 else
8268 action = ACTION_SHOW;
8269 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008270 case 'i': // ":ilist" and ":dlist"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008271 action = ACTION_SHOW_ALL;
8272 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008273 case 'u': // ":ijump" and ":djump"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008274 action = ACTION_GOTO;
8275 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008276 default: // ":isplit" and ":dsplit"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008277 action = ACTION_SPLIT;
8278 break;
8279 }
8280
8281 n = 1;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008282 if (vim_isdigit(*eap->arg)) // get count
Bram Moolenaar071d4272004-06-13 20:20:40 +00008283 {
8284 n = getdigits(&eap->arg);
8285 eap->arg = skipwhite(eap->arg);
8286 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008287 if (*eap->arg == '/') // Match regexp, not just whole words
Bram Moolenaar071d4272004-06-13 20:20:40 +00008288 {
8289 whole = FALSE;
8290 ++eap->arg;
Bram Moolenaare8c4abb2020-04-02 21:13:25 +02008291 p = skip_regexp(eap->arg, '/', p_magic);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008292 if (*p)
8293 {
8294 *p++ = NUL;
8295 p = skipwhite(p);
8296
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008297 // Check for trailing illegal characters
Bram Moolenaarfaac4102020-04-20 17:46:14 +02008298 if (!ends_excmd2(eap->arg, p))
Bram Moolenaar8930caa2020-07-23 16:37:03 +02008299 eap->errmsg = ex_errmsg(e_trailing_arg, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008300 else
8301 eap->nextcmd = check_nextcmd(p);
8302 }
8303 }
8304 if (!eap->skip)
8305 find_pattern_in_path(eap->arg, 0, (int)STRLEN(eap->arg),
8306 whole, !eap->forceit,
8307 *eap->cmd == 'd' ? FIND_DEFINE : FIND_ANY,
8308 n, action, eap->line1, eap->line2);
8309}
8310#endif
8311
Bram Moolenaar071d4272004-06-13 20:20:40 +00008312
Bram Moolenaar4033c552017-09-16 20:54:51 +02008313#ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00008314/*
8315 * ":ptag", ":ptselect", ":ptjump", ":ptnext", etc.
8316 */
8317 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008318ex_ptag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008319{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008320 g_do_tagpreview = p_pvh; // will be reset to 0 in ex_tag_cmd()
Bram Moolenaar071d4272004-06-13 20:20:40 +00008321 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
8322}
8323
8324/*
8325 * ":pedit"
8326 */
8327 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008328ex_pedit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008329{
8330 win_T *curwin_save = curwin;
8331
Bram Moolenaar3a2505c2020-03-09 16:40:41 +01008332 if (ERROR_IF_ANY_POPUP_WINDOW)
8333 return;
8334
Bram Moolenaar026587b2019-08-17 15:08:00 +02008335 // Open the preview window or popup and make it the current window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008336 g_do_tagpreview = p_pvh;
Bram Moolenaar576a4a62019-08-18 15:25:17 +02008337 prepare_tagpreview(TRUE, TRUE, FALSE);
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02008338
Bram Moolenaar026587b2019-08-17 15:08:00 +02008339 // Edit the file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008340 do_exedit(eap, NULL);
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02008341
Bram Moolenaar071d4272004-06-13 20:20:40 +00008342 if (curwin != curwin_save && win_valid(curwin_save))
8343 {
Bram Moolenaar026587b2019-08-17 15:08:00 +02008344 // Return cursor to where we were
Bram Moolenaar071d4272004-06-13 20:20:40 +00008345 validate_cursor();
8346 redraw_later(VALID);
8347 win_enter(curwin_save, TRUE);
8348 }
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01008349# ifdef FEAT_PROP_POPUP
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02008350 else if (WIN_IS_POPUP(curwin))
8351 {
8352 // can't keep focus in popup window
8353 win_enter(firstwin, TRUE);
8354 }
8355# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008356 g_do_tagpreview = 0;
8357}
Bram Moolenaar4033c552017-09-16 20:54:51 +02008358#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008359
8360/*
8361 * ":stag", ":stselect" and ":stjump".
8362 */
8363 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008364ex_stag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008365{
8366 postponed_split = -1;
Bram Moolenaare1004402020-10-24 20:49:43 +02008367 postponed_split_flags = cmdmod.cmod_split;
8368 postponed_split_tab = cmdmod.cmod_tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008369 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
8370 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00008371 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008372}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008373
8374/*
8375 * ":tag", ":tselect", ":tjump", ":tnext", etc.
8376 */
8377 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008378ex_tag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008379{
8380 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name);
8381}
8382
8383 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008384ex_tag_cmd(exarg_T *eap, char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008385{
8386 int cmd;
8387
8388 switch (name[1])
8389 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008390 case 'j': cmd = DT_JUMP; // ":tjump"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008391 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008392 case 's': cmd = DT_SELECT; // ":tselect"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008393 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008394 case 'p': cmd = DT_PREV; // ":tprevious"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008395 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008396 case 'N': cmd = DT_PREV; // ":tNext"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008397 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008398 case 'n': cmd = DT_NEXT; // ":tnext"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008399 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008400 case 'o': cmd = DT_POP; // ":pop"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008401 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008402 case 'f': // ":tfirst"
8403 case 'r': cmd = DT_FIRST; // ":trewind"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008404 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008405 case 'l': cmd = DT_LAST; // ":tlast"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008406 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008407 default: // ":tag"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008408#ifdef FEAT_CSCOPE
Bram Moolenaar7c94c262008-06-20 09:11:34 +00008409 if (p_cst && *eap->arg != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008410 {
Bram Moolenaard4db7712016-11-12 19:16:46 +01008411 ex_cstag(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008412 return;
8413 }
8414#endif
8415 cmd = DT_TAG;
8416 break;
8417 }
8418
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00008419 if (name[0] == 'l')
8420 {
8421#ifndef FEAT_QUICKFIX
8422 ex_ni(eap);
8423 return;
8424#else
8425 cmd = DT_LTAG;
8426#endif
8427 }
8428
Bram Moolenaar071d4272004-06-13 20:20:40 +00008429 do_tag(eap->arg, cmd, eap->addr_count > 0 ? (int)eap->line2 : 1,
8430 eap->forceit, TRUE);
8431}
8432
8433/*
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008434 * Check "str" for starting with a special cmdline variable.
8435 * If found return one of the SPEC_ values and set "*usedlen" to the length of
8436 * the variable. Otherwise return -1 and "*usedlen" is unchanged.
8437 */
8438 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008439find_cmdline_var(char_u *src, int *usedlen)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008440{
8441 int len;
8442 int i;
Bram Moolenaar8f0b2d42009-05-16 14:41:10 +00008443 static char *(spec_str[]) = {
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008444 "%",
8445#define SPEC_PERC 0
8446 "#",
Bram Moolenaar65f08472017-09-10 18:16:20 +02008447#define SPEC_HASH (SPEC_PERC + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008448 "<cword>", // cursor word
Bram Moolenaar65f08472017-09-10 18:16:20 +02008449#define SPEC_CWORD (SPEC_HASH + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008450 "<cWORD>", // cursor WORD
Bram Moolenaar65f08472017-09-10 18:16:20 +02008451#define SPEC_CCWORD (SPEC_CWORD + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008452 "<cexpr>", // expr under cursor
Bram Moolenaar65f08472017-09-10 18:16:20 +02008453#define SPEC_CEXPR (SPEC_CCWORD + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008454 "<cfile>", // cursor path name
Bram Moolenaar65f08472017-09-10 18:16:20 +02008455#define SPEC_CFILE (SPEC_CEXPR + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008456 "<sfile>", // ":so" file name
Bram Moolenaar65f08472017-09-10 18:16:20 +02008457#define SPEC_SFILE (SPEC_CFILE + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008458 "<slnum>", // ":so" file line number
Bram Moolenaar65f08472017-09-10 18:16:20 +02008459#define SPEC_SLNUM (SPEC_SFILE + 1)
Bram Moolenaara5d04232020-07-26 15:37:02 +02008460 "<stack>", // call stack
8461#define SPEC_STACK (SPEC_SLNUM + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008462 "<afile>", // autocommand file name
Bram Moolenaara5d04232020-07-26 15:37:02 +02008463#define SPEC_AFILE (SPEC_STACK + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008464 "<abuf>", // autocommand buffer number
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008465#define SPEC_ABUF (SPEC_AFILE + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008466 "<amatch>", // autocommand match name
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01008467#define SPEC_AMATCH (SPEC_ABUF + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008468 "<sflnum>", // script file line number
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008469#define SPEC_SFLNUM (SPEC_AMATCH + 1)
Bram Moolenaar90944302020-08-01 20:45:11 +02008470 "<SID>", // script ID: <SNR>123_
8471#define SPEC_SID (SPEC_SFLNUM + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008472#ifdef FEAT_CLIENTSERVER
8473 "<client>"
Bram Moolenaar90944302020-08-01 20:45:11 +02008474# define SPEC_CLIENT (SPEC_SID + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008475#endif
8476 };
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008477
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00008478 for (i = 0; i < (int)(sizeof(spec_str) / sizeof(char *)); ++i)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008479 {
8480 len = (int)STRLEN(spec_str[i]);
8481 if (STRNCMP(src, spec_str[i], len) == 0)
8482 {
8483 *usedlen = len;
8484 return i;
8485 }
8486 }
8487 return -1;
8488}
8489
8490/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008491 * Evaluate cmdline variables.
8492 *
8493 * change '%' to curbuf->b_ffname
Bram Moolenaar59d8e562020-11-07 18:41:10 +01008494 * '#' to curwin->w_alt_fnum
Bram Moolenaar071d4272004-06-13 20:20:40 +00008495 * '<cword>' to word under the cursor
8496 * '<cWORD>' to WORD under the cursor
Bram Moolenaar8071cb22019-07-12 17:58:01 +02008497 * '<cexpr>' to C-expression under the cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00008498 * '<cfile>' to path name under the cursor
8499 * '<sfile>' to sourced file name
Bram Moolenaar4f25b1a2020-09-10 19:25:05 +02008500 * '<stack>' to call stack
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01008501 * '<slnum>' to sourced file line number
Bram Moolenaar071d4272004-06-13 20:20:40 +00008502 * '<afile>' to file name for autocommand
8503 * '<abuf>' to buffer number for autocommand
8504 * '<amatch>' to matching name for autocommand
8505 *
8506 * When an error is detected, "errormsg" is set to a non-NULL pointer (may be
8507 * "" for error without a message) and NULL is returned.
8508 * Returns an allocated string if a valid match was found.
8509 * Returns NULL if no match was found. "usedlen" then still contains the
8510 * number of characters to skip.
8511 */
8512 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008513eval_vars(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008514 char_u *src, // pointer into commandline
8515 char_u *srcstart, // beginning of valid memory for src
8516 int *usedlen, // characters after src that are used
8517 linenr_T *lnump, // line number for :e command, or NULL
8518 char **errormsg, // pointer to error message
8519 int *escaped) // return value has escaped white space (can
8520 // be NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008521{
8522 int i;
8523 char_u *s;
8524 char_u *result;
8525 char_u *resultbuf = NULL;
8526 int resultlen;
8527 buf_T *buf;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008528 int valid = VALID_HEAD + VALID_PATH; // assume valid result
Bram Moolenaar071d4272004-06-13 20:20:40 +00008529 int spec_idx;
Bram Moolenaarfd249462018-07-28 16:14:30 +02008530 int tilde_file = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008531 int skip_mod = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008532 char_u strbuf[30];
Bram Moolenaar071d4272004-06-13 20:20:40 +00008533
8534 *errormsg = NULL;
Bram Moolenaar63b92542007-03-27 14:57:09 +00008535 if (escaped != NULL)
8536 *escaped = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008537
8538 /*
8539 * Check if there is something to do.
8540 */
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008541 spec_idx = find_cmdline_var(src, usedlen);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008542 if (spec_idx < 0) // no match
Bram Moolenaar071d4272004-06-13 20:20:40 +00008543 {
8544 *usedlen = 1;
8545 return NULL;
8546 }
8547
8548 /*
8549 * Skip when preceded with a backslash "\%" and "\#".
8550 * Note: In "\\%" the % is also not recognized!
8551 */
8552 if (src > srcstart && src[-1] == '\\')
8553 {
8554 *usedlen = 0;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008555 STRMOVE(src - 1, src); // remove backslash
Bram Moolenaar071d4272004-06-13 20:20:40 +00008556 return NULL;
8557 }
8558
8559 /*
8560 * word or WORD under cursor
8561 */
Bram Moolenaar65f08472017-09-10 18:16:20 +02008562 if (spec_idx == SPEC_CWORD || spec_idx == SPEC_CCWORD
8563 || spec_idx == SPEC_CEXPR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008564 {
Bram Moolenaar65f08472017-09-10 18:16:20 +02008565 resultlen = find_ident_under_cursor(&result,
8566 spec_idx == SPEC_CWORD ? (FIND_IDENT | FIND_STRING)
8567 : spec_idx == SPEC_CEXPR ? (FIND_IDENT | FIND_STRING | FIND_EVAL)
8568 : FIND_STRING);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008569 if (resultlen == 0)
8570 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008571 *errormsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +00008572 return NULL;
8573 }
8574 }
8575
8576 /*
8577 * '#': Alternate file name
8578 * '%': Current file name
8579 * File name under the cursor
8580 * File name for autocommand
8581 * and following modifiers
8582 */
8583 else
8584 {
8585 switch (spec_idx)
8586 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008587 case SPEC_PERC: // '%': current file
Bram Moolenaar071d4272004-06-13 20:20:40 +00008588 if (curbuf->b_fname == NULL)
8589 {
8590 result = (char_u *)"";
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008591 valid = 0; // Must have ":p:h" to be valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00008592 }
8593 else
Bram Moolenaar00136dc2018-07-25 21:19:13 +02008594 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008595 result = curbuf->b_fname;
Bram Moolenaar00136dc2018-07-25 21:19:13 +02008596 tilde_file = STRCMP(result, "~") == 0;
8597 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008598 break;
8599
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008600 case SPEC_HASH: // '#' or "#99": alternate file
8601 if (src[1] == '#') // "##": the argument list
Bram Moolenaar071d4272004-06-13 20:20:40 +00008602 {
8603 result = arg_all();
8604 resultbuf = result;
8605 *usedlen = 2;
Bram Moolenaar63b92542007-03-27 14:57:09 +00008606 if (escaped != NULL)
8607 *escaped = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008608 skip_mod = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008609 break;
8610 }
8611 s = src + 1;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008612 if (*s == '<') // "#<99" uses v:oldfiles
Bram Moolenaard812df62008-11-09 12:46:09 +00008613 ++s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008614 i = (int)getdigits(&s);
Bram Moolenaarc312b8b2017-10-28 17:53:04 +02008615 if (s == src + 2 && src[1] == '-')
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008616 // just a minus sign, don't skip over it
Bram Moolenaarc312b8b2017-10-28 17:53:04 +02008617 s--;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008618 *usedlen = (int)(s - src); // length of what we expand
Bram Moolenaar071d4272004-06-13 20:20:40 +00008619
Bram Moolenaarc312b8b2017-10-28 17:53:04 +02008620 if (src[1] == '<' && i != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008621 {
Bram Moolenaard812df62008-11-09 12:46:09 +00008622 if (*usedlen < 2)
8623 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008624 // Should we give an error message for #<text?
Bram Moolenaard812df62008-11-09 12:46:09 +00008625 *usedlen = 1;
8626 return NULL;
8627 }
8628#ifdef FEAT_EVAL
8629 result = list_find_str(get_vim_var_list(VV_OLDFILES),
8630 (long)i);
8631 if (result == NULL)
8632 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008633 *errormsg = "";
Bram Moolenaard812df62008-11-09 12:46:09 +00008634 return NULL;
8635 }
8636#else
Bram Moolenaar8e481e82019-01-15 20:07:48 +01008637 *errormsg = _("E809: #< is not available without the +eval feature");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008638 return NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +00008639#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008640 }
8641 else
Bram Moolenaard812df62008-11-09 12:46:09 +00008642 {
Bram Moolenaarc312b8b2017-10-28 17:53:04 +02008643 if (i == 0 && src[1] == '<' && *usedlen > 1)
8644 *usedlen = 1;
Bram Moolenaard812df62008-11-09 12:46:09 +00008645 buf = buflist_findnr(i);
8646 if (buf == NULL)
8647 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008648 *errormsg = _("E194: No alternate file name to substitute for '#'");
Bram Moolenaard812df62008-11-09 12:46:09 +00008649 return NULL;
8650 }
8651 if (lnump != NULL)
8652 *lnump = ECMD_LAST;
8653 if (buf->b_fname == NULL)
8654 {
8655 result = (char_u *)"";
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008656 valid = 0; // Must have ":p:h" to be valid
Bram Moolenaard812df62008-11-09 12:46:09 +00008657 }
8658 else
Bram Moolenaar00136dc2018-07-25 21:19:13 +02008659 {
Bram Moolenaard812df62008-11-09 12:46:09 +00008660 result = buf->b_fname;
Bram Moolenaar00136dc2018-07-25 21:19:13 +02008661 tilde_file = STRCMP(result, "~") == 0;
8662 }
Bram Moolenaard812df62008-11-09 12:46:09 +00008663 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008664 break;
8665
8666#ifdef FEAT_SEARCHPATH
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008667 case SPEC_CFILE: // file name under cursor
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008668 result = file_name_at_cursor(FNAME_MESS|FNAME_HYP, 1L, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008669 if (result == NULL)
8670 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008671 *errormsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +00008672 return NULL;
8673 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008674 resultbuf = result; // remember allocated string
Bram Moolenaar071d4272004-06-13 20:20:40 +00008675 break;
8676#endif
8677
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008678 case SPEC_AFILE: // file name for autocommand
Bram Moolenaar071d4272004-06-13 20:20:40 +00008679 result = autocmd_fname;
Bram Moolenaarf6dad432008-09-18 19:29:58 +00008680 if (result != NULL && !autocmd_fname_full)
8681 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008682 // Still need to turn the fname into a full path. It is
8683 // postponed to avoid a delay when <afile> is not used.
Bram Moolenaarf6dad432008-09-18 19:29:58 +00008684 autocmd_fname_full = TRUE;
8685 result = FullName_save(autocmd_fname, FALSE);
8686 vim_free(autocmd_fname);
8687 autocmd_fname = result;
8688 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008689 if (result == NULL)
8690 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008691 *errormsg = _("E495: no autocommand file name to substitute for \"<afile>\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008692 return NULL;
8693 }
Bram Moolenaara0174af2008-01-02 20:08:25 +00008694 result = shorten_fname1(result);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008695 break;
8696
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008697 case SPEC_ABUF: // buffer number for autocommand
Bram Moolenaar071d4272004-06-13 20:20:40 +00008698 if (autocmd_bufnr <= 0)
8699 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008700 *errormsg = _("E496: no autocommand buffer number to substitute for \"<abuf>\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008701 return NULL;
8702 }
8703 sprintf((char *)strbuf, "%d", autocmd_bufnr);
8704 result = strbuf;
8705 break;
8706
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008707 case SPEC_AMATCH: // match name for autocommand
Bram Moolenaar071d4272004-06-13 20:20:40 +00008708 result = autocmd_match;
8709 if (result == NULL)
8710 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008711 *errormsg = _("E497: no autocommand match name to substitute for \"<amatch>\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008712 return NULL;
8713 }
8714 break;
8715
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008716 case SPEC_SFILE: // file name for ":so" command
Bram Moolenaara5d04232020-07-26 15:37:02 +02008717 case SPEC_STACK: // call stack
Bram Moolenaar4f25b1a2020-09-10 19:25:05 +02008718 result = estack_sfile(spec_idx == SPEC_SFILE
8719 ? ESTACK_SFILE : ESTACK_STACK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008720 if (result == NULL)
8721 {
Bram Moolenaara5d04232020-07-26 15:37:02 +02008722 *errormsg = spec_idx == SPEC_SFILE
8723 ? _("E498: no :source file name to substitute for \"<sfile>\"")
8724 : _("E489: no call stack to substitute for \"<stack>\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008725 return NULL;
8726 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01008727 resultbuf = result; // remember allocated string
Bram Moolenaar071d4272004-06-13 20:20:40 +00008728 break;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008729
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008730 case SPEC_SLNUM: // line in file for ":so" command
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01008731 if (SOURCING_NAME == NULL || SOURCING_LNUM == 0)
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01008732 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008733 *errormsg = _("E842: no line number to use for \"<slnum>\"");
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01008734 return NULL;
8735 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01008736 sprintf((char *)strbuf, "%ld", SOURCING_LNUM);
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01008737 result = strbuf;
8738 break;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008739
8740#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008741 case SPEC_SFLNUM: // line in script file
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01008742 if (current_sctx.sc_lnum + SOURCING_LNUM == 0)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008743 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008744 *errormsg = _("E961: no line number to use for \"<sflnum>\"");
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008745 return NULL;
8746 }
8747 sprintf((char *)strbuf, "%ld",
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01008748 (long)(current_sctx.sc_lnum + SOURCING_LNUM));
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008749 result = strbuf;
8750 break;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008751
Bram Moolenaar90944302020-08-01 20:45:11 +02008752 case SPEC_SID:
8753 if (current_sctx.sc_sid <= 0)
8754 {
8755 *errormsg = _(e_usingsid);
8756 return NULL;
8757 }
8758 sprintf((char *)strbuf, "<SNR>%d_", current_sctx.sc_sid);
8759 result = strbuf;
8760 break;
Bram Moolenaare4218b92020-08-01 21:11:38 +02008761#endif
Bram Moolenaar90944302020-08-01 20:45:11 +02008762
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008763#ifdef FEAT_CLIENTSERVER
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008764 case SPEC_CLIENT: // Source of last submitted input
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008765 sprintf((char *)strbuf, PRINTF_HEX_LONG_U,
8766 (long_u)clientWindow);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008767 result = strbuf;
8768 break;
8769#endif
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008770
Bram Moolenaar9e0f6ec2017-05-16 09:36:54 +02008771 default:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008772 result = (char_u *)""; // avoid gcc warning
Bram Moolenaar9e0f6ec2017-05-16 09:36:54 +02008773 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008774 }
8775
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008776 resultlen = (int)STRLEN(result); // length of new string
8777 if (src[*usedlen] == '<') // remove the file name extension
Bram Moolenaar071d4272004-06-13 20:20:40 +00008778 {
8779 ++*usedlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008780 if ((s = vim_strrchr(result, '.')) != NULL && s >= gettail(result))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008781 resultlen = (int)(s - result);
8782 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008783 else if (!skip_mod)
8784 {
Bram Moolenaar00136dc2018-07-25 21:19:13 +02008785 valid |= modify_fname(src, tilde_file, usedlen, &result, &resultbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008786 &resultlen);
8787 if (result == NULL)
8788 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008789 *errormsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +00008790 return NULL;
8791 }
8792 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008793 }
8794
8795 if (resultlen == 0 || valid != VALID_HEAD + VALID_PATH)
8796 {
8797 if (valid != VALID_HEAD + VALID_PATH)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008798 // xgettext:no-c-format
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008799 *errormsg = _("E499: Empty file name for '%' or '#', only works with \":p:h\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008800 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008801 *errormsg = _("E500: Evaluates to an empty string");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008802 result = NULL;
8803 }
8804 else
8805 result = vim_strnsave(result, resultlen);
8806 vim_free(resultbuf);
8807 return result;
8808}
8809
8810/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008811 * Expand the <sfile> string in "arg".
8812 *
8813 * Returns an allocated string, or NULL for any error.
8814 */
8815 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008816expand_sfile(char_u *arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008817{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008818 char *errormsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008819 int len;
8820 char_u *result;
8821 char_u *newres;
8822 char_u *repl;
8823 int srclen;
8824 char_u *p;
8825
8826 result = vim_strsave(arg);
8827 if (result == NULL)
8828 return NULL;
8829
8830 for (p = result; *p; )
8831 {
8832 if (STRNCMP(p, "<sfile>", 7) != 0)
8833 ++p;
8834 else
8835 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008836 // replace "<sfile>" with the sourced file name, and do ":" stuff
Bram Moolenaar63b92542007-03-27 14:57:09 +00008837 repl = eval_vars(p, result, &srclen, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008838 if (errormsg != NULL)
8839 {
8840 if (*errormsg)
8841 emsg(errormsg);
8842 vim_free(result);
8843 return NULL;
8844 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008845 if (repl == NULL) // no match (cannot happen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008846 {
8847 p += srclen;
8848 continue;
8849 }
8850 len = (int)STRLEN(result) - srclen + (int)STRLEN(repl) + 1;
8851 newres = alloc(len);
8852 if (newres == NULL)
8853 {
8854 vim_free(repl);
8855 vim_free(result);
8856 return NULL;
8857 }
8858 mch_memmove(newres, result, (size_t)(p - result));
8859 STRCPY(newres + (p - result), repl);
8860 len = (int)STRLEN(newres);
8861 STRCAT(newres, p + srclen);
8862 vim_free(repl);
8863 vim_free(result);
8864 result = newres;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008865 p = newres + len; // continue after the match
Bram Moolenaar071d4272004-06-13 20:20:40 +00008866 }
8867 }
8868
8869 return result;
8870}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008871
Bram Moolenaar071d4272004-06-13 20:20:40 +00008872#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG) || defined(PROTO)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00008873/*
Bram Moolenaard9462e32011-04-11 21:35:11 +02008874 * Make a dialog message in "buff[DIALOG_MSG_SIZE]".
Bram Moolenaarb765d632005-06-07 21:00:02 +00008875 * "format" must contain "%s".
Bram Moolenaar9c13b352005-05-19 20:53:52 +00008876 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008877 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008878dialog_msg(char_u *buff, char *format, char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008879{
Bram Moolenaar071d4272004-06-13 20:20:40 +00008880 if (fname == NULL)
8881 fname = (char_u *)_("Untitled");
Bram Moolenaard9462e32011-04-11 21:35:11 +02008882 vim_snprintf((char *)buff, DIALOG_MSG_SIZE, format, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008883}
8884#endif
8885
8886/*
8887 * ":behave {mswin,xterm}"
8888 */
8889 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008890ex_behave(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008891{
8892 if (STRCMP(eap->arg, "mswin") == 0)
8893 {
8894 set_option_value((char_u *)"selection", 0L, (char_u *)"exclusive", 0);
8895 set_option_value((char_u *)"selectmode", 0L, (char_u *)"mouse,key", 0);
8896 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"popup", 0);
8897 set_option_value((char_u *)"keymodel", 0L,
8898 (char_u *)"startsel,stopsel", 0);
8899 }
8900 else if (STRCMP(eap->arg, "xterm") == 0)
8901 {
8902 set_option_value((char_u *)"selection", 0L, (char_u *)"inclusive", 0);
8903 set_option_value((char_u *)"selectmode", 0L, (char_u *)"", 0);
8904 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"extend", 0);
8905 set_option_value((char_u *)"keymodel", 0L, (char_u *)"", 0);
8906 }
8907 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008908 semsg(_(e_invarg2), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008909}
8910
Bram Moolenaar071d4272004-06-13 20:20:40 +00008911static int filetype_detect = FALSE;
8912static int filetype_plugin = FALSE;
8913static int filetype_indent = FALSE;
8914
8915/*
8916 * ":filetype [plugin] [indent] {on,off,detect}"
8917 * on: Load the filetype.vim file to install autocommands for file types.
8918 * off: Load the ftoff.vim file to remove all autocommands for file types.
8919 * plugin on: load filetype.vim and ftplugin.vim
8920 * plugin off: load ftplugof.vim
8921 * indent on: load filetype.vim and indent.vim
8922 * indent off: load indoff.vim
8923 */
8924 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008925ex_filetype(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008926{
8927 char_u *arg = eap->arg;
8928 int plugin = FALSE;
8929 int indent = FALSE;
8930
8931 if (*eap->arg == NUL)
8932 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008933 // Print current status.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008934 smsg("filetype detection:%s plugin:%s indent:%s",
Bram Moolenaar071d4272004-06-13 20:20:40 +00008935 filetype_detect ? "ON" : "OFF",
8936 filetype_plugin ? (filetype_detect ? "ON" : "(on)") : "OFF",
8937 filetype_indent ? (filetype_detect ? "ON" : "(on)") : "OFF");
8938 return;
8939 }
8940
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008941 // Accept "plugin" and "indent" in any order.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008942 for (;;)
8943 {
8944 if (STRNCMP(arg, "plugin", 6) == 0)
8945 {
8946 plugin = TRUE;
8947 arg = skipwhite(arg + 6);
8948 continue;
8949 }
8950 if (STRNCMP(arg, "indent", 6) == 0)
8951 {
8952 indent = TRUE;
8953 arg = skipwhite(arg + 6);
8954 continue;
8955 }
8956 break;
8957 }
8958 if (STRCMP(arg, "on") == 0 || STRCMP(arg, "detect") == 0)
8959 {
8960 if (*arg == 'o' || !filetype_detect)
8961 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01008962 source_runtime((char_u *)FILETYPE_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008963 filetype_detect = TRUE;
8964 if (plugin)
8965 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01008966 source_runtime((char_u *)FTPLUGIN_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008967 filetype_plugin = TRUE;
8968 }
8969 if (indent)
8970 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01008971 source_runtime((char_u *)INDENT_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008972 filetype_indent = TRUE;
8973 }
8974 }
8975 if (*arg == 'd')
8976 {
Bram Moolenaar1610d052016-06-09 22:53:01 +02008977 (void)do_doautocmd((char_u *)"filetypedetect BufRead", TRUE, NULL);
Bram Moolenaara3227e22006-03-08 21:32:40 +00008978 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008979 }
8980 }
8981 else if (STRCMP(arg, "off") == 0)
8982 {
8983 if (plugin || indent)
8984 {
8985 if (plugin)
8986 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01008987 source_runtime((char_u *)FTPLUGOF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008988 filetype_plugin = FALSE;
8989 }
8990 if (indent)
8991 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01008992 source_runtime((char_u *)INDOFF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008993 filetype_indent = FALSE;
8994 }
8995 }
8996 else
8997 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01008998 source_runtime((char_u *)FTOFF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008999 filetype_detect = FALSE;
9000 }
9001 }
9002 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009003 semsg(_(e_invarg2), arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009004}
9005
9006/*
Bram Moolenaar3e545692017-06-04 19:00:32 +02009007 * ":setfiletype [FALLBACK] {name}"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009008 */
9009 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009010ex_setfiletype(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009011{
9012 if (!did_filetype)
Bram Moolenaar3e545692017-06-04 19:00:32 +02009013 {
9014 char_u *arg = eap->arg;
9015
9016 if (STRNCMP(arg, "FALLBACK ", 9) == 0)
9017 arg += 9;
9018
9019 set_option_value((char_u *)"filetype", 0L, arg, OPT_LOCAL);
9020 if (arg != eap->arg)
9021 did_filetype = FALSE;
9022 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009023}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009024
Bram Moolenaar071d4272004-06-13 20:20:40 +00009025 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009026ex_digraphs(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009027{
9028#ifdef FEAT_DIGRAPHS
9029 if (*eap->arg != NUL)
9030 putdigraph(eap->arg);
9031 else
Bram Moolenaareae8ae12018-12-14 18:53:02 +01009032 listdigraphs(eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009033#else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009034 emsg(_("E196: No digraphs in this version"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009035#endif
9036}
9037
Bram Moolenaar451fc7b2018-04-27 22:53:07 +02009038#if defined(FEAT_SEARCH_EXTRA) || defined(PROTO)
9039 void
9040set_no_hlsearch(int flag)
9041{
9042 no_hlsearch = flag;
9043# ifdef FEAT_EVAL
9044 set_vim_var_nr(VV_HLSEARCH, !no_hlsearch && p_hls);
9045# endif
9046}
9047
Bram Moolenaar071d4272004-06-13 20:20:40 +00009048/*
9049 * ":nohlsearch"
9050 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009051 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009052ex_nohlsearch(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009053{
Bram Moolenaar451fc7b2018-04-27 22:53:07 +02009054 set_no_hlsearch(TRUE);
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00009055 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009056}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009057#endif
9058
9059#ifdef FEAT_CRYPT
9060/*
9061 * ":X": Get crypt key
9062 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009063 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009064ex_X(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009065{
Bram Moolenaar3a0c9082014-11-12 15:15:42 +01009066 crypt_check_current_method();
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02009067 (void)crypt_get_key(TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009068}
9069#endif
9070
9071#ifdef FEAT_FOLDING
9072 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009073ex_fold(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009074{
9075 if (foldManualAllowed(TRUE))
9076 foldCreate(eap->line1, eap->line2);
9077}
9078
9079 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009080ex_foldopen(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009081{
9082 opFoldRange(eap->line1, eap->line2, eap->cmdidx == CMD_foldopen,
9083 eap->forceit, FALSE);
9084}
9085
9086 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009087ex_folddo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009088{
9089 linenr_T lnum;
9090
Bram Moolenaar4facea32019-10-12 20:17:40 +02009091# ifdef FEAT_CLIPBOARD
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02009092 start_global_changes();
Bram Moolenaar4facea32019-10-12 20:17:40 +02009093# endif
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02009094
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009095 // First set the marks for all lines closed/open.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009096 for (lnum = eap->line1; lnum <= eap->line2; ++lnum)
9097 if (hasFolding(lnum, NULL, NULL) == (eap->cmdidx == CMD_folddoclosed))
9098 ml_setmarked(lnum);
9099
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009100 // Execute the command on the marked lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009101 global_exe(eap->arg);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01009102 ml_clearmarked(); // clear rest of the marks
Bram Moolenaar4facea32019-10-12 20:17:40 +02009103# ifdef FEAT_CLIPBOARD
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02009104 end_global_changes();
Bram Moolenaar4facea32019-10-12 20:17:40 +02009105# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009106}
9107#endif
Bram Moolenaarf5291f32017-09-14 22:55:37 +02009108
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01009109#if defined(FEAT_QUICKFIX) || defined(PROTO)
Bram Moolenaar39665952018-08-15 20:59:48 +02009110/*
9111 * Returns TRUE if the supplied Ex cmdidx is for a location list command
9112 * instead of a quickfix command.
9113 */
9114 int
9115is_loclist_cmd(int cmdidx)
9116{
Bram Moolenaar74c8be22018-08-23 22:51:40 +02009117 if (cmdidx < 0 || cmdidx >= CMD_SIZE)
Bram Moolenaar39665952018-08-15 20:59:48 +02009118 return FALSE;
9119 return cmdnames[cmdidx].cmd_name[0] == 'l';
9120}
9121#endif
9122
Bram Moolenaar4facea32019-10-12 20:17:40 +02009123#if defined(FEAT_TIMERS) || defined(PROTO)
Bram Moolenaarf5291f32017-09-14 22:55:37 +02009124 int
9125get_pressedreturn(void)
9126{
9127 return ex_pressedreturn;
9128}
9129
9130 void
9131set_pressedreturn(int val)
9132{
9133 ex_pressedreturn = val;
9134}
9135#endif