blob: cea3936a5b3c424a833bf46777bc34122dd30fd3 [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 **);
47static char_u *skip_cmd_arg(char_u *p, int rembs);
48static int getargopt(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +000049#ifndef FEAT_QUICKFIX
50# define ex_make ex_ni
Bram Moolenaar86b68352004-12-27 21:59:20 +000051# define ex_cbuffer ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +000052# define ex_cc ex_ni
53# define ex_cnext ex_ni
Bram Moolenaar3ff33112019-05-03 21:56:35 +020054# define ex_cbelow ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +000055# define ex_cfile ex_ni
56# define qf_list ex_ni
57# define qf_age ex_ni
Bram Moolenaarf6acffb2016-07-16 16:54:24 +020058# define qf_history ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +000059# define ex_helpgrep ex_ni
Bram Moolenaar86b68352004-12-27 21:59:20 +000060# define ex_vimgrep ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +000061#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +020062#if !defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +000063# define ex_cclose ex_ni
64# define ex_copen ex_ni
65# define ex_cwindow ex_ni
Bram Moolenaardcb17002016-07-07 18:58:59 +020066# define ex_cbottom ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +000067#endif
Bram Moolenaar1e015462005-09-25 22:16:38 +000068#if !defined(FEAT_QUICKFIX) || !defined(FEAT_EVAL)
69# define ex_cexpr ex_ni
70#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000071
Bram Moolenaarb7316892019-05-01 18:08:42 +020072static 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 +010073static void get_flags(exarg_T *eap);
Bram Moolenaar85363ab2010-07-18 13:58:26 +020074#if !defined(FEAT_PERL) \
75 || !defined(FEAT_PYTHON) || !defined(FEAT_PYTHON3) \
76 || !defined(FEAT_TCL) \
77 || !defined(FEAT_RUBY) \
78 || !defined(FEAT_LUA) \
79 || !defined(FEAT_MZSCHEME)
Bram Moolenaar7bb75552007-07-16 18:39:49 +000080# define HAVE_EX_SCRIPT_NI
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010081static void ex_script_ni(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +000082#endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010083static char *invalid_range(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010084static void correct_range(exarg_T *eap);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000085#ifdef FEAT_QUICKFIX
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010086static char_u *replace_makeprg(exarg_T *eap, char_u *p, char_u **cmdlinep);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000087#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010088static char_u *repl_cmdline(exarg_T *eap, char_u *src, int srclen, char_u *repl, char_u **cmdlinep);
89static void ex_highlight(exarg_T *eap);
90static void ex_colorscheme(exarg_T *eap);
91static void ex_quit(exarg_T *eap);
92static void ex_cquit(exarg_T *eap);
93static void ex_quit_all(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010094static void ex_close(exarg_T *eap);
95static void ex_win_close(int forceit, win_T *win, tabpage_T *tp);
96static void ex_only(exarg_T *eap);
97static void ex_resize(exarg_T *eap);
98static void ex_stag(exarg_T *eap);
99static void ex_tabclose(exarg_T *eap);
100static void ex_tabonly(exarg_T *eap);
101static void ex_tabnext(exarg_T *eap);
102static void ex_tabmove(exarg_T *eap);
103static void ex_tabs(exarg_T *eap);
Bram Moolenaar4033c552017-09-16 20:54:51 +0200104#if defined(FEAT_QUICKFIX)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100105static void ex_pclose(exarg_T *eap);
106static void ex_ptag(exarg_T *eap);
107static void ex_pedit(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000108#else
109# define ex_pclose ex_ni
110# define ex_ptag ex_ni
111# define ex_pedit ex_ni
112#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100113static void ex_hide(exarg_T *eap);
114static void ex_stop(exarg_T *eap);
115static void ex_exit(exarg_T *eap);
116static void ex_print(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000117#ifdef FEAT_BYTEOFF
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100118static void ex_goto(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000119#else
120# define ex_goto ex_ni
121#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100122static void ex_shell(exarg_T *eap);
123static void ex_preserve(exarg_T *eap);
124static void ex_recover(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100125static void ex_mode(exarg_T *eap);
126static void ex_wrongmodifier(exarg_T *eap);
127static void ex_find(exarg_T *eap);
128static void ex_open(exarg_T *eap);
129static void ex_edit(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000130#ifndef FEAT_GUI
131# define ex_gui ex_nogui
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100132static void ex_nogui(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000133#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +0100134#if defined(FEAT_GUI_MSWIN) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100135static void ex_tearoff(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000136#else
137# define ex_tearoff ex_ni
138#endif
Bram Moolenaar29a2c082018-03-05 21:06:23 +0100139#if (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK) \
140 || defined(FEAT_TERM_POPUP_MENU)) && defined(FEAT_MENU)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100141static void ex_popup(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000142#else
143# define ex_popup ex_ni
144#endif
145#ifndef FEAT_GUI_MSWIN
146# define ex_simalt ex_ni
147#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +0000148#if !defined(FEAT_GUI_MSWIN) && !defined(FEAT_GUI_GTK) && !defined(FEAT_GUI_MOTIF)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000149# define gui_mch_find_dialog ex_ni
150# define gui_mch_replace_dialog ex_ni
151#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +0000152#if !defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000153# define ex_helpfind ex_ni
154#endif
155#ifndef FEAT_CSCOPE
Bram Moolenaard4db7712016-11-12 19:16:46 +0100156# define ex_cscope ex_ni
157# define ex_scscope ex_ni
158# define ex_cstag ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000159#endif
160#ifndef FEAT_SYN_HL
161# define ex_syntax ex_ni
Bram Moolenaar860cae12010-06-05 23:22:07 +0200162# define ex_ownsyntax ex_ni
Bram Moolenaarf71a3db2006-03-12 21:50:18 +0000163#endif
Bram Moolenaarf7512552013-06-06 14:55:19 +0200164#if !defined(FEAT_SYN_HL) || !defined(FEAT_PROFILE)
Bram Moolenaar8a7f5a22013-06-06 14:01:46 +0200165# define ex_syntime ex_ni
166#endif
Bram Moolenaarf71a3db2006-03-12 21:50:18 +0000167#ifndef FEAT_SPELL
Bram Moolenaarb765d632005-06-07 21:00:02 +0000168# define ex_spell ex_ni
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000169# define ex_mkspell ex_ni
Bram Moolenaarf417f2b2005-06-23 22:29:21 +0000170# define ex_spelldump ex_ni
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000171# define ex_spellinfo ex_ni
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000172# define ex_spellrepall ex_ni
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000173#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200174#ifndef FEAT_PERSISTENT_UNDO
175# define ex_rundo ex_ni
176# define ex_wundo ex_ni
177#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200178#ifndef FEAT_LUA
179# define ex_lua ex_script_ni
180# define ex_luado ex_ni
181# define ex_luafile ex_ni
182#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000183#ifndef FEAT_MZSCHEME
184# define ex_mzscheme ex_script_ni
185# define ex_mzfile ex_ni
186#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000187#ifndef FEAT_PERL
188# define ex_perl ex_script_ni
189# define ex_perldo ex_ni
190#endif
191#ifndef FEAT_PYTHON
192# define ex_python ex_script_ni
Bram Moolenaard620aa92013-05-17 16:40:06 +0200193# define ex_pydo ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000194# define ex_pyfile ex_ni
195#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200196#ifndef FEAT_PYTHON3
Bram Moolenaar368373e2010-07-19 20:46:22 +0200197# define ex_py3 ex_script_ni
Bram Moolenaar3dab2802013-05-15 18:28:13 +0200198# define ex_py3do ex_ni
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200199# define ex_py3file ex_ni
200#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +0100201#if !defined(FEAT_PYTHON) && !defined(FEAT_PYTHON3)
202# define ex_pyx ex_script_ni
203# define ex_pyxdo ex_ni
204# define ex_pyxfile ex_ni
205#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000206#ifndef FEAT_TCL
207# define ex_tcl ex_script_ni
208# define ex_tcldo ex_ni
209# define ex_tclfile ex_ni
210#endif
211#ifndef FEAT_RUBY
212# define ex_ruby ex_script_ni
213# define ex_rubydo ex_ni
214# define ex_rubyfile ex_ni
215#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000216#ifndef FEAT_KEYMAP
217# define ex_loadkeymap ex_ni
218#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100219static void ex_swapname(exarg_T *eap);
220static void ex_syncbind(exarg_T *eap);
221static void ex_read(exarg_T *eap);
222static void ex_pwd(exarg_T *eap);
223static void ex_equal(exarg_T *eap);
224static void ex_sleep(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100225static void ex_winsize(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100226static void ex_wincmd(exarg_T *eap);
Bram Moolenaar843ee412004-06-30 16:16:41 +0000227#if defined(FEAT_GUI) || defined(UNIX) || defined(VMS) || defined(MSWIN)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100228static void ex_winpos(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000229#else
230# define ex_winpos ex_ni
231#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100232static void ex_operators(exarg_T *eap);
233static void ex_put(exarg_T *eap);
234static void ex_copymove(exarg_T *eap);
235static void ex_submagic(exarg_T *eap);
236static void ex_join(exarg_T *eap);
237static void ex_at(exarg_T *eap);
238static void ex_bang(exarg_T *eap);
239static void ex_undo(exarg_T *eap);
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200240#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100241static void ex_wundo(exarg_T *eap);
242static void ex_rundo(exarg_T *eap);
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200243#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100244static void ex_redo(exarg_T *eap);
245static void ex_later(exarg_T *eap);
246static void ex_redir(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100247static void ex_redrawstatus(exarg_T *eap);
Bram Moolenaare12bab32019-01-08 22:02:56 +0100248static void ex_redrawtabline(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100249static void close_redir(void);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100250static void ex_mark(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100251static void ex_startinsert(exarg_T *eap);
252static void ex_stopinsert(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000253#ifdef FEAT_FIND_ID
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100254static void ex_checkpath(exarg_T *eap);
255static void ex_findpat(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000256#else
257# define ex_findpat ex_ni
258# define ex_checkpath ex_ni
259#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +0200260#if defined(FEAT_FIND_ID) && defined(FEAT_QUICKFIX)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100261static void ex_psearch(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000262#else
263# define ex_psearch ex_ni
264#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100265static void ex_tag(exarg_T *eap);
266static void ex_tag_cmd(exarg_T *eap, char_u *name);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000267#ifndef FEAT_EVAL
Bram Moolenaare4ce8252019-08-04 15:30:16 +0200268# define ex_break ex_ni
269# define ex_breakadd ex_ni
270# define ex_breakdel ex_ni
271# define ex_breaklist ex_ni
272# define ex_call ex_ni
273# define ex_catch ex_ni
274# define ex_compiler ex_ni
275# define ex_const ex_ni
276# define ex_continue ex_ni
277# define ex_debug ex_ni
278# define ex_debuggreedy ex_ni
279# define ex_delfunction ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000280# define ex_echo ex_ni
281# define ex_echohl ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000282# define ex_else ex_ni
Bram Moolenaare4ce8252019-08-04 15:30:16 +0200283# define ex_endfunction ex_ni
284# define ex_endif ex_ni
285# define ex_endtry ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000286# define ex_endwhile ex_ni
Bram Moolenaare4ce8252019-08-04 15:30:16 +0200287# define ex_eval ex_ni
288# define ex_execute ex_ni
289# define ex_finally ex_ni
290# define ex_finish ex_ni
291# define ex_function ex_ni
292# define ex_if ex_ni
293# define ex_let ex_ni
294# define ex_lockvar ex_ni
295# define ex_oldfiles ex_ni
296# define ex_options ex_ni
297# define ex_packadd ex_ni
298# define ex_packloadall ex_ni
299# define ex_return ex_ni
300# define ex_scriptnames ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000301# define ex_throw ex_ni
302# define ex_try ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000303# define ex_unlet ex_ni
Bram Moolenaar65c1b012005-01-31 19:02:28 +0000304# define ex_unlockvar ex_ni
Bram Moolenaare4ce8252019-08-04 15:30:16 +0200305# define ex_while ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000306#endif
Bram Moolenaar84538072019-07-28 14:15:42 +0200307#ifndef FEAT_SESSION
Bram Moolenaar071d4272004-06-13 20:20:40 +0000308# define ex_loadview ex_ni
309#endif
Bram Moolenaardefa0672019-07-21 19:25:37 +0200310#ifndef FEAT_VIMINFO
Bram Moolenaar071d4272004-06-13 20:20:40 +0000311# define ex_viminfo ex_ni
312#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100313static void ex_behave(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100314static void ex_filetype(exarg_T *eap);
315static void ex_setfiletype(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000316#ifndef FEAT_DIFF
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000317# define ex_diffoff ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000318# define ex_diffpatch ex_ni
319# define ex_diffgetput ex_ni
320# define ex_diffsplit ex_ni
321# define ex_diffthis ex_ni
322# define ex_diffupdate ex_ni
323#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100324static void ex_digraphs(exarg_T *eap);
325static void ex_set(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000326#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100327static void ex_nohlsearch(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000328#else
329# define ex_nohlsearch ex_ni
330# define ex_match ex_ni
331#endif
332#ifdef FEAT_CRYPT
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100333static void ex_X(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000334#else
335# define ex_X ex_ni
336#endif
337#ifdef FEAT_FOLDING
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100338static void ex_fold(exarg_T *eap);
339static void ex_foldopen(exarg_T *eap);
340static void ex_folddo(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000341#else
342# define ex_fold ex_ni
343# define ex_foldopen ex_ni
344# define ex_folddo ex_ni
345#endif
Bram Moolenaar13505972019-01-24 15:04:48 +0100346#if !(defined(HAVE_LOCALE_H) || defined(X_LOCALE))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000347# define ex_language ex_ni
348#endif
349#ifndef FEAT_SIGNS
350# define ex_sign ex_ni
351#endif
Bram Moolenaar009b2592004-10-24 19:18:58 +0000352#ifndef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200353# define ex_nbclose ex_ni
Bram Moolenaar009b2592004-10-24 19:18:58 +0000354# define ex_nbkey ex_ni
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200355# define ex_nbstart ex_ni
Bram Moolenaar009b2592004-10-24 19:18:58 +0000356#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000357
Bram Moolenaar071d4272004-06-13 20:20:40 +0000358#ifndef FEAT_JUMPLIST
359# define ex_jumps ex_ni
Bram Moolenaar2d358992016-06-12 21:20:54 +0200360# define ex_clearjumps ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000361# define ex_changes ex_ni
362#endif
363
Bram Moolenaar05159a02005-02-26 23:04:13 +0000364#ifndef FEAT_PROFILE
365# define ex_profile ex_ni
366#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200367#ifndef FEAT_TERMINAL
368# define ex_terminal ex_ni
369#endif
Bram Moolenaard4aa83a2019-05-09 18:59:31 +0200370#if !defined(FEAT_X11) || !defined(FEAT_XCLIPBOARD)
371# define ex_xrestore ex_ni
372#endif
Bram Moolenaar682725c2019-05-25 20:10:37 +0200373#if !defined(FEAT_TEXT_PROP)
374# define ex_popupclear ex_ni
375#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +0000376
Bram Moolenaar071d4272004-06-13 20:20:40 +0000377/*
378 * Declare cmdnames[].
379 */
380#define DO_DECLARE_EXCMD
381#include "ex_cmds.h"
Bram Moolenaar6de5e122017-04-20 21:55:44 +0200382#include "ex_cmdidxs.h"
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +0100383
Bram Moolenaar071d4272004-06-13 20:20:40 +0000384static char_u dollar_command[2] = {'$', 0};
385
386
387#ifdef FEAT_EVAL
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000388/* Struct for storing a line inside a while/for loop */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000389typedef struct
390{
391 char_u *line; /* command line */
392 linenr_T lnum; /* sourcing_lnum of the line */
393} wcmd_T;
394
395/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000396 * Structure used to store info for line position in a while or for loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000397 * This is required, because do_one_cmd() may invoke ex_function(), which
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000398 * reads more lines that may come from the while/for loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000399 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000400struct loop_cookie
Bram Moolenaar071d4272004-06-13 20:20:40 +0000401{
402 garray_T *lines_gap; /* growarray with line info */
403 int current_line; /* last read line from growarray */
404 int repeating; /* TRUE when looping a second time */
405 /* When "repeating" is FALSE use "getline" and "cookie" to get lines */
Bram Moolenaare96a2492019-06-25 04:12:16 +0200406 char_u *(*getline)(int, void *, int, int);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000407 void *cookie;
408};
409
Bram Moolenaare96a2492019-06-25 04:12:16 +0200410static char_u *get_loop_line(int c, void *cookie, int indent, int do_concat);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100411static int store_loop_line(garray_T *gap, char_u *line);
412static void free_cmdlines(garray_T *gap);
Bram Moolenaared203462004-06-16 11:19:22 +0000413
414/* Struct to save a few things while debugging. Used in do_cmdline() only. */
415struct dbg_stuff
416{
417 int trylevel;
418 int force_abort;
419 except_T *caught_stack;
420 char_u *vv_exception;
421 char_u *vv_throwpoint;
422 int did_emsg;
423 int got_int;
424 int did_throw;
425 int need_rethrow;
426 int check_cstack;
427 except_T *current_exception;
428};
429
Bram Moolenaared203462004-06-16 11:19:22 +0000430 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100431save_dbg_stuff(struct dbg_stuff *dsp)
Bram Moolenaared203462004-06-16 11:19:22 +0000432{
433 dsp->trylevel = trylevel; trylevel = 0;
434 dsp->force_abort = force_abort; force_abort = FALSE;
435 dsp->caught_stack = caught_stack; caught_stack = NULL;
436 dsp->vv_exception = v_exception(NULL);
437 dsp->vv_throwpoint = v_throwpoint(NULL);
438
439 /* Necessary for debugging an inactive ":catch", ":finally", ":endtry" */
440 dsp->did_emsg = did_emsg; did_emsg = FALSE;
441 dsp->got_int = got_int; got_int = FALSE;
442 dsp->did_throw = did_throw; did_throw = FALSE;
443 dsp->need_rethrow = need_rethrow; need_rethrow = FALSE;
444 dsp->check_cstack = check_cstack; check_cstack = FALSE;
445 dsp->current_exception = current_exception; current_exception = NULL;
446}
447
448 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100449restore_dbg_stuff(struct dbg_stuff *dsp)
Bram Moolenaared203462004-06-16 11:19:22 +0000450{
451 suppress_errthrow = FALSE;
452 trylevel = dsp->trylevel;
453 force_abort = dsp->force_abort;
454 caught_stack = dsp->caught_stack;
455 (void)v_exception(dsp->vv_exception);
456 (void)v_throwpoint(dsp->vv_throwpoint);
457 did_emsg = dsp->did_emsg;
458 got_int = dsp->got_int;
459 did_throw = dsp->did_throw;
460 need_rethrow = dsp->need_rethrow;
461 check_cstack = dsp->check_cstack;
462 current_exception = dsp->current_exception;
463}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000464#endif
465
Bram Moolenaar071d4272004-06-13 20:20:40 +0000466/*
467 * do_exmode(): Repeatedly get commands for the "Ex" mode, until the ":vi"
468 * command is given.
469 */
470 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100471do_exmode(
472 int improved) /* TRUE for "improved Ex" mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000473{
474 int save_msg_scroll;
475 int prev_msg_row;
476 linenr_T prev_line;
Bram Moolenaar79518e22017-02-17 16:31:35 +0100477 varnumber_T changedtick;
Bram Moolenaardf177f62005-02-22 08:39:57 +0000478
479 if (improved)
480 exmode_active = EXMODE_VIM;
481 else
482 exmode_active = EXMODE_NORMAL;
483 State = NORMAL;
484
485 /* When using ":global /pat/ visual" and then "Q" we return to continue
486 * the :global command. */
487 if (global_busy)
488 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000489
490 save_msg_scroll = msg_scroll;
491 ++RedrawingDisabled; /* don't redisplay the window */
492 ++no_wait_return; /* don't wait for return */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000493#ifdef FEAT_GUI
494 /* Ignore scrollbar and mouse events in Ex mode */
495 ++hold_gui_events;
496#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000497
Bram Moolenaar32526b32019-01-19 17:43:09 +0100498 msg(_("Entering Ex mode. Type \"visual\" to go to Normal mode."));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000499 while (exmode_active)
500 {
Bram Moolenaar7c626922005-02-07 22:01:03 +0000501 /* Check for a ":normal" command and no more characters left. */
502 if (ex_normal_busy > 0 && typebuf.tb_len == 0)
503 {
504 exmode_active = FALSE;
505 break;
506 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000507 msg_scroll = TRUE;
508 need_wait_return = FALSE;
509 ex_pressedreturn = FALSE;
510 ex_no_reprint = FALSE;
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100511 changedtick = CHANGEDTICK(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000512 prev_msg_row = msg_row;
513 prev_line = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000514 if (improved)
515 {
516 cmdline_row = msg_row;
517 do_cmdline(NULL, getexline, NULL, 0);
518 }
519 else
520 do_cmdline(NULL, getexmodeline, NULL, DOCMD_NOWAIT);
521 lines_left = Rows - 1;
522
Bram Moolenaardf177f62005-02-22 08:39:57 +0000523 if ((prev_line != curwin->w_cursor.lnum
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100524 || changedtick != CHANGEDTICK(curbuf)) && !ex_no_reprint)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000525 {
Bram Moolenaardf177f62005-02-22 08:39:57 +0000526 if (curbuf->b_ml.ml_flags & ML_EMPTY)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100527 emsg(_(e_emptybuf));
Bram Moolenaardf177f62005-02-22 08:39:57 +0000528 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000529 {
Bram Moolenaardf177f62005-02-22 08:39:57 +0000530 if (ex_pressedreturn)
531 {
532 /* go up one line, to overwrite the ":<CR>" line, so the
Bram Moolenaar81870892007-11-11 18:17:28 +0000533 * output doesn't contain empty lines. */
Bram Moolenaardf177f62005-02-22 08:39:57 +0000534 msg_row = prev_msg_row;
535 if (prev_msg_row == Rows - 1)
536 msg_row--;
537 }
538 msg_col = 0;
539 print_line_no_prefix(curwin->w_cursor.lnum, FALSE, FALSE);
540 msg_clr_eos();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000541 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000542 }
Bram Moolenaardf177f62005-02-22 08:39:57 +0000543 else if (ex_pressedreturn && !ex_no_reprint) /* must be at EOF */
544 {
545 if (curbuf->b_ml.ml_flags & ML_EMPTY)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100546 emsg(_(e_emptybuf));
Bram Moolenaardf177f62005-02-22 08:39:57 +0000547 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100548 emsg(_("E501: At end-of-file"));
Bram Moolenaardf177f62005-02-22 08:39:57 +0000549 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000550 }
551
552#ifdef FEAT_GUI
553 --hold_gui_events;
554#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000555 --RedrawingDisabled;
556 --no_wait_return;
557 update_screen(CLEAR);
558 need_wait_return = FALSE;
559 msg_scroll = save_msg_scroll;
560}
561
562/*
563 * Execute a simple command line. Used for translated commands like "*".
564 */
565 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100566do_cmdline_cmd(char_u *cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000567{
568 return do_cmdline(cmd, NULL, NULL,
569 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
570}
571
572/*
573 * do_cmdline(): execute one Ex command line
574 *
575 * 1. Execute "cmdline" when it is not NULL.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100576 * If "cmdline" is NULL, or more lines are needed, fgetline() is used.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000577 * 2. Split up in parts separated with '|'.
578 *
579 * This function can be called recursively!
580 *
581 * flags:
582 * DOCMD_VERBOSE - The command will be included in the error message.
583 * DOCMD_NOWAIT - Don't call wait_return() and friends.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100584 * DOCMD_REPEAT - Repeat execution until fgetline() returns NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000585 * DOCMD_KEYTYPED - Don't reset KeyTyped.
586 * DOCMD_EXCRESET - Reset the exception environment (used for debugging).
587 * DOCMD_KEEPLINE - Store first typed line (for repeating with ".").
588 *
589 * return FAIL if cmdline could not be executed, OK otherwise
590 */
591 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100592do_cmdline(
593 char_u *cmdline,
Bram Moolenaare96a2492019-06-25 04:12:16 +0200594 char_u *(*fgetline)(int, void *, int, int),
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100595 void *cookie, /* argument for fgetline() */
596 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000597{
598 char_u *next_cmdline; /* next cmd to execute */
599 char_u *cmdline_copy = NULL; /* copy of cmd line */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100600 int used_getline = FALSE; /* used "fgetline" to obtain command */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000601 static int recursive = 0; /* recursive depth */
602 int msg_didout_before_start = 0;
603 int count = 0; /* line number count */
604 int did_inc = FALSE; /* incremented RedrawingDisabled */
605 int retval = OK;
606#ifdef FEAT_EVAL
607 struct condstack cstack; /* conditional stack */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000608 garray_T lines_ga; /* keep lines for ":while"/":for" */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000609 int current_line = 0; /* active line in lines_ga */
610 char_u *fname = NULL; /* function or script name */
611 linenr_T *breakpoint = NULL; /* ptr to breakpoint field in cookie */
612 int *dbg_tick = NULL; /* ptr to dbg_tick field in cookie */
Bram Moolenaared203462004-06-16 11:19:22 +0000613 struct dbg_stuff debug_saved; /* saved things for debug mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000614 int initial_trylevel;
615 struct msglist **saved_msg_list = NULL;
616 struct msglist *private_msg_list;
617
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100618 /* "fgetline" and "cookie" passed to do_one_cmd() */
Bram Moolenaare96a2492019-06-25 04:12:16 +0200619 char_u *(*cmd_getline)(int, void *, int, int);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000620 void *cmd_cookie;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000621 struct loop_cookie cmd_loop_cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000622 void *real_cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +0000623 int getline_is_func;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000624#else
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100625# define cmd_getline fgetline
Bram Moolenaar071d4272004-06-13 20:20:40 +0000626# define cmd_cookie cookie
627#endif
628 static int call_depth = 0; /* recursiveness */
629
630#ifdef FEAT_EVAL
631 /* For every pair of do_cmdline()/do_one_cmd() calls, use an extra memory
632 * location for storing error messages to be converted to an exception.
Bram Moolenaarcf3630f2005-01-08 16:04:29 +0000633 * This ensures that the do_errthrow() call in do_one_cmd() does not
634 * combine the messages stored by an earlier invocation of do_one_cmd()
635 * with the command name of the later one. This would happen when
636 * BufWritePost autocommands are executed after a write error. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000637 saved_msg_list = msg_list;
638 msg_list = &private_msg_list;
639 private_msg_list = NULL;
640#endif
641
642 /* It's possible to create an endless loop with ":execute", catch that
Bram Moolenaar777b30f2017-01-02 15:26:27 +0100643 * here. The value of 200 allows nested function calls, ":source", etc.
644 * Allow 200 or 'maxfuncdepth', whatever is larger. */
Bram Moolenaarb094ff42017-01-02 16:16:39 +0100645 if (call_depth >= 200
646#ifdef FEAT_EVAL
647 && call_depth >= p_mfd
648#endif
649 )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000650 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100651 emsg(_("E169: Command too recursive"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000652#ifdef FEAT_EVAL
653 /* When converting to an exception, we do not include the command name
654 * since this is not an error of the specific command. */
655 do_errthrow((struct condstack *)NULL, (char_u *)NULL);
656 msg_list = saved_msg_list;
657#endif
658 return FAIL;
659 }
660 ++call_depth;
661
662#ifdef FEAT_EVAL
663 cstack.cs_idx = -1;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000664 cstack.cs_looplevel = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000665 cstack.cs_trylevel = 0;
666 cstack.cs_emsg_silent_list = NULL;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000667 cstack.cs_lflags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000668 ga_init2(&lines_ga, (int)sizeof(wcmd_T), 10);
669
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100670 real_cookie = getline_cookie(fgetline, cookie);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000671
672 /* Inside a function use a higher nesting level. */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100673 getline_is_func = getline_equal(fgetline, cookie, get_func_line);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000674 if (getline_is_func && ex_nesting_level == func_level(real_cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000675 ++ex_nesting_level;
676
677 /* Get the function or script name and the address where the next breakpoint
678 * line and the debug tick for a function or script are stored. */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000679 if (getline_is_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000680 {
681 fname = func_name(real_cookie);
682 breakpoint = func_breakpoint(real_cookie);
683 dbg_tick = func_dbg_tick(real_cookie);
684 }
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100685 else if (getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000686 {
687 fname = sourcing_name;
688 breakpoint = source_breakpoint(real_cookie);
689 dbg_tick = source_dbg_tick(real_cookie);
690 }
691
692 /*
693 * Initialize "force_abort" and "suppress_errthrow" at the top level.
694 */
695 if (!recursive)
696 {
697 force_abort = FALSE;
698 suppress_errthrow = FALSE;
699 }
700
701 /*
702 * If requested, store and reset the global values controlling the
Bram Moolenaar89d40322006-08-29 15:30:07 +0000703 * exception handling (used when debugging). Otherwise clear it to avoid
704 * a bogus compiler warning when the optimizer uses inline functions...
Bram Moolenaar071d4272004-06-13 20:20:40 +0000705 */
Bram Moolenaarb4872942006-05-13 10:32:52 +0000706 if (flags & DOCMD_EXCRESET)
Bram Moolenaared203462004-06-16 11:19:22 +0000707 save_dbg_stuff(&debug_saved);
Bram Moolenaar89d40322006-08-29 15:30:07 +0000708 else
Bram Moolenaar69b67f72015-09-25 16:59:47 +0200709 vim_memset(&debug_saved, 0, sizeof(debug_saved));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000710
711 initial_trylevel = trylevel;
712
713 /*
714 * "did_throw" will be set to TRUE when an exception is being thrown.
715 */
716 did_throw = FALSE;
717#endif
718 /*
719 * "did_emsg" will be set to TRUE when emsg() is used, in which case we
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000720 * cancel the whole command line, and any if/endif or loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000721 * If force_abort is set, we cancel everything.
722 */
723 did_emsg = FALSE;
724
725 /*
726 * KeyTyped is only set when calling vgetc(). Reset it here when not
727 * calling vgetc() (sourced command lines).
728 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100729 if (!(flags & DOCMD_KEYTYPED)
730 && !getline_equal(fgetline, cookie, getexline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000731 KeyTyped = FALSE;
732
733 /*
734 * Continue executing command lines:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000735 * - when inside an ":if", ":while" or ":for"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000736 * - for multiple commands on one line, separated with '|'
737 * - when repeating until there are no more lines (for ":source")
738 */
739 next_cmdline = cmdline;
740 do
741 {
Bram Moolenaar05159a02005-02-26 23:04:13 +0000742#ifdef FEAT_EVAL
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100743 getline_is_func = getline_equal(fgetline, cookie, get_func_line);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000744#endif
745
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000746 /* stop skipping cmds for an error msg after all endif/while/for */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000747 if (next_cmdline == NULL
748#ifdef FEAT_EVAL
749 && !force_abort
750 && cstack.cs_idx < 0
Bram Moolenaar05159a02005-02-26 23:04:13 +0000751 && !(getline_is_func && func_has_abort(real_cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000752#endif
753 )
754 did_emsg = FALSE;
755
756 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000757 * 1. If repeating a line in a loop, get a line from lines_ga.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100758 * 2. If no line given: Get an allocated line with fgetline().
Bram Moolenaar071d4272004-06-13 20:20:40 +0000759 * 3. If a line is given: Make a copy, so we can mess with it.
760 */
761
762#ifdef FEAT_EVAL
763 /* 1. If repeating, get a previous line from lines_ga. */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000764 if (cstack.cs_looplevel > 0 && current_line < lines_ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000765 {
766 /* Each '|' separated command is stored separately in lines_ga, to
767 * be able to jump to it. Don't use next_cmdline now. */
Bram Moolenaard23a8232018-02-10 18:45:26 +0100768 VIM_CLEAR(cmdline_copy);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000769
770 /* Check if a function has returned or, unless it has an unclosed
771 * try conditional, aborted. */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000772 if (getline_is_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000773 {
Bram Moolenaar05159a02005-02-26 23:04:13 +0000774# ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000775 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +0000776 func_line_end(real_cookie);
777# endif
778 if (func_has_ended(real_cookie))
779 {
780 retval = FAIL;
781 break;
782 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000783 }
Bram Moolenaar05159a02005-02-26 23:04:13 +0000784#ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000785 else if (do_profiling == PROF_YES
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100786 && getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar05159a02005-02-26 23:04:13 +0000787 script_line_end();
788#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000789
790 /* Check if a sourced file hit a ":finish" command. */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100791 if (source_finished(fgetline, cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000792 {
793 retval = FAIL;
794 break;
795 }
796
797 /* If breakpoints have been added/deleted need to check for it. */
798 if (breakpoint != NULL && dbg_tick != NULL
799 && *dbg_tick != debug_tick)
800 {
801 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100802 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar071d4272004-06-13 20:20:40 +0000803 fname, sourcing_lnum);
804 *dbg_tick = debug_tick;
805 }
806
807 next_cmdline = ((wcmd_T *)(lines_ga.ga_data))[current_line].line;
808 sourcing_lnum = ((wcmd_T *)(lines_ga.ga_data))[current_line].lnum;
809
810 /* Did we encounter a breakpoint? */
811 if (breakpoint != NULL && *breakpoint != 0
812 && *breakpoint <= sourcing_lnum)
813 {
814 dbg_breakpoint(fname, sourcing_lnum);
815 /* Find next breakpoint. */
816 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100817 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar071d4272004-06-13 20:20:40 +0000818 fname, sourcing_lnum);
819 *dbg_tick = debug_tick;
820 }
Bram Moolenaar05159a02005-02-26 23:04:13 +0000821# ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000822 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +0000823 {
824 if (getline_is_func)
825 func_line_start(real_cookie);
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100826 else if (getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar05159a02005-02-26 23:04:13 +0000827 script_line_start();
828 }
829# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000830 }
831
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000832 if (cstack.cs_looplevel > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000833 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000834 /* Inside a while/for loop we need to store the lines and use them
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100835 * again. Pass a different "fgetline" function to do_one_cmd()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000836 * below, so that it stores lines in or reads them from
837 * "lines_ga". Makes it possible to define a function inside a
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000838 * while/for loop. */
839 cmd_getline = get_loop_line;
840 cmd_cookie = (void *)&cmd_loop_cookie;
841 cmd_loop_cookie.lines_gap = &lines_ga;
842 cmd_loop_cookie.current_line = current_line;
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100843 cmd_loop_cookie.getline = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000844 cmd_loop_cookie.cookie = cookie;
845 cmd_loop_cookie.repeating = (current_line < lines_ga.ga_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000846 }
847 else
848 {
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100849 cmd_getline = fgetline;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000850 cmd_cookie = cookie;
851 }
852#endif
853
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100854 /* 2. If no line given, get an allocated line with fgetline(). */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000855 if (next_cmdline == NULL)
856 {
857 /*
858 * Need to set msg_didout for the first line after an ":if",
859 * otherwise the ":if" will be overwritten.
860 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100861 if (count == 1 && getline_equal(fgetline, cookie, getexline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000862 msg_didout = TRUE;
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100863 if (fgetline == NULL || (next_cmdline = fgetline(':', cookie,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000864#ifdef FEAT_EVAL
865 cstack.cs_idx < 0 ? 0 : (cstack.cs_idx + 1) * 2
866#else
867 0
868#endif
Bram Moolenaare96a2492019-06-25 04:12:16 +0200869 , TRUE)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000870 {
871 /* Don't call wait_return for aborted command line. The NULL
872 * returned for the end of a sourced file or executed function
873 * doesn't do this. */
874 if (KeyTyped && !(flags & DOCMD_REPEAT))
875 need_wait_return = FALSE;
876 retval = FAIL;
877 break;
878 }
879 used_getline = TRUE;
880
881 /*
882 * Keep the first typed line. Clear it when more lines are typed.
883 */
884 if (flags & DOCMD_KEEPLINE)
885 {
886 vim_free(repeat_cmdline);
887 if (count == 0)
888 repeat_cmdline = vim_strsave(next_cmdline);
889 else
890 repeat_cmdline = NULL;
891 }
892 }
893
894 /* 3. Make a copy of the command so we can mess with it. */
895 else if (cmdline_copy == NULL)
896 {
897 next_cmdline = vim_strsave(next_cmdline);
898 if (next_cmdline == NULL)
899 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100900 emsg(_(e_outofmem));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000901 retval = FAIL;
902 break;
903 }
904 }
905 cmdline_copy = next_cmdline;
906
907#ifdef FEAT_EVAL
908 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000909 * Save the current line when inside a ":while" or ":for", and when
910 * the command looks like a ":while" or ":for", because we may need it
911 * later. When there is a '|' and another command, it is stored
912 * separately, because we need to be able to jump back to it from an
913 * :endwhile/:endfor.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000914 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000915 if (current_line == lines_ga.ga_len
916 && (cstack.cs_looplevel || has_loop_cmd(next_cmdline)))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000917 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000918 if (store_loop_line(&lines_ga, next_cmdline) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000919 {
920 retval = FAIL;
921 break;
922 }
923 }
924 did_endif = FALSE;
925#endif
926
927 if (count++ == 0)
928 {
929 /*
930 * All output from the commands is put below each other, without
931 * waiting for a return. Don't do this when executing commands
932 * from a script or when being called recursive (e.g. for ":e
933 * +command file").
934 */
935 if (!(flags & DOCMD_NOWAIT) && !recursive)
936 {
937 msg_didout_before_start = msg_didout;
938 msg_didany = FALSE; /* no output yet */
939 msg_start();
940 msg_scroll = TRUE; /* put messages below each other */
Bram Moolenaar84a05ac2013-05-06 04:24:17 +0200941 ++no_wait_return; /* don't wait for return until finished */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000942 ++RedrawingDisabled;
943 did_inc = TRUE;
944 }
945 }
946
947 if (p_verbose >= 15 && sourcing_name != NULL)
948 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000949 ++no_wait_return;
Bram Moolenaar8b044b32005-05-31 22:05:58 +0000950 verbose_enter_scroll();
951
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100952 smsg(_("line %ld: %s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +0000953 (long)sourcing_lnum, cmdline_copy);
Bram Moolenaar8b044b32005-05-31 22:05:58 +0000954 if (msg_silent == 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +0100955 msg_puts("\n"); /* don't overwrite this */
Bram Moolenaar8b044b32005-05-31 22:05:58 +0000956
957 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000958 --no_wait_return;
959 }
960
961 /*
962 * 2. Execute one '|' separated command.
963 * do_one_cmd() will return NULL if there is no trailing '|'.
964 * "cmdline_copy" can change, e.g. for '%' and '#' expansion.
965 */
966 ++recursive;
967 next_cmdline = do_one_cmd(&cmdline_copy, flags & DOCMD_VERBOSE,
968#ifdef FEAT_EVAL
969 &cstack,
970#endif
971 cmd_getline, cmd_cookie);
972 --recursive;
973
974#ifdef FEAT_EVAL
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000975 if (cmd_cookie == (void *)&cmd_loop_cookie)
976 /* Use "current_line" from "cmd_loop_cookie", it may have been
Bram Moolenaar071d4272004-06-13 20:20:40 +0000977 * incremented when defining a function. */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000978 current_line = cmd_loop_cookie.current_line;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000979#endif
980
981 if (next_cmdline == NULL)
982 {
Bram Moolenaard23a8232018-02-10 18:45:26 +0100983 VIM_CLEAR(cmdline_copy);
Bram Moolenaard7663c22019-08-06 21:59:57 +0200984
Bram Moolenaar071d4272004-06-13 20:20:40 +0000985 /*
986 * If the command was typed, remember it for the ':' register.
987 * Do this AFTER executing the command to make :@: work.
988 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100989 if (getline_equal(fgetline, cookie, getexline)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000990 && new_last_cmdline != NULL)
991 {
992 vim_free(last_cmdline);
993 last_cmdline = new_last_cmdline;
994 new_last_cmdline = NULL;
995 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000996 }
997 else
998 {
999 /* need to copy the command after the '|' to cmdline_copy, for the
1000 * next do_one_cmd() */
Bram Moolenaara7241f52008-06-24 20:39:31 +00001001 STRMOVE(cmdline_copy, next_cmdline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001002 next_cmdline = cmdline_copy;
1003 }
1004
1005
1006#ifdef FEAT_EVAL
1007 /* reset did_emsg for a function that is not aborted by an error */
1008 if (did_emsg && !force_abort
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001009 && getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001010 && !func_has_abort(real_cookie))
1011 did_emsg = FALSE;
1012
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001013 if (cstack.cs_looplevel > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001014 {
1015 ++current_line;
1016
1017 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001018 * An ":endwhile", ":endfor" and ":continue" is handled here.
1019 * If we were executing commands, jump back to the ":while" or
1020 * ":for".
1021 * If we were not executing commands, decrement cs_looplevel.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001022 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001023 if (cstack.cs_lflags & (CSL_HAD_CONT | CSL_HAD_ENDLOOP))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001024 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001025 cstack.cs_lflags &= ~(CSL_HAD_CONT | CSL_HAD_ENDLOOP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001026
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001027 /* Jump back to the matching ":while" or ":for". Be careful
1028 * not to use a cs_line[] from an entry that isn't a ":while"
1029 * or ":for": It would make "current_line" invalid and can
1030 * cause a crash. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001031 if (!did_emsg && !got_int && !did_throw
1032 && cstack.cs_idx >= 0
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001033 && (cstack.cs_flags[cstack.cs_idx]
1034 & (CSF_WHILE | CSF_FOR))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001035 && cstack.cs_line[cstack.cs_idx] >= 0
1036 && (cstack.cs_flags[cstack.cs_idx] & CSF_ACTIVE))
1037 {
1038 current_line = cstack.cs_line[cstack.cs_idx];
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001039 /* remember we jumped there */
1040 cstack.cs_lflags |= CSL_HAD_LOOP;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001041 line_breakcheck(); /* check if CTRL-C typed */
1042
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001043 /* Check for the next breakpoint at or after the ":while"
1044 * or ":for". */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001045 if (breakpoint != NULL)
1046 {
1047 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001048 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001049 fname,
1050 ((wcmd_T *)lines_ga.ga_data)[current_line].lnum-1);
1051 *dbg_tick = debug_tick;
1052 }
1053 }
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001054 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001055 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001056 /* can only get here with ":endwhile" or ":endfor" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001057 if (cstack.cs_idx >= 0)
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001058 rewind_conditionals(&cstack, cstack.cs_idx - 1,
1059 CSF_WHILE | CSF_FOR, &cstack.cs_looplevel);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001060 }
1061 }
1062
1063 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001064 * For a ":while" or ":for" we need to remember the line number.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001065 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001066 else if (cstack.cs_lflags & CSL_HAD_LOOP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001067 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001068 cstack.cs_lflags &= ~CSL_HAD_LOOP;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001069 cstack.cs_line[cstack.cs_idx] = current_line - 1;
1070 }
1071 }
1072
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01001073 /* Check for the next breakpoint after a watchexpression */
1074 if (breakpoint != NULL && has_watchexpr())
1075 {
1076 *breakpoint = dbg_find_breakpoint(FALSE, fname, sourcing_lnum);
1077 *dbg_tick = debug_tick;
1078 }
1079
Bram Moolenaar071d4272004-06-13 20:20:40 +00001080 /*
1081 * When not inside any ":while" loop, clear remembered lines.
1082 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001083 if (cstack.cs_looplevel == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001084 {
1085 if (lines_ga.ga_len > 0)
1086 {
1087 sourcing_lnum =
1088 ((wcmd_T *)lines_ga.ga_data)[lines_ga.ga_len - 1].lnum;
1089 free_cmdlines(&lines_ga);
1090 }
1091 current_line = 0;
1092 }
1093
1094 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001095 * A ":finally" makes did_emsg, got_int, and did_throw pending for
1096 * being restored at the ":endtry". Reset them here and set the
1097 * ACTIVE and FINALLY flags, so that the finally clause gets executed.
1098 * This includes the case where a missing ":endif", ":endwhile" or
1099 * ":endfor" was detected by the ":finally" itself.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001100 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001101 if (cstack.cs_lflags & CSL_HAD_FINA)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001102 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001103 cstack.cs_lflags &= ~CSL_HAD_FINA;
1104 report_make_pending(cstack.cs_pending[cstack.cs_idx]
1105 & (CSTP_ERROR | CSTP_INTERRUPT | CSTP_THROW),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001106 did_throw ? (void *)current_exception : NULL);
1107 did_emsg = got_int = did_throw = FALSE;
1108 cstack.cs_flags[cstack.cs_idx] |= CSF_ACTIVE | CSF_FINALLY;
1109 }
1110
1111 /* Update global "trylevel" for recursive calls to do_cmdline() from
1112 * within this loop. */
1113 trylevel = initial_trylevel + cstack.cs_trylevel;
1114
1115 /*
Bram Moolenaarc1762cc2007-05-10 16:56:30 +00001116 * If the outermost try conditional (across function calls and sourced
Bram Moolenaar071d4272004-06-13 20:20:40 +00001117 * files) is aborted because of an error, an interrupt, or an uncaught
1118 * exception, cancel everything. If it is left normally, reset
1119 * force_abort to get the non-EH compatible abortion behavior for
1120 * the rest of the script.
1121 */
1122 if (trylevel == 0 && !did_emsg && !got_int && !did_throw)
1123 force_abort = FALSE;
1124
1125 /* Convert an interrupt to an exception if appropriate. */
1126 (void)do_intthrow(&cstack);
1127#endif /* FEAT_EVAL */
1128
1129 }
1130 /*
1131 * Continue executing command lines when:
1132 * - no CTRL-C typed, no aborting error, no exception thrown or try
1133 * conditionals need to be checked for executing finally clauses or
1134 * catching an interrupt exception
1135 * - didn't get an error message or lines are not typed
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001136 * - there is a command after '|', inside a :if, :while, :for or :try, or
Bram Moolenaar071d4272004-06-13 20:20:40 +00001137 * looping for ":source" command or function call.
1138 */
1139 while (!((got_int
1140#ifdef FEAT_EVAL
1141 || (did_emsg && force_abort) || did_throw
1142#endif
1143 )
1144#ifdef FEAT_EVAL
1145 && cstack.cs_trylevel == 0
1146#endif
1147 )
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001148 && !(did_emsg
1149#ifdef FEAT_EVAL
1150 /* Keep going when inside try/catch, so that the error can be
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02001151 * deal with, except when it is a syntax error, it may cause
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001152 * the :endtry to be missed. */
1153 && (cstack.cs_trylevel == 0 || did_emsg_syntax)
1154#endif
1155 && used_getline
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001156 && (getline_equal(fgetline, cookie, getexmodeline)
1157 || getline_equal(fgetline, cookie, getexline)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001158 && (next_cmdline != NULL
1159#ifdef FEAT_EVAL
1160 || cstack.cs_idx >= 0
1161#endif
1162 || (flags & DOCMD_REPEAT)));
1163
1164 vim_free(cmdline_copy);
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001165 did_emsg_syntax = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001166#ifdef FEAT_EVAL
1167 free_cmdlines(&lines_ga);
1168 ga_clear(&lines_ga);
1169
1170 if (cstack.cs_idx >= 0)
1171 {
1172 /*
1173 * If a sourced file or executed function ran to its end, report the
1174 * unclosed conditional.
1175 */
1176 if (!got_int && !did_throw
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001177 && ((getline_equal(fgetline, cookie, getsourceline)
1178 && !source_finished(fgetline, cookie))
1179 || (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001180 && !func_has_ended(real_cookie))))
1181 {
1182 if (cstack.cs_flags[cstack.cs_idx] & CSF_TRY)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001183 emsg(_(e_endtry));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001184 else if (cstack.cs_flags[cstack.cs_idx] & CSF_WHILE)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001185 emsg(_(e_endwhile));
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001186 else if (cstack.cs_flags[cstack.cs_idx] & CSF_FOR)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001187 emsg(_(e_endfor));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001188 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001189 emsg(_(e_endif));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001190 }
1191
1192 /*
1193 * Reset "trylevel" in case of a ":finish" or ":return" or a missing
1194 * ":endtry" in a sourced file or executed function. If the try
1195 * conditional is in its finally clause, ignore anything pending.
1196 * If it is in a catch clause, finish the caught exception.
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001197 * Also cleanup any "cs_forinfo" structures.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001198 */
1199 do
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001200 {
1201 int idx = cleanup_conditionals(&cstack, 0, TRUE);
1202
Bram Moolenaar89e5d682005-01-17 22:06:23 +00001203 if (idx >= 0)
1204 --idx; /* remove try block not in its finally clause */
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001205 rewind_conditionals(&cstack, idx, CSF_WHILE | CSF_FOR,
1206 &cstack.cs_looplevel);
1207 }
1208 while (cstack.cs_idx >= 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001209 trylevel = initial_trylevel;
1210 }
1211
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001212 /* If a missing ":endtry", ":endwhile", ":endfor", or ":endif" or a memory
1213 * lack was reported above and the error message is to be converted to an
Bram Moolenaar071d4272004-06-13 20:20:40 +00001214 * exception, do this now after rewinding the cstack. */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001215 do_errthrow(&cstack, getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001216 ? (char_u *)"endfunction" : (char_u *)NULL);
1217
1218 if (trylevel == 0)
1219 {
1220 /*
1221 * When an exception is being thrown out of the outermost try
1222 * conditional, discard the uncaught exception, disable the conversion
1223 * of interrupts or errors to exceptions, and ensure that no more
1224 * commands are executed.
1225 */
1226 if (did_throw)
1227 {
1228 void *p = NULL;
1229 char_u *saved_sourcing_name;
1230 int saved_sourcing_lnum;
1231 struct msglist *messages = NULL, *next;
1232
1233 /*
1234 * If the uncaught exception is a user exception, report it as an
1235 * error. If it is an error exception, display the saved error
1236 * message now. For an interrupt exception, do nothing; the
1237 * interrupt message is given elsewhere.
1238 */
1239 switch (current_exception->type)
1240 {
1241 case ET_USER:
Bram Moolenaar9c13b352005-05-19 20:53:52 +00001242 vim_snprintf((char *)IObuff, IOSIZE,
1243 _("E605: Exception not caught: %s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001244 current_exception->value);
1245 p = vim_strsave(IObuff);
1246 break;
1247 case ET_ERROR:
1248 messages = current_exception->messages;
1249 current_exception->messages = NULL;
1250 break;
1251 case ET_INTERRUPT:
1252 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001253 }
1254
1255 saved_sourcing_name = sourcing_name;
1256 saved_sourcing_lnum = sourcing_lnum;
1257 sourcing_name = current_exception->throw_name;
1258 sourcing_lnum = current_exception->throw_lnum;
1259 current_exception->throw_name = NULL;
1260
1261 discard_current_exception(); /* uses IObuff if 'verbose' */
1262 suppress_errthrow = TRUE;
1263 force_abort = TRUE;
1264
1265 if (messages != NULL)
1266 {
1267 do
1268 {
1269 next = messages->next;
1270 emsg(messages->msg);
1271 vim_free(messages->msg);
1272 vim_free(messages);
1273 messages = next;
1274 }
1275 while (messages != NULL);
1276 }
1277 else if (p != NULL)
1278 {
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01001279 emsg(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001280 vim_free(p);
1281 }
1282 vim_free(sourcing_name);
1283 sourcing_name = saved_sourcing_name;
1284 sourcing_lnum = saved_sourcing_lnum;
1285 }
1286
1287 /*
1288 * On an interrupt or an aborting error not converted to an exception,
1289 * disable the conversion of errors to exceptions. (Interrupts are not
1290 * converted any more, here.) This enables also the interrupt message
1291 * when force_abort is set and did_emsg unset in case of an interrupt
1292 * from a finally clause after an error.
1293 */
1294 else if (got_int || (did_emsg && force_abort))
1295 suppress_errthrow = TRUE;
1296 }
1297
1298 /*
1299 * The current cstack will be freed when do_cmdline() returns. An uncaught
1300 * exception will have to be rethrown in the previous cstack. If a function
1301 * has just returned or a script file was just finished and the previous
1302 * cstack belongs to the same function or, respectively, script file, it
1303 * will have to be checked for finally clauses to be executed due to the
1304 * ":return" or ":finish". This is done in do_one_cmd().
1305 */
1306 if (did_throw)
1307 need_rethrow = TRUE;
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001308 if ((getline_equal(fgetline, cookie, getsourceline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001309 && ex_nesting_level > source_level(real_cookie))
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001310 || (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001311 && ex_nesting_level > func_level(real_cookie) + 1))
1312 {
1313 if (!did_throw)
1314 check_cstack = TRUE;
1315 }
1316 else
1317 {
1318 /* When leaving a function, reduce nesting level. */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001319 if (getline_equal(fgetline, cookie, get_func_line))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001320 --ex_nesting_level;
1321 /*
1322 * Go to debug mode when returning from a function in which we are
1323 * single-stepping.
1324 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001325 if ((getline_equal(fgetline, cookie, getsourceline)
1326 || getline_equal(fgetline, cookie, get_func_line))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001327 && ex_nesting_level + 1 <= debug_break_level)
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001328 do_debug(getline_equal(fgetline, cookie, getsourceline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001329 ? (char_u *)_("End of sourced file")
1330 : (char_u *)_("End of function"));
1331 }
1332
1333 /*
1334 * Restore the exception environment (done after returning from the
1335 * debugger).
1336 */
1337 if (flags & DOCMD_EXCRESET)
Bram Moolenaared203462004-06-16 11:19:22 +00001338 restore_dbg_stuff(&debug_saved);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001339
1340 msg_list = saved_msg_list;
1341#endif /* FEAT_EVAL */
1342
1343 /*
1344 * If there was too much output to fit on the command line, ask the user to
1345 * hit return before redrawing the screen. With the ":global" command we do
1346 * this only once after the command is finished.
1347 */
1348 if (did_inc)
1349 {
1350 --RedrawingDisabled;
1351 --no_wait_return;
1352 msg_scroll = FALSE;
1353
1354 /*
1355 * When just finished an ":if"-":else" which was typed, no need to
1356 * wait for hit-return. Also for an error situation.
1357 */
1358 if (retval == FAIL
1359#ifdef FEAT_EVAL
1360 || (did_endif && KeyTyped && !did_emsg)
1361#endif
1362 )
1363 {
1364 need_wait_return = FALSE;
1365 msg_didany = FALSE; /* don't wait when restarting edit */
1366 }
1367 else if (need_wait_return)
1368 {
1369 /*
1370 * The msg_start() above clears msg_didout. The wait_return we do
1371 * here should not overwrite the command that may be shown before
1372 * doing that.
1373 */
1374 msg_didout |= msg_didout_before_start;
1375 wait_return(FALSE);
1376 }
1377 }
1378
Bram Moolenaar2a942252012-11-28 23:03:07 +01001379#ifdef FEAT_EVAL
1380 did_endif = FALSE; /* in case do_cmdline used recursively */
1381#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001382 /*
1383 * Reset if_level, in case a sourced script file contains more ":if" than
1384 * ":endif" (could be ":if x | foo | endif").
1385 */
1386 if_level = 0;
Bram Moolenaar2e18a122012-11-28 19:10:54 +01001387#endif
Bram Moolenaard4ad0d42012-11-28 17:34:48 +01001388
Bram Moolenaar071d4272004-06-13 20:20:40 +00001389 --call_depth;
1390 return retval;
1391}
1392
1393#ifdef FEAT_EVAL
1394/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001395 * Obtain a line when inside a ":while" or ":for" loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001396 */
1397 static char_u *
Bram Moolenaare96a2492019-06-25 04:12:16 +02001398get_loop_line(int c, void *cookie, int indent, int do_concat)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001399{
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001400 struct loop_cookie *cp = (struct loop_cookie *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001401 wcmd_T *wp;
1402 char_u *line;
1403
1404 if (cp->current_line + 1 >= cp->lines_gap->ga_len)
1405 {
1406 if (cp->repeating)
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001407 return NULL; /* trying to read past ":endwhile"/":endfor" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001408
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001409 /* First time inside the ":while"/":for": get line normally. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001410 if (cp->getline == NULL)
Bram Moolenaare96a2492019-06-25 04:12:16 +02001411 line = getcmdline(c, 0L, indent, do_concat);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001412 else
Bram Moolenaare96a2492019-06-25 04:12:16 +02001413 line = cp->getline(c, cp->cookie, indent, do_concat);
Bram Moolenaard68071d2006-05-02 22:08:30 +00001414 if (line != NULL && store_loop_line(cp->lines_gap, line) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001415 ++cp->current_line;
1416
1417 return line;
1418 }
1419
1420 KeyTyped = FALSE;
1421 ++cp->current_line;
1422 wp = (wcmd_T *)(cp->lines_gap->ga_data) + cp->current_line;
1423 sourcing_lnum = wp->lnum;
1424 return vim_strsave(wp->line);
1425}
1426
1427/*
1428 * Store a line in "gap" so that a ":while" loop can execute it again.
1429 */
1430 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001431store_loop_line(garray_T *gap, char_u *line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001432{
1433 if (ga_grow(gap, 1) == FAIL)
1434 return FAIL;
1435 ((wcmd_T *)(gap->ga_data))[gap->ga_len].line = vim_strsave(line);
1436 ((wcmd_T *)(gap->ga_data))[gap->ga_len].lnum = sourcing_lnum;
1437 ++gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001438 return OK;
1439}
1440
1441/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001442 * Free the lines stored for a ":while" or ":for" loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001443 */
1444 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001445free_cmdlines(garray_T *gap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001446{
1447 while (gap->ga_len > 0)
1448 {
1449 vim_free(((wcmd_T *)(gap->ga_data))[gap->ga_len - 1].line);
1450 --gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001451 }
1452}
1453#endif
1454
1455/*
Bram Moolenaar89d40322006-08-29 15:30:07 +00001456 * If "fgetline" is get_loop_line(), return TRUE if the getline it uses equals
1457 * "func". * Otherwise return TRUE when "fgetline" equals "func".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001458 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001459 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001460getline_equal(
Bram Moolenaare96a2492019-06-25 04:12:16 +02001461 char_u *(*fgetline)(int, void *, int, int),
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001462 void *cookie UNUSED, /* argument for fgetline() */
Bram Moolenaare96a2492019-06-25 04:12:16 +02001463 char_u *(*func)(int, void *, int, int))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001464{
1465#ifdef FEAT_EVAL
Bram Moolenaare96a2492019-06-25 04:12:16 +02001466 char_u *(*gp)(int, void *, int, int);
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001467 struct loop_cookie *cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001468
Bram Moolenaar89d40322006-08-29 15:30:07 +00001469 /* When "fgetline" is "get_loop_line()" use the "cookie" to find the
Bram Moolenaarc1762cc2007-05-10 16:56:30 +00001470 * function that's originally used to obtain the lines. This may be
1471 * nested several levels. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00001472 gp = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001473 cp = (struct loop_cookie *)cookie;
1474 while (gp == get_loop_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001475 {
1476 gp = cp->getline;
1477 cp = cp->cookie;
1478 }
1479 return gp == func;
1480#else
Bram Moolenaar89d40322006-08-29 15:30:07 +00001481 return fgetline == func;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001482#endif
1483}
1484
Bram Moolenaar071d4272004-06-13 20:20:40 +00001485/*
Bram Moolenaar89d40322006-08-29 15:30:07 +00001486 * If "fgetline" is get_loop_line(), return the cookie used by the original
Bram Moolenaar071d4272004-06-13 20:20:40 +00001487 * getline function. Otherwise return "cookie".
1488 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001489 void *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001490getline_cookie(
Bram Moolenaare96a2492019-06-25 04:12:16 +02001491 char_u *(*fgetline)(int, void *, int, int) UNUSED,
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001492 void *cookie) /* argument for fgetline() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001493{
Bram Moolenaar13505972019-01-24 15:04:48 +01001494#ifdef FEAT_EVAL
Bram Moolenaare96a2492019-06-25 04:12:16 +02001495 char_u *(*gp)(int, void *, int, int);
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001496 struct loop_cookie *cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001497
Bram Moolenaar89d40322006-08-29 15:30:07 +00001498 /* When "fgetline" is "get_loop_line()" use the "cookie" to find the
Bram Moolenaarc1762cc2007-05-10 16:56:30 +00001499 * cookie that's originally used to obtain the lines. This may be nested
Bram Moolenaar071d4272004-06-13 20:20:40 +00001500 * several levels. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00001501 gp = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001502 cp = (struct loop_cookie *)cookie;
1503 while (gp == get_loop_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001504 {
1505 gp = cp->getline;
1506 cp = cp->cookie;
1507 }
1508 return cp;
Bram Moolenaar13505972019-01-24 15:04:48 +01001509#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001510 return cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001511#endif
Bram Moolenaar13505972019-01-24 15:04:48 +01001512}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001513
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001514
1515/*
1516 * Helper function to apply an offset for buffer commands, i.e. ":bdelete",
1517 * ":bwipeout", etc.
1518 * Returns the buffer number.
1519 */
1520 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001521compute_buffer_local_count(int addr_type, int lnum, int offset)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001522{
1523 buf_T *buf;
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001524 buf_T *nextbuf;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001525 int count = offset;
1526
1527 buf = firstbuf;
1528 while (buf->b_next != NULL && buf->b_fnum < lnum)
1529 buf = buf->b_next;
1530 while (count != 0)
1531 {
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001532 count += (offset < 0) ? 1 : -1;
1533 nextbuf = (offset < 0) ? buf->b_prev : buf->b_next;
1534 if (nextbuf == NULL)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001535 break;
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001536 buf = nextbuf;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001537 if (addr_type == ADDR_LOADED_BUFFERS)
1538 /* skip over unloaded buffers */
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001539 while (buf->b_ml.ml_mfp == NULL)
1540 {
1541 nextbuf = (offset < 0) ? buf->b_prev : buf->b_next;
1542 if (nextbuf == NULL)
1543 break;
1544 buf = nextbuf;
1545 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001546 }
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001547 /* we might have gone too far, last buffer is not loadedd */
1548 if (addr_type == ADDR_LOADED_BUFFERS)
1549 while (buf->b_ml.ml_mfp == NULL)
1550 {
1551 nextbuf = (offset >= 0) ? buf->b_prev : buf->b_next;
1552 if (nextbuf == NULL)
1553 break;
1554 buf = nextbuf;
1555 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001556 return buf->b_fnum;
1557}
1558
Bram Moolenaarb7316892019-05-01 18:08:42 +02001559/*
1560 * Return the window number of "win".
1561 * When "win" is NULL return the number of windows.
1562 */
Bram Moolenaarf240e182014-11-27 18:33:02 +01001563 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001564current_win_nr(win_T *win)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001565{
1566 win_T *wp;
1567 int nr = 0;
1568
Bram Moolenaar29323592016-07-24 22:04:11 +02001569 FOR_ALL_WINDOWS(wp)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001570 {
1571 ++nr;
1572 if (wp == win)
1573 break;
1574 }
1575 return nr;
1576}
1577
1578 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001579current_tab_nr(tabpage_T *tab)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001580{
1581 tabpage_T *tp;
1582 int nr = 0;
1583
Bram Moolenaar29323592016-07-24 22:04:11 +02001584 FOR_ALL_TABPAGES(tp)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001585 {
1586 ++nr;
1587 if (tp == tab)
1588 break;
1589 }
1590 return nr;
1591}
1592
1593# define CURRENT_WIN_NR current_win_nr(curwin)
1594# define LAST_WIN_NR current_win_nr(NULL)
1595# define CURRENT_TAB_NR current_tab_nr(curtab)
1596# define LAST_TAB_NR current_tab_nr(NULL)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001597
Bram Moolenaar071d4272004-06-13 20:20:40 +00001598/*
1599 * Execute one Ex command.
1600 *
1601 * If 'sourcing' is TRUE, the command will be included in the error message.
1602 *
1603 * 1. skip comment lines and leading space
1604 * 2. handle command modifiers
Bram Moolenaar1c40a662014-11-27 16:38:11 +01001605 * 3. find the command
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001606 * 4. parse range
Bram Moolenaar1c40a662014-11-27 16:38:11 +01001607 * 5. Parse the command.
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001608 * 6. parse arguments
1609 * 7. switch on command name
Bram Moolenaar071d4272004-06-13 20:20:40 +00001610 *
Bram Moolenaar89d40322006-08-29 15:30:07 +00001611 * Note: "fgetline" can be NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001612 *
1613 * This function may be called recursively!
1614 */
1615#if (_MSC_VER == 1200)
1616/*
Bram Moolenaared203462004-06-16 11:19:22 +00001617 * Avoid optimisation bug in VC++ version 6.0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001618 */
Bram Moolenaar281bdce2005-01-25 21:53:18 +00001619 #pragma optimize( "g", off )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001620#endif
1621 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001622do_one_cmd(
1623 char_u **cmdlinep,
1624 int sourcing,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001625#ifdef FEAT_EVAL
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001626 struct condstack *cstack,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001627#endif
Bram Moolenaare96a2492019-06-25 04:12:16 +02001628 char_u *(*fgetline)(int, void *, int, int),
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001629 void *cookie) /* argument for fgetline() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001630{
1631 char_u *p;
1632 linenr_T lnum;
1633 long n;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001634 char *errormsg = NULL; /* error message */
Bram Moolenaar7b668e82016-08-23 23:51:21 +02001635 char_u *after_modifier = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001636 exarg_T ea; /* Ex command arguments */
Bram Moolenaar8e258a42009-07-09 13:55:43 +00001637 int save_msg_scroll = msg_scroll;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001638 cmdmod_T save_cmdmod;
Bram Moolenaar9a2c0912019-03-30 14:26:18 +01001639 int save_reg_executing = reg_executing;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001640 int ni; /* set when Not Implemented */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001641 char_u *cmd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001642
1643 vim_memset(&ea, 0, sizeof(ea));
1644 ea.line1 = 1;
1645 ea.line2 = 1;
1646#ifdef FEAT_EVAL
1647 ++ex_nesting_level;
1648#endif
1649
Bram Moolenaar21691f82012-12-05 19:13:18 +01001650 /* When the last file has not been edited :q has to be typed twice. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001651 if (quitmore
1652#ifdef FEAT_EVAL
1653 /* avoid that a function call in 'statusline' does this */
Bram Moolenaar89d40322006-08-29 15:30:07 +00001654 && !getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001655#endif
Bram Moolenaar21691f82012-12-05 19:13:18 +01001656 /* avoid that an autocommand, e.g. QuitPre, does this */
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001657 && !getline_equal(fgetline, cookie, getnextac))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001658 --quitmore;
1659
1660 /*
1661 * Reset browse, confirm, etc.. They are restored when returning, for
1662 * recursive calls.
1663 */
1664 save_cmdmod = cmdmod;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001665
Bram Moolenaarcbb37ad2006-08-16 15:04:21 +00001666 /* "#!anything" is handled like a comment. */
1667 if ((*cmdlinep)[0] == '#' && (*cmdlinep)[1] == '!')
1668 goto doend;
1669
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02001670/*
1671 * 1. Skip comment lines and leading white space and colons.
1672 * 2. Handle command modifiers.
1673 */
1674 // The "ea" structure holds the arguments that can be used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001675 ea.cmd = *cmdlinep;
Bram Moolenaareffed932018-08-14 13:38:17 +02001676 ea.cmdlinep = cmdlinep;
1677 ea.getline = fgetline;
1678 ea.cookie = cookie;
1679#ifdef FEAT_EVAL
1680 ea.cstack = cstack;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001681#endif
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02001682 if (parse_command_modifiers(&ea, &errormsg, FALSE) == FAIL)
Bram Moolenaareffed932018-08-14 13:38:17 +02001683 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001684
Bram Moolenaar7b668e82016-08-23 23:51:21 +02001685 after_modifier = ea.cmd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001686
1687#ifdef FEAT_EVAL
1688 ea.skip = did_emsg || got_int || did_throw || (cstack->cs_idx >= 0
1689 && !(cstack->cs_flags[cstack->cs_idx] & CSF_ACTIVE));
1690#else
1691 ea.skip = (if_level > 0);
1692#endif
1693
Bram Moolenaar071d4272004-06-13 20:20:40 +00001694/*
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001695 * 3. Skip over the range to find the command. Let "p" point to after it.
1696 *
1697 * We need the command to know what kind of range it uses.
1698 */
1699 cmd = ea.cmd;
1700 ea.cmd = skip_range(ea.cmd, NULL);
1701 if (*ea.cmd == '*' && vim_strchr(p_cpo, CPO_STAR) == NULL)
1702 ea.cmd = skipwhite(ea.cmd + 1);
1703 p = find_command(&ea, NULL);
1704
Bram Moolenaar7feb35e2018-08-21 17:49:54 +02001705#ifdef FEAT_EVAL
1706# ifdef FEAT_PROFILE
1707 // Count this line for profiling if skip is TRUE.
1708 if (do_profiling == PROF_YES
1709 && (!ea.skip || cstack->cs_idx == 0 || (cstack->cs_idx > 0
1710 && (cstack->cs_flags[cstack->cs_idx - 1] & CSF_ACTIVE))))
1711 {
1712 int skip = did_emsg || got_int || did_throw;
1713
1714 if (ea.cmdidx == CMD_catch)
1715 skip = !skip && !(cstack->cs_idx >= 0
1716 && (cstack->cs_flags[cstack->cs_idx] & CSF_THROWN)
1717 && !(cstack->cs_flags[cstack->cs_idx] & CSF_CAUGHT));
1718 else if (ea.cmdidx == CMD_else || ea.cmdidx == CMD_elseif)
1719 skip = skip || !(cstack->cs_idx >= 0
1720 && !(cstack->cs_flags[cstack->cs_idx]
1721 & (CSF_ACTIVE | CSF_TRUE)));
1722 else if (ea.cmdidx == CMD_finally)
1723 skip = FALSE;
1724 else if (ea.cmdidx != CMD_endif
1725 && ea.cmdidx != CMD_endfor
1726 && ea.cmdidx != CMD_endtry
1727 && ea.cmdidx != CMD_endwhile)
1728 skip = ea.skip;
1729
1730 if (!skip)
1731 {
1732 if (getline_equal(fgetline, cookie, get_func_line))
1733 func_line_exec(getline_cookie(fgetline, cookie));
1734 else if (getline_equal(fgetline, cookie, getsourceline))
1735 script_line_exec();
1736 }
1737 }
1738# endif
1739
1740 /* May go to debug mode. If this happens and the ">quit" debug command is
1741 * used, throw an interrupt exception and skip the next command. */
1742 dbg_check_breakpoint(&ea);
1743 if (!ea.skip && got_int)
1744 {
1745 ea.skip = TRUE;
1746 (void)do_intthrow(cstack);
1747 }
1748#endif
1749
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001750/*
1751 * 4. parse a range specifier of the form: addr [,addr] [;addr] ..
Bram Moolenaar071d4272004-06-13 20:20:40 +00001752 *
1753 * where 'addr' is:
1754 *
1755 * % (entire file)
1756 * $ [+-NUM]
1757 * 'x [+-NUM] (where x denotes a currently defined mark)
1758 * . [+-NUM]
1759 * [+-NUM]..
1760 * NUM
1761 *
1762 * The ea.cmd pointer is updated to point to the first character following the
1763 * range spec. If an initial address is found, but no second, the upper bound
1764 * is equal to the lower.
1765 */
1766
Bram Moolenaar25190db2019-05-04 15:05:28 +02001767 // ea.addr_type for user commands is set by find_ucmd
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01001768 if (!IS_USER_CMDIDX(ea.cmdidx))
1769 {
1770 if (ea.cmdidx != CMD_SIZE)
1771 ea.addr_type = cmdnames[(int)ea.cmdidx].cmd_addr_type;
1772 else
1773 ea.addr_type = ADDR_LINES;
1774
Bram Moolenaar25190db2019-05-04 15:05:28 +02001775 // :wincmd range depends on the argument.
Bram Moolenaar164f3262015-01-14 21:22:01 +01001776 if (ea.cmdidx == CMD_wincmd && p != NULL)
1777 get_wincmd_addr_type(skipwhite(p), &ea);
Bram Moolenaar25190db2019-05-04 15:05:28 +02001778#ifdef FEAT_QUICKFIX
1779 // :.cc in quickfix window uses line number
1780 if ((ea.cmdidx == CMD_cc || ea.cmdidx == CMD_ll) && bt_quickfix(curbuf))
1781 ea.addr_type = ADDR_OTHER;
1782#endif
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01001783 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001784
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01001785 ea.cmd = cmd;
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02001786 if (parse_cmd_address(&ea, &errormsg, FALSE) == FAIL)
Bram Moolenaaree8415b2018-08-10 23:13:12 +02001787 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001788
Bram Moolenaar071d4272004-06-13 20:20:40 +00001789/*
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001790 * 5. Parse the command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001791 */
1792
1793 /*
1794 * Skip ':' and any white space
1795 */
1796 ea.cmd = skipwhite(ea.cmd);
1797 while (*ea.cmd == ':')
1798 ea.cmd = skipwhite(ea.cmd + 1);
1799
1800 /*
1801 * If we got a line, but no command, then go to the line.
1802 * If we find a '|' or '\n' we set ea.nextcmd.
1803 */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001804 if (*ea.cmd == NUL || *ea.cmd == '"'
1805 || (ea.nextcmd = check_nextcmd(ea.cmd)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001806 {
1807 /*
1808 * strange vi behaviour:
1809 * ":3" jumps to line 3
1810 * ":3|..." prints line 3
1811 * ":|" prints current line
1812 */
1813 if (ea.skip) /* skip this if inside :if */
1814 goto doend;
Bram Moolenaardf177f62005-02-22 08:39:57 +00001815 if (*ea.cmd == '|' || (exmode_active && ea.line1 != ea.line2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001816 {
1817 ea.cmdidx = CMD_print;
Bram Moolenaar8071cb22019-07-12 17:58:01 +02001818 ea.argt = EX_RANGE+EX_COUNT+EX_TRLBAR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001819 if ((errormsg = invalid_range(&ea)) == NULL)
1820 {
1821 correct_range(&ea);
1822 ex_print(&ea);
1823 }
1824 }
1825 else if (ea.addr_count != 0)
1826 {
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00001827 if (ea.line2 > curbuf->b_ml.ml_line_count)
1828 {
1829 /* With '-' in 'cpoptions' a line number past the file is an
1830 * error, otherwise put it at the end of the file. */
1831 if (vim_strchr(p_cpo, CPO_MINUS) != NULL)
1832 ea.line2 = -1;
1833 else
1834 ea.line2 = curbuf->b_ml.ml_line_count;
1835 }
1836
1837 if (ea.line2 < 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001838 errormsg = _(e_invrange);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001839 else
1840 {
1841 if (ea.line2 == 0)
1842 curwin->w_cursor.lnum = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001843 else
1844 curwin->w_cursor.lnum = ea.line2;
1845 beginline(BL_SOL | BL_FIX);
1846 }
1847 }
1848 goto doend;
1849 }
1850
Bram Moolenaard5005162014-08-22 23:05:54 +02001851 /* If this looks like an undefined user command and there are CmdUndefined
1852 * autocommands defined, trigger the matching autocommands. */
1853 if (p != NULL && ea.cmdidx == CMD_SIZE && !ea.skip
1854 && ASCII_ISUPPER(*ea.cmd)
1855 && has_cmdundefined())
1856 {
Bram Moolenaard5005162014-08-22 23:05:54 +02001857 int ret;
1858
Bram Moolenaar5a31b462014-08-23 14:16:20 +02001859 p = ea.cmd;
Bram Moolenaard5005162014-08-22 23:05:54 +02001860 while (ASCII_ISALNUM(*p))
1861 ++p;
Bram Moolenaar120f4a82014-09-09 12:22:06 +02001862 p = vim_strnsave(ea.cmd, (int)(p - ea.cmd));
Bram Moolenaard5005162014-08-22 23:05:54 +02001863 ret = apply_autocmds(EVENT_CMDUNDEFINED, p, p, TRUE, NULL);
1864 vim_free(p);
Bram Moolenaar829aef12015-07-28 14:25:48 +02001865 /* If the autocommands did something and didn't cause an error, try
1866 * finding the command again. */
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001867 p = (ret
1868#ifdef FEAT_EVAL
1869 && !aborting()
Bram Moolenaard5005162014-08-22 23:05:54 +02001870#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001871 ) ? find_command(&ea, NULL) : ea.cmd;
1872 }
Bram Moolenaard5005162014-08-22 23:05:54 +02001873
Bram Moolenaar071d4272004-06-13 20:20:40 +00001874 if (p == NULL)
1875 {
1876 if (!ea.skip)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001877 errormsg = _("E464: Ambiguous use of user-defined command");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001878 goto doend;
1879 }
Bram Moolenaarac9fb182019-04-27 13:04:13 +02001880 // Check for wrong commands.
Bram Moolenaar6f9a4762017-06-22 20:39:17 +02001881 if (*p == '!' && ea.cmd[1] == 0151 && ea.cmd[0] == 78
1882 && !IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001883 {
1884 errormsg = uc_fun_cmd();
1885 goto doend;
1886 }
Bram Moolenaarac9fb182019-04-27 13:04:13 +02001887
Bram Moolenaar071d4272004-06-13 20:20:40 +00001888 if (ea.cmdidx == CMD_SIZE)
1889 {
1890 if (!ea.skip)
1891 {
1892 STRCPY(IObuff, _("E492: Not an editor command"));
1893 if (!sourcing)
Bram Moolenaar7b668e82016-08-23 23:51:21 +02001894 {
1895 /* If the modifier was parsed OK the error must be in the
1896 * following command */
1897 if (after_modifier != NULL)
1898 append_command(after_modifier);
1899 else
1900 append_command(*cmdlinep);
1901 }
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001902 errormsg = (char *)IObuff;
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001903 did_emsg_syntax = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001904 }
1905 goto doend;
1906 }
1907
Bram Moolenaar958636c2014-10-21 20:01:58 +02001908 ni = (!IS_USER_CMDIDX(ea.cmdidx)
1909 && (cmdnames[ea.cmdidx].cmd_func == ex_ni
Bram Moolenaar7bb75552007-07-16 18:39:49 +00001910#ifdef HAVE_EX_SCRIPT_NI
1911 || cmdnames[ea.cmdidx].cmd_func == ex_script_ni
1912#endif
1913 ));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001914
1915#ifndef FEAT_EVAL
1916 /*
1917 * When the expression evaluation is disabled, recognize the ":if" and
1918 * ":endif" commands and ignore everything in between it.
1919 */
1920 if (ea.cmdidx == CMD_if)
1921 ++if_level;
1922 if (if_level)
1923 {
1924 if (ea.cmdidx == CMD_endif)
1925 --if_level;
1926 goto doend;
1927 }
1928
1929#endif
1930
Bram Moolenaar196b3b02008-06-20 16:51:41 +00001931 /* forced commands */
1932 if (*p == '!' && ea.cmdidx != CMD_substitute
1933 && ea.cmdidx != CMD_smagic && ea.cmdidx != CMD_snomagic)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001934 {
1935 ++p;
1936 ea.forceit = TRUE;
1937 }
1938 else
1939 ea.forceit = FALSE;
1940
1941/*
Bram Moolenaarb7316892019-05-01 18:08:42 +02001942 * 6. Parse arguments. Then check for errors.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001943 */
Bram Moolenaar958636c2014-10-21 20:01:58 +02001944 if (!IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001945 ea.argt = (long)cmdnames[(int)ea.cmdidx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001946
1947 if (!ea.skip)
1948 {
1949#ifdef HAVE_SANDBOX
Bram Moolenaar8071cb22019-07-12 17:58:01 +02001950 if (sandbox != 0 && !(ea.argt & EX_SBOXOK))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001951 {
Bram Moolenaar8c62a082019-02-08 14:34:10 +01001952 // Command not allowed in sandbox.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001953 errormsg = _(e_sandbox);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001954 goto doend;
1955 }
1956#endif
Bram Moolenaar8071cb22019-07-12 17:58:01 +02001957 if (restricted != 0 && (ea.argt & EX_RESTRICT))
Bram Moolenaar8c62a082019-02-08 14:34:10 +01001958 {
1959 errormsg = _("E981: Command not allowed in rvim");
1960 goto doend;
1961 }
Bram Moolenaar8071cb22019-07-12 17:58:01 +02001962 if (!curbuf->b_p_ma && (ea.argt & EX_MODIFY))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001963 {
1964 /* Command not allowed in non-'modifiable' buffer */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001965 errormsg = _(e_modifiable);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001966 goto doend;
1967 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00001968
Bram Moolenaar8071cb22019-07-12 17:58:01 +02001969 if (text_locked() && !(ea.argt & EX_CMDWIN)
Bram Moolenaar958636c2014-10-21 20:01:58 +02001970 && !IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001971 {
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00001972 /* Command not allowed when editing the command line. */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001973 errormsg = _(get_text_locked_msg());
Bram Moolenaar071d4272004-06-13 20:20:40 +00001974 goto doend;
1975 }
Bram Moolenaar379fb762018-08-30 15:58:28 +02001976
Bram Moolenaar5555acc2006-04-07 21:33:12 +00001977 /* Disallow editing another buffer when "curbuf_lock" is set.
Bram Moolenaar379fb762018-08-30 15:58:28 +02001978 * Do allow ":checktime" (it is postponed).
1979 * Do allow ":edit" (check for an argument later).
1980 * Do allow ":file" with no arguments (check for an argument later). */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02001981 if (!(ea.argt & EX_CMDWIN)
Bram Moolenaar5555acc2006-04-07 21:33:12 +00001982 && ea.cmdidx != CMD_checktime
Bram Moolenaar379fb762018-08-30 15:58:28 +02001983 && ea.cmdidx != CMD_edit
1984 && ea.cmdidx != CMD_file
Bram Moolenaar958636c2014-10-21 20:01:58 +02001985 && !IS_USER_CMDIDX(ea.cmdidx)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001986 && curbuf_locked())
1987 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001988
Bram Moolenaar8071cb22019-07-12 17:58:01 +02001989 if (!ni && !(ea.argt & EX_RANGE) && ea.addr_count > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001990 {
1991 /* no range allowed */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001992 errormsg = _(e_norange);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001993 goto doend;
1994 }
1995 }
1996
Bram Moolenaar8071cb22019-07-12 17:58:01 +02001997 if (!ni && !(ea.argt & EX_BANG) && ea.forceit) // no <!> allowed
Bram Moolenaar071d4272004-06-13 20:20:40 +00001998 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001999 errormsg = _(e_nobang);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002000 goto doend;
2001 }
2002
2003 /*
2004 * Don't complain about the range if it is not used
2005 * (could happen if line_count is accidentally set to 0).
2006 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002007 if (!ea.skip && !ni && (ea.argt & EX_RANGE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002008 {
2009 /*
2010 * If the range is backwards, ask for confirmation and, if given, swap
2011 * ea.line1 & ea.line2 so it's forwards again.
2012 * When global command is busy, don't ask, will fail below.
2013 */
2014 if (!global_busy && ea.line1 > ea.line2)
2015 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002016 if (msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002017 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002018 if (sourcing || exmode_active)
2019 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002020 errormsg = _("E493: Backwards range given");
Bram Moolenaara5792f52005-11-23 21:25:05 +00002021 goto doend;
2022 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002023 if (ask_yesno((char_u *)
2024 _("Backwards range given, OK to swap"), FALSE) != 'y')
Bram Moolenaara5792f52005-11-23 21:25:05 +00002025 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002026 }
2027 lnum = ea.line1;
2028 ea.line1 = ea.line2;
2029 ea.line2 = lnum;
2030 }
2031 if ((errormsg = invalid_range(&ea)) != NULL)
2032 goto doend;
2033 }
2034
Bram Moolenaarb7316892019-05-01 18:08:42 +02002035 if ((ea.addr_type == ADDR_OTHER) && ea.addr_count == 0)
2036 // default is 1, not cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00002037 ea.line2 = 1;
2038
2039 correct_range(&ea);
2040
2041#ifdef FEAT_FOLDING
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002042 if (((ea.argt & EX_WHOLEFOLD) || ea.addr_count >= 2) && !global_busy
Bram Moolenaara3306952016-01-02 21:41:06 +01002043 && ea.addr_type == ADDR_LINES)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002044 {
2045 /* Put the first line at the start of a closed fold, put the last line
2046 * at the end of a closed fold. */
2047 (void)hasFolding(ea.line1, &ea.line1, NULL);
2048 (void)hasFolding(ea.line2, NULL, &ea.line2);
2049 }
2050#endif
2051
2052#ifdef FEAT_QUICKFIX
2053 /*
Bram Moolenaar86b68352004-12-27 21:59:20 +00002054 * For the ":make" and ":grep" commands we insert the 'makeprg'/'grepprg'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002055 * option here, so things like % get expanded.
2056 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002057 p = replace_makeprg(&ea, p, cmdlinep);
2058 if (p == NULL)
2059 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002060#endif
2061
2062 /*
2063 * Skip to start of argument.
2064 * Don't do this for the ":!" command, because ":!! -l" needs the space.
2065 */
2066 if (ea.cmdidx == CMD_bang)
2067 ea.arg = p;
2068 else
2069 ea.arg = skipwhite(p);
2070
Bram Moolenaar379fb762018-08-30 15:58:28 +02002071 // ":file" cannot be run with an argument when "curbuf_lock" is set
2072 if (ea.cmdidx == CMD_file && *ea.arg != NUL && curbuf_locked())
2073 goto doend;
2074
Bram Moolenaar071d4272004-06-13 20:20:40 +00002075 /*
2076 * Check for "++opt=val" argument.
2077 * Must be first, allow ":w ++enc=utf8 !cmd"
2078 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002079 if (ea.argt & EX_ARGOPT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002080 while (ea.arg[0] == '+' && ea.arg[1] == '+')
2081 if (getargopt(&ea) == FAIL && !ni)
2082 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002083 errormsg = _(e_invarg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002084 goto doend;
2085 }
2086
2087 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
2088 {
2089 if (*ea.arg == '>') /* append */
2090 {
2091 if (*++ea.arg != '>') /* typed wrong */
2092 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002093 errormsg = _("E494: Use w or w>>");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002094 goto doend;
2095 }
2096 ea.arg = skipwhite(ea.arg + 1);
2097 ea.append = TRUE;
2098 }
2099 else if (*ea.arg == '!' && ea.cmdidx == CMD_write) /* :w !filter */
2100 {
2101 ++ea.arg;
2102 ea.usefilter = TRUE;
2103 }
2104 }
2105
2106 if (ea.cmdidx == CMD_read)
2107 {
2108 if (ea.forceit)
2109 {
2110 ea.usefilter = TRUE; /* :r! filter if ea.forceit */
2111 ea.forceit = FALSE;
2112 }
2113 else if (*ea.arg == '!') /* :r !filter */
2114 {
2115 ++ea.arg;
2116 ea.usefilter = TRUE;
2117 }
2118 }
2119
2120 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
2121 {
2122 ea.amount = 1;
2123 while (*ea.arg == *ea.cmd) /* count number of '>' or '<' */
2124 {
2125 ++ea.arg;
2126 ++ea.amount;
2127 }
2128 ea.arg = skipwhite(ea.arg);
2129 }
2130
2131 /*
2132 * Check for "+command" argument, before checking for next command.
2133 * Don't do this for ":read !cmd" and ":write !cmd".
2134 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002135 if ((ea.argt & EX_CMDARG) && !ea.usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002136 ea.do_ecmd_cmd = getargcmd(&ea.arg);
2137
2138 /*
2139 * Check for '|' to separate commands and '"' to start comments.
2140 * Don't do this for ":read !cmd" and ":write !cmd".
2141 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002142 if ((ea.argt & EX_TRLBAR) && !ea.usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002143 separate_nextcmd(&ea);
2144
2145 /*
2146 * Check for <newline> to end a shell command.
Bram Moolenaardf177f62005-02-22 08:39:57 +00002147 * Also do this for ":read !cmd", ":write !cmd" and ":global".
2148 * Any others?
Bram Moolenaar071d4272004-06-13 20:20:40 +00002149 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00002150 else if (ea.cmdidx == CMD_bang
Bram Moolenaar67883b42017-07-27 22:57:00 +02002151 || ea.cmdidx == CMD_terminal
Bram Moolenaardf177f62005-02-22 08:39:57 +00002152 || ea.cmdidx == CMD_global
2153 || ea.cmdidx == CMD_vglobal
2154 || ea.usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002155 {
2156 for (p = ea.arg; *p; ++p)
2157 {
2158 /* Remove one backslash before a newline, so that it's possible to
2159 * pass a newline to the shell and also a newline that is preceded
2160 * with a backslash. This makes it impossible to end a shell
2161 * command in a backslash, but that doesn't appear useful.
2162 * Halving the number of backslashes is incompatible with previous
2163 * versions. */
2164 if (*p == '\\' && p[1] == '\n')
Bram Moolenaara7241f52008-06-24 20:39:31 +00002165 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002166 else if (*p == '\n')
2167 {
2168 ea.nextcmd = p + 1;
2169 *p = NUL;
2170 break;
2171 }
2172 }
2173 }
2174
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002175 if ((ea.argt & EX_DFLALL) && ea.addr_count == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002176 {
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002177 buf_T *buf;
2178
Bram Moolenaar071d4272004-06-13 20:20:40 +00002179 ea.line1 = 1;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002180 switch (ea.addr_type)
2181 {
2182 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02002183 case ADDR_OTHER:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002184 ea.line2 = curbuf->b_ml.ml_line_count;
2185 break;
2186 case ADDR_LOADED_BUFFERS:
2187 buf = firstbuf;
2188 while (buf->b_next != NULL && buf->b_ml.ml_mfp == NULL)
2189 buf = buf->b_next;
2190 ea.line1 = buf->b_fnum;
2191 buf = lastbuf;
2192 while (buf->b_prev != NULL && buf->b_ml.ml_mfp == NULL)
2193 buf = buf->b_prev;
2194 ea.line2 = buf->b_fnum;
2195 break;
2196 case ADDR_BUFFERS:
2197 ea.line1 = firstbuf->b_fnum;
2198 ea.line2 = lastbuf->b_fnum;
2199 break;
2200 case ADDR_WINDOWS:
2201 ea.line2 = LAST_WIN_NR;
2202 break;
2203 case ADDR_TABS:
2204 ea.line2 = LAST_TAB_NR;
2205 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01002206 case ADDR_TABS_RELATIVE:
2207 ea.line2 = 1;
2208 break;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002209 case ADDR_ARGUMENTS:
2210 if (ARGCOUNT == 0)
2211 ea.line1 = ea.line2 = 0;
2212 else
2213 ea.line2 = ARGCOUNT;
2214 break;
Bram Moolenaar25190db2019-05-04 15:05:28 +02002215 case ADDR_QUICKFIX_VALID:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02002216#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02002217 ea.line2 = qf_get_valid_size(&ea);
Bram Moolenaaraa23b372015-09-08 18:46:31 +02002218 if (ea.line2 == 0)
2219 ea.line2 = 1;
Bram Moolenaare906c502015-09-09 21:10:39 +02002220#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02002221 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02002222 case ADDR_NONE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02002223 case ADDR_UNSIGNED:
2224 case ADDR_QUICKFIX:
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002225 iemsg(_("INTERNAL: Cannot use EX_DFLALL with ADDR_NONE, ADDR_UNSIGNED or ADDR_QUICKFIX"));
Bram Moolenaarb7316892019-05-01 18:08:42 +02002226 break;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002227 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002228 }
2229
2230 /* accept numbered register only when no count allowed (:put) */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002231 if ( (ea.argt & EX_REGSTR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002232 && *ea.arg != NUL
Bram Moolenaar958636c2014-10-21 20:01:58 +02002233 /* Do not allow register = for user commands */
2234 && (!IS_USER_CMDIDX(ea.cmdidx) || *ea.arg != '=')
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002235 && !((ea.argt & EX_COUNT) && VIM_ISDIGIT(*ea.arg)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002236 {
Bram Moolenaar85de2062011-05-05 14:26:41 +02002237#ifndef FEAT_CLIPBOARD
2238 /* check these explicitly for a more specific error message */
2239 if (*ea.arg == '*' || *ea.arg == '+')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002240 {
Bram Moolenaar99b12722019-01-14 20:16:40 +01002241 errormsg = _(e_invalidreg);
Bram Moolenaar85de2062011-05-05 14:26:41 +02002242 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002243 }
2244#endif
Bram Moolenaar958636c2014-10-21 20:01:58 +02002245 if (valid_yank_reg(*ea.arg, (ea.cmdidx != CMD_put
2246 && !IS_USER_CMDIDX(ea.cmdidx))))
Bram Moolenaar85de2062011-05-05 14:26:41 +02002247 {
2248 ea.regname = *ea.arg++;
2249#ifdef FEAT_EVAL
2250 /* for '=' register: accept the rest of the line as an expression */
2251 if (ea.arg[-1] == '=' && ea.arg[0] != NUL)
2252 {
2253 set_expr_line(vim_strsave(ea.arg));
2254 ea.arg += STRLEN(ea.arg);
2255 }
2256#endif
2257 ea.arg = skipwhite(ea.arg);
2258 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002259 }
2260
2261 /*
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002262 * Check for a count. When accepting a EX_BUFNAME, don't use "123foo" as a
Bram Moolenaar071d4272004-06-13 20:20:40 +00002263 * count, it's a buffer name.
2264 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002265 if ((ea.argt & EX_COUNT) && VIM_ISDIGIT(*ea.arg)
2266 && (!(ea.argt & EX_BUFNAME) || *(p = skipdigits(ea.arg)) == NUL
Bram Moolenaar1c465442017-03-12 20:10:05 +01002267 || VIM_ISWHITE(*p)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002268 {
2269 n = getdigits(&ea.arg);
2270 ea.arg = skipwhite(ea.arg);
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002271 if (n <= 0 && !ni && (ea.argt & EX_ZEROR) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002272 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002273 errormsg = _(e_zerocount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002274 goto doend;
2275 }
Bram Moolenaarb7316892019-05-01 18:08:42 +02002276 if (ea.addr_type != ADDR_LINES) // e.g. :buffer 2, :sleep 3
Bram Moolenaar071d4272004-06-13 20:20:40 +00002277 {
2278 ea.line2 = n;
2279 if (ea.addr_count == 0)
2280 ea.addr_count = 1;
2281 }
2282 else
2283 {
2284 ea.line1 = ea.line2;
2285 ea.line2 += n - 1;
2286 ++ea.addr_count;
2287 /*
2288 * Be vi compatible: no error message for out of range.
2289 */
Bram Moolenaarb7316892019-05-01 18:08:42 +02002290 if (ea.line2 > curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002291 ea.line2 = curbuf->b_ml.ml_line_count;
2292 }
2293 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00002294
2295 /*
2296 * Check for flags: 'l', 'p' and '#'.
2297 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002298 if (ea.argt & EX_FLAGS)
Bram Moolenaardf177f62005-02-22 08:39:57 +00002299 get_flags(&ea);
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002300 if (!ni && !(ea.argt & EX_EXTRA) && *ea.arg != NUL
2301 && *ea.arg != '"' && (*ea.arg != '|' || (ea.argt & EX_TRLBAR) == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002302 {
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002303 // no arguments allowed but there is something
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002304 errormsg = _(e_trailing);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002305 goto doend;
2306 }
2307
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002308 if (!ni && (ea.argt & EX_NEEDARG) && *ea.arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002309 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002310 errormsg = _(e_argreq);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002311 goto doend;
2312 }
2313
2314#ifdef FEAT_EVAL
2315 /*
2316 * Skip the command when it's not going to be executed.
2317 * The commands like :if, :endif, etc. always need to be executed.
2318 * Also make an exception for commands that handle a trailing command
2319 * themselves.
2320 */
2321 if (ea.skip)
2322 {
2323 switch (ea.cmdidx)
2324 {
2325 /* commands that need evaluation */
2326 case CMD_while:
2327 case CMD_endwhile:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00002328 case CMD_for:
2329 case CMD_endfor:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002330 case CMD_if:
2331 case CMD_elseif:
2332 case CMD_else:
2333 case CMD_endif:
2334 case CMD_try:
2335 case CMD_catch:
2336 case CMD_finally:
2337 case CMD_endtry:
2338 case CMD_function:
2339 break;
2340
2341 /* Commands that handle '|' themselves. Check: A command should
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002342 * either have the EX_TRLBAR flag, appear in this list or appear in
Bram Moolenaar071d4272004-06-13 20:20:40 +00002343 * the list at ":help :bar". */
2344 case CMD_aboveleft:
2345 case CMD_and:
2346 case CMD_belowright:
2347 case CMD_botright:
2348 case CMD_browse:
2349 case CMD_call:
2350 case CMD_confirm:
2351 case CMD_delfunction:
2352 case CMD_djump:
2353 case CMD_dlist:
2354 case CMD_dsearch:
2355 case CMD_dsplit:
2356 case CMD_echo:
2357 case CMD_echoerr:
2358 case CMD_echomsg:
2359 case CMD_echon:
2360 case CMD_execute:
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002361 case CMD_filter:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002362 case CMD_help:
2363 case CMD_hide:
2364 case CMD_ijump:
2365 case CMD_ilist:
2366 case CMD_isearch:
2367 case CMD_isplit:
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002368 case CMD_keepalt:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002369 case CMD_keepjumps:
2370 case CMD_keepmarks:
Bram Moolenaara939e432013-11-09 05:30:26 +01002371 case CMD_keeppatterns:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002372 case CMD_leftabove:
2373 case CMD_let:
2374 case CMD_lockmarks:
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002375 case CMD_lua:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002376 case CMD_match:
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002377 case CMD_mzscheme:
Bram Moolenaar5803ae62014-03-23 16:04:02 +01002378 case CMD_noautocmd:
2379 case CMD_noswapfile:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002380 case CMD_perl:
2381 case CMD_psearch:
2382 case CMD_python:
Bram Moolenaar368373e2010-07-19 20:46:22 +02002383 case CMD_py3:
Bram Moolenaarb6590522010-07-21 16:00:43 +02002384 case CMD_python3:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002385 case CMD_return:
2386 case CMD_rightbelow:
2387 case CMD_ruby:
2388 case CMD_silent:
2389 case CMD_smagic:
2390 case CMD_snomagic:
2391 case CMD_substitute:
2392 case CMD_syntax:
Bram Moolenaara226a6d2006-02-26 23:59:20 +00002393 case CMD_tab:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002394 case CMD_tcl:
2395 case CMD_throw:
2396 case CMD_tilde:
2397 case CMD_topleft:
2398 case CMD_unlet:
2399 case CMD_verbose:
2400 case CMD_vertical:
Bram Moolenaar12bde492011-06-13 01:19:56 +02002401 case CMD_wincmd:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002402 break;
2403
2404 default: goto doend;
2405 }
2406 }
2407#endif
2408
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002409 if (ea.argt & EX_XFILE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002410 {
2411 if (expand_filename(&ea, cmdlinep, &errormsg) == FAIL)
2412 goto doend;
2413 }
2414
Bram Moolenaar071d4272004-06-13 20:20:40 +00002415 /*
2416 * Accept buffer name. Cannot be used at the same time with a buffer
2417 * number. Don't do this for a user command.
2418 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002419 if ((ea.argt & EX_BUFNAME) && *ea.arg != NUL && ea.addr_count == 0
Bram Moolenaar958636c2014-10-21 20:01:58 +02002420 && !IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002421 {
2422 /*
2423 * :bdelete, :bwipeout and :bunload take several arguments, separated
2424 * by spaces: find next space (skipping over escaped characters).
2425 * The others take one argument: ignore trailing spaces.
2426 */
2427 if (ea.cmdidx == CMD_bdelete || ea.cmdidx == CMD_bwipeout
2428 || ea.cmdidx == CMD_bunload)
2429 p = skiptowhite_esc(ea.arg);
2430 else
2431 {
2432 p = ea.arg + STRLEN(ea.arg);
Bram Moolenaar1c465442017-03-12 20:10:05 +01002433 while (p > ea.arg && VIM_ISWHITE(p[-1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002434 --p;
2435 }
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002436 ea.line2 = buflist_findpat(ea.arg, p, (ea.argt & EX_BUFUNL) != 0,
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002437 FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002438 if (ea.line2 < 0) /* failed */
2439 goto doend;
2440 ea.addr_count = 1;
2441 ea.arg = skipwhite(p);
2442 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002443
Bram Moolenaar2be57332018-02-13 18:05:18 +01002444 /* The :try command saves the emsg_silent flag, reset it here when
2445 * ":silent! try" was used, it should only apply to :try itself. */
Bram Moolenaareffed932018-08-14 13:38:17 +02002446 if (ea.cmdidx == CMD_try && ea.did_esilent > 0)
Bram Moolenaar2be57332018-02-13 18:05:18 +01002447 {
Bram Moolenaareffed932018-08-14 13:38:17 +02002448 emsg_silent -= ea.did_esilent;
Bram Moolenaar2be57332018-02-13 18:05:18 +01002449 if (emsg_silent < 0)
2450 emsg_silent = 0;
Bram Moolenaareffed932018-08-14 13:38:17 +02002451 ea.did_esilent = 0;
Bram Moolenaar2be57332018-02-13 18:05:18 +01002452 }
2453
Bram Moolenaar071d4272004-06-13 20:20:40 +00002454/*
Bram Moolenaar2be57332018-02-13 18:05:18 +01002455 * 7. Execute the command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002456 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002457
Bram Moolenaar958636c2014-10-21 20:01:58 +02002458 if (IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002459 {
2460 /*
2461 * Execute a user-defined command.
2462 */
2463 do_ucmd(&ea);
2464 }
2465 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002466 {
2467 /*
Bram Moolenaarac9fb182019-04-27 13:04:13 +02002468 * Call the function to execute the builtin command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002469 */
2470 ea.errmsg = NULL;
2471 (cmdnames[ea.cmdidx].cmd_func)(&ea);
2472 if (ea.errmsg != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002473 errormsg = _(ea.errmsg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002474 }
2475
2476#ifdef FEAT_EVAL
2477 /*
2478 * If the command just executed called do_cmdline(), any throw or ":return"
2479 * or ":finish" encountered there must also check the cstack of the still
2480 * active do_cmdline() that called this do_one_cmd(). Rethrow an uncaught
2481 * exception, or reanimate a returned function or finished script file and
2482 * return or finish it again.
2483 */
2484 if (need_rethrow)
2485 do_throw(cstack);
2486 else if (check_cstack)
2487 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002488 if (source_finished(fgetline, cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002489 do_finish(&ea, TRUE);
Bram Moolenaar89d40322006-08-29 15:30:07 +00002490 else if (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002491 && current_func_returned())
2492 do_return(&ea, TRUE, FALSE, NULL);
2493 }
2494 need_rethrow = check_cstack = FALSE;
2495#endif
2496
2497doend:
2498 if (curwin->w_cursor.lnum == 0) /* can happen with zero line number */
Bram Moolenaar6de5e122017-04-20 21:55:44 +02002499 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002500 curwin->w_cursor.lnum = 1;
Bram Moolenaar6de5e122017-04-20 21:55:44 +02002501 curwin->w_cursor.col = 0;
2502 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002503
2504 if (errormsg != NULL && *errormsg != NUL && !did_emsg)
2505 {
2506 if (sourcing)
2507 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002508 if (errormsg != (char *)IObuff)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002509 {
2510 STRCPY(IObuff, errormsg);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002511 errormsg = (char *)IObuff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002512 }
Bram Moolenaara6f4d612011-09-21 19:10:46 +02002513 append_command(*cmdlinep);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002514 }
2515 emsg(errormsg);
2516 }
2517#ifdef FEAT_EVAL
2518 do_errthrow(cstack,
Bram Moolenaar958636c2014-10-21 20:01:58 +02002519 (ea.cmdidx != CMD_SIZE && !IS_USER_CMDIDX(ea.cmdidx))
2520 ? cmdnames[(int)ea.cmdidx].cmd_name : (char_u *)NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002521#endif
2522
Bram Moolenaareffed932018-08-14 13:38:17 +02002523 if (ea.verbose_save >= 0)
2524 p_verbose = ea.verbose_save;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002525
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002526 free_cmdmod();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002527 cmdmod = save_cmdmod;
Bram Moolenaar9a2c0912019-03-30 14:26:18 +01002528 reg_executing = save_reg_executing;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002529
Bram Moolenaareffed932018-08-14 13:38:17 +02002530 if (ea.save_msg_silent != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002531 {
2532 /* messages could be enabled for a serious error, need to check if the
2533 * counters don't become negative */
Bram Moolenaareffed932018-08-14 13:38:17 +02002534 if (!did_emsg || msg_silent > ea.save_msg_silent)
2535 msg_silent = ea.save_msg_silent;
2536 emsg_silent -= ea.did_esilent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002537 if (emsg_silent < 0)
2538 emsg_silent = 0;
2539 /* Restore msg_scroll, it's set by file I/O commands, even when no
2540 * message is actually displayed. */
2541 msg_scroll = save_msg_scroll;
Bram Moolenaar77ab2802009-04-22 12:44:48 +00002542
2543 /* "silent reg" or "silent echo x" inside "redir" leaves msg_col
2544 * somewhere in the line. Put it back in the first column. */
2545 if (redirecting())
2546 msg_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002547 }
2548
Bram Moolenaar7171abe2004-10-11 10:06:20 +00002549#ifdef HAVE_SANDBOX
Bram Moolenaareffed932018-08-14 13:38:17 +02002550 if (ea.did_sandbox)
Bram Moolenaar7171abe2004-10-11 10:06:20 +00002551 --sandbox;
2552#endif
2553
Bram Moolenaar071d4272004-06-13 20:20:40 +00002554 if (ea.nextcmd && *ea.nextcmd == NUL) /* not really a next command */
2555 ea.nextcmd = NULL;
2556
2557#ifdef FEAT_EVAL
2558 --ex_nesting_level;
2559#endif
2560
2561 return ea.nextcmd;
2562}
2563#if (_MSC_VER == 1200)
Bram Moolenaar281bdce2005-01-25 21:53:18 +00002564 #pragma optimize( "", on )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002565#endif
2566
2567/*
Bram Moolenaareffed932018-08-14 13:38:17 +02002568 * Parse and skip over command modifiers:
2569 * - update eap->cmd
2570 * - store flags in "cmdmod".
2571 * - Set ex_pressedreturn for an empty command line.
2572 * - set msg_silent for ":silent"
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002573 * - set 'eventignore' to "all" for ":noautocmd"
Bram Moolenaareffed932018-08-14 13:38:17 +02002574 * - set p_verbose for ":verbose"
2575 * - Increment "sandbox" for ":sandbox"
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002576 * When "skip_only" is TRUE the global variables are not changed, except for
2577 * "cmdmod".
Bram Moolenaareffed932018-08-14 13:38:17 +02002578 * Return FAIL when the command is not to be executed.
2579 * May set "errormsg" to an error message.
2580 */
2581 int
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002582parse_command_modifiers(exarg_T *eap, char **errormsg, int skip_only)
Bram Moolenaareffed932018-08-14 13:38:17 +02002583{
2584 char_u *p;
2585
2586 vim_memset(&cmdmod, 0, sizeof(cmdmod));
2587 eap->verbose_save = -1;
2588 eap->save_msg_silent = -1;
2589
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002590 // Repeat until no more command modifiers are found.
Bram Moolenaareffed932018-08-14 13:38:17 +02002591 for (;;)
2592 {
Bram Moolenaareffed932018-08-14 13:38:17 +02002593 while (*eap->cmd == ' ' || *eap->cmd == '\t' || *eap->cmd == ':')
2594 ++eap->cmd;
2595
2596 /* in ex mode, an empty line works like :+ */
2597 if (*eap->cmd == NUL && exmode_active
2598 && (getline_equal(eap->getline, eap->cookie, getexmodeline)
2599 || getline_equal(eap->getline, eap->cookie, getexline))
2600 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
2601 {
2602 eap->cmd = (char_u *)"+";
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002603 if (!skip_only)
2604 ex_pressedreturn = TRUE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002605 }
2606
2607 /* ignore comment and empty lines */
2608 if (*eap->cmd == '"')
2609 return FAIL;
2610 if (*eap->cmd == NUL)
2611 {
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002612 if (!skip_only)
2613 ex_pressedreturn = TRUE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002614 return FAIL;
2615 }
2616
Bram Moolenaareffed932018-08-14 13:38:17 +02002617 p = skip_range(eap->cmd, NULL);
2618 switch (*p)
2619 {
2620 /* When adding an entry, also modify cmd_exists(). */
2621 case 'a': if (!checkforcmd(&eap->cmd, "aboveleft", 3))
2622 break;
2623 cmdmod.split |= WSP_ABOVE;
2624 continue;
2625
2626 case 'b': if (checkforcmd(&eap->cmd, "belowright", 3))
2627 {
2628 cmdmod.split |= WSP_BELOW;
2629 continue;
2630 }
2631 if (checkforcmd(&eap->cmd, "browse", 3))
2632 {
2633#ifdef FEAT_BROWSE_CMD
2634 cmdmod.browse = TRUE;
2635#endif
2636 continue;
2637 }
2638 if (!checkforcmd(&eap->cmd, "botright", 2))
2639 break;
2640 cmdmod.split |= WSP_BOT;
2641 continue;
2642
2643 case 'c': if (!checkforcmd(&eap->cmd, "confirm", 4))
2644 break;
2645#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
2646 cmdmod.confirm = TRUE;
2647#endif
2648 continue;
2649
2650 case 'k': if (checkforcmd(&eap->cmd, "keepmarks", 3))
2651 {
2652 cmdmod.keepmarks = TRUE;
2653 continue;
2654 }
2655 if (checkforcmd(&eap->cmd, "keepalt", 5))
2656 {
2657 cmdmod.keepalt = TRUE;
2658 continue;
2659 }
2660 if (checkforcmd(&eap->cmd, "keeppatterns", 5))
2661 {
2662 cmdmod.keeppatterns = TRUE;
2663 continue;
2664 }
2665 if (!checkforcmd(&eap->cmd, "keepjumps", 5))
2666 break;
2667 cmdmod.keepjumps = TRUE;
2668 continue;
2669
2670 case 'f': /* only accept ":filter {pat} cmd" */
2671 {
2672 char_u *reg_pat;
2673
2674 if (!checkforcmd(&p, "filter", 4)
2675 || *p == NUL || ends_excmd(*p))
2676 break;
2677 if (*p == '!')
2678 {
2679 cmdmod.filter_force = TRUE;
2680 p = skipwhite(p + 1);
2681 if (*p == NUL || ends_excmd(*p))
2682 break;
2683 }
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002684 if (skip_only)
2685 p = skip_vimgrep_pat(p, NULL, NULL);
2686 else
2687 // NOTE: This puts a NUL after the pattern.
2688 p = skip_vimgrep_pat(p, &reg_pat, NULL);
Bram Moolenaareffed932018-08-14 13:38:17 +02002689 if (p == NULL || *p == NUL)
2690 break;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002691 if (!skip_only)
2692 {
2693 cmdmod.filter_regmatch.regprog =
Bram Moolenaareffed932018-08-14 13:38:17 +02002694 vim_regcomp(reg_pat, RE_MAGIC);
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002695 if (cmdmod.filter_regmatch.regprog == NULL)
2696 break;
2697 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002698 eap->cmd = p;
2699 continue;
2700 }
2701
2702 /* ":hide" and ":hide | cmd" are not modifiers */
2703 case 'h': if (p != eap->cmd || !checkforcmd(&p, "hide", 3)
2704 || *p == NUL || ends_excmd(*p))
2705 break;
2706 eap->cmd = p;
2707 cmdmod.hide = TRUE;
2708 continue;
2709
2710 case 'l': if (checkforcmd(&eap->cmd, "lockmarks", 3))
2711 {
2712 cmdmod.lockmarks = TRUE;
2713 continue;
2714 }
2715
2716 if (!checkforcmd(&eap->cmd, "leftabove", 5))
2717 break;
2718 cmdmod.split |= WSP_ABOVE;
2719 continue;
2720
2721 case 'n': if (checkforcmd(&eap->cmd, "noautocmd", 3))
2722 {
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002723 if (cmdmod.save_ei == NULL && !skip_only)
Bram Moolenaareffed932018-08-14 13:38:17 +02002724 {
2725 /* Set 'eventignore' to "all". Restore the
2726 * existing option value later. */
2727 cmdmod.save_ei = vim_strsave(p_ei);
2728 set_string_option_direct((char_u *)"ei", -1,
2729 (char_u *)"all", OPT_FREE, SID_NONE);
2730 }
2731 continue;
2732 }
2733 if (!checkforcmd(&eap->cmd, "noswapfile", 3))
2734 break;
2735 cmdmod.noswapfile = TRUE;
2736 continue;
2737
2738 case 'r': if (!checkforcmd(&eap->cmd, "rightbelow", 6))
2739 break;
2740 cmdmod.split |= WSP_BELOW;
2741 continue;
2742
2743 case 's': if (checkforcmd(&eap->cmd, "sandbox", 3))
2744 {
2745#ifdef HAVE_SANDBOX
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002746 if (!skip_only)
2747 {
2748 if (!eap->did_sandbox)
2749 ++sandbox;
2750 eap->did_sandbox = TRUE;
2751 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002752#endif
2753 continue;
2754 }
2755 if (!checkforcmd(&eap->cmd, "silent", 3))
2756 break;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002757 if (!skip_only)
2758 {
2759 if (eap->save_msg_silent == -1)
2760 eap->save_msg_silent = msg_silent;
2761 ++msg_silent;
2762 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002763 if (*eap->cmd == '!' && !VIM_ISWHITE(eap->cmd[-1]))
2764 {
2765 /* ":silent!", but not "silent !cmd" */
2766 eap->cmd = skipwhite(eap->cmd + 1);
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002767 if (!skip_only)
2768 {
2769 ++emsg_silent;
2770 ++eap->did_esilent;
2771 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002772 }
2773 continue;
2774
2775 case 't': if (checkforcmd(&p, "tab", 3))
2776 {
Bram Moolenaar548e5982018-12-26 21:45:00 +01002777 if (!skip_only)
Bram Moolenaareffed932018-08-14 13:38:17 +02002778 {
Bram Moolenaar548e5982018-12-26 21:45:00 +01002779 long tabnr = get_address(eap, &eap->cmd,
2780 ADDR_TABS, eap->skip,
2781 skip_only, FALSE, 1);
2782 if (tabnr == MAXLNUM)
2783 cmdmod.tab = tabpage_index(curtab) + 1;
2784 else
Bram Moolenaareffed932018-08-14 13:38:17 +02002785 {
Bram Moolenaar548e5982018-12-26 21:45:00 +01002786 if (tabnr < 0 || tabnr > LAST_TAB_NR)
2787 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002788 *errormsg = _(e_invrange);
Bram Moolenaar548e5982018-12-26 21:45:00 +01002789 return FAIL;
2790 }
2791 cmdmod.tab = tabnr + 1;
Bram Moolenaareffed932018-08-14 13:38:17 +02002792 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002793 }
2794 eap->cmd = p;
2795 continue;
2796 }
2797 if (!checkforcmd(&eap->cmd, "topleft", 2))
2798 break;
2799 cmdmod.split |= WSP_TOP;
2800 continue;
2801
2802 case 'u': if (!checkforcmd(&eap->cmd, "unsilent", 3))
2803 break;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002804 if (!skip_only)
2805 {
2806 if (eap->save_msg_silent == -1)
2807 eap->save_msg_silent = msg_silent;
2808 msg_silent = 0;
2809 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002810 continue;
2811
2812 case 'v': if (checkforcmd(&eap->cmd, "vertical", 4))
2813 {
2814 cmdmod.split |= WSP_VERT;
2815 continue;
2816 }
2817 if (!checkforcmd(&p, "verbose", 4))
2818 break;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002819 if (!skip_only)
2820 {
2821 if (eap->verbose_save < 0)
2822 eap->verbose_save = p_verbose;
2823 if (vim_isdigit(*eap->cmd))
2824 p_verbose = atoi((char *)eap->cmd);
2825 else
2826 p_verbose = 1;
2827 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002828 eap->cmd = p;
2829 continue;
2830 }
2831 break;
2832 }
2833
2834 return OK;
2835}
2836
2837/*
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002838 * Free contents of "cmdmod".
2839 */
2840 static void
2841free_cmdmod(void)
2842{
2843 if (cmdmod.save_ei != NULL)
2844 {
2845 /* Restore 'eventignore' to the value before ":noautocmd". */
2846 set_string_option_direct((char_u *)"ei", -1, cmdmod.save_ei,
2847 OPT_FREE, SID_NONE);
2848 free_string_option(cmdmod.save_ei);
2849 }
2850
2851 if (cmdmod.filter_regmatch.regprog != NULL)
2852 vim_regfree(cmdmod.filter_regmatch.regprog);
2853}
2854
2855/*
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002856 * Parse the address range, if any, in "eap".
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02002857 * May set the last search pattern, unless "silent" is TRUE.
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002858 * Return FAIL and set "errormsg" or return OK.
2859 */
2860 int
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002861parse_cmd_address(exarg_T *eap, char **errormsg, int silent)
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002862{
2863 int address_count = 1;
2864 linenr_T lnum;
2865
2866 // Repeat for all ',' or ';' separated addresses.
2867 for (;;)
2868 {
2869 eap->line1 = eap->line2;
2870 switch (eap->addr_type)
2871 {
2872 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02002873 case ADDR_OTHER:
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002874 // default is current line number
2875 eap->line2 = curwin->w_cursor.lnum;
2876 break;
2877 case ADDR_WINDOWS:
2878 eap->line2 = CURRENT_WIN_NR;
2879 break;
2880 case ADDR_ARGUMENTS:
2881 eap->line2 = curwin->w_arg_idx + 1;
2882 if (eap->line2 > ARGCOUNT)
2883 eap->line2 = ARGCOUNT;
2884 break;
2885 case ADDR_LOADED_BUFFERS:
2886 case ADDR_BUFFERS:
2887 eap->line2 = curbuf->b_fnum;
2888 break;
2889 case ADDR_TABS:
2890 eap->line2 = CURRENT_TAB_NR;
2891 break;
2892 case ADDR_TABS_RELATIVE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02002893 case ADDR_UNSIGNED:
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002894 eap->line2 = 1;
2895 break;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002896 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02002897#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02002898 eap->line2 = qf_get_cur_idx(eap);
2899#endif
2900 break;
2901 case ADDR_QUICKFIX_VALID:
2902#ifdef FEAT_QUICKFIX
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002903 eap->line2 = qf_get_cur_valid_idx(eap);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002904#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02002905 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02002906 case ADDR_NONE:
2907 // Will give an error later if a range is found.
2908 break;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002909 }
2910 eap->cmd = skipwhite(eap->cmd);
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02002911 lnum = get_address(eap, &eap->cmd, eap->addr_type, eap->skip, silent,
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002912 eap->addr_count == 0, address_count++);
2913 if (eap->cmd == NULL) // error detected
2914 return FAIL;
2915 if (lnum == MAXLNUM)
2916 {
2917 if (*eap->cmd == '%') // '%' - all lines
2918 {
2919 ++eap->cmd;
2920 switch (eap->addr_type)
2921 {
2922 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02002923 case ADDR_OTHER:
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002924 eap->line1 = 1;
2925 eap->line2 = curbuf->b_ml.ml_line_count;
2926 break;
2927 case ADDR_LOADED_BUFFERS:
2928 {
2929 buf_T *buf = firstbuf;
2930
2931 while (buf->b_next != NULL
2932 && buf->b_ml.ml_mfp == NULL)
2933 buf = buf->b_next;
2934 eap->line1 = buf->b_fnum;
2935 buf = lastbuf;
2936 while (buf->b_prev != NULL
2937 && buf->b_ml.ml_mfp == NULL)
2938 buf = buf->b_prev;
2939 eap->line2 = buf->b_fnum;
2940 break;
2941 }
2942 case ADDR_BUFFERS:
2943 eap->line1 = firstbuf->b_fnum;
2944 eap->line2 = lastbuf->b_fnum;
2945 break;
2946 case ADDR_WINDOWS:
2947 case ADDR_TABS:
2948 if (IS_USER_CMDIDX(eap->cmdidx))
2949 {
2950 eap->line1 = 1;
2951 eap->line2 = eap->addr_type == ADDR_WINDOWS
2952 ? LAST_WIN_NR : LAST_TAB_NR;
2953 }
2954 else
2955 {
2956 // there is no Vim command which uses '%' and
2957 // ADDR_WINDOWS or ADDR_TABS
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002958 *errormsg = _(e_invrange);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002959 return FAIL;
2960 }
2961 break;
2962 case ADDR_TABS_RELATIVE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02002963 case ADDR_UNSIGNED:
2964 case ADDR_QUICKFIX:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002965 *errormsg = _(e_invrange);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002966 return FAIL;
2967 case ADDR_ARGUMENTS:
2968 if (ARGCOUNT == 0)
2969 eap->line1 = eap->line2 = 0;
2970 else
2971 {
2972 eap->line1 = 1;
2973 eap->line2 = ARGCOUNT;
2974 }
2975 break;
Bram Moolenaar25190db2019-05-04 15:05:28 +02002976 case ADDR_QUICKFIX_VALID:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02002977#ifdef FEAT_QUICKFIX
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002978 eap->line1 = 1;
Bram Moolenaar25190db2019-05-04 15:05:28 +02002979 eap->line2 = qf_get_valid_size(eap);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002980 if (eap->line2 == 0)
2981 eap->line2 = 1;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002982#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02002983 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02002984 case ADDR_NONE:
2985 // Will give an error later if a range is found.
2986 break;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002987 }
2988 ++eap->addr_count;
2989 }
2990 else if (*eap->cmd == '*' && vim_strchr(p_cpo, CPO_STAR) == NULL)
2991 {
2992 pos_T *fp;
2993
2994 // '*' - visual area
2995 if (eap->addr_type != ADDR_LINES)
2996 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002997 *errormsg = _(e_invrange);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002998 return FAIL;
2999 }
3000
3001 ++eap->cmd;
3002 if (!eap->skip)
3003 {
3004 fp = getmark('<', FALSE);
3005 if (check_mark(fp) == FAIL)
3006 return FAIL;
3007 eap->line1 = fp->lnum;
3008 fp = getmark('>', FALSE);
3009 if (check_mark(fp) == FAIL)
3010 return FAIL;
3011 eap->line2 = fp->lnum;
3012 ++eap->addr_count;
3013 }
3014 }
3015 }
3016 else
3017 eap->line2 = lnum;
3018 eap->addr_count++;
3019
3020 if (*eap->cmd == ';')
3021 {
3022 if (!eap->skip)
3023 {
3024 curwin->w_cursor.lnum = eap->line2;
3025 // don't leave the cursor on an illegal line or column
3026 check_cursor();
3027 }
3028 }
3029 else if (*eap->cmd != ',')
3030 break;
3031 ++eap->cmd;
3032 }
3033
3034 // One address given: set start and end lines.
3035 if (eap->addr_count == 1)
3036 {
3037 eap->line1 = eap->line2;
3038 // ... but only implicit: really no address given
3039 if (lnum == MAXLNUM)
3040 eap->addr_count = 0;
3041 }
3042 return OK;
3043}
3044
3045/*
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00003046 * Check for an Ex command with optional tail.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003047 * If there is a match advance "pp" to the argument and return TRUE.
3048 */
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00003049 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003050checkforcmd(
3051 char_u **pp, /* start of command */
3052 char *cmd, /* name of command */
3053 int len) /* required length */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003054{
3055 int i;
3056
3057 for (i = 0; cmd[i] != NUL; ++i)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003058 if (((char_u *)cmd)[i] != (*pp)[i])
Bram Moolenaar071d4272004-06-13 20:20:40 +00003059 break;
3060 if (i >= len && !isalpha((*pp)[i]))
3061 {
3062 *pp = skipwhite(*pp + i);
3063 return TRUE;
3064 }
3065 return FALSE;
3066}
3067
3068/*
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003069 * Append "cmd" to the error message in IObuff.
3070 * Takes care of limiting the length and handling 0xa0, which would be
3071 * invisible otherwise.
3072 */
3073 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003074append_command(char_u *cmd)
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003075{
3076 char_u *s = cmd;
3077 char_u *d;
3078
3079 STRCAT(IObuff, ": ");
3080 d = IObuff + STRLEN(IObuff);
3081 while (*s != NUL && d - IObuff < IOSIZE - 7)
3082 {
Bram Moolenaar13505972019-01-24 15:04:48 +01003083 if (enc_utf8 ? (s[0] == 0xc2 && s[1] == 0xa0) : *s == 0xa0)
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003084 {
Bram Moolenaar13505972019-01-24 15:04:48 +01003085 s += enc_utf8 ? 2 : 1;
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003086 STRCPY(d, "<a0>");
3087 d += 4;
3088 }
3089 else
3090 MB_COPY_CHAR(s, d);
3091 }
3092 *d = NUL;
3093}
3094
3095/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003096 * Find an Ex command by its name, either built-in or user.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003097 * Start of the name can be found at eap->cmd.
Bram Moolenaar25190db2019-05-04 15:05:28 +02003098 * Sets eap->cmdidx and returns a pointer to char after the command name.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003099 * "full" is set to TRUE if the whole command name matched.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003100 * Returns NULL for an ambiguous user command.
3101 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003102 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003103find_command(exarg_T *eap, int *full UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003104{
3105 int len;
3106 char_u *p;
Bram Moolenaardf177f62005-02-22 08:39:57 +00003107 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003108
3109 /*
3110 * Isolate the command and search for it in the command table.
Bram Moolenaar81870892007-11-11 18:17:28 +00003111 * Exceptions:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003112 * - the 'k' command can directly be followed by any character.
3113 * - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003114 * but :sre[wind] is another command, as are :scr[iptnames],
Bram Moolenaar071d4272004-06-13 20:20:40 +00003115 * :scs[cope], :sim[alt], :sig[ns] and :sil[ent].
Bram Moolenaardf177f62005-02-22 08:39:57 +00003116 * - the "d" command can directly be followed by 'l' or 'p' flag.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003117 */
3118 p = eap->cmd;
3119 if (*p == 'k')
3120 {
3121 eap->cmdidx = CMD_k;
3122 ++p;
3123 }
3124 else if (p[0] == 's'
Bram Moolenaar204b93f2015-08-04 22:02:51 +02003125 && ((p[1] == 'c' && (p[2] == NUL || (p[2] != 's' && p[2] != 'r'
3126 && (p[3] == NUL || (p[3] != 'i' && p[4] != 'p')))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003127 || p[1] == 'g'
3128 || (p[1] == 'i' && p[2] != 'm' && p[2] != 'l' && p[2] != 'g')
3129 || p[1] == 'I'
3130 || (p[1] == 'r' && p[2] != 'e')))
3131 {
3132 eap->cmdidx = CMD_substitute;
3133 ++p;
3134 }
3135 else
3136 {
3137 while (ASCII_ISALPHA(*p))
3138 ++p;
Bram Moolenaarb6590522010-07-21 16:00:43 +02003139 /* for python 3.x support ":py3", ":python3", ":py3file", etc. */
Bram Moolenaar55d5c032010-07-17 23:52:29 +02003140 if (eap->cmd[0] == 'p' && eap->cmd[1] == 'y')
Bram Moolenaarb6590522010-07-21 16:00:43 +02003141 while (ASCII_ISALNUM(*p))
3142 ++p;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02003143
Bram Moolenaar071d4272004-06-13 20:20:40 +00003144 /* check for non-alpha command */
3145 if (p == eap->cmd && vim_strchr((char_u *)"@*!=><&~#", *p) != NULL)
3146 ++p;
3147 len = (int)(p - eap->cmd);
Bram Moolenaardf177f62005-02-22 08:39:57 +00003148 if (*eap->cmd == 'd' && (p[-1] == 'l' || p[-1] == 'p'))
3149 {
3150 /* Check for ":dl", ":dell", etc. to ":deletel": that's
3151 * :delete with the 'l' flag. Same for 'p'. */
3152 for (i = 0; i < len; ++i)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003153 if (eap->cmd[i] != ((char_u *)"delete")[i])
Bram Moolenaardf177f62005-02-22 08:39:57 +00003154 break;
3155 if (i == len - 1)
3156 {
3157 --len;
3158 if (p[-1] == 'l')
3159 eap->flags |= EXFLAG_LIST;
3160 else
3161 eap->flags |= EXFLAG_PRINT;
3162 }
3163 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003164
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003165 if (ASCII_ISLOWER(eap->cmd[0]))
3166 {
Bram Moolenaar6c0c1e82017-03-25 15:07:43 +01003167 int c1 = eap->cmd[0];
Bram Moolenaar94f82cb2019-07-24 22:30:27 +02003168 int c2 = len == 1 ? NUL : eap->cmd[1];
Bram Moolenaar6c0c1e82017-03-25 15:07:43 +01003169
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003170 if (command_count != (int)CMD_SIZE)
3171 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003172 iemsg(_("E943: Command table needs to be updated, run 'make cmdidxs'"));
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003173 getout(1);
3174 }
3175
3176 /* Use a precomputed index for fast look-up in cmdnames[]
3177 * taking into account the first 2 letters of eap->cmd. */
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003178 eap->cmdidx = cmdidxs1[CharOrdLow(c1)];
3179 if (ASCII_ISLOWER(c2))
3180 eap->cmdidx += cmdidxs2[CharOrdLow(c1)][CharOrdLow(c2)];
3181 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003182 else
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003183 eap->cmdidx = CMD_bang;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003184
3185 for ( ; (int)eap->cmdidx < (int)CMD_SIZE;
3186 eap->cmdidx = (cmdidx_T)((int)eap->cmdidx + 1))
3187 if (STRNCMP(cmdnames[(int)eap->cmdidx].cmd_name, (char *)eap->cmd,
3188 (size_t)len) == 0)
3189 {
3190#ifdef FEAT_EVAL
3191 if (full != NULL
3192 && cmdnames[(int)eap->cmdidx].cmd_name[len] == NUL)
3193 *full = TRUE;
3194#endif
3195 break;
3196 }
3197
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003198 // Look for a user defined command as a last resort. Let ":Print" be
3199 // overruled by a user defined command.
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01003200 if ((eap->cmdidx == CMD_SIZE || eap->cmdidx == CMD_Print)
3201 && *eap->cmd >= 'A' && *eap->cmd <= 'Z')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003202 {
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003203 // User defined commands may contain digits.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003204 while (ASCII_ISALNUM(*p))
3205 ++p;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003206 p = find_ucmd(eap, p, full, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003207 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003208 if (p == eap->cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003209 eap->cmdidx = CMD_SIZE;
3210 }
3211
3212 return p;
3213}
3214
3215#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003216static struct cmdmod
3217{
3218 char *name;
3219 int minlen;
3220 int has_count; /* :123verbose :3tab */
3221} cmdmods[] = {
3222 {"aboveleft", 3, FALSE},
3223 {"belowright", 3, FALSE},
3224 {"botright", 2, FALSE},
3225 {"browse", 3, FALSE},
3226 {"confirm", 4, FALSE},
Bram Moolenaar7b668e82016-08-23 23:51:21 +02003227 {"filter", 4, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003228 {"hide", 3, FALSE},
3229 {"keepalt", 5, FALSE},
3230 {"keepjumps", 5, FALSE},
3231 {"keepmarks", 3, FALSE},
Bram Moolenaara939e432013-11-09 05:30:26 +01003232 {"keeppatterns", 5, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003233 {"leftabove", 5, FALSE},
3234 {"lockmarks", 3, FALSE},
Bram Moolenaarca9f9582008-09-18 10:44:28 +00003235 {"noautocmd", 3, FALSE},
Bram Moolenaar5803ae62014-03-23 16:04:02 +01003236 {"noswapfile", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003237 {"rightbelow", 6, FALSE},
3238 {"sandbox", 3, FALSE},
3239 {"silent", 3, FALSE},
3240 {"tab", 3, TRUE},
3241 {"topleft", 2, FALSE},
Bram Moolenaar8e258a42009-07-09 13:55:43 +00003242 {"unsilent", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003243 {"verbose", 4, TRUE},
3244 {"vertical", 4, FALSE},
3245};
3246
3247/*
3248 * Return length of a command modifier (including optional count).
3249 * Return zero when it's not a modifier.
3250 */
3251 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003252modifier_len(char_u *cmd)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003253{
3254 int i, j;
3255 char_u *p = cmd;
3256
3257 if (VIM_ISDIGIT(*cmd))
3258 p = skipwhite(skipdigits(cmd));
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00003259 for (i = 0; i < (int)(sizeof(cmdmods) / sizeof(struct cmdmod)); ++i)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003260 {
3261 for (j = 0; p[j] != NUL; ++j)
3262 if (p[j] != cmdmods[i].name[j])
3263 break;
Bram Moolenaar2d473ab2013-06-12 17:12:24 +02003264 if (!ASCII_ISALPHA(p[j]) && j >= cmdmods[i].minlen
Bram Moolenaared53fb92007-11-24 20:50:24 +00003265 && (p == cmd || cmdmods[i].has_count))
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00003266 return j + (int)(p - cmd);
Bram Moolenaared53fb92007-11-24 20:50:24 +00003267 }
3268 return 0;
3269}
3270
Bram Moolenaar071d4272004-06-13 20:20:40 +00003271/*
3272 * Return > 0 if an Ex command "name" exists.
3273 * Return 2 if there is an exact match.
3274 * Return 3 if there is an ambiguous match.
3275 */
3276 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003277cmd_exists(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003278{
3279 exarg_T ea;
3280 int full = FALSE;
3281 int i;
3282 int j;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003283 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003284
3285 /* Check command modifiers. */
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00003286 for (i = 0; i < (int)(sizeof(cmdmods) / sizeof(struct cmdmod)); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003287 {
3288 for (j = 0; name[j] != NUL; ++j)
3289 if (name[j] != cmdmods[i].name[j])
3290 break;
3291 if (name[j] == NUL && j >= cmdmods[i].minlen)
3292 return (cmdmods[i].name[j] == NUL ? 2 : 1);
3293 }
3294
Bram Moolenaara9587612006-05-04 21:47:50 +00003295 /* Check built-in commands and user defined commands.
3296 * For ":2match" and ":3match" we need to skip the number. */
3297 ea.cmd = (*name == '2' || *name == '3') ? name + 1 : name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003298 ea.cmdidx = (cmdidx_T)0;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003299 p = find_command(&ea, &full);
3300 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003301 return 3;
Bram Moolenaara9587612006-05-04 21:47:50 +00003302 if (vim_isdigit(*name) && ea.cmdidx != CMD_match)
3303 return 0;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003304 if (*skipwhite(p) != NUL)
3305 return 0; /* trailing garbage */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003306 return (ea.cmdidx == CMD_SIZE ? 0 : (full ? 2 : 1));
3307}
3308#endif
3309
3310/*
3311 * This is all pretty much copied from do_one_cmd(), with all the extra stuff
3312 * we don't need/want deleted. Maybe this could be done better if we didn't
3313 * repeat all this stuff. The only problem is that they may not stay
3314 * perfectly compatible with each other, but then the command line syntax
3315 * probably won't change that much -- webb.
3316 */
3317 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003318set_one_cmd_context(
3319 expand_T *xp,
3320 char_u *buff) /* buffer for command string */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003321{
3322 char_u *p;
3323 char_u *cmd, *arg;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003324 int len = 0;
3325 exarg_T ea;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003326#ifdef FEAT_CMDL_COMPL
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003327 int compl = EXPAND_NOTHING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003328 int delim;
3329#endif
3330 int forceit = FALSE;
3331 int usefilter = FALSE; /* filter instead of file name */
3332
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003333 ExpandInit(xp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003334 xp->xp_pattern = buff;
3335 xp->xp_context = EXPAND_COMMANDS; /* Default until we get past command */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003336 ea.argt = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003337
3338/*
Bram Moolenaar1c40a662014-11-27 16:38:11 +01003339 * 1. skip comment lines and leading space, colons or bars
Bram Moolenaar071d4272004-06-13 20:20:40 +00003340 */
3341 for (cmd = buff; vim_strchr((char_u *)" \t:|", *cmd) != NULL; cmd++)
3342 ;
3343 xp->xp_pattern = cmd;
3344
3345 if (*cmd == NUL)
3346 return NULL;
3347 if (*cmd == '"') /* ignore comment lines */
3348 {
3349 xp->xp_context = EXPAND_NOTHING;
3350 return NULL;
3351 }
3352
3353/*
Bram Moolenaar1c40a662014-11-27 16:38:11 +01003354 * 3. Skip over the range to find the command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003355 */
3356 cmd = skip_range(cmd, &xp->xp_context);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003357 xp->xp_pattern = cmd;
3358 if (*cmd == NUL)
3359 return NULL;
3360 if (*cmd == '"')
3361 {
3362 xp->xp_context = EXPAND_NOTHING;
3363 return NULL;
3364 }
3365
3366 if (*cmd == '|' || *cmd == '\n')
3367 return cmd + 1; /* There's another command */
3368
3369 /*
3370 * Isolate the command and search for it in the command table.
3371 * Exceptions:
3372 * - the 'k' command can directly be followed by any character, but
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003373 * do accept "keepmarks", "keepalt" and "keepjumps".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003374 * - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
3375 */
3376 if (*cmd == 'k' && cmd[1] != 'e')
3377 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003378 ea.cmdidx = CMD_k;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003379 p = cmd + 1;
3380 }
3381 else
3382 {
3383 p = cmd;
3384 while (ASCII_ISALPHA(*p) || *p == '*') /* Allow * wild card */
3385 ++p;
Bram Moolenaar23d1b622015-10-13 19:18:04 +02003386 /* a user command may contain digits */
3387 if (ASCII_ISUPPER(cmd[0]))
3388 while (ASCII_ISALNUM(*p) || *p == '*')
3389 ++p;
Bram Moolenaar9f5d6002013-06-02 19:22:13 +02003390 /* for python 3.x: ":py3*" commands completion */
3391 if (cmd[0] == 'p' && cmd[1] == 'y' && p == cmd + 2 && *p == '3')
Bram Moolenaar893b2d72013-12-11 17:44:38 +01003392 {
Bram Moolenaar9f5d6002013-06-02 19:22:13 +02003393 ++p;
Bram Moolenaar893b2d72013-12-11 17:44:38 +01003394 while (ASCII_ISALPHA(*p) || *p == '*')
3395 ++p;
3396 }
Bram Moolenaar23d1b622015-10-13 19:18:04 +02003397 /* check for non-alpha command */
3398 if (p == cmd && vim_strchr((char_u *)"@*!=><&~#", *p) != NULL)
3399 ++p;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003400 len = (int)(p - cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003401
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003402 if (len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003403 {
3404 xp->xp_context = EXPAND_UNSUCCESSFUL;
3405 return NULL;
3406 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003407 for (ea.cmdidx = (cmdidx_T)0; (int)ea.cmdidx < (int)CMD_SIZE;
Bram Moolenaar42b4dda2010-03-02 15:56:05 +01003408 ea.cmdidx = (cmdidx_T)((int)ea.cmdidx + 1))
3409 if (STRNCMP(cmdnames[(int)ea.cmdidx].cmd_name, cmd,
3410 (size_t)len) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003411 break;
3412
Bram Moolenaar071d4272004-06-13 20:20:40 +00003413 if (cmd[0] >= 'A' && cmd[0] <= 'Z')
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003414 while (ASCII_ISALNUM(*p) || *p == '*') // Allow * wild card
Bram Moolenaar071d4272004-06-13 20:20:40 +00003415 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003416 }
3417
3418 /*
3419 * If the cursor is touching the command, and it ends in an alpha-numeric
3420 * character, complete the command name.
3421 */
3422 if (*p == NUL && ASCII_ISALNUM(p[-1]))
3423 return NULL;
3424
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003425 if (ea.cmdidx == CMD_SIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003426 {
3427 if (*cmd == 's' && vim_strchr((char_u *)"cgriI", cmd[1]) != NULL)
3428 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003429 ea.cmdidx = CMD_substitute;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003430 p = cmd + 1;
3431 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003432 else if (cmd[0] >= 'A' && cmd[0] <= 'Z')
3433 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003434 ea.cmd = cmd;
3435 p = find_ucmd(&ea, p, NULL, xp,
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003436#if defined(FEAT_CMDL_COMPL)
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003437 &compl
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003438#else
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003439 NULL
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003440#endif
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003441 );
Bram Moolenaarebefac62005-12-28 22:39:57 +00003442 if (p == NULL)
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003443 ea.cmdidx = CMD_SIZE; // ambiguous user command
Bram Moolenaar071d4272004-06-13 20:20:40 +00003444 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003445 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003446 if (ea.cmdidx == CMD_SIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003447 {
3448 /* Not still touching the command and it was an illegal one */
3449 xp->xp_context = EXPAND_UNSUCCESSFUL;
3450 return NULL;
3451 }
3452
3453 xp->xp_context = EXPAND_NOTHING; /* Default now that we're past command */
3454
3455 if (*p == '!') /* forced commands */
3456 {
3457 forceit = TRUE;
3458 ++p;
3459 }
3460
3461/*
Bram Moolenaar1c40a662014-11-27 16:38:11 +01003462 * 6. parse arguments
Bram Moolenaar071d4272004-06-13 20:20:40 +00003463 */
Bram Moolenaar958636c2014-10-21 20:01:58 +02003464 if (!IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003465 ea.argt = (long)cmdnames[(int)ea.cmdidx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003466
3467 arg = skipwhite(p);
3468
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003469 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003470 {
3471 if (*arg == '>') /* append */
3472 {
3473 if (*++arg == '>')
3474 ++arg;
3475 arg = skipwhite(arg);
3476 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003477 else if (*arg == '!' && ea.cmdidx == CMD_write) /* :w !filter */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003478 {
3479 ++arg;
3480 usefilter = TRUE;
3481 }
3482 }
3483
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003484 if (ea.cmdidx == CMD_read)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003485 {
3486 usefilter = forceit; /* :r! filter if forced */
3487 if (*arg == '!') /* :r !filter */
3488 {
3489 ++arg;
3490 usefilter = TRUE;
3491 }
3492 }
3493
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003494 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003495 {
3496 while (*arg == *cmd) /* allow any number of '>' or '<' */
3497 ++arg;
3498 arg = skipwhite(arg);
3499 }
3500
3501 /* Does command allow "+command"? */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02003502 if ((ea.argt & EX_CMDARG) && !usefilter && *arg == '+')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003503 {
3504 /* Check if we're in the +command */
3505 p = arg + 1;
3506 arg = skip_cmd_arg(arg, FALSE);
3507
3508 /* Still touching the command after '+'? */
3509 if (*arg == NUL)
3510 return p;
3511
3512 /* Skip space(s) after +command to get to the real argument */
3513 arg = skipwhite(arg);
3514 }
3515
3516 /*
3517 * Check for '|' to separate commands and '"' to start comments.
3518 * Don't do this for ":read !cmd" and ":write !cmd".
3519 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02003520 if ((ea.argt & EX_TRLBAR) && !usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003521 {
3522 p = arg;
3523 /* ":redir @" is not the start of a comment */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003524 if (ea.cmdidx == CMD_redir && p[0] == '@' && p[1] == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003525 p += 2;
3526 while (*p)
3527 {
3528 if (*p == Ctrl_V)
3529 {
3530 if (p[1] != NUL)
3531 ++p;
3532 }
Bram Moolenaar8071cb22019-07-12 17:58:01 +02003533 else if ( (*p == '"' && !(ea.argt & EX_NOTRLCOM))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003534 || *p == '|' || *p == '\n')
3535 {
3536 if (*(p - 1) != '\\')
3537 {
3538 if (*p == '|' || *p == '\n')
3539 return p + 1;
3540 return NULL; /* It's a comment */
3541 }
3542 }
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003543 MB_PTR_ADV(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003544 }
3545 }
3546
Bram Moolenaar8071cb22019-07-12 17:58:01 +02003547 if (!(ea.argt & EX_EXTRA) && *arg != NUL
3548 && vim_strchr((char_u *)"|\"", *arg) == NULL)
3549 // no arguments allowed but there is something
Bram Moolenaar071d4272004-06-13 20:20:40 +00003550 return NULL;
3551
3552 /* Find start of last argument (argument just before cursor): */
Bram Moolenaar848f8762012-07-25 17:22:23 +02003553 p = buff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003554 xp->xp_pattern = p;
Bram Moolenaar09168a72012-08-02 21:24:42 +02003555 len = (int)STRLEN(buff);
Bram Moolenaar848f8762012-07-25 17:22:23 +02003556 while (*p && p < buff + len)
3557 {
3558 if (*p == ' ' || *p == TAB)
3559 {
3560 /* argument starts after a space */
3561 xp->xp_pattern = ++p;
3562 }
3563 else
3564 {
3565 if (*p == '\\' && *(p + 1) != NUL)
3566 ++p; /* skip over escaped character */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003567 MB_PTR_ADV(p);
Bram Moolenaar848f8762012-07-25 17:22:23 +02003568 }
3569 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003570
Bram Moolenaar8071cb22019-07-12 17:58:01 +02003571 if (ea.argt & EX_XFILE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003572 {
Bram Moolenaar6529c102007-08-18 15:47:34 +00003573 int c;
3574 int in_quote = FALSE;
3575 char_u *bow = NULL; /* Beginning of word */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003576
3577 /*
3578 * Allow spaces within back-quotes to count as part of the argument
3579 * being expanded.
3580 */
3581 xp->xp_pattern = skipwhite(arg);
Bram Moolenaar6529c102007-08-18 15:47:34 +00003582 p = xp->xp_pattern;
3583 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003584 {
Bram Moolenaar6529c102007-08-18 15:47:34 +00003585 if (has_mbyte)
3586 c = mb_ptr2char(p);
3587 else
Bram Moolenaar6529c102007-08-18 15:47:34 +00003588 c = *p;
3589 if (c == '\\' && p[1] != NUL)
3590 ++p;
3591 else if (c == '`')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003592 {
3593 if (!in_quote)
3594 {
3595 xp->xp_pattern = p;
3596 bow = p + 1;
3597 }
3598 in_quote = !in_quote;
3599 }
Bram Moolenaar332fa0c2008-01-13 16:12:10 +00003600 /* An argument can contain just about everything, except
3601 * characters that end the command and white space. */
Bram Moolenaar1c465442017-03-12 20:10:05 +01003602 else if (c == '|' || c == '\n' || c == '"' || (VIM_ISWHITE(c)
Bram Moolenaar6529c102007-08-18 15:47:34 +00003603#ifdef SPACE_IN_FILENAME
Bram Moolenaar8071cb22019-07-12 17:58:01 +02003604 && (!(ea.argt & EX_NOSPC) || usefilter)
Bram Moolenaar6529c102007-08-18 15:47:34 +00003605#endif
Bram Moolenaar332fa0c2008-01-13 16:12:10 +00003606 ))
Bram Moolenaar6529c102007-08-18 15:47:34 +00003607 {
Bram Moolenaarcf5a5b82008-02-26 20:30:12 +00003608 len = 0; /* avoid getting stuck when space is in 'isfname' */
Bram Moolenaar6529c102007-08-18 15:47:34 +00003609 while (*p != NUL)
3610 {
Bram Moolenaar6529c102007-08-18 15:47:34 +00003611 if (has_mbyte)
3612 c = mb_ptr2char(p);
3613 else
Bram Moolenaar6529c102007-08-18 15:47:34 +00003614 c = *p;
Bram Moolenaardd87969c2007-08-21 13:07:12 +00003615 if (c == '`' || vim_isfilec_or_wc(c))
Bram Moolenaar6529c102007-08-18 15:47:34 +00003616 break;
Bram Moolenaar6529c102007-08-18 15:47:34 +00003617 if (has_mbyte)
3618 len = (*mb_ptr2len)(p);
3619 else
Bram Moolenaar6529c102007-08-18 15:47:34 +00003620 len = 1;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003621 MB_PTR_ADV(p);
Bram Moolenaar6529c102007-08-18 15:47:34 +00003622 }
3623 if (in_quote)
3624 bow = p;
3625 else
3626 xp->xp_pattern = p;
3627 p -= len;
3628 }
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003629 MB_PTR_ADV(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003630 }
3631
3632 /*
3633 * If we are still inside the quotes, and we passed a space, just
3634 * expand from there.
3635 */
3636 if (bow != NULL && in_quote)
3637 xp->xp_pattern = bow;
3638 xp->xp_context = EXPAND_FILES;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003639
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003640 /* For a shell command more chars need to be escaped. */
Bram Moolenaar67883b42017-07-27 22:57:00 +02003641 if (usefilter || ea.cmdidx == CMD_bang || ea.cmdidx == CMD_terminal)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003642 {
Bram Moolenaarc0cba4d2010-08-07 17:07:21 +02003643#ifndef BACKSLASH_IN_FILENAME
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003644 xp->xp_shell = TRUE;
Bram Moolenaarc0cba4d2010-08-07 17:07:21 +02003645#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003646 /* When still after the command name expand executables. */
3647 if (xp->xp_pattern == skipwhite(arg))
Bram Moolenaar5c5b0942007-05-06 12:07:59 +00003648 xp->xp_context = EXPAND_SHELLCMD;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003649 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003650
3651 /* Check for environment variable */
3652 if (*xp->xp_pattern == '$'
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003653#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003654 || *xp->xp_pattern == '%'
3655#endif
3656 )
3657 {
3658 for (p = xp->xp_pattern + 1; *p != NUL; ++p)
3659 if (!vim_isIDc(*p))
3660 break;
3661 if (*p == NUL)
3662 {
3663 xp->xp_context = EXPAND_ENV_VARS;
3664 ++xp->xp_pattern;
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003665#if defined(FEAT_CMDL_COMPL)
Bram Moolenaar21cf8232004-07-16 20:18:37 +00003666 /* Avoid that the assignment uses EXPAND_FILES again. */
Bram Moolenaara466c992005-07-09 21:03:22 +00003667 if (compl != EXPAND_USER_DEFINED && compl != EXPAND_USER_LIST)
Bram Moolenaar21cf8232004-07-16 20:18:37 +00003668 compl = EXPAND_ENV_VARS;
3669#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003670 }
3671 }
Bram Moolenaar24305862012-08-15 14:05:05 +02003672#if defined(FEAT_CMDL_COMPL)
3673 /* Check for user names */
3674 if (*xp->xp_pattern == '~')
3675 {
3676 for (p = xp->xp_pattern + 1; *p != NUL && *p != '/'; ++p)
3677 ;
3678 /* Complete ~user only if it partially matches a user name.
3679 * A full match ~user<Tab> will be replaced by user's home
3680 * directory i.e. something like ~user<Tab> -> /home/user/ */
3681 if (*p == NUL && p > xp->xp_pattern + 1
Bram Moolenaar6c5d1042018-07-07 16:41:13 +02003682 && match_user(xp->xp_pattern + 1) >= 1)
Bram Moolenaar24305862012-08-15 14:05:05 +02003683 {
3684 xp->xp_context = EXPAND_USER;
3685 ++xp->xp_pattern;
3686 }
3687 }
3688#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003689 }
3690
3691/*
Bram Moolenaar1c40a662014-11-27 16:38:11 +01003692 * 6. Switch on command name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003693 */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003694 switch (ea.cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003695 {
Bram Moolenaarcc448b32010-07-14 16:52:17 +02003696 case CMD_find:
3697 case CMD_sfind:
3698 case CMD_tabfind:
Bram Moolenaarc24b6972010-08-16 22:34:29 +02003699 if (xp->xp_context == EXPAND_FILES)
3700 xp->xp_context = EXPAND_FILES_IN_PATH;
Bram Moolenaarcc448b32010-07-14 16:52:17 +02003701 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003702 case CMD_cd:
3703 case CMD_chdir:
Bram Moolenaar00aa0692019-04-27 20:37:57 +02003704 case CMD_tcd:
3705 case CMD_tchdir:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003706 case CMD_lcd:
3707 case CMD_lchdir:
3708 if (xp->xp_context == EXPAND_FILES)
3709 xp->xp_context = EXPAND_DIRECTORIES;
3710 break;
3711 case CMD_help:
3712 xp->xp_context = EXPAND_HELP;
3713 xp->xp_pattern = arg;
3714 break;
3715
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003716 /* Command modifiers: return the argument.
3717 * Also for commands with an argument that is a command. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003718 case CMD_aboveleft:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003719 case CMD_argdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003720 case CMD_belowright:
3721 case CMD_botright:
3722 case CMD_browse:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003723 case CMD_bufdo:
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003724 case CMD_cdo:
3725 case CMD_cfdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003726 case CMD_confirm:
Bram Moolenaardf177f62005-02-22 08:39:57 +00003727 case CMD_debug:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003728 case CMD_folddoclosed:
3729 case CMD_folddoopen:
3730 case CMD_hide:
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003731 case CMD_keepalt:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003732 case CMD_keepjumps:
3733 case CMD_keepmarks:
Bram Moolenaara939e432013-11-09 05:30:26 +01003734 case CMD_keeppatterns:
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003735 case CMD_ldo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003736 case CMD_leftabove:
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003737 case CMD_lfdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003738 case CMD_lockmarks:
Bram Moolenaar5803ae62014-03-23 16:04:02 +01003739 case CMD_noautocmd:
3740 case CMD_noswapfile:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003741 case CMD_rightbelow:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003742 case CMD_sandbox:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003743 case CMD_silent:
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003744 case CMD_tab:
Bram Moolenaar70baa402013-06-16 16:14:03 +02003745 case CMD_tabdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003746 case CMD_topleft:
3747 case CMD_verbose:
3748 case CMD_vertical:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003749 case CMD_windo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003750 return arg;
3751
Bram Moolenaar7069bf12017-01-07 20:39:53 +01003752 case CMD_filter:
3753 if (*arg != NUL)
3754 arg = skip_vimgrep_pat(arg, NULL, NULL);
3755 if (arg == NULL || *arg == NUL)
3756 {
3757 xp->xp_context = EXPAND_NOTHING;
3758 return NULL;
3759 }
3760 return skipwhite(arg);
3761
Bram Moolenaar4f688582007-07-24 12:34:30 +00003762#ifdef FEAT_CMDL_COMPL
3763# ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar071d4272004-06-13 20:20:40 +00003764 case CMD_match:
3765 if (*arg == NUL || !ends_excmd(*arg))
3766 {
Bram Moolenaar4f688582007-07-24 12:34:30 +00003767 /* also complete "None" */
3768 set_context_in_echohl_cmd(xp, arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003769 arg = skipwhite(skiptowhite(arg));
3770 if (*arg != NUL)
3771 {
3772 xp->xp_context = EXPAND_NOTHING;
3773 arg = skip_regexp(arg + 1, *arg, p_magic, NULL);
3774 }
3775 }
3776 return find_nextcmd(arg);
Bram Moolenaar4f688582007-07-24 12:34:30 +00003777# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003778
Bram Moolenaar071d4272004-06-13 20:20:40 +00003779/*
3780 * All completion for the +cmdline_compl feature goes here.
3781 */
3782
Bram Moolenaar071d4272004-06-13 20:20:40 +00003783 case CMD_command:
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003784 return set_context_in_user_cmd(xp, arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003785
3786 case CMD_delcommand:
3787 xp->xp_context = EXPAND_USER_COMMANDS;
3788 xp->xp_pattern = arg;
3789 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003790
3791 case CMD_global:
3792 case CMD_vglobal:
3793 delim = *arg; /* get the delimiter */
3794 if (delim)
3795 ++arg; /* skip delimiter if there is one */
3796
3797 while (arg[0] != NUL && arg[0] != delim)
3798 {
3799 if (arg[0] == '\\' && arg[1] != NUL)
3800 ++arg;
3801 ++arg;
3802 }
3803 if (arg[0] != NUL)
3804 return arg + 1;
3805 break;
3806 case CMD_and:
3807 case CMD_substitute:
3808 delim = *arg;
3809 if (delim)
3810 {
3811 /* skip "from" part */
3812 ++arg;
3813 arg = skip_regexp(arg, delim, p_magic, NULL);
3814 }
3815 /* skip "to" part */
3816 while (arg[0] != NUL && arg[0] != delim)
3817 {
3818 if (arg[0] == '\\' && arg[1] != NUL)
3819 ++arg;
3820 ++arg;
3821 }
3822 if (arg[0] != NUL) /* skip delimiter */
3823 ++arg;
3824 while (arg[0] && vim_strchr((char_u *)"|\"#", arg[0]) == NULL)
3825 ++arg;
3826 if (arg[0] != NUL)
3827 return arg;
3828 break;
3829 case CMD_isearch:
3830 case CMD_dsearch:
3831 case CMD_ilist:
3832 case CMD_dlist:
3833 case CMD_ijump:
3834 case CMD_psearch:
3835 case CMD_djump:
3836 case CMD_isplit:
3837 case CMD_dsplit:
3838 arg = skipwhite(skipdigits(arg)); /* skip count */
3839 if (*arg == '/') /* Match regexp, not just whole words */
3840 {
3841 for (++arg; *arg && *arg != '/'; arg++)
3842 if (*arg == '\\' && arg[1] != NUL)
3843 arg++;
3844 if (*arg)
3845 {
3846 arg = skipwhite(arg + 1);
3847
3848 /* Check for trailing illegal characters */
3849 if (*arg && vim_strchr((char_u *)"|\"\n", *arg) == NULL)
3850 xp->xp_context = EXPAND_NOTHING;
3851 else
3852 return arg;
3853 }
3854 }
3855 break;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003856
Bram Moolenaar071d4272004-06-13 20:20:40 +00003857 case CMD_autocmd:
3858 return set_context_in_autocmd(xp, arg, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003859 case CMD_doautocmd:
Bram Moolenaar73a9d7b2008-11-06 16:16:44 +00003860 case CMD_doautoall:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003861 return set_context_in_autocmd(xp, arg, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003862 case CMD_set:
3863 set_context_in_set_cmd(xp, arg, 0);
3864 break;
3865 case CMD_setglobal:
3866 set_context_in_set_cmd(xp, arg, OPT_GLOBAL);
3867 break;
3868 case CMD_setlocal:
3869 set_context_in_set_cmd(xp, arg, OPT_LOCAL);
3870 break;
3871 case CMD_tag:
3872 case CMD_stag:
3873 case CMD_ptag:
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00003874 case CMD_ltag:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003875 case CMD_tselect:
3876 case CMD_stselect:
3877 case CMD_ptselect:
3878 case CMD_tjump:
3879 case CMD_stjump:
3880 case CMD_ptjump:
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00003881 if (*p_wop != NUL)
3882 xp->xp_context = EXPAND_TAGS_LISTFILES;
3883 else
3884 xp->xp_context = EXPAND_TAGS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003885 xp->xp_pattern = arg;
3886 break;
3887 case CMD_augroup:
3888 xp->xp_context = EXPAND_AUGROUP;
3889 xp->xp_pattern = arg;
3890 break;
3891#ifdef FEAT_SYN_HL
3892 case CMD_syntax:
3893 set_context_in_syntax_cmd(xp, arg);
3894 break;
3895#endif
3896#ifdef FEAT_EVAL
3897 case CMD_let:
3898 case CMD_if:
3899 case CMD_elseif:
3900 case CMD_while:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00003901 case CMD_for:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003902 case CMD_echo:
3903 case CMD_echon:
3904 case CMD_execute:
3905 case CMD_echomsg:
3906 case CMD_echoerr:
3907 case CMD_call:
3908 case CMD_return:
Bram Moolenaar2b2207b2017-01-22 16:46:56 +01003909 case CMD_cexpr:
3910 case CMD_caddexpr:
3911 case CMD_cgetexpr:
3912 case CMD_lexpr:
3913 case CMD_laddexpr:
3914 case CMD_lgetexpr:
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003915 set_context_for_expression(xp, arg, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003916 break;
3917
3918 case CMD_unlet:
3919 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
3920 arg = xp->xp_pattern + 1;
Bram Moolenaar19834012018-06-12 17:03:39 +02003921
Bram Moolenaar071d4272004-06-13 20:20:40 +00003922 xp->xp_context = EXPAND_USER_VARS;
3923 xp->xp_pattern = arg;
Bram Moolenaar19834012018-06-12 17:03:39 +02003924
3925 if (*xp->xp_pattern == '$')
3926 {
3927 xp->xp_context = EXPAND_ENV_VARS;
3928 ++xp->xp_pattern;
3929 }
3930
Bram Moolenaar071d4272004-06-13 20:20:40 +00003931 break;
3932
3933 case CMD_function:
3934 case CMD_delfunction:
3935 xp->xp_context = EXPAND_USER_FUNC;
3936 xp->xp_pattern = arg;
3937 break;
3938
3939 case CMD_echohl:
Bram Moolenaar4f688582007-07-24 12:34:30 +00003940 set_context_in_echohl_cmd(xp, arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003941 break;
3942#endif
3943 case CMD_highlight:
3944 set_context_in_highlight_cmd(xp, arg);
3945 break;
Bram Moolenaarf4580d82009-03-18 11:52:53 +00003946#ifdef FEAT_CSCOPE
3947 case CMD_cscope:
Bram Moolenaar7bfef802009-04-22 14:25:01 +00003948 case CMD_lcscope:
3949 case CMD_scscope:
3950 set_context_in_cscope_cmd(xp, arg, ea.cmdidx);
Bram Moolenaarf4580d82009-03-18 11:52:53 +00003951 break;
3952#endif
Bram Moolenaar3c65e312009-04-29 16:47:23 +00003953#ifdef FEAT_SIGNS
3954 case CMD_sign:
3955 set_context_in_sign_cmd(xp, arg);
3956 break;
3957#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003958 case CMD_bdelete:
3959 case CMD_bwipeout:
3960 case CMD_bunload:
3961 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
3962 arg = xp->xp_pattern + 1;
Bram Moolenaar2f40d122017-10-24 21:49:36 +02003963 /* FALLTHROUGH */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003964 case CMD_buffer:
3965 case CMD_sbuffer:
3966 case CMD_checktime:
3967 xp->xp_context = EXPAND_BUFFERS;
3968 xp->xp_pattern = arg;
3969 break;
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003970
Bram Moolenaar071d4272004-06-13 20:20:40 +00003971 case CMD_USER:
3972 case CMD_USER_BUF:
3973 if (compl != EXPAND_NOTHING)
3974 {
Bram Moolenaar8071cb22019-07-12 17:58:01 +02003975 // EX_XFILE: file names are handled above
3976 if (!(ea.argt & EX_XFILE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003977 {
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003978#ifdef FEAT_MENU
Bram Moolenaar071d4272004-06-13 20:20:40 +00003979 if (compl == EXPAND_MENUS)
3980 return set_context_in_menu_cmd(xp, cmd, arg, forceit);
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003981#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003982 if (compl == EXPAND_COMMANDS)
3983 return arg;
3984 if (compl == EXPAND_MAPPINGS)
3985 return set_context_in_map_cmd(xp, (char_u *)"map",
3986 arg, forceit, FALSE, FALSE, CMD_map);
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003987 // Find start of last argument.
Bram Moolenaar848f8762012-07-25 17:22:23 +02003988 p = arg;
3989 while (*p)
3990 {
3991 if (*p == ' ')
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003992 // argument starts after a space
Bram Moolenaar848f8762012-07-25 17:22:23 +02003993 arg = p + 1;
Bram Moolenaara07c8312012-07-27 21:12:07 +02003994 else if (*p == '\\' && *(p + 1) != NUL)
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003995 ++p; // skip over escaped character
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003996 MB_PTR_ADV(p);
Bram Moolenaar848f8762012-07-25 17:22:23 +02003997 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003998 xp->xp_pattern = arg;
3999 }
4000 xp->xp_context = compl;
4001 }
4002 break;
Bram Moolenaarac9fb182019-04-27 13:04:13 +02004003
Bram Moolenaar071d4272004-06-13 20:20:40 +00004004 case CMD_map: case CMD_noremap:
4005 case CMD_nmap: case CMD_nnoremap:
4006 case CMD_vmap: case CMD_vnoremap:
4007 case CMD_omap: case CMD_onoremap:
4008 case CMD_imap: case CMD_inoremap:
4009 case CMD_cmap: case CMD_cnoremap:
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004010 case CMD_lmap: case CMD_lnoremap:
Bram Moolenaar09bb33d2013-05-07 05:18:20 +02004011 case CMD_smap: case CMD_snoremap:
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +02004012 case CMD_tmap: case CMD_tnoremap:
Bram Moolenaar09bb33d2013-05-07 05:18:20 +02004013 case CMD_xmap: case CMD_xnoremap:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004014 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004015 FALSE, FALSE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004016 case CMD_unmap:
4017 case CMD_nunmap:
4018 case CMD_vunmap:
4019 case CMD_ounmap:
4020 case CMD_iunmap:
4021 case CMD_cunmap:
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004022 case CMD_lunmap:
Bram Moolenaar09bb33d2013-05-07 05:18:20 +02004023 case CMD_sunmap:
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +02004024 case CMD_tunmap:
Bram Moolenaar09bb33d2013-05-07 05:18:20 +02004025 case CMD_xunmap:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004026 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004027 FALSE, TRUE, ea.cmdidx);
Bram Moolenaarcae92dc2017-08-06 15:22:15 +02004028 case CMD_mapclear:
4029 case CMD_nmapclear:
4030 case CMD_vmapclear:
4031 case CMD_omapclear:
4032 case CMD_imapclear:
4033 case CMD_cmapclear:
4034 case CMD_lmapclear:
4035 case CMD_smapclear:
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +02004036 case CMD_tmapclear:
Bram Moolenaarcae92dc2017-08-06 15:22:15 +02004037 case CMD_xmapclear:
4038 xp->xp_context = EXPAND_MAPCLEAR;
4039 xp->xp_pattern = arg;
4040 break;
4041
Bram Moolenaar071d4272004-06-13 20:20:40 +00004042 case CMD_abbreviate: case CMD_noreabbrev:
4043 case CMD_cabbrev: case CMD_cnoreabbrev:
4044 case CMD_iabbrev: case CMD_inoreabbrev:
4045 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004046 TRUE, FALSE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004047 case CMD_unabbreviate:
4048 case CMD_cunabbrev:
4049 case CMD_iunabbrev:
4050 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004051 TRUE, TRUE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004052#ifdef FEAT_MENU
4053 case CMD_menu: case CMD_noremenu: case CMD_unmenu:
4054 case CMD_amenu: case CMD_anoremenu: case CMD_aunmenu:
4055 case CMD_nmenu: case CMD_nnoremenu: case CMD_nunmenu:
4056 case CMD_vmenu: case CMD_vnoremenu: case CMD_vunmenu:
4057 case CMD_omenu: case CMD_onoremenu: case CMD_ounmenu:
4058 case CMD_imenu: case CMD_inoremenu: case CMD_iunmenu:
4059 case CMD_cmenu: case CMD_cnoremenu: case CMD_cunmenu:
Bram Moolenaar4c5d8152018-10-19 22:36:53 +02004060 case CMD_tlmenu: case CMD_tlnoremenu: case CMD_tlunmenu:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004061 case CMD_tmenu: case CMD_tunmenu:
4062 case CMD_popup: case CMD_tearoff: case CMD_emenu:
4063 return set_context_in_menu_cmd(xp, cmd, arg, forceit);
4064#endif
4065
4066 case CMD_colorscheme:
4067 xp->xp_context = EXPAND_COLORS;
4068 xp->xp_pattern = arg;
4069 break;
4070
4071 case CMD_compiler:
4072 xp->xp_context = EXPAND_COMPILER;
4073 xp->xp_pattern = arg;
4074 break;
4075
Bram Moolenaar883f5d02010-06-21 06:24:34 +02004076 case CMD_ownsyntax:
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02004077 xp->xp_context = EXPAND_OWNSYNTAX;
4078 xp->xp_pattern = arg;
4079 break;
4080
4081 case CMD_setfiletype:
Bram Moolenaar883f5d02010-06-21 06:24:34 +02004082 xp->xp_context = EXPAND_FILETYPE;
4083 xp->xp_pattern = arg;
4084 break;
4085
Bram Moolenaar35ca0e72016-03-05 17:41:49 +01004086 case CMD_packadd:
4087 xp->xp_context = EXPAND_PACKADD;
4088 xp->xp_pattern = arg;
4089 break;
4090
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01004091#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004092 case CMD_language:
Bram Moolenaara660dc82011-05-25 12:51:22 +02004093 p = skiptowhite(arg);
4094 if (*p == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004095 {
4096 xp->xp_context = EXPAND_LANGUAGE;
4097 xp->xp_pattern = arg;
4098 }
4099 else
Bram Moolenaara660dc82011-05-25 12:51:22 +02004100 {
4101 if ( STRNCMP(arg, "messages", p - arg) == 0
4102 || STRNCMP(arg, "ctype", p - arg) == 0
4103 || STRNCMP(arg, "time", p - arg) == 0)
4104 {
4105 xp->xp_context = EXPAND_LOCALES;
4106 xp->xp_pattern = skipwhite(p);
4107 }
4108 else
4109 xp->xp_context = EXPAND_NOTHING;
4110 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004111 break;
4112#endif
Bram Moolenaarf86f26c2010-02-03 15:14:22 +01004113#if defined(FEAT_PROFILE)
4114 case CMD_profile:
4115 set_context_in_profile_cmd(xp, arg);
4116 break;
4117#endif
Bram Moolenaar42b4dda2010-03-02 15:56:05 +01004118 case CMD_behave:
4119 xp->xp_context = EXPAND_BEHAVE;
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02004120 xp->xp_pattern = arg;
Bram Moolenaar42b4dda2010-03-02 15:56:05 +01004121 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004122
Bram Moolenaar9e507ca2016-10-15 15:39:39 +02004123 case CMD_messages:
4124 xp->xp_context = EXPAND_MESSAGES;
4125 xp->xp_pattern = arg;
4126 break;
4127
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02004128 case CMD_history:
4129 xp->xp_context = EXPAND_HISTORY;
4130 xp->xp_pattern = arg;
4131 break;
Bram Moolenaarcd9c4622013-06-08 15:24:48 +02004132#if defined(FEAT_PROFILE)
4133 case CMD_syntime:
4134 xp->xp_context = EXPAND_SYNTIME;
4135 xp->xp_pattern = arg;
4136 break;
4137#endif
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02004138
Bram Moolenaarcd43eff2018-03-29 15:55:38 +02004139 case CMD_argdelete:
4140 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
4141 arg = xp->xp_pattern + 1;
4142 xp->xp_context = EXPAND_ARGLIST;
4143 xp->xp_pattern = arg;
4144 break;
4145
Bram Moolenaar071d4272004-06-13 20:20:40 +00004146#endif /* FEAT_CMDL_COMPL */
4147
4148 default:
4149 break;
4150 }
4151 return NULL;
4152}
4153
4154/*
Bram Moolenaaree8415b2018-08-10 23:13:12 +02004155 * Skip a range specifier of the form: addr [,addr] [;addr] ..
Bram Moolenaar071d4272004-06-13 20:20:40 +00004156 *
4157 * Backslashed delimiters after / or ? will be skipped, and commands will
4158 * not be expanded between /'s and ?'s or after "'".
4159 *
Bram Moolenaardf177f62005-02-22 08:39:57 +00004160 * Also skip white space and ":" characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004161 * Returns the "cmd" pointer advanced to beyond the range.
4162 */
4163 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004164skip_range(
4165 char_u *cmd,
4166 int *ctx) /* pointer to xp_context or NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004167{
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004168 unsigned delim;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004169
Bram Moolenaarcbf20fb2017-02-03 21:19:04 +01004170 while (vim_strchr((char_u *)" \t0123456789.$%'/?-+,;\\", *cmd) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004171 {
Bram Moolenaarcbf20fb2017-02-03 21:19:04 +01004172 if (*cmd == '\\')
4173 {
4174 if (cmd[1] == '?' || cmd[1] == '/' || cmd[1] == '&')
4175 ++cmd;
4176 else
4177 break;
4178 }
4179 else if (*cmd == '\'')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004180 {
4181 if (*++cmd == NUL && ctx != NULL)
4182 *ctx = EXPAND_NOTHING;
4183 }
4184 else if (*cmd == '/' || *cmd == '?')
4185 {
4186 delim = *cmd++;
4187 while (*cmd != NUL && *cmd != delim)
4188 if (*cmd++ == '\\' && *cmd != NUL)
4189 ++cmd;
4190 if (*cmd == NUL && ctx != NULL)
4191 *ctx = EXPAND_NOTHING;
4192 }
4193 if (*cmd != NUL)
4194 ++cmd;
4195 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00004196
4197 /* Skip ":" and white space. */
4198 while (*cmd == ':')
4199 cmd = skipwhite(cmd + 1);
4200
Bram Moolenaar071d4272004-06-13 20:20:40 +00004201 return cmd;
4202}
4203
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004204 static void
4205addr_error(cmd_addr_T addr_type)
4206{
4207 if (addr_type == ADDR_NONE)
4208 emsg(_(e_norange));
4209 else
4210 emsg(_(e_invrange));
4211}
4212
Bram Moolenaar071d4272004-06-13 20:20:40 +00004213/*
Bram Moolenaar198cb662018-09-06 21:44:17 +02004214 * Get a single EX address.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004215 *
4216 * Set ptr to the next character after the part that was interpreted.
4217 * Set ptr to NULL when an error is encountered.
Bram Moolenaar198cb662018-09-06 21:44:17 +02004218 * This may set the last used search pattern.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004219 *
4220 * Return MAXLNUM when no Ex address was found.
4221 */
4222 static linenr_T
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004223get_address(
4224 exarg_T *eap UNUSED,
4225 char_u **ptr,
Bram Moolenaarb7316892019-05-01 18:08:42 +02004226 cmd_addr_T addr_type_arg,
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02004227 int skip, // only skip the address, don't use it
4228 int silent, // no errors or side effects
4229 int to_other_file, // flag: may jump to other file
4230 int address_count UNUSED) // 1 for first address, >1 after comma
Bram Moolenaar071d4272004-06-13 20:20:40 +00004231{
Bram Moolenaarb7316892019-05-01 18:08:42 +02004232 cmd_addr_T addr_type = addr_type_arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004233 int c;
4234 int i;
4235 long n;
4236 char_u *cmd;
4237 pos_T pos;
4238 pos_T *fp;
4239 linenr_T lnum;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004240 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004241
4242 cmd = skipwhite(*ptr);
4243 lnum = MAXLNUM;
4244 do
4245 {
4246 switch (*cmd)
4247 {
4248 case '.': /* '.' - Cursor position */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004249 ++cmd;
4250 switch (addr_type)
4251 {
4252 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004253 case ADDR_OTHER:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004254 lnum = curwin->w_cursor.lnum;
4255 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004256 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004257 lnum = CURRENT_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004258 break;
4259 case ADDR_ARGUMENTS:
4260 lnum = curwin->w_arg_idx + 1;
4261 break;
4262 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004263 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004264 lnum = curbuf->b_fnum;
4265 break;
4266 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004267 lnum = CURRENT_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004268 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02004269 case ADDR_NONE:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004270 case ADDR_TABS_RELATIVE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02004271 case ADDR_UNSIGNED:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004272 addr_error(addr_type);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004273 cmd = NULL;
4274 goto error;
4275 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004276 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004277#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02004278 lnum = qf_get_cur_idx(eap);
4279#endif
4280 break;
4281 case ADDR_QUICKFIX_VALID:
4282#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004283 lnum = qf_get_cur_valid_idx(eap);
Bram Moolenaare906c502015-09-09 21:10:39 +02004284#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004285 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004286 }
4287 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004288
4289 case '$': /* '$' - last line */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004290 ++cmd;
4291 switch (addr_type)
4292 {
4293 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004294 case ADDR_OTHER:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004295 lnum = curbuf->b_ml.ml_line_count;
4296 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004297 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004298 lnum = LAST_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004299 break;
4300 case ADDR_ARGUMENTS:
4301 lnum = ARGCOUNT;
4302 break;
4303 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004304 buf = lastbuf;
4305 while (buf->b_ml.ml_mfp == NULL)
4306 {
4307 if (buf->b_prev == NULL)
4308 break;
4309 buf = buf->b_prev;
4310 }
4311 lnum = buf->b_fnum;
4312 break;
4313 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004314 lnum = lastbuf->b_fnum;
4315 break;
4316 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004317 lnum = LAST_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004318 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02004319 case ADDR_NONE:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004320 case ADDR_TABS_RELATIVE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02004321 case ADDR_UNSIGNED:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004322 addr_error(addr_type);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004323 cmd = NULL;
4324 goto error;
4325 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004326 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004327#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004328 lnum = qf_get_size(eap);
4329 if (lnum == 0)
4330 lnum = 1;
Bram Moolenaare906c502015-09-09 21:10:39 +02004331#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004332 break;
Bram Moolenaar25190db2019-05-04 15:05:28 +02004333 case ADDR_QUICKFIX_VALID:
4334#ifdef FEAT_QUICKFIX
4335 lnum = qf_get_valid_size(eap);
4336 if (lnum == 0)
4337 lnum = 1;
4338#endif
4339 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004340 }
4341 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004342
4343 case '\'': /* ''' - mark */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004344 if (*++cmd == NUL)
4345 {
4346 cmd = NULL;
4347 goto error;
4348 }
4349 if (addr_type != ADDR_LINES)
4350 {
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004351 addr_error(addr_type);
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004352 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004353 goto error;
4354 }
4355 if (skip)
4356 ++cmd;
4357 else
4358 {
4359 /* Only accept a mark in another file when it is
4360 * used by itself: ":'M". */
4361 fp = getmark(*cmd, to_other_file && cmd[1] == NUL);
4362 ++cmd;
4363 if (fp == (pos_T *)-1)
4364 /* Jumped to another file. */
4365 lnum = curwin->w_cursor.lnum;
4366 else
4367 {
4368 if (check_mark(fp) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004369 {
4370 cmd = NULL;
4371 goto error;
4372 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004373 lnum = fp->lnum;
4374 }
4375 }
4376 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004377
4378 case '/':
4379 case '?': /* '/' or '?' - search */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004380 c = *cmd++;
4381 if (addr_type != ADDR_LINES)
4382 {
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004383 addr_error(addr_type);
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004384 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004385 goto error;
4386 }
4387 if (skip) /* skip "/pat/" */
4388 {
4389 cmd = skip_regexp(cmd, c, (int)p_magic, NULL);
4390 if (*cmd == c)
4391 ++cmd;
4392 }
4393 else
4394 {
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02004395 int flags;
4396
4397 pos = curwin->w_cursor; // save curwin->w_cursor
4398
4399 // When '/' or '?' follows another address, start from
4400 // there.
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004401 if (lnum != MAXLNUM)
4402 curwin->w_cursor.lnum = lnum;
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02004403
4404 // Start a forward search at the end of the line (unless
4405 // before the first line).
4406 // Start a backward search at the start of the line.
4407 // This makes sure we never match in the current
4408 // line, and can match anywhere in the
4409 // next/previous line.
Bram Moolenaar8ada6aa2017-12-19 21:23:21 +01004410 if (c == '/' && curwin->w_cursor.lnum > 0)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004411 curwin->w_cursor.col = MAXCOL;
4412 else
4413 curwin->w_cursor.col = 0;
4414 searchcmdlen = 0;
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02004415 flags = silent ? 0 : SEARCH_HIS | SEARCH_MSG;
4416 if (!do_search(NULL, c, cmd, 1L, flags, NULL, NULL))
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004417 {
4418 curwin->w_cursor = pos;
4419 cmd = NULL;
4420 goto error;
4421 }
4422 lnum = curwin->w_cursor.lnum;
4423 curwin->w_cursor = pos;
4424 /* adjust command string pointer */
4425 cmd += searchcmdlen;
4426 }
4427 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004428
4429 case '\\': /* "\?", "\/" or "\&", repeat search */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004430 ++cmd;
4431 if (addr_type != ADDR_LINES)
4432 {
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004433 addr_error(addr_type);
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004434 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004435 goto error;
4436 }
4437 if (*cmd == '&')
4438 i = RE_SUBST;
4439 else if (*cmd == '?' || *cmd == '/')
4440 i = RE_SEARCH;
4441 else
4442 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004443 emsg(_(e_backslash));
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004444 cmd = NULL;
4445 goto error;
4446 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004447
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004448 if (!skip)
4449 {
4450 /*
4451 * When search follows another address, start from
4452 * there.
4453 */
4454 if (lnum != MAXLNUM)
4455 pos.lnum = lnum;
4456 else
4457 pos.lnum = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004458
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004459 /*
4460 * Start the search just like for the above
4461 * do_search().
4462 */
4463 if (*cmd != '?')
4464 pos.col = MAXCOL;
4465 else
4466 pos.col = 0;
Bram Moolenaarbd8539a2015-08-11 18:53:03 +02004467 pos.coladd = 0;
Bram Moolenaar5d24a222018-12-23 19:10:09 +01004468 if (searchit(curwin, curbuf, &pos, NULL,
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004469 *cmd == '?' ? BACKWARD : FORWARD,
4470 (char_u *)"", 1L, SEARCH_MSG,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02004471 i, (linenr_T)0, NULL, NULL) != FAIL)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004472 lnum = pos.lnum;
4473 else
4474 {
4475 cmd = NULL;
4476 goto error;
4477 }
4478 }
4479 ++cmd;
4480 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004481
4482 default:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004483 if (VIM_ISDIGIT(*cmd)) /* absolute line number */
4484 lnum = getdigits(&cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004485 }
4486
4487 for (;;)
4488 {
4489 cmd = skipwhite(cmd);
4490 if (*cmd != '-' && *cmd != '+' && !VIM_ISDIGIT(*cmd))
4491 break;
4492
4493 if (lnum == MAXLNUM)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004494 {
4495 switch (addr_type)
4496 {
4497 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004498 case ADDR_OTHER:
4499 // "+1" is same as ".+1"
Bram Moolenaarf240e182014-11-27 18:33:02 +01004500 lnum = curwin->w_cursor.lnum;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004501 break;
4502 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004503 lnum = CURRENT_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004504 break;
4505 case ADDR_ARGUMENTS:
4506 lnum = curwin->w_arg_idx + 1;
4507 break;
4508 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004509 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004510 lnum = curbuf->b_fnum;
4511 break;
4512 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004513 lnum = CURRENT_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004514 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004515 case ADDR_TABS_RELATIVE:
4516 lnum = 1;
4517 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004518 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004519#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02004520 lnum = qf_get_cur_idx(eap);
4521#endif
4522 break;
4523 case ADDR_QUICKFIX_VALID:
4524#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004525 lnum = qf_get_cur_valid_idx(eap);
Bram Moolenaare906c502015-09-09 21:10:39 +02004526#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004527 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02004528 case ADDR_NONE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02004529 case ADDR_UNSIGNED:
4530 lnum = 0;
Bram Moolenaarb7316892019-05-01 18:08:42 +02004531 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004532 }
4533 }
4534
Bram Moolenaar071d4272004-06-13 20:20:40 +00004535 if (VIM_ISDIGIT(*cmd))
4536 i = '+'; /* "number" is same as "+number" */
4537 else
4538 i = *cmd++;
4539 if (!VIM_ISDIGIT(*cmd)) /* '+' is '+1', but '+0' is not '+1' */
4540 n = 1;
4541 else
4542 n = getdigits(&cmd);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004543
4544 if (addr_type == ADDR_TABS_RELATIVE)
4545 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004546 emsg(_(e_invrange));
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004547 cmd = NULL;
4548 goto error;
4549 }
4550 else if (addr_type == ADDR_LOADED_BUFFERS
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004551 || addr_type == ADDR_BUFFERS)
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004552 lnum = compute_buffer_local_count(
4553 addr_type, lnum, (i == '-') ? -1 * n : n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004554 else
Bram Moolenaarded27822017-01-02 14:27:34 +01004555 {
4556#ifdef FEAT_FOLDING
4557 /* Relative line addressing, need to adjust for folded lines
4558 * now, but only do it after the first address. */
4559 if (addr_type == ADDR_LINES && (i == '-' || i == '+')
4560 && address_count >= 2)
4561 (void)hasFolding(lnum, NULL, &lnum);
4562#endif
4563 if (i == '-')
4564 lnum -= n;
4565 else
4566 lnum += n;
4567 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004568 }
4569 } while (*cmd == '/' || *cmd == '?');
4570
4571error:
4572 *ptr = cmd;
4573 return lnum;
4574}
4575
4576/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00004577 * Get flags from an Ex command argument.
4578 */
4579 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004580get_flags(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00004581{
4582 while (vim_strchr((char_u *)"lp#", *eap->arg) != NULL)
4583 {
4584 if (*eap->arg == 'l')
4585 eap->flags |= EXFLAG_LIST;
4586 else if (*eap->arg == 'p')
4587 eap->flags |= EXFLAG_PRINT;
4588 else
4589 eap->flags |= EXFLAG_NR;
4590 eap->arg = skipwhite(eap->arg + 1);
4591 }
4592}
4593
4594/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004595 * Function called for command which is Not Implemented. NI!
4596 */
4597 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004598ex_ni(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004599{
4600 if (!eap->skip)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004601 eap->errmsg = N_("E319: Sorry, the command is not available in this version");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004602}
4603
Bram Moolenaar7bb75552007-07-16 18:39:49 +00004604#ifdef HAVE_EX_SCRIPT_NI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004605/*
4606 * Function called for script command which is Not Implemented. NI!
4607 * Skips over ":perl <<EOF" constructs.
4608 */
4609 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004610ex_script_ni(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004611{
4612 if (!eap->skip)
4613 ex_ni(eap);
4614 else
4615 vim_free(script_get(eap, eap->arg));
4616}
4617#endif
4618
4619/*
4620 * Check range in Ex command for validity.
4621 * Return NULL when valid, error message when invalid.
4622 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004623 static char *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004624invalid_range(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004625{
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004626 buf_T *buf;
Bram Moolenaar25190db2019-05-04 15:05:28 +02004627
Bram Moolenaar071d4272004-06-13 20:20:40 +00004628 if ( eap->line1 < 0
4629 || eap->line2 < 0
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004630 || eap->line1 > eap->line2)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004631 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004632
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004633 if (eap->argt & EX_RANGE)
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004634 {
Bram Moolenaarb7316892019-05-01 18:08:42 +02004635 switch (eap->addr_type)
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004636 {
4637 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004638 if (eap->line2 > curbuf->b_ml.ml_line_count
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004639#ifdef FEAT_DIFF
4640 + (eap->cmdidx == CMD_diffget)
4641#endif
4642 )
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004643 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004644 break;
4645 case ADDR_ARGUMENTS:
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004646 /* add 1 if ARGCOUNT is 0 */
4647 if (eap->line2 > ARGCOUNT + (!ARGCOUNT))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004648 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004649 break;
4650 case ADDR_BUFFERS:
4651 if (eap->line1 < firstbuf->b_fnum
4652 || eap->line2 > lastbuf->b_fnum)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004653 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004654 break;
4655 case ADDR_LOADED_BUFFERS:
4656 buf = firstbuf;
4657 while (buf->b_ml.ml_mfp == NULL)
4658 {
4659 if (buf->b_next == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004660 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004661 buf = buf->b_next;
4662 }
4663 if (eap->line1 < buf->b_fnum)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004664 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004665 buf = lastbuf;
4666 while (buf->b_ml.ml_mfp == NULL)
4667 {
4668 if (buf->b_prev == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004669 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004670 buf = buf->b_prev;
4671 }
4672 if (eap->line2 > buf->b_fnum)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004673 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004674 break;
4675 case ADDR_WINDOWS:
Bram Moolenaar8be63882015-01-14 11:25:05 +01004676 if (eap->line2 > LAST_WIN_NR)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004677 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004678 break;
4679 case ADDR_TABS:
4680 if (eap->line2 > LAST_TAB_NR)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004681 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004682 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004683 case ADDR_TABS_RELATIVE:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004684 case ADDR_OTHER:
4685 // Any range is OK.
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004686 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004687 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004688#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02004689 // No error for value that is too big, will use the last entry.
4690 if (eap->line2 <= 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004691 return _(e_invrange);
Bram Moolenaare906c502015-09-09 21:10:39 +02004692#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004693 break;
Bram Moolenaar25190db2019-05-04 15:05:28 +02004694 case ADDR_QUICKFIX_VALID:
4695#ifdef FEAT_QUICKFIX
4696 if ((eap->line2 != 1 && eap->line2 > qf_get_valid_size(eap))
4697 || eap->line2 < 0)
4698 return _(e_invrange);
4699#endif
4700 break;
4701 case ADDR_UNSIGNED:
4702 if (eap->line2 < 0)
4703 return _(e_invrange);
4704 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02004705 case ADDR_NONE:
4706 // Will give an error elsewhere.
4707 break;
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004708 }
4709 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004710 return NULL;
4711}
4712
4713/*
4714 * Correct the range for zero line number, if required.
4715 */
4716 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004717correct_range(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004718{
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004719 if (!(eap->argt & EX_ZEROR)) // zero in range not allowed
Bram Moolenaar071d4272004-06-13 20:20:40 +00004720 {
4721 if (eap->line1 == 0)
4722 eap->line1 = 1;
4723 if (eap->line2 == 0)
4724 eap->line2 = 1;
4725 }
4726}
4727
Bram Moolenaar748bf032005-02-02 23:04:36 +00004728#ifdef FEAT_QUICKFIX
Bram Moolenaar748bf032005-02-02 23:04:36 +00004729/*
4730 * For a ":vimgrep" or ":vimgrepadd" command return a pointer past the
4731 * pattern. Otherwise return eap->arg.
4732 */
4733 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004734skip_grep_pat(exarg_T *eap)
Bram Moolenaar748bf032005-02-02 23:04:36 +00004735{
4736 char_u *p = eap->arg;
4737
Bram Moolenaara37420f2006-02-04 22:37:47 +00004738 if (*p != NUL && (eap->cmdidx == CMD_vimgrep || eap->cmdidx == CMD_lvimgrep
4739 || eap->cmdidx == CMD_vimgrepadd
4740 || eap->cmdidx == CMD_lvimgrepadd
4741 || grep_internal(eap->cmdidx)))
Bram Moolenaar748bf032005-02-02 23:04:36 +00004742 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00004743 p = skip_vimgrep_pat(p, NULL, NULL);
Bram Moolenaar748bf032005-02-02 23:04:36 +00004744 if (p == NULL)
4745 p = eap->arg;
Bram Moolenaar748bf032005-02-02 23:04:36 +00004746 }
4747 return p;
4748}
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004749
4750/*
4751 * For the ":make" and ":grep" commands insert the 'makeprg'/'grepprg' option
4752 * in the command line, so that things like % get expanded.
4753 */
4754 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004755replace_makeprg(exarg_T *eap, char_u *p, char_u **cmdlinep)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004756{
4757 char_u *new_cmdline;
4758 char_u *program;
4759 char_u *pos;
4760 char_u *ptr;
4761 int len;
4762 int i;
4763
4764 /*
4765 * Don't do it when ":vimgrep" is used for ":grep".
4766 */
Bram Moolenaara37420f2006-02-04 22:37:47 +00004767 if ((eap->cmdidx == CMD_make || eap->cmdidx == CMD_lmake
4768 || eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4769 || eap->cmdidx == CMD_grepadd
4770 || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004771 && !grep_internal(eap->cmdidx))
4772 {
Bram Moolenaara37420f2006-02-04 22:37:47 +00004773 if (eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4774 || eap->cmdidx == CMD_grepadd || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004775 {
4776 if (*curbuf->b_p_gp == NUL)
4777 program = p_gp;
4778 else
4779 program = curbuf->b_p_gp;
4780 }
4781 else
4782 {
4783 if (*curbuf->b_p_mp == NUL)
4784 program = p_mp;
4785 else
4786 program = curbuf->b_p_mp;
4787 }
4788
4789 p = skipwhite(p);
4790
4791 if ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4792 {
4793 /* replace $* by given arguments */
4794 i = 1;
4795 while ((pos = (char_u *)strstr((char *)pos + 2, "$*")) != NULL)
4796 ++i;
4797 len = (int)STRLEN(p);
Bram Moolenaar51e14382019-05-25 20:21:28 +02004798 new_cmdline = alloc(STRLEN(program) + i * (len - 2) + 1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004799 if (new_cmdline == NULL)
4800 return NULL; /* out of memory */
4801 ptr = new_cmdline;
4802 while ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4803 {
4804 i = (int)(pos - program);
4805 STRNCPY(ptr, program, i);
4806 STRCPY(ptr += i, p);
4807 ptr += len;
4808 program = pos + 2;
4809 }
4810 STRCPY(ptr, program);
4811 }
4812 else
4813 {
Bram Moolenaar51e14382019-05-25 20:21:28 +02004814 new_cmdline = alloc(STRLEN(program) + STRLEN(p) + 2);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004815 if (new_cmdline == NULL)
4816 return NULL; /* out of memory */
4817 STRCPY(new_cmdline, program);
4818 STRCAT(new_cmdline, " ");
4819 STRCAT(new_cmdline, p);
4820 }
4821 msg_make(p);
4822
4823 /* 'eap->cmd' is not set here, because it is not used at CMD_make */
4824 vim_free(*cmdlinep);
4825 *cmdlinep = new_cmdline;
4826 p = new_cmdline;
4827 }
4828 return p;
4829}
Bram Moolenaar748bf032005-02-02 23:04:36 +00004830#endif
4831
Bram Moolenaar071d4272004-06-13 20:20:40 +00004832/*
4833 * Expand file name in Ex command argument.
Bram Moolenaar0abb4272019-06-15 16:06:00 +02004834 * When an error is detected, "errormsgp" is set to a non-NULL pointer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004835 * Return FAIL for failure, OK otherwise.
4836 */
4837 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004838expand_filename(
4839 exarg_T *eap,
4840 char_u **cmdlinep,
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004841 char **errormsgp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004842{
4843 int has_wildcards; /* need to expand wildcards */
4844 char_u *repl;
4845 int srclen;
4846 char_u *p;
4847 int n;
Bram Moolenaar63b92542007-03-27 14:57:09 +00004848 int escaped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004849
Bram Moolenaar748bf032005-02-02 23:04:36 +00004850#ifdef FEAT_QUICKFIX
4851 /* Skip a regexp pattern for ":vimgrep[add] pat file..." */
4852 p = skip_grep_pat(eap);
4853#else
4854 p = eap->arg;
4855#endif
4856
Bram Moolenaar071d4272004-06-13 20:20:40 +00004857 /*
4858 * Decide to expand wildcards *before* replacing '%', '#', etc. If
4859 * the file name contains a wildcard it should not cause expanding.
4860 * (it will be expanded anyway if there is a wildcard before replacing).
4861 */
Bram Moolenaar748bf032005-02-02 23:04:36 +00004862 has_wildcards = mch_has_wildcard(p);
4863 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004864 {
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004865#ifdef FEAT_EVAL
4866 /* Skip over `=expr`, wildcards in it are not expanded. */
4867 if (p[0] == '`' && p[1] == '=')
4868 {
4869 p += 2;
4870 (void)skip_expr(&p);
4871 if (*p == '`')
4872 ++p;
4873 continue;
4874 }
4875#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004876 /*
4877 * Quick check if this cannot be the start of a special string.
4878 * Also removes backslash before '%', '#' and '<'.
4879 */
4880 if (vim_strchr((char_u *)"%#<", *p) == NULL)
4881 {
4882 ++p;
4883 continue;
4884 }
4885
4886 /*
4887 * Try to find a match at this position.
4888 */
Bram Moolenaar63b92542007-03-27 14:57:09 +00004889 repl = eval_vars(p, eap->arg, &srclen, &(eap->do_ecmd_lnum),
4890 errormsgp, &escaped);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004891 if (*errormsgp != NULL) /* error detected */
4892 return FAIL;
4893 if (repl == NULL) /* no match found */
4894 {
4895 p += srclen;
4896 continue;
4897 }
4898
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00004899 /* Wildcards won't be expanded below, the replacement is taken
4900 * literally. But do expand "~/file", "~user/file" and "$HOME/file". */
4901 if (vim_strchr(repl, '$') != NULL || vim_strchr(repl, '~') != NULL)
4902 {
4903 char_u *l = repl;
4904
4905 repl = expand_env_save(repl);
4906 vim_free(l);
4907 }
4908
Bram Moolenaar63b92542007-03-27 14:57:09 +00004909 /* Need to escape white space et al. with a backslash.
4910 * Don't do this for:
4911 * - replacement that already has been escaped: "##"
4912 * - shell commands (may have to use quotes instead).
4913 * - non-unix systems when there is a single argument (spaces don't
4914 * separate arguments then).
4915 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004916 if (!eap->usefilter
Bram Moolenaar63b92542007-03-27 14:57:09 +00004917 && !escaped
Bram Moolenaar071d4272004-06-13 20:20:40 +00004918 && eap->cmdidx != CMD_bang
Bram Moolenaar071d4272004-06-13 20:20:40 +00004919 && eap->cmdidx != CMD_grep
4920 && eap->cmdidx != CMD_grepadd
Bram Moolenaarbf15b8d2017-06-04 20:43:48 +02004921 && eap->cmdidx != CMD_hardcopy
Bram Moolenaar67883b42017-07-27 22:57:00 +02004922 && eap->cmdidx != CMD_lgrep
4923 && eap->cmdidx != CMD_lgrepadd
4924 && eap->cmdidx != CMD_lmake
4925 && eap->cmdidx != CMD_make
4926 && eap->cmdidx != CMD_terminal
Bram Moolenaar071d4272004-06-13 20:20:40 +00004927#ifndef UNIX
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004928 && !(eap->argt & EX_NOSPC)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004929#endif
4930 )
4931 {
4932 char_u *l;
4933#ifdef BACKSLASH_IN_FILENAME
4934 /* Don't escape a backslash here, because rem_backslash() doesn't
4935 * remove it later. */
4936 static char_u *nobslash = (char_u *)" \t\"|";
4937# define ESCAPE_CHARS nobslash
4938#else
4939# define ESCAPE_CHARS escape_chars
4940#endif
4941
4942 for (l = repl; *l; ++l)
4943 if (vim_strchr(ESCAPE_CHARS, *l) != NULL)
4944 {
4945 l = vim_strsave_escaped(repl, ESCAPE_CHARS);
4946 if (l != NULL)
4947 {
4948 vim_free(repl);
4949 repl = l;
4950 }
4951 break;
4952 }
4953 }
4954
4955 /* For a shell command a '!' must be escaped. */
Bram Moolenaar67883b42017-07-27 22:57:00 +02004956 if ((eap->usefilter || eap->cmdidx == CMD_bang
4957 || eap->cmdidx == CMD_terminal)
Bram Moolenaar31b7d382014-04-01 18:54:48 +02004958 && vim_strpbrk(repl, (char_u *)"!") != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004959 {
4960 char_u *l;
4961
Bram Moolenaar31b7d382014-04-01 18:54:48 +02004962 l = vim_strsave_escaped(repl, (char_u *)"!");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004963 if (l != NULL)
4964 {
4965 vim_free(repl);
4966 repl = l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004967 }
4968 }
4969
4970 p = repl_cmdline(eap, p, srclen, repl, cmdlinep);
4971 vim_free(repl);
4972 if (p == NULL)
4973 return FAIL;
4974 }
4975
4976 /*
4977 * One file argument: Expand wildcards.
4978 * Don't do this with ":r !command" or ":w !command".
4979 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004980 if ((eap->argt & EX_NOSPC) && !eap->usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004981 {
4982 /*
4983 * May do this twice:
4984 * 1. Replace environment variables.
4985 * 2. Replace any other wildcards, remove backslashes.
4986 */
4987 for (n = 1; n <= 2; ++n)
4988 {
4989 if (n == 2)
4990 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004991 /*
4992 * Halve the number of backslashes (this is Vi compatible).
4993 * For Unix and OS/2, when wildcards are expanded, this is
4994 * done by ExpandOne() below.
4995 */
Bram Moolenaare7fedb62015-12-31 19:07:19 +01004996#if defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004997 if (!has_wildcards)
4998#endif
4999 backslash_halve(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005000 }
5001
5002 if (has_wildcards)
5003 {
5004 if (n == 1)
5005 {
5006 /*
5007 * First loop: May expand environment variables. This
5008 * can be done much faster with expand_env() than with
5009 * something else (e.g., calling a shell).
5010 * After expanding environment variables, check again
5011 * if there are still wildcards present.
5012 */
5013 if (vim_strchr(eap->arg, '$') != NULL
5014 || vim_strchr(eap->arg, '~') != NULL)
5015 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +00005016 expand_env_esc(eap->arg, NameBuff, MAXPATHL,
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00005017 TRUE, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005018 has_wildcards = mch_has_wildcard(NameBuff);
5019 p = NameBuff;
5020 }
5021 else
5022 p = NULL;
5023 }
5024 else /* n == 2 */
5025 {
5026 expand_T xpc;
Bram Moolenaar94950a92010-12-02 16:01:29 +01005027 int options = WILD_LIST_NOTFOUND|WILD_ADD_SLASH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005028
5029 ExpandInit(&xpc);
5030 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar94950a92010-12-02 16:01:29 +01005031 if (p_wic)
5032 options += WILD_ICASE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005033 p = ExpandOne(&xpc, eap->arg, NULL,
Bram Moolenaar94950a92010-12-02 16:01:29 +01005034 options, WILD_EXPAND_FREE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005035 if (p == NULL)
5036 return FAIL;
5037 }
5038 if (p != NULL)
5039 {
5040 (void)repl_cmdline(eap, eap->arg, (int)STRLEN(eap->arg),
5041 p, cmdlinep);
5042 if (n == 2) /* p came from ExpandOne() */
5043 vim_free(p);
5044 }
5045 }
5046 }
5047 }
5048 return OK;
5049}
5050
5051/*
5052 * Replace part of the command line, keeping eap->cmd, eap->arg and
5053 * eap->nextcmd correct.
5054 * "src" points to the part that is to be replaced, of length "srclen".
5055 * "repl" is the replacement string.
5056 * Returns a pointer to the character after the replaced string.
5057 * Returns NULL for failure.
5058 */
5059 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005060repl_cmdline(
5061 exarg_T *eap,
5062 char_u *src,
5063 int srclen,
5064 char_u *repl,
5065 char_u **cmdlinep)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005066{
5067 int len;
5068 int i;
5069 char_u *new_cmdline;
5070
5071 /*
5072 * The new command line is build in new_cmdline[].
5073 * First allocate it.
5074 * Careful: a "+cmd" argument may have been NUL terminated.
5075 */
5076 len = (int)STRLEN(repl);
5077 i = (int)(src - *cmdlinep) + (int)STRLEN(src + srclen) + len + 3;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005078 if (eap->nextcmd != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005079 i += (int)STRLEN(eap->nextcmd);/* add space for next command */
Bram Moolenaar964b3742019-05-24 18:54:09 +02005080 if ((new_cmdline = alloc(i)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005081 return NULL; /* out of memory! */
5082
5083 /*
5084 * Copy the stuff before the expanded part.
5085 * Copy the expanded stuff.
5086 * Copy what came after the expanded part.
5087 * Copy the next commands, if there are any.
5088 */
5089 i = (int)(src - *cmdlinep); /* length of part before match */
5090 mch_memmove(new_cmdline, *cmdlinep, (size_t)i);
Bram Moolenaara3ffd9c2005-07-21 21:03:15 +00005091
Bram Moolenaar071d4272004-06-13 20:20:40 +00005092 mch_memmove(new_cmdline + i, repl, (size_t)len);
5093 i += len; /* remember the end of the string */
5094 STRCPY(new_cmdline + i, src + srclen);
5095 src = new_cmdline + i; /* remember where to continue */
5096
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005097 if (eap->nextcmd != NULL) /* append next command */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005098 {
5099 i = (int)STRLEN(new_cmdline) + 1;
5100 STRCPY(new_cmdline + i, eap->nextcmd);
5101 eap->nextcmd = new_cmdline + i;
5102 }
5103 eap->cmd = new_cmdline + (eap->cmd - *cmdlinep);
5104 eap->arg = new_cmdline + (eap->arg - *cmdlinep);
5105 if (eap->do_ecmd_cmd != NULL && eap->do_ecmd_cmd != dollar_command)
5106 eap->do_ecmd_cmd = new_cmdline + (eap->do_ecmd_cmd - *cmdlinep);
5107 vim_free(*cmdlinep);
5108 *cmdlinep = new_cmdline;
5109
5110 return src;
5111}
5112
5113/*
5114 * Check for '|' to separate commands and '"' to start comments.
5115 */
5116 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005117separate_nextcmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005118{
5119 char_u *p;
5120
Bram Moolenaar86b68352004-12-27 21:59:20 +00005121#ifdef FEAT_QUICKFIX
Bram Moolenaar748bf032005-02-02 23:04:36 +00005122 p = skip_grep_pat(eap);
5123#else
5124 p = eap->arg;
Bram Moolenaar86b68352004-12-27 21:59:20 +00005125#endif
5126
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005127 for ( ; *p; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005128 {
5129 if (*p == Ctrl_V)
5130 {
Bram Moolenaar8071cb22019-07-12 17:58:01 +02005131 if (eap->argt & (EX_CTRLV | EX_XFILE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005132 ++p; /* skip CTRL-V and next char */
5133 else
Bram Moolenaarb0db5692007-08-14 20:54:49 +00005134 /* remove CTRL-V and skip next char */
Bram Moolenaara7241f52008-06-24 20:39:31 +00005135 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005136 if (*p == NUL) /* stop at NUL after CTRL-V */
5137 break;
5138 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005139
5140#ifdef FEAT_EVAL
5141 /* Skip over `=expr` when wildcards are expanded. */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02005142 else if (p[0] == '`' && p[1] == '=' && (eap->argt & EX_XFILE))
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005143 {
5144 p += 2;
5145 (void)skip_expr(&p);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005146 }
5147#endif
5148
Bram Moolenaar071d4272004-06-13 20:20:40 +00005149 /* Check for '"': start of comment or '|': next command */
5150 /* :@" and :*" do not start a comment!
5151 * :redir @" doesn't either. */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02005152 else if ((*p == '"' && !(eap->argt & EX_NOTRLCOM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005153 && ((eap->cmdidx != CMD_at && eap->cmdidx != CMD_star)
5154 || p != eap->arg)
5155 && (eap->cmdidx != CMD_redir
5156 || p != eap->arg + 1 || p[-1] != '@'))
5157 || *p == '|' || *p == '\n')
5158 {
5159 /*
Bram Moolenaar8071cb22019-07-12 17:58:01 +02005160 * We remove the '\' before the '|', unless EX_CTRLV is used
Bram Moolenaar071d4272004-06-13 20:20:40 +00005161 * AND 'b' is present in 'cpoptions'.
5162 */
5163 if ((vim_strchr(p_cpo, CPO_BAR) == NULL
Bram Moolenaar8071cb22019-07-12 17:58:01 +02005164 || !(eap->argt & EX_CTRLV)) && *(p - 1) == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005165 {
Bram Moolenaara7241f52008-06-24 20:39:31 +00005166 STRMOVE(p - 1, p); /* remove the '\' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005167 --p;
5168 }
5169 else
5170 {
5171 eap->nextcmd = check_nextcmd(p);
5172 *p = NUL;
5173 break;
5174 }
5175 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005176 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00005177
Bram Moolenaar8071cb22019-07-12 17:58:01 +02005178 if (!(eap->argt & EX_NOTRLCOM)) /* remove trailing spaces */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005179 del_trailing_spaces(eap->arg);
5180}
5181
5182/*
5183 * get + command from ex argument
5184 */
5185 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005186getargcmd(char_u **argp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005187{
5188 char_u *arg = *argp;
5189 char_u *command = NULL;
5190
5191 if (*arg == '+') /* +[command] */
5192 {
5193 ++arg;
Bram Moolenaar3e451592014-04-02 14:22:05 +02005194 if (vim_isspace(*arg) || *arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005195 command = dollar_command;
5196 else
5197 {
5198 command = arg;
5199 arg = skip_cmd_arg(command, TRUE);
5200 if (*arg != NUL)
5201 *arg++ = NUL; /* terminate command with NUL */
5202 }
5203
5204 arg = skipwhite(arg); /* skip over spaces */
5205 *argp = arg;
5206 }
5207 return command;
5208}
5209
5210/*
5211 * Find end of "+command" argument. Skip over "\ " and "\\".
5212 */
5213 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005214skip_cmd_arg(
5215 char_u *p,
5216 int rembs) /* TRUE to halve the number of backslashes */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005217{
5218 while (*p && !vim_isspace(*p))
5219 {
5220 if (*p == '\\' && p[1] != NUL)
5221 {
5222 if (rembs)
Bram Moolenaara7241f52008-06-24 20:39:31 +00005223 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005224 else
5225 ++p;
5226 }
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005227 MB_PTR_ADV(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005228 }
5229 return p;
5230}
5231
Bram Moolenaar333b80a2018-04-04 22:57:29 +02005232 int
5233get_bad_opt(char_u *p, exarg_T *eap)
5234{
5235 if (STRICMP(p, "keep") == 0)
5236 eap->bad_char = BAD_KEEP;
5237 else if (STRICMP(p, "drop") == 0)
5238 eap->bad_char = BAD_DROP;
5239 else if (MB_BYTE2LEN(*p) == 1 && p[1] == NUL)
5240 eap->bad_char = *p;
Bram Moolenaar75808492018-06-12 12:39:41 +02005241 else
5242 return FAIL;
5243 return OK;
Bram Moolenaar333b80a2018-04-04 22:57:29 +02005244}
5245
Bram Moolenaar071d4272004-06-13 20:20:40 +00005246/*
5247 * Get "++opt=arg" argument.
5248 * Return FAIL or OK.
5249 */
5250 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005251getargopt(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005252{
5253 char_u *arg = eap->arg + 2;
5254 int *pp = NULL;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02005255 int bad_char_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005256 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005257
5258 /* ":edit ++[no]bin[ary] file" */
5259 if (STRNCMP(arg, "bin", 3) == 0 || STRNCMP(arg, "nobin", 5) == 0)
5260 {
5261 if (*arg == 'n')
5262 {
5263 arg += 2;
5264 eap->force_bin = FORCE_NOBIN;
5265 }
5266 else
5267 eap->force_bin = FORCE_BIN;
5268 if (!checkforcmd(&arg, "binary", 3))
5269 return FAIL;
5270 eap->arg = skipwhite(arg);
5271 return OK;
5272 }
5273
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005274 /* ":read ++edit file" */
5275 if (STRNCMP(arg, "edit", 4) == 0)
5276 {
5277 eap->read_edit = TRUE;
5278 eap->arg = skipwhite(arg + 4);
5279 return OK;
5280 }
5281
Bram Moolenaar071d4272004-06-13 20:20:40 +00005282 if (STRNCMP(arg, "ff", 2) == 0)
5283 {
5284 arg += 2;
5285 pp = &eap->force_ff;
5286 }
5287 else if (STRNCMP(arg, "fileformat", 10) == 0)
5288 {
5289 arg += 10;
5290 pp = &eap->force_ff;
5291 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005292 else if (STRNCMP(arg, "enc", 3) == 0)
5293 {
Bram Moolenaarb38e9ab2011-12-14 14:49:45 +01005294 if (STRNCMP(arg, "encoding", 8) == 0)
5295 arg += 8;
5296 else
5297 arg += 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005298 pp = &eap->force_enc;
5299 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005300 else if (STRNCMP(arg, "bad", 3) == 0)
5301 {
5302 arg += 3;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02005303 pp = &bad_char_idx;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005304 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005305
5306 if (pp == NULL || *arg != '=')
5307 return FAIL;
5308
5309 ++arg;
5310 *pp = (int)(arg - eap->cmd);
5311 arg = skip_cmd_arg(arg, FALSE);
5312 eap->arg = skipwhite(arg);
5313 *arg = NUL;
5314
Bram Moolenaar071d4272004-06-13 20:20:40 +00005315 if (pp == &eap->force_ff)
5316 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005317 if (check_ff_value(eap->cmd + eap->force_ff) == FAIL)
5318 return FAIL;
Bram Moolenaar333b80a2018-04-04 22:57:29 +02005319 eap->force_ff = eap->cmd[eap->force_ff];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005320 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005321 else if (pp == &eap->force_enc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005322 {
5323 /* Make 'fileencoding' lower case. */
5324 for (p = eap->cmd + eap->force_enc; *p != NUL; ++p)
5325 *p = TOLOWER_ASC(*p);
5326 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005327 else
5328 {
5329 /* Check ++bad= argument. Must be a single-byte character, "keep" or
5330 * "drop". */
Bram Moolenaar333b80a2018-04-04 22:57:29 +02005331 if (get_bad_opt(eap->cmd + bad_char_idx, eap) == FAIL)
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005332 return FAIL;
5333 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005334
5335 return OK;
5336}
5337
Bram Moolenaar071d4272004-06-13 20:20:40 +00005338 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005339ex_autocmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005340{
5341 /*
Bram Moolenaar26d4b892018-07-04 22:26:28 +02005342 * Disallow autocommands from .exrc and .vimrc in current
Bram Moolenaar071d4272004-06-13 20:20:40 +00005343 * directory for security reasons.
5344 */
5345 if (secure)
5346 {
5347 secure = 2;
5348 eap->errmsg = e_curdir;
5349 }
5350 else if (eap->cmdidx == CMD_autocmd)
5351 do_autocmd(eap->arg, eap->forceit);
5352 else
5353 do_augroup(eap->arg, eap->forceit);
5354}
5355
5356/*
5357 * ":doautocmd": Apply the automatic commands to the current buffer.
5358 */
5359 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005360ex_doautocmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005361{
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005362 char_u *arg = eap->arg;
5363 int call_do_modelines = check_nomodeline(&arg);
Bram Moolenaar1610d052016-06-09 22:53:01 +02005364 int did_aucmd;
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005365
Bram Moolenaar1610d052016-06-09 22:53:01 +02005366 (void)do_doautocmd(arg, TRUE, &did_aucmd);
5367 /* Only when there is no <nomodeline>. */
5368 if (call_do_modelines && did_aucmd)
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005369 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005370}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005371
Bram Moolenaar071d4272004-06-13 20:20:40 +00005372/*
5373 * :[N]bunload[!] [N] [bufname] unload buffer
5374 * :[N]bdelete[!] [N] [bufname] delete buffer from buffer list
5375 * :[N]bwipeout[!] [N] [bufname] delete buffer really
5376 */
5377 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005378ex_bunload(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005379{
Bram Moolenaar8cdbd5b2019-06-16 15:50:45 +02005380 if (ERROR_IF_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02005381 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005382 eap->errmsg = do_bufdel(
5383 eap->cmdidx == CMD_bdelete ? DOBUF_DEL
5384 : eap->cmdidx == CMD_bwipeout ? DOBUF_WIPE
5385 : DOBUF_UNLOAD, eap->arg,
5386 eap->addr_count, (int)eap->line1, (int)eap->line2, eap->forceit);
5387}
5388
5389/*
5390 * :[N]buffer [N] to buffer N
5391 * :[N]sbuffer [N] to buffer N
5392 */
5393 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005394ex_buffer(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005395{
Bram Moolenaar8cdbd5b2019-06-16 15:50:45 +02005396 if (ERROR_IF_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02005397 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005398 if (*eap->arg)
5399 eap->errmsg = e_trailing;
5400 else
5401 {
5402 if (eap->addr_count == 0) /* default is current buffer */
5403 goto_buffer(eap, DOBUF_CURRENT, FORWARD, 0);
5404 else
5405 goto_buffer(eap, DOBUF_FIRST, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005406 if (eap->do_ecmd_cmd != NULL)
5407 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005408 }
5409}
5410
5411/*
5412 * :[N]bmodified [N] to next mod. buffer
5413 * :[N]sbmodified [N] to next mod. buffer
5414 */
5415 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005416ex_bmodified(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005417{
5418 goto_buffer(eap, DOBUF_MOD, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005419 if (eap->do_ecmd_cmd != NULL)
5420 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005421}
5422
5423/*
5424 * :[N]bnext [N] to next buffer
5425 * :[N]sbnext [N] split and to next buffer
5426 */
5427 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005428ex_bnext(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005429{
5430 goto_buffer(eap, DOBUF_CURRENT, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005431 if (eap->do_ecmd_cmd != NULL)
5432 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005433}
5434
5435/*
5436 * :[N]bNext [N] to previous buffer
5437 * :[N]bprevious [N] to previous buffer
5438 * :[N]sbNext [N] split and to previous buffer
5439 * :[N]sbprevious [N] split and to previous buffer
5440 */
5441 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005442ex_bprevious(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005443{
5444 goto_buffer(eap, DOBUF_CURRENT, BACKWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005445 if (eap->do_ecmd_cmd != NULL)
5446 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005447}
5448
5449/*
5450 * :brewind to first buffer
5451 * :bfirst to first buffer
5452 * :sbrewind split and to first buffer
5453 * :sbfirst split and to first buffer
5454 */
5455 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005456ex_brewind(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005457{
5458 goto_buffer(eap, DOBUF_FIRST, FORWARD, 0);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005459 if (eap->do_ecmd_cmd != NULL)
5460 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005461}
5462
5463/*
5464 * :blast to last buffer
5465 * :sblast split and to last buffer
5466 */
5467 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005468ex_blast(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005469{
5470 goto_buffer(eap, DOBUF_LAST, BACKWARD, 0);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005471 if (eap->do_ecmd_cmd != NULL)
5472 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005473}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005474
5475 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005476ends_excmd(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005477{
5478 return (c == NUL || c == '|' || c == '"' || c == '\n');
5479}
5480
5481#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA) || defined(FEAT_EVAL) \
5482 || defined(PROTO)
5483/*
5484 * Return the next command, after the first '|' or '\n'.
5485 * Return NULL if not found.
5486 */
5487 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005488find_nextcmd(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005489{
5490 while (*p != '|' && *p != '\n')
5491 {
5492 if (*p == NUL)
5493 return NULL;
5494 ++p;
5495 }
5496 return (p + 1);
5497}
5498#endif
5499
5500/*
Bram Moolenaar2256c992016-11-15 21:17:07 +01005501 * Check if *p is a separator between Ex commands, skipping over white space.
5502 * Return NULL if it isn't, the following character if it is.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005503 */
5504 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005505check_nextcmd(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005506{
Bram Moolenaar2256c992016-11-15 21:17:07 +01005507 char_u *s = skipwhite(p);
5508
5509 if (*s == '|' || *s == '\n')
5510 return (s + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005511 else
5512 return NULL;
5513}
5514
5515/*
5516 * - if there are more files to edit
5517 * - and this is the last window
5518 * - and forceit not used
5519 * - and not repeated twice on a row
5520 * return FAIL and give error message if 'message' TRUE
5521 * return OK otherwise
5522 */
5523 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005524check_more(
5525 int message, /* when FALSE check only, no messages */
5526 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005527{
5528 int n = ARGCOUNT - curwin->w_arg_idx - 1;
5529
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005530 if (!forceit && only_one_window()
5531 && ARGCOUNT > 1 && !arg_had_last && n >= 0 && quitmore == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005532 {
5533 if (message)
5534 {
5535#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
5536 if ((p_confirm || cmdmod.confirm) && curbuf->b_fname != NULL)
5537 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02005538 char_u buff[DIALOG_MSG_SIZE];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005539
Bram Moolenaarda6e8912018-08-21 15:12:14 +02005540 vim_snprintf((char *)buff, DIALOG_MSG_SIZE,
5541 NGETTEXT("%d more file to edit. Quit anyway?",
5542 "%d more files to edit. Quit anyway?", n), n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005543 if (vim_dialog_yesno(VIM_QUESTION, NULL, buff, 1) == VIM_YES)
5544 return OK;
5545 return FAIL;
5546 }
5547#endif
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01005548 semsg(NGETTEXT("E173: %d more file to edit",
5549 "E173: %d more files to edit", n), n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005550 quitmore = 2; /* next try to quit is allowed */
5551 }
5552 return FAIL;
5553 }
5554 return OK;
5555}
5556
Bram Moolenaarac9fb182019-04-27 13:04:13 +02005557#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005558/*
5559 * Function given to ExpandGeneric() to obtain the list of command names.
5560 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005561 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005562get_command_name(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005563{
5564 if (idx >= (int)CMD_SIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005565 return get_user_command_name(idx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005566 return cmdnames[idx].cmd_name;
5567}
5568#endif
5569
Bram Moolenaar071d4272004-06-13 20:20:40 +00005570 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005571ex_colorscheme(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005572{
Bram Moolenaare6850792010-05-14 15:28:44 +02005573 if (*eap->arg == NUL)
5574 {
5575#ifdef FEAT_EVAL
5576 char_u *expr = vim_strsave((char_u *)"g:colors_name");
5577 char_u *p = NULL;
5578
5579 if (expr != NULL)
5580 {
5581 ++emsg_off;
5582 p = eval_to_string(expr, NULL, FALSE);
5583 --emsg_off;
5584 vim_free(expr);
5585 }
5586 if (p != NULL)
5587 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01005588 msg((char *)p);
Bram Moolenaare6850792010-05-14 15:28:44 +02005589 vim_free(p);
5590 }
5591 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01005592 msg("default");
Bram Moolenaare6850792010-05-14 15:28:44 +02005593#else
Bram Moolenaar32526b32019-01-19 17:43:09 +01005594 msg(_("unknown"));
Bram Moolenaare6850792010-05-14 15:28:44 +02005595#endif
5596 }
5597 else if (load_colors(eap->arg) == FAIL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005598 semsg(_("E185: Cannot find color scheme '%s'"), eap->arg);
Bram Moolenaarf58d81a2019-01-28 20:19:05 +01005599
5600#ifdef FEAT_VTP
5601 else if (has_vtp_working())
5602 {
5603 // background color change requires clear + redraw
5604 update_screen(CLEAR);
5605 redrawcmd();
5606 }
5607#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005608}
5609
5610 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005611ex_highlight(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005612{
5613 if (*eap->arg == NUL && eap->cmd[2] == '!')
Bram Moolenaar32526b32019-01-19 17:43:09 +01005614 msg(_("Greetings, Vim user!"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005615 do_highlight(eap->arg, eap->forceit, FALSE);
5616}
5617
5618
5619/*
5620 * Call this function if we thought we were going to exit, but we won't
5621 * (because of an error). May need to restore the terminal mode.
5622 */
5623 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005624not_exiting(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005625{
5626 exiting = FALSE;
5627 settmode(TMODE_RAW);
5628}
5629
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005630 static int
5631before_quit_autocmds(win_T *wp, int quit_all, int forceit)
5632{
5633 apply_autocmds(EVENT_QUITPRE, NULL, NULL, FALSE, wp->w_buffer);
5634
5635 /* Bail out when autocommands closed the window.
5636 * Refuse to quit when the buffer in the last window is being closed (can
5637 * only happen in autocommands). */
5638 if (!win_valid(wp)
5639 || curbuf_locked()
5640 || (wp->w_buffer->b_nwindows == 1 && wp->w_buffer->b_locked > 0))
5641 return TRUE;
5642
5643 if (quit_all || (check_more(FALSE, forceit) == OK && only_one_window()))
5644 {
5645 apply_autocmds(EVENT_EXITPRE, NULL, NULL, FALSE, curbuf);
5646 /* Refuse to quit when locked or when the buffer in the last window is
5647 * being closed (can only happen in autocommands). */
5648 if (curbuf_locked()
5649 || (curbuf->b_nwindows == 1 && curbuf->b_locked > 0))
5650 return TRUE;
5651 }
5652
5653 return FALSE;
5654}
5655
Bram Moolenaar071d4272004-06-13 20:20:40 +00005656/*
Bram Moolenaarf240e182014-11-27 18:33:02 +01005657 * ":quit": quit current window, quit Vim if the last window is closed.
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005658 * ":{nr}quit": quit window {nr}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005659 */
5660 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005661ex_quit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005662{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005663 win_T *wp;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005664
Bram Moolenaar071d4272004-06-13 20:20:40 +00005665#ifdef FEAT_CMDWIN
5666 if (cmdwin_type != 0)
5667 {
5668 cmdwin_result = Ctrl_C;
5669 return;
5670 }
5671#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005672 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005673 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005674 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005675 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005676 return;
5677 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005678 if (eap->addr_count > 0)
5679 {
Bram Moolenaarf240e182014-11-27 18:33:02 +01005680 int wnr = eap->line2;
5681
5682 for (wp = firstwin; wp->w_next != NULL; wp = wp->w_next)
5683 if (--wnr <= 0)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005684 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005685 }
5686 else
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005687 wp = curwin;
Bram Moolenaarf240e182014-11-27 18:33:02 +01005688
Bram Moolenaar87ffb5c2017-10-19 12:37:42 +02005689 /* Refuse to quit when locked. */
5690 if (curbuf_locked())
5691 return;
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005692
5693 /* Trigger QuitPre and maybe ExitPre */
5694 if (before_quit_autocmds(wp, FALSE, eap->forceit))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005695 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005696
5697#ifdef FEAT_NETBEANS_INTG
5698 netbeansForcedQuit = eap->forceit;
5699#endif
5700
5701 /*
5702 * If there are more files or windows we won't exit.
5703 */
5704 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
5705 exiting = TRUE;
Bram Moolenaarff930ca2017-10-19 17:12:10 +02005706 if ((!buf_hide(wp->w_buffer)
5707 && check_changed(wp->w_buffer, (p_awa ? CCGD_AW : 0)
Bram Moolenaar45d3b142013-11-09 03:31:51 +01005708 | (eap->forceit ? CCGD_FORCEIT : 0)
5709 | CCGD_EXCMD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005710 || check_more(TRUE, eap->forceit) == FAIL
Bram Moolenaar027387f2016-01-02 22:25:52 +01005711 || (only_one_window() && check_changed_any(eap->forceit, TRUE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005712 {
5713 not_exiting();
5714 }
5715 else
5716 {
Bram Moolenaarc7a0d322015-06-19 12:43:07 +02005717 /* quit last window
5718 * Note: only_one_window() returns true, even so a help window is
5719 * still open. In that case only quit, if no address has been
5720 * specified. Example:
5721 * :h|wincmd w|1q - don't quit
5722 * :h|wincmd w|q - quit
5723 */
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01005724 if (only_one_window() && (ONE_WINDOW || eap->addr_count == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005725 getout(0);
Bram Moolenaar2c33d7b2017-10-14 16:06:20 +02005726 not_exiting();
Bram Moolenaar4033c552017-09-16 20:54:51 +02005727#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00005728 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005729#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005730 /* close window; may free buffer */
Bram Moolenaareb44a682017-08-03 22:44:55 +02005731 win_close(wp, !buf_hide(wp->w_buffer) || eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005732 }
5733}
5734
5735/*
5736 * ":cquit".
5737 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005738 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005739ex_cquit(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005740{
5741 getout(1); /* this does not always pass on the exit code to the Manx
5742 compiler. why? */
5743}
5744
5745/*
5746 * ":qall": try to quit all windows
5747 */
5748 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005749ex_quit_all(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005750{
5751# ifdef FEAT_CMDWIN
5752 if (cmdwin_type != 0)
5753 {
5754 if (eap->forceit)
5755 cmdwin_result = K_XF1; /* ex_window() takes care of this */
5756 else
5757 cmdwin_result = K_XF2;
5758 return;
5759 }
5760# endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005761
5762 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005763 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005764 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005765 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005766 return;
5767 }
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005768
5769 if (before_quit_autocmds(curwin, TRUE, eap->forceit))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005770 return;
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005771
Bram Moolenaar071d4272004-06-13 20:20:40 +00005772 exiting = TRUE;
Bram Moolenaar027387f2016-01-02 22:25:52 +01005773 if (eap->forceit || !check_changed_any(FALSE, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005774 getout(0);
5775 not_exiting();
5776}
5777
Bram Moolenaar071d4272004-06-13 20:20:40 +00005778/*
5779 * ":close": close current window, unless it is the last one
5780 */
5781 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005782ex_close(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005783{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005784 win_T *win;
5785 int winnr = 0;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005786#ifdef FEAT_CMDWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00005787 if (cmdwin_type != 0)
Bram Moolenaar9bd1a7e2011-05-19 14:50:54 +02005788 cmdwin_result = Ctrl_C;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005789 else
Bram Moolenaar4033c552017-09-16 20:54:51 +02005790#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005791 if (!text_locked() && !curbuf_locked())
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005792 {
5793 if (eap->addr_count == 0)
5794 ex_win_close(eap->forceit, curwin, NULL);
Bram Moolenaar6d41c782018-06-06 09:11:12 +02005795 else
5796 {
Bram Moolenaar29323592016-07-24 22:04:11 +02005797 FOR_ALL_WINDOWS(win)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005798 {
5799 winnr++;
5800 if (winnr == eap->line2)
5801 break;
5802 }
5803 if (win == NULL)
5804 win = lastwin;
5805 ex_win_close(eap->forceit, win, NULL);
5806 }
5807 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005808}
5809
Bram Moolenaar4033c552017-09-16 20:54:51 +02005810#ifdef FEAT_QUICKFIX
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005811/*
5812 * ":pclose": Close any preview window.
5813 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005814 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005815ex_pclose(exarg_T *eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005816{
5817 win_T *win;
5818
Bram Moolenaar79648732019-07-18 21:43:07 +02005819 // First close any normal window.
Bram Moolenaar29323592016-07-24 22:04:11 +02005820 FOR_ALL_WINDOWS(win)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005821 if (win->w_p_pvw)
5822 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00005823 ex_win_close(eap->forceit, win, NULL);
Bram Moolenaar79648732019-07-18 21:43:07 +02005824 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005825 }
Bram Moolenaar79648732019-07-18 21:43:07 +02005826# ifdef FEAT_TEXT_PROP
5827 // Also when 'previewpopup' is empty, it might have been cleared.
5828 popup_close_preview();
5829# endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005830}
Bram Moolenaar4033c552017-09-16 20:54:51 +02005831#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005832
Bram Moolenaarf740b292006-02-16 22:11:02 +00005833/*
5834 * Close window "win" and take care of handling closing the last window for a
5835 * modified buffer.
5836 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005837 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005838ex_win_close(
5839 int forceit,
5840 win_T *win,
5841 tabpage_T *tp) /* NULL or the tab page "win" is in */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005842{
5843 int need_hide;
5844 buf_T *buf = win->w_buffer;
5845
5846 need_hide = (bufIsChanged(buf) && buf->b_nwindows <= 1);
Bram Moolenaareb44a682017-08-03 22:44:55 +02005847 if (need_hide && !buf_hide(buf) && !forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005848 {
Bram Moolenaar4033c552017-09-16 20:54:51 +02005849#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005850 if ((p_confirm || cmdmod.confirm) && p_write)
5851 {
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005852 bufref_T bufref;
5853
5854 set_bufref(&bufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005855 dialog_changed(buf, FALSE);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005856 if (bufref_valid(&bufref) && bufIsChanged(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005857 return;
5858 need_hide = FALSE;
5859 }
5860 else
Bram Moolenaar4033c552017-09-16 20:54:51 +02005861#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005862 {
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02005863 no_write_message();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005864 return;
5865 }
5866 }
5867
Bram Moolenaar4033c552017-09-16 20:54:51 +02005868#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00005869 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005870#endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00005871
Bram Moolenaar071d4272004-06-13 20:20:40 +00005872 /* free buffer when not hiding it or when it's a scratch buffer */
Bram Moolenaarf740b292006-02-16 22:11:02 +00005873 if (tp == NULL)
Bram Moolenaareb44a682017-08-03 22:44:55 +02005874 win_close(win, !need_hide && !buf_hide(buf));
Bram Moolenaarf740b292006-02-16 22:11:02 +00005875 else
Bram Moolenaareb44a682017-08-03 22:44:55 +02005876 win_close_othertab(win, !need_hide && !buf_hide(buf), tp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005877}
5878
Bram Moolenaar071d4272004-06-13 20:20:40 +00005879/*
Bram Moolenaardea25702017-01-29 15:18:10 +01005880 * Handle the argument for a tabpage related ex command.
5881 * Returns a tabpage number.
5882 * When an error is encountered then eap->errmsg is set.
5883 */
5884 static int
5885get_tabpage_arg(exarg_T *eap)
5886{
5887 int tab_number;
5888 int unaccept_arg0 = (eap->cmdidx == CMD_tabmove) ? 0 : 1;
5889
5890 if (eap->arg && *eap->arg != NUL)
5891 {
5892 char_u *p = eap->arg;
5893 char_u *p_save;
5894 int relative = 0; /* argument +N/-N means: go to N places to the
5895 * right/left relative to the current position. */
5896
5897 if (*p == '-')
5898 {
5899 relative = -1;
5900 p++;
5901 }
5902 else if (*p == '+')
5903 {
5904 relative = 1;
5905 p++;
5906 }
5907
5908 p_save = p;
5909 tab_number = getdigits(&p);
5910
5911 if (relative == 0)
5912 {
5913 if (STRCMP(p, "$") == 0)
5914 tab_number = LAST_TAB_NR;
5915 else if (p == p_save || *p_save == '-' || *p != NUL
5916 || tab_number > LAST_TAB_NR)
5917 {
5918 /* No numbers as argument. */
5919 eap->errmsg = e_invarg;
5920 goto theend;
5921 }
5922 }
5923 else
5924 {
5925 if (*p_save == NUL)
5926 tab_number = 1;
5927 else if (p == p_save || *p_save == '-' || *p != NUL
5928 || tab_number == 0)
5929 {
5930 /* No numbers as argument. */
5931 eap->errmsg = e_invarg;
5932 goto theend;
5933 }
5934 tab_number = tab_number * relative + tabpage_index(curtab);
5935 if (!unaccept_arg0 && relative == -1)
5936 --tab_number;
5937 }
5938 if (tab_number < unaccept_arg0 || tab_number > LAST_TAB_NR)
5939 eap->errmsg = e_invarg;
5940 }
5941 else if (eap->addr_count > 0)
5942 {
5943 if (unaccept_arg0 && eap->line2 == 0)
Bram Moolenaarc6251552017-01-29 21:42:20 +01005944 {
Bram Moolenaardea25702017-01-29 15:18:10 +01005945 eap->errmsg = e_invrange;
Bram Moolenaarc6251552017-01-29 21:42:20 +01005946 tab_number = 0;
5947 }
Bram Moolenaardea25702017-01-29 15:18:10 +01005948 else
5949 {
5950 tab_number = eap->line2;
Bram Moolenaar82a12462019-01-22 22:41:42 +01005951 if (!unaccept_arg0 && *skipwhite(*eap->cmdlinep) == '-')
Bram Moolenaardea25702017-01-29 15:18:10 +01005952 {
5953 --tab_number;
5954 if (tab_number < unaccept_arg0)
5955 eap->errmsg = e_invarg;
5956 }
5957 }
5958 }
5959 else
5960 {
5961 switch (eap->cmdidx)
5962 {
5963 case CMD_tabnext:
5964 tab_number = tabpage_index(curtab) + 1;
5965 if (tab_number > LAST_TAB_NR)
5966 tab_number = 1;
5967 break;
5968 case CMD_tabmove:
5969 tab_number = LAST_TAB_NR;
5970 break;
5971 default:
5972 tab_number = tabpage_index(curtab);
5973 }
5974 }
5975
5976theend:
5977 return tab_number;
5978}
5979
5980/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00005981 * ":tabclose": close current tab page, unless it is the last one.
5982 * ":tabclose N": close tab page N.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005983 */
5984 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005985ex_tabclose(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005986{
Bram Moolenaarf740b292006-02-16 22:11:02 +00005987 tabpage_T *tp;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005988 int tab_number;
Bram Moolenaarf740b292006-02-16 22:11:02 +00005989
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005990# ifdef FEAT_CMDWIN
5991 if (cmdwin_type != 0)
5992 cmdwin_result = K_IGNORE;
5993 else
5994# endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00005995 if (first_tabpage->tp_next == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005996 emsg(_("E784: Cannot close last tab page"));
Bram Moolenaarf740b292006-02-16 22:11:02 +00005997 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005998 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005999 tab_number = get_tabpage_arg(eap);
6000 if (eap->errmsg == NULL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006001 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006002 tp = find_tabpage(tab_number);
Bram Moolenaarf740b292006-02-16 22:11:02 +00006003 if (tp == NULL)
6004 {
6005 beep_flush();
6006 return;
6007 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00006008 if (tp != curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006009 {
6010 tabpage_close_other(tp, eap->forceit);
6011 return;
6012 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006013 else if (!text_locked() && !curbuf_locked())
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006014 tabpage_close(eap->forceit);
6015 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006016 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006017}
6018
6019/*
6020 * ":tabonly": close all tab pages except the current one
6021 */
6022 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006023ex_tabonly(exarg_T *eap)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006024{
6025 tabpage_T *tp;
6026 int done;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006027 int tab_number;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006028
6029# ifdef FEAT_CMDWIN
6030 if (cmdwin_type != 0)
6031 cmdwin_result = K_IGNORE;
6032 else
6033# endif
6034 if (first_tabpage->tp_next == NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01006035 msg(_("Already only one tab page"));
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006036 else
6037 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006038 tab_number = get_tabpage_arg(eap);
6039 if (eap->errmsg == NULL)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006040 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006041 goto_tabpage(tab_number);
6042 /* Repeat this up to a 1000 times, because autocommands may
6043 * mess up the lists. */
6044 for (done = 0; done < 1000; ++done)
6045 {
6046 FOR_ALL_TABPAGES(tp)
6047 if (tp->tp_topframe != topframe)
6048 {
6049 tabpage_close_other(tp, eap->forceit);
6050 /* if we failed to close it quit */
6051 if (valid_tabpage(tp))
6052 done = 1000;
6053 /* start over, "tp" is now invalid */
6054 break;
6055 }
6056 if (first_tabpage->tp_next == NULL)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006057 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006058 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006059 }
6060 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006061}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006062
6063/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00006064 * Close the current tab page.
6065 */
6066 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006067tabpage_close(int forceit)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006068{
6069 /* First close all the windows but the current one. If that worked then
6070 * close the last window in this tab, that will close it. */
Bram Moolenaar459ca562016-11-10 18:16:33 +01006071 if (!ONE_WINDOW)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006072 close_others(TRUE, forceit);
Bram Moolenaar459ca562016-11-10 18:16:33 +01006073 if (ONE_WINDOW)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006074 ex_win_close(forceit, curwin, NULL);
6075# ifdef FEAT_GUI
6076 need_mouse_correct = TRUE;
6077# endif
6078}
6079
6080/*
6081 * Close tab page "tp", which is not the current tab page.
6082 * Note that autocommands may make "tp" invalid.
Bram Moolenaar7875acc2006-09-10 13:51:17 +00006083 * Also takes care of the tab pages line disappearing when closing the
6084 * last-but-one tab page.
Bram Moolenaarf740b292006-02-16 22:11:02 +00006085 */
6086 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006087tabpage_close_other(tabpage_T *tp, int forceit)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006088{
6089 int done = 0;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006090 win_T *wp;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00006091 int h = tabline_height();
Bram Moolenaarf740b292006-02-16 22:11:02 +00006092
6093 /* Limit to 1000 windows, autocommands may add a window while we close
6094 * one. OK, so I'm paranoid... */
6095 while (++done < 1000)
6096 {
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006097 wp = tp->tp_firstwin;
6098 ex_win_close(forceit, wp, tp);
Bram Moolenaarf740b292006-02-16 22:11:02 +00006099
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006100 /* Autocommands may delete the tab page under our fingers and we may
6101 * fail to close a window with a modified buffer. */
6102 if (!valid_tabpage(tp) || tp->tp_firstwin == wp)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006103 break;
6104 }
Bram Moolenaar7875acc2006-09-10 13:51:17 +00006105
Bram Moolenaar29323592016-07-24 22:04:11 +02006106 apply_autocmds(EVENT_TABCLOSED, NULL, NULL, FALSE, curbuf);
Bram Moolenaar29323592016-07-24 22:04:11 +02006107
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006108 redraw_tabline = TRUE;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00006109 if (h != tabline_height())
6110 shell_new_rows();
Bram Moolenaarf740b292006-02-16 22:11:02 +00006111}
6112
6113/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006114 * ":only".
6115 */
6116 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006117ex_only(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006118{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01006119 win_T *wp;
6120 int wnr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006121# ifdef FEAT_GUI
6122 need_mouse_correct = TRUE;
6123# endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01006124 if (eap->addr_count > 0)
6125 {
6126 wnr = eap->line2;
6127 for (wp = firstwin; --wnr > 0; )
6128 {
6129 if (wp->w_next == NULL)
6130 break;
6131 else
6132 wp = wp->w_next;
6133 }
6134 win_goto(wp);
6135 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006136 close_others(TRUE, eap->forceit);
6137}
6138
Bram Moolenaar071d4272004-06-13 20:20:40 +00006139 static void
Bram Moolenaar5e1e6d22017-01-02 17:26:00 +01006140ex_hide(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006141{
Bram Moolenaar2256c992016-11-15 21:17:07 +01006142 /* ":hide" or ":hide | cmd": hide current window */
Bram Moolenaar2256c992016-11-15 21:17:07 +01006143 if (!eap->skip)
6144 {
Bram Moolenaar4033c552017-09-16 20:54:51 +02006145#ifdef FEAT_GUI
Bram Moolenaar2256c992016-11-15 21:17:07 +01006146 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006147#endif
Bram Moolenaar2256c992016-11-15 21:17:07 +01006148 if (eap->addr_count == 0)
6149 win_close(curwin, FALSE); /* don't free buffer */
6150 else
6151 {
6152 int winnr = 0;
6153 win_T *win;
Bram Moolenaarf240e182014-11-27 18:33:02 +01006154
Bram Moolenaar2256c992016-11-15 21:17:07 +01006155 FOR_ALL_WINDOWS(win)
6156 {
6157 winnr++;
6158 if (winnr == eap->line2)
6159 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01006160 }
Bram Moolenaar2256c992016-11-15 21:17:07 +01006161 if (win == NULL)
6162 win = lastwin;
6163 win_close(win, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006164 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006165 }
6166}
6167
6168/*
6169 * ":stop" and ":suspend": Suspend Vim.
6170 */
6171 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006172ex_stop(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006173{
6174 /*
6175 * Disallow suspending for "rvim".
6176 */
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006177 if (!check_restricted())
Bram Moolenaar071d4272004-06-13 20:20:40 +00006178 {
6179 if (!eap->forceit)
6180 autowrite_all();
6181 windgoto((int)Rows - 1, 0);
6182 out_char('\n');
6183 out_flush();
6184 stoptermcap();
6185 out_flush(); /* needed for SUN to restore xterm buffer */
6186#ifdef FEAT_TITLE
Bram Moolenaar40385db2018-08-07 22:31:44 +02006187 mch_restore_title(SAVE_RESTORE_BOTH); /* restore window titles */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006188#endif
6189 ui_suspend(); /* call machine specific function */
6190#ifdef FEAT_TITLE
6191 maketitle();
6192 resettitle(); /* force updating the title */
6193#endif
6194 starttermcap();
6195 scroll_start(); /* scroll screen before redrawing */
6196 redraw_later_clear();
6197 shell_resized(); /* may have resized window */
6198 }
6199}
6200
6201/*
Bram Moolenaar12a96de2018-03-11 14:44:18 +01006202 * ":exit", ":xit" and ":wq": Write file and quite the current window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006203 */
6204 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006205ex_exit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006206{
6207#ifdef FEAT_CMDWIN
6208 if (cmdwin_type != 0)
6209 {
6210 cmdwin_result = Ctrl_C;
6211 return;
6212 }
6213#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006214 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006215 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006216 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006217 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006218 return;
6219 }
Bram Moolenaar12a96de2018-03-11 14:44:18 +01006220
6221 if (before_quit_autocmds(curwin, FALSE, eap->forceit))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006222 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006223
6224 /*
6225 * if more files or windows we won't exit
6226 */
6227 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
6228 exiting = TRUE;
6229 if ( ((eap->cmdidx == CMD_wq
6230 || curbufIsChanged())
6231 && do_write(eap) == FAIL)
6232 || check_more(TRUE, eap->forceit) == FAIL
Bram Moolenaar027387f2016-01-02 22:25:52 +01006233 || (only_one_window() && check_changed_any(eap->forceit, FALSE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006234 {
6235 not_exiting();
6236 }
6237 else
6238 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006239 if (only_one_window()) /* quit last window, exit Vim */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006240 getout(0);
Bram Moolenaar2c33d7b2017-10-14 16:06:20 +02006241 not_exiting();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006242# ifdef FEAT_GUI
6243 need_mouse_correct = TRUE;
6244# endif
Bram Moolenaar1a4a75c2013-07-28 16:03:06 +02006245 /* Quit current window, may free the buffer. */
Bram Moolenaareb44a682017-08-03 22:44:55 +02006246 win_close(curwin, !buf_hide(curwin->w_buffer));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006247 }
6248}
6249
6250/*
6251 * ":print", ":list", ":number".
6252 */
6253 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006254ex_print(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006255{
Bram Moolenaardf177f62005-02-22 08:39:57 +00006256 if (curbuf->b_ml.ml_flags & ML_EMPTY)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006257 emsg(_(e_emptybuf));
Bram Moolenaardf177f62005-02-22 08:39:57 +00006258 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006259 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00006260 for ( ;!got_int; ui_breakcheck())
6261 {
6262 print_line(eap->line1,
6263 (eap->cmdidx == CMD_number || eap->cmdidx == CMD_pound
6264 || (eap->flags & EXFLAG_NR)),
6265 eap->cmdidx == CMD_list || (eap->flags & EXFLAG_LIST));
6266 if (++eap->line1 > eap->line2)
6267 break;
6268 out_flush(); /* show one line at a time */
6269 }
6270 setpcmark();
6271 /* put cursor at last line */
6272 curwin->w_cursor.lnum = eap->line2;
6273 beginline(BL_SOL | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006274 }
6275
Bram Moolenaar071d4272004-06-13 20:20:40 +00006276 ex_no_reprint = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006277}
6278
6279#ifdef FEAT_BYTEOFF
6280 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006281ex_goto(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006282{
6283 goto_byte(eap->line2);
6284}
6285#endif
6286
6287/*
6288 * ":shell".
6289 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006290 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006291ex_shell(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006292{
6293 do_shell(NULL, 0);
6294}
6295
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006296#if defined(HAVE_DROP_FILE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006297
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006298static int drop_busy = FALSE;
6299static int drop_filec;
6300static char_u **drop_filev = NULL;
6301static int drop_split;
6302static void (*drop_callback)(void *);
6303static void *drop_cookie;
6304
6305 static void
6306handle_drop_internal(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006307{
6308 exarg_T ea;
6309 int save_msg_scroll = msg_scroll;
6310
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006311 // Setting the argument list may cause screen updates and being called
6312 // recursively. Avoid that by setting drop_busy.
6313 drop_busy = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006314
6315 /* Check whether the current buffer is changed. If so, we will need
6316 * to split the current window or data could be lost.
6317 * We don't need to check if the 'hidden' option is set, as in this
6318 * case the buffer won't be lost.
6319 */
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006320 if (!buf_hide(curbuf) && !drop_split)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006321 {
6322 ++emsg_off;
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006323 drop_split = check_changed(curbuf, CCGD_AW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006324 --emsg_off;
6325 }
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006326 if (drop_split)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006327 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006328 if (win_split(0, 0) == FAIL)
6329 return;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02006330 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006331
6332 /* When splitting the window, create a new alist. Otherwise the
6333 * existing one is overwritten. */
6334 alist_unlink(curwin->w_alist);
6335 alist_new();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006336 }
6337
6338 /*
6339 * Set up the new argument list.
6340 */
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006341 alist_set(ALIST(curwin), drop_filec, drop_filev, FALSE, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006342
6343 /*
6344 * Move to the first file.
6345 */
6346 /* Fake up a minimal "next" command for do_argfile() */
6347 vim_memset(&ea, 0, sizeof(ea));
6348 ea.cmd = (char_u *)"next";
6349 do_argfile(&ea, 0);
6350
6351 /* do_ecmd() may set need_start_insertmode, but since we never left Insert
6352 * mode that is not needed here. */
6353 need_start_insertmode = FALSE;
6354
6355 /* Restore msg_scroll, otherwise a following command may cause scrolling
6356 * unexpectedly. The screen will be redrawn by the caller, thus
6357 * msg_scroll being set by displaying a message is irrelevant. */
6358 msg_scroll = save_msg_scroll;
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006359
6360 if (drop_callback != NULL)
6361 drop_callback(drop_cookie);
6362
6363 drop_filev = NULL;
6364 drop_busy = FALSE;
6365}
6366
6367/*
6368 * Handle a file drop. The code is here because a drop is *nearly* like an
6369 * :args command, but not quite (we have a list of exact filenames, so we
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01006370 * don't want to (a) parse a command line, or (b) expand wildcards). So the
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006371 * code is very similar to :args and hence needs access to a lot of the static
6372 * functions in this file.
6373 *
6374 * The "filev" list must have been allocated using alloc(), as should each item
6375 * in the list. This function takes over responsibility for freeing the "filev"
6376 * list.
6377 */
6378 void
6379handle_drop(
6380 int filec, // the number of files dropped
6381 char_u **filev, // the list of files dropped
6382 int split, // force splitting the window
6383 void (*callback)(void *), // to be called after setting the argument
6384 // list
6385 void *cookie) // argument for "callback" (allocated)
6386{
6387 // Cannot handle recursive drops, finish the pending one.
6388 if (drop_busy)
6389 {
6390 FreeWild(filec, filev);
6391 vim_free(cookie);
6392 return;
6393 }
6394
6395 // When calling handle_drop() more than once in a row we only use the last
6396 // one.
6397 if (drop_filev != NULL)
6398 {
6399 FreeWild(drop_filec, drop_filev);
6400 vim_free(drop_cookie);
6401 }
6402
6403 drop_filec = filec;
6404 drop_filev = filev;
6405 drop_split = split;
6406 drop_callback = callback;
6407 drop_cookie = cookie;
6408
6409 // Postpone this when:
6410 // - editing the command line
6411 // - not possible to change the current buffer
6412 // - updating the screen
6413 // As it may change buffers and window structures that are in use and cause
6414 // freed memory to be used.
6415 if (text_locked() || curbuf_locked() || updating_screen)
6416 return;
6417
6418 handle_drop_internal();
6419}
6420
6421/*
6422 * To be called when text is unlocked, curbuf is unlocked or updating_screen is
6423 * reset: Handle a postponed drop.
6424 */
6425 void
6426handle_any_postponed_drop(void)
6427{
6428 if (!drop_busy && drop_filev != NULL
6429 && !text_locked() && !curbuf_locked() && !updating_screen)
6430 handle_drop_internal();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006431}
6432#endif
6433
Bram Moolenaar071d4272004-06-13 20:20:40 +00006434/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006435 * ":preserve".
6436 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006437 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006438ex_preserve(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006439{
Bram Moolenaar4399ef42005-02-12 14:29:27 +00006440 curbuf->b_flags |= BF_PRESERVED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006441 ml_preserve(curbuf, TRUE);
6442}
6443
6444/*
6445 * ":recover".
6446 */
6447 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006448ex_recover(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006449{
6450 /* Set recoverymode right away to avoid the ATTENTION prompt. */
6451 recoverymode = TRUE;
Bram Moolenaar45d3b142013-11-09 03:31:51 +01006452 if (!check_changed(curbuf, (p_awa ? CCGD_AW : 0)
6453 | CCGD_MULTWIN
6454 | (eap->forceit ? CCGD_FORCEIT : 0)
6455 | CCGD_EXCMD)
6456
Bram Moolenaar071d4272004-06-13 20:20:40 +00006457 && (*eap->arg == NUL
6458 || setfname(curbuf, eap->arg, NULL, TRUE) == OK))
Bram Moolenaar99499b12019-05-23 21:35:48 +02006459 ml_recover(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006460 recoverymode = FALSE;
6461}
6462
6463/*
6464 * Command modifier used in a wrong way.
6465 */
6466 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006467ex_wrongmodifier(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006468{
6469 eap->errmsg = e_invcmd;
6470}
6471
Bram Moolenaar071d4272004-06-13 20:20:40 +00006472/*
6473 * :sview [+command] file split window with new file, read-only
6474 * :split [[+command] file] split window with current or new file
6475 * :vsplit [[+command] file] split window vertically with current or new file
6476 * :new [[+command] file] split window with no or new file
6477 * :vnew [[+command] file] split vertically window with no or new file
6478 * :sfind [+command] file split window with file in 'path'
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006479 *
6480 * :tabedit open new Tab page with empty window
6481 * :tabedit [+command] file open new Tab page and edit "file"
6482 * :tabnew [[+command] file] just like :tabedit
6483 * :tabfind [+command] file open new Tab page and find "file"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006484 */
6485 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006486ex_splitview(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006487{
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006488 win_T *old_curwin = curwin;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006489#if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006490 char_u *fname = NULL;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006491#endif
6492#ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00006493 int browse_flag = cmdmod.browse;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006494#endif
Bram Moolenaar39902a02018-06-21 22:10:08 +02006495 int use_tab = eap->cmdidx == CMD_tabedit
6496 || eap->cmdidx == CMD_tabfind
6497 || eap->cmdidx == CMD_tabnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006498
Bram Moolenaar8cdbd5b2019-06-16 15:50:45 +02006499 if (ERROR_IF_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02006500 return;
6501
Bram Moolenaar4033c552017-09-16 20:54:51 +02006502#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00006503 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006504#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006505
Bram Moolenaar4033c552017-09-16 20:54:51 +02006506#ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00006507 /* A ":split" in the quickfix window works like ":new". Don't want two
Bram Moolenaar05bb9532008-07-04 09:44:11 +00006508 * quickfix windows. But it's OK when doing ":tab split". */
6509 if (bt_quickfix(curbuf) && cmdmod.tab == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006510 {
6511 if (eap->cmdidx == CMD_split)
6512 eap->cmdidx = CMD_new;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006513 if (eap->cmdidx == CMD_vsplit)
6514 eap->cmdidx = CMD_vnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006515 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02006516#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006517
Bram Moolenaar4033c552017-09-16 20:54:51 +02006518#ifdef FEAT_SEARCHPATH
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006519 if (eap->cmdidx == CMD_sfind || eap->cmdidx == CMD_tabfind)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006520 {
6521 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg),
6522 FNAME_MESS, TRUE, curbuf->b_ffname);
6523 if (fname == NULL)
6524 goto theend;
6525 eap->arg = fname;
6526 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006527# ifdef FEAT_BROWSE
Bram Moolenaar4033c552017-09-16 20:54:51 +02006528 else
6529# endif
6530#endif
6531#ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00006532 if (cmdmod.browse
Bram Moolenaar071d4272004-06-13 20:20:40 +00006533 && eap->cmdidx != CMD_vnew
Bram Moolenaar071d4272004-06-13 20:20:40 +00006534 && eap->cmdidx != CMD_new)
6535 {
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006536 if (
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006537# ifdef FEAT_GUI
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006538 !gui.in_use &&
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006539# endif
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006540 au_has_group((char_u *)"FileExplorer"))
6541 {
6542 /* No browsing supported but we do have the file explorer:
6543 * Edit the directory. */
6544 if (*eap->arg == NUL || !mch_isdir(eap->arg))
6545 eap->arg = (char_u *)".";
6546 }
6547 else
6548 {
Bram Moolenaar39902a02018-06-21 22:10:08 +02006549 fname = do_browse(0, (char_u *)(use_tab
6550 ? _("Edit File in new tab page")
6551 : _("Edit File in new window")),
Bram Moolenaar071d4272004-06-13 20:20:40 +00006552 eap->arg, NULL, NULL, NULL, curbuf);
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006553 if (fname == NULL)
6554 goto theend;
6555 eap->arg = fname;
6556 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006557 }
6558 cmdmod.browse = FALSE; /* Don't browse again in do_ecmd(). */
Bram Moolenaar4033c552017-09-16 20:54:51 +02006559#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006560
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006561 /*
6562 * Either open new tab page or split the window.
6563 */
Bram Moolenaar39902a02018-06-21 22:10:08 +02006564 if (use_tab)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006565 {
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006566 if (win_new_tabpage(cmdmod.tab != 0 ? cmdmod.tab
6567 : eap->addr_count == 0 ? 0
6568 : (int)eap->line2 + 1) != FAIL)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006569 {
Bram Moolenaard2b66012008-01-09 19:30:36 +00006570 do_exedit(eap, old_curwin);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006571
6572 /* set the alternate buffer for the window we came from */
6573 if (curwin != old_curwin
6574 && win_valid(old_curwin)
6575 && old_curwin->w_buffer != curbuf
6576 && !cmdmod.keepalt)
6577 old_curwin->w_alt_fnum = curbuf->b_fnum;
6578 }
6579 }
6580 else if (win_split(eap->addr_count > 0 ? (int)eap->line2 : 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006581 *eap->cmd == 'v' ? WSP_VERT : 0) != FAIL)
6582 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006583 /* Reset 'scrollbind' when editing another file, but keep it when
6584 * doing ":split" without arguments. */
6585 if (*eap->arg != NUL
Bram Moolenaar8a3bb562018-03-04 20:14:14 +01006586# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00006587 || cmdmod.browse
Bram Moolenaar8a3bb562018-03-04 20:14:14 +01006588# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006589 )
Bram Moolenaar3368ea22010-09-21 16:56:35 +02006590 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006591 else
6592 do_check_scrollbind(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006593 do_exedit(eap, old_curwin);
6594 }
6595
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006596# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00006597 cmdmod.browse = browse_flag;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006598# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006599
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006600# if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006601theend:
6602 vim_free(fname);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006603# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006604}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006605
6606/*
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006607 * Open a new tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006608 */
6609 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006610tabpage_new(void)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006611{
6612 exarg_T ea;
6613
6614 vim_memset(&ea, 0, sizeof(ea));
6615 ea.cmdidx = CMD_tabnew;
6616 ea.cmd = (char_u *)"tabn";
6617 ea.arg = (char_u *)"";
6618 ex_splitview(&ea);
6619}
6620
6621/*
6622 * :tabnext command
6623 */
6624 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006625ex_tabnext(exarg_T *eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006626{
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006627 int tab_number;
6628
Bram Moolenaar8cdbd5b2019-06-16 15:50:45 +02006629 if (ERROR_IF_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02006630 return;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006631 switch (eap->cmdidx)
6632 {
6633 case CMD_tabfirst:
6634 case CMD_tabrewind:
6635 goto_tabpage(1);
6636 break;
6637 case CMD_tablast:
6638 goto_tabpage(9999);
6639 break;
6640 case CMD_tabprevious:
6641 case CMD_tabNext:
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006642 if (eap->arg && *eap->arg != NUL)
6643 {
6644 char_u *p = eap->arg;
6645 char_u *p_save = p;
6646
6647 tab_number = getdigits(&p);
6648 if (p == p_save || *p_save == '-' || *p != NUL
6649 || tab_number == 0)
6650 {
6651 /* No numbers as argument. */
6652 eap->errmsg = e_invarg;
6653 return;
6654 }
6655 }
6656 else
6657 {
6658 if (eap->addr_count == 0)
6659 tab_number = 1;
6660 else
6661 {
6662 tab_number = eap->line2;
6663 if (tab_number < 1)
6664 {
6665 eap->errmsg = e_invrange;
6666 return;
6667 }
6668 }
6669 }
6670 goto_tabpage(-tab_number);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006671 break;
6672 default: /* CMD_tabnext */
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006673 tab_number = get_tabpage_arg(eap);
6674 if (eap->errmsg == NULL)
6675 goto_tabpage(tab_number);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006676 break;
6677 }
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006678}
6679
6680/*
6681 * :tabmove command
6682 */
6683 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006684ex_tabmove(exarg_T *eap)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006685{
Bram Moolenaar40ce3a42015-04-21 18:08:39 +02006686 int tab_number;
Bram Moolenaar8cb8dca2012-07-06 18:27:39 +02006687
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006688 tab_number = get_tabpage_arg(eap);
6689 if (eap->errmsg == NULL)
6690 tabpage_move(tab_number);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006691}
6692
6693/*
6694 * :tabs command: List tabs and their contents.
6695 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006696 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006697ex_tabs(exarg_T *eap UNUSED)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006698{
6699 tabpage_T *tp;
6700 win_T *wp;
6701 int tabcount = 1;
6702
6703 msg_start();
6704 msg_scroll = TRUE;
6705 for (tp = first_tabpage; tp != NULL && !got_int; tp = tp->tp_next)
6706 {
6707 msg_putchar('\n');
6708 vim_snprintf((char *)IObuff, IOSIZE, _("Tab page %d"), tabcount++);
Bram Moolenaar8820b482017-03-16 17:23:31 +01006709 msg_outtrans_attr(IObuff, HL_ATTR(HLF_T));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006710 out_flush(); /* output one line at a time */
6711 ui_breakcheck();
6712
Bram Moolenaar030f0df2006-02-21 22:02:53 +00006713 if (tp == curtab)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006714 wp = firstwin;
6715 else
6716 wp = tp->tp_firstwin;
6717 for ( ; wp != NULL && !got_int; wp = wp->w_next)
6718 {
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006719 msg_putchar('\n');
6720 msg_putchar(wp == curwin ? '>' : ' ');
6721 msg_putchar(' ');
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006722 msg_putchar(bufIsChanged(wp->w_buffer) ? '+' : ' ');
6723 msg_putchar(' ');
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006724 if (buf_spname(wp->w_buffer) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02006725 vim_strncpy(IObuff, buf_spname(wp->w_buffer), IOSIZE - 1);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006726 else
6727 home_replace(wp->w_buffer, wp->w_buffer->b_fname,
6728 IObuff, IOSIZE, TRUE);
6729 msg_outtrans(IObuff);
6730 out_flush(); /* output one line at a time */
6731 ui_breakcheck();
6732 }
6733 }
6734}
6735
Bram Moolenaar071d4272004-06-13 20:20:40 +00006736/*
6737 * ":mode": Set screen mode.
6738 * If no argument given, just get the screen size and redraw.
6739 */
6740 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006741ex_mode(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006742{
6743 if (*eap->arg == NUL)
6744 shell_resized();
6745 else
6746 mch_screenmode(eap->arg);
6747}
6748
Bram Moolenaar071d4272004-06-13 20:20:40 +00006749/*
6750 * ":resize".
6751 * set, increment or decrement current window height
6752 */
6753 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006754ex_resize(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006755{
6756 int n;
6757 win_T *wp = curwin;
6758
6759 if (eap->addr_count > 0)
6760 {
6761 n = eap->line2;
6762 for (wp = firstwin; wp->w_next != NULL && --n > 0; wp = wp->w_next)
6763 ;
6764 }
6765
Bram Moolenaar44a2f922016-03-19 22:11:51 +01006766# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00006767 need_mouse_correct = TRUE;
Bram Moolenaar44a2f922016-03-19 22:11:51 +01006768# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006769 n = atol((char *)eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006770 if (cmdmod.split & WSP_VERT)
6771 {
6772 if (*eap->arg == '-' || *eap->arg == '+')
Bram Moolenaar02631462017-09-22 15:20:32 +02006773 n += curwin->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006774 else if (n == 0 && eap->arg[0] == NUL) /* default is very wide */
6775 n = 9999;
6776 win_setwidth_win((int)n, wp);
6777 }
6778 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006779 {
6780 if (*eap->arg == '-' || *eap->arg == '+')
6781 n += curwin->w_height;
Bram Moolenaar1f2903c2017-07-23 19:51:01 +02006782 else if (n == 0 && eap->arg[0] == NUL) /* default is very high */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006783 n = 9999;
6784 win_setheight_win((int)n, wp);
6785 }
6786}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006787
6788/*
6789 * ":find [+command] <file>" command.
6790 */
6791 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006792ex_find(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006793{
6794#ifdef FEAT_SEARCHPATH
6795 char_u *fname;
6796 int count;
6797
6798 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg), FNAME_MESS,
6799 TRUE, curbuf->b_ffname);
6800 if (eap->addr_count > 0)
6801 {
6802 /* Repeat finding the file "count" times. This matters when it
6803 * appears several times in the path. */
6804 count = eap->line2;
6805 while (fname != NULL && --count > 0)
6806 {
6807 vim_free(fname);
6808 fname = find_file_in_path(NULL, 0, FNAME_MESS,
6809 FALSE, curbuf->b_ffname);
6810 }
6811 }
6812
6813 if (fname != NULL)
6814 {
6815 eap->arg = fname;
6816#endif
6817 do_exedit(eap, NULL);
6818#ifdef FEAT_SEARCHPATH
6819 vim_free(fname);
6820 }
6821#endif
6822}
6823
6824/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00006825 * ":open" simulation: for now just work like ":visual".
6826 */
6827 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006828ex_open(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00006829{
6830 regmatch_T regmatch;
6831 char_u *p;
6832
6833 curwin->w_cursor.lnum = eap->line2;
6834 beginline(BL_SOL | BL_FIX);
6835 if (*eap->arg == '/')
6836 {
6837 /* ":open /pattern/": put cursor in column found with pattern */
6838 ++eap->arg;
6839 p = skip_regexp(eap->arg, '/', p_magic, NULL);
6840 *p = NUL;
6841 regmatch.regprog = vim_regcomp(eap->arg, p_magic ? RE_MAGIC : 0);
6842 if (regmatch.regprog != NULL)
6843 {
6844 regmatch.rm_ic = p_ic;
6845 p = ml_get_curline();
6846 if (vim_regexec(&regmatch, p, (colnr_T)0))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006847 curwin->w_cursor.col = (colnr_T)(regmatch.startp[0] - p);
Bram Moolenaardf177f62005-02-22 08:39:57 +00006848 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006849 emsg(_(e_nomatch));
Bram Moolenaar473de612013-06-08 18:19:48 +02006850 vim_regfree(regmatch.regprog);
Bram Moolenaardf177f62005-02-22 08:39:57 +00006851 }
6852 /* Move to the NUL, ignore any other arguments. */
6853 eap->arg += STRLEN(eap->arg);
6854 }
6855 check_cursor();
6856
6857 eap->cmdidx = CMD_visual;
6858 do_exedit(eap, NULL);
6859}
6860
6861/*
6862 * ":edit", ":badd", ":visual".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006863 */
6864 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006865ex_edit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006866{
6867 do_exedit(eap, NULL);
6868}
6869
6870/*
6871 * ":edit <file>" command and alikes.
6872 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006873 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006874do_exedit(
6875 exarg_T *eap,
6876 win_T *old_curwin) /* curwin before doing a split or NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006877{
6878 int n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006879 int need_hide;
Bram Moolenaardf177f62005-02-22 08:39:57 +00006880 int exmode_was = exmode_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006881
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02006882 if (eap->cmdidx != CMD_pedit && ERROR_IF_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02006883 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006884 /*
6885 * ":vi" command ends Ex mode.
6886 */
6887 if (exmode_active && (eap->cmdidx == CMD_visual
6888 || eap->cmdidx == CMD_view))
6889 {
6890 exmode_active = FALSE;
6891 if (*eap->arg == NUL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00006892 {
6893 /* Special case: ":global/pat/visual\NLvi-commands" */
6894 if (global_busy)
6895 {
6896 int rd = RedrawingDisabled;
6897 int nwr = no_wait_return;
6898 int ms = msg_scroll;
6899#ifdef FEAT_GUI
6900 int he = hold_gui_events;
6901#endif
6902
6903 if (eap->nextcmd != NULL)
6904 {
6905 stuffReadbuff(eap->nextcmd);
6906 eap->nextcmd = NULL;
6907 }
6908
6909 if (exmode_was != EXMODE_VIM)
6910 settmode(TMODE_RAW);
6911 RedrawingDisabled = 0;
6912 no_wait_return = 0;
6913 need_wait_return = FALSE;
6914 msg_scroll = 0;
6915#ifdef FEAT_GUI
6916 hold_gui_events = 0;
6917#endif
6918 must_redraw = CLEAR;
6919
6920 main_loop(FALSE, TRUE);
6921
6922 RedrawingDisabled = rd;
6923 no_wait_return = nwr;
6924 msg_scroll = ms;
6925#ifdef FEAT_GUI
6926 hold_gui_events = he;
6927#endif
6928 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006929 return;
Bram Moolenaardf177f62005-02-22 08:39:57 +00006930 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006931 }
6932
6933 if ((eap->cmdidx == CMD_new
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006934 || eap->cmdidx == CMD_tabnew
6935 || eap->cmdidx == CMD_tabedit
Bram Moolenaar4033c552017-09-16 20:54:51 +02006936 || eap->cmdidx == CMD_vnew) && *eap->arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006937 {
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006938 /* ":new" or ":tabnew" without argument: edit an new empty buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006939 setpcmark();
6940 (void)do_ecmd(0, NULL, NULL, eap, ECMD_ONE,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00006941 ECMD_HIDE + (eap->forceit ? ECMD_FORCEIT : 0),
6942 old_curwin == NULL ? curwin : NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006943 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02006944 else if ((eap->cmdidx != CMD_split && eap->cmdidx != CMD_vsplit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006945 || *eap->arg != NUL
6946#ifdef FEAT_BROWSE
6947 || cmdmod.browse
6948#endif
6949 )
6950 {
Bram Moolenaar5555acc2006-04-07 21:33:12 +00006951 /* Can't edit another file when "curbuf_lock" is set. Only ":edit"
6952 * can bring us here, others are stopped earlier. */
6953 if (*eap->arg != NUL && curbuf_locked())
6954 return;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006955
Bram Moolenaar071d4272004-06-13 20:20:40 +00006956 n = readonlymode;
6957 if (eap->cmdidx == CMD_view || eap->cmdidx == CMD_sview)
6958 readonlymode = TRUE;
6959 else if (eap->cmdidx == CMD_enew)
6960 readonlymode = FALSE; /* 'readonly' doesn't make sense in an
6961 empty buffer */
6962 setpcmark();
6963 if (do_ecmd(0, (eap->cmdidx == CMD_enew ? NULL : eap->arg),
6964 NULL, eap,
6965 /* ":edit" goes to first line if Vi compatible */
6966 (*eap->arg == NUL && eap->do_ecmd_lnum == 0
6967 && vim_strchr(p_cpo, CPO_GOTO1) != NULL)
6968 ? ECMD_ONE : eap->do_ecmd_lnum,
Bram Moolenaareb44a682017-08-03 22:44:55 +02006969 (buf_hide(curbuf) ? ECMD_HIDE : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006970 + (eap->forceit ? ECMD_FORCEIT : 0)
Bram Moolenaar7b449342014-03-25 13:03:48 +01006971 /* after a split we can use an existing buffer */
6972 + (old_curwin != NULL ? ECMD_OLDBUF : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006973 + (eap->cmdidx == CMD_badd ? ECMD_ADDBUF : 0 )
Bram Moolenaar701f7af2008-11-15 13:12:07 +00006974 , old_curwin == NULL ? curwin : NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006975 {
6976 /* Editing the file failed. If the window was split, close it. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006977 if (old_curwin != NULL)
6978 {
6979 need_hide = (curbufIsChanged() && curbuf->b_nwindows <= 1);
Bram Moolenaareb44a682017-08-03 22:44:55 +02006980 if (!need_hide || buf_hide(curbuf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006981 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006982#if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00006983 cleanup_T cs;
6984
6985 /* Reset the error/interrupt/exception state here so that
6986 * aborting() returns FALSE when closing a window. */
6987 enter_cleanup(&cs);
Bram Moolenaar4033c552017-09-16 20:54:51 +02006988#endif
6989#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00006990 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006991#endif
Bram Moolenaareb44a682017-08-03 22:44:55 +02006992 win_close(curwin, !need_hide && !buf_hide(curbuf));
Bram Moolenaarc0197e22004-09-13 20:26:32 +00006993
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006994#if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00006995 /* Restore the error/interrupt/exception state if not
6996 * discarded by a new aborting error, interrupt, or
6997 * uncaught exception. */
6998 leave_cleanup(&cs);
Bram Moolenaar4033c552017-09-16 20:54:51 +02006999#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007000 }
7001 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007002 }
7003 else if (readonlymode && curbuf->b_nwindows == 1)
7004 {
7005 /* When editing an already visited buffer, 'readonly' won't be set
7006 * but the previous value is kept. With ":view" and ":sview" we
7007 * want the file to be readonly, except when another window is
7008 * editing the same buffer. */
7009 curbuf->b_p_ro = TRUE;
7010 }
7011 readonlymode = n;
7012 }
7013 else
7014 {
7015 if (eap->do_ecmd_cmd != NULL)
7016 do_cmdline_cmd(eap->do_ecmd_cmd);
7017#ifdef FEAT_TITLE
7018 n = curwin->w_arg_idx_invalid;
7019#endif
7020 check_arg_idx(curwin);
7021#ifdef FEAT_TITLE
7022 if (n != curwin->w_arg_idx_invalid)
7023 maketitle();
7024#endif
7025 }
7026
Bram Moolenaar071d4272004-06-13 20:20:40 +00007027 /*
7028 * if ":split file" worked, set alternate file name in old window to new
7029 * file
7030 */
7031 if (old_curwin != NULL
7032 && *eap->arg != NUL
7033 && curwin != old_curwin
7034 && win_valid(old_curwin)
Bram Moolenaard4755bb2004-09-02 19:12:26 +00007035 && old_curwin->w_buffer != curbuf
7036 && !cmdmod.keepalt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007037 old_curwin->w_alt_fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007038
7039 ex_no_reprint = TRUE;
7040}
7041
7042#ifndef FEAT_GUI
7043/*
7044 * ":gui" and ":gvim" when there is no GUI.
7045 */
7046 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007047ex_nogui(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007048{
7049 eap->errmsg = e_nogvim;
7050}
7051#endif
7052
Bram Moolenaar4f974752019-02-17 17:44:42 +01007053#if defined(FEAT_GUI_MSWIN) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007054 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007055ex_tearoff(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007056{
7057 gui_make_tearoff(eap->arg);
7058}
7059#endif
7060
Bram Moolenaar29a2c082018-03-05 21:06:23 +01007061#if (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK) \
7062 || defined(FEAT_TERM_POPUP_MENU)) && defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007063 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007064ex_popup(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007065{
Bram Moolenaar29a2c082018-03-05 21:06:23 +01007066# if defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK)
7067 if (gui.in_use)
7068 gui_make_popup(eap->arg, eap->forceit);
7069# ifdef FEAT_TERM_POPUP_MENU
7070 else
7071# endif
7072# endif
7073# ifdef FEAT_TERM_POPUP_MENU
7074 pum_make_popup(eap->arg, eap->forceit);
7075# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007076}
7077#endif
7078
Bram Moolenaar071d4272004-06-13 20:20:40 +00007079 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007080ex_swapname(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007081{
7082 if (curbuf->b_ml.ml_mfp == NULL || curbuf->b_ml.ml_mfp->mf_fname == NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01007083 msg(_("No swap file"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007084 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01007085 msg((char *)curbuf->b_ml.ml_mfp->mf_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007086}
7087
7088/*
7089 * ":syncbind" forces all 'scrollbind' windows to have the same relative
7090 * offset.
7091 * (1998-11-02 16:21:01 R. Edward Ralston <eralston@computer.org>)
7092 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007093 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007094ex_syncbind(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007095{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007096 win_T *wp;
Bram Moolenaardedd1b02013-12-14 13:06:17 +01007097 win_T *save_curwin = curwin;
7098 buf_T *save_curbuf = curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007099 long topline;
7100 long y;
7101 linenr_T old_linenr = curwin->w_cursor.lnum;
7102
7103 setpcmark();
7104
7105 /*
7106 * determine max topline
7107 */
7108 if (curwin->w_p_scb)
7109 {
7110 topline = curwin->w_topline;
Bram Moolenaar29323592016-07-24 22:04:11 +02007111 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007112 {
7113 if (wp->w_p_scb && wp->w_buffer)
7114 {
Bram Moolenaar375e3392019-01-31 18:26:10 +01007115 y = wp->w_buffer->b_ml.ml_line_count - get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007116 if (topline > y)
7117 topline = y;
7118 }
7119 }
7120 if (topline < 1)
7121 topline = 1;
7122 }
7123 else
7124 {
7125 topline = 1;
7126 }
7127
7128
7129 /*
Bram Moolenaardedd1b02013-12-14 13:06:17 +01007130 * Set all scrollbind windows to the same topline.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007131 */
Bram Moolenaar29323592016-07-24 22:04:11 +02007132 FOR_ALL_WINDOWS(curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007133 {
7134 if (curwin->w_p_scb)
7135 {
Bram Moolenaardedd1b02013-12-14 13:06:17 +01007136 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007137 y = topline - curwin->w_topline;
7138 if (y > 0)
7139 scrollup(y, TRUE);
7140 else
7141 scrolldown(-y, TRUE);
7142 curwin->w_scbind_pos = topline;
7143 redraw_later(VALID);
7144 cursor_correct();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007145 curwin->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007146 }
7147 }
Bram Moolenaardedd1b02013-12-14 13:06:17 +01007148 curwin = save_curwin;
7149 curbuf = save_curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007150 if (curwin->w_p_scb)
7151 {
7152 did_syncbind = TRUE;
7153 checkpcmark();
7154 if (old_linenr != curwin->w_cursor.lnum)
7155 {
7156 char_u ctrl_o[2];
7157
7158 ctrl_o[0] = Ctrl_O;
7159 ctrl_o[1] = 0;
7160 ins_typebuf(ctrl_o, REMAP_NONE, 0, TRUE, FALSE);
7161 }
7162 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007163}
7164
7165
7166 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007167ex_read(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007168{
Bram Moolenaardf177f62005-02-22 08:39:57 +00007169 int i;
7170 int empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
7171 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007172
7173 if (eap->usefilter) /* :r!cmd */
7174 do_bang(1, eap, FALSE, FALSE, TRUE);
7175 else
7176 {
7177 if (u_save(eap->line2, (linenr_T)(eap->line2 + 1)) == FAIL)
7178 return;
7179
7180#ifdef FEAT_BROWSE
7181 if (cmdmod.browse)
7182 {
7183 char_u *browseFile;
7184
Bram Moolenaar7171abe2004-10-11 10:06:20 +00007185 browseFile = do_browse(0, (char_u *)_("Append File"), eap->arg,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007186 NULL, NULL, NULL, curbuf);
7187 if (browseFile != NULL)
7188 {
7189 i = readfile(browseFile, NULL,
7190 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
7191 vim_free(browseFile);
7192 }
7193 else
7194 i = OK;
7195 }
7196 else
7197#endif
7198 if (*eap->arg == NUL)
7199 {
7200 if (check_fname() == FAIL) /* check for no file name */
7201 return;
7202 i = readfile(curbuf->b_ffname, curbuf->b_fname,
7203 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
7204 }
7205 else
7206 {
7207 if (vim_strchr(p_cpo, CPO_ALTREAD) != NULL)
7208 (void)setaltfname(eap->arg, eap->arg, (linenr_T)1);
7209 i = readfile(eap->arg, NULL,
7210 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
7211
7212 }
Bram Moolenaare13b9af2017-01-13 22:01:02 +01007213 if (i != OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007214 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007215#if defined(FEAT_EVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007216 if (!aborting())
7217#endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007218 semsg(_(e_notopen), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007219 }
7220 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00007221 {
7222 if (empty && exmode_active)
7223 {
7224 /* Delete the empty line that remains. Historically ex does
7225 * this but vi doesn't. */
7226 if (eap->line2 == 0)
7227 lnum = curbuf->b_ml.ml_line_count;
7228 else
7229 lnum = 1;
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00007230 if (*ml_get(lnum) == NUL && u_savedel(lnum, 1L) == OK)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007231 {
7232 ml_delete(lnum, FALSE);
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00007233 if (curwin->w_cursor.lnum > 1
7234 && curwin->w_cursor.lnum >= lnum)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007235 --curwin->w_cursor.lnum;
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00007236 deleted_lines_mark(lnum, 1L);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007237 }
7238 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007239 redraw_curbuf_later(VALID);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007240 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007241 }
7242}
7243
Bram Moolenaarea408852005-06-25 22:49:46 +00007244static char_u *prev_dir = NULL;
7245
7246#if defined(EXITFREE) || defined(PROTO)
7247 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007248free_cd_dir(void)
Bram Moolenaarea408852005-06-25 22:49:46 +00007249{
Bram Moolenaard23a8232018-02-10 18:45:26 +01007250 VIM_CLEAR(prev_dir);
7251 VIM_CLEAR(globaldir);
Bram Moolenaarea408852005-06-25 22:49:46 +00007252}
7253#endif
7254
Bram Moolenaarf4258302013-06-02 18:20:17 +02007255/*
7256 * Deal with the side effects of changing the current directory.
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007257 * When 'scope' is CDSCOPE_TABPAGE then this was after an ":tcd" command.
7258 * When 'scope' is CDSCOPE_WINDOW then this was after an ":lcd" command.
Bram Moolenaarf4258302013-06-02 18:20:17 +02007259 */
7260 void
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007261post_chdir(cdscope_T scope)
Bram Moolenaarf4258302013-06-02 18:20:17 +02007262{
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007263 if (scope != CDSCOPE_WINDOW)
Bram Moolenaar00aa0692019-04-27 20:37:57 +02007264 // Clear tab local directory for both :cd and :tcd
7265 VIM_CLEAR(curtab->tp_localdir);
Bram Moolenaard23a8232018-02-10 18:45:26 +01007266 VIM_CLEAR(curwin->w_localdir);
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007267 if (scope != CDSCOPE_GLOBAL)
Bram Moolenaarf4258302013-06-02 18:20:17 +02007268 {
7269 /* If still in global directory, need to remember current
7270 * directory as global directory. */
7271 if (globaldir == NULL && prev_dir != NULL)
7272 globaldir = vim_strsave(prev_dir);
7273 /* Remember this local directory for the window. */
7274 if (mch_dirname(NameBuff, MAXPATHL) == OK)
Bram Moolenaar00aa0692019-04-27 20:37:57 +02007275 {
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007276 if (scope == CDSCOPE_TABPAGE)
Bram Moolenaar00aa0692019-04-27 20:37:57 +02007277 curtab->tp_localdir = vim_strsave(NameBuff);
7278 else
7279 curwin->w_localdir = vim_strsave(NameBuff);
7280 }
Bram Moolenaarf4258302013-06-02 18:20:17 +02007281 }
7282 else
7283 {
7284 /* We are now in the global directory, no need to remember its
7285 * name. */
Bram Moolenaard23a8232018-02-10 18:45:26 +01007286 VIM_CLEAR(globaldir);
Bram Moolenaarf4258302013-06-02 18:20:17 +02007287 }
7288
7289 shorten_fnames(TRUE);
7290}
7291
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007292/*
7293 * Change directory function used by :cd/:tcd/:lcd Ex commands and the
7294 * chdir() function. If 'winlocaldir' is TRUE, then changes the window-local
7295 * directory. If 'tablocaldir' is TRUE, then changes the tab-local directory.
7296 * Otherwise changes the global directory.
7297 * Returns TRUE if the directory is successfully changed.
7298 */
7299 int
7300changedir_func(
7301 char_u *new_dir,
7302 int forceit,
7303 cdscope_T scope)
7304{
7305 char_u *tofree;
7306 int dir_differs;
7307 int retval = FALSE;
7308
7309 if (allbuf_locked())
7310 return FALSE;
7311
7312 if (vim_strchr(p_cpo, CPO_CHDIR) != NULL && curbufIsChanged() && !forceit)
7313 {
7314 emsg(_("E747: Cannot change directory, buffer is modified (add ! to override)"));
7315 return FALSE;
7316 }
7317
7318 // ":cd -": Change to previous directory
7319 if (STRCMP(new_dir, "-") == 0)
7320 {
7321 if (prev_dir == NULL)
7322 {
7323 emsg(_("E186: No previous directory"));
7324 return FALSE;
7325 }
7326 new_dir = prev_dir;
7327 }
7328
7329 // Save current directory for next ":cd -"
7330 tofree = prev_dir;
7331 if (mch_dirname(NameBuff, MAXPATHL) == OK)
7332 prev_dir = vim_strsave(NameBuff);
7333 else
7334 prev_dir = NULL;
7335
7336#if defined(UNIX) || defined(VMS)
7337 // for UNIX ":cd" means: go to home directory
7338 if (*new_dir == NUL)
7339 {
7340 // use NameBuff for home directory name
7341# ifdef VMS
7342 char_u *p;
7343
7344 p = mch_getenv((char_u *)"SYS$LOGIN");
7345 if (p == NULL || *p == NUL) // empty is the same as not set
7346 NameBuff[0] = NUL;
7347 else
7348 vim_strncpy(NameBuff, p, MAXPATHL - 1);
7349# else
7350 expand_env((char_u *)"$HOME", NameBuff, MAXPATHL);
7351# endif
7352 new_dir = NameBuff;
7353 }
7354#endif
7355 dir_differs = new_dir == NULL || prev_dir == NULL
7356 || pathcmp((char *)prev_dir, (char *)new_dir, -1) != 0;
7357 if (new_dir == NULL || (dir_differs && vim_chdir(new_dir)))
7358 emsg(_(e_failed));
7359 else
7360 {
7361 char_u *acmd_fname;
7362
7363 post_chdir(scope);
7364
7365 if (dir_differs)
7366 {
7367 if (scope == CDSCOPE_WINDOW)
7368 acmd_fname = (char_u *)"window";
7369 else if (scope == CDSCOPE_TABPAGE)
7370 acmd_fname = (char_u *)"tabpage";
7371 else
7372 acmd_fname = (char_u *)"global";
7373 apply_autocmds(EVENT_DIRCHANGED, acmd_fname, new_dir, FALSE,
7374 curbuf);
7375 }
7376 retval = TRUE;
7377 }
7378 vim_free(tofree);
7379
7380 return retval;
7381}
Bram Moolenaarea408852005-06-25 22:49:46 +00007382
Bram Moolenaar071d4272004-06-13 20:20:40 +00007383/*
Bram Moolenaar00aa0692019-04-27 20:37:57 +02007384 * ":cd", ":tcd", ":lcd", ":chdir" ":tchdir" and ":lchdir".
Bram Moolenaar071d4272004-06-13 20:20:40 +00007385 */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00007386 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007387ex_cd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007388{
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01007389 char_u *new_dir;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007390
7391 new_dir = eap->arg;
7392#if !defined(UNIX) && !defined(VMS)
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007393 // for non-UNIX ":cd" means: print current directory
Bram Moolenaar071d4272004-06-13 20:20:40 +00007394 if (*new_dir == NUL)
7395 ex_pwd(NULL);
7396 else
7397#endif
7398 {
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007399 cdscope_T scope = CDSCOPE_GLOBAL;
7400
7401 if (eap->cmdidx == CMD_lcd || eap->cmdidx == CMD_lchdir)
7402 scope = CDSCOPE_WINDOW;
7403 else if (eap->cmdidx == CMD_tcd || eap->cmdidx == CMD_tchdir)
7404 scope = CDSCOPE_TABPAGE;
7405
7406 if (changedir_func(new_dir, eap->forceit, scope))
Bram Moolenaardf177f62005-02-22 08:39:57 +00007407 {
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007408 // Echo the new current directory if the command was typed.
Bram Moolenaarfcfbc672009-07-09 18:13:49 +00007409 if (KeyTyped || p_verbose >= 5)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007410 ex_pwd(eap);
7411 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007412 }
7413}
7414
7415/*
7416 * ":pwd".
7417 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007418 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007419ex_pwd(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007420{
7421 if (mch_dirname(NameBuff, MAXPATHL) == OK)
7422 {
7423#ifdef BACKSLASH_IN_FILENAME
7424 slash_adjust(NameBuff);
7425#endif
Bram Moolenaar32526b32019-01-19 17:43:09 +01007426 msg((char *)NameBuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007427 }
7428 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007429 emsg(_("E187: Unknown"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007430}
7431
7432/*
7433 * ":=".
7434 */
7435 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007436ex_equal(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007437{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007438 smsg("%ld", (long)eap->line2);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007439 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007440}
7441
7442 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007443ex_sleep(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007444{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007445 int n;
7446 long len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007447
7448 if (cursor_valid())
7449 {
7450 n = W_WINROW(curwin) + curwin->w_wrow - msg_scrolled;
7451 if (n >= 0)
Bram Moolenaar53f81742017-09-22 14:35:51 +02007452 windgoto((int)n, curwin->w_wincol + curwin->w_wcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007453 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007454
7455 len = eap->line2;
7456 switch (*eap->arg)
7457 {
7458 case 'm': break;
7459 case NUL: len *= 1000L; break;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007460 default: semsg(_(e_invarg2), eap->arg); return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007461 }
7462 do_sleep(len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007463}
7464
7465/*
7466 * Sleep for "msec" milliseconds, but keep checking for a CTRL-C every second.
7467 */
7468 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007469do_sleep(long msec)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007470{
Bram Moolenaar52953192019-08-16 10:27:13 +02007471 long done = 0;
Bram Moolenaar975b5272016-03-15 23:10:59 +01007472 long wait_now;
Bram Moolenaar52953192019-08-16 10:27:13 +02007473# ifdef ELAPSED_FUNC
7474 elapsed_T start_tv;
7475
7476 // Remember at what time we started, so that we know how much longer we
7477 // should wait after waiting for a bit.
7478 ELAPSED_INIT(start_tv);
7479# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007480
7481 cursor_on();
Bram Moolenaarf45d7472018-09-30 18:22:26 +02007482 out_flush_cursor(FALSE, FALSE);
Bram Moolenaar52953192019-08-16 10:27:13 +02007483 while (!got_int && done < msec)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007484 {
Bram Moolenaar975b5272016-03-15 23:10:59 +01007485 wait_now = msec - done > 1000L ? 1000L : msec - done;
7486#ifdef FEAT_TIMERS
7487 {
7488 long due_time = check_due_timer();
7489
7490 if (due_time > 0 && due_time < wait_now)
7491 wait_now = due_time;
7492 }
7493#endif
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02007494#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar28e67e02019-08-15 23:05:49 +02007495 if (has_any_channel() && wait_now > 20L)
7496 wait_now = 20L;
7497#endif
7498#ifdef FEAT_SOUND
7499 if (has_any_sound_callback() && wait_now > 20L)
7500 wait_now = 20L;
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02007501#endif
Bram Moolenaar975b5272016-03-15 23:10:59 +01007502 ui_delay(wait_now, TRUE);
Bram Moolenaar52953192019-08-16 10:27:13 +02007503
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02007504#ifdef FEAT_JOB_CHANNEL
7505 if (has_any_channel())
7506 ui_breakcheck_force(TRUE);
7507 else
7508#endif
7509 ui_breakcheck();
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007510#ifdef MESSAGE_QUEUE
Bram Moolenaar52953192019-08-16 10:27:13 +02007511 // Process the netbeans and clientserver messages that may have been
7512 // received in the call to ui_breakcheck() when the GUI is in use. This
7513 // may occur when running a test case.
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007514 parse_queued_messages();
Bram Moolenaare3cc6d42011-10-20 21:58:34 +02007515#endif
Bram Moolenaar52953192019-08-16 10:27:13 +02007516
7517# ifdef ELAPSED_FUNC
7518 // actual time passed
7519 done = ELAPSED_FUNC(start_tv);
7520# else
7521 // guestimate time passed (will actually be more)
7522 done += wait_now;
7523# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007524 }
Bram Moolenaar1ebff3d2018-07-07 16:18:13 +02007525
7526 // If CTRL-C was typed to interrupt the sleep, drop the CTRL-C from the
7527 // input buffer, otherwise a following call to input() fails.
7528 if (got_int)
7529 (void)vpeekc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007530}
7531
Bram Moolenaar071d4272004-06-13 20:20:40 +00007532/*
7533 * ":winsize" command (obsolete).
7534 */
7535 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007536ex_winsize(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007537{
7538 int w, h;
7539 char_u *arg = eap->arg;
7540 char_u *p;
7541
7542 w = getdigits(&arg);
7543 arg = skipwhite(arg);
7544 p = arg;
7545 h = getdigits(&arg);
7546 if (*p != NUL && *arg == NUL)
7547 set_shellsize(w, h, TRUE);
7548 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007549 emsg(_("E465: :winsize requires two number arguments"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007550}
7551
Bram Moolenaar071d4272004-06-13 20:20:40 +00007552 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007553ex_wincmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007554{
7555 int xchar = NUL;
7556 char_u *p;
7557
7558 if (*eap->arg == 'g' || *eap->arg == Ctrl_G)
7559 {
7560 /* CTRL-W g and CTRL-W CTRL-G have an extra command character */
7561 if (eap->arg[1] == NUL)
7562 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007563 emsg(_(e_invarg));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007564 return;
7565 }
7566 xchar = eap->arg[1];
7567 p = eap->arg + 2;
7568 }
7569 else
7570 p = eap->arg + 1;
7571
7572 eap->nextcmd = check_nextcmd(p);
7573 p = skipwhite(p);
7574 if (*p != NUL && *p != '"' && eap->nextcmd == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007575 emsg(_(e_invarg));
Bram Moolenaar12bde492011-06-13 01:19:56 +02007576 else if (!eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007577 {
7578 /* Pass flags on for ":vertical wincmd ]". */
7579 postponed_split_flags = cmdmod.split;
Bram Moolenaard326ce82007-03-11 14:48:29 +00007580 postponed_split_tab = cmdmod.tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007581 do_window(*eap->arg, eap->addr_count > 0 ? eap->line2 : 0L, xchar);
7582 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00007583 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007584 }
7585}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007586
Bram Moolenaar843ee412004-06-30 16:16:41 +00007587#if defined(FEAT_GUI) || defined(UNIX) || defined(VMS) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007588/*
7589 * ":winpos".
7590 */
7591 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007592ex_winpos(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007593{
7594 int x, y;
7595 char_u *arg = eap->arg;
7596 char_u *p;
7597
7598 if (*arg == NUL)
7599 {
Bram Moolenaar843ee412004-06-30 16:16:41 +00007600# if defined(FEAT_GUI) || defined(MSWIN)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007601# ifdef VIMDLL
7602 if (gui.in_use ? gui_mch_get_winpos(&x, &y) != FAIL :
7603 mch_get_winpos(&x, &y) != FAIL)
7604# elif defined(FEAT_GUI)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007605 if (gui.in_use && gui_mch_get_winpos(&x, &y) != FAIL)
Bram Moolenaar843ee412004-06-30 16:16:41 +00007606# else
7607 if (mch_get_winpos(&x, &y) != FAIL)
7608# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007609 {
7610 sprintf((char *)IObuff, _("Window position: X %d, Y %d"), x, y);
Bram Moolenaar32526b32019-01-19 17:43:09 +01007611 msg((char *)IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007612 }
7613 else
7614# endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007615 emsg(_("E188: Obtaining window position not implemented for this platform"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007616 }
7617 else
7618 {
7619 x = getdigits(&arg);
7620 arg = skipwhite(arg);
7621 p = arg;
7622 y = getdigits(&arg);
7623 if (*p == NUL || *arg != NUL)
7624 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007625 emsg(_("E466: :winpos requires two number arguments"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007626 return;
7627 }
7628# ifdef FEAT_GUI
7629 if (gui.in_use)
7630 gui_mch_set_winpos(x, y);
7631 else if (gui.starting)
7632 {
7633 /* Remember the coordinates for when the window is opened. */
7634 gui_win_x = x;
7635 gui_win_y = y;
7636 }
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007637# if defined(HAVE_TGETENT) || defined(VIMDLL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007638 else
7639# endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007640# endif
7641# if defined(MSWIN) && (!defined(FEAT_GUI) || defined(VIMDLL))
Bram Moolenaar843ee412004-06-30 16:16:41 +00007642 mch_set_winpos(x, y);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007643# endif
7644# ifdef HAVE_TGETENT
7645 if (*T_CWP)
7646 term_set_winpos(x, y);
7647# endif
7648 }
7649}
7650#endif
7651
7652/*
7653 * Handle command that work like operators: ":delete", ":yank", ":>" and ":<".
7654 */
7655 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007656ex_operators(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007657{
7658 oparg_T oa;
7659
7660 clear_oparg(&oa);
7661 oa.regname = eap->regname;
7662 oa.start.lnum = eap->line1;
7663 oa.end.lnum = eap->line2;
7664 oa.line_count = eap->line2 - eap->line1 + 1;
7665 oa.motion_type = MLINE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007666 virtual_op = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007667 if (eap->cmdidx != CMD_yank) /* position cursor for undo */
7668 {
7669 setpcmark();
7670 curwin->w_cursor.lnum = eap->line1;
7671 beginline(BL_SOL | BL_FIX);
7672 }
7673
Bram Moolenaard07c6e12013-11-21 14:21:40 +01007674 if (VIsual_active)
7675 end_visual_mode();
Bram Moolenaard07c6e12013-11-21 14:21:40 +01007676
Bram Moolenaar071d4272004-06-13 20:20:40 +00007677 switch (eap->cmdidx)
7678 {
7679 case CMD_delete:
7680 oa.op_type = OP_DELETE;
7681 op_delete(&oa);
7682 break;
7683
7684 case CMD_yank:
7685 oa.op_type = OP_YANK;
7686 (void)op_yank(&oa, FALSE, TRUE);
7687 break;
7688
7689 default: /* CMD_rshift or CMD_lshift */
Bram Moolenaar6e707362013-06-14 19:15:58 +02007690 if (
Bram Moolenaar071d4272004-06-13 20:20:40 +00007691#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6e707362013-06-14 19:15:58 +02007692 (eap->cmdidx == CMD_rshift) ^ curwin->w_p_rl
7693#else
7694 eap->cmdidx == CMD_rshift
Bram Moolenaar071d4272004-06-13 20:20:40 +00007695#endif
Bram Moolenaar6e707362013-06-14 19:15:58 +02007696 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00007697 oa.op_type = OP_RSHIFT;
7698 else
7699 oa.op_type = OP_LSHIFT;
7700 op_shift(&oa, FALSE, eap->amount);
7701 break;
7702 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007703 virtual_op = MAYBE;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007704 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007705}
7706
7707/*
7708 * ":put".
7709 */
7710 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007711ex_put(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007712{
7713 /* ":0put" works like ":1put!". */
7714 if (eap->line2 == 0)
7715 {
7716 eap->line2 = 1;
7717 eap->forceit = TRUE;
7718 }
7719 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007720 do_put(eap->regname, eap->forceit ? BACKWARD : FORWARD, 1L,
7721 PUT_LINE|PUT_CURSLINE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007722}
7723
7724/*
7725 * Handle ":copy" and ":move".
7726 */
7727 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007728ex_copymove(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007729{
7730 long n;
7731
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02007732 n = get_address(eap, &eap->arg, eap->addr_type, FALSE, FALSE, FALSE, 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007733 if (eap->arg == NULL) /* error detected */
7734 {
7735 eap->nextcmd = NULL;
7736 return;
7737 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00007738 get_flags(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007739
7740 /*
7741 * move or copy lines from 'eap->line1'-'eap->line2' to below line 'n'
7742 */
7743 if (n == MAXLNUM || n < 0 || n > curbuf->b_ml.ml_line_count)
7744 {
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02007745 emsg(_(e_invrange));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007746 return;
7747 }
7748
7749 if (eap->cmdidx == CMD_move)
7750 {
7751 if (do_move(eap->line1, eap->line2, n) == FAIL)
7752 return;
7753 }
7754 else
7755 ex_copy(eap->line1, eap->line2, n);
7756 u_clearline();
7757 beginline(BL_SOL | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007758 ex_may_print(eap);
7759}
7760
7761/*
7762 * Print the current line if flags were given to the Ex command.
7763 */
Bram Moolenaarfd3fe982014-04-01 17:49:44 +02007764 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007765ex_may_print(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007766{
7767 if (eap->flags != 0)
7768 {
7769 print_line(curwin->w_cursor.lnum, (eap->flags & EXFLAG_NR),
7770 (eap->flags & EXFLAG_LIST));
7771 ex_no_reprint = TRUE;
7772 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007773}
7774
7775/*
7776 * ":smagic" and ":snomagic".
7777 */
7778 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007779ex_submagic(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007780{
7781 int magic_save = p_magic;
7782
7783 p_magic = (eap->cmdidx == CMD_smagic);
7784 do_sub(eap);
7785 p_magic = magic_save;
7786}
7787
7788/*
7789 * ":join".
7790 */
7791 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007792ex_join(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007793{
7794 curwin->w_cursor.lnum = eap->line1;
7795 if (eap->line1 == eap->line2)
7796 {
7797 if (eap->addr_count >= 2) /* :2,2join does nothing */
7798 return;
7799 if (eap->line2 == curbuf->b_ml.ml_line_count)
7800 {
7801 beep_flush();
7802 return;
7803 }
7804 ++eap->line2;
7805 }
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02007806 (void)do_join(eap->line2 - eap->line1 + 1, !eap->forceit, TRUE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007807 beginline(BL_WHITE | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007808 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007809}
7810
7811/*
7812 * ":[addr]@r" or ":[addr]*r": execute register
7813 */
7814 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007815ex_at(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007816{
7817 int c;
Bram Moolenaar60462872009-11-03 11:40:19 +00007818 int prev_len = typebuf.tb_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007819
7820 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaar4930a762016-09-11 14:39:53 +02007821 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007822
7823#ifdef USE_ON_FLY_SCROLL
7824 dont_scroll = TRUE; /* disallow scrolling here */
7825#endif
7826
7827 /* get the register name. No name means to use the previous one */
7828 c = *eap->arg;
7829 if (c == NUL || (c == '*' && *eap->cmd == '*'))
7830 c = '@';
Bram Moolenaard333d1e2006-11-07 17:43:47 +00007831 /* Put the register in the typeahead buffer with the "silent" flag. */
7832 if (do_execreg(c, TRUE, vim_strchr(p_cpo, CPO_EXECBUF) != NULL, TRUE)
7833 == FAIL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007834 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007835 beep_flush();
Bram Moolenaardf177f62005-02-22 08:39:57 +00007836 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007837 else
7838 {
7839 int save_efr = exec_from_reg;
7840
7841 exec_from_reg = TRUE;
7842
7843 /*
7844 * Execute from the typeahead buffer.
Bram Moolenaar60462872009-11-03 11:40:19 +00007845 * Continue until the stuff buffer is empty and all added characters
7846 * have been consumed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007847 */
Bram Moolenaar60462872009-11-03 11:40:19 +00007848 while (!stuff_empty() || typebuf.tb_len > prev_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007849 (void)do_cmdline(NULL, getexline, NULL, DOCMD_NOWAIT|DOCMD_VERBOSE);
7850
7851 exec_from_reg = save_efr;
7852 }
7853}
7854
7855/*
7856 * ":!".
7857 */
7858 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007859ex_bang(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007860{
7861 do_bang(eap->addr_count, eap, eap->forceit, TRUE, TRUE);
7862}
7863
7864/*
7865 * ":undo".
7866 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007867 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +01007868ex_undo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007869{
Bram Moolenaard3667a22006-03-16 21:35:52 +00007870 if (eap->addr_count == 1) /* :undo 123 */
Bram Moolenaar730cde92010-06-27 05:18:54 +02007871 undo_time(eap->line2, FALSE, FALSE, TRUE);
Bram Moolenaard3667a22006-03-16 21:35:52 +00007872 else
7873 u_undo(1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007874}
7875
Bram Moolenaar55debbe2010-05-23 23:34:36 +02007876#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar6df6f472010-07-18 18:04:50 +02007877 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007878ex_wundo(exarg_T *eap)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02007879{
7880 char_u hash[UNDO_HASH_SIZE];
7881
7882 u_compute_hash(hash);
7883 u_write_undo(eap->arg, eap->forceit, curbuf, hash);
7884}
7885
Bram Moolenaar6df6f472010-07-18 18:04:50 +02007886 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007887ex_rundo(exarg_T *eap)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02007888{
7889 char_u hash[UNDO_HASH_SIZE];
7890
7891 u_compute_hash(hash);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02007892 u_read_undo(eap->arg, hash, NULL);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02007893}
7894#endif
7895
Bram Moolenaar071d4272004-06-13 20:20:40 +00007896/*
7897 * ":redo".
7898 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007899 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007900ex_redo(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007901{
7902 u_redo(1);
7903}
7904
7905/*
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007906 * ":earlier" and ":later".
7907 */
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007908 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007909ex_later(exarg_T *eap)
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007910{
7911 long count = 0;
7912 int sec = FALSE;
Bram Moolenaar730cde92010-06-27 05:18:54 +02007913 int file = FALSE;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007914 char_u *p = eap->arg;
7915
7916 if (*p == NUL)
7917 count = 1;
7918 else if (isdigit(*p))
7919 {
7920 count = getdigits(&p);
7921 switch (*p)
7922 {
7923 case 's': ++p; sec = TRUE; break;
7924 case 'm': ++p; sec = TRUE; count *= 60; break;
7925 case 'h': ++p; sec = TRUE; count *= 60 * 60; break;
Bram Moolenaar730cde92010-06-27 05:18:54 +02007926 case 'd': ++p; sec = TRUE; count *= 24 * 60 * 60; break;
7927 case 'f': ++p; file = TRUE; break;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007928 }
7929 }
7930
7931 if (*p != NUL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007932 semsg(_(e_invarg2), eap->arg);
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007933 else
Bram Moolenaar730cde92010-06-27 05:18:54 +02007934 undo_time(eap->cmdidx == CMD_earlier ? -count : count,
7935 sec, file, FALSE);
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007936}
7937
7938/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007939 * ":redir": start/stop redirection.
7940 */
7941 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007942ex_redir(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007943{
7944 char *mode;
7945 char_u *fname;
Bram Moolenaarca472992005-01-21 11:46:23 +00007946 char_u *arg = eap->arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007947
Bram Moolenaarba768492016-07-08 15:32:54 +02007948#ifdef FEAT_EVAL
Bram Moolenaar79815f12016-07-09 17:07:29 +02007949 if (redir_execute)
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02007950 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007951 emsg(_("E930: Cannot use :redir inside execute()"));
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02007952 return;
7953 }
Bram Moolenaarba768492016-07-08 15:32:54 +02007954#endif
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02007955
Bram Moolenaar071d4272004-06-13 20:20:40 +00007956 if (STRICMP(eap->arg, "END") == 0)
7957 close_redir();
7958 else
7959 {
Bram Moolenaarca472992005-01-21 11:46:23 +00007960 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00007961 {
Bram Moolenaarca472992005-01-21 11:46:23 +00007962 ++arg;
7963 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00007964 {
Bram Moolenaarca472992005-01-21 11:46:23 +00007965 ++arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007966 mode = "a";
7967 }
7968 else
7969 mode = "w";
Bram Moolenaarca472992005-01-21 11:46:23 +00007970 arg = skipwhite(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007971
7972 close_redir();
7973
7974 /* Expand environment variables and "~/". */
Bram Moolenaarca472992005-01-21 11:46:23 +00007975 fname = expand_env_save(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007976 if (fname == NULL)
7977 return;
7978#ifdef FEAT_BROWSE
7979 if (cmdmod.browse)
7980 {
7981 char_u *browseFile;
7982
Bram Moolenaar7171abe2004-10-11 10:06:20 +00007983 browseFile = do_browse(BROWSE_SAVE,
7984 (char_u *)_("Save Redirection"),
Bram Moolenaarc36651b2018-04-29 12:22:56 +02007985 fname, NULL, NULL,
7986 (char_u *)_(BROWSE_FILTER_ALL_FILES), curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007987 if (browseFile == NULL)
7988 return; /* operation cancelled */
7989 vim_free(fname);
7990 fname = browseFile;
7991 eap->forceit = TRUE; /* since dialog already asked */
7992 }
7993#endif
7994
7995 redir_fd = open_exfile(fname, eap->forceit, mode);
7996 vim_free(fname);
7997 }
7998#ifdef FEAT_EVAL
Bram Moolenaarca472992005-01-21 11:46:23 +00007999 else if (*arg == '@')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008000 {
8001 /* redirect to a register a-z (resp. A-Z for appending) */
8002 close_redir();
Bram Moolenaarca472992005-01-21 11:46:23 +00008003 ++arg;
8004 if (ASCII_ISALPHA(*arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008005# ifdef FEAT_CLIPBOARD
Bram Moolenaarca472992005-01-21 11:46:23 +00008006 || *arg == '*'
Bram Moolenaar9a51c6e2006-11-14 19:25:02 +00008007 || *arg == '+'
Bram Moolenaar071d4272004-06-13 20:20:40 +00008008# endif
Bram Moolenaarca472992005-01-21 11:46:23 +00008009 || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008010 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008011 redir_reg = *arg++;
Bram Moolenaarc188b882007-10-19 14:20:54 +00008012 if (*arg == '>' && arg[1] == '>') /* append */
Bram Moolenaard9d30582005-05-18 22:10:28 +00008013 arg += 2;
Bram Moolenaarc188b882007-10-19 14:20:54 +00008014 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008015 {
Bram Moolenaarc188b882007-10-19 14:20:54 +00008016 /* Can use both "@a" and "@a>". */
Bram Moolenaar2c29bee2005-06-01 21:46:07 +00008017 if (*arg == '>')
8018 arg++;
Bram Moolenaarc188b882007-10-19 14:20:54 +00008019 /* Make register empty when not using @A-@Z and the
8020 * command is valid. */
8021 if (*arg == NUL && !isupper(redir_reg))
8022 write_reg_contents(redir_reg, (char_u *)"", -1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008023 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00008024 }
8025 if (*arg != NUL)
8026 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00008027 redir_reg = 0;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008028 semsg(_(e_invarg2), eap->arg);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008029 }
8030 }
8031 else if (*arg == '=' && arg[1] == '>')
8032 {
8033 int append;
8034
8035 /* redirect to a variable */
8036 close_redir();
8037 arg += 2;
8038
8039 if (*arg == '>')
8040 {
8041 ++arg;
8042 append = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008043 }
8044 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00008045 append = FALSE;
8046
8047 if (var_redir_start(skipwhite(arg), append) == OK)
8048 redir_vname = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008049 }
8050#endif
8051
8052 /* TODO: redirect to a buffer */
8053
Bram Moolenaar071d4272004-06-13 20:20:40 +00008054 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008055 semsg(_(e_invarg2), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008056 }
Bram Moolenaar29b2d262006-09-10 19:07:28 +00008057
8058 /* Make sure redirection is not off. Can happen for cmdline completion
8059 * that indirectly invokes a command to catch its output. */
8060 if (redir_fd != NULL
8061#ifdef FEAT_EVAL
8062 || redir_reg || redir_vname
8063#endif
8064 )
8065 redir_off = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008066}
8067
8068/*
8069 * ":redraw": force redraw
8070 */
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01008071 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008072ex_redraw(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008073{
8074 int r = RedrawingDisabled;
8075 int p = p_lz;
8076
8077 RedrawingDisabled = 0;
8078 p_lz = FALSE;
Bram Moolenaarabc39ab2017-03-01 18:04:05 +01008079 validate_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008080 update_topline();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01008081 update_screen(eap->forceit ? CLEAR : VIsual_active ? INVERTED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008082#ifdef FEAT_TITLE
8083 if (need_maketitle)
8084 maketitle();
8085#endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02008086#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
8087# ifdef VIMDLL
8088 if (!gui.in_use)
8089# endif
8090 resize_console_buf();
Bram Moolenaar78d21da2019-02-17 15:00:52 +01008091#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008092 RedrawingDisabled = r;
8093 p_lz = p;
8094
8095 /* Reset msg_didout, so that a message that's there is overwritten. */
8096 msg_didout = FALSE;
8097 msg_col = 0;
8098
Bram Moolenaar56667a52013-07-17 11:54:28 +02008099 /* No need to wait after an intentional redraw. */
8100 need_wait_return = FALSE;
8101
Bram Moolenaar071d4272004-06-13 20:20:40 +00008102 out_flush();
8103}
8104
8105/*
8106 * ":redrawstatus": force redraw of status line(s)
8107 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008108 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008109ex_redrawstatus(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008110{
Bram Moolenaar071d4272004-06-13 20:20:40 +00008111 int r = RedrawingDisabled;
8112 int p = p_lz;
8113
8114 RedrawingDisabled = 0;
8115 p_lz = FALSE;
8116 if (eap->forceit)
8117 status_redraw_all();
8118 else
8119 status_redraw_curbuf();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01008120 update_screen(VIsual_active ? INVERTED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008121 RedrawingDisabled = r;
8122 p_lz = p;
8123 out_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008124}
8125
Bram Moolenaare12bab32019-01-08 22:02:56 +01008126/*
8127 * ":redrawtabline": force redraw of the tabline
8128 */
8129 static void
8130ex_redrawtabline(exarg_T *eap UNUSED)
8131{
8132 int r = RedrawingDisabled;
8133 int p = p_lz;
8134
8135 RedrawingDisabled = 0;
8136 p_lz = FALSE;
8137
8138 draw_tabline();
8139
8140 RedrawingDisabled = r;
8141 p_lz = p;
8142 out_flush();
8143}
8144
Bram Moolenaar071d4272004-06-13 20:20:40 +00008145 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008146close_redir(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008147{
8148 if (redir_fd != NULL)
8149 {
8150 fclose(redir_fd);
8151 redir_fd = NULL;
8152 }
8153#ifdef FEAT_EVAL
8154 redir_reg = 0;
Bram Moolenaardf177f62005-02-22 08:39:57 +00008155 if (redir_vname)
8156 {
8157 var_redir_stop();
8158 redir_vname = 0;
8159 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008160#endif
8161}
8162
Bram Moolenaarf96ae0b2019-07-28 15:21:55 +02008163#if (defined(FEAT_SESSION) || defined(FEAT_EVAL)) || defined(PROTO)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008164 int
Bram Moolenaarf1d25012016-03-03 12:22:53 +01008165vim_mkdir_emsg(char_u *name, int prot)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008166{
8167 if (vim_mkdir(name, prot) != 0)
8168 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008169 semsg(_("E739: Cannot create directory: %s"), name);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008170 return FAIL;
8171 }
8172 return OK;
8173}
8174#endif
8175
Bram Moolenaar071d4272004-06-13 20:20:40 +00008176/*
8177 * Open a file for writing for an Ex command, with some checks.
8178 * Return file descriptor, or NULL on failure.
8179 */
8180 FILE *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008181open_exfile(
8182 char_u *fname,
8183 int forceit,
8184 char *mode) /* "w" for create new file or "a" for append */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008185{
8186 FILE *fd;
8187
8188#ifdef UNIX
8189 /* with Unix it is possible to open a directory */
8190 if (mch_isdir(fname))
8191 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008192 semsg(_(e_isadir2), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008193 return NULL;
8194 }
8195#endif
8196 if (!forceit && *mode != 'a' && vim_fexists(fname))
8197 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008198 semsg(_("E189: \"%s\" exists (add ! to override)"), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008199 return NULL;
8200 }
8201
8202 if ((fd = mch_fopen((char *)fname, mode)) == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008203 semsg(_("E190: Cannot open \"%s\" for writing"), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008204
8205 return fd;
8206}
8207
8208/*
8209 * ":mark" and ":k".
8210 */
8211 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008212ex_mark(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008213{
8214 pos_T pos;
8215
8216 if (*eap->arg == NUL) /* No argument? */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008217 emsg(_(e_argreq));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008218 else if (eap->arg[1] != NUL) /* more than one character? */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008219 emsg(_(e_trailing));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008220 else
8221 {
8222 pos = curwin->w_cursor; /* save curwin->w_cursor */
8223 curwin->w_cursor.lnum = eap->line2;
8224 beginline(BL_WHITE | BL_FIX);
8225 if (setmark(*eap->arg) == FAIL) /* set mark */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008226 emsg(_("E191: Argument must be a letter or forward/backward quote"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008227 curwin->w_cursor = pos; /* restore curwin->w_cursor */
8228 }
8229}
8230
8231/*
8232 * Update w_topline, w_leftcol and the cursor position.
8233 */
8234 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008235update_topline_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008236{
8237 check_cursor(); /* put cursor on valid line */
8238 update_topline();
8239 if (!curwin->w_p_wrap)
8240 validate_cursor();
8241 update_curswant();
8242}
8243
Bram Moolenaar071d4272004-06-13 20:20:40 +00008244/*
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008245 * Save the current State and go to Normal mode.
8246 * Return TRUE if the typeahead could be saved.
8247 */
8248 int
8249save_current_state(save_state_T *sst)
8250{
8251 sst->save_msg_scroll = msg_scroll;
8252 sst->save_restart_edit = restart_edit;
8253 sst->save_msg_didout = msg_didout;
8254 sst->save_State = State;
8255 sst->save_insertmode = p_im;
8256 sst->save_finish_op = finish_op;
8257 sst->save_opcount = opcount;
Bram Moolenaarcce713d2019-03-04 11:40:12 +01008258 sst->save_reg_executing = reg_executing;
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008259
8260 msg_scroll = FALSE; /* no msg scrolling in Normal mode */
8261 restart_edit = 0; /* don't go to Insert mode */
8262 p_im = FALSE; /* don't use 'insertmode' */
8263
8264 /*
8265 * Save the current typeahead. This is required to allow using ":normal"
8266 * from an event handler and makes sure we don't hang when the argument
8267 * ends with half a command.
8268 */
8269 save_typeahead(&sst->tabuf);
8270 return sst->tabuf.typebuf_valid;
8271}
8272
8273 void
8274restore_current_state(save_state_T *sst)
8275{
8276 /* Restore the previous typeahead. */
8277 restore_typeahead(&sst->tabuf);
8278
8279 msg_scroll = sst->save_msg_scroll;
8280 restart_edit = sst->save_restart_edit;
8281 p_im = sst->save_insertmode;
8282 finish_op = sst->save_finish_op;
8283 opcount = sst->save_opcount;
Bram Moolenaarcce713d2019-03-04 11:40:12 +01008284 reg_executing = sst->save_reg_executing;
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008285 msg_didout |= sst->save_msg_didout; /* don't reset msg_didout now */
8286
8287 /* Restore the state (needed when called from a function executed for
8288 * 'indentexpr'). Update the mouse and cursor, they may have changed. */
8289 State = sst->save_State;
8290#ifdef CURSOR_SHAPE
8291 ui_cursor_shape(); /* may show different cursor shape */
8292#endif
8293}
8294
8295/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008296 * ":normal[!] {commands}": Execute normal mode commands.
8297 */
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01008298 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008299ex_normal(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008300{
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008301 save_state_T save_state;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008302 char_u *arg = NULL;
8303 int l;
8304 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008305
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008306 if (ex_normal_lock > 0)
8307 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008308 emsg(_(e_secure));
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008309 return;
8310 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008311 if (ex_normal_busy >= p_mmd)
8312 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008313 emsg(_("E192: Recursive use of :normal too deep"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008314 return;
8315 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008316
Bram Moolenaar071d4272004-06-13 20:20:40 +00008317 /*
8318 * vgetc() expects a CSI and K_SPECIAL to have been escaped. Don't do
8319 * this for the K_SPECIAL leading byte, otherwise special keys will not
8320 * work.
8321 */
8322 if (has_mbyte)
8323 {
8324 int len = 0;
8325
8326 /* Count the number of characters to be escaped. */
8327 for (p = eap->arg; *p != NUL; ++p)
8328 {
Bram Moolenaar13505972019-01-24 15:04:48 +01008329#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008330 if (*p == CSI) /* leadbyte CSI */
8331 len += 2;
Bram Moolenaar13505972019-01-24 15:04:48 +01008332#endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008333 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008334 if (*++p == K_SPECIAL /* trailbyte K_SPECIAL or CSI */
Bram Moolenaar13505972019-01-24 15:04:48 +01008335#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008336 || *p == CSI
Bram Moolenaar13505972019-01-24 15:04:48 +01008337#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008338 )
8339 len += 2;
8340 }
8341 if (len > 0)
8342 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02008343 arg = alloc(STRLEN(eap->arg) + len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008344 if (arg != NULL)
8345 {
8346 len = 0;
8347 for (p = eap->arg; *p != NUL; ++p)
8348 {
8349 arg[len++] = *p;
Bram Moolenaar13505972019-01-24 15:04:48 +01008350#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008351 if (*p == CSI)
8352 {
8353 arg[len++] = KS_EXTRA;
8354 arg[len++] = (int)KE_CSI;
8355 }
Bram Moolenaar13505972019-01-24 15:04:48 +01008356#endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008357 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008358 {
8359 arg[len++] = *++p;
8360 if (*p == K_SPECIAL)
8361 {
8362 arg[len++] = KS_SPECIAL;
8363 arg[len++] = KE_FILLER;
8364 }
Bram Moolenaar13505972019-01-24 15:04:48 +01008365#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008366 else if (*p == CSI)
8367 {
8368 arg[len++] = KS_EXTRA;
8369 arg[len++] = (int)KE_CSI;
8370 }
Bram Moolenaar13505972019-01-24 15:04:48 +01008371#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008372 }
8373 arg[len] = NUL;
8374 }
8375 }
8376 }
8377 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008378
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008379 ++ex_normal_busy;
8380 if (save_current_state(&save_state))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008381 {
8382 /*
8383 * Repeat the :normal command for each line in the range. When no
8384 * range given, execute it just once, without positioning the cursor
8385 * first.
8386 */
8387 do
8388 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008389 if (eap->addr_count != 0)
8390 {
8391 curwin->w_cursor.lnum = eap->line1++;
8392 curwin->w_cursor.col = 0;
Bram Moolenaard5d37532017-03-27 23:02:07 +02008393 check_cursor_moved(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008394 }
8395
Bram Moolenaar13505972019-01-24 15:04:48 +01008396 exec_normal_cmd(arg != NULL
8397 ? arg
8398 : eap->arg, eap->forceit ? REMAP_NONE : REMAP_YES, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008399 }
8400 while (eap->addr_count > 0 && eap->line1 <= eap->line2 && !got_int);
8401 }
8402
8403 /* Might not return to the main loop when in an event handler. */
8404 update_topline_cursor();
8405
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008406 restore_current_state(&save_state);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008407 --ex_normal_busy;
Bram Moolenaareda73602014-11-05 09:53:23 +01008408#ifdef FEAT_MOUSE
8409 setmouse();
8410#endif
8411#ifdef CURSOR_SHAPE
8412 ui_cursor_shape(); /* may show different cursor shape */
8413#endif
8414
Bram Moolenaar071d4272004-06-13 20:20:40 +00008415 vim_free(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008416}
8417
8418/*
Bram Moolenaar64969662005-12-14 21:59:55 +00008419 * ":startinsert", ":startreplace" and ":startgreplace"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008420 */
8421 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008422ex_startinsert(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008423{
Bram Moolenaarfd371682005-01-14 21:42:54 +00008424 if (eap->forceit)
8425 {
Bram Moolenaar09ca9322017-09-26 17:40:45 +02008426 /* cursor line can be zero on startup */
8427 if (!curwin->w_cursor.lnum)
8428 curwin->w_cursor.lnum = 1;
Bram Moolenaarfd371682005-01-14 21:42:54 +00008429 coladvance((colnr_T)MAXCOL);
8430 curwin->w_curswant = MAXCOL;
8431 curwin->w_set_curswant = FALSE;
8432 }
8433
Bram Moolenaara40c5002005-01-09 21:16:21 +00008434 /* Ignore the command when already in Insert mode. Inserting an
8435 * expression register that invokes a function can do this. */
8436 if (State & INSERT)
8437 return;
8438
Bram Moolenaar64969662005-12-14 21:59:55 +00008439 if (eap->cmdidx == CMD_startinsert)
8440 restart_edit = 'a';
8441 else if (eap->cmdidx == CMD_startreplace)
8442 restart_edit = 'R';
Bram Moolenaar071d4272004-06-13 20:20:40 +00008443 else
Bram Moolenaar64969662005-12-14 21:59:55 +00008444 restart_edit = 'V';
8445
8446 if (!eap->forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008447 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00008448 if (eap->cmdidx == CMD_startinsert)
8449 restart_edit = 'i';
Bram Moolenaar071d4272004-06-13 20:20:40 +00008450 curwin->w_curswant = 0; /* avoid MAXCOL */
8451 }
8452}
8453
8454/*
8455 * ":stopinsert"
8456 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008457 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008458ex_stopinsert(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008459{
8460 restart_edit = 0;
8461 stop_insert_mode = TRUE;
Bram Moolenaarfd773e92016-04-02 19:39:16 +02008462 clearmode();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008463}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008464
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008465/*
8466 * Execute normal mode command "cmd".
8467 * "remap" can be REMAP_NONE or REMAP_YES.
8468 */
8469 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008470exec_normal_cmd(char_u *cmd, int remap, int silent)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008471{
Bram Moolenaar25281632016-01-21 23:32:32 +01008472 /* Stuff the argument into the typeahead buffer. */
8473 ins_typebuf(cmd, remap, 0, TRUE, silent);
Bram Moolenaar586c70c2018-10-02 16:23:58 +02008474 exec_normal(FALSE, FALSE, FALSE);
Bram Moolenaar25281632016-01-21 23:32:32 +01008475}
Bram Moolenaar25281632016-01-21 23:32:32 +01008476
Bram Moolenaar25281632016-01-21 23:32:32 +01008477/*
8478 * Execute normal_cmd() until there is no typeahead left.
Bram Moolenaar586c70c2018-10-02 16:23:58 +02008479 * When "use_vpeekc" is TRUE use vpeekc() to check for available chars.
Bram Moolenaar25281632016-01-21 23:32:32 +01008480 */
8481 void
Bram Moolenaar586c70c2018-10-02 16:23:58 +02008482exec_normal(int was_typed, int use_vpeekc, int may_use_terminal_loop UNUSED)
Bram Moolenaar25281632016-01-21 23:32:32 +01008483{
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008484 oparg_T oa;
Bram Moolenaar905dd902019-04-07 14:21:47 +02008485 int c;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008486
Bram Moolenaar905dd902019-04-07 14:21:47 +02008487 // When calling vpeekc() from feedkeys() it will return Ctrl_C when there
8488 // is nothing to get, so also check for Ctrl_C.
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008489 clear_oparg(&oa);
8490 finish_op = FALSE;
Bram Moolenaar586c70c2018-10-02 16:23:58 +02008491 while ((!stuff_empty()
8492 || ((was_typed || !typebuf_typed()) && typebuf.tb_len > 0)
Bram Moolenaar905dd902019-04-07 14:21:47 +02008493 || (use_vpeekc && (c = vpeekc()) != NUL && c != Ctrl_C))
Bram Moolenaar586c70c2018-10-02 16:23:58 +02008494 && !got_int)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008495 {
8496 update_topline_cursor();
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02008497#ifdef FEAT_TERMINAL
Bram Moolenaar655a82a2018-05-08 22:01:07 +02008498 if (may_use_terminal_loop && term_use_loop()
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02008499 && oa.op_type == OP_NOP && oa.regname == NUL
8500 && !VIsual_active)
8501 {
8502 /* If terminal_loop() returns OK we got a key that is handled
8503 * in Normal model. With FAIL we first need to position the
8504 * cursor and the screen needs to be redrawn. */
8505 if (terminal_loop(TRUE) == OK)
8506 normal_cmd(&oa, TRUE);
8507 }
8508 else
8509#endif
8510 /* execute a Normal mode cmd */
8511 normal_cmd(&oa, TRUE);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008512 }
8513}
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008514
Bram Moolenaar071d4272004-06-13 20:20:40 +00008515#ifdef FEAT_FIND_ID
8516 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008517ex_checkpath(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008518{
8519 find_pattern_in_path(NULL, 0, 0, FALSE, FALSE, CHECK_PATH, 1L,
8520 eap->forceit ? ACTION_SHOW_ALL : ACTION_SHOW,
8521 (linenr_T)1, (linenr_T)MAXLNUM);
8522}
8523
Bram Moolenaar4033c552017-09-16 20:54:51 +02008524#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008525/*
8526 * ":psearch"
8527 */
8528 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008529ex_psearch(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008530{
8531 g_do_tagpreview = p_pvh;
8532 ex_findpat(eap);
8533 g_do_tagpreview = 0;
8534}
8535#endif
8536
8537 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008538ex_findpat(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008539{
8540 int whole = TRUE;
8541 long n;
8542 char_u *p;
8543 int action;
8544
8545 switch (cmdnames[eap->cmdidx].cmd_name[2])
8546 {
8547 case 'e': /* ":psearch", ":isearch" and ":dsearch" */
8548 if (cmdnames[eap->cmdidx].cmd_name[0] == 'p')
8549 action = ACTION_GOTO;
8550 else
8551 action = ACTION_SHOW;
8552 break;
8553 case 'i': /* ":ilist" and ":dlist" */
8554 action = ACTION_SHOW_ALL;
8555 break;
8556 case 'u': /* ":ijump" and ":djump" */
8557 action = ACTION_GOTO;
8558 break;
8559 default: /* ":isplit" and ":dsplit" */
8560 action = ACTION_SPLIT;
8561 break;
8562 }
8563
8564 n = 1;
8565 if (vim_isdigit(*eap->arg)) /* get count */
8566 {
8567 n = getdigits(&eap->arg);
8568 eap->arg = skipwhite(eap->arg);
8569 }
8570 if (*eap->arg == '/') /* Match regexp, not just whole words */
8571 {
8572 whole = FALSE;
8573 ++eap->arg;
8574 p = skip_regexp(eap->arg, '/', p_magic, NULL);
8575 if (*p)
8576 {
8577 *p++ = NUL;
8578 p = skipwhite(p);
8579
8580 /* Check for trailing illegal characters */
8581 if (!ends_excmd(*p))
8582 eap->errmsg = e_trailing;
8583 else
8584 eap->nextcmd = check_nextcmd(p);
8585 }
8586 }
8587 if (!eap->skip)
8588 find_pattern_in_path(eap->arg, 0, (int)STRLEN(eap->arg),
8589 whole, !eap->forceit,
8590 *eap->cmd == 'd' ? FIND_DEFINE : FIND_ANY,
8591 n, action, eap->line1, eap->line2);
8592}
8593#endif
8594
Bram Moolenaar071d4272004-06-13 20:20:40 +00008595
Bram Moolenaar4033c552017-09-16 20:54:51 +02008596#ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00008597/*
8598 * ":ptag", ":ptselect", ":ptjump", ":ptnext", etc.
8599 */
8600 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008601ex_ptag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008602{
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01008603 g_do_tagpreview = p_pvh; /* will be reset to 0 in ex_tag_cmd() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008604 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
8605}
8606
8607/*
8608 * ":pedit"
8609 */
8610 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008611ex_pedit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008612{
8613 win_T *curwin_save = curwin;
8614
8615 g_do_tagpreview = p_pvh;
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00008616 prepare_tagpreview(TRUE);
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02008617
Bram Moolenaar67883b42017-07-27 22:57:00 +02008618 keep_help_flag = bt_help(curwin_save->w_buffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008619 do_exedit(eap, NULL);
8620 keep_help_flag = FALSE;
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02008621
Bram Moolenaar071d4272004-06-13 20:20:40 +00008622 if (curwin != curwin_save && win_valid(curwin_save))
8623 {
8624 /* Return cursor to where we were */
8625 validate_cursor();
8626 redraw_later(VALID);
8627 win_enter(curwin_save, TRUE);
8628 }
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02008629# ifdef FEAT_TEXT_PROP
8630 else if (WIN_IS_POPUP(curwin))
8631 {
8632 // can't keep focus in popup window
8633 win_enter(firstwin, TRUE);
8634 }
8635# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008636 g_do_tagpreview = 0;
8637}
Bram Moolenaar4033c552017-09-16 20:54:51 +02008638#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008639
8640/*
8641 * ":stag", ":stselect" and ":stjump".
8642 */
8643 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008644ex_stag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008645{
8646 postponed_split = -1;
8647 postponed_split_flags = cmdmod.split;
Bram Moolenaard326ce82007-03-11 14:48:29 +00008648 postponed_split_tab = cmdmod.tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008649 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
8650 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00008651 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008652}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008653
8654/*
8655 * ":tag", ":tselect", ":tjump", ":tnext", etc.
8656 */
8657 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008658ex_tag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008659{
8660 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name);
8661}
8662
8663 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008664ex_tag_cmd(exarg_T *eap, char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008665{
8666 int cmd;
8667
8668 switch (name[1])
8669 {
8670 case 'j': cmd = DT_JUMP; /* ":tjump" */
8671 break;
8672 case 's': cmd = DT_SELECT; /* ":tselect" */
8673 break;
8674 case 'p': cmd = DT_PREV; /* ":tprevious" */
8675 break;
8676 case 'N': cmd = DT_PREV; /* ":tNext" */
8677 break;
8678 case 'n': cmd = DT_NEXT; /* ":tnext" */
8679 break;
8680 case 'o': cmd = DT_POP; /* ":pop" */
8681 break;
8682 case 'f': /* ":tfirst" */
8683 case 'r': cmd = DT_FIRST; /* ":trewind" */
8684 break;
8685 case 'l': cmd = DT_LAST; /* ":tlast" */
8686 break;
8687 default: /* ":tag" */
8688#ifdef FEAT_CSCOPE
Bram Moolenaar7c94c262008-06-20 09:11:34 +00008689 if (p_cst && *eap->arg != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008690 {
Bram Moolenaard4db7712016-11-12 19:16:46 +01008691 ex_cstag(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008692 return;
8693 }
8694#endif
8695 cmd = DT_TAG;
8696 break;
8697 }
8698
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00008699 if (name[0] == 'l')
8700 {
8701#ifndef FEAT_QUICKFIX
8702 ex_ni(eap);
8703 return;
8704#else
8705 cmd = DT_LTAG;
8706#endif
8707 }
8708
Bram Moolenaar071d4272004-06-13 20:20:40 +00008709 do_tag(eap->arg, cmd, eap->addr_count > 0 ? (int)eap->line2 : 1,
8710 eap->forceit, TRUE);
8711}
8712
8713/*
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008714 * Check "str" for starting with a special cmdline variable.
8715 * If found return one of the SPEC_ values and set "*usedlen" to the length of
8716 * the variable. Otherwise return -1 and "*usedlen" is unchanged.
8717 */
8718 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008719find_cmdline_var(char_u *src, int *usedlen)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008720{
8721 int len;
8722 int i;
Bram Moolenaar8f0b2d42009-05-16 14:41:10 +00008723 static char *(spec_str[]) = {
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008724 "%",
8725#define SPEC_PERC 0
8726 "#",
Bram Moolenaar65f08472017-09-10 18:16:20 +02008727#define SPEC_HASH (SPEC_PERC + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008728 "<cword>", /* cursor word */
Bram Moolenaar65f08472017-09-10 18:16:20 +02008729#define SPEC_CWORD (SPEC_HASH + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008730 "<cWORD>", /* cursor WORD */
Bram Moolenaar65f08472017-09-10 18:16:20 +02008731#define SPEC_CCWORD (SPEC_CWORD + 1)
8732 "<cexpr>", /* expr under cursor */
8733#define SPEC_CEXPR (SPEC_CCWORD + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008734 "<cfile>", /* cursor path name */
Bram Moolenaar65f08472017-09-10 18:16:20 +02008735#define SPEC_CFILE (SPEC_CEXPR + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008736 "<sfile>", /* ":so" file name */
Bram Moolenaar65f08472017-09-10 18:16:20 +02008737#define SPEC_SFILE (SPEC_CFILE + 1)
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01008738 "<slnum>", /* ":so" file line number */
Bram Moolenaar65f08472017-09-10 18:16:20 +02008739#define SPEC_SLNUM (SPEC_SFILE + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008740 "<afile>", /* autocommand file name */
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008741#define SPEC_AFILE (SPEC_SLNUM + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008742 "<abuf>", /* autocommand buffer number */
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008743#define SPEC_ABUF (SPEC_AFILE + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008744 "<amatch>", /* autocommand match name */
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01008745#define SPEC_AMATCH (SPEC_ABUF + 1)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008746 "<sflnum>", /* script file line number */
8747#define SPEC_SFLNUM (SPEC_AMATCH + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008748#ifdef FEAT_CLIENTSERVER
8749 "<client>"
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008750# define SPEC_CLIENT (SPEC_SFLNUM + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008751#endif
8752 };
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008753
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00008754 for (i = 0; i < (int)(sizeof(spec_str) / sizeof(char *)); ++i)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008755 {
8756 len = (int)STRLEN(spec_str[i]);
8757 if (STRNCMP(src, spec_str[i], len) == 0)
8758 {
8759 *usedlen = len;
8760 return i;
8761 }
8762 }
8763 return -1;
8764}
8765
8766/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008767 * Evaluate cmdline variables.
8768 *
8769 * change '%' to curbuf->b_ffname
8770 * '#' to curwin->w_altfile
8771 * '<cword>' to word under the cursor
8772 * '<cWORD>' to WORD under the cursor
Bram Moolenaar8071cb22019-07-12 17:58:01 +02008773 * '<cexpr>' to C-expression under the cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00008774 * '<cfile>' to path name under the cursor
8775 * '<sfile>' to sourced file name
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01008776 * '<slnum>' to sourced file line number
Bram Moolenaar071d4272004-06-13 20:20:40 +00008777 * '<afile>' to file name for autocommand
8778 * '<abuf>' to buffer number for autocommand
8779 * '<amatch>' to matching name for autocommand
8780 *
8781 * When an error is detected, "errormsg" is set to a non-NULL pointer (may be
8782 * "" for error without a message) and NULL is returned.
8783 * Returns an allocated string if a valid match was found.
8784 * Returns NULL if no match was found. "usedlen" then still contains the
8785 * number of characters to skip.
8786 */
8787 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008788eval_vars(
8789 char_u *src, /* pointer into commandline */
8790 char_u *srcstart, /* beginning of valid memory for src */
8791 int *usedlen, /* characters after src that are used */
8792 linenr_T *lnump, /* line number for :e command, or NULL */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008793 char **errormsg, /* pointer to error message */
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008794 int *escaped) /* return value has escaped white space (can
Bram Moolenaar63b92542007-03-27 14:57:09 +00008795 * be NULL) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008796{
8797 int i;
8798 char_u *s;
8799 char_u *result;
8800 char_u *resultbuf = NULL;
8801 int resultlen;
8802 buf_T *buf;
8803 int valid = VALID_HEAD + VALID_PATH; /* assume valid result */
8804 int spec_idx;
8805#ifdef FEAT_MODIFY_FNAME
Bram Moolenaarfd249462018-07-28 16:14:30 +02008806 int tilde_file = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008807 int skip_mod = FALSE;
8808#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008809 char_u strbuf[30];
Bram Moolenaar071d4272004-06-13 20:20:40 +00008810
8811 *errormsg = NULL;
Bram Moolenaar63b92542007-03-27 14:57:09 +00008812 if (escaped != NULL)
8813 *escaped = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008814
8815 /*
8816 * Check if there is something to do.
8817 */
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008818 spec_idx = find_cmdline_var(src, usedlen);
8819 if (spec_idx < 0) /* no match */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008820 {
8821 *usedlen = 1;
8822 return NULL;
8823 }
8824
8825 /*
8826 * Skip when preceded with a backslash "\%" and "\#".
8827 * Note: In "\\%" the % is also not recognized!
8828 */
8829 if (src > srcstart && src[-1] == '\\')
8830 {
8831 *usedlen = 0;
Bram Moolenaara7241f52008-06-24 20:39:31 +00008832 STRMOVE(src - 1, src); /* remove backslash */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008833 return NULL;
8834 }
8835
8836 /*
8837 * word or WORD under cursor
8838 */
Bram Moolenaar65f08472017-09-10 18:16:20 +02008839 if (spec_idx == SPEC_CWORD || spec_idx == SPEC_CCWORD
8840 || spec_idx == SPEC_CEXPR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008841 {
Bram Moolenaar65f08472017-09-10 18:16:20 +02008842 resultlen = find_ident_under_cursor(&result,
8843 spec_idx == SPEC_CWORD ? (FIND_IDENT | FIND_STRING)
8844 : spec_idx == SPEC_CEXPR ? (FIND_IDENT | FIND_STRING | FIND_EVAL)
8845 : FIND_STRING);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008846 if (resultlen == 0)
8847 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008848 *errormsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +00008849 return NULL;
8850 }
8851 }
8852
8853 /*
8854 * '#': Alternate file name
8855 * '%': Current file name
8856 * File name under the cursor
8857 * File name for autocommand
8858 * and following modifiers
8859 */
8860 else
8861 {
8862 switch (spec_idx)
8863 {
8864 case SPEC_PERC: /* '%': current file */
8865 if (curbuf->b_fname == NULL)
8866 {
8867 result = (char_u *)"";
8868 valid = 0; /* Must have ":p:h" to be valid */
8869 }
8870 else
Bram Moolenaar00136dc2018-07-25 21:19:13 +02008871 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008872 result = curbuf->b_fname;
Bram Moolenaarfd249462018-07-28 16:14:30 +02008873#ifdef FEAT_MODIFY_FNAME
Bram Moolenaar00136dc2018-07-25 21:19:13 +02008874 tilde_file = STRCMP(result, "~") == 0;
Bram Moolenaarfd249462018-07-28 16:14:30 +02008875#endif
Bram Moolenaar00136dc2018-07-25 21:19:13 +02008876 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008877 break;
8878
8879 case SPEC_HASH: /* '#' or "#99": alternate file */
8880 if (src[1] == '#') /* "##": the argument list */
8881 {
8882 result = arg_all();
8883 resultbuf = result;
8884 *usedlen = 2;
Bram Moolenaar63b92542007-03-27 14:57:09 +00008885 if (escaped != NULL)
8886 *escaped = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008887#ifdef FEAT_MODIFY_FNAME
8888 skip_mod = TRUE;
8889#endif
8890 break;
8891 }
8892 s = src + 1;
Bram Moolenaard812df62008-11-09 12:46:09 +00008893 if (*s == '<') /* "#<99" uses v:oldfiles */
8894 ++s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008895 i = (int)getdigits(&s);
Bram Moolenaarc312b8b2017-10-28 17:53:04 +02008896 if (s == src + 2 && src[1] == '-')
8897 /* just a minus sign, don't skip over it */
8898 s--;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008899 *usedlen = (int)(s - src); /* length of what we expand */
8900
Bram Moolenaarc312b8b2017-10-28 17:53:04 +02008901 if (src[1] == '<' && i != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008902 {
Bram Moolenaard812df62008-11-09 12:46:09 +00008903 if (*usedlen < 2)
8904 {
8905 /* Should we give an error message for #<text? */
8906 *usedlen = 1;
8907 return NULL;
8908 }
8909#ifdef FEAT_EVAL
8910 result = list_find_str(get_vim_var_list(VV_OLDFILES),
8911 (long)i);
8912 if (result == NULL)
8913 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008914 *errormsg = "";
Bram Moolenaard812df62008-11-09 12:46:09 +00008915 return NULL;
8916 }
8917#else
Bram Moolenaar8e481e82019-01-15 20:07:48 +01008918 *errormsg = _("E809: #< is not available without the +eval feature");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008919 return NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +00008920#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008921 }
8922 else
Bram Moolenaard812df62008-11-09 12:46:09 +00008923 {
Bram Moolenaarc312b8b2017-10-28 17:53:04 +02008924 if (i == 0 && src[1] == '<' && *usedlen > 1)
8925 *usedlen = 1;
Bram Moolenaard812df62008-11-09 12:46:09 +00008926 buf = buflist_findnr(i);
8927 if (buf == NULL)
8928 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008929 *errormsg = _("E194: No alternate file name to substitute for '#'");
Bram Moolenaard812df62008-11-09 12:46:09 +00008930 return NULL;
8931 }
8932 if (lnump != NULL)
8933 *lnump = ECMD_LAST;
8934 if (buf->b_fname == NULL)
8935 {
8936 result = (char_u *)"";
8937 valid = 0; /* Must have ":p:h" to be valid */
8938 }
8939 else
Bram Moolenaar00136dc2018-07-25 21:19:13 +02008940 {
Bram Moolenaard812df62008-11-09 12:46:09 +00008941 result = buf->b_fname;
Bram Moolenaarfd249462018-07-28 16:14:30 +02008942#ifdef FEAT_MODIFY_FNAME
Bram Moolenaar00136dc2018-07-25 21:19:13 +02008943 tilde_file = STRCMP(result, "~") == 0;
Bram Moolenaarfd249462018-07-28 16:14:30 +02008944#endif
Bram Moolenaar00136dc2018-07-25 21:19:13 +02008945 }
Bram Moolenaard812df62008-11-09 12:46:09 +00008946 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008947 break;
8948
8949#ifdef FEAT_SEARCHPATH
8950 case SPEC_CFILE: /* file name under cursor */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008951 result = file_name_at_cursor(FNAME_MESS|FNAME_HYP, 1L, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008952 if (result == NULL)
8953 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008954 *errormsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +00008955 return NULL;
8956 }
8957 resultbuf = result; /* remember allocated string */
8958 break;
8959#endif
8960
Bram Moolenaar071d4272004-06-13 20:20:40 +00008961 case SPEC_AFILE: /* file name for autocommand */
8962 result = autocmd_fname;
Bram Moolenaarf6dad432008-09-18 19:29:58 +00008963 if (result != NULL && !autocmd_fname_full)
8964 {
8965 /* Still need to turn the fname into a full path. It is
8966 * postponed to avoid a delay when <afile> is not used. */
8967 autocmd_fname_full = TRUE;
8968 result = FullName_save(autocmd_fname, FALSE);
8969 vim_free(autocmd_fname);
8970 autocmd_fname = result;
8971 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008972 if (result == NULL)
8973 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008974 *errormsg = _("E495: no autocommand file name to substitute for \"<afile>\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008975 return NULL;
8976 }
Bram Moolenaara0174af2008-01-02 20:08:25 +00008977 result = shorten_fname1(result);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008978 break;
8979
8980 case SPEC_ABUF: /* buffer number for autocommand */
8981 if (autocmd_bufnr <= 0)
8982 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008983 *errormsg = _("E496: no autocommand buffer number to substitute for \"<abuf>\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008984 return NULL;
8985 }
8986 sprintf((char *)strbuf, "%d", autocmd_bufnr);
8987 result = strbuf;
8988 break;
8989
8990 case SPEC_AMATCH: /* match name for autocommand */
8991 result = autocmd_match;
8992 if (result == NULL)
8993 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008994 *errormsg = _("E497: no autocommand match name to substitute for \"<amatch>\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008995 return NULL;
8996 }
8997 break;
8998
Bram Moolenaar071d4272004-06-13 20:20:40 +00008999 case SPEC_SFILE: /* file name for ":so" command */
9000 result = sourcing_name;
9001 if (result == NULL)
9002 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009003 *errormsg = _("E498: no :source file name to substitute for \"<sfile>\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00009004 return NULL;
9005 }
9006 break;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009007
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009008 case SPEC_SLNUM: /* line in file for ":so" command */
9009 if (sourcing_name == NULL || sourcing_lnum == 0)
9010 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009011 *errormsg = _("E842: no line number to use for \"<slnum>\"");
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009012 return NULL;
9013 }
9014 sprintf((char *)strbuf, "%ld", (long)sourcing_lnum);
9015 result = strbuf;
9016 break;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009017
9018#ifdef FEAT_EVAL
9019 case SPEC_SFLNUM: /* line in script file */
9020 if (current_sctx.sc_lnum + sourcing_lnum == 0)
9021 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009022 *errormsg = _("E961: no line number to use for \"<sflnum>\"");
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009023 return NULL;
9024 }
9025 sprintf((char *)strbuf, "%ld",
9026 (long)(current_sctx.sc_lnum + sourcing_lnum));
9027 result = strbuf;
9028 break;
9029#endif
9030
9031#ifdef FEAT_CLIENTSERVER
Bram Moolenaar071d4272004-06-13 20:20:40 +00009032 case SPEC_CLIENT: /* Source of last submitted input */
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009033 sprintf((char *)strbuf, PRINTF_HEX_LONG_U,
9034 (long_u)clientWindow);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009035 result = strbuf;
9036 break;
9037#endif
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009038
Bram Moolenaar9e0f6ec2017-05-16 09:36:54 +02009039 default:
9040 result = (char_u *)""; /* avoid gcc warning */
9041 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009042 }
9043
9044 resultlen = (int)STRLEN(result); /* length of new string */
9045 if (src[*usedlen] == '<') /* remove the file name extension */
9046 {
9047 ++*usedlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009048 if ((s = vim_strrchr(result, '.')) != NULL && s >= gettail(result))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009049 resultlen = (int)(s - result);
9050 }
9051#ifdef FEAT_MODIFY_FNAME
9052 else if (!skip_mod)
9053 {
Bram Moolenaar00136dc2018-07-25 21:19:13 +02009054 valid |= modify_fname(src, tilde_file, usedlen, &result, &resultbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009055 &resultlen);
9056 if (result == NULL)
9057 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009058 *errormsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +00009059 return NULL;
9060 }
9061 }
9062#endif
9063 }
9064
9065 if (resultlen == 0 || valid != VALID_HEAD + VALID_PATH)
9066 {
9067 if (valid != VALID_HEAD + VALID_PATH)
9068 /* xgettext:no-c-format */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009069 *errormsg = _("E499: Empty file name for '%' or '#', only works with \":p:h\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00009070 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009071 *errormsg = _("E500: Evaluates to an empty string");
Bram Moolenaar071d4272004-06-13 20:20:40 +00009072 result = NULL;
9073 }
9074 else
9075 result = vim_strnsave(result, resultlen);
9076 vim_free(resultbuf);
9077 return result;
9078}
9079
9080/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009081 * Expand the <sfile> string in "arg".
9082 *
9083 * Returns an allocated string, or NULL for any error.
9084 */
9085 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009086expand_sfile(char_u *arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009087{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009088 char *errormsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009089 int len;
9090 char_u *result;
9091 char_u *newres;
9092 char_u *repl;
9093 int srclen;
9094 char_u *p;
9095
9096 result = vim_strsave(arg);
9097 if (result == NULL)
9098 return NULL;
9099
9100 for (p = result; *p; )
9101 {
9102 if (STRNCMP(p, "<sfile>", 7) != 0)
9103 ++p;
9104 else
9105 {
9106 /* replace "<sfile>" with the sourced file name, and do ":" stuff */
Bram Moolenaar63b92542007-03-27 14:57:09 +00009107 repl = eval_vars(p, result, &srclen, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009108 if (errormsg != NULL)
9109 {
9110 if (*errormsg)
9111 emsg(errormsg);
9112 vim_free(result);
9113 return NULL;
9114 }
9115 if (repl == NULL) /* no match (cannot happen) */
9116 {
9117 p += srclen;
9118 continue;
9119 }
9120 len = (int)STRLEN(result) - srclen + (int)STRLEN(repl) + 1;
9121 newres = alloc(len);
9122 if (newres == NULL)
9123 {
9124 vim_free(repl);
9125 vim_free(result);
9126 return NULL;
9127 }
9128 mch_memmove(newres, result, (size_t)(p - result));
9129 STRCPY(newres + (p - result), repl);
9130 len = (int)STRLEN(newres);
9131 STRCAT(newres, p + srclen);
9132 vim_free(repl);
9133 vim_free(result);
9134 result = newres;
9135 p = newres + len; /* continue after the match */
9136 }
9137 }
9138
9139 return result;
9140}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009141
Bram Moolenaar071d4272004-06-13 20:20:40 +00009142#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG) || defined(PROTO)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00009143/*
Bram Moolenaard9462e32011-04-11 21:35:11 +02009144 * Make a dialog message in "buff[DIALOG_MSG_SIZE]".
Bram Moolenaarb765d632005-06-07 21:00:02 +00009145 * "format" must contain "%s".
Bram Moolenaar9c13b352005-05-19 20:53:52 +00009146 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009147 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009148dialog_msg(char_u *buff, char *format, char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009149{
Bram Moolenaar071d4272004-06-13 20:20:40 +00009150 if (fname == NULL)
9151 fname = (char_u *)_("Untitled");
Bram Moolenaard9462e32011-04-11 21:35:11 +02009152 vim_snprintf((char *)buff, DIALOG_MSG_SIZE, format, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009153}
9154#endif
9155
9156/*
9157 * ":behave {mswin,xterm}"
9158 */
9159 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009160ex_behave(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009161{
9162 if (STRCMP(eap->arg, "mswin") == 0)
9163 {
9164 set_option_value((char_u *)"selection", 0L, (char_u *)"exclusive", 0);
9165 set_option_value((char_u *)"selectmode", 0L, (char_u *)"mouse,key", 0);
9166 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"popup", 0);
9167 set_option_value((char_u *)"keymodel", 0L,
9168 (char_u *)"startsel,stopsel", 0);
9169 }
9170 else if (STRCMP(eap->arg, "xterm") == 0)
9171 {
9172 set_option_value((char_u *)"selection", 0L, (char_u *)"inclusive", 0);
9173 set_option_value((char_u *)"selectmode", 0L, (char_u *)"", 0);
9174 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"extend", 0);
9175 set_option_value((char_u *)"keymodel", 0L, (char_u *)"", 0);
9176 }
9177 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009178 semsg(_(e_invarg2), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009179}
9180
Bram Moolenaar42b4dda2010-03-02 15:56:05 +01009181#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
9182/*
9183 * Function given to ExpandGeneric() to obtain the possible arguments of the
9184 * ":behave {mswin,xterm}" command.
9185 */
9186 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009187get_behave_arg(expand_T *xp UNUSED, int idx)
Bram Moolenaar42b4dda2010-03-02 15:56:05 +01009188{
9189 if (idx == 0)
9190 return (char_u *)"mswin";
9191 if (idx == 1)
9192 return (char_u *)"xterm";
9193 return NULL;
9194}
Bram Moolenaar9e507ca2016-10-15 15:39:39 +02009195
9196/*
9197 * Function given to ExpandGeneric() to obtain the possible arguments of the
9198 * ":messages {clear}" command.
9199 */
9200 char_u *
9201get_messages_arg(expand_T *xp UNUSED, int idx)
9202{
9203 if (idx == 0)
9204 return (char_u *)"clear";
9205 return NULL;
9206}
Bram Moolenaar42b4dda2010-03-02 15:56:05 +01009207#endif
9208
Bram Moolenaar113e1072019-01-20 15:30:40 +01009209#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
Bram Moolenaarcae92dc2017-08-06 15:22:15 +02009210 char_u *
9211get_mapclear_arg(expand_T *xp UNUSED, int idx)
9212{
9213 if (idx == 0)
9214 return (char_u *)"<buffer>";
9215 return NULL;
9216}
Bram Moolenaar113e1072019-01-20 15:30:40 +01009217#endif
Bram Moolenaarcae92dc2017-08-06 15:22:15 +02009218
Bram Moolenaar071d4272004-06-13 20:20:40 +00009219static int filetype_detect = FALSE;
9220static int filetype_plugin = FALSE;
9221static int filetype_indent = FALSE;
9222
9223/*
9224 * ":filetype [plugin] [indent] {on,off,detect}"
9225 * on: Load the filetype.vim file to install autocommands for file types.
9226 * off: Load the ftoff.vim file to remove all autocommands for file types.
9227 * plugin on: load filetype.vim and ftplugin.vim
9228 * plugin off: load ftplugof.vim
9229 * indent on: load filetype.vim and indent.vim
9230 * indent off: load indoff.vim
9231 */
9232 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009233ex_filetype(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009234{
9235 char_u *arg = eap->arg;
9236 int plugin = FALSE;
9237 int indent = FALSE;
9238
9239 if (*eap->arg == NUL)
9240 {
9241 /* Print current status. */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009242 smsg("filetype detection:%s plugin:%s indent:%s",
Bram Moolenaar071d4272004-06-13 20:20:40 +00009243 filetype_detect ? "ON" : "OFF",
9244 filetype_plugin ? (filetype_detect ? "ON" : "(on)") : "OFF",
9245 filetype_indent ? (filetype_detect ? "ON" : "(on)") : "OFF");
9246 return;
9247 }
9248
9249 /* Accept "plugin" and "indent" in any order. */
9250 for (;;)
9251 {
9252 if (STRNCMP(arg, "plugin", 6) == 0)
9253 {
9254 plugin = TRUE;
9255 arg = skipwhite(arg + 6);
9256 continue;
9257 }
9258 if (STRNCMP(arg, "indent", 6) == 0)
9259 {
9260 indent = TRUE;
9261 arg = skipwhite(arg + 6);
9262 continue;
9263 }
9264 break;
9265 }
9266 if (STRCMP(arg, "on") == 0 || STRCMP(arg, "detect") == 0)
9267 {
9268 if (*arg == 'o' || !filetype_detect)
9269 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01009270 source_runtime((char_u *)FILETYPE_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009271 filetype_detect = TRUE;
9272 if (plugin)
9273 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01009274 source_runtime((char_u *)FTPLUGIN_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009275 filetype_plugin = TRUE;
9276 }
9277 if (indent)
9278 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01009279 source_runtime((char_u *)INDENT_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009280 filetype_indent = TRUE;
9281 }
9282 }
9283 if (*arg == 'd')
9284 {
Bram Moolenaar1610d052016-06-09 22:53:01 +02009285 (void)do_doautocmd((char_u *)"filetypedetect BufRead", TRUE, NULL);
Bram Moolenaara3227e22006-03-08 21:32:40 +00009286 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009287 }
9288 }
9289 else if (STRCMP(arg, "off") == 0)
9290 {
9291 if (plugin || indent)
9292 {
9293 if (plugin)
9294 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01009295 source_runtime((char_u *)FTPLUGOF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009296 filetype_plugin = FALSE;
9297 }
9298 if (indent)
9299 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01009300 source_runtime((char_u *)INDOFF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009301 filetype_indent = FALSE;
9302 }
9303 }
9304 else
9305 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01009306 source_runtime((char_u *)FTOFF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009307 filetype_detect = FALSE;
9308 }
9309 }
9310 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009311 semsg(_(e_invarg2), arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009312}
9313
9314/*
Bram Moolenaar3e545692017-06-04 19:00:32 +02009315 * ":setfiletype [FALLBACK] {name}"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009316 */
9317 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009318ex_setfiletype(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009319{
9320 if (!did_filetype)
Bram Moolenaar3e545692017-06-04 19:00:32 +02009321 {
9322 char_u *arg = eap->arg;
9323
9324 if (STRNCMP(arg, "FALLBACK ", 9) == 0)
9325 arg += 9;
9326
9327 set_option_value((char_u *)"filetype", 0L, arg, OPT_LOCAL);
9328 if (arg != eap->arg)
9329 did_filetype = FALSE;
9330 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009331}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009332
Bram Moolenaar071d4272004-06-13 20:20:40 +00009333 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009334ex_digraphs(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009335{
9336#ifdef FEAT_DIGRAPHS
9337 if (*eap->arg != NUL)
9338 putdigraph(eap->arg);
9339 else
Bram Moolenaareae8ae12018-12-14 18:53:02 +01009340 listdigraphs(eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009341#else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009342 emsg(_("E196: No digraphs in this version"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009343#endif
9344}
9345
9346 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009347ex_set(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009348{
9349 int flags = 0;
9350
9351 if (eap->cmdidx == CMD_setlocal)
9352 flags = OPT_LOCAL;
9353 else if (eap->cmdidx == CMD_setglobal)
9354 flags = OPT_GLOBAL;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01009355#if defined(FEAT_EVAL) && defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009356 if (cmdmod.browse && flags == 0)
9357 ex_options(eap);
9358 else
9359#endif
9360 (void)do_set(eap->arg, flags);
9361}
9362
Bram Moolenaar451fc7b2018-04-27 22:53:07 +02009363#if defined(FEAT_SEARCH_EXTRA) || defined(PROTO)
9364 void
9365set_no_hlsearch(int flag)
9366{
9367 no_hlsearch = flag;
9368# ifdef FEAT_EVAL
9369 set_vim_var_nr(VV_HLSEARCH, !no_hlsearch && p_hls);
9370# endif
9371}
9372
Bram Moolenaar071d4272004-06-13 20:20:40 +00009373/*
9374 * ":nohlsearch"
9375 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009376 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009377ex_nohlsearch(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009378{
Bram Moolenaar451fc7b2018-04-27 22:53:07 +02009379 set_no_hlsearch(TRUE);
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00009380 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009381}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009382#endif
9383
9384#ifdef FEAT_CRYPT
9385/*
9386 * ":X": Get crypt key
9387 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009388 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009389ex_X(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009390{
Bram Moolenaar3a0c9082014-11-12 15:15:42 +01009391 crypt_check_current_method();
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02009392 (void)crypt_get_key(TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009393}
9394#endif
9395
9396#ifdef FEAT_FOLDING
9397 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009398ex_fold(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009399{
9400 if (foldManualAllowed(TRUE))
9401 foldCreate(eap->line1, eap->line2);
9402}
9403
9404 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009405ex_foldopen(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009406{
9407 opFoldRange(eap->line1, eap->line2, eap->cmdidx == CMD_foldopen,
9408 eap->forceit, FALSE);
9409}
9410
9411 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009412ex_folddo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009413{
9414 linenr_T lnum;
9415
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02009416#ifdef FEAT_CLIPBOARD
9417 start_global_changes();
9418#endif
9419
Bram Moolenaar071d4272004-06-13 20:20:40 +00009420 /* First set the marks for all lines closed/open. */
9421 for (lnum = eap->line1; lnum <= eap->line2; ++lnum)
9422 if (hasFolding(lnum, NULL, NULL) == (eap->cmdidx == CMD_folddoclosed))
9423 ml_setmarked(lnum);
9424
9425 /* Execute the command on the marked lines. */
9426 global_exe(eap->arg);
9427 ml_clearmarked(); /* clear rest of the marks */
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02009428#ifdef FEAT_CLIPBOARD
9429 end_global_changes();
9430#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009431}
9432#endif
Bram Moolenaarf5291f32017-09-14 22:55:37 +02009433
Bram Moolenaar39665952018-08-15 20:59:48 +02009434#ifdef FEAT_QUICKFIX
9435/*
9436 * Returns TRUE if the supplied Ex cmdidx is for a location list command
9437 * instead of a quickfix command.
9438 */
9439 int
9440is_loclist_cmd(int cmdidx)
9441{
Bram Moolenaar74c8be22018-08-23 22:51:40 +02009442 if (cmdidx < 0 || cmdidx >= CMD_SIZE)
Bram Moolenaar39665952018-08-15 20:59:48 +02009443 return FALSE;
9444 return cmdnames[cmdidx].cmd_name[0] == 'l';
9445}
9446#endif
9447
Bram Moolenaarf5291f32017-09-14 22:55:37 +02009448# if defined(FEAT_TIMERS) || defined(PROTO)
9449 int
9450get_pressedreturn(void)
9451{
9452 return ex_pressedreturn;
9453}
9454
9455 void
9456set_pressedreturn(int val)
9457{
9458 ex_pressedreturn = val;
9459}
9460#endif