blob: 514985633a7674c1832390277ebf62ea1e3a1c2b [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 Moolenaarf2bd8ef2018-03-04 18:08:14 +0100164#ifndef FEAT_EVAL
165# define ex_packadd ex_ni
166# define ex_packloadall ex_ni
167#endif
Bram Moolenaarf7512552013-06-06 14:55:19 +0200168#if !defined(FEAT_SYN_HL) || !defined(FEAT_PROFILE)
Bram Moolenaar8a7f5a22013-06-06 14:01:46 +0200169# define ex_syntime ex_ni
170#endif
Bram Moolenaarf71a3db2006-03-12 21:50:18 +0000171#ifndef FEAT_SPELL
Bram Moolenaarb765d632005-06-07 21:00:02 +0000172# define ex_spell ex_ni
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000173# define ex_mkspell ex_ni
Bram Moolenaarf417f2b2005-06-23 22:29:21 +0000174# define ex_spelldump ex_ni
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000175# define ex_spellinfo ex_ni
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000176# define ex_spellrepall ex_ni
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000177#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200178#ifndef FEAT_PERSISTENT_UNDO
179# define ex_rundo ex_ni
180# define ex_wundo ex_ni
181#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200182#ifndef FEAT_LUA
183# define ex_lua ex_script_ni
184# define ex_luado ex_ni
185# define ex_luafile ex_ni
186#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000187#ifndef FEAT_MZSCHEME
188# define ex_mzscheme ex_script_ni
189# define ex_mzfile ex_ni
190#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000191#ifndef FEAT_PERL
192# define ex_perl ex_script_ni
193# define ex_perldo ex_ni
194#endif
195#ifndef FEAT_PYTHON
196# define ex_python ex_script_ni
Bram Moolenaard620aa92013-05-17 16:40:06 +0200197# define ex_pydo ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000198# define ex_pyfile ex_ni
199#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200200#ifndef FEAT_PYTHON3
Bram Moolenaar368373e2010-07-19 20:46:22 +0200201# define ex_py3 ex_script_ni
Bram Moolenaar3dab2802013-05-15 18:28:13 +0200202# define ex_py3do ex_ni
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200203# define ex_py3file ex_ni
204#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +0100205#if !defined(FEAT_PYTHON) && !defined(FEAT_PYTHON3)
206# define ex_pyx ex_script_ni
207# define ex_pyxdo ex_ni
208# define ex_pyxfile ex_ni
209#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000210#ifndef FEAT_TCL
211# define ex_tcl ex_script_ni
212# define ex_tcldo ex_ni
213# define ex_tclfile ex_ni
214#endif
215#ifndef FEAT_RUBY
216# define ex_ruby ex_script_ni
217# define ex_rubydo ex_ni
218# define ex_rubyfile ex_ni
219#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000220#ifndef FEAT_KEYMAP
221# define ex_loadkeymap ex_ni
222#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100223static void ex_swapname(exarg_T *eap);
224static void ex_syncbind(exarg_T *eap);
225static void ex_read(exarg_T *eap);
226static void ex_pwd(exarg_T *eap);
227static void ex_equal(exarg_T *eap);
228static void ex_sleep(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100229static void ex_winsize(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100230static void ex_wincmd(exarg_T *eap);
Bram Moolenaar843ee412004-06-30 16:16:41 +0000231#if defined(FEAT_GUI) || defined(UNIX) || defined(VMS) || defined(MSWIN)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100232static void ex_winpos(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000233#else
234# define ex_winpos ex_ni
235#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100236static void ex_operators(exarg_T *eap);
237static void ex_put(exarg_T *eap);
238static void ex_copymove(exarg_T *eap);
239static void ex_submagic(exarg_T *eap);
240static void ex_join(exarg_T *eap);
241static void ex_at(exarg_T *eap);
242static void ex_bang(exarg_T *eap);
243static void ex_undo(exarg_T *eap);
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200244#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100245static void ex_wundo(exarg_T *eap);
246static void ex_rundo(exarg_T *eap);
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200247#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100248static void ex_redo(exarg_T *eap);
249static void ex_later(exarg_T *eap);
250static void ex_redir(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100251static void ex_redrawstatus(exarg_T *eap);
Bram Moolenaare12bab32019-01-08 22:02:56 +0100252static void ex_redrawtabline(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100253static void close_redir(void);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100254static void ex_mark(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100255static void ex_startinsert(exarg_T *eap);
256static void ex_stopinsert(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000257#ifdef FEAT_FIND_ID
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100258static void ex_checkpath(exarg_T *eap);
259static void ex_findpat(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000260#else
261# define ex_findpat ex_ni
262# define ex_checkpath ex_ni
263#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +0200264#if defined(FEAT_FIND_ID) && defined(FEAT_QUICKFIX)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100265static void ex_psearch(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000266#else
267# define ex_psearch ex_ni
268#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100269static void ex_tag(exarg_T *eap);
270static void ex_tag_cmd(exarg_T *eap, char_u *name);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000271#ifndef FEAT_EVAL
272# define ex_scriptnames ex_ni
273# define ex_finish ex_ni
274# define ex_echo ex_ni
275# define ex_echohl ex_ni
276# define ex_execute ex_ni
277# define ex_call ex_ni
278# define ex_if ex_ni
279# define ex_endif ex_ni
280# define ex_else ex_ni
281# define ex_while ex_ni
282# define ex_continue ex_ni
283# define ex_break ex_ni
284# define ex_endwhile ex_ni
285# define ex_throw ex_ni
286# define ex_try ex_ni
287# define ex_catch ex_ni
288# define ex_finally ex_ni
289# define ex_endtry ex_ni
290# define ex_endfunction ex_ni
291# define ex_let ex_ni
Bram Moolenaar0abb4272019-06-15 16:06:00 +0200292# define ex_const ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000293# define ex_unlet ex_ni
Bram Moolenaar65c1b012005-01-31 19:02:28 +0000294# define ex_lockvar ex_ni
295# define ex_unlockvar ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000296# define ex_function ex_ni
297# define ex_delfunction ex_ni
298# define ex_return ex_ni
Bram Moolenaard812df62008-11-09 12:46:09 +0000299# define ex_oldfiles ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000300#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100301static char_u *arg_all(void);
Bram Moolenaar84538072019-07-28 14:15:42 +0200302#ifndef FEAT_SESSION
Bram Moolenaar071d4272004-06-13 20:20:40 +0000303# define ex_loadview ex_ni
304#endif
305#ifndef FEAT_EVAL
306# define ex_compiler ex_ni
307#endif
Bram Moolenaardefa0672019-07-21 19:25:37 +0200308#ifndef FEAT_VIMINFO
Bram Moolenaar071d4272004-06-13 20:20:40 +0000309# define ex_viminfo ex_ni
310#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100311static void ex_behave(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100312static void ex_filetype(exarg_T *eap);
313static void ex_setfiletype(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000314#ifndef FEAT_DIFF
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000315# define ex_diffoff ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000316# define ex_diffpatch ex_ni
317# define ex_diffgetput ex_ni
318# define ex_diffsplit ex_ni
319# define ex_diffthis ex_ni
320# define ex_diffupdate ex_ni
321#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100322static void ex_digraphs(exarg_T *eap);
323static void ex_set(exarg_T *eap);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100324#if !defined(FEAT_EVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000325# define ex_options ex_ni
326#endif
327#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100328static void ex_nohlsearch(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000329#else
330# define ex_nohlsearch ex_ni
331# define ex_match ex_ni
332#endif
333#ifdef FEAT_CRYPT
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100334static void ex_X(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000335#else
336# define ex_X ex_ni
337#endif
338#ifdef FEAT_FOLDING
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100339static void ex_fold(exarg_T *eap);
340static void ex_foldopen(exarg_T *eap);
341static void ex_folddo(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000342#else
343# define ex_fold ex_ni
344# define ex_foldopen ex_ni
345# define ex_folddo ex_ni
346#endif
Bram Moolenaar13505972019-01-24 15:04:48 +0100347#if !(defined(HAVE_LOCALE_H) || defined(X_LOCALE))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000348# define ex_language ex_ni
349#endif
350#ifndef FEAT_SIGNS
351# define ex_sign ex_ni
352#endif
Bram Moolenaar009b2592004-10-24 19:18:58 +0000353#ifndef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200354# define ex_nbclose ex_ni
Bram Moolenaar009b2592004-10-24 19:18:58 +0000355# define ex_nbkey ex_ni
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200356# define ex_nbstart ex_ni
Bram Moolenaar009b2592004-10-24 19:18:58 +0000357#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000358
359#ifndef FEAT_EVAL
360# define ex_debug ex_ni
361# define ex_breakadd ex_ni
362# define ex_debuggreedy ex_ni
363# define ex_breakdel ex_ni
364# define ex_breaklist ex_ni
365#endif
366
367#ifndef FEAT_CMDHIST
368# define ex_history ex_ni
369#endif
370#ifndef FEAT_JUMPLIST
371# define ex_jumps ex_ni
Bram Moolenaar2d358992016-06-12 21:20:54 +0200372# define ex_clearjumps ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000373# define ex_changes ex_ni
374#endif
375
Bram Moolenaar05159a02005-02-26 23:04:13 +0000376#ifndef FEAT_PROFILE
377# define ex_profile ex_ni
378#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200379#ifndef FEAT_TERMINAL
380# define ex_terminal ex_ni
381#endif
Bram Moolenaard4aa83a2019-05-09 18:59:31 +0200382#if !defined(FEAT_X11) || !defined(FEAT_XCLIPBOARD)
383# define ex_xrestore ex_ni
384#endif
Bram Moolenaar682725c2019-05-25 20:10:37 +0200385#if !defined(FEAT_TEXT_PROP)
386# define ex_popupclear ex_ni
387#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +0000388
Bram Moolenaar071d4272004-06-13 20:20:40 +0000389/*
390 * Declare cmdnames[].
391 */
392#define DO_DECLARE_EXCMD
393#include "ex_cmds.h"
Bram Moolenaar6de5e122017-04-20 21:55:44 +0200394#include "ex_cmdidxs.h"
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +0100395
Bram Moolenaar071d4272004-06-13 20:20:40 +0000396static char_u dollar_command[2] = {'$', 0};
397
398
399#ifdef FEAT_EVAL
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000400/* Struct for storing a line inside a while/for loop */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000401typedef struct
402{
403 char_u *line; /* command line */
404 linenr_T lnum; /* sourcing_lnum of the line */
405} wcmd_T;
406
407/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000408 * Structure used to store info for line position in a while or for loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000409 * This is required, because do_one_cmd() may invoke ex_function(), which
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000410 * reads more lines that may come from the while/for loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000411 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000412struct loop_cookie
Bram Moolenaar071d4272004-06-13 20:20:40 +0000413{
414 garray_T *lines_gap; /* growarray with line info */
415 int current_line; /* last read line from growarray */
416 int repeating; /* TRUE when looping a second time */
417 /* When "repeating" is FALSE use "getline" and "cookie" to get lines */
Bram Moolenaare96a2492019-06-25 04:12:16 +0200418 char_u *(*getline)(int, void *, int, int);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000419 void *cookie;
420};
421
Bram Moolenaare96a2492019-06-25 04:12:16 +0200422static char_u *get_loop_line(int c, void *cookie, int indent, int do_concat);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100423static int store_loop_line(garray_T *gap, char_u *line);
424static void free_cmdlines(garray_T *gap);
Bram Moolenaared203462004-06-16 11:19:22 +0000425
426/* Struct to save a few things while debugging. Used in do_cmdline() only. */
427struct dbg_stuff
428{
429 int trylevel;
430 int force_abort;
431 except_T *caught_stack;
432 char_u *vv_exception;
433 char_u *vv_throwpoint;
434 int did_emsg;
435 int got_int;
436 int did_throw;
437 int need_rethrow;
438 int check_cstack;
439 except_T *current_exception;
440};
441
Bram Moolenaared203462004-06-16 11:19:22 +0000442 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100443save_dbg_stuff(struct dbg_stuff *dsp)
Bram Moolenaared203462004-06-16 11:19:22 +0000444{
445 dsp->trylevel = trylevel; trylevel = 0;
446 dsp->force_abort = force_abort; force_abort = FALSE;
447 dsp->caught_stack = caught_stack; caught_stack = NULL;
448 dsp->vv_exception = v_exception(NULL);
449 dsp->vv_throwpoint = v_throwpoint(NULL);
450
451 /* Necessary for debugging an inactive ":catch", ":finally", ":endtry" */
452 dsp->did_emsg = did_emsg; did_emsg = FALSE;
453 dsp->got_int = got_int; got_int = FALSE;
454 dsp->did_throw = did_throw; did_throw = FALSE;
455 dsp->need_rethrow = need_rethrow; need_rethrow = FALSE;
456 dsp->check_cstack = check_cstack; check_cstack = FALSE;
457 dsp->current_exception = current_exception; current_exception = NULL;
458}
459
460 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100461restore_dbg_stuff(struct dbg_stuff *dsp)
Bram Moolenaared203462004-06-16 11:19:22 +0000462{
463 suppress_errthrow = FALSE;
464 trylevel = dsp->trylevel;
465 force_abort = dsp->force_abort;
466 caught_stack = dsp->caught_stack;
467 (void)v_exception(dsp->vv_exception);
468 (void)v_throwpoint(dsp->vv_throwpoint);
469 did_emsg = dsp->did_emsg;
470 got_int = dsp->got_int;
471 did_throw = dsp->did_throw;
472 need_rethrow = dsp->need_rethrow;
473 check_cstack = dsp->check_cstack;
474 current_exception = dsp->current_exception;
475}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000476#endif
477
Bram Moolenaar071d4272004-06-13 20:20:40 +0000478/*
479 * do_exmode(): Repeatedly get commands for the "Ex" mode, until the ":vi"
480 * command is given.
481 */
482 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100483do_exmode(
484 int improved) /* TRUE for "improved Ex" mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000485{
486 int save_msg_scroll;
487 int prev_msg_row;
488 linenr_T prev_line;
Bram Moolenaar79518e22017-02-17 16:31:35 +0100489 varnumber_T changedtick;
Bram Moolenaardf177f62005-02-22 08:39:57 +0000490
491 if (improved)
492 exmode_active = EXMODE_VIM;
493 else
494 exmode_active = EXMODE_NORMAL;
495 State = NORMAL;
496
497 /* When using ":global /pat/ visual" and then "Q" we return to continue
498 * the :global command. */
499 if (global_busy)
500 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000501
502 save_msg_scroll = msg_scroll;
503 ++RedrawingDisabled; /* don't redisplay the window */
504 ++no_wait_return; /* don't wait for return */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000505#ifdef FEAT_GUI
506 /* Ignore scrollbar and mouse events in Ex mode */
507 ++hold_gui_events;
508#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000509
Bram Moolenaar32526b32019-01-19 17:43:09 +0100510 msg(_("Entering Ex mode. Type \"visual\" to go to Normal mode."));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000511 while (exmode_active)
512 {
Bram Moolenaar7c626922005-02-07 22:01:03 +0000513 /* Check for a ":normal" command and no more characters left. */
514 if (ex_normal_busy > 0 && typebuf.tb_len == 0)
515 {
516 exmode_active = FALSE;
517 break;
518 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000519 msg_scroll = TRUE;
520 need_wait_return = FALSE;
521 ex_pressedreturn = FALSE;
522 ex_no_reprint = FALSE;
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100523 changedtick = CHANGEDTICK(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000524 prev_msg_row = msg_row;
525 prev_line = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000526 if (improved)
527 {
528 cmdline_row = msg_row;
529 do_cmdline(NULL, getexline, NULL, 0);
530 }
531 else
532 do_cmdline(NULL, getexmodeline, NULL, DOCMD_NOWAIT);
533 lines_left = Rows - 1;
534
Bram Moolenaardf177f62005-02-22 08:39:57 +0000535 if ((prev_line != curwin->w_cursor.lnum
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100536 || changedtick != CHANGEDTICK(curbuf)) && !ex_no_reprint)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000537 {
Bram Moolenaardf177f62005-02-22 08:39:57 +0000538 if (curbuf->b_ml.ml_flags & ML_EMPTY)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100539 emsg(_(e_emptybuf));
Bram Moolenaardf177f62005-02-22 08:39:57 +0000540 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000541 {
Bram Moolenaardf177f62005-02-22 08:39:57 +0000542 if (ex_pressedreturn)
543 {
544 /* go up one line, to overwrite the ":<CR>" line, so the
Bram Moolenaar81870892007-11-11 18:17:28 +0000545 * output doesn't contain empty lines. */
Bram Moolenaardf177f62005-02-22 08:39:57 +0000546 msg_row = prev_msg_row;
547 if (prev_msg_row == Rows - 1)
548 msg_row--;
549 }
550 msg_col = 0;
551 print_line_no_prefix(curwin->w_cursor.lnum, FALSE, FALSE);
552 msg_clr_eos();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000553 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000554 }
Bram Moolenaardf177f62005-02-22 08:39:57 +0000555 else if (ex_pressedreturn && !ex_no_reprint) /* must be at EOF */
556 {
557 if (curbuf->b_ml.ml_flags & ML_EMPTY)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100558 emsg(_(e_emptybuf));
Bram Moolenaardf177f62005-02-22 08:39:57 +0000559 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100560 emsg(_("E501: At end-of-file"));
Bram Moolenaardf177f62005-02-22 08:39:57 +0000561 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000562 }
563
564#ifdef FEAT_GUI
565 --hold_gui_events;
566#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000567 --RedrawingDisabled;
568 --no_wait_return;
569 update_screen(CLEAR);
570 need_wait_return = FALSE;
571 msg_scroll = save_msg_scroll;
572}
573
574/*
575 * Execute a simple command line. Used for translated commands like "*".
576 */
577 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100578do_cmdline_cmd(char_u *cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000579{
580 return do_cmdline(cmd, NULL, NULL,
581 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
582}
583
584/*
585 * do_cmdline(): execute one Ex command line
586 *
587 * 1. Execute "cmdline" when it is not NULL.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100588 * If "cmdline" is NULL, or more lines are needed, fgetline() is used.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000589 * 2. Split up in parts separated with '|'.
590 *
591 * This function can be called recursively!
592 *
593 * flags:
594 * DOCMD_VERBOSE - The command will be included in the error message.
595 * DOCMD_NOWAIT - Don't call wait_return() and friends.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100596 * DOCMD_REPEAT - Repeat execution until fgetline() returns NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000597 * DOCMD_KEYTYPED - Don't reset KeyTyped.
598 * DOCMD_EXCRESET - Reset the exception environment (used for debugging).
599 * DOCMD_KEEPLINE - Store first typed line (for repeating with ".").
600 *
601 * return FAIL if cmdline could not be executed, OK otherwise
602 */
603 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100604do_cmdline(
605 char_u *cmdline,
Bram Moolenaare96a2492019-06-25 04:12:16 +0200606 char_u *(*fgetline)(int, void *, int, int),
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100607 void *cookie, /* argument for fgetline() */
608 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000609{
610 char_u *next_cmdline; /* next cmd to execute */
611 char_u *cmdline_copy = NULL; /* copy of cmd line */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100612 int used_getline = FALSE; /* used "fgetline" to obtain command */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000613 static int recursive = 0; /* recursive depth */
614 int msg_didout_before_start = 0;
615 int count = 0; /* line number count */
616 int did_inc = FALSE; /* incremented RedrawingDisabled */
617 int retval = OK;
618#ifdef FEAT_EVAL
619 struct condstack cstack; /* conditional stack */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000620 garray_T lines_ga; /* keep lines for ":while"/":for" */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000621 int current_line = 0; /* active line in lines_ga */
622 char_u *fname = NULL; /* function or script name */
623 linenr_T *breakpoint = NULL; /* ptr to breakpoint field in cookie */
624 int *dbg_tick = NULL; /* ptr to dbg_tick field in cookie */
Bram Moolenaared203462004-06-16 11:19:22 +0000625 struct dbg_stuff debug_saved; /* saved things for debug mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000626 int initial_trylevel;
627 struct msglist **saved_msg_list = NULL;
628 struct msglist *private_msg_list;
629
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100630 /* "fgetline" and "cookie" passed to do_one_cmd() */
Bram Moolenaare96a2492019-06-25 04:12:16 +0200631 char_u *(*cmd_getline)(int, void *, int, int);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000632 void *cmd_cookie;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000633 struct loop_cookie cmd_loop_cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000634 void *real_cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +0000635 int getline_is_func;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000636#else
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100637# define cmd_getline fgetline
Bram Moolenaar071d4272004-06-13 20:20:40 +0000638# define cmd_cookie cookie
639#endif
640 static int call_depth = 0; /* recursiveness */
641
642#ifdef FEAT_EVAL
643 /* For every pair of do_cmdline()/do_one_cmd() calls, use an extra memory
644 * location for storing error messages to be converted to an exception.
Bram Moolenaarcf3630f2005-01-08 16:04:29 +0000645 * This ensures that the do_errthrow() call in do_one_cmd() does not
646 * combine the messages stored by an earlier invocation of do_one_cmd()
647 * with the command name of the later one. This would happen when
648 * BufWritePost autocommands are executed after a write error. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000649 saved_msg_list = msg_list;
650 msg_list = &private_msg_list;
651 private_msg_list = NULL;
652#endif
653
654 /* It's possible to create an endless loop with ":execute", catch that
Bram Moolenaar777b30f2017-01-02 15:26:27 +0100655 * here. The value of 200 allows nested function calls, ":source", etc.
656 * Allow 200 or 'maxfuncdepth', whatever is larger. */
Bram Moolenaarb094ff42017-01-02 16:16:39 +0100657 if (call_depth >= 200
658#ifdef FEAT_EVAL
659 && call_depth >= p_mfd
660#endif
661 )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000662 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100663 emsg(_("E169: Command too recursive"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000664#ifdef FEAT_EVAL
665 /* When converting to an exception, we do not include the command name
666 * since this is not an error of the specific command. */
667 do_errthrow((struct condstack *)NULL, (char_u *)NULL);
668 msg_list = saved_msg_list;
669#endif
670 return FAIL;
671 }
672 ++call_depth;
673
674#ifdef FEAT_EVAL
675 cstack.cs_idx = -1;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000676 cstack.cs_looplevel = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000677 cstack.cs_trylevel = 0;
678 cstack.cs_emsg_silent_list = NULL;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000679 cstack.cs_lflags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000680 ga_init2(&lines_ga, (int)sizeof(wcmd_T), 10);
681
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100682 real_cookie = getline_cookie(fgetline, cookie);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000683
684 /* Inside a function use a higher nesting level. */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100685 getline_is_func = getline_equal(fgetline, cookie, get_func_line);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000686 if (getline_is_func && ex_nesting_level == func_level(real_cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000687 ++ex_nesting_level;
688
689 /* Get the function or script name and the address where the next breakpoint
690 * line and the debug tick for a function or script are stored. */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000691 if (getline_is_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000692 {
693 fname = func_name(real_cookie);
694 breakpoint = func_breakpoint(real_cookie);
695 dbg_tick = func_dbg_tick(real_cookie);
696 }
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100697 else if (getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000698 {
699 fname = sourcing_name;
700 breakpoint = source_breakpoint(real_cookie);
701 dbg_tick = source_dbg_tick(real_cookie);
702 }
703
704 /*
705 * Initialize "force_abort" and "suppress_errthrow" at the top level.
706 */
707 if (!recursive)
708 {
709 force_abort = FALSE;
710 suppress_errthrow = FALSE;
711 }
712
713 /*
714 * If requested, store and reset the global values controlling the
Bram Moolenaar89d40322006-08-29 15:30:07 +0000715 * exception handling (used when debugging). Otherwise clear it to avoid
716 * a bogus compiler warning when the optimizer uses inline functions...
Bram Moolenaar071d4272004-06-13 20:20:40 +0000717 */
Bram Moolenaarb4872942006-05-13 10:32:52 +0000718 if (flags & DOCMD_EXCRESET)
Bram Moolenaared203462004-06-16 11:19:22 +0000719 save_dbg_stuff(&debug_saved);
Bram Moolenaar89d40322006-08-29 15:30:07 +0000720 else
Bram Moolenaar69b67f72015-09-25 16:59:47 +0200721 vim_memset(&debug_saved, 0, sizeof(debug_saved));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000722
723 initial_trylevel = trylevel;
724
725 /*
726 * "did_throw" will be set to TRUE when an exception is being thrown.
727 */
728 did_throw = FALSE;
729#endif
730 /*
731 * "did_emsg" will be set to TRUE when emsg() is used, in which case we
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000732 * cancel the whole command line, and any if/endif or loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000733 * If force_abort is set, we cancel everything.
734 */
735 did_emsg = FALSE;
736
737 /*
738 * KeyTyped is only set when calling vgetc(). Reset it here when not
739 * calling vgetc() (sourced command lines).
740 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100741 if (!(flags & DOCMD_KEYTYPED)
742 && !getline_equal(fgetline, cookie, getexline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000743 KeyTyped = FALSE;
744
745 /*
746 * Continue executing command lines:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000747 * - when inside an ":if", ":while" or ":for"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000748 * - for multiple commands on one line, separated with '|'
749 * - when repeating until there are no more lines (for ":source")
750 */
751 next_cmdline = cmdline;
752 do
753 {
Bram Moolenaar05159a02005-02-26 23:04:13 +0000754#ifdef FEAT_EVAL
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100755 getline_is_func = getline_equal(fgetline, cookie, get_func_line);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000756#endif
757
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000758 /* stop skipping cmds for an error msg after all endif/while/for */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000759 if (next_cmdline == NULL
760#ifdef FEAT_EVAL
761 && !force_abort
762 && cstack.cs_idx < 0
Bram Moolenaar05159a02005-02-26 23:04:13 +0000763 && !(getline_is_func && func_has_abort(real_cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000764#endif
765 )
766 did_emsg = FALSE;
767
768 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000769 * 1. If repeating a line in a loop, get a line from lines_ga.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100770 * 2. If no line given: Get an allocated line with fgetline().
Bram Moolenaar071d4272004-06-13 20:20:40 +0000771 * 3. If a line is given: Make a copy, so we can mess with it.
772 */
773
774#ifdef FEAT_EVAL
775 /* 1. If repeating, get a previous line from lines_ga. */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000776 if (cstack.cs_looplevel > 0 && current_line < lines_ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000777 {
778 /* Each '|' separated command is stored separately in lines_ga, to
779 * be able to jump to it. Don't use next_cmdline now. */
Bram Moolenaard23a8232018-02-10 18:45:26 +0100780 VIM_CLEAR(cmdline_copy);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000781
782 /* Check if a function has returned or, unless it has an unclosed
783 * try conditional, aborted. */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000784 if (getline_is_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000785 {
Bram Moolenaar05159a02005-02-26 23:04:13 +0000786# ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000787 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +0000788 func_line_end(real_cookie);
789# endif
790 if (func_has_ended(real_cookie))
791 {
792 retval = FAIL;
793 break;
794 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000795 }
Bram Moolenaar05159a02005-02-26 23:04:13 +0000796#ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000797 else if (do_profiling == PROF_YES
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100798 && getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar05159a02005-02-26 23:04:13 +0000799 script_line_end();
800#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000801
802 /* Check if a sourced file hit a ":finish" command. */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100803 if (source_finished(fgetline, cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000804 {
805 retval = FAIL;
806 break;
807 }
808
809 /* If breakpoints have been added/deleted need to check for it. */
810 if (breakpoint != NULL && dbg_tick != NULL
811 && *dbg_tick != debug_tick)
812 {
813 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100814 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar071d4272004-06-13 20:20:40 +0000815 fname, sourcing_lnum);
816 *dbg_tick = debug_tick;
817 }
818
819 next_cmdline = ((wcmd_T *)(lines_ga.ga_data))[current_line].line;
820 sourcing_lnum = ((wcmd_T *)(lines_ga.ga_data))[current_line].lnum;
821
822 /* Did we encounter a breakpoint? */
823 if (breakpoint != NULL && *breakpoint != 0
824 && *breakpoint <= sourcing_lnum)
825 {
826 dbg_breakpoint(fname, sourcing_lnum);
827 /* Find next breakpoint. */
828 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100829 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar071d4272004-06-13 20:20:40 +0000830 fname, sourcing_lnum);
831 *dbg_tick = debug_tick;
832 }
Bram Moolenaar05159a02005-02-26 23:04:13 +0000833# ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000834 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +0000835 {
836 if (getline_is_func)
837 func_line_start(real_cookie);
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100838 else if (getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar05159a02005-02-26 23:04:13 +0000839 script_line_start();
840 }
841# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000842 }
843
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000844 if (cstack.cs_looplevel > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000845 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000846 /* Inside a while/for loop we need to store the lines and use them
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100847 * again. Pass a different "fgetline" function to do_one_cmd()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000848 * below, so that it stores lines in or reads them from
849 * "lines_ga". Makes it possible to define a function inside a
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000850 * while/for loop. */
851 cmd_getline = get_loop_line;
852 cmd_cookie = (void *)&cmd_loop_cookie;
853 cmd_loop_cookie.lines_gap = &lines_ga;
854 cmd_loop_cookie.current_line = current_line;
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100855 cmd_loop_cookie.getline = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000856 cmd_loop_cookie.cookie = cookie;
857 cmd_loop_cookie.repeating = (current_line < lines_ga.ga_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000858 }
859 else
860 {
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100861 cmd_getline = fgetline;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000862 cmd_cookie = cookie;
863 }
864#endif
865
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100866 /* 2. If no line given, get an allocated line with fgetline(). */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000867 if (next_cmdline == NULL)
868 {
869 /*
870 * Need to set msg_didout for the first line after an ":if",
871 * otherwise the ":if" will be overwritten.
872 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100873 if (count == 1 && getline_equal(fgetline, cookie, getexline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000874 msg_didout = TRUE;
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100875 if (fgetline == NULL || (next_cmdline = fgetline(':', cookie,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000876#ifdef FEAT_EVAL
877 cstack.cs_idx < 0 ? 0 : (cstack.cs_idx + 1) * 2
878#else
879 0
880#endif
Bram Moolenaare96a2492019-06-25 04:12:16 +0200881 , TRUE)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000882 {
883 /* Don't call wait_return for aborted command line. The NULL
884 * returned for the end of a sourced file or executed function
885 * doesn't do this. */
886 if (KeyTyped && !(flags & DOCMD_REPEAT))
887 need_wait_return = FALSE;
888 retval = FAIL;
889 break;
890 }
891 used_getline = TRUE;
892
893 /*
894 * Keep the first typed line. Clear it when more lines are typed.
895 */
896 if (flags & DOCMD_KEEPLINE)
897 {
898 vim_free(repeat_cmdline);
899 if (count == 0)
900 repeat_cmdline = vim_strsave(next_cmdline);
901 else
902 repeat_cmdline = NULL;
903 }
904 }
905
906 /* 3. Make a copy of the command so we can mess with it. */
907 else if (cmdline_copy == NULL)
908 {
909 next_cmdline = vim_strsave(next_cmdline);
910 if (next_cmdline == NULL)
911 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100912 emsg(_(e_outofmem));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000913 retval = FAIL;
914 break;
915 }
916 }
917 cmdline_copy = next_cmdline;
918
919#ifdef FEAT_EVAL
920 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000921 * Save the current line when inside a ":while" or ":for", and when
922 * the command looks like a ":while" or ":for", because we may need it
923 * later. When there is a '|' and another command, it is stored
924 * separately, because we need to be able to jump back to it from an
925 * :endwhile/:endfor.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000926 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000927 if (current_line == lines_ga.ga_len
928 && (cstack.cs_looplevel || has_loop_cmd(next_cmdline)))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000929 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000930 if (store_loop_line(&lines_ga, next_cmdline) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000931 {
932 retval = FAIL;
933 break;
934 }
935 }
936 did_endif = FALSE;
937#endif
938
939 if (count++ == 0)
940 {
941 /*
942 * All output from the commands is put below each other, without
943 * waiting for a return. Don't do this when executing commands
944 * from a script or when being called recursive (e.g. for ":e
945 * +command file").
946 */
947 if (!(flags & DOCMD_NOWAIT) && !recursive)
948 {
949 msg_didout_before_start = msg_didout;
950 msg_didany = FALSE; /* no output yet */
951 msg_start();
952 msg_scroll = TRUE; /* put messages below each other */
Bram Moolenaar84a05ac2013-05-06 04:24:17 +0200953 ++no_wait_return; /* don't wait for return until finished */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000954 ++RedrawingDisabled;
955 did_inc = TRUE;
956 }
957 }
958
959 if (p_verbose >= 15 && sourcing_name != NULL)
960 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000961 ++no_wait_return;
Bram Moolenaar8b044b32005-05-31 22:05:58 +0000962 verbose_enter_scroll();
963
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100964 smsg(_("line %ld: %s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +0000965 (long)sourcing_lnum, cmdline_copy);
Bram Moolenaar8b044b32005-05-31 22:05:58 +0000966 if (msg_silent == 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +0100967 msg_puts("\n"); /* don't overwrite this */
Bram Moolenaar8b044b32005-05-31 22:05:58 +0000968
969 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000970 --no_wait_return;
971 }
972
973 /*
974 * 2. Execute one '|' separated command.
975 * do_one_cmd() will return NULL if there is no trailing '|'.
976 * "cmdline_copy" can change, e.g. for '%' and '#' expansion.
977 */
978 ++recursive;
979 next_cmdline = do_one_cmd(&cmdline_copy, flags & DOCMD_VERBOSE,
980#ifdef FEAT_EVAL
981 &cstack,
982#endif
983 cmd_getline, cmd_cookie);
984 --recursive;
985
986#ifdef FEAT_EVAL
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000987 if (cmd_cookie == (void *)&cmd_loop_cookie)
988 /* Use "current_line" from "cmd_loop_cookie", it may have been
Bram Moolenaar071d4272004-06-13 20:20:40 +0000989 * incremented when defining a function. */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000990 current_line = cmd_loop_cookie.current_line;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000991#endif
992
993 if (next_cmdline == NULL)
994 {
Bram Moolenaard23a8232018-02-10 18:45:26 +0100995 VIM_CLEAR(cmdline_copy);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000996#ifdef FEAT_CMDHIST
997 /*
998 * If the command was typed, remember it for the ':' register.
999 * Do this AFTER executing the command to make :@: work.
1000 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001001 if (getline_equal(fgetline, cookie, getexline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001002 && new_last_cmdline != NULL)
1003 {
1004 vim_free(last_cmdline);
1005 last_cmdline = new_last_cmdline;
1006 new_last_cmdline = NULL;
1007 }
1008#endif
1009 }
1010 else
1011 {
1012 /* need to copy the command after the '|' to cmdline_copy, for the
1013 * next do_one_cmd() */
Bram Moolenaara7241f52008-06-24 20:39:31 +00001014 STRMOVE(cmdline_copy, next_cmdline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001015 next_cmdline = cmdline_copy;
1016 }
1017
1018
1019#ifdef FEAT_EVAL
1020 /* reset did_emsg for a function that is not aborted by an error */
1021 if (did_emsg && !force_abort
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001022 && getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001023 && !func_has_abort(real_cookie))
1024 did_emsg = FALSE;
1025
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001026 if (cstack.cs_looplevel > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001027 {
1028 ++current_line;
1029
1030 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001031 * An ":endwhile", ":endfor" and ":continue" is handled here.
1032 * If we were executing commands, jump back to the ":while" or
1033 * ":for".
1034 * If we were not executing commands, decrement cs_looplevel.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001035 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001036 if (cstack.cs_lflags & (CSL_HAD_CONT | CSL_HAD_ENDLOOP))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001037 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001038 cstack.cs_lflags &= ~(CSL_HAD_CONT | CSL_HAD_ENDLOOP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001039
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001040 /* Jump back to the matching ":while" or ":for". Be careful
1041 * not to use a cs_line[] from an entry that isn't a ":while"
1042 * or ":for": It would make "current_line" invalid and can
1043 * cause a crash. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001044 if (!did_emsg && !got_int && !did_throw
1045 && cstack.cs_idx >= 0
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001046 && (cstack.cs_flags[cstack.cs_idx]
1047 & (CSF_WHILE | CSF_FOR))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001048 && cstack.cs_line[cstack.cs_idx] >= 0
1049 && (cstack.cs_flags[cstack.cs_idx] & CSF_ACTIVE))
1050 {
1051 current_line = cstack.cs_line[cstack.cs_idx];
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001052 /* remember we jumped there */
1053 cstack.cs_lflags |= CSL_HAD_LOOP;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001054 line_breakcheck(); /* check if CTRL-C typed */
1055
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001056 /* Check for the next breakpoint at or after the ":while"
1057 * or ":for". */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001058 if (breakpoint != NULL)
1059 {
1060 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001061 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001062 fname,
1063 ((wcmd_T *)lines_ga.ga_data)[current_line].lnum-1);
1064 *dbg_tick = debug_tick;
1065 }
1066 }
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001067 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001068 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001069 /* can only get here with ":endwhile" or ":endfor" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001070 if (cstack.cs_idx >= 0)
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001071 rewind_conditionals(&cstack, cstack.cs_idx - 1,
1072 CSF_WHILE | CSF_FOR, &cstack.cs_looplevel);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001073 }
1074 }
1075
1076 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001077 * For a ":while" or ":for" we need to remember the line number.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001078 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001079 else if (cstack.cs_lflags & CSL_HAD_LOOP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001080 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001081 cstack.cs_lflags &= ~CSL_HAD_LOOP;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001082 cstack.cs_line[cstack.cs_idx] = current_line - 1;
1083 }
1084 }
1085
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01001086 /* Check for the next breakpoint after a watchexpression */
1087 if (breakpoint != NULL && has_watchexpr())
1088 {
1089 *breakpoint = dbg_find_breakpoint(FALSE, fname, sourcing_lnum);
1090 *dbg_tick = debug_tick;
1091 }
1092
Bram Moolenaar071d4272004-06-13 20:20:40 +00001093 /*
1094 * When not inside any ":while" loop, clear remembered lines.
1095 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001096 if (cstack.cs_looplevel == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001097 {
1098 if (lines_ga.ga_len > 0)
1099 {
1100 sourcing_lnum =
1101 ((wcmd_T *)lines_ga.ga_data)[lines_ga.ga_len - 1].lnum;
1102 free_cmdlines(&lines_ga);
1103 }
1104 current_line = 0;
1105 }
1106
1107 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001108 * A ":finally" makes did_emsg, got_int, and did_throw pending for
1109 * being restored at the ":endtry". Reset them here and set the
1110 * ACTIVE and FINALLY flags, so that the finally clause gets executed.
1111 * This includes the case where a missing ":endif", ":endwhile" or
1112 * ":endfor" was detected by the ":finally" itself.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001113 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001114 if (cstack.cs_lflags & CSL_HAD_FINA)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001115 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001116 cstack.cs_lflags &= ~CSL_HAD_FINA;
1117 report_make_pending(cstack.cs_pending[cstack.cs_idx]
1118 & (CSTP_ERROR | CSTP_INTERRUPT | CSTP_THROW),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001119 did_throw ? (void *)current_exception : NULL);
1120 did_emsg = got_int = did_throw = FALSE;
1121 cstack.cs_flags[cstack.cs_idx] |= CSF_ACTIVE | CSF_FINALLY;
1122 }
1123
1124 /* Update global "trylevel" for recursive calls to do_cmdline() from
1125 * within this loop. */
1126 trylevel = initial_trylevel + cstack.cs_trylevel;
1127
1128 /*
Bram Moolenaarc1762cc2007-05-10 16:56:30 +00001129 * If the outermost try conditional (across function calls and sourced
Bram Moolenaar071d4272004-06-13 20:20:40 +00001130 * files) is aborted because of an error, an interrupt, or an uncaught
1131 * exception, cancel everything. If it is left normally, reset
1132 * force_abort to get the non-EH compatible abortion behavior for
1133 * the rest of the script.
1134 */
1135 if (trylevel == 0 && !did_emsg && !got_int && !did_throw)
1136 force_abort = FALSE;
1137
1138 /* Convert an interrupt to an exception if appropriate. */
1139 (void)do_intthrow(&cstack);
1140#endif /* FEAT_EVAL */
1141
1142 }
1143 /*
1144 * Continue executing command lines when:
1145 * - no CTRL-C typed, no aborting error, no exception thrown or try
1146 * conditionals need to be checked for executing finally clauses or
1147 * catching an interrupt exception
1148 * - didn't get an error message or lines are not typed
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001149 * - there is a command after '|', inside a :if, :while, :for or :try, or
Bram Moolenaar071d4272004-06-13 20:20:40 +00001150 * looping for ":source" command or function call.
1151 */
1152 while (!((got_int
1153#ifdef FEAT_EVAL
1154 || (did_emsg && force_abort) || did_throw
1155#endif
1156 )
1157#ifdef FEAT_EVAL
1158 && cstack.cs_trylevel == 0
1159#endif
1160 )
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001161 && !(did_emsg
1162#ifdef FEAT_EVAL
1163 /* Keep going when inside try/catch, so that the error can be
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02001164 * deal with, except when it is a syntax error, it may cause
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001165 * the :endtry to be missed. */
1166 && (cstack.cs_trylevel == 0 || did_emsg_syntax)
1167#endif
1168 && used_getline
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001169 && (getline_equal(fgetline, cookie, getexmodeline)
1170 || getline_equal(fgetline, cookie, getexline)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001171 && (next_cmdline != NULL
1172#ifdef FEAT_EVAL
1173 || cstack.cs_idx >= 0
1174#endif
1175 || (flags & DOCMD_REPEAT)));
1176
1177 vim_free(cmdline_copy);
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001178 did_emsg_syntax = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001179#ifdef FEAT_EVAL
1180 free_cmdlines(&lines_ga);
1181 ga_clear(&lines_ga);
1182
1183 if (cstack.cs_idx >= 0)
1184 {
1185 /*
1186 * If a sourced file or executed function ran to its end, report the
1187 * unclosed conditional.
1188 */
1189 if (!got_int && !did_throw
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001190 && ((getline_equal(fgetline, cookie, getsourceline)
1191 && !source_finished(fgetline, cookie))
1192 || (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001193 && !func_has_ended(real_cookie))))
1194 {
1195 if (cstack.cs_flags[cstack.cs_idx] & CSF_TRY)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001196 emsg(_(e_endtry));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001197 else if (cstack.cs_flags[cstack.cs_idx] & CSF_WHILE)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001198 emsg(_(e_endwhile));
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001199 else if (cstack.cs_flags[cstack.cs_idx] & CSF_FOR)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001200 emsg(_(e_endfor));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001201 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001202 emsg(_(e_endif));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001203 }
1204
1205 /*
1206 * Reset "trylevel" in case of a ":finish" or ":return" or a missing
1207 * ":endtry" in a sourced file or executed function. If the try
1208 * conditional is in its finally clause, ignore anything pending.
1209 * If it is in a catch clause, finish the caught exception.
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001210 * Also cleanup any "cs_forinfo" structures.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001211 */
1212 do
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001213 {
1214 int idx = cleanup_conditionals(&cstack, 0, TRUE);
1215
Bram Moolenaar89e5d682005-01-17 22:06:23 +00001216 if (idx >= 0)
1217 --idx; /* remove try block not in its finally clause */
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001218 rewind_conditionals(&cstack, idx, CSF_WHILE | CSF_FOR,
1219 &cstack.cs_looplevel);
1220 }
1221 while (cstack.cs_idx >= 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001222 trylevel = initial_trylevel;
1223 }
1224
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001225 /* If a missing ":endtry", ":endwhile", ":endfor", or ":endif" or a memory
1226 * lack was reported above and the error message is to be converted to an
Bram Moolenaar071d4272004-06-13 20:20:40 +00001227 * exception, do this now after rewinding the cstack. */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001228 do_errthrow(&cstack, getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001229 ? (char_u *)"endfunction" : (char_u *)NULL);
1230
1231 if (trylevel == 0)
1232 {
1233 /*
1234 * When an exception is being thrown out of the outermost try
1235 * conditional, discard the uncaught exception, disable the conversion
1236 * of interrupts or errors to exceptions, and ensure that no more
1237 * commands are executed.
1238 */
1239 if (did_throw)
1240 {
1241 void *p = NULL;
1242 char_u *saved_sourcing_name;
1243 int saved_sourcing_lnum;
1244 struct msglist *messages = NULL, *next;
1245
1246 /*
1247 * If the uncaught exception is a user exception, report it as an
1248 * error. If it is an error exception, display the saved error
1249 * message now. For an interrupt exception, do nothing; the
1250 * interrupt message is given elsewhere.
1251 */
1252 switch (current_exception->type)
1253 {
1254 case ET_USER:
Bram Moolenaar9c13b352005-05-19 20:53:52 +00001255 vim_snprintf((char *)IObuff, IOSIZE,
1256 _("E605: Exception not caught: %s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001257 current_exception->value);
1258 p = vim_strsave(IObuff);
1259 break;
1260 case ET_ERROR:
1261 messages = current_exception->messages;
1262 current_exception->messages = NULL;
1263 break;
1264 case ET_INTERRUPT:
1265 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001266 }
1267
1268 saved_sourcing_name = sourcing_name;
1269 saved_sourcing_lnum = sourcing_lnum;
1270 sourcing_name = current_exception->throw_name;
1271 sourcing_lnum = current_exception->throw_lnum;
1272 current_exception->throw_name = NULL;
1273
1274 discard_current_exception(); /* uses IObuff if 'verbose' */
1275 suppress_errthrow = TRUE;
1276 force_abort = TRUE;
1277
1278 if (messages != NULL)
1279 {
1280 do
1281 {
1282 next = messages->next;
1283 emsg(messages->msg);
1284 vim_free(messages->msg);
1285 vim_free(messages);
1286 messages = next;
1287 }
1288 while (messages != NULL);
1289 }
1290 else if (p != NULL)
1291 {
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01001292 emsg(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001293 vim_free(p);
1294 }
1295 vim_free(sourcing_name);
1296 sourcing_name = saved_sourcing_name;
1297 sourcing_lnum = saved_sourcing_lnum;
1298 }
1299
1300 /*
1301 * On an interrupt or an aborting error not converted to an exception,
1302 * disable the conversion of errors to exceptions. (Interrupts are not
1303 * converted any more, here.) This enables also the interrupt message
1304 * when force_abort is set and did_emsg unset in case of an interrupt
1305 * from a finally clause after an error.
1306 */
1307 else if (got_int || (did_emsg && force_abort))
1308 suppress_errthrow = TRUE;
1309 }
1310
1311 /*
1312 * The current cstack will be freed when do_cmdline() returns. An uncaught
1313 * exception will have to be rethrown in the previous cstack. If a function
1314 * has just returned or a script file was just finished and the previous
1315 * cstack belongs to the same function or, respectively, script file, it
1316 * will have to be checked for finally clauses to be executed due to the
1317 * ":return" or ":finish". This is done in do_one_cmd().
1318 */
1319 if (did_throw)
1320 need_rethrow = TRUE;
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001321 if ((getline_equal(fgetline, cookie, getsourceline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001322 && ex_nesting_level > source_level(real_cookie))
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001323 || (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001324 && ex_nesting_level > func_level(real_cookie) + 1))
1325 {
1326 if (!did_throw)
1327 check_cstack = TRUE;
1328 }
1329 else
1330 {
1331 /* When leaving a function, reduce nesting level. */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001332 if (getline_equal(fgetline, cookie, get_func_line))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001333 --ex_nesting_level;
1334 /*
1335 * Go to debug mode when returning from a function in which we are
1336 * single-stepping.
1337 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001338 if ((getline_equal(fgetline, cookie, getsourceline)
1339 || getline_equal(fgetline, cookie, get_func_line))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001340 && ex_nesting_level + 1 <= debug_break_level)
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001341 do_debug(getline_equal(fgetline, cookie, getsourceline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001342 ? (char_u *)_("End of sourced file")
1343 : (char_u *)_("End of function"));
1344 }
1345
1346 /*
1347 * Restore the exception environment (done after returning from the
1348 * debugger).
1349 */
1350 if (flags & DOCMD_EXCRESET)
Bram Moolenaared203462004-06-16 11:19:22 +00001351 restore_dbg_stuff(&debug_saved);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001352
1353 msg_list = saved_msg_list;
1354#endif /* FEAT_EVAL */
1355
1356 /*
1357 * If there was too much output to fit on the command line, ask the user to
1358 * hit return before redrawing the screen. With the ":global" command we do
1359 * this only once after the command is finished.
1360 */
1361 if (did_inc)
1362 {
1363 --RedrawingDisabled;
1364 --no_wait_return;
1365 msg_scroll = FALSE;
1366
1367 /*
1368 * When just finished an ":if"-":else" which was typed, no need to
1369 * wait for hit-return. Also for an error situation.
1370 */
1371 if (retval == FAIL
1372#ifdef FEAT_EVAL
1373 || (did_endif && KeyTyped && !did_emsg)
1374#endif
1375 )
1376 {
1377 need_wait_return = FALSE;
1378 msg_didany = FALSE; /* don't wait when restarting edit */
1379 }
1380 else if (need_wait_return)
1381 {
1382 /*
1383 * The msg_start() above clears msg_didout. The wait_return we do
1384 * here should not overwrite the command that may be shown before
1385 * doing that.
1386 */
1387 msg_didout |= msg_didout_before_start;
1388 wait_return(FALSE);
1389 }
1390 }
1391
Bram Moolenaar2a942252012-11-28 23:03:07 +01001392#ifdef FEAT_EVAL
1393 did_endif = FALSE; /* in case do_cmdline used recursively */
1394#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001395 /*
1396 * Reset if_level, in case a sourced script file contains more ":if" than
1397 * ":endif" (could be ":if x | foo | endif").
1398 */
1399 if_level = 0;
Bram Moolenaar2e18a122012-11-28 19:10:54 +01001400#endif
Bram Moolenaard4ad0d42012-11-28 17:34:48 +01001401
Bram Moolenaar071d4272004-06-13 20:20:40 +00001402 --call_depth;
1403 return retval;
1404}
1405
1406#ifdef FEAT_EVAL
1407/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001408 * Obtain a line when inside a ":while" or ":for" loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001409 */
1410 static char_u *
Bram Moolenaare96a2492019-06-25 04:12:16 +02001411get_loop_line(int c, void *cookie, int indent, int do_concat)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001412{
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001413 struct loop_cookie *cp = (struct loop_cookie *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001414 wcmd_T *wp;
1415 char_u *line;
1416
1417 if (cp->current_line + 1 >= cp->lines_gap->ga_len)
1418 {
1419 if (cp->repeating)
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001420 return NULL; /* trying to read past ":endwhile"/":endfor" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001421
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001422 /* First time inside the ":while"/":for": get line normally. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001423 if (cp->getline == NULL)
Bram Moolenaare96a2492019-06-25 04:12:16 +02001424 line = getcmdline(c, 0L, indent, do_concat);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001425 else
Bram Moolenaare96a2492019-06-25 04:12:16 +02001426 line = cp->getline(c, cp->cookie, indent, do_concat);
Bram Moolenaard68071d2006-05-02 22:08:30 +00001427 if (line != NULL && store_loop_line(cp->lines_gap, line) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001428 ++cp->current_line;
1429
1430 return line;
1431 }
1432
1433 KeyTyped = FALSE;
1434 ++cp->current_line;
1435 wp = (wcmd_T *)(cp->lines_gap->ga_data) + cp->current_line;
1436 sourcing_lnum = wp->lnum;
1437 return vim_strsave(wp->line);
1438}
1439
1440/*
1441 * Store a line in "gap" so that a ":while" loop can execute it again.
1442 */
1443 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001444store_loop_line(garray_T *gap, char_u *line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001445{
1446 if (ga_grow(gap, 1) == FAIL)
1447 return FAIL;
1448 ((wcmd_T *)(gap->ga_data))[gap->ga_len].line = vim_strsave(line);
1449 ((wcmd_T *)(gap->ga_data))[gap->ga_len].lnum = sourcing_lnum;
1450 ++gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001451 return OK;
1452}
1453
1454/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001455 * Free the lines stored for a ":while" or ":for" loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001456 */
1457 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001458free_cmdlines(garray_T *gap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001459{
1460 while (gap->ga_len > 0)
1461 {
1462 vim_free(((wcmd_T *)(gap->ga_data))[gap->ga_len - 1].line);
1463 --gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001464 }
1465}
1466#endif
1467
1468/*
Bram Moolenaar89d40322006-08-29 15:30:07 +00001469 * If "fgetline" is get_loop_line(), return TRUE if the getline it uses equals
1470 * "func". * Otherwise return TRUE when "fgetline" equals "func".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001471 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001472 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001473getline_equal(
Bram Moolenaare96a2492019-06-25 04:12:16 +02001474 char_u *(*fgetline)(int, void *, int, int),
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001475 void *cookie UNUSED, /* argument for fgetline() */
Bram Moolenaare96a2492019-06-25 04:12:16 +02001476 char_u *(*func)(int, void *, int, int))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001477{
1478#ifdef FEAT_EVAL
Bram Moolenaare96a2492019-06-25 04:12:16 +02001479 char_u *(*gp)(int, void *, int, int);
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001480 struct loop_cookie *cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001481
Bram Moolenaar89d40322006-08-29 15:30:07 +00001482 /* When "fgetline" is "get_loop_line()" use the "cookie" to find the
Bram Moolenaarc1762cc2007-05-10 16:56:30 +00001483 * function that's originally used to obtain the lines. This may be
1484 * nested several levels. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00001485 gp = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001486 cp = (struct loop_cookie *)cookie;
1487 while (gp == get_loop_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001488 {
1489 gp = cp->getline;
1490 cp = cp->cookie;
1491 }
1492 return gp == func;
1493#else
Bram Moolenaar89d40322006-08-29 15:30:07 +00001494 return fgetline == func;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001495#endif
1496}
1497
Bram Moolenaar071d4272004-06-13 20:20:40 +00001498/*
Bram Moolenaar89d40322006-08-29 15:30:07 +00001499 * If "fgetline" is get_loop_line(), return the cookie used by the original
Bram Moolenaar071d4272004-06-13 20:20:40 +00001500 * getline function. Otherwise return "cookie".
1501 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001502 void *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001503getline_cookie(
Bram Moolenaare96a2492019-06-25 04:12:16 +02001504 char_u *(*fgetline)(int, void *, int, int) UNUSED,
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001505 void *cookie) /* argument for fgetline() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001506{
Bram Moolenaar13505972019-01-24 15:04:48 +01001507#ifdef FEAT_EVAL
Bram Moolenaare96a2492019-06-25 04:12:16 +02001508 char_u *(*gp)(int, void *, int, int);
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001509 struct loop_cookie *cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001510
Bram Moolenaar89d40322006-08-29 15:30:07 +00001511 /* When "fgetline" is "get_loop_line()" use the "cookie" to find the
Bram Moolenaarc1762cc2007-05-10 16:56:30 +00001512 * cookie that's originally used to obtain the lines. This may be nested
Bram Moolenaar071d4272004-06-13 20:20:40 +00001513 * several levels. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00001514 gp = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001515 cp = (struct loop_cookie *)cookie;
1516 while (gp == get_loop_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001517 {
1518 gp = cp->getline;
1519 cp = cp->cookie;
1520 }
1521 return cp;
Bram Moolenaar13505972019-01-24 15:04:48 +01001522#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001523 return cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001524#endif
Bram Moolenaar13505972019-01-24 15:04:48 +01001525}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001526
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001527
1528/*
1529 * Helper function to apply an offset for buffer commands, i.e. ":bdelete",
1530 * ":bwipeout", etc.
1531 * Returns the buffer number.
1532 */
1533 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001534compute_buffer_local_count(int addr_type, int lnum, int offset)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001535{
1536 buf_T *buf;
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001537 buf_T *nextbuf;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001538 int count = offset;
1539
1540 buf = firstbuf;
1541 while (buf->b_next != NULL && buf->b_fnum < lnum)
1542 buf = buf->b_next;
1543 while (count != 0)
1544 {
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001545 count += (offset < 0) ? 1 : -1;
1546 nextbuf = (offset < 0) ? buf->b_prev : buf->b_next;
1547 if (nextbuf == NULL)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001548 break;
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001549 buf = nextbuf;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001550 if (addr_type == ADDR_LOADED_BUFFERS)
1551 /* skip over unloaded buffers */
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001552 while (buf->b_ml.ml_mfp == NULL)
1553 {
1554 nextbuf = (offset < 0) ? buf->b_prev : buf->b_next;
1555 if (nextbuf == NULL)
1556 break;
1557 buf = nextbuf;
1558 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001559 }
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001560 /* we might have gone too far, last buffer is not loadedd */
1561 if (addr_type == ADDR_LOADED_BUFFERS)
1562 while (buf->b_ml.ml_mfp == NULL)
1563 {
1564 nextbuf = (offset >= 0) ? buf->b_prev : buf->b_next;
1565 if (nextbuf == NULL)
1566 break;
1567 buf = nextbuf;
1568 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001569 return buf->b_fnum;
1570}
1571
Bram Moolenaarb7316892019-05-01 18:08:42 +02001572/*
1573 * Return the window number of "win".
1574 * When "win" is NULL return the number of windows.
1575 */
Bram Moolenaarf240e182014-11-27 18:33:02 +01001576 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001577current_win_nr(win_T *win)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001578{
1579 win_T *wp;
1580 int nr = 0;
1581
Bram Moolenaar29323592016-07-24 22:04:11 +02001582 FOR_ALL_WINDOWS(wp)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001583 {
1584 ++nr;
1585 if (wp == win)
1586 break;
1587 }
1588 return nr;
1589}
1590
1591 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001592current_tab_nr(tabpage_T *tab)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001593{
1594 tabpage_T *tp;
1595 int nr = 0;
1596
Bram Moolenaar29323592016-07-24 22:04:11 +02001597 FOR_ALL_TABPAGES(tp)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001598 {
1599 ++nr;
1600 if (tp == tab)
1601 break;
1602 }
1603 return nr;
1604}
1605
1606# define CURRENT_WIN_NR current_win_nr(curwin)
1607# define LAST_WIN_NR current_win_nr(NULL)
1608# define CURRENT_TAB_NR current_tab_nr(curtab)
1609# define LAST_TAB_NR current_tab_nr(NULL)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001610
Bram Moolenaar071d4272004-06-13 20:20:40 +00001611/*
1612 * Execute one Ex command.
1613 *
1614 * If 'sourcing' is TRUE, the command will be included in the error message.
1615 *
1616 * 1. skip comment lines and leading space
1617 * 2. handle command modifiers
Bram Moolenaar1c40a662014-11-27 16:38:11 +01001618 * 3. find the command
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001619 * 4. parse range
Bram Moolenaar1c40a662014-11-27 16:38:11 +01001620 * 5. Parse the command.
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001621 * 6. parse arguments
1622 * 7. switch on command name
Bram Moolenaar071d4272004-06-13 20:20:40 +00001623 *
Bram Moolenaar89d40322006-08-29 15:30:07 +00001624 * Note: "fgetline" can be NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001625 *
1626 * This function may be called recursively!
1627 */
1628#if (_MSC_VER == 1200)
1629/*
Bram Moolenaared203462004-06-16 11:19:22 +00001630 * Avoid optimisation bug in VC++ version 6.0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001631 */
Bram Moolenaar281bdce2005-01-25 21:53:18 +00001632 #pragma optimize( "g", off )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001633#endif
1634 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001635do_one_cmd(
1636 char_u **cmdlinep,
1637 int sourcing,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001638#ifdef FEAT_EVAL
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001639 struct condstack *cstack,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001640#endif
Bram Moolenaare96a2492019-06-25 04:12:16 +02001641 char_u *(*fgetline)(int, void *, int, int),
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001642 void *cookie) /* argument for fgetline() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001643{
1644 char_u *p;
1645 linenr_T lnum;
1646 long n;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001647 char *errormsg = NULL; /* error message */
Bram Moolenaar7b668e82016-08-23 23:51:21 +02001648 char_u *after_modifier = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001649 exarg_T ea; /* Ex command arguments */
Bram Moolenaar8e258a42009-07-09 13:55:43 +00001650 int save_msg_scroll = msg_scroll;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001651 cmdmod_T save_cmdmod;
Bram Moolenaar9a2c0912019-03-30 14:26:18 +01001652 int save_reg_executing = reg_executing;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001653 int ni; /* set when Not Implemented */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001654 char_u *cmd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001655
1656 vim_memset(&ea, 0, sizeof(ea));
1657 ea.line1 = 1;
1658 ea.line2 = 1;
1659#ifdef FEAT_EVAL
1660 ++ex_nesting_level;
1661#endif
1662
Bram Moolenaar21691f82012-12-05 19:13:18 +01001663 /* When the last file has not been edited :q has to be typed twice. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001664 if (quitmore
1665#ifdef FEAT_EVAL
1666 /* avoid that a function call in 'statusline' does this */
Bram Moolenaar89d40322006-08-29 15:30:07 +00001667 && !getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001668#endif
Bram Moolenaar21691f82012-12-05 19:13:18 +01001669 /* avoid that an autocommand, e.g. QuitPre, does this */
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001670 && !getline_equal(fgetline, cookie, getnextac))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001671 --quitmore;
1672
1673 /*
1674 * Reset browse, confirm, etc.. They are restored when returning, for
1675 * recursive calls.
1676 */
1677 save_cmdmod = cmdmod;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001678
Bram Moolenaarcbb37ad2006-08-16 15:04:21 +00001679 /* "#!anything" is handled like a comment. */
1680 if ((*cmdlinep)[0] == '#' && (*cmdlinep)[1] == '!')
1681 goto doend;
1682
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02001683/*
1684 * 1. Skip comment lines and leading white space and colons.
1685 * 2. Handle command modifiers.
1686 */
1687 // The "ea" structure holds the arguments that can be used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001688 ea.cmd = *cmdlinep;
Bram Moolenaareffed932018-08-14 13:38:17 +02001689 ea.cmdlinep = cmdlinep;
1690 ea.getline = fgetline;
1691 ea.cookie = cookie;
1692#ifdef FEAT_EVAL
1693 ea.cstack = cstack;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001694#endif
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02001695 if (parse_command_modifiers(&ea, &errormsg, FALSE) == FAIL)
Bram Moolenaareffed932018-08-14 13:38:17 +02001696 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001697
Bram Moolenaar7b668e82016-08-23 23:51:21 +02001698 after_modifier = ea.cmd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001699
1700#ifdef FEAT_EVAL
1701 ea.skip = did_emsg || got_int || did_throw || (cstack->cs_idx >= 0
1702 && !(cstack->cs_flags[cstack->cs_idx] & CSF_ACTIVE));
1703#else
1704 ea.skip = (if_level > 0);
1705#endif
1706
Bram Moolenaar071d4272004-06-13 20:20:40 +00001707/*
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001708 * 3. Skip over the range to find the command. Let "p" point to after it.
1709 *
1710 * We need the command to know what kind of range it uses.
1711 */
1712 cmd = ea.cmd;
1713 ea.cmd = skip_range(ea.cmd, NULL);
1714 if (*ea.cmd == '*' && vim_strchr(p_cpo, CPO_STAR) == NULL)
1715 ea.cmd = skipwhite(ea.cmd + 1);
1716 p = find_command(&ea, NULL);
1717
Bram Moolenaar7feb35e2018-08-21 17:49:54 +02001718#ifdef FEAT_EVAL
1719# ifdef FEAT_PROFILE
1720 // Count this line for profiling if skip is TRUE.
1721 if (do_profiling == PROF_YES
1722 && (!ea.skip || cstack->cs_idx == 0 || (cstack->cs_idx > 0
1723 && (cstack->cs_flags[cstack->cs_idx - 1] & CSF_ACTIVE))))
1724 {
1725 int skip = did_emsg || got_int || did_throw;
1726
1727 if (ea.cmdidx == CMD_catch)
1728 skip = !skip && !(cstack->cs_idx >= 0
1729 && (cstack->cs_flags[cstack->cs_idx] & CSF_THROWN)
1730 && !(cstack->cs_flags[cstack->cs_idx] & CSF_CAUGHT));
1731 else if (ea.cmdidx == CMD_else || ea.cmdidx == CMD_elseif)
1732 skip = skip || !(cstack->cs_idx >= 0
1733 && !(cstack->cs_flags[cstack->cs_idx]
1734 & (CSF_ACTIVE | CSF_TRUE)));
1735 else if (ea.cmdidx == CMD_finally)
1736 skip = FALSE;
1737 else if (ea.cmdidx != CMD_endif
1738 && ea.cmdidx != CMD_endfor
1739 && ea.cmdidx != CMD_endtry
1740 && ea.cmdidx != CMD_endwhile)
1741 skip = ea.skip;
1742
1743 if (!skip)
1744 {
1745 if (getline_equal(fgetline, cookie, get_func_line))
1746 func_line_exec(getline_cookie(fgetline, cookie));
1747 else if (getline_equal(fgetline, cookie, getsourceline))
1748 script_line_exec();
1749 }
1750 }
1751# endif
1752
1753 /* May go to debug mode. If this happens and the ">quit" debug command is
1754 * used, throw an interrupt exception and skip the next command. */
1755 dbg_check_breakpoint(&ea);
1756 if (!ea.skip && got_int)
1757 {
1758 ea.skip = TRUE;
1759 (void)do_intthrow(cstack);
1760 }
1761#endif
1762
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001763/*
1764 * 4. parse a range specifier of the form: addr [,addr] [;addr] ..
Bram Moolenaar071d4272004-06-13 20:20:40 +00001765 *
1766 * where 'addr' is:
1767 *
1768 * % (entire file)
1769 * $ [+-NUM]
1770 * 'x [+-NUM] (where x denotes a currently defined mark)
1771 * . [+-NUM]
1772 * [+-NUM]..
1773 * NUM
1774 *
1775 * The ea.cmd pointer is updated to point to the first character following the
1776 * range spec. If an initial address is found, but no second, the upper bound
1777 * is equal to the lower.
1778 */
1779
Bram Moolenaar25190db2019-05-04 15:05:28 +02001780 // ea.addr_type for user commands is set by find_ucmd
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01001781 if (!IS_USER_CMDIDX(ea.cmdidx))
1782 {
1783 if (ea.cmdidx != CMD_SIZE)
1784 ea.addr_type = cmdnames[(int)ea.cmdidx].cmd_addr_type;
1785 else
1786 ea.addr_type = ADDR_LINES;
1787
Bram Moolenaar25190db2019-05-04 15:05:28 +02001788 // :wincmd range depends on the argument.
Bram Moolenaar164f3262015-01-14 21:22:01 +01001789 if (ea.cmdidx == CMD_wincmd && p != NULL)
1790 get_wincmd_addr_type(skipwhite(p), &ea);
Bram Moolenaar25190db2019-05-04 15:05:28 +02001791#ifdef FEAT_QUICKFIX
1792 // :.cc in quickfix window uses line number
1793 if ((ea.cmdidx == CMD_cc || ea.cmdidx == CMD_ll) && bt_quickfix(curbuf))
1794 ea.addr_type = ADDR_OTHER;
1795#endif
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01001796 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001797
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01001798 ea.cmd = cmd;
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02001799 if (parse_cmd_address(&ea, &errormsg, FALSE) == FAIL)
Bram Moolenaaree8415b2018-08-10 23:13:12 +02001800 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001801
Bram Moolenaar071d4272004-06-13 20:20:40 +00001802/*
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001803 * 5. Parse the command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001804 */
1805
1806 /*
1807 * Skip ':' and any white space
1808 */
1809 ea.cmd = skipwhite(ea.cmd);
1810 while (*ea.cmd == ':')
1811 ea.cmd = skipwhite(ea.cmd + 1);
1812
1813 /*
1814 * If we got a line, but no command, then go to the line.
1815 * If we find a '|' or '\n' we set ea.nextcmd.
1816 */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001817 if (*ea.cmd == NUL || *ea.cmd == '"'
1818 || (ea.nextcmd = check_nextcmd(ea.cmd)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001819 {
1820 /*
1821 * strange vi behaviour:
1822 * ":3" jumps to line 3
1823 * ":3|..." prints line 3
1824 * ":|" prints current line
1825 */
1826 if (ea.skip) /* skip this if inside :if */
1827 goto doend;
Bram Moolenaardf177f62005-02-22 08:39:57 +00001828 if (*ea.cmd == '|' || (exmode_active && ea.line1 != ea.line2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001829 {
1830 ea.cmdidx = CMD_print;
Bram Moolenaar8071cb22019-07-12 17:58:01 +02001831 ea.argt = EX_RANGE+EX_COUNT+EX_TRLBAR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001832 if ((errormsg = invalid_range(&ea)) == NULL)
1833 {
1834 correct_range(&ea);
1835 ex_print(&ea);
1836 }
1837 }
1838 else if (ea.addr_count != 0)
1839 {
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00001840 if (ea.line2 > curbuf->b_ml.ml_line_count)
1841 {
1842 /* With '-' in 'cpoptions' a line number past the file is an
1843 * error, otherwise put it at the end of the file. */
1844 if (vim_strchr(p_cpo, CPO_MINUS) != NULL)
1845 ea.line2 = -1;
1846 else
1847 ea.line2 = curbuf->b_ml.ml_line_count;
1848 }
1849
1850 if (ea.line2 < 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001851 errormsg = _(e_invrange);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001852 else
1853 {
1854 if (ea.line2 == 0)
1855 curwin->w_cursor.lnum = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001856 else
1857 curwin->w_cursor.lnum = ea.line2;
1858 beginline(BL_SOL | BL_FIX);
1859 }
1860 }
1861 goto doend;
1862 }
1863
Bram Moolenaard5005162014-08-22 23:05:54 +02001864 /* If this looks like an undefined user command and there are CmdUndefined
1865 * autocommands defined, trigger the matching autocommands. */
1866 if (p != NULL && ea.cmdidx == CMD_SIZE && !ea.skip
1867 && ASCII_ISUPPER(*ea.cmd)
1868 && has_cmdundefined())
1869 {
Bram Moolenaard5005162014-08-22 23:05:54 +02001870 int ret;
1871
Bram Moolenaar5a31b462014-08-23 14:16:20 +02001872 p = ea.cmd;
Bram Moolenaard5005162014-08-22 23:05:54 +02001873 while (ASCII_ISALNUM(*p))
1874 ++p;
Bram Moolenaar120f4a82014-09-09 12:22:06 +02001875 p = vim_strnsave(ea.cmd, (int)(p - ea.cmd));
Bram Moolenaard5005162014-08-22 23:05:54 +02001876 ret = apply_autocmds(EVENT_CMDUNDEFINED, p, p, TRUE, NULL);
1877 vim_free(p);
Bram Moolenaar829aef12015-07-28 14:25:48 +02001878 /* If the autocommands did something and didn't cause an error, try
1879 * finding the command again. */
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001880 p = (ret
1881#ifdef FEAT_EVAL
1882 && !aborting()
Bram Moolenaard5005162014-08-22 23:05:54 +02001883#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001884 ) ? find_command(&ea, NULL) : ea.cmd;
1885 }
Bram Moolenaard5005162014-08-22 23:05:54 +02001886
Bram Moolenaar071d4272004-06-13 20:20:40 +00001887 if (p == NULL)
1888 {
1889 if (!ea.skip)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001890 errormsg = _("E464: Ambiguous use of user-defined command");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001891 goto doend;
1892 }
Bram Moolenaarac9fb182019-04-27 13:04:13 +02001893 // Check for wrong commands.
Bram Moolenaar6f9a4762017-06-22 20:39:17 +02001894 if (*p == '!' && ea.cmd[1] == 0151 && ea.cmd[0] == 78
1895 && !IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001896 {
1897 errormsg = uc_fun_cmd();
1898 goto doend;
1899 }
Bram Moolenaarac9fb182019-04-27 13:04:13 +02001900
Bram Moolenaar071d4272004-06-13 20:20:40 +00001901 if (ea.cmdidx == CMD_SIZE)
1902 {
1903 if (!ea.skip)
1904 {
1905 STRCPY(IObuff, _("E492: Not an editor command"));
1906 if (!sourcing)
Bram Moolenaar7b668e82016-08-23 23:51:21 +02001907 {
1908 /* If the modifier was parsed OK the error must be in the
1909 * following command */
1910 if (after_modifier != NULL)
1911 append_command(after_modifier);
1912 else
1913 append_command(*cmdlinep);
1914 }
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001915 errormsg = (char *)IObuff;
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001916 did_emsg_syntax = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001917 }
1918 goto doend;
1919 }
1920
Bram Moolenaar958636c2014-10-21 20:01:58 +02001921 ni = (!IS_USER_CMDIDX(ea.cmdidx)
1922 && (cmdnames[ea.cmdidx].cmd_func == ex_ni
Bram Moolenaar7bb75552007-07-16 18:39:49 +00001923#ifdef HAVE_EX_SCRIPT_NI
1924 || cmdnames[ea.cmdidx].cmd_func == ex_script_ni
1925#endif
1926 ));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001927
1928#ifndef FEAT_EVAL
1929 /*
1930 * When the expression evaluation is disabled, recognize the ":if" and
1931 * ":endif" commands and ignore everything in between it.
1932 */
1933 if (ea.cmdidx == CMD_if)
1934 ++if_level;
1935 if (if_level)
1936 {
1937 if (ea.cmdidx == CMD_endif)
1938 --if_level;
1939 goto doend;
1940 }
1941
1942#endif
1943
Bram Moolenaar196b3b02008-06-20 16:51:41 +00001944 /* forced commands */
1945 if (*p == '!' && ea.cmdidx != CMD_substitute
1946 && ea.cmdidx != CMD_smagic && ea.cmdidx != CMD_snomagic)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001947 {
1948 ++p;
1949 ea.forceit = TRUE;
1950 }
1951 else
1952 ea.forceit = FALSE;
1953
1954/*
Bram Moolenaarb7316892019-05-01 18:08:42 +02001955 * 6. Parse arguments. Then check for errors.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001956 */
Bram Moolenaar958636c2014-10-21 20:01:58 +02001957 if (!IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001958 ea.argt = (long)cmdnames[(int)ea.cmdidx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001959
1960 if (!ea.skip)
1961 {
1962#ifdef HAVE_SANDBOX
Bram Moolenaar8071cb22019-07-12 17:58:01 +02001963 if (sandbox != 0 && !(ea.argt & EX_SBOXOK))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001964 {
Bram Moolenaar8c62a082019-02-08 14:34:10 +01001965 // Command not allowed in sandbox.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001966 errormsg = _(e_sandbox);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001967 goto doend;
1968 }
1969#endif
Bram Moolenaar8071cb22019-07-12 17:58:01 +02001970 if (restricted != 0 && (ea.argt & EX_RESTRICT))
Bram Moolenaar8c62a082019-02-08 14:34:10 +01001971 {
1972 errormsg = _("E981: Command not allowed in rvim");
1973 goto doend;
1974 }
Bram Moolenaar8071cb22019-07-12 17:58:01 +02001975 if (!curbuf->b_p_ma && (ea.argt & EX_MODIFY))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001976 {
1977 /* Command not allowed in non-'modifiable' buffer */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001978 errormsg = _(e_modifiable);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001979 goto doend;
1980 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00001981
Bram Moolenaar8071cb22019-07-12 17:58:01 +02001982 if (text_locked() && !(ea.argt & EX_CMDWIN)
Bram Moolenaar958636c2014-10-21 20:01:58 +02001983 && !IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001984 {
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00001985 /* Command not allowed when editing the command line. */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001986 errormsg = _(get_text_locked_msg());
Bram Moolenaar071d4272004-06-13 20:20:40 +00001987 goto doend;
1988 }
Bram Moolenaar379fb762018-08-30 15:58:28 +02001989
Bram Moolenaar5555acc2006-04-07 21:33:12 +00001990 /* Disallow editing another buffer when "curbuf_lock" is set.
Bram Moolenaar379fb762018-08-30 15:58:28 +02001991 * Do allow ":checktime" (it is postponed).
1992 * Do allow ":edit" (check for an argument later).
1993 * Do allow ":file" with no arguments (check for an argument later). */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02001994 if (!(ea.argt & EX_CMDWIN)
Bram Moolenaar5555acc2006-04-07 21:33:12 +00001995 && ea.cmdidx != CMD_checktime
Bram Moolenaar379fb762018-08-30 15:58:28 +02001996 && ea.cmdidx != CMD_edit
1997 && ea.cmdidx != CMD_file
Bram Moolenaar958636c2014-10-21 20:01:58 +02001998 && !IS_USER_CMDIDX(ea.cmdidx)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001999 && curbuf_locked())
2000 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002001
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002002 if (!ni && !(ea.argt & EX_RANGE) && ea.addr_count > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002003 {
2004 /* no range allowed */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002005 errormsg = _(e_norange);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002006 goto doend;
2007 }
2008 }
2009
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002010 if (!ni && !(ea.argt & EX_BANG) && ea.forceit) // no <!> allowed
Bram Moolenaar071d4272004-06-13 20:20:40 +00002011 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002012 errormsg = _(e_nobang);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002013 goto doend;
2014 }
2015
2016 /*
2017 * Don't complain about the range if it is not used
2018 * (could happen if line_count is accidentally set to 0).
2019 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002020 if (!ea.skip && !ni && (ea.argt & EX_RANGE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002021 {
2022 /*
2023 * If the range is backwards, ask for confirmation and, if given, swap
2024 * ea.line1 & ea.line2 so it's forwards again.
2025 * When global command is busy, don't ask, will fail below.
2026 */
2027 if (!global_busy && ea.line1 > ea.line2)
2028 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002029 if (msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002030 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002031 if (sourcing || exmode_active)
2032 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002033 errormsg = _("E493: Backwards range given");
Bram Moolenaara5792f52005-11-23 21:25:05 +00002034 goto doend;
2035 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002036 if (ask_yesno((char_u *)
2037 _("Backwards range given, OK to swap"), FALSE) != 'y')
Bram Moolenaara5792f52005-11-23 21:25:05 +00002038 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002039 }
2040 lnum = ea.line1;
2041 ea.line1 = ea.line2;
2042 ea.line2 = lnum;
2043 }
2044 if ((errormsg = invalid_range(&ea)) != NULL)
2045 goto doend;
2046 }
2047
Bram Moolenaarb7316892019-05-01 18:08:42 +02002048 if ((ea.addr_type == ADDR_OTHER) && ea.addr_count == 0)
2049 // default is 1, not cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00002050 ea.line2 = 1;
2051
2052 correct_range(&ea);
2053
2054#ifdef FEAT_FOLDING
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002055 if (((ea.argt & EX_WHOLEFOLD) || ea.addr_count >= 2) && !global_busy
Bram Moolenaara3306952016-01-02 21:41:06 +01002056 && ea.addr_type == ADDR_LINES)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002057 {
2058 /* Put the first line at the start of a closed fold, put the last line
2059 * at the end of a closed fold. */
2060 (void)hasFolding(ea.line1, &ea.line1, NULL);
2061 (void)hasFolding(ea.line2, NULL, &ea.line2);
2062 }
2063#endif
2064
2065#ifdef FEAT_QUICKFIX
2066 /*
Bram Moolenaar86b68352004-12-27 21:59:20 +00002067 * For the ":make" and ":grep" commands we insert the 'makeprg'/'grepprg'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002068 * option here, so things like % get expanded.
2069 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002070 p = replace_makeprg(&ea, p, cmdlinep);
2071 if (p == NULL)
2072 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002073#endif
2074
2075 /*
2076 * Skip to start of argument.
2077 * Don't do this for the ":!" command, because ":!! -l" needs the space.
2078 */
2079 if (ea.cmdidx == CMD_bang)
2080 ea.arg = p;
2081 else
2082 ea.arg = skipwhite(p);
2083
Bram Moolenaar379fb762018-08-30 15:58:28 +02002084 // ":file" cannot be run with an argument when "curbuf_lock" is set
2085 if (ea.cmdidx == CMD_file && *ea.arg != NUL && curbuf_locked())
2086 goto doend;
2087
Bram Moolenaar071d4272004-06-13 20:20:40 +00002088 /*
2089 * Check for "++opt=val" argument.
2090 * Must be first, allow ":w ++enc=utf8 !cmd"
2091 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002092 if (ea.argt & EX_ARGOPT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002093 while (ea.arg[0] == '+' && ea.arg[1] == '+')
2094 if (getargopt(&ea) == FAIL && !ni)
2095 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002096 errormsg = _(e_invarg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002097 goto doend;
2098 }
2099
2100 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
2101 {
2102 if (*ea.arg == '>') /* append */
2103 {
2104 if (*++ea.arg != '>') /* typed wrong */
2105 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002106 errormsg = _("E494: Use w or w>>");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002107 goto doend;
2108 }
2109 ea.arg = skipwhite(ea.arg + 1);
2110 ea.append = TRUE;
2111 }
2112 else if (*ea.arg == '!' && ea.cmdidx == CMD_write) /* :w !filter */
2113 {
2114 ++ea.arg;
2115 ea.usefilter = TRUE;
2116 }
2117 }
2118
2119 if (ea.cmdidx == CMD_read)
2120 {
2121 if (ea.forceit)
2122 {
2123 ea.usefilter = TRUE; /* :r! filter if ea.forceit */
2124 ea.forceit = FALSE;
2125 }
2126 else if (*ea.arg == '!') /* :r !filter */
2127 {
2128 ++ea.arg;
2129 ea.usefilter = TRUE;
2130 }
2131 }
2132
2133 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
2134 {
2135 ea.amount = 1;
2136 while (*ea.arg == *ea.cmd) /* count number of '>' or '<' */
2137 {
2138 ++ea.arg;
2139 ++ea.amount;
2140 }
2141 ea.arg = skipwhite(ea.arg);
2142 }
2143
2144 /*
2145 * Check for "+command" argument, before checking for next command.
2146 * Don't do this for ":read !cmd" and ":write !cmd".
2147 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002148 if ((ea.argt & EX_CMDARG) && !ea.usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002149 ea.do_ecmd_cmd = getargcmd(&ea.arg);
2150
2151 /*
2152 * Check for '|' to separate commands and '"' to start comments.
2153 * Don't do this for ":read !cmd" and ":write !cmd".
2154 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002155 if ((ea.argt & EX_TRLBAR) && !ea.usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002156 separate_nextcmd(&ea);
2157
2158 /*
2159 * Check for <newline> to end a shell command.
Bram Moolenaardf177f62005-02-22 08:39:57 +00002160 * Also do this for ":read !cmd", ":write !cmd" and ":global".
2161 * Any others?
Bram Moolenaar071d4272004-06-13 20:20:40 +00002162 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00002163 else if (ea.cmdidx == CMD_bang
Bram Moolenaar67883b42017-07-27 22:57:00 +02002164 || ea.cmdidx == CMD_terminal
Bram Moolenaardf177f62005-02-22 08:39:57 +00002165 || ea.cmdidx == CMD_global
2166 || ea.cmdidx == CMD_vglobal
2167 || ea.usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002168 {
2169 for (p = ea.arg; *p; ++p)
2170 {
2171 /* Remove one backslash before a newline, so that it's possible to
2172 * pass a newline to the shell and also a newline that is preceded
2173 * with a backslash. This makes it impossible to end a shell
2174 * command in a backslash, but that doesn't appear useful.
2175 * Halving the number of backslashes is incompatible with previous
2176 * versions. */
2177 if (*p == '\\' && p[1] == '\n')
Bram Moolenaara7241f52008-06-24 20:39:31 +00002178 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002179 else if (*p == '\n')
2180 {
2181 ea.nextcmd = p + 1;
2182 *p = NUL;
2183 break;
2184 }
2185 }
2186 }
2187
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002188 if ((ea.argt & EX_DFLALL) && ea.addr_count == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002189 {
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002190 buf_T *buf;
2191
Bram Moolenaar071d4272004-06-13 20:20:40 +00002192 ea.line1 = 1;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002193 switch (ea.addr_type)
2194 {
2195 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02002196 case ADDR_OTHER:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002197 ea.line2 = curbuf->b_ml.ml_line_count;
2198 break;
2199 case ADDR_LOADED_BUFFERS:
2200 buf = firstbuf;
2201 while (buf->b_next != NULL && buf->b_ml.ml_mfp == NULL)
2202 buf = buf->b_next;
2203 ea.line1 = buf->b_fnum;
2204 buf = lastbuf;
2205 while (buf->b_prev != NULL && buf->b_ml.ml_mfp == NULL)
2206 buf = buf->b_prev;
2207 ea.line2 = buf->b_fnum;
2208 break;
2209 case ADDR_BUFFERS:
2210 ea.line1 = firstbuf->b_fnum;
2211 ea.line2 = lastbuf->b_fnum;
2212 break;
2213 case ADDR_WINDOWS:
2214 ea.line2 = LAST_WIN_NR;
2215 break;
2216 case ADDR_TABS:
2217 ea.line2 = LAST_TAB_NR;
2218 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01002219 case ADDR_TABS_RELATIVE:
2220 ea.line2 = 1;
2221 break;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002222 case ADDR_ARGUMENTS:
2223 if (ARGCOUNT == 0)
2224 ea.line1 = ea.line2 = 0;
2225 else
2226 ea.line2 = ARGCOUNT;
2227 break;
Bram Moolenaar25190db2019-05-04 15:05:28 +02002228 case ADDR_QUICKFIX_VALID:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02002229#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02002230 ea.line2 = qf_get_valid_size(&ea);
Bram Moolenaaraa23b372015-09-08 18:46:31 +02002231 if (ea.line2 == 0)
2232 ea.line2 = 1;
Bram Moolenaare906c502015-09-09 21:10:39 +02002233#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02002234 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02002235 case ADDR_NONE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02002236 case ADDR_UNSIGNED:
2237 case ADDR_QUICKFIX:
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002238 iemsg(_("INTERNAL: Cannot use EX_DFLALL with ADDR_NONE, ADDR_UNSIGNED or ADDR_QUICKFIX"));
Bram Moolenaarb7316892019-05-01 18:08:42 +02002239 break;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002240 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002241 }
2242
2243 /* accept numbered register only when no count allowed (:put) */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002244 if ( (ea.argt & EX_REGSTR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002245 && *ea.arg != NUL
Bram Moolenaar958636c2014-10-21 20:01:58 +02002246 /* Do not allow register = for user commands */
2247 && (!IS_USER_CMDIDX(ea.cmdidx) || *ea.arg != '=')
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002248 && !((ea.argt & EX_COUNT) && VIM_ISDIGIT(*ea.arg)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002249 {
Bram Moolenaar85de2062011-05-05 14:26:41 +02002250#ifndef FEAT_CLIPBOARD
2251 /* check these explicitly for a more specific error message */
2252 if (*ea.arg == '*' || *ea.arg == '+')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002253 {
Bram Moolenaar99b12722019-01-14 20:16:40 +01002254 errormsg = _(e_invalidreg);
Bram Moolenaar85de2062011-05-05 14:26:41 +02002255 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002256 }
2257#endif
Bram Moolenaar958636c2014-10-21 20:01:58 +02002258 if (valid_yank_reg(*ea.arg, (ea.cmdidx != CMD_put
2259 && !IS_USER_CMDIDX(ea.cmdidx))))
Bram Moolenaar85de2062011-05-05 14:26:41 +02002260 {
2261 ea.regname = *ea.arg++;
2262#ifdef FEAT_EVAL
2263 /* for '=' register: accept the rest of the line as an expression */
2264 if (ea.arg[-1] == '=' && ea.arg[0] != NUL)
2265 {
2266 set_expr_line(vim_strsave(ea.arg));
2267 ea.arg += STRLEN(ea.arg);
2268 }
2269#endif
2270 ea.arg = skipwhite(ea.arg);
2271 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002272 }
2273
2274 /*
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002275 * Check for a count. When accepting a EX_BUFNAME, don't use "123foo" as a
Bram Moolenaar071d4272004-06-13 20:20:40 +00002276 * count, it's a buffer name.
2277 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002278 if ((ea.argt & EX_COUNT) && VIM_ISDIGIT(*ea.arg)
2279 && (!(ea.argt & EX_BUFNAME) || *(p = skipdigits(ea.arg)) == NUL
Bram Moolenaar1c465442017-03-12 20:10:05 +01002280 || VIM_ISWHITE(*p)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002281 {
2282 n = getdigits(&ea.arg);
2283 ea.arg = skipwhite(ea.arg);
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002284 if (n <= 0 && !ni && (ea.argt & EX_ZEROR) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002285 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002286 errormsg = _(e_zerocount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002287 goto doend;
2288 }
Bram Moolenaarb7316892019-05-01 18:08:42 +02002289 if (ea.addr_type != ADDR_LINES) // e.g. :buffer 2, :sleep 3
Bram Moolenaar071d4272004-06-13 20:20:40 +00002290 {
2291 ea.line2 = n;
2292 if (ea.addr_count == 0)
2293 ea.addr_count = 1;
2294 }
2295 else
2296 {
2297 ea.line1 = ea.line2;
2298 ea.line2 += n - 1;
2299 ++ea.addr_count;
2300 /*
2301 * Be vi compatible: no error message for out of range.
2302 */
Bram Moolenaarb7316892019-05-01 18:08:42 +02002303 if (ea.line2 > curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002304 ea.line2 = curbuf->b_ml.ml_line_count;
2305 }
2306 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00002307
2308 /*
2309 * Check for flags: 'l', 'p' and '#'.
2310 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002311 if (ea.argt & EX_FLAGS)
Bram Moolenaardf177f62005-02-22 08:39:57 +00002312 get_flags(&ea);
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002313 if (!ni && !(ea.argt & EX_EXTRA) && *ea.arg != NUL
2314 && *ea.arg != '"' && (*ea.arg != '|' || (ea.argt & EX_TRLBAR) == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002315 {
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002316 // no arguments allowed but there is something
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002317 errormsg = _(e_trailing);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002318 goto doend;
2319 }
2320
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002321 if (!ni && (ea.argt & EX_NEEDARG) && *ea.arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002322 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002323 errormsg = _(e_argreq);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002324 goto doend;
2325 }
2326
2327#ifdef FEAT_EVAL
2328 /*
2329 * Skip the command when it's not going to be executed.
2330 * The commands like :if, :endif, etc. always need to be executed.
2331 * Also make an exception for commands that handle a trailing command
2332 * themselves.
2333 */
2334 if (ea.skip)
2335 {
2336 switch (ea.cmdidx)
2337 {
2338 /* commands that need evaluation */
2339 case CMD_while:
2340 case CMD_endwhile:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00002341 case CMD_for:
2342 case CMD_endfor:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002343 case CMD_if:
2344 case CMD_elseif:
2345 case CMD_else:
2346 case CMD_endif:
2347 case CMD_try:
2348 case CMD_catch:
2349 case CMD_finally:
2350 case CMD_endtry:
2351 case CMD_function:
2352 break;
2353
2354 /* Commands that handle '|' themselves. Check: A command should
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002355 * either have the EX_TRLBAR flag, appear in this list or appear in
Bram Moolenaar071d4272004-06-13 20:20:40 +00002356 * the list at ":help :bar". */
2357 case CMD_aboveleft:
2358 case CMD_and:
2359 case CMD_belowright:
2360 case CMD_botright:
2361 case CMD_browse:
2362 case CMD_call:
2363 case CMD_confirm:
2364 case CMD_delfunction:
2365 case CMD_djump:
2366 case CMD_dlist:
2367 case CMD_dsearch:
2368 case CMD_dsplit:
2369 case CMD_echo:
2370 case CMD_echoerr:
2371 case CMD_echomsg:
2372 case CMD_echon:
2373 case CMD_execute:
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002374 case CMD_filter:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002375 case CMD_help:
2376 case CMD_hide:
2377 case CMD_ijump:
2378 case CMD_ilist:
2379 case CMD_isearch:
2380 case CMD_isplit:
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002381 case CMD_keepalt:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002382 case CMD_keepjumps:
2383 case CMD_keepmarks:
Bram Moolenaara939e432013-11-09 05:30:26 +01002384 case CMD_keeppatterns:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002385 case CMD_leftabove:
2386 case CMD_let:
2387 case CMD_lockmarks:
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002388 case CMD_lua:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002389 case CMD_match:
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002390 case CMD_mzscheme:
Bram Moolenaar5803ae62014-03-23 16:04:02 +01002391 case CMD_noautocmd:
2392 case CMD_noswapfile:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002393 case CMD_perl:
2394 case CMD_psearch:
2395 case CMD_python:
Bram Moolenaar368373e2010-07-19 20:46:22 +02002396 case CMD_py3:
Bram Moolenaarb6590522010-07-21 16:00:43 +02002397 case CMD_python3:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002398 case CMD_return:
2399 case CMD_rightbelow:
2400 case CMD_ruby:
2401 case CMD_silent:
2402 case CMD_smagic:
2403 case CMD_snomagic:
2404 case CMD_substitute:
2405 case CMD_syntax:
Bram Moolenaara226a6d2006-02-26 23:59:20 +00002406 case CMD_tab:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002407 case CMD_tcl:
2408 case CMD_throw:
2409 case CMD_tilde:
2410 case CMD_topleft:
2411 case CMD_unlet:
2412 case CMD_verbose:
2413 case CMD_vertical:
Bram Moolenaar12bde492011-06-13 01:19:56 +02002414 case CMD_wincmd:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002415 break;
2416
2417 default: goto doend;
2418 }
2419 }
2420#endif
2421
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002422 if (ea.argt & EX_XFILE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002423 {
2424 if (expand_filename(&ea, cmdlinep, &errormsg) == FAIL)
2425 goto doend;
2426 }
2427
Bram Moolenaar071d4272004-06-13 20:20:40 +00002428 /*
2429 * Accept buffer name. Cannot be used at the same time with a buffer
2430 * number. Don't do this for a user command.
2431 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002432 if ((ea.argt & EX_BUFNAME) && *ea.arg != NUL && ea.addr_count == 0
Bram Moolenaar958636c2014-10-21 20:01:58 +02002433 && !IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002434 {
2435 /*
2436 * :bdelete, :bwipeout and :bunload take several arguments, separated
2437 * by spaces: find next space (skipping over escaped characters).
2438 * The others take one argument: ignore trailing spaces.
2439 */
2440 if (ea.cmdidx == CMD_bdelete || ea.cmdidx == CMD_bwipeout
2441 || ea.cmdidx == CMD_bunload)
2442 p = skiptowhite_esc(ea.arg);
2443 else
2444 {
2445 p = ea.arg + STRLEN(ea.arg);
Bram Moolenaar1c465442017-03-12 20:10:05 +01002446 while (p > ea.arg && VIM_ISWHITE(p[-1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002447 --p;
2448 }
Bram Moolenaar8071cb22019-07-12 17:58:01 +02002449 ea.line2 = buflist_findpat(ea.arg, p, (ea.argt & EX_BUFUNL) != 0,
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002450 FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002451 if (ea.line2 < 0) /* failed */
2452 goto doend;
2453 ea.addr_count = 1;
2454 ea.arg = skipwhite(p);
2455 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002456
Bram Moolenaar2be57332018-02-13 18:05:18 +01002457 /* The :try command saves the emsg_silent flag, reset it here when
2458 * ":silent! try" was used, it should only apply to :try itself. */
Bram Moolenaareffed932018-08-14 13:38:17 +02002459 if (ea.cmdidx == CMD_try && ea.did_esilent > 0)
Bram Moolenaar2be57332018-02-13 18:05:18 +01002460 {
Bram Moolenaareffed932018-08-14 13:38:17 +02002461 emsg_silent -= ea.did_esilent;
Bram Moolenaar2be57332018-02-13 18:05:18 +01002462 if (emsg_silent < 0)
2463 emsg_silent = 0;
Bram Moolenaareffed932018-08-14 13:38:17 +02002464 ea.did_esilent = 0;
Bram Moolenaar2be57332018-02-13 18:05:18 +01002465 }
2466
Bram Moolenaar071d4272004-06-13 20:20:40 +00002467/*
Bram Moolenaar2be57332018-02-13 18:05:18 +01002468 * 7. Execute the command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002469 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002470
Bram Moolenaar958636c2014-10-21 20:01:58 +02002471 if (IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002472 {
2473 /*
2474 * Execute a user-defined command.
2475 */
2476 do_ucmd(&ea);
2477 }
2478 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002479 {
2480 /*
Bram Moolenaarac9fb182019-04-27 13:04:13 +02002481 * Call the function to execute the builtin command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002482 */
2483 ea.errmsg = NULL;
2484 (cmdnames[ea.cmdidx].cmd_func)(&ea);
2485 if (ea.errmsg != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002486 errormsg = _(ea.errmsg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002487 }
2488
2489#ifdef FEAT_EVAL
2490 /*
2491 * If the command just executed called do_cmdline(), any throw or ":return"
2492 * or ":finish" encountered there must also check the cstack of the still
2493 * active do_cmdline() that called this do_one_cmd(). Rethrow an uncaught
2494 * exception, or reanimate a returned function or finished script file and
2495 * return or finish it again.
2496 */
2497 if (need_rethrow)
2498 do_throw(cstack);
2499 else if (check_cstack)
2500 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002501 if (source_finished(fgetline, cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002502 do_finish(&ea, TRUE);
Bram Moolenaar89d40322006-08-29 15:30:07 +00002503 else if (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002504 && current_func_returned())
2505 do_return(&ea, TRUE, FALSE, NULL);
2506 }
2507 need_rethrow = check_cstack = FALSE;
2508#endif
2509
2510doend:
2511 if (curwin->w_cursor.lnum == 0) /* can happen with zero line number */
Bram Moolenaar6de5e122017-04-20 21:55:44 +02002512 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002513 curwin->w_cursor.lnum = 1;
Bram Moolenaar6de5e122017-04-20 21:55:44 +02002514 curwin->w_cursor.col = 0;
2515 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002516
2517 if (errormsg != NULL && *errormsg != NUL && !did_emsg)
2518 {
2519 if (sourcing)
2520 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002521 if (errormsg != (char *)IObuff)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002522 {
2523 STRCPY(IObuff, errormsg);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002524 errormsg = (char *)IObuff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002525 }
Bram Moolenaara6f4d612011-09-21 19:10:46 +02002526 append_command(*cmdlinep);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002527 }
2528 emsg(errormsg);
2529 }
2530#ifdef FEAT_EVAL
2531 do_errthrow(cstack,
Bram Moolenaar958636c2014-10-21 20:01:58 +02002532 (ea.cmdidx != CMD_SIZE && !IS_USER_CMDIDX(ea.cmdidx))
2533 ? cmdnames[(int)ea.cmdidx].cmd_name : (char_u *)NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002534#endif
2535
Bram Moolenaareffed932018-08-14 13:38:17 +02002536 if (ea.verbose_save >= 0)
2537 p_verbose = ea.verbose_save;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002538
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002539 free_cmdmod();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002540 cmdmod = save_cmdmod;
Bram Moolenaar9a2c0912019-03-30 14:26:18 +01002541 reg_executing = save_reg_executing;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002542
Bram Moolenaareffed932018-08-14 13:38:17 +02002543 if (ea.save_msg_silent != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002544 {
2545 /* messages could be enabled for a serious error, need to check if the
2546 * counters don't become negative */
Bram Moolenaareffed932018-08-14 13:38:17 +02002547 if (!did_emsg || msg_silent > ea.save_msg_silent)
2548 msg_silent = ea.save_msg_silent;
2549 emsg_silent -= ea.did_esilent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002550 if (emsg_silent < 0)
2551 emsg_silent = 0;
2552 /* Restore msg_scroll, it's set by file I/O commands, even when no
2553 * message is actually displayed. */
2554 msg_scroll = save_msg_scroll;
Bram Moolenaar77ab2802009-04-22 12:44:48 +00002555
2556 /* "silent reg" or "silent echo x" inside "redir" leaves msg_col
2557 * somewhere in the line. Put it back in the first column. */
2558 if (redirecting())
2559 msg_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002560 }
2561
Bram Moolenaar7171abe2004-10-11 10:06:20 +00002562#ifdef HAVE_SANDBOX
Bram Moolenaareffed932018-08-14 13:38:17 +02002563 if (ea.did_sandbox)
Bram Moolenaar7171abe2004-10-11 10:06:20 +00002564 --sandbox;
2565#endif
2566
Bram Moolenaar071d4272004-06-13 20:20:40 +00002567 if (ea.nextcmd && *ea.nextcmd == NUL) /* not really a next command */
2568 ea.nextcmd = NULL;
2569
2570#ifdef FEAT_EVAL
2571 --ex_nesting_level;
2572#endif
2573
2574 return ea.nextcmd;
2575}
2576#if (_MSC_VER == 1200)
Bram Moolenaar281bdce2005-01-25 21:53:18 +00002577 #pragma optimize( "", on )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002578#endif
2579
2580/*
Bram Moolenaareffed932018-08-14 13:38:17 +02002581 * Parse and skip over command modifiers:
2582 * - update eap->cmd
2583 * - store flags in "cmdmod".
2584 * - Set ex_pressedreturn for an empty command line.
2585 * - set msg_silent for ":silent"
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002586 * - set 'eventignore' to "all" for ":noautocmd"
Bram Moolenaareffed932018-08-14 13:38:17 +02002587 * - set p_verbose for ":verbose"
2588 * - Increment "sandbox" for ":sandbox"
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002589 * When "skip_only" is TRUE the global variables are not changed, except for
2590 * "cmdmod".
Bram Moolenaareffed932018-08-14 13:38:17 +02002591 * Return FAIL when the command is not to be executed.
2592 * May set "errormsg" to an error message.
2593 */
2594 int
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002595parse_command_modifiers(exarg_T *eap, char **errormsg, int skip_only)
Bram Moolenaareffed932018-08-14 13:38:17 +02002596{
2597 char_u *p;
2598
2599 vim_memset(&cmdmod, 0, sizeof(cmdmod));
2600 eap->verbose_save = -1;
2601 eap->save_msg_silent = -1;
2602
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002603 // Repeat until no more command modifiers are found.
Bram Moolenaareffed932018-08-14 13:38:17 +02002604 for (;;)
2605 {
Bram Moolenaareffed932018-08-14 13:38:17 +02002606 while (*eap->cmd == ' ' || *eap->cmd == '\t' || *eap->cmd == ':')
2607 ++eap->cmd;
2608
2609 /* in ex mode, an empty line works like :+ */
2610 if (*eap->cmd == NUL && exmode_active
2611 && (getline_equal(eap->getline, eap->cookie, getexmodeline)
2612 || getline_equal(eap->getline, eap->cookie, getexline))
2613 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
2614 {
2615 eap->cmd = (char_u *)"+";
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002616 if (!skip_only)
2617 ex_pressedreturn = TRUE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002618 }
2619
2620 /* ignore comment and empty lines */
2621 if (*eap->cmd == '"')
2622 return FAIL;
2623 if (*eap->cmd == NUL)
2624 {
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002625 if (!skip_only)
2626 ex_pressedreturn = TRUE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002627 return FAIL;
2628 }
2629
Bram Moolenaareffed932018-08-14 13:38:17 +02002630 p = skip_range(eap->cmd, NULL);
2631 switch (*p)
2632 {
2633 /* When adding an entry, also modify cmd_exists(). */
2634 case 'a': if (!checkforcmd(&eap->cmd, "aboveleft", 3))
2635 break;
2636 cmdmod.split |= WSP_ABOVE;
2637 continue;
2638
2639 case 'b': if (checkforcmd(&eap->cmd, "belowright", 3))
2640 {
2641 cmdmod.split |= WSP_BELOW;
2642 continue;
2643 }
2644 if (checkforcmd(&eap->cmd, "browse", 3))
2645 {
2646#ifdef FEAT_BROWSE_CMD
2647 cmdmod.browse = TRUE;
2648#endif
2649 continue;
2650 }
2651 if (!checkforcmd(&eap->cmd, "botright", 2))
2652 break;
2653 cmdmod.split |= WSP_BOT;
2654 continue;
2655
2656 case 'c': if (!checkforcmd(&eap->cmd, "confirm", 4))
2657 break;
2658#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
2659 cmdmod.confirm = TRUE;
2660#endif
2661 continue;
2662
2663 case 'k': if (checkforcmd(&eap->cmd, "keepmarks", 3))
2664 {
2665 cmdmod.keepmarks = TRUE;
2666 continue;
2667 }
2668 if (checkforcmd(&eap->cmd, "keepalt", 5))
2669 {
2670 cmdmod.keepalt = TRUE;
2671 continue;
2672 }
2673 if (checkforcmd(&eap->cmd, "keeppatterns", 5))
2674 {
2675 cmdmod.keeppatterns = TRUE;
2676 continue;
2677 }
2678 if (!checkforcmd(&eap->cmd, "keepjumps", 5))
2679 break;
2680 cmdmod.keepjumps = TRUE;
2681 continue;
2682
2683 case 'f': /* only accept ":filter {pat} cmd" */
2684 {
2685 char_u *reg_pat;
2686
2687 if (!checkforcmd(&p, "filter", 4)
2688 || *p == NUL || ends_excmd(*p))
2689 break;
2690 if (*p == '!')
2691 {
2692 cmdmod.filter_force = TRUE;
2693 p = skipwhite(p + 1);
2694 if (*p == NUL || ends_excmd(*p))
2695 break;
2696 }
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002697 if (skip_only)
2698 p = skip_vimgrep_pat(p, NULL, NULL);
2699 else
2700 // NOTE: This puts a NUL after the pattern.
2701 p = skip_vimgrep_pat(p, &reg_pat, NULL);
Bram Moolenaareffed932018-08-14 13:38:17 +02002702 if (p == NULL || *p == NUL)
2703 break;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002704 if (!skip_only)
2705 {
2706 cmdmod.filter_regmatch.regprog =
Bram Moolenaareffed932018-08-14 13:38:17 +02002707 vim_regcomp(reg_pat, RE_MAGIC);
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002708 if (cmdmod.filter_regmatch.regprog == NULL)
2709 break;
2710 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002711 eap->cmd = p;
2712 continue;
2713 }
2714
2715 /* ":hide" and ":hide | cmd" are not modifiers */
2716 case 'h': if (p != eap->cmd || !checkforcmd(&p, "hide", 3)
2717 || *p == NUL || ends_excmd(*p))
2718 break;
2719 eap->cmd = p;
2720 cmdmod.hide = TRUE;
2721 continue;
2722
2723 case 'l': if (checkforcmd(&eap->cmd, "lockmarks", 3))
2724 {
2725 cmdmod.lockmarks = TRUE;
2726 continue;
2727 }
2728
2729 if (!checkforcmd(&eap->cmd, "leftabove", 5))
2730 break;
2731 cmdmod.split |= WSP_ABOVE;
2732 continue;
2733
2734 case 'n': if (checkforcmd(&eap->cmd, "noautocmd", 3))
2735 {
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002736 if (cmdmod.save_ei == NULL && !skip_only)
Bram Moolenaareffed932018-08-14 13:38:17 +02002737 {
2738 /* Set 'eventignore' to "all". Restore the
2739 * existing option value later. */
2740 cmdmod.save_ei = vim_strsave(p_ei);
2741 set_string_option_direct((char_u *)"ei", -1,
2742 (char_u *)"all", OPT_FREE, SID_NONE);
2743 }
2744 continue;
2745 }
2746 if (!checkforcmd(&eap->cmd, "noswapfile", 3))
2747 break;
2748 cmdmod.noswapfile = TRUE;
2749 continue;
2750
2751 case 'r': if (!checkforcmd(&eap->cmd, "rightbelow", 6))
2752 break;
2753 cmdmod.split |= WSP_BELOW;
2754 continue;
2755
2756 case 's': if (checkforcmd(&eap->cmd, "sandbox", 3))
2757 {
2758#ifdef HAVE_SANDBOX
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002759 if (!skip_only)
2760 {
2761 if (!eap->did_sandbox)
2762 ++sandbox;
2763 eap->did_sandbox = TRUE;
2764 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002765#endif
2766 continue;
2767 }
2768 if (!checkforcmd(&eap->cmd, "silent", 3))
2769 break;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002770 if (!skip_only)
2771 {
2772 if (eap->save_msg_silent == -1)
2773 eap->save_msg_silent = msg_silent;
2774 ++msg_silent;
2775 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002776 if (*eap->cmd == '!' && !VIM_ISWHITE(eap->cmd[-1]))
2777 {
2778 /* ":silent!", but not "silent !cmd" */
2779 eap->cmd = skipwhite(eap->cmd + 1);
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002780 if (!skip_only)
2781 {
2782 ++emsg_silent;
2783 ++eap->did_esilent;
2784 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002785 }
2786 continue;
2787
2788 case 't': if (checkforcmd(&p, "tab", 3))
2789 {
Bram Moolenaar548e5982018-12-26 21:45:00 +01002790 if (!skip_only)
Bram Moolenaareffed932018-08-14 13:38:17 +02002791 {
Bram Moolenaar548e5982018-12-26 21:45:00 +01002792 long tabnr = get_address(eap, &eap->cmd,
2793 ADDR_TABS, eap->skip,
2794 skip_only, FALSE, 1);
2795 if (tabnr == MAXLNUM)
2796 cmdmod.tab = tabpage_index(curtab) + 1;
2797 else
Bram Moolenaareffed932018-08-14 13:38:17 +02002798 {
Bram Moolenaar548e5982018-12-26 21:45:00 +01002799 if (tabnr < 0 || tabnr > LAST_TAB_NR)
2800 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002801 *errormsg = _(e_invrange);
Bram Moolenaar548e5982018-12-26 21:45:00 +01002802 return FAIL;
2803 }
2804 cmdmod.tab = tabnr + 1;
Bram Moolenaareffed932018-08-14 13:38:17 +02002805 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002806 }
2807 eap->cmd = p;
2808 continue;
2809 }
2810 if (!checkforcmd(&eap->cmd, "topleft", 2))
2811 break;
2812 cmdmod.split |= WSP_TOP;
2813 continue;
2814
2815 case 'u': if (!checkforcmd(&eap->cmd, "unsilent", 3))
2816 break;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002817 if (!skip_only)
2818 {
2819 if (eap->save_msg_silent == -1)
2820 eap->save_msg_silent = msg_silent;
2821 msg_silent = 0;
2822 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002823 continue;
2824
2825 case 'v': if (checkforcmd(&eap->cmd, "vertical", 4))
2826 {
2827 cmdmod.split |= WSP_VERT;
2828 continue;
2829 }
2830 if (!checkforcmd(&p, "verbose", 4))
2831 break;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002832 if (!skip_only)
2833 {
2834 if (eap->verbose_save < 0)
2835 eap->verbose_save = p_verbose;
2836 if (vim_isdigit(*eap->cmd))
2837 p_verbose = atoi((char *)eap->cmd);
2838 else
2839 p_verbose = 1;
2840 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002841 eap->cmd = p;
2842 continue;
2843 }
2844 break;
2845 }
2846
2847 return OK;
2848}
2849
2850/*
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002851 * Free contents of "cmdmod".
2852 */
2853 static void
2854free_cmdmod(void)
2855{
2856 if (cmdmod.save_ei != NULL)
2857 {
2858 /* Restore 'eventignore' to the value before ":noautocmd". */
2859 set_string_option_direct((char_u *)"ei", -1, cmdmod.save_ei,
2860 OPT_FREE, SID_NONE);
2861 free_string_option(cmdmod.save_ei);
2862 }
2863
2864 if (cmdmod.filter_regmatch.regprog != NULL)
2865 vim_regfree(cmdmod.filter_regmatch.regprog);
2866}
2867
2868/*
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002869 * Parse the address range, if any, in "eap".
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02002870 * May set the last search pattern, unless "silent" is TRUE.
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002871 * Return FAIL and set "errormsg" or return OK.
2872 */
2873 int
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002874parse_cmd_address(exarg_T *eap, char **errormsg, int silent)
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002875{
2876 int address_count = 1;
2877 linenr_T lnum;
2878
2879 // Repeat for all ',' or ';' separated addresses.
2880 for (;;)
2881 {
2882 eap->line1 = eap->line2;
2883 switch (eap->addr_type)
2884 {
2885 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02002886 case ADDR_OTHER:
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002887 // default is current line number
2888 eap->line2 = curwin->w_cursor.lnum;
2889 break;
2890 case ADDR_WINDOWS:
2891 eap->line2 = CURRENT_WIN_NR;
2892 break;
2893 case ADDR_ARGUMENTS:
2894 eap->line2 = curwin->w_arg_idx + 1;
2895 if (eap->line2 > ARGCOUNT)
2896 eap->line2 = ARGCOUNT;
2897 break;
2898 case ADDR_LOADED_BUFFERS:
2899 case ADDR_BUFFERS:
2900 eap->line2 = curbuf->b_fnum;
2901 break;
2902 case ADDR_TABS:
2903 eap->line2 = CURRENT_TAB_NR;
2904 break;
2905 case ADDR_TABS_RELATIVE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02002906 case ADDR_UNSIGNED:
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002907 eap->line2 = 1;
2908 break;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002909 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02002910#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02002911 eap->line2 = qf_get_cur_idx(eap);
2912#endif
2913 break;
2914 case ADDR_QUICKFIX_VALID:
2915#ifdef FEAT_QUICKFIX
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002916 eap->line2 = qf_get_cur_valid_idx(eap);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002917#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02002918 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02002919 case ADDR_NONE:
2920 // Will give an error later if a range is found.
2921 break;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002922 }
2923 eap->cmd = skipwhite(eap->cmd);
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02002924 lnum = get_address(eap, &eap->cmd, eap->addr_type, eap->skip, silent,
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002925 eap->addr_count == 0, address_count++);
2926 if (eap->cmd == NULL) // error detected
2927 return FAIL;
2928 if (lnum == MAXLNUM)
2929 {
2930 if (*eap->cmd == '%') // '%' - all lines
2931 {
2932 ++eap->cmd;
2933 switch (eap->addr_type)
2934 {
2935 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02002936 case ADDR_OTHER:
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002937 eap->line1 = 1;
2938 eap->line2 = curbuf->b_ml.ml_line_count;
2939 break;
2940 case ADDR_LOADED_BUFFERS:
2941 {
2942 buf_T *buf = firstbuf;
2943
2944 while (buf->b_next != NULL
2945 && buf->b_ml.ml_mfp == NULL)
2946 buf = buf->b_next;
2947 eap->line1 = buf->b_fnum;
2948 buf = lastbuf;
2949 while (buf->b_prev != NULL
2950 && buf->b_ml.ml_mfp == NULL)
2951 buf = buf->b_prev;
2952 eap->line2 = buf->b_fnum;
2953 break;
2954 }
2955 case ADDR_BUFFERS:
2956 eap->line1 = firstbuf->b_fnum;
2957 eap->line2 = lastbuf->b_fnum;
2958 break;
2959 case ADDR_WINDOWS:
2960 case ADDR_TABS:
2961 if (IS_USER_CMDIDX(eap->cmdidx))
2962 {
2963 eap->line1 = 1;
2964 eap->line2 = eap->addr_type == ADDR_WINDOWS
2965 ? LAST_WIN_NR : LAST_TAB_NR;
2966 }
2967 else
2968 {
2969 // there is no Vim command which uses '%' and
2970 // ADDR_WINDOWS or ADDR_TABS
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002971 *errormsg = _(e_invrange);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002972 return FAIL;
2973 }
2974 break;
2975 case ADDR_TABS_RELATIVE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02002976 case ADDR_UNSIGNED:
2977 case ADDR_QUICKFIX:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002978 *errormsg = _(e_invrange);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002979 return FAIL;
2980 case ADDR_ARGUMENTS:
2981 if (ARGCOUNT == 0)
2982 eap->line1 = eap->line2 = 0;
2983 else
2984 {
2985 eap->line1 = 1;
2986 eap->line2 = ARGCOUNT;
2987 }
2988 break;
Bram Moolenaar25190db2019-05-04 15:05:28 +02002989 case ADDR_QUICKFIX_VALID:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02002990#ifdef FEAT_QUICKFIX
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002991 eap->line1 = 1;
Bram Moolenaar25190db2019-05-04 15:05:28 +02002992 eap->line2 = qf_get_valid_size(eap);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002993 if (eap->line2 == 0)
2994 eap->line2 = 1;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002995#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02002996 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02002997 case ADDR_NONE:
2998 // Will give an error later if a range is found.
2999 break;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003000 }
3001 ++eap->addr_count;
3002 }
3003 else if (*eap->cmd == '*' && vim_strchr(p_cpo, CPO_STAR) == NULL)
3004 {
3005 pos_T *fp;
3006
3007 // '*' - visual area
3008 if (eap->addr_type != ADDR_LINES)
3009 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003010 *errormsg = _(e_invrange);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003011 return FAIL;
3012 }
3013
3014 ++eap->cmd;
3015 if (!eap->skip)
3016 {
3017 fp = getmark('<', FALSE);
3018 if (check_mark(fp) == FAIL)
3019 return FAIL;
3020 eap->line1 = fp->lnum;
3021 fp = getmark('>', FALSE);
3022 if (check_mark(fp) == FAIL)
3023 return FAIL;
3024 eap->line2 = fp->lnum;
3025 ++eap->addr_count;
3026 }
3027 }
3028 }
3029 else
3030 eap->line2 = lnum;
3031 eap->addr_count++;
3032
3033 if (*eap->cmd == ';')
3034 {
3035 if (!eap->skip)
3036 {
3037 curwin->w_cursor.lnum = eap->line2;
3038 // don't leave the cursor on an illegal line or column
3039 check_cursor();
3040 }
3041 }
3042 else if (*eap->cmd != ',')
3043 break;
3044 ++eap->cmd;
3045 }
3046
3047 // One address given: set start and end lines.
3048 if (eap->addr_count == 1)
3049 {
3050 eap->line1 = eap->line2;
3051 // ... but only implicit: really no address given
3052 if (lnum == MAXLNUM)
3053 eap->addr_count = 0;
3054 }
3055 return OK;
3056}
3057
3058/*
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00003059 * Check for an Ex command with optional tail.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003060 * If there is a match advance "pp" to the argument and return TRUE.
3061 */
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00003062 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003063checkforcmd(
3064 char_u **pp, /* start of command */
3065 char *cmd, /* name of command */
3066 int len) /* required length */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003067{
3068 int i;
3069
3070 for (i = 0; cmd[i] != NUL; ++i)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003071 if (((char_u *)cmd)[i] != (*pp)[i])
Bram Moolenaar071d4272004-06-13 20:20:40 +00003072 break;
3073 if (i >= len && !isalpha((*pp)[i]))
3074 {
3075 *pp = skipwhite(*pp + i);
3076 return TRUE;
3077 }
3078 return FALSE;
3079}
3080
3081/*
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003082 * Append "cmd" to the error message in IObuff.
3083 * Takes care of limiting the length and handling 0xa0, which would be
3084 * invisible otherwise.
3085 */
3086 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003087append_command(char_u *cmd)
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003088{
3089 char_u *s = cmd;
3090 char_u *d;
3091
3092 STRCAT(IObuff, ": ");
3093 d = IObuff + STRLEN(IObuff);
3094 while (*s != NUL && d - IObuff < IOSIZE - 7)
3095 {
Bram Moolenaar13505972019-01-24 15:04:48 +01003096 if (enc_utf8 ? (s[0] == 0xc2 && s[1] == 0xa0) : *s == 0xa0)
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003097 {
Bram Moolenaar13505972019-01-24 15:04:48 +01003098 s += enc_utf8 ? 2 : 1;
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003099 STRCPY(d, "<a0>");
3100 d += 4;
3101 }
3102 else
3103 MB_COPY_CHAR(s, d);
3104 }
3105 *d = NUL;
3106}
3107
3108/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003109 * Find an Ex command by its name, either built-in or user.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003110 * Start of the name can be found at eap->cmd.
Bram Moolenaar25190db2019-05-04 15:05:28 +02003111 * Sets eap->cmdidx and returns a pointer to char after the command name.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003112 * "full" is set to TRUE if the whole command name matched.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003113 * Returns NULL for an ambiguous user command.
3114 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003115 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003116find_command(exarg_T *eap, int *full UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003117{
3118 int len;
3119 char_u *p;
Bram Moolenaardf177f62005-02-22 08:39:57 +00003120 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003121
3122 /*
3123 * Isolate the command and search for it in the command table.
Bram Moolenaar81870892007-11-11 18:17:28 +00003124 * Exceptions:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003125 * - the 'k' command can directly be followed by any character.
3126 * - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003127 * but :sre[wind] is another command, as are :scr[iptnames],
Bram Moolenaar071d4272004-06-13 20:20:40 +00003128 * :scs[cope], :sim[alt], :sig[ns] and :sil[ent].
Bram Moolenaardf177f62005-02-22 08:39:57 +00003129 * - the "d" command can directly be followed by 'l' or 'p' flag.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003130 */
3131 p = eap->cmd;
3132 if (*p == 'k')
3133 {
3134 eap->cmdidx = CMD_k;
3135 ++p;
3136 }
3137 else if (p[0] == 's'
Bram Moolenaar204b93f2015-08-04 22:02:51 +02003138 && ((p[1] == 'c' && (p[2] == NUL || (p[2] != 's' && p[2] != 'r'
3139 && (p[3] == NUL || (p[3] != 'i' && p[4] != 'p')))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003140 || p[1] == 'g'
3141 || (p[1] == 'i' && p[2] != 'm' && p[2] != 'l' && p[2] != 'g')
3142 || p[1] == 'I'
3143 || (p[1] == 'r' && p[2] != 'e')))
3144 {
3145 eap->cmdidx = CMD_substitute;
3146 ++p;
3147 }
3148 else
3149 {
3150 while (ASCII_ISALPHA(*p))
3151 ++p;
Bram Moolenaarb6590522010-07-21 16:00:43 +02003152 /* for python 3.x support ":py3", ":python3", ":py3file", etc. */
Bram Moolenaar55d5c032010-07-17 23:52:29 +02003153 if (eap->cmd[0] == 'p' && eap->cmd[1] == 'y')
Bram Moolenaarb6590522010-07-21 16:00:43 +02003154 while (ASCII_ISALNUM(*p))
3155 ++p;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02003156
Bram Moolenaar071d4272004-06-13 20:20:40 +00003157 /* check for non-alpha command */
3158 if (p == eap->cmd && vim_strchr((char_u *)"@*!=><&~#", *p) != NULL)
3159 ++p;
3160 len = (int)(p - eap->cmd);
Bram Moolenaardf177f62005-02-22 08:39:57 +00003161 if (*eap->cmd == 'd' && (p[-1] == 'l' || p[-1] == 'p'))
3162 {
3163 /* Check for ":dl", ":dell", etc. to ":deletel": that's
3164 * :delete with the 'l' flag. Same for 'p'. */
3165 for (i = 0; i < len; ++i)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003166 if (eap->cmd[i] != ((char_u *)"delete")[i])
Bram Moolenaardf177f62005-02-22 08:39:57 +00003167 break;
3168 if (i == len - 1)
3169 {
3170 --len;
3171 if (p[-1] == 'l')
3172 eap->flags |= EXFLAG_LIST;
3173 else
3174 eap->flags |= EXFLAG_PRINT;
3175 }
3176 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003177
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003178 if (ASCII_ISLOWER(eap->cmd[0]))
3179 {
Bram Moolenaar6c0c1e82017-03-25 15:07:43 +01003180 int c1 = eap->cmd[0];
Bram Moolenaar94f82cb2019-07-24 22:30:27 +02003181 int c2 = len == 1 ? NUL : eap->cmd[1];
Bram Moolenaar6c0c1e82017-03-25 15:07:43 +01003182
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003183 if (command_count != (int)CMD_SIZE)
3184 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003185 iemsg(_("E943: Command table needs to be updated, run 'make cmdidxs'"));
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003186 getout(1);
3187 }
3188
3189 /* Use a precomputed index for fast look-up in cmdnames[]
3190 * taking into account the first 2 letters of eap->cmd. */
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003191 eap->cmdidx = cmdidxs1[CharOrdLow(c1)];
3192 if (ASCII_ISLOWER(c2))
3193 eap->cmdidx += cmdidxs2[CharOrdLow(c1)][CharOrdLow(c2)];
3194 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003195 else
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003196 eap->cmdidx = CMD_bang;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003197
3198 for ( ; (int)eap->cmdidx < (int)CMD_SIZE;
3199 eap->cmdidx = (cmdidx_T)((int)eap->cmdidx + 1))
3200 if (STRNCMP(cmdnames[(int)eap->cmdidx].cmd_name, (char *)eap->cmd,
3201 (size_t)len) == 0)
3202 {
3203#ifdef FEAT_EVAL
3204 if (full != NULL
3205 && cmdnames[(int)eap->cmdidx].cmd_name[len] == NUL)
3206 *full = TRUE;
3207#endif
3208 break;
3209 }
3210
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003211 // Look for a user defined command as a last resort. Let ":Print" be
3212 // overruled by a user defined command.
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01003213 if ((eap->cmdidx == CMD_SIZE || eap->cmdidx == CMD_Print)
3214 && *eap->cmd >= 'A' && *eap->cmd <= 'Z')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003215 {
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003216 // User defined commands may contain digits.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003217 while (ASCII_ISALNUM(*p))
3218 ++p;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003219 p = find_ucmd(eap, p, full, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003220 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003221 if (p == eap->cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003222 eap->cmdidx = CMD_SIZE;
3223 }
3224
3225 return p;
3226}
3227
3228#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003229static struct cmdmod
3230{
3231 char *name;
3232 int minlen;
3233 int has_count; /* :123verbose :3tab */
3234} cmdmods[] = {
3235 {"aboveleft", 3, FALSE},
3236 {"belowright", 3, FALSE},
3237 {"botright", 2, FALSE},
3238 {"browse", 3, FALSE},
3239 {"confirm", 4, FALSE},
Bram Moolenaar7b668e82016-08-23 23:51:21 +02003240 {"filter", 4, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003241 {"hide", 3, FALSE},
3242 {"keepalt", 5, FALSE},
3243 {"keepjumps", 5, FALSE},
3244 {"keepmarks", 3, FALSE},
Bram Moolenaara939e432013-11-09 05:30:26 +01003245 {"keeppatterns", 5, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003246 {"leftabove", 5, FALSE},
3247 {"lockmarks", 3, FALSE},
Bram Moolenaarca9f9582008-09-18 10:44:28 +00003248 {"noautocmd", 3, FALSE},
Bram Moolenaar5803ae62014-03-23 16:04:02 +01003249 {"noswapfile", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003250 {"rightbelow", 6, FALSE},
3251 {"sandbox", 3, FALSE},
3252 {"silent", 3, FALSE},
3253 {"tab", 3, TRUE},
3254 {"topleft", 2, FALSE},
Bram Moolenaar8e258a42009-07-09 13:55:43 +00003255 {"unsilent", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003256 {"verbose", 4, TRUE},
3257 {"vertical", 4, FALSE},
3258};
3259
3260/*
3261 * Return length of a command modifier (including optional count).
3262 * Return zero when it's not a modifier.
3263 */
3264 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003265modifier_len(char_u *cmd)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003266{
3267 int i, j;
3268 char_u *p = cmd;
3269
3270 if (VIM_ISDIGIT(*cmd))
3271 p = skipwhite(skipdigits(cmd));
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00003272 for (i = 0; i < (int)(sizeof(cmdmods) / sizeof(struct cmdmod)); ++i)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003273 {
3274 for (j = 0; p[j] != NUL; ++j)
3275 if (p[j] != cmdmods[i].name[j])
3276 break;
Bram Moolenaar2d473ab2013-06-12 17:12:24 +02003277 if (!ASCII_ISALPHA(p[j]) && j >= cmdmods[i].minlen
Bram Moolenaared53fb92007-11-24 20:50:24 +00003278 && (p == cmd || cmdmods[i].has_count))
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00003279 return j + (int)(p - cmd);
Bram Moolenaared53fb92007-11-24 20:50:24 +00003280 }
3281 return 0;
3282}
3283
Bram Moolenaar071d4272004-06-13 20:20:40 +00003284/*
3285 * Return > 0 if an Ex command "name" exists.
3286 * Return 2 if there is an exact match.
3287 * Return 3 if there is an ambiguous match.
3288 */
3289 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003290cmd_exists(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003291{
3292 exarg_T ea;
3293 int full = FALSE;
3294 int i;
3295 int j;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003296 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003297
3298 /* Check command modifiers. */
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00003299 for (i = 0; i < (int)(sizeof(cmdmods) / sizeof(struct cmdmod)); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003300 {
3301 for (j = 0; name[j] != NUL; ++j)
3302 if (name[j] != cmdmods[i].name[j])
3303 break;
3304 if (name[j] == NUL && j >= cmdmods[i].minlen)
3305 return (cmdmods[i].name[j] == NUL ? 2 : 1);
3306 }
3307
Bram Moolenaara9587612006-05-04 21:47:50 +00003308 /* Check built-in commands and user defined commands.
3309 * For ":2match" and ":3match" we need to skip the number. */
3310 ea.cmd = (*name == '2' || *name == '3') ? name + 1 : name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003311 ea.cmdidx = (cmdidx_T)0;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003312 p = find_command(&ea, &full);
3313 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003314 return 3;
Bram Moolenaara9587612006-05-04 21:47:50 +00003315 if (vim_isdigit(*name) && ea.cmdidx != CMD_match)
3316 return 0;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003317 if (*skipwhite(p) != NUL)
3318 return 0; /* trailing garbage */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003319 return (ea.cmdidx == CMD_SIZE ? 0 : (full ? 2 : 1));
3320}
3321#endif
3322
3323/*
3324 * This is all pretty much copied from do_one_cmd(), with all the extra stuff
3325 * we don't need/want deleted. Maybe this could be done better if we didn't
3326 * repeat all this stuff. The only problem is that they may not stay
3327 * perfectly compatible with each other, but then the command line syntax
3328 * probably won't change that much -- webb.
3329 */
3330 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003331set_one_cmd_context(
3332 expand_T *xp,
3333 char_u *buff) /* buffer for command string */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003334{
3335 char_u *p;
3336 char_u *cmd, *arg;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003337 int len = 0;
3338 exarg_T ea;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003339#ifdef FEAT_CMDL_COMPL
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003340 int compl = EXPAND_NOTHING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003341 int delim;
3342#endif
3343 int forceit = FALSE;
3344 int usefilter = FALSE; /* filter instead of file name */
3345
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003346 ExpandInit(xp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003347 xp->xp_pattern = buff;
3348 xp->xp_context = EXPAND_COMMANDS; /* Default until we get past command */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003349 ea.argt = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003350
3351/*
Bram Moolenaar1c40a662014-11-27 16:38:11 +01003352 * 1. skip comment lines and leading space, colons or bars
Bram Moolenaar071d4272004-06-13 20:20:40 +00003353 */
3354 for (cmd = buff; vim_strchr((char_u *)" \t:|", *cmd) != NULL; cmd++)
3355 ;
3356 xp->xp_pattern = cmd;
3357
3358 if (*cmd == NUL)
3359 return NULL;
3360 if (*cmd == '"') /* ignore comment lines */
3361 {
3362 xp->xp_context = EXPAND_NOTHING;
3363 return NULL;
3364 }
3365
3366/*
Bram Moolenaar1c40a662014-11-27 16:38:11 +01003367 * 3. Skip over the range to find the command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003368 */
3369 cmd = skip_range(cmd, &xp->xp_context);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003370 xp->xp_pattern = cmd;
3371 if (*cmd == NUL)
3372 return NULL;
3373 if (*cmd == '"')
3374 {
3375 xp->xp_context = EXPAND_NOTHING;
3376 return NULL;
3377 }
3378
3379 if (*cmd == '|' || *cmd == '\n')
3380 return cmd + 1; /* There's another command */
3381
3382 /*
3383 * Isolate the command and search for it in the command table.
3384 * Exceptions:
3385 * - the 'k' command can directly be followed by any character, but
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003386 * do accept "keepmarks", "keepalt" and "keepjumps".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003387 * - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
3388 */
3389 if (*cmd == 'k' && cmd[1] != 'e')
3390 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003391 ea.cmdidx = CMD_k;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003392 p = cmd + 1;
3393 }
3394 else
3395 {
3396 p = cmd;
3397 while (ASCII_ISALPHA(*p) || *p == '*') /* Allow * wild card */
3398 ++p;
Bram Moolenaar23d1b622015-10-13 19:18:04 +02003399 /* a user command may contain digits */
3400 if (ASCII_ISUPPER(cmd[0]))
3401 while (ASCII_ISALNUM(*p) || *p == '*')
3402 ++p;
Bram Moolenaar9f5d6002013-06-02 19:22:13 +02003403 /* for python 3.x: ":py3*" commands completion */
3404 if (cmd[0] == 'p' && cmd[1] == 'y' && p == cmd + 2 && *p == '3')
Bram Moolenaar893b2d72013-12-11 17:44:38 +01003405 {
Bram Moolenaar9f5d6002013-06-02 19:22:13 +02003406 ++p;
Bram Moolenaar893b2d72013-12-11 17:44:38 +01003407 while (ASCII_ISALPHA(*p) || *p == '*')
3408 ++p;
3409 }
Bram Moolenaar23d1b622015-10-13 19:18:04 +02003410 /* check for non-alpha command */
3411 if (p == cmd && vim_strchr((char_u *)"@*!=><&~#", *p) != NULL)
3412 ++p;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003413 len = (int)(p - cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003414
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003415 if (len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003416 {
3417 xp->xp_context = EXPAND_UNSUCCESSFUL;
3418 return NULL;
3419 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003420 for (ea.cmdidx = (cmdidx_T)0; (int)ea.cmdidx < (int)CMD_SIZE;
Bram Moolenaar42b4dda2010-03-02 15:56:05 +01003421 ea.cmdidx = (cmdidx_T)((int)ea.cmdidx + 1))
3422 if (STRNCMP(cmdnames[(int)ea.cmdidx].cmd_name, cmd,
3423 (size_t)len) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003424 break;
3425
Bram Moolenaar071d4272004-06-13 20:20:40 +00003426 if (cmd[0] >= 'A' && cmd[0] <= 'Z')
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003427 while (ASCII_ISALNUM(*p) || *p == '*') // Allow * wild card
Bram Moolenaar071d4272004-06-13 20:20:40 +00003428 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003429 }
3430
3431 /*
3432 * If the cursor is touching the command, and it ends in an alpha-numeric
3433 * character, complete the command name.
3434 */
3435 if (*p == NUL && ASCII_ISALNUM(p[-1]))
3436 return NULL;
3437
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003438 if (ea.cmdidx == CMD_SIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003439 {
3440 if (*cmd == 's' && vim_strchr((char_u *)"cgriI", cmd[1]) != NULL)
3441 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003442 ea.cmdidx = CMD_substitute;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003443 p = cmd + 1;
3444 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003445 else if (cmd[0] >= 'A' && cmd[0] <= 'Z')
3446 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003447 ea.cmd = cmd;
3448 p = find_ucmd(&ea, p, NULL, xp,
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003449#if defined(FEAT_CMDL_COMPL)
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003450 &compl
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003451#else
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003452 NULL
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003453#endif
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003454 );
Bram Moolenaarebefac62005-12-28 22:39:57 +00003455 if (p == NULL)
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003456 ea.cmdidx = CMD_SIZE; // ambiguous user command
Bram Moolenaar071d4272004-06-13 20:20:40 +00003457 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003458 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003459 if (ea.cmdidx == CMD_SIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003460 {
3461 /* Not still touching the command and it was an illegal one */
3462 xp->xp_context = EXPAND_UNSUCCESSFUL;
3463 return NULL;
3464 }
3465
3466 xp->xp_context = EXPAND_NOTHING; /* Default now that we're past command */
3467
3468 if (*p == '!') /* forced commands */
3469 {
3470 forceit = TRUE;
3471 ++p;
3472 }
3473
3474/*
Bram Moolenaar1c40a662014-11-27 16:38:11 +01003475 * 6. parse arguments
Bram Moolenaar071d4272004-06-13 20:20:40 +00003476 */
Bram Moolenaar958636c2014-10-21 20:01:58 +02003477 if (!IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003478 ea.argt = (long)cmdnames[(int)ea.cmdidx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003479
3480 arg = skipwhite(p);
3481
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003482 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003483 {
3484 if (*arg == '>') /* append */
3485 {
3486 if (*++arg == '>')
3487 ++arg;
3488 arg = skipwhite(arg);
3489 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003490 else if (*arg == '!' && ea.cmdidx == CMD_write) /* :w !filter */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003491 {
3492 ++arg;
3493 usefilter = TRUE;
3494 }
3495 }
3496
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003497 if (ea.cmdidx == CMD_read)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003498 {
3499 usefilter = forceit; /* :r! filter if forced */
3500 if (*arg == '!') /* :r !filter */
3501 {
3502 ++arg;
3503 usefilter = TRUE;
3504 }
3505 }
3506
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003507 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003508 {
3509 while (*arg == *cmd) /* allow any number of '>' or '<' */
3510 ++arg;
3511 arg = skipwhite(arg);
3512 }
3513
3514 /* Does command allow "+command"? */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02003515 if ((ea.argt & EX_CMDARG) && !usefilter && *arg == '+')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003516 {
3517 /* Check if we're in the +command */
3518 p = arg + 1;
3519 arg = skip_cmd_arg(arg, FALSE);
3520
3521 /* Still touching the command after '+'? */
3522 if (*arg == NUL)
3523 return p;
3524
3525 /* Skip space(s) after +command to get to the real argument */
3526 arg = skipwhite(arg);
3527 }
3528
3529 /*
3530 * Check for '|' to separate commands and '"' to start comments.
3531 * Don't do this for ":read !cmd" and ":write !cmd".
3532 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02003533 if ((ea.argt & EX_TRLBAR) && !usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003534 {
3535 p = arg;
3536 /* ":redir @" is not the start of a comment */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003537 if (ea.cmdidx == CMD_redir && p[0] == '@' && p[1] == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003538 p += 2;
3539 while (*p)
3540 {
3541 if (*p == Ctrl_V)
3542 {
3543 if (p[1] != NUL)
3544 ++p;
3545 }
Bram Moolenaar8071cb22019-07-12 17:58:01 +02003546 else if ( (*p == '"' && !(ea.argt & EX_NOTRLCOM))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003547 || *p == '|' || *p == '\n')
3548 {
3549 if (*(p - 1) != '\\')
3550 {
3551 if (*p == '|' || *p == '\n')
3552 return p + 1;
3553 return NULL; /* It's a comment */
3554 }
3555 }
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003556 MB_PTR_ADV(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003557 }
3558 }
3559
Bram Moolenaar8071cb22019-07-12 17:58:01 +02003560 if (!(ea.argt & EX_EXTRA) && *arg != NUL
3561 && vim_strchr((char_u *)"|\"", *arg) == NULL)
3562 // no arguments allowed but there is something
Bram Moolenaar071d4272004-06-13 20:20:40 +00003563 return NULL;
3564
3565 /* Find start of last argument (argument just before cursor): */
Bram Moolenaar848f8762012-07-25 17:22:23 +02003566 p = buff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003567 xp->xp_pattern = p;
Bram Moolenaar09168a72012-08-02 21:24:42 +02003568 len = (int)STRLEN(buff);
Bram Moolenaar848f8762012-07-25 17:22:23 +02003569 while (*p && p < buff + len)
3570 {
3571 if (*p == ' ' || *p == TAB)
3572 {
3573 /* argument starts after a space */
3574 xp->xp_pattern = ++p;
3575 }
3576 else
3577 {
3578 if (*p == '\\' && *(p + 1) != NUL)
3579 ++p; /* skip over escaped character */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003580 MB_PTR_ADV(p);
Bram Moolenaar848f8762012-07-25 17:22:23 +02003581 }
3582 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003583
Bram Moolenaar8071cb22019-07-12 17:58:01 +02003584 if (ea.argt & EX_XFILE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003585 {
Bram Moolenaar6529c102007-08-18 15:47:34 +00003586 int c;
3587 int in_quote = FALSE;
3588 char_u *bow = NULL; /* Beginning of word */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003589
3590 /*
3591 * Allow spaces within back-quotes to count as part of the argument
3592 * being expanded.
3593 */
3594 xp->xp_pattern = skipwhite(arg);
Bram Moolenaar6529c102007-08-18 15:47:34 +00003595 p = xp->xp_pattern;
3596 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003597 {
Bram Moolenaar6529c102007-08-18 15:47:34 +00003598 if (has_mbyte)
3599 c = mb_ptr2char(p);
3600 else
Bram Moolenaar6529c102007-08-18 15:47:34 +00003601 c = *p;
3602 if (c == '\\' && p[1] != NUL)
3603 ++p;
3604 else if (c == '`')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003605 {
3606 if (!in_quote)
3607 {
3608 xp->xp_pattern = p;
3609 bow = p + 1;
3610 }
3611 in_quote = !in_quote;
3612 }
Bram Moolenaar332fa0c2008-01-13 16:12:10 +00003613 /* An argument can contain just about everything, except
3614 * characters that end the command and white space. */
Bram Moolenaar1c465442017-03-12 20:10:05 +01003615 else if (c == '|' || c == '\n' || c == '"' || (VIM_ISWHITE(c)
Bram Moolenaar6529c102007-08-18 15:47:34 +00003616#ifdef SPACE_IN_FILENAME
Bram Moolenaar8071cb22019-07-12 17:58:01 +02003617 && (!(ea.argt & EX_NOSPC) || usefilter)
Bram Moolenaar6529c102007-08-18 15:47:34 +00003618#endif
Bram Moolenaar332fa0c2008-01-13 16:12:10 +00003619 ))
Bram Moolenaar6529c102007-08-18 15:47:34 +00003620 {
Bram Moolenaarcf5a5b82008-02-26 20:30:12 +00003621 len = 0; /* avoid getting stuck when space is in 'isfname' */
Bram Moolenaar6529c102007-08-18 15:47:34 +00003622 while (*p != NUL)
3623 {
Bram Moolenaar6529c102007-08-18 15:47:34 +00003624 if (has_mbyte)
3625 c = mb_ptr2char(p);
3626 else
Bram Moolenaar6529c102007-08-18 15:47:34 +00003627 c = *p;
Bram Moolenaardd87969c2007-08-21 13:07:12 +00003628 if (c == '`' || vim_isfilec_or_wc(c))
Bram Moolenaar6529c102007-08-18 15:47:34 +00003629 break;
Bram Moolenaar6529c102007-08-18 15:47:34 +00003630 if (has_mbyte)
3631 len = (*mb_ptr2len)(p);
3632 else
Bram Moolenaar6529c102007-08-18 15:47:34 +00003633 len = 1;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003634 MB_PTR_ADV(p);
Bram Moolenaar6529c102007-08-18 15:47:34 +00003635 }
3636 if (in_quote)
3637 bow = p;
3638 else
3639 xp->xp_pattern = p;
3640 p -= len;
3641 }
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003642 MB_PTR_ADV(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003643 }
3644
3645 /*
3646 * If we are still inside the quotes, and we passed a space, just
3647 * expand from there.
3648 */
3649 if (bow != NULL && in_quote)
3650 xp->xp_pattern = bow;
3651 xp->xp_context = EXPAND_FILES;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003652
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003653 /* For a shell command more chars need to be escaped. */
Bram Moolenaar67883b42017-07-27 22:57:00 +02003654 if (usefilter || ea.cmdidx == CMD_bang || ea.cmdidx == CMD_terminal)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003655 {
Bram Moolenaarc0cba4d2010-08-07 17:07:21 +02003656#ifndef BACKSLASH_IN_FILENAME
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003657 xp->xp_shell = TRUE;
Bram Moolenaarc0cba4d2010-08-07 17:07:21 +02003658#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003659 /* When still after the command name expand executables. */
3660 if (xp->xp_pattern == skipwhite(arg))
Bram Moolenaar5c5b0942007-05-06 12:07:59 +00003661 xp->xp_context = EXPAND_SHELLCMD;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003662 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003663
3664 /* Check for environment variable */
3665 if (*xp->xp_pattern == '$'
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003666#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003667 || *xp->xp_pattern == '%'
3668#endif
3669 )
3670 {
3671 for (p = xp->xp_pattern + 1; *p != NUL; ++p)
3672 if (!vim_isIDc(*p))
3673 break;
3674 if (*p == NUL)
3675 {
3676 xp->xp_context = EXPAND_ENV_VARS;
3677 ++xp->xp_pattern;
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003678#if defined(FEAT_CMDL_COMPL)
Bram Moolenaar21cf8232004-07-16 20:18:37 +00003679 /* Avoid that the assignment uses EXPAND_FILES again. */
Bram Moolenaara466c992005-07-09 21:03:22 +00003680 if (compl != EXPAND_USER_DEFINED && compl != EXPAND_USER_LIST)
Bram Moolenaar21cf8232004-07-16 20:18:37 +00003681 compl = EXPAND_ENV_VARS;
3682#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003683 }
3684 }
Bram Moolenaar24305862012-08-15 14:05:05 +02003685#if defined(FEAT_CMDL_COMPL)
3686 /* Check for user names */
3687 if (*xp->xp_pattern == '~')
3688 {
3689 for (p = xp->xp_pattern + 1; *p != NUL && *p != '/'; ++p)
3690 ;
3691 /* Complete ~user only if it partially matches a user name.
3692 * A full match ~user<Tab> will be replaced by user's home
3693 * directory i.e. something like ~user<Tab> -> /home/user/ */
3694 if (*p == NUL && p > xp->xp_pattern + 1
Bram Moolenaar6c5d1042018-07-07 16:41:13 +02003695 && match_user(xp->xp_pattern + 1) >= 1)
Bram Moolenaar24305862012-08-15 14:05:05 +02003696 {
3697 xp->xp_context = EXPAND_USER;
3698 ++xp->xp_pattern;
3699 }
3700 }
3701#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003702 }
3703
3704/*
Bram Moolenaar1c40a662014-11-27 16:38:11 +01003705 * 6. Switch on command name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003706 */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003707 switch (ea.cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003708 {
Bram Moolenaarcc448b32010-07-14 16:52:17 +02003709 case CMD_find:
3710 case CMD_sfind:
3711 case CMD_tabfind:
Bram Moolenaarc24b6972010-08-16 22:34:29 +02003712 if (xp->xp_context == EXPAND_FILES)
3713 xp->xp_context = EXPAND_FILES_IN_PATH;
Bram Moolenaarcc448b32010-07-14 16:52:17 +02003714 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003715 case CMD_cd:
3716 case CMD_chdir:
Bram Moolenaar00aa0692019-04-27 20:37:57 +02003717 case CMD_tcd:
3718 case CMD_tchdir:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003719 case CMD_lcd:
3720 case CMD_lchdir:
3721 if (xp->xp_context == EXPAND_FILES)
3722 xp->xp_context = EXPAND_DIRECTORIES;
3723 break;
3724 case CMD_help:
3725 xp->xp_context = EXPAND_HELP;
3726 xp->xp_pattern = arg;
3727 break;
3728
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003729 /* Command modifiers: return the argument.
3730 * Also for commands with an argument that is a command. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003731 case CMD_aboveleft:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003732 case CMD_argdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003733 case CMD_belowright:
3734 case CMD_botright:
3735 case CMD_browse:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003736 case CMD_bufdo:
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003737 case CMD_cdo:
3738 case CMD_cfdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003739 case CMD_confirm:
Bram Moolenaardf177f62005-02-22 08:39:57 +00003740 case CMD_debug:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003741 case CMD_folddoclosed:
3742 case CMD_folddoopen:
3743 case CMD_hide:
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003744 case CMD_keepalt:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003745 case CMD_keepjumps:
3746 case CMD_keepmarks:
Bram Moolenaara939e432013-11-09 05:30:26 +01003747 case CMD_keeppatterns:
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003748 case CMD_ldo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003749 case CMD_leftabove:
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003750 case CMD_lfdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003751 case CMD_lockmarks:
Bram Moolenaar5803ae62014-03-23 16:04:02 +01003752 case CMD_noautocmd:
3753 case CMD_noswapfile:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003754 case CMD_rightbelow:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003755 case CMD_sandbox:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003756 case CMD_silent:
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003757 case CMD_tab:
Bram Moolenaar70baa402013-06-16 16:14:03 +02003758 case CMD_tabdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003759 case CMD_topleft:
3760 case CMD_verbose:
3761 case CMD_vertical:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003762 case CMD_windo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003763 return arg;
3764
Bram Moolenaar7069bf12017-01-07 20:39:53 +01003765 case CMD_filter:
3766 if (*arg != NUL)
3767 arg = skip_vimgrep_pat(arg, NULL, NULL);
3768 if (arg == NULL || *arg == NUL)
3769 {
3770 xp->xp_context = EXPAND_NOTHING;
3771 return NULL;
3772 }
3773 return skipwhite(arg);
3774
Bram Moolenaar4f688582007-07-24 12:34:30 +00003775#ifdef FEAT_CMDL_COMPL
3776# ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar071d4272004-06-13 20:20:40 +00003777 case CMD_match:
3778 if (*arg == NUL || !ends_excmd(*arg))
3779 {
Bram Moolenaar4f688582007-07-24 12:34:30 +00003780 /* also complete "None" */
3781 set_context_in_echohl_cmd(xp, arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003782 arg = skipwhite(skiptowhite(arg));
3783 if (*arg != NUL)
3784 {
3785 xp->xp_context = EXPAND_NOTHING;
3786 arg = skip_regexp(arg + 1, *arg, p_magic, NULL);
3787 }
3788 }
3789 return find_nextcmd(arg);
Bram Moolenaar4f688582007-07-24 12:34:30 +00003790# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003791
Bram Moolenaar071d4272004-06-13 20:20:40 +00003792/*
3793 * All completion for the +cmdline_compl feature goes here.
3794 */
3795
Bram Moolenaar071d4272004-06-13 20:20:40 +00003796 case CMD_command:
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003797 return set_context_in_user_cmd(xp, arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003798
3799 case CMD_delcommand:
3800 xp->xp_context = EXPAND_USER_COMMANDS;
3801 xp->xp_pattern = arg;
3802 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003803
3804 case CMD_global:
3805 case CMD_vglobal:
3806 delim = *arg; /* get the delimiter */
3807 if (delim)
3808 ++arg; /* skip delimiter if there is one */
3809
3810 while (arg[0] != NUL && arg[0] != delim)
3811 {
3812 if (arg[0] == '\\' && arg[1] != NUL)
3813 ++arg;
3814 ++arg;
3815 }
3816 if (arg[0] != NUL)
3817 return arg + 1;
3818 break;
3819 case CMD_and:
3820 case CMD_substitute:
3821 delim = *arg;
3822 if (delim)
3823 {
3824 /* skip "from" part */
3825 ++arg;
3826 arg = skip_regexp(arg, delim, p_magic, NULL);
3827 }
3828 /* skip "to" part */
3829 while (arg[0] != NUL && arg[0] != delim)
3830 {
3831 if (arg[0] == '\\' && arg[1] != NUL)
3832 ++arg;
3833 ++arg;
3834 }
3835 if (arg[0] != NUL) /* skip delimiter */
3836 ++arg;
3837 while (arg[0] && vim_strchr((char_u *)"|\"#", arg[0]) == NULL)
3838 ++arg;
3839 if (arg[0] != NUL)
3840 return arg;
3841 break;
3842 case CMD_isearch:
3843 case CMD_dsearch:
3844 case CMD_ilist:
3845 case CMD_dlist:
3846 case CMD_ijump:
3847 case CMD_psearch:
3848 case CMD_djump:
3849 case CMD_isplit:
3850 case CMD_dsplit:
3851 arg = skipwhite(skipdigits(arg)); /* skip count */
3852 if (*arg == '/') /* Match regexp, not just whole words */
3853 {
3854 for (++arg; *arg && *arg != '/'; arg++)
3855 if (*arg == '\\' && arg[1] != NUL)
3856 arg++;
3857 if (*arg)
3858 {
3859 arg = skipwhite(arg + 1);
3860
3861 /* Check for trailing illegal characters */
3862 if (*arg && vim_strchr((char_u *)"|\"\n", *arg) == NULL)
3863 xp->xp_context = EXPAND_NOTHING;
3864 else
3865 return arg;
3866 }
3867 }
3868 break;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003869
Bram Moolenaar071d4272004-06-13 20:20:40 +00003870 case CMD_autocmd:
3871 return set_context_in_autocmd(xp, arg, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003872 case CMD_doautocmd:
Bram Moolenaar73a9d7b2008-11-06 16:16:44 +00003873 case CMD_doautoall:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003874 return set_context_in_autocmd(xp, arg, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003875 case CMD_set:
3876 set_context_in_set_cmd(xp, arg, 0);
3877 break;
3878 case CMD_setglobal:
3879 set_context_in_set_cmd(xp, arg, OPT_GLOBAL);
3880 break;
3881 case CMD_setlocal:
3882 set_context_in_set_cmd(xp, arg, OPT_LOCAL);
3883 break;
3884 case CMD_tag:
3885 case CMD_stag:
3886 case CMD_ptag:
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00003887 case CMD_ltag:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003888 case CMD_tselect:
3889 case CMD_stselect:
3890 case CMD_ptselect:
3891 case CMD_tjump:
3892 case CMD_stjump:
3893 case CMD_ptjump:
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00003894 if (*p_wop != NUL)
3895 xp->xp_context = EXPAND_TAGS_LISTFILES;
3896 else
3897 xp->xp_context = EXPAND_TAGS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003898 xp->xp_pattern = arg;
3899 break;
3900 case CMD_augroup:
3901 xp->xp_context = EXPAND_AUGROUP;
3902 xp->xp_pattern = arg;
3903 break;
3904#ifdef FEAT_SYN_HL
3905 case CMD_syntax:
3906 set_context_in_syntax_cmd(xp, arg);
3907 break;
3908#endif
3909#ifdef FEAT_EVAL
3910 case CMD_let:
3911 case CMD_if:
3912 case CMD_elseif:
3913 case CMD_while:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00003914 case CMD_for:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003915 case CMD_echo:
3916 case CMD_echon:
3917 case CMD_execute:
3918 case CMD_echomsg:
3919 case CMD_echoerr:
3920 case CMD_call:
3921 case CMD_return:
Bram Moolenaar2b2207b2017-01-22 16:46:56 +01003922 case CMD_cexpr:
3923 case CMD_caddexpr:
3924 case CMD_cgetexpr:
3925 case CMD_lexpr:
3926 case CMD_laddexpr:
3927 case CMD_lgetexpr:
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003928 set_context_for_expression(xp, arg, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003929 break;
3930
3931 case CMD_unlet:
3932 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
3933 arg = xp->xp_pattern + 1;
Bram Moolenaar19834012018-06-12 17:03:39 +02003934
Bram Moolenaar071d4272004-06-13 20:20:40 +00003935 xp->xp_context = EXPAND_USER_VARS;
3936 xp->xp_pattern = arg;
Bram Moolenaar19834012018-06-12 17:03:39 +02003937
3938 if (*xp->xp_pattern == '$')
3939 {
3940 xp->xp_context = EXPAND_ENV_VARS;
3941 ++xp->xp_pattern;
3942 }
3943
Bram Moolenaar071d4272004-06-13 20:20:40 +00003944 break;
3945
3946 case CMD_function:
3947 case CMD_delfunction:
3948 xp->xp_context = EXPAND_USER_FUNC;
3949 xp->xp_pattern = arg;
3950 break;
3951
3952 case CMD_echohl:
Bram Moolenaar4f688582007-07-24 12:34:30 +00003953 set_context_in_echohl_cmd(xp, arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003954 break;
3955#endif
3956 case CMD_highlight:
3957 set_context_in_highlight_cmd(xp, arg);
3958 break;
Bram Moolenaarf4580d82009-03-18 11:52:53 +00003959#ifdef FEAT_CSCOPE
3960 case CMD_cscope:
Bram Moolenaar7bfef802009-04-22 14:25:01 +00003961 case CMD_lcscope:
3962 case CMD_scscope:
3963 set_context_in_cscope_cmd(xp, arg, ea.cmdidx);
Bram Moolenaarf4580d82009-03-18 11:52:53 +00003964 break;
3965#endif
Bram Moolenaar3c65e312009-04-29 16:47:23 +00003966#ifdef FEAT_SIGNS
3967 case CMD_sign:
3968 set_context_in_sign_cmd(xp, arg);
3969 break;
3970#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003971 case CMD_bdelete:
3972 case CMD_bwipeout:
3973 case CMD_bunload:
3974 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
3975 arg = xp->xp_pattern + 1;
Bram Moolenaar2f40d122017-10-24 21:49:36 +02003976 /* FALLTHROUGH */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003977 case CMD_buffer:
3978 case CMD_sbuffer:
3979 case CMD_checktime:
3980 xp->xp_context = EXPAND_BUFFERS;
3981 xp->xp_pattern = arg;
3982 break;
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003983
Bram Moolenaar071d4272004-06-13 20:20:40 +00003984 case CMD_USER:
3985 case CMD_USER_BUF:
3986 if (compl != EXPAND_NOTHING)
3987 {
Bram Moolenaar8071cb22019-07-12 17:58:01 +02003988 // EX_XFILE: file names are handled above
3989 if (!(ea.argt & EX_XFILE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003990 {
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003991#ifdef FEAT_MENU
Bram Moolenaar071d4272004-06-13 20:20:40 +00003992 if (compl == EXPAND_MENUS)
3993 return set_context_in_menu_cmd(xp, cmd, arg, forceit);
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003994#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003995 if (compl == EXPAND_COMMANDS)
3996 return arg;
3997 if (compl == EXPAND_MAPPINGS)
3998 return set_context_in_map_cmd(xp, (char_u *)"map",
3999 arg, forceit, FALSE, FALSE, CMD_map);
Bram Moolenaarac9fb182019-04-27 13:04:13 +02004000 // Find start of last argument.
Bram Moolenaar848f8762012-07-25 17:22:23 +02004001 p = arg;
4002 while (*p)
4003 {
4004 if (*p == ' ')
Bram Moolenaarac9fb182019-04-27 13:04:13 +02004005 // argument starts after a space
Bram Moolenaar848f8762012-07-25 17:22:23 +02004006 arg = p + 1;
Bram Moolenaara07c8312012-07-27 21:12:07 +02004007 else if (*p == '\\' && *(p + 1) != NUL)
Bram Moolenaarac9fb182019-04-27 13:04:13 +02004008 ++p; // skip over escaped character
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004009 MB_PTR_ADV(p);
Bram Moolenaar848f8762012-07-25 17:22:23 +02004010 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004011 xp->xp_pattern = arg;
4012 }
4013 xp->xp_context = compl;
4014 }
4015 break;
Bram Moolenaarac9fb182019-04-27 13:04:13 +02004016
Bram Moolenaar071d4272004-06-13 20:20:40 +00004017 case CMD_map: case CMD_noremap:
4018 case CMD_nmap: case CMD_nnoremap:
4019 case CMD_vmap: case CMD_vnoremap:
4020 case CMD_omap: case CMD_onoremap:
4021 case CMD_imap: case CMD_inoremap:
4022 case CMD_cmap: case CMD_cnoremap:
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004023 case CMD_lmap: case CMD_lnoremap:
Bram Moolenaar09bb33d2013-05-07 05:18:20 +02004024 case CMD_smap: case CMD_snoremap:
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +02004025 case CMD_tmap: case CMD_tnoremap:
Bram Moolenaar09bb33d2013-05-07 05:18:20 +02004026 case CMD_xmap: case CMD_xnoremap:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004027 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004028 FALSE, FALSE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004029 case CMD_unmap:
4030 case CMD_nunmap:
4031 case CMD_vunmap:
4032 case CMD_ounmap:
4033 case CMD_iunmap:
4034 case CMD_cunmap:
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004035 case CMD_lunmap:
Bram Moolenaar09bb33d2013-05-07 05:18:20 +02004036 case CMD_sunmap:
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +02004037 case CMD_tunmap:
Bram Moolenaar09bb33d2013-05-07 05:18:20 +02004038 case CMD_xunmap:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004039 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004040 FALSE, TRUE, ea.cmdidx);
Bram Moolenaarcae92dc2017-08-06 15:22:15 +02004041 case CMD_mapclear:
4042 case CMD_nmapclear:
4043 case CMD_vmapclear:
4044 case CMD_omapclear:
4045 case CMD_imapclear:
4046 case CMD_cmapclear:
4047 case CMD_lmapclear:
4048 case CMD_smapclear:
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +02004049 case CMD_tmapclear:
Bram Moolenaarcae92dc2017-08-06 15:22:15 +02004050 case CMD_xmapclear:
4051 xp->xp_context = EXPAND_MAPCLEAR;
4052 xp->xp_pattern = arg;
4053 break;
4054
Bram Moolenaar071d4272004-06-13 20:20:40 +00004055 case CMD_abbreviate: case CMD_noreabbrev:
4056 case CMD_cabbrev: case CMD_cnoreabbrev:
4057 case CMD_iabbrev: case CMD_inoreabbrev:
4058 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004059 TRUE, FALSE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004060 case CMD_unabbreviate:
4061 case CMD_cunabbrev:
4062 case CMD_iunabbrev:
4063 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004064 TRUE, TRUE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004065#ifdef FEAT_MENU
4066 case CMD_menu: case CMD_noremenu: case CMD_unmenu:
4067 case CMD_amenu: case CMD_anoremenu: case CMD_aunmenu:
4068 case CMD_nmenu: case CMD_nnoremenu: case CMD_nunmenu:
4069 case CMD_vmenu: case CMD_vnoremenu: case CMD_vunmenu:
4070 case CMD_omenu: case CMD_onoremenu: case CMD_ounmenu:
4071 case CMD_imenu: case CMD_inoremenu: case CMD_iunmenu:
4072 case CMD_cmenu: case CMD_cnoremenu: case CMD_cunmenu:
Bram Moolenaar4c5d8152018-10-19 22:36:53 +02004073 case CMD_tlmenu: case CMD_tlnoremenu: case CMD_tlunmenu:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004074 case CMD_tmenu: case CMD_tunmenu:
4075 case CMD_popup: case CMD_tearoff: case CMD_emenu:
4076 return set_context_in_menu_cmd(xp, cmd, arg, forceit);
4077#endif
4078
4079 case CMD_colorscheme:
4080 xp->xp_context = EXPAND_COLORS;
4081 xp->xp_pattern = arg;
4082 break;
4083
4084 case CMD_compiler:
4085 xp->xp_context = EXPAND_COMPILER;
4086 xp->xp_pattern = arg;
4087 break;
4088
Bram Moolenaar883f5d02010-06-21 06:24:34 +02004089 case CMD_ownsyntax:
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02004090 xp->xp_context = EXPAND_OWNSYNTAX;
4091 xp->xp_pattern = arg;
4092 break;
4093
4094 case CMD_setfiletype:
Bram Moolenaar883f5d02010-06-21 06:24:34 +02004095 xp->xp_context = EXPAND_FILETYPE;
4096 xp->xp_pattern = arg;
4097 break;
4098
Bram Moolenaar35ca0e72016-03-05 17:41:49 +01004099 case CMD_packadd:
4100 xp->xp_context = EXPAND_PACKADD;
4101 xp->xp_pattern = arg;
4102 break;
4103
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01004104#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004105 case CMD_language:
Bram Moolenaara660dc82011-05-25 12:51:22 +02004106 p = skiptowhite(arg);
4107 if (*p == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004108 {
4109 xp->xp_context = EXPAND_LANGUAGE;
4110 xp->xp_pattern = arg;
4111 }
4112 else
Bram Moolenaara660dc82011-05-25 12:51:22 +02004113 {
4114 if ( STRNCMP(arg, "messages", p - arg) == 0
4115 || STRNCMP(arg, "ctype", p - arg) == 0
4116 || STRNCMP(arg, "time", p - arg) == 0)
4117 {
4118 xp->xp_context = EXPAND_LOCALES;
4119 xp->xp_pattern = skipwhite(p);
4120 }
4121 else
4122 xp->xp_context = EXPAND_NOTHING;
4123 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004124 break;
4125#endif
Bram Moolenaarf86f26c2010-02-03 15:14:22 +01004126#if defined(FEAT_PROFILE)
4127 case CMD_profile:
4128 set_context_in_profile_cmd(xp, arg);
4129 break;
4130#endif
Bram Moolenaar42b4dda2010-03-02 15:56:05 +01004131 case CMD_behave:
4132 xp->xp_context = EXPAND_BEHAVE;
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02004133 xp->xp_pattern = arg;
Bram Moolenaar42b4dda2010-03-02 15:56:05 +01004134 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004135
Bram Moolenaar9e507ca2016-10-15 15:39:39 +02004136 case CMD_messages:
4137 xp->xp_context = EXPAND_MESSAGES;
4138 xp->xp_pattern = arg;
4139 break;
4140
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02004141#if defined(FEAT_CMDHIST)
4142 case CMD_history:
4143 xp->xp_context = EXPAND_HISTORY;
4144 xp->xp_pattern = arg;
4145 break;
4146#endif
Bram Moolenaarcd9c4622013-06-08 15:24:48 +02004147#if defined(FEAT_PROFILE)
4148 case CMD_syntime:
4149 xp->xp_context = EXPAND_SYNTIME;
4150 xp->xp_pattern = arg;
4151 break;
4152#endif
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02004153
Bram Moolenaarcd43eff2018-03-29 15:55:38 +02004154 case CMD_argdelete:
4155 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
4156 arg = xp->xp_pattern + 1;
4157 xp->xp_context = EXPAND_ARGLIST;
4158 xp->xp_pattern = arg;
4159 break;
4160
Bram Moolenaar071d4272004-06-13 20:20:40 +00004161#endif /* FEAT_CMDL_COMPL */
4162
4163 default:
4164 break;
4165 }
4166 return NULL;
4167}
4168
4169/*
Bram Moolenaaree8415b2018-08-10 23:13:12 +02004170 * Skip a range specifier of the form: addr [,addr] [;addr] ..
Bram Moolenaar071d4272004-06-13 20:20:40 +00004171 *
4172 * Backslashed delimiters after / or ? will be skipped, and commands will
4173 * not be expanded between /'s and ?'s or after "'".
4174 *
Bram Moolenaardf177f62005-02-22 08:39:57 +00004175 * Also skip white space and ":" characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004176 * Returns the "cmd" pointer advanced to beyond the range.
4177 */
4178 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004179skip_range(
4180 char_u *cmd,
4181 int *ctx) /* pointer to xp_context or NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004182{
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004183 unsigned delim;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004184
Bram Moolenaarcbf20fb2017-02-03 21:19:04 +01004185 while (vim_strchr((char_u *)" \t0123456789.$%'/?-+,;\\", *cmd) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004186 {
Bram Moolenaarcbf20fb2017-02-03 21:19:04 +01004187 if (*cmd == '\\')
4188 {
4189 if (cmd[1] == '?' || cmd[1] == '/' || cmd[1] == '&')
4190 ++cmd;
4191 else
4192 break;
4193 }
4194 else if (*cmd == '\'')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004195 {
4196 if (*++cmd == NUL && ctx != NULL)
4197 *ctx = EXPAND_NOTHING;
4198 }
4199 else if (*cmd == '/' || *cmd == '?')
4200 {
4201 delim = *cmd++;
4202 while (*cmd != NUL && *cmd != delim)
4203 if (*cmd++ == '\\' && *cmd != NUL)
4204 ++cmd;
4205 if (*cmd == NUL && ctx != NULL)
4206 *ctx = EXPAND_NOTHING;
4207 }
4208 if (*cmd != NUL)
4209 ++cmd;
4210 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00004211
4212 /* Skip ":" and white space. */
4213 while (*cmd == ':')
4214 cmd = skipwhite(cmd + 1);
4215
Bram Moolenaar071d4272004-06-13 20:20:40 +00004216 return cmd;
4217}
4218
4219/*
Bram Moolenaar198cb662018-09-06 21:44:17 +02004220 * Get a single EX address.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004221 *
4222 * Set ptr to the next character after the part that was interpreted.
4223 * Set ptr to NULL when an error is encountered.
Bram Moolenaar198cb662018-09-06 21:44:17 +02004224 * This may set the last used search pattern.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004225 *
4226 * Return MAXLNUM when no Ex address was found.
4227 */
4228 static linenr_T
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004229get_address(
4230 exarg_T *eap UNUSED,
4231 char_u **ptr,
Bram Moolenaarb7316892019-05-01 18:08:42 +02004232 cmd_addr_T addr_type_arg,
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02004233 int skip, // only skip the address, don't use it
4234 int silent, // no errors or side effects
4235 int to_other_file, // flag: may jump to other file
4236 int address_count UNUSED) // 1 for first address, >1 after comma
Bram Moolenaar071d4272004-06-13 20:20:40 +00004237{
Bram Moolenaarb7316892019-05-01 18:08:42 +02004238 cmd_addr_T addr_type = addr_type_arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004239 int c;
4240 int i;
4241 long n;
4242 char_u *cmd;
4243 pos_T pos;
4244 pos_T *fp;
4245 linenr_T lnum;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004246 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004247
4248 cmd = skipwhite(*ptr);
4249 lnum = MAXLNUM;
4250 do
4251 {
4252 switch (*cmd)
4253 {
4254 case '.': /* '.' - Cursor position */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004255 ++cmd;
4256 switch (addr_type)
4257 {
4258 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004259 case ADDR_OTHER:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004260 lnum = curwin->w_cursor.lnum;
4261 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004262 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004263 lnum = CURRENT_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004264 break;
4265 case ADDR_ARGUMENTS:
4266 lnum = curwin->w_arg_idx + 1;
4267 break;
4268 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004269 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004270 lnum = curbuf->b_fnum;
4271 break;
4272 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004273 lnum = CURRENT_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004274 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004275 case ADDR_TABS_RELATIVE:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004276 case ADDR_NONE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02004277 case ADDR_UNSIGNED:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004278 emsg(_(e_invrange));
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004279 cmd = NULL;
4280 goto error;
4281 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004282 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004283#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02004284 lnum = qf_get_cur_idx(eap);
4285#endif
4286 break;
4287 case ADDR_QUICKFIX_VALID:
4288#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004289 lnum = qf_get_cur_valid_idx(eap);
Bram Moolenaare906c502015-09-09 21:10:39 +02004290#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004291 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004292 }
4293 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004294
4295 case '$': /* '$' - last line */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004296 ++cmd;
4297 switch (addr_type)
4298 {
4299 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004300 case ADDR_OTHER:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004301 lnum = curbuf->b_ml.ml_line_count;
4302 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004303 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004304 lnum = LAST_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004305 break;
4306 case ADDR_ARGUMENTS:
4307 lnum = ARGCOUNT;
4308 break;
4309 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004310 buf = lastbuf;
4311 while (buf->b_ml.ml_mfp == NULL)
4312 {
4313 if (buf->b_prev == NULL)
4314 break;
4315 buf = buf->b_prev;
4316 }
4317 lnum = buf->b_fnum;
4318 break;
4319 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004320 lnum = lastbuf->b_fnum;
4321 break;
4322 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004323 lnum = LAST_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004324 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004325 case ADDR_TABS_RELATIVE:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004326 case ADDR_NONE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02004327 case ADDR_UNSIGNED:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004328 emsg(_(e_invrange));
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004329 cmd = NULL;
4330 goto error;
4331 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004332 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004333#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004334 lnum = qf_get_size(eap);
4335 if (lnum == 0)
4336 lnum = 1;
Bram Moolenaare906c502015-09-09 21:10:39 +02004337#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004338 break;
Bram Moolenaar25190db2019-05-04 15:05:28 +02004339 case ADDR_QUICKFIX_VALID:
4340#ifdef FEAT_QUICKFIX
4341 lnum = qf_get_valid_size(eap);
4342 if (lnum == 0)
4343 lnum = 1;
4344#endif
4345 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004346 }
4347 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004348
4349 case '\'': /* ''' - mark */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004350 if (*++cmd == NUL)
4351 {
4352 cmd = NULL;
4353 goto error;
4354 }
4355 if (addr_type != ADDR_LINES)
4356 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004357 emsg(_(e_invaddr));
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004358 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004359 goto error;
4360 }
4361 if (skip)
4362 ++cmd;
4363 else
4364 {
4365 /* Only accept a mark in another file when it is
4366 * used by itself: ":'M". */
4367 fp = getmark(*cmd, to_other_file && cmd[1] == NUL);
4368 ++cmd;
4369 if (fp == (pos_T *)-1)
4370 /* Jumped to another file. */
4371 lnum = curwin->w_cursor.lnum;
4372 else
4373 {
4374 if (check_mark(fp) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004375 {
4376 cmd = NULL;
4377 goto error;
4378 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004379 lnum = fp->lnum;
4380 }
4381 }
4382 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004383
4384 case '/':
4385 case '?': /* '/' or '?' - search */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004386 c = *cmd++;
4387 if (addr_type != ADDR_LINES)
4388 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004389 emsg(_(e_invaddr));
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004390 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004391 goto error;
4392 }
4393 if (skip) /* skip "/pat/" */
4394 {
4395 cmd = skip_regexp(cmd, c, (int)p_magic, NULL);
4396 if (*cmd == c)
4397 ++cmd;
4398 }
4399 else
4400 {
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02004401 int flags;
4402
4403 pos = curwin->w_cursor; // save curwin->w_cursor
4404
4405 // When '/' or '?' follows another address, start from
4406 // there.
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004407 if (lnum != MAXLNUM)
4408 curwin->w_cursor.lnum = lnum;
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02004409
4410 // Start a forward search at the end of the line (unless
4411 // before the first line).
4412 // Start a backward search at the start of the line.
4413 // This makes sure we never match in the current
4414 // line, and can match anywhere in the
4415 // next/previous line.
Bram Moolenaar8ada6aa2017-12-19 21:23:21 +01004416 if (c == '/' && curwin->w_cursor.lnum > 0)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004417 curwin->w_cursor.col = MAXCOL;
4418 else
4419 curwin->w_cursor.col = 0;
4420 searchcmdlen = 0;
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02004421 flags = silent ? 0 : SEARCH_HIS | SEARCH_MSG;
4422 if (!do_search(NULL, c, cmd, 1L, flags, NULL, NULL))
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004423 {
4424 curwin->w_cursor = pos;
4425 cmd = NULL;
4426 goto error;
4427 }
4428 lnum = curwin->w_cursor.lnum;
4429 curwin->w_cursor = pos;
4430 /* adjust command string pointer */
4431 cmd += searchcmdlen;
4432 }
4433 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004434
4435 case '\\': /* "\?", "\/" or "\&", repeat search */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004436 ++cmd;
4437 if (addr_type != ADDR_LINES)
4438 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004439 emsg(_(e_invaddr));
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004440 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004441 goto error;
4442 }
4443 if (*cmd == '&')
4444 i = RE_SUBST;
4445 else if (*cmd == '?' || *cmd == '/')
4446 i = RE_SEARCH;
4447 else
4448 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004449 emsg(_(e_backslash));
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004450 cmd = NULL;
4451 goto error;
4452 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004453
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004454 if (!skip)
4455 {
4456 /*
4457 * When search follows another address, start from
4458 * there.
4459 */
4460 if (lnum != MAXLNUM)
4461 pos.lnum = lnum;
4462 else
4463 pos.lnum = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004464
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004465 /*
4466 * Start the search just like for the above
4467 * do_search().
4468 */
4469 if (*cmd != '?')
4470 pos.col = MAXCOL;
4471 else
4472 pos.col = 0;
Bram Moolenaarbd8539a2015-08-11 18:53:03 +02004473 pos.coladd = 0;
Bram Moolenaar5d24a222018-12-23 19:10:09 +01004474 if (searchit(curwin, curbuf, &pos, NULL,
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004475 *cmd == '?' ? BACKWARD : FORWARD,
4476 (char_u *)"", 1L, SEARCH_MSG,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02004477 i, (linenr_T)0, NULL, NULL) != FAIL)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004478 lnum = pos.lnum;
4479 else
4480 {
4481 cmd = NULL;
4482 goto error;
4483 }
4484 }
4485 ++cmd;
4486 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004487
4488 default:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004489 if (VIM_ISDIGIT(*cmd)) /* absolute line number */
4490 lnum = getdigits(&cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004491 }
4492
4493 for (;;)
4494 {
4495 cmd = skipwhite(cmd);
4496 if (*cmd != '-' && *cmd != '+' && !VIM_ISDIGIT(*cmd))
4497 break;
4498
4499 if (lnum == MAXLNUM)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004500 {
4501 switch (addr_type)
4502 {
4503 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004504 case ADDR_OTHER:
4505 // "+1" is same as ".+1"
Bram Moolenaarf240e182014-11-27 18:33:02 +01004506 lnum = curwin->w_cursor.lnum;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004507 break;
4508 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004509 lnum = CURRENT_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004510 break;
4511 case ADDR_ARGUMENTS:
4512 lnum = curwin->w_arg_idx + 1;
4513 break;
4514 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004515 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004516 lnum = curbuf->b_fnum;
4517 break;
4518 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004519 lnum = CURRENT_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004520 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004521 case ADDR_TABS_RELATIVE:
4522 lnum = 1;
4523 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004524 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004525#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02004526 lnum = qf_get_cur_idx(eap);
4527#endif
4528 break;
4529 case ADDR_QUICKFIX_VALID:
4530#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004531 lnum = qf_get_cur_valid_idx(eap);
Bram Moolenaare906c502015-09-09 21:10:39 +02004532#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004533 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02004534 case ADDR_NONE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02004535 case ADDR_UNSIGNED:
4536 lnum = 0;
Bram Moolenaarb7316892019-05-01 18:08:42 +02004537 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004538 }
4539 }
4540
Bram Moolenaar071d4272004-06-13 20:20:40 +00004541 if (VIM_ISDIGIT(*cmd))
4542 i = '+'; /* "number" is same as "+number" */
4543 else
4544 i = *cmd++;
4545 if (!VIM_ISDIGIT(*cmd)) /* '+' is '+1', but '+0' is not '+1' */
4546 n = 1;
4547 else
4548 n = getdigits(&cmd);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004549
4550 if (addr_type == ADDR_TABS_RELATIVE)
4551 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004552 emsg(_(e_invrange));
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004553 cmd = NULL;
4554 goto error;
4555 }
4556 else if (addr_type == ADDR_LOADED_BUFFERS
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004557 || addr_type == ADDR_BUFFERS)
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004558 lnum = compute_buffer_local_count(
4559 addr_type, lnum, (i == '-') ? -1 * n : n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004560 else
Bram Moolenaarded27822017-01-02 14:27:34 +01004561 {
4562#ifdef FEAT_FOLDING
4563 /* Relative line addressing, need to adjust for folded lines
4564 * now, but only do it after the first address. */
4565 if (addr_type == ADDR_LINES && (i == '-' || i == '+')
4566 && address_count >= 2)
4567 (void)hasFolding(lnum, NULL, &lnum);
4568#endif
4569 if (i == '-')
4570 lnum -= n;
4571 else
4572 lnum += n;
4573 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004574 }
4575 } while (*cmd == '/' || *cmd == '?');
4576
4577error:
4578 *ptr = cmd;
4579 return lnum;
4580}
4581
4582/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00004583 * Get flags from an Ex command argument.
4584 */
4585 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004586get_flags(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00004587{
4588 while (vim_strchr((char_u *)"lp#", *eap->arg) != NULL)
4589 {
4590 if (*eap->arg == 'l')
4591 eap->flags |= EXFLAG_LIST;
4592 else if (*eap->arg == 'p')
4593 eap->flags |= EXFLAG_PRINT;
4594 else
4595 eap->flags |= EXFLAG_NR;
4596 eap->arg = skipwhite(eap->arg + 1);
4597 }
4598}
4599
4600/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004601 * Function called for command which is Not Implemented. NI!
4602 */
4603 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004604ex_ni(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004605{
4606 if (!eap->skip)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004607 eap->errmsg = N_("E319: Sorry, the command is not available in this version");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004608}
4609
Bram Moolenaar7bb75552007-07-16 18:39:49 +00004610#ifdef HAVE_EX_SCRIPT_NI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004611/*
4612 * Function called for script command which is Not Implemented. NI!
4613 * Skips over ":perl <<EOF" constructs.
4614 */
4615 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004616ex_script_ni(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004617{
4618 if (!eap->skip)
4619 ex_ni(eap);
4620 else
4621 vim_free(script_get(eap, eap->arg));
4622}
4623#endif
4624
4625/*
4626 * Check range in Ex command for validity.
4627 * Return NULL when valid, error message when invalid.
4628 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004629 static char *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004630invalid_range(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004631{
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004632 buf_T *buf;
Bram Moolenaar25190db2019-05-04 15:05:28 +02004633
Bram Moolenaar071d4272004-06-13 20:20:40 +00004634 if ( eap->line1 < 0
4635 || eap->line2 < 0
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004636 || eap->line1 > eap->line2)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004637 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004638
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004639 if (eap->argt & EX_RANGE)
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004640 {
Bram Moolenaarb7316892019-05-01 18:08:42 +02004641 switch (eap->addr_type)
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004642 {
4643 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004644 if (eap->line2 > curbuf->b_ml.ml_line_count
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004645#ifdef FEAT_DIFF
4646 + (eap->cmdidx == CMD_diffget)
4647#endif
4648 )
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004649 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004650 break;
4651 case ADDR_ARGUMENTS:
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004652 /* add 1 if ARGCOUNT is 0 */
4653 if (eap->line2 > ARGCOUNT + (!ARGCOUNT))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004654 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004655 break;
4656 case ADDR_BUFFERS:
4657 if (eap->line1 < firstbuf->b_fnum
4658 || eap->line2 > lastbuf->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_LOADED_BUFFERS:
4662 buf = firstbuf;
4663 while (buf->b_ml.ml_mfp == NULL)
4664 {
4665 if (buf->b_next == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004666 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004667 buf = buf->b_next;
4668 }
4669 if (eap->line1 < buf->b_fnum)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004670 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004671 buf = lastbuf;
4672 while (buf->b_ml.ml_mfp == NULL)
4673 {
4674 if (buf->b_prev == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004675 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004676 buf = buf->b_prev;
4677 }
4678 if (eap->line2 > buf->b_fnum)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004679 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004680 break;
4681 case ADDR_WINDOWS:
Bram Moolenaar8be63882015-01-14 11:25:05 +01004682 if (eap->line2 > LAST_WIN_NR)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004683 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004684 break;
4685 case ADDR_TABS:
4686 if (eap->line2 > LAST_TAB_NR)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004687 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004688 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004689 case ADDR_TABS_RELATIVE:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004690 case ADDR_OTHER:
4691 // Any range is OK.
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004692 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004693 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004694#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02004695 // No error for value that is too big, will use the last entry.
4696 if (eap->line2 <= 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004697 return _(e_invrange);
Bram Moolenaare906c502015-09-09 21:10:39 +02004698#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004699 break;
Bram Moolenaar25190db2019-05-04 15:05:28 +02004700 case ADDR_QUICKFIX_VALID:
4701#ifdef FEAT_QUICKFIX
4702 if ((eap->line2 != 1 && eap->line2 > qf_get_valid_size(eap))
4703 || eap->line2 < 0)
4704 return _(e_invrange);
4705#endif
4706 break;
4707 case ADDR_UNSIGNED:
4708 if (eap->line2 < 0)
4709 return _(e_invrange);
4710 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02004711 case ADDR_NONE:
4712 // Will give an error elsewhere.
4713 break;
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004714 }
4715 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004716 return NULL;
4717}
4718
4719/*
4720 * Correct the range for zero line number, if required.
4721 */
4722 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004723correct_range(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004724{
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004725 if (!(eap->argt & EX_ZEROR)) // zero in range not allowed
Bram Moolenaar071d4272004-06-13 20:20:40 +00004726 {
4727 if (eap->line1 == 0)
4728 eap->line1 = 1;
4729 if (eap->line2 == 0)
4730 eap->line2 = 1;
4731 }
4732}
4733
Bram Moolenaar748bf032005-02-02 23:04:36 +00004734#ifdef FEAT_QUICKFIX
Bram Moolenaar748bf032005-02-02 23:04:36 +00004735/*
4736 * For a ":vimgrep" or ":vimgrepadd" command return a pointer past the
4737 * pattern. Otherwise return eap->arg.
4738 */
4739 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004740skip_grep_pat(exarg_T *eap)
Bram Moolenaar748bf032005-02-02 23:04:36 +00004741{
4742 char_u *p = eap->arg;
4743
Bram Moolenaara37420f2006-02-04 22:37:47 +00004744 if (*p != NUL && (eap->cmdidx == CMD_vimgrep || eap->cmdidx == CMD_lvimgrep
4745 || eap->cmdidx == CMD_vimgrepadd
4746 || eap->cmdidx == CMD_lvimgrepadd
4747 || grep_internal(eap->cmdidx)))
Bram Moolenaar748bf032005-02-02 23:04:36 +00004748 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00004749 p = skip_vimgrep_pat(p, NULL, NULL);
Bram Moolenaar748bf032005-02-02 23:04:36 +00004750 if (p == NULL)
4751 p = eap->arg;
Bram Moolenaar748bf032005-02-02 23:04:36 +00004752 }
4753 return p;
4754}
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004755
4756/*
4757 * For the ":make" and ":grep" commands insert the 'makeprg'/'grepprg' option
4758 * in the command line, so that things like % get expanded.
4759 */
4760 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004761replace_makeprg(exarg_T *eap, char_u *p, char_u **cmdlinep)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004762{
4763 char_u *new_cmdline;
4764 char_u *program;
4765 char_u *pos;
4766 char_u *ptr;
4767 int len;
4768 int i;
4769
4770 /*
4771 * Don't do it when ":vimgrep" is used for ":grep".
4772 */
Bram Moolenaara37420f2006-02-04 22:37:47 +00004773 if ((eap->cmdidx == CMD_make || eap->cmdidx == CMD_lmake
4774 || eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4775 || eap->cmdidx == CMD_grepadd
4776 || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004777 && !grep_internal(eap->cmdidx))
4778 {
Bram Moolenaara37420f2006-02-04 22:37:47 +00004779 if (eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4780 || eap->cmdidx == CMD_grepadd || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004781 {
4782 if (*curbuf->b_p_gp == NUL)
4783 program = p_gp;
4784 else
4785 program = curbuf->b_p_gp;
4786 }
4787 else
4788 {
4789 if (*curbuf->b_p_mp == NUL)
4790 program = p_mp;
4791 else
4792 program = curbuf->b_p_mp;
4793 }
4794
4795 p = skipwhite(p);
4796
4797 if ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4798 {
4799 /* replace $* by given arguments */
4800 i = 1;
4801 while ((pos = (char_u *)strstr((char *)pos + 2, "$*")) != NULL)
4802 ++i;
4803 len = (int)STRLEN(p);
Bram Moolenaar51e14382019-05-25 20:21:28 +02004804 new_cmdline = alloc(STRLEN(program) + i * (len - 2) + 1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004805 if (new_cmdline == NULL)
4806 return NULL; /* out of memory */
4807 ptr = new_cmdline;
4808 while ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4809 {
4810 i = (int)(pos - program);
4811 STRNCPY(ptr, program, i);
4812 STRCPY(ptr += i, p);
4813 ptr += len;
4814 program = pos + 2;
4815 }
4816 STRCPY(ptr, program);
4817 }
4818 else
4819 {
Bram Moolenaar51e14382019-05-25 20:21:28 +02004820 new_cmdline = alloc(STRLEN(program) + STRLEN(p) + 2);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004821 if (new_cmdline == NULL)
4822 return NULL; /* out of memory */
4823 STRCPY(new_cmdline, program);
4824 STRCAT(new_cmdline, " ");
4825 STRCAT(new_cmdline, p);
4826 }
4827 msg_make(p);
4828
4829 /* 'eap->cmd' is not set here, because it is not used at CMD_make */
4830 vim_free(*cmdlinep);
4831 *cmdlinep = new_cmdline;
4832 p = new_cmdline;
4833 }
4834 return p;
4835}
Bram Moolenaar748bf032005-02-02 23:04:36 +00004836#endif
4837
Bram Moolenaar071d4272004-06-13 20:20:40 +00004838/*
4839 * Expand file name in Ex command argument.
Bram Moolenaar0abb4272019-06-15 16:06:00 +02004840 * When an error is detected, "errormsgp" is set to a non-NULL pointer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004841 * Return FAIL for failure, OK otherwise.
4842 */
4843 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004844expand_filename(
4845 exarg_T *eap,
4846 char_u **cmdlinep,
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004847 char **errormsgp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004848{
4849 int has_wildcards; /* need to expand wildcards */
4850 char_u *repl;
4851 int srclen;
4852 char_u *p;
4853 int n;
Bram Moolenaar63b92542007-03-27 14:57:09 +00004854 int escaped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004855
Bram Moolenaar748bf032005-02-02 23:04:36 +00004856#ifdef FEAT_QUICKFIX
4857 /* Skip a regexp pattern for ":vimgrep[add] pat file..." */
4858 p = skip_grep_pat(eap);
4859#else
4860 p = eap->arg;
4861#endif
4862
Bram Moolenaar071d4272004-06-13 20:20:40 +00004863 /*
4864 * Decide to expand wildcards *before* replacing '%', '#', etc. If
4865 * the file name contains a wildcard it should not cause expanding.
4866 * (it will be expanded anyway if there is a wildcard before replacing).
4867 */
Bram Moolenaar748bf032005-02-02 23:04:36 +00004868 has_wildcards = mch_has_wildcard(p);
4869 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004870 {
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004871#ifdef FEAT_EVAL
4872 /* Skip over `=expr`, wildcards in it are not expanded. */
4873 if (p[0] == '`' && p[1] == '=')
4874 {
4875 p += 2;
4876 (void)skip_expr(&p);
4877 if (*p == '`')
4878 ++p;
4879 continue;
4880 }
4881#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004882 /*
4883 * Quick check if this cannot be the start of a special string.
4884 * Also removes backslash before '%', '#' and '<'.
4885 */
4886 if (vim_strchr((char_u *)"%#<", *p) == NULL)
4887 {
4888 ++p;
4889 continue;
4890 }
4891
4892 /*
4893 * Try to find a match at this position.
4894 */
Bram Moolenaar63b92542007-03-27 14:57:09 +00004895 repl = eval_vars(p, eap->arg, &srclen, &(eap->do_ecmd_lnum),
4896 errormsgp, &escaped);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004897 if (*errormsgp != NULL) /* error detected */
4898 return FAIL;
4899 if (repl == NULL) /* no match found */
4900 {
4901 p += srclen;
4902 continue;
4903 }
4904
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00004905 /* Wildcards won't be expanded below, the replacement is taken
4906 * literally. But do expand "~/file", "~user/file" and "$HOME/file". */
4907 if (vim_strchr(repl, '$') != NULL || vim_strchr(repl, '~') != NULL)
4908 {
4909 char_u *l = repl;
4910
4911 repl = expand_env_save(repl);
4912 vim_free(l);
4913 }
4914
Bram Moolenaar63b92542007-03-27 14:57:09 +00004915 /* Need to escape white space et al. with a backslash.
4916 * Don't do this for:
4917 * - replacement that already has been escaped: "##"
4918 * - shell commands (may have to use quotes instead).
4919 * - non-unix systems when there is a single argument (spaces don't
4920 * separate arguments then).
4921 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004922 if (!eap->usefilter
Bram Moolenaar63b92542007-03-27 14:57:09 +00004923 && !escaped
Bram Moolenaar071d4272004-06-13 20:20:40 +00004924 && eap->cmdidx != CMD_bang
Bram Moolenaar071d4272004-06-13 20:20:40 +00004925 && eap->cmdidx != CMD_grep
4926 && eap->cmdidx != CMD_grepadd
Bram Moolenaarbf15b8d2017-06-04 20:43:48 +02004927 && eap->cmdidx != CMD_hardcopy
Bram Moolenaar67883b42017-07-27 22:57:00 +02004928 && eap->cmdidx != CMD_lgrep
4929 && eap->cmdidx != CMD_lgrepadd
4930 && eap->cmdidx != CMD_lmake
4931 && eap->cmdidx != CMD_make
4932 && eap->cmdidx != CMD_terminal
Bram Moolenaar071d4272004-06-13 20:20:40 +00004933#ifndef UNIX
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004934 && !(eap->argt & EX_NOSPC)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004935#endif
4936 )
4937 {
4938 char_u *l;
4939#ifdef BACKSLASH_IN_FILENAME
4940 /* Don't escape a backslash here, because rem_backslash() doesn't
4941 * remove it later. */
4942 static char_u *nobslash = (char_u *)" \t\"|";
4943# define ESCAPE_CHARS nobslash
4944#else
4945# define ESCAPE_CHARS escape_chars
4946#endif
4947
4948 for (l = repl; *l; ++l)
4949 if (vim_strchr(ESCAPE_CHARS, *l) != NULL)
4950 {
4951 l = vim_strsave_escaped(repl, ESCAPE_CHARS);
4952 if (l != NULL)
4953 {
4954 vim_free(repl);
4955 repl = l;
4956 }
4957 break;
4958 }
4959 }
4960
4961 /* For a shell command a '!' must be escaped. */
Bram Moolenaar67883b42017-07-27 22:57:00 +02004962 if ((eap->usefilter || eap->cmdidx == CMD_bang
4963 || eap->cmdidx == CMD_terminal)
Bram Moolenaar31b7d382014-04-01 18:54:48 +02004964 && vim_strpbrk(repl, (char_u *)"!") != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004965 {
4966 char_u *l;
4967
Bram Moolenaar31b7d382014-04-01 18:54:48 +02004968 l = vim_strsave_escaped(repl, (char_u *)"!");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004969 if (l != NULL)
4970 {
4971 vim_free(repl);
4972 repl = l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004973 }
4974 }
4975
4976 p = repl_cmdline(eap, p, srclen, repl, cmdlinep);
4977 vim_free(repl);
4978 if (p == NULL)
4979 return FAIL;
4980 }
4981
4982 /*
4983 * One file argument: Expand wildcards.
4984 * Don't do this with ":r !command" or ":w !command".
4985 */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004986 if ((eap->argt & EX_NOSPC) && !eap->usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004987 {
4988 /*
4989 * May do this twice:
4990 * 1. Replace environment variables.
4991 * 2. Replace any other wildcards, remove backslashes.
4992 */
4993 for (n = 1; n <= 2; ++n)
4994 {
4995 if (n == 2)
4996 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004997 /*
4998 * Halve the number of backslashes (this is Vi compatible).
4999 * For Unix and OS/2, when wildcards are expanded, this is
5000 * done by ExpandOne() below.
5001 */
Bram Moolenaare7fedb62015-12-31 19:07:19 +01005002#if defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005003 if (!has_wildcards)
5004#endif
5005 backslash_halve(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005006 }
5007
5008 if (has_wildcards)
5009 {
5010 if (n == 1)
5011 {
5012 /*
5013 * First loop: May expand environment variables. This
5014 * can be done much faster with expand_env() than with
5015 * something else (e.g., calling a shell).
5016 * After expanding environment variables, check again
5017 * if there are still wildcards present.
5018 */
5019 if (vim_strchr(eap->arg, '$') != NULL
5020 || vim_strchr(eap->arg, '~') != NULL)
5021 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +00005022 expand_env_esc(eap->arg, NameBuff, MAXPATHL,
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00005023 TRUE, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005024 has_wildcards = mch_has_wildcard(NameBuff);
5025 p = NameBuff;
5026 }
5027 else
5028 p = NULL;
5029 }
5030 else /* n == 2 */
5031 {
5032 expand_T xpc;
Bram Moolenaar94950a92010-12-02 16:01:29 +01005033 int options = WILD_LIST_NOTFOUND|WILD_ADD_SLASH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005034
5035 ExpandInit(&xpc);
5036 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar94950a92010-12-02 16:01:29 +01005037 if (p_wic)
5038 options += WILD_ICASE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005039 p = ExpandOne(&xpc, eap->arg, NULL,
Bram Moolenaar94950a92010-12-02 16:01:29 +01005040 options, WILD_EXPAND_FREE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005041 if (p == NULL)
5042 return FAIL;
5043 }
5044 if (p != NULL)
5045 {
5046 (void)repl_cmdline(eap, eap->arg, (int)STRLEN(eap->arg),
5047 p, cmdlinep);
5048 if (n == 2) /* p came from ExpandOne() */
5049 vim_free(p);
5050 }
5051 }
5052 }
5053 }
5054 return OK;
5055}
5056
5057/*
5058 * Replace part of the command line, keeping eap->cmd, eap->arg and
5059 * eap->nextcmd correct.
5060 * "src" points to the part that is to be replaced, of length "srclen".
5061 * "repl" is the replacement string.
5062 * Returns a pointer to the character after the replaced string.
5063 * Returns NULL for failure.
5064 */
5065 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005066repl_cmdline(
5067 exarg_T *eap,
5068 char_u *src,
5069 int srclen,
5070 char_u *repl,
5071 char_u **cmdlinep)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005072{
5073 int len;
5074 int i;
5075 char_u *new_cmdline;
5076
5077 /*
5078 * The new command line is build in new_cmdline[].
5079 * First allocate it.
5080 * Careful: a "+cmd" argument may have been NUL terminated.
5081 */
5082 len = (int)STRLEN(repl);
5083 i = (int)(src - *cmdlinep) + (int)STRLEN(src + srclen) + len + 3;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005084 if (eap->nextcmd != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005085 i += (int)STRLEN(eap->nextcmd);/* add space for next command */
Bram Moolenaar964b3742019-05-24 18:54:09 +02005086 if ((new_cmdline = alloc(i)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005087 return NULL; /* out of memory! */
5088
5089 /*
5090 * Copy the stuff before the expanded part.
5091 * Copy the expanded stuff.
5092 * Copy what came after the expanded part.
5093 * Copy the next commands, if there are any.
5094 */
5095 i = (int)(src - *cmdlinep); /* length of part before match */
5096 mch_memmove(new_cmdline, *cmdlinep, (size_t)i);
Bram Moolenaara3ffd9c2005-07-21 21:03:15 +00005097
Bram Moolenaar071d4272004-06-13 20:20:40 +00005098 mch_memmove(new_cmdline + i, repl, (size_t)len);
5099 i += len; /* remember the end of the string */
5100 STRCPY(new_cmdline + i, src + srclen);
5101 src = new_cmdline + i; /* remember where to continue */
5102
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005103 if (eap->nextcmd != NULL) /* append next command */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005104 {
5105 i = (int)STRLEN(new_cmdline) + 1;
5106 STRCPY(new_cmdline + i, eap->nextcmd);
5107 eap->nextcmd = new_cmdline + i;
5108 }
5109 eap->cmd = new_cmdline + (eap->cmd - *cmdlinep);
5110 eap->arg = new_cmdline + (eap->arg - *cmdlinep);
5111 if (eap->do_ecmd_cmd != NULL && eap->do_ecmd_cmd != dollar_command)
5112 eap->do_ecmd_cmd = new_cmdline + (eap->do_ecmd_cmd - *cmdlinep);
5113 vim_free(*cmdlinep);
5114 *cmdlinep = new_cmdline;
5115
5116 return src;
5117}
5118
5119/*
5120 * Check for '|' to separate commands and '"' to start comments.
5121 */
5122 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005123separate_nextcmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005124{
5125 char_u *p;
5126
Bram Moolenaar86b68352004-12-27 21:59:20 +00005127#ifdef FEAT_QUICKFIX
Bram Moolenaar748bf032005-02-02 23:04:36 +00005128 p = skip_grep_pat(eap);
5129#else
5130 p = eap->arg;
Bram Moolenaar86b68352004-12-27 21:59:20 +00005131#endif
5132
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005133 for ( ; *p; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005134 {
5135 if (*p == Ctrl_V)
5136 {
Bram Moolenaar8071cb22019-07-12 17:58:01 +02005137 if (eap->argt & (EX_CTRLV | EX_XFILE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005138 ++p; /* skip CTRL-V and next char */
5139 else
Bram Moolenaarb0db5692007-08-14 20:54:49 +00005140 /* remove CTRL-V and skip next char */
Bram Moolenaara7241f52008-06-24 20:39:31 +00005141 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005142 if (*p == NUL) /* stop at NUL after CTRL-V */
5143 break;
5144 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005145
5146#ifdef FEAT_EVAL
5147 /* Skip over `=expr` when wildcards are expanded. */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02005148 else if (p[0] == '`' && p[1] == '=' && (eap->argt & EX_XFILE))
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005149 {
5150 p += 2;
5151 (void)skip_expr(&p);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005152 }
5153#endif
5154
Bram Moolenaar071d4272004-06-13 20:20:40 +00005155 /* Check for '"': start of comment or '|': next command */
5156 /* :@" and :*" do not start a comment!
5157 * :redir @" doesn't either. */
Bram Moolenaar8071cb22019-07-12 17:58:01 +02005158 else if ((*p == '"' && !(eap->argt & EX_NOTRLCOM)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005159 && ((eap->cmdidx != CMD_at && eap->cmdidx != CMD_star)
5160 || p != eap->arg)
5161 && (eap->cmdidx != CMD_redir
5162 || p != eap->arg + 1 || p[-1] != '@'))
5163 || *p == '|' || *p == '\n')
5164 {
5165 /*
Bram Moolenaar8071cb22019-07-12 17:58:01 +02005166 * We remove the '\' before the '|', unless EX_CTRLV is used
Bram Moolenaar071d4272004-06-13 20:20:40 +00005167 * AND 'b' is present in 'cpoptions'.
5168 */
5169 if ((vim_strchr(p_cpo, CPO_BAR) == NULL
Bram Moolenaar8071cb22019-07-12 17:58:01 +02005170 || !(eap->argt & EX_CTRLV)) && *(p - 1) == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005171 {
Bram Moolenaara7241f52008-06-24 20:39:31 +00005172 STRMOVE(p - 1, p); /* remove the '\' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005173 --p;
5174 }
5175 else
5176 {
5177 eap->nextcmd = check_nextcmd(p);
5178 *p = NUL;
5179 break;
5180 }
5181 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005182 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00005183
Bram Moolenaar8071cb22019-07-12 17:58:01 +02005184 if (!(eap->argt & EX_NOTRLCOM)) /* remove trailing spaces */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005185 del_trailing_spaces(eap->arg);
5186}
5187
5188/*
5189 * get + command from ex argument
5190 */
5191 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005192getargcmd(char_u **argp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005193{
5194 char_u *arg = *argp;
5195 char_u *command = NULL;
5196
5197 if (*arg == '+') /* +[command] */
5198 {
5199 ++arg;
Bram Moolenaar3e451592014-04-02 14:22:05 +02005200 if (vim_isspace(*arg) || *arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005201 command = dollar_command;
5202 else
5203 {
5204 command = arg;
5205 arg = skip_cmd_arg(command, TRUE);
5206 if (*arg != NUL)
5207 *arg++ = NUL; /* terminate command with NUL */
5208 }
5209
5210 arg = skipwhite(arg); /* skip over spaces */
5211 *argp = arg;
5212 }
5213 return command;
5214}
5215
5216/*
5217 * Find end of "+command" argument. Skip over "\ " and "\\".
5218 */
5219 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005220skip_cmd_arg(
5221 char_u *p,
5222 int rembs) /* TRUE to halve the number of backslashes */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005223{
5224 while (*p && !vim_isspace(*p))
5225 {
5226 if (*p == '\\' && p[1] != NUL)
5227 {
5228 if (rembs)
Bram Moolenaara7241f52008-06-24 20:39:31 +00005229 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005230 else
5231 ++p;
5232 }
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005233 MB_PTR_ADV(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005234 }
5235 return p;
5236}
5237
Bram Moolenaar333b80a2018-04-04 22:57:29 +02005238 int
5239get_bad_opt(char_u *p, exarg_T *eap)
5240{
5241 if (STRICMP(p, "keep") == 0)
5242 eap->bad_char = BAD_KEEP;
5243 else if (STRICMP(p, "drop") == 0)
5244 eap->bad_char = BAD_DROP;
5245 else if (MB_BYTE2LEN(*p) == 1 && p[1] == NUL)
5246 eap->bad_char = *p;
Bram Moolenaar75808492018-06-12 12:39:41 +02005247 else
5248 return FAIL;
5249 return OK;
Bram Moolenaar333b80a2018-04-04 22:57:29 +02005250}
5251
Bram Moolenaar071d4272004-06-13 20:20:40 +00005252/*
5253 * Get "++opt=arg" argument.
5254 * Return FAIL or OK.
5255 */
5256 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005257getargopt(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005258{
5259 char_u *arg = eap->arg + 2;
5260 int *pp = NULL;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02005261 int bad_char_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005262 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005263
5264 /* ":edit ++[no]bin[ary] file" */
5265 if (STRNCMP(arg, "bin", 3) == 0 || STRNCMP(arg, "nobin", 5) == 0)
5266 {
5267 if (*arg == 'n')
5268 {
5269 arg += 2;
5270 eap->force_bin = FORCE_NOBIN;
5271 }
5272 else
5273 eap->force_bin = FORCE_BIN;
5274 if (!checkforcmd(&arg, "binary", 3))
5275 return FAIL;
5276 eap->arg = skipwhite(arg);
5277 return OK;
5278 }
5279
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005280 /* ":read ++edit file" */
5281 if (STRNCMP(arg, "edit", 4) == 0)
5282 {
5283 eap->read_edit = TRUE;
5284 eap->arg = skipwhite(arg + 4);
5285 return OK;
5286 }
5287
Bram Moolenaar071d4272004-06-13 20:20:40 +00005288 if (STRNCMP(arg, "ff", 2) == 0)
5289 {
5290 arg += 2;
5291 pp = &eap->force_ff;
5292 }
5293 else if (STRNCMP(arg, "fileformat", 10) == 0)
5294 {
5295 arg += 10;
5296 pp = &eap->force_ff;
5297 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005298 else if (STRNCMP(arg, "enc", 3) == 0)
5299 {
Bram Moolenaarb38e9ab2011-12-14 14:49:45 +01005300 if (STRNCMP(arg, "encoding", 8) == 0)
5301 arg += 8;
5302 else
5303 arg += 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005304 pp = &eap->force_enc;
5305 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005306 else if (STRNCMP(arg, "bad", 3) == 0)
5307 {
5308 arg += 3;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02005309 pp = &bad_char_idx;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005310 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005311
5312 if (pp == NULL || *arg != '=')
5313 return FAIL;
5314
5315 ++arg;
5316 *pp = (int)(arg - eap->cmd);
5317 arg = skip_cmd_arg(arg, FALSE);
5318 eap->arg = skipwhite(arg);
5319 *arg = NUL;
5320
Bram Moolenaar071d4272004-06-13 20:20:40 +00005321 if (pp == &eap->force_ff)
5322 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005323 if (check_ff_value(eap->cmd + eap->force_ff) == FAIL)
5324 return FAIL;
Bram Moolenaar333b80a2018-04-04 22:57:29 +02005325 eap->force_ff = eap->cmd[eap->force_ff];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005326 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005327 else if (pp == &eap->force_enc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005328 {
5329 /* Make 'fileencoding' lower case. */
5330 for (p = eap->cmd + eap->force_enc; *p != NUL; ++p)
5331 *p = TOLOWER_ASC(*p);
5332 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005333 else
5334 {
5335 /* Check ++bad= argument. Must be a single-byte character, "keep" or
5336 * "drop". */
Bram Moolenaar333b80a2018-04-04 22:57:29 +02005337 if (get_bad_opt(eap->cmd + bad_char_idx, eap) == FAIL)
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005338 return FAIL;
5339 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005340
5341 return OK;
5342}
5343
Bram Moolenaar071d4272004-06-13 20:20:40 +00005344 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005345ex_autocmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005346{
5347 /*
Bram Moolenaar26d4b892018-07-04 22:26:28 +02005348 * Disallow autocommands from .exrc and .vimrc in current
Bram Moolenaar071d4272004-06-13 20:20:40 +00005349 * directory for security reasons.
5350 */
5351 if (secure)
5352 {
5353 secure = 2;
5354 eap->errmsg = e_curdir;
5355 }
5356 else if (eap->cmdidx == CMD_autocmd)
5357 do_autocmd(eap->arg, eap->forceit);
5358 else
5359 do_augroup(eap->arg, eap->forceit);
5360}
5361
5362/*
5363 * ":doautocmd": Apply the automatic commands to the current buffer.
5364 */
5365 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005366ex_doautocmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005367{
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005368 char_u *arg = eap->arg;
5369 int call_do_modelines = check_nomodeline(&arg);
Bram Moolenaar1610d052016-06-09 22:53:01 +02005370 int did_aucmd;
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005371
Bram Moolenaar1610d052016-06-09 22:53:01 +02005372 (void)do_doautocmd(arg, TRUE, &did_aucmd);
5373 /* Only when there is no <nomodeline>. */
5374 if (call_do_modelines && did_aucmd)
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005375 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005376}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005377
Bram Moolenaar071d4272004-06-13 20:20:40 +00005378/*
5379 * :[N]bunload[!] [N] [bufname] unload buffer
5380 * :[N]bdelete[!] [N] [bufname] delete buffer from buffer list
5381 * :[N]bwipeout[!] [N] [bufname] delete buffer really
5382 */
5383 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005384ex_bunload(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005385{
Bram Moolenaar8cdbd5b2019-06-16 15:50:45 +02005386 if (ERROR_IF_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02005387 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005388 eap->errmsg = do_bufdel(
5389 eap->cmdidx == CMD_bdelete ? DOBUF_DEL
5390 : eap->cmdidx == CMD_bwipeout ? DOBUF_WIPE
5391 : DOBUF_UNLOAD, eap->arg,
5392 eap->addr_count, (int)eap->line1, (int)eap->line2, eap->forceit);
5393}
5394
5395/*
5396 * :[N]buffer [N] to buffer N
5397 * :[N]sbuffer [N] to buffer N
5398 */
5399 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005400ex_buffer(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005401{
Bram Moolenaar8cdbd5b2019-06-16 15:50:45 +02005402 if (ERROR_IF_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02005403 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005404 if (*eap->arg)
5405 eap->errmsg = e_trailing;
5406 else
5407 {
5408 if (eap->addr_count == 0) /* default is current buffer */
5409 goto_buffer(eap, DOBUF_CURRENT, FORWARD, 0);
5410 else
5411 goto_buffer(eap, DOBUF_FIRST, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005412 if (eap->do_ecmd_cmd != NULL)
5413 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005414 }
5415}
5416
5417/*
5418 * :[N]bmodified [N] to next mod. buffer
5419 * :[N]sbmodified [N] to next mod. buffer
5420 */
5421 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005422ex_bmodified(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005423{
5424 goto_buffer(eap, DOBUF_MOD, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005425 if (eap->do_ecmd_cmd != NULL)
5426 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005427}
5428
5429/*
5430 * :[N]bnext [N] to next buffer
5431 * :[N]sbnext [N] split and to next buffer
5432 */
5433 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005434ex_bnext(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005435{
5436 goto_buffer(eap, DOBUF_CURRENT, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005437 if (eap->do_ecmd_cmd != NULL)
5438 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005439}
5440
5441/*
5442 * :[N]bNext [N] to previous buffer
5443 * :[N]bprevious [N] to previous buffer
5444 * :[N]sbNext [N] split and to previous buffer
5445 * :[N]sbprevious [N] split and to previous buffer
5446 */
5447 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005448ex_bprevious(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005449{
5450 goto_buffer(eap, DOBUF_CURRENT, BACKWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005451 if (eap->do_ecmd_cmd != NULL)
5452 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005453}
5454
5455/*
5456 * :brewind to first buffer
5457 * :bfirst to first buffer
5458 * :sbrewind split and to first buffer
5459 * :sbfirst split and to first buffer
5460 */
5461 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005462ex_brewind(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005463{
5464 goto_buffer(eap, DOBUF_FIRST, FORWARD, 0);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005465 if (eap->do_ecmd_cmd != NULL)
5466 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005467}
5468
5469/*
5470 * :blast to last buffer
5471 * :sblast split and to last buffer
5472 */
5473 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005474ex_blast(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005475{
5476 goto_buffer(eap, DOBUF_LAST, BACKWARD, 0);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005477 if (eap->do_ecmd_cmd != NULL)
5478 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005479}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005480
5481 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005482ends_excmd(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005483{
5484 return (c == NUL || c == '|' || c == '"' || c == '\n');
5485}
5486
5487#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA) || defined(FEAT_EVAL) \
5488 || defined(PROTO)
5489/*
5490 * Return the next command, after the first '|' or '\n'.
5491 * Return NULL if not found.
5492 */
5493 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005494find_nextcmd(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005495{
5496 while (*p != '|' && *p != '\n')
5497 {
5498 if (*p == NUL)
5499 return NULL;
5500 ++p;
5501 }
5502 return (p + 1);
5503}
5504#endif
5505
5506/*
Bram Moolenaar2256c992016-11-15 21:17:07 +01005507 * Check if *p is a separator between Ex commands, skipping over white space.
5508 * Return NULL if it isn't, the following character if it is.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005509 */
5510 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005511check_nextcmd(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005512{
Bram Moolenaar2256c992016-11-15 21:17:07 +01005513 char_u *s = skipwhite(p);
5514
5515 if (*s == '|' || *s == '\n')
5516 return (s + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005517 else
5518 return NULL;
5519}
5520
5521/*
5522 * - if there are more files to edit
5523 * - and this is the last window
5524 * - and forceit not used
5525 * - and not repeated twice on a row
5526 * return FAIL and give error message if 'message' TRUE
5527 * return OK otherwise
5528 */
5529 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005530check_more(
5531 int message, /* when FALSE check only, no messages */
5532 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005533{
5534 int n = ARGCOUNT - curwin->w_arg_idx - 1;
5535
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005536 if (!forceit && only_one_window()
5537 && ARGCOUNT > 1 && !arg_had_last && n >= 0 && quitmore == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005538 {
5539 if (message)
5540 {
5541#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
5542 if ((p_confirm || cmdmod.confirm) && curbuf->b_fname != NULL)
5543 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02005544 char_u buff[DIALOG_MSG_SIZE];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005545
Bram Moolenaarda6e8912018-08-21 15:12:14 +02005546 vim_snprintf((char *)buff, DIALOG_MSG_SIZE,
5547 NGETTEXT("%d more file to edit. Quit anyway?",
5548 "%d more files to edit. Quit anyway?", n), n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005549 if (vim_dialog_yesno(VIM_QUESTION, NULL, buff, 1) == VIM_YES)
5550 return OK;
5551 return FAIL;
5552 }
5553#endif
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01005554 semsg(NGETTEXT("E173: %d more file to edit",
5555 "E173: %d more files to edit", n), n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005556 quitmore = 2; /* next try to quit is allowed */
5557 }
5558 return FAIL;
5559 }
5560 return OK;
5561}
5562
Bram Moolenaarac9fb182019-04-27 13:04:13 +02005563#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005564/*
5565 * Function given to ExpandGeneric() to obtain the list of command names.
5566 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005567 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005568get_command_name(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005569{
5570 if (idx >= (int)CMD_SIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005571 return get_user_command_name(idx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005572 return cmdnames[idx].cmd_name;
5573}
5574#endif
5575
Bram Moolenaar071d4272004-06-13 20:20:40 +00005576 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005577ex_colorscheme(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005578{
Bram Moolenaare6850792010-05-14 15:28:44 +02005579 if (*eap->arg == NUL)
5580 {
5581#ifdef FEAT_EVAL
5582 char_u *expr = vim_strsave((char_u *)"g:colors_name");
5583 char_u *p = NULL;
5584
5585 if (expr != NULL)
5586 {
5587 ++emsg_off;
5588 p = eval_to_string(expr, NULL, FALSE);
5589 --emsg_off;
5590 vim_free(expr);
5591 }
5592 if (p != NULL)
5593 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01005594 msg((char *)p);
Bram Moolenaare6850792010-05-14 15:28:44 +02005595 vim_free(p);
5596 }
5597 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01005598 msg("default");
Bram Moolenaare6850792010-05-14 15:28:44 +02005599#else
Bram Moolenaar32526b32019-01-19 17:43:09 +01005600 msg(_("unknown"));
Bram Moolenaare6850792010-05-14 15:28:44 +02005601#endif
5602 }
5603 else if (load_colors(eap->arg) == FAIL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005604 semsg(_("E185: Cannot find color scheme '%s'"), eap->arg);
Bram Moolenaarf58d81a2019-01-28 20:19:05 +01005605
5606#ifdef FEAT_VTP
5607 else if (has_vtp_working())
5608 {
5609 // background color change requires clear + redraw
5610 update_screen(CLEAR);
5611 redrawcmd();
5612 }
5613#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005614}
5615
5616 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005617ex_highlight(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005618{
5619 if (*eap->arg == NUL && eap->cmd[2] == '!')
Bram Moolenaar32526b32019-01-19 17:43:09 +01005620 msg(_("Greetings, Vim user!"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005621 do_highlight(eap->arg, eap->forceit, FALSE);
5622}
5623
5624
5625/*
5626 * Call this function if we thought we were going to exit, but we won't
5627 * (because of an error). May need to restore the terminal mode.
5628 */
5629 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005630not_exiting(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005631{
5632 exiting = FALSE;
5633 settmode(TMODE_RAW);
5634}
5635
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005636 static int
5637before_quit_autocmds(win_T *wp, int quit_all, int forceit)
5638{
5639 apply_autocmds(EVENT_QUITPRE, NULL, NULL, FALSE, wp->w_buffer);
5640
5641 /* Bail out when autocommands closed the window.
5642 * Refuse to quit when the buffer in the last window is being closed (can
5643 * only happen in autocommands). */
5644 if (!win_valid(wp)
5645 || curbuf_locked()
5646 || (wp->w_buffer->b_nwindows == 1 && wp->w_buffer->b_locked > 0))
5647 return TRUE;
5648
5649 if (quit_all || (check_more(FALSE, forceit) == OK && only_one_window()))
5650 {
5651 apply_autocmds(EVENT_EXITPRE, NULL, NULL, FALSE, curbuf);
5652 /* Refuse to quit when locked or when the buffer in the last window is
5653 * being closed (can only happen in autocommands). */
5654 if (curbuf_locked()
5655 || (curbuf->b_nwindows == 1 && curbuf->b_locked > 0))
5656 return TRUE;
5657 }
5658
5659 return FALSE;
5660}
5661
Bram Moolenaar071d4272004-06-13 20:20:40 +00005662/*
Bram Moolenaarf240e182014-11-27 18:33:02 +01005663 * ":quit": quit current window, quit Vim if the last window is closed.
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005664 * ":{nr}quit": quit window {nr}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005665 */
5666 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005667ex_quit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005668{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005669 win_T *wp;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005670
Bram Moolenaar071d4272004-06-13 20:20:40 +00005671#ifdef FEAT_CMDWIN
5672 if (cmdwin_type != 0)
5673 {
5674 cmdwin_result = Ctrl_C;
5675 return;
5676 }
5677#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005678 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005679 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005680 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005681 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005682 return;
5683 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005684 if (eap->addr_count > 0)
5685 {
Bram Moolenaarf240e182014-11-27 18:33:02 +01005686 int wnr = eap->line2;
5687
5688 for (wp = firstwin; wp->w_next != NULL; wp = wp->w_next)
5689 if (--wnr <= 0)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005690 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005691 }
5692 else
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005693 wp = curwin;
Bram Moolenaarf240e182014-11-27 18:33:02 +01005694
Bram Moolenaar87ffb5c2017-10-19 12:37:42 +02005695 /* Refuse to quit when locked. */
5696 if (curbuf_locked())
5697 return;
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005698
5699 /* Trigger QuitPre and maybe ExitPre */
5700 if (before_quit_autocmds(wp, FALSE, eap->forceit))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005701 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005702
5703#ifdef FEAT_NETBEANS_INTG
5704 netbeansForcedQuit = eap->forceit;
5705#endif
5706
5707 /*
5708 * If there are more files or windows we won't exit.
5709 */
5710 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
5711 exiting = TRUE;
Bram Moolenaarff930ca2017-10-19 17:12:10 +02005712 if ((!buf_hide(wp->w_buffer)
5713 && check_changed(wp->w_buffer, (p_awa ? CCGD_AW : 0)
Bram Moolenaar45d3b142013-11-09 03:31:51 +01005714 | (eap->forceit ? CCGD_FORCEIT : 0)
5715 | CCGD_EXCMD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005716 || check_more(TRUE, eap->forceit) == FAIL
Bram Moolenaar027387f2016-01-02 22:25:52 +01005717 || (only_one_window() && check_changed_any(eap->forceit, TRUE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005718 {
5719 not_exiting();
5720 }
5721 else
5722 {
Bram Moolenaarc7a0d322015-06-19 12:43:07 +02005723 /* quit last window
5724 * Note: only_one_window() returns true, even so a help window is
5725 * still open. In that case only quit, if no address has been
5726 * specified. Example:
5727 * :h|wincmd w|1q - don't quit
5728 * :h|wincmd w|q - quit
5729 */
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01005730 if (only_one_window() && (ONE_WINDOW || eap->addr_count == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005731 getout(0);
Bram Moolenaar2c33d7b2017-10-14 16:06:20 +02005732 not_exiting();
Bram Moolenaar4033c552017-09-16 20:54:51 +02005733#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00005734 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005735#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005736 /* close window; may free buffer */
Bram Moolenaareb44a682017-08-03 22:44:55 +02005737 win_close(wp, !buf_hide(wp->w_buffer) || eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005738 }
5739}
5740
5741/*
5742 * ":cquit".
5743 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005744 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005745ex_cquit(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005746{
5747 getout(1); /* this does not always pass on the exit code to the Manx
5748 compiler. why? */
5749}
5750
5751/*
5752 * ":qall": try to quit all windows
5753 */
5754 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005755ex_quit_all(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005756{
5757# ifdef FEAT_CMDWIN
5758 if (cmdwin_type != 0)
5759 {
5760 if (eap->forceit)
5761 cmdwin_result = K_XF1; /* ex_window() takes care of this */
5762 else
5763 cmdwin_result = K_XF2;
5764 return;
5765 }
5766# endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005767
5768 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005769 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005770 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005771 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005772 return;
5773 }
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005774
5775 if (before_quit_autocmds(curwin, TRUE, eap->forceit))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005776 return;
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005777
Bram Moolenaar071d4272004-06-13 20:20:40 +00005778 exiting = TRUE;
Bram Moolenaar027387f2016-01-02 22:25:52 +01005779 if (eap->forceit || !check_changed_any(FALSE, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005780 getout(0);
5781 not_exiting();
5782}
5783
Bram Moolenaar071d4272004-06-13 20:20:40 +00005784/*
5785 * ":close": close current window, unless it is the last one
5786 */
5787 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005788ex_close(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005789{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005790 win_T *win;
5791 int winnr = 0;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005792#ifdef FEAT_CMDWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00005793 if (cmdwin_type != 0)
Bram Moolenaar9bd1a7e2011-05-19 14:50:54 +02005794 cmdwin_result = Ctrl_C;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005795 else
Bram Moolenaar4033c552017-09-16 20:54:51 +02005796#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005797 if (!text_locked() && !curbuf_locked())
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005798 {
5799 if (eap->addr_count == 0)
5800 ex_win_close(eap->forceit, curwin, NULL);
Bram Moolenaar6d41c782018-06-06 09:11:12 +02005801 else
5802 {
Bram Moolenaar29323592016-07-24 22:04:11 +02005803 FOR_ALL_WINDOWS(win)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005804 {
5805 winnr++;
5806 if (winnr == eap->line2)
5807 break;
5808 }
5809 if (win == NULL)
5810 win = lastwin;
5811 ex_win_close(eap->forceit, win, NULL);
5812 }
5813 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005814}
5815
Bram Moolenaar4033c552017-09-16 20:54:51 +02005816#ifdef FEAT_QUICKFIX
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005817/*
5818 * ":pclose": Close any preview window.
5819 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005820 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005821ex_pclose(exarg_T *eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005822{
5823 win_T *win;
5824
Bram Moolenaar79648732019-07-18 21:43:07 +02005825 // First close any normal window.
Bram Moolenaar29323592016-07-24 22:04:11 +02005826 FOR_ALL_WINDOWS(win)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005827 if (win->w_p_pvw)
5828 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00005829 ex_win_close(eap->forceit, win, NULL);
Bram Moolenaar79648732019-07-18 21:43:07 +02005830 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005831 }
Bram Moolenaar79648732019-07-18 21:43:07 +02005832# ifdef FEAT_TEXT_PROP
5833 // Also when 'previewpopup' is empty, it might have been cleared.
5834 popup_close_preview();
5835# endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005836}
Bram Moolenaar4033c552017-09-16 20:54:51 +02005837#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005838
Bram Moolenaarf740b292006-02-16 22:11:02 +00005839/*
5840 * Close window "win" and take care of handling closing the last window for a
5841 * modified buffer.
5842 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005843 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005844ex_win_close(
5845 int forceit,
5846 win_T *win,
5847 tabpage_T *tp) /* NULL or the tab page "win" is in */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005848{
5849 int need_hide;
5850 buf_T *buf = win->w_buffer;
5851
5852 need_hide = (bufIsChanged(buf) && buf->b_nwindows <= 1);
Bram Moolenaareb44a682017-08-03 22:44:55 +02005853 if (need_hide && !buf_hide(buf) && !forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005854 {
Bram Moolenaar4033c552017-09-16 20:54:51 +02005855#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005856 if ((p_confirm || cmdmod.confirm) && p_write)
5857 {
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005858 bufref_T bufref;
5859
5860 set_bufref(&bufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005861 dialog_changed(buf, FALSE);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005862 if (bufref_valid(&bufref) && bufIsChanged(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005863 return;
5864 need_hide = FALSE;
5865 }
5866 else
Bram Moolenaar4033c552017-09-16 20:54:51 +02005867#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005868 {
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02005869 no_write_message();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005870 return;
5871 }
5872 }
5873
Bram Moolenaar4033c552017-09-16 20:54:51 +02005874#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00005875 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005876#endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00005877
Bram Moolenaar071d4272004-06-13 20:20:40 +00005878 /* free buffer when not hiding it or when it's a scratch buffer */
Bram Moolenaarf740b292006-02-16 22:11:02 +00005879 if (tp == NULL)
Bram Moolenaareb44a682017-08-03 22:44:55 +02005880 win_close(win, !need_hide && !buf_hide(buf));
Bram Moolenaarf740b292006-02-16 22:11:02 +00005881 else
Bram Moolenaareb44a682017-08-03 22:44:55 +02005882 win_close_othertab(win, !need_hide && !buf_hide(buf), tp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005883}
5884
Bram Moolenaar071d4272004-06-13 20:20:40 +00005885/*
Bram Moolenaardea25702017-01-29 15:18:10 +01005886 * Handle the argument for a tabpage related ex command.
5887 * Returns a tabpage number.
5888 * When an error is encountered then eap->errmsg is set.
5889 */
5890 static int
5891get_tabpage_arg(exarg_T *eap)
5892{
5893 int tab_number;
5894 int unaccept_arg0 = (eap->cmdidx == CMD_tabmove) ? 0 : 1;
5895
5896 if (eap->arg && *eap->arg != NUL)
5897 {
5898 char_u *p = eap->arg;
5899 char_u *p_save;
5900 int relative = 0; /* argument +N/-N means: go to N places to the
5901 * right/left relative to the current position. */
5902
5903 if (*p == '-')
5904 {
5905 relative = -1;
5906 p++;
5907 }
5908 else if (*p == '+')
5909 {
5910 relative = 1;
5911 p++;
5912 }
5913
5914 p_save = p;
5915 tab_number = getdigits(&p);
5916
5917 if (relative == 0)
5918 {
5919 if (STRCMP(p, "$") == 0)
5920 tab_number = LAST_TAB_NR;
5921 else if (p == p_save || *p_save == '-' || *p != NUL
5922 || tab_number > LAST_TAB_NR)
5923 {
5924 /* No numbers as argument. */
5925 eap->errmsg = e_invarg;
5926 goto theend;
5927 }
5928 }
5929 else
5930 {
5931 if (*p_save == NUL)
5932 tab_number = 1;
5933 else if (p == p_save || *p_save == '-' || *p != NUL
5934 || tab_number == 0)
5935 {
5936 /* No numbers as argument. */
5937 eap->errmsg = e_invarg;
5938 goto theend;
5939 }
5940 tab_number = tab_number * relative + tabpage_index(curtab);
5941 if (!unaccept_arg0 && relative == -1)
5942 --tab_number;
5943 }
5944 if (tab_number < unaccept_arg0 || tab_number > LAST_TAB_NR)
5945 eap->errmsg = e_invarg;
5946 }
5947 else if (eap->addr_count > 0)
5948 {
5949 if (unaccept_arg0 && eap->line2 == 0)
Bram Moolenaarc6251552017-01-29 21:42:20 +01005950 {
Bram Moolenaardea25702017-01-29 15:18:10 +01005951 eap->errmsg = e_invrange;
Bram Moolenaarc6251552017-01-29 21:42:20 +01005952 tab_number = 0;
5953 }
Bram Moolenaardea25702017-01-29 15:18:10 +01005954 else
5955 {
5956 tab_number = eap->line2;
Bram Moolenaar82a12462019-01-22 22:41:42 +01005957 if (!unaccept_arg0 && *skipwhite(*eap->cmdlinep) == '-')
Bram Moolenaardea25702017-01-29 15:18:10 +01005958 {
5959 --tab_number;
5960 if (tab_number < unaccept_arg0)
5961 eap->errmsg = e_invarg;
5962 }
5963 }
5964 }
5965 else
5966 {
5967 switch (eap->cmdidx)
5968 {
5969 case CMD_tabnext:
5970 tab_number = tabpage_index(curtab) + 1;
5971 if (tab_number > LAST_TAB_NR)
5972 tab_number = 1;
5973 break;
5974 case CMD_tabmove:
5975 tab_number = LAST_TAB_NR;
5976 break;
5977 default:
5978 tab_number = tabpage_index(curtab);
5979 }
5980 }
5981
5982theend:
5983 return tab_number;
5984}
5985
5986/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00005987 * ":tabclose": close current tab page, unless it is the last one.
5988 * ":tabclose N": close tab page N.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005989 */
5990 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005991ex_tabclose(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005992{
Bram Moolenaarf740b292006-02-16 22:11:02 +00005993 tabpage_T *tp;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005994 int tab_number;
Bram Moolenaarf740b292006-02-16 22:11:02 +00005995
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005996# ifdef FEAT_CMDWIN
5997 if (cmdwin_type != 0)
5998 cmdwin_result = K_IGNORE;
5999 else
6000# endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00006001 if (first_tabpage->tp_next == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006002 emsg(_("E784: Cannot close last tab page"));
Bram Moolenaarf740b292006-02-16 22:11:02 +00006003 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006004 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006005 tab_number = get_tabpage_arg(eap);
6006 if (eap->errmsg == NULL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006007 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006008 tp = find_tabpage(tab_number);
Bram Moolenaarf740b292006-02-16 22:11:02 +00006009 if (tp == NULL)
6010 {
6011 beep_flush();
6012 return;
6013 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00006014 if (tp != curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006015 {
6016 tabpage_close_other(tp, eap->forceit);
6017 return;
6018 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006019 else if (!text_locked() && !curbuf_locked())
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006020 tabpage_close(eap->forceit);
6021 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006022 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006023}
6024
6025/*
6026 * ":tabonly": close all tab pages except the current one
6027 */
6028 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006029ex_tabonly(exarg_T *eap)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006030{
6031 tabpage_T *tp;
6032 int done;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006033 int tab_number;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006034
6035# ifdef FEAT_CMDWIN
6036 if (cmdwin_type != 0)
6037 cmdwin_result = K_IGNORE;
6038 else
6039# endif
6040 if (first_tabpage->tp_next == NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01006041 msg(_("Already only one tab page"));
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006042 else
6043 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006044 tab_number = get_tabpage_arg(eap);
6045 if (eap->errmsg == NULL)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006046 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006047 goto_tabpage(tab_number);
6048 /* Repeat this up to a 1000 times, because autocommands may
6049 * mess up the lists. */
6050 for (done = 0; done < 1000; ++done)
6051 {
6052 FOR_ALL_TABPAGES(tp)
6053 if (tp->tp_topframe != topframe)
6054 {
6055 tabpage_close_other(tp, eap->forceit);
6056 /* if we failed to close it quit */
6057 if (valid_tabpage(tp))
6058 done = 1000;
6059 /* start over, "tp" is now invalid */
6060 break;
6061 }
6062 if (first_tabpage->tp_next == NULL)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006063 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006064 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006065 }
6066 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006067}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006068
6069/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00006070 * Close the current tab page.
6071 */
6072 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006073tabpage_close(int forceit)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006074{
6075 /* First close all the windows but the current one. If that worked then
6076 * close the last window in this tab, that will close it. */
Bram Moolenaar459ca562016-11-10 18:16:33 +01006077 if (!ONE_WINDOW)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006078 close_others(TRUE, forceit);
Bram Moolenaar459ca562016-11-10 18:16:33 +01006079 if (ONE_WINDOW)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006080 ex_win_close(forceit, curwin, NULL);
6081# ifdef FEAT_GUI
6082 need_mouse_correct = TRUE;
6083# endif
6084}
6085
6086/*
6087 * Close tab page "tp", which is not the current tab page.
6088 * Note that autocommands may make "tp" invalid.
Bram Moolenaar7875acc2006-09-10 13:51:17 +00006089 * Also takes care of the tab pages line disappearing when closing the
6090 * last-but-one tab page.
Bram Moolenaarf740b292006-02-16 22:11:02 +00006091 */
6092 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006093tabpage_close_other(tabpage_T *tp, int forceit)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006094{
6095 int done = 0;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006096 win_T *wp;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00006097 int h = tabline_height();
Bram Moolenaarf740b292006-02-16 22:11:02 +00006098
6099 /* Limit to 1000 windows, autocommands may add a window while we close
6100 * one. OK, so I'm paranoid... */
6101 while (++done < 1000)
6102 {
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006103 wp = tp->tp_firstwin;
6104 ex_win_close(forceit, wp, tp);
Bram Moolenaarf740b292006-02-16 22:11:02 +00006105
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006106 /* Autocommands may delete the tab page under our fingers and we may
6107 * fail to close a window with a modified buffer. */
6108 if (!valid_tabpage(tp) || tp->tp_firstwin == wp)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006109 break;
6110 }
Bram Moolenaar7875acc2006-09-10 13:51:17 +00006111
Bram Moolenaar29323592016-07-24 22:04:11 +02006112 apply_autocmds(EVENT_TABCLOSED, NULL, NULL, FALSE, curbuf);
Bram Moolenaar29323592016-07-24 22:04:11 +02006113
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006114 redraw_tabline = TRUE;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00006115 if (h != tabline_height())
6116 shell_new_rows();
Bram Moolenaarf740b292006-02-16 22:11:02 +00006117}
6118
6119/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006120 * ":only".
6121 */
6122 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006123ex_only(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006124{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01006125 win_T *wp;
6126 int wnr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006127# ifdef FEAT_GUI
6128 need_mouse_correct = TRUE;
6129# endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01006130 if (eap->addr_count > 0)
6131 {
6132 wnr = eap->line2;
6133 for (wp = firstwin; --wnr > 0; )
6134 {
6135 if (wp->w_next == NULL)
6136 break;
6137 else
6138 wp = wp->w_next;
6139 }
6140 win_goto(wp);
6141 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006142 close_others(TRUE, eap->forceit);
6143}
6144
6145/*
6146 * ":all" and ":sall".
Bram Moolenaard9967712006-03-11 21:18:15 +00006147 * Also used for ":tab drop file ..." after setting the argument list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006148 */
Bram Moolenaard9967712006-03-11 21:18:15 +00006149 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006150ex_all(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006151{
6152 if (eap->addr_count == 0)
6153 eap->line2 = 9999;
Bram Moolenaard9967712006-03-11 21:18:15 +00006154 do_arg_all((int)eap->line2, eap->forceit, eap->cmdidx == CMD_drop);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006155}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006156
6157 static void
Bram Moolenaar5e1e6d22017-01-02 17:26:00 +01006158ex_hide(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006159{
Bram Moolenaar2256c992016-11-15 21:17:07 +01006160 /* ":hide" or ":hide | cmd": hide current window */
Bram Moolenaar2256c992016-11-15 21:17:07 +01006161 if (!eap->skip)
6162 {
Bram Moolenaar4033c552017-09-16 20:54:51 +02006163#ifdef FEAT_GUI
Bram Moolenaar2256c992016-11-15 21:17:07 +01006164 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006165#endif
Bram Moolenaar2256c992016-11-15 21:17:07 +01006166 if (eap->addr_count == 0)
6167 win_close(curwin, FALSE); /* don't free buffer */
6168 else
6169 {
6170 int winnr = 0;
6171 win_T *win;
Bram Moolenaarf240e182014-11-27 18:33:02 +01006172
Bram Moolenaar2256c992016-11-15 21:17:07 +01006173 FOR_ALL_WINDOWS(win)
6174 {
6175 winnr++;
6176 if (winnr == eap->line2)
6177 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01006178 }
Bram Moolenaar2256c992016-11-15 21:17:07 +01006179 if (win == NULL)
6180 win = lastwin;
6181 win_close(win, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006182 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006183 }
6184}
6185
6186/*
6187 * ":stop" and ":suspend": Suspend Vim.
6188 */
6189 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006190ex_stop(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006191{
6192 /*
6193 * Disallow suspending for "rvim".
6194 */
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006195 if (!check_restricted())
Bram Moolenaar071d4272004-06-13 20:20:40 +00006196 {
6197 if (!eap->forceit)
6198 autowrite_all();
6199 windgoto((int)Rows - 1, 0);
6200 out_char('\n');
6201 out_flush();
6202 stoptermcap();
6203 out_flush(); /* needed for SUN to restore xterm buffer */
6204#ifdef FEAT_TITLE
Bram Moolenaar40385db2018-08-07 22:31:44 +02006205 mch_restore_title(SAVE_RESTORE_BOTH); /* restore window titles */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006206#endif
6207 ui_suspend(); /* call machine specific function */
6208#ifdef FEAT_TITLE
6209 maketitle();
6210 resettitle(); /* force updating the title */
6211#endif
6212 starttermcap();
6213 scroll_start(); /* scroll screen before redrawing */
6214 redraw_later_clear();
6215 shell_resized(); /* may have resized window */
6216 }
6217}
6218
6219/*
Bram Moolenaar12a96de2018-03-11 14:44:18 +01006220 * ":exit", ":xit" and ":wq": Write file and quite the current window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006221 */
6222 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006223ex_exit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006224{
6225#ifdef FEAT_CMDWIN
6226 if (cmdwin_type != 0)
6227 {
6228 cmdwin_result = Ctrl_C;
6229 return;
6230 }
6231#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006232 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006233 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006234 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006235 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006236 return;
6237 }
Bram Moolenaar12a96de2018-03-11 14:44:18 +01006238
6239 if (before_quit_autocmds(curwin, FALSE, eap->forceit))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006240 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006241
6242 /*
6243 * if more files or windows we won't exit
6244 */
6245 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
6246 exiting = TRUE;
6247 if ( ((eap->cmdidx == CMD_wq
6248 || curbufIsChanged())
6249 && do_write(eap) == FAIL)
6250 || check_more(TRUE, eap->forceit) == FAIL
Bram Moolenaar027387f2016-01-02 22:25:52 +01006251 || (only_one_window() && check_changed_any(eap->forceit, FALSE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006252 {
6253 not_exiting();
6254 }
6255 else
6256 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006257 if (only_one_window()) /* quit last window, exit Vim */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006258 getout(0);
Bram Moolenaar2c33d7b2017-10-14 16:06:20 +02006259 not_exiting();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006260# ifdef FEAT_GUI
6261 need_mouse_correct = TRUE;
6262# endif
Bram Moolenaar1a4a75c2013-07-28 16:03:06 +02006263 /* Quit current window, may free the buffer. */
Bram Moolenaareb44a682017-08-03 22:44:55 +02006264 win_close(curwin, !buf_hide(curwin->w_buffer));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006265 }
6266}
6267
6268/*
6269 * ":print", ":list", ":number".
6270 */
6271 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006272ex_print(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006273{
Bram Moolenaardf177f62005-02-22 08:39:57 +00006274 if (curbuf->b_ml.ml_flags & ML_EMPTY)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006275 emsg(_(e_emptybuf));
Bram Moolenaardf177f62005-02-22 08:39:57 +00006276 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006277 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00006278 for ( ;!got_int; ui_breakcheck())
6279 {
6280 print_line(eap->line1,
6281 (eap->cmdidx == CMD_number || eap->cmdidx == CMD_pound
6282 || (eap->flags & EXFLAG_NR)),
6283 eap->cmdidx == CMD_list || (eap->flags & EXFLAG_LIST));
6284 if (++eap->line1 > eap->line2)
6285 break;
6286 out_flush(); /* show one line at a time */
6287 }
6288 setpcmark();
6289 /* put cursor at last line */
6290 curwin->w_cursor.lnum = eap->line2;
6291 beginline(BL_SOL | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006292 }
6293
Bram Moolenaar071d4272004-06-13 20:20:40 +00006294 ex_no_reprint = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006295}
6296
6297#ifdef FEAT_BYTEOFF
6298 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006299ex_goto(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006300{
6301 goto_byte(eap->line2);
6302}
6303#endif
6304
6305/*
6306 * ":shell".
6307 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006308 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006309ex_shell(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006310{
6311 do_shell(NULL, 0);
6312}
6313
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006314#if defined(HAVE_DROP_FILE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006315
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006316static int drop_busy = FALSE;
6317static int drop_filec;
6318static char_u **drop_filev = NULL;
6319static int drop_split;
6320static void (*drop_callback)(void *);
6321static void *drop_cookie;
6322
6323 static void
6324handle_drop_internal(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006325{
6326 exarg_T ea;
6327 int save_msg_scroll = msg_scroll;
6328
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006329 // Setting the argument list may cause screen updates and being called
6330 // recursively. Avoid that by setting drop_busy.
6331 drop_busy = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006332
6333 /* Check whether the current buffer is changed. If so, we will need
6334 * to split the current window or data could be lost.
6335 * We don't need to check if the 'hidden' option is set, as in this
6336 * case the buffer won't be lost.
6337 */
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006338 if (!buf_hide(curbuf) && !drop_split)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006339 {
6340 ++emsg_off;
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006341 drop_split = check_changed(curbuf, CCGD_AW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006342 --emsg_off;
6343 }
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006344 if (drop_split)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006345 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006346 if (win_split(0, 0) == FAIL)
6347 return;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02006348 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006349
6350 /* When splitting the window, create a new alist. Otherwise the
6351 * existing one is overwritten. */
6352 alist_unlink(curwin->w_alist);
6353 alist_new();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006354 }
6355
6356 /*
6357 * Set up the new argument list.
6358 */
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006359 alist_set(ALIST(curwin), drop_filec, drop_filev, FALSE, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006360
6361 /*
6362 * Move to the first file.
6363 */
6364 /* Fake up a minimal "next" command for do_argfile() */
6365 vim_memset(&ea, 0, sizeof(ea));
6366 ea.cmd = (char_u *)"next";
6367 do_argfile(&ea, 0);
6368
6369 /* do_ecmd() may set need_start_insertmode, but since we never left Insert
6370 * mode that is not needed here. */
6371 need_start_insertmode = FALSE;
6372
6373 /* Restore msg_scroll, otherwise a following command may cause scrolling
6374 * unexpectedly. The screen will be redrawn by the caller, thus
6375 * msg_scroll being set by displaying a message is irrelevant. */
6376 msg_scroll = save_msg_scroll;
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006377
6378 if (drop_callback != NULL)
6379 drop_callback(drop_cookie);
6380
6381 drop_filev = NULL;
6382 drop_busy = FALSE;
6383}
6384
6385/*
6386 * Handle a file drop. The code is here because a drop is *nearly* like an
6387 * :args command, but not quite (we have a list of exact filenames, so we
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01006388 * don't want to (a) parse a command line, or (b) expand wildcards). So the
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006389 * code is very similar to :args and hence needs access to a lot of the static
6390 * functions in this file.
6391 *
6392 * The "filev" list must have been allocated using alloc(), as should each item
6393 * in the list. This function takes over responsibility for freeing the "filev"
6394 * list.
6395 */
6396 void
6397handle_drop(
6398 int filec, // the number of files dropped
6399 char_u **filev, // the list of files dropped
6400 int split, // force splitting the window
6401 void (*callback)(void *), // to be called after setting the argument
6402 // list
6403 void *cookie) // argument for "callback" (allocated)
6404{
6405 // Cannot handle recursive drops, finish the pending one.
6406 if (drop_busy)
6407 {
6408 FreeWild(filec, filev);
6409 vim_free(cookie);
6410 return;
6411 }
6412
6413 // When calling handle_drop() more than once in a row we only use the last
6414 // one.
6415 if (drop_filev != NULL)
6416 {
6417 FreeWild(drop_filec, drop_filev);
6418 vim_free(drop_cookie);
6419 }
6420
6421 drop_filec = filec;
6422 drop_filev = filev;
6423 drop_split = split;
6424 drop_callback = callback;
6425 drop_cookie = cookie;
6426
6427 // Postpone this when:
6428 // - editing the command line
6429 // - not possible to change the current buffer
6430 // - updating the screen
6431 // As it may change buffers and window structures that are in use and cause
6432 // freed memory to be used.
6433 if (text_locked() || curbuf_locked() || updating_screen)
6434 return;
6435
6436 handle_drop_internal();
6437}
6438
6439/*
6440 * To be called when text is unlocked, curbuf is unlocked or updating_screen is
6441 * reset: Handle a postponed drop.
6442 */
6443 void
6444handle_any_postponed_drop(void)
6445{
6446 if (!drop_busy && drop_filev != NULL
6447 && !text_locked() && !curbuf_locked() && !updating_screen)
6448 handle_drop_internal();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006449}
6450#endif
6451
Bram Moolenaar071d4272004-06-13 20:20:40 +00006452/*
6453 * Clear an argument list: free all file names and reset it to zero entries.
6454 */
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00006455 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006456alist_clear(alist_T *al)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006457{
6458 while (--al->al_ga.ga_len >= 0)
6459 vim_free(AARGLIST(al)[al->al_ga.ga_len].ae_fname);
6460 ga_clear(&al->al_ga);
6461}
6462
6463/*
6464 * Init an argument list.
6465 */
6466 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006467alist_init(alist_T *al)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006468{
6469 ga_init2(&al->al_ga, (int)sizeof(aentry_T), 5);
6470}
6471
Bram Moolenaar071d4272004-06-13 20:20:40 +00006472/*
6473 * Remove a reference from an argument list.
6474 * Ignored when the argument list is the global one.
6475 * If the argument list is no longer used by any window, free it.
6476 */
6477 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006478alist_unlink(alist_T *al)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006479{
6480 if (al != &global_alist && --al->al_refcount <= 0)
6481 {
6482 alist_clear(al);
6483 vim_free(al);
6484 }
6485}
6486
Bram Moolenaar071d4272004-06-13 20:20:40 +00006487/*
6488 * Create a new argument list and use it for the current window.
6489 */
6490 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006491alist_new(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006492{
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006493 curwin->w_alist = ALLOC_ONE(alist_T);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006494 if (curwin->w_alist == NULL)
6495 {
6496 curwin->w_alist = &global_alist;
6497 ++global_alist.al_refcount;
6498 }
6499 else
6500 {
6501 curwin->w_alist->al_refcount = 1;
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02006502 curwin->w_alist->id = ++max_alist_id;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006503 alist_init(curwin->w_alist);
6504 }
6505}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006506
Bram Moolenaara06ecab2016-07-16 14:47:36 +02006507#if !defined(UNIX) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006508/*
6509 * Expand the file names in the global argument list.
Bram Moolenaar86b68352004-12-27 21:59:20 +00006510 * If "fnum_list" is not NULL, use "fnum_list[fnum_len]" as a list of buffer
6511 * numbers to be re-used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006512 */
6513 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006514alist_expand(int *fnum_list, int fnum_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006515{
6516 char_u **old_arg_files;
Bram Moolenaar86b68352004-12-27 21:59:20 +00006517 int old_arg_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006518 char_u **new_arg_files;
6519 int new_arg_file_count;
6520 char_u *save_p_su = p_su;
6521 int i;
6522
6523 /* Don't use 'suffixes' here. This should work like the shell did the
6524 * expansion. Also, the vimrc file isn't read yet, thus the user
6525 * can't set the options. */
6526 p_su = empty_option;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006527 old_arg_files = ALLOC_MULT(char_u *, GARGCOUNT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006528 if (old_arg_files != NULL)
6529 {
6530 for (i = 0; i < GARGCOUNT; ++i)
Bram Moolenaar86b68352004-12-27 21:59:20 +00006531 old_arg_files[i] = vim_strsave(GARGLIST[i].ae_fname);
6532 old_arg_count = GARGCOUNT;
6533 if (expand_wildcards(old_arg_count, old_arg_files,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006534 &new_arg_file_count, &new_arg_files,
Bram Moolenaarb5609832011-07-20 15:04:58 +02006535 EW_FILE|EW_NOTFOUND|EW_ADDSLASH|EW_NOERROR) == OK
Bram Moolenaar071d4272004-06-13 20:20:40 +00006536 && new_arg_file_count > 0)
6537 {
Bram Moolenaar86b68352004-12-27 21:59:20 +00006538 alist_set(&global_alist, new_arg_file_count, new_arg_files,
6539 TRUE, fnum_list, fnum_len);
6540 FreeWild(old_arg_count, old_arg_files);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006541 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006542 }
6543 p_su = save_p_su;
6544}
6545#endif
6546
6547/*
6548 * Set the argument list for the current window.
6549 * Takes over the allocated files[] and the allocated fnames in it.
6550 */
6551 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006552alist_set(
6553 alist_T *al,
6554 int count,
6555 char_u **files,
6556 int use_curbuf,
6557 int *fnum_list,
6558 int fnum_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006559{
6560 int i;
Bram Moolenaar9e33efd2018-02-09 17:50:28 +01006561 static int recursive = 0;
6562
6563 if (recursive)
6564 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006565 emsg(_(e_au_recursive));
Bram Moolenaar9e33efd2018-02-09 17:50:28 +01006566 return;
6567 }
6568 ++recursive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006569
6570 alist_clear(al);
6571 if (ga_grow(&al->al_ga, count) == OK)
6572 {
6573 for (i = 0; i < count; ++i)
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00006574 {
6575 if (got_int)
6576 {
6577 /* When adding many buffers this can take a long time. Allow
6578 * interrupting here. */
6579 while (i < count)
6580 vim_free(files[i++]);
6581 break;
6582 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00006583
6584 /* May set buffer name of a buffer previously used for the
6585 * argument list, so that it's re-used by alist_add. */
6586 if (fnum_list != NULL && i < fnum_len)
6587 buf_set_name(fnum_list[i], files[i]);
6588
Bram Moolenaar071d4272004-06-13 20:20:40 +00006589 alist_add(al, files[i], use_curbuf ? 2 : 1);
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00006590 ui_breakcheck();
6591 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006592 vim_free(files);
6593 }
6594 else
6595 FreeWild(count, files);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006596 if (al == &global_alist)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006597 arg_had_last = FALSE;
Bram Moolenaar9e33efd2018-02-09 17:50:28 +01006598
6599 --recursive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006600}
6601
6602/*
6603 * Add file "fname" to argument list "al".
6604 * "fname" must have been allocated and "al" must have been checked for room.
6605 */
6606 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006607alist_add(
6608 alist_T *al,
6609 char_u *fname,
6610 int set_fnum) /* 1: set buffer number; 2: re-use curbuf */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006611{
6612 if (fname == NULL) /* don't add NULL file names */
6613 return;
6614#ifdef BACKSLASH_IN_FILENAME
6615 slash_adjust(fname);
6616#endif
6617 AARGLIST(al)[al->al_ga.ga_len].ae_fname = fname;
6618 if (set_fnum > 0)
6619 AARGLIST(al)[al->al_ga.ga_len].ae_fnum =
6620 buflist_add(fname, BLN_LISTED | (set_fnum == 2 ? BLN_CURBUF : 0));
6621 ++al->al_ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006622}
6623
6624#if defined(BACKSLASH_IN_FILENAME) || defined(PROTO)
6625/*
6626 * Adjust slashes in file names. Called after 'shellslash' was set.
6627 */
6628 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006629alist_slash_adjust(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006630{
6631 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006632 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00006633 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006634
6635 for (i = 0; i < GARGCOUNT; ++i)
6636 if (GARGLIST[i].ae_fname != NULL)
6637 slash_adjust(GARGLIST[i].ae_fname);
Bram Moolenaarf740b292006-02-16 22:11:02 +00006638 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006639 if (wp->w_alist != &global_alist)
6640 for (i = 0; i < WARGCOUNT(wp); ++i)
6641 if (WARGLIST(wp)[i].ae_fname != NULL)
6642 slash_adjust(WARGLIST(wp)[i].ae_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006643}
6644#endif
6645
6646/*
6647 * ":preserve".
6648 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006649 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006650ex_preserve(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006651{
Bram Moolenaar4399ef42005-02-12 14:29:27 +00006652 curbuf->b_flags |= BF_PRESERVED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006653 ml_preserve(curbuf, TRUE);
6654}
6655
6656/*
6657 * ":recover".
6658 */
6659 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006660ex_recover(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006661{
6662 /* Set recoverymode right away to avoid the ATTENTION prompt. */
6663 recoverymode = TRUE;
Bram Moolenaar45d3b142013-11-09 03:31:51 +01006664 if (!check_changed(curbuf, (p_awa ? CCGD_AW : 0)
6665 | CCGD_MULTWIN
6666 | (eap->forceit ? CCGD_FORCEIT : 0)
6667 | CCGD_EXCMD)
6668
Bram Moolenaar071d4272004-06-13 20:20:40 +00006669 && (*eap->arg == NUL
6670 || setfname(curbuf, eap->arg, NULL, TRUE) == OK))
Bram Moolenaar99499b12019-05-23 21:35:48 +02006671 ml_recover(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006672 recoverymode = FALSE;
6673}
6674
6675/*
6676 * Command modifier used in a wrong way.
6677 */
6678 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006679ex_wrongmodifier(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006680{
6681 eap->errmsg = e_invcmd;
6682}
6683
Bram Moolenaar071d4272004-06-13 20:20:40 +00006684/*
6685 * :sview [+command] file split window with new file, read-only
6686 * :split [[+command] file] split window with current or new file
6687 * :vsplit [[+command] file] split window vertically with current or new file
6688 * :new [[+command] file] split window with no or new file
6689 * :vnew [[+command] file] split vertically window with no or new file
6690 * :sfind [+command] file split window with file in 'path'
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006691 *
6692 * :tabedit open new Tab page with empty window
6693 * :tabedit [+command] file open new Tab page and edit "file"
6694 * :tabnew [[+command] file] just like :tabedit
6695 * :tabfind [+command] file open new Tab page and find "file"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006696 */
6697 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006698ex_splitview(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006699{
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006700 win_T *old_curwin = curwin;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006701#if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006702 char_u *fname = NULL;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006703#endif
6704#ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00006705 int browse_flag = cmdmod.browse;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006706#endif
Bram Moolenaar39902a02018-06-21 22:10:08 +02006707 int use_tab = eap->cmdidx == CMD_tabedit
6708 || eap->cmdidx == CMD_tabfind
6709 || eap->cmdidx == CMD_tabnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006710
Bram Moolenaar8cdbd5b2019-06-16 15:50:45 +02006711 if (ERROR_IF_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02006712 return;
6713
Bram Moolenaar4033c552017-09-16 20:54:51 +02006714#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00006715 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006716#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006717
Bram Moolenaar4033c552017-09-16 20:54:51 +02006718#ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00006719 /* A ":split" in the quickfix window works like ":new". Don't want two
Bram Moolenaar05bb9532008-07-04 09:44:11 +00006720 * quickfix windows. But it's OK when doing ":tab split". */
6721 if (bt_quickfix(curbuf) && cmdmod.tab == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006722 {
6723 if (eap->cmdidx == CMD_split)
6724 eap->cmdidx = CMD_new;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006725 if (eap->cmdidx == CMD_vsplit)
6726 eap->cmdidx = CMD_vnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006727 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02006728#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006729
Bram Moolenaar4033c552017-09-16 20:54:51 +02006730#ifdef FEAT_SEARCHPATH
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006731 if (eap->cmdidx == CMD_sfind || eap->cmdidx == CMD_tabfind)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006732 {
6733 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg),
6734 FNAME_MESS, TRUE, curbuf->b_ffname);
6735 if (fname == NULL)
6736 goto theend;
6737 eap->arg = fname;
6738 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006739# ifdef FEAT_BROWSE
Bram Moolenaar4033c552017-09-16 20:54:51 +02006740 else
6741# endif
6742#endif
6743#ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00006744 if (cmdmod.browse
Bram Moolenaar071d4272004-06-13 20:20:40 +00006745 && eap->cmdidx != CMD_vnew
Bram Moolenaar071d4272004-06-13 20:20:40 +00006746 && eap->cmdidx != CMD_new)
6747 {
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006748 if (
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006749# ifdef FEAT_GUI
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006750 !gui.in_use &&
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006751# endif
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006752 au_has_group((char_u *)"FileExplorer"))
6753 {
6754 /* No browsing supported but we do have the file explorer:
6755 * Edit the directory. */
6756 if (*eap->arg == NUL || !mch_isdir(eap->arg))
6757 eap->arg = (char_u *)".";
6758 }
6759 else
6760 {
Bram Moolenaar39902a02018-06-21 22:10:08 +02006761 fname = do_browse(0, (char_u *)(use_tab
6762 ? _("Edit File in new tab page")
6763 : _("Edit File in new window")),
Bram Moolenaar071d4272004-06-13 20:20:40 +00006764 eap->arg, NULL, NULL, NULL, curbuf);
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006765 if (fname == NULL)
6766 goto theend;
6767 eap->arg = fname;
6768 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006769 }
6770 cmdmod.browse = FALSE; /* Don't browse again in do_ecmd(). */
Bram Moolenaar4033c552017-09-16 20:54:51 +02006771#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006772
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006773 /*
6774 * Either open new tab page or split the window.
6775 */
Bram Moolenaar39902a02018-06-21 22:10:08 +02006776 if (use_tab)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006777 {
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006778 if (win_new_tabpage(cmdmod.tab != 0 ? cmdmod.tab
6779 : eap->addr_count == 0 ? 0
6780 : (int)eap->line2 + 1) != FAIL)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006781 {
Bram Moolenaard2b66012008-01-09 19:30:36 +00006782 do_exedit(eap, old_curwin);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006783
6784 /* set the alternate buffer for the window we came from */
6785 if (curwin != old_curwin
6786 && win_valid(old_curwin)
6787 && old_curwin->w_buffer != curbuf
6788 && !cmdmod.keepalt)
6789 old_curwin->w_alt_fnum = curbuf->b_fnum;
6790 }
6791 }
6792 else if (win_split(eap->addr_count > 0 ? (int)eap->line2 : 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006793 *eap->cmd == 'v' ? WSP_VERT : 0) != FAIL)
6794 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006795 /* Reset 'scrollbind' when editing another file, but keep it when
6796 * doing ":split" without arguments. */
6797 if (*eap->arg != NUL
Bram Moolenaar8a3bb562018-03-04 20:14:14 +01006798# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00006799 || cmdmod.browse
Bram Moolenaar8a3bb562018-03-04 20:14:14 +01006800# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006801 )
Bram Moolenaar3368ea22010-09-21 16:56:35 +02006802 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006803 else
6804 do_check_scrollbind(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006805 do_exedit(eap, old_curwin);
6806 }
6807
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006808# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00006809 cmdmod.browse = browse_flag;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006810# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006811
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006812# if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006813theend:
6814 vim_free(fname);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006815# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006816}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006817
6818/*
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006819 * Open a new tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006820 */
6821 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006822tabpage_new(void)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006823{
6824 exarg_T ea;
6825
6826 vim_memset(&ea, 0, sizeof(ea));
6827 ea.cmdidx = CMD_tabnew;
6828 ea.cmd = (char_u *)"tabn";
6829 ea.arg = (char_u *)"";
6830 ex_splitview(&ea);
6831}
6832
6833/*
6834 * :tabnext command
6835 */
6836 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006837ex_tabnext(exarg_T *eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006838{
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006839 int tab_number;
6840
Bram Moolenaar8cdbd5b2019-06-16 15:50:45 +02006841 if (ERROR_IF_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02006842 return;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006843 switch (eap->cmdidx)
6844 {
6845 case CMD_tabfirst:
6846 case CMD_tabrewind:
6847 goto_tabpage(1);
6848 break;
6849 case CMD_tablast:
6850 goto_tabpage(9999);
6851 break;
6852 case CMD_tabprevious:
6853 case CMD_tabNext:
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006854 if (eap->arg && *eap->arg != NUL)
6855 {
6856 char_u *p = eap->arg;
6857 char_u *p_save = p;
6858
6859 tab_number = getdigits(&p);
6860 if (p == p_save || *p_save == '-' || *p != NUL
6861 || tab_number == 0)
6862 {
6863 /* No numbers as argument. */
6864 eap->errmsg = e_invarg;
6865 return;
6866 }
6867 }
6868 else
6869 {
6870 if (eap->addr_count == 0)
6871 tab_number = 1;
6872 else
6873 {
6874 tab_number = eap->line2;
6875 if (tab_number < 1)
6876 {
6877 eap->errmsg = e_invrange;
6878 return;
6879 }
6880 }
6881 }
6882 goto_tabpage(-tab_number);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006883 break;
6884 default: /* CMD_tabnext */
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006885 tab_number = get_tabpage_arg(eap);
6886 if (eap->errmsg == NULL)
6887 goto_tabpage(tab_number);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006888 break;
6889 }
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006890}
6891
6892/*
6893 * :tabmove command
6894 */
6895 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006896ex_tabmove(exarg_T *eap)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006897{
Bram Moolenaar40ce3a42015-04-21 18:08:39 +02006898 int tab_number;
Bram Moolenaar8cb8dca2012-07-06 18:27:39 +02006899
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006900 tab_number = get_tabpage_arg(eap);
6901 if (eap->errmsg == NULL)
6902 tabpage_move(tab_number);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006903}
6904
6905/*
6906 * :tabs command: List tabs and their contents.
6907 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006908 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006909ex_tabs(exarg_T *eap UNUSED)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006910{
6911 tabpage_T *tp;
6912 win_T *wp;
6913 int tabcount = 1;
6914
6915 msg_start();
6916 msg_scroll = TRUE;
6917 for (tp = first_tabpage; tp != NULL && !got_int; tp = tp->tp_next)
6918 {
6919 msg_putchar('\n');
6920 vim_snprintf((char *)IObuff, IOSIZE, _("Tab page %d"), tabcount++);
Bram Moolenaar8820b482017-03-16 17:23:31 +01006921 msg_outtrans_attr(IObuff, HL_ATTR(HLF_T));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006922 out_flush(); /* output one line at a time */
6923 ui_breakcheck();
6924
Bram Moolenaar030f0df2006-02-21 22:02:53 +00006925 if (tp == curtab)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006926 wp = firstwin;
6927 else
6928 wp = tp->tp_firstwin;
6929 for ( ; wp != NULL && !got_int; wp = wp->w_next)
6930 {
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006931 msg_putchar('\n');
6932 msg_putchar(wp == curwin ? '>' : ' ');
6933 msg_putchar(' ');
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006934 msg_putchar(bufIsChanged(wp->w_buffer) ? '+' : ' ');
6935 msg_putchar(' ');
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006936 if (buf_spname(wp->w_buffer) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02006937 vim_strncpy(IObuff, buf_spname(wp->w_buffer), IOSIZE - 1);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006938 else
6939 home_replace(wp->w_buffer, wp->w_buffer->b_fname,
6940 IObuff, IOSIZE, TRUE);
6941 msg_outtrans(IObuff);
6942 out_flush(); /* output one line at a time */
6943 ui_breakcheck();
6944 }
6945 }
6946}
6947
Bram Moolenaar071d4272004-06-13 20:20:40 +00006948/*
6949 * ":mode": Set screen mode.
6950 * If no argument given, just get the screen size and redraw.
6951 */
6952 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006953ex_mode(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006954{
6955 if (*eap->arg == NUL)
6956 shell_resized();
6957 else
6958 mch_screenmode(eap->arg);
6959}
6960
Bram Moolenaar071d4272004-06-13 20:20:40 +00006961/*
6962 * ":resize".
6963 * set, increment or decrement current window height
6964 */
6965 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006966ex_resize(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006967{
6968 int n;
6969 win_T *wp = curwin;
6970
6971 if (eap->addr_count > 0)
6972 {
6973 n = eap->line2;
6974 for (wp = firstwin; wp->w_next != NULL && --n > 0; wp = wp->w_next)
6975 ;
6976 }
6977
Bram Moolenaar44a2f922016-03-19 22:11:51 +01006978# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00006979 need_mouse_correct = TRUE;
Bram Moolenaar44a2f922016-03-19 22:11:51 +01006980# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006981 n = atol((char *)eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006982 if (cmdmod.split & WSP_VERT)
6983 {
6984 if (*eap->arg == '-' || *eap->arg == '+')
Bram Moolenaar02631462017-09-22 15:20:32 +02006985 n += curwin->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006986 else if (n == 0 && eap->arg[0] == NUL) /* default is very wide */
6987 n = 9999;
6988 win_setwidth_win((int)n, wp);
6989 }
6990 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006991 {
6992 if (*eap->arg == '-' || *eap->arg == '+')
6993 n += curwin->w_height;
Bram Moolenaar1f2903c2017-07-23 19:51:01 +02006994 else if (n == 0 && eap->arg[0] == NUL) /* default is very high */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006995 n = 9999;
6996 win_setheight_win((int)n, wp);
6997 }
6998}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006999
7000/*
7001 * ":find [+command] <file>" command.
7002 */
7003 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007004ex_find(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007005{
7006#ifdef FEAT_SEARCHPATH
7007 char_u *fname;
7008 int count;
7009
7010 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg), FNAME_MESS,
7011 TRUE, curbuf->b_ffname);
7012 if (eap->addr_count > 0)
7013 {
7014 /* Repeat finding the file "count" times. This matters when it
7015 * appears several times in the path. */
7016 count = eap->line2;
7017 while (fname != NULL && --count > 0)
7018 {
7019 vim_free(fname);
7020 fname = find_file_in_path(NULL, 0, FNAME_MESS,
7021 FALSE, curbuf->b_ffname);
7022 }
7023 }
7024
7025 if (fname != NULL)
7026 {
7027 eap->arg = fname;
7028#endif
7029 do_exedit(eap, NULL);
7030#ifdef FEAT_SEARCHPATH
7031 vim_free(fname);
7032 }
7033#endif
7034}
7035
7036/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00007037 * ":open" simulation: for now just work like ":visual".
7038 */
7039 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007040ex_open(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007041{
7042 regmatch_T regmatch;
7043 char_u *p;
7044
7045 curwin->w_cursor.lnum = eap->line2;
7046 beginline(BL_SOL | BL_FIX);
7047 if (*eap->arg == '/')
7048 {
7049 /* ":open /pattern/": put cursor in column found with pattern */
7050 ++eap->arg;
7051 p = skip_regexp(eap->arg, '/', p_magic, NULL);
7052 *p = NUL;
7053 regmatch.regprog = vim_regcomp(eap->arg, p_magic ? RE_MAGIC : 0);
7054 if (regmatch.regprog != NULL)
7055 {
7056 regmatch.rm_ic = p_ic;
7057 p = ml_get_curline();
7058 if (vim_regexec(&regmatch, p, (colnr_T)0))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007059 curwin->w_cursor.col = (colnr_T)(regmatch.startp[0] - p);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007060 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007061 emsg(_(e_nomatch));
Bram Moolenaar473de612013-06-08 18:19:48 +02007062 vim_regfree(regmatch.regprog);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007063 }
7064 /* Move to the NUL, ignore any other arguments. */
7065 eap->arg += STRLEN(eap->arg);
7066 }
7067 check_cursor();
7068
7069 eap->cmdidx = CMD_visual;
7070 do_exedit(eap, NULL);
7071}
7072
7073/*
7074 * ":edit", ":badd", ":visual".
Bram Moolenaar071d4272004-06-13 20:20:40 +00007075 */
7076 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007077ex_edit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007078{
7079 do_exedit(eap, NULL);
7080}
7081
7082/*
7083 * ":edit <file>" command and alikes.
7084 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007085 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007086do_exedit(
7087 exarg_T *eap,
7088 win_T *old_curwin) /* curwin before doing a split or NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007089{
7090 int n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007091 int need_hide;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007092 int exmode_was = exmode_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007093
Bram Moolenaar8cdbd5b2019-06-16 15:50:45 +02007094 if (ERROR_IF_POPUP_WINDOW)
Bram Moolenaar815b76b2019-06-01 14:15:52 +02007095 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007096 /*
7097 * ":vi" command ends Ex mode.
7098 */
7099 if (exmode_active && (eap->cmdidx == CMD_visual
7100 || eap->cmdidx == CMD_view))
7101 {
7102 exmode_active = FALSE;
7103 if (*eap->arg == NUL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007104 {
7105 /* Special case: ":global/pat/visual\NLvi-commands" */
7106 if (global_busy)
7107 {
7108 int rd = RedrawingDisabled;
7109 int nwr = no_wait_return;
7110 int ms = msg_scroll;
7111#ifdef FEAT_GUI
7112 int he = hold_gui_events;
7113#endif
7114
7115 if (eap->nextcmd != NULL)
7116 {
7117 stuffReadbuff(eap->nextcmd);
7118 eap->nextcmd = NULL;
7119 }
7120
7121 if (exmode_was != EXMODE_VIM)
7122 settmode(TMODE_RAW);
7123 RedrawingDisabled = 0;
7124 no_wait_return = 0;
7125 need_wait_return = FALSE;
7126 msg_scroll = 0;
7127#ifdef FEAT_GUI
7128 hold_gui_events = 0;
7129#endif
7130 must_redraw = CLEAR;
7131
7132 main_loop(FALSE, TRUE);
7133
7134 RedrawingDisabled = rd;
7135 no_wait_return = nwr;
7136 msg_scroll = ms;
7137#ifdef FEAT_GUI
7138 hold_gui_events = he;
7139#endif
7140 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007141 return;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007142 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007143 }
7144
7145 if ((eap->cmdidx == CMD_new
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007146 || eap->cmdidx == CMD_tabnew
7147 || eap->cmdidx == CMD_tabedit
Bram Moolenaar4033c552017-09-16 20:54:51 +02007148 || eap->cmdidx == CMD_vnew) && *eap->arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007149 {
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007150 /* ":new" or ":tabnew" without argument: edit an new empty buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007151 setpcmark();
7152 (void)do_ecmd(0, NULL, NULL, eap, ECMD_ONE,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00007153 ECMD_HIDE + (eap->forceit ? ECMD_FORCEIT : 0),
7154 old_curwin == NULL ? curwin : NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007155 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02007156 else if ((eap->cmdidx != CMD_split && eap->cmdidx != CMD_vsplit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007157 || *eap->arg != NUL
7158#ifdef FEAT_BROWSE
7159 || cmdmod.browse
7160#endif
7161 )
7162 {
Bram Moolenaar5555acc2006-04-07 21:33:12 +00007163 /* Can't edit another file when "curbuf_lock" is set. Only ":edit"
7164 * can bring us here, others are stopped earlier. */
7165 if (*eap->arg != NUL && curbuf_locked())
7166 return;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007167
Bram Moolenaar071d4272004-06-13 20:20:40 +00007168 n = readonlymode;
7169 if (eap->cmdidx == CMD_view || eap->cmdidx == CMD_sview)
7170 readonlymode = TRUE;
7171 else if (eap->cmdidx == CMD_enew)
7172 readonlymode = FALSE; /* 'readonly' doesn't make sense in an
7173 empty buffer */
7174 setpcmark();
7175 if (do_ecmd(0, (eap->cmdidx == CMD_enew ? NULL : eap->arg),
7176 NULL, eap,
7177 /* ":edit" goes to first line if Vi compatible */
7178 (*eap->arg == NUL && eap->do_ecmd_lnum == 0
7179 && vim_strchr(p_cpo, CPO_GOTO1) != NULL)
7180 ? ECMD_ONE : eap->do_ecmd_lnum,
Bram Moolenaareb44a682017-08-03 22:44:55 +02007181 (buf_hide(curbuf) ? ECMD_HIDE : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007182 + (eap->forceit ? ECMD_FORCEIT : 0)
Bram Moolenaar7b449342014-03-25 13:03:48 +01007183 /* after a split we can use an existing buffer */
7184 + (old_curwin != NULL ? ECMD_OLDBUF : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007185 + (eap->cmdidx == CMD_badd ? ECMD_ADDBUF : 0 )
Bram Moolenaar701f7af2008-11-15 13:12:07 +00007186 , old_curwin == NULL ? curwin : NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007187 {
7188 /* Editing the file failed. If the window was split, close it. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007189 if (old_curwin != NULL)
7190 {
7191 need_hide = (curbufIsChanged() && curbuf->b_nwindows <= 1);
Bram Moolenaareb44a682017-08-03 22:44:55 +02007192 if (!need_hide || buf_hide(curbuf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007193 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007194#if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007195 cleanup_T cs;
7196
7197 /* Reset the error/interrupt/exception state here so that
7198 * aborting() returns FALSE when closing a window. */
7199 enter_cleanup(&cs);
Bram Moolenaar4033c552017-09-16 20:54:51 +02007200#endif
7201#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007202 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02007203#endif
Bram Moolenaareb44a682017-08-03 22:44:55 +02007204 win_close(curwin, !need_hide && !buf_hide(curbuf));
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007205
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007206#if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007207 /* Restore the error/interrupt/exception state if not
7208 * discarded by a new aborting error, interrupt, or
7209 * uncaught exception. */
7210 leave_cleanup(&cs);
Bram Moolenaar4033c552017-09-16 20:54:51 +02007211#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007212 }
7213 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007214 }
7215 else if (readonlymode && curbuf->b_nwindows == 1)
7216 {
7217 /* When editing an already visited buffer, 'readonly' won't be set
7218 * but the previous value is kept. With ":view" and ":sview" we
7219 * want the file to be readonly, except when another window is
7220 * editing the same buffer. */
7221 curbuf->b_p_ro = TRUE;
7222 }
7223 readonlymode = n;
7224 }
7225 else
7226 {
7227 if (eap->do_ecmd_cmd != NULL)
7228 do_cmdline_cmd(eap->do_ecmd_cmd);
7229#ifdef FEAT_TITLE
7230 n = curwin->w_arg_idx_invalid;
7231#endif
7232 check_arg_idx(curwin);
7233#ifdef FEAT_TITLE
7234 if (n != curwin->w_arg_idx_invalid)
7235 maketitle();
7236#endif
7237 }
7238
Bram Moolenaar071d4272004-06-13 20:20:40 +00007239 /*
7240 * if ":split file" worked, set alternate file name in old window to new
7241 * file
7242 */
7243 if (old_curwin != NULL
7244 && *eap->arg != NUL
7245 && curwin != old_curwin
7246 && win_valid(old_curwin)
Bram Moolenaard4755bb2004-09-02 19:12:26 +00007247 && old_curwin->w_buffer != curbuf
7248 && !cmdmod.keepalt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007249 old_curwin->w_alt_fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007250
7251 ex_no_reprint = TRUE;
7252}
7253
7254#ifndef FEAT_GUI
7255/*
7256 * ":gui" and ":gvim" when there is no GUI.
7257 */
7258 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007259ex_nogui(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007260{
7261 eap->errmsg = e_nogvim;
7262}
7263#endif
7264
Bram Moolenaar4f974752019-02-17 17:44:42 +01007265#if defined(FEAT_GUI_MSWIN) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007266 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007267ex_tearoff(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007268{
7269 gui_make_tearoff(eap->arg);
7270}
7271#endif
7272
Bram Moolenaar29a2c082018-03-05 21:06:23 +01007273#if (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK) \
7274 || defined(FEAT_TERM_POPUP_MENU)) && defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007275 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007276ex_popup(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007277{
Bram Moolenaar29a2c082018-03-05 21:06:23 +01007278# if defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK)
7279 if (gui.in_use)
7280 gui_make_popup(eap->arg, eap->forceit);
7281# ifdef FEAT_TERM_POPUP_MENU
7282 else
7283# endif
7284# endif
7285# ifdef FEAT_TERM_POPUP_MENU
7286 pum_make_popup(eap->arg, eap->forceit);
7287# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007288}
7289#endif
7290
Bram Moolenaar071d4272004-06-13 20:20:40 +00007291 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007292ex_swapname(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007293{
7294 if (curbuf->b_ml.ml_mfp == NULL || curbuf->b_ml.ml_mfp->mf_fname == NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01007295 msg(_("No swap file"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007296 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01007297 msg((char *)curbuf->b_ml.ml_mfp->mf_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007298}
7299
7300/*
7301 * ":syncbind" forces all 'scrollbind' windows to have the same relative
7302 * offset.
7303 * (1998-11-02 16:21:01 R. Edward Ralston <eralston@computer.org>)
7304 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007305 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007306ex_syncbind(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007307{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007308 win_T *wp;
Bram Moolenaardedd1b02013-12-14 13:06:17 +01007309 win_T *save_curwin = curwin;
7310 buf_T *save_curbuf = curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007311 long topline;
7312 long y;
7313 linenr_T old_linenr = curwin->w_cursor.lnum;
7314
7315 setpcmark();
7316
7317 /*
7318 * determine max topline
7319 */
7320 if (curwin->w_p_scb)
7321 {
7322 topline = curwin->w_topline;
Bram Moolenaar29323592016-07-24 22:04:11 +02007323 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007324 {
7325 if (wp->w_p_scb && wp->w_buffer)
7326 {
Bram Moolenaar375e3392019-01-31 18:26:10 +01007327 y = wp->w_buffer->b_ml.ml_line_count - get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007328 if (topline > y)
7329 topline = y;
7330 }
7331 }
7332 if (topline < 1)
7333 topline = 1;
7334 }
7335 else
7336 {
7337 topline = 1;
7338 }
7339
7340
7341 /*
Bram Moolenaardedd1b02013-12-14 13:06:17 +01007342 * Set all scrollbind windows to the same topline.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007343 */
Bram Moolenaar29323592016-07-24 22:04:11 +02007344 FOR_ALL_WINDOWS(curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007345 {
7346 if (curwin->w_p_scb)
7347 {
Bram Moolenaardedd1b02013-12-14 13:06:17 +01007348 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007349 y = topline - curwin->w_topline;
7350 if (y > 0)
7351 scrollup(y, TRUE);
7352 else
7353 scrolldown(-y, TRUE);
7354 curwin->w_scbind_pos = topline;
7355 redraw_later(VALID);
7356 cursor_correct();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007357 curwin->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007358 }
7359 }
Bram Moolenaardedd1b02013-12-14 13:06:17 +01007360 curwin = save_curwin;
7361 curbuf = save_curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007362 if (curwin->w_p_scb)
7363 {
7364 did_syncbind = TRUE;
7365 checkpcmark();
7366 if (old_linenr != curwin->w_cursor.lnum)
7367 {
7368 char_u ctrl_o[2];
7369
7370 ctrl_o[0] = Ctrl_O;
7371 ctrl_o[1] = 0;
7372 ins_typebuf(ctrl_o, REMAP_NONE, 0, TRUE, FALSE);
7373 }
7374 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007375}
7376
7377
7378 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007379ex_read(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007380{
Bram Moolenaardf177f62005-02-22 08:39:57 +00007381 int i;
7382 int empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
7383 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007384
7385 if (eap->usefilter) /* :r!cmd */
7386 do_bang(1, eap, FALSE, FALSE, TRUE);
7387 else
7388 {
7389 if (u_save(eap->line2, (linenr_T)(eap->line2 + 1)) == FAIL)
7390 return;
7391
7392#ifdef FEAT_BROWSE
7393 if (cmdmod.browse)
7394 {
7395 char_u *browseFile;
7396
Bram Moolenaar7171abe2004-10-11 10:06:20 +00007397 browseFile = do_browse(0, (char_u *)_("Append File"), eap->arg,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007398 NULL, NULL, NULL, curbuf);
7399 if (browseFile != NULL)
7400 {
7401 i = readfile(browseFile, NULL,
7402 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
7403 vim_free(browseFile);
7404 }
7405 else
7406 i = OK;
7407 }
7408 else
7409#endif
7410 if (*eap->arg == NUL)
7411 {
7412 if (check_fname() == FAIL) /* check for no file name */
7413 return;
7414 i = readfile(curbuf->b_ffname, curbuf->b_fname,
7415 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
7416 }
7417 else
7418 {
7419 if (vim_strchr(p_cpo, CPO_ALTREAD) != NULL)
7420 (void)setaltfname(eap->arg, eap->arg, (linenr_T)1);
7421 i = readfile(eap->arg, NULL,
7422 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
7423
7424 }
Bram Moolenaare13b9af2017-01-13 22:01:02 +01007425 if (i != OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007426 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007427#if defined(FEAT_EVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007428 if (!aborting())
7429#endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007430 semsg(_(e_notopen), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007431 }
7432 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00007433 {
7434 if (empty && exmode_active)
7435 {
7436 /* Delete the empty line that remains. Historically ex does
7437 * this but vi doesn't. */
7438 if (eap->line2 == 0)
7439 lnum = curbuf->b_ml.ml_line_count;
7440 else
7441 lnum = 1;
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00007442 if (*ml_get(lnum) == NUL && u_savedel(lnum, 1L) == OK)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007443 {
7444 ml_delete(lnum, FALSE);
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00007445 if (curwin->w_cursor.lnum > 1
7446 && curwin->w_cursor.lnum >= lnum)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007447 --curwin->w_cursor.lnum;
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00007448 deleted_lines_mark(lnum, 1L);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007449 }
7450 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007451 redraw_curbuf_later(VALID);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007452 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007453 }
7454}
7455
Bram Moolenaarea408852005-06-25 22:49:46 +00007456static char_u *prev_dir = NULL;
7457
7458#if defined(EXITFREE) || defined(PROTO)
7459 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007460free_cd_dir(void)
Bram Moolenaarea408852005-06-25 22:49:46 +00007461{
Bram Moolenaard23a8232018-02-10 18:45:26 +01007462 VIM_CLEAR(prev_dir);
7463 VIM_CLEAR(globaldir);
Bram Moolenaarea408852005-06-25 22:49:46 +00007464}
7465#endif
7466
Bram Moolenaarf4258302013-06-02 18:20:17 +02007467/*
7468 * Deal with the side effects of changing the current directory.
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007469 * When 'scope' is CDSCOPE_TABPAGE then this was after an ":tcd" command.
7470 * When 'scope' is CDSCOPE_WINDOW then this was after an ":lcd" command.
Bram Moolenaarf4258302013-06-02 18:20:17 +02007471 */
7472 void
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007473post_chdir(cdscope_T scope)
Bram Moolenaarf4258302013-06-02 18:20:17 +02007474{
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007475 if (scope != CDSCOPE_WINDOW)
Bram Moolenaar00aa0692019-04-27 20:37:57 +02007476 // Clear tab local directory for both :cd and :tcd
7477 VIM_CLEAR(curtab->tp_localdir);
Bram Moolenaard23a8232018-02-10 18:45:26 +01007478 VIM_CLEAR(curwin->w_localdir);
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007479 if (scope != CDSCOPE_GLOBAL)
Bram Moolenaarf4258302013-06-02 18:20:17 +02007480 {
7481 /* If still in global directory, need to remember current
7482 * directory as global directory. */
7483 if (globaldir == NULL && prev_dir != NULL)
7484 globaldir = vim_strsave(prev_dir);
7485 /* Remember this local directory for the window. */
7486 if (mch_dirname(NameBuff, MAXPATHL) == OK)
Bram Moolenaar00aa0692019-04-27 20:37:57 +02007487 {
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007488 if (scope == CDSCOPE_TABPAGE)
Bram Moolenaar00aa0692019-04-27 20:37:57 +02007489 curtab->tp_localdir = vim_strsave(NameBuff);
7490 else
7491 curwin->w_localdir = vim_strsave(NameBuff);
7492 }
Bram Moolenaarf4258302013-06-02 18:20:17 +02007493 }
7494 else
7495 {
7496 /* We are now in the global directory, no need to remember its
7497 * name. */
Bram Moolenaard23a8232018-02-10 18:45:26 +01007498 VIM_CLEAR(globaldir);
Bram Moolenaarf4258302013-06-02 18:20:17 +02007499 }
7500
7501 shorten_fnames(TRUE);
7502}
7503
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007504/*
7505 * Change directory function used by :cd/:tcd/:lcd Ex commands and the
7506 * chdir() function. If 'winlocaldir' is TRUE, then changes the window-local
7507 * directory. If 'tablocaldir' is TRUE, then changes the tab-local directory.
7508 * Otherwise changes the global directory.
7509 * Returns TRUE if the directory is successfully changed.
7510 */
7511 int
7512changedir_func(
7513 char_u *new_dir,
7514 int forceit,
7515 cdscope_T scope)
7516{
7517 char_u *tofree;
7518 int dir_differs;
7519 int retval = FALSE;
7520
7521 if (allbuf_locked())
7522 return FALSE;
7523
7524 if (vim_strchr(p_cpo, CPO_CHDIR) != NULL && curbufIsChanged() && !forceit)
7525 {
7526 emsg(_("E747: Cannot change directory, buffer is modified (add ! to override)"));
7527 return FALSE;
7528 }
7529
7530 // ":cd -": Change to previous directory
7531 if (STRCMP(new_dir, "-") == 0)
7532 {
7533 if (prev_dir == NULL)
7534 {
7535 emsg(_("E186: No previous directory"));
7536 return FALSE;
7537 }
7538 new_dir = prev_dir;
7539 }
7540
7541 // Save current directory for next ":cd -"
7542 tofree = prev_dir;
7543 if (mch_dirname(NameBuff, MAXPATHL) == OK)
7544 prev_dir = vim_strsave(NameBuff);
7545 else
7546 prev_dir = NULL;
7547
7548#if defined(UNIX) || defined(VMS)
7549 // for UNIX ":cd" means: go to home directory
7550 if (*new_dir == NUL)
7551 {
7552 // use NameBuff for home directory name
7553# ifdef VMS
7554 char_u *p;
7555
7556 p = mch_getenv((char_u *)"SYS$LOGIN");
7557 if (p == NULL || *p == NUL) // empty is the same as not set
7558 NameBuff[0] = NUL;
7559 else
7560 vim_strncpy(NameBuff, p, MAXPATHL - 1);
7561# else
7562 expand_env((char_u *)"$HOME", NameBuff, MAXPATHL);
7563# endif
7564 new_dir = NameBuff;
7565 }
7566#endif
7567 dir_differs = new_dir == NULL || prev_dir == NULL
7568 || pathcmp((char *)prev_dir, (char *)new_dir, -1) != 0;
7569 if (new_dir == NULL || (dir_differs && vim_chdir(new_dir)))
7570 emsg(_(e_failed));
7571 else
7572 {
7573 char_u *acmd_fname;
7574
7575 post_chdir(scope);
7576
7577 if (dir_differs)
7578 {
7579 if (scope == CDSCOPE_WINDOW)
7580 acmd_fname = (char_u *)"window";
7581 else if (scope == CDSCOPE_TABPAGE)
7582 acmd_fname = (char_u *)"tabpage";
7583 else
7584 acmd_fname = (char_u *)"global";
7585 apply_autocmds(EVENT_DIRCHANGED, acmd_fname, new_dir, FALSE,
7586 curbuf);
7587 }
7588 retval = TRUE;
7589 }
7590 vim_free(tofree);
7591
7592 return retval;
7593}
Bram Moolenaarea408852005-06-25 22:49:46 +00007594
Bram Moolenaar071d4272004-06-13 20:20:40 +00007595/*
Bram Moolenaar00aa0692019-04-27 20:37:57 +02007596 * ":cd", ":tcd", ":lcd", ":chdir" ":tchdir" and ":lchdir".
Bram Moolenaar071d4272004-06-13 20:20:40 +00007597 */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00007598 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007599ex_cd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007600{
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01007601 char_u *new_dir;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007602
7603 new_dir = eap->arg;
7604#if !defined(UNIX) && !defined(VMS)
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007605 // for non-UNIX ":cd" means: print current directory
Bram Moolenaar071d4272004-06-13 20:20:40 +00007606 if (*new_dir == NUL)
7607 ex_pwd(NULL);
7608 else
7609#endif
7610 {
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007611 cdscope_T scope = CDSCOPE_GLOBAL;
7612
7613 if (eap->cmdidx == CMD_lcd || eap->cmdidx == CMD_lchdir)
7614 scope = CDSCOPE_WINDOW;
7615 else if (eap->cmdidx == CMD_tcd || eap->cmdidx == CMD_tchdir)
7616 scope = CDSCOPE_TABPAGE;
7617
7618 if (changedir_func(new_dir, eap->forceit, scope))
Bram Moolenaardf177f62005-02-22 08:39:57 +00007619 {
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007620 // Echo the new current directory if the command was typed.
Bram Moolenaarfcfbc672009-07-09 18:13:49 +00007621 if (KeyTyped || p_verbose >= 5)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007622 ex_pwd(eap);
7623 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007624 }
7625}
7626
7627/*
7628 * ":pwd".
7629 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007630 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007631ex_pwd(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007632{
7633 if (mch_dirname(NameBuff, MAXPATHL) == OK)
7634 {
7635#ifdef BACKSLASH_IN_FILENAME
7636 slash_adjust(NameBuff);
7637#endif
Bram Moolenaar32526b32019-01-19 17:43:09 +01007638 msg((char *)NameBuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007639 }
7640 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007641 emsg(_("E187: Unknown"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007642}
7643
7644/*
7645 * ":=".
7646 */
7647 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007648ex_equal(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007649{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007650 smsg("%ld", (long)eap->line2);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007651 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007652}
7653
7654 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007655ex_sleep(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007656{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007657 int n;
7658 long len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007659
7660 if (cursor_valid())
7661 {
7662 n = W_WINROW(curwin) + curwin->w_wrow - msg_scrolled;
7663 if (n >= 0)
Bram Moolenaar53f81742017-09-22 14:35:51 +02007664 windgoto((int)n, curwin->w_wincol + curwin->w_wcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007665 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007666
7667 len = eap->line2;
7668 switch (*eap->arg)
7669 {
7670 case 'm': break;
7671 case NUL: len *= 1000L; break;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007672 default: semsg(_(e_invarg2), eap->arg); return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007673 }
7674 do_sleep(len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007675}
7676
7677/*
7678 * Sleep for "msec" milliseconds, but keep checking for a CTRL-C every second.
7679 */
7680 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007681do_sleep(long msec)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007682{
7683 long done;
Bram Moolenaar975b5272016-03-15 23:10:59 +01007684 long wait_now;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007685
7686 cursor_on();
Bram Moolenaarf45d7472018-09-30 18:22:26 +02007687 out_flush_cursor(FALSE, FALSE);
Bram Moolenaar975b5272016-03-15 23:10:59 +01007688 for (done = 0; !got_int && done < msec; done += wait_now)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007689 {
Bram Moolenaar975b5272016-03-15 23:10:59 +01007690 wait_now = msec - done > 1000L ? 1000L : msec - done;
7691#ifdef FEAT_TIMERS
7692 {
7693 long due_time = check_due_timer();
7694
7695 if (due_time > 0 && due_time < wait_now)
7696 wait_now = due_time;
7697 }
7698#endif
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02007699#ifdef FEAT_JOB_CHANNEL
7700 if (has_any_channel() && wait_now > 100L)
7701 wait_now = 100L;
7702#endif
Bram Moolenaar975b5272016-03-15 23:10:59 +01007703 ui_delay(wait_now, TRUE);
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02007704#ifdef FEAT_JOB_CHANNEL
7705 if (has_any_channel())
7706 ui_breakcheck_force(TRUE);
7707 else
7708#endif
7709 ui_breakcheck();
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007710#ifdef MESSAGE_QUEUE
7711 /* Process the netbeans and clientserver messages that may have been
7712 * received in the call to ui_breakcheck() when the GUI is in use. This
7713 * may occur when running a test case. */
7714 parse_queued_messages();
Bram Moolenaare3cc6d42011-10-20 21:58:34 +02007715#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007716 }
Bram Moolenaar1ebff3d2018-07-07 16:18:13 +02007717
7718 // If CTRL-C was typed to interrupt the sleep, drop the CTRL-C from the
7719 // input buffer, otherwise a following call to input() fails.
7720 if (got_int)
7721 (void)vpeekc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007722}
7723
Bram Moolenaar071d4272004-06-13 20:20:40 +00007724/*
7725 * ":winsize" command (obsolete).
7726 */
7727 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007728ex_winsize(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007729{
7730 int w, h;
7731 char_u *arg = eap->arg;
7732 char_u *p;
7733
7734 w = getdigits(&arg);
7735 arg = skipwhite(arg);
7736 p = arg;
7737 h = getdigits(&arg);
7738 if (*p != NUL && *arg == NUL)
7739 set_shellsize(w, h, TRUE);
7740 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007741 emsg(_("E465: :winsize requires two number arguments"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007742}
7743
Bram Moolenaar071d4272004-06-13 20:20:40 +00007744 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007745ex_wincmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007746{
7747 int xchar = NUL;
7748 char_u *p;
7749
7750 if (*eap->arg == 'g' || *eap->arg == Ctrl_G)
7751 {
7752 /* CTRL-W g and CTRL-W CTRL-G have an extra command character */
7753 if (eap->arg[1] == NUL)
7754 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007755 emsg(_(e_invarg));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007756 return;
7757 }
7758 xchar = eap->arg[1];
7759 p = eap->arg + 2;
7760 }
7761 else
7762 p = eap->arg + 1;
7763
7764 eap->nextcmd = check_nextcmd(p);
7765 p = skipwhite(p);
7766 if (*p != NUL && *p != '"' && eap->nextcmd == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007767 emsg(_(e_invarg));
Bram Moolenaar12bde492011-06-13 01:19:56 +02007768 else if (!eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007769 {
7770 /* Pass flags on for ":vertical wincmd ]". */
7771 postponed_split_flags = cmdmod.split;
Bram Moolenaard326ce82007-03-11 14:48:29 +00007772 postponed_split_tab = cmdmod.tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007773 do_window(*eap->arg, eap->addr_count > 0 ? eap->line2 : 0L, xchar);
7774 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00007775 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007776 }
7777}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007778
Bram Moolenaar843ee412004-06-30 16:16:41 +00007779#if defined(FEAT_GUI) || defined(UNIX) || defined(VMS) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007780/*
7781 * ":winpos".
7782 */
7783 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007784ex_winpos(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007785{
7786 int x, y;
7787 char_u *arg = eap->arg;
7788 char_u *p;
7789
7790 if (*arg == NUL)
7791 {
Bram Moolenaar843ee412004-06-30 16:16:41 +00007792# if defined(FEAT_GUI) || defined(MSWIN)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007793# ifdef VIMDLL
7794 if (gui.in_use ? gui_mch_get_winpos(&x, &y) != FAIL :
7795 mch_get_winpos(&x, &y) != FAIL)
7796# elif defined(FEAT_GUI)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007797 if (gui.in_use && gui_mch_get_winpos(&x, &y) != FAIL)
Bram Moolenaar843ee412004-06-30 16:16:41 +00007798# else
7799 if (mch_get_winpos(&x, &y) != FAIL)
7800# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007801 {
7802 sprintf((char *)IObuff, _("Window position: X %d, Y %d"), x, y);
Bram Moolenaar32526b32019-01-19 17:43:09 +01007803 msg((char *)IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007804 }
7805 else
7806# endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007807 emsg(_("E188: Obtaining window position not implemented for this platform"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007808 }
7809 else
7810 {
7811 x = getdigits(&arg);
7812 arg = skipwhite(arg);
7813 p = arg;
7814 y = getdigits(&arg);
7815 if (*p == NUL || *arg != NUL)
7816 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007817 emsg(_("E466: :winpos requires two number arguments"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007818 return;
7819 }
7820# ifdef FEAT_GUI
7821 if (gui.in_use)
7822 gui_mch_set_winpos(x, y);
7823 else if (gui.starting)
7824 {
7825 /* Remember the coordinates for when the window is opened. */
7826 gui_win_x = x;
7827 gui_win_y = y;
7828 }
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007829# if defined(HAVE_TGETENT) || defined(VIMDLL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007830 else
7831# endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007832# endif
7833# if defined(MSWIN) && (!defined(FEAT_GUI) || defined(VIMDLL))
Bram Moolenaar843ee412004-06-30 16:16:41 +00007834 mch_set_winpos(x, y);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007835# endif
7836# ifdef HAVE_TGETENT
7837 if (*T_CWP)
7838 term_set_winpos(x, y);
7839# endif
7840 }
7841}
7842#endif
7843
7844/*
7845 * Handle command that work like operators: ":delete", ":yank", ":>" and ":<".
7846 */
7847 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007848ex_operators(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007849{
7850 oparg_T oa;
7851
7852 clear_oparg(&oa);
7853 oa.regname = eap->regname;
7854 oa.start.lnum = eap->line1;
7855 oa.end.lnum = eap->line2;
7856 oa.line_count = eap->line2 - eap->line1 + 1;
7857 oa.motion_type = MLINE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007858 virtual_op = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007859 if (eap->cmdidx != CMD_yank) /* position cursor for undo */
7860 {
7861 setpcmark();
7862 curwin->w_cursor.lnum = eap->line1;
7863 beginline(BL_SOL | BL_FIX);
7864 }
7865
Bram Moolenaard07c6e12013-11-21 14:21:40 +01007866 if (VIsual_active)
7867 end_visual_mode();
Bram Moolenaard07c6e12013-11-21 14:21:40 +01007868
Bram Moolenaar071d4272004-06-13 20:20:40 +00007869 switch (eap->cmdidx)
7870 {
7871 case CMD_delete:
7872 oa.op_type = OP_DELETE;
7873 op_delete(&oa);
7874 break;
7875
7876 case CMD_yank:
7877 oa.op_type = OP_YANK;
7878 (void)op_yank(&oa, FALSE, TRUE);
7879 break;
7880
7881 default: /* CMD_rshift or CMD_lshift */
Bram Moolenaar6e707362013-06-14 19:15:58 +02007882 if (
Bram Moolenaar071d4272004-06-13 20:20:40 +00007883#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6e707362013-06-14 19:15:58 +02007884 (eap->cmdidx == CMD_rshift) ^ curwin->w_p_rl
7885#else
7886 eap->cmdidx == CMD_rshift
Bram Moolenaar071d4272004-06-13 20:20:40 +00007887#endif
Bram Moolenaar6e707362013-06-14 19:15:58 +02007888 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00007889 oa.op_type = OP_RSHIFT;
7890 else
7891 oa.op_type = OP_LSHIFT;
7892 op_shift(&oa, FALSE, eap->amount);
7893 break;
7894 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007895 virtual_op = MAYBE;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007896 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007897}
7898
7899/*
7900 * ":put".
7901 */
7902 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007903ex_put(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007904{
7905 /* ":0put" works like ":1put!". */
7906 if (eap->line2 == 0)
7907 {
7908 eap->line2 = 1;
7909 eap->forceit = TRUE;
7910 }
7911 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007912 do_put(eap->regname, eap->forceit ? BACKWARD : FORWARD, 1L,
7913 PUT_LINE|PUT_CURSLINE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007914}
7915
7916/*
7917 * Handle ":copy" and ":move".
7918 */
7919 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007920ex_copymove(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007921{
7922 long n;
7923
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02007924 n = get_address(eap, &eap->arg, eap->addr_type, FALSE, FALSE, FALSE, 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007925 if (eap->arg == NULL) /* error detected */
7926 {
7927 eap->nextcmd = NULL;
7928 return;
7929 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00007930 get_flags(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007931
7932 /*
7933 * move or copy lines from 'eap->line1'-'eap->line2' to below line 'n'
7934 */
7935 if (n == MAXLNUM || n < 0 || n > curbuf->b_ml.ml_line_count)
7936 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007937 emsg(_(e_invaddr));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007938 return;
7939 }
7940
7941 if (eap->cmdidx == CMD_move)
7942 {
7943 if (do_move(eap->line1, eap->line2, n) == FAIL)
7944 return;
7945 }
7946 else
7947 ex_copy(eap->line1, eap->line2, n);
7948 u_clearline();
7949 beginline(BL_SOL | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007950 ex_may_print(eap);
7951}
7952
7953/*
7954 * Print the current line if flags were given to the Ex command.
7955 */
Bram Moolenaarfd3fe982014-04-01 17:49:44 +02007956 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007957ex_may_print(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007958{
7959 if (eap->flags != 0)
7960 {
7961 print_line(curwin->w_cursor.lnum, (eap->flags & EXFLAG_NR),
7962 (eap->flags & EXFLAG_LIST));
7963 ex_no_reprint = TRUE;
7964 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007965}
7966
7967/*
7968 * ":smagic" and ":snomagic".
7969 */
7970 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007971ex_submagic(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007972{
7973 int magic_save = p_magic;
7974
7975 p_magic = (eap->cmdidx == CMD_smagic);
7976 do_sub(eap);
7977 p_magic = magic_save;
7978}
7979
7980/*
7981 * ":join".
7982 */
7983 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007984ex_join(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007985{
7986 curwin->w_cursor.lnum = eap->line1;
7987 if (eap->line1 == eap->line2)
7988 {
7989 if (eap->addr_count >= 2) /* :2,2join does nothing */
7990 return;
7991 if (eap->line2 == curbuf->b_ml.ml_line_count)
7992 {
7993 beep_flush();
7994 return;
7995 }
7996 ++eap->line2;
7997 }
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02007998 (void)do_join(eap->line2 - eap->line1 + 1, !eap->forceit, TRUE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007999 beginline(BL_WHITE | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008000 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008001}
8002
8003/*
8004 * ":[addr]@r" or ":[addr]*r": execute register
8005 */
8006 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008007ex_at(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008008{
8009 int c;
Bram Moolenaar60462872009-11-03 11:40:19 +00008010 int prev_len = typebuf.tb_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008011
8012 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaar4930a762016-09-11 14:39:53 +02008013 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008014
8015#ifdef USE_ON_FLY_SCROLL
8016 dont_scroll = TRUE; /* disallow scrolling here */
8017#endif
8018
8019 /* get the register name. No name means to use the previous one */
8020 c = *eap->arg;
8021 if (c == NUL || (c == '*' && *eap->cmd == '*'))
8022 c = '@';
Bram Moolenaard333d1e2006-11-07 17:43:47 +00008023 /* Put the register in the typeahead buffer with the "silent" flag. */
8024 if (do_execreg(c, TRUE, vim_strchr(p_cpo, CPO_EXECBUF) != NULL, TRUE)
8025 == FAIL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008026 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008027 beep_flush();
Bram Moolenaardf177f62005-02-22 08:39:57 +00008028 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008029 else
8030 {
8031 int save_efr = exec_from_reg;
8032
8033 exec_from_reg = TRUE;
8034
8035 /*
8036 * Execute from the typeahead buffer.
Bram Moolenaar60462872009-11-03 11:40:19 +00008037 * Continue until the stuff buffer is empty and all added characters
8038 * have been consumed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008039 */
Bram Moolenaar60462872009-11-03 11:40:19 +00008040 while (!stuff_empty() || typebuf.tb_len > prev_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008041 (void)do_cmdline(NULL, getexline, NULL, DOCMD_NOWAIT|DOCMD_VERBOSE);
8042
8043 exec_from_reg = save_efr;
8044 }
8045}
8046
8047/*
8048 * ":!".
8049 */
8050 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008051ex_bang(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008052{
8053 do_bang(eap->addr_count, eap, eap->forceit, TRUE, TRUE);
8054}
8055
8056/*
8057 * ":undo".
8058 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008059 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +01008060ex_undo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008061{
Bram Moolenaard3667a22006-03-16 21:35:52 +00008062 if (eap->addr_count == 1) /* :undo 123 */
Bram Moolenaar730cde92010-06-27 05:18:54 +02008063 undo_time(eap->line2, FALSE, FALSE, TRUE);
Bram Moolenaard3667a22006-03-16 21:35:52 +00008064 else
8065 u_undo(1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008066}
8067
Bram Moolenaar55debbe2010-05-23 23:34:36 +02008068#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar6df6f472010-07-18 18:04:50 +02008069 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008070ex_wundo(exarg_T *eap)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02008071{
8072 char_u hash[UNDO_HASH_SIZE];
8073
8074 u_compute_hash(hash);
8075 u_write_undo(eap->arg, eap->forceit, curbuf, hash);
8076}
8077
Bram Moolenaar6df6f472010-07-18 18:04:50 +02008078 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008079ex_rundo(exarg_T *eap)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02008080{
8081 char_u hash[UNDO_HASH_SIZE];
8082
8083 u_compute_hash(hash);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02008084 u_read_undo(eap->arg, hash, NULL);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02008085}
8086#endif
8087
Bram Moolenaar071d4272004-06-13 20:20:40 +00008088/*
8089 * ":redo".
8090 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008091 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008092ex_redo(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008093{
8094 u_redo(1);
8095}
8096
8097/*
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008098 * ":earlier" and ":later".
8099 */
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008100 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008101ex_later(exarg_T *eap)
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008102{
8103 long count = 0;
8104 int sec = FALSE;
Bram Moolenaar730cde92010-06-27 05:18:54 +02008105 int file = FALSE;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008106 char_u *p = eap->arg;
8107
8108 if (*p == NUL)
8109 count = 1;
8110 else if (isdigit(*p))
8111 {
8112 count = getdigits(&p);
8113 switch (*p)
8114 {
8115 case 's': ++p; sec = TRUE; break;
8116 case 'm': ++p; sec = TRUE; count *= 60; break;
8117 case 'h': ++p; sec = TRUE; count *= 60 * 60; break;
Bram Moolenaar730cde92010-06-27 05:18:54 +02008118 case 'd': ++p; sec = TRUE; count *= 24 * 60 * 60; break;
8119 case 'f': ++p; file = TRUE; break;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008120 }
8121 }
8122
8123 if (*p != NUL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008124 semsg(_(e_invarg2), eap->arg);
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008125 else
Bram Moolenaar730cde92010-06-27 05:18:54 +02008126 undo_time(eap->cmdidx == CMD_earlier ? -count : count,
8127 sec, file, FALSE);
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008128}
8129
8130/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008131 * ":redir": start/stop redirection.
8132 */
8133 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008134ex_redir(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008135{
8136 char *mode;
8137 char_u *fname;
Bram Moolenaarca472992005-01-21 11:46:23 +00008138 char_u *arg = eap->arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008139
Bram Moolenaarba768492016-07-08 15:32:54 +02008140#ifdef FEAT_EVAL
Bram Moolenaar79815f12016-07-09 17:07:29 +02008141 if (redir_execute)
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02008142 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008143 emsg(_("E930: Cannot use :redir inside execute()"));
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02008144 return;
8145 }
Bram Moolenaarba768492016-07-08 15:32:54 +02008146#endif
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02008147
Bram Moolenaar071d4272004-06-13 20:20:40 +00008148 if (STRICMP(eap->arg, "END") == 0)
8149 close_redir();
8150 else
8151 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008152 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008153 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008154 ++arg;
8155 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008156 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008157 ++arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008158 mode = "a";
8159 }
8160 else
8161 mode = "w";
Bram Moolenaarca472992005-01-21 11:46:23 +00008162 arg = skipwhite(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008163
8164 close_redir();
8165
8166 /* Expand environment variables and "~/". */
Bram Moolenaarca472992005-01-21 11:46:23 +00008167 fname = expand_env_save(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008168 if (fname == NULL)
8169 return;
8170#ifdef FEAT_BROWSE
8171 if (cmdmod.browse)
8172 {
8173 char_u *browseFile;
8174
Bram Moolenaar7171abe2004-10-11 10:06:20 +00008175 browseFile = do_browse(BROWSE_SAVE,
8176 (char_u *)_("Save Redirection"),
Bram Moolenaarc36651b2018-04-29 12:22:56 +02008177 fname, NULL, NULL,
8178 (char_u *)_(BROWSE_FILTER_ALL_FILES), curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008179 if (browseFile == NULL)
8180 return; /* operation cancelled */
8181 vim_free(fname);
8182 fname = browseFile;
8183 eap->forceit = TRUE; /* since dialog already asked */
8184 }
8185#endif
8186
8187 redir_fd = open_exfile(fname, eap->forceit, mode);
8188 vim_free(fname);
8189 }
8190#ifdef FEAT_EVAL
Bram Moolenaarca472992005-01-21 11:46:23 +00008191 else if (*arg == '@')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008192 {
8193 /* redirect to a register a-z (resp. A-Z for appending) */
8194 close_redir();
Bram Moolenaarca472992005-01-21 11:46:23 +00008195 ++arg;
8196 if (ASCII_ISALPHA(*arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008197# ifdef FEAT_CLIPBOARD
Bram Moolenaarca472992005-01-21 11:46:23 +00008198 || *arg == '*'
Bram Moolenaar9a51c6e2006-11-14 19:25:02 +00008199 || *arg == '+'
Bram Moolenaar071d4272004-06-13 20:20:40 +00008200# endif
Bram Moolenaarca472992005-01-21 11:46:23 +00008201 || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008202 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008203 redir_reg = *arg++;
Bram Moolenaarc188b882007-10-19 14:20:54 +00008204 if (*arg == '>' && arg[1] == '>') /* append */
Bram Moolenaard9d30582005-05-18 22:10:28 +00008205 arg += 2;
Bram Moolenaarc188b882007-10-19 14:20:54 +00008206 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008207 {
Bram Moolenaarc188b882007-10-19 14:20:54 +00008208 /* Can use both "@a" and "@a>". */
Bram Moolenaar2c29bee2005-06-01 21:46:07 +00008209 if (*arg == '>')
8210 arg++;
Bram Moolenaarc188b882007-10-19 14:20:54 +00008211 /* Make register empty when not using @A-@Z and the
8212 * command is valid. */
8213 if (*arg == NUL && !isupper(redir_reg))
8214 write_reg_contents(redir_reg, (char_u *)"", -1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008215 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00008216 }
8217 if (*arg != NUL)
8218 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00008219 redir_reg = 0;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008220 semsg(_(e_invarg2), eap->arg);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008221 }
8222 }
8223 else if (*arg == '=' && arg[1] == '>')
8224 {
8225 int append;
8226
8227 /* redirect to a variable */
8228 close_redir();
8229 arg += 2;
8230
8231 if (*arg == '>')
8232 {
8233 ++arg;
8234 append = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008235 }
8236 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00008237 append = FALSE;
8238
8239 if (var_redir_start(skipwhite(arg), append) == OK)
8240 redir_vname = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008241 }
8242#endif
8243
8244 /* TODO: redirect to a buffer */
8245
Bram Moolenaar071d4272004-06-13 20:20:40 +00008246 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008247 semsg(_(e_invarg2), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008248 }
Bram Moolenaar29b2d262006-09-10 19:07:28 +00008249
8250 /* Make sure redirection is not off. Can happen for cmdline completion
8251 * that indirectly invokes a command to catch its output. */
8252 if (redir_fd != NULL
8253#ifdef FEAT_EVAL
8254 || redir_reg || redir_vname
8255#endif
8256 )
8257 redir_off = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008258}
8259
8260/*
8261 * ":redraw": force redraw
8262 */
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01008263 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008264ex_redraw(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008265{
8266 int r = RedrawingDisabled;
8267 int p = p_lz;
8268
8269 RedrawingDisabled = 0;
8270 p_lz = FALSE;
Bram Moolenaarabc39ab2017-03-01 18:04:05 +01008271 validate_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008272 update_topline();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01008273 update_screen(eap->forceit ? CLEAR : VIsual_active ? INVERTED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008274#ifdef FEAT_TITLE
8275 if (need_maketitle)
8276 maketitle();
8277#endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02008278#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
8279# ifdef VIMDLL
8280 if (!gui.in_use)
8281# endif
8282 resize_console_buf();
Bram Moolenaar78d21da2019-02-17 15:00:52 +01008283#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008284 RedrawingDisabled = r;
8285 p_lz = p;
8286
8287 /* Reset msg_didout, so that a message that's there is overwritten. */
8288 msg_didout = FALSE;
8289 msg_col = 0;
8290
Bram Moolenaar56667a52013-07-17 11:54:28 +02008291 /* No need to wait after an intentional redraw. */
8292 need_wait_return = FALSE;
8293
Bram Moolenaar071d4272004-06-13 20:20:40 +00008294 out_flush();
8295}
8296
8297/*
8298 * ":redrawstatus": force redraw of status line(s)
8299 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008300 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008301ex_redrawstatus(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008302{
Bram Moolenaar071d4272004-06-13 20:20:40 +00008303 int r = RedrawingDisabled;
8304 int p = p_lz;
8305
8306 RedrawingDisabled = 0;
8307 p_lz = FALSE;
8308 if (eap->forceit)
8309 status_redraw_all();
8310 else
8311 status_redraw_curbuf();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01008312 update_screen(VIsual_active ? INVERTED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008313 RedrawingDisabled = r;
8314 p_lz = p;
8315 out_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008316}
8317
Bram Moolenaare12bab32019-01-08 22:02:56 +01008318/*
8319 * ":redrawtabline": force redraw of the tabline
8320 */
8321 static void
8322ex_redrawtabline(exarg_T *eap UNUSED)
8323{
8324 int r = RedrawingDisabled;
8325 int p = p_lz;
8326
8327 RedrawingDisabled = 0;
8328 p_lz = FALSE;
8329
8330 draw_tabline();
8331
8332 RedrawingDisabled = r;
8333 p_lz = p;
8334 out_flush();
8335}
8336
Bram Moolenaar071d4272004-06-13 20:20:40 +00008337 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008338close_redir(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008339{
8340 if (redir_fd != NULL)
8341 {
8342 fclose(redir_fd);
8343 redir_fd = NULL;
8344 }
8345#ifdef FEAT_EVAL
8346 redir_reg = 0;
Bram Moolenaardf177f62005-02-22 08:39:57 +00008347 if (redir_vname)
8348 {
8349 var_redir_stop();
8350 redir_vname = 0;
8351 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008352#endif
8353}
8354
Bram Moolenaarf96ae0b2019-07-28 15:21:55 +02008355#if (defined(FEAT_SESSION) || defined(FEAT_EVAL)) || defined(PROTO)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008356 int
Bram Moolenaarf1d25012016-03-03 12:22:53 +01008357vim_mkdir_emsg(char_u *name, int prot)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008358{
8359 if (vim_mkdir(name, prot) != 0)
8360 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008361 semsg(_("E739: Cannot create directory: %s"), name);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008362 return FAIL;
8363 }
8364 return OK;
8365}
8366#endif
8367
Bram Moolenaar071d4272004-06-13 20:20:40 +00008368/*
8369 * Open a file for writing for an Ex command, with some checks.
8370 * Return file descriptor, or NULL on failure.
8371 */
8372 FILE *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008373open_exfile(
8374 char_u *fname,
8375 int forceit,
8376 char *mode) /* "w" for create new file or "a" for append */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008377{
8378 FILE *fd;
8379
8380#ifdef UNIX
8381 /* with Unix it is possible to open a directory */
8382 if (mch_isdir(fname))
8383 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008384 semsg(_(e_isadir2), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008385 return NULL;
8386 }
8387#endif
8388 if (!forceit && *mode != 'a' && vim_fexists(fname))
8389 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008390 semsg(_("E189: \"%s\" exists (add ! to override)"), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008391 return NULL;
8392 }
8393
8394 if ((fd = mch_fopen((char *)fname, mode)) == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008395 semsg(_("E190: Cannot open \"%s\" for writing"), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008396
8397 return fd;
8398}
8399
8400/*
8401 * ":mark" and ":k".
8402 */
8403 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008404ex_mark(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008405{
8406 pos_T pos;
8407
8408 if (*eap->arg == NUL) /* No argument? */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008409 emsg(_(e_argreq));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008410 else if (eap->arg[1] != NUL) /* more than one character? */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008411 emsg(_(e_trailing));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008412 else
8413 {
8414 pos = curwin->w_cursor; /* save curwin->w_cursor */
8415 curwin->w_cursor.lnum = eap->line2;
8416 beginline(BL_WHITE | BL_FIX);
8417 if (setmark(*eap->arg) == FAIL) /* set mark */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008418 emsg(_("E191: Argument must be a letter or forward/backward quote"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008419 curwin->w_cursor = pos; /* restore curwin->w_cursor */
8420 }
8421}
8422
8423/*
8424 * Update w_topline, w_leftcol and the cursor position.
8425 */
8426 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008427update_topline_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008428{
8429 check_cursor(); /* put cursor on valid line */
8430 update_topline();
8431 if (!curwin->w_p_wrap)
8432 validate_cursor();
8433 update_curswant();
8434}
8435
Bram Moolenaar071d4272004-06-13 20:20:40 +00008436/*
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008437 * Save the current State and go to Normal mode.
8438 * Return TRUE if the typeahead could be saved.
8439 */
8440 int
8441save_current_state(save_state_T *sst)
8442{
8443 sst->save_msg_scroll = msg_scroll;
8444 sst->save_restart_edit = restart_edit;
8445 sst->save_msg_didout = msg_didout;
8446 sst->save_State = State;
8447 sst->save_insertmode = p_im;
8448 sst->save_finish_op = finish_op;
8449 sst->save_opcount = opcount;
Bram Moolenaarcce713d2019-03-04 11:40:12 +01008450 sst->save_reg_executing = reg_executing;
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008451
8452 msg_scroll = FALSE; /* no msg scrolling in Normal mode */
8453 restart_edit = 0; /* don't go to Insert mode */
8454 p_im = FALSE; /* don't use 'insertmode' */
8455
8456 /*
8457 * Save the current typeahead. This is required to allow using ":normal"
8458 * from an event handler and makes sure we don't hang when the argument
8459 * ends with half a command.
8460 */
8461 save_typeahead(&sst->tabuf);
8462 return sst->tabuf.typebuf_valid;
8463}
8464
8465 void
8466restore_current_state(save_state_T *sst)
8467{
8468 /* Restore the previous typeahead. */
8469 restore_typeahead(&sst->tabuf);
8470
8471 msg_scroll = sst->save_msg_scroll;
8472 restart_edit = sst->save_restart_edit;
8473 p_im = sst->save_insertmode;
8474 finish_op = sst->save_finish_op;
8475 opcount = sst->save_opcount;
Bram Moolenaarcce713d2019-03-04 11:40:12 +01008476 reg_executing = sst->save_reg_executing;
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008477 msg_didout |= sst->save_msg_didout; /* don't reset msg_didout now */
8478
8479 /* Restore the state (needed when called from a function executed for
8480 * 'indentexpr'). Update the mouse and cursor, they may have changed. */
8481 State = sst->save_State;
8482#ifdef CURSOR_SHAPE
8483 ui_cursor_shape(); /* may show different cursor shape */
8484#endif
8485}
8486
8487/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008488 * ":normal[!] {commands}": Execute normal mode commands.
8489 */
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01008490 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008491ex_normal(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008492{
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008493 save_state_T save_state;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008494 char_u *arg = NULL;
8495 int l;
8496 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008497
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008498 if (ex_normal_lock > 0)
8499 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008500 emsg(_(e_secure));
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008501 return;
8502 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008503 if (ex_normal_busy >= p_mmd)
8504 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008505 emsg(_("E192: Recursive use of :normal too deep"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008506 return;
8507 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008508
Bram Moolenaar071d4272004-06-13 20:20:40 +00008509 /*
8510 * vgetc() expects a CSI and K_SPECIAL to have been escaped. Don't do
8511 * this for the K_SPECIAL leading byte, otherwise special keys will not
8512 * work.
8513 */
8514 if (has_mbyte)
8515 {
8516 int len = 0;
8517
8518 /* Count the number of characters to be escaped. */
8519 for (p = eap->arg; *p != NUL; ++p)
8520 {
Bram Moolenaar13505972019-01-24 15:04:48 +01008521#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008522 if (*p == CSI) /* leadbyte CSI */
8523 len += 2;
Bram Moolenaar13505972019-01-24 15:04:48 +01008524#endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008525 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008526 if (*++p == K_SPECIAL /* trailbyte K_SPECIAL or CSI */
Bram Moolenaar13505972019-01-24 15:04:48 +01008527#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008528 || *p == CSI
Bram Moolenaar13505972019-01-24 15:04:48 +01008529#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008530 )
8531 len += 2;
8532 }
8533 if (len > 0)
8534 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02008535 arg = alloc(STRLEN(eap->arg) + len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008536 if (arg != NULL)
8537 {
8538 len = 0;
8539 for (p = eap->arg; *p != NUL; ++p)
8540 {
8541 arg[len++] = *p;
Bram Moolenaar13505972019-01-24 15:04:48 +01008542#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008543 if (*p == CSI)
8544 {
8545 arg[len++] = KS_EXTRA;
8546 arg[len++] = (int)KE_CSI;
8547 }
Bram Moolenaar13505972019-01-24 15:04:48 +01008548#endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008549 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008550 {
8551 arg[len++] = *++p;
8552 if (*p == K_SPECIAL)
8553 {
8554 arg[len++] = KS_SPECIAL;
8555 arg[len++] = KE_FILLER;
8556 }
Bram Moolenaar13505972019-01-24 15:04:48 +01008557#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008558 else if (*p == CSI)
8559 {
8560 arg[len++] = KS_EXTRA;
8561 arg[len++] = (int)KE_CSI;
8562 }
Bram Moolenaar13505972019-01-24 15:04:48 +01008563#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008564 }
8565 arg[len] = NUL;
8566 }
8567 }
8568 }
8569 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008570
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008571 ++ex_normal_busy;
8572 if (save_current_state(&save_state))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008573 {
8574 /*
8575 * Repeat the :normal command for each line in the range. When no
8576 * range given, execute it just once, without positioning the cursor
8577 * first.
8578 */
8579 do
8580 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008581 if (eap->addr_count != 0)
8582 {
8583 curwin->w_cursor.lnum = eap->line1++;
8584 curwin->w_cursor.col = 0;
Bram Moolenaard5d37532017-03-27 23:02:07 +02008585 check_cursor_moved(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008586 }
8587
Bram Moolenaar13505972019-01-24 15:04:48 +01008588 exec_normal_cmd(arg != NULL
8589 ? arg
8590 : eap->arg, eap->forceit ? REMAP_NONE : REMAP_YES, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008591 }
8592 while (eap->addr_count > 0 && eap->line1 <= eap->line2 && !got_int);
8593 }
8594
8595 /* Might not return to the main loop when in an event handler. */
8596 update_topline_cursor();
8597
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008598 restore_current_state(&save_state);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008599 --ex_normal_busy;
Bram Moolenaareda73602014-11-05 09:53:23 +01008600#ifdef FEAT_MOUSE
8601 setmouse();
8602#endif
8603#ifdef CURSOR_SHAPE
8604 ui_cursor_shape(); /* may show different cursor shape */
8605#endif
8606
Bram Moolenaar071d4272004-06-13 20:20:40 +00008607 vim_free(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008608}
8609
8610/*
Bram Moolenaar64969662005-12-14 21:59:55 +00008611 * ":startinsert", ":startreplace" and ":startgreplace"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008612 */
8613 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008614ex_startinsert(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008615{
Bram Moolenaarfd371682005-01-14 21:42:54 +00008616 if (eap->forceit)
8617 {
Bram Moolenaar09ca9322017-09-26 17:40:45 +02008618 /* cursor line can be zero on startup */
8619 if (!curwin->w_cursor.lnum)
8620 curwin->w_cursor.lnum = 1;
Bram Moolenaarfd371682005-01-14 21:42:54 +00008621 coladvance((colnr_T)MAXCOL);
8622 curwin->w_curswant = MAXCOL;
8623 curwin->w_set_curswant = FALSE;
8624 }
8625
Bram Moolenaara40c5002005-01-09 21:16:21 +00008626 /* Ignore the command when already in Insert mode. Inserting an
8627 * expression register that invokes a function can do this. */
8628 if (State & INSERT)
8629 return;
8630
Bram Moolenaar64969662005-12-14 21:59:55 +00008631 if (eap->cmdidx == CMD_startinsert)
8632 restart_edit = 'a';
8633 else if (eap->cmdidx == CMD_startreplace)
8634 restart_edit = 'R';
Bram Moolenaar071d4272004-06-13 20:20:40 +00008635 else
Bram Moolenaar64969662005-12-14 21:59:55 +00008636 restart_edit = 'V';
8637
8638 if (!eap->forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008639 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00008640 if (eap->cmdidx == CMD_startinsert)
8641 restart_edit = 'i';
Bram Moolenaar071d4272004-06-13 20:20:40 +00008642 curwin->w_curswant = 0; /* avoid MAXCOL */
8643 }
8644}
8645
8646/*
8647 * ":stopinsert"
8648 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008649 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008650ex_stopinsert(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008651{
8652 restart_edit = 0;
8653 stop_insert_mode = TRUE;
Bram Moolenaarfd773e92016-04-02 19:39:16 +02008654 clearmode();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008655}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008656
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008657/*
8658 * Execute normal mode command "cmd".
8659 * "remap" can be REMAP_NONE or REMAP_YES.
8660 */
8661 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008662exec_normal_cmd(char_u *cmd, int remap, int silent)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008663{
Bram Moolenaar25281632016-01-21 23:32:32 +01008664 /* Stuff the argument into the typeahead buffer. */
8665 ins_typebuf(cmd, remap, 0, TRUE, silent);
Bram Moolenaar586c70c2018-10-02 16:23:58 +02008666 exec_normal(FALSE, FALSE, FALSE);
Bram Moolenaar25281632016-01-21 23:32:32 +01008667}
Bram Moolenaar25281632016-01-21 23:32:32 +01008668
Bram Moolenaar25281632016-01-21 23:32:32 +01008669/*
8670 * Execute normal_cmd() until there is no typeahead left.
Bram Moolenaar586c70c2018-10-02 16:23:58 +02008671 * When "use_vpeekc" is TRUE use vpeekc() to check for available chars.
Bram Moolenaar25281632016-01-21 23:32:32 +01008672 */
8673 void
Bram Moolenaar586c70c2018-10-02 16:23:58 +02008674exec_normal(int was_typed, int use_vpeekc, int may_use_terminal_loop UNUSED)
Bram Moolenaar25281632016-01-21 23:32:32 +01008675{
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008676 oparg_T oa;
Bram Moolenaar905dd902019-04-07 14:21:47 +02008677 int c;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008678
Bram Moolenaar905dd902019-04-07 14:21:47 +02008679 // When calling vpeekc() from feedkeys() it will return Ctrl_C when there
8680 // is nothing to get, so also check for Ctrl_C.
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008681 clear_oparg(&oa);
8682 finish_op = FALSE;
Bram Moolenaar586c70c2018-10-02 16:23:58 +02008683 while ((!stuff_empty()
8684 || ((was_typed || !typebuf_typed()) && typebuf.tb_len > 0)
Bram Moolenaar905dd902019-04-07 14:21:47 +02008685 || (use_vpeekc && (c = vpeekc()) != NUL && c != Ctrl_C))
Bram Moolenaar586c70c2018-10-02 16:23:58 +02008686 && !got_int)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008687 {
8688 update_topline_cursor();
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02008689#ifdef FEAT_TERMINAL
Bram Moolenaar655a82a2018-05-08 22:01:07 +02008690 if (may_use_terminal_loop && term_use_loop()
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02008691 && oa.op_type == OP_NOP && oa.regname == NUL
8692 && !VIsual_active)
8693 {
8694 /* If terminal_loop() returns OK we got a key that is handled
8695 * in Normal model. With FAIL we first need to position the
8696 * cursor and the screen needs to be redrawn. */
8697 if (terminal_loop(TRUE) == OK)
8698 normal_cmd(&oa, TRUE);
8699 }
8700 else
8701#endif
8702 /* execute a Normal mode cmd */
8703 normal_cmd(&oa, TRUE);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008704 }
8705}
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008706
Bram Moolenaar071d4272004-06-13 20:20:40 +00008707#ifdef FEAT_FIND_ID
8708 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008709ex_checkpath(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008710{
8711 find_pattern_in_path(NULL, 0, 0, FALSE, FALSE, CHECK_PATH, 1L,
8712 eap->forceit ? ACTION_SHOW_ALL : ACTION_SHOW,
8713 (linenr_T)1, (linenr_T)MAXLNUM);
8714}
8715
Bram Moolenaar4033c552017-09-16 20:54:51 +02008716#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008717/*
8718 * ":psearch"
8719 */
8720 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008721ex_psearch(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008722{
8723 g_do_tagpreview = p_pvh;
8724 ex_findpat(eap);
8725 g_do_tagpreview = 0;
8726}
8727#endif
8728
8729 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008730ex_findpat(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008731{
8732 int whole = TRUE;
8733 long n;
8734 char_u *p;
8735 int action;
8736
8737 switch (cmdnames[eap->cmdidx].cmd_name[2])
8738 {
8739 case 'e': /* ":psearch", ":isearch" and ":dsearch" */
8740 if (cmdnames[eap->cmdidx].cmd_name[0] == 'p')
8741 action = ACTION_GOTO;
8742 else
8743 action = ACTION_SHOW;
8744 break;
8745 case 'i': /* ":ilist" and ":dlist" */
8746 action = ACTION_SHOW_ALL;
8747 break;
8748 case 'u': /* ":ijump" and ":djump" */
8749 action = ACTION_GOTO;
8750 break;
8751 default: /* ":isplit" and ":dsplit" */
8752 action = ACTION_SPLIT;
8753 break;
8754 }
8755
8756 n = 1;
8757 if (vim_isdigit(*eap->arg)) /* get count */
8758 {
8759 n = getdigits(&eap->arg);
8760 eap->arg = skipwhite(eap->arg);
8761 }
8762 if (*eap->arg == '/') /* Match regexp, not just whole words */
8763 {
8764 whole = FALSE;
8765 ++eap->arg;
8766 p = skip_regexp(eap->arg, '/', p_magic, NULL);
8767 if (*p)
8768 {
8769 *p++ = NUL;
8770 p = skipwhite(p);
8771
8772 /* Check for trailing illegal characters */
8773 if (!ends_excmd(*p))
8774 eap->errmsg = e_trailing;
8775 else
8776 eap->nextcmd = check_nextcmd(p);
8777 }
8778 }
8779 if (!eap->skip)
8780 find_pattern_in_path(eap->arg, 0, (int)STRLEN(eap->arg),
8781 whole, !eap->forceit,
8782 *eap->cmd == 'd' ? FIND_DEFINE : FIND_ANY,
8783 n, action, eap->line1, eap->line2);
8784}
8785#endif
8786
Bram Moolenaar071d4272004-06-13 20:20:40 +00008787
Bram Moolenaar4033c552017-09-16 20:54:51 +02008788#ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00008789/*
8790 * ":ptag", ":ptselect", ":ptjump", ":ptnext", etc.
8791 */
8792 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008793ex_ptag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008794{
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01008795 g_do_tagpreview = p_pvh; /* will be reset to 0 in ex_tag_cmd() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008796 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
8797}
8798
8799/*
8800 * ":pedit"
8801 */
8802 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008803ex_pedit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008804{
8805 win_T *curwin_save = curwin;
8806
8807 g_do_tagpreview = p_pvh;
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00008808 prepare_tagpreview(TRUE);
Bram Moolenaar67883b42017-07-27 22:57:00 +02008809 keep_help_flag = bt_help(curwin_save->w_buffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008810 do_exedit(eap, NULL);
8811 keep_help_flag = FALSE;
8812 if (curwin != curwin_save && win_valid(curwin_save))
8813 {
8814 /* Return cursor to where we were */
8815 validate_cursor();
8816 redraw_later(VALID);
8817 win_enter(curwin_save, TRUE);
8818 }
8819 g_do_tagpreview = 0;
8820}
Bram Moolenaar4033c552017-09-16 20:54:51 +02008821#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008822
8823/*
8824 * ":stag", ":stselect" and ":stjump".
8825 */
8826 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008827ex_stag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008828{
8829 postponed_split = -1;
8830 postponed_split_flags = cmdmod.split;
Bram Moolenaard326ce82007-03-11 14:48:29 +00008831 postponed_split_tab = cmdmod.tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008832 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
8833 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00008834 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008835}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008836
8837/*
8838 * ":tag", ":tselect", ":tjump", ":tnext", etc.
8839 */
8840 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008841ex_tag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008842{
8843 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name);
8844}
8845
8846 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008847ex_tag_cmd(exarg_T *eap, char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008848{
8849 int cmd;
8850
8851 switch (name[1])
8852 {
8853 case 'j': cmd = DT_JUMP; /* ":tjump" */
8854 break;
8855 case 's': cmd = DT_SELECT; /* ":tselect" */
8856 break;
8857 case 'p': cmd = DT_PREV; /* ":tprevious" */
8858 break;
8859 case 'N': cmd = DT_PREV; /* ":tNext" */
8860 break;
8861 case 'n': cmd = DT_NEXT; /* ":tnext" */
8862 break;
8863 case 'o': cmd = DT_POP; /* ":pop" */
8864 break;
8865 case 'f': /* ":tfirst" */
8866 case 'r': cmd = DT_FIRST; /* ":trewind" */
8867 break;
8868 case 'l': cmd = DT_LAST; /* ":tlast" */
8869 break;
8870 default: /* ":tag" */
8871#ifdef FEAT_CSCOPE
Bram Moolenaar7c94c262008-06-20 09:11:34 +00008872 if (p_cst && *eap->arg != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008873 {
Bram Moolenaard4db7712016-11-12 19:16:46 +01008874 ex_cstag(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008875 return;
8876 }
8877#endif
8878 cmd = DT_TAG;
8879 break;
8880 }
8881
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00008882 if (name[0] == 'l')
8883 {
8884#ifndef FEAT_QUICKFIX
8885 ex_ni(eap);
8886 return;
8887#else
8888 cmd = DT_LTAG;
8889#endif
8890 }
8891
Bram Moolenaar071d4272004-06-13 20:20:40 +00008892 do_tag(eap->arg, cmd, eap->addr_count > 0 ? (int)eap->line2 : 1,
8893 eap->forceit, TRUE);
8894}
8895
8896/*
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008897 * Check "str" for starting with a special cmdline variable.
8898 * If found return one of the SPEC_ values and set "*usedlen" to the length of
8899 * the variable. Otherwise return -1 and "*usedlen" is unchanged.
8900 */
8901 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008902find_cmdline_var(char_u *src, int *usedlen)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008903{
8904 int len;
8905 int i;
Bram Moolenaar8f0b2d42009-05-16 14:41:10 +00008906 static char *(spec_str[]) = {
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008907 "%",
8908#define SPEC_PERC 0
8909 "#",
Bram Moolenaar65f08472017-09-10 18:16:20 +02008910#define SPEC_HASH (SPEC_PERC + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008911 "<cword>", /* cursor word */
Bram Moolenaar65f08472017-09-10 18:16:20 +02008912#define SPEC_CWORD (SPEC_HASH + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008913 "<cWORD>", /* cursor WORD */
Bram Moolenaar65f08472017-09-10 18:16:20 +02008914#define SPEC_CCWORD (SPEC_CWORD + 1)
8915 "<cexpr>", /* expr under cursor */
8916#define SPEC_CEXPR (SPEC_CCWORD + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008917 "<cfile>", /* cursor path name */
Bram Moolenaar65f08472017-09-10 18:16:20 +02008918#define SPEC_CFILE (SPEC_CEXPR + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008919 "<sfile>", /* ":so" file name */
Bram Moolenaar65f08472017-09-10 18:16:20 +02008920#define SPEC_SFILE (SPEC_CFILE + 1)
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01008921 "<slnum>", /* ":so" file line number */
Bram Moolenaar65f08472017-09-10 18:16:20 +02008922#define SPEC_SLNUM (SPEC_SFILE + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008923 "<afile>", /* autocommand file name */
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008924#define SPEC_AFILE (SPEC_SLNUM + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008925 "<abuf>", /* autocommand buffer number */
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008926#define SPEC_ABUF (SPEC_AFILE + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008927 "<amatch>", /* autocommand match name */
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01008928#define SPEC_AMATCH (SPEC_ABUF + 1)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008929 "<sflnum>", /* script file line number */
8930#define SPEC_SFLNUM (SPEC_AMATCH + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008931#ifdef FEAT_CLIENTSERVER
8932 "<client>"
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02008933# define SPEC_CLIENT (SPEC_SFLNUM + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008934#endif
8935 };
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008936
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00008937 for (i = 0; i < (int)(sizeof(spec_str) / sizeof(char *)); ++i)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008938 {
8939 len = (int)STRLEN(spec_str[i]);
8940 if (STRNCMP(src, spec_str[i], len) == 0)
8941 {
8942 *usedlen = len;
8943 return i;
8944 }
8945 }
8946 return -1;
8947}
8948
8949/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008950 * Evaluate cmdline variables.
8951 *
8952 * change '%' to curbuf->b_ffname
8953 * '#' to curwin->w_altfile
8954 * '<cword>' to word under the cursor
8955 * '<cWORD>' to WORD under the cursor
Bram Moolenaar8071cb22019-07-12 17:58:01 +02008956 * '<cexpr>' to C-expression under the cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00008957 * '<cfile>' to path name under the cursor
8958 * '<sfile>' to sourced file name
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01008959 * '<slnum>' to sourced file line number
Bram Moolenaar071d4272004-06-13 20:20:40 +00008960 * '<afile>' to file name for autocommand
8961 * '<abuf>' to buffer number for autocommand
8962 * '<amatch>' to matching name for autocommand
8963 *
8964 * When an error is detected, "errormsg" is set to a non-NULL pointer (may be
8965 * "" for error without a message) and NULL is returned.
8966 * Returns an allocated string if a valid match was found.
8967 * Returns NULL if no match was found. "usedlen" then still contains the
8968 * number of characters to skip.
8969 */
8970 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008971eval_vars(
8972 char_u *src, /* pointer into commandline */
8973 char_u *srcstart, /* beginning of valid memory for src */
8974 int *usedlen, /* characters after src that are used */
8975 linenr_T *lnump, /* line number for :e command, or NULL */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008976 char **errormsg, /* pointer to error message */
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008977 int *escaped) /* return value has escaped white space (can
Bram Moolenaar63b92542007-03-27 14:57:09 +00008978 * be NULL) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008979{
8980 int i;
8981 char_u *s;
8982 char_u *result;
8983 char_u *resultbuf = NULL;
8984 int resultlen;
8985 buf_T *buf;
8986 int valid = VALID_HEAD + VALID_PATH; /* assume valid result */
8987 int spec_idx;
8988#ifdef FEAT_MODIFY_FNAME
Bram Moolenaarfd249462018-07-28 16:14:30 +02008989 int tilde_file = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008990 int skip_mod = FALSE;
8991#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008992 char_u strbuf[30];
Bram Moolenaar071d4272004-06-13 20:20:40 +00008993
8994 *errormsg = NULL;
Bram Moolenaar63b92542007-03-27 14:57:09 +00008995 if (escaped != NULL)
8996 *escaped = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008997
8998 /*
8999 * Check if there is something to do.
9000 */
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009001 spec_idx = find_cmdline_var(src, usedlen);
9002 if (spec_idx < 0) /* no match */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009003 {
9004 *usedlen = 1;
9005 return NULL;
9006 }
9007
9008 /*
9009 * Skip when preceded with a backslash "\%" and "\#".
9010 * Note: In "\\%" the % is also not recognized!
9011 */
9012 if (src > srcstart && src[-1] == '\\')
9013 {
9014 *usedlen = 0;
Bram Moolenaara7241f52008-06-24 20:39:31 +00009015 STRMOVE(src - 1, src); /* remove backslash */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009016 return NULL;
9017 }
9018
9019 /*
9020 * word or WORD under cursor
9021 */
Bram Moolenaar65f08472017-09-10 18:16:20 +02009022 if (spec_idx == SPEC_CWORD || spec_idx == SPEC_CCWORD
9023 || spec_idx == SPEC_CEXPR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009024 {
Bram Moolenaar65f08472017-09-10 18:16:20 +02009025 resultlen = find_ident_under_cursor(&result,
9026 spec_idx == SPEC_CWORD ? (FIND_IDENT | FIND_STRING)
9027 : spec_idx == SPEC_CEXPR ? (FIND_IDENT | FIND_STRING | FIND_EVAL)
9028 : FIND_STRING);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009029 if (resultlen == 0)
9030 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009031 *errormsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +00009032 return NULL;
9033 }
9034 }
9035
9036 /*
9037 * '#': Alternate file name
9038 * '%': Current file name
9039 * File name under the cursor
9040 * File name for autocommand
9041 * and following modifiers
9042 */
9043 else
9044 {
9045 switch (spec_idx)
9046 {
9047 case SPEC_PERC: /* '%': current file */
9048 if (curbuf->b_fname == NULL)
9049 {
9050 result = (char_u *)"";
9051 valid = 0; /* Must have ":p:h" to be valid */
9052 }
9053 else
Bram Moolenaar00136dc2018-07-25 21:19:13 +02009054 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009055 result = curbuf->b_fname;
Bram Moolenaarfd249462018-07-28 16:14:30 +02009056#ifdef FEAT_MODIFY_FNAME
Bram Moolenaar00136dc2018-07-25 21:19:13 +02009057 tilde_file = STRCMP(result, "~") == 0;
Bram Moolenaarfd249462018-07-28 16:14:30 +02009058#endif
Bram Moolenaar00136dc2018-07-25 21:19:13 +02009059 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009060 break;
9061
9062 case SPEC_HASH: /* '#' or "#99": alternate file */
9063 if (src[1] == '#') /* "##": the argument list */
9064 {
9065 result = arg_all();
9066 resultbuf = result;
9067 *usedlen = 2;
Bram Moolenaar63b92542007-03-27 14:57:09 +00009068 if (escaped != NULL)
9069 *escaped = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009070#ifdef FEAT_MODIFY_FNAME
9071 skip_mod = TRUE;
9072#endif
9073 break;
9074 }
9075 s = src + 1;
Bram Moolenaard812df62008-11-09 12:46:09 +00009076 if (*s == '<') /* "#<99" uses v:oldfiles */
9077 ++s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009078 i = (int)getdigits(&s);
Bram Moolenaarc312b8b2017-10-28 17:53:04 +02009079 if (s == src + 2 && src[1] == '-')
9080 /* just a minus sign, don't skip over it */
9081 s--;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009082 *usedlen = (int)(s - src); /* length of what we expand */
9083
Bram Moolenaarc312b8b2017-10-28 17:53:04 +02009084 if (src[1] == '<' && i != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009085 {
Bram Moolenaard812df62008-11-09 12:46:09 +00009086 if (*usedlen < 2)
9087 {
9088 /* Should we give an error message for #<text? */
9089 *usedlen = 1;
9090 return NULL;
9091 }
9092#ifdef FEAT_EVAL
9093 result = list_find_str(get_vim_var_list(VV_OLDFILES),
9094 (long)i);
9095 if (result == NULL)
9096 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009097 *errormsg = "";
Bram Moolenaard812df62008-11-09 12:46:09 +00009098 return NULL;
9099 }
9100#else
Bram Moolenaar8e481e82019-01-15 20:07:48 +01009101 *errormsg = _("E809: #< is not available without the +eval feature");
Bram Moolenaar071d4272004-06-13 20:20:40 +00009102 return NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +00009103#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009104 }
9105 else
Bram Moolenaard812df62008-11-09 12:46:09 +00009106 {
Bram Moolenaarc312b8b2017-10-28 17:53:04 +02009107 if (i == 0 && src[1] == '<' && *usedlen > 1)
9108 *usedlen = 1;
Bram Moolenaard812df62008-11-09 12:46:09 +00009109 buf = buflist_findnr(i);
9110 if (buf == NULL)
9111 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009112 *errormsg = _("E194: No alternate file name to substitute for '#'");
Bram Moolenaard812df62008-11-09 12:46:09 +00009113 return NULL;
9114 }
9115 if (lnump != NULL)
9116 *lnump = ECMD_LAST;
9117 if (buf->b_fname == NULL)
9118 {
9119 result = (char_u *)"";
9120 valid = 0; /* Must have ":p:h" to be valid */
9121 }
9122 else
Bram Moolenaar00136dc2018-07-25 21:19:13 +02009123 {
Bram Moolenaard812df62008-11-09 12:46:09 +00009124 result = buf->b_fname;
Bram Moolenaarfd249462018-07-28 16:14:30 +02009125#ifdef FEAT_MODIFY_FNAME
Bram Moolenaar00136dc2018-07-25 21:19:13 +02009126 tilde_file = STRCMP(result, "~") == 0;
Bram Moolenaarfd249462018-07-28 16:14:30 +02009127#endif
Bram Moolenaar00136dc2018-07-25 21:19:13 +02009128 }
Bram Moolenaard812df62008-11-09 12:46:09 +00009129 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009130 break;
9131
9132#ifdef FEAT_SEARCHPATH
9133 case SPEC_CFILE: /* file name under cursor */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00009134 result = file_name_at_cursor(FNAME_MESS|FNAME_HYP, 1L, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009135 if (result == NULL)
9136 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009137 *errormsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +00009138 return NULL;
9139 }
9140 resultbuf = result; /* remember allocated string */
9141 break;
9142#endif
9143
Bram Moolenaar071d4272004-06-13 20:20:40 +00009144 case SPEC_AFILE: /* file name for autocommand */
9145 result = autocmd_fname;
Bram Moolenaarf6dad432008-09-18 19:29:58 +00009146 if (result != NULL && !autocmd_fname_full)
9147 {
9148 /* Still need to turn the fname into a full path. It is
9149 * postponed to avoid a delay when <afile> is not used. */
9150 autocmd_fname_full = TRUE;
9151 result = FullName_save(autocmd_fname, FALSE);
9152 vim_free(autocmd_fname);
9153 autocmd_fname = result;
9154 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009155 if (result == NULL)
9156 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009157 *errormsg = _("E495: no autocommand file name to substitute for \"<afile>\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00009158 return NULL;
9159 }
Bram Moolenaara0174af2008-01-02 20:08:25 +00009160 result = shorten_fname1(result);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009161 break;
9162
9163 case SPEC_ABUF: /* buffer number for autocommand */
9164 if (autocmd_bufnr <= 0)
9165 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009166 *errormsg = _("E496: no autocommand buffer number to substitute for \"<abuf>\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00009167 return NULL;
9168 }
9169 sprintf((char *)strbuf, "%d", autocmd_bufnr);
9170 result = strbuf;
9171 break;
9172
9173 case SPEC_AMATCH: /* match name for autocommand */
9174 result = autocmd_match;
9175 if (result == NULL)
9176 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009177 *errormsg = _("E497: no autocommand match name to substitute for \"<amatch>\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00009178 return NULL;
9179 }
9180 break;
9181
Bram Moolenaar071d4272004-06-13 20:20:40 +00009182 case SPEC_SFILE: /* file name for ":so" command */
9183 result = sourcing_name;
9184 if (result == NULL)
9185 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009186 *errormsg = _("E498: no :source file name to substitute for \"<sfile>\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00009187 return NULL;
9188 }
9189 break;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009190
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009191 case SPEC_SLNUM: /* line in file for ":so" command */
9192 if (sourcing_name == NULL || sourcing_lnum == 0)
9193 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009194 *errormsg = _("E842: no line number to use for \"<slnum>\"");
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009195 return NULL;
9196 }
9197 sprintf((char *)strbuf, "%ld", (long)sourcing_lnum);
9198 result = strbuf;
9199 break;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009200
9201#ifdef FEAT_EVAL
9202 case SPEC_SFLNUM: /* line in script file */
9203 if (current_sctx.sc_lnum + sourcing_lnum == 0)
9204 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009205 *errormsg = _("E961: no line number to use for \"<sflnum>\"");
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009206 return NULL;
9207 }
9208 sprintf((char *)strbuf, "%ld",
9209 (long)(current_sctx.sc_lnum + sourcing_lnum));
9210 result = strbuf;
9211 break;
9212#endif
9213
9214#ifdef FEAT_CLIENTSERVER
Bram Moolenaar071d4272004-06-13 20:20:40 +00009215 case SPEC_CLIENT: /* Source of last submitted input */
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009216 sprintf((char *)strbuf, PRINTF_HEX_LONG_U,
9217 (long_u)clientWindow);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009218 result = strbuf;
9219 break;
9220#endif
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009221
Bram Moolenaar9e0f6ec2017-05-16 09:36:54 +02009222 default:
9223 result = (char_u *)""; /* avoid gcc warning */
9224 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009225 }
9226
9227 resultlen = (int)STRLEN(result); /* length of new string */
9228 if (src[*usedlen] == '<') /* remove the file name extension */
9229 {
9230 ++*usedlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009231 if ((s = vim_strrchr(result, '.')) != NULL && s >= gettail(result))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009232 resultlen = (int)(s - result);
9233 }
9234#ifdef FEAT_MODIFY_FNAME
9235 else if (!skip_mod)
9236 {
Bram Moolenaar00136dc2018-07-25 21:19:13 +02009237 valid |= modify_fname(src, tilde_file, usedlen, &result, &resultbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009238 &resultlen);
9239 if (result == NULL)
9240 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009241 *errormsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +00009242 return NULL;
9243 }
9244 }
9245#endif
9246 }
9247
9248 if (resultlen == 0 || valid != VALID_HEAD + VALID_PATH)
9249 {
9250 if (valid != VALID_HEAD + VALID_PATH)
9251 /* xgettext:no-c-format */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009252 *errormsg = _("E499: Empty file name for '%' or '#', only works with \":p:h\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00009253 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009254 *errormsg = _("E500: Evaluates to an empty string");
Bram Moolenaar071d4272004-06-13 20:20:40 +00009255 result = NULL;
9256 }
9257 else
9258 result = vim_strnsave(result, resultlen);
9259 vim_free(resultbuf);
9260 return result;
9261}
9262
9263/*
9264 * Concatenate all files in the argument list, separated by spaces, and return
9265 * it in one allocated string.
9266 * Spaces and backslashes in the file names are escaped with a backslash.
9267 * Returns NULL when out of memory.
9268 */
9269 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009270arg_all(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009271{
9272 int len;
9273 int idx;
9274 char_u *retval = NULL;
9275 char_u *p;
9276
9277 /*
9278 * Do this loop two times:
9279 * first time: compute the total length
9280 * second time: concatenate the names
9281 */
9282 for (;;)
9283 {
9284 len = 0;
9285 for (idx = 0; idx < ARGCOUNT; ++idx)
9286 {
9287 p = alist_name(&ARGLIST[idx]);
9288 if (p != NULL)
9289 {
9290 if (len > 0)
9291 {
9292 /* insert a space in between names */
9293 if (retval != NULL)
9294 retval[len] = ' ';
9295 ++len;
9296 }
9297 for ( ; *p != NUL; ++p)
9298 {
Bram Moolenaar6e8d3b02015-06-09 21:33:31 +02009299 if (*p == ' '
9300#ifndef BACKSLASH_IN_FILENAME
9301 || *p == '\\'
9302#endif
Bram Moolenaar2c8c6812018-07-28 17:07:52 +02009303 || *p == '`')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009304 {
9305 /* insert a backslash */
9306 if (retval != NULL)
9307 retval[len] = '\\';
9308 ++len;
9309 }
9310 if (retval != NULL)
9311 retval[len] = *p;
9312 ++len;
9313 }
9314 }
9315 }
9316
9317 /* second time: break here */
9318 if (retval != NULL)
9319 {
9320 retval[len] = NUL;
9321 break;
9322 }
9323
9324 /* allocate memory */
Bram Moolenaar964b3742019-05-24 18:54:09 +02009325 retval = alloc(len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009326 if (retval == NULL)
9327 break;
9328 }
9329
9330 return retval;
9331}
9332
Bram Moolenaar071d4272004-06-13 20:20:40 +00009333/*
9334 * Expand the <sfile> string in "arg".
9335 *
9336 * Returns an allocated string, or NULL for any error.
9337 */
9338 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009339expand_sfile(char_u *arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009340{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009341 char *errormsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009342 int len;
9343 char_u *result;
9344 char_u *newres;
9345 char_u *repl;
9346 int srclen;
9347 char_u *p;
9348
9349 result = vim_strsave(arg);
9350 if (result == NULL)
9351 return NULL;
9352
9353 for (p = result; *p; )
9354 {
9355 if (STRNCMP(p, "<sfile>", 7) != 0)
9356 ++p;
9357 else
9358 {
9359 /* replace "<sfile>" with the sourced file name, and do ":" stuff */
Bram Moolenaar63b92542007-03-27 14:57:09 +00009360 repl = eval_vars(p, result, &srclen, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009361 if (errormsg != NULL)
9362 {
9363 if (*errormsg)
9364 emsg(errormsg);
9365 vim_free(result);
9366 return NULL;
9367 }
9368 if (repl == NULL) /* no match (cannot happen) */
9369 {
9370 p += srclen;
9371 continue;
9372 }
9373 len = (int)STRLEN(result) - srclen + (int)STRLEN(repl) + 1;
9374 newres = alloc(len);
9375 if (newres == NULL)
9376 {
9377 vim_free(repl);
9378 vim_free(result);
9379 return NULL;
9380 }
9381 mch_memmove(newres, result, (size_t)(p - result));
9382 STRCPY(newres + (p - result), repl);
9383 len = (int)STRLEN(newres);
9384 STRCAT(newres, p + srclen);
9385 vim_free(repl);
9386 vim_free(result);
9387 result = newres;
9388 p = newres + len; /* continue after the match */
9389 }
9390 }
9391
9392 return result;
9393}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009394
Bram Moolenaar071d4272004-06-13 20:20:40 +00009395#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG) || defined(PROTO)
Bram Moolenaar9c13b352005-05-19 20:53:52 +00009396/*
Bram Moolenaard9462e32011-04-11 21:35:11 +02009397 * Make a dialog message in "buff[DIALOG_MSG_SIZE]".
Bram Moolenaarb765d632005-06-07 21:00:02 +00009398 * "format" must contain "%s".
Bram Moolenaar9c13b352005-05-19 20:53:52 +00009399 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009400 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009401dialog_msg(char_u *buff, char *format, char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009402{
Bram Moolenaar071d4272004-06-13 20:20:40 +00009403 if (fname == NULL)
9404 fname = (char_u *)_("Untitled");
Bram Moolenaard9462e32011-04-11 21:35:11 +02009405 vim_snprintf((char *)buff, DIALOG_MSG_SIZE, format, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009406}
9407#endif
9408
9409/*
9410 * ":behave {mswin,xterm}"
9411 */
9412 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009413ex_behave(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009414{
9415 if (STRCMP(eap->arg, "mswin") == 0)
9416 {
9417 set_option_value((char_u *)"selection", 0L, (char_u *)"exclusive", 0);
9418 set_option_value((char_u *)"selectmode", 0L, (char_u *)"mouse,key", 0);
9419 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"popup", 0);
9420 set_option_value((char_u *)"keymodel", 0L,
9421 (char_u *)"startsel,stopsel", 0);
9422 }
9423 else if (STRCMP(eap->arg, "xterm") == 0)
9424 {
9425 set_option_value((char_u *)"selection", 0L, (char_u *)"inclusive", 0);
9426 set_option_value((char_u *)"selectmode", 0L, (char_u *)"", 0);
9427 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"extend", 0);
9428 set_option_value((char_u *)"keymodel", 0L, (char_u *)"", 0);
9429 }
9430 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009431 semsg(_(e_invarg2), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009432}
9433
Bram Moolenaar42b4dda2010-03-02 15:56:05 +01009434#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
9435/*
9436 * Function given to ExpandGeneric() to obtain the possible arguments of the
9437 * ":behave {mswin,xterm}" command.
9438 */
9439 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009440get_behave_arg(expand_T *xp UNUSED, int idx)
Bram Moolenaar42b4dda2010-03-02 15:56:05 +01009441{
9442 if (idx == 0)
9443 return (char_u *)"mswin";
9444 if (idx == 1)
9445 return (char_u *)"xterm";
9446 return NULL;
9447}
Bram Moolenaar9e507ca2016-10-15 15:39:39 +02009448
9449/*
9450 * Function given to ExpandGeneric() to obtain the possible arguments of the
9451 * ":messages {clear}" command.
9452 */
9453 char_u *
9454get_messages_arg(expand_T *xp UNUSED, int idx)
9455{
9456 if (idx == 0)
9457 return (char_u *)"clear";
9458 return NULL;
9459}
Bram Moolenaar42b4dda2010-03-02 15:56:05 +01009460#endif
9461
Bram Moolenaar113e1072019-01-20 15:30:40 +01009462#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
Bram Moolenaarcae92dc2017-08-06 15:22:15 +02009463 char_u *
9464get_mapclear_arg(expand_T *xp UNUSED, int idx)
9465{
9466 if (idx == 0)
9467 return (char_u *)"<buffer>";
9468 return NULL;
9469}
Bram Moolenaar113e1072019-01-20 15:30:40 +01009470#endif
Bram Moolenaarcae92dc2017-08-06 15:22:15 +02009471
Bram Moolenaar071d4272004-06-13 20:20:40 +00009472static int filetype_detect = FALSE;
9473static int filetype_plugin = FALSE;
9474static int filetype_indent = FALSE;
9475
9476/*
9477 * ":filetype [plugin] [indent] {on,off,detect}"
9478 * on: Load the filetype.vim file to install autocommands for file types.
9479 * off: Load the ftoff.vim file to remove all autocommands for file types.
9480 * plugin on: load filetype.vim and ftplugin.vim
9481 * plugin off: load ftplugof.vim
9482 * indent on: load filetype.vim and indent.vim
9483 * indent off: load indoff.vim
9484 */
9485 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009486ex_filetype(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009487{
9488 char_u *arg = eap->arg;
9489 int plugin = FALSE;
9490 int indent = FALSE;
9491
9492 if (*eap->arg == NUL)
9493 {
9494 /* Print current status. */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009495 smsg("filetype detection:%s plugin:%s indent:%s",
Bram Moolenaar071d4272004-06-13 20:20:40 +00009496 filetype_detect ? "ON" : "OFF",
9497 filetype_plugin ? (filetype_detect ? "ON" : "(on)") : "OFF",
9498 filetype_indent ? (filetype_detect ? "ON" : "(on)") : "OFF");
9499 return;
9500 }
9501
9502 /* Accept "plugin" and "indent" in any order. */
9503 for (;;)
9504 {
9505 if (STRNCMP(arg, "plugin", 6) == 0)
9506 {
9507 plugin = TRUE;
9508 arg = skipwhite(arg + 6);
9509 continue;
9510 }
9511 if (STRNCMP(arg, "indent", 6) == 0)
9512 {
9513 indent = TRUE;
9514 arg = skipwhite(arg + 6);
9515 continue;
9516 }
9517 break;
9518 }
9519 if (STRCMP(arg, "on") == 0 || STRCMP(arg, "detect") == 0)
9520 {
9521 if (*arg == 'o' || !filetype_detect)
9522 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01009523 source_runtime((char_u *)FILETYPE_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009524 filetype_detect = TRUE;
9525 if (plugin)
9526 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01009527 source_runtime((char_u *)FTPLUGIN_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009528 filetype_plugin = TRUE;
9529 }
9530 if (indent)
9531 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01009532 source_runtime((char_u *)INDENT_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009533 filetype_indent = TRUE;
9534 }
9535 }
9536 if (*arg == 'd')
9537 {
Bram Moolenaar1610d052016-06-09 22:53:01 +02009538 (void)do_doautocmd((char_u *)"filetypedetect BufRead", TRUE, NULL);
Bram Moolenaara3227e22006-03-08 21:32:40 +00009539 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009540 }
9541 }
9542 else if (STRCMP(arg, "off") == 0)
9543 {
9544 if (plugin || indent)
9545 {
9546 if (plugin)
9547 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01009548 source_runtime((char_u *)FTPLUGOF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009549 filetype_plugin = FALSE;
9550 }
9551 if (indent)
9552 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01009553 source_runtime((char_u *)INDOFF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009554 filetype_indent = FALSE;
9555 }
9556 }
9557 else
9558 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01009559 source_runtime((char_u *)FTOFF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009560 filetype_detect = FALSE;
9561 }
9562 }
9563 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009564 semsg(_(e_invarg2), arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009565}
9566
9567/*
Bram Moolenaar3e545692017-06-04 19:00:32 +02009568 * ":setfiletype [FALLBACK] {name}"
Bram Moolenaar071d4272004-06-13 20:20:40 +00009569 */
9570 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009571ex_setfiletype(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009572{
9573 if (!did_filetype)
Bram Moolenaar3e545692017-06-04 19:00:32 +02009574 {
9575 char_u *arg = eap->arg;
9576
9577 if (STRNCMP(arg, "FALLBACK ", 9) == 0)
9578 arg += 9;
9579
9580 set_option_value((char_u *)"filetype", 0L, arg, OPT_LOCAL);
9581 if (arg != eap->arg)
9582 did_filetype = FALSE;
9583 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009584}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009585
Bram Moolenaar071d4272004-06-13 20:20:40 +00009586 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009587ex_digraphs(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009588{
9589#ifdef FEAT_DIGRAPHS
9590 if (*eap->arg != NUL)
9591 putdigraph(eap->arg);
9592 else
Bram Moolenaareae8ae12018-12-14 18:53:02 +01009593 listdigraphs(eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009594#else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009595 emsg(_("E196: No digraphs in this version"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009596#endif
9597}
9598
9599 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009600ex_set(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009601{
9602 int flags = 0;
9603
9604 if (eap->cmdidx == CMD_setlocal)
9605 flags = OPT_LOCAL;
9606 else if (eap->cmdidx == CMD_setglobal)
9607 flags = OPT_GLOBAL;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01009608#if defined(FEAT_EVAL) && defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009609 if (cmdmod.browse && flags == 0)
9610 ex_options(eap);
9611 else
9612#endif
9613 (void)do_set(eap->arg, flags);
9614}
9615
Bram Moolenaar451fc7b2018-04-27 22:53:07 +02009616#if defined(FEAT_SEARCH_EXTRA) || defined(PROTO)
9617 void
9618set_no_hlsearch(int flag)
9619{
9620 no_hlsearch = flag;
9621# ifdef FEAT_EVAL
9622 set_vim_var_nr(VV_HLSEARCH, !no_hlsearch && p_hls);
9623# endif
9624}
9625
Bram Moolenaar071d4272004-06-13 20:20:40 +00009626/*
9627 * ":nohlsearch"
9628 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009629 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009630ex_nohlsearch(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009631{
Bram Moolenaar451fc7b2018-04-27 22:53:07 +02009632 set_no_hlsearch(TRUE);
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00009633 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009634}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009635#endif
9636
9637#ifdef FEAT_CRYPT
9638/*
9639 * ":X": Get crypt key
9640 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009641 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009642ex_X(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009643{
Bram Moolenaar3a0c9082014-11-12 15:15:42 +01009644 crypt_check_current_method();
Bram Moolenaar8f4ac012014-08-10 13:38:34 +02009645 (void)crypt_get_key(TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009646}
9647#endif
9648
9649#ifdef FEAT_FOLDING
9650 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009651ex_fold(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009652{
9653 if (foldManualAllowed(TRUE))
9654 foldCreate(eap->line1, eap->line2);
9655}
9656
9657 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009658ex_foldopen(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009659{
9660 opFoldRange(eap->line1, eap->line2, eap->cmdidx == CMD_foldopen,
9661 eap->forceit, FALSE);
9662}
9663
9664 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009665ex_folddo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009666{
9667 linenr_T lnum;
9668
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02009669#ifdef FEAT_CLIPBOARD
9670 start_global_changes();
9671#endif
9672
Bram Moolenaar071d4272004-06-13 20:20:40 +00009673 /* First set the marks for all lines closed/open. */
9674 for (lnum = eap->line1; lnum <= eap->line2; ++lnum)
9675 if (hasFolding(lnum, NULL, NULL) == (eap->cmdidx == CMD_folddoclosed))
9676 ml_setmarked(lnum);
9677
9678 /* Execute the command on the marked lines. */
9679 global_exe(eap->arg);
9680 ml_clearmarked(); /* clear rest of the marks */
Bram Moolenaar6b1ee342014-08-06 18:17:11 +02009681#ifdef FEAT_CLIPBOARD
9682 end_global_changes();
9683#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009684}
9685#endif
Bram Moolenaarf5291f32017-09-14 22:55:37 +02009686
Bram Moolenaar39665952018-08-15 20:59:48 +02009687#ifdef FEAT_QUICKFIX
9688/*
9689 * Returns TRUE if the supplied Ex cmdidx is for a location list command
9690 * instead of a quickfix command.
9691 */
9692 int
9693is_loclist_cmd(int cmdidx)
9694{
Bram Moolenaar74c8be22018-08-23 22:51:40 +02009695 if (cmdidx < 0 || cmdidx >= CMD_SIZE)
Bram Moolenaar39665952018-08-15 20:59:48 +02009696 return FALSE;
9697 return cmdnames[cmdidx].cmd_name[0] == 'l';
9698}
9699#endif
9700
Bram Moolenaarf5291f32017-09-14 22:55:37 +02009701# if defined(FEAT_TIMERS) || defined(PROTO)
9702 int
9703get_pressedreturn(void)
9704{
9705 return ex_pressedreturn;
9706}
9707
9708 void
9709set_pressedreturn(int val)
9710{
9711 ex_pressedreturn = val;
9712}
9713#endif