blob: 385cf3b4ae279b7007b01d5e57148547ebecfe47 [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 Moolenaare96a2492019-06-25 04:12:16 +020023static char_u *do_one_cmd(char_u **, int, struct condstack *, 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 Moolenaar071d4272004-06-13 20:20:40 +000026static int if_level = 0; /* depth in :if */
27#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 Moolenaar682725c2019-05-25 20:10:37 +0200371#if !defined(FEAT_TEXT_PROP)
372# 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 Moolenaarb32ce2d2005-01-05 22:07:01 +0000386/* Struct for storing a line inside a while/for loop */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000387typedef struct
388{
389 char_u *line; /* command line */
390 linenr_T lnum; /* sourcing_lnum of the line */
391} 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{
400 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
412/* Struct to save a few things while debugging. Used in do_cmdline() only. */
413struct 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
437 /* Necessary for debugging an inactive ":catch", ":finally", ":endtry" */
438 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(
470 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
483 /* When using ":global /pat/ visual" and then "Q" we return to continue
484 * the :global command. */
485 if (global_busy)
486 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000487
488 save_msg_scroll = msg_scroll;
489 ++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
492 /* Ignore scrollbar and mouse events in Ex mode */
493 ++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 Moolenaar7c626922005-02-07 22:01:03 +0000499 /* Check for a ":normal" command and no more characters left. */
500 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 {
530 /* go up one line, to overwrite the ":<CR>" line, so the
Bram Moolenaar81870892007-11-11 18:17:28 +0000531 * 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 Moolenaardf177f62005-02-22 08:39:57 +0000541 else if (ex_pressedreturn && !ex_no_reprint) /* must be at EOF */
542 {
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 Moolenaar78c0b7d2016-01-30 15:52:46 +0100614 void *cookie, /* argument for fgetline() */
615 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000616{
617 char_u *next_cmdline; /* next cmd to execute */
618 char_u *cmdline_copy = NULL; /* copy of cmd line */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100619 int used_getline = FALSE; /* used "fgetline" to obtain command */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000620 static int recursive = 0; /* recursive depth */
621 int msg_didout_before_start = 0;
622 int count = 0; /* line number count */
623 int did_inc = FALSE; /* incremented RedrawingDisabled */
624 int retval = OK;
625#ifdef FEAT_EVAL
626 struct condstack cstack; /* conditional stack */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000627 garray_T lines_ga; /* keep lines for ":while"/":for" */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000628 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 */
Bram Moolenaared203462004-06-16 11:19:22 +0000632 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 Moolenaarbf55e142010-11-16 11:32:01 +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
647 static int call_depth = 0; /* recursiveness */
648
649#ifdef FEAT_EVAL
650 /* For every pair of do_cmdline()/do_one_cmd() calls, use an extra memory
651 * location for storing error messages to be converted to an exception.
Bram Moolenaarcf3630f2005-01-08 16:04:29 +0000652 * This ensures that the do_errthrow() call in do_one_cmd() does not
653 * combine the messages stored by an earlier invocation of do_one_cmd()
654 * with the command name of the later one. This would happen when
655 * BufWritePost autocommands are executed after a write error. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000656 saved_msg_list = msg_list;
657 msg_list = &private_msg_list;
658 private_msg_list = NULL;
659#endif
660
661 /* It's possible to create an endless loop with ":execute", catch that
Bram Moolenaar777b30f2017-01-02 15:26:27 +0100662 * here. The value of 200 allows nested function calls, ":source", etc.
663 * Allow 200 or 'maxfuncdepth', whatever is larger. */
Bram Moolenaarb094ff42017-01-02 16:16:39 +0100664 if (call_depth >= 200
665#ifdef FEAT_EVAL
666 && call_depth >= p_mfd
667#endif
668 )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000669 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100670 emsg(_("E169: Command too recursive"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000671#ifdef FEAT_EVAL
672 /* When converting to an exception, we do not include the command name
673 * since this is not an error of the specific command. */
674 do_errthrow((struct condstack *)NULL, (char_u *)NULL);
675 msg_list = saved_msg_list;
676#endif
677 return FAIL;
678 }
679 ++call_depth;
680
681#ifdef FEAT_EVAL
682 cstack.cs_idx = -1;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000683 cstack.cs_looplevel = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000684 cstack.cs_trylevel = 0;
685 cstack.cs_emsg_silent_list = NULL;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000686 cstack.cs_lflags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000687 ga_init2(&lines_ga, (int)sizeof(wcmd_T), 10);
688
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100689 real_cookie = getline_cookie(fgetline, cookie);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000690
691 /* Inside a function use a higher nesting level. */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100692 getline_is_func = getline_equal(fgetline, cookie, get_func_line);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000693 if (getline_is_func && ex_nesting_level == func_level(real_cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000694 ++ex_nesting_level;
695
696 /* Get the function or script name and the address where the next breakpoint
697 * line and the debug tick for a function or script are stored. */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000698 if (getline_is_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000699 {
700 fname = func_name(real_cookie);
701 breakpoint = func_breakpoint(real_cookie);
702 dbg_tick = func_dbg_tick(real_cookie);
703 }
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100704 else if (getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000705 {
706 fname = sourcing_name;
707 breakpoint = source_breakpoint(real_cookie);
708 dbg_tick = source_dbg_tick(real_cookie);
709 }
710
711 /*
712 * Initialize "force_abort" and "suppress_errthrow" at the top level.
713 */
714 if (!recursive)
715 {
716 force_abort = FALSE;
717 suppress_errthrow = FALSE;
718 }
719
720 /*
721 * If requested, store and reset the global values controlling the
Bram Moolenaar89d40322006-08-29 15:30:07 +0000722 * exception handling (used when debugging). Otherwise clear it to avoid
723 * a bogus compiler warning when the optimizer uses inline functions...
Bram Moolenaar071d4272004-06-13 20:20:40 +0000724 */
Bram Moolenaarb4872942006-05-13 10:32:52 +0000725 if (flags & DOCMD_EXCRESET)
Bram Moolenaared203462004-06-16 11:19:22 +0000726 save_dbg_stuff(&debug_saved);
Bram Moolenaar89d40322006-08-29 15:30:07 +0000727 else
Bram Moolenaar69b67f72015-09-25 16:59:47 +0200728 vim_memset(&debug_saved, 0, sizeof(debug_saved));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000729
730 initial_trylevel = trylevel;
731
732 /*
733 * "did_throw" will be set to TRUE when an exception is being thrown.
734 */
735 did_throw = FALSE;
736#endif
737 /*
738 * "did_emsg" will be set to TRUE when emsg() is used, in which case we
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000739 * cancel the whole command line, and any if/endif or loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000740 * If force_abort is set, we cancel everything.
741 */
742 did_emsg = FALSE;
743
744 /*
745 * KeyTyped is only set when calling vgetc(). Reset it here when not
746 * calling vgetc() (sourced command lines).
747 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100748 if (!(flags & DOCMD_KEYTYPED)
749 && !getline_equal(fgetline, cookie, getexline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000750 KeyTyped = FALSE;
751
752 /*
753 * Continue executing command lines:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000754 * - when inside an ":if", ":while" or ":for"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000755 * - for multiple commands on one line, separated with '|'
756 * - when repeating until there are no more lines (for ":source")
757 */
758 next_cmdline = cmdline;
759 do
760 {
Bram Moolenaar05159a02005-02-26 23:04:13 +0000761#ifdef FEAT_EVAL
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100762 getline_is_func = getline_equal(fgetline, cookie, get_func_line);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000763#endif
764
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000765 /* stop skipping cmds for an error msg after all endif/while/for */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000766 if (next_cmdline == NULL
767#ifdef FEAT_EVAL
768 && !force_abort
769 && cstack.cs_idx < 0
Bram Moolenaar05159a02005-02-26 23:04:13 +0000770 && !(getline_is_func && func_has_abort(real_cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000771#endif
772 )
773 did_emsg = FALSE;
774
775 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000776 * 1. If repeating a line in a loop, get a line from lines_ga.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100777 * 2. If no line given: Get an allocated line with fgetline().
Bram Moolenaar071d4272004-06-13 20:20:40 +0000778 * 3. If a line is given: Make a copy, so we can mess with it.
779 */
780
781#ifdef FEAT_EVAL
782 /* 1. If repeating, get a previous line from lines_ga. */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000783 if (cstack.cs_looplevel > 0 && current_line < lines_ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000784 {
785 /* Each '|' separated command is stored separately in lines_ga, to
786 * be able to jump to it. Don't use next_cmdline now. */
Bram Moolenaard23a8232018-02-10 18:45:26 +0100787 VIM_CLEAR(cmdline_copy);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000788
789 /* Check if a function has returned or, unless it has an unclosed
790 * try conditional, aborted. */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000791 if (getline_is_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000792 {
Bram Moolenaar05159a02005-02-26 23:04:13 +0000793# ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000794 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +0000795 func_line_end(real_cookie);
796# endif
797 if (func_has_ended(real_cookie))
798 {
799 retval = FAIL;
800 break;
801 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000802 }
Bram Moolenaar05159a02005-02-26 23:04:13 +0000803#ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000804 else if (do_profiling == PROF_YES
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100805 && getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar05159a02005-02-26 23:04:13 +0000806 script_line_end();
807#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000808
809 /* Check if a sourced file hit a ":finish" command. */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100810 if (source_finished(fgetline, cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000811 {
812 retval = FAIL;
813 break;
814 }
815
816 /* If breakpoints have been added/deleted need to check for it. */
817 if (breakpoint != NULL && dbg_tick != NULL
818 && *dbg_tick != debug_tick)
819 {
820 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100821 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar071d4272004-06-13 20:20:40 +0000822 fname, sourcing_lnum);
823 *dbg_tick = debug_tick;
824 }
825
826 next_cmdline = ((wcmd_T *)(lines_ga.ga_data))[current_line].line;
827 sourcing_lnum = ((wcmd_T *)(lines_ga.ga_data))[current_line].lnum;
828
829 /* Did we encounter a breakpoint? */
830 if (breakpoint != NULL && *breakpoint != 0
831 && *breakpoint <= sourcing_lnum)
832 {
833 dbg_breakpoint(fname, sourcing_lnum);
834 /* Find next breakpoint. */
835 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100836 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar071d4272004-06-13 20:20:40 +0000837 fname, sourcing_lnum);
838 *dbg_tick = debug_tick;
839 }
Bram Moolenaar05159a02005-02-26 23:04:13 +0000840# ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000841 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +0000842 {
843 if (getline_is_func)
844 func_line_start(real_cookie);
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100845 else if (getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar05159a02005-02-26 23:04:13 +0000846 script_line_start();
847 }
848# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000849 }
850
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000851 if (cstack.cs_looplevel > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000852 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000853 /* Inside a while/for loop we need to store the lines and use them
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100854 * again. Pass a different "fgetline" function to do_one_cmd()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000855 * below, so that it stores lines in or reads them from
856 * "lines_ga". Makes it possible to define a function inside a
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000857 * while/for loop. */
858 cmd_getline = get_loop_line;
859 cmd_cookie = (void *)&cmd_loop_cookie;
860 cmd_loop_cookie.lines_gap = &lines_ga;
861 cmd_loop_cookie.current_line = current_line;
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100862 cmd_loop_cookie.getline = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000863 cmd_loop_cookie.cookie = cookie;
864 cmd_loop_cookie.repeating = (current_line < lines_ga.ga_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000865 }
866 else
867 {
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100868 cmd_getline = fgetline;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000869 cmd_cookie = cookie;
870 }
871#endif
872
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100873 /* 2. If no line given, get an allocated line with fgetline(). */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000874 if (next_cmdline == NULL)
875 {
876 /*
877 * Need to set msg_didout for the first line after an ":if",
878 * otherwise the ":if" will be overwritten.
879 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100880 if (count == 1 && getline_equal(fgetline, cookie, getexline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000881 msg_didout = TRUE;
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100882 if (fgetline == NULL || (next_cmdline = fgetline(':', cookie,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000883#ifdef FEAT_EVAL
884 cstack.cs_idx < 0 ? 0 : (cstack.cs_idx + 1) * 2
885#else
886 0
887#endif
Bram Moolenaare96a2492019-06-25 04:12:16 +0200888 , TRUE)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000889 {
890 /* Don't call wait_return for aborted command line. The NULL
891 * returned for the end of a sourced file or executed function
892 * doesn't do this. */
893 if (KeyTyped && !(flags & DOCMD_REPEAT))
894 need_wait_return = FALSE;
895 retval = FAIL;
896 break;
897 }
898 used_getline = TRUE;
899
900 /*
901 * Keep the first typed line. Clear it when more lines are typed.
902 */
903 if (flags & DOCMD_KEEPLINE)
904 {
905 vim_free(repeat_cmdline);
906 if (count == 0)
907 repeat_cmdline = vim_strsave(next_cmdline);
908 else
909 repeat_cmdline = NULL;
910 }
911 }
912
913 /* 3. Make a copy of the command so we can mess with it. */
914 else if (cmdline_copy == NULL)
915 {
916 next_cmdline = vim_strsave(next_cmdline);
917 if (next_cmdline == NULL)
918 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100919 emsg(_(e_outofmem));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000920 retval = FAIL;
921 break;
922 }
923 }
924 cmdline_copy = next_cmdline;
925
926#ifdef FEAT_EVAL
927 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000928 * Save the current line when inside a ":while" or ":for", and when
929 * the command looks like a ":while" or ":for", because we may need it
930 * later. When there is a '|' and another command, it is stored
931 * separately, because we need to be able to jump back to it from an
932 * :endwhile/:endfor.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000933 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000934 if (current_line == lines_ga.ga_len
935 && (cstack.cs_looplevel || has_loop_cmd(next_cmdline)))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000936 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000937 if (store_loop_line(&lines_ga, next_cmdline) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000938 {
939 retval = FAIL;
940 break;
941 }
942 }
943 did_endif = FALSE;
944#endif
945
946 if (count++ == 0)
947 {
948 /*
949 * All output from the commands is put below each other, without
950 * waiting for a return. Don't do this when executing commands
951 * from a script or when being called recursive (e.g. for ":e
952 * +command file").
953 */
954 if (!(flags & DOCMD_NOWAIT) && !recursive)
955 {
956 msg_didout_before_start = msg_didout;
957 msg_didany = FALSE; /* no output yet */
958 msg_start();
959 msg_scroll = TRUE; /* put messages below each other */
Bram Moolenaar84a05ac2013-05-06 04:24:17 +0200960 ++no_wait_return; /* don't wait for return until finished */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000961 ++RedrawingDisabled;
962 did_inc = TRUE;
963 }
964 }
965
966 if (p_verbose >= 15 && sourcing_name != NULL)
Bram Moolenaar4facea32019-10-12 20:17:40 +0200967 msg_verbose_cmd(sourcing_lnum, cmdline_copy);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000968
969 /*
970 * 2. Execute one '|' separated command.
971 * do_one_cmd() will return NULL if there is no trailing '|'.
972 * "cmdline_copy" can change, e.g. for '%' and '#' expansion.
973 */
974 ++recursive;
975 next_cmdline = do_one_cmd(&cmdline_copy, flags & DOCMD_VERBOSE,
976#ifdef FEAT_EVAL
977 &cstack,
978#endif
979 cmd_getline, cmd_cookie);
980 --recursive;
981
982#ifdef FEAT_EVAL
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000983 if (cmd_cookie == (void *)&cmd_loop_cookie)
984 /* Use "current_line" from "cmd_loop_cookie", it may have been
Bram Moolenaar071d4272004-06-13 20:20:40 +0000985 * incremented when defining a function. */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000986 current_line = cmd_loop_cookie.current_line;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000987#endif
988
989 if (next_cmdline == NULL)
990 {
Bram Moolenaard23a8232018-02-10 18:45:26 +0100991 VIM_CLEAR(cmdline_copy);
Bram Moolenaard7663c22019-08-06 21:59:57 +0200992
Bram Moolenaar071d4272004-06-13 20:20:40 +0000993 /*
994 * If the command was typed, remember it for the ':' register.
995 * Do this AFTER executing the command to make :@: work.
996 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100997 if (getline_equal(fgetline, cookie, getexline)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000998 && new_last_cmdline != NULL)
999 {
1000 vim_free(last_cmdline);
1001 last_cmdline = new_last_cmdline;
1002 new_last_cmdline = NULL;
1003 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001004 }
1005 else
1006 {
1007 /* need to copy the command after the '|' to cmdline_copy, for the
1008 * next do_one_cmd() */
Bram Moolenaara7241f52008-06-24 20:39:31 +00001009 STRMOVE(cmdline_copy, next_cmdline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001010 next_cmdline = cmdline_copy;
1011 }
1012
1013
1014#ifdef FEAT_EVAL
1015 /* reset did_emsg for a function that is not aborted by an error */
1016 if (did_emsg && !force_abort
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001017 && getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001018 && !func_has_abort(real_cookie))
1019 did_emsg = FALSE;
1020
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001021 if (cstack.cs_looplevel > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001022 {
1023 ++current_line;
1024
1025 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001026 * An ":endwhile", ":endfor" and ":continue" is handled here.
1027 * If we were executing commands, jump back to the ":while" or
1028 * ":for".
1029 * If we were not executing commands, decrement cs_looplevel.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001030 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001031 if (cstack.cs_lflags & (CSL_HAD_CONT | CSL_HAD_ENDLOOP))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001032 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001033 cstack.cs_lflags &= ~(CSL_HAD_CONT | CSL_HAD_ENDLOOP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001034
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001035 /* Jump back to the matching ":while" or ":for". Be careful
1036 * not to use a cs_line[] from an entry that isn't a ":while"
1037 * or ":for": It would make "current_line" invalid and can
1038 * cause a crash. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001039 if (!did_emsg && !got_int && !did_throw
1040 && cstack.cs_idx >= 0
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001041 && (cstack.cs_flags[cstack.cs_idx]
1042 & (CSF_WHILE | CSF_FOR))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001043 && cstack.cs_line[cstack.cs_idx] >= 0
1044 && (cstack.cs_flags[cstack.cs_idx] & CSF_ACTIVE))
1045 {
1046 current_line = cstack.cs_line[cstack.cs_idx];
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001047 /* remember we jumped there */
1048 cstack.cs_lflags |= CSL_HAD_LOOP;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001049 line_breakcheck(); /* check if CTRL-C typed */
1050
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001051 /* Check for the next breakpoint at or after the ":while"
1052 * or ":for". */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001053 if (breakpoint != NULL)
1054 {
1055 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001056 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001057 fname,
1058 ((wcmd_T *)lines_ga.ga_data)[current_line].lnum-1);
1059 *dbg_tick = debug_tick;
1060 }
1061 }
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001062 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001063 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001064 /* can only get here with ":endwhile" or ":endfor" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001065 if (cstack.cs_idx >= 0)
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001066 rewind_conditionals(&cstack, cstack.cs_idx - 1,
1067 CSF_WHILE | CSF_FOR, &cstack.cs_looplevel);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001068 }
1069 }
1070
1071 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001072 * For a ":while" or ":for" we need to remember the line number.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001073 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001074 else if (cstack.cs_lflags & CSL_HAD_LOOP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001075 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001076 cstack.cs_lflags &= ~CSL_HAD_LOOP;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001077 cstack.cs_line[cstack.cs_idx] = current_line - 1;
1078 }
1079 }
1080
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01001081 /* Check for the next breakpoint after a watchexpression */
1082 if (breakpoint != NULL && has_watchexpr())
1083 {
1084 *breakpoint = dbg_find_breakpoint(FALSE, fname, sourcing_lnum);
1085 *dbg_tick = debug_tick;
1086 }
1087
Bram Moolenaar071d4272004-06-13 20:20:40 +00001088 /*
1089 * When not inside any ":while" loop, clear remembered lines.
1090 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001091 if (cstack.cs_looplevel == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001092 {
1093 if (lines_ga.ga_len > 0)
1094 {
1095 sourcing_lnum =
1096 ((wcmd_T *)lines_ga.ga_data)[lines_ga.ga_len - 1].lnum;
1097 free_cmdlines(&lines_ga);
1098 }
1099 current_line = 0;
1100 }
1101
1102 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001103 * A ":finally" makes did_emsg, got_int, and did_throw pending for
1104 * being restored at the ":endtry". Reset them here and set the
1105 * ACTIVE and FINALLY flags, so that the finally clause gets executed.
1106 * This includes the case where a missing ":endif", ":endwhile" or
1107 * ":endfor" was detected by the ":finally" itself.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001108 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001109 if (cstack.cs_lflags & CSL_HAD_FINA)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001110 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001111 cstack.cs_lflags &= ~CSL_HAD_FINA;
1112 report_make_pending(cstack.cs_pending[cstack.cs_idx]
1113 & (CSTP_ERROR | CSTP_INTERRUPT | CSTP_THROW),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001114 did_throw ? (void *)current_exception : NULL);
1115 did_emsg = got_int = did_throw = FALSE;
1116 cstack.cs_flags[cstack.cs_idx] |= CSF_ACTIVE | CSF_FINALLY;
1117 }
1118
1119 /* Update global "trylevel" for recursive calls to do_cmdline() from
1120 * within this loop. */
1121 trylevel = initial_trylevel + cstack.cs_trylevel;
1122
1123 /*
Bram Moolenaarc1762cc2007-05-10 16:56:30 +00001124 * If the outermost try conditional (across function calls and sourced
Bram Moolenaar071d4272004-06-13 20:20:40 +00001125 * files) is aborted because of an error, an interrupt, or an uncaught
1126 * exception, cancel everything. If it is left normally, reset
1127 * force_abort to get the non-EH compatible abortion behavior for
1128 * the rest of the script.
1129 */
1130 if (trylevel == 0 && !did_emsg && !got_int && !did_throw)
1131 force_abort = FALSE;
1132
1133 /* Convert an interrupt to an exception if appropriate. */
1134 (void)do_intthrow(&cstack);
1135#endif /* FEAT_EVAL */
1136
1137 }
1138 /*
1139 * Continue executing command lines when:
1140 * - no CTRL-C typed, no aborting error, no exception thrown or try
1141 * conditionals need to be checked for executing finally clauses or
1142 * catching an interrupt exception
1143 * - didn't get an error message or lines are not typed
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001144 * - there is a command after '|', inside a :if, :while, :for or :try, or
Bram Moolenaar071d4272004-06-13 20:20:40 +00001145 * looping for ":source" command or function call.
1146 */
1147 while (!((got_int
1148#ifdef FEAT_EVAL
1149 || (did_emsg && force_abort) || did_throw
1150#endif
1151 )
1152#ifdef FEAT_EVAL
1153 && cstack.cs_trylevel == 0
1154#endif
1155 )
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001156 && !(did_emsg
1157#ifdef FEAT_EVAL
1158 /* Keep going when inside try/catch, so that the error can be
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02001159 * deal with, except when it is a syntax error, it may cause
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001160 * the :endtry to be missed. */
1161 && (cstack.cs_trylevel == 0 || did_emsg_syntax)
1162#endif
1163 && used_getline
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001164 && (getline_equal(fgetline, cookie, getexmodeline)
1165 || getline_equal(fgetline, cookie, getexline)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001166 && (next_cmdline != NULL
1167#ifdef FEAT_EVAL
1168 || cstack.cs_idx >= 0
1169#endif
1170 || (flags & DOCMD_REPEAT)));
1171
1172 vim_free(cmdline_copy);
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001173 did_emsg_syntax = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001174#ifdef FEAT_EVAL
1175 free_cmdlines(&lines_ga);
1176 ga_clear(&lines_ga);
1177
1178 if (cstack.cs_idx >= 0)
1179 {
1180 /*
1181 * If a sourced file or executed function ran to its end, report the
1182 * unclosed conditional.
1183 */
1184 if (!got_int && !did_throw
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001185 && ((getline_equal(fgetline, cookie, getsourceline)
1186 && !source_finished(fgetline, cookie))
1187 || (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001188 && !func_has_ended(real_cookie))))
1189 {
1190 if (cstack.cs_flags[cstack.cs_idx] & CSF_TRY)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001191 emsg(_(e_endtry));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001192 else if (cstack.cs_flags[cstack.cs_idx] & CSF_WHILE)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001193 emsg(_(e_endwhile));
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001194 else if (cstack.cs_flags[cstack.cs_idx] & CSF_FOR)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001195 emsg(_(e_endfor));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001196 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001197 emsg(_(e_endif));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001198 }
1199
1200 /*
1201 * Reset "trylevel" in case of a ":finish" or ":return" or a missing
1202 * ":endtry" in a sourced file or executed function. If the try
1203 * conditional is in its finally clause, ignore anything pending.
1204 * If it is in a catch clause, finish the caught exception.
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001205 * Also cleanup any "cs_forinfo" structures.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001206 */
1207 do
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001208 {
1209 int idx = cleanup_conditionals(&cstack, 0, TRUE);
1210
Bram Moolenaar89e5d682005-01-17 22:06:23 +00001211 if (idx >= 0)
1212 --idx; /* remove try block not in its finally clause */
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001213 rewind_conditionals(&cstack, idx, CSF_WHILE | CSF_FOR,
1214 &cstack.cs_looplevel);
1215 }
1216 while (cstack.cs_idx >= 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001217 trylevel = initial_trylevel;
1218 }
1219
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001220 /* If a missing ":endtry", ":endwhile", ":endfor", or ":endif" or a memory
1221 * lack was reported above and the error message is to be converted to an
Bram Moolenaar071d4272004-06-13 20:20:40 +00001222 * exception, do this now after rewinding the cstack. */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001223 do_errthrow(&cstack, getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001224 ? (char_u *)"endfunction" : (char_u *)NULL);
1225
1226 if (trylevel == 0)
1227 {
1228 /*
1229 * When an exception is being thrown out of the outermost try
1230 * conditional, discard the uncaught exception, disable the conversion
1231 * of interrupts or errors to exceptions, and ensure that no more
1232 * commands are executed.
1233 */
1234 if (did_throw)
1235 {
1236 void *p = NULL;
1237 char_u *saved_sourcing_name;
1238 int saved_sourcing_lnum;
1239 struct msglist *messages = NULL, *next;
1240
1241 /*
1242 * If the uncaught exception is a user exception, report it as an
1243 * error. If it is an error exception, display the saved error
1244 * message now. For an interrupt exception, do nothing; the
1245 * interrupt message is given elsewhere.
1246 */
1247 switch (current_exception->type)
1248 {
1249 case ET_USER:
Bram Moolenaar9c13b352005-05-19 20:53:52 +00001250 vim_snprintf((char *)IObuff, IOSIZE,
1251 _("E605: Exception not caught: %s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001252 current_exception->value);
1253 p = vim_strsave(IObuff);
1254 break;
1255 case ET_ERROR:
1256 messages = current_exception->messages;
1257 current_exception->messages = NULL;
1258 break;
1259 case ET_INTERRUPT:
1260 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001261 }
1262
1263 saved_sourcing_name = sourcing_name;
1264 saved_sourcing_lnum = sourcing_lnum;
1265 sourcing_name = current_exception->throw_name;
1266 sourcing_lnum = current_exception->throw_lnum;
1267 current_exception->throw_name = NULL;
1268
1269 discard_current_exception(); /* uses IObuff if 'verbose' */
1270 suppress_errthrow = TRUE;
1271 force_abort = TRUE;
1272
1273 if (messages != NULL)
1274 {
1275 do
1276 {
1277 next = messages->next;
1278 emsg(messages->msg);
1279 vim_free(messages->msg);
1280 vim_free(messages);
1281 messages = next;
1282 }
1283 while (messages != NULL);
1284 }
1285 else if (p != NULL)
1286 {
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01001287 emsg(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001288 vim_free(p);
1289 }
1290 vim_free(sourcing_name);
1291 sourcing_name = saved_sourcing_name;
1292 sourcing_lnum = saved_sourcing_lnum;
1293 }
1294
1295 /*
1296 * On an interrupt or an aborting error not converted to an exception,
1297 * disable the conversion of errors to exceptions. (Interrupts are not
1298 * converted any more, here.) This enables also the interrupt message
1299 * when force_abort is set and did_emsg unset in case of an interrupt
1300 * from a finally clause after an error.
1301 */
1302 else if (got_int || (did_emsg && force_abort))
1303 suppress_errthrow = TRUE;
1304 }
1305
1306 /*
1307 * The current cstack will be freed when do_cmdline() returns. An uncaught
1308 * exception will have to be rethrown in the previous cstack. If a function
1309 * has just returned or a script file was just finished and the previous
1310 * cstack belongs to the same function or, respectively, script file, it
1311 * will have to be checked for finally clauses to be executed due to the
1312 * ":return" or ":finish". This is done in do_one_cmd().
1313 */
1314 if (did_throw)
1315 need_rethrow = TRUE;
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001316 if ((getline_equal(fgetline, cookie, getsourceline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001317 && ex_nesting_level > source_level(real_cookie))
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001318 || (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001319 && ex_nesting_level > func_level(real_cookie) + 1))
1320 {
1321 if (!did_throw)
1322 check_cstack = TRUE;
1323 }
1324 else
1325 {
1326 /* When leaving a function, reduce nesting level. */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001327 if (getline_equal(fgetline, cookie, get_func_line))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001328 --ex_nesting_level;
1329 /*
1330 * Go to debug mode when returning from a function in which we are
1331 * single-stepping.
1332 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001333 if ((getline_equal(fgetline, cookie, getsourceline)
1334 || getline_equal(fgetline, cookie, get_func_line))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001335 && ex_nesting_level + 1 <= debug_break_level)
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001336 do_debug(getline_equal(fgetline, cookie, getsourceline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001337 ? (char_u *)_("End of sourced file")
1338 : (char_u *)_("End of function"));
1339 }
1340
1341 /*
1342 * Restore the exception environment (done after returning from the
1343 * debugger).
1344 */
1345 if (flags & DOCMD_EXCRESET)
Bram Moolenaared203462004-06-16 11:19:22 +00001346 restore_dbg_stuff(&debug_saved);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001347
1348 msg_list = saved_msg_list;
1349#endif /* FEAT_EVAL */
1350
1351 /*
1352 * If there was too much output to fit on the command line, ask the user to
1353 * hit return before redrawing the screen. With the ":global" command we do
1354 * this only once after the command is finished.
1355 */
1356 if (did_inc)
1357 {
1358 --RedrawingDisabled;
1359 --no_wait_return;
1360 msg_scroll = FALSE;
1361
1362 /*
1363 * When just finished an ":if"-":else" which was typed, no need to
1364 * wait for hit-return. Also for an error situation.
1365 */
1366 if (retval == FAIL
1367#ifdef FEAT_EVAL
1368 || (did_endif && KeyTyped && !did_emsg)
1369#endif
1370 )
1371 {
1372 need_wait_return = FALSE;
1373 msg_didany = FALSE; /* don't wait when restarting edit */
1374 }
1375 else if (need_wait_return)
1376 {
1377 /*
1378 * The msg_start() above clears msg_didout. The wait_return we do
1379 * here should not overwrite the command that may be shown before
1380 * doing that.
1381 */
1382 msg_didout |= msg_didout_before_start;
1383 wait_return(FALSE);
1384 }
1385 }
1386
Bram Moolenaar2a942252012-11-28 23:03:07 +01001387#ifdef FEAT_EVAL
1388 did_endif = FALSE; /* in case do_cmdline used recursively */
1389#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001390 /*
1391 * Reset if_level, in case a sourced script file contains more ":if" than
1392 * ":endif" (could be ":if x | foo | endif").
1393 */
1394 if_level = 0;
Bram Moolenaar2e18a122012-11-28 19:10:54 +01001395#endif
Bram Moolenaard4ad0d42012-11-28 17:34:48 +01001396
Bram Moolenaar071d4272004-06-13 20:20:40 +00001397 --call_depth;
1398 return retval;
1399}
1400
1401#ifdef FEAT_EVAL
1402/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001403 * Obtain a line when inside a ":while" or ":for" loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001404 */
1405 static char_u *
Bram Moolenaare96a2492019-06-25 04:12:16 +02001406get_loop_line(int c, void *cookie, int indent, int do_concat)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001407{
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001408 struct loop_cookie *cp = (struct loop_cookie *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001409 wcmd_T *wp;
1410 char_u *line;
1411
1412 if (cp->current_line + 1 >= cp->lines_gap->ga_len)
1413 {
1414 if (cp->repeating)
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001415 return NULL; /* trying to read past ":endwhile"/":endfor" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001416
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001417 /* First time inside the ":while"/":for": get line normally. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001418 if (cp->getline == NULL)
Bram Moolenaare96a2492019-06-25 04:12:16 +02001419 line = getcmdline(c, 0L, indent, do_concat);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001420 else
Bram Moolenaare96a2492019-06-25 04:12:16 +02001421 line = cp->getline(c, cp->cookie, indent, do_concat);
Bram Moolenaard68071d2006-05-02 22:08:30 +00001422 if (line != NULL && store_loop_line(cp->lines_gap, line) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001423 ++cp->current_line;
1424
1425 return line;
1426 }
1427
1428 KeyTyped = FALSE;
1429 ++cp->current_line;
1430 wp = (wcmd_T *)(cp->lines_gap->ga_data) + cp->current_line;
1431 sourcing_lnum = wp->lnum;
1432 return vim_strsave(wp->line);
1433}
1434
1435/*
1436 * Store a line in "gap" so that a ":while" loop can execute it again.
1437 */
1438 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001439store_loop_line(garray_T *gap, char_u *line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001440{
1441 if (ga_grow(gap, 1) == FAIL)
1442 return FAIL;
1443 ((wcmd_T *)(gap->ga_data))[gap->ga_len].line = vim_strsave(line);
1444 ((wcmd_T *)(gap->ga_data))[gap->ga_len].lnum = sourcing_lnum;
1445 ++gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001446 return OK;
1447}
1448
1449/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001450 * Free the lines stored for a ":while" or ":for" loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001451 */
1452 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001453free_cmdlines(garray_T *gap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001454{
1455 while (gap->ga_len > 0)
1456 {
1457 vim_free(((wcmd_T *)(gap->ga_data))[gap->ga_len - 1].line);
1458 --gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001459 }
1460}
1461#endif
1462
1463/*
Bram Moolenaar89d40322006-08-29 15:30:07 +00001464 * If "fgetline" is get_loop_line(), return TRUE if the getline it uses equals
1465 * "func". * Otherwise return TRUE when "fgetline" equals "func".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001466 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001467 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001468getline_equal(
Bram Moolenaare96a2492019-06-25 04:12:16 +02001469 char_u *(*fgetline)(int, void *, int, int),
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001470 void *cookie UNUSED, /* argument for fgetline() */
Bram Moolenaare96a2492019-06-25 04:12:16 +02001471 char_u *(*func)(int, void *, int, int))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001472{
1473#ifdef FEAT_EVAL
Bram Moolenaare96a2492019-06-25 04:12:16 +02001474 char_u *(*gp)(int, void *, int, int);
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001475 struct loop_cookie *cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001476
Bram Moolenaar89d40322006-08-29 15:30:07 +00001477 /* When "fgetline" is "get_loop_line()" use the "cookie" to find the
Bram Moolenaarc1762cc2007-05-10 16:56:30 +00001478 * function that's originally used to obtain the lines. This may be
1479 * nested several levels. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00001480 gp = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001481 cp = (struct loop_cookie *)cookie;
1482 while (gp == get_loop_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001483 {
1484 gp = cp->getline;
1485 cp = cp->cookie;
1486 }
1487 return gp == func;
1488#else
Bram Moolenaar89d40322006-08-29 15:30:07 +00001489 return fgetline == func;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001490#endif
1491}
1492
Bram Moolenaar071d4272004-06-13 20:20:40 +00001493/*
Bram Moolenaar89d40322006-08-29 15:30:07 +00001494 * If "fgetline" is get_loop_line(), return the cookie used by the original
Bram Moolenaar071d4272004-06-13 20:20:40 +00001495 * getline function. Otherwise return "cookie".
1496 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001497 void *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001498getline_cookie(
Bram Moolenaare96a2492019-06-25 04:12:16 +02001499 char_u *(*fgetline)(int, void *, int, int) UNUSED,
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001500 void *cookie) /* argument for fgetline() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001501{
Bram Moolenaar13505972019-01-24 15:04:48 +01001502#ifdef FEAT_EVAL
Bram Moolenaare96a2492019-06-25 04:12:16 +02001503 char_u *(*gp)(int, void *, int, int);
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001504 struct loop_cookie *cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001505
Bram Moolenaar89d40322006-08-29 15:30:07 +00001506 /* When "fgetline" is "get_loop_line()" use the "cookie" to find the
Bram Moolenaarc1762cc2007-05-10 16:56:30 +00001507 * cookie that's originally used to obtain the lines. This may be nested
Bram Moolenaar071d4272004-06-13 20:20:40 +00001508 * several levels. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00001509 gp = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001510 cp = (struct loop_cookie *)cookie;
1511 while (gp == get_loop_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001512 {
1513 gp = cp->getline;
1514 cp = cp->cookie;
1515 }
1516 return cp;
Bram Moolenaar13505972019-01-24 15:04:48 +01001517#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001518 return cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001519#endif
Bram Moolenaar13505972019-01-24 15:04:48 +01001520}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001521
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001522
1523/*
1524 * Helper function to apply an offset for buffer commands, i.e. ":bdelete",
1525 * ":bwipeout", etc.
1526 * Returns the buffer number.
1527 */
1528 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001529compute_buffer_local_count(int addr_type, int lnum, int offset)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001530{
1531 buf_T *buf;
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001532 buf_T *nextbuf;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001533 int count = offset;
1534
1535 buf = firstbuf;
1536 while (buf->b_next != NULL && buf->b_fnum < lnum)
1537 buf = buf->b_next;
1538 while (count != 0)
1539 {
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001540 count += (offset < 0) ? 1 : -1;
1541 nextbuf = (offset < 0) ? buf->b_prev : buf->b_next;
1542 if (nextbuf == NULL)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001543 break;
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001544 buf = nextbuf;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001545 if (addr_type == ADDR_LOADED_BUFFERS)
1546 /* skip over unloaded buffers */
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001547 while (buf->b_ml.ml_mfp == NULL)
1548 {
1549 nextbuf = (offset < 0) ? buf->b_prev : buf->b_next;
1550 if (nextbuf == NULL)
1551 break;
1552 buf = nextbuf;
1553 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001554 }
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001555 /* we might have gone too far, last buffer is not loadedd */
1556 if (addr_type == ADDR_LOADED_BUFFERS)
1557 while (buf->b_ml.ml_mfp == NULL)
1558 {
1559 nextbuf = (offset >= 0) ? buf->b_prev : buf->b_next;
1560 if (nextbuf == NULL)
1561 break;
1562 buf = nextbuf;
1563 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001564 return buf->b_fnum;
1565}
1566
Bram Moolenaarb7316892019-05-01 18:08:42 +02001567/*
1568 * Return the window number of "win".
1569 * When "win" is NULL return the number of windows.
1570 */
Bram Moolenaarf240e182014-11-27 18:33:02 +01001571 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001572current_win_nr(win_T *win)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001573{
1574 win_T *wp;
1575 int nr = 0;
1576
Bram Moolenaar29323592016-07-24 22:04:11 +02001577 FOR_ALL_WINDOWS(wp)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001578 {
1579 ++nr;
1580 if (wp == win)
1581 break;
1582 }
1583 return nr;
1584}
1585
1586 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001587current_tab_nr(tabpage_T *tab)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001588{
1589 tabpage_T *tp;
1590 int nr = 0;
1591
Bram Moolenaar29323592016-07-24 22:04:11 +02001592 FOR_ALL_TABPAGES(tp)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001593 {
1594 ++nr;
1595 if (tp == tab)
1596 break;
1597 }
1598 return nr;
1599}
1600
1601# define CURRENT_WIN_NR current_win_nr(curwin)
1602# define LAST_WIN_NR current_win_nr(NULL)
1603# define CURRENT_TAB_NR current_tab_nr(curtab)
1604# define LAST_TAB_NR current_tab_nr(NULL)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001605
Bram Moolenaar071d4272004-06-13 20:20:40 +00001606/*
1607 * Execute one Ex command.
1608 *
1609 * If 'sourcing' is TRUE, the command will be included in the error message.
1610 *
1611 * 1. skip comment lines and leading space
1612 * 2. handle command modifiers
Bram Moolenaar1c40a662014-11-27 16:38:11 +01001613 * 3. find the command
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001614 * 4. parse range
Bram Moolenaar1c40a662014-11-27 16:38:11 +01001615 * 5. Parse the command.
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001616 * 6. parse arguments
1617 * 7. switch on command name
Bram Moolenaar071d4272004-06-13 20:20:40 +00001618 *
Bram Moolenaar89d40322006-08-29 15:30:07 +00001619 * Note: "fgetline" can be NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001620 *
1621 * This function may be called recursively!
1622 */
1623#if (_MSC_VER == 1200)
1624/*
Bram Moolenaared203462004-06-16 11:19:22 +00001625 * Avoid optimisation bug in VC++ version 6.0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001626 */
Bram Moolenaar281bdce2005-01-25 21:53:18 +00001627 #pragma optimize( "g", off )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001628#endif
1629 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001630do_one_cmd(
1631 char_u **cmdlinep,
1632 int sourcing,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001633#ifdef FEAT_EVAL
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001634 struct condstack *cstack,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001635#endif
Bram Moolenaare96a2492019-06-25 04:12:16 +02001636 char_u *(*fgetline)(int, void *, int, int),
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001637 void *cookie) /* argument for fgetline() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001638{
1639 char_u *p;
1640 linenr_T lnum;
1641 long n;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001642 char *errormsg = NULL; /* error message */
Bram Moolenaar7b668e82016-08-23 23:51:21 +02001643 char_u *after_modifier = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001644 exarg_T ea; /* Ex command arguments */
Bram Moolenaar8e258a42009-07-09 13:55:43 +00001645 int save_msg_scroll = msg_scroll;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001646 cmdmod_T save_cmdmod;
Bram Moolenaar9a2c0912019-03-30 14:26:18 +01001647 int save_reg_executing = reg_executing;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001648 int ni; /* set when Not Implemented */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001649 char_u *cmd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001650
1651 vim_memset(&ea, 0, sizeof(ea));
1652 ea.line1 = 1;
1653 ea.line2 = 1;
1654#ifdef FEAT_EVAL
1655 ++ex_nesting_level;
1656#endif
1657
Bram Moolenaar21691f82012-12-05 19:13:18 +01001658 /* When the last file has not been edited :q has to be typed twice. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001659 if (quitmore
1660#ifdef FEAT_EVAL
1661 /* avoid that a function call in 'statusline' does this */
Bram Moolenaar89d40322006-08-29 15:30:07 +00001662 && !getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001663#endif
Bram Moolenaar21691f82012-12-05 19:13:18 +01001664 /* avoid that an autocommand, e.g. QuitPre, does this */
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001665 && !getline_equal(fgetline, cookie, getnextac))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001666 --quitmore;
1667
1668 /*
1669 * Reset browse, confirm, etc.. They are restored when returning, for
1670 * recursive calls.
1671 */
1672 save_cmdmod = cmdmod;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001673
Bram Moolenaarcbb37ad2006-08-16 15:04:21 +00001674 /* "#!anything" is handled like a comment. */
1675 if ((*cmdlinep)[0] == '#' && (*cmdlinep)[1] == '!')
1676 goto doend;
1677
Bram Moolenaar4facea32019-10-12 20:17:40 +02001678 if (p_verbose >= 16)
1679 msg_verbose_cmd(0, *cmdlinep);
1680
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02001681/*
1682 * 1. Skip comment lines and leading white space and colons.
1683 * 2. Handle command modifiers.
1684 */
1685 // The "ea" structure holds the arguments that can be used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001686 ea.cmd = *cmdlinep;
Bram Moolenaareffed932018-08-14 13:38:17 +02001687 ea.cmdlinep = cmdlinep;
1688 ea.getline = fgetline;
1689 ea.cookie = cookie;
1690#ifdef FEAT_EVAL
1691 ea.cstack = cstack;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001692#endif
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02001693 if (parse_command_modifiers(&ea, &errormsg, FALSE) == FAIL)
Bram Moolenaareffed932018-08-14 13:38:17 +02001694 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001695
Bram Moolenaar7b668e82016-08-23 23:51:21 +02001696 after_modifier = ea.cmd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001697
1698#ifdef FEAT_EVAL
1699 ea.skip = did_emsg || got_int || did_throw || (cstack->cs_idx >= 0
1700 && !(cstack->cs_flags[cstack->cs_idx] & CSF_ACTIVE));
1701#else
1702 ea.skip = (if_level > 0);
1703#endif
1704
Bram Moolenaar071d4272004-06-13 20:20:40 +00001705/*
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001706 * 3. Skip over the range to find the command. Let "p" point to after it.
1707 *
1708 * We need the command to know what kind of range it uses.
1709 */
1710 cmd = ea.cmd;
1711 ea.cmd = skip_range(ea.cmd, NULL);
1712 if (*ea.cmd == '*' && vim_strchr(p_cpo, CPO_STAR) == NULL)
1713 ea.cmd = skipwhite(ea.cmd + 1);
1714 p = find_command(&ea, NULL);
1715
Bram Moolenaar7feb35e2018-08-21 17:49:54 +02001716#ifdef FEAT_EVAL
1717# ifdef FEAT_PROFILE
1718 // Count this line for profiling if skip is TRUE.
1719 if (do_profiling == PROF_YES
1720 && (!ea.skip || cstack->cs_idx == 0 || (cstack->cs_idx > 0
1721 && (cstack->cs_flags[cstack->cs_idx - 1] & CSF_ACTIVE))))
1722 {
1723 int skip = did_emsg || got_int || did_throw;
1724
1725 if (ea.cmdidx == CMD_catch)
1726 skip = !skip && !(cstack->cs_idx >= 0
1727 && (cstack->cs_flags[cstack->cs_idx] & CSF_THROWN)
1728 && !(cstack->cs_flags[cstack->cs_idx] & CSF_CAUGHT));
1729 else if (ea.cmdidx == CMD_else || ea.cmdidx == CMD_elseif)
1730 skip = skip || !(cstack->cs_idx >= 0
1731 && !(cstack->cs_flags[cstack->cs_idx]
1732 & (CSF_ACTIVE | CSF_TRUE)));
1733 else if (ea.cmdidx == CMD_finally)
1734 skip = FALSE;
1735 else if (ea.cmdidx != CMD_endif
1736 && ea.cmdidx != CMD_endfor
1737 && ea.cmdidx != CMD_endtry
1738 && ea.cmdidx != CMD_endwhile)
1739 skip = ea.skip;
1740
1741 if (!skip)
1742 {
1743 if (getline_equal(fgetline, cookie, get_func_line))
1744 func_line_exec(getline_cookie(fgetline, cookie));
1745 else if (getline_equal(fgetline, cookie, getsourceline))
1746 script_line_exec();
1747 }
1748 }
1749# endif
1750
1751 /* May go to debug mode. If this happens and the ">quit" debug command is
1752 * used, throw an interrupt exception and skip the next command. */
1753 dbg_check_breakpoint(&ea);
1754 if (!ea.skip && got_int)
1755 {
1756 ea.skip = TRUE;
1757 (void)do_intthrow(cstack);
1758 }
1759#endif
1760
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001761/*
1762 * 4. parse a range specifier of the form: addr [,addr] [;addr] ..
Bram Moolenaar071d4272004-06-13 20:20:40 +00001763 *
1764 * where 'addr' is:
1765 *
1766 * % (entire file)
1767 * $ [+-NUM]
1768 * 'x [+-NUM] (where x denotes a currently defined mark)
1769 * . [+-NUM]
1770 * [+-NUM]..
1771 * NUM
1772 *
1773 * The ea.cmd pointer is updated to point to the first character following the
1774 * range spec. If an initial address is found, but no second, the upper bound
1775 * is equal to the lower.
1776 */
1777
Bram Moolenaar25190db2019-05-04 15:05:28 +02001778 // ea.addr_type for user commands is set by find_ucmd
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01001779 if (!IS_USER_CMDIDX(ea.cmdidx))
1780 {
1781 if (ea.cmdidx != CMD_SIZE)
1782 ea.addr_type = cmdnames[(int)ea.cmdidx].cmd_addr_type;
1783 else
1784 ea.addr_type = ADDR_LINES;
1785
Bram Moolenaar25190db2019-05-04 15:05:28 +02001786 // :wincmd range depends on the argument.
Bram Moolenaar164f3262015-01-14 21:22:01 +01001787 if (ea.cmdidx == CMD_wincmd && p != NULL)
1788 get_wincmd_addr_type(skipwhite(p), &ea);
Bram Moolenaar25190db2019-05-04 15:05:28 +02001789#ifdef FEAT_QUICKFIX
1790 // :.cc in quickfix window uses line number
1791 if ((ea.cmdidx == CMD_cc || ea.cmdidx == CMD_ll) && bt_quickfix(curbuf))
1792 ea.addr_type = ADDR_OTHER;
1793#endif
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01001794 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001795
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01001796 ea.cmd = cmd;
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02001797 if (parse_cmd_address(&ea, &errormsg, FALSE) == FAIL)
Bram Moolenaaree8415b2018-08-10 23:13:12 +02001798 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001799
Bram Moolenaar071d4272004-06-13 20:20:40 +00001800/*
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001801 * 5. Parse the command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001802 */
1803
1804 /*
1805 * Skip ':' and any white space
1806 */
1807 ea.cmd = skipwhite(ea.cmd);
1808 while (*ea.cmd == ':')
1809 ea.cmd = skipwhite(ea.cmd + 1);
1810
1811 /*
1812 * If we got a line, but no command, then go to the line.
1813 * If we find a '|' or '\n' we set ea.nextcmd.
1814 */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001815 if (*ea.cmd == NUL || *ea.cmd == '"'
1816 || (ea.nextcmd = check_nextcmd(ea.cmd)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001817 {
1818 /*
1819 * strange vi behaviour:
1820 * ":3" jumps to line 3
1821 * ":3|..." prints line 3
1822 * ":|" prints current line
1823 */
1824 if (ea.skip) /* skip this if inside :if */
1825 goto doend;
Bram Moolenaardf177f62005-02-22 08:39:57 +00001826 if (*ea.cmd == '|' || (exmode_active && ea.line1 != ea.line2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001827 {
1828 ea.cmdidx = CMD_print;
Bram Moolenaar8071cb22019-07-12 17:58:01 +02001829 ea.argt = EX_RANGE+EX_COUNT+EX_TRLBAR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001830 if ((errormsg = invalid_range(&ea)) == NULL)
1831 {
1832 correct_range(&ea);
1833 ex_print(&ea);
1834 }
1835 }
1836 else if (ea.addr_count != 0)
1837 {
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00001838 if (ea.line2 > curbuf->b_ml.ml_line_count)
1839 {
1840 /* With '-' in 'cpoptions' a line number past the file is an
1841 * error, otherwise put it at the end of the file. */
1842 if (vim_strchr(p_cpo, CPO_MINUS) != NULL)
1843 ea.line2 = -1;
1844 else
1845 ea.line2 = curbuf->b_ml.ml_line_count;
1846 }
1847
1848 if (ea.line2 < 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001849 errormsg = _(e_invrange);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001850 else
1851 {
1852 if (ea.line2 == 0)
1853 curwin->w_cursor.lnum = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001854 else
1855 curwin->w_cursor.lnum = ea.line2;
1856 beginline(BL_SOL | BL_FIX);
1857 }
1858 }
1859 goto doend;
1860 }
1861
Bram Moolenaard5005162014-08-22 23:05:54 +02001862 /* If this looks like an undefined user command and there are CmdUndefined
1863 * autocommands defined, trigger the matching autocommands. */
1864 if (p != NULL && ea.cmdidx == CMD_SIZE && !ea.skip
1865 && ASCII_ISUPPER(*ea.cmd)
1866 && has_cmdundefined())
1867 {
Bram Moolenaard5005162014-08-22 23:05:54 +02001868 int ret;
1869
Bram Moolenaar5a31b462014-08-23 14:16:20 +02001870 p = ea.cmd;
Bram Moolenaard5005162014-08-22 23:05:54 +02001871 while (ASCII_ISALNUM(*p))
1872 ++p;
Bram Moolenaar120f4a82014-09-09 12:22:06 +02001873 p = vim_strnsave(ea.cmd, (int)(p - ea.cmd));
Bram Moolenaard5005162014-08-22 23:05:54 +02001874 ret = apply_autocmds(EVENT_CMDUNDEFINED, p, p, TRUE, NULL);
1875 vim_free(p);
Bram Moolenaar829aef12015-07-28 14:25:48 +02001876 /* If the autocommands did something and didn't cause an error, try
1877 * finding the command again. */
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001878 p = (ret
1879#ifdef FEAT_EVAL
1880 && !aborting()
Bram Moolenaard5005162014-08-22 23:05:54 +02001881#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001882 ) ? find_command(&ea, NULL) : ea.cmd;
1883 }
Bram Moolenaard5005162014-08-22 23:05:54 +02001884
Bram Moolenaar071d4272004-06-13 20:20:40 +00001885 if (p == NULL)
1886 {
1887 if (!ea.skip)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001888 errormsg = _("E464: Ambiguous use of user-defined command");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001889 goto doend;
1890 }
Bram Moolenaarac9fb182019-04-27 13:04:13 +02001891 // Check for wrong commands.
Bram Moolenaar6f9a4762017-06-22 20:39:17 +02001892 if (*p == '!' && ea.cmd[1] == 0151 && ea.cmd[0] == 78
1893 && !IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001894 {
1895 errormsg = uc_fun_cmd();
1896 goto doend;
1897 }
Bram Moolenaarac9fb182019-04-27 13:04:13 +02001898
Bram Moolenaar071d4272004-06-13 20:20:40 +00001899 if (ea.cmdidx == CMD_SIZE)
1900 {
1901 if (!ea.skip)
1902 {
1903 STRCPY(IObuff, _("E492: Not an editor command"));
1904 if (!sourcing)
Bram Moolenaar7b668e82016-08-23 23:51:21 +02001905 {
1906 /* If the modifier was parsed OK the error must be in the
1907 * following command */
1908 if (after_modifier != NULL)
1909 append_command(after_modifier);
1910 else
1911 append_command(*cmdlinep);
1912 }
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001913 errormsg = (char *)IObuff;
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001914 did_emsg_syntax = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001915 }
1916 goto doend;
1917 }
1918
Bram Moolenaar958636c2014-10-21 20:01:58 +02001919 ni = (!IS_USER_CMDIDX(ea.cmdidx)
1920 && (cmdnames[ea.cmdidx].cmd_func == ex_ni
Bram Moolenaar7bb75552007-07-16 18:39:49 +00001921#ifdef HAVE_EX_SCRIPT_NI
1922 || cmdnames[ea.cmdidx].cmd_func == ex_script_ni
1923#endif
1924 ));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001925
1926#ifndef FEAT_EVAL
1927 /*
1928 * When the expression evaluation is disabled, recognize the ":if" and
1929 * ":endif" commands and ignore everything in between it.
1930 */
1931 if (ea.cmdidx == CMD_if)
1932 ++if_level;
1933 if (if_level)
1934 {
1935 if (ea.cmdidx == CMD_endif)
1936 --if_level;
1937 goto doend;
1938 }
1939
1940#endif
1941
Bram Moolenaar196b3b02008-06-20 16:51:41 +00001942 /* forced commands */
1943 if (*p == '!' && ea.cmdidx != CMD_substitute
1944 && ea.cmdidx != CMD_smagic && ea.cmdidx != CMD_snomagic)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001945 {
1946 ++p;
1947 ea.forceit = TRUE;
1948 }
1949 else
1950 ea.forceit = FALSE;
1951
1952/*
Bram Moolenaarb7316892019-05-01 18:08:42 +02001953 * 6. Parse arguments. Then check for errors.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001954 */
Bram Moolenaar958636c2014-10-21 20:01:58 +02001955 if (!IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001956 ea.argt = (long)cmdnames[(int)ea.cmdidx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001957
1958 if (!ea.skip)
1959 {
1960#ifdef HAVE_SANDBOX
Bram Moolenaar8071cb22019-07-12 17:58:01 +02001961 if (sandbox != 0 && !(ea.argt & EX_SBOXOK))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001962 {
Bram Moolenaar8c62a082019-02-08 14:34:10 +01001963 // Command not allowed in sandbox.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001964 errormsg = _(e_sandbox);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001965 goto doend;
1966 }
1967#endif
Bram Moolenaar8071cb22019-07-12 17:58:01 +02001968 if (restricted != 0 && (ea.argt & EX_RESTRICT))
Bram Moolenaar8c62a082019-02-08 14:34:10 +01001969 {
1970 errormsg = _("E981: Command not allowed in rvim");
1971 goto doend;
1972 }
Bram Moolenaar8071cb22019-07-12 17:58:01 +02001973 if (!curbuf->b_p_ma && (ea.argt & EX_MODIFY))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001974 {
1975 /* Command not allowed in non-'modifiable' buffer */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001976 errormsg = _(e_modifiable);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001977 goto doend;
1978 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00001979
Bram Moolenaar8071cb22019-07-12 17:58:01 +02001980 if (text_locked() && !(ea.argt & EX_CMDWIN)
Bram Moolenaar958636c2014-10-21 20:01:58 +02001981 && !IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001982 {
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00001983 /* Command not allowed when editing the command line. */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001984 errormsg = _(get_text_locked_msg());
Bram Moolenaar071d4272004-06-13 20:20:40 +00001985 goto doend;
1986 }
Bram Moolenaar379fb762018-08-30 15:58:28 +02001987
Bram Moolenaar5555acc2006-04-07 21:33:12 +00001988 /* Disallow editing another buffer when "curbuf_lock" is set.
Bram Moolenaar379fb762018-08-30 15:58:28 +02001989 * Do allow ":checktime" (it is postponed).
1990 * Do allow ":edit" (check for an argument later).
1991 * Do allow ":file" with no arguments (check for an argument later). */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02001992 if (!(ea.argt & EX_CMDWIN)
Bram Moolenaar5555acc2006-04-07 21:33:12 +00001993 && ea.cmdidx != CMD_checktime
Bram Moolenaar379fb762018-08-30 15:58:28 +02001994 && ea.cmdidx != CMD_edit
1995 && ea.cmdidx != CMD_file
Bram Moolenaar958636c2014-10-21 20:01:58 +02001996 && !IS_USER_CMDIDX(ea.cmdidx)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001997 && curbuf_locked())
1998 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001999
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002000 if (!ni && !(ea.argt & EX_RANGE) && ea.addr_count > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002001 {
2002 /* no range allowed */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002003 errormsg = _(e_norange);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002004 goto doend;
2005 }
2006 }
2007
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002008 if (!ni && !(ea.argt & EX_BANG) && ea.forceit) // no <!> allowed
Bram Moolenaar071d4272004-06-13 20:20:40 +00002009 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002010 errormsg = _(e_nobang);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002011 goto doend;
2012 }
2013
2014 /*
2015 * Don't complain about the range if it is not used
2016 * (could happen if line_count is accidentally set to 0).
2017 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002018 if (!ea.skip && !ni && (ea.argt & EX_RANGE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002019 {
2020 /*
2021 * If the range is backwards, ask for confirmation and, if given, swap
2022 * ea.line1 & ea.line2 so it's forwards again.
2023 * When global command is busy, don't ask, will fail below.
2024 */
2025 if (!global_busy && ea.line1 > ea.line2)
2026 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002027 if (msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002028 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002029 if (sourcing || exmode_active)
2030 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002031 errormsg = _("E493: Backwards range given");
Bram Moolenaara5792f52005-11-23 21:25:05 +00002032 goto doend;
2033 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002034 if (ask_yesno((char_u *)
2035 _("Backwards range given, OK to swap"), FALSE) != 'y')
Bram Moolenaara5792f52005-11-23 21:25:05 +00002036 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002037 }
2038 lnum = ea.line1;
2039 ea.line1 = ea.line2;
2040 ea.line2 = lnum;
2041 }
2042 if ((errormsg = invalid_range(&ea)) != NULL)
2043 goto doend;
2044 }
2045
Bram Moolenaarb7316892019-05-01 18:08:42 +02002046 if ((ea.addr_type == ADDR_OTHER) && ea.addr_count == 0)
2047 // default is 1, not cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00002048 ea.line2 = 1;
2049
2050 correct_range(&ea);
2051
2052#ifdef FEAT_FOLDING
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002053 if (((ea.argt & EX_WHOLEFOLD) || ea.addr_count >= 2) && !global_busy
Bram Moolenaara3306952016-01-02 21:41:06 +01002054 && ea.addr_type == ADDR_LINES)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002055 {
2056 /* Put the first line at the start of a closed fold, put the last line
2057 * at the end of a closed fold. */
2058 (void)hasFolding(ea.line1, &ea.line1, NULL);
2059 (void)hasFolding(ea.line2, NULL, &ea.line2);
2060 }
2061#endif
2062
2063#ifdef FEAT_QUICKFIX
2064 /*
Bram Moolenaar86b68352004-12-27 21:59:20 +00002065 * For the ":make" and ":grep" commands we insert the 'makeprg'/'grepprg'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002066 * option here, so things like % get expanded.
2067 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002068 p = replace_makeprg(&ea, p, cmdlinep);
2069 if (p == NULL)
2070 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002071#endif
2072
2073 /*
2074 * Skip to start of argument.
2075 * Don't do this for the ":!" command, because ":!! -l" needs the space.
2076 */
2077 if (ea.cmdidx == CMD_bang)
2078 ea.arg = p;
2079 else
2080 ea.arg = skipwhite(p);
2081
Bram Moolenaar379fb762018-08-30 15:58:28 +02002082 // ":file" cannot be run with an argument when "curbuf_lock" is set
2083 if (ea.cmdidx == CMD_file && *ea.arg != NUL && curbuf_locked())
2084 goto doend;
2085
Bram Moolenaar071d4272004-06-13 20:20:40 +00002086 /*
2087 * Check for "++opt=val" argument.
2088 * Must be first, allow ":w ++enc=utf8 !cmd"
2089 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002090 if (ea.argt & EX_ARGOPT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002091 while (ea.arg[0] == '+' && ea.arg[1] == '+')
2092 if (getargopt(&ea) == FAIL && !ni)
2093 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002094 errormsg = _(e_invarg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002095 goto doend;
2096 }
2097
2098 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
2099 {
2100 if (*ea.arg == '>') /* append */
2101 {
2102 if (*++ea.arg != '>') /* typed wrong */
2103 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002104 errormsg = _("E494: Use w or w>>");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002105 goto doend;
2106 }
2107 ea.arg = skipwhite(ea.arg + 1);
2108 ea.append = TRUE;
2109 }
2110 else if (*ea.arg == '!' && ea.cmdidx == CMD_write) /* :w !filter */
2111 {
2112 ++ea.arg;
2113 ea.usefilter = TRUE;
2114 }
2115 }
2116
2117 if (ea.cmdidx == CMD_read)
2118 {
2119 if (ea.forceit)
2120 {
2121 ea.usefilter = TRUE; /* :r! filter if ea.forceit */
2122 ea.forceit = FALSE;
2123 }
2124 else if (*ea.arg == '!') /* :r !filter */
2125 {
2126 ++ea.arg;
2127 ea.usefilter = TRUE;
2128 }
2129 }
2130
2131 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
2132 {
2133 ea.amount = 1;
2134 while (*ea.arg == *ea.cmd) /* count number of '>' or '<' */
2135 {
2136 ++ea.arg;
2137 ++ea.amount;
2138 }
2139 ea.arg = skipwhite(ea.arg);
2140 }
2141
2142 /*
2143 * Check for "+command" argument, before checking for next command.
2144 * Don't do this for ":read !cmd" and ":write !cmd".
2145 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002146 if ((ea.argt & EX_CMDARG) && !ea.usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002147 ea.do_ecmd_cmd = getargcmd(&ea.arg);
2148
2149 /*
2150 * Check for '|' to separate commands and '"' to start comments.
2151 * Don't do this for ":read !cmd" and ":write !cmd".
2152 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002153 if ((ea.argt & EX_TRLBAR) && !ea.usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002154 separate_nextcmd(&ea);
2155
2156 /*
2157 * Check for <newline> to end a shell command.
Bram Moolenaardf177f62005-02-22 08:39:57 +00002158 * Also do this for ":read !cmd", ":write !cmd" and ":global".
2159 * Any others?
Bram Moolenaar071d4272004-06-13 20:20:40 +00002160 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00002161 else if (ea.cmdidx == CMD_bang
Bram Moolenaar67883b42017-07-27 22:57:00 +02002162 || ea.cmdidx == CMD_terminal
Bram Moolenaardf177f62005-02-22 08:39:57 +00002163 || ea.cmdidx == CMD_global
2164 || ea.cmdidx == CMD_vglobal
2165 || ea.usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002166 {
2167 for (p = ea.arg; *p; ++p)
2168 {
2169 /* Remove one backslash before a newline, so that it's possible to
2170 * pass a newline to the shell and also a newline that is preceded
2171 * with a backslash. This makes it impossible to end a shell
2172 * command in a backslash, but that doesn't appear useful.
2173 * Halving the number of backslashes is incompatible with previous
2174 * versions. */
2175 if (*p == '\\' && p[1] == '\n')
Bram Moolenaara7241f52008-06-24 20:39:31 +00002176 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002177 else if (*p == '\n')
2178 {
2179 ea.nextcmd = p + 1;
2180 *p = NUL;
2181 break;
2182 }
2183 }
2184 }
2185
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002186 if ((ea.argt & EX_DFLALL) && ea.addr_count == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002187 {
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002188 buf_T *buf;
2189
Bram Moolenaar071d4272004-06-13 20:20:40 +00002190 ea.line1 = 1;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002191 switch (ea.addr_type)
2192 {
2193 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02002194 case ADDR_OTHER:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002195 ea.line2 = curbuf->b_ml.ml_line_count;
2196 break;
2197 case ADDR_LOADED_BUFFERS:
2198 buf = firstbuf;
2199 while (buf->b_next != NULL && buf->b_ml.ml_mfp == NULL)
2200 buf = buf->b_next;
2201 ea.line1 = buf->b_fnum;
2202 buf = lastbuf;
2203 while (buf->b_prev != NULL && buf->b_ml.ml_mfp == NULL)
2204 buf = buf->b_prev;
2205 ea.line2 = buf->b_fnum;
2206 break;
2207 case ADDR_BUFFERS:
2208 ea.line1 = firstbuf->b_fnum;
2209 ea.line2 = lastbuf->b_fnum;
2210 break;
2211 case ADDR_WINDOWS:
2212 ea.line2 = LAST_WIN_NR;
2213 break;
2214 case ADDR_TABS:
2215 ea.line2 = LAST_TAB_NR;
2216 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01002217 case ADDR_TABS_RELATIVE:
2218 ea.line2 = 1;
2219 break;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002220 case ADDR_ARGUMENTS:
2221 if (ARGCOUNT == 0)
2222 ea.line1 = ea.line2 = 0;
2223 else
2224 ea.line2 = ARGCOUNT;
2225 break;
Bram Moolenaar25190db2019-05-04 15:05:28 +02002226 case ADDR_QUICKFIX_VALID:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02002227#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02002228 ea.line2 = qf_get_valid_size(&ea);
Bram Moolenaaraa23b372015-09-08 18:46:31 +02002229 if (ea.line2 == 0)
2230 ea.line2 = 1;
Bram Moolenaare906c502015-09-09 21:10:39 +02002231#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02002232 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02002233 case ADDR_NONE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02002234 case ADDR_UNSIGNED:
2235 case ADDR_QUICKFIX:
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002236 iemsg(_("INTERNAL: Cannot use EX_DFLALL with ADDR_NONE, ADDR_UNSIGNED or ADDR_QUICKFIX"));
Bram Moolenaarb7316892019-05-01 18:08:42 +02002237 break;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002238 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002239 }
2240
2241 /* accept numbered register only when no count allowed (:put) */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002242 if ( (ea.argt & EX_REGSTR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002243 && *ea.arg != NUL
Bram Moolenaar958636c2014-10-21 20:01:58 +02002244 /* Do not allow register = for user commands */
2245 && (!IS_USER_CMDIDX(ea.cmdidx) || *ea.arg != '=')
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002246 && !((ea.argt & EX_COUNT) && VIM_ISDIGIT(*ea.arg)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002247 {
Bram Moolenaar85de2062011-05-05 14:26:41 +02002248#ifndef FEAT_CLIPBOARD
2249 /* check these explicitly for a more specific error message */
2250 if (*ea.arg == '*' || *ea.arg == '+')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002251 {
Bram Moolenaar99b12722019-01-14 20:16:40 +01002252 errormsg = _(e_invalidreg);
Bram Moolenaar85de2062011-05-05 14:26:41 +02002253 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002254 }
2255#endif
Bram Moolenaar958636c2014-10-21 20:01:58 +02002256 if (valid_yank_reg(*ea.arg, (ea.cmdidx != CMD_put
2257 && !IS_USER_CMDIDX(ea.cmdidx))))
Bram Moolenaar85de2062011-05-05 14:26:41 +02002258 {
2259 ea.regname = *ea.arg++;
2260#ifdef FEAT_EVAL
2261 /* for '=' register: accept the rest of the line as an expression */
2262 if (ea.arg[-1] == '=' && ea.arg[0] != NUL)
2263 {
2264 set_expr_line(vim_strsave(ea.arg));
2265 ea.arg += STRLEN(ea.arg);
2266 }
2267#endif
2268 ea.arg = skipwhite(ea.arg);
2269 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002270 }
2271
2272 /*
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002273 * Check for a count. When accepting a EX_BUFNAME, don't use "123foo" as a
Bram Moolenaar071d4272004-06-13 20:20:40 +00002274 * count, it's a buffer name.
2275 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002276 if ((ea.argt & EX_COUNT) && VIM_ISDIGIT(*ea.arg)
2277 && (!(ea.argt & EX_BUFNAME) || *(p = skipdigits(ea.arg)) == NUL
Bram Moolenaar1c465442017-03-12 20:10:05 +01002278 || VIM_ISWHITE(*p)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002279 {
2280 n = getdigits(&ea.arg);
2281 ea.arg = skipwhite(ea.arg);
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002282 if (n <= 0 && !ni && (ea.argt & EX_ZEROR) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002283 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002284 errormsg = _(e_zerocount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002285 goto doend;
2286 }
Bram Moolenaarb7316892019-05-01 18:08:42 +02002287 if (ea.addr_type != ADDR_LINES) // e.g. :buffer 2, :sleep 3
Bram Moolenaar071d4272004-06-13 20:20:40 +00002288 {
2289 ea.line2 = n;
2290 if (ea.addr_count == 0)
2291 ea.addr_count = 1;
2292 }
2293 else
2294 {
2295 ea.line1 = ea.line2;
2296 ea.line2 += n - 1;
2297 ++ea.addr_count;
2298 /*
2299 * Be vi compatible: no error message for out of range.
2300 */
Bram Moolenaarb7316892019-05-01 18:08:42 +02002301 if (ea.line2 > curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002302 ea.line2 = curbuf->b_ml.ml_line_count;
2303 }
2304 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00002305
2306 /*
2307 * Check for flags: 'l', 'p' and '#'.
2308 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002309 if (ea.argt & EX_FLAGS)
Bram Moolenaardf177f62005-02-22 08:39:57 +00002310 get_flags(&ea);
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002311 if (!ni && !(ea.argt & EX_EXTRA) && *ea.arg != NUL
2312 && *ea.arg != '"' && (*ea.arg != '|' || (ea.argt & EX_TRLBAR) == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002313 {
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002314 // no arguments allowed but there is something
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002315 errormsg = _(e_trailing);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002316 goto doend;
2317 }
2318
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002319 if (!ni && (ea.argt & EX_NEEDARG) && *ea.arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002320 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002321 errormsg = _(e_argreq);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002322 goto doend;
2323 }
2324
2325#ifdef FEAT_EVAL
2326 /*
2327 * Skip the command when it's not going to be executed.
2328 * The commands like :if, :endif, etc. always need to be executed.
2329 * Also make an exception for commands that handle a trailing command
2330 * themselves.
2331 */
2332 if (ea.skip)
2333 {
2334 switch (ea.cmdidx)
2335 {
2336 /* commands that need evaluation */
2337 case CMD_while:
2338 case CMD_endwhile:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00002339 case CMD_for:
2340 case CMD_endfor:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002341 case CMD_if:
2342 case CMD_elseif:
2343 case CMD_else:
2344 case CMD_endif:
2345 case CMD_try:
2346 case CMD_catch:
2347 case CMD_finally:
2348 case CMD_endtry:
2349 case CMD_function:
2350 break;
2351
2352 /* Commands that handle '|' themselves. Check: A command should
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002353 * either have the EX_TRLBAR flag, appear in this list or appear in
Bram Moolenaar071d4272004-06-13 20:20:40 +00002354 * the list at ":help :bar". */
2355 case CMD_aboveleft:
2356 case CMD_and:
2357 case CMD_belowright:
2358 case CMD_botright:
2359 case CMD_browse:
2360 case CMD_call:
2361 case CMD_confirm:
2362 case CMD_delfunction:
2363 case CMD_djump:
2364 case CMD_dlist:
2365 case CMD_dsearch:
2366 case CMD_dsplit:
2367 case CMD_echo:
2368 case CMD_echoerr:
2369 case CMD_echomsg:
2370 case CMD_echon:
2371 case CMD_execute:
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002372 case CMD_filter:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002373 case CMD_help:
2374 case CMD_hide:
2375 case CMD_ijump:
2376 case CMD_ilist:
2377 case CMD_isearch:
2378 case CMD_isplit:
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002379 case CMD_keepalt:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002380 case CMD_keepjumps:
2381 case CMD_keepmarks:
Bram Moolenaara939e432013-11-09 05:30:26 +01002382 case CMD_keeppatterns:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002383 case CMD_leftabove:
2384 case CMD_let:
2385 case CMD_lockmarks:
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:
2410 case CMD_verbose:
2411 case CMD_vertical:
Bram Moolenaar12bde492011-06-13 01:19:56 +02002412 case CMD_wincmd:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002413 break;
2414
2415 default: goto doend;
2416 }
2417 }
2418#endif
2419
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002420 if (ea.argt & EX_XFILE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002421 {
2422 if (expand_filename(&ea, cmdlinep, &errormsg) == FAIL)
2423 goto doend;
2424 }
2425
Bram Moolenaar071d4272004-06-13 20:20:40 +00002426 /*
2427 * Accept buffer name. Cannot be used at the same time with a buffer
2428 * number. Don't do this for a user command.
2429 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002430 if ((ea.argt & EX_BUFNAME) && *ea.arg != NUL && ea.addr_count == 0
Bram Moolenaar958636c2014-10-21 20:01:58 +02002431 && !IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002432 {
2433 /*
2434 * :bdelete, :bwipeout and :bunload take several arguments, separated
2435 * by spaces: find next space (skipping over escaped characters).
2436 * The others take one argument: ignore trailing spaces.
2437 */
2438 if (ea.cmdidx == CMD_bdelete || ea.cmdidx == CMD_bwipeout
2439 || ea.cmdidx == CMD_bunload)
2440 p = skiptowhite_esc(ea.arg);
2441 else
2442 {
2443 p = ea.arg + STRLEN(ea.arg);
Bram Moolenaar1c465442017-03-12 20:10:05 +01002444 while (p > ea.arg && VIM_ISWHITE(p[-1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002445 --p;
2446 }
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002447 ea.line2 = buflist_findpat(ea.arg, p, (ea.argt & EX_BUFUNL) != 0,
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002448 FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002449 if (ea.line2 < 0) /* failed */
2450 goto doend;
2451 ea.addr_count = 1;
2452 ea.arg = skipwhite(p);
2453 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002454
Bram Moolenaar2be57332018-02-13 18:05:18 +01002455 /* The :try command saves the emsg_silent flag, reset it here when
2456 * ":silent! try" was used, it should only apply to :try itself. */
Bram Moolenaareffed932018-08-14 13:38:17 +02002457 if (ea.cmdidx == CMD_try && ea.did_esilent > 0)
Bram Moolenaar2be57332018-02-13 18:05:18 +01002458 {
Bram Moolenaareffed932018-08-14 13:38:17 +02002459 emsg_silent -= ea.did_esilent;
Bram Moolenaar2be57332018-02-13 18:05:18 +01002460 if (emsg_silent < 0)
2461 emsg_silent = 0;
Bram Moolenaareffed932018-08-14 13:38:17 +02002462 ea.did_esilent = 0;
Bram Moolenaar2be57332018-02-13 18:05:18 +01002463 }
2464
Bram Moolenaar071d4272004-06-13 20:20:40 +00002465/*
Bram Moolenaar2be57332018-02-13 18:05:18 +01002466 * 7. Execute the command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002467 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002468
Bram Moolenaar958636c2014-10-21 20:01:58 +02002469 if (IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002470 {
2471 /*
2472 * Execute a user-defined command.
2473 */
2474 do_ucmd(&ea);
2475 }
2476 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002477 {
2478 /*
Bram Moolenaarac9fb182019-04-27 13:04:13 +02002479 * Call the function to execute the builtin command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002480 */
2481 ea.errmsg = NULL;
2482 (cmdnames[ea.cmdidx].cmd_func)(&ea);
2483 if (ea.errmsg != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002484 errormsg = _(ea.errmsg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002485 }
2486
2487#ifdef FEAT_EVAL
2488 /*
2489 * If the command just executed called do_cmdline(), any throw or ":return"
2490 * or ":finish" encountered there must also check the cstack of the still
2491 * active do_cmdline() that called this do_one_cmd(). Rethrow an uncaught
2492 * exception, or reanimate a returned function or finished script file and
2493 * return or finish it again.
2494 */
2495 if (need_rethrow)
2496 do_throw(cstack);
2497 else if (check_cstack)
2498 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002499 if (source_finished(fgetline, cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002500 do_finish(&ea, TRUE);
Bram Moolenaar89d40322006-08-29 15:30:07 +00002501 else if (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002502 && current_func_returned())
2503 do_return(&ea, TRUE, FALSE, NULL);
2504 }
2505 need_rethrow = check_cstack = FALSE;
2506#endif
2507
2508doend:
2509 if (curwin->w_cursor.lnum == 0) /* can happen with zero line number */
Bram Moolenaar6de5e122017-04-20 21:55:44 +02002510 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002511 curwin->w_cursor.lnum = 1;
Bram Moolenaar6de5e122017-04-20 21:55:44 +02002512 curwin->w_cursor.col = 0;
2513 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002514
2515 if (errormsg != NULL && *errormsg != NUL && !did_emsg)
2516 {
2517 if (sourcing)
2518 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002519 if (errormsg != (char *)IObuff)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002520 {
2521 STRCPY(IObuff, errormsg);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002522 errormsg = (char *)IObuff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002523 }
Bram Moolenaara6f4d612011-09-21 19:10:46 +02002524 append_command(*cmdlinep);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002525 }
2526 emsg(errormsg);
2527 }
2528#ifdef FEAT_EVAL
2529 do_errthrow(cstack,
Bram Moolenaar958636c2014-10-21 20:01:58 +02002530 (ea.cmdidx != CMD_SIZE && !IS_USER_CMDIDX(ea.cmdidx))
2531 ? cmdnames[(int)ea.cmdidx].cmd_name : (char_u *)NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002532#endif
2533
Bram Moolenaareffed932018-08-14 13:38:17 +02002534 if (ea.verbose_save >= 0)
2535 p_verbose = ea.verbose_save;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002536
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002537 free_cmdmod();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002538 cmdmod = save_cmdmod;
Bram Moolenaar9a2c0912019-03-30 14:26:18 +01002539 reg_executing = save_reg_executing;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002540
Bram Moolenaareffed932018-08-14 13:38:17 +02002541 if (ea.save_msg_silent != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002542 {
2543 /* messages could be enabled for a serious error, need to check if the
2544 * counters don't become negative */
Bram Moolenaareffed932018-08-14 13:38:17 +02002545 if (!did_emsg || msg_silent > ea.save_msg_silent)
2546 msg_silent = ea.save_msg_silent;
2547 emsg_silent -= ea.did_esilent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002548 if (emsg_silent < 0)
2549 emsg_silent = 0;
2550 /* Restore msg_scroll, it's set by file I/O commands, even when no
2551 * message is actually displayed. */
2552 msg_scroll = save_msg_scroll;
Bram Moolenaar77ab2802009-04-22 12:44:48 +00002553
2554 /* "silent reg" or "silent echo x" inside "redir" leaves msg_col
2555 * somewhere in the line. Put it back in the first column. */
2556 if (redirecting())
2557 msg_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002558 }
2559
Bram Moolenaar7171abe2004-10-11 10:06:20 +00002560#ifdef HAVE_SANDBOX
Bram Moolenaareffed932018-08-14 13:38:17 +02002561 if (ea.did_sandbox)
Bram Moolenaar7171abe2004-10-11 10:06:20 +00002562 --sandbox;
2563#endif
2564
Bram Moolenaar071d4272004-06-13 20:20:40 +00002565 if (ea.nextcmd && *ea.nextcmd == NUL) /* not really a next command */
2566 ea.nextcmd = NULL;
2567
2568#ifdef FEAT_EVAL
2569 --ex_nesting_level;
2570#endif
2571
2572 return ea.nextcmd;
2573}
2574#if (_MSC_VER == 1200)
Bram Moolenaar281bdce2005-01-25 21:53:18 +00002575 #pragma optimize( "", on )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002576#endif
2577
2578/*
Bram Moolenaareffed932018-08-14 13:38:17 +02002579 * Parse and skip over command modifiers:
2580 * - update eap->cmd
2581 * - store flags in "cmdmod".
2582 * - Set ex_pressedreturn for an empty command line.
2583 * - set msg_silent for ":silent"
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002584 * - set 'eventignore' to "all" for ":noautocmd"
Bram Moolenaareffed932018-08-14 13:38:17 +02002585 * - set p_verbose for ":verbose"
2586 * - Increment "sandbox" for ":sandbox"
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002587 * When "skip_only" is TRUE the global variables are not changed, except for
2588 * "cmdmod".
Bram Moolenaareffed932018-08-14 13:38:17 +02002589 * Return FAIL when the command is not to be executed.
2590 * May set "errormsg" to an error message.
2591 */
2592 int
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002593parse_command_modifiers(exarg_T *eap, char **errormsg, int skip_only)
Bram Moolenaareffed932018-08-14 13:38:17 +02002594{
2595 char_u *p;
2596
2597 vim_memset(&cmdmod, 0, sizeof(cmdmod));
2598 eap->verbose_save = -1;
2599 eap->save_msg_silent = -1;
2600
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002601 // Repeat until no more command modifiers are found.
Bram Moolenaareffed932018-08-14 13:38:17 +02002602 for (;;)
2603 {
Bram Moolenaareffed932018-08-14 13:38:17 +02002604 while (*eap->cmd == ' ' || *eap->cmd == '\t' || *eap->cmd == ':')
2605 ++eap->cmd;
2606
2607 /* in ex mode, an empty line works like :+ */
2608 if (*eap->cmd == NUL && exmode_active
2609 && (getline_equal(eap->getline, eap->cookie, getexmodeline)
2610 || getline_equal(eap->getline, eap->cookie, getexline))
2611 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
2612 {
2613 eap->cmd = (char_u *)"+";
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002614 if (!skip_only)
2615 ex_pressedreturn = TRUE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002616 }
2617
2618 /* ignore comment and empty lines */
2619 if (*eap->cmd == '"')
2620 return FAIL;
2621 if (*eap->cmd == NUL)
2622 {
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002623 if (!skip_only)
2624 ex_pressedreturn = TRUE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002625 return FAIL;
2626 }
2627
Bram Moolenaareffed932018-08-14 13:38:17 +02002628 p = skip_range(eap->cmd, NULL);
2629 switch (*p)
2630 {
2631 /* When adding an entry, also modify cmd_exists(). */
2632 case 'a': if (!checkforcmd(&eap->cmd, "aboveleft", 3))
2633 break;
2634 cmdmod.split |= WSP_ABOVE;
2635 continue;
2636
2637 case 'b': if (checkforcmd(&eap->cmd, "belowright", 3))
2638 {
2639 cmdmod.split |= WSP_BELOW;
2640 continue;
2641 }
2642 if (checkforcmd(&eap->cmd, "browse", 3))
2643 {
2644#ifdef FEAT_BROWSE_CMD
2645 cmdmod.browse = TRUE;
2646#endif
2647 continue;
2648 }
2649 if (!checkforcmd(&eap->cmd, "botright", 2))
2650 break;
2651 cmdmod.split |= WSP_BOT;
2652 continue;
2653
2654 case 'c': if (!checkforcmd(&eap->cmd, "confirm", 4))
2655 break;
2656#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
2657 cmdmod.confirm = TRUE;
2658#endif
2659 continue;
2660
2661 case 'k': if (checkforcmd(&eap->cmd, "keepmarks", 3))
2662 {
2663 cmdmod.keepmarks = TRUE;
2664 continue;
2665 }
2666 if (checkforcmd(&eap->cmd, "keepalt", 5))
2667 {
2668 cmdmod.keepalt = TRUE;
2669 continue;
2670 }
2671 if (checkforcmd(&eap->cmd, "keeppatterns", 5))
2672 {
2673 cmdmod.keeppatterns = TRUE;
2674 continue;
2675 }
2676 if (!checkforcmd(&eap->cmd, "keepjumps", 5))
2677 break;
2678 cmdmod.keepjumps = TRUE;
2679 continue;
2680
2681 case 'f': /* only accept ":filter {pat} cmd" */
2682 {
2683 char_u *reg_pat;
2684
2685 if (!checkforcmd(&p, "filter", 4)
2686 || *p == NUL || ends_excmd(*p))
2687 break;
2688 if (*p == '!')
2689 {
2690 cmdmod.filter_force = TRUE;
2691 p = skipwhite(p + 1);
2692 if (*p == NUL || ends_excmd(*p))
2693 break;
2694 }
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002695 if (skip_only)
2696 p = skip_vimgrep_pat(p, NULL, NULL);
2697 else
2698 // NOTE: This puts a NUL after the pattern.
2699 p = skip_vimgrep_pat(p, &reg_pat, NULL);
Bram Moolenaareffed932018-08-14 13:38:17 +02002700 if (p == NULL || *p == NUL)
2701 break;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002702 if (!skip_only)
2703 {
2704 cmdmod.filter_regmatch.regprog =
Bram Moolenaareffed932018-08-14 13:38:17 +02002705 vim_regcomp(reg_pat, RE_MAGIC);
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002706 if (cmdmod.filter_regmatch.regprog == NULL)
2707 break;
2708 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002709 eap->cmd = p;
2710 continue;
2711 }
2712
2713 /* ":hide" and ":hide | cmd" are not modifiers */
2714 case 'h': if (p != eap->cmd || !checkforcmd(&p, "hide", 3)
2715 || *p == NUL || ends_excmd(*p))
2716 break;
2717 eap->cmd = p;
2718 cmdmod.hide = TRUE;
2719 continue;
2720
2721 case 'l': if (checkforcmd(&eap->cmd, "lockmarks", 3))
2722 {
2723 cmdmod.lockmarks = TRUE;
2724 continue;
2725 }
2726
2727 if (!checkforcmd(&eap->cmd, "leftabove", 5))
2728 break;
2729 cmdmod.split |= WSP_ABOVE;
2730 continue;
2731
2732 case 'n': if (checkforcmd(&eap->cmd, "noautocmd", 3))
2733 {
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002734 if (cmdmod.save_ei == NULL && !skip_only)
Bram Moolenaareffed932018-08-14 13:38:17 +02002735 {
2736 /* Set 'eventignore' to "all". Restore the
2737 * existing option value later. */
2738 cmdmod.save_ei = vim_strsave(p_ei);
2739 set_string_option_direct((char_u *)"ei", -1,
2740 (char_u *)"all", OPT_FREE, SID_NONE);
2741 }
2742 continue;
2743 }
2744 if (!checkforcmd(&eap->cmd, "noswapfile", 3))
2745 break;
2746 cmdmod.noswapfile = TRUE;
2747 continue;
2748
2749 case 'r': if (!checkforcmd(&eap->cmd, "rightbelow", 6))
2750 break;
2751 cmdmod.split |= WSP_BELOW;
2752 continue;
2753
2754 case 's': if (checkforcmd(&eap->cmd, "sandbox", 3))
2755 {
2756#ifdef HAVE_SANDBOX
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002757 if (!skip_only)
2758 {
2759 if (!eap->did_sandbox)
2760 ++sandbox;
2761 eap->did_sandbox = TRUE;
2762 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002763#endif
2764 continue;
2765 }
2766 if (!checkforcmd(&eap->cmd, "silent", 3))
2767 break;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002768 if (!skip_only)
2769 {
2770 if (eap->save_msg_silent == -1)
2771 eap->save_msg_silent = msg_silent;
2772 ++msg_silent;
2773 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002774 if (*eap->cmd == '!' && !VIM_ISWHITE(eap->cmd[-1]))
2775 {
2776 /* ":silent!", but not "silent !cmd" */
2777 eap->cmd = skipwhite(eap->cmd + 1);
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002778 if (!skip_only)
2779 {
2780 ++emsg_silent;
2781 ++eap->did_esilent;
2782 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002783 }
2784 continue;
2785
2786 case 't': if (checkforcmd(&p, "tab", 3))
2787 {
Bram Moolenaar548e5982018-12-26 21:45:00 +01002788 if (!skip_only)
Bram Moolenaareffed932018-08-14 13:38:17 +02002789 {
Bram Moolenaar548e5982018-12-26 21:45:00 +01002790 long tabnr = get_address(eap, &eap->cmd,
2791 ADDR_TABS, eap->skip,
2792 skip_only, FALSE, 1);
2793 if (tabnr == MAXLNUM)
2794 cmdmod.tab = tabpage_index(curtab) + 1;
2795 else
Bram Moolenaareffed932018-08-14 13:38:17 +02002796 {
Bram Moolenaar548e5982018-12-26 21:45:00 +01002797 if (tabnr < 0 || tabnr > LAST_TAB_NR)
2798 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002799 *errormsg = _(e_invrange);
Bram Moolenaar548e5982018-12-26 21:45:00 +01002800 return FAIL;
2801 }
2802 cmdmod.tab = tabnr + 1;
Bram Moolenaareffed932018-08-14 13:38:17 +02002803 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002804 }
2805 eap->cmd = p;
2806 continue;
2807 }
2808 if (!checkforcmd(&eap->cmd, "topleft", 2))
2809 break;
2810 cmdmod.split |= WSP_TOP;
2811 continue;
2812
2813 case 'u': if (!checkforcmd(&eap->cmd, "unsilent", 3))
2814 break;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002815 if (!skip_only)
2816 {
2817 if (eap->save_msg_silent == -1)
2818 eap->save_msg_silent = msg_silent;
2819 msg_silent = 0;
2820 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002821 continue;
2822
2823 case 'v': if (checkforcmd(&eap->cmd, "vertical", 4))
2824 {
2825 cmdmod.split |= WSP_VERT;
2826 continue;
2827 }
2828 if (!checkforcmd(&p, "verbose", 4))
2829 break;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002830 if (!skip_only)
2831 {
2832 if (eap->verbose_save < 0)
2833 eap->verbose_save = p_verbose;
2834 if (vim_isdigit(*eap->cmd))
2835 p_verbose = atoi((char *)eap->cmd);
2836 else
2837 p_verbose = 1;
2838 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002839 eap->cmd = p;
2840 continue;
2841 }
2842 break;
2843 }
2844
2845 return OK;
2846}
2847
2848/*
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002849 * Free contents of "cmdmod".
2850 */
2851 static void
2852free_cmdmod(void)
2853{
2854 if (cmdmod.save_ei != NULL)
2855 {
2856 /* Restore 'eventignore' to the value before ":noautocmd". */
2857 set_string_option_direct((char_u *)"ei", -1, cmdmod.save_ei,
2858 OPT_FREE, SID_NONE);
2859 free_string_option(cmdmod.save_ei);
2860 }
2861
2862 if (cmdmod.filter_regmatch.regprog != NULL)
2863 vim_regfree(cmdmod.filter_regmatch.regprog);
2864}
2865
2866/*
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002867 * Parse the address range, if any, in "eap".
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02002868 * May set the last search pattern, unless "silent" is TRUE.
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002869 * Return FAIL and set "errormsg" or return OK.
2870 */
2871 int
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002872parse_cmd_address(exarg_T *eap, char **errormsg, int silent)
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002873{
2874 int address_count = 1;
2875 linenr_T lnum;
2876
2877 // Repeat for all ',' or ';' separated addresses.
2878 for (;;)
2879 {
2880 eap->line1 = eap->line2;
2881 switch (eap->addr_type)
2882 {
2883 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02002884 case ADDR_OTHER:
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002885 // default is current line number
2886 eap->line2 = curwin->w_cursor.lnum;
2887 break;
2888 case ADDR_WINDOWS:
2889 eap->line2 = CURRENT_WIN_NR;
2890 break;
2891 case ADDR_ARGUMENTS:
2892 eap->line2 = curwin->w_arg_idx + 1;
2893 if (eap->line2 > ARGCOUNT)
2894 eap->line2 = ARGCOUNT;
2895 break;
2896 case ADDR_LOADED_BUFFERS:
2897 case ADDR_BUFFERS:
2898 eap->line2 = curbuf->b_fnum;
2899 break;
2900 case ADDR_TABS:
2901 eap->line2 = CURRENT_TAB_NR;
2902 break;
2903 case ADDR_TABS_RELATIVE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02002904 case ADDR_UNSIGNED:
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002905 eap->line2 = 1;
2906 break;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002907 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02002908#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02002909 eap->line2 = qf_get_cur_idx(eap);
2910#endif
2911 break;
2912 case ADDR_QUICKFIX_VALID:
2913#ifdef FEAT_QUICKFIX
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002914 eap->line2 = qf_get_cur_valid_idx(eap);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002915#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02002916 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02002917 case ADDR_NONE:
2918 // Will give an error later if a range is found.
2919 break;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002920 }
2921 eap->cmd = skipwhite(eap->cmd);
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02002922 lnum = get_address(eap, &eap->cmd, eap->addr_type, eap->skip, silent,
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002923 eap->addr_count == 0, address_count++);
2924 if (eap->cmd == NULL) // error detected
2925 return FAIL;
2926 if (lnum == MAXLNUM)
2927 {
2928 if (*eap->cmd == '%') // '%' - all lines
2929 {
2930 ++eap->cmd;
2931 switch (eap->addr_type)
2932 {
2933 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02002934 case ADDR_OTHER:
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002935 eap->line1 = 1;
2936 eap->line2 = curbuf->b_ml.ml_line_count;
2937 break;
2938 case ADDR_LOADED_BUFFERS:
2939 {
2940 buf_T *buf = firstbuf;
2941
2942 while (buf->b_next != NULL
2943 && buf->b_ml.ml_mfp == NULL)
2944 buf = buf->b_next;
2945 eap->line1 = buf->b_fnum;
2946 buf = lastbuf;
2947 while (buf->b_prev != NULL
2948 && buf->b_ml.ml_mfp == NULL)
2949 buf = buf->b_prev;
2950 eap->line2 = buf->b_fnum;
2951 break;
2952 }
2953 case ADDR_BUFFERS:
2954 eap->line1 = firstbuf->b_fnum;
2955 eap->line2 = lastbuf->b_fnum;
2956 break;
2957 case ADDR_WINDOWS:
2958 case ADDR_TABS:
2959 if (IS_USER_CMDIDX(eap->cmdidx))
2960 {
2961 eap->line1 = 1;
2962 eap->line2 = eap->addr_type == ADDR_WINDOWS
2963 ? LAST_WIN_NR : LAST_TAB_NR;
2964 }
2965 else
2966 {
2967 // there is no Vim command which uses '%' and
2968 // ADDR_WINDOWS or ADDR_TABS
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002969 *errormsg = _(e_invrange);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002970 return FAIL;
2971 }
2972 break;
2973 case ADDR_TABS_RELATIVE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02002974 case ADDR_UNSIGNED:
2975 case ADDR_QUICKFIX:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002976 *errormsg = _(e_invrange);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002977 return FAIL;
2978 case ADDR_ARGUMENTS:
2979 if (ARGCOUNT == 0)
2980 eap->line1 = eap->line2 = 0;
2981 else
2982 {
2983 eap->line1 = 1;
2984 eap->line2 = ARGCOUNT;
2985 }
2986 break;
Bram Moolenaar25190db2019-05-04 15:05:28 +02002987 case ADDR_QUICKFIX_VALID:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02002988#ifdef FEAT_QUICKFIX
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002989 eap->line1 = 1;
Bram Moolenaar25190db2019-05-04 15:05:28 +02002990 eap->line2 = qf_get_valid_size(eap);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002991 if (eap->line2 == 0)
2992 eap->line2 = 1;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002993#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02002994 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02002995 case ADDR_NONE:
2996 // Will give an error later if a range is found.
2997 break;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002998 }
2999 ++eap->addr_count;
3000 }
3001 else if (*eap->cmd == '*' && vim_strchr(p_cpo, CPO_STAR) == NULL)
3002 {
3003 pos_T *fp;
3004
3005 // '*' - visual area
3006 if (eap->addr_type != ADDR_LINES)
3007 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003008 *errormsg = _(e_invrange);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003009 return FAIL;
3010 }
3011
3012 ++eap->cmd;
3013 if (!eap->skip)
3014 {
3015 fp = getmark('<', FALSE);
3016 if (check_mark(fp) == FAIL)
3017 return FAIL;
3018 eap->line1 = fp->lnum;
3019 fp = getmark('>', FALSE);
3020 if (check_mark(fp) == FAIL)
3021 return FAIL;
3022 eap->line2 = fp->lnum;
3023 ++eap->addr_count;
3024 }
3025 }
3026 }
3027 else
3028 eap->line2 = lnum;
3029 eap->addr_count++;
3030
3031 if (*eap->cmd == ';')
3032 {
3033 if (!eap->skip)
3034 {
3035 curwin->w_cursor.lnum = eap->line2;
3036 // don't leave the cursor on an illegal line or column
3037 check_cursor();
3038 }
3039 }
3040 else if (*eap->cmd != ',')
3041 break;
3042 ++eap->cmd;
3043 }
3044
3045 // One address given: set start and end lines.
3046 if (eap->addr_count == 1)
3047 {
3048 eap->line1 = eap->line2;
3049 // ... but only implicit: really no address given
3050 if (lnum == MAXLNUM)
3051 eap->addr_count = 0;
3052 }
3053 return OK;
3054}
3055
3056/*
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00003057 * Check for an Ex command with optional tail.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003058 * If there is a match advance "pp" to the argument and return TRUE.
3059 */
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00003060 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003061checkforcmd(
3062 char_u **pp, /* start of command */
3063 char *cmd, /* name of command */
3064 int len) /* required length */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003065{
3066 int i;
3067
3068 for (i = 0; cmd[i] != NUL; ++i)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003069 if (((char_u *)cmd)[i] != (*pp)[i])
Bram Moolenaar071d4272004-06-13 20:20:40 +00003070 break;
3071 if (i >= len && !isalpha((*pp)[i]))
3072 {
3073 *pp = skipwhite(*pp + i);
3074 return TRUE;
3075 }
3076 return FALSE;
3077}
3078
3079/*
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003080 * Append "cmd" to the error message in IObuff.
3081 * Takes care of limiting the length and handling 0xa0, which would be
3082 * invisible otherwise.
3083 */
3084 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003085append_command(char_u *cmd)
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003086{
3087 char_u *s = cmd;
3088 char_u *d;
3089
3090 STRCAT(IObuff, ": ");
3091 d = IObuff + STRLEN(IObuff);
3092 while (*s != NUL && d - IObuff < IOSIZE - 7)
3093 {
Bram Moolenaar13505972019-01-24 15:04:48 +01003094 if (enc_utf8 ? (s[0] == 0xc2 && s[1] == 0xa0) : *s == 0xa0)
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003095 {
Bram Moolenaar13505972019-01-24 15:04:48 +01003096 s += enc_utf8 ? 2 : 1;
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003097 STRCPY(d, "<a0>");
3098 d += 4;
3099 }
3100 else
3101 MB_COPY_CHAR(s, d);
3102 }
3103 *d = NUL;
3104}
3105
3106/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003107 * Find an Ex command by its name, either built-in or user.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003108 * Start of the name can be found at eap->cmd.
Bram Moolenaar25190db2019-05-04 15:05:28 +02003109 * Sets eap->cmdidx and returns a pointer to char after the command name.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003110 * "full" is set to TRUE if the whole command name matched.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003111 * Returns NULL for an ambiguous user command.
3112 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003113 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003114find_command(exarg_T *eap, int *full UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003115{
3116 int len;
3117 char_u *p;
Bram Moolenaardf177f62005-02-22 08:39:57 +00003118 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003119
3120 /*
3121 * Isolate the command and search for it in the command table.
Bram Moolenaar81870892007-11-11 18:17:28 +00003122 * Exceptions:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003123 * - the 'k' command can directly be followed by any character.
3124 * - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003125 * but :sre[wind] is another command, as are :scr[iptnames],
Bram Moolenaar071d4272004-06-13 20:20:40 +00003126 * :scs[cope], :sim[alt], :sig[ns] and :sil[ent].
Bram Moolenaardf177f62005-02-22 08:39:57 +00003127 * - the "d" command can directly be followed by 'l' or 'p' flag.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003128 */
3129 p = eap->cmd;
3130 if (*p == 'k')
3131 {
3132 eap->cmdidx = CMD_k;
3133 ++p;
3134 }
3135 else if (p[0] == 's'
Bram Moolenaar204b93f2015-08-04 22:02:51 +02003136 && ((p[1] == 'c' && (p[2] == NUL || (p[2] != 's' && p[2] != 'r'
3137 && (p[3] == NUL || (p[3] != 'i' && p[4] != 'p')))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003138 || p[1] == 'g'
3139 || (p[1] == 'i' && p[2] != 'm' && p[2] != 'l' && p[2] != 'g')
3140 || p[1] == 'I'
3141 || (p[1] == 'r' && p[2] != 'e')))
3142 {
3143 eap->cmdidx = CMD_substitute;
3144 ++p;
3145 }
3146 else
3147 {
3148 while (ASCII_ISALPHA(*p))
3149 ++p;
Bram Moolenaarb6590522010-07-21 16:00:43 +02003150 /* for python 3.x support ":py3", ":python3", ":py3file", etc. */
Bram Moolenaar55d5c032010-07-17 23:52:29 +02003151 if (eap->cmd[0] == 'p' && eap->cmd[1] == 'y')
Bram Moolenaarb6590522010-07-21 16:00:43 +02003152 while (ASCII_ISALNUM(*p))
3153 ++p;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02003154
Bram Moolenaar071d4272004-06-13 20:20:40 +00003155 /* check for non-alpha command */
3156 if (p == eap->cmd && vim_strchr((char_u *)"@*!=><&~#", *p) != NULL)
3157 ++p;
3158 len = (int)(p - eap->cmd);
Bram Moolenaardf177f62005-02-22 08:39:57 +00003159 if (*eap->cmd == 'd' && (p[-1] == 'l' || p[-1] == 'p'))
3160 {
3161 /* Check for ":dl", ":dell", etc. to ":deletel": that's
3162 * :delete with the 'l' flag. Same for 'p'. */
3163 for (i = 0; i < len; ++i)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003164 if (eap->cmd[i] != ((char_u *)"delete")[i])
Bram Moolenaardf177f62005-02-22 08:39:57 +00003165 break;
3166 if (i == len - 1)
3167 {
3168 --len;
3169 if (p[-1] == 'l')
3170 eap->flags |= EXFLAG_LIST;
3171 else
3172 eap->flags |= EXFLAG_PRINT;
3173 }
3174 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003175
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003176 if (ASCII_ISLOWER(eap->cmd[0]))
3177 {
Bram Moolenaar6c0c1e82017-03-25 15:07:43 +01003178 int c1 = eap->cmd[0];
Bram Moolenaar94f82cb2019-07-24 22:30:27 +02003179 int c2 = len == 1 ? NUL : eap->cmd[1];
Bram Moolenaar6c0c1e82017-03-25 15:07:43 +01003180
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003181 if (command_count != (int)CMD_SIZE)
3182 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003183 iemsg(_("E943: Command table needs to be updated, run 'make cmdidxs'"));
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003184 getout(1);
3185 }
3186
3187 /* Use a precomputed index for fast look-up in cmdnames[]
3188 * taking into account the first 2 letters of eap->cmd. */
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003189 eap->cmdidx = cmdidxs1[CharOrdLow(c1)];
3190 if (ASCII_ISLOWER(c2))
3191 eap->cmdidx += cmdidxs2[CharOrdLow(c1)][CharOrdLow(c2)];
3192 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003193 else
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003194 eap->cmdidx = CMD_bang;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003195
3196 for ( ; (int)eap->cmdidx < (int)CMD_SIZE;
3197 eap->cmdidx = (cmdidx_T)((int)eap->cmdidx + 1))
3198 if (STRNCMP(cmdnames[(int)eap->cmdidx].cmd_name, (char *)eap->cmd,
3199 (size_t)len) == 0)
3200 {
3201#ifdef FEAT_EVAL
3202 if (full != NULL
3203 && cmdnames[(int)eap->cmdidx].cmd_name[len] == NUL)
3204 *full = TRUE;
3205#endif
3206 break;
3207 }
3208
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003209 // Look for a user defined command as a last resort. Let ":Print" be
3210 // overruled by a user defined command.
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01003211 if ((eap->cmdidx == CMD_SIZE || eap->cmdidx == CMD_Print)
3212 && *eap->cmd >= 'A' && *eap->cmd <= 'Z')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003213 {
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003214 // User defined commands may contain digits.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003215 while (ASCII_ISALNUM(*p))
3216 ++p;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003217 p = find_ucmd(eap, p, full, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003218 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003219 if (p == eap->cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003220 eap->cmdidx = CMD_SIZE;
3221 }
3222
3223 return p;
3224}
3225
3226#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003227static struct cmdmod
3228{
3229 char *name;
3230 int minlen;
3231 int has_count; /* :123verbose :3tab */
3232} cmdmods[] = {
3233 {"aboveleft", 3, FALSE},
3234 {"belowright", 3, FALSE},
3235 {"botright", 2, FALSE},
3236 {"browse", 3, FALSE},
3237 {"confirm", 4, FALSE},
Bram Moolenaar7b668e82016-08-23 23:51:21 +02003238 {"filter", 4, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003239 {"hide", 3, FALSE},
3240 {"keepalt", 5, FALSE},
3241 {"keepjumps", 5, FALSE},
3242 {"keepmarks", 3, FALSE},
Bram Moolenaara939e432013-11-09 05:30:26 +01003243 {"keeppatterns", 5, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003244 {"leftabove", 5, FALSE},
3245 {"lockmarks", 3, FALSE},
Bram Moolenaarca9f9582008-09-18 10:44:28 +00003246 {"noautocmd", 3, FALSE},
Bram Moolenaar5803ae62014-03-23 16:04:02 +01003247 {"noswapfile", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003248 {"rightbelow", 6, FALSE},
3249 {"sandbox", 3, FALSE},
3250 {"silent", 3, FALSE},
3251 {"tab", 3, TRUE},
3252 {"topleft", 2, FALSE},
Bram Moolenaar8e258a42009-07-09 13:55:43 +00003253 {"unsilent", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003254 {"verbose", 4, TRUE},
3255 {"vertical", 4, FALSE},
3256};
3257
3258/*
3259 * Return length of a command modifier (including optional count).
3260 * Return zero when it's not a modifier.
3261 */
3262 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003263modifier_len(char_u *cmd)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003264{
3265 int i, j;
3266 char_u *p = cmd;
3267
3268 if (VIM_ISDIGIT(*cmd))
3269 p = skipwhite(skipdigits(cmd));
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00003270 for (i = 0; i < (int)(sizeof(cmdmods) / sizeof(struct cmdmod)); ++i)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003271 {
3272 for (j = 0; p[j] != NUL; ++j)
3273 if (p[j] != cmdmods[i].name[j])
3274 break;
Bram Moolenaar2d473ab2013-06-12 17:12:24 +02003275 if (!ASCII_ISALPHA(p[j]) && j >= cmdmods[i].minlen
Bram Moolenaared53fb92007-11-24 20:50:24 +00003276 && (p == cmd || cmdmods[i].has_count))
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00003277 return j + (int)(p - cmd);
Bram Moolenaared53fb92007-11-24 20:50:24 +00003278 }
3279 return 0;
3280}
3281
Bram Moolenaar071d4272004-06-13 20:20:40 +00003282/*
3283 * Return > 0 if an Ex command "name" exists.
3284 * Return 2 if there is an exact match.
3285 * Return 3 if there is an ambiguous match.
3286 */
3287 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003288cmd_exists(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003289{
3290 exarg_T ea;
3291 int full = FALSE;
3292 int i;
3293 int j;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003294 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003295
3296 /* Check command modifiers. */
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00003297 for (i = 0; i < (int)(sizeof(cmdmods) / sizeof(struct cmdmod)); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003298 {
3299 for (j = 0; name[j] != NUL; ++j)
3300 if (name[j] != cmdmods[i].name[j])
3301 break;
3302 if (name[j] == NUL && j >= cmdmods[i].minlen)
3303 return (cmdmods[i].name[j] == NUL ? 2 : 1);
3304 }
3305
Bram Moolenaara9587612006-05-04 21:47:50 +00003306 /* Check built-in commands and user defined commands.
3307 * For ":2match" and ":3match" we need to skip the number. */
3308 ea.cmd = (*name == '2' || *name == '3') ? name + 1 : name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003309 ea.cmdidx = (cmdidx_T)0;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003310 p = find_command(&ea, &full);
3311 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003312 return 3;
Bram Moolenaara9587612006-05-04 21:47:50 +00003313 if (vim_isdigit(*name) && ea.cmdidx != CMD_match)
3314 return 0;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003315 if (*skipwhite(p) != NUL)
3316 return 0; /* trailing garbage */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003317 return (ea.cmdidx == CMD_SIZE ? 0 : (full ? 2 : 1));
3318}
3319#endif
3320
Bram Moolenaard0190392019-08-23 21:17:35 +02003321 cmdidx_T
3322excmd_get_cmdidx(char_u *cmd, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003323{
Bram Moolenaard0190392019-08-23 21:17:35 +02003324 cmdidx_T idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003325
Bram Moolenaard0190392019-08-23 21:17:35 +02003326 for (idx = (cmdidx_T)0; (int)idx < (int)CMD_SIZE;
3327 idx = (cmdidx_T)((int)idx + 1))
3328 if (STRNCMP(cmdnames[(int)idx].cmd_name, cmd, (size_t)len) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003329 break;
3330
Bram Moolenaard0190392019-08-23 21:17:35 +02003331 return idx;
3332}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003333
Bram Moolenaard0190392019-08-23 21:17:35 +02003334 long
3335excmd_get_argt(cmdidx_T idx)
3336{
3337 return (long)cmdnames[(int)idx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003338}
3339
3340/*
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003341 * Skip a range specifier of the form: addr [,addr] [;addr] ..
Bram Moolenaar071d4272004-06-13 20:20:40 +00003342 *
3343 * Backslashed delimiters after / or ? will be skipped, and commands will
3344 * not be expanded between /'s and ?'s or after "'".
3345 *
Bram Moolenaardf177f62005-02-22 08:39:57 +00003346 * Also skip white space and ":" characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003347 * Returns the "cmd" pointer advanced to beyond the range.
3348 */
3349 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003350skip_range(
3351 char_u *cmd,
3352 int *ctx) /* pointer to xp_context or NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003353{
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003354 unsigned delim;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003355
Bram Moolenaarcbf20fb2017-02-03 21:19:04 +01003356 while (vim_strchr((char_u *)" \t0123456789.$%'/?-+,;\\", *cmd) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003357 {
Bram Moolenaarcbf20fb2017-02-03 21:19:04 +01003358 if (*cmd == '\\')
3359 {
3360 if (cmd[1] == '?' || cmd[1] == '/' || cmd[1] == '&')
3361 ++cmd;
3362 else
3363 break;
3364 }
3365 else if (*cmd == '\'')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003366 {
3367 if (*++cmd == NUL && ctx != NULL)
3368 *ctx = EXPAND_NOTHING;
3369 }
3370 else if (*cmd == '/' || *cmd == '?')
3371 {
3372 delim = *cmd++;
3373 while (*cmd != NUL && *cmd != delim)
3374 if (*cmd++ == '\\' && *cmd != NUL)
3375 ++cmd;
3376 if (*cmd == NUL && ctx != NULL)
3377 *ctx = EXPAND_NOTHING;
3378 }
3379 if (*cmd != NUL)
3380 ++cmd;
3381 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00003382
3383 /* Skip ":" and white space. */
3384 while (*cmd == ':')
3385 cmd = skipwhite(cmd + 1);
3386
Bram Moolenaar071d4272004-06-13 20:20:40 +00003387 return cmd;
3388}
3389
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02003390 static void
3391addr_error(cmd_addr_T addr_type)
3392{
3393 if (addr_type == ADDR_NONE)
3394 emsg(_(e_norange));
3395 else
3396 emsg(_(e_invrange));
3397}
3398
Bram Moolenaar071d4272004-06-13 20:20:40 +00003399/*
Bram Moolenaar198cb662018-09-06 21:44:17 +02003400 * Get a single EX address.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003401 *
3402 * Set ptr to the next character after the part that was interpreted.
3403 * Set ptr to NULL when an error is encountered.
Bram Moolenaar198cb662018-09-06 21:44:17 +02003404 * This may set the last used search pattern.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003405 *
3406 * Return MAXLNUM when no Ex address was found.
3407 */
3408 static linenr_T
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003409get_address(
3410 exarg_T *eap UNUSED,
3411 char_u **ptr,
Bram Moolenaarb7316892019-05-01 18:08:42 +02003412 cmd_addr_T addr_type_arg,
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02003413 int skip, // only skip the address, don't use it
3414 int silent, // no errors or side effects
3415 int to_other_file, // flag: may jump to other file
3416 int address_count UNUSED) // 1 for first address, >1 after comma
Bram Moolenaar071d4272004-06-13 20:20:40 +00003417{
Bram Moolenaarb7316892019-05-01 18:08:42 +02003418 cmd_addr_T addr_type = addr_type_arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003419 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 {
3434 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
3475 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
3529 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 {
3545 /* Only accept a mark in another file when it is
3546 * used by itself: ":'M". */
3547 fp = getmark(*cmd, to_other_file && cmd[1] == NUL);
3548 ++cmd;
3549 if (fp == (pos_T *)-1)
3550 /* Jumped to another file. */
3551 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 '/':
3565 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 }
3573 if (skip) /* skip "/pat/" */
3574 {
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;
3610 /* adjust command string pointer */
3611 cmd += searchcmdlen;
3612 }
3613 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003614
3615 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 Moolenaarb96a7f32014-11-27 16:22:48 +01003668 if (VIM_ISDIGIT(*cmd)) /* absolute line number */
3669 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))
3721 i = '+'; /* "number" is same as "+number" */
3722 else
3723 i = *cmd++;
3724 if (!VIM_ISDIGIT(*cmd)) /* '+' is '+1', but '+0' is not '+1' */
3725 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
3742 /* Relative line addressing, need to adjust for folded lines
3743 * now, but only do it after the first address. */
3744 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 Moolenaarc0a37b92015-02-03 19:10:53 +01003831 /* add 1 if ARGCOUNT is 0 */
3832 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 {
3979 /* replace $* by given arguments */
3980 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)
3986 return NULL; /* out of memory */
3987 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)
4002 return NULL; /* out of memory */
4003 STRCPY(new_cmdline, program);
4004 STRCAT(new_cmdline, " ");
4005 STRCAT(new_cmdline, p);
4006 }
4007 msg_make(p);
4008
4009 /* 'eap->cmd' is not set here, because it is not used at CMD_make */
4010 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{
4029 int has_wildcards; /* need to expand wildcards */
4030 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
4037 /* Skip a regexp pattern for ":vimgrep[add] pat file..." */
4038 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
4052 /* Skip over `=expr`, wildcards in it are not expanded. */
4053 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 Moolenaar071d4272004-06-13 20:20:40 +00004077 if (*errormsgp != NULL) /* error detected */
4078 return FAIL;
4079 if (repl == NULL) /* no match found */
4080 {
4081 p += srclen;
4082 continue;
4083 }
4084
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00004085 /* Wildcards won't be expanded below, the replacement is taken
4086 * literally. But do expand "~/file", "~user/file" and "$HOME/file". */
4087 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 Moolenaar63b92542007-03-27 14:57:09 +00004095 /* 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).
4101 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004102 if (!eap->usefilter
Bram Moolenaar63b92542007-03-27 14:57:09 +00004103 && !escaped
Bram Moolenaar071d4272004-06-13 20:20:40 +00004104 && eap->cmdidx != CMD_bang
Bram Moolenaar071d4272004-06-13 20:20:40 +00004105 && eap->cmdidx != CMD_grep
4106 && eap->cmdidx != CMD_grepadd
Bram Moolenaarbf15b8d2017-06-04 20:43:48 +02004107 && eap->cmdidx != CMD_hardcopy
Bram Moolenaar67883b42017-07-27 22:57:00 +02004108 && eap->cmdidx != CMD_lgrep
4109 && eap->cmdidx != CMD_lgrepadd
4110 && eap->cmdidx != CMD_lmake
4111 && eap->cmdidx != CMD_make
4112 && eap->cmdidx != CMD_terminal
Bram Moolenaar071d4272004-06-13 20:20:40 +00004113#ifndef UNIX
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004114 && !(eap->argt & EX_NOSPC)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004115#endif
4116 )
4117 {
4118 char_u *l;
4119#ifdef BACKSLASH_IN_FILENAME
4120 /* Don't escape a backslash here, because rem_backslash() doesn't
4121 * remove it later. */
4122 static char_u *nobslash = (char_u *)" \t\"|";
4123# define ESCAPE_CHARS nobslash
4124#else
4125# define ESCAPE_CHARS escape_chars
4126#endif
4127
4128 for (l = repl; *l; ++l)
4129 if (vim_strchr(ESCAPE_CHARS, *l) != NULL)
4130 {
4131 l = vim_strsave_escaped(repl, ESCAPE_CHARS);
4132 if (l != NULL)
4133 {
4134 vim_free(repl);
4135 repl = l;
4136 }
4137 break;
4138 }
4139 }
4140
4141 /* For a shell command a '!' must be escaped. */
Bram Moolenaar67883b42017-07-27 22:57:00 +02004142 if ((eap->usefilter || eap->cmdidx == CMD_bang
4143 || eap->cmdidx == CMD_terminal)
Bram Moolenaar31b7d382014-04-01 18:54:48 +02004144 && vim_strpbrk(repl, (char_u *)"!") != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004145 {
4146 char_u *l;
4147
Bram Moolenaar31b7d382014-04-01 18:54:48 +02004148 l = vim_strsave_escaped(repl, (char_u *)"!");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004149 if (l != NULL)
4150 {
4151 vim_free(repl);
4152 repl = l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004153 }
4154 }
4155
4156 p = repl_cmdline(eap, p, srclen, repl, cmdlinep);
4157 vim_free(repl);
4158 if (p == NULL)
4159 return FAIL;
4160 }
4161
4162 /*
4163 * One file argument: Expand wildcards.
4164 * Don't do this with ":r !command" or ":w !command".
4165 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004166 if ((eap->argt & EX_NOSPC) && !eap->usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004167 {
4168 /*
4169 * May do this twice:
4170 * 1. Replace environment variables.
4171 * 2. Replace any other wildcards, remove backslashes.
4172 */
4173 for (n = 1; n <= 2; ++n)
4174 {
4175 if (n == 2)
4176 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004177 /*
4178 * Halve the number of backslashes (this is Vi compatible).
4179 * For Unix and OS/2, when wildcards are expanded, this is
4180 * done by ExpandOne() below.
4181 */
Bram Moolenaare7fedb62015-12-31 19:07:19 +01004182#if defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004183 if (!has_wildcards)
4184#endif
4185 backslash_halve(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004186 }
4187
4188 if (has_wildcards)
4189 {
4190 if (n == 1)
4191 {
4192 /*
4193 * First loop: May expand environment variables. This
4194 * can be done much faster with expand_env() than with
4195 * something else (e.g., calling a shell).
4196 * After expanding environment variables, check again
4197 * if there are still wildcards present.
4198 */
4199 if (vim_strchr(eap->arg, '$') != NULL
4200 || vim_strchr(eap->arg, '~') != NULL)
4201 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +00004202 expand_env_esc(eap->arg, NameBuff, MAXPATHL,
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00004203 TRUE, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004204 has_wildcards = mch_has_wildcard(NameBuff);
4205 p = NameBuff;
4206 }
4207 else
4208 p = NULL;
4209 }
4210 else /* n == 2 */
4211 {
4212 expand_T xpc;
Bram Moolenaarb40c2572019-10-19 21:01:05 +02004213 int options = WILD_LIST_NOTFOUND
4214 | WILD_NOERROR | WILD_ADD_SLASH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004215
4216 ExpandInit(&xpc);
4217 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar94950a92010-12-02 16:01:29 +01004218 if (p_wic)
4219 options += WILD_ICASE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004220 p = ExpandOne(&xpc, eap->arg, NULL,
Bram Moolenaar94950a92010-12-02 16:01:29 +01004221 options, WILD_EXPAND_FREE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004222 if (p == NULL)
4223 return FAIL;
4224 }
4225 if (p != NULL)
4226 {
4227 (void)repl_cmdline(eap, eap->arg, (int)STRLEN(eap->arg),
4228 p, cmdlinep);
4229 if (n == 2) /* p came from ExpandOne() */
4230 vim_free(p);
4231 }
4232 }
4233 }
4234 }
4235 return OK;
4236}
4237
4238/*
4239 * Replace part of the command line, keeping eap->cmd, eap->arg and
4240 * eap->nextcmd correct.
4241 * "src" points to the part that is to be replaced, of length "srclen".
4242 * "repl" is the replacement string.
4243 * Returns a pointer to the character after the replaced string.
4244 * Returns NULL for failure.
4245 */
4246 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004247repl_cmdline(
4248 exarg_T *eap,
4249 char_u *src,
4250 int srclen,
4251 char_u *repl,
4252 char_u **cmdlinep)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004253{
4254 int len;
4255 int i;
4256 char_u *new_cmdline;
4257
4258 /*
4259 * The new command line is build in new_cmdline[].
4260 * First allocate it.
4261 * Careful: a "+cmd" argument may have been NUL terminated.
4262 */
4263 len = (int)STRLEN(repl);
4264 i = (int)(src - *cmdlinep) + (int)STRLEN(src + srclen) + len + 3;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004265 if (eap->nextcmd != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004266 i += (int)STRLEN(eap->nextcmd);/* add space for next command */
Bram Moolenaar964b3742019-05-24 18:54:09 +02004267 if ((new_cmdline = alloc(i)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004268 return NULL; /* out of memory! */
4269
4270 /*
4271 * Copy the stuff before the expanded part.
4272 * Copy the expanded stuff.
4273 * Copy what came after the expanded part.
4274 * Copy the next commands, if there are any.
4275 */
4276 i = (int)(src - *cmdlinep); /* length of part before match */
4277 mch_memmove(new_cmdline, *cmdlinep, (size_t)i);
Bram Moolenaara3ffd9c2005-07-21 21:03:15 +00004278
Bram Moolenaar071d4272004-06-13 20:20:40 +00004279 mch_memmove(new_cmdline + i, repl, (size_t)len);
4280 i += len; /* remember the end of the string */
4281 STRCPY(new_cmdline + i, src + srclen);
4282 src = new_cmdline + i; /* remember where to continue */
4283
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004284 if (eap->nextcmd != NULL) /* append next command */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004285 {
4286 i = (int)STRLEN(new_cmdline) + 1;
4287 STRCPY(new_cmdline + i, eap->nextcmd);
4288 eap->nextcmd = new_cmdline + i;
4289 }
4290 eap->cmd = new_cmdline + (eap->cmd - *cmdlinep);
4291 eap->arg = new_cmdline + (eap->arg - *cmdlinep);
4292 if (eap->do_ecmd_cmd != NULL && eap->do_ecmd_cmd != dollar_command)
4293 eap->do_ecmd_cmd = new_cmdline + (eap->do_ecmd_cmd - *cmdlinep);
4294 vim_free(*cmdlinep);
4295 *cmdlinep = new_cmdline;
4296
4297 return src;
4298}
4299
4300/*
4301 * Check for '|' to separate commands and '"' to start comments.
4302 */
4303 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004304separate_nextcmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004305{
4306 char_u *p;
4307
Bram Moolenaar86b68352004-12-27 21:59:20 +00004308#ifdef FEAT_QUICKFIX
Bram Moolenaar748bf032005-02-02 23:04:36 +00004309 p = skip_grep_pat(eap);
4310#else
4311 p = eap->arg;
Bram Moolenaar86b68352004-12-27 21:59:20 +00004312#endif
4313
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004314 for ( ; *p; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004315 {
4316 if (*p == Ctrl_V)
4317 {
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004318 if (eap->argt & (EX_CTRLV | EX_XFILE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004319 ++p; /* skip CTRL-V and next char */
4320 else
Bram Moolenaarb0db5692007-08-14 20:54:49 +00004321 /* remove CTRL-V and skip next char */
Bram Moolenaara7241f52008-06-24 20:39:31 +00004322 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004323 if (*p == NUL) /* stop at NUL after CTRL-V */
4324 break;
4325 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004326
4327#ifdef FEAT_EVAL
4328 /* Skip over `=expr` when wildcards are expanded. */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004329 else if (p[0] == '`' && p[1] == '=' && (eap->argt & EX_XFILE))
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004330 {
4331 p += 2;
4332 (void)skip_expr(&p);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004333 }
4334#endif
4335
Bram Moolenaar071d4272004-06-13 20:20:40 +00004336 /* Check for '"': start of comment or '|': next command */
4337 /* :@" and :*" do not start a comment!
4338 * :redir @" doesn't either. */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004339 else if ((*p == '"' && !(eap->argt & EX_NOTRLCOM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004340 && ((eap->cmdidx != CMD_at && eap->cmdidx != CMD_star)
4341 || p != eap->arg)
4342 && (eap->cmdidx != CMD_redir
4343 || p != eap->arg + 1 || p[-1] != '@'))
4344 || *p == '|' || *p == '\n')
4345 {
4346 /*
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004347 * We remove the '\' before the '|', unless EX_CTRLV is used
Bram Moolenaar071d4272004-06-13 20:20:40 +00004348 * AND 'b' is present in 'cpoptions'.
4349 */
4350 if ((vim_strchr(p_cpo, CPO_BAR) == NULL
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004351 || !(eap->argt & EX_CTRLV)) && *(p - 1) == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004352 {
Bram Moolenaara7241f52008-06-24 20:39:31 +00004353 STRMOVE(p - 1, p); /* remove the '\' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004354 --p;
4355 }
4356 else
4357 {
4358 eap->nextcmd = check_nextcmd(p);
4359 *p = NUL;
4360 break;
4361 }
4362 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004363 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00004364
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004365 if (!(eap->argt & EX_NOTRLCOM)) /* remove trailing spaces */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004366 del_trailing_spaces(eap->arg);
4367}
4368
4369/*
4370 * get + command from ex argument
4371 */
4372 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004373getargcmd(char_u **argp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004374{
4375 char_u *arg = *argp;
4376 char_u *command = NULL;
4377
4378 if (*arg == '+') /* +[command] */
4379 {
4380 ++arg;
Bram Moolenaar3e451592014-04-02 14:22:05 +02004381 if (vim_isspace(*arg) || *arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004382 command = dollar_command;
4383 else
4384 {
4385 command = arg;
4386 arg = skip_cmd_arg(command, TRUE);
4387 if (*arg != NUL)
4388 *arg++ = NUL; /* terminate command with NUL */
4389 }
4390
4391 arg = skipwhite(arg); /* skip over spaces */
4392 *argp = arg;
4393 }
4394 return command;
4395}
4396
4397/*
4398 * Find end of "+command" argument. Skip over "\ " and "\\".
4399 */
Bram Moolenaard0190392019-08-23 21:17:35 +02004400 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004401skip_cmd_arg(
4402 char_u *p,
4403 int rembs) /* TRUE to halve the number of backslashes */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004404{
4405 while (*p && !vim_isspace(*p))
4406 {
4407 if (*p == '\\' && p[1] != NUL)
4408 {
4409 if (rembs)
Bram Moolenaara7241f52008-06-24 20:39:31 +00004410 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004411 else
4412 ++p;
4413 }
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004414 MB_PTR_ADV(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004415 }
4416 return p;
4417}
4418
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004419 int
4420get_bad_opt(char_u *p, exarg_T *eap)
4421{
4422 if (STRICMP(p, "keep") == 0)
4423 eap->bad_char = BAD_KEEP;
4424 else if (STRICMP(p, "drop") == 0)
4425 eap->bad_char = BAD_DROP;
4426 else if (MB_BYTE2LEN(*p) == 1 && p[1] == NUL)
4427 eap->bad_char = *p;
Bram Moolenaar75808492018-06-12 12:39:41 +02004428 else
4429 return FAIL;
4430 return OK;
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004431}
4432
Bram Moolenaar071d4272004-06-13 20:20:40 +00004433/*
4434 * Get "++opt=arg" argument.
4435 * Return FAIL or OK.
4436 */
4437 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004438getargopt(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004439{
4440 char_u *arg = eap->arg + 2;
4441 int *pp = NULL;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02004442 int bad_char_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004443 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004444
4445 /* ":edit ++[no]bin[ary] file" */
4446 if (STRNCMP(arg, "bin", 3) == 0 || STRNCMP(arg, "nobin", 5) == 0)
4447 {
4448 if (*arg == 'n')
4449 {
4450 arg += 2;
4451 eap->force_bin = FORCE_NOBIN;
4452 }
4453 else
4454 eap->force_bin = FORCE_BIN;
4455 if (!checkforcmd(&arg, "binary", 3))
4456 return FAIL;
4457 eap->arg = skipwhite(arg);
4458 return OK;
4459 }
4460
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004461 /* ":read ++edit file" */
4462 if (STRNCMP(arg, "edit", 4) == 0)
4463 {
4464 eap->read_edit = TRUE;
4465 eap->arg = skipwhite(arg + 4);
4466 return OK;
4467 }
4468
Bram Moolenaar071d4272004-06-13 20:20:40 +00004469 if (STRNCMP(arg, "ff", 2) == 0)
4470 {
4471 arg += 2;
4472 pp = &eap->force_ff;
4473 }
4474 else if (STRNCMP(arg, "fileformat", 10) == 0)
4475 {
4476 arg += 10;
4477 pp = &eap->force_ff;
4478 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004479 else if (STRNCMP(arg, "enc", 3) == 0)
4480 {
Bram Moolenaarb38e9ab2011-12-14 14:49:45 +01004481 if (STRNCMP(arg, "encoding", 8) == 0)
4482 arg += 8;
4483 else
4484 arg += 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004485 pp = &eap->force_enc;
4486 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004487 else if (STRNCMP(arg, "bad", 3) == 0)
4488 {
4489 arg += 3;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02004490 pp = &bad_char_idx;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004491 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004492
4493 if (pp == NULL || *arg != '=')
4494 return FAIL;
4495
4496 ++arg;
4497 *pp = (int)(arg - eap->cmd);
4498 arg = skip_cmd_arg(arg, FALSE);
4499 eap->arg = skipwhite(arg);
4500 *arg = NUL;
4501
Bram Moolenaar071d4272004-06-13 20:20:40 +00004502 if (pp == &eap->force_ff)
4503 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004504 if (check_ff_value(eap->cmd + eap->force_ff) == FAIL)
4505 return FAIL;
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004506 eap->force_ff = eap->cmd[eap->force_ff];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004507 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004508 else if (pp == &eap->force_enc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004509 {
4510 /* Make 'fileencoding' lower case. */
4511 for (p = eap->cmd + eap->force_enc; *p != NUL; ++p)
4512 *p = TOLOWER_ASC(*p);
4513 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004514 else
4515 {
4516 /* Check ++bad= argument. Must be a single-byte character, "keep" or
4517 * "drop". */
Bram Moolenaar333b80a2018-04-04 22:57:29 +02004518 if (get_bad_opt(eap->cmd + bad_char_idx, eap) == FAIL)
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00004519 return FAIL;
4520 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004521
4522 return OK;
4523}
4524
Bram Moolenaar071d4272004-06-13 20:20:40 +00004525 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004526ex_autocmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004527{
4528 /*
Bram Moolenaar26d4b892018-07-04 22:26:28 +02004529 * Disallow autocommands from .exrc and .vimrc in current
Bram Moolenaar071d4272004-06-13 20:20:40 +00004530 * directory for security reasons.
4531 */
4532 if (secure)
4533 {
4534 secure = 2;
4535 eap->errmsg = e_curdir;
4536 }
4537 else if (eap->cmdidx == CMD_autocmd)
4538 do_autocmd(eap->arg, eap->forceit);
4539 else
4540 do_augroup(eap->arg, eap->forceit);
4541}
4542
4543/*
4544 * ":doautocmd": Apply the automatic commands to the current buffer.
4545 */
4546 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004547ex_doautocmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004548{
Bram Moolenaar60542ac2012-02-12 20:14:01 +01004549 char_u *arg = eap->arg;
4550 int call_do_modelines = check_nomodeline(&arg);
Bram Moolenaar1610d052016-06-09 22:53:01 +02004551 int did_aucmd;
Bram Moolenaar60542ac2012-02-12 20:14:01 +01004552
Bram Moolenaar1610d052016-06-09 22:53:01 +02004553 (void)do_doautocmd(arg, TRUE, &did_aucmd);
4554 /* Only when there is no <nomodeline>. */
4555 if (call_do_modelines && did_aucmd)
Bram Moolenaar60542ac2012-02-12 20:14:01 +01004556 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004557}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004558
Bram Moolenaar071d4272004-06-13 20:20:40 +00004559/*
4560 * :[N]bunload[!] [N] [bufname] unload buffer
4561 * :[N]bdelete[!] [N] [bufname] delete buffer from buffer list
4562 * :[N]bwipeout[!] [N] [bufname] delete buffer really
4563 */
4564 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004565ex_bunload(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004566{
Bram Moolenaar8cdbd5b2019-06-16 15:50:45 +02004567 if (ERROR_IF_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02004568 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004569 eap->errmsg = do_bufdel(
4570 eap->cmdidx == CMD_bdelete ? DOBUF_DEL
4571 : eap->cmdidx == CMD_bwipeout ? DOBUF_WIPE
4572 : DOBUF_UNLOAD, eap->arg,
4573 eap->addr_count, (int)eap->line1, (int)eap->line2, eap->forceit);
4574}
4575
4576/*
4577 * :[N]buffer [N] to buffer N
4578 * :[N]sbuffer [N] to buffer N
4579 */
4580 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004581ex_buffer(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004582{
Bram Moolenaar8cdbd5b2019-06-16 15:50:45 +02004583 if (ERROR_IF_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02004584 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004585 if (*eap->arg)
4586 eap->errmsg = e_trailing;
4587 else
4588 {
4589 if (eap->addr_count == 0) /* default is current buffer */
4590 goto_buffer(eap, DOBUF_CURRENT, FORWARD, 0);
4591 else
4592 goto_buffer(eap, DOBUF_FIRST, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02004593 if (eap->do_ecmd_cmd != NULL)
4594 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004595 }
4596}
4597
4598/*
4599 * :[N]bmodified [N] to next mod. buffer
4600 * :[N]sbmodified [N] to next mod. buffer
4601 */
4602 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004603ex_bmodified(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004604{
4605 goto_buffer(eap, DOBUF_MOD, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02004606 if (eap->do_ecmd_cmd != NULL)
4607 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004608}
4609
4610/*
4611 * :[N]bnext [N] to next buffer
4612 * :[N]sbnext [N] split and to next buffer
4613 */
4614 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004615ex_bnext(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004616{
4617 goto_buffer(eap, DOBUF_CURRENT, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02004618 if (eap->do_ecmd_cmd != NULL)
4619 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004620}
4621
4622/*
4623 * :[N]bNext [N] to previous buffer
4624 * :[N]bprevious [N] to previous buffer
4625 * :[N]sbNext [N] split and to previous buffer
4626 * :[N]sbprevious [N] split and to previous buffer
4627 */
4628 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004629ex_bprevious(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004630{
4631 goto_buffer(eap, DOBUF_CURRENT, BACKWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02004632 if (eap->do_ecmd_cmd != NULL)
4633 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004634}
4635
4636/*
4637 * :brewind to first buffer
4638 * :bfirst to first buffer
4639 * :sbrewind split and to first buffer
4640 * :sbfirst split and to first buffer
4641 */
4642 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004643ex_brewind(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004644{
4645 goto_buffer(eap, DOBUF_FIRST, FORWARD, 0);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02004646 if (eap->do_ecmd_cmd != NULL)
4647 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004648}
4649
4650/*
4651 * :blast to last buffer
4652 * :sblast split and to last buffer
4653 */
4654 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004655ex_blast(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004656{
4657 goto_buffer(eap, DOBUF_LAST, BACKWARD, 0);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02004658 if (eap->do_ecmd_cmd != NULL)
4659 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004660}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004661
4662 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004663ends_excmd(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004664{
4665 return (c == NUL || c == '|' || c == '"' || c == '\n');
4666}
4667
4668#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA) || defined(FEAT_EVAL) \
4669 || defined(PROTO)
4670/*
4671 * Return the next command, after the first '|' or '\n'.
4672 * Return NULL if not found.
4673 */
4674 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004675find_nextcmd(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004676{
4677 while (*p != '|' && *p != '\n')
4678 {
4679 if (*p == NUL)
4680 return NULL;
4681 ++p;
4682 }
4683 return (p + 1);
4684}
4685#endif
4686
4687/*
Bram Moolenaar2256c992016-11-15 21:17:07 +01004688 * Check if *p is a separator between Ex commands, skipping over white space.
4689 * Return NULL if it isn't, the following character if it is.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004690 */
4691 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004692check_nextcmd(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004693{
Bram Moolenaar2256c992016-11-15 21:17:07 +01004694 char_u *s = skipwhite(p);
4695
4696 if (*s == '|' || *s == '\n')
4697 return (s + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004698 else
4699 return NULL;
4700}
4701
4702/*
4703 * - if there are more files to edit
4704 * - and this is the last window
4705 * - and forceit not used
4706 * - and not repeated twice on a row
4707 * return FAIL and give error message if 'message' TRUE
4708 * return OK otherwise
4709 */
4710 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004711check_more(
4712 int message, /* when FALSE check only, no messages */
4713 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004714{
4715 int n = ARGCOUNT - curwin->w_arg_idx - 1;
4716
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00004717 if (!forceit && only_one_window()
4718 && ARGCOUNT > 1 && !arg_had_last && n >= 0 && quitmore == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004719 {
4720 if (message)
4721 {
4722#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
4723 if ((p_confirm || cmdmod.confirm) && curbuf->b_fname != NULL)
4724 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02004725 char_u buff[DIALOG_MSG_SIZE];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004726
Bram Moolenaarda6e8912018-08-21 15:12:14 +02004727 vim_snprintf((char *)buff, DIALOG_MSG_SIZE,
4728 NGETTEXT("%d more file to edit. Quit anyway?",
4729 "%d more files to edit. Quit anyway?", n), n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004730 if (vim_dialog_yesno(VIM_QUESTION, NULL, buff, 1) == VIM_YES)
4731 return OK;
4732 return FAIL;
4733 }
4734#endif
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01004735 semsg(NGETTEXT("E173: %d more file to edit",
4736 "E173: %d more files to edit", n), n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004737 quitmore = 2; /* next try to quit is allowed */
4738 }
4739 return FAIL;
4740 }
4741 return OK;
4742}
4743
Bram Moolenaar071d4272004-06-13 20:20:40 +00004744/*
4745 * Function given to ExpandGeneric() to obtain the list of command names.
4746 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004747 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004748get_command_name(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004749{
4750 if (idx >= (int)CMD_SIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004751 return get_user_command_name(idx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004752 return cmdnames[idx].cmd_name;
4753}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004754
Bram Moolenaar071d4272004-06-13 20:20:40 +00004755 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004756ex_colorscheme(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004757{
Bram Moolenaare6850792010-05-14 15:28:44 +02004758 if (*eap->arg == NUL)
4759 {
4760#ifdef FEAT_EVAL
4761 char_u *expr = vim_strsave((char_u *)"g:colors_name");
4762 char_u *p = NULL;
4763
4764 if (expr != NULL)
4765 {
4766 ++emsg_off;
4767 p = eval_to_string(expr, NULL, FALSE);
4768 --emsg_off;
4769 vim_free(expr);
4770 }
4771 if (p != NULL)
4772 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01004773 msg((char *)p);
Bram Moolenaare6850792010-05-14 15:28:44 +02004774 vim_free(p);
4775 }
4776 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01004777 msg("default");
Bram Moolenaare6850792010-05-14 15:28:44 +02004778#else
Bram Moolenaar32526b32019-01-19 17:43:09 +01004779 msg(_("unknown"));
Bram Moolenaare6850792010-05-14 15:28:44 +02004780#endif
4781 }
4782 else if (load_colors(eap->arg) == FAIL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004783 semsg(_("E185: Cannot find color scheme '%s'"), eap->arg);
Bram Moolenaarf58d81a2019-01-28 20:19:05 +01004784
4785#ifdef FEAT_VTP
4786 else if (has_vtp_working())
4787 {
4788 // background color change requires clear + redraw
4789 update_screen(CLEAR);
4790 redrawcmd();
4791 }
4792#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004793}
4794
4795 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004796ex_highlight(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004797{
4798 if (*eap->arg == NUL && eap->cmd[2] == '!')
Bram Moolenaar32526b32019-01-19 17:43:09 +01004799 msg(_("Greetings, Vim user!"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004800 do_highlight(eap->arg, eap->forceit, FALSE);
4801}
4802
4803
4804/*
4805 * Call this function if we thought we were going to exit, but we won't
4806 * (because of an error). May need to restore the terminal mode.
4807 */
4808 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004809not_exiting(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004810{
4811 exiting = FALSE;
4812 settmode(TMODE_RAW);
4813}
4814
Bram Moolenaar12a96de2018-03-11 14:44:18 +01004815 static int
4816before_quit_autocmds(win_T *wp, int quit_all, int forceit)
4817{
4818 apply_autocmds(EVENT_QUITPRE, NULL, NULL, FALSE, wp->w_buffer);
4819
Bram Moolenaar34ba06b2019-10-20 22:27:10 +02004820 // Bail out when autocommands closed the window.
4821 // Refuse to quit when the buffer in the last window is being closed (can
4822 // only happen in autocommands).
Bram Moolenaar12a96de2018-03-11 14:44:18 +01004823 if (!win_valid(wp)
4824 || curbuf_locked()
4825 || (wp->w_buffer->b_nwindows == 1 && wp->w_buffer->b_locked > 0))
4826 return TRUE;
4827
4828 if (quit_all || (check_more(FALSE, forceit) == OK && only_one_window()))
4829 {
4830 apply_autocmds(EVENT_EXITPRE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar34ba06b2019-10-20 22:27:10 +02004831 // Refuse to quit when locked or when the window was closed or the
4832 // buffer in the last window is being closed (can only happen in
4833 // autocommands).
4834 if (!win_valid(wp) || curbuf_locked()
Bram Moolenaar12a96de2018-03-11 14:44:18 +01004835 || (curbuf->b_nwindows == 1 && curbuf->b_locked > 0))
4836 return TRUE;
4837 }
4838
4839 return FALSE;
4840}
4841
Bram Moolenaar071d4272004-06-13 20:20:40 +00004842/*
Bram Moolenaarf240e182014-11-27 18:33:02 +01004843 * ":quit": quit current window, quit Vim if the last window is closed.
Bram Moolenaar12a96de2018-03-11 14:44:18 +01004844 * ":{nr}quit": quit window {nr}
Bram Moolenaar4d14bac2019-10-20 21:15:15 +02004845 * Also used when closing a terminal window that's the last one.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004846 */
Bram Moolenaar4d14bac2019-10-20 21:15:15 +02004847 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004848ex_quit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004849{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004850 win_T *wp;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004851
Bram Moolenaar071d4272004-06-13 20:20:40 +00004852#ifdef FEAT_CMDWIN
4853 if (cmdwin_type != 0)
4854 {
4855 cmdwin_result = Ctrl_C;
4856 return;
4857 }
4858#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00004859 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00004860 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00004861 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00004862 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00004863 return;
4864 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004865 if (eap->addr_count > 0)
4866 {
Bram Moolenaarf240e182014-11-27 18:33:02 +01004867 int wnr = eap->line2;
4868
4869 for (wp = firstwin; wp->w_next != NULL; wp = wp->w_next)
4870 if (--wnr <= 0)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004871 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004872 }
4873 else
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004874 wp = curwin;
Bram Moolenaarf240e182014-11-27 18:33:02 +01004875
Bram Moolenaar87ffb5c2017-10-19 12:37:42 +02004876 /* Refuse to quit when locked. */
4877 if (curbuf_locked())
4878 return;
Bram Moolenaar12a96de2018-03-11 14:44:18 +01004879
4880 /* Trigger QuitPre and maybe ExitPre */
4881 if (before_quit_autocmds(wp, FALSE, eap->forceit))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004882 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004883
4884#ifdef FEAT_NETBEANS_INTG
4885 netbeansForcedQuit = eap->forceit;
4886#endif
4887
4888 /*
4889 * If there are more files or windows we won't exit.
4890 */
4891 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
4892 exiting = TRUE;
Bram Moolenaarff930ca2017-10-19 17:12:10 +02004893 if ((!buf_hide(wp->w_buffer)
4894 && check_changed(wp->w_buffer, (p_awa ? CCGD_AW : 0)
Bram Moolenaar45d3b142013-11-09 03:31:51 +01004895 | (eap->forceit ? CCGD_FORCEIT : 0)
4896 | CCGD_EXCMD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004897 || check_more(TRUE, eap->forceit) == FAIL
Bram Moolenaar027387f2016-01-02 22:25:52 +01004898 || (only_one_window() && check_changed_any(eap->forceit, TRUE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004899 {
4900 not_exiting();
4901 }
4902 else
4903 {
Bram Moolenaarc7a0d322015-06-19 12:43:07 +02004904 /* quit last window
4905 * Note: only_one_window() returns true, even so a help window is
4906 * still open. In that case only quit, if no address has been
4907 * specified. Example:
4908 * :h|wincmd w|1q - don't quit
4909 * :h|wincmd w|q - quit
4910 */
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01004911 if (only_one_window() && (ONE_WINDOW || eap->addr_count == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004912 getout(0);
Bram Moolenaar2c33d7b2017-10-14 16:06:20 +02004913 not_exiting();
Bram Moolenaar4033c552017-09-16 20:54:51 +02004914#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004915 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02004916#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004917 /* close window; may free buffer */
Bram Moolenaareb44a682017-08-03 22:44:55 +02004918 win_close(wp, !buf_hide(wp->w_buffer) || eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004919 }
4920}
4921
4922/*
4923 * ":cquit".
4924 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004925 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004926ex_cquit(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004927{
4928 getout(1); /* this does not always pass on the exit code to the Manx
4929 compiler. why? */
4930}
4931
4932/*
4933 * ":qall": try to quit all windows
4934 */
4935 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004936ex_quit_all(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004937{
4938# ifdef FEAT_CMDWIN
4939 if (cmdwin_type != 0)
4940 {
4941 if (eap->forceit)
4942 cmdwin_result = K_XF1; /* ex_window() takes care of this */
4943 else
4944 cmdwin_result = K_XF2;
4945 return;
4946 }
4947# endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00004948
4949 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00004950 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00004951 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00004952 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00004953 return;
4954 }
Bram Moolenaar12a96de2018-03-11 14:44:18 +01004955
4956 if (before_quit_autocmds(curwin, TRUE, eap->forceit))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004957 return;
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00004958
Bram Moolenaar071d4272004-06-13 20:20:40 +00004959 exiting = TRUE;
Bram Moolenaar027387f2016-01-02 22:25:52 +01004960 if (eap->forceit || !check_changed_any(FALSE, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004961 getout(0);
4962 not_exiting();
4963}
4964
Bram Moolenaar071d4272004-06-13 20:20:40 +00004965/*
4966 * ":close": close current window, unless it is the last one
4967 */
4968 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004969ex_close(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004970{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004971 win_T *win;
4972 int winnr = 0;
Bram Moolenaar4033c552017-09-16 20:54:51 +02004973#ifdef FEAT_CMDWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00004974 if (cmdwin_type != 0)
Bram Moolenaar9bd1a7e2011-05-19 14:50:54 +02004975 cmdwin_result = Ctrl_C;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004976 else
Bram Moolenaar4033c552017-09-16 20:54:51 +02004977#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01004978 if (!text_locked() && !curbuf_locked())
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004979 {
4980 if (eap->addr_count == 0)
4981 ex_win_close(eap->forceit, curwin, NULL);
Bram Moolenaar6d41c782018-06-06 09:11:12 +02004982 else
4983 {
Bram Moolenaar29323592016-07-24 22:04:11 +02004984 FOR_ALL_WINDOWS(win)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004985 {
4986 winnr++;
4987 if (winnr == eap->line2)
4988 break;
4989 }
4990 if (win == NULL)
4991 win = lastwin;
4992 ex_win_close(eap->forceit, win, NULL);
4993 }
4994 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004995}
4996
Bram Moolenaar4033c552017-09-16 20:54:51 +02004997#ifdef FEAT_QUICKFIX
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004998/*
4999 * ":pclose": Close any preview window.
5000 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005001 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005002ex_pclose(exarg_T *eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005003{
5004 win_T *win;
5005
Bram Moolenaar79648732019-07-18 21:43:07 +02005006 // First close any normal window.
Bram Moolenaar29323592016-07-24 22:04:11 +02005007 FOR_ALL_WINDOWS(win)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005008 if (win->w_p_pvw)
5009 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00005010 ex_win_close(eap->forceit, win, NULL);
Bram Moolenaar79648732019-07-18 21:43:07 +02005011 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005012 }
Bram Moolenaar79648732019-07-18 21:43:07 +02005013# ifdef FEAT_TEXT_PROP
5014 // Also when 'previewpopup' is empty, it might have been cleared.
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02005015 popup_close_preview();
Bram Moolenaar79648732019-07-18 21:43:07 +02005016# endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005017}
Bram Moolenaar4033c552017-09-16 20:54:51 +02005018#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005019
Bram Moolenaarf740b292006-02-16 22:11:02 +00005020/*
5021 * Close window "win" and take care of handling closing the last window for a
5022 * modified buffer.
5023 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005024 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005025ex_win_close(
5026 int forceit,
5027 win_T *win,
5028 tabpage_T *tp) /* NULL or the tab page "win" is in */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005029{
5030 int need_hide;
5031 buf_T *buf = win->w_buffer;
5032
5033 need_hide = (bufIsChanged(buf) && buf->b_nwindows <= 1);
Bram Moolenaareb44a682017-08-03 22:44:55 +02005034 if (need_hide && !buf_hide(buf) && !forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005035 {
Bram Moolenaar4033c552017-09-16 20:54:51 +02005036#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005037 if ((p_confirm || cmdmod.confirm) && p_write)
5038 {
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005039 bufref_T bufref;
5040
5041 set_bufref(&bufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005042 dialog_changed(buf, FALSE);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005043 if (bufref_valid(&bufref) && bufIsChanged(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005044 return;
5045 need_hide = FALSE;
5046 }
5047 else
Bram Moolenaar4033c552017-09-16 20:54:51 +02005048#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005049 {
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02005050 no_write_message();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005051 return;
5052 }
5053 }
5054
Bram Moolenaar4033c552017-09-16 20:54:51 +02005055#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00005056 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005057#endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00005058
Bram Moolenaar071d4272004-06-13 20:20:40 +00005059 /* free buffer when not hiding it or when it's a scratch buffer */
Bram Moolenaarf740b292006-02-16 22:11:02 +00005060 if (tp == NULL)
Bram Moolenaareb44a682017-08-03 22:44:55 +02005061 win_close(win, !need_hide && !buf_hide(buf));
Bram Moolenaarf740b292006-02-16 22:11:02 +00005062 else
Bram Moolenaareb44a682017-08-03 22:44:55 +02005063 win_close_othertab(win, !need_hide && !buf_hide(buf), tp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005064}
5065
Bram Moolenaar071d4272004-06-13 20:20:40 +00005066/*
Bram Moolenaardea25702017-01-29 15:18:10 +01005067 * Handle the argument for a tabpage related ex command.
5068 * Returns a tabpage number.
5069 * When an error is encountered then eap->errmsg is set.
5070 */
5071 static int
5072get_tabpage_arg(exarg_T *eap)
5073{
5074 int tab_number;
5075 int unaccept_arg0 = (eap->cmdidx == CMD_tabmove) ? 0 : 1;
5076
5077 if (eap->arg && *eap->arg != NUL)
5078 {
5079 char_u *p = eap->arg;
5080 char_u *p_save;
5081 int relative = 0; /* argument +N/-N means: go to N places to the
5082 * right/left relative to the current position. */
5083
5084 if (*p == '-')
5085 {
5086 relative = -1;
5087 p++;
5088 }
5089 else if (*p == '+')
5090 {
5091 relative = 1;
5092 p++;
5093 }
5094
5095 p_save = p;
5096 tab_number = getdigits(&p);
5097
5098 if (relative == 0)
5099 {
5100 if (STRCMP(p, "$") == 0)
5101 tab_number = LAST_TAB_NR;
5102 else if (p == p_save || *p_save == '-' || *p != NUL
5103 || tab_number > LAST_TAB_NR)
5104 {
5105 /* No numbers as argument. */
5106 eap->errmsg = e_invarg;
5107 goto theend;
5108 }
5109 }
5110 else
5111 {
5112 if (*p_save == NUL)
5113 tab_number = 1;
5114 else if (p == p_save || *p_save == '-' || *p != NUL
5115 || tab_number == 0)
5116 {
5117 /* No numbers as argument. */
5118 eap->errmsg = e_invarg;
5119 goto theend;
5120 }
5121 tab_number = tab_number * relative + tabpage_index(curtab);
5122 if (!unaccept_arg0 && relative == -1)
5123 --tab_number;
5124 }
5125 if (tab_number < unaccept_arg0 || tab_number > LAST_TAB_NR)
5126 eap->errmsg = e_invarg;
5127 }
5128 else if (eap->addr_count > 0)
5129 {
5130 if (unaccept_arg0 && eap->line2 == 0)
Bram Moolenaarc6251552017-01-29 21:42:20 +01005131 {
Bram Moolenaardea25702017-01-29 15:18:10 +01005132 eap->errmsg = e_invrange;
Bram Moolenaarc6251552017-01-29 21:42:20 +01005133 tab_number = 0;
5134 }
Bram Moolenaardea25702017-01-29 15:18:10 +01005135 else
5136 {
5137 tab_number = eap->line2;
Bram Moolenaar82a12462019-01-22 22:41:42 +01005138 if (!unaccept_arg0 && *skipwhite(*eap->cmdlinep) == '-')
Bram Moolenaardea25702017-01-29 15:18:10 +01005139 {
5140 --tab_number;
5141 if (tab_number < unaccept_arg0)
5142 eap->errmsg = e_invarg;
5143 }
5144 }
5145 }
5146 else
5147 {
5148 switch (eap->cmdidx)
5149 {
5150 case CMD_tabnext:
5151 tab_number = tabpage_index(curtab) + 1;
5152 if (tab_number > LAST_TAB_NR)
5153 tab_number = 1;
5154 break;
5155 case CMD_tabmove:
5156 tab_number = LAST_TAB_NR;
5157 break;
5158 default:
5159 tab_number = tabpage_index(curtab);
5160 }
5161 }
5162
5163theend:
5164 return tab_number;
5165}
5166
5167/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00005168 * ":tabclose": close current tab page, unless it is the last one.
5169 * ":tabclose N": close tab page N.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005170 */
5171 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005172ex_tabclose(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005173{
Bram Moolenaarf740b292006-02-16 22:11:02 +00005174 tabpage_T *tp;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005175 int tab_number;
Bram Moolenaarf740b292006-02-16 22:11:02 +00005176
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005177# ifdef FEAT_CMDWIN
5178 if (cmdwin_type != 0)
5179 cmdwin_result = K_IGNORE;
5180 else
5181# endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00005182 if (first_tabpage->tp_next == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005183 emsg(_("E784: Cannot close last tab page"));
Bram Moolenaarf740b292006-02-16 22:11:02 +00005184 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005185 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005186 tab_number = get_tabpage_arg(eap);
5187 if (eap->errmsg == NULL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005188 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005189 tp = find_tabpage(tab_number);
Bram Moolenaarf740b292006-02-16 22:11:02 +00005190 if (tp == NULL)
5191 {
5192 beep_flush();
5193 return;
5194 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005195 if (tp != curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00005196 {
5197 tabpage_close_other(tp, eap->forceit);
5198 return;
5199 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005200 else if (!text_locked() && !curbuf_locked())
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005201 tabpage_close(eap->forceit);
5202 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005203 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005204}
5205
5206/*
5207 * ":tabonly": close all tab pages except the current one
5208 */
5209 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005210ex_tabonly(exarg_T *eap)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005211{
5212 tabpage_T *tp;
5213 int done;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005214 int tab_number;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005215
5216# ifdef FEAT_CMDWIN
5217 if (cmdwin_type != 0)
5218 cmdwin_result = K_IGNORE;
5219 else
5220# endif
5221 if (first_tabpage->tp_next == NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01005222 msg(_("Already only one tab page"));
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005223 else
5224 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005225 tab_number = get_tabpage_arg(eap);
5226 if (eap->errmsg == NULL)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005227 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005228 goto_tabpage(tab_number);
5229 /* Repeat this up to a 1000 times, because autocommands may
5230 * mess up the lists. */
5231 for (done = 0; done < 1000; ++done)
5232 {
5233 FOR_ALL_TABPAGES(tp)
5234 if (tp->tp_topframe != topframe)
5235 {
5236 tabpage_close_other(tp, eap->forceit);
5237 /* if we failed to close it quit */
5238 if (valid_tabpage(tp))
5239 done = 1000;
5240 /* start over, "tp" is now invalid */
5241 break;
5242 }
5243 if (first_tabpage->tp_next == NULL)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005244 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005245 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005246 }
5247 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005248}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005249
5250/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00005251 * Close the current tab page.
5252 */
5253 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005254tabpage_close(int forceit)
Bram Moolenaarf740b292006-02-16 22:11:02 +00005255{
5256 /* First close all the windows but the current one. If that worked then
5257 * close the last window in this tab, that will close it. */
Bram Moolenaar459ca562016-11-10 18:16:33 +01005258 if (!ONE_WINDOW)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005259 close_others(TRUE, forceit);
Bram Moolenaar459ca562016-11-10 18:16:33 +01005260 if (ONE_WINDOW)
Bram Moolenaarf740b292006-02-16 22:11:02 +00005261 ex_win_close(forceit, curwin, NULL);
5262# ifdef FEAT_GUI
5263 need_mouse_correct = TRUE;
5264# endif
5265}
5266
5267/*
5268 * Close tab page "tp", which is not the current tab page.
5269 * Note that autocommands may make "tp" invalid.
Bram Moolenaar7875acc2006-09-10 13:51:17 +00005270 * Also takes care of the tab pages line disappearing when closing the
5271 * last-but-one tab page.
Bram Moolenaarf740b292006-02-16 22:11:02 +00005272 */
5273 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005274tabpage_close_other(tabpage_T *tp, int forceit)
Bram Moolenaarf740b292006-02-16 22:11:02 +00005275{
5276 int done = 0;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005277 win_T *wp;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00005278 int h = tabline_height();
Bram Moolenaarf740b292006-02-16 22:11:02 +00005279
5280 /* Limit to 1000 windows, autocommands may add a window while we close
5281 * one. OK, so I'm paranoid... */
5282 while (++done < 1000)
5283 {
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005284 wp = tp->tp_firstwin;
5285 ex_win_close(forceit, wp, tp);
Bram Moolenaarf740b292006-02-16 22:11:02 +00005286
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005287 /* Autocommands may delete the tab page under our fingers and we may
5288 * fail to close a window with a modified buffer. */
5289 if (!valid_tabpage(tp) || tp->tp_firstwin == wp)
Bram Moolenaarf740b292006-02-16 22:11:02 +00005290 break;
5291 }
Bram Moolenaar7875acc2006-09-10 13:51:17 +00005292
Bram Moolenaar29323592016-07-24 22:04:11 +02005293 apply_autocmds(EVENT_TABCLOSED, NULL, NULL, FALSE, curbuf);
Bram Moolenaar29323592016-07-24 22:04:11 +02005294
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005295 redraw_tabline = TRUE;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00005296 if (h != tabline_height())
5297 shell_new_rows();
Bram Moolenaarf740b292006-02-16 22:11:02 +00005298}
5299
5300/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005301 * ":only".
5302 */
5303 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005304ex_only(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005305{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005306 win_T *wp;
5307 int wnr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005308# ifdef FEAT_GUI
5309 need_mouse_correct = TRUE;
5310# endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005311 if (eap->addr_count > 0)
5312 {
5313 wnr = eap->line2;
5314 for (wp = firstwin; --wnr > 0; )
5315 {
5316 if (wp->w_next == NULL)
5317 break;
5318 else
5319 wp = wp->w_next;
5320 }
5321 win_goto(wp);
5322 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005323 close_others(TRUE, eap->forceit);
5324}
5325
Bram Moolenaar071d4272004-06-13 20:20:40 +00005326 static void
Bram Moolenaar5e1e6d22017-01-02 17:26:00 +01005327ex_hide(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005328{
Bram Moolenaar2256c992016-11-15 21:17:07 +01005329 /* ":hide" or ":hide | cmd": hide current window */
Bram Moolenaar2256c992016-11-15 21:17:07 +01005330 if (!eap->skip)
5331 {
Bram Moolenaar4033c552017-09-16 20:54:51 +02005332#ifdef FEAT_GUI
Bram Moolenaar2256c992016-11-15 21:17:07 +01005333 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005334#endif
Bram Moolenaar2256c992016-11-15 21:17:07 +01005335 if (eap->addr_count == 0)
5336 win_close(curwin, FALSE); /* don't free buffer */
5337 else
5338 {
5339 int winnr = 0;
5340 win_T *win;
Bram Moolenaarf240e182014-11-27 18:33:02 +01005341
Bram Moolenaar2256c992016-11-15 21:17:07 +01005342 FOR_ALL_WINDOWS(win)
5343 {
5344 winnr++;
5345 if (winnr == eap->line2)
5346 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005347 }
Bram Moolenaar2256c992016-11-15 21:17:07 +01005348 if (win == NULL)
5349 win = lastwin;
5350 win_close(win, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005351 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005352 }
5353}
5354
5355/*
5356 * ":stop" and ":suspend": Suspend Vim.
5357 */
5358 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005359ex_stop(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005360{
5361 /*
5362 * Disallow suspending for "rvim".
5363 */
Bram Moolenaarcea912a2016-10-12 14:20:24 +02005364 if (!check_restricted())
Bram Moolenaar071d4272004-06-13 20:20:40 +00005365 {
5366 if (!eap->forceit)
5367 autowrite_all();
5368 windgoto((int)Rows - 1, 0);
5369 out_char('\n');
5370 out_flush();
5371 stoptermcap();
5372 out_flush(); /* needed for SUN to restore xterm buffer */
5373#ifdef FEAT_TITLE
Bram Moolenaar40385db2018-08-07 22:31:44 +02005374 mch_restore_title(SAVE_RESTORE_BOTH); /* restore window titles */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005375#endif
5376 ui_suspend(); /* call machine specific function */
5377#ifdef FEAT_TITLE
5378 maketitle();
5379 resettitle(); /* force updating the title */
5380#endif
5381 starttermcap();
5382 scroll_start(); /* scroll screen before redrawing */
5383 redraw_later_clear();
5384 shell_resized(); /* may have resized window */
5385 }
5386}
5387
5388/*
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005389 * ":exit", ":xit" and ":wq": Write file and quite the current window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005390 */
5391 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005392ex_exit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005393{
5394#ifdef FEAT_CMDWIN
5395 if (cmdwin_type != 0)
5396 {
5397 cmdwin_result = Ctrl_C;
5398 return;
5399 }
5400#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005401 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005402 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005403 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005404 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005405 return;
5406 }
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005407
5408 if (before_quit_autocmds(curwin, FALSE, eap->forceit))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005409 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005410
5411 /*
5412 * if more files or windows we won't exit
5413 */
5414 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
5415 exiting = TRUE;
5416 if ( ((eap->cmdidx == CMD_wq
5417 || curbufIsChanged())
5418 && do_write(eap) == FAIL)
5419 || check_more(TRUE, eap->forceit) == FAIL
Bram Moolenaar027387f2016-01-02 22:25:52 +01005420 || (only_one_window() && check_changed_any(eap->forceit, FALSE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005421 {
5422 not_exiting();
5423 }
5424 else
5425 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005426 if (only_one_window()) /* quit last window, exit Vim */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005427 getout(0);
Bram Moolenaar2c33d7b2017-10-14 16:06:20 +02005428 not_exiting();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005429# ifdef FEAT_GUI
5430 need_mouse_correct = TRUE;
5431# endif
Bram Moolenaar1a4a75c2013-07-28 16:03:06 +02005432 /* Quit current window, may free the buffer. */
Bram Moolenaareb44a682017-08-03 22:44:55 +02005433 win_close(curwin, !buf_hide(curwin->w_buffer));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005434 }
5435}
5436
5437/*
5438 * ":print", ":list", ":number".
5439 */
5440 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005441ex_print(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005442{
Bram Moolenaardf177f62005-02-22 08:39:57 +00005443 if (curbuf->b_ml.ml_flags & ML_EMPTY)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005444 emsg(_(e_emptybuf));
Bram Moolenaardf177f62005-02-22 08:39:57 +00005445 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005446 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00005447 for ( ;!got_int; ui_breakcheck())
5448 {
5449 print_line(eap->line1,
5450 (eap->cmdidx == CMD_number || eap->cmdidx == CMD_pound
5451 || (eap->flags & EXFLAG_NR)),
5452 eap->cmdidx == CMD_list || (eap->flags & EXFLAG_LIST));
5453 if (++eap->line1 > eap->line2)
5454 break;
5455 out_flush(); /* show one line at a time */
5456 }
5457 setpcmark();
5458 /* put cursor at last line */
5459 curwin->w_cursor.lnum = eap->line2;
5460 beginline(BL_SOL | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005461 }
5462
Bram Moolenaar071d4272004-06-13 20:20:40 +00005463 ex_no_reprint = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005464}
5465
5466#ifdef FEAT_BYTEOFF
5467 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005468ex_goto(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005469{
5470 goto_byte(eap->line2);
5471}
5472#endif
5473
5474/*
5475 * ":shell".
5476 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005477 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005478ex_shell(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005479{
5480 do_shell(NULL, 0);
5481}
5482
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005483#if defined(HAVE_DROP_FILE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005484
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005485static int drop_busy = FALSE;
5486static int drop_filec;
5487static char_u **drop_filev = NULL;
5488static int drop_split;
5489static void (*drop_callback)(void *);
5490static void *drop_cookie;
5491
5492 static void
5493handle_drop_internal(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005494{
5495 exarg_T ea;
5496 int save_msg_scroll = msg_scroll;
5497
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005498 // Setting the argument list may cause screen updates and being called
5499 // recursively. Avoid that by setting drop_busy.
5500 drop_busy = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005501
5502 /* Check whether the current buffer is changed. If so, we will need
5503 * to split the current window or data could be lost.
5504 * We don't need to check if the 'hidden' option is set, as in this
5505 * case the buffer won't be lost.
5506 */
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005507 if (!buf_hide(curbuf) && !drop_split)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005508 {
5509 ++emsg_off;
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005510 drop_split = check_changed(curbuf, CCGD_AW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005511 --emsg_off;
5512 }
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005513 if (drop_split)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005514 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005515 if (win_split(0, 0) == FAIL)
5516 return;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02005517 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005518
5519 /* When splitting the window, create a new alist. Otherwise the
5520 * existing one is overwritten. */
5521 alist_unlink(curwin->w_alist);
5522 alist_new();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005523 }
5524
5525 /*
5526 * Set up the new argument list.
5527 */
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005528 alist_set(ALIST(curwin), drop_filec, drop_filev, FALSE, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005529
5530 /*
5531 * Move to the first file.
5532 */
5533 /* Fake up a minimal "next" command for do_argfile() */
5534 vim_memset(&ea, 0, sizeof(ea));
5535 ea.cmd = (char_u *)"next";
5536 do_argfile(&ea, 0);
5537
5538 /* do_ecmd() may set need_start_insertmode, but since we never left Insert
5539 * mode that is not needed here. */
5540 need_start_insertmode = FALSE;
5541
5542 /* Restore msg_scroll, otherwise a following command may cause scrolling
5543 * unexpectedly. The screen will be redrawn by the caller, thus
5544 * msg_scroll being set by displaying a message is irrelevant. */
5545 msg_scroll = save_msg_scroll;
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005546
5547 if (drop_callback != NULL)
5548 drop_callback(drop_cookie);
5549
5550 drop_filev = NULL;
5551 drop_busy = FALSE;
5552}
5553
5554/*
5555 * Handle a file drop. The code is here because a drop is *nearly* like an
5556 * :args command, but not quite (we have a list of exact filenames, so we
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01005557 * don't want to (a) parse a command line, or (b) expand wildcards). So the
Bram Moolenaar92d147b2018-07-29 17:35:23 +02005558 * code is very similar to :args and hence needs access to a lot of the static
5559 * functions in this file.
5560 *
5561 * The "filev" list must have been allocated using alloc(), as should each item
5562 * in the list. This function takes over responsibility for freeing the "filev"
5563 * list.
5564 */
5565 void
5566handle_drop(
5567 int filec, // the number of files dropped
5568 char_u **filev, // the list of files dropped
5569 int split, // force splitting the window
5570 void (*callback)(void *), // to be called after setting the argument
5571 // list
5572 void *cookie) // argument for "callback" (allocated)
5573{
5574 // Cannot handle recursive drops, finish the pending one.
5575 if (drop_busy)
5576 {
5577 FreeWild(filec, filev);
5578 vim_free(cookie);
5579 return;
5580 }
5581
5582 // When calling handle_drop() more than once in a row we only use the last
5583 // one.
5584 if (drop_filev != NULL)
5585 {
5586 FreeWild(drop_filec, drop_filev);
5587 vim_free(drop_cookie);
5588 }
5589
5590 drop_filec = filec;
5591 drop_filev = filev;
5592 drop_split = split;
5593 drop_callback = callback;
5594 drop_cookie = cookie;
5595
5596 // Postpone this when:
5597 // - editing the command line
5598 // - not possible to change the current buffer
5599 // - updating the screen
5600 // As it may change buffers and window structures that are in use and cause
5601 // freed memory to be used.
5602 if (text_locked() || curbuf_locked() || updating_screen)
5603 return;
5604
5605 handle_drop_internal();
5606}
5607
5608/*
5609 * To be called when text is unlocked, curbuf is unlocked or updating_screen is
5610 * reset: Handle a postponed drop.
5611 */
5612 void
5613handle_any_postponed_drop(void)
5614{
5615 if (!drop_busy && drop_filev != NULL
5616 && !text_locked() && !curbuf_locked() && !updating_screen)
5617 handle_drop_internal();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005618}
5619#endif
5620
Bram Moolenaar071d4272004-06-13 20:20:40 +00005621/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005622 * ":preserve".
5623 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005624 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005625ex_preserve(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005626{
Bram Moolenaar4399ef42005-02-12 14:29:27 +00005627 curbuf->b_flags |= BF_PRESERVED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005628 ml_preserve(curbuf, TRUE);
5629}
5630
5631/*
5632 * ":recover".
5633 */
5634 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005635ex_recover(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005636{
5637 /* Set recoverymode right away to avoid the ATTENTION prompt. */
5638 recoverymode = TRUE;
Bram Moolenaar45d3b142013-11-09 03:31:51 +01005639 if (!check_changed(curbuf, (p_awa ? CCGD_AW : 0)
5640 | CCGD_MULTWIN
5641 | (eap->forceit ? CCGD_FORCEIT : 0)
5642 | CCGD_EXCMD)
5643
Bram Moolenaar071d4272004-06-13 20:20:40 +00005644 && (*eap->arg == NUL
5645 || setfname(curbuf, eap->arg, NULL, TRUE) == OK))
Bram Moolenaar99499b12019-05-23 21:35:48 +02005646 ml_recover(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005647 recoverymode = FALSE;
5648}
5649
5650/*
5651 * Command modifier used in a wrong way.
5652 */
5653 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005654ex_wrongmodifier(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005655{
5656 eap->errmsg = e_invcmd;
5657}
5658
Bram Moolenaar071d4272004-06-13 20:20:40 +00005659/*
5660 * :sview [+command] file split window with new file, read-only
5661 * :split [[+command] file] split window with current or new file
5662 * :vsplit [[+command] file] split window vertically with current or new file
5663 * :new [[+command] file] split window with no or new file
5664 * :vnew [[+command] file] split vertically window with no or new file
5665 * :sfind [+command] file split window with file in 'path'
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005666 *
5667 * :tabedit open new Tab page with empty window
5668 * :tabedit [+command] file open new Tab page and edit "file"
5669 * :tabnew [[+command] file] just like :tabedit
5670 * :tabfind [+command] file open new Tab page and find "file"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005671 */
5672 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005673ex_splitview(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005674{
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005675 win_T *old_curwin = curwin;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005676#if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005677 char_u *fname = NULL;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005678#endif
5679#ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00005680 int browse_flag = cmdmod.browse;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005681#endif
Bram Moolenaar39902a02018-06-21 22:10:08 +02005682 int use_tab = eap->cmdidx == CMD_tabedit
5683 || eap->cmdidx == CMD_tabfind
5684 || eap->cmdidx == CMD_tabnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005685
Bram Moolenaar8cdbd5b2019-06-16 15:50:45 +02005686 if (ERROR_IF_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02005687 return;
5688
Bram Moolenaar4033c552017-09-16 20:54:51 +02005689#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00005690 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005691#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005692
Bram Moolenaar4033c552017-09-16 20:54:51 +02005693#ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00005694 /* A ":split" in the quickfix window works like ":new". Don't want two
Bram Moolenaar05bb9532008-07-04 09:44:11 +00005695 * quickfix windows. But it's OK when doing ":tab split". */
5696 if (bt_quickfix(curbuf) && cmdmod.tab == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005697 {
5698 if (eap->cmdidx == CMD_split)
5699 eap->cmdidx = CMD_new;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005700 if (eap->cmdidx == CMD_vsplit)
5701 eap->cmdidx = CMD_vnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005702 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02005703#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005704
Bram Moolenaar4033c552017-09-16 20:54:51 +02005705#ifdef FEAT_SEARCHPATH
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005706 if (eap->cmdidx == CMD_sfind || eap->cmdidx == CMD_tabfind)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005707 {
5708 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg),
5709 FNAME_MESS, TRUE, curbuf->b_ffname);
5710 if (fname == NULL)
5711 goto theend;
5712 eap->arg = fname;
5713 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005714# ifdef FEAT_BROWSE
Bram Moolenaar4033c552017-09-16 20:54:51 +02005715 else
5716# endif
5717#endif
5718#ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00005719 if (cmdmod.browse
Bram Moolenaar071d4272004-06-13 20:20:40 +00005720 && eap->cmdidx != CMD_vnew
Bram Moolenaar071d4272004-06-13 20:20:40 +00005721 && eap->cmdidx != CMD_new)
5722 {
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00005723 if (
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005724# ifdef FEAT_GUI
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00005725 !gui.in_use &&
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005726# endif
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00005727 au_has_group((char_u *)"FileExplorer"))
5728 {
5729 /* No browsing supported but we do have the file explorer:
5730 * Edit the directory. */
5731 if (*eap->arg == NUL || !mch_isdir(eap->arg))
5732 eap->arg = (char_u *)".";
5733 }
5734 else
5735 {
Bram Moolenaar39902a02018-06-21 22:10:08 +02005736 fname = do_browse(0, (char_u *)(use_tab
5737 ? _("Edit File in new tab page")
5738 : _("Edit File in new window")),
Bram Moolenaar071d4272004-06-13 20:20:40 +00005739 eap->arg, NULL, NULL, NULL, curbuf);
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00005740 if (fname == NULL)
5741 goto theend;
5742 eap->arg = fname;
5743 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005744 }
5745 cmdmod.browse = FALSE; /* Don't browse again in do_ecmd(). */
Bram Moolenaar4033c552017-09-16 20:54:51 +02005746#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005747
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005748 /*
5749 * Either open new tab page or split the window.
5750 */
Bram Moolenaar39902a02018-06-21 22:10:08 +02005751 if (use_tab)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005752 {
Bram Moolenaar8dff8182006-04-06 20:18:50 +00005753 if (win_new_tabpage(cmdmod.tab != 0 ? cmdmod.tab
5754 : eap->addr_count == 0 ? 0
5755 : (int)eap->line2 + 1) != FAIL)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005756 {
Bram Moolenaard2b66012008-01-09 19:30:36 +00005757 do_exedit(eap, old_curwin);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00005758
5759 /* set the alternate buffer for the window we came from */
5760 if (curwin != old_curwin
5761 && win_valid(old_curwin)
5762 && old_curwin->w_buffer != curbuf
5763 && !cmdmod.keepalt)
5764 old_curwin->w_alt_fnum = curbuf->b_fnum;
5765 }
5766 }
5767 else if (win_split(eap->addr_count > 0 ? (int)eap->line2 : 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +00005768 *eap->cmd == 'v' ? WSP_VERT : 0) != FAIL)
5769 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005770 /* Reset 'scrollbind' when editing another file, but keep it when
5771 * doing ":split" without arguments. */
5772 if (*eap->arg != NUL
Bram Moolenaar8a3bb562018-03-04 20:14:14 +01005773# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00005774 || cmdmod.browse
Bram Moolenaar8a3bb562018-03-04 20:14:14 +01005775# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005776 )
Bram Moolenaar3368ea22010-09-21 16:56:35 +02005777 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005778 else
5779 do_check_scrollbind(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005780 do_exedit(eap, old_curwin);
5781 }
5782
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005783# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00005784 cmdmod.browse = browse_flag;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005785# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005786
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005787# if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005788theend:
5789 vim_free(fname);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005790# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005791}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005792
5793/*
Bram Moolenaar80a94a52006-02-23 21:26:58 +00005794 * Open a new tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005795 */
5796 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005797tabpage_new(void)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00005798{
5799 exarg_T ea;
5800
5801 vim_memset(&ea, 0, sizeof(ea));
5802 ea.cmdidx = CMD_tabnew;
5803 ea.cmd = (char_u *)"tabn";
5804 ea.arg = (char_u *)"";
5805 ex_splitview(&ea);
5806}
5807
5808/*
5809 * :tabnext command
5810 */
5811 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005812ex_tabnext(exarg_T *eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005813{
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005814 int tab_number;
5815
Bram Moolenaar8cdbd5b2019-06-16 15:50:45 +02005816 if (ERROR_IF_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02005817 return;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005818 switch (eap->cmdidx)
5819 {
5820 case CMD_tabfirst:
5821 case CMD_tabrewind:
5822 goto_tabpage(1);
5823 break;
5824 case CMD_tablast:
5825 goto_tabpage(9999);
5826 break;
5827 case CMD_tabprevious:
5828 case CMD_tabNext:
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005829 if (eap->arg && *eap->arg != NUL)
5830 {
5831 char_u *p = eap->arg;
5832 char_u *p_save = p;
5833
5834 tab_number = getdigits(&p);
5835 if (p == p_save || *p_save == '-' || *p != NUL
5836 || tab_number == 0)
5837 {
5838 /* No numbers as argument. */
5839 eap->errmsg = e_invarg;
5840 return;
5841 }
5842 }
5843 else
5844 {
5845 if (eap->addr_count == 0)
5846 tab_number = 1;
5847 else
5848 {
5849 tab_number = eap->line2;
5850 if (tab_number < 1)
5851 {
5852 eap->errmsg = e_invrange;
5853 return;
5854 }
5855 }
5856 }
5857 goto_tabpage(-tab_number);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005858 break;
5859 default: /* CMD_tabnext */
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005860 tab_number = get_tabpage_arg(eap);
5861 if (eap->errmsg == NULL)
5862 goto_tabpage(tab_number);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005863 break;
5864 }
Bram Moolenaar80a94a52006-02-23 21:26:58 +00005865}
5866
5867/*
5868 * :tabmove command
5869 */
5870 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005871ex_tabmove(exarg_T *eap)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00005872{
Bram Moolenaar40ce3a42015-04-21 18:08:39 +02005873 int tab_number;
Bram Moolenaar8cb8dca2012-07-06 18:27:39 +02005874
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005875 tab_number = get_tabpage_arg(eap);
5876 if (eap->errmsg == NULL)
5877 tabpage_move(tab_number);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005878}
5879
5880/*
5881 * :tabs command: List tabs and their contents.
5882 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005883 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005884ex_tabs(exarg_T *eap UNUSED)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005885{
5886 tabpage_T *tp;
5887 win_T *wp;
5888 int tabcount = 1;
5889
5890 msg_start();
5891 msg_scroll = TRUE;
5892 for (tp = first_tabpage; tp != NULL && !got_int; tp = tp->tp_next)
5893 {
5894 msg_putchar('\n');
5895 vim_snprintf((char *)IObuff, IOSIZE, _("Tab page %d"), tabcount++);
Bram Moolenaar8820b482017-03-16 17:23:31 +01005896 msg_outtrans_attr(IObuff, HL_ATTR(HLF_T));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005897 out_flush(); /* output one line at a time */
5898 ui_breakcheck();
5899
Bram Moolenaar030f0df2006-02-21 22:02:53 +00005900 if (tp == curtab)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005901 wp = firstwin;
5902 else
5903 wp = tp->tp_firstwin;
5904 for ( ; wp != NULL && !got_int; wp = wp->w_next)
5905 {
Bram Moolenaar80a94a52006-02-23 21:26:58 +00005906 msg_putchar('\n');
5907 msg_putchar(wp == curwin ? '>' : ' ');
5908 msg_putchar(' ');
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005909 msg_putchar(bufIsChanged(wp->w_buffer) ? '+' : ' ');
5910 msg_putchar(' ');
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005911 if (buf_spname(wp->w_buffer) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02005912 vim_strncpy(IObuff, buf_spname(wp->w_buffer), IOSIZE - 1);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005913 else
5914 home_replace(wp->w_buffer, wp->w_buffer->b_fname,
5915 IObuff, IOSIZE, TRUE);
5916 msg_outtrans(IObuff);
5917 out_flush(); /* output one line at a time */
5918 ui_breakcheck();
5919 }
5920 }
5921}
5922
Bram Moolenaar071d4272004-06-13 20:20:40 +00005923/*
5924 * ":mode": Set screen mode.
5925 * If no argument given, just get the screen size and redraw.
5926 */
5927 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005928ex_mode(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005929{
5930 if (*eap->arg == NUL)
5931 shell_resized();
5932 else
5933 mch_screenmode(eap->arg);
5934}
5935
Bram Moolenaar071d4272004-06-13 20:20:40 +00005936/*
5937 * ":resize".
5938 * set, increment or decrement current window height
5939 */
5940 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005941ex_resize(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005942{
5943 int n;
5944 win_T *wp = curwin;
5945
5946 if (eap->addr_count > 0)
5947 {
5948 n = eap->line2;
5949 for (wp = firstwin; wp->w_next != NULL && --n > 0; wp = wp->w_next)
5950 ;
5951 }
5952
Bram Moolenaar44a2f922016-03-19 22:11:51 +01005953# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00005954 need_mouse_correct = TRUE;
Bram Moolenaar44a2f922016-03-19 22:11:51 +01005955# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005956 n = atol((char *)eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005957 if (cmdmod.split & WSP_VERT)
5958 {
5959 if (*eap->arg == '-' || *eap->arg == '+')
Bram Moolenaar02631462017-09-22 15:20:32 +02005960 n += curwin->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005961 else if (n == 0 && eap->arg[0] == NUL) /* default is very wide */
5962 n = 9999;
5963 win_setwidth_win((int)n, wp);
5964 }
5965 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005966 {
5967 if (*eap->arg == '-' || *eap->arg == '+')
5968 n += curwin->w_height;
Bram Moolenaar1f2903c2017-07-23 19:51:01 +02005969 else if (n == 0 && eap->arg[0] == NUL) /* default is very high */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005970 n = 9999;
5971 win_setheight_win((int)n, wp);
5972 }
5973}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005974
5975/*
5976 * ":find [+command] <file>" command.
5977 */
5978 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005979ex_find(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005980{
5981#ifdef FEAT_SEARCHPATH
5982 char_u *fname;
5983 int count;
5984
5985 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg), FNAME_MESS,
5986 TRUE, curbuf->b_ffname);
5987 if (eap->addr_count > 0)
5988 {
5989 /* Repeat finding the file "count" times. This matters when it
5990 * appears several times in the path. */
5991 count = eap->line2;
5992 while (fname != NULL && --count > 0)
5993 {
5994 vim_free(fname);
5995 fname = find_file_in_path(NULL, 0, FNAME_MESS,
5996 FALSE, curbuf->b_ffname);
5997 }
5998 }
5999
6000 if (fname != NULL)
6001 {
6002 eap->arg = fname;
6003#endif
6004 do_exedit(eap, NULL);
6005#ifdef FEAT_SEARCHPATH
6006 vim_free(fname);
6007 }
6008#endif
6009}
6010
6011/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00006012 * ":open" simulation: for now just work like ":visual".
6013 */
6014 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006015ex_open(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00006016{
6017 regmatch_T regmatch;
6018 char_u *p;
6019
6020 curwin->w_cursor.lnum = eap->line2;
6021 beginline(BL_SOL | BL_FIX);
6022 if (*eap->arg == '/')
6023 {
6024 /* ":open /pattern/": put cursor in column found with pattern */
6025 ++eap->arg;
6026 p = skip_regexp(eap->arg, '/', p_magic, NULL);
6027 *p = NUL;
6028 regmatch.regprog = vim_regcomp(eap->arg, p_magic ? RE_MAGIC : 0);
6029 if (regmatch.regprog != NULL)
6030 {
6031 regmatch.rm_ic = p_ic;
6032 p = ml_get_curline();
6033 if (vim_regexec(&regmatch, p, (colnr_T)0))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006034 curwin->w_cursor.col = (colnr_T)(regmatch.startp[0] - p);
Bram Moolenaardf177f62005-02-22 08:39:57 +00006035 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006036 emsg(_(e_nomatch));
Bram Moolenaar473de612013-06-08 18:19:48 +02006037 vim_regfree(regmatch.regprog);
Bram Moolenaardf177f62005-02-22 08:39:57 +00006038 }
6039 /* Move to the NUL, ignore any other arguments. */
6040 eap->arg += STRLEN(eap->arg);
6041 }
6042 check_cursor();
6043
6044 eap->cmdidx = CMD_visual;
6045 do_exedit(eap, NULL);
6046}
6047
6048/*
6049 * ":edit", ":badd", ":visual".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006050 */
6051 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006052ex_edit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006053{
6054 do_exedit(eap, NULL);
6055}
6056
6057/*
6058 * ":edit <file>" command and alikes.
6059 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006060 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006061do_exedit(
6062 exarg_T *eap,
6063 win_T *old_curwin) /* curwin before doing a split or NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006064{
6065 int n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006066 int need_hide;
Bram Moolenaardf177f62005-02-22 08:39:57 +00006067 int exmode_was = exmode_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006068
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02006069 if (eap->cmdidx != CMD_pedit && ERROR_IF_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02006070 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006071 /*
6072 * ":vi" command ends Ex mode.
6073 */
6074 if (exmode_active && (eap->cmdidx == CMD_visual
6075 || eap->cmdidx == CMD_view))
6076 {
6077 exmode_active = FALSE;
6078 if (*eap->arg == NUL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00006079 {
6080 /* Special case: ":global/pat/visual\NLvi-commands" */
6081 if (global_busy)
6082 {
6083 int rd = RedrawingDisabled;
6084 int nwr = no_wait_return;
6085 int ms = msg_scroll;
6086#ifdef FEAT_GUI
6087 int he = hold_gui_events;
6088#endif
6089
6090 if (eap->nextcmd != NULL)
6091 {
6092 stuffReadbuff(eap->nextcmd);
6093 eap->nextcmd = NULL;
6094 }
6095
6096 if (exmode_was != EXMODE_VIM)
6097 settmode(TMODE_RAW);
6098 RedrawingDisabled = 0;
6099 no_wait_return = 0;
6100 need_wait_return = FALSE;
6101 msg_scroll = 0;
6102#ifdef FEAT_GUI
6103 hold_gui_events = 0;
6104#endif
6105 must_redraw = CLEAR;
6106
6107 main_loop(FALSE, TRUE);
6108
6109 RedrawingDisabled = rd;
6110 no_wait_return = nwr;
6111 msg_scroll = ms;
6112#ifdef FEAT_GUI
6113 hold_gui_events = he;
6114#endif
6115 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006116 return;
Bram Moolenaardf177f62005-02-22 08:39:57 +00006117 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006118 }
6119
6120 if ((eap->cmdidx == CMD_new
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006121 || eap->cmdidx == CMD_tabnew
6122 || eap->cmdidx == CMD_tabedit
Bram Moolenaar4033c552017-09-16 20:54:51 +02006123 || eap->cmdidx == CMD_vnew) && *eap->arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006124 {
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006125 /* ":new" or ":tabnew" without argument: edit an new empty buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006126 setpcmark();
6127 (void)do_ecmd(0, NULL, NULL, eap, ECMD_ONE,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00006128 ECMD_HIDE + (eap->forceit ? ECMD_FORCEIT : 0),
6129 old_curwin == NULL ? curwin : NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006130 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02006131 else if ((eap->cmdidx != CMD_split && eap->cmdidx != CMD_vsplit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006132 || *eap->arg != NUL
6133#ifdef FEAT_BROWSE
6134 || cmdmod.browse
6135#endif
6136 )
6137 {
Bram Moolenaar5555acc2006-04-07 21:33:12 +00006138 /* Can't edit another file when "curbuf_lock" is set. Only ":edit"
6139 * can bring us here, others are stopped earlier. */
6140 if (*eap->arg != NUL && curbuf_locked())
6141 return;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006142
Bram Moolenaar071d4272004-06-13 20:20:40 +00006143 n = readonlymode;
6144 if (eap->cmdidx == CMD_view || eap->cmdidx == CMD_sview)
6145 readonlymode = TRUE;
6146 else if (eap->cmdidx == CMD_enew)
6147 readonlymode = FALSE; /* 'readonly' doesn't make sense in an
6148 empty buffer */
6149 setpcmark();
6150 if (do_ecmd(0, (eap->cmdidx == CMD_enew ? NULL : eap->arg),
6151 NULL, eap,
6152 /* ":edit" goes to first line if Vi compatible */
6153 (*eap->arg == NUL && eap->do_ecmd_lnum == 0
6154 && vim_strchr(p_cpo, CPO_GOTO1) != NULL)
6155 ? ECMD_ONE : eap->do_ecmd_lnum,
Bram Moolenaareb44a682017-08-03 22:44:55 +02006156 (buf_hide(curbuf) ? ECMD_HIDE : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006157 + (eap->forceit ? ECMD_FORCEIT : 0)
Bram Moolenaar7b449342014-03-25 13:03:48 +01006158 /* after a split we can use an existing buffer */
6159 + (old_curwin != NULL ? ECMD_OLDBUF : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006160 + (eap->cmdidx == CMD_badd ? ECMD_ADDBUF : 0 )
Bram Moolenaar701f7af2008-11-15 13:12:07 +00006161 , old_curwin == NULL ? curwin : NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006162 {
6163 /* Editing the file failed. If the window was split, close it. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006164 if (old_curwin != NULL)
6165 {
6166 need_hide = (curbufIsChanged() && curbuf->b_nwindows <= 1);
Bram Moolenaareb44a682017-08-03 22:44:55 +02006167 if (!need_hide || buf_hide(curbuf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006168 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006169#if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00006170 cleanup_T cs;
6171
6172 /* Reset the error/interrupt/exception state here so that
6173 * aborting() returns FALSE when closing a window. */
6174 enter_cleanup(&cs);
Bram Moolenaar4033c552017-09-16 20:54:51 +02006175#endif
6176#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00006177 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006178#endif
Bram Moolenaareb44a682017-08-03 22:44:55 +02006179 win_close(curwin, !need_hide && !buf_hide(curbuf));
Bram Moolenaarc0197e22004-09-13 20:26:32 +00006180
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006181#if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00006182 /* Restore the error/interrupt/exception state if not
6183 * discarded by a new aborting error, interrupt, or
6184 * uncaught exception. */
6185 leave_cleanup(&cs);
Bram Moolenaar4033c552017-09-16 20:54:51 +02006186#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006187 }
6188 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006189 }
6190 else if (readonlymode && curbuf->b_nwindows == 1)
6191 {
6192 /* When editing an already visited buffer, 'readonly' won't be set
6193 * but the previous value is kept. With ":view" and ":sview" we
6194 * want the file to be readonly, except when another window is
6195 * editing the same buffer. */
6196 curbuf->b_p_ro = TRUE;
6197 }
6198 readonlymode = n;
6199 }
6200 else
6201 {
6202 if (eap->do_ecmd_cmd != NULL)
6203 do_cmdline_cmd(eap->do_ecmd_cmd);
6204#ifdef FEAT_TITLE
6205 n = curwin->w_arg_idx_invalid;
6206#endif
6207 check_arg_idx(curwin);
6208#ifdef FEAT_TITLE
6209 if (n != curwin->w_arg_idx_invalid)
6210 maketitle();
6211#endif
6212 }
6213
Bram Moolenaar071d4272004-06-13 20:20:40 +00006214 /*
6215 * if ":split file" worked, set alternate file name in old window to new
6216 * file
6217 */
6218 if (old_curwin != NULL
6219 && *eap->arg != NUL
6220 && curwin != old_curwin
6221 && win_valid(old_curwin)
Bram Moolenaard4755bb2004-09-02 19:12:26 +00006222 && old_curwin->w_buffer != curbuf
6223 && !cmdmod.keepalt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006224 old_curwin->w_alt_fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006225
6226 ex_no_reprint = TRUE;
6227}
6228
6229#ifndef FEAT_GUI
6230/*
6231 * ":gui" and ":gvim" when there is no GUI.
6232 */
6233 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006234ex_nogui(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006235{
6236 eap->errmsg = e_nogvim;
6237}
6238#endif
6239
Bram Moolenaar4f974752019-02-17 17:44:42 +01006240#if defined(FEAT_GUI_MSWIN) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006241 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006242ex_tearoff(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006243{
6244 gui_make_tearoff(eap->arg);
6245}
6246#endif
6247
Bram Moolenaar29a2c082018-03-05 21:06:23 +01006248#if (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK) \
6249 || defined(FEAT_TERM_POPUP_MENU)) && defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006250 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006251ex_popup(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006252{
Bram Moolenaar29a2c082018-03-05 21:06:23 +01006253# if defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK)
6254 if (gui.in_use)
6255 gui_make_popup(eap->arg, eap->forceit);
6256# ifdef FEAT_TERM_POPUP_MENU
6257 else
6258# endif
6259# endif
6260# ifdef FEAT_TERM_POPUP_MENU
6261 pum_make_popup(eap->arg, eap->forceit);
6262# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006263}
6264#endif
6265
Bram Moolenaar071d4272004-06-13 20:20:40 +00006266 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006267ex_swapname(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006268{
6269 if (curbuf->b_ml.ml_mfp == NULL || curbuf->b_ml.ml_mfp->mf_fname == NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01006270 msg(_("No swap file"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006271 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01006272 msg((char *)curbuf->b_ml.ml_mfp->mf_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006273}
6274
6275/*
6276 * ":syncbind" forces all 'scrollbind' windows to have the same relative
6277 * offset.
6278 * (1998-11-02 16:21:01 R. Edward Ralston <eralston@computer.org>)
6279 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006280 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006281ex_syncbind(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006282{
Bram Moolenaar071d4272004-06-13 20:20:40 +00006283 win_T *wp;
Bram Moolenaardedd1b02013-12-14 13:06:17 +01006284 win_T *save_curwin = curwin;
6285 buf_T *save_curbuf = curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006286 long topline;
6287 long y;
6288 linenr_T old_linenr = curwin->w_cursor.lnum;
6289
6290 setpcmark();
6291
6292 /*
6293 * determine max topline
6294 */
6295 if (curwin->w_p_scb)
6296 {
6297 topline = curwin->w_topline;
Bram Moolenaar29323592016-07-24 22:04:11 +02006298 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006299 {
6300 if (wp->w_p_scb && wp->w_buffer)
6301 {
Bram Moolenaar375e3392019-01-31 18:26:10 +01006302 y = wp->w_buffer->b_ml.ml_line_count - get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006303 if (topline > y)
6304 topline = y;
6305 }
6306 }
6307 if (topline < 1)
6308 topline = 1;
6309 }
6310 else
6311 {
6312 topline = 1;
6313 }
6314
6315
6316 /*
Bram Moolenaardedd1b02013-12-14 13:06:17 +01006317 * Set all scrollbind windows to the same topline.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006318 */
Bram Moolenaar29323592016-07-24 22:04:11 +02006319 FOR_ALL_WINDOWS(curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006320 {
6321 if (curwin->w_p_scb)
6322 {
Bram Moolenaardedd1b02013-12-14 13:06:17 +01006323 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006324 y = topline - curwin->w_topline;
6325 if (y > 0)
6326 scrollup(y, TRUE);
6327 else
6328 scrolldown(-y, TRUE);
6329 curwin->w_scbind_pos = topline;
6330 redraw_later(VALID);
6331 cursor_correct();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006332 curwin->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006333 }
6334 }
Bram Moolenaardedd1b02013-12-14 13:06:17 +01006335 curwin = save_curwin;
6336 curbuf = save_curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006337 if (curwin->w_p_scb)
6338 {
6339 did_syncbind = TRUE;
6340 checkpcmark();
6341 if (old_linenr != curwin->w_cursor.lnum)
6342 {
6343 char_u ctrl_o[2];
6344
6345 ctrl_o[0] = Ctrl_O;
6346 ctrl_o[1] = 0;
6347 ins_typebuf(ctrl_o, REMAP_NONE, 0, TRUE, FALSE);
6348 }
6349 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006350}
6351
6352
6353 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006354ex_read(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006355{
Bram Moolenaardf177f62005-02-22 08:39:57 +00006356 int i;
6357 int empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
6358 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006359
6360 if (eap->usefilter) /* :r!cmd */
6361 do_bang(1, eap, FALSE, FALSE, TRUE);
6362 else
6363 {
6364 if (u_save(eap->line2, (linenr_T)(eap->line2 + 1)) == FAIL)
6365 return;
6366
6367#ifdef FEAT_BROWSE
6368 if (cmdmod.browse)
6369 {
6370 char_u *browseFile;
6371
Bram Moolenaar7171abe2004-10-11 10:06:20 +00006372 browseFile = do_browse(0, (char_u *)_("Append File"), eap->arg,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006373 NULL, NULL, NULL, curbuf);
6374 if (browseFile != NULL)
6375 {
6376 i = readfile(browseFile, NULL,
6377 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
6378 vim_free(browseFile);
6379 }
6380 else
6381 i = OK;
6382 }
6383 else
6384#endif
6385 if (*eap->arg == NUL)
6386 {
6387 if (check_fname() == FAIL) /* check for no file name */
6388 return;
6389 i = readfile(curbuf->b_ffname, curbuf->b_fname,
6390 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
6391 }
6392 else
6393 {
6394 if (vim_strchr(p_cpo, CPO_ALTREAD) != NULL)
6395 (void)setaltfname(eap->arg, eap->arg, (linenr_T)1);
6396 i = readfile(eap->arg, NULL,
6397 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
6398
6399 }
Bram Moolenaare13b9af2017-01-13 22:01:02 +01006400 if (i != OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006401 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006402#if defined(FEAT_EVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006403 if (!aborting())
6404#endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006405 semsg(_(e_notopen), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006406 }
6407 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00006408 {
6409 if (empty && exmode_active)
6410 {
6411 /* Delete the empty line that remains. Historically ex does
6412 * this but vi doesn't. */
6413 if (eap->line2 == 0)
6414 lnum = curbuf->b_ml.ml_line_count;
6415 else
6416 lnum = 1;
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00006417 if (*ml_get(lnum) == NUL && u_savedel(lnum, 1L) == OK)
Bram Moolenaardf177f62005-02-22 08:39:57 +00006418 {
6419 ml_delete(lnum, FALSE);
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00006420 if (curwin->w_cursor.lnum > 1
6421 && curwin->w_cursor.lnum >= lnum)
Bram Moolenaardf177f62005-02-22 08:39:57 +00006422 --curwin->w_cursor.lnum;
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00006423 deleted_lines_mark(lnum, 1L);
Bram Moolenaardf177f62005-02-22 08:39:57 +00006424 }
6425 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006426 redraw_curbuf_later(VALID);
Bram Moolenaardf177f62005-02-22 08:39:57 +00006427 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006428 }
6429}
6430
Bram Moolenaarea408852005-06-25 22:49:46 +00006431static char_u *prev_dir = NULL;
6432
6433#if defined(EXITFREE) || defined(PROTO)
6434 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006435free_cd_dir(void)
Bram Moolenaarea408852005-06-25 22:49:46 +00006436{
Bram Moolenaard23a8232018-02-10 18:45:26 +01006437 VIM_CLEAR(prev_dir);
6438 VIM_CLEAR(globaldir);
Bram Moolenaarea408852005-06-25 22:49:46 +00006439}
6440#endif
6441
Bram Moolenaarf4258302013-06-02 18:20:17 +02006442/*
6443 * Deal with the side effects of changing the current directory.
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006444 * When 'scope' is CDSCOPE_TABPAGE then this was after an ":tcd" command.
6445 * When 'scope' is CDSCOPE_WINDOW then this was after an ":lcd" command.
Bram Moolenaarf4258302013-06-02 18:20:17 +02006446 */
6447 void
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006448post_chdir(cdscope_T scope)
Bram Moolenaarf4258302013-06-02 18:20:17 +02006449{
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006450 if (scope != CDSCOPE_WINDOW)
Bram Moolenaar00aa0692019-04-27 20:37:57 +02006451 // Clear tab local directory for both :cd and :tcd
6452 VIM_CLEAR(curtab->tp_localdir);
Bram Moolenaard23a8232018-02-10 18:45:26 +01006453 VIM_CLEAR(curwin->w_localdir);
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006454 if (scope != CDSCOPE_GLOBAL)
Bram Moolenaarf4258302013-06-02 18:20:17 +02006455 {
6456 /* If still in global directory, need to remember current
6457 * directory as global directory. */
6458 if (globaldir == NULL && prev_dir != NULL)
6459 globaldir = vim_strsave(prev_dir);
6460 /* Remember this local directory for the window. */
6461 if (mch_dirname(NameBuff, MAXPATHL) == OK)
Bram Moolenaar00aa0692019-04-27 20:37:57 +02006462 {
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006463 if (scope == CDSCOPE_TABPAGE)
Bram Moolenaar00aa0692019-04-27 20:37:57 +02006464 curtab->tp_localdir = vim_strsave(NameBuff);
6465 else
6466 curwin->w_localdir = vim_strsave(NameBuff);
6467 }
Bram Moolenaarf4258302013-06-02 18:20:17 +02006468 }
6469 else
6470 {
6471 /* We are now in the global directory, no need to remember its
6472 * name. */
Bram Moolenaard23a8232018-02-10 18:45:26 +01006473 VIM_CLEAR(globaldir);
Bram Moolenaarf4258302013-06-02 18:20:17 +02006474 }
6475
6476 shorten_fnames(TRUE);
6477}
6478
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006479/*
6480 * Change directory function used by :cd/:tcd/:lcd Ex commands and the
6481 * chdir() function. If 'winlocaldir' is TRUE, then changes the window-local
6482 * directory. If 'tablocaldir' is TRUE, then changes the tab-local directory.
6483 * Otherwise changes the global directory.
6484 * Returns TRUE if the directory is successfully changed.
6485 */
6486 int
6487changedir_func(
6488 char_u *new_dir,
6489 int forceit,
6490 cdscope_T scope)
6491{
6492 char_u *tofree;
6493 int dir_differs;
6494 int retval = FALSE;
6495
6496 if (allbuf_locked())
6497 return FALSE;
6498
6499 if (vim_strchr(p_cpo, CPO_CHDIR) != NULL && curbufIsChanged() && !forceit)
6500 {
6501 emsg(_("E747: Cannot change directory, buffer is modified (add ! to override)"));
6502 return FALSE;
6503 }
6504
6505 // ":cd -": Change to previous directory
6506 if (STRCMP(new_dir, "-") == 0)
6507 {
6508 if (prev_dir == NULL)
6509 {
6510 emsg(_("E186: No previous directory"));
6511 return FALSE;
6512 }
6513 new_dir = prev_dir;
6514 }
6515
6516 // Save current directory for next ":cd -"
6517 tofree = prev_dir;
6518 if (mch_dirname(NameBuff, MAXPATHL) == OK)
6519 prev_dir = vim_strsave(NameBuff);
6520 else
6521 prev_dir = NULL;
6522
6523#if defined(UNIX) || defined(VMS)
6524 // for UNIX ":cd" means: go to home directory
6525 if (*new_dir == NUL)
6526 {
6527 // use NameBuff for home directory name
6528# ifdef VMS
6529 char_u *p;
6530
6531 p = mch_getenv((char_u *)"SYS$LOGIN");
6532 if (p == NULL || *p == NUL) // empty is the same as not set
6533 NameBuff[0] = NUL;
6534 else
6535 vim_strncpy(NameBuff, p, MAXPATHL - 1);
6536# else
6537 expand_env((char_u *)"$HOME", NameBuff, MAXPATHL);
6538# endif
6539 new_dir = NameBuff;
6540 }
6541#endif
6542 dir_differs = new_dir == NULL || prev_dir == NULL
6543 || pathcmp((char *)prev_dir, (char *)new_dir, -1) != 0;
6544 if (new_dir == NULL || (dir_differs && vim_chdir(new_dir)))
6545 emsg(_(e_failed));
6546 else
6547 {
6548 char_u *acmd_fname;
6549
6550 post_chdir(scope);
6551
6552 if (dir_differs)
6553 {
6554 if (scope == CDSCOPE_WINDOW)
6555 acmd_fname = (char_u *)"window";
6556 else if (scope == CDSCOPE_TABPAGE)
6557 acmd_fname = (char_u *)"tabpage";
6558 else
6559 acmd_fname = (char_u *)"global";
6560 apply_autocmds(EVENT_DIRCHANGED, acmd_fname, new_dir, FALSE,
6561 curbuf);
6562 }
6563 retval = TRUE;
6564 }
6565 vim_free(tofree);
6566
6567 return retval;
6568}
Bram Moolenaarea408852005-06-25 22:49:46 +00006569
Bram Moolenaar071d4272004-06-13 20:20:40 +00006570/*
Bram Moolenaar00aa0692019-04-27 20:37:57 +02006571 * ":cd", ":tcd", ":lcd", ":chdir" ":tchdir" and ":lchdir".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006572 */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00006573 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006574ex_cd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006575{
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01006576 char_u *new_dir;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006577
6578 new_dir = eap->arg;
6579#if !defined(UNIX) && !defined(VMS)
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006580 // for non-UNIX ":cd" means: print current directory
Bram Moolenaar071d4272004-06-13 20:20:40 +00006581 if (*new_dir == NUL)
6582 ex_pwd(NULL);
6583 else
6584#endif
6585 {
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006586 cdscope_T scope = CDSCOPE_GLOBAL;
6587
6588 if (eap->cmdidx == CMD_lcd || eap->cmdidx == CMD_lchdir)
6589 scope = CDSCOPE_WINDOW;
6590 else if (eap->cmdidx == CMD_tcd || eap->cmdidx == CMD_tchdir)
6591 scope = CDSCOPE_TABPAGE;
6592
6593 if (changedir_func(new_dir, eap->forceit, scope))
Bram Moolenaardf177f62005-02-22 08:39:57 +00006594 {
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02006595 // Echo the new current directory if the command was typed.
Bram Moolenaarfcfbc672009-07-09 18:13:49 +00006596 if (KeyTyped || p_verbose >= 5)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006597 ex_pwd(eap);
6598 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006599 }
6600}
6601
6602/*
6603 * ":pwd".
6604 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006605 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006606ex_pwd(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006607{
6608 if (mch_dirname(NameBuff, MAXPATHL) == OK)
6609 {
6610#ifdef BACKSLASH_IN_FILENAME
6611 slash_adjust(NameBuff);
6612#endif
Bram Moolenaar32526b32019-01-19 17:43:09 +01006613 msg((char *)NameBuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006614 }
6615 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006616 emsg(_("E187: Unknown"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006617}
6618
6619/*
6620 * ":=".
6621 */
6622 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006623ex_equal(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006624{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006625 smsg("%ld", (long)eap->line2);
Bram Moolenaardf177f62005-02-22 08:39:57 +00006626 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006627}
6628
6629 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006630ex_sleep(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006631{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006632 int n;
6633 long len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006634
6635 if (cursor_valid())
6636 {
6637 n = W_WINROW(curwin) + curwin->w_wrow - msg_scrolled;
6638 if (n >= 0)
Bram Moolenaar53f81742017-09-22 14:35:51 +02006639 windgoto((int)n, curwin->w_wincol + curwin->w_wcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006640 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006641
6642 len = eap->line2;
6643 switch (*eap->arg)
6644 {
6645 case 'm': break;
6646 case NUL: len *= 1000L; break;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006647 default: semsg(_(e_invarg2), eap->arg); return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00006648 }
6649 do_sleep(len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006650}
6651
6652/*
6653 * Sleep for "msec" milliseconds, but keep checking for a CTRL-C every second.
6654 */
6655 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006656do_sleep(long msec)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006657{
Bram Moolenaar52953192019-08-16 10:27:13 +02006658 long done = 0;
Bram Moolenaar975b5272016-03-15 23:10:59 +01006659 long wait_now;
Bram Moolenaar52953192019-08-16 10:27:13 +02006660# ifdef ELAPSED_FUNC
6661 elapsed_T start_tv;
6662
6663 // Remember at what time we started, so that we know how much longer we
6664 // should wait after waiting for a bit.
6665 ELAPSED_INIT(start_tv);
6666# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006667
6668 cursor_on();
Bram Moolenaarf45d7472018-09-30 18:22:26 +02006669 out_flush_cursor(FALSE, FALSE);
Bram Moolenaar52953192019-08-16 10:27:13 +02006670 while (!got_int && done < msec)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006671 {
Bram Moolenaar975b5272016-03-15 23:10:59 +01006672 wait_now = msec - done > 1000L ? 1000L : msec - done;
6673#ifdef FEAT_TIMERS
6674 {
6675 long due_time = check_due_timer();
6676
6677 if (due_time > 0 && due_time < wait_now)
6678 wait_now = due_time;
6679 }
6680#endif
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02006681#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar28e67e02019-08-15 23:05:49 +02006682 if (has_any_channel() && wait_now > 20L)
6683 wait_now = 20L;
6684#endif
6685#ifdef FEAT_SOUND
6686 if (has_any_sound_callback() && wait_now > 20L)
6687 wait_now = 20L;
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02006688#endif
Bram Moolenaar975b5272016-03-15 23:10:59 +01006689 ui_delay(wait_now, TRUE);
Bram Moolenaar52953192019-08-16 10:27:13 +02006690
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02006691#ifdef FEAT_JOB_CHANNEL
6692 if (has_any_channel())
6693 ui_breakcheck_force(TRUE);
6694 else
6695#endif
6696 ui_breakcheck();
Bram Moolenaar93c88e02015-09-15 14:12:05 +02006697#ifdef MESSAGE_QUEUE
Bram Moolenaar52953192019-08-16 10:27:13 +02006698 // Process the netbeans and clientserver messages that may have been
6699 // received in the call to ui_breakcheck() when the GUI is in use. This
6700 // may occur when running a test case.
Bram Moolenaar93c88e02015-09-15 14:12:05 +02006701 parse_queued_messages();
Bram Moolenaare3cc6d42011-10-20 21:58:34 +02006702#endif
Bram Moolenaar52953192019-08-16 10:27:13 +02006703
6704# ifdef ELAPSED_FUNC
6705 // actual time passed
6706 done = ELAPSED_FUNC(start_tv);
6707# else
6708 // guestimate time passed (will actually be more)
6709 done += wait_now;
6710# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006711 }
Bram Moolenaar1ebff3d2018-07-07 16:18:13 +02006712
6713 // If CTRL-C was typed to interrupt the sleep, drop the CTRL-C from the
6714 // input buffer, otherwise a following call to input() fails.
6715 if (got_int)
6716 (void)vpeekc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006717}
6718
Bram Moolenaar071d4272004-06-13 20:20:40 +00006719/*
6720 * ":winsize" command (obsolete).
6721 */
6722 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006723ex_winsize(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006724{
6725 int w, h;
6726 char_u *arg = eap->arg;
6727 char_u *p;
6728
6729 w = getdigits(&arg);
6730 arg = skipwhite(arg);
6731 p = arg;
6732 h = getdigits(&arg);
6733 if (*p != NUL && *arg == NUL)
6734 set_shellsize(w, h, TRUE);
6735 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006736 emsg(_("E465: :winsize requires two number arguments"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006737}
6738
Bram Moolenaar071d4272004-06-13 20:20:40 +00006739 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006740ex_wincmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006741{
6742 int xchar = NUL;
6743 char_u *p;
6744
6745 if (*eap->arg == 'g' || *eap->arg == Ctrl_G)
6746 {
6747 /* CTRL-W g and CTRL-W CTRL-G have an extra command character */
6748 if (eap->arg[1] == NUL)
6749 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006750 emsg(_(e_invarg));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006751 return;
6752 }
6753 xchar = eap->arg[1];
6754 p = eap->arg + 2;
6755 }
6756 else
6757 p = eap->arg + 1;
6758
6759 eap->nextcmd = check_nextcmd(p);
6760 p = skipwhite(p);
6761 if (*p != NUL && *p != '"' && eap->nextcmd == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006762 emsg(_(e_invarg));
Bram Moolenaar12bde492011-06-13 01:19:56 +02006763 else if (!eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006764 {
6765 /* Pass flags on for ":vertical wincmd ]". */
6766 postponed_split_flags = cmdmod.split;
Bram Moolenaard326ce82007-03-11 14:48:29 +00006767 postponed_split_tab = cmdmod.tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006768 do_window(*eap->arg, eap->addr_count > 0 ? eap->line2 : 0L, xchar);
6769 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00006770 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006771 }
6772}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006773
Bram Moolenaar843ee412004-06-30 16:16:41 +00006774#if defined(FEAT_GUI) || defined(UNIX) || defined(VMS) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006775/*
6776 * ":winpos".
6777 */
6778 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006779ex_winpos(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006780{
6781 int x, y;
6782 char_u *arg = eap->arg;
6783 char_u *p;
6784
6785 if (*arg == NUL)
6786 {
Bram Moolenaar843ee412004-06-30 16:16:41 +00006787# if defined(FEAT_GUI) || defined(MSWIN)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02006788# ifdef VIMDLL
6789 if (gui.in_use ? gui_mch_get_winpos(&x, &y) != FAIL :
6790 mch_get_winpos(&x, &y) != FAIL)
6791# elif defined(FEAT_GUI)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006792 if (gui.in_use && gui_mch_get_winpos(&x, &y) != FAIL)
Bram Moolenaar843ee412004-06-30 16:16:41 +00006793# else
6794 if (mch_get_winpos(&x, &y) != FAIL)
6795# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006796 {
6797 sprintf((char *)IObuff, _("Window position: X %d, Y %d"), x, y);
Bram Moolenaar32526b32019-01-19 17:43:09 +01006798 msg((char *)IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006799 }
6800 else
6801# endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006802 emsg(_("E188: Obtaining window position not implemented for this platform"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006803 }
6804 else
6805 {
6806 x = getdigits(&arg);
6807 arg = skipwhite(arg);
6808 p = arg;
6809 y = getdigits(&arg);
6810 if (*p == NUL || *arg != NUL)
6811 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006812 emsg(_("E466: :winpos requires two number arguments"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006813 return;
6814 }
6815# ifdef FEAT_GUI
6816 if (gui.in_use)
6817 gui_mch_set_winpos(x, y);
6818 else if (gui.starting)
6819 {
6820 /* Remember the coordinates for when the window is opened. */
6821 gui_win_x = x;
6822 gui_win_y = y;
6823 }
Bram Moolenaarafde13b2019-04-28 19:46:49 +02006824# if defined(HAVE_TGETENT) || defined(VIMDLL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006825 else
6826# endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02006827# endif
6828# if defined(MSWIN) && (!defined(FEAT_GUI) || defined(VIMDLL))
Bram Moolenaar843ee412004-06-30 16:16:41 +00006829 mch_set_winpos(x, y);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006830# endif
6831# ifdef HAVE_TGETENT
6832 if (*T_CWP)
6833 term_set_winpos(x, y);
6834# endif
6835 }
6836}
6837#endif
6838
6839/*
6840 * Handle command that work like operators: ":delete", ":yank", ":>" and ":<".
6841 */
6842 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006843ex_operators(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006844{
6845 oparg_T oa;
6846
6847 clear_oparg(&oa);
6848 oa.regname = eap->regname;
6849 oa.start.lnum = eap->line1;
6850 oa.end.lnum = eap->line2;
6851 oa.line_count = eap->line2 - eap->line1 + 1;
6852 oa.motion_type = MLINE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006853 virtual_op = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006854 if (eap->cmdidx != CMD_yank) /* position cursor for undo */
6855 {
6856 setpcmark();
6857 curwin->w_cursor.lnum = eap->line1;
6858 beginline(BL_SOL | BL_FIX);
6859 }
6860
Bram Moolenaard07c6e12013-11-21 14:21:40 +01006861 if (VIsual_active)
6862 end_visual_mode();
Bram Moolenaard07c6e12013-11-21 14:21:40 +01006863
Bram Moolenaar071d4272004-06-13 20:20:40 +00006864 switch (eap->cmdidx)
6865 {
6866 case CMD_delete:
6867 oa.op_type = OP_DELETE;
6868 op_delete(&oa);
6869 break;
6870
6871 case CMD_yank:
6872 oa.op_type = OP_YANK;
6873 (void)op_yank(&oa, FALSE, TRUE);
6874 break;
6875
6876 default: /* CMD_rshift or CMD_lshift */
Bram Moolenaar6e707362013-06-14 19:15:58 +02006877 if (
Bram Moolenaar071d4272004-06-13 20:20:40 +00006878#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6e707362013-06-14 19:15:58 +02006879 (eap->cmdidx == CMD_rshift) ^ curwin->w_p_rl
6880#else
6881 eap->cmdidx == CMD_rshift
Bram Moolenaar071d4272004-06-13 20:20:40 +00006882#endif
Bram Moolenaar6e707362013-06-14 19:15:58 +02006883 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00006884 oa.op_type = OP_RSHIFT;
6885 else
6886 oa.op_type = OP_LSHIFT;
6887 op_shift(&oa, FALSE, eap->amount);
6888 break;
6889 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006890 virtual_op = MAYBE;
Bram Moolenaardf177f62005-02-22 08:39:57 +00006891 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006892}
6893
6894/*
6895 * ":put".
6896 */
6897 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006898ex_put(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006899{
6900 /* ":0put" works like ":1put!". */
6901 if (eap->line2 == 0)
6902 {
6903 eap->line2 = 1;
6904 eap->forceit = TRUE;
6905 }
6906 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaardf177f62005-02-22 08:39:57 +00006907 do_put(eap->regname, eap->forceit ? BACKWARD : FORWARD, 1L,
6908 PUT_LINE|PUT_CURSLINE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006909}
6910
6911/*
6912 * Handle ":copy" and ":move".
6913 */
6914 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006915ex_copymove(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006916{
6917 long n;
6918
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02006919 n = get_address(eap, &eap->arg, eap->addr_type, FALSE, FALSE, FALSE, 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006920 if (eap->arg == NULL) /* error detected */
6921 {
6922 eap->nextcmd = NULL;
6923 return;
6924 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00006925 get_flags(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006926
6927 /*
6928 * move or copy lines from 'eap->line1'-'eap->line2' to below line 'n'
6929 */
6930 if (n == MAXLNUM || n < 0 || n > curbuf->b_ml.ml_line_count)
6931 {
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02006932 emsg(_(e_invrange));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006933 return;
6934 }
6935
6936 if (eap->cmdidx == CMD_move)
6937 {
6938 if (do_move(eap->line1, eap->line2, n) == FAIL)
6939 return;
6940 }
6941 else
6942 ex_copy(eap->line1, eap->line2, n);
6943 u_clearline();
6944 beginline(BL_SOL | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00006945 ex_may_print(eap);
6946}
6947
6948/*
6949 * Print the current line if flags were given to the Ex command.
6950 */
Bram Moolenaarfd3fe982014-04-01 17:49:44 +02006951 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006952ex_may_print(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00006953{
6954 if (eap->flags != 0)
6955 {
6956 print_line(curwin->w_cursor.lnum, (eap->flags & EXFLAG_NR),
6957 (eap->flags & EXFLAG_LIST));
6958 ex_no_reprint = TRUE;
6959 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006960}
6961
6962/*
6963 * ":smagic" and ":snomagic".
6964 */
6965 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006966ex_submagic(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006967{
6968 int magic_save = p_magic;
6969
6970 p_magic = (eap->cmdidx == CMD_smagic);
6971 do_sub(eap);
6972 p_magic = magic_save;
6973}
6974
6975/*
6976 * ":join".
6977 */
6978 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006979ex_join(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006980{
6981 curwin->w_cursor.lnum = eap->line1;
6982 if (eap->line1 == eap->line2)
6983 {
6984 if (eap->addr_count >= 2) /* :2,2join does nothing */
6985 return;
6986 if (eap->line2 == curbuf->b_ml.ml_line_count)
6987 {
6988 beep_flush();
6989 return;
6990 }
6991 ++eap->line2;
6992 }
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02006993 (void)do_join(eap->line2 - eap->line1 + 1, !eap->forceit, TRUE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006994 beginline(BL_WHITE | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00006995 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006996}
6997
6998/*
6999 * ":[addr]@r" or ":[addr]*r": execute register
7000 */
7001 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007002ex_at(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007003{
7004 int c;
Bram Moolenaar60462872009-11-03 11:40:19 +00007005 int prev_len = typebuf.tb_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007006
7007 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaar4930a762016-09-11 14:39:53 +02007008 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007009
7010#ifdef USE_ON_FLY_SCROLL
7011 dont_scroll = TRUE; /* disallow scrolling here */
7012#endif
7013
7014 /* get the register name. No name means to use the previous one */
7015 c = *eap->arg;
7016 if (c == NUL || (c == '*' && *eap->cmd == '*'))
7017 c = '@';
Bram Moolenaard333d1e2006-11-07 17:43:47 +00007018 /* Put the register in the typeahead buffer with the "silent" flag. */
7019 if (do_execreg(c, TRUE, vim_strchr(p_cpo, CPO_EXECBUF) != NULL, TRUE)
7020 == FAIL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007021 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007022 beep_flush();
Bram Moolenaardf177f62005-02-22 08:39:57 +00007023 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007024 else
7025 {
7026 int save_efr = exec_from_reg;
7027
7028 exec_from_reg = TRUE;
7029
7030 /*
7031 * Execute from the typeahead buffer.
Bram Moolenaar60462872009-11-03 11:40:19 +00007032 * Continue until the stuff buffer is empty and all added characters
7033 * have been consumed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007034 */
Bram Moolenaar60462872009-11-03 11:40:19 +00007035 while (!stuff_empty() || typebuf.tb_len > prev_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007036 (void)do_cmdline(NULL, getexline, NULL, DOCMD_NOWAIT|DOCMD_VERBOSE);
7037
7038 exec_from_reg = save_efr;
7039 }
7040}
7041
7042/*
7043 * ":!".
7044 */
7045 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007046ex_bang(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007047{
7048 do_bang(eap->addr_count, eap, eap->forceit, TRUE, TRUE);
7049}
7050
7051/*
7052 * ":undo".
7053 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007054 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +01007055ex_undo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007056{
Bram Moolenaard3667a22006-03-16 21:35:52 +00007057 if (eap->addr_count == 1) /* :undo 123 */
Bram Moolenaar730cde92010-06-27 05:18:54 +02007058 undo_time(eap->line2, FALSE, FALSE, TRUE);
Bram Moolenaard3667a22006-03-16 21:35:52 +00007059 else
7060 u_undo(1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007061}
7062
Bram Moolenaar55debbe2010-05-23 23:34:36 +02007063#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar6df6f472010-07-18 18:04:50 +02007064 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007065ex_wundo(exarg_T *eap)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02007066{
7067 char_u hash[UNDO_HASH_SIZE];
7068
7069 u_compute_hash(hash);
7070 u_write_undo(eap->arg, eap->forceit, curbuf, hash);
7071}
7072
Bram Moolenaar6df6f472010-07-18 18:04:50 +02007073 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007074ex_rundo(exarg_T *eap)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02007075{
7076 char_u hash[UNDO_HASH_SIZE];
7077
7078 u_compute_hash(hash);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02007079 u_read_undo(eap->arg, hash, NULL);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02007080}
7081#endif
7082
Bram Moolenaar071d4272004-06-13 20:20:40 +00007083/*
7084 * ":redo".
7085 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007086 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007087ex_redo(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007088{
7089 u_redo(1);
7090}
7091
7092/*
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007093 * ":earlier" and ":later".
7094 */
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007095 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007096ex_later(exarg_T *eap)
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007097{
7098 long count = 0;
7099 int sec = FALSE;
Bram Moolenaar730cde92010-06-27 05:18:54 +02007100 int file = FALSE;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007101 char_u *p = eap->arg;
7102
7103 if (*p == NUL)
7104 count = 1;
7105 else if (isdigit(*p))
7106 {
7107 count = getdigits(&p);
7108 switch (*p)
7109 {
7110 case 's': ++p; sec = TRUE; break;
7111 case 'm': ++p; sec = TRUE; count *= 60; break;
7112 case 'h': ++p; sec = TRUE; count *= 60 * 60; break;
Bram Moolenaar730cde92010-06-27 05:18:54 +02007113 case 'd': ++p; sec = TRUE; count *= 24 * 60 * 60; break;
7114 case 'f': ++p; file = TRUE; break;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007115 }
7116 }
7117
7118 if (*p != NUL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007119 semsg(_(e_invarg2), eap->arg);
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007120 else
Bram Moolenaar730cde92010-06-27 05:18:54 +02007121 undo_time(eap->cmdidx == CMD_earlier ? -count : count,
7122 sec, file, FALSE);
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007123}
7124
7125/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007126 * ":redir": start/stop redirection.
7127 */
7128 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007129ex_redir(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007130{
7131 char *mode;
7132 char_u *fname;
Bram Moolenaarca472992005-01-21 11:46:23 +00007133 char_u *arg = eap->arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007134
Bram Moolenaarba768492016-07-08 15:32:54 +02007135#ifdef FEAT_EVAL
Bram Moolenaar79815f12016-07-09 17:07:29 +02007136 if (redir_execute)
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02007137 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007138 emsg(_("E930: Cannot use :redir inside execute()"));
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02007139 return;
7140 }
Bram Moolenaarba768492016-07-08 15:32:54 +02007141#endif
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02007142
Bram Moolenaar071d4272004-06-13 20:20:40 +00007143 if (STRICMP(eap->arg, "END") == 0)
7144 close_redir();
7145 else
7146 {
Bram Moolenaarca472992005-01-21 11:46:23 +00007147 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00007148 {
Bram Moolenaarca472992005-01-21 11:46:23 +00007149 ++arg;
7150 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00007151 {
Bram Moolenaarca472992005-01-21 11:46:23 +00007152 ++arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007153 mode = "a";
7154 }
7155 else
7156 mode = "w";
Bram Moolenaarca472992005-01-21 11:46:23 +00007157 arg = skipwhite(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007158
7159 close_redir();
7160
7161 /* Expand environment variables and "~/". */
Bram Moolenaarca472992005-01-21 11:46:23 +00007162 fname = expand_env_save(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007163 if (fname == NULL)
7164 return;
7165#ifdef FEAT_BROWSE
7166 if (cmdmod.browse)
7167 {
7168 char_u *browseFile;
7169
Bram Moolenaar7171abe2004-10-11 10:06:20 +00007170 browseFile = do_browse(BROWSE_SAVE,
7171 (char_u *)_("Save Redirection"),
Bram Moolenaarc36651b2018-04-29 12:22:56 +02007172 fname, NULL, NULL,
7173 (char_u *)_(BROWSE_FILTER_ALL_FILES), curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007174 if (browseFile == NULL)
7175 return; /* operation cancelled */
7176 vim_free(fname);
7177 fname = browseFile;
7178 eap->forceit = TRUE; /* since dialog already asked */
7179 }
7180#endif
7181
7182 redir_fd = open_exfile(fname, eap->forceit, mode);
7183 vim_free(fname);
7184 }
7185#ifdef FEAT_EVAL
Bram Moolenaarca472992005-01-21 11:46:23 +00007186 else if (*arg == '@')
Bram Moolenaar071d4272004-06-13 20:20:40 +00007187 {
7188 /* redirect to a register a-z (resp. A-Z for appending) */
7189 close_redir();
Bram Moolenaarca472992005-01-21 11:46:23 +00007190 ++arg;
7191 if (ASCII_ISALPHA(*arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007192# ifdef FEAT_CLIPBOARD
Bram Moolenaarca472992005-01-21 11:46:23 +00007193 || *arg == '*'
Bram Moolenaar9a51c6e2006-11-14 19:25:02 +00007194 || *arg == '+'
Bram Moolenaar071d4272004-06-13 20:20:40 +00007195# endif
Bram Moolenaarca472992005-01-21 11:46:23 +00007196 || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00007197 {
Bram Moolenaarca472992005-01-21 11:46:23 +00007198 redir_reg = *arg++;
Bram Moolenaarc188b882007-10-19 14:20:54 +00007199 if (*arg == '>' && arg[1] == '>') /* append */
Bram Moolenaard9d30582005-05-18 22:10:28 +00007200 arg += 2;
Bram Moolenaarc188b882007-10-19 14:20:54 +00007201 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007202 {
Bram Moolenaarc188b882007-10-19 14:20:54 +00007203 /* Can use both "@a" and "@a>". */
Bram Moolenaar2c29bee2005-06-01 21:46:07 +00007204 if (*arg == '>')
7205 arg++;
Bram Moolenaarc188b882007-10-19 14:20:54 +00007206 /* Make register empty when not using @A-@Z and the
7207 * command is valid. */
7208 if (*arg == NUL && !isupper(redir_reg))
7209 write_reg_contents(redir_reg, (char_u *)"", -1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007210 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00007211 }
7212 if (*arg != NUL)
7213 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00007214 redir_reg = 0;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007215 semsg(_(e_invarg2), eap->arg);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007216 }
7217 }
7218 else if (*arg == '=' && arg[1] == '>')
7219 {
7220 int append;
7221
7222 /* redirect to a variable */
7223 close_redir();
7224 arg += 2;
7225
7226 if (*arg == '>')
7227 {
7228 ++arg;
7229 append = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007230 }
7231 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00007232 append = FALSE;
7233
7234 if (var_redir_start(skipwhite(arg), append) == OK)
7235 redir_vname = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007236 }
7237#endif
7238
7239 /* TODO: redirect to a buffer */
7240
Bram Moolenaar071d4272004-06-13 20:20:40 +00007241 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007242 semsg(_(e_invarg2), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007243 }
Bram Moolenaar29b2d262006-09-10 19:07:28 +00007244
7245 /* Make sure redirection is not off. Can happen for cmdline completion
7246 * that indirectly invokes a command to catch its output. */
7247 if (redir_fd != NULL
7248#ifdef FEAT_EVAL
7249 || redir_reg || redir_vname
7250#endif
7251 )
7252 redir_off = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007253}
7254
7255/*
7256 * ":redraw": force redraw
7257 */
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01007258 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007259ex_redraw(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007260{
7261 int r = RedrawingDisabled;
7262 int p = p_lz;
7263
7264 RedrawingDisabled = 0;
7265 p_lz = FALSE;
Bram Moolenaarabc39ab2017-03-01 18:04:05 +01007266 validate_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007267 update_topline();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007268 update_screen(eap->forceit ? CLEAR : VIsual_active ? INVERTED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007269#ifdef FEAT_TITLE
7270 if (need_maketitle)
7271 maketitle();
7272#endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007273#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
7274# ifdef VIMDLL
7275 if (!gui.in_use)
7276# endif
7277 resize_console_buf();
Bram Moolenaar78d21da2019-02-17 15:00:52 +01007278#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007279 RedrawingDisabled = r;
7280 p_lz = p;
7281
7282 /* Reset msg_didout, so that a message that's there is overwritten. */
7283 msg_didout = FALSE;
7284 msg_col = 0;
7285
Bram Moolenaar56667a52013-07-17 11:54:28 +02007286 /* No need to wait after an intentional redraw. */
7287 need_wait_return = FALSE;
7288
Bram Moolenaar071d4272004-06-13 20:20:40 +00007289 out_flush();
7290}
7291
7292/*
7293 * ":redrawstatus": force redraw of status line(s)
7294 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007295 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007296ex_redrawstatus(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007297{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007298 int r = RedrawingDisabled;
7299 int p = p_lz;
7300
7301 RedrawingDisabled = 0;
7302 p_lz = FALSE;
7303 if (eap->forceit)
7304 status_redraw_all();
7305 else
7306 status_redraw_curbuf();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007307 update_screen(VIsual_active ? INVERTED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007308 RedrawingDisabled = r;
7309 p_lz = p;
7310 out_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007311}
7312
Bram Moolenaare12bab32019-01-08 22:02:56 +01007313/*
7314 * ":redrawtabline": force redraw of the tabline
7315 */
7316 static void
7317ex_redrawtabline(exarg_T *eap UNUSED)
7318{
7319 int r = RedrawingDisabled;
7320 int p = p_lz;
7321
7322 RedrawingDisabled = 0;
7323 p_lz = FALSE;
7324
7325 draw_tabline();
7326
7327 RedrawingDisabled = r;
7328 p_lz = p;
7329 out_flush();
7330}
7331
Bram Moolenaar071d4272004-06-13 20:20:40 +00007332 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007333close_redir(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007334{
7335 if (redir_fd != NULL)
7336 {
7337 fclose(redir_fd);
7338 redir_fd = NULL;
7339 }
7340#ifdef FEAT_EVAL
7341 redir_reg = 0;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007342 if (redir_vname)
7343 {
7344 var_redir_stop();
7345 redir_vname = 0;
7346 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007347#endif
7348}
7349
Bram Moolenaarf96ae0b2019-07-28 15:21:55 +02007350#if (defined(FEAT_SESSION) || defined(FEAT_EVAL)) || defined(PROTO)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007351 int
Bram Moolenaarbd67aac2019-09-21 23:09:04 +02007352vim_mkdir_emsg(char_u *name, int prot UNUSED)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007353{
7354 if (vim_mkdir(name, prot) != 0)
7355 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007356 semsg(_("E739: Cannot create directory: %s"), name);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007357 return FAIL;
7358 }
7359 return OK;
7360}
7361#endif
7362
Bram Moolenaar071d4272004-06-13 20:20:40 +00007363/*
7364 * Open a file for writing for an Ex command, with some checks.
7365 * Return file descriptor, or NULL on failure.
7366 */
7367 FILE *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007368open_exfile(
7369 char_u *fname,
7370 int forceit,
7371 char *mode) /* "w" for create new file or "a" for append */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007372{
7373 FILE *fd;
7374
7375#ifdef UNIX
7376 /* with Unix it is possible to open a directory */
7377 if (mch_isdir(fname))
7378 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007379 semsg(_(e_isadir2), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007380 return NULL;
7381 }
7382#endif
7383 if (!forceit && *mode != 'a' && vim_fexists(fname))
7384 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007385 semsg(_("E189: \"%s\" exists (add ! to override)"), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007386 return NULL;
7387 }
7388
7389 if ((fd = mch_fopen((char *)fname, mode)) == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007390 semsg(_("E190: Cannot open \"%s\" for writing"), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007391
7392 return fd;
7393}
7394
7395/*
7396 * ":mark" and ":k".
7397 */
7398 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007399ex_mark(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007400{
7401 pos_T pos;
7402
7403 if (*eap->arg == NUL) /* No argument? */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007404 emsg(_(e_argreq));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007405 else if (eap->arg[1] != NUL) /* more than one character? */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007406 emsg(_(e_trailing));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007407 else
7408 {
7409 pos = curwin->w_cursor; /* save curwin->w_cursor */
7410 curwin->w_cursor.lnum = eap->line2;
7411 beginline(BL_WHITE | BL_FIX);
7412 if (setmark(*eap->arg) == FAIL) /* set mark */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007413 emsg(_("E191: Argument must be a letter or forward/backward quote"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007414 curwin->w_cursor = pos; /* restore curwin->w_cursor */
7415 }
7416}
7417
7418/*
7419 * Update w_topline, w_leftcol and the cursor position.
7420 */
7421 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007422update_topline_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007423{
7424 check_cursor(); /* put cursor on valid line */
7425 update_topline();
7426 if (!curwin->w_p_wrap)
7427 validate_cursor();
7428 update_curswant();
7429}
7430
Bram Moolenaar071d4272004-06-13 20:20:40 +00007431/*
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007432 * Save the current State and go to Normal mode.
7433 * Return TRUE if the typeahead could be saved.
7434 */
7435 int
7436save_current_state(save_state_T *sst)
7437{
7438 sst->save_msg_scroll = msg_scroll;
7439 sst->save_restart_edit = restart_edit;
7440 sst->save_msg_didout = msg_didout;
7441 sst->save_State = State;
7442 sst->save_insertmode = p_im;
7443 sst->save_finish_op = finish_op;
7444 sst->save_opcount = opcount;
Bram Moolenaarcce713d2019-03-04 11:40:12 +01007445 sst->save_reg_executing = reg_executing;
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007446
7447 msg_scroll = FALSE; /* no msg scrolling in Normal mode */
7448 restart_edit = 0; /* don't go to Insert mode */
7449 p_im = FALSE; /* don't use 'insertmode' */
7450
7451 /*
7452 * Save the current typeahead. This is required to allow using ":normal"
7453 * from an event handler and makes sure we don't hang when the argument
7454 * ends with half a command.
7455 */
7456 save_typeahead(&sst->tabuf);
7457 return sst->tabuf.typebuf_valid;
7458}
7459
7460 void
7461restore_current_state(save_state_T *sst)
7462{
7463 /* Restore the previous typeahead. */
7464 restore_typeahead(&sst->tabuf);
7465
7466 msg_scroll = sst->save_msg_scroll;
7467 restart_edit = sst->save_restart_edit;
7468 p_im = sst->save_insertmode;
7469 finish_op = sst->save_finish_op;
7470 opcount = sst->save_opcount;
Bram Moolenaarcce713d2019-03-04 11:40:12 +01007471 reg_executing = sst->save_reg_executing;
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007472 msg_didout |= sst->save_msg_didout; /* don't reset msg_didout now */
7473
7474 /* Restore the state (needed when called from a function executed for
7475 * 'indentexpr'). Update the mouse and cursor, they may have changed. */
7476 State = sst->save_State;
7477#ifdef CURSOR_SHAPE
7478 ui_cursor_shape(); /* may show different cursor shape */
7479#endif
7480}
7481
7482/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007483 * ":normal[!] {commands}": Execute normal mode commands.
7484 */
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01007485 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007486ex_normal(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007487{
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007488 save_state_T save_state;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007489 char_u *arg = NULL;
7490 int l;
7491 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007492
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00007493 if (ex_normal_lock > 0)
7494 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007495 emsg(_(e_secure));
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00007496 return;
7497 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007498 if (ex_normal_busy >= p_mmd)
7499 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007500 emsg(_("E192: Recursive use of :normal too deep"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007501 return;
7502 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007503
Bram Moolenaar071d4272004-06-13 20:20:40 +00007504 /*
7505 * vgetc() expects a CSI and K_SPECIAL to have been escaped. Don't do
7506 * this for the K_SPECIAL leading byte, otherwise special keys will not
7507 * work.
7508 */
7509 if (has_mbyte)
7510 {
7511 int len = 0;
7512
7513 /* Count the number of characters to be escaped. */
7514 for (p = eap->arg; *p != NUL; ++p)
7515 {
Bram Moolenaar13505972019-01-24 15:04:48 +01007516#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007517 if (*p == CSI) /* leadbyte CSI */
7518 len += 2;
Bram Moolenaar13505972019-01-24 15:04:48 +01007519#endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007520 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007521 if (*++p == K_SPECIAL /* trailbyte K_SPECIAL or CSI */
Bram Moolenaar13505972019-01-24 15:04:48 +01007522#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007523 || *p == CSI
Bram Moolenaar13505972019-01-24 15:04:48 +01007524#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007525 )
7526 len += 2;
7527 }
7528 if (len > 0)
7529 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02007530 arg = alloc(STRLEN(eap->arg) + len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007531 if (arg != NULL)
7532 {
7533 len = 0;
7534 for (p = eap->arg; *p != NUL; ++p)
7535 {
7536 arg[len++] = *p;
Bram Moolenaar13505972019-01-24 15:04:48 +01007537#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007538 if (*p == CSI)
7539 {
7540 arg[len++] = KS_EXTRA;
7541 arg[len++] = (int)KE_CSI;
7542 }
Bram Moolenaar13505972019-01-24 15:04:48 +01007543#endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007544 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007545 {
7546 arg[len++] = *++p;
7547 if (*p == K_SPECIAL)
7548 {
7549 arg[len++] = KS_SPECIAL;
7550 arg[len++] = KE_FILLER;
7551 }
Bram Moolenaar13505972019-01-24 15:04:48 +01007552#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007553 else if (*p == CSI)
7554 {
7555 arg[len++] = KS_EXTRA;
7556 arg[len++] = (int)KE_CSI;
7557 }
Bram Moolenaar13505972019-01-24 15:04:48 +01007558#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007559 }
7560 arg[len] = NUL;
7561 }
7562 }
7563 }
7564 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007565
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007566 ++ex_normal_busy;
7567 if (save_current_state(&save_state))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007568 {
7569 /*
7570 * Repeat the :normal command for each line in the range. When no
7571 * range given, execute it just once, without positioning the cursor
7572 * first.
7573 */
7574 do
7575 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007576 if (eap->addr_count != 0)
7577 {
7578 curwin->w_cursor.lnum = eap->line1++;
7579 curwin->w_cursor.col = 0;
Bram Moolenaard5d37532017-03-27 23:02:07 +02007580 check_cursor_moved(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007581 }
7582
Bram Moolenaar13505972019-01-24 15:04:48 +01007583 exec_normal_cmd(arg != NULL
7584 ? arg
7585 : eap->arg, eap->forceit ? REMAP_NONE : REMAP_YES, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007586 }
7587 while (eap->addr_count > 0 && eap->line1 <= eap->line2 && !got_int);
7588 }
7589
7590 /* Might not return to the main loop when in an event handler. */
7591 update_topline_cursor();
7592
Bram Moolenaara21a6a92017-09-23 16:33:50 +02007593 restore_current_state(&save_state);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007594 --ex_normal_busy;
Bram Moolenaareda73602014-11-05 09:53:23 +01007595 setmouse();
Bram Moolenaareda73602014-11-05 09:53:23 +01007596#ifdef CURSOR_SHAPE
7597 ui_cursor_shape(); /* may show different cursor shape */
7598#endif
7599
Bram Moolenaar071d4272004-06-13 20:20:40 +00007600 vim_free(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007601}
7602
7603/*
Bram Moolenaar64969662005-12-14 21:59:55 +00007604 * ":startinsert", ":startreplace" and ":startgreplace"
Bram Moolenaar071d4272004-06-13 20:20:40 +00007605 */
7606 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007607ex_startinsert(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007608{
Bram Moolenaarfd371682005-01-14 21:42:54 +00007609 if (eap->forceit)
7610 {
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02007611 // cursor line can be zero on startup
Bram Moolenaar09ca9322017-09-26 17:40:45 +02007612 if (!curwin->w_cursor.lnum)
7613 curwin->w_cursor.lnum = 1;
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02007614 set_cursor_for_append_to_line();
Bram Moolenaarfd371682005-01-14 21:42:54 +00007615 }
7616
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02007617 // Ignore the command when already in Insert mode. Inserting an
7618 // expression register that invokes a function can do this.
Bram Moolenaara40c5002005-01-09 21:16:21 +00007619 if (State & INSERT)
7620 return;
7621
Bram Moolenaar64969662005-12-14 21:59:55 +00007622 if (eap->cmdidx == CMD_startinsert)
7623 restart_edit = 'a';
7624 else if (eap->cmdidx == CMD_startreplace)
7625 restart_edit = 'R';
Bram Moolenaar071d4272004-06-13 20:20:40 +00007626 else
Bram Moolenaar64969662005-12-14 21:59:55 +00007627 restart_edit = 'V';
7628
7629 if (!eap->forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007630 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00007631 if (eap->cmdidx == CMD_startinsert)
7632 restart_edit = 'i';
Bram Moolenaar8d3b5102019-09-05 21:29:01 +02007633 curwin->w_curswant = 0; // avoid MAXCOL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007634 }
7635}
7636
7637/*
7638 * ":stopinsert"
7639 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007640 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007641ex_stopinsert(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007642{
7643 restart_edit = 0;
7644 stop_insert_mode = TRUE;
Bram Moolenaarfd773e92016-04-02 19:39:16 +02007645 clearmode();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007646}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007647
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00007648/*
7649 * Execute normal mode command "cmd".
7650 * "remap" can be REMAP_NONE or REMAP_YES.
7651 */
7652 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007653exec_normal_cmd(char_u *cmd, int remap, int silent)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00007654{
Bram Moolenaar25281632016-01-21 23:32:32 +01007655 /* Stuff the argument into the typeahead buffer. */
7656 ins_typebuf(cmd, remap, 0, TRUE, silent);
Bram Moolenaar586c70c2018-10-02 16:23:58 +02007657 exec_normal(FALSE, FALSE, FALSE);
Bram Moolenaar25281632016-01-21 23:32:32 +01007658}
Bram Moolenaar25281632016-01-21 23:32:32 +01007659
Bram Moolenaar25281632016-01-21 23:32:32 +01007660/*
7661 * Execute normal_cmd() until there is no typeahead left.
Bram Moolenaar586c70c2018-10-02 16:23:58 +02007662 * When "use_vpeekc" is TRUE use vpeekc() to check for available chars.
Bram Moolenaar25281632016-01-21 23:32:32 +01007663 */
7664 void
Bram Moolenaar586c70c2018-10-02 16:23:58 +02007665exec_normal(int was_typed, int use_vpeekc, int may_use_terminal_loop UNUSED)
Bram Moolenaar25281632016-01-21 23:32:32 +01007666{
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00007667 oparg_T oa;
Bram Moolenaar905dd902019-04-07 14:21:47 +02007668 int c;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00007669
Bram Moolenaar905dd902019-04-07 14:21:47 +02007670 // When calling vpeekc() from feedkeys() it will return Ctrl_C when there
7671 // is nothing to get, so also check for Ctrl_C.
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00007672 clear_oparg(&oa);
7673 finish_op = FALSE;
Bram Moolenaar586c70c2018-10-02 16:23:58 +02007674 while ((!stuff_empty()
7675 || ((was_typed || !typebuf_typed()) && typebuf.tb_len > 0)
Bram Moolenaar905dd902019-04-07 14:21:47 +02007676 || (use_vpeekc && (c = vpeekc()) != NUL && c != Ctrl_C))
Bram Moolenaar586c70c2018-10-02 16:23:58 +02007677 && !got_int)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00007678 {
7679 update_topline_cursor();
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02007680#ifdef FEAT_TERMINAL
Bram Moolenaar655a82a2018-05-08 22:01:07 +02007681 if (may_use_terminal_loop && term_use_loop()
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02007682 && oa.op_type == OP_NOP && oa.regname == NUL
7683 && !VIsual_active)
7684 {
7685 /* If terminal_loop() returns OK we got a key that is handled
7686 * in Normal model. With FAIL we first need to position the
7687 * cursor and the screen needs to be redrawn. */
7688 if (terminal_loop(TRUE) == OK)
7689 normal_cmd(&oa, TRUE);
7690 }
7691 else
7692#endif
7693 /* execute a Normal mode cmd */
7694 normal_cmd(&oa, TRUE);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00007695 }
7696}
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00007697
Bram Moolenaar071d4272004-06-13 20:20:40 +00007698#ifdef FEAT_FIND_ID
7699 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007700ex_checkpath(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007701{
7702 find_pattern_in_path(NULL, 0, 0, FALSE, FALSE, CHECK_PATH, 1L,
7703 eap->forceit ? ACTION_SHOW_ALL : ACTION_SHOW,
7704 (linenr_T)1, (linenr_T)MAXLNUM);
7705}
7706
Bram Moolenaar4033c552017-09-16 20:54:51 +02007707#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007708/*
7709 * ":psearch"
7710 */
7711 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007712ex_psearch(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007713{
7714 g_do_tagpreview = p_pvh;
7715 ex_findpat(eap);
7716 g_do_tagpreview = 0;
7717}
7718#endif
7719
7720 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007721ex_findpat(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007722{
7723 int whole = TRUE;
7724 long n;
7725 char_u *p;
7726 int action;
7727
7728 switch (cmdnames[eap->cmdidx].cmd_name[2])
7729 {
7730 case 'e': /* ":psearch", ":isearch" and ":dsearch" */
7731 if (cmdnames[eap->cmdidx].cmd_name[0] == 'p')
7732 action = ACTION_GOTO;
7733 else
7734 action = ACTION_SHOW;
7735 break;
7736 case 'i': /* ":ilist" and ":dlist" */
7737 action = ACTION_SHOW_ALL;
7738 break;
7739 case 'u': /* ":ijump" and ":djump" */
7740 action = ACTION_GOTO;
7741 break;
7742 default: /* ":isplit" and ":dsplit" */
7743 action = ACTION_SPLIT;
7744 break;
7745 }
7746
7747 n = 1;
7748 if (vim_isdigit(*eap->arg)) /* get count */
7749 {
7750 n = getdigits(&eap->arg);
7751 eap->arg = skipwhite(eap->arg);
7752 }
7753 if (*eap->arg == '/') /* Match regexp, not just whole words */
7754 {
7755 whole = FALSE;
7756 ++eap->arg;
7757 p = skip_regexp(eap->arg, '/', p_magic, NULL);
7758 if (*p)
7759 {
7760 *p++ = NUL;
7761 p = skipwhite(p);
7762
7763 /* Check for trailing illegal characters */
7764 if (!ends_excmd(*p))
7765 eap->errmsg = e_trailing;
7766 else
7767 eap->nextcmd = check_nextcmd(p);
7768 }
7769 }
7770 if (!eap->skip)
7771 find_pattern_in_path(eap->arg, 0, (int)STRLEN(eap->arg),
7772 whole, !eap->forceit,
7773 *eap->cmd == 'd' ? FIND_DEFINE : FIND_ANY,
7774 n, action, eap->line1, eap->line2);
7775}
7776#endif
7777
Bram Moolenaar071d4272004-06-13 20:20:40 +00007778
Bram Moolenaar4033c552017-09-16 20:54:51 +02007779#ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00007780/*
7781 * ":ptag", ":ptselect", ":ptjump", ":ptnext", etc.
7782 */
7783 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007784ex_ptag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007785{
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01007786 g_do_tagpreview = p_pvh; /* will be reset to 0 in ex_tag_cmd() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007787 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
7788}
7789
7790/*
7791 * ":pedit"
7792 */
7793 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007794ex_pedit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007795{
7796 win_T *curwin_save = curwin;
7797
Bram Moolenaar026587b2019-08-17 15:08:00 +02007798 // Open the preview window or popup and make it the current window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007799 g_do_tagpreview = p_pvh;
Bram Moolenaar576a4a62019-08-18 15:25:17 +02007800 prepare_tagpreview(TRUE, TRUE, FALSE);
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02007801
Bram Moolenaar026587b2019-08-17 15:08:00 +02007802 // Edit the file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007803 do_exedit(eap, NULL);
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02007804
Bram Moolenaar071d4272004-06-13 20:20:40 +00007805 if (curwin != curwin_save && win_valid(curwin_save))
7806 {
Bram Moolenaar026587b2019-08-17 15:08:00 +02007807 // Return cursor to where we were
Bram Moolenaar071d4272004-06-13 20:20:40 +00007808 validate_cursor();
7809 redraw_later(VALID);
7810 win_enter(curwin_save, TRUE);
7811 }
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02007812# ifdef FEAT_TEXT_PROP
7813 else if (WIN_IS_POPUP(curwin))
7814 {
7815 // can't keep focus in popup window
7816 win_enter(firstwin, TRUE);
7817 }
7818# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007819 g_do_tagpreview = 0;
7820}
Bram Moolenaar4033c552017-09-16 20:54:51 +02007821#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007822
7823/*
7824 * ":stag", ":stselect" and ":stjump".
7825 */
7826 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007827ex_stag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007828{
7829 postponed_split = -1;
7830 postponed_split_flags = cmdmod.split;
Bram Moolenaard326ce82007-03-11 14:48:29 +00007831 postponed_split_tab = cmdmod.tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007832 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
7833 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00007834 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007835}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007836
7837/*
7838 * ":tag", ":tselect", ":tjump", ":tnext", etc.
7839 */
7840 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007841ex_tag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007842{
7843 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name);
7844}
7845
7846 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007847ex_tag_cmd(exarg_T *eap, char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007848{
7849 int cmd;
7850
7851 switch (name[1])
7852 {
7853 case 'j': cmd = DT_JUMP; /* ":tjump" */
7854 break;
7855 case 's': cmd = DT_SELECT; /* ":tselect" */
7856 break;
7857 case 'p': cmd = DT_PREV; /* ":tprevious" */
7858 break;
7859 case 'N': cmd = DT_PREV; /* ":tNext" */
7860 break;
7861 case 'n': cmd = DT_NEXT; /* ":tnext" */
7862 break;
7863 case 'o': cmd = DT_POP; /* ":pop" */
7864 break;
7865 case 'f': /* ":tfirst" */
7866 case 'r': cmd = DT_FIRST; /* ":trewind" */
7867 break;
7868 case 'l': cmd = DT_LAST; /* ":tlast" */
7869 break;
7870 default: /* ":tag" */
7871#ifdef FEAT_CSCOPE
Bram Moolenaar7c94c262008-06-20 09:11:34 +00007872 if (p_cst && *eap->arg != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007873 {
Bram Moolenaard4db7712016-11-12 19:16:46 +01007874 ex_cstag(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007875 return;
7876 }
7877#endif
7878 cmd = DT_TAG;
7879 break;
7880 }
7881
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00007882 if (name[0] == 'l')
7883 {
7884#ifndef FEAT_QUICKFIX
7885 ex_ni(eap);
7886 return;
7887#else
7888 cmd = DT_LTAG;
7889#endif
7890 }
7891
Bram Moolenaar071d4272004-06-13 20:20:40 +00007892 do_tag(eap->arg, cmd, eap->addr_count > 0 ? (int)eap->line2 : 1,
7893 eap->forceit, TRUE);
7894}
7895
7896/*
Bram Moolenaar05bb9532008-07-04 09:44:11 +00007897 * Check "str" for starting with a special cmdline variable.
7898 * If found return one of the SPEC_ values and set "*usedlen" to the length of
7899 * the variable. Otherwise return -1 and "*usedlen" is unchanged.
7900 */
7901 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007902find_cmdline_var(char_u *src, int *usedlen)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00007903{
7904 int len;
7905 int i;
Bram Moolenaar8f0b2d42009-05-16 14:41:10 +00007906 static char *(spec_str[]) = {
Bram Moolenaar05bb9532008-07-04 09:44:11 +00007907 "%",
7908#define SPEC_PERC 0
7909 "#",
Bram Moolenaar65f08472017-09-10 18:16:20 +02007910#define SPEC_HASH (SPEC_PERC + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00007911 "<cword>", /* cursor word */
Bram Moolenaar65f08472017-09-10 18:16:20 +02007912#define SPEC_CWORD (SPEC_HASH + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00007913 "<cWORD>", /* cursor WORD */
Bram Moolenaar65f08472017-09-10 18:16:20 +02007914#define SPEC_CCWORD (SPEC_CWORD + 1)
7915 "<cexpr>", /* expr under cursor */
7916#define SPEC_CEXPR (SPEC_CCWORD + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00007917 "<cfile>", /* cursor path name */
Bram Moolenaar65f08472017-09-10 18:16:20 +02007918#define SPEC_CFILE (SPEC_CEXPR + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00007919 "<sfile>", /* ":so" file name */
Bram Moolenaar65f08472017-09-10 18:16:20 +02007920#define SPEC_SFILE (SPEC_CFILE + 1)
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01007921 "<slnum>", /* ":so" file line number */
Bram Moolenaar65f08472017-09-10 18:16:20 +02007922#define SPEC_SLNUM (SPEC_SFILE + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00007923 "<afile>", /* autocommand file name */
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02007924#define SPEC_AFILE (SPEC_SLNUM + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00007925 "<abuf>", /* autocommand buffer number */
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02007926#define SPEC_ABUF (SPEC_AFILE + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00007927 "<amatch>", /* autocommand match name */
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007928#define SPEC_AMATCH (SPEC_ABUF + 1)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02007929 "<sflnum>", /* script file line number */
7930#define SPEC_SFLNUM (SPEC_AMATCH + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00007931#ifdef FEAT_CLIENTSERVER
7932 "<client>"
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02007933# define SPEC_CLIENT (SPEC_SFLNUM + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00007934#endif
7935 };
Bram Moolenaar05bb9532008-07-04 09:44:11 +00007936
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00007937 for (i = 0; i < (int)(sizeof(spec_str) / sizeof(char *)); ++i)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00007938 {
7939 len = (int)STRLEN(spec_str[i]);
7940 if (STRNCMP(src, spec_str[i], len) == 0)
7941 {
7942 *usedlen = len;
7943 return i;
7944 }
7945 }
7946 return -1;
7947}
7948
7949/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007950 * Evaluate cmdline variables.
7951 *
7952 * change '%' to curbuf->b_ffname
7953 * '#' to curwin->w_altfile
7954 * '<cword>' to word under the cursor
7955 * '<cWORD>' to WORD under the cursor
Bram Moolenaar8071cb22019-07-12 17:58:01 +02007956 * '<cexpr>' to C-expression under the cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00007957 * '<cfile>' to path name under the cursor
7958 * '<sfile>' to sourced file name
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01007959 * '<slnum>' to sourced file line number
Bram Moolenaar071d4272004-06-13 20:20:40 +00007960 * '<afile>' to file name for autocommand
7961 * '<abuf>' to buffer number for autocommand
7962 * '<amatch>' to matching name for autocommand
7963 *
7964 * When an error is detected, "errormsg" is set to a non-NULL pointer (may be
7965 * "" for error without a message) and NULL is returned.
7966 * Returns an allocated string if a valid match was found.
7967 * Returns NULL if no match was found. "usedlen" then still contains the
7968 * number of characters to skip.
7969 */
7970 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007971eval_vars(
7972 char_u *src, /* pointer into commandline */
7973 char_u *srcstart, /* beginning of valid memory for src */
7974 int *usedlen, /* characters after src that are used */
7975 linenr_T *lnump, /* line number for :e command, or NULL */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007976 char **errormsg, /* pointer to error message */
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007977 int *escaped) /* return value has escaped white space (can
Bram Moolenaar63b92542007-03-27 14:57:09 +00007978 * be NULL) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007979{
7980 int i;
7981 char_u *s;
7982 char_u *result;
7983 char_u *resultbuf = NULL;
7984 int resultlen;
7985 buf_T *buf;
7986 int valid = VALID_HEAD + VALID_PATH; /* assume valid result */
7987 int spec_idx;
Bram Moolenaarfd249462018-07-28 16:14:30 +02007988 int tilde_file = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007989 int skip_mod = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007990 char_u strbuf[30];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007991
7992 *errormsg = NULL;
Bram Moolenaar63b92542007-03-27 14:57:09 +00007993 if (escaped != NULL)
7994 *escaped = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007995
7996 /*
7997 * Check if there is something to do.
7998 */
Bram Moolenaar05bb9532008-07-04 09:44:11 +00007999 spec_idx = find_cmdline_var(src, usedlen);
8000 if (spec_idx < 0) /* no match */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008001 {
8002 *usedlen = 1;
8003 return NULL;
8004 }
8005
8006 /*
8007 * Skip when preceded with a backslash "\%" and "\#".
8008 * Note: In "\\%" the % is also not recognized!
8009 */
8010 if (src > srcstart && src[-1] == '\\')
8011 {
8012 *usedlen = 0;
Bram Moolenaara7241f52008-06-24 20:39:31 +00008013 STRMOVE(src - 1, src); /* remove backslash */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008014 return NULL;
8015 }
8016
8017 /*
8018 * word or WORD under cursor
8019 */
Bram Moolenaar65f08472017-09-10 18:16:20 +02008020 if (spec_idx == SPEC_CWORD || spec_idx == SPEC_CCWORD
8021 || spec_idx == SPEC_CEXPR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008022 {
Bram Moolenaar65f08472017-09-10 18:16:20 +02008023 resultlen = find_ident_under_cursor(&result,
8024 spec_idx == SPEC_CWORD ? (FIND_IDENT | FIND_STRING)
8025 : spec_idx == SPEC_CEXPR ? (FIND_IDENT | FIND_STRING | FIND_EVAL)
8026 : FIND_STRING);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008027 if (resultlen == 0)
8028 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008029 *errormsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +00008030 return NULL;
8031 }
8032 }
8033
8034 /*
8035 * '#': Alternate file name
8036 * '%': Current file name
8037 * File name under the cursor
8038 * File name for autocommand
8039 * and following modifiers
8040 */
8041 else
8042 {
8043 switch (spec_idx)
8044 {
8045 case SPEC_PERC: /* '%': current file */
8046 if (curbuf->b_fname == NULL)
8047 {
8048 result = (char_u *)"";
8049 valid = 0; /* Must have ":p:h" to be valid */
8050 }
8051 else
Bram Moolenaar00136dc2018-07-25 21:19:13 +02008052 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008053 result = curbuf->b_fname;
Bram Moolenaar00136dc2018-07-25 21:19:13 +02008054 tilde_file = STRCMP(result, "~") == 0;
8055 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008056 break;
8057
8058 case SPEC_HASH: /* '#' or "#99": alternate file */
8059 if (src[1] == '#') /* "##": the argument list */
8060 {
8061 result = arg_all();
8062 resultbuf = result;
8063 *usedlen = 2;
Bram Moolenaar63b92542007-03-27 14:57:09 +00008064 if (escaped != NULL)
8065 *escaped = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008066 skip_mod = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008067 break;
8068 }
8069 s = src + 1;
Bram Moolenaard812df62008-11-09 12:46:09 +00008070 if (*s == '<') /* "#<99" uses v:oldfiles */
8071 ++s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008072 i = (int)getdigits(&s);
Bram Moolenaarc312b8b2017-10-28 17:53:04 +02008073 if (s == src + 2 && src[1] == '-')
8074 /* just a minus sign, don't skip over it */
8075 s--;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008076 *usedlen = (int)(s - src); /* length of what we expand */
8077
Bram Moolenaarc312b8b2017-10-28 17:53:04 +02008078 if (src[1] == '<' && i != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008079 {
Bram Moolenaard812df62008-11-09 12:46:09 +00008080 if (*usedlen < 2)
8081 {
8082 /* Should we give an error message for #<text? */
8083 *usedlen = 1;
8084 return NULL;
8085 }
8086#ifdef FEAT_EVAL
8087 result = list_find_str(get_vim_var_list(VV_OLDFILES),
8088 (long)i);
8089 if (result == NULL)
8090 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008091 *errormsg = "";
Bram Moolenaard812df62008-11-09 12:46:09 +00008092 return NULL;
8093 }
8094#else
Bram Moolenaar8e481e82019-01-15 20:07:48 +01008095 *errormsg = _("E809: #< is not available without the +eval feature");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008096 return NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +00008097#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008098 }
8099 else
Bram Moolenaard812df62008-11-09 12:46:09 +00008100 {
Bram Moolenaarc312b8b2017-10-28 17:53:04 +02008101 if (i == 0 && src[1] == '<' && *usedlen > 1)
8102 *usedlen = 1;
Bram Moolenaard812df62008-11-09 12:46:09 +00008103 buf = buflist_findnr(i);
8104 if (buf == NULL)
8105 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008106 *errormsg = _("E194: No alternate file name to substitute for '#'");
Bram Moolenaard812df62008-11-09 12:46:09 +00008107 return NULL;
8108 }
8109 if (lnump != NULL)
8110 *lnump = ECMD_LAST;
8111 if (buf->b_fname == NULL)
8112 {
8113 result = (char_u *)"";
8114 valid = 0; /* Must have ":p:h" to be valid */
8115 }
8116 else
Bram Moolenaar00136dc2018-07-25 21:19:13 +02008117 {
Bram Moolenaard812df62008-11-09 12:46:09 +00008118 result = buf->b_fname;
Bram Moolenaar00136dc2018-07-25 21:19:13 +02008119 tilde_file = STRCMP(result, "~") == 0;
8120 }
Bram Moolenaard812df62008-11-09 12:46:09 +00008121 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008122 break;
8123
8124#ifdef FEAT_SEARCHPATH
8125 case SPEC_CFILE: /* file name under cursor */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008126 result = file_name_at_cursor(FNAME_MESS|FNAME_HYP, 1L, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008127 if (result == NULL)
8128 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008129 *errormsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +00008130 return NULL;
8131 }
8132 resultbuf = result; /* remember allocated string */
8133 break;
8134#endif
8135
Bram Moolenaar071d4272004-06-13 20:20:40 +00008136 case SPEC_AFILE: /* file name for autocommand */
8137 result = autocmd_fname;
Bram Moolenaarf6dad432008-09-18 19:29:58 +00008138 if (result != NULL && !autocmd_fname_full)
8139 {
8140 /* Still need to turn the fname into a full path. It is
8141 * postponed to avoid a delay when <afile> is not used. */
8142 autocmd_fname_full = TRUE;
8143 result = FullName_save(autocmd_fname, FALSE);
8144 vim_free(autocmd_fname);
8145 autocmd_fname = result;
8146 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008147 if (result == NULL)
8148 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008149 *errormsg = _("E495: no autocommand file name to substitute for \"<afile>\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008150 return NULL;
8151 }
Bram Moolenaara0174af2008-01-02 20:08:25 +00008152 result = shorten_fname1(result);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008153 break;
8154
8155 case SPEC_ABUF: /* buffer number for autocommand */
8156 if (autocmd_bufnr <= 0)
8157 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008158 *errormsg = _("E496: no autocommand buffer number to substitute for \"<abuf>\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008159 return NULL;
8160 }
8161 sprintf((char *)strbuf, "%d", autocmd_bufnr);
8162 result = strbuf;
8163 break;
8164
8165 case SPEC_AMATCH: /* match name for autocommand */
8166 result = autocmd_match;
8167 if (result == NULL)
8168 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008169 *errormsg = _("E497: no autocommand match name to substitute for \"<amatch>\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008170 return NULL;
8171 }
8172 break;
8173
Bram Moolenaar071d4272004-06-13 20:20:40 +00008174 case SPEC_SFILE: /* file name for ":so" command */
8175 result = sourcing_name;
8176 if (result == NULL)
8177 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008178 *errormsg = _("E498: no :source file name to substitute for \"<sfile>\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008179 return NULL;
8180 }
8181 break;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008182
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01008183 case SPEC_SLNUM: /* line in file for ":so" command */
8184 if (sourcing_name == NULL || sourcing_lnum == 0)
8185 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008186 *errormsg = _("E842: no line number to use for \"<slnum>\"");
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01008187 return NULL;
8188 }
8189 sprintf((char *)strbuf, "%ld", (long)sourcing_lnum);
8190 result = strbuf;
8191 break;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008192
8193#ifdef FEAT_EVAL
8194 case SPEC_SFLNUM: /* line in script file */
8195 if (current_sctx.sc_lnum + sourcing_lnum == 0)
8196 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008197 *errormsg = _("E961: no line number to use for \"<sflnum>\"");
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008198 return NULL;
8199 }
8200 sprintf((char *)strbuf, "%ld",
8201 (long)(current_sctx.sc_lnum + sourcing_lnum));
8202 result = strbuf;
8203 break;
8204#endif
8205
8206#ifdef FEAT_CLIENTSERVER
Bram Moolenaar071d4272004-06-13 20:20:40 +00008207 case SPEC_CLIENT: /* Source of last submitted input */
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008208 sprintf((char *)strbuf, PRINTF_HEX_LONG_U,
8209 (long_u)clientWindow);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008210 result = strbuf;
8211 break;
8212#endif
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008213
Bram Moolenaar9e0f6ec2017-05-16 09:36:54 +02008214 default:
8215 result = (char_u *)""; /* avoid gcc warning */
8216 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008217 }
8218
8219 resultlen = (int)STRLEN(result); /* length of new string */
8220 if (src[*usedlen] == '<') /* remove the file name extension */
8221 {
8222 ++*usedlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008223 if ((s = vim_strrchr(result, '.')) != NULL && s >= gettail(result))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008224 resultlen = (int)(s - result);
8225 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008226 else if (!skip_mod)
8227 {
Bram Moolenaar00136dc2018-07-25 21:19:13 +02008228 valid |= modify_fname(src, tilde_file, usedlen, &result, &resultbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008229 &resultlen);
8230 if (result == NULL)
8231 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008232 *errormsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +00008233 return NULL;
8234 }
8235 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008236 }
8237
8238 if (resultlen == 0 || valid != VALID_HEAD + VALID_PATH)
8239 {
8240 if (valid != VALID_HEAD + VALID_PATH)
8241 /* xgettext:no-c-format */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008242 *errormsg = _("E499: Empty file name for '%' or '#', only works with \":p:h\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008243 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008244 *errormsg = _("E500: Evaluates to an empty string");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008245 result = NULL;
8246 }
8247 else
8248 result = vim_strnsave(result, resultlen);
8249 vim_free(resultbuf);
8250 return result;
8251}
8252
8253/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008254 * Expand the <sfile> string in "arg".
8255 *
8256 * Returns an allocated string, or NULL for any error.
8257 */
8258 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008259expand_sfile(char_u *arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008260{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008261 char *errormsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008262 int len;
8263 char_u *result;
8264 char_u *newres;
8265 char_u *repl;
8266 int srclen;
8267 char_u *p;
8268
8269 result = vim_strsave(arg);
8270 if (result == NULL)
8271 return NULL;
8272
8273 for (p = result; *p; )
8274 {
8275 if (STRNCMP(p, "<sfile>", 7) != 0)
8276 ++p;
8277 else
8278 {
8279 /* replace "<sfile>" with the sourced file name, and do ":" stuff */
Bram Moolenaar63b92542007-03-27 14:57:09 +00008280 repl = eval_vars(p, result, &srclen, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008281 if (errormsg != NULL)
8282 {
8283 if (*errormsg)
8284 emsg(errormsg);
8285 vim_free(result);
8286 return NULL;
8287 }
8288 if (repl == NULL) /* no match (cannot happen) */
8289 {
8290 p += srclen;
8291 continue;
8292 }
8293 len = (int)STRLEN(result) - srclen + (int)STRLEN(repl) + 1;
8294 newres = alloc(len);
8295 if (newres == NULL)
8296 {
8297 vim_free(repl);
8298 vim_free(result);
8299 return NULL;
8300 }
8301 mch_memmove(newres, result, (size_t)(p - result));
8302 STRCPY(newres + (p - result), repl);
8303 len = (int)STRLEN(newres);
8304 STRCAT(newres, p + srclen);
8305 vim_free(repl);
8306 vim_free(result);
8307 result = newres;
8308 p = newres + len; /* continue after the match */
8309 }
8310 }
8311
8312 return result;
8313}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008314
Bram Moolenaar071d4272004-06-13 20:20:40 +00008315#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG) || defined(PROTO)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00008316/*
Bram Moolenaard9462e32011-04-11 21:35:11 +02008317 * Make a dialog message in "buff[DIALOG_MSG_SIZE]".
Bram Moolenaarb765d632005-06-07 21:00:02 +00008318 * "format" must contain "%s".
Bram Moolenaar9c13b352005-05-19 20:53:52 +00008319 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008320 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008321dialog_msg(char_u *buff, char *format, char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008322{
Bram Moolenaar071d4272004-06-13 20:20:40 +00008323 if (fname == NULL)
8324 fname = (char_u *)_("Untitled");
Bram Moolenaard9462e32011-04-11 21:35:11 +02008325 vim_snprintf((char *)buff, DIALOG_MSG_SIZE, format, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008326}
8327#endif
8328
8329/*
8330 * ":behave {mswin,xterm}"
8331 */
8332 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008333ex_behave(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008334{
8335 if (STRCMP(eap->arg, "mswin") == 0)
8336 {
8337 set_option_value((char_u *)"selection", 0L, (char_u *)"exclusive", 0);
8338 set_option_value((char_u *)"selectmode", 0L, (char_u *)"mouse,key", 0);
8339 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"popup", 0);
8340 set_option_value((char_u *)"keymodel", 0L,
8341 (char_u *)"startsel,stopsel", 0);
8342 }
8343 else if (STRCMP(eap->arg, "xterm") == 0)
8344 {
8345 set_option_value((char_u *)"selection", 0L, (char_u *)"inclusive", 0);
8346 set_option_value((char_u *)"selectmode", 0L, (char_u *)"", 0);
8347 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"extend", 0);
8348 set_option_value((char_u *)"keymodel", 0L, (char_u *)"", 0);
8349 }
8350 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008351 semsg(_(e_invarg2), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008352}
8353
Bram Moolenaar071d4272004-06-13 20:20:40 +00008354static int filetype_detect = FALSE;
8355static int filetype_plugin = FALSE;
8356static int filetype_indent = FALSE;
8357
8358/*
8359 * ":filetype [plugin] [indent] {on,off,detect}"
8360 * on: Load the filetype.vim file to install autocommands for file types.
8361 * off: Load the ftoff.vim file to remove all autocommands for file types.
8362 * plugin on: load filetype.vim and ftplugin.vim
8363 * plugin off: load ftplugof.vim
8364 * indent on: load filetype.vim and indent.vim
8365 * indent off: load indoff.vim
8366 */
8367 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008368ex_filetype(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008369{
8370 char_u *arg = eap->arg;
8371 int plugin = FALSE;
8372 int indent = FALSE;
8373
8374 if (*eap->arg == NUL)
8375 {
8376 /* Print current status. */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008377 smsg("filetype detection:%s plugin:%s indent:%s",
Bram Moolenaar071d4272004-06-13 20:20:40 +00008378 filetype_detect ? "ON" : "OFF",
8379 filetype_plugin ? (filetype_detect ? "ON" : "(on)") : "OFF",
8380 filetype_indent ? (filetype_detect ? "ON" : "(on)") : "OFF");
8381 return;
8382 }
8383
8384 /* Accept "plugin" and "indent" in any order. */
8385 for (;;)
8386 {
8387 if (STRNCMP(arg, "plugin", 6) == 0)
8388 {
8389 plugin = TRUE;
8390 arg = skipwhite(arg + 6);
8391 continue;
8392 }
8393 if (STRNCMP(arg, "indent", 6) == 0)
8394 {
8395 indent = TRUE;
8396 arg = skipwhite(arg + 6);
8397 continue;
8398 }
8399 break;
8400 }
8401 if (STRCMP(arg, "on") == 0 || STRCMP(arg, "detect") == 0)
8402 {
8403 if (*arg == 'o' || !filetype_detect)
8404 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01008405 source_runtime((char_u *)FILETYPE_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008406 filetype_detect = TRUE;
8407 if (plugin)
8408 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01008409 source_runtime((char_u *)FTPLUGIN_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008410 filetype_plugin = TRUE;
8411 }
8412 if (indent)
8413 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01008414 source_runtime((char_u *)INDENT_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008415 filetype_indent = TRUE;
8416 }
8417 }
8418 if (*arg == 'd')
8419 {
Bram Moolenaar1610d052016-06-09 22:53:01 +02008420 (void)do_doautocmd((char_u *)"filetypedetect BufRead", TRUE, NULL);
Bram Moolenaara3227e22006-03-08 21:32:40 +00008421 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008422 }
8423 }
8424 else if (STRCMP(arg, "off") == 0)
8425 {
8426 if (plugin || indent)
8427 {
8428 if (plugin)
8429 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01008430 source_runtime((char_u *)FTPLUGOF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008431 filetype_plugin = FALSE;
8432 }
8433 if (indent)
8434 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01008435 source_runtime((char_u *)INDOFF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008436 filetype_indent = FALSE;
8437 }
8438 }
8439 else
8440 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01008441 source_runtime((char_u *)FTOFF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008442 filetype_detect = FALSE;
8443 }
8444 }
8445 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008446 semsg(_(e_invarg2), arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008447}
8448
8449/*
Bram Moolenaar3e545692017-06-04 19:00:32 +02008450 * ":setfiletype [FALLBACK] {name}"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008451 */
8452 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008453ex_setfiletype(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008454{
8455 if (!did_filetype)
Bram Moolenaar3e545692017-06-04 19:00:32 +02008456 {
8457 char_u *arg = eap->arg;
8458
8459 if (STRNCMP(arg, "FALLBACK ", 9) == 0)
8460 arg += 9;
8461
8462 set_option_value((char_u *)"filetype", 0L, arg, OPT_LOCAL);
8463 if (arg != eap->arg)
8464 did_filetype = FALSE;
8465 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008466}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008467
Bram Moolenaar071d4272004-06-13 20:20:40 +00008468 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008469ex_digraphs(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008470{
8471#ifdef FEAT_DIGRAPHS
8472 if (*eap->arg != NUL)
8473 putdigraph(eap->arg);
8474 else
Bram Moolenaareae8ae12018-12-14 18:53:02 +01008475 listdigraphs(eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008476#else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008477 emsg(_("E196: No digraphs in this version"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008478#endif
8479}
8480
8481 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008482ex_set(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008483{
8484 int flags = 0;
8485
8486 if (eap->cmdidx == CMD_setlocal)
8487 flags = OPT_LOCAL;
8488 else if (eap->cmdidx == CMD_setglobal)
8489 flags = OPT_GLOBAL;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01008490#if defined(FEAT_EVAL) && defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008491 if (cmdmod.browse && flags == 0)
8492 ex_options(eap);
8493 else
8494#endif
8495 (void)do_set(eap->arg, flags);
8496}
8497
Bram Moolenaar451fc7b2018-04-27 22:53:07 +02008498#if defined(FEAT_SEARCH_EXTRA) || defined(PROTO)
8499 void
8500set_no_hlsearch(int flag)
8501{
8502 no_hlsearch = flag;
8503# ifdef FEAT_EVAL
8504 set_vim_var_nr(VV_HLSEARCH, !no_hlsearch && p_hls);
8505# endif
8506}
8507
Bram Moolenaar071d4272004-06-13 20:20:40 +00008508/*
8509 * ":nohlsearch"
8510 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008511 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008512ex_nohlsearch(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008513{
Bram Moolenaar451fc7b2018-04-27 22:53:07 +02008514 set_no_hlsearch(TRUE);
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00008515 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008516}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008517#endif
8518
8519#ifdef FEAT_CRYPT
8520/*
8521 * ":X": Get crypt key
8522 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008523 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008524ex_X(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008525{
Bram Moolenaar3a0c9082014-11-12 15:15:42 +01008526 crypt_check_current_method();
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02008527 (void)crypt_get_key(TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008528}
8529#endif
8530
8531#ifdef FEAT_FOLDING
8532 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008533ex_fold(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008534{
8535 if (foldManualAllowed(TRUE))
8536 foldCreate(eap->line1, eap->line2);
8537}
8538
8539 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008540ex_foldopen(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008541{
8542 opFoldRange(eap->line1, eap->line2, eap->cmdidx == CMD_foldopen,
8543 eap->forceit, FALSE);
8544}
8545
8546 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008547ex_folddo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008548{
8549 linenr_T lnum;
8550
Bram Moolenaar4facea32019-10-12 20:17:40 +02008551# ifdef FEAT_CLIPBOARD
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02008552 start_global_changes();
Bram Moolenaar4facea32019-10-12 20:17:40 +02008553# endif
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02008554
Bram Moolenaar071d4272004-06-13 20:20:40 +00008555 /* First set the marks for all lines closed/open. */
8556 for (lnum = eap->line1; lnum <= eap->line2; ++lnum)
8557 if (hasFolding(lnum, NULL, NULL) == (eap->cmdidx == CMD_folddoclosed))
8558 ml_setmarked(lnum);
8559
8560 /* Execute the command on the marked lines. */
8561 global_exe(eap->arg);
8562 ml_clearmarked(); /* clear rest of the marks */
Bram Moolenaar4facea32019-10-12 20:17:40 +02008563# ifdef FEAT_CLIPBOARD
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02008564 end_global_changes();
Bram Moolenaar4facea32019-10-12 20:17:40 +02008565# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008566}
8567#endif
Bram Moolenaarf5291f32017-09-14 22:55:37 +02008568
Bram Moolenaar39665952018-08-15 20:59:48 +02008569#ifdef FEAT_QUICKFIX
8570/*
8571 * Returns TRUE if the supplied Ex cmdidx is for a location list command
8572 * instead of a quickfix command.
8573 */
8574 int
8575is_loclist_cmd(int cmdidx)
8576{
Bram Moolenaar74c8be22018-08-23 22:51:40 +02008577 if (cmdidx < 0 || cmdidx >= CMD_SIZE)
Bram Moolenaar39665952018-08-15 20:59:48 +02008578 return FALSE;
8579 return cmdnames[cmdidx].cmd_name[0] == 'l';
8580}
8581#endif
8582
Bram Moolenaar4facea32019-10-12 20:17:40 +02008583#if defined(FEAT_TIMERS) || defined(PROTO)
Bram Moolenaarf5291f32017-09-14 22:55:37 +02008584 int
8585get_pressedreturn(void)
8586{
8587 return ex_pressedreturn;
8588}
8589
8590 void
8591set_pressedreturn(int val)
8592{
8593 ex_pressedreturn = val;
8594}
8595#endif