blob: aad1bae66f8f18ee20b37a816fe0c8c084f687ae [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 Moolenaarac9fb182019-04-27 13:04:13 +02003326 int compl = EXPAND_NOTHING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003327 int delim;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003328 int forceit = FALSE;
3329 int usefilter = FALSE; /* filter instead of file name */
3330
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003331 ExpandInit(xp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003332 xp->xp_pattern = buff;
3333 xp->xp_context = EXPAND_COMMANDS; /* Default until we get past command */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003334 ea.argt = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003335
3336/*
Bram Moolenaar1c40a662014-11-27 16:38:11 +01003337 * 1. skip comment lines and leading space, colons or bars
Bram Moolenaar071d4272004-06-13 20:20:40 +00003338 */
3339 for (cmd = buff; vim_strchr((char_u *)" \t:|", *cmd) != NULL; cmd++)
3340 ;
3341 xp->xp_pattern = cmd;
3342
3343 if (*cmd == NUL)
3344 return NULL;
3345 if (*cmd == '"') /* ignore comment lines */
3346 {
3347 xp->xp_context = EXPAND_NOTHING;
3348 return NULL;
3349 }
3350
3351/*
Bram Moolenaar1c40a662014-11-27 16:38:11 +01003352 * 3. Skip over the range to find the command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003353 */
3354 cmd = skip_range(cmd, &xp->xp_context);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003355 xp->xp_pattern = cmd;
3356 if (*cmd == NUL)
3357 return NULL;
3358 if (*cmd == '"')
3359 {
3360 xp->xp_context = EXPAND_NOTHING;
3361 return NULL;
3362 }
3363
3364 if (*cmd == '|' || *cmd == '\n')
3365 return cmd + 1; /* There's another command */
3366
3367 /*
3368 * Isolate the command and search for it in the command table.
3369 * Exceptions:
3370 * - the 'k' command can directly be followed by any character, but
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003371 * do accept "keepmarks", "keepalt" and "keepjumps".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003372 * - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
3373 */
3374 if (*cmd == 'k' && cmd[1] != 'e')
3375 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003376 ea.cmdidx = CMD_k;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003377 p = cmd + 1;
3378 }
3379 else
3380 {
3381 p = cmd;
3382 while (ASCII_ISALPHA(*p) || *p == '*') /* Allow * wild card */
3383 ++p;
Bram Moolenaar23d1b622015-10-13 19:18:04 +02003384 /* a user command may contain digits */
3385 if (ASCII_ISUPPER(cmd[0]))
3386 while (ASCII_ISALNUM(*p) || *p == '*')
3387 ++p;
Bram Moolenaar9f5d6002013-06-02 19:22:13 +02003388 /* for python 3.x: ":py3*" commands completion */
3389 if (cmd[0] == 'p' && cmd[1] == 'y' && p == cmd + 2 && *p == '3')
Bram Moolenaar893b2d72013-12-11 17:44:38 +01003390 {
Bram Moolenaar9f5d6002013-06-02 19:22:13 +02003391 ++p;
Bram Moolenaar893b2d72013-12-11 17:44:38 +01003392 while (ASCII_ISALPHA(*p) || *p == '*')
3393 ++p;
3394 }
Bram Moolenaar23d1b622015-10-13 19:18:04 +02003395 /* check for non-alpha command */
3396 if (p == cmd && vim_strchr((char_u *)"@*!=><&~#", *p) != NULL)
3397 ++p;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003398 len = (int)(p - cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003399
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003400 if (len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003401 {
3402 xp->xp_context = EXPAND_UNSUCCESSFUL;
3403 return NULL;
3404 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003405 for (ea.cmdidx = (cmdidx_T)0; (int)ea.cmdidx < (int)CMD_SIZE;
Bram Moolenaar42b4dda2010-03-02 15:56:05 +01003406 ea.cmdidx = (cmdidx_T)((int)ea.cmdidx + 1))
3407 if (STRNCMP(cmdnames[(int)ea.cmdidx].cmd_name, cmd,
3408 (size_t)len) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003409 break;
3410
Bram Moolenaar071d4272004-06-13 20:20:40 +00003411 if (cmd[0] >= 'A' && cmd[0] <= 'Z')
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003412 while (ASCII_ISALNUM(*p) || *p == '*') // Allow * wild card
Bram Moolenaar071d4272004-06-13 20:20:40 +00003413 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003414 }
3415
3416 /*
3417 * If the cursor is touching the command, and it ends in an alpha-numeric
3418 * character, complete the command name.
3419 */
3420 if (*p == NUL && ASCII_ISALNUM(p[-1]))
3421 return NULL;
3422
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003423 if (ea.cmdidx == CMD_SIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003424 {
3425 if (*cmd == 's' && vim_strchr((char_u *)"cgriI", cmd[1]) != NULL)
3426 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003427 ea.cmdidx = CMD_substitute;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003428 p = cmd + 1;
3429 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003430 else if (cmd[0] >= 'A' && cmd[0] <= 'Z')
3431 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003432 ea.cmd = cmd;
Bram Moolenaar0a52df52019-08-18 22:26:31 +02003433 p = find_ucmd(&ea, p, NULL, xp, &compl);
Bram Moolenaarebefac62005-12-28 22:39:57 +00003434 if (p == NULL)
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003435 ea.cmdidx = CMD_SIZE; // ambiguous user command
Bram Moolenaar071d4272004-06-13 20:20:40 +00003436 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003437 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003438 if (ea.cmdidx == CMD_SIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003439 {
3440 /* Not still touching the command and it was an illegal one */
3441 xp->xp_context = EXPAND_UNSUCCESSFUL;
3442 return NULL;
3443 }
3444
3445 xp->xp_context = EXPAND_NOTHING; /* Default now that we're past command */
3446
3447 if (*p == '!') /* forced commands */
3448 {
3449 forceit = TRUE;
3450 ++p;
3451 }
3452
3453/*
Bram Moolenaar1c40a662014-11-27 16:38:11 +01003454 * 6. parse arguments
Bram Moolenaar071d4272004-06-13 20:20:40 +00003455 */
Bram Moolenaar958636c2014-10-21 20:01:58 +02003456 if (!IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003457 ea.argt = (long)cmdnames[(int)ea.cmdidx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003458
3459 arg = skipwhite(p);
3460
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003461 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003462 {
3463 if (*arg == '>') /* append */
3464 {
3465 if (*++arg == '>')
3466 ++arg;
3467 arg = skipwhite(arg);
3468 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003469 else if (*arg == '!' && ea.cmdidx == CMD_write) /* :w !filter */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003470 {
3471 ++arg;
3472 usefilter = TRUE;
3473 }
3474 }
3475
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003476 if (ea.cmdidx == CMD_read)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003477 {
3478 usefilter = forceit; /* :r! filter if forced */
3479 if (*arg == '!') /* :r !filter */
3480 {
3481 ++arg;
3482 usefilter = TRUE;
3483 }
3484 }
3485
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003486 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003487 {
3488 while (*arg == *cmd) /* allow any number of '>' or '<' */
3489 ++arg;
3490 arg = skipwhite(arg);
3491 }
3492
3493 /* Does command allow "+command"? */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02003494 if ((ea.argt & EX_CMDARG) && !usefilter && *arg == '+')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003495 {
3496 /* Check if we're in the +command */
3497 p = arg + 1;
3498 arg = skip_cmd_arg(arg, FALSE);
3499
3500 /* Still touching the command after '+'? */
3501 if (*arg == NUL)
3502 return p;
3503
3504 /* Skip space(s) after +command to get to the real argument */
3505 arg = skipwhite(arg);
3506 }
3507
3508 /*
3509 * Check for '|' to separate commands and '"' to start comments.
3510 * Don't do this for ":read !cmd" and ":write !cmd".
3511 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02003512 if ((ea.argt & EX_TRLBAR) && !usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003513 {
3514 p = arg;
3515 /* ":redir @" is not the start of a comment */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003516 if (ea.cmdidx == CMD_redir && p[0] == '@' && p[1] == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003517 p += 2;
3518 while (*p)
3519 {
3520 if (*p == Ctrl_V)
3521 {
3522 if (p[1] != NUL)
3523 ++p;
3524 }
Bram Moolenaar8071cb22019-07-12 17:58:01 +02003525 else if ( (*p == '"' && !(ea.argt & EX_NOTRLCOM))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003526 || *p == '|' || *p == '\n')
3527 {
3528 if (*(p - 1) != '\\')
3529 {
3530 if (*p == '|' || *p == '\n')
3531 return p + 1;
3532 return NULL; /* It's a comment */
3533 }
3534 }
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003535 MB_PTR_ADV(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003536 }
3537 }
3538
Bram Moolenaar8071cb22019-07-12 17:58:01 +02003539 if (!(ea.argt & EX_EXTRA) && *arg != NUL
3540 && vim_strchr((char_u *)"|\"", *arg) == NULL)
3541 // no arguments allowed but there is something
Bram Moolenaar071d4272004-06-13 20:20:40 +00003542 return NULL;
3543
3544 /* Find start of last argument (argument just before cursor): */
Bram Moolenaar848f8762012-07-25 17:22:23 +02003545 p = buff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003546 xp->xp_pattern = p;
Bram Moolenaar09168a72012-08-02 21:24:42 +02003547 len = (int)STRLEN(buff);
Bram Moolenaar848f8762012-07-25 17:22:23 +02003548 while (*p && p < buff + len)
3549 {
3550 if (*p == ' ' || *p == TAB)
3551 {
3552 /* argument starts after a space */
3553 xp->xp_pattern = ++p;
3554 }
3555 else
3556 {
3557 if (*p == '\\' && *(p + 1) != NUL)
3558 ++p; /* skip over escaped character */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003559 MB_PTR_ADV(p);
Bram Moolenaar848f8762012-07-25 17:22:23 +02003560 }
3561 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003562
Bram Moolenaar8071cb22019-07-12 17:58:01 +02003563 if (ea.argt & EX_XFILE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003564 {
Bram Moolenaar6529c102007-08-18 15:47:34 +00003565 int c;
3566 int in_quote = FALSE;
3567 char_u *bow = NULL; /* Beginning of word */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003568
3569 /*
3570 * Allow spaces within back-quotes to count as part of the argument
3571 * being expanded.
3572 */
3573 xp->xp_pattern = skipwhite(arg);
Bram Moolenaar6529c102007-08-18 15:47:34 +00003574 p = xp->xp_pattern;
3575 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003576 {
Bram Moolenaar6529c102007-08-18 15:47:34 +00003577 if (has_mbyte)
3578 c = mb_ptr2char(p);
3579 else
Bram Moolenaar6529c102007-08-18 15:47:34 +00003580 c = *p;
3581 if (c == '\\' && p[1] != NUL)
3582 ++p;
3583 else if (c == '`')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003584 {
3585 if (!in_quote)
3586 {
3587 xp->xp_pattern = p;
3588 bow = p + 1;
3589 }
3590 in_quote = !in_quote;
3591 }
Bram Moolenaar332fa0c2008-01-13 16:12:10 +00003592 /* An argument can contain just about everything, except
3593 * characters that end the command and white space. */
Bram Moolenaar1c465442017-03-12 20:10:05 +01003594 else if (c == '|' || c == '\n' || c == '"' || (VIM_ISWHITE(c)
Bram Moolenaar6529c102007-08-18 15:47:34 +00003595#ifdef SPACE_IN_FILENAME
Bram Moolenaar8071cb22019-07-12 17:58:01 +02003596 && (!(ea.argt & EX_NOSPC) || usefilter)
Bram Moolenaar6529c102007-08-18 15:47:34 +00003597#endif
Bram Moolenaar332fa0c2008-01-13 16:12:10 +00003598 ))
Bram Moolenaar6529c102007-08-18 15:47:34 +00003599 {
Bram Moolenaarcf5a5b82008-02-26 20:30:12 +00003600 len = 0; /* avoid getting stuck when space is in 'isfname' */
Bram Moolenaar6529c102007-08-18 15:47:34 +00003601 while (*p != NUL)
3602 {
Bram Moolenaar6529c102007-08-18 15:47:34 +00003603 if (has_mbyte)
3604 c = mb_ptr2char(p);
3605 else
Bram Moolenaar6529c102007-08-18 15:47:34 +00003606 c = *p;
Bram Moolenaardd87969c2007-08-21 13:07:12 +00003607 if (c == '`' || vim_isfilec_or_wc(c))
Bram Moolenaar6529c102007-08-18 15:47:34 +00003608 break;
Bram Moolenaar6529c102007-08-18 15:47:34 +00003609 if (has_mbyte)
3610 len = (*mb_ptr2len)(p);
3611 else
Bram Moolenaar6529c102007-08-18 15:47:34 +00003612 len = 1;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003613 MB_PTR_ADV(p);
Bram Moolenaar6529c102007-08-18 15:47:34 +00003614 }
3615 if (in_quote)
3616 bow = p;
3617 else
3618 xp->xp_pattern = p;
3619 p -= len;
3620 }
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003621 MB_PTR_ADV(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003622 }
3623
3624 /*
3625 * If we are still inside the quotes, and we passed a space, just
3626 * expand from there.
3627 */
3628 if (bow != NULL && in_quote)
3629 xp->xp_pattern = bow;
3630 xp->xp_context = EXPAND_FILES;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003631
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003632 /* For a shell command more chars need to be escaped. */
Bram Moolenaar67883b42017-07-27 22:57:00 +02003633 if (usefilter || ea.cmdidx == CMD_bang || ea.cmdidx == CMD_terminal)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003634 {
Bram Moolenaarc0cba4d2010-08-07 17:07:21 +02003635#ifndef BACKSLASH_IN_FILENAME
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003636 xp->xp_shell = TRUE;
Bram Moolenaarc0cba4d2010-08-07 17:07:21 +02003637#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003638 /* When still after the command name expand executables. */
3639 if (xp->xp_pattern == skipwhite(arg))
Bram Moolenaar5c5b0942007-05-06 12:07:59 +00003640 xp->xp_context = EXPAND_SHELLCMD;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003641 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003642
3643 /* Check for environment variable */
3644 if (*xp->xp_pattern == '$'
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003645#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003646 || *xp->xp_pattern == '%'
3647#endif
3648 )
3649 {
3650 for (p = xp->xp_pattern + 1; *p != NUL; ++p)
3651 if (!vim_isIDc(*p))
3652 break;
3653 if (*p == NUL)
3654 {
3655 xp->xp_context = EXPAND_ENV_VARS;
3656 ++xp->xp_pattern;
Bram Moolenaar21cf8232004-07-16 20:18:37 +00003657 /* Avoid that the assignment uses EXPAND_FILES again. */
Bram Moolenaara466c992005-07-09 21:03:22 +00003658 if (compl != EXPAND_USER_DEFINED && compl != EXPAND_USER_LIST)
Bram Moolenaar21cf8232004-07-16 20:18:37 +00003659 compl = EXPAND_ENV_VARS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003660 }
3661 }
Bram Moolenaar24305862012-08-15 14:05:05 +02003662 /* Check for user names */
3663 if (*xp->xp_pattern == '~')
3664 {
3665 for (p = xp->xp_pattern + 1; *p != NUL && *p != '/'; ++p)
3666 ;
3667 /* Complete ~user only if it partially matches a user name.
3668 * A full match ~user<Tab> will be replaced by user's home
3669 * directory i.e. something like ~user<Tab> -> /home/user/ */
3670 if (*p == NUL && p > xp->xp_pattern + 1
Bram Moolenaar6c5d1042018-07-07 16:41:13 +02003671 && match_user(xp->xp_pattern + 1) >= 1)
Bram Moolenaar24305862012-08-15 14:05:05 +02003672 {
3673 xp->xp_context = EXPAND_USER;
3674 ++xp->xp_pattern;
3675 }
3676 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003677 }
3678
3679/*
Bram Moolenaar1c40a662014-11-27 16:38:11 +01003680 * 6. Switch on command name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003681 */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003682 switch (ea.cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003683 {
Bram Moolenaarcc448b32010-07-14 16:52:17 +02003684 case CMD_find:
3685 case CMD_sfind:
3686 case CMD_tabfind:
Bram Moolenaarc24b6972010-08-16 22:34:29 +02003687 if (xp->xp_context == EXPAND_FILES)
3688 xp->xp_context = EXPAND_FILES_IN_PATH;
Bram Moolenaarcc448b32010-07-14 16:52:17 +02003689 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003690 case CMD_cd:
3691 case CMD_chdir:
Bram Moolenaar00aa0692019-04-27 20:37:57 +02003692 case CMD_tcd:
3693 case CMD_tchdir:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003694 case CMD_lcd:
3695 case CMD_lchdir:
3696 if (xp->xp_context == EXPAND_FILES)
3697 xp->xp_context = EXPAND_DIRECTORIES;
3698 break;
3699 case CMD_help:
3700 xp->xp_context = EXPAND_HELP;
3701 xp->xp_pattern = arg;
3702 break;
3703
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003704 /* Command modifiers: return the argument.
3705 * Also for commands with an argument that is a command. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003706 case CMD_aboveleft:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003707 case CMD_argdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003708 case CMD_belowright:
3709 case CMD_botright:
3710 case CMD_browse:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003711 case CMD_bufdo:
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003712 case CMD_cdo:
3713 case CMD_cfdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003714 case CMD_confirm:
Bram Moolenaardf177f62005-02-22 08:39:57 +00003715 case CMD_debug:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003716 case CMD_folddoclosed:
3717 case CMD_folddoopen:
3718 case CMD_hide:
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003719 case CMD_keepalt:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003720 case CMD_keepjumps:
3721 case CMD_keepmarks:
Bram Moolenaara939e432013-11-09 05:30:26 +01003722 case CMD_keeppatterns:
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003723 case CMD_ldo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003724 case CMD_leftabove:
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003725 case CMD_lfdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003726 case CMD_lockmarks:
Bram Moolenaar5803ae62014-03-23 16:04:02 +01003727 case CMD_noautocmd:
3728 case CMD_noswapfile:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003729 case CMD_rightbelow:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003730 case CMD_sandbox:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003731 case CMD_silent:
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003732 case CMD_tab:
Bram Moolenaar70baa402013-06-16 16:14:03 +02003733 case CMD_tabdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003734 case CMD_topleft:
3735 case CMD_verbose:
3736 case CMD_vertical:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003737 case CMD_windo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003738 return arg;
3739
Bram Moolenaar7069bf12017-01-07 20:39:53 +01003740 case CMD_filter:
3741 if (*arg != NUL)
3742 arg = skip_vimgrep_pat(arg, NULL, NULL);
3743 if (arg == NULL || *arg == NUL)
3744 {
3745 xp->xp_context = EXPAND_NOTHING;
3746 return NULL;
3747 }
3748 return skipwhite(arg);
3749
Bram Moolenaar0a52df52019-08-18 22:26:31 +02003750#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar071d4272004-06-13 20:20:40 +00003751 case CMD_match:
3752 if (*arg == NUL || !ends_excmd(*arg))
3753 {
Bram Moolenaar4f688582007-07-24 12:34:30 +00003754 /* also complete "None" */
3755 set_context_in_echohl_cmd(xp, arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003756 arg = skipwhite(skiptowhite(arg));
3757 if (*arg != NUL)
3758 {
3759 xp->xp_context = EXPAND_NOTHING;
3760 arg = skip_regexp(arg + 1, *arg, p_magic, NULL);
3761 }
3762 }
3763 return find_nextcmd(arg);
Bram Moolenaar0a52df52019-08-18 22:26:31 +02003764#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003765
Bram Moolenaar071d4272004-06-13 20:20:40 +00003766/*
3767 * All completion for the +cmdline_compl feature goes here.
3768 */
3769
Bram Moolenaar071d4272004-06-13 20:20:40 +00003770 case CMD_command:
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003771 return set_context_in_user_cmd(xp, arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003772
3773 case CMD_delcommand:
3774 xp->xp_context = EXPAND_USER_COMMANDS;
3775 xp->xp_pattern = arg;
3776 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003777
3778 case CMD_global:
3779 case CMD_vglobal:
3780 delim = *arg; /* get the delimiter */
3781 if (delim)
3782 ++arg; /* skip delimiter if there is one */
3783
3784 while (arg[0] != NUL && arg[0] != delim)
3785 {
3786 if (arg[0] == '\\' && arg[1] != NUL)
3787 ++arg;
3788 ++arg;
3789 }
3790 if (arg[0] != NUL)
3791 return arg + 1;
3792 break;
3793 case CMD_and:
3794 case CMD_substitute:
3795 delim = *arg;
3796 if (delim)
3797 {
3798 /* skip "from" part */
3799 ++arg;
3800 arg = skip_regexp(arg, delim, p_magic, NULL);
3801 }
3802 /* skip "to" part */
3803 while (arg[0] != NUL && arg[0] != delim)
3804 {
3805 if (arg[0] == '\\' && arg[1] != NUL)
3806 ++arg;
3807 ++arg;
3808 }
3809 if (arg[0] != NUL) /* skip delimiter */
3810 ++arg;
3811 while (arg[0] && vim_strchr((char_u *)"|\"#", arg[0]) == NULL)
3812 ++arg;
3813 if (arg[0] != NUL)
3814 return arg;
3815 break;
3816 case CMD_isearch:
3817 case CMD_dsearch:
3818 case CMD_ilist:
3819 case CMD_dlist:
3820 case CMD_ijump:
3821 case CMD_psearch:
3822 case CMD_djump:
3823 case CMD_isplit:
3824 case CMD_dsplit:
3825 arg = skipwhite(skipdigits(arg)); /* skip count */
3826 if (*arg == '/') /* Match regexp, not just whole words */
3827 {
3828 for (++arg; *arg && *arg != '/'; arg++)
3829 if (*arg == '\\' && arg[1] != NUL)
3830 arg++;
3831 if (*arg)
3832 {
3833 arg = skipwhite(arg + 1);
3834
3835 /* Check for trailing illegal characters */
3836 if (*arg && vim_strchr((char_u *)"|\"\n", *arg) == NULL)
3837 xp->xp_context = EXPAND_NOTHING;
3838 else
3839 return arg;
3840 }
3841 }
3842 break;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003843
Bram Moolenaar071d4272004-06-13 20:20:40 +00003844 case CMD_autocmd:
3845 return set_context_in_autocmd(xp, arg, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003846 case CMD_doautocmd:
Bram Moolenaar73a9d7b2008-11-06 16:16:44 +00003847 case CMD_doautoall:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003848 return set_context_in_autocmd(xp, arg, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003849 case CMD_set:
3850 set_context_in_set_cmd(xp, arg, 0);
3851 break;
3852 case CMD_setglobal:
3853 set_context_in_set_cmd(xp, arg, OPT_GLOBAL);
3854 break;
3855 case CMD_setlocal:
3856 set_context_in_set_cmd(xp, arg, OPT_LOCAL);
3857 break;
3858 case CMD_tag:
3859 case CMD_stag:
3860 case CMD_ptag:
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00003861 case CMD_ltag:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003862 case CMD_tselect:
3863 case CMD_stselect:
3864 case CMD_ptselect:
3865 case CMD_tjump:
3866 case CMD_stjump:
3867 case CMD_ptjump:
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00003868 if (*p_wop != NUL)
3869 xp->xp_context = EXPAND_TAGS_LISTFILES;
3870 else
3871 xp->xp_context = EXPAND_TAGS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003872 xp->xp_pattern = arg;
3873 break;
3874 case CMD_augroup:
3875 xp->xp_context = EXPAND_AUGROUP;
3876 xp->xp_pattern = arg;
3877 break;
3878#ifdef FEAT_SYN_HL
3879 case CMD_syntax:
3880 set_context_in_syntax_cmd(xp, arg);
3881 break;
3882#endif
3883#ifdef FEAT_EVAL
3884 case CMD_let:
3885 case CMD_if:
3886 case CMD_elseif:
3887 case CMD_while:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00003888 case CMD_for:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003889 case CMD_echo:
3890 case CMD_echon:
3891 case CMD_execute:
3892 case CMD_echomsg:
3893 case CMD_echoerr:
3894 case CMD_call:
3895 case CMD_return:
Bram Moolenaar2b2207b2017-01-22 16:46:56 +01003896 case CMD_cexpr:
3897 case CMD_caddexpr:
3898 case CMD_cgetexpr:
3899 case CMD_lexpr:
3900 case CMD_laddexpr:
3901 case CMD_lgetexpr:
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003902 set_context_for_expression(xp, arg, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003903 break;
3904
3905 case CMD_unlet:
3906 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
3907 arg = xp->xp_pattern + 1;
Bram Moolenaar19834012018-06-12 17:03:39 +02003908
Bram Moolenaar071d4272004-06-13 20:20:40 +00003909 xp->xp_context = EXPAND_USER_VARS;
3910 xp->xp_pattern = arg;
Bram Moolenaar19834012018-06-12 17:03:39 +02003911
3912 if (*xp->xp_pattern == '$')
3913 {
3914 xp->xp_context = EXPAND_ENV_VARS;
3915 ++xp->xp_pattern;
3916 }
3917
Bram Moolenaar071d4272004-06-13 20:20:40 +00003918 break;
3919
3920 case CMD_function:
3921 case CMD_delfunction:
3922 xp->xp_context = EXPAND_USER_FUNC;
3923 xp->xp_pattern = arg;
3924 break;
3925
3926 case CMD_echohl:
Bram Moolenaar4f688582007-07-24 12:34:30 +00003927 set_context_in_echohl_cmd(xp, arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003928 break;
3929#endif
3930 case CMD_highlight:
3931 set_context_in_highlight_cmd(xp, arg);
3932 break;
Bram Moolenaarf4580d82009-03-18 11:52:53 +00003933#ifdef FEAT_CSCOPE
3934 case CMD_cscope:
Bram Moolenaar7bfef802009-04-22 14:25:01 +00003935 case CMD_lcscope:
3936 case CMD_scscope:
3937 set_context_in_cscope_cmd(xp, arg, ea.cmdidx);
Bram Moolenaarf4580d82009-03-18 11:52:53 +00003938 break;
3939#endif
Bram Moolenaar3c65e312009-04-29 16:47:23 +00003940#ifdef FEAT_SIGNS
3941 case CMD_sign:
3942 set_context_in_sign_cmd(xp, arg);
3943 break;
3944#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003945 case CMD_bdelete:
3946 case CMD_bwipeout:
3947 case CMD_bunload:
3948 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
3949 arg = xp->xp_pattern + 1;
Bram Moolenaar2f40d122017-10-24 21:49:36 +02003950 /* FALLTHROUGH */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003951 case CMD_buffer:
3952 case CMD_sbuffer:
3953 case CMD_checktime:
3954 xp->xp_context = EXPAND_BUFFERS;
3955 xp->xp_pattern = arg;
3956 break;
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003957
Bram Moolenaar071d4272004-06-13 20:20:40 +00003958 case CMD_USER:
3959 case CMD_USER_BUF:
3960 if (compl != EXPAND_NOTHING)
3961 {
Bram Moolenaar8071cb22019-07-12 17:58:01 +02003962 // EX_XFILE: file names are handled above
3963 if (!(ea.argt & EX_XFILE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003964 {
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003965#ifdef FEAT_MENU
Bram Moolenaar071d4272004-06-13 20:20:40 +00003966 if (compl == EXPAND_MENUS)
3967 return set_context_in_menu_cmd(xp, cmd, arg, forceit);
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003968#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003969 if (compl == EXPAND_COMMANDS)
3970 return arg;
3971 if (compl == EXPAND_MAPPINGS)
3972 return set_context_in_map_cmd(xp, (char_u *)"map",
3973 arg, forceit, FALSE, FALSE, CMD_map);
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003974 // Find start of last argument.
Bram Moolenaar848f8762012-07-25 17:22:23 +02003975 p = arg;
3976 while (*p)
3977 {
3978 if (*p == ' ')
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003979 // argument starts after a space
Bram Moolenaar848f8762012-07-25 17:22:23 +02003980 arg = p + 1;
Bram Moolenaara07c8312012-07-27 21:12:07 +02003981 else if (*p == '\\' && *(p + 1) != NUL)
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003982 ++p; // skip over escaped character
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003983 MB_PTR_ADV(p);
Bram Moolenaar848f8762012-07-25 17:22:23 +02003984 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003985 xp->xp_pattern = arg;
3986 }
3987 xp->xp_context = compl;
3988 }
3989 break;
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003990
Bram Moolenaar071d4272004-06-13 20:20:40 +00003991 case CMD_map: case CMD_noremap:
3992 case CMD_nmap: case CMD_nnoremap:
3993 case CMD_vmap: case CMD_vnoremap:
3994 case CMD_omap: case CMD_onoremap:
3995 case CMD_imap: case CMD_inoremap:
3996 case CMD_cmap: case CMD_cnoremap:
Bram Moolenaar22b306f2010-07-25 13:50:33 +02003997 case CMD_lmap: case CMD_lnoremap:
Bram Moolenaar09bb33d2013-05-07 05:18:20 +02003998 case CMD_smap: case CMD_snoremap:
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +02003999 case CMD_tmap: case CMD_tnoremap:
Bram Moolenaar09bb33d2013-05-07 05:18:20 +02004000 case CMD_xmap: case CMD_xnoremap:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004001 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004002 FALSE, FALSE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004003 case CMD_unmap:
4004 case CMD_nunmap:
4005 case CMD_vunmap:
4006 case CMD_ounmap:
4007 case CMD_iunmap:
4008 case CMD_cunmap:
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004009 case CMD_lunmap:
Bram Moolenaar09bb33d2013-05-07 05:18:20 +02004010 case CMD_sunmap:
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +02004011 case CMD_tunmap:
Bram Moolenaar09bb33d2013-05-07 05:18:20 +02004012 case CMD_xunmap:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004013 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004014 FALSE, TRUE, ea.cmdidx);
Bram Moolenaarcae92dc2017-08-06 15:22:15 +02004015 case CMD_mapclear:
4016 case CMD_nmapclear:
4017 case CMD_vmapclear:
4018 case CMD_omapclear:
4019 case CMD_imapclear:
4020 case CMD_cmapclear:
4021 case CMD_lmapclear:
4022 case CMD_smapclear:
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +02004023 case CMD_tmapclear:
Bram Moolenaarcae92dc2017-08-06 15:22:15 +02004024 case CMD_xmapclear:
4025 xp->xp_context = EXPAND_MAPCLEAR;
4026 xp->xp_pattern = arg;
4027 break;
4028
Bram Moolenaar071d4272004-06-13 20:20:40 +00004029 case CMD_abbreviate: case CMD_noreabbrev:
4030 case CMD_cabbrev: case CMD_cnoreabbrev:
4031 case CMD_iabbrev: case CMD_inoreabbrev:
4032 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004033 TRUE, FALSE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004034 case CMD_unabbreviate:
4035 case CMD_cunabbrev:
4036 case CMD_iunabbrev:
4037 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004038 TRUE, TRUE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004039#ifdef FEAT_MENU
4040 case CMD_menu: case CMD_noremenu: case CMD_unmenu:
4041 case CMD_amenu: case CMD_anoremenu: case CMD_aunmenu:
4042 case CMD_nmenu: case CMD_nnoremenu: case CMD_nunmenu:
4043 case CMD_vmenu: case CMD_vnoremenu: case CMD_vunmenu:
4044 case CMD_omenu: case CMD_onoremenu: case CMD_ounmenu:
4045 case CMD_imenu: case CMD_inoremenu: case CMD_iunmenu:
4046 case CMD_cmenu: case CMD_cnoremenu: case CMD_cunmenu:
Bram Moolenaar4c5d8152018-10-19 22:36:53 +02004047 case CMD_tlmenu: case CMD_tlnoremenu: case CMD_tlunmenu:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004048 case CMD_tmenu: case CMD_tunmenu:
4049 case CMD_popup: case CMD_tearoff: case CMD_emenu:
4050 return set_context_in_menu_cmd(xp, cmd, arg, forceit);
4051#endif
4052
4053 case CMD_colorscheme:
4054 xp->xp_context = EXPAND_COLORS;
4055 xp->xp_pattern = arg;
4056 break;
4057
4058 case CMD_compiler:
4059 xp->xp_context = EXPAND_COMPILER;
4060 xp->xp_pattern = arg;
4061 break;
4062
Bram Moolenaar883f5d02010-06-21 06:24:34 +02004063 case CMD_ownsyntax:
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02004064 xp->xp_context = EXPAND_OWNSYNTAX;
4065 xp->xp_pattern = arg;
4066 break;
4067
4068 case CMD_setfiletype:
Bram Moolenaar883f5d02010-06-21 06:24:34 +02004069 xp->xp_context = EXPAND_FILETYPE;
4070 xp->xp_pattern = arg;
4071 break;
4072
Bram Moolenaar35ca0e72016-03-05 17:41:49 +01004073 case CMD_packadd:
4074 xp->xp_context = EXPAND_PACKADD;
4075 xp->xp_pattern = arg;
4076 break;
4077
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01004078#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004079 case CMD_language:
Bram Moolenaara660dc82011-05-25 12:51:22 +02004080 p = skiptowhite(arg);
4081 if (*p == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004082 {
4083 xp->xp_context = EXPAND_LANGUAGE;
4084 xp->xp_pattern = arg;
4085 }
4086 else
Bram Moolenaara660dc82011-05-25 12:51:22 +02004087 {
4088 if ( STRNCMP(arg, "messages", p - arg) == 0
4089 || STRNCMP(arg, "ctype", p - arg) == 0
4090 || STRNCMP(arg, "time", p - arg) == 0)
4091 {
4092 xp->xp_context = EXPAND_LOCALES;
4093 xp->xp_pattern = skipwhite(p);
4094 }
4095 else
4096 xp->xp_context = EXPAND_NOTHING;
4097 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004098 break;
4099#endif
Bram Moolenaarf86f26c2010-02-03 15:14:22 +01004100#if defined(FEAT_PROFILE)
4101 case CMD_profile:
4102 set_context_in_profile_cmd(xp, arg);
4103 break;
4104#endif
Bram Moolenaar42b4dda2010-03-02 15:56:05 +01004105 case CMD_behave:
4106 xp->xp_context = EXPAND_BEHAVE;
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02004107 xp->xp_pattern = arg;
Bram Moolenaar42b4dda2010-03-02 15:56:05 +01004108 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004109
Bram Moolenaar9e507ca2016-10-15 15:39:39 +02004110 case CMD_messages:
4111 xp->xp_context = EXPAND_MESSAGES;
4112 xp->xp_pattern = arg;
4113 break;
4114
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02004115 case CMD_history:
4116 xp->xp_context = EXPAND_HISTORY;
4117 xp->xp_pattern = arg;
4118 break;
Bram Moolenaarcd9c4622013-06-08 15:24:48 +02004119#if defined(FEAT_PROFILE)
4120 case CMD_syntime:
4121 xp->xp_context = EXPAND_SYNTIME;
4122 xp->xp_pattern = arg;
4123 break;
4124#endif
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02004125
Bram Moolenaarcd43eff2018-03-29 15:55:38 +02004126 case CMD_argdelete:
4127 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
4128 arg = xp->xp_pattern + 1;
4129 xp->xp_context = EXPAND_ARGLIST;
4130 xp->xp_pattern = arg;
4131 break;
4132
Bram Moolenaar071d4272004-06-13 20:20:40 +00004133 default:
4134 break;
4135 }
4136 return NULL;
4137}
4138
4139/*
Bram Moolenaaree8415b2018-08-10 23:13:12 +02004140 * Skip a range specifier of the form: addr [,addr] [;addr] ..
Bram Moolenaar071d4272004-06-13 20:20:40 +00004141 *
4142 * Backslashed delimiters after / or ? will be skipped, and commands will
4143 * not be expanded between /'s and ?'s or after "'".
4144 *
Bram Moolenaardf177f62005-02-22 08:39:57 +00004145 * Also skip white space and ":" characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004146 * Returns the "cmd" pointer advanced to beyond the range.
4147 */
4148 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004149skip_range(
4150 char_u *cmd,
4151 int *ctx) /* pointer to xp_context or NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004152{
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004153 unsigned delim;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004154
Bram Moolenaarcbf20fb2017-02-03 21:19:04 +01004155 while (vim_strchr((char_u *)" \t0123456789.$%'/?-+,;\\", *cmd) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004156 {
Bram Moolenaarcbf20fb2017-02-03 21:19:04 +01004157 if (*cmd == '\\')
4158 {
4159 if (cmd[1] == '?' || cmd[1] == '/' || cmd[1] == '&')
4160 ++cmd;
4161 else
4162 break;
4163 }
4164 else if (*cmd == '\'')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004165 {
4166 if (*++cmd == NUL && ctx != NULL)
4167 *ctx = EXPAND_NOTHING;
4168 }
4169 else if (*cmd == '/' || *cmd == '?')
4170 {
4171 delim = *cmd++;
4172 while (*cmd != NUL && *cmd != delim)
4173 if (*cmd++ == '\\' && *cmd != NUL)
4174 ++cmd;
4175 if (*cmd == NUL && ctx != NULL)
4176 *ctx = EXPAND_NOTHING;
4177 }
4178 if (*cmd != NUL)
4179 ++cmd;
4180 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00004181
4182 /* Skip ":" and white space. */
4183 while (*cmd == ':')
4184 cmd = skipwhite(cmd + 1);
4185
Bram Moolenaar071d4272004-06-13 20:20:40 +00004186 return cmd;
4187}
4188
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004189 static void
4190addr_error(cmd_addr_T addr_type)
4191{
4192 if (addr_type == ADDR_NONE)
4193 emsg(_(e_norange));
4194 else
4195 emsg(_(e_invrange));
4196}
4197
Bram Moolenaar071d4272004-06-13 20:20:40 +00004198/*
Bram Moolenaar198cb662018-09-06 21:44:17 +02004199 * Get a single EX address.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004200 *
4201 * Set ptr to the next character after the part that was interpreted.
4202 * Set ptr to NULL when an error is encountered.
Bram Moolenaar198cb662018-09-06 21:44:17 +02004203 * This may set the last used search pattern.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004204 *
4205 * Return MAXLNUM when no Ex address was found.
4206 */
4207 static linenr_T
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004208get_address(
4209 exarg_T *eap UNUSED,
4210 char_u **ptr,
Bram Moolenaarb7316892019-05-01 18:08:42 +02004211 cmd_addr_T addr_type_arg,
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02004212 int skip, // only skip the address, don't use it
4213 int silent, // no errors or side effects
4214 int to_other_file, // flag: may jump to other file
4215 int address_count UNUSED) // 1 for first address, >1 after comma
Bram Moolenaar071d4272004-06-13 20:20:40 +00004216{
Bram Moolenaarb7316892019-05-01 18:08:42 +02004217 cmd_addr_T addr_type = addr_type_arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004218 int c;
4219 int i;
4220 long n;
4221 char_u *cmd;
4222 pos_T pos;
4223 pos_T *fp;
4224 linenr_T lnum;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004225 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004226
4227 cmd = skipwhite(*ptr);
4228 lnum = MAXLNUM;
4229 do
4230 {
4231 switch (*cmd)
4232 {
4233 case '.': /* '.' - Cursor position */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004234 ++cmd;
4235 switch (addr_type)
4236 {
4237 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004238 case ADDR_OTHER:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004239 lnum = curwin->w_cursor.lnum;
4240 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004241 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004242 lnum = CURRENT_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004243 break;
4244 case ADDR_ARGUMENTS:
4245 lnum = curwin->w_arg_idx + 1;
4246 break;
4247 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004248 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004249 lnum = curbuf->b_fnum;
4250 break;
4251 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004252 lnum = CURRENT_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004253 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02004254 case ADDR_NONE:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004255 case ADDR_TABS_RELATIVE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02004256 case ADDR_UNSIGNED:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004257 addr_error(addr_type);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004258 cmd = NULL;
4259 goto error;
4260 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004261 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004262#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02004263 lnum = qf_get_cur_idx(eap);
4264#endif
4265 break;
4266 case ADDR_QUICKFIX_VALID:
4267#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004268 lnum = qf_get_cur_valid_idx(eap);
Bram Moolenaare906c502015-09-09 21:10:39 +02004269#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004270 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004271 }
4272 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004273
4274 case '$': /* '$' - last line */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004275 ++cmd;
4276 switch (addr_type)
4277 {
4278 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004279 case ADDR_OTHER:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004280 lnum = curbuf->b_ml.ml_line_count;
4281 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004282 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004283 lnum = LAST_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004284 break;
4285 case ADDR_ARGUMENTS:
4286 lnum = ARGCOUNT;
4287 break;
4288 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004289 buf = lastbuf;
4290 while (buf->b_ml.ml_mfp == NULL)
4291 {
4292 if (buf->b_prev == NULL)
4293 break;
4294 buf = buf->b_prev;
4295 }
4296 lnum = buf->b_fnum;
4297 break;
4298 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004299 lnum = lastbuf->b_fnum;
4300 break;
4301 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004302 lnum = LAST_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004303 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02004304 case ADDR_NONE:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004305 case ADDR_TABS_RELATIVE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02004306 case ADDR_UNSIGNED:
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004307 addr_error(addr_type);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004308 cmd = NULL;
4309 goto error;
4310 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004311 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004312#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004313 lnum = qf_get_size(eap);
4314 if (lnum == 0)
4315 lnum = 1;
Bram Moolenaare906c502015-09-09 21:10:39 +02004316#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004317 break;
Bram Moolenaar25190db2019-05-04 15:05:28 +02004318 case ADDR_QUICKFIX_VALID:
4319#ifdef FEAT_QUICKFIX
4320 lnum = qf_get_valid_size(eap);
4321 if (lnum == 0)
4322 lnum = 1;
4323#endif
4324 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004325 }
4326 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004327
4328 case '\'': /* ''' - mark */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004329 if (*++cmd == NUL)
4330 {
4331 cmd = NULL;
4332 goto error;
4333 }
4334 if (addr_type != ADDR_LINES)
4335 {
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004336 addr_error(addr_type);
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004337 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004338 goto error;
4339 }
4340 if (skip)
4341 ++cmd;
4342 else
4343 {
4344 /* Only accept a mark in another file when it is
4345 * used by itself: ":'M". */
4346 fp = getmark(*cmd, to_other_file && cmd[1] == NUL);
4347 ++cmd;
4348 if (fp == (pos_T *)-1)
4349 /* Jumped to another file. */
4350 lnum = curwin->w_cursor.lnum;
4351 else
4352 {
4353 if (check_mark(fp) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004354 {
4355 cmd = NULL;
4356 goto error;
4357 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004358 lnum = fp->lnum;
4359 }
4360 }
4361 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004362
4363 case '/':
4364 case '?': /* '/' or '?' - search */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004365 c = *cmd++;
4366 if (addr_type != ADDR_LINES)
4367 {
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004368 addr_error(addr_type);
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004369 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004370 goto error;
4371 }
4372 if (skip) /* skip "/pat/" */
4373 {
4374 cmd = skip_regexp(cmd, c, (int)p_magic, NULL);
4375 if (*cmd == c)
4376 ++cmd;
4377 }
4378 else
4379 {
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02004380 int flags;
4381
4382 pos = curwin->w_cursor; // save curwin->w_cursor
4383
4384 // When '/' or '?' follows another address, start from
4385 // there.
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004386 if (lnum != MAXLNUM)
4387 curwin->w_cursor.lnum = lnum;
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02004388
4389 // Start a forward search at the end of the line (unless
4390 // before the first line).
4391 // Start a backward search at the start of the line.
4392 // This makes sure we never match in the current
4393 // line, and can match anywhere in the
4394 // next/previous line.
Bram Moolenaar8ada6aa2017-12-19 21:23:21 +01004395 if (c == '/' && curwin->w_cursor.lnum > 0)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004396 curwin->w_cursor.col = MAXCOL;
4397 else
4398 curwin->w_cursor.col = 0;
4399 searchcmdlen = 0;
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02004400 flags = silent ? 0 : SEARCH_HIS | SEARCH_MSG;
4401 if (!do_search(NULL, c, cmd, 1L, flags, NULL, NULL))
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004402 {
4403 curwin->w_cursor = pos;
4404 cmd = NULL;
4405 goto error;
4406 }
4407 lnum = curwin->w_cursor.lnum;
4408 curwin->w_cursor = pos;
4409 /* adjust command string pointer */
4410 cmd += searchcmdlen;
4411 }
4412 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004413
4414 case '\\': /* "\?", "\/" or "\&", repeat search */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004415 ++cmd;
4416 if (addr_type != ADDR_LINES)
4417 {
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02004418 addr_error(addr_type);
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004419 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004420 goto error;
4421 }
4422 if (*cmd == '&')
4423 i = RE_SUBST;
4424 else if (*cmd == '?' || *cmd == '/')
4425 i = RE_SEARCH;
4426 else
4427 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004428 emsg(_(e_backslash));
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004429 cmd = NULL;
4430 goto error;
4431 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004432
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004433 if (!skip)
4434 {
4435 /*
4436 * When search follows another address, start from
4437 * there.
4438 */
4439 if (lnum != MAXLNUM)
4440 pos.lnum = lnum;
4441 else
4442 pos.lnum = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004443
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004444 /*
4445 * Start the search just like for the above
4446 * do_search().
4447 */
4448 if (*cmd != '?')
4449 pos.col = MAXCOL;
4450 else
4451 pos.col = 0;
Bram Moolenaarbd8539a2015-08-11 18:53:03 +02004452 pos.coladd = 0;
Bram Moolenaar5d24a222018-12-23 19:10:09 +01004453 if (searchit(curwin, curbuf, &pos, NULL,
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004454 *cmd == '?' ? BACKWARD : FORWARD,
4455 (char_u *)"", 1L, SEARCH_MSG,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02004456 i, (linenr_T)0, NULL, NULL) != FAIL)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004457 lnum = pos.lnum;
4458 else
4459 {
4460 cmd = NULL;
4461 goto error;
4462 }
4463 }
4464 ++cmd;
4465 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004466
4467 default:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004468 if (VIM_ISDIGIT(*cmd)) /* absolute line number */
4469 lnum = getdigits(&cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004470 }
4471
4472 for (;;)
4473 {
4474 cmd = skipwhite(cmd);
4475 if (*cmd != '-' && *cmd != '+' && !VIM_ISDIGIT(*cmd))
4476 break;
4477
4478 if (lnum == MAXLNUM)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004479 {
4480 switch (addr_type)
4481 {
4482 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004483 case ADDR_OTHER:
4484 // "+1" is same as ".+1"
Bram Moolenaarf240e182014-11-27 18:33:02 +01004485 lnum = curwin->w_cursor.lnum;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004486 break;
4487 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004488 lnum = CURRENT_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004489 break;
4490 case ADDR_ARGUMENTS:
4491 lnum = curwin->w_arg_idx + 1;
4492 break;
4493 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004494 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004495 lnum = curbuf->b_fnum;
4496 break;
4497 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004498 lnum = CURRENT_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004499 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004500 case ADDR_TABS_RELATIVE:
4501 lnum = 1;
4502 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004503 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004504#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02004505 lnum = qf_get_cur_idx(eap);
4506#endif
4507 break;
4508 case ADDR_QUICKFIX_VALID:
4509#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004510 lnum = qf_get_cur_valid_idx(eap);
Bram Moolenaare906c502015-09-09 21:10:39 +02004511#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004512 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02004513 case ADDR_NONE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02004514 case ADDR_UNSIGNED:
4515 lnum = 0;
Bram Moolenaarb7316892019-05-01 18:08:42 +02004516 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004517 }
4518 }
4519
Bram Moolenaar071d4272004-06-13 20:20:40 +00004520 if (VIM_ISDIGIT(*cmd))
4521 i = '+'; /* "number" is same as "+number" */
4522 else
4523 i = *cmd++;
4524 if (!VIM_ISDIGIT(*cmd)) /* '+' is '+1', but '+0' is not '+1' */
4525 n = 1;
4526 else
4527 n = getdigits(&cmd);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004528
4529 if (addr_type == ADDR_TABS_RELATIVE)
4530 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004531 emsg(_(e_invrange));
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004532 cmd = NULL;
4533 goto error;
4534 }
4535 else if (addr_type == ADDR_LOADED_BUFFERS
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004536 || addr_type == ADDR_BUFFERS)
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004537 lnum = compute_buffer_local_count(
4538 addr_type, lnum, (i == '-') ? -1 * n : n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004539 else
Bram Moolenaarded27822017-01-02 14:27:34 +01004540 {
4541#ifdef FEAT_FOLDING
4542 /* Relative line addressing, need to adjust for folded lines
4543 * now, but only do it after the first address. */
4544 if (addr_type == ADDR_LINES && (i == '-' || i == '+')
4545 && address_count >= 2)
4546 (void)hasFolding(lnum, NULL, &lnum);
4547#endif
4548 if (i == '-')
4549 lnum -= n;
4550 else
4551 lnum += n;
4552 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004553 }
4554 } while (*cmd == '/' || *cmd == '?');
4555
4556error:
4557 *ptr = cmd;
4558 return lnum;
4559}
4560
4561/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00004562 * Get flags from an Ex command argument.
4563 */
4564 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004565get_flags(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00004566{
4567 while (vim_strchr((char_u *)"lp#", *eap->arg) != NULL)
4568 {
4569 if (*eap->arg == 'l')
4570 eap->flags |= EXFLAG_LIST;
4571 else if (*eap->arg == 'p')
4572 eap->flags |= EXFLAG_PRINT;
4573 else
4574 eap->flags |= EXFLAG_NR;
4575 eap->arg = skipwhite(eap->arg + 1);
4576 }
4577}
4578
4579/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004580 * Function called for command which is Not Implemented. NI!
4581 */
4582 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004583ex_ni(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004584{
4585 if (!eap->skip)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004586 eap->errmsg = N_("E319: Sorry, the command is not available in this version");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004587}
4588
Bram Moolenaar7bb75552007-07-16 18:39:49 +00004589#ifdef HAVE_EX_SCRIPT_NI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004590/*
4591 * Function called for script command which is Not Implemented. NI!
4592 * Skips over ":perl <<EOF" constructs.
4593 */
4594 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004595ex_script_ni(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004596{
4597 if (!eap->skip)
4598 ex_ni(eap);
4599 else
4600 vim_free(script_get(eap, eap->arg));
4601}
4602#endif
4603
4604/*
4605 * Check range in Ex command for validity.
4606 * Return NULL when valid, error message when invalid.
4607 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004608 static char *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004609invalid_range(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004610{
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004611 buf_T *buf;
Bram Moolenaar25190db2019-05-04 15:05:28 +02004612
Bram Moolenaar071d4272004-06-13 20:20:40 +00004613 if ( eap->line1 < 0
4614 || eap->line2 < 0
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004615 || eap->line1 > eap->line2)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004616 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004617
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004618 if (eap->argt & EX_RANGE)
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004619 {
Bram Moolenaarb7316892019-05-01 18:08:42 +02004620 switch (eap->addr_type)
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004621 {
4622 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004623 if (eap->line2 > curbuf->b_ml.ml_line_count
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004624#ifdef FEAT_DIFF
4625 + (eap->cmdidx == CMD_diffget)
4626#endif
4627 )
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004628 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004629 break;
4630 case ADDR_ARGUMENTS:
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004631 /* add 1 if ARGCOUNT is 0 */
4632 if (eap->line2 > ARGCOUNT + (!ARGCOUNT))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004633 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004634 break;
4635 case ADDR_BUFFERS:
4636 if (eap->line1 < firstbuf->b_fnum
4637 || eap->line2 > lastbuf->b_fnum)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004638 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004639 break;
4640 case ADDR_LOADED_BUFFERS:
4641 buf = firstbuf;
4642 while (buf->b_ml.ml_mfp == NULL)
4643 {
4644 if (buf->b_next == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004645 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004646 buf = buf->b_next;
4647 }
4648 if (eap->line1 < buf->b_fnum)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004649 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004650 buf = lastbuf;
4651 while (buf->b_ml.ml_mfp == NULL)
4652 {
4653 if (buf->b_prev == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004654 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004655 buf = buf->b_prev;
4656 }
4657 if (eap->line2 > buf->b_fnum)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004658 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004659 break;
4660 case ADDR_WINDOWS:
Bram Moolenaar8be63882015-01-14 11:25:05 +01004661 if (eap->line2 > LAST_WIN_NR)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004662 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004663 break;
4664 case ADDR_TABS:
4665 if (eap->line2 > LAST_TAB_NR)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004666 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004667 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004668 case ADDR_TABS_RELATIVE:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004669 case ADDR_OTHER:
4670 // Any range is OK.
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004671 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004672 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004673#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02004674 // No error for value that is too big, will use the last entry.
4675 if (eap->line2 <= 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004676 return _(e_invrange);
Bram Moolenaare906c502015-09-09 21:10:39 +02004677#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004678 break;
Bram Moolenaar25190db2019-05-04 15:05:28 +02004679 case ADDR_QUICKFIX_VALID:
4680#ifdef FEAT_QUICKFIX
4681 if ((eap->line2 != 1 && eap->line2 > qf_get_valid_size(eap))
4682 || eap->line2 < 0)
4683 return _(e_invrange);
4684#endif
4685 break;
4686 case ADDR_UNSIGNED:
4687 if (eap->line2 < 0)
4688 return _(e_invrange);
4689 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02004690 case ADDR_NONE:
4691 // Will give an error elsewhere.
4692 break;
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004693 }
4694 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004695 return NULL;
4696}
4697
4698/*
4699 * Correct the range for zero line number, if required.
4700 */
4701 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004702correct_range(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004703{
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004704 if (!(eap->argt & EX_ZEROR)) // zero in range not allowed
Bram Moolenaar071d4272004-06-13 20:20:40 +00004705 {
4706 if (eap->line1 == 0)
4707 eap->line1 = 1;
4708 if (eap->line2 == 0)
4709 eap->line2 = 1;
4710 }
4711}
4712
Bram Moolenaar748bf032005-02-02 23:04:36 +00004713#ifdef FEAT_QUICKFIX
Bram Moolenaar748bf032005-02-02 23:04:36 +00004714/*
4715 * For a ":vimgrep" or ":vimgrepadd" command return a pointer past the
4716 * pattern. Otherwise return eap->arg.
4717 */
4718 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004719skip_grep_pat(exarg_T *eap)
Bram Moolenaar748bf032005-02-02 23:04:36 +00004720{
4721 char_u *p = eap->arg;
4722
Bram Moolenaara37420f2006-02-04 22:37:47 +00004723 if (*p != NUL && (eap->cmdidx == CMD_vimgrep || eap->cmdidx == CMD_lvimgrep
4724 || eap->cmdidx == CMD_vimgrepadd
4725 || eap->cmdidx == CMD_lvimgrepadd
4726 || grep_internal(eap->cmdidx)))
Bram Moolenaar748bf032005-02-02 23:04:36 +00004727 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00004728 p = skip_vimgrep_pat(p, NULL, NULL);
Bram Moolenaar748bf032005-02-02 23:04:36 +00004729 if (p == NULL)
4730 p = eap->arg;
Bram Moolenaar748bf032005-02-02 23:04:36 +00004731 }
4732 return p;
4733}
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004734
4735/*
4736 * For the ":make" and ":grep" commands insert the 'makeprg'/'grepprg' option
4737 * in the command line, so that things like % get expanded.
4738 */
4739 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004740replace_makeprg(exarg_T *eap, char_u *p, char_u **cmdlinep)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004741{
4742 char_u *new_cmdline;
4743 char_u *program;
4744 char_u *pos;
4745 char_u *ptr;
4746 int len;
4747 int i;
4748
4749 /*
4750 * Don't do it when ":vimgrep" is used for ":grep".
4751 */
Bram Moolenaara37420f2006-02-04 22:37:47 +00004752 if ((eap->cmdidx == CMD_make || eap->cmdidx == CMD_lmake
4753 || eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4754 || eap->cmdidx == CMD_grepadd
4755 || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004756 && !grep_internal(eap->cmdidx))
4757 {
Bram Moolenaara37420f2006-02-04 22:37:47 +00004758 if (eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4759 || eap->cmdidx == CMD_grepadd || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004760 {
4761 if (*curbuf->b_p_gp == NUL)
4762 program = p_gp;
4763 else
4764 program = curbuf->b_p_gp;
4765 }
4766 else
4767 {
4768 if (*curbuf->b_p_mp == NUL)
4769 program = p_mp;
4770 else
4771 program = curbuf->b_p_mp;
4772 }
4773
4774 p = skipwhite(p);
4775
4776 if ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4777 {
4778 /* replace $* by given arguments */
4779 i = 1;
4780 while ((pos = (char_u *)strstr((char *)pos + 2, "$*")) != NULL)
4781 ++i;
4782 len = (int)STRLEN(p);
Bram Moolenaar51e14382019-05-25 20:21:28 +02004783 new_cmdline = alloc(STRLEN(program) + i * (len - 2) + 1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004784 if (new_cmdline == NULL)
4785 return NULL; /* out of memory */
4786 ptr = new_cmdline;
4787 while ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4788 {
4789 i = (int)(pos - program);
4790 STRNCPY(ptr, program, i);
4791 STRCPY(ptr += i, p);
4792 ptr += len;
4793 program = pos + 2;
4794 }
4795 STRCPY(ptr, program);
4796 }
4797 else
4798 {
Bram Moolenaar51e14382019-05-25 20:21:28 +02004799 new_cmdline = alloc(STRLEN(program) + STRLEN(p) + 2);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004800 if (new_cmdline == NULL)
4801 return NULL; /* out of memory */
4802 STRCPY(new_cmdline, program);
4803 STRCAT(new_cmdline, " ");
4804 STRCAT(new_cmdline, p);
4805 }
4806 msg_make(p);
4807
4808 /* 'eap->cmd' is not set here, because it is not used at CMD_make */
4809 vim_free(*cmdlinep);
4810 *cmdlinep = new_cmdline;
4811 p = new_cmdline;
4812 }
4813 return p;
4814}
Bram Moolenaar748bf032005-02-02 23:04:36 +00004815#endif
4816
Bram Moolenaar071d4272004-06-13 20:20:40 +00004817/*
4818 * Expand file name in Ex command argument.
Bram Moolenaar0abb4272019-06-15 16:06:00 +02004819 * When an error is detected, "errormsgp" is set to a non-NULL pointer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004820 * Return FAIL for failure, OK otherwise.
4821 */
4822 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004823expand_filename(
4824 exarg_T *eap,
4825 char_u **cmdlinep,
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004826 char **errormsgp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004827{
4828 int has_wildcards; /* need to expand wildcards */
4829 char_u *repl;
4830 int srclen;
4831 char_u *p;
4832 int n;
Bram Moolenaar63b92542007-03-27 14:57:09 +00004833 int escaped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004834
Bram Moolenaar748bf032005-02-02 23:04:36 +00004835#ifdef FEAT_QUICKFIX
4836 /* Skip a regexp pattern for ":vimgrep[add] pat file..." */
4837 p = skip_grep_pat(eap);
4838#else
4839 p = eap->arg;
4840#endif
4841
Bram Moolenaar071d4272004-06-13 20:20:40 +00004842 /*
4843 * Decide to expand wildcards *before* replacing '%', '#', etc. If
4844 * the file name contains a wildcard it should not cause expanding.
4845 * (it will be expanded anyway if there is a wildcard before replacing).
4846 */
Bram Moolenaar748bf032005-02-02 23:04:36 +00004847 has_wildcards = mch_has_wildcard(p);
4848 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004849 {
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004850#ifdef FEAT_EVAL
4851 /* Skip over `=expr`, wildcards in it are not expanded. */
4852 if (p[0] == '`' && p[1] == '=')
4853 {
4854 p += 2;
4855 (void)skip_expr(&p);
4856 if (*p == '`')
4857 ++p;
4858 continue;
4859 }
4860#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004861 /*
4862 * Quick check if this cannot be the start of a special string.
4863 * Also removes backslash before '%', '#' and '<'.
4864 */
4865 if (vim_strchr((char_u *)"%#<", *p) == NULL)
4866 {
4867 ++p;
4868 continue;
4869 }
4870
4871 /*
4872 * Try to find a match at this position.
4873 */
Bram Moolenaar63b92542007-03-27 14:57:09 +00004874 repl = eval_vars(p, eap->arg, &srclen, &(eap->do_ecmd_lnum),
4875 errormsgp, &escaped);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004876 if (*errormsgp != NULL) /* error detected */
4877 return FAIL;
4878 if (repl == NULL) /* no match found */
4879 {
4880 p += srclen;
4881 continue;
4882 }
4883
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00004884 /* Wildcards won't be expanded below, the replacement is taken
4885 * literally. But do expand "~/file", "~user/file" and "$HOME/file". */
4886 if (vim_strchr(repl, '$') != NULL || vim_strchr(repl, '~') != NULL)
4887 {
4888 char_u *l = repl;
4889
4890 repl = expand_env_save(repl);
4891 vim_free(l);
4892 }
4893
Bram Moolenaar63b92542007-03-27 14:57:09 +00004894 /* Need to escape white space et al. with a backslash.
4895 * Don't do this for:
4896 * - replacement that already has been escaped: "##"
4897 * - shell commands (may have to use quotes instead).
4898 * - non-unix systems when there is a single argument (spaces don't
4899 * separate arguments then).
4900 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004901 if (!eap->usefilter
Bram Moolenaar63b92542007-03-27 14:57:09 +00004902 && !escaped
Bram Moolenaar071d4272004-06-13 20:20:40 +00004903 && eap->cmdidx != CMD_bang
Bram Moolenaar071d4272004-06-13 20:20:40 +00004904 && eap->cmdidx != CMD_grep
4905 && eap->cmdidx != CMD_grepadd
Bram Moolenaarbf15b8d2017-06-04 20:43:48 +02004906 && eap->cmdidx != CMD_hardcopy
Bram Moolenaar67883b42017-07-27 22:57:00 +02004907 && eap->cmdidx != CMD_lgrep
4908 && eap->cmdidx != CMD_lgrepadd
4909 && eap->cmdidx != CMD_lmake
4910 && eap->cmdidx != CMD_make
4911 && eap->cmdidx != CMD_terminal
Bram Moolenaar071d4272004-06-13 20:20:40 +00004912#ifndef UNIX
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004913 && !(eap->argt & EX_NOSPC)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004914#endif
4915 )
4916 {
4917 char_u *l;
4918#ifdef BACKSLASH_IN_FILENAME
4919 /* Don't escape a backslash here, because rem_backslash() doesn't
4920 * remove it later. */
4921 static char_u *nobslash = (char_u *)" \t\"|";
4922# define ESCAPE_CHARS nobslash
4923#else
4924# define ESCAPE_CHARS escape_chars
4925#endif
4926
4927 for (l = repl; *l; ++l)
4928 if (vim_strchr(ESCAPE_CHARS, *l) != NULL)
4929 {
4930 l = vim_strsave_escaped(repl, ESCAPE_CHARS);
4931 if (l != NULL)
4932 {
4933 vim_free(repl);
4934 repl = l;
4935 }
4936 break;
4937 }
4938 }
4939
4940 /* For a shell command a '!' must be escaped. */
Bram Moolenaar67883b42017-07-27 22:57:00 +02004941 if ((eap->usefilter || eap->cmdidx == CMD_bang
4942 || eap->cmdidx == CMD_terminal)
Bram Moolenaar31b7d382014-04-01 18:54:48 +02004943 && vim_strpbrk(repl, (char_u *)"!") != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004944 {
4945 char_u *l;
4946
Bram Moolenaar31b7d382014-04-01 18:54:48 +02004947 l = vim_strsave_escaped(repl, (char_u *)"!");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004948 if (l != NULL)
4949 {
4950 vim_free(repl);
4951 repl = l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004952 }
4953 }
4954
4955 p = repl_cmdline(eap, p, srclen, repl, cmdlinep);
4956 vim_free(repl);
4957 if (p == NULL)
4958 return FAIL;
4959 }
4960
4961 /*
4962 * One file argument: Expand wildcards.
4963 * Don't do this with ":r !command" or ":w !command".
4964 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004965 if ((eap->argt & EX_NOSPC) && !eap->usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004966 {
4967 /*
4968 * May do this twice:
4969 * 1. Replace environment variables.
4970 * 2. Replace any other wildcards, remove backslashes.
4971 */
4972 for (n = 1; n <= 2; ++n)
4973 {
4974 if (n == 2)
4975 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004976 /*
4977 * Halve the number of backslashes (this is Vi compatible).
4978 * For Unix and OS/2, when wildcards are expanded, this is
4979 * done by ExpandOne() below.
4980 */
Bram Moolenaare7fedb62015-12-31 19:07:19 +01004981#if defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004982 if (!has_wildcards)
4983#endif
4984 backslash_halve(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004985 }
4986
4987 if (has_wildcards)
4988 {
4989 if (n == 1)
4990 {
4991 /*
4992 * First loop: May expand environment variables. This
4993 * can be done much faster with expand_env() than with
4994 * something else (e.g., calling a shell).
4995 * After expanding environment variables, check again
4996 * if there are still wildcards present.
4997 */
4998 if (vim_strchr(eap->arg, '$') != NULL
4999 || vim_strchr(eap->arg, '~') != NULL)
5000 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +00005001 expand_env_esc(eap->arg, NameBuff, MAXPATHL,
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00005002 TRUE, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005003 has_wildcards = mch_has_wildcard(NameBuff);
5004 p = NameBuff;
5005 }
5006 else
5007 p = NULL;
5008 }
5009 else /* n == 2 */
5010 {
5011 expand_T xpc;
Bram Moolenaar94950a92010-12-02 16:01:29 +01005012 int options = WILD_LIST_NOTFOUND|WILD_ADD_SLASH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005013
5014 ExpandInit(&xpc);
5015 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar94950a92010-12-02 16:01:29 +01005016 if (p_wic)
5017 options += WILD_ICASE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005018 p = ExpandOne(&xpc, eap->arg, NULL,
Bram Moolenaar94950a92010-12-02 16:01:29 +01005019 options, WILD_EXPAND_FREE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005020 if (p == NULL)
5021 return FAIL;
5022 }
5023 if (p != NULL)
5024 {
5025 (void)repl_cmdline(eap, eap->arg, (int)STRLEN(eap->arg),
5026 p, cmdlinep);
5027 if (n == 2) /* p came from ExpandOne() */
5028 vim_free(p);
5029 }
5030 }
5031 }
5032 }
5033 return OK;
5034}
5035
5036/*
5037 * Replace part of the command line, keeping eap->cmd, eap->arg and
5038 * eap->nextcmd correct.
5039 * "src" points to the part that is to be replaced, of length "srclen".
5040 * "repl" is the replacement string.
5041 * Returns a pointer to the character after the replaced string.
5042 * Returns NULL for failure.
5043 */
5044 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005045repl_cmdline(
5046 exarg_T *eap,
5047 char_u *src,
5048 int srclen,
5049 char_u *repl,
5050 char_u **cmdlinep)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005051{
5052 int len;
5053 int i;
5054 char_u *new_cmdline;
5055
5056 /*
5057 * The new command line is build in new_cmdline[].
5058 * First allocate it.
5059 * Careful: a "+cmd" argument may have been NUL terminated.
5060 */
5061 len = (int)STRLEN(repl);
5062 i = (int)(src - *cmdlinep) + (int)STRLEN(src + srclen) + len + 3;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005063 if (eap->nextcmd != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005064 i += (int)STRLEN(eap->nextcmd);/* add space for next command */
Bram Moolenaar964b3742019-05-24 18:54:09 +02005065 if ((new_cmdline = alloc(i)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005066 return NULL; /* out of memory! */
5067
5068 /*
5069 * Copy the stuff before the expanded part.
5070 * Copy the expanded stuff.
5071 * Copy what came after the expanded part.
5072 * Copy the next commands, if there are any.
5073 */
5074 i = (int)(src - *cmdlinep); /* length of part before match */
5075 mch_memmove(new_cmdline, *cmdlinep, (size_t)i);
Bram Moolenaara3ffd9c2005-07-21 21:03:15 +00005076
Bram Moolenaar071d4272004-06-13 20:20:40 +00005077 mch_memmove(new_cmdline + i, repl, (size_t)len);
5078 i += len; /* remember the end of the string */
5079 STRCPY(new_cmdline + i, src + srclen);
5080 src = new_cmdline + i; /* remember where to continue */
5081
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005082 if (eap->nextcmd != NULL) /* append next command */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005083 {
5084 i = (int)STRLEN(new_cmdline) + 1;
5085 STRCPY(new_cmdline + i, eap->nextcmd);
5086 eap->nextcmd = new_cmdline + i;
5087 }
5088 eap->cmd = new_cmdline + (eap->cmd - *cmdlinep);
5089 eap->arg = new_cmdline + (eap->arg - *cmdlinep);
5090 if (eap->do_ecmd_cmd != NULL && eap->do_ecmd_cmd != dollar_command)
5091 eap->do_ecmd_cmd = new_cmdline + (eap->do_ecmd_cmd - *cmdlinep);
5092 vim_free(*cmdlinep);
5093 *cmdlinep = new_cmdline;
5094
5095 return src;
5096}
5097
5098/*
5099 * Check for '|' to separate commands and '"' to start comments.
5100 */
5101 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005102separate_nextcmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005103{
5104 char_u *p;
5105
Bram Moolenaar86b68352004-12-27 21:59:20 +00005106#ifdef FEAT_QUICKFIX
Bram Moolenaar748bf032005-02-02 23:04:36 +00005107 p = skip_grep_pat(eap);
5108#else
5109 p = eap->arg;
Bram Moolenaar86b68352004-12-27 21:59:20 +00005110#endif
5111
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005112 for ( ; *p; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005113 {
5114 if (*p == Ctrl_V)
5115 {
Bram Moolenaar8071cb22019-07-12 17:58:01 +02005116 if (eap->argt & (EX_CTRLV | EX_XFILE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005117 ++p; /* skip CTRL-V and next char */
5118 else
Bram Moolenaarb0db5692007-08-14 20:54:49 +00005119 /* remove CTRL-V and skip next char */
Bram Moolenaara7241f52008-06-24 20:39:31 +00005120 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005121 if (*p == NUL) /* stop at NUL after CTRL-V */
5122 break;
5123 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005124
5125#ifdef FEAT_EVAL
5126 /* Skip over `=expr` when wildcards are expanded. */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02005127 else if (p[0] == '`' && p[1] == '=' && (eap->argt & EX_XFILE))
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005128 {
5129 p += 2;
5130 (void)skip_expr(&p);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005131 }
5132#endif
5133
Bram Moolenaar071d4272004-06-13 20:20:40 +00005134 /* Check for '"': start of comment or '|': next command */
5135 /* :@" and :*" do not start a comment!
5136 * :redir @" doesn't either. */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02005137 else if ((*p == '"' && !(eap->argt & EX_NOTRLCOM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005138 && ((eap->cmdidx != CMD_at && eap->cmdidx != CMD_star)
5139 || p != eap->arg)
5140 && (eap->cmdidx != CMD_redir
5141 || p != eap->arg + 1 || p[-1] != '@'))
5142 || *p == '|' || *p == '\n')
5143 {
5144 /*
Bram Moolenaar8071cb22019-07-12 17:58:01 +02005145 * We remove the '\' before the '|', unless EX_CTRLV is used
Bram Moolenaar071d4272004-06-13 20:20:40 +00005146 * AND 'b' is present in 'cpoptions'.
5147 */
5148 if ((vim_strchr(p_cpo, CPO_BAR) == NULL
Bram Moolenaar8071cb22019-07-12 17:58:01 +02005149 || !(eap->argt & EX_CTRLV)) && *(p - 1) == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005150 {
Bram Moolenaara7241f52008-06-24 20:39:31 +00005151 STRMOVE(p - 1, p); /* remove the '\' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005152 --p;
5153 }
5154 else
5155 {
5156 eap->nextcmd = check_nextcmd(p);
5157 *p = NUL;
5158 break;
5159 }
5160 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005161 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00005162
Bram Moolenaar8071cb22019-07-12 17:58:01 +02005163 if (!(eap->argt & EX_NOTRLCOM)) /* remove trailing spaces */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005164 del_trailing_spaces(eap->arg);
5165}
5166
5167/*
5168 * get + command from ex argument
5169 */
5170 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005171getargcmd(char_u **argp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005172{
5173 char_u *arg = *argp;
5174 char_u *command = NULL;
5175
5176 if (*arg == '+') /* +[command] */
5177 {
5178 ++arg;
Bram Moolenaar3e451592014-04-02 14:22:05 +02005179 if (vim_isspace(*arg) || *arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005180 command = dollar_command;
5181 else
5182 {
5183 command = arg;
5184 arg = skip_cmd_arg(command, TRUE);
5185 if (*arg != NUL)
5186 *arg++ = NUL; /* terminate command with NUL */
5187 }
5188
5189 arg = skipwhite(arg); /* skip over spaces */
5190 *argp = arg;
5191 }
5192 return command;
5193}
5194
5195/*
5196 * Find end of "+command" argument. Skip over "\ " and "\\".
5197 */
5198 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005199skip_cmd_arg(
5200 char_u *p,
5201 int rembs) /* TRUE to halve the number of backslashes */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005202{
5203 while (*p && !vim_isspace(*p))
5204 {
5205 if (*p == '\\' && p[1] != NUL)
5206 {
5207 if (rembs)
Bram Moolenaara7241f52008-06-24 20:39:31 +00005208 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005209 else
5210 ++p;
5211 }
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005212 MB_PTR_ADV(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005213 }
5214 return p;
5215}
5216
Bram Moolenaar333b80a2018-04-04 22:57:29 +02005217 int
5218get_bad_opt(char_u *p, exarg_T *eap)
5219{
5220 if (STRICMP(p, "keep") == 0)
5221 eap->bad_char = BAD_KEEP;
5222 else if (STRICMP(p, "drop") == 0)
5223 eap->bad_char = BAD_DROP;
5224 else if (MB_BYTE2LEN(*p) == 1 && p[1] == NUL)
5225 eap->bad_char = *p;
Bram Moolenaar75808492018-06-12 12:39:41 +02005226 else
5227 return FAIL;
5228 return OK;
Bram Moolenaar333b80a2018-04-04 22:57:29 +02005229}
5230
Bram Moolenaar071d4272004-06-13 20:20:40 +00005231/*
5232 * Get "++opt=arg" argument.
5233 * Return FAIL or OK.
5234 */
5235 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005236getargopt(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005237{
5238 char_u *arg = eap->arg + 2;
5239 int *pp = NULL;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02005240 int bad_char_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005241 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005242
5243 /* ":edit ++[no]bin[ary] file" */
5244 if (STRNCMP(arg, "bin", 3) == 0 || STRNCMP(arg, "nobin", 5) == 0)
5245 {
5246 if (*arg == 'n')
5247 {
5248 arg += 2;
5249 eap->force_bin = FORCE_NOBIN;
5250 }
5251 else
5252 eap->force_bin = FORCE_BIN;
5253 if (!checkforcmd(&arg, "binary", 3))
5254 return FAIL;
5255 eap->arg = skipwhite(arg);
5256 return OK;
5257 }
5258
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005259 /* ":read ++edit file" */
5260 if (STRNCMP(arg, "edit", 4) == 0)
5261 {
5262 eap->read_edit = TRUE;
5263 eap->arg = skipwhite(arg + 4);
5264 return OK;
5265 }
5266
Bram Moolenaar071d4272004-06-13 20:20:40 +00005267 if (STRNCMP(arg, "ff", 2) == 0)
5268 {
5269 arg += 2;
5270 pp = &eap->force_ff;
5271 }
5272 else if (STRNCMP(arg, "fileformat", 10) == 0)
5273 {
5274 arg += 10;
5275 pp = &eap->force_ff;
5276 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005277 else if (STRNCMP(arg, "enc", 3) == 0)
5278 {
Bram Moolenaarb38e9ab2011-12-14 14:49:45 +01005279 if (STRNCMP(arg, "encoding", 8) == 0)
5280 arg += 8;
5281 else
5282 arg += 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005283 pp = &eap->force_enc;
5284 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005285 else if (STRNCMP(arg, "bad", 3) == 0)
5286 {
5287 arg += 3;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02005288 pp = &bad_char_idx;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005289 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005290
5291 if (pp == NULL || *arg != '=')
5292 return FAIL;
5293
5294 ++arg;
5295 *pp = (int)(arg - eap->cmd);
5296 arg = skip_cmd_arg(arg, FALSE);
5297 eap->arg = skipwhite(arg);
5298 *arg = NUL;
5299
Bram Moolenaar071d4272004-06-13 20:20:40 +00005300 if (pp == &eap->force_ff)
5301 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005302 if (check_ff_value(eap->cmd + eap->force_ff) == FAIL)
5303 return FAIL;
Bram Moolenaar333b80a2018-04-04 22:57:29 +02005304 eap->force_ff = eap->cmd[eap->force_ff];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005305 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005306 else if (pp == &eap->force_enc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005307 {
5308 /* Make 'fileencoding' lower case. */
5309 for (p = eap->cmd + eap->force_enc; *p != NUL; ++p)
5310 *p = TOLOWER_ASC(*p);
5311 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005312 else
5313 {
5314 /* Check ++bad= argument. Must be a single-byte character, "keep" or
5315 * "drop". */
Bram Moolenaar333b80a2018-04-04 22:57:29 +02005316 if (get_bad_opt(eap->cmd + bad_char_idx, eap) == FAIL)
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005317 return FAIL;
5318 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005319
5320 return OK;
5321}
5322
Bram Moolenaar071d4272004-06-13 20:20:40 +00005323 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005324ex_autocmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005325{
5326 /*
Bram Moolenaar26d4b892018-07-04 22:26:28 +02005327 * Disallow autocommands from .exrc and .vimrc in current
Bram Moolenaar071d4272004-06-13 20:20:40 +00005328 * directory for security reasons.
5329 */
5330 if (secure)
5331 {
5332 secure = 2;
5333 eap->errmsg = e_curdir;
5334 }
5335 else if (eap->cmdidx == CMD_autocmd)
5336 do_autocmd(eap->arg, eap->forceit);
5337 else
5338 do_augroup(eap->arg, eap->forceit);
5339}
5340
5341/*
5342 * ":doautocmd": Apply the automatic commands to the current buffer.
5343 */
5344 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005345ex_doautocmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005346{
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005347 char_u *arg = eap->arg;
5348 int call_do_modelines = check_nomodeline(&arg);
Bram Moolenaar1610d052016-06-09 22:53:01 +02005349 int did_aucmd;
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005350
Bram Moolenaar1610d052016-06-09 22:53:01 +02005351 (void)do_doautocmd(arg, TRUE, &did_aucmd);
5352 /* Only when there is no <nomodeline>. */
5353 if (call_do_modelines && did_aucmd)
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005354 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005355}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005356
Bram Moolenaar071d4272004-06-13 20:20:40 +00005357/*
5358 * :[N]bunload[!] [N] [bufname] unload buffer
5359 * :[N]bdelete[!] [N] [bufname] delete buffer from buffer list
5360 * :[N]bwipeout[!] [N] [bufname] delete buffer really
5361 */
5362 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005363ex_bunload(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005364{
Bram Moolenaar8cdbd5b2019-06-16 15:50:45 +02005365 if (ERROR_IF_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02005366 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005367 eap->errmsg = do_bufdel(
5368 eap->cmdidx == CMD_bdelete ? DOBUF_DEL
5369 : eap->cmdidx == CMD_bwipeout ? DOBUF_WIPE
5370 : DOBUF_UNLOAD, eap->arg,
5371 eap->addr_count, (int)eap->line1, (int)eap->line2, eap->forceit);
5372}
5373
5374/*
5375 * :[N]buffer [N] to buffer N
5376 * :[N]sbuffer [N] to buffer N
5377 */
5378 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005379ex_buffer(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005380{
Bram Moolenaar8cdbd5b2019-06-16 15:50:45 +02005381 if (ERROR_IF_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02005382 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005383 if (*eap->arg)
5384 eap->errmsg = e_trailing;
5385 else
5386 {
5387 if (eap->addr_count == 0) /* default is current buffer */
5388 goto_buffer(eap, DOBUF_CURRENT, FORWARD, 0);
5389 else
5390 goto_buffer(eap, DOBUF_FIRST, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005391 if (eap->do_ecmd_cmd != NULL)
5392 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005393 }
5394}
5395
5396/*
5397 * :[N]bmodified [N] to next mod. buffer
5398 * :[N]sbmodified [N] to next mod. buffer
5399 */
5400 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005401ex_bmodified(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005402{
5403 goto_buffer(eap, DOBUF_MOD, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005404 if (eap->do_ecmd_cmd != NULL)
5405 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005406}
5407
5408/*
5409 * :[N]bnext [N] to next buffer
5410 * :[N]sbnext [N] split and to next buffer
5411 */
5412 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005413ex_bnext(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005414{
5415 goto_buffer(eap, DOBUF_CURRENT, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005416 if (eap->do_ecmd_cmd != NULL)
5417 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005418}
5419
5420/*
5421 * :[N]bNext [N] to previous buffer
5422 * :[N]bprevious [N] to previous buffer
5423 * :[N]sbNext [N] split and to previous buffer
5424 * :[N]sbprevious [N] split and to previous buffer
5425 */
5426 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005427ex_bprevious(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005428{
5429 goto_buffer(eap, DOBUF_CURRENT, BACKWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005430 if (eap->do_ecmd_cmd != NULL)
5431 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005432}
5433
5434/*
5435 * :brewind to first buffer
5436 * :bfirst to first buffer
5437 * :sbrewind split and to first buffer
5438 * :sbfirst split and to first buffer
5439 */
5440 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005441ex_brewind(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005442{
5443 goto_buffer(eap, DOBUF_FIRST, FORWARD, 0);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005444 if (eap->do_ecmd_cmd != NULL)
5445 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005446}
5447
5448/*
5449 * :blast to last buffer
5450 * :sblast split and to last buffer
5451 */
5452 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005453ex_blast(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005454{
5455 goto_buffer(eap, DOBUF_LAST, BACKWARD, 0);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005456 if (eap->do_ecmd_cmd != NULL)
5457 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005458}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005459
5460 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005461ends_excmd(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005462{
5463 return (c == NUL || c == '|' || c == '"' || c == '\n');
5464}
5465
5466#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA) || defined(FEAT_EVAL) \
5467 || defined(PROTO)
5468/*
5469 * Return the next command, after the first '|' or '\n'.
5470 * Return NULL if not found.
5471 */
5472 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005473find_nextcmd(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005474{
5475 while (*p != '|' && *p != '\n')
5476 {
5477 if (*p == NUL)
5478 return NULL;
5479 ++p;
5480 }
5481 return (p + 1);
5482}
5483#endif
5484
5485/*
Bram Moolenaar2256c992016-11-15 21:17:07 +01005486 * Check if *p is a separator between Ex commands, skipping over white space.
5487 * Return NULL if it isn't, the following character if it is.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005488 */
5489 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005490check_nextcmd(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005491{
Bram Moolenaar2256c992016-11-15 21:17:07 +01005492 char_u *s = skipwhite(p);
5493
5494 if (*s == '|' || *s == '\n')
5495 return (s + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005496 else
5497 return NULL;
5498}
5499
5500/*
5501 * - if there are more files to edit
5502 * - and this is the last window
5503 * - and forceit not used
5504 * - and not repeated twice on a row
5505 * return FAIL and give error message if 'message' TRUE
5506 * return OK otherwise
5507 */
5508 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005509check_more(
5510 int message, /* when FALSE check only, no messages */
5511 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005512{
5513 int n = ARGCOUNT - curwin->w_arg_idx - 1;
5514
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005515 if (!forceit && only_one_window()
5516 && ARGCOUNT > 1 && !arg_had_last && n >= 0 && quitmore == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005517 {
5518 if (message)
5519 {
5520#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
5521 if ((p_confirm || cmdmod.confirm) && curbuf->b_fname != NULL)
5522 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02005523 char_u buff[DIALOG_MSG_SIZE];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005524
Bram Moolenaarda6e8912018-08-21 15:12:14 +02005525 vim_snprintf((char *)buff, DIALOG_MSG_SIZE,
5526 NGETTEXT("%d more file to edit. Quit anyway?",
5527 "%d more files to edit. Quit anyway?", n), n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005528 if (vim_dialog_yesno(VIM_QUESTION, NULL, buff, 1) == VIM_YES)
5529 return OK;
5530 return FAIL;
5531 }
5532#endif
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01005533 semsg(NGETTEXT("E173: %d more file to edit",
5534 "E173: %d more files to edit", n), n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005535 quitmore = 2; /* next try to quit is allowed */
5536 }
5537 return FAIL;
5538 }
5539 return OK;
5540}
5541
Bram Moolenaar071d4272004-06-13 20:20:40 +00005542/*
5543 * Function given to ExpandGeneric() to obtain the list of command names.
5544 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005545 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005546get_command_name(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005547{
5548 if (idx >= (int)CMD_SIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005549 return get_user_command_name(idx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005550 return cmdnames[idx].cmd_name;
5551}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005552
Bram Moolenaar071d4272004-06-13 20:20:40 +00005553 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005554ex_colorscheme(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005555{
Bram Moolenaare6850792010-05-14 15:28:44 +02005556 if (*eap->arg == NUL)
5557 {
5558#ifdef FEAT_EVAL
5559 char_u *expr = vim_strsave((char_u *)"g:colors_name");
5560 char_u *p = NULL;
5561
5562 if (expr != NULL)
5563 {
5564 ++emsg_off;
5565 p = eval_to_string(expr, NULL, FALSE);
5566 --emsg_off;
5567 vim_free(expr);
5568 }
5569 if (p != NULL)
5570 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01005571 msg((char *)p);
Bram Moolenaare6850792010-05-14 15:28:44 +02005572 vim_free(p);
5573 }
5574 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01005575 msg("default");
Bram Moolenaare6850792010-05-14 15:28:44 +02005576#else
Bram Moolenaar32526b32019-01-19 17:43:09 +01005577 msg(_("unknown"));
Bram Moolenaare6850792010-05-14 15:28:44 +02005578#endif
5579 }
5580 else if (load_colors(eap->arg) == FAIL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005581 semsg(_("E185: Cannot find color scheme '%s'"), eap->arg);
Bram Moolenaarf58d81a2019-01-28 20:19:05 +01005582
5583#ifdef FEAT_VTP
5584 else if (has_vtp_working())
5585 {
5586 // background color change requires clear + redraw
5587 update_screen(CLEAR);
5588 redrawcmd();
5589 }
5590#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005591}
5592
5593 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005594ex_highlight(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005595{
5596 if (*eap->arg == NUL && eap->cmd[2] == '!')
Bram Moolenaar32526b32019-01-19 17:43:09 +01005597 msg(_("Greetings, Vim user!"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005598 do_highlight(eap->arg, eap->forceit, FALSE);
5599}
5600
5601
5602/*
5603 * Call this function if we thought we were going to exit, but we won't
5604 * (because of an error). May need to restore the terminal mode.
5605 */
5606 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005607not_exiting(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005608{
5609 exiting = FALSE;
5610 settmode(TMODE_RAW);
5611}
5612
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005613 static int
5614before_quit_autocmds(win_T *wp, int quit_all, int forceit)
5615{
5616 apply_autocmds(EVENT_QUITPRE, NULL, NULL, FALSE, wp->w_buffer);
5617
5618 /* Bail out when autocommands closed the window.
5619 * Refuse to quit when the buffer in the last window is being closed (can
5620 * only happen in autocommands). */
5621 if (!win_valid(wp)
5622 || curbuf_locked()
5623 || (wp->w_buffer->b_nwindows == 1 && wp->w_buffer->b_locked > 0))
5624 return TRUE;
5625
5626 if (quit_all || (check_more(FALSE, forceit) == OK && only_one_window()))
5627 {
5628 apply_autocmds(EVENT_EXITPRE, NULL, NULL, FALSE, curbuf);
5629 /* Refuse to quit when locked or when the buffer in the last window is
5630 * being closed (can only happen in autocommands). */
5631 if (curbuf_locked()
5632 || (curbuf->b_nwindows == 1 && curbuf->b_locked > 0))
5633 return TRUE;
5634 }
5635
5636 return FALSE;
5637}
5638
Bram Moolenaar071d4272004-06-13 20:20:40 +00005639/*
Bram Moolenaarf240e182014-11-27 18:33:02 +01005640 * ":quit": quit current window, quit Vim if the last window is closed.
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005641 * ":{nr}quit": quit window {nr}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005642 */
5643 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005644ex_quit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005645{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005646 win_T *wp;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005647
Bram Moolenaar071d4272004-06-13 20:20:40 +00005648#ifdef FEAT_CMDWIN
5649 if (cmdwin_type != 0)
5650 {
5651 cmdwin_result = Ctrl_C;
5652 return;
5653 }
5654#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005655 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005656 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005657 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005658 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005659 return;
5660 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005661 if (eap->addr_count > 0)
5662 {
Bram Moolenaarf240e182014-11-27 18:33:02 +01005663 int wnr = eap->line2;
5664
5665 for (wp = firstwin; wp->w_next != NULL; wp = wp->w_next)
5666 if (--wnr <= 0)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005667 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005668 }
5669 else
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005670 wp = curwin;
Bram Moolenaarf240e182014-11-27 18:33:02 +01005671
Bram Moolenaar87ffb5c2017-10-19 12:37:42 +02005672 /* Refuse to quit when locked. */
5673 if (curbuf_locked())
5674 return;
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005675
5676 /* Trigger QuitPre and maybe ExitPre */
5677 if (before_quit_autocmds(wp, FALSE, eap->forceit))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005678 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005679
5680#ifdef FEAT_NETBEANS_INTG
5681 netbeansForcedQuit = eap->forceit;
5682#endif
5683
5684 /*
5685 * If there are more files or windows we won't exit.
5686 */
5687 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
5688 exiting = TRUE;
Bram Moolenaarff930ca2017-10-19 17:12:10 +02005689 if ((!buf_hide(wp->w_buffer)
5690 && check_changed(wp->w_buffer, (p_awa ? CCGD_AW : 0)
Bram Moolenaar45d3b142013-11-09 03:31:51 +01005691 | (eap->forceit ? CCGD_FORCEIT : 0)
5692 | CCGD_EXCMD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005693 || check_more(TRUE, eap->forceit) == FAIL
Bram Moolenaar027387f2016-01-02 22:25:52 +01005694 || (only_one_window() && check_changed_any(eap->forceit, TRUE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005695 {
5696 not_exiting();
5697 }
5698 else
5699 {
Bram Moolenaarc7a0d322015-06-19 12:43:07 +02005700 /* quit last window
5701 * Note: only_one_window() returns true, even so a help window is
5702 * still open. In that case only quit, if no address has been
5703 * specified. Example:
5704 * :h|wincmd w|1q - don't quit
5705 * :h|wincmd w|q - quit
5706 */
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01005707 if (only_one_window() && (ONE_WINDOW || eap->addr_count == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005708 getout(0);
Bram Moolenaar2c33d7b2017-10-14 16:06:20 +02005709 not_exiting();
Bram Moolenaar4033c552017-09-16 20:54:51 +02005710#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00005711 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005712#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005713 /* close window; may free buffer */
Bram Moolenaareb44a682017-08-03 22:44:55 +02005714 win_close(wp, !buf_hide(wp->w_buffer) || eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005715 }
5716}
5717
5718/*
5719 * ":cquit".
5720 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005721 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005722ex_cquit(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005723{
5724 getout(1); /* this does not always pass on the exit code to the Manx
5725 compiler. why? */
5726}
5727
5728/*
5729 * ":qall": try to quit all windows
5730 */
5731 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005732ex_quit_all(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005733{
5734# ifdef FEAT_CMDWIN
5735 if (cmdwin_type != 0)
5736 {
5737 if (eap->forceit)
5738 cmdwin_result = K_XF1; /* ex_window() takes care of this */
5739 else
5740 cmdwin_result = K_XF2;
5741 return;
5742 }
5743# endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005744
5745 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005746 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005747 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005748 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005749 return;
5750 }
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005751
5752 if (before_quit_autocmds(curwin, TRUE, eap->forceit))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005753 return;
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005754
Bram Moolenaar071d4272004-06-13 20:20:40 +00005755 exiting = TRUE;
Bram Moolenaar027387f2016-01-02 22:25:52 +01005756 if (eap->forceit || !check_changed_any(FALSE, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005757 getout(0);
5758 not_exiting();
5759}
5760
Bram Moolenaar071d4272004-06-13 20:20:40 +00005761/*
5762 * ":close": close current window, unless it is the last one
5763 */
5764 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005765ex_close(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005766{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005767 win_T *win;
5768 int winnr = 0;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005769#ifdef FEAT_CMDWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00005770 if (cmdwin_type != 0)
Bram Moolenaar9bd1a7e2011-05-19 14:50:54 +02005771 cmdwin_result = Ctrl_C;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005772 else
Bram Moolenaar4033c552017-09-16 20:54:51 +02005773#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005774 if (!text_locked() && !curbuf_locked())
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005775 {
5776 if (eap->addr_count == 0)
5777 ex_win_close(eap->forceit, curwin, NULL);
Bram Moolenaar6d41c782018-06-06 09:11:12 +02005778 else
5779 {
Bram Moolenaar29323592016-07-24 22:04:11 +02005780 FOR_ALL_WINDOWS(win)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005781 {
5782 winnr++;
5783 if (winnr == eap->line2)
5784 break;
5785 }
5786 if (win == NULL)
5787 win = lastwin;
5788 ex_win_close(eap->forceit, win, NULL);
5789 }
5790 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005791}
5792
Bram Moolenaar4033c552017-09-16 20:54:51 +02005793#ifdef FEAT_QUICKFIX
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005794/*
5795 * ":pclose": Close any preview window.
5796 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005797 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005798ex_pclose(exarg_T *eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005799{
5800 win_T *win;
5801
Bram Moolenaar79648732019-07-18 21:43:07 +02005802 // First close any normal window.
Bram Moolenaar29323592016-07-24 22:04:11 +02005803 FOR_ALL_WINDOWS(win)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005804 if (win->w_p_pvw)
5805 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00005806 ex_win_close(eap->forceit, win, NULL);
Bram Moolenaar79648732019-07-18 21:43:07 +02005807 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005808 }
Bram Moolenaar79648732019-07-18 21:43:07 +02005809# ifdef FEAT_TEXT_PROP
5810 // Also when 'previewpopup' is empty, it might have been cleared.
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02005811 popup_close_preview();
Bram Moolenaar79648732019-07-18 21:43:07 +02005812# endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005813}
Bram Moolenaar4033c552017-09-16 20:54:51 +02005814#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005815
Bram Moolenaarf740b292006-02-16 22:11:02 +00005816/*
5817 * Close window "win" and take care of handling closing the last window for a
5818 * modified buffer.
5819 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005820 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005821ex_win_close(
5822 int forceit,
5823 win_T *win,
5824 tabpage_T *tp) /* NULL or the tab page "win" is in */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005825{
5826 int need_hide;
5827 buf_T *buf = win->w_buffer;
5828
5829 need_hide = (bufIsChanged(buf) && buf->b_nwindows <= 1);
Bram Moolenaareb44a682017-08-03 22:44:55 +02005830 if (need_hide && !buf_hide(buf) && !forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005831 {
Bram Moolenaar4033c552017-09-16 20:54:51 +02005832#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005833 if ((p_confirm || cmdmod.confirm) && p_write)
5834 {
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005835 bufref_T bufref;
5836
5837 set_bufref(&bufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005838 dialog_changed(buf, FALSE);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005839 if (bufref_valid(&bufref) && bufIsChanged(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005840 return;
5841 need_hide = FALSE;
5842 }
5843 else
Bram Moolenaar4033c552017-09-16 20:54:51 +02005844#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005845 {
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02005846 no_write_message();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005847 return;
5848 }
5849 }
5850
Bram Moolenaar4033c552017-09-16 20:54:51 +02005851#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00005852 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005853#endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00005854
Bram Moolenaar071d4272004-06-13 20:20:40 +00005855 /* free buffer when not hiding it or when it's a scratch buffer */
Bram Moolenaarf740b292006-02-16 22:11:02 +00005856 if (tp == NULL)
Bram Moolenaareb44a682017-08-03 22:44:55 +02005857 win_close(win, !need_hide && !buf_hide(buf));
Bram Moolenaarf740b292006-02-16 22:11:02 +00005858 else
Bram Moolenaareb44a682017-08-03 22:44:55 +02005859 win_close_othertab(win, !need_hide && !buf_hide(buf), tp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005860}
5861
Bram Moolenaar071d4272004-06-13 20:20:40 +00005862/*
Bram Moolenaardea25702017-01-29 15:18:10 +01005863 * Handle the argument for a tabpage related ex command.
5864 * Returns a tabpage number.
5865 * When an error is encountered then eap->errmsg is set.
5866 */
5867 static int
5868get_tabpage_arg(exarg_T *eap)
5869{
5870 int tab_number;
5871 int unaccept_arg0 = (eap->cmdidx == CMD_tabmove) ? 0 : 1;
5872
5873 if (eap->arg && *eap->arg != NUL)
5874 {
5875 char_u *p = eap->arg;
5876 char_u *p_save;
5877 int relative = 0; /* argument +N/-N means: go to N places to the
5878 * right/left relative to the current position. */
5879
5880 if (*p == '-')
5881 {
5882 relative = -1;
5883 p++;
5884 }
5885 else if (*p == '+')
5886 {
5887 relative = 1;
5888 p++;
5889 }
5890
5891 p_save = p;
5892 tab_number = getdigits(&p);
5893
5894 if (relative == 0)
5895 {
5896 if (STRCMP(p, "$") == 0)
5897 tab_number = LAST_TAB_NR;
5898 else if (p == p_save || *p_save == '-' || *p != NUL
5899 || tab_number > LAST_TAB_NR)
5900 {
5901 /* No numbers as argument. */
5902 eap->errmsg = e_invarg;
5903 goto theend;
5904 }
5905 }
5906 else
5907 {
5908 if (*p_save == NUL)
5909 tab_number = 1;
5910 else if (p == p_save || *p_save == '-' || *p != NUL
5911 || tab_number == 0)
5912 {
5913 /* No numbers as argument. */
5914 eap->errmsg = e_invarg;
5915 goto theend;
5916 }
5917 tab_number = tab_number * relative + tabpage_index(curtab);
5918 if (!unaccept_arg0 && relative == -1)
5919 --tab_number;
5920 }
5921 if (tab_number < unaccept_arg0 || tab_number > LAST_TAB_NR)
5922 eap->errmsg = e_invarg;
5923 }
5924 else if (eap->addr_count > 0)
5925 {
5926 if (unaccept_arg0 && eap->line2 == 0)
Bram Moolenaarc6251552017-01-29 21:42:20 +01005927 {
Bram Moolenaardea25702017-01-29 15:18:10 +01005928 eap->errmsg = e_invrange;
Bram Moolenaarc6251552017-01-29 21:42:20 +01005929 tab_number = 0;
5930 }
Bram Moolenaardea25702017-01-29 15:18:10 +01005931 else
5932 {
5933 tab_number = eap->line2;
Bram Moolenaar82a12462019-01-22 22:41:42 +01005934 if (!unaccept_arg0 && *skipwhite(*eap->cmdlinep) == '-')
Bram Moolenaardea25702017-01-29 15:18:10 +01005935 {
5936 --tab_number;
5937 if (tab_number < unaccept_arg0)
5938 eap->errmsg = e_invarg;
5939 }
5940 }
5941 }
5942 else
5943 {
5944 switch (eap->cmdidx)
5945 {
5946 case CMD_tabnext:
5947 tab_number = tabpage_index(curtab) + 1;
5948 if (tab_number > LAST_TAB_NR)
5949 tab_number = 1;
5950 break;
5951 case CMD_tabmove:
5952 tab_number = LAST_TAB_NR;
5953 break;
5954 default:
5955 tab_number = tabpage_index(curtab);
5956 }
5957 }
5958
5959theend:
5960 return tab_number;
5961}
5962
5963/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00005964 * ":tabclose": close current tab page, unless it is the last one.
5965 * ":tabclose N": close tab page N.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005966 */
5967 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005968ex_tabclose(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005969{
Bram Moolenaarf740b292006-02-16 22:11:02 +00005970 tabpage_T *tp;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005971 int tab_number;
Bram Moolenaarf740b292006-02-16 22:11:02 +00005972
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005973# ifdef FEAT_CMDWIN
5974 if (cmdwin_type != 0)
5975 cmdwin_result = K_IGNORE;
5976 else
5977# endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00005978 if (first_tabpage->tp_next == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005979 emsg(_("E784: Cannot close last tab page"));
Bram Moolenaarf740b292006-02-16 22:11:02 +00005980 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005981 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005982 tab_number = get_tabpage_arg(eap);
5983 if (eap->errmsg == NULL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005984 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005985 tp = find_tabpage(tab_number);
Bram Moolenaarf740b292006-02-16 22:11:02 +00005986 if (tp == NULL)
5987 {
5988 beep_flush();
5989 return;
5990 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005991 if (tp != curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00005992 {
5993 tabpage_close_other(tp, eap->forceit);
5994 return;
5995 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005996 else if (!text_locked() && !curbuf_locked())
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005997 tabpage_close(eap->forceit);
5998 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005999 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006000}
6001
6002/*
6003 * ":tabonly": close all tab pages except the current one
6004 */
6005 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006006ex_tabonly(exarg_T *eap)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006007{
6008 tabpage_T *tp;
6009 int done;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006010 int tab_number;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006011
6012# ifdef FEAT_CMDWIN
6013 if (cmdwin_type != 0)
6014 cmdwin_result = K_IGNORE;
6015 else
6016# endif
6017 if (first_tabpage->tp_next == NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01006018 msg(_("Already only one tab page"));
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006019 else
6020 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006021 tab_number = get_tabpage_arg(eap);
6022 if (eap->errmsg == NULL)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006023 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006024 goto_tabpage(tab_number);
6025 /* Repeat this up to a 1000 times, because autocommands may
6026 * mess up the lists. */
6027 for (done = 0; done < 1000; ++done)
6028 {
6029 FOR_ALL_TABPAGES(tp)
6030 if (tp->tp_topframe != topframe)
6031 {
6032 tabpage_close_other(tp, eap->forceit);
6033 /* if we failed to close it quit */
6034 if (valid_tabpage(tp))
6035 done = 1000;
6036 /* start over, "tp" is now invalid */
6037 break;
6038 }
6039 if (first_tabpage->tp_next == NULL)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006040 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006041 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006042 }
6043 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006044}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006045
6046/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00006047 * Close the current tab page.
6048 */
6049 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006050tabpage_close(int forceit)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006051{
6052 /* First close all the windows but the current one. If that worked then
6053 * close the last window in this tab, that will close it. */
Bram Moolenaar459ca562016-11-10 18:16:33 +01006054 if (!ONE_WINDOW)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006055 close_others(TRUE, forceit);
Bram Moolenaar459ca562016-11-10 18:16:33 +01006056 if (ONE_WINDOW)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006057 ex_win_close(forceit, curwin, NULL);
6058# ifdef FEAT_GUI
6059 need_mouse_correct = TRUE;
6060# endif
6061}
6062
6063/*
6064 * Close tab page "tp", which is not the current tab page.
6065 * Note that autocommands may make "tp" invalid.
Bram Moolenaar7875acc2006-09-10 13:51:17 +00006066 * Also takes care of the tab pages line disappearing when closing the
6067 * last-but-one tab page.
Bram Moolenaarf740b292006-02-16 22:11:02 +00006068 */
6069 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006070tabpage_close_other(tabpage_T *tp, int forceit)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006071{
6072 int done = 0;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006073 win_T *wp;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00006074 int h = tabline_height();
Bram Moolenaarf740b292006-02-16 22:11:02 +00006075
6076 /* Limit to 1000 windows, autocommands may add a window while we close
6077 * one. OK, so I'm paranoid... */
6078 while (++done < 1000)
6079 {
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006080 wp = tp->tp_firstwin;
6081 ex_win_close(forceit, wp, tp);
Bram Moolenaarf740b292006-02-16 22:11:02 +00006082
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006083 /* Autocommands may delete the tab page under our fingers and we may
6084 * fail to close a window with a modified buffer. */
6085 if (!valid_tabpage(tp) || tp->tp_firstwin == wp)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006086 break;
6087 }
Bram Moolenaar7875acc2006-09-10 13:51:17 +00006088
Bram Moolenaar29323592016-07-24 22:04:11 +02006089 apply_autocmds(EVENT_TABCLOSED, NULL, NULL, FALSE, curbuf);
Bram Moolenaar29323592016-07-24 22:04:11 +02006090
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006091 redraw_tabline = TRUE;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00006092 if (h != tabline_height())
6093 shell_new_rows();
Bram Moolenaarf740b292006-02-16 22:11:02 +00006094}
6095
6096/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006097 * ":only".
6098 */
6099 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006100ex_only(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006101{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01006102 win_T *wp;
6103 int wnr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006104# ifdef FEAT_GUI
6105 need_mouse_correct = TRUE;
6106# endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01006107 if (eap->addr_count > 0)
6108 {
6109 wnr = eap->line2;
6110 for (wp = firstwin; --wnr > 0; )
6111 {
6112 if (wp->w_next == NULL)
6113 break;
6114 else
6115 wp = wp->w_next;
6116 }
6117 win_goto(wp);
6118 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006119 close_others(TRUE, eap->forceit);
6120}
6121
Bram Moolenaar071d4272004-06-13 20:20:40 +00006122 static void
Bram Moolenaar5e1e6d22017-01-02 17:26:00 +01006123ex_hide(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006124{
Bram Moolenaar2256c992016-11-15 21:17:07 +01006125 /* ":hide" or ":hide | cmd": hide current window */
Bram Moolenaar2256c992016-11-15 21:17:07 +01006126 if (!eap->skip)
6127 {
Bram Moolenaar4033c552017-09-16 20:54:51 +02006128#ifdef FEAT_GUI
Bram Moolenaar2256c992016-11-15 21:17:07 +01006129 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006130#endif
Bram Moolenaar2256c992016-11-15 21:17:07 +01006131 if (eap->addr_count == 0)
6132 win_close(curwin, FALSE); /* don't free buffer */
6133 else
6134 {
6135 int winnr = 0;
6136 win_T *win;
Bram Moolenaarf240e182014-11-27 18:33:02 +01006137
Bram Moolenaar2256c992016-11-15 21:17:07 +01006138 FOR_ALL_WINDOWS(win)
6139 {
6140 winnr++;
6141 if (winnr == eap->line2)
6142 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01006143 }
Bram Moolenaar2256c992016-11-15 21:17:07 +01006144 if (win == NULL)
6145 win = lastwin;
6146 win_close(win, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006147 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006148 }
6149}
6150
6151/*
6152 * ":stop" and ":suspend": Suspend Vim.
6153 */
6154 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006155ex_stop(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006156{
6157 /*
6158 * Disallow suspending for "rvim".
6159 */
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006160 if (!check_restricted())
Bram Moolenaar071d4272004-06-13 20:20:40 +00006161 {
6162 if (!eap->forceit)
6163 autowrite_all();
6164 windgoto((int)Rows - 1, 0);
6165 out_char('\n');
6166 out_flush();
6167 stoptermcap();
6168 out_flush(); /* needed for SUN to restore xterm buffer */
6169#ifdef FEAT_TITLE
Bram Moolenaar40385db2018-08-07 22:31:44 +02006170 mch_restore_title(SAVE_RESTORE_BOTH); /* restore window titles */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006171#endif
6172 ui_suspend(); /* call machine specific function */
6173#ifdef FEAT_TITLE
6174 maketitle();
6175 resettitle(); /* force updating the title */
6176#endif
6177 starttermcap();
6178 scroll_start(); /* scroll screen before redrawing */
6179 redraw_later_clear();
6180 shell_resized(); /* may have resized window */
6181 }
6182}
6183
6184/*
Bram Moolenaar12a96de2018-03-11 14:44:18 +01006185 * ":exit", ":xit" and ":wq": Write file and quite the current window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006186 */
6187 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006188ex_exit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006189{
6190#ifdef FEAT_CMDWIN
6191 if (cmdwin_type != 0)
6192 {
6193 cmdwin_result = Ctrl_C;
6194 return;
6195 }
6196#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006197 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006198 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006199 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006200 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006201 return;
6202 }
Bram Moolenaar12a96de2018-03-11 14:44:18 +01006203
6204 if (before_quit_autocmds(curwin, FALSE, eap->forceit))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006205 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006206
6207 /*
6208 * if more files or windows we won't exit
6209 */
6210 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
6211 exiting = TRUE;
6212 if ( ((eap->cmdidx == CMD_wq
6213 || curbufIsChanged())
6214 && do_write(eap) == FAIL)
6215 || check_more(TRUE, eap->forceit) == FAIL
Bram Moolenaar027387f2016-01-02 22:25:52 +01006216 || (only_one_window() && check_changed_any(eap->forceit, FALSE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006217 {
6218 not_exiting();
6219 }
6220 else
6221 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006222 if (only_one_window()) /* quit last window, exit Vim */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006223 getout(0);
Bram Moolenaar2c33d7b2017-10-14 16:06:20 +02006224 not_exiting();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006225# ifdef FEAT_GUI
6226 need_mouse_correct = TRUE;
6227# endif
Bram Moolenaar1a4a75c2013-07-28 16:03:06 +02006228 /* Quit current window, may free the buffer. */
Bram Moolenaareb44a682017-08-03 22:44:55 +02006229 win_close(curwin, !buf_hide(curwin->w_buffer));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006230 }
6231}
6232
6233/*
6234 * ":print", ":list", ":number".
6235 */
6236 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006237ex_print(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006238{
Bram Moolenaardf177f62005-02-22 08:39:57 +00006239 if (curbuf->b_ml.ml_flags & ML_EMPTY)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006240 emsg(_(e_emptybuf));
Bram Moolenaardf177f62005-02-22 08:39:57 +00006241 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006242 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00006243 for ( ;!got_int; ui_breakcheck())
6244 {
6245 print_line(eap->line1,
6246 (eap->cmdidx == CMD_number || eap->cmdidx == CMD_pound
6247 || (eap->flags & EXFLAG_NR)),
6248 eap->cmdidx == CMD_list || (eap->flags & EXFLAG_LIST));
6249 if (++eap->line1 > eap->line2)
6250 break;
6251 out_flush(); /* show one line at a time */
6252 }
6253 setpcmark();
6254 /* put cursor at last line */
6255 curwin->w_cursor.lnum = eap->line2;
6256 beginline(BL_SOL | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006257 }
6258
Bram Moolenaar071d4272004-06-13 20:20:40 +00006259 ex_no_reprint = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006260}
6261
6262#ifdef FEAT_BYTEOFF
6263 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006264ex_goto(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006265{
6266 goto_byte(eap->line2);
6267}
6268#endif
6269
6270/*
6271 * ":shell".
6272 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006273 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006274ex_shell(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006275{
6276 do_shell(NULL, 0);
6277}
6278
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006279#if defined(HAVE_DROP_FILE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006280
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006281static int drop_busy = FALSE;
6282static int drop_filec;
6283static char_u **drop_filev = NULL;
6284static int drop_split;
6285static void (*drop_callback)(void *);
6286static void *drop_cookie;
6287
6288 static void
6289handle_drop_internal(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006290{
6291 exarg_T ea;
6292 int save_msg_scroll = msg_scroll;
6293
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006294 // Setting the argument list may cause screen updates and being called
6295 // recursively. Avoid that by setting drop_busy.
6296 drop_busy = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006297
6298 /* Check whether the current buffer is changed. If so, we will need
6299 * to split the current window or data could be lost.
6300 * We don't need to check if the 'hidden' option is set, as in this
6301 * case the buffer won't be lost.
6302 */
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006303 if (!buf_hide(curbuf) && !drop_split)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006304 {
6305 ++emsg_off;
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006306 drop_split = check_changed(curbuf, CCGD_AW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006307 --emsg_off;
6308 }
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006309 if (drop_split)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006310 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006311 if (win_split(0, 0) == FAIL)
6312 return;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02006313 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006314
6315 /* When splitting the window, create a new alist. Otherwise the
6316 * existing one is overwritten. */
6317 alist_unlink(curwin->w_alist);
6318 alist_new();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006319 }
6320
6321 /*
6322 * Set up the new argument list.
6323 */
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006324 alist_set(ALIST(curwin), drop_filec, drop_filev, FALSE, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006325
6326 /*
6327 * Move to the first file.
6328 */
6329 /* Fake up a minimal "next" command for do_argfile() */
6330 vim_memset(&ea, 0, sizeof(ea));
6331 ea.cmd = (char_u *)"next";
6332 do_argfile(&ea, 0);
6333
6334 /* do_ecmd() may set need_start_insertmode, but since we never left Insert
6335 * mode that is not needed here. */
6336 need_start_insertmode = FALSE;
6337
6338 /* Restore msg_scroll, otherwise a following command may cause scrolling
6339 * unexpectedly. The screen will be redrawn by the caller, thus
6340 * msg_scroll being set by displaying a message is irrelevant. */
6341 msg_scroll = save_msg_scroll;
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006342
6343 if (drop_callback != NULL)
6344 drop_callback(drop_cookie);
6345
6346 drop_filev = NULL;
6347 drop_busy = FALSE;
6348}
6349
6350/*
6351 * Handle a file drop. The code is here because a drop is *nearly* like an
6352 * :args command, but not quite (we have a list of exact filenames, so we
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01006353 * don't want to (a) parse a command line, or (b) expand wildcards). So the
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006354 * code is very similar to :args and hence needs access to a lot of the static
6355 * functions in this file.
6356 *
6357 * The "filev" list must have been allocated using alloc(), as should each item
6358 * in the list. This function takes over responsibility for freeing the "filev"
6359 * list.
6360 */
6361 void
6362handle_drop(
6363 int filec, // the number of files dropped
6364 char_u **filev, // the list of files dropped
6365 int split, // force splitting the window
6366 void (*callback)(void *), // to be called after setting the argument
6367 // list
6368 void *cookie) // argument for "callback" (allocated)
6369{
6370 // Cannot handle recursive drops, finish the pending one.
6371 if (drop_busy)
6372 {
6373 FreeWild(filec, filev);
6374 vim_free(cookie);
6375 return;
6376 }
6377
6378 // When calling handle_drop() more than once in a row we only use the last
6379 // one.
6380 if (drop_filev != NULL)
6381 {
6382 FreeWild(drop_filec, drop_filev);
6383 vim_free(drop_cookie);
6384 }
6385
6386 drop_filec = filec;
6387 drop_filev = filev;
6388 drop_split = split;
6389 drop_callback = callback;
6390 drop_cookie = cookie;
6391
6392 // Postpone this when:
6393 // - editing the command line
6394 // - not possible to change the current buffer
6395 // - updating the screen
6396 // As it may change buffers and window structures that are in use and cause
6397 // freed memory to be used.
6398 if (text_locked() || curbuf_locked() || updating_screen)
6399 return;
6400
6401 handle_drop_internal();
6402}
6403
6404/*
6405 * To be called when text is unlocked, curbuf is unlocked or updating_screen is
6406 * reset: Handle a postponed drop.
6407 */
6408 void
6409handle_any_postponed_drop(void)
6410{
6411 if (!drop_busy && drop_filev != NULL
6412 && !text_locked() && !curbuf_locked() && !updating_screen)
6413 handle_drop_internal();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006414}
6415#endif
6416
Bram Moolenaar071d4272004-06-13 20:20:40 +00006417/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006418 * ":preserve".
6419 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006420 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006421ex_preserve(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006422{
Bram Moolenaar4399ef42005-02-12 14:29:27 +00006423 curbuf->b_flags |= BF_PRESERVED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006424 ml_preserve(curbuf, TRUE);
6425}
6426
6427/*
6428 * ":recover".
6429 */
6430 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006431ex_recover(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006432{
6433 /* Set recoverymode right away to avoid the ATTENTION prompt. */
6434 recoverymode = TRUE;
Bram Moolenaar45d3b142013-11-09 03:31:51 +01006435 if (!check_changed(curbuf, (p_awa ? CCGD_AW : 0)
6436 | CCGD_MULTWIN
6437 | (eap->forceit ? CCGD_FORCEIT : 0)
6438 | CCGD_EXCMD)
6439
Bram Moolenaar071d4272004-06-13 20:20:40 +00006440 && (*eap->arg == NUL
6441 || setfname(curbuf, eap->arg, NULL, TRUE) == OK))
Bram Moolenaar99499b12019-05-23 21:35:48 +02006442 ml_recover(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006443 recoverymode = FALSE;
6444}
6445
6446/*
6447 * Command modifier used in a wrong way.
6448 */
6449 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006450ex_wrongmodifier(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006451{
6452 eap->errmsg = e_invcmd;
6453}
6454
Bram Moolenaar071d4272004-06-13 20:20:40 +00006455/*
6456 * :sview [+command] file split window with new file, read-only
6457 * :split [[+command] file] split window with current or new file
6458 * :vsplit [[+command] file] split window vertically with current or new file
6459 * :new [[+command] file] split window with no or new file
6460 * :vnew [[+command] file] split vertically window with no or new file
6461 * :sfind [+command] file split window with file in 'path'
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006462 *
6463 * :tabedit open new Tab page with empty window
6464 * :tabedit [+command] file open new Tab page and edit "file"
6465 * :tabnew [[+command] file] just like :tabedit
6466 * :tabfind [+command] file open new Tab page and find "file"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006467 */
6468 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006469ex_splitview(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006470{
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006471 win_T *old_curwin = curwin;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006472#if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006473 char_u *fname = NULL;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006474#endif
6475#ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00006476 int browse_flag = cmdmod.browse;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006477#endif
Bram Moolenaar39902a02018-06-21 22:10:08 +02006478 int use_tab = eap->cmdidx == CMD_tabedit
6479 || eap->cmdidx == CMD_tabfind
6480 || eap->cmdidx == CMD_tabnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006481
Bram Moolenaar8cdbd5b2019-06-16 15:50:45 +02006482 if (ERROR_IF_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02006483 return;
6484
Bram Moolenaar4033c552017-09-16 20:54:51 +02006485#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00006486 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006487#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006488
Bram Moolenaar4033c552017-09-16 20:54:51 +02006489#ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00006490 /* A ":split" in the quickfix window works like ":new". Don't want two
Bram Moolenaar05bb9532008-07-04 09:44:11 +00006491 * quickfix windows. But it's OK when doing ":tab split". */
6492 if (bt_quickfix(curbuf) && cmdmod.tab == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006493 {
6494 if (eap->cmdidx == CMD_split)
6495 eap->cmdidx = CMD_new;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006496 if (eap->cmdidx == CMD_vsplit)
6497 eap->cmdidx = CMD_vnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006498 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02006499#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006500
Bram Moolenaar4033c552017-09-16 20:54:51 +02006501#ifdef FEAT_SEARCHPATH
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006502 if (eap->cmdidx == CMD_sfind || eap->cmdidx == CMD_tabfind)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006503 {
6504 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg),
6505 FNAME_MESS, TRUE, curbuf->b_ffname);
6506 if (fname == NULL)
6507 goto theend;
6508 eap->arg = fname;
6509 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006510# ifdef FEAT_BROWSE
Bram Moolenaar4033c552017-09-16 20:54:51 +02006511 else
6512# endif
6513#endif
6514#ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00006515 if (cmdmod.browse
Bram Moolenaar071d4272004-06-13 20:20:40 +00006516 && eap->cmdidx != CMD_vnew
Bram Moolenaar071d4272004-06-13 20:20:40 +00006517 && eap->cmdidx != CMD_new)
6518 {
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006519 if (
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006520# ifdef FEAT_GUI
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006521 !gui.in_use &&
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006522# endif
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006523 au_has_group((char_u *)"FileExplorer"))
6524 {
6525 /* No browsing supported but we do have the file explorer:
6526 * Edit the directory. */
6527 if (*eap->arg == NUL || !mch_isdir(eap->arg))
6528 eap->arg = (char_u *)".";
6529 }
6530 else
6531 {
Bram Moolenaar39902a02018-06-21 22:10:08 +02006532 fname = do_browse(0, (char_u *)(use_tab
6533 ? _("Edit File in new tab page")
6534 : _("Edit File in new window")),
Bram Moolenaar071d4272004-06-13 20:20:40 +00006535 eap->arg, NULL, NULL, NULL, curbuf);
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006536 if (fname == NULL)
6537 goto theend;
6538 eap->arg = fname;
6539 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006540 }
6541 cmdmod.browse = FALSE; /* Don't browse again in do_ecmd(). */
Bram Moolenaar4033c552017-09-16 20:54:51 +02006542#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006543
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006544 /*
6545 * Either open new tab page or split the window.
6546 */
Bram Moolenaar39902a02018-06-21 22:10:08 +02006547 if (use_tab)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006548 {
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006549 if (win_new_tabpage(cmdmod.tab != 0 ? cmdmod.tab
6550 : eap->addr_count == 0 ? 0
6551 : (int)eap->line2 + 1) != FAIL)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006552 {
Bram Moolenaard2b66012008-01-09 19:30:36 +00006553 do_exedit(eap, old_curwin);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006554
6555 /* set the alternate buffer for the window we came from */
6556 if (curwin != old_curwin
6557 && win_valid(old_curwin)
6558 && old_curwin->w_buffer != curbuf
6559 && !cmdmod.keepalt)
6560 old_curwin->w_alt_fnum = curbuf->b_fnum;
6561 }
6562 }
6563 else if (win_split(eap->addr_count > 0 ? (int)eap->line2 : 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006564 *eap->cmd == 'v' ? WSP_VERT : 0) != FAIL)
6565 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006566 /* Reset 'scrollbind' when editing another file, but keep it when
6567 * doing ":split" without arguments. */
6568 if (*eap->arg != NUL
Bram Moolenaar8a3bb562018-03-04 20:14:14 +01006569# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00006570 || cmdmod.browse
Bram Moolenaar8a3bb562018-03-04 20:14:14 +01006571# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006572 )
Bram Moolenaar3368ea22010-09-21 16:56:35 +02006573 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006574 else
6575 do_check_scrollbind(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006576 do_exedit(eap, old_curwin);
6577 }
6578
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006579# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00006580 cmdmod.browse = browse_flag;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006581# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006582
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006583# if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006584theend:
6585 vim_free(fname);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006586# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006587}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006588
6589/*
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006590 * Open a new tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006591 */
6592 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006593tabpage_new(void)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006594{
6595 exarg_T ea;
6596
6597 vim_memset(&ea, 0, sizeof(ea));
6598 ea.cmdidx = CMD_tabnew;
6599 ea.cmd = (char_u *)"tabn";
6600 ea.arg = (char_u *)"";
6601 ex_splitview(&ea);
6602}
6603
6604/*
6605 * :tabnext command
6606 */
6607 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006608ex_tabnext(exarg_T *eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006609{
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006610 int tab_number;
6611
Bram Moolenaar8cdbd5b2019-06-16 15:50:45 +02006612 if (ERROR_IF_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02006613 return;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006614 switch (eap->cmdidx)
6615 {
6616 case CMD_tabfirst:
6617 case CMD_tabrewind:
6618 goto_tabpage(1);
6619 break;
6620 case CMD_tablast:
6621 goto_tabpage(9999);
6622 break;
6623 case CMD_tabprevious:
6624 case CMD_tabNext:
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006625 if (eap->arg && *eap->arg != NUL)
6626 {
6627 char_u *p = eap->arg;
6628 char_u *p_save = p;
6629
6630 tab_number = getdigits(&p);
6631 if (p == p_save || *p_save == '-' || *p != NUL
6632 || tab_number == 0)
6633 {
6634 /* No numbers as argument. */
6635 eap->errmsg = e_invarg;
6636 return;
6637 }
6638 }
6639 else
6640 {
6641 if (eap->addr_count == 0)
6642 tab_number = 1;
6643 else
6644 {
6645 tab_number = eap->line2;
6646 if (tab_number < 1)
6647 {
6648 eap->errmsg = e_invrange;
6649 return;
6650 }
6651 }
6652 }
6653 goto_tabpage(-tab_number);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006654 break;
6655 default: /* CMD_tabnext */
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006656 tab_number = get_tabpage_arg(eap);
6657 if (eap->errmsg == NULL)
6658 goto_tabpage(tab_number);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006659 break;
6660 }
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006661}
6662
6663/*
6664 * :tabmove command
6665 */
6666 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006667ex_tabmove(exarg_T *eap)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006668{
Bram Moolenaar40ce3a42015-04-21 18:08:39 +02006669 int tab_number;
Bram Moolenaar8cb8dca2012-07-06 18:27:39 +02006670
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006671 tab_number = get_tabpage_arg(eap);
6672 if (eap->errmsg == NULL)
6673 tabpage_move(tab_number);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006674}
6675
6676/*
6677 * :tabs command: List tabs and their contents.
6678 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006679 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006680ex_tabs(exarg_T *eap UNUSED)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006681{
6682 tabpage_T *tp;
6683 win_T *wp;
6684 int tabcount = 1;
6685
6686 msg_start();
6687 msg_scroll = TRUE;
6688 for (tp = first_tabpage; tp != NULL && !got_int; tp = tp->tp_next)
6689 {
6690 msg_putchar('\n');
6691 vim_snprintf((char *)IObuff, IOSIZE, _("Tab page %d"), tabcount++);
Bram Moolenaar8820b482017-03-16 17:23:31 +01006692 msg_outtrans_attr(IObuff, HL_ATTR(HLF_T));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006693 out_flush(); /* output one line at a time */
6694 ui_breakcheck();
6695
Bram Moolenaar030f0df2006-02-21 22:02:53 +00006696 if (tp == curtab)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006697 wp = firstwin;
6698 else
6699 wp = tp->tp_firstwin;
6700 for ( ; wp != NULL && !got_int; wp = wp->w_next)
6701 {
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006702 msg_putchar('\n');
6703 msg_putchar(wp == curwin ? '>' : ' ');
6704 msg_putchar(' ');
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006705 msg_putchar(bufIsChanged(wp->w_buffer) ? '+' : ' ');
6706 msg_putchar(' ');
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006707 if (buf_spname(wp->w_buffer) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02006708 vim_strncpy(IObuff, buf_spname(wp->w_buffer), IOSIZE - 1);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006709 else
6710 home_replace(wp->w_buffer, wp->w_buffer->b_fname,
6711 IObuff, IOSIZE, TRUE);
6712 msg_outtrans(IObuff);
6713 out_flush(); /* output one line at a time */
6714 ui_breakcheck();
6715 }
6716 }
6717}
6718
Bram Moolenaar071d4272004-06-13 20:20:40 +00006719/*
6720 * ":mode": Set screen mode.
6721 * If no argument given, just get the screen size and redraw.
6722 */
6723 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006724ex_mode(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006725{
6726 if (*eap->arg == NUL)
6727 shell_resized();
6728 else
6729 mch_screenmode(eap->arg);
6730}
6731
Bram Moolenaar071d4272004-06-13 20:20:40 +00006732/*
6733 * ":resize".
6734 * set, increment or decrement current window height
6735 */
6736 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006737ex_resize(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006738{
6739 int n;
6740 win_T *wp = curwin;
6741
6742 if (eap->addr_count > 0)
6743 {
6744 n = eap->line2;
6745 for (wp = firstwin; wp->w_next != NULL && --n > 0; wp = wp->w_next)
6746 ;
6747 }
6748
Bram Moolenaar44a2f922016-03-19 22:11:51 +01006749# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00006750 need_mouse_correct = TRUE;
Bram Moolenaar44a2f922016-03-19 22:11:51 +01006751# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006752 n = atol((char *)eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006753 if (cmdmod.split & WSP_VERT)
6754 {
6755 if (*eap->arg == '-' || *eap->arg == '+')
Bram Moolenaar02631462017-09-22 15:20:32 +02006756 n += curwin->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006757 else if (n == 0 && eap->arg[0] == NUL) /* default is very wide */
6758 n = 9999;
6759 win_setwidth_win((int)n, wp);
6760 }
6761 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006762 {
6763 if (*eap->arg == '-' || *eap->arg == '+')
6764 n += curwin->w_height;
Bram Moolenaar1f2903c2017-07-23 19:51:01 +02006765 else if (n == 0 && eap->arg[0] == NUL) /* default is very high */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006766 n = 9999;
6767 win_setheight_win((int)n, wp);
6768 }
6769}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006770
6771/*
6772 * ":find [+command] <file>" command.
6773 */
6774 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006775ex_find(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006776{
6777#ifdef FEAT_SEARCHPATH
6778 char_u *fname;
6779 int count;
6780
6781 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg), FNAME_MESS,
6782 TRUE, curbuf->b_ffname);
6783 if (eap->addr_count > 0)
6784 {
6785 /* Repeat finding the file "count" times. This matters when it
6786 * appears several times in the path. */
6787 count = eap->line2;
6788 while (fname != NULL && --count > 0)
6789 {
6790 vim_free(fname);
6791 fname = find_file_in_path(NULL, 0, FNAME_MESS,
6792 FALSE, curbuf->b_ffname);
6793 }
6794 }
6795
6796 if (fname != NULL)
6797 {
6798 eap->arg = fname;
6799#endif
6800 do_exedit(eap, NULL);
6801#ifdef FEAT_SEARCHPATH
6802 vim_free(fname);
6803 }
6804#endif
6805}
6806
6807/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00006808 * ":open" simulation: for now just work like ":visual".
6809 */
6810 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006811ex_open(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00006812{
6813 regmatch_T regmatch;
6814 char_u *p;
6815
6816 curwin->w_cursor.lnum = eap->line2;
6817 beginline(BL_SOL | BL_FIX);
6818 if (*eap->arg == '/')
6819 {
6820 /* ":open /pattern/": put cursor in column found with pattern */
6821 ++eap->arg;
6822 p = skip_regexp(eap->arg, '/', p_magic, NULL);
6823 *p = NUL;
6824 regmatch.regprog = vim_regcomp(eap->arg, p_magic ? RE_MAGIC : 0);
6825 if (regmatch.regprog != NULL)
6826 {
6827 regmatch.rm_ic = p_ic;
6828 p = ml_get_curline();
6829 if (vim_regexec(&regmatch, p, (colnr_T)0))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006830 curwin->w_cursor.col = (colnr_T)(regmatch.startp[0] - p);
Bram Moolenaardf177f62005-02-22 08:39:57 +00006831 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006832 emsg(_(e_nomatch));
Bram Moolenaar473de612013-06-08 18:19:48 +02006833 vim_regfree(regmatch.regprog);
Bram Moolenaardf177f62005-02-22 08:39:57 +00006834 }
6835 /* Move to the NUL, ignore any other arguments. */
6836 eap->arg += STRLEN(eap->arg);
6837 }
6838 check_cursor();
6839
6840 eap->cmdidx = CMD_visual;
6841 do_exedit(eap, NULL);
6842}
6843
6844/*
6845 * ":edit", ":badd", ":visual".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006846 */
6847 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006848ex_edit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006849{
6850 do_exedit(eap, NULL);
6851}
6852
6853/*
6854 * ":edit <file>" command and alikes.
6855 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006856 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006857do_exedit(
6858 exarg_T *eap,
6859 win_T *old_curwin) /* curwin before doing a split or NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006860{
6861 int n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006862 int need_hide;
Bram Moolenaardf177f62005-02-22 08:39:57 +00006863 int exmode_was = exmode_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006864
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02006865 if (eap->cmdidx != CMD_pedit && ERROR_IF_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02006866 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006867 /*
6868 * ":vi" command ends Ex mode.
6869 */
6870 if (exmode_active && (eap->cmdidx == CMD_visual
6871 || eap->cmdidx == CMD_view))
6872 {
6873 exmode_active = FALSE;
6874 if (*eap->arg == NUL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00006875 {
6876 /* Special case: ":global/pat/visual\NLvi-commands" */
6877 if (global_busy)
6878 {
6879 int rd = RedrawingDisabled;
6880 int nwr = no_wait_return;
6881 int ms = msg_scroll;
6882#ifdef FEAT_GUI
6883 int he = hold_gui_events;
6884#endif
6885
6886 if (eap->nextcmd != NULL)
6887 {
6888 stuffReadbuff(eap->nextcmd);
6889 eap->nextcmd = NULL;
6890 }
6891
6892 if (exmode_was != EXMODE_VIM)
6893 settmode(TMODE_RAW);
6894 RedrawingDisabled = 0;
6895 no_wait_return = 0;
6896 need_wait_return = FALSE;
6897 msg_scroll = 0;
6898#ifdef FEAT_GUI
6899 hold_gui_events = 0;
6900#endif
6901 must_redraw = CLEAR;
6902
6903 main_loop(FALSE, TRUE);
6904
6905 RedrawingDisabled = rd;
6906 no_wait_return = nwr;
6907 msg_scroll = ms;
6908#ifdef FEAT_GUI
6909 hold_gui_events = he;
6910#endif
6911 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006912 return;
Bram Moolenaardf177f62005-02-22 08:39:57 +00006913 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006914 }
6915
6916 if ((eap->cmdidx == CMD_new
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006917 || eap->cmdidx == CMD_tabnew
6918 || eap->cmdidx == CMD_tabedit
Bram Moolenaar4033c552017-09-16 20:54:51 +02006919 || eap->cmdidx == CMD_vnew) && *eap->arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006920 {
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006921 /* ":new" or ":tabnew" without argument: edit an new empty buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006922 setpcmark();
6923 (void)do_ecmd(0, NULL, NULL, eap, ECMD_ONE,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00006924 ECMD_HIDE + (eap->forceit ? ECMD_FORCEIT : 0),
6925 old_curwin == NULL ? curwin : NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006926 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02006927 else if ((eap->cmdidx != CMD_split && eap->cmdidx != CMD_vsplit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006928 || *eap->arg != NUL
6929#ifdef FEAT_BROWSE
6930 || cmdmod.browse
6931#endif
6932 )
6933 {
Bram Moolenaar5555acc2006-04-07 21:33:12 +00006934 /* Can't edit another file when "curbuf_lock" is set. Only ":edit"
6935 * can bring us here, others are stopped earlier. */
6936 if (*eap->arg != NUL && curbuf_locked())
6937 return;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006938
Bram Moolenaar071d4272004-06-13 20:20:40 +00006939 n = readonlymode;
6940 if (eap->cmdidx == CMD_view || eap->cmdidx == CMD_sview)
6941 readonlymode = TRUE;
6942 else if (eap->cmdidx == CMD_enew)
6943 readonlymode = FALSE; /* 'readonly' doesn't make sense in an
6944 empty buffer */
6945 setpcmark();
6946 if (do_ecmd(0, (eap->cmdidx == CMD_enew ? NULL : eap->arg),
6947 NULL, eap,
6948 /* ":edit" goes to first line if Vi compatible */
6949 (*eap->arg == NUL && eap->do_ecmd_lnum == 0
6950 && vim_strchr(p_cpo, CPO_GOTO1) != NULL)
6951 ? ECMD_ONE : eap->do_ecmd_lnum,
Bram Moolenaareb44a682017-08-03 22:44:55 +02006952 (buf_hide(curbuf) ? ECMD_HIDE : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006953 + (eap->forceit ? ECMD_FORCEIT : 0)
Bram Moolenaar7b449342014-03-25 13:03:48 +01006954 /* after a split we can use an existing buffer */
6955 + (old_curwin != NULL ? ECMD_OLDBUF : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006956 + (eap->cmdidx == CMD_badd ? ECMD_ADDBUF : 0 )
Bram Moolenaar701f7af2008-11-15 13:12:07 +00006957 , old_curwin == NULL ? curwin : NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006958 {
6959 /* Editing the file failed. If the window was split, close it. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006960 if (old_curwin != NULL)
6961 {
6962 need_hide = (curbufIsChanged() && curbuf->b_nwindows <= 1);
Bram Moolenaareb44a682017-08-03 22:44:55 +02006963 if (!need_hide || buf_hide(curbuf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006964 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006965#if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00006966 cleanup_T cs;
6967
6968 /* Reset the error/interrupt/exception state here so that
6969 * aborting() returns FALSE when closing a window. */
6970 enter_cleanup(&cs);
Bram Moolenaar4033c552017-09-16 20:54:51 +02006971#endif
6972#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00006973 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006974#endif
Bram Moolenaareb44a682017-08-03 22:44:55 +02006975 win_close(curwin, !need_hide && !buf_hide(curbuf));
Bram Moolenaarc0197e22004-09-13 20:26:32 +00006976
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006977#if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00006978 /* Restore the error/interrupt/exception state if not
6979 * discarded by a new aborting error, interrupt, or
6980 * uncaught exception. */
6981 leave_cleanup(&cs);
Bram Moolenaar4033c552017-09-16 20:54:51 +02006982#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006983 }
6984 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006985 }
6986 else if (readonlymode && curbuf->b_nwindows == 1)
6987 {
6988 /* When editing an already visited buffer, 'readonly' won't be set
6989 * but the previous value is kept. With ":view" and ":sview" we
6990 * want the file to be readonly, except when another window is
6991 * editing the same buffer. */
6992 curbuf->b_p_ro = TRUE;
6993 }
6994 readonlymode = n;
6995 }
6996 else
6997 {
6998 if (eap->do_ecmd_cmd != NULL)
6999 do_cmdline_cmd(eap->do_ecmd_cmd);
7000#ifdef FEAT_TITLE
7001 n = curwin->w_arg_idx_invalid;
7002#endif
7003 check_arg_idx(curwin);
7004#ifdef FEAT_TITLE
7005 if (n != curwin->w_arg_idx_invalid)
7006 maketitle();
7007#endif
7008 }
7009
Bram Moolenaar071d4272004-06-13 20:20:40 +00007010 /*
7011 * if ":split file" worked, set alternate file name in old window to new
7012 * file
7013 */
7014 if (old_curwin != NULL
7015 && *eap->arg != NUL
7016 && curwin != old_curwin
7017 && win_valid(old_curwin)
Bram Moolenaard4755bb2004-09-02 19:12:26 +00007018 && old_curwin->w_buffer != curbuf
7019 && !cmdmod.keepalt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007020 old_curwin->w_alt_fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007021
7022 ex_no_reprint = TRUE;
7023}
7024
7025#ifndef FEAT_GUI
7026/*
7027 * ":gui" and ":gvim" when there is no GUI.
7028 */
7029 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007030ex_nogui(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007031{
7032 eap->errmsg = e_nogvim;
7033}
7034#endif
7035
Bram Moolenaar4f974752019-02-17 17:44:42 +01007036#if defined(FEAT_GUI_MSWIN) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007037 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007038ex_tearoff(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007039{
7040 gui_make_tearoff(eap->arg);
7041}
7042#endif
7043
Bram Moolenaar29a2c082018-03-05 21:06:23 +01007044#if (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK) \
7045 || defined(FEAT_TERM_POPUP_MENU)) && defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007046 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007047ex_popup(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007048{
Bram Moolenaar29a2c082018-03-05 21:06:23 +01007049# if defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK)
7050 if (gui.in_use)
7051 gui_make_popup(eap->arg, eap->forceit);
7052# ifdef FEAT_TERM_POPUP_MENU
7053 else
7054# endif
7055# endif
7056# ifdef FEAT_TERM_POPUP_MENU
7057 pum_make_popup(eap->arg, eap->forceit);
7058# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007059}
7060#endif
7061
Bram Moolenaar071d4272004-06-13 20:20:40 +00007062 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007063ex_swapname(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007064{
7065 if (curbuf->b_ml.ml_mfp == NULL || curbuf->b_ml.ml_mfp->mf_fname == NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01007066 msg(_("No swap file"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007067 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01007068 msg((char *)curbuf->b_ml.ml_mfp->mf_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007069}
7070
7071/*
7072 * ":syncbind" forces all 'scrollbind' windows to have the same relative
7073 * offset.
7074 * (1998-11-02 16:21:01 R. Edward Ralston <eralston@computer.org>)
7075 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007076 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007077ex_syncbind(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007078{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007079 win_T *wp;
Bram Moolenaardedd1b02013-12-14 13:06:17 +01007080 win_T *save_curwin = curwin;
7081 buf_T *save_curbuf = curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007082 long topline;
7083 long y;
7084 linenr_T old_linenr = curwin->w_cursor.lnum;
7085
7086 setpcmark();
7087
7088 /*
7089 * determine max topline
7090 */
7091 if (curwin->w_p_scb)
7092 {
7093 topline = curwin->w_topline;
Bram Moolenaar29323592016-07-24 22:04:11 +02007094 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007095 {
7096 if (wp->w_p_scb && wp->w_buffer)
7097 {
Bram Moolenaar375e3392019-01-31 18:26:10 +01007098 y = wp->w_buffer->b_ml.ml_line_count - get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007099 if (topline > y)
7100 topline = y;
7101 }
7102 }
7103 if (topline < 1)
7104 topline = 1;
7105 }
7106 else
7107 {
7108 topline = 1;
7109 }
7110
7111
7112 /*
Bram Moolenaardedd1b02013-12-14 13:06:17 +01007113 * Set all scrollbind windows to the same topline.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007114 */
Bram Moolenaar29323592016-07-24 22:04:11 +02007115 FOR_ALL_WINDOWS(curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007116 {
7117 if (curwin->w_p_scb)
7118 {
Bram Moolenaardedd1b02013-12-14 13:06:17 +01007119 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007120 y = topline - curwin->w_topline;
7121 if (y > 0)
7122 scrollup(y, TRUE);
7123 else
7124 scrolldown(-y, TRUE);
7125 curwin->w_scbind_pos = topline;
7126 redraw_later(VALID);
7127 cursor_correct();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007128 curwin->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007129 }
7130 }
Bram Moolenaardedd1b02013-12-14 13:06:17 +01007131 curwin = save_curwin;
7132 curbuf = save_curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007133 if (curwin->w_p_scb)
7134 {
7135 did_syncbind = TRUE;
7136 checkpcmark();
7137 if (old_linenr != curwin->w_cursor.lnum)
7138 {
7139 char_u ctrl_o[2];
7140
7141 ctrl_o[0] = Ctrl_O;
7142 ctrl_o[1] = 0;
7143 ins_typebuf(ctrl_o, REMAP_NONE, 0, TRUE, FALSE);
7144 }
7145 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007146}
7147
7148
7149 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007150ex_read(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007151{
Bram Moolenaardf177f62005-02-22 08:39:57 +00007152 int i;
7153 int empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
7154 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007155
7156 if (eap->usefilter) /* :r!cmd */
7157 do_bang(1, eap, FALSE, FALSE, TRUE);
7158 else
7159 {
7160 if (u_save(eap->line2, (linenr_T)(eap->line2 + 1)) == FAIL)
7161 return;
7162
7163#ifdef FEAT_BROWSE
7164 if (cmdmod.browse)
7165 {
7166 char_u *browseFile;
7167
Bram Moolenaar7171abe2004-10-11 10:06:20 +00007168 browseFile = do_browse(0, (char_u *)_("Append File"), eap->arg,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007169 NULL, NULL, NULL, curbuf);
7170 if (browseFile != NULL)
7171 {
7172 i = readfile(browseFile, NULL,
7173 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
7174 vim_free(browseFile);
7175 }
7176 else
7177 i = OK;
7178 }
7179 else
7180#endif
7181 if (*eap->arg == NUL)
7182 {
7183 if (check_fname() == FAIL) /* check for no file name */
7184 return;
7185 i = readfile(curbuf->b_ffname, curbuf->b_fname,
7186 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
7187 }
7188 else
7189 {
7190 if (vim_strchr(p_cpo, CPO_ALTREAD) != NULL)
7191 (void)setaltfname(eap->arg, eap->arg, (linenr_T)1);
7192 i = readfile(eap->arg, NULL,
7193 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
7194
7195 }
Bram Moolenaare13b9af2017-01-13 22:01:02 +01007196 if (i != OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007197 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007198#if defined(FEAT_EVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007199 if (!aborting())
7200#endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007201 semsg(_(e_notopen), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007202 }
7203 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00007204 {
7205 if (empty && exmode_active)
7206 {
7207 /* Delete the empty line that remains. Historically ex does
7208 * this but vi doesn't. */
7209 if (eap->line2 == 0)
7210 lnum = curbuf->b_ml.ml_line_count;
7211 else
7212 lnum = 1;
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00007213 if (*ml_get(lnum) == NUL && u_savedel(lnum, 1L) == OK)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007214 {
7215 ml_delete(lnum, FALSE);
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00007216 if (curwin->w_cursor.lnum > 1
7217 && curwin->w_cursor.lnum >= lnum)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007218 --curwin->w_cursor.lnum;
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00007219 deleted_lines_mark(lnum, 1L);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007220 }
7221 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007222 redraw_curbuf_later(VALID);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007223 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007224 }
7225}
7226
Bram Moolenaarea408852005-06-25 22:49:46 +00007227static char_u *prev_dir = NULL;
7228
7229#if defined(EXITFREE) || defined(PROTO)
7230 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007231free_cd_dir(void)
Bram Moolenaarea408852005-06-25 22:49:46 +00007232{
Bram Moolenaard23a8232018-02-10 18:45:26 +01007233 VIM_CLEAR(prev_dir);
7234 VIM_CLEAR(globaldir);
Bram Moolenaarea408852005-06-25 22:49:46 +00007235}
7236#endif
7237
Bram Moolenaarf4258302013-06-02 18:20:17 +02007238/*
7239 * Deal with the side effects of changing the current directory.
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007240 * When 'scope' is CDSCOPE_TABPAGE then this was after an ":tcd" command.
7241 * When 'scope' is CDSCOPE_WINDOW then this was after an ":lcd" command.
Bram Moolenaarf4258302013-06-02 18:20:17 +02007242 */
7243 void
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007244post_chdir(cdscope_T scope)
Bram Moolenaarf4258302013-06-02 18:20:17 +02007245{
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007246 if (scope != CDSCOPE_WINDOW)
Bram Moolenaar00aa0692019-04-27 20:37:57 +02007247 // Clear tab local directory for both :cd and :tcd
7248 VIM_CLEAR(curtab->tp_localdir);
Bram Moolenaard23a8232018-02-10 18:45:26 +01007249 VIM_CLEAR(curwin->w_localdir);
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007250 if (scope != CDSCOPE_GLOBAL)
Bram Moolenaarf4258302013-06-02 18:20:17 +02007251 {
7252 /* If still in global directory, need to remember current
7253 * directory as global directory. */
7254 if (globaldir == NULL && prev_dir != NULL)
7255 globaldir = vim_strsave(prev_dir);
7256 /* Remember this local directory for the window. */
7257 if (mch_dirname(NameBuff, MAXPATHL) == OK)
Bram Moolenaar00aa0692019-04-27 20:37:57 +02007258 {
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007259 if (scope == CDSCOPE_TABPAGE)
Bram Moolenaar00aa0692019-04-27 20:37:57 +02007260 curtab->tp_localdir = vim_strsave(NameBuff);
7261 else
7262 curwin->w_localdir = vim_strsave(NameBuff);
7263 }
Bram Moolenaarf4258302013-06-02 18:20:17 +02007264 }
7265 else
7266 {
7267 /* We are now in the global directory, no need to remember its
7268 * name. */
Bram Moolenaard23a8232018-02-10 18:45:26 +01007269 VIM_CLEAR(globaldir);
Bram Moolenaarf4258302013-06-02 18:20:17 +02007270 }
7271
7272 shorten_fnames(TRUE);
7273}
7274
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007275/*
7276 * Change directory function used by :cd/:tcd/:lcd Ex commands and the
7277 * chdir() function. If 'winlocaldir' is TRUE, then changes the window-local
7278 * directory. If 'tablocaldir' is TRUE, then changes the tab-local directory.
7279 * Otherwise changes the global directory.
7280 * Returns TRUE if the directory is successfully changed.
7281 */
7282 int
7283changedir_func(
7284 char_u *new_dir,
7285 int forceit,
7286 cdscope_T scope)
7287{
7288 char_u *tofree;
7289 int dir_differs;
7290 int retval = FALSE;
7291
7292 if (allbuf_locked())
7293 return FALSE;
7294
7295 if (vim_strchr(p_cpo, CPO_CHDIR) != NULL && curbufIsChanged() && !forceit)
7296 {
7297 emsg(_("E747: Cannot change directory, buffer is modified (add ! to override)"));
7298 return FALSE;
7299 }
7300
7301 // ":cd -": Change to previous directory
7302 if (STRCMP(new_dir, "-") == 0)
7303 {
7304 if (prev_dir == NULL)
7305 {
7306 emsg(_("E186: No previous directory"));
7307 return FALSE;
7308 }
7309 new_dir = prev_dir;
7310 }
7311
7312 // Save current directory for next ":cd -"
7313 tofree = prev_dir;
7314 if (mch_dirname(NameBuff, MAXPATHL) == OK)
7315 prev_dir = vim_strsave(NameBuff);
7316 else
7317 prev_dir = NULL;
7318
7319#if defined(UNIX) || defined(VMS)
7320 // for UNIX ":cd" means: go to home directory
7321 if (*new_dir == NUL)
7322 {
7323 // use NameBuff for home directory name
7324# ifdef VMS
7325 char_u *p;
7326
7327 p = mch_getenv((char_u *)"SYS$LOGIN");
7328 if (p == NULL || *p == NUL) // empty is the same as not set
7329 NameBuff[0] = NUL;
7330 else
7331 vim_strncpy(NameBuff, p, MAXPATHL - 1);
7332# else
7333 expand_env((char_u *)"$HOME", NameBuff, MAXPATHL);
7334# endif
7335 new_dir = NameBuff;
7336 }
7337#endif
7338 dir_differs = new_dir == NULL || prev_dir == NULL
7339 || pathcmp((char *)prev_dir, (char *)new_dir, -1) != 0;
7340 if (new_dir == NULL || (dir_differs && vim_chdir(new_dir)))
7341 emsg(_(e_failed));
7342 else
7343 {
7344 char_u *acmd_fname;
7345
7346 post_chdir(scope);
7347
7348 if (dir_differs)
7349 {
7350 if (scope == CDSCOPE_WINDOW)
7351 acmd_fname = (char_u *)"window";
7352 else if (scope == CDSCOPE_TABPAGE)
7353 acmd_fname = (char_u *)"tabpage";
7354 else
7355 acmd_fname = (char_u *)"global";
7356 apply_autocmds(EVENT_DIRCHANGED, acmd_fname, new_dir, FALSE,
7357 curbuf);
7358 }
7359 retval = TRUE;
7360 }
7361 vim_free(tofree);
7362
7363 return retval;
7364}
Bram Moolenaarea408852005-06-25 22:49:46 +00007365
Bram Moolenaar071d4272004-06-13 20:20:40 +00007366/*
Bram Moolenaar00aa0692019-04-27 20:37:57 +02007367 * ":cd", ":tcd", ":lcd", ":chdir" ":tchdir" and ":lchdir".
Bram Moolenaar071d4272004-06-13 20:20:40 +00007368 */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00007369 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007370ex_cd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007371{
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01007372 char_u *new_dir;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007373
7374 new_dir = eap->arg;
7375#if !defined(UNIX) && !defined(VMS)
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007376 // for non-UNIX ":cd" means: print current directory
Bram Moolenaar071d4272004-06-13 20:20:40 +00007377 if (*new_dir == NUL)
7378 ex_pwd(NULL);
7379 else
7380#endif
7381 {
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007382 cdscope_T scope = CDSCOPE_GLOBAL;
7383
7384 if (eap->cmdidx == CMD_lcd || eap->cmdidx == CMD_lchdir)
7385 scope = CDSCOPE_WINDOW;
7386 else if (eap->cmdidx == CMD_tcd || eap->cmdidx == CMD_tchdir)
7387 scope = CDSCOPE_TABPAGE;
7388
7389 if (changedir_func(new_dir, eap->forceit, scope))
Bram Moolenaardf177f62005-02-22 08:39:57 +00007390 {
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007391 // Echo the new current directory if the command was typed.
Bram Moolenaarfcfbc672009-07-09 18:13:49 +00007392 if (KeyTyped || p_verbose >= 5)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007393 ex_pwd(eap);
7394 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007395 }
7396}
7397
7398/*
7399 * ":pwd".
7400 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007401 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007402ex_pwd(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007403{
7404 if (mch_dirname(NameBuff, MAXPATHL) == OK)
7405 {
7406#ifdef BACKSLASH_IN_FILENAME
7407 slash_adjust(NameBuff);
7408#endif
Bram Moolenaar32526b32019-01-19 17:43:09 +01007409 msg((char *)NameBuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007410 }
7411 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007412 emsg(_("E187: Unknown"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007413}
7414
7415/*
7416 * ":=".
7417 */
7418 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007419ex_equal(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007420{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007421 smsg("%ld", (long)eap->line2);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007422 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007423}
7424
7425 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007426ex_sleep(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007427{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007428 int n;
7429 long len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007430
7431 if (cursor_valid())
7432 {
7433 n = W_WINROW(curwin) + curwin->w_wrow - msg_scrolled;
7434 if (n >= 0)
Bram Moolenaar53f81742017-09-22 14:35:51 +02007435 windgoto((int)n, curwin->w_wincol + curwin->w_wcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007436 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007437
7438 len = eap->line2;
7439 switch (*eap->arg)
7440 {
7441 case 'm': break;
7442 case NUL: len *= 1000L; break;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007443 default: semsg(_(e_invarg2), eap->arg); return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007444 }
7445 do_sleep(len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007446}
7447
7448/*
7449 * Sleep for "msec" milliseconds, but keep checking for a CTRL-C every second.
7450 */
7451 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007452do_sleep(long msec)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007453{
Bram Moolenaar52953192019-08-16 10:27:13 +02007454 long done = 0;
Bram Moolenaar975b5272016-03-15 23:10:59 +01007455 long wait_now;
Bram Moolenaar52953192019-08-16 10:27:13 +02007456# ifdef ELAPSED_FUNC
7457 elapsed_T start_tv;
7458
7459 // Remember at what time we started, so that we know how much longer we
7460 // should wait after waiting for a bit.
7461 ELAPSED_INIT(start_tv);
7462# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007463
7464 cursor_on();
Bram Moolenaarf45d7472018-09-30 18:22:26 +02007465 out_flush_cursor(FALSE, FALSE);
Bram Moolenaar52953192019-08-16 10:27:13 +02007466 while (!got_int && done < msec)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007467 {
Bram Moolenaar975b5272016-03-15 23:10:59 +01007468 wait_now = msec - done > 1000L ? 1000L : msec - done;
7469#ifdef FEAT_TIMERS
7470 {
7471 long due_time = check_due_timer();
7472
7473 if (due_time > 0 && due_time < wait_now)
7474 wait_now = due_time;
7475 }
7476#endif
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02007477#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar28e67e02019-08-15 23:05:49 +02007478 if (has_any_channel() && wait_now > 20L)
7479 wait_now = 20L;
7480#endif
7481#ifdef FEAT_SOUND
7482 if (has_any_sound_callback() && wait_now > 20L)
7483 wait_now = 20L;
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02007484#endif
Bram Moolenaar975b5272016-03-15 23:10:59 +01007485 ui_delay(wait_now, TRUE);
Bram Moolenaar52953192019-08-16 10:27:13 +02007486
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02007487#ifdef FEAT_JOB_CHANNEL
7488 if (has_any_channel())
7489 ui_breakcheck_force(TRUE);
7490 else
7491#endif
7492 ui_breakcheck();
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007493#ifdef MESSAGE_QUEUE
Bram Moolenaar52953192019-08-16 10:27:13 +02007494 // Process the netbeans and clientserver messages that may have been
7495 // received in the call to ui_breakcheck() when the GUI is in use. This
7496 // may occur when running a test case.
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007497 parse_queued_messages();
Bram Moolenaare3cc6d42011-10-20 21:58:34 +02007498#endif
Bram Moolenaar52953192019-08-16 10:27:13 +02007499
7500# ifdef ELAPSED_FUNC
7501 // actual time passed
7502 done = ELAPSED_FUNC(start_tv);
7503# else
7504 // guestimate time passed (will actually be more)
7505 done += wait_now;
7506# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007507 }
Bram Moolenaar1ebff3d2018-07-07 16:18:13 +02007508
7509 // If CTRL-C was typed to interrupt the sleep, drop the CTRL-C from the
7510 // input buffer, otherwise a following call to input() fails.
7511 if (got_int)
7512 (void)vpeekc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007513}
7514
Bram Moolenaar071d4272004-06-13 20:20:40 +00007515/*
7516 * ":winsize" command (obsolete).
7517 */
7518 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007519ex_winsize(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007520{
7521 int w, h;
7522 char_u *arg = eap->arg;
7523 char_u *p;
7524
7525 w = getdigits(&arg);
7526 arg = skipwhite(arg);
7527 p = arg;
7528 h = getdigits(&arg);
7529 if (*p != NUL && *arg == NUL)
7530 set_shellsize(w, h, TRUE);
7531 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007532 emsg(_("E465: :winsize requires two number arguments"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007533}
7534
Bram Moolenaar071d4272004-06-13 20:20:40 +00007535 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007536ex_wincmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007537{
7538 int xchar = NUL;
7539 char_u *p;
7540
7541 if (*eap->arg == 'g' || *eap->arg == Ctrl_G)
7542 {
7543 /* CTRL-W g and CTRL-W CTRL-G have an extra command character */
7544 if (eap->arg[1] == NUL)
7545 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007546 emsg(_(e_invarg));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007547 return;
7548 }
7549 xchar = eap->arg[1];
7550 p = eap->arg + 2;
7551 }
7552 else
7553 p = eap->arg + 1;
7554
7555 eap->nextcmd = check_nextcmd(p);
7556 p = skipwhite(p);
7557 if (*p != NUL && *p != '"' && eap->nextcmd == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007558 emsg(_(e_invarg));
Bram Moolenaar12bde492011-06-13 01:19:56 +02007559 else if (!eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007560 {
7561 /* Pass flags on for ":vertical wincmd ]". */
7562 postponed_split_flags = cmdmod.split;
Bram Moolenaard326ce82007-03-11 14:48:29 +00007563 postponed_split_tab = cmdmod.tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007564 do_window(*eap->arg, eap->addr_count > 0 ? eap->line2 : 0L, xchar);
7565 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00007566 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007567 }
7568}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007569
Bram Moolenaar843ee412004-06-30 16:16:41 +00007570#if defined(FEAT_GUI) || defined(UNIX) || defined(VMS) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007571/*
7572 * ":winpos".
7573 */
7574 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007575ex_winpos(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007576{
7577 int x, y;
7578 char_u *arg = eap->arg;
7579 char_u *p;
7580
7581 if (*arg == NUL)
7582 {
Bram Moolenaar843ee412004-06-30 16:16:41 +00007583# if defined(FEAT_GUI) || defined(MSWIN)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007584# ifdef VIMDLL
7585 if (gui.in_use ? gui_mch_get_winpos(&x, &y) != FAIL :
7586 mch_get_winpos(&x, &y) != FAIL)
7587# elif defined(FEAT_GUI)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007588 if (gui.in_use && gui_mch_get_winpos(&x, &y) != FAIL)
Bram Moolenaar843ee412004-06-30 16:16:41 +00007589# else
7590 if (mch_get_winpos(&x, &y) != FAIL)
7591# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007592 {
7593 sprintf((char *)IObuff, _("Window position: X %d, Y %d"), x, y);
Bram Moolenaar32526b32019-01-19 17:43:09 +01007594 msg((char *)IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007595 }
7596 else
7597# endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007598 emsg(_("E188: Obtaining window position not implemented for this platform"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007599 }
7600 else
7601 {
7602 x = getdigits(&arg);
7603 arg = skipwhite(arg);
7604 p = arg;
7605 y = getdigits(&arg);
7606 if (*p == NUL || *arg != NUL)
7607 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007608 emsg(_("E466: :winpos requires two number arguments"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007609 return;
7610 }
7611# ifdef FEAT_GUI
7612 if (gui.in_use)
7613 gui_mch_set_winpos(x, y);
7614 else if (gui.starting)
7615 {
7616 /* Remember the coordinates for when the window is opened. */
7617 gui_win_x = x;
7618 gui_win_y = y;
7619 }
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007620# if defined(HAVE_TGETENT) || defined(VIMDLL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007621 else
7622# endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007623# endif
7624# if defined(MSWIN) && (!defined(FEAT_GUI) || defined(VIMDLL))
Bram Moolenaar843ee412004-06-30 16:16:41 +00007625 mch_set_winpos(x, y);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007626# endif
7627# ifdef HAVE_TGETENT
7628 if (*T_CWP)
7629 term_set_winpos(x, y);
7630# endif
7631 }
7632}
7633#endif
7634
7635/*
7636 * Handle command that work like operators: ":delete", ":yank", ":>" and ":<".
7637 */
7638 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007639ex_operators(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007640{
7641 oparg_T oa;
7642
7643 clear_oparg(&oa);
7644 oa.regname = eap->regname;
7645 oa.start.lnum = eap->line1;
7646 oa.end.lnum = eap->line2;
7647 oa.line_count = eap->line2 - eap->line1 + 1;
7648 oa.motion_type = MLINE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007649 virtual_op = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007650 if (eap->cmdidx != CMD_yank) /* position cursor for undo */
7651 {
7652 setpcmark();
7653 curwin->w_cursor.lnum = eap->line1;
7654 beginline(BL_SOL | BL_FIX);
7655 }
7656
Bram Moolenaard07c6e12013-11-21 14:21:40 +01007657 if (VIsual_active)
7658 end_visual_mode();
Bram Moolenaard07c6e12013-11-21 14:21:40 +01007659
Bram Moolenaar071d4272004-06-13 20:20:40 +00007660 switch (eap->cmdidx)
7661 {
7662 case CMD_delete:
7663 oa.op_type = OP_DELETE;
7664 op_delete(&oa);
7665 break;
7666
7667 case CMD_yank:
7668 oa.op_type = OP_YANK;
7669 (void)op_yank(&oa, FALSE, TRUE);
7670 break;
7671
7672 default: /* CMD_rshift or CMD_lshift */
Bram Moolenaar6e707362013-06-14 19:15:58 +02007673 if (
Bram Moolenaar071d4272004-06-13 20:20:40 +00007674#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6e707362013-06-14 19:15:58 +02007675 (eap->cmdidx == CMD_rshift) ^ curwin->w_p_rl
7676#else
7677 eap->cmdidx == CMD_rshift
Bram Moolenaar071d4272004-06-13 20:20:40 +00007678#endif
Bram Moolenaar6e707362013-06-14 19:15:58 +02007679 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00007680 oa.op_type = OP_RSHIFT;
7681 else
7682 oa.op_type = OP_LSHIFT;
7683 op_shift(&oa, FALSE, eap->amount);
7684 break;
7685 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007686 virtual_op = MAYBE;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007687 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007688}
7689
7690/*
7691 * ":put".
7692 */
7693 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007694ex_put(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007695{
7696 /* ":0put" works like ":1put!". */
7697 if (eap->line2 == 0)
7698 {
7699 eap->line2 = 1;
7700 eap->forceit = TRUE;
7701 }
7702 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007703 do_put(eap->regname, eap->forceit ? BACKWARD : FORWARD, 1L,
7704 PUT_LINE|PUT_CURSLINE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007705}
7706
7707/*
7708 * Handle ":copy" and ":move".
7709 */
7710 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007711ex_copymove(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007712{
7713 long n;
7714
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02007715 n = get_address(eap, &eap->arg, eap->addr_type, FALSE, FALSE, FALSE, 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007716 if (eap->arg == NULL) /* error detected */
7717 {
7718 eap->nextcmd = NULL;
7719 return;
7720 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00007721 get_flags(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007722
7723 /*
7724 * move or copy lines from 'eap->line1'-'eap->line2' to below line 'n'
7725 */
7726 if (n == MAXLNUM || n < 0 || n > curbuf->b_ml.ml_line_count)
7727 {
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02007728 emsg(_(e_invrange));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007729 return;
7730 }
7731
7732 if (eap->cmdidx == CMD_move)
7733 {
7734 if (do_move(eap->line1, eap->line2, n) == FAIL)
7735 return;
7736 }
7737 else
7738 ex_copy(eap->line1, eap->line2, n);
7739 u_clearline();
7740 beginline(BL_SOL | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007741 ex_may_print(eap);
7742}
7743
7744/*
7745 * Print the current line if flags were given to the Ex command.
7746 */
Bram Moolenaarfd3fe982014-04-01 17:49:44 +02007747 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007748ex_may_print(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007749{
7750 if (eap->flags != 0)
7751 {
7752 print_line(curwin->w_cursor.lnum, (eap->flags & EXFLAG_NR),
7753 (eap->flags & EXFLAG_LIST));
7754 ex_no_reprint = TRUE;
7755 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007756}
7757
7758/*
7759 * ":smagic" and ":snomagic".
7760 */
7761 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007762ex_submagic(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007763{
7764 int magic_save = p_magic;
7765
7766 p_magic = (eap->cmdidx == CMD_smagic);
7767 do_sub(eap);
7768 p_magic = magic_save;
7769}
7770
7771/*
7772 * ":join".
7773 */
7774 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007775ex_join(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007776{
7777 curwin->w_cursor.lnum = eap->line1;
7778 if (eap->line1 == eap->line2)
7779 {
7780 if (eap->addr_count >= 2) /* :2,2join does nothing */
7781 return;
7782 if (eap->line2 == curbuf->b_ml.ml_line_count)
7783 {
7784 beep_flush();
7785 return;
7786 }
7787 ++eap->line2;
7788 }
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02007789 (void)do_join(eap->line2 - eap->line1 + 1, !eap->forceit, TRUE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007790 beginline(BL_WHITE | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007791 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007792}
7793
7794/*
7795 * ":[addr]@r" or ":[addr]*r": execute register
7796 */
7797 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007798ex_at(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007799{
7800 int c;
Bram Moolenaar60462872009-11-03 11:40:19 +00007801 int prev_len = typebuf.tb_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007802
7803 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaar4930a762016-09-11 14:39:53 +02007804 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007805
7806#ifdef USE_ON_FLY_SCROLL
7807 dont_scroll = TRUE; /* disallow scrolling here */
7808#endif
7809
7810 /* get the register name. No name means to use the previous one */
7811 c = *eap->arg;
7812 if (c == NUL || (c == '*' && *eap->cmd == '*'))
7813 c = '@';
Bram Moolenaard333d1e2006-11-07 17:43:47 +00007814 /* Put the register in the typeahead buffer with the "silent" flag. */
7815 if (do_execreg(c, TRUE, vim_strchr(p_cpo, CPO_EXECBUF) != NULL, TRUE)
7816 == FAIL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007817 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007818 beep_flush();
Bram Moolenaardf177f62005-02-22 08:39:57 +00007819 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007820 else
7821 {
7822 int save_efr = exec_from_reg;
7823
7824 exec_from_reg = TRUE;
7825
7826 /*
7827 * Execute from the typeahead buffer.
Bram Moolenaar60462872009-11-03 11:40:19 +00007828 * Continue until the stuff buffer is empty and all added characters
7829 * have been consumed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007830 */
Bram Moolenaar60462872009-11-03 11:40:19 +00007831 while (!stuff_empty() || typebuf.tb_len > prev_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007832 (void)do_cmdline(NULL, getexline, NULL, DOCMD_NOWAIT|DOCMD_VERBOSE);
7833
7834 exec_from_reg = save_efr;
7835 }
7836}
7837
7838/*
7839 * ":!".
7840 */
7841 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007842ex_bang(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007843{
7844 do_bang(eap->addr_count, eap, eap->forceit, TRUE, TRUE);
7845}
7846
7847/*
7848 * ":undo".
7849 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007850 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +01007851ex_undo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007852{
Bram Moolenaard3667a22006-03-16 21:35:52 +00007853 if (eap->addr_count == 1) /* :undo 123 */
Bram Moolenaar730cde92010-06-27 05:18:54 +02007854 undo_time(eap->line2, FALSE, FALSE, TRUE);
Bram Moolenaard3667a22006-03-16 21:35:52 +00007855 else
7856 u_undo(1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007857}
7858
Bram Moolenaar55debbe2010-05-23 23:34:36 +02007859#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar6df6f472010-07-18 18:04:50 +02007860 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007861ex_wundo(exarg_T *eap)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02007862{
7863 char_u hash[UNDO_HASH_SIZE];
7864
7865 u_compute_hash(hash);
7866 u_write_undo(eap->arg, eap->forceit, curbuf, hash);
7867}
7868
Bram Moolenaar6df6f472010-07-18 18:04:50 +02007869 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007870ex_rundo(exarg_T *eap)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02007871{
7872 char_u hash[UNDO_HASH_SIZE];
7873
7874 u_compute_hash(hash);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02007875 u_read_undo(eap->arg, hash, NULL);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02007876}
7877#endif
7878
Bram Moolenaar071d4272004-06-13 20:20:40 +00007879/*
7880 * ":redo".
7881 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007882 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007883ex_redo(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007884{
7885 u_redo(1);
7886}
7887
7888/*
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007889 * ":earlier" and ":later".
7890 */
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007891 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007892ex_later(exarg_T *eap)
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007893{
7894 long count = 0;
7895 int sec = FALSE;
Bram Moolenaar730cde92010-06-27 05:18:54 +02007896 int file = FALSE;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007897 char_u *p = eap->arg;
7898
7899 if (*p == NUL)
7900 count = 1;
7901 else if (isdigit(*p))
7902 {
7903 count = getdigits(&p);
7904 switch (*p)
7905 {
7906 case 's': ++p; sec = TRUE; break;
7907 case 'm': ++p; sec = TRUE; count *= 60; break;
7908 case 'h': ++p; sec = TRUE; count *= 60 * 60; break;
Bram Moolenaar730cde92010-06-27 05:18:54 +02007909 case 'd': ++p; sec = TRUE; count *= 24 * 60 * 60; break;
7910 case 'f': ++p; file = TRUE; break;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007911 }
7912 }
7913
7914 if (*p != NUL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007915 semsg(_(e_invarg2), eap->arg);
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007916 else
Bram Moolenaar730cde92010-06-27 05:18:54 +02007917 undo_time(eap->cmdidx == CMD_earlier ? -count : count,
7918 sec, file, FALSE);
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007919}
7920
7921/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007922 * ":redir": start/stop redirection.
7923 */
7924 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007925ex_redir(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007926{
7927 char *mode;
7928 char_u *fname;
Bram Moolenaarca472992005-01-21 11:46:23 +00007929 char_u *arg = eap->arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007930
Bram Moolenaarba768492016-07-08 15:32:54 +02007931#ifdef FEAT_EVAL
Bram Moolenaar79815f12016-07-09 17:07:29 +02007932 if (redir_execute)
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02007933 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007934 emsg(_("E930: Cannot use :redir inside execute()"));
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02007935 return;
7936 }
Bram Moolenaarba768492016-07-08 15:32:54 +02007937#endif
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02007938
Bram Moolenaar071d4272004-06-13 20:20:40 +00007939 if (STRICMP(eap->arg, "END") == 0)
7940 close_redir();
7941 else
7942 {
Bram Moolenaarca472992005-01-21 11:46:23 +00007943 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00007944 {
Bram Moolenaarca472992005-01-21 11:46:23 +00007945 ++arg;
7946 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00007947 {
Bram Moolenaarca472992005-01-21 11:46:23 +00007948 ++arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007949 mode = "a";
7950 }
7951 else
7952 mode = "w";
Bram Moolenaarca472992005-01-21 11:46:23 +00007953 arg = skipwhite(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007954
7955 close_redir();
7956
7957 /* Expand environment variables and "~/". */
Bram Moolenaarca472992005-01-21 11:46:23 +00007958 fname = expand_env_save(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007959 if (fname == NULL)
7960 return;
7961#ifdef FEAT_BROWSE
7962 if (cmdmod.browse)
7963 {
7964 char_u *browseFile;
7965
Bram Moolenaar7171abe2004-10-11 10:06:20 +00007966 browseFile = do_browse(BROWSE_SAVE,
7967 (char_u *)_("Save Redirection"),
Bram Moolenaarc36651b2018-04-29 12:22:56 +02007968 fname, NULL, NULL,
7969 (char_u *)_(BROWSE_FILTER_ALL_FILES), curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007970 if (browseFile == NULL)
7971 return; /* operation cancelled */
7972 vim_free(fname);
7973 fname = browseFile;
7974 eap->forceit = TRUE; /* since dialog already asked */
7975 }
7976#endif
7977
7978 redir_fd = open_exfile(fname, eap->forceit, mode);
7979 vim_free(fname);
7980 }
7981#ifdef FEAT_EVAL
Bram Moolenaarca472992005-01-21 11:46:23 +00007982 else if (*arg == '@')
Bram Moolenaar071d4272004-06-13 20:20:40 +00007983 {
7984 /* redirect to a register a-z (resp. A-Z for appending) */
7985 close_redir();
Bram Moolenaarca472992005-01-21 11:46:23 +00007986 ++arg;
7987 if (ASCII_ISALPHA(*arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007988# ifdef FEAT_CLIPBOARD
Bram Moolenaarca472992005-01-21 11:46:23 +00007989 || *arg == '*'
Bram Moolenaar9a51c6e2006-11-14 19:25:02 +00007990 || *arg == '+'
Bram Moolenaar071d4272004-06-13 20:20:40 +00007991# endif
Bram Moolenaarca472992005-01-21 11:46:23 +00007992 || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00007993 {
Bram Moolenaarca472992005-01-21 11:46:23 +00007994 redir_reg = *arg++;
Bram Moolenaarc188b882007-10-19 14:20:54 +00007995 if (*arg == '>' && arg[1] == '>') /* append */
Bram Moolenaard9d30582005-05-18 22:10:28 +00007996 arg += 2;
Bram Moolenaarc188b882007-10-19 14:20:54 +00007997 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007998 {
Bram Moolenaarc188b882007-10-19 14:20:54 +00007999 /* Can use both "@a" and "@a>". */
Bram Moolenaar2c29bee2005-06-01 21:46:07 +00008000 if (*arg == '>')
8001 arg++;
Bram Moolenaarc188b882007-10-19 14:20:54 +00008002 /* Make register empty when not using @A-@Z and the
8003 * command is valid. */
8004 if (*arg == NUL && !isupper(redir_reg))
8005 write_reg_contents(redir_reg, (char_u *)"", -1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008006 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00008007 }
8008 if (*arg != NUL)
8009 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00008010 redir_reg = 0;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008011 semsg(_(e_invarg2), eap->arg);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008012 }
8013 }
8014 else if (*arg == '=' && arg[1] == '>')
8015 {
8016 int append;
8017
8018 /* redirect to a variable */
8019 close_redir();
8020 arg += 2;
8021
8022 if (*arg == '>')
8023 {
8024 ++arg;
8025 append = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008026 }
8027 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00008028 append = FALSE;
8029
8030 if (var_redir_start(skipwhite(arg), append) == OK)
8031 redir_vname = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008032 }
8033#endif
8034
8035 /* TODO: redirect to a buffer */
8036
Bram Moolenaar071d4272004-06-13 20:20:40 +00008037 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008038 semsg(_(e_invarg2), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008039 }
Bram Moolenaar29b2d262006-09-10 19:07:28 +00008040
8041 /* Make sure redirection is not off. Can happen for cmdline completion
8042 * that indirectly invokes a command to catch its output. */
8043 if (redir_fd != NULL
8044#ifdef FEAT_EVAL
8045 || redir_reg || redir_vname
8046#endif
8047 )
8048 redir_off = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008049}
8050
8051/*
8052 * ":redraw": force redraw
8053 */
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01008054 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008055ex_redraw(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008056{
8057 int r = RedrawingDisabled;
8058 int p = p_lz;
8059
8060 RedrawingDisabled = 0;
8061 p_lz = FALSE;
Bram Moolenaarabc39ab2017-03-01 18:04:05 +01008062 validate_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008063 update_topline();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01008064 update_screen(eap->forceit ? CLEAR : VIsual_active ? INVERTED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008065#ifdef FEAT_TITLE
8066 if (need_maketitle)
8067 maketitle();
8068#endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02008069#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
8070# ifdef VIMDLL
8071 if (!gui.in_use)
8072# endif
8073 resize_console_buf();
Bram Moolenaar78d21da2019-02-17 15:00:52 +01008074#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008075 RedrawingDisabled = r;
8076 p_lz = p;
8077
8078 /* Reset msg_didout, so that a message that's there is overwritten. */
8079 msg_didout = FALSE;
8080 msg_col = 0;
8081
Bram Moolenaar56667a52013-07-17 11:54:28 +02008082 /* No need to wait after an intentional redraw. */
8083 need_wait_return = FALSE;
8084
Bram Moolenaar071d4272004-06-13 20:20:40 +00008085 out_flush();
8086}
8087
8088/*
8089 * ":redrawstatus": force redraw of status line(s)
8090 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008091 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008092ex_redrawstatus(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008093{
Bram Moolenaar071d4272004-06-13 20:20:40 +00008094 int r = RedrawingDisabled;
8095 int p = p_lz;
8096
8097 RedrawingDisabled = 0;
8098 p_lz = FALSE;
8099 if (eap->forceit)
8100 status_redraw_all();
8101 else
8102 status_redraw_curbuf();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01008103 update_screen(VIsual_active ? INVERTED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008104 RedrawingDisabled = r;
8105 p_lz = p;
8106 out_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008107}
8108
Bram Moolenaare12bab32019-01-08 22:02:56 +01008109/*
8110 * ":redrawtabline": force redraw of the tabline
8111 */
8112 static void
8113ex_redrawtabline(exarg_T *eap UNUSED)
8114{
8115 int r = RedrawingDisabled;
8116 int p = p_lz;
8117
8118 RedrawingDisabled = 0;
8119 p_lz = FALSE;
8120
8121 draw_tabline();
8122
8123 RedrawingDisabled = r;
8124 p_lz = p;
8125 out_flush();
8126}
8127
Bram Moolenaar071d4272004-06-13 20:20:40 +00008128 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008129close_redir(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008130{
8131 if (redir_fd != NULL)
8132 {
8133 fclose(redir_fd);
8134 redir_fd = NULL;
8135 }
8136#ifdef FEAT_EVAL
8137 redir_reg = 0;
Bram Moolenaardf177f62005-02-22 08:39:57 +00008138 if (redir_vname)
8139 {
8140 var_redir_stop();
8141 redir_vname = 0;
8142 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008143#endif
8144}
8145
Bram Moolenaarf96ae0b2019-07-28 15:21:55 +02008146#if (defined(FEAT_SESSION) || defined(FEAT_EVAL)) || defined(PROTO)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008147 int
Bram Moolenaarf1d25012016-03-03 12:22:53 +01008148vim_mkdir_emsg(char_u *name, int prot)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008149{
8150 if (vim_mkdir(name, prot) != 0)
8151 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008152 semsg(_("E739: Cannot create directory: %s"), name);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008153 return FAIL;
8154 }
8155 return OK;
8156}
8157#endif
8158
Bram Moolenaar071d4272004-06-13 20:20:40 +00008159/*
8160 * Open a file for writing for an Ex command, with some checks.
8161 * Return file descriptor, or NULL on failure.
8162 */
8163 FILE *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008164open_exfile(
8165 char_u *fname,
8166 int forceit,
8167 char *mode) /* "w" for create new file or "a" for append */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008168{
8169 FILE *fd;
8170
8171#ifdef UNIX
8172 /* with Unix it is possible to open a directory */
8173 if (mch_isdir(fname))
8174 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008175 semsg(_(e_isadir2), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008176 return NULL;
8177 }
8178#endif
8179 if (!forceit && *mode != 'a' && vim_fexists(fname))
8180 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008181 semsg(_("E189: \"%s\" exists (add ! to override)"), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008182 return NULL;
8183 }
8184
8185 if ((fd = mch_fopen((char *)fname, mode)) == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008186 semsg(_("E190: Cannot open \"%s\" for writing"), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008187
8188 return fd;
8189}
8190
8191/*
8192 * ":mark" and ":k".
8193 */
8194 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008195ex_mark(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008196{
8197 pos_T pos;
8198
8199 if (*eap->arg == NUL) /* No argument? */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008200 emsg(_(e_argreq));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008201 else if (eap->arg[1] != NUL) /* more than one character? */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008202 emsg(_(e_trailing));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008203 else
8204 {
8205 pos = curwin->w_cursor; /* save curwin->w_cursor */
8206 curwin->w_cursor.lnum = eap->line2;
8207 beginline(BL_WHITE | BL_FIX);
8208 if (setmark(*eap->arg) == FAIL) /* set mark */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008209 emsg(_("E191: Argument must be a letter or forward/backward quote"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008210 curwin->w_cursor = pos; /* restore curwin->w_cursor */
8211 }
8212}
8213
8214/*
8215 * Update w_topline, w_leftcol and the cursor position.
8216 */
8217 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008218update_topline_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008219{
8220 check_cursor(); /* put cursor on valid line */
8221 update_topline();
8222 if (!curwin->w_p_wrap)
8223 validate_cursor();
8224 update_curswant();
8225}
8226
Bram Moolenaar071d4272004-06-13 20:20:40 +00008227/*
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008228 * Save the current State and go to Normal mode.
8229 * Return TRUE if the typeahead could be saved.
8230 */
8231 int
8232save_current_state(save_state_T *sst)
8233{
8234 sst->save_msg_scroll = msg_scroll;
8235 sst->save_restart_edit = restart_edit;
8236 sst->save_msg_didout = msg_didout;
8237 sst->save_State = State;
8238 sst->save_insertmode = p_im;
8239 sst->save_finish_op = finish_op;
8240 sst->save_opcount = opcount;
Bram Moolenaarcce713d2019-03-04 11:40:12 +01008241 sst->save_reg_executing = reg_executing;
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008242
8243 msg_scroll = FALSE; /* no msg scrolling in Normal mode */
8244 restart_edit = 0; /* don't go to Insert mode */
8245 p_im = FALSE; /* don't use 'insertmode' */
8246
8247 /*
8248 * Save the current typeahead. This is required to allow using ":normal"
8249 * from an event handler and makes sure we don't hang when the argument
8250 * ends with half a command.
8251 */
8252 save_typeahead(&sst->tabuf);
8253 return sst->tabuf.typebuf_valid;
8254}
8255
8256 void
8257restore_current_state(save_state_T *sst)
8258{
8259 /* Restore the previous typeahead. */
8260 restore_typeahead(&sst->tabuf);
8261
8262 msg_scroll = sst->save_msg_scroll;
8263 restart_edit = sst->save_restart_edit;
8264 p_im = sst->save_insertmode;
8265 finish_op = sst->save_finish_op;
8266 opcount = sst->save_opcount;
Bram Moolenaarcce713d2019-03-04 11:40:12 +01008267 reg_executing = sst->save_reg_executing;
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008268 msg_didout |= sst->save_msg_didout; /* don't reset msg_didout now */
8269
8270 /* Restore the state (needed when called from a function executed for
8271 * 'indentexpr'). Update the mouse and cursor, they may have changed. */
8272 State = sst->save_State;
8273#ifdef CURSOR_SHAPE
8274 ui_cursor_shape(); /* may show different cursor shape */
8275#endif
8276}
8277
8278/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008279 * ":normal[!] {commands}": Execute normal mode commands.
8280 */
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01008281 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008282ex_normal(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008283{
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008284 save_state_T save_state;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008285 char_u *arg = NULL;
8286 int l;
8287 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008288
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008289 if (ex_normal_lock > 0)
8290 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008291 emsg(_(e_secure));
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008292 return;
8293 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008294 if (ex_normal_busy >= p_mmd)
8295 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008296 emsg(_("E192: Recursive use of :normal too deep"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008297 return;
8298 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008299
Bram Moolenaar071d4272004-06-13 20:20:40 +00008300 /*
8301 * vgetc() expects a CSI and K_SPECIAL to have been escaped. Don't do
8302 * this for the K_SPECIAL leading byte, otherwise special keys will not
8303 * work.
8304 */
8305 if (has_mbyte)
8306 {
8307 int len = 0;
8308
8309 /* Count the number of characters to be escaped. */
8310 for (p = eap->arg; *p != NUL; ++p)
8311 {
Bram Moolenaar13505972019-01-24 15:04:48 +01008312#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008313 if (*p == CSI) /* leadbyte CSI */
8314 len += 2;
Bram Moolenaar13505972019-01-24 15:04:48 +01008315#endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008316 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008317 if (*++p == K_SPECIAL /* trailbyte K_SPECIAL or CSI */
Bram Moolenaar13505972019-01-24 15:04:48 +01008318#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008319 || *p == CSI
Bram Moolenaar13505972019-01-24 15:04:48 +01008320#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008321 )
8322 len += 2;
8323 }
8324 if (len > 0)
8325 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02008326 arg = alloc(STRLEN(eap->arg) + len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008327 if (arg != NULL)
8328 {
8329 len = 0;
8330 for (p = eap->arg; *p != NUL; ++p)
8331 {
8332 arg[len++] = *p;
Bram Moolenaar13505972019-01-24 15:04:48 +01008333#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008334 if (*p == CSI)
8335 {
8336 arg[len++] = KS_EXTRA;
8337 arg[len++] = (int)KE_CSI;
8338 }
Bram Moolenaar13505972019-01-24 15:04:48 +01008339#endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008340 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008341 {
8342 arg[len++] = *++p;
8343 if (*p == K_SPECIAL)
8344 {
8345 arg[len++] = KS_SPECIAL;
8346 arg[len++] = KE_FILLER;
8347 }
Bram Moolenaar13505972019-01-24 15:04:48 +01008348#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008349 else if (*p == CSI)
8350 {
8351 arg[len++] = KS_EXTRA;
8352 arg[len++] = (int)KE_CSI;
8353 }
Bram Moolenaar13505972019-01-24 15:04:48 +01008354#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008355 }
8356 arg[len] = NUL;
8357 }
8358 }
8359 }
8360 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008361
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008362 ++ex_normal_busy;
8363 if (save_current_state(&save_state))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008364 {
8365 /*
8366 * Repeat the :normal command for each line in the range. When no
8367 * range given, execute it just once, without positioning the cursor
8368 * first.
8369 */
8370 do
8371 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008372 if (eap->addr_count != 0)
8373 {
8374 curwin->w_cursor.lnum = eap->line1++;
8375 curwin->w_cursor.col = 0;
Bram Moolenaard5d37532017-03-27 23:02:07 +02008376 check_cursor_moved(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008377 }
8378
Bram Moolenaar13505972019-01-24 15:04:48 +01008379 exec_normal_cmd(arg != NULL
8380 ? arg
8381 : eap->arg, eap->forceit ? REMAP_NONE : REMAP_YES, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008382 }
8383 while (eap->addr_count > 0 && eap->line1 <= eap->line2 && !got_int);
8384 }
8385
8386 /* Might not return to the main loop when in an event handler. */
8387 update_topline_cursor();
8388
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008389 restore_current_state(&save_state);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008390 --ex_normal_busy;
Bram Moolenaareda73602014-11-05 09:53:23 +01008391#ifdef FEAT_MOUSE
8392 setmouse();
8393#endif
8394#ifdef CURSOR_SHAPE
8395 ui_cursor_shape(); /* may show different cursor shape */
8396#endif
8397
Bram Moolenaar071d4272004-06-13 20:20:40 +00008398 vim_free(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008399}
8400
8401/*
Bram Moolenaar64969662005-12-14 21:59:55 +00008402 * ":startinsert", ":startreplace" and ":startgreplace"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008403 */
8404 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008405ex_startinsert(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008406{
Bram Moolenaarfd371682005-01-14 21:42:54 +00008407 if (eap->forceit)
8408 {
Bram Moolenaar09ca9322017-09-26 17:40:45 +02008409 /* cursor line can be zero on startup */
8410 if (!curwin->w_cursor.lnum)
8411 curwin->w_cursor.lnum = 1;
Bram Moolenaarfd371682005-01-14 21:42:54 +00008412 coladvance((colnr_T)MAXCOL);
8413 curwin->w_curswant = MAXCOL;
8414 curwin->w_set_curswant = FALSE;
8415 }
8416
Bram Moolenaara40c5002005-01-09 21:16:21 +00008417 /* Ignore the command when already in Insert mode. Inserting an
8418 * expression register that invokes a function can do this. */
8419 if (State & INSERT)
8420 return;
8421
Bram Moolenaar64969662005-12-14 21:59:55 +00008422 if (eap->cmdidx == CMD_startinsert)
8423 restart_edit = 'a';
8424 else if (eap->cmdidx == CMD_startreplace)
8425 restart_edit = 'R';
Bram Moolenaar071d4272004-06-13 20:20:40 +00008426 else
Bram Moolenaar64969662005-12-14 21:59:55 +00008427 restart_edit = 'V';
8428
8429 if (!eap->forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008430 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00008431 if (eap->cmdidx == CMD_startinsert)
8432 restart_edit = 'i';
Bram Moolenaar071d4272004-06-13 20:20:40 +00008433 curwin->w_curswant = 0; /* avoid MAXCOL */
8434 }
8435}
8436
8437/*
8438 * ":stopinsert"
8439 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008440 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008441ex_stopinsert(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008442{
8443 restart_edit = 0;
8444 stop_insert_mode = TRUE;
Bram Moolenaarfd773e92016-04-02 19:39:16 +02008445 clearmode();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008446}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008447
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008448/*
8449 * Execute normal mode command "cmd".
8450 * "remap" can be REMAP_NONE or REMAP_YES.
8451 */
8452 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008453exec_normal_cmd(char_u *cmd, int remap, int silent)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008454{
Bram Moolenaar25281632016-01-21 23:32:32 +01008455 /* Stuff the argument into the typeahead buffer. */
8456 ins_typebuf(cmd, remap, 0, TRUE, silent);
Bram Moolenaar586c70c2018-10-02 16:23:58 +02008457 exec_normal(FALSE, FALSE, FALSE);
Bram Moolenaar25281632016-01-21 23:32:32 +01008458}
Bram Moolenaar25281632016-01-21 23:32:32 +01008459
Bram Moolenaar25281632016-01-21 23:32:32 +01008460/*
8461 * Execute normal_cmd() until there is no typeahead left.
Bram Moolenaar586c70c2018-10-02 16:23:58 +02008462 * When "use_vpeekc" is TRUE use vpeekc() to check for available chars.
Bram Moolenaar25281632016-01-21 23:32:32 +01008463 */
8464 void
Bram Moolenaar586c70c2018-10-02 16:23:58 +02008465exec_normal(int was_typed, int use_vpeekc, int may_use_terminal_loop UNUSED)
Bram Moolenaar25281632016-01-21 23:32:32 +01008466{
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008467 oparg_T oa;
Bram Moolenaar905dd902019-04-07 14:21:47 +02008468 int c;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008469
Bram Moolenaar905dd902019-04-07 14:21:47 +02008470 // When calling vpeekc() from feedkeys() it will return Ctrl_C when there
8471 // is nothing to get, so also check for Ctrl_C.
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008472 clear_oparg(&oa);
8473 finish_op = FALSE;
Bram Moolenaar586c70c2018-10-02 16:23:58 +02008474 while ((!stuff_empty()
8475 || ((was_typed || !typebuf_typed()) && typebuf.tb_len > 0)
Bram Moolenaar905dd902019-04-07 14:21:47 +02008476 || (use_vpeekc && (c = vpeekc()) != NUL && c != Ctrl_C))
Bram Moolenaar586c70c2018-10-02 16:23:58 +02008477 && !got_int)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008478 {
8479 update_topline_cursor();
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02008480#ifdef FEAT_TERMINAL
Bram Moolenaar655a82a2018-05-08 22:01:07 +02008481 if (may_use_terminal_loop && term_use_loop()
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02008482 && oa.op_type == OP_NOP && oa.regname == NUL
8483 && !VIsual_active)
8484 {
8485 /* If terminal_loop() returns OK we got a key that is handled
8486 * in Normal model. With FAIL we first need to position the
8487 * cursor and the screen needs to be redrawn. */
8488 if (terminal_loop(TRUE) == OK)
8489 normal_cmd(&oa, TRUE);
8490 }
8491 else
8492#endif
8493 /* execute a Normal mode cmd */
8494 normal_cmd(&oa, TRUE);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008495 }
8496}
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008497
Bram Moolenaar071d4272004-06-13 20:20:40 +00008498#ifdef FEAT_FIND_ID
8499 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008500ex_checkpath(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008501{
8502 find_pattern_in_path(NULL, 0, 0, FALSE, FALSE, CHECK_PATH, 1L,
8503 eap->forceit ? ACTION_SHOW_ALL : ACTION_SHOW,
8504 (linenr_T)1, (linenr_T)MAXLNUM);
8505}
8506
Bram Moolenaar4033c552017-09-16 20:54:51 +02008507#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008508/*
8509 * ":psearch"
8510 */
8511 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008512ex_psearch(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008513{
8514 g_do_tagpreview = p_pvh;
8515 ex_findpat(eap);
8516 g_do_tagpreview = 0;
8517}
8518#endif
8519
8520 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008521ex_findpat(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008522{
8523 int whole = TRUE;
8524 long n;
8525 char_u *p;
8526 int action;
8527
8528 switch (cmdnames[eap->cmdidx].cmd_name[2])
8529 {
8530 case 'e': /* ":psearch", ":isearch" and ":dsearch" */
8531 if (cmdnames[eap->cmdidx].cmd_name[0] == 'p')
8532 action = ACTION_GOTO;
8533 else
8534 action = ACTION_SHOW;
8535 break;
8536 case 'i': /* ":ilist" and ":dlist" */
8537 action = ACTION_SHOW_ALL;
8538 break;
8539 case 'u': /* ":ijump" and ":djump" */
8540 action = ACTION_GOTO;
8541 break;
8542 default: /* ":isplit" and ":dsplit" */
8543 action = ACTION_SPLIT;
8544 break;
8545 }
8546
8547 n = 1;
8548 if (vim_isdigit(*eap->arg)) /* get count */
8549 {
8550 n = getdigits(&eap->arg);
8551 eap->arg = skipwhite(eap->arg);
8552 }
8553 if (*eap->arg == '/') /* Match regexp, not just whole words */
8554 {
8555 whole = FALSE;
8556 ++eap->arg;
8557 p = skip_regexp(eap->arg, '/', p_magic, NULL);
8558 if (*p)
8559 {
8560 *p++ = NUL;
8561 p = skipwhite(p);
8562
8563 /* Check for trailing illegal characters */
8564 if (!ends_excmd(*p))
8565 eap->errmsg = e_trailing;
8566 else
8567 eap->nextcmd = check_nextcmd(p);
8568 }
8569 }
8570 if (!eap->skip)
8571 find_pattern_in_path(eap->arg, 0, (int)STRLEN(eap->arg),
8572 whole, !eap->forceit,
8573 *eap->cmd == 'd' ? FIND_DEFINE : FIND_ANY,
8574 n, action, eap->line1, eap->line2);
8575}
8576#endif
8577
Bram Moolenaar071d4272004-06-13 20:20:40 +00008578
Bram Moolenaar4033c552017-09-16 20:54:51 +02008579#ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00008580/*
8581 * ":ptag", ":ptselect", ":ptjump", ":ptnext", etc.
8582 */
8583 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008584ex_ptag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008585{
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01008586 g_do_tagpreview = p_pvh; /* will be reset to 0 in ex_tag_cmd() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008587 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
8588}
8589
8590/*
8591 * ":pedit"
8592 */
8593 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008594ex_pedit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008595{
8596 win_T *curwin_save = curwin;
8597
Bram Moolenaar026587b2019-08-17 15:08:00 +02008598 // Open the preview window or popup and make it the current window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008599 g_do_tagpreview = p_pvh;
Bram Moolenaar576a4a62019-08-18 15:25:17 +02008600 prepare_tagpreview(TRUE, TRUE, FALSE);
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02008601
Bram Moolenaar026587b2019-08-17 15:08:00 +02008602 // Edit the file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008603 do_exedit(eap, NULL);
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02008604
Bram Moolenaar071d4272004-06-13 20:20:40 +00008605 if (curwin != curwin_save && win_valid(curwin_save))
8606 {
Bram Moolenaar026587b2019-08-17 15:08:00 +02008607 // Return cursor to where we were
Bram Moolenaar071d4272004-06-13 20:20:40 +00008608 validate_cursor();
8609 redraw_later(VALID);
8610 win_enter(curwin_save, TRUE);
8611 }
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02008612# ifdef FEAT_TEXT_PROP
8613 else if (WIN_IS_POPUP(curwin))
8614 {
8615 // can't keep focus in popup window
8616 win_enter(firstwin, TRUE);
8617 }
8618# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008619 g_do_tagpreview = 0;
8620}
Bram Moolenaar4033c552017-09-16 20:54:51 +02008621#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008622
8623/*
8624 * ":stag", ":stselect" and ":stjump".
8625 */
8626 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008627ex_stag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008628{
8629 postponed_split = -1;
8630 postponed_split_flags = cmdmod.split;
Bram Moolenaard326ce82007-03-11 14:48:29 +00008631 postponed_split_tab = cmdmod.tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008632 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
8633 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00008634 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008635}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008636
8637/*
8638 * ":tag", ":tselect", ":tjump", ":tnext", etc.
8639 */
8640 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008641ex_tag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008642{
8643 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name);
8644}
8645
8646 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008647ex_tag_cmd(exarg_T *eap, char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008648{
8649 int cmd;
8650
8651 switch (name[1])
8652 {
8653 case 'j': cmd = DT_JUMP; /* ":tjump" */
8654 break;
8655 case 's': cmd = DT_SELECT; /* ":tselect" */
8656 break;
8657 case 'p': cmd = DT_PREV; /* ":tprevious" */
8658 break;
8659 case 'N': cmd = DT_PREV; /* ":tNext" */
8660 break;
8661 case 'n': cmd = DT_NEXT; /* ":tnext" */
8662 break;
8663 case 'o': cmd = DT_POP; /* ":pop" */
8664 break;
8665 case 'f': /* ":tfirst" */
8666 case 'r': cmd = DT_FIRST; /* ":trewind" */
8667 break;
8668 case 'l': cmd = DT_LAST; /* ":tlast" */
8669 break;
8670 default: /* ":tag" */
8671#ifdef FEAT_CSCOPE
Bram Moolenaar7c94c262008-06-20 09:11:34 +00008672 if (p_cst && *eap->arg != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008673 {
Bram Moolenaard4db7712016-11-12 19:16:46 +01008674 ex_cstag(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008675 return;
8676 }
8677#endif
8678 cmd = DT_TAG;
8679 break;
8680 }
8681
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00008682 if (name[0] == 'l')
8683 {
8684#ifndef FEAT_QUICKFIX
8685 ex_ni(eap);
8686 return;
8687#else
8688 cmd = DT_LTAG;
8689#endif
8690 }
8691
Bram Moolenaar071d4272004-06-13 20:20:40 +00008692 do_tag(eap->arg, cmd, eap->addr_count > 0 ? (int)eap->line2 : 1,
8693 eap->forceit, TRUE);
8694}
8695
8696/*
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008697 * Check "str" for starting with a special cmdline variable.
8698 * If found return one of the SPEC_ values and set "*usedlen" to the length of
8699 * the variable. Otherwise return -1 and "*usedlen" is unchanged.
8700 */
8701 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008702find_cmdline_var(char_u *src, int *usedlen)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008703{
8704 int len;
8705 int i;
Bram Moolenaar8f0b2d42009-05-16 14:41:10 +00008706 static char *(spec_str[]) = {
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008707 "%",
8708#define SPEC_PERC 0
8709 "#",
Bram Moolenaar65f08472017-09-10 18:16:20 +02008710#define SPEC_HASH (SPEC_PERC + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008711 "<cword>", /* cursor word */
Bram Moolenaar65f08472017-09-10 18:16:20 +02008712#define SPEC_CWORD (SPEC_HASH + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008713 "<cWORD>", /* cursor WORD */
Bram Moolenaar65f08472017-09-10 18:16:20 +02008714#define SPEC_CCWORD (SPEC_CWORD + 1)
8715 "<cexpr>", /* expr under cursor */
8716#define SPEC_CEXPR (SPEC_CCWORD + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008717 "<cfile>", /* cursor path name */
Bram Moolenaar65f08472017-09-10 18:16:20 +02008718#define SPEC_CFILE (SPEC_CEXPR + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008719 "<sfile>", /* ":so" file name */
Bram Moolenaar65f08472017-09-10 18:16:20 +02008720#define SPEC_SFILE (SPEC_CFILE + 1)
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01008721 "<slnum>", /* ":so" file line number */
Bram Moolenaar65f08472017-09-10 18:16:20 +02008722#define SPEC_SLNUM (SPEC_SFILE + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008723 "<afile>", /* autocommand file name */
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008724#define SPEC_AFILE (SPEC_SLNUM + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008725 "<abuf>", /* autocommand buffer number */
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008726#define SPEC_ABUF (SPEC_AFILE + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008727 "<amatch>", /* autocommand match name */
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01008728#define SPEC_AMATCH (SPEC_ABUF + 1)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008729 "<sflnum>", /* script file line number */
8730#define SPEC_SFLNUM (SPEC_AMATCH + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008731#ifdef FEAT_CLIENTSERVER
8732 "<client>"
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008733# define SPEC_CLIENT (SPEC_SFLNUM + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008734#endif
8735 };
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008736
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00008737 for (i = 0; i < (int)(sizeof(spec_str) / sizeof(char *)); ++i)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008738 {
8739 len = (int)STRLEN(spec_str[i]);
8740 if (STRNCMP(src, spec_str[i], len) == 0)
8741 {
8742 *usedlen = len;
8743 return i;
8744 }
8745 }
8746 return -1;
8747}
8748
8749/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008750 * Evaluate cmdline variables.
8751 *
8752 * change '%' to curbuf->b_ffname
8753 * '#' to curwin->w_altfile
8754 * '<cword>' to word under the cursor
8755 * '<cWORD>' to WORD under the cursor
Bram Moolenaar8071cb22019-07-12 17:58:01 +02008756 * '<cexpr>' to C-expression under the cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00008757 * '<cfile>' to path name under the cursor
8758 * '<sfile>' to sourced file name
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01008759 * '<slnum>' to sourced file line number
Bram Moolenaar071d4272004-06-13 20:20:40 +00008760 * '<afile>' to file name for autocommand
8761 * '<abuf>' to buffer number for autocommand
8762 * '<amatch>' to matching name for autocommand
8763 *
8764 * When an error is detected, "errormsg" is set to a non-NULL pointer (may be
8765 * "" for error without a message) and NULL is returned.
8766 * Returns an allocated string if a valid match was found.
8767 * Returns NULL if no match was found. "usedlen" then still contains the
8768 * number of characters to skip.
8769 */
8770 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008771eval_vars(
8772 char_u *src, /* pointer into commandline */
8773 char_u *srcstart, /* beginning of valid memory for src */
8774 int *usedlen, /* characters after src that are used */
8775 linenr_T *lnump, /* line number for :e command, or NULL */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008776 char **errormsg, /* pointer to error message */
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008777 int *escaped) /* return value has escaped white space (can
Bram Moolenaar63b92542007-03-27 14:57:09 +00008778 * be NULL) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008779{
8780 int i;
8781 char_u *s;
8782 char_u *result;
8783 char_u *resultbuf = NULL;
8784 int resultlen;
8785 buf_T *buf;
8786 int valid = VALID_HEAD + VALID_PATH; /* assume valid result */
8787 int spec_idx;
8788#ifdef FEAT_MODIFY_FNAME
Bram Moolenaarfd249462018-07-28 16:14:30 +02008789 int tilde_file = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008790 int skip_mod = FALSE;
8791#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008792 char_u strbuf[30];
Bram Moolenaar071d4272004-06-13 20:20:40 +00008793
8794 *errormsg = NULL;
Bram Moolenaar63b92542007-03-27 14:57:09 +00008795 if (escaped != NULL)
8796 *escaped = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008797
8798 /*
8799 * Check if there is something to do.
8800 */
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008801 spec_idx = find_cmdline_var(src, usedlen);
8802 if (spec_idx < 0) /* no match */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008803 {
8804 *usedlen = 1;
8805 return NULL;
8806 }
8807
8808 /*
8809 * Skip when preceded with a backslash "\%" and "\#".
8810 * Note: In "\\%" the % is also not recognized!
8811 */
8812 if (src > srcstart && src[-1] == '\\')
8813 {
8814 *usedlen = 0;
Bram Moolenaara7241f52008-06-24 20:39:31 +00008815 STRMOVE(src - 1, src); /* remove backslash */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008816 return NULL;
8817 }
8818
8819 /*
8820 * word or WORD under cursor
8821 */
Bram Moolenaar65f08472017-09-10 18:16:20 +02008822 if (spec_idx == SPEC_CWORD || spec_idx == SPEC_CCWORD
8823 || spec_idx == SPEC_CEXPR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008824 {
Bram Moolenaar65f08472017-09-10 18:16:20 +02008825 resultlen = find_ident_under_cursor(&result,
8826 spec_idx == SPEC_CWORD ? (FIND_IDENT | FIND_STRING)
8827 : spec_idx == SPEC_CEXPR ? (FIND_IDENT | FIND_STRING | FIND_EVAL)
8828 : FIND_STRING);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008829 if (resultlen == 0)
8830 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008831 *errormsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +00008832 return NULL;
8833 }
8834 }
8835
8836 /*
8837 * '#': Alternate file name
8838 * '%': Current file name
8839 * File name under the cursor
8840 * File name for autocommand
8841 * and following modifiers
8842 */
8843 else
8844 {
8845 switch (spec_idx)
8846 {
8847 case SPEC_PERC: /* '%': current file */
8848 if (curbuf->b_fname == NULL)
8849 {
8850 result = (char_u *)"";
8851 valid = 0; /* Must have ":p:h" to be valid */
8852 }
8853 else
Bram Moolenaar00136dc2018-07-25 21:19:13 +02008854 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008855 result = curbuf->b_fname;
Bram Moolenaarfd249462018-07-28 16:14:30 +02008856#ifdef FEAT_MODIFY_FNAME
Bram Moolenaar00136dc2018-07-25 21:19:13 +02008857 tilde_file = STRCMP(result, "~") == 0;
Bram Moolenaarfd249462018-07-28 16:14:30 +02008858#endif
Bram Moolenaar00136dc2018-07-25 21:19:13 +02008859 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008860 break;
8861
8862 case SPEC_HASH: /* '#' or "#99": alternate file */
8863 if (src[1] == '#') /* "##": the argument list */
8864 {
8865 result = arg_all();
8866 resultbuf = result;
8867 *usedlen = 2;
Bram Moolenaar63b92542007-03-27 14:57:09 +00008868 if (escaped != NULL)
8869 *escaped = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008870#ifdef FEAT_MODIFY_FNAME
8871 skip_mod = TRUE;
8872#endif
8873 break;
8874 }
8875 s = src + 1;
Bram Moolenaard812df62008-11-09 12:46:09 +00008876 if (*s == '<') /* "#<99" uses v:oldfiles */
8877 ++s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008878 i = (int)getdigits(&s);
Bram Moolenaarc312b8b2017-10-28 17:53:04 +02008879 if (s == src + 2 && src[1] == '-')
8880 /* just a minus sign, don't skip over it */
8881 s--;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008882 *usedlen = (int)(s - src); /* length of what we expand */
8883
Bram Moolenaarc312b8b2017-10-28 17:53:04 +02008884 if (src[1] == '<' && i != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008885 {
Bram Moolenaard812df62008-11-09 12:46:09 +00008886 if (*usedlen < 2)
8887 {
8888 /* Should we give an error message for #<text? */
8889 *usedlen = 1;
8890 return NULL;
8891 }
8892#ifdef FEAT_EVAL
8893 result = list_find_str(get_vim_var_list(VV_OLDFILES),
8894 (long)i);
8895 if (result == NULL)
8896 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008897 *errormsg = "";
Bram Moolenaard812df62008-11-09 12:46:09 +00008898 return NULL;
8899 }
8900#else
Bram Moolenaar8e481e82019-01-15 20:07:48 +01008901 *errormsg = _("E809: #< is not available without the +eval feature");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008902 return NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +00008903#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008904 }
8905 else
Bram Moolenaard812df62008-11-09 12:46:09 +00008906 {
Bram Moolenaarc312b8b2017-10-28 17:53:04 +02008907 if (i == 0 && src[1] == '<' && *usedlen > 1)
8908 *usedlen = 1;
Bram Moolenaard812df62008-11-09 12:46:09 +00008909 buf = buflist_findnr(i);
8910 if (buf == NULL)
8911 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008912 *errormsg = _("E194: No alternate file name to substitute for '#'");
Bram Moolenaard812df62008-11-09 12:46:09 +00008913 return NULL;
8914 }
8915 if (lnump != NULL)
8916 *lnump = ECMD_LAST;
8917 if (buf->b_fname == NULL)
8918 {
8919 result = (char_u *)"";
8920 valid = 0; /* Must have ":p:h" to be valid */
8921 }
8922 else
Bram Moolenaar00136dc2018-07-25 21:19:13 +02008923 {
Bram Moolenaard812df62008-11-09 12:46:09 +00008924 result = buf->b_fname;
Bram Moolenaarfd249462018-07-28 16:14:30 +02008925#ifdef FEAT_MODIFY_FNAME
Bram Moolenaar00136dc2018-07-25 21:19:13 +02008926 tilde_file = STRCMP(result, "~") == 0;
Bram Moolenaarfd249462018-07-28 16:14:30 +02008927#endif
Bram Moolenaar00136dc2018-07-25 21:19:13 +02008928 }
Bram Moolenaard812df62008-11-09 12:46:09 +00008929 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008930 break;
8931
8932#ifdef FEAT_SEARCHPATH
8933 case SPEC_CFILE: /* file name under cursor */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008934 result = file_name_at_cursor(FNAME_MESS|FNAME_HYP, 1L, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008935 if (result == NULL)
8936 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008937 *errormsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +00008938 return NULL;
8939 }
8940 resultbuf = result; /* remember allocated string */
8941 break;
8942#endif
8943
Bram Moolenaar071d4272004-06-13 20:20:40 +00008944 case SPEC_AFILE: /* file name for autocommand */
8945 result = autocmd_fname;
Bram Moolenaarf6dad432008-09-18 19:29:58 +00008946 if (result != NULL && !autocmd_fname_full)
8947 {
8948 /* Still need to turn the fname into a full path. It is
8949 * postponed to avoid a delay when <afile> is not used. */
8950 autocmd_fname_full = TRUE;
8951 result = FullName_save(autocmd_fname, FALSE);
8952 vim_free(autocmd_fname);
8953 autocmd_fname = result;
8954 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008955 if (result == NULL)
8956 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008957 *errormsg = _("E495: no autocommand file name to substitute for \"<afile>\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008958 return NULL;
8959 }
Bram Moolenaara0174af2008-01-02 20:08:25 +00008960 result = shorten_fname1(result);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008961 break;
8962
8963 case SPEC_ABUF: /* buffer number for autocommand */
8964 if (autocmd_bufnr <= 0)
8965 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008966 *errormsg = _("E496: no autocommand buffer number to substitute for \"<abuf>\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008967 return NULL;
8968 }
8969 sprintf((char *)strbuf, "%d", autocmd_bufnr);
8970 result = strbuf;
8971 break;
8972
8973 case SPEC_AMATCH: /* match name for autocommand */
8974 result = autocmd_match;
8975 if (result == NULL)
8976 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008977 *errormsg = _("E497: no autocommand match name to substitute for \"<amatch>\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008978 return NULL;
8979 }
8980 break;
8981
Bram Moolenaar071d4272004-06-13 20:20:40 +00008982 case SPEC_SFILE: /* file name for ":so" command */
8983 result = sourcing_name;
8984 if (result == NULL)
8985 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008986 *errormsg = _("E498: no :source file name to substitute for \"<sfile>\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008987 return NULL;
8988 }
8989 break;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008990
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01008991 case SPEC_SLNUM: /* line in file for ":so" command */
8992 if (sourcing_name == NULL || sourcing_lnum == 0)
8993 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008994 *errormsg = _("E842: no line number to use for \"<slnum>\"");
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01008995 return NULL;
8996 }
8997 sprintf((char *)strbuf, "%ld", (long)sourcing_lnum);
8998 result = strbuf;
8999 break;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009000
9001#ifdef FEAT_EVAL
9002 case SPEC_SFLNUM: /* line in script file */
9003 if (current_sctx.sc_lnum + sourcing_lnum == 0)
9004 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009005 *errormsg = _("E961: no line number to use for \"<sflnum>\"");
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009006 return NULL;
9007 }
9008 sprintf((char *)strbuf, "%ld",
9009 (long)(current_sctx.sc_lnum + sourcing_lnum));
9010 result = strbuf;
9011 break;
9012#endif
9013
9014#ifdef FEAT_CLIENTSERVER
Bram Moolenaar071d4272004-06-13 20:20:40 +00009015 case SPEC_CLIENT: /* Source of last submitted input */
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009016 sprintf((char *)strbuf, PRINTF_HEX_LONG_U,
9017 (long_u)clientWindow);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009018 result = strbuf;
9019 break;
9020#endif
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009021
Bram Moolenaar9e0f6ec2017-05-16 09:36:54 +02009022 default:
9023 result = (char_u *)""; /* avoid gcc warning */
9024 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009025 }
9026
9027 resultlen = (int)STRLEN(result); /* length of new string */
9028 if (src[*usedlen] == '<') /* remove the file name extension */
9029 {
9030 ++*usedlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009031 if ((s = vim_strrchr(result, '.')) != NULL && s >= gettail(result))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009032 resultlen = (int)(s - result);
9033 }
9034#ifdef FEAT_MODIFY_FNAME
9035 else if (!skip_mod)
9036 {
Bram Moolenaar00136dc2018-07-25 21:19:13 +02009037 valid |= modify_fname(src, tilde_file, usedlen, &result, &resultbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009038 &resultlen);
9039 if (result == NULL)
9040 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009041 *errormsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +00009042 return NULL;
9043 }
9044 }
9045#endif
9046 }
9047
9048 if (resultlen == 0 || valid != VALID_HEAD + VALID_PATH)
9049 {
9050 if (valid != VALID_HEAD + VALID_PATH)
9051 /* xgettext:no-c-format */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009052 *errormsg = _("E499: Empty file name for '%' or '#', only works with \":p:h\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00009053 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009054 *errormsg = _("E500: Evaluates to an empty string");
Bram Moolenaar071d4272004-06-13 20:20:40 +00009055 result = NULL;
9056 }
9057 else
9058 result = vim_strnsave(result, resultlen);
9059 vim_free(resultbuf);
9060 return result;
9061}
9062
9063/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009064 * Expand the <sfile> string in "arg".
9065 *
9066 * Returns an allocated string, or NULL for any error.
9067 */
9068 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009069expand_sfile(char_u *arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009070{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009071 char *errormsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009072 int len;
9073 char_u *result;
9074 char_u *newres;
9075 char_u *repl;
9076 int srclen;
9077 char_u *p;
9078
9079 result = vim_strsave(arg);
9080 if (result == NULL)
9081 return NULL;
9082
9083 for (p = result; *p; )
9084 {
9085 if (STRNCMP(p, "<sfile>", 7) != 0)
9086 ++p;
9087 else
9088 {
9089 /* replace "<sfile>" with the sourced file name, and do ":" stuff */
Bram Moolenaar63b92542007-03-27 14:57:09 +00009090 repl = eval_vars(p, result, &srclen, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009091 if (errormsg != NULL)
9092 {
9093 if (*errormsg)
9094 emsg(errormsg);
9095 vim_free(result);
9096 return NULL;
9097 }
9098 if (repl == NULL) /* no match (cannot happen) */
9099 {
9100 p += srclen;
9101 continue;
9102 }
9103 len = (int)STRLEN(result) - srclen + (int)STRLEN(repl) + 1;
9104 newres = alloc(len);
9105 if (newres == NULL)
9106 {
9107 vim_free(repl);
9108 vim_free(result);
9109 return NULL;
9110 }
9111 mch_memmove(newres, result, (size_t)(p - result));
9112 STRCPY(newres + (p - result), repl);
9113 len = (int)STRLEN(newres);
9114 STRCAT(newres, p + srclen);
9115 vim_free(repl);
9116 vim_free(result);
9117 result = newres;
9118 p = newres + len; /* continue after the match */
9119 }
9120 }
9121
9122 return result;
9123}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009124
Bram Moolenaar071d4272004-06-13 20:20:40 +00009125#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG) || defined(PROTO)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00009126/*
Bram Moolenaard9462e32011-04-11 21:35:11 +02009127 * Make a dialog message in "buff[DIALOG_MSG_SIZE]".
Bram Moolenaarb765d632005-06-07 21:00:02 +00009128 * "format" must contain "%s".
Bram Moolenaar9c13b352005-05-19 20:53:52 +00009129 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009130 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009131dialog_msg(char_u *buff, char *format, char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009132{
Bram Moolenaar071d4272004-06-13 20:20:40 +00009133 if (fname == NULL)
9134 fname = (char_u *)_("Untitled");
Bram Moolenaard9462e32011-04-11 21:35:11 +02009135 vim_snprintf((char *)buff, DIALOG_MSG_SIZE, format, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009136}
9137#endif
9138
9139/*
9140 * ":behave {mswin,xterm}"
9141 */
9142 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009143ex_behave(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009144{
9145 if (STRCMP(eap->arg, "mswin") == 0)
9146 {
9147 set_option_value((char_u *)"selection", 0L, (char_u *)"exclusive", 0);
9148 set_option_value((char_u *)"selectmode", 0L, (char_u *)"mouse,key", 0);
9149 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"popup", 0);
9150 set_option_value((char_u *)"keymodel", 0L,
9151 (char_u *)"startsel,stopsel", 0);
9152 }
9153 else if (STRCMP(eap->arg, "xterm") == 0)
9154 {
9155 set_option_value((char_u *)"selection", 0L, (char_u *)"inclusive", 0);
9156 set_option_value((char_u *)"selectmode", 0L, (char_u *)"", 0);
9157 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"extend", 0);
9158 set_option_value((char_u *)"keymodel", 0L, (char_u *)"", 0);
9159 }
9160 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009161 semsg(_(e_invarg2), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009162}
9163
Bram Moolenaar42b4dda2010-03-02 15:56:05 +01009164/*
9165 * Function given to ExpandGeneric() to obtain the possible arguments of the
9166 * ":behave {mswin,xterm}" command.
9167 */
9168 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009169get_behave_arg(expand_T *xp UNUSED, int idx)
Bram Moolenaar42b4dda2010-03-02 15:56:05 +01009170{
9171 if (idx == 0)
9172 return (char_u *)"mswin";
9173 if (idx == 1)
9174 return (char_u *)"xterm";
9175 return NULL;
9176}
Bram Moolenaar9e507ca2016-10-15 15:39:39 +02009177
9178/*
9179 * Function given to ExpandGeneric() to obtain the possible arguments of the
9180 * ":messages {clear}" command.
9181 */
9182 char_u *
9183get_messages_arg(expand_T *xp UNUSED, int idx)
9184{
9185 if (idx == 0)
9186 return (char_u *)"clear";
9187 return NULL;
9188}
Bram Moolenaar42b4dda2010-03-02 15:56:05 +01009189
Bram Moolenaarcae92dc2017-08-06 15:22:15 +02009190 char_u *
9191get_mapclear_arg(expand_T *xp UNUSED, int idx)
9192{
9193 if (idx == 0)
9194 return (char_u *)"<buffer>";
9195 return NULL;
9196}
9197
Bram Moolenaar071d4272004-06-13 20:20:40 +00009198static int filetype_detect = FALSE;
9199static int filetype_plugin = FALSE;
9200static int filetype_indent = FALSE;
9201
9202/*
9203 * ":filetype [plugin] [indent] {on,off,detect}"
9204 * on: Load the filetype.vim file to install autocommands for file types.
9205 * off: Load the ftoff.vim file to remove all autocommands for file types.
9206 * plugin on: load filetype.vim and ftplugin.vim
9207 * plugin off: load ftplugof.vim
9208 * indent on: load filetype.vim and indent.vim
9209 * indent off: load indoff.vim
9210 */
9211 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009212ex_filetype(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009213{
9214 char_u *arg = eap->arg;
9215 int plugin = FALSE;
9216 int indent = FALSE;
9217
9218 if (*eap->arg == NUL)
9219 {
9220 /* Print current status. */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009221 smsg("filetype detection:%s plugin:%s indent:%s",
Bram Moolenaar071d4272004-06-13 20:20:40 +00009222 filetype_detect ? "ON" : "OFF",
9223 filetype_plugin ? (filetype_detect ? "ON" : "(on)") : "OFF",
9224 filetype_indent ? (filetype_detect ? "ON" : "(on)") : "OFF");
9225 return;
9226 }
9227
9228 /* Accept "plugin" and "indent" in any order. */
9229 for (;;)
9230 {
9231 if (STRNCMP(arg, "plugin", 6) == 0)
9232 {
9233 plugin = TRUE;
9234 arg = skipwhite(arg + 6);
9235 continue;
9236 }
9237 if (STRNCMP(arg, "indent", 6) == 0)
9238 {
9239 indent = TRUE;
9240 arg = skipwhite(arg + 6);
9241 continue;
9242 }
9243 break;
9244 }
9245 if (STRCMP(arg, "on") == 0 || STRCMP(arg, "detect") == 0)
9246 {
9247 if (*arg == 'o' || !filetype_detect)
9248 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01009249 source_runtime((char_u *)FILETYPE_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009250 filetype_detect = TRUE;
9251 if (plugin)
9252 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01009253 source_runtime((char_u *)FTPLUGIN_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009254 filetype_plugin = TRUE;
9255 }
9256 if (indent)
9257 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01009258 source_runtime((char_u *)INDENT_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009259 filetype_indent = TRUE;
9260 }
9261 }
9262 if (*arg == 'd')
9263 {
Bram Moolenaar1610d052016-06-09 22:53:01 +02009264 (void)do_doautocmd((char_u *)"filetypedetect BufRead", TRUE, NULL);
Bram Moolenaara3227e22006-03-08 21:32:40 +00009265 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009266 }
9267 }
9268 else if (STRCMP(arg, "off") == 0)
9269 {
9270 if (plugin || indent)
9271 {
9272 if (plugin)
9273 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01009274 source_runtime((char_u *)FTPLUGOF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009275 filetype_plugin = FALSE;
9276 }
9277 if (indent)
9278 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01009279 source_runtime((char_u *)INDOFF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009280 filetype_indent = FALSE;
9281 }
9282 }
9283 else
9284 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01009285 source_runtime((char_u *)FTOFF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009286 filetype_detect = FALSE;
9287 }
9288 }
9289 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009290 semsg(_(e_invarg2), arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009291}
9292
9293/*
Bram Moolenaar3e545692017-06-04 19:00:32 +02009294 * ":setfiletype [FALLBACK] {name}"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009295 */
9296 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009297ex_setfiletype(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009298{
9299 if (!did_filetype)
Bram Moolenaar3e545692017-06-04 19:00:32 +02009300 {
9301 char_u *arg = eap->arg;
9302
9303 if (STRNCMP(arg, "FALLBACK ", 9) == 0)
9304 arg += 9;
9305
9306 set_option_value((char_u *)"filetype", 0L, arg, OPT_LOCAL);
9307 if (arg != eap->arg)
9308 did_filetype = FALSE;
9309 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009310}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009311
Bram Moolenaar071d4272004-06-13 20:20:40 +00009312 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009313ex_digraphs(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009314{
9315#ifdef FEAT_DIGRAPHS
9316 if (*eap->arg != NUL)
9317 putdigraph(eap->arg);
9318 else
Bram Moolenaareae8ae12018-12-14 18:53:02 +01009319 listdigraphs(eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009320#else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009321 emsg(_("E196: No digraphs in this version"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009322#endif
9323}
9324
9325 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009326ex_set(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009327{
9328 int flags = 0;
9329
9330 if (eap->cmdidx == CMD_setlocal)
9331 flags = OPT_LOCAL;
9332 else if (eap->cmdidx == CMD_setglobal)
9333 flags = OPT_GLOBAL;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01009334#if defined(FEAT_EVAL) && defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009335 if (cmdmod.browse && flags == 0)
9336 ex_options(eap);
9337 else
9338#endif
9339 (void)do_set(eap->arg, flags);
9340}
9341
Bram Moolenaar451fc7b2018-04-27 22:53:07 +02009342#if defined(FEAT_SEARCH_EXTRA) || defined(PROTO)
9343 void
9344set_no_hlsearch(int flag)
9345{
9346 no_hlsearch = flag;
9347# ifdef FEAT_EVAL
9348 set_vim_var_nr(VV_HLSEARCH, !no_hlsearch && p_hls);
9349# endif
9350}
9351
Bram Moolenaar071d4272004-06-13 20:20:40 +00009352/*
9353 * ":nohlsearch"
9354 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009355 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009356ex_nohlsearch(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009357{
Bram Moolenaar451fc7b2018-04-27 22:53:07 +02009358 set_no_hlsearch(TRUE);
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00009359 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009360}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009361#endif
9362
9363#ifdef FEAT_CRYPT
9364/*
9365 * ":X": Get crypt key
9366 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009367 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009368ex_X(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009369{
Bram Moolenaar3a0c9082014-11-12 15:15:42 +01009370 crypt_check_current_method();
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02009371 (void)crypt_get_key(TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009372}
9373#endif
9374
9375#ifdef FEAT_FOLDING
9376 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009377ex_fold(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009378{
9379 if (foldManualAllowed(TRUE))
9380 foldCreate(eap->line1, eap->line2);
9381}
9382
9383 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009384ex_foldopen(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009385{
9386 opFoldRange(eap->line1, eap->line2, eap->cmdidx == CMD_foldopen,
9387 eap->forceit, FALSE);
9388}
9389
9390 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009391ex_folddo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009392{
9393 linenr_T lnum;
9394
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02009395#ifdef FEAT_CLIPBOARD
9396 start_global_changes();
9397#endif
9398
Bram Moolenaar071d4272004-06-13 20:20:40 +00009399 /* First set the marks for all lines closed/open. */
9400 for (lnum = eap->line1; lnum <= eap->line2; ++lnum)
9401 if (hasFolding(lnum, NULL, NULL) == (eap->cmdidx == CMD_folddoclosed))
9402 ml_setmarked(lnum);
9403
9404 /* Execute the command on the marked lines. */
9405 global_exe(eap->arg);
9406 ml_clearmarked(); /* clear rest of the marks */
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02009407#ifdef FEAT_CLIPBOARD
9408 end_global_changes();
9409#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009410}
9411#endif
Bram Moolenaarf5291f32017-09-14 22:55:37 +02009412
Bram Moolenaar39665952018-08-15 20:59:48 +02009413#ifdef FEAT_QUICKFIX
9414/*
9415 * Returns TRUE if the supplied Ex cmdidx is for a location list command
9416 * instead of a quickfix command.
9417 */
9418 int
9419is_loclist_cmd(int cmdidx)
9420{
Bram Moolenaar74c8be22018-08-23 22:51:40 +02009421 if (cmdidx < 0 || cmdidx >= CMD_SIZE)
Bram Moolenaar39665952018-08-15 20:59:48 +02009422 return FALSE;
9423 return cmdnames[cmdidx].cmd_name[0] == 'l';
9424}
9425#endif
9426
Bram Moolenaarf5291f32017-09-14 22:55:37 +02009427# if defined(FEAT_TIMERS) || defined(PROTO)
9428 int
9429get_pressedreturn(void)
9430{
9431 return ex_pressedreturn;
9432}
9433
9434 void
9435set_pressedreturn(int val)
9436{
9437 ex_pressedreturn = val;
9438}
9439#endif