blob: 2b2ef36bdd5302b337128b10473163766ec30aef [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:
Bram Moolenaar00b0d6d2019-08-21 22:25:30 +02004636 // Only a boundary check, not whether the buffers actually
4637 // exist.
4638 if (eap->line1 < 1 || eap->line2 > get_highest_fnum())
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004639 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004640 break;
4641 case ADDR_LOADED_BUFFERS:
4642 buf = firstbuf;
4643 while (buf->b_ml.ml_mfp == NULL)
4644 {
4645 if (buf->b_next == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004646 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004647 buf = buf->b_next;
4648 }
4649 if (eap->line1 < buf->b_fnum)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004650 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004651 buf = lastbuf;
4652 while (buf->b_ml.ml_mfp == NULL)
4653 {
4654 if (buf->b_prev == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004655 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004656 buf = buf->b_prev;
4657 }
4658 if (eap->line2 > buf->b_fnum)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004659 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004660 break;
4661 case ADDR_WINDOWS:
Bram Moolenaar8be63882015-01-14 11:25:05 +01004662 if (eap->line2 > LAST_WIN_NR)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004663 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004664 break;
4665 case ADDR_TABS:
4666 if (eap->line2 > LAST_TAB_NR)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004667 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004668 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004669 case ADDR_TABS_RELATIVE:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004670 case ADDR_OTHER:
4671 // Any range is OK.
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004672 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004673 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004674#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02004675 // No error for value that is too big, will use the last entry.
4676 if (eap->line2 <= 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004677 return _(e_invrange);
Bram Moolenaare906c502015-09-09 21:10:39 +02004678#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004679 break;
Bram Moolenaar25190db2019-05-04 15:05:28 +02004680 case ADDR_QUICKFIX_VALID:
4681#ifdef FEAT_QUICKFIX
4682 if ((eap->line2 != 1 && eap->line2 > qf_get_valid_size(eap))
4683 || eap->line2 < 0)
4684 return _(e_invrange);
4685#endif
4686 break;
4687 case ADDR_UNSIGNED:
4688 if (eap->line2 < 0)
4689 return _(e_invrange);
4690 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02004691 case ADDR_NONE:
4692 // Will give an error elsewhere.
4693 break;
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004694 }
4695 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004696 return NULL;
4697}
4698
4699/*
4700 * Correct the range for zero line number, if required.
4701 */
4702 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004703correct_range(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004704{
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004705 if (!(eap->argt & EX_ZEROR)) // zero in range not allowed
Bram Moolenaar071d4272004-06-13 20:20:40 +00004706 {
4707 if (eap->line1 == 0)
4708 eap->line1 = 1;
4709 if (eap->line2 == 0)
4710 eap->line2 = 1;
4711 }
4712}
4713
Bram Moolenaar748bf032005-02-02 23:04:36 +00004714#ifdef FEAT_QUICKFIX
Bram Moolenaar748bf032005-02-02 23:04:36 +00004715/*
4716 * For a ":vimgrep" or ":vimgrepadd" command return a pointer past the
4717 * pattern. Otherwise return eap->arg.
4718 */
4719 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004720skip_grep_pat(exarg_T *eap)
Bram Moolenaar748bf032005-02-02 23:04:36 +00004721{
4722 char_u *p = eap->arg;
4723
Bram Moolenaara37420f2006-02-04 22:37:47 +00004724 if (*p != NUL && (eap->cmdidx == CMD_vimgrep || eap->cmdidx == CMD_lvimgrep
4725 || eap->cmdidx == CMD_vimgrepadd
4726 || eap->cmdidx == CMD_lvimgrepadd
4727 || grep_internal(eap->cmdidx)))
Bram Moolenaar748bf032005-02-02 23:04:36 +00004728 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00004729 p = skip_vimgrep_pat(p, NULL, NULL);
Bram Moolenaar748bf032005-02-02 23:04:36 +00004730 if (p == NULL)
4731 p = eap->arg;
Bram Moolenaar748bf032005-02-02 23:04:36 +00004732 }
4733 return p;
4734}
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004735
4736/*
4737 * For the ":make" and ":grep" commands insert the 'makeprg'/'grepprg' option
4738 * in the command line, so that things like % get expanded.
4739 */
4740 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004741replace_makeprg(exarg_T *eap, char_u *p, char_u **cmdlinep)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004742{
4743 char_u *new_cmdline;
4744 char_u *program;
4745 char_u *pos;
4746 char_u *ptr;
4747 int len;
4748 int i;
4749
4750 /*
4751 * Don't do it when ":vimgrep" is used for ":grep".
4752 */
Bram Moolenaara37420f2006-02-04 22:37:47 +00004753 if ((eap->cmdidx == CMD_make || eap->cmdidx == CMD_lmake
4754 || eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4755 || eap->cmdidx == CMD_grepadd
4756 || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004757 && !grep_internal(eap->cmdidx))
4758 {
Bram Moolenaara37420f2006-02-04 22:37:47 +00004759 if (eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4760 || eap->cmdidx == CMD_grepadd || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004761 {
4762 if (*curbuf->b_p_gp == NUL)
4763 program = p_gp;
4764 else
4765 program = curbuf->b_p_gp;
4766 }
4767 else
4768 {
4769 if (*curbuf->b_p_mp == NUL)
4770 program = p_mp;
4771 else
4772 program = curbuf->b_p_mp;
4773 }
4774
4775 p = skipwhite(p);
4776
4777 if ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4778 {
4779 /* replace $* by given arguments */
4780 i = 1;
4781 while ((pos = (char_u *)strstr((char *)pos + 2, "$*")) != NULL)
4782 ++i;
4783 len = (int)STRLEN(p);
Bram Moolenaar51e14382019-05-25 20:21:28 +02004784 new_cmdline = alloc(STRLEN(program) + i * (len - 2) + 1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004785 if (new_cmdline == NULL)
4786 return NULL; /* out of memory */
4787 ptr = new_cmdline;
4788 while ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4789 {
4790 i = (int)(pos - program);
4791 STRNCPY(ptr, program, i);
4792 STRCPY(ptr += i, p);
4793 ptr += len;
4794 program = pos + 2;
4795 }
4796 STRCPY(ptr, program);
4797 }
4798 else
4799 {
Bram Moolenaar51e14382019-05-25 20:21:28 +02004800 new_cmdline = alloc(STRLEN(program) + STRLEN(p) + 2);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004801 if (new_cmdline == NULL)
4802 return NULL; /* out of memory */
4803 STRCPY(new_cmdline, program);
4804 STRCAT(new_cmdline, " ");
4805 STRCAT(new_cmdline, p);
4806 }
4807 msg_make(p);
4808
4809 /* 'eap->cmd' is not set here, because it is not used at CMD_make */
4810 vim_free(*cmdlinep);
4811 *cmdlinep = new_cmdline;
4812 p = new_cmdline;
4813 }
4814 return p;
4815}
Bram Moolenaar748bf032005-02-02 23:04:36 +00004816#endif
4817
Bram Moolenaar071d4272004-06-13 20:20:40 +00004818/*
4819 * Expand file name in Ex command argument.
Bram Moolenaar0abb4272019-06-15 16:06:00 +02004820 * When an error is detected, "errormsgp" is set to a non-NULL pointer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004821 * Return FAIL for failure, OK otherwise.
4822 */
4823 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004824expand_filename(
4825 exarg_T *eap,
4826 char_u **cmdlinep,
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004827 char **errormsgp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004828{
4829 int has_wildcards; /* need to expand wildcards */
4830 char_u *repl;
4831 int srclen;
4832 char_u *p;
4833 int n;
Bram Moolenaar63b92542007-03-27 14:57:09 +00004834 int escaped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004835
Bram Moolenaar748bf032005-02-02 23:04:36 +00004836#ifdef FEAT_QUICKFIX
4837 /* Skip a regexp pattern for ":vimgrep[add] pat file..." */
4838 p = skip_grep_pat(eap);
4839#else
4840 p = eap->arg;
4841#endif
4842
Bram Moolenaar071d4272004-06-13 20:20:40 +00004843 /*
4844 * Decide to expand wildcards *before* replacing '%', '#', etc. If
4845 * the file name contains a wildcard it should not cause expanding.
4846 * (it will be expanded anyway if there is a wildcard before replacing).
4847 */
Bram Moolenaar748bf032005-02-02 23:04:36 +00004848 has_wildcards = mch_has_wildcard(p);
4849 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004850 {
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004851#ifdef FEAT_EVAL
4852 /* Skip over `=expr`, wildcards in it are not expanded. */
4853 if (p[0] == '`' && p[1] == '=')
4854 {
4855 p += 2;
4856 (void)skip_expr(&p);
4857 if (*p == '`')
4858 ++p;
4859 continue;
4860 }
4861#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004862 /*
4863 * Quick check if this cannot be the start of a special string.
4864 * Also removes backslash before '%', '#' and '<'.
4865 */
4866 if (vim_strchr((char_u *)"%#<", *p) == NULL)
4867 {
4868 ++p;
4869 continue;
4870 }
4871
4872 /*
4873 * Try to find a match at this position.
4874 */
Bram Moolenaar63b92542007-03-27 14:57:09 +00004875 repl = eval_vars(p, eap->arg, &srclen, &(eap->do_ecmd_lnum),
4876 errormsgp, &escaped);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004877 if (*errormsgp != NULL) /* error detected */
4878 return FAIL;
4879 if (repl == NULL) /* no match found */
4880 {
4881 p += srclen;
4882 continue;
4883 }
4884
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00004885 /* Wildcards won't be expanded below, the replacement is taken
4886 * literally. But do expand "~/file", "~user/file" and "$HOME/file". */
4887 if (vim_strchr(repl, '$') != NULL || vim_strchr(repl, '~') != NULL)
4888 {
4889 char_u *l = repl;
4890
4891 repl = expand_env_save(repl);
4892 vim_free(l);
4893 }
4894
Bram Moolenaar63b92542007-03-27 14:57:09 +00004895 /* Need to escape white space et al. with a backslash.
4896 * Don't do this for:
4897 * - replacement that already has been escaped: "##"
4898 * - shell commands (may have to use quotes instead).
4899 * - non-unix systems when there is a single argument (spaces don't
4900 * separate arguments then).
4901 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004902 if (!eap->usefilter
Bram Moolenaar63b92542007-03-27 14:57:09 +00004903 && !escaped
Bram Moolenaar071d4272004-06-13 20:20:40 +00004904 && eap->cmdidx != CMD_bang
Bram Moolenaar071d4272004-06-13 20:20:40 +00004905 && eap->cmdidx != CMD_grep
4906 && eap->cmdidx != CMD_grepadd
Bram Moolenaarbf15b8d2017-06-04 20:43:48 +02004907 && eap->cmdidx != CMD_hardcopy
Bram Moolenaar67883b42017-07-27 22:57:00 +02004908 && eap->cmdidx != CMD_lgrep
4909 && eap->cmdidx != CMD_lgrepadd
4910 && eap->cmdidx != CMD_lmake
4911 && eap->cmdidx != CMD_make
4912 && eap->cmdidx != CMD_terminal
Bram Moolenaar071d4272004-06-13 20:20:40 +00004913#ifndef UNIX
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004914 && !(eap->argt & EX_NOSPC)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004915#endif
4916 )
4917 {
4918 char_u *l;
4919#ifdef BACKSLASH_IN_FILENAME
4920 /* Don't escape a backslash here, because rem_backslash() doesn't
4921 * remove it later. */
4922 static char_u *nobslash = (char_u *)" \t\"|";
4923# define ESCAPE_CHARS nobslash
4924#else
4925# define ESCAPE_CHARS escape_chars
4926#endif
4927
4928 for (l = repl; *l; ++l)
4929 if (vim_strchr(ESCAPE_CHARS, *l) != NULL)
4930 {
4931 l = vim_strsave_escaped(repl, ESCAPE_CHARS);
4932 if (l != NULL)
4933 {
4934 vim_free(repl);
4935 repl = l;
4936 }
4937 break;
4938 }
4939 }
4940
4941 /* For a shell command a '!' must be escaped. */
Bram Moolenaar67883b42017-07-27 22:57:00 +02004942 if ((eap->usefilter || eap->cmdidx == CMD_bang
4943 || eap->cmdidx == CMD_terminal)
Bram Moolenaar31b7d382014-04-01 18:54:48 +02004944 && vim_strpbrk(repl, (char_u *)"!") != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004945 {
4946 char_u *l;
4947
Bram Moolenaar31b7d382014-04-01 18:54:48 +02004948 l = vim_strsave_escaped(repl, (char_u *)"!");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004949 if (l != NULL)
4950 {
4951 vim_free(repl);
4952 repl = l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004953 }
4954 }
4955
4956 p = repl_cmdline(eap, p, srclen, repl, cmdlinep);
4957 vim_free(repl);
4958 if (p == NULL)
4959 return FAIL;
4960 }
4961
4962 /*
4963 * One file argument: Expand wildcards.
4964 * Don't do this with ":r !command" or ":w !command".
4965 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004966 if ((eap->argt & EX_NOSPC) && !eap->usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004967 {
4968 /*
4969 * May do this twice:
4970 * 1. Replace environment variables.
4971 * 2. Replace any other wildcards, remove backslashes.
4972 */
4973 for (n = 1; n <= 2; ++n)
4974 {
4975 if (n == 2)
4976 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004977 /*
4978 * Halve the number of backslashes (this is Vi compatible).
4979 * For Unix and OS/2, when wildcards are expanded, this is
4980 * done by ExpandOne() below.
4981 */
Bram Moolenaare7fedb62015-12-31 19:07:19 +01004982#if defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004983 if (!has_wildcards)
4984#endif
4985 backslash_halve(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004986 }
4987
4988 if (has_wildcards)
4989 {
4990 if (n == 1)
4991 {
4992 /*
4993 * First loop: May expand environment variables. This
4994 * can be done much faster with expand_env() than with
4995 * something else (e.g., calling a shell).
4996 * After expanding environment variables, check again
4997 * if there are still wildcards present.
4998 */
4999 if (vim_strchr(eap->arg, '$') != NULL
5000 || vim_strchr(eap->arg, '~') != NULL)
5001 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +00005002 expand_env_esc(eap->arg, NameBuff, MAXPATHL,
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00005003 TRUE, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005004 has_wildcards = mch_has_wildcard(NameBuff);
5005 p = NameBuff;
5006 }
5007 else
5008 p = NULL;
5009 }
5010 else /* n == 2 */
5011 {
5012 expand_T xpc;
Bram Moolenaar94950a92010-12-02 16:01:29 +01005013 int options = WILD_LIST_NOTFOUND|WILD_ADD_SLASH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005014
5015 ExpandInit(&xpc);
5016 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar94950a92010-12-02 16:01:29 +01005017 if (p_wic)
5018 options += WILD_ICASE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005019 p = ExpandOne(&xpc, eap->arg, NULL,
Bram Moolenaar94950a92010-12-02 16:01:29 +01005020 options, WILD_EXPAND_FREE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005021 if (p == NULL)
5022 return FAIL;
5023 }
5024 if (p != NULL)
5025 {
5026 (void)repl_cmdline(eap, eap->arg, (int)STRLEN(eap->arg),
5027 p, cmdlinep);
5028 if (n == 2) /* p came from ExpandOne() */
5029 vim_free(p);
5030 }
5031 }
5032 }
5033 }
5034 return OK;
5035}
5036
5037/*
5038 * Replace part of the command line, keeping eap->cmd, eap->arg and
5039 * eap->nextcmd correct.
5040 * "src" points to the part that is to be replaced, of length "srclen".
5041 * "repl" is the replacement string.
5042 * Returns a pointer to the character after the replaced string.
5043 * Returns NULL for failure.
5044 */
5045 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005046repl_cmdline(
5047 exarg_T *eap,
5048 char_u *src,
5049 int srclen,
5050 char_u *repl,
5051 char_u **cmdlinep)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005052{
5053 int len;
5054 int i;
5055 char_u *new_cmdline;
5056
5057 /*
5058 * The new command line is build in new_cmdline[].
5059 * First allocate it.
5060 * Careful: a "+cmd" argument may have been NUL terminated.
5061 */
5062 len = (int)STRLEN(repl);
5063 i = (int)(src - *cmdlinep) + (int)STRLEN(src + srclen) + len + 3;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005064 if (eap->nextcmd != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005065 i += (int)STRLEN(eap->nextcmd);/* add space for next command */
Bram Moolenaar964b3742019-05-24 18:54:09 +02005066 if ((new_cmdline = alloc(i)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005067 return NULL; /* out of memory! */
5068
5069 /*
5070 * Copy the stuff before the expanded part.
5071 * Copy the expanded stuff.
5072 * Copy what came after the expanded part.
5073 * Copy the next commands, if there are any.
5074 */
5075 i = (int)(src - *cmdlinep); /* length of part before match */
5076 mch_memmove(new_cmdline, *cmdlinep, (size_t)i);
Bram Moolenaara3ffd9c2005-07-21 21:03:15 +00005077
Bram Moolenaar071d4272004-06-13 20:20:40 +00005078 mch_memmove(new_cmdline + i, repl, (size_t)len);
5079 i += len; /* remember the end of the string */
5080 STRCPY(new_cmdline + i, src + srclen);
5081 src = new_cmdline + i; /* remember where to continue */
5082
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005083 if (eap->nextcmd != NULL) /* append next command */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005084 {
5085 i = (int)STRLEN(new_cmdline) + 1;
5086 STRCPY(new_cmdline + i, eap->nextcmd);
5087 eap->nextcmd = new_cmdline + i;
5088 }
5089 eap->cmd = new_cmdline + (eap->cmd - *cmdlinep);
5090 eap->arg = new_cmdline + (eap->arg - *cmdlinep);
5091 if (eap->do_ecmd_cmd != NULL && eap->do_ecmd_cmd != dollar_command)
5092 eap->do_ecmd_cmd = new_cmdline + (eap->do_ecmd_cmd - *cmdlinep);
5093 vim_free(*cmdlinep);
5094 *cmdlinep = new_cmdline;
5095
5096 return src;
5097}
5098
5099/*
5100 * Check for '|' to separate commands and '"' to start comments.
5101 */
5102 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005103separate_nextcmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005104{
5105 char_u *p;
5106
Bram Moolenaar86b68352004-12-27 21:59:20 +00005107#ifdef FEAT_QUICKFIX
Bram Moolenaar748bf032005-02-02 23:04:36 +00005108 p = skip_grep_pat(eap);
5109#else
5110 p = eap->arg;
Bram Moolenaar86b68352004-12-27 21:59:20 +00005111#endif
5112
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005113 for ( ; *p; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005114 {
5115 if (*p == Ctrl_V)
5116 {
Bram Moolenaar8071cb22019-07-12 17:58:01 +02005117 if (eap->argt & (EX_CTRLV | EX_XFILE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005118 ++p; /* skip CTRL-V and next char */
5119 else
Bram Moolenaarb0db5692007-08-14 20:54:49 +00005120 /* remove CTRL-V and skip next char */
Bram Moolenaara7241f52008-06-24 20:39:31 +00005121 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005122 if (*p == NUL) /* stop at NUL after CTRL-V */
5123 break;
5124 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005125
5126#ifdef FEAT_EVAL
5127 /* Skip over `=expr` when wildcards are expanded. */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02005128 else if (p[0] == '`' && p[1] == '=' && (eap->argt & EX_XFILE))
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005129 {
5130 p += 2;
5131 (void)skip_expr(&p);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005132 }
5133#endif
5134
Bram Moolenaar071d4272004-06-13 20:20:40 +00005135 /* Check for '"': start of comment or '|': next command */
5136 /* :@" and :*" do not start a comment!
5137 * :redir @" doesn't either. */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02005138 else if ((*p == '"' && !(eap->argt & EX_NOTRLCOM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005139 && ((eap->cmdidx != CMD_at && eap->cmdidx != CMD_star)
5140 || p != eap->arg)
5141 && (eap->cmdidx != CMD_redir
5142 || p != eap->arg + 1 || p[-1] != '@'))
5143 || *p == '|' || *p == '\n')
5144 {
5145 /*
Bram Moolenaar8071cb22019-07-12 17:58:01 +02005146 * We remove the '\' before the '|', unless EX_CTRLV is used
Bram Moolenaar071d4272004-06-13 20:20:40 +00005147 * AND 'b' is present in 'cpoptions'.
5148 */
5149 if ((vim_strchr(p_cpo, CPO_BAR) == NULL
Bram Moolenaar8071cb22019-07-12 17:58:01 +02005150 || !(eap->argt & EX_CTRLV)) && *(p - 1) == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005151 {
Bram Moolenaara7241f52008-06-24 20:39:31 +00005152 STRMOVE(p - 1, p); /* remove the '\' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005153 --p;
5154 }
5155 else
5156 {
5157 eap->nextcmd = check_nextcmd(p);
5158 *p = NUL;
5159 break;
5160 }
5161 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005162 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00005163
Bram Moolenaar8071cb22019-07-12 17:58:01 +02005164 if (!(eap->argt & EX_NOTRLCOM)) /* remove trailing spaces */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005165 del_trailing_spaces(eap->arg);
5166}
5167
5168/*
5169 * get + command from ex argument
5170 */
5171 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005172getargcmd(char_u **argp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005173{
5174 char_u *arg = *argp;
5175 char_u *command = NULL;
5176
5177 if (*arg == '+') /* +[command] */
5178 {
5179 ++arg;
Bram Moolenaar3e451592014-04-02 14:22:05 +02005180 if (vim_isspace(*arg) || *arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005181 command = dollar_command;
5182 else
5183 {
5184 command = arg;
5185 arg = skip_cmd_arg(command, TRUE);
5186 if (*arg != NUL)
5187 *arg++ = NUL; /* terminate command with NUL */
5188 }
5189
5190 arg = skipwhite(arg); /* skip over spaces */
5191 *argp = arg;
5192 }
5193 return command;
5194}
5195
5196/*
5197 * Find end of "+command" argument. Skip over "\ " and "\\".
5198 */
5199 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005200skip_cmd_arg(
5201 char_u *p,
5202 int rembs) /* TRUE to halve the number of backslashes */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005203{
5204 while (*p && !vim_isspace(*p))
5205 {
5206 if (*p == '\\' && p[1] != NUL)
5207 {
5208 if (rembs)
Bram Moolenaara7241f52008-06-24 20:39:31 +00005209 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005210 else
5211 ++p;
5212 }
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005213 MB_PTR_ADV(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005214 }
5215 return p;
5216}
5217
Bram Moolenaar333b80a2018-04-04 22:57:29 +02005218 int
5219get_bad_opt(char_u *p, exarg_T *eap)
5220{
5221 if (STRICMP(p, "keep") == 0)
5222 eap->bad_char = BAD_KEEP;
5223 else if (STRICMP(p, "drop") == 0)
5224 eap->bad_char = BAD_DROP;
5225 else if (MB_BYTE2LEN(*p) == 1 && p[1] == NUL)
5226 eap->bad_char = *p;
Bram Moolenaar75808492018-06-12 12:39:41 +02005227 else
5228 return FAIL;
5229 return OK;
Bram Moolenaar333b80a2018-04-04 22:57:29 +02005230}
5231
Bram Moolenaar071d4272004-06-13 20:20:40 +00005232/*
5233 * Get "++opt=arg" argument.
5234 * Return FAIL or OK.
5235 */
5236 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005237getargopt(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005238{
5239 char_u *arg = eap->arg + 2;
5240 int *pp = NULL;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02005241 int bad_char_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005242 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005243
5244 /* ":edit ++[no]bin[ary] file" */
5245 if (STRNCMP(arg, "bin", 3) == 0 || STRNCMP(arg, "nobin", 5) == 0)
5246 {
5247 if (*arg == 'n')
5248 {
5249 arg += 2;
5250 eap->force_bin = FORCE_NOBIN;
5251 }
5252 else
5253 eap->force_bin = FORCE_BIN;
5254 if (!checkforcmd(&arg, "binary", 3))
5255 return FAIL;
5256 eap->arg = skipwhite(arg);
5257 return OK;
5258 }
5259
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005260 /* ":read ++edit file" */
5261 if (STRNCMP(arg, "edit", 4) == 0)
5262 {
5263 eap->read_edit = TRUE;
5264 eap->arg = skipwhite(arg + 4);
5265 return OK;
5266 }
5267
Bram Moolenaar071d4272004-06-13 20:20:40 +00005268 if (STRNCMP(arg, "ff", 2) == 0)
5269 {
5270 arg += 2;
5271 pp = &eap->force_ff;
5272 }
5273 else if (STRNCMP(arg, "fileformat", 10) == 0)
5274 {
5275 arg += 10;
5276 pp = &eap->force_ff;
5277 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005278 else if (STRNCMP(arg, "enc", 3) == 0)
5279 {
Bram Moolenaarb38e9ab2011-12-14 14:49:45 +01005280 if (STRNCMP(arg, "encoding", 8) == 0)
5281 arg += 8;
5282 else
5283 arg += 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005284 pp = &eap->force_enc;
5285 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005286 else if (STRNCMP(arg, "bad", 3) == 0)
5287 {
5288 arg += 3;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02005289 pp = &bad_char_idx;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005290 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005291
5292 if (pp == NULL || *arg != '=')
5293 return FAIL;
5294
5295 ++arg;
5296 *pp = (int)(arg - eap->cmd);
5297 arg = skip_cmd_arg(arg, FALSE);
5298 eap->arg = skipwhite(arg);
5299 *arg = NUL;
5300
Bram Moolenaar071d4272004-06-13 20:20:40 +00005301 if (pp == &eap->force_ff)
5302 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005303 if (check_ff_value(eap->cmd + eap->force_ff) == FAIL)
5304 return FAIL;
Bram Moolenaar333b80a2018-04-04 22:57:29 +02005305 eap->force_ff = eap->cmd[eap->force_ff];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005306 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005307 else if (pp == &eap->force_enc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005308 {
5309 /* Make 'fileencoding' lower case. */
5310 for (p = eap->cmd + eap->force_enc; *p != NUL; ++p)
5311 *p = TOLOWER_ASC(*p);
5312 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005313 else
5314 {
5315 /* Check ++bad= argument. Must be a single-byte character, "keep" or
5316 * "drop". */
Bram Moolenaar333b80a2018-04-04 22:57:29 +02005317 if (get_bad_opt(eap->cmd + bad_char_idx, eap) == FAIL)
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005318 return FAIL;
5319 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005320
5321 return OK;
5322}
5323
Bram Moolenaar071d4272004-06-13 20:20:40 +00005324 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005325ex_autocmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005326{
5327 /*
Bram Moolenaar26d4b892018-07-04 22:26:28 +02005328 * Disallow autocommands from .exrc and .vimrc in current
Bram Moolenaar071d4272004-06-13 20:20:40 +00005329 * directory for security reasons.
5330 */
5331 if (secure)
5332 {
5333 secure = 2;
5334 eap->errmsg = e_curdir;
5335 }
5336 else if (eap->cmdidx == CMD_autocmd)
5337 do_autocmd(eap->arg, eap->forceit);
5338 else
5339 do_augroup(eap->arg, eap->forceit);
5340}
5341
5342/*
5343 * ":doautocmd": Apply the automatic commands to the current buffer.
5344 */
5345 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005346ex_doautocmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005347{
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005348 char_u *arg = eap->arg;
5349 int call_do_modelines = check_nomodeline(&arg);
Bram Moolenaar1610d052016-06-09 22:53:01 +02005350 int did_aucmd;
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005351
Bram Moolenaar1610d052016-06-09 22:53:01 +02005352 (void)do_doautocmd(arg, TRUE, &did_aucmd);
5353 /* Only when there is no <nomodeline>. */
5354 if (call_do_modelines && did_aucmd)
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005355 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005356}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005357
Bram Moolenaar071d4272004-06-13 20:20:40 +00005358/*
5359 * :[N]bunload[!] [N] [bufname] unload buffer
5360 * :[N]bdelete[!] [N] [bufname] delete buffer from buffer list
5361 * :[N]bwipeout[!] [N] [bufname] delete buffer really
5362 */
5363 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005364ex_bunload(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005365{
Bram Moolenaar8cdbd5b2019-06-16 15:50:45 +02005366 if (ERROR_IF_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02005367 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005368 eap->errmsg = do_bufdel(
5369 eap->cmdidx == CMD_bdelete ? DOBUF_DEL
5370 : eap->cmdidx == CMD_bwipeout ? DOBUF_WIPE
5371 : DOBUF_UNLOAD, eap->arg,
5372 eap->addr_count, (int)eap->line1, (int)eap->line2, eap->forceit);
5373}
5374
5375/*
5376 * :[N]buffer [N] to buffer N
5377 * :[N]sbuffer [N] to buffer N
5378 */
5379 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005380ex_buffer(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005381{
Bram Moolenaar8cdbd5b2019-06-16 15:50:45 +02005382 if (ERROR_IF_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02005383 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005384 if (*eap->arg)
5385 eap->errmsg = e_trailing;
5386 else
5387 {
5388 if (eap->addr_count == 0) /* default is current buffer */
5389 goto_buffer(eap, DOBUF_CURRENT, FORWARD, 0);
5390 else
5391 goto_buffer(eap, DOBUF_FIRST, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005392 if (eap->do_ecmd_cmd != NULL)
5393 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005394 }
5395}
5396
5397/*
5398 * :[N]bmodified [N] to next mod. buffer
5399 * :[N]sbmodified [N] to next mod. buffer
5400 */
5401 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005402ex_bmodified(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005403{
5404 goto_buffer(eap, DOBUF_MOD, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005405 if (eap->do_ecmd_cmd != NULL)
5406 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005407}
5408
5409/*
5410 * :[N]bnext [N] to next buffer
5411 * :[N]sbnext [N] split and to next buffer
5412 */
5413 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005414ex_bnext(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005415{
5416 goto_buffer(eap, DOBUF_CURRENT, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005417 if (eap->do_ecmd_cmd != NULL)
5418 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005419}
5420
5421/*
5422 * :[N]bNext [N] to previous buffer
5423 * :[N]bprevious [N] to previous buffer
5424 * :[N]sbNext [N] split and to previous buffer
5425 * :[N]sbprevious [N] split and to previous buffer
5426 */
5427 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005428ex_bprevious(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005429{
5430 goto_buffer(eap, DOBUF_CURRENT, BACKWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005431 if (eap->do_ecmd_cmd != NULL)
5432 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005433}
5434
5435/*
5436 * :brewind to first buffer
5437 * :bfirst to first buffer
5438 * :sbrewind split and to first buffer
5439 * :sbfirst split and to first buffer
5440 */
5441 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005442ex_brewind(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005443{
5444 goto_buffer(eap, DOBUF_FIRST, FORWARD, 0);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005445 if (eap->do_ecmd_cmd != NULL)
5446 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005447}
5448
5449/*
5450 * :blast to last buffer
5451 * :sblast split and to last buffer
5452 */
5453 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005454ex_blast(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005455{
5456 goto_buffer(eap, DOBUF_LAST, BACKWARD, 0);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005457 if (eap->do_ecmd_cmd != NULL)
5458 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005459}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005460
5461 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005462ends_excmd(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005463{
5464 return (c == NUL || c == '|' || c == '"' || c == '\n');
5465}
5466
5467#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA) || defined(FEAT_EVAL) \
5468 || defined(PROTO)
5469/*
5470 * Return the next command, after the first '|' or '\n'.
5471 * Return NULL if not found.
5472 */
5473 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005474find_nextcmd(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005475{
5476 while (*p != '|' && *p != '\n')
5477 {
5478 if (*p == NUL)
5479 return NULL;
5480 ++p;
5481 }
5482 return (p + 1);
5483}
5484#endif
5485
5486/*
Bram Moolenaar2256c992016-11-15 21:17:07 +01005487 * Check if *p is a separator between Ex commands, skipping over white space.
5488 * Return NULL if it isn't, the following character if it is.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005489 */
5490 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005491check_nextcmd(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005492{
Bram Moolenaar2256c992016-11-15 21:17:07 +01005493 char_u *s = skipwhite(p);
5494
5495 if (*s == '|' || *s == '\n')
5496 return (s + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005497 else
5498 return NULL;
5499}
5500
5501/*
5502 * - if there are more files to edit
5503 * - and this is the last window
5504 * - and forceit not used
5505 * - and not repeated twice on a row
5506 * return FAIL and give error message if 'message' TRUE
5507 * return OK otherwise
5508 */
5509 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005510check_more(
5511 int message, /* when FALSE check only, no messages */
5512 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005513{
5514 int n = ARGCOUNT - curwin->w_arg_idx - 1;
5515
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005516 if (!forceit && only_one_window()
5517 && ARGCOUNT > 1 && !arg_had_last && n >= 0 && quitmore == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005518 {
5519 if (message)
5520 {
5521#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
5522 if ((p_confirm || cmdmod.confirm) && curbuf->b_fname != NULL)
5523 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02005524 char_u buff[DIALOG_MSG_SIZE];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005525
Bram Moolenaarda6e8912018-08-21 15:12:14 +02005526 vim_snprintf((char *)buff, DIALOG_MSG_SIZE,
5527 NGETTEXT("%d more file to edit. Quit anyway?",
5528 "%d more files to edit. Quit anyway?", n), n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005529 if (vim_dialog_yesno(VIM_QUESTION, NULL, buff, 1) == VIM_YES)
5530 return OK;
5531 return FAIL;
5532 }
5533#endif
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01005534 semsg(NGETTEXT("E173: %d more file to edit",
5535 "E173: %d more files to edit", n), n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005536 quitmore = 2; /* next try to quit is allowed */
5537 }
5538 return FAIL;
5539 }
5540 return OK;
5541}
5542
Bram Moolenaar071d4272004-06-13 20:20:40 +00005543/*
5544 * Function given to ExpandGeneric() to obtain the list of command names.
5545 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005546 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005547get_command_name(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005548{
5549 if (idx >= (int)CMD_SIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005550 return get_user_command_name(idx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005551 return cmdnames[idx].cmd_name;
5552}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005553
Bram Moolenaar071d4272004-06-13 20:20:40 +00005554 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005555ex_colorscheme(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005556{
Bram Moolenaare6850792010-05-14 15:28:44 +02005557 if (*eap->arg == NUL)
5558 {
5559#ifdef FEAT_EVAL
5560 char_u *expr = vim_strsave((char_u *)"g:colors_name");
5561 char_u *p = NULL;
5562
5563 if (expr != NULL)
5564 {
5565 ++emsg_off;
5566 p = eval_to_string(expr, NULL, FALSE);
5567 --emsg_off;
5568 vim_free(expr);
5569 }
5570 if (p != NULL)
5571 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01005572 msg((char *)p);
Bram Moolenaare6850792010-05-14 15:28:44 +02005573 vim_free(p);
5574 }
5575 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01005576 msg("default");
Bram Moolenaare6850792010-05-14 15:28:44 +02005577#else
Bram Moolenaar32526b32019-01-19 17:43:09 +01005578 msg(_("unknown"));
Bram Moolenaare6850792010-05-14 15:28:44 +02005579#endif
5580 }
5581 else if (load_colors(eap->arg) == FAIL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005582 semsg(_("E185: Cannot find color scheme '%s'"), eap->arg);
Bram Moolenaarf58d81a2019-01-28 20:19:05 +01005583
5584#ifdef FEAT_VTP
5585 else if (has_vtp_working())
5586 {
5587 // background color change requires clear + redraw
5588 update_screen(CLEAR);
5589 redrawcmd();
5590 }
5591#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005592}
5593
5594 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005595ex_highlight(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005596{
5597 if (*eap->arg == NUL && eap->cmd[2] == '!')
Bram Moolenaar32526b32019-01-19 17:43:09 +01005598 msg(_("Greetings, Vim user!"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005599 do_highlight(eap->arg, eap->forceit, FALSE);
5600}
5601
5602
5603/*
5604 * Call this function if we thought we were going to exit, but we won't
5605 * (because of an error). May need to restore the terminal mode.
5606 */
5607 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005608not_exiting(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005609{
5610 exiting = FALSE;
5611 settmode(TMODE_RAW);
5612}
5613
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005614 static int
5615before_quit_autocmds(win_T *wp, int quit_all, int forceit)
5616{
5617 apply_autocmds(EVENT_QUITPRE, NULL, NULL, FALSE, wp->w_buffer);
5618
5619 /* Bail out when autocommands closed the window.
5620 * Refuse to quit when the buffer in the last window is being closed (can
5621 * only happen in autocommands). */
5622 if (!win_valid(wp)
5623 || curbuf_locked()
5624 || (wp->w_buffer->b_nwindows == 1 && wp->w_buffer->b_locked > 0))
5625 return TRUE;
5626
5627 if (quit_all || (check_more(FALSE, forceit) == OK && only_one_window()))
5628 {
5629 apply_autocmds(EVENT_EXITPRE, NULL, NULL, FALSE, curbuf);
5630 /* Refuse to quit when locked or when the buffer in the last window is
5631 * being closed (can only happen in autocommands). */
5632 if (curbuf_locked()
5633 || (curbuf->b_nwindows == 1 && curbuf->b_locked > 0))
5634 return TRUE;
5635 }
5636
5637 return FALSE;
5638}
5639
Bram Moolenaar071d4272004-06-13 20:20:40 +00005640/*
Bram Moolenaarf240e182014-11-27 18:33:02 +01005641 * ":quit": quit current window, quit Vim if the last window is closed.
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005642 * ":{nr}quit": quit window {nr}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005643 */
5644 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005645ex_quit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005646{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005647 win_T *wp;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005648
Bram Moolenaar071d4272004-06-13 20:20:40 +00005649#ifdef FEAT_CMDWIN
5650 if (cmdwin_type != 0)
5651 {
5652 cmdwin_result = Ctrl_C;
5653 return;
5654 }
5655#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005656 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005657 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005658 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005659 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005660 return;
5661 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005662 if (eap->addr_count > 0)
5663 {
Bram Moolenaarf240e182014-11-27 18:33:02 +01005664 int wnr = eap->line2;
5665
5666 for (wp = firstwin; wp->w_next != NULL; wp = wp->w_next)
5667 if (--wnr <= 0)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005668 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005669 }
5670 else
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005671 wp = curwin;
Bram Moolenaarf240e182014-11-27 18:33:02 +01005672
Bram Moolenaar87ffb5c2017-10-19 12:37:42 +02005673 /* Refuse to quit when locked. */
5674 if (curbuf_locked())
5675 return;
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005676
5677 /* Trigger QuitPre and maybe ExitPre */
5678 if (before_quit_autocmds(wp, FALSE, eap->forceit))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005679 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005680
5681#ifdef FEAT_NETBEANS_INTG
5682 netbeansForcedQuit = eap->forceit;
5683#endif
5684
5685 /*
5686 * If there are more files or windows we won't exit.
5687 */
5688 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
5689 exiting = TRUE;
Bram Moolenaarff930ca2017-10-19 17:12:10 +02005690 if ((!buf_hide(wp->w_buffer)
5691 && check_changed(wp->w_buffer, (p_awa ? CCGD_AW : 0)
Bram Moolenaar45d3b142013-11-09 03:31:51 +01005692 | (eap->forceit ? CCGD_FORCEIT : 0)
5693 | CCGD_EXCMD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005694 || check_more(TRUE, eap->forceit) == FAIL
Bram Moolenaar027387f2016-01-02 22:25:52 +01005695 || (only_one_window() && check_changed_any(eap->forceit, TRUE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005696 {
5697 not_exiting();
5698 }
5699 else
5700 {
Bram Moolenaarc7a0d322015-06-19 12:43:07 +02005701 /* quit last window
5702 * Note: only_one_window() returns true, even so a help window is
5703 * still open. In that case only quit, if no address has been
5704 * specified. Example:
5705 * :h|wincmd w|1q - don't quit
5706 * :h|wincmd w|q - quit
5707 */
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01005708 if (only_one_window() && (ONE_WINDOW || eap->addr_count == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005709 getout(0);
Bram Moolenaar2c33d7b2017-10-14 16:06:20 +02005710 not_exiting();
Bram Moolenaar4033c552017-09-16 20:54:51 +02005711#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00005712 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005713#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005714 /* close window; may free buffer */
Bram Moolenaareb44a682017-08-03 22:44:55 +02005715 win_close(wp, !buf_hide(wp->w_buffer) || eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005716 }
5717}
5718
5719/*
5720 * ":cquit".
5721 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005722 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005723ex_cquit(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005724{
5725 getout(1); /* this does not always pass on the exit code to the Manx
5726 compiler. why? */
5727}
5728
5729/*
5730 * ":qall": try to quit all windows
5731 */
5732 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005733ex_quit_all(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005734{
5735# ifdef FEAT_CMDWIN
5736 if (cmdwin_type != 0)
5737 {
5738 if (eap->forceit)
5739 cmdwin_result = K_XF1; /* ex_window() takes care of this */
5740 else
5741 cmdwin_result = K_XF2;
5742 return;
5743 }
5744# endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005745
5746 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005747 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005748 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005749 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005750 return;
5751 }
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005752
5753 if (before_quit_autocmds(curwin, TRUE, eap->forceit))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005754 return;
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005755
Bram Moolenaar071d4272004-06-13 20:20:40 +00005756 exiting = TRUE;
Bram Moolenaar027387f2016-01-02 22:25:52 +01005757 if (eap->forceit || !check_changed_any(FALSE, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005758 getout(0);
5759 not_exiting();
5760}
5761
Bram Moolenaar071d4272004-06-13 20:20:40 +00005762/*
5763 * ":close": close current window, unless it is the last one
5764 */
5765 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005766ex_close(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005767{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005768 win_T *win;
5769 int winnr = 0;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005770#ifdef FEAT_CMDWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00005771 if (cmdwin_type != 0)
Bram Moolenaar9bd1a7e2011-05-19 14:50:54 +02005772 cmdwin_result = Ctrl_C;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005773 else
Bram Moolenaar4033c552017-09-16 20:54:51 +02005774#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005775 if (!text_locked() && !curbuf_locked())
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005776 {
5777 if (eap->addr_count == 0)
5778 ex_win_close(eap->forceit, curwin, NULL);
Bram Moolenaar6d41c782018-06-06 09:11:12 +02005779 else
5780 {
Bram Moolenaar29323592016-07-24 22:04:11 +02005781 FOR_ALL_WINDOWS(win)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005782 {
5783 winnr++;
5784 if (winnr == eap->line2)
5785 break;
5786 }
5787 if (win == NULL)
5788 win = lastwin;
5789 ex_win_close(eap->forceit, win, NULL);
5790 }
5791 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005792}
5793
Bram Moolenaar4033c552017-09-16 20:54:51 +02005794#ifdef FEAT_QUICKFIX
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005795/*
5796 * ":pclose": Close any preview window.
5797 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005798 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005799ex_pclose(exarg_T *eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005800{
5801 win_T *win;
5802
Bram Moolenaar79648732019-07-18 21:43:07 +02005803 // First close any normal window.
Bram Moolenaar29323592016-07-24 22:04:11 +02005804 FOR_ALL_WINDOWS(win)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005805 if (win->w_p_pvw)
5806 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00005807 ex_win_close(eap->forceit, win, NULL);
Bram Moolenaar79648732019-07-18 21:43:07 +02005808 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005809 }
Bram Moolenaar79648732019-07-18 21:43:07 +02005810# ifdef FEAT_TEXT_PROP
5811 // Also when 'previewpopup' is empty, it might have been cleared.
Bram Moolenaarc7c5f102019-08-21 18:31:03 +02005812 popup_close_preview();
Bram Moolenaar79648732019-07-18 21:43:07 +02005813# endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005814}
Bram Moolenaar4033c552017-09-16 20:54:51 +02005815#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005816
Bram Moolenaarf740b292006-02-16 22:11:02 +00005817/*
5818 * Close window "win" and take care of handling closing the last window for a
5819 * modified buffer.
5820 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005821 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005822ex_win_close(
5823 int forceit,
5824 win_T *win,
5825 tabpage_T *tp) /* NULL or the tab page "win" is in */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005826{
5827 int need_hide;
5828 buf_T *buf = win->w_buffer;
5829
5830 need_hide = (bufIsChanged(buf) && buf->b_nwindows <= 1);
Bram Moolenaareb44a682017-08-03 22:44:55 +02005831 if (need_hide && !buf_hide(buf) && !forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005832 {
Bram Moolenaar4033c552017-09-16 20:54:51 +02005833#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005834 if ((p_confirm || cmdmod.confirm) && p_write)
5835 {
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005836 bufref_T bufref;
5837
5838 set_bufref(&bufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005839 dialog_changed(buf, FALSE);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005840 if (bufref_valid(&bufref) && bufIsChanged(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005841 return;
5842 need_hide = FALSE;
5843 }
5844 else
Bram Moolenaar4033c552017-09-16 20:54:51 +02005845#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005846 {
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02005847 no_write_message();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005848 return;
5849 }
5850 }
5851
Bram Moolenaar4033c552017-09-16 20:54:51 +02005852#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00005853 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005854#endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00005855
Bram Moolenaar071d4272004-06-13 20:20:40 +00005856 /* free buffer when not hiding it or when it's a scratch buffer */
Bram Moolenaarf740b292006-02-16 22:11:02 +00005857 if (tp == NULL)
Bram Moolenaareb44a682017-08-03 22:44:55 +02005858 win_close(win, !need_hide && !buf_hide(buf));
Bram Moolenaarf740b292006-02-16 22:11:02 +00005859 else
Bram Moolenaareb44a682017-08-03 22:44:55 +02005860 win_close_othertab(win, !need_hide && !buf_hide(buf), tp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005861}
5862
Bram Moolenaar071d4272004-06-13 20:20:40 +00005863/*
Bram Moolenaardea25702017-01-29 15:18:10 +01005864 * Handle the argument for a tabpage related ex command.
5865 * Returns a tabpage number.
5866 * When an error is encountered then eap->errmsg is set.
5867 */
5868 static int
5869get_tabpage_arg(exarg_T *eap)
5870{
5871 int tab_number;
5872 int unaccept_arg0 = (eap->cmdidx == CMD_tabmove) ? 0 : 1;
5873
5874 if (eap->arg && *eap->arg != NUL)
5875 {
5876 char_u *p = eap->arg;
5877 char_u *p_save;
5878 int relative = 0; /* argument +N/-N means: go to N places to the
5879 * right/left relative to the current position. */
5880
5881 if (*p == '-')
5882 {
5883 relative = -1;
5884 p++;
5885 }
5886 else if (*p == '+')
5887 {
5888 relative = 1;
5889 p++;
5890 }
5891
5892 p_save = p;
5893 tab_number = getdigits(&p);
5894
5895 if (relative == 0)
5896 {
5897 if (STRCMP(p, "$") == 0)
5898 tab_number = LAST_TAB_NR;
5899 else if (p == p_save || *p_save == '-' || *p != NUL
5900 || tab_number > LAST_TAB_NR)
5901 {
5902 /* No numbers as argument. */
5903 eap->errmsg = e_invarg;
5904 goto theend;
5905 }
5906 }
5907 else
5908 {
5909 if (*p_save == NUL)
5910 tab_number = 1;
5911 else if (p == p_save || *p_save == '-' || *p != NUL
5912 || tab_number == 0)
5913 {
5914 /* No numbers as argument. */
5915 eap->errmsg = e_invarg;
5916 goto theend;
5917 }
5918 tab_number = tab_number * relative + tabpage_index(curtab);
5919 if (!unaccept_arg0 && relative == -1)
5920 --tab_number;
5921 }
5922 if (tab_number < unaccept_arg0 || tab_number > LAST_TAB_NR)
5923 eap->errmsg = e_invarg;
5924 }
5925 else if (eap->addr_count > 0)
5926 {
5927 if (unaccept_arg0 && eap->line2 == 0)
Bram Moolenaarc6251552017-01-29 21:42:20 +01005928 {
Bram Moolenaardea25702017-01-29 15:18:10 +01005929 eap->errmsg = e_invrange;
Bram Moolenaarc6251552017-01-29 21:42:20 +01005930 tab_number = 0;
5931 }
Bram Moolenaardea25702017-01-29 15:18:10 +01005932 else
5933 {
5934 tab_number = eap->line2;
Bram Moolenaar82a12462019-01-22 22:41:42 +01005935 if (!unaccept_arg0 && *skipwhite(*eap->cmdlinep) == '-')
Bram Moolenaardea25702017-01-29 15:18:10 +01005936 {
5937 --tab_number;
5938 if (tab_number < unaccept_arg0)
5939 eap->errmsg = e_invarg;
5940 }
5941 }
5942 }
5943 else
5944 {
5945 switch (eap->cmdidx)
5946 {
5947 case CMD_tabnext:
5948 tab_number = tabpage_index(curtab) + 1;
5949 if (tab_number > LAST_TAB_NR)
5950 tab_number = 1;
5951 break;
5952 case CMD_tabmove:
5953 tab_number = LAST_TAB_NR;
5954 break;
5955 default:
5956 tab_number = tabpage_index(curtab);
5957 }
5958 }
5959
5960theend:
5961 return tab_number;
5962}
5963
5964/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00005965 * ":tabclose": close current tab page, unless it is the last one.
5966 * ":tabclose N": close tab page N.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005967 */
5968 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005969ex_tabclose(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005970{
Bram Moolenaarf740b292006-02-16 22:11:02 +00005971 tabpage_T *tp;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005972 int tab_number;
Bram Moolenaarf740b292006-02-16 22:11:02 +00005973
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005974# ifdef FEAT_CMDWIN
5975 if (cmdwin_type != 0)
5976 cmdwin_result = K_IGNORE;
5977 else
5978# endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00005979 if (first_tabpage->tp_next == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005980 emsg(_("E784: Cannot close last tab page"));
Bram Moolenaarf740b292006-02-16 22:11:02 +00005981 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005982 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005983 tab_number = get_tabpage_arg(eap);
5984 if (eap->errmsg == NULL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005985 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005986 tp = find_tabpage(tab_number);
Bram Moolenaarf740b292006-02-16 22:11:02 +00005987 if (tp == NULL)
5988 {
5989 beep_flush();
5990 return;
5991 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005992 if (tp != curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00005993 {
5994 tabpage_close_other(tp, eap->forceit);
5995 return;
5996 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005997 else if (!text_locked() && !curbuf_locked())
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005998 tabpage_close(eap->forceit);
5999 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006000 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006001}
6002
6003/*
6004 * ":tabonly": close all tab pages except the current one
6005 */
6006 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006007ex_tabonly(exarg_T *eap)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006008{
6009 tabpage_T *tp;
6010 int done;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006011 int tab_number;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006012
6013# ifdef FEAT_CMDWIN
6014 if (cmdwin_type != 0)
6015 cmdwin_result = K_IGNORE;
6016 else
6017# endif
6018 if (first_tabpage->tp_next == NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01006019 msg(_("Already only one tab page"));
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006020 else
6021 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006022 tab_number = get_tabpage_arg(eap);
6023 if (eap->errmsg == NULL)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006024 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006025 goto_tabpage(tab_number);
6026 /* Repeat this up to a 1000 times, because autocommands may
6027 * mess up the lists. */
6028 for (done = 0; done < 1000; ++done)
6029 {
6030 FOR_ALL_TABPAGES(tp)
6031 if (tp->tp_topframe != topframe)
6032 {
6033 tabpage_close_other(tp, eap->forceit);
6034 /* if we failed to close it quit */
6035 if (valid_tabpage(tp))
6036 done = 1000;
6037 /* start over, "tp" is now invalid */
6038 break;
6039 }
6040 if (first_tabpage->tp_next == NULL)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006041 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006042 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006043 }
6044 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006045}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006046
6047/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00006048 * Close the current tab page.
6049 */
6050 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006051tabpage_close(int forceit)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006052{
6053 /* First close all the windows but the current one. If that worked then
6054 * close the last window in this tab, that will close it. */
Bram Moolenaar459ca562016-11-10 18:16:33 +01006055 if (!ONE_WINDOW)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006056 close_others(TRUE, forceit);
Bram Moolenaar459ca562016-11-10 18:16:33 +01006057 if (ONE_WINDOW)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006058 ex_win_close(forceit, curwin, NULL);
6059# ifdef FEAT_GUI
6060 need_mouse_correct = TRUE;
6061# endif
6062}
6063
6064/*
6065 * Close tab page "tp", which is not the current tab page.
6066 * Note that autocommands may make "tp" invalid.
Bram Moolenaar7875acc2006-09-10 13:51:17 +00006067 * Also takes care of the tab pages line disappearing when closing the
6068 * last-but-one tab page.
Bram Moolenaarf740b292006-02-16 22:11:02 +00006069 */
6070 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006071tabpage_close_other(tabpage_T *tp, int forceit)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006072{
6073 int done = 0;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006074 win_T *wp;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00006075 int h = tabline_height();
Bram Moolenaarf740b292006-02-16 22:11:02 +00006076
6077 /* Limit to 1000 windows, autocommands may add a window while we close
6078 * one. OK, so I'm paranoid... */
6079 while (++done < 1000)
6080 {
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006081 wp = tp->tp_firstwin;
6082 ex_win_close(forceit, wp, tp);
Bram Moolenaarf740b292006-02-16 22:11:02 +00006083
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006084 /* Autocommands may delete the tab page under our fingers and we may
6085 * fail to close a window with a modified buffer. */
6086 if (!valid_tabpage(tp) || tp->tp_firstwin == wp)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006087 break;
6088 }
Bram Moolenaar7875acc2006-09-10 13:51:17 +00006089
Bram Moolenaar29323592016-07-24 22:04:11 +02006090 apply_autocmds(EVENT_TABCLOSED, NULL, NULL, FALSE, curbuf);
Bram Moolenaar29323592016-07-24 22:04:11 +02006091
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006092 redraw_tabline = TRUE;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00006093 if (h != tabline_height())
6094 shell_new_rows();
Bram Moolenaarf740b292006-02-16 22:11:02 +00006095}
6096
6097/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006098 * ":only".
6099 */
6100 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006101ex_only(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006102{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01006103 win_T *wp;
6104 int wnr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006105# ifdef FEAT_GUI
6106 need_mouse_correct = TRUE;
6107# endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01006108 if (eap->addr_count > 0)
6109 {
6110 wnr = eap->line2;
6111 for (wp = firstwin; --wnr > 0; )
6112 {
6113 if (wp->w_next == NULL)
6114 break;
6115 else
6116 wp = wp->w_next;
6117 }
6118 win_goto(wp);
6119 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006120 close_others(TRUE, eap->forceit);
6121}
6122
Bram Moolenaar071d4272004-06-13 20:20:40 +00006123 static void
Bram Moolenaar5e1e6d22017-01-02 17:26:00 +01006124ex_hide(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006125{
Bram Moolenaar2256c992016-11-15 21:17:07 +01006126 /* ":hide" or ":hide | cmd": hide current window */
Bram Moolenaar2256c992016-11-15 21:17:07 +01006127 if (!eap->skip)
6128 {
Bram Moolenaar4033c552017-09-16 20:54:51 +02006129#ifdef FEAT_GUI
Bram Moolenaar2256c992016-11-15 21:17:07 +01006130 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006131#endif
Bram Moolenaar2256c992016-11-15 21:17:07 +01006132 if (eap->addr_count == 0)
6133 win_close(curwin, FALSE); /* don't free buffer */
6134 else
6135 {
6136 int winnr = 0;
6137 win_T *win;
Bram Moolenaarf240e182014-11-27 18:33:02 +01006138
Bram Moolenaar2256c992016-11-15 21:17:07 +01006139 FOR_ALL_WINDOWS(win)
6140 {
6141 winnr++;
6142 if (winnr == eap->line2)
6143 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01006144 }
Bram Moolenaar2256c992016-11-15 21:17:07 +01006145 if (win == NULL)
6146 win = lastwin;
6147 win_close(win, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006148 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006149 }
6150}
6151
6152/*
6153 * ":stop" and ":suspend": Suspend Vim.
6154 */
6155 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006156ex_stop(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006157{
6158 /*
6159 * Disallow suspending for "rvim".
6160 */
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006161 if (!check_restricted())
Bram Moolenaar071d4272004-06-13 20:20:40 +00006162 {
6163 if (!eap->forceit)
6164 autowrite_all();
6165 windgoto((int)Rows - 1, 0);
6166 out_char('\n');
6167 out_flush();
6168 stoptermcap();
6169 out_flush(); /* needed for SUN to restore xterm buffer */
6170#ifdef FEAT_TITLE
Bram Moolenaar40385db2018-08-07 22:31:44 +02006171 mch_restore_title(SAVE_RESTORE_BOTH); /* restore window titles */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006172#endif
6173 ui_suspend(); /* call machine specific function */
6174#ifdef FEAT_TITLE
6175 maketitle();
6176 resettitle(); /* force updating the title */
6177#endif
6178 starttermcap();
6179 scroll_start(); /* scroll screen before redrawing */
6180 redraw_later_clear();
6181 shell_resized(); /* may have resized window */
6182 }
6183}
6184
6185/*
Bram Moolenaar12a96de2018-03-11 14:44:18 +01006186 * ":exit", ":xit" and ":wq": Write file and quite the current window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006187 */
6188 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006189ex_exit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006190{
6191#ifdef FEAT_CMDWIN
6192 if (cmdwin_type != 0)
6193 {
6194 cmdwin_result = Ctrl_C;
6195 return;
6196 }
6197#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006198 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006199 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006200 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006201 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006202 return;
6203 }
Bram Moolenaar12a96de2018-03-11 14:44:18 +01006204
6205 if (before_quit_autocmds(curwin, FALSE, eap->forceit))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006206 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006207
6208 /*
6209 * if more files or windows we won't exit
6210 */
6211 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
6212 exiting = TRUE;
6213 if ( ((eap->cmdidx == CMD_wq
6214 || curbufIsChanged())
6215 && do_write(eap) == FAIL)
6216 || check_more(TRUE, eap->forceit) == FAIL
Bram Moolenaar027387f2016-01-02 22:25:52 +01006217 || (only_one_window() && check_changed_any(eap->forceit, FALSE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006218 {
6219 not_exiting();
6220 }
6221 else
6222 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006223 if (only_one_window()) /* quit last window, exit Vim */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006224 getout(0);
Bram Moolenaar2c33d7b2017-10-14 16:06:20 +02006225 not_exiting();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006226# ifdef FEAT_GUI
6227 need_mouse_correct = TRUE;
6228# endif
Bram Moolenaar1a4a75c2013-07-28 16:03:06 +02006229 /* Quit current window, may free the buffer. */
Bram Moolenaareb44a682017-08-03 22:44:55 +02006230 win_close(curwin, !buf_hide(curwin->w_buffer));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006231 }
6232}
6233
6234/*
6235 * ":print", ":list", ":number".
6236 */
6237 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006238ex_print(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006239{
Bram Moolenaardf177f62005-02-22 08:39:57 +00006240 if (curbuf->b_ml.ml_flags & ML_EMPTY)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006241 emsg(_(e_emptybuf));
Bram Moolenaardf177f62005-02-22 08:39:57 +00006242 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006243 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00006244 for ( ;!got_int; ui_breakcheck())
6245 {
6246 print_line(eap->line1,
6247 (eap->cmdidx == CMD_number || eap->cmdidx == CMD_pound
6248 || (eap->flags & EXFLAG_NR)),
6249 eap->cmdidx == CMD_list || (eap->flags & EXFLAG_LIST));
6250 if (++eap->line1 > eap->line2)
6251 break;
6252 out_flush(); /* show one line at a time */
6253 }
6254 setpcmark();
6255 /* put cursor at last line */
6256 curwin->w_cursor.lnum = eap->line2;
6257 beginline(BL_SOL | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006258 }
6259
Bram Moolenaar071d4272004-06-13 20:20:40 +00006260 ex_no_reprint = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006261}
6262
6263#ifdef FEAT_BYTEOFF
6264 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006265ex_goto(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006266{
6267 goto_byte(eap->line2);
6268}
6269#endif
6270
6271/*
6272 * ":shell".
6273 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006274 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006275ex_shell(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006276{
6277 do_shell(NULL, 0);
6278}
6279
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006280#if defined(HAVE_DROP_FILE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006281
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006282static int drop_busy = FALSE;
6283static int drop_filec;
6284static char_u **drop_filev = NULL;
6285static int drop_split;
6286static void (*drop_callback)(void *);
6287static void *drop_cookie;
6288
6289 static void
6290handle_drop_internal(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006291{
6292 exarg_T ea;
6293 int save_msg_scroll = msg_scroll;
6294
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006295 // Setting the argument list may cause screen updates and being called
6296 // recursively. Avoid that by setting drop_busy.
6297 drop_busy = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006298
6299 /* Check whether the current buffer is changed. If so, we will need
6300 * to split the current window or data could be lost.
6301 * We don't need to check if the 'hidden' option is set, as in this
6302 * case the buffer won't be lost.
6303 */
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006304 if (!buf_hide(curbuf) && !drop_split)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006305 {
6306 ++emsg_off;
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006307 drop_split = check_changed(curbuf, CCGD_AW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006308 --emsg_off;
6309 }
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006310 if (drop_split)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006311 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006312 if (win_split(0, 0) == FAIL)
6313 return;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02006314 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006315
6316 /* When splitting the window, create a new alist. Otherwise the
6317 * existing one is overwritten. */
6318 alist_unlink(curwin->w_alist);
6319 alist_new();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006320 }
6321
6322 /*
6323 * Set up the new argument list.
6324 */
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006325 alist_set(ALIST(curwin), drop_filec, drop_filev, FALSE, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006326
6327 /*
6328 * Move to the first file.
6329 */
6330 /* Fake up a minimal "next" command for do_argfile() */
6331 vim_memset(&ea, 0, sizeof(ea));
6332 ea.cmd = (char_u *)"next";
6333 do_argfile(&ea, 0);
6334
6335 /* do_ecmd() may set need_start_insertmode, but since we never left Insert
6336 * mode that is not needed here. */
6337 need_start_insertmode = FALSE;
6338
6339 /* Restore msg_scroll, otherwise a following command may cause scrolling
6340 * unexpectedly. The screen will be redrawn by the caller, thus
6341 * msg_scroll being set by displaying a message is irrelevant. */
6342 msg_scroll = save_msg_scroll;
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006343
6344 if (drop_callback != NULL)
6345 drop_callback(drop_cookie);
6346
6347 drop_filev = NULL;
6348 drop_busy = FALSE;
6349}
6350
6351/*
6352 * Handle a file drop. The code is here because a drop is *nearly* like an
6353 * :args command, but not quite (we have a list of exact filenames, so we
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01006354 * don't want to (a) parse a command line, or (b) expand wildcards). So the
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006355 * code is very similar to :args and hence needs access to a lot of the static
6356 * functions in this file.
6357 *
6358 * The "filev" list must have been allocated using alloc(), as should each item
6359 * in the list. This function takes over responsibility for freeing the "filev"
6360 * list.
6361 */
6362 void
6363handle_drop(
6364 int filec, // the number of files dropped
6365 char_u **filev, // the list of files dropped
6366 int split, // force splitting the window
6367 void (*callback)(void *), // to be called after setting the argument
6368 // list
6369 void *cookie) // argument for "callback" (allocated)
6370{
6371 // Cannot handle recursive drops, finish the pending one.
6372 if (drop_busy)
6373 {
6374 FreeWild(filec, filev);
6375 vim_free(cookie);
6376 return;
6377 }
6378
6379 // When calling handle_drop() more than once in a row we only use the last
6380 // one.
6381 if (drop_filev != NULL)
6382 {
6383 FreeWild(drop_filec, drop_filev);
6384 vim_free(drop_cookie);
6385 }
6386
6387 drop_filec = filec;
6388 drop_filev = filev;
6389 drop_split = split;
6390 drop_callback = callback;
6391 drop_cookie = cookie;
6392
6393 // Postpone this when:
6394 // - editing the command line
6395 // - not possible to change the current buffer
6396 // - updating the screen
6397 // As it may change buffers and window structures that are in use and cause
6398 // freed memory to be used.
6399 if (text_locked() || curbuf_locked() || updating_screen)
6400 return;
6401
6402 handle_drop_internal();
6403}
6404
6405/*
6406 * To be called when text is unlocked, curbuf is unlocked or updating_screen is
6407 * reset: Handle a postponed drop.
6408 */
6409 void
6410handle_any_postponed_drop(void)
6411{
6412 if (!drop_busy && drop_filev != NULL
6413 && !text_locked() && !curbuf_locked() && !updating_screen)
6414 handle_drop_internal();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006415}
6416#endif
6417
Bram Moolenaar071d4272004-06-13 20:20:40 +00006418/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006419 * ":preserve".
6420 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006421 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006422ex_preserve(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006423{
Bram Moolenaar4399ef42005-02-12 14:29:27 +00006424 curbuf->b_flags |= BF_PRESERVED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006425 ml_preserve(curbuf, TRUE);
6426}
6427
6428/*
6429 * ":recover".
6430 */
6431 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006432ex_recover(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006433{
6434 /* Set recoverymode right away to avoid the ATTENTION prompt. */
6435 recoverymode = TRUE;
Bram Moolenaar45d3b142013-11-09 03:31:51 +01006436 if (!check_changed(curbuf, (p_awa ? CCGD_AW : 0)
6437 | CCGD_MULTWIN
6438 | (eap->forceit ? CCGD_FORCEIT : 0)
6439 | CCGD_EXCMD)
6440
Bram Moolenaar071d4272004-06-13 20:20:40 +00006441 && (*eap->arg == NUL
6442 || setfname(curbuf, eap->arg, NULL, TRUE) == OK))
Bram Moolenaar99499b12019-05-23 21:35:48 +02006443 ml_recover(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006444 recoverymode = FALSE;
6445}
6446
6447/*
6448 * Command modifier used in a wrong way.
6449 */
6450 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006451ex_wrongmodifier(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006452{
6453 eap->errmsg = e_invcmd;
6454}
6455
Bram Moolenaar071d4272004-06-13 20:20:40 +00006456/*
6457 * :sview [+command] file split window with new file, read-only
6458 * :split [[+command] file] split window with current or new file
6459 * :vsplit [[+command] file] split window vertically with current or new file
6460 * :new [[+command] file] split window with no or new file
6461 * :vnew [[+command] file] split vertically window with no or new file
6462 * :sfind [+command] file split window with file in 'path'
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006463 *
6464 * :tabedit open new Tab page with empty window
6465 * :tabedit [+command] file open new Tab page and edit "file"
6466 * :tabnew [[+command] file] just like :tabedit
6467 * :tabfind [+command] file open new Tab page and find "file"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006468 */
6469 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006470ex_splitview(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006471{
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006472 win_T *old_curwin = curwin;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006473#if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006474 char_u *fname = NULL;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006475#endif
6476#ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00006477 int browse_flag = cmdmod.browse;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006478#endif
Bram Moolenaar39902a02018-06-21 22:10:08 +02006479 int use_tab = eap->cmdidx == CMD_tabedit
6480 || eap->cmdidx == CMD_tabfind
6481 || eap->cmdidx == CMD_tabnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006482
Bram Moolenaar8cdbd5b2019-06-16 15:50:45 +02006483 if (ERROR_IF_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02006484 return;
6485
Bram Moolenaar4033c552017-09-16 20:54:51 +02006486#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00006487 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006488#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006489
Bram Moolenaar4033c552017-09-16 20:54:51 +02006490#ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00006491 /* A ":split" in the quickfix window works like ":new". Don't want two
Bram Moolenaar05bb9532008-07-04 09:44:11 +00006492 * quickfix windows. But it's OK when doing ":tab split". */
6493 if (bt_quickfix(curbuf) && cmdmod.tab == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006494 {
6495 if (eap->cmdidx == CMD_split)
6496 eap->cmdidx = CMD_new;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006497 if (eap->cmdidx == CMD_vsplit)
6498 eap->cmdidx = CMD_vnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006499 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02006500#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006501
Bram Moolenaar4033c552017-09-16 20:54:51 +02006502#ifdef FEAT_SEARCHPATH
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006503 if (eap->cmdidx == CMD_sfind || eap->cmdidx == CMD_tabfind)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006504 {
6505 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg),
6506 FNAME_MESS, TRUE, curbuf->b_ffname);
6507 if (fname == NULL)
6508 goto theend;
6509 eap->arg = fname;
6510 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006511# ifdef FEAT_BROWSE
Bram Moolenaar4033c552017-09-16 20:54:51 +02006512 else
6513# endif
6514#endif
6515#ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00006516 if (cmdmod.browse
Bram Moolenaar071d4272004-06-13 20:20:40 +00006517 && eap->cmdidx != CMD_vnew
Bram Moolenaar071d4272004-06-13 20:20:40 +00006518 && eap->cmdidx != CMD_new)
6519 {
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006520 if (
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006521# ifdef FEAT_GUI
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006522 !gui.in_use &&
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006523# endif
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006524 au_has_group((char_u *)"FileExplorer"))
6525 {
6526 /* No browsing supported but we do have the file explorer:
6527 * Edit the directory. */
6528 if (*eap->arg == NUL || !mch_isdir(eap->arg))
6529 eap->arg = (char_u *)".";
6530 }
6531 else
6532 {
Bram Moolenaar39902a02018-06-21 22:10:08 +02006533 fname = do_browse(0, (char_u *)(use_tab
6534 ? _("Edit File in new tab page")
6535 : _("Edit File in new window")),
Bram Moolenaar071d4272004-06-13 20:20:40 +00006536 eap->arg, NULL, NULL, NULL, curbuf);
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006537 if (fname == NULL)
6538 goto theend;
6539 eap->arg = fname;
6540 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006541 }
6542 cmdmod.browse = FALSE; /* Don't browse again in do_ecmd(). */
Bram Moolenaar4033c552017-09-16 20:54:51 +02006543#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006544
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006545 /*
6546 * Either open new tab page or split the window.
6547 */
Bram Moolenaar39902a02018-06-21 22:10:08 +02006548 if (use_tab)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006549 {
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006550 if (win_new_tabpage(cmdmod.tab != 0 ? cmdmod.tab
6551 : eap->addr_count == 0 ? 0
6552 : (int)eap->line2 + 1) != FAIL)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006553 {
Bram Moolenaard2b66012008-01-09 19:30:36 +00006554 do_exedit(eap, old_curwin);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006555
6556 /* set the alternate buffer for the window we came from */
6557 if (curwin != old_curwin
6558 && win_valid(old_curwin)
6559 && old_curwin->w_buffer != curbuf
6560 && !cmdmod.keepalt)
6561 old_curwin->w_alt_fnum = curbuf->b_fnum;
6562 }
6563 }
6564 else if (win_split(eap->addr_count > 0 ? (int)eap->line2 : 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006565 *eap->cmd == 'v' ? WSP_VERT : 0) != FAIL)
6566 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006567 /* Reset 'scrollbind' when editing another file, but keep it when
6568 * doing ":split" without arguments. */
6569 if (*eap->arg != NUL
Bram Moolenaar8a3bb562018-03-04 20:14:14 +01006570# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00006571 || cmdmod.browse
Bram Moolenaar8a3bb562018-03-04 20:14:14 +01006572# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006573 )
Bram Moolenaar3368ea22010-09-21 16:56:35 +02006574 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006575 else
6576 do_check_scrollbind(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006577 do_exedit(eap, old_curwin);
6578 }
6579
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006580# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00006581 cmdmod.browse = browse_flag;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006582# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006583
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006584# if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006585theend:
6586 vim_free(fname);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006587# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006588}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006589
6590/*
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006591 * Open a new tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006592 */
6593 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006594tabpage_new(void)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006595{
6596 exarg_T ea;
6597
6598 vim_memset(&ea, 0, sizeof(ea));
6599 ea.cmdidx = CMD_tabnew;
6600 ea.cmd = (char_u *)"tabn";
6601 ea.arg = (char_u *)"";
6602 ex_splitview(&ea);
6603}
6604
6605/*
6606 * :tabnext command
6607 */
6608 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006609ex_tabnext(exarg_T *eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006610{
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006611 int tab_number;
6612
Bram Moolenaar8cdbd5b2019-06-16 15:50:45 +02006613 if (ERROR_IF_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02006614 return;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006615 switch (eap->cmdidx)
6616 {
6617 case CMD_tabfirst:
6618 case CMD_tabrewind:
6619 goto_tabpage(1);
6620 break;
6621 case CMD_tablast:
6622 goto_tabpage(9999);
6623 break;
6624 case CMD_tabprevious:
6625 case CMD_tabNext:
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006626 if (eap->arg && *eap->arg != NUL)
6627 {
6628 char_u *p = eap->arg;
6629 char_u *p_save = p;
6630
6631 tab_number = getdigits(&p);
6632 if (p == p_save || *p_save == '-' || *p != NUL
6633 || tab_number == 0)
6634 {
6635 /* No numbers as argument. */
6636 eap->errmsg = e_invarg;
6637 return;
6638 }
6639 }
6640 else
6641 {
6642 if (eap->addr_count == 0)
6643 tab_number = 1;
6644 else
6645 {
6646 tab_number = eap->line2;
6647 if (tab_number < 1)
6648 {
6649 eap->errmsg = e_invrange;
6650 return;
6651 }
6652 }
6653 }
6654 goto_tabpage(-tab_number);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006655 break;
6656 default: /* CMD_tabnext */
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006657 tab_number = get_tabpage_arg(eap);
6658 if (eap->errmsg == NULL)
6659 goto_tabpage(tab_number);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006660 break;
6661 }
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006662}
6663
6664/*
6665 * :tabmove command
6666 */
6667 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006668ex_tabmove(exarg_T *eap)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006669{
Bram Moolenaar40ce3a42015-04-21 18:08:39 +02006670 int tab_number;
Bram Moolenaar8cb8dca2012-07-06 18:27:39 +02006671
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006672 tab_number = get_tabpage_arg(eap);
6673 if (eap->errmsg == NULL)
6674 tabpage_move(tab_number);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006675}
6676
6677/*
6678 * :tabs command: List tabs and their contents.
6679 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006680 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006681ex_tabs(exarg_T *eap UNUSED)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006682{
6683 tabpage_T *tp;
6684 win_T *wp;
6685 int tabcount = 1;
6686
6687 msg_start();
6688 msg_scroll = TRUE;
6689 for (tp = first_tabpage; tp != NULL && !got_int; tp = tp->tp_next)
6690 {
6691 msg_putchar('\n');
6692 vim_snprintf((char *)IObuff, IOSIZE, _("Tab page %d"), tabcount++);
Bram Moolenaar8820b482017-03-16 17:23:31 +01006693 msg_outtrans_attr(IObuff, HL_ATTR(HLF_T));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006694 out_flush(); /* output one line at a time */
6695 ui_breakcheck();
6696
Bram Moolenaar030f0df2006-02-21 22:02:53 +00006697 if (tp == curtab)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006698 wp = firstwin;
6699 else
6700 wp = tp->tp_firstwin;
6701 for ( ; wp != NULL && !got_int; wp = wp->w_next)
6702 {
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006703 msg_putchar('\n');
6704 msg_putchar(wp == curwin ? '>' : ' ');
6705 msg_putchar(' ');
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006706 msg_putchar(bufIsChanged(wp->w_buffer) ? '+' : ' ');
6707 msg_putchar(' ');
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006708 if (buf_spname(wp->w_buffer) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02006709 vim_strncpy(IObuff, buf_spname(wp->w_buffer), IOSIZE - 1);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006710 else
6711 home_replace(wp->w_buffer, wp->w_buffer->b_fname,
6712 IObuff, IOSIZE, TRUE);
6713 msg_outtrans(IObuff);
6714 out_flush(); /* output one line at a time */
6715 ui_breakcheck();
6716 }
6717 }
6718}
6719
Bram Moolenaar071d4272004-06-13 20:20:40 +00006720/*
6721 * ":mode": Set screen mode.
6722 * If no argument given, just get the screen size and redraw.
6723 */
6724 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006725ex_mode(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006726{
6727 if (*eap->arg == NUL)
6728 shell_resized();
6729 else
6730 mch_screenmode(eap->arg);
6731}
6732
Bram Moolenaar071d4272004-06-13 20:20:40 +00006733/*
6734 * ":resize".
6735 * set, increment or decrement current window height
6736 */
6737 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006738ex_resize(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006739{
6740 int n;
6741 win_T *wp = curwin;
6742
6743 if (eap->addr_count > 0)
6744 {
6745 n = eap->line2;
6746 for (wp = firstwin; wp->w_next != NULL && --n > 0; wp = wp->w_next)
6747 ;
6748 }
6749
Bram Moolenaar44a2f922016-03-19 22:11:51 +01006750# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00006751 need_mouse_correct = TRUE;
Bram Moolenaar44a2f922016-03-19 22:11:51 +01006752# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006753 n = atol((char *)eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006754 if (cmdmod.split & WSP_VERT)
6755 {
6756 if (*eap->arg == '-' || *eap->arg == '+')
Bram Moolenaar02631462017-09-22 15:20:32 +02006757 n += curwin->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006758 else if (n == 0 && eap->arg[0] == NUL) /* default is very wide */
6759 n = 9999;
6760 win_setwidth_win((int)n, wp);
6761 }
6762 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006763 {
6764 if (*eap->arg == '-' || *eap->arg == '+')
6765 n += curwin->w_height;
Bram Moolenaar1f2903c2017-07-23 19:51:01 +02006766 else if (n == 0 && eap->arg[0] == NUL) /* default is very high */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006767 n = 9999;
6768 win_setheight_win((int)n, wp);
6769 }
6770}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006771
6772/*
6773 * ":find [+command] <file>" command.
6774 */
6775 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006776ex_find(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006777{
6778#ifdef FEAT_SEARCHPATH
6779 char_u *fname;
6780 int count;
6781
6782 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg), FNAME_MESS,
6783 TRUE, curbuf->b_ffname);
6784 if (eap->addr_count > 0)
6785 {
6786 /* Repeat finding the file "count" times. This matters when it
6787 * appears several times in the path. */
6788 count = eap->line2;
6789 while (fname != NULL && --count > 0)
6790 {
6791 vim_free(fname);
6792 fname = find_file_in_path(NULL, 0, FNAME_MESS,
6793 FALSE, curbuf->b_ffname);
6794 }
6795 }
6796
6797 if (fname != NULL)
6798 {
6799 eap->arg = fname;
6800#endif
6801 do_exedit(eap, NULL);
6802#ifdef FEAT_SEARCHPATH
6803 vim_free(fname);
6804 }
6805#endif
6806}
6807
6808/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00006809 * ":open" simulation: for now just work like ":visual".
6810 */
6811 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006812ex_open(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00006813{
6814 regmatch_T regmatch;
6815 char_u *p;
6816
6817 curwin->w_cursor.lnum = eap->line2;
6818 beginline(BL_SOL | BL_FIX);
6819 if (*eap->arg == '/')
6820 {
6821 /* ":open /pattern/": put cursor in column found with pattern */
6822 ++eap->arg;
6823 p = skip_regexp(eap->arg, '/', p_magic, NULL);
6824 *p = NUL;
6825 regmatch.regprog = vim_regcomp(eap->arg, p_magic ? RE_MAGIC : 0);
6826 if (regmatch.regprog != NULL)
6827 {
6828 regmatch.rm_ic = p_ic;
6829 p = ml_get_curline();
6830 if (vim_regexec(&regmatch, p, (colnr_T)0))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006831 curwin->w_cursor.col = (colnr_T)(regmatch.startp[0] - p);
Bram Moolenaardf177f62005-02-22 08:39:57 +00006832 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006833 emsg(_(e_nomatch));
Bram Moolenaar473de612013-06-08 18:19:48 +02006834 vim_regfree(regmatch.regprog);
Bram Moolenaardf177f62005-02-22 08:39:57 +00006835 }
6836 /* Move to the NUL, ignore any other arguments. */
6837 eap->arg += STRLEN(eap->arg);
6838 }
6839 check_cursor();
6840
6841 eap->cmdidx = CMD_visual;
6842 do_exedit(eap, NULL);
6843}
6844
6845/*
6846 * ":edit", ":badd", ":visual".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006847 */
6848 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006849ex_edit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006850{
6851 do_exedit(eap, NULL);
6852}
6853
6854/*
6855 * ":edit <file>" command and alikes.
6856 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006857 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006858do_exedit(
6859 exarg_T *eap,
6860 win_T *old_curwin) /* curwin before doing a split or NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006861{
6862 int n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006863 int need_hide;
Bram Moolenaardf177f62005-02-22 08:39:57 +00006864 int exmode_was = exmode_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006865
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02006866 if (eap->cmdidx != CMD_pedit && ERROR_IF_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02006867 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006868 /*
6869 * ":vi" command ends Ex mode.
6870 */
6871 if (exmode_active && (eap->cmdidx == CMD_visual
6872 || eap->cmdidx == CMD_view))
6873 {
6874 exmode_active = FALSE;
6875 if (*eap->arg == NUL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00006876 {
6877 /* Special case: ":global/pat/visual\NLvi-commands" */
6878 if (global_busy)
6879 {
6880 int rd = RedrawingDisabled;
6881 int nwr = no_wait_return;
6882 int ms = msg_scroll;
6883#ifdef FEAT_GUI
6884 int he = hold_gui_events;
6885#endif
6886
6887 if (eap->nextcmd != NULL)
6888 {
6889 stuffReadbuff(eap->nextcmd);
6890 eap->nextcmd = NULL;
6891 }
6892
6893 if (exmode_was != EXMODE_VIM)
6894 settmode(TMODE_RAW);
6895 RedrawingDisabled = 0;
6896 no_wait_return = 0;
6897 need_wait_return = FALSE;
6898 msg_scroll = 0;
6899#ifdef FEAT_GUI
6900 hold_gui_events = 0;
6901#endif
6902 must_redraw = CLEAR;
6903
6904 main_loop(FALSE, TRUE);
6905
6906 RedrawingDisabled = rd;
6907 no_wait_return = nwr;
6908 msg_scroll = ms;
6909#ifdef FEAT_GUI
6910 hold_gui_events = he;
6911#endif
6912 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006913 return;
Bram Moolenaardf177f62005-02-22 08:39:57 +00006914 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006915 }
6916
6917 if ((eap->cmdidx == CMD_new
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006918 || eap->cmdidx == CMD_tabnew
6919 || eap->cmdidx == CMD_tabedit
Bram Moolenaar4033c552017-09-16 20:54:51 +02006920 || eap->cmdidx == CMD_vnew) && *eap->arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006921 {
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006922 /* ":new" or ":tabnew" without argument: edit an new empty buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006923 setpcmark();
6924 (void)do_ecmd(0, NULL, NULL, eap, ECMD_ONE,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00006925 ECMD_HIDE + (eap->forceit ? ECMD_FORCEIT : 0),
6926 old_curwin == NULL ? curwin : NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006927 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02006928 else if ((eap->cmdidx != CMD_split && eap->cmdidx != CMD_vsplit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006929 || *eap->arg != NUL
6930#ifdef FEAT_BROWSE
6931 || cmdmod.browse
6932#endif
6933 )
6934 {
Bram Moolenaar5555acc2006-04-07 21:33:12 +00006935 /* Can't edit another file when "curbuf_lock" is set. Only ":edit"
6936 * can bring us here, others are stopped earlier. */
6937 if (*eap->arg != NUL && curbuf_locked())
6938 return;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006939
Bram Moolenaar071d4272004-06-13 20:20:40 +00006940 n = readonlymode;
6941 if (eap->cmdidx == CMD_view || eap->cmdidx == CMD_sview)
6942 readonlymode = TRUE;
6943 else if (eap->cmdidx == CMD_enew)
6944 readonlymode = FALSE; /* 'readonly' doesn't make sense in an
6945 empty buffer */
6946 setpcmark();
6947 if (do_ecmd(0, (eap->cmdidx == CMD_enew ? NULL : eap->arg),
6948 NULL, eap,
6949 /* ":edit" goes to first line if Vi compatible */
6950 (*eap->arg == NUL && eap->do_ecmd_lnum == 0
6951 && vim_strchr(p_cpo, CPO_GOTO1) != NULL)
6952 ? ECMD_ONE : eap->do_ecmd_lnum,
Bram Moolenaareb44a682017-08-03 22:44:55 +02006953 (buf_hide(curbuf) ? ECMD_HIDE : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006954 + (eap->forceit ? ECMD_FORCEIT : 0)
Bram Moolenaar7b449342014-03-25 13:03:48 +01006955 /* after a split we can use an existing buffer */
6956 + (old_curwin != NULL ? ECMD_OLDBUF : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006957 + (eap->cmdidx == CMD_badd ? ECMD_ADDBUF : 0 )
Bram Moolenaar701f7af2008-11-15 13:12:07 +00006958 , old_curwin == NULL ? curwin : NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006959 {
6960 /* Editing the file failed. If the window was split, close it. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006961 if (old_curwin != NULL)
6962 {
6963 need_hide = (curbufIsChanged() && curbuf->b_nwindows <= 1);
Bram Moolenaareb44a682017-08-03 22:44:55 +02006964 if (!need_hide || buf_hide(curbuf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006965 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006966#if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00006967 cleanup_T cs;
6968
6969 /* Reset the error/interrupt/exception state here so that
6970 * aborting() returns FALSE when closing a window. */
6971 enter_cleanup(&cs);
Bram Moolenaar4033c552017-09-16 20:54:51 +02006972#endif
6973#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00006974 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006975#endif
Bram Moolenaareb44a682017-08-03 22:44:55 +02006976 win_close(curwin, !need_hide && !buf_hide(curbuf));
Bram Moolenaarc0197e22004-09-13 20:26:32 +00006977
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006978#if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00006979 /* Restore the error/interrupt/exception state if not
6980 * discarded by a new aborting error, interrupt, or
6981 * uncaught exception. */
6982 leave_cleanup(&cs);
Bram Moolenaar4033c552017-09-16 20:54:51 +02006983#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006984 }
6985 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006986 }
6987 else if (readonlymode && curbuf->b_nwindows == 1)
6988 {
6989 /* When editing an already visited buffer, 'readonly' won't be set
6990 * but the previous value is kept. With ":view" and ":sview" we
6991 * want the file to be readonly, except when another window is
6992 * editing the same buffer. */
6993 curbuf->b_p_ro = TRUE;
6994 }
6995 readonlymode = n;
6996 }
6997 else
6998 {
6999 if (eap->do_ecmd_cmd != NULL)
7000 do_cmdline_cmd(eap->do_ecmd_cmd);
7001#ifdef FEAT_TITLE
7002 n = curwin->w_arg_idx_invalid;
7003#endif
7004 check_arg_idx(curwin);
7005#ifdef FEAT_TITLE
7006 if (n != curwin->w_arg_idx_invalid)
7007 maketitle();
7008#endif
7009 }
7010
Bram Moolenaar071d4272004-06-13 20:20:40 +00007011 /*
7012 * if ":split file" worked, set alternate file name in old window to new
7013 * file
7014 */
7015 if (old_curwin != NULL
7016 && *eap->arg != NUL
7017 && curwin != old_curwin
7018 && win_valid(old_curwin)
Bram Moolenaard4755bb2004-09-02 19:12:26 +00007019 && old_curwin->w_buffer != curbuf
7020 && !cmdmod.keepalt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007021 old_curwin->w_alt_fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007022
7023 ex_no_reprint = TRUE;
7024}
7025
7026#ifndef FEAT_GUI
7027/*
7028 * ":gui" and ":gvim" when there is no GUI.
7029 */
7030 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007031ex_nogui(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007032{
7033 eap->errmsg = e_nogvim;
7034}
7035#endif
7036
Bram Moolenaar4f974752019-02-17 17:44:42 +01007037#if defined(FEAT_GUI_MSWIN) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007038 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007039ex_tearoff(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007040{
7041 gui_make_tearoff(eap->arg);
7042}
7043#endif
7044
Bram Moolenaar29a2c082018-03-05 21:06:23 +01007045#if (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK) \
7046 || defined(FEAT_TERM_POPUP_MENU)) && defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007047 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007048ex_popup(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007049{
Bram Moolenaar29a2c082018-03-05 21:06:23 +01007050# if defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK)
7051 if (gui.in_use)
7052 gui_make_popup(eap->arg, eap->forceit);
7053# ifdef FEAT_TERM_POPUP_MENU
7054 else
7055# endif
7056# endif
7057# ifdef FEAT_TERM_POPUP_MENU
7058 pum_make_popup(eap->arg, eap->forceit);
7059# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007060}
7061#endif
7062
Bram Moolenaar071d4272004-06-13 20:20:40 +00007063 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007064ex_swapname(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007065{
7066 if (curbuf->b_ml.ml_mfp == NULL || curbuf->b_ml.ml_mfp->mf_fname == NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01007067 msg(_("No swap file"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007068 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01007069 msg((char *)curbuf->b_ml.ml_mfp->mf_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007070}
7071
7072/*
7073 * ":syncbind" forces all 'scrollbind' windows to have the same relative
7074 * offset.
7075 * (1998-11-02 16:21:01 R. Edward Ralston <eralston@computer.org>)
7076 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007077 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007078ex_syncbind(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007079{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007080 win_T *wp;
Bram Moolenaardedd1b02013-12-14 13:06:17 +01007081 win_T *save_curwin = curwin;
7082 buf_T *save_curbuf = curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007083 long topline;
7084 long y;
7085 linenr_T old_linenr = curwin->w_cursor.lnum;
7086
7087 setpcmark();
7088
7089 /*
7090 * determine max topline
7091 */
7092 if (curwin->w_p_scb)
7093 {
7094 topline = curwin->w_topline;
Bram Moolenaar29323592016-07-24 22:04:11 +02007095 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007096 {
7097 if (wp->w_p_scb && wp->w_buffer)
7098 {
Bram Moolenaar375e3392019-01-31 18:26:10 +01007099 y = wp->w_buffer->b_ml.ml_line_count - get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007100 if (topline > y)
7101 topline = y;
7102 }
7103 }
7104 if (topline < 1)
7105 topline = 1;
7106 }
7107 else
7108 {
7109 topline = 1;
7110 }
7111
7112
7113 /*
Bram Moolenaardedd1b02013-12-14 13:06:17 +01007114 * Set all scrollbind windows to the same topline.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007115 */
Bram Moolenaar29323592016-07-24 22:04:11 +02007116 FOR_ALL_WINDOWS(curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007117 {
7118 if (curwin->w_p_scb)
7119 {
Bram Moolenaardedd1b02013-12-14 13:06:17 +01007120 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007121 y = topline - curwin->w_topline;
7122 if (y > 0)
7123 scrollup(y, TRUE);
7124 else
7125 scrolldown(-y, TRUE);
7126 curwin->w_scbind_pos = topline;
7127 redraw_later(VALID);
7128 cursor_correct();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007129 curwin->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007130 }
7131 }
Bram Moolenaardedd1b02013-12-14 13:06:17 +01007132 curwin = save_curwin;
7133 curbuf = save_curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007134 if (curwin->w_p_scb)
7135 {
7136 did_syncbind = TRUE;
7137 checkpcmark();
7138 if (old_linenr != curwin->w_cursor.lnum)
7139 {
7140 char_u ctrl_o[2];
7141
7142 ctrl_o[0] = Ctrl_O;
7143 ctrl_o[1] = 0;
7144 ins_typebuf(ctrl_o, REMAP_NONE, 0, TRUE, FALSE);
7145 }
7146 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007147}
7148
7149
7150 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007151ex_read(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007152{
Bram Moolenaardf177f62005-02-22 08:39:57 +00007153 int i;
7154 int empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
7155 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007156
7157 if (eap->usefilter) /* :r!cmd */
7158 do_bang(1, eap, FALSE, FALSE, TRUE);
7159 else
7160 {
7161 if (u_save(eap->line2, (linenr_T)(eap->line2 + 1)) == FAIL)
7162 return;
7163
7164#ifdef FEAT_BROWSE
7165 if (cmdmod.browse)
7166 {
7167 char_u *browseFile;
7168
Bram Moolenaar7171abe2004-10-11 10:06:20 +00007169 browseFile = do_browse(0, (char_u *)_("Append File"), eap->arg,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007170 NULL, NULL, NULL, curbuf);
7171 if (browseFile != NULL)
7172 {
7173 i = readfile(browseFile, NULL,
7174 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
7175 vim_free(browseFile);
7176 }
7177 else
7178 i = OK;
7179 }
7180 else
7181#endif
7182 if (*eap->arg == NUL)
7183 {
7184 if (check_fname() == FAIL) /* check for no file name */
7185 return;
7186 i = readfile(curbuf->b_ffname, curbuf->b_fname,
7187 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
7188 }
7189 else
7190 {
7191 if (vim_strchr(p_cpo, CPO_ALTREAD) != NULL)
7192 (void)setaltfname(eap->arg, eap->arg, (linenr_T)1);
7193 i = readfile(eap->arg, NULL,
7194 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
7195
7196 }
Bram Moolenaare13b9af2017-01-13 22:01:02 +01007197 if (i != OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007198 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007199#if defined(FEAT_EVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007200 if (!aborting())
7201#endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007202 semsg(_(e_notopen), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007203 }
7204 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00007205 {
7206 if (empty && exmode_active)
7207 {
7208 /* Delete the empty line that remains. Historically ex does
7209 * this but vi doesn't. */
7210 if (eap->line2 == 0)
7211 lnum = curbuf->b_ml.ml_line_count;
7212 else
7213 lnum = 1;
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00007214 if (*ml_get(lnum) == NUL && u_savedel(lnum, 1L) == OK)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007215 {
7216 ml_delete(lnum, FALSE);
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00007217 if (curwin->w_cursor.lnum > 1
7218 && curwin->w_cursor.lnum >= lnum)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007219 --curwin->w_cursor.lnum;
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00007220 deleted_lines_mark(lnum, 1L);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007221 }
7222 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007223 redraw_curbuf_later(VALID);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007224 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007225 }
7226}
7227
Bram Moolenaarea408852005-06-25 22:49:46 +00007228static char_u *prev_dir = NULL;
7229
7230#if defined(EXITFREE) || defined(PROTO)
7231 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007232free_cd_dir(void)
Bram Moolenaarea408852005-06-25 22:49:46 +00007233{
Bram Moolenaard23a8232018-02-10 18:45:26 +01007234 VIM_CLEAR(prev_dir);
7235 VIM_CLEAR(globaldir);
Bram Moolenaarea408852005-06-25 22:49:46 +00007236}
7237#endif
7238
Bram Moolenaarf4258302013-06-02 18:20:17 +02007239/*
7240 * Deal with the side effects of changing the current directory.
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007241 * When 'scope' is CDSCOPE_TABPAGE then this was after an ":tcd" command.
7242 * When 'scope' is CDSCOPE_WINDOW then this was after an ":lcd" command.
Bram Moolenaarf4258302013-06-02 18:20:17 +02007243 */
7244 void
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007245post_chdir(cdscope_T scope)
Bram Moolenaarf4258302013-06-02 18:20:17 +02007246{
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007247 if (scope != CDSCOPE_WINDOW)
Bram Moolenaar00aa0692019-04-27 20:37:57 +02007248 // Clear tab local directory for both :cd and :tcd
7249 VIM_CLEAR(curtab->tp_localdir);
Bram Moolenaard23a8232018-02-10 18:45:26 +01007250 VIM_CLEAR(curwin->w_localdir);
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007251 if (scope != CDSCOPE_GLOBAL)
Bram Moolenaarf4258302013-06-02 18:20:17 +02007252 {
7253 /* If still in global directory, need to remember current
7254 * directory as global directory. */
7255 if (globaldir == NULL && prev_dir != NULL)
7256 globaldir = vim_strsave(prev_dir);
7257 /* Remember this local directory for the window. */
7258 if (mch_dirname(NameBuff, MAXPATHL) == OK)
Bram Moolenaar00aa0692019-04-27 20:37:57 +02007259 {
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007260 if (scope == CDSCOPE_TABPAGE)
Bram Moolenaar00aa0692019-04-27 20:37:57 +02007261 curtab->tp_localdir = vim_strsave(NameBuff);
7262 else
7263 curwin->w_localdir = vim_strsave(NameBuff);
7264 }
Bram Moolenaarf4258302013-06-02 18:20:17 +02007265 }
7266 else
7267 {
7268 /* We are now in the global directory, no need to remember its
7269 * name. */
Bram Moolenaard23a8232018-02-10 18:45:26 +01007270 VIM_CLEAR(globaldir);
Bram Moolenaarf4258302013-06-02 18:20:17 +02007271 }
7272
7273 shorten_fnames(TRUE);
7274}
7275
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007276/*
7277 * Change directory function used by :cd/:tcd/:lcd Ex commands and the
7278 * chdir() function. If 'winlocaldir' is TRUE, then changes the window-local
7279 * directory. If 'tablocaldir' is TRUE, then changes the tab-local directory.
7280 * Otherwise changes the global directory.
7281 * Returns TRUE if the directory is successfully changed.
7282 */
7283 int
7284changedir_func(
7285 char_u *new_dir,
7286 int forceit,
7287 cdscope_T scope)
7288{
7289 char_u *tofree;
7290 int dir_differs;
7291 int retval = FALSE;
7292
7293 if (allbuf_locked())
7294 return FALSE;
7295
7296 if (vim_strchr(p_cpo, CPO_CHDIR) != NULL && curbufIsChanged() && !forceit)
7297 {
7298 emsg(_("E747: Cannot change directory, buffer is modified (add ! to override)"));
7299 return FALSE;
7300 }
7301
7302 // ":cd -": Change to previous directory
7303 if (STRCMP(new_dir, "-") == 0)
7304 {
7305 if (prev_dir == NULL)
7306 {
7307 emsg(_("E186: No previous directory"));
7308 return FALSE;
7309 }
7310 new_dir = prev_dir;
7311 }
7312
7313 // Save current directory for next ":cd -"
7314 tofree = prev_dir;
7315 if (mch_dirname(NameBuff, MAXPATHL) == OK)
7316 prev_dir = vim_strsave(NameBuff);
7317 else
7318 prev_dir = NULL;
7319
7320#if defined(UNIX) || defined(VMS)
7321 // for UNIX ":cd" means: go to home directory
7322 if (*new_dir == NUL)
7323 {
7324 // use NameBuff for home directory name
7325# ifdef VMS
7326 char_u *p;
7327
7328 p = mch_getenv((char_u *)"SYS$LOGIN");
7329 if (p == NULL || *p == NUL) // empty is the same as not set
7330 NameBuff[0] = NUL;
7331 else
7332 vim_strncpy(NameBuff, p, MAXPATHL - 1);
7333# else
7334 expand_env((char_u *)"$HOME", NameBuff, MAXPATHL);
7335# endif
7336 new_dir = NameBuff;
7337 }
7338#endif
7339 dir_differs = new_dir == NULL || prev_dir == NULL
7340 || pathcmp((char *)prev_dir, (char *)new_dir, -1) != 0;
7341 if (new_dir == NULL || (dir_differs && vim_chdir(new_dir)))
7342 emsg(_(e_failed));
7343 else
7344 {
7345 char_u *acmd_fname;
7346
7347 post_chdir(scope);
7348
7349 if (dir_differs)
7350 {
7351 if (scope == CDSCOPE_WINDOW)
7352 acmd_fname = (char_u *)"window";
7353 else if (scope == CDSCOPE_TABPAGE)
7354 acmd_fname = (char_u *)"tabpage";
7355 else
7356 acmd_fname = (char_u *)"global";
7357 apply_autocmds(EVENT_DIRCHANGED, acmd_fname, new_dir, FALSE,
7358 curbuf);
7359 }
7360 retval = TRUE;
7361 }
7362 vim_free(tofree);
7363
7364 return retval;
7365}
Bram Moolenaarea408852005-06-25 22:49:46 +00007366
Bram Moolenaar071d4272004-06-13 20:20:40 +00007367/*
Bram Moolenaar00aa0692019-04-27 20:37:57 +02007368 * ":cd", ":tcd", ":lcd", ":chdir" ":tchdir" and ":lchdir".
Bram Moolenaar071d4272004-06-13 20:20:40 +00007369 */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00007370 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007371ex_cd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007372{
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01007373 char_u *new_dir;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007374
7375 new_dir = eap->arg;
7376#if !defined(UNIX) && !defined(VMS)
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007377 // for non-UNIX ":cd" means: print current directory
Bram Moolenaar071d4272004-06-13 20:20:40 +00007378 if (*new_dir == NUL)
7379 ex_pwd(NULL);
7380 else
7381#endif
7382 {
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007383 cdscope_T scope = CDSCOPE_GLOBAL;
7384
7385 if (eap->cmdidx == CMD_lcd || eap->cmdidx == CMD_lchdir)
7386 scope = CDSCOPE_WINDOW;
7387 else if (eap->cmdidx == CMD_tcd || eap->cmdidx == CMD_tchdir)
7388 scope = CDSCOPE_TABPAGE;
7389
7390 if (changedir_func(new_dir, eap->forceit, scope))
Bram Moolenaardf177f62005-02-22 08:39:57 +00007391 {
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007392 // Echo the new current directory if the command was typed.
Bram Moolenaarfcfbc672009-07-09 18:13:49 +00007393 if (KeyTyped || p_verbose >= 5)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007394 ex_pwd(eap);
7395 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007396 }
7397}
7398
7399/*
7400 * ":pwd".
7401 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007402 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007403ex_pwd(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007404{
7405 if (mch_dirname(NameBuff, MAXPATHL) == OK)
7406 {
7407#ifdef BACKSLASH_IN_FILENAME
7408 slash_adjust(NameBuff);
7409#endif
Bram Moolenaar32526b32019-01-19 17:43:09 +01007410 msg((char *)NameBuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007411 }
7412 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007413 emsg(_("E187: Unknown"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007414}
7415
7416/*
7417 * ":=".
7418 */
7419 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007420ex_equal(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007421{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007422 smsg("%ld", (long)eap->line2);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007423 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007424}
7425
7426 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007427ex_sleep(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007428{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007429 int n;
7430 long len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007431
7432 if (cursor_valid())
7433 {
7434 n = W_WINROW(curwin) + curwin->w_wrow - msg_scrolled;
7435 if (n >= 0)
Bram Moolenaar53f81742017-09-22 14:35:51 +02007436 windgoto((int)n, curwin->w_wincol + curwin->w_wcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007437 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007438
7439 len = eap->line2;
7440 switch (*eap->arg)
7441 {
7442 case 'm': break;
7443 case NUL: len *= 1000L; break;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007444 default: semsg(_(e_invarg2), eap->arg); return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007445 }
7446 do_sleep(len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007447}
7448
7449/*
7450 * Sleep for "msec" milliseconds, but keep checking for a CTRL-C every second.
7451 */
7452 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007453do_sleep(long msec)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007454{
Bram Moolenaar52953192019-08-16 10:27:13 +02007455 long done = 0;
Bram Moolenaar975b5272016-03-15 23:10:59 +01007456 long wait_now;
Bram Moolenaar52953192019-08-16 10:27:13 +02007457# ifdef ELAPSED_FUNC
7458 elapsed_T start_tv;
7459
7460 // Remember at what time we started, so that we know how much longer we
7461 // should wait after waiting for a bit.
7462 ELAPSED_INIT(start_tv);
7463# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007464
7465 cursor_on();
Bram Moolenaarf45d7472018-09-30 18:22:26 +02007466 out_flush_cursor(FALSE, FALSE);
Bram Moolenaar52953192019-08-16 10:27:13 +02007467 while (!got_int && done < msec)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007468 {
Bram Moolenaar975b5272016-03-15 23:10:59 +01007469 wait_now = msec - done > 1000L ? 1000L : msec - done;
7470#ifdef FEAT_TIMERS
7471 {
7472 long due_time = check_due_timer();
7473
7474 if (due_time > 0 && due_time < wait_now)
7475 wait_now = due_time;
7476 }
7477#endif
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02007478#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar28e67e02019-08-15 23:05:49 +02007479 if (has_any_channel() && wait_now > 20L)
7480 wait_now = 20L;
7481#endif
7482#ifdef FEAT_SOUND
7483 if (has_any_sound_callback() && wait_now > 20L)
7484 wait_now = 20L;
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02007485#endif
Bram Moolenaar975b5272016-03-15 23:10:59 +01007486 ui_delay(wait_now, TRUE);
Bram Moolenaar52953192019-08-16 10:27:13 +02007487
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02007488#ifdef FEAT_JOB_CHANNEL
7489 if (has_any_channel())
7490 ui_breakcheck_force(TRUE);
7491 else
7492#endif
7493 ui_breakcheck();
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007494#ifdef MESSAGE_QUEUE
Bram Moolenaar52953192019-08-16 10:27:13 +02007495 // Process the netbeans and clientserver messages that may have been
7496 // received in the call to ui_breakcheck() when the GUI is in use. This
7497 // may occur when running a test case.
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007498 parse_queued_messages();
Bram Moolenaare3cc6d42011-10-20 21:58:34 +02007499#endif
Bram Moolenaar52953192019-08-16 10:27:13 +02007500
7501# ifdef ELAPSED_FUNC
7502 // actual time passed
7503 done = ELAPSED_FUNC(start_tv);
7504# else
7505 // guestimate time passed (will actually be more)
7506 done += wait_now;
7507# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007508 }
Bram Moolenaar1ebff3d2018-07-07 16:18:13 +02007509
7510 // If CTRL-C was typed to interrupt the sleep, drop the CTRL-C from the
7511 // input buffer, otherwise a following call to input() fails.
7512 if (got_int)
7513 (void)vpeekc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007514}
7515
Bram Moolenaar071d4272004-06-13 20:20:40 +00007516/*
7517 * ":winsize" command (obsolete).
7518 */
7519 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007520ex_winsize(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007521{
7522 int w, h;
7523 char_u *arg = eap->arg;
7524 char_u *p;
7525
7526 w = getdigits(&arg);
7527 arg = skipwhite(arg);
7528 p = arg;
7529 h = getdigits(&arg);
7530 if (*p != NUL && *arg == NUL)
7531 set_shellsize(w, h, TRUE);
7532 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007533 emsg(_("E465: :winsize requires two number arguments"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007534}
7535
Bram Moolenaar071d4272004-06-13 20:20:40 +00007536 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007537ex_wincmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007538{
7539 int xchar = NUL;
7540 char_u *p;
7541
7542 if (*eap->arg == 'g' || *eap->arg == Ctrl_G)
7543 {
7544 /* CTRL-W g and CTRL-W CTRL-G have an extra command character */
7545 if (eap->arg[1] == NUL)
7546 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007547 emsg(_(e_invarg));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007548 return;
7549 }
7550 xchar = eap->arg[1];
7551 p = eap->arg + 2;
7552 }
7553 else
7554 p = eap->arg + 1;
7555
7556 eap->nextcmd = check_nextcmd(p);
7557 p = skipwhite(p);
7558 if (*p != NUL && *p != '"' && eap->nextcmd == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007559 emsg(_(e_invarg));
Bram Moolenaar12bde492011-06-13 01:19:56 +02007560 else if (!eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007561 {
7562 /* Pass flags on for ":vertical wincmd ]". */
7563 postponed_split_flags = cmdmod.split;
Bram Moolenaard326ce82007-03-11 14:48:29 +00007564 postponed_split_tab = cmdmod.tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007565 do_window(*eap->arg, eap->addr_count > 0 ? eap->line2 : 0L, xchar);
7566 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00007567 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007568 }
7569}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007570
Bram Moolenaar843ee412004-06-30 16:16:41 +00007571#if defined(FEAT_GUI) || defined(UNIX) || defined(VMS) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007572/*
7573 * ":winpos".
7574 */
7575 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007576ex_winpos(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007577{
7578 int x, y;
7579 char_u *arg = eap->arg;
7580 char_u *p;
7581
7582 if (*arg == NUL)
7583 {
Bram Moolenaar843ee412004-06-30 16:16:41 +00007584# if defined(FEAT_GUI) || defined(MSWIN)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007585# ifdef VIMDLL
7586 if (gui.in_use ? gui_mch_get_winpos(&x, &y) != FAIL :
7587 mch_get_winpos(&x, &y) != FAIL)
7588# elif defined(FEAT_GUI)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007589 if (gui.in_use && gui_mch_get_winpos(&x, &y) != FAIL)
Bram Moolenaar843ee412004-06-30 16:16:41 +00007590# else
7591 if (mch_get_winpos(&x, &y) != FAIL)
7592# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007593 {
7594 sprintf((char *)IObuff, _("Window position: X %d, Y %d"), x, y);
Bram Moolenaar32526b32019-01-19 17:43:09 +01007595 msg((char *)IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007596 }
7597 else
7598# endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007599 emsg(_("E188: Obtaining window position not implemented for this platform"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007600 }
7601 else
7602 {
7603 x = getdigits(&arg);
7604 arg = skipwhite(arg);
7605 p = arg;
7606 y = getdigits(&arg);
7607 if (*p == NUL || *arg != NUL)
7608 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007609 emsg(_("E466: :winpos requires two number arguments"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007610 return;
7611 }
7612# ifdef FEAT_GUI
7613 if (gui.in_use)
7614 gui_mch_set_winpos(x, y);
7615 else if (gui.starting)
7616 {
7617 /* Remember the coordinates for when the window is opened. */
7618 gui_win_x = x;
7619 gui_win_y = y;
7620 }
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007621# if defined(HAVE_TGETENT) || defined(VIMDLL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007622 else
7623# endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007624# endif
7625# if defined(MSWIN) && (!defined(FEAT_GUI) || defined(VIMDLL))
Bram Moolenaar843ee412004-06-30 16:16:41 +00007626 mch_set_winpos(x, y);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007627# endif
7628# ifdef HAVE_TGETENT
7629 if (*T_CWP)
7630 term_set_winpos(x, y);
7631# endif
7632 }
7633}
7634#endif
7635
7636/*
7637 * Handle command that work like operators: ":delete", ":yank", ":>" and ":<".
7638 */
7639 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007640ex_operators(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007641{
7642 oparg_T oa;
7643
7644 clear_oparg(&oa);
7645 oa.regname = eap->regname;
7646 oa.start.lnum = eap->line1;
7647 oa.end.lnum = eap->line2;
7648 oa.line_count = eap->line2 - eap->line1 + 1;
7649 oa.motion_type = MLINE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007650 virtual_op = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007651 if (eap->cmdidx != CMD_yank) /* position cursor for undo */
7652 {
7653 setpcmark();
7654 curwin->w_cursor.lnum = eap->line1;
7655 beginline(BL_SOL | BL_FIX);
7656 }
7657
Bram Moolenaard07c6e12013-11-21 14:21:40 +01007658 if (VIsual_active)
7659 end_visual_mode();
Bram Moolenaard07c6e12013-11-21 14:21:40 +01007660
Bram Moolenaar071d4272004-06-13 20:20:40 +00007661 switch (eap->cmdidx)
7662 {
7663 case CMD_delete:
7664 oa.op_type = OP_DELETE;
7665 op_delete(&oa);
7666 break;
7667
7668 case CMD_yank:
7669 oa.op_type = OP_YANK;
7670 (void)op_yank(&oa, FALSE, TRUE);
7671 break;
7672
7673 default: /* CMD_rshift or CMD_lshift */
Bram Moolenaar6e707362013-06-14 19:15:58 +02007674 if (
Bram Moolenaar071d4272004-06-13 20:20:40 +00007675#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6e707362013-06-14 19:15:58 +02007676 (eap->cmdidx == CMD_rshift) ^ curwin->w_p_rl
7677#else
7678 eap->cmdidx == CMD_rshift
Bram Moolenaar071d4272004-06-13 20:20:40 +00007679#endif
Bram Moolenaar6e707362013-06-14 19:15:58 +02007680 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00007681 oa.op_type = OP_RSHIFT;
7682 else
7683 oa.op_type = OP_LSHIFT;
7684 op_shift(&oa, FALSE, eap->amount);
7685 break;
7686 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007687 virtual_op = MAYBE;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007688 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007689}
7690
7691/*
7692 * ":put".
7693 */
7694 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007695ex_put(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007696{
7697 /* ":0put" works like ":1put!". */
7698 if (eap->line2 == 0)
7699 {
7700 eap->line2 = 1;
7701 eap->forceit = TRUE;
7702 }
7703 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007704 do_put(eap->regname, eap->forceit ? BACKWARD : FORWARD, 1L,
7705 PUT_LINE|PUT_CURSLINE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007706}
7707
7708/*
7709 * Handle ":copy" and ":move".
7710 */
7711 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007712ex_copymove(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007713{
7714 long n;
7715
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02007716 n = get_address(eap, &eap->arg, eap->addr_type, FALSE, FALSE, FALSE, 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007717 if (eap->arg == NULL) /* error detected */
7718 {
7719 eap->nextcmd = NULL;
7720 return;
7721 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00007722 get_flags(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007723
7724 /*
7725 * move or copy lines from 'eap->line1'-'eap->line2' to below line 'n'
7726 */
7727 if (n == MAXLNUM || n < 0 || n > curbuf->b_ml.ml_line_count)
7728 {
Bram Moolenaar0acae7a2019-08-06 21:29:29 +02007729 emsg(_(e_invrange));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007730 return;
7731 }
7732
7733 if (eap->cmdidx == CMD_move)
7734 {
7735 if (do_move(eap->line1, eap->line2, n) == FAIL)
7736 return;
7737 }
7738 else
7739 ex_copy(eap->line1, eap->line2, n);
7740 u_clearline();
7741 beginline(BL_SOL | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007742 ex_may_print(eap);
7743}
7744
7745/*
7746 * Print the current line if flags were given to the Ex command.
7747 */
Bram Moolenaarfd3fe982014-04-01 17:49:44 +02007748 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007749ex_may_print(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007750{
7751 if (eap->flags != 0)
7752 {
7753 print_line(curwin->w_cursor.lnum, (eap->flags & EXFLAG_NR),
7754 (eap->flags & EXFLAG_LIST));
7755 ex_no_reprint = TRUE;
7756 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007757}
7758
7759/*
7760 * ":smagic" and ":snomagic".
7761 */
7762 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007763ex_submagic(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007764{
7765 int magic_save = p_magic;
7766
7767 p_magic = (eap->cmdidx == CMD_smagic);
7768 do_sub(eap);
7769 p_magic = magic_save;
7770}
7771
7772/*
7773 * ":join".
7774 */
7775 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007776ex_join(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007777{
7778 curwin->w_cursor.lnum = eap->line1;
7779 if (eap->line1 == eap->line2)
7780 {
7781 if (eap->addr_count >= 2) /* :2,2join does nothing */
7782 return;
7783 if (eap->line2 == curbuf->b_ml.ml_line_count)
7784 {
7785 beep_flush();
7786 return;
7787 }
7788 ++eap->line2;
7789 }
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02007790 (void)do_join(eap->line2 - eap->line1 + 1, !eap->forceit, TRUE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007791 beginline(BL_WHITE | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007792 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007793}
7794
7795/*
7796 * ":[addr]@r" or ":[addr]*r": execute register
7797 */
7798 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007799ex_at(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007800{
7801 int c;
Bram Moolenaar60462872009-11-03 11:40:19 +00007802 int prev_len = typebuf.tb_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007803
7804 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaar4930a762016-09-11 14:39:53 +02007805 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007806
7807#ifdef USE_ON_FLY_SCROLL
7808 dont_scroll = TRUE; /* disallow scrolling here */
7809#endif
7810
7811 /* get the register name. No name means to use the previous one */
7812 c = *eap->arg;
7813 if (c == NUL || (c == '*' && *eap->cmd == '*'))
7814 c = '@';
Bram Moolenaard333d1e2006-11-07 17:43:47 +00007815 /* Put the register in the typeahead buffer with the "silent" flag. */
7816 if (do_execreg(c, TRUE, vim_strchr(p_cpo, CPO_EXECBUF) != NULL, TRUE)
7817 == FAIL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007818 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007819 beep_flush();
Bram Moolenaardf177f62005-02-22 08:39:57 +00007820 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007821 else
7822 {
7823 int save_efr = exec_from_reg;
7824
7825 exec_from_reg = TRUE;
7826
7827 /*
7828 * Execute from the typeahead buffer.
Bram Moolenaar60462872009-11-03 11:40:19 +00007829 * Continue until the stuff buffer is empty and all added characters
7830 * have been consumed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007831 */
Bram Moolenaar60462872009-11-03 11:40:19 +00007832 while (!stuff_empty() || typebuf.tb_len > prev_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007833 (void)do_cmdline(NULL, getexline, NULL, DOCMD_NOWAIT|DOCMD_VERBOSE);
7834
7835 exec_from_reg = save_efr;
7836 }
7837}
7838
7839/*
7840 * ":!".
7841 */
7842 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007843ex_bang(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007844{
7845 do_bang(eap->addr_count, eap, eap->forceit, TRUE, TRUE);
7846}
7847
7848/*
7849 * ":undo".
7850 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007851 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +01007852ex_undo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007853{
Bram Moolenaard3667a22006-03-16 21:35:52 +00007854 if (eap->addr_count == 1) /* :undo 123 */
Bram Moolenaar730cde92010-06-27 05:18:54 +02007855 undo_time(eap->line2, FALSE, FALSE, TRUE);
Bram Moolenaard3667a22006-03-16 21:35:52 +00007856 else
7857 u_undo(1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007858}
7859
Bram Moolenaar55debbe2010-05-23 23:34:36 +02007860#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar6df6f472010-07-18 18:04:50 +02007861 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007862ex_wundo(exarg_T *eap)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02007863{
7864 char_u hash[UNDO_HASH_SIZE];
7865
7866 u_compute_hash(hash);
7867 u_write_undo(eap->arg, eap->forceit, curbuf, hash);
7868}
7869
Bram Moolenaar6df6f472010-07-18 18:04:50 +02007870 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007871ex_rundo(exarg_T *eap)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02007872{
7873 char_u hash[UNDO_HASH_SIZE];
7874
7875 u_compute_hash(hash);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02007876 u_read_undo(eap->arg, hash, NULL);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02007877}
7878#endif
7879
Bram Moolenaar071d4272004-06-13 20:20:40 +00007880/*
7881 * ":redo".
7882 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007883 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007884ex_redo(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007885{
7886 u_redo(1);
7887}
7888
7889/*
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007890 * ":earlier" and ":later".
7891 */
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007892 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007893ex_later(exarg_T *eap)
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007894{
7895 long count = 0;
7896 int sec = FALSE;
Bram Moolenaar730cde92010-06-27 05:18:54 +02007897 int file = FALSE;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007898 char_u *p = eap->arg;
7899
7900 if (*p == NUL)
7901 count = 1;
7902 else if (isdigit(*p))
7903 {
7904 count = getdigits(&p);
7905 switch (*p)
7906 {
7907 case 's': ++p; sec = TRUE; break;
7908 case 'm': ++p; sec = TRUE; count *= 60; break;
7909 case 'h': ++p; sec = TRUE; count *= 60 * 60; break;
Bram Moolenaar730cde92010-06-27 05:18:54 +02007910 case 'd': ++p; sec = TRUE; count *= 24 * 60 * 60; break;
7911 case 'f': ++p; file = TRUE; break;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007912 }
7913 }
7914
7915 if (*p != NUL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007916 semsg(_(e_invarg2), eap->arg);
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007917 else
Bram Moolenaar730cde92010-06-27 05:18:54 +02007918 undo_time(eap->cmdidx == CMD_earlier ? -count : count,
7919 sec, file, FALSE);
Bram Moolenaarc7d89352006-03-14 22:53:34 +00007920}
7921
7922/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007923 * ":redir": start/stop redirection.
7924 */
7925 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007926ex_redir(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007927{
7928 char *mode;
7929 char_u *fname;
Bram Moolenaarca472992005-01-21 11:46:23 +00007930 char_u *arg = eap->arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007931
Bram Moolenaarba768492016-07-08 15:32:54 +02007932#ifdef FEAT_EVAL
Bram Moolenaar79815f12016-07-09 17:07:29 +02007933 if (redir_execute)
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02007934 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007935 emsg(_("E930: Cannot use :redir inside execute()"));
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02007936 return;
7937 }
Bram Moolenaarba768492016-07-08 15:32:54 +02007938#endif
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02007939
Bram Moolenaar071d4272004-06-13 20:20:40 +00007940 if (STRICMP(eap->arg, "END") == 0)
7941 close_redir();
7942 else
7943 {
Bram Moolenaarca472992005-01-21 11:46:23 +00007944 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00007945 {
Bram Moolenaarca472992005-01-21 11:46:23 +00007946 ++arg;
7947 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00007948 {
Bram Moolenaarca472992005-01-21 11:46:23 +00007949 ++arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007950 mode = "a";
7951 }
7952 else
7953 mode = "w";
Bram Moolenaarca472992005-01-21 11:46:23 +00007954 arg = skipwhite(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007955
7956 close_redir();
7957
7958 /* Expand environment variables and "~/". */
Bram Moolenaarca472992005-01-21 11:46:23 +00007959 fname = expand_env_save(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007960 if (fname == NULL)
7961 return;
7962#ifdef FEAT_BROWSE
7963 if (cmdmod.browse)
7964 {
7965 char_u *browseFile;
7966
Bram Moolenaar7171abe2004-10-11 10:06:20 +00007967 browseFile = do_browse(BROWSE_SAVE,
7968 (char_u *)_("Save Redirection"),
Bram Moolenaarc36651b2018-04-29 12:22:56 +02007969 fname, NULL, NULL,
7970 (char_u *)_(BROWSE_FILTER_ALL_FILES), curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007971 if (browseFile == NULL)
7972 return; /* operation cancelled */
7973 vim_free(fname);
7974 fname = browseFile;
7975 eap->forceit = TRUE; /* since dialog already asked */
7976 }
7977#endif
7978
7979 redir_fd = open_exfile(fname, eap->forceit, mode);
7980 vim_free(fname);
7981 }
7982#ifdef FEAT_EVAL
Bram Moolenaarca472992005-01-21 11:46:23 +00007983 else if (*arg == '@')
Bram Moolenaar071d4272004-06-13 20:20:40 +00007984 {
7985 /* redirect to a register a-z (resp. A-Z for appending) */
7986 close_redir();
Bram Moolenaarca472992005-01-21 11:46:23 +00007987 ++arg;
7988 if (ASCII_ISALPHA(*arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007989# ifdef FEAT_CLIPBOARD
Bram Moolenaarca472992005-01-21 11:46:23 +00007990 || *arg == '*'
Bram Moolenaar9a51c6e2006-11-14 19:25:02 +00007991 || *arg == '+'
Bram Moolenaar071d4272004-06-13 20:20:40 +00007992# endif
Bram Moolenaarca472992005-01-21 11:46:23 +00007993 || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00007994 {
Bram Moolenaarca472992005-01-21 11:46:23 +00007995 redir_reg = *arg++;
Bram Moolenaarc188b882007-10-19 14:20:54 +00007996 if (*arg == '>' && arg[1] == '>') /* append */
Bram Moolenaard9d30582005-05-18 22:10:28 +00007997 arg += 2;
Bram Moolenaarc188b882007-10-19 14:20:54 +00007998 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007999 {
Bram Moolenaarc188b882007-10-19 14:20:54 +00008000 /* Can use both "@a" and "@a>". */
Bram Moolenaar2c29bee2005-06-01 21:46:07 +00008001 if (*arg == '>')
8002 arg++;
Bram Moolenaarc188b882007-10-19 14:20:54 +00008003 /* Make register empty when not using @A-@Z and the
8004 * command is valid. */
8005 if (*arg == NUL && !isupper(redir_reg))
8006 write_reg_contents(redir_reg, (char_u *)"", -1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008007 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00008008 }
8009 if (*arg != NUL)
8010 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00008011 redir_reg = 0;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008012 semsg(_(e_invarg2), eap->arg);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008013 }
8014 }
8015 else if (*arg == '=' && arg[1] == '>')
8016 {
8017 int append;
8018
8019 /* redirect to a variable */
8020 close_redir();
8021 arg += 2;
8022
8023 if (*arg == '>')
8024 {
8025 ++arg;
8026 append = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008027 }
8028 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00008029 append = FALSE;
8030
8031 if (var_redir_start(skipwhite(arg), append) == OK)
8032 redir_vname = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008033 }
8034#endif
8035
8036 /* TODO: redirect to a buffer */
8037
Bram Moolenaar071d4272004-06-13 20:20:40 +00008038 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008039 semsg(_(e_invarg2), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008040 }
Bram Moolenaar29b2d262006-09-10 19:07:28 +00008041
8042 /* Make sure redirection is not off. Can happen for cmdline completion
8043 * that indirectly invokes a command to catch its output. */
8044 if (redir_fd != NULL
8045#ifdef FEAT_EVAL
8046 || redir_reg || redir_vname
8047#endif
8048 )
8049 redir_off = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008050}
8051
8052/*
8053 * ":redraw": force redraw
8054 */
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01008055 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008056ex_redraw(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008057{
8058 int r = RedrawingDisabled;
8059 int p = p_lz;
8060
8061 RedrawingDisabled = 0;
8062 p_lz = FALSE;
Bram Moolenaarabc39ab2017-03-01 18:04:05 +01008063 validate_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008064 update_topline();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01008065 update_screen(eap->forceit ? CLEAR : VIsual_active ? INVERTED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008066#ifdef FEAT_TITLE
8067 if (need_maketitle)
8068 maketitle();
8069#endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02008070#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
8071# ifdef VIMDLL
8072 if (!gui.in_use)
8073# endif
8074 resize_console_buf();
Bram Moolenaar78d21da2019-02-17 15:00:52 +01008075#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008076 RedrawingDisabled = r;
8077 p_lz = p;
8078
8079 /* Reset msg_didout, so that a message that's there is overwritten. */
8080 msg_didout = FALSE;
8081 msg_col = 0;
8082
Bram Moolenaar56667a52013-07-17 11:54:28 +02008083 /* No need to wait after an intentional redraw. */
8084 need_wait_return = FALSE;
8085
Bram Moolenaar071d4272004-06-13 20:20:40 +00008086 out_flush();
8087}
8088
8089/*
8090 * ":redrawstatus": force redraw of status line(s)
8091 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008092 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008093ex_redrawstatus(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008094{
Bram Moolenaar071d4272004-06-13 20:20:40 +00008095 int r = RedrawingDisabled;
8096 int p = p_lz;
8097
8098 RedrawingDisabled = 0;
8099 p_lz = FALSE;
8100 if (eap->forceit)
8101 status_redraw_all();
8102 else
8103 status_redraw_curbuf();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01008104 update_screen(VIsual_active ? INVERTED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008105 RedrawingDisabled = r;
8106 p_lz = p;
8107 out_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008108}
8109
Bram Moolenaare12bab32019-01-08 22:02:56 +01008110/*
8111 * ":redrawtabline": force redraw of the tabline
8112 */
8113 static void
8114ex_redrawtabline(exarg_T *eap UNUSED)
8115{
8116 int r = RedrawingDisabled;
8117 int p = p_lz;
8118
8119 RedrawingDisabled = 0;
8120 p_lz = FALSE;
8121
8122 draw_tabline();
8123
8124 RedrawingDisabled = r;
8125 p_lz = p;
8126 out_flush();
8127}
8128
Bram Moolenaar071d4272004-06-13 20:20:40 +00008129 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008130close_redir(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008131{
8132 if (redir_fd != NULL)
8133 {
8134 fclose(redir_fd);
8135 redir_fd = NULL;
8136 }
8137#ifdef FEAT_EVAL
8138 redir_reg = 0;
Bram Moolenaardf177f62005-02-22 08:39:57 +00008139 if (redir_vname)
8140 {
8141 var_redir_stop();
8142 redir_vname = 0;
8143 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008144#endif
8145}
8146
Bram Moolenaarf96ae0b2019-07-28 15:21:55 +02008147#if (defined(FEAT_SESSION) || defined(FEAT_EVAL)) || defined(PROTO)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008148 int
Bram Moolenaarf1d25012016-03-03 12:22:53 +01008149vim_mkdir_emsg(char_u *name, int prot)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008150{
8151 if (vim_mkdir(name, prot) != 0)
8152 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008153 semsg(_("E739: Cannot create directory: %s"), name);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008154 return FAIL;
8155 }
8156 return OK;
8157}
8158#endif
8159
Bram Moolenaar071d4272004-06-13 20:20:40 +00008160/*
8161 * Open a file for writing for an Ex command, with some checks.
8162 * Return file descriptor, or NULL on failure.
8163 */
8164 FILE *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008165open_exfile(
8166 char_u *fname,
8167 int forceit,
8168 char *mode) /* "w" for create new file or "a" for append */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008169{
8170 FILE *fd;
8171
8172#ifdef UNIX
8173 /* with Unix it is possible to open a directory */
8174 if (mch_isdir(fname))
8175 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008176 semsg(_(e_isadir2), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008177 return NULL;
8178 }
8179#endif
8180 if (!forceit && *mode != 'a' && vim_fexists(fname))
8181 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008182 semsg(_("E189: \"%s\" exists (add ! to override)"), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008183 return NULL;
8184 }
8185
8186 if ((fd = mch_fopen((char *)fname, mode)) == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008187 semsg(_("E190: Cannot open \"%s\" for writing"), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008188
8189 return fd;
8190}
8191
8192/*
8193 * ":mark" and ":k".
8194 */
8195 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008196ex_mark(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008197{
8198 pos_T pos;
8199
8200 if (*eap->arg == NUL) /* No argument? */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008201 emsg(_(e_argreq));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008202 else if (eap->arg[1] != NUL) /* more than one character? */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008203 emsg(_(e_trailing));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008204 else
8205 {
8206 pos = curwin->w_cursor; /* save curwin->w_cursor */
8207 curwin->w_cursor.lnum = eap->line2;
8208 beginline(BL_WHITE | BL_FIX);
8209 if (setmark(*eap->arg) == FAIL) /* set mark */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008210 emsg(_("E191: Argument must be a letter or forward/backward quote"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008211 curwin->w_cursor = pos; /* restore curwin->w_cursor */
8212 }
8213}
8214
8215/*
8216 * Update w_topline, w_leftcol and the cursor position.
8217 */
8218 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008219update_topline_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008220{
8221 check_cursor(); /* put cursor on valid line */
8222 update_topline();
8223 if (!curwin->w_p_wrap)
8224 validate_cursor();
8225 update_curswant();
8226}
8227
Bram Moolenaar071d4272004-06-13 20:20:40 +00008228/*
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008229 * Save the current State and go to Normal mode.
8230 * Return TRUE if the typeahead could be saved.
8231 */
8232 int
8233save_current_state(save_state_T *sst)
8234{
8235 sst->save_msg_scroll = msg_scroll;
8236 sst->save_restart_edit = restart_edit;
8237 sst->save_msg_didout = msg_didout;
8238 sst->save_State = State;
8239 sst->save_insertmode = p_im;
8240 sst->save_finish_op = finish_op;
8241 sst->save_opcount = opcount;
Bram Moolenaarcce713d2019-03-04 11:40:12 +01008242 sst->save_reg_executing = reg_executing;
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008243
8244 msg_scroll = FALSE; /* no msg scrolling in Normal mode */
8245 restart_edit = 0; /* don't go to Insert mode */
8246 p_im = FALSE; /* don't use 'insertmode' */
8247
8248 /*
8249 * Save the current typeahead. This is required to allow using ":normal"
8250 * from an event handler and makes sure we don't hang when the argument
8251 * ends with half a command.
8252 */
8253 save_typeahead(&sst->tabuf);
8254 return sst->tabuf.typebuf_valid;
8255}
8256
8257 void
8258restore_current_state(save_state_T *sst)
8259{
8260 /* Restore the previous typeahead. */
8261 restore_typeahead(&sst->tabuf);
8262
8263 msg_scroll = sst->save_msg_scroll;
8264 restart_edit = sst->save_restart_edit;
8265 p_im = sst->save_insertmode;
8266 finish_op = sst->save_finish_op;
8267 opcount = sst->save_opcount;
Bram Moolenaarcce713d2019-03-04 11:40:12 +01008268 reg_executing = sst->save_reg_executing;
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008269 msg_didout |= sst->save_msg_didout; /* don't reset msg_didout now */
8270
8271 /* Restore the state (needed when called from a function executed for
8272 * 'indentexpr'). Update the mouse and cursor, they may have changed. */
8273 State = sst->save_State;
8274#ifdef CURSOR_SHAPE
8275 ui_cursor_shape(); /* may show different cursor shape */
8276#endif
8277}
8278
8279/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008280 * ":normal[!] {commands}": Execute normal mode commands.
8281 */
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01008282 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008283ex_normal(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008284{
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008285 save_state_T save_state;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008286 char_u *arg = NULL;
8287 int l;
8288 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008289
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008290 if (ex_normal_lock > 0)
8291 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008292 emsg(_(e_secure));
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008293 return;
8294 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008295 if (ex_normal_busy >= p_mmd)
8296 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008297 emsg(_("E192: Recursive use of :normal too deep"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008298 return;
8299 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008300
Bram Moolenaar071d4272004-06-13 20:20:40 +00008301 /*
8302 * vgetc() expects a CSI and K_SPECIAL to have been escaped. Don't do
8303 * this for the K_SPECIAL leading byte, otherwise special keys will not
8304 * work.
8305 */
8306 if (has_mbyte)
8307 {
8308 int len = 0;
8309
8310 /* Count the number of characters to be escaped. */
8311 for (p = eap->arg; *p != NUL; ++p)
8312 {
Bram Moolenaar13505972019-01-24 15:04:48 +01008313#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008314 if (*p == CSI) /* leadbyte CSI */
8315 len += 2;
Bram Moolenaar13505972019-01-24 15:04:48 +01008316#endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008317 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008318 if (*++p == K_SPECIAL /* trailbyte K_SPECIAL or CSI */
Bram Moolenaar13505972019-01-24 15:04:48 +01008319#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008320 || *p == CSI
Bram Moolenaar13505972019-01-24 15:04:48 +01008321#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008322 )
8323 len += 2;
8324 }
8325 if (len > 0)
8326 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02008327 arg = alloc(STRLEN(eap->arg) + len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008328 if (arg != NULL)
8329 {
8330 len = 0;
8331 for (p = eap->arg; *p != NUL; ++p)
8332 {
8333 arg[len++] = *p;
Bram Moolenaar13505972019-01-24 15:04:48 +01008334#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008335 if (*p == CSI)
8336 {
8337 arg[len++] = KS_EXTRA;
8338 arg[len++] = (int)KE_CSI;
8339 }
Bram Moolenaar13505972019-01-24 15:04:48 +01008340#endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008341 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008342 {
8343 arg[len++] = *++p;
8344 if (*p == K_SPECIAL)
8345 {
8346 arg[len++] = KS_SPECIAL;
8347 arg[len++] = KE_FILLER;
8348 }
Bram Moolenaar13505972019-01-24 15:04:48 +01008349#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008350 else if (*p == CSI)
8351 {
8352 arg[len++] = KS_EXTRA;
8353 arg[len++] = (int)KE_CSI;
8354 }
Bram Moolenaar13505972019-01-24 15:04:48 +01008355#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008356 }
8357 arg[len] = NUL;
8358 }
8359 }
8360 }
8361 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008362
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008363 ++ex_normal_busy;
8364 if (save_current_state(&save_state))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008365 {
8366 /*
8367 * Repeat the :normal command for each line in the range. When no
8368 * range given, execute it just once, without positioning the cursor
8369 * first.
8370 */
8371 do
8372 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008373 if (eap->addr_count != 0)
8374 {
8375 curwin->w_cursor.lnum = eap->line1++;
8376 curwin->w_cursor.col = 0;
Bram Moolenaard5d37532017-03-27 23:02:07 +02008377 check_cursor_moved(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008378 }
8379
Bram Moolenaar13505972019-01-24 15:04:48 +01008380 exec_normal_cmd(arg != NULL
8381 ? arg
8382 : eap->arg, eap->forceit ? REMAP_NONE : REMAP_YES, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008383 }
8384 while (eap->addr_count > 0 && eap->line1 <= eap->line2 && !got_int);
8385 }
8386
8387 /* Might not return to the main loop when in an event handler. */
8388 update_topline_cursor();
8389
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008390 restore_current_state(&save_state);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008391 --ex_normal_busy;
Bram Moolenaareda73602014-11-05 09:53:23 +01008392#ifdef FEAT_MOUSE
8393 setmouse();
8394#endif
8395#ifdef CURSOR_SHAPE
8396 ui_cursor_shape(); /* may show different cursor shape */
8397#endif
8398
Bram Moolenaar071d4272004-06-13 20:20:40 +00008399 vim_free(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008400}
8401
8402/*
Bram Moolenaar64969662005-12-14 21:59:55 +00008403 * ":startinsert", ":startreplace" and ":startgreplace"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008404 */
8405 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008406ex_startinsert(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008407{
Bram Moolenaarfd371682005-01-14 21:42:54 +00008408 if (eap->forceit)
8409 {
Bram Moolenaar09ca9322017-09-26 17:40:45 +02008410 /* cursor line can be zero on startup */
8411 if (!curwin->w_cursor.lnum)
8412 curwin->w_cursor.lnum = 1;
Bram Moolenaarfd371682005-01-14 21:42:54 +00008413 coladvance((colnr_T)MAXCOL);
8414 curwin->w_curswant = MAXCOL;
8415 curwin->w_set_curswant = FALSE;
8416 }
8417
Bram Moolenaara40c5002005-01-09 21:16:21 +00008418 /* Ignore the command when already in Insert mode. Inserting an
8419 * expression register that invokes a function can do this. */
8420 if (State & INSERT)
8421 return;
8422
Bram Moolenaar64969662005-12-14 21:59:55 +00008423 if (eap->cmdidx == CMD_startinsert)
8424 restart_edit = 'a';
8425 else if (eap->cmdidx == CMD_startreplace)
8426 restart_edit = 'R';
Bram Moolenaar071d4272004-06-13 20:20:40 +00008427 else
Bram Moolenaar64969662005-12-14 21:59:55 +00008428 restart_edit = 'V';
8429
8430 if (!eap->forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008431 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00008432 if (eap->cmdidx == CMD_startinsert)
8433 restart_edit = 'i';
Bram Moolenaar071d4272004-06-13 20:20:40 +00008434 curwin->w_curswant = 0; /* avoid MAXCOL */
8435 }
8436}
8437
8438/*
8439 * ":stopinsert"
8440 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008441 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008442ex_stopinsert(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008443{
8444 restart_edit = 0;
8445 stop_insert_mode = TRUE;
Bram Moolenaarfd773e92016-04-02 19:39:16 +02008446 clearmode();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008447}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008448
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008449/*
8450 * Execute normal mode command "cmd".
8451 * "remap" can be REMAP_NONE or REMAP_YES.
8452 */
8453 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008454exec_normal_cmd(char_u *cmd, int remap, int silent)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008455{
Bram Moolenaar25281632016-01-21 23:32:32 +01008456 /* Stuff the argument into the typeahead buffer. */
8457 ins_typebuf(cmd, remap, 0, TRUE, silent);
Bram Moolenaar586c70c2018-10-02 16:23:58 +02008458 exec_normal(FALSE, FALSE, FALSE);
Bram Moolenaar25281632016-01-21 23:32:32 +01008459}
Bram Moolenaar25281632016-01-21 23:32:32 +01008460
Bram Moolenaar25281632016-01-21 23:32:32 +01008461/*
8462 * Execute normal_cmd() until there is no typeahead left.
Bram Moolenaar586c70c2018-10-02 16:23:58 +02008463 * When "use_vpeekc" is TRUE use vpeekc() to check for available chars.
Bram Moolenaar25281632016-01-21 23:32:32 +01008464 */
8465 void
Bram Moolenaar586c70c2018-10-02 16:23:58 +02008466exec_normal(int was_typed, int use_vpeekc, int may_use_terminal_loop UNUSED)
Bram Moolenaar25281632016-01-21 23:32:32 +01008467{
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008468 oparg_T oa;
Bram Moolenaar905dd902019-04-07 14:21:47 +02008469 int c;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008470
Bram Moolenaar905dd902019-04-07 14:21:47 +02008471 // When calling vpeekc() from feedkeys() it will return Ctrl_C when there
8472 // is nothing to get, so also check for Ctrl_C.
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008473 clear_oparg(&oa);
8474 finish_op = FALSE;
Bram Moolenaar586c70c2018-10-02 16:23:58 +02008475 while ((!stuff_empty()
8476 || ((was_typed || !typebuf_typed()) && typebuf.tb_len > 0)
Bram Moolenaar905dd902019-04-07 14:21:47 +02008477 || (use_vpeekc && (c = vpeekc()) != NUL && c != Ctrl_C))
Bram Moolenaar586c70c2018-10-02 16:23:58 +02008478 && !got_int)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008479 {
8480 update_topline_cursor();
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02008481#ifdef FEAT_TERMINAL
Bram Moolenaar655a82a2018-05-08 22:01:07 +02008482 if (may_use_terminal_loop && term_use_loop()
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02008483 && oa.op_type == OP_NOP && oa.regname == NUL
8484 && !VIsual_active)
8485 {
8486 /* If terminal_loop() returns OK we got a key that is handled
8487 * in Normal model. With FAIL we first need to position the
8488 * cursor and the screen needs to be redrawn. */
8489 if (terminal_loop(TRUE) == OK)
8490 normal_cmd(&oa, TRUE);
8491 }
8492 else
8493#endif
8494 /* execute a Normal mode cmd */
8495 normal_cmd(&oa, TRUE);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008496 }
8497}
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008498
Bram Moolenaar071d4272004-06-13 20:20:40 +00008499#ifdef FEAT_FIND_ID
8500 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008501ex_checkpath(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008502{
8503 find_pattern_in_path(NULL, 0, 0, FALSE, FALSE, CHECK_PATH, 1L,
8504 eap->forceit ? ACTION_SHOW_ALL : ACTION_SHOW,
8505 (linenr_T)1, (linenr_T)MAXLNUM);
8506}
8507
Bram Moolenaar4033c552017-09-16 20:54:51 +02008508#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008509/*
8510 * ":psearch"
8511 */
8512 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008513ex_psearch(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008514{
8515 g_do_tagpreview = p_pvh;
8516 ex_findpat(eap);
8517 g_do_tagpreview = 0;
8518}
8519#endif
8520
8521 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008522ex_findpat(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008523{
8524 int whole = TRUE;
8525 long n;
8526 char_u *p;
8527 int action;
8528
8529 switch (cmdnames[eap->cmdidx].cmd_name[2])
8530 {
8531 case 'e': /* ":psearch", ":isearch" and ":dsearch" */
8532 if (cmdnames[eap->cmdidx].cmd_name[0] == 'p')
8533 action = ACTION_GOTO;
8534 else
8535 action = ACTION_SHOW;
8536 break;
8537 case 'i': /* ":ilist" and ":dlist" */
8538 action = ACTION_SHOW_ALL;
8539 break;
8540 case 'u': /* ":ijump" and ":djump" */
8541 action = ACTION_GOTO;
8542 break;
8543 default: /* ":isplit" and ":dsplit" */
8544 action = ACTION_SPLIT;
8545 break;
8546 }
8547
8548 n = 1;
8549 if (vim_isdigit(*eap->arg)) /* get count */
8550 {
8551 n = getdigits(&eap->arg);
8552 eap->arg = skipwhite(eap->arg);
8553 }
8554 if (*eap->arg == '/') /* Match regexp, not just whole words */
8555 {
8556 whole = FALSE;
8557 ++eap->arg;
8558 p = skip_regexp(eap->arg, '/', p_magic, NULL);
8559 if (*p)
8560 {
8561 *p++ = NUL;
8562 p = skipwhite(p);
8563
8564 /* Check for trailing illegal characters */
8565 if (!ends_excmd(*p))
8566 eap->errmsg = e_trailing;
8567 else
8568 eap->nextcmd = check_nextcmd(p);
8569 }
8570 }
8571 if (!eap->skip)
8572 find_pattern_in_path(eap->arg, 0, (int)STRLEN(eap->arg),
8573 whole, !eap->forceit,
8574 *eap->cmd == 'd' ? FIND_DEFINE : FIND_ANY,
8575 n, action, eap->line1, eap->line2);
8576}
8577#endif
8578
Bram Moolenaar071d4272004-06-13 20:20:40 +00008579
Bram Moolenaar4033c552017-09-16 20:54:51 +02008580#ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00008581/*
8582 * ":ptag", ":ptselect", ":ptjump", ":ptnext", etc.
8583 */
8584 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008585ex_ptag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008586{
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01008587 g_do_tagpreview = p_pvh; /* will be reset to 0 in ex_tag_cmd() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008588 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
8589}
8590
8591/*
8592 * ":pedit"
8593 */
8594 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008595ex_pedit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008596{
8597 win_T *curwin_save = curwin;
8598
Bram Moolenaar026587b2019-08-17 15:08:00 +02008599 // Open the preview window or popup and make it the current window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008600 g_do_tagpreview = p_pvh;
Bram Moolenaar576a4a62019-08-18 15:25:17 +02008601 prepare_tagpreview(TRUE, TRUE, FALSE);
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02008602
Bram Moolenaar026587b2019-08-17 15:08:00 +02008603 // Edit the file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008604 do_exedit(eap, NULL);
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02008605
Bram Moolenaar071d4272004-06-13 20:20:40 +00008606 if (curwin != curwin_save && win_valid(curwin_save))
8607 {
Bram Moolenaar026587b2019-08-17 15:08:00 +02008608 // Return cursor to where we were
Bram Moolenaar071d4272004-06-13 20:20:40 +00008609 validate_cursor();
8610 redraw_later(VALID);
8611 win_enter(curwin_save, TRUE);
8612 }
Bram Moolenaar1b6d9c42019-08-05 21:52:04 +02008613# ifdef FEAT_TEXT_PROP
8614 else if (WIN_IS_POPUP(curwin))
8615 {
8616 // can't keep focus in popup window
8617 win_enter(firstwin, TRUE);
8618 }
8619# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008620 g_do_tagpreview = 0;
8621}
Bram Moolenaar4033c552017-09-16 20:54:51 +02008622#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008623
8624/*
8625 * ":stag", ":stselect" and ":stjump".
8626 */
8627 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008628ex_stag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008629{
8630 postponed_split = -1;
8631 postponed_split_flags = cmdmod.split;
Bram Moolenaard326ce82007-03-11 14:48:29 +00008632 postponed_split_tab = cmdmod.tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008633 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
8634 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00008635 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008636}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008637
8638/*
8639 * ":tag", ":tselect", ":tjump", ":tnext", etc.
8640 */
8641 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008642ex_tag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008643{
8644 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name);
8645}
8646
8647 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008648ex_tag_cmd(exarg_T *eap, char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008649{
8650 int cmd;
8651
8652 switch (name[1])
8653 {
8654 case 'j': cmd = DT_JUMP; /* ":tjump" */
8655 break;
8656 case 's': cmd = DT_SELECT; /* ":tselect" */
8657 break;
8658 case 'p': cmd = DT_PREV; /* ":tprevious" */
8659 break;
8660 case 'N': cmd = DT_PREV; /* ":tNext" */
8661 break;
8662 case 'n': cmd = DT_NEXT; /* ":tnext" */
8663 break;
8664 case 'o': cmd = DT_POP; /* ":pop" */
8665 break;
8666 case 'f': /* ":tfirst" */
8667 case 'r': cmd = DT_FIRST; /* ":trewind" */
8668 break;
8669 case 'l': cmd = DT_LAST; /* ":tlast" */
8670 break;
8671 default: /* ":tag" */
8672#ifdef FEAT_CSCOPE
Bram Moolenaar7c94c262008-06-20 09:11:34 +00008673 if (p_cst && *eap->arg != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008674 {
Bram Moolenaard4db7712016-11-12 19:16:46 +01008675 ex_cstag(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008676 return;
8677 }
8678#endif
8679 cmd = DT_TAG;
8680 break;
8681 }
8682
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00008683 if (name[0] == 'l')
8684 {
8685#ifndef FEAT_QUICKFIX
8686 ex_ni(eap);
8687 return;
8688#else
8689 cmd = DT_LTAG;
8690#endif
8691 }
8692
Bram Moolenaar071d4272004-06-13 20:20:40 +00008693 do_tag(eap->arg, cmd, eap->addr_count > 0 ? (int)eap->line2 : 1,
8694 eap->forceit, TRUE);
8695}
8696
8697/*
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008698 * Check "str" for starting with a special cmdline variable.
8699 * If found return one of the SPEC_ values and set "*usedlen" to the length of
8700 * the variable. Otherwise return -1 and "*usedlen" is unchanged.
8701 */
8702 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008703find_cmdline_var(char_u *src, int *usedlen)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008704{
8705 int len;
8706 int i;
Bram Moolenaar8f0b2d42009-05-16 14:41:10 +00008707 static char *(spec_str[]) = {
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008708 "%",
8709#define SPEC_PERC 0
8710 "#",
Bram Moolenaar65f08472017-09-10 18:16:20 +02008711#define SPEC_HASH (SPEC_PERC + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008712 "<cword>", /* cursor word */
Bram Moolenaar65f08472017-09-10 18:16:20 +02008713#define SPEC_CWORD (SPEC_HASH + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008714 "<cWORD>", /* cursor WORD */
Bram Moolenaar65f08472017-09-10 18:16:20 +02008715#define SPEC_CCWORD (SPEC_CWORD + 1)
8716 "<cexpr>", /* expr under cursor */
8717#define SPEC_CEXPR (SPEC_CCWORD + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008718 "<cfile>", /* cursor path name */
Bram Moolenaar65f08472017-09-10 18:16:20 +02008719#define SPEC_CFILE (SPEC_CEXPR + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008720 "<sfile>", /* ":so" file name */
Bram Moolenaar65f08472017-09-10 18:16:20 +02008721#define SPEC_SFILE (SPEC_CFILE + 1)
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01008722 "<slnum>", /* ":so" file line number */
Bram Moolenaar65f08472017-09-10 18:16:20 +02008723#define SPEC_SLNUM (SPEC_SFILE + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008724 "<afile>", /* autocommand file name */
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008725#define SPEC_AFILE (SPEC_SLNUM + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008726 "<abuf>", /* autocommand buffer number */
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008727#define SPEC_ABUF (SPEC_AFILE + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008728 "<amatch>", /* autocommand match name */
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01008729#define SPEC_AMATCH (SPEC_ABUF + 1)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008730 "<sflnum>", /* script file line number */
8731#define SPEC_SFLNUM (SPEC_AMATCH + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008732#ifdef FEAT_CLIENTSERVER
8733 "<client>"
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008734# define SPEC_CLIENT (SPEC_SFLNUM + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008735#endif
8736 };
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008737
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00008738 for (i = 0; i < (int)(sizeof(spec_str) / sizeof(char *)); ++i)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008739 {
8740 len = (int)STRLEN(spec_str[i]);
8741 if (STRNCMP(src, spec_str[i], len) == 0)
8742 {
8743 *usedlen = len;
8744 return i;
8745 }
8746 }
8747 return -1;
8748}
8749
8750/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008751 * Evaluate cmdline variables.
8752 *
8753 * change '%' to curbuf->b_ffname
8754 * '#' to curwin->w_altfile
8755 * '<cword>' to word under the cursor
8756 * '<cWORD>' to WORD under the cursor
Bram Moolenaar8071cb22019-07-12 17:58:01 +02008757 * '<cexpr>' to C-expression under the cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00008758 * '<cfile>' to path name under the cursor
8759 * '<sfile>' to sourced file name
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01008760 * '<slnum>' to sourced file line number
Bram Moolenaar071d4272004-06-13 20:20:40 +00008761 * '<afile>' to file name for autocommand
8762 * '<abuf>' to buffer number for autocommand
8763 * '<amatch>' to matching name for autocommand
8764 *
8765 * When an error is detected, "errormsg" is set to a non-NULL pointer (may be
8766 * "" for error without a message) and NULL is returned.
8767 * Returns an allocated string if a valid match was found.
8768 * Returns NULL if no match was found. "usedlen" then still contains the
8769 * number of characters to skip.
8770 */
8771 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008772eval_vars(
8773 char_u *src, /* pointer into commandline */
8774 char_u *srcstart, /* beginning of valid memory for src */
8775 int *usedlen, /* characters after src that are used */
8776 linenr_T *lnump, /* line number for :e command, or NULL */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008777 char **errormsg, /* pointer to error message */
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008778 int *escaped) /* return value has escaped white space (can
Bram Moolenaar63b92542007-03-27 14:57:09 +00008779 * be NULL) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008780{
8781 int i;
8782 char_u *s;
8783 char_u *result;
8784 char_u *resultbuf = NULL;
8785 int resultlen;
8786 buf_T *buf;
8787 int valid = VALID_HEAD + VALID_PATH; /* assume valid result */
8788 int spec_idx;
8789#ifdef FEAT_MODIFY_FNAME
Bram Moolenaarfd249462018-07-28 16:14:30 +02008790 int tilde_file = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008791 int skip_mod = FALSE;
8792#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008793 char_u strbuf[30];
Bram Moolenaar071d4272004-06-13 20:20:40 +00008794
8795 *errormsg = NULL;
Bram Moolenaar63b92542007-03-27 14:57:09 +00008796 if (escaped != NULL)
8797 *escaped = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008798
8799 /*
8800 * Check if there is something to do.
8801 */
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008802 spec_idx = find_cmdline_var(src, usedlen);
8803 if (spec_idx < 0) /* no match */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008804 {
8805 *usedlen = 1;
8806 return NULL;
8807 }
8808
8809 /*
8810 * Skip when preceded with a backslash "\%" and "\#".
8811 * Note: In "\\%" the % is also not recognized!
8812 */
8813 if (src > srcstart && src[-1] == '\\')
8814 {
8815 *usedlen = 0;
Bram Moolenaara7241f52008-06-24 20:39:31 +00008816 STRMOVE(src - 1, src); /* remove backslash */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008817 return NULL;
8818 }
8819
8820 /*
8821 * word or WORD under cursor
8822 */
Bram Moolenaar65f08472017-09-10 18:16:20 +02008823 if (spec_idx == SPEC_CWORD || spec_idx == SPEC_CCWORD
8824 || spec_idx == SPEC_CEXPR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008825 {
Bram Moolenaar65f08472017-09-10 18:16:20 +02008826 resultlen = find_ident_under_cursor(&result,
8827 spec_idx == SPEC_CWORD ? (FIND_IDENT | FIND_STRING)
8828 : spec_idx == SPEC_CEXPR ? (FIND_IDENT | FIND_STRING | FIND_EVAL)
8829 : FIND_STRING);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008830 if (resultlen == 0)
8831 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008832 *errormsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +00008833 return NULL;
8834 }
8835 }
8836
8837 /*
8838 * '#': Alternate file name
8839 * '%': Current file name
8840 * File name under the cursor
8841 * File name for autocommand
8842 * and following modifiers
8843 */
8844 else
8845 {
8846 switch (spec_idx)
8847 {
8848 case SPEC_PERC: /* '%': current file */
8849 if (curbuf->b_fname == NULL)
8850 {
8851 result = (char_u *)"";
8852 valid = 0; /* Must have ":p:h" to be valid */
8853 }
8854 else
Bram Moolenaar00136dc2018-07-25 21:19:13 +02008855 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008856 result = curbuf->b_fname;
Bram Moolenaarfd249462018-07-28 16:14:30 +02008857#ifdef FEAT_MODIFY_FNAME
Bram Moolenaar00136dc2018-07-25 21:19:13 +02008858 tilde_file = STRCMP(result, "~") == 0;
Bram Moolenaarfd249462018-07-28 16:14:30 +02008859#endif
Bram Moolenaar00136dc2018-07-25 21:19:13 +02008860 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008861 break;
8862
8863 case SPEC_HASH: /* '#' or "#99": alternate file */
8864 if (src[1] == '#') /* "##": the argument list */
8865 {
8866 result = arg_all();
8867 resultbuf = result;
8868 *usedlen = 2;
Bram Moolenaar63b92542007-03-27 14:57:09 +00008869 if (escaped != NULL)
8870 *escaped = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008871#ifdef FEAT_MODIFY_FNAME
8872 skip_mod = TRUE;
8873#endif
8874 break;
8875 }
8876 s = src + 1;
Bram Moolenaard812df62008-11-09 12:46:09 +00008877 if (*s == '<') /* "#<99" uses v:oldfiles */
8878 ++s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008879 i = (int)getdigits(&s);
Bram Moolenaarc312b8b2017-10-28 17:53:04 +02008880 if (s == src + 2 && src[1] == '-')
8881 /* just a minus sign, don't skip over it */
8882 s--;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008883 *usedlen = (int)(s - src); /* length of what we expand */
8884
Bram Moolenaarc312b8b2017-10-28 17:53:04 +02008885 if (src[1] == '<' && i != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008886 {
Bram Moolenaard812df62008-11-09 12:46:09 +00008887 if (*usedlen < 2)
8888 {
8889 /* Should we give an error message for #<text? */
8890 *usedlen = 1;
8891 return NULL;
8892 }
8893#ifdef FEAT_EVAL
8894 result = list_find_str(get_vim_var_list(VV_OLDFILES),
8895 (long)i);
8896 if (result == NULL)
8897 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008898 *errormsg = "";
Bram Moolenaard812df62008-11-09 12:46:09 +00008899 return NULL;
8900 }
8901#else
Bram Moolenaar8e481e82019-01-15 20:07:48 +01008902 *errormsg = _("E809: #< is not available without the +eval feature");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008903 return NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +00008904#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008905 }
8906 else
Bram Moolenaard812df62008-11-09 12:46:09 +00008907 {
Bram Moolenaarc312b8b2017-10-28 17:53:04 +02008908 if (i == 0 && src[1] == '<' && *usedlen > 1)
8909 *usedlen = 1;
Bram Moolenaard812df62008-11-09 12:46:09 +00008910 buf = buflist_findnr(i);
8911 if (buf == NULL)
8912 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008913 *errormsg = _("E194: No alternate file name to substitute for '#'");
Bram Moolenaard812df62008-11-09 12:46:09 +00008914 return NULL;
8915 }
8916 if (lnump != NULL)
8917 *lnump = ECMD_LAST;
8918 if (buf->b_fname == NULL)
8919 {
8920 result = (char_u *)"";
8921 valid = 0; /* Must have ":p:h" to be valid */
8922 }
8923 else
Bram Moolenaar00136dc2018-07-25 21:19:13 +02008924 {
Bram Moolenaard812df62008-11-09 12:46:09 +00008925 result = buf->b_fname;
Bram Moolenaarfd249462018-07-28 16:14:30 +02008926#ifdef FEAT_MODIFY_FNAME
Bram Moolenaar00136dc2018-07-25 21:19:13 +02008927 tilde_file = STRCMP(result, "~") == 0;
Bram Moolenaarfd249462018-07-28 16:14:30 +02008928#endif
Bram Moolenaar00136dc2018-07-25 21:19:13 +02008929 }
Bram Moolenaard812df62008-11-09 12:46:09 +00008930 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008931 break;
8932
8933#ifdef FEAT_SEARCHPATH
8934 case SPEC_CFILE: /* file name under cursor */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00008935 result = file_name_at_cursor(FNAME_MESS|FNAME_HYP, 1L, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008936 if (result == NULL)
8937 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008938 *errormsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +00008939 return NULL;
8940 }
8941 resultbuf = result; /* remember allocated string */
8942 break;
8943#endif
8944
Bram Moolenaar071d4272004-06-13 20:20:40 +00008945 case SPEC_AFILE: /* file name for autocommand */
8946 result = autocmd_fname;
Bram Moolenaarf6dad432008-09-18 19:29:58 +00008947 if (result != NULL && !autocmd_fname_full)
8948 {
8949 /* Still need to turn the fname into a full path. It is
8950 * postponed to avoid a delay when <afile> is not used. */
8951 autocmd_fname_full = TRUE;
8952 result = FullName_save(autocmd_fname, FALSE);
8953 vim_free(autocmd_fname);
8954 autocmd_fname = result;
8955 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008956 if (result == NULL)
8957 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008958 *errormsg = _("E495: no autocommand file name to substitute for \"<afile>\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008959 return NULL;
8960 }
Bram Moolenaara0174af2008-01-02 20:08:25 +00008961 result = shorten_fname1(result);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008962 break;
8963
8964 case SPEC_ABUF: /* buffer number for autocommand */
8965 if (autocmd_bufnr <= 0)
8966 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008967 *errormsg = _("E496: no autocommand buffer number to substitute for \"<abuf>\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008968 return NULL;
8969 }
8970 sprintf((char *)strbuf, "%d", autocmd_bufnr);
8971 result = strbuf;
8972 break;
8973
8974 case SPEC_AMATCH: /* match name for autocommand */
8975 result = autocmd_match;
8976 if (result == NULL)
8977 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008978 *errormsg = _("E497: no autocommand match name to substitute for \"<amatch>\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008979 return NULL;
8980 }
8981 break;
8982
Bram Moolenaar071d4272004-06-13 20:20:40 +00008983 case SPEC_SFILE: /* file name for ":so" command */
8984 result = sourcing_name;
8985 if (result == NULL)
8986 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008987 *errormsg = _("E498: no :source file name to substitute for \"<sfile>\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00008988 return NULL;
8989 }
8990 break;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008991
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01008992 case SPEC_SLNUM: /* line in file for ":so" command */
8993 if (sourcing_name == NULL || sourcing_lnum == 0)
8994 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008995 *errormsg = _("E842: no line number to use for \"<slnum>\"");
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01008996 return NULL;
8997 }
8998 sprintf((char *)strbuf, "%ld", (long)sourcing_lnum);
8999 result = strbuf;
9000 break;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009001
9002#ifdef FEAT_EVAL
9003 case SPEC_SFLNUM: /* line in script file */
9004 if (current_sctx.sc_lnum + sourcing_lnum == 0)
9005 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009006 *errormsg = _("E961: no line number to use for \"<sflnum>\"");
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009007 return NULL;
9008 }
9009 sprintf((char *)strbuf, "%ld",
9010 (long)(current_sctx.sc_lnum + sourcing_lnum));
9011 result = strbuf;
9012 break;
9013#endif
9014
9015#ifdef FEAT_CLIENTSERVER
Bram Moolenaar071d4272004-06-13 20:20:40 +00009016 case SPEC_CLIENT: /* Source of last submitted input */
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009017 sprintf((char *)strbuf, PRINTF_HEX_LONG_U,
9018 (long_u)clientWindow);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009019 result = strbuf;
9020 break;
9021#endif
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009022
Bram Moolenaar9e0f6ec2017-05-16 09:36:54 +02009023 default:
9024 result = (char_u *)""; /* avoid gcc warning */
9025 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009026 }
9027
9028 resultlen = (int)STRLEN(result); /* length of new string */
9029 if (src[*usedlen] == '<') /* remove the file name extension */
9030 {
9031 ++*usedlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009032 if ((s = vim_strrchr(result, '.')) != NULL && s >= gettail(result))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009033 resultlen = (int)(s - result);
9034 }
9035#ifdef FEAT_MODIFY_FNAME
9036 else if (!skip_mod)
9037 {
Bram Moolenaar00136dc2018-07-25 21:19:13 +02009038 valid |= modify_fname(src, tilde_file, usedlen, &result, &resultbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009039 &resultlen);
9040 if (result == NULL)
9041 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009042 *errormsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +00009043 return NULL;
9044 }
9045 }
9046#endif
9047 }
9048
9049 if (resultlen == 0 || valid != VALID_HEAD + VALID_PATH)
9050 {
9051 if (valid != VALID_HEAD + VALID_PATH)
9052 /* xgettext:no-c-format */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009053 *errormsg = _("E499: Empty file name for '%' or '#', only works with \":p:h\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00009054 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009055 *errormsg = _("E500: Evaluates to an empty string");
Bram Moolenaar071d4272004-06-13 20:20:40 +00009056 result = NULL;
9057 }
9058 else
9059 result = vim_strnsave(result, resultlen);
9060 vim_free(resultbuf);
9061 return result;
9062}
9063
9064/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009065 * Expand the <sfile> string in "arg".
9066 *
9067 * Returns an allocated string, or NULL for any error.
9068 */
9069 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009070expand_sfile(char_u *arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009071{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009072 char *errormsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009073 int len;
9074 char_u *result;
9075 char_u *newres;
9076 char_u *repl;
9077 int srclen;
9078 char_u *p;
9079
9080 result = vim_strsave(arg);
9081 if (result == NULL)
9082 return NULL;
9083
9084 for (p = result; *p; )
9085 {
9086 if (STRNCMP(p, "<sfile>", 7) != 0)
9087 ++p;
9088 else
9089 {
9090 /* replace "<sfile>" with the sourced file name, and do ":" stuff */
Bram Moolenaar63b92542007-03-27 14:57:09 +00009091 repl = eval_vars(p, result, &srclen, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009092 if (errormsg != NULL)
9093 {
9094 if (*errormsg)
9095 emsg(errormsg);
9096 vim_free(result);
9097 return NULL;
9098 }
9099 if (repl == NULL) /* no match (cannot happen) */
9100 {
9101 p += srclen;
9102 continue;
9103 }
9104 len = (int)STRLEN(result) - srclen + (int)STRLEN(repl) + 1;
9105 newres = alloc(len);
9106 if (newres == NULL)
9107 {
9108 vim_free(repl);
9109 vim_free(result);
9110 return NULL;
9111 }
9112 mch_memmove(newres, result, (size_t)(p - result));
9113 STRCPY(newres + (p - result), repl);
9114 len = (int)STRLEN(newres);
9115 STRCAT(newres, p + srclen);
9116 vim_free(repl);
9117 vim_free(result);
9118 result = newres;
9119 p = newres + len; /* continue after the match */
9120 }
9121 }
9122
9123 return result;
9124}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009125
Bram Moolenaar071d4272004-06-13 20:20:40 +00009126#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG) || defined(PROTO)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00009127/*
Bram Moolenaard9462e32011-04-11 21:35:11 +02009128 * Make a dialog message in "buff[DIALOG_MSG_SIZE]".
Bram Moolenaarb765d632005-06-07 21:00:02 +00009129 * "format" must contain "%s".
Bram Moolenaar9c13b352005-05-19 20:53:52 +00009130 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009131 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009132dialog_msg(char_u *buff, char *format, char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009133{
Bram Moolenaar071d4272004-06-13 20:20:40 +00009134 if (fname == NULL)
9135 fname = (char_u *)_("Untitled");
Bram Moolenaard9462e32011-04-11 21:35:11 +02009136 vim_snprintf((char *)buff, DIALOG_MSG_SIZE, format, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009137}
9138#endif
9139
9140/*
9141 * ":behave {mswin,xterm}"
9142 */
9143 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009144ex_behave(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009145{
9146 if (STRCMP(eap->arg, "mswin") == 0)
9147 {
9148 set_option_value((char_u *)"selection", 0L, (char_u *)"exclusive", 0);
9149 set_option_value((char_u *)"selectmode", 0L, (char_u *)"mouse,key", 0);
9150 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"popup", 0);
9151 set_option_value((char_u *)"keymodel", 0L,
9152 (char_u *)"startsel,stopsel", 0);
9153 }
9154 else if (STRCMP(eap->arg, "xterm") == 0)
9155 {
9156 set_option_value((char_u *)"selection", 0L, (char_u *)"inclusive", 0);
9157 set_option_value((char_u *)"selectmode", 0L, (char_u *)"", 0);
9158 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"extend", 0);
9159 set_option_value((char_u *)"keymodel", 0L, (char_u *)"", 0);
9160 }
9161 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009162 semsg(_(e_invarg2), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009163}
9164
Bram Moolenaar42b4dda2010-03-02 15:56:05 +01009165/*
9166 * Function given to ExpandGeneric() to obtain the possible arguments of the
9167 * ":behave {mswin,xterm}" command.
9168 */
9169 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009170get_behave_arg(expand_T *xp UNUSED, int idx)
Bram Moolenaar42b4dda2010-03-02 15:56:05 +01009171{
9172 if (idx == 0)
9173 return (char_u *)"mswin";
9174 if (idx == 1)
9175 return (char_u *)"xterm";
9176 return NULL;
9177}
Bram Moolenaar9e507ca2016-10-15 15:39:39 +02009178
9179/*
9180 * Function given to ExpandGeneric() to obtain the possible arguments of the
9181 * ":messages {clear}" command.
9182 */
9183 char_u *
9184get_messages_arg(expand_T *xp UNUSED, int idx)
9185{
9186 if (idx == 0)
9187 return (char_u *)"clear";
9188 return NULL;
9189}
Bram Moolenaar42b4dda2010-03-02 15:56:05 +01009190
Bram Moolenaarcae92dc2017-08-06 15:22:15 +02009191 char_u *
9192get_mapclear_arg(expand_T *xp UNUSED, int idx)
9193{
9194 if (idx == 0)
9195 return (char_u *)"<buffer>";
9196 return NULL;
9197}
9198
Bram Moolenaar071d4272004-06-13 20:20:40 +00009199static int filetype_detect = FALSE;
9200static int filetype_plugin = FALSE;
9201static int filetype_indent = FALSE;
9202
9203/*
9204 * ":filetype [plugin] [indent] {on,off,detect}"
9205 * on: Load the filetype.vim file to install autocommands for file types.
9206 * off: Load the ftoff.vim file to remove all autocommands for file types.
9207 * plugin on: load filetype.vim and ftplugin.vim
9208 * plugin off: load ftplugof.vim
9209 * indent on: load filetype.vim and indent.vim
9210 * indent off: load indoff.vim
9211 */
9212 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009213ex_filetype(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009214{
9215 char_u *arg = eap->arg;
9216 int plugin = FALSE;
9217 int indent = FALSE;
9218
9219 if (*eap->arg == NUL)
9220 {
9221 /* Print current status. */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009222 smsg("filetype detection:%s plugin:%s indent:%s",
Bram Moolenaar071d4272004-06-13 20:20:40 +00009223 filetype_detect ? "ON" : "OFF",
9224 filetype_plugin ? (filetype_detect ? "ON" : "(on)") : "OFF",
9225 filetype_indent ? (filetype_detect ? "ON" : "(on)") : "OFF");
9226 return;
9227 }
9228
9229 /* Accept "plugin" and "indent" in any order. */
9230 for (;;)
9231 {
9232 if (STRNCMP(arg, "plugin", 6) == 0)
9233 {
9234 plugin = TRUE;
9235 arg = skipwhite(arg + 6);
9236 continue;
9237 }
9238 if (STRNCMP(arg, "indent", 6) == 0)
9239 {
9240 indent = TRUE;
9241 arg = skipwhite(arg + 6);
9242 continue;
9243 }
9244 break;
9245 }
9246 if (STRCMP(arg, "on") == 0 || STRCMP(arg, "detect") == 0)
9247 {
9248 if (*arg == 'o' || !filetype_detect)
9249 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01009250 source_runtime((char_u *)FILETYPE_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009251 filetype_detect = TRUE;
9252 if (plugin)
9253 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01009254 source_runtime((char_u *)FTPLUGIN_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009255 filetype_plugin = TRUE;
9256 }
9257 if (indent)
9258 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01009259 source_runtime((char_u *)INDENT_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009260 filetype_indent = TRUE;
9261 }
9262 }
9263 if (*arg == 'd')
9264 {
Bram Moolenaar1610d052016-06-09 22:53:01 +02009265 (void)do_doautocmd((char_u *)"filetypedetect BufRead", TRUE, NULL);
Bram Moolenaara3227e22006-03-08 21:32:40 +00009266 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009267 }
9268 }
9269 else if (STRCMP(arg, "off") == 0)
9270 {
9271 if (plugin || indent)
9272 {
9273 if (plugin)
9274 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01009275 source_runtime((char_u *)FTPLUGOF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009276 filetype_plugin = FALSE;
9277 }
9278 if (indent)
9279 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01009280 source_runtime((char_u *)INDOFF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009281 filetype_indent = FALSE;
9282 }
9283 }
9284 else
9285 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01009286 source_runtime((char_u *)FTOFF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009287 filetype_detect = FALSE;
9288 }
9289 }
9290 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009291 semsg(_(e_invarg2), arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009292}
9293
9294/*
Bram Moolenaar3e545692017-06-04 19:00:32 +02009295 * ":setfiletype [FALLBACK] {name}"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009296 */
9297 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009298ex_setfiletype(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009299{
9300 if (!did_filetype)
Bram Moolenaar3e545692017-06-04 19:00:32 +02009301 {
9302 char_u *arg = eap->arg;
9303
9304 if (STRNCMP(arg, "FALLBACK ", 9) == 0)
9305 arg += 9;
9306
9307 set_option_value((char_u *)"filetype", 0L, arg, OPT_LOCAL);
9308 if (arg != eap->arg)
9309 did_filetype = FALSE;
9310 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009311}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009312
Bram Moolenaar071d4272004-06-13 20:20:40 +00009313 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009314ex_digraphs(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009315{
9316#ifdef FEAT_DIGRAPHS
9317 if (*eap->arg != NUL)
9318 putdigraph(eap->arg);
9319 else
Bram Moolenaareae8ae12018-12-14 18:53:02 +01009320 listdigraphs(eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009321#else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009322 emsg(_("E196: No digraphs in this version"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009323#endif
9324}
9325
9326 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009327ex_set(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009328{
9329 int flags = 0;
9330
9331 if (eap->cmdidx == CMD_setlocal)
9332 flags = OPT_LOCAL;
9333 else if (eap->cmdidx == CMD_setglobal)
9334 flags = OPT_GLOBAL;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01009335#if defined(FEAT_EVAL) && defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009336 if (cmdmod.browse && flags == 0)
9337 ex_options(eap);
9338 else
9339#endif
9340 (void)do_set(eap->arg, flags);
9341}
9342
Bram Moolenaar451fc7b2018-04-27 22:53:07 +02009343#if defined(FEAT_SEARCH_EXTRA) || defined(PROTO)
9344 void
9345set_no_hlsearch(int flag)
9346{
9347 no_hlsearch = flag;
9348# ifdef FEAT_EVAL
9349 set_vim_var_nr(VV_HLSEARCH, !no_hlsearch && p_hls);
9350# endif
9351}
9352
Bram Moolenaar071d4272004-06-13 20:20:40 +00009353/*
9354 * ":nohlsearch"
9355 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009356 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009357ex_nohlsearch(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009358{
Bram Moolenaar451fc7b2018-04-27 22:53:07 +02009359 set_no_hlsearch(TRUE);
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00009360 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009361}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009362#endif
9363
9364#ifdef FEAT_CRYPT
9365/*
9366 * ":X": Get crypt key
9367 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009368 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009369ex_X(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009370{
Bram Moolenaar3a0c9082014-11-12 15:15:42 +01009371 crypt_check_current_method();
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02009372 (void)crypt_get_key(TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009373}
9374#endif
9375
9376#ifdef FEAT_FOLDING
9377 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009378ex_fold(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009379{
9380 if (foldManualAllowed(TRUE))
9381 foldCreate(eap->line1, eap->line2);
9382}
9383
9384 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009385ex_foldopen(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009386{
9387 opFoldRange(eap->line1, eap->line2, eap->cmdidx == CMD_foldopen,
9388 eap->forceit, FALSE);
9389}
9390
9391 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009392ex_folddo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009393{
9394 linenr_T lnum;
9395
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02009396#ifdef FEAT_CLIPBOARD
9397 start_global_changes();
9398#endif
9399
Bram Moolenaar071d4272004-06-13 20:20:40 +00009400 /* First set the marks for all lines closed/open. */
9401 for (lnum = eap->line1; lnum <= eap->line2; ++lnum)
9402 if (hasFolding(lnum, NULL, NULL) == (eap->cmdidx == CMD_folddoclosed))
9403 ml_setmarked(lnum);
9404
9405 /* Execute the command on the marked lines. */
9406 global_exe(eap->arg);
9407 ml_clearmarked(); /* clear rest of the marks */
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02009408#ifdef FEAT_CLIPBOARD
9409 end_global_changes();
9410#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009411}
9412#endif
Bram Moolenaarf5291f32017-09-14 22:55:37 +02009413
Bram Moolenaar39665952018-08-15 20:59:48 +02009414#ifdef FEAT_QUICKFIX
9415/*
9416 * Returns TRUE if the supplied Ex cmdidx is for a location list command
9417 * instead of a quickfix command.
9418 */
9419 int
9420is_loclist_cmd(int cmdidx)
9421{
Bram Moolenaar74c8be22018-08-23 22:51:40 +02009422 if (cmdidx < 0 || cmdidx >= CMD_SIZE)
Bram Moolenaar39665952018-08-15 20:59:48 +02009423 return FALSE;
9424 return cmdnames[cmdidx].cmd_name[0] == 'l';
9425}
9426#endif
9427
Bram Moolenaarf5291f32017-09-14 22:55:37 +02009428# if defined(FEAT_TIMERS) || defined(PROTO)
9429 int
9430get_pressedreturn(void)
9431{
9432 return ex_pressedreturn;
9433}
9434
9435 void
9436set_pressedreturn(int val)
9437{
9438 ex_pressedreturn = val;
9439}
9440#endif