blob: a3442edbe7507b6aecb8ad55b20e72818f8bf494 [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);
323static void ex_set(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000324#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100325static void ex_nohlsearch(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000326#else
327# define ex_nohlsearch ex_ni
328# define ex_match ex_ni
329#endif
330#ifdef FEAT_CRYPT
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100331static void ex_X(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000332#else
333# define ex_X ex_ni
334#endif
335#ifdef FEAT_FOLDING
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100336static void ex_fold(exarg_T *eap);
337static void ex_foldopen(exarg_T *eap);
338static void ex_folddo(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000339#else
340# define ex_fold ex_ni
341# define ex_foldopen ex_ni
342# define ex_folddo ex_ni
343#endif
Bram Moolenaar13505972019-01-24 15:04:48 +0100344#if !(defined(HAVE_LOCALE_H) || defined(X_LOCALE))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000345# define ex_language ex_ni
346#endif
347#ifndef FEAT_SIGNS
348# define ex_sign ex_ni
349#endif
Bram Moolenaar009b2592004-10-24 19:18:58 +0000350#ifndef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200351# define ex_nbclose ex_ni
Bram Moolenaar009b2592004-10-24 19:18:58 +0000352# define ex_nbkey ex_ni
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200353# define ex_nbstart ex_ni
Bram Moolenaar009b2592004-10-24 19:18:58 +0000354#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000355
Bram Moolenaar071d4272004-06-13 20:20:40 +0000356#ifndef FEAT_JUMPLIST
357# define ex_jumps ex_ni
Bram Moolenaar2d358992016-06-12 21:20:54 +0200358# define ex_clearjumps ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000359# define ex_changes ex_ni
360#endif
361
Bram Moolenaar05159a02005-02-26 23:04:13 +0000362#ifndef FEAT_PROFILE
363# define ex_profile ex_ni
364#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200365#ifndef FEAT_TERMINAL
366# define ex_terminal ex_ni
367#endif
Bram Moolenaard4aa83a2019-05-09 18:59:31 +0200368#if !defined(FEAT_X11) || !defined(FEAT_XCLIPBOARD)
369# define ex_xrestore ex_ni
370#endif
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +0100371#if !defined(FEAT_PROP_POPUP)
Bram Moolenaar682725c2019-05-25 20:10:37 +0200372# define ex_popupclear ex_ni
373#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +0000374
Bram Moolenaar071d4272004-06-13 20:20:40 +0000375/*
376 * Declare cmdnames[].
377 */
378#define DO_DECLARE_EXCMD
379#include "ex_cmds.h"
Bram Moolenaar6de5e122017-04-20 21:55:44 +0200380#include "ex_cmdidxs.h"
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +0100381
Bram Moolenaar071d4272004-06-13 20:20:40 +0000382static char_u dollar_command[2] = {'$', 0};
383
384
385#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100386// Struct for storing a line inside a while/for loop
Bram Moolenaar071d4272004-06-13 20:20:40 +0000387typedef struct
388{
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100389 char_u *line; // command line
390 linenr_T lnum; // sourcing_lnum of the line
Bram Moolenaar071d4272004-06-13 20:20:40 +0000391} wcmd_T;
392
393/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000394 * Structure used to store info for line position in a while or for loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000395 * This is required, because do_one_cmd() may invoke ex_function(), which
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000396 * reads more lines that may come from the while/for loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000397 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000398struct loop_cookie
Bram Moolenaar071d4272004-06-13 20:20:40 +0000399{
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100400 garray_T *lines_gap; // growarray with line info
401 int current_line; // last read line from growarray
402 int repeating; // TRUE when looping a second time
403 // When "repeating" is FALSE use "getline" and "cookie" to get lines
Bram Moolenaare96a2492019-06-25 04:12:16 +0200404 char_u *(*getline)(int, void *, int, int);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000405 void *cookie;
406};
407
Bram Moolenaare96a2492019-06-25 04:12:16 +0200408static char_u *get_loop_line(int c, void *cookie, int indent, int do_concat);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100409static int store_loop_line(garray_T *gap, char_u *line);
410static void free_cmdlines(garray_T *gap);
Bram Moolenaared203462004-06-16 11:19:22 +0000411
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100412// Struct to save a few things while debugging. Used in do_cmdline() only.
Bram Moolenaared203462004-06-16 11:19:22 +0000413struct dbg_stuff
414{
415 int trylevel;
416 int force_abort;
417 except_T *caught_stack;
418 char_u *vv_exception;
419 char_u *vv_throwpoint;
420 int did_emsg;
421 int got_int;
422 int did_throw;
423 int need_rethrow;
424 int check_cstack;
425 except_T *current_exception;
426};
427
Bram Moolenaared203462004-06-16 11:19:22 +0000428 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100429save_dbg_stuff(struct dbg_stuff *dsp)
Bram Moolenaared203462004-06-16 11:19:22 +0000430{
431 dsp->trylevel = trylevel; trylevel = 0;
432 dsp->force_abort = force_abort; force_abort = FALSE;
433 dsp->caught_stack = caught_stack; caught_stack = NULL;
434 dsp->vv_exception = v_exception(NULL);
435 dsp->vv_throwpoint = v_throwpoint(NULL);
436
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100437 // Necessary for debugging an inactive ":catch", ":finally", ":endtry"
Bram Moolenaared203462004-06-16 11:19:22 +0000438 dsp->did_emsg = did_emsg; did_emsg = FALSE;
439 dsp->got_int = got_int; got_int = FALSE;
440 dsp->did_throw = did_throw; did_throw = FALSE;
441 dsp->need_rethrow = need_rethrow; need_rethrow = FALSE;
442 dsp->check_cstack = check_cstack; check_cstack = FALSE;
443 dsp->current_exception = current_exception; current_exception = NULL;
444}
445
446 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100447restore_dbg_stuff(struct dbg_stuff *dsp)
Bram Moolenaared203462004-06-16 11:19:22 +0000448{
449 suppress_errthrow = FALSE;
450 trylevel = dsp->trylevel;
451 force_abort = dsp->force_abort;
452 caught_stack = dsp->caught_stack;
453 (void)v_exception(dsp->vv_exception);
454 (void)v_throwpoint(dsp->vv_throwpoint);
455 did_emsg = dsp->did_emsg;
456 got_int = dsp->got_int;
457 did_throw = dsp->did_throw;
458 need_rethrow = dsp->need_rethrow;
459 check_cstack = dsp->check_cstack;
460 current_exception = dsp->current_exception;
461}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000462#endif
463
Bram Moolenaar071d4272004-06-13 20:20:40 +0000464/*
465 * do_exmode(): Repeatedly get commands for the "Ex" mode, until the ":vi"
466 * command is given.
467 */
468 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100469do_exmode(
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100470 int improved) // TRUE for "improved Ex" mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000471{
472 int save_msg_scroll;
473 int prev_msg_row;
474 linenr_T prev_line;
Bram Moolenaar79518e22017-02-17 16:31:35 +0100475 varnumber_T changedtick;
Bram Moolenaardf177f62005-02-22 08:39:57 +0000476
477 if (improved)
478 exmode_active = EXMODE_VIM;
479 else
480 exmode_active = EXMODE_NORMAL;
481 State = NORMAL;
482
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100483 // When using ":global /pat/ visual" and then "Q" we return to continue
484 // the :global command.
Bram Moolenaardf177f62005-02-22 08:39:57 +0000485 if (global_busy)
486 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000487
488 save_msg_scroll = msg_scroll;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100489 ++RedrawingDisabled; // don't redisplay the window
490 ++no_wait_return; // don't wait for return
Bram Moolenaar071d4272004-06-13 20:20:40 +0000491#ifdef FEAT_GUI
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100492 // Ignore scrollbar and mouse events in Ex mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000493 ++hold_gui_events;
494#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000495
Bram Moolenaar32526b32019-01-19 17:43:09 +0100496 msg(_("Entering Ex mode. Type \"visual\" to go to Normal mode."));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000497 while (exmode_active)
498 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100499 // Check for a ":normal" command and no more characters left.
Bram Moolenaar7c626922005-02-07 22:01:03 +0000500 if (ex_normal_busy > 0 && typebuf.tb_len == 0)
501 {
502 exmode_active = FALSE;
503 break;
504 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000505 msg_scroll = TRUE;
506 need_wait_return = FALSE;
507 ex_pressedreturn = FALSE;
508 ex_no_reprint = FALSE;
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100509 changedtick = CHANGEDTICK(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000510 prev_msg_row = msg_row;
511 prev_line = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000512 if (improved)
513 {
514 cmdline_row = msg_row;
515 do_cmdline(NULL, getexline, NULL, 0);
516 }
517 else
518 do_cmdline(NULL, getexmodeline, NULL, DOCMD_NOWAIT);
519 lines_left = Rows - 1;
520
Bram Moolenaardf177f62005-02-22 08:39:57 +0000521 if ((prev_line != curwin->w_cursor.lnum
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100522 || changedtick != CHANGEDTICK(curbuf)) && !ex_no_reprint)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000523 {
Bram Moolenaardf177f62005-02-22 08:39:57 +0000524 if (curbuf->b_ml.ml_flags & ML_EMPTY)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100525 emsg(_(e_emptybuf));
Bram Moolenaardf177f62005-02-22 08:39:57 +0000526 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000527 {
Bram Moolenaardf177f62005-02-22 08:39:57 +0000528 if (ex_pressedreturn)
529 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100530 // go up one line, to overwrite the ":<CR>" line, so the
531 // output doesn't contain empty lines.
Bram Moolenaardf177f62005-02-22 08:39:57 +0000532 msg_row = prev_msg_row;
533 if (prev_msg_row == Rows - 1)
534 msg_row--;
535 }
536 msg_col = 0;
537 print_line_no_prefix(curwin->w_cursor.lnum, FALSE, FALSE);
538 msg_clr_eos();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000539 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000540 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100541 else if (ex_pressedreturn && !ex_no_reprint) // must be at EOF
Bram Moolenaardf177f62005-02-22 08:39:57 +0000542 {
543 if (curbuf->b_ml.ml_flags & ML_EMPTY)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100544 emsg(_(e_emptybuf));
Bram Moolenaardf177f62005-02-22 08:39:57 +0000545 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100546 emsg(_("E501: At end-of-file"));
Bram Moolenaardf177f62005-02-22 08:39:57 +0000547 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000548 }
549
550#ifdef FEAT_GUI
551 --hold_gui_events;
552#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000553 --RedrawingDisabled;
554 --no_wait_return;
555 update_screen(CLEAR);
556 need_wait_return = FALSE;
557 msg_scroll = save_msg_scroll;
558}
559
560/*
Bram Moolenaar4facea32019-10-12 20:17:40 +0200561 * Print the executed command for when 'verbose' is set.
562 * When "lnum" is 0 only print the command.
563 */
564 static void
565msg_verbose_cmd(linenr_T lnum, char_u *cmd)
566{
567 ++no_wait_return;
568 verbose_enter_scroll();
569
570 if (lnum == 0)
571 smsg(_("Executing: %s"), cmd);
572 else
573 smsg(_("line %ld: %s"), (long)lnum, cmd);
574 if (msg_silent == 0)
575 msg_puts("\n"); // don't overwrite this
576
577 verbose_leave_scroll();
578 --no_wait_return;
579}
580
581/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000582 * Execute a simple command line. Used for translated commands like "*".
583 */
584 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100585do_cmdline_cmd(char_u *cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000586{
587 return do_cmdline(cmd, NULL, NULL,
588 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
589}
590
591/*
592 * do_cmdline(): execute one Ex command line
593 *
594 * 1. Execute "cmdline" when it is not NULL.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100595 * If "cmdline" is NULL, or more lines are needed, fgetline() is used.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000596 * 2. Split up in parts separated with '|'.
597 *
598 * This function can be called recursively!
599 *
600 * flags:
601 * DOCMD_VERBOSE - The command will be included in the error message.
602 * DOCMD_NOWAIT - Don't call wait_return() and friends.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100603 * DOCMD_REPEAT - Repeat execution until fgetline() returns NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000604 * DOCMD_KEYTYPED - Don't reset KeyTyped.
605 * DOCMD_EXCRESET - Reset the exception environment (used for debugging).
606 * DOCMD_KEEPLINE - Store first typed line (for repeating with ".").
607 *
608 * return FAIL if cmdline could not be executed, OK otherwise
609 */
610 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100611do_cmdline(
612 char_u *cmdline,
Bram Moolenaare96a2492019-06-25 04:12:16 +0200613 char_u *(*fgetline)(int, void *, int, int),
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100614 void *cookie, // argument for fgetline()
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100615 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000616{
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100617 char_u *next_cmdline; // next cmd to execute
618 char_u *cmdline_copy = NULL; // copy of cmd line
619 int used_getline = FALSE; // used "fgetline" to obtain command
620 static int recursive = 0; // recursive depth
Bram Moolenaar071d4272004-06-13 20:20:40 +0000621 int msg_didout_before_start = 0;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100622 int count = 0; // line number count
623 int did_inc = FALSE; // incremented RedrawingDisabled
Bram Moolenaar071d4272004-06-13 20:20:40 +0000624 int retval = OK;
625#ifdef FEAT_EVAL
Bram Moolenaarddef1292019-12-16 17:10:33 +0100626 cstack_T cstack; // conditional stack
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100627 garray_T lines_ga; // keep lines for ":while"/":for"
628 int current_line = 0; // active line in lines_ga
629 char_u *fname = NULL; // function or script name
630 linenr_T *breakpoint = NULL; // ptr to breakpoint field in cookie
631 int *dbg_tick = NULL; // ptr to dbg_tick field in cookie
632 struct dbg_stuff debug_saved; // saved things for debug mode
Bram Moolenaar071d4272004-06-13 20:20:40 +0000633 int initial_trylevel;
634 struct msglist **saved_msg_list = NULL;
635 struct msglist *private_msg_list;
636
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100637 // "fgetline" and "cookie" passed to do_one_cmd()
Bram Moolenaare96a2492019-06-25 04:12:16 +0200638 char_u *(*cmd_getline)(int, void *, int, int);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000639 void *cmd_cookie;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000640 struct loop_cookie cmd_loop_cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000641 void *real_cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +0000642 int getline_is_func;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000643#else
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100644# define cmd_getline fgetline
Bram Moolenaar071d4272004-06-13 20:20:40 +0000645# define cmd_cookie cookie
646#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100647 static int call_depth = 0; // recursiveness
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 Moolenaar071d4272004-06-13 20:20:40 +00001263 current_exception->throw_name = NULL;
1264
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001265 discard_current_exception(); // uses IObuff if 'verbose'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001266 suppress_errthrow = TRUE;
1267 force_abort = TRUE;
1268
1269 if (messages != NULL)
1270 {
1271 do
1272 {
1273 next = messages->next;
1274 emsg(messages->msg);
1275 vim_free(messages->msg);
1276 vim_free(messages);
1277 messages = next;
1278 }
1279 while (messages != NULL);
1280 }
1281 else if (p != NULL)
1282 {
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01001283 emsg(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001284 vim_free(p);
1285 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001286 vim_free(SOURCING_NAME);
1287 estack_pop();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001288 }
1289
1290 /*
1291 * On an interrupt or an aborting error not converted to an exception,
1292 * disable the conversion of errors to exceptions. (Interrupts are not
1293 * converted any more, here.) This enables also the interrupt message
1294 * when force_abort is set and did_emsg unset in case of an interrupt
1295 * from a finally clause after an error.
1296 */
1297 else if (got_int || (did_emsg && force_abort))
1298 suppress_errthrow = TRUE;
1299 }
1300
1301 /*
1302 * The current cstack will be freed when do_cmdline() returns. An uncaught
1303 * exception will have to be rethrown in the previous cstack. If a function
1304 * has just returned or a script file was just finished and the previous
1305 * cstack belongs to the same function or, respectively, script file, it
1306 * will have to be checked for finally clauses to be executed due to the
1307 * ":return" or ":finish". This is done in do_one_cmd().
1308 */
1309 if (did_throw)
1310 need_rethrow = TRUE;
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001311 if ((getline_equal(fgetline, cookie, getsourceline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001312 && ex_nesting_level > source_level(real_cookie))
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001313 || (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001314 && ex_nesting_level > func_level(real_cookie) + 1))
1315 {
1316 if (!did_throw)
1317 check_cstack = TRUE;
1318 }
1319 else
1320 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001321 // When leaving a function, reduce nesting level.
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001322 if (getline_equal(fgetline, cookie, get_func_line))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001323 --ex_nesting_level;
1324 /*
1325 * Go to debug mode when returning from a function in which we are
1326 * single-stepping.
1327 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001328 if ((getline_equal(fgetline, cookie, getsourceline)
1329 || getline_equal(fgetline, cookie, get_func_line))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001330 && ex_nesting_level + 1 <= debug_break_level)
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001331 do_debug(getline_equal(fgetline, cookie, getsourceline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001332 ? (char_u *)_("End of sourced file")
1333 : (char_u *)_("End of function"));
1334 }
1335
1336 /*
1337 * Restore the exception environment (done after returning from the
1338 * debugger).
1339 */
1340 if (flags & DOCMD_EXCRESET)
Bram Moolenaared203462004-06-16 11:19:22 +00001341 restore_dbg_stuff(&debug_saved);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001342
1343 msg_list = saved_msg_list;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001344#endif // FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001345
1346 /*
1347 * If there was too much output to fit on the command line, ask the user to
1348 * hit return before redrawing the screen. With the ":global" command we do
1349 * this only once after the command is finished.
1350 */
1351 if (did_inc)
1352 {
1353 --RedrawingDisabled;
1354 --no_wait_return;
1355 msg_scroll = FALSE;
1356
1357 /*
1358 * When just finished an ":if"-":else" which was typed, no need to
1359 * wait for hit-return. Also for an error situation.
1360 */
1361 if (retval == FAIL
1362#ifdef FEAT_EVAL
1363 || (did_endif && KeyTyped && !did_emsg)
1364#endif
1365 )
1366 {
1367 need_wait_return = FALSE;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001368 msg_didany = FALSE; // don't wait when restarting edit
Bram Moolenaar071d4272004-06-13 20:20:40 +00001369 }
1370 else if (need_wait_return)
1371 {
1372 /*
1373 * The msg_start() above clears msg_didout. The wait_return we do
1374 * here should not overwrite the command that may be shown before
1375 * doing that.
1376 */
1377 msg_didout |= msg_didout_before_start;
1378 wait_return(FALSE);
1379 }
1380 }
1381
Bram Moolenaar2a942252012-11-28 23:03:07 +01001382#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001383 did_endif = FALSE; // in case do_cmdline used recursively
Bram Moolenaar2a942252012-11-28 23:03:07 +01001384#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001385 /*
1386 * Reset if_level, in case a sourced script file contains more ":if" than
1387 * ":endif" (could be ":if x | foo | endif").
1388 */
1389 if_level = 0;
Bram Moolenaar2e18a122012-11-28 19:10:54 +01001390#endif
Bram Moolenaard4ad0d42012-11-28 17:34:48 +01001391
Bram Moolenaar071d4272004-06-13 20:20:40 +00001392 --call_depth;
1393 return retval;
1394}
1395
1396#ifdef FEAT_EVAL
1397/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001398 * Obtain a line when inside a ":while" or ":for" loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001399 */
1400 static char_u *
Bram Moolenaare96a2492019-06-25 04:12:16 +02001401get_loop_line(int c, void *cookie, int indent, int do_concat)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001402{
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001403 struct loop_cookie *cp = (struct loop_cookie *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001404 wcmd_T *wp;
1405 char_u *line;
1406
1407 if (cp->current_line + 1 >= cp->lines_gap->ga_len)
1408 {
1409 if (cp->repeating)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001410 return NULL; // trying to read past ":endwhile"/":endfor"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001411
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001412 // First time inside the ":while"/":for": get line normally.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001413 if (cp->getline == NULL)
Bram Moolenaare96a2492019-06-25 04:12:16 +02001414 line = getcmdline(c, 0L, indent, do_concat);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001415 else
Bram Moolenaare96a2492019-06-25 04:12:16 +02001416 line = cp->getline(c, cp->cookie, indent, do_concat);
Bram Moolenaard68071d2006-05-02 22:08:30 +00001417 if (line != NULL && store_loop_line(cp->lines_gap, line) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001418 ++cp->current_line;
1419
1420 return line;
1421 }
1422
1423 KeyTyped = FALSE;
1424 ++cp->current_line;
1425 wp = (wcmd_T *)(cp->lines_gap->ga_data) + cp->current_line;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001426 SOURCING_LNUM = wp->lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001427 return vim_strsave(wp->line);
1428}
1429
1430/*
1431 * Store a line in "gap" so that a ":while" loop can execute it again.
1432 */
1433 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001434store_loop_line(garray_T *gap, char_u *line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001435{
1436 if (ga_grow(gap, 1) == FAIL)
1437 return FAIL;
1438 ((wcmd_T *)(gap->ga_data))[gap->ga_len].line = vim_strsave(line);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001439 ((wcmd_T *)(gap->ga_data))[gap->ga_len].lnum = SOURCING_LNUM;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001440 ++gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001441 return OK;
1442}
1443
1444/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001445 * Free the lines stored for a ":while" or ":for" loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001446 */
1447 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001448free_cmdlines(garray_T *gap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001449{
1450 while (gap->ga_len > 0)
1451 {
1452 vim_free(((wcmd_T *)(gap->ga_data))[gap->ga_len - 1].line);
1453 --gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001454 }
1455}
1456#endif
1457
1458/*
Bram Moolenaar89d40322006-08-29 15:30:07 +00001459 * If "fgetline" is get_loop_line(), return TRUE if the getline it uses equals
1460 * "func". * Otherwise return TRUE when "fgetline" equals "func".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001461 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001462 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001463getline_equal(
Bram Moolenaare96a2492019-06-25 04:12:16 +02001464 char_u *(*fgetline)(int, void *, int, int),
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001465 void *cookie UNUSED, // argument for fgetline()
Bram Moolenaare96a2492019-06-25 04:12:16 +02001466 char_u *(*func)(int, void *, int, int))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001467{
1468#ifdef FEAT_EVAL
Bram Moolenaare96a2492019-06-25 04:12:16 +02001469 char_u *(*gp)(int, void *, int, int);
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001470 struct loop_cookie *cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001471
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001472 // When "fgetline" is "get_loop_line()" use the "cookie" to find the
1473 // function that's originally used to obtain the lines. This may be
1474 // nested several levels.
Bram Moolenaar89d40322006-08-29 15:30:07 +00001475 gp = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001476 cp = (struct loop_cookie *)cookie;
1477 while (gp == get_loop_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001478 {
1479 gp = cp->getline;
1480 cp = cp->cookie;
1481 }
1482 return gp == func;
1483#else
Bram Moolenaar89d40322006-08-29 15:30:07 +00001484 return fgetline == func;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001485#endif
1486}
1487
Bram Moolenaar071d4272004-06-13 20:20:40 +00001488/*
Bram Moolenaar89d40322006-08-29 15:30:07 +00001489 * If "fgetline" is get_loop_line(), return the cookie used by the original
Bram Moolenaar071d4272004-06-13 20:20:40 +00001490 * getline function. Otherwise return "cookie".
1491 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001492 void *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001493getline_cookie(
Bram Moolenaare96a2492019-06-25 04:12:16 +02001494 char_u *(*fgetline)(int, void *, int, int) UNUSED,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001495 void *cookie) // argument for fgetline()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001496{
Bram Moolenaar13505972019-01-24 15:04:48 +01001497#ifdef FEAT_EVAL
Bram Moolenaare96a2492019-06-25 04:12:16 +02001498 char_u *(*gp)(int, void *, int, int);
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001499 struct loop_cookie *cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001500
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001501 // When "fgetline" is "get_loop_line()" use the "cookie" to find the
1502 // cookie that's originally used to obtain the lines. This may be nested
1503 // several levels.
Bram Moolenaar89d40322006-08-29 15:30:07 +00001504 gp = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001505 cp = (struct loop_cookie *)cookie;
1506 while (gp == get_loop_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001507 {
1508 gp = cp->getline;
1509 cp = cp->cookie;
1510 }
1511 return cp;
Bram Moolenaar13505972019-01-24 15:04:48 +01001512#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001513 return cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001514#endif
Bram Moolenaar13505972019-01-24 15:04:48 +01001515}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001516
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001517
1518/*
1519 * Helper function to apply an offset for buffer commands, i.e. ":bdelete",
1520 * ":bwipeout", etc.
1521 * Returns the buffer number.
1522 */
1523 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001524compute_buffer_local_count(int addr_type, int lnum, int offset)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001525{
1526 buf_T *buf;
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001527 buf_T *nextbuf;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001528 int count = offset;
1529
1530 buf = firstbuf;
1531 while (buf->b_next != NULL && buf->b_fnum < lnum)
1532 buf = buf->b_next;
1533 while (count != 0)
1534 {
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001535 count += (offset < 0) ? 1 : -1;
1536 nextbuf = (offset < 0) ? buf->b_prev : buf->b_next;
1537 if (nextbuf == NULL)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001538 break;
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001539 buf = nextbuf;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001540 if (addr_type == ADDR_LOADED_BUFFERS)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001541 // skip over unloaded buffers
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001542 while (buf->b_ml.ml_mfp == NULL)
1543 {
1544 nextbuf = (offset < 0) ? buf->b_prev : buf->b_next;
1545 if (nextbuf == NULL)
1546 break;
1547 buf = nextbuf;
1548 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001549 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001550 // we might have gone too far, last buffer is not loadedd
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001551 if (addr_type == ADDR_LOADED_BUFFERS)
1552 while (buf->b_ml.ml_mfp == NULL)
1553 {
1554 nextbuf = (offset >= 0) ? buf->b_prev : buf->b_next;
1555 if (nextbuf == NULL)
1556 break;
1557 buf = nextbuf;
1558 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001559 return buf->b_fnum;
1560}
1561
Bram Moolenaarb7316892019-05-01 18:08:42 +02001562/*
1563 * Return the window number of "win".
1564 * When "win" is NULL return the number of windows.
1565 */
Bram Moolenaarf240e182014-11-27 18:33:02 +01001566 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001567current_win_nr(win_T *win)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001568{
1569 win_T *wp;
1570 int nr = 0;
1571
Bram Moolenaar29323592016-07-24 22:04:11 +02001572 FOR_ALL_WINDOWS(wp)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001573 {
1574 ++nr;
1575 if (wp == win)
1576 break;
1577 }
1578 return nr;
1579}
1580
1581 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001582current_tab_nr(tabpage_T *tab)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001583{
1584 tabpage_T *tp;
1585 int nr = 0;
1586
Bram Moolenaar29323592016-07-24 22:04:11 +02001587 FOR_ALL_TABPAGES(tp)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001588 {
1589 ++nr;
1590 if (tp == tab)
1591 break;
1592 }
1593 return nr;
1594}
1595
1596# define CURRENT_WIN_NR current_win_nr(curwin)
1597# define LAST_WIN_NR current_win_nr(NULL)
1598# define CURRENT_TAB_NR current_tab_nr(curtab)
1599# define LAST_TAB_NR current_tab_nr(NULL)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001600
Bram Moolenaar071d4272004-06-13 20:20:40 +00001601/*
1602 * Execute one Ex command.
1603 *
1604 * If 'sourcing' is TRUE, the command will be included in the error message.
1605 *
1606 * 1. skip comment lines and leading space
1607 * 2. handle command modifiers
Bram Moolenaar1c40a662014-11-27 16:38:11 +01001608 * 3. find the command
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001609 * 4. parse range
Bram Moolenaar1c40a662014-11-27 16:38:11 +01001610 * 5. Parse the command.
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001611 * 6. parse arguments
1612 * 7. switch on command name
Bram Moolenaar071d4272004-06-13 20:20:40 +00001613 *
Bram Moolenaar89d40322006-08-29 15:30:07 +00001614 * Note: "fgetline" can be NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001615 *
1616 * This function may be called recursively!
1617 */
1618#if (_MSC_VER == 1200)
1619/*
Bram Moolenaared203462004-06-16 11:19:22 +00001620 * Avoid optimisation bug in VC++ version 6.0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001621 */
Bram Moolenaar281bdce2005-01-25 21:53:18 +00001622 #pragma optimize( "g", off )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001623#endif
1624 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001625do_one_cmd(
Bram Moolenaarddef1292019-12-16 17:10:33 +01001626 char_u **cmdlinep,
1627 int sourcing,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001628#ifdef FEAT_EVAL
Bram Moolenaarddef1292019-12-16 17:10:33 +01001629 cstack_T *cstack,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001630#endif
Bram Moolenaarddef1292019-12-16 17:10:33 +01001631 char_u *(*fgetline)(int, void *, int, int),
1632 void *cookie) // argument for fgetline()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001633{
Bram Moolenaarddef1292019-12-16 17:10:33 +01001634 char_u *p;
1635 linenr_T lnum;
1636 long n;
1637 char *errormsg = NULL; // error message
1638 char_u *after_modifier = NULL;
1639 exarg_T ea; // Ex command arguments
1640 int save_msg_scroll = msg_scroll;
1641 cmdmod_T save_cmdmod;
1642 int save_reg_executing = reg_executing;
1643 int ni; // set when Not Implemented
1644 char_u *cmd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001645
1646 vim_memset(&ea, 0, sizeof(ea));
1647 ea.line1 = 1;
1648 ea.line2 = 1;
1649#ifdef FEAT_EVAL
1650 ++ex_nesting_level;
1651#endif
1652
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001653 // When the last file has not been edited :q has to be typed twice.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001654 if (quitmore
1655#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001656 // avoid that a function call in 'statusline' does this
Bram Moolenaar89d40322006-08-29 15:30:07 +00001657 && !getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001658#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001659 // avoid that an autocommand, e.g. QuitPre, does this
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001660 && !getline_equal(fgetline, cookie, getnextac))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001661 --quitmore;
1662
1663 /*
1664 * Reset browse, confirm, etc.. They are restored when returning, for
1665 * recursive calls.
1666 */
1667 save_cmdmod = cmdmod;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001668
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001669 // "#!anything" is handled like a comment.
Bram Moolenaarcbb37ad2006-08-16 15:04:21 +00001670 if ((*cmdlinep)[0] == '#' && (*cmdlinep)[1] == '!')
1671 goto doend;
1672
Bram Moolenaar4facea32019-10-12 20:17:40 +02001673 if (p_verbose >= 16)
1674 msg_verbose_cmd(0, *cmdlinep);
1675
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02001676/*
1677 * 1. Skip comment lines and leading white space and colons.
1678 * 2. Handle command modifiers.
1679 */
1680 // The "ea" structure holds the arguments that can be used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001681 ea.cmd = *cmdlinep;
Bram Moolenaareffed932018-08-14 13:38:17 +02001682 ea.cmdlinep = cmdlinep;
1683 ea.getline = fgetline;
1684 ea.cookie = cookie;
1685#ifdef FEAT_EVAL
1686 ea.cstack = cstack;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001687#endif
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02001688 if (parse_command_modifiers(&ea, &errormsg, FALSE) == FAIL)
Bram Moolenaareffed932018-08-14 13:38:17 +02001689 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001690
Bram Moolenaar7b668e82016-08-23 23:51:21 +02001691 after_modifier = ea.cmd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001692
1693#ifdef FEAT_EVAL
1694 ea.skip = did_emsg || got_int || did_throw || (cstack->cs_idx >= 0
1695 && !(cstack->cs_flags[cstack->cs_idx] & CSF_ACTIVE));
1696#else
1697 ea.skip = (if_level > 0);
1698#endif
1699
Bram Moolenaar071d4272004-06-13 20:20:40 +00001700/*
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001701 * 3. Skip over the range to find the command. Let "p" point to after it.
1702 *
1703 * We need the command to know what kind of range it uses.
1704 */
1705 cmd = ea.cmd;
1706 ea.cmd = skip_range(ea.cmd, NULL);
1707 if (*ea.cmd == '*' && vim_strchr(p_cpo, CPO_STAR) == NULL)
1708 ea.cmd = skipwhite(ea.cmd + 1);
1709 p = find_command(&ea, NULL);
1710
Bram Moolenaar7feb35e2018-08-21 17:49:54 +02001711#ifdef FEAT_EVAL
1712# ifdef FEAT_PROFILE
1713 // Count this line for profiling if skip is TRUE.
1714 if (do_profiling == PROF_YES
1715 && (!ea.skip || cstack->cs_idx == 0 || (cstack->cs_idx > 0
1716 && (cstack->cs_flags[cstack->cs_idx - 1] & CSF_ACTIVE))))
1717 {
1718 int skip = did_emsg || got_int || did_throw;
1719
1720 if (ea.cmdidx == CMD_catch)
1721 skip = !skip && !(cstack->cs_idx >= 0
1722 && (cstack->cs_flags[cstack->cs_idx] & CSF_THROWN)
1723 && !(cstack->cs_flags[cstack->cs_idx] & CSF_CAUGHT));
1724 else if (ea.cmdidx == CMD_else || ea.cmdidx == CMD_elseif)
1725 skip = skip || !(cstack->cs_idx >= 0
1726 && !(cstack->cs_flags[cstack->cs_idx]
1727 & (CSF_ACTIVE | CSF_TRUE)));
1728 else if (ea.cmdidx == CMD_finally)
1729 skip = FALSE;
1730 else if (ea.cmdidx != CMD_endif
1731 && ea.cmdidx != CMD_endfor
1732 && ea.cmdidx != CMD_endtry
1733 && ea.cmdidx != CMD_endwhile)
1734 skip = ea.skip;
1735
1736 if (!skip)
1737 {
1738 if (getline_equal(fgetline, cookie, get_func_line))
1739 func_line_exec(getline_cookie(fgetline, cookie));
1740 else if (getline_equal(fgetline, cookie, getsourceline))
1741 script_line_exec();
1742 }
1743 }
1744# endif
1745
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001746 // May go to debug mode. If this happens and the ">quit" debug command is
1747 // used, throw an interrupt exception and skip the next command.
Bram Moolenaar7feb35e2018-08-21 17:49:54 +02001748 dbg_check_breakpoint(&ea);
1749 if (!ea.skip && got_int)
1750 {
1751 ea.skip = TRUE;
1752 (void)do_intthrow(cstack);
1753 }
1754#endif
1755
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001756/*
1757 * 4. parse a range specifier of the form: addr [,addr] [;addr] ..
Bram Moolenaar071d4272004-06-13 20:20:40 +00001758 *
1759 * where 'addr' is:
1760 *
1761 * % (entire file)
1762 * $ [+-NUM]
1763 * 'x [+-NUM] (where x denotes a currently defined mark)
1764 * . [+-NUM]
1765 * [+-NUM]..
1766 * NUM
1767 *
1768 * The ea.cmd pointer is updated to point to the first character following the
1769 * range spec. If an initial address is found, but no second, the upper bound
1770 * is equal to the lower.
1771 */
1772
Bram Moolenaar25190db2019-05-04 15:05:28 +02001773 // ea.addr_type for user commands is set by find_ucmd
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01001774 if (!IS_USER_CMDIDX(ea.cmdidx))
1775 {
1776 if (ea.cmdidx != CMD_SIZE)
1777 ea.addr_type = cmdnames[(int)ea.cmdidx].cmd_addr_type;
1778 else
1779 ea.addr_type = ADDR_LINES;
1780
Bram Moolenaar25190db2019-05-04 15:05:28 +02001781 // :wincmd range depends on the argument.
Bram Moolenaar164f3262015-01-14 21:22:01 +01001782 if (ea.cmdidx == CMD_wincmd && p != NULL)
1783 get_wincmd_addr_type(skipwhite(p), &ea);
Bram Moolenaar25190db2019-05-04 15:05:28 +02001784#ifdef FEAT_QUICKFIX
1785 // :.cc in quickfix window uses line number
1786 if ((ea.cmdidx == CMD_cc || ea.cmdidx == CMD_ll) && bt_quickfix(curbuf))
1787 ea.addr_type = ADDR_OTHER;
1788#endif
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01001789 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001790
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01001791 ea.cmd = cmd;
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02001792 if (parse_cmd_address(&ea, &errormsg, FALSE) == FAIL)
Bram Moolenaaree8415b2018-08-10 23:13:12 +02001793 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001794
Bram Moolenaar071d4272004-06-13 20:20:40 +00001795/*
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001796 * 5. Parse the command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001797 */
1798
1799 /*
1800 * Skip ':' and any white space
1801 */
1802 ea.cmd = skipwhite(ea.cmd);
1803 while (*ea.cmd == ':')
1804 ea.cmd = skipwhite(ea.cmd + 1);
1805
1806 /*
1807 * If we got a line, but no command, then go to the line.
1808 * If we find a '|' or '\n' we set ea.nextcmd.
1809 */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001810 if (*ea.cmd == NUL || *ea.cmd == '"'
1811 || (ea.nextcmd = check_nextcmd(ea.cmd)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001812 {
1813 /*
1814 * strange vi behaviour:
1815 * ":3" jumps to line 3
1816 * ":3|..." prints line 3
1817 * ":|" prints current line
1818 */
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001819 if (ea.skip) // skip this if inside :if
Bram Moolenaar071d4272004-06-13 20:20:40 +00001820 goto doend;
Bram Moolenaardf177f62005-02-22 08:39:57 +00001821 if (*ea.cmd == '|' || (exmode_active && ea.line1 != ea.line2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001822 {
1823 ea.cmdidx = CMD_print;
Bram Moolenaar8071cb22019-07-12 17:58:01 +02001824 ea.argt = EX_RANGE+EX_COUNT+EX_TRLBAR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001825 if ((errormsg = invalid_range(&ea)) == NULL)
1826 {
1827 correct_range(&ea);
1828 ex_print(&ea);
1829 }
1830 }
1831 else if (ea.addr_count != 0)
1832 {
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00001833 if (ea.line2 > curbuf->b_ml.ml_line_count)
1834 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001835 // With '-' in 'cpoptions' a line number past the file is an
1836 // error, otherwise put it at the end of the file.
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00001837 if (vim_strchr(p_cpo, CPO_MINUS) != NULL)
1838 ea.line2 = -1;
1839 else
1840 ea.line2 = curbuf->b_ml.ml_line_count;
1841 }
1842
1843 if (ea.line2 < 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001844 errormsg = _(e_invrange);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001845 else
1846 {
1847 if (ea.line2 == 0)
1848 curwin->w_cursor.lnum = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001849 else
1850 curwin->w_cursor.lnum = ea.line2;
1851 beginline(BL_SOL | BL_FIX);
1852 }
1853 }
1854 goto doend;
1855 }
1856
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001857 // If this looks like an undefined user command and there are CmdUndefined
1858 // autocommands defined, trigger the matching autocommands.
Bram Moolenaard5005162014-08-22 23:05:54 +02001859 if (p != NULL && ea.cmdidx == CMD_SIZE && !ea.skip
1860 && ASCII_ISUPPER(*ea.cmd)
1861 && has_cmdundefined())
1862 {
Bram Moolenaard5005162014-08-22 23:05:54 +02001863 int ret;
1864
Bram Moolenaar5a31b462014-08-23 14:16:20 +02001865 p = ea.cmd;
Bram Moolenaard5005162014-08-22 23:05:54 +02001866 while (ASCII_ISALNUM(*p))
1867 ++p;
Bram Moolenaar120f4a82014-09-09 12:22:06 +02001868 p = vim_strnsave(ea.cmd, (int)(p - ea.cmd));
Bram Moolenaard5005162014-08-22 23:05:54 +02001869 ret = apply_autocmds(EVENT_CMDUNDEFINED, p, p, TRUE, NULL);
1870 vim_free(p);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001871 // If the autocommands did something and didn't cause an error, try
1872 // finding the command again.
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001873 p = (ret
1874#ifdef FEAT_EVAL
1875 && !aborting()
Bram Moolenaard5005162014-08-22 23:05:54 +02001876#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001877 ) ? find_command(&ea, NULL) : ea.cmd;
1878 }
Bram Moolenaard5005162014-08-22 23:05:54 +02001879
Bram Moolenaar071d4272004-06-13 20:20:40 +00001880 if (p == NULL)
1881 {
1882 if (!ea.skip)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001883 errormsg = _("E464: Ambiguous use of user-defined command");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001884 goto doend;
1885 }
Bram Moolenaarac9fb182019-04-27 13:04:13 +02001886 // Check for wrong commands.
Bram Moolenaar6f9a4762017-06-22 20:39:17 +02001887 if (*p == '!' && ea.cmd[1] == 0151 && ea.cmd[0] == 78
1888 && !IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001889 {
1890 errormsg = uc_fun_cmd();
1891 goto doend;
1892 }
Bram Moolenaarac9fb182019-04-27 13:04:13 +02001893
Bram Moolenaar071d4272004-06-13 20:20:40 +00001894 if (ea.cmdidx == CMD_SIZE)
1895 {
1896 if (!ea.skip)
1897 {
1898 STRCPY(IObuff, _("E492: Not an editor command"));
1899 if (!sourcing)
Bram Moolenaar7b668e82016-08-23 23:51:21 +02001900 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001901 // If the modifier was parsed OK the error must be in the
1902 // following command
Bram Moolenaar7b668e82016-08-23 23:51:21 +02001903 if (after_modifier != NULL)
1904 append_command(after_modifier);
1905 else
1906 append_command(*cmdlinep);
1907 }
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001908 errormsg = (char *)IObuff;
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001909 did_emsg_syntax = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001910 }
1911 goto doend;
1912 }
1913
Bram Moolenaar958636c2014-10-21 20:01:58 +02001914 ni = (!IS_USER_CMDIDX(ea.cmdidx)
1915 && (cmdnames[ea.cmdidx].cmd_func == ex_ni
Bram Moolenaar7bb75552007-07-16 18:39:49 +00001916#ifdef HAVE_EX_SCRIPT_NI
1917 || cmdnames[ea.cmdidx].cmd_func == ex_script_ni
1918#endif
1919 ));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001920
1921#ifndef FEAT_EVAL
1922 /*
1923 * When the expression evaluation is disabled, recognize the ":if" and
1924 * ":endif" commands and ignore everything in between it.
1925 */
1926 if (ea.cmdidx == CMD_if)
1927 ++if_level;
1928 if (if_level)
1929 {
1930 if (ea.cmdidx == CMD_endif)
1931 --if_level;
1932 goto doend;
1933 }
1934
1935#endif
1936
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001937 // forced commands
Bram Moolenaar196b3b02008-06-20 16:51:41 +00001938 if (*p == '!' && ea.cmdidx != CMD_substitute
1939 && ea.cmdidx != CMD_smagic && ea.cmdidx != CMD_snomagic)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001940 {
1941 ++p;
1942 ea.forceit = TRUE;
1943 }
1944 else
1945 ea.forceit = FALSE;
1946
1947/*
Bram Moolenaarb7316892019-05-01 18:08:42 +02001948 * 6. Parse arguments. Then check for errors.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001949 */
Bram Moolenaar958636c2014-10-21 20:01:58 +02001950 if (!IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001951 ea.argt = (long)cmdnames[(int)ea.cmdidx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001952
1953 if (!ea.skip)
1954 {
1955#ifdef HAVE_SANDBOX
Bram Moolenaar8071cb22019-07-12 17:58:01 +02001956 if (sandbox != 0 && !(ea.argt & EX_SBOXOK))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001957 {
Bram Moolenaar8c62a082019-02-08 14:34:10 +01001958 // Command not allowed in sandbox.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001959 errormsg = _(e_sandbox);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001960 goto doend;
1961 }
1962#endif
Bram Moolenaar8071cb22019-07-12 17:58:01 +02001963 if (restricted != 0 && (ea.argt & EX_RESTRICT))
Bram Moolenaar8c62a082019-02-08 14:34:10 +01001964 {
1965 errormsg = _("E981: Command not allowed in rvim");
1966 goto doend;
1967 }
Bram Moolenaar8071cb22019-07-12 17:58:01 +02001968 if (!curbuf->b_p_ma && (ea.argt & EX_MODIFY))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001969 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001970 // Command not allowed in non-'modifiable' buffer
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001971 errormsg = _(e_modifiable);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001972 goto doend;
1973 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00001974
Bram Moolenaar8071cb22019-07-12 17:58:01 +02001975 if (text_locked() && !(ea.argt & EX_CMDWIN)
Bram Moolenaar958636c2014-10-21 20:01:58 +02001976 && !IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001977 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001978 // Command not allowed when editing the command line.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001979 errormsg = _(get_text_locked_msg());
Bram Moolenaar071d4272004-06-13 20:20:40 +00001980 goto doend;
1981 }
Bram Moolenaar379fb762018-08-30 15:58:28 +02001982
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001983 // Disallow editing another buffer when "curbuf_lock" is set.
1984 // Do allow ":checktime" (it is postponed).
1985 // Do allow ":edit" (check for an argument later).
1986 // Do allow ":file" with no arguments (check for an argument later).
Bram Moolenaar8071cb22019-07-12 17:58:01 +02001987 if (!(ea.argt & EX_CMDWIN)
Bram Moolenaar5555acc2006-04-07 21:33:12 +00001988 && ea.cmdidx != CMD_checktime
Bram Moolenaar379fb762018-08-30 15:58:28 +02001989 && ea.cmdidx != CMD_edit
1990 && ea.cmdidx != CMD_file
Bram Moolenaar958636c2014-10-21 20:01:58 +02001991 && !IS_USER_CMDIDX(ea.cmdidx)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001992 && curbuf_locked())
1993 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001994
Bram Moolenaar8071cb22019-07-12 17:58:01 +02001995 if (!ni && !(ea.argt & EX_RANGE) && ea.addr_count > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001996 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001997 // no range allowed
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001998 errormsg = _(e_norange);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001999 goto doend;
2000 }
2001 }
2002
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002003 if (!ni && !(ea.argt & EX_BANG) && ea.forceit) // no <!> allowed
Bram Moolenaar071d4272004-06-13 20:20:40 +00002004 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002005 errormsg = _(e_nobang);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002006 goto doend;
2007 }
2008
2009 /*
2010 * Don't complain about the range if it is not used
2011 * (could happen if line_count is accidentally set to 0).
2012 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002013 if (!ea.skip && !ni && (ea.argt & EX_RANGE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002014 {
2015 /*
2016 * If the range is backwards, ask for confirmation and, if given, swap
2017 * ea.line1 & ea.line2 so it's forwards again.
2018 * When global command is busy, don't ask, will fail below.
2019 */
2020 if (!global_busy && ea.line1 > ea.line2)
2021 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002022 if (msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002023 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002024 if (sourcing || exmode_active)
2025 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002026 errormsg = _("E493: Backwards range given");
Bram Moolenaara5792f52005-11-23 21:25:05 +00002027 goto doend;
2028 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002029 if (ask_yesno((char_u *)
2030 _("Backwards range given, OK to swap"), FALSE) != 'y')
Bram Moolenaara5792f52005-11-23 21:25:05 +00002031 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002032 }
2033 lnum = ea.line1;
2034 ea.line1 = ea.line2;
2035 ea.line2 = lnum;
2036 }
2037 if ((errormsg = invalid_range(&ea)) != NULL)
2038 goto doend;
2039 }
2040
Bram Moolenaarb7316892019-05-01 18:08:42 +02002041 if ((ea.addr_type == ADDR_OTHER) && ea.addr_count == 0)
2042 // default is 1, not cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00002043 ea.line2 = 1;
2044
2045 correct_range(&ea);
2046
2047#ifdef FEAT_FOLDING
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002048 if (((ea.argt & EX_WHOLEFOLD) || ea.addr_count >= 2) && !global_busy
Bram Moolenaara3306952016-01-02 21:41:06 +01002049 && ea.addr_type == ADDR_LINES)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002050 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002051 // Put the first line at the start of a closed fold, put the last line
2052 // at the end of a closed fold.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002053 (void)hasFolding(ea.line1, &ea.line1, NULL);
2054 (void)hasFolding(ea.line2, NULL, &ea.line2);
2055 }
2056#endif
2057
2058#ifdef FEAT_QUICKFIX
2059 /*
Bram Moolenaar86b68352004-12-27 21:59:20 +00002060 * For the ":make" and ":grep" commands we insert the 'makeprg'/'grepprg'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002061 * option here, so things like % get expanded.
2062 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002063 p = replace_makeprg(&ea, p, cmdlinep);
2064 if (p == NULL)
2065 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002066#endif
2067
2068 /*
2069 * Skip to start of argument.
2070 * Don't do this for the ":!" command, because ":!! -l" needs the space.
2071 */
2072 if (ea.cmdidx == CMD_bang)
2073 ea.arg = p;
2074 else
2075 ea.arg = skipwhite(p);
2076
Bram Moolenaar379fb762018-08-30 15:58:28 +02002077 // ":file" cannot be run with an argument when "curbuf_lock" is set
2078 if (ea.cmdidx == CMD_file && *ea.arg != NUL && curbuf_locked())
2079 goto doend;
2080
Bram Moolenaar071d4272004-06-13 20:20:40 +00002081 /*
2082 * Check for "++opt=val" argument.
2083 * Must be first, allow ":w ++enc=utf8 !cmd"
2084 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002085 if (ea.argt & EX_ARGOPT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002086 while (ea.arg[0] == '+' && ea.arg[1] == '+')
2087 if (getargopt(&ea) == FAIL && !ni)
2088 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002089 errormsg = _(e_invarg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002090 goto doend;
2091 }
2092
2093 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
2094 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002095 if (*ea.arg == '>') // append
Bram Moolenaar071d4272004-06-13 20:20:40 +00002096 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002097 if (*++ea.arg != '>') // typed wrong
Bram Moolenaar071d4272004-06-13 20:20:40 +00002098 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002099 errormsg = _("E494: Use w or w>>");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002100 goto doend;
2101 }
2102 ea.arg = skipwhite(ea.arg + 1);
2103 ea.append = TRUE;
2104 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002105 else if (*ea.arg == '!' && ea.cmdidx == CMD_write) // :w !filter
Bram Moolenaar071d4272004-06-13 20:20:40 +00002106 {
2107 ++ea.arg;
2108 ea.usefilter = TRUE;
2109 }
2110 }
2111
2112 if (ea.cmdidx == CMD_read)
2113 {
2114 if (ea.forceit)
2115 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002116 ea.usefilter = TRUE; // :r! filter if ea.forceit
Bram Moolenaar071d4272004-06-13 20:20:40 +00002117 ea.forceit = FALSE;
2118 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002119 else if (*ea.arg == '!') // :r !filter
Bram Moolenaar071d4272004-06-13 20:20:40 +00002120 {
2121 ++ea.arg;
2122 ea.usefilter = TRUE;
2123 }
2124 }
2125
2126 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
2127 {
2128 ea.amount = 1;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002129 while (*ea.arg == *ea.cmd) // count number of '>' or '<'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002130 {
2131 ++ea.arg;
2132 ++ea.amount;
2133 }
2134 ea.arg = skipwhite(ea.arg);
2135 }
2136
2137 /*
2138 * Check for "+command" argument, before checking for next command.
2139 * Don't do this for ":read !cmd" and ":write !cmd".
2140 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002141 if ((ea.argt & EX_CMDARG) && !ea.usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002142 ea.do_ecmd_cmd = getargcmd(&ea.arg);
2143
2144 /*
2145 * Check for '|' to separate commands and '"' to start comments.
2146 * Don't do this for ":read !cmd" and ":write !cmd".
2147 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002148 if ((ea.argt & EX_TRLBAR) && !ea.usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002149 separate_nextcmd(&ea);
2150
2151 /*
2152 * Check for <newline> to end a shell command.
Bram Moolenaardf177f62005-02-22 08:39:57 +00002153 * Also do this for ":read !cmd", ":write !cmd" and ":global".
2154 * Any others?
Bram Moolenaar071d4272004-06-13 20:20:40 +00002155 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00002156 else if (ea.cmdidx == CMD_bang
Bram Moolenaar67883b42017-07-27 22:57:00 +02002157 || ea.cmdidx == CMD_terminal
Bram Moolenaardf177f62005-02-22 08:39:57 +00002158 || ea.cmdidx == CMD_global
2159 || ea.cmdidx == CMD_vglobal
2160 || ea.usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002161 {
2162 for (p = ea.arg; *p; ++p)
2163 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002164 // Remove one backslash before a newline, so that it's possible to
2165 // pass a newline to the shell and also a newline that is preceded
2166 // with a backslash. This makes it impossible to end a shell
2167 // command in a backslash, but that doesn't appear useful.
2168 // Halving the number of backslashes is incompatible with previous
2169 // versions.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002170 if (*p == '\\' && p[1] == '\n')
Bram Moolenaara7241f52008-06-24 20:39:31 +00002171 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002172 else if (*p == '\n')
2173 {
2174 ea.nextcmd = p + 1;
2175 *p = NUL;
2176 break;
2177 }
2178 }
2179 }
2180
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002181 if ((ea.argt & EX_DFLALL) && ea.addr_count == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002182 {
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002183 buf_T *buf;
2184
Bram Moolenaar071d4272004-06-13 20:20:40 +00002185 ea.line1 = 1;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002186 switch (ea.addr_type)
2187 {
2188 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02002189 case ADDR_OTHER:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002190 ea.line2 = curbuf->b_ml.ml_line_count;
2191 break;
2192 case ADDR_LOADED_BUFFERS:
2193 buf = firstbuf;
2194 while (buf->b_next != NULL && buf->b_ml.ml_mfp == NULL)
2195 buf = buf->b_next;
2196 ea.line1 = buf->b_fnum;
2197 buf = lastbuf;
2198 while (buf->b_prev != NULL && buf->b_ml.ml_mfp == NULL)
2199 buf = buf->b_prev;
2200 ea.line2 = buf->b_fnum;
2201 break;
2202 case ADDR_BUFFERS:
2203 ea.line1 = firstbuf->b_fnum;
2204 ea.line2 = lastbuf->b_fnum;
2205 break;
2206 case ADDR_WINDOWS:
2207 ea.line2 = LAST_WIN_NR;
2208 break;
2209 case ADDR_TABS:
2210 ea.line2 = LAST_TAB_NR;
2211 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01002212 case ADDR_TABS_RELATIVE:
2213 ea.line2 = 1;
2214 break;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002215 case ADDR_ARGUMENTS:
2216 if (ARGCOUNT == 0)
2217 ea.line1 = ea.line2 = 0;
2218 else
2219 ea.line2 = ARGCOUNT;
2220 break;
Bram Moolenaar25190db2019-05-04 15:05:28 +02002221 case ADDR_QUICKFIX_VALID:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02002222#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02002223 ea.line2 = qf_get_valid_size(&ea);
Bram Moolenaaraa23b372015-09-08 18:46:31 +02002224 if (ea.line2 == 0)
2225 ea.line2 = 1;
Bram Moolenaare906c502015-09-09 21:10:39 +02002226#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02002227 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02002228 case ADDR_NONE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02002229 case ADDR_UNSIGNED:
2230 case ADDR_QUICKFIX:
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002231 iemsg(_("INTERNAL: Cannot use EX_DFLALL with ADDR_NONE, ADDR_UNSIGNED or ADDR_QUICKFIX"));
Bram Moolenaarb7316892019-05-01 18:08:42 +02002232 break;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002233 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002234 }
2235
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002236 // accept numbered register only when no count allowed (:put)
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002237 if ( (ea.argt & EX_REGSTR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002238 && *ea.arg != NUL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002239 // Do not allow register = for user commands
Bram Moolenaar958636c2014-10-21 20:01:58 +02002240 && (!IS_USER_CMDIDX(ea.cmdidx) || *ea.arg != '=')
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002241 && !((ea.argt & EX_COUNT) && VIM_ISDIGIT(*ea.arg)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002242 {
Bram Moolenaar85de2062011-05-05 14:26:41 +02002243#ifndef FEAT_CLIPBOARD
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002244 // check these explicitly for a more specific error message
Bram Moolenaar85de2062011-05-05 14:26:41 +02002245 if (*ea.arg == '*' || *ea.arg == '+')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002246 {
Bram Moolenaar99b12722019-01-14 20:16:40 +01002247 errormsg = _(e_invalidreg);
Bram Moolenaar85de2062011-05-05 14:26:41 +02002248 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002249 }
2250#endif
Bram Moolenaar958636c2014-10-21 20:01:58 +02002251 if (valid_yank_reg(*ea.arg, (ea.cmdidx != CMD_put
2252 && !IS_USER_CMDIDX(ea.cmdidx))))
Bram Moolenaar85de2062011-05-05 14:26:41 +02002253 {
2254 ea.regname = *ea.arg++;
2255#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002256 // for '=' register: accept the rest of the line as an expression
Bram Moolenaar85de2062011-05-05 14:26:41 +02002257 if (ea.arg[-1] == '=' && ea.arg[0] != NUL)
2258 {
2259 set_expr_line(vim_strsave(ea.arg));
2260 ea.arg += STRLEN(ea.arg);
2261 }
2262#endif
2263 ea.arg = skipwhite(ea.arg);
2264 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002265 }
2266
2267 /*
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002268 * Check for a count. When accepting a EX_BUFNAME, don't use "123foo" as a
Bram Moolenaar071d4272004-06-13 20:20:40 +00002269 * count, it's a buffer name.
2270 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002271 if ((ea.argt & EX_COUNT) && VIM_ISDIGIT(*ea.arg)
2272 && (!(ea.argt & EX_BUFNAME) || *(p = skipdigits(ea.arg)) == NUL
Bram Moolenaar1c465442017-03-12 20:10:05 +01002273 || VIM_ISWHITE(*p)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002274 {
2275 n = getdigits(&ea.arg);
2276 ea.arg = skipwhite(ea.arg);
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002277 if (n <= 0 && !ni && (ea.argt & EX_ZEROR) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002278 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002279 errormsg = _(e_zerocount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002280 goto doend;
2281 }
Bram Moolenaarb7316892019-05-01 18:08:42 +02002282 if (ea.addr_type != ADDR_LINES) // e.g. :buffer 2, :sleep 3
Bram Moolenaar071d4272004-06-13 20:20:40 +00002283 {
2284 ea.line2 = n;
2285 if (ea.addr_count == 0)
2286 ea.addr_count = 1;
2287 }
2288 else
2289 {
2290 ea.line1 = ea.line2;
2291 ea.line2 += n - 1;
2292 ++ea.addr_count;
2293 /*
2294 * Be vi compatible: no error message for out of range.
2295 */
Bram Moolenaarb7316892019-05-01 18:08:42 +02002296 if (ea.line2 > curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002297 ea.line2 = curbuf->b_ml.ml_line_count;
2298 }
2299 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00002300
2301 /*
2302 * Check for flags: 'l', 'p' and '#'.
2303 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002304 if (ea.argt & EX_FLAGS)
Bram Moolenaardf177f62005-02-22 08:39:57 +00002305 get_flags(&ea);
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002306 if (!ni && !(ea.argt & EX_EXTRA) && *ea.arg != NUL
2307 && *ea.arg != '"' && (*ea.arg != '|' || (ea.argt & EX_TRLBAR) == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002308 {
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002309 // no arguments allowed but there is something
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002310 errormsg = _(e_trailing);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002311 goto doend;
2312 }
2313
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002314 if (!ni && (ea.argt & EX_NEEDARG) && *ea.arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002315 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002316 errormsg = _(e_argreq);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002317 goto doend;
2318 }
2319
2320#ifdef FEAT_EVAL
2321 /*
2322 * Skip the command when it's not going to be executed.
2323 * The commands like :if, :endif, etc. always need to be executed.
2324 * Also make an exception for commands that handle a trailing command
2325 * themselves.
2326 */
2327 if (ea.skip)
2328 {
2329 switch (ea.cmdidx)
2330 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002331 // commands that need evaluation
Bram Moolenaar071d4272004-06-13 20:20:40 +00002332 case CMD_while:
2333 case CMD_endwhile:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00002334 case CMD_for:
2335 case CMD_endfor:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002336 case CMD_if:
2337 case CMD_elseif:
2338 case CMD_else:
2339 case CMD_endif:
2340 case CMD_try:
2341 case CMD_catch:
2342 case CMD_finally:
2343 case CMD_endtry:
2344 case CMD_function:
2345 break;
2346
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002347 // Commands that handle '|' themselves. Check: A command should
2348 // either have the EX_TRLBAR flag, appear in this list or appear in
2349 // the list at ":help :bar".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002350 case CMD_aboveleft:
2351 case CMD_and:
2352 case CMD_belowright:
2353 case CMD_botright:
2354 case CMD_browse:
2355 case CMD_call:
2356 case CMD_confirm:
Bram Moolenaar8f76e6b2019-11-26 16:50:30 +01002357 case CMD_const:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002358 case CMD_delfunction:
2359 case CMD_djump:
2360 case CMD_dlist:
2361 case CMD_dsearch:
2362 case CMD_dsplit:
2363 case CMD_echo:
2364 case CMD_echoerr:
2365 case CMD_echomsg:
2366 case CMD_echon:
2367 case CMD_execute:
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002368 case CMD_filter:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002369 case CMD_help:
2370 case CMD_hide:
2371 case CMD_ijump:
2372 case CMD_ilist:
2373 case CMD_isearch:
2374 case CMD_isplit:
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002375 case CMD_keepalt:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002376 case CMD_keepjumps:
2377 case CMD_keepmarks:
Bram Moolenaara939e432013-11-09 05:30:26 +01002378 case CMD_keeppatterns:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002379 case CMD_leftabove:
2380 case CMD_let:
2381 case CMD_lockmarks:
Bram Moolenaarcc4423a2019-11-26 17:05:00 +01002382 case CMD_lockvar:
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002383 case CMD_lua:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002384 case CMD_match:
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002385 case CMD_mzscheme:
Bram Moolenaar5803ae62014-03-23 16:04:02 +01002386 case CMD_noautocmd:
2387 case CMD_noswapfile:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002388 case CMD_perl:
2389 case CMD_psearch:
2390 case CMD_python:
Bram Moolenaar368373e2010-07-19 20:46:22 +02002391 case CMD_py3:
Bram Moolenaarb6590522010-07-21 16:00:43 +02002392 case CMD_python3:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002393 case CMD_return:
2394 case CMD_rightbelow:
2395 case CMD_ruby:
2396 case CMD_silent:
2397 case CMD_smagic:
2398 case CMD_snomagic:
2399 case CMD_substitute:
2400 case CMD_syntax:
Bram Moolenaara226a6d2006-02-26 23:59:20 +00002401 case CMD_tab:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002402 case CMD_tcl:
2403 case CMD_throw:
2404 case CMD_tilde:
2405 case CMD_topleft:
2406 case CMD_unlet:
Bram Moolenaarcc4423a2019-11-26 17:05:00 +01002407 case CMD_unlockvar:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002408 case CMD_verbose:
2409 case CMD_vertical:
Bram Moolenaar12bde492011-06-13 01:19:56 +02002410 case CMD_wincmd:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002411 break;
2412
2413 default: goto doend;
2414 }
2415 }
2416#endif
2417
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002418 if (ea.argt & EX_XFILE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002419 {
2420 if (expand_filename(&ea, cmdlinep, &errormsg) == FAIL)
2421 goto doend;
2422 }
2423
Bram Moolenaar071d4272004-06-13 20:20:40 +00002424 /*
2425 * Accept buffer name. Cannot be used at the same time with a buffer
2426 * number. Don't do this for a user command.
2427 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002428 if ((ea.argt & EX_BUFNAME) && *ea.arg != NUL && ea.addr_count == 0
Bram Moolenaar958636c2014-10-21 20:01:58 +02002429 && !IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002430 {
2431 /*
2432 * :bdelete, :bwipeout and :bunload take several arguments, separated
2433 * by spaces: find next space (skipping over escaped characters).
2434 * The others take one argument: ignore trailing spaces.
2435 */
2436 if (ea.cmdidx == CMD_bdelete || ea.cmdidx == CMD_bwipeout
2437 || ea.cmdidx == CMD_bunload)
2438 p = skiptowhite_esc(ea.arg);
2439 else
2440 {
2441 p = ea.arg + STRLEN(ea.arg);
Bram Moolenaar1c465442017-03-12 20:10:05 +01002442 while (p > ea.arg && VIM_ISWHITE(p[-1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002443 --p;
2444 }
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002445 ea.line2 = buflist_findpat(ea.arg, p, (ea.argt & EX_BUFUNL) != 0,
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002446 FALSE, FALSE);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002447 if (ea.line2 < 0) // failed
Bram Moolenaar071d4272004-06-13 20:20:40 +00002448 goto doend;
2449 ea.addr_count = 1;
2450 ea.arg = skipwhite(p);
2451 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002452
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002453 // The :try command saves the emsg_silent flag, reset it here when
2454 // ":silent! try" was used, it should only apply to :try itself.
Bram Moolenaareffed932018-08-14 13:38:17 +02002455 if (ea.cmdidx == CMD_try && ea.did_esilent > 0)
Bram Moolenaar2be57332018-02-13 18:05:18 +01002456 {
Bram Moolenaareffed932018-08-14 13:38:17 +02002457 emsg_silent -= ea.did_esilent;
Bram Moolenaar2be57332018-02-13 18:05:18 +01002458 if (emsg_silent < 0)
2459 emsg_silent = 0;
Bram Moolenaareffed932018-08-14 13:38:17 +02002460 ea.did_esilent = 0;
Bram Moolenaar2be57332018-02-13 18:05:18 +01002461 }
2462
Bram Moolenaar071d4272004-06-13 20:20:40 +00002463/*
Bram Moolenaar2be57332018-02-13 18:05:18 +01002464 * 7. Execute the command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002465 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002466
Bram Moolenaar958636c2014-10-21 20:01:58 +02002467 if (IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002468 {
2469 /*
2470 * Execute a user-defined command.
2471 */
2472 do_ucmd(&ea);
2473 }
2474 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002475 {
2476 /*
Bram Moolenaarac9fb182019-04-27 13:04:13 +02002477 * Call the function to execute the builtin command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002478 */
2479 ea.errmsg = NULL;
2480 (cmdnames[ea.cmdidx].cmd_func)(&ea);
2481 if (ea.errmsg != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002482 errormsg = _(ea.errmsg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002483 }
2484
2485#ifdef FEAT_EVAL
2486 /*
2487 * If the command just executed called do_cmdline(), any throw or ":return"
2488 * or ":finish" encountered there must also check the cstack of the still
2489 * active do_cmdline() that called this do_one_cmd(). Rethrow an uncaught
2490 * exception, or reanimate a returned function or finished script file and
2491 * return or finish it again.
2492 */
2493 if (need_rethrow)
2494 do_throw(cstack);
2495 else if (check_cstack)
2496 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002497 if (source_finished(fgetline, cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002498 do_finish(&ea, TRUE);
Bram Moolenaar89d40322006-08-29 15:30:07 +00002499 else if (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002500 && current_func_returned())
2501 do_return(&ea, TRUE, FALSE, NULL);
2502 }
2503 need_rethrow = check_cstack = FALSE;
2504#endif
2505
2506doend:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002507 if (curwin->w_cursor.lnum == 0) // can happen with zero line number
Bram Moolenaar6de5e122017-04-20 21:55:44 +02002508 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002509 curwin->w_cursor.lnum = 1;
Bram Moolenaar6de5e122017-04-20 21:55:44 +02002510 curwin->w_cursor.col = 0;
2511 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002512
2513 if (errormsg != NULL && *errormsg != NUL && !did_emsg)
2514 {
2515 if (sourcing)
2516 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002517 if (errormsg != (char *)IObuff)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002518 {
2519 STRCPY(IObuff, errormsg);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002520 errormsg = (char *)IObuff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002521 }
Bram Moolenaara6f4d612011-09-21 19:10:46 +02002522 append_command(*cmdlinep);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002523 }
2524 emsg(errormsg);
2525 }
2526#ifdef FEAT_EVAL
2527 do_errthrow(cstack,
Bram Moolenaar958636c2014-10-21 20:01:58 +02002528 (ea.cmdidx != CMD_SIZE && !IS_USER_CMDIDX(ea.cmdidx))
2529 ? cmdnames[(int)ea.cmdidx].cmd_name : (char_u *)NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002530#endif
2531
Bram Moolenaareffed932018-08-14 13:38:17 +02002532 if (ea.verbose_save >= 0)
2533 p_verbose = ea.verbose_save;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002534
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002535 free_cmdmod();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002536 cmdmod = save_cmdmod;
Bram Moolenaar9a2c0912019-03-30 14:26:18 +01002537 reg_executing = save_reg_executing;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002538
Bram Moolenaareffed932018-08-14 13:38:17 +02002539 if (ea.save_msg_silent != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002540 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002541 // messages could be enabled for a serious error, need to check if the
2542 // counters don't become negative
Bram Moolenaareffed932018-08-14 13:38:17 +02002543 if (!did_emsg || msg_silent > ea.save_msg_silent)
2544 msg_silent = ea.save_msg_silent;
2545 emsg_silent -= ea.did_esilent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002546 if (emsg_silent < 0)
2547 emsg_silent = 0;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002548 // Restore msg_scroll, it's set by file I/O commands, even when no
2549 // message is actually displayed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002550 msg_scroll = save_msg_scroll;
Bram Moolenaar77ab2802009-04-22 12:44:48 +00002551
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002552 // "silent reg" or "silent echo x" inside "redir" leaves msg_col
2553 // somewhere in the line. Put it back in the first column.
Bram Moolenaar77ab2802009-04-22 12:44:48 +00002554 if (redirecting())
2555 msg_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002556 }
2557
Bram Moolenaar7171abe2004-10-11 10:06:20 +00002558#ifdef HAVE_SANDBOX
Bram Moolenaareffed932018-08-14 13:38:17 +02002559 if (ea.did_sandbox)
Bram Moolenaar7171abe2004-10-11 10:06:20 +00002560 --sandbox;
2561#endif
2562
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002563 if (ea.nextcmd && *ea.nextcmd == NUL) // not really a next command
Bram Moolenaar071d4272004-06-13 20:20:40 +00002564 ea.nextcmd = NULL;
2565
2566#ifdef FEAT_EVAL
2567 --ex_nesting_level;
2568#endif
2569
2570 return ea.nextcmd;
2571}
2572#if (_MSC_VER == 1200)
Bram Moolenaar281bdce2005-01-25 21:53:18 +00002573 #pragma optimize( "", on )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002574#endif
2575
2576/*
Bram Moolenaareffed932018-08-14 13:38:17 +02002577 * Parse and skip over command modifiers:
2578 * - update eap->cmd
2579 * - store flags in "cmdmod".
2580 * - Set ex_pressedreturn for an empty command line.
2581 * - set msg_silent for ":silent"
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002582 * - set 'eventignore' to "all" for ":noautocmd"
Bram Moolenaareffed932018-08-14 13:38:17 +02002583 * - set p_verbose for ":verbose"
2584 * - Increment "sandbox" for ":sandbox"
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002585 * When "skip_only" is TRUE the global variables are not changed, except for
2586 * "cmdmod".
Bram Moolenaareffed932018-08-14 13:38:17 +02002587 * Return FAIL when the command is not to be executed.
2588 * May set "errormsg" to an error message.
2589 */
2590 int
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002591parse_command_modifiers(exarg_T *eap, char **errormsg, int skip_only)
Bram Moolenaareffed932018-08-14 13:38:17 +02002592{
2593 char_u *p;
2594
2595 vim_memset(&cmdmod, 0, sizeof(cmdmod));
2596 eap->verbose_save = -1;
2597 eap->save_msg_silent = -1;
2598
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002599 // Repeat until no more command modifiers are found.
Bram Moolenaareffed932018-08-14 13:38:17 +02002600 for (;;)
2601 {
Bram Moolenaareffed932018-08-14 13:38:17 +02002602 while (*eap->cmd == ' ' || *eap->cmd == '\t' || *eap->cmd == ':')
2603 ++eap->cmd;
2604
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002605 // in ex mode, an empty line works like :+
Bram Moolenaareffed932018-08-14 13:38:17 +02002606 if (*eap->cmd == NUL && exmode_active
2607 && (getline_equal(eap->getline, eap->cookie, getexmodeline)
2608 || getline_equal(eap->getline, eap->cookie, getexline))
2609 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
2610 {
2611 eap->cmd = (char_u *)"+";
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002612 if (!skip_only)
2613 ex_pressedreturn = TRUE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002614 }
2615
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002616 // ignore comment and empty lines
Bram Moolenaareffed932018-08-14 13:38:17 +02002617 if (*eap->cmd == '"')
2618 return FAIL;
2619 if (*eap->cmd == NUL)
2620 {
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002621 if (!skip_only)
2622 ex_pressedreturn = TRUE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002623 return FAIL;
2624 }
2625
Bram Moolenaareffed932018-08-14 13:38:17 +02002626 p = skip_range(eap->cmd, NULL);
2627 switch (*p)
2628 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002629 // When adding an entry, also modify cmd_exists().
Bram Moolenaareffed932018-08-14 13:38:17 +02002630 case 'a': if (!checkforcmd(&eap->cmd, "aboveleft", 3))
2631 break;
2632 cmdmod.split |= WSP_ABOVE;
2633 continue;
2634
2635 case 'b': if (checkforcmd(&eap->cmd, "belowright", 3))
2636 {
2637 cmdmod.split |= WSP_BELOW;
2638 continue;
2639 }
2640 if (checkforcmd(&eap->cmd, "browse", 3))
2641 {
2642#ifdef FEAT_BROWSE_CMD
2643 cmdmod.browse = TRUE;
2644#endif
2645 continue;
2646 }
2647 if (!checkforcmd(&eap->cmd, "botright", 2))
2648 break;
2649 cmdmod.split |= WSP_BOT;
2650 continue;
2651
2652 case 'c': if (!checkforcmd(&eap->cmd, "confirm", 4))
2653 break;
2654#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
2655 cmdmod.confirm = TRUE;
2656#endif
2657 continue;
2658
2659 case 'k': if (checkforcmd(&eap->cmd, "keepmarks", 3))
2660 {
2661 cmdmod.keepmarks = TRUE;
2662 continue;
2663 }
2664 if (checkforcmd(&eap->cmd, "keepalt", 5))
2665 {
2666 cmdmod.keepalt = TRUE;
2667 continue;
2668 }
2669 if (checkforcmd(&eap->cmd, "keeppatterns", 5))
2670 {
2671 cmdmod.keeppatterns = TRUE;
2672 continue;
2673 }
2674 if (!checkforcmd(&eap->cmd, "keepjumps", 5))
2675 break;
2676 cmdmod.keepjumps = TRUE;
2677 continue;
2678
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002679 case 'f': // only accept ":filter {pat} cmd"
Bram Moolenaareffed932018-08-14 13:38:17 +02002680 {
2681 char_u *reg_pat;
2682
2683 if (!checkforcmd(&p, "filter", 4)
2684 || *p == NUL || ends_excmd(*p))
2685 break;
2686 if (*p == '!')
2687 {
2688 cmdmod.filter_force = TRUE;
2689 p = skipwhite(p + 1);
2690 if (*p == NUL || ends_excmd(*p))
2691 break;
2692 }
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002693 if (skip_only)
2694 p = skip_vimgrep_pat(p, NULL, NULL);
2695 else
2696 // NOTE: This puts a NUL after the pattern.
2697 p = skip_vimgrep_pat(p, &reg_pat, NULL);
Bram Moolenaareffed932018-08-14 13:38:17 +02002698 if (p == NULL || *p == NUL)
2699 break;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002700 if (!skip_only)
2701 {
2702 cmdmod.filter_regmatch.regprog =
Bram Moolenaareffed932018-08-14 13:38:17 +02002703 vim_regcomp(reg_pat, RE_MAGIC);
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002704 if (cmdmod.filter_regmatch.regprog == NULL)
2705 break;
2706 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002707 eap->cmd = p;
2708 continue;
2709 }
2710
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002711 // ":hide" and ":hide | cmd" are not modifiers
Bram Moolenaareffed932018-08-14 13:38:17 +02002712 case 'h': if (p != eap->cmd || !checkforcmd(&p, "hide", 3)
2713 || *p == NUL || ends_excmd(*p))
2714 break;
2715 eap->cmd = p;
2716 cmdmod.hide = TRUE;
2717 continue;
2718
2719 case 'l': if (checkforcmd(&eap->cmd, "lockmarks", 3))
2720 {
2721 cmdmod.lockmarks = TRUE;
2722 continue;
2723 }
2724
2725 if (!checkforcmd(&eap->cmd, "leftabove", 5))
2726 break;
2727 cmdmod.split |= WSP_ABOVE;
2728 continue;
2729
2730 case 'n': if (checkforcmd(&eap->cmd, "noautocmd", 3))
2731 {
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002732 if (cmdmod.save_ei == NULL && !skip_only)
Bram Moolenaareffed932018-08-14 13:38:17 +02002733 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002734 // Set 'eventignore' to "all". Restore the
2735 // existing option value later.
Bram Moolenaareffed932018-08-14 13:38:17 +02002736 cmdmod.save_ei = vim_strsave(p_ei);
2737 set_string_option_direct((char_u *)"ei", -1,
2738 (char_u *)"all", OPT_FREE, SID_NONE);
2739 }
2740 continue;
2741 }
2742 if (!checkforcmd(&eap->cmd, "noswapfile", 3))
2743 break;
2744 cmdmod.noswapfile = TRUE;
2745 continue;
2746
2747 case 'r': if (!checkforcmd(&eap->cmd, "rightbelow", 6))
2748 break;
2749 cmdmod.split |= WSP_BELOW;
2750 continue;
2751
2752 case 's': if (checkforcmd(&eap->cmd, "sandbox", 3))
2753 {
2754#ifdef HAVE_SANDBOX
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002755 if (!skip_only)
2756 {
2757 if (!eap->did_sandbox)
2758 ++sandbox;
2759 eap->did_sandbox = TRUE;
2760 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002761#endif
2762 continue;
2763 }
2764 if (!checkforcmd(&eap->cmd, "silent", 3))
2765 break;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002766 if (!skip_only)
2767 {
2768 if (eap->save_msg_silent == -1)
2769 eap->save_msg_silent = msg_silent;
2770 ++msg_silent;
2771 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002772 if (*eap->cmd == '!' && !VIM_ISWHITE(eap->cmd[-1]))
2773 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002774 // ":silent!", but not "silent !cmd"
Bram Moolenaareffed932018-08-14 13:38:17 +02002775 eap->cmd = skipwhite(eap->cmd + 1);
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002776 if (!skip_only)
2777 {
2778 ++emsg_silent;
2779 ++eap->did_esilent;
2780 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002781 }
2782 continue;
2783
2784 case 't': if (checkforcmd(&p, "tab", 3))
2785 {
Bram Moolenaar548e5982018-12-26 21:45:00 +01002786 if (!skip_only)
Bram Moolenaareffed932018-08-14 13:38:17 +02002787 {
Bram Moolenaar548e5982018-12-26 21:45:00 +01002788 long tabnr = get_address(eap, &eap->cmd,
2789 ADDR_TABS, eap->skip,
2790 skip_only, FALSE, 1);
2791 if (tabnr == MAXLNUM)
2792 cmdmod.tab = tabpage_index(curtab) + 1;
2793 else
Bram Moolenaareffed932018-08-14 13:38:17 +02002794 {
Bram Moolenaar548e5982018-12-26 21:45:00 +01002795 if (tabnr < 0 || tabnr > LAST_TAB_NR)
2796 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002797 *errormsg = _(e_invrange);
Bram Moolenaar548e5982018-12-26 21:45:00 +01002798 return FAIL;
2799 }
2800 cmdmod.tab = tabnr + 1;
Bram Moolenaareffed932018-08-14 13:38:17 +02002801 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002802 }
2803 eap->cmd = p;
2804 continue;
2805 }
2806 if (!checkforcmd(&eap->cmd, "topleft", 2))
2807 break;
2808 cmdmod.split |= WSP_TOP;
2809 continue;
2810
2811 case 'u': if (!checkforcmd(&eap->cmd, "unsilent", 3))
2812 break;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002813 if (!skip_only)
2814 {
2815 if (eap->save_msg_silent == -1)
2816 eap->save_msg_silent = msg_silent;
2817 msg_silent = 0;
2818 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002819 continue;
2820
2821 case 'v': if (checkforcmd(&eap->cmd, "vertical", 4))
2822 {
2823 cmdmod.split |= WSP_VERT;
2824 continue;
2825 }
2826 if (!checkforcmd(&p, "verbose", 4))
2827 break;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002828 if (!skip_only)
2829 {
2830 if (eap->verbose_save < 0)
2831 eap->verbose_save = p_verbose;
2832 if (vim_isdigit(*eap->cmd))
2833 p_verbose = atoi((char *)eap->cmd);
2834 else
2835 p_verbose = 1;
2836 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002837 eap->cmd = p;
2838 continue;
2839 }
2840 break;
2841 }
2842
2843 return OK;
2844}
2845
2846/*
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002847 * Free contents of "cmdmod".
2848 */
2849 static void
2850free_cmdmod(void)
2851{
2852 if (cmdmod.save_ei != NULL)
2853 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002854 // Restore 'eventignore' to the value before ":noautocmd".
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002855 set_string_option_direct((char_u *)"ei", -1, cmdmod.save_ei,
2856 OPT_FREE, SID_NONE);
2857 free_string_option(cmdmod.save_ei);
2858 }
2859
2860 if (cmdmod.filter_regmatch.regprog != NULL)
2861 vim_regfree(cmdmod.filter_regmatch.regprog);
2862}
2863
2864/*
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002865 * Parse the address range, if any, in "eap".
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02002866 * May set the last search pattern, unless "silent" is TRUE.
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002867 * Return FAIL and set "errormsg" or return OK.
2868 */
2869 int
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002870parse_cmd_address(exarg_T *eap, char **errormsg, int silent)
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002871{
2872 int address_count = 1;
2873 linenr_T lnum;
2874
2875 // Repeat for all ',' or ';' separated addresses.
2876 for (;;)
2877 {
2878 eap->line1 = eap->line2;
2879 switch (eap->addr_type)
2880 {
2881 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02002882 case ADDR_OTHER:
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002883 // default is current line number
2884 eap->line2 = curwin->w_cursor.lnum;
2885 break;
2886 case ADDR_WINDOWS:
2887 eap->line2 = CURRENT_WIN_NR;
2888 break;
2889 case ADDR_ARGUMENTS:
2890 eap->line2 = curwin->w_arg_idx + 1;
2891 if (eap->line2 > ARGCOUNT)
2892 eap->line2 = ARGCOUNT;
2893 break;
2894 case ADDR_LOADED_BUFFERS:
2895 case ADDR_BUFFERS:
2896 eap->line2 = curbuf->b_fnum;
2897 break;
2898 case ADDR_TABS:
2899 eap->line2 = CURRENT_TAB_NR;
2900 break;
2901 case ADDR_TABS_RELATIVE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02002902 case ADDR_UNSIGNED:
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002903 eap->line2 = 1;
2904 break;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002905 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02002906#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02002907 eap->line2 = qf_get_cur_idx(eap);
2908#endif
2909 break;
2910 case ADDR_QUICKFIX_VALID:
2911#ifdef FEAT_QUICKFIX
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002912 eap->line2 = qf_get_cur_valid_idx(eap);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002913#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02002914 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02002915 case ADDR_NONE:
2916 // Will give an error later if a range is found.
2917 break;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002918 }
2919 eap->cmd = skipwhite(eap->cmd);
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02002920 lnum = get_address(eap, &eap->cmd, eap->addr_type, eap->skip, silent,
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002921 eap->addr_count == 0, address_count++);
2922 if (eap->cmd == NULL) // error detected
2923 return FAIL;
2924 if (lnum == MAXLNUM)
2925 {
2926 if (*eap->cmd == '%') // '%' - all lines
2927 {
2928 ++eap->cmd;
2929 switch (eap->addr_type)
2930 {
2931 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02002932 case ADDR_OTHER:
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002933 eap->line1 = 1;
2934 eap->line2 = curbuf->b_ml.ml_line_count;
2935 break;
2936 case ADDR_LOADED_BUFFERS:
2937 {
2938 buf_T *buf = firstbuf;
2939
2940 while (buf->b_next != NULL
2941 && buf->b_ml.ml_mfp == NULL)
2942 buf = buf->b_next;
2943 eap->line1 = buf->b_fnum;
2944 buf = lastbuf;
2945 while (buf->b_prev != NULL
2946 && buf->b_ml.ml_mfp == NULL)
2947 buf = buf->b_prev;
2948 eap->line2 = buf->b_fnum;
2949 break;
2950 }
2951 case ADDR_BUFFERS:
2952 eap->line1 = firstbuf->b_fnum;
2953 eap->line2 = lastbuf->b_fnum;
2954 break;
2955 case ADDR_WINDOWS:
2956 case ADDR_TABS:
2957 if (IS_USER_CMDIDX(eap->cmdidx))
2958 {
2959 eap->line1 = 1;
2960 eap->line2 = eap->addr_type == ADDR_WINDOWS
2961 ? LAST_WIN_NR : LAST_TAB_NR;
2962 }
2963 else
2964 {
2965 // there is no Vim command which uses '%' and
2966 // ADDR_WINDOWS or ADDR_TABS
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002967 *errormsg = _(e_invrange);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002968 return FAIL;
2969 }
2970 break;
2971 case ADDR_TABS_RELATIVE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02002972 case ADDR_UNSIGNED:
2973 case ADDR_QUICKFIX:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002974 *errormsg = _(e_invrange);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002975 return FAIL;
2976 case ADDR_ARGUMENTS:
2977 if (ARGCOUNT == 0)
2978 eap->line1 = eap->line2 = 0;
2979 else
2980 {
2981 eap->line1 = 1;
2982 eap->line2 = ARGCOUNT;
2983 }
2984 break;
Bram Moolenaar25190db2019-05-04 15:05:28 +02002985 case ADDR_QUICKFIX_VALID:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02002986#ifdef FEAT_QUICKFIX
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002987 eap->line1 = 1;
Bram Moolenaar25190db2019-05-04 15:05:28 +02002988 eap->line2 = qf_get_valid_size(eap);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002989 if (eap->line2 == 0)
2990 eap->line2 = 1;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002991#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02002992 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02002993 case ADDR_NONE:
2994 // Will give an error later if a range is found.
2995 break;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002996 }
2997 ++eap->addr_count;
2998 }
2999 else if (*eap->cmd == '*' && vim_strchr(p_cpo, CPO_STAR) == NULL)
3000 {
3001 pos_T *fp;
3002
3003 // '*' - visual area
3004 if (eap->addr_type != ADDR_LINES)
3005 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003006 *errormsg = _(e_invrange);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003007 return FAIL;
3008 }
3009
3010 ++eap->cmd;
3011 if (!eap->skip)
3012 {
3013 fp = getmark('<', FALSE);
3014 if (check_mark(fp) == FAIL)
3015 return FAIL;
3016 eap->line1 = fp->lnum;
3017 fp = getmark('>', FALSE);
3018 if (check_mark(fp) == FAIL)
3019 return FAIL;
3020 eap->line2 = fp->lnum;
3021 ++eap->addr_count;
3022 }
3023 }
3024 }
3025 else
3026 eap->line2 = lnum;
3027 eap->addr_count++;
3028
3029 if (*eap->cmd == ';')
3030 {
3031 if (!eap->skip)
3032 {
3033 curwin->w_cursor.lnum = eap->line2;
3034 // don't leave the cursor on an illegal line or column
3035 check_cursor();
3036 }
3037 }
3038 else if (*eap->cmd != ',')
3039 break;
3040 ++eap->cmd;
3041 }
3042
3043 // One address given: set start and end lines.
3044 if (eap->addr_count == 1)
3045 {
3046 eap->line1 = eap->line2;
3047 // ... but only implicit: really no address given
3048 if (lnum == MAXLNUM)
3049 eap->addr_count = 0;
3050 }
3051 return OK;
3052}
3053
3054/*
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00003055 * Check for an Ex command with optional tail.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003056 * If there is a match advance "pp" to the argument and return TRUE.
3057 */
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00003058 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003059checkforcmd(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003060 char_u **pp, // start of command
3061 char *cmd, // name of command
3062 int len) // required length
Bram Moolenaar071d4272004-06-13 20:20:40 +00003063{
3064 int i;
3065
3066 for (i = 0; cmd[i] != NUL; ++i)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003067 if (((char_u *)cmd)[i] != (*pp)[i])
Bram Moolenaar071d4272004-06-13 20:20:40 +00003068 break;
3069 if (i >= len && !isalpha((*pp)[i]))
3070 {
3071 *pp = skipwhite(*pp + i);
3072 return TRUE;
3073 }
3074 return FALSE;
3075}
3076
3077/*
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003078 * Append "cmd" to the error message in IObuff.
3079 * Takes care of limiting the length and handling 0xa0, which would be
3080 * invisible otherwise.
3081 */
3082 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003083append_command(char_u *cmd)
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003084{
3085 char_u *s = cmd;
3086 char_u *d;
3087
3088 STRCAT(IObuff, ": ");
3089 d = IObuff + STRLEN(IObuff);
3090 while (*s != NUL && d - IObuff < IOSIZE - 7)
3091 {
Bram Moolenaar13505972019-01-24 15:04:48 +01003092 if (enc_utf8 ? (s[0] == 0xc2 && s[1] == 0xa0) : *s == 0xa0)
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003093 {
Bram Moolenaar13505972019-01-24 15:04:48 +01003094 s += enc_utf8 ? 2 : 1;
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003095 STRCPY(d, "<a0>");
3096 d += 4;
3097 }
3098 else
3099 MB_COPY_CHAR(s, d);
3100 }
3101 *d = NUL;
3102}
3103
3104/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003105 * Find an Ex command by its name, either built-in or user.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003106 * Start of the name can be found at eap->cmd.
Bram Moolenaar25190db2019-05-04 15:05:28 +02003107 * Sets eap->cmdidx and returns a pointer to char after the command name.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003108 * "full" is set to TRUE if the whole command name matched.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003109 * Returns NULL for an ambiguous user command.
3110 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003111 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003112find_command(exarg_T *eap, int *full UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003113{
3114 int len;
3115 char_u *p;
Bram Moolenaardf177f62005-02-22 08:39:57 +00003116 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003117
3118 /*
3119 * Isolate the command and search for it in the command table.
Bram Moolenaar81870892007-11-11 18:17:28 +00003120 * Exceptions:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003121 * - the 'k' command can directly be followed by any character.
3122 * - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003123 * but :sre[wind] is another command, as are :scr[iptnames],
Bram Moolenaar071d4272004-06-13 20:20:40 +00003124 * :scs[cope], :sim[alt], :sig[ns] and :sil[ent].
Bram Moolenaardf177f62005-02-22 08:39:57 +00003125 * - the "d" command can directly be followed by 'l' or 'p' flag.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003126 */
3127 p = eap->cmd;
3128 if (*p == 'k')
3129 {
3130 eap->cmdidx = CMD_k;
3131 ++p;
3132 }
3133 else if (p[0] == 's'
Bram Moolenaar204b93f2015-08-04 22:02:51 +02003134 && ((p[1] == 'c' && (p[2] == NUL || (p[2] != 's' && p[2] != 'r'
3135 && (p[3] == NUL || (p[3] != 'i' && p[4] != 'p')))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003136 || p[1] == 'g'
3137 || (p[1] == 'i' && p[2] != 'm' && p[2] != 'l' && p[2] != 'g')
3138 || p[1] == 'I'
3139 || (p[1] == 'r' && p[2] != 'e')))
3140 {
3141 eap->cmdidx = CMD_substitute;
3142 ++p;
3143 }
3144 else
3145 {
3146 while (ASCII_ISALPHA(*p))
3147 ++p;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003148 // for python 3.x support ":py3", ":python3", ":py3file", etc.
Bram Moolenaar55d5c032010-07-17 23:52:29 +02003149 if (eap->cmd[0] == 'p' && eap->cmd[1] == 'y')
Bram Moolenaarb6590522010-07-21 16:00:43 +02003150 while (ASCII_ISALNUM(*p))
3151 ++p;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02003152
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003153 // check for non-alpha command
Bram Moolenaar071d4272004-06-13 20:20:40 +00003154 if (p == eap->cmd && vim_strchr((char_u *)"@*!=><&~#", *p) != NULL)
3155 ++p;
3156 len = (int)(p - eap->cmd);
Bram Moolenaardf177f62005-02-22 08:39:57 +00003157 if (*eap->cmd == 'd' && (p[-1] == 'l' || p[-1] == 'p'))
3158 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003159 // Check for ":dl", ":dell", etc. to ":deletel": that's
3160 // :delete with the 'l' flag. Same for 'p'.
Bram Moolenaardf177f62005-02-22 08:39:57 +00003161 for (i = 0; i < len; ++i)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003162 if (eap->cmd[i] != ((char_u *)"delete")[i])
Bram Moolenaardf177f62005-02-22 08:39:57 +00003163 break;
3164 if (i == len - 1)
3165 {
3166 --len;
3167 if (p[-1] == 'l')
3168 eap->flags |= EXFLAG_LIST;
3169 else
3170 eap->flags |= EXFLAG_PRINT;
3171 }
3172 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003173
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003174 if (ASCII_ISLOWER(eap->cmd[0]))
3175 {
Bram Moolenaar6c0c1e82017-03-25 15:07:43 +01003176 int c1 = eap->cmd[0];
Bram Moolenaar94f82cb2019-07-24 22:30:27 +02003177 int c2 = len == 1 ? NUL : eap->cmd[1];
Bram Moolenaar6c0c1e82017-03-25 15:07:43 +01003178
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003179 if (command_count != (int)CMD_SIZE)
3180 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003181 iemsg(_("E943: Command table needs to be updated, run 'make cmdidxs'"));
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003182 getout(1);
3183 }
3184
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003185 // Use a precomputed index for fast look-up in cmdnames[]
3186 // taking into account the first 2 letters of eap->cmd.
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003187 eap->cmdidx = cmdidxs1[CharOrdLow(c1)];
3188 if (ASCII_ISLOWER(c2))
3189 eap->cmdidx += cmdidxs2[CharOrdLow(c1)][CharOrdLow(c2)];
3190 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003191 else
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003192 eap->cmdidx = CMD_bang;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003193
3194 for ( ; (int)eap->cmdidx < (int)CMD_SIZE;
3195 eap->cmdidx = (cmdidx_T)((int)eap->cmdidx + 1))
3196 if (STRNCMP(cmdnames[(int)eap->cmdidx].cmd_name, (char *)eap->cmd,
3197 (size_t)len) == 0)
3198 {
3199#ifdef FEAT_EVAL
3200 if (full != NULL
3201 && cmdnames[(int)eap->cmdidx].cmd_name[len] == NUL)
3202 *full = TRUE;
3203#endif
3204 break;
3205 }
3206
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003207 // Look for a user defined command as a last resort. Let ":Print" be
3208 // overruled by a user defined command.
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01003209 if ((eap->cmdidx == CMD_SIZE || eap->cmdidx == CMD_Print)
3210 && *eap->cmd >= 'A' && *eap->cmd <= 'Z')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003211 {
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003212 // User defined commands may contain digits.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003213 while (ASCII_ISALNUM(*p))
3214 ++p;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003215 p = find_ucmd(eap, p, full, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003216 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003217 if (p == eap->cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003218 eap->cmdidx = CMD_SIZE;
3219 }
3220
3221 return p;
3222}
3223
3224#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003225static struct cmdmod
3226{
3227 char *name;
3228 int minlen;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003229 int has_count; // :123verbose :3tab
Bram Moolenaared53fb92007-11-24 20:50:24 +00003230} cmdmods[] = {
3231 {"aboveleft", 3, FALSE},
3232 {"belowright", 3, FALSE},
3233 {"botright", 2, FALSE},
3234 {"browse", 3, FALSE},
3235 {"confirm", 4, FALSE},
Bram Moolenaar7b668e82016-08-23 23:51:21 +02003236 {"filter", 4, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003237 {"hide", 3, FALSE},
3238 {"keepalt", 5, FALSE},
3239 {"keepjumps", 5, FALSE},
3240 {"keepmarks", 3, FALSE},
Bram Moolenaara939e432013-11-09 05:30:26 +01003241 {"keeppatterns", 5, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003242 {"leftabove", 5, FALSE},
3243 {"lockmarks", 3, FALSE},
Bram Moolenaarca9f9582008-09-18 10:44:28 +00003244 {"noautocmd", 3, FALSE},
Bram Moolenaar5803ae62014-03-23 16:04:02 +01003245 {"noswapfile", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003246 {"rightbelow", 6, FALSE},
3247 {"sandbox", 3, FALSE},
3248 {"silent", 3, FALSE},
3249 {"tab", 3, TRUE},
3250 {"topleft", 2, FALSE},
Bram Moolenaar8e258a42009-07-09 13:55:43 +00003251 {"unsilent", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003252 {"verbose", 4, TRUE},
3253 {"vertical", 4, FALSE},
3254};
3255
3256/*
3257 * Return length of a command modifier (including optional count).
3258 * Return zero when it's not a modifier.
3259 */
3260 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003261modifier_len(char_u *cmd)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003262{
3263 int i, j;
3264 char_u *p = cmd;
3265
3266 if (VIM_ISDIGIT(*cmd))
3267 p = skipwhite(skipdigits(cmd));
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00003268 for (i = 0; i < (int)(sizeof(cmdmods) / sizeof(struct cmdmod)); ++i)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003269 {
3270 for (j = 0; p[j] != NUL; ++j)
3271 if (p[j] != cmdmods[i].name[j])
3272 break;
Bram Moolenaar2d473ab2013-06-12 17:12:24 +02003273 if (!ASCII_ISALPHA(p[j]) && j >= cmdmods[i].minlen
Bram Moolenaared53fb92007-11-24 20:50:24 +00003274 && (p == cmd || cmdmods[i].has_count))
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00003275 return j + (int)(p - cmd);
Bram Moolenaared53fb92007-11-24 20:50:24 +00003276 }
3277 return 0;
3278}
3279
Bram Moolenaar071d4272004-06-13 20:20:40 +00003280/*
3281 * Return > 0 if an Ex command "name" exists.
3282 * Return 2 if there is an exact match.
3283 * Return 3 if there is an ambiguous match.
3284 */
3285 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003286cmd_exists(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003287{
3288 exarg_T ea;
3289 int full = FALSE;
3290 int i;
3291 int j;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003292 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003293
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003294 // Check command modifiers.
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00003295 for (i = 0; i < (int)(sizeof(cmdmods) / sizeof(struct cmdmod)); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003296 {
3297 for (j = 0; name[j] != NUL; ++j)
3298 if (name[j] != cmdmods[i].name[j])
3299 break;
3300 if (name[j] == NUL && j >= cmdmods[i].minlen)
3301 return (cmdmods[i].name[j] == NUL ? 2 : 1);
3302 }
3303
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003304 // Check built-in commands and user defined commands.
3305 // For ":2match" and ":3match" we need to skip the number.
Bram Moolenaara9587612006-05-04 21:47:50 +00003306 ea.cmd = (*name == '2' || *name == '3') ? name + 1 : name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003307 ea.cmdidx = (cmdidx_T)0;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003308 p = find_command(&ea, &full);
3309 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003310 return 3;
Bram Moolenaara9587612006-05-04 21:47:50 +00003311 if (vim_isdigit(*name) && ea.cmdidx != CMD_match)
3312 return 0;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003313 if (*skipwhite(p) != NUL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003314 return 0; // trailing garbage
Bram Moolenaar071d4272004-06-13 20:20:40 +00003315 return (ea.cmdidx == CMD_SIZE ? 0 : (full ? 2 : 1));
3316}
3317#endif
3318
Bram Moolenaard0190392019-08-23 21:17:35 +02003319 cmdidx_T
3320excmd_get_cmdidx(char_u *cmd, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003321{
Bram Moolenaard0190392019-08-23 21:17:35 +02003322 cmdidx_T idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003323
Bram Moolenaard0190392019-08-23 21:17:35 +02003324 for (idx = (cmdidx_T)0; (int)idx < (int)CMD_SIZE;
3325 idx = (cmdidx_T)((int)idx + 1))
3326 if (STRNCMP(cmdnames[(int)idx].cmd_name, cmd, (size_t)len) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003327 break;
3328
Bram Moolenaard0190392019-08-23 21:17:35 +02003329 return idx;
3330}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003331
Bram Moolenaard0190392019-08-23 21:17:35 +02003332 long
3333excmd_get_argt(cmdidx_T idx)
3334{
3335 return (long)cmdnames[(int)idx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003336}
3337
3338/*
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003339 * Skip a range specifier of the form: addr [,addr] [;addr] ..
Bram Moolenaar071d4272004-06-13 20:20:40 +00003340 *
3341 * Backslashed delimiters after / or ? will be skipped, and commands will
3342 * not be expanded between /'s and ?'s or after "'".
3343 *
Bram Moolenaardf177f62005-02-22 08:39:57 +00003344 * Also skip white space and ":" characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003345 * Returns the "cmd" pointer advanced to beyond the range.
3346 */
3347 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003348skip_range(
3349 char_u *cmd,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003350 int *ctx) // pointer to xp_context or NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003351{
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003352 unsigned delim;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003353
Bram Moolenaarcbf20fb2017-02-03 21:19:04 +01003354 while (vim_strchr((char_u *)" \t0123456789.$%'/?-+,;\\", *cmd) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003355 {
Bram Moolenaarcbf20fb2017-02-03 21:19:04 +01003356 if (*cmd == '\\')
3357 {
3358 if (cmd[1] == '?' || cmd[1] == '/' || cmd[1] == '&')
3359 ++cmd;
3360 else
3361 break;
3362 }
3363 else if (*cmd == '\'')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003364 {
3365 if (*++cmd == NUL && ctx != NULL)
3366 *ctx = EXPAND_NOTHING;
3367 }
3368 else if (*cmd == '/' || *cmd == '?')
3369 {
3370 delim = *cmd++;
3371 while (*cmd != NUL && *cmd != delim)
3372 if (*cmd++ == '\\' && *cmd != NUL)
3373 ++cmd;
3374 if (*cmd == NUL && ctx != NULL)
3375 *ctx = EXPAND_NOTHING;
3376 }
3377 if (*cmd != NUL)
3378 ++cmd;
3379 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00003380
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003381 // Skip ":" and white space.
Bram Moolenaardf177f62005-02-22 08:39:57 +00003382 while (*cmd == ':')
3383 cmd = skipwhite(cmd + 1);
3384
Bram Moolenaar071d4272004-06-13 20:20:40 +00003385 return cmd;
3386}
3387
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003388 static void
3389addr_error(cmd_addr_T addr_type)
3390{
3391 if (addr_type == ADDR_NONE)
3392 emsg(_(e_norange));
3393 else
3394 emsg(_(e_invrange));
3395}
3396
Bram Moolenaar071d4272004-06-13 20:20:40 +00003397/*
Bram Moolenaar198cb662018-09-06 21:44:17 +02003398 * Get a single EX address.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003399 *
3400 * Set ptr to the next character after the part that was interpreted.
3401 * Set ptr to NULL when an error is encountered.
Bram Moolenaar198cb662018-09-06 21:44:17 +02003402 * This may set the last used search pattern.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003403 *
3404 * Return MAXLNUM when no Ex address was found.
3405 */
3406 static linenr_T
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003407get_address(
3408 exarg_T *eap UNUSED,
3409 char_u **ptr,
Bram Moolenaar1b03a192019-12-08 17:08:29 +01003410 cmd_addr_T addr_type,
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02003411 int skip, // only skip the address, don't use it
3412 int silent, // no errors or side effects
3413 int to_other_file, // flag: may jump to other file
3414 int address_count UNUSED) // 1 for first address, >1 after comma
Bram Moolenaar071d4272004-06-13 20:20:40 +00003415{
3416 int c;
3417 int i;
3418 long n;
3419 char_u *cmd;
3420 pos_T pos;
3421 pos_T *fp;
3422 linenr_T lnum;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003423 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003424
3425 cmd = skipwhite(*ptr);
3426 lnum = MAXLNUM;
3427 do
3428 {
3429 switch (*cmd)
3430 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003431 case '.': // '.' - Cursor position
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003432 ++cmd;
3433 switch (addr_type)
3434 {
3435 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02003436 case ADDR_OTHER:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003437 lnum = curwin->w_cursor.lnum;
3438 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003439 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01003440 lnum = CURRENT_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003441 break;
3442 case ADDR_ARGUMENTS:
3443 lnum = curwin->w_arg_idx + 1;
3444 break;
3445 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003446 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003447 lnum = curbuf->b_fnum;
3448 break;
3449 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01003450 lnum = CURRENT_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003451 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02003452 case ADDR_NONE:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003453 case ADDR_TABS_RELATIVE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02003454 case ADDR_UNSIGNED:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003455 addr_error(addr_type);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01003456 cmd = NULL;
3457 goto error;
3458 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003459 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003460#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02003461 lnum = qf_get_cur_idx(eap);
3462#endif
3463 break;
3464 case ADDR_QUICKFIX_VALID:
3465#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003466 lnum = qf_get_cur_valid_idx(eap);
Bram Moolenaare906c502015-09-09 21:10:39 +02003467#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003468 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003469 }
3470 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003471
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003472 case '$': // '$' - last line
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003473 ++cmd;
3474 switch (addr_type)
3475 {
3476 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02003477 case ADDR_OTHER:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003478 lnum = curbuf->b_ml.ml_line_count;
3479 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003480 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01003481 lnum = LAST_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003482 break;
3483 case ADDR_ARGUMENTS:
3484 lnum = ARGCOUNT;
3485 break;
3486 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003487 buf = lastbuf;
3488 while (buf->b_ml.ml_mfp == NULL)
3489 {
3490 if (buf->b_prev == NULL)
3491 break;
3492 buf = buf->b_prev;
3493 }
3494 lnum = buf->b_fnum;
3495 break;
3496 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003497 lnum = lastbuf->b_fnum;
3498 break;
3499 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01003500 lnum = LAST_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003501 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02003502 case ADDR_NONE:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003503 case ADDR_TABS_RELATIVE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02003504 case ADDR_UNSIGNED:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003505 addr_error(addr_type);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01003506 cmd = NULL;
3507 goto error;
3508 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003509 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003510#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003511 lnum = qf_get_size(eap);
3512 if (lnum == 0)
3513 lnum = 1;
Bram Moolenaare906c502015-09-09 21:10:39 +02003514#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003515 break;
Bram Moolenaar25190db2019-05-04 15:05:28 +02003516 case ADDR_QUICKFIX_VALID:
3517#ifdef FEAT_QUICKFIX
3518 lnum = qf_get_valid_size(eap);
3519 if (lnum == 0)
3520 lnum = 1;
3521#endif
3522 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003523 }
3524 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003525
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003526 case '\'': // ''' - mark
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003527 if (*++cmd == NUL)
3528 {
3529 cmd = NULL;
3530 goto error;
3531 }
3532 if (addr_type != ADDR_LINES)
3533 {
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003534 addr_error(addr_type);
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01003535 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003536 goto error;
3537 }
3538 if (skip)
3539 ++cmd;
3540 else
3541 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003542 // Only accept a mark in another file when it is
3543 // used by itself: ":'M".
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003544 fp = getmark(*cmd, to_other_file && cmd[1] == NUL);
3545 ++cmd;
3546 if (fp == (pos_T *)-1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003547 // Jumped to another file.
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003548 lnum = curwin->w_cursor.lnum;
3549 else
3550 {
3551 if (check_mark(fp) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003552 {
3553 cmd = NULL;
3554 goto error;
3555 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003556 lnum = fp->lnum;
3557 }
3558 }
3559 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003560
3561 case '/':
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003562 case '?': // '/' or '?' - search
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003563 c = *cmd++;
3564 if (addr_type != ADDR_LINES)
3565 {
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003566 addr_error(addr_type);
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01003567 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003568 goto error;
3569 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003570 if (skip) // skip "/pat/"
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003571 {
3572 cmd = skip_regexp(cmd, c, (int)p_magic, NULL);
3573 if (*cmd == c)
3574 ++cmd;
3575 }
3576 else
3577 {
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02003578 int flags;
3579
3580 pos = curwin->w_cursor; // save curwin->w_cursor
3581
3582 // When '/' or '?' follows another address, start from
3583 // there.
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003584 if (lnum != MAXLNUM)
3585 curwin->w_cursor.lnum = lnum;
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02003586
3587 // Start a forward search at the end of the line (unless
3588 // before the first line).
3589 // Start a backward search at the start of the line.
3590 // This makes sure we never match in the current
3591 // line, and can match anywhere in the
3592 // next/previous line.
Bram Moolenaar8ada6aa2017-12-19 21:23:21 +01003593 if (c == '/' && curwin->w_cursor.lnum > 0)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003594 curwin->w_cursor.col = MAXCOL;
3595 else
3596 curwin->w_cursor.col = 0;
3597 searchcmdlen = 0;
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02003598 flags = silent ? 0 : SEARCH_HIS | SEARCH_MSG;
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02003599 if (!do_search(NULL, c, cmd, 1L, flags, NULL))
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003600 {
3601 curwin->w_cursor = pos;
3602 cmd = NULL;
3603 goto error;
3604 }
3605 lnum = curwin->w_cursor.lnum;
3606 curwin->w_cursor = pos;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003607 // adjust command string pointer
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003608 cmd += searchcmdlen;
3609 }
3610 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003611
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003612 case '\\': // "\?", "\/" or "\&", repeat search
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003613 ++cmd;
3614 if (addr_type != ADDR_LINES)
3615 {
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003616 addr_error(addr_type);
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01003617 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003618 goto error;
3619 }
3620 if (*cmd == '&')
3621 i = RE_SUBST;
3622 else if (*cmd == '?' || *cmd == '/')
3623 i = RE_SEARCH;
3624 else
3625 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003626 emsg(_(e_backslash));
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003627 cmd = NULL;
3628 goto error;
3629 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003630
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003631 if (!skip)
3632 {
3633 /*
3634 * When search follows another address, start from
3635 * there.
3636 */
3637 if (lnum != MAXLNUM)
3638 pos.lnum = lnum;
3639 else
3640 pos.lnum = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003641
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003642 /*
3643 * Start the search just like for the above
3644 * do_search().
3645 */
3646 if (*cmd != '?')
3647 pos.col = MAXCOL;
3648 else
3649 pos.col = 0;
Bram Moolenaarbd8539a2015-08-11 18:53:03 +02003650 pos.coladd = 0;
Bram Moolenaar5d24a222018-12-23 19:10:09 +01003651 if (searchit(curwin, curbuf, &pos, NULL,
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003652 *cmd == '?' ? BACKWARD : FORWARD,
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02003653 (char_u *)"", 1L, SEARCH_MSG, i, NULL) != FAIL)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003654 lnum = pos.lnum;
3655 else
3656 {
3657 cmd = NULL;
3658 goto error;
3659 }
3660 }
3661 ++cmd;
3662 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003663
3664 default:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003665 if (VIM_ISDIGIT(*cmd)) // absolute line number
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003666 lnum = getdigits(&cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003667 }
3668
3669 for (;;)
3670 {
3671 cmd = skipwhite(cmd);
3672 if (*cmd != '-' && *cmd != '+' && !VIM_ISDIGIT(*cmd))
3673 break;
3674
3675 if (lnum == MAXLNUM)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003676 {
3677 switch (addr_type)
3678 {
3679 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02003680 case ADDR_OTHER:
3681 // "+1" is same as ".+1"
Bram Moolenaarf240e182014-11-27 18:33:02 +01003682 lnum = curwin->w_cursor.lnum;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003683 break;
3684 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01003685 lnum = CURRENT_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003686 break;
3687 case ADDR_ARGUMENTS:
3688 lnum = curwin->w_arg_idx + 1;
3689 break;
3690 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003691 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003692 lnum = curbuf->b_fnum;
3693 break;
3694 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01003695 lnum = CURRENT_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003696 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01003697 case ADDR_TABS_RELATIVE:
3698 lnum = 1;
3699 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003700 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003701#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02003702 lnum = qf_get_cur_idx(eap);
3703#endif
3704 break;
3705 case ADDR_QUICKFIX_VALID:
3706#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003707 lnum = qf_get_cur_valid_idx(eap);
Bram Moolenaare906c502015-09-09 21:10:39 +02003708#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003709 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02003710 case ADDR_NONE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02003711 case ADDR_UNSIGNED:
3712 lnum = 0;
Bram Moolenaarb7316892019-05-01 18:08:42 +02003713 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003714 }
3715 }
3716
Bram Moolenaar071d4272004-06-13 20:20:40 +00003717 if (VIM_ISDIGIT(*cmd))
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003718 i = '+'; // "number" is same as "+number"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003719 else
3720 i = *cmd++;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003721 if (!VIM_ISDIGIT(*cmd)) // '+' is '+1', but '+0' is not '+1'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003722 n = 1;
3723 else
3724 n = getdigits(&cmd);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01003725
3726 if (addr_type == ADDR_TABS_RELATIVE)
3727 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003728 emsg(_(e_invrange));
Bram Moolenaar2f72c702017-01-29 14:48:10 +01003729 cmd = NULL;
3730 goto error;
3731 }
3732 else if (addr_type == ADDR_LOADED_BUFFERS
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003733 || addr_type == ADDR_BUFFERS)
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01003734 lnum = compute_buffer_local_count(
3735 addr_type, lnum, (i == '-') ? -1 * n : n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003736 else
Bram Moolenaarded27822017-01-02 14:27:34 +01003737 {
3738#ifdef FEAT_FOLDING
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003739 // Relative line addressing, need to adjust for folded lines
3740 // now, but only do it after the first address.
Bram Moolenaarded27822017-01-02 14:27:34 +01003741 if (addr_type == ADDR_LINES && (i == '-' || i == '+')
3742 && address_count >= 2)
3743 (void)hasFolding(lnum, NULL, &lnum);
3744#endif
3745 if (i == '-')
3746 lnum -= n;
3747 else
3748 lnum += n;
3749 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003750 }
3751 } while (*cmd == '/' || *cmd == '?');
3752
3753error:
3754 *ptr = cmd;
3755 return lnum;
3756}
3757
3758/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00003759 * Get flags from an Ex command argument.
3760 */
3761 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003762get_flags(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00003763{
3764 while (vim_strchr((char_u *)"lp#", *eap->arg) != NULL)
3765 {
3766 if (*eap->arg == 'l')
3767 eap->flags |= EXFLAG_LIST;
3768 else if (*eap->arg == 'p')
3769 eap->flags |= EXFLAG_PRINT;
3770 else
3771 eap->flags |= EXFLAG_NR;
3772 eap->arg = skipwhite(eap->arg + 1);
3773 }
3774}
3775
3776/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003777 * Function called for command which is Not Implemented. NI!
3778 */
3779 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003780ex_ni(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003781{
3782 if (!eap->skip)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003783 eap->errmsg = N_("E319: Sorry, the command is not available in this version");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003784}
3785
Bram Moolenaar7bb75552007-07-16 18:39:49 +00003786#ifdef HAVE_EX_SCRIPT_NI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003787/*
3788 * Function called for script command which is Not Implemented. NI!
3789 * Skips over ":perl <<EOF" constructs.
3790 */
3791 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003792ex_script_ni(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003793{
3794 if (!eap->skip)
3795 ex_ni(eap);
3796 else
3797 vim_free(script_get(eap, eap->arg));
3798}
3799#endif
3800
3801/*
3802 * Check range in Ex command for validity.
3803 * Return NULL when valid, error message when invalid.
3804 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003805 static char *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003806invalid_range(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003807{
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003808 buf_T *buf;
Bram Moolenaar25190db2019-05-04 15:05:28 +02003809
Bram Moolenaar071d4272004-06-13 20:20:40 +00003810 if ( eap->line1 < 0
3811 || eap->line2 < 0
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003812 || eap->line1 > eap->line2)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003813 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003814
Bram Moolenaar8071cb22019-07-12 17:58:01 +02003815 if (eap->argt & EX_RANGE)
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003816 {
Bram Moolenaarb7316892019-05-01 18:08:42 +02003817 switch (eap->addr_type)
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003818 {
3819 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02003820 if (eap->line2 > curbuf->b_ml.ml_line_count
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003821#ifdef FEAT_DIFF
3822 + (eap->cmdidx == CMD_diffget)
3823#endif
3824 )
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003825 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003826 break;
3827 case ADDR_ARGUMENTS:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003828 // add 1 if ARGCOUNT is 0
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01003829 if (eap->line2 > ARGCOUNT + (!ARGCOUNT))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003830 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003831 break;
3832 case ADDR_BUFFERS:
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02003833 // Only a boundary check, not whether the buffers actually
3834 // exist.
3835 if (eap->line1 < 1 || eap->line2 > get_highest_fnum())
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003836 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003837 break;
3838 case ADDR_LOADED_BUFFERS:
3839 buf = firstbuf;
3840 while (buf->b_ml.ml_mfp == NULL)
3841 {
3842 if (buf->b_next == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003843 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003844 buf = buf->b_next;
3845 }
3846 if (eap->line1 < buf->b_fnum)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003847 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003848 buf = lastbuf;
3849 while (buf->b_ml.ml_mfp == NULL)
3850 {
3851 if (buf->b_prev == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003852 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003853 buf = buf->b_prev;
3854 }
3855 if (eap->line2 > buf->b_fnum)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003856 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003857 break;
3858 case ADDR_WINDOWS:
Bram Moolenaar8be63882015-01-14 11:25:05 +01003859 if (eap->line2 > LAST_WIN_NR)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003860 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003861 break;
3862 case ADDR_TABS:
3863 if (eap->line2 > LAST_TAB_NR)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003864 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003865 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01003866 case ADDR_TABS_RELATIVE:
Bram Moolenaarb7316892019-05-01 18:08:42 +02003867 case ADDR_OTHER:
3868 // Any range is OK.
Bram Moolenaar2f72c702017-01-29 14:48:10 +01003869 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003870 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003871#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02003872 // No error for value that is too big, will use the last entry.
3873 if (eap->line2 <= 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003874 return _(e_invrange);
Bram Moolenaare906c502015-09-09 21:10:39 +02003875#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003876 break;
Bram Moolenaar25190db2019-05-04 15:05:28 +02003877 case ADDR_QUICKFIX_VALID:
3878#ifdef FEAT_QUICKFIX
3879 if ((eap->line2 != 1 && eap->line2 > qf_get_valid_size(eap))
3880 || eap->line2 < 0)
3881 return _(e_invrange);
3882#endif
3883 break;
3884 case ADDR_UNSIGNED:
3885 if (eap->line2 < 0)
3886 return _(e_invrange);
3887 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02003888 case ADDR_NONE:
3889 // Will give an error elsewhere.
3890 break;
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003891 }
3892 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003893 return NULL;
3894}
3895
3896/*
3897 * Correct the range for zero line number, if required.
3898 */
3899 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003900correct_range(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003901{
Bram Moolenaar8071cb22019-07-12 17:58:01 +02003902 if (!(eap->argt & EX_ZEROR)) // zero in range not allowed
Bram Moolenaar071d4272004-06-13 20:20:40 +00003903 {
3904 if (eap->line1 == 0)
3905 eap->line1 = 1;
3906 if (eap->line2 == 0)
3907 eap->line2 = 1;
3908 }
3909}
3910
Bram Moolenaar748bf032005-02-02 23:04:36 +00003911#ifdef FEAT_QUICKFIX
Bram Moolenaar748bf032005-02-02 23:04:36 +00003912/*
3913 * For a ":vimgrep" or ":vimgrepadd" command return a pointer past the
3914 * pattern. Otherwise return eap->arg.
3915 */
3916 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003917skip_grep_pat(exarg_T *eap)
Bram Moolenaar748bf032005-02-02 23:04:36 +00003918{
3919 char_u *p = eap->arg;
3920
Bram Moolenaara37420f2006-02-04 22:37:47 +00003921 if (*p != NUL && (eap->cmdidx == CMD_vimgrep || eap->cmdidx == CMD_lvimgrep
3922 || eap->cmdidx == CMD_vimgrepadd
3923 || eap->cmdidx == CMD_lvimgrepadd
3924 || grep_internal(eap->cmdidx)))
Bram Moolenaar748bf032005-02-02 23:04:36 +00003925 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00003926 p = skip_vimgrep_pat(p, NULL, NULL);
Bram Moolenaar748bf032005-02-02 23:04:36 +00003927 if (p == NULL)
3928 p = eap->arg;
Bram Moolenaar748bf032005-02-02 23:04:36 +00003929 }
3930 return p;
3931}
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003932
3933/*
3934 * For the ":make" and ":grep" commands insert the 'makeprg'/'grepprg' option
3935 * in the command line, so that things like % get expanded.
3936 */
3937 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003938replace_makeprg(exarg_T *eap, char_u *p, char_u **cmdlinep)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003939{
3940 char_u *new_cmdline;
3941 char_u *program;
3942 char_u *pos;
3943 char_u *ptr;
3944 int len;
3945 int i;
3946
3947 /*
3948 * Don't do it when ":vimgrep" is used for ":grep".
3949 */
Bram Moolenaara37420f2006-02-04 22:37:47 +00003950 if ((eap->cmdidx == CMD_make || eap->cmdidx == CMD_lmake
3951 || eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
3952 || eap->cmdidx == CMD_grepadd
3953 || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003954 && !grep_internal(eap->cmdidx))
3955 {
Bram Moolenaara37420f2006-02-04 22:37:47 +00003956 if (eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
3957 || eap->cmdidx == CMD_grepadd || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003958 {
3959 if (*curbuf->b_p_gp == NUL)
3960 program = p_gp;
3961 else
3962 program = curbuf->b_p_gp;
3963 }
3964 else
3965 {
3966 if (*curbuf->b_p_mp == NUL)
3967 program = p_mp;
3968 else
3969 program = curbuf->b_p_mp;
3970 }
3971
3972 p = skipwhite(p);
3973
3974 if ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
3975 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003976 // replace $* by given arguments
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003977 i = 1;
3978 while ((pos = (char_u *)strstr((char *)pos + 2, "$*")) != NULL)
3979 ++i;
3980 len = (int)STRLEN(p);
Bram Moolenaar51e14382019-05-25 20:21:28 +02003981 new_cmdline = alloc(STRLEN(program) + i * (len - 2) + 1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003982 if (new_cmdline == NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003983 return NULL; // out of memory
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003984 ptr = new_cmdline;
3985 while ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
3986 {
3987 i = (int)(pos - program);
3988 STRNCPY(ptr, program, i);
3989 STRCPY(ptr += i, p);
3990 ptr += len;
3991 program = pos + 2;
3992 }
3993 STRCPY(ptr, program);
3994 }
3995 else
3996 {
Bram Moolenaar51e14382019-05-25 20:21:28 +02003997 new_cmdline = alloc(STRLEN(program) + STRLEN(p) + 2);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003998 if (new_cmdline == NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003999 return NULL; // out of memory
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004000 STRCPY(new_cmdline, program);
4001 STRCAT(new_cmdline, " ");
4002 STRCAT(new_cmdline, p);
4003 }
4004 msg_make(p);
4005
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004006 // 'eap->cmd' is not set here, because it is not used at CMD_make
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004007 vim_free(*cmdlinep);
4008 *cmdlinep = new_cmdline;
4009 p = new_cmdline;
4010 }
4011 return p;
4012}
Bram Moolenaar748bf032005-02-02 23:04:36 +00004013#endif
4014
Bram Moolenaar071d4272004-06-13 20:20:40 +00004015/*
4016 * Expand file name in Ex command argument.
Bram Moolenaar0abb4272019-06-15 16:06:00 +02004017 * When an error is detected, "errormsgp" is set to a non-NULL pointer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004018 * Return FAIL for failure, OK otherwise.
4019 */
4020 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004021expand_filename(
4022 exarg_T *eap,
4023 char_u **cmdlinep,
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004024 char **errormsgp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004025{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004026 int has_wildcards; // need to expand wildcards
Bram Moolenaar071d4272004-06-13 20:20:40 +00004027 char_u *repl;
4028 int srclen;
4029 char_u *p;
4030 int n;
Bram Moolenaar63b92542007-03-27 14:57:09 +00004031 int escaped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004032
Bram Moolenaar748bf032005-02-02 23:04:36 +00004033#ifdef FEAT_QUICKFIX
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004034 // Skip a regexp pattern for ":vimgrep[add] pat file..."
Bram Moolenaar748bf032005-02-02 23:04:36 +00004035 p = skip_grep_pat(eap);
4036#else
4037 p = eap->arg;
4038#endif
4039
Bram Moolenaar071d4272004-06-13 20:20:40 +00004040 /*
4041 * Decide to expand wildcards *before* replacing '%', '#', etc. If
4042 * the file name contains a wildcard it should not cause expanding.
4043 * (it will be expanded anyway if there is a wildcard before replacing).
4044 */
Bram Moolenaar748bf032005-02-02 23:04:36 +00004045 has_wildcards = mch_has_wildcard(p);
4046 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004047 {
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004048#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004049 // Skip over `=expr`, wildcards in it are not expanded.
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004050 if (p[0] == '`' && p[1] == '=')
4051 {
4052 p += 2;
4053 (void)skip_expr(&p);
4054 if (*p == '`')
4055 ++p;
4056 continue;
4057 }
4058#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004059 /*
4060 * Quick check if this cannot be the start of a special string.
4061 * Also removes backslash before '%', '#' and '<'.
4062 */
4063 if (vim_strchr((char_u *)"%#<", *p) == NULL)
4064 {
4065 ++p;
4066 continue;
4067 }
4068
4069 /*
4070 * Try to find a match at this position.
4071 */
Bram Moolenaar63b92542007-03-27 14:57:09 +00004072 repl = eval_vars(p, eap->arg, &srclen, &(eap->do_ecmd_lnum),
4073 errormsgp, &escaped);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004074 if (*errormsgp != NULL) // error detected
Bram Moolenaar071d4272004-06-13 20:20:40 +00004075 return FAIL;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004076 if (repl == NULL) // no match found
Bram Moolenaar071d4272004-06-13 20:20:40 +00004077 {
4078 p += srclen;
4079 continue;
4080 }
4081
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004082 // Wildcards won't be expanded below, the replacement is taken
4083 // literally. But do expand "~/file", "~user/file" and "$HOME/file".
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00004084 if (vim_strchr(repl, '$') != NULL || vim_strchr(repl, '~') != NULL)
4085 {
4086 char_u *l = repl;
4087
4088 repl = expand_env_save(repl);
4089 vim_free(l);
4090 }
4091
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004092 // Need to escape white space et al. with a backslash.
4093 // Don't do this for:
4094 // - replacement that already has been escaped: "##"
4095 // - shell commands (may have to use quotes instead).
4096 // - non-unix systems when there is a single argument (spaces don't
4097 // separate arguments then).
Bram Moolenaar071d4272004-06-13 20:20:40 +00004098 if (!eap->usefilter
Bram Moolenaar63b92542007-03-27 14:57:09 +00004099 && !escaped
Bram Moolenaar071d4272004-06-13 20:20:40 +00004100 && eap->cmdidx != CMD_bang
Bram Moolenaar071d4272004-06-13 20:20:40 +00004101 && eap->cmdidx != CMD_grep
4102 && eap->cmdidx != CMD_grepadd
Bram Moolenaarbf15b8d2017-06-04 20:43:48 +02004103 && eap->cmdidx != CMD_hardcopy
Bram Moolenaar67883b42017-07-27 22:57:00 +02004104 && eap->cmdidx != CMD_lgrep
4105 && eap->cmdidx != CMD_lgrepadd
4106 && eap->cmdidx != CMD_lmake
4107 && eap->cmdidx != CMD_make
4108 && eap->cmdidx != CMD_terminal
Bram Moolenaar071d4272004-06-13 20:20:40 +00004109#ifndef UNIX
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004110 && !(eap->argt & EX_NOSPC)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004111#endif
4112 )
4113 {
4114 char_u *l;
4115#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004116 // Don't escape a backslash here, because rem_backslash() doesn't
4117 // remove it later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004118 static char_u *nobslash = (char_u *)" \t\"|";
4119# define ESCAPE_CHARS nobslash
4120#else
4121# define ESCAPE_CHARS escape_chars
4122#endif
4123
4124 for (l = repl; *l; ++l)
4125 if (vim_strchr(ESCAPE_CHARS, *l) != NULL)
4126 {
4127 l = vim_strsave_escaped(repl, ESCAPE_CHARS);
4128 if (l != NULL)
4129 {
4130 vim_free(repl);
4131 repl = l;
4132 }
4133 break;
4134 }
4135 }
4136
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004137 // For a shell command a '!' must be escaped.
Bram Moolenaar67883b42017-07-27 22:57:00 +02004138 if ((eap->usefilter || eap->cmdidx == CMD_bang
4139 || eap->cmdidx == CMD_terminal)
Bram Moolenaar31b7d382014-04-01 18:54:48 +02004140 && vim_strpbrk(repl, (char_u *)"!") != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004141 {
4142 char_u *l;
4143
Bram Moolenaar31b7d382014-04-01 18:54:48 +02004144 l = vim_strsave_escaped(repl, (char_u *)"!");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004145 if (l != NULL)
4146 {
4147 vim_free(repl);
4148 repl = l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004149 }
4150 }
4151
4152 p = repl_cmdline(eap, p, srclen, repl, cmdlinep);
4153 vim_free(repl);
4154 if (p == NULL)
4155 return FAIL;
4156 }
4157
4158 /*
4159 * One file argument: Expand wildcards.
4160 * Don't do this with ":r !command" or ":w !command".
4161 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004162 if ((eap->argt & EX_NOSPC) && !eap->usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004163 {
4164 /*
4165 * May do this twice:
4166 * 1. Replace environment variables.
4167 * 2. Replace any other wildcards, remove backslashes.
4168 */
4169 for (n = 1; n <= 2; ++n)
4170 {
4171 if (n == 2)
4172 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004173 /*
4174 * Halve the number of backslashes (this is Vi compatible).
4175 * For Unix and OS/2, when wildcards are expanded, this is
4176 * done by ExpandOne() below.
4177 */
Bram Moolenaare7fedb62015-12-31 19:07:19 +01004178#if defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004179 if (!has_wildcards)
4180#endif
4181 backslash_halve(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004182 }
4183
4184 if (has_wildcards)
4185 {
4186 if (n == 1)
4187 {
4188 /*
4189 * First loop: May expand environment variables. This
4190 * can be done much faster with expand_env() than with
4191 * something else (e.g., calling a shell).
4192 * After expanding environment variables, check again
4193 * if there are still wildcards present.
4194 */
4195 if (vim_strchr(eap->arg, '$') != NULL
4196 || vim_strchr(eap->arg, '~') != NULL)
4197 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +00004198 expand_env_esc(eap->arg, NameBuff, MAXPATHL,
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00004199 TRUE, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004200 has_wildcards = mch_has_wildcard(NameBuff);
4201 p = NameBuff;
4202 }
4203 else
4204 p = NULL;
4205 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004206 else // n == 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00004207 {
4208 expand_T xpc;
Bram Moolenaarb40c2572019-10-19 21:01:05 +02004209 int options = WILD_LIST_NOTFOUND
4210 | WILD_NOERROR | WILD_ADD_SLASH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004211
4212 ExpandInit(&xpc);
4213 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar94950a92010-12-02 16:01:29 +01004214 if (p_wic)
4215 options += WILD_ICASE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004216 p = ExpandOne(&xpc, eap->arg, NULL,
Bram Moolenaar94950a92010-12-02 16:01:29 +01004217 options, WILD_EXPAND_FREE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004218 if (p == NULL)
4219 return FAIL;
4220 }
4221 if (p != NULL)
4222 {
4223 (void)repl_cmdline(eap, eap->arg, (int)STRLEN(eap->arg),
4224 p, cmdlinep);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004225 if (n == 2) // p came from ExpandOne()
Bram Moolenaar071d4272004-06-13 20:20:40 +00004226 vim_free(p);
4227 }
4228 }
4229 }
4230 }
4231 return OK;
4232}
4233
4234/*
4235 * Replace part of the command line, keeping eap->cmd, eap->arg and
4236 * eap->nextcmd correct.
4237 * "src" points to the part that is to be replaced, of length "srclen".
4238 * "repl" is the replacement string.
4239 * Returns a pointer to the character after the replaced string.
4240 * Returns NULL for failure.
4241 */
4242 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004243repl_cmdline(
4244 exarg_T *eap,
4245 char_u *src,
4246 int srclen,
4247 char_u *repl,
4248 char_u **cmdlinep)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004249{
4250 int len;
4251 int i;
4252 char_u *new_cmdline;
4253
4254 /*
4255 * The new command line is build in new_cmdline[].
4256 * First allocate it.
4257 * Careful: a "+cmd" argument may have been NUL terminated.
4258 */
4259 len = (int)STRLEN(repl);
4260 i = (int)(src - *cmdlinep) + (int)STRLEN(src + srclen) + len + 3;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004261 if (eap->nextcmd != NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004262 i += (int)STRLEN(eap->nextcmd);// add space for next command
Bram Moolenaar964b3742019-05-24 18:54:09 +02004263 if ((new_cmdline = alloc(i)) == NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004264 return NULL; // out of memory!
Bram Moolenaar071d4272004-06-13 20:20:40 +00004265
4266 /*
4267 * Copy the stuff before the expanded part.
4268 * Copy the expanded stuff.
4269 * Copy what came after the expanded part.
4270 * Copy the next commands, if there are any.
4271 */
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004272 i = (int)(src - *cmdlinep); // length of part before match
Bram Moolenaar071d4272004-06-13 20:20:40 +00004273 mch_memmove(new_cmdline, *cmdlinep, (size_t)i);
Bram Moolenaara3ffd9c2005-07-21 21:03:15 +00004274
Bram Moolenaar071d4272004-06-13 20:20:40 +00004275 mch_memmove(new_cmdline + i, repl, (size_t)len);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004276 i += len; // remember the end of the string
Bram Moolenaar071d4272004-06-13 20:20:40 +00004277 STRCPY(new_cmdline + i, src + srclen);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004278 src = new_cmdline + i; // remember where to continue
Bram Moolenaar071d4272004-06-13 20:20:40 +00004279
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004280 if (eap->nextcmd != NULL) // append next command
Bram Moolenaar071d4272004-06-13 20:20:40 +00004281 {
4282 i = (int)STRLEN(new_cmdline) + 1;
4283 STRCPY(new_cmdline + i, eap->nextcmd);
4284 eap->nextcmd = new_cmdline + i;
4285 }
4286 eap->cmd = new_cmdline + (eap->cmd - *cmdlinep);
4287 eap->arg = new_cmdline + (eap->arg - *cmdlinep);
4288 if (eap->do_ecmd_cmd != NULL && eap->do_ecmd_cmd != dollar_command)
4289 eap->do_ecmd_cmd = new_cmdline + (eap->do_ecmd_cmd - *cmdlinep);
4290 vim_free(*cmdlinep);
4291 *cmdlinep = new_cmdline;
4292
4293 return src;
4294}
4295
4296/*
4297 * Check for '|' to separate commands and '"' to start comments.
4298 */
4299 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004300separate_nextcmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004301{
4302 char_u *p;
4303
Bram Moolenaar86b68352004-12-27 21:59:20 +00004304#ifdef FEAT_QUICKFIX
Bram Moolenaar748bf032005-02-02 23:04:36 +00004305 p = skip_grep_pat(eap);
4306#else
4307 p = eap->arg;
Bram Moolenaar86b68352004-12-27 21:59:20 +00004308#endif
4309
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004310 for ( ; *p; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004311 {
4312 if (*p == Ctrl_V)
4313 {
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004314 if (eap->argt & (EX_CTRLV | EX_XFILE))
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004315 ++p; // skip CTRL-V and next char
Bram Moolenaar071d4272004-06-13 20:20:40 +00004316 else
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004317 // remove CTRL-V and skip next char
Bram Moolenaara7241f52008-06-24 20:39:31 +00004318 STRMOVE(p, p + 1);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004319 if (*p == NUL) // stop at NUL after CTRL-V
Bram Moolenaar071d4272004-06-13 20:20:40 +00004320 break;
4321 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004322
4323#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004324 // Skip over `=expr` when wildcards are expanded.
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004325 else if (p[0] == '`' && p[1] == '=' && (eap->argt & EX_XFILE))
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004326 {
4327 p += 2;
4328 (void)skip_expr(&p);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004329 }
4330#endif
4331
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004332 // Check for '"': start of comment or '|': next command
4333 // :@" and :*" do not start a comment!
4334 // :redir @" doesn't either.
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004335 else if ((*p == '"' && !(eap->argt & EX_NOTRLCOM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004336 && ((eap->cmdidx != CMD_at && eap->cmdidx != CMD_star)
4337 || p != eap->arg)
4338 && (eap->cmdidx != CMD_redir
4339 || p != eap->arg + 1 || p[-1] != '@'))
4340 || *p == '|' || *p == '\n')
4341 {
4342 /*
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004343 * We remove the '\' before the '|', unless EX_CTRLV is used
Bram Moolenaar071d4272004-06-13 20:20:40 +00004344 * AND 'b' is present in 'cpoptions'.
4345 */
4346 if ((vim_strchr(p_cpo, CPO_BAR) == NULL
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004347 || !(eap->argt & EX_CTRLV)) && *(p - 1) == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004348 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004349 STRMOVE(p - 1, p); // remove the '\'
Bram Moolenaar071d4272004-06-13 20:20:40 +00004350 --p;
4351 }
4352 else
4353 {
4354 eap->nextcmd = check_nextcmd(p);
4355 *p = NUL;
4356 break;
4357 }
4358 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004359 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00004360
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004361 if (!(eap->argt & EX_NOTRLCOM)) // remove trailing spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00004362 del_trailing_spaces(eap->arg);
4363}
4364
4365/*
4366 * get + command from ex argument
4367 */
4368 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004369getargcmd(char_u **argp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004370{
4371 char_u *arg = *argp;
4372 char_u *command = NULL;
4373
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004374 if (*arg == '+') // +[command]
Bram Moolenaar071d4272004-06-13 20:20:40 +00004375 {
4376 ++arg;
Bram Moolenaar3e451592014-04-02 14:22:05 +02004377 if (vim_isspace(*arg) || *arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004378 command = dollar_command;
4379 else
4380 {
4381 command = arg;
4382 arg = skip_cmd_arg(command, TRUE);
4383 if (*arg != NUL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004384 *arg++ = NUL; // terminate command with NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004385 }
4386
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004387 arg = skipwhite(arg); // skip over spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00004388 *argp = arg;
4389 }
4390 return command;
4391}
4392
4393/*
4394 * Find end of "+command" argument. Skip over "\ " and "\\".
4395 */
Bram Moolenaard0190392019-08-23 21:17:35 +02004396 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004397skip_cmd_arg(
4398 char_u *p,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004399 int rembs) // TRUE to halve the number of backslashes
Bram Moolenaar071d4272004-06-13 20:20:40 +00004400{
4401 while (*p && !vim_isspace(*p))
4402 {
4403 if (*p == '\\' && p[1] != NUL)
4404 {
4405 if (rembs)
Bram Moolenaara7241f52008-06-24 20:39:31 +00004406 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004407 else
4408 ++p;
4409 }
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004410 MB_PTR_ADV(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004411 }
4412 return p;
4413}
4414
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004415 int
4416get_bad_opt(char_u *p, exarg_T *eap)
4417{
4418 if (STRICMP(p, "keep") == 0)
4419 eap->bad_char = BAD_KEEP;
4420 else if (STRICMP(p, "drop") == 0)
4421 eap->bad_char = BAD_DROP;
4422 else if (MB_BYTE2LEN(*p) == 1 && p[1] == NUL)
4423 eap->bad_char = *p;
Bram Moolenaar75808492018-06-12 12:39:41 +02004424 else
4425 return FAIL;
4426 return OK;
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004427}
4428
Bram Moolenaar071d4272004-06-13 20:20:40 +00004429/*
4430 * Get "++opt=arg" argument.
4431 * Return FAIL or OK.
4432 */
4433 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004434getargopt(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004435{
4436 char_u *arg = eap->arg + 2;
4437 int *pp = NULL;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02004438 int bad_char_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004439 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004440
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004441 // ":edit ++[no]bin[ary] file"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004442 if (STRNCMP(arg, "bin", 3) == 0 || STRNCMP(arg, "nobin", 5) == 0)
4443 {
4444 if (*arg == 'n')
4445 {
4446 arg += 2;
4447 eap->force_bin = FORCE_NOBIN;
4448 }
4449 else
4450 eap->force_bin = FORCE_BIN;
4451 if (!checkforcmd(&arg, "binary", 3))
4452 return FAIL;
4453 eap->arg = skipwhite(arg);
4454 return OK;
4455 }
4456
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004457 // ":read ++edit file"
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004458 if (STRNCMP(arg, "edit", 4) == 0)
4459 {
4460 eap->read_edit = TRUE;
4461 eap->arg = skipwhite(arg + 4);
4462 return OK;
4463 }
4464
Bram Moolenaar071d4272004-06-13 20:20:40 +00004465 if (STRNCMP(arg, "ff", 2) == 0)
4466 {
4467 arg += 2;
4468 pp = &eap->force_ff;
4469 }
4470 else if (STRNCMP(arg, "fileformat", 10) == 0)
4471 {
4472 arg += 10;
4473 pp = &eap->force_ff;
4474 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004475 else if (STRNCMP(arg, "enc", 3) == 0)
4476 {
Bram Moolenaarb38e9ab2011-12-14 14:49:45 +01004477 if (STRNCMP(arg, "encoding", 8) == 0)
4478 arg += 8;
4479 else
4480 arg += 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004481 pp = &eap->force_enc;
4482 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004483 else if (STRNCMP(arg, "bad", 3) == 0)
4484 {
4485 arg += 3;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02004486 pp = &bad_char_idx;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004487 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004488
4489 if (pp == NULL || *arg != '=')
4490 return FAIL;
4491
4492 ++arg;
4493 *pp = (int)(arg - eap->cmd);
4494 arg = skip_cmd_arg(arg, FALSE);
4495 eap->arg = skipwhite(arg);
4496 *arg = NUL;
4497
Bram Moolenaar071d4272004-06-13 20:20:40 +00004498 if (pp == &eap->force_ff)
4499 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004500 if (check_ff_value(eap->cmd + eap->force_ff) == FAIL)
4501 return FAIL;
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004502 eap->force_ff = eap->cmd[eap->force_ff];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004503 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004504 else if (pp == &eap->force_enc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004505 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004506 // Make 'fileencoding' lower case.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004507 for (p = eap->cmd + eap->force_enc; *p != NUL; ++p)
4508 *p = TOLOWER_ASC(*p);
4509 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004510 else
4511 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004512 // Check ++bad= argument. Must be a single-byte character, "keep" or
4513 // "drop".
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004514 if (get_bad_opt(eap->cmd + bad_char_idx, eap) == FAIL)
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004515 return FAIL;
4516 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004517
4518 return OK;
4519}
4520
Bram Moolenaar071d4272004-06-13 20:20:40 +00004521 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004522ex_autocmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004523{
4524 /*
Bram Moolenaar26d4b892018-07-04 22:26:28 +02004525 * Disallow autocommands from .exrc and .vimrc in current
Bram Moolenaar071d4272004-06-13 20:20:40 +00004526 * directory for security reasons.
4527 */
4528 if (secure)
4529 {
4530 secure = 2;
4531 eap->errmsg = e_curdir;
4532 }
4533 else if (eap->cmdidx == CMD_autocmd)
4534 do_autocmd(eap->arg, eap->forceit);
4535 else
4536 do_augroup(eap->arg, eap->forceit);
4537}
4538
4539/*
4540 * ":doautocmd": Apply the automatic commands to the current buffer.
4541 */
4542 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004543ex_doautocmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004544{
Bram Moolenaar60542ac2012-02-12 20:14:01 +01004545 char_u *arg = eap->arg;
4546 int call_do_modelines = check_nomodeline(&arg);
Bram Moolenaar1610d052016-06-09 22:53:01 +02004547 int did_aucmd;
Bram Moolenaar60542ac2012-02-12 20:14:01 +01004548
Bram Moolenaar1610d052016-06-09 22:53:01 +02004549 (void)do_doautocmd(arg, TRUE, &did_aucmd);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004550 // Only when there is no <nomodeline>.
Bram Moolenaar1610d052016-06-09 22:53:01 +02004551 if (call_do_modelines && did_aucmd)
Bram Moolenaar60542ac2012-02-12 20:14:01 +01004552 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004553}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004554
Bram Moolenaar071d4272004-06-13 20:20:40 +00004555/*
4556 * :[N]bunload[!] [N] [bufname] unload buffer
4557 * :[N]bdelete[!] [N] [bufname] delete buffer from buffer list
4558 * :[N]bwipeout[!] [N] [bufname] delete buffer really
4559 */
4560 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004561ex_bunload(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004562{
Bram Moolenaar8cdbd5b2019-06-16 15:50:45 +02004563 if (ERROR_IF_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02004564 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004565 eap->errmsg = do_bufdel(
4566 eap->cmdidx == CMD_bdelete ? DOBUF_DEL
4567 : eap->cmdidx == CMD_bwipeout ? DOBUF_WIPE
4568 : DOBUF_UNLOAD, eap->arg,
4569 eap->addr_count, (int)eap->line1, (int)eap->line2, eap->forceit);
4570}
4571
4572/*
4573 * :[N]buffer [N] to buffer N
4574 * :[N]sbuffer [N] to buffer N
4575 */
4576 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004577ex_buffer(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004578{
Bram Moolenaar8cdbd5b2019-06-16 15:50:45 +02004579 if (ERROR_IF_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02004580 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004581 if (*eap->arg)
4582 eap->errmsg = e_trailing;
4583 else
4584 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004585 if (eap->addr_count == 0) // default is current buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00004586 goto_buffer(eap, DOBUF_CURRENT, FORWARD, 0);
4587 else
4588 goto_buffer(eap, DOBUF_FIRST, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02004589 if (eap->do_ecmd_cmd != NULL)
4590 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004591 }
4592}
4593
4594/*
4595 * :[N]bmodified [N] to next mod. buffer
4596 * :[N]sbmodified [N] to next mod. buffer
4597 */
4598 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004599ex_bmodified(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004600{
4601 goto_buffer(eap, DOBUF_MOD, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02004602 if (eap->do_ecmd_cmd != NULL)
4603 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004604}
4605
4606/*
4607 * :[N]bnext [N] to next buffer
4608 * :[N]sbnext [N] split and to next buffer
4609 */
4610 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004611ex_bnext(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004612{
4613 goto_buffer(eap, DOBUF_CURRENT, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02004614 if (eap->do_ecmd_cmd != NULL)
4615 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004616}
4617
4618/*
4619 * :[N]bNext [N] to previous buffer
4620 * :[N]bprevious [N] to previous buffer
4621 * :[N]sbNext [N] split and to previous buffer
4622 * :[N]sbprevious [N] split and to previous buffer
4623 */
4624 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004625ex_bprevious(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004626{
4627 goto_buffer(eap, DOBUF_CURRENT, BACKWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02004628 if (eap->do_ecmd_cmd != NULL)
4629 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004630}
4631
4632/*
4633 * :brewind to first buffer
4634 * :bfirst to first buffer
4635 * :sbrewind split and to first buffer
4636 * :sbfirst split and to first buffer
4637 */
4638 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004639ex_brewind(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004640{
4641 goto_buffer(eap, DOBUF_FIRST, FORWARD, 0);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02004642 if (eap->do_ecmd_cmd != NULL)
4643 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004644}
4645
4646/*
4647 * :blast to last buffer
4648 * :sblast split and to last buffer
4649 */
4650 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004651ex_blast(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004652{
4653 goto_buffer(eap, DOBUF_LAST, BACKWARD, 0);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02004654 if (eap->do_ecmd_cmd != NULL)
4655 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004656}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004657
4658 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004659ends_excmd(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004660{
4661 return (c == NUL || c == '|' || c == '"' || c == '\n');
4662}
4663
4664#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA) || defined(FEAT_EVAL) \
4665 || defined(PROTO)
4666/*
4667 * Return the next command, after the first '|' or '\n'.
4668 * Return NULL if not found.
4669 */
4670 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004671find_nextcmd(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004672{
4673 while (*p != '|' && *p != '\n')
4674 {
4675 if (*p == NUL)
4676 return NULL;
4677 ++p;
4678 }
4679 return (p + 1);
4680}
4681#endif
4682
4683/*
Bram Moolenaar2256c992016-11-15 21:17:07 +01004684 * Check if *p is a separator between Ex commands, skipping over white space.
4685 * Return NULL if it isn't, the following character if it is.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004686 */
4687 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004688check_nextcmd(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004689{
Bram Moolenaar2256c992016-11-15 21:17:07 +01004690 char_u *s = skipwhite(p);
4691
4692 if (*s == '|' || *s == '\n')
4693 return (s + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004694 else
4695 return NULL;
4696}
4697
4698/*
4699 * - if there are more files to edit
4700 * - and this is the last window
4701 * - and forceit not used
4702 * - and not repeated twice on a row
4703 * return FAIL and give error message if 'message' TRUE
4704 * return OK otherwise
4705 */
4706 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004707check_more(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004708 int message, // when FALSE check only, no messages
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004709 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004710{
4711 int n = ARGCOUNT - curwin->w_arg_idx - 1;
4712
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00004713 if (!forceit && only_one_window()
4714 && ARGCOUNT > 1 && !arg_had_last && n >= 0 && quitmore == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004715 {
4716 if (message)
4717 {
4718#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
4719 if ((p_confirm || cmdmod.confirm) && curbuf->b_fname != NULL)
4720 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02004721 char_u buff[DIALOG_MSG_SIZE];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004722
Bram Moolenaarda6e8912018-08-21 15:12:14 +02004723 vim_snprintf((char *)buff, DIALOG_MSG_SIZE,
4724 NGETTEXT("%d more file to edit. Quit anyway?",
4725 "%d more files to edit. Quit anyway?", n), n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004726 if (vim_dialog_yesno(VIM_QUESTION, NULL, buff, 1) == VIM_YES)
4727 return OK;
4728 return FAIL;
4729 }
4730#endif
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01004731 semsg(NGETTEXT("E173: %d more file to edit",
4732 "E173: %d more files to edit", n), n);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004733 quitmore = 2; // next try to quit is allowed
Bram Moolenaar071d4272004-06-13 20:20:40 +00004734 }
4735 return FAIL;
4736 }
4737 return OK;
4738}
4739
Bram Moolenaar071d4272004-06-13 20:20:40 +00004740/*
4741 * Function given to ExpandGeneric() to obtain the list of command names.
4742 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004743 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004744get_command_name(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004745{
4746 if (idx >= (int)CMD_SIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004747 return get_user_command_name(idx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004748 return cmdnames[idx].cmd_name;
4749}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004750
Bram Moolenaar071d4272004-06-13 20:20:40 +00004751 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004752ex_colorscheme(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004753{
Bram Moolenaare6850792010-05-14 15:28:44 +02004754 if (*eap->arg == NUL)
4755 {
4756#ifdef FEAT_EVAL
4757 char_u *expr = vim_strsave((char_u *)"g:colors_name");
4758 char_u *p = NULL;
4759
4760 if (expr != NULL)
4761 {
4762 ++emsg_off;
4763 p = eval_to_string(expr, NULL, FALSE);
4764 --emsg_off;
4765 vim_free(expr);
4766 }
4767 if (p != NULL)
4768 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01004769 msg((char *)p);
Bram Moolenaare6850792010-05-14 15:28:44 +02004770 vim_free(p);
4771 }
4772 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01004773 msg("default");
Bram Moolenaare6850792010-05-14 15:28:44 +02004774#else
Bram Moolenaar32526b32019-01-19 17:43:09 +01004775 msg(_("unknown"));
Bram Moolenaare6850792010-05-14 15:28:44 +02004776#endif
4777 }
4778 else if (load_colors(eap->arg) == FAIL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004779 semsg(_("E185: Cannot find color scheme '%s'"), eap->arg);
Bram Moolenaarf58d81a2019-01-28 20:19:05 +01004780
4781#ifdef FEAT_VTP
4782 else if (has_vtp_working())
4783 {
4784 // background color change requires clear + redraw
4785 update_screen(CLEAR);
4786 redrawcmd();
4787 }
4788#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004789}
4790
4791 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004792ex_highlight(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004793{
4794 if (*eap->arg == NUL && eap->cmd[2] == '!')
Bram Moolenaar32526b32019-01-19 17:43:09 +01004795 msg(_("Greetings, Vim user!"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004796 do_highlight(eap->arg, eap->forceit, FALSE);
4797}
4798
4799
4800/*
4801 * Call this function if we thought we were going to exit, but we won't
4802 * (because of an error). May need to restore the terminal mode.
4803 */
4804 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004805not_exiting(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004806{
4807 exiting = FALSE;
4808 settmode(TMODE_RAW);
4809}
4810
Bram Moolenaar12a96de2018-03-11 14:44:18 +01004811 static int
4812before_quit_autocmds(win_T *wp, int quit_all, int forceit)
4813{
4814 apply_autocmds(EVENT_QUITPRE, NULL, NULL, FALSE, wp->w_buffer);
4815
Bram Moolenaar34ba06b2019-10-20 22:27:10 +02004816 // Bail out when autocommands closed the window.
4817 // Refuse to quit when the buffer in the last window is being closed (can
4818 // only happen in autocommands).
Bram Moolenaar12a96de2018-03-11 14:44:18 +01004819 if (!win_valid(wp)
4820 || curbuf_locked()
4821 || (wp->w_buffer->b_nwindows == 1 && wp->w_buffer->b_locked > 0))
4822 return TRUE;
4823
4824 if (quit_all || (check_more(FALSE, forceit) == OK && only_one_window()))
4825 {
4826 apply_autocmds(EVENT_EXITPRE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar34ba06b2019-10-20 22:27:10 +02004827 // Refuse to quit when locked or when the window was closed or the
4828 // buffer in the last window is being closed (can only happen in
4829 // autocommands).
4830 if (!win_valid(wp) || curbuf_locked()
Bram Moolenaar12a96de2018-03-11 14:44:18 +01004831 || (curbuf->b_nwindows == 1 && curbuf->b_locked > 0))
4832 return TRUE;
4833 }
4834
4835 return FALSE;
4836}
4837
Bram Moolenaar071d4272004-06-13 20:20:40 +00004838/*
Bram Moolenaarf240e182014-11-27 18:33:02 +01004839 * ":quit": quit current window, quit Vim if the last window is closed.
Bram Moolenaar12a96de2018-03-11 14:44:18 +01004840 * ":{nr}quit": quit window {nr}
Bram Moolenaar4d14bac2019-10-20 21:15:15 +02004841 * Also used when closing a terminal window that's the last one.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004842 */
Bram Moolenaar4d14bac2019-10-20 21:15:15 +02004843 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004844ex_quit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004845{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004846 win_T *wp;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004847
Bram Moolenaar071d4272004-06-13 20:20:40 +00004848#ifdef FEAT_CMDWIN
4849 if (cmdwin_type != 0)
4850 {
4851 cmdwin_result = Ctrl_C;
4852 return;
4853 }
4854#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004855 // Don't quit while editing the command line.
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00004856 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00004857 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00004858 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00004859 return;
4860 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004861 if (eap->addr_count > 0)
4862 {
Bram Moolenaarf240e182014-11-27 18:33:02 +01004863 int wnr = eap->line2;
4864
4865 for (wp = firstwin; wp->w_next != NULL; wp = wp->w_next)
4866 if (--wnr <= 0)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004867 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004868 }
4869 else
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004870 wp = curwin;
Bram Moolenaarf240e182014-11-27 18:33:02 +01004871
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004872 // Refuse to quit when locked.
Bram Moolenaar87ffb5c2017-10-19 12:37:42 +02004873 if (curbuf_locked())
4874 return;
Bram Moolenaar12a96de2018-03-11 14:44:18 +01004875
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004876 // Trigger QuitPre and maybe ExitPre
Bram Moolenaar12a96de2018-03-11 14:44:18 +01004877 if (before_quit_autocmds(wp, FALSE, eap->forceit))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004878 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004879
4880#ifdef FEAT_NETBEANS_INTG
4881 netbeansForcedQuit = eap->forceit;
4882#endif
4883
4884 /*
4885 * If there are more files or windows we won't exit.
4886 */
4887 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
4888 exiting = TRUE;
Bram Moolenaarff930ca2017-10-19 17:12:10 +02004889 if ((!buf_hide(wp->w_buffer)
4890 && check_changed(wp->w_buffer, (p_awa ? CCGD_AW : 0)
Bram Moolenaar45d3b142013-11-09 03:31:51 +01004891 | (eap->forceit ? CCGD_FORCEIT : 0)
4892 | CCGD_EXCMD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004893 || check_more(TRUE, eap->forceit) == FAIL
Bram Moolenaar027387f2016-01-02 22:25:52 +01004894 || (only_one_window() && check_changed_any(eap->forceit, TRUE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004895 {
4896 not_exiting();
4897 }
4898 else
4899 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004900 // quit last window
4901 // Note: only_one_window() returns true, even so a help window is
4902 // still open. In that case only quit, if no address has been
4903 // specified. Example:
4904 // :h|wincmd w|1q - don't quit
4905 // :h|wincmd w|q - quit
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01004906 if (only_one_window() && (ONE_WINDOW || eap->addr_count == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004907 getout(0);
Bram Moolenaar2c33d7b2017-10-14 16:06:20 +02004908 not_exiting();
Bram Moolenaar4033c552017-09-16 20:54:51 +02004909#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004910 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02004911#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004912 // close window; may free buffer
Bram Moolenaareb44a682017-08-03 22:44:55 +02004913 win_close(wp, !buf_hide(wp->w_buffer) || eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004914 }
4915}
4916
4917/*
4918 * ":cquit".
4919 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004920 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004921ex_cquit(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004922{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004923 getout(1); // this does not always pass on the exit code to the Manx
4924 // compiler. why?
Bram Moolenaar071d4272004-06-13 20:20:40 +00004925}
4926
4927/*
4928 * ":qall": try to quit all windows
4929 */
4930 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004931ex_quit_all(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004932{
4933# ifdef FEAT_CMDWIN
4934 if (cmdwin_type != 0)
4935 {
4936 if (eap->forceit)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004937 cmdwin_result = K_XF1; // ex_window() takes care of this
Bram Moolenaar071d4272004-06-13 20:20:40 +00004938 else
4939 cmdwin_result = K_XF2;
4940 return;
4941 }
4942# endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00004943
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004944 // Don't quit while editing the command line.
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00004945 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00004946 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00004947 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00004948 return;
4949 }
Bram Moolenaar12a96de2018-03-11 14:44:18 +01004950
4951 if (before_quit_autocmds(curwin, TRUE, eap->forceit))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004952 return;
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00004953
Bram Moolenaar071d4272004-06-13 20:20:40 +00004954 exiting = TRUE;
Bram Moolenaar027387f2016-01-02 22:25:52 +01004955 if (eap->forceit || !check_changed_any(FALSE, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004956 getout(0);
4957 not_exiting();
4958}
4959
Bram Moolenaar071d4272004-06-13 20:20:40 +00004960/*
4961 * ":close": close current window, unless it is the last one
4962 */
4963 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004964ex_close(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004965{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004966 win_T *win;
4967 int winnr = 0;
Bram Moolenaar4033c552017-09-16 20:54:51 +02004968#ifdef FEAT_CMDWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00004969 if (cmdwin_type != 0)
Bram Moolenaar9bd1a7e2011-05-19 14:50:54 +02004970 cmdwin_result = Ctrl_C;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004971 else
Bram Moolenaar4033c552017-09-16 20:54:51 +02004972#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004973 if (!text_locked() && !curbuf_locked())
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004974 {
4975 if (eap->addr_count == 0)
4976 ex_win_close(eap->forceit, curwin, NULL);
Bram Moolenaar6d41c782018-06-06 09:11:12 +02004977 else
4978 {
Bram Moolenaar29323592016-07-24 22:04:11 +02004979 FOR_ALL_WINDOWS(win)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004980 {
4981 winnr++;
4982 if (winnr == eap->line2)
4983 break;
4984 }
4985 if (win == NULL)
4986 win = lastwin;
4987 ex_win_close(eap->forceit, win, NULL);
4988 }
4989 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004990}
4991
Bram Moolenaar4033c552017-09-16 20:54:51 +02004992#ifdef FEAT_QUICKFIX
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004993/*
4994 * ":pclose": Close any preview window.
4995 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004996 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004997ex_pclose(exarg_T *eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004998{
4999 win_T *win;
5000
Bram Moolenaar79648732019-07-18 21:43:07 +02005001 // First close any normal window.
Bram Moolenaar29323592016-07-24 22:04:11 +02005002 FOR_ALL_WINDOWS(win)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005003 if (win->w_p_pvw)
5004 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00005005 ex_win_close(eap->forceit, win, NULL);
Bram Moolenaar79648732019-07-18 21:43:07 +02005006 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005007 }
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01005008# ifdef FEAT_PROP_POPUP
Bram Moolenaar79648732019-07-18 21:43:07 +02005009 // Also when 'previewpopup' is empty, it might have been cleared.
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02005010 popup_close_preview();
Bram Moolenaar79648732019-07-18 21:43:07 +02005011# endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005012}
Bram Moolenaar4033c552017-09-16 20:54:51 +02005013#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005014
Bram Moolenaarf740b292006-02-16 22:11:02 +00005015/*
5016 * Close window "win" and take care of handling closing the last window for a
5017 * modified buffer.
5018 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005019 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005020ex_win_close(
5021 int forceit,
5022 win_T *win,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005023 tabpage_T *tp) // NULL or the tab page "win" is in
Bram Moolenaar071d4272004-06-13 20:20:40 +00005024{
5025 int need_hide;
5026 buf_T *buf = win->w_buffer;
5027
5028 need_hide = (bufIsChanged(buf) && buf->b_nwindows <= 1);
Bram Moolenaareb44a682017-08-03 22:44:55 +02005029 if (need_hide && !buf_hide(buf) && !forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005030 {
Bram Moolenaar4033c552017-09-16 20:54:51 +02005031#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005032 if ((p_confirm || cmdmod.confirm) && p_write)
5033 {
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005034 bufref_T bufref;
5035
5036 set_bufref(&bufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005037 dialog_changed(buf, FALSE);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005038 if (bufref_valid(&bufref) && bufIsChanged(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005039 return;
5040 need_hide = FALSE;
5041 }
5042 else
Bram Moolenaar4033c552017-09-16 20:54:51 +02005043#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005044 {
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02005045 no_write_message();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005046 return;
5047 }
5048 }
5049
Bram Moolenaar4033c552017-09-16 20:54:51 +02005050#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00005051 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005052#endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00005053
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005054 // free buffer when not hiding it or when it's a scratch buffer
Bram Moolenaarf740b292006-02-16 22:11:02 +00005055 if (tp == NULL)
Bram Moolenaareb44a682017-08-03 22:44:55 +02005056 win_close(win, !need_hide && !buf_hide(buf));
Bram Moolenaarf740b292006-02-16 22:11:02 +00005057 else
Bram Moolenaareb44a682017-08-03 22:44:55 +02005058 win_close_othertab(win, !need_hide && !buf_hide(buf), tp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005059}
5060
Bram Moolenaar071d4272004-06-13 20:20:40 +00005061/*
Bram Moolenaardea25702017-01-29 15:18:10 +01005062 * Handle the argument for a tabpage related ex command.
5063 * Returns a tabpage number.
5064 * When an error is encountered then eap->errmsg is set.
5065 */
5066 static int
5067get_tabpage_arg(exarg_T *eap)
5068{
5069 int tab_number;
5070 int unaccept_arg0 = (eap->cmdidx == CMD_tabmove) ? 0 : 1;
5071
5072 if (eap->arg && *eap->arg != NUL)
5073 {
5074 char_u *p = eap->arg;
5075 char_u *p_save;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005076 int relative = 0; // argument +N/-N means: go to N places to the
5077 // right/left relative to the current position.
Bram Moolenaardea25702017-01-29 15:18:10 +01005078
5079 if (*p == '-')
5080 {
5081 relative = -1;
5082 p++;
5083 }
5084 else if (*p == '+')
5085 {
5086 relative = 1;
5087 p++;
5088 }
5089
5090 p_save = p;
5091 tab_number = getdigits(&p);
5092
5093 if (relative == 0)
5094 {
5095 if (STRCMP(p, "$") == 0)
5096 tab_number = LAST_TAB_NR;
5097 else if (p == p_save || *p_save == '-' || *p != NUL
5098 || tab_number > LAST_TAB_NR)
5099 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005100 // No numbers as argument.
Bram Moolenaardea25702017-01-29 15:18:10 +01005101 eap->errmsg = e_invarg;
5102 goto theend;
5103 }
5104 }
5105 else
5106 {
5107 if (*p_save == NUL)
5108 tab_number = 1;
5109 else if (p == p_save || *p_save == '-' || *p != NUL
5110 || tab_number == 0)
5111 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005112 // No numbers as argument.
Bram Moolenaardea25702017-01-29 15:18:10 +01005113 eap->errmsg = e_invarg;
5114 goto theend;
5115 }
5116 tab_number = tab_number * relative + tabpage_index(curtab);
5117 if (!unaccept_arg0 && relative == -1)
5118 --tab_number;
5119 }
5120 if (tab_number < unaccept_arg0 || tab_number > LAST_TAB_NR)
5121 eap->errmsg = e_invarg;
5122 }
5123 else if (eap->addr_count > 0)
5124 {
5125 if (unaccept_arg0 && eap->line2 == 0)
Bram Moolenaarc6251552017-01-29 21:42:20 +01005126 {
Bram Moolenaardea25702017-01-29 15:18:10 +01005127 eap->errmsg = e_invrange;
Bram Moolenaarc6251552017-01-29 21:42:20 +01005128 tab_number = 0;
5129 }
Bram Moolenaardea25702017-01-29 15:18:10 +01005130 else
5131 {
5132 tab_number = eap->line2;
Bram Moolenaar82a12462019-01-22 22:41:42 +01005133 if (!unaccept_arg0 && *skipwhite(*eap->cmdlinep) == '-')
Bram Moolenaardea25702017-01-29 15:18:10 +01005134 {
5135 --tab_number;
5136 if (tab_number < unaccept_arg0)
5137 eap->errmsg = e_invarg;
5138 }
5139 }
5140 }
5141 else
5142 {
5143 switch (eap->cmdidx)
5144 {
5145 case CMD_tabnext:
5146 tab_number = tabpage_index(curtab) + 1;
5147 if (tab_number > LAST_TAB_NR)
5148 tab_number = 1;
5149 break;
5150 case CMD_tabmove:
5151 tab_number = LAST_TAB_NR;
5152 break;
5153 default:
5154 tab_number = tabpage_index(curtab);
5155 }
5156 }
5157
5158theend:
5159 return tab_number;
5160}
5161
5162/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00005163 * ":tabclose": close current tab page, unless it is the last one.
5164 * ":tabclose N": close tab page N.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005165 */
5166 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005167ex_tabclose(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005168{
Bram Moolenaarf740b292006-02-16 22:11:02 +00005169 tabpage_T *tp;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005170 int tab_number;
Bram Moolenaarf740b292006-02-16 22:11:02 +00005171
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005172# ifdef FEAT_CMDWIN
5173 if (cmdwin_type != 0)
5174 cmdwin_result = K_IGNORE;
5175 else
5176# endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00005177 if (first_tabpage->tp_next == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005178 emsg(_("E784: Cannot close last tab page"));
Bram Moolenaarf740b292006-02-16 22:11:02 +00005179 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005180 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005181 tab_number = get_tabpage_arg(eap);
5182 if (eap->errmsg == NULL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005183 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005184 tp = find_tabpage(tab_number);
Bram Moolenaarf740b292006-02-16 22:11:02 +00005185 if (tp == NULL)
5186 {
5187 beep_flush();
5188 return;
5189 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005190 if (tp != curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00005191 {
5192 tabpage_close_other(tp, eap->forceit);
5193 return;
5194 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005195 else if (!text_locked() && !curbuf_locked())
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005196 tabpage_close(eap->forceit);
5197 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005198 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005199}
5200
5201/*
5202 * ":tabonly": close all tab pages except the current one
5203 */
5204 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005205ex_tabonly(exarg_T *eap)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005206{
5207 tabpage_T *tp;
5208 int done;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005209 int tab_number;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005210
5211# ifdef FEAT_CMDWIN
5212 if (cmdwin_type != 0)
5213 cmdwin_result = K_IGNORE;
5214 else
5215# endif
5216 if (first_tabpage->tp_next == NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01005217 msg(_("Already only one tab page"));
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005218 else
5219 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005220 tab_number = get_tabpage_arg(eap);
5221 if (eap->errmsg == NULL)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005222 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005223 goto_tabpage(tab_number);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005224 // Repeat this up to a 1000 times, because autocommands may
5225 // mess up the lists.
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005226 for (done = 0; done < 1000; ++done)
5227 {
5228 FOR_ALL_TABPAGES(tp)
5229 if (tp->tp_topframe != topframe)
5230 {
5231 tabpage_close_other(tp, eap->forceit);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005232 // if we failed to close it quit
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005233 if (valid_tabpage(tp))
5234 done = 1000;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005235 // start over, "tp" is now invalid
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005236 break;
5237 }
5238 if (first_tabpage->tp_next == NULL)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005239 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005240 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005241 }
5242 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005243}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005244
5245/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00005246 * Close the current tab page.
5247 */
5248 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005249tabpage_close(int forceit)
Bram Moolenaarf740b292006-02-16 22:11:02 +00005250{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005251 // First close all the windows but the current one. If that worked then
5252 // close the last window in this tab, that will close it.
Bram Moolenaar459ca562016-11-10 18:16:33 +01005253 if (!ONE_WINDOW)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005254 close_others(TRUE, forceit);
Bram Moolenaar459ca562016-11-10 18:16:33 +01005255 if (ONE_WINDOW)
Bram Moolenaarf740b292006-02-16 22:11:02 +00005256 ex_win_close(forceit, curwin, NULL);
5257# ifdef FEAT_GUI
5258 need_mouse_correct = TRUE;
5259# endif
5260}
5261
5262/*
5263 * Close tab page "tp", which is not the current tab page.
5264 * Note that autocommands may make "tp" invalid.
Bram Moolenaar7875acc2006-09-10 13:51:17 +00005265 * Also takes care of the tab pages line disappearing when closing the
5266 * last-but-one tab page.
Bram Moolenaarf740b292006-02-16 22:11:02 +00005267 */
5268 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005269tabpage_close_other(tabpage_T *tp, int forceit)
Bram Moolenaarf740b292006-02-16 22:11:02 +00005270{
5271 int done = 0;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005272 win_T *wp;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00005273 int h = tabline_height();
Bram Moolenaarf740b292006-02-16 22:11:02 +00005274
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005275 // Limit to 1000 windows, autocommands may add a window while we close
5276 // one. OK, so I'm paranoid...
Bram Moolenaarf740b292006-02-16 22:11:02 +00005277 while (++done < 1000)
5278 {
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005279 wp = tp->tp_firstwin;
5280 ex_win_close(forceit, wp, tp);
Bram Moolenaarf740b292006-02-16 22:11:02 +00005281
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005282 // Autocommands may delete the tab page under our fingers and we may
5283 // fail to close a window with a modified buffer.
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005284 if (!valid_tabpage(tp) || tp->tp_firstwin == wp)
Bram Moolenaarf740b292006-02-16 22:11:02 +00005285 break;
5286 }
Bram Moolenaar7875acc2006-09-10 13:51:17 +00005287
Bram Moolenaar29323592016-07-24 22:04:11 +02005288 apply_autocmds(EVENT_TABCLOSED, NULL, NULL, FALSE, curbuf);
Bram Moolenaar29323592016-07-24 22:04:11 +02005289
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005290 redraw_tabline = TRUE;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00005291 if (h != tabline_height())
5292 shell_new_rows();
Bram Moolenaarf740b292006-02-16 22:11:02 +00005293}
5294
5295/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005296 * ":only".
5297 */
5298 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005299ex_only(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005300{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005301 win_T *wp;
5302 int wnr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005303# ifdef FEAT_GUI
5304 need_mouse_correct = TRUE;
5305# endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005306 if (eap->addr_count > 0)
5307 {
5308 wnr = eap->line2;
5309 for (wp = firstwin; --wnr > 0; )
5310 {
5311 if (wp->w_next == NULL)
5312 break;
5313 else
5314 wp = wp->w_next;
5315 }
5316 win_goto(wp);
5317 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005318 close_others(TRUE, eap->forceit);
5319}
5320
Bram Moolenaar071d4272004-06-13 20:20:40 +00005321 static void
Bram Moolenaar5e1e6d22017-01-02 17:26:00 +01005322ex_hide(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005323{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005324 // ":hide" or ":hide | cmd": hide current window
Bram Moolenaar2256c992016-11-15 21:17:07 +01005325 if (!eap->skip)
5326 {
Bram Moolenaar4033c552017-09-16 20:54:51 +02005327#ifdef FEAT_GUI
Bram Moolenaar2256c992016-11-15 21:17:07 +01005328 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005329#endif
Bram Moolenaar2256c992016-11-15 21:17:07 +01005330 if (eap->addr_count == 0)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005331 win_close(curwin, FALSE); // don't free buffer
Bram Moolenaar2256c992016-11-15 21:17:07 +01005332 else
5333 {
5334 int winnr = 0;
5335 win_T *win;
Bram Moolenaarf240e182014-11-27 18:33:02 +01005336
Bram Moolenaar2256c992016-11-15 21:17:07 +01005337 FOR_ALL_WINDOWS(win)
5338 {
5339 winnr++;
5340 if (winnr == eap->line2)
5341 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005342 }
Bram Moolenaar2256c992016-11-15 21:17:07 +01005343 if (win == NULL)
5344 win = lastwin;
5345 win_close(win, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005346 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005347 }
5348}
5349
5350/*
5351 * ":stop" and ":suspend": Suspend Vim.
5352 */
5353 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005354ex_stop(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005355{
5356 /*
5357 * Disallow suspending for "rvim".
5358 */
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005359 if (!check_restricted())
Bram Moolenaar071d4272004-06-13 20:20:40 +00005360 {
5361 if (!eap->forceit)
5362 autowrite_all();
5363 windgoto((int)Rows - 1, 0);
5364 out_char('\n');
5365 out_flush();
5366 stoptermcap();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005367 out_flush(); // needed for SUN to restore xterm buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00005368#ifdef FEAT_TITLE
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005369 mch_restore_title(SAVE_RESTORE_BOTH); // restore window titles
Bram Moolenaar071d4272004-06-13 20:20:40 +00005370#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005371 ui_suspend(); // call machine specific function
Bram Moolenaar071d4272004-06-13 20:20:40 +00005372#ifdef FEAT_TITLE
5373 maketitle();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005374 resettitle(); // force updating the title
Bram Moolenaar071d4272004-06-13 20:20:40 +00005375#endif
5376 starttermcap();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005377 scroll_start(); // scroll screen before redrawing
Bram Moolenaar071d4272004-06-13 20:20:40 +00005378 redraw_later_clear();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005379 shell_resized(); // may have resized window
Bram Moolenaar071d4272004-06-13 20:20:40 +00005380 }
5381}
5382
5383/*
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005384 * ":exit", ":xit" and ":wq": Write file and quite the current window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005385 */
5386 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005387ex_exit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005388{
5389#ifdef FEAT_CMDWIN
5390 if (cmdwin_type != 0)
5391 {
5392 cmdwin_result = Ctrl_C;
5393 return;
5394 }
5395#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005396 // Don't quit while editing the command line.
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005397 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005398 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005399 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005400 return;
5401 }
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005402
5403 if (before_quit_autocmds(curwin, FALSE, eap->forceit))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005404 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005405
5406 /*
5407 * if more files or windows we won't exit
5408 */
5409 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
5410 exiting = TRUE;
5411 if ( ((eap->cmdidx == CMD_wq
5412 || curbufIsChanged())
5413 && do_write(eap) == FAIL)
5414 || check_more(TRUE, eap->forceit) == FAIL
Bram Moolenaar027387f2016-01-02 22:25:52 +01005415 || (only_one_window() && check_changed_any(eap->forceit, FALSE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005416 {
5417 not_exiting();
5418 }
5419 else
5420 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005421 if (only_one_window()) // quit last window, exit Vim
Bram Moolenaar071d4272004-06-13 20:20:40 +00005422 getout(0);
Bram Moolenaar2c33d7b2017-10-14 16:06:20 +02005423 not_exiting();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005424# ifdef FEAT_GUI
5425 need_mouse_correct = TRUE;
5426# endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005427 // Quit current window, may free the buffer.
Bram Moolenaareb44a682017-08-03 22:44:55 +02005428 win_close(curwin, !buf_hide(curwin->w_buffer));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005429 }
5430}
5431
5432/*
5433 * ":print", ":list", ":number".
5434 */
5435 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005436ex_print(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005437{
Bram Moolenaardf177f62005-02-22 08:39:57 +00005438 if (curbuf->b_ml.ml_flags & ML_EMPTY)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005439 emsg(_(e_emptybuf));
Bram Moolenaardf177f62005-02-22 08:39:57 +00005440 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005441 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00005442 for ( ;!got_int; ui_breakcheck())
5443 {
5444 print_line(eap->line1,
5445 (eap->cmdidx == CMD_number || eap->cmdidx == CMD_pound
5446 || (eap->flags & EXFLAG_NR)),
5447 eap->cmdidx == CMD_list || (eap->flags & EXFLAG_LIST));
5448 if (++eap->line1 > eap->line2)
5449 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005450 out_flush(); // show one line at a time
Bram Moolenaardf177f62005-02-22 08:39:57 +00005451 }
5452 setpcmark();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005453 // put cursor at last line
Bram Moolenaardf177f62005-02-22 08:39:57 +00005454 curwin->w_cursor.lnum = eap->line2;
5455 beginline(BL_SOL | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005456 }
5457
Bram Moolenaar071d4272004-06-13 20:20:40 +00005458 ex_no_reprint = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005459}
5460
5461#ifdef FEAT_BYTEOFF
5462 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005463ex_goto(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005464{
5465 goto_byte(eap->line2);
5466}
5467#endif
5468
5469/*
5470 * ":shell".
5471 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005472 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005473ex_shell(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005474{
5475 do_shell(NULL, 0);
5476}
5477
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005478#if defined(HAVE_DROP_FILE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005479
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005480static int drop_busy = FALSE;
5481static int drop_filec;
5482static char_u **drop_filev = NULL;
5483static int drop_split;
5484static void (*drop_callback)(void *);
5485static void *drop_cookie;
5486
5487 static void
5488handle_drop_internal(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005489{
5490 exarg_T ea;
5491 int save_msg_scroll = msg_scroll;
5492
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005493 // Setting the argument list may cause screen updates and being called
5494 // recursively. Avoid that by setting drop_busy.
5495 drop_busy = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005496
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005497 // Check whether the current buffer is changed. If so, we will need
5498 // to split the current window or data could be lost.
5499 // We don't need to check if the 'hidden' option is set, as in this
5500 // case the buffer won't be lost.
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005501 if (!buf_hide(curbuf) && !drop_split)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005502 {
5503 ++emsg_off;
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005504 drop_split = check_changed(curbuf, CCGD_AW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005505 --emsg_off;
5506 }
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005507 if (drop_split)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005508 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005509 if (win_split(0, 0) == FAIL)
5510 return;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02005511 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005512
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005513 // When splitting the window, create a new alist. Otherwise the
5514 // existing one is overwritten.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005515 alist_unlink(curwin->w_alist);
5516 alist_new();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005517 }
5518
5519 /*
5520 * Set up the new argument list.
5521 */
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005522 alist_set(ALIST(curwin), drop_filec, drop_filev, FALSE, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005523
5524 /*
5525 * Move to the first file.
5526 */
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005527 // Fake up a minimal "next" command for do_argfile()
Bram Moolenaar071d4272004-06-13 20:20:40 +00005528 vim_memset(&ea, 0, sizeof(ea));
5529 ea.cmd = (char_u *)"next";
5530 do_argfile(&ea, 0);
5531
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005532 // do_ecmd() may set need_start_insertmode, but since we never left Insert
5533 // mode that is not needed here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005534 need_start_insertmode = FALSE;
5535
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005536 // Restore msg_scroll, otherwise a following command may cause scrolling
5537 // unexpectedly. The screen will be redrawn by the caller, thus
5538 // msg_scroll being set by displaying a message is irrelevant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005539 msg_scroll = save_msg_scroll;
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005540
5541 if (drop_callback != NULL)
5542 drop_callback(drop_cookie);
5543
5544 drop_filev = NULL;
5545 drop_busy = FALSE;
5546}
5547
5548/*
5549 * Handle a file drop. The code is here because a drop is *nearly* like an
5550 * :args command, but not quite (we have a list of exact filenames, so we
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01005551 * don't want to (a) parse a command line, or (b) expand wildcards). So the
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005552 * code is very similar to :args and hence needs access to a lot of the static
5553 * functions in this file.
5554 *
5555 * The "filev" list must have been allocated using alloc(), as should each item
5556 * in the list. This function takes over responsibility for freeing the "filev"
5557 * list.
5558 */
5559 void
5560handle_drop(
5561 int filec, // the number of files dropped
5562 char_u **filev, // the list of files dropped
5563 int split, // force splitting the window
5564 void (*callback)(void *), // to be called after setting the argument
5565 // list
5566 void *cookie) // argument for "callback" (allocated)
5567{
5568 // Cannot handle recursive drops, finish the pending one.
5569 if (drop_busy)
5570 {
5571 FreeWild(filec, filev);
5572 vim_free(cookie);
5573 return;
5574 }
5575
5576 // When calling handle_drop() more than once in a row we only use the last
5577 // one.
5578 if (drop_filev != NULL)
5579 {
5580 FreeWild(drop_filec, drop_filev);
5581 vim_free(drop_cookie);
5582 }
5583
5584 drop_filec = filec;
5585 drop_filev = filev;
5586 drop_split = split;
5587 drop_callback = callback;
5588 drop_cookie = cookie;
5589
5590 // Postpone this when:
5591 // - editing the command line
5592 // - not possible to change the current buffer
5593 // - updating the screen
5594 // As it may change buffers and window structures that are in use and cause
5595 // freed memory to be used.
5596 if (text_locked() || curbuf_locked() || updating_screen)
5597 return;
5598
5599 handle_drop_internal();
5600}
5601
5602/*
5603 * To be called when text is unlocked, curbuf is unlocked or updating_screen is
5604 * reset: Handle a postponed drop.
5605 */
5606 void
5607handle_any_postponed_drop(void)
5608{
5609 if (!drop_busy && drop_filev != NULL
5610 && !text_locked() && !curbuf_locked() && !updating_screen)
5611 handle_drop_internal();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005612}
5613#endif
5614
Bram Moolenaar071d4272004-06-13 20:20:40 +00005615/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005616 * ":preserve".
5617 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005618 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005619ex_preserve(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005620{
Bram Moolenaar4399ef42005-02-12 14:29:27 +00005621 curbuf->b_flags |= BF_PRESERVED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005622 ml_preserve(curbuf, TRUE);
5623}
5624
5625/*
5626 * ":recover".
5627 */
5628 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005629ex_recover(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005630{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005631 // Set recoverymode right away to avoid the ATTENTION prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005632 recoverymode = TRUE;
Bram Moolenaar45d3b142013-11-09 03:31:51 +01005633 if (!check_changed(curbuf, (p_awa ? CCGD_AW : 0)
5634 | CCGD_MULTWIN
5635 | (eap->forceit ? CCGD_FORCEIT : 0)
5636 | CCGD_EXCMD)
5637
Bram Moolenaar071d4272004-06-13 20:20:40 +00005638 && (*eap->arg == NUL
5639 || setfname(curbuf, eap->arg, NULL, TRUE) == OK))
Bram Moolenaar99499b12019-05-23 21:35:48 +02005640 ml_recover(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005641 recoverymode = FALSE;
5642}
5643
5644/*
5645 * Command modifier used in a wrong way.
5646 */
5647 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005648ex_wrongmodifier(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005649{
5650 eap->errmsg = e_invcmd;
5651}
5652
Bram Moolenaar071d4272004-06-13 20:20:40 +00005653/*
5654 * :sview [+command] file split window with new file, read-only
5655 * :split [[+command] file] split window with current or new file
5656 * :vsplit [[+command] file] split window vertically with current or new file
5657 * :new [[+command] file] split window with no or new file
5658 * :vnew [[+command] file] split vertically window with no or new file
5659 * :sfind [+command] file split window with file in 'path'
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005660 *
5661 * :tabedit open new Tab page with empty window
5662 * :tabedit [+command] file open new Tab page and edit "file"
5663 * :tabnew [[+command] file] just like :tabedit
5664 * :tabfind [+command] file open new Tab page and find "file"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005665 */
5666 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005667ex_splitview(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005668{
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005669 win_T *old_curwin = curwin;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005670#if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005671 char_u *fname = NULL;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005672#endif
5673#ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00005674 int browse_flag = cmdmod.browse;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005675#endif
Bram Moolenaar39902a02018-06-21 22:10:08 +02005676 int use_tab = eap->cmdidx == CMD_tabedit
5677 || eap->cmdidx == CMD_tabfind
5678 || eap->cmdidx == CMD_tabnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005679
Bram Moolenaar8cdbd5b2019-06-16 15:50:45 +02005680 if (ERROR_IF_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02005681 return;
5682
Bram Moolenaar4033c552017-09-16 20:54:51 +02005683#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00005684 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005685#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005686
Bram Moolenaar4033c552017-09-16 20:54:51 +02005687#ifdef FEAT_QUICKFIX
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005688 // A ":split" in the quickfix window works like ":new". Don't want two
5689 // quickfix windows. But it's OK when doing ":tab split".
Bram Moolenaar05bb9532008-07-04 09:44:11 +00005690 if (bt_quickfix(curbuf) && cmdmod.tab == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005691 {
5692 if (eap->cmdidx == CMD_split)
5693 eap->cmdidx = CMD_new;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005694 if (eap->cmdidx == CMD_vsplit)
5695 eap->cmdidx = CMD_vnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005696 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02005697#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005698
Bram Moolenaar4033c552017-09-16 20:54:51 +02005699#ifdef FEAT_SEARCHPATH
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005700 if (eap->cmdidx == CMD_sfind || eap->cmdidx == CMD_tabfind)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005701 {
5702 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg),
5703 FNAME_MESS, TRUE, curbuf->b_ffname);
5704 if (fname == NULL)
5705 goto theend;
5706 eap->arg = fname;
5707 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005708# ifdef FEAT_BROWSE
Bram Moolenaar4033c552017-09-16 20:54:51 +02005709 else
5710# endif
5711#endif
5712#ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00005713 if (cmdmod.browse
Bram Moolenaar071d4272004-06-13 20:20:40 +00005714 && eap->cmdidx != CMD_vnew
Bram Moolenaar071d4272004-06-13 20:20:40 +00005715 && eap->cmdidx != CMD_new)
5716 {
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00005717 if (
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005718# ifdef FEAT_GUI
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00005719 !gui.in_use &&
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005720# endif
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00005721 au_has_group((char_u *)"FileExplorer"))
5722 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005723 // No browsing supported but we do have the file explorer:
5724 // Edit the directory.
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00005725 if (*eap->arg == NUL || !mch_isdir(eap->arg))
5726 eap->arg = (char_u *)".";
5727 }
5728 else
5729 {
Bram Moolenaar39902a02018-06-21 22:10:08 +02005730 fname = do_browse(0, (char_u *)(use_tab
5731 ? _("Edit File in new tab page")
5732 : _("Edit File in new window")),
Bram Moolenaar071d4272004-06-13 20:20:40 +00005733 eap->arg, NULL, NULL, NULL, curbuf);
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00005734 if (fname == NULL)
5735 goto theend;
5736 eap->arg = fname;
5737 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005738 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005739 cmdmod.browse = FALSE; // Don't browse again in do_ecmd().
Bram Moolenaar4033c552017-09-16 20:54:51 +02005740#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005741
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005742 /*
5743 * Either open new tab page or split the window.
5744 */
Bram Moolenaar39902a02018-06-21 22:10:08 +02005745 if (use_tab)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005746 {
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005747 if (win_new_tabpage(cmdmod.tab != 0 ? cmdmod.tab
5748 : eap->addr_count == 0 ? 0
5749 : (int)eap->line2 + 1) != FAIL)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005750 {
Bram Moolenaard2b66012008-01-09 19:30:36 +00005751 do_exedit(eap, old_curwin);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005752
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005753 // set the alternate buffer for the window we came from
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005754 if (curwin != old_curwin
5755 && win_valid(old_curwin)
5756 && old_curwin->w_buffer != curbuf
5757 && !cmdmod.keepalt)
5758 old_curwin->w_alt_fnum = curbuf->b_fnum;
5759 }
5760 }
5761 else if (win_split(eap->addr_count > 0 ? (int)eap->line2 : 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005762 *eap->cmd == 'v' ? WSP_VERT : 0) != FAIL)
5763 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005764 // Reset 'scrollbind' when editing another file, but keep it when
5765 // doing ":split" without arguments.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005766 if (*eap->arg != NUL
Bram Moolenaar8a3bb562018-03-04 20:14:14 +01005767# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00005768 || cmdmod.browse
Bram Moolenaar8a3bb562018-03-04 20:14:14 +01005769# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005770 )
Bram Moolenaar3368ea22010-09-21 16:56:35 +02005771 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005772 else
5773 do_check_scrollbind(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005774 do_exedit(eap, old_curwin);
5775 }
5776
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005777# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00005778 cmdmod.browse = browse_flag;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005779# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005780
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005781# if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005782theend:
5783 vim_free(fname);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005784# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005785}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005786
5787/*
Bram Moolenaar80a94a52006-02-23 21:26:58 +00005788 * Open a new tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005789 */
5790 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005791tabpage_new(void)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00005792{
5793 exarg_T ea;
5794
5795 vim_memset(&ea, 0, sizeof(ea));
5796 ea.cmdidx = CMD_tabnew;
5797 ea.cmd = (char_u *)"tabn";
5798 ea.arg = (char_u *)"";
5799 ex_splitview(&ea);
5800}
5801
5802/*
5803 * :tabnext command
5804 */
5805 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005806ex_tabnext(exarg_T *eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005807{
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005808 int tab_number;
5809
Bram Moolenaar8cdbd5b2019-06-16 15:50:45 +02005810 if (ERROR_IF_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02005811 return;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005812 switch (eap->cmdidx)
5813 {
5814 case CMD_tabfirst:
5815 case CMD_tabrewind:
5816 goto_tabpage(1);
5817 break;
5818 case CMD_tablast:
5819 goto_tabpage(9999);
5820 break;
5821 case CMD_tabprevious:
5822 case CMD_tabNext:
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005823 if (eap->arg && *eap->arg != NUL)
5824 {
5825 char_u *p = eap->arg;
5826 char_u *p_save = p;
5827
5828 tab_number = getdigits(&p);
5829 if (p == p_save || *p_save == '-' || *p != NUL
5830 || tab_number == 0)
5831 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005832 // No numbers as argument.
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005833 eap->errmsg = e_invarg;
5834 return;
5835 }
5836 }
5837 else
5838 {
5839 if (eap->addr_count == 0)
5840 tab_number = 1;
5841 else
5842 {
5843 tab_number = eap->line2;
5844 if (tab_number < 1)
5845 {
5846 eap->errmsg = e_invrange;
5847 return;
5848 }
5849 }
5850 }
5851 goto_tabpage(-tab_number);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005852 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005853 default: // CMD_tabnext
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005854 tab_number = get_tabpage_arg(eap);
5855 if (eap->errmsg == NULL)
5856 goto_tabpage(tab_number);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005857 break;
5858 }
Bram Moolenaar80a94a52006-02-23 21:26:58 +00005859}
5860
5861/*
5862 * :tabmove command
5863 */
5864 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005865ex_tabmove(exarg_T *eap)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00005866{
Bram Moolenaar40ce3a42015-04-21 18:08:39 +02005867 int tab_number;
Bram Moolenaar8cb8dca2012-07-06 18:27:39 +02005868
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005869 tab_number = get_tabpage_arg(eap);
5870 if (eap->errmsg == NULL)
5871 tabpage_move(tab_number);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005872}
5873
5874/*
5875 * :tabs command: List tabs and their contents.
5876 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005877 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005878ex_tabs(exarg_T *eap UNUSED)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005879{
5880 tabpage_T *tp;
5881 win_T *wp;
5882 int tabcount = 1;
5883
5884 msg_start();
5885 msg_scroll = TRUE;
5886 for (tp = first_tabpage; tp != NULL && !got_int; tp = tp->tp_next)
5887 {
5888 msg_putchar('\n');
5889 vim_snprintf((char *)IObuff, IOSIZE, _("Tab page %d"), tabcount++);
Bram Moolenaar8820b482017-03-16 17:23:31 +01005890 msg_outtrans_attr(IObuff, HL_ATTR(HLF_T));
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005891 out_flush(); // output one line at a time
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005892 ui_breakcheck();
5893
Bram Moolenaar030f0df2006-02-21 22:02:53 +00005894 if (tp == curtab)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005895 wp = firstwin;
5896 else
5897 wp = tp->tp_firstwin;
5898 for ( ; wp != NULL && !got_int; wp = wp->w_next)
5899 {
Bram Moolenaar80a94a52006-02-23 21:26:58 +00005900 msg_putchar('\n');
5901 msg_putchar(wp == curwin ? '>' : ' ');
5902 msg_putchar(' ');
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005903 msg_putchar(bufIsChanged(wp->w_buffer) ? '+' : ' ');
5904 msg_putchar(' ');
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005905 if (buf_spname(wp->w_buffer) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02005906 vim_strncpy(IObuff, buf_spname(wp->w_buffer), IOSIZE - 1);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005907 else
5908 home_replace(wp->w_buffer, wp->w_buffer->b_fname,
5909 IObuff, IOSIZE, TRUE);
5910 msg_outtrans(IObuff);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005911 out_flush(); // output one line at a time
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005912 ui_breakcheck();
5913 }
5914 }
5915}
5916
Bram Moolenaar071d4272004-06-13 20:20:40 +00005917/*
5918 * ":mode": Set screen mode.
5919 * If no argument given, just get the screen size and redraw.
5920 */
5921 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005922ex_mode(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005923{
5924 if (*eap->arg == NUL)
5925 shell_resized();
5926 else
5927 mch_screenmode(eap->arg);
5928}
5929
Bram Moolenaar071d4272004-06-13 20:20:40 +00005930/*
5931 * ":resize".
5932 * set, increment or decrement current window height
5933 */
5934 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005935ex_resize(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005936{
5937 int n;
5938 win_T *wp = curwin;
5939
5940 if (eap->addr_count > 0)
5941 {
5942 n = eap->line2;
5943 for (wp = firstwin; wp->w_next != NULL && --n > 0; wp = wp->w_next)
5944 ;
5945 }
5946
Bram Moolenaar44a2f922016-03-19 22:11:51 +01005947# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00005948 need_mouse_correct = TRUE;
Bram Moolenaar44a2f922016-03-19 22:11:51 +01005949# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005950 n = atol((char *)eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005951 if (cmdmod.split & WSP_VERT)
5952 {
5953 if (*eap->arg == '-' || *eap->arg == '+')
Bram Moolenaar02631462017-09-22 15:20:32 +02005954 n += curwin->w_width;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005955 else if (n == 0 && eap->arg[0] == NUL) // default is very wide
Bram Moolenaar071d4272004-06-13 20:20:40 +00005956 n = 9999;
5957 win_setwidth_win((int)n, wp);
5958 }
5959 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005960 {
5961 if (*eap->arg == '-' || *eap->arg == '+')
5962 n += curwin->w_height;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005963 else if (n == 0 && eap->arg[0] == NUL) // default is very high
Bram Moolenaar071d4272004-06-13 20:20:40 +00005964 n = 9999;
5965 win_setheight_win((int)n, wp);
5966 }
5967}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005968
5969/*
5970 * ":find [+command] <file>" command.
5971 */
5972 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005973ex_find(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005974{
5975#ifdef FEAT_SEARCHPATH
5976 char_u *fname;
5977 int count;
5978
5979 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg), FNAME_MESS,
5980 TRUE, curbuf->b_ffname);
5981 if (eap->addr_count > 0)
5982 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005983 // Repeat finding the file "count" times. This matters when it
5984 // appears several times in the path.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005985 count = eap->line2;
5986 while (fname != NULL && --count > 0)
5987 {
5988 vim_free(fname);
5989 fname = find_file_in_path(NULL, 0, FNAME_MESS,
5990 FALSE, curbuf->b_ffname);
5991 }
5992 }
5993
5994 if (fname != NULL)
5995 {
5996 eap->arg = fname;
5997#endif
5998 do_exedit(eap, NULL);
5999#ifdef FEAT_SEARCHPATH
6000 vim_free(fname);
6001 }
6002#endif
6003}
6004
6005/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00006006 * ":open" simulation: for now just work like ":visual".
6007 */
6008 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006009ex_open(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00006010{
6011 regmatch_T regmatch;
6012 char_u *p;
6013
6014 curwin->w_cursor.lnum = eap->line2;
6015 beginline(BL_SOL | BL_FIX);
6016 if (*eap->arg == '/')
6017 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006018 // ":open /pattern/": put cursor in column found with pattern
Bram Moolenaardf177f62005-02-22 08:39:57 +00006019 ++eap->arg;
6020 p = skip_regexp(eap->arg, '/', p_magic, NULL);
6021 *p = NUL;
6022 regmatch.regprog = vim_regcomp(eap->arg, p_magic ? RE_MAGIC : 0);
6023 if (regmatch.regprog != NULL)
6024 {
6025 regmatch.rm_ic = p_ic;
6026 p = ml_get_curline();
6027 if (vim_regexec(&regmatch, p, (colnr_T)0))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006028 curwin->w_cursor.col = (colnr_T)(regmatch.startp[0] - p);
Bram Moolenaardf177f62005-02-22 08:39:57 +00006029 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006030 emsg(_(e_nomatch));
Bram Moolenaar473de612013-06-08 18:19:48 +02006031 vim_regfree(regmatch.regprog);
Bram Moolenaardf177f62005-02-22 08:39:57 +00006032 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006033 // Move to the NUL, ignore any other arguments.
Bram Moolenaardf177f62005-02-22 08:39:57 +00006034 eap->arg += STRLEN(eap->arg);
6035 }
6036 check_cursor();
6037
6038 eap->cmdidx = CMD_visual;
6039 do_exedit(eap, NULL);
6040}
6041
6042/*
6043 * ":edit", ":badd", ":visual".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006044 */
6045 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006046ex_edit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006047{
6048 do_exedit(eap, NULL);
6049}
6050
6051/*
6052 * ":edit <file>" command and alikes.
6053 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006054 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006055do_exedit(
6056 exarg_T *eap,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006057 win_T *old_curwin) // curwin before doing a split or NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006058{
6059 int n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006060 int need_hide;
Bram Moolenaardf177f62005-02-22 08:39:57 +00006061 int exmode_was = exmode_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006062
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02006063 if (eap->cmdidx != CMD_pedit && ERROR_IF_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02006064 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006065 /*
6066 * ":vi" command ends Ex mode.
6067 */
6068 if (exmode_active && (eap->cmdidx == CMD_visual
6069 || eap->cmdidx == CMD_view))
6070 {
6071 exmode_active = FALSE;
6072 if (*eap->arg == NUL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00006073 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006074 // Special case: ":global/pat/visual\NLvi-commands"
Bram Moolenaardf177f62005-02-22 08:39:57 +00006075 if (global_busy)
6076 {
6077 int rd = RedrawingDisabled;
6078 int nwr = no_wait_return;
6079 int ms = msg_scroll;
6080#ifdef FEAT_GUI
6081 int he = hold_gui_events;
6082#endif
6083
6084 if (eap->nextcmd != NULL)
6085 {
6086 stuffReadbuff(eap->nextcmd);
6087 eap->nextcmd = NULL;
6088 }
6089
6090 if (exmode_was != EXMODE_VIM)
6091 settmode(TMODE_RAW);
6092 RedrawingDisabled = 0;
6093 no_wait_return = 0;
6094 need_wait_return = FALSE;
6095 msg_scroll = 0;
6096#ifdef FEAT_GUI
6097 hold_gui_events = 0;
6098#endif
6099 must_redraw = CLEAR;
6100
6101 main_loop(FALSE, TRUE);
6102
6103 RedrawingDisabled = rd;
6104 no_wait_return = nwr;
6105 msg_scroll = ms;
6106#ifdef FEAT_GUI
6107 hold_gui_events = he;
6108#endif
6109 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006110 return;
Bram Moolenaardf177f62005-02-22 08:39:57 +00006111 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006112 }
6113
6114 if ((eap->cmdidx == CMD_new
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006115 || eap->cmdidx == CMD_tabnew
6116 || eap->cmdidx == CMD_tabedit
Bram Moolenaar4033c552017-09-16 20:54:51 +02006117 || eap->cmdidx == CMD_vnew) && *eap->arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006118 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006119 // ":new" or ":tabnew" without argument: edit an new empty buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00006120 setpcmark();
6121 (void)do_ecmd(0, NULL, NULL, eap, ECMD_ONE,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00006122 ECMD_HIDE + (eap->forceit ? ECMD_FORCEIT : 0),
6123 old_curwin == NULL ? curwin : NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006124 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02006125 else if ((eap->cmdidx != CMD_split && eap->cmdidx != CMD_vsplit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006126 || *eap->arg != NUL
6127#ifdef FEAT_BROWSE
6128 || cmdmod.browse
6129#endif
6130 )
6131 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006132 // Can't edit another file when "curbuf_lock" is set. Only ":edit"
6133 // can bring us here, others are stopped earlier.
Bram Moolenaar5555acc2006-04-07 21:33:12 +00006134 if (*eap->arg != NUL && curbuf_locked())
6135 return;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006136
Bram Moolenaar071d4272004-06-13 20:20:40 +00006137 n = readonlymode;
6138 if (eap->cmdidx == CMD_view || eap->cmdidx == CMD_sview)
6139 readonlymode = TRUE;
6140 else if (eap->cmdidx == CMD_enew)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006141 readonlymode = FALSE; // 'readonly' doesn't make sense in an
6142 // empty buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00006143 setpcmark();
6144 if (do_ecmd(0, (eap->cmdidx == CMD_enew ? NULL : eap->arg),
6145 NULL, eap,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006146 // ":edit" goes to first line if Vi compatible
Bram Moolenaar071d4272004-06-13 20:20:40 +00006147 (*eap->arg == NUL && eap->do_ecmd_lnum == 0
6148 && vim_strchr(p_cpo, CPO_GOTO1) != NULL)
6149 ? ECMD_ONE : eap->do_ecmd_lnum,
Bram Moolenaareb44a682017-08-03 22:44:55 +02006150 (buf_hide(curbuf) ? ECMD_HIDE : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006151 + (eap->forceit ? ECMD_FORCEIT : 0)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006152 // after a split we can use an existing buffer
Bram Moolenaar7b449342014-03-25 13:03:48 +01006153 + (old_curwin != NULL ? ECMD_OLDBUF : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006154 + (eap->cmdidx == CMD_badd ? ECMD_ADDBUF : 0 )
Bram Moolenaar701f7af2008-11-15 13:12:07 +00006155 , old_curwin == NULL ? curwin : NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006156 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006157 // Editing the file failed. If the window was split, close it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006158 if (old_curwin != NULL)
6159 {
6160 need_hide = (curbufIsChanged() && curbuf->b_nwindows <= 1);
Bram Moolenaareb44a682017-08-03 22:44:55 +02006161 if (!need_hide || buf_hide(curbuf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006162 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006163#if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00006164 cleanup_T cs;
6165
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006166 // Reset the error/interrupt/exception state here so that
6167 // aborting() returns FALSE when closing a window.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00006168 enter_cleanup(&cs);
Bram Moolenaar4033c552017-09-16 20:54:51 +02006169#endif
6170#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00006171 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006172#endif
Bram Moolenaareb44a682017-08-03 22:44:55 +02006173 win_close(curwin, !need_hide && !buf_hide(curbuf));
Bram Moolenaarc0197e22004-09-13 20:26:32 +00006174
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006175#if defined(FEAT_EVAL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006176 // Restore the error/interrupt/exception state if not
6177 // discarded by a new aborting error, interrupt, or
6178 // uncaught exception.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00006179 leave_cleanup(&cs);
Bram Moolenaar4033c552017-09-16 20:54:51 +02006180#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006181 }
6182 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006183 }
6184 else if (readonlymode && curbuf->b_nwindows == 1)
6185 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006186 // When editing an already visited buffer, 'readonly' won't be set
6187 // but the previous value is kept. With ":view" and ":sview" we
6188 // want the file to be readonly, except when another window is
6189 // editing the same buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006190 curbuf->b_p_ro = TRUE;
6191 }
6192 readonlymode = n;
6193 }
6194 else
6195 {
6196 if (eap->do_ecmd_cmd != NULL)
6197 do_cmdline_cmd(eap->do_ecmd_cmd);
6198#ifdef FEAT_TITLE
6199 n = curwin->w_arg_idx_invalid;
6200#endif
6201 check_arg_idx(curwin);
6202#ifdef FEAT_TITLE
6203 if (n != curwin->w_arg_idx_invalid)
6204 maketitle();
6205#endif
6206 }
6207
Bram Moolenaar071d4272004-06-13 20:20:40 +00006208 /*
6209 * if ":split file" worked, set alternate file name in old window to new
6210 * file
6211 */
6212 if (old_curwin != NULL
6213 && *eap->arg != NUL
6214 && curwin != old_curwin
6215 && win_valid(old_curwin)
Bram Moolenaard4755bb2004-09-02 19:12:26 +00006216 && old_curwin->w_buffer != curbuf
6217 && !cmdmod.keepalt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006218 old_curwin->w_alt_fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006219
6220 ex_no_reprint = TRUE;
6221}
6222
6223#ifndef FEAT_GUI
6224/*
6225 * ":gui" and ":gvim" when there is no GUI.
6226 */
6227 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006228ex_nogui(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006229{
6230 eap->errmsg = e_nogvim;
6231}
6232#endif
6233
Bram Moolenaar4f974752019-02-17 17:44:42 +01006234#if defined(FEAT_GUI_MSWIN) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006235 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006236ex_tearoff(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006237{
6238 gui_make_tearoff(eap->arg);
6239}
6240#endif
6241
Bram Moolenaar29a2c082018-03-05 21:06:23 +01006242#if (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK) \
6243 || defined(FEAT_TERM_POPUP_MENU)) && defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006244 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006245ex_popup(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006246{
Bram Moolenaar29a2c082018-03-05 21:06:23 +01006247# if defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK)
6248 if (gui.in_use)
6249 gui_make_popup(eap->arg, eap->forceit);
6250# ifdef FEAT_TERM_POPUP_MENU
6251 else
6252# endif
6253# endif
6254# ifdef FEAT_TERM_POPUP_MENU
6255 pum_make_popup(eap->arg, eap->forceit);
6256# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006257}
6258#endif
6259
Bram Moolenaar071d4272004-06-13 20:20:40 +00006260 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006261ex_swapname(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006262{
6263 if (curbuf->b_ml.ml_mfp == NULL || curbuf->b_ml.ml_mfp->mf_fname == NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01006264 msg(_("No swap file"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006265 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01006266 msg((char *)curbuf->b_ml.ml_mfp->mf_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006267}
6268
6269/*
6270 * ":syncbind" forces all 'scrollbind' windows to have the same relative
6271 * offset.
6272 * (1998-11-02 16:21:01 R. Edward Ralston <eralston@computer.org>)
6273 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006274 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006275ex_syncbind(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006276{
Bram Moolenaar071d4272004-06-13 20:20:40 +00006277 win_T *wp;
Bram Moolenaardedd1b02013-12-14 13:06:17 +01006278 win_T *save_curwin = curwin;
6279 buf_T *save_curbuf = curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006280 long topline;
6281 long y;
6282 linenr_T old_linenr = curwin->w_cursor.lnum;
6283
6284 setpcmark();
6285
6286 /*
6287 * determine max topline
6288 */
6289 if (curwin->w_p_scb)
6290 {
6291 topline = curwin->w_topline;
Bram Moolenaar29323592016-07-24 22:04:11 +02006292 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006293 {
6294 if (wp->w_p_scb && wp->w_buffer)
6295 {
Bram Moolenaar375e3392019-01-31 18:26:10 +01006296 y = wp->w_buffer->b_ml.ml_line_count - get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006297 if (topline > y)
6298 topline = y;
6299 }
6300 }
6301 if (topline < 1)
6302 topline = 1;
6303 }
6304 else
6305 {
6306 topline = 1;
6307 }
6308
6309
6310 /*
Bram Moolenaardedd1b02013-12-14 13:06:17 +01006311 * Set all scrollbind windows to the same topline.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006312 */
Bram Moolenaar29323592016-07-24 22:04:11 +02006313 FOR_ALL_WINDOWS(curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006314 {
6315 if (curwin->w_p_scb)
6316 {
Bram Moolenaardedd1b02013-12-14 13:06:17 +01006317 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006318 y = topline - curwin->w_topline;
6319 if (y > 0)
6320 scrollup(y, TRUE);
6321 else
6322 scrolldown(-y, TRUE);
6323 curwin->w_scbind_pos = topline;
6324 redraw_later(VALID);
6325 cursor_correct();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006326 curwin->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006327 }
6328 }
Bram Moolenaardedd1b02013-12-14 13:06:17 +01006329 curwin = save_curwin;
6330 curbuf = save_curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006331 if (curwin->w_p_scb)
6332 {
6333 did_syncbind = TRUE;
6334 checkpcmark();
6335 if (old_linenr != curwin->w_cursor.lnum)
6336 {
6337 char_u ctrl_o[2];
6338
6339 ctrl_o[0] = Ctrl_O;
6340 ctrl_o[1] = 0;
6341 ins_typebuf(ctrl_o, REMAP_NONE, 0, TRUE, FALSE);
6342 }
6343 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006344}
6345
6346
6347 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006348ex_read(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006349{
Bram Moolenaardf177f62005-02-22 08:39:57 +00006350 int i;
6351 int empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
6352 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006353
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006354 if (eap->usefilter) // :r!cmd
Bram Moolenaar071d4272004-06-13 20:20:40 +00006355 do_bang(1, eap, FALSE, FALSE, TRUE);
6356 else
6357 {
6358 if (u_save(eap->line2, (linenr_T)(eap->line2 + 1)) == FAIL)
6359 return;
6360
6361#ifdef FEAT_BROWSE
6362 if (cmdmod.browse)
6363 {
6364 char_u *browseFile;
6365
Bram Moolenaar7171abe2004-10-11 10:06:20 +00006366 browseFile = do_browse(0, (char_u *)_("Append File"), eap->arg,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006367 NULL, NULL, NULL, curbuf);
6368 if (browseFile != NULL)
6369 {
6370 i = readfile(browseFile, NULL,
6371 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
6372 vim_free(browseFile);
6373 }
6374 else
6375 i = OK;
6376 }
6377 else
6378#endif
6379 if (*eap->arg == NUL)
6380 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006381 if (check_fname() == FAIL) // check for no file name
Bram Moolenaar071d4272004-06-13 20:20:40 +00006382 return;
6383 i = readfile(curbuf->b_ffname, curbuf->b_fname,
6384 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
6385 }
6386 else
6387 {
6388 if (vim_strchr(p_cpo, CPO_ALTREAD) != NULL)
6389 (void)setaltfname(eap->arg, eap->arg, (linenr_T)1);
6390 i = readfile(eap->arg, NULL,
6391 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
6392
6393 }
Bram Moolenaare13b9af2017-01-13 22:01:02 +01006394 if (i != OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006395 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006396#if defined(FEAT_EVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006397 if (!aborting())
6398#endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006399 semsg(_(e_notopen), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006400 }
6401 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00006402 {
6403 if (empty && exmode_active)
6404 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006405 // Delete the empty line that remains. Historically ex does
6406 // this but vi doesn't.
Bram Moolenaardf177f62005-02-22 08:39:57 +00006407 if (eap->line2 == 0)
6408 lnum = curbuf->b_ml.ml_line_count;
6409 else
6410 lnum = 1;
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00006411 if (*ml_get(lnum) == NUL && u_savedel(lnum, 1L) == OK)
Bram Moolenaardf177f62005-02-22 08:39:57 +00006412 {
6413 ml_delete(lnum, FALSE);
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00006414 if (curwin->w_cursor.lnum > 1
6415 && curwin->w_cursor.lnum >= lnum)
Bram Moolenaardf177f62005-02-22 08:39:57 +00006416 --curwin->w_cursor.lnum;
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00006417 deleted_lines_mark(lnum, 1L);
Bram Moolenaardf177f62005-02-22 08:39:57 +00006418 }
6419 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006420 redraw_curbuf_later(VALID);
Bram Moolenaardf177f62005-02-22 08:39:57 +00006421 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006422 }
6423}
6424
Bram Moolenaarea408852005-06-25 22:49:46 +00006425static char_u *prev_dir = NULL;
6426
6427#if defined(EXITFREE) || defined(PROTO)
6428 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006429free_cd_dir(void)
Bram Moolenaarea408852005-06-25 22:49:46 +00006430{
Bram Moolenaard23a8232018-02-10 18:45:26 +01006431 VIM_CLEAR(prev_dir);
6432 VIM_CLEAR(globaldir);
Bram Moolenaarea408852005-06-25 22:49:46 +00006433}
6434#endif
6435
Bram Moolenaarf4258302013-06-02 18:20:17 +02006436/*
6437 * Deal with the side effects of changing the current directory.
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006438 * When 'scope' is CDSCOPE_TABPAGE then this was after an ":tcd" command.
6439 * When 'scope' is CDSCOPE_WINDOW then this was after an ":lcd" command.
Bram Moolenaarf4258302013-06-02 18:20:17 +02006440 */
6441 void
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006442post_chdir(cdscope_T scope)
Bram Moolenaarf4258302013-06-02 18:20:17 +02006443{
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006444 if (scope != CDSCOPE_WINDOW)
Bram Moolenaar00aa0692019-04-27 20:37:57 +02006445 // Clear tab local directory for both :cd and :tcd
6446 VIM_CLEAR(curtab->tp_localdir);
Bram Moolenaard23a8232018-02-10 18:45:26 +01006447 VIM_CLEAR(curwin->w_localdir);
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006448 if (scope != CDSCOPE_GLOBAL)
Bram Moolenaarf4258302013-06-02 18:20:17 +02006449 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006450 // If still in global directory, need to remember current
6451 // directory as global directory.
Bram Moolenaarf4258302013-06-02 18:20:17 +02006452 if (globaldir == NULL && prev_dir != NULL)
6453 globaldir = vim_strsave(prev_dir);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006454 // Remember this local directory for the window.
Bram Moolenaarf4258302013-06-02 18:20:17 +02006455 if (mch_dirname(NameBuff, MAXPATHL) == OK)
Bram Moolenaar00aa0692019-04-27 20:37:57 +02006456 {
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006457 if (scope == CDSCOPE_TABPAGE)
Bram Moolenaar00aa0692019-04-27 20:37:57 +02006458 curtab->tp_localdir = vim_strsave(NameBuff);
6459 else
6460 curwin->w_localdir = vim_strsave(NameBuff);
6461 }
Bram Moolenaarf4258302013-06-02 18:20:17 +02006462 }
6463 else
6464 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006465 // We are now in the global directory, no need to remember its
6466 // name.
Bram Moolenaard23a8232018-02-10 18:45:26 +01006467 VIM_CLEAR(globaldir);
Bram Moolenaarf4258302013-06-02 18:20:17 +02006468 }
6469
6470 shorten_fnames(TRUE);
6471}
6472
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006473/*
6474 * Change directory function used by :cd/:tcd/:lcd Ex commands and the
6475 * chdir() function. If 'winlocaldir' is TRUE, then changes the window-local
6476 * directory. If 'tablocaldir' is TRUE, then changes the tab-local directory.
6477 * Otherwise changes the global directory.
6478 * Returns TRUE if the directory is successfully changed.
6479 */
6480 int
6481changedir_func(
6482 char_u *new_dir,
6483 int forceit,
6484 cdscope_T scope)
6485{
6486 char_u *tofree;
6487 int dir_differs;
6488 int retval = FALSE;
6489
6490 if (allbuf_locked())
6491 return FALSE;
6492
6493 if (vim_strchr(p_cpo, CPO_CHDIR) != NULL && curbufIsChanged() && !forceit)
6494 {
6495 emsg(_("E747: Cannot change directory, buffer is modified (add ! to override)"));
6496 return FALSE;
6497 }
6498
6499 // ":cd -": Change to previous directory
6500 if (STRCMP(new_dir, "-") == 0)
6501 {
6502 if (prev_dir == NULL)
6503 {
6504 emsg(_("E186: No previous directory"));
6505 return FALSE;
6506 }
6507 new_dir = prev_dir;
6508 }
6509
6510 // Save current directory for next ":cd -"
6511 tofree = prev_dir;
6512 if (mch_dirname(NameBuff, MAXPATHL) == OK)
6513 prev_dir = vim_strsave(NameBuff);
6514 else
6515 prev_dir = NULL;
6516
6517#if defined(UNIX) || defined(VMS)
6518 // for UNIX ":cd" means: go to home directory
6519 if (*new_dir == NUL)
6520 {
6521 // use NameBuff for home directory name
6522# ifdef VMS
6523 char_u *p;
6524
6525 p = mch_getenv((char_u *)"SYS$LOGIN");
6526 if (p == NULL || *p == NUL) // empty is the same as not set
6527 NameBuff[0] = NUL;
6528 else
6529 vim_strncpy(NameBuff, p, MAXPATHL - 1);
6530# else
6531 expand_env((char_u *)"$HOME", NameBuff, MAXPATHL);
6532# endif
6533 new_dir = NameBuff;
6534 }
6535#endif
6536 dir_differs = new_dir == NULL || prev_dir == NULL
6537 || pathcmp((char *)prev_dir, (char *)new_dir, -1) != 0;
6538 if (new_dir == NULL || (dir_differs && vim_chdir(new_dir)))
6539 emsg(_(e_failed));
6540 else
6541 {
6542 char_u *acmd_fname;
6543
6544 post_chdir(scope);
6545
6546 if (dir_differs)
6547 {
6548 if (scope == CDSCOPE_WINDOW)
6549 acmd_fname = (char_u *)"window";
6550 else if (scope == CDSCOPE_TABPAGE)
6551 acmd_fname = (char_u *)"tabpage";
6552 else
6553 acmd_fname = (char_u *)"global";
6554 apply_autocmds(EVENT_DIRCHANGED, acmd_fname, new_dir, FALSE,
6555 curbuf);
6556 }
6557 retval = TRUE;
6558 }
6559 vim_free(tofree);
6560
6561 return retval;
6562}
Bram Moolenaarea408852005-06-25 22:49:46 +00006563
Bram Moolenaar071d4272004-06-13 20:20:40 +00006564/*
Bram Moolenaar00aa0692019-04-27 20:37:57 +02006565 * ":cd", ":tcd", ":lcd", ":chdir" ":tchdir" and ":lchdir".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006566 */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00006567 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006568ex_cd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006569{
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01006570 char_u *new_dir;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006571
6572 new_dir = eap->arg;
6573#if !defined(UNIX) && !defined(VMS)
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006574 // for non-UNIX ":cd" means: print current directory
Bram Moolenaar071d4272004-06-13 20:20:40 +00006575 if (*new_dir == NUL)
6576 ex_pwd(NULL);
6577 else
6578#endif
6579 {
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006580 cdscope_T scope = CDSCOPE_GLOBAL;
6581
6582 if (eap->cmdidx == CMD_lcd || eap->cmdidx == CMD_lchdir)
6583 scope = CDSCOPE_WINDOW;
6584 else if (eap->cmdidx == CMD_tcd || eap->cmdidx == CMD_tchdir)
6585 scope = CDSCOPE_TABPAGE;
6586
6587 if (changedir_func(new_dir, eap->forceit, scope))
Bram Moolenaardf177f62005-02-22 08:39:57 +00006588 {
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006589 // Echo the new current directory if the command was typed.
Bram Moolenaarfcfbc672009-07-09 18:13:49 +00006590 if (KeyTyped || p_verbose >= 5)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006591 ex_pwd(eap);
6592 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006593 }
6594}
6595
6596/*
6597 * ":pwd".
6598 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006599 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006600ex_pwd(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006601{
6602 if (mch_dirname(NameBuff, MAXPATHL) == OK)
6603 {
6604#ifdef BACKSLASH_IN_FILENAME
6605 slash_adjust(NameBuff);
6606#endif
Bram Moolenaar32526b32019-01-19 17:43:09 +01006607 msg((char *)NameBuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006608 }
6609 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006610 emsg(_("E187: Unknown"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006611}
6612
6613/*
6614 * ":=".
6615 */
6616 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006617ex_equal(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006618{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006619 smsg("%ld", (long)eap->line2);
Bram Moolenaardf177f62005-02-22 08:39:57 +00006620 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006621}
6622
6623 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006624ex_sleep(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006625{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006626 int n;
6627 long len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006628
6629 if (cursor_valid())
6630 {
6631 n = W_WINROW(curwin) + curwin->w_wrow - msg_scrolled;
6632 if (n >= 0)
Bram Moolenaar53f81742017-09-22 14:35:51 +02006633 windgoto((int)n, curwin->w_wincol + curwin->w_wcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006634 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006635
6636 len = eap->line2;
6637 switch (*eap->arg)
6638 {
6639 case 'm': break;
6640 case NUL: len *= 1000L; break;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006641 default: semsg(_(e_invarg2), eap->arg); return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006642 }
6643 do_sleep(len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006644}
6645
6646/*
6647 * Sleep for "msec" milliseconds, but keep checking for a CTRL-C every second.
6648 */
6649 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006650do_sleep(long msec)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006651{
Bram Moolenaar52953192019-08-16 10:27:13 +02006652 long done = 0;
Bram Moolenaar975b5272016-03-15 23:10:59 +01006653 long wait_now;
Bram Moolenaar52953192019-08-16 10:27:13 +02006654# ifdef ELAPSED_FUNC
6655 elapsed_T start_tv;
6656
6657 // Remember at what time we started, so that we know how much longer we
6658 // should wait after waiting for a bit.
6659 ELAPSED_INIT(start_tv);
6660# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006661
6662 cursor_on();
Bram Moolenaarf45d7472018-09-30 18:22:26 +02006663 out_flush_cursor(FALSE, FALSE);
Bram Moolenaar52953192019-08-16 10:27:13 +02006664 while (!got_int && done < msec)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006665 {
Bram Moolenaar975b5272016-03-15 23:10:59 +01006666 wait_now = msec - done > 1000L ? 1000L : msec - done;
6667#ifdef FEAT_TIMERS
6668 {
6669 long due_time = check_due_timer();
6670
6671 if (due_time > 0 && due_time < wait_now)
6672 wait_now = due_time;
6673 }
6674#endif
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02006675#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar28e67e02019-08-15 23:05:49 +02006676 if (has_any_channel() && wait_now > 20L)
6677 wait_now = 20L;
6678#endif
6679#ifdef FEAT_SOUND
6680 if (has_any_sound_callback() && wait_now > 20L)
6681 wait_now = 20L;
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02006682#endif
Bram Moolenaar975b5272016-03-15 23:10:59 +01006683 ui_delay(wait_now, TRUE);
Bram Moolenaar52953192019-08-16 10:27:13 +02006684
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02006685#ifdef FEAT_JOB_CHANNEL
6686 if (has_any_channel())
6687 ui_breakcheck_force(TRUE);
6688 else
6689#endif
6690 ui_breakcheck();
Bram Moolenaar93c88e02015-09-15 14:12:05 +02006691#ifdef MESSAGE_QUEUE
Bram Moolenaar52953192019-08-16 10:27:13 +02006692 // Process the netbeans and clientserver messages that may have been
6693 // received in the call to ui_breakcheck() when the GUI is in use. This
6694 // may occur when running a test case.
Bram Moolenaar93c88e02015-09-15 14:12:05 +02006695 parse_queued_messages();
Bram Moolenaare3cc6d42011-10-20 21:58:34 +02006696#endif
Bram Moolenaar52953192019-08-16 10:27:13 +02006697
6698# ifdef ELAPSED_FUNC
6699 // actual time passed
6700 done = ELAPSED_FUNC(start_tv);
6701# else
6702 // guestimate time passed (will actually be more)
6703 done += wait_now;
6704# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006705 }
Bram Moolenaar1ebff3d2018-07-07 16:18:13 +02006706
6707 // If CTRL-C was typed to interrupt the sleep, drop the CTRL-C from the
6708 // input buffer, otherwise a following call to input() fails.
6709 if (got_int)
6710 (void)vpeekc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006711}
6712
Bram Moolenaar071d4272004-06-13 20:20:40 +00006713/*
6714 * ":winsize" command (obsolete).
6715 */
6716 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006717ex_winsize(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006718{
6719 int w, h;
6720 char_u *arg = eap->arg;
6721 char_u *p;
6722
6723 w = getdigits(&arg);
6724 arg = skipwhite(arg);
6725 p = arg;
6726 h = getdigits(&arg);
6727 if (*p != NUL && *arg == NUL)
6728 set_shellsize(w, h, TRUE);
6729 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006730 emsg(_("E465: :winsize requires two number arguments"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006731}
6732
Bram Moolenaar071d4272004-06-13 20:20:40 +00006733 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006734ex_wincmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006735{
6736 int xchar = NUL;
6737 char_u *p;
6738
6739 if (*eap->arg == 'g' || *eap->arg == Ctrl_G)
6740 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006741 // CTRL-W g and CTRL-W CTRL-G have an extra command character
Bram Moolenaar071d4272004-06-13 20:20:40 +00006742 if (eap->arg[1] == NUL)
6743 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006744 emsg(_(e_invarg));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006745 return;
6746 }
6747 xchar = eap->arg[1];
6748 p = eap->arg + 2;
6749 }
6750 else
6751 p = eap->arg + 1;
6752
6753 eap->nextcmd = check_nextcmd(p);
6754 p = skipwhite(p);
6755 if (*p != NUL && *p != '"' && eap->nextcmd == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006756 emsg(_(e_invarg));
Bram Moolenaar12bde492011-06-13 01:19:56 +02006757 else if (!eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006758 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006759 // Pass flags on for ":vertical wincmd ]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006760 postponed_split_flags = cmdmod.split;
Bram Moolenaard326ce82007-03-11 14:48:29 +00006761 postponed_split_tab = cmdmod.tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006762 do_window(*eap->arg, eap->addr_count > 0 ? eap->line2 : 0L, xchar);
6763 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00006764 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006765 }
6766}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006767
Bram Moolenaar843ee412004-06-30 16:16:41 +00006768#if defined(FEAT_GUI) || defined(UNIX) || defined(VMS) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006769/*
6770 * ":winpos".
6771 */
6772 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006773ex_winpos(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006774{
6775 int x, y;
6776 char_u *arg = eap->arg;
6777 char_u *p;
6778
6779 if (*arg == NUL)
6780 {
Bram Moolenaar843ee412004-06-30 16:16:41 +00006781# if defined(FEAT_GUI) || defined(MSWIN)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02006782# ifdef VIMDLL
6783 if (gui.in_use ? gui_mch_get_winpos(&x, &y) != FAIL :
6784 mch_get_winpos(&x, &y) != FAIL)
6785# elif defined(FEAT_GUI)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006786 if (gui.in_use && gui_mch_get_winpos(&x, &y) != FAIL)
Bram Moolenaar843ee412004-06-30 16:16:41 +00006787# else
6788 if (mch_get_winpos(&x, &y) != FAIL)
6789# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006790 {
6791 sprintf((char *)IObuff, _("Window position: X %d, Y %d"), x, y);
Bram Moolenaar32526b32019-01-19 17:43:09 +01006792 msg((char *)IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006793 }
6794 else
6795# endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006796 emsg(_("E188: Obtaining window position not implemented for this platform"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006797 }
6798 else
6799 {
6800 x = getdigits(&arg);
6801 arg = skipwhite(arg);
6802 p = arg;
6803 y = getdigits(&arg);
6804 if (*p == NUL || *arg != NUL)
6805 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006806 emsg(_("E466: :winpos requires two number arguments"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006807 return;
6808 }
6809# ifdef FEAT_GUI
6810 if (gui.in_use)
6811 gui_mch_set_winpos(x, y);
6812 else if (gui.starting)
6813 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006814 // Remember the coordinates for when the window is opened.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006815 gui_win_x = x;
6816 gui_win_y = y;
6817 }
Bram Moolenaarafde13b2019-04-28 19:46:49 +02006818# if defined(HAVE_TGETENT) || defined(VIMDLL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006819 else
6820# endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02006821# endif
6822# if defined(MSWIN) && (!defined(FEAT_GUI) || defined(VIMDLL))
Bram Moolenaar843ee412004-06-30 16:16:41 +00006823 mch_set_winpos(x, y);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006824# endif
6825# ifdef HAVE_TGETENT
6826 if (*T_CWP)
6827 term_set_winpos(x, y);
6828# endif
6829 }
6830}
6831#endif
6832
6833/*
6834 * Handle command that work like operators: ":delete", ":yank", ":>" and ":<".
6835 */
6836 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006837ex_operators(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006838{
6839 oparg_T oa;
6840
6841 clear_oparg(&oa);
6842 oa.regname = eap->regname;
6843 oa.start.lnum = eap->line1;
6844 oa.end.lnum = eap->line2;
6845 oa.line_count = eap->line2 - eap->line1 + 1;
6846 oa.motion_type = MLINE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006847 virtual_op = FALSE;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006848 if (eap->cmdidx != CMD_yank) // position cursor for undo
Bram Moolenaar071d4272004-06-13 20:20:40 +00006849 {
6850 setpcmark();
6851 curwin->w_cursor.lnum = eap->line1;
6852 beginline(BL_SOL | BL_FIX);
6853 }
6854
Bram Moolenaard07c6e12013-11-21 14:21:40 +01006855 if (VIsual_active)
6856 end_visual_mode();
Bram Moolenaard07c6e12013-11-21 14:21:40 +01006857
Bram Moolenaar071d4272004-06-13 20:20:40 +00006858 switch (eap->cmdidx)
6859 {
6860 case CMD_delete:
6861 oa.op_type = OP_DELETE;
6862 op_delete(&oa);
6863 break;
6864
6865 case CMD_yank:
6866 oa.op_type = OP_YANK;
6867 (void)op_yank(&oa, FALSE, TRUE);
6868 break;
6869
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006870 default: // CMD_rshift or CMD_lshift
Bram Moolenaar6e707362013-06-14 19:15:58 +02006871 if (
Bram Moolenaar071d4272004-06-13 20:20:40 +00006872#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6e707362013-06-14 19:15:58 +02006873 (eap->cmdidx == CMD_rshift) ^ curwin->w_p_rl
6874#else
6875 eap->cmdidx == CMD_rshift
Bram Moolenaar071d4272004-06-13 20:20:40 +00006876#endif
Bram Moolenaar6e707362013-06-14 19:15:58 +02006877 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006878 oa.op_type = OP_RSHIFT;
6879 else
6880 oa.op_type = OP_LSHIFT;
6881 op_shift(&oa, FALSE, eap->amount);
6882 break;
6883 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006884 virtual_op = MAYBE;
Bram Moolenaardf177f62005-02-22 08:39:57 +00006885 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006886}
6887
6888/*
6889 * ":put".
6890 */
6891 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006892ex_put(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006893{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006894 // ":0put" works like ":1put!".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006895 if (eap->line2 == 0)
6896 {
6897 eap->line2 = 1;
6898 eap->forceit = TRUE;
6899 }
6900 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaardf177f62005-02-22 08:39:57 +00006901 do_put(eap->regname, eap->forceit ? BACKWARD : FORWARD, 1L,
6902 PUT_LINE|PUT_CURSLINE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006903}
6904
6905/*
6906 * Handle ":copy" and ":move".
6907 */
6908 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006909ex_copymove(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006910{
6911 long n;
6912
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02006913 n = get_address(eap, &eap->arg, eap->addr_type, FALSE, FALSE, FALSE, 1);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006914 if (eap->arg == NULL) // error detected
Bram Moolenaar071d4272004-06-13 20:20:40 +00006915 {
6916 eap->nextcmd = NULL;
6917 return;
6918 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00006919 get_flags(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006920
6921 /*
6922 * move or copy lines from 'eap->line1'-'eap->line2' to below line 'n'
6923 */
6924 if (n == MAXLNUM || n < 0 || n > curbuf->b_ml.ml_line_count)
6925 {
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02006926 emsg(_(e_invrange));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006927 return;
6928 }
6929
6930 if (eap->cmdidx == CMD_move)
6931 {
6932 if (do_move(eap->line1, eap->line2, n) == FAIL)
6933 return;
6934 }
6935 else
6936 ex_copy(eap->line1, eap->line2, n);
6937 u_clearline();
6938 beginline(BL_SOL | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00006939 ex_may_print(eap);
6940}
6941
6942/*
6943 * Print the current line if flags were given to the Ex command.
6944 */
Bram Moolenaarfd3fe982014-04-01 17:49:44 +02006945 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006946ex_may_print(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00006947{
6948 if (eap->flags != 0)
6949 {
6950 print_line(curwin->w_cursor.lnum, (eap->flags & EXFLAG_NR),
6951 (eap->flags & EXFLAG_LIST));
6952 ex_no_reprint = TRUE;
6953 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006954}
6955
6956/*
6957 * ":smagic" and ":snomagic".
6958 */
6959 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006960ex_submagic(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006961{
6962 int magic_save = p_magic;
6963
6964 p_magic = (eap->cmdidx == CMD_smagic);
6965 do_sub(eap);
6966 p_magic = magic_save;
6967}
6968
6969/*
6970 * ":join".
6971 */
6972 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006973ex_join(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006974{
6975 curwin->w_cursor.lnum = eap->line1;
6976 if (eap->line1 == eap->line2)
6977 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006978 if (eap->addr_count >= 2) // :2,2join does nothing
Bram Moolenaar071d4272004-06-13 20:20:40 +00006979 return;
6980 if (eap->line2 == curbuf->b_ml.ml_line_count)
6981 {
6982 beep_flush();
6983 return;
6984 }
6985 ++eap->line2;
6986 }
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02006987 (void)do_join(eap->line2 - eap->line1 + 1, !eap->forceit, TRUE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006988 beginline(BL_WHITE | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00006989 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006990}
6991
6992/*
6993 * ":[addr]@r" or ":[addr]*r": execute register
6994 */
6995 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006996ex_at(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006997{
6998 int c;
Bram Moolenaar60462872009-11-03 11:40:19 +00006999 int prev_len = typebuf.tb_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007000
7001 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaar4930a762016-09-11 14:39:53 +02007002 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007003
7004#ifdef USE_ON_FLY_SCROLL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007005 dont_scroll = TRUE; // disallow scrolling here
Bram Moolenaar071d4272004-06-13 20:20:40 +00007006#endif
7007
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007008 // get the register name. No name means to use the previous one
Bram Moolenaar071d4272004-06-13 20:20:40 +00007009 c = *eap->arg;
7010 if (c == NUL || (c == '*' && *eap->cmd == '*'))
7011 c = '@';
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007012 // Put the register in the typeahead buffer with the "silent" flag.
Bram Moolenaard333d1e2006-11-07 17:43:47 +00007013 if (do_execreg(c, TRUE, vim_strchr(p_cpo, CPO_EXECBUF) != NULL, TRUE)
7014 == FAIL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007015 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007016 beep_flush();
Bram Moolenaardf177f62005-02-22 08:39:57 +00007017 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007018 else
7019 {
7020 int save_efr = exec_from_reg;
7021
7022 exec_from_reg = TRUE;
7023
7024 /*
7025 * Execute from the typeahead buffer.
Bram Moolenaar60462872009-11-03 11:40:19 +00007026 * Continue until the stuff buffer is empty and all added characters
7027 * have been consumed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007028 */
Bram Moolenaar60462872009-11-03 11:40:19 +00007029 while (!stuff_empty() || typebuf.tb_len > prev_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007030 (void)do_cmdline(NULL, getexline, NULL, DOCMD_NOWAIT|DOCMD_VERBOSE);
7031
7032 exec_from_reg = save_efr;
7033 }
7034}
7035
7036/*
7037 * ":!".
7038 */
7039 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007040ex_bang(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007041{
7042 do_bang(eap->addr_count, eap, eap->forceit, TRUE, TRUE);
7043}
7044
7045/*
7046 * ":undo".
7047 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007048 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +01007049ex_undo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007050{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007051 if (eap->addr_count == 1) // :undo 123
Bram Moolenaar730cde92010-06-27 05:18:54 +02007052 undo_time(eap->line2, FALSE, FALSE, TRUE);
Bram Moolenaard3667a22006-03-16 21:35:52 +00007053 else
7054 u_undo(1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007055}
7056
Bram Moolenaar55debbe2010-05-23 23:34:36 +02007057#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar6df6f472010-07-18 18:04:50 +02007058 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007059ex_wundo(exarg_T *eap)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02007060{
7061 char_u hash[UNDO_HASH_SIZE];
7062
7063 u_compute_hash(hash);
7064 u_write_undo(eap->arg, eap->forceit, curbuf, hash);
7065}
7066
Bram Moolenaar6df6f472010-07-18 18:04:50 +02007067 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007068ex_rundo(exarg_T *eap)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02007069{
7070 char_u hash[UNDO_HASH_SIZE];
7071
7072 u_compute_hash(hash);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02007073 u_read_undo(eap->arg, hash, NULL);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02007074}
7075#endif
7076
Bram Moolenaar071d4272004-06-13 20:20:40 +00007077/*
7078 * ":redo".
7079 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007080 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007081ex_redo(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007082{
7083 u_redo(1);
7084}
7085
7086/*
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007087 * ":earlier" and ":later".
7088 */
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007089 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007090ex_later(exarg_T *eap)
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007091{
7092 long count = 0;
7093 int sec = FALSE;
Bram Moolenaar730cde92010-06-27 05:18:54 +02007094 int file = FALSE;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007095 char_u *p = eap->arg;
7096
7097 if (*p == NUL)
7098 count = 1;
7099 else if (isdigit(*p))
7100 {
7101 count = getdigits(&p);
7102 switch (*p)
7103 {
7104 case 's': ++p; sec = TRUE; break;
7105 case 'm': ++p; sec = TRUE; count *= 60; break;
7106 case 'h': ++p; sec = TRUE; count *= 60 * 60; break;
Bram Moolenaar730cde92010-06-27 05:18:54 +02007107 case 'd': ++p; sec = TRUE; count *= 24 * 60 * 60; break;
7108 case 'f': ++p; file = TRUE; break;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007109 }
7110 }
7111
7112 if (*p != NUL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007113 semsg(_(e_invarg2), eap->arg);
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007114 else
Bram Moolenaar730cde92010-06-27 05:18:54 +02007115 undo_time(eap->cmdidx == CMD_earlier ? -count : count,
7116 sec, file, FALSE);
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007117}
7118
7119/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007120 * ":redir": start/stop redirection.
7121 */
7122 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007123ex_redir(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007124{
7125 char *mode;
7126 char_u *fname;
Bram Moolenaarca472992005-01-21 11:46:23 +00007127 char_u *arg = eap->arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007128
Bram Moolenaarba768492016-07-08 15:32:54 +02007129#ifdef FEAT_EVAL
Bram Moolenaar79815f12016-07-09 17:07:29 +02007130 if (redir_execute)
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02007131 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007132 emsg(_("E930: Cannot use :redir inside execute()"));
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02007133 return;
7134 }
Bram Moolenaarba768492016-07-08 15:32:54 +02007135#endif
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02007136
Bram Moolenaar071d4272004-06-13 20:20:40 +00007137 if (STRICMP(eap->arg, "END") == 0)
7138 close_redir();
7139 else
7140 {
Bram Moolenaarca472992005-01-21 11:46:23 +00007141 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00007142 {
Bram Moolenaarca472992005-01-21 11:46:23 +00007143 ++arg;
7144 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00007145 {
Bram Moolenaarca472992005-01-21 11:46:23 +00007146 ++arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007147 mode = "a";
7148 }
7149 else
7150 mode = "w";
Bram Moolenaarca472992005-01-21 11:46:23 +00007151 arg = skipwhite(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007152
7153 close_redir();
7154
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007155 // Expand environment variables and "~/".
Bram Moolenaarca472992005-01-21 11:46:23 +00007156 fname = expand_env_save(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007157 if (fname == NULL)
7158 return;
7159#ifdef FEAT_BROWSE
7160 if (cmdmod.browse)
7161 {
7162 char_u *browseFile;
7163
Bram Moolenaar7171abe2004-10-11 10:06:20 +00007164 browseFile = do_browse(BROWSE_SAVE,
7165 (char_u *)_("Save Redirection"),
Bram Moolenaarc36651b2018-04-29 12:22:56 +02007166 fname, NULL, NULL,
7167 (char_u *)_(BROWSE_FILTER_ALL_FILES), curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007168 if (browseFile == NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007169 return; // operation cancelled
Bram Moolenaar071d4272004-06-13 20:20:40 +00007170 vim_free(fname);
7171 fname = browseFile;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007172 eap->forceit = TRUE; // since dialog already asked
Bram Moolenaar071d4272004-06-13 20:20:40 +00007173 }
7174#endif
7175
7176 redir_fd = open_exfile(fname, eap->forceit, mode);
7177 vim_free(fname);
7178 }
7179#ifdef FEAT_EVAL
Bram Moolenaarca472992005-01-21 11:46:23 +00007180 else if (*arg == '@')
Bram Moolenaar071d4272004-06-13 20:20:40 +00007181 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007182 // redirect to a register a-z (resp. A-Z for appending)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007183 close_redir();
Bram Moolenaarca472992005-01-21 11:46:23 +00007184 ++arg;
7185 if (ASCII_ISALPHA(*arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007186# ifdef FEAT_CLIPBOARD
Bram Moolenaarca472992005-01-21 11:46:23 +00007187 || *arg == '*'
Bram Moolenaar9a51c6e2006-11-14 19:25:02 +00007188 || *arg == '+'
Bram Moolenaar071d4272004-06-13 20:20:40 +00007189# endif
Bram Moolenaarca472992005-01-21 11:46:23 +00007190 || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00007191 {
Bram Moolenaarca472992005-01-21 11:46:23 +00007192 redir_reg = *arg++;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007193 if (*arg == '>' && arg[1] == '>') // append
Bram Moolenaard9d30582005-05-18 22:10:28 +00007194 arg += 2;
Bram Moolenaarc188b882007-10-19 14:20:54 +00007195 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007196 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007197 // Can use both "@a" and "@a>".
Bram Moolenaar2c29bee2005-06-01 21:46:07 +00007198 if (*arg == '>')
7199 arg++;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007200 // Make register empty when not using @A-@Z and the
7201 // command is valid.
Bram Moolenaarc188b882007-10-19 14:20:54 +00007202 if (*arg == NUL && !isupper(redir_reg))
7203 write_reg_contents(redir_reg, (char_u *)"", -1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007204 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00007205 }
7206 if (*arg != NUL)
7207 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00007208 redir_reg = 0;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007209 semsg(_(e_invarg2), eap->arg);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007210 }
7211 }
7212 else if (*arg == '=' && arg[1] == '>')
7213 {
7214 int append;
7215
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007216 // redirect to a variable
Bram Moolenaardf177f62005-02-22 08:39:57 +00007217 close_redir();
7218 arg += 2;
7219
7220 if (*arg == '>')
7221 {
7222 ++arg;
7223 append = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007224 }
7225 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00007226 append = FALSE;
7227
7228 if (var_redir_start(skipwhite(arg), append) == OK)
7229 redir_vname = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007230 }
7231#endif
7232
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007233 // TODO: redirect to a buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00007234
Bram Moolenaar071d4272004-06-13 20:20:40 +00007235 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007236 semsg(_(e_invarg2), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007237 }
Bram Moolenaar29b2d262006-09-10 19:07:28 +00007238
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007239 // Make sure redirection is not off. Can happen for cmdline completion
7240 // that indirectly invokes a command to catch its output.
Bram Moolenaar29b2d262006-09-10 19:07:28 +00007241 if (redir_fd != NULL
7242#ifdef FEAT_EVAL
7243 || redir_reg || redir_vname
7244#endif
7245 )
7246 redir_off = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007247}
7248
7249/*
7250 * ":redraw": force redraw
7251 */
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01007252 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007253ex_redraw(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007254{
7255 int r = RedrawingDisabled;
7256 int p = p_lz;
7257
7258 RedrawingDisabled = 0;
7259 p_lz = FALSE;
Bram Moolenaarabc39ab2017-03-01 18:04:05 +01007260 validate_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007261 update_topline();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007262 update_screen(eap->forceit ? CLEAR : VIsual_active ? INVERTED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007263#ifdef FEAT_TITLE
7264 if (need_maketitle)
7265 maketitle();
7266#endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007267#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
7268# ifdef VIMDLL
7269 if (!gui.in_use)
7270# endif
7271 resize_console_buf();
Bram Moolenaar78d21da2019-02-17 15:00:52 +01007272#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007273 RedrawingDisabled = r;
7274 p_lz = p;
7275
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007276 // Reset msg_didout, so that a message that's there is overwritten.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007277 msg_didout = FALSE;
7278 msg_col = 0;
7279
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007280 // No need to wait after an intentional redraw.
Bram Moolenaar56667a52013-07-17 11:54:28 +02007281 need_wait_return = FALSE;
7282
Bram Moolenaar071d4272004-06-13 20:20:40 +00007283 out_flush();
7284}
7285
7286/*
7287 * ":redrawstatus": force redraw of status line(s)
7288 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007289 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007290ex_redrawstatus(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007291{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007292 int r = RedrawingDisabled;
7293 int p = p_lz;
7294
7295 RedrawingDisabled = 0;
7296 p_lz = FALSE;
7297 if (eap->forceit)
7298 status_redraw_all();
7299 else
7300 status_redraw_curbuf();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007301 update_screen(VIsual_active ? INVERTED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007302 RedrawingDisabled = r;
7303 p_lz = p;
7304 out_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007305}
7306
Bram Moolenaare12bab32019-01-08 22:02:56 +01007307/*
7308 * ":redrawtabline": force redraw of the tabline
7309 */
7310 static void
7311ex_redrawtabline(exarg_T *eap UNUSED)
7312{
7313 int r = RedrawingDisabled;
7314 int p = p_lz;
7315
7316 RedrawingDisabled = 0;
7317 p_lz = FALSE;
7318
7319 draw_tabline();
7320
7321 RedrawingDisabled = r;
7322 p_lz = p;
7323 out_flush();
7324}
7325
Bram Moolenaar071d4272004-06-13 20:20:40 +00007326 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007327close_redir(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007328{
7329 if (redir_fd != NULL)
7330 {
7331 fclose(redir_fd);
7332 redir_fd = NULL;
7333 }
7334#ifdef FEAT_EVAL
7335 redir_reg = 0;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007336 if (redir_vname)
7337 {
7338 var_redir_stop();
7339 redir_vname = 0;
7340 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007341#endif
7342}
7343
Bram Moolenaarf96ae0b2019-07-28 15:21:55 +02007344#if (defined(FEAT_SESSION) || defined(FEAT_EVAL)) || defined(PROTO)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007345 int
Bram Moolenaarbd67aac2019-09-21 23:09:04 +02007346vim_mkdir_emsg(char_u *name, int prot UNUSED)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007347{
7348 if (vim_mkdir(name, prot) != 0)
7349 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007350 semsg(_("E739: Cannot create directory: %s"), name);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007351 return FAIL;
7352 }
7353 return OK;
7354}
7355#endif
7356
Bram Moolenaar071d4272004-06-13 20:20:40 +00007357/*
7358 * Open a file for writing for an Ex command, with some checks.
7359 * Return file descriptor, or NULL on failure.
7360 */
7361 FILE *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007362open_exfile(
7363 char_u *fname,
7364 int forceit,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007365 char *mode) // "w" for create new file or "a" for append
Bram Moolenaar071d4272004-06-13 20:20:40 +00007366{
7367 FILE *fd;
7368
7369#ifdef UNIX
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007370 // with Unix it is possible to open a directory
Bram Moolenaar071d4272004-06-13 20:20:40 +00007371 if (mch_isdir(fname))
7372 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007373 semsg(_(e_isadir2), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007374 return NULL;
7375 }
7376#endif
7377 if (!forceit && *mode != 'a' && vim_fexists(fname))
7378 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007379 semsg(_("E189: \"%s\" exists (add ! to override)"), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007380 return NULL;
7381 }
7382
7383 if ((fd = mch_fopen((char *)fname, mode)) == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007384 semsg(_("E190: Cannot open \"%s\" for writing"), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007385
7386 return fd;
7387}
7388
7389/*
7390 * ":mark" and ":k".
7391 */
7392 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007393ex_mark(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007394{
7395 pos_T pos;
7396
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007397 if (*eap->arg == NUL) // No argument?
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007398 emsg(_(e_argreq));
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007399 else if (eap->arg[1] != NUL) // more than one character?
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007400 emsg(_(e_trailing));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007401 else
7402 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007403 pos = curwin->w_cursor; // save curwin->w_cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00007404 curwin->w_cursor.lnum = eap->line2;
7405 beginline(BL_WHITE | BL_FIX);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007406 if (setmark(*eap->arg) == FAIL) // set mark
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007407 emsg(_("E191: Argument must be a letter or forward/backward quote"));
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007408 curwin->w_cursor = pos; // restore curwin->w_cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00007409 }
7410}
7411
7412/*
7413 * Update w_topline, w_leftcol and the cursor position.
7414 */
7415 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007416update_topline_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007417{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007418 check_cursor(); // put cursor on valid line
Bram Moolenaar071d4272004-06-13 20:20:40 +00007419 update_topline();
7420 if (!curwin->w_p_wrap)
7421 validate_cursor();
7422 update_curswant();
7423}
7424
Bram Moolenaar071d4272004-06-13 20:20:40 +00007425/*
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007426 * Save the current State and go to Normal mode.
7427 * Return TRUE if the typeahead could be saved.
7428 */
7429 int
7430save_current_state(save_state_T *sst)
7431{
7432 sst->save_msg_scroll = msg_scroll;
7433 sst->save_restart_edit = restart_edit;
7434 sst->save_msg_didout = msg_didout;
7435 sst->save_State = State;
7436 sst->save_insertmode = p_im;
7437 sst->save_finish_op = finish_op;
7438 sst->save_opcount = opcount;
Bram Moolenaarcce713d2019-03-04 11:40:12 +01007439 sst->save_reg_executing = reg_executing;
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007440
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007441 msg_scroll = FALSE; // no msg scrolling in Normal mode
7442 restart_edit = 0; // don't go to Insert mode
7443 p_im = FALSE; // don't use 'insertmode'
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007444
7445 /*
7446 * Save the current typeahead. This is required to allow using ":normal"
7447 * from an event handler and makes sure we don't hang when the argument
7448 * ends with half a command.
7449 */
7450 save_typeahead(&sst->tabuf);
7451 return sst->tabuf.typebuf_valid;
7452}
7453
7454 void
7455restore_current_state(save_state_T *sst)
7456{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007457 // Restore the previous typeahead.
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007458 restore_typeahead(&sst->tabuf);
7459
7460 msg_scroll = sst->save_msg_scroll;
7461 restart_edit = sst->save_restart_edit;
7462 p_im = sst->save_insertmode;
7463 finish_op = sst->save_finish_op;
7464 opcount = sst->save_opcount;
Bram Moolenaarcce713d2019-03-04 11:40:12 +01007465 reg_executing = sst->save_reg_executing;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007466 msg_didout |= sst->save_msg_didout; // don't reset msg_didout now
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007467
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007468 // Restore the state (needed when called from a function executed for
7469 // 'indentexpr'). Update the mouse and cursor, they may have changed.
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007470 State = sst->save_State;
7471#ifdef CURSOR_SHAPE
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007472 ui_cursor_shape(); // may show different cursor shape
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007473#endif
7474}
7475
7476/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007477 * ":normal[!] {commands}": Execute normal mode commands.
7478 */
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01007479 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007480ex_normal(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007481{
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007482 save_state_T save_state;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007483 char_u *arg = NULL;
7484 int l;
7485 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007486
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00007487 if (ex_normal_lock > 0)
7488 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007489 emsg(_(e_secure));
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00007490 return;
7491 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007492 if (ex_normal_busy >= p_mmd)
7493 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007494 emsg(_("E192: Recursive use of :normal too deep"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007495 return;
7496 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007497
Bram Moolenaar071d4272004-06-13 20:20:40 +00007498 /*
7499 * vgetc() expects a CSI and K_SPECIAL to have been escaped. Don't do
7500 * this for the K_SPECIAL leading byte, otherwise special keys will not
7501 * work.
7502 */
7503 if (has_mbyte)
7504 {
7505 int len = 0;
7506
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007507 // Count the number of characters to be escaped.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007508 for (p = eap->arg; *p != NUL; ++p)
7509 {
Bram Moolenaar13505972019-01-24 15:04:48 +01007510#ifdef FEAT_GUI
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007511 if (*p == CSI) // leadbyte CSI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007512 len += 2;
Bram Moolenaar13505972019-01-24 15:04:48 +01007513#endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007514 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007515 if (*++p == K_SPECIAL // trailbyte K_SPECIAL or CSI
Bram Moolenaar13505972019-01-24 15:04:48 +01007516#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007517 || *p == CSI
Bram Moolenaar13505972019-01-24 15:04:48 +01007518#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007519 )
7520 len += 2;
7521 }
7522 if (len > 0)
7523 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02007524 arg = alloc(STRLEN(eap->arg) + len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007525 if (arg != NULL)
7526 {
7527 len = 0;
7528 for (p = eap->arg; *p != NUL; ++p)
7529 {
7530 arg[len++] = *p;
Bram Moolenaar13505972019-01-24 15:04:48 +01007531#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007532 if (*p == CSI)
7533 {
7534 arg[len++] = KS_EXTRA;
7535 arg[len++] = (int)KE_CSI;
7536 }
Bram Moolenaar13505972019-01-24 15:04:48 +01007537#endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007538 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007539 {
7540 arg[len++] = *++p;
7541 if (*p == K_SPECIAL)
7542 {
7543 arg[len++] = KS_SPECIAL;
7544 arg[len++] = KE_FILLER;
7545 }
Bram Moolenaar13505972019-01-24 15:04:48 +01007546#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007547 else if (*p == CSI)
7548 {
7549 arg[len++] = KS_EXTRA;
7550 arg[len++] = (int)KE_CSI;
7551 }
Bram Moolenaar13505972019-01-24 15:04:48 +01007552#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007553 }
7554 arg[len] = NUL;
7555 }
7556 }
7557 }
7558 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007559
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007560 ++ex_normal_busy;
7561 if (save_current_state(&save_state))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007562 {
7563 /*
7564 * Repeat the :normal command for each line in the range. When no
7565 * range given, execute it just once, without positioning the cursor
7566 * first.
7567 */
7568 do
7569 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007570 if (eap->addr_count != 0)
7571 {
7572 curwin->w_cursor.lnum = eap->line1++;
7573 curwin->w_cursor.col = 0;
Bram Moolenaard5d37532017-03-27 23:02:07 +02007574 check_cursor_moved(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007575 }
7576
Bram Moolenaar13505972019-01-24 15:04:48 +01007577 exec_normal_cmd(arg != NULL
7578 ? arg
7579 : eap->arg, eap->forceit ? REMAP_NONE : REMAP_YES, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007580 }
7581 while (eap->addr_count > 0 && eap->line1 <= eap->line2 && !got_int);
7582 }
7583
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007584 // Might not return to the main loop when in an event handler.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007585 update_topline_cursor();
7586
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007587 restore_current_state(&save_state);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007588 --ex_normal_busy;
Bram Moolenaareda73602014-11-05 09:53:23 +01007589 setmouse();
Bram Moolenaareda73602014-11-05 09:53:23 +01007590#ifdef CURSOR_SHAPE
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007591 ui_cursor_shape(); // may show different cursor shape
Bram Moolenaareda73602014-11-05 09:53:23 +01007592#endif
7593
Bram Moolenaar071d4272004-06-13 20:20:40 +00007594 vim_free(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007595}
7596
7597/*
Bram Moolenaar64969662005-12-14 21:59:55 +00007598 * ":startinsert", ":startreplace" and ":startgreplace"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007599 */
7600 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007601ex_startinsert(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007602{
Bram Moolenaarfd371682005-01-14 21:42:54 +00007603 if (eap->forceit)
7604 {
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02007605 // cursor line can be zero on startup
Bram Moolenaar09ca9322017-09-26 17:40:45 +02007606 if (!curwin->w_cursor.lnum)
7607 curwin->w_cursor.lnum = 1;
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02007608 set_cursor_for_append_to_line();
Bram Moolenaarfd371682005-01-14 21:42:54 +00007609 }
7610
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02007611 // Ignore the command when already in Insert mode. Inserting an
7612 // expression register that invokes a function can do this.
Bram Moolenaara40c5002005-01-09 21:16:21 +00007613 if (State & INSERT)
7614 return;
7615
Bram Moolenaar64969662005-12-14 21:59:55 +00007616 if (eap->cmdidx == CMD_startinsert)
7617 restart_edit = 'a';
7618 else if (eap->cmdidx == CMD_startreplace)
7619 restart_edit = 'R';
Bram Moolenaar071d4272004-06-13 20:20:40 +00007620 else
Bram Moolenaar64969662005-12-14 21:59:55 +00007621 restart_edit = 'V';
7622
7623 if (!eap->forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007624 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00007625 if (eap->cmdidx == CMD_startinsert)
7626 restart_edit = 'i';
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02007627 curwin->w_curswant = 0; // avoid MAXCOL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007628 }
7629}
7630
7631/*
7632 * ":stopinsert"
7633 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007634 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007635ex_stopinsert(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007636{
7637 restart_edit = 0;
7638 stop_insert_mode = TRUE;
Bram Moolenaarfd773e92016-04-02 19:39:16 +02007639 clearmode();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007640}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007641
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00007642/*
7643 * Execute normal mode command "cmd".
7644 * "remap" can be REMAP_NONE or REMAP_YES.
7645 */
7646 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007647exec_normal_cmd(char_u *cmd, int remap, int silent)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00007648{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007649 // Stuff the argument into the typeahead buffer.
Bram Moolenaar25281632016-01-21 23:32:32 +01007650 ins_typebuf(cmd, remap, 0, TRUE, silent);
Bram Moolenaar586c70c2018-10-02 16:23:58 +02007651 exec_normal(FALSE, FALSE, FALSE);
Bram Moolenaar25281632016-01-21 23:32:32 +01007652}
Bram Moolenaar25281632016-01-21 23:32:32 +01007653
Bram Moolenaar25281632016-01-21 23:32:32 +01007654/*
7655 * Execute normal_cmd() until there is no typeahead left.
Bram Moolenaar586c70c2018-10-02 16:23:58 +02007656 * When "use_vpeekc" is TRUE use vpeekc() to check for available chars.
Bram Moolenaar25281632016-01-21 23:32:32 +01007657 */
7658 void
Bram Moolenaar586c70c2018-10-02 16:23:58 +02007659exec_normal(int was_typed, int use_vpeekc, int may_use_terminal_loop UNUSED)
Bram Moolenaar25281632016-01-21 23:32:32 +01007660{
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00007661 oparg_T oa;
Bram Moolenaar905dd902019-04-07 14:21:47 +02007662 int c;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00007663
Bram Moolenaar905dd902019-04-07 14:21:47 +02007664 // When calling vpeekc() from feedkeys() it will return Ctrl_C when there
7665 // is nothing to get, so also check for Ctrl_C.
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00007666 clear_oparg(&oa);
7667 finish_op = FALSE;
Bram Moolenaar586c70c2018-10-02 16:23:58 +02007668 while ((!stuff_empty()
7669 || ((was_typed || !typebuf_typed()) && typebuf.tb_len > 0)
Bram Moolenaar905dd902019-04-07 14:21:47 +02007670 || (use_vpeekc && (c = vpeekc()) != NUL && c != Ctrl_C))
Bram Moolenaar586c70c2018-10-02 16:23:58 +02007671 && !got_int)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00007672 {
7673 update_topline_cursor();
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02007674#ifdef FEAT_TERMINAL
Bram Moolenaar655a82a2018-05-08 22:01:07 +02007675 if (may_use_terminal_loop && term_use_loop()
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02007676 && oa.op_type == OP_NOP && oa.regname == NUL
7677 && !VIsual_active)
7678 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007679 // If terminal_loop() returns OK we got a key that is handled
7680 // in Normal model. With FAIL we first need to position the
7681 // cursor and the screen needs to be redrawn.
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02007682 if (terminal_loop(TRUE) == OK)
7683 normal_cmd(&oa, TRUE);
7684 }
7685 else
7686#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007687 // execute a Normal mode cmd
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02007688 normal_cmd(&oa, TRUE);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00007689 }
7690}
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00007691
Bram Moolenaar071d4272004-06-13 20:20:40 +00007692#ifdef FEAT_FIND_ID
7693 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007694ex_checkpath(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007695{
7696 find_pattern_in_path(NULL, 0, 0, FALSE, FALSE, CHECK_PATH, 1L,
7697 eap->forceit ? ACTION_SHOW_ALL : ACTION_SHOW,
7698 (linenr_T)1, (linenr_T)MAXLNUM);
7699}
7700
Bram Moolenaar4033c552017-09-16 20:54:51 +02007701#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007702/*
7703 * ":psearch"
7704 */
7705 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007706ex_psearch(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007707{
7708 g_do_tagpreview = p_pvh;
7709 ex_findpat(eap);
7710 g_do_tagpreview = 0;
7711}
7712#endif
7713
7714 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007715ex_findpat(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007716{
7717 int whole = TRUE;
7718 long n;
7719 char_u *p;
7720 int action;
7721
7722 switch (cmdnames[eap->cmdidx].cmd_name[2])
7723 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007724 case 'e': // ":psearch", ":isearch" and ":dsearch"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007725 if (cmdnames[eap->cmdidx].cmd_name[0] == 'p')
7726 action = ACTION_GOTO;
7727 else
7728 action = ACTION_SHOW;
7729 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007730 case 'i': // ":ilist" and ":dlist"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007731 action = ACTION_SHOW_ALL;
7732 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007733 case 'u': // ":ijump" and ":djump"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007734 action = ACTION_GOTO;
7735 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007736 default: // ":isplit" and ":dsplit"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007737 action = ACTION_SPLIT;
7738 break;
7739 }
7740
7741 n = 1;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007742 if (vim_isdigit(*eap->arg)) // get count
Bram Moolenaar071d4272004-06-13 20:20:40 +00007743 {
7744 n = getdigits(&eap->arg);
7745 eap->arg = skipwhite(eap->arg);
7746 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007747 if (*eap->arg == '/') // Match regexp, not just whole words
Bram Moolenaar071d4272004-06-13 20:20:40 +00007748 {
7749 whole = FALSE;
7750 ++eap->arg;
7751 p = skip_regexp(eap->arg, '/', p_magic, NULL);
7752 if (*p)
7753 {
7754 *p++ = NUL;
7755 p = skipwhite(p);
7756
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007757 // Check for trailing illegal characters
Bram Moolenaar071d4272004-06-13 20:20:40 +00007758 if (!ends_excmd(*p))
7759 eap->errmsg = e_trailing;
7760 else
7761 eap->nextcmd = check_nextcmd(p);
7762 }
7763 }
7764 if (!eap->skip)
7765 find_pattern_in_path(eap->arg, 0, (int)STRLEN(eap->arg),
7766 whole, !eap->forceit,
7767 *eap->cmd == 'd' ? FIND_DEFINE : FIND_ANY,
7768 n, action, eap->line1, eap->line2);
7769}
7770#endif
7771
Bram Moolenaar071d4272004-06-13 20:20:40 +00007772
Bram Moolenaar4033c552017-09-16 20:54:51 +02007773#ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00007774/*
7775 * ":ptag", ":ptselect", ":ptjump", ":ptnext", etc.
7776 */
7777 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007778ex_ptag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007779{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007780 g_do_tagpreview = p_pvh; // will be reset to 0 in ex_tag_cmd()
Bram Moolenaar071d4272004-06-13 20:20:40 +00007781 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
7782}
7783
7784/*
7785 * ":pedit"
7786 */
7787 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007788ex_pedit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007789{
7790 win_T *curwin_save = curwin;
7791
Bram Moolenaar026587b2019-08-17 15:08:00 +02007792 // Open the preview window or popup and make it the current window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007793 g_do_tagpreview = p_pvh;
Bram Moolenaar576a4a62019-08-18 15:25:17 +02007794 prepare_tagpreview(TRUE, TRUE, FALSE);
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02007795
Bram Moolenaar026587b2019-08-17 15:08:00 +02007796 // Edit the file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007797 do_exedit(eap, NULL);
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02007798
Bram Moolenaar071d4272004-06-13 20:20:40 +00007799 if (curwin != curwin_save && win_valid(curwin_save))
7800 {
Bram Moolenaar026587b2019-08-17 15:08:00 +02007801 // Return cursor to where we were
Bram Moolenaar071d4272004-06-13 20:20:40 +00007802 validate_cursor();
7803 redraw_later(VALID);
7804 win_enter(curwin_save, TRUE);
7805 }
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01007806# ifdef FEAT_PROP_POPUP
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02007807 else if (WIN_IS_POPUP(curwin))
7808 {
7809 // can't keep focus in popup window
7810 win_enter(firstwin, TRUE);
7811 }
7812# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007813 g_do_tagpreview = 0;
7814}
Bram Moolenaar4033c552017-09-16 20:54:51 +02007815#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007816
7817/*
7818 * ":stag", ":stselect" and ":stjump".
7819 */
7820 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007821ex_stag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007822{
7823 postponed_split = -1;
7824 postponed_split_flags = cmdmod.split;
Bram Moolenaard326ce82007-03-11 14:48:29 +00007825 postponed_split_tab = cmdmod.tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007826 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
7827 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00007828 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007829}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007830
7831/*
7832 * ":tag", ":tselect", ":tjump", ":tnext", etc.
7833 */
7834 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007835ex_tag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007836{
7837 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name);
7838}
7839
7840 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007841ex_tag_cmd(exarg_T *eap, char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007842{
7843 int cmd;
7844
7845 switch (name[1])
7846 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007847 case 'j': cmd = DT_JUMP; // ":tjump"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007848 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007849 case 's': cmd = DT_SELECT; // ":tselect"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007850 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007851 case 'p': cmd = DT_PREV; // ":tprevious"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007852 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007853 case 'N': cmd = DT_PREV; // ":tNext"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007854 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007855 case 'n': cmd = DT_NEXT; // ":tnext"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007856 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007857 case 'o': cmd = DT_POP; // ":pop"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007858 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007859 case 'f': // ":tfirst"
7860 case 'r': cmd = DT_FIRST; // ":trewind"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007861 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007862 case 'l': cmd = DT_LAST; // ":tlast"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007863 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007864 default: // ":tag"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007865#ifdef FEAT_CSCOPE
Bram Moolenaar7c94c262008-06-20 09:11:34 +00007866 if (p_cst && *eap->arg != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007867 {
Bram Moolenaard4db7712016-11-12 19:16:46 +01007868 ex_cstag(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007869 return;
7870 }
7871#endif
7872 cmd = DT_TAG;
7873 break;
7874 }
7875
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00007876 if (name[0] == 'l')
7877 {
7878#ifndef FEAT_QUICKFIX
7879 ex_ni(eap);
7880 return;
7881#else
7882 cmd = DT_LTAG;
7883#endif
7884 }
7885
Bram Moolenaar071d4272004-06-13 20:20:40 +00007886 do_tag(eap->arg, cmd, eap->addr_count > 0 ? (int)eap->line2 : 1,
7887 eap->forceit, TRUE);
7888}
7889
7890/*
Bram Moolenaar05bb9532008-07-04 09:44:11 +00007891 * Check "str" for starting with a special cmdline variable.
7892 * If found return one of the SPEC_ values and set "*usedlen" to the length of
7893 * the variable. Otherwise return -1 and "*usedlen" is unchanged.
7894 */
7895 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007896find_cmdline_var(char_u *src, int *usedlen)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00007897{
7898 int len;
7899 int i;
Bram Moolenaar8f0b2d42009-05-16 14:41:10 +00007900 static char *(spec_str[]) = {
Bram Moolenaar05bb9532008-07-04 09:44:11 +00007901 "%",
7902#define SPEC_PERC 0
7903 "#",
Bram Moolenaar65f08472017-09-10 18:16:20 +02007904#define SPEC_HASH (SPEC_PERC + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007905 "<cword>", // cursor word
Bram Moolenaar65f08472017-09-10 18:16:20 +02007906#define SPEC_CWORD (SPEC_HASH + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007907 "<cWORD>", // cursor WORD
Bram Moolenaar65f08472017-09-10 18:16:20 +02007908#define SPEC_CCWORD (SPEC_CWORD + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007909 "<cexpr>", // expr under cursor
Bram Moolenaar65f08472017-09-10 18:16:20 +02007910#define SPEC_CEXPR (SPEC_CCWORD + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007911 "<cfile>", // cursor path name
Bram Moolenaar65f08472017-09-10 18:16:20 +02007912#define SPEC_CFILE (SPEC_CEXPR + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007913 "<sfile>", // ":so" file name
Bram Moolenaar65f08472017-09-10 18:16:20 +02007914#define SPEC_SFILE (SPEC_CFILE + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007915 "<slnum>", // ":so" file line number
Bram Moolenaar65f08472017-09-10 18:16:20 +02007916#define SPEC_SLNUM (SPEC_SFILE + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007917 "<afile>", // autocommand file name
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02007918#define SPEC_AFILE (SPEC_SLNUM + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007919 "<abuf>", // autocommand buffer number
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02007920#define SPEC_ABUF (SPEC_AFILE + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007921 "<amatch>", // autocommand match name
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007922#define SPEC_AMATCH (SPEC_ABUF + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007923 "<sflnum>", // script file line number
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02007924#define SPEC_SFLNUM (SPEC_AMATCH + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00007925#ifdef FEAT_CLIENTSERVER
7926 "<client>"
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02007927# define SPEC_CLIENT (SPEC_SFLNUM + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00007928#endif
7929 };
Bram Moolenaar05bb9532008-07-04 09:44:11 +00007930
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00007931 for (i = 0; i < (int)(sizeof(spec_str) / sizeof(char *)); ++i)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00007932 {
7933 len = (int)STRLEN(spec_str[i]);
7934 if (STRNCMP(src, spec_str[i], len) == 0)
7935 {
7936 *usedlen = len;
7937 return i;
7938 }
7939 }
7940 return -1;
7941}
7942
7943/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007944 * Evaluate cmdline variables.
7945 *
7946 * change '%' to curbuf->b_ffname
7947 * '#' to curwin->w_altfile
7948 * '<cword>' to word under the cursor
7949 * '<cWORD>' to WORD under the cursor
Bram Moolenaar8071cb22019-07-12 17:58:01 +02007950 * '<cexpr>' to C-expression under the cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00007951 * '<cfile>' to path name under the cursor
7952 * '<sfile>' to sourced file name
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01007953 * '<slnum>' to sourced file line number
Bram Moolenaar071d4272004-06-13 20:20:40 +00007954 * '<afile>' to file name for autocommand
7955 * '<abuf>' to buffer number for autocommand
7956 * '<amatch>' to matching name for autocommand
7957 *
7958 * When an error is detected, "errormsg" is set to a non-NULL pointer (may be
7959 * "" for error without a message) and NULL is returned.
7960 * Returns an allocated string if a valid match was found.
7961 * Returns NULL if no match was found. "usedlen" then still contains the
7962 * number of characters to skip.
7963 */
7964 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007965eval_vars(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007966 char_u *src, // pointer into commandline
7967 char_u *srcstart, // beginning of valid memory for src
7968 int *usedlen, // characters after src that are used
7969 linenr_T *lnump, // line number for :e command, or NULL
7970 char **errormsg, // pointer to error message
7971 int *escaped) // return value has escaped white space (can
7972 // be NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007973{
7974 int i;
7975 char_u *s;
7976 char_u *result;
7977 char_u *resultbuf = NULL;
7978 int resultlen;
7979 buf_T *buf;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007980 int valid = VALID_HEAD + VALID_PATH; // assume valid result
Bram Moolenaar071d4272004-06-13 20:20:40 +00007981 int spec_idx;
Bram Moolenaarfd249462018-07-28 16:14:30 +02007982 int tilde_file = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007983 int skip_mod = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007984 char_u strbuf[30];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007985
7986 *errormsg = NULL;
Bram Moolenaar63b92542007-03-27 14:57:09 +00007987 if (escaped != NULL)
7988 *escaped = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007989
7990 /*
7991 * Check if there is something to do.
7992 */
Bram Moolenaar05bb9532008-07-04 09:44:11 +00007993 spec_idx = find_cmdline_var(src, usedlen);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007994 if (spec_idx < 0) // no match
Bram Moolenaar071d4272004-06-13 20:20:40 +00007995 {
7996 *usedlen = 1;
7997 return NULL;
7998 }
7999
8000 /*
8001 * Skip when preceded with a backslash "\%" and "\#".
8002 * Note: In "\\%" the % is also not recognized!
8003 */
8004 if (src > srcstart && src[-1] == '\\')
8005 {
8006 *usedlen = 0;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008007 STRMOVE(src - 1, src); // remove backslash
Bram Moolenaar071d4272004-06-13 20:20:40 +00008008 return NULL;
8009 }
8010
8011 /*
8012 * word or WORD under cursor
8013 */
Bram Moolenaar65f08472017-09-10 18:16:20 +02008014 if (spec_idx == SPEC_CWORD || spec_idx == SPEC_CCWORD
8015 || spec_idx == SPEC_CEXPR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008016 {
Bram Moolenaar65f08472017-09-10 18:16:20 +02008017 resultlen = find_ident_under_cursor(&result,
8018 spec_idx == SPEC_CWORD ? (FIND_IDENT | FIND_STRING)
8019 : spec_idx == SPEC_CEXPR ? (FIND_IDENT | FIND_STRING | FIND_EVAL)
8020 : FIND_STRING);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008021 if (resultlen == 0)
8022 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008023 *errormsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +00008024 return NULL;
8025 }
8026 }
8027
8028 /*
8029 * '#': Alternate file name
8030 * '%': Current file name
8031 * File name under the cursor
8032 * File name for autocommand
8033 * and following modifiers
8034 */
8035 else
8036 {
8037 switch (spec_idx)
8038 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008039 case SPEC_PERC: // '%': current file
Bram Moolenaar071d4272004-06-13 20:20:40 +00008040 if (curbuf->b_fname == NULL)
8041 {
8042 result = (char_u *)"";
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008043 valid = 0; // Must have ":p:h" to be valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00008044 }
8045 else
Bram Moolenaar00136dc2018-07-25 21:19:13 +02008046 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008047 result = curbuf->b_fname;
Bram Moolenaar00136dc2018-07-25 21:19:13 +02008048 tilde_file = STRCMP(result, "~") == 0;
8049 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008050 break;
8051
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008052 case SPEC_HASH: // '#' or "#99": alternate file
8053 if (src[1] == '#') // "##": the argument list
Bram Moolenaar071d4272004-06-13 20:20:40 +00008054 {
8055 result = arg_all();
8056 resultbuf = result;
8057 *usedlen = 2;
Bram Moolenaar63b92542007-03-27 14:57:09 +00008058 if (escaped != NULL)
8059 *escaped = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008060 skip_mod = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008061 break;
8062 }
8063 s = src + 1;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008064 if (*s == '<') // "#<99" uses v:oldfiles
Bram Moolenaard812df62008-11-09 12:46:09 +00008065 ++s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008066 i = (int)getdigits(&s);
Bram Moolenaarc312b8b2017-10-28 17:53:04 +02008067 if (s == src + 2 && src[1] == '-')
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008068 // just a minus sign, don't skip over it
Bram Moolenaarc312b8b2017-10-28 17:53:04 +02008069 s--;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008070 *usedlen = (int)(s - src); // length of what we expand
Bram Moolenaar071d4272004-06-13 20:20:40 +00008071
Bram Moolenaarc312b8b2017-10-28 17:53:04 +02008072 if (src[1] == '<' && i != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008073 {
Bram Moolenaard812df62008-11-09 12:46:09 +00008074 if (*usedlen < 2)
8075 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008076 // Should we give an error message for #<text?
Bram Moolenaard812df62008-11-09 12:46:09 +00008077 *usedlen = 1;
8078 return NULL;
8079 }
8080#ifdef FEAT_EVAL
8081 result = list_find_str(get_vim_var_list(VV_OLDFILES),
8082 (long)i);
8083 if (result == NULL)
8084 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008085 *errormsg = "";
Bram Moolenaard812df62008-11-09 12:46:09 +00008086 return NULL;
8087 }
8088#else
Bram Moolenaar8e481e82019-01-15 20:07:48 +01008089 *errormsg = _("E809: #< is not available without the +eval feature");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008090 return NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +00008091#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008092 }
8093 else
Bram Moolenaard812df62008-11-09 12:46:09 +00008094 {
Bram Moolenaarc312b8b2017-10-28 17:53:04 +02008095 if (i == 0 && src[1] == '<' && *usedlen > 1)
8096 *usedlen = 1;
Bram Moolenaard812df62008-11-09 12:46:09 +00008097 buf = buflist_findnr(i);
8098 if (buf == NULL)
8099 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008100 *errormsg = _("E194: No alternate file name to substitute for '#'");
Bram Moolenaard812df62008-11-09 12:46:09 +00008101 return NULL;
8102 }
8103 if (lnump != NULL)
8104 *lnump = ECMD_LAST;
8105 if (buf->b_fname == NULL)
8106 {
8107 result = (char_u *)"";
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008108 valid = 0; // Must have ":p:h" to be valid
Bram Moolenaard812df62008-11-09 12:46:09 +00008109 }
8110 else
Bram Moolenaar00136dc2018-07-25 21:19:13 +02008111 {
Bram Moolenaard812df62008-11-09 12:46:09 +00008112 result = buf->b_fname;
Bram Moolenaar00136dc2018-07-25 21:19:13 +02008113 tilde_file = STRCMP(result, "~") == 0;
8114 }
Bram Moolenaard812df62008-11-09 12:46:09 +00008115 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008116 break;
8117
8118#ifdef FEAT_SEARCHPATH
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008119 case SPEC_CFILE: // file name under cursor
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008120 result = file_name_at_cursor(FNAME_MESS|FNAME_HYP, 1L, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008121 if (result == NULL)
8122 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008123 *errormsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +00008124 return NULL;
8125 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008126 resultbuf = result; // remember allocated string
Bram Moolenaar071d4272004-06-13 20:20:40 +00008127 break;
8128#endif
8129
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008130 case SPEC_AFILE: // file name for autocommand
Bram Moolenaar071d4272004-06-13 20:20:40 +00008131 result = autocmd_fname;
Bram Moolenaarf6dad432008-09-18 19:29:58 +00008132 if (result != NULL && !autocmd_fname_full)
8133 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008134 // Still need to turn the fname into a full path. It is
8135 // postponed to avoid a delay when <afile> is not used.
Bram Moolenaarf6dad432008-09-18 19:29:58 +00008136 autocmd_fname_full = TRUE;
8137 result = FullName_save(autocmd_fname, FALSE);
8138 vim_free(autocmd_fname);
8139 autocmd_fname = result;
8140 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008141 if (result == NULL)
8142 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008143 *errormsg = _("E495: no autocommand file name to substitute for \"<afile>\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008144 return NULL;
8145 }
Bram Moolenaara0174af2008-01-02 20:08:25 +00008146 result = shorten_fname1(result);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008147 break;
8148
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008149 case SPEC_ABUF: // buffer number for autocommand
Bram Moolenaar071d4272004-06-13 20:20:40 +00008150 if (autocmd_bufnr <= 0)
8151 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008152 *errormsg = _("E496: no autocommand buffer number to substitute for \"<abuf>\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008153 return NULL;
8154 }
8155 sprintf((char *)strbuf, "%d", autocmd_bufnr);
8156 result = strbuf;
8157 break;
8158
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008159 case SPEC_AMATCH: // match name for autocommand
Bram Moolenaar071d4272004-06-13 20:20:40 +00008160 result = autocmd_match;
8161 if (result == NULL)
8162 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008163 *errormsg = _("E497: no autocommand match name to substitute for \"<amatch>\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008164 return NULL;
8165 }
8166 break;
8167
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008168 case SPEC_SFILE: // file name for ":so" command
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01008169 result = estack_sfile();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008170 if (result == NULL)
8171 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008172 *errormsg = _("E498: no :source file name to substitute for \"<sfile>\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008173 return NULL;
8174 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01008175 resultbuf = result; // remember allocated string
Bram Moolenaar071d4272004-06-13 20:20:40 +00008176 break;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008177
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008178 case SPEC_SLNUM: // line in file for ":so" command
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01008179 if (SOURCING_NAME == NULL || SOURCING_LNUM == 0)
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01008180 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008181 *errormsg = _("E842: no line number to use for \"<slnum>\"");
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01008182 return NULL;
8183 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01008184 sprintf((char *)strbuf, "%ld", SOURCING_LNUM);
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01008185 result = strbuf;
8186 break;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008187
8188#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008189 case SPEC_SFLNUM: // line in script file
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01008190 if (current_sctx.sc_lnum + SOURCING_LNUM == 0)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008191 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008192 *errormsg = _("E961: no line number to use for \"<sflnum>\"");
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008193 return NULL;
8194 }
8195 sprintf((char *)strbuf, "%ld",
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01008196 (long)(current_sctx.sc_lnum + SOURCING_LNUM));
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008197 result = strbuf;
8198 break;
8199#endif
8200
8201#ifdef FEAT_CLIENTSERVER
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008202 case SPEC_CLIENT: // Source of last submitted input
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008203 sprintf((char *)strbuf, PRINTF_HEX_LONG_U,
8204 (long_u)clientWindow);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008205 result = strbuf;
8206 break;
8207#endif
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008208
Bram Moolenaar9e0f6ec2017-05-16 09:36:54 +02008209 default:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008210 result = (char_u *)""; // avoid gcc warning
Bram Moolenaar9e0f6ec2017-05-16 09:36:54 +02008211 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008212 }
8213
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008214 resultlen = (int)STRLEN(result); // length of new string
8215 if (src[*usedlen] == '<') // remove the file name extension
Bram Moolenaar071d4272004-06-13 20:20:40 +00008216 {
8217 ++*usedlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008218 if ((s = vim_strrchr(result, '.')) != NULL && s >= gettail(result))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008219 resultlen = (int)(s - result);
8220 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008221 else if (!skip_mod)
8222 {
Bram Moolenaar00136dc2018-07-25 21:19:13 +02008223 valid |= modify_fname(src, tilde_file, usedlen, &result, &resultbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008224 &resultlen);
8225 if (result == NULL)
8226 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008227 *errormsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +00008228 return NULL;
8229 }
8230 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008231 }
8232
8233 if (resultlen == 0 || valid != VALID_HEAD + VALID_PATH)
8234 {
8235 if (valid != VALID_HEAD + VALID_PATH)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008236 // xgettext:no-c-format
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008237 *errormsg = _("E499: Empty file name for '%' or '#', only works with \":p:h\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008238 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008239 *errormsg = _("E500: Evaluates to an empty string");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008240 result = NULL;
8241 }
8242 else
8243 result = vim_strnsave(result, resultlen);
8244 vim_free(resultbuf);
8245 return result;
8246}
8247
8248/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008249 * Expand the <sfile> string in "arg".
8250 *
8251 * Returns an allocated string, or NULL for any error.
8252 */
8253 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008254expand_sfile(char_u *arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008255{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008256 char *errormsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008257 int len;
8258 char_u *result;
8259 char_u *newres;
8260 char_u *repl;
8261 int srclen;
8262 char_u *p;
8263
8264 result = vim_strsave(arg);
8265 if (result == NULL)
8266 return NULL;
8267
8268 for (p = result; *p; )
8269 {
8270 if (STRNCMP(p, "<sfile>", 7) != 0)
8271 ++p;
8272 else
8273 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008274 // replace "<sfile>" with the sourced file name, and do ":" stuff
Bram Moolenaar63b92542007-03-27 14:57:09 +00008275 repl = eval_vars(p, result, &srclen, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008276 if (errormsg != NULL)
8277 {
8278 if (*errormsg)
8279 emsg(errormsg);
8280 vim_free(result);
8281 return NULL;
8282 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008283 if (repl == NULL) // no match (cannot happen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008284 {
8285 p += srclen;
8286 continue;
8287 }
8288 len = (int)STRLEN(result) - srclen + (int)STRLEN(repl) + 1;
8289 newres = alloc(len);
8290 if (newres == NULL)
8291 {
8292 vim_free(repl);
8293 vim_free(result);
8294 return NULL;
8295 }
8296 mch_memmove(newres, result, (size_t)(p - result));
8297 STRCPY(newres + (p - result), repl);
8298 len = (int)STRLEN(newres);
8299 STRCAT(newres, p + srclen);
8300 vim_free(repl);
8301 vim_free(result);
8302 result = newres;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008303 p = newres + len; // continue after the match
Bram Moolenaar071d4272004-06-13 20:20:40 +00008304 }
8305 }
8306
8307 return result;
8308}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008309
Bram Moolenaar071d4272004-06-13 20:20:40 +00008310#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG) || defined(PROTO)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00008311/*
Bram Moolenaard9462e32011-04-11 21:35:11 +02008312 * Make a dialog message in "buff[DIALOG_MSG_SIZE]".
Bram Moolenaarb765d632005-06-07 21:00:02 +00008313 * "format" must contain "%s".
Bram Moolenaar9c13b352005-05-19 20:53:52 +00008314 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008315 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008316dialog_msg(char_u *buff, char *format, char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008317{
Bram Moolenaar071d4272004-06-13 20:20:40 +00008318 if (fname == NULL)
8319 fname = (char_u *)_("Untitled");
Bram Moolenaard9462e32011-04-11 21:35:11 +02008320 vim_snprintf((char *)buff, DIALOG_MSG_SIZE, format, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008321}
8322#endif
8323
8324/*
8325 * ":behave {mswin,xterm}"
8326 */
8327 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008328ex_behave(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008329{
8330 if (STRCMP(eap->arg, "mswin") == 0)
8331 {
8332 set_option_value((char_u *)"selection", 0L, (char_u *)"exclusive", 0);
8333 set_option_value((char_u *)"selectmode", 0L, (char_u *)"mouse,key", 0);
8334 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"popup", 0);
8335 set_option_value((char_u *)"keymodel", 0L,
8336 (char_u *)"startsel,stopsel", 0);
8337 }
8338 else if (STRCMP(eap->arg, "xterm") == 0)
8339 {
8340 set_option_value((char_u *)"selection", 0L, (char_u *)"inclusive", 0);
8341 set_option_value((char_u *)"selectmode", 0L, (char_u *)"", 0);
8342 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"extend", 0);
8343 set_option_value((char_u *)"keymodel", 0L, (char_u *)"", 0);
8344 }
8345 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008346 semsg(_(e_invarg2), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008347}
8348
Bram Moolenaar071d4272004-06-13 20:20:40 +00008349static int filetype_detect = FALSE;
8350static int filetype_plugin = FALSE;
8351static int filetype_indent = FALSE;
8352
8353/*
8354 * ":filetype [plugin] [indent] {on,off,detect}"
8355 * on: Load the filetype.vim file to install autocommands for file types.
8356 * off: Load the ftoff.vim file to remove all autocommands for file types.
8357 * plugin on: load filetype.vim and ftplugin.vim
8358 * plugin off: load ftplugof.vim
8359 * indent on: load filetype.vim and indent.vim
8360 * indent off: load indoff.vim
8361 */
8362 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008363ex_filetype(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008364{
8365 char_u *arg = eap->arg;
8366 int plugin = FALSE;
8367 int indent = FALSE;
8368
8369 if (*eap->arg == NUL)
8370 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008371 // Print current status.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008372 smsg("filetype detection:%s plugin:%s indent:%s",
Bram Moolenaar071d4272004-06-13 20:20:40 +00008373 filetype_detect ? "ON" : "OFF",
8374 filetype_plugin ? (filetype_detect ? "ON" : "(on)") : "OFF",
8375 filetype_indent ? (filetype_detect ? "ON" : "(on)") : "OFF");
8376 return;
8377 }
8378
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008379 // Accept "plugin" and "indent" in any order.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008380 for (;;)
8381 {
8382 if (STRNCMP(arg, "plugin", 6) == 0)
8383 {
8384 plugin = TRUE;
8385 arg = skipwhite(arg + 6);
8386 continue;
8387 }
8388 if (STRNCMP(arg, "indent", 6) == 0)
8389 {
8390 indent = TRUE;
8391 arg = skipwhite(arg + 6);
8392 continue;
8393 }
8394 break;
8395 }
8396 if (STRCMP(arg, "on") == 0 || STRCMP(arg, "detect") == 0)
8397 {
8398 if (*arg == 'o' || !filetype_detect)
8399 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01008400 source_runtime((char_u *)FILETYPE_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008401 filetype_detect = TRUE;
8402 if (plugin)
8403 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01008404 source_runtime((char_u *)FTPLUGIN_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008405 filetype_plugin = TRUE;
8406 }
8407 if (indent)
8408 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01008409 source_runtime((char_u *)INDENT_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008410 filetype_indent = TRUE;
8411 }
8412 }
8413 if (*arg == 'd')
8414 {
Bram Moolenaar1610d052016-06-09 22:53:01 +02008415 (void)do_doautocmd((char_u *)"filetypedetect BufRead", TRUE, NULL);
Bram Moolenaara3227e22006-03-08 21:32:40 +00008416 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008417 }
8418 }
8419 else if (STRCMP(arg, "off") == 0)
8420 {
8421 if (plugin || indent)
8422 {
8423 if (plugin)
8424 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01008425 source_runtime((char_u *)FTPLUGOF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008426 filetype_plugin = FALSE;
8427 }
8428 if (indent)
8429 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01008430 source_runtime((char_u *)INDOFF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008431 filetype_indent = FALSE;
8432 }
8433 }
8434 else
8435 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01008436 source_runtime((char_u *)FTOFF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008437 filetype_detect = FALSE;
8438 }
8439 }
8440 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008441 semsg(_(e_invarg2), arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008442}
8443
8444/*
Bram Moolenaar3e545692017-06-04 19:00:32 +02008445 * ":setfiletype [FALLBACK] {name}"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008446 */
8447 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008448ex_setfiletype(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008449{
8450 if (!did_filetype)
Bram Moolenaar3e545692017-06-04 19:00:32 +02008451 {
8452 char_u *arg = eap->arg;
8453
8454 if (STRNCMP(arg, "FALLBACK ", 9) == 0)
8455 arg += 9;
8456
8457 set_option_value((char_u *)"filetype", 0L, arg, OPT_LOCAL);
8458 if (arg != eap->arg)
8459 did_filetype = FALSE;
8460 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008461}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008462
Bram Moolenaar071d4272004-06-13 20:20:40 +00008463 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008464ex_digraphs(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008465{
8466#ifdef FEAT_DIGRAPHS
8467 if (*eap->arg != NUL)
8468 putdigraph(eap->arg);
8469 else
Bram Moolenaareae8ae12018-12-14 18:53:02 +01008470 listdigraphs(eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008471#else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008472 emsg(_("E196: No digraphs in this version"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008473#endif
8474}
8475
8476 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008477ex_set(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008478{
8479 int flags = 0;
8480
8481 if (eap->cmdidx == CMD_setlocal)
8482 flags = OPT_LOCAL;
8483 else if (eap->cmdidx == CMD_setglobal)
8484 flags = OPT_GLOBAL;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01008485#if defined(FEAT_EVAL) && defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008486 if (cmdmod.browse && flags == 0)
8487 ex_options(eap);
8488 else
8489#endif
8490 (void)do_set(eap->arg, flags);
8491}
8492
Bram Moolenaar451fc7b2018-04-27 22:53:07 +02008493#if defined(FEAT_SEARCH_EXTRA) || defined(PROTO)
8494 void
8495set_no_hlsearch(int flag)
8496{
8497 no_hlsearch = flag;
8498# ifdef FEAT_EVAL
8499 set_vim_var_nr(VV_HLSEARCH, !no_hlsearch && p_hls);
8500# endif
8501}
8502
Bram Moolenaar071d4272004-06-13 20:20:40 +00008503/*
8504 * ":nohlsearch"
8505 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008506 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008507ex_nohlsearch(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008508{
Bram Moolenaar451fc7b2018-04-27 22:53:07 +02008509 set_no_hlsearch(TRUE);
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00008510 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008511}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008512#endif
8513
8514#ifdef FEAT_CRYPT
8515/*
8516 * ":X": Get crypt key
8517 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008518 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008519ex_X(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008520{
Bram Moolenaar3a0c9082014-11-12 15:15:42 +01008521 crypt_check_current_method();
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02008522 (void)crypt_get_key(TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008523}
8524#endif
8525
8526#ifdef FEAT_FOLDING
8527 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008528ex_fold(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008529{
8530 if (foldManualAllowed(TRUE))
8531 foldCreate(eap->line1, eap->line2);
8532}
8533
8534 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008535ex_foldopen(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008536{
8537 opFoldRange(eap->line1, eap->line2, eap->cmdidx == CMD_foldopen,
8538 eap->forceit, FALSE);
8539}
8540
8541 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008542ex_folddo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008543{
8544 linenr_T lnum;
8545
Bram Moolenaar4facea32019-10-12 20:17:40 +02008546# ifdef FEAT_CLIPBOARD
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02008547 start_global_changes();
Bram Moolenaar4facea32019-10-12 20:17:40 +02008548# endif
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02008549
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008550 // First set the marks for all lines closed/open.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008551 for (lnum = eap->line1; lnum <= eap->line2; ++lnum)
8552 if (hasFolding(lnum, NULL, NULL) == (eap->cmdidx == CMD_folddoclosed))
8553 ml_setmarked(lnum);
8554
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008555 // Execute the command on the marked lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008556 global_exe(eap->arg);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008557 ml_clearmarked(); // clear rest of the marks
Bram Moolenaar4facea32019-10-12 20:17:40 +02008558# ifdef FEAT_CLIPBOARD
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02008559 end_global_changes();
Bram Moolenaar4facea32019-10-12 20:17:40 +02008560# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008561}
8562#endif
Bram Moolenaarf5291f32017-09-14 22:55:37 +02008563
Bram Moolenaar39665952018-08-15 20:59:48 +02008564#ifdef FEAT_QUICKFIX
8565/*
8566 * Returns TRUE if the supplied Ex cmdidx is for a location list command
8567 * instead of a quickfix command.
8568 */
8569 int
8570is_loclist_cmd(int cmdidx)
8571{
Bram Moolenaar74c8be22018-08-23 22:51:40 +02008572 if (cmdidx < 0 || cmdidx >= CMD_SIZE)
Bram Moolenaar39665952018-08-15 20:59:48 +02008573 return FALSE;
8574 return cmdnames[cmdidx].cmd_name[0] == 'l';
8575}
8576#endif
8577
Bram Moolenaar4facea32019-10-12 20:17:40 +02008578#if defined(FEAT_TIMERS) || defined(PROTO)
Bram Moolenaarf5291f32017-09-14 22:55:37 +02008579 int
8580get_pressedreturn(void)
8581{
8582 return ex_pressedreturn;
8583}
8584
8585 void
8586set_pressedreturn(int val)
8587{
8588 ex_pressedreturn = val;
8589}
8590#endif