blob: 193cfcfdffab49b0849bf090fadc15324319ac80 [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 Moolenaarddef1292019-12-16 17:10:33 +010023static char_u *do_one_cmd(char_u **, int, cstack_T *, char_u *(*fgetline)(int, void *, int, int), void *cookie);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024#else
Bram Moolenaare96a2492019-06-25 04:12:16 +020025static char_u *do_one_cmd(char_u **, int, char_u *(*fgetline)(int, void *, int, int), 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 Moolenaar33c4dbb2018-08-14 16:06:16 +020028static void free_cmdmod(void);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010029static void append_command(char_u *cmd);
30static char_u *find_command(exarg_T *eap, int *full);
Bram Moolenaar071d4272004-06-13 20:20:40 +000031
Bram Moolenaar071d4272004-06-13 20:20:40 +000032#ifndef FEAT_MENU
33# define ex_emenu ex_ni
34# define ex_menu ex_ni
35# define ex_menutranslate ex_ni
36#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010037static void ex_autocmd(exarg_T *eap);
38static void ex_doautocmd(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010039static void ex_bunload(exarg_T *eap);
40static void ex_buffer(exarg_T *eap);
41static void ex_bmodified(exarg_T *eap);
42static void ex_bnext(exarg_T *eap);
43static void ex_bprevious(exarg_T *eap);
44static void ex_brewind(exarg_T *eap);
45static void ex_blast(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010046static char_u *getargcmd(char_u **);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010047static int getargopt(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +000048#ifndef FEAT_QUICKFIX
49# define ex_make ex_ni
Bram Moolenaar86b68352004-12-27 21:59:20 +000050# define ex_cbuffer ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +000051# define ex_cc ex_ni
52# define ex_cnext ex_ni
Bram Moolenaar3ff33112019-05-03 21:56:35 +020053# define ex_cbelow ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +000054# define ex_cfile ex_ni
55# define qf_list ex_ni
56# define qf_age ex_ni
Bram Moolenaarf6acffb2016-07-16 16:54:24 +020057# define qf_history ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +000058# define ex_helpgrep ex_ni
Bram Moolenaar86b68352004-12-27 21:59:20 +000059# define ex_vimgrep ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +000060#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +020061#if !defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +000062# define ex_cclose ex_ni
63# define ex_copen ex_ni
64# define ex_cwindow ex_ni
Bram Moolenaardcb17002016-07-07 18:58:59 +020065# define ex_cbottom ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +000066#endif
Bram Moolenaar1e015462005-09-25 22:16:38 +000067#if !defined(FEAT_QUICKFIX) || !defined(FEAT_EVAL)
68# define ex_cexpr ex_ni
69#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000070
Bram Moolenaarb7316892019-05-01 18:08:42 +020071static 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 Moolenaarf28dbce2016-01-29 22:03:47 +010072static void get_flags(exarg_T *eap);
Bram Moolenaar85363ab2010-07-18 13:58:26 +020073#if !defined(FEAT_PERL) \
74 || !defined(FEAT_PYTHON) || !defined(FEAT_PYTHON3) \
75 || !defined(FEAT_TCL) \
76 || !defined(FEAT_RUBY) \
77 || !defined(FEAT_LUA) \
78 || !defined(FEAT_MZSCHEME)
Bram Moolenaar7bb75552007-07-16 18:39:49 +000079# define HAVE_EX_SCRIPT_NI
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010080static void ex_script_ni(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +000081#endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010082static char *invalid_range(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010083static void correct_range(exarg_T *eap);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000084#ifdef FEAT_QUICKFIX
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010085static char_u *replace_makeprg(exarg_T *eap, char_u *p, char_u **cmdlinep);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000086#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010087static char_u *repl_cmdline(exarg_T *eap, char_u *src, int srclen, char_u *repl, char_u **cmdlinep);
88static void ex_highlight(exarg_T *eap);
89static void ex_colorscheme(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010090static void ex_cquit(exarg_T *eap);
91static void ex_quit_all(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010092static void ex_close(exarg_T *eap);
93static void ex_win_close(int forceit, win_T *win, tabpage_T *tp);
94static void ex_only(exarg_T *eap);
95static void ex_resize(exarg_T *eap);
96static void ex_stag(exarg_T *eap);
97static void ex_tabclose(exarg_T *eap);
98static void ex_tabonly(exarg_T *eap);
99static void ex_tabnext(exarg_T *eap);
100static void ex_tabmove(exarg_T *eap);
101static void ex_tabs(exarg_T *eap);
Bram Moolenaar4033c552017-09-16 20:54:51 +0200102#if defined(FEAT_QUICKFIX)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100103static void ex_pclose(exarg_T *eap);
104static void ex_ptag(exarg_T *eap);
105static void ex_pedit(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000106#else
107# define ex_pclose ex_ni
108# define ex_ptag ex_ni
109# define ex_pedit ex_ni
110#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100111static void ex_hide(exarg_T *eap);
112static void ex_stop(exarg_T *eap);
113static void ex_exit(exarg_T *eap);
114static void ex_print(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000115#ifdef FEAT_BYTEOFF
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100116static void ex_goto(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000117#else
118# define ex_goto ex_ni
119#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100120static void ex_shell(exarg_T *eap);
121static void ex_preserve(exarg_T *eap);
122static void ex_recover(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100123static void ex_mode(exarg_T *eap);
124static void ex_wrongmodifier(exarg_T *eap);
125static void ex_find(exarg_T *eap);
126static void ex_open(exarg_T *eap);
127static void ex_edit(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000128#ifndef FEAT_GUI
129# define ex_gui ex_nogui
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100130static void ex_nogui(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000131#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +0100132#if defined(FEAT_GUI_MSWIN) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100133static void ex_tearoff(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000134#else
135# define ex_tearoff ex_ni
136#endif
Bram Moolenaar29a2c082018-03-05 21:06:23 +0100137#if (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK) \
138 || defined(FEAT_TERM_POPUP_MENU)) && defined(FEAT_MENU)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100139static void ex_popup(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000140#else
141# define ex_popup ex_ni
142#endif
143#ifndef FEAT_GUI_MSWIN
144# define ex_simalt ex_ni
145#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +0000146#if !defined(FEAT_GUI_MSWIN) && !defined(FEAT_GUI_GTK) && !defined(FEAT_GUI_MOTIF)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000147# define gui_mch_find_dialog ex_ni
148# define gui_mch_replace_dialog ex_ni
149#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +0000150#if !defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000151# define ex_helpfind ex_ni
152#endif
153#ifndef FEAT_CSCOPE
Bram Moolenaard4db7712016-11-12 19:16:46 +0100154# define ex_cscope ex_ni
155# define ex_scscope ex_ni
156# define ex_cstag ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000157#endif
158#ifndef FEAT_SYN_HL
159# define ex_syntax ex_ni
Bram Moolenaar860cae12010-06-05 23:22:07 +0200160# define ex_ownsyntax ex_ni
Bram Moolenaarf71a3db2006-03-12 21:50:18 +0000161#endif
Bram Moolenaarf7512552013-06-06 14:55:19 +0200162#if !defined(FEAT_SYN_HL) || !defined(FEAT_PROFILE)
Bram Moolenaar8a7f5a22013-06-06 14:01:46 +0200163# define ex_syntime ex_ni
164#endif
Bram Moolenaarf71a3db2006-03-12 21:50:18 +0000165#ifndef FEAT_SPELL
Bram Moolenaarb765d632005-06-07 21:00:02 +0000166# define ex_spell ex_ni
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000167# define ex_mkspell ex_ni
Bram Moolenaarf417f2b2005-06-23 22:29:21 +0000168# define ex_spelldump ex_ni
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000169# define ex_spellinfo ex_ni
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000170# define ex_spellrepall ex_ni
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000171#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200172#ifndef FEAT_PERSISTENT_UNDO
173# define ex_rundo ex_ni
174# define ex_wundo ex_ni
175#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200176#ifndef FEAT_LUA
177# define ex_lua ex_script_ni
178# define ex_luado ex_ni
179# define ex_luafile ex_ni
180#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000181#ifndef FEAT_MZSCHEME
182# define ex_mzscheme ex_script_ni
183# define ex_mzfile ex_ni
184#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000185#ifndef FEAT_PERL
186# define ex_perl ex_script_ni
187# define ex_perldo ex_ni
188#endif
189#ifndef FEAT_PYTHON
190# define ex_python ex_script_ni
Bram Moolenaard620aa92013-05-17 16:40:06 +0200191# define ex_pydo ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000192# define ex_pyfile ex_ni
193#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200194#ifndef FEAT_PYTHON3
Bram Moolenaar368373e2010-07-19 20:46:22 +0200195# define ex_py3 ex_script_ni
Bram Moolenaar3dab2802013-05-15 18:28:13 +0200196# define ex_py3do ex_ni
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200197# define ex_py3file ex_ni
198#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +0100199#if !defined(FEAT_PYTHON) && !defined(FEAT_PYTHON3)
200# define ex_pyx ex_script_ni
201# define ex_pyxdo ex_ni
202# define ex_pyxfile ex_ni
203#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000204#ifndef FEAT_TCL
205# define ex_tcl ex_script_ni
206# define ex_tcldo ex_ni
207# define ex_tclfile ex_ni
208#endif
209#ifndef FEAT_RUBY
210# define ex_ruby ex_script_ni
211# define ex_rubydo ex_ni
212# define ex_rubyfile ex_ni
213#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000214#ifndef FEAT_KEYMAP
215# define ex_loadkeymap ex_ni
216#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100217static void ex_swapname(exarg_T *eap);
218static void ex_syncbind(exarg_T *eap);
219static void ex_read(exarg_T *eap);
220static void ex_pwd(exarg_T *eap);
221static void ex_equal(exarg_T *eap);
222static void ex_sleep(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100223static void ex_winsize(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100224static void ex_wincmd(exarg_T *eap);
Bram Moolenaar843ee412004-06-30 16:16:41 +0000225#if defined(FEAT_GUI) || defined(UNIX) || defined(VMS) || defined(MSWIN)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100226static void ex_winpos(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000227#else
228# define ex_winpos ex_ni
229#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100230static void ex_operators(exarg_T *eap);
231static void ex_put(exarg_T *eap);
232static void ex_copymove(exarg_T *eap);
233static void ex_submagic(exarg_T *eap);
234static void ex_join(exarg_T *eap);
235static void ex_at(exarg_T *eap);
236static void ex_bang(exarg_T *eap);
237static void ex_undo(exarg_T *eap);
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200238#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100239static void ex_wundo(exarg_T *eap);
240static void ex_rundo(exarg_T *eap);
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200241#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100242static void ex_redo(exarg_T *eap);
243static void ex_later(exarg_T *eap);
244static void ex_redir(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100245static void ex_redrawstatus(exarg_T *eap);
Bram Moolenaare12bab32019-01-08 22:02:56 +0100246static void ex_redrawtabline(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100247static void close_redir(void);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100248static void ex_mark(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100249static void ex_startinsert(exarg_T *eap);
250static void ex_stopinsert(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000251#ifdef FEAT_FIND_ID
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100252static void ex_checkpath(exarg_T *eap);
253static void ex_findpat(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000254#else
255# define ex_findpat ex_ni
256# define ex_checkpath ex_ni
257#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +0200258#if defined(FEAT_FIND_ID) && defined(FEAT_QUICKFIX)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100259static void ex_psearch(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000260#else
261# define ex_psearch ex_ni
262#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100263static void ex_tag(exarg_T *eap);
264static void ex_tag_cmd(exarg_T *eap, char_u *name);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000265#ifndef FEAT_EVAL
Bram Moolenaare4ce8252019-08-04 15:30:16 +0200266# define ex_break ex_ni
267# define ex_breakadd ex_ni
268# define ex_breakdel ex_ni
269# define ex_breaklist ex_ni
270# define ex_call ex_ni
271# define ex_catch ex_ni
272# define ex_compiler ex_ni
273# define ex_const ex_ni
274# define ex_continue ex_ni
275# define ex_debug ex_ni
276# define ex_debuggreedy ex_ni
277# define ex_delfunction ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000278# define ex_echo ex_ni
279# define ex_echohl ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000280# define ex_else ex_ni
Bram Moolenaare4ce8252019-08-04 15:30:16 +0200281# define ex_endfunction ex_ni
282# define ex_endif ex_ni
283# define ex_endtry ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000284# define ex_endwhile ex_ni
Bram Moolenaare4ce8252019-08-04 15:30:16 +0200285# define ex_eval ex_ni
286# define ex_execute ex_ni
287# define ex_finally ex_ni
288# define ex_finish ex_ni
289# define ex_function ex_ni
290# define ex_if ex_ni
291# define ex_let ex_ni
292# define ex_lockvar ex_ni
293# define ex_oldfiles ex_ni
294# define ex_options ex_ni
295# define ex_packadd ex_ni
296# define ex_packloadall ex_ni
297# define ex_return ex_ni
298# define ex_scriptnames ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000299# define ex_throw ex_ni
300# define ex_try ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000301# define ex_unlet ex_ni
Bram Moolenaar65c1b012005-01-31 19:02:28 +0000302# define ex_unlockvar ex_ni
Bram Moolenaare4ce8252019-08-04 15:30:16 +0200303# define ex_while ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000304#endif
Bram Moolenaar84538072019-07-28 14:15:42 +0200305#ifndef FEAT_SESSION
Bram Moolenaar071d4272004-06-13 20:20:40 +0000306# define ex_loadview ex_ni
307#endif
Bram Moolenaardefa0672019-07-21 19:25:37 +0200308#ifndef FEAT_VIMINFO
Bram Moolenaar071d4272004-06-13 20:20:40 +0000309# define ex_viminfo ex_ni
310#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100311static void ex_behave(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100312static void ex_filetype(exarg_T *eap);
313static void ex_setfiletype(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000314#ifndef FEAT_DIFF
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000315# define ex_diffoff ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000316# define ex_diffpatch ex_ni
317# define ex_diffgetput ex_ni
318# define ex_diffsplit ex_ni
319# define ex_diffthis ex_ni
320# define ex_diffupdate ex_ni
321#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100322static void ex_digraphs(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000323#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100324static void ex_nohlsearch(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000325#else
326# define ex_nohlsearch ex_ni
327# define ex_match ex_ni
328#endif
329#ifdef FEAT_CRYPT
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100330static void ex_X(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000331#else
332# define ex_X ex_ni
333#endif
334#ifdef FEAT_FOLDING
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100335static void ex_fold(exarg_T *eap);
336static void ex_foldopen(exarg_T *eap);
337static void ex_folddo(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000338#else
339# define ex_fold ex_ni
340# define ex_foldopen ex_ni
341# define ex_folddo ex_ni
342#endif
Bram Moolenaar13505972019-01-24 15:04:48 +0100343#if !(defined(HAVE_LOCALE_H) || defined(X_LOCALE))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000344# define ex_language ex_ni
345#endif
346#ifndef FEAT_SIGNS
347# define ex_sign ex_ni
348#endif
Bram Moolenaar009b2592004-10-24 19:18:58 +0000349#ifndef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200350# define ex_nbclose ex_ni
Bram Moolenaar009b2592004-10-24 19:18:58 +0000351# define ex_nbkey ex_ni
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200352# define ex_nbstart ex_ni
Bram Moolenaar009b2592004-10-24 19:18:58 +0000353#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000354
Bram Moolenaar071d4272004-06-13 20:20:40 +0000355#ifndef FEAT_JUMPLIST
356# define ex_jumps ex_ni
Bram Moolenaar2d358992016-06-12 21:20:54 +0200357# define ex_clearjumps ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000358# define ex_changes ex_ni
359#endif
360
Bram Moolenaar05159a02005-02-26 23:04:13 +0000361#ifndef FEAT_PROFILE
362# define ex_profile ex_ni
363#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200364#ifndef FEAT_TERMINAL
365# define ex_terminal ex_ni
366#endif
Bram Moolenaard4aa83a2019-05-09 18:59:31 +0200367#if !defined(FEAT_X11) || !defined(FEAT_XCLIPBOARD)
368# define ex_xrestore ex_ni
369#endif
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +0100370#if !defined(FEAT_PROP_POPUP)
Bram Moolenaar682725c2019-05-25 20:10:37 +0200371# define ex_popupclear ex_ni
372#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +0000373
Bram Moolenaar071d4272004-06-13 20:20:40 +0000374/*
375 * Declare cmdnames[].
376 */
377#define DO_DECLARE_EXCMD
378#include "ex_cmds.h"
Bram Moolenaar6de5e122017-04-20 21:55:44 +0200379#include "ex_cmdidxs.h"
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +0100380
Bram Moolenaar071d4272004-06-13 20:20:40 +0000381static char_u dollar_command[2] = {'$', 0};
382
383
384#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100385// Struct for storing a line inside a while/for loop
Bram Moolenaar071d4272004-06-13 20:20:40 +0000386typedef struct
387{
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100388 char_u *line; // command line
389 linenr_T lnum; // sourcing_lnum of the line
Bram Moolenaar071d4272004-06-13 20:20:40 +0000390} wcmd_T;
391
392/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000393 * Structure used to store info for line position in a while or for loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000394 * This is required, because do_one_cmd() may invoke ex_function(), which
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000395 * reads more lines that may come from the while/for loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000396 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000397struct loop_cookie
Bram Moolenaar071d4272004-06-13 20:20:40 +0000398{
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100399 garray_T *lines_gap; // growarray with line info
400 int current_line; // last read line from growarray
401 int repeating; // TRUE when looping a second time
402 // When "repeating" is FALSE use "getline" and "cookie" to get lines
Bram Moolenaare96a2492019-06-25 04:12:16 +0200403 char_u *(*getline)(int, void *, int, int);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000404 void *cookie;
405};
406
Bram Moolenaare96a2492019-06-25 04:12:16 +0200407static char_u *get_loop_line(int c, void *cookie, int indent, int do_concat);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100408static int store_loop_line(garray_T *gap, char_u *line);
409static void free_cmdlines(garray_T *gap);
Bram Moolenaared203462004-06-16 11:19:22 +0000410
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100411// Struct to save a few things while debugging. Used in do_cmdline() only.
Bram Moolenaared203462004-06-16 11:19:22 +0000412struct dbg_stuff
413{
414 int trylevel;
415 int force_abort;
416 except_T *caught_stack;
417 char_u *vv_exception;
418 char_u *vv_throwpoint;
419 int did_emsg;
420 int got_int;
421 int did_throw;
422 int need_rethrow;
423 int check_cstack;
424 except_T *current_exception;
425};
426
Bram Moolenaared203462004-06-16 11:19:22 +0000427 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100428save_dbg_stuff(struct dbg_stuff *dsp)
Bram Moolenaared203462004-06-16 11:19:22 +0000429{
430 dsp->trylevel = trylevel; trylevel = 0;
431 dsp->force_abort = force_abort; force_abort = FALSE;
432 dsp->caught_stack = caught_stack; caught_stack = NULL;
433 dsp->vv_exception = v_exception(NULL);
434 dsp->vv_throwpoint = v_throwpoint(NULL);
435
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100436 // Necessary for debugging an inactive ":catch", ":finally", ":endtry"
Bram Moolenaared203462004-06-16 11:19:22 +0000437 dsp->did_emsg = did_emsg; did_emsg = FALSE;
438 dsp->got_int = got_int; got_int = FALSE;
439 dsp->did_throw = did_throw; did_throw = FALSE;
440 dsp->need_rethrow = need_rethrow; need_rethrow = FALSE;
441 dsp->check_cstack = check_cstack; check_cstack = FALSE;
442 dsp->current_exception = current_exception; current_exception = NULL;
443}
444
445 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100446restore_dbg_stuff(struct dbg_stuff *dsp)
Bram Moolenaared203462004-06-16 11:19:22 +0000447{
448 suppress_errthrow = FALSE;
449 trylevel = dsp->trylevel;
450 force_abort = dsp->force_abort;
451 caught_stack = dsp->caught_stack;
452 (void)v_exception(dsp->vv_exception);
453 (void)v_throwpoint(dsp->vv_throwpoint);
454 did_emsg = dsp->did_emsg;
455 got_int = dsp->got_int;
456 did_throw = dsp->did_throw;
457 need_rethrow = dsp->need_rethrow;
458 check_cstack = dsp->check_cstack;
459 current_exception = dsp->current_exception;
460}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000461#endif
462
Bram Moolenaar071d4272004-06-13 20:20:40 +0000463/*
464 * do_exmode(): Repeatedly get commands for the "Ex" mode, until the ":vi"
465 * command is given.
466 */
467 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100468do_exmode(
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100469 int improved) // TRUE for "improved Ex" mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000470{
471 int save_msg_scroll;
472 int prev_msg_row;
473 linenr_T prev_line;
Bram Moolenaar79518e22017-02-17 16:31:35 +0100474 varnumber_T changedtick;
Bram Moolenaardf177f62005-02-22 08:39:57 +0000475
476 if (improved)
477 exmode_active = EXMODE_VIM;
478 else
479 exmode_active = EXMODE_NORMAL;
480 State = NORMAL;
481
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100482 // When using ":global /pat/ visual" and then "Q" we return to continue
483 // the :global command.
Bram Moolenaardf177f62005-02-22 08:39:57 +0000484 if (global_busy)
485 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000486
487 save_msg_scroll = msg_scroll;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100488 ++RedrawingDisabled; // don't redisplay the window
489 ++no_wait_return; // don't wait for return
Bram Moolenaar071d4272004-06-13 20:20:40 +0000490#ifdef FEAT_GUI
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100491 // Ignore scrollbar and mouse events in Ex mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000492 ++hold_gui_events;
493#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000494
Bram Moolenaar32526b32019-01-19 17:43:09 +0100495 msg(_("Entering Ex mode. Type \"visual\" to go to Normal mode."));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000496 while (exmode_active)
497 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100498 // Check for a ":normal" command and no more characters left.
Bram Moolenaar7c626922005-02-07 22:01:03 +0000499 if (ex_normal_busy > 0 && typebuf.tb_len == 0)
500 {
501 exmode_active = FALSE;
502 break;
503 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000504 msg_scroll = TRUE;
505 need_wait_return = FALSE;
506 ex_pressedreturn = FALSE;
507 ex_no_reprint = FALSE;
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100508 changedtick = CHANGEDTICK(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000509 prev_msg_row = msg_row;
510 prev_line = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000511 if (improved)
512 {
513 cmdline_row = msg_row;
514 do_cmdline(NULL, getexline, NULL, 0);
515 }
516 else
517 do_cmdline(NULL, getexmodeline, NULL, DOCMD_NOWAIT);
518 lines_left = Rows - 1;
519
Bram Moolenaardf177f62005-02-22 08:39:57 +0000520 if ((prev_line != curwin->w_cursor.lnum
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100521 || changedtick != CHANGEDTICK(curbuf)) && !ex_no_reprint)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000522 {
Bram Moolenaardf177f62005-02-22 08:39:57 +0000523 if (curbuf->b_ml.ml_flags & ML_EMPTY)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100524 emsg(_(e_emptybuf));
Bram Moolenaardf177f62005-02-22 08:39:57 +0000525 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000526 {
Bram Moolenaardf177f62005-02-22 08:39:57 +0000527 if (ex_pressedreturn)
528 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100529 // go up one line, to overwrite the ":<CR>" line, so the
530 // output doesn't contain empty lines.
Bram Moolenaardf177f62005-02-22 08:39:57 +0000531 msg_row = prev_msg_row;
532 if (prev_msg_row == Rows - 1)
533 msg_row--;
534 }
535 msg_col = 0;
536 print_line_no_prefix(curwin->w_cursor.lnum, FALSE, FALSE);
537 msg_clr_eos();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000538 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000539 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100540 else if (ex_pressedreturn && !ex_no_reprint) // must be at EOF
Bram Moolenaardf177f62005-02-22 08:39:57 +0000541 {
542 if (curbuf->b_ml.ml_flags & ML_EMPTY)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100543 emsg(_(e_emptybuf));
Bram Moolenaardf177f62005-02-22 08:39:57 +0000544 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100545 emsg(_("E501: At end-of-file"));
Bram Moolenaardf177f62005-02-22 08:39:57 +0000546 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000547 }
548
549#ifdef FEAT_GUI
550 --hold_gui_events;
551#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000552 --RedrawingDisabled;
553 --no_wait_return;
554 update_screen(CLEAR);
555 need_wait_return = FALSE;
556 msg_scroll = save_msg_scroll;
557}
558
559/*
Bram Moolenaar4facea32019-10-12 20:17:40 +0200560 * Print the executed command for when 'verbose' is set.
561 * When "lnum" is 0 only print the command.
562 */
563 static void
564msg_verbose_cmd(linenr_T lnum, char_u *cmd)
565{
566 ++no_wait_return;
567 verbose_enter_scroll();
568
569 if (lnum == 0)
570 smsg(_("Executing: %s"), cmd);
571 else
572 smsg(_("line %ld: %s"), (long)lnum, cmd);
573 if (msg_silent == 0)
574 msg_puts("\n"); // don't overwrite this
575
576 verbose_leave_scroll();
577 --no_wait_return;
578}
579
580/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000581 * Execute a simple command line. Used for translated commands like "*".
582 */
583 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100584do_cmdline_cmd(char_u *cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000585{
586 return do_cmdline(cmd, NULL, NULL,
587 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
588}
589
590/*
591 * do_cmdline(): execute one Ex command line
592 *
593 * 1. Execute "cmdline" when it is not NULL.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100594 * If "cmdline" is NULL, or more lines are needed, fgetline() is used.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000595 * 2. Split up in parts separated with '|'.
596 *
597 * This function can be called recursively!
598 *
599 * flags:
600 * DOCMD_VERBOSE - The command will be included in the error message.
601 * DOCMD_NOWAIT - Don't call wait_return() and friends.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100602 * DOCMD_REPEAT - Repeat execution until fgetline() returns NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000603 * DOCMD_KEYTYPED - Don't reset KeyTyped.
604 * DOCMD_EXCRESET - Reset the exception environment (used for debugging).
605 * DOCMD_KEEPLINE - Store first typed line (for repeating with ".").
606 *
607 * return FAIL if cmdline could not be executed, OK otherwise
608 */
609 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100610do_cmdline(
611 char_u *cmdline,
Bram Moolenaare96a2492019-06-25 04:12:16 +0200612 char_u *(*fgetline)(int, void *, int, int),
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100613 void *cookie, // argument for fgetline()
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100614 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000615{
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100616 char_u *next_cmdline; // next cmd to execute
617 char_u *cmdline_copy = NULL; // copy of cmd line
618 int used_getline = FALSE; // used "fgetline" to obtain command
619 static int recursive = 0; // recursive depth
Bram Moolenaar071d4272004-06-13 20:20:40 +0000620 int msg_didout_before_start = 0;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100621 int count = 0; // line number count
622 int did_inc = FALSE; // incremented RedrawingDisabled
Bram Moolenaar071d4272004-06-13 20:20:40 +0000623 int retval = OK;
624#ifdef FEAT_EVAL
Bram Moolenaarddef1292019-12-16 17:10:33 +0100625 cstack_T cstack; // conditional stack
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100626 garray_T lines_ga; // keep lines for ":while"/":for"
627 int current_line = 0; // active line in lines_ga
628 char_u *fname = NULL; // function or script name
629 linenr_T *breakpoint = NULL; // ptr to breakpoint field in cookie
630 int *dbg_tick = NULL; // ptr to dbg_tick field in cookie
631 struct dbg_stuff debug_saved; // saved things for debug mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000632 int initial_trylevel;
633 struct msglist **saved_msg_list = NULL;
634 struct msglist *private_msg_list;
635
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100636 // "fgetline" and "cookie" passed to do_one_cmd()
Bram Moolenaare96a2492019-06-25 04:12:16 +0200637 char_u *(*cmd_getline)(int, void *, int, int);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000638 void *cmd_cookie;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000639 struct loop_cookie cmd_loop_cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000640 void *real_cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +0000641 int getline_is_func;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000642#else
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100643# define cmd_getline fgetline
Bram Moolenaar071d4272004-06-13 20:20:40 +0000644# define cmd_cookie cookie
645#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100646 static int call_depth = 0; // recursiveness
Bram Moolenaare31ee862020-01-07 20:59:34 +0100647 ESTACK_CHECK_DECLARATION
Bram Moolenaar071d4272004-06-13 20:20:40 +0000648
649#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100650 // For every pair of do_cmdline()/do_one_cmd() calls, use an extra memory
651 // location for storing error messages to be converted to an exception.
652 // This ensures that the do_errthrow() call in do_one_cmd() does not
653 // combine the messages stored by an earlier invocation of do_one_cmd()
654 // with the command name of the later one. This would happen when
655 // BufWritePost autocommands are executed after a write error.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000656 saved_msg_list = msg_list;
657 msg_list = &private_msg_list;
658 private_msg_list = NULL;
659#endif
660
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100661 // It's possible to create an endless loop with ":execute", catch that
662 // here. The value of 200 allows nested function calls, ":source", etc.
663 // Allow 200 or 'maxfuncdepth', whatever is larger.
Bram Moolenaarb094ff42017-01-02 16:16:39 +0100664 if (call_depth >= 200
665#ifdef FEAT_EVAL
666 && call_depth >= p_mfd
667#endif
668 )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000669 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100670 emsg(_("E169: Command too recursive"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000671#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100672 // When converting to an exception, we do not include the command name
673 // since this is not an error of the specific command.
Bram Moolenaarddef1292019-12-16 17:10:33 +0100674 do_errthrow((cstack_T *)NULL, (char_u *)NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000675 msg_list = saved_msg_list;
676#endif
677 return FAIL;
678 }
679 ++call_depth;
680
681#ifdef FEAT_EVAL
682 cstack.cs_idx = -1;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000683 cstack.cs_looplevel = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000684 cstack.cs_trylevel = 0;
685 cstack.cs_emsg_silent_list = NULL;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000686 cstack.cs_lflags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000687 ga_init2(&lines_ga, (int)sizeof(wcmd_T), 10);
688
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100689 real_cookie = getline_cookie(fgetline, cookie);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000690
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100691 // Inside a function use a higher nesting level.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100692 getline_is_func = getline_equal(fgetline, cookie, get_func_line);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000693 if (getline_is_func && ex_nesting_level == func_level(real_cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000694 ++ex_nesting_level;
695
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100696 // Get the function or script name and the address where the next breakpoint
697 // line and the debug tick for a function or script are stored.
Bram Moolenaar05159a02005-02-26 23:04:13 +0000698 if (getline_is_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000699 {
700 fname = func_name(real_cookie);
701 breakpoint = func_breakpoint(real_cookie);
702 dbg_tick = func_dbg_tick(real_cookie);
703 }
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100704 else if (getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000705 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100706 fname = SOURCING_NAME;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000707 breakpoint = source_breakpoint(real_cookie);
708 dbg_tick = source_dbg_tick(real_cookie);
709 }
710
711 /*
712 * Initialize "force_abort" and "suppress_errthrow" at the top level.
713 */
714 if (!recursive)
715 {
716 force_abort = FALSE;
717 suppress_errthrow = FALSE;
718 }
719
720 /*
721 * If requested, store and reset the global values controlling the
Bram Moolenaar89d40322006-08-29 15:30:07 +0000722 * exception handling (used when debugging). Otherwise clear it to avoid
723 * a bogus compiler warning when the optimizer uses inline functions...
Bram Moolenaar071d4272004-06-13 20:20:40 +0000724 */
Bram Moolenaarb4872942006-05-13 10:32:52 +0000725 if (flags & DOCMD_EXCRESET)
Bram Moolenaared203462004-06-16 11:19:22 +0000726 save_dbg_stuff(&debug_saved);
Bram Moolenaar89d40322006-08-29 15:30:07 +0000727 else
Bram Moolenaar69b67f72015-09-25 16:59:47 +0200728 vim_memset(&debug_saved, 0, sizeof(debug_saved));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000729
730 initial_trylevel = trylevel;
731
732 /*
733 * "did_throw" will be set to TRUE when an exception is being thrown.
734 */
735 did_throw = FALSE;
736#endif
737 /*
738 * "did_emsg" will be set to TRUE when emsg() is used, in which case we
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000739 * cancel the whole command line, and any if/endif or loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000740 * If force_abort is set, we cancel everything.
741 */
742 did_emsg = FALSE;
743
744 /*
745 * KeyTyped is only set when calling vgetc(). Reset it here when not
746 * calling vgetc() (sourced command lines).
747 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100748 if (!(flags & DOCMD_KEYTYPED)
749 && !getline_equal(fgetline, cookie, getexline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000750 KeyTyped = FALSE;
751
752 /*
753 * Continue executing command lines:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000754 * - when inside an ":if", ":while" or ":for"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000755 * - for multiple commands on one line, separated with '|'
756 * - when repeating until there are no more lines (for ":source")
757 */
758 next_cmdline = cmdline;
759 do
760 {
Bram Moolenaar05159a02005-02-26 23:04:13 +0000761#ifdef FEAT_EVAL
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100762 getline_is_func = getline_equal(fgetline, cookie, get_func_line);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000763#endif
764
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100765 // stop skipping cmds for an error msg after all endif/while/for
Bram Moolenaar071d4272004-06-13 20:20:40 +0000766 if (next_cmdline == NULL
767#ifdef FEAT_EVAL
768 && !force_abort
769 && cstack.cs_idx < 0
Bram Moolenaar05159a02005-02-26 23:04:13 +0000770 && !(getline_is_func && func_has_abort(real_cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000771#endif
772 )
773 did_emsg = FALSE;
774
775 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000776 * 1. If repeating a line in a loop, get a line from lines_ga.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100777 * 2. If no line given: Get an allocated line with fgetline().
Bram Moolenaar071d4272004-06-13 20:20:40 +0000778 * 3. If a line is given: Make a copy, so we can mess with it.
779 */
780
781#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100782 // 1. If repeating, get a previous line from lines_ga.
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000783 if (cstack.cs_looplevel > 0 && current_line < lines_ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000784 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100785 // Each '|' separated command is stored separately in lines_ga, to
786 // be able to jump to it. Don't use next_cmdline now.
Bram Moolenaard23a8232018-02-10 18:45:26 +0100787 VIM_CLEAR(cmdline_copy);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000788
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100789 // Check if a function has returned or, unless it has an unclosed
790 // try conditional, aborted.
Bram Moolenaar05159a02005-02-26 23:04:13 +0000791 if (getline_is_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000792 {
Bram Moolenaar05159a02005-02-26 23:04:13 +0000793# ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000794 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +0000795 func_line_end(real_cookie);
796# endif
797 if (func_has_ended(real_cookie))
798 {
799 retval = FAIL;
800 break;
801 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000802 }
Bram Moolenaar05159a02005-02-26 23:04:13 +0000803#ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000804 else if (do_profiling == PROF_YES
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100805 && getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar05159a02005-02-26 23:04:13 +0000806 script_line_end();
807#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000808
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100809 // Check if a sourced file hit a ":finish" command.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100810 if (source_finished(fgetline, cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000811 {
812 retval = FAIL;
813 break;
814 }
815
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100816 // If breakpoints have been added/deleted need to check for it.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000817 if (breakpoint != NULL && dbg_tick != NULL
818 && *dbg_tick != debug_tick)
819 {
820 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100821 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100822 fname, SOURCING_LNUM);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000823 *dbg_tick = debug_tick;
824 }
825
826 next_cmdline = ((wcmd_T *)(lines_ga.ga_data))[current_line].line;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100827 SOURCING_LNUM = ((wcmd_T *)(lines_ga.ga_data))[current_line].lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000828
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100829 // Did we encounter a breakpoint?
Bram Moolenaar071d4272004-06-13 20:20:40 +0000830 if (breakpoint != NULL && *breakpoint != 0
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100831 && *breakpoint <= SOURCING_LNUM)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000832 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100833 dbg_breakpoint(fname, SOURCING_LNUM);
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100834 // Find next breakpoint.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000835 *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 }
Bram Moolenaar05159a02005-02-26 23:04:13 +0000840# ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000841 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +0000842 {
843 if (getline_is_func)
844 func_line_start(real_cookie);
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100845 else if (getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar05159a02005-02-26 23:04:13 +0000846 script_line_start();
847 }
848# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000849 }
850
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000851 if (cstack.cs_looplevel > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000852 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100853 // Inside a while/for loop we need to store the lines and use them
854 // again. Pass a different "fgetline" function to do_one_cmd()
855 // below, so that it stores lines in or reads them from
856 // "lines_ga". Makes it possible to define a function inside a
857 // while/for loop.
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000858 cmd_getline = get_loop_line;
859 cmd_cookie = (void *)&cmd_loop_cookie;
860 cmd_loop_cookie.lines_gap = &lines_ga;
861 cmd_loop_cookie.current_line = current_line;
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100862 cmd_loop_cookie.getline = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000863 cmd_loop_cookie.cookie = cookie;
864 cmd_loop_cookie.repeating = (current_line < lines_ga.ga_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000865 }
866 else
867 {
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100868 cmd_getline = fgetline;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000869 cmd_cookie = cookie;
870 }
871#endif
872
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100873 // 2. If no line given, get an allocated line with fgetline().
Bram Moolenaar071d4272004-06-13 20:20:40 +0000874 if (next_cmdline == NULL)
875 {
876 /*
877 * Need to set msg_didout for the first line after an ":if",
878 * otherwise the ":if" will be overwritten.
879 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100880 if (count == 1 && getline_equal(fgetline, cookie, getexline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000881 msg_didout = TRUE;
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100882 if (fgetline == NULL || (next_cmdline = fgetline(':', cookie,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000883#ifdef FEAT_EVAL
884 cstack.cs_idx < 0 ? 0 : (cstack.cs_idx + 1) * 2
885#else
886 0
887#endif
Bram Moolenaare96a2492019-06-25 04:12:16 +0200888 , TRUE)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000889 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100890 // Don't call wait_return for aborted command line. The NULL
891 // returned for the end of a sourced file or executed function
892 // doesn't do this.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000893 if (KeyTyped && !(flags & DOCMD_REPEAT))
894 need_wait_return = FALSE;
895 retval = FAIL;
896 break;
897 }
898 used_getline = TRUE;
899
900 /*
901 * Keep the first typed line. Clear it when more lines are typed.
902 */
903 if (flags & DOCMD_KEEPLINE)
904 {
905 vim_free(repeat_cmdline);
906 if (count == 0)
907 repeat_cmdline = vim_strsave(next_cmdline);
908 else
909 repeat_cmdline = NULL;
910 }
911 }
912
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100913 // 3. Make a copy of the command so we can mess with it.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000914 else if (cmdline_copy == NULL)
915 {
916 next_cmdline = vim_strsave(next_cmdline);
917 if (next_cmdline == NULL)
918 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100919 emsg(_(e_outofmem));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000920 retval = FAIL;
921 break;
922 }
923 }
924 cmdline_copy = next_cmdline;
925
926#ifdef FEAT_EVAL
927 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000928 * Save the current line when inside a ":while" or ":for", and when
929 * the command looks like a ":while" or ":for", because we may need it
930 * later. When there is a '|' and another command, it is stored
931 * separately, because we need to be able to jump back to it from an
932 * :endwhile/:endfor.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000933 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000934 if (current_line == lines_ga.ga_len
935 && (cstack.cs_looplevel || has_loop_cmd(next_cmdline)))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000936 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000937 if (store_loop_line(&lines_ga, next_cmdline) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000938 {
939 retval = FAIL;
940 break;
941 }
942 }
943 did_endif = FALSE;
944#endif
945
946 if (count++ == 0)
947 {
948 /*
949 * All output from the commands is put below each other, without
950 * waiting for a return. Don't do this when executing commands
951 * from a script or when being called recursive (e.g. for ":e
952 * +command file").
953 */
954 if (!(flags & DOCMD_NOWAIT) && !recursive)
955 {
956 msg_didout_before_start = msg_didout;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100957 msg_didany = FALSE; // no output yet
Bram Moolenaar071d4272004-06-13 20:20:40 +0000958 msg_start();
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100959 msg_scroll = TRUE; // put messages below each other
960 ++no_wait_return; // don't wait for return until finished
Bram Moolenaar071d4272004-06-13 20:20:40 +0000961 ++RedrawingDisabled;
962 did_inc = TRUE;
963 }
964 }
965
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100966 if (p_verbose >= 15 && SOURCING_NAME != NULL)
967 msg_verbose_cmd(SOURCING_LNUM, cmdline_copy);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000968
969 /*
970 * 2. Execute one '|' separated command.
971 * do_one_cmd() will return NULL if there is no trailing '|'.
972 * "cmdline_copy" can change, e.g. for '%' and '#' expansion.
973 */
974 ++recursive;
975 next_cmdline = do_one_cmd(&cmdline_copy, flags & DOCMD_VERBOSE,
976#ifdef FEAT_EVAL
977 &cstack,
978#endif
979 cmd_getline, cmd_cookie);
980 --recursive;
981
982#ifdef FEAT_EVAL
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000983 if (cmd_cookie == (void *)&cmd_loop_cookie)
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100984 // Use "current_line" from "cmd_loop_cookie", it may have been
985 // incremented when defining a function.
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000986 current_line = cmd_loop_cookie.current_line;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000987#endif
988
989 if (next_cmdline == NULL)
990 {
Bram Moolenaard23a8232018-02-10 18:45:26 +0100991 VIM_CLEAR(cmdline_copy);
Bram Moolenaard7663c22019-08-06 21:59:57 +0200992
Bram Moolenaar071d4272004-06-13 20:20:40 +0000993 /*
994 * If the command was typed, remember it for the ':' register.
995 * Do this AFTER executing the command to make :@: work.
996 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100997 if (getline_equal(fgetline, cookie, getexline)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000998 && new_last_cmdline != NULL)
999 {
1000 vim_free(last_cmdline);
1001 last_cmdline = new_last_cmdline;
1002 new_last_cmdline = NULL;
1003 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001004 }
1005 else
1006 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001007 // need to copy the command after the '|' to cmdline_copy, for the
1008 // next do_one_cmd()
Bram Moolenaara7241f52008-06-24 20:39:31 +00001009 STRMOVE(cmdline_copy, next_cmdline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001010 next_cmdline = cmdline_copy;
1011 }
1012
1013
1014#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001015 // reset did_emsg for a function that is not aborted by an error
Bram Moolenaar071d4272004-06-13 20:20:40 +00001016 if (did_emsg && !force_abort
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001017 && getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001018 && !func_has_abort(real_cookie))
1019 did_emsg = FALSE;
1020
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001021 if (cstack.cs_looplevel > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001022 {
1023 ++current_line;
1024
1025 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001026 * An ":endwhile", ":endfor" and ":continue" is handled here.
1027 * If we were executing commands, jump back to the ":while" or
1028 * ":for".
1029 * If we were not executing commands, decrement cs_looplevel.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001030 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001031 if (cstack.cs_lflags & (CSL_HAD_CONT | CSL_HAD_ENDLOOP))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001032 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001033 cstack.cs_lflags &= ~(CSL_HAD_CONT | CSL_HAD_ENDLOOP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001034
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001035 // Jump back to the matching ":while" or ":for". Be careful
1036 // not to use a cs_line[] from an entry that isn't a ":while"
1037 // or ":for": It would make "current_line" invalid and can
1038 // cause a crash.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001039 if (!did_emsg && !got_int && !did_throw
1040 && cstack.cs_idx >= 0
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001041 && (cstack.cs_flags[cstack.cs_idx]
1042 & (CSF_WHILE | CSF_FOR))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001043 && cstack.cs_line[cstack.cs_idx] >= 0
1044 && (cstack.cs_flags[cstack.cs_idx] & CSF_ACTIVE))
1045 {
1046 current_line = cstack.cs_line[cstack.cs_idx];
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001047 // remember we jumped there
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001048 cstack.cs_lflags |= CSL_HAD_LOOP;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001049 line_breakcheck(); // check if CTRL-C typed
Bram Moolenaar071d4272004-06-13 20:20:40 +00001050
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001051 // Check for the next breakpoint at or after the ":while"
1052 // or ":for".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001053 if (breakpoint != NULL)
1054 {
1055 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001056 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001057 fname,
1058 ((wcmd_T *)lines_ga.ga_data)[current_line].lnum-1);
1059 *dbg_tick = debug_tick;
1060 }
1061 }
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001062 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001063 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001064 // can only get here with ":endwhile" or ":endfor"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001065 if (cstack.cs_idx >= 0)
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001066 rewind_conditionals(&cstack, cstack.cs_idx - 1,
1067 CSF_WHILE | CSF_FOR, &cstack.cs_looplevel);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001068 }
1069 }
1070
1071 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001072 * For a ":while" or ":for" we need to remember the line number.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001073 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001074 else if (cstack.cs_lflags & CSL_HAD_LOOP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001075 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001076 cstack.cs_lflags &= ~CSL_HAD_LOOP;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001077 cstack.cs_line[cstack.cs_idx] = current_line - 1;
1078 }
1079 }
1080
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001081 // Check for the next breakpoint after a watchexpression
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01001082 if (breakpoint != NULL && has_watchexpr())
1083 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001084 *breakpoint = dbg_find_breakpoint(FALSE, fname, SOURCING_LNUM);
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01001085 *dbg_tick = debug_tick;
1086 }
1087
Bram Moolenaar071d4272004-06-13 20:20:40 +00001088 /*
1089 * When not inside any ":while" loop, clear remembered lines.
1090 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001091 if (cstack.cs_looplevel == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001092 {
1093 if (lines_ga.ga_len > 0)
1094 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001095 SOURCING_LNUM =
Bram Moolenaar071d4272004-06-13 20:20:40 +00001096 ((wcmd_T *)lines_ga.ga_data)[lines_ga.ga_len - 1].lnum;
1097 free_cmdlines(&lines_ga);
1098 }
1099 current_line = 0;
1100 }
1101
1102 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001103 * A ":finally" makes did_emsg, got_int, and did_throw pending for
1104 * being restored at the ":endtry". Reset them here and set the
1105 * ACTIVE and FINALLY flags, so that the finally clause gets executed.
1106 * This includes the case where a missing ":endif", ":endwhile" or
1107 * ":endfor" was detected by the ":finally" itself.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001108 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001109 if (cstack.cs_lflags & CSL_HAD_FINA)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001110 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001111 cstack.cs_lflags &= ~CSL_HAD_FINA;
1112 report_make_pending(cstack.cs_pending[cstack.cs_idx]
1113 & (CSTP_ERROR | CSTP_INTERRUPT | CSTP_THROW),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001114 did_throw ? (void *)current_exception : NULL);
1115 did_emsg = got_int = did_throw = FALSE;
1116 cstack.cs_flags[cstack.cs_idx] |= CSF_ACTIVE | CSF_FINALLY;
1117 }
1118
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001119 // Update global "trylevel" for recursive calls to do_cmdline() from
1120 // within this loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001121 trylevel = initial_trylevel + cstack.cs_trylevel;
1122
1123 /*
Bram Moolenaarc1762cc2007-05-10 16:56:30 +00001124 * If the outermost try conditional (across function calls and sourced
Bram Moolenaar071d4272004-06-13 20:20:40 +00001125 * files) is aborted because of an error, an interrupt, or an uncaught
1126 * exception, cancel everything. If it is left normally, reset
1127 * force_abort to get the non-EH compatible abortion behavior for
1128 * the rest of the script.
1129 */
1130 if (trylevel == 0 && !did_emsg && !got_int && !did_throw)
1131 force_abort = FALSE;
1132
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001133 // Convert an interrupt to an exception if appropriate.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001134 (void)do_intthrow(&cstack);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001135#endif // FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001136
1137 }
1138 /*
1139 * Continue executing command lines when:
1140 * - no CTRL-C typed, no aborting error, no exception thrown or try
1141 * conditionals need to be checked for executing finally clauses or
1142 * catching an interrupt exception
1143 * - didn't get an error message or lines are not typed
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001144 * - there is a command after '|', inside a :if, :while, :for or :try, or
Bram Moolenaar071d4272004-06-13 20:20:40 +00001145 * looping for ":source" command or function call.
1146 */
1147 while (!((got_int
1148#ifdef FEAT_EVAL
1149 || (did_emsg && force_abort) || did_throw
1150#endif
1151 )
1152#ifdef FEAT_EVAL
1153 && cstack.cs_trylevel == 0
1154#endif
1155 )
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001156 && !(did_emsg
1157#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001158 // Keep going when inside try/catch, so that the error can be
1159 // deal with, except when it is a syntax error, it may cause
1160 // the :endtry to be missed.
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001161 && (cstack.cs_trylevel == 0 || did_emsg_syntax)
1162#endif
1163 && used_getline
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001164 && (getline_equal(fgetline, cookie, getexmodeline)
1165 || getline_equal(fgetline, cookie, getexline)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001166 && (next_cmdline != NULL
1167#ifdef FEAT_EVAL
1168 || cstack.cs_idx >= 0
1169#endif
1170 || (flags & DOCMD_REPEAT)));
1171
1172 vim_free(cmdline_copy);
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001173 did_emsg_syntax = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001174#ifdef FEAT_EVAL
1175 free_cmdlines(&lines_ga);
1176 ga_clear(&lines_ga);
1177
1178 if (cstack.cs_idx >= 0)
1179 {
1180 /*
1181 * If a sourced file or executed function ran to its end, report the
1182 * unclosed conditional.
1183 */
1184 if (!got_int && !did_throw
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001185 && ((getline_equal(fgetline, cookie, getsourceline)
1186 && !source_finished(fgetline, cookie))
1187 || (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001188 && !func_has_ended(real_cookie))))
1189 {
1190 if (cstack.cs_flags[cstack.cs_idx] & CSF_TRY)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001191 emsg(_(e_endtry));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001192 else if (cstack.cs_flags[cstack.cs_idx] & CSF_WHILE)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001193 emsg(_(e_endwhile));
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001194 else if (cstack.cs_flags[cstack.cs_idx] & CSF_FOR)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001195 emsg(_(e_endfor));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001196 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001197 emsg(_(e_endif));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001198 }
1199
1200 /*
1201 * Reset "trylevel" in case of a ":finish" or ":return" or a missing
1202 * ":endtry" in a sourced file or executed function. If the try
1203 * conditional is in its finally clause, ignore anything pending.
1204 * If it is in a catch clause, finish the caught exception.
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001205 * Also cleanup any "cs_forinfo" structures.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001206 */
1207 do
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001208 {
1209 int idx = cleanup_conditionals(&cstack, 0, TRUE);
1210
Bram Moolenaar89e5d682005-01-17 22:06:23 +00001211 if (idx >= 0)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001212 --idx; // remove try block not in its finally clause
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001213 rewind_conditionals(&cstack, idx, CSF_WHILE | CSF_FOR,
1214 &cstack.cs_looplevel);
1215 }
1216 while (cstack.cs_idx >= 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001217 trylevel = initial_trylevel;
1218 }
1219
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001220 // If a missing ":endtry", ":endwhile", ":endfor", or ":endif" or a memory
1221 // lack was reported above and the error message is to be converted to an
1222 // exception, do this now after rewinding the cstack.
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001223 do_errthrow(&cstack, getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001224 ? (char_u *)"endfunction" : (char_u *)NULL);
1225
1226 if (trylevel == 0)
1227 {
1228 /*
1229 * When an exception is being thrown out of the outermost try
1230 * conditional, discard the uncaught exception, disable the conversion
1231 * of interrupts or errors to exceptions, and ensure that no more
1232 * commands are executed.
1233 */
1234 if (did_throw)
1235 {
1236 void *p = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001237 struct msglist *messages = NULL, *next;
1238
1239 /*
1240 * If the uncaught exception is a user exception, report it as an
1241 * error. If it is an error exception, display the saved error
1242 * message now. For an interrupt exception, do nothing; the
1243 * interrupt message is given elsewhere.
1244 */
1245 switch (current_exception->type)
1246 {
1247 case ET_USER:
Bram Moolenaar9c13b352005-05-19 20:53:52 +00001248 vim_snprintf((char *)IObuff, IOSIZE,
1249 _("E605: Exception not caught: %s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001250 current_exception->value);
1251 p = vim_strsave(IObuff);
1252 break;
1253 case ET_ERROR:
1254 messages = current_exception->messages;
1255 current_exception->messages = NULL;
1256 break;
1257 case ET_INTERRUPT:
1258 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001259 }
1260
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001261 estack_push(ETYPE_EXCEPT, current_exception->throw_name,
1262 current_exception->throw_lnum);
Bram Moolenaare31ee862020-01-07 20:59:34 +01001263 ESTACK_CHECK_SETUP
Bram Moolenaar071d4272004-06-13 20:20:40 +00001264 current_exception->throw_name = NULL;
1265
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001266 discard_current_exception(); // uses IObuff if 'verbose'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001267 suppress_errthrow = TRUE;
1268 force_abort = TRUE;
1269
1270 if (messages != NULL)
1271 {
1272 do
1273 {
1274 next = messages->next;
1275 emsg(messages->msg);
1276 vim_free(messages->msg);
1277 vim_free(messages);
1278 messages = next;
1279 }
1280 while (messages != NULL);
1281 }
1282 else if (p != NULL)
1283 {
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01001284 emsg(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001285 vim_free(p);
1286 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001287 vim_free(SOURCING_NAME);
Bram Moolenaare31ee862020-01-07 20:59:34 +01001288 ESTACK_CHECK_NOW
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001289 estack_pop();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001290 }
1291
1292 /*
1293 * On an interrupt or an aborting error not converted to an exception,
1294 * disable the conversion of errors to exceptions. (Interrupts are not
1295 * converted any more, here.) This enables also the interrupt message
1296 * when force_abort is set and did_emsg unset in case of an interrupt
1297 * from a finally clause after an error.
1298 */
1299 else if (got_int || (did_emsg && force_abort))
1300 suppress_errthrow = TRUE;
1301 }
1302
1303 /*
1304 * The current cstack will be freed when do_cmdline() returns. An uncaught
1305 * exception will have to be rethrown in the previous cstack. If a function
1306 * has just returned or a script file was just finished and the previous
1307 * cstack belongs to the same function or, respectively, script file, it
1308 * will have to be checked for finally clauses to be executed due to the
1309 * ":return" or ":finish". This is done in do_one_cmd().
1310 */
1311 if (did_throw)
1312 need_rethrow = TRUE;
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001313 if ((getline_equal(fgetline, cookie, getsourceline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001314 && ex_nesting_level > source_level(real_cookie))
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001315 || (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001316 && ex_nesting_level > func_level(real_cookie) + 1))
1317 {
1318 if (!did_throw)
1319 check_cstack = TRUE;
1320 }
1321 else
1322 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001323 // When leaving a function, reduce nesting level.
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001324 if (getline_equal(fgetline, cookie, get_func_line))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001325 --ex_nesting_level;
1326 /*
1327 * Go to debug mode when returning from a function in which we are
1328 * single-stepping.
1329 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001330 if ((getline_equal(fgetline, cookie, getsourceline)
1331 || getline_equal(fgetline, cookie, get_func_line))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001332 && ex_nesting_level + 1 <= debug_break_level)
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001333 do_debug(getline_equal(fgetline, cookie, getsourceline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001334 ? (char_u *)_("End of sourced file")
1335 : (char_u *)_("End of function"));
1336 }
1337
1338 /*
1339 * Restore the exception environment (done after returning from the
1340 * debugger).
1341 */
1342 if (flags & DOCMD_EXCRESET)
Bram Moolenaared203462004-06-16 11:19:22 +00001343 restore_dbg_stuff(&debug_saved);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001344
1345 msg_list = saved_msg_list;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001346#endif // FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001347
1348 /*
1349 * If there was too much output to fit on the command line, ask the user to
1350 * hit return before redrawing the screen. With the ":global" command we do
1351 * this only once after the command is finished.
1352 */
1353 if (did_inc)
1354 {
1355 --RedrawingDisabled;
1356 --no_wait_return;
1357 msg_scroll = FALSE;
1358
1359 /*
1360 * When just finished an ":if"-":else" which was typed, no need to
1361 * wait for hit-return. Also for an error situation.
1362 */
1363 if (retval == FAIL
1364#ifdef FEAT_EVAL
1365 || (did_endif && KeyTyped && !did_emsg)
1366#endif
1367 )
1368 {
1369 need_wait_return = FALSE;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001370 msg_didany = FALSE; // don't wait when restarting edit
Bram Moolenaar071d4272004-06-13 20:20:40 +00001371 }
1372 else if (need_wait_return)
1373 {
1374 /*
1375 * The msg_start() above clears msg_didout. The wait_return we do
1376 * here should not overwrite the command that may be shown before
1377 * doing that.
1378 */
1379 msg_didout |= msg_didout_before_start;
1380 wait_return(FALSE);
1381 }
1382 }
1383
Bram Moolenaar2a942252012-11-28 23:03:07 +01001384#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001385 did_endif = FALSE; // in case do_cmdline used recursively
Bram Moolenaar2a942252012-11-28 23:03:07 +01001386#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001387 /*
1388 * Reset if_level, in case a sourced script file contains more ":if" than
1389 * ":endif" (could be ":if x | foo | endif").
1390 */
1391 if_level = 0;
Bram Moolenaar2e18a122012-11-28 19:10:54 +01001392#endif
Bram Moolenaard4ad0d42012-11-28 17:34:48 +01001393
Bram Moolenaar071d4272004-06-13 20:20:40 +00001394 --call_depth;
1395 return retval;
1396}
1397
1398#ifdef FEAT_EVAL
1399/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001400 * Obtain a line when inside a ":while" or ":for" loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001401 */
1402 static char_u *
Bram Moolenaare96a2492019-06-25 04:12:16 +02001403get_loop_line(int c, void *cookie, int indent, int do_concat)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001404{
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001405 struct loop_cookie *cp = (struct loop_cookie *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001406 wcmd_T *wp;
1407 char_u *line;
1408
1409 if (cp->current_line + 1 >= cp->lines_gap->ga_len)
1410 {
1411 if (cp->repeating)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001412 return NULL; // trying to read past ":endwhile"/":endfor"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001413
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001414 // First time inside the ":while"/":for": get line normally.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001415 if (cp->getline == NULL)
Bram Moolenaare96a2492019-06-25 04:12:16 +02001416 line = getcmdline(c, 0L, indent, do_concat);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001417 else
Bram Moolenaare96a2492019-06-25 04:12:16 +02001418 line = cp->getline(c, cp->cookie, indent, do_concat);
Bram Moolenaard68071d2006-05-02 22:08:30 +00001419 if (line != NULL && store_loop_line(cp->lines_gap, line) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001420 ++cp->current_line;
1421
1422 return line;
1423 }
1424
1425 KeyTyped = FALSE;
1426 ++cp->current_line;
1427 wp = (wcmd_T *)(cp->lines_gap->ga_data) + cp->current_line;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001428 SOURCING_LNUM = wp->lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001429 return vim_strsave(wp->line);
1430}
1431
1432/*
1433 * Store a line in "gap" so that a ":while" loop can execute it again.
1434 */
1435 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001436store_loop_line(garray_T *gap, char_u *line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001437{
1438 if (ga_grow(gap, 1) == FAIL)
1439 return FAIL;
1440 ((wcmd_T *)(gap->ga_data))[gap->ga_len].line = vim_strsave(line);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001441 ((wcmd_T *)(gap->ga_data))[gap->ga_len].lnum = SOURCING_LNUM;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001442 ++gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001443 return OK;
1444}
1445
1446/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001447 * Free the lines stored for a ":while" or ":for" loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001448 */
1449 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001450free_cmdlines(garray_T *gap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001451{
1452 while (gap->ga_len > 0)
1453 {
1454 vim_free(((wcmd_T *)(gap->ga_data))[gap->ga_len - 1].line);
1455 --gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001456 }
1457}
1458#endif
1459
1460/*
Bram Moolenaar89d40322006-08-29 15:30:07 +00001461 * If "fgetline" is get_loop_line(), return TRUE if the getline it uses equals
1462 * "func". * Otherwise return TRUE when "fgetline" equals "func".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001463 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001464 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001465getline_equal(
Bram Moolenaare96a2492019-06-25 04:12:16 +02001466 char_u *(*fgetline)(int, void *, int, int),
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001467 void *cookie UNUSED, // argument for fgetline()
Bram Moolenaare96a2492019-06-25 04:12:16 +02001468 char_u *(*func)(int, void *, int, int))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001469{
1470#ifdef FEAT_EVAL
Bram Moolenaare96a2492019-06-25 04:12:16 +02001471 char_u *(*gp)(int, void *, int, int);
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001472 struct loop_cookie *cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001473
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001474 // When "fgetline" is "get_loop_line()" use the "cookie" to find the
1475 // function that's originally used to obtain the lines. This may be
1476 // nested several levels.
Bram Moolenaar89d40322006-08-29 15:30:07 +00001477 gp = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001478 cp = (struct loop_cookie *)cookie;
1479 while (gp == get_loop_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001480 {
1481 gp = cp->getline;
1482 cp = cp->cookie;
1483 }
1484 return gp == func;
1485#else
Bram Moolenaar89d40322006-08-29 15:30:07 +00001486 return fgetline == func;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001487#endif
1488}
1489
Bram Moolenaar071d4272004-06-13 20:20:40 +00001490/*
Bram Moolenaar89d40322006-08-29 15:30:07 +00001491 * If "fgetline" is get_loop_line(), return the cookie used by the original
Bram Moolenaar071d4272004-06-13 20:20:40 +00001492 * getline function. Otherwise return "cookie".
1493 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001494 void *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001495getline_cookie(
Bram Moolenaare96a2492019-06-25 04:12:16 +02001496 char_u *(*fgetline)(int, void *, int, int) UNUSED,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001497 void *cookie) // argument for fgetline()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001498{
Bram Moolenaar13505972019-01-24 15:04:48 +01001499#ifdef FEAT_EVAL
Bram Moolenaare96a2492019-06-25 04:12:16 +02001500 char_u *(*gp)(int, void *, int, int);
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001501 struct loop_cookie *cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001502
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001503 // When "fgetline" is "get_loop_line()" use the "cookie" to find the
1504 // cookie that's originally used to obtain the lines. This may be nested
1505 // several levels.
Bram Moolenaar89d40322006-08-29 15:30:07 +00001506 gp = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001507 cp = (struct loop_cookie *)cookie;
1508 while (gp == get_loop_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001509 {
1510 gp = cp->getline;
1511 cp = cp->cookie;
1512 }
1513 return cp;
Bram Moolenaar13505972019-01-24 15:04:48 +01001514#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001515 return cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001516#endif
Bram Moolenaar13505972019-01-24 15:04:48 +01001517}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001518
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001519
1520/*
1521 * Helper function to apply an offset for buffer commands, i.e. ":bdelete",
1522 * ":bwipeout", etc.
1523 * Returns the buffer number.
1524 */
1525 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001526compute_buffer_local_count(int addr_type, int lnum, int offset)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001527{
1528 buf_T *buf;
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001529 buf_T *nextbuf;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001530 int count = offset;
1531
1532 buf = firstbuf;
1533 while (buf->b_next != NULL && buf->b_fnum < lnum)
1534 buf = buf->b_next;
1535 while (count != 0)
1536 {
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001537 count += (offset < 0) ? 1 : -1;
1538 nextbuf = (offset < 0) ? buf->b_prev : buf->b_next;
1539 if (nextbuf == NULL)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001540 break;
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001541 buf = nextbuf;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001542 if (addr_type == ADDR_LOADED_BUFFERS)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001543 // skip over unloaded buffers
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001544 while (buf->b_ml.ml_mfp == NULL)
1545 {
1546 nextbuf = (offset < 0) ? buf->b_prev : buf->b_next;
1547 if (nextbuf == NULL)
1548 break;
1549 buf = nextbuf;
1550 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001551 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001552 // we might have gone too far, last buffer is not loadedd
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001553 if (addr_type == ADDR_LOADED_BUFFERS)
1554 while (buf->b_ml.ml_mfp == NULL)
1555 {
1556 nextbuf = (offset >= 0) ? buf->b_prev : buf->b_next;
1557 if (nextbuf == NULL)
1558 break;
1559 buf = nextbuf;
1560 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001561 return buf->b_fnum;
1562}
1563
Bram Moolenaarb7316892019-05-01 18:08:42 +02001564/*
1565 * Return the window number of "win".
1566 * When "win" is NULL return the number of windows.
1567 */
Bram Moolenaarf240e182014-11-27 18:33:02 +01001568 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001569current_win_nr(win_T *win)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001570{
1571 win_T *wp;
1572 int nr = 0;
1573
Bram Moolenaar29323592016-07-24 22:04:11 +02001574 FOR_ALL_WINDOWS(wp)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001575 {
1576 ++nr;
1577 if (wp == win)
1578 break;
1579 }
1580 return nr;
1581}
1582
1583 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001584current_tab_nr(tabpage_T *tab)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001585{
1586 tabpage_T *tp;
1587 int nr = 0;
1588
Bram Moolenaar29323592016-07-24 22:04:11 +02001589 FOR_ALL_TABPAGES(tp)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001590 {
1591 ++nr;
1592 if (tp == tab)
1593 break;
1594 }
1595 return nr;
1596}
1597
1598# define CURRENT_WIN_NR current_win_nr(curwin)
1599# define LAST_WIN_NR current_win_nr(NULL)
1600# define CURRENT_TAB_NR current_tab_nr(curtab)
1601# define LAST_TAB_NR current_tab_nr(NULL)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001602
Bram Moolenaar071d4272004-06-13 20:20:40 +00001603/*
1604 * Execute one Ex command.
1605 *
1606 * If 'sourcing' is TRUE, the command will be included in the error message.
1607 *
1608 * 1. skip comment lines and leading space
1609 * 2. handle command modifiers
Bram Moolenaar1c40a662014-11-27 16:38:11 +01001610 * 3. find the command
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001611 * 4. parse range
Bram Moolenaar1c40a662014-11-27 16:38:11 +01001612 * 5. Parse the command.
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001613 * 6. parse arguments
1614 * 7. switch on command name
Bram Moolenaar071d4272004-06-13 20:20:40 +00001615 *
Bram Moolenaar89d40322006-08-29 15:30:07 +00001616 * Note: "fgetline" can be NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001617 *
1618 * This function may be called recursively!
1619 */
1620#if (_MSC_VER == 1200)
1621/*
Bram Moolenaared203462004-06-16 11:19:22 +00001622 * Avoid optimisation bug in VC++ version 6.0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001623 */
Bram Moolenaar281bdce2005-01-25 21:53:18 +00001624 #pragma optimize( "g", off )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001625#endif
1626 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001627do_one_cmd(
Bram Moolenaarddef1292019-12-16 17:10:33 +01001628 char_u **cmdlinep,
1629 int sourcing,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001630#ifdef FEAT_EVAL
Bram Moolenaarddef1292019-12-16 17:10:33 +01001631 cstack_T *cstack,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001632#endif
Bram Moolenaarddef1292019-12-16 17:10:33 +01001633 char_u *(*fgetline)(int, void *, int, int),
1634 void *cookie) // argument for fgetline()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001635{
Bram Moolenaarddef1292019-12-16 17:10:33 +01001636 char_u *p;
1637 linenr_T lnum;
1638 long n;
1639 char *errormsg = NULL; // error message
1640 char_u *after_modifier = NULL;
1641 exarg_T ea; // Ex command arguments
1642 int save_msg_scroll = msg_scroll;
1643 cmdmod_T save_cmdmod;
1644 int save_reg_executing = reg_executing;
1645 int ni; // set when Not Implemented
1646 char_u *cmd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001647
1648 vim_memset(&ea, 0, sizeof(ea));
1649 ea.line1 = 1;
1650 ea.line2 = 1;
1651#ifdef FEAT_EVAL
1652 ++ex_nesting_level;
1653#endif
1654
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001655 // When the last file has not been edited :q has to be typed twice.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001656 if (quitmore
1657#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001658 // avoid that a function call in 'statusline' does this
Bram Moolenaar89d40322006-08-29 15:30:07 +00001659 && !getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001660#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001661 // avoid that an autocommand, e.g. QuitPre, does this
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001662 && !getline_equal(fgetline, cookie, getnextac))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001663 --quitmore;
1664
1665 /*
1666 * Reset browse, confirm, etc.. They are restored when returning, for
1667 * recursive calls.
1668 */
1669 save_cmdmod = cmdmod;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001670
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001671 // "#!anything" is handled like a comment.
Bram Moolenaarcbb37ad2006-08-16 15:04:21 +00001672 if ((*cmdlinep)[0] == '#' && (*cmdlinep)[1] == '!')
1673 goto doend;
1674
Bram Moolenaar4facea32019-10-12 20:17:40 +02001675 if (p_verbose >= 16)
1676 msg_verbose_cmd(0, *cmdlinep);
1677
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02001678/*
1679 * 1. Skip comment lines and leading white space and colons.
1680 * 2. Handle command modifiers.
1681 */
1682 // The "ea" structure holds the arguments that can be used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001683 ea.cmd = *cmdlinep;
Bram Moolenaareffed932018-08-14 13:38:17 +02001684 ea.cmdlinep = cmdlinep;
1685 ea.getline = fgetline;
1686 ea.cookie = cookie;
1687#ifdef FEAT_EVAL
1688 ea.cstack = cstack;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001689#endif
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02001690 if (parse_command_modifiers(&ea, &errormsg, FALSE) == FAIL)
Bram Moolenaareffed932018-08-14 13:38:17 +02001691 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001692
Bram Moolenaar7b668e82016-08-23 23:51:21 +02001693 after_modifier = ea.cmd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001694
1695#ifdef FEAT_EVAL
1696 ea.skip = did_emsg || got_int || did_throw || (cstack->cs_idx >= 0
1697 && !(cstack->cs_flags[cstack->cs_idx] & CSF_ACTIVE));
1698#else
1699 ea.skip = (if_level > 0);
1700#endif
1701
Bram Moolenaar071d4272004-06-13 20:20:40 +00001702/*
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001703 * 3. Skip over the range to find the command. Let "p" point to after it.
1704 *
1705 * We need the command to know what kind of range it uses.
1706 */
1707 cmd = ea.cmd;
1708 ea.cmd = skip_range(ea.cmd, NULL);
1709 if (*ea.cmd == '*' && vim_strchr(p_cpo, CPO_STAR) == NULL)
1710 ea.cmd = skipwhite(ea.cmd + 1);
1711 p = find_command(&ea, NULL);
1712
Bram Moolenaar7feb35e2018-08-21 17:49:54 +02001713#ifdef FEAT_EVAL
1714# ifdef FEAT_PROFILE
1715 // Count this line for profiling if skip is TRUE.
1716 if (do_profiling == PROF_YES
1717 && (!ea.skip || cstack->cs_idx == 0 || (cstack->cs_idx > 0
1718 && (cstack->cs_flags[cstack->cs_idx - 1] & CSF_ACTIVE))))
1719 {
1720 int skip = did_emsg || got_int || did_throw;
1721
1722 if (ea.cmdidx == CMD_catch)
1723 skip = !skip && !(cstack->cs_idx >= 0
1724 && (cstack->cs_flags[cstack->cs_idx] & CSF_THROWN)
1725 && !(cstack->cs_flags[cstack->cs_idx] & CSF_CAUGHT));
1726 else if (ea.cmdidx == CMD_else || ea.cmdidx == CMD_elseif)
1727 skip = skip || !(cstack->cs_idx >= 0
1728 && !(cstack->cs_flags[cstack->cs_idx]
1729 & (CSF_ACTIVE | CSF_TRUE)));
1730 else if (ea.cmdidx == CMD_finally)
1731 skip = FALSE;
1732 else if (ea.cmdidx != CMD_endif
1733 && ea.cmdidx != CMD_endfor
1734 && ea.cmdidx != CMD_endtry
1735 && ea.cmdidx != CMD_endwhile)
1736 skip = ea.skip;
1737
1738 if (!skip)
1739 {
1740 if (getline_equal(fgetline, cookie, get_func_line))
1741 func_line_exec(getline_cookie(fgetline, cookie));
1742 else if (getline_equal(fgetline, cookie, getsourceline))
1743 script_line_exec();
1744 }
1745 }
1746# endif
1747
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001748 // May go to debug mode. If this happens and the ">quit" debug command is
1749 // used, throw an interrupt exception and skip the next command.
Bram Moolenaar7feb35e2018-08-21 17:49:54 +02001750 dbg_check_breakpoint(&ea);
1751 if (!ea.skip && got_int)
1752 {
1753 ea.skip = TRUE;
1754 (void)do_intthrow(cstack);
1755 }
1756#endif
1757
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001758/*
1759 * 4. parse a range specifier of the form: addr [,addr] [;addr] ..
Bram Moolenaar071d4272004-06-13 20:20:40 +00001760 *
1761 * where 'addr' is:
1762 *
1763 * % (entire file)
1764 * $ [+-NUM]
1765 * 'x [+-NUM] (where x denotes a currently defined mark)
1766 * . [+-NUM]
1767 * [+-NUM]..
1768 * NUM
1769 *
1770 * The ea.cmd pointer is updated to point to the first character following the
1771 * range spec. If an initial address is found, but no second, the upper bound
1772 * is equal to the lower.
1773 */
1774
Bram Moolenaar25190db2019-05-04 15:05:28 +02001775 // ea.addr_type for user commands is set by find_ucmd
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01001776 if (!IS_USER_CMDIDX(ea.cmdidx))
1777 {
1778 if (ea.cmdidx != CMD_SIZE)
1779 ea.addr_type = cmdnames[(int)ea.cmdidx].cmd_addr_type;
1780 else
1781 ea.addr_type = ADDR_LINES;
1782
Bram Moolenaar25190db2019-05-04 15:05:28 +02001783 // :wincmd range depends on the argument.
Bram Moolenaar164f3262015-01-14 21:22:01 +01001784 if (ea.cmdidx == CMD_wincmd && p != NULL)
1785 get_wincmd_addr_type(skipwhite(p), &ea);
Bram Moolenaar25190db2019-05-04 15:05:28 +02001786#ifdef FEAT_QUICKFIX
1787 // :.cc in quickfix window uses line number
1788 if ((ea.cmdidx == CMD_cc || ea.cmdidx == CMD_ll) && bt_quickfix(curbuf))
1789 ea.addr_type = ADDR_OTHER;
1790#endif
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01001791 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001792
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01001793 ea.cmd = cmd;
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02001794 if (parse_cmd_address(&ea, &errormsg, FALSE) == FAIL)
Bram Moolenaaree8415b2018-08-10 23:13:12 +02001795 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001796
Bram Moolenaar071d4272004-06-13 20:20:40 +00001797/*
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001798 * 5. Parse the command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001799 */
1800
1801 /*
1802 * Skip ':' and any white space
1803 */
1804 ea.cmd = skipwhite(ea.cmd);
1805 while (*ea.cmd == ':')
1806 ea.cmd = skipwhite(ea.cmd + 1);
1807
1808 /*
1809 * If we got a line, but no command, then go to the line.
1810 * If we find a '|' or '\n' we set ea.nextcmd.
1811 */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001812 if (*ea.cmd == NUL || *ea.cmd == '"'
1813 || (ea.nextcmd = check_nextcmd(ea.cmd)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001814 {
1815 /*
1816 * strange vi behaviour:
1817 * ":3" jumps to line 3
1818 * ":3|..." prints line 3
1819 * ":|" prints current line
1820 */
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001821 if (ea.skip) // skip this if inside :if
Bram Moolenaar071d4272004-06-13 20:20:40 +00001822 goto doend;
Bram Moolenaardf177f62005-02-22 08:39:57 +00001823 if (*ea.cmd == '|' || (exmode_active && ea.line1 != ea.line2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001824 {
1825 ea.cmdidx = CMD_print;
Bram Moolenaar8071cb22019-07-12 17:58:01 +02001826 ea.argt = EX_RANGE+EX_COUNT+EX_TRLBAR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001827 if ((errormsg = invalid_range(&ea)) == NULL)
1828 {
1829 correct_range(&ea);
1830 ex_print(&ea);
1831 }
1832 }
1833 else if (ea.addr_count != 0)
1834 {
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00001835 if (ea.line2 > curbuf->b_ml.ml_line_count)
1836 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001837 // With '-' in 'cpoptions' a line number past the file is an
1838 // error, otherwise put it at the end of the file.
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00001839 if (vim_strchr(p_cpo, CPO_MINUS) != NULL)
1840 ea.line2 = -1;
1841 else
1842 ea.line2 = curbuf->b_ml.ml_line_count;
1843 }
1844
1845 if (ea.line2 < 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001846 errormsg = _(e_invrange);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001847 else
1848 {
1849 if (ea.line2 == 0)
1850 curwin->w_cursor.lnum = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001851 else
1852 curwin->w_cursor.lnum = ea.line2;
1853 beginline(BL_SOL | BL_FIX);
1854 }
1855 }
1856 goto doend;
1857 }
1858
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001859 // If this looks like an undefined user command and there are CmdUndefined
1860 // autocommands defined, trigger the matching autocommands.
Bram Moolenaard5005162014-08-22 23:05:54 +02001861 if (p != NULL && ea.cmdidx == CMD_SIZE && !ea.skip
1862 && ASCII_ISUPPER(*ea.cmd)
1863 && has_cmdundefined())
1864 {
Bram Moolenaard5005162014-08-22 23:05:54 +02001865 int ret;
1866
Bram Moolenaar5a31b462014-08-23 14:16:20 +02001867 p = ea.cmd;
Bram Moolenaard5005162014-08-22 23:05:54 +02001868 while (ASCII_ISALNUM(*p))
1869 ++p;
Bram Moolenaar120f4a82014-09-09 12:22:06 +02001870 p = vim_strnsave(ea.cmd, (int)(p - ea.cmd));
Bram Moolenaard5005162014-08-22 23:05:54 +02001871 ret = apply_autocmds(EVENT_CMDUNDEFINED, p, p, TRUE, NULL);
1872 vim_free(p);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001873 // If the autocommands did something and didn't cause an error, try
1874 // finding the command again.
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001875 p = (ret
1876#ifdef FEAT_EVAL
1877 && !aborting()
Bram Moolenaard5005162014-08-22 23:05:54 +02001878#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001879 ) ? find_command(&ea, NULL) : ea.cmd;
1880 }
Bram Moolenaard5005162014-08-22 23:05:54 +02001881
Bram Moolenaar071d4272004-06-13 20:20:40 +00001882 if (p == NULL)
1883 {
1884 if (!ea.skip)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001885 errormsg = _("E464: Ambiguous use of user-defined command");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001886 goto doend;
1887 }
Bram Moolenaarac9fb182019-04-27 13:04:13 +02001888 // Check for wrong commands.
Bram Moolenaar6f9a4762017-06-22 20:39:17 +02001889 if (*p == '!' && ea.cmd[1] == 0151 && ea.cmd[0] == 78
1890 && !IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001891 {
1892 errormsg = uc_fun_cmd();
1893 goto doend;
1894 }
Bram Moolenaarac9fb182019-04-27 13:04:13 +02001895
Bram Moolenaar071d4272004-06-13 20:20:40 +00001896 if (ea.cmdidx == CMD_SIZE)
1897 {
1898 if (!ea.skip)
1899 {
1900 STRCPY(IObuff, _("E492: Not an editor command"));
1901 if (!sourcing)
Bram Moolenaar7b668e82016-08-23 23:51:21 +02001902 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001903 // If the modifier was parsed OK the error must be in the
1904 // following command
Bram Moolenaar7b668e82016-08-23 23:51:21 +02001905 if (after_modifier != NULL)
1906 append_command(after_modifier);
1907 else
1908 append_command(*cmdlinep);
1909 }
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001910 errormsg = (char *)IObuff;
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001911 did_emsg_syntax = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001912 }
1913 goto doend;
1914 }
1915
Bram Moolenaar958636c2014-10-21 20:01:58 +02001916 ni = (!IS_USER_CMDIDX(ea.cmdidx)
1917 && (cmdnames[ea.cmdidx].cmd_func == ex_ni
Bram Moolenaar7bb75552007-07-16 18:39:49 +00001918#ifdef HAVE_EX_SCRIPT_NI
1919 || cmdnames[ea.cmdidx].cmd_func == ex_script_ni
1920#endif
1921 ));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001922
1923#ifndef FEAT_EVAL
1924 /*
1925 * When the expression evaluation is disabled, recognize the ":if" and
1926 * ":endif" commands and ignore everything in between it.
1927 */
1928 if (ea.cmdidx == CMD_if)
1929 ++if_level;
1930 if (if_level)
1931 {
1932 if (ea.cmdidx == CMD_endif)
1933 --if_level;
1934 goto doend;
1935 }
1936
1937#endif
1938
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001939 // forced commands
Bram Moolenaar196b3b02008-06-20 16:51:41 +00001940 if (*p == '!' && ea.cmdidx != CMD_substitute
1941 && ea.cmdidx != CMD_smagic && ea.cmdidx != CMD_snomagic)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001942 {
1943 ++p;
1944 ea.forceit = TRUE;
1945 }
1946 else
1947 ea.forceit = FALSE;
1948
1949/*
Bram Moolenaarb7316892019-05-01 18:08:42 +02001950 * 6. Parse arguments. Then check for errors.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001951 */
Bram Moolenaar958636c2014-10-21 20:01:58 +02001952 if (!IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001953 ea.argt = (long)cmdnames[(int)ea.cmdidx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001954
1955 if (!ea.skip)
1956 {
1957#ifdef HAVE_SANDBOX
Bram Moolenaar8071cb22019-07-12 17:58:01 +02001958 if (sandbox != 0 && !(ea.argt & EX_SBOXOK))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001959 {
Bram Moolenaar8c62a082019-02-08 14:34:10 +01001960 // Command not allowed in sandbox.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001961 errormsg = _(e_sandbox);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001962 goto doend;
1963 }
1964#endif
Bram Moolenaar8071cb22019-07-12 17:58:01 +02001965 if (restricted != 0 && (ea.argt & EX_RESTRICT))
Bram Moolenaar8c62a082019-02-08 14:34:10 +01001966 {
1967 errormsg = _("E981: Command not allowed in rvim");
1968 goto doend;
1969 }
Bram Moolenaar8071cb22019-07-12 17:58:01 +02001970 if (!curbuf->b_p_ma && (ea.argt & EX_MODIFY))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001971 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001972 // Command not allowed in non-'modifiable' buffer
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001973 errormsg = _(e_modifiable);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001974 goto doend;
1975 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00001976
Bram Moolenaar8071cb22019-07-12 17:58:01 +02001977 if (text_locked() && !(ea.argt & EX_CMDWIN)
Bram Moolenaar958636c2014-10-21 20:01:58 +02001978 && !IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001979 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001980 // Command not allowed when editing the command line.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001981 errormsg = _(get_text_locked_msg());
Bram Moolenaar071d4272004-06-13 20:20:40 +00001982 goto doend;
1983 }
Bram Moolenaar379fb762018-08-30 15:58:28 +02001984
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001985 // Disallow editing another buffer when "curbuf_lock" is set.
1986 // Do allow ":checktime" (it is postponed).
1987 // Do allow ":edit" (check for an argument later).
1988 // Do allow ":file" with no arguments (check for an argument later).
Bram Moolenaar8071cb22019-07-12 17:58:01 +02001989 if (!(ea.argt & EX_CMDWIN)
Bram Moolenaar5555acc2006-04-07 21:33:12 +00001990 && ea.cmdidx != CMD_checktime
Bram Moolenaar379fb762018-08-30 15:58:28 +02001991 && ea.cmdidx != CMD_edit
1992 && ea.cmdidx != CMD_file
Bram Moolenaar958636c2014-10-21 20:01:58 +02001993 && !IS_USER_CMDIDX(ea.cmdidx)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001994 && curbuf_locked())
1995 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001996
Bram Moolenaar8071cb22019-07-12 17:58:01 +02001997 if (!ni && !(ea.argt & EX_RANGE) && ea.addr_count > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001998 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001999 // no range allowed
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002000 errormsg = _(e_norange);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002001 goto doend;
2002 }
2003 }
2004
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002005 if (!ni && !(ea.argt & EX_BANG) && ea.forceit) // no <!> allowed
Bram Moolenaar071d4272004-06-13 20:20:40 +00002006 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002007 errormsg = _(e_nobang);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002008 goto doend;
2009 }
2010
2011 /*
2012 * Don't complain about the range if it is not used
2013 * (could happen if line_count is accidentally set to 0).
2014 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002015 if (!ea.skip && !ni && (ea.argt & EX_RANGE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002016 {
2017 /*
2018 * If the range is backwards, ask for confirmation and, if given, swap
2019 * ea.line1 & ea.line2 so it's forwards again.
2020 * When global command is busy, don't ask, will fail below.
2021 */
2022 if (!global_busy && ea.line1 > ea.line2)
2023 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002024 if (msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002025 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002026 if (sourcing || exmode_active)
2027 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002028 errormsg = _("E493: Backwards range given");
Bram Moolenaara5792f52005-11-23 21:25:05 +00002029 goto doend;
2030 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002031 if (ask_yesno((char_u *)
2032 _("Backwards range given, OK to swap"), FALSE) != 'y')
Bram Moolenaara5792f52005-11-23 21:25:05 +00002033 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002034 }
2035 lnum = ea.line1;
2036 ea.line1 = ea.line2;
2037 ea.line2 = lnum;
2038 }
2039 if ((errormsg = invalid_range(&ea)) != NULL)
2040 goto doend;
2041 }
2042
Bram Moolenaarb7316892019-05-01 18:08:42 +02002043 if ((ea.addr_type == ADDR_OTHER) && ea.addr_count == 0)
2044 // default is 1, not cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00002045 ea.line2 = 1;
2046
2047 correct_range(&ea);
2048
2049#ifdef FEAT_FOLDING
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002050 if (((ea.argt & EX_WHOLEFOLD) || ea.addr_count >= 2) && !global_busy
Bram Moolenaara3306952016-01-02 21:41:06 +01002051 && ea.addr_type == ADDR_LINES)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002052 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002053 // Put the first line at the start of a closed fold, put the last line
2054 // at the end of a closed fold.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002055 (void)hasFolding(ea.line1, &ea.line1, NULL);
2056 (void)hasFolding(ea.line2, NULL, &ea.line2);
2057 }
2058#endif
2059
2060#ifdef FEAT_QUICKFIX
2061 /*
Bram Moolenaar86b68352004-12-27 21:59:20 +00002062 * For the ":make" and ":grep" commands we insert the 'makeprg'/'grepprg'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002063 * option here, so things like % get expanded.
2064 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002065 p = replace_makeprg(&ea, p, cmdlinep);
2066 if (p == NULL)
2067 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002068#endif
2069
2070 /*
2071 * Skip to start of argument.
2072 * Don't do this for the ":!" command, because ":!! -l" needs the space.
2073 */
2074 if (ea.cmdidx == CMD_bang)
2075 ea.arg = p;
2076 else
2077 ea.arg = skipwhite(p);
2078
Bram Moolenaar379fb762018-08-30 15:58:28 +02002079 // ":file" cannot be run with an argument when "curbuf_lock" is set
2080 if (ea.cmdidx == CMD_file && *ea.arg != NUL && curbuf_locked())
2081 goto doend;
2082
Bram Moolenaar071d4272004-06-13 20:20:40 +00002083 /*
2084 * Check for "++opt=val" argument.
2085 * Must be first, allow ":w ++enc=utf8 !cmd"
2086 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002087 if (ea.argt & EX_ARGOPT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002088 while (ea.arg[0] == '+' && ea.arg[1] == '+')
2089 if (getargopt(&ea) == FAIL && !ni)
2090 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002091 errormsg = _(e_invarg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002092 goto doend;
2093 }
2094
2095 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
2096 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002097 if (*ea.arg == '>') // append
Bram Moolenaar071d4272004-06-13 20:20:40 +00002098 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002099 if (*++ea.arg != '>') // typed wrong
Bram Moolenaar071d4272004-06-13 20:20:40 +00002100 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002101 errormsg = _("E494: Use w or w>>");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002102 goto doend;
2103 }
2104 ea.arg = skipwhite(ea.arg + 1);
2105 ea.append = TRUE;
2106 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002107 else if (*ea.arg == '!' && ea.cmdidx == CMD_write) // :w !filter
Bram Moolenaar071d4272004-06-13 20:20:40 +00002108 {
2109 ++ea.arg;
2110 ea.usefilter = TRUE;
2111 }
2112 }
2113
2114 if (ea.cmdidx == CMD_read)
2115 {
2116 if (ea.forceit)
2117 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002118 ea.usefilter = TRUE; // :r! filter if ea.forceit
Bram Moolenaar071d4272004-06-13 20:20:40 +00002119 ea.forceit = FALSE;
2120 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002121 else if (*ea.arg == '!') // :r !filter
Bram Moolenaar071d4272004-06-13 20:20:40 +00002122 {
2123 ++ea.arg;
2124 ea.usefilter = TRUE;
2125 }
2126 }
2127
2128 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
2129 {
2130 ea.amount = 1;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002131 while (*ea.arg == *ea.cmd) // count number of '>' or '<'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002132 {
2133 ++ea.arg;
2134 ++ea.amount;
2135 }
2136 ea.arg = skipwhite(ea.arg);
2137 }
2138
2139 /*
2140 * Check for "+command" argument, before checking for next command.
2141 * Don't do this for ":read !cmd" and ":write !cmd".
2142 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002143 if ((ea.argt & EX_CMDARG) && !ea.usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002144 ea.do_ecmd_cmd = getargcmd(&ea.arg);
2145
2146 /*
2147 * Check for '|' to separate commands and '"' to start comments.
2148 * Don't do this for ":read !cmd" and ":write !cmd".
2149 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002150 if ((ea.argt & EX_TRLBAR) && !ea.usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002151 separate_nextcmd(&ea);
2152
2153 /*
2154 * Check for <newline> to end a shell command.
Bram Moolenaardf177f62005-02-22 08:39:57 +00002155 * Also do this for ":read !cmd", ":write !cmd" and ":global".
2156 * Any others?
Bram Moolenaar071d4272004-06-13 20:20:40 +00002157 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00002158 else if (ea.cmdidx == CMD_bang
Bram Moolenaar67883b42017-07-27 22:57:00 +02002159 || ea.cmdidx == CMD_terminal
Bram Moolenaardf177f62005-02-22 08:39:57 +00002160 || ea.cmdidx == CMD_global
2161 || ea.cmdidx == CMD_vglobal
2162 || ea.usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002163 {
2164 for (p = ea.arg; *p; ++p)
2165 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002166 // Remove one backslash before a newline, so that it's possible to
2167 // pass a newline to the shell and also a newline that is preceded
2168 // with a backslash. This makes it impossible to end a shell
2169 // command in a backslash, but that doesn't appear useful.
2170 // Halving the number of backslashes is incompatible with previous
2171 // versions.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002172 if (*p == '\\' && p[1] == '\n')
Bram Moolenaara7241f52008-06-24 20:39:31 +00002173 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002174 else if (*p == '\n')
2175 {
2176 ea.nextcmd = p + 1;
2177 *p = NUL;
2178 break;
2179 }
2180 }
2181 }
2182
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002183 if ((ea.argt & EX_DFLALL) && ea.addr_count == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002184 {
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002185 buf_T *buf;
2186
Bram Moolenaar071d4272004-06-13 20:20:40 +00002187 ea.line1 = 1;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002188 switch (ea.addr_type)
2189 {
2190 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02002191 case ADDR_OTHER:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002192 ea.line2 = curbuf->b_ml.ml_line_count;
2193 break;
2194 case ADDR_LOADED_BUFFERS:
2195 buf = firstbuf;
2196 while (buf->b_next != NULL && buf->b_ml.ml_mfp == NULL)
2197 buf = buf->b_next;
2198 ea.line1 = buf->b_fnum;
2199 buf = lastbuf;
2200 while (buf->b_prev != NULL && buf->b_ml.ml_mfp == NULL)
2201 buf = buf->b_prev;
2202 ea.line2 = buf->b_fnum;
2203 break;
2204 case ADDR_BUFFERS:
2205 ea.line1 = firstbuf->b_fnum;
2206 ea.line2 = lastbuf->b_fnum;
2207 break;
2208 case ADDR_WINDOWS:
2209 ea.line2 = LAST_WIN_NR;
2210 break;
2211 case ADDR_TABS:
2212 ea.line2 = LAST_TAB_NR;
2213 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01002214 case ADDR_TABS_RELATIVE:
2215 ea.line2 = 1;
2216 break;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002217 case ADDR_ARGUMENTS:
2218 if (ARGCOUNT == 0)
2219 ea.line1 = ea.line2 = 0;
2220 else
2221 ea.line2 = ARGCOUNT;
2222 break;
Bram Moolenaar25190db2019-05-04 15:05:28 +02002223 case ADDR_QUICKFIX_VALID:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02002224#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02002225 ea.line2 = qf_get_valid_size(&ea);
Bram Moolenaaraa23b372015-09-08 18:46:31 +02002226 if (ea.line2 == 0)
2227 ea.line2 = 1;
Bram Moolenaare906c502015-09-09 21:10:39 +02002228#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02002229 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02002230 case ADDR_NONE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02002231 case ADDR_UNSIGNED:
2232 case ADDR_QUICKFIX:
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002233 iemsg(_("INTERNAL: Cannot use EX_DFLALL with ADDR_NONE, ADDR_UNSIGNED or ADDR_QUICKFIX"));
Bram Moolenaarb7316892019-05-01 18:08:42 +02002234 break;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002235 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002236 }
2237
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002238 // accept numbered register only when no count allowed (:put)
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002239 if ( (ea.argt & EX_REGSTR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002240 && *ea.arg != NUL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002241 // Do not allow register = for user commands
Bram Moolenaar958636c2014-10-21 20:01:58 +02002242 && (!IS_USER_CMDIDX(ea.cmdidx) || *ea.arg != '=')
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002243 && !((ea.argt & EX_COUNT) && VIM_ISDIGIT(*ea.arg)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002244 {
Bram Moolenaar85de2062011-05-05 14:26:41 +02002245#ifndef FEAT_CLIPBOARD
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002246 // check these explicitly for a more specific error message
Bram Moolenaar85de2062011-05-05 14:26:41 +02002247 if (*ea.arg == '*' || *ea.arg == '+')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002248 {
Bram Moolenaar99b12722019-01-14 20:16:40 +01002249 errormsg = _(e_invalidreg);
Bram Moolenaar85de2062011-05-05 14:26:41 +02002250 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002251 }
2252#endif
Bram Moolenaar958636c2014-10-21 20:01:58 +02002253 if (valid_yank_reg(*ea.arg, (ea.cmdidx != CMD_put
2254 && !IS_USER_CMDIDX(ea.cmdidx))))
Bram Moolenaar85de2062011-05-05 14:26:41 +02002255 {
2256 ea.regname = *ea.arg++;
2257#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002258 // for '=' register: accept the rest of the line as an expression
Bram Moolenaar85de2062011-05-05 14:26:41 +02002259 if (ea.arg[-1] == '=' && ea.arg[0] != NUL)
2260 {
2261 set_expr_line(vim_strsave(ea.arg));
2262 ea.arg += STRLEN(ea.arg);
2263 }
2264#endif
2265 ea.arg = skipwhite(ea.arg);
2266 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002267 }
2268
2269 /*
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002270 * Check for a count. When accepting a EX_BUFNAME, don't use "123foo" as a
Bram Moolenaar071d4272004-06-13 20:20:40 +00002271 * count, it's a buffer name.
2272 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002273 if ((ea.argt & EX_COUNT) && VIM_ISDIGIT(*ea.arg)
2274 && (!(ea.argt & EX_BUFNAME) || *(p = skipdigits(ea.arg)) == NUL
Bram Moolenaar1c465442017-03-12 20:10:05 +01002275 || VIM_ISWHITE(*p)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002276 {
2277 n = getdigits(&ea.arg);
2278 ea.arg = skipwhite(ea.arg);
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002279 if (n <= 0 && !ni && (ea.argt & EX_ZEROR) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002280 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002281 errormsg = _(e_zerocount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002282 goto doend;
2283 }
Bram Moolenaarb7316892019-05-01 18:08:42 +02002284 if (ea.addr_type != ADDR_LINES) // e.g. :buffer 2, :sleep 3
Bram Moolenaar071d4272004-06-13 20:20:40 +00002285 {
2286 ea.line2 = n;
2287 if (ea.addr_count == 0)
2288 ea.addr_count = 1;
2289 }
2290 else
2291 {
2292 ea.line1 = ea.line2;
2293 ea.line2 += n - 1;
2294 ++ea.addr_count;
2295 /*
2296 * Be vi compatible: no error message for out of range.
2297 */
Bram Moolenaarb7316892019-05-01 18:08:42 +02002298 if (ea.line2 > curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002299 ea.line2 = curbuf->b_ml.ml_line_count;
2300 }
2301 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00002302
2303 /*
2304 * Check for flags: 'l', 'p' and '#'.
2305 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002306 if (ea.argt & EX_FLAGS)
Bram Moolenaardf177f62005-02-22 08:39:57 +00002307 get_flags(&ea);
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002308 if (!ni && !(ea.argt & EX_EXTRA) && *ea.arg != NUL
2309 && *ea.arg != '"' && (*ea.arg != '|' || (ea.argt & EX_TRLBAR) == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002310 {
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002311 // no arguments allowed but there is something
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002312 errormsg = _(e_trailing);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002313 goto doend;
2314 }
2315
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002316 if (!ni && (ea.argt & EX_NEEDARG) && *ea.arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002317 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002318 errormsg = _(e_argreq);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002319 goto doend;
2320 }
2321
2322#ifdef FEAT_EVAL
2323 /*
2324 * Skip the command when it's not going to be executed.
2325 * The commands like :if, :endif, etc. always need to be executed.
2326 * Also make an exception for commands that handle a trailing command
2327 * themselves.
2328 */
2329 if (ea.skip)
2330 {
2331 switch (ea.cmdidx)
2332 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002333 // commands that need evaluation
Bram Moolenaar071d4272004-06-13 20:20:40 +00002334 case CMD_while:
2335 case CMD_endwhile:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00002336 case CMD_for:
2337 case CMD_endfor:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002338 case CMD_if:
2339 case CMD_elseif:
2340 case CMD_else:
2341 case CMD_endif:
2342 case CMD_try:
2343 case CMD_catch:
2344 case CMD_finally:
2345 case CMD_endtry:
2346 case CMD_function:
2347 break;
2348
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002349 // Commands that handle '|' themselves. Check: A command should
2350 // either have the EX_TRLBAR flag, appear in this list or appear in
2351 // the list at ":help :bar".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002352 case CMD_aboveleft:
2353 case CMD_and:
2354 case CMD_belowright:
2355 case CMD_botright:
2356 case CMD_browse:
2357 case CMD_call:
2358 case CMD_confirm:
Bram Moolenaar8f76e6b2019-11-26 16:50:30 +01002359 case CMD_const:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002360 case CMD_delfunction:
2361 case CMD_djump:
2362 case CMD_dlist:
2363 case CMD_dsearch:
2364 case CMD_dsplit:
2365 case CMD_echo:
2366 case CMD_echoerr:
2367 case CMD_echomsg:
2368 case CMD_echon:
2369 case CMD_execute:
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002370 case CMD_filter:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002371 case CMD_help:
2372 case CMD_hide:
2373 case CMD_ijump:
2374 case CMD_ilist:
2375 case CMD_isearch:
2376 case CMD_isplit:
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002377 case CMD_keepalt:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002378 case CMD_keepjumps:
2379 case CMD_keepmarks:
Bram Moolenaara939e432013-11-09 05:30:26 +01002380 case CMD_keeppatterns:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002381 case CMD_leftabove:
2382 case CMD_let:
2383 case CMD_lockmarks:
Bram Moolenaarcc4423a2019-11-26 17:05:00 +01002384 case CMD_lockvar:
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002385 case CMD_lua:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002386 case CMD_match:
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002387 case CMD_mzscheme:
Bram Moolenaar5803ae62014-03-23 16:04:02 +01002388 case CMD_noautocmd:
2389 case CMD_noswapfile:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002390 case CMD_perl:
2391 case CMD_psearch:
2392 case CMD_python:
Bram Moolenaar368373e2010-07-19 20:46:22 +02002393 case CMD_py3:
Bram Moolenaarb6590522010-07-21 16:00:43 +02002394 case CMD_python3:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002395 case CMD_return:
2396 case CMD_rightbelow:
2397 case CMD_ruby:
2398 case CMD_silent:
2399 case CMD_smagic:
2400 case CMD_snomagic:
2401 case CMD_substitute:
2402 case CMD_syntax:
Bram Moolenaara226a6d2006-02-26 23:59:20 +00002403 case CMD_tab:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002404 case CMD_tcl:
2405 case CMD_throw:
2406 case CMD_tilde:
2407 case CMD_topleft:
2408 case CMD_unlet:
Bram Moolenaarcc4423a2019-11-26 17:05:00 +01002409 case CMD_unlockvar:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002410 case CMD_verbose:
2411 case CMD_vertical:
Bram Moolenaar12bde492011-06-13 01:19:56 +02002412 case CMD_wincmd:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002413 break;
2414
2415 default: goto doend;
2416 }
2417 }
2418#endif
2419
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002420 if (ea.argt & EX_XFILE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002421 {
2422 if (expand_filename(&ea, cmdlinep, &errormsg) == FAIL)
2423 goto doend;
2424 }
2425
Bram Moolenaar071d4272004-06-13 20:20:40 +00002426 /*
2427 * Accept buffer name. Cannot be used at the same time with a buffer
2428 * number. Don't do this for a user command.
2429 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002430 if ((ea.argt & EX_BUFNAME) && *ea.arg != NUL && ea.addr_count == 0
Bram Moolenaar958636c2014-10-21 20:01:58 +02002431 && !IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002432 {
2433 /*
2434 * :bdelete, :bwipeout and :bunload take several arguments, separated
2435 * by spaces: find next space (skipping over escaped characters).
2436 * The others take one argument: ignore trailing spaces.
2437 */
2438 if (ea.cmdidx == CMD_bdelete || ea.cmdidx == CMD_bwipeout
2439 || ea.cmdidx == CMD_bunload)
2440 p = skiptowhite_esc(ea.arg);
2441 else
2442 {
2443 p = ea.arg + STRLEN(ea.arg);
Bram Moolenaar1c465442017-03-12 20:10:05 +01002444 while (p > ea.arg && VIM_ISWHITE(p[-1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002445 --p;
2446 }
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002447 ea.line2 = buflist_findpat(ea.arg, p, (ea.argt & EX_BUFUNL) != 0,
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002448 FALSE, FALSE);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002449 if (ea.line2 < 0) // failed
Bram Moolenaar071d4272004-06-13 20:20:40 +00002450 goto doend;
2451 ea.addr_count = 1;
2452 ea.arg = skipwhite(p);
2453 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002454
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002455 // The :try command saves the emsg_silent flag, reset it here when
2456 // ":silent! try" was used, it should only apply to :try itself.
Bram Moolenaareffed932018-08-14 13:38:17 +02002457 if (ea.cmdidx == CMD_try && ea.did_esilent > 0)
Bram Moolenaar2be57332018-02-13 18:05:18 +01002458 {
Bram Moolenaareffed932018-08-14 13:38:17 +02002459 emsg_silent -= ea.did_esilent;
Bram Moolenaar2be57332018-02-13 18:05:18 +01002460 if (emsg_silent < 0)
2461 emsg_silent = 0;
Bram Moolenaareffed932018-08-14 13:38:17 +02002462 ea.did_esilent = 0;
Bram Moolenaar2be57332018-02-13 18:05:18 +01002463 }
2464
Bram Moolenaar071d4272004-06-13 20:20:40 +00002465/*
Bram Moolenaar2be57332018-02-13 18:05:18 +01002466 * 7. Execute the command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002467 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002468
Bram Moolenaar958636c2014-10-21 20:01:58 +02002469 if (IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002470 {
2471 /*
2472 * Execute a user-defined command.
2473 */
2474 do_ucmd(&ea);
2475 }
2476 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002477 {
2478 /*
Bram Moolenaarac9fb182019-04-27 13:04:13 +02002479 * Call the function to execute the builtin command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002480 */
2481 ea.errmsg = NULL;
2482 (cmdnames[ea.cmdidx].cmd_func)(&ea);
2483 if (ea.errmsg != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002484 errormsg = _(ea.errmsg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002485 }
2486
2487#ifdef FEAT_EVAL
2488 /*
2489 * If the command just executed called do_cmdline(), any throw or ":return"
2490 * or ":finish" encountered there must also check the cstack of the still
2491 * active do_cmdline() that called this do_one_cmd(). Rethrow an uncaught
2492 * exception, or reanimate a returned function or finished script file and
2493 * return or finish it again.
2494 */
2495 if (need_rethrow)
2496 do_throw(cstack);
2497 else if (check_cstack)
2498 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002499 if (source_finished(fgetline, cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002500 do_finish(&ea, TRUE);
Bram Moolenaar89d40322006-08-29 15:30:07 +00002501 else if (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002502 && current_func_returned())
2503 do_return(&ea, TRUE, FALSE, NULL);
2504 }
2505 need_rethrow = check_cstack = FALSE;
2506#endif
2507
2508doend:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002509 if (curwin->w_cursor.lnum == 0) // can happen with zero line number
Bram Moolenaar6de5e122017-04-20 21:55:44 +02002510 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002511 curwin->w_cursor.lnum = 1;
Bram Moolenaar6de5e122017-04-20 21:55:44 +02002512 curwin->w_cursor.col = 0;
2513 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002514
2515 if (errormsg != NULL && *errormsg != NUL && !did_emsg)
2516 {
2517 if (sourcing)
2518 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002519 if (errormsg != (char *)IObuff)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002520 {
2521 STRCPY(IObuff, errormsg);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002522 errormsg = (char *)IObuff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002523 }
Bram Moolenaara6f4d612011-09-21 19:10:46 +02002524 append_command(*cmdlinep);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002525 }
2526 emsg(errormsg);
2527 }
2528#ifdef FEAT_EVAL
2529 do_errthrow(cstack,
Bram Moolenaar958636c2014-10-21 20:01:58 +02002530 (ea.cmdidx != CMD_SIZE && !IS_USER_CMDIDX(ea.cmdidx))
2531 ? cmdnames[(int)ea.cmdidx].cmd_name : (char_u *)NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002532#endif
2533
Bram Moolenaareffed932018-08-14 13:38:17 +02002534 if (ea.verbose_save >= 0)
2535 p_verbose = ea.verbose_save;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002536
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002537 free_cmdmod();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002538 cmdmod = save_cmdmod;
Bram Moolenaar9a2c0912019-03-30 14:26:18 +01002539 reg_executing = save_reg_executing;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002540
Bram Moolenaareffed932018-08-14 13:38:17 +02002541 if (ea.save_msg_silent != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002542 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002543 // messages could be enabled for a serious error, need to check if the
2544 // counters don't become negative
Bram Moolenaareffed932018-08-14 13:38:17 +02002545 if (!did_emsg || msg_silent > ea.save_msg_silent)
2546 msg_silent = ea.save_msg_silent;
2547 emsg_silent -= ea.did_esilent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002548 if (emsg_silent < 0)
2549 emsg_silent = 0;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002550 // Restore msg_scroll, it's set by file I/O commands, even when no
2551 // message is actually displayed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002552 msg_scroll = save_msg_scroll;
Bram Moolenaar77ab2802009-04-22 12:44:48 +00002553
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002554 // "silent reg" or "silent echo x" inside "redir" leaves msg_col
2555 // somewhere in the line. Put it back in the first column.
Bram Moolenaar77ab2802009-04-22 12:44:48 +00002556 if (redirecting())
2557 msg_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002558 }
2559
Bram Moolenaar7171abe2004-10-11 10:06:20 +00002560#ifdef HAVE_SANDBOX
Bram Moolenaareffed932018-08-14 13:38:17 +02002561 if (ea.did_sandbox)
Bram Moolenaar7171abe2004-10-11 10:06:20 +00002562 --sandbox;
2563#endif
2564
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002565 if (ea.nextcmd && *ea.nextcmd == NUL) // not really a next command
Bram Moolenaar071d4272004-06-13 20:20:40 +00002566 ea.nextcmd = NULL;
2567
2568#ifdef FEAT_EVAL
2569 --ex_nesting_level;
2570#endif
2571
2572 return ea.nextcmd;
2573}
2574#if (_MSC_VER == 1200)
Bram Moolenaar281bdce2005-01-25 21:53:18 +00002575 #pragma optimize( "", on )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002576#endif
2577
2578/*
Bram Moolenaareffed932018-08-14 13:38:17 +02002579 * Parse and skip over command modifiers:
2580 * - update eap->cmd
2581 * - store flags in "cmdmod".
2582 * - Set ex_pressedreturn for an empty command line.
2583 * - set msg_silent for ":silent"
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002584 * - set 'eventignore' to "all" for ":noautocmd"
Bram Moolenaareffed932018-08-14 13:38:17 +02002585 * - set p_verbose for ":verbose"
2586 * - Increment "sandbox" for ":sandbox"
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002587 * When "skip_only" is TRUE the global variables are not changed, except for
2588 * "cmdmod".
Bram Moolenaareffed932018-08-14 13:38:17 +02002589 * Return FAIL when the command is not to be executed.
2590 * May set "errormsg" to an error message.
2591 */
2592 int
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002593parse_command_modifiers(exarg_T *eap, char **errormsg, int skip_only)
Bram Moolenaareffed932018-08-14 13:38:17 +02002594{
2595 char_u *p;
2596
2597 vim_memset(&cmdmod, 0, sizeof(cmdmod));
2598 eap->verbose_save = -1;
2599 eap->save_msg_silent = -1;
2600
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002601 // Repeat until no more command modifiers are found.
Bram Moolenaareffed932018-08-14 13:38:17 +02002602 for (;;)
2603 {
Bram Moolenaareffed932018-08-14 13:38:17 +02002604 while (*eap->cmd == ' ' || *eap->cmd == '\t' || *eap->cmd == ':')
2605 ++eap->cmd;
2606
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002607 // in ex mode, an empty line works like :+
Bram Moolenaareffed932018-08-14 13:38:17 +02002608 if (*eap->cmd == NUL && exmode_active
2609 && (getline_equal(eap->getline, eap->cookie, getexmodeline)
2610 || getline_equal(eap->getline, eap->cookie, getexline))
2611 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
2612 {
2613 eap->cmd = (char_u *)"+";
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002614 if (!skip_only)
2615 ex_pressedreturn = TRUE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002616 }
2617
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002618 // ignore comment and empty lines
Bram Moolenaareffed932018-08-14 13:38:17 +02002619 if (*eap->cmd == '"')
2620 return FAIL;
2621 if (*eap->cmd == NUL)
2622 {
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002623 if (!skip_only)
2624 ex_pressedreturn = TRUE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002625 return FAIL;
2626 }
2627
Bram Moolenaareffed932018-08-14 13:38:17 +02002628 p = skip_range(eap->cmd, NULL);
2629 switch (*p)
2630 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002631 // When adding an entry, also modify cmd_exists().
Bram Moolenaareffed932018-08-14 13:38:17 +02002632 case 'a': if (!checkforcmd(&eap->cmd, "aboveleft", 3))
2633 break;
2634 cmdmod.split |= WSP_ABOVE;
2635 continue;
2636
2637 case 'b': if (checkforcmd(&eap->cmd, "belowright", 3))
2638 {
2639 cmdmod.split |= WSP_BELOW;
2640 continue;
2641 }
2642 if (checkforcmd(&eap->cmd, "browse", 3))
2643 {
2644#ifdef FEAT_BROWSE_CMD
2645 cmdmod.browse = TRUE;
2646#endif
2647 continue;
2648 }
2649 if (!checkforcmd(&eap->cmd, "botright", 2))
2650 break;
2651 cmdmod.split |= WSP_BOT;
2652 continue;
2653
2654 case 'c': if (!checkforcmd(&eap->cmd, "confirm", 4))
2655 break;
2656#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
2657 cmdmod.confirm = TRUE;
2658#endif
2659 continue;
2660
2661 case 'k': if (checkforcmd(&eap->cmd, "keepmarks", 3))
2662 {
2663 cmdmod.keepmarks = TRUE;
2664 continue;
2665 }
2666 if (checkforcmd(&eap->cmd, "keepalt", 5))
2667 {
2668 cmdmod.keepalt = TRUE;
2669 continue;
2670 }
2671 if (checkforcmd(&eap->cmd, "keeppatterns", 5))
2672 {
2673 cmdmod.keeppatterns = TRUE;
2674 continue;
2675 }
2676 if (!checkforcmd(&eap->cmd, "keepjumps", 5))
2677 break;
2678 cmdmod.keepjumps = TRUE;
2679 continue;
2680
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002681 case 'f': // only accept ":filter {pat} cmd"
Bram Moolenaareffed932018-08-14 13:38:17 +02002682 {
2683 char_u *reg_pat;
2684
2685 if (!checkforcmd(&p, "filter", 4)
2686 || *p == NUL || ends_excmd(*p))
2687 break;
2688 if (*p == '!')
2689 {
2690 cmdmod.filter_force = TRUE;
2691 p = skipwhite(p + 1);
2692 if (*p == NUL || ends_excmd(*p))
2693 break;
2694 }
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002695 if (skip_only)
2696 p = skip_vimgrep_pat(p, NULL, NULL);
2697 else
2698 // NOTE: This puts a NUL after the pattern.
2699 p = skip_vimgrep_pat(p, &reg_pat, NULL);
Bram Moolenaareffed932018-08-14 13:38:17 +02002700 if (p == NULL || *p == NUL)
2701 break;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002702 if (!skip_only)
2703 {
2704 cmdmod.filter_regmatch.regprog =
Bram Moolenaareffed932018-08-14 13:38:17 +02002705 vim_regcomp(reg_pat, RE_MAGIC);
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002706 if (cmdmod.filter_regmatch.regprog == NULL)
2707 break;
2708 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002709 eap->cmd = p;
2710 continue;
2711 }
2712
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002713 // ":hide" and ":hide | cmd" are not modifiers
Bram Moolenaareffed932018-08-14 13:38:17 +02002714 case 'h': if (p != eap->cmd || !checkforcmd(&p, "hide", 3)
2715 || *p == NUL || ends_excmd(*p))
2716 break;
2717 eap->cmd = p;
2718 cmdmod.hide = TRUE;
2719 continue;
2720
2721 case 'l': if (checkforcmd(&eap->cmd, "lockmarks", 3))
2722 {
2723 cmdmod.lockmarks = TRUE;
2724 continue;
2725 }
2726
2727 if (!checkforcmd(&eap->cmd, "leftabove", 5))
2728 break;
2729 cmdmod.split |= WSP_ABOVE;
2730 continue;
2731
2732 case 'n': if (checkforcmd(&eap->cmd, "noautocmd", 3))
2733 {
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002734 if (cmdmod.save_ei == NULL && !skip_only)
Bram Moolenaareffed932018-08-14 13:38:17 +02002735 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002736 // Set 'eventignore' to "all". Restore the
2737 // existing option value later.
Bram Moolenaareffed932018-08-14 13:38:17 +02002738 cmdmod.save_ei = vim_strsave(p_ei);
2739 set_string_option_direct((char_u *)"ei", -1,
2740 (char_u *)"all", OPT_FREE, SID_NONE);
2741 }
2742 continue;
2743 }
2744 if (!checkforcmd(&eap->cmd, "noswapfile", 3))
2745 break;
2746 cmdmod.noswapfile = TRUE;
2747 continue;
2748
2749 case 'r': if (!checkforcmd(&eap->cmd, "rightbelow", 6))
2750 break;
2751 cmdmod.split |= WSP_BELOW;
2752 continue;
2753
2754 case 's': if (checkforcmd(&eap->cmd, "sandbox", 3))
2755 {
2756#ifdef HAVE_SANDBOX
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002757 if (!skip_only)
2758 {
2759 if (!eap->did_sandbox)
2760 ++sandbox;
2761 eap->did_sandbox = TRUE;
2762 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002763#endif
2764 continue;
2765 }
2766 if (!checkforcmd(&eap->cmd, "silent", 3))
2767 break;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002768 if (!skip_only)
2769 {
2770 if (eap->save_msg_silent == -1)
2771 eap->save_msg_silent = msg_silent;
2772 ++msg_silent;
2773 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002774 if (*eap->cmd == '!' && !VIM_ISWHITE(eap->cmd[-1]))
2775 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002776 // ":silent!", but not "silent !cmd"
Bram Moolenaareffed932018-08-14 13:38:17 +02002777 eap->cmd = skipwhite(eap->cmd + 1);
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002778 if (!skip_only)
2779 {
2780 ++emsg_silent;
2781 ++eap->did_esilent;
2782 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002783 }
2784 continue;
2785
2786 case 't': if (checkforcmd(&p, "tab", 3))
2787 {
Bram Moolenaar548e5982018-12-26 21:45:00 +01002788 if (!skip_only)
Bram Moolenaareffed932018-08-14 13:38:17 +02002789 {
Bram Moolenaar548e5982018-12-26 21:45:00 +01002790 long tabnr = get_address(eap, &eap->cmd,
2791 ADDR_TABS, eap->skip,
2792 skip_only, FALSE, 1);
2793 if (tabnr == MAXLNUM)
2794 cmdmod.tab = tabpage_index(curtab) + 1;
2795 else
Bram Moolenaareffed932018-08-14 13:38:17 +02002796 {
Bram Moolenaar548e5982018-12-26 21:45:00 +01002797 if (tabnr < 0 || tabnr > LAST_TAB_NR)
2798 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002799 *errormsg = _(e_invrange);
Bram Moolenaar548e5982018-12-26 21:45:00 +01002800 return FAIL;
2801 }
2802 cmdmod.tab = tabnr + 1;
Bram Moolenaareffed932018-08-14 13:38:17 +02002803 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002804 }
2805 eap->cmd = p;
2806 continue;
2807 }
2808 if (!checkforcmd(&eap->cmd, "topleft", 2))
2809 break;
2810 cmdmod.split |= WSP_TOP;
2811 continue;
2812
2813 case 'u': if (!checkforcmd(&eap->cmd, "unsilent", 3))
2814 break;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002815 if (!skip_only)
2816 {
2817 if (eap->save_msg_silent == -1)
2818 eap->save_msg_silent = msg_silent;
2819 msg_silent = 0;
2820 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002821 continue;
2822
2823 case 'v': if (checkforcmd(&eap->cmd, "vertical", 4))
2824 {
2825 cmdmod.split |= WSP_VERT;
2826 continue;
2827 }
2828 if (!checkforcmd(&p, "verbose", 4))
2829 break;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002830 if (!skip_only)
2831 {
2832 if (eap->verbose_save < 0)
2833 eap->verbose_save = p_verbose;
2834 if (vim_isdigit(*eap->cmd))
2835 p_verbose = atoi((char *)eap->cmd);
2836 else
2837 p_verbose = 1;
2838 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002839 eap->cmd = p;
2840 continue;
2841 }
2842 break;
2843 }
2844
2845 return OK;
2846}
2847
2848/*
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002849 * Free contents of "cmdmod".
2850 */
2851 static void
2852free_cmdmod(void)
2853{
2854 if (cmdmod.save_ei != NULL)
2855 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002856 // Restore 'eventignore' to the value before ":noautocmd".
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002857 set_string_option_direct((char_u *)"ei", -1, cmdmod.save_ei,
2858 OPT_FREE, SID_NONE);
2859 free_string_option(cmdmod.save_ei);
2860 }
2861
2862 if (cmdmod.filter_regmatch.regprog != NULL)
2863 vim_regfree(cmdmod.filter_regmatch.regprog);
2864}
2865
2866/*
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002867 * Parse the address range, if any, in "eap".
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02002868 * May set the last search pattern, unless "silent" is TRUE.
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002869 * Return FAIL and set "errormsg" or return OK.
2870 */
2871 int
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002872parse_cmd_address(exarg_T *eap, char **errormsg, int silent)
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002873{
2874 int address_count = 1;
2875 linenr_T lnum;
2876
2877 // Repeat for all ',' or ';' separated addresses.
2878 for (;;)
2879 {
2880 eap->line1 = eap->line2;
2881 switch (eap->addr_type)
2882 {
2883 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02002884 case ADDR_OTHER:
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002885 // default is current line number
2886 eap->line2 = curwin->w_cursor.lnum;
2887 break;
2888 case ADDR_WINDOWS:
2889 eap->line2 = CURRENT_WIN_NR;
2890 break;
2891 case ADDR_ARGUMENTS:
2892 eap->line2 = curwin->w_arg_idx + 1;
2893 if (eap->line2 > ARGCOUNT)
2894 eap->line2 = ARGCOUNT;
2895 break;
2896 case ADDR_LOADED_BUFFERS:
2897 case ADDR_BUFFERS:
2898 eap->line2 = curbuf->b_fnum;
2899 break;
2900 case ADDR_TABS:
2901 eap->line2 = CURRENT_TAB_NR;
2902 break;
2903 case ADDR_TABS_RELATIVE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02002904 case ADDR_UNSIGNED:
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002905 eap->line2 = 1;
2906 break;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002907 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02002908#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02002909 eap->line2 = qf_get_cur_idx(eap);
2910#endif
2911 break;
2912 case ADDR_QUICKFIX_VALID:
2913#ifdef FEAT_QUICKFIX
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002914 eap->line2 = qf_get_cur_valid_idx(eap);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002915#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02002916 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02002917 case ADDR_NONE:
2918 // Will give an error later if a range is found.
2919 break;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002920 }
2921 eap->cmd = skipwhite(eap->cmd);
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02002922 lnum = get_address(eap, &eap->cmd, eap->addr_type, eap->skip, silent,
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002923 eap->addr_count == 0, address_count++);
2924 if (eap->cmd == NULL) // error detected
2925 return FAIL;
2926 if (lnum == MAXLNUM)
2927 {
2928 if (*eap->cmd == '%') // '%' - all lines
2929 {
2930 ++eap->cmd;
2931 switch (eap->addr_type)
2932 {
2933 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02002934 case ADDR_OTHER:
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002935 eap->line1 = 1;
2936 eap->line2 = curbuf->b_ml.ml_line_count;
2937 break;
2938 case ADDR_LOADED_BUFFERS:
2939 {
2940 buf_T *buf = firstbuf;
2941
2942 while (buf->b_next != NULL
2943 && buf->b_ml.ml_mfp == NULL)
2944 buf = buf->b_next;
2945 eap->line1 = buf->b_fnum;
2946 buf = lastbuf;
2947 while (buf->b_prev != NULL
2948 && buf->b_ml.ml_mfp == NULL)
2949 buf = buf->b_prev;
2950 eap->line2 = buf->b_fnum;
2951 break;
2952 }
2953 case ADDR_BUFFERS:
2954 eap->line1 = firstbuf->b_fnum;
2955 eap->line2 = lastbuf->b_fnum;
2956 break;
2957 case ADDR_WINDOWS:
2958 case ADDR_TABS:
2959 if (IS_USER_CMDIDX(eap->cmdidx))
2960 {
2961 eap->line1 = 1;
2962 eap->line2 = eap->addr_type == ADDR_WINDOWS
2963 ? LAST_WIN_NR : LAST_TAB_NR;
2964 }
2965 else
2966 {
2967 // there is no Vim command which uses '%' and
2968 // ADDR_WINDOWS or ADDR_TABS
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002969 *errormsg = _(e_invrange);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002970 return FAIL;
2971 }
2972 break;
2973 case ADDR_TABS_RELATIVE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02002974 case ADDR_UNSIGNED:
2975 case ADDR_QUICKFIX:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002976 *errormsg = _(e_invrange);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002977 return FAIL;
2978 case ADDR_ARGUMENTS:
2979 if (ARGCOUNT == 0)
2980 eap->line1 = eap->line2 = 0;
2981 else
2982 {
2983 eap->line1 = 1;
2984 eap->line2 = ARGCOUNT;
2985 }
2986 break;
Bram Moolenaar25190db2019-05-04 15:05:28 +02002987 case ADDR_QUICKFIX_VALID:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02002988#ifdef FEAT_QUICKFIX
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002989 eap->line1 = 1;
Bram Moolenaar25190db2019-05-04 15:05:28 +02002990 eap->line2 = qf_get_valid_size(eap);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002991 if (eap->line2 == 0)
2992 eap->line2 = 1;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002993#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02002994 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02002995 case ADDR_NONE:
2996 // Will give an error later if a range is found.
2997 break;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002998 }
2999 ++eap->addr_count;
3000 }
3001 else if (*eap->cmd == '*' && vim_strchr(p_cpo, CPO_STAR) == NULL)
3002 {
3003 pos_T *fp;
3004
3005 // '*' - visual area
3006 if (eap->addr_type != ADDR_LINES)
3007 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003008 *errormsg = _(e_invrange);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003009 return FAIL;
3010 }
3011
3012 ++eap->cmd;
3013 if (!eap->skip)
3014 {
3015 fp = getmark('<', FALSE);
3016 if (check_mark(fp) == FAIL)
3017 return FAIL;
3018 eap->line1 = fp->lnum;
3019 fp = getmark('>', FALSE);
3020 if (check_mark(fp) == FAIL)
3021 return FAIL;
3022 eap->line2 = fp->lnum;
3023 ++eap->addr_count;
3024 }
3025 }
3026 }
3027 else
3028 eap->line2 = lnum;
3029 eap->addr_count++;
3030
3031 if (*eap->cmd == ';')
3032 {
3033 if (!eap->skip)
3034 {
3035 curwin->w_cursor.lnum = eap->line2;
3036 // don't leave the cursor on an illegal line or column
3037 check_cursor();
3038 }
3039 }
3040 else if (*eap->cmd != ',')
3041 break;
3042 ++eap->cmd;
3043 }
3044
3045 // One address given: set start and end lines.
3046 if (eap->addr_count == 1)
3047 {
3048 eap->line1 = eap->line2;
3049 // ... but only implicit: really no address given
3050 if (lnum == MAXLNUM)
3051 eap->addr_count = 0;
3052 }
3053 return OK;
3054}
3055
3056/*
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00003057 * Check for an Ex command with optional tail.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003058 * If there is a match advance "pp" to the argument and return TRUE.
3059 */
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00003060 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003061checkforcmd(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003062 char_u **pp, // start of command
3063 char *cmd, // name of command
3064 int len) // required length
Bram Moolenaar071d4272004-06-13 20:20:40 +00003065{
3066 int i;
3067
3068 for (i = 0; cmd[i] != NUL; ++i)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003069 if (((char_u *)cmd)[i] != (*pp)[i])
Bram Moolenaar071d4272004-06-13 20:20:40 +00003070 break;
3071 if (i >= len && !isalpha((*pp)[i]))
3072 {
3073 *pp = skipwhite(*pp + i);
3074 return TRUE;
3075 }
3076 return FALSE;
3077}
3078
3079/*
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003080 * Append "cmd" to the error message in IObuff.
3081 * Takes care of limiting the length and handling 0xa0, which would be
3082 * invisible otherwise.
3083 */
3084 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003085append_command(char_u *cmd)
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003086{
3087 char_u *s = cmd;
3088 char_u *d;
3089
3090 STRCAT(IObuff, ": ");
3091 d = IObuff + STRLEN(IObuff);
3092 while (*s != NUL && d - IObuff < IOSIZE - 7)
3093 {
Bram Moolenaar13505972019-01-24 15:04:48 +01003094 if (enc_utf8 ? (s[0] == 0xc2 && s[1] == 0xa0) : *s == 0xa0)
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003095 {
Bram Moolenaar13505972019-01-24 15:04:48 +01003096 s += enc_utf8 ? 2 : 1;
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003097 STRCPY(d, "<a0>");
3098 d += 4;
3099 }
3100 else
3101 MB_COPY_CHAR(s, d);
3102 }
3103 *d = NUL;
3104}
3105
3106/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003107 * Find an Ex command by its name, either built-in or user.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003108 * Start of the name can be found at eap->cmd.
Bram Moolenaar25190db2019-05-04 15:05:28 +02003109 * Sets eap->cmdidx and returns a pointer to char after the command name.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003110 * "full" is set to TRUE if the whole command name matched.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003111 * Returns NULL for an ambiguous user command.
3112 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003113 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003114find_command(exarg_T *eap, int *full UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003115{
3116 int len;
3117 char_u *p;
Bram Moolenaardf177f62005-02-22 08:39:57 +00003118 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003119
3120 /*
3121 * Isolate the command and search for it in the command table.
Bram Moolenaar81870892007-11-11 18:17:28 +00003122 * Exceptions:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003123 * - the 'k' command can directly be followed by any character.
3124 * - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003125 * but :sre[wind] is another command, as are :scr[iptnames],
Bram Moolenaar071d4272004-06-13 20:20:40 +00003126 * :scs[cope], :sim[alt], :sig[ns] and :sil[ent].
Bram Moolenaardf177f62005-02-22 08:39:57 +00003127 * - the "d" command can directly be followed by 'l' or 'p' flag.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003128 */
3129 p = eap->cmd;
3130 if (*p == 'k')
3131 {
3132 eap->cmdidx = CMD_k;
3133 ++p;
3134 }
3135 else if (p[0] == 's'
Bram Moolenaar204b93f2015-08-04 22:02:51 +02003136 && ((p[1] == 'c' && (p[2] == NUL || (p[2] != 's' && p[2] != 'r'
3137 && (p[3] == NUL || (p[3] != 'i' && p[4] != 'p')))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003138 || p[1] == 'g'
3139 || (p[1] == 'i' && p[2] != 'm' && p[2] != 'l' && p[2] != 'g')
3140 || p[1] == 'I'
3141 || (p[1] == 'r' && p[2] != 'e')))
3142 {
3143 eap->cmdidx = CMD_substitute;
3144 ++p;
3145 }
3146 else
3147 {
3148 while (ASCII_ISALPHA(*p))
3149 ++p;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003150 // for python 3.x support ":py3", ":python3", ":py3file", etc.
Bram Moolenaar55d5c032010-07-17 23:52:29 +02003151 if (eap->cmd[0] == 'p' && eap->cmd[1] == 'y')
Bram Moolenaarb6590522010-07-21 16:00:43 +02003152 while (ASCII_ISALNUM(*p))
3153 ++p;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02003154
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003155 // check for non-alpha command
Bram Moolenaar071d4272004-06-13 20:20:40 +00003156 if (p == eap->cmd && vim_strchr((char_u *)"@*!=><&~#", *p) != NULL)
3157 ++p;
3158 len = (int)(p - eap->cmd);
Bram Moolenaardf177f62005-02-22 08:39:57 +00003159 if (*eap->cmd == 'd' && (p[-1] == 'l' || p[-1] == 'p'))
3160 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003161 // Check for ":dl", ":dell", etc. to ":deletel": that's
3162 // :delete with the 'l' flag. Same for 'p'.
Bram Moolenaardf177f62005-02-22 08:39:57 +00003163 for (i = 0; i < len; ++i)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003164 if (eap->cmd[i] != ((char_u *)"delete")[i])
Bram Moolenaardf177f62005-02-22 08:39:57 +00003165 break;
3166 if (i == len - 1)
3167 {
3168 --len;
3169 if (p[-1] == 'l')
3170 eap->flags |= EXFLAG_LIST;
3171 else
3172 eap->flags |= EXFLAG_PRINT;
3173 }
3174 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003175
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003176 if (ASCII_ISLOWER(eap->cmd[0]))
3177 {
Bram Moolenaar6c0c1e82017-03-25 15:07:43 +01003178 int c1 = eap->cmd[0];
Bram Moolenaar94f82cb2019-07-24 22:30:27 +02003179 int c2 = len == 1 ? NUL : eap->cmd[1];
Bram Moolenaar6c0c1e82017-03-25 15:07:43 +01003180
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003181 if (command_count != (int)CMD_SIZE)
3182 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003183 iemsg(_("E943: Command table needs to be updated, run 'make cmdidxs'"));
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003184 getout(1);
3185 }
3186
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003187 // Use a precomputed index for fast look-up in cmdnames[]
3188 // taking into account the first 2 letters of eap->cmd.
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003189 eap->cmdidx = cmdidxs1[CharOrdLow(c1)];
3190 if (ASCII_ISLOWER(c2))
3191 eap->cmdidx += cmdidxs2[CharOrdLow(c1)][CharOrdLow(c2)];
3192 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003193 else
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003194 eap->cmdidx = CMD_bang;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003195
3196 for ( ; (int)eap->cmdidx < (int)CMD_SIZE;
3197 eap->cmdidx = (cmdidx_T)((int)eap->cmdidx + 1))
3198 if (STRNCMP(cmdnames[(int)eap->cmdidx].cmd_name, (char *)eap->cmd,
3199 (size_t)len) == 0)
3200 {
3201#ifdef FEAT_EVAL
3202 if (full != NULL
3203 && cmdnames[(int)eap->cmdidx].cmd_name[len] == NUL)
3204 *full = TRUE;
3205#endif
3206 break;
3207 }
3208
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003209 // Look for a user defined command as a last resort. Let ":Print" be
3210 // overruled by a user defined command.
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01003211 if ((eap->cmdidx == CMD_SIZE || eap->cmdidx == CMD_Print)
3212 && *eap->cmd >= 'A' && *eap->cmd <= 'Z')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003213 {
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003214 // User defined commands may contain digits.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003215 while (ASCII_ISALNUM(*p))
3216 ++p;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003217 p = find_ucmd(eap, p, full, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003218 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003219 if (p == eap->cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003220 eap->cmdidx = CMD_SIZE;
3221 }
3222
3223 return p;
3224}
3225
3226#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003227static struct cmdmod
3228{
3229 char *name;
3230 int minlen;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003231 int has_count; // :123verbose :3tab
Bram Moolenaared53fb92007-11-24 20:50:24 +00003232} cmdmods[] = {
3233 {"aboveleft", 3, FALSE},
3234 {"belowright", 3, FALSE},
3235 {"botright", 2, FALSE},
3236 {"browse", 3, FALSE},
3237 {"confirm", 4, FALSE},
Bram Moolenaar7b668e82016-08-23 23:51:21 +02003238 {"filter", 4, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003239 {"hide", 3, FALSE},
3240 {"keepalt", 5, FALSE},
3241 {"keepjumps", 5, FALSE},
3242 {"keepmarks", 3, FALSE},
Bram Moolenaara939e432013-11-09 05:30:26 +01003243 {"keeppatterns", 5, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003244 {"leftabove", 5, FALSE},
3245 {"lockmarks", 3, FALSE},
Bram Moolenaarca9f9582008-09-18 10:44:28 +00003246 {"noautocmd", 3, FALSE},
Bram Moolenaar5803ae62014-03-23 16:04:02 +01003247 {"noswapfile", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003248 {"rightbelow", 6, FALSE},
3249 {"sandbox", 3, FALSE},
3250 {"silent", 3, FALSE},
3251 {"tab", 3, TRUE},
3252 {"topleft", 2, FALSE},
Bram Moolenaar8e258a42009-07-09 13:55:43 +00003253 {"unsilent", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003254 {"verbose", 4, TRUE},
3255 {"vertical", 4, FALSE},
3256};
3257
3258/*
3259 * Return length of a command modifier (including optional count).
3260 * Return zero when it's not a modifier.
3261 */
3262 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003263modifier_len(char_u *cmd)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003264{
3265 int i, j;
3266 char_u *p = cmd;
3267
3268 if (VIM_ISDIGIT(*cmd))
3269 p = skipwhite(skipdigits(cmd));
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00003270 for (i = 0; i < (int)(sizeof(cmdmods) / sizeof(struct cmdmod)); ++i)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003271 {
3272 for (j = 0; p[j] != NUL; ++j)
3273 if (p[j] != cmdmods[i].name[j])
3274 break;
Bram Moolenaar2d473ab2013-06-12 17:12:24 +02003275 if (!ASCII_ISALPHA(p[j]) && j >= cmdmods[i].minlen
Bram Moolenaared53fb92007-11-24 20:50:24 +00003276 && (p == cmd || cmdmods[i].has_count))
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00003277 return j + (int)(p - cmd);
Bram Moolenaared53fb92007-11-24 20:50:24 +00003278 }
3279 return 0;
3280}
3281
Bram Moolenaar071d4272004-06-13 20:20:40 +00003282/*
3283 * Return > 0 if an Ex command "name" exists.
3284 * Return 2 if there is an exact match.
3285 * Return 3 if there is an ambiguous match.
3286 */
3287 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003288cmd_exists(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003289{
3290 exarg_T ea;
3291 int full = FALSE;
3292 int i;
3293 int j;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003294 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003295
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003296 // Check command modifiers.
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00003297 for (i = 0; i < (int)(sizeof(cmdmods) / sizeof(struct cmdmod)); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003298 {
3299 for (j = 0; name[j] != NUL; ++j)
3300 if (name[j] != cmdmods[i].name[j])
3301 break;
3302 if (name[j] == NUL && j >= cmdmods[i].minlen)
3303 return (cmdmods[i].name[j] == NUL ? 2 : 1);
3304 }
3305
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003306 // Check built-in commands and user defined commands.
3307 // For ":2match" and ":3match" we need to skip the number.
Bram Moolenaara9587612006-05-04 21:47:50 +00003308 ea.cmd = (*name == '2' || *name == '3') ? name + 1 : name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003309 ea.cmdidx = (cmdidx_T)0;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003310 p = find_command(&ea, &full);
3311 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003312 return 3;
Bram Moolenaara9587612006-05-04 21:47:50 +00003313 if (vim_isdigit(*name) && ea.cmdidx != CMD_match)
3314 return 0;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003315 if (*skipwhite(p) != NUL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003316 return 0; // trailing garbage
Bram Moolenaar071d4272004-06-13 20:20:40 +00003317 return (ea.cmdidx == CMD_SIZE ? 0 : (full ? 2 : 1));
3318}
3319#endif
3320
Bram Moolenaard0190392019-08-23 21:17:35 +02003321 cmdidx_T
3322excmd_get_cmdidx(char_u *cmd, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003323{
Bram Moolenaard0190392019-08-23 21:17:35 +02003324 cmdidx_T idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003325
Bram Moolenaard0190392019-08-23 21:17:35 +02003326 for (idx = (cmdidx_T)0; (int)idx < (int)CMD_SIZE;
3327 idx = (cmdidx_T)((int)idx + 1))
3328 if (STRNCMP(cmdnames[(int)idx].cmd_name, cmd, (size_t)len) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003329 break;
3330
Bram Moolenaard0190392019-08-23 21:17:35 +02003331 return idx;
3332}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003333
Bram Moolenaard0190392019-08-23 21:17:35 +02003334 long
3335excmd_get_argt(cmdidx_T idx)
3336{
3337 return (long)cmdnames[(int)idx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003338}
3339
3340/*
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003341 * Skip a range specifier of the form: addr [,addr] [;addr] ..
Bram Moolenaar071d4272004-06-13 20:20:40 +00003342 *
3343 * Backslashed delimiters after / or ? will be skipped, and commands will
3344 * not be expanded between /'s and ?'s or after "'".
3345 *
Bram Moolenaardf177f62005-02-22 08:39:57 +00003346 * Also skip white space and ":" characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003347 * Returns the "cmd" pointer advanced to beyond the range.
3348 */
3349 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003350skip_range(
3351 char_u *cmd,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003352 int *ctx) // pointer to xp_context or NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003353{
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003354 unsigned delim;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003355
Bram Moolenaarcbf20fb2017-02-03 21:19:04 +01003356 while (vim_strchr((char_u *)" \t0123456789.$%'/?-+,;\\", *cmd) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003357 {
Bram Moolenaarcbf20fb2017-02-03 21:19:04 +01003358 if (*cmd == '\\')
3359 {
3360 if (cmd[1] == '?' || cmd[1] == '/' || cmd[1] == '&')
3361 ++cmd;
3362 else
3363 break;
3364 }
3365 else if (*cmd == '\'')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003366 {
3367 if (*++cmd == NUL && ctx != NULL)
3368 *ctx = EXPAND_NOTHING;
3369 }
3370 else if (*cmd == '/' || *cmd == '?')
3371 {
3372 delim = *cmd++;
3373 while (*cmd != NUL && *cmd != delim)
3374 if (*cmd++ == '\\' && *cmd != NUL)
3375 ++cmd;
3376 if (*cmd == NUL && ctx != NULL)
3377 *ctx = EXPAND_NOTHING;
3378 }
3379 if (*cmd != NUL)
3380 ++cmd;
3381 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00003382
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003383 // Skip ":" and white space.
Bram Moolenaardf177f62005-02-22 08:39:57 +00003384 while (*cmd == ':')
3385 cmd = skipwhite(cmd + 1);
3386
Bram Moolenaar071d4272004-06-13 20:20:40 +00003387 return cmd;
3388}
3389
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003390 static void
3391addr_error(cmd_addr_T addr_type)
3392{
3393 if (addr_type == ADDR_NONE)
3394 emsg(_(e_norange));
3395 else
3396 emsg(_(e_invrange));
3397}
3398
Bram Moolenaar071d4272004-06-13 20:20:40 +00003399/*
Bram Moolenaar198cb662018-09-06 21:44:17 +02003400 * Get a single EX address.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003401 *
3402 * Set ptr to the next character after the part that was interpreted.
3403 * Set ptr to NULL when an error is encountered.
Bram Moolenaar198cb662018-09-06 21:44:17 +02003404 * This may set the last used search pattern.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003405 *
3406 * Return MAXLNUM when no Ex address was found.
3407 */
3408 static linenr_T
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003409get_address(
3410 exarg_T *eap UNUSED,
3411 char_u **ptr,
Bram Moolenaar1b03a192019-12-08 17:08:29 +01003412 cmd_addr_T addr_type,
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02003413 int skip, // only skip the address, don't use it
3414 int silent, // no errors or side effects
3415 int to_other_file, // flag: may jump to other file
3416 int address_count UNUSED) // 1 for first address, >1 after comma
Bram Moolenaar071d4272004-06-13 20:20:40 +00003417{
3418 int c;
3419 int i;
3420 long n;
3421 char_u *cmd;
3422 pos_T pos;
3423 pos_T *fp;
3424 linenr_T lnum;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003425 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003426
3427 cmd = skipwhite(*ptr);
3428 lnum = MAXLNUM;
3429 do
3430 {
3431 switch (*cmd)
3432 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003433 case '.': // '.' - Cursor position
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003434 ++cmd;
3435 switch (addr_type)
3436 {
3437 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02003438 case ADDR_OTHER:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003439 lnum = curwin->w_cursor.lnum;
3440 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003441 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01003442 lnum = CURRENT_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003443 break;
3444 case ADDR_ARGUMENTS:
3445 lnum = curwin->w_arg_idx + 1;
3446 break;
3447 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003448 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003449 lnum = curbuf->b_fnum;
3450 break;
3451 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01003452 lnum = CURRENT_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003453 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02003454 case ADDR_NONE:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003455 case ADDR_TABS_RELATIVE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02003456 case ADDR_UNSIGNED:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003457 addr_error(addr_type);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01003458 cmd = NULL;
3459 goto error;
3460 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003461 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003462#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02003463 lnum = qf_get_cur_idx(eap);
3464#endif
3465 break;
3466 case ADDR_QUICKFIX_VALID:
3467#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003468 lnum = qf_get_cur_valid_idx(eap);
Bram Moolenaare906c502015-09-09 21:10:39 +02003469#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003470 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003471 }
3472 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003473
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003474 case '$': // '$' - last line
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003475 ++cmd;
3476 switch (addr_type)
3477 {
3478 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02003479 case ADDR_OTHER:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003480 lnum = curbuf->b_ml.ml_line_count;
3481 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003482 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01003483 lnum = LAST_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003484 break;
3485 case ADDR_ARGUMENTS:
3486 lnum = ARGCOUNT;
3487 break;
3488 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003489 buf = lastbuf;
3490 while (buf->b_ml.ml_mfp == NULL)
3491 {
3492 if (buf->b_prev == NULL)
3493 break;
3494 buf = buf->b_prev;
3495 }
3496 lnum = buf->b_fnum;
3497 break;
3498 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003499 lnum = lastbuf->b_fnum;
3500 break;
3501 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01003502 lnum = LAST_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003503 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02003504 case ADDR_NONE:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003505 case ADDR_TABS_RELATIVE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02003506 case ADDR_UNSIGNED:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003507 addr_error(addr_type);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01003508 cmd = NULL;
3509 goto error;
3510 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003511 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003512#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003513 lnum = qf_get_size(eap);
3514 if (lnum == 0)
3515 lnum = 1;
Bram Moolenaare906c502015-09-09 21:10:39 +02003516#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003517 break;
Bram Moolenaar25190db2019-05-04 15:05:28 +02003518 case ADDR_QUICKFIX_VALID:
3519#ifdef FEAT_QUICKFIX
3520 lnum = qf_get_valid_size(eap);
3521 if (lnum == 0)
3522 lnum = 1;
3523#endif
3524 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003525 }
3526 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003527
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003528 case '\'': // ''' - mark
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003529 if (*++cmd == NUL)
3530 {
3531 cmd = NULL;
3532 goto error;
3533 }
3534 if (addr_type != ADDR_LINES)
3535 {
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003536 addr_error(addr_type);
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01003537 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003538 goto error;
3539 }
3540 if (skip)
3541 ++cmd;
3542 else
3543 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003544 // Only accept a mark in another file when it is
3545 // used by itself: ":'M".
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003546 fp = getmark(*cmd, to_other_file && cmd[1] == NUL);
3547 ++cmd;
3548 if (fp == (pos_T *)-1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003549 // Jumped to another file.
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003550 lnum = curwin->w_cursor.lnum;
3551 else
3552 {
3553 if (check_mark(fp) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003554 {
3555 cmd = NULL;
3556 goto error;
3557 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003558 lnum = fp->lnum;
3559 }
3560 }
3561 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003562
3563 case '/':
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003564 case '?': // '/' or '?' - search
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003565 c = *cmd++;
3566 if (addr_type != ADDR_LINES)
3567 {
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003568 addr_error(addr_type);
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01003569 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003570 goto error;
3571 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003572 if (skip) // skip "/pat/"
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003573 {
3574 cmd = skip_regexp(cmd, c, (int)p_magic, NULL);
3575 if (*cmd == c)
3576 ++cmd;
3577 }
3578 else
3579 {
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02003580 int flags;
3581
3582 pos = curwin->w_cursor; // save curwin->w_cursor
3583
3584 // When '/' or '?' follows another address, start from
3585 // there.
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003586 if (lnum != MAXLNUM)
3587 curwin->w_cursor.lnum = lnum;
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02003588
3589 // Start a forward search at the end of the line (unless
3590 // before the first line).
3591 // Start a backward search at the start of the line.
3592 // This makes sure we never match in the current
3593 // line, and can match anywhere in the
3594 // next/previous line.
Bram Moolenaar8ada6aa2017-12-19 21:23:21 +01003595 if (c == '/' && curwin->w_cursor.lnum > 0)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003596 curwin->w_cursor.col = MAXCOL;
3597 else
3598 curwin->w_cursor.col = 0;
3599 searchcmdlen = 0;
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02003600 flags = silent ? 0 : SEARCH_HIS | SEARCH_MSG;
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02003601 if (!do_search(NULL, c, cmd, 1L, flags, NULL))
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003602 {
3603 curwin->w_cursor = pos;
3604 cmd = NULL;
3605 goto error;
3606 }
3607 lnum = curwin->w_cursor.lnum;
3608 curwin->w_cursor = pos;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003609 // adjust command string pointer
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003610 cmd += searchcmdlen;
3611 }
3612 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003613
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003614 case '\\': // "\?", "\/" or "\&", repeat search
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003615 ++cmd;
3616 if (addr_type != ADDR_LINES)
3617 {
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003618 addr_error(addr_type);
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01003619 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003620 goto error;
3621 }
3622 if (*cmd == '&')
3623 i = RE_SUBST;
3624 else if (*cmd == '?' || *cmd == '/')
3625 i = RE_SEARCH;
3626 else
3627 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003628 emsg(_(e_backslash));
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003629 cmd = NULL;
3630 goto error;
3631 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003632
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003633 if (!skip)
3634 {
3635 /*
3636 * When search follows another address, start from
3637 * there.
3638 */
3639 if (lnum != MAXLNUM)
3640 pos.lnum = lnum;
3641 else
3642 pos.lnum = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003643
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003644 /*
3645 * Start the search just like for the above
3646 * do_search().
3647 */
3648 if (*cmd != '?')
3649 pos.col = MAXCOL;
3650 else
3651 pos.col = 0;
Bram Moolenaarbd8539a2015-08-11 18:53:03 +02003652 pos.coladd = 0;
Bram Moolenaar5d24a222018-12-23 19:10:09 +01003653 if (searchit(curwin, curbuf, &pos, NULL,
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003654 *cmd == '?' ? BACKWARD : FORWARD,
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02003655 (char_u *)"", 1L, SEARCH_MSG, i, NULL) != FAIL)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003656 lnum = pos.lnum;
3657 else
3658 {
3659 cmd = NULL;
3660 goto error;
3661 }
3662 }
3663 ++cmd;
3664 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003665
3666 default:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003667 if (VIM_ISDIGIT(*cmd)) // absolute line number
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003668 lnum = getdigits(&cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003669 }
3670
3671 for (;;)
3672 {
3673 cmd = skipwhite(cmd);
3674 if (*cmd != '-' && *cmd != '+' && !VIM_ISDIGIT(*cmd))
3675 break;
3676
3677 if (lnum == MAXLNUM)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003678 {
3679 switch (addr_type)
3680 {
3681 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02003682 case ADDR_OTHER:
3683 // "+1" is same as ".+1"
Bram Moolenaarf240e182014-11-27 18:33:02 +01003684 lnum = curwin->w_cursor.lnum;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003685 break;
3686 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01003687 lnum = CURRENT_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003688 break;
3689 case ADDR_ARGUMENTS:
3690 lnum = curwin->w_arg_idx + 1;
3691 break;
3692 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003693 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003694 lnum = curbuf->b_fnum;
3695 break;
3696 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01003697 lnum = CURRENT_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003698 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01003699 case ADDR_TABS_RELATIVE:
3700 lnum = 1;
3701 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003702 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003703#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02003704 lnum = qf_get_cur_idx(eap);
3705#endif
3706 break;
3707 case ADDR_QUICKFIX_VALID:
3708#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003709 lnum = qf_get_cur_valid_idx(eap);
Bram Moolenaare906c502015-09-09 21:10:39 +02003710#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003711 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02003712 case ADDR_NONE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02003713 case ADDR_UNSIGNED:
3714 lnum = 0;
Bram Moolenaarb7316892019-05-01 18:08:42 +02003715 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003716 }
3717 }
3718
Bram Moolenaar071d4272004-06-13 20:20:40 +00003719 if (VIM_ISDIGIT(*cmd))
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003720 i = '+'; // "number" is same as "+number"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003721 else
3722 i = *cmd++;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003723 if (!VIM_ISDIGIT(*cmd)) // '+' is '+1', but '+0' is not '+1'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003724 n = 1;
3725 else
3726 n = getdigits(&cmd);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01003727
3728 if (addr_type == ADDR_TABS_RELATIVE)
3729 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003730 emsg(_(e_invrange));
Bram Moolenaar2f72c702017-01-29 14:48:10 +01003731 cmd = NULL;
3732 goto error;
3733 }
3734 else if (addr_type == ADDR_LOADED_BUFFERS
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003735 || addr_type == ADDR_BUFFERS)
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01003736 lnum = compute_buffer_local_count(
3737 addr_type, lnum, (i == '-') ? -1 * n : n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003738 else
Bram Moolenaarded27822017-01-02 14:27:34 +01003739 {
3740#ifdef FEAT_FOLDING
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003741 // Relative line addressing, need to adjust for folded lines
3742 // now, but only do it after the first address.
Bram Moolenaarded27822017-01-02 14:27:34 +01003743 if (addr_type == ADDR_LINES && (i == '-' || i == '+')
3744 && address_count >= 2)
3745 (void)hasFolding(lnum, NULL, &lnum);
3746#endif
3747 if (i == '-')
3748 lnum -= n;
3749 else
3750 lnum += n;
3751 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003752 }
3753 } while (*cmd == '/' || *cmd == '?');
3754
3755error:
3756 *ptr = cmd;
3757 return lnum;
3758}
3759
3760/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00003761 * Get flags from an Ex command argument.
3762 */
3763 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003764get_flags(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00003765{
3766 while (vim_strchr((char_u *)"lp#", *eap->arg) != NULL)
3767 {
3768 if (*eap->arg == 'l')
3769 eap->flags |= EXFLAG_LIST;
3770 else if (*eap->arg == 'p')
3771 eap->flags |= EXFLAG_PRINT;
3772 else
3773 eap->flags |= EXFLAG_NR;
3774 eap->arg = skipwhite(eap->arg + 1);
3775 }
3776}
3777
3778/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003779 * Function called for command which is Not Implemented. NI!
3780 */
3781 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003782ex_ni(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003783{
3784 if (!eap->skip)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003785 eap->errmsg = N_("E319: Sorry, the command is not available in this version");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003786}
3787
Bram Moolenaar7bb75552007-07-16 18:39:49 +00003788#ifdef HAVE_EX_SCRIPT_NI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003789/*
3790 * Function called for script command which is Not Implemented. NI!
3791 * Skips over ":perl <<EOF" constructs.
3792 */
3793 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003794ex_script_ni(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003795{
3796 if (!eap->skip)
3797 ex_ni(eap);
3798 else
3799 vim_free(script_get(eap, eap->arg));
3800}
3801#endif
3802
3803/*
3804 * Check range in Ex command for validity.
3805 * Return NULL when valid, error message when invalid.
3806 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003807 static char *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003808invalid_range(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003809{
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003810 buf_T *buf;
Bram Moolenaar25190db2019-05-04 15:05:28 +02003811
Bram Moolenaar071d4272004-06-13 20:20:40 +00003812 if ( eap->line1 < 0
3813 || eap->line2 < 0
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003814 || eap->line1 > eap->line2)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003815 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003816
Bram Moolenaar8071cb22019-07-12 17:58:01 +02003817 if (eap->argt & EX_RANGE)
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003818 {
Bram Moolenaarb7316892019-05-01 18:08:42 +02003819 switch (eap->addr_type)
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003820 {
3821 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02003822 if (eap->line2 > curbuf->b_ml.ml_line_count
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003823#ifdef FEAT_DIFF
3824 + (eap->cmdidx == CMD_diffget)
3825#endif
3826 )
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003827 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003828 break;
3829 case ADDR_ARGUMENTS:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003830 // add 1 if ARGCOUNT is 0
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01003831 if (eap->line2 > ARGCOUNT + (!ARGCOUNT))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003832 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003833 break;
3834 case ADDR_BUFFERS:
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02003835 // Only a boundary check, not whether the buffers actually
3836 // exist.
3837 if (eap->line1 < 1 || eap->line2 > get_highest_fnum())
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003838 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003839 break;
3840 case ADDR_LOADED_BUFFERS:
3841 buf = firstbuf;
3842 while (buf->b_ml.ml_mfp == NULL)
3843 {
3844 if (buf->b_next == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003845 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003846 buf = buf->b_next;
3847 }
3848 if (eap->line1 < buf->b_fnum)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003849 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003850 buf = lastbuf;
3851 while (buf->b_ml.ml_mfp == NULL)
3852 {
3853 if (buf->b_prev == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003854 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003855 buf = buf->b_prev;
3856 }
3857 if (eap->line2 > buf->b_fnum)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003858 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003859 break;
3860 case ADDR_WINDOWS:
Bram Moolenaar8be63882015-01-14 11:25:05 +01003861 if (eap->line2 > LAST_WIN_NR)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003862 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003863 break;
3864 case ADDR_TABS:
3865 if (eap->line2 > LAST_TAB_NR)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003866 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003867 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01003868 case ADDR_TABS_RELATIVE:
Bram Moolenaarb7316892019-05-01 18:08:42 +02003869 case ADDR_OTHER:
3870 // Any range is OK.
Bram Moolenaar2f72c702017-01-29 14:48:10 +01003871 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003872 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003873#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02003874 // No error for value that is too big, will use the last entry.
3875 if (eap->line2 <= 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003876 return _(e_invrange);
Bram Moolenaare906c502015-09-09 21:10:39 +02003877#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003878 break;
Bram Moolenaar25190db2019-05-04 15:05:28 +02003879 case ADDR_QUICKFIX_VALID:
3880#ifdef FEAT_QUICKFIX
3881 if ((eap->line2 != 1 && eap->line2 > qf_get_valid_size(eap))
3882 || eap->line2 < 0)
3883 return _(e_invrange);
3884#endif
3885 break;
3886 case ADDR_UNSIGNED:
3887 if (eap->line2 < 0)
3888 return _(e_invrange);
3889 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02003890 case ADDR_NONE:
3891 // Will give an error elsewhere.
3892 break;
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003893 }
3894 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003895 return NULL;
3896}
3897
3898/*
3899 * Correct the range for zero line number, if required.
3900 */
3901 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003902correct_range(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003903{
Bram Moolenaar8071cb22019-07-12 17:58:01 +02003904 if (!(eap->argt & EX_ZEROR)) // zero in range not allowed
Bram Moolenaar071d4272004-06-13 20:20:40 +00003905 {
3906 if (eap->line1 == 0)
3907 eap->line1 = 1;
3908 if (eap->line2 == 0)
3909 eap->line2 = 1;
3910 }
3911}
3912
Bram Moolenaar748bf032005-02-02 23:04:36 +00003913#ifdef FEAT_QUICKFIX
Bram Moolenaar748bf032005-02-02 23:04:36 +00003914/*
3915 * For a ":vimgrep" or ":vimgrepadd" command return a pointer past the
3916 * pattern. Otherwise return eap->arg.
3917 */
3918 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003919skip_grep_pat(exarg_T *eap)
Bram Moolenaar748bf032005-02-02 23:04:36 +00003920{
3921 char_u *p = eap->arg;
3922
Bram Moolenaara37420f2006-02-04 22:37:47 +00003923 if (*p != NUL && (eap->cmdidx == CMD_vimgrep || eap->cmdidx == CMD_lvimgrep
3924 || eap->cmdidx == CMD_vimgrepadd
3925 || eap->cmdidx == CMD_lvimgrepadd
3926 || grep_internal(eap->cmdidx)))
Bram Moolenaar748bf032005-02-02 23:04:36 +00003927 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00003928 p = skip_vimgrep_pat(p, NULL, NULL);
Bram Moolenaar748bf032005-02-02 23:04:36 +00003929 if (p == NULL)
3930 p = eap->arg;
Bram Moolenaar748bf032005-02-02 23:04:36 +00003931 }
3932 return p;
3933}
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003934
3935/*
3936 * For the ":make" and ":grep" commands insert the 'makeprg'/'grepprg' option
3937 * in the command line, so that things like % get expanded.
3938 */
3939 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003940replace_makeprg(exarg_T *eap, char_u *p, char_u **cmdlinep)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003941{
3942 char_u *new_cmdline;
3943 char_u *program;
3944 char_u *pos;
3945 char_u *ptr;
3946 int len;
3947 int i;
3948
3949 /*
3950 * Don't do it when ":vimgrep" is used for ":grep".
3951 */
Bram Moolenaara37420f2006-02-04 22:37:47 +00003952 if ((eap->cmdidx == CMD_make || eap->cmdidx == CMD_lmake
3953 || eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
3954 || eap->cmdidx == CMD_grepadd
3955 || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003956 && !grep_internal(eap->cmdidx))
3957 {
Bram Moolenaara37420f2006-02-04 22:37:47 +00003958 if (eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
3959 || eap->cmdidx == CMD_grepadd || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003960 {
3961 if (*curbuf->b_p_gp == NUL)
3962 program = p_gp;
3963 else
3964 program = curbuf->b_p_gp;
3965 }
3966 else
3967 {
3968 if (*curbuf->b_p_mp == NUL)
3969 program = p_mp;
3970 else
3971 program = curbuf->b_p_mp;
3972 }
3973
3974 p = skipwhite(p);
3975
3976 if ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
3977 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003978 // replace $* by given arguments
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003979 i = 1;
3980 while ((pos = (char_u *)strstr((char *)pos + 2, "$*")) != NULL)
3981 ++i;
3982 len = (int)STRLEN(p);
Bram Moolenaar51e14382019-05-25 20:21:28 +02003983 new_cmdline = alloc(STRLEN(program) + i * (len - 2) + 1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003984 if (new_cmdline == NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003985 return NULL; // out of memory
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003986 ptr = new_cmdline;
3987 while ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
3988 {
3989 i = (int)(pos - program);
3990 STRNCPY(ptr, program, i);
3991 STRCPY(ptr += i, p);
3992 ptr += len;
3993 program = pos + 2;
3994 }
3995 STRCPY(ptr, program);
3996 }
3997 else
3998 {
Bram Moolenaar51e14382019-05-25 20:21:28 +02003999 new_cmdline = alloc(STRLEN(program) + STRLEN(p) + 2);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004000 if (new_cmdline == NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004001 return NULL; // out of memory
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004002 STRCPY(new_cmdline, program);
4003 STRCAT(new_cmdline, " ");
4004 STRCAT(new_cmdline, p);
4005 }
4006 msg_make(p);
4007
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004008 // 'eap->cmd' is not set here, because it is not used at CMD_make
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004009 vim_free(*cmdlinep);
4010 *cmdlinep = new_cmdline;
4011 p = new_cmdline;
4012 }
4013 return p;
4014}
Bram Moolenaar748bf032005-02-02 23:04:36 +00004015#endif
4016
Bram Moolenaar071d4272004-06-13 20:20:40 +00004017/*
4018 * Expand file name in Ex command argument.
Bram Moolenaar0abb4272019-06-15 16:06:00 +02004019 * When an error is detected, "errormsgp" is set to a non-NULL pointer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004020 * Return FAIL for failure, OK otherwise.
4021 */
4022 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004023expand_filename(
4024 exarg_T *eap,
4025 char_u **cmdlinep,
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004026 char **errormsgp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004027{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004028 int has_wildcards; // need to expand wildcards
Bram Moolenaar071d4272004-06-13 20:20:40 +00004029 char_u *repl;
4030 int srclen;
4031 char_u *p;
4032 int n;
Bram Moolenaar63b92542007-03-27 14:57:09 +00004033 int escaped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004034
Bram Moolenaar748bf032005-02-02 23:04:36 +00004035#ifdef FEAT_QUICKFIX
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004036 // Skip a regexp pattern for ":vimgrep[add] pat file..."
Bram Moolenaar748bf032005-02-02 23:04:36 +00004037 p = skip_grep_pat(eap);
4038#else
4039 p = eap->arg;
4040#endif
4041
Bram Moolenaar071d4272004-06-13 20:20:40 +00004042 /*
4043 * Decide to expand wildcards *before* replacing '%', '#', etc. If
4044 * the file name contains a wildcard it should not cause expanding.
4045 * (it will be expanded anyway if there is a wildcard before replacing).
4046 */
Bram Moolenaar748bf032005-02-02 23:04:36 +00004047 has_wildcards = mch_has_wildcard(p);
4048 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004049 {
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004050#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004051 // Skip over `=expr`, wildcards in it are not expanded.
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004052 if (p[0] == '`' && p[1] == '=')
4053 {
4054 p += 2;
4055 (void)skip_expr(&p);
4056 if (*p == '`')
4057 ++p;
4058 continue;
4059 }
4060#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004061 /*
4062 * Quick check if this cannot be the start of a special string.
4063 * Also removes backslash before '%', '#' and '<'.
4064 */
4065 if (vim_strchr((char_u *)"%#<", *p) == NULL)
4066 {
4067 ++p;
4068 continue;
4069 }
4070
4071 /*
4072 * Try to find a match at this position.
4073 */
Bram Moolenaar63b92542007-03-27 14:57:09 +00004074 repl = eval_vars(p, eap->arg, &srclen, &(eap->do_ecmd_lnum),
4075 errormsgp, &escaped);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004076 if (*errormsgp != NULL) // error detected
Bram Moolenaar071d4272004-06-13 20:20:40 +00004077 return FAIL;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004078 if (repl == NULL) // no match found
Bram Moolenaar071d4272004-06-13 20:20:40 +00004079 {
4080 p += srclen;
4081 continue;
4082 }
4083
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004084 // Wildcards won't be expanded below, the replacement is taken
4085 // literally. But do expand "~/file", "~user/file" and "$HOME/file".
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00004086 if (vim_strchr(repl, '$') != NULL || vim_strchr(repl, '~') != NULL)
4087 {
4088 char_u *l = repl;
4089
4090 repl = expand_env_save(repl);
4091 vim_free(l);
4092 }
4093
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004094 // Need to escape white space et al. with a backslash.
4095 // Don't do this for:
4096 // - replacement that already has been escaped: "##"
4097 // - shell commands (may have to use quotes instead).
4098 // - non-unix systems when there is a single argument (spaces don't
4099 // separate arguments then).
Bram Moolenaar071d4272004-06-13 20:20:40 +00004100 if (!eap->usefilter
Bram Moolenaar63b92542007-03-27 14:57:09 +00004101 && !escaped
Bram Moolenaar071d4272004-06-13 20:20:40 +00004102 && eap->cmdidx != CMD_bang
Bram Moolenaar071d4272004-06-13 20:20:40 +00004103 && eap->cmdidx != CMD_grep
4104 && eap->cmdidx != CMD_grepadd
Bram Moolenaarbf15b8d2017-06-04 20:43:48 +02004105 && eap->cmdidx != CMD_hardcopy
Bram Moolenaar67883b42017-07-27 22:57:00 +02004106 && eap->cmdidx != CMD_lgrep
4107 && eap->cmdidx != CMD_lgrepadd
4108 && eap->cmdidx != CMD_lmake
4109 && eap->cmdidx != CMD_make
4110 && eap->cmdidx != CMD_terminal
Bram Moolenaar071d4272004-06-13 20:20:40 +00004111#ifndef UNIX
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004112 && !(eap->argt & EX_NOSPC)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004113#endif
4114 )
4115 {
4116 char_u *l;
4117#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004118 // Don't escape a backslash here, because rem_backslash() doesn't
4119 // remove it later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004120 static char_u *nobslash = (char_u *)" \t\"|";
4121# define ESCAPE_CHARS nobslash
4122#else
4123# define ESCAPE_CHARS escape_chars
4124#endif
4125
4126 for (l = repl; *l; ++l)
4127 if (vim_strchr(ESCAPE_CHARS, *l) != NULL)
4128 {
4129 l = vim_strsave_escaped(repl, ESCAPE_CHARS);
4130 if (l != NULL)
4131 {
4132 vim_free(repl);
4133 repl = l;
4134 }
4135 break;
4136 }
4137 }
4138
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004139 // For a shell command a '!' must be escaped.
Bram Moolenaar67883b42017-07-27 22:57:00 +02004140 if ((eap->usefilter || eap->cmdidx == CMD_bang
4141 || eap->cmdidx == CMD_terminal)
Bram Moolenaar31b7d382014-04-01 18:54:48 +02004142 && vim_strpbrk(repl, (char_u *)"!") != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004143 {
4144 char_u *l;
4145
Bram Moolenaar31b7d382014-04-01 18:54:48 +02004146 l = vim_strsave_escaped(repl, (char_u *)"!");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004147 if (l != NULL)
4148 {
4149 vim_free(repl);
4150 repl = l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004151 }
4152 }
4153
4154 p = repl_cmdline(eap, p, srclen, repl, cmdlinep);
4155 vim_free(repl);
4156 if (p == NULL)
4157 return FAIL;
4158 }
4159
4160 /*
4161 * One file argument: Expand wildcards.
4162 * Don't do this with ":r !command" or ":w !command".
4163 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004164 if ((eap->argt & EX_NOSPC) && !eap->usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004165 {
4166 /*
4167 * May do this twice:
4168 * 1. Replace environment variables.
4169 * 2. Replace any other wildcards, remove backslashes.
4170 */
4171 for (n = 1; n <= 2; ++n)
4172 {
4173 if (n == 2)
4174 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004175 /*
4176 * Halve the number of backslashes (this is Vi compatible).
4177 * For Unix and OS/2, when wildcards are expanded, this is
4178 * done by ExpandOne() below.
4179 */
Bram Moolenaare7fedb62015-12-31 19:07:19 +01004180#if defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004181 if (!has_wildcards)
4182#endif
4183 backslash_halve(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004184 }
4185
4186 if (has_wildcards)
4187 {
4188 if (n == 1)
4189 {
4190 /*
4191 * First loop: May expand environment variables. This
4192 * can be done much faster with expand_env() than with
4193 * something else (e.g., calling a shell).
4194 * After expanding environment variables, check again
4195 * if there are still wildcards present.
4196 */
4197 if (vim_strchr(eap->arg, '$') != NULL
4198 || vim_strchr(eap->arg, '~') != NULL)
4199 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +00004200 expand_env_esc(eap->arg, NameBuff, MAXPATHL,
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00004201 TRUE, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004202 has_wildcards = mch_has_wildcard(NameBuff);
4203 p = NameBuff;
4204 }
4205 else
4206 p = NULL;
4207 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004208 else // n == 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00004209 {
4210 expand_T xpc;
Bram Moolenaarb40c2572019-10-19 21:01:05 +02004211 int options = WILD_LIST_NOTFOUND
4212 | WILD_NOERROR | WILD_ADD_SLASH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004213
4214 ExpandInit(&xpc);
4215 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar94950a92010-12-02 16:01:29 +01004216 if (p_wic)
4217 options += WILD_ICASE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004218 p = ExpandOne(&xpc, eap->arg, NULL,
Bram Moolenaar94950a92010-12-02 16:01:29 +01004219 options, WILD_EXPAND_FREE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004220 if (p == NULL)
4221 return FAIL;
4222 }
4223 if (p != NULL)
4224 {
4225 (void)repl_cmdline(eap, eap->arg, (int)STRLEN(eap->arg),
4226 p, cmdlinep);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004227 if (n == 2) // p came from ExpandOne()
Bram Moolenaar071d4272004-06-13 20:20:40 +00004228 vim_free(p);
4229 }
4230 }
4231 }
4232 }
4233 return OK;
4234}
4235
4236/*
4237 * Replace part of the command line, keeping eap->cmd, eap->arg and
4238 * eap->nextcmd correct.
4239 * "src" points to the part that is to be replaced, of length "srclen".
4240 * "repl" is the replacement string.
4241 * Returns a pointer to the character after the replaced string.
4242 * Returns NULL for failure.
4243 */
4244 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004245repl_cmdline(
4246 exarg_T *eap,
4247 char_u *src,
4248 int srclen,
4249 char_u *repl,
4250 char_u **cmdlinep)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004251{
4252 int len;
4253 int i;
4254 char_u *new_cmdline;
4255
4256 /*
4257 * The new command line is build in new_cmdline[].
4258 * First allocate it.
4259 * Careful: a "+cmd" argument may have been NUL terminated.
4260 */
4261 len = (int)STRLEN(repl);
4262 i = (int)(src - *cmdlinep) + (int)STRLEN(src + srclen) + len + 3;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004263 if (eap->nextcmd != NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004264 i += (int)STRLEN(eap->nextcmd);// add space for next command
Bram Moolenaar964b3742019-05-24 18:54:09 +02004265 if ((new_cmdline = alloc(i)) == NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004266 return NULL; // out of memory!
Bram Moolenaar071d4272004-06-13 20:20:40 +00004267
4268 /*
4269 * Copy the stuff before the expanded part.
4270 * Copy the expanded stuff.
4271 * Copy what came after the expanded part.
4272 * Copy the next commands, if there are any.
4273 */
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004274 i = (int)(src - *cmdlinep); // length of part before match
Bram Moolenaar071d4272004-06-13 20:20:40 +00004275 mch_memmove(new_cmdline, *cmdlinep, (size_t)i);
Bram Moolenaara3ffd9c2005-07-21 21:03:15 +00004276
Bram Moolenaar071d4272004-06-13 20:20:40 +00004277 mch_memmove(new_cmdline + i, repl, (size_t)len);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004278 i += len; // remember the end of the string
Bram Moolenaar071d4272004-06-13 20:20:40 +00004279 STRCPY(new_cmdline + i, src + srclen);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004280 src = new_cmdline + i; // remember where to continue
Bram Moolenaar071d4272004-06-13 20:20:40 +00004281
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004282 if (eap->nextcmd != NULL) // append next command
Bram Moolenaar071d4272004-06-13 20:20:40 +00004283 {
4284 i = (int)STRLEN(new_cmdline) + 1;
4285 STRCPY(new_cmdline + i, eap->nextcmd);
4286 eap->nextcmd = new_cmdline + i;
4287 }
4288 eap->cmd = new_cmdline + (eap->cmd - *cmdlinep);
4289 eap->arg = new_cmdline + (eap->arg - *cmdlinep);
4290 if (eap->do_ecmd_cmd != NULL && eap->do_ecmd_cmd != dollar_command)
4291 eap->do_ecmd_cmd = new_cmdline + (eap->do_ecmd_cmd - *cmdlinep);
4292 vim_free(*cmdlinep);
4293 *cmdlinep = new_cmdline;
4294
4295 return src;
4296}
4297
4298/*
4299 * Check for '|' to separate commands and '"' to start comments.
4300 */
4301 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004302separate_nextcmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004303{
4304 char_u *p;
4305
Bram Moolenaar86b68352004-12-27 21:59:20 +00004306#ifdef FEAT_QUICKFIX
Bram Moolenaar748bf032005-02-02 23:04:36 +00004307 p = skip_grep_pat(eap);
4308#else
4309 p = eap->arg;
Bram Moolenaar86b68352004-12-27 21:59:20 +00004310#endif
4311
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004312 for ( ; *p; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004313 {
4314 if (*p == Ctrl_V)
4315 {
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004316 if (eap->argt & (EX_CTRLV | EX_XFILE))
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004317 ++p; // skip CTRL-V and next char
Bram Moolenaar071d4272004-06-13 20:20:40 +00004318 else
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004319 // remove CTRL-V and skip next char
Bram Moolenaara7241f52008-06-24 20:39:31 +00004320 STRMOVE(p, p + 1);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004321 if (*p == NUL) // stop at NUL after CTRL-V
Bram Moolenaar071d4272004-06-13 20:20:40 +00004322 break;
4323 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004324
4325#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004326 // Skip over `=expr` when wildcards are expanded.
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004327 else if (p[0] == '`' && p[1] == '=' && (eap->argt & EX_XFILE))
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004328 {
4329 p += 2;
4330 (void)skip_expr(&p);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004331 }
4332#endif
4333
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004334 // Check for '"': start of comment or '|': next command
4335 // :@" and :*" do not start a comment!
4336 // :redir @" doesn't either.
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004337 else if ((*p == '"' && !(eap->argt & EX_NOTRLCOM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004338 && ((eap->cmdidx != CMD_at && eap->cmdidx != CMD_star)
4339 || p != eap->arg)
4340 && (eap->cmdidx != CMD_redir
4341 || p != eap->arg + 1 || p[-1] != '@'))
4342 || *p == '|' || *p == '\n')
4343 {
4344 /*
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004345 * We remove the '\' before the '|', unless EX_CTRLV is used
Bram Moolenaar071d4272004-06-13 20:20:40 +00004346 * AND 'b' is present in 'cpoptions'.
4347 */
4348 if ((vim_strchr(p_cpo, CPO_BAR) == NULL
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004349 || !(eap->argt & EX_CTRLV)) && *(p - 1) == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004350 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004351 STRMOVE(p - 1, p); // remove the '\'
Bram Moolenaar071d4272004-06-13 20:20:40 +00004352 --p;
4353 }
4354 else
4355 {
4356 eap->nextcmd = check_nextcmd(p);
4357 *p = NUL;
4358 break;
4359 }
4360 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004361 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00004362
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004363 if (!(eap->argt & EX_NOTRLCOM)) // remove trailing spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00004364 del_trailing_spaces(eap->arg);
4365}
4366
4367/*
4368 * get + command from ex argument
4369 */
4370 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004371getargcmd(char_u **argp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004372{
4373 char_u *arg = *argp;
4374 char_u *command = NULL;
4375
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004376 if (*arg == '+') // +[command]
Bram Moolenaar071d4272004-06-13 20:20:40 +00004377 {
4378 ++arg;
Bram Moolenaar3e451592014-04-02 14:22:05 +02004379 if (vim_isspace(*arg) || *arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004380 command = dollar_command;
4381 else
4382 {
4383 command = arg;
4384 arg = skip_cmd_arg(command, TRUE);
4385 if (*arg != NUL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004386 *arg++ = NUL; // terminate command with NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004387 }
4388
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004389 arg = skipwhite(arg); // skip over spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00004390 *argp = arg;
4391 }
4392 return command;
4393}
4394
4395/*
4396 * Find end of "+command" argument. Skip over "\ " and "\\".
4397 */
Bram Moolenaard0190392019-08-23 21:17:35 +02004398 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004399skip_cmd_arg(
4400 char_u *p,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004401 int rembs) // TRUE to halve the number of backslashes
Bram Moolenaar071d4272004-06-13 20:20:40 +00004402{
4403 while (*p && !vim_isspace(*p))
4404 {
4405 if (*p == '\\' && p[1] != NUL)
4406 {
4407 if (rembs)
Bram Moolenaara7241f52008-06-24 20:39:31 +00004408 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004409 else
4410 ++p;
4411 }
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004412 MB_PTR_ADV(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004413 }
4414 return p;
4415}
4416
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004417 int
4418get_bad_opt(char_u *p, exarg_T *eap)
4419{
4420 if (STRICMP(p, "keep") == 0)
4421 eap->bad_char = BAD_KEEP;
4422 else if (STRICMP(p, "drop") == 0)
4423 eap->bad_char = BAD_DROP;
4424 else if (MB_BYTE2LEN(*p) == 1 && p[1] == NUL)
4425 eap->bad_char = *p;
Bram Moolenaar75808492018-06-12 12:39:41 +02004426 else
4427 return FAIL;
4428 return OK;
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004429}
4430
Bram Moolenaar071d4272004-06-13 20:20:40 +00004431/*
4432 * Get "++opt=arg" argument.
4433 * Return FAIL or OK.
4434 */
4435 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004436getargopt(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004437{
4438 char_u *arg = eap->arg + 2;
4439 int *pp = NULL;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02004440 int bad_char_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004441 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004442
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004443 // ":edit ++[no]bin[ary] file"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004444 if (STRNCMP(arg, "bin", 3) == 0 || STRNCMP(arg, "nobin", 5) == 0)
4445 {
4446 if (*arg == 'n')
4447 {
4448 arg += 2;
4449 eap->force_bin = FORCE_NOBIN;
4450 }
4451 else
4452 eap->force_bin = FORCE_BIN;
4453 if (!checkforcmd(&arg, "binary", 3))
4454 return FAIL;
4455 eap->arg = skipwhite(arg);
4456 return OK;
4457 }
4458
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004459 // ":read ++edit file"
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004460 if (STRNCMP(arg, "edit", 4) == 0)
4461 {
4462 eap->read_edit = TRUE;
4463 eap->arg = skipwhite(arg + 4);
4464 return OK;
4465 }
4466
Bram Moolenaar071d4272004-06-13 20:20:40 +00004467 if (STRNCMP(arg, "ff", 2) == 0)
4468 {
4469 arg += 2;
4470 pp = &eap->force_ff;
4471 }
4472 else if (STRNCMP(arg, "fileformat", 10) == 0)
4473 {
4474 arg += 10;
4475 pp = &eap->force_ff;
4476 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004477 else if (STRNCMP(arg, "enc", 3) == 0)
4478 {
Bram Moolenaarb38e9ab2011-12-14 14:49:45 +01004479 if (STRNCMP(arg, "encoding", 8) == 0)
4480 arg += 8;
4481 else
4482 arg += 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004483 pp = &eap->force_enc;
4484 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004485 else if (STRNCMP(arg, "bad", 3) == 0)
4486 {
4487 arg += 3;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02004488 pp = &bad_char_idx;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004489 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004490
4491 if (pp == NULL || *arg != '=')
4492 return FAIL;
4493
4494 ++arg;
4495 *pp = (int)(arg - eap->cmd);
4496 arg = skip_cmd_arg(arg, FALSE);
4497 eap->arg = skipwhite(arg);
4498 *arg = NUL;
4499
Bram Moolenaar071d4272004-06-13 20:20:40 +00004500 if (pp == &eap->force_ff)
4501 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004502 if (check_ff_value(eap->cmd + eap->force_ff) == FAIL)
4503 return FAIL;
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004504 eap->force_ff = eap->cmd[eap->force_ff];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004505 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004506 else if (pp == &eap->force_enc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004507 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004508 // Make 'fileencoding' lower case.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004509 for (p = eap->cmd + eap->force_enc; *p != NUL; ++p)
4510 *p = TOLOWER_ASC(*p);
4511 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004512 else
4513 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004514 // Check ++bad= argument. Must be a single-byte character, "keep" or
4515 // "drop".
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004516 if (get_bad_opt(eap->cmd + bad_char_idx, eap) == FAIL)
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004517 return FAIL;
4518 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004519
4520 return OK;
4521}
4522
Bram Moolenaar071d4272004-06-13 20:20:40 +00004523 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004524ex_autocmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004525{
4526 /*
Bram Moolenaar26d4b892018-07-04 22:26:28 +02004527 * Disallow autocommands from .exrc and .vimrc in current
Bram Moolenaar071d4272004-06-13 20:20:40 +00004528 * directory for security reasons.
4529 */
4530 if (secure)
4531 {
4532 secure = 2;
4533 eap->errmsg = e_curdir;
4534 }
4535 else if (eap->cmdidx == CMD_autocmd)
4536 do_autocmd(eap->arg, eap->forceit);
4537 else
4538 do_augroup(eap->arg, eap->forceit);
4539}
4540
4541/*
4542 * ":doautocmd": Apply the automatic commands to the current buffer.
4543 */
4544 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004545ex_doautocmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004546{
Bram Moolenaar60542ac2012-02-12 20:14:01 +01004547 char_u *arg = eap->arg;
4548 int call_do_modelines = check_nomodeline(&arg);
Bram Moolenaar1610d052016-06-09 22:53:01 +02004549 int did_aucmd;
Bram Moolenaar60542ac2012-02-12 20:14:01 +01004550
Bram Moolenaar1610d052016-06-09 22:53:01 +02004551 (void)do_doautocmd(arg, TRUE, &did_aucmd);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004552 // Only when there is no <nomodeline>.
Bram Moolenaar1610d052016-06-09 22:53:01 +02004553 if (call_do_modelines && did_aucmd)
Bram Moolenaar60542ac2012-02-12 20:14:01 +01004554 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004555}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004556
Bram Moolenaar071d4272004-06-13 20:20:40 +00004557/*
4558 * :[N]bunload[!] [N] [bufname] unload buffer
4559 * :[N]bdelete[!] [N] [bufname] delete buffer from buffer list
4560 * :[N]bwipeout[!] [N] [bufname] delete buffer really
4561 */
4562 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004563ex_bunload(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004564{
Bram Moolenaar8cdbd5b2019-06-16 15:50:45 +02004565 if (ERROR_IF_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02004566 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004567 eap->errmsg = do_bufdel(
4568 eap->cmdidx == CMD_bdelete ? DOBUF_DEL
4569 : eap->cmdidx == CMD_bwipeout ? DOBUF_WIPE
4570 : DOBUF_UNLOAD, eap->arg,
4571 eap->addr_count, (int)eap->line1, (int)eap->line2, eap->forceit);
4572}
4573
4574/*
4575 * :[N]buffer [N] to buffer N
4576 * :[N]sbuffer [N] to buffer N
4577 */
4578 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004579ex_buffer(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004580{
Bram Moolenaar8cdbd5b2019-06-16 15:50:45 +02004581 if (ERROR_IF_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02004582 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004583 if (*eap->arg)
4584 eap->errmsg = e_trailing;
4585 else
4586 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004587 if (eap->addr_count == 0) // default is current buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00004588 goto_buffer(eap, DOBUF_CURRENT, FORWARD, 0);
4589 else
4590 goto_buffer(eap, DOBUF_FIRST, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02004591 if (eap->do_ecmd_cmd != NULL)
4592 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004593 }
4594}
4595
4596/*
4597 * :[N]bmodified [N] to next mod. buffer
4598 * :[N]sbmodified [N] to next mod. buffer
4599 */
4600 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004601ex_bmodified(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004602{
4603 goto_buffer(eap, DOBUF_MOD, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02004604 if (eap->do_ecmd_cmd != NULL)
4605 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004606}
4607
4608/*
4609 * :[N]bnext [N] to next buffer
4610 * :[N]sbnext [N] split and to next buffer
4611 */
4612 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004613ex_bnext(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004614{
Bram Moolenaar3029bcc2020-01-18 15:06:19 +01004615 if (ERROR_IF_POPUP_WINDOW)
4616 return;
4617
Bram Moolenaar071d4272004-06-13 20:20:40 +00004618 goto_buffer(eap, DOBUF_CURRENT, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02004619 if (eap->do_ecmd_cmd != NULL)
4620 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004621}
4622
4623/*
4624 * :[N]bNext [N] to previous buffer
4625 * :[N]bprevious [N] to previous buffer
4626 * :[N]sbNext [N] split and to previous buffer
4627 * :[N]sbprevious [N] split and to previous buffer
4628 */
4629 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004630ex_bprevious(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004631{
Bram Moolenaar3029bcc2020-01-18 15:06:19 +01004632 if (ERROR_IF_POPUP_WINDOW)
4633 return;
4634
Bram Moolenaar071d4272004-06-13 20:20:40 +00004635 goto_buffer(eap, DOBUF_CURRENT, BACKWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02004636 if (eap->do_ecmd_cmd != NULL)
4637 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004638}
4639
4640/*
4641 * :brewind to first buffer
4642 * :bfirst to first buffer
4643 * :sbrewind split and to first buffer
4644 * :sbfirst split and to first buffer
4645 */
4646 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004647ex_brewind(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004648{
Bram Moolenaar3029bcc2020-01-18 15:06:19 +01004649 if (ERROR_IF_POPUP_WINDOW)
4650 return;
4651
Bram Moolenaar071d4272004-06-13 20:20:40 +00004652 goto_buffer(eap, DOBUF_FIRST, FORWARD, 0);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02004653 if (eap->do_ecmd_cmd != NULL)
4654 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004655}
4656
4657/*
4658 * :blast to last buffer
4659 * :sblast split and to last buffer
4660 */
4661 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004662ex_blast(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004663{
Bram Moolenaar3029bcc2020-01-18 15:06:19 +01004664 if (ERROR_IF_POPUP_WINDOW)
4665 return;
4666
Bram Moolenaar071d4272004-06-13 20:20:40 +00004667 goto_buffer(eap, DOBUF_LAST, BACKWARD, 0);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02004668 if (eap->do_ecmd_cmd != NULL)
4669 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004670}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004671
4672 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004673ends_excmd(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004674{
4675 return (c == NUL || c == '|' || c == '"' || c == '\n');
4676}
4677
4678#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA) || defined(FEAT_EVAL) \
4679 || defined(PROTO)
4680/*
4681 * Return the next command, after the first '|' or '\n'.
4682 * Return NULL if not found.
4683 */
4684 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004685find_nextcmd(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004686{
4687 while (*p != '|' && *p != '\n')
4688 {
4689 if (*p == NUL)
4690 return NULL;
4691 ++p;
4692 }
4693 return (p + 1);
4694}
4695#endif
4696
4697/*
Bram Moolenaar2256c992016-11-15 21:17:07 +01004698 * Check if *p is a separator between Ex commands, skipping over white space.
4699 * Return NULL if it isn't, the following character if it is.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004700 */
4701 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004702check_nextcmd(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004703{
Bram Moolenaar2256c992016-11-15 21:17:07 +01004704 char_u *s = skipwhite(p);
4705
4706 if (*s == '|' || *s == '\n')
4707 return (s + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004708 else
4709 return NULL;
4710}
4711
4712/*
4713 * - if there are more files to edit
4714 * - and this is the last window
4715 * - and forceit not used
4716 * - and not repeated twice on a row
4717 * return FAIL and give error message if 'message' TRUE
4718 * return OK otherwise
4719 */
4720 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004721check_more(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004722 int message, // when FALSE check only, no messages
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004723 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004724{
4725 int n = ARGCOUNT - curwin->w_arg_idx - 1;
4726
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00004727 if (!forceit && only_one_window()
4728 && ARGCOUNT > 1 && !arg_had_last && n >= 0 && quitmore == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004729 {
4730 if (message)
4731 {
4732#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
4733 if ((p_confirm || cmdmod.confirm) && curbuf->b_fname != NULL)
4734 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02004735 char_u buff[DIALOG_MSG_SIZE];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004736
Bram Moolenaarda6e8912018-08-21 15:12:14 +02004737 vim_snprintf((char *)buff, DIALOG_MSG_SIZE,
4738 NGETTEXT("%d more file to edit. Quit anyway?",
4739 "%d more files to edit. Quit anyway?", n), n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004740 if (vim_dialog_yesno(VIM_QUESTION, NULL, buff, 1) == VIM_YES)
4741 return OK;
4742 return FAIL;
4743 }
4744#endif
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01004745 semsg(NGETTEXT("E173: %d more file to edit",
4746 "E173: %d more files to edit", n), n);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004747 quitmore = 2; // next try to quit is allowed
Bram Moolenaar071d4272004-06-13 20:20:40 +00004748 }
4749 return FAIL;
4750 }
4751 return OK;
4752}
4753
Bram Moolenaar071d4272004-06-13 20:20:40 +00004754/*
4755 * Function given to ExpandGeneric() to obtain the list of command names.
4756 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004757 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004758get_command_name(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004759{
4760 if (idx >= (int)CMD_SIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004761 return get_user_command_name(idx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004762 return cmdnames[idx].cmd_name;
4763}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004764
Bram Moolenaar071d4272004-06-13 20:20:40 +00004765 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004766ex_colorscheme(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004767{
Bram Moolenaare6850792010-05-14 15:28:44 +02004768 if (*eap->arg == NUL)
4769 {
4770#ifdef FEAT_EVAL
4771 char_u *expr = vim_strsave((char_u *)"g:colors_name");
4772 char_u *p = NULL;
4773
4774 if (expr != NULL)
4775 {
4776 ++emsg_off;
4777 p = eval_to_string(expr, NULL, FALSE);
4778 --emsg_off;
4779 vim_free(expr);
4780 }
4781 if (p != NULL)
4782 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01004783 msg((char *)p);
Bram Moolenaare6850792010-05-14 15:28:44 +02004784 vim_free(p);
4785 }
4786 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01004787 msg("default");
Bram Moolenaare6850792010-05-14 15:28:44 +02004788#else
Bram Moolenaar32526b32019-01-19 17:43:09 +01004789 msg(_("unknown"));
Bram Moolenaare6850792010-05-14 15:28:44 +02004790#endif
4791 }
4792 else if (load_colors(eap->arg) == FAIL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004793 semsg(_("E185: Cannot find color scheme '%s'"), eap->arg);
Bram Moolenaarf58d81a2019-01-28 20:19:05 +01004794
4795#ifdef FEAT_VTP
4796 else if (has_vtp_working())
4797 {
4798 // background color change requires clear + redraw
4799 update_screen(CLEAR);
4800 redrawcmd();
4801 }
4802#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004803}
4804
4805 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004806ex_highlight(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004807{
4808 if (*eap->arg == NUL && eap->cmd[2] == '!')
Bram Moolenaar32526b32019-01-19 17:43:09 +01004809 msg(_("Greetings, Vim user!"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004810 do_highlight(eap->arg, eap->forceit, FALSE);
4811}
4812
4813
4814/*
4815 * Call this function if we thought we were going to exit, but we won't
4816 * (because of an error). May need to restore the terminal mode.
4817 */
4818 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004819not_exiting(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004820{
4821 exiting = FALSE;
4822 settmode(TMODE_RAW);
4823}
4824
Bram Moolenaar12a96de2018-03-11 14:44:18 +01004825 static int
4826before_quit_autocmds(win_T *wp, int quit_all, int forceit)
4827{
4828 apply_autocmds(EVENT_QUITPRE, NULL, NULL, FALSE, wp->w_buffer);
4829
Bram Moolenaar34ba06b2019-10-20 22:27:10 +02004830 // Bail out when autocommands closed the window.
4831 // Refuse to quit when the buffer in the last window is being closed (can
4832 // only happen in autocommands).
Bram Moolenaar12a96de2018-03-11 14:44:18 +01004833 if (!win_valid(wp)
4834 || curbuf_locked()
4835 || (wp->w_buffer->b_nwindows == 1 && wp->w_buffer->b_locked > 0))
4836 return TRUE;
4837
4838 if (quit_all || (check_more(FALSE, forceit) == OK && only_one_window()))
4839 {
4840 apply_autocmds(EVENT_EXITPRE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar34ba06b2019-10-20 22:27:10 +02004841 // Refuse to quit when locked or when the window was closed or the
4842 // buffer in the last window is being closed (can only happen in
4843 // autocommands).
4844 if (!win_valid(wp) || curbuf_locked()
Bram Moolenaar12a96de2018-03-11 14:44:18 +01004845 || (curbuf->b_nwindows == 1 && curbuf->b_locked > 0))
4846 return TRUE;
4847 }
4848
4849 return FALSE;
4850}
4851
Bram Moolenaar071d4272004-06-13 20:20:40 +00004852/*
Bram Moolenaarf240e182014-11-27 18:33:02 +01004853 * ":quit": quit current window, quit Vim if the last window is closed.
Bram Moolenaar12a96de2018-03-11 14:44:18 +01004854 * ":{nr}quit": quit window {nr}
Bram Moolenaar4d14bac2019-10-20 21:15:15 +02004855 * Also used when closing a terminal window that's the last one.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004856 */
Bram Moolenaar4d14bac2019-10-20 21:15:15 +02004857 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004858ex_quit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004859{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004860 win_T *wp;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004861
Bram Moolenaar071d4272004-06-13 20:20:40 +00004862#ifdef FEAT_CMDWIN
4863 if (cmdwin_type != 0)
4864 {
4865 cmdwin_result = Ctrl_C;
4866 return;
4867 }
4868#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004869 // Don't quit while editing the command line.
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00004870 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00004871 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00004872 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00004873 return;
4874 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004875 if (eap->addr_count > 0)
4876 {
Bram Moolenaarf240e182014-11-27 18:33:02 +01004877 int wnr = eap->line2;
4878
4879 for (wp = firstwin; wp->w_next != NULL; wp = wp->w_next)
4880 if (--wnr <= 0)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004881 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004882 }
4883 else
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004884 wp = curwin;
Bram Moolenaarf240e182014-11-27 18:33:02 +01004885
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004886 // Refuse to quit when locked.
Bram Moolenaar87ffb5c2017-10-19 12:37:42 +02004887 if (curbuf_locked())
4888 return;
Bram Moolenaar12a96de2018-03-11 14:44:18 +01004889
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004890 // Trigger QuitPre and maybe ExitPre
Bram Moolenaar12a96de2018-03-11 14:44:18 +01004891 if (before_quit_autocmds(wp, FALSE, eap->forceit))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004892 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004893
4894#ifdef FEAT_NETBEANS_INTG
4895 netbeansForcedQuit = eap->forceit;
4896#endif
4897
4898 /*
4899 * If there are more files or windows we won't exit.
4900 */
4901 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
4902 exiting = TRUE;
Bram Moolenaarff930ca2017-10-19 17:12:10 +02004903 if ((!buf_hide(wp->w_buffer)
4904 && check_changed(wp->w_buffer, (p_awa ? CCGD_AW : 0)
Bram Moolenaar45d3b142013-11-09 03:31:51 +01004905 | (eap->forceit ? CCGD_FORCEIT : 0)
4906 | CCGD_EXCMD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004907 || check_more(TRUE, eap->forceit) == FAIL
Bram Moolenaar027387f2016-01-02 22:25:52 +01004908 || (only_one_window() && check_changed_any(eap->forceit, TRUE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004909 {
4910 not_exiting();
4911 }
4912 else
4913 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004914 // quit last window
4915 // Note: only_one_window() returns true, even so a help window is
4916 // still open. In that case only quit, if no address has been
4917 // specified. Example:
4918 // :h|wincmd w|1q - don't quit
4919 // :h|wincmd w|q - quit
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01004920 if (only_one_window() && (ONE_WINDOW || eap->addr_count == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004921 getout(0);
Bram Moolenaar2c33d7b2017-10-14 16:06:20 +02004922 not_exiting();
Bram Moolenaar4033c552017-09-16 20:54:51 +02004923#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004924 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02004925#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004926 // close window; may free buffer
Bram Moolenaareb44a682017-08-03 22:44:55 +02004927 win_close(wp, !buf_hide(wp->w_buffer) || eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004928 }
4929}
4930
4931/*
4932 * ":cquit".
4933 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004934 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004935ex_cquit(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004936{
Bram Moolenaar1860bde2020-01-06 21:47:21 +01004937 // this does not always pass on the exit code to the Manx compiler. why?
4938 getout(eap->addr_count > 0 ? (int)eap->line2 : EXIT_FAILURE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004939}
4940
4941/*
4942 * ":qall": try to quit all windows
4943 */
4944 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004945ex_quit_all(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004946{
4947# ifdef FEAT_CMDWIN
4948 if (cmdwin_type != 0)
4949 {
4950 if (eap->forceit)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004951 cmdwin_result = K_XF1; // ex_window() takes care of this
Bram Moolenaar071d4272004-06-13 20:20:40 +00004952 else
4953 cmdwin_result = K_XF2;
4954 return;
4955 }
4956# endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00004957
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004958 // Don't quit while editing the command line.
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00004959 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00004960 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00004961 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00004962 return;
4963 }
Bram Moolenaar12a96de2018-03-11 14:44:18 +01004964
4965 if (before_quit_autocmds(curwin, TRUE, eap->forceit))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004966 return;
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00004967
Bram Moolenaar071d4272004-06-13 20:20:40 +00004968 exiting = TRUE;
Bram Moolenaar027387f2016-01-02 22:25:52 +01004969 if (eap->forceit || !check_changed_any(FALSE, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004970 getout(0);
4971 not_exiting();
4972}
4973
Bram Moolenaar071d4272004-06-13 20:20:40 +00004974/*
4975 * ":close": close current window, unless it is the last one
4976 */
4977 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004978ex_close(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004979{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004980 win_T *win;
4981 int winnr = 0;
Bram Moolenaar4033c552017-09-16 20:54:51 +02004982#ifdef FEAT_CMDWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00004983 if (cmdwin_type != 0)
Bram Moolenaar9bd1a7e2011-05-19 14:50:54 +02004984 cmdwin_result = Ctrl_C;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004985 else
Bram Moolenaar4033c552017-09-16 20:54:51 +02004986#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004987 if (!text_locked() && !curbuf_locked())
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004988 {
4989 if (eap->addr_count == 0)
4990 ex_win_close(eap->forceit, curwin, NULL);
Bram Moolenaar6d41c782018-06-06 09:11:12 +02004991 else
4992 {
Bram Moolenaar29323592016-07-24 22:04:11 +02004993 FOR_ALL_WINDOWS(win)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004994 {
4995 winnr++;
4996 if (winnr == eap->line2)
4997 break;
4998 }
4999 if (win == NULL)
5000 win = lastwin;
5001 ex_win_close(eap->forceit, win, NULL);
5002 }
5003 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005004}
5005
Bram Moolenaar4033c552017-09-16 20:54:51 +02005006#ifdef FEAT_QUICKFIX
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005007/*
5008 * ":pclose": Close any preview window.
5009 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005010 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005011ex_pclose(exarg_T *eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005012{
5013 win_T *win;
5014
Bram Moolenaar79648732019-07-18 21:43:07 +02005015 // First close any normal window.
Bram Moolenaar29323592016-07-24 22:04:11 +02005016 FOR_ALL_WINDOWS(win)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005017 if (win->w_p_pvw)
5018 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00005019 ex_win_close(eap->forceit, win, NULL);
Bram Moolenaar79648732019-07-18 21:43:07 +02005020 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005021 }
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01005022# ifdef FEAT_PROP_POPUP
Bram Moolenaar79648732019-07-18 21:43:07 +02005023 // Also when 'previewpopup' is empty, it might have been cleared.
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02005024 popup_close_preview();
Bram Moolenaar79648732019-07-18 21:43:07 +02005025# endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005026}
Bram Moolenaar4033c552017-09-16 20:54:51 +02005027#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005028
Bram Moolenaarf740b292006-02-16 22:11:02 +00005029/*
5030 * Close window "win" and take care of handling closing the last window for a
5031 * modified buffer.
5032 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005033 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005034ex_win_close(
5035 int forceit,
5036 win_T *win,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005037 tabpage_T *tp) // NULL or the tab page "win" is in
Bram Moolenaar071d4272004-06-13 20:20:40 +00005038{
5039 int need_hide;
5040 buf_T *buf = win->w_buffer;
5041
5042 need_hide = (bufIsChanged(buf) && buf->b_nwindows <= 1);
Bram Moolenaareb44a682017-08-03 22:44:55 +02005043 if (need_hide && !buf_hide(buf) && !forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005044 {
Bram Moolenaar4033c552017-09-16 20:54:51 +02005045#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005046 if ((p_confirm || cmdmod.confirm) && p_write)
5047 {
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005048 bufref_T bufref;
5049
5050 set_bufref(&bufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005051 dialog_changed(buf, FALSE);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005052 if (bufref_valid(&bufref) && bufIsChanged(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005053 return;
5054 need_hide = FALSE;
5055 }
5056 else
Bram Moolenaar4033c552017-09-16 20:54:51 +02005057#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005058 {
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02005059 no_write_message();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005060 return;
5061 }
5062 }
5063
Bram Moolenaar4033c552017-09-16 20:54:51 +02005064#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00005065 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005066#endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00005067
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005068 // free buffer when not hiding it or when it's a scratch buffer
Bram Moolenaarf740b292006-02-16 22:11:02 +00005069 if (tp == NULL)
Bram Moolenaareb44a682017-08-03 22:44:55 +02005070 win_close(win, !need_hide && !buf_hide(buf));
Bram Moolenaarf740b292006-02-16 22:11:02 +00005071 else
Bram Moolenaareb44a682017-08-03 22:44:55 +02005072 win_close_othertab(win, !need_hide && !buf_hide(buf), tp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005073}
5074
Bram Moolenaar071d4272004-06-13 20:20:40 +00005075/*
Bram Moolenaardea25702017-01-29 15:18:10 +01005076 * Handle the argument for a tabpage related ex command.
5077 * Returns a tabpage number.
5078 * When an error is encountered then eap->errmsg is set.
5079 */
5080 static int
5081get_tabpage_arg(exarg_T *eap)
5082{
5083 int tab_number;
5084 int unaccept_arg0 = (eap->cmdidx == CMD_tabmove) ? 0 : 1;
5085
5086 if (eap->arg && *eap->arg != NUL)
5087 {
5088 char_u *p = eap->arg;
5089 char_u *p_save;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005090 int relative = 0; // argument +N/-N means: go to N places to the
5091 // right/left relative to the current position.
Bram Moolenaardea25702017-01-29 15:18:10 +01005092
5093 if (*p == '-')
5094 {
5095 relative = -1;
5096 p++;
5097 }
5098 else if (*p == '+')
5099 {
5100 relative = 1;
5101 p++;
5102 }
5103
5104 p_save = p;
5105 tab_number = getdigits(&p);
5106
5107 if (relative == 0)
5108 {
5109 if (STRCMP(p, "$") == 0)
5110 tab_number = LAST_TAB_NR;
5111 else if (p == p_save || *p_save == '-' || *p != NUL
5112 || tab_number > LAST_TAB_NR)
5113 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005114 // No numbers as argument.
Bram Moolenaardea25702017-01-29 15:18:10 +01005115 eap->errmsg = e_invarg;
5116 goto theend;
5117 }
5118 }
5119 else
5120 {
5121 if (*p_save == NUL)
5122 tab_number = 1;
5123 else if (p == p_save || *p_save == '-' || *p != NUL
5124 || tab_number == 0)
5125 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005126 // No numbers as argument.
Bram Moolenaardea25702017-01-29 15:18:10 +01005127 eap->errmsg = e_invarg;
5128 goto theend;
5129 }
5130 tab_number = tab_number * relative + tabpage_index(curtab);
5131 if (!unaccept_arg0 && relative == -1)
5132 --tab_number;
5133 }
5134 if (tab_number < unaccept_arg0 || tab_number > LAST_TAB_NR)
5135 eap->errmsg = e_invarg;
5136 }
5137 else if (eap->addr_count > 0)
5138 {
5139 if (unaccept_arg0 && eap->line2 == 0)
Bram Moolenaarc6251552017-01-29 21:42:20 +01005140 {
Bram Moolenaardea25702017-01-29 15:18:10 +01005141 eap->errmsg = e_invrange;
Bram Moolenaarc6251552017-01-29 21:42:20 +01005142 tab_number = 0;
5143 }
Bram Moolenaardea25702017-01-29 15:18:10 +01005144 else
5145 {
5146 tab_number = eap->line2;
Bram Moolenaar82a12462019-01-22 22:41:42 +01005147 if (!unaccept_arg0 && *skipwhite(*eap->cmdlinep) == '-')
Bram Moolenaardea25702017-01-29 15:18:10 +01005148 {
5149 --tab_number;
5150 if (tab_number < unaccept_arg0)
5151 eap->errmsg = e_invarg;
5152 }
5153 }
5154 }
5155 else
5156 {
5157 switch (eap->cmdidx)
5158 {
5159 case CMD_tabnext:
5160 tab_number = tabpage_index(curtab) + 1;
5161 if (tab_number > LAST_TAB_NR)
5162 tab_number = 1;
5163 break;
5164 case CMD_tabmove:
5165 tab_number = LAST_TAB_NR;
5166 break;
5167 default:
5168 tab_number = tabpage_index(curtab);
5169 }
5170 }
5171
5172theend:
5173 return tab_number;
5174}
5175
5176/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00005177 * ":tabclose": close current tab page, unless it is the last one.
5178 * ":tabclose N": close tab page N.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005179 */
5180 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005181ex_tabclose(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005182{
Bram Moolenaarf740b292006-02-16 22:11:02 +00005183 tabpage_T *tp;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005184 int tab_number;
Bram Moolenaarf740b292006-02-16 22:11:02 +00005185
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005186# ifdef FEAT_CMDWIN
5187 if (cmdwin_type != 0)
5188 cmdwin_result = K_IGNORE;
5189 else
5190# endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00005191 if (first_tabpage->tp_next == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005192 emsg(_("E784: Cannot close last tab page"));
Bram Moolenaarf740b292006-02-16 22:11:02 +00005193 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005194 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005195 tab_number = get_tabpage_arg(eap);
5196 if (eap->errmsg == NULL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005197 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005198 tp = find_tabpage(tab_number);
Bram Moolenaarf740b292006-02-16 22:11:02 +00005199 if (tp == NULL)
5200 {
5201 beep_flush();
5202 return;
5203 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005204 if (tp != curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00005205 {
5206 tabpage_close_other(tp, eap->forceit);
5207 return;
5208 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005209 else if (!text_locked() && !curbuf_locked())
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005210 tabpage_close(eap->forceit);
5211 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005212 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005213}
5214
5215/*
5216 * ":tabonly": close all tab pages except the current one
5217 */
5218 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005219ex_tabonly(exarg_T *eap)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005220{
5221 tabpage_T *tp;
5222 int done;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005223 int tab_number;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005224
5225# ifdef FEAT_CMDWIN
5226 if (cmdwin_type != 0)
5227 cmdwin_result = K_IGNORE;
5228 else
5229# endif
5230 if (first_tabpage->tp_next == NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01005231 msg(_("Already only one tab page"));
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005232 else
5233 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005234 tab_number = get_tabpage_arg(eap);
5235 if (eap->errmsg == NULL)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005236 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005237 goto_tabpage(tab_number);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005238 // Repeat this up to a 1000 times, because autocommands may
5239 // mess up the lists.
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005240 for (done = 0; done < 1000; ++done)
5241 {
5242 FOR_ALL_TABPAGES(tp)
5243 if (tp->tp_topframe != topframe)
5244 {
5245 tabpage_close_other(tp, eap->forceit);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005246 // if we failed to close it quit
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005247 if (valid_tabpage(tp))
5248 done = 1000;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005249 // start over, "tp" is now invalid
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005250 break;
5251 }
5252 if (first_tabpage->tp_next == NULL)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005253 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005254 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005255 }
5256 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005257}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005258
5259/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00005260 * Close the current tab page.
5261 */
5262 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005263tabpage_close(int forceit)
Bram Moolenaarf740b292006-02-16 22:11:02 +00005264{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005265 // First close all the windows but the current one. If that worked then
5266 // close the last window in this tab, that will close it.
Bram Moolenaar459ca562016-11-10 18:16:33 +01005267 if (!ONE_WINDOW)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005268 close_others(TRUE, forceit);
Bram Moolenaar459ca562016-11-10 18:16:33 +01005269 if (ONE_WINDOW)
Bram Moolenaarf740b292006-02-16 22:11:02 +00005270 ex_win_close(forceit, curwin, NULL);
5271# ifdef FEAT_GUI
5272 need_mouse_correct = TRUE;
5273# endif
5274}
5275
5276/*
5277 * Close tab page "tp", which is not the current tab page.
5278 * Note that autocommands may make "tp" invalid.
Bram Moolenaar7875acc2006-09-10 13:51:17 +00005279 * Also takes care of the tab pages line disappearing when closing the
5280 * last-but-one tab page.
Bram Moolenaarf740b292006-02-16 22:11:02 +00005281 */
5282 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005283tabpage_close_other(tabpage_T *tp, int forceit)
Bram Moolenaarf740b292006-02-16 22:11:02 +00005284{
5285 int done = 0;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005286 win_T *wp;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00005287 int h = tabline_height();
Bram Moolenaarf740b292006-02-16 22:11:02 +00005288
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005289 // Limit to 1000 windows, autocommands may add a window while we close
5290 // one. OK, so I'm paranoid...
Bram Moolenaarf740b292006-02-16 22:11:02 +00005291 while (++done < 1000)
5292 {
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005293 wp = tp->tp_firstwin;
5294 ex_win_close(forceit, wp, tp);
Bram Moolenaarf740b292006-02-16 22:11:02 +00005295
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005296 // Autocommands may delete the tab page under our fingers and we may
5297 // fail to close a window with a modified buffer.
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005298 if (!valid_tabpage(tp) || tp->tp_firstwin == wp)
Bram Moolenaarf740b292006-02-16 22:11:02 +00005299 break;
5300 }
Bram Moolenaar7875acc2006-09-10 13:51:17 +00005301
Bram Moolenaar29323592016-07-24 22:04:11 +02005302 apply_autocmds(EVENT_TABCLOSED, NULL, NULL, FALSE, curbuf);
Bram Moolenaar29323592016-07-24 22:04:11 +02005303
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005304 redraw_tabline = TRUE;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00005305 if (h != tabline_height())
5306 shell_new_rows();
Bram Moolenaarf740b292006-02-16 22:11:02 +00005307}
5308
5309/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005310 * ":only".
5311 */
5312 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005313ex_only(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005314{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005315 win_T *wp;
5316 int wnr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005317# ifdef FEAT_GUI
5318 need_mouse_correct = TRUE;
5319# endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005320 if (eap->addr_count > 0)
5321 {
5322 wnr = eap->line2;
5323 for (wp = firstwin; --wnr > 0; )
5324 {
5325 if (wp->w_next == NULL)
5326 break;
5327 else
5328 wp = wp->w_next;
5329 }
5330 win_goto(wp);
5331 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005332 close_others(TRUE, eap->forceit);
5333}
5334
Bram Moolenaar071d4272004-06-13 20:20:40 +00005335 static void
Bram Moolenaar5e1e6d22017-01-02 17:26:00 +01005336ex_hide(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005337{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005338 // ":hide" or ":hide | cmd": hide current window
Bram Moolenaar2256c992016-11-15 21:17:07 +01005339 if (!eap->skip)
5340 {
Bram Moolenaar4033c552017-09-16 20:54:51 +02005341#ifdef FEAT_GUI
Bram Moolenaar2256c992016-11-15 21:17:07 +01005342 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005343#endif
Bram Moolenaar2256c992016-11-15 21:17:07 +01005344 if (eap->addr_count == 0)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005345 win_close(curwin, FALSE); // don't free buffer
Bram Moolenaar2256c992016-11-15 21:17:07 +01005346 else
5347 {
5348 int winnr = 0;
5349 win_T *win;
Bram Moolenaarf240e182014-11-27 18:33:02 +01005350
Bram Moolenaar2256c992016-11-15 21:17:07 +01005351 FOR_ALL_WINDOWS(win)
5352 {
5353 winnr++;
5354 if (winnr == eap->line2)
5355 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005356 }
Bram Moolenaar2256c992016-11-15 21:17:07 +01005357 if (win == NULL)
5358 win = lastwin;
5359 win_close(win, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005360 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005361 }
5362}
5363
5364/*
5365 * ":stop" and ":suspend": Suspend Vim.
5366 */
5367 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005368ex_stop(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005369{
5370 /*
5371 * Disallow suspending for "rvim".
5372 */
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005373 if (!check_restricted())
Bram Moolenaar071d4272004-06-13 20:20:40 +00005374 {
5375 if (!eap->forceit)
5376 autowrite_all();
5377 windgoto((int)Rows - 1, 0);
5378 out_char('\n');
5379 out_flush();
5380 stoptermcap();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005381 out_flush(); // needed for SUN to restore xterm buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00005382#ifdef FEAT_TITLE
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005383 mch_restore_title(SAVE_RESTORE_BOTH); // restore window titles
Bram Moolenaar071d4272004-06-13 20:20:40 +00005384#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005385 ui_suspend(); // call machine specific function
Bram Moolenaar071d4272004-06-13 20:20:40 +00005386#ifdef FEAT_TITLE
5387 maketitle();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005388 resettitle(); // force updating the title
Bram Moolenaar071d4272004-06-13 20:20:40 +00005389#endif
5390 starttermcap();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005391 scroll_start(); // scroll screen before redrawing
Bram Moolenaar071d4272004-06-13 20:20:40 +00005392 redraw_later_clear();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005393 shell_resized(); // may have resized window
Bram Moolenaar071d4272004-06-13 20:20:40 +00005394 }
5395}
5396
5397/*
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005398 * ":exit", ":xit" and ":wq": Write file and quite the current window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005399 */
5400 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005401ex_exit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005402{
5403#ifdef FEAT_CMDWIN
5404 if (cmdwin_type != 0)
5405 {
5406 cmdwin_result = Ctrl_C;
5407 return;
5408 }
5409#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005410 // Don't quit while editing the command line.
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005411 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005412 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005413 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005414 return;
5415 }
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005416
5417 if (before_quit_autocmds(curwin, FALSE, eap->forceit))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005418 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005419
5420 /*
5421 * if more files or windows we won't exit
5422 */
5423 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
5424 exiting = TRUE;
5425 if ( ((eap->cmdidx == CMD_wq
5426 || curbufIsChanged())
5427 && do_write(eap) == FAIL)
5428 || check_more(TRUE, eap->forceit) == FAIL
Bram Moolenaar027387f2016-01-02 22:25:52 +01005429 || (only_one_window() && check_changed_any(eap->forceit, FALSE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005430 {
5431 not_exiting();
5432 }
5433 else
5434 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005435 if (only_one_window()) // quit last window, exit Vim
Bram Moolenaar071d4272004-06-13 20:20:40 +00005436 getout(0);
Bram Moolenaar2c33d7b2017-10-14 16:06:20 +02005437 not_exiting();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005438# ifdef FEAT_GUI
5439 need_mouse_correct = TRUE;
5440# endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005441 // Quit current window, may free the buffer.
Bram Moolenaareb44a682017-08-03 22:44:55 +02005442 win_close(curwin, !buf_hide(curwin->w_buffer));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005443 }
5444}
5445
5446/*
5447 * ":print", ":list", ":number".
5448 */
5449 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005450ex_print(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005451{
Bram Moolenaardf177f62005-02-22 08:39:57 +00005452 if (curbuf->b_ml.ml_flags & ML_EMPTY)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005453 emsg(_(e_emptybuf));
Bram Moolenaardf177f62005-02-22 08:39:57 +00005454 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005455 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00005456 for ( ;!got_int; ui_breakcheck())
5457 {
5458 print_line(eap->line1,
5459 (eap->cmdidx == CMD_number || eap->cmdidx == CMD_pound
5460 || (eap->flags & EXFLAG_NR)),
5461 eap->cmdidx == CMD_list || (eap->flags & EXFLAG_LIST));
5462 if (++eap->line1 > eap->line2)
5463 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005464 out_flush(); // show one line at a time
Bram Moolenaardf177f62005-02-22 08:39:57 +00005465 }
5466 setpcmark();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005467 // put cursor at last line
Bram Moolenaardf177f62005-02-22 08:39:57 +00005468 curwin->w_cursor.lnum = eap->line2;
5469 beginline(BL_SOL | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005470 }
5471
Bram Moolenaar071d4272004-06-13 20:20:40 +00005472 ex_no_reprint = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005473}
5474
5475#ifdef FEAT_BYTEOFF
5476 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005477ex_goto(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005478{
5479 goto_byte(eap->line2);
5480}
5481#endif
5482
5483/*
5484 * ":shell".
5485 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005486 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005487ex_shell(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005488{
5489 do_shell(NULL, 0);
5490}
5491
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005492#if defined(HAVE_DROP_FILE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005493
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005494static int drop_busy = FALSE;
5495static int drop_filec;
5496static char_u **drop_filev = NULL;
5497static int drop_split;
5498static void (*drop_callback)(void *);
5499static void *drop_cookie;
5500
5501 static void
5502handle_drop_internal(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005503{
5504 exarg_T ea;
5505 int save_msg_scroll = msg_scroll;
5506
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005507 // Setting the argument list may cause screen updates and being called
5508 // recursively. Avoid that by setting drop_busy.
5509 drop_busy = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005510
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005511 // Check whether the current buffer is changed. If so, we will need
5512 // to split the current window or data could be lost.
5513 // We don't need to check if the 'hidden' option is set, as in this
5514 // case the buffer won't be lost.
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005515 if (!buf_hide(curbuf) && !drop_split)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005516 {
5517 ++emsg_off;
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005518 drop_split = check_changed(curbuf, CCGD_AW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005519 --emsg_off;
5520 }
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005521 if (drop_split)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005522 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005523 if (win_split(0, 0) == FAIL)
5524 return;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02005525 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005526
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005527 // When splitting the window, create a new alist. Otherwise the
5528 // existing one is overwritten.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005529 alist_unlink(curwin->w_alist);
5530 alist_new();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005531 }
5532
5533 /*
5534 * Set up the new argument list.
5535 */
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005536 alist_set(ALIST(curwin), drop_filec, drop_filev, FALSE, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005537
5538 /*
5539 * Move to the first file.
5540 */
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005541 // Fake up a minimal "next" command for do_argfile()
Bram Moolenaar071d4272004-06-13 20:20:40 +00005542 vim_memset(&ea, 0, sizeof(ea));
5543 ea.cmd = (char_u *)"next";
5544 do_argfile(&ea, 0);
5545
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005546 // do_ecmd() may set need_start_insertmode, but since we never left Insert
5547 // mode that is not needed here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005548 need_start_insertmode = FALSE;
5549
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005550 // Restore msg_scroll, otherwise a following command may cause scrolling
5551 // unexpectedly. The screen will be redrawn by the caller, thus
5552 // msg_scroll being set by displaying a message is irrelevant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005553 msg_scroll = save_msg_scroll;
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005554
5555 if (drop_callback != NULL)
5556 drop_callback(drop_cookie);
5557
5558 drop_filev = NULL;
5559 drop_busy = FALSE;
5560}
5561
5562/*
5563 * Handle a file drop. The code is here because a drop is *nearly* like an
5564 * :args command, but not quite (we have a list of exact filenames, so we
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01005565 * don't want to (a) parse a command line, or (b) expand wildcards). So the
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005566 * code is very similar to :args and hence needs access to a lot of the static
5567 * functions in this file.
5568 *
5569 * The "filev" list must have been allocated using alloc(), as should each item
5570 * in the list. This function takes over responsibility for freeing the "filev"
5571 * list.
5572 */
5573 void
5574handle_drop(
5575 int filec, // the number of files dropped
5576 char_u **filev, // the list of files dropped
5577 int split, // force splitting the window
5578 void (*callback)(void *), // to be called after setting the argument
5579 // list
5580 void *cookie) // argument for "callback" (allocated)
5581{
5582 // Cannot handle recursive drops, finish the pending one.
5583 if (drop_busy)
5584 {
5585 FreeWild(filec, filev);
5586 vim_free(cookie);
5587 return;
5588 }
5589
5590 // When calling handle_drop() more than once in a row we only use the last
5591 // one.
5592 if (drop_filev != NULL)
5593 {
5594 FreeWild(drop_filec, drop_filev);
5595 vim_free(drop_cookie);
5596 }
5597
5598 drop_filec = filec;
5599 drop_filev = filev;
5600 drop_split = split;
5601 drop_callback = callback;
5602 drop_cookie = cookie;
5603
5604 // Postpone this when:
5605 // - editing the command line
5606 // - not possible to change the current buffer
5607 // - updating the screen
5608 // As it may change buffers and window structures that are in use and cause
5609 // freed memory to be used.
5610 if (text_locked() || curbuf_locked() || updating_screen)
5611 return;
5612
5613 handle_drop_internal();
5614}
5615
5616/*
5617 * To be called when text is unlocked, curbuf is unlocked or updating_screen is
5618 * reset: Handle a postponed drop.
5619 */
5620 void
5621handle_any_postponed_drop(void)
5622{
5623 if (!drop_busy && drop_filev != NULL
5624 && !text_locked() && !curbuf_locked() && !updating_screen)
5625 handle_drop_internal();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005626}
5627#endif
5628
Bram Moolenaar071d4272004-06-13 20:20:40 +00005629/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005630 * ":preserve".
5631 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005632 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005633ex_preserve(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005634{
Bram Moolenaar4399ef42005-02-12 14:29:27 +00005635 curbuf->b_flags |= BF_PRESERVED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005636 ml_preserve(curbuf, TRUE);
5637}
5638
5639/*
5640 * ":recover".
5641 */
5642 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005643ex_recover(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005644{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005645 // Set recoverymode right away to avoid the ATTENTION prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005646 recoverymode = TRUE;
Bram Moolenaar45d3b142013-11-09 03:31:51 +01005647 if (!check_changed(curbuf, (p_awa ? CCGD_AW : 0)
5648 | CCGD_MULTWIN
5649 | (eap->forceit ? CCGD_FORCEIT : 0)
5650 | CCGD_EXCMD)
5651
Bram Moolenaar071d4272004-06-13 20:20:40 +00005652 && (*eap->arg == NUL
5653 || setfname(curbuf, eap->arg, NULL, TRUE) == OK))
Bram Moolenaar99499b12019-05-23 21:35:48 +02005654 ml_recover(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005655 recoverymode = FALSE;
5656}
5657
5658/*
5659 * Command modifier used in a wrong way.
5660 */
5661 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005662ex_wrongmodifier(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005663{
5664 eap->errmsg = e_invcmd;
5665}
5666
Bram Moolenaar071d4272004-06-13 20:20:40 +00005667/*
5668 * :sview [+command] file split window with new file, read-only
5669 * :split [[+command] file] split window with current or new file
5670 * :vsplit [[+command] file] split window vertically with current or new file
5671 * :new [[+command] file] split window with no or new file
5672 * :vnew [[+command] file] split vertically window with no or new file
5673 * :sfind [+command] file split window with file in 'path'
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005674 *
5675 * :tabedit open new Tab page with empty window
5676 * :tabedit [+command] file open new Tab page and edit "file"
5677 * :tabnew [[+command] file] just like :tabedit
5678 * :tabfind [+command] file open new Tab page and find "file"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005679 */
5680 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005681ex_splitview(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005682{
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005683 win_T *old_curwin = curwin;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005684#if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005685 char_u *fname = NULL;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005686#endif
5687#ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00005688 int browse_flag = cmdmod.browse;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005689#endif
Bram Moolenaar39902a02018-06-21 22:10:08 +02005690 int use_tab = eap->cmdidx == CMD_tabedit
5691 || eap->cmdidx == CMD_tabfind
5692 || eap->cmdidx == CMD_tabnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005693
Bram Moolenaar8cdbd5b2019-06-16 15:50:45 +02005694 if (ERROR_IF_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02005695 return;
5696
Bram Moolenaar4033c552017-09-16 20:54:51 +02005697#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00005698 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005699#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005700
Bram Moolenaar4033c552017-09-16 20:54:51 +02005701#ifdef FEAT_QUICKFIX
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005702 // A ":split" in the quickfix window works like ":new". Don't want two
5703 // quickfix windows. But it's OK when doing ":tab split".
Bram Moolenaar05bb9532008-07-04 09:44:11 +00005704 if (bt_quickfix(curbuf) && cmdmod.tab == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005705 {
5706 if (eap->cmdidx == CMD_split)
5707 eap->cmdidx = CMD_new;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005708 if (eap->cmdidx == CMD_vsplit)
5709 eap->cmdidx = CMD_vnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005710 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02005711#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005712
Bram Moolenaar4033c552017-09-16 20:54:51 +02005713#ifdef FEAT_SEARCHPATH
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005714 if (eap->cmdidx == CMD_sfind || eap->cmdidx == CMD_tabfind)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005715 {
5716 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg),
5717 FNAME_MESS, TRUE, curbuf->b_ffname);
5718 if (fname == NULL)
5719 goto theend;
5720 eap->arg = fname;
5721 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005722# ifdef FEAT_BROWSE
Bram Moolenaar4033c552017-09-16 20:54:51 +02005723 else
5724# endif
5725#endif
5726#ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00005727 if (cmdmod.browse
Bram Moolenaar071d4272004-06-13 20:20:40 +00005728 && eap->cmdidx != CMD_vnew
Bram Moolenaar071d4272004-06-13 20:20:40 +00005729 && eap->cmdidx != CMD_new)
5730 {
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00005731 if (
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005732# ifdef FEAT_GUI
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00005733 !gui.in_use &&
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005734# endif
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00005735 au_has_group((char_u *)"FileExplorer"))
5736 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005737 // No browsing supported but we do have the file explorer:
5738 // Edit the directory.
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00005739 if (*eap->arg == NUL || !mch_isdir(eap->arg))
5740 eap->arg = (char_u *)".";
5741 }
5742 else
5743 {
Bram Moolenaar39902a02018-06-21 22:10:08 +02005744 fname = do_browse(0, (char_u *)(use_tab
5745 ? _("Edit File in new tab page")
5746 : _("Edit File in new window")),
Bram Moolenaar071d4272004-06-13 20:20:40 +00005747 eap->arg, NULL, NULL, NULL, curbuf);
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00005748 if (fname == NULL)
5749 goto theend;
5750 eap->arg = fname;
5751 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005752 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005753 cmdmod.browse = FALSE; // Don't browse again in do_ecmd().
Bram Moolenaar4033c552017-09-16 20:54:51 +02005754#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005755
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005756 /*
5757 * Either open new tab page or split the window.
5758 */
Bram Moolenaar39902a02018-06-21 22:10:08 +02005759 if (use_tab)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005760 {
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005761 if (win_new_tabpage(cmdmod.tab != 0 ? cmdmod.tab
5762 : eap->addr_count == 0 ? 0
5763 : (int)eap->line2 + 1) != FAIL)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005764 {
Bram Moolenaard2b66012008-01-09 19:30:36 +00005765 do_exedit(eap, old_curwin);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005766
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005767 // set the alternate buffer for the window we came from
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005768 if (curwin != old_curwin
5769 && win_valid(old_curwin)
5770 && old_curwin->w_buffer != curbuf
5771 && !cmdmod.keepalt)
5772 old_curwin->w_alt_fnum = curbuf->b_fnum;
5773 }
5774 }
5775 else if (win_split(eap->addr_count > 0 ? (int)eap->line2 : 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005776 *eap->cmd == 'v' ? WSP_VERT : 0) != FAIL)
5777 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005778 // Reset 'scrollbind' when editing another file, but keep it when
5779 // doing ":split" without arguments.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005780 if (*eap->arg != NUL
Bram Moolenaar8a3bb562018-03-04 20:14:14 +01005781# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00005782 || cmdmod.browse
Bram Moolenaar8a3bb562018-03-04 20:14:14 +01005783# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005784 )
Bram Moolenaar3368ea22010-09-21 16:56:35 +02005785 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005786 else
5787 do_check_scrollbind(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005788 do_exedit(eap, old_curwin);
5789 }
5790
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005791# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00005792 cmdmod.browse = browse_flag;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005793# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005794
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005795# if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005796theend:
5797 vim_free(fname);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005798# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005799}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005800
5801/*
Bram Moolenaar80a94a52006-02-23 21:26:58 +00005802 * Open a new tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005803 */
5804 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005805tabpage_new(void)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00005806{
5807 exarg_T ea;
5808
5809 vim_memset(&ea, 0, sizeof(ea));
5810 ea.cmdidx = CMD_tabnew;
5811 ea.cmd = (char_u *)"tabn";
5812 ea.arg = (char_u *)"";
5813 ex_splitview(&ea);
5814}
5815
5816/*
5817 * :tabnext command
5818 */
5819 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005820ex_tabnext(exarg_T *eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005821{
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005822 int tab_number;
5823
Bram Moolenaar8cdbd5b2019-06-16 15:50:45 +02005824 if (ERROR_IF_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02005825 return;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005826 switch (eap->cmdidx)
5827 {
5828 case CMD_tabfirst:
5829 case CMD_tabrewind:
5830 goto_tabpage(1);
5831 break;
5832 case CMD_tablast:
5833 goto_tabpage(9999);
5834 break;
5835 case CMD_tabprevious:
5836 case CMD_tabNext:
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005837 if (eap->arg && *eap->arg != NUL)
5838 {
5839 char_u *p = eap->arg;
5840 char_u *p_save = p;
5841
5842 tab_number = getdigits(&p);
5843 if (p == p_save || *p_save == '-' || *p != NUL
5844 || tab_number == 0)
5845 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005846 // No numbers as argument.
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005847 eap->errmsg = e_invarg;
5848 return;
5849 }
5850 }
5851 else
5852 {
5853 if (eap->addr_count == 0)
5854 tab_number = 1;
5855 else
5856 {
5857 tab_number = eap->line2;
5858 if (tab_number < 1)
5859 {
5860 eap->errmsg = e_invrange;
5861 return;
5862 }
5863 }
5864 }
5865 goto_tabpage(-tab_number);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005866 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005867 default: // CMD_tabnext
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005868 tab_number = get_tabpage_arg(eap);
5869 if (eap->errmsg == NULL)
5870 goto_tabpage(tab_number);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005871 break;
5872 }
Bram Moolenaar80a94a52006-02-23 21:26:58 +00005873}
5874
5875/*
5876 * :tabmove command
5877 */
5878 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005879ex_tabmove(exarg_T *eap)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00005880{
Bram Moolenaar40ce3a42015-04-21 18:08:39 +02005881 int tab_number;
Bram Moolenaar8cb8dca2012-07-06 18:27:39 +02005882
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005883 tab_number = get_tabpage_arg(eap);
5884 if (eap->errmsg == NULL)
5885 tabpage_move(tab_number);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005886}
5887
5888/*
5889 * :tabs command: List tabs and their contents.
5890 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005891 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005892ex_tabs(exarg_T *eap UNUSED)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005893{
5894 tabpage_T *tp;
5895 win_T *wp;
5896 int tabcount = 1;
5897
5898 msg_start();
5899 msg_scroll = TRUE;
5900 for (tp = first_tabpage; tp != NULL && !got_int; tp = tp->tp_next)
5901 {
5902 msg_putchar('\n');
5903 vim_snprintf((char *)IObuff, IOSIZE, _("Tab page %d"), tabcount++);
Bram Moolenaar8820b482017-03-16 17:23:31 +01005904 msg_outtrans_attr(IObuff, HL_ATTR(HLF_T));
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005905 out_flush(); // output one line at a time
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005906 ui_breakcheck();
5907
Bram Moolenaar030f0df2006-02-21 22:02:53 +00005908 if (tp == curtab)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005909 wp = firstwin;
5910 else
5911 wp = tp->tp_firstwin;
5912 for ( ; wp != NULL && !got_int; wp = wp->w_next)
5913 {
Bram Moolenaar80a94a52006-02-23 21:26:58 +00005914 msg_putchar('\n');
5915 msg_putchar(wp == curwin ? '>' : ' ');
5916 msg_putchar(' ');
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005917 msg_putchar(bufIsChanged(wp->w_buffer) ? '+' : ' ');
5918 msg_putchar(' ');
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005919 if (buf_spname(wp->w_buffer) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02005920 vim_strncpy(IObuff, buf_spname(wp->w_buffer), IOSIZE - 1);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005921 else
5922 home_replace(wp->w_buffer, wp->w_buffer->b_fname,
5923 IObuff, IOSIZE, TRUE);
5924 msg_outtrans(IObuff);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005925 out_flush(); // output one line at a time
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005926 ui_breakcheck();
5927 }
5928 }
5929}
5930
Bram Moolenaar071d4272004-06-13 20:20:40 +00005931/*
5932 * ":mode": Set screen mode.
5933 * If no argument given, just get the screen size and redraw.
5934 */
5935 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005936ex_mode(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005937{
5938 if (*eap->arg == NUL)
5939 shell_resized();
5940 else
Bram Moolenaar3c71aec2020-01-17 19:32:20 +01005941 emsg(_(e_screenmode));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005942}
5943
Bram Moolenaar071d4272004-06-13 20:20:40 +00005944/*
5945 * ":resize".
5946 * set, increment or decrement current window height
5947 */
5948 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005949ex_resize(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005950{
5951 int n;
5952 win_T *wp = curwin;
5953
5954 if (eap->addr_count > 0)
5955 {
5956 n = eap->line2;
5957 for (wp = firstwin; wp->w_next != NULL && --n > 0; wp = wp->w_next)
5958 ;
5959 }
5960
Bram Moolenaar44a2f922016-03-19 22:11:51 +01005961# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00005962 need_mouse_correct = TRUE;
Bram Moolenaar44a2f922016-03-19 22:11:51 +01005963# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005964 n = atol((char *)eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005965 if (cmdmod.split & WSP_VERT)
5966 {
5967 if (*eap->arg == '-' || *eap->arg == '+')
Bram Moolenaar02631462017-09-22 15:20:32 +02005968 n += curwin->w_width;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005969 else if (n == 0 && eap->arg[0] == NUL) // default is very wide
Bram Moolenaar071d4272004-06-13 20:20:40 +00005970 n = 9999;
5971 win_setwidth_win((int)n, wp);
5972 }
5973 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005974 {
5975 if (*eap->arg == '-' || *eap->arg == '+')
5976 n += curwin->w_height;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005977 else if (n == 0 && eap->arg[0] == NUL) // default is very high
Bram Moolenaar071d4272004-06-13 20:20:40 +00005978 n = 9999;
5979 win_setheight_win((int)n, wp);
5980 }
5981}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005982
5983/*
5984 * ":find [+command] <file>" command.
5985 */
5986 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005987ex_find(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005988{
5989#ifdef FEAT_SEARCHPATH
5990 char_u *fname;
5991 int count;
5992
5993 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg), FNAME_MESS,
5994 TRUE, curbuf->b_ffname);
5995 if (eap->addr_count > 0)
5996 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005997 // Repeat finding the file "count" times. This matters when it
5998 // appears several times in the path.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005999 count = eap->line2;
6000 while (fname != NULL && --count > 0)
6001 {
6002 vim_free(fname);
6003 fname = find_file_in_path(NULL, 0, FNAME_MESS,
6004 FALSE, curbuf->b_ffname);
6005 }
6006 }
6007
6008 if (fname != NULL)
6009 {
6010 eap->arg = fname;
6011#endif
6012 do_exedit(eap, NULL);
6013#ifdef FEAT_SEARCHPATH
6014 vim_free(fname);
6015 }
6016#endif
6017}
6018
6019/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00006020 * ":open" simulation: for now just work like ":visual".
6021 */
6022 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006023ex_open(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00006024{
6025 regmatch_T regmatch;
6026 char_u *p;
6027
6028 curwin->w_cursor.lnum = eap->line2;
6029 beginline(BL_SOL | BL_FIX);
6030 if (*eap->arg == '/')
6031 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006032 // ":open /pattern/": put cursor in column found with pattern
Bram Moolenaardf177f62005-02-22 08:39:57 +00006033 ++eap->arg;
6034 p = skip_regexp(eap->arg, '/', p_magic, NULL);
6035 *p = NUL;
6036 regmatch.regprog = vim_regcomp(eap->arg, p_magic ? RE_MAGIC : 0);
6037 if (regmatch.regprog != NULL)
6038 {
6039 regmatch.rm_ic = p_ic;
6040 p = ml_get_curline();
6041 if (vim_regexec(&regmatch, p, (colnr_T)0))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006042 curwin->w_cursor.col = (colnr_T)(regmatch.startp[0] - p);
Bram Moolenaardf177f62005-02-22 08:39:57 +00006043 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006044 emsg(_(e_nomatch));
Bram Moolenaar473de612013-06-08 18:19:48 +02006045 vim_regfree(regmatch.regprog);
Bram Moolenaardf177f62005-02-22 08:39:57 +00006046 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006047 // Move to the NUL, ignore any other arguments.
Bram Moolenaardf177f62005-02-22 08:39:57 +00006048 eap->arg += STRLEN(eap->arg);
6049 }
6050 check_cursor();
6051
6052 eap->cmdidx = CMD_visual;
6053 do_exedit(eap, NULL);
6054}
6055
6056/*
6057 * ":edit", ":badd", ":visual".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006058 */
6059 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006060ex_edit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006061{
6062 do_exedit(eap, NULL);
6063}
6064
6065/*
6066 * ":edit <file>" command and alikes.
6067 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006068 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006069do_exedit(
6070 exarg_T *eap,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006071 win_T *old_curwin) // curwin before doing a split or NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006072{
6073 int n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006074 int need_hide;
Bram Moolenaardf177f62005-02-22 08:39:57 +00006075 int exmode_was = exmode_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006076
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02006077 if (eap->cmdidx != CMD_pedit && ERROR_IF_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02006078 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006079 /*
6080 * ":vi" command ends Ex mode.
6081 */
6082 if (exmode_active && (eap->cmdidx == CMD_visual
6083 || eap->cmdidx == CMD_view))
6084 {
6085 exmode_active = FALSE;
6086 if (*eap->arg == NUL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00006087 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006088 // Special case: ":global/pat/visual\NLvi-commands"
Bram Moolenaardf177f62005-02-22 08:39:57 +00006089 if (global_busy)
6090 {
6091 int rd = RedrawingDisabled;
6092 int nwr = no_wait_return;
6093 int ms = msg_scroll;
6094#ifdef FEAT_GUI
6095 int he = hold_gui_events;
6096#endif
6097
6098 if (eap->nextcmd != NULL)
6099 {
6100 stuffReadbuff(eap->nextcmd);
6101 eap->nextcmd = NULL;
6102 }
6103
6104 if (exmode_was != EXMODE_VIM)
6105 settmode(TMODE_RAW);
6106 RedrawingDisabled = 0;
6107 no_wait_return = 0;
6108 need_wait_return = FALSE;
6109 msg_scroll = 0;
6110#ifdef FEAT_GUI
6111 hold_gui_events = 0;
6112#endif
6113 must_redraw = CLEAR;
6114
6115 main_loop(FALSE, TRUE);
6116
6117 RedrawingDisabled = rd;
6118 no_wait_return = nwr;
6119 msg_scroll = ms;
6120#ifdef FEAT_GUI
6121 hold_gui_events = he;
6122#endif
6123 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006124 return;
Bram Moolenaardf177f62005-02-22 08:39:57 +00006125 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006126 }
6127
6128 if ((eap->cmdidx == CMD_new
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006129 || eap->cmdidx == CMD_tabnew
6130 || eap->cmdidx == CMD_tabedit
Bram Moolenaar4033c552017-09-16 20:54:51 +02006131 || eap->cmdidx == CMD_vnew) && *eap->arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006132 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006133 // ":new" or ":tabnew" without argument: edit an new empty buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00006134 setpcmark();
6135 (void)do_ecmd(0, NULL, NULL, eap, ECMD_ONE,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00006136 ECMD_HIDE + (eap->forceit ? ECMD_FORCEIT : 0),
6137 old_curwin == NULL ? curwin : NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006138 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02006139 else if ((eap->cmdidx != CMD_split && eap->cmdidx != CMD_vsplit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006140 || *eap->arg != NUL
6141#ifdef FEAT_BROWSE
6142 || cmdmod.browse
6143#endif
6144 )
6145 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006146 // Can't edit another file when "curbuf_lock" is set. Only ":edit"
6147 // can bring us here, others are stopped earlier.
Bram Moolenaar5555acc2006-04-07 21:33:12 +00006148 if (*eap->arg != NUL && curbuf_locked())
6149 return;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006150
Bram Moolenaar071d4272004-06-13 20:20:40 +00006151 n = readonlymode;
6152 if (eap->cmdidx == CMD_view || eap->cmdidx == CMD_sview)
6153 readonlymode = TRUE;
6154 else if (eap->cmdidx == CMD_enew)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006155 readonlymode = FALSE; // 'readonly' doesn't make sense in an
6156 // empty buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00006157 setpcmark();
6158 if (do_ecmd(0, (eap->cmdidx == CMD_enew ? NULL : eap->arg),
6159 NULL, eap,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006160 // ":edit" goes to first line if Vi compatible
Bram Moolenaar071d4272004-06-13 20:20:40 +00006161 (*eap->arg == NUL && eap->do_ecmd_lnum == 0
6162 && vim_strchr(p_cpo, CPO_GOTO1) != NULL)
6163 ? ECMD_ONE : eap->do_ecmd_lnum,
Bram Moolenaareb44a682017-08-03 22:44:55 +02006164 (buf_hide(curbuf) ? ECMD_HIDE : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006165 + (eap->forceit ? ECMD_FORCEIT : 0)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006166 // after a split we can use an existing buffer
Bram Moolenaar7b449342014-03-25 13:03:48 +01006167 + (old_curwin != NULL ? ECMD_OLDBUF : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006168 + (eap->cmdidx == CMD_badd ? ECMD_ADDBUF : 0 )
Bram Moolenaar701f7af2008-11-15 13:12:07 +00006169 , old_curwin == NULL ? curwin : NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006170 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006171 // Editing the file failed. If the window was split, close it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006172 if (old_curwin != NULL)
6173 {
6174 need_hide = (curbufIsChanged() && curbuf->b_nwindows <= 1);
Bram Moolenaareb44a682017-08-03 22:44:55 +02006175 if (!need_hide || buf_hide(curbuf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006176 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006177#if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00006178 cleanup_T cs;
6179
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006180 // Reset the error/interrupt/exception state here so that
6181 // aborting() returns FALSE when closing a window.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00006182 enter_cleanup(&cs);
Bram Moolenaar4033c552017-09-16 20:54:51 +02006183#endif
6184#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00006185 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006186#endif
Bram Moolenaareb44a682017-08-03 22:44:55 +02006187 win_close(curwin, !need_hide && !buf_hide(curbuf));
Bram Moolenaarc0197e22004-09-13 20:26:32 +00006188
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006189#if defined(FEAT_EVAL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006190 // Restore the error/interrupt/exception state if not
6191 // discarded by a new aborting error, interrupt, or
6192 // uncaught exception.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00006193 leave_cleanup(&cs);
Bram Moolenaar4033c552017-09-16 20:54:51 +02006194#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006195 }
6196 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006197 }
6198 else if (readonlymode && curbuf->b_nwindows == 1)
6199 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006200 // When editing an already visited buffer, 'readonly' won't be set
6201 // but the previous value is kept. With ":view" and ":sview" we
6202 // want the file to be readonly, except when another window is
6203 // editing the same buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006204 curbuf->b_p_ro = TRUE;
6205 }
6206 readonlymode = n;
6207 }
6208 else
6209 {
6210 if (eap->do_ecmd_cmd != NULL)
6211 do_cmdline_cmd(eap->do_ecmd_cmd);
6212#ifdef FEAT_TITLE
6213 n = curwin->w_arg_idx_invalid;
6214#endif
6215 check_arg_idx(curwin);
6216#ifdef FEAT_TITLE
6217 if (n != curwin->w_arg_idx_invalid)
6218 maketitle();
6219#endif
6220 }
6221
Bram Moolenaar071d4272004-06-13 20:20:40 +00006222 /*
6223 * if ":split file" worked, set alternate file name in old window to new
6224 * file
6225 */
6226 if (old_curwin != NULL
6227 && *eap->arg != NUL
6228 && curwin != old_curwin
6229 && win_valid(old_curwin)
Bram Moolenaard4755bb2004-09-02 19:12:26 +00006230 && old_curwin->w_buffer != curbuf
6231 && !cmdmod.keepalt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006232 old_curwin->w_alt_fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006233
6234 ex_no_reprint = TRUE;
6235}
6236
6237#ifndef FEAT_GUI
6238/*
6239 * ":gui" and ":gvim" when there is no GUI.
6240 */
6241 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006242ex_nogui(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006243{
6244 eap->errmsg = e_nogvim;
6245}
6246#endif
6247
Bram Moolenaar4f974752019-02-17 17:44:42 +01006248#if defined(FEAT_GUI_MSWIN) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006249 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006250ex_tearoff(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006251{
6252 gui_make_tearoff(eap->arg);
6253}
6254#endif
6255
Bram Moolenaar29a2c082018-03-05 21:06:23 +01006256#if (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK) \
6257 || defined(FEAT_TERM_POPUP_MENU)) && defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006258 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006259ex_popup(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006260{
Bram Moolenaar29a2c082018-03-05 21:06:23 +01006261# if defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK)
6262 if (gui.in_use)
6263 gui_make_popup(eap->arg, eap->forceit);
6264# ifdef FEAT_TERM_POPUP_MENU
6265 else
6266# endif
6267# endif
6268# ifdef FEAT_TERM_POPUP_MENU
6269 pum_make_popup(eap->arg, eap->forceit);
6270# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006271}
6272#endif
6273
Bram Moolenaar071d4272004-06-13 20:20:40 +00006274 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006275ex_swapname(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006276{
6277 if (curbuf->b_ml.ml_mfp == NULL || curbuf->b_ml.ml_mfp->mf_fname == NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01006278 msg(_("No swap file"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006279 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01006280 msg((char *)curbuf->b_ml.ml_mfp->mf_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006281}
6282
6283/*
6284 * ":syncbind" forces all 'scrollbind' windows to have the same relative
6285 * offset.
6286 * (1998-11-02 16:21:01 R. Edward Ralston <eralston@computer.org>)
6287 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006288 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006289ex_syncbind(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006290{
Bram Moolenaar071d4272004-06-13 20:20:40 +00006291 win_T *wp;
Bram Moolenaardedd1b02013-12-14 13:06:17 +01006292 win_T *save_curwin = curwin;
6293 buf_T *save_curbuf = curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006294 long topline;
6295 long y;
6296 linenr_T old_linenr = curwin->w_cursor.lnum;
6297
6298 setpcmark();
6299
6300 /*
6301 * determine max topline
6302 */
6303 if (curwin->w_p_scb)
6304 {
6305 topline = curwin->w_topline;
Bram Moolenaar29323592016-07-24 22:04:11 +02006306 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006307 {
6308 if (wp->w_p_scb && wp->w_buffer)
6309 {
Bram Moolenaar375e3392019-01-31 18:26:10 +01006310 y = wp->w_buffer->b_ml.ml_line_count - get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006311 if (topline > y)
6312 topline = y;
6313 }
6314 }
6315 if (topline < 1)
6316 topline = 1;
6317 }
6318 else
6319 {
6320 topline = 1;
6321 }
6322
6323
6324 /*
Bram Moolenaardedd1b02013-12-14 13:06:17 +01006325 * Set all scrollbind windows to the same topline.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006326 */
Bram Moolenaar29323592016-07-24 22:04:11 +02006327 FOR_ALL_WINDOWS(curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006328 {
6329 if (curwin->w_p_scb)
6330 {
Bram Moolenaardedd1b02013-12-14 13:06:17 +01006331 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006332 y = topline - curwin->w_topline;
6333 if (y > 0)
6334 scrollup(y, TRUE);
6335 else
6336 scrolldown(-y, TRUE);
6337 curwin->w_scbind_pos = topline;
6338 redraw_later(VALID);
6339 cursor_correct();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006340 curwin->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006341 }
6342 }
Bram Moolenaardedd1b02013-12-14 13:06:17 +01006343 curwin = save_curwin;
6344 curbuf = save_curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006345 if (curwin->w_p_scb)
6346 {
6347 did_syncbind = TRUE;
6348 checkpcmark();
6349 if (old_linenr != curwin->w_cursor.lnum)
6350 {
6351 char_u ctrl_o[2];
6352
6353 ctrl_o[0] = Ctrl_O;
6354 ctrl_o[1] = 0;
6355 ins_typebuf(ctrl_o, REMAP_NONE, 0, TRUE, FALSE);
6356 }
6357 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006358}
6359
6360
6361 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006362ex_read(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006363{
Bram Moolenaardf177f62005-02-22 08:39:57 +00006364 int i;
6365 int empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
6366 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006367
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006368 if (eap->usefilter) // :r!cmd
Bram Moolenaar071d4272004-06-13 20:20:40 +00006369 do_bang(1, eap, FALSE, FALSE, TRUE);
6370 else
6371 {
6372 if (u_save(eap->line2, (linenr_T)(eap->line2 + 1)) == FAIL)
6373 return;
6374
6375#ifdef FEAT_BROWSE
6376 if (cmdmod.browse)
6377 {
6378 char_u *browseFile;
6379
Bram Moolenaar7171abe2004-10-11 10:06:20 +00006380 browseFile = do_browse(0, (char_u *)_("Append File"), eap->arg,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006381 NULL, NULL, NULL, curbuf);
6382 if (browseFile != NULL)
6383 {
6384 i = readfile(browseFile, NULL,
6385 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
6386 vim_free(browseFile);
6387 }
6388 else
6389 i = OK;
6390 }
6391 else
6392#endif
6393 if (*eap->arg == NUL)
6394 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006395 if (check_fname() == FAIL) // check for no file name
Bram Moolenaar071d4272004-06-13 20:20:40 +00006396 return;
6397 i = readfile(curbuf->b_ffname, curbuf->b_fname,
6398 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
6399 }
6400 else
6401 {
6402 if (vim_strchr(p_cpo, CPO_ALTREAD) != NULL)
6403 (void)setaltfname(eap->arg, eap->arg, (linenr_T)1);
6404 i = readfile(eap->arg, NULL,
6405 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
6406
6407 }
Bram Moolenaare13b9af2017-01-13 22:01:02 +01006408 if (i != OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006409 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006410#if defined(FEAT_EVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006411 if (!aborting())
6412#endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006413 semsg(_(e_notopen), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006414 }
6415 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00006416 {
6417 if (empty && exmode_active)
6418 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006419 // Delete the empty line that remains. Historically ex does
6420 // this but vi doesn't.
Bram Moolenaardf177f62005-02-22 08:39:57 +00006421 if (eap->line2 == 0)
6422 lnum = curbuf->b_ml.ml_line_count;
6423 else
6424 lnum = 1;
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00006425 if (*ml_get(lnum) == NUL && u_savedel(lnum, 1L) == OK)
Bram Moolenaardf177f62005-02-22 08:39:57 +00006426 {
6427 ml_delete(lnum, FALSE);
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00006428 if (curwin->w_cursor.lnum > 1
6429 && curwin->w_cursor.lnum >= lnum)
Bram Moolenaardf177f62005-02-22 08:39:57 +00006430 --curwin->w_cursor.lnum;
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00006431 deleted_lines_mark(lnum, 1L);
Bram Moolenaardf177f62005-02-22 08:39:57 +00006432 }
6433 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006434 redraw_curbuf_later(VALID);
Bram Moolenaardf177f62005-02-22 08:39:57 +00006435 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006436 }
6437}
6438
Bram Moolenaarea408852005-06-25 22:49:46 +00006439static char_u *prev_dir = NULL;
6440
6441#if defined(EXITFREE) || defined(PROTO)
6442 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006443free_cd_dir(void)
Bram Moolenaarea408852005-06-25 22:49:46 +00006444{
Bram Moolenaard23a8232018-02-10 18:45:26 +01006445 VIM_CLEAR(prev_dir);
6446 VIM_CLEAR(globaldir);
Bram Moolenaarea408852005-06-25 22:49:46 +00006447}
6448#endif
6449
Bram Moolenaarf4258302013-06-02 18:20:17 +02006450/*
6451 * Deal with the side effects of changing the current directory.
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006452 * When 'scope' is CDSCOPE_TABPAGE then this was after an ":tcd" command.
6453 * When 'scope' is CDSCOPE_WINDOW then this was after an ":lcd" command.
Bram Moolenaarf4258302013-06-02 18:20:17 +02006454 */
6455 void
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006456post_chdir(cdscope_T scope)
Bram Moolenaarf4258302013-06-02 18:20:17 +02006457{
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006458 if (scope != CDSCOPE_WINDOW)
Bram Moolenaar00aa0692019-04-27 20:37:57 +02006459 // Clear tab local directory for both :cd and :tcd
6460 VIM_CLEAR(curtab->tp_localdir);
Bram Moolenaard23a8232018-02-10 18:45:26 +01006461 VIM_CLEAR(curwin->w_localdir);
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006462 if (scope != CDSCOPE_GLOBAL)
Bram Moolenaarf4258302013-06-02 18:20:17 +02006463 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006464 // If still in global directory, need to remember current
6465 // directory as global directory.
Bram Moolenaarf4258302013-06-02 18:20:17 +02006466 if (globaldir == NULL && prev_dir != NULL)
6467 globaldir = vim_strsave(prev_dir);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006468 // Remember this local directory for the window.
Bram Moolenaarf4258302013-06-02 18:20:17 +02006469 if (mch_dirname(NameBuff, MAXPATHL) == OK)
Bram Moolenaar00aa0692019-04-27 20:37:57 +02006470 {
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006471 if (scope == CDSCOPE_TABPAGE)
Bram Moolenaar00aa0692019-04-27 20:37:57 +02006472 curtab->tp_localdir = vim_strsave(NameBuff);
6473 else
6474 curwin->w_localdir = vim_strsave(NameBuff);
6475 }
Bram Moolenaarf4258302013-06-02 18:20:17 +02006476 }
6477 else
6478 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006479 // We are now in the global directory, no need to remember its
6480 // name.
Bram Moolenaard23a8232018-02-10 18:45:26 +01006481 VIM_CLEAR(globaldir);
Bram Moolenaarf4258302013-06-02 18:20:17 +02006482 }
6483
6484 shorten_fnames(TRUE);
6485}
6486
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006487/*
6488 * Change directory function used by :cd/:tcd/:lcd Ex commands and the
6489 * chdir() function. If 'winlocaldir' is TRUE, then changes the window-local
6490 * directory. If 'tablocaldir' is TRUE, then changes the tab-local directory.
6491 * Otherwise changes the global directory.
6492 * Returns TRUE if the directory is successfully changed.
6493 */
6494 int
6495changedir_func(
6496 char_u *new_dir,
6497 int forceit,
6498 cdscope_T scope)
6499{
6500 char_u *tofree;
6501 int dir_differs;
6502 int retval = FALSE;
6503
6504 if (allbuf_locked())
6505 return FALSE;
6506
6507 if (vim_strchr(p_cpo, CPO_CHDIR) != NULL && curbufIsChanged() && !forceit)
6508 {
6509 emsg(_("E747: Cannot change directory, buffer is modified (add ! to override)"));
6510 return FALSE;
6511 }
6512
6513 // ":cd -": Change to previous directory
6514 if (STRCMP(new_dir, "-") == 0)
6515 {
6516 if (prev_dir == NULL)
6517 {
6518 emsg(_("E186: No previous directory"));
6519 return FALSE;
6520 }
6521 new_dir = prev_dir;
6522 }
6523
6524 // Save current directory for next ":cd -"
6525 tofree = prev_dir;
6526 if (mch_dirname(NameBuff, MAXPATHL) == OK)
6527 prev_dir = vim_strsave(NameBuff);
6528 else
6529 prev_dir = NULL;
6530
6531#if defined(UNIX) || defined(VMS)
6532 // for UNIX ":cd" means: go to home directory
6533 if (*new_dir == NUL)
6534 {
6535 // use NameBuff for home directory name
6536# ifdef VMS
6537 char_u *p;
6538
6539 p = mch_getenv((char_u *)"SYS$LOGIN");
6540 if (p == NULL || *p == NUL) // empty is the same as not set
6541 NameBuff[0] = NUL;
6542 else
6543 vim_strncpy(NameBuff, p, MAXPATHL - 1);
6544# else
6545 expand_env((char_u *)"$HOME", NameBuff, MAXPATHL);
6546# endif
6547 new_dir = NameBuff;
6548 }
6549#endif
6550 dir_differs = new_dir == NULL || prev_dir == NULL
6551 || pathcmp((char *)prev_dir, (char *)new_dir, -1) != 0;
6552 if (new_dir == NULL || (dir_differs && vim_chdir(new_dir)))
6553 emsg(_(e_failed));
6554 else
6555 {
6556 char_u *acmd_fname;
6557
6558 post_chdir(scope);
6559
6560 if (dir_differs)
6561 {
6562 if (scope == CDSCOPE_WINDOW)
6563 acmd_fname = (char_u *)"window";
6564 else if (scope == CDSCOPE_TABPAGE)
6565 acmd_fname = (char_u *)"tabpage";
6566 else
6567 acmd_fname = (char_u *)"global";
6568 apply_autocmds(EVENT_DIRCHANGED, acmd_fname, new_dir, FALSE,
6569 curbuf);
6570 }
6571 retval = TRUE;
6572 }
6573 vim_free(tofree);
6574
6575 return retval;
6576}
Bram Moolenaarea408852005-06-25 22:49:46 +00006577
Bram Moolenaar071d4272004-06-13 20:20:40 +00006578/*
Bram Moolenaar00aa0692019-04-27 20:37:57 +02006579 * ":cd", ":tcd", ":lcd", ":chdir" ":tchdir" and ":lchdir".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006580 */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00006581 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006582ex_cd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006583{
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01006584 char_u *new_dir;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006585
6586 new_dir = eap->arg;
6587#if !defined(UNIX) && !defined(VMS)
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006588 // for non-UNIX ":cd" means: print current directory
Bram Moolenaar071d4272004-06-13 20:20:40 +00006589 if (*new_dir == NUL)
6590 ex_pwd(NULL);
6591 else
6592#endif
6593 {
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006594 cdscope_T scope = CDSCOPE_GLOBAL;
6595
6596 if (eap->cmdidx == CMD_lcd || eap->cmdidx == CMD_lchdir)
6597 scope = CDSCOPE_WINDOW;
6598 else if (eap->cmdidx == CMD_tcd || eap->cmdidx == CMD_tchdir)
6599 scope = CDSCOPE_TABPAGE;
6600
6601 if (changedir_func(new_dir, eap->forceit, scope))
Bram Moolenaardf177f62005-02-22 08:39:57 +00006602 {
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006603 // Echo the new current directory if the command was typed.
Bram Moolenaarfcfbc672009-07-09 18:13:49 +00006604 if (KeyTyped || p_verbose >= 5)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006605 ex_pwd(eap);
6606 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006607 }
6608}
6609
6610/*
6611 * ":pwd".
6612 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006613 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006614ex_pwd(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006615{
6616 if (mch_dirname(NameBuff, MAXPATHL) == OK)
6617 {
6618#ifdef BACKSLASH_IN_FILENAME
6619 slash_adjust(NameBuff);
6620#endif
Bram Moolenaar32526b32019-01-19 17:43:09 +01006621 msg((char *)NameBuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006622 }
6623 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006624 emsg(_("E187: Unknown"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006625}
6626
6627/*
6628 * ":=".
6629 */
6630 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006631ex_equal(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006632{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006633 smsg("%ld", (long)eap->line2);
Bram Moolenaardf177f62005-02-22 08:39:57 +00006634 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006635}
6636
6637 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006638ex_sleep(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006639{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006640 int n;
6641 long len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006642
6643 if (cursor_valid())
6644 {
6645 n = W_WINROW(curwin) + curwin->w_wrow - msg_scrolled;
6646 if (n >= 0)
Bram Moolenaar53f81742017-09-22 14:35:51 +02006647 windgoto((int)n, curwin->w_wincol + curwin->w_wcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006648 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006649
6650 len = eap->line2;
6651 switch (*eap->arg)
6652 {
6653 case 'm': break;
6654 case NUL: len *= 1000L; break;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006655 default: semsg(_(e_invarg2), eap->arg); return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006656 }
6657 do_sleep(len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006658}
6659
6660/*
6661 * Sleep for "msec" milliseconds, but keep checking for a CTRL-C every second.
6662 */
6663 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006664do_sleep(long msec)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006665{
Bram Moolenaar52953192019-08-16 10:27:13 +02006666 long done = 0;
Bram Moolenaar975b5272016-03-15 23:10:59 +01006667 long wait_now;
Bram Moolenaar52953192019-08-16 10:27:13 +02006668# ifdef ELAPSED_FUNC
6669 elapsed_T start_tv;
6670
6671 // Remember at what time we started, so that we know how much longer we
6672 // should wait after waiting for a bit.
6673 ELAPSED_INIT(start_tv);
6674# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006675
6676 cursor_on();
Bram Moolenaarf45d7472018-09-30 18:22:26 +02006677 out_flush_cursor(FALSE, FALSE);
Bram Moolenaar52953192019-08-16 10:27:13 +02006678 while (!got_int && done < msec)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006679 {
Bram Moolenaar975b5272016-03-15 23:10:59 +01006680 wait_now = msec - done > 1000L ? 1000L : msec - done;
6681#ifdef FEAT_TIMERS
6682 {
6683 long due_time = check_due_timer();
6684
6685 if (due_time > 0 && due_time < wait_now)
6686 wait_now = due_time;
6687 }
6688#endif
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02006689#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar28e67e02019-08-15 23:05:49 +02006690 if (has_any_channel() && wait_now > 20L)
6691 wait_now = 20L;
6692#endif
6693#ifdef FEAT_SOUND
6694 if (has_any_sound_callback() && wait_now > 20L)
6695 wait_now = 20L;
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02006696#endif
Bram Moolenaar975b5272016-03-15 23:10:59 +01006697 ui_delay(wait_now, TRUE);
Bram Moolenaar52953192019-08-16 10:27:13 +02006698
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02006699#ifdef FEAT_JOB_CHANNEL
6700 if (has_any_channel())
6701 ui_breakcheck_force(TRUE);
6702 else
6703#endif
6704 ui_breakcheck();
Bram Moolenaar93c88e02015-09-15 14:12:05 +02006705#ifdef MESSAGE_QUEUE
Bram Moolenaar52953192019-08-16 10:27:13 +02006706 // Process the netbeans and clientserver messages that may have been
6707 // received in the call to ui_breakcheck() when the GUI is in use. This
6708 // may occur when running a test case.
Bram Moolenaar93c88e02015-09-15 14:12:05 +02006709 parse_queued_messages();
Bram Moolenaare3cc6d42011-10-20 21:58:34 +02006710#endif
Bram Moolenaar52953192019-08-16 10:27:13 +02006711
6712# ifdef ELAPSED_FUNC
6713 // actual time passed
6714 done = ELAPSED_FUNC(start_tv);
6715# else
6716 // guestimate time passed (will actually be more)
6717 done += wait_now;
6718# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006719 }
Bram Moolenaar1ebff3d2018-07-07 16:18:13 +02006720
6721 // If CTRL-C was typed to interrupt the sleep, drop the CTRL-C from the
6722 // input buffer, otherwise a following call to input() fails.
6723 if (got_int)
6724 (void)vpeekc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006725}
6726
Bram Moolenaar071d4272004-06-13 20:20:40 +00006727/*
6728 * ":winsize" command (obsolete).
6729 */
6730 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006731ex_winsize(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006732{
6733 int w, h;
6734 char_u *arg = eap->arg;
6735 char_u *p;
6736
6737 w = getdigits(&arg);
6738 arg = skipwhite(arg);
6739 p = arg;
6740 h = getdigits(&arg);
6741 if (*p != NUL && *arg == NUL)
6742 set_shellsize(w, h, TRUE);
6743 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006744 emsg(_("E465: :winsize requires two number arguments"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006745}
6746
Bram Moolenaar071d4272004-06-13 20:20:40 +00006747 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006748ex_wincmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006749{
6750 int xchar = NUL;
6751 char_u *p;
6752
6753 if (*eap->arg == 'g' || *eap->arg == Ctrl_G)
6754 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006755 // CTRL-W g and CTRL-W CTRL-G have an extra command character
Bram Moolenaar071d4272004-06-13 20:20:40 +00006756 if (eap->arg[1] == NUL)
6757 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006758 emsg(_(e_invarg));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006759 return;
6760 }
6761 xchar = eap->arg[1];
6762 p = eap->arg + 2;
6763 }
6764 else
6765 p = eap->arg + 1;
6766
6767 eap->nextcmd = check_nextcmd(p);
6768 p = skipwhite(p);
6769 if (*p != NUL && *p != '"' && eap->nextcmd == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006770 emsg(_(e_invarg));
Bram Moolenaar12bde492011-06-13 01:19:56 +02006771 else if (!eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006772 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006773 // Pass flags on for ":vertical wincmd ]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006774 postponed_split_flags = cmdmod.split;
Bram Moolenaard326ce82007-03-11 14:48:29 +00006775 postponed_split_tab = cmdmod.tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006776 do_window(*eap->arg, eap->addr_count > 0 ? eap->line2 : 0L, xchar);
6777 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00006778 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006779 }
6780}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006781
Bram Moolenaar843ee412004-06-30 16:16:41 +00006782#if defined(FEAT_GUI) || defined(UNIX) || defined(VMS) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006783/*
6784 * ":winpos".
6785 */
6786 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006787ex_winpos(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006788{
6789 int x, y;
6790 char_u *arg = eap->arg;
6791 char_u *p;
6792
6793 if (*arg == NUL)
6794 {
Bram Moolenaar843ee412004-06-30 16:16:41 +00006795# if defined(FEAT_GUI) || defined(MSWIN)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02006796# ifdef VIMDLL
6797 if (gui.in_use ? gui_mch_get_winpos(&x, &y) != FAIL :
6798 mch_get_winpos(&x, &y) != FAIL)
6799# elif defined(FEAT_GUI)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006800 if (gui.in_use && gui_mch_get_winpos(&x, &y) != FAIL)
Bram Moolenaar843ee412004-06-30 16:16:41 +00006801# else
6802 if (mch_get_winpos(&x, &y) != FAIL)
6803# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006804 {
6805 sprintf((char *)IObuff, _("Window position: X %d, Y %d"), x, y);
Bram Moolenaar32526b32019-01-19 17:43:09 +01006806 msg((char *)IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006807 }
6808 else
6809# endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006810 emsg(_("E188: Obtaining window position not implemented for this platform"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006811 }
6812 else
6813 {
6814 x = getdigits(&arg);
6815 arg = skipwhite(arg);
6816 p = arg;
6817 y = getdigits(&arg);
6818 if (*p == NUL || *arg != NUL)
6819 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006820 emsg(_("E466: :winpos requires two number arguments"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006821 return;
6822 }
6823# ifdef FEAT_GUI
6824 if (gui.in_use)
6825 gui_mch_set_winpos(x, y);
6826 else if (gui.starting)
6827 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006828 // Remember the coordinates for when the window is opened.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006829 gui_win_x = x;
6830 gui_win_y = y;
6831 }
Bram Moolenaarafde13b2019-04-28 19:46:49 +02006832# if defined(HAVE_TGETENT) || defined(VIMDLL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006833 else
6834# endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02006835# endif
6836# if defined(MSWIN) && (!defined(FEAT_GUI) || defined(VIMDLL))
Bram Moolenaar843ee412004-06-30 16:16:41 +00006837 mch_set_winpos(x, y);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006838# endif
6839# ifdef HAVE_TGETENT
6840 if (*T_CWP)
6841 term_set_winpos(x, y);
6842# endif
6843 }
6844}
6845#endif
6846
6847/*
6848 * Handle command that work like operators: ":delete", ":yank", ":>" and ":<".
6849 */
6850 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006851ex_operators(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006852{
6853 oparg_T oa;
6854
6855 clear_oparg(&oa);
6856 oa.regname = eap->regname;
6857 oa.start.lnum = eap->line1;
6858 oa.end.lnum = eap->line2;
6859 oa.line_count = eap->line2 - eap->line1 + 1;
6860 oa.motion_type = MLINE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006861 virtual_op = FALSE;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006862 if (eap->cmdidx != CMD_yank) // position cursor for undo
Bram Moolenaar071d4272004-06-13 20:20:40 +00006863 {
6864 setpcmark();
6865 curwin->w_cursor.lnum = eap->line1;
6866 beginline(BL_SOL | BL_FIX);
6867 }
6868
Bram Moolenaard07c6e12013-11-21 14:21:40 +01006869 if (VIsual_active)
6870 end_visual_mode();
Bram Moolenaard07c6e12013-11-21 14:21:40 +01006871
Bram Moolenaar071d4272004-06-13 20:20:40 +00006872 switch (eap->cmdidx)
6873 {
6874 case CMD_delete:
6875 oa.op_type = OP_DELETE;
6876 op_delete(&oa);
6877 break;
6878
6879 case CMD_yank:
6880 oa.op_type = OP_YANK;
6881 (void)op_yank(&oa, FALSE, TRUE);
6882 break;
6883
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006884 default: // CMD_rshift or CMD_lshift
Bram Moolenaar6e707362013-06-14 19:15:58 +02006885 if (
Bram Moolenaar071d4272004-06-13 20:20:40 +00006886#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6e707362013-06-14 19:15:58 +02006887 (eap->cmdidx == CMD_rshift) ^ curwin->w_p_rl
6888#else
6889 eap->cmdidx == CMD_rshift
Bram Moolenaar071d4272004-06-13 20:20:40 +00006890#endif
Bram Moolenaar6e707362013-06-14 19:15:58 +02006891 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006892 oa.op_type = OP_RSHIFT;
6893 else
6894 oa.op_type = OP_LSHIFT;
6895 op_shift(&oa, FALSE, eap->amount);
6896 break;
6897 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006898 virtual_op = MAYBE;
Bram Moolenaardf177f62005-02-22 08:39:57 +00006899 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006900}
6901
6902/*
6903 * ":put".
6904 */
6905 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006906ex_put(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006907{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006908 // ":0put" works like ":1put!".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006909 if (eap->line2 == 0)
6910 {
6911 eap->line2 = 1;
6912 eap->forceit = TRUE;
6913 }
6914 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaardf177f62005-02-22 08:39:57 +00006915 do_put(eap->regname, eap->forceit ? BACKWARD : FORWARD, 1L,
6916 PUT_LINE|PUT_CURSLINE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006917}
6918
6919/*
6920 * Handle ":copy" and ":move".
6921 */
6922 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006923ex_copymove(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006924{
6925 long n;
6926
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02006927 n = get_address(eap, &eap->arg, eap->addr_type, FALSE, FALSE, FALSE, 1);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006928 if (eap->arg == NULL) // error detected
Bram Moolenaar071d4272004-06-13 20:20:40 +00006929 {
6930 eap->nextcmd = NULL;
6931 return;
6932 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00006933 get_flags(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006934
6935 /*
6936 * move or copy lines from 'eap->line1'-'eap->line2' to below line 'n'
6937 */
6938 if (n == MAXLNUM || n < 0 || n > curbuf->b_ml.ml_line_count)
6939 {
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02006940 emsg(_(e_invrange));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006941 return;
6942 }
6943
6944 if (eap->cmdidx == CMD_move)
6945 {
6946 if (do_move(eap->line1, eap->line2, n) == FAIL)
6947 return;
6948 }
6949 else
6950 ex_copy(eap->line1, eap->line2, n);
6951 u_clearline();
6952 beginline(BL_SOL | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00006953 ex_may_print(eap);
6954}
6955
6956/*
6957 * Print the current line if flags were given to the Ex command.
6958 */
Bram Moolenaarfd3fe982014-04-01 17:49:44 +02006959 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006960ex_may_print(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00006961{
6962 if (eap->flags != 0)
6963 {
6964 print_line(curwin->w_cursor.lnum, (eap->flags & EXFLAG_NR),
6965 (eap->flags & EXFLAG_LIST));
6966 ex_no_reprint = TRUE;
6967 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006968}
6969
6970/*
6971 * ":smagic" and ":snomagic".
6972 */
6973 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006974ex_submagic(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006975{
6976 int magic_save = p_magic;
6977
6978 p_magic = (eap->cmdidx == CMD_smagic);
6979 do_sub(eap);
6980 p_magic = magic_save;
6981}
6982
6983/*
6984 * ":join".
6985 */
6986 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006987ex_join(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006988{
6989 curwin->w_cursor.lnum = eap->line1;
6990 if (eap->line1 == eap->line2)
6991 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006992 if (eap->addr_count >= 2) // :2,2join does nothing
Bram Moolenaar071d4272004-06-13 20:20:40 +00006993 return;
6994 if (eap->line2 == curbuf->b_ml.ml_line_count)
6995 {
6996 beep_flush();
6997 return;
6998 }
6999 ++eap->line2;
7000 }
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02007001 (void)do_join(eap->line2 - eap->line1 + 1, !eap->forceit, TRUE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007002 beginline(BL_WHITE | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007003 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007004}
7005
7006/*
7007 * ":[addr]@r" or ":[addr]*r": execute register
7008 */
7009 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007010ex_at(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007011{
7012 int c;
Bram Moolenaar60462872009-11-03 11:40:19 +00007013 int prev_len = typebuf.tb_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007014
7015 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaar4930a762016-09-11 14:39:53 +02007016 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007017
7018#ifdef USE_ON_FLY_SCROLL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007019 dont_scroll = TRUE; // disallow scrolling here
Bram Moolenaar071d4272004-06-13 20:20:40 +00007020#endif
7021
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007022 // get the register name. No name means to use the previous one
Bram Moolenaar071d4272004-06-13 20:20:40 +00007023 c = *eap->arg;
7024 if (c == NUL || (c == '*' && *eap->cmd == '*'))
7025 c = '@';
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007026 // Put the register in the typeahead buffer with the "silent" flag.
Bram Moolenaard333d1e2006-11-07 17:43:47 +00007027 if (do_execreg(c, TRUE, vim_strchr(p_cpo, CPO_EXECBUF) != NULL, TRUE)
7028 == FAIL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007029 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007030 beep_flush();
Bram Moolenaardf177f62005-02-22 08:39:57 +00007031 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007032 else
7033 {
7034 int save_efr = exec_from_reg;
7035
7036 exec_from_reg = TRUE;
7037
7038 /*
7039 * Execute from the typeahead buffer.
Bram Moolenaar60462872009-11-03 11:40:19 +00007040 * Continue until the stuff buffer is empty and all added characters
7041 * have been consumed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007042 */
Bram Moolenaar60462872009-11-03 11:40:19 +00007043 while (!stuff_empty() || typebuf.tb_len > prev_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007044 (void)do_cmdline(NULL, getexline, NULL, DOCMD_NOWAIT|DOCMD_VERBOSE);
7045
7046 exec_from_reg = save_efr;
7047 }
7048}
7049
7050/*
7051 * ":!".
7052 */
7053 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007054ex_bang(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007055{
7056 do_bang(eap->addr_count, eap, eap->forceit, TRUE, TRUE);
7057}
7058
7059/*
7060 * ":undo".
7061 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007062 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +01007063ex_undo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007064{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007065 if (eap->addr_count == 1) // :undo 123
Bram Moolenaar730cde92010-06-27 05:18:54 +02007066 undo_time(eap->line2, FALSE, FALSE, TRUE);
Bram Moolenaard3667a22006-03-16 21:35:52 +00007067 else
7068 u_undo(1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007069}
7070
Bram Moolenaar55debbe2010-05-23 23:34:36 +02007071#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar6df6f472010-07-18 18:04:50 +02007072 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007073ex_wundo(exarg_T *eap)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02007074{
7075 char_u hash[UNDO_HASH_SIZE];
7076
7077 u_compute_hash(hash);
7078 u_write_undo(eap->arg, eap->forceit, curbuf, hash);
7079}
7080
Bram Moolenaar6df6f472010-07-18 18:04:50 +02007081 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007082ex_rundo(exarg_T *eap)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02007083{
7084 char_u hash[UNDO_HASH_SIZE];
7085
7086 u_compute_hash(hash);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02007087 u_read_undo(eap->arg, hash, NULL);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02007088}
7089#endif
7090
Bram Moolenaar071d4272004-06-13 20:20:40 +00007091/*
7092 * ":redo".
7093 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007094 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007095ex_redo(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007096{
7097 u_redo(1);
7098}
7099
7100/*
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007101 * ":earlier" and ":later".
7102 */
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007103 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007104ex_later(exarg_T *eap)
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007105{
7106 long count = 0;
7107 int sec = FALSE;
Bram Moolenaar730cde92010-06-27 05:18:54 +02007108 int file = FALSE;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007109 char_u *p = eap->arg;
7110
7111 if (*p == NUL)
7112 count = 1;
7113 else if (isdigit(*p))
7114 {
7115 count = getdigits(&p);
7116 switch (*p)
7117 {
7118 case 's': ++p; sec = TRUE; break;
7119 case 'm': ++p; sec = TRUE; count *= 60; break;
7120 case 'h': ++p; sec = TRUE; count *= 60 * 60; break;
Bram Moolenaar730cde92010-06-27 05:18:54 +02007121 case 'd': ++p; sec = TRUE; count *= 24 * 60 * 60; break;
7122 case 'f': ++p; file = TRUE; break;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007123 }
7124 }
7125
7126 if (*p != NUL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007127 semsg(_(e_invarg2), eap->arg);
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007128 else
Bram Moolenaar730cde92010-06-27 05:18:54 +02007129 undo_time(eap->cmdidx == CMD_earlier ? -count : count,
7130 sec, file, FALSE);
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007131}
7132
7133/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007134 * ":redir": start/stop redirection.
7135 */
7136 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007137ex_redir(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007138{
7139 char *mode;
7140 char_u *fname;
Bram Moolenaarca472992005-01-21 11:46:23 +00007141 char_u *arg = eap->arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007142
Bram Moolenaarba768492016-07-08 15:32:54 +02007143#ifdef FEAT_EVAL
Bram Moolenaar79815f12016-07-09 17:07:29 +02007144 if (redir_execute)
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02007145 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007146 emsg(_("E930: Cannot use :redir inside execute()"));
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02007147 return;
7148 }
Bram Moolenaarba768492016-07-08 15:32:54 +02007149#endif
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02007150
Bram Moolenaar071d4272004-06-13 20:20:40 +00007151 if (STRICMP(eap->arg, "END") == 0)
7152 close_redir();
7153 else
7154 {
Bram Moolenaarca472992005-01-21 11:46:23 +00007155 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00007156 {
Bram Moolenaarca472992005-01-21 11:46:23 +00007157 ++arg;
7158 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00007159 {
Bram Moolenaarca472992005-01-21 11:46:23 +00007160 ++arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007161 mode = "a";
7162 }
7163 else
7164 mode = "w";
Bram Moolenaarca472992005-01-21 11:46:23 +00007165 arg = skipwhite(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007166
7167 close_redir();
7168
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007169 // Expand environment variables and "~/".
Bram Moolenaarca472992005-01-21 11:46:23 +00007170 fname = expand_env_save(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007171 if (fname == NULL)
7172 return;
7173#ifdef FEAT_BROWSE
7174 if (cmdmod.browse)
7175 {
7176 char_u *browseFile;
7177
Bram Moolenaar7171abe2004-10-11 10:06:20 +00007178 browseFile = do_browse(BROWSE_SAVE,
7179 (char_u *)_("Save Redirection"),
Bram Moolenaarc36651b2018-04-29 12:22:56 +02007180 fname, NULL, NULL,
7181 (char_u *)_(BROWSE_FILTER_ALL_FILES), curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007182 if (browseFile == NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007183 return; // operation cancelled
Bram Moolenaar071d4272004-06-13 20:20:40 +00007184 vim_free(fname);
7185 fname = browseFile;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007186 eap->forceit = TRUE; // since dialog already asked
Bram Moolenaar071d4272004-06-13 20:20:40 +00007187 }
7188#endif
7189
7190 redir_fd = open_exfile(fname, eap->forceit, mode);
7191 vim_free(fname);
7192 }
7193#ifdef FEAT_EVAL
Bram Moolenaarca472992005-01-21 11:46:23 +00007194 else if (*arg == '@')
Bram Moolenaar071d4272004-06-13 20:20:40 +00007195 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007196 // redirect to a register a-z (resp. A-Z for appending)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007197 close_redir();
Bram Moolenaarca472992005-01-21 11:46:23 +00007198 ++arg;
7199 if (ASCII_ISALPHA(*arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007200# ifdef FEAT_CLIPBOARD
Bram Moolenaarca472992005-01-21 11:46:23 +00007201 || *arg == '*'
Bram Moolenaar9a51c6e2006-11-14 19:25:02 +00007202 || *arg == '+'
Bram Moolenaar071d4272004-06-13 20:20:40 +00007203# endif
Bram Moolenaarca472992005-01-21 11:46:23 +00007204 || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00007205 {
Bram Moolenaarca472992005-01-21 11:46:23 +00007206 redir_reg = *arg++;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007207 if (*arg == '>' && arg[1] == '>') // append
Bram Moolenaard9d30582005-05-18 22:10:28 +00007208 arg += 2;
Bram Moolenaarc188b882007-10-19 14:20:54 +00007209 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007210 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007211 // Can use both "@a" and "@a>".
Bram Moolenaar2c29bee2005-06-01 21:46:07 +00007212 if (*arg == '>')
7213 arg++;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007214 // Make register empty when not using @A-@Z and the
7215 // command is valid.
Bram Moolenaarc188b882007-10-19 14:20:54 +00007216 if (*arg == NUL && !isupper(redir_reg))
7217 write_reg_contents(redir_reg, (char_u *)"", -1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007218 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00007219 }
7220 if (*arg != NUL)
7221 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00007222 redir_reg = 0;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007223 semsg(_(e_invarg2), eap->arg);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007224 }
7225 }
7226 else if (*arg == '=' && arg[1] == '>')
7227 {
7228 int append;
7229
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007230 // redirect to a variable
Bram Moolenaardf177f62005-02-22 08:39:57 +00007231 close_redir();
7232 arg += 2;
7233
7234 if (*arg == '>')
7235 {
7236 ++arg;
7237 append = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007238 }
7239 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00007240 append = FALSE;
7241
7242 if (var_redir_start(skipwhite(arg), append) == OK)
7243 redir_vname = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007244 }
7245#endif
7246
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007247 // TODO: redirect to a buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00007248
Bram Moolenaar071d4272004-06-13 20:20:40 +00007249 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007250 semsg(_(e_invarg2), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007251 }
Bram Moolenaar29b2d262006-09-10 19:07:28 +00007252
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007253 // Make sure redirection is not off. Can happen for cmdline completion
7254 // that indirectly invokes a command to catch its output.
Bram Moolenaar29b2d262006-09-10 19:07:28 +00007255 if (redir_fd != NULL
7256#ifdef FEAT_EVAL
7257 || redir_reg || redir_vname
7258#endif
7259 )
7260 redir_off = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007261}
7262
7263/*
7264 * ":redraw": force redraw
7265 */
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01007266 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007267ex_redraw(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007268{
7269 int r = RedrawingDisabled;
7270 int p = p_lz;
7271
7272 RedrawingDisabled = 0;
7273 p_lz = FALSE;
Bram Moolenaarabc39ab2017-03-01 18:04:05 +01007274 validate_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007275 update_topline();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007276 update_screen(eap->forceit ? CLEAR : VIsual_active ? INVERTED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007277#ifdef FEAT_TITLE
7278 if (need_maketitle)
7279 maketitle();
7280#endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007281#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
7282# ifdef VIMDLL
7283 if (!gui.in_use)
7284# endif
7285 resize_console_buf();
Bram Moolenaar78d21da2019-02-17 15:00:52 +01007286#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007287 RedrawingDisabled = r;
7288 p_lz = p;
7289
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007290 // Reset msg_didout, so that a message that's there is overwritten.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007291 msg_didout = FALSE;
7292 msg_col = 0;
7293
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007294 // No need to wait after an intentional redraw.
Bram Moolenaar56667a52013-07-17 11:54:28 +02007295 need_wait_return = FALSE;
7296
Bram Moolenaar071d4272004-06-13 20:20:40 +00007297 out_flush();
7298}
7299
7300/*
7301 * ":redrawstatus": force redraw of status line(s)
7302 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007303 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007304ex_redrawstatus(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007305{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007306 int r = RedrawingDisabled;
7307 int p = p_lz;
7308
7309 RedrawingDisabled = 0;
7310 p_lz = FALSE;
7311 if (eap->forceit)
7312 status_redraw_all();
7313 else
7314 status_redraw_curbuf();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007315 update_screen(VIsual_active ? INVERTED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007316 RedrawingDisabled = r;
7317 p_lz = p;
7318 out_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007319}
7320
Bram Moolenaare12bab32019-01-08 22:02:56 +01007321/*
7322 * ":redrawtabline": force redraw of the tabline
7323 */
7324 static void
7325ex_redrawtabline(exarg_T *eap UNUSED)
7326{
7327 int r = RedrawingDisabled;
7328 int p = p_lz;
7329
7330 RedrawingDisabled = 0;
7331 p_lz = FALSE;
7332
7333 draw_tabline();
7334
7335 RedrawingDisabled = r;
7336 p_lz = p;
7337 out_flush();
7338}
7339
Bram Moolenaar071d4272004-06-13 20:20:40 +00007340 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007341close_redir(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007342{
7343 if (redir_fd != NULL)
7344 {
7345 fclose(redir_fd);
7346 redir_fd = NULL;
7347 }
7348#ifdef FEAT_EVAL
7349 redir_reg = 0;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007350 if (redir_vname)
7351 {
7352 var_redir_stop();
7353 redir_vname = 0;
7354 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007355#endif
7356}
7357
Bram Moolenaarf96ae0b2019-07-28 15:21:55 +02007358#if (defined(FEAT_SESSION) || defined(FEAT_EVAL)) || defined(PROTO)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007359 int
Bram Moolenaarbd67aac2019-09-21 23:09:04 +02007360vim_mkdir_emsg(char_u *name, int prot UNUSED)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007361{
7362 if (vim_mkdir(name, prot) != 0)
7363 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007364 semsg(_("E739: Cannot create directory: %s"), name);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007365 return FAIL;
7366 }
7367 return OK;
7368}
7369#endif
7370
Bram Moolenaar071d4272004-06-13 20:20:40 +00007371/*
7372 * Open a file for writing for an Ex command, with some checks.
7373 * Return file descriptor, or NULL on failure.
7374 */
7375 FILE *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007376open_exfile(
7377 char_u *fname,
7378 int forceit,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007379 char *mode) // "w" for create new file or "a" for append
Bram Moolenaar071d4272004-06-13 20:20:40 +00007380{
7381 FILE *fd;
7382
7383#ifdef UNIX
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007384 // with Unix it is possible to open a directory
Bram Moolenaar071d4272004-06-13 20:20:40 +00007385 if (mch_isdir(fname))
7386 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007387 semsg(_(e_isadir2), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007388 return NULL;
7389 }
7390#endif
7391 if (!forceit && *mode != 'a' && vim_fexists(fname))
7392 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007393 semsg(_("E189: \"%s\" exists (add ! to override)"), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007394 return NULL;
7395 }
7396
7397 if ((fd = mch_fopen((char *)fname, mode)) == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007398 semsg(_("E190: Cannot open \"%s\" for writing"), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007399
7400 return fd;
7401}
7402
7403/*
7404 * ":mark" and ":k".
7405 */
7406 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007407ex_mark(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007408{
7409 pos_T pos;
7410
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007411 if (*eap->arg == NUL) // No argument?
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007412 emsg(_(e_argreq));
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007413 else if (eap->arg[1] != NUL) // more than one character?
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007414 emsg(_(e_trailing));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007415 else
7416 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007417 pos = curwin->w_cursor; // save curwin->w_cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00007418 curwin->w_cursor.lnum = eap->line2;
7419 beginline(BL_WHITE | BL_FIX);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007420 if (setmark(*eap->arg) == FAIL) // set mark
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007421 emsg(_("E191: Argument must be a letter or forward/backward quote"));
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007422 curwin->w_cursor = pos; // restore curwin->w_cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00007423 }
7424}
7425
7426/*
7427 * Update w_topline, w_leftcol and the cursor position.
7428 */
7429 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007430update_topline_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007431{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007432 check_cursor(); // put cursor on valid line
Bram Moolenaar071d4272004-06-13 20:20:40 +00007433 update_topline();
7434 if (!curwin->w_p_wrap)
7435 validate_cursor();
7436 update_curswant();
7437}
7438
Bram Moolenaar071d4272004-06-13 20:20:40 +00007439/*
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007440 * Save the current State and go to Normal mode.
7441 * Return TRUE if the typeahead could be saved.
7442 */
7443 int
7444save_current_state(save_state_T *sst)
7445{
7446 sst->save_msg_scroll = msg_scroll;
7447 sst->save_restart_edit = restart_edit;
7448 sst->save_msg_didout = msg_didout;
7449 sst->save_State = State;
7450 sst->save_insertmode = p_im;
7451 sst->save_finish_op = finish_op;
7452 sst->save_opcount = opcount;
Bram Moolenaarcce713d2019-03-04 11:40:12 +01007453 sst->save_reg_executing = reg_executing;
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007454
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007455 msg_scroll = FALSE; // no msg scrolling in Normal mode
7456 restart_edit = 0; // don't go to Insert mode
7457 p_im = FALSE; // don't use 'insertmode'
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007458
7459 /*
7460 * Save the current typeahead. This is required to allow using ":normal"
7461 * from an event handler and makes sure we don't hang when the argument
7462 * ends with half a command.
7463 */
7464 save_typeahead(&sst->tabuf);
7465 return sst->tabuf.typebuf_valid;
7466}
7467
7468 void
7469restore_current_state(save_state_T *sst)
7470{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007471 // Restore the previous typeahead.
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007472 restore_typeahead(&sst->tabuf);
7473
7474 msg_scroll = sst->save_msg_scroll;
7475 restart_edit = sst->save_restart_edit;
7476 p_im = sst->save_insertmode;
7477 finish_op = sst->save_finish_op;
7478 opcount = sst->save_opcount;
Bram Moolenaarcce713d2019-03-04 11:40:12 +01007479 reg_executing = sst->save_reg_executing;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007480 msg_didout |= sst->save_msg_didout; // don't reset msg_didout now
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007481
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007482 // Restore the state (needed when called from a function executed for
7483 // 'indentexpr'). Update the mouse and cursor, they may have changed.
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007484 State = sst->save_State;
7485#ifdef CURSOR_SHAPE
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007486 ui_cursor_shape(); // may show different cursor shape
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007487#endif
7488}
7489
7490/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007491 * ":normal[!] {commands}": Execute normal mode commands.
7492 */
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01007493 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007494ex_normal(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007495{
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007496 save_state_T save_state;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007497 char_u *arg = NULL;
7498 int l;
7499 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007500
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00007501 if (ex_normal_lock > 0)
7502 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007503 emsg(_(e_secure));
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00007504 return;
7505 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007506 if (ex_normal_busy >= p_mmd)
7507 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007508 emsg(_("E192: Recursive use of :normal too deep"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007509 return;
7510 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007511
Bram Moolenaar071d4272004-06-13 20:20:40 +00007512 /*
7513 * vgetc() expects a CSI and K_SPECIAL to have been escaped. Don't do
7514 * this for the K_SPECIAL leading byte, otherwise special keys will not
7515 * work.
7516 */
7517 if (has_mbyte)
7518 {
7519 int len = 0;
7520
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007521 // Count the number of characters to be escaped.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007522 for (p = eap->arg; *p != NUL; ++p)
7523 {
Bram Moolenaar13505972019-01-24 15:04:48 +01007524#ifdef FEAT_GUI
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007525 if (*p == CSI) // leadbyte CSI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007526 len += 2;
Bram Moolenaar13505972019-01-24 15:04:48 +01007527#endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007528 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007529 if (*++p == K_SPECIAL // trailbyte K_SPECIAL or CSI
Bram Moolenaar13505972019-01-24 15:04:48 +01007530#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007531 || *p == CSI
Bram Moolenaar13505972019-01-24 15:04:48 +01007532#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007533 )
7534 len += 2;
7535 }
7536 if (len > 0)
7537 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02007538 arg = alloc(STRLEN(eap->arg) + len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007539 if (arg != NULL)
7540 {
7541 len = 0;
7542 for (p = eap->arg; *p != NUL; ++p)
7543 {
7544 arg[len++] = *p;
Bram Moolenaar13505972019-01-24 15:04:48 +01007545#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007546 if (*p == CSI)
7547 {
7548 arg[len++] = KS_EXTRA;
7549 arg[len++] = (int)KE_CSI;
7550 }
Bram Moolenaar13505972019-01-24 15:04:48 +01007551#endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007552 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007553 {
7554 arg[len++] = *++p;
7555 if (*p == K_SPECIAL)
7556 {
7557 arg[len++] = KS_SPECIAL;
7558 arg[len++] = KE_FILLER;
7559 }
Bram Moolenaar13505972019-01-24 15:04:48 +01007560#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007561 else if (*p == CSI)
7562 {
7563 arg[len++] = KS_EXTRA;
7564 arg[len++] = (int)KE_CSI;
7565 }
Bram Moolenaar13505972019-01-24 15:04:48 +01007566#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007567 }
7568 arg[len] = NUL;
7569 }
7570 }
7571 }
7572 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007573
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007574 ++ex_normal_busy;
7575 if (save_current_state(&save_state))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007576 {
7577 /*
7578 * Repeat the :normal command for each line in the range. When no
7579 * range given, execute it just once, without positioning the cursor
7580 * first.
7581 */
7582 do
7583 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007584 if (eap->addr_count != 0)
7585 {
7586 curwin->w_cursor.lnum = eap->line1++;
7587 curwin->w_cursor.col = 0;
Bram Moolenaard5d37532017-03-27 23:02:07 +02007588 check_cursor_moved(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007589 }
7590
Bram Moolenaar13505972019-01-24 15:04:48 +01007591 exec_normal_cmd(arg != NULL
7592 ? arg
7593 : eap->arg, eap->forceit ? REMAP_NONE : REMAP_YES, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007594 }
7595 while (eap->addr_count > 0 && eap->line1 <= eap->line2 && !got_int);
7596 }
7597
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007598 // Might not return to the main loop when in an event handler.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007599 update_topline_cursor();
7600
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007601 restore_current_state(&save_state);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007602 --ex_normal_busy;
Bram Moolenaareda73602014-11-05 09:53:23 +01007603 setmouse();
Bram Moolenaareda73602014-11-05 09:53:23 +01007604#ifdef CURSOR_SHAPE
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007605 ui_cursor_shape(); // may show different cursor shape
Bram Moolenaareda73602014-11-05 09:53:23 +01007606#endif
7607
Bram Moolenaar071d4272004-06-13 20:20:40 +00007608 vim_free(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007609}
7610
7611/*
Bram Moolenaar64969662005-12-14 21:59:55 +00007612 * ":startinsert", ":startreplace" and ":startgreplace"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007613 */
7614 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007615ex_startinsert(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007616{
Bram Moolenaarfd371682005-01-14 21:42:54 +00007617 if (eap->forceit)
7618 {
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02007619 // cursor line can be zero on startup
Bram Moolenaar09ca9322017-09-26 17:40:45 +02007620 if (!curwin->w_cursor.lnum)
7621 curwin->w_cursor.lnum = 1;
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02007622 set_cursor_for_append_to_line();
Bram Moolenaarfd371682005-01-14 21:42:54 +00007623 }
7624
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02007625 // Ignore the command when already in Insert mode. Inserting an
7626 // expression register that invokes a function can do this.
Bram Moolenaara40c5002005-01-09 21:16:21 +00007627 if (State & INSERT)
7628 return;
7629
Bram Moolenaar64969662005-12-14 21:59:55 +00007630 if (eap->cmdidx == CMD_startinsert)
7631 restart_edit = 'a';
7632 else if (eap->cmdidx == CMD_startreplace)
7633 restart_edit = 'R';
Bram Moolenaar071d4272004-06-13 20:20:40 +00007634 else
Bram Moolenaar64969662005-12-14 21:59:55 +00007635 restart_edit = 'V';
7636
7637 if (!eap->forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007638 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00007639 if (eap->cmdidx == CMD_startinsert)
7640 restart_edit = 'i';
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02007641 curwin->w_curswant = 0; // avoid MAXCOL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007642 }
7643}
7644
7645/*
7646 * ":stopinsert"
7647 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007648 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007649ex_stopinsert(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007650{
7651 restart_edit = 0;
7652 stop_insert_mode = TRUE;
Bram Moolenaarfd773e92016-04-02 19:39:16 +02007653 clearmode();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007654}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007655
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00007656/*
7657 * Execute normal mode command "cmd".
7658 * "remap" can be REMAP_NONE or REMAP_YES.
7659 */
7660 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007661exec_normal_cmd(char_u *cmd, int remap, int silent)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00007662{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007663 // Stuff the argument into the typeahead buffer.
Bram Moolenaar25281632016-01-21 23:32:32 +01007664 ins_typebuf(cmd, remap, 0, TRUE, silent);
Bram Moolenaar586c70c2018-10-02 16:23:58 +02007665 exec_normal(FALSE, FALSE, FALSE);
Bram Moolenaar25281632016-01-21 23:32:32 +01007666}
Bram Moolenaar25281632016-01-21 23:32:32 +01007667
Bram Moolenaar25281632016-01-21 23:32:32 +01007668/*
7669 * Execute normal_cmd() until there is no typeahead left.
Bram Moolenaar586c70c2018-10-02 16:23:58 +02007670 * When "use_vpeekc" is TRUE use vpeekc() to check for available chars.
Bram Moolenaar25281632016-01-21 23:32:32 +01007671 */
7672 void
Bram Moolenaar586c70c2018-10-02 16:23:58 +02007673exec_normal(int was_typed, int use_vpeekc, int may_use_terminal_loop UNUSED)
Bram Moolenaar25281632016-01-21 23:32:32 +01007674{
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00007675 oparg_T oa;
Bram Moolenaar905dd902019-04-07 14:21:47 +02007676 int c;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00007677
Bram Moolenaar905dd902019-04-07 14:21:47 +02007678 // When calling vpeekc() from feedkeys() it will return Ctrl_C when there
7679 // is nothing to get, so also check for Ctrl_C.
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00007680 clear_oparg(&oa);
7681 finish_op = FALSE;
Bram Moolenaar586c70c2018-10-02 16:23:58 +02007682 while ((!stuff_empty()
7683 || ((was_typed || !typebuf_typed()) && typebuf.tb_len > 0)
Bram Moolenaar905dd902019-04-07 14:21:47 +02007684 || (use_vpeekc && (c = vpeekc()) != NUL && c != Ctrl_C))
Bram Moolenaar586c70c2018-10-02 16:23:58 +02007685 && !got_int)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00007686 {
7687 update_topline_cursor();
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02007688#ifdef FEAT_TERMINAL
Bram Moolenaar655a82a2018-05-08 22:01:07 +02007689 if (may_use_terminal_loop && term_use_loop()
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02007690 && oa.op_type == OP_NOP && oa.regname == NUL
7691 && !VIsual_active)
7692 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007693 // If terminal_loop() returns OK we got a key that is handled
7694 // in Normal model. With FAIL we first need to position the
7695 // cursor and the screen needs to be redrawn.
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02007696 if (terminal_loop(TRUE) == OK)
7697 normal_cmd(&oa, TRUE);
7698 }
7699 else
7700#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007701 // execute a Normal mode cmd
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02007702 normal_cmd(&oa, TRUE);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00007703 }
7704}
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00007705
Bram Moolenaar071d4272004-06-13 20:20:40 +00007706#ifdef FEAT_FIND_ID
7707 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007708ex_checkpath(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007709{
7710 find_pattern_in_path(NULL, 0, 0, FALSE, FALSE, CHECK_PATH, 1L,
7711 eap->forceit ? ACTION_SHOW_ALL : ACTION_SHOW,
7712 (linenr_T)1, (linenr_T)MAXLNUM);
7713}
7714
Bram Moolenaar4033c552017-09-16 20:54:51 +02007715#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007716/*
7717 * ":psearch"
7718 */
7719 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007720ex_psearch(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007721{
7722 g_do_tagpreview = p_pvh;
7723 ex_findpat(eap);
7724 g_do_tagpreview = 0;
7725}
7726#endif
7727
7728 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007729ex_findpat(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007730{
7731 int whole = TRUE;
7732 long n;
7733 char_u *p;
7734 int action;
7735
7736 switch (cmdnames[eap->cmdidx].cmd_name[2])
7737 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007738 case 'e': // ":psearch", ":isearch" and ":dsearch"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007739 if (cmdnames[eap->cmdidx].cmd_name[0] == 'p')
7740 action = ACTION_GOTO;
7741 else
7742 action = ACTION_SHOW;
7743 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007744 case 'i': // ":ilist" and ":dlist"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007745 action = ACTION_SHOW_ALL;
7746 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007747 case 'u': // ":ijump" and ":djump"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007748 action = ACTION_GOTO;
7749 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007750 default: // ":isplit" and ":dsplit"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007751 action = ACTION_SPLIT;
7752 break;
7753 }
7754
7755 n = 1;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007756 if (vim_isdigit(*eap->arg)) // get count
Bram Moolenaar071d4272004-06-13 20:20:40 +00007757 {
7758 n = getdigits(&eap->arg);
7759 eap->arg = skipwhite(eap->arg);
7760 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007761 if (*eap->arg == '/') // Match regexp, not just whole words
Bram Moolenaar071d4272004-06-13 20:20:40 +00007762 {
7763 whole = FALSE;
7764 ++eap->arg;
7765 p = skip_regexp(eap->arg, '/', p_magic, NULL);
7766 if (*p)
7767 {
7768 *p++ = NUL;
7769 p = skipwhite(p);
7770
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007771 // Check for trailing illegal characters
Bram Moolenaar071d4272004-06-13 20:20:40 +00007772 if (!ends_excmd(*p))
7773 eap->errmsg = e_trailing;
7774 else
7775 eap->nextcmd = check_nextcmd(p);
7776 }
7777 }
7778 if (!eap->skip)
7779 find_pattern_in_path(eap->arg, 0, (int)STRLEN(eap->arg),
7780 whole, !eap->forceit,
7781 *eap->cmd == 'd' ? FIND_DEFINE : FIND_ANY,
7782 n, action, eap->line1, eap->line2);
7783}
7784#endif
7785
Bram Moolenaar071d4272004-06-13 20:20:40 +00007786
Bram Moolenaar4033c552017-09-16 20:54:51 +02007787#ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00007788/*
7789 * ":ptag", ":ptselect", ":ptjump", ":ptnext", etc.
7790 */
7791 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007792ex_ptag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007793{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007794 g_do_tagpreview = p_pvh; // will be reset to 0 in ex_tag_cmd()
Bram Moolenaar071d4272004-06-13 20:20:40 +00007795 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
7796}
7797
7798/*
7799 * ":pedit"
7800 */
7801 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007802ex_pedit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007803{
7804 win_T *curwin_save = curwin;
7805
Bram Moolenaar026587b2019-08-17 15:08:00 +02007806 // Open the preview window or popup and make it the current window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007807 g_do_tagpreview = p_pvh;
Bram Moolenaar576a4a62019-08-18 15:25:17 +02007808 prepare_tagpreview(TRUE, TRUE, FALSE);
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02007809
Bram Moolenaar026587b2019-08-17 15:08:00 +02007810 // Edit the file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007811 do_exedit(eap, NULL);
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02007812
Bram Moolenaar071d4272004-06-13 20:20:40 +00007813 if (curwin != curwin_save && win_valid(curwin_save))
7814 {
Bram Moolenaar026587b2019-08-17 15:08:00 +02007815 // Return cursor to where we were
Bram Moolenaar071d4272004-06-13 20:20:40 +00007816 validate_cursor();
7817 redraw_later(VALID);
7818 win_enter(curwin_save, TRUE);
7819 }
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01007820# ifdef FEAT_PROP_POPUP
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02007821 else if (WIN_IS_POPUP(curwin))
7822 {
7823 // can't keep focus in popup window
7824 win_enter(firstwin, TRUE);
7825 }
7826# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007827 g_do_tagpreview = 0;
7828}
Bram Moolenaar4033c552017-09-16 20:54:51 +02007829#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007830
7831/*
7832 * ":stag", ":stselect" and ":stjump".
7833 */
7834 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007835ex_stag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007836{
7837 postponed_split = -1;
7838 postponed_split_flags = cmdmod.split;
Bram Moolenaard326ce82007-03-11 14:48:29 +00007839 postponed_split_tab = cmdmod.tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007840 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
7841 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00007842 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007843}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007844
7845/*
7846 * ":tag", ":tselect", ":tjump", ":tnext", etc.
7847 */
7848 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007849ex_tag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007850{
7851 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name);
7852}
7853
7854 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007855ex_tag_cmd(exarg_T *eap, char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007856{
7857 int cmd;
7858
7859 switch (name[1])
7860 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007861 case 'j': cmd = DT_JUMP; // ":tjump"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007862 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007863 case 's': cmd = DT_SELECT; // ":tselect"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007864 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007865 case 'p': cmd = DT_PREV; // ":tprevious"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007866 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007867 case 'N': cmd = DT_PREV; // ":tNext"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007868 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007869 case 'n': cmd = DT_NEXT; // ":tnext"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007870 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007871 case 'o': cmd = DT_POP; // ":pop"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007872 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007873 case 'f': // ":tfirst"
7874 case 'r': cmd = DT_FIRST; // ":trewind"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007875 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007876 case 'l': cmd = DT_LAST; // ":tlast"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007877 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007878 default: // ":tag"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007879#ifdef FEAT_CSCOPE
Bram Moolenaar7c94c262008-06-20 09:11:34 +00007880 if (p_cst && *eap->arg != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007881 {
Bram Moolenaard4db7712016-11-12 19:16:46 +01007882 ex_cstag(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007883 return;
7884 }
7885#endif
7886 cmd = DT_TAG;
7887 break;
7888 }
7889
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00007890 if (name[0] == 'l')
7891 {
7892#ifndef FEAT_QUICKFIX
7893 ex_ni(eap);
7894 return;
7895#else
7896 cmd = DT_LTAG;
7897#endif
7898 }
7899
Bram Moolenaar071d4272004-06-13 20:20:40 +00007900 do_tag(eap->arg, cmd, eap->addr_count > 0 ? (int)eap->line2 : 1,
7901 eap->forceit, TRUE);
7902}
7903
7904/*
Bram Moolenaar05bb9532008-07-04 09:44:11 +00007905 * Check "str" for starting with a special cmdline variable.
7906 * If found return one of the SPEC_ values and set "*usedlen" to the length of
7907 * the variable. Otherwise return -1 and "*usedlen" is unchanged.
7908 */
7909 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007910find_cmdline_var(char_u *src, int *usedlen)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00007911{
7912 int len;
7913 int i;
Bram Moolenaar8f0b2d42009-05-16 14:41:10 +00007914 static char *(spec_str[]) = {
Bram Moolenaar05bb9532008-07-04 09:44:11 +00007915 "%",
7916#define SPEC_PERC 0
7917 "#",
Bram Moolenaar65f08472017-09-10 18:16:20 +02007918#define SPEC_HASH (SPEC_PERC + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007919 "<cword>", // cursor word
Bram Moolenaar65f08472017-09-10 18:16:20 +02007920#define SPEC_CWORD (SPEC_HASH + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007921 "<cWORD>", // cursor WORD
Bram Moolenaar65f08472017-09-10 18:16:20 +02007922#define SPEC_CCWORD (SPEC_CWORD + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007923 "<cexpr>", // expr under cursor
Bram Moolenaar65f08472017-09-10 18:16:20 +02007924#define SPEC_CEXPR (SPEC_CCWORD + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007925 "<cfile>", // cursor path name
Bram Moolenaar65f08472017-09-10 18:16:20 +02007926#define SPEC_CFILE (SPEC_CEXPR + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007927 "<sfile>", // ":so" file name
Bram Moolenaar65f08472017-09-10 18:16:20 +02007928#define SPEC_SFILE (SPEC_CFILE + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007929 "<slnum>", // ":so" file line number
Bram Moolenaar65f08472017-09-10 18:16:20 +02007930#define SPEC_SLNUM (SPEC_SFILE + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007931 "<afile>", // autocommand file name
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02007932#define SPEC_AFILE (SPEC_SLNUM + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007933 "<abuf>", // autocommand buffer number
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02007934#define SPEC_ABUF (SPEC_AFILE + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007935 "<amatch>", // autocommand match name
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007936#define SPEC_AMATCH (SPEC_ABUF + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007937 "<sflnum>", // script file line number
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02007938#define SPEC_SFLNUM (SPEC_AMATCH + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00007939#ifdef FEAT_CLIENTSERVER
7940 "<client>"
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02007941# define SPEC_CLIENT (SPEC_SFLNUM + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00007942#endif
7943 };
Bram Moolenaar05bb9532008-07-04 09:44:11 +00007944
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00007945 for (i = 0; i < (int)(sizeof(spec_str) / sizeof(char *)); ++i)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00007946 {
7947 len = (int)STRLEN(spec_str[i]);
7948 if (STRNCMP(src, spec_str[i], len) == 0)
7949 {
7950 *usedlen = len;
7951 return i;
7952 }
7953 }
7954 return -1;
7955}
7956
7957/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007958 * Evaluate cmdline variables.
7959 *
7960 * change '%' to curbuf->b_ffname
7961 * '#' to curwin->w_altfile
7962 * '<cword>' to word under the cursor
7963 * '<cWORD>' to WORD under the cursor
Bram Moolenaar8071cb22019-07-12 17:58:01 +02007964 * '<cexpr>' to C-expression under the cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00007965 * '<cfile>' to path name under the cursor
7966 * '<sfile>' to sourced file name
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01007967 * '<slnum>' to sourced file line number
Bram Moolenaar071d4272004-06-13 20:20:40 +00007968 * '<afile>' to file name for autocommand
7969 * '<abuf>' to buffer number for autocommand
7970 * '<amatch>' to matching name for autocommand
7971 *
7972 * When an error is detected, "errormsg" is set to a non-NULL pointer (may be
7973 * "" for error without a message) and NULL is returned.
7974 * Returns an allocated string if a valid match was found.
7975 * Returns NULL if no match was found. "usedlen" then still contains the
7976 * number of characters to skip.
7977 */
7978 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007979eval_vars(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007980 char_u *src, // pointer into commandline
7981 char_u *srcstart, // beginning of valid memory for src
7982 int *usedlen, // characters after src that are used
7983 linenr_T *lnump, // line number for :e command, or NULL
7984 char **errormsg, // pointer to error message
7985 int *escaped) // return value has escaped white space (can
7986 // be NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007987{
7988 int i;
7989 char_u *s;
7990 char_u *result;
7991 char_u *resultbuf = NULL;
7992 int resultlen;
7993 buf_T *buf;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007994 int valid = VALID_HEAD + VALID_PATH; // assume valid result
Bram Moolenaar071d4272004-06-13 20:20:40 +00007995 int spec_idx;
Bram Moolenaarfd249462018-07-28 16:14:30 +02007996 int tilde_file = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007997 int skip_mod = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007998 char_u strbuf[30];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007999
8000 *errormsg = NULL;
Bram Moolenaar63b92542007-03-27 14:57:09 +00008001 if (escaped != NULL)
8002 *escaped = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008003
8004 /*
8005 * Check if there is something to do.
8006 */
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008007 spec_idx = find_cmdline_var(src, usedlen);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008008 if (spec_idx < 0) // no match
Bram Moolenaar071d4272004-06-13 20:20:40 +00008009 {
8010 *usedlen = 1;
8011 return NULL;
8012 }
8013
8014 /*
8015 * Skip when preceded with a backslash "\%" and "\#".
8016 * Note: In "\\%" the % is also not recognized!
8017 */
8018 if (src > srcstart && src[-1] == '\\')
8019 {
8020 *usedlen = 0;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008021 STRMOVE(src - 1, src); // remove backslash
Bram Moolenaar071d4272004-06-13 20:20:40 +00008022 return NULL;
8023 }
8024
8025 /*
8026 * word or WORD under cursor
8027 */
Bram Moolenaar65f08472017-09-10 18:16:20 +02008028 if (spec_idx == SPEC_CWORD || spec_idx == SPEC_CCWORD
8029 || spec_idx == SPEC_CEXPR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008030 {
Bram Moolenaar65f08472017-09-10 18:16:20 +02008031 resultlen = find_ident_under_cursor(&result,
8032 spec_idx == SPEC_CWORD ? (FIND_IDENT | FIND_STRING)
8033 : spec_idx == SPEC_CEXPR ? (FIND_IDENT | FIND_STRING | FIND_EVAL)
8034 : FIND_STRING);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008035 if (resultlen == 0)
8036 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008037 *errormsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +00008038 return NULL;
8039 }
8040 }
8041
8042 /*
8043 * '#': Alternate file name
8044 * '%': Current file name
8045 * File name under the cursor
8046 * File name for autocommand
8047 * and following modifiers
8048 */
8049 else
8050 {
8051 switch (spec_idx)
8052 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008053 case SPEC_PERC: // '%': current file
Bram Moolenaar071d4272004-06-13 20:20:40 +00008054 if (curbuf->b_fname == NULL)
8055 {
8056 result = (char_u *)"";
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008057 valid = 0; // Must have ":p:h" to be valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00008058 }
8059 else
Bram Moolenaar00136dc2018-07-25 21:19:13 +02008060 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008061 result = curbuf->b_fname;
Bram Moolenaar00136dc2018-07-25 21:19:13 +02008062 tilde_file = STRCMP(result, "~") == 0;
8063 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008064 break;
8065
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008066 case SPEC_HASH: // '#' or "#99": alternate file
8067 if (src[1] == '#') // "##": the argument list
Bram Moolenaar071d4272004-06-13 20:20:40 +00008068 {
8069 result = arg_all();
8070 resultbuf = result;
8071 *usedlen = 2;
Bram Moolenaar63b92542007-03-27 14:57:09 +00008072 if (escaped != NULL)
8073 *escaped = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008074 skip_mod = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008075 break;
8076 }
8077 s = src + 1;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008078 if (*s == '<') // "#<99" uses v:oldfiles
Bram Moolenaard812df62008-11-09 12:46:09 +00008079 ++s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008080 i = (int)getdigits(&s);
Bram Moolenaarc312b8b2017-10-28 17:53:04 +02008081 if (s == src + 2 && src[1] == '-')
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008082 // just a minus sign, don't skip over it
Bram Moolenaarc312b8b2017-10-28 17:53:04 +02008083 s--;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008084 *usedlen = (int)(s - src); // length of what we expand
Bram Moolenaar071d4272004-06-13 20:20:40 +00008085
Bram Moolenaarc312b8b2017-10-28 17:53:04 +02008086 if (src[1] == '<' && i != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008087 {
Bram Moolenaard812df62008-11-09 12:46:09 +00008088 if (*usedlen < 2)
8089 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008090 // Should we give an error message for #<text?
Bram Moolenaard812df62008-11-09 12:46:09 +00008091 *usedlen = 1;
8092 return NULL;
8093 }
8094#ifdef FEAT_EVAL
8095 result = list_find_str(get_vim_var_list(VV_OLDFILES),
8096 (long)i);
8097 if (result == NULL)
8098 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008099 *errormsg = "";
Bram Moolenaard812df62008-11-09 12:46:09 +00008100 return NULL;
8101 }
8102#else
Bram Moolenaar8e481e82019-01-15 20:07:48 +01008103 *errormsg = _("E809: #< is not available without the +eval feature");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008104 return NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +00008105#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008106 }
8107 else
Bram Moolenaard812df62008-11-09 12:46:09 +00008108 {
Bram Moolenaarc312b8b2017-10-28 17:53:04 +02008109 if (i == 0 && src[1] == '<' && *usedlen > 1)
8110 *usedlen = 1;
Bram Moolenaard812df62008-11-09 12:46:09 +00008111 buf = buflist_findnr(i);
8112 if (buf == NULL)
8113 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008114 *errormsg = _("E194: No alternate file name to substitute for '#'");
Bram Moolenaard812df62008-11-09 12:46:09 +00008115 return NULL;
8116 }
8117 if (lnump != NULL)
8118 *lnump = ECMD_LAST;
8119 if (buf->b_fname == NULL)
8120 {
8121 result = (char_u *)"";
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008122 valid = 0; // Must have ":p:h" to be valid
Bram Moolenaard812df62008-11-09 12:46:09 +00008123 }
8124 else
Bram Moolenaar00136dc2018-07-25 21:19:13 +02008125 {
Bram Moolenaard812df62008-11-09 12:46:09 +00008126 result = buf->b_fname;
Bram Moolenaar00136dc2018-07-25 21:19:13 +02008127 tilde_file = STRCMP(result, "~") == 0;
8128 }
Bram Moolenaard812df62008-11-09 12:46:09 +00008129 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008130 break;
8131
8132#ifdef FEAT_SEARCHPATH
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008133 case SPEC_CFILE: // file name under cursor
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008134 result = file_name_at_cursor(FNAME_MESS|FNAME_HYP, 1L, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008135 if (result == NULL)
8136 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008137 *errormsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +00008138 return NULL;
8139 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008140 resultbuf = result; // remember allocated string
Bram Moolenaar071d4272004-06-13 20:20:40 +00008141 break;
8142#endif
8143
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008144 case SPEC_AFILE: // file name for autocommand
Bram Moolenaar071d4272004-06-13 20:20:40 +00008145 result = autocmd_fname;
Bram Moolenaarf6dad432008-09-18 19:29:58 +00008146 if (result != NULL && !autocmd_fname_full)
8147 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008148 // Still need to turn the fname into a full path. It is
8149 // postponed to avoid a delay when <afile> is not used.
Bram Moolenaarf6dad432008-09-18 19:29:58 +00008150 autocmd_fname_full = TRUE;
8151 result = FullName_save(autocmd_fname, FALSE);
8152 vim_free(autocmd_fname);
8153 autocmd_fname = result;
8154 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008155 if (result == NULL)
8156 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008157 *errormsg = _("E495: no autocommand file name to substitute for \"<afile>\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008158 return NULL;
8159 }
Bram Moolenaara0174af2008-01-02 20:08:25 +00008160 result = shorten_fname1(result);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008161 break;
8162
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008163 case SPEC_ABUF: // buffer number for autocommand
Bram Moolenaar071d4272004-06-13 20:20:40 +00008164 if (autocmd_bufnr <= 0)
8165 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008166 *errormsg = _("E496: no autocommand buffer number to substitute for \"<abuf>\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008167 return NULL;
8168 }
8169 sprintf((char *)strbuf, "%d", autocmd_bufnr);
8170 result = strbuf;
8171 break;
8172
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008173 case SPEC_AMATCH: // match name for autocommand
Bram Moolenaar071d4272004-06-13 20:20:40 +00008174 result = autocmd_match;
8175 if (result == NULL)
8176 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008177 *errormsg = _("E497: no autocommand match name to substitute for \"<amatch>\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008178 return NULL;
8179 }
8180 break;
8181
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008182 case SPEC_SFILE: // file name for ":so" command
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01008183 result = estack_sfile();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008184 if (result == NULL)
8185 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008186 *errormsg = _("E498: no :source file name to substitute for \"<sfile>\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008187 return NULL;
8188 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01008189 resultbuf = result; // remember allocated string
Bram Moolenaar071d4272004-06-13 20:20:40 +00008190 break;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008191
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008192 case SPEC_SLNUM: // line in file for ":so" command
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01008193 if (SOURCING_NAME == NULL || SOURCING_LNUM == 0)
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01008194 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008195 *errormsg = _("E842: no line number to use for \"<slnum>\"");
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01008196 return NULL;
8197 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01008198 sprintf((char *)strbuf, "%ld", SOURCING_LNUM);
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01008199 result = strbuf;
8200 break;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008201
8202#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008203 case SPEC_SFLNUM: // line in script file
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01008204 if (current_sctx.sc_lnum + SOURCING_LNUM == 0)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008205 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008206 *errormsg = _("E961: no line number to use for \"<sflnum>\"");
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008207 return NULL;
8208 }
8209 sprintf((char *)strbuf, "%ld",
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01008210 (long)(current_sctx.sc_lnum + SOURCING_LNUM));
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008211 result = strbuf;
8212 break;
8213#endif
8214
8215#ifdef FEAT_CLIENTSERVER
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008216 case SPEC_CLIENT: // Source of last submitted input
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008217 sprintf((char *)strbuf, PRINTF_HEX_LONG_U,
8218 (long_u)clientWindow);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008219 result = strbuf;
8220 break;
8221#endif
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008222
Bram Moolenaar9e0f6ec2017-05-16 09:36:54 +02008223 default:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008224 result = (char_u *)""; // avoid gcc warning
Bram Moolenaar9e0f6ec2017-05-16 09:36:54 +02008225 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008226 }
8227
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008228 resultlen = (int)STRLEN(result); // length of new string
8229 if (src[*usedlen] == '<') // remove the file name extension
Bram Moolenaar071d4272004-06-13 20:20:40 +00008230 {
8231 ++*usedlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008232 if ((s = vim_strrchr(result, '.')) != NULL && s >= gettail(result))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008233 resultlen = (int)(s - result);
8234 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008235 else if (!skip_mod)
8236 {
Bram Moolenaar00136dc2018-07-25 21:19:13 +02008237 valid |= modify_fname(src, tilde_file, usedlen, &result, &resultbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008238 &resultlen);
8239 if (result == NULL)
8240 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008241 *errormsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +00008242 return NULL;
8243 }
8244 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008245 }
8246
8247 if (resultlen == 0 || valid != VALID_HEAD + VALID_PATH)
8248 {
8249 if (valid != VALID_HEAD + VALID_PATH)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008250 // xgettext:no-c-format
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008251 *errormsg = _("E499: Empty file name for '%' or '#', only works with \":p:h\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008252 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008253 *errormsg = _("E500: Evaluates to an empty string");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008254 result = NULL;
8255 }
8256 else
8257 result = vim_strnsave(result, resultlen);
8258 vim_free(resultbuf);
8259 return result;
8260}
8261
8262/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008263 * Expand the <sfile> string in "arg".
8264 *
8265 * Returns an allocated string, or NULL for any error.
8266 */
8267 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008268expand_sfile(char_u *arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008269{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008270 char *errormsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008271 int len;
8272 char_u *result;
8273 char_u *newres;
8274 char_u *repl;
8275 int srclen;
8276 char_u *p;
8277
8278 result = vim_strsave(arg);
8279 if (result == NULL)
8280 return NULL;
8281
8282 for (p = result; *p; )
8283 {
8284 if (STRNCMP(p, "<sfile>", 7) != 0)
8285 ++p;
8286 else
8287 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008288 // replace "<sfile>" with the sourced file name, and do ":" stuff
Bram Moolenaar63b92542007-03-27 14:57:09 +00008289 repl = eval_vars(p, result, &srclen, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008290 if (errormsg != NULL)
8291 {
8292 if (*errormsg)
8293 emsg(errormsg);
8294 vim_free(result);
8295 return NULL;
8296 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008297 if (repl == NULL) // no match (cannot happen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008298 {
8299 p += srclen;
8300 continue;
8301 }
8302 len = (int)STRLEN(result) - srclen + (int)STRLEN(repl) + 1;
8303 newres = alloc(len);
8304 if (newres == NULL)
8305 {
8306 vim_free(repl);
8307 vim_free(result);
8308 return NULL;
8309 }
8310 mch_memmove(newres, result, (size_t)(p - result));
8311 STRCPY(newres + (p - result), repl);
8312 len = (int)STRLEN(newres);
8313 STRCAT(newres, p + srclen);
8314 vim_free(repl);
8315 vim_free(result);
8316 result = newres;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008317 p = newres + len; // continue after the match
Bram Moolenaar071d4272004-06-13 20:20:40 +00008318 }
8319 }
8320
8321 return result;
8322}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008323
Bram Moolenaar071d4272004-06-13 20:20:40 +00008324#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG) || defined(PROTO)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00008325/*
Bram Moolenaard9462e32011-04-11 21:35:11 +02008326 * Make a dialog message in "buff[DIALOG_MSG_SIZE]".
Bram Moolenaarb765d632005-06-07 21:00:02 +00008327 * "format" must contain "%s".
Bram Moolenaar9c13b352005-05-19 20:53:52 +00008328 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008329 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008330dialog_msg(char_u *buff, char *format, char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008331{
Bram Moolenaar071d4272004-06-13 20:20:40 +00008332 if (fname == NULL)
8333 fname = (char_u *)_("Untitled");
Bram Moolenaard9462e32011-04-11 21:35:11 +02008334 vim_snprintf((char *)buff, DIALOG_MSG_SIZE, format, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008335}
8336#endif
8337
8338/*
8339 * ":behave {mswin,xterm}"
8340 */
8341 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008342ex_behave(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008343{
8344 if (STRCMP(eap->arg, "mswin") == 0)
8345 {
8346 set_option_value((char_u *)"selection", 0L, (char_u *)"exclusive", 0);
8347 set_option_value((char_u *)"selectmode", 0L, (char_u *)"mouse,key", 0);
8348 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"popup", 0);
8349 set_option_value((char_u *)"keymodel", 0L,
8350 (char_u *)"startsel,stopsel", 0);
8351 }
8352 else if (STRCMP(eap->arg, "xterm") == 0)
8353 {
8354 set_option_value((char_u *)"selection", 0L, (char_u *)"inclusive", 0);
8355 set_option_value((char_u *)"selectmode", 0L, (char_u *)"", 0);
8356 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"extend", 0);
8357 set_option_value((char_u *)"keymodel", 0L, (char_u *)"", 0);
8358 }
8359 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008360 semsg(_(e_invarg2), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008361}
8362
Bram Moolenaar071d4272004-06-13 20:20:40 +00008363static int filetype_detect = FALSE;
8364static int filetype_plugin = FALSE;
8365static int filetype_indent = FALSE;
8366
8367/*
8368 * ":filetype [plugin] [indent] {on,off,detect}"
8369 * on: Load the filetype.vim file to install autocommands for file types.
8370 * off: Load the ftoff.vim file to remove all autocommands for file types.
8371 * plugin on: load filetype.vim and ftplugin.vim
8372 * plugin off: load ftplugof.vim
8373 * indent on: load filetype.vim and indent.vim
8374 * indent off: load indoff.vim
8375 */
8376 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008377ex_filetype(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008378{
8379 char_u *arg = eap->arg;
8380 int plugin = FALSE;
8381 int indent = FALSE;
8382
8383 if (*eap->arg == NUL)
8384 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008385 // Print current status.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008386 smsg("filetype detection:%s plugin:%s indent:%s",
Bram Moolenaar071d4272004-06-13 20:20:40 +00008387 filetype_detect ? "ON" : "OFF",
8388 filetype_plugin ? (filetype_detect ? "ON" : "(on)") : "OFF",
8389 filetype_indent ? (filetype_detect ? "ON" : "(on)") : "OFF");
8390 return;
8391 }
8392
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008393 // Accept "plugin" and "indent" in any order.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008394 for (;;)
8395 {
8396 if (STRNCMP(arg, "plugin", 6) == 0)
8397 {
8398 plugin = TRUE;
8399 arg = skipwhite(arg + 6);
8400 continue;
8401 }
8402 if (STRNCMP(arg, "indent", 6) == 0)
8403 {
8404 indent = TRUE;
8405 arg = skipwhite(arg + 6);
8406 continue;
8407 }
8408 break;
8409 }
8410 if (STRCMP(arg, "on") == 0 || STRCMP(arg, "detect") == 0)
8411 {
8412 if (*arg == 'o' || !filetype_detect)
8413 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01008414 source_runtime((char_u *)FILETYPE_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008415 filetype_detect = TRUE;
8416 if (plugin)
8417 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01008418 source_runtime((char_u *)FTPLUGIN_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008419 filetype_plugin = TRUE;
8420 }
8421 if (indent)
8422 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01008423 source_runtime((char_u *)INDENT_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008424 filetype_indent = TRUE;
8425 }
8426 }
8427 if (*arg == 'd')
8428 {
Bram Moolenaar1610d052016-06-09 22:53:01 +02008429 (void)do_doautocmd((char_u *)"filetypedetect BufRead", TRUE, NULL);
Bram Moolenaara3227e22006-03-08 21:32:40 +00008430 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008431 }
8432 }
8433 else if (STRCMP(arg, "off") == 0)
8434 {
8435 if (plugin || indent)
8436 {
8437 if (plugin)
8438 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01008439 source_runtime((char_u *)FTPLUGOF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008440 filetype_plugin = FALSE;
8441 }
8442 if (indent)
8443 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01008444 source_runtime((char_u *)INDOFF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008445 filetype_indent = FALSE;
8446 }
8447 }
8448 else
8449 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01008450 source_runtime((char_u *)FTOFF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008451 filetype_detect = FALSE;
8452 }
8453 }
8454 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008455 semsg(_(e_invarg2), arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008456}
8457
8458/*
Bram Moolenaar3e545692017-06-04 19:00:32 +02008459 * ":setfiletype [FALLBACK] {name}"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008460 */
8461 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008462ex_setfiletype(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008463{
8464 if (!did_filetype)
Bram Moolenaar3e545692017-06-04 19:00:32 +02008465 {
8466 char_u *arg = eap->arg;
8467
8468 if (STRNCMP(arg, "FALLBACK ", 9) == 0)
8469 arg += 9;
8470
8471 set_option_value((char_u *)"filetype", 0L, arg, OPT_LOCAL);
8472 if (arg != eap->arg)
8473 did_filetype = FALSE;
8474 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008475}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008476
Bram Moolenaar071d4272004-06-13 20:20:40 +00008477 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008478ex_digraphs(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008479{
8480#ifdef FEAT_DIGRAPHS
8481 if (*eap->arg != NUL)
8482 putdigraph(eap->arg);
8483 else
Bram Moolenaareae8ae12018-12-14 18:53:02 +01008484 listdigraphs(eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008485#else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008486 emsg(_("E196: No digraphs in this version"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008487#endif
8488}
8489
Bram Moolenaar451fc7b2018-04-27 22:53:07 +02008490#if defined(FEAT_SEARCH_EXTRA) || defined(PROTO)
8491 void
8492set_no_hlsearch(int flag)
8493{
8494 no_hlsearch = flag;
8495# ifdef FEAT_EVAL
8496 set_vim_var_nr(VV_HLSEARCH, !no_hlsearch && p_hls);
8497# endif
8498}
8499
Bram Moolenaar071d4272004-06-13 20:20:40 +00008500/*
8501 * ":nohlsearch"
8502 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008503 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008504ex_nohlsearch(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008505{
Bram Moolenaar451fc7b2018-04-27 22:53:07 +02008506 set_no_hlsearch(TRUE);
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00008507 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008508}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008509#endif
8510
8511#ifdef FEAT_CRYPT
8512/*
8513 * ":X": Get crypt key
8514 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008515 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008516ex_X(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008517{
Bram Moolenaar3a0c9082014-11-12 15:15:42 +01008518 crypt_check_current_method();
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02008519 (void)crypt_get_key(TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008520}
8521#endif
8522
8523#ifdef FEAT_FOLDING
8524 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008525ex_fold(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008526{
8527 if (foldManualAllowed(TRUE))
8528 foldCreate(eap->line1, eap->line2);
8529}
8530
8531 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008532ex_foldopen(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008533{
8534 opFoldRange(eap->line1, eap->line2, eap->cmdidx == CMD_foldopen,
8535 eap->forceit, FALSE);
8536}
8537
8538 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008539ex_folddo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008540{
8541 linenr_T lnum;
8542
Bram Moolenaar4facea32019-10-12 20:17:40 +02008543# ifdef FEAT_CLIPBOARD
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02008544 start_global_changes();
Bram Moolenaar4facea32019-10-12 20:17:40 +02008545# endif
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02008546
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008547 // First set the marks for all lines closed/open.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008548 for (lnum = eap->line1; lnum <= eap->line2; ++lnum)
8549 if (hasFolding(lnum, NULL, NULL) == (eap->cmdidx == CMD_folddoclosed))
8550 ml_setmarked(lnum);
8551
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008552 // Execute the command on the marked lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008553 global_exe(eap->arg);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008554 ml_clearmarked(); // clear rest of the marks
Bram Moolenaar4facea32019-10-12 20:17:40 +02008555# ifdef FEAT_CLIPBOARD
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02008556 end_global_changes();
Bram Moolenaar4facea32019-10-12 20:17:40 +02008557# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008558}
8559#endif
Bram Moolenaarf5291f32017-09-14 22:55:37 +02008560
Bram Moolenaar39665952018-08-15 20:59:48 +02008561#ifdef FEAT_QUICKFIX
8562/*
8563 * Returns TRUE if the supplied Ex cmdidx is for a location list command
8564 * instead of a quickfix command.
8565 */
8566 int
8567is_loclist_cmd(int cmdidx)
8568{
Bram Moolenaar74c8be22018-08-23 22:51:40 +02008569 if (cmdidx < 0 || cmdidx >= CMD_SIZE)
Bram Moolenaar39665952018-08-15 20:59:48 +02008570 return FALSE;
8571 return cmdnames[cmdidx].cmd_name[0] == 'l';
8572}
8573#endif
8574
Bram Moolenaar4facea32019-10-12 20:17:40 +02008575#if defined(FEAT_TIMERS) || defined(PROTO)
Bram Moolenaarf5291f32017-09-14 22:55:37 +02008576 int
8577get_pressedreturn(void)
8578{
8579 return ex_pressedreturn;
8580}
8581
8582 void
8583set_pressedreturn(int val)
8584{
8585 ex_pressedreturn = val;
8586}
8587#endif