blob: e11ad08d43132828e76dfb793b1965bcad2e64b2 [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 Moolenaare31ee862020-01-07 20:59:34 +0100648 ESTACK_CHECK_DECLARATION
Bram Moolenaar071d4272004-06-13 20:20:40 +0000649
650#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100651 // For every pair of do_cmdline()/do_one_cmd() calls, use an extra memory
652 // location for storing error messages to be converted to an exception.
653 // This ensures that the do_errthrow() call in do_one_cmd() does not
654 // combine the messages stored by an earlier invocation of do_one_cmd()
655 // with the command name of the later one. This would happen when
656 // BufWritePost autocommands are executed after a write error.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000657 saved_msg_list = msg_list;
658 msg_list = &private_msg_list;
659 private_msg_list = NULL;
660#endif
661
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100662 // It's possible to create an endless loop with ":execute", catch that
663 // here. The value of 200 allows nested function calls, ":source", etc.
664 // Allow 200 or 'maxfuncdepth', whatever is larger.
Bram Moolenaarb094ff42017-01-02 16:16:39 +0100665 if (call_depth >= 200
666#ifdef FEAT_EVAL
667 && call_depth >= p_mfd
668#endif
669 )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000670 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100671 emsg(_("E169: Command too recursive"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000672#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100673 // When converting to an exception, we do not include the command name
674 // since this is not an error of the specific command.
Bram Moolenaarddef1292019-12-16 17:10:33 +0100675 do_errthrow((cstack_T *)NULL, (char_u *)NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000676 msg_list = saved_msg_list;
677#endif
678 return FAIL;
679 }
680 ++call_depth;
681
682#ifdef FEAT_EVAL
683 cstack.cs_idx = -1;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000684 cstack.cs_looplevel = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000685 cstack.cs_trylevel = 0;
686 cstack.cs_emsg_silent_list = NULL;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000687 cstack.cs_lflags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000688 ga_init2(&lines_ga, (int)sizeof(wcmd_T), 10);
689
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100690 real_cookie = getline_cookie(fgetline, cookie);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000691
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100692 // Inside a function use a higher nesting level.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100693 getline_is_func = getline_equal(fgetline, cookie, get_func_line);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000694 if (getline_is_func && ex_nesting_level == func_level(real_cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000695 ++ex_nesting_level;
696
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100697 // Get the function or script name and the address where the next breakpoint
698 // line and the debug tick for a function or script are stored.
Bram Moolenaar05159a02005-02-26 23:04:13 +0000699 if (getline_is_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000700 {
701 fname = func_name(real_cookie);
702 breakpoint = func_breakpoint(real_cookie);
703 dbg_tick = func_dbg_tick(real_cookie);
704 }
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100705 else if (getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000706 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100707 fname = SOURCING_NAME;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000708 breakpoint = source_breakpoint(real_cookie);
709 dbg_tick = source_dbg_tick(real_cookie);
710 }
711
712 /*
713 * Initialize "force_abort" and "suppress_errthrow" at the top level.
714 */
715 if (!recursive)
716 {
717 force_abort = FALSE;
718 suppress_errthrow = FALSE;
719 }
720
721 /*
722 * If requested, store and reset the global values controlling the
Bram Moolenaar89d40322006-08-29 15:30:07 +0000723 * exception handling (used when debugging). Otherwise clear it to avoid
724 * a bogus compiler warning when the optimizer uses inline functions...
Bram Moolenaar071d4272004-06-13 20:20:40 +0000725 */
Bram Moolenaarb4872942006-05-13 10:32:52 +0000726 if (flags & DOCMD_EXCRESET)
Bram Moolenaared203462004-06-16 11:19:22 +0000727 save_dbg_stuff(&debug_saved);
Bram Moolenaar89d40322006-08-29 15:30:07 +0000728 else
Bram Moolenaar69b67f72015-09-25 16:59:47 +0200729 vim_memset(&debug_saved, 0, sizeof(debug_saved));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000730
731 initial_trylevel = trylevel;
732
733 /*
734 * "did_throw" will be set to TRUE when an exception is being thrown.
735 */
736 did_throw = FALSE;
737#endif
738 /*
739 * "did_emsg" will be set to TRUE when emsg() is used, in which case we
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000740 * cancel the whole command line, and any if/endif or loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000741 * If force_abort is set, we cancel everything.
742 */
743 did_emsg = FALSE;
744
745 /*
746 * KeyTyped is only set when calling vgetc(). Reset it here when not
747 * calling vgetc() (sourced command lines).
748 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100749 if (!(flags & DOCMD_KEYTYPED)
750 && !getline_equal(fgetline, cookie, getexline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000751 KeyTyped = FALSE;
752
753 /*
754 * Continue executing command lines:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000755 * - when inside an ":if", ":while" or ":for"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000756 * - for multiple commands on one line, separated with '|'
757 * - when repeating until there are no more lines (for ":source")
758 */
759 next_cmdline = cmdline;
760 do
761 {
Bram Moolenaar05159a02005-02-26 23:04:13 +0000762#ifdef FEAT_EVAL
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100763 getline_is_func = getline_equal(fgetline, cookie, get_func_line);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000764#endif
765
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100766 // stop skipping cmds for an error msg after all endif/while/for
Bram Moolenaar071d4272004-06-13 20:20:40 +0000767 if (next_cmdline == NULL
768#ifdef FEAT_EVAL
769 && !force_abort
770 && cstack.cs_idx < 0
Bram Moolenaar05159a02005-02-26 23:04:13 +0000771 && !(getline_is_func && func_has_abort(real_cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000772#endif
773 )
774 did_emsg = FALSE;
775
776 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000777 * 1. If repeating a line in a loop, get a line from lines_ga.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100778 * 2. If no line given: Get an allocated line with fgetline().
Bram Moolenaar071d4272004-06-13 20:20:40 +0000779 * 3. If a line is given: Make a copy, so we can mess with it.
780 */
781
782#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100783 // 1. If repeating, get a previous line from lines_ga.
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000784 if (cstack.cs_looplevel > 0 && current_line < lines_ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000785 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100786 // Each '|' separated command is stored separately in lines_ga, to
787 // be able to jump to it. Don't use next_cmdline now.
Bram Moolenaard23a8232018-02-10 18:45:26 +0100788 VIM_CLEAR(cmdline_copy);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000789
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100790 // Check if a function has returned or, unless it has an unclosed
791 // try conditional, aborted.
Bram Moolenaar05159a02005-02-26 23:04:13 +0000792 if (getline_is_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000793 {
Bram Moolenaar05159a02005-02-26 23:04:13 +0000794# ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000795 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +0000796 func_line_end(real_cookie);
797# endif
798 if (func_has_ended(real_cookie))
799 {
800 retval = FAIL;
801 break;
802 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000803 }
Bram Moolenaar05159a02005-02-26 23:04:13 +0000804#ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000805 else if (do_profiling == PROF_YES
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100806 && getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar05159a02005-02-26 23:04:13 +0000807 script_line_end();
808#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000809
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100810 // Check if a sourced file hit a ":finish" command.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100811 if (source_finished(fgetline, cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000812 {
813 retval = FAIL;
814 break;
815 }
816
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100817 // If breakpoints have been added/deleted need to check for it.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000818 if (breakpoint != NULL && dbg_tick != NULL
819 && *dbg_tick != debug_tick)
820 {
821 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100822 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100823 fname, SOURCING_LNUM);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000824 *dbg_tick = debug_tick;
825 }
826
827 next_cmdline = ((wcmd_T *)(lines_ga.ga_data))[current_line].line;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100828 SOURCING_LNUM = ((wcmd_T *)(lines_ga.ga_data))[current_line].lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000829
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100830 // Did we encounter a breakpoint?
Bram Moolenaar071d4272004-06-13 20:20:40 +0000831 if (breakpoint != NULL && *breakpoint != 0
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100832 && *breakpoint <= SOURCING_LNUM)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000833 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100834 dbg_breakpoint(fname, SOURCING_LNUM);
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100835 // Find next breakpoint.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000836 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100837 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100838 fname, SOURCING_LNUM);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000839 *dbg_tick = debug_tick;
840 }
Bram Moolenaar05159a02005-02-26 23:04:13 +0000841# ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000842 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +0000843 {
844 if (getline_is_func)
845 func_line_start(real_cookie);
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100846 else if (getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar05159a02005-02-26 23:04:13 +0000847 script_line_start();
848 }
849# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000850 }
851
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000852 if (cstack.cs_looplevel > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000853 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100854 // Inside a while/for loop we need to store the lines and use them
855 // again. Pass a different "fgetline" function to do_one_cmd()
856 // below, so that it stores lines in or reads them from
857 // "lines_ga". Makes it possible to define a function inside a
858 // while/for loop.
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000859 cmd_getline = get_loop_line;
860 cmd_cookie = (void *)&cmd_loop_cookie;
861 cmd_loop_cookie.lines_gap = &lines_ga;
862 cmd_loop_cookie.current_line = current_line;
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100863 cmd_loop_cookie.getline = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000864 cmd_loop_cookie.cookie = cookie;
865 cmd_loop_cookie.repeating = (current_line < lines_ga.ga_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000866 }
867 else
868 {
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100869 cmd_getline = fgetline;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000870 cmd_cookie = cookie;
871 }
872#endif
873
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100874 // 2. If no line given, get an allocated line with fgetline().
Bram Moolenaar071d4272004-06-13 20:20:40 +0000875 if (next_cmdline == NULL)
876 {
877 /*
878 * Need to set msg_didout for the first line after an ":if",
879 * otherwise the ":if" will be overwritten.
880 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100881 if (count == 1 && getline_equal(fgetline, cookie, getexline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000882 msg_didout = TRUE;
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100883 if (fgetline == NULL || (next_cmdline = fgetline(':', cookie,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000884#ifdef FEAT_EVAL
885 cstack.cs_idx < 0 ? 0 : (cstack.cs_idx + 1) * 2
886#else
887 0
888#endif
Bram Moolenaare96a2492019-06-25 04:12:16 +0200889 , TRUE)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000890 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100891 // Don't call wait_return for aborted command line. The NULL
892 // returned for the end of a sourced file or executed function
893 // doesn't do this.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000894 if (KeyTyped && !(flags & DOCMD_REPEAT))
895 need_wait_return = FALSE;
896 retval = FAIL;
897 break;
898 }
899 used_getline = TRUE;
900
901 /*
902 * Keep the first typed line. Clear it when more lines are typed.
903 */
904 if (flags & DOCMD_KEEPLINE)
905 {
906 vim_free(repeat_cmdline);
907 if (count == 0)
908 repeat_cmdline = vim_strsave(next_cmdline);
909 else
910 repeat_cmdline = NULL;
911 }
912 }
913
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100914 // 3. Make a copy of the command so we can mess with it.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000915 else if (cmdline_copy == NULL)
916 {
917 next_cmdline = vim_strsave(next_cmdline);
918 if (next_cmdline == NULL)
919 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100920 emsg(_(e_outofmem));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000921 retval = FAIL;
922 break;
923 }
924 }
925 cmdline_copy = next_cmdline;
926
927#ifdef FEAT_EVAL
928 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000929 * Save the current line when inside a ":while" or ":for", and when
930 * the command looks like a ":while" or ":for", because we may need it
931 * later. When there is a '|' and another command, it is stored
932 * separately, because we need to be able to jump back to it from an
933 * :endwhile/:endfor.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000934 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000935 if (current_line == lines_ga.ga_len
936 && (cstack.cs_looplevel || has_loop_cmd(next_cmdline)))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000937 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000938 if (store_loop_line(&lines_ga, next_cmdline) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000939 {
940 retval = FAIL;
941 break;
942 }
943 }
944 did_endif = FALSE;
945#endif
946
947 if (count++ == 0)
948 {
949 /*
950 * All output from the commands is put below each other, without
951 * waiting for a return. Don't do this when executing commands
952 * from a script or when being called recursive (e.g. for ":e
953 * +command file").
954 */
955 if (!(flags & DOCMD_NOWAIT) && !recursive)
956 {
957 msg_didout_before_start = msg_didout;
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100958 msg_didany = FALSE; // no output yet
Bram Moolenaar071d4272004-06-13 20:20:40 +0000959 msg_start();
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100960 msg_scroll = TRUE; // put messages below each other
961 ++no_wait_return; // don't wait for return until finished
Bram Moolenaar071d4272004-06-13 20:20:40 +0000962 ++RedrawingDisabled;
963 did_inc = TRUE;
964 }
965 }
966
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100967 if (p_verbose >= 15 && SOURCING_NAME != NULL)
968 msg_verbose_cmd(SOURCING_LNUM, cmdline_copy);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000969
970 /*
971 * 2. Execute one '|' separated command.
972 * do_one_cmd() will return NULL if there is no trailing '|'.
973 * "cmdline_copy" can change, e.g. for '%' and '#' expansion.
974 */
975 ++recursive;
976 next_cmdline = do_one_cmd(&cmdline_copy, flags & DOCMD_VERBOSE,
977#ifdef FEAT_EVAL
978 &cstack,
979#endif
980 cmd_getline, cmd_cookie);
981 --recursive;
982
983#ifdef FEAT_EVAL
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000984 if (cmd_cookie == (void *)&cmd_loop_cookie)
Bram Moolenaar217e1b82019-12-01 21:41:28 +0100985 // Use "current_line" from "cmd_loop_cookie", it may have been
986 // incremented when defining a function.
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000987 current_line = cmd_loop_cookie.current_line;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000988#endif
989
990 if (next_cmdline == NULL)
991 {
Bram Moolenaard23a8232018-02-10 18:45:26 +0100992 VIM_CLEAR(cmdline_copy);
Bram Moolenaard7663c22019-08-06 21:59:57 +0200993
Bram Moolenaar071d4272004-06-13 20:20:40 +0000994 /*
995 * If the command was typed, remember it for the ':' register.
996 * Do this AFTER executing the command to make :@: work.
997 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100998 if (getline_equal(fgetline, cookie, getexline)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000999 && new_last_cmdline != NULL)
1000 {
1001 vim_free(last_cmdline);
1002 last_cmdline = new_last_cmdline;
1003 new_last_cmdline = NULL;
1004 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001005 }
1006 else
1007 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001008 // need to copy the command after the '|' to cmdline_copy, for the
1009 // next do_one_cmd()
Bram Moolenaara7241f52008-06-24 20:39:31 +00001010 STRMOVE(cmdline_copy, next_cmdline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001011 next_cmdline = cmdline_copy;
1012 }
1013
1014
1015#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001016 // reset did_emsg for a function that is not aborted by an error
Bram Moolenaar071d4272004-06-13 20:20:40 +00001017 if (did_emsg && !force_abort
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001018 && getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001019 && !func_has_abort(real_cookie))
1020 did_emsg = FALSE;
1021
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001022 if (cstack.cs_looplevel > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001023 {
1024 ++current_line;
1025
1026 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001027 * An ":endwhile", ":endfor" and ":continue" is handled here.
1028 * If we were executing commands, jump back to the ":while" or
1029 * ":for".
1030 * If we were not executing commands, decrement cs_looplevel.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001031 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001032 if (cstack.cs_lflags & (CSL_HAD_CONT | CSL_HAD_ENDLOOP))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001033 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001034 cstack.cs_lflags &= ~(CSL_HAD_CONT | CSL_HAD_ENDLOOP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001035
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001036 // Jump back to the matching ":while" or ":for". Be careful
1037 // not to use a cs_line[] from an entry that isn't a ":while"
1038 // or ":for": It would make "current_line" invalid and can
1039 // cause a crash.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001040 if (!did_emsg && !got_int && !did_throw
1041 && cstack.cs_idx >= 0
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001042 && (cstack.cs_flags[cstack.cs_idx]
1043 & (CSF_WHILE | CSF_FOR))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001044 && cstack.cs_line[cstack.cs_idx] >= 0
1045 && (cstack.cs_flags[cstack.cs_idx] & CSF_ACTIVE))
1046 {
1047 current_line = cstack.cs_line[cstack.cs_idx];
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001048 // remember we jumped there
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001049 cstack.cs_lflags |= CSL_HAD_LOOP;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001050 line_breakcheck(); // check if CTRL-C typed
Bram Moolenaar071d4272004-06-13 20:20:40 +00001051
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001052 // Check for the next breakpoint at or after the ":while"
1053 // or ":for".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001054 if (breakpoint != NULL)
1055 {
1056 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001057 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001058 fname,
1059 ((wcmd_T *)lines_ga.ga_data)[current_line].lnum-1);
1060 *dbg_tick = debug_tick;
1061 }
1062 }
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001063 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001064 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001065 // can only get here with ":endwhile" or ":endfor"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001066 if (cstack.cs_idx >= 0)
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001067 rewind_conditionals(&cstack, cstack.cs_idx - 1,
1068 CSF_WHILE | CSF_FOR, &cstack.cs_looplevel);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001069 }
1070 }
1071
1072 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001073 * For a ":while" or ":for" we need to remember the line number.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001074 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001075 else if (cstack.cs_lflags & CSL_HAD_LOOP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001076 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001077 cstack.cs_lflags &= ~CSL_HAD_LOOP;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001078 cstack.cs_line[cstack.cs_idx] = current_line - 1;
1079 }
1080 }
1081
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001082 // Check for the next breakpoint after a watchexpression
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01001083 if (breakpoint != NULL && has_watchexpr())
1084 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001085 *breakpoint = dbg_find_breakpoint(FALSE, fname, SOURCING_LNUM);
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01001086 *dbg_tick = debug_tick;
1087 }
1088
Bram Moolenaar071d4272004-06-13 20:20:40 +00001089 /*
1090 * When not inside any ":while" loop, clear remembered lines.
1091 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001092 if (cstack.cs_looplevel == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001093 {
1094 if (lines_ga.ga_len > 0)
1095 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001096 SOURCING_LNUM =
Bram Moolenaar071d4272004-06-13 20:20:40 +00001097 ((wcmd_T *)lines_ga.ga_data)[lines_ga.ga_len - 1].lnum;
1098 free_cmdlines(&lines_ga);
1099 }
1100 current_line = 0;
1101 }
1102
1103 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001104 * A ":finally" makes did_emsg, got_int, and did_throw pending for
1105 * being restored at the ":endtry". Reset them here and set the
1106 * ACTIVE and FINALLY flags, so that the finally clause gets executed.
1107 * This includes the case where a missing ":endif", ":endwhile" or
1108 * ":endfor" was detected by the ":finally" itself.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001109 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001110 if (cstack.cs_lflags & CSL_HAD_FINA)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001111 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001112 cstack.cs_lflags &= ~CSL_HAD_FINA;
1113 report_make_pending(cstack.cs_pending[cstack.cs_idx]
1114 & (CSTP_ERROR | CSTP_INTERRUPT | CSTP_THROW),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001115 did_throw ? (void *)current_exception : NULL);
1116 did_emsg = got_int = did_throw = FALSE;
1117 cstack.cs_flags[cstack.cs_idx] |= CSF_ACTIVE | CSF_FINALLY;
1118 }
1119
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001120 // Update global "trylevel" for recursive calls to do_cmdline() from
1121 // within this loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001122 trylevel = initial_trylevel + cstack.cs_trylevel;
1123
1124 /*
Bram Moolenaarc1762cc2007-05-10 16:56:30 +00001125 * If the outermost try conditional (across function calls and sourced
Bram Moolenaar071d4272004-06-13 20:20:40 +00001126 * files) is aborted because of an error, an interrupt, or an uncaught
1127 * exception, cancel everything. If it is left normally, reset
1128 * force_abort to get the non-EH compatible abortion behavior for
1129 * the rest of the script.
1130 */
1131 if (trylevel == 0 && !did_emsg && !got_int && !did_throw)
1132 force_abort = FALSE;
1133
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001134 // Convert an interrupt to an exception if appropriate.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001135 (void)do_intthrow(&cstack);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001136#endif // FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001137
1138 }
1139 /*
1140 * Continue executing command lines when:
1141 * - no CTRL-C typed, no aborting error, no exception thrown or try
1142 * conditionals need to be checked for executing finally clauses or
1143 * catching an interrupt exception
1144 * - didn't get an error message or lines are not typed
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001145 * - there is a command after '|', inside a :if, :while, :for or :try, or
Bram Moolenaar071d4272004-06-13 20:20:40 +00001146 * looping for ":source" command or function call.
1147 */
1148 while (!((got_int
1149#ifdef FEAT_EVAL
1150 || (did_emsg && force_abort) || did_throw
1151#endif
1152 )
1153#ifdef FEAT_EVAL
1154 && cstack.cs_trylevel == 0
1155#endif
1156 )
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001157 && !(did_emsg
1158#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001159 // Keep going when inside try/catch, so that the error can be
1160 // deal with, except when it is a syntax error, it may cause
1161 // the :endtry to be missed.
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001162 && (cstack.cs_trylevel == 0 || did_emsg_syntax)
1163#endif
1164 && used_getline
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001165 && (getline_equal(fgetline, cookie, getexmodeline)
1166 || getline_equal(fgetline, cookie, getexline)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001167 && (next_cmdline != NULL
1168#ifdef FEAT_EVAL
1169 || cstack.cs_idx >= 0
1170#endif
1171 || (flags & DOCMD_REPEAT)));
1172
1173 vim_free(cmdline_copy);
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001174 did_emsg_syntax = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001175#ifdef FEAT_EVAL
1176 free_cmdlines(&lines_ga);
1177 ga_clear(&lines_ga);
1178
1179 if (cstack.cs_idx >= 0)
1180 {
1181 /*
1182 * If a sourced file or executed function ran to its end, report the
1183 * unclosed conditional.
1184 */
1185 if (!got_int && !did_throw
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001186 && ((getline_equal(fgetline, cookie, getsourceline)
1187 && !source_finished(fgetline, cookie))
1188 || (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001189 && !func_has_ended(real_cookie))))
1190 {
1191 if (cstack.cs_flags[cstack.cs_idx] & CSF_TRY)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001192 emsg(_(e_endtry));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001193 else if (cstack.cs_flags[cstack.cs_idx] & CSF_WHILE)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001194 emsg(_(e_endwhile));
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001195 else if (cstack.cs_flags[cstack.cs_idx] & CSF_FOR)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001196 emsg(_(e_endfor));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001197 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001198 emsg(_(e_endif));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001199 }
1200
1201 /*
1202 * Reset "trylevel" in case of a ":finish" or ":return" or a missing
1203 * ":endtry" in a sourced file or executed function. If the try
1204 * conditional is in its finally clause, ignore anything pending.
1205 * If it is in a catch clause, finish the caught exception.
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001206 * Also cleanup any "cs_forinfo" structures.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001207 */
1208 do
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001209 {
1210 int idx = cleanup_conditionals(&cstack, 0, TRUE);
1211
Bram Moolenaar89e5d682005-01-17 22:06:23 +00001212 if (idx >= 0)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001213 --idx; // remove try block not in its finally clause
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001214 rewind_conditionals(&cstack, idx, CSF_WHILE | CSF_FOR,
1215 &cstack.cs_looplevel);
1216 }
1217 while (cstack.cs_idx >= 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001218 trylevel = initial_trylevel;
1219 }
1220
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001221 // If a missing ":endtry", ":endwhile", ":endfor", or ":endif" or a memory
1222 // lack was reported above and the error message is to be converted to an
1223 // exception, do this now after rewinding the cstack.
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001224 do_errthrow(&cstack, getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001225 ? (char_u *)"endfunction" : (char_u *)NULL);
1226
1227 if (trylevel == 0)
1228 {
1229 /*
1230 * When an exception is being thrown out of the outermost try
1231 * conditional, discard the uncaught exception, disable the conversion
1232 * of interrupts or errors to exceptions, and ensure that no more
1233 * commands are executed.
1234 */
1235 if (did_throw)
1236 {
1237 void *p = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001238 struct msglist *messages = NULL, *next;
1239
1240 /*
1241 * If the uncaught exception is a user exception, report it as an
1242 * error. If it is an error exception, display the saved error
1243 * message now. For an interrupt exception, do nothing; the
1244 * interrupt message is given elsewhere.
1245 */
1246 switch (current_exception->type)
1247 {
1248 case ET_USER:
Bram Moolenaar9c13b352005-05-19 20:53:52 +00001249 vim_snprintf((char *)IObuff, IOSIZE,
1250 _("E605: Exception not caught: %s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001251 current_exception->value);
1252 p = vim_strsave(IObuff);
1253 break;
1254 case ET_ERROR:
1255 messages = current_exception->messages;
1256 current_exception->messages = NULL;
1257 break;
1258 case ET_INTERRUPT:
1259 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001260 }
1261
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001262 estack_push(ETYPE_EXCEPT, current_exception->throw_name,
1263 current_exception->throw_lnum);
Bram Moolenaare31ee862020-01-07 20:59:34 +01001264 ESTACK_CHECK_SETUP
Bram Moolenaar071d4272004-06-13 20:20:40 +00001265 current_exception->throw_name = NULL;
1266
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001267 discard_current_exception(); // uses IObuff if 'verbose'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001268 suppress_errthrow = TRUE;
1269 force_abort = TRUE;
1270
1271 if (messages != NULL)
1272 {
1273 do
1274 {
1275 next = messages->next;
1276 emsg(messages->msg);
1277 vim_free(messages->msg);
1278 vim_free(messages);
1279 messages = next;
1280 }
1281 while (messages != NULL);
1282 }
1283 else if (p != NULL)
1284 {
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01001285 emsg(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001286 vim_free(p);
1287 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001288 vim_free(SOURCING_NAME);
Bram Moolenaare31ee862020-01-07 20:59:34 +01001289 ESTACK_CHECK_NOW
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001290 estack_pop();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001291 }
1292
1293 /*
1294 * On an interrupt or an aborting error not converted to an exception,
1295 * disable the conversion of errors to exceptions. (Interrupts are not
1296 * converted any more, here.) This enables also the interrupt message
1297 * when force_abort is set and did_emsg unset in case of an interrupt
1298 * from a finally clause after an error.
1299 */
1300 else if (got_int || (did_emsg && force_abort))
1301 suppress_errthrow = TRUE;
1302 }
1303
1304 /*
1305 * The current cstack will be freed when do_cmdline() returns. An uncaught
1306 * exception will have to be rethrown in the previous cstack. If a function
1307 * has just returned or a script file was just finished and the previous
1308 * cstack belongs to the same function or, respectively, script file, it
1309 * will have to be checked for finally clauses to be executed due to the
1310 * ":return" or ":finish". This is done in do_one_cmd().
1311 */
1312 if (did_throw)
1313 need_rethrow = TRUE;
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001314 if ((getline_equal(fgetline, cookie, getsourceline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001315 && ex_nesting_level > source_level(real_cookie))
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001316 || (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001317 && ex_nesting_level > func_level(real_cookie) + 1))
1318 {
1319 if (!did_throw)
1320 check_cstack = TRUE;
1321 }
1322 else
1323 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001324 // When leaving a function, reduce nesting level.
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001325 if (getline_equal(fgetline, cookie, get_func_line))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001326 --ex_nesting_level;
1327 /*
1328 * Go to debug mode when returning from a function in which we are
1329 * single-stepping.
1330 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001331 if ((getline_equal(fgetline, cookie, getsourceline)
1332 || getline_equal(fgetline, cookie, get_func_line))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001333 && ex_nesting_level + 1 <= debug_break_level)
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001334 do_debug(getline_equal(fgetline, cookie, getsourceline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001335 ? (char_u *)_("End of sourced file")
1336 : (char_u *)_("End of function"));
1337 }
1338
1339 /*
1340 * Restore the exception environment (done after returning from the
1341 * debugger).
1342 */
1343 if (flags & DOCMD_EXCRESET)
Bram Moolenaared203462004-06-16 11:19:22 +00001344 restore_dbg_stuff(&debug_saved);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001345
1346 msg_list = saved_msg_list;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001347#endif // FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001348
1349 /*
1350 * If there was too much output to fit on the command line, ask the user to
1351 * hit return before redrawing the screen. With the ":global" command we do
1352 * this only once after the command is finished.
1353 */
1354 if (did_inc)
1355 {
1356 --RedrawingDisabled;
1357 --no_wait_return;
1358 msg_scroll = FALSE;
1359
1360 /*
1361 * When just finished an ":if"-":else" which was typed, no need to
1362 * wait for hit-return. Also for an error situation.
1363 */
1364 if (retval == FAIL
1365#ifdef FEAT_EVAL
1366 || (did_endif && KeyTyped && !did_emsg)
1367#endif
1368 )
1369 {
1370 need_wait_return = FALSE;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001371 msg_didany = FALSE; // don't wait when restarting edit
Bram Moolenaar071d4272004-06-13 20:20:40 +00001372 }
1373 else if (need_wait_return)
1374 {
1375 /*
1376 * The msg_start() above clears msg_didout. The wait_return we do
1377 * here should not overwrite the command that may be shown before
1378 * doing that.
1379 */
1380 msg_didout |= msg_didout_before_start;
1381 wait_return(FALSE);
1382 }
1383 }
1384
Bram Moolenaar2a942252012-11-28 23:03:07 +01001385#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001386 did_endif = FALSE; // in case do_cmdline used recursively
Bram Moolenaar2a942252012-11-28 23:03:07 +01001387#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001388 /*
1389 * Reset if_level, in case a sourced script file contains more ":if" than
1390 * ":endif" (could be ":if x | foo | endif").
1391 */
1392 if_level = 0;
Bram Moolenaar2e18a122012-11-28 19:10:54 +01001393#endif
Bram Moolenaard4ad0d42012-11-28 17:34:48 +01001394
Bram Moolenaar071d4272004-06-13 20:20:40 +00001395 --call_depth;
1396 return retval;
1397}
1398
1399#ifdef FEAT_EVAL
1400/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001401 * Obtain a line when inside a ":while" or ":for" loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001402 */
1403 static char_u *
Bram Moolenaare96a2492019-06-25 04:12:16 +02001404get_loop_line(int c, void *cookie, int indent, int do_concat)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001405{
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001406 struct loop_cookie *cp = (struct loop_cookie *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001407 wcmd_T *wp;
1408 char_u *line;
1409
1410 if (cp->current_line + 1 >= cp->lines_gap->ga_len)
1411 {
1412 if (cp->repeating)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001413 return NULL; // trying to read past ":endwhile"/":endfor"
Bram Moolenaar071d4272004-06-13 20:20:40 +00001414
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001415 // First time inside the ":while"/":for": get line normally.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001416 if (cp->getline == NULL)
Bram Moolenaare96a2492019-06-25 04:12:16 +02001417 line = getcmdline(c, 0L, indent, do_concat);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001418 else
Bram Moolenaare96a2492019-06-25 04:12:16 +02001419 line = cp->getline(c, cp->cookie, indent, do_concat);
Bram Moolenaard68071d2006-05-02 22:08:30 +00001420 if (line != NULL && store_loop_line(cp->lines_gap, line) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001421 ++cp->current_line;
1422
1423 return line;
1424 }
1425
1426 KeyTyped = FALSE;
1427 ++cp->current_line;
1428 wp = (wcmd_T *)(cp->lines_gap->ga_data) + cp->current_line;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001429 SOURCING_LNUM = wp->lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001430 return vim_strsave(wp->line);
1431}
1432
1433/*
1434 * Store a line in "gap" so that a ":while" loop can execute it again.
1435 */
1436 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001437store_loop_line(garray_T *gap, char_u *line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001438{
1439 if (ga_grow(gap, 1) == FAIL)
1440 return FAIL;
1441 ((wcmd_T *)(gap->ga_data))[gap->ga_len].line = vim_strsave(line);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001442 ((wcmd_T *)(gap->ga_data))[gap->ga_len].lnum = SOURCING_LNUM;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001443 ++gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001444 return OK;
1445}
1446
1447/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001448 * Free the lines stored for a ":while" or ":for" loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001449 */
1450 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001451free_cmdlines(garray_T *gap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001452{
1453 while (gap->ga_len > 0)
1454 {
1455 vim_free(((wcmd_T *)(gap->ga_data))[gap->ga_len - 1].line);
1456 --gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001457 }
1458}
1459#endif
1460
1461/*
Bram Moolenaar89d40322006-08-29 15:30:07 +00001462 * If "fgetline" is get_loop_line(), return TRUE if the getline it uses equals
1463 * "func". * Otherwise return TRUE when "fgetline" equals "func".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001464 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001465 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001466getline_equal(
Bram Moolenaare96a2492019-06-25 04:12:16 +02001467 char_u *(*fgetline)(int, void *, int, int),
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001468 void *cookie UNUSED, // argument for fgetline()
Bram Moolenaare96a2492019-06-25 04:12:16 +02001469 char_u *(*func)(int, void *, int, int))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001470{
1471#ifdef FEAT_EVAL
Bram Moolenaare96a2492019-06-25 04:12:16 +02001472 char_u *(*gp)(int, void *, int, int);
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001473 struct loop_cookie *cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001474
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001475 // When "fgetline" is "get_loop_line()" use the "cookie" to find the
1476 // function that's originally used to obtain the lines. This may be
1477 // nested several levels.
Bram Moolenaar89d40322006-08-29 15:30:07 +00001478 gp = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001479 cp = (struct loop_cookie *)cookie;
1480 while (gp == get_loop_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001481 {
1482 gp = cp->getline;
1483 cp = cp->cookie;
1484 }
1485 return gp == func;
1486#else
Bram Moolenaar89d40322006-08-29 15:30:07 +00001487 return fgetline == func;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001488#endif
1489}
1490
Bram Moolenaar071d4272004-06-13 20:20:40 +00001491/*
Bram Moolenaar89d40322006-08-29 15:30:07 +00001492 * If "fgetline" is get_loop_line(), return the cookie used by the original
Bram Moolenaar071d4272004-06-13 20:20:40 +00001493 * getline function. Otherwise return "cookie".
1494 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001495 void *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001496getline_cookie(
Bram Moolenaare96a2492019-06-25 04:12:16 +02001497 char_u *(*fgetline)(int, void *, int, int) UNUSED,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001498 void *cookie) // argument for fgetline()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001499{
Bram Moolenaar13505972019-01-24 15:04:48 +01001500#ifdef FEAT_EVAL
Bram Moolenaare96a2492019-06-25 04:12:16 +02001501 char_u *(*gp)(int, void *, int, int);
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001502 struct loop_cookie *cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001503
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001504 // When "fgetline" is "get_loop_line()" use the "cookie" to find the
1505 // cookie that's originally used to obtain the lines. This may be nested
1506 // several levels.
Bram Moolenaar89d40322006-08-29 15:30:07 +00001507 gp = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001508 cp = (struct loop_cookie *)cookie;
1509 while (gp == get_loop_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001510 {
1511 gp = cp->getline;
1512 cp = cp->cookie;
1513 }
1514 return cp;
Bram Moolenaar13505972019-01-24 15:04:48 +01001515#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001516 return cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001517#endif
Bram Moolenaar13505972019-01-24 15:04:48 +01001518}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001519
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001520
1521/*
1522 * Helper function to apply an offset for buffer commands, i.e. ":bdelete",
1523 * ":bwipeout", etc.
1524 * Returns the buffer number.
1525 */
1526 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001527compute_buffer_local_count(int addr_type, int lnum, int offset)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001528{
1529 buf_T *buf;
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001530 buf_T *nextbuf;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001531 int count = offset;
1532
1533 buf = firstbuf;
1534 while (buf->b_next != NULL && buf->b_fnum < lnum)
1535 buf = buf->b_next;
1536 while (count != 0)
1537 {
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001538 count += (offset < 0) ? 1 : -1;
1539 nextbuf = (offset < 0) ? buf->b_prev : buf->b_next;
1540 if (nextbuf == NULL)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001541 break;
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001542 buf = nextbuf;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001543 if (addr_type == ADDR_LOADED_BUFFERS)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001544 // skip over unloaded buffers
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001545 while (buf->b_ml.ml_mfp == NULL)
1546 {
1547 nextbuf = (offset < 0) ? buf->b_prev : buf->b_next;
1548 if (nextbuf == NULL)
1549 break;
1550 buf = nextbuf;
1551 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001552 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001553 // we might have gone too far, last buffer is not loadedd
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001554 if (addr_type == ADDR_LOADED_BUFFERS)
1555 while (buf->b_ml.ml_mfp == NULL)
1556 {
1557 nextbuf = (offset >= 0) ? buf->b_prev : buf->b_next;
1558 if (nextbuf == NULL)
1559 break;
1560 buf = nextbuf;
1561 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001562 return buf->b_fnum;
1563}
1564
Bram Moolenaarb7316892019-05-01 18:08:42 +02001565/*
1566 * Return the window number of "win".
1567 * When "win" is NULL return the number of windows.
1568 */
Bram Moolenaarf240e182014-11-27 18:33:02 +01001569 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001570current_win_nr(win_T *win)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001571{
1572 win_T *wp;
1573 int nr = 0;
1574
Bram Moolenaar29323592016-07-24 22:04:11 +02001575 FOR_ALL_WINDOWS(wp)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001576 {
1577 ++nr;
1578 if (wp == win)
1579 break;
1580 }
1581 return nr;
1582}
1583
1584 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001585current_tab_nr(tabpage_T *tab)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001586{
1587 tabpage_T *tp;
1588 int nr = 0;
1589
Bram Moolenaar29323592016-07-24 22:04:11 +02001590 FOR_ALL_TABPAGES(tp)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001591 {
1592 ++nr;
1593 if (tp == tab)
1594 break;
1595 }
1596 return nr;
1597}
1598
1599# define CURRENT_WIN_NR current_win_nr(curwin)
1600# define LAST_WIN_NR current_win_nr(NULL)
1601# define CURRENT_TAB_NR current_tab_nr(curtab)
1602# define LAST_TAB_NR current_tab_nr(NULL)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001603
Bram Moolenaar071d4272004-06-13 20:20:40 +00001604/*
1605 * Execute one Ex command.
1606 *
1607 * If 'sourcing' is TRUE, the command will be included in the error message.
1608 *
1609 * 1. skip comment lines and leading space
1610 * 2. handle command modifiers
Bram Moolenaar1c40a662014-11-27 16:38:11 +01001611 * 3. find the command
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001612 * 4. parse range
Bram Moolenaar1c40a662014-11-27 16:38:11 +01001613 * 5. Parse the command.
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001614 * 6. parse arguments
1615 * 7. switch on command name
Bram Moolenaar071d4272004-06-13 20:20:40 +00001616 *
Bram Moolenaar89d40322006-08-29 15:30:07 +00001617 * Note: "fgetline" can be NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001618 *
1619 * This function may be called recursively!
1620 */
1621#if (_MSC_VER == 1200)
1622/*
Bram Moolenaared203462004-06-16 11:19:22 +00001623 * Avoid optimisation bug in VC++ version 6.0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001624 */
Bram Moolenaar281bdce2005-01-25 21:53:18 +00001625 #pragma optimize( "g", off )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001626#endif
1627 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001628do_one_cmd(
Bram Moolenaarddef1292019-12-16 17:10:33 +01001629 char_u **cmdlinep,
1630 int sourcing,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001631#ifdef FEAT_EVAL
Bram Moolenaarddef1292019-12-16 17:10:33 +01001632 cstack_T *cstack,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001633#endif
Bram Moolenaarddef1292019-12-16 17:10:33 +01001634 char_u *(*fgetline)(int, void *, int, int),
1635 void *cookie) // argument for fgetline()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001636{
Bram Moolenaarddef1292019-12-16 17:10:33 +01001637 char_u *p;
1638 linenr_T lnum;
1639 long n;
1640 char *errormsg = NULL; // error message
1641 char_u *after_modifier = NULL;
1642 exarg_T ea; // Ex command arguments
1643 int save_msg_scroll = msg_scroll;
1644 cmdmod_T save_cmdmod;
1645 int save_reg_executing = reg_executing;
1646 int ni; // set when Not Implemented
1647 char_u *cmd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001648
1649 vim_memset(&ea, 0, sizeof(ea));
1650 ea.line1 = 1;
1651 ea.line2 = 1;
1652#ifdef FEAT_EVAL
1653 ++ex_nesting_level;
1654#endif
1655
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001656 // When the last file has not been edited :q has to be typed twice.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001657 if (quitmore
1658#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001659 // avoid that a function call in 'statusline' does this
Bram Moolenaar89d40322006-08-29 15:30:07 +00001660 && !getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001661#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001662 // avoid that an autocommand, e.g. QuitPre, does this
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001663 && !getline_equal(fgetline, cookie, getnextac))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001664 --quitmore;
1665
1666 /*
1667 * Reset browse, confirm, etc.. They are restored when returning, for
1668 * recursive calls.
1669 */
1670 save_cmdmod = cmdmod;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001671
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001672 // "#!anything" is handled like a comment.
Bram Moolenaarcbb37ad2006-08-16 15:04:21 +00001673 if ((*cmdlinep)[0] == '#' && (*cmdlinep)[1] == '!')
1674 goto doend;
1675
Bram Moolenaar4facea32019-10-12 20:17:40 +02001676 if (p_verbose >= 16)
1677 msg_verbose_cmd(0, *cmdlinep);
1678
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02001679/*
1680 * 1. Skip comment lines and leading white space and colons.
1681 * 2. Handle command modifiers.
1682 */
1683 // The "ea" structure holds the arguments that can be used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001684 ea.cmd = *cmdlinep;
Bram Moolenaareffed932018-08-14 13:38:17 +02001685 ea.cmdlinep = cmdlinep;
1686 ea.getline = fgetline;
1687 ea.cookie = cookie;
1688#ifdef FEAT_EVAL
1689 ea.cstack = cstack;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001690#endif
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02001691 if (parse_command_modifiers(&ea, &errormsg, FALSE) == FAIL)
Bram Moolenaareffed932018-08-14 13:38:17 +02001692 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001693
Bram Moolenaar7b668e82016-08-23 23:51:21 +02001694 after_modifier = ea.cmd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001695
1696#ifdef FEAT_EVAL
1697 ea.skip = did_emsg || got_int || did_throw || (cstack->cs_idx >= 0
1698 && !(cstack->cs_flags[cstack->cs_idx] & CSF_ACTIVE));
1699#else
1700 ea.skip = (if_level > 0);
1701#endif
1702
Bram Moolenaar071d4272004-06-13 20:20:40 +00001703/*
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001704 * 3. Skip over the range to find the command. Let "p" point to after it.
1705 *
1706 * We need the command to know what kind of range it uses.
1707 */
1708 cmd = ea.cmd;
1709 ea.cmd = skip_range(ea.cmd, NULL);
1710 if (*ea.cmd == '*' && vim_strchr(p_cpo, CPO_STAR) == NULL)
1711 ea.cmd = skipwhite(ea.cmd + 1);
1712 p = find_command(&ea, NULL);
1713
Bram Moolenaar7feb35e2018-08-21 17:49:54 +02001714#ifdef FEAT_EVAL
1715# ifdef FEAT_PROFILE
1716 // Count this line for profiling if skip is TRUE.
1717 if (do_profiling == PROF_YES
1718 && (!ea.skip || cstack->cs_idx == 0 || (cstack->cs_idx > 0
1719 && (cstack->cs_flags[cstack->cs_idx - 1] & CSF_ACTIVE))))
1720 {
1721 int skip = did_emsg || got_int || did_throw;
1722
1723 if (ea.cmdidx == CMD_catch)
1724 skip = !skip && !(cstack->cs_idx >= 0
1725 && (cstack->cs_flags[cstack->cs_idx] & CSF_THROWN)
1726 && !(cstack->cs_flags[cstack->cs_idx] & CSF_CAUGHT));
1727 else if (ea.cmdidx == CMD_else || ea.cmdidx == CMD_elseif)
1728 skip = skip || !(cstack->cs_idx >= 0
1729 && !(cstack->cs_flags[cstack->cs_idx]
1730 & (CSF_ACTIVE | CSF_TRUE)));
1731 else if (ea.cmdidx == CMD_finally)
1732 skip = FALSE;
1733 else if (ea.cmdidx != CMD_endif
1734 && ea.cmdidx != CMD_endfor
1735 && ea.cmdidx != CMD_endtry
1736 && ea.cmdidx != CMD_endwhile)
1737 skip = ea.skip;
1738
1739 if (!skip)
1740 {
1741 if (getline_equal(fgetline, cookie, get_func_line))
1742 func_line_exec(getline_cookie(fgetline, cookie));
1743 else if (getline_equal(fgetline, cookie, getsourceline))
1744 script_line_exec();
1745 }
1746 }
1747# endif
1748
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001749 // May go to debug mode. If this happens and the ">quit" debug command is
1750 // used, throw an interrupt exception and skip the next command.
Bram Moolenaar7feb35e2018-08-21 17:49:54 +02001751 dbg_check_breakpoint(&ea);
1752 if (!ea.skip && got_int)
1753 {
1754 ea.skip = TRUE;
1755 (void)do_intthrow(cstack);
1756 }
1757#endif
1758
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001759/*
1760 * 4. parse a range specifier of the form: addr [,addr] [;addr] ..
Bram Moolenaar071d4272004-06-13 20:20:40 +00001761 *
1762 * where 'addr' is:
1763 *
1764 * % (entire file)
1765 * $ [+-NUM]
1766 * 'x [+-NUM] (where x denotes a currently defined mark)
1767 * . [+-NUM]
1768 * [+-NUM]..
1769 * NUM
1770 *
1771 * The ea.cmd pointer is updated to point to the first character following the
1772 * range spec. If an initial address is found, but no second, the upper bound
1773 * is equal to the lower.
1774 */
1775
Bram Moolenaar25190db2019-05-04 15:05:28 +02001776 // ea.addr_type for user commands is set by find_ucmd
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01001777 if (!IS_USER_CMDIDX(ea.cmdidx))
1778 {
1779 if (ea.cmdidx != CMD_SIZE)
1780 ea.addr_type = cmdnames[(int)ea.cmdidx].cmd_addr_type;
1781 else
1782 ea.addr_type = ADDR_LINES;
1783
Bram Moolenaar25190db2019-05-04 15:05:28 +02001784 // :wincmd range depends on the argument.
Bram Moolenaar164f3262015-01-14 21:22:01 +01001785 if (ea.cmdidx == CMD_wincmd && p != NULL)
1786 get_wincmd_addr_type(skipwhite(p), &ea);
Bram Moolenaar25190db2019-05-04 15:05:28 +02001787#ifdef FEAT_QUICKFIX
1788 // :.cc in quickfix window uses line number
1789 if ((ea.cmdidx == CMD_cc || ea.cmdidx == CMD_ll) && bt_quickfix(curbuf))
1790 ea.addr_type = ADDR_OTHER;
1791#endif
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01001792 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001793
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01001794 ea.cmd = cmd;
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02001795 if (parse_cmd_address(&ea, &errormsg, FALSE) == FAIL)
Bram Moolenaaree8415b2018-08-10 23:13:12 +02001796 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001797
Bram Moolenaar071d4272004-06-13 20:20:40 +00001798/*
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001799 * 5. Parse the command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001800 */
1801
1802 /*
1803 * Skip ':' and any white space
1804 */
1805 ea.cmd = skipwhite(ea.cmd);
1806 while (*ea.cmd == ':')
1807 ea.cmd = skipwhite(ea.cmd + 1);
1808
1809 /*
1810 * If we got a line, but no command, then go to the line.
1811 * If we find a '|' or '\n' we set ea.nextcmd.
1812 */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001813 if (*ea.cmd == NUL || *ea.cmd == '"'
1814 || (ea.nextcmd = check_nextcmd(ea.cmd)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001815 {
1816 /*
1817 * strange vi behaviour:
1818 * ":3" jumps to line 3
1819 * ":3|..." prints line 3
1820 * ":|" prints current line
1821 */
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001822 if (ea.skip) // skip this if inside :if
Bram Moolenaar071d4272004-06-13 20:20:40 +00001823 goto doend;
Bram Moolenaardf177f62005-02-22 08:39:57 +00001824 if (*ea.cmd == '|' || (exmode_active && ea.line1 != ea.line2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001825 {
1826 ea.cmdidx = CMD_print;
Bram Moolenaar8071cb22019-07-12 17:58:01 +02001827 ea.argt = EX_RANGE+EX_COUNT+EX_TRLBAR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001828 if ((errormsg = invalid_range(&ea)) == NULL)
1829 {
1830 correct_range(&ea);
1831 ex_print(&ea);
1832 }
1833 }
1834 else if (ea.addr_count != 0)
1835 {
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00001836 if (ea.line2 > curbuf->b_ml.ml_line_count)
1837 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001838 // With '-' in 'cpoptions' a line number past the file is an
1839 // error, otherwise put it at the end of the file.
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00001840 if (vim_strchr(p_cpo, CPO_MINUS) != NULL)
1841 ea.line2 = -1;
1842 else
1843 ea.line2 = curbuf->b_ml.ml_line_count;
1844 }
1845
1846 if (ea.line2 < 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001847 errormsg = _(e_invrange);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001848 else
1849 {
1850 if (ea.line2 == 0)
1851 curwin->w_cursor.lnum = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001852 else
1853 curwin->w_cursor.lnum = ea.line2;
1854 beginline(BL_SOL | BL_FIX);
1855 }
1856 }
1857 goto doend;
1858 }
1859
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001860 // If this looks like an undefined user command and there are CmdUndefined
1861 // autocommands defined, trigger the matching autocommands.
Bram Moolenaard5005162014-08-22 23:05:54 +02001862 if (p != NULL && ea.cmdidx == CMD_SIZE && !ea.skip
1863 && ASCII_ISUPPER(*ea.cmd)
1864 && has_cmdundefined())
1865 {
Bram Moolenaard5005162014-08-22 23:05:54 +02001866 int ret;
1867
Bram Moolenaar5a31b462014-08-23 14:16:20 +02001868 p = ea.cmd;
Bram Moolenaard5005162014-08-22 23:05:54 +02001869 while (ASCII_ISALNUM(*p))
1870 ++p;
Bram Moolenaar120f4a82014-09-09 12:22:06 +02001871 p = vim_strnsave(ea.cmd, (int)(p - ea.cmd));
Bram Moolenaard5005162014-08-22 23:05:54 +02001872 ret = apply_autocmds(EVENT_CMDUNDEFINED, p, p, TRUE, NULL);
1873 vim_free(p);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001874 // If the autocommands did something and didn't cause an error, try
1875 // finding the command again.
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001876 p = (ret
1877#ifdef FEAT_EVAL
1878 && !aborting()
Bram Moolenaard5005162014-08-22 23:05:54 +02001879#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001880 ) ? find_command(&ea, NULL) : ea.cmd;
1881 }
Bram Moolenaard5005162014-08-22 23:05:54 +02001882
Bram Moolenaar071d4272004-06-13 20:20:40 +00001883 if (p == NULL)
1884 {
1885 if (!ea.skip)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001886 errormsg = _("E464: Ambiguous use of user-defined command");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001887 goto doend;
1888 }
Bram Moolenaarac9fb182019-04-27 13:04:13 +02001889 // Check for wrong commands.
Bram Moolenaar6f9a4762017-06-22 20:39:17 +02001890 if (*p == '!' && ea.cmd[1] == 0151 && ea.cmd[0] == 78
1891 && !IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001892 {
1893 errormsg = uc_fun_cmd();
1894 goto doend;
1895 }
Bram Moolenaarac9fb182019-04-27 13:04:13 +02001896
Bram Moolenaar071d4272004-06-13 20:20:40 +00001897 if (ea.cmdidx == CMD_SIZE)
1898 {
1899 if (!ea.skip)
1900 {
1901 STRCPY(IObuff, _("E492: Not an editor command"));
1902 if (!sourcing)
Bram Moolenaar7b668e82016-08-23 23:51:21 +02001903 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001904 // If the modifier was parsed OK the error must be in the
1905 // following command
Bram Moolenaar7b668e82016-08-23 23:51:21 +02001906 if (after_modifier != NULL)
1907 append_command(after_modifier);
1908 else
1909 append_command(*cmdlinep);
1910 }
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001911 errormsg = (char *)IObuff;
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001912 did_emsg_syntax = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001913 }
1914 goto doend;
1915 }
1916
Bram Moolenaar958636c2014-10-21 20:01:58 +02001917 ni = (!IS_USER_CMDIDX(ea.cmdidx)
1918 && (cmdnames[ea.cmdidx].cmd_func == ex_ni
Bram Moolenaar7bb75552007-07-16 18:39:49 +00001919#ifdef HAVE_EX_SCRIPT_NI
1920 || cmdnames[ea.cmdidx].cmd_func == ex_script_ni
1921#endif
1922 ));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001923
1924#ifndef FEAT_EVAL
1925 /*
1926 * When the expression evaluation is disabled, recognize the ":if" and
1927 * ":endif" commands and ignore everything in between it.
1928 */
1929 if (ea.cmdidx == CMD_if)
1930 ++if_level;
1931 if (if_level)
1932 {
1933 if (ea.cmdidx == CMD_endif)
1934 --if_level;
1935 goto doend;
1936 }
1937
1938#endif
1939
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001940 // forced commands
Bram Moolenaar196b3b02008-06-20 16:51:41 +00001941 if (*p == '!' && ea.cmdidx != CMD_substitute
1942 && ea.cmdidx != CMD_smagic && ea.cmdidx != CMD_snomagic)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001943 {
1944 ++p;
1945 ea.forceit = TRUE;
1946 }
1947 else
1948 ea.forceit = FALSE;
1949
1950/*
Bram Moolenaarb7316892019-05-01 18:08:42 +02001951 * 6. Parse arguments. Then check for errors.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001952 */
Bram Moolenaar958636c2014-10-21 20:01:58 +02001953 if (!IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001954 ea.argt = (long)cmdnames[(int)ea.cmdidx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001955
1956 if (!ea.skip)
1957 {
1958#ifdef HAVE_SANDBOX
Bram Moolenaar8071cb22019-07-12 17:58:01 +02001959 if (sandbox != 0 && !(ea.argt & EX_SBOXOK))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001960 {
Bram Moolenaar8c62a082019-02-08 14:34:10 +01001961 // Command not allowed in sandbox.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001962 errormsg = _(e_sandbox);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001963 goto doend;
1964 }
1965#endif
Bram Moolenaar8071cb22019-07-12 17:58:01 +02001966 if (restricted != 0 && (ea.argt & EX_RESTRICT))
Bram Moolenaar8c62a082019-02-08 14:34:10 +01001967 {
1968 errormsg = _("E981: Command not allowed in rvim");
1969 goto doend;
1970 }
Bram Moolenaar8071cb22019-07-12 17:58:01 +02001971 if (!curbuf->b_p_ma && (ea.argt & EX_MODIFY))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001972 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001973 // Command not allowed in non-'modifiable' buffer
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001974 errormsg = _(e_modifiable);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001975 goto doend;
1976 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00001977
Bram Moolenaar8071cb22019-07-12 17:58:01 +02001978 if (text_locked() && !(ea.argt & EX_CMDWIN)
Bram Moolenaar958636c2014-10-21 20:01:58 +02001979 && !IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001980 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001981 // Command not allowed when editing the command line.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001982 errormsg = _(get_text_locked_msg());
Bram Moolenaar071d4272004-06-13 20:20:40 +00001983 goto doend;
1984 }
Bram Moolenaar379fb762018-08-30 15:58:28 +02001985
Bram Moolenaar217e1b82019-12-01 21:41:28 +01001986 // Disallow editing another buffer when "curbuf_lock" is set.
1987 // Do allow ":checktime" (it is postponed).
1988 // Do allow ":edit" (check for an argument later).
1989 // Do allow ":file" with no arguments (check for an argument later).
Bram Moolenaar8071cb22019-07-12 17:58:01 +02001990 if (!(ea.argt & EX_CMDWIN)
Bram Moolenaar5555acc2006-04-07 21:33:12 +00001991 && ea.cmdidx != CMD_checktime
Bram Moolenaar379fb762018-08-30 15:58:28 +02001992 && ea.cmdidx != CMD_edit
1993 && ea.cmdidx != CMD_file
Bram Moolenaar958636c2014-10-21 20:01:58 +02001994 && !IS_USER_CMDIDX(ea.cmdidx)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001995 && curbuf_locked())
1996 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001997
Bram Moolenaar8071cb22019-07-12 17:58:01 +02001998 if (!ni && !(ea.argt & EX_RANGE) && ea.addr_count > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001999 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002000 // no range allowed
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002001 errormsg = _(e_norange);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002002 goto doend;
2003 }
2004 }
2005
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002006 if (!ni && !(ea.argt & EX_BANG) && ea.forceit) // no <!> allowed
Bram Moolenaar071d4272004-06-13 20:20:40 +00002007 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002008 errormsg = _(e_nobang);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002009 goto doend;
2010 }
2011
2012 /*
2013 * Don't complain about the range if it is not used
2014 * (could happen if line_count is accidentally set to 0).
2015 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002016 if (!ea.skip && !ni && (ea.argt & EX_RANGE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002017 {
2018 /*
2019 * If the range is backwards, ask for confirmation and, if given, swap
2020 * ea.line1 & ea.line2 so it's forwards again.
2021 * When global command is busy, don't ask, will fail below.
2022 */
2023 if (!global_busy && ea.line1 > ea.line2)
2024 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002025 if (msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002026 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002027 if (sourcing || exmode_active)
2028 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002029 errormsg = _("E493: Backwards range given");
Bram Moolenaara5792f52005-11-23 21:25:05 +00002030 goto doend;
2031 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002032 if (ask_yesno((char_u *)
2033 _("Backwards range given, OK to swap"), FALSE) != 'y')
Bram Moolenaara5792f52005-11-23 21:25:05 +00002034 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002035 }
2036 lnum = ea.line1;
2037 ea.line1 = ea.line2;
2038 ea.line2 = lnum;
2039 }
2040 if ((errormsg = invalid_range(&ea)) != NULL)
2041 goto doend;
2042 }
2043
Bram Moolenaarb7316892019-05-01 18:08:42 +02002044 if ((ea.addr_type == ADDR_OTHER) && ea.addr_count == 0)
2045 // default is 1, not cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00002046 ea.line2 = 1;
2047
2048 correct_range(&ea);
2049
2050#ifdef FEAT_FOLDING
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002051 if (((ea.argt & EX_WHOLEFOLD) || ea.addr_count >= 2) && !global_busy
Bram Moolenaara3306952016-01-02 21:41:06 +01002052 && ea.addr_type == ADDR_LINES)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002053 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002054 // Put the first line at the start of a closed fold, put the last line
2055 // at the end of a closed fold.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002056 (void)hasFolding(ea.line1, &ea.line1, NULL);
2057 (void)hasFolding(ea.line2, NULL, &ea.line2);
2058 }
2059#endif
2060
2061#ifdef FEAT_QUICKFIX
2062 /*
Bram Moolenaar86b68352004-12-27 21:59:20 +00002063 * For the ":make" and ":grep" commands we insert the 'makeprg'/'grepprg'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002064 * option here, so things like % get expanded.
2065 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002066 p = replace_makeprg(&ea, p, cmdlinep);
2067 if (p == NULL)
2068 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002069#endif
2070
2071 /*
2072 * Skip to start of argument.
2073 * Don't do this for the ":!" command, because ":!! -l" needs the space.
2074 */
2075 if (ea.cmdidx == CMD_bang)
2076 ea.arg = p;
2077 else
2078 ea.arg = skipwhite(p);
2079
Bram Moolenaar379fb762018-08-30 15:58:28 +02002080 // ":file" cannot be run with an argument when "curbuf_lock" is set
2081 if (ea.cmdidx == CMD_file && *ea.arg != NUL && curbuf_locked())
2082 goto doend;
2083
Bram Moolenaar071d4272004-06-13 20:20:40 +00002084 /*
2085 * Check for "++opt=val" argument.
2086 * Must be first, allow ":w ++enc=utf8 !cmd"
2087 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002088 if (ea.argt & EX_ARGOPT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002089 while (ea.arg[0] == '+' && ea.arg[1] == '+')
2090 if (getargopt(&ea) == FAIL && !ni)
2091 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002092 errormsg = _(e_invarg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002093 goto doend;
2094 }
2095
2096 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
2097 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002098 if (*ea.arg == '>') // append
Bram Moolenaar071d4272004-06-13 20:20:40 +00002099 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002100 if (*++ea.arg != '>') // typed wrong
Bram Moolenaar071d4272004-06-13 20:20:40 +00002101 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002102 errormsg = _("E494: Use w or w>>");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002103 goto doend;
2104 }
2105 ea.arg = skipwhite(ea.arg + 1);
2106 ea.append = TRUE;
2107 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002108 else if (*ea.arg == '!' && ea.cmdidx == CMD_write) // :w !filter
Bram Moolenaar071d4272004-06-13 20:20:40 +00002109 {
2110 ++ea.arg;
2111 ea.usefilter = TRUE;
2112 }
2113 }
2114
2115 if (ea.cmdidx == CMD_read)
2116 {
2117 if (ea.forceit)
2118 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002119 ea.usefilter = TRUE; // :r! filter if ea.forceit
Bram Moolenaar071d4272004-06-13 20:20:40 +00002120 ea.forceit = FALSE;
2121 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002122 else if (*ea.arg == '!') // :r !filter
Bram Moolenaar071d4272004-06-13 20:20:40 +00002123 {
2124 ++ea.arg;
2125 ea.usefilter = TRUE;
2126 }
2127 }
2128
2129 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
2130 {
2131 ea.amount = 1;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002132 while (*ea.arg == *ea.cmd) // count number of '>' or '<'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002133 {
2134 ++ea.arg;
2135 ++ea.amount;
2136 }
2137 ea.arg = skipwhite(ea.arg);
2138 }
2139
2140 /*
2141 * Check for "+command" argument, before checking for next command.
2142 * Don't do this for ":read !cmd" and ":write !cmd".
2143 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002144 if ((ea.argt & EX_CMDARG) && !ea.usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002145 ea.do_ecmd_cmd = getargcmd(&ea.arg);
2146
2147 /*
2148 * Check for '|' to separate commands and '"' to start comments.
2149 * Don't do this for ":read !cmd" and ":write !cmd".
2150 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002151 if ((ea.argt & EX_TRLBAR) && !ea.usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002152 separate_nextcmd(&ea);
2153
2154 /*
2155 * Check for <newline> to end a shell command.
Bram Moolenaardf177f62005-02-22 08:39:57 +00002156 * Also do this for ":read !cmd", ":write !cmd" and ":global".
2157 * Any others?
Bram Moolenaar071d4272004-06-13 20:20:40 +00002158 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00002159 else if (ea.cmdidx == CMD_bang
Bram Moolenaar67883b42017-07-27 22:57:00 +02002160 || ea.cmdidx == CMD_terminal
Bram Moolenaardf177f62005-02-22 08:39:57 +00002161 || ea.cmdidx == CMD_global
2162 || ea.cmdidx == CMD_vglobal
2163 || ea.usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002164 {
2165 for (p = ea.arg; *p; ++p)
2166 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002167 // Remove one backslash before a newline, so that it's possible to
2168 // pass a newline to the shell and also a newline that is preceded
2169 // with a backslash. This makes it impossible to end a shell
2170 // command in a backslash, but that doesn't appear useful.
2171 // Halving the number of backslashes is incompatible with previous
2172 // versions.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002173 if (*p == '\\' && p[1] == '\n')
Bram Moolenaara7241f52008-06-24 20:39:31 +00002174 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002175 else if (*p == '\n')
2176 {
2177 ea.nextcmd = p + 1;
2178 *p = NUL;
2179 break;
2180 }
2181 }
2182 }
2183
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002184 if ((ea.argt & EX_DFLALL) && ea.addr_count == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002185 {
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002186 buf_T *buf;
2187
Bram Moolenaar071d4272004-06-13 20:20:40 +00002188 ea.line1 = 1;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002189 switch (ea.addr_type)
2190 {
2191 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02002192 case ADDR_OTHER:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002193 ea.line2 = curbuf->b_ml.ml_line_count;
2194 break;
2195 case ADDR_LOADED_BUFFERS:
2196 buf = firstbuf;
2197 while (buf->b_next != NULL && buf->b_ml.ml_mfp == NULL)
2198 buf = buf->b_next;
2199 ea.line1 = buf->b_fnum;
2200 buf = lastbuf;
2201 while (buf->b_prev != NULL && buf->b_ml.ml_mfp == NULL)
2202 buf = buf->b_prev;
2203 ea.line2 = buf->b_fnum;
2204 break;
2205 case ADDR_BUFFERS:
2206 ea.line1 = firstbuf->b_fnum;
2207 ea.line2 = lastbuf->b_fnum;
2208 break;
2209 case ADDR_WINDOWS:
2210 ea.line2 = LAST_WIN_NR;
2211 break;
2212 case ADDR_TABS:
2213 ea.line2 = LAST_TAB_NR;
2214 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01002215 case ADDR_TABS_RELATIVE:
2216 ea.line2 = 1;
2217 break;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002218 case ADDR_ARGUMENTS:
2219 if (ARGCOUNT == 0)
2220 ea.line1 = ea.line2 = 0;
2221 else
2222 ea.line2 = ARGCOUNT;
2223 break;
Bram Moolenaar25190db2019-05-04 15:05:28 +02002224 case ADDR_QUICKFIX_VALID:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02002225#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02002226 ea.line2 = qf_get_valid_size(&ea);
Bram Moolenaaraa23b372015-09-08 18:46:31 +02002227 if (ea.line2 == 0)
2228 ea.line2 = 1;
Bram Moolenaare906c502015-09-09 21:10:39 +02002229#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02002230 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02002231 case ADDR_NONE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02002232 case ADDR_UNSIGNED:
2233 case ADDR_QUICKFIX:
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002234 iemsg(_("INTERNAL: Cannot use EX_DFLALL with ADDR_NONE, ADDR_UNSIGNED or ADDR_QUICKFIX"));
Bram Moolenaarb7316892019-05-01 18:08:42 +02002235 break;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002236 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002237 }
2238
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002239 // accept numbered register only when no count allowed (:put)
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002240 if ( (ea.argt & EX_REGSTR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002241 && *ea.arg != NUL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002242 // Do not allow register = for user commands
Bram Moolenaar958636c2014-10-21 20:01:58 +02002243 && (!IS_USER_CMDIDX(ea.cmdidx) || *ea.arg != '=')
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002244 && !((ea.argt & EX_COUNT) && VIM_ISDIGIT(*ea.arg)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002245 {
Bram Moolenaar85de2062011-05-05 14:26:41 +02002246#ifndef FEAT_CLIPBOARD
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002247 // check these explicitly for a more specific error message
Bram Moolenaar85de2062011-05-05 14:26:41 +02002248 if (*ea.arg == '*' || *ea.arg == '+')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002249 {
Bram Moolenaar99b12722019-01-14 20:16:40 +01002250 errormsg = _(e_invalidreg);
Bram Moolenaar85de2062011-05-05 14:26:41 +02002251 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002252 }
2253#endif
Bram Moolenaar958636c2014-10-21 20:01:58 +02002254 if (valid_yank_reg(*ea.arg, (ea.cmdidx != CMD_put
2255 && !IS_USER_CMDIDX(ea.cmdidx))))
Bram Moolenaar85de2062011-05-05 14:26:41 +02002256 {
2257 ea.regname = *ea.arg++;
2258#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002259 // for '=' register: accept the rest of the line as an expression
Bram Moolenaar85de2062011-05-05 14:26:41 +02002260 if (ea.arg[-1] == '=' && ea.arg[0] != NUL)
2261 {
2262 set_expr_line(vim_strsave(ea.arg));
2263 ea.arg += STRLEN(ea.arg);
2264 }
2265#endif
2266 ea.arg = skipwhite(ea.arg);
2267 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002268 }
2269
2270 /*
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002271 * Check for a count. When accepting a EX_BUFNAME, don't use "123foo" as a
Bram Moolenaar071d4272004-06-13 20:20:40 +00002272 * count, it's a buffer name.
2273 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002274 if ((ea.argt & EX_COUNT) && VIM_ISDIGIT(*ea.arg)
2275 && (!(ea.argt & EX_BUFNAME) || *(p = skipdigits(ea.arg)) == NUL
Bram Moolenaar1c465442017-03-12 20:10:05 +01002276 || VIM_ISWHITE(*p)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002277 {
2278 n = getdigits(&ea.arg);
2279 ea.arg = skipwhite(ea.arg);
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002280 if (n <= 0 && !ni && (ea.argt & EX_ZEROR) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002281 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002282 errormsg = _(e_zerocount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002283 goto doend;
2284 }
Bram Moolenaarb7316892019-05-01 18:08:42 +02002285 if (ea.addr_type != ADDR_LINES) // e.g. :buffer 2, :sleep 3
Bram Moolenaar071d4272004-06-13 20:20:40 +00002286 {
2287 ea.line2 = n;
2288 if (ea.addr_count == 0)
2289 ea.addr_count = 1;
2290 }
2291 else
2292 {
2293 ea.line1 = ea.line2;
2294 ea.line2 += n - 1;
2295 ++ea.addr_count;
2296 /*
2297 * Be vi compatible: no error message for out of range.
2298 */
Bram Moolenaarb7316892019-05-01 18:08:42 +02002299 if (ea.line2 > curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002300 ea.line2 = curbuf->b_ml.ml_line_count;
2301 }
2302 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00002303
2304 /*
2305 * Check for flags: 'l', 'p' and '#'.
2306 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002307 if (ea.argt & EX_FLAGS)
Bram Moolenaardf177f62005-02-22 08:39:57 +00002308 get_flags(&ea);
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002309 if (!ni && !(ea.argt & EX_EXTRA) && *ea.arg != NUL
2310 && *ea.arg != '"' && (*ea.arg != '|' || (ea.argt & EX_TRLBAR) == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002311 {
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002312 // no arguments allowed but there is something
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002313 errormsg = _(e_trailing);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002314 goto doend;
2315 }
2316
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002317 if (!ni && (ea.argt & EX_NEEDARG) && *ea.arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002318 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002319 errormsg = _(e_argreq);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002320 goto doend;
2321 }
2322
2323#ifdef FEAT_EVAL
2324 /*
2325 * Skip the command when it's not going to be executed.
2326 * The commands like :if, :endif, etc. always need to be executed.
2327 * Also make an exception for commands that handle a trailing command
2328 * themselves.
2329 */
2330 if (ea.skip)
2331 {
2332 switch (ea.cmdidx)
2333 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002334 // commands that need evaluation
Bram Moolenaar071d4272004-06-13 20:20:40 +00002335 case CMD_while:
2336 case CMD_endwhile:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00002337 case CMD_for:
2338 case CMD_endfor:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002339 case CMD_if:
2340 case CMD_elseif:
2341 case CMD_else:
2342 case CMD_endif:
2343 case CMD_try:
2344 case CMD_catch:
2345 case CMD_finally:
2346 case CMD_endtry:
2347 case CMD_function:
2348 break;
2349
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002350 // Commands that handle '|' themselves. Check: A command should
2351 // either have the EX_TRLBAR flag, appear in this list or appear in
2352 // the list at ":help :bar".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002353 case CMD_aboveleft:
2354 case CMD_and:
2355 case CMD_belowright:
2356 case CMD_botright:
2357 case CMD_browse:
2358 case CMD_call:
2359 case CMD_confirm:
Bram Moolenaar8f76e6b2019-11-26 16:50:30 +01002360 case CMD_const:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002361 case CMD_delfunction:
2362 case CMD_djump:
2363 case CMD_dlist:
2364 case CMD_dsearch:
2365 case CMD_dsplit:
2366 case CMD_echo:
2367 case CMD_echoerr:
2368 case CMD_echomsg:
2369 case CMD_echon:
2370 case CMD_execute:
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002371 case CMD_filter:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002372 case CMD_help:
2373 case CMD_hide:
2374 case CMD_ijump:
2375 case CMD_ilist:
2376 case CMD_isearch:
2377 case CMD_isplit:
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002378 case CMD_keepalt:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002379 case CMD_keepjumps:
2380 case CMD_keepmarks:
Bram Moolenaara939e432013-11-09 05:30:26 +01002381 case CMD_keeppatterns:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002382 case CMD_leftabove:
2383 case CMD_let:
2384 case CMD_lockmarks:
Bram Moolenaarcc4423a2019-11-26 17:05:00 +01002385 case CMD_lockvar:
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002386 case CMD_lua:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002387 case CMD_match:
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002388 case CMD_mzscheme:
Bram Moolenaar5803ae62014-03-23 16:04:02 +01002389 case CMD_noautocmd:
2390 case CMD_noswapfile:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002391 case CMD_perl:
2392 case CMD_psearch:
2393 case CMD_python:
Bram Moolenaar368373e2010-07-19 20:46:22 +02002394 case CMD_py3:
Bram Moolenaarb6590522010-07-21 16:00:43 +02002395 case CMD_python3:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002396 case CMD_return:
2397 case CMD_rightbelow:
2398 case CMD_ruby:
2399 case CMD_silent:
2400 case CMD_smagic:
2401 case CMD_snomagic:
2402 case CMD_substitute:
2403 case CMD_syntax:
Bram Moolenaara226a6d2006-02-26 23:59:20 +00002404 case CMD_tab:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002405 case CMD_tcl:
2406 case CMD_throw:
2407 case CMD_tilde:
2408 case CMD_topleft:
2409 case CMD_unlet:
Bram Moolenaarcc4423a2019-11-26 17:05:00 +01002410 case CMD_unlockvar:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002411 case CMD_verbose:
2412 case CMD_vertical:
Bram Moolenaar12bde492011-06-13 01:19:56 +02002413 case CMD_wincmd:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002414 break;
2415
2416 default: goto doend;
2417 }
2418 }
2419#endif
2420
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002421 if (ea.argt & EX_XFILE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002422 {
2423 if (expand_filename(&ea, cmdlinep, &errormsg) == FAIL)
2424 goto doend;
2425 }
2426
Bram Moolenaar071d4272004-06-13 20:20:40 +00002427 /*
2428 * Accept buffer name. Cannot be used at the same time with a buffer
2429 * number. Don't do this for a user command.
2430 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002431 if ((ea.argt & EX_BUFNAME) && *ea.arg != NUL && ea.addr_count == 0
Bram Moolenaar958636c2014-10-21 20:01:58 +02002432 && !IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002433 {
2434 /*
2435 * :bdelete, :bwipeout and :bunload take several arguments, separated
2436 * by spaces: find next space (skipping over escaped characters).
2437 * The others take one argument: ignore trailing spaces.
2438 */
2439 if (ea.cmdidx == CMD_bdelete || ea.cmdidx == CMD_bwipeout
2440 || ea.cmdidx == CMD_bunload)
2441 p = skiptowhite_esc(ea.arg);
2442 else
2443 {
2444 p = ea.arg + STRLEN(ea.arg);
Bram Moolenaar1c465442017-03-12 20:10:05 +01002445 while (p > ea.arg && VIM_ISWHITE(p[-1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002446 --p;
2447 }
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002448 ea.line2 = buflist_findpat(ea.arg, p, (ea.argt & EX_BUFUNL) != 0,
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002449 FALSE, FALSE);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002450 if (ea.line2 < 0) // failed
Bram Moolenaar071d4272004-06-13 20:20:40 +00002451 goto doend;
2452 ea.addr_count = 1;
2453 ea.arg = skipwhite(p);
2454 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002455
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002456 // The :try command saves the emsg_silent flag, reset it here when
2457 // ":silent! try" was used, it should only apply to :try itself.
Bram Moolenaareffed932018-08-14 13:38:17 +02002458 if (ea.cmdidx == CMD_try && ea.did_esilent > 0)
Bram Moolenaar2be57332018-02-13 18:05:18 +01002459 {
Bram Moolenaareffed932018-08-14 13:38:17 +02002460 emsg_silent -= ea.did_esilent;
Bram Moolenaar2be57332018-02-13 18:05:18 +01002461 if (emsg_silent < 0)
2462 emsg_silent = 0;
Bram Moolenaareffed932018-08-14 13:38:17 +02002463 ea.did_esilent = 0;
Bram Moolenaar2be57332018-02-13 18:05:18 +01002464 }
2465
Bram Moolenaar071d4272004-06-13 20:20:40 +00002466/*
Bram Moolenaar2be57332018-02-13 18:05:18 +01002467 * 7. Execute the command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002468 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002469
Bram Moolenaar958636c2014-10-21 20:01:58 +02002470 if (IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002471 {
2472 /*
2473 * Execute a user-defined command.
2474 */
2475 do_ucmd(&ea);
2476 }
2477 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002478 {
2479 /*
Bram Moolenaarac9fb182019-04-27 13:04:13 +02002480 * Call the function to execute the builtin command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002481 */
2482 ea.errmsg = NULL;
2483 (cmdnames[ea.cmdidx].cmd_func)(&ea);
2484 if (ea.errmsg != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002485 errormsg = _(ea.errmsg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002486 }
2487
2488#ifdef FEAT_EVAL
2489 /*
2490 * If the command just executed called do_cmdline(), any throw or ":return"
2491 * or ":finish" encountered there must also check the cstack of the still
2492 * active do_cmdline() that called this do_one_cmd(). Rethrow an uncaught
2493 * exception, or reanimate a returned function or finished script file and
2494 * return or finish it again.
2495 */
2496 if (need_rethrow)
2497 do_throw(cstack);
2498 else if (check_cstack)
2499 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002500 if (source_finished(fgetline, cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002501 do_finish(&ea, TRUE);
Bram Moolenaar89d40322006-08-29 15:30:07 +00002502 else if (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002503 && current_func_returned())
2504 do_return(&ea, TRUE, FALSE, NULL);
2505 }
2506 need_rethrow = check_cstack = FALSE;
2507#endif
2508
2509doend:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002510 if (curwin->w_cursor.lnum == 0) // can happen with zero line number
Bram Moolenaar6de5e122017-04-20 21:55:44 +02002511 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002512 curwin->w_cursor.lnum = 1;
Bram Moolenaar6de5e122017-04-20 21:55:44 +02002513 curwin->w_cursor.col = 0;
2514 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002515
2516 if (errormsg != NULL && *errormsg != NUL && !did_emsg)
2517 {
2518 if (sourcing)
2519 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002520 if (errormsg != (char *)IObuff)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002521 {
2522 STRCPY(IObuff, errormsg);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002523 errormsg = (char *)IObuff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002524 }
Bram Moolenaara6f4d612011-09-21 19:10:46 +02002525 append_command(*cmdlinep);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002526 }
2527 emsg(errormsg);
2528 }
2529#ifdef FEAT_EVAL
2530 do_errthrow(cstack,
Bram Moolenaar958636c2014-10-21 20:01:58 +02002531 (ea.cmdidx != CMD_SIZE && !IS_USER_CMDIDX(ea.cmdidx))
2532 ? cmdnames[(int)ea.cmdidx].cmd_name : (char_u *)NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002533#endif
2534
Bram Moolenaareffed932018-08-14 13:38:17 +02002535 if (ea.verbose_save >= 0)
2536 p_verbose = ea.verbose_save;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002537
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002538 free_cmdmod();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002539 cmdmod = save_cmdmod;
Bram Moolenaar9a2c0912019-03-30 14:26:18 +01002540 reg_executing = save_reg_executing;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002541
Bram Moolenaareffed932018-08-14 13:38:17 +02002542 if (ea.save_msg_silent != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002543 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002544 // messages could be enabled for a serious error, need to check if the
2545 // counters don't become negative
Bram Moolenaareffed932018-08-14 13:38:17 +02002546 if (!did_emsg || msg_silent > ea.save_msg_silent)
2547 msg_silent = ea.save_msg_silent;
2548 emsg_silent -= ea.did_esilent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002549 if (emsg_silent < 0)
2550 emsg_silent = 0;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002551 // Restore msg_scroll, it's set by file I/O commands, even when no
2552 // message is actually displayed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002553 msg_scroll = save_msg_scroll;
Bram Moolenaar77ab2802009-04-22 12:44:48 +00002554
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002555 // "silent reg" or "silent echo x" inside "redir" leaves msg_col
2556 // somewhere in the line. Put it back in the first column.
Bram Moolenaar77ab2802009-04-22 12:44:48 +00002557 if (redirecting())
2558 msg_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002559 }
2560
Bram Moolenaar7171abe2004-10-11 10:06:20 +00002561#ifdef HAVE_SANDBOX
Bram Moolenaareffed932018-08-14 13:38:17 +02002562 if (ea.did_sandbox)
Bram Moolenaar7171abe2004-10-11 10:06:20 +00002563 --sandbox;
2564#endif
2565
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002566 if (ea.nextcmd && *ea.nextcmd == NUL) // not really a next command
Bram Moolenaar071d4272004-06-13 20:20:40 +00002567 ea.nextcmd = NULL;
2568
2569#ifdef FEAT_EVAL
2570 --ex_nesting_level;
2571#endif
2572
2573 return ea.nextcmd;
2574}
2575#if (_MSC_VER == 1200)
Bram Moolenaar281bdce2005-01-25 21:53:18 +00002576 #pragma optimize( "", on )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002577#endif
2578
2579/*
Bram Moolenaareffed932018-08-14 13:38:17 +02002580 * Parse and skip over command modifiers:
2581 * - update eap->cmd
2582 * - store flags in "cmdmod".
2583 * - Set ex_pressedreturn for an empty command line.
2584 * - set msg_silent for ":silent"
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002585 * - set 'eventignore' to "all" for ":noautocmd"
Bram Moolenaareffed932018-08-14 13:38:17 +02002586 * - set p_verbose for ":verbose"
2587 * - Increment "sandbox" for ":sandbox"
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002588 * When "skip_only" is TRUE the global variables are not changed, except for
2589 * "cmdmod".
Bram Moolenaareffed932018-08-14 13:38:17 +02002590 * Return FAIL when the command is not to be executed.
2591 * May set "errormsg" to an error message.
2592 */
2593 int
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002594parse_command_modifiers(exarg_T *eap, char **errormsg, int skip_only)
Bram Moolenaareffed932018-08-14 13:38:17 +02002595{
2596 char_u *p;
2597
2598 vim_memset(&cmdmod, 0, sizeof(cmdmod));
2599 eap->verbose_save = -1;
2600 eap->save_msg_silent = -1;
2601
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002602 // Repeat until no more command modifiers are found.
Bram Moolenaareffed932018-08-14 13:38:17 +02002603 for (;;)
2604 {
Bram Moolenaareffed932018-08-14 13:38:17 +02002605 while (*eap->cmd == ' ' || *eap->cmd == '\t' || *eap->cmd == ':')
2606 ++eap->cmd;
2607
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002608 // in ex mode, an empty line works like :+
Bram Moolenaareffed932018-08-14 13:38:17 +02002609 if (*eap->cmd == NUL && exmode_active
2610 && (getline_equal(eap->getline, eap->cookie, getexmodeline)
2611 || getline_equal(eap->getline, eap->cookie, getexline))
2612 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
2613 {
2614 eap->cmd = (char_u *)"+";
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002615 if (!skip_only)
2616 ex_pressedreturn = TRUE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002617 }
2618
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002619 // ignore comment and empty lines
Bram Moolenaareffed932018-08-14 13:38:17 +02002620 if (*eap->cmd == '"')
2621 return FAIL;
2622 if (*eap->cmd == NUL)
2623 {
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002624 if (!skip_only)
2625 ex_pressedreturn = TRUE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002626 return FAIL;
2627 }
2628
Bram Moolenaareffed932018-08-14 13:38:17 +02002629 p = skip_range(eap->cmd, NULL);
2630 switch (*p)
2631 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002632 // When adding an entry, also modify cmd_exists().
Bram Moolenaareffed932018-08-14 13:38:17 +02002633 case 'a': if (!checkforcmd(&eap->cmd, "aboveleft", 3))
2634 break;
2635 cmdmod.split |= WSP_ABOVE;
2636 continue;
2637
2638 case 'b': if (checkforcmd(&eap->cmd, "belowright", 3))
2639 {
2640 cmdmod.split |= WSP_BELOW;
2641 continue;
2642 }
2643 if (checkforcmd(&eap->cmd, "browse", 3))
2644 {
2645#ifdef FEAT_BROWSE_CMD
2646 cmdmod.browse = TRUE;
2647#endif
2648 continue;
2649 }
2650 if (!checkforcmd(&eap->cmd, "botright", 2))
2651 break;
2652 cmdmod.split |= WSP_BOT;
2653 continue;
2654
2655 case 'c': if (!checkforcmd(&eap->cmd, "confirm", 4))
2656 break;
2657#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
2658 cmdmod.confirm = TRUE;
2659#endif
2660 continue;
2661
2662 case 'k': if (checkforcmd(&eap->cmd, "keepmarks", 3))
2663 {
2664 cmdmod.keepmarks = TRUE;
2665 continue;
2666 }
2667 if (checkforcmd(&eap->cmd, "keepalt", 5))
2668 {
2669 cmdmod.keepalt = TRUE;
2670 continue;
2671 }
2672 if (checkforcmd(&eap->cmd, "keeppatterns", 5))
2673 {
2674 cmdmod.keeppatterns = TRUE;
2675 continue;
2676 }
2677 if (!checkforcmd(&eap->cmd, "keepjumps", 5))
2678 break;
2679 cmdmod.keepjumps = TRUE;
2680 continue;
2681
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002682 case 'f': // only accept ":filter {pat} cmd"
Bram Moolenaareffed932018-08-14 13:38:17 +02002683 {
2684 char_u *reg_pat;
2685
2686 if (!checkforcmd(&p, "filter", 4)
2687 || *p == NUL || ends_excmd(*p))
2688 break;
2689 if (*p == '!')
2690 {
2691 cmdmod.filter_force = TRUE;
2692 p = skipwhite(p + 1);
2693 if (*p == NUL || ends_excmd(*p))
2694 break;
2695 }
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002696 if (skip_only)
2697 p = skip_vimgrep_pat(p, NULL, NULL);
2698 else
2699 // NOTE: This puts a NUL after the pattern.
2700 p = skip_vimgrep_pat(p, &reg_pat, NULL);
Bram Moolenaareffed932018-08-14 13:38:17 +02002701 if (p == NULL || *p == NUL)
2702 break;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002703 if (!skip_only)
2704 {
2705 cmdmod.filter_regmatch.regprog =
Bram Moolenaareffed932018-08-14 13:38:17 +02002706 vim_regcomp(reg_pat, RE_MAGIC);
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002707 if (cmdmod.filter_regmatch.regprog == NULL)
2708 break;
2709 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002710 eap->cmd = p;
2711 continue;
2712 }
2713
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002714 // ":hide" and ":hide | cmd" are not modifiers
Bram Moolenaareffed932018-08-14 13:38:17 +02002715 case 'h': if (p != eap->cmd || !checkforcmd(&p, "hide", 3)
2716 || *p == NUL || ends_excmd(*p))
2717 break;
2718 eap->cmd = p;
2719 cmdmod.hide = TRUE;
2720 continue;
2721
2722 case 'l': if (checkforcmd(&eap->cmd, "lockmarks", 3))
2723 {
2724 cmdmod.lockmarks = TRUE;
2725 continue;
2726 }
2727
2728 if (!checkforcmd(&eap->cmd, "leftabove", 5))
2729 break;
2730 cmdmod.split |= WSP_ABOVE;
2731 continue;
2732
2733 case 'n': if (checkforcmd(&eap->cmd, "noautocmd", 3))
2734 {
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002735 if (cmdmod.save_ei == NULL && !skip_only)
Bram Moolenaareffed932018-08-14 13:38:17 +02002736 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002737 // Set 'eventignore' to "all". Restore the
2738 // existing option value later.
Bram Moolenaareffed932018-08-14 13:38:17 +02002739 cmdmod.save_ei = vim_strsave(p_ei);
2740 set_string_option_direct((char_u *)"ei", -1,
2741 (char_u *)"all", OPT_FREE, SID_NONE);
2742 }
2743 continue;
2744 }
2745 if (!checkforcmd(&eap->cmd, "noswapfile", 3))
2746 break;
2747 cmdmod.noswapfile = TRUE;
2748 continue;
2749
2750 case 'r': if (!checkforcmd(&eap->cmd, "rightbelow", 6))
2751 break;
2752 cmdmod.split |= WSP_BELOW;
2753 continue;
2754
2755 case 's': if (checkforcmd(&eap->cmd, "sandbox", 3))
2756 {
2757#ifdef HAVE_SANDBOX
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002758 if (!skip_only)
2759 {
2760 if (!eap->did_sandbox)
2761 ++sandbox;
2762 eap->did_sandbox = TRUE;
2763 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002764#endif
2765 continue;
2766 }
2767 if (!checkforcmd(&eap->cmd, "silent", 3))
2768 break;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002769 if (!skip_only)
2770 {
2771 if (eap->save_msg_silent == -1)
2772 eap->save_msg_silent = msg_silent;
2773 ++msg_silent;
2774 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002775 if (*eap->cmd == '!' && !VIM_ISWHITE(eap->cmd[-1]))
2776 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002777 // ":silent!", but not "silent !cmd"
Bram Moolenaareffed932018-08-14 13:38:17 +02002778 eap->cmd = skipwhite(eap->cmd + 1);
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002779 if (!skip_only)
2780 {
2781 ++emsg_silent;
2782 ++eap->did_esilent;
2783 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002784 }
2785 continue;
2786
2787 case 't': if (checkforcmd(&p, "tab", 3))
2788 {
Bram Moolenaar548e5982018-12-26 21:45:00 +01002789 if (!skip_only)
Bram Moolenaareffed932018-08-14 13:38:17 +02002790 {
Bram Moolenaar548e5982018-12-26 21:45:00 +01002791 long tabnr = get_address(eap, &eap->cmd,
2792 ADDR_TABS, eap->skip,
2793 skip_only, FALSE, 1);
2794 if (tabnr == MAXLNUM)
2795 cmdmod.tab = tabpage_index(curtab) + 1;
2796 else
Bram Moolenaareffed932018-08-14 13:38:17 +02002797 {
Bram Moolenaar548e5982018-12-26 21:45:00 +01002798 if (tabnr < 0 || tabnr > LAST_TAB_NR)
2799 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002800 *errormsg = _(e_invrange);
Bram Moolenaar548e5982018-12-26 21:45:00 +01002801 return FAIL;
2802 }
2803 cmdmod.tab = tabnr + 1;
Bram Moolenaareffed932018-08-14 13:38:17 +02002804 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002805 }
2806 eap->cmd = p;
2807 continue;
2808 }
2809 if (!checkforcmd(&eap->cmd, "topleft", 2))
2810 break;
2811 cmdmod.split |= WSP_TOP;
2812 continue;
2813
2814 case 'u': if (!checkforcmd(&eap->cmd, "unsilent", 3))
2815 break;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002816 if (!skip_only)
2817 {
2818 if (eap->save_msg_silent == -1)
2819 eap->save_msg_silent = msg_silent;
2820 msg_silent = 0;
2821 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002822 continue;
2823
2824 case 'v': if (checkforcmd(&eap->cmd, "vertical", 4))
2825 {
2826 cmdmod.split |= WSP_VERT;
2827 continue;
2828 }
2829 if (!checkforcmd(&p, "verbose", 4))
2830 break;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002831 if (!skip_only)
2832 {
2833 if (eap->verbose_save < 0)
2834 eap->verbose_save = p_verbose;
2835 if (vim_isdigit(*eap->cmd))
2836 p_verbose = atoi((char *)eap->cmd);
2837 else
2838 p_verbose = 1;
2839 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002840 eap->cmd = p;
2841 continue;
2842 }
2843 break;
2844 }
2845
2846 return OK;
2847}
2848
2849/*
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002850 * Free contents of "cmdmod".
2851 */
2852 static void
2853free_cmdmod(void)
2854{
2855 if (cmdmod.save_ei != NULL)
2856 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01002857 // Restore 'eventignore' to the value before ":noautocmd".
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002858 set_string_option_direct((char_u *)"ei", -1, cmdmod.save_ei,
2859 OPT_FREE, SID_NONE);
2860 free_string_option(cmdmod.save_ei);
2861 }
2862
2863 if (cmdmod.filter_regmatch.regprog != NULL)
2864 vim_regfree(cmdmod.filter_regmatch.regprog);
2865}
2866
2867/*
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002868 * Parse the address range, if any, in "eap".
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02002869 * May set the last search pattern, unless "silent" is TRUE.
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002870 * Return FAIL and set "errormsg" or return OK.
2871 */
2872 int
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002873parse_cmd_address(exarg_T *eap, char **errormsg, int silent)
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002874{
2875 int address_count = 1;
2876 linenr_T lnum;
2877
2878 // Repeat for all ',' or ';' separated addresses.
2879 for (;;)
2880 {
2881 eap->line1 = eap->line2;
2882 switch (eap->addr_type)
2883 {
2884 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02002885 case ADDR_OTHER:
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002886 // default is current line number
2887 eap->line2 = curwin->w_cursor.lnum;
2888 break;
2889 case ADDR_WINDOWS:
2890 eap->line2 = CURRENT_WIN_NR;
2891 break;
2892 case ADDR_ARGUMENTS:
2893 eap->line2 = curwin->w_arg_idx + 1;
2894 if (eap->line2 > ARGCOUNT)
2895 eap->line2 = ARGCOUNT;
2896 break;
2897 case ADDR_LOADED_BUFFERS:
2898 case ADDR_BUFFERS:
2899 eap->line2 = curbuf->b_fnum;
2900 break;
2901 case ADDR_TABS:
2902 eap->line2 = CURRENT_TAB_NR;
2903 break;
2904 case ADDR_TABS_RELATIVE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02002905 case ADDR_UNSIGNED:
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002906 eap->line2 = 1;
2907 break;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002908 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02002909#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02002910 eap->line2 = qf_get_cur_idx(eap);
2911#endif
2912 break;
2913 case ADDR_QUICKFIX_VALID:
2914#ifdef FEAT_QUICKFIX
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002915 eap->line2 = qf_get_cur_valid_idx(eap);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002916#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02002917 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02002918 case ADDR_NONE:
2919 // Will give an error later if a range is found.
2920 break;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002921 }
2922 eap->cmd = skipwhite(eap->cmd);
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02002923 lnum = get_address(eap, &eap->cmd, eap->addr_type, eap->skip, silent,
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002924 eap->addr_count == 0, address_count++);
2925 if (eap->cmd == NULL) // error detected
2926 return FAIL;
2927 if (lnum == MAXLNUM)
2928 {
2929 if (*eap->cmd == '%') // '%' - all lines
2930 {
2931 ++eap->cmd;
2932 switch (eap->addr_type)
2933 {
2934 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02002935 case ADDR_OTHER:
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002936 eap->line1 = 1;
2937 eap->line2 = curbuf->b_ml.ml_line_count;
2938 break;
2939 case ADDR_LOADED_BUFFERS:
2940 {
2941 buf_T *buf = firstbuf;
2942
2943 while (buf->b_next != NULL
2944 && buf->b_ml.ml_mfp == NULL)
2945 buf = buf->b_next;
2946 eap->line1 = buf->b_fnum;
2947 buf = lastbuf;
2948 while (buf->b_prev != NULL
2949 && buf->b_ml.ml_mfp == NULL)
2950 buf = buf->b_prev;
2951 eap->line2 = buf->b_fnum;
2952 break;
2953 }
2954 case ADDR_BUFFERS:
2955 eap->line1 = firstbuf->b_fnum;
2956 eap->line2 = lastbuf->b_fnum;
2957 break;
2958 case ADDR_WINDOWS:
2959 case ADDR_TABS:
2960 if (IS_USER_CMDIDX(eap->cmdidx))
2961 {
2962 eap->line1 = 1;
2963 eap->line2 = eap->addr_type == ADDR_WINDOWS
2964 ? LAST_WIN_NR : LAST_TAB_NR;
2965 }
2966 else
2967 {
2968 // there is no Vim command which uses '%' and
2969 // ADDR_WINDOWS or ADDR_TABS
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002970 *errormsg = _(e_invrange);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002971 return FAIL;
2972 }
2973 break;
2974 case ADDR_TABS_RELATIVE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02002975 case ADDR_UNSIGNED:
2976 case ADDR_QUICKFIX:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002977 *errormsg = _(e_invrange);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002978 return FAIL;
2979 case ADDR_ARGUMENTS:
2980 if (ARGCOUNT == 0)
2981 eap->line1 = eap->line2 = 0;
2982 else
2983 {
2984 eap->line1 = 1;
2985 eap->line2 = ARGCOUNT;
2986 }
2987 break;
Bram Moolenaar25190db2019-05-04 15:05:28 +02002988 case ADDR_QUICKFIX_VALID:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02002989#ifdef FEAT_QUICKFIX
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002990 eap->line1 = 1;
Bram Moolenaar25190db2019-05-04 15:05:28 +02002991 eap->line2 = qf_get_valid_size(eap);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002992 if (eap->line2 == 0)
2993 eap->line2 = 1;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002994#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02002995 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02002996 case ADDR_NONE:
2997 // Will give an error later if a range is found.
2998 break;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002999 }
3000 ++eap->addr_count;
3001 }
3002 else if (*eap->cmd == '*' && vim_strchr(p_cpo, CPO_STAR) == NULL)
3003 {
3004 pos_T *fp;
3005
3006 // '*' - visual area
3007 if (eap->addr_type != ADDR_LINES)
3008 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003009 *errormsg = _(e_invrange);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003010 return FAIL;
3011 }
3012
3013 ++eap->cmd;
3014 if (!eap->skip)
3015 {
3016 fp = getmark('<', FALSE);
3017 if (check_mark(fp) == FAIL)
3018 return FAIL;
3019 eap->line1 = fp->lnum;
3020 fp = getmark('>', FALSE);
3021 if (check_mark(fp) == FAIL)
3022 return FAIL;
3023 eap->line2 = fp->lnum;
3024 ++eap->addr_count;
3025 }
3026 }
3027 }
3028 else
3029 eap->line2 = lnum;
3030 eap->addr_count++;
3031
3032 if (*eap->cmd == ';')
3033 {
3034 if (!eap->skip)
3035 {
3036 curwin->w_cursor.lnum = eap->line2;
3037 // don't leave the cursor on an illegal line or column
3038 check_cursor();
3039 }
3040 }
3041 else if (*eap->cmd != ',')
3042 break;
3043 ++eap->cmd;
3044 }
3045
3046 // One address given: set start and end lines.
3047 if (eap->addr_count == 1)
3048 {
3049 eap->line1 = eap->line2;
3050 // ... but only implicit: really no address given
3051 if (lnum == MAXLNUM)
3052 eap->addr_count = 0;
3053 }
3054 return OK;
3055}
3056
3057/*
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00003058 * Check for an Ex command with optional tail.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003059 * If there is a match advance "pp" to the argument and return TRUE.
3060 */
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00003061 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003062checkforcmd(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003063 char_u **pp, // start of command
3064 char *cmd, // name of command
3065 int len) // required length
Bram Moolenaar071d4272004-06-13 20:20:40 +00003066{
3067 int i;
3068
3069 for (i = 0; cmd[i] != NUL; ++i)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003070 if (((char_u *)cmd)[i] != (*pp)[i])
Bram Moolenaar071d4272004-06-13 20:20:40 +00003071 break;
3072 if (i >= len && !isalpha((*pp)[i]))
3073 {
3074 *pp = skipwhite(*pp + i);
3075 return TRUE;
3076 }
3077 return FALSE;
3078}
3079
3080/*
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003081 * Append "cmd" to the error message in IObuff.
3082 * Takes care of limiting the length and handling 0xa0, which would be
3083 * invisible otherwise.
3084 */
3085 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003086append_command(char_u *cmd)
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003087{
3088 char_u *s = cmd;
3089 char_u *d;
3090
3091 STRCAT(IObuff, ": ");
3092 d = IObuff + STRLEN(IObuff);
3093 while (*s != NUL && d - IObuff < IOSIZE - 7)
3094 {
Bram Moolenaar13505972019-01-24 15:04:48 +01003095 if (enc_utf8 ? (s[0] == 0xc2 && s[1] == 0xa0) : *s == 0xa0)
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003096 {
Bram Moolenaar13505972019-01-24 15:04:48 +01003097 s += enc_utf8 ? 2 : 1;
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003098 STRCPY(d, "<a0>");
3099 d += 4;
3100 }
3101 else
3102 MB_COPY_CHAR(s, d);
3103 }
3104 *d = NUL;
3105}
3106
3107/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003108 * Find an Ex command by its name, either built-in or user.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003109 * Start of the name can be found at eap->cmd.
Bram Moolenaar25190db2019-05-04 15:05:28 +02003110 * Sets eap->cmdidx and returns a pointer to char after the command name.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003111 * "full" is set to TRUE if the whole command name matched.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003112 * Returns NULL for an ambiguous user command.
3113 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003114 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003115find_command(exarg_T *eap, int *full UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003116{
3117 int len;
3118 char_u *p;
Bram Moolenaardf177f62005-02-22 08:39:57 +00003119 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003120
3121 /*
3122 * Isolate the command and search for it in the command table.
Bram Moolenaar81870892007-11-11 18:17:28 +00003123 * Exceptions:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003124 * - the 'k' command can directly be followed by any character.
3125 * - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003126 * but :sre[wind] is another command, as are :scr[iptnames],
Bram Moolenaar071d4272004-06-13 20:20:40 +00003127 * :scs[cope], :sim[alt], :sig[ns] and :sil[ent].
Bram Moolenaardf177f62005-02-22 08:39:57 +00003128 * - the "d" command can directly be followed by 'l' or 'p' flag.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003129 */
3130 p = eap->cmd;
3131 if (*p == 'k')
3132 {
3133 eap->cmdidx = CMD_k;
3134 ++p;
3135 }
3136 else if (p[0] == 's'
Bram Moolenaar204b93f2015-08-04 22:02:51 +02003137 && ((p[1] == 'c' && (p[2] == NUL || (p[2] != 's' && p[2] != 'r'
3138 && (p[3] == NUL || (p[3] != 'i' && p[4] != 'p')))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003139 || p[1] == 'g'
3140 || (p[1] == 'i' && p[2] != 'm' && p[2] != 'l' && p[2] != 'g')
3141 || p[1] == 'I'
3142 || (p[1] == 'r' && p[2] != 'e')))
3143 {
3144 eap->cmdidx = CMD_substitute;
3145 ++p;
3146 }
3147 else
3148 {
3149 while (ASCII_ISALPHA(*p))
3150 ++p;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003151 // for python 3.x support ":py3", ":python3", ":py3file", etc.
Bram Moolenaar55d5c032010-07-17 23:52:29 +02003152 if (eap->cmd[0] == 'p' && eap->cmd[1] == 'y')
Bram Moolenaarb6590522010-07-21 16:00:43 +02003153 while (ASCII_ISALNUM(*p))
3154 ++p;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02003155
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003156 // check for non-alpha command
Bram Moolenaar071d4272004-06-13 20:20:40 +00003157 if (p == eap->cmd && vim_strchr((char_u *)"@*!=><&~#", *p) != NULL)
3158 ++p;
3159 len = (int)(p - eap->cmd);
Bram Moolenaardf177f62005-02-22 08:39:57 +00003160 if (*eap->cmd == 'd' && (p[-1] == 'l' || p[-1] == 'p'))
3161 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003162 // Check for ":dl", ":dell", etc. to ":deletel": that's
3163 // :delete with the 'l' flag. Same for 'p'.
Bram Moolenaardf177f62005-02-22 08:39:57 +00003164 for (i = 0; i < len; ++i)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003165 if (eap->cmd[i] != ((char_u *)"delete")[i])
Bram Moolenaardf177f62005-02-22 08:39:57 +00003166 break;
3167 if (i == len - 1)
3168 {
3169 --len;
3170 if (p[-1] == 'l')
3171 eap->flags |= EXFLAG_LIST;
3172 else
3173 eap->flags |= EXFLAG_PRINT;
3174 }
3175 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003176
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003177 if (ASCII_ISLOWER(eap->cmd[0]))
3178 {
Bram Moolenaar6c0c1e82017-03-25 15:07:43 +01003179 int c1 = eap->cmd[0];
Bram Moolenaar94f82cb2019-07-24 22:30:27 +02003180 int c2 = len == 1 ? NUL : eap->cmd[1];
Bram Moolenaar6c0c1e82017-03-25 15:07:43 +01003181
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003182 if (command_count != (int)CMD_SIZE)
3183 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003184 iemsg(_("E943: Command table needs to be updated, run 'make cmdidxs'"));
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003185 getout(1);
3186 }
3187
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003188 // Use a precomputed index for fast look-up in cmdnames[]
3189 // taking into account the first 2 letters of eap->cmd.
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003190 eap->cmdidx = cmdidxs1[CharOrdLow(c1)];
3191 if (ASCII_ISLOWER(c2))
3192 eap->cmdidx += cmdidxs2[CharOrdLow(c1)][CharOrdLow(c2)];
3193 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003194 else
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003195 eap->cmdidx = CMD_bang;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003196
3197 for ( ; (int)eap->cmdidx < (int)CMD_SIZE;
3198 eap->cmdidx = (cmdidx_T)((int)eap->cmdidx + 1))
3199 if (STRNCMP(cmdnames[(int)eap->cmdidx].cmd_name, (char *)eap->cmd,
3200 (size_t)len) == 0)
3201 {
3202#ifdef FEAT_EVAL
3203 if (full != NULL
3204 && cmdnames[(int)eap->cmdidx].cmd_name[len] == NUL)
3205 *full = TRUE;
3206#endif
3207 break;
3208 }
3209
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003210 // Look for a user defined command as a last resort. Let ":Print" be
3211 // overruled by a user defined command.
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01003212 if ((eap->cmdidx == CMD_SIZE || eap->cmdidx == CMD_Print)
3213 && *eap->cmd >= 'A' && *eap->cmd <= 'Z')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003214 {
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003215 // User defined commands may contain digits.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003216 while (ASCII_ISALNUM(*p))
3217 ++p;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003218 p = find_ucmd(eap, p, full, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003219 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003220 if (p == eap->cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003221 eap->cmdidx = CMD_SIZE;
3222 }
3223
3224 return p;
3225}
3226
3227#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003228static struct cmdmod
3229{
3230 char *name;
3231 int minlen;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003232 int has_count; // :123verbose :3tab
Bram Moolenaared53fb92007-11-24 20:50:24 +00003233} cmdmods[] = {
3234 {"aboveleft", 3, FALSE},
3235 {"belowright", 3, FALSE},
3236 {"botright", 2, FALSE},
3237 {"browse", 3, FALSE},
3238 {"confirm", 4, FALSE},
Bram Moolenaar7b668e82016-08-23 23:51:21 +02003239 {"filter", 4, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003240 {"hide", 3, FALSE},
3241 {"keepalt", 5, FALSE},
3242 {"keepjumps", 5, FALSE},
3243 {"keepmarks", 3, FALSE},
Bram Moolenaara939e432013-11-09 05:30:26 +01003244 {"keeppatterns", 5, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003245 {"leftabove", 5, FALSE},
3246 {"lockmarks", 3, FALSE},
Bram Moolenaarca9f9582008-09-18 10:44:28 +00003247 {"noautocmd", 3, FALSE},
Bram Moolenaar5803ae62014-03-23 16:04:02 +01003248 {"noswapfile", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003249 {"rightbelow", 6, FALSE},
3250 {"sandbox", 3, FALSE},
3251 {"silent", 3, FALSE},
3252 {"tab", 3, TRUE},
3253 {"topleft", 2, FALSE},
Bram Moolenaar8e258a42009-07-09 13:55:43 +00003254 {"unsilent", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003255 {"verbose", 4, TRUE},
3256 {"vertical", 4, FALSE},
3257};
3258
3259/*
3260 * Return length of a command modifier (including optional count).
3261 * Return zero when it's not a modifier.
3262 */
3263 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003264modifier_len(char_u *cmd)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003265{
3266 int i, j;
3267 char_u *p = cmd;
3268
3269 if (VIM_ISDIGIT(*cmd))
3270 p = skipwhite(skipdigits(cmd));
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00003271 for (i = 0; i < (int)(sizeof(cmdmods) / sizeof(struct cmdmod)); ++i)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003272 {
3273 for (j = 0; p[j] != NUL; ++j)
3274 if (p[j] != cmdmods[i].name[j])
3275 break;
Bram Moolenaar2d473ab2013-06-12 17:12:24 +02003276 if (!ASCII_ISALPHA(p[j]) && j >= cmdmods[i].minlen
Bram Moolenaared53fb92007-11-24 20:50:24 +00003277 && (p == cmd || cmdmods[i].has_count))
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00003278 return j + (int)(p - cmd);
Bram Moolenaared53fb92007-11-24 20:50:24 +00003279 }
3280 return 0;
3281}
3282
Bram Moolenaar071d4272004-06-13 20:20:40 +00003283/*
3284 * Return > 0 if an Ex command "name" exists.
3285 * Return 2 if there is an exact match.
3286 * Return 3 if there is an ambiguous match.
3287 */
3288 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003289cmd_exists(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003290{
3291 exarg_T ea;
3292 int full = FALSE;
3293 int i;
3294 int j;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003295 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003296
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003297 // Check command modifiers.
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00003298 for (i = 0; i < (int)(sizeof(cmdmods) / sizeof(struct cmdmod)); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003299 {
3300 for (j = 0; name[j] != NUL; ++j)
3301 if (name[j] != cmdmods[i].name[j])
3302 break;
3303 if (name[j] == NUL && j >= cmdmods[i].minlen)
3304 return (cmdmods[i].name[j] == NUL ? 2 : 1);
3305 }
3306
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003307 // Check built-in commands and user defined commands.
3308 // For ":2match" and ":3match" we need to skip the number.
Bram Moolenaara9587612006-05-04 21:47:50 +00003309 ea.cmd = (*name == '2' || *name == '3') ? name + 1 : name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003310 ea.cmdidx = (cmdidx_T)0;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003311 p = find_command(&ea, &full);
3312 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003313 return 3;
Bram Moolenaara9587612006-05-04 21:47:50 +00003314 if (vim_isdigit(*name) && ea.cmdidx != CMD_match)
3315 return 0;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003316 if (*skipwhite(p) != NUL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003317 return 0; // trailing garbage
Bram Moolenaar071d4272004-06-13 20:20:40 +00003318 return (ea.cmdidx == CMD_SIZE ? 0 : (full ? 2 : 1));
3319}
3320#endif
3321
Bram Moolenaard0190392019-08-23 21:17:35 +02003322 cmdidx_T
3323excmd_get_cmdidx(char_u *cmd, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003324{
Bram Moolenaard0190392019-08-23 21:17:35 +02003325 cmdidx_T idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003326
Bram Moolenaard0190392019-08-23 21:17:35 +02003327 for (idx = (cmdidx_T)0; (int)idx < (int)CMD_SIZE;
3328 idx = (cmdidx_T)((int)idx + 1))
3329 if (STRNCMP(cmdnames[(int)idx].cmd_name, cmd, (size_t)len) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003330 break;
3331
Bram Moolenaard0190392019-08-23 21:17:35 +02003332 return idx;
3333}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003334
Bram Moolenaard0190392019-08-23 21:17:35 +02003335 long
3336excmd_get_argt(cmdidx_T idx)
3337{
3338 return (long)cmdnames[(int)idx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003339}
3340
3341/*
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003342 * Skip a range specifier of the form: addr [,addr] [;addr] ..
Bram Moolenaar071d4272004-06-13 20:20:40 +00003343 *
3344 * Backslashed delimiters after / or ? will be skipped, and commands will
3345 * not be expanded between /'s and ?'s or after "'".
3346 *
Bram Moolenaardf177f62005-02-22 08:39:57 +00003347 * Also skip white space and ":" characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003348 * Returns the "cmd" pointer advanced to beyond the range.
3349 */
3350 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003351skip_range(
3352 char_u *cmd,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003353 int *ctx) // pointer to xp_context or NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003354{
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003355 unsigned delim;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003356
Bram Moolenaarcbf20fb2017-02-03 21:19:04 +01003357 while (vim_strchr((char_u *)" \t0123456789.$%'/?-+,;\\", *cmd) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003358 {
Bram Moolenaarcbf20fb2017-02-03 21:19:04 +01003359 if (*cmd == '\\')
3360 {
3361 if (cmd[1] == '?' || cmd[1] == '/' || cmd[1] == '&')
3362 ++cmd;
3363 else
3364 break;
3365 }
3366 else if (*cmd == '\'')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003367 {
3368 if (*++cmd == NUL && ctx != NULL)
3369 *ctx = EXPAND_NOTHING;
3370 }
3371 else if (*cmd == '/' || *cmd == '?')
3372 {
3373 delim = *cmd++;
3374 while (*cmd != NUL && *cmd != delim)
3375 if (*cmd++ == '\\' && *cmd != NUL)
3376 ++cmd;
3377 if (*cmd == NUL && ctx != NULL)
3378 *ctx = EXPAND_NOTHING;
3379 }
3380 if (*cmd != NUL)
3381 ++cmd;
3382 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00003383
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003384 // Skip ":" and white space.
Bram Moolenaardf177f62005-02-22 08:39:57 +00003385 while (*cmd == ':')
3386 cmd = skipwhite(cmd + 1);
3387
Bram Moolenaar071d4272004-06-13 20:20:40 +00003388 return cmd;
3389}
3390
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003391 static void
3392addr_error(cmd_addr_T addr_type)
3393{
3394 if (addr_type == ADDR_NONE)
3395 emsg(_(e_norange));
3396 else
3397 emsg(_(e_invrange));
3398}
3399
Bram Moolenaar071d4272004-06-13 20:20:40 +00003400/*
Bram Moolenaar198cb662018-09-06 21:44:17 +02003401 * Get a single EX address.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003402 *
3403 * Set ptr to the next character after the part that was interpreted.
3404 * Set ptr to NULL when an error is encountered.
Bram Moolenaar198cb662018-09-06 21:44:17 +02003405 * This may set the last used search pattern.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003406 *
3407 * Return MAXLNUM when no Ex address was found.
3408 */
3409 static linenr_T
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003410get_address(
3411 exarg_T *eap UNUSED,
3412 char_u **ptr,
Bram Moolenaar1b03a192019-12-08 17:08:29 +01003413 cmd_addr_T addr_type,
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02003414 int skip, // only skip the address, don't use it
3415 int silent, // no errors or side effects
3416 int to_other_file, // flag: may jump to other file
3417 int address_count UNUSED) // 1 for first address, >1 after comma
Bram Moolenaar071d4272004-06-13 20:20:40 +00003418{
3419 int c;
3420 int i;
3421 long n;
3422 char_u *cmd;
3423 pos_T pos;
3424 pos_T *fp;
3425 linenr_T lnum;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003426 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003427
3428 cmd = skipwhite(*ptr);
3429 lnum = MAXLNUM;
3430 do
3431 {
3432 switch (*cmd)
3433 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003434 case '.': // '.' - Cursor position
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003435 ++cmd;
3436 switch (addr_type)
3437 {
3438 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02003439 case ADDR_OTHER:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003440 lnum = curwin->w_cursor.lnum;
3441 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003442 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01003443 lnum = CURRENT_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003444 break;
3445 case ADDR_ARGUMENTS:
3446 lnum = curwin->w_arg_idx + 1;
3447 break;
3448 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003449 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003450 lnum = curbuf->b_fnum;
3451 break;
3452 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01003453 lnum = CURRENT_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003454 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02003455 case ADDR_NONE:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003456 case ADDR_TABS_RELATIVE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02003457 case ADDR_UNSIGNED:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003458 addr_error(addr_type);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01003459 cmd = NULL;
3460 goto error;
3461 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003462 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003463#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02003464 lnum = qf_get_cur_idx(eap);
3465#endif
3466 break;
3467 case ADDR_QUICKFIX_VALID:
3468#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003469 lnum = qf_get_cur_valid_idx(eap);
Bram Moolenaare906c502015-09-09 21:10:39 +02003470#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003471 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003472 }
3473 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003474
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003475 case '$': // '$' - last line
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003476 ++cmd;
3477 switch (addr_type)
3478 {
3479 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02003480 case ADDR_OTHER:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003481 lnum = curbuf->b_ml.ml_line_count;
3482 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003483 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01003484 lnum = LAST_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003485 break;
3486 case ADDR_ARGUMENTS:
3487 lnum = ARGCOUNT;
3488 break;
3489 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003490 buf = lastbuf;
3491 while (buf->b_ml.ml_mfp == NULL)
3492 {
3493 if (buf->b_prev == NULL)
3494 break;
3495 buf = buf->b_prev;
3496 }
3497 lnum = buf->b_fnum;
3498 break;
3499 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003500 lnum = lastbuf->b_fnum;
3501 break;
3502 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01003503 lnum = LAST_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003504 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02003505 case ADDR_NONE:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003506 case ADDR_TABS_RELATIVE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02003507 case ADDR_UNSIGNED:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003508 addr_error(addr_type);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01003509 cmd = NULL;
3510 goto error;
3511 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003512 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003513#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003514 lnum = qf_get_size(eap);
3515 if (lnum == 0)
3516 lnum = 1;
Bram Moolenaare906c502015-09-09 21:10:39 +02003517#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003518 break;
Bram Moolenaar25190db2019-05-04 15:05:28 +02003519 case ADDR_QUICKFIX_VALID:
3520#ifdef FEAT_QUICKFIX
3521 lnum = qf_get_valid_size(eap);
3522 if (lnum == 0)
3523 lnum = 1;
3524#endif
3525 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003526 }
3527 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003528
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003529 case '\'': // ''' - mark
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003530 if (*++cmd == NUL)
3531 {
3532 cmd = NULL;
3533 goto error;
3534 }
3535 if (addr_type != ADDR_LINES)
3536 {
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003537 addr_error(addr_type);
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01003538 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003539 goto error;
3540 }
3541 if (skip)
3542 ++cmd;
3543 else
3544 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003545 // Only accept a mark in another file when it is
3546 // used by itself: ":'M".
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003547 fp = getmark(*cmd, to_other_file && cmd[1] == NUL);
3548 ++cmd;
3549 if (fp == (pos_T *)-1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003550 // Jumped to another file.
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003551 lnum = curwin->w_cursor.lnum;
3552 else
3553 {
3554 if (check_mark(fp) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003555 {
3556 cmd = NULL;
3557 goto error;
3558 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003559 lnum = fp->lnum;
3560 }
3561 }
3562 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003563
3564 case '/':
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003565 case '?': // '/' or '?' - search
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003566 c = *cmd++;
3567 if (addr_type != ADDR_LINES)
3568 {
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003569 addr_error(addr_type);
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01003570 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003571 goto error;
3572 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003573 if (skip) // skip "/pat/"
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003574 {
3575 cmd = skip_regexp(cmd, c, (int)p_magic, NULL);
3576 if (*cmd == c)
3577 ++cmd;
3578 }
3579 else
3580 {
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02003581 int flags;
3582
3583 pos = curwin->w_cursor; // save curwin->w_cursor
3584
3585 // When '/' or '?' follows another address, start from
3586 // there.
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003587 if (lnum != MAXLNUM)
3588 curwin->w_cursor.lnum = lnum;
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02003589
3590 // Start a forward search at the end of the line (unless
3591 // before the first line).
3592 // Start a backward search at the start of the line.
3593 // This makes sure we never match in the current
3594 // line, and can match anywhere in the
3595 // next/previous line.
Bram Moolenaar8ada6aa2017-12-19 21:23:21 +01003596 if (c == '/' && curwin->w_cursor.lnum > 0)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003597 curwin->w_cursor.col = MAXCOL;
3598 else
3599 curwin->w_cursor.col = 0;
3600 searchcmdlen = 0;
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02003601 flags = silent ? 0 : SEARCH_HIS | SEARCH_MSG;
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02003602 if (!do_search(NULL, c, cmd, 1L, flags, NULL))
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003603 {
3604 curwin->w_cursor = pos;
3605 cmd = NULL;
3606 goto error;
3607 }
3608 lnum = curwin->w_cursor.lnum;
3609 curwin->w_cursor = pos;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003610 // adjust command string pointer
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003611 cmd += searchcmdlen;
3612 }
3613 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003614
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003615 case '\\': // "\?", "\/" or "\&", repeat search
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003616 ++cmd;
3617 if (addr_type != ADDR_LINES)
3618 {
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003619 addr_error(addr_type);
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01003620 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003621 goto error;
3622 }
3623 if (*cmd == '&')
3624 i = RE_SUBST;
3625 else if (*cmd == '?' || *cmd == '/')
3626 i = RE_SEARCH;
3627 else
3628 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003629 emsg(_(e_backslash));
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003630 cmd = NULL;
3631 goto error;
3632 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003633
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003634 if (!skip)
3635 {
3636 /*
3637 * When search follows another address, start from
3638 * there.
3639 */
3640 if (lnum != MAXLNUM)
3641 pos.lnum = lnum;
3642 else
3643 pos.lnum = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003644
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003645 /*
3646 * Start the search just like for the above
3647 * do_search().
3648 */
3649 if (*cmd != '?')
3650 pos.col = MAXCOL;
3651 else
3652 pos.col = 0;
Bram Moolenaarbd8539a2015-08-11 18:53:03 +02003653 pos.coladd = 0;
Bram Moolenaar5d24a222018-12-23 19:10:09 +01003654 if (searchit(curwin, curbuf, &pos, NULL,
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003655 *cmd == '?' ? BACKWARD : FORWARD,
Bram Moolenaar92ea26b2019-10-18 20:53:34 +02003656 (char_u *)"", 1L, SEARCH_MSG, i, NULL) != FAIL)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003657 lnum = pos.lnum;
3658 else
3659 {
3660 cmd = NULL;
3661 goto error;
3662 }
3663 }
3664 ++cmd;
3665 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003666
3667 default:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003668 if (VIM_ISDIGIT(*cmd)) // absolute line number
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003669 lnum = getdigits(&cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003670 }
3671
3672 for (;;)
3673 {
3674 cmd = skipwhite(cmd);
3675 if (*cmd != '-' && *cmd != '+' && !VIM_ISDIGIT(*cmd))
3676 break;
3677
3678 if (lnum == MAXLNUM)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003679 {
3680 switch (addr_type)
3681 {
3682 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02003683 case ADDR_OTHER:
3684 // "+1" is same as ".+1"
Bram Moolenaarf240e182014-11-27 18:33:02 +01003685 lnum = curwin->w_cursor.lnum;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003686 break;
3687 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01003688 lnum = CURRENT_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003689 break;
3690 case ADDR_ARGUMENTS:
3691 lnum = curwin->w_arg_idx + 1;
3692 break;
3693 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003694 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003695 lnum = curbuf->b_fnum;
3696 break;
3697 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01003698 lnum = CURRENT_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003699 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01003700 case ADDR_TABS_RELATIVE:
3701 lnum = 1;
3702 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003703 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003704#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02003705 lnum = qf_get_cur_idx(eap);
3706#endif
3707 break;
3708 case ADDR_QUICKFIX_VALID:
3709#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003710 lnum = qf_get_cur_valid_idx(eap);
Bram Moolenaare906c502015-09-09 21:10:39 +02003711#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003712 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02003713 case ADDR_NONE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02003714 case ADDR_UNSIGNED:
3715 lnum = 0;
Bram Moolenaarb7316892019-05-01 18:08:42 +02003716 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01003717 }
3718 }
3719
Bram Moolenaar071d4272004-06-13 20:20:40 +00003720 if (VIM_ISDIGIT(*cmd))
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003721 i = '+'; // "number" is same as "+number"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003722 else
3723 i = *cmd++;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003724 if (!VIM_ISDIGIT(*cmd)) // '+' is '+1', but '+0' is not '+1'
Bram Moolenaar071d4272004-06-13 20:20:40 +00003725 n = 1;
3726 else
3727 n = getdigits(&cmd);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01003728
3729 if (addr_type == ADDR_TABS_RELATIVE)
3730 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003731 emsg(_(e_invrange));
Bram Moolenaar2f72c702017-01-29 14:48:10 +01003732 cmd = NULL;
3733 goto error;
3734 }
3735 else if (addr_type == ADDR_LOADED_BUFFERS
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01003736 || addr_type == ADDR_BUFFERS)
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01003737 lnum = compute_buffer_local_count(
3738 addr_type, lnum, (i == '-') ? -1 * n : n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003739 else
Bram Moolenaarded27822017-01-02 14:27:34 +01003740 {
3741#ifdef FEAT_FOLDING
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003742 // Relative line addressing, need to adjust for folded lines
3743 // now, but only do it after the first address.
Bram Moolenaarded27822017-01-02 14:27:34 +01003744 if (addr_type == ADDR_LINES && (i == '-' || i == '+')
3745 && address_count >= 2)
3746 (void)hasFolding(lnum, NULL, &lnum);
3747#endif
3748 if (i == '-')
3749 lnum -= n;
3750 else
3751 lnum += n;
3752 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003753 }
3754 } while (*cmd == '/' || *cmd == '?');
3755
3756error:
3757 *ptr = cmd;
3758 return lnum;
3759}
3760
3761/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00003762 * Get flags from an Ex command argument.
3763 */
3764 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003765get_flags(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00003766{
3767 while (vim_strchr((char_u *)"lp#", *eap->arg) != NULL)
3768 {
3769 if (*eap->arg == 'l')
3770 eap->flags |= EXFLAG_LIST;
3771 else if (*eap->arg == 'p')
3772 eap->flags |= EXFLAG_PRINT;
3773 else
3774 eap->flags |= EXFLAG_NR;
3775 eap->arg = skipwhite(eap->arg + 1);
3776 }
3777}
3778
3779/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003780 * Function called for command which is Not Implemented. NI!
3781 */
3782 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003783ex_ni(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003784{
3785 if (!eap->skip)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003786 eap->errmsg = N_("E319: Sorry, the command is not available in this version");
Bram Moolenaar071d4272004-06-13 20:20:40 +00003787}
3788
Bram Moolenaar7bb75552007-07-16 18:39:49 +00003789#ifdef HAVE_EX_SCRIPT_NI
Bram Moolenaar071d4272004-06-13 20:20:40 +00003790/*
3791 * Function called for script command which is Not Implemented. NI!
3792 * Skips over ":perl <<EOF" constructs.
3793 */
3794 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003795ex_script_ni(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003796{
3797 if (!eap->skip)
3798 ex_ni(eap);
3799 else
3800 vim_free(script_get(eap, eap->arg));
3801}
3802#endif
3803
3804/*
3805 * Check range in Ex command for validity.
3806 * Return NULL when valid, error message when invalid.
3807 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003808 static char *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003809invalid_range(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003810{
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003811 buf_T *buf;
Bram Moolenaar25190db2019-05-04 15:05:28 +02003812
Bram Moolenaar071d4272004-06-13 20:20:40 +00003813 if ( eap->line1 < 0
3814 || eap->line2 < 0
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003815 || eap->line1 > eap->line2)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003816 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003817
Bram Moolenaar8071cb22019-07-12 17:58:01 +02003818 if (eap->argt & EX_RANGE)
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003819 {
Bram Moolenaarb7316892019-05-01 18:08:42 +02003820 switch (eap->addr_type)
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003821 {
3822 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02003823 if (eap->line2 > curbuf->b_ml.ml_line_count
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003824#ifdef FEAT_DIFF
3825 + (eap->cmdidx == CMD_diffget)
3826#endif
3827 )
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003828 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003829 break;
3830 case ADDR_ARGUMENTS:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003831 // add 1 if ARGCOUNT is 0
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01003832 if (eap->line2 > ARGCOUNT + (!ARGCOUNT))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003833 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003834 break;
3835 case ADDR_BUFFERS:
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02003836 // Only a boundary check, not whether the buffers actually
3837 // exist.
3838 if (eap->line1 < 1 || eap->line2 > get_highest_fnum())
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003839 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003840 break;
3841 case ADDR_LOADED_BUFFERS:
3842 buf = firstbuf;
3843 while (buf->b_ml.ml_mfp == NULL)
3844 {
3845 if (buf->b_next == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003846 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003847 buf = buf->b_next;
3848 }
3849 if (eap->line1 < buf->b_fnum)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003850 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003851 buf = lastbuf;
3852 while (buf->b_ml.ml_mfp == NULL)
3853 {
3854 if (buf->b_prev == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003855 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003856 buf = buf->b_prev;
3857 }
3858 if (eap->line2 > buf->b_fnum)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003859 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003860 break;
3861 case ADDR_WINDOWS:
Bram Moolenaar8be63882015-01-14 11:25:05 +01003862 if (eap->line2 > LAST_WIN_NR)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003863 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003864 break;
3865 case ADDR_TABS:
3866 if (eap->line2 > LAST_TAB_NR)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003867 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003868 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01003869 case ADDR_TABS_RELATIVE:
Bram Moolenaarb7316892019-05-01 18:08:42 +02003870 case ADDR_OTHER:
3871 // Any range is OK.
Bram Moolenaar2f72c702017-01-29 14:48:10 +01003872 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003873 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003874#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02003875 // No error for value that is too big, will use the last entry.
3876 if (eap->line2 <= 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003877 return _(e_invrange);
Bram Moolenaare906c502015-09-09 21:10:39 +02003878#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003879 break;
Bram Moolenaar25190db2019-05-04 15:05:28 +02003880 case ADDR_QUICKFIX_VALID:
3881#ifdef FEAT_QUICKFIX
3882 if ((eap->line2 != 1 && eap->line2 > qf_get_valid_size(eap))
3883 || eap->line2 < 0)
3884 return _(e_invrange);
3885#endif
3886 break;
3887 case ADDR_UNSIGNED:
3888 if (eap->line2 < 0)
3889 return _(e_invrange);
3890 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02003891 case ADDR_NONE:
3892 // Will give an error elsewhere.
3893 break;
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003894 }
3895 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003896 return NULL;
3897}
3898
3899/*
3900 * Correct the range for zero line number, if required.
3901 */
3902 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003903correct_range(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003904{
Bram Moolenaar8071cb22019-07-12 17:58:01 +02003905 if (!(eap->argt & EX_ZEROR)) // zero in range not allowed
Bram Moolenaar071d4272004-06-13 20:20:40 +00003906 {
3907 if (eap->line1 == 0)
3908 eap->line1 = 1;
3909 if (eap->line2 == 0)
3910 eap->line2 = 1;
3911 }
3912}
3913
Bram Moolenaar748bf032005-02-02 23:04:36 +00003914#ifdef FEAT_QUICKFIX
Bram Moolenaar748bf032005-02-02 23:04:36 +00003915/*
3916 * For a ":vimgrep" or ":vimgrepadd" command return a pointer past the
3917 * pattern. Otherwise return eap->arg.
3918 */
3919 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003920skip_grep_pat(exarg_T *eap)
Bram Moolenaar748bf032005-02-02 23:04:36 +00003921{
3922 char_u *p = eap->arg;
3923
Bram Moolenaara37420f2006-02-04 22:37:47 +00003924 if (*p != NUL && (eap->cmdidx == CMD_vimgrep || eap->cmdidx == CMD_lvimgrep
3925 || eap->cmdidx == CMD_vimgrepadd
3926 || eap->cmdidx == CMD_lvimgrepadd
3927 || grep_internal(eap->cmdidx)))
Bram Moolenaar748bf032005-02-02 23:04:36 +00003928 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00003929 p = skip_vimgrep_pat(p, NULL, NULL);
Bram Moolenaar748bf032005-02-02 23:04:36 +00003930 if (p == NULL)
3931 p = eap->arg;
Bram Moolenaar748bf032005-02-02 23:04:36 +00003932 }
3933 return p;
3934}
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003935
3936/*
3937 * For the ":make" and ":grep" commands insert the 'makeprg'/'grepprg' option
3938 * in the command line, so that things like % get expanded.
3939 */
3940 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003941replace_makeprg(exarg_T *eap, char_u *p, char_u **cmdlinep)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003942{
3943 char_u *new_cmdline;
3944 char_u *program;
3945 char_u *pos;
3946 char_u *ptr;
3947 int len;
3948 int i;
3949
3950 /*
3951 * Don't do it when ":vimgrep" is used for ":grep".
3952 */
Bram Moolenaara37420f2006-02-04 22:37:47 +00003953 if ((eap->cmdidx == CMD_make || eap->cmdidx == CMD_lmake
3954 || eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
3955 || eap->cmdidx == CMD_grepadd
3956 || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003957 && !grep_internal(eap->cmdidx))
3958 {
Bram Moolenaara37420f2006-02-04 22:37:47 +00003959 if (eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
3960 || eap->cmdidx == CMD_grepadd || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003961 {
3962 if (*curbuf->b_p_gp == NUL)
3963 program = p_gp;
3964 else
3965 program = curbuf->b_p_gp;
3966 }
3967 else
3968 {
3969 if (*curbuf->b_p_mp == NUL)
3970 program = p_mp;
3971 else
3972 program = curbuf->b_p_mp;
3973 }
3974
3975 p = skipwhite(p);
3976
3977 if ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
3978 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003979 // replace $* by given arguments
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003980 i = 1;
3981 while ((pos = (char_u *)strstr((char *)pos + 2, "$*")) != NULL)
3982 ++i;
3983 len = (int)STRLEN(p);
Bram Moolenaar51e14382019-05-25 20:21:28 +02003984 new_cmdline = alloc(STRLEN(program) + i * (len - 2) + 1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003985 if (new_cmdline == NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01003986 return NULL; // out of memory
Bram Moolenaard857f0e2005-06-21 22:37:39 +00003987 ptr = new_cmdline;
3988 while ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
3989 {
3990 i = (int)(pos - program);
3991 STRNCPY(ptr, program, i);
3992 STRCPY(ptr += i, p);
3993 ptr += len;
3994 program = pos + 2;
3995 }
3996 STRCPY(ptr, program);
3997 }
3998 else
3999 {
Bram Moolenaar51e14382019-05-25 20:21:28 +02004000 new_cmdline = alloc(STRLEN(program) + STRLEN(p) + 2);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004001 if (new_cmdline == NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004002 return NULL; // out of memory
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004003 STRCPY(new_cmdline, program);
4004 STRCAT(new_cmdline, " ");
4005 STRCAT(new_cmdline, p);
4006 }
4007 msg_make(p);
4008
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004009 // 'eap->cmd' is not set here, because it is not used at CMD_make
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004010 vim_free(*cmdlinep);
4011 *cmdlinep = new_cmdline;
4012 p = new_cmdline;
4013 }
4014 return p;
4015}
Bram Moolenaar748bf032005-02-02 23:04:36 +00004016#endif
4017
Bram Moolenaar071d4272004-06-13 20:20:40 +00004018/*
4019 * Expand file name in Ex command argument.
Bram Moolenaar0abb4272019-06-15 16:06:00 +02004020 * When an error is detected, "errormsgp" is set to a non-NULL pointer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004021 * Return FAIL for failure, OK otherwise.
4022 */
4023 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004024expand_filename(
4025 exarg_T *eap,
4026 char_u **cmdlinep,
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004027 char **errormsgp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004028{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004029 int has_wildcards; // need to expand wildcards
Bram Moolenaar071d4272004-06-13 20:20:40 +00004030 char_u *repl;
4031 int srclen;
4032 char_u *p;
4033 int n;
Bram Moolenaar63b92542007-03-27 14:57:09 +00004034 int escaped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004035
Bram Moolenaar748bf032005-02-02 23:04:36 +00004036#ifdef FEAT_QUICKFIX
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004037 // Skip a regexp pattern for ":vimgrep[add] pat file..."
Bram Moolenaar748bf032005-02-02 23:04:36 +00004038 p = skip_grep_pat(eap);
4039#else
4040 p = eap->arg;
4041#endif
4042
Bram Moolenaar071d4272004-06-13 20:20:40 +00004043 /*
4044 * Decide to expand wildcards *before* replacing '%', '#', etc. If
4045 * the file name contains a wildcard it should not cause expanding.
4046 * (it will be expanded anyway if there is a wildcard before replacing).
4047 */
Bram Moolenaar748bf032005-02-02 23:04:36 +00004048 has_wildcards = mch_has_wildcard(p);
4049 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004050 {
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004051#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004052 // Skip over `=expr`, wildcards in it are not expanded.
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004053 if (p[0] == '`' && p[1] == '=')
4054 {
4055 p += 2;
4056 (void)skip_expr(&p);
4057 if (*p == '`')
4058 ++p;
4059 continue;
4060 }
4061#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004062 /*
4063 * Quick check if this cannot be the start of a special string.
4064 * Also removes backslash before '%', '#' and '<'.
4065 */
4066 if (vim_strchr((char_u *)"%#<", *p) == NULL)
4067 {
4068 ++p;
4069 continue;
4070 }
4071
4072 /*
4073 * Try to find a match at this position.
4074 */
Bram Moolenaar63b92542007-03-27 14:57:09 +00004075 repl = eval_vars(p, eap->arg, &srclen, &(eap->do_ecmd_lnum),
4076 errormsgp, &escaped);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004077 if (*errormsgp != NULL) // error detected
Bram Moolenaar071d4272004-06-13 20:20:40 +00004078 return FAIL;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004079 if (repl == NULL) // no match found
Bram Moolenaar071d4272004-06-13 20:20:40 +00004080 {
4081 p += srclen;
4082 continue;
4083 }
4084
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004085 // Wildcards won't be expanded below, the replacement is taken
4086 // literally. But do expand "~/file", "~user/file" and "$HOME/file".
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00004087 if (vim_strchr(repl, '$') != NULL || vim_strchr(repl, '~') != NULL)
4088 {
4089 char_u *l = repl;
4090
4091 repl = expand_env_save(repl);
4092 vim_free(l);
4093 }
4094
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004095 // Need to escape white space et al. with a backslash.
4096 // Don't do this for:
4097 // - replacement that already has been escaped: "##"
4098 // - shell commands (may have to use quotes instead).
4099 // - non-unix systems when there is a single argument (spaces don't
4100 // separate arguments then).
Bram Moolenaar071d4272004-06-13 20:20:40 +00004101 if (!eap->usefilter
Bram Moolenaar63b92542007-03-27 14:57:09 +00004102 && !escaped
Bram Moolenaar071d4272004-06-13 20:20:40 +00004103 && eap->cmdidx != CMD_bang
Bram Moolenaar071d4272004-06-13 20:20:40 +00004104 && eap->cmdidx != CMD_grep
4105 && eap->cmdidx != CMD_grepadd
Bram Moolenaarbf15b8d2017-06-04 20:43:48 +02004106 && eap->cmdidx != CMD_hardcopy
Bram Moolenaar67883b42017-07-27 22:57:00 +02004107 && eap->cmdidx != CMD_lgrep
4108 && eap->cmdidx != CMD_lgrepadd
4109 && eap->cmdidx != CMD_lmake
4110 && eap->cmdidx != CMD_make
4111 && eap->cmdidx != CMD_terminal
Bram Moolenaar071d4272004-06-13 20:20:40 +00004112#ifndef UNIX
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004113 && !(eap->argt & EX_NOSPC)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004114#endif
4115 )
4116 {
4117 char_u *l;
4118#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004119 // Don't escape a backslash here, because rem_backslash() doesn't
4120 // remove it later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004121 static char_u *nobslash = (char_u *)" \t\"|";
4122# define ESCAPE_CHARS nobslash
4123#else
4124# define ESCAPE_CHARS escape_chars
4125#endif
4126
4127 for (l = repl; *l; ++l)
4128 if (vim_strchr(ESCAPE_CHARS, *l) != NULL)
4129 {
4130 l = vim_strsave_escaped(repl, ESCAPE_CHARS);
4131 if (l != NULL)
4132 {
4133 vim_free(repl);
4134 repl = l;
4135 }
4136 break;
4137 }
4138 }
4139
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004140 // For a shell command a '!' must be escaped.
Bram Moolenaar67883b42017-07-27 22:57:00 +02004141 if ((eap->usefilter || eap->cmdidx == CMD_bang
4142 || eap->cmdidx == CMD_terminal)
Bram Moolenaar31b7d382014-04-01 18:54:48 +02004143 && vim_strpbrk(repl, (char_u *)"!") != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004144 {
4145 char_u *l;
4146
Bram Moolenaar31b7d382014-04-01 18:54:48 +02004147 l = vim_strsave_escaped(repl, (char_u *)"!");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004148 if (l != NULL)
4149 {
4150 vim_free(repl);
4151 repl = l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004152 }
4153 }
4154
4155 p = repl_cmdline(eap, p, srclen, repl, cmdlinep);
4156 vim_free(repl);
4157 if (p == NULL)
4158 return FAIL;
4159 }
4160
4161 /*
4162 * One file argument: Expand wildcards.
4163 * Don't do this with ":r !command" or ":w !command".
4164 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004165 if ((eap->argt & EX_NOSPC) && !eap->usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004166 {
4167 /*
4168 * May do this twice:
4169 * 1. Replace environment variables.
4170 * 2. Replace any other wildcards, remove backslashes.
4171 */
4172 for (n = 1; n <= 2; ++n)
4173 {
4174 if (n == 2)
4175 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004176 /*
4177 * Halve the number of backslashes (this is Vi compatible).
4178 * For Unix and OS/2, when wildcards are expanded, this is
4179 * done by ExpandOne() below.
4180 */
Bram Moolenaare7fedb62015-12-31 19:07:19 +01004181#if defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004182 if (!has_wildcards)
4183#endif
4184 backslash_halve(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004185 }
4186
4187 if (has_wildcards)
4188 {
4189 if (n == 1)
4190 {
4191 /*
4192 * First loop: May expand environment variables. This
4193 * can be done much faster with expand_env() than with
4194 * something else (e.g., calling a shell).
4195 * After expanding environment variables, check again
4196 * if there are still wildcards present.
4197 */
4198 if (vim_strchr(eap->arg, '$') != NULL
4199 || vim_strchr(eap->arg, '~') != NULL)
4200 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +00004201 expand_env_esc(eap->arg, NameBuff, MAXPATHL,
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00004202 TRUE, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004203 has_wildcards = mch_has_wildcard(NameBuff);
4204 p = NameBuff;
4205 }
4206 else
4207 p = NULL;
4208 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004209 else // n == 2
Bram Moolenaar071d4272004-06-13 20:20:40 +00004210 {
4211 expand_T xpc;
Bram Moolenaarb40c2572019-10-19 21:01:05 +02004212 int options = WILD_LIST_NOTFOUND
4213 | WILD_NOERROR | WILD_ADD_SLASH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004214
4215 ExpandInit(&xpc);
4216 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar94950a92010-12-02 16:01:29 +01004217 if (p_wic)
4218 options += WILD_ICASE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004219 p = ExpandOne(&xpc, eap->arg, NULL,
Bram Moolenaar94950a92010-12-02 16:01:29 +01004220 options, WILD_EXPAND_FREE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004221 if (p == NULL)
4222 return FAIL;
4223 }
4224 if (p != NULL)
4225 {
4226 (void)repl_cmdline(eap, eap->arg, (int)STRLEN(eap->arg),
4227 p, cmdlinep);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004228 if (n == 2) // p came from ExpandOne()
Bram Moolenaar071d4272004-06-13 20:20:40 +00004229 vim_free(p);
4230 }
4231 }
4232 }
4233 }
4234 return OK;
4235}
4236
4237/*
4238 * Replace part of the command line, keeping eap->cmd, eap->arg and
4239 * eap->nextcmd correct.
4240 * "src" points to the part that is to be replaced, of length "srclen".
4241 * "repl" is the replacement string.
4242 * Returns a pointer to the character after the replaced string.
4243 * Returns NULL for failure.
4244 */
4245 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004246repl_cmdline(
4247 exarg_T *eap,
4248 char_u *src,
4249 int srclen,
4250 char_u *repl,
4251 char_u **cmdlinep)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004252{
4253 int len;
4254 int i;
4255 char_u *new_cmdline;
4256
4257 /*
4258 * The new command line is build in new_cmdline[].
4259 * First allocate it.
4260 * Careful: a "+cmd" argument may have been NUL terminated.
4261 */
4262 len = (int)STRLEN(repl);
4263 i = (int)(src - *cmdlinep) + (int)STRLEN(src + srclen) + len + 3;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004264 if (eap->nextcmd != NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004265 i += (int)STRLEN(eap->nextcmd);// add space for next command
Bram Moolenaar964b3742019-05-24 18:54:09 +02004266 if ((new_cmdline = alloc(i)) == NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004267 return NULL; // out of memory!
Bram Moolenaar071d4272004-06-13 20:20:40 +00004268
4269 /*
4270 * Copy the stuff before the expanded part.
4271 * Copy the expanded stuff.
4272 * Copy what came after the expanded part.
4273 * Copy the next commands, if there are any.
4274 */
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004275 i = (int)(src - *cmdlinep); // length of part before match
Bram Moolenaar071d4272004-06-13 20:20:40 +00004276 mch_memmove(new_cmdline, *cmdlinep, (size_t)i);
Bram Moolenaara3ffd9c2005-07-21 21:03:15 +00004277
Bram Moolenaar071d4272004-06-13 20:20:40 +00004278 mch_memmove(new_cmdline + i, repl, (size_t)len);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004279 i += len; // remember the end of the string
Bram Moolenaar071d4272004-06-13 20:20:40 +00004280 STRCPY(new_cmdline + i, src + srclen);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004281 src = new_cmdline + i; // remember where to continue
Bram Moolenaar071d4272004-06-13 20:20:40 +00004282
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004283 if (eap->nextcmd != NULL) // append next command
Bram Moolenaar071d4272004-06-13 20:20:40 +00004284 {
4285 i = (int)STRLEN(new_cmdline) + 1;
4286 STRCPY(new_cmdline + i, eap->nextcmd);
4287 eap->nextcmd = new_cmdline + i;
4288 }
4289 eap->cmd = new_cmdline + (eap->cmd - *cmdlinep);
4290 eap->arg = new_cmdline + (eap->arg - *cmdlinep);
4291 if (eap->do_ecmd_cmd != NULL && eap->do_ecmd_cmd != dollar_command)
4292 eap->do_ecmd_cmd = new_cmdline + (eap->do_ecmd_cmd - *cmdlinep);
4293 vim_free(*cmdlinep);
4294 *cmdlinep = new_cmdline;
4295
4296 return src;
4297}
4298
4299/*
4300 * Check for '|' to separate commands and '"' to start comments.
4301 */
4302 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004303separate_nextcmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004304{
4305 char_u *p;
4306
Bram Moolenaar86b68352004-12-27 21:59:20 +00004307#ifdef FEAT_QUICKFIX
Bram Moolenaar748bf032005-02-02 23:04:36 +00004308 p = skip_grep_pat(eap);
4309#else
4310 p = eap->arg;
Bram Moolenaar86b68352004-12-27 21:59:20 +00004311#endif
4312
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004313 for ( ; *p; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004314 {
4315 if (*p == Ctrl_V)
4316 {
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004317 if (eap->argt & (EX_CTRLV | EX_XFILE))
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004318 ++p; // skip CTRL-V and next char
Bram Moolenaar071d4272004-06-13 20:20:40 +00004319 else
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004320 // remove CTRL-V and skip next char
Bram Moolenaara7241f52008-06-24 20:39:31 +00004321 STRMOVE(p, p + 1);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004322 if (*p == NUL) // stop at NUL after CTRL-V
Bram Moolenaar071d4272004-06-13 20:20:40 +00004323 break;
4324 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004325
4326#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004327 // Skip over `=expr` when wildcards are expanded.
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004328 else if (p[0] == '`' && p[1] == '=' && (eap->argt & EX_XFILE))
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004329 {
4330 p += 2;
4331 (void)skip_expr(&p);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004332 }
4333#endif
4334
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004335 // Check for '"': start of comment or '|': next command
4336 // :@" and :*" do not start a comment!
4337 // :redir @" doesn't either.
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004338 else if ((*p == '"' && !(eap->argt & EX_NOTRLCOM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004339 && ((eap->cmdidx != CMD_at && eap->cmdidx != CMD_star)
4340 || p != eap->arg)
4341 && (eap->cmdidx != CMD_redir
4342 || p != eap->arg + 1 || p[-1] != '@'))
4343 || *p == '|' || *p == '\n')
4344 {
4345 /*
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004346 * We remove the '\' before the '|', unless EX_CTRLV is used
Bram Moolenaar071d4272004-06-13 20:20:40 +00004347 * AND 'b' is present in 'cpoptions'.
4348 */
4349 if ((vim_strchr(p_cpo, CPO_BAR) == NULL
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004350 || !(eap->argt & EX_CTRLV)) && *(p - 1) == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004351 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004352 STRMOVE(p - 1, p); // remove the '\'
Bram Moolenaar071d4272004-06-13 20:20:40 +00004353 --p;
4354 }
4355 else
4356 {
4357 eap->nextcmd = check_nextcmd(p);
4358 *p = NUL;
4359 break;
4360 }
4361 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004362 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00004363
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004364 if (!(eap->argt & EX_NOTRLCOM)) // remove trailing spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00004365 del_trailing_spaces(eap->arg);
4366}
4367
4368/*
4369 * get + command from ex argument
4370 */
4371 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004372getargcmd(char_u **argp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004373{
4374 char_u *arg = *argp;
4375 char_u *command = NULL;
4376
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004377 if (*arg == '+') // +[command]
Bram Moolenaar071d4272004-06-13 20:20:40 +00004378 {
4379 ++arg;
Bram Moolenaar3e451592014-04-02 14:22:05 +02004380 if (vim_isspace(*arg) || *arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004381 command = dollar_command;
4382 else
4383 {
4384 command = arg;
4385 arg = skip_cmd_arg(command, TRUE);
4386 if (*arg != NUL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004387 *arg++ = NUL; // terminate command with NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004388 }
4389
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004390 arg = skipwhite(arg); // skip over spaces
Bram Moolenaar071d4272004-06-13 20:20:40 +00004391 *argp = arg;
4392 }
4393 return command;
4394}
4395
4396/*
4397 * Find end of "+command" argument. Skip over "\ " and "\\".
4398 */
Bram Moolenaard0190392019-08-23 21:17:35 +02004399 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004400skip_cmd_arg(
4401 char_u *p,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004402 int rembs) // TRUE to halve the number of backslashes
Bram Moolenaar071d4272004-06-13 20:20:40 +00004403{
4404 while (*p && !vim_isspace(*p))
4405 {
4406 if (*p == '\\' && p[1] != NUL)
4407 {
4408 if (rembs)
Bram Moolenaara7241f52008-06-24 20:39:31 +00004409 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004410 else
4411 ++p;
4412 }
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004413 MB_PTR_ADV(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004414 }
4415 return p;
4416}
4417
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004418 int
4419get_bad_opt(char_u *p, exarg_T *eap)
4420{
4421 if (STRICMP(p, "keep") == 0)
4422 eap->bad_char = BAD_KEEP;
4423 else if (STRICMP(p, "drop") == 0)
4424 eap->bad_char = BAD_DROP;
4425 else if (MB_BYTE2LEN(*p) == 1 && p[1] == NUL)
4426 eap->bad_char = *p;
Bram Moolenaar75808492018-06-12 12:39:41 +02004427 else
4428 return FAIL;
4429 return OK;
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004430}
4431
Bram Moolenaar071d4272004-06-13 20:20:40 +00004432/*
4433 * Get "++opt=arg" argument.
4434 * Return FAIL or OK.
4435 */
4436 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004437getargopt(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004438{
4439 char_u *arg = eap->arg + 2;
4440 int *pp = NULL;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02004441 int bad_char_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004442 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004443
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004444 // ":edit ++[no]bin[ary] file"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004445 if (STRNCMP(arg, "bin", 3) == 0 || STRNCMP(arg, "nobin", 5) == 0)
4446 {
4447 if (*arg == 'n')
4448 {
4449 arg += 2;
4450 eap->force_bin = FORCE_NOBIN;
4451 }
4452 else
4453 eap->force_bin = FORCE_BIN;
4454 if (!checkforcmd(&arg, "binary", 3))
4455 return FAIL;
4456 eap->arg = skipwhite(arg);
4457 return OK;
4458 }
4459
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004460 // ":read ++edit file"
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004461 if (STRNCMP(arg, "edit", 4) == 0)
4462 {
4463 eap->read_edit = TRUE;
4464 eap->arg = skipwhite(arg + 4);
4465 return OK;
4466 }
4467
Bram Moolenaar071d4272004-06-13 20:20:40 +00004468 if (STRNCMP(arg, "ff", 2) == 0)
4469 {
4470 arg += 2;
4471 pp = &eap->force_ff;
4472 }
4473 else if (STRNCMP(arg, "fileformat", 10) == 0)
4474 {
4475 arg += 10;
4476 pp = &eap->force_ff;
4477 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004478 else if (STRNCMP(arg, "enc", 3) == 0)
4479 {
Bram Moolenaarb38e9ab2011-12-14 14:49:45 +01004480 if (STRNCMP(arg, "encoding", 8) == 0)
4481 arg += 8;
4482 else
4483 arg += 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004484 pp = &eap->force_enc;
4485 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004486 else if (STRNCMP(arg, "bad", 3) == 0)
4487 {
4488 arg += 3;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02004489 pp = &bad_char_idx;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004490 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004491
4492 if (pp == NULL || *arg != '=')
4493 return FAIL;
4494
4495 ++arg;
4496 *pp = (int)(arg - eap->cmd);
4497 arg = skip_cmd_arg(arg, FALSE);
4498 eap->arg = skipwhite(arg);
4499 *arg = NUL;
4500
Bram Moolenaar071d4272004-06-13 20:20:40 +00004501 if (pp == &eap->force_ff)
4502 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004503 if (check_ff_value(eap->cmd + eap->force_ff) == FAIL)
4504 return FAIL;
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004505 eap->force_ff = eap->cmd[eap->force_ff];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004506 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004507 else if (pp == &eap->force_enc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004508 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004509 // Make 'fileencoding' lower case.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004510 for (p = eap->cmd + eap->force_enc; *p != NUL; ++p)
4511 *p = TOLOWER_ASC(*p);
4512 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004513 else
4514 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004515 // Check ++bad= argument. Must be a single-byte character, "keep" or
4516 // "drop".
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004517 if (get_bad_opt(eap->cmd + bad_char_idx, eap) == FAIL)
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004518 return FAIL;
4519 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004520
4521 return OK;
4522}
4523
Bram Moolenaar071d4272004-06-13 20:20:40 +00004524 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004525ex_autocmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004526{
4527 /*
Bram Moolenaar26d4b892018-07-04 22:26:28 +02004528 * Disallow autocommands from .exrc and .vimrc in current
Bram Moolenaar071d4272004-06-13 20:20:40 +00004529 * directory for security reasons.
4530 */
4531 if (secure)
4532 {
4533 secure = 2;
4534 eap->errmsg = e_curdir;
4535 }
4536 else if (eap->cmdidx == CMD_autocmd)
4537 do_autocmd(eap->arg, eap->forceit);
4538 else
4539 do_augroup(eap->arg, eap->forceit);
4540}
4541
4542/*
4543 * ":doautocmd": Apply the automatic commands to the current buffer.
4544 */
4545 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004546ex_doautocmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004547{
Bram Moolenaar60542ac2012-02-12 20:14:01 +01004548 char_u *arg = eap->arg;
4549 int call_do_modelines = check_nomodeline(&arg);
Bram Moolenaar1610d052016-06-09 22:53:01 +02004550 int did_aucmd;
Bram Moolenaar60542ac2012-02-12 20:14:01 +01004551
Bram Moolenaar1610d052016-06-09 22:53:01 +02004552 (void)do_doautocmd(arg, TRUE, &did_aucmd);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004553 // Only when there is no <nomodeline>.
Bram Moolenaar1610d052016-06-09 22:53:01 +02004554 if (call_do_modelines && did_aucmd)
Bram Moolenaar60542ac2012-02-12 20:14:01 +01004555 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004556}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004557
Bram Moolenaar071d4272004-06-13 20:20:40 +00004558/*
4559 * :[N]bunload[!] [N] [bufname] unload buffer
4560 * :[N]bdelete[!] [N] [bufname] delete buffer from buffer list
4561 * :[N]bwipeout[!] [N] [bufname] delete buffer really
4562 */
4563 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004564ex_bunload(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004565{
Bram Moolenaar8cdbd5b2019-06-16 15:50:45 +02004566 if (ERROR_IF_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02004567 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004568 eap->errmsg = do_bufdel(
4569 eap->cmdidx == CMD_bdelete ? DOBUF_DEL
4570 : eap->cmdidx == CMD_bwipeout ? DOBUF_WIPE
4571 : DOBUF_UNLOAD, eap->arg,
4572 eap->addr_count, (int)eap->line1, (int)eap->line2, eap->forceit);
4573}
4574
4575/*
4576 * :[N]buffer [N] to buffer N
4577 * :[N]sbuffer [N] to buffer N
4578 */
4579 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004580ex_buffer(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004581{
Bram Moolenaar8cdbd5b2019-06-16 15:50:45 +02004582 if (ERROR_IF_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02004583 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004584 if (*eap->arg)
4585 eap->errmsg = e_trailing;
4586 else
4587 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004588 if (eap->addr_count == 0) // default is current buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00004589 goto_buffer(eap, DOBUF_CURRENT, FORWARD, 0);
4590 else
4591 goto_buffer(eap, DOBUF_FIRST, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02004592 if (eap->do_ecmd_cmd != NULL)
4593 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004594 }
4595}
4596
4597/*
4598 * :[N]bmodified [N] to next mod. buffer
4599 * :[N]sbmodified [N] to next mod. buffer
4600 */
4601 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004602ex_bmodified(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004603{
4604 goto_buffer(eap, DOBUF_MOD, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02004605 if (eap->do_ecmd_cmd != NULL)
4606 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004607}
4608
4609/*
4610 * :[N]bnext [N] to next buffer
4611 * :[N]sbnext [N] split and to next buffer
4612 */
4613 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004614ex_bnext(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004615{
4616 goto_buffer(eap, DOBUF_CURRENT, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02004617 if (eap->do_ecmd_cmd != NULL)
4618 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004619}
4620
4621/*
4622 * :[N]bNext [N] to previous buffer
4623 * :[N]bprevious [N] to previous buffer
4624 * :[N]sbNext [N] split and to previous buffer
4625 * :[N]sbprevious [N] split and to previous buffer
4626 */
4627 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004628ex_bprevious(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004629{
4630 goto_buffer(eap, DOBUF_CURRENT, BACKWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02004631 if (eap->do_ecmd_cmd != NULL)
4632 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004633}
4634
4635/*
4636 * :brewind to first buffer
4637 * :bfirst to first buffer
4638 * :sbrewind split and to first buffer
4639 * :sbfirst split and to first buffer
4640 */
4641 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004642ex_brewind(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004643{
4644 goto_buffer(eap, DOBUF_FIRST, FORWARD, 0);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02004645 if (eap->do_ecmd_cmd != NULL)
4646 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004647}
4648
4649/*
4650 * :blast to last buffer
4651 * :sblast split and to last buffer
4652 */
4653 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004654ex_blast(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004655{
4656 goto_buffer(eap, DOBUF_LAST, BACKWARD, 0);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02004657 if (eap->do_ecmd_cmd != NULL)
4658 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004659}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004660
4661 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004662ends_excmd(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004663{
4664 return (c == NUL || c == '|' || c == '"' || c == '\n');
4665}
4666
4667#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA) || defined(FEAT_EVAL) \
4668 || defined(PROTO)
4669/*
4670 * Return the next command, after the first '|' or '\n'.
4671 * Return NULL if not found.
4672 */
4673 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004674find_nextcmd(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004675{
4676 while (*p != '|' && *p != '\n')
4677 {
4678 if (*p == NUL)
4679 return NULL;
4680 ++p;
4681 }
4682 return (p + 1);
4683}
4684#endif
4685
4686/*
Bram Moolenaar2256c992016-11-15 21:17:07 +01004687 * Check if *p is a separator between Ex commands, skipping over white space.
4688 * Return NULL if it isn't, the following character if it is.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004689 */
4690 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004691check_nextcmd(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004692{
Bram Moolenaar2256c992016-11-15 21:17:07 +01004693 char_u *s = skipwhite(p);
4694
4695 if (*s == '|' || *s == '\n')
4696 return (s + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004697 else
4698 return NULL;
4699}
4700
4701/*
4702 * - if there are more files to edit
4703 * - and this is the last window
4704 * - and forceit not used
4705 * - and not repeated twice on a row
4706 * return FAIL and give error message if 'message' TRUE
4707 * return OK otherwise
4708 */
4709 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004710check_more(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004711 int message, // when FALSE check only, no messages
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004712 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004713{
4714 int n = ARGCOUNT - curwin->w_arg_idx - 1;
4715
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00004716 if (!forceit && only_one_window()
4717 && ARGCOUNT > 1 && !arg_had_last && n >= 0 && quitmore == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004718 {
4719 if (message)
4720 {
4721#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
4722 if ((p_confirm || cmdmod.confirm) && curbuf->b_fname != NULL)
4723 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02004724 char_u buff[DIALOG_MSG_SIZE];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004725
Bram Moolenaarda6e8912018-08-21 15:12:14 +02004726 vim_snprintf((char *)buff, DIALOG_MSG_SIZE,
4727 NGETTEXT("%d more file to edit. Quit anyway?",
4728 "%d more files to edit. Quit anyway?", n), n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004729 if (vim_dialog_yesno(VIM_QUESTION, NULL, buff, 1) == VIM_YES)
4730 return OK;
4731 return FAIL;
4732 }
4733#endif
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01004734 semsg(NGETTEXT("E173: %d more file to edit",
4735 "E173: %d more files to edit", n), n);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004736 quitmore = 2; // next try to quit is allowed
Bram Moolenaar071d4272004-06-13 20:20:40 +00004737 }
4738 return FAIL;
4739 }
4740 return OK;
4741}
4742
Bram Moolenaar071d4272004-06-13 20:20:40 +00004743/*
4744 * Function given to ExpandGeneric() to obtain the list of command names.
4745 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004746 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004747get_command_name(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004748{
4749 if (idx >= (int)CMD_SIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004750 return get_user_command_name(idx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004751 return cmdnames[idx].cmd_name;
4752}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004753
Bram Moolenaar071d4272004-06-13 20:20:40 +00004754 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004755ex_colorscheme(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004756{
Bram Moolenaare6850792010-05-14 15:28:44 +02004757 if (*eap->arg == NUL)
4758 {
4759#ifdef FEAT_EVAL
4760 char_u *expr = vim_strsave((char_u *)"g:colors_name");
4761 char_u *p = NULL;
4762
4763 if (expr != NULL)
4764 {
4765 ++emsg_off;
4766 p = eval_to_string(expr, NULL, FALSE);
4767 --emsg_off;
4768 vim_free(expr);
4769 }
4770 if (p != NULL)
4771 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01004772 msg((char *)p);
Bram Moolenaare6850792010-05-14 15:28:44 +02004773 vim_free(p);
4774 }
4775 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01004776 msg("default");
Bram Moolenaare6850792010-05-14 15:28:44 +02004777#else
Bram Moolenaar32526b32019-01-19 17:43:09 +01004778 msg(_("unknown"));
Bram Moolenaare6850792010-05-14 15:28:44 +02004779#endif
4780 }
4781 else if (load_colors(eap->arg) == FAIL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004782 semsg(_("E185: Cannot find color scheme '%s'"), eap->arg);
Bram Moolenaarf58d81a2019-01-28 20:19:05 +01004783
4784#ifdef FEAT_VTP
4785 else if (has_vtp_working())
4786 {
4787 // background color change requires clear + redraw
4788 update_screen(CLEAR);
4789 redrawcmd();
4790 }
4791#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004792}
4793
4794 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004795ex_highlight(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004796{
4797 if (*eap->arg == NUL && eap->cmd[2] == '!')
Bram Moolenaar32526b32019-01-19 17:43:09 +01004798 msg(_("Greetings, Vim user!"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004799 do_highlight(eap->arg, eap->forceit, FALSE);
4800}
4801
4802
4803/*
4804 * Call this function if we thought we were going to exit, but we won't
4805 * (because of an error). May need to restore the terminal mode.
4806 */
4807 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004808not_exiting(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004809{
4810 exiting = FALSE;
4811 settmode(TMODE_RAW);
4812}
4813
Bram Moolenaar12a96de2018-03-11 14:44:18 +01004814 static int
4815before_quit_autocmds(win_T *wp, int quit_all, int forceit)
4816{
4817 apply_autocmds(EVENT_QUITPRE, NULL, NULL, FALSE, wp->w_buffer);
4818
Bram Moolenaar34ba06b2019-10-20 22:27:10 +02004819 // Bail out when autocommands closed the window.
4820 // Refuse to quit when the buffer in the last window is being closed (can
4821 // only happen in autocommands).
Bram Moolenaar12a96de2018-03-11 14:44:18 +01004822 if (!win_valid(wp)
4823 || curbuf_locked()
4824 || (wp->w_buffer->b_nwindows == 1 && wp->w_buffer->b_locked > 0))
4825 return TRUE;
4826
4827 if (quit_all || (check_more(FALSE, forceit) == OK && only_one_window()))
4828 {
4829 apply_autocmds(EVENT_EXITPRE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar34ba06b2019-10-20 22:27:10 +02004830 // Refuse to quit when locked or when the window was closed or the
4831 // buffer in the last window is being closed (can only happen in
4832 // autocommands).
4833 if (!win_valid(wp) || curbuf_locked()
Bram Moolenaar12a96de2018-03-11 14:44:18 +01004834 || (curbuf->b_nwindows == 1 && curbuf->b_locked > 0))
4835 return TRUE;
4836 }
4837
4838 return FALSE;
4839}
4840
Bram Moolenaar071d4272004-06-13 20:20:40 +00004841/*
Bram Moolenaarf240e182014-11-27 18:33:02 +01004842 * ":quit": quit current window, quit Vim if the last window is closed.
Bram Moolenaar12a96de2018-03-11 14:44:18 +01004843 * ":{nr}quit": quit window {nr}
Bram Moolenaar4d14bac2019-10-20 21:15:15 +02004844 * Also used when closing a terminal window that's the last one.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004845 */
Bram Moolenaar4d14bac2019-10-20 21:15:15 +02004846 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004847ex_quit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004848{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004849 win_T *wp;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004850
Bram Moolenaar071d4272004-06-13 20:20:40 +00004851#ifdef FEAT_CMDWIN
4852 if (cmdwin_type != 0)
4853 {
4854 cmdwin_result = Ctrl_C;
4855 return;
4856 }
4857#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004858 // Don't quit while editing the command line.
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00004859 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00004860 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00004861 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00004862 return;
4863 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004864 if (eap->addr_count > 0)
4865 {
Bram Moolenaarf240e182014-11-27 18:33:02 +01004866 int wnr = eap->line2;
4867
4868 for (wp = firstwin; wp->w_next != NULL; wp = wp->w_next)
4869 if (--wnr <= 0)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004870 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004871 }
4872 else
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004873 wp = curwin;
Bram Moolenaarf240e182014-11-27 18:33:02 +01004874
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004875 // Refuse to quit when locked.
Bram Moolenaar87ffb5c2017-10-19 12:37:42 +02004876 if (curbuf_locked())
4877 return;
Bram Moolenaar12a96de2018-03-11 14:44:18 +01004878
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004879 // Trigger QuitPre and maybe ExitPre
Bram Moolenaar12a96de2018-03-11 14:44:18 +01004880 if (before_quit_autocmds(wp, FALSE, eap->forceit))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004881 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004882
4883#ifdef FEAT_NETBEANS_INTG
4884 netbeansForcedQuit = eap->forceit;
4885#endif
4886
4887 /*
4888 * If there are more files or windows we won't exit.
4889 */
4890 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
4891 exiting = TRUE;
Bram Moolenaarff930ca2017-10-19 17:12:10 +02004892 if ((!buf_hide(wp->w_buffer)
4893 && check_changed(wp->w_buffer, (p_awa ? CCGD_AW : 0)
Bram Moolenaar45d3b142013-11-09 03:31:51 +01004894 | (eap->forceit ? CCGD_FORCEIT : 0)
4895 | CCGD_EXCMD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004896 || check_more(TRUE, eap->forceit) == FAIL
Bram Moolenaar027387f2016-01-02 22:25:52 +01004897 || (only_one_window() && check_changed_any(eap->forceit, TRUE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004898 {
4899 not_exiting();
4900 }
4901 else
4902 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004903 // quit last window
4904 // Note: only_one_window() returns true, even so a help window is
4905 // still open. In that case only quit, if no address has been
4906 // specified. Example:
4907 // :h|wincmd w|1q - don't quit
4908 // :h|wincmd w|q - quit
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01004909 if (only_one_window() && (ONE_WINDOW || eap->addr_count == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004910 getout(0);
Bram Moolenaar2c33d7b2017-10-14 16:06:20 +02004911 not_exiting();
Bram Moolenaar4033c552017-09-16 20:54:51 +02004912#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004913 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02004914#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004915 // close window; may free buffer
Bram Moolenaareb44a682017-08-03 22:44:55 +02004916 win_close(wp, !buf_hide(wp->w_buffer) || eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004917 }
4918}
4919
4920/*
4921 * ":cquit".
4922 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004923 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004924ex_cquit(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004925{
Bram Moolenaar1860bde2020-01-06 21:47:21 +01004926 // this does not always pass on the exit code to the Manx compiler. why?
4927 getout(eap->addr_count > 0 ? (int)eap->line2 : EXIT_FAILURE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004928}
4929
4930/*
4931 * ":qall": try to quit all windows
4932 */
4933 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004934ex_quit_all(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004935{
4936# ifdef FEAT_CMDWIN
4937 if (cmdwin_type != 0)
4938 {
4939 if (eap->forceit)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004940 cmdwin_result = K_XF1; // ex_window() takes care of this
Bram Moolenaar071d4272004-06-13 20:20:40 +00004941 else
4942 cmdwin_result = K_XF2;
4943 return;
4944 }
4945# endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00004946
Bram Moolenaar217e1b82019-12-01 21:41:28 +01004947 // Don't quit while editing the command line.
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00004948 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00004949 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00004950 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00004951 return;
4952 }
Bram Moolenaar12a96de2018-03-11 14:44:18 +01004953
4954 if (before_quit_autocmds(curwin, TRUE, eap->forceit))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004955 return;
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00004956
Bram Moolenaar071d4272004-06-13 20:20:40 +00004957 exiting = TRUE;
Bram Moolenaar027387f2016-01-02 22:25:52 +01004958 if (eap->forceit || !check_changed_any(FALSE, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004959 getout(0);
4960 not_exiting();
4961}
4962
Bram Moolenaar071d4272004-06-13 20:20:40 +00004963/*
4964 * ":close": close current window, unless it is the last one
4965 */
4966 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004967ex_close(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004968{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004969 win_T *win;
4970 int winnr = 0;
Bram Moolenaar4033c552017-09-16 20:54:51 +02004971#ifdef FEAT_CMDWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00004972 if (cmdwin_type != 0)
Bram Moolenaar9bd1a7e2011-05-19 14:50:54 +02004973 cmdwin_result = Ctrl_C;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004974 else
Bram Moolenaar4033c552017-09-16 20:54:51 +02004975#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004976 if (!text_locked() && !curbuf_locked())
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004977 {
4978 if (eap->addr_count == 0)
4979 ex_win_close(eap->forceit, curwin, NULL);
Bram Moolenaar6d41c782018-06-06 09:11:12 +02004980 else
4981 {
Bram Moolenaar29323592016-07-24 22:04:11 +02004982 FOR_ALL_WINDOWS(win)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004983 {
4984 winnr++;
4985 if (winnr == eap->line2)
4986 break;
4987 }
4988 if (win == NULL)
4989 win = lastwin;
4990 ex_win_close(eap->forceit, win, NULL);
4991 }
4992 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004993}
4994
Bram Moolenaar4033c552017-09-16 20:54:51 +02004995#ifdef FEAT_QUICKFIX
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004996/*
4997 * ":pclose": Close any preview window.
4998 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004999 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005000ex_pclose(exarg_T *eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005001{
5002 win_T *win;
5003
Bram Moolenaar79648732019-07-18 21:43:07 +02005004 // First close any normal window.
Bram Moolenaar29323592016-07-24 22:04:11 +02005005 FOR_ALL_WINDOWS(win)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005006 if (win->w_p_pvw)
5007 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00005008 ex_win_close(eap->forceit, win, NULL);
Bram Moolenaar79648732019-07-18 21:43:07 +02005009 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005010 }
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01005011# ifdef FEAT_PROP_POPUP
Bram Moolenaar79648732019-07-18 21:43:07 +02005012 // Also when 'previewpopup' is empty, it might have been cleared.
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02005013 popup_close_preview();
Bram Moolenaar79648732019-07-18 21:43:07 +02005014# endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005015}
Bram Moolenaar4033c552017-09-16 20:54:51 +02005016#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005017
Bram Moolenaarf740b292006-02-16 22:11:02 +00005018/*
5019 * Close window "win" and take care of handling closing the last window for a
5020 * modified buffer.
5021 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005022 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005023ex_win_close(
5024 int forceit,
5025 win_T *win,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005026 tabpage_T *tp) // NULL or the tab page "win" is in
Bram Moolenaar071d4272004-06-13 20:20:40 +00005027{
5028 int need_hide;
5029 buf_T *buf = win->w_buffer;
5030
5031 need_hide = (bufIsChanged(buf) && buf->b_nwindows <= 1);
Bram Moolenaareb44a682017-08-03 22:44:55 +02005032 if (need_hide && !buf_hide(buf) && !forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005033 {
Bram Moolenaar4033c552017-09-16 20:54:51 +02005034#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005035 if ((p_confirm || cmdmod.confirm) && p_write)
5036 {
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005037 bufref_T bufref;
5038
5039 set_bufref(&bufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005040 dialog_changed(buf, FALSE);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005041 if (bufref_valid(&bufref) && bufIsChanged(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005042 return;
5043 need_hide = FALSE;
5044 }
5045 else
Bram Moolenaar4033c552017-09-16 20:54:51 +02005046#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005047 {
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02005048 no_write_message();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005049 return;
5050 }
5051 }
5052
Bram Moolenaar4033c552017-09-16 20:54:51 +02005053#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00005054 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005055#endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00005056
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005057 // free buffer when not hiding it or when it's a scratch buffer
Bram Moolenaarf740b292006-02-16 22:11:02 +00005058 if (tp == NULL)
Bram Moolenaareb44a682017-08-03 22:44:55 +02005059 win_close(win, !need_hide && !buf_hide(buf));
Bram Moolenaarf740b292006-02-16 22:11:02 +00005060 else
Bram Moolenaareb44a682017-08-03 22:44:55 +02005061 win_close_othertab(win, !need_hide && !buf_hide(buf), tp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005062}
5063
Bram Moolenaar071d4272004-06-13 20:20:40 +00005064/*
Bram Moolenaardea25702017-01-29 15:18:10 +01005065 * Handle the argument for a tabpage related ex command.
5066 * Returns a tabpage number.
5067 * When an error is encountered then eap->errmsg is set.
5068 */
5069 static int
5070get_tabpage_arg(exarg_T *eap)
5071{
5072 int tab_number;
5073 int unaccept_arg0 = (eap->cmdidx == CMD_tabmove) ? 0 : 1;
5074
5075 if (eap->arg && *eap->arg != NUL)
5076 {
5077 char_u *p = eap->arg;
5078 char_u *p_save;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005079 int relative = 0; // argument +N/-N means: go to N places to the
5080 // right/left relative to the current position.
Bram Moolenaardea25702017-01-29 15:18:10 +01005081
5082 if (*p == '-')
5083 {
5084 relative = -1;
5085 p++;
5086 }
5087 else if (*p == '+')
5088 {
5089 relative = 1;
5090 p++;
5091 }
5092
5093 p_save = p;
5094 tab_number = getdigits(&p);
5095
5096 if (relative == 0)
5097 {
5098 if (STRCMP(p, "$") == 0)
5099 tab_number = LAST_TAB_NR;
5100 else if (p == p_save || *p_save == '-' || *p != NUL
5101 || tab_number > LAST_TAB_NR)
5102 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005103 // No numbers as argument.
Bram Moolenaardea25702017-01-29 15:18:10 +01005104 eap->errmsg = e_invarg;
5105 goto theend;
5106 }
5107 }
5108 else
5109 {
5110 if (*p_save == NUL)
5111 tab_number = 1;
5112 else if (p == p_save || *p_save == '-' || *p != NUL
5113 || tab_number == 0)
5114 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005115 // No numbers as argument.
Bram Moolenaardea25702017-01-29 15:18:10 +01005116 eap->errmsg = e_invarg;
5117 goto theend;
5118 }
5119 tab_number = tab_number * relative + tabpage_index(curtab);
5120 if (!unaccept_arg0 && relative == -1)
5121 --tab_number;
5122 }
5123 if (tab_number < unaccept_arg0 || tab_number > LAST_TAB_NR)
5124 eap->errmsg = e_invarg;
5125 }
5126 else if (eap->addr_count > 0)
5127 {
5128 if (unaccept_arg0 && eap->line2 == 0)
Bram Moolenaarc6251552017-01-29 21:42:20 +01005129 {
Bram Moolenaardea25702017-01-29 15:18:10 +01005130 eap->errmsg = e_invrange;
Bram Moolenaarc6251552017-01-29 21:42:20 +01005131 tab_number = 0;
5132 }
Bram Moolenaardea25702017-01-29 15:18:10 +01005133 else
5134 {
5135 tab_number = eap->line2;
Bram Moolenaar82a12462019-01-22 22:41:42 +01005136 if (!unaccept_arg0 && *skipwhite(*eap->cmdlinep) == '-')
Bram Moolenaardea25702017-01-29 15:18:10 +01005137 {
5138 --tab_number;
5139 if (tab_number < unaccept_arg0)
5140 eap->errmsg = e_invarg;
5141 }
5142 }
5143 }
5144 else
5145 {
5146 switch (eap->cmdidx)
5147 {
5148 case CMD_tabnext:
5149 tab_number = tabpage_index(curtab) + 1;
5150 if (tab_number > LAST_TAB_NR)
5151 tab_number = 1;
5152 break;
5153 case CMD_tabmove:
5154 tab_number = LAST_TAB_NR;
5155 break;
5156 default:
5157 tab_number = tabpage_index(curtab);
5158 }
5159 }
5160
5161theend:
5162 return tab_number;
5163}
5164
5165/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00005166 * ":tabclose": close current tab page, unless it is the last one.
5167 * ":tabclose N": close tab page N.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005168 */
5169 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005170ex_tabclose(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005171{
Bram Moolenaarf740b292006-02-16 22:11:02 +00005172 tabpage_T *tp;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005173 int tab_number;
Bram Moolenaarf740b292006-02-16 22:11:02 +00005174
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005175# ifdef FEAT_CMDWIN
5176 if (cmdwin_type != 0)
5177 cmdwin_result = K_IGNORE;
5178 else
5179# endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00005180 if (first_tabpage->tp_next == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005181 emsg(_("E784: Cannot close last tab page"));
Bram Moolenaarf740b292006-02-16 22:11:02 +00005182 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005183 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005184 tab_number = get_tabpage_arg(eap);
5185 if (eap->errmsg == NULL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005186 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005187 tp = find_tabpage(tab_number);
Bram Moolenaarf740b292006-02-16 22:11:02 +00005188 if (tp == NULL)
5189 {
5190 beep_flush();
5191 return;
5192 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005193 if (tp != curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00005194 {
5195 tabpage_close_other(tp, eap->forceit);
5196 return;
5197 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005198 else if (!text_locked() && !curbuf_locked())
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005199 tabpage_close(eap->forceit);
5200 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005201 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005202}
5203
5204/*
5205 * ":tabonly": close all tab pages except the current one
5206 */
5207 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005208ex_tabonly(exarg_T *eap)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005209{
5210 tabpage_T *tp;
5211 int done;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005212 int tab_number;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005213
5214# ifdef FEAT_CMDWIN
5215 if (cmdwin_type != 0)
5216 cmdwin_result = K_IGNORE;
5217 else
5218# endif
5219 if (first_tabpage->tp_next == NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01005220 msg(_("Already only one tab page"));
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005221 else
5222 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005223 tab_number = get_tabpage_arg(eap);
5224 if (eap->errmsg == NULL)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005225 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005226 goto_tabpage(tab_number);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005227 // Repeat this up to a 1000 times, because autocommands may
5228 // mess up the lists.
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005229 for (done = 0; done < 1000; ++done)
5230 {
5231 FOR_ALL_TABPAGES(tp)
5232 if (tp->tp_topframe != topframe)
5233 {
5234 tabpage_close_other(tp, eap->forceit);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005235 // if we failed to close it quit
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005236 if (valid_tabpage(tp))
5237 done = 1000;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005238 // start over, "tp" is now invalid
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005239 break;
5240 }
5241 if (first_tabpage->tp_next == NULL)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005242 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005243 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005244 }
5245 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005246}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005247
5248/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00005249 * Close the current tab page.
5250 */
5251 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005252tabpage_close(int forceit)
Bram Moolenaarf740b292006-02-16 22:11:02 +00005253{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005254 // First close all the windows but the current one. If that worked then
5255 // close the last window in this tab, that will close it.
Bram Moolenaar459ca562016-11-10 18:16:33 +01005256 if (!ONE_WINDOW)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005257 close_others(TRUE, forceit);
Bram Moolenaar459ca562016-11-10 18:16:33 +01005258 if (ONE_WINDOW)
Bram Moolenaarf740b292006-02-16 22:11:02 +00005259 ex_win_close(forceit, curwin, NULL);
5260# ifdef FEAT_GUI
5261 need_mouse_correct = TRUE;
5262# endif
5263}
5264
5265/*
5266 * Close tab page "tp", which is not the current tab page.
5267 * Note that autocommands may make "tp" invalid.
Bram Moolenaar7875acc2006-09-10 13:51:17 +00005268 * Also takes care of the tab pages line disappearing when closing the
5269 * last-but-one tab page.
Bram Moolenaarf740b292006-02-16 22:11:02 +00005270 */
5271 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005272tabpage_close_other(tabpage_T *tp, int forceit)
Bram Moolenaarf740b292006-02-16 22:11:02 +00005273{
5274 int done = 0;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005275 win_T *wp;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00005276 int h = tabline_height();
Bram Moolenaarf740b292006-02-16 22:11:02 +00005277
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005278 // Limit to 1000 windows, autocommands may add a window while we close
5279 // one. OK, so I'm paranoid...
Bram Moolenaarf740b292006-02-16 22:11:02 +00005280 while (++done < 1000)
5281 {
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005282 wp = tp->tp_firstwin;
5283 ex_win_close(forceit, wp, tp);
Bram Moolenaarf740b292006-02-16 22:11:02 +00005284
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005285 // Autocommands may delete the tab page under our fingers and we may
5286 // fail to close a window with a modified buffer.
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005287 if (!valid_tabpage(tp) || tp->tp_firstwin == wp)
Bram Moolenaarf740b292006-02-16 22:11:02 +00005288 break;
5289 }
Bram Moolenaar7875acc2006-09-10 13:51:17 +00005290
Bram Moolenaar29323592016-07-24 22:04:11 +02005291 apply_autocmds(EVENT_TABCLOSED, NULL, NULL, FALSE, curbuf);
Bram Moolenaar29323592016-07-24 22:04:11 +02005292
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005293 redraw_tabline = TRUE;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00005294 if (h != tabline_height())
5295 shell_new_rows();
Bram Moolenaarf740b292006-02-16 22:11:02 +00005296}
5297
5298/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005299 * ":only".
5300 */
5301 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005302ex_only(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005303{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005304 win_T *wp;
5305 int wnr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005306# ifdef FEAT_GUI
5307 need_mouse_correct = TRUE;
5308# endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005309 if (eap->addr_count > 0)
5310 {
5311 wnr = eap->line2;
5312 for (wp = firstwin; --wnr > 0; )
5313 {
5314 if (wp->w_next == NULL)
5315 break;
5316 else
5317 wp = wp->w_next;
5318 }
5319 win_goto(wp);
5320 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005321 close_others(TRUE, eap->forceit);
5322}
5323
Bram Moolenaar071d4272004-06-13 20:20:40 +00005324 static void
Bram Moolenaar5e1e6d22017-01-02 17:26:00 +01005325ex_hide(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005326{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005327 // ":hide" or ":hide | cmd": hide current window
Bram Moolenaar2256c992016-11-15 21:17:07 +01005328 if (!eap->skip)
5329 {
Bram Moolenaar4033c552017-09-16 20:54:51 +02005330#ifdef FEAT_GUI
Bram Moolenaar2256c992016-11-15 21:17:07 +01005331 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005332#endif
Bram Moolenaar2256c992016-11-15 21:17:07 +01005333 if (eap->addr_count == 0)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005334 win_close(curwin, FALSE); // don't free buffer
Bram Moolenaar2256c992016-11-15 21:17:07 +01005335 else
5336 {
5337 int winnr = 0;
5338 win_T *win;
Bram Moolenaarf240e182014-11-27 18:33:02 +01005339
Bram Moolenaar2256c992016-11-15 21:17:07 +01005340 FOR_ALL_WINDOWS(win)
5341 {
5342 winnr++;
5343 if (winnr == eap->line2)
5344 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005345 }
Bram Moolenaar2256c992016-11-15 21:17:07 +01005346 if (win == NULL)
5347 win = lastwin;
5348 win_close(win, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005349 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005350 }
5351}
5352
5353/*
5354 * ":stop" and ":suspend": Suspend Vim.
5355 */
5356 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005357ex_stop(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005358{
5359 /*
5360 * Disallow suspending for "rvim".
5361 */
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005362 if (!check_restricted())
Bram Moolenaar071d4272004-06-13 20:20:40 +00005363 {
5364 if (!eap->forceit)
5365 autowrite_all();
5366 windgoto((int)Rows - 1, 0);
5367 out_char('\n');
5368 out_flush();
5369 stoptermcap();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005370 out_flush(); // needed for SUN to restore xterm buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00005371#ifdef FEAT_TITLE
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005372 mch_restore_title(SAVE_RESTORE_BOTH); // restore window titles
Bram Moolenaar071d4272004-06-13 20:20:40 +00005373#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005374 ui_suspend(); // call machine specific function
Bram Moolenaar071d4272004-06-13 20:20:40 +00005375#ifdef FEAT_TITLE
5376 maketitle();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005377 resettitle(); // force updating the title
Bram Moolenaar071d4272004-06-13 20:20:40 +00005378#endif
5379 starttermcap();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005380 scroll_start(); // scroll screen before redrawing
Bram Moolenaar071d4272004-06-13 20:20:40 +00005381 redraw_later_clear();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005382 shell_resized(); // may have resized window
Bram Moolenaar071d4272004-06-13 20:20:40 +00005383 }
5384}
5385
5386/*
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005387 * ":exit", ":xit" and ":wq": Write file and quite the current window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005388 */
5389 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005390ex_exit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005391{
5392#ifdef FEAT_CMDWIN
5393 if (cmdwin_type != 0)
5394 {
5395 cmdwin_result = Ctrl_C;
5396 return;
5397 }
5398#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005399 // Don't quit while editing the command line.
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005400 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005401 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005402 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005403 return;
5404 }
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005405
5406 if (before_quit_autocmds(curwin, FALSE, eap->forceit))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005407 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005408
5409 /*
5410 * if more files or windows we won't exit
5411 */
5412 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
5413 exiting = TRUE;
5414 if ( ((eap->cmdidx == CMD_wq
5415 || curbufIsChanged())
5416 && do_write(eap) == FAIL)
5417 || check_more(TRUE, eap->forceit) == FAIL
Bram Moolenaar027387f2016-01-02 22:25:52 +01005418 || (only_one_window() && check_changed_any(eap->forceit, FALSE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005419 {
5420 not_exiting();
5421 }
5422 else
5423 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005424 if (only_one_window()) // quit last window, exit Vim
Bram Moolenaar071d4272004-06-13 20:20:40 +00005425 getout(0);
Bram Moolenaar2c33d7b2017-10-14 16:06:20 +02005426 not_exiting();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005427# ifdef FEAT_GUI
5428 need_mouse_correct = TRUE;
5429# endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005430 // Quit current window, may free the buffer.
Bram Moolenaareb44a682017-08-03 22:44:55 +02005431 win_close(curwin, !buf_hide(curwin->w_buffer));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005432 }
5433}
5434
5435/*
5436 * ":print", ":list", ":number".
5437 */
5438 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005439ex_print(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005440{
Bram Moolenaardf177f62005-02-22 08:39:57 +00005441 if (curbuf->b_ml.ml_flags & ML_EMPTY)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005442 emsg(_(e_emptybuf));
Bram Moolenaardf177f62005-02-22 08:39:57 +00005443 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005444 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00005445 for ( ;!got_int; ui_breakcheck())
5446 {
5447 print_line(eap->line1,
5448 (eap->cmdidx == CMD_number || eap->cmdidx == CMD_pound
5449 || (eap->flags & EXFLAG_NR)),
5450 eap->cmdidx == CMD_list || (eap->flags & EXFLAG_LIST));
5451 if (++eap->line1 > eap->line2)
5452 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005453 out_flush(); // show one line at a time
Bram Moolenaardf177f62005-02-22 08:39:57 +00005454 }
5455 setpcmark();
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005456 // put cursor at last line
Bram Moolenaardf177f62005-02-22 08:39:57 +00005457 curwin->w_cursor.lnum = eap->line2;
5458 beginline(BL_SOL | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005459 }
5460
Bram Moolenaar071d4272004-06-13 20:20:40 +00005461 ex_no_reprint = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005462}
5463
5464#ifdef FEAT_BYTEOFF
5465 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005466ex_goto(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005467{
5468 goto_byte(eap->line2);
5469}
5470#endif
5471
5472/*
5473 * ":shell".
5474 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005475 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005476ex_shell(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005477{
5478 do_shell(NULL, 0);
5479}
5480
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005481#if defined(HAVE_DROP_FILE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005482
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005483static int drop_busy = FALSE;
5484static int drop_filec;
5485static char_u **drop_filev = NULL;
5486static int drop_split;
5487static void (*drop_callback)(void *);
5488static void *drop_cookie;
5489
5490 static void
5491handle_drop_internal(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005492{
5493 exarg_T ea;
5494 int save_msg_scroll = msg_scroll;
5495
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005496 // Setting the argument list may cause screen updates and being called
5497 // recursively. Avoid that by setting drop_busy.
5498 drop_busy = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005499
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005500 // Check whether the current buffer is changed. If so, we will need
5501 // to split the current window or data could be lost.
5502 // We don't need to check if the 'hidden' option is set, as in this
5503 // case the buffer won't be lost.
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005504 if (!buf_hide(curbuf) && !drop_split)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005505 {
5506 ++emsg_off;
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005507 drop_split = check_changed(curbuf, CCGD_AW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005508 --emsg_off;
5509 }
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005510 if (drop_split)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005511 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005512 if (win_split(0, 0) == FAIL)
5513 return;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02005514 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005515
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005516 // When splitting the window, create a new alist. Otherwise the
5517 // existing one is overwritten.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005518 alist_unlink(curwin->w_alist);
5519 alist_new();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005520 }
5521
5522 /*
5523 * Set up the new argument list.
5524 */
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005525 alist_set(ALIST(curwin), drop_filec, drop_filev, FALSE, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005526
5527 /*
5528 * Move to the first file.
5529 */
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005530 // Fake up a minimal "next" command for do_argfile()
Bram Moolenaar071d4272004-06-13 20:20:40 +00005531 vim_memset(&ea, 0, sizeof(ea));
5532 ea.cmd = (char_u *)"next";
5533 do_argfile(&ea, 0);
5534
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005535 // do_ecmd() may set need_start_insertmode, but since we never left Insert
5536 // mode that is not needed here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005537 need_start_insertmode = FALSE;
5538
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005539 // Restore msg_scroll, otherwise a following command may cause scrolling
5540 // unexpectedly. The screen will be redrawn by the caller, thus
5541 // msg_scroll being set by displaying a message is irrelevant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005542 msg_scroll = save_msg_scroll;
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005543
5544 if (drop_callback != NULL)
5545 drop_callback(drop_cookie);
5546
5547 drop_filev = NULL;
5548 drop_busy = FALSE;
5549}
5550
5551/*
5552 * Handle a file drop. The code is here because a drop is *nearly* like an
5553 * :args command, but not quite (we have a list of exact filenames, so we
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01005554 * don't want to (a) parse a command line, or (b) expand wildcards). So the
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005555 * code is very similar to :args and hence needs access to a lot of the static
5556 * functions in this file.
5557 *
5558 * The "filev" list must have been allocated using alloc(), as should each item
5559 * in the list. This function takes over responsibility for freeing the "filev"
5560 * list.
5561 */
5562 void
5563handle_drop(
5564 int filec, // the number of files dropped
5565 char_u **filev, // the list of files dropped
5566 int split, // force splitting the window
5567 void (*callback)(void *), // to be called after setting the argument
5568 // list
5569 void *cookie) // argument for "callback" (allocated)
5570{
5571 // Cannot handle recursive drops, finish the pending one.
5572 if (drop_busy)
5573 {
5574 FreeWild(filec, filev);
5575 vim_free(cookie);
5576 return;
5577 }
5578
5579 // When calling handle_drop() more than once in a row we only use the last
5580 // one.
5581 if (drop_filev != NULL)
5582 {
5583 FreeWild(drop_filec, drop_filev);
5584 vim_free(drop_cookie);
5585 }
5586
5587 drop_filec = filec;
5588 drop_filev = filev;
5589 drop_split = split;
5590 drop_callback = callback;
5591 drop_cookie = cookie;
5592
5593 // Postpone this when:
5594 // - editing the command line
5595 // - not possible to change the current buffer
5596 // - updating the screen
5597 // As it may change buffers and window structures that are in use and cause
5598 // freed memory to be used.
5599 if (text_locked() || curbuf_locked() || updating_screen)
5600 return;
5601
5602 handle_drop_internal();
5603}
5604
5605/*
5606 * To be called when text is unlocked, curbuf is unlocked or updating_screen is
5607 * reset: Handle a postponed drop.
5608 */
5609 void
5610handle_any_postponed_drop(void)
5611{
5612 if (!drop_busy && drop_filev != NULL
5613 && !text_locked() && !curbuf_locked() && !updating_screen)
5614 handle_drop_internal();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005615}
5616#endif
5617
Bram Moolenaar071d4272004-06-13 20:20:40 +00005618/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005619 * ":preserve".
5620 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005621 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005622ex_preserve(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005623{
Bram Moolenaar4399ef42005-02-12 14:29:27 +00005624 curbuf->b_flags |= BF_PRESERVED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005625 ml_preserve(curbuf, TRUE);
5626}
5627
5628/*
5629 * ":recover".
5630 */
5631 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005632ex_recover(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005633{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005634 // Set recoverymode right away to avoid the ATTENTION prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005635 recoverymode = TRUE;
Bram Moolenaar45d3b142013-11-09 03:31:51 +01005636 if (!check_changed(curbuf, (p_awa ? CCGD_AW : 0)
5637 | CCGD_MULTWIN
5638 | (eap->forceit ? CCGD_FORCEIT : 0)
5639 | CCGD_EXCMD)
5640
Bram Moolenaar071d4272004-06-13 20:20:40 +00005641 && (*eap->arg == NUL
5642 || setfname(curbuf, eap->arg, NULL, TRUE) == OK))
Bram Moolenaar99499b12019-05-23 21:35:48 +02005643 ml_recover(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005644 recoverymode = FALSE;
5645}
5646
5647/*
5648 * Command modifier used in a wrong way.
5649 */
5650 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005651ex_wrongmodifier(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005652{
5653 eap->errmsg = e_invcmd;
5654}
5655
Bram Moolenaar071d4272004-06-13 20:20:40 +00005656/*
5657 * :sview [+command] file split window with new file, read-only
5658 * :split [[+command] file] split window with current or new file
5659 * :vsplit [[+command] file] split window vertically with current or new file
5660 * :new [[+command] file] split window with no or new file
5661 * :vnew [[+command] file] split vertically window with no or new file
5662 * :sfind [+command] file split window with file in 'path'
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005663 *
5664 * :tabedit open new Tab page with empty window
5665 * :tabedit [+command] file open new Tab page and edit "file"
5666 * :tabnew [[+command] file] just like :tabedit
5667 * :tabfind [+command] file open new Tab page and find "file"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005668 */
5669 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005670ex_splitview(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005671{
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005672 win_T *old_curwin = curwin;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005673#if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005674 char_u *fname = NULL;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005675#endif
5676#ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00005677 int browse_flag = cmdmod.browse;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005678#endif
Bram Moolenaar39902a02018-06-21 22:10:08 +02005679 int use_tab = eap->cmdidx == CMD_tabedit
5680 || eap->cmdidx == CMD_tabfind
5681 || eap->cmdidx == CMD_tabnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005682
Bram Moolenaar8cdbd5b2019-06-16 15:50:45 +02005683 if (ERROR_IF_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02005684 return;
5685
Bram Moolenaar4033c552017-09-16 20:54:51 +02005686#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00005687 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005688#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005689
Bram Moolenaar4033c552017-09-16 20:54:51 +02005690#ifdef FEAT_QUICKFIX
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005691 // A ":split" in the quickfix window works like ":new". Don't want two
5692 // quickfix windows. But it's OK when doing ":tab split".
Bram Moolenaar05bb9532008-07-04 09:44:11 +00005693 if (bt_quickfix(curbuf) && cmdmod.tab == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005694 {
5695 if (eap->cmdidx == CMD_split)
5696 eap->cmdidx = CMD_new;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005697 if (eap->cmdidx == CMD_vsplit)
5698 eap->cmdidx = CMD_vnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005699 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02005700#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005701
Bram Moolenaar4033c552017-09-16 20:54:51 +02005702#ifdef FEAT_SEARCHPATH
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005703 if (eap->cmdidx == CMD_sfind || eap->cmdidx == CMD_tabfind)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005704 {
5705 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg),
5706 FNAME_MESS, TRUE, curbuf->b_ffname);
5707 if (fname == NULL)
5708 goto theend;
5709 eap->arg = fname;
5710 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005711# ifdef FEAT_BROWSE
Bram Moolenaar4033c552017-09-16 20:54:51 +02005712 else
5713# endif
5714#endif
5715#ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00005716 if (cmdmod.browse
Bram Moolenaar071d4272004-06-13 20:20:40 +00005717 && eap->cmdidx != CMD_vnew
Bram Moolenaar071d4272004-06-13 20:20:40 +00005718 && eap->cmdidx != CMD_new)
5719 {
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00005720 if (
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005721# ifdef FEAT_GUI
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00005722 !gui.in_use &&
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005723# endif
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00005724 au_has_group((char_u *)"FileExplorer"))
5725 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005726 // No browsing supported but we do have the file explorer:
5727 // Edit the directory.
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00005728 if (*eap->arg == NUL || !mch_isdir(eap->arg))
5729 eap->arg = (char_u *)".";
5730 }
5731 else
5732 {
Bram Moolenaar39902a02018-06-21 22:10:08 +02005733 fname = do_browse(0, (char_u *)(use_tab
5734 ? _("Edit File in new tab page")
5735 : _("Edit File in new window")),
Bram Moolenaar071d4272004-06-13 20:20:40 +00005736 eap->arg, NULL, NULL, NULL, curbuf);
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00005737 if (fname == NULL)
5738 goto theend;
5739 eap->arg = fname;
5740 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005741 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005742 cmdmod.browse = FALSE; // Don't browse again in do_ecmd().
Bram Moolenaar4033c552017-09-16 20:54:51 +02005743#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005744
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005745 /*
5746 * Either open new tab page or split the window.
5747 */
Bram Moolenaar39902a02018-06-21 22:10:08 +02005748 if (use_tab)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005749 {
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005750 if (win_new_tabpage(cmdmod.tab != 0 ? cmdmod.tab
5751 : eap->addr_count == 0 ? 0
5752 : (int)eap->line2 + 1) != FAIL)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005753 {
Bram Moolenaard2b66012008-01-09 19:30:36 +00005754 do_exedit(eap, old_curwin);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005755
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005756 // set the alternate buffer for the window we came from
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005757 if (curwin != old_curwin
5758 && win_valid(old_curwin)
5759 && old_curwin->w_buffer != curbuf
5760 && !cmdmod.keepalt)
5761 old_curwin->w_alt_fnum = curbuf->b_fnum;
5762 }
5763 }
5764 else if (win_split(eap->addr_count > 0 ? (int)eap->line2 : 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005765 *eap->cmd == 'v' ? WSP_VERT : 0) != FAIL)
5766 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005767 // Reset 'scrollbind' when editing another file, but keep it when
5768 // doing ":split" without arguments.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005769 if (*eap->arg != NUL
Bram Moolenaar8a3bb562018-03-04 20:14:14 +01005770# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00005771 || cmdmod.browse
Bram Moolenaar8a3bb562018-03-04 20:14:14 +01005772# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005773 )
Bram Moolenaar3368ea22010-09-21 16:56:35 +02005774 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005775 else
5776 do_check_scrollbind(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005777 do_exedit(eap, old_curwin);
5778 }
5779
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005780# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00005781 cmdmod.browse = browse_flag;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005782# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005783
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005784# if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005785theend:
5786 vim_free(fname);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005787# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005788}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005789
5790/*
Bram Moolenaar80a94a52006-02-23 21:26:58 +00005791 * Open a new tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005792 */
5793 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005794tabpage_new(void)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00005795{
5796 exarg_T ea;
5797
5798 vim_memset(&ea, 0, sizeof(ea));
5799 ea.cmdidx = CMD_tabnew;
5800 ea.cmd = (char_u *)"tabn";
5801 ea.arg = (char_u *)"";
5802 ex_splitview(&ea);
5803}
5804
5805/*
5806 * :tabnext command
5807 */
5808 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005809ex_tabnext(exarg_T *eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005810{
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005811 int tab_number;
5812
Bram Moolenaar8cdbd5b2019-06-16 15:50:45 +02005813 if (ERROR_IF_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02005814 return;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005815 switch (eap->cmdidx)
5816 {
5817 case CMD_tabfirst:
5818 case CMD_tabrewind:
5819 goto_tabpage(1);
5820 break;
5821 case CMD_tablast:
5822 goto_tabpage(9999);
5823 break;
5824 case CMD_tabprevious:
5825 case CMD_tabNext:
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005826 if (eap->arg && *eap->arg != NUL)
5827 {
5828 char_u *p = eap->arg;
5829 char_u *p_save = p;
5830
5831 tab_number = getdigits(&p);
5832 if (p == p_save || *p_save == '-' || *p != NUL
5833 || tab_number == 0)
5834 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005835 // No numbers as argument.
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005836 eap->errmsg = e_invarg;
5837 return;
5838 }
5839 }
5840 else
5841 {
5842 if (eap->addr_count == 0)
5843 tab_number = 1;
5844 else
5845 {
5846 tab_number = eap->line2;
5847 if (tab_number < 1)
5848 {
5849 eap->errmsg = e_invrange;
5850 return;
5851 }
5852 }
5853 }
5854 goto_tabpage(-tab_number);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005855 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005856 default: // CMD_tabnext
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005857 tab_number = get_tabpage_arg(eap);
5858 if (eap->errmsg == NULL)
5859 goto_tabpage(tab_number);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005860 break;
5861 }
Bram Moolenaar80a94a52006-02-23 21:26:58 +00005862}
5863
5864/*
5865 * :tabmove command
5866 */
5867 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005868ex_tabmove(exarg_T *eap)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00005869{
Bram Moolenaar40ce3a42015-04-21 18:08:39 +02005870 int tab_number;
Bram Moolenaar8cb8dca2012-07-06 18:27:39 +02005871
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005872 tab_number = get_tabpage_arg(eap);
5873 if (eap->errmsg == NULL)
5874 tabpage_move(tab_number);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005875}
5876
5877/*
5878 * :tabs command: List tabs and their contents.
5879 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005880 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005881ex_tabs(exarg_T *eap UNUSED)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005882{
5883 tabpage_T *tp;
5884 win_T *wp;
5885 int tabcount = 1;
5886
5887 msg_start();
5888 msg_scroll = TRUE;
5889 for (tp = first_tabpage; tp != NULL && !got_int; tp = tp->tp_next)
5890 {
5891 msg_putchar('\n');
5892 vim_snprintf((char *)IObuff, IOSIZE, _("Tab page %d"), tabcount++);
Bram Moolenaar8820b482017-03-16 17:23:31 +01005893 msg_outtrans_attr(IObuff, HL_ATTR(HLF_T));
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005894 out_flush(); // output one line at a time
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005895 ui_breakcheck();
5896
Bram Moolenaar030f0df2006-02-21 22:02:53 +00005897 if (tp == curtab)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005898 wp = firstwin;
5899 else
5900 wp = tp->tp_firstwin;
5901 for ( ; wp != NULL && !got_int; wp = wp->w_next)
5902 {
Bram Moolenaar80a94a52006-02-23 21:26:58 +00005903 msg_putchar('\n');
5904 msg_putchar(wp == curwin ? '>' : ' ');
5905 msg_putchar(' ');
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005906 msg_putchar(bufIsChanged(wp->w_buffer) ? '+' : ' ');
5907 msg_putchar(' ');
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005908 if (buf_spname(wp->w_buffer) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02005909 vim_strncpy(IObuff, buf_spname(wp->w_buffer), IOSIZE - 1);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005910 else
5911 home_replace(wp->w_buffer, wp->w_buffer->b_fname,
5912 IObuff, IOSIZE, TRUE);
5913 msg_outtrans(IObuff);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005914 out_flush(); // output one line at a time
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005915 ui_breakcheck();
5916 }
5917 }
5918}
5919
Bram Moolenaar071d4272004-06-13 20:20:40 +00005920/*
5921 * ":mode": Set screen mode.
5922 * If no argument given, just get the screen size and redraw.
5923 */
5924 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005925ex_mode(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005926{
5927 if (*eap->arg == NUL)
5928 shell_resized();
5929 else
5930 mch_screenmode(eap->arg);
5931}
5932
Bram Moolenaar071d4272004-06-13 20:20:40 +00005933/*
5934 * ":resize".
5935 * set, increment or decrement current window height
5936 */
5937 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005938ex_resize(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005939{
5940 int n;
5941 win_T *wp = curwin;
5942
5943 if (eap->addr_count > 0)
5944 {
5945 n = eap->line2;
5946 for (wp = firstwin; wp->w_next != NULL && --n > 0; wp = wp->w_next)
5947 ;
5948 }
5949
Bram Moolenaar44a2f922016-03-19 22:11:51 +01005950# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00005951 need_mouse_correct = TRUE;
Bram Moolenaar44a2f922016-03-19 22:11:51 +01005952# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005953 n = atol((char *)eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005954 if (cmdmod.split & WSP_VERT)
5955 {
5956 if (*eap->arg == '-' || *eap->arg == '+')
Bram Moolenaar02631462017-09-22 15:20:32 +02005957 n += curwin->w_width;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005958 else if (n == 0 && eap->arg[0] == NUL) // default is very wide
Bram Moolenaar071d4272004-06-13 20:20:40 +00005959 n = 9999;
5960 win_setwidth_win((int)n, wp);
5961 }
5962 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005963 {
5964 if (*eap->arg == '-' || *eap->arg == '+')
5965 n += curwin->w_height;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005966 else if (n == 0 && eap->arg[0] == NUL) // default is very high
Bram Moolenaar071d4272004-06-13 20:20:40 +00005967 n = 9999;
5968 win_setheight_win((int)n, wp);
5969 }
5970}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005971
5972/*
5973 * ":find [+command] <file>" command.
5974 */
5975 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005976ex_find(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005977{
5978#ifdef FEAT_SEARCHPATH
5979 char_u *fname;
5980 int count;
5981
5982 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg), FNAME_MESS,
5983 TRUE, curbuf->b_ffname);
5984 if (eap->addr_count > 0)
5985 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01005986 // Repeat finding the file "count" times. This matters when it
5987 // appears several times in the path.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005988 count = eap->line2;
5989 while (fname != NULL && --count > 0)
5990 {
5991 vim_free(fname);
5992 fname = find_file_in_path(NULL, 0, FNAME_MESS,
5993 FALSE, curbuf->b_ffname);
5994 }
5995 }
5996
5997 if (fname != NULL)
5998 {
5999 eap->arg = fname;
6000#endif
6001 do_exedit(eap, NULL);
6002#ifdef FEAT_SEARCHPATH
6003 vim_free(fname);
6004 }
6005#endif
6006}
6007
6008/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00006009 * ":open" simulation: for now just work like ":visual".
6010 */
6011 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006012ex_open(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00006013{
6014 regmatch_T regmatch;
6015 char_u *p;
6016
6017 curwin->w_cursor.lnum = eap->line2;
6018 beginline(BL_SOL | BL_FIX);
6019 if (*eap->arg == '/')
6020 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006021 // ":open /pattern/": put cursor in column found with pattern
Bram Moolenaardf177f62005-02-22 08:39:57 +00006022 ++eap->arg;
6023 p = skip_regexp(eap->arg, '/', p_magic, NULL);
6024 *p = NUL;
6025 regmatch.regprog = vim_regcomp(eap->arg, p_magic ? RE_MAGIC : 0);
6026 if (regmatch.regprog != NULL)
6027 {
6028 regmatch.rm_ic = p_ic;
6029 p = ml_get_curline();
6030 if (vim_regexec(&regmatch, p, (colnr_T)0))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006031 curwin->w_cursor.col = (colnr_T)(regmatch.startp[0] - p);
Bram Moolenaardf177f62005-02-22 08:39:57 +00006032 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006033 emsg(_(e_nomatch));
Bram Moolenaar473de612013-06-08 18:19:48 +02006034 vim_regfree(regmatch.regprog);
Bram Moolenaardf177f62005-02-22 08:39:57 +00006035 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006036 // Move to the NUL, ignore any other arguments.
Bram Moolenaardf177f62005-02-22 08:39:57 +00006037 eap->arg += STRLEN(eap->arg);
6038 }
6039 check_cursor();
6040
6041 eap->cmdidx = CMD_visual;
6042 do_exedit(eap, NULL);
6043}
6044
6045/*
6046 * ":edit", ":badd", ":visual".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006047 */
6048 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006049ex_edit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006050{
6051 do_exedit(eap, NULL);
6052}
6053
6054/*
6055 * ":edit <file>" command and alikes.
6056 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006057 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006058do_exedit(
6059 exarg_T *eap,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006060 win_T *old_curwin) // curwin before doing a split or NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006061{
6062 int n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006063 int need_hide;
Bram Moolenaardf177f62005-02-22 08:39:57 +00006064 int exmode_was = exmode_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006065
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02006066 if (eap->cmdidx != CMD_pedit && ERROR_IF_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02006067 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006068 /*
6069 * ":vi" command ends Ex mode.
6070 */
6071 if (exmode_active && (eap->cmdidx == CMD_visual
6072 || eap->cmdidx == CMD_view))
6073 {
6074 exmode_active = FALSE;
6075 if (*eap->arg == NUL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00006076 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006077 // Special case: ":global/pat/visual\NLvi-commands"
Bram Moolenaardf177f62005-02-22 08:39:57 +00006078 if (global_busy)
6079 {
6080 int rd = RedrawingDisabled;
6081 int nwr = no_wait_return;
6082 int ms = msg_scroll;
6083#ifdef FEAT_GUI
6084 int he = hold_gui_events;
6085#endif
6086
6087 if (eap->nextcmd != NULL)
6088 {
6089 stuffReadbuff(eap->nextcmd);
6090 eap->nextcmd = NULL;
6091 }
6092
6093 if (exmode_was != EXMODE_VIM)
6094 settmode(TMODE_RAW);
6095 RedrawingDisabled = 0;
6096 no_wait_return = 0;
6097 need_wait_return = FALSE;
6098 msg_scroll = 0;
6099#ifdef FEAT_GUI
6100 hold_gui_events = 0;
6101#endif
6102 must_redraw = CLEAR;
6103
6104 main_loop(FALSE, TRUE);
6105
6106 RedrawingDisabled = rd;
6107 no_wait_return = nwr;
6108 msg_scroll = ms;
6109#ifdef FEAT_GUI
6110 hold_gui_events = he;
6111#endif
6112 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006113 return;
Bram Moolenaardf177f62005-02-22 08:39:57 +00006114 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006115 }
6116
6117 if ((eap->cmdidx == CMD_new
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006118 || eap->cmdidx == CMD_tabnew
6119 || eap->cmdidx == CMD_tabedit
Bram Moolenaar4033c552017-09-16 20:54:51 +02006120 || eap->cmdidx == CMD_vnew) && *eap->arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006121 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006122 // ":new" or ":tabnew" without argument: edit an new empty buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00006123 setpcmark();
6124 (void)do_ecmd(0, NULL, NULL, eap, ECMD_ONE,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00006125 ECMD_HIDE + (eap->forceit ? ECMD_FORCEIT : 0),
6126 old_curwin == NULL ? curwin : NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006127 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02006128 else if ((eap->cmdidx != CMD_split && eap->cmdidx != CMD_vsplit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006129 || *eap->arg != NUL
6130#ifdef FEAT_BROWSE
6131 || cmdmod.browse
6132#endif
6133 )
6134 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006135 // Can't edit another file when "curbuf_lock" is set. Only ":edit"
6136 // can bring us here, others are stopped earlier.
Bram Moolenaar5555acc2006-04-07 21:33:12 +00006137 if (*eap->arg != NUL && curbuf_locked())
6138 return;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006139
Bram Moolenaar071d4272004-06-13 20:20:40 +00006140 n = readonlymode;
6141 if (eap->cmdidx == CMD_view || eap->cmdidx == CMD_sview)
6142 readonlymode = TRUE;
6143 else if (eap->cmdidx == CMD_enew)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006144 readonlymode = FALSE; // 'readonly' doesn't make sense in an
6145 // empty buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00006146 setpcmark();
6147 if (do_ecmd(0, (eap->cmdidx == CMD_enew ? NULL : eap->arg),
6148 NULL, eap,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006149 // ":edit" goes to first line if Vi compatible
Bram Moolenaar071d4272004-06-13 20:20:40 +00006150 (*eap->arg == NUL && eap->do_ecmd_lnum == 0
6151 && vim_strchr(p_cpo, CPO_GOTO1) != NULL)
6152 ? ECMD_ONE : eap->do_ecmd_lnum,
Bram Moolenaareb44a682017-08-03 22:44:55 +02006153 (buf_hide(curbuf) ? ECMD_HIDE : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006154 + (eap->forceit ? ECMD_FORCEIT : 0)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006155 // after a split we can use an existing buffer
Bram Moolenaar7b449342014-03-25 13:03:48 +01006156 + (old_curwin != NULL ? ECMD_OLDBUF : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006157 + (eap->cmdidx == CMD_badd ? ECMD_ADDBUF : 0 )
Bram Moolenaar701f7af2008-11-15 13:12:07 +00006158 , old_curwin == NULL ? curwin : NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006159 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006160 // Editing the file failed. If the window was split, close it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006161 if (old_curwin != NULL)
6162 {
6163 need_hide = (curbufIsChanged() && curbuf->b_nwindows <= 1);
Bram Moolenaareb44a682017-08-03 22:44:55 +02006164 if (!need_hide || buf_hide(curbuf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006165 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006166#if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00006167 cleanup_T cs;
6168
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006169 // Reset the error/interrupt/exception state here so that
6170 // aborting() returns FALSE when closing a window.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00006171 enter_cleanup(&cs);
Bram Moolenaar4033c552017-09-16 20:54:51 +02006172#endif
6173#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00006174 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006175#endif
Bram Moolenaareb44a682017-08-03 22:44:55 +02006176 win_close(curwin, !need_hide && !buf_hide(curbuf));
Bram Moolenaarc0197e22004-09-13 20:26:32 +00006177
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006178#if defined(FEAT_EVAL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006179 // Restore the error/interrupt/exception state if not
6180 // discarded by a new aborting error, interrupt, or
6181 // uncaught exception.
Bram Moolenaarc0197e22004-09-13 20:26:32 +00006182 leave_cleanup(&cs);
Bram Moolenaar4033c552017-09-16 20:54:51 +02006183#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006184 }
6185 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006186 }
6187 else if (readonlymode && curbuf->b_nwindows == 1)
6188 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006189 // When editing an already visited buffer, 'readonly' won't be set
6190 // but the previous value is kept. With ":view" and ":sview" we
6191 // want the file to be readonly, except when another window is
6192 // editing the same buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006193 curbuf->b_p_ro = TRUE;
6194 }
6195 readonlymode = n;
6196 }
6197 else
6198 {
6199 if (eap->do_ecmd_cmd != NULL)
6200 do_cmdline_cmd(eap->do_ecmd_cmd);
6201#ifdef FEAT_TITLE
6202 n = curwin->w_arg_idx_invalid;
6203#endif
6204 check_arg_idx(curwin);
6205#ifdef FEAT_TITLE
6206 if (n != curwin->w_arg_idx_invalid)
6207 maketitle();
6208#endif
6209 }
6210
Bram Moolenaar071d4272004-06-13 20:20:40 +00006211 /*
6212 * if ":split file" worked, set alternate file name in old window to new
6213 * file
6214 */
6215 if (old_curwin != NULL
6216 && *eap->arg != NUL
6217 && curwin != old_curwin
6218 && win_valid(old_curwin)
Bram Moolenaard4755bb2004-09-02 19:12:26 +00006219 && old_curwin->w_buffer != curbuf
6220 && !cmdmod.keepalt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006221 old_curwin->w_alt_fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006222
6223 ex_no_reprint = TRUE;
6224}
6225
6226#ifndef FEAT_GUI
6227/*
6228 * ":gui" and ":gvim" when there is no GUI.
6229 */
6230 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006231ex_nogui(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006232{
6233 eap->errmsg = e_nogvim;
6234}
6235#endif
6236
Bram Moolenaar4f974752019-02-17 17:44:42 +01006237#if defined(FEAT_GUI_MSWIN) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006238 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006239ex_tearoff(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006240{
6241 gui_make_tearoff(eap->arg);
6242}
6243#endif
6244
Bram Moolenaar29a2c082018-03-05 21:06:23 +01006245#if (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK) \
6246 || defined(FEAT_TERM_POPUP_MENU)) && defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006247 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006248ex_popup(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006249{
Bram Moolenaar29a2c082018-03-05 21:06:23 +01006250# if defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK)
6251 if (gui.in_use)
6252 gui_make_popup(eap->arg, eap->forceit);
6253# ifdef FEAT_TERM_POPUP_MENU
6254 else
6255# endif
6256# endif
6257# ifdef FEAT_TERM_POPUP_MENU
6258 pum_make_popup(eap->arg, eap->forceit);
6259# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006260}
6261#endif
6262
Bram Moolenaar071d4272004-06-13 20:20:40 +00006263 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006264ex_swapname(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006265{
6266 if (curbuf->b_ml.ml_mfp == NULL || curbuf->b_ml.ml_mfp->mf_fname == NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01006267 msg(_("No swap file"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006268 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01006269 msg((char *)curbuf->b_ml.ml_mfp->mf_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006270}
6271
6272/*
6273 * ":syncbind" forces all 'scrollbind' windows to have the same relative
6274 * offset.
6275 * (1998-11-02 16:21:01 R. Edward Ralston <eralston@computer.org>)
6276 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006277 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006278ex_syncbind(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006279{
Bram Moolenaar071d4272004-06-13 20:20:40 +00006280 win_T *wp;
Bram Moolenaardedd1b02013-12-14 13:06:17 +01006281 win_T *save_curwin = curwin;
6282 buf_T *save_curbuf = curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006283 long topline;
6284 long y;
6285 linenr_T old_linenr = curwin->w_cursor.lnum;
6286
6287 setpcmark();
6288
6289 /*
6290 * determine max topline
6291 */
6292 if (curwin->w_p_scb)
6293 {
6294 topline = curwin->w_topline;
Bram Moolenaar29323592016-07-24 22:04:11 +02006295 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006296 {
6297 if (wp->w_p_scb && wp->w_buffer)
6298 {
Bram Moolenaar375e3392019-01-31 18:26:10 +01006299 y = wp->w_buffer->b_ml.ml_line_count - get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006300 if (topline > y)
6301 topline = y;
6302 }
6303 }
6304 if (topline < 1)
6305 topline = 1;
6306 }
6307 else
6308 {
6309 topline = 1;
6310 }
6311
6312
6313 /*
Bram Moolenaardedd1b02013-12-14 13:06:17 +01006314 * Set all scrollbind windows to the same topline.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006315 */
Bram Moolenaar29323592016-07-24 22:04:11 +02006316 FOR_ALL_WINDOWS(curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006317 {
6318 if (curwin->w_p_scb)
6319 {
Bram Moolenaardedd1b02013-12-14 13:06:17 +01006320 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006321 y = topline - curwin->w_topline;
6322 if (y > 0)
6323 scrollup(y, TRUE);
6324 else
6325 scrolldown(-y, TRUE);
6326 curwin->w_scbind_pos = topline;
6327 redraw_later(VALID);
6328 cursor_correct();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006329 curwin->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006330 }
6331 }
Bram Moolenaardedd1b02013-12-14 13:06:17 +01006332 curwin = save_curwin;
6333 curbuf = save_curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006334 if (curwin->w_p_scb)
6335 {
6336 did_syncbind = TRUE;
6337 checkpcmark();
6338 if (old_linenr != curwin->w_cursor.lnum)
6339 {
6340 char_u ctrl_o[2];
6341
6342 ctrl_o[0] = Ctrl_O;
6343 ctrl_o[1] = 0;
6344 ins_typebuf(ctrl_o, REMAP_NONE, 0, TRUE, FALSE);
6345 }
6346 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006347}
6348
6349
6350 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006351ex_read(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006352{
Bram Moolenaardf177f62005-02-22 08:39:57 +00006353 int i;
6354 int empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
6355 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006356
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006357 if (eap->usefilter) // :r!cmd
Bram Moolenaar071d4272004-06-13 20:20:40 +00006358 do_bang(1, eap, FALSE, FALSE, TRUE);
6359 else
6360 {
6361 if (u_save(eap->line2, (linenr_T)(eap->line2 + 1)) == FAIL)
6362 return;
6363
6364#ifdef FEAT_BROWSE
6365 if (cmdmod.browse)
6366 {
6367 char_u *browseFile;
6368
Bram Moolenaar7171abe2004-10-11 10:06:20 +00006369 browseFile = do_browse(0, (char_u *)_("Append File"), eap->arg,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006370 NULL, NULL, NULL, curbuf);
6371 if (browseFile != NULL)
6372 {
6373 i = readfile(browseFile, NULL,
6374 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
6375 vim_free(browseFile);
6376 }
6377 else
6378 i = OK;
6379 }
6380 else
6381#endif
6382 if (*eap->arg == NUL)
6383 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006384 if (check_fname() == FAIL) // check for no file name
Bram Moolenaar071d4272004-06-13 20:20:40 +00006385 return;
6386 i = readfile(curbuf->b_ffname, curbuf->b_fname,
6387 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
6388 }
6389 else
6390 {
6391 if (vim_strchr(p_cpo, CPO_ALTREAD) != NULL)
6392 (void)setaltfname(eap->arg, eap->arg, (linenr_T)1);
6393 i = readfile(eap->arg, NULL,
6394 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
6395
6396 }
Bram Moolenaare13b9af2017-01-13 22:01:02 +01006397 if (i != OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006398 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006399#if defined(FEAT_EVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006400 if (!aborting())
6401#endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006402 semsg(_(e_notopen), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006403 }
6404 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00006405 {
6406 if (empty && exmode_active)
6407 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006408 // Delete the empty line that remains. Historically ex does
6409 // this but vi doesn't.
Bram Moolenaardf177f62005-02-22 08:39:57 +00006410 if (eap->line2 == 0)
6411 lnum = curbuf->b_ml.ml_line_count;
6412 else
6413 lnum = 1;
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00006414 if (*ml_get(lnum) == NUL && u_savedel(lnum, 1L) == OK)
Bram Moolenaardf177f62005-02-22 08:39:57 +00006415 {
6416 ml_delete(lnum, FALSE);
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00006417 if (curwin->w_cursor.lnum > 1
6418 && curwin->w_cursor.lnum >= lnum)
Bram Moolenaardf177f62005-02-22 08:39:57 +00006419 --curwin->w_cursor.lnum;
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00006420 deleted_lines_mark(lnum, 1L);
Bram Moolenaardf177f62005-02-22 08:39:57 +00006421 }
6422 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006423 redraw_curbuf_later(VALID);
Bram Moolenaardf177f62005-02-22 08:39:57 +00006424 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006425 }
6426}
6427
Bram Moolenaarea408852005-06-25 22:49:46 +00006428static char_u *prev_dir = NULL;
6429
6430#if defined(EXITFREE) || defined(PROTO)
6431 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006432free_cd_dir(void)
Bram Moolenaarea408852005-06-25 22:49:46 +00006433{
Bram Moolenaard23a8232018-02-10 18:45:26 +01006434 VIM_CLEAR(prev_dir);
6435 VIM_CLEAR(globaldir);
Bram Moolenaarea408852005-06-25 22:49:46 +00006436}
6437#endif
6438
Bram Moolenaarf4258302013-06-02 18:20:17 +02006439/*
6440 * Deal with the side effects of changing the current directory.
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006441 * When 'scope' is CDSCOPE_TABPAGE then this was after an ":tcd" command.
6442 * When 'scope' is CDSCOPE_WINDOW then this was after an ":lcd" command.
Bram Moolenaarf4258302013-06-02 18:20:17 +02006443 */
6444 void
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006445post_chdir(cdscope_T scope)
Bram Moolenaarf4258302013-06-02 18:20:17 +02006446{
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006447 if (scope != CDSCOPE_WINDOW)
Bram Moolenaar00aa0692019-04-27 20:37:57 +02006448 // Clear tab local directory for both :cd and :tcd
6449 VIM_CLEAR(curtab->tp_localdir);
Bram Moolenaard23a8232018-02-10 18:45:26 +01006450 VIM_CLEAR(curwin->w_localdir);
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006451 if (scope != CDSCOPE_GLOBAL)
Bram Moolenaarf4258302013-06-02 18:20:17 +02006452 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006453 // If still in global directory, need to remember current
6454 // directory as global directory.
Bram Moolenaarf4258302013-06-02 18:20:17 +02006455 if (globaldir == NULL && prev_dir != NULL)
6456 globaldir = vim_strsave(prev_dir);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006457 // Remember this local directory for the window.
Bram Moolenaarf4258302013-06-02 18:20:17 +02006458 if (mch_dirname(NameBuff, MAXPATHL) == OK)
Bram Moolenaar00aa0692019-04-27 20:37:57 +02006459 {
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006460 if (scope == CDSCOPE_TABPAGE)
Bram Moolenaar00aa0692019-04-27 20:37:57 +02006461 curtab->tp_localdir = vim_strsave(NameBuff);
6462 else
6463 curwin->w_localdir = vim_strsave(NameBuff);
6464 }
Bram Moolenaarf4258302013-06-02 18:20:17 +02006465 }
6466 else
6467 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006468 // We are now in the global directory, no need to remember its
6469 // name.
Bram Moolenaard23a8232018-02-10 18:45:26 +01006470 VIM_CLEAR(globaldir);
Bram Moolenaarf4258302013-06-02 18:20:17 +02006471 }
6472
6473 shorten_fnames(TRUE);
6474}
6475
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006476/*
6477 * Change directory function used by :cd/:tcd/:lcd Ex commands and the
6478 * chdir() function. If 'winlocaldir' is TRUE, then changes the window-local
6479 * directory. If 'tablocaldir' is TRUE, then changes the tab-local directory.
6480 * Otherwise changes the global directory.
6481 * Returns TRUE if the directory is successfully changed.
6482 */
6483 int
6484changedir_func(
6485 char_u *new_dir,
6486 int forceit,
6487 cdscope_T scope)
6488{
6489 char_u *tofree;
6490 int dir_differs;
6491 int retval = FALSE;
6492
6493 if (allbuf_locked())
6494 return FALSE;
6495
6496 if (vim_strchr(p_cpo, CPO_CHDIR) != NULL && curbufIsChanged() && !forceit)
6497 {
6498 emsg(_("E747: Cannot change directory, buffer is modified (add ! to override)"));
6499 return FALSE;
6500 }
6501
6502 // ":cd -": Change to previous directory
6503 if (STRCMP(new_dir, "-") == 0)
6504 {
6505 if (prev_dir == NULL)
6506 {
6507 emsg(_("E186: No previous directory"));
6508 return FALSE;
6509 }
6510 new_dir = prev_dir;
6511 }
6512
6513 // Save current directory for next ":cd -"
6514 tofree = prev_dir;
6515 if (mch_dirname(NameBuff, MAXPATHL) == OK)
6516 prev_dir = vim_strsave(NameBuff);
6517 else
6518 prev_dir = NULL;
6519
6520#if defined(UNIX) || defined(VMS)
6521 // for UNIX ":cd" means: go to home directory
6522 if (*new_dir == NUL)
6523 {
6524 // use NameBuff for home directory name
6525# ifdef VMS
6526 char_u *p;
6527
6528 p = mch_getenv((char_u *)"SYS$LOGIN");
6529 if (p == NULL || *p == NUL) // empty is the same as not set
6530 NameBuff[0] = NUL;
6531 else
6532 vim_strncpy(NameBuff, p, MAXPATHL - 1);
6533# else
6534 expand_env((char_u *)"$HOME", NameBuff, MAXPATHL);
6535# endif
6536 new_dir = NameBuff;
6537 }
6538#endif
6539 dir_differs = new_dir == NULL || prev_dir == NULL
6540 || pathcmp((char *)prev_dir, (char *)new_dir, -1) != 0;
6541 if (new_dir == NULL || (dir_differs && vim_chdir(new_dir)))
6542 emsg(_(e_failed));
6543 else
6544 {
6545 char_u *acmd_fname;
6546
6547 post_chdir(scope);
6548
6549 if (dir_differs)
6550 {
6551 if (scope == CDSCOPE_WINDOW)
6552 acmd_fname = (char_u *)"window";
6553 else if (scope == CDSCOPE_TABPAGE)
6554 acmd_fname = (char_u *)"tabpage";
6555 else
6556 acmd_fname = (char_u *)"global";
6557 apply_autocmds(EVENT_DIRCHANGED, acmd_fname, new_dir, FALSE,
6558 curbuf);
6559 }
6560 retval = TRUE;
6561 }
6562 vim_free(tofree);
6563
6564 return retval;
6565}
Bram Moolenaarea408852005-06-25 22:49:46 +00006566
Bram Moolenaar071d4272004-06-13 20:20:40 +00006567/*
Bram Moolenaar00aa0692019-04-27 20:37:57 +02006568 * ":cd", ":tcd", ":lcd", ":chdir" ":tchdir" and ":lchdir".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006569 */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00006570 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006571ex_cd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006572{
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01006573 char_u *new_dir;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006574
6575 new_dir = eap->arg;
6576#if !defined(UNIX) && !defined(VMS)
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006577 // for non-UNIX ":cd" means: print current directory
Bram Moolenaar071d4272004-06-13 20:20:40 +00006578 if (*new_dir == NUL)
6579 ex_pwd(NULL);
6580 else
6581#endif
6582 {
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006583 cdscope_T scope = CDSCOPE_GLOBAL;
6584
6585 if (eap->cmdidx == CMD_lcd || eap->cmdidx == CMD_lchdir)
6586 scope = CDSCOPE_WINDOW;
6587 else if (eap->cmdidx == CMD_tcd || eap->cmdidx == CMD_tchdir)
6588 scope = CDSCOPE_TABPAGE;
6589
6590 if (changedir_func(new_dir, eap->forceit, scope))
Bram Moolenaardf177f62005-02-22 08:39:57 +00006591 {
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006592 // Echo the new current directory if the command was typed.
Bram Moolenaarfcfbc672009-07-09 18:13:49 +00006593 if (KeyTyped || p_verbose >= 5)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006594 ex_pwd(eap);
6595 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006596 }
6597}
6598
6599/*
6600 * ":pwd".
6601 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006602 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006603ex_pwd(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006604{
6605 if (mch_dirname(NameBuff, MAXPATHL) == OK)
6606 {
6607#ifdef BACKSLASH_IN_FILENAME
6608 slash_adjust(NameBuff);
6609#endif
Bram Moolenaar32526b32019-01-19 17:43:09 +01006610 msg((char *)NameBuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006611 }
6612 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006613 emsg(_("E187: Unknown"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006614}
6615
6616/*
6617 * ":=".
6618 */
6619 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006620ex_equal(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006621{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006622 smsg("%ld", (long)eap->line2);
Bram Moolenaardf177f62005-02-22 08:39:57 +00006623 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006624}
6625
6626 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006627ex_sleep(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006628{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006629 int n;
6630 long len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006631
6632 if (cursor_valid())
6633 {
6634 n = W_WINROW(curwin) + curwin->w_wrow - msg_scrolled;
6635 if (n >= 0)
Bram Moolenaar53f81742017-09-22 14:35:51 +02006636 windgoto((int)n, curwin->w_wincol + curwin->w_wcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006637 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006638
6639 len = eap->line2;
6640 switch (*eap->arg)
6641 {
6642 case 'm': break;
6643 case NUL: len *= 1000L; break;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006644 default: semsg(_(e_invarg2), eap->arg); return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006645 }
6646 do_sleep(len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006647}
6648
6649/*
6650 * Sleep for "msec" milliseconds, but keep checking for a CTRL-C every second.
6651 */
6652 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006653do_sleep(long msec)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006654{
Bram Moolenaar52953192019-08-16 10:27:13 +02006655 long done = 0;
Bram Moolenaar975b5272016-03-15 23:10:59 +01006656 long wait_now;
Bram Moolenaar52953192019-08-16 10:27:13 +02006657# ifdef ELAPSED_FUNC
6658 elapsed_T start_tv;
6659
6660 // Remember at what time we started, so that we know how much longer we
6661 // should wait after waiting for a bit.
6662 ELAPSED_INIT(start_tv);
6663# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006664
6665 cursor_on();
Bram Moolenaarf45d7472018-09-30 18:22:26 +02006666 out_flush_cursor(FALSE, FALSE);
Bram Moolenaar52953192019-08-16 10:27:13 +02006667 while (!got_int && done < msec)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006668 {
Bram Moolenaar975b5272016-03-15 23:10:59 +01006669 wait_now = msec - done > 1000L ? 1000L : msec - done;
6670#ifdef FEAT_TIMERS
6671 {
6672 long due_time = check_due_timer();
6673
6674 if (due_time > 0 && due_time < wait_now)
6675 wait_now = due_time;
6676 }
6677#endif
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02006678#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar28e67e02019-08-15 23:05:49 +02006679 if (has_any_channel() && wait_now > 20L)
6680 wait_now = 20L;
6681#endif
6682#ifdef FEAT_SOUND
6683 if (has_any_sound_callback() && wait_now > 20L)
6684 wait_now = 20L;
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02006685#endif
Bram Moolenaar975b5272016-03-15 23:10:59 +01006686 ui_delay(wait_now, TRUE);
Bram Moolenaar52953192019-08-16 10:27:13 +02006687
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02006688#ifdef FEAT_JOB_CHANNEL
6689 if (has_any_channel())
6690 ui_breakcheck_force(TRUE);
6691 else
6692#endif
6693 ui_breakcheck();
Bram Moolenaar93c88e02015-09-15 14:12:05 +02006694#ifdef MESSAGE_QUEUE
Bram Moolenaar52953192019-08-16 10:27:13 +02006695 // Process the netbeans and clientserver messages that may have been
6696 // received in the call to ui_breakcheck() when the GUI is in use. This
6697 // may occur when running a test case.
Bram Moolenaar93c88e02015-09-15 14:12:05 +02006698 parse_queued_messages();
Bram Moolenaare3cc6d42011-10-20 21:58:34 +02006699#endif
Bram Moolenaar52953192019-08-16 10:27:13 +02006700
6701# ifdef ELAPSED_FUNC
6702 // actual time passed
6703 done = ELAPSED_FUNC(start_tv);
6704# else
6705 // guestimate time passed (will actually be more)
6706 done += wait_now;
6707# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006708 }
Bram Moolenaar1ebff3d2018-07-07 16:18:13 +02006709
6710 // If CTRL-C was typed to interrupt the sleep, drop the CTRL-C from the
6711 // input buffer, otherwise a following call to input() fails.
6712 if (got_int)
6713 (void)vpeekc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006714}
6715
Bram Moolenaar071d4272004-06-13 20:20:40 +00006716/*
6717 * ":winsize" command (obsolete).
6718 */
6719 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006720ex_winsize(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006721{
6722 int w, h;
6723 char_u *arg = eap->arg;
6724 char_u *p;
6725
6726 w = getdigits(&arg);
6727 arg = skipwhite(arg);
6728 p = arg;
6729 h = getdigits(&arg);
6730 if (*p != NUL && *arg == NUL)
6731 set_shellsize(w, h, TRUE);
6732 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006733 emsg(_("E465: :winsize requires two number arguments"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006734}
6735
Bram Moolenaar071d4272004-06-13 20:20:40 +00006736 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006737ex_wincmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006738{
6739 int xchar = NUL;
6740 char_u *p;
6741
6742 if (*eap->arg == 'g' || *eap->arg == Ctrl_G)
6743 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006744 // CTRL-W g and CTRL-W CTRL-G have an extra command character
Bram Moolenaar071d4272004-06-13 20:20:40 +00006745 if (eap->arg[1] == NUL)
6746 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006747 emsg(_(e_invarg));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006748 return;
6749 }
6750 xchar = eap->arg[1];
6751 p = eap->arg + 2;
6752 }
6753 else
6754 p = eap->arg + 1;
6755
6756 eap->nextcmd = check_nextcmd(p);
6757 p = skipwhite(p);
6758 if (*p != NUL && *p != '"' && eap->nextcmd == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006759 emsg(_(e_invarg));
Bram Moolenaar12bde492011-06-13 01:19:56 +02006760 else if (!eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006761 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006762 // Pass flags on for ":vertical wincmd ]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006763 postponed_split_flags = cmdmod.split;
Bram Moolenaard326ce82007-03-11 14:48:29 +00006764 postponed_split_tab = cmdmod.tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006765 do_window(*eap->arg, eap->addr_count > 0 ? eap->line2 : 0L, xchar);
6766 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00006767 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006768 }
6769}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006770
Bram Moolenaar843ee412004-06-30 16:16:41 +00006771#if defined(FEAT_GUI) || defined(UNIX) || defined(VMS) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006772/*
6773 * ":winpos".
6774 */
6775 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006776ex_winpos(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006777{
6778 int x, y;
6779 char_u *arg = eap->arg;
6780 char_u *p;
6781
6782 if (*arg == NUL)
6783 {
Bram Moolenaar843ee412004-06-30 16:16:41 +00006784# if defined(FEAT_GUI) || defined(MSWIN)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02006785# ifdef VIMDLL
6786 if (gui.in_use ? gui_mch_get_winpos(&x, &y) != FAIL :
6787 mch_get_winpos(&x, &y) != FAIL)
6788# elif defined(FEAT_GUI)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006789 if (gui.in_use && gui_mch_get_winpos(&x, &y) != FAIL)
Bram Moolenaar843ee412004-06-30 16:16:41 +00006790# else
6791 if (mch_get_winpos(&x, &y) != FAIL)
6792# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006793 {
6794 sprintf((char *)IObuff, _("Window position: X %d, Y %d"), x, y);
Bram Moolenaar32526b32019-01-19 17:43:09 +01006795 msg((char *)IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006796 }
6797 else
6798# endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006799 emsg(_("E188: Obtaining window position not implemented for this platform"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006800 }
6801 else
6802 {
6803 x = getdigits(&arg);
6804 arg = skipwhite(arg);
6805 p = arg;
6806 y = getdigits(&arg);
6807 if (*p == NUL || *arg != NUL)
6808 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006809 emsg(_("E466: :winpos requires two number arguments"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006810 return;
6811 }
6812# ifdef FEAT_GUI
6813 if (gui.in_use)
6814 gui_mch_set_winpos(x, y);
6815 else if (gui.starting)
6816 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006817 // Remember the coordinates for when the window is opened.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006818 gui_win_x = x;
6819 gui_win_y = y;
6820 }
Bram Moolenaarafde13b2019-04-28 19:46:49 +02006821# if defined(HAVE_TGETENT) || defined(VIMDLL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006822 else
6823# endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02006824# endif
6825# if defined(MSWIN) && (!defined(FEAT_GUI) || defined(VIMDLL))
Bram Moolenaar843ee412004-06-30 16:16:41 +00006826 mch_set_winpos(x, y);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006827# endif
6828# ifdef HAVE_TGETENT
6829 if (*T_CWP)
6830 term_set_winpos(x, y);
6831# endif
6832 }
6833}
6834#endif
6835
6836/*
6837 * Handle command that work like operators: ":delete", ":yank", ":>" and ":<".
6838 */
6839 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006840ex_operators(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006841{
6842 oparg_T oa;
6843
6844 clear_oparg(&oa);
6845 oa.regname = eap->regname;
6846 oa.start.lnum = eap->line1;
6847 oa.end.lnum = eap->line2;
6848 oa.line_count = eap->line2 - eap->line1 + 1;
6849 oa.motion_type = MLINE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006850 virtual_op = FALSE;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006851 if (eap->cmdidx != CMD_yank) // position cursor for undo
Bram Moolenaar071d4272004-06-13 20:20:40 +00006852 {
6853 setpcmark();
6854 curwin->w_cursor.lnum = eap->line1;
6855 beginline(BL_SOL | BL_FIX);
6856 }
6857
Bram Moolenaard07c6e12013-11-21 14:21:40 +01006858 if (VIsual_active)
6859 end_visual_mode();
Bram Moolenaard07c6e12013-11-21 14:21:40 +01006860
Bram Moolenaar071d4272004-06-13 20:20:40 +00006861 switch (eap->cmdidx)
6862 {
6863 case CMD_delete:
6864 oa.op_type = OP_DELETE;
6865 op_delete(&oa);
6866 break;
6867
6868 case CMD_yank:
6869 oa.op_type = OP_YANK;
6870 (void)op_yank(&oa, FALSE, TRUE);
6871 break;
6872
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006873 default: // CMD_rshift or CMD_lshift
Bram Moolenaar6e707362013-06-14 19:15:58 +02006874 if (
Bram Moolenaar071d4272004-06-13 20:20:40 +00006875#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6e707362013-06-14 19:15:58 +02006876 (eap->cmdidx == CMD_rshift) ^ curwin->w_p_rl
6877#else
6878 eap->cmdidx == CMD_rshift
Bram Moolenaar071d4272004-06-13 20:20:40 +00006879#endif
Bram Moolenaar6e707362013-06-14 19:15:58 +02006880 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006881 oa.op_type = OP_RSHIFT;
6882 else
6883 oa.op_type = OP_LSHIFT;
6884 op_shift(&oa, FALSE, eap->amount);
6885 break;
6886 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006887 virtual_op = MAYBE;
Bram Moolenaardf177f62005-02-22 08:39:57 +00006888 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006889}
6890
6891/*
6892 * ":put".
6893 */
6894 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006895ex_put(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006896{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006897 // ":0put" works like ":1put!".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006898 if (eap->line2 == 0)
6899 {
6900 eap->line2 = 1;
6901 eap->forceit = TRUE;
6902 }
6903 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaardf177f62005-02-22 08:39:57 +00006904 do_put(eap->regname, eap->forceit ? BACKWARD : FORWARD, 1L,
6905 PUT_LINE|PUT_CURSLINE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006906}
6907
6908/*
6909 * Handle ":copy" and ":move".
6910 */
6911 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006912ex_copymove(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006913{
6914 long n;
6915
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02006916 n = get_address(eap, &eap->arg, eap->addr_type, FALSE, FALSE, FALSE, 1);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006917 if (eap->arg == NULL) // error detected
Bram Moolenaar071d4272004-06-13 20:20:40 +00006918 {
6919 eap->nextcmd = NULL;
6920 return;
6921 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00006922 get_flags(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006923
6924 /*
6925 * move or copy lines from 'eap->line1'-'eap->line2' to below line 'n'
6926 */
6927 if (n == MAXLNUM || n < 0 || n > curbuf->b_ml.ml_line_count)
6928 {
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02006929 emsg(_(e_invrange));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006930 return;
6931 }
6932
6933 if (eap->cmdidx == CMD_move)
6934 {
6935 if (do_move(eap->line1, eap->line2, n) == FAIL)
6936 return;
6937 }
6938 else
6939 ex_copy(eap->line1, eap->line2, n);
6940 u_clearline();
6941 beginline(BL_SOL | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00006942 ex_may_print(eap);
6943}
6944
6945/*
6946 * Print the current line if flags were given to the Ex command.
6947 */
Bram Moolenaarfd3fe982014-04-01 17:49:44 +02006948 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006949ex_may_print(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00006950{
6951 if (eap->flags != 0)
6952 {
6953 print_line(curwin->w_cursor.lnum, (eap->flags & EXFLAG_NR),
6954 (eap->flags & EXFLAG_LIST));
6955 ex_no_reprint = TRUE;
6956 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006957}
6958
6959/*
6960 * ":smagic" and ":snomagic".
6961 */
6962 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006963ex_submagic(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006964{
6965 int magic_save = p_magic;
6966
6967 p_magic = (eap->cmdidx == CMD_smagic);
6968 do_sub(eap);
6969 p_magic = magic_save;
6970}
6971
6972/*
6973 * ":join".
6974 */
6975 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006976ex_join(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006977{
6978 curwin->w_cursor.lnum = eap->line1;
6979 if (eap->line1 == eap->line2)
6980 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01006981 if (eap->addr_count >= 2) // :2,2join does nothing
Bram Moolenaar071d4272004-06-13 20:20:40 +00006982 return;
6983 if (eap->line2 == curbuf->b_ml.ml_line_count)
6984 {
6985 beep_flush();
6986 return;
6987 }
6988 ++eap->line2;
6989 }
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02006990 (void)do_join(eap->line2 - eap->line1 + 1, !eap->forceit, TRUE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006991 beginline(BL_WHITE | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00006992 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006993}
6994
6995/*
6996 * ":[addr]@r" or ":[addr]*r": execute register
6997 */
6998 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006999ex_at(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007000{
7001 int c;
Bram Moolenaar60462872009-11-03 11:40:19 +00007002 int prev_len = typebuf.tb_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007003
7004 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaar4930a762016-09-11 14:39:53 +02007005 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007006
7007#ifdef USE_ON_FLY_SCROLL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007008 dont_scroll = TRUE; // disallow scrolling here
Bram Moolenaar071d4272004-06-13 20:20:40 +00007009#endif
7010
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007011 // get the register name. No name means to use the previous one
Bram Moolenaar071d4272004-06-13 20:20:40 +00007012 c = *eap->arg;
7013 if (c == NUL || (c == '*' && *eap->cmd == '*'))
7014 c = '@';
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007015 // Put the register in the typeahead buffer with the "silent" flag.
Bram Moolenaard333d1e2006-11-07 17:43:47 +00007016 if (do_execreg(c, TRUE, vim_strchr(p_cpo, CPO_EXECBUF) != NULL, TRUE)
7017 == FAIL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007018 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007019 beep_flush();
Bram Moolenaardf177f62005-02-22 08:39:57 +00007020 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007021 else
7022 {
7023 int save_efr = exec_from_reg;
7024
7025 exec_from_reg = TRUE;
7026
7027 /*
7028 * Execute from the typeahead buffer.
Bram Moolenaar60462872009-11-03 11:40:19 +00007029 * Continue until the stuff buffer is empty and all added characters
7030 * have been consumed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007031 */
Bram Moolenaar60462872009-11-03 11:40:19 +00007032 while (!stuff_empty() || typebuf.tb_len > prev_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007033 (void)do_cmdline(NULL, getexline, NULL, DOCMD_NOWAIT|DOCMD_VERBOSE);
7034
7035 exec_from_reg = save_efr;
7036 }
7037}
7038
7039/*
7040 * ":!".
7041 */
7042 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007043ex_bang(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007044{
7045 do_bang(eap->addr_count, eap, eap->forceit, TRUE, TRUE);
7046}
7047
7048/*
7049 * ":undo".
7050 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007051 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +01007052ex_undo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007053{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007054 if (eap->addr_count == 1) // :undo 123
Bram Moolenaar730cde92010-06-27 05:18:54 +02007055 undo_time(eap->line2, FALSE, FALSE, TRUE);
Bram Moolenaard3667a22006-03-16 21:35:52 +00007056 else
7057 u_undo(1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007058}
7059
Bram Moolenaar55debbe2010-05-23 23:34:36 +02007060#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar6df6f472010-07-18 18:04:50 +02007061 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007062ex_wundo(exarg_T *eap)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02007063{
7064 char_u hash[UNDO_HASH_SIZE];
7065
7066 u_compute_hash(hash);
7067 u_write_undo(eap->arg, eap->forceit, curbuf, hash);
7068}
7069
Bram Moolenaar6df6f472010-07-18 18:04:50 +02007070 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007071ex_rundo(exarg_T *eap)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02007072{
7073 char_u hash[UNDO_HASH_SIZE];
7074
7075 u_compute_hash(hash);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02007076 u_read_undo(eap->arg, hash, NULL);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02007077}
7078#endif
7079
Bram Moolenaar071d4272004-06-13 20:20:40 +00007080/*
7081 * ":redo".
7082 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007083 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007084ex_redo(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007085{
7086 u_redo(1);
7087}
7088
7089/*
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007090 * ":earlier" and ":later".
7091 */
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007092 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007093ex_later(exarg_T *eap)
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007094{
7095 long count = 0;
7096 int sec = FALSE;
Bram Moolenaar730cde92010-06-27 05:18:54 +02007097 int file = FALSE;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007098 char_u *p = eap->arg;
7099
7100 if (*p == NUL)
7101 count = 1;
7102 else if (isdigit(*p))
7103 {
7104 count = getdigits(&p);
7105 switch (*p)
7106 {
7107 case 's': ++p; sec = TRUE; break;
7108 case 'm': ++p; sec = TRUE; count *= 60; break;
7109 case 'h': ++p; sec = TRUE; count *= 60 * 60; break;
Bram Moolenaar730cde92010-06-27 05:18:54 +02007110 case 'd': ++p; sec = TRUE; count *= 24 * 60 * 60; break;
7111 case 'f': ++p; file = TRUE; break;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007112 }
7113 }
7114
7115 if (*p != NUL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007116 semsg(_(e_invarg2), eap->arg);
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007117 else
Bram Moolenaar730cde92010-06-27 05:18:54 +02007118 undo_time(eap->cmdidx == CMD_earlier ? -count : count,
7119 sec, file, FALSE);
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007120}
7121
7122/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007123 * ":redir": start/stop redirection.
7124 */
7125 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007126ex_redir(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007127{
7128 char *mode;
7129 char_u *fname;
Bram Moolenaarca472992005-01-21 11:46:23 +00007130 char_u *arg = eap->arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007131
Bram Moolenaarba768492016-07-08 15:32:54 +02007132#ifdef FEAT_EVAL
Bram Moolenaar79815f12016-07-09 17:07:29 +02007133 if (redir_execute)
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02007134 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007135 emsg(_("E930: Cannot use :redir inside execute()"));
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02007136 return;
7137 }
Bram Moolenaarba768492016-07-08 15:32:54 +02007138#endif
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02007139
Bram Moolenaar071d4272004-06-13 20:20:40 +00007140 if (STRICMP(eap->arg, "END") == 0)
7141 close_redir();
7142 else
7143 {
Bram Moolenaarca472992005-01-21 11:46:23 +00007144 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00007145 {
Bram Moolenaarca472992005-01-21 11:46:23 +00007146 ++arg;
7147 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00007148 {
Bram Moolenaarca472992005-01-21 11:46:23 +00007149 ++arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007150 mode = "a";
7151 }
7152 else
7153 mode = "w";
Bram Moolenaarca472992005-01-21 11:46:23 +00007154 arg = skipwhite(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007155
7156 close_redir();
7157
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007158 // Expand environment variables and "~/".
Bram Moolenaarca472992005-01-21 11:46:23 +00007159 fname = expand_env_save(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007160 if (fname == NULL)
7161 return;
7162#ifdef FEAT_BROWSE
7163 if (cmdmod.browse)
7164 {
7165 char_u *browseFile;
7166
Bram Moolenaar7171abe2004-10-11 10:06:20 +00007167 browseFile = do_browse(BROWSE_SAVE,
7168 (char_u *)_("Save Redirection"),
Bram Moolenaarc36651b2018-04-29 12:22:56 +02007169 fname, NULL, NULL,
7170 (char_u *)_(BROWSE_FILTER_ALL_FILES), curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007171 if (browseFile == NULL)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007172 return; // operation cancelled
Bram Moolenaar071d4272004-06-13 20:20:40 +00007173 vim_free(fname);
7174 fname = browseFile;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007175 eap->forceit = TRUE; // since dialog already asked
Bram Moolenaar071d4272004-06-13 20:20:40 +00007176 }
7177#endif
7178
7179 redir_fd = open_exfile(fname, eap->forceit, mode);
7180 vim_free(fname);
7181 }
7182#ifdef FEAT_EVAL
Bram Moolenaarca472992005-01-21 11:46:23 +00007183 else if (*arg == '@')
Bram Moolenaar071d4272004-06-13 20:20:40 +00007184 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007185 // redirect to a register a-z (resp. A-Z for appending)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007186 close_redir();
Bram Moolenaarca472992005-01-21 11:46:23 +00007187 ++arg;
7188 if (ASCII_ISALPHA(*arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007189# ifdef FEAT_CLIPBOARD
Bram Moolenaarca472992005-01-21 11:46:23 +00007190 || *arg == '*'
Bram Moolenaar9a51c6e2006-11-14 19:25:02 +00007191 || *arg == '+'
Bram Moolenaar071d4272004-06-13 20:20:40 +00007192# endif
Bram Moolenaarca472992005-01-21 11:46:23 +00007193 || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00007194 {
Bram Moolenaarca472992005-01-21 11:46:23 +00007195 redir_reg = *arg++;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007196 if (*arg == '>' && arg[1] == '>') // append
Bram Moolenaard9d30582005-05-18 22:10:28 +00007197 arg += 2;
Bram Moolenaarc188b882007-10-19 14:20:54 +00007198 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007199 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007200 // Can use both "@a" and "@a>".
Bram Moolenaar2c29bee2005-06-01 21:46:07 +00007201 if (*arg == '>')
7202 arg++;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007203 // Make register empty when not using @A-@Z and the
7204 // command is valid.
Bram Moolenaarc188b882007-10-19 14:20:54 +00007205 if (*arg == NUL && !isupper(redir_reg))
7206 write_reg_contents(redir_reg, (char_u *)"", -1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007207 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00007208 }
7209 if (*arg != NUL)
7210 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00007211 redir_reg = 0;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007212 semsg(_(e_invarg2), eap->arg);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007213 }
7214 }
7215 else if (*arg == '=' && arg[1] == '>')
7216 {
7217 int append;
7218
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007219 // redirect to a variable
Bram Moolenaardf177f62005-02-22 08:39:57 +00007220 close_redir();
7221 arg += 2;
7222
7223 if (*arg == '>')
7224 {
7225 ++arg;
7226 append = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007227 }
7228 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00007229 append = FALSE;
7230
7231 if (var_redir_start(skipwhite(arg), append) == OK)
7232 redir_vname = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007233 }
7234#endif
7235
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007236 // TODO: redirect to a buffer
Bram Moolenaar071d4272004-06-13 20:20:40 +00007237
Bram Moolenaar071d4272004-06-13 20:20:40 +00007238 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007239 semsg(_(e_invarg2), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007240 }
Bram Moolenaar29b2d262006-09-10 19:07:28 +00007241
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007242 // Make sure redirection is not off. Can happen for cmdline completion
7243 // that indirectly invokes a command to catch its output.
Bram Moolenaar29b2d262006-09-10 19:07:28 +00007244 if (redir_fd != NULL
7245#ifdef FEAT_EVAL
7246 || redir_reg || redir_vname
7247#endif
7248 )
7249 redir_off = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007250}
7251
7252/*
7253 * ":redraw": force redraw
7254 */
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01007255 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007256ex_redraw(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007257{
7258 int r = RedrawingDisabled;
7259 int p = p_lz;
7260
7261 RedrawingDisabled = 0;
7262 p_lz = FALSE;
Bram Moolenaarabc39ab2017-03-01 18:04:05 +01007263 validate_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007264 update_topline();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007265 update_screen(eap->forceit ? CLEAR : VIsual_active ? INVERTED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007266#ifdef FEAT_TITLE
7267 if (need_maketitle)
7268 maketitle();
7269#endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007270#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
7271# ifdef VIMDLL
7272 if (!gui.in_use)
7273# endif
7274 resize_console_buf();
Bram Moolenaar78d21da2019-02-17 15:00:52 +01007275#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007276 RedrawingDisabled = r;
7277 p_lz = p;
7278
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007279 // Reset msg_didout, so that a message that's there is overwritten.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007280 msg_didout = FALSE;
7281 msg_col = 0;
7282
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007283 // No need to wait after an intentional redraw.
Bram Moolenaar56667a52013-07-17 11:54:28 +02007284 need_wait_return = FALSE;
7285
Bram Moolenaar071d4272004-06-13 20:20:40 +00007286 out_flush();
7287}
7288
7289/*
7290 * ":redrawstatus": force redraw of status line(s)
7291 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007292 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007293ex_redrawstatus(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007294{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007295 int r = RedrawingDisabled;
7296 int p = p_lz;
7297
7298 RedrawingDisabled = 0;
7299 p_lz = FALSE;
7300 if (eap->forceit)
7301 status_redraw_all();
7302 else
7303 status_redraw_curbuf();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007304 update_screen(VIsual_active ? INVERTED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007305 RedrawingDisabled = r;
7306 p_lz = p;
7307 out_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007308}
7309
Bram Moolenaare12bab32019-01-08 22:02:56 +01007310/*
7311 * ":redrawtabline": force redraw of the tabline
7312 */
7313 static void
7314ex_redrawtabline(exarg_T *eap UNUSED)
7315{
7316 int r = RedrawingDisabled;
7317 int p = p_lz;
7318
7319 RedrawingDisabled = 0;
7320 p_lz = FALSE;
7321
7322 draw_tabline();
7323
7324 RedrawingDisabled = r;
7325 p_lz = p;
7326 out_flush();
7327}
7328
Bram Moolenaar071d4272004-06-13 20:20:40 +00007329 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007330close_redir(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007331{
7332 if (redir_fd != NULL)
7333 {
7334 fclose(redir_fd);
7335 redir_fd = NULL;
7336 }
7337#ifdef FEAT_EVAL
7338 redir_reg = 0;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007339 if (redir_vname)
7340 {
7341 var_redir_stop();
7342 redir_vname = 0;
7343 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007344#endif
7345}
7346
Bram Moolenaarf96ae0b2019-07-28 15:21:55 +02007347#if (defined(FEAT_SESSION) || defined(FEAT_EVAL)) || defined(PROTO)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007348 int
Bram Moolenaarbd67aac2019-09-21 23:09:04 +02007349vim_mkdir_emsg(char_u *name, int prot UNUSED)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007350{
7351 if (vim_mkdir(name, prot) != 0)
7352 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007353 semsg(_("E739: Cannot create directory: %s"), name);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007354 return FAIL;
7355 }
7356 return OK;
7357}
7358#endif
7359
Bram Moolenaar071d4272004-06-13 20:20:40 +00007360/*
7361 * Open a file for writing for an Ex command, with some checks.
7362 * Return file descriptor, or NULL on failure.
7363 */
7364 FILE *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007365open_exfile(
7366 char_u *fname,
7367 int forceit,
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007368 char *mode) // "w" for create new file or "a" for append
Bram Moolenaar071d4272004-06-13 20:20:40 +00007369{
7370 FILE *fd;
7371
7372#ifdef UNIX
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007373 // with Unix it is possible to open a directory
Bram Moolenaar071d4272004-06-13 20:20:40 +00007374 if (mch_isdir(fname))
7375 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007376 semsg(_(e_isadir2), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007377 return NULL;
7378 }
7379#endif
7380 if (!forceit && *mode != 'a' && vim_fexists(fname))
7381 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007382 semsg(_("E189: \"%s\" exists (add ! to override)"), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007383 return NULL;
7384 }
7385
7386 if ((fd = mch_fopen((char *)fname, mode)) == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007387 semsg(_("E190: Cannot open \"%s\" for writing"), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007388
7389 return fd;
7390}
7391
7392/*
7393 * ":mark" and ":k".
7394 */
7395 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007396ex_mark(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007397{
7398 pos_T pos;
7399
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007400 if (*eap->arg == NUL) // No argument?
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007401 emsg(_(e_argreq));
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007402 else if (eap->arg[1] != NUL) // more than one character?
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007403 emsg(_(e_trailing));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007404 else
7405 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007406 pos = curwin->w_cursor; // save curwin->w_cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00007407 curwin->w_cursor.lnum = eap->line2;
7408 beginline(BL_WHITE | BL_FIX);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007409 if (setmark(*eap->arg) == FAIL) // set mark
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007410 emsg(_("E191: Argument must be a letter or forward/backward quote"));
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007411 curwin->w_cursor = pos; // restore curwin->w_cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00007412 }
7413}
7414
7415/*
7416 * Update w_topline, w_leftcol and the cursor position.
7417 */
7418 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007419update_topline_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007420{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007421 check_cursor(); // put cursor on valid line
Bram Moolenaar071d4272004-06-13 20:20:40 +00007422 update_topline();
7423 if (!curwin->w_p_wrap)
7424 validate_cursor();
7425 update_curswant();
7426}
7427
Bram Moolenaar071d4272004-06-13 20:20:40 +00007428/*
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007429 * Save the current State and go to Normal mode.
7430 * Return TRUE if the typeahead could be saved.
7431 */
7432 int
7433save_current_state(save_state_T *sst)
7434{
7435 sst->save_msg_scroll = msg_scroll;
7436 sst->save_restart_edit = restart_edit;
7437 sst->save_msg_didout = msg_didout;
7438 sst->save_State = State;
7439 sst->save_insertmode = p_im;
7440 sst->save_finish_op = finish_op;
7441 sst->save_opcount = opcount;
Bram Moolenaarcce713d2019-03-04 11:40:12 +01007442 sst->save_reg_executing = reg_executing;
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007443
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007444 msg_scroll = FALSE; // no msg scrolling in Normal mode
7445 restart_edit = 0; // don't go to Insert mode
7446 p_im = FALSE; // don't use 'insertmode'
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007447
7448 /*
7449 * Save the current typeahead. This is required to allow using ":normal"
7450 * from an event handler and makes sure we don't hang when the argument
7451 * ends with half a command.
7452 */
7453 save_typeahead(&sst->tabuf);
7454 return sst->tabuf.typebuf_valid;
7455}
7456
7457 void
7458restore_current_state(save_state_T *sst)
7459{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007460 // Restore the previous typeahead.
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007461 restore_typeahead(&sst->tabuf);
7462
7463 msg_scroll = sst->save_msg_scroll;
7464 restart_edit = sst->save_restart_edit;
7465 p_im = sst->save_insertmode;
7466 finish_op = sst->save_finish_op;
7467 opcount = sst->save_opcount;
Bram Moolenaarcce713d2019-03-04 11:40:12 +01007468 reg_executing = sst->save_reg_executing;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007469 msg_didout |= sst->save_msg_didout; // don't reset msg_didout now
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007470
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007471 // Restore the state (needed when called from a function executed for
7472 // 'indentexpr'). Update the mouse and cursor, they may have changed.
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007473 State = sst->save_State;
7474#ifdef CURSOR_SHAPE
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007475 ui_cursor_shape(); // may show different cursor shape
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007476#endif
7477}
7478
7479/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007480 * ":normal[!] {commands}": Execute normal mode commands.
7481 */
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01007482 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007483ex_normal(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007484{
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007485 save_state_T save_state;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007486 char_u *arg = NULL;
7487 int l;
7488 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007489
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00007490 if (ex_normal_lock > 0)
7491 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007492 emsg(_(e_secure));
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00007493 return;
7494 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007495 if (ex_normal_busy >= p_mmd)
7496 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007497 emsg(_("E192: Recursive use of :normal too deep"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007498 return;
7499 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007500
Bram Moolenaar071d4272004-06-13 20:20:40 +00007501 /*
7502 * vgetc() expects a CSI and K_SPECIAL to have been escaped. Don't do
7503 * this for the K_SPECIAL leading byte, otherwise special keys will not
7504 * work.
7505 */
7506 if (has_mbyte)
7507 {
7508 int len = 0;
7509
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007510 // Count the number of characters to be escaped.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007511 for (p = eap->arg; *p != NUL; ++p)
7512 {
Bram Moolenaar13505972019-01-24 15:04:48 +01007513#ifdef FEAT_GUI
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007514 if (*p == CSI) // leadbyte CSI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007515 len += 2;
Bram Moolenaar13505972019-01-24 15:04:48 +01007516#endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007517 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007518 if (*++p == K_SPECIAL // trailbyte K_SPECIAL or CSI
Bram Moolenaar13505972019-01-24 15:04:48 +01007519#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007520 || *p == CSI
Bram Moolenaar13505972019-01-24 15:04:48 +01007521#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007522 )
7523 len += 2;
7524 }
7525 if (len > 0)
7526 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02007527 arg = alloc(STRLEN(eap->arg) + len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007528 if (arg != NULL)
7529 {
7530 len = 0;
7531 for (p = eap->arg; *p != NUL; ++p)
7532 {
7533 arg[len++] = *p;
Bram Moolenaar13505972019-01-24 15:04:48 +01007534#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007535 if (*p == CSI)
7536 {
7537 arg[len++] = KS_EXTRA;
7538 arg[len++] = (int)KE_CSI;
7539 }
Bram Moolenaar13505972019-01-24 15:04:48 +01007540#endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007541 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007542 {
7543 arg[len++] = *++p;
7544 if (*p == K_SPECIAL)
7545 {
7546 arg[len++] = KS_SPECIAL;
7547 arg[len++] = KE_FILLER;
7548 }
Bram Moolenaar13505972019-01-24 15:04:48 +01007549#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007550 else if (*p == CSI)
7551 {
7552 arg[len++] = KS_EXTRA;
7553 arg[len++] = (int)KE_CSI;
7554 }
Bram Moolenaar13505972019-01-24 15:04:48 +01007555#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007556 }
7557 arg[len] = NUL;
7558 }
7559 }
7560 }
7561 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007562
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007563 ++ex_normal_busy;
7564 if (save_current_state(&save_state))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007565 {
7566 /*
7567 * Repeat the :normal command for each line in the range. When no
7568 * range given, execute it just once, without positioning the cursor
7569 * first.
7570 */
7571 do
7572 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007573 if (eap->addr_count != 0)
7574 {
7575 curwin->w_cursor.lnum = eap->line1++;
7576 curwin->w_cursor.col = 0;
Bram Moolenaard5d37532017-03-27 23:02:07 +02007577 check_cursor_moved(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007578 }
7579
Bram Moolenaar13505972019-01-24 15:04:48 +01007580 exec_normal_cmd(arg != NULL
7581 ? arg
7582 : eap->arg, eap->forceit ? REMAP_NONE : REMAP_YES, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007583 }
7584 while (eap->addr_count > 0 && eap->line1 <= eap->line2 && !got_int);
7585 }
7586
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007587 // Might not return to the main loop when in an event handler.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007588 update_topline_cursor();
7589
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007590 restore_current_state(&save_state);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007591 --ex_normal_busy;
Bram Moolenaareda73602014-11-05 09:53:23 +01007592 setmouse();
Bram Moolenaareda73602014-11-05 09:53:23 +01007593#ifdef CURSOR_SHAPE
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007594 ui_cursor_shape(); // may show different cursor shape
Bram Moolenaareda73602014-11-05 09:53:23 +01007595#endif
7596
Bram Moolenaar071d4272004-06-13 20:20:40 +00007597 vim_free(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007598}
7599
7600/*
Bram Moolenaar64969662005-12-14 21:59:55 +00007601 * ":startinsert", ":startreplace" and ":startgreplace"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007602 */
7603 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007604ex_startinsert(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007605{
Bram Moolenaarfd371682005-01-14 21:42:54 +00007606 if (eap->forceit)
7607 {
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02007608 // cursor line can be zero on startup
Bram Moolenaar09ca9322017-09-26 17:40:45 +02007609 if (!curwin->w_cursor.lnum)
7610 curwin->w_cursor.lnum = 1;
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02007611 set_cursor_for_append_to_line();
Bram Moolenaarfd371682005-01-14 21:42:54 +00007612 }
7613
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02007614 // Ignore the command when already in Insert mode. Inserting an
7615 // expression register that invokes a function can do this.
Bram Moolenaara40c5002005-01-09 21:16:21 +00007616 if (State & INSERT)
7617 return;
7618
Bram Moolenaar64969662005-12-14 21:59:55 +00007619 if (eap->cmdidx == CMD_startinsert)
7620 restart_edit = 'a';
7621 else if (eap->cmdidx == CMD_startreplace)
7622 restart_edit = 'R';
Bram Moolenaar071d4272004-06-13 20:20:40 +00007623 else
Bram Moolenaar64969662005-12-14 21:59:55 +00007624 restart_edit = 'V';
7625
7626 if (!eap->forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007627 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00007628 if (eap->cmdidx == CMD_startinsert)
7629 restart_edit = 'i';
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02007630 curwin->w_curswant = 0; // avoid MAXCOL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007631 }
7632}
7633
7634/*
7635 * ":stopinsert"
7636 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007637 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007638ex_stopinsert(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007639{
7640 restart_edit = 0;
7641 stop_insert_mode = TRUE;
Bram Moolenaarfd773e92016-04-02 19:39:16 +02007642 clearmode();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007643}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007644
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00007645/*
7646 * Execute normal mode command "cmd".
7647 * "remap" can be REMAP_NONE or REMAP_YES.
7648 */
7649 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007650exec_normal_cmd(char_u *cmd, int remap, int silent)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00007651{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007652 // Stuff the argument into the typeahead buffer.
Bram Moolenaar25281632016-01-21 23:32:32 +01007653 ins_typebuf(cmd, remap, 0, TRUE, silent);
Bram Moolenaar586c70c2018-10-02 16:23:58 +02007654 exec_normal(FALSE, FALSE, FALSE);
Bram Moolenaar25281632016-01-21 23:32:32 +01007655}
Bram Moolenaar25281632016-01-21 23:32:32 +01007656
Bram Moolenaar25281632016-01-21 23:32:32 +01007657/*
7658 * Execute normal_cmd() until there is no typeahead left.
Bram Moolenaar586c70c2018-10-02 16:23:58 +02007659 * When "use_vpeekc" is TRUE use vpeekc() to check for available chars.
Bram Moolenaar25281632016-01-21 23:32:32 +01007660 */
7661 void
Bram Moolenaar586c70c2018-10-02 16:23:58 +02007662exec_normal(int was_typed, int use_vpeekc, int may_use_terminal_loop UNUSED)
Bram Moolenaar25281632016-01-21 23:32:32 +01007663{
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00007664 oparg_T oa;
Bram Moolenaar905dd902019-04-07 14:21:47 +02007665 int c;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00007666
Bram Moolenaar905dd902019-04-07 14:21:47 +02007667 // When calling vpeekc() from feedkeys() it will return Ctrl_C when there
7668 // is nothing to get, so also check for Ctrl_C.
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00007669 clear_oparg(&oa);
7670 finish_op = FALSE;
Bram Moolenaar586c70c2018-10-02 16:23:58 +02007671 while ((!stuff_empty()
7672 || ((was_typed || !typebuf_typed()) && typebuf.tb_len > 0)
Bram Moolenaar905dd902019-04-07 14:21:47 +02007673 || (use_vpeekc && (c = vpeekc()) != NUL && c != Ctrl_C))
Bram Moolenaar586c70c2018-10-02 16:23:58 +02007674 && !got_int)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00007675 {
7676 update_topline_cursor();
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02007677#ifdef FEAT_TERMINAL
Bram Moolenaar655a82a2018-05-08 22:01:07 +02007678 if (may_use_terminal_loop && term_use_loop()
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02007679 && oa.op_type == OP_NOP && oa.regname == NUL
7680 && !VIsual_active)
7681 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007682 // If terminal_loop() returns OK we got a key that is handled
7683 // in Normal model. With FAIL we first need to position the
7684 // cursor and the screen needs to be redrawn.
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02007685 if (terminal_loop(TRUE) == OK)
7686 normal_cmd(&oa, TRUE);
7687 }
7688 else
7689#endif
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007690 // execute a Normal mode cmd
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02007691 normal_cmd(&oa, TRUE);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00007692 }
7693}
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00007694
Bram Moolenaar071d4272004-06-13 20:20:40 +00007695#ifdef FEAT_FIND_ID
7696 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007697ex_checkpath(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007698{
7699 find_pattern_in_path(NULL, 0, 0, FALSE, FALSE, CHECK_PATH, 1L,
7700 eap->forceit ? ACTION_SHOW_ALL : ACTION_SHOW,
7701 (linenr_T)1, (linenr_T)MAXLNUM);
7702}
7703
Bram Moolenaar4033c552017-09-16 20:54:51 +02007704#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007705/*
7706 * ":psearch"
7707 */
7708 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007709ex_psearch(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007710{
7711 g_do_tagpreview = p_pvh;
7712 ex_findpat(eap);
7713 g_do_tagpreview = 0;
7714}
7715#endif
7716
7717 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007718ex_findpat(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007719{
7720 int whole = TRUE;
7721 long n;
7722 char_u *p;
7723 int action;
7724
7725 switch (cmdnames[eap->cmdidx].cmd_name[2])
7726 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007727 case 'e': // ":psearch", ":isearch" and ":dsearch"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007728 if (cmdnames[eap->cmdidx].cmd_name[0] == 'p')
7729 action = ACTION_GOTO;
7730 else
7731 action = ACTION_SHOW;
7732 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007733 case 'i': // ":ilist" and ":dlist"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007734 action = ACTION_SHOW_ALL;
7735 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007736 case 'u': // ":ijump" and ":djump"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007737 action = ACTION_GOTO;
7738 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007739 default: // ":isplit" and ":dsplit"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007740 action = ACTION_SPLIT;
7741 break;
7742 }
7743
7744 n = 1;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007745 if (vim_isdigit(*eap->arg)) // get count
Bram Moolenaar071d4272004-06-13 20:20:40 +00007746 {
7747 n = getdigits(&eap->arg);
7748 eap->arg = skipwhite(eap->arg);
7749 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007750 if (*eap->arg == '/') // Match regexp, not just whole words
Bram Moolenaar071d4272004-06-13 20:20:40 +00007751 {
7752 whole = FALSE;
7753 ++eap->arg;
7754 p = skip_regexp(eap->arg, '/', p_magic, NULL);
7755 if (*p)
7756 {
7757 *p++ = NUL;
7758 p = skipwhite(p);
7759
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007760 // Check for trailing illegal characters
Bram Moolenaar071d4272004-06-13 20:20:40 +00007761 if (!ends_excmd(*p))
7762 eap->errmsg = e_trailing;
7763 else
7764 eap->nextcmd = check_nextcmd(p);
7765 }
7766 }
7767 if (!eap->skip)
7768 find_pattern_in_path(eap->arg, 0, (int)STRLEN(eap->arg),
7769 whole, !eap->forceit,
7770 *eap->cmd == 'd' ? FIND_DEFINE : FIND_ANY,
7771 n, action, eap->line1, eap->line2);
7772}
7773#endif
7774
Bram Moolenaar071d4272004-06-13 20:20:40 +00007775
Bram Moolenaar4033c552017-09-16 20:54:51 +02007776#ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00007777/*
7778 * ":ptag", ":ptselect", ":ptjump", ":ptnext", etc.
7779 */
7780 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007781ex_ptag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007782{
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007783 g_do_tagpreview = p_pvh; // will be reset to 0 in ex_tag_cmd()
Bram Moolenaar071d4272004-06-13 20:20:40 +00007784 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
7785}
7786
7787/*
7788 * ":pedit"
7789 */
7790 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007791ex_pedit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007792{
7793 win_T *curwin_save = curwin;
7794
Bram Moolenaar026587b2019-08-17 15:08:00 +02007795 // Open the preview window or popup and make it the current window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007796 g_do_tagpreview = p_pvh;
Bram Moolenaar576a4a62019-08-18 15:25:17 +02007797 prepare_tagpreview(TRUE, TRUE, FALSE);
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02007798
Bram Moolenaar026587b2019-08-17 15:08:00 +02007799 // Edit the file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007800 do_exedit(eap, NULL);
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02007801
Bram Moolenaar071d4272004-06-13 20:20:40 +00007802 if (curwin != curwin_save && win_valid(curwin_save))
7803 {
Bram Moolenaar026587b2019-08-17 15:08:00 +02007804 // Return cursor to where we were
Bram Moolenaar071d4272004-06-13 20:20:40 +00007805 validate_cursor();
7806 redraw_later(VALID);
7807 win_enter(curwin_save, TRUE);
7808 }
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01007809# ifdef FEAT_PROP_POPUP
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02007810 else if (WIN_IS_POPUP(curwin))
7811 {
7812 // can't keep focus in popup window
7813 win_enter(firstwin, TRUE);
7814 }
7815# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007816 g_do_tagpreview = 0;
7817}
Bram Moolenaar4033c552017-09-16 20:54:51 +02007818#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007819
7820/*
7821 * ":stag", ":stselect" and ":stjump".
7822 */
7823 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007824ex_stag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007825{
7826 postponed_split = -1;
7827 postponed_split_flags = cmdmod.split;
Bram Moolenaard326ce82007-03-11 14:48:29 +00007828 postponed_split_tab = cmdmod.tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007829 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
7830 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00007831 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007832}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007833
7834/*
7835 * ":tag", ":tselect", ":tjump", ":tnext", etc.
7836 */
7837 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007838ex_tag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007839{
7840 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name);
7841}
7842
7843 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007844ex_tag_cmd(exarg_T *eap, char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007845{
7846 int cmd;
7847
7848 switch (name[1])
7849 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007850 case 'j': cmd = DT_JUMP; // ":tjump"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007851 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007852 case 's': cmd = DT_SELECT; // ":tselect"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007853 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007854 case 'p': cmd = DT_PREV; // ":tprevious"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007855 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007856 case 'N': cmd = DT_PREV; // ":tNext"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007857 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007858 case 'n': cmd = DT_NEXT; // ":tnext"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007859 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007860 case 'o': cmd = DT_POP; // ":pop"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007861 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007862 case 'f': // ":tfirst"
7863 case 'r': cmd = DT_FIRST; // ":trewind"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007864 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007865 case 'l': cmd = DT_LAST; // ":tlast"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007866 break;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007867 default: // ":tag"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007868#ifdef FEAT_CSCOPE
Bram Moolenaar7c94c262008-06-20 09:11:34 +00007869 if (p_cst && *eap->arg != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007870 {
Bram Moolenaard4db7712016-11-12 19:16:46 +01007871 ex_cstag(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007872 return;
7873 }
7874#endif
7875 cmd = DT_TAG;
7876 break;
7877 }
7878
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00007879 if (name[0] == 'l')
7880 {
7881#ifndef FEAT_QUICKFIX
7882 ex_ni(eap);
7883 return;
7884#else
7885 cmd = DT_LTAG;
7886#endif
7887 }
7888
Bram Moolenaar071d4272004-06-13 20:20:40 +00007889 do_tag(eap->arg, cmd, eap->addr_count > 0 ? (int)eap->line2 : 1,
7890 eap->forceit, TRUE);
7891}
7892
7893/*
Bram Moolenaar05bb9532008-07-04 09:44:11 +00007894 * Check "str" for starting with a special cmdline variable.
7895 * If found return one of the SPEC_ values and set "*usedlen" to the length of
7896 * the variable. Otherwise return -1 and "*usedlen" is unchanged.
7897 */
7898 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007899find_cmdline_var(char_u *src, int *usedlen)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00007900{
7901 int len;
7902 int i;
Bram Moolenaar8f0b2d42009-05-16 14:41:10 +00007903 static char *(spec_str[]) = {
Bram Moolenaar05bb9532008-07-04 09:44:11 +00007904 "%",
7905#define SPEC_PERC 0
7906 "#",
Bram Moolenaar65f08472017-09-10 18:16:20 +02007907#define SPEC_HASH (SPEC_PERC + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007908 "<cword>", // cursor word
Bram Moolenaar65f08472017-09-10 18:16:20 +02007909#define SPEC_CWORD (SPEC_HASH + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007910 "<cWORD>", // cursor WORD
Bram Moolenaar65f08472017-09-10 18:16:20 +02007911#define SPEC_CCWORD (SPEC_CWORD + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007912 "<cexpr>", // expr under cursor
Bram Moolenaar65f08472017-09-10 18:16:20 +02007913#define SPEC_CEXPR (SPEC_CCWORD + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007914 "<cfile>", // cursor path name
Bram Moolenaar65f08472017-09-10 18:16:20 +02007915#define SPEC_CFILE (SPEC_CEXPR + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007916 "<sfile>", // ":so" file name
Bram Moolenaar65f08472017-09-10 18:16:20 +02007917#define SPEC_SFILE (SPEC_CFILE + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007918 "<slnum>", // ":so" file line number
Bram Moolenaar65f08472017-09-10 18:16:20 +02007919#define SPEC_SLNUM (SPEC_SFILE + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007920 "<afile>", // autocommand file name
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02007921#define SPEC_AFILE (SPEC_SLNUM + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007922 "<abuf>", // autocommand buffer number
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02007923#define SPEC_ABUF (SPEC_AFILE + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007924 "<amatch>", // autocommand match name
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007925#define SPEC_AMATCH (SPEC_ABUF + 1)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007926 "<sflnum>", // script file line number
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02007927#define SPEC_SFLNUM (SPEC_AMATCH + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00007928#ifdef FEAT_CLIENTSERVER
7929 "<client>"
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02007930# define SPEC_CLIENT (SPEC_SFLNUM + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00007931#endif
7932 };
Bram Moolenaar05bb9532008-07-04 09:44:11 +00007933
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00007934 for (i = 0; i < (int)(sizeof(spec_str) / sizeof(char *)); ++i)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00007935 {
7936 len = (int)STRLEN(spec_str[i]);
7937 if (STRNCMP(src, spec_str[i], len) == 0)
7938 {
7939 *usedlen = len;
7940 return i;
7941 }
7942 }
7943 return -1;
7944}
7945
7946/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007947 * Evaluate cmdline variables.
7948 *
7949 * change '%' to curbuf->b_ffname
7950 * '#' to curwin->w_altfile
7951 * '<cword>' to word under the cursor
7952 * '<cWORD>' to WORD under the cursor
Bram Moolenaar8071cb22019-07-12 17:58:01 +02007953 * '<cexpr>' to C-expression under the cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00007954 * '<cfile>' to path name under the cursor
7955 * '<sfile>' to sourced file name
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01007956 * '<slnum>' to sourced file line number
Bram Moolenaar071d4272004-06-13 20:20:40 +00007957 * '<afile>' to file name for autocommand
7958 * '<abuf>' to buffer number for autocommand
7959 * '<amatch>' to matching name for autocommand
7960 *
7961 * When an error is detected, "errormsg" is set to a non-NULL pointer (may be
7962 * "" for error without a message) and NULL is returned.
7963 * Returns an allocated string if a valid match was found.
7964 * Returns NULL if no match was found. "usedlen" then still contains the
7965 * number of characters to skip.
7966 */
7967 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007968eval_vars(
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007969 char_u *src, // pointer into commandline
7970 char_u *srcstart, // beginning of valid memory for src
7971 int *usedlen, // characters after src that are used
7972 linenr_T *lnump, // line number for :e command, or NULL
7973 char **errormsg, // pointer to error message
7974 int *escaped) // return value has escaped white space (can
7975 // be NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007976{
7977 int i;
7978 char_u *s;
7979 char_u *result;
7980 char_u *resultbuf = NULL;
7981 int resultlen;
7982 buf_T *buf;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007983 int valid = VALID_HEAD + VALID_PATH; // assume valid result
Bram Moolenaar071d4272004-06-13 20:20:40 +00007984 int spec_idx;
Bram Moolenaarfd249462018-07-28 16:14:30 +02007985 int tilde_file = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007986 int skip_mod = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007987 char_u strbuf[30];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007988
7989 *errormsg = NULL;
Bram Moolenaar63b92542007-03-27 14:57:09 +00007990 if (escaped != NULL)
7991 *escaped = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007992
7993 /*
7994 * Check if there is something to do.
7995 */
Bram Moolenaar05bb9532008-07-04 09:44:11 +00007996 spec_idx = find_cmdline_var(src, usedlen);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01007997 if (spec_idx < 0) // no match
Bram Moolenaar071d4272004-06-13 20:20:40 +00007998 {
7999 *usedlen = 1;
8000 return NULL;
8001 }
8002
8003 /*
8004 * Skip when preceded with a backslash "\%" and "\#".
8005 * Note: In "\\%" the % is also not recognized!
8006 */
8007 if (src > srcstart && src[-1] == '\\')
8008 {
8009 *usedlen = 0;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008010 STRMOVE(src - 1, src); // remove backslash
Bram Moolenaar071d4272004-06-13 20:20:40 +00008011 return NULL;
8012 }
8013
8014 /*
8015 * word or WORD under cursor
8016 */
Bram Moolenaar65f08472017-09-10 18:16:20 +02008017 if (spec_idx == SPEC_CWORD || spec_idx == SPEC_CCWORD
8018 || spec_idx == SPEC_CEXPR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008019 {
Bram Moolenaar65f08472017-09-10 18:16:20 +02008020 resultlen = find_ident_under_cursor(&result,
8021 spec_idx == SPEC_CWORD ? (FIND_IDENT | FIND_STRING)
8022 : spec_idx == SPEC_CEXPR ? (FIND_IDENT | FIND_STRING | FIND_EVAL)
8023 : FIND_STRING);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008024 if (resultlen == 0)
8025 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008026 *errormsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +00008027 return NULL;
8028 }
8029 }
8030
8031 /*
8032 * '#': Alternate file name
8033 * '%': Current file name
8034 * File name under the cursor
8035 * File name for autocommand
8036 * and following modifiers
8037 */
8038 else
8039 {
8040 switch (spec_idx)
8041 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008042 case SPEC_PERC: // '%': current file
Bram Moolenaar071d4272004-06-13 20:20:40 +00008043 if (curbuf->b_fname == NULL)
8044 {
8045 result = (char_u *)"";
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008046 valid = 0; // Must have ":p:h" to be valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00008047 }
8048 else
Bram Moolenaar00136dc2018-07-25 21:19:13 +02008049 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008050 result = curbuf->b_fname;
Bram Moolenaar00136dc2018-07-25 21:19:13 +02008051 tilde_file = STRCMP(result, "~") == 0;
8052 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008053 break;
8054
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008055 case SPEC_HASH: // '#' or "#99": alternate file
8056 if (src[1] == '#') // "##": the argument list
Bram Moolenaar071d4272004-06-13 20:20:40 +00008057 {
8058 result = arg_all();
8059 resultbuf = result;
8060 *usedlen = 2;
Bram Moolenaar63b92542007-03-27 14:57:09 +00008061 if (escaped != NULL)
8062 *escaped = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008063 skip_mod = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008064 break;
8065 }
8066 s = src + 1;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008067 if (*s == '<') // "#<99" uses v:oldfiles
Bram Moolenaard812df62008-11-09 12:46:09 +00008068 ++s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008069 i = (int)getdigits(&s);
Bram Moolenaarc312b8b2017-10-28 17:53:04 +02008070 if (s == src + 2 && src[1] == '-')
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008071 // just a minus sign, don't skip over it
Bram Moolenaarc312b8b2017-10-28 17:53:04 +02008072 s--;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008073 *usedlen = (int)(s - src); // length of what we expand
Bram Moolenaar071d4272004-06-13 20:20:40 +00008074
Bram Moolenaarc312b8b2017-10-28 17:53:04 +02008075 if (src[1] == '<' && i != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008076 {
Bram Moolenaard812df62008-11-09 12:46:09 +00008077 if (*usedlen < 2)
8078 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008079 // Should we give an error message for #<text?
Bram Moolenaard812df62008-11-09 12:46:09 +00008080 *usedlen = 1;
8081 return NULL;
8082 }
8083#ifdef FEAT_EVAL
8084 result = list_find_str(get_vim_var_list(VV_OLDFILES),
8085 (long)i);
8086 if (result == NULL)
8087 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008088 *errormsg = "";
Bram Moolenaard812df62008-11-09 12:46:09 +00008089 return NULL;
8090 }
8091#else
Bram Moolenaar8e481e82019-01-15 20:07:48 +01008092 *errormsg = _("E809: #< is not available without the +eval feature");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008093 return NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +00008094#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008095 }
8096 else
Bram Moolenaard812df62008-11-09 12:46:09 +00008097 {
Bram Moolenaarc312b8b2017-10-28 17:53:04 +02008098 if (i == 0 && src[1] == '<' && *usedlen > 1)
8099 *usedlen = 1;
Bram Moolenaard812df62008-11-09 12:46:09 +00008100 buf = buflist_findnr(i);
8101 if (buf == NULL)
8102 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008103 *errormsg = _("E194: No alternate file name to substitute for '#'");
Bram Moolenaard812df62008-11-09 12:46:09 +00008104 return NULL;
8105 }
8106 if (lnump != NULL)
8107 *lnump = ECMD_LAST;
8108 if (buf->b_fname == NULL)
8109 {
8110 result = (char_u *)"";
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008111 valid = 0; // Must have ":p:h" to be valid
Bram Moolenaard812df62008-11-09 12:46:09 +00008112 }
8113 else
Bram Moolenaar00136dc2018-07-25 21:19:13 +02008114 {
Bram Moolenaard812df62008-11-09 12:46:09 +00008115 result = buf->b_fname;
Bram Moolenaar00136dc2018-07-25 21:19:13 +02008116 tilde_file = STRCMP(result, "~") == 0;
8117 }
Bram Moolenaard812df62008-11-09 12:46:09 +00008118 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008119 break;
8120
8121#ifdef FEAT_SEARCHPATH
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008122 case SPEC_CFILE: // file name under cursor
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008123 result = file_name_at_cursor(FNAME_MESS|FNAME_HYP, 1L, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008124 if (result == NULL)
8125 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008126 *errormsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +00008127 return NULL;
8128 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008129 resultbuf = result; // remember allocated string
Bram Moolenaar071d4272004-06-13 20:20:40 +00008130 break;
8131#endif
8132
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008133 case SPEC_AFILE: // file name for autocommand
Bram Moolenaar071d4272004-06-13 20:20:40 +00008134 result = autocmd_fname;
Bram Moolenaarf6dad432008-09-18 19:29:58 +00008135 if (result != NULL && !autocmd_fname_full)
8136 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008137 // Still need to turn the fname into a full path. It is
8138 // postponed to avoid a delay when <afile> is not used.
Bram Moolenaarf6dad432008-09-18 19:29:58 +00008139 autocmd_fname_full = TRUE;
8140 result = FullName_save(autocmd_fname, FALSE);
8141 vim_free(autocmd_fname);
8142 autocmd_fname = result;
8143 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008144 if (result == NULL)
8145 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008146 *errormsg = _("E495: no autocommand file name to substitute for \"<afile>\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008147 return NULL;
8148 }
Bram Moolenaara0174af2008-01-02 20:08:25 +00008149 result = shorten_fname1(result);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008150 break;
8151
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008152 case SPEC_ABUF: // buffer number for autocommand
Bram Moolenaar071d4272004-06-13 20:20:40 +00008153 if (autocmd_bufnr <= 0)
8154 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008155 *errormsg = _("E496: no autocommand buffer number to substitute for \"<abuf>\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008156 return NULL;
8157 }
8158 sprintf((char *)strbuf, "%d", autocmd_bufnr);
8159 result = strbuf;
8160 break;
8161
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008162 case SPEC_AMATCH: // match name for autocommand
Bram Moolenaar071d4272004-06-13 20:20:40 +00008163 result = autocmd_match;
8164 if (result == NULL)
8165 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008166 *errormsg = _("E497: no autocommand match name to substitute for \"<amatch>\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008167 return NULL;
8168 }
8169 break;
8170
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008171 case SPEC_SFILE: // file name for ":so" command
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01008172 result = estack_sfile();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008173 if (result == NULL)
8174 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008175 *errormsg = _("E498: no :source file name to substitute for \"<sfile>\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008176 return NULL;
8177 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01008178 resultbuf = result; // remember allocated string
Bram Moolenaar071d4272004-06-13 20:20:40 +00008179 break;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008180
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008181 case SPEC_SLNUM: // line in file for ":so" command
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01008182 if (SOURCING_NAME == NULL || SOURCING_LNUM == 0)
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01008183 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008184 *errormsg = _("E842: no line number to use for \"<slnum>\"");
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01008185 return NULL;
8186 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01008187 sprintf((char *)strbuf, "%ld", SOURCING_LNUM);
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01008188 result = strbuf;
8189 break;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008190
8191#ifdef FEAT_EVAL
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008192 case SPEC_SFLNUM: // line in script file
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01008193 if (current_sctx.sc_lnum + SOURCING_LNUM == 0)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008194 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008195 *errormsg = _("E961: no line number to use for \"<sflnum>\"");
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008196 return NULL;
8197 }
8198 sprintf((char *)strbuf, "%ld",
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01008199 (long)(current_sctx.sc_lnum + SOURCING_LNUM));
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008200 result = strbuf;
8201 break;
8202#endif
8203
8204#ifdef FEAT_CLIENTSERVER
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008205 case SPEC_CLIENT: // Source of last submitted input
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008206 sprintf((char *)strbuf, PRINTF_HEX_LONG_U,
8207 (long_u)clientWindow);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008208 result = strbuf;
8209 break;
8210#endif
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008211
Bram Moolenaar9e0f6ec2017-05-16 09:36:54 +02008212 default:
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008213 result = (char_u *)""; // avoid gcc warning
Bram Moolenaar9e0f6ec2017-05-16 09:36:54 +02008214 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008215 }
8216
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008217 resultlen = (int)STRLEN(result); // length of new string
8218 if (src[*usedlen] == '<') // remove the file name extension
Bram Moolenaar071d4272004-06-13 20:20:40 +00008219 {
8220 ++*usedlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008221 if ((s = vim_strrchr(result, '.')) != NULL && s >= gettail(result))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008222 resultlen = (int)(s - result);
8223 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008224 else if (!skip_mod)
8225 {
Bram Moolenaar00136dc2018-07-25 21:19:13 +02008226 valid |= modify_fname(src, tilde_file, usedlen, &result, &resultbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008227 &resultlen);
8228 if (result == NULL)
8229 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008230 *errormsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +00008231 return NULL;
8232 }
8233 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008234 }
8235
8236 if (resultlen == 0 || valid != VALID_HEAD + VALID_PATH)
8237 {
8238 if (valid != VALID_HEAD + VALID_PATH)
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008239 // xgettext:no-c-format
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008240 *errormsg = _("E499: Empty file name for '%' or '#', only works with \":p:h\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008241 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008242 *errormsg = _("E500: Evaluates to an empty string");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008243 result = NULL;
8244 }
8245 else
8246 result = vim_strnsave(result, resultlen);
8247 vim_free(resultbuf);
8248 return result;
8249}
8250
8251/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008252 * Expand the <sfile> string in "arg".
8253 *
8254 * Returns an allocated string, or NULL for any error.
8255 */
8256 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008257expand_sfile(char_u *arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008258{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008259 char *errormsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008260 int len;
8261 char_u *result;
8262 char_u *newres;
8263 char_u *repl;
8264 int srclen;
8265 char_u *p;
8266
8267 result = vim_strsave(arg);
8268 if (result == NULL)
8269 return NULL;
8270
8271 for (p = result; *p; )
8272 {
8273 if (STRNCMP(p, "<sfile>", 7) != 0)
8274 ++p;
8275 else
8276 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008277 // replace "<sfile>" with the sourced file name, and do ":" stuff
Bram Moolenaar63b92542007-03-27 14:57:09 +00008278 repl = eval_vars(p, result, &srclen, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008279 if (errormsg != NULL)
8280 {
8281 if (*errormsg)
8282 emsg(errormsg);
8283 vim_free(result);
8284 return NULL;
8285 }
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008286 if (repl == NULL) // no match (cannot happen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008287 {
8288 p += srclen;
8289 continue;
8290 }
8291 len = (int)STRLEN(result) - srclen + (int)STRLEN(repl) + 1;
8292 newres = alloc(len);
8293 if (newres == NULL)
8294 {
8295 vim_free(repl);
8296 vim_free(result);
8297 return NULL;
8298 }
8299 mch_memmove(newres, result, (size_t)(p - result));
8300 STRCPY(newres + (p - result), repl);
8301 len = (int)STRLEN(newres);
8302 STRCAT(newres, p + srclen);
8303 vim_free(repl);
8304 vim_free(result);
8305 result = newres;
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008306 p = newres + len; // continue after the match
Bram Moolenaar071d4272004-06-13 20:20:40 +00008307 }
8308 }
8309
8310 return result;
8311}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008312
Bram Moolenaar071d4272004-06-13 20:20:40 +00008313#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG) || defined(PROTO)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00008314/*
Bram Moolenaard9462e32011-04-11 21:35:11 +02008315 * Make a dialog message in "buff[DIALOG_MSG_SIZE]".
Bram Moolenaarb765d632005-06-07 21:00:02 +00008316 * "format" must contain "%s".
Bram Moolenaar9c13b352005-05-19 20:53:52 +00008317 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008318 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008319dialog_msg(char_u *buff, char *format, char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008320{
Bram Moolenaar071d4272004-06-13 20:20:40 +00008321 if (fname == NULL)
8322 fname = (char_u *)_("Untitled");
Bram Moolenaard9462e32011-04-11 21:35:11 +02008323 vim_snprintf((char *)buff, DIALOG_MSG_SIZE, format, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008324}
8325#endif
8326
8327/*
8328 * ":behave {mswin,xterm}"
8329 */
8330 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008331ex_behave(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008332{
8333 if (STRCMP(eap->arg, "mswin") == 0)
8334 {
8335 set_option_value((char_u *)"selection", 0L, (char_u *)"exclusive", 0);
8336 set_option_value((char_u *)"selectmode", 0L, (char_u *)"mouse,key", 0);
8337 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"popup", 0);
8338 set_option_value((char_u *)"keymodel", 0L,
8339 (char_u *)"startsel,stopsel", 0);
8340 }
8341 else if (STRCMP(eap->arg, "xterm") == 0)
8342 {
8343 set_option_value((char_u *)"selection", 0L, (char_u *)"inclusive", 0);
8344 set_option_value((char_u *)"selectmode", 0L, (char_u *)"", 0);
8345 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"extend", 0);
8346 set_option_value((char_u *)"keymodel", 0L, (char_u *)"", 0);
8347 }
8348 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008349 semsg(_(e_invarg2), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008350}
8351
Bram Moolenaar071d4272004-06-13 20:20:40 +00008352static int filetype_detect = FALSE;
8353static int filetype_plugin = FALSE;
8354static int filetype_indent = FALSE;
8355
8356/*
8357 * ":filetype [plugin] [indent] {on,off,detect}"
8358 * on: Load the filetype.vim file to install autocommands for file types.
8359 * off: Load the ftoff.vim file to remove all autocommands for file types.
8360 * plugin on: load filetype.vim and ftplugin.vim
8361 * plugin off: load ftplugof.vim
8362 * indent on: load filetype.vim and indent.vim
8363 * indent off: load indoff.vim
8364 */
8365 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008366ex_filetype(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008367{
8368 char_u *arg = eap->arg;
8369 int plugin = FALSE;
8370 int indent = FALSE;
8371
8372 if (*eap->arg == NUL)
8373 {
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008374 // Print current status.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008375 smsg("filetype detection:%s plugin:%s indent:%s",
Bram Moolenaar071d4272004-06-13 20:20:40 +00008376 filetype_detect ? "ON" : "OFF",
8377 filetype_plugin ? (filetype_detect ? "ON" : "(on)") : "OFF",
8378 filetype_indent ? (filetype_detect ? "ON" : "(on)") : "OFF");
8379 return;
8380 }
8381
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008382 // Accept "plugin" and "indent" in any order.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008383 for (;;)
8384 {
8385 if (STRNCMP(arg, "plugin", 6) == 0)
8386 {
8387 plugin = TRUE;
8388 arg = skipwhite(arg + 6);
8389 continue;
8390 }
8391 if (STRNCMP(arg, "indent", 6) == 0)
8392 {
8393 indent = TRUE;
8394 arg = skipwhite(arg + 6);
8395 continue;
8396 }
8397 break;
8398 }
8399 if (STRCMP(arg, "on") == 0 || STRCMP(arg, "detect") == 0)
8400 {
8401 if (*arg == 'o' || !filetype_detect)
8402 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01008403 source_runtime((char_u *)FILETYPE_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008404 filetype_detect = TRUE;
8405 if (plugin)
8406 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01008407 source_runtime((char_u *)FTPLUGIN_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008408 filetype_plugin = TRUE;
8409 }
8410 if (indent)
8411 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01008412 source_runtime((char_u *)INDENT_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008413 filetype_indent = TRUE;
8414 }
8415 }
8416 if (*arg == 'd')
8417 {
Bram Moolenaar1610d052016-06-09 22:53:01 +02008418 (void)do_doautocmd((char_u *)"filetypedetect BufRead", TRUE, NULL);
Bram Moolenaara3227e22006-03-08 21:32:40 +00008419 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008420 }
8421 }
8422 else if (STRCMP(arg, "off") == 0)
8423 {
8424 if (plugin || indent)
8425 {
8426 if (plugin)
8427 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01008428 source_runtime((char_u *)FTPLUGOF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008429 filetype_plugin = FALSE;
8430 }
8431 if (indent)
8432 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01008433 source_runtime((char_u *)INDOFF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008434 filetype_indent = FALSE;
8435 }
8436 }
8437 else
8438 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01008439 source_runtime((char_u *)FTOFF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008440 filetype_detect = FALSE;
8441 }
8442 }
8443 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008444 semsg(_(e_invarg2), arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008445}
8446
8447/*
Bram Moolenaar3e545692017-06-04 19:00:32 +02008448 * ":setfiletype [FALLBACK] {name}"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008449 */
8450 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008451ex_setfiletype(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008452{
8453 if (!did_filetype)
Bram Moolenaar3e545692017-06-04 19:00:32 +02008454 {
8455 char_u *arg = eap->arg;
8456
8457 if (STRNCMP(arg, "FALLBACK ", 9) == 0)
8458 arg += 9;
8459
8460 set_option_value((char_u *)"filetype", 0L, arg, OPT_LOCAL);
8461 if (arg != eap->arg)
8462 did_filetype = FALSE;
8463 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008464}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008465
Bram Moolenaar071d4272004-06-13 20:20:40 +00008466 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008467ex_digraphs(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008468{
8469#ifdef FEAT_DIGRAPHS
8470 if (*eap->arg != NUL)
8471 putdigraph(eap->arg);
8472 else
Bram Moolenaareae8ae12018-12-14 18:53:02 +01008473 listdigraphs(eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008474#else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008475 emsg(_("E196: No digraphs in this version"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008476#endif
8477}
8478
8479 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008480ex_set(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008481{
8482 int flags = 0;
8483
8484 if (eap->cmdidx == CMD_setlocal)
8485 flags = OPT_LOCAL;
8486 else if (eap->cmdidx == CMD_setglobal)
8487 flags = OPT_GLOBAL;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01008488#if defined(FEAT_EVAL) && defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008489 if (cmdmod.browse && flags == 0)
8490 ex_options(eap);
8491 else
8492#endif
8493 (void)do_set(eap->arg, flags);
8494}
8495
Bram Moolenaar451fc7b2018-04-27 22:53:07 +02008496#if defined(FEAT_SEARCH_EXTRA) || defined(PROTO)
8497 void
8498set_no_hlsearch(int flag)
8499{
8500 no_hlsearch = flag;
8501# ifdef FEAT_EVAL
8502 set_vim_var_nr(VV_HLSEARCH, !no_hlsearch && p_hls);
8503# endif
8504}
8505
Bram Moolenaar071d4272004-06-13 20:20:40 +00008506/*
8507 * ":nohlsearch"
8508 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008509 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008510ex_nohlsearch(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008511{
Bram Moolenaar451fc7b2018-04-27 22:53:07 +02008512 set_no_hlsearch(TRUE);
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00008513 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008514}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008515#endif
8516
8517#ifdef FEAT_CRYPT
8518/*
8519 * ":X": Get crypt key
8520 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008521 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008522ex_X(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008523{
Bram Moolenaar3a0c9082014-11-12 15:15:42 +01008524 crypt_check_current_method();
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02008525 (void)crypt_get_key(TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008526}
8527#endif
8528
8529#ifdef FEAT_FOLDING
8530 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008531ex_fold(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008532{
8533 if (foldManualAllowed(TRUE))
8534 foldCreate(eap->line1, eap->line2);
8535}
8536
8537 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008538ex_foldopen(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008539{
8540 opFoldRange(eap->line1, eap->line2, eap->cmdidx == CMD_foldopen,
8541 eap->forceit, FALSE);
8542}
8543
8544 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008545ex_folddo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008546{
8547 linenr_T lnum;
8548
Bram Moolenaar4facea32019-10-12 20:17:40 +02008549# ifdef FEAT_CLIPBOARD
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02008550 start_global_changes();
Bram Moolenaar4facea32019-10-12 20:17:40 +02008551# endif
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02008552
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008553 // First set the marks for all lines closed/open.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008554 for (lnum = eap->line1; lnum <= eap->line2; ++lnum)
8555 if (hasFolding(lnum, NULL, NULL) == (eap->cmdidx == CMD_folddoclosed))
8556 ml_setmarked(lnum);
8557
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008558 // Execute the command on the marked lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008559 global_exe(eap->arg);
Bram Moolenaar217e1b82019-12-01 21:41:28 +01008560 ml_clearmarked(); // clear rest of the marks
Bram Moolenaar4facea32019-10-12 20:17:40 +02008561# ifdef FEAT_CLIPBOARD
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02008562 end_global_changes();
Bram Moolenaar4facea32019-10-12 20:17:40 +02008563# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008564}
8565#endif
Bram Moolenaarf5291f32017-09-14 22:55:37 +02008566
Bram Moolenaar39665952018-08-15 20:59:48 +02008567#ifdef FEAT_QUICKFIX
8568/*
8569 * Returns TRUE if the supplied Ex cmdidx is for a location list command
8570 * instead of a quickfix command.
8571 */
8572 int
8573is_loclist_cmd(int cmdidx)
8574{
Bram Moolenaar74c8be22018-08-23 22:51:40 +02008575 if (cmdidx < 0 || cmdidx >= CMD_SIZE)
Bram Moolenaar39665952018-08-15 20:59:48 +02008576 return FALSE;
8577 return cmdnames[cmdidx].cmd_name[0] == 'l';
8578}
8579#endif
8580
Bram Moolenaar4facea32019-10-12 20:17:40 +02008581#if defined(FEAT_TIMERS) || defined(PROTO)
Bram Moolenaarf5291f32017-09-14 22:55:37 +02008582 int
8583get_pressedreturn(void)
8584{
8585 return ex_pressedreturn;
8586}
8587
8588 void
8589set_pressedreturn(int val)
8590{
8591 ex_pressedreturn = val;
8592}
8593#endif