blob: 0086589342877db1c8393dcaa72e8c438eb42e3e [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 Moolenaarf28dbce2016-01-29 22:03:47 +010023static char_u *do_one_cmd(char_u **, int, struct condstack *, char_u *(*fgetline)(int, void *, int), void *cookie);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024#else
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010025static char_u *do_one_cmd(char_u **, int, char_u *(*fgetline)(int, void *, 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 Moolenaarf28dbce2016-01-29 22:03:47 +010032static void ex_abbreviate(exarg_T *eap);
33static void ex_map(exarg_T *eap);
34static void ex_unmap(exarg_T *eap);
35static void ex_mapclear(exarg_T *eap);
36static void ex_abclear(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +000037#ifndef FEAT_MENU
38# define ex_emenu ex_ni
39# define ex_menu ex_ni
40# define ex_menutranslate ex_ni
41#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010042static void ex_autocmd(exarg_T *eap);
43static void ex_doautocmd(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010044static void ex_bunload(exarg_T *eap);
45static void ex_buffer(exarg_T *eap);
46static void ex_bmodified(exarg_T *eap);
47static void ex_bnext(exarg_T *eap);
48static void ex_bprevious(exarg_T *eap);
49static void ex_brewind(exarg_T *eap);
50static void ex_blast(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010051static char_u *getargcmd(char_u **);
52static char_u *skip_cmd_arg(char_u *p, int rembs);
53static int getargopt(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +000054#ifndef FEAT_QUICKFIX
55# define ex_make ex_ni
Bram Moolenaar86b68352004-12-27 21:59:20 +000056# define ex_cbuffer ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +000057# define ex_cc ex_ni
58# define ex_cnext ex_ni
59# define ex_cfile ex_ni
60# define qf_list ex_ni
61# define qf_age ex_ni
Bram Moolenaarf6acffb2016-07-16 16:54:24 +020062# define qf_history ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +000063# define ex_helpgrep ex_ni
Bram Moolenaar86b68352004-12-27 21:59:20 +000064# define ex_vimgrep ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +000065#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +020066#if !defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +000067# define ex_cclose ex_ni
68# define ex_copen ex_ni
69# define ex_cwindow ex_ni
Bram Moolenaardcb17002016-07-07 18:58:59 +020070# define ex_cbottom ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +000071#endif
Bram Moolenaar1e015462005-09-25 22:16:38 +000072#if !defined(FEAT_QUICKFIX) || !defined(FEAT_EVAL)
73# define ex_cexpr ex_ni
74#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000075
Bram Moolenaar50eb16c2018-09-15 15:42:40 +020076static linenr_T get_address(exarg_T *, char_u **, int addr_type, int skip, int silent, int to_other_file, int address_count);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010077static void get_flags(exarg_T *eap);
Bram Moolenaar85363ab2010-07-18 13:58:26 +020078#if !defined(FEAT_PERL) \
79 || !defined(FEAT_PYTHON) || !defined(FEAT_PYTHON3) \
80 || !defined(FEAT_TCL) \
81 || !defined(FEAT_RUBY) \
82 || !defined(FEAT_LUA) \
83 || !defined(FEAT_MZSCHEME)
Bram Moolenaar7bb75552007-07-16 18:39:49 +000084# define HAVE_EX_SCRIPT_NI
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010085static void ex_script_ni(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +000086#endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010087static char *invalid_range(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010088static void correct_range(exarg_T *eap);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000089#ifdef FEAT_QUICKFIX
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010090static char_u *replace_makeprg(exarg_T *eap, char_u *p, char_u **cmdlinep);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000091#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010092static char_u *repl_cmdline(exarg_T *eap, char_u *src, int srclen, char_u *repl, char_u **cmdlinep);
93static void ex_highlight(exarg_T *eap);
94static void ex_colorscheme(exarg_T *eap);
95static void ex_quit(exarg_T *eap);
96static void ex_cquit(exarg_T *eap);
97static void ex_quit_all(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010098static void ex_close(exarg_T *eap);
99static void ex_win_close(int forceit, win_T *win, tabpage_T *tp);
100static void ex_only(exarg_T *eap);
101static void ex_resize(exarg_T *eap);
102static void ex_stag(exarg_T *eap);
103static void ex_tabclose(exarg_T *eap);
104static void ex_tabonly(exarg_T *eap);
105static void ex_tabnext(exarg_T *eap);
106static void ex_tabmove(exarg_T *eap);
107static void ex_tabs(exarg_T *eap);
Bram Moolenaar4033c552017-09-16 20:54:51 +0200108#if defined(FEAT_QUICKFIX)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100109static void ex_pclose(exarg_T *eap);
110static void ex_ptag(exarg_T *eap);
111static void ex_pedit(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000112#else
113# define ex_pclose ex_ni
114# define ex_ptag ex_ni
115# define ex_pedit ex_ni
116#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100117static void ex_hide(exarg_T *eap);
118static void ex_stop(exarg_T *eap);
119static void ex_exit(exarg_T *eap);
120static void ex_print(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000121#ifdef FEAT_BYTEOFF
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100122static void ex_goto(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000123#else
124# define ex_goto ex_ni
125#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100126static void ex_shell(exarg_T *eap);
127static void ex_preserve(exarg_T *eap);
128static void ex_recover(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100129static void ex_mode(exarg_T *eap);
130static void ex_wrongmodifier(exarg_T *eap);
131static void ex_find(exarg_T *eap);
132static void ex_open(exarg_T *eap);
133static void ex_edit(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000134#ifndef FEAT_GUI
135# define ex_gui ex_nogui
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100136static void ex_nogui(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000137#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +0100138#if defined(FEAT_GUI_MSWIN) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100139static void ex_tearoff(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000140#else
141# define ex_tearoff ex_ni
142#endif
Bram Moolenaar29a2c082018-03-05 21:06:23 +0100143#if (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK) \
144 || defined(FEAT_TERM_POPUP_MENU)) && defined(FEAT_MENU)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100145static void ex_popup(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000146#else
147# define ex_popup ex_ni
148#endif
149#ifndef FEAT_GUI_MSWIN
150# define ex_simalt ex_ni
151#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +0000152#if !defined(FEAT_GUI_MSWIN) && !defined(FEAT_GUI_GTK) && !defined(FEAT_GUI_MOTIF)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000153# define gui_mch_find_dialog ex_ni
154# define gui_mch_replace_dialog ex_ni
155#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +0000156#if !defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000157# define ex_helpfind ex_ni
158#endif
159#ifndef FEAT_CSCOPE
Bram Moolenaard4db7712016-11-12 19:16:46 +0100160# define ex_cscope ex_ni
161# define ex_scscope ex_ni
162# define ex_cstag ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000163#endif
164#ifndef FEAT_SYN_HL
165# define ex_syntax ex_ni
Bram Moolenaar860cae12010-06-05 23:22:07 +0200166# define ex_ownsyntax ex_ni
Bram Moolenaarf71a3db2006-03-12 21:50:18 +0000167#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100168#ifndef FEAT_EVAL
169# define ex_packadd ex_ni
170# define ex_packloadall ex_ni
171#endif
Bram Moolenaarf7512552013-06-06 14:55:19 +0200172#if !defined(FEAT_SYN_HL) || !defined(FEAT_PROFILE)
Bram Moolenaar8a7f5a22013-06-06 14:01:46 +0200173# define ex_syntime ex_ni
174#endif
Bram Moolenaarf71a3db2006-03-12 21:50:18 +0000175#ifndef FEAT_SPELL
Bram Moolenaarb765d632005-06-07 21:00:02 +0000176# define ex_spell ex_ni
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000177# define ex_mkspell ex_ni
Bram Moolenaarf417f2b2005-06-23 22:29:21 +0000178# define ex_spelldump ex_ni
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000179# define ex_spellinfo ex_ni
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000180# define ex_spellrepall ex_ni
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000181#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200182#ifndef FEAT_PERSISTENT_UNDO
183# define ex_rundo ex_ni
184# define ex_wundo ex_ni
185#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200186#ifndef FEAT_LUA
187# define ex_lua ex_script_ni
188# define ex_luado ex_ni
189# define ex_luafile ex_ni
190#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000191#ifndef FEAT_MZSCHEME
192# define ex_mzscheme ex_script_ni
193# define ex_mzfile ex_ni
194#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000195#ifndef FEAT_PERL
196# define ex_perl ex_script_ni
197# define ex_perldo ex_ni
198#endif
199#ifndef FEAT_PYTHON
200# define ex_python ex_script_ni
Bram Moolenaard620aa92013-05-17 16:40:06 +0200201# define ex_pydo ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000202# define ex_pyfile ex_ni
203#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200204#ifndef FEAT_PYTHON3
Bram Moolenaar368373e2010-07-19 20:46:22 +0200205# define ex_py3 ex_script_ni
Bram Moolenaar3dab2802013-05-15 18:28:13 +0200206# define ex_py3do ex_ni
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200207# define ex_py3file ex_ni
208#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +0100209#if !defined(FEAT_PYTHON) && !defined(FEAT_PYTHON3)
210# define ex_pyx ex_script_ni
211# define ex_pyxdo ex_ni
212# define ex_pyxfile ex_ni
213#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000214#ifndef FEAT_TCL
215# define ex_tcl ex_script_ni
216# define ex_tcldo ex_ni
217# define ex_tclfile ex_ni
218#endif
219#ifndef FEAT_RUBY
220# define ex_ruby ex_script_ni
221# define ex_rubydo ex_ni
222# define ex_rubyfile ex_ni
223#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000224#ifndef FEAT_KEYMAP
225# define ex_loadkeymap ex_ni
226#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100227static void ex_swapname(exarg_T *eap);
228static void ex_syncbind(exarg_T *eap);
229static void ex_read(exarg_T *eap);
230static void ex_pwd(exarg_T *eap);
231static void ex_equal(exarg_T *eap);
232static void ex_sleep(exarg_T *eap);
233static void do_exmap(exarg_T *eap, int isabbrev);
234static void ex_winsize(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100235static void ex_wincmd(exarg_T *eap);
Bram Moolenaar843ee412004-06-30 16:16:41 +0000236#if defined(FEAT_GUI) || defined(UNIX) || defined(VMS) || defined(MSWIN)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100237static void ex_winpos(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000238#else
239# define ex_winpos ex_ni
240#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100241static void ex_operators(exarg_T *eap);
242static void ex_put(exarg_T *eap);
243static void ex_copymove(exarg_T *eap);
244static void ex_submagic(exarg_T *eap);
245static void ex_join(exarg_T *eap);
246static void ex_at(exarg_T *eap);
247static void ex_bang(exarg_T *eap);
248static void ex_undo(exarg_T *eap);
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200249#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100250static void ex_wundo(exarg_T *eap);
251static void ex_rundo(exarg_T *eap);
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200252#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100253static void ex_redo(exarg_T *eap);
254static void ex_later(exarg_T *eap);
255static void ex_redir(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100256static void ex_redrawstatus(exarg_T *eap);
Bram Moolenaare12bab32019-01-08 22:02:56 +0100257static void ex_redrawtabline(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100258static void close_redir(void);
259static void ex_mkrc(exarg_T *eap);
260static void ex_mark(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100261static void ex_startinsert(exarg_T *eap);
262static void ex_stopinsert(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000263#ifdef FEAT_FIND_ID
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100264static void ex_checkpath(exarg_T *eap);
265static void ex_findpat(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000266#else
267# define ex_findpat ex_ni
268# define ex_checkpath ex_ni
269#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +0200270#if defined(FEAT_FIND_ID) && defined(FEAT_QUICKFIX)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100271static void ex_psearch(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000272#else
273# define ex_psearch ex_ni
274#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100275static void ex_tag(exarg_T *eap);
276static void ex_tag_cmd(exarg_T *eap, char_u *name);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000277#ifndef FEAT_EVAL
278# define ex_scriptnames ex_ni
279# define ex_finish ex_ni
280# define ex_echo ex_ni
281# define ex_echohl ex_ni
282# define ex_execute ex_ni
283# define ex_call ex_ni
284# define ex_if ex_ni
285# define ex_endif ex_ni
286# define ex_else ex_ni
287# define ex_while ex_ni
288# define ex_continue ex_ni
289# define ex_break ex_ni
290# define ex_endwhile ex_ni
291# define ex_throw ex_ni
292# define ex_try ex_ni
293# define ex_catch ex_ni
294# define ex_finally ex_ni
295# define ex_endtry ex_ni
296# define ex_endfunction ex_ni
297# define ex_let ex_ni
298# define ex_unlet ex_ni
Bram Moolenaar65c1b012005-01-31 19:02:28 +0000299# define ex_lockvar ex_ni
300# define ex_unlockvar ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000301# define ex_function ex_ni
302# define ex_delfunction ex_ni
303# define ex_return ex_ni
Bram Moolenaard812df62008-11-09 12:46:09 +0000304# define ex_oldfiles ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000305#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100306static char_u *arg_all(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000307#ifdef FEAT_SESSION
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100308static int makeopens(FILE *fd, char_u *dirnow);
309static int put_view(FILE *fd, win_T *wp, int add_edit, unsigned *flagp, int current_arg_idx);
310static void ex_loadview(exarg_T *eap);
311static char_u *get_view_file(int c);
Bram Moolenaareeefcc72007-05-01 21:21:21 +0000312static int did_lcd; /* whether ":lcd" was produced for a session */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000313#else
314# define ex_loadview ex_ni
315#endif
316#ifndef FEAT_EVAL
317# define ex_compiler ex_ni
318#endif
319#ifdef FEAT_VIMINFO
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100320static void ex_viminfo(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000321#else
322# define ex_viminfo ex_ni
323#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100324static void ex_behave(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100325static void ex_filetype(exarg_T *eap);
326static void ex_setfiletype(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000327#ifndef FEAT_DIFF
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000328# define ex_diffoff ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000329# define ex_diffpatch ex_ni
330# define ex_diffgetput ex_ni
331# define ex_diffsplit ex_ni
332# define ex_diffthis ex_ni
333# define ex_diffupdate ex_ni
334#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100335static void ex_digraphs(exarg_T *eap);
336static void ex_set(exarg_T *eap);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100337#if !defined(FEAT_EVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000338# define ex_options ex_ni
339#endif
340#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100341static void ex_nohlsearch(exarg_T *eap);
342static void ex_match(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000343#else
344# define ex_nohlsearch ex_ni
345# define ex_match ex_ni
346#endif
347#ifdef FEAT_CRYPT
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100348static void ex_X(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000349#else
350# define ex_X ex_ni
351#endif
352#ifdef FEAT_FOLDING
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100353static void ex_fold(exarg_T *eap);
354static void ex_foldopen(exarg_T *eap);
355static void ex_folddo(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000356#else
357# define ex_fold ex_ni
358# define ex_foldopen ex_ni
359# define ex_folddo ex_ni
360#endif
Bram Moolenaar13505972019-01-24 15:04:48 +0100361#if !(defined(HAVE_LOCALE_H) || defined(X_LOCALE))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000362# define ex_language ex_ni
363#endif
364#ifndef FEAT_SIGNS
365# define ex_sign ex_ni
366#endif
Bram Moolenaar009b2592004-10-24 19:18:58 +0000367#ifndef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200368# define ex_nbclose ex_ni
Bram Moolenaar009b2592004-10-24 19:18:58 +0000369# define ex_nbkey ex_ni
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200370# define ex_nbstart ex_ni
Bram Moolenaar009b2592004-10-24 19:18:58 +0000371#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000372
373#ifndef FEAT_EVAL
374# define ex_debug ex_ni
375# define ex_breakadd ex_ni
376# define ex_debuggreedy ex_ni
377# define ex_breakdel ex_ni
378# define ex_breaklist ex_ni
379#endif
380
381#ifndef FEAT_CMDHIST
382# define ex_history ex_ni
383#endif
384#ifndef FEAT_JUMPLIST
385# define ex_jumps ex_ni
Bram Moolenaar2d358992016-06-12 21:20:54 +0200386# define ex_clearjumps ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000387# define ex_changes ex_ni
388#endif
389
Bram Moolenaar05159a02005-02-26 23:04:13 +0000390#ifndef FEAT_PROFILE
391# define ex_profile ex_ni
392#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200393#ifndef FEAT_TERMINAL
394# define ex_terminal ex_ni
395#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +0000396
Bram Moolenaar071d4272004-06-13 20:20:40 +0000397/*
398 * Declare cmdnames[].
399 */
400#define DO_DECLARE_EXCMD
401#include "ex_cmds.h"
Bram Moolenaar6de5e122017-04-20 21:55:44 +0200402#include "ex_cmdidxs.h"
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +0100403
Bram Moolenaar071d4272004-06-13 20:20:40 +0000404static char_u dollar_command[2] = {'$', 0};
405
406
407#ifdef FEAT_EVAL
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000408/* Struct for storing a line inside a while/for loop */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000409typedef struct
410{
411 char_u *line; /* command line */
412 linenr_T lnum; /* sourcing_lnum of the line */
413} wcmd_T;
414
415/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000416 * Structure used to store info for line position in a while or for loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000417 * This is required, because do_one_cmd() may invoke ex_function(), which
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000418 * reads more lines that may come from the while/for loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000419 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000420struct loop_cookie
Bram Moolenaar071d4272004-06-13 20:20:40 +0000421{
422 garray_T *lines_gap; /* growarray with line info */
423 int current_line; /* last read line from growarray */
424 int repeating; /* TRUE when looping a second time */
425 /* When "repeating" is FALSE use "getline" and "cookie" to get lines */
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100426 char_u *(*getline)(int, void *, int);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000427 void *cookie;
428};
429
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100430static char_u *get_loop_line(int c, void *cookie, int indent);
431static int store_loop_line(garray_T *gap, char_u *line);
432static void free_cmdlines(garray_T *gap);
Bram Moolenaared203462004-06-16 11:19:22 +0000433
434/* Struct to save a few things while debugging. Used in do_cmdline() only. */
435struct dbg_stuff
436{
437 int trylevel;
438 int force_abort;
439 except_T *caught_stack;
440 char_u *vv_exception;
441 char_u *vv_throwpoint;
442 int did_emsg;
443 int got_int;
444 int did_throw;
445 int need_rethrow;
446 int check_cstack;
447 except_T *current_exception;
448};
449
Bram Moolenaared203462004-06-16 11:19:22 +0000450 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100451save_dbg_stuff(struct dbg_stuff *dsp)
Bram Moolenaared203462004-06-16 11:19:22 +0000452{
453 dsp->trylevel = trylevel; trylevel = 0;
454 dsp->force_abort = force_abort; force_abort = FALSE;
455 dsp->caught_stack = caught_stack; caught_stack = NULL;
456 dsp->vv_exception = v_exception(NULL);
457 dsp->vv_throwpoint = v_throwpoint(NULL);
458
459 /* Necessary for debugging an inactive ":catch", ":finally", ":endtry" */
460 dsp->did_emsg = did_emsg; did_emsg = FALSE;
461 dsp->got_int = got_int; got_int = FALSE;
462 dsp->did_throw = did_throw; did_throw = FALSE;
463 dsp->need_rethrow = need_rethrow; need_rethrow = FALSE;
464 dsp->check_cstack = check_cstack; check_cstack = FALSE;
465 dsp->current_exception = current_exception; current_exception = NULL;
466}
467
468 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100469restore_dbg_stuff(struct dbg_stuff *dsp)
Bram Moolenaared203462004-06-16 11:19:22 +0000470{
471 suppress_errthrow = FALSE;
472 trylevel = dsp->trylevel;
473 force_abort = dsp->force_abort;
474 caught_stack = dsp->caught_stack;
475 (void)v_exception(dsp->vv_exception);
476 (void)v_throwpoint(dsp->vv_throwpoint);
477 did_emsg = dsp->did_emsg;
478 got_int = dsp->got_int;
479 did_throw = dsp->did_throw;
480 need_rethrow = dsp->need_rethrow;
481 check_cstack = dsp->check_cstack;
482 current_exception = dsp->current_exception;
483}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000484#endif
485
Bram Moolenaar071d4272004-06-13 20:20:40 +0000486/*
487 * do_exmode(): Repeatedly get commands for the "Ex" mode, until the ":vi"
488 * command is given.
489 */
490 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100491do_exmode(
492 int improved) /* TRUE for "improved Ex" mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000493{
494 int save_msg_scroll;
495 int prev_msg_row;
496 linenr_T prev_line;
Bram Moolenaar79518e22017-02-17 16:31:35 +0100497 varnumber_T changedtick;
Bram Moolenaardf177f62005-02-22 08:39:57 +0000498
499 if (improved)
500 exmode_active = EXMODE_VIM;
501 else
502 exmode_active = EXMODE_NORMAL;
503 State = NORMAL;
504
505 /* When using ":global /pat/ visual" and then "Q" we return to continue
506 * the :global command. */
507 if (global_busy)
508 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000509
510 save_msg_scroll = msg_scroll;
511 ++RedrawingDisabled; /* don't redisplay the window */
512 ++no_wait_return; /* don't wait for return */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000513#ifdef FEAT_GUI
514 /* Ignore scrollbar and mouse events in Ex mode */
515 ++hold_gui_events;
516#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000517
Bram Moolenaar32526b32019-01-19 17:43:09 +0100518 msg(_("Entering Ex mode. Type \"visual\" to go to Normal mode."));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000519 while (exmode_active)
520 {
Bram Moolenaar7c626922005-02-07 22:01:03 +0000521 /* Check for a ":normal" command and no more characters left. */
522 if (ex_normal_busy > 0 && typebuf.tb_len == 0)
523 {
524 exmode_active = FALSE;
525 break;
526 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000527 msg_scroll = TRUE;
528 need_wait_return = FALSE;
529 ex_pressedreturn = FALSE;
530 ex_no_reprint = FALSE;
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100531 changedtick = CHANGEDTICK(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000532 prev_msg_row = msg_row;
533 prev_line = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000534 if (improved)
535 {
536 cmdline_row = msg_row;
537 do_cmdline(NULL, getexline, NULL, 0);
538 }
539 else
540 do_cmdline(NULL, getexmodeline, NULL, DOCMD_NOWAIT);
541 lines_left = Rows - 1;
542
Bram Moolenaardf177f62005-02-22 08:39:57 +0000543 if ((prev_line != curwin->w_cursor.lnum
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100544 || changedtick != CHANGEDTICK(curbuf)) && !ex_no_reprint)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000545 {
Bram Moolenaardf177f62005-02-22 08:39:57 +0000546 if (curbuf->b_ml.ml_flags & ML_EMPTY)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100547 emsg(_(e_emptybuf));
Bram Moolenaardf177f62005-02-22 08:39:57 +0000548 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000549 {
Bram Moolenaardf177f62005-02-22 08:39:57 +0000550 if (ex_pressedreturn)
551 {
552 /* go up one line, to overwrite the ":<CR>" line, so the
Bram Moolenaar81870892007-11-11 18:17:28 +0000553 * output doesn't contain empty lines. */
Bram Moolenaardf177f62005-02-22 08:39:57 +0000554 msg_row = prev_msg_row;
555 if (prev_msg_row == Rows - 1)
556 msg_row--;
557 }
558 msg_col = 0;
559 print_line_no_prefix(curwin->w_cursor.lnum, FALSE, FALSE);
560 msg_clr_eos();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000561 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000562 }
Bram Moolenaardf177f62005-02-22 08:39:57 +0000563 else if (ex_pressedreturn && !ex_no_reprint) /* must be at EOF */
564 {
565 if (curbuf->b_ml.ml_flags & ML_EMPTY)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100566 emsg(_(e_emptybuf));
Bram Moolenaardf177f62005-02-22 08:39:57 +0000567 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100568 emsg(_("E501: At end-of-file"));
Bram Moolenaardf177f62005-02-22 08:39:57 +0000569 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000570 }
571
572#ifdef FEAT_GUI
573 --hold_gui_events;
574#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000575 --RedrawingDisabled;
576 --no_wait_return;
577 update_screen(CLEAR);
578 need_wait_return = FALSE;
579 msg_scroll = save_msg_scroll;
580}
581
582/*
583 * Execute a simple command line. Used for translated commands like "*".
584 */
585 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100586do_cmdline_cmd(char_u *cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000587{
588 return do_cmdline(cmd, NULL, NULL,
589 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
590}
591
592/*
593 * do_cmdline(): execute one Ex command line
594 *
595 * 1. Execute "cmdline" when it is not NULL.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100596 * If "cmdline" is NULL, or more lines are needed, fgetline() is used.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000597 * 2. Split up in parts separated with '|'.
598 *
599 * This function can be called recursively!
600 *
601 * flags:
602 * DOCMD_VERBOSE - The command will be included in the error message.
603 * DOCMD_NOWAIT - Don't call wait_return() and friends.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100604 * DOCMD_REPEAT - Repeat execution until fgetline() returns NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000605 * DOCMD_KEYTYPED - Don't reset KeyTyped.
606 * DOCMD_EXCRESET - Reset the exception environment (used for debugging).
607 * DOCMD_KEEPLINE - Store first typed line (for repeating with ".").
608 *
609 * return FAIL if cmdline could not be executed, OK otherwise
610 */
611 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100612do_cmdline(
613 char_u *cmdline,
614 char_u *(*fgetline)(int, void *, int),
615 void *cookie, /* argument for fgetline() */
616 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000617{
618 char_u *next_cmdline; /* next cmd to execute */
619 char_u *cmdline_copy = NULL; /* copy of cmd line */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100620 int used_getline = FALSE; /* used "fgetline" to obtain command */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000621 static int recursive = 0; /* recursive depth */
622 int msg_didout_before_start = 0;
623 int count = 0; /* line number count */
624 int did_inc = FALSE; /* incremented RedrawingDisabled */
625 int retval = OK;
626#ifdef FEAT_EVAL
627 struct condstack cstack; /* conditional stack */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000628 garray_T lines_ga; /* keep lines for ":while"/":for" */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000629 int current_line = 0; /* active line in lines_ga */
630 char_u *fname = NULL; /* function or script name */
631 linenr_T *breakpoint = NULL; /* ptr to breakpoint field in cookie */
632 int *dbg_tick = NULL; /* ptr to dbg_tick field in cookie */
Bram Moolenaared203462004-06-16 11:19:22 +0000633 struct dbg_stuff debug_saved; /* saved things for debug mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000634 int initial_trylevel;
635 struct msglist **saved_msg_list = NULL;
636 struct msglist *private_msg_list;
637
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100638 /* "fgetline" and "cookie" passed to do_one_cmd() */
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100639 char_u *(*cmd_getline)(int, void *, int);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000640 void *cmd_cookie;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000641 struct loop_cookie cmd_loop_cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000642 void *real_cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +0000643 int getline_is_func;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000644#else
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100645# define cmd_getline fgetline
Bram Moolenaar071d4272004-06-13 20:20:40 +0000646# define cmd_cookie cookie
647#endif
648 static int call_depth = 0; /* recursiveness */
649
650#ifdef FEAT_EVAL
651 /* For every pair of do_cmdline()/do_one_cmd() calls, use an extra memory
652 * location for storing error messages to be converted to an exception.
Bram Moolenaarcf3630f2005-01-08 16:04:29 +0000653 * This ensures that the do_errthrow() call in do_one_cmd() does not
654 * combine the messages stored by an earlier invocation of do_one_cmd()
655 * with the command name of the later one. This would happen when
656 * BufWritePost autocommands are executed after a write error. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000657 saved_msg_list = msg_list;
658 msg_list = &private_msg_list;
659 private_msg_list = NULL;
660#endif
661
662 /* It's possible to create an endless loop with ":execute", catch that
Bram Moolenaar777b30f2017-01-02 15:26:27 +0100663 * here. The value of 200 allows nested function calls, ":source", etc.
664 * Allow 200 or 'maxfuncdepth', whatever is larger. */
Bram Moolenaarb094ff42017-01-02 16:16:39 +0100665 if (call_depth >= 200
666#ifdef FEAT_EVAL
667 && call_depth >= p_mfd
668#endif
669 )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000670 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100671 emsg(_("E169: Command too recursive"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000672#ifdef FEAT_EVAL
673 /* When converting to an exception, we do not include the command name
674 * since this is not an error of the specific command. */
675 do_errthrow((struct condstack *)NULL, (char_u *)NULL);
676 msg_list = saved_msg_list;
677#endif
678 return FAIL;
679 }
680 ++call_depth;
681
682#ifdef FEAT_EVAL
683 cstack.cs_idx = -1;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000684 cstack.cs_looplevel = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000685 cstack.cs_trylevel = 0;
686 cstack.cs_emsg_silent_list = NULL;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000687 cstack.cs_lflags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000688 ga_init2(&lines_ga, (int)sizeof(wcmd_T), 10);
689
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100690 real_cookie = getline_cookie(fgetline, cookie);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000691
692 /* Inside a function use a higher nesting level. */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100693 getline_is_func = getline_equal(fgetline, cookie, get_func_line);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000694 if (getline_is_func && ex_nesting_level == func_level(real_cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000695 ++ex_nesting_level;
696
697 /* Get the function or script name and the address where the next breakpoint
698 * line and the debug tick for a function or script are stored. */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000699 if (getline_is_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000700 {
701 fname = func_name(real_cookie);
702 breakpoint = func_breakpoint(real_cookie);
703 dbg_tick = func_dbg_tick(real_cookie);
704 }
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100705 else if (getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000706 {
707 fname = sourcing_name;
708 breakpoint = source_breakpoint(real_cookie);
709 dbg_tick = source_dbg_tick(real_cookie);
710 }
711
712 /*
713 * Initialize "force_abort" and "suppress_errthrow" at the top level.
714 */
715 if (!recursive)
716 {
717 force_abort = FALSE;
718 suppress_errthrow = FALSE;
719 }
720
721 /*
722 * If requested, store and reset the global values controlling the
Bram Moolenaar89d40322006-08-29 15:30:07 +0000723 * exception handling (used when debugging). Otherwise clear it to avoid
724 * a bogus compiler warning when the optimizer uses inline functions...
Bram Moolenaar071d4272004-06-13 20:20:40 +0000725 */
Bram Moolenaarb4872942006-05-13 10:32:52 +0000726 if (flags & DOCMD_EXCRESET)
Bram Moolenaared203462004-06-16 11:19:22 +0000727 save_dbg_stuff(&debug_saved);
Bram Moolenaar89d40322006-08-29 15:30:07 +0000728 else
Bram Moolenaar69b67f72015-09-25 16:59:47 +0200729 vim_memset(&debug_saved, 0, sizeof(debug_saved));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000730
731 initial_trylevel = trylevel;
732
733 /*
734 * "did_throw" will be set to TRUE when an exception is being thrown.
735 */
736 did_throw = FALSE;
737#endif
738 /*
739 * "did_emsg" will be set to TRUE when emsg() is used, in which case we
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000740 * cancel the whole command line, and any if/endif or loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000741 * If force_abort is set, we cancel everything.
742 */
743 did_emsg = FALSE;
744
745 /*
746 * KeyTyped is only set when calling vgetc(). Reset it here when not
747 * calling vgetc() (sourced command lines).
748 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100749 if (!(flags & DOCMD_KEYTYPED)
750 && !getline_equal(fgetline, cookie, getexline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000751 KeyTyped = FALSE;
752
753 /*
754 * Continue executing command lines:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000755 * - when inside an ":if", ":while" or ":for"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000756 * - for multiple commands on one line, separated with '|'
757 * - when repeating until there are no more lines (for ":source")
758 */
759 next_cmdline = cmdline;
760 do
761 {
Bram Moolenaar05159a02005-02-26 23:04:13 +0000762#ifdef FEAT_EVAL
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100763 getline_is_func = getline_equal(fgetline, cookie, get_func_line);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000764#endif
765
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000766 /* stop skipping cmds for an error msg after all endif/while/for */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000767 if (next_cmdline == NULL
768#ifdef FEAT_EVAL
769 && !force_abort
770 && cstack.cs_idx < 0
Bram Moolenaar05159a02005-02-26 23:04:13 +0000771 && !(getline_is_func && func_has_abort(real_cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000772#endif
773 )
774 did_emsg = FALSE;
775
776 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000777 * 1. If repeating a line in a loop, get a line from lines_ga.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100778 * 2. If no line given: Get an allocated line with fgetline().
Bram Moolenaar071d4272004-06-13 20:20:40 +0000779 * 3. If a line is given: Make a copy, so we can mess with it.
780 */
781
782#ifdef FEAT_EVAL
783 /* 1. If repeating, get a previous line from lines_ga. */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000784 if (cstack.cs_looplevel > 0 && current_line < lines_ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000785 {
786 /* Each '|' separated command is stored separately in lines_ga, to
787 * be able to jump to it. Don't use next_cmdline now. */
Bram Moolenaard23a8232018-02-10 18:45:26 +0100788 VIM_CLEAR(cmdline_copy);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000789
790 /* Check if a function has returned or, unless it has an unclosed
791 * try conditional, aborted. */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000792 if (getline_is_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000793 {
Bram Moolenaar05159a02005-02-26 23:04:13 +0000794# ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000795 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +0000796 func_line_end(real_cookie);
797# endif
798 if (func_has_ended(real_cookie))
799 {
800 retval = FAIL;
801 break;
802 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000803 }
Bram Moolenaar05159a02005-02-26 23:04:13 +0000804#ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000805 else if (do_profiling == PROF_YES
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100806 && getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar05159a02005-02-26 23:04:13 +0000807 script_line_end();
808#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000809
810 /* Check if a sourced file hit a ":finish" command. */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100811 if (source_finished(fgetline, cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000812 {
813 retval = FAIL;
814 break;
815 }
816
817 /* If breakpoints have been added/deleted need to check for it. */
818 if (breakpoint != NULL && dbg_tick != NULL
819 && *dbg_tick != debug_tick)
820 {
821 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100822 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar071d4272004-06-13 20:20:40 +0000823 fname, sourcing_lnum);
824 *dbg_tick = debug_tick;
825 }
826
827 next_cmdline = ((wcmd_T *)(lines_ga.ga_data))[current_line].line;
828 sourcing_lnum = ((wcmd_T *)(lines_ga.ga_data))[current_line].lnum;
829
830 /* Did we encounter a breakpoint? */
831 if (breakpoint != NULL && *breakpoint != 0
832 && *breakpoint <= sourcing_lnum)
833 {
834 dbg_breakpoint(fname, sourcing_lnum);
835 /* Find next breakpoint. */
836 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100837 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar071d4272004-06-13 20:20:40 +0000838 fname, sourcing_lnum);
839 *dbg_tick = debug_tick;
840 }
Bram Moolenaar05159a02005-02-26 23:04:13 +0000841# ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000842 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +0000843 {
844 if (getline_is_func)
845 func_line_start(real_cookie);
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100846 else if (getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar05159a02005-02-26 23:04:13 +0000847 script_line_start();
848 }
849# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000850 }
851
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000852 if (cstack.cs_looplevel > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000853 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000854 /* Inside a while/for loop we need to store the lines and use them
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100855 * again. Pass a different "fgetline" function to do_one_cmd()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000856 * below, so that it stores lines in or reads them from
857 * "lines_ga". Makes it possible to define a function inside a
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000858 * while/for loop. */
859 cmd_getline = get_loop_line;
860 cmd_cookie = (void *)&cmd_loop_cookie;
861 cmd_loop_cookie.lines_gap = &lines_ga;
862 cmd_loop_cookie.current_line = current_line;
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100863 cmd_loop_cookie.getline = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000864 cmd_loop_cookie.cookie = cookie;
865 cmd_loop_cookie.repeating = (current_line < lines_ga.ga_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000866 }
867 else
868 {
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100869 cmd_getline = fgetline;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000870 cmd_cookie = cookie;
871 }
872#endif
873
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100874 /* 2. If no line given, get an allocated line with fgetline(). */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000875 if (next_cmdline == NULL)
876 {
877 /*
878 * Need to set msg_didout for the first line after an ":if",
879 * otherwise the ":if" will be overwritten.
880 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100881 if (count == 1 && getline_equal(fgetline, cookie, getexline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000882 msg_didout = TRUE;
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100883 if (fgetline == NULL || (next_cmdline = fgetline(':', cookie,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000884#ifdef FEAT_EVAL
885 cstack.cs_idx < 0 ? 0 : (cstack.cs_idx + 1) * 2
886#else
887 0
888#endif
889 )) == NULL)
890 {
891 /* Don't call wait_return for aborted command line. The NULL
892 * returned for the end of a sourced file or executed function
893 * doesn't do this. */
894 if (KeyTyped && !(flags & DOCMD_REPEAT))
895 need_wait_return = FALSE;
896 retval = FAIL;
897 break;
898 }
899 used_getline = TRUE;
900
901 /*
902 * Keep the first typed line. Clear it when more lines are typed.
903 */
904 if (flags & DOCMD_KEEPLINE)
905 {
906 vim_free(repeat_cmdline);
907 if (count == 0)
908 repeat_cmdline = vim_strsave(next_cmdline);
909 else
910 repeat_cmdline = NULL;
911 }
912 }
913
914 /* 3. Make a copy of the command so we can mess with it. */
915 else if (cmdline_copy == NULL)
916 {
917 next_cmdline = vim_strsave(next_cmdline);
918 if (next_cmdline == NULL)
919 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100920 emsg(_(e_outofmem));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000921 retval = FAIL;
922 break;
923 }
924 }
925 cmdline_copy = next_cmdline;
926
927#ifdef FEAT_EVAL
928 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000929 * Save the current line when inside a ":while" or ":for", and when
930 * the command looks like a ":while" or ":for", because we may need it
931 * later. When there is a '|' and another command, it is stored
932 * separately, because we need to be able to jump back to it from an
933 * :endwhile/:endfor.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000934 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000935 if (current_line == lines_ga.ga_len
936 && (cstack.cs_looplevel || has_loop_cmd(next_cmdline)))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000937 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000938 if (store_loop_line(&lines_ga, next_cmdline) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000939 {
940 retval = FAIL;
941 break;
942 }
943 }
944 did_endif = FALSE;
945#endif
946
947 if (count++ == 0)
948 {
949 /*
950 * All output from the commands is put below each other, without
951 * waiting for a return. Don't do this when executing commands
952 * from a script or when being called recursive (e.g. for ":e
953 * +command file").
954 */
955 if (!(flags & DOCMD_NOWAIT) && !recursive)
956 {
957 msg_didout_before_start = msg_didout;
958 msg_didany = FALSE; /* no output yet */
959 msg_start();
960 msg_scroll = TRUE; /* put messages below each other */
Bram Moolenaar84a05ac2013-05-06 04:24:17 +0200961 ++no_wait_return; /* don't wait for return until finished */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000962 ++RedrawingDisabled;
963 did_inc = TRUE;
964 }
965 }
966
967 if (p_verbose >= 15 && sourcing_name != NULL)
968 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000969 ++no_wait_return;
Bram Moolenaar8b044b32005-05-31 22:05:58 +0000970 verbose_enter_scroll();
971
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100972 smsg(_("line %ld: %s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +0000973 (long)sourcing_lnum, cmdline_copy);
Bram Moolenaar8b044b32005-05-31 22:05:58 +0000974 if (msg_silent == 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +0100975 msg_puts("\n"); /* don't overwrite this */
Bram Moolenaar8b044b32005-05-31 22:05:58 +0000976
977 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000978 --no_wait_return;
979 }
980
981 /*
982 * 2. Execute one '|' separated command.
983 * do_one_cmd() will return NULL if there is no trailing '|'.
984 * "cmdline_copy" can change, e.g. for '%' and '#' expansion.
985 */
986 ++recursive;
987 next_cmdline = do_one_cmd(&cmdline_copy, flags & DOCMD_VERBOSE,
988#ifdef FEAT_EVAL
989 &cstack,
990#endif
991 cmd_getline, cmd_cookie);
992 --recursive;
993
994#ifdef FEAT_EVAL
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000995 if (cmd_cookie == (void *)&cmd_loop_cookie)
996 /* Use "current_line" from "cmd_loop_cookie", it may have been
Bram Moolenaar071d4272004-06-13 20:20:40 +0000997 * incremented when defining a function. */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000998 current_line = cmd_loop_cookie.current_line;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000999#endif
1000
1001 if (next_cmdline == NULL)
1002 {
Bram Moolenaard23a8232018-02-10 18:45:26 +01001003 VIM_CLEAR(cmdline_copy);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001004#ifdef FEAT_CMDHIST
1005 /*
1006 * If the command was typed, remember it for the ':' register.
1007 * Do this AFTER executing the command to make :@: work.
1008 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001009 if (getline_equal(fgetline, cookie, getexline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001010 && new_last_cmdline != NULL)
1011 {
1012 vim_free(last_cmdline);
1013 last_cmdline = new_last_cmdline;
1014 new_last_cmdline = NULL;
1015 }
1016#endif
1017 }
1018 else
1019 {
1020 /* need to copy the command after the '|' to cmdline_copy, for the
1021 * next do_one_cmd() */
Bram Moolenaara7241f52008-06-24 20:39:31 +00001022 STRMOVE(cmdline_copy, next_cmdline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001023 next_cmdline = cmdline_copy;
1024 }
1025
1026
1027#ifdef FEAT_EVAL
1028 /* reset did_emsg for a function that is not aborted by an error */
1029 if (did_emsg && !force_abort
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001030 && getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001031 && !func_has_abort(real_cookie))
1032 did_emsg = FALSE;
1033
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001034 if (cstack.cs_looplevel > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001035 {
1036 ++current_line;
1037
1038 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001039 * An ":endwhile", ":endfor" and ":continue" is handled here.
1040 * If we were executing commands, jump back to the ":while" or
1041 * ":for".
1042 * If we were not executing commands, decrement cs_looplevel.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001043 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001044 if (cstack.cs_lflags & (CSL_HAD_CONT | CSL_HAD_ENDLOOP))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001045 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001046 cstack.cs_lflags &= ~(CSL_HAD_CONT | CSL_HAD_ENDLOOP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001047
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001048 /* Jump back to the matching ":while" or ":for". Be careful
1049 * not to use a cs_line[] from an entry that isn't a ":while"
1050 * or ":for": It would make "current_line" invalid and can
1051 * cause a crash. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001052 if (!did_emsg && !got_int && !did_throw
1053 && cstack.cs_idx >= 0
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001054 && (cstack.cs_flags[cstack.cs_idx]
1055 & (CSF_WHILE | CSF_FOR))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001056 && cstack.cs_line[cstack.cs_idx] >= 0
1057 && (cstack.cs_flags[cstack.cs_idx] & CSF_ACTIVE))
1058 {
1059 current_line = cstack.cs_line[cstack.cs_idx];
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001060 /* remember we jumped there */
1061 cstack.cs_lflags |= CSL_HAD_LOOP;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001062 line_breakcheck(); /* check if CTRL-C typed */
1063
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001064 /* Check for the next breakpoint at or after the ":while"
1065 * or ":for". */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001066 if (breakpoint != NULL)
1067 {
1068 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001069 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001070 fname,
1071 ((wcmd_T *)lines_ga.ga_data)[current_line].lnum-1);
1072 *dbg_tick = debug_tick;
1073 }
1074 }
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001075 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001076 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001077 /* can only get here with ":endwhile" or ":endfor" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001078 if (cstack.cs_idx >= 0)
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001079 rewind_conditionals(&cstack, cstack.cs_idx - 1,
1080 CSF_WHILE | CSF_FOR, &cstack.cs_looplevel);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001081 }
1082 }
1083
1084 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001085 * For a ":while" or ":for" we need to remember the line number.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001086 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001087 else if (cstack.cs_lflags & CSL_HAD_LOOP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001088 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001089 cstack.cs_lflags &= ~CSL_HAD_LOOP;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001090 cstack.cs_line[cstack.cs_idx] = current_line - 1;
1091 }
1092 }
1093
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01001094 /* Check for the next breakpoint after a watchexpression */
1095 if (breakpoint != NULL && has_watchexpr())
1096 {
1097 *breakpoint = dbg_find_breakpoint(FALSE, fname, sourcing_lnum);
1098 *dbg_tick = debug_tick;
1099 }
1100
Bram Moolenaar071d4272004-06-13 20:20:40 +00001101 /*
1102 * When not inside any ":while" loop, clear remembered lines.
1103 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001104 if (cstack.cs_looplevel == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001105 {
1106 if (lines_ga.ga_len > 0)
1107 {
1108 sourcing_lnum =
1109 ((wcmd_T *)lines_ga.ga_data)[lines_ga.ga_len - 1].lnum;
1110 free_cmdlines(&lines_ga);
1111 }
1112 current_line = 0;
1113 }
1114
1115 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001116 * A ":finally" makes did_emsg, got_int, and did_throw pending for
1117 * being restored at the ":endtry". Reset them here and set the
1118 * ACTIVE and FINALLY flags, so that the finally clause gets executed.
1119 * This includes the case where a missing ":endif", ":endwhile" or
1120 * ":endfor" was detected by the ":finally" itself.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001121 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001122 if (cstack.cs_lflags & CSL_HAD_FINA)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001123 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001124 cstack.cs_lflags &= ~CSL_HAD_FINA;
1125 report_make_pending(cstack.cs_pending[cstack.cs_idx]
1126 & (CSTP_ERROR | CSTP_INTERRUPT | CSTP_THROW),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001127 did_throw ? (void *)current_exception : NULL);
1128 did_emsg = got_int = did_throw = FALSE;
1129 cstack.cs_flags[cstack.cs_idx] |= CSF_ACTIVE | CSF_FINALLY;
1130 }
1131
1132 /* Update global "trylevel" for recursive calls to do_cmdline() from
1133 * within this loop. */
1134 trylevel = initial_trylevel + cstack.cs_trylevel;
1135
1136 /*
Bram Moolenaarc1762cc2007-05-10 16:56:30 +00001137 * If the outermost try conditional (across function calls and sourced
Bram Moolenaar071d4272004-06-13 20:20:40 +00001138 * files) is aborted because of an error, an interrupt, or an uncaught
1139 * exception, cancel everything. If it is left normally, reset
1140 * force_abort to get the non-EH compatible abortion behavior for
1141 * the rest of the script.
1142 */
1143 if (trylevel == 0 && !did_emsg && !got_int && !did_throw)
1144 force_abort = FALSE;
1145
1146 /* Convert an interrupt to an exception if appropriate. */
1147 (void)do_intthrow(&cstack);
1148#endif /* FEAT_EVAL */
1149
1150 }
1151 /*
1152 * Continue executing command lines when:
1153 * - no CTRL-C typed, no aborting error, no exception thrown or try
1154 * conditionals need to be checked for executing finally clauses or
1155 * catching an interrupt exception
1156 * - didn't get an error message or lines are not typed
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001157 * - there is a command after '|', inside a :if, :while, :for or :try, or
Bram Moolenaar071d4272004-06-13 20:20:40 +00001158 * looping for ":source" command or function call.
1159 */
1160 while (!((got_int
1161#ifdef FEAT_EVAL
1162 || (did_emsg && force_abort) || did_throw
1163#endif
1164 )
1165#ifdef FEAT_EVAL
1166 && cstack.cs_trylevel == 0
1167#endif
1168 )
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001169 && !(did_emsg
1170#ifdef FEAT_EVAL
1171 /* Keep going when inside try/catch, so that the error can be
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02001172 * deal with, except when it is a syntax error, it may cause
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001173 * the :endtry to be missed. */
1174 && (cstack.cs_trylevel == 0 || did_emsg_syntax)
1175#endif
1176 && used_getline
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001177 && (getline_equal(fgetline, cookie, getexmodeline)
1178 || getline_equal(fgetline, cookie, getexline)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001179 && (next_cmdline != NULL
1180#ifdef FEAT_EVAL
1181 || cstack.cs_idx >= 0
1182#endif
1183 || (flags & DOCMD_REPEAT)));
1184
1185 vim_free(cmdline_copy);
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001186 did_emsg_syntax = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001187#ifdef FEAT_EVAL
1188 free_cmdlines(&lines_ga);
1189 ga_clear(&lines_ga);
1190
1191 if (cstack.cs_idx >= 0)
1192 {
1193 /*
1194 * If a sourced file or executed function ran to its end, report the
1195 * unclosed conditional.
1196 */
1197 if (!got_int && !did_throw
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001198 && ((getline_equal(fgetline, cookie, getsourceline)
1199 && !source_finished(fgetline, cookie))
1200 || (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001201 && !func_has_ended(real_cookie))))
1202 {
1203 if (cstack.cs_flags[cstack.cs_idx] & CSF_TRY)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001204 emsg(_(e_endtry));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001205 else if (cstack.cs_flags[cstack.cs_idx] & CSF_WHILE)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001206 emsg(_(e_endwhile));
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001207 else if (cstack.cs_flags[cstack.cs_idx] & CSF_FOR)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001208 emsg(_(e_endfor));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001209 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001210 emsg(_(e_endif));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001211 }
1212
1213 /*
1214 * Reset "trylevel" in case of a ":finish" or ":return" or a missing
1215 * ":endtry" in a sourced file or executed function. If the try
1216 * conditional is in its finally clause, ignore anything pending.
1217 * If it is in a catch clause, finish the caught exception.
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001218 * Also cleanup any "cs_forinfo" structures.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001219 */
1220 do
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001221 {
1222 int idx = cleanup_conditionals(&cstack, 0, TRUE);
1223
Bram Moolenaar89e5d682005-01-17 22:06:23 +00001224 if (idx >= 0)
1225 --idx; /* remove try block not in its finally clause */
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001226 rewind_conditionals(&cstack, idx, CSF_WHILE | CSF_FOR,
1227 &cstack.cs_looplevel);
1228 }
1229 while (cstack.cs_idx >= 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001230 trylevel = initial_trylevel;
1231 }
1232
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001233 /* If a missing ":endtry", ":endwhile", ":endfor", or ":endif" or a memory
1234 * lack was reported above and the error message is to be converted to an
Bram Moolenaar071d4272004-06-13 20:20:40 +00001235 * exception, do this now after rewinding the cstack. */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001236 do_errthrow(&cstack, getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001237 ? (char_u *)"endfunction" : (char_u *)NULL);
1238
1239 if (trylevel == 0)
1240 {
1241 /*
1242 * When an exception is being thrown out of the outermost try
1243 * conditional, discard the uncaught exception, disable the conversion
1244 * of interrupts or errors to exceptions, and ensure that no more
1245 * commands are executed.
1246 */
1247 if (did_throw)
1248 {
1249 void *p = NULL;
1250 char_u *saved_sourcing_name;
1251 int saved_sourcing_lnum;
1252 struct msglist *messages = NULL, *next;
1253
1254 /*
1255 * If the uncaught exception is a user exception, report it as an
1256 * error. If it is an error exception, display the saved error
1257 * message now. For an interrupt exception, do nothing; the
1258 * interrupt message is given elsewhere.
1259 */
1260 switch (current_exception->type)
1261 {
1262 case ET_USER:
Bram Moolenaar9c13b352005-05-19 20:53:52 +00001263 vim_snprintf((char *)IObuff, IOSIZE,
1264 _("E605: Exception not caught: %s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001265 current_exception->value);
1266 p = vim_strsave(IObuff);
1267 break;
1268 case ET_ERROR:
1269 messages = current_exception->messages;
1270 current_exception->messages = NULL;
1271 break;
1272 case ET_INTERRUPT:
1273 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001274 }
1275
1276 saved_sourcing_name = sourcing_name;
1277 saved_sourcing_lnum = sourcing_lnum;
1278 sourcing_name = current_exception->throw_name;
1279 sourcing_lnum = current_exception->throw_lnum;
1280 current_exception->throw_name = NULL;
1281
1282 discard_current_exception(); /* uses IObuff if 'verbose' */
1283 suppress_errthrow = TRUE;
1284 force_abort = TRUE;
1285
1286 if (messages != NULL)
1287 {
1288 do
1289 {
1290 next = messages->next;
1291 emsg(messages->msg);
1292 vim_free(messages->msg);
1293 vim_free(messages);
1294 messages = next;
1295 }
1296 while (messages != NULL);
1297 }
1298 else if (p != NULL)
1299 {
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01001300 emsg(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001301 vim_free(p);
1302 }
1303 vim_free(sourcing_name);
1304 sourcing_name = saved_sourcing_name;
1305 sourcing_lnum = saved_sourcing_lnum;
1306 }
1307
1308 /*
1309 * On an interrupt or an aborting error not converted to an exception,
1310 * disable the conversion of errors to exceptions. (Interrupts are not
1311 * converted any more, here.) This enables also the interrupt message
1312 * when force_abort is set and did_emsg unset in case of an interrupt
1313 * from a finally clause after an error.
1314 */
1315 else if (got_int || (did_emsg && force_abort))
1316 suppress_errthrow = TRUE;
1317 }
1318
1319 /*
1320 * The current cstack will be freed when do_cmdline() returns. An uncaught
1321 * exception will have to be rethrown in the previous cstack. If a function
1322 * has just returned or a script file was just finished and the previous
1323 * cstack belongs to the same function or, respectively, script file, it
1324 * will have to be checked for finally clauses to be executed due to the
1325 * ":return" or ":finish". This is done in do_one_cmd().
1326 */
1327 if (did_throw)
1328 need_rethrow = TRUE;
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001329 if ((getline_equal(fgetline, cookie, getsourceline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001330 && ex_nesting_level > source_level(real_cookie))
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001331 || (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001332 && ex_nesting_level > func_level(real_cookie) + 1))
1333 {
1334 if (!did_throw)
1335 check_cstack = TRUE;
1336 }
1337 else
1338 {
1339 /* When leaving a function, reduce nesting level. */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001340 if (getline_equal(fgetline, cookie, get_func_line))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001341 --ex_nesting_level;
1342 /*
1343 * Go to debug mode when returning from a function in which we are
1344 * single-stepping.
1345 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001346 if ((getline_equal(fgetline, cookie, getsourceline)
1347 || getline_equal(fgetline, cookie, get_func_line))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001348 && ex_nesting_level + 1 <= debug_break_level)
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001349 do_debug(getline_equal(fgetline, cookie, getsourceline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001350 ? (char_u *)_("End of sourced file")
1351 : (char_u *)_("End of function"));
1352 }
1353
1354 /*
1355 * Restore the exception environment (done after returning from the
1356 * debugger).
1357 */
1358 if (flags & DOCMD_EXCRESET)
Bram Moolenaared203462004-06-16 11:19:22 +00001359 restore_dbg_stuff(&debug_saved);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001360
1361 msg_list = saved_msg_list;
1362#endif /* FEAT_EVAL */
1363
1364 /*
1365 * If there was too much output to fit on the command line, ask the user to
1366 * hit return before redrawing the screen. With the ":global" command we do
1367 * this only once after the command is finished.
1368 */
1369 if (did_inc)
1370 {
1371 --RedrawingDisabled;
1372 --no_wait_return;
1373 msg_scroll = FALSE;
1374
1375 /*
1376 * When just finished an ":if"-":else" which was typed, no need to
1377 * wait for hit-return. Also for an error situation.
1378 */
1379 if (retval == FAIL
1380#ifdef FEAT_EVAL
1381 || (did_endif && KeyTyped && !did_emsg)
1382#endif
1383 )
1384 {
1385 need_wait_return = FALSE;
1386 msg_didany = FALSE; /* don't wait when restarting edit */
1387 }
1388 else if (need_wait_return)
1389 {
1390 /*
1391 * The msg_start() above clears msg_didout. The wait_return we do
1392 * here should not overwrite the command that may be shown before
1393 * doing that.
1394 */
1395 msg_didout |= msg_didout_before_start;
1396 wait_return(FALSE);
1397 }
1398 }
1399
Bram Moolenaar2a942252012-11-28 23:03:07 +01001400#ifdef FEAT_EVAL
1401 did_endif = FALSE; /* in case do_cmdline used recursively */
1402#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001403 /*
1404 * Reset if_level, in case a sourced script file contains more ":if" than
1405 * ":endif" (could be ":if x | foo | endif").
1406 */
1407 if_level = 0;
Bram Moolenaar2e18a122012-11-28 19:10:54 +01001408#endif
Bram Moolenaard4ad0d42012-11-28 17:34:48 +01001409
Bram Moolenaar071d4272004-06-13 20:20:40 +00001410 --call_depth;
1411 return retval;
1412}
1413
1414#ifdef FEAT_EVAL
1415/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001416 * Obtain a line when inside a ":while" or ":for" loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001417 */
1418 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001419get_loop_line(int c, void *cookie, int indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001420{
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001421 struct loop_cookie *cp = (struct loop_cookie *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001422 wcmd_T *wp;
1423 char_u *line;
1424
1425 if (cp->current_line + 1 >= cp->lines_gap->ga_len)
1426 {
1427 if (cp->repeating)
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001428 return NULL; /* trying to read past ":endwhile"/":endfor" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001429
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001430 /* First time inside the ":while"/":for": get line normally. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001431 if (cp->getline == NULL)
1432 line = getcmdline(c, 0L, indent);
1433 else
1434 line = cp->getline(c, cp->cookie, indent);
Bram Moolenaard68071d2006-05-02 22:08:30 +00001435 if (line != NULL && store_loop_line(cp->lines_gap, line) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001436 ++cp->current_line;
1437
1438 return line;
1439 }
1440
1441 KeyTyped = FALSE;
1442 ++cp->current_line;
1443 wp = (wcmd_T *)(cp->lines_gap->ga_data) + cp->current_line;
1444 sourcing_lnum = wp->lnum;
1445 return vim_strsave(wp->line);
1446}
1447
1448/*
1449 * Store a line in "gap" so that a ":while" loop can execute it again.
1450 */
1451 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001452store_loop_line(garray_T *gap, char_u *line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001453{
1454 if (ga_grow(gap, 1) == FAIL)
1455 return FAIL;
1456 ((wcmd_T *)(gap->ga_data))[gap->ga_len].line = vim_strsave(line);
1457 ((wcmd_T *)(gap->ga_data))[gap->ga_len].lnum = sourcing_lnum;
1458 ++gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001459 return OK;
1460}
1461
1462/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001463 * Free the lines stored for a ":while" or ":for" loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001464 */
1465 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001466free_cmdlines(garray_T *gap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001467{
1468 while (gap->ga_len > 0)
1469 {
1470 vim_free(((wcmd_T *)(gap->ga_data))[gap->ga_len - 1].line);
1471 --gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001472 }
1473}
1474#endif
1475
1476/*
Bram Moolenaar89d40322006-08-29 15:30:07 +00001477 * If "fgetline" is get_loop_line(), return TRUE if the getline it uses equals
1478 * "func". * Otherwise return TRUE when "fgetline" equals "func".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001479 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001480 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001481getline_equal(
1482 char_u *(*fgetline)(int, void *, int),
1483 void *cookie UNUSED, /* argument for fgetline() */
1484 char_u *(*func)(int, void *, int))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001485{
1486#ifdef FEAT_EVAL
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01001487 char_u *(*gp)(int, void *, int);
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001488 struct loop_cookie *cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001489
Bram Moolenaar89d40322006-08-29 15:30:07 +00001490 /* When "fgetline" is "get_loop_line()" use the "cookie" to find the
Bram Moolenaarc1762cc2007-05-10 16:56:30 +00001491 * function that's originally used to obtain the lines. This may be
1492 * nested several levels. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00001493 gp = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001494 cp = (struct loop_cookie *)cookie;
1495 while (gp == get_loop_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001496 {
1497 gp = cp->getline;
1498 cp = cp->cookie;
1499 }
1500 return gp == func;
1501#else
Bram Moolenaar89d40322006-08-29 15:30:07 +00001502 return fgetline == func;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001503#endif
1504}
1505
Bram Moolenaar071d4272004-06-13 20:20:40 +00001506/*
Bram Moolenaar89d40322006-08-29 15:30:07 +00001507 * If "fgetline" is get_loop_line(), return the cookie used by the original
Bram Moolenaar071d4272004-06-13 20:20:40 +00001508 * getline function. Otherwise return "cookie".
1509 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001510 void *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001511getline_cookie(
1512 char_u *(*fgetline)(int, void *, int) UNUSED,
1513 void *cookie) /* argument for fgetline() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001514{
Bram Moolenaar13505972019-01-24 15:04:48 +01001515#ifdef FEAT_EVAL
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01001516 char_u *(*gp)(int, void *, int);
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001517 struct loop_cookie *cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001518
Bram Moolenaar89d40322006-08-29 15:30:07 +00001519 /* When "fgetline" is "get_loop_line()" use the "cookie" to find the
Bram Moolenaarc1762cc2007-05-10 16:56:30 +00001520 * cookie that's originally used to obtain the lines. This may be nested
Bram Moolenaar071d4272004-06-13 20:20:40 +00001521 * several levels. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00001522 gp = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001523 cp = (struct loop_cookie *)cookie;
1524 while (gp == get_loop_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001525 {
1526 gp = cp->getline;
1527 cp = cp->cookie;
1528 }
1529 return cp;
Bram Moolenaar13505972019-01-24 15:04:48 +01001530#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001531 return cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001532#endif
Bram Moolenaar13505972019-01-24 15:04:48 +01001533}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001534
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001535
1536/*
1537 * Helper function to apply an offset for buffer commands, i.e. ":bdelete",
1538 * ":bwipeout", etc.
1539 * Returns the buffer number.
1540 */
1541 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001542compute_buffer_local_count(int addr_type, int lnum, int offset)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001543{
1544 buf_T *buf;
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001545 buf_T *nextbuf;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001546 int count = offset;
1547
1548 buf = firstbuf;
1549 while (buf->b_next != NULL && buf->b_fnum < lnum)
1550 buf = buf->b_next;
1551 while (count != 0)
1552 {
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001553 count += (offset < 0) ? 1 : -1;
1554 nextbuf = (offset < 0) ? buf->b_prev : buf->b_next;
1555 if (nextbuf == NULL)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001556 break;
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001557 buf = nextbuf;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001558 if (addr_type == ADDR_LOADED_BUFFERS)
1559 /* skip over unloaded buffers */
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001560 while (buf->b_ml.ml_mfp == NULL)
1561 {
1562 nextbuf = (offset < 0) ? buf->b_prev : buf->b_next;
1563 if (nextbuf == NULL)
1564 break;
1565 buf = nextbuf;
1566 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001567 }
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001568 /* we might have gone too far, last buffer is not loadedd */
1569 if (addr_type == ADDR_LOADED_BUFFERS)
1570 while (buf->b_ml.ml_mfp == NULL)
1571 {
1572 nextbuf = (offset >= 0) ? buf->b_prev : buf->b_next;
1573 if (nextbuf == NULL)
1574 break;
1575 buf = nextbuf;
1576 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001577 return buf->b_fnum;
1578}
1579
Bram Moolenaarf240e182014-11-27 18:33:02 +01001580 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001581current_win_nr(win_T *win)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001582{
1583 win_T *wp;
1584 int nr = 0;
1585
Bram Moolenaar29323592016-07-24 22:04:11 +02001586 FOR_ALL_WINDOWS(wp)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001587 {
1588 ++nr;
1589 if (wp == win)
1590 break;
1591 }
1592 return nr;
1593}
1594
1595 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001596current_tab_nr(tabpage_T *tab)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001597{
1598 tabpage_T *tp;
1599 int nr = 0;
1600
Bram Moolenaar29323592016-07-24 22:04:11 +02001601 FOR_ALL_TABPAGES(tp)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001602 {
1603 ++nr;
1604 if (tp == tab)
1605 break;
1606 }
1607 return nr;
1608}
1609
1610# define CURRENT_WIN_NR current_win_nr(curwin)
1611# define LAST_WIN_NR current_win_nr(NULL)
1612# define CURRENT_TAB_NR current_tab_nr(curtab)
1613# define LAST_TAB_NR current_tab_nr(NULL)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001614
Bram Moolenaar071d4272004-06-13 20:20:40 +00001615/*
1616 * Execute one Ex command.
1617 *
1618 * If 'sourcing' is TRUE, the command will be included in the error message.
1619 *
1620 * 1. skip comment lines and leading space
1621 * 2. handle command modifiers
Bram Moolenaar1c40a662014-11-27 16:38:11 +01001622 * 3. find the command
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001623 * 4. parse range
Bram Moolenaar1c40a662014-11-27 16:38:11 +01001624 * 5. Parse the command.
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001625 * 6. parse arguments
1626 * 7. switch on command name
Bram Moolenaar071d4272004-06-13 20:20:40 +00001627 *
Bram Moolenaar89d40322006-08-29 15:30:07 +00001628 * Note: "fgetline" can be NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001629 *
1630 * This function may be called recursively!
1631 */
1632#if (_MSC_VER == 1200)
1633/*
Bram Moolenaared203462004-06-16 11:19:22 +00001634 * Avoid optimisation bug in VC++ version 6.0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001635 */
Bram Moolenaar281bdce2005-01-25 21:53:18 +00001636 #pragma optimize( "g", off )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001637#endif
1638 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001639do_one_cmd(
1640 char_u **cmdlinep,
1641 int sourcing,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001642#ifdef FEAT_EVAL
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001643 struct condstack *cstack,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001644#endif
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001645 char_u *(*fgetline)(int, void *, int),
1646 void *cookie) /* argument for fgetline() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001647{
1648 char_u *p;
1649 linenr_T lnum;
1650 long n;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001651 char *errormsg = NULL; /* error message */
Bram Moolenaar7b668e82016-08-23 23:51:21 +02001652 char_u *after_modifier = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001653 exarg_T ea; /* Ex command arguments */
Bram Moolenaar8e258a42009-07-09 13:55:43 +00001654 int save_msg_scroll = msg_scroll;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001655 cmdmod_T save_cmdmod;
Bram Moolenaar9a2c0912019-03-30 14:26:18 +01001656 int save_reg_executing = reg_executing;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001657 int ni; /* set when Not Implemented */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001658 char_u *cmd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001659
1660 vim_memset(&ea, 0, sizeof(ea));
1661 ea.line1 = 1;
1662 ea.line2 = 1;
1663#ifdef FEAT_EVAL
1664 ++ex_nesting_level;
1665#endif
1666
Bram Moolenaar21691f82012-12-05 19:13:18 +01001667 /* When the last file has not been edited :q has to be typed twice. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001668 if (quitmore
1669#ifdef FEAT_EVAL
1670 /* avoid that a function call in 'statusline' does this */
Bram Moolenaar89d40322006-08-29 15:30:07 +00001671 && !getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001672#endif
Bram Moolenaar21691f82012-12-05 19:13:18 +01001673 /* avoid that an autocommand, e.g. QuitPre, does this */
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001674 && !getline_equal(fgetline, cookie, getnextac))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001675 --quitmore;
1676
1677 /*
1678 * Reset browse, confirm, etc.. They are restored when returning, for
1679 * recursive calls.
1680 */
1681 save_cmdmod = cmdmod;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001682
Bram Moolenaarcbb37ad2006-08-16 15:04:21 +00001683 /* "#!anything" is handled like a comment. */
1684 if ((*cmdlinep)[0] == '#' && (*cmdlinep)[1] == '!')
1685 goto doend;
1686
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02001687/*
1688 * 1. Skip comment lines and leading white space and colons.
1689 * 2. Handle command modifiers.
1690 */
1691 // The "ea" structure holds the arguments that can be used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001692 ea.cmd = *cmdlinep;
Bram Moolenaareffed932018-08-14 13:38:17 +02001693 ea.cmdlinep = cmdlinep;
1694 ea.getline = fgetline;
1695 ea.cookie = cookie;
1696#ifdef FEAT_EVAL
1697 ea.cstack = cstack;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001698#endif
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02001699 if (parse_command_modifiers(&ea, &errormsg, FALSE) == FAIL)
Bram Moolenaareffed932018-08-14 13:38:17 +02001700 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001701
Bram Moolenaar7b668e82016-08-23 23:51:21 +02001702 after_modifier = ea.cmd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001703
1704#ifdef FEAT_EVAL
1705 ea.skip = did_emsg || got_int || did_throw || (cstack->cs_idx >= 0
1706 && !(cstack->cs_flags[cstack->cs_idx] & CSF_ACTIVE));
1707#else
1708 ea.skip = (if_level > 0);
1709#endif
1710
Bram Moolenaar071d4272004-06-13 20:20:40 +00001711/*
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001712 * 3. Skip over the range to find the command. Let "p" point to after it.
1713 *
1714 * We need the command to know what kind of range it uses.
1715 */
1716 cmd = ea.cmd;
1717 ea.cmd = skip_range(ea.cmd, NULL);
1718 if (*ea.cmd == '*' && vim_strchr(p_cpo, CPO_STAR) == NULL)
1719 ea.cmd = skipwhite(ea.cmd + 1);
1720 p = find_command(&ea, NULL);
1721
Bram Moolenaar7feb35e2018-08-21 17:49:54 +02001722#ifdef FEAT_EVAL
1723# ifdef FEAT_PROFILE
1724 // Count this line for profiling if skip is TRUE.
1725 if (do_profiling == PROF_YES
1726 && (!ea.skip || cstack->cs_idx == 0 || (cstack->cs_idx > 0
1727 && (cstack->cs_flags[cstack->cs_idx - 1] & CSF_ACTIVE))))
1728 {
1729 int skip = did_emsg || got_int || did_throw;
1730
1731 if (ea.cmdidx == CMD_catch)
1732 skip = !skip && !(cstack->cs_idx >= 0
1733 && (cstack->cs_flags[cstack->cs_idx] & CSF_THROWN)
1734 && !(cstack->cs_flags[cstack->cs_idx] & CSF_CAUGHT));
1735 else if (ea.cmdidx == CMD_else || ea.cmdidx == CMD_elseif)
1736 skip = skip || !(cstack->cs_idx >= 0
1737 && !(cstack->cs_flags[cstack->cs_idx]
1738 & (CSF_ACTIVE | CSF_TRUE)));
1739 else if (ea.cmdidx == CMD_finally)
1740 skip = FALSE;
1741 else if (ea.cmdidx != CMD_endif
1742 && ea.cmdidx != CMD_endfor
1743 && ea.cmdidx != CMD_endtry
1744 && ea.cmdidx != CMD_endwhile)
1745 skip = ea.skip;
1746
1747 if (!skip)
1748 {
1749 if (getline_equal(fgetline, cookie, get_func_line))
1750 func_line_exec(getline_cookie(fgetline, cookie));
1751 else if (getline_equal(fgetline, cookie, getsourceline))
1752 script_line_exec();
1753 }
1754 }
1755# endif
1756
1757 /* May go to debug mode. If this happens and the ">quit" debug command is
1758 * used, throw an interrupt exception and skip the next command. */
1759 dbg_check_breakpoint(&ea);
1760 if (!ea.skip && got_int)
1761 {
1762 ea.skip = TRUE;
1763 (void)do_intthrow(cstack);
1764 }
1765#endif
1766
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001767/*
1768 * 4. parse a range specifier of the form: addr [,addr] [;addr] ..
Bram Moolenaar071d4272004-06-13 20:20:40 +00001769 *
1770 * where 'addr' is:
1771 *
1772 * % (entire file)
1773 * $ [+-NUM]
1774 * 'x [+-NUM] (where x denotes a currently defined mark)
1775 * . [+-NUM]
1776 * [+-NUM]..
1777 * NUM
1778 *
1779 * The ea.cmd pointer is updated to point to the first character following the
1780 * range spec. If an initial address is found, but no second, the upper bound
1781 * is equal to the lower.
1782 */
1783
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01001784 /* ea.addr_type for user commands is set by find_ucmd */
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01001785 if (!IS_USER_CMDIDX(ea.cmdidx))
1786 {
1787 if (ea.cmdidx != CMD_SIZE)
1788 ea.addr_type = cmdnames[(int)ea.cmdidx].cmd_addr_type;
1789 else
1790 ea.addr_type = ADDR_LINES;
1791
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01001792 /* :wincmd range depends on the argument. */
Bram Moolenaar164f3262015-01-14 21:22:01 +01001793 if (ea.cmdidx == CMD_wincmd && p != NULL)
1794 get_wincmd_addr_type(skipwhite(p), &ea);
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01001795 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001796
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01001797 ea.cmd = cmd;
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02001798 if (parse_cmd_address(&ea, &errormsg, FALSE) == FAIL)
Bram Moolenaaree8415b2018-08-10 23:13:12 +02001799 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001800
Bram Moolenaar071d4272004-06-13 20:20:40 +00001801/*
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001802 * 5. Parse the command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001803 */
1804
1805 /*
1806 * Skip ':' and any white space
1807 */
1808 ea.cmd = skipwhite(ea.cmd);
1809 while (*ea.cmd == ':')
1810 ea.cmd = skipwhite(ea.cmd + 1);
1811
1812 /*
1813 * If we got a line, but no command, then go to the line.
1814 * If we find a '|' or '\n' we set ea.nextcmd.
1815 */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001816 if (*ea.cmd == NUL || *ea.cmd == '"'
1817 || (ea.nextcmd = check_nextcmd(ea.cmd)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001818 {
1819 /*
1820 * strange vi behaviour:
1821 * ":3" jumps to line 3
1822 * ":3|..." prints line 3
1823 * ":|" prints current line
1824 */
1825 if (ea.skip) /* skip this if inside :if */
1826 goto doend;
Bram Moolenaardf177f62005-02-22 08:39:57 +00001827 if (*ea.cmd == '|' || (exmode_active && ea.line1 != ea.line2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001828 {
1829 ea.cmdidx = CMD_print;
1830 ea.argt = RANGE+COUNT+TRLBAR;
1831 if ((errormsg = invalid_range(&ea)) == NULL)
1832 {
1833 correct_range(&ea);
1834 ex_print(&ea);
1835 }
1836 }
1837 else if (ea.addr_count != 0)
1838 {
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00001839 if (ea.line2 > curbuf->b_ml.ml_line_count)
1840 {
1841 /* With '-' in 'cpoptions' a line number past the file is an
1842 * error, otherwise put it at the end of the file. */
1843 if (vim_strchr(p_cpo, CPO_MINUS) != NULL)
1844 ea.line2 = -1;
1845 else
1846 ea.line2 = curbuf->b_ml.ml_line_count;
1847 }
1848
1849 if (ea.line2 < 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001850 errormsg = _(e_invrange);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001851 else
1852 {
1853 if (ea.line2 == 0)
1854 curwin->w_cursor.lnum = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001855 else
1856 curwin->w_cursor.lnum = ea.line2;
1857 beginline(BL_SOL | BL_FIX);
1858 }
1859 }
1860 goto doend;
1861 }
1862
Bram Moolenaard5005162014-08-22 23:05:54 +02001863 /* If this looks like an undefined user command and there are CmdUndefined
1864 * autocommands defined, trigger the matching autocommands. */
1865 if (p != NULL && ea.cmdidx == CMD_SIZE && !ea.skip
1866 && ASCII_ISUPPER(*ea.cmd)
1867 && has_cmdundefined())
1868 {
Bram Moolenaard5005162014-08-22 23:05:54 +02001869 int ret;
1870
Bram Moolenaar5a31b462014-08-23 14:16:20 +02001871 p = ea.cmd;
Bram Moolenaard5005162014-08-22 23:05:54 +02001872 while (ASCII_ISALNUM(*p))
1873 ++p;
Bram Moolenaar120f4a82014-09-09 12:22:06 +02001874 p = vim_strnsave(ea.cmd, (int)(p - ea.cmd));
Bram Moolenaard5005162014-08-22 23:05:54 +02001875 ret = apply_autocmds(EVENT_CMDUNDEFINED, p, p, TRUE, NULL);
1876 vim_free(p);
Bram Moolenaar829aef12015-07-28 14:25:48 +02001877 /* If the autocommands did something and didn't cause an error, try
1878 * finding the command again. */
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001879 p = (ret
1880#ifdef FEAT_EVAL
1881 && !aborting()
Bram Moolenaard5005162014-08-22 23:05:54 +02001882#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001883 ) ? find_command(&ea, NULL) : ea.cmd;
1884 }
Bram Moolenaard5005162014-08-22 23:05:54 +02001885
Bram Moolenaar071d4272004-06-13 20:20:40 +00001886 if (p == NULL)
1887 {
1888 if (!ea.skip)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001889 errormsg = _("E464: Ambiguous use of user-defined command");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001890 goto doend;
1891 }
Bram Moolenaarac9fb182019-04-27 13:04:13 +02001892 // Check for wrong commands.
Bram Moolenaar6f9a4762017-06-22 20:39:17 +02001893 if (*p == '!' && ea.cmd[1] == 0151 && ea.cmd[0] == 78
1894 && !IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001895 {
1896 errormsg = uc_fun_cmd();
1897 goto doend;
1898 }
Bram Moolenaarac9fb182019-04-27 13:04:13 +02001899
Bram Moolenaar071d4272004-06-13 20:20:40 +00001900 if (ea.cmdidx == CMD_SIZE)
1901 {
1902 if (!ea.skip)
1903 {
1904 STRCPY(IObuff, _("E492: Not an editor command"));
1905 if (!sourcing)
Bram Moolenaar7b668e82016-08-23 23:51:21 +02001906 {
1907 /* If the modifier was parsed OK the error must be in the
1908 * following command */
1909 if (after_modifier != NULL)
1910 append_command(after_modifier);
1911 else
1912 append_command(*cmdlinep);
1913 }
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001914 errormsg = (char *)IObuff;
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001915 did_emsg_syntax = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001916 }
1917 goto doend;
1918 }
1919
Bram Moolenaar958636c2014-10-21 20:01:58 +02001920 ni = (!IS_USER_CMDIDX(ea.cmdidx)
1921 && (cmdnames[ea.cmdidx].cmd_func == ex_ni
Bram Moolenaar7bb75552007-07-16 18:39:49 +00001922#ifdef HAVE_EX_SCRIPT_NI
1923 || cmdnames[ea.cmdidx].cmd_func == ex_script_ni
1924#endif
1925 ));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001926
1927#ifndef FEAT_EVAL
1928 /*
1929 * When the expression evaluation is disabled, recognize the ":if" and
1930 * ":endif" commands and ignore everything in between it.
1931 */
1932 if (ea.cmdidx == CMD_if)
1933 ++if_level;
1934 if (if_level)
1935 {
1936 if (ea.cmdidx == CMD_endif)
1937 --if_level;
1938 goto doend;
1939 }
1940
1941#endif
1942
Bram Moolenaar196b3b02008-06-20 16:51:41 +00001943 /* forced commands */
1944 if (*p == '!' && ea.cmdidx != CMD_substitute
1945 && ea.cmdidx != CMD_smagic && ea.cmdidx != CMD_snomagic)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001946 {
1947 ++p;
1948 ea.forceit = TRUE;
1949 }
1950 else
1951 ea.forceit = FALSE;
1952
1953/*
Bram Moolenaar1c40a662014-11-27 16:38:11 +01001954 * 6. Parse arguments.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001955 */
Bram Moolenaar958636c2014-10-21 20:01:58 +02001956 if (!IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001957 ea.argt = (long)cmdnames[(int)ea.cmdidx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001958
1959 if (!ea.skip)
1960 {
1961#ifdef HAVE_SANDBOX
1962 if (sandbox != 0 && !(ea.argt & SBOXOK))
1963 {
Bram Moolenaar8c62a082019-02-08 14:34:10 +01001964 // Command not allowed in sandbox.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001965 errormsg = _(e_sandbox);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001966 goto doend;
1967 }
1968#endif
Bram Moolenaar8c62a082019-02-08 14:34:10 +01001969 if (restricted != 0 && (ea.argt & RESTRICT))
1970 {
1971 errormsg = _("E981: Command not allowed in rvim");
1972 goto doend;
1973 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001974 if (!curbuf->b_p_ma && (ea.argt & MODIFY))
1975 {
1976 /* Command not allowed in non-'modifiable' buffer */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001977 errormsg = _(e_modifiable);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001978 goto doend;
1979 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00001980
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00001981 if (text_locked() && !(ea.argt & CMDWIN)
Bram Moolenaar958636c2014-10-21 20:01:58 +02001982 && !IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001983 {
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00001984 /* Command not allowed when editing the command line. */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001985 errormsg = _(get_text_locked_msg());
Bram Moolenaar071d4272004-06-13 20:20:40 +00001986 goto doend;
1987 }
Bram Moolenaar379fb762018-08-30 15:58:28 +02001988
Bram Moolenaar5555acc2006-04-07 21:33:12 +00001989 /* Disallow editing another buffer when "curbuf_lock" is set.
Bram Moolenaar379fb762018-08-30 15:58:28 +02001990 * Do allow ":checktime" (it is postponed).
1991 * Do allow ":edit" (check for an argument later).
1992 * Do allow ":file" with no arguments (check for an argument later). */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001993 if (!(ea.argt & CMDWIN)
Bram Moolenaar5555acc2006-04-07 21:33:12 +00001994 && ea.cmdidx != CMD_checktime
Bram Moolenaar379fb762018-08-30 15:58:28 +02001995 && ea.cmdidx != CMD_edit
1996 && ea.cmdidx != CMD_file
Bram Moolenaar958636c2014-10-21 20:01:58 +02001997 && !IS_USER_CMDIDX(ea.cmdidx)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001998 && curbuf_locked())
1999 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002000
2001 if (!ni && !(ea.argt & RANGE) && ea.addr_count > 0)
2002 {
2003 /* no range allowed */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002004 errormsg = _(e_norange);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002005 goto doend;
2006 }
2007 }
2008
2009 if (!ni && !(ea.argt & BANG) && ea.forceit) /* no <!> allowed */
2010 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002011 errormsg = _(e_nobang);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002012 goto doend;
2013 }
2014
2015 /*
2016 * Don't complain about the range if it is not used
2017 * (could happen if line_count is accidentally set to 0).
2018 */
2019 if (!ea.skip && !ni)
2020 {
2021 /*
2022 * If the range is backwards, ask for confirmation and, if given, swap
2023 * ea.line1 & ea.line2 so it's forwards again.
2024 * When global command is busy, don't ask, will fail below.
2025 */
2026 if (!global_busy && ea.line1 > ea.line2)
2027 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002028 if (msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002029 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002030 if (sourcing || exmode_active)
2031 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002032 errormsg = _("E493: Backwards range given");
Bram Moolenaara5792f52005-11-23 21:25:05 +00002033 goto doend;
2034 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002035 if (ask_yesno((char_u *)
2036 _("Backwards range given, OK to swap"), FALSE) != 'y')
Bram Moolenaara5792f52005-11-23 21:25:05 +00002037 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002038 }
2039 lnum = ea.line1;
2040 ea.line1 = ea.line2;
2041 ea.line2 = lnum;
2042 }
2043 if ((errormsg = invalid_range(&ea)) != NULL)
2044 goto doend;
2045 }
2046
2047 if ((ea.argt & NOTADR) && ea.addr_count == 0) /* default is 1, not cursor */
2048 ea.line2 = 1;
2049
2050 correct_range(&ea);
2051
2052#ifdef FEAT_FOLDING
Bram Moolenaara3306952016-01-02 21:41:06 +01002053 if (((ea.argt & WHOLEFOLD) || ea.addr_count >= 2) && !global_busy
2054 && ea.addr_type == ADDR_LINES)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002055 {
2056 /* Put the first line at the start of a closed fold, put the last line
2057 * at the end of a closed fold. */
2058 (void)hasFolding(ea.line1, &ea.line1, NULL);
2059 (void)hasFolding(ea.line2, NULL, &ea.line2);
2060 }
2061#endif
2062
2063#ifdef FEAT_QUICKFIX
2064 /*
Bram Moolenaar86b68352004-12-27 21:59:20 +00002065 * For the ":make" and ":grep" commands we insert the 'makeprg'/'grepprg'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002066 * option here, so things like % get expanded.
2067 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002068 p = replace_makeprg(&ea, p, cmdlinep);
2069 if (p == NULL)
2070 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002071#endif
2072
2073 /*
2074 * Skip to start of argument.
2075 * Don't do this for the ":!" command, because ":!! -l" needs the space.
2076 */
2077 if (ea.cmdidx == CMD_bang)
2078 ea.arg = p;
2079 else
2080 ea.arg = skipwhite(p);
2081
Bram Moolenaar379fb762018-08-30 15:58:28 +02002082 // ":file" cannot be run with an argument when "curbuf_lock" is set
2083 if (ea.cmdidx == CMD_file && *ea.arg != NUL && curbuf_locked())
2084 goto doend;
2085
Bram Moolenaar071d4272004-06-13 20:20:40 +00002086 /*
2087 * Check for "++opt=val" argument.
2088 * Must be first, allow ":w ++enc=utf8 !cmd"
2089 */
2090 if (ea.argt & ARGOPT)
2091 while (ea.arg[0] == '+' && ea.arg[1] == '+')
2092 if (getargopt(&ea) == FAIL && !ni)
2093 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002094 errormsg = _(e_invarg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002095 goto doend;
2096 }
2097
2098 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
2099 {
2100 if (*ea.arg == '>') /* append */
2101 {
2102 if (*++ea.arg != '>') /* typed wrong */
2103 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002104 errormsg = _("E494: Use w or w>>");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002105 goto doend;
2106 }
2107 ea.arg = skipwhite(ea.arg + 1);
2108 ea.append = TRUE;
2109 }
2110 else if (*ea.arg == '!' && ea.cmdidx == CMD_write) /* :w !filter */
2111 {
2112 ++ea.arg;
2113 ea.usefilter = TRUE;
2114 }
2115 }
2116
2117 if (ea.cmdidx == CMD_read)
2118 {
2119 if (ea.forceit)
2120 {
2121 ea.usefilter = TRUE; /* :r! filter if ea.forceit */
2122 ea.forceit = FALSE;
2123 }
2124 else if (*ea.arg == '!') /* :r !filter */
2125 {
2126 ++ea.arg;
2127 ea.usefilter = TRUE;
2128 }
2129 }
2130
2131 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
2132 {
2133 ea.amount = 1;
2134 while (*ea.arg == *ea.cmd) /* count number of '>' or '<' */
2135 {
2136 ++ea.arg;
2137 ++ea.amount;
2138 }
2139 ea.arg = skipwhite(ea.arg);
2140 }
2141
2142 /*
2143 * Check for "+command" argument, before checking for next command.
2144 * Don't do this for ":read !cmd" and ":write !cmd".
2145 */
2146 if ((ea.argt & EDITCMD) && !ea.usefilter)
2147 ea.do_ecmd_cmd = getargcmd(&ea.arg);
2148
2149 /*
2150 * Check for '|' to separate commands and '"' to start comments.
2151 * Don't do this for ":read !cmd" and ":write !cmd".
2152 */
2153 if ((ea.argt & TRLBAR) && !ea.usefilter)
2154 separate_nextcmd(&ea);
2155
2156 /*
2157 * Check for <newline> to end a shell command.
Bram Moolenaardf177f62005-02-22 08:39:57 +00002158 * Also do this for ":read !cmd", ":write !cmd" and ":global".
2159 * Any others?
Bram Moolenaar071d4272004-06-13 20:20:40 +00002160 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00002161 else if (ea.cmdidx == CMD_bang
Bram Moolenaar67883b42017-07-27 22:57:00 +02002162 || ea.cmdidx == CMD_terminal
Bram Moolenaardf177f62005-02-22 08:39:57 +00002163 || ea.cmdidx == CMD_global
2164 || ea.cmdidx == CMD_vglobal
2165 || ea.usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002166 {
2167 for (p = ea.arg; *p; ++p)
2168 {
2169 /* Remove one backslash before a newline, so that it's possible to
2170 * pass a newline to the shell and also a newline that is preceded
2171 * with a backslash. This makes it impossible to end a shell
2172 * command in a backslash, but that doesn't appear useful.
2173 * Halving the number of backslashes is incompatible with previous
2174 * versions. */
2175 if (*p == '\\' && p[1] == '\n')
Bram Moolenaara7241f52008-06-24 20:39:31 +00002176 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002177 else if (*p == '\n')
2178 {
2179 ea.nextcmd = p + 1;
2180 *p = NUL;
2181 break;
2182 }
2183 }
2184 }
2185
2186 if ((ea.argt & DFLALL) && ea.addr_count == 0)
2187 {
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002188 buf_T *buf;
2189
Bram Moolenaar071d4272004-06-13 20:20:40 +00002190 ea.line1 = 1;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002191 switch (ea.addr_type)
2192 {
2193 case ADDR_LINES:
2194 ea.line2 = curbuf->b_ml.ml_line_count;
2195 break;
2196 case ADDR_LOADED_BUFFERS:
2197 buf = firstbuf;
2198 while (buf->b_next != NULL && buf->b_ml.ml_mfp == NULL)
2199 buf = buf->b_next;
2200 ea.line1 = buf->b_fnum;
2201 buf = lastbuf;
2202 while (buf->b_prev != NULL && buf->b_ml.ml_mfp == NULL)
2203 buf = buf->b_prev;
2204 ea.line2 = buf->b_fnum;
2205 break;
2206 case ADDR_BUFFERS:
2207 ea.line1 = firstbuf->b_fnum;
2208 ea.line2 = lastbuf->b_fnum;
2209 break;
2210 case ADDR_WINDOWS:
2211 ea.line2 = LAST_WIN_NR;
2212 break;
2213 case ADDR_TABS:
2214 ea.line2 = LAST_TAB_NR;
2215 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01002216 case ADDR_TABS_RELATIVE:
2217 ea.line2 = 1;
2218 break;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002219 case ADDR_ARGUMENTS:
2220 if (ARGCOUNT == 0)
2221 ea.line1 = ea.line2 = 0;
2222 else
2223 ea.line2 = ARGCOUNT;
2224 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02002225#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02002226 case ADDR_QUICKFIX:
2227 ea.line2 = qf_get_size(&ea);
2228 if (ea.line2 == 0)
2229 ea.line2 = 1;
2230 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02002231#endif
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002232 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002233 }
2234
2235 /* accept numbered register only when no count allowed (:put) */
2236 if ( (ea.argt & REGSTR)
2237 && *ea.arg != NUL
Bram Moolenaar958636c2014-10-21 20:01:58 +02002238 /* Do not allow register = for user commands */
2239 && (!IS_USER_CMDIDX(ea.cmdidx) || *ea.arg != '=')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002240 && !((ea.argt & COUNT) && VIM_ISDIGIT(*ea.arg)))
2241 {
Bram Moolenaar85de2062011-05-05 14:26:41 +02002242#ifndef FEAT_CLIPBOARD
2243 /* check these explicitly for a more specific error message */
2244 if (*ea.arg == '*' || *ea.arg == '+')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002245 {
Bram Moolenaar99b12722019-01-14 20:16:40 +01002246 errormsg = _(e_invalidreg);
Bram Moolenaar85de2062011-05-05 14:26:41 +02002247 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002248 }
2249#endif
Bram Moolenaar958636c2014-10-21 20:01:58 +02002250 if (valid_yank_reg(*ea.arg, (ea.cmdidx != CMD_put
2251 && !IS_USER_CMDIDX(ea.cmdidx))))
Bram Moolenaar85de2062011-05-05 14:26:41 +02002252 {
2253 ea.regname = *ea.arg++;
2254#ifdef FEAT_EVAL
2255 /* for '=' register: accept the rest of the line as an expression */
2256 if (ea.arg[-1] == '=' && ea.arg[0] != NUL)
2257 {
2258 set_expr_line(vim_strsave(ea.arg));
2259 ea.arg += STRLEN(ea.arg);
2260 }
2261#endif
2262 ea.arg = skipwhite(ea.arg);
2263 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002264 }
2265
2266 /*
2267 * Check for a count. When accepting a BUFNAME, don't use "123foo" as a
2268 * count, it's a buffer name.
2269 */
2270 if ((ea.argt & COUNT) && VIM_ISDIGIT(*ea.arg)
2271 && (!(ea.argt & BUFNAME) || *(p = skipdigits(ea.arg)) == NUL
Bram Moolenaar1c465442017-03-12 20:10:05 +01002272 || VIM_ISWHITE(*p)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002273 {
2274 n = getdigits(&ea.arg);
2275 ea.arg = skipwhite(ea.arg);
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002276 if (n <= 0 && !ni && (ea.argt & ZEROR) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002277 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002278 errormsg = _(e_zerocount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002279 goto doend;
2280 }
2281 if (ea.argt & NOTADR) /* e.g. :buffer 2, :sleep 3 */
2282 {
2283 ea.line2 = n;
2284 if (ea.addr_count == 0)
2285 ea.addr_count = 1;
2286 }
2287 else
2288 {
2289 ea.line1 = ea.line2;
2290 ea.line2 += n - 1;
2291 ++ea.addr_count;
2292 /*
2293 * Be vi compatible: no error message for out of range.
2294 */
Bram Moolenaar2f72c702017-01-29 14:48:10 +01002295 if (ea.addr_type == ADDR_LINES
2296 && ea.line2 > curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002297 ea.line2 = curbuf->b_ml.ml_line_count;
2298 }
2299 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00002300
2301 /*
2302 * Check for flags: 'l', 'p' and '#'.
2303 */
2304 if (ea.argt & EXFLAGS)
2305 get_flags(&ea);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002306 /* no arguments allowed */
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002307 if (!ni && !(ea.argt & EXTRA) && *ea.arg != NUL
Bram Moolenaara2031822006-03-07 22:29:51 +00002308 && *ea.arg != '"' && (*ea.arg != '|' || (ea.argt & TRLBAR) == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002309 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002310 errormsg = _(e_trailing);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002311 goto doend;
2312 }
2313
2314 if (!ni && (ea.argt & NEEDARG) && *ea.arg == NUL)
2315 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002316 errormsg = _(e_argreq);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002317 goto doend;
2318 }
2319
2320#ifdef FEAT_EVAL
2321 /*
2322 * Skip the command when it's not going to be executed.
2323 * The commands like :if, :endif, etc. always need to be executed.
2324 * Also make an exception for commands that handle a trailing command
2325 * themselves.
2326 */
2327 if (ea.skip)
2328 {
2329 switch (ea.cmdidx)
2330 {
2331 /* commands that need evaluation */
2332 case CMD_while:
2333 case CMD_endwhile:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00002334 case CMD_for:
2335 case CMD_endfor:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002336 case CMD_if:
2337 case CMD_elseif:
2338 case CMD_else:
2339 case CMD_endif:
2340 case CMD_try:
2341 case CMD_catch:
2342 case CMD_finally:
2343 case CMD_endtry:
2344 case CMD_function:
2345 break;
2346
2347 /* Commands that handle '|' themselves. Check: A command should
2348 * either have the TRLBAR flag, appear in this list or appear in
2349 * the list at ":help :bar". */
2350 case CMD_aboveleft:
2351 case CMD_and:
2352 case CMD_belowright:
2353 case CMD_botright:
2354 case CMD_browse:
2355 case CMD_call:
2356 case CMD_confirm:
2357 case CMD_delfunction:
2358 case CMD_djump:
2359 case CMD_dlist:
2360 case CMD_dsearch:
2361 case CMD_dsplit:
2362 case CMD_echo:
2363 case CMD_echoerr:
2364 case CMD_echomsg:
2365 case CMD_echon:
2366 case CMD_execute:
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002367 case CMD_filter:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002368 case CMD_help:
2369 case CMD_hide:
2370 case CMD_ijump:
2371 case CMD_ilist:
2372 case CMD_isearch:
2373 case CMD_isplit:
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002374 case CMD_keepalt:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002375 case CMD_keepjumps:
2376 case CMD_keepmarks:
Bram Moolenaara939e432013-11-09 05:30:26 +01002377 case CMD_keeppatterns:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002378 case CMD_leftabove:
2379 case CMD_let:
2380 case CMD_lockmarks:
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002381 case CMD_lua:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002382 case CMD_match:
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002383 case CMD_mzscheme:
Bram Moolenaar5803ae62014-03-23 16:04:02 +01002384 case CMD_noautocmd:
2385 case CMD_noswapfile:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002386 case CMD_perl:
2387 case CMD_psearch:
2388 case CMD_python:
Bram Moolenaar368373e2010-07-19 20:46:22 +02002389 case CMD_py3:
Bram Moolenaarb6590522010-07-21 16:00:43 +02002390 case CMD_python3:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002391 case CMD_return:
2392 case CMD_rightbelow:
2393 case CMD_ruby:
2394 case CMD_silent:
2395 case CMD_smagic:
2396 case CMD_snomagic:
2397 case CMD_substitute:
2398 case CMD_syntax:
Bram Moolenaara226a6d2006-02-26 23:59:20 +00002399 case CMD_tab:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002400 case CMD_tcl:
2401 case CMD_throw:
2402 case CMD_tilde:
2403 case CMD_topleft:
2404 case CMD_unlet:
2405 case CMD_verbose:
2406 case CMD_vertical:
Bram Moolenaar12bde492011-06-13 01:19:56 +02002407 case CMD_wincmd:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002408 break;
2409
2410 default: goto doend;
2411 }
2412 }
2413#endif
2414
2415 if (ea.argt & XFILE)
2416 {
2417 if (expand_filename(&ea, cmdlinep, &errormsg) == FAIL)
2418 goto doend;
2419 }
2420
Bram Moolenaar071d4272004-06-13 20:20:40 +00002421 /*
2422 * Accept buffer name. Cannot be used at the same time with a buffer
2423 * number. Don't do this for a user command.
2424 */
2425 if ((ea.argt & BUFNAME) && *ea.arg != NUL && ea.addr_count == 0
Bram Moolenaar958636c2014-10-21 20:01:58 +02002426 && !IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002427 {
2428 /*
2429 * :bdelete, :bwipeout and :bunload take several arguments, separated
2430 * by spaces: find next space (skipping over escaped characters).
2431 * The others take one argument: ignore trailing spaces.
2432 */
2433 if (ea.cmdidx == CMD_bdelete || ea.cmdidx == CMD_bwipeout
2434 || ea.cmdidx == CMD_bunload)
2435 p = skiptowhite_esc(ea.arg);
2436 else
2437 {
2438 p = ea.arg + STRLEN(ea.arg);
Bram Moolenaar1c465442017-03-12 20:10:05 +01002439 while (p > ea.arg && VIM_ISWHITE(p[-1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002440 --p;
2441 }
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002442 ea.line2 = buflist_findpat(ea.arg, p, (ea.argt & BUFUNL) != 0,
2443 FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002444 if (ea.line2 < 0) /* failed */
2445 goto doend;
2446 ea.addr_count = 1;
2447 ea.arg = skipwhite(p);
2448 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002449
Bram Moolenaar2be57332018-02-13 18:05:18 +01002450 /* The :try command saves the emsg_silent flag, reset it here when
2451 * ":silent! try" was used, it should only apply to :try itself. */
Bram Moolenaareffed932018-08-14 13:38:17 +02002452 if (ea.cmdidx == CMD_try && ea.did_esilent > 0)
Bram Moolenaar2be57332018-02-13 18:05:18 +01002453 {
Bram Moolenaareffed932018-08-14 13:38:17 +02002454 emsg_silent -= ea.did_esilent;
Bram Moolenaar2be57332018-02-13 18:05:18 +01002455 if (emsg_silent < 0)
2456 emsg_silent = 0;
Bram Moolenaareffed932018-08-14 13:38:17 +02002457 ea.did_esilent = 0;
Bram Moolenaar2be57332018-02-13 18:05:18 +01002458 }
2459
Bram Moolenaar071d4272004-06-13 20:20:40 +00002460/*
Bram Moolenaar2be57332018-02-13 18:05:18 +01002461 * 7. Execute the command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002462 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002463
Bram Moolenaar958636c2014-10-21 20:01:58 +02002464 if (IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002465 {
2466 /*
2467 * Execute a user-defined command.
2468 */
2469 do_ucmd(&ea);
2470 }
2471 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002472 {
2473 /*
Bram Moolenaarac9fb182019-04-27 13:04:13 +02002474 * Call the function to execute the builtin command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002475 */
2476 ea.errmsg = NULL;
2477 (cmdnames[ea.cmdidx].cmd_func)(&ea);
2478 if (ea.errmsg != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002479 errormsg = _(ea.errmsg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002480 }
2481
2482#ifdef FEAT_EVAL
2483 /*
2484 * If the command just executed called do_cmdline(), any throw or ":return"
2485 * or ":finish" encountered there must also check the cstack of the still
2486 * active do_cmdline() that called this do_one_cmd(). Rethrow an uncaught
2487 * exception, or reanimate a returned function or finished script file and
2488 * return or finish it again.
2489 */
2490 if (need_rethrow)
2491 do_throw(cstack);
2492 else if (check_cstack)
2493 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002494 if (source_finished(fgetline, cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002495 do_finish(&ea, TRUE);
Bram Moolenaar89d40322006-08-29 15:30:07 +00002496 else if (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002497 && current_func_returned())
2498 do_return(&ea, TRUE, FALSE, NULL);
2499 }
2500 need_rethrow = check_cstack = FALSE;
2501#endif
2502
2503doend:
2504 if (curwin->w_cursor.lnum == 0) /* can happen with zero line number */
Bram Moolenaar6de5e122017-04-20 21:55:44 +02002505 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002506 curwin->w_cursor.lnum = 1;
Bram Moolenaar6de5e122017-04-20 21:55:44 +02002507 curwin->w_cursor.col = 0;
2508 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002509
2510 if (errormsg != NULL && *errormsg != NUL && !did_emsg)
2511 {
2512 if (sourcing)
2513 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002514 if (errormsg != (char *)IObuff)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002515 {
2516 STRCPY(IObuff, errormsg);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002517 errormsg = (char *)IObuff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002518 }
Bram Moolenaara6f4d612011-09-21 19:10:46 +02002519 append_command(*cmdlinep);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002520 }
2521 emsg(errormsg);
2522 }
2523#ifdef FEAT_EVAL
2524 do_errthrow(cstack,
Bram Moolenaar958636c2014-10-21 20:01:58 +02002525 (ea.cmdidx != CMD_SIZE && !IS_USER_CMDIDX(ea.cmdidx))
2526 ? cmdnames[(int)ea.cmdidx].cmd_name : (char_u *)NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002527#endif
2528
Bram Moolenaareffed932018-08-14 13:38:17 +02002529 if (ea.verbose_save >= 0)
2530 p_verbose = ea.verbose_save;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002531
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002532 free_cmdmod();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002533 cmdmod = save_cmdmod;
Bram Moolenaar9a2c0912019-03-30 14:26:18 +01002534 reg_executing = save_reg_executing;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002535
Bram Moolenaareffed932018-08-14 13:38:17 +02002536 if (ea.save_msg_silent != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002537 {
2538 /* messages could be enabled for a serious error, need to check if the
2539 * counters don't become negative */
Bram Moolenaareffed932018-08-14 13:38:17 +02002540 if (!did_emsg || msg_silent > ea.save_msg_silent)
2541 msg_silent = ea.save_msg_silent;
2542 emsg_silent -= ea.did_esilent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002543 if (emsg_silent < 0)
2544 emsg_silent = 0;
2545 /* Restore msg_scroll, it's set by file I/O commands, even when no
2546 * message is actually displayed. */
2547 msg_scroll = save_msg_scroll;
Bram Moolenaar77ab2802009-04-22 12:44:48 +00002548
2549 /* "silent reg" or "silent echo x" inside "redir" leaves msg_col
2550 * somewhere in the line. Put it back in the first column. */
2551 if (redirecting())
2552 msg_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002553 }
2554
Bram Moolenaar7171abe2004-10-11 10:06:20 +00002555#ifdef HAVE_SANDBOX
Bram Moolenaareffed932018-08-14 13:38:17 +02002556 if (ea.did_sandbox)
Bram Moolenaar7171abe2004-10-11 10:06:20 +00002557 --sandbox;
2558#endif
2559
Bram Moolenaar071d4272004-06-13 20:20:40 +00002560 if (ea.nextcmd && *ea.nextcmd == NUL) /* not really a next command */
2561 ea.nextcmd = NULL;
2562
2563#ifdef FEAT_EVAL
2564 --ex_nesting_level;
2565#endif
2566
2567 return ea.nextcmd;
2568}
2569#if (_MSC_VER == 1200)
Bram Moolenaar281bdce2005-01-25 21:53:18 +00002570 #pragma optimize( "", on )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002571#endif
2572
2573/*
Bram Moolenaareffed932018-08-14 13:38:17 +02002574 * Parse and skip over command modifiers:
2575 * - update eap->cmd
2576 * - store flags in "cmdmod".
2577 * - Set ex_pressedreturn for an empty command line.
2578 * - set msg_silent for ":silent"
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002579 * - set 'eventignore' to "all" for ":noautocmd"
Bram Moolenaareffed932018-08-14 13:38:17 +02002580 * - set p_verbose for ":verbose"
2581 * - Increment "sandbox" for ":sandbox"
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002582 * When "skip_only" is TRUE the global variables are not changed, except for
2583 * "cmdmod".
Bram Moolenaareffed932018-08-14 13:38:17 +02002584 * Return FAIL when the command is not to be executed.
2585 * May set "errormsg" to an error message.
2586 */
2587 int
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002588parse_command_modifiers(exarg_T *eap, char **errormsg, int skip_only)
Bram Moolenaareffed932018-08-14 13:38:17 +02002589{
2590 char_u *p;
2591
2592 vim_memset(&cmdmod, 0, sizeof(cmdmod));
2593 eap->verbose_save = -1;
2594 eap->save_msg_silent = -1;
2595
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002596 // Repeat until no more command modifiers are found.
Bram Moolenaareffed932018-08-14 13:38:17 +02002597 for (;;)
2598 {
Bram Moolenaareffed932018-08-14 13:38:17 +02002599 while (*eap->cmd == ' ' || *eap->cmd == '\t' || *eap->cmd == ':')
2600 ++eap->cmd;
2601
2602 /* in ex mode, an empty line works like :+ */
2603 if (*eap->cmd == NUL && exmode_active
2604 && (getline_equal(eap->getline, eap->cookie, getexmodeline)
2605 || getline_equal(eap->getline, eap->cookie, getexline))
2606 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
2607 {
2608 eap->cmd = (char_u *)"+";
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002609 if (!skip_only)
2610 ex_pressedreturn = TRUE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002611 }
2612
2613 /* ignore comment and empty lines */
2614 if (*eap->cmd == '"')
2615 return FAIL;
2616 if (*eap->cmd == NUL)
2617 {
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002618 if (!skip_only)
2619 ex_pressedreturn = TRUE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002620 return FAIL;
2621 }
2622
Bram Moolenaareffed932018-08-14 13:38:17 +02002623 p = skip_range(eap->cmd, NULL);
2624 switch (*p)
2625 {
2626 /* When adding an entry, also modify cmd_exists(). */
2627 case 'a': if (!checkforcmd(&eap->cmd, "aboveleft", 3))
2628 break;
2629 cmdmod.split |= WSP_ABOVE;
2630 continue;
2631
2632 case 'b': if (checkforcmd(&eap->cmd, "belowright", 3))
2633 {
2634 cmdmod.split |= WSP_BELOW;
2635 continue;
2636 }
2637 if (checkforcmd(&eap->cmd, "browse", 3))
2638 {
2639#ifdef FEAT_BROWSE_CMD
2640 cmdmod.browse = TRUE;
2641#endif
2642 continue;
2643 }
2644 if (!checkforcmd(&eap->cmd, "botright", 2))
2645 break;
2646 cmdmod.split |= WSP_BOT;
2647 continue;
2648
2649 case 'c': if (!checkforcmd(&eap->cmd, "confirm", 4))
2650 break;
2651#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
2652 cmdmod.confirm = TRUE;
2653#endif
2654 continue;
2655
2656 case 'k': if (checkforcmd(&eap->cmd, "keepmarks", 3))
2657 {
2658 cmdmod.keepmarks = TRUE;
2659 continue;
2660 }
2661 if (checkforcmd(&eap->cmd, "keepalt", 5))
2662 {
2663 cmdmod.keepalt = TRUE;
2664 continue;
2665 }
2666 if (checkforcmd(&eap->cmd, "keeppatterns", 5))
2667 {
2668 cmdmod.keeppatterns = TRUE;
2669 continue;
2670 }
2671 if (!checkforcmd(&eap->cmd, "keepjumps", 5))
2672 break;
2673 cmdmod.keepjumps = TRUE;
2674 continue;
2675
2676 case 'f': /* only accept ":filter {pat} cmd" */
2677 {
2678 char_u *reg_pat;
2679
2680 if (!checkforcmd(&p, "filter", 4)
2681 || *p == NUL || ends_excmd(*p))
2682 break;
2683 if (*p == '!')
2684 {
2685 cmdmod.filter_force = TRUE;
2686 p = skipwhite(p + 1);
2687 if (*p == NUL || ends_excmd(*p))
2688 break;
2689 }
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002690 if (skip_only)
2691 p = skip_vimgrep_pat(p, NULL, NULL);
2692 else
2693 // NOTE: This puts a NUL after the pattern.
2694 p = skip_vimgrep_pat(p, &reg_pat, NULL);
Bram Moolenaareffed932018-08-14 13:38:17 +02002695 if (p == NULL || *p == NUL)
2696 break;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002697 if (!skip_only)
2698 {
2699 cmdmod.filter_regmatch.regprog =
Bram Moolenaareffed932018-08-14 13:38:17 +02002700 vim_regcomp(reg_pat, RE_MAGIC);
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002701 if (cmdmod.filter_regmatch.regprog == NULL)
2702 break;
2703 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002704 eap->cmd = p;
2705 continue;
2706 }
2707
2708 /* ":hide" and ":hide | cmd" are not modifiers */
2709 case 'h': if (p != eap->cmd || !checkforcmd(&p, "hide", 3)
2710 || *p == NUL || ends_excmd(*p))
2711 break;
2712 eap->cmd = p;
2713 cmdmod.hide = TRUE;
2714 continue;
2715
2716 case 'l': if (checkforcmd(&eap->cmd, "lockmarks", 3))
2717 {
2718 cmdmod.lockmarks = TRUE;
2719 continue;
2720 }
2721
2722 if (!checkforcmd(&eap->cmd, "leftabove", 5))
2723 break;
2724 cmdmod.split |= WSP_ABOVE;
2725 continue;
2726
2727 case 'n': if (checkforcmd(&eap->cmd, "noautocmd", 3))
2728 {
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002729 if (cmdmod.save_ei == NULL && !skip_only)
Bram Moolenaareffed932018-08-14 13:38:17 +02002730 {
2731 /* Set 'eventignore' to "all". Restore the
2732 * existing option value later. */
2733 cmdmod.save_ei = vim_strsave(p_ei);
2734 set_string_option_direct((char_u *)"ei", -1,
2735 (char_u *)"all", OPT_FREE, SID_NONE);
2736 }
2737 continue;
2738 }
2739 if (!checkforcmd(&eap->cmd, "noswapfile", 3))
2740 break;
2741 cmdmod.noswapfile = TRUE;
2742 continue;
2743
2744 case 'r': if (!checkforcmd(&eap->cmd, "rightbelow", 6))
2745 break;
2746 cmdmod.split |= WSP_BELOW;
2747 continue;
2748
2749 case 's': if (checkforcmd(&eap->cmd, "sandbox", 3))
2750 {
2751#ifdef HAVE_SANDBOX
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002752 if (!skip_only)
2753 {
2754 if (!eap->did_sandbox)
2755 ++sandbox;
2756 eap->did_sandbox = TRUE;
2757 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002758#endif
2759 continue;
2760 }
2761 if (!checkforcmd(&eap->cmd, "silent", 3))
2762 break;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002763 if (!skip_only)
2764 {
2765 if (eap->save_msg_silent == -1)
2766 eap->save_msg_silent = msg_silent;
2767 ++msg_silent;
2768 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002769 if (*eap->cmd == '!' && !VIM_ISWHITE(eap->cmd[-1]))
2770 {
2771 /* ":silent!", but not "silent !cmd" */
2772 eap->cmd = skipwhite(eap->cmd + 1);
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002773 if (!skip_only)
2774 {
2775 ++emsg_silent;
2776 ++eap->did_esilent;
2777 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002778 }
2779 continue;
2780
2781 case 't': if (checkforcmd(&p, "tab", 3))
2782 {
Bram Moolenaar548e5982018-12-26 21:45:00 +01002783 if (!skip_only)
Bram Moolenaareffed932018-08-14 13:38:17 +02002784 {
Bram Moolenaar548e5982018-12-26 21:45:00 +01002785 long tabnr = get_address(eap, &eap->cmd,
2786 ADDR_TABS, eap->skip,
2787 skip_only, FALSE, 1);
2788 if (tabnr == MAXLNUM)
2789 cmdmod.tab = tabpage_index(curtab) + 1;
2790 else
Bram Moolenaareffed932018-08-14 13:38:17 +02002791 {
Bram Moolenaar548e5982018-12-26 21:45:00 +01002792 if (tabnr < 0 || tabnr > LAST_TAB_NR)
2793 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002794 *errormsg = _(e_invrange);
Bram Moolenaar548e5982018-12-26 21:45:00 +01002795 return FAIL;
2796 }
2797 cmdmod.tab = tabnr + 1;
Bram Moolenaareffed932018-08-14 13:38:17 +02002798 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002799 }
2800 eap->cmd = p;
2801 continue;
2802 }
2803 if (!checkforcmd(&eap->cmd, "topleft", 2))
2804 break;
2805 cmdmod.split |= WSP_TOP;
2806 continue;
2807
2808 case 'u': if (!checkforcmd(&eap->cmd, "unsilent", 3))
2809 break;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002810 if (!skip_only)
2811 {
2812 if (eap->save_msg_silent == -1)
2813 eap->save_msg_silent = msg_silent;
2814 msg_silent = 0;
2815 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002816 continue;
2817
2818 case 'v': if (checkforcmd(&eap->cmd, "vertical", 4))
2819 {
2820 cmdmod.split |= WSP_VERT;
2821 continue;
2822 }
2823 if (!checkforcmd(&p, "verbose", 4))
2824 break;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002825 if (!skip_only)
2826 {
2827 if (eap->verbose_save < 0)
2828 eap->verbose_save = p_verbose;
2829 if (vim_isdigit(*eap->cmd))
2830 p_verbose = atoi((char *)eap->cmd);
2831 else
2832 p_verbose = 1;
2833 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002834 eap->cmd = p;
2835 continue;
2836 }
2837 break;
2838 }
2839
2840 return OK;
2841}
2842
2843/*
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002844 * Free contents of "cmdmod".
2845 */
2846 static void
2847free_cmdmod(void)
2848{
2849 if (cmdmod.save_ei != NULL)
2850 {
2851 /* Restore 'eventignore' to the value before ":noautocmd". */
2852 set_string_option_direct((char_u *)"ei", -1, cmdmod.save_ei,
2853 OPT_FREE, SID_NONE);
2854 free_string_option(cmdmod.save_ei);
2855 }
2856
2857 if (cmdmod.filter_regmatch.regprog != NULL)
2858 vim_regfree(cmdmod.filter_regmatch.regprog);
2859}
2860
2861/*
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002862 * Parse the address range, if any, in "eap".
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02002863 * May set the last search pattern, unless "silent" is TRUE.
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002864 * Return FAIL and set "errormsg" or return OK.
2865 */
2866 int
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002867parse_cmd_address(exarg_T *eap, char **errormsg, int silent)
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002868{
2869 int address_count = 1;
2870 linenr_T lnum;
2871
2872 // Repeat for all ',' or ';' separated addresses.
2873 for (;;)
2874 {
2875 eap->line1 = eap->line2;
2876 switch (eap->addr_type)
2877 {
2878 case ADDR_LINES:
2879 // default is current line number
2880 eap->line2 = curwin->w_cursor.lnum;
2881 break;
2882 case ADDR_WINDOWS:
2883 eap->line2 = CURRENT_WIN_NR;
2884 break;
2885 case ADDR_ARGUMENTS:
2886 eap->line2 = curwin->w_arg_idx + 1;
2887 if (eap->line2 > ARGCOUNT)
2888 eap->line2 = ARGCOUNT;
2889 break;
2890 case ADDR_LOADED_BUFFERS:
2891 case ADDR_BUFFERS:
2892 eap->line2 = curbuf->b_fnum;
2893 break;
2894 case ADDR_TABS:
2895 eap->line2 = CURRENT_TAB_NR;
2896 break;
2897 case ADDR_TABS_RELATIVE:
2898 eap->line2 = 1;
2899 break;
2900#ifdef FEAT_QUICKFIX
2901 case ADDR_QUICKFIX:
2902 eap->line2 = qf_get_cur_valid_idx(eap);
2903 break;
2904#endif
2905 }
2906 eap->cmd = skipwhite(eap->cmd);
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02002907 lnum = get_address(eap, &eap->cmd, eap->addr_type, eap->skip, silent,
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002908 eap->addr_count == 0, address_count++);
2909 if (eap->cmd == NULL) // error detected
2910 return FAIL;
2911 if (lnum == MAXLNUM)
2912 {
2913 if (*eap->cmd == '%') // '%' - all lines
2914 {
2915 ++eap->cmd;
2916 switch (eap->addr_type)
2917 {
2918 case ADDR_LINES:
2919 eap->line1 = 1;
2920 eap->line2 = curbuf->b_ml.ml_line_count;
2921 break;
2922 case ADDR_LOADED_BUFFERS:
2923 {
2924 buf_T *buf = firstbuf;
2925
2926 while (buf->b_next != NULL
2927 && buf->b_ml.ml_mfp == NULL)
2928 buf = buf->b_next;
2929 eap->line1 = buf->b_fnum;
2930 buf = lastbuf;
2931 while (buf->b_prev != NULL
2932 && buf->b_ml.ml_mfp == NULL)
2933 buf = buf->b_prev;
2934 eap->line2 = buf->b_fnum;
2935 break;
2936 }
2937 case ADDR_BUFFERS:
2938 eap->line1 = firstbuf->b_fnum;
2939 eap->line2 = lastbuf->b_fnum;
2940 break;
2941 case ADDR_WINDOWS:
2942 case ADDR_TABS:
2943 if (IS_USER_CMDIDX(eap->cmdidx))
2944 {
2945 eap->line1 = 1;
2946 eap->line2 = eap->addr_type == ADDR_WINDOWS
2947 ? LAST_WIN_NR : LAST_TAB_NR;
2948 }
2949 else
2950 {
2951 // there is no Vim command which uses '%' and
2952 // ADDR_WINDOWS or ADDR_TABS
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002953 *errormsg = _(e_invrange);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002954 return FAIL;
2955 }
2956 break;
2957 case ADDR_TABS_RELATIVE:
Bram Moolenaar51a74542018-12-02 18:21:49 +01002958 case ADDR_OTHER:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002959 *errormsg = _(e_invrange);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002960 return FAIL;
2961 case ADDR_ARGUMENTS:
2962 if (ARGCOUNT == 0)
2963 eap->line1 = eap->line2 = 0;
2964 else
2965 {
2966 eap->line1 = 1;
2967 eap->line2 = ARGCOUNT;
2968 }
2969 break;
2970#ifdef FEAT_QUICKFIX
2971 case ADDR_QUICKFIX:
2972 eap->line1 = 1;
2973 eap->line2 = qf_get_size(eap);
2974 if (eap->line2 == 0)
2975 eap->line2 = 1;
2976 break;
2977#endif
2978 }
2979 ++eap->addr_count;
2980 }
2981 else if (*eap->cmd == '*' && vim_strchr(p_cpo, CPO_STAR) == NULL)
2982 {
2983 pos_T *fp;
2984
2985 // '*' - visual area
2986 if (eap->addr_type != ADDR_LINES)
2987 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002988 *errormsg = _(e_invrange);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002989 return FAIL;
2990 }
2991
2992 ++eap->cmd;
2993 if (!eap->skip)
2994 {
2995 fp = getmark('<', FALSE);
2996 if (check_mark(fp) == FAIL)
2997 return FAIL;
2998 eap->line1 = fp->lnum;
2999 fp = getmark('>', FALSE);
3000 if (check_mark(fp) == FAIL)
3001 return FAIL;
3002 eap->line2 = fp->lnum;
3003 ++eap->addr_count;
3004 }
3005 }
3006 }
3007 else
3008 eap->line2 = lnum;
3009 eap->addr_count++;
3010
3011 if (*eap->cmd == ';')
3012 {
3013 if (!eap->skip)
3014 {
3015 curwin->w_cursor.lnum = eap->line2;
3016 // don't leave the cursor on an illegal line or column
3017 check_cursor();
3018 }
3019 }
3020 else if (*eap->cmd != ',')
3021 break;
3022 ++eap->cmd;
3023 }
3024
3025 // One address given: set start and end lines.
3026 if (eap->addr_count == 1)
3027 {
3028 eap->line1 = eap->line2;
3029 // ... but only implicit: really no address given
3030 if (lnum == MAXLNUM)
3031 eap->addr_count = 0;
3032 }
3033 return OK;
3034}
3035
3036/*
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00003037 * Check for an Ex command with optional tail.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003038 * If there is a match advance "pp" to the argument and return TRUE.
3039 */
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00003040 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003041checkforcmd(
3042 char_u **pp, /* start of command */
3043 char *cmd, /* name of command */
3044 int len) /* required length */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003045{
3046 int i;
3047
3048 for (i = 0; cmd[i] != NUL; ++i)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003049 if (((char_u *)cmd)[i] != (*pp)[i])
Bram Moolenaar071d4272004-06-13 20:20:40 +00003050 break;
3051 if (i >= len && !isalpha((*pp)[i]))
3052 {
3053 *pp = skipwhite(*pp + i);
3054 return TRUE;
3055 }
3056 return FALSE;
3057}
3058
3059/*
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003060 * Append "cmd" to the error message in IObuff.
3061 * Takes care of limiting the length and handling 0xa0, which would be
3062 * invisible otherwise.
3063 */
3064 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003065append_command(char_u *cmd)
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003066{
3067 char_u *s = cmd;
3068 char_u *d;
3069
3070 STRCAT(IObuff, ": ");
3071 d = IObuff + STRLEN(IObuff);
3072 while (*s != NUL && d - IObuff < IOSIZE - 7)
3073 {
Bram Moolenaar13505972019-01-24 15:04:48 +01003074 if (enc_utf8 ? (s[0] == 0xc2 && s[1] == 0xa0) : *s == 0xa0)
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003075 {
Bram Moolenaar13505972019-01-24 15:04:48 +01003076 s += enc_utf8 ? 2 : 1;
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003077 STRCPY(d, "<a0>");
3078 d += 4;
3079 }
3080 else
3081 MB_COPY_CHAR(s, d);
3082 }
3083 *d = NUL;
3084}
3085
3086/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003087 * Find an Ex command by its name, either built-in or user.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003088 * Start of the name can be found at eap->cmd.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003089 * Returns pointer to char after the command name.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003090 * "full" is set to TRUE if the whole command name matched.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003091 * Returns NULL for an ambiguous user command.
3092 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003093 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003094find_command(exarg_T *eap, int *full UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003095{
3096 int len;
3097 char_u *p;
Bram Moolenaardf177f62005-02-22 08:39:57 +00003098 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003099
3100 /*
3101 * Isolate the command and search for it in the command table.
Bram Moolenaar81870892007-11-11 18:17:28 +00003102 * Exceptions:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003103 * - the 'k' command can directly be followed by any character.
3104 * - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003105 * but :sre[wind] is another command, as are :scr[iptnames],
Bram Moolenaar071d4272004-06-13 20:20:40 +00003106 * :scs[cope], :sim[alt], :sig[ns] and :sil[ent].
Bram Moolenaardf177f62005-02-22 08:39:57 +00003107 * - the "d" command can directly be followed by 'l' or 'p' flag.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003108 */
3109 p = eap->cmd;
3110 if (*p == 'k')
3111 {
3112 eap->cmdidx = CMD_k;
3113 ++p;
3114 }
3115 else if (p[0] == 's'
Bram Moolenaar204b93f2015-08-04 22:02:51 +02003116 && ((p[1] == 'c' && (p[2] == NUL || (p[2] != 's' && p[2] != 'r'
3117 && (p[3] == NUL || (p[3] != 'i' && p[4] != 'p')))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003118 || p[1] == 'g'
3119 || (p[1] == 'i' && p[2] != 'm' && p[2] != 'l' && p[2] != 'g')
3120 || p[1] == 'I'
3121 || (p[1] == 'r' && p[2] != 'e')))
3122 {
3123 eap->cmdidx = CMD_substitute;
3124 ++p;
3125 }
3126 else
3127 {
3128 while (ASCII_ISALPHA(*p))
3129 ++p;
Bram Moolenaarb6590522010-07-21 16:00:43 +02003130 /* for python 3.x support ":py3", ":python3", ":py3file", etc. */
Bram Moolenaar55d5c032010-07-17 23:52:29 +02003131 if (eap->cmd[0] == 'p' && eap->cmd[1] == 'y')
Bram Moolenaarb6590522010-07-21 16:00:43 +02003132 while (ASCII_ISALNUM(*p))
3133 ++p;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02003134
Bram Moolenaar071d4272004-06-13 20:20:40 +00003135 /* check for non-alpha command */
3136 if (p == eap->cmd && vim_strchr((char_u *)"@*!=><&~#", *p) != NULL)
3137 ++p;
3138 len = (int)(p - eap->cmd);
Bram Moolenaardf177f62005-02-22 08:39:57 +00003139 if (*eap->cmd == 'd' && (p[-1] == 'l' || p[-1] == 'p'))
3140 {
3141 /* Check for ":dl", ":dell", etc. to ":deletel": that's
3142 * :delete with the 'l' flag. Same for 'p'. */
3143 for (i = 0; i < len; ++i)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003144 if (eap->cmd[i] != ((char_u *)"delete")[i])
Bram Moolenaardf177f62005-02-22 08:39:57 +00003145 break;
3146 if (i == len - 1)
3147 {
3148 --len;
3149 if (p[-1] == 'l')
3150 eap->flags |= EXFLAG_LIST;
3151 else
3152 eap->flags |= EXFLAG_PRINT;
3153 }
3154 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003155
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003156 if (ASCII_ISLOWER(eap->cmd[0]))
3157 {
Bram Moolenaar6c0c1e82017-03-25 15:07:43 +01003158 int c1 = eap->cmd[0];
3159 int c2 = eap->cmd[1];
3160
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003161 if (command_count != (int)CMD_SIZE)
3162 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003163 iemsg(_("E943: Command table needs to be updated, run 'make cmdidxs'"));
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003164 getout(1);
3165 }
3166
3167 /* Use a precomputed index for fast look-up in cmdnames[]
3168 * taking into account the first 2 letters of eap->cmd. */
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003169 eap->cmdidx = cmdidxs1[CharOrdLow(c1)];
3170 if (ASCII_ISLOWER(c2))
3171 eap->cmdidx += cmdidxs2[CharOrdLow(c1)][CharOrdLow(c2)];
3172 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003173 else
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003174 eap->cmdidx = CMD_bang;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003175
3176 for ( ; (int)eap->cmdidx < (int)CMD_SIZE;
3177 eap->cmdidx = (cmdidx_T)((int)eap->cmdidx + 1))
3178 if (STRNCMP(cmdnames[(int)eap->cmdidx].cmd_name, (char *)eap->cmd,
3179 (size_t)len) == 0)
3180 {
3181#ifdef FEAT_EVAL
3182 if (full != NULL
3183 && cmdnames[(int)eap->cmdidx].cmd_name[len] == NUL)
3184 *full = TRUE;
3185#endif
3186 break;
3187 }
3188
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003189 // Look for a user defined command as a last resort. Let ":Print" be
3190 // overruled by a user defined command.
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01003191 if ((eap->cmdidx == CMD_SIZE || eap->cmdidx == CMD_Print)
3192 && *eap->cmd >= 'A' && *eap->cmd <= 'Z')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003193 {
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003194 // User defined commands may contain digits.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003195 while (ASCII_ISALNUM(*p))
3196 ++p;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003197 p = find_ucmd(eap, p, full, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003198 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003199 if (p == eap->cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003200 eap->cmdidx = CMD_SIZE;
3201 }
3202
3203 return p;
3204}
3205
3206#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003207static struct cmdmod
3208{
3209 char *name;
3210 int minlen;
3211 int has_count; /* :123verbose :3tab */
3212} cmdmods[] = {
3213 {"aboveleft", 3, FALSE},
3214 {"belowright", 3, FALSE},
3215 {"botright", 2, FALSE},
3216 {"browse", 3, FALSE},
3217 {"confirm", 4, FALSE},
Bram Moolenaar7b668e82016-08-23 23:51:21 +02003218 {"filter", 4, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003219 {"hide", 3, FALSE},
3220 {"keepalt", 5, FALSE},
3221 {"keepjumps", 5, FALSE},
3222 {"keepmarks", 3, FALSE},
Bram Moolenaara939e432013-11-09 05:30:26 +01003223 {"keeppatterns", 5, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003224 {"leftabove", 5, FALSE},
3225 {"lockmarks", 3, FALSE},
Bram Moolenaarca9f9582008-09-18 10:44:28 +00003226 {"noautocmd", 3, FALSE},
Bram Moolenaar5803ae62014-03-23 16:04:02 +01003227 {"noswapfile", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003228 {"rightbelow", 6, FALSE},
3229 {"sandbox", 3, FALSE},
3230 {"silent", 3, FALSE},
3231 {"tab", 3, TRUE},
3232 {"topleft", 2, FALSE},
Bram Moolenaar8e258a42009-07-09 13:55:43 +00003233 {"unsilent", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003234 {"verbose", 4, TRUE},
3235 {"vertical", 4, FALSE},
3236};
3237
3238/*
3239 * Return length of a command modifier (including optional count).
3240 * Return zero when it's not a modifier.
3241 */
3242 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003243modifier_len(char_u *cmd)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003244{
3245 int i, j;
3246 char_u *p = cmd;
3247
3248 if (VIM_ISDIGIT(*cmd))
3249 p = skipwhite(skipdigits(cmd));
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00003250 for (i = 0; i < (int)(sizeof(cmdmods) / sizeof(struct cmdmod)); ++i)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003251 {
3252 for (j = 0; p[j] != NUL; ++j)
3253 if (p[j] != cmdmods[i].name[j])
3254 break;
Bram Moolenaar2d473ab2013-06-12 17:12:24 +02003255 if (!ASCII_ISALPHA(p[j]) && j >= cmdmods[i].minlen
Bram Moolenaared53fb92007-11-24 20:50:24 +00003256 && (p == cmd || cmdmods[i].has_count))
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00003257 return j + (int)(p - cmd);
Bram Moolenaared53fb92007-11-24 20:50:24 +00003258 }
3259 return 0;
3260}
3261
Bram Moolenaar071d4272004-06-13 20:20:40 +00003262/*
3263 * Return > 0 if an Ex command "name" exists.
3264 * Return 2 if there is an exact match.
3265 * Return 3 if there is an ambiguous match.
3266 */
3267 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003268cmd_exists(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003269{
3270 exarg_T ea;
3271 int full = FALSE;
3272 int i;
3273 int j;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003274 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003275
3276 /* Check command modifiers. */
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00003277 for (i = 0; i < (int)(sizeof(cmdmods) / sizeof(struct cmdmod)); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003278 {
3279 for (j = 0; name[j] != NUL; ++j)
3280 if (name[j] != cmdmods[i].name[j])
3281 break;
3282 if (name[j] == NUL && j >= cmdmods[i].minlen)
3283 return (cmdmods[i].name[j] == NUL ? 2 : 1);
3284 }
3285
Bram Moolenaara9587612006-05-04 21:47:50 +00003286 /* Check built-in commands and user defined commands.
3287 * For ":2match" and ":3match" we need to skip the number. */
3288 ea.cmd = (*name == '2' || *name == '3') ? name + 1 : name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003289 ea.cmdidx = (cmdidx_T)0;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003290 p = find_command(&ea, &full);
3291 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003292 return 3;
Bram Moolenaara9587612006-05-04 21:47:50 +00003293 if (vim_isdigit(*name) && ea.cmdidx != CMD_match)
3294 return 0;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003295 if (*skipwhite(p) != NUL)
3296 return 0; /* trailing garbage */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003297 return (ea.cmdidx == CMD_SIZE ? 0 : (full ? 2 : 1));
3298}
3299#endif
3300
3301/*
3302 * This is all pretty much copied from do_one_cmd(), with all the extra stuff
3303 * we don't need/want deleted. Maybe this could be done better if we didn't
3304 * repeat all this stuff. The only problem is that they may not stay
3305 * perfectly compatible with each other, but then the command line syntax
3306 * probably won't change that much -- webb.
3307 */
3308 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003309set_one_cmd_context(
3310 expand_T *xp,
3311 char_u *buff) /* buffer for command string */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003312{
3313 char_u *p;
3314 char_u *cmd, *arg;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003315 int len = 0;
3316 exarg_T ea;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003317#ifdef FEAT_CMDL_COMPL
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003318 int compl = EXPAND_NOTHING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003319 int delim;
3320#endif
3321 int forceit = FALSE;
3322 int usefilter = FALSE; /* filter instead of file name */
3323
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003324 ExpandInit(xp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003325 xp->xp_pattern = buff;
3326 xp->xp_context = EXPAND_COMMANDS; /* Default until we get past command */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003327 ea.argt = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003328
3329/*
Bram Moolenaar1c40a662014-11-27 16:38:11 +01003330 * 1. skip comment lines and leading space, colons or bars
Bram Moolenaar071d4272004-06-13 20:20:40 +00003331 */
3332 for (cmd = buff; vim_strchr((char_u *)" \t:|", *cmd) != NULL; cmd++)
3333 ;
3334 xp->xp_pattern = cmd;
3335
3336 if (*cmd == NUL)
3337 return NULL;
3338 if (*cmd == '"') /* ignore comment lines */
3339 {
3340 xp->xp_context = EXPAND_NOTHING;
3341 return NULL;
3342 }
3343
3344/*
Bram Moolenaar1c40a662014-11-27 16:38:11 +01003345 * 3. Skip over the range to find the command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003346 */
3347 cmd = skip_range(cmd, &xp->xp_context);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003348 xp->xp_pattern = cmd;
3349 if (*cmd == NUL)
3350 return NULL;
3351 if (*cmd == '"')
3352 {
3353 xp->xp_context = EXPAND_NOTHING;
3354 return NULL;
3355 }
3356
3357 if (*cmd == '|' || *cmd == '\n')
3358 return cmd + 1; /* There's another command */
3359
3360 /*
3361 * Isolate the command and search for it in the command table.
3362 * Exceptions:
3363 * - the 'k' command can directly be followed by any character, but
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003364 * do accept "keepmarks", "keepalt" and "keepjumps".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003365 * - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
3366 */
3367 if (*cmd == 'k' && cmd[1] != 'e')
3368 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003369 ea.cmdidx = CMD_k;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003370 p = cmd + 1;
3371 }
3372 else
3373 {
3374 p = cmd;
3375 while (ASCII_ISALPHA(*p) || *p == '*') /* Allow * wild card */
3376 ++p;
Bram Moolenaar23d1b622015-10-13 19:18:04 +02003377 /* a user command may contain digits */
3378 if (ASCII_ISUPPER(cmd[0]))
3379 while (ASCII_ISALNUM(*p) || *p == '*')
3380 ++p;
Bram Moolenaar9f5d6002013-06-02 19:22:13 +02003381 /* for python 3.x: ":py3*" commands completion */
3382 if (cmd[0] == 'p' && cmd[1] == 'y' && p == cmd + 2 && *p == '3')
Bram Moolenaar893b2d72013-12-11 17:44:38 +01003383 {
Bram Moolenaar9f5d6002013-06-02 19:22:13 +02003384 ++p;
Bram Moolenaar893b2d72013-12-11 17:44:38 +01003385 while (ASCII_ISALPHA(*p) || *p == '*')
3386 ++p;
3387 }
Bram Moolenaar23d1b622015-10-13 19:18:04 +02003388 /* check for non-alpha command */
3389 if (p == cmd && vim_strchr((char_u *)"@*!=><&~#", *p) != NULL)
3390 ++p;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003391 len = (int)(p - cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003392
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003393 if (len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003394 {
3395 xp->xp_context = EXPAND_UNSUCCESSFUL;
3396 return NULL;
3397 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003398 for (ea.cmdidx = (cmdidx_T)0; (int)ea.cmdidx < (int)CMD_SIZE;
Bram Moolenaar42b4dda2010-03-02 15:56:05 +01003399 ea.cmdidx = (cmdidx_T)((int)ea.cmdidx + 1))
3400 if (STRNCMP(cmdnames[(int)ea.cmdidx].cmd_name, cmd,
3401 (size_t)len) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003402 break;
3403
Bram Moolenaar071d4272004-06-13 20:20:40 +00003404 if (cmd[0] >= 'A' && cmd[0] <= 'Z')
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003405 while (ASCII_ISALNUM(*p) || *p == '*') // Allow * wild card
Bram Moolenaar071d4272004-06-13 20:20:40 +00003406 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003407 }
3408
3409 /*
3410 * If the cursor is touching the command, and it ends in an alpha-numeric
3411 * character, complete the command name.
3412 */
3413 if (*p == NUL && ASCII_ISALNUM(p[-1]))
3414 return NULL;
3415
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003416 if (ea.cmdidx == CMD_SIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003417 {
3418 if (*cmd == 's' && vim_strchr((char_u *)"cgriI", cmd[1]) != NULL)
3419 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003420 ea.cmdidx = CMD_substitute;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003421 p = cmd + 1;
3422 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003423 else if (cmd[0] >= 'A' && cmd[0] <= 'Z')
3424 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003425 ea.cmd = cmd;
3426 p = find_ucmd(&ea, p, NULL, xp,
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003427#if defined(FEAT_CMDL_COMPL)
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003428 &compl
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003429#else
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003430 NULL
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003431#endif
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003432 );
Bram Moolenaarebefac62005-12-28 22:39:57 +00003433 if (p == NULL)
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003434 ea.cmdidx = CMD_SIZE; // ambiguous user command
Bram Moolenaar071d4272004-06-13 20:20:40 +00003435 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003436 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003437 if (ea.cmdidx == CMD_SIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003438 {
3439 /* Not still touching the command and it was an illegal one */
3440 xp->xp_context = EXPAND_UNSUCCESSFUL;
3441 return NULL;
3442 }
3443
3444 xp->xp_context = EXPAND_NOTHING; /* Default now that we're past command */
3445
3446 if (*p == '!') /* forced commands */
3447 {
3448 forceit = TRUE;
3449 ++p;
3450 }
3451
3452/*
Bram Moolenaar1c40a662014-11-27 16:38:11 +01003453 * 6. parse arguments
Bram Moolenaar071d4272004-06-13 20:20:40 +00003454 */
Bram Moolenaar958636c2014-10-21 20:01:58 +02003455 if (!IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003456 ea.argt = (long)cmdnames[(int)ea.cmdidx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003457
3458 arg = skipwhite(p);
3459
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003460 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003461 {
3462 if (*arg == '>') /* append */
3463 {
3464 if (*++arg == '>')
3465 ++arg;
3466 arg = skipwhite(arg);
3467 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003468 else if (*arg == '!' && ea.cmdidx == CMD_write) /* :w !filter */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003469 {
3470 ++arg;
3471 usefilter = TRUE;
3472 }
3473 }
3474
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003475 if (ea.cmdidx == CMD_read)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003476 {
3477 usefilter = forceit; /* :r! filter if forced */
3478 if (*arg == '!') /* :r !filter */
3479 {
3480 ++arg;
3481 usefilter = TRUE;
3482 }
3483 }
3484
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003485 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003486 {
3487 while (*arg == *cmd) /* allow any number of '>' or '<' */
3488 ++arg;
3489 arg = skipwhite(arg);
3490 }
3491
3492 /* Does command allow "+command"? */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003493 if ((ea.argt & EDITCMD) && !usefilter && *arg == '+')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003494 {
3495 /* Check if we're in the +command */
3496 p = arg + 1;
3497 arg = skip_cmd_arg(arg, FALSE);
3498
3499 /* Still touching the command after '+'? */
3500 if (*arg == NUL)
3501 return p;
3502
3503 /* Skip space(s) after +command to get to the real argument */
3504 arg = skipwhite(arg);
3505 }
3506
3507 /*
3508 * Check for '|' to separate commands and '"' to start comments.
3509 * Don't do this for ":read !cmd" and ":write !cmd".
3510 */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003511 if ((ea.argt & TRLBAR) && !usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003512 {
3513 p = arg;
3514 /* ":redir @" is not the start of a comment */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003515 if (ea.cmdidx == CMD_redir && p[0] == '@' && p[1] == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003516 p += 2;
3517 while (*p)
3518 {
3519 if (*p == Ctrl_V)
3520 {
3521 if (p[1] != NUL)
3522 ++p;
3523 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003524 else if ( (*p == '"' && !(ea.argt & NOTRLCOM))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003525 || *p == '|' || *p == '\n')
3526 {
3527 if (*(p - 1) != '\\')
3528 {
3529 if (*p == '|' || *p == '\n')
3530 return p + 1;
3531 return NULL; /* It's a comment */
3532 }
3533 }
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003534 MB_PTR_ADV(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003535 }
3536 }
3537
3538 /* no arguments allowed */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003539 if (!(ea.argt & EXTRA) && *arg != NUL &&
Bram Moolenaar071d4272004-06-13 20:20:40 +00003540 vim_strchr((char_u *)"|\"", *arg) == NULL)
3541 return NULL;
3542
3543 /* Find start of last argument (argument just before cursor): */
Bram Moolenaar848f8762012-07-25 17:22:23 +02003544 p = buff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003545 xp->xp_pattern = p;
Bram Moolenaar09168a72012-08-02 21:24:42 +02003546 len = (int)STRLEN(buff);
Bram Moolenaar848f8762012-07-25 17:22:23 +02003547 while (*p && p < buff + len)
3548 {
3549 if (*p == ' ' || *p == TAB)
3550 {
3551 /* argument starts after a space */
3552 xp->xp_pattern = ++p;
3553 }
3554 else
3555 {
3556 if (*p == '\\' && *(p + 1) != NUL)
3557 ++p; /* skip over escaped character */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003558 MB_PTR_ADV(p);
Bram Moolenaar848f8762012-07-25 17:22:23 +02003559 }
3560 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003561
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003562 if (ea.argt & XFILE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003563 {
Bram Moolenaar6529c102007-08-18 15:47:34 +00003564 int c;
3565 int in_quote = FALSE;
3566 char_u *bow = NULL; /* Beginning of word */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003567
3568 /*
3569 * Allow spaces within back-quotes to count as part of the argument
3570 * being expanded.
3571 */
3572 xp->xp_pattern = skipwhite(arg);
Bram Moolenaar6529c102007-08-18 15:47:34 +00003573 p = xp->xp_pattern;
3574 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003575 {
Bram Moolenaar6529c102007-08-18 15:47:34 +00003576 if (has_mbyte)
3577 c = mb_ptr2char(p);
3578 else
Bram Moolenaar6529c102007-08-18 15:47:34 +00003579 c = *p;
3580 if (c == '\\' && p[1] != NUL)
3581 ++p;
3582 else if (c == '`')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003583 {
3584 if (!in_quote)
3585 {
3586 xp->xp_pattern = p;
3587 bow = p + 1;
3588 }
3589 in_quote = !in_quote;
3590 }
Bram Moolenaar332fa0c2008-01-13 16:12:10 +00003591 /* An argument can contain just about everything, except
3592 * characters that end the command and white space. */
Bram Moolenaar1c465442017-03-12 20:10:05 +01003593 else if (c == '|' || c == '\n' || c == '"' || (VIM_ISWHITE(c)
Bram Moolenaar6529c102007-08-18 15:47:34 +00003594#ifdef SPACE_IN_FILENAME
Bram Moolenaar332fa0c2008-01-13 16:12:10 +00003595 && (!(ea.argt & NOSPC) || usefilter)
Bram Moolenaar6529c102007-08-18 15:47:34 +00003596#endif
Bram Moolenaar332fa0c2008-01-13 16:12:10 +00003597 ))
Bram Moolenaar6529c102007-08-18 15:47:34 +00003598 {
Bram Moolenaarcf5a5b82008-02-26 20:30:12 +00003599 len = 0; /* avoid getting stuck when space is in 'isfname' */
Bram Moolenaar6529c102007-08-18 15:47:34 +00003600 while (*p != NUL)
3601 {
Bram Moolenaar6529c102007-08-18 15:47:34 +00003602 if (has_mbyte)
3603 c = mb_ptr2char(p);
3604 else
Bram Moolenaar6529c102007-08-18 15:47:34 +00003605 c = *p;
Bram Moolenaardd87969c2007-08-21 13:07:12 +00003606 if (c == '`' || vim_isfilec_or_wc(c))
Bram Moolenaar6529c102007-08-18 15:47:34 +00003607 break;
Bram Moolenaar6529c102007-08-18 15:47:34 +00003608 if (has_mbyte)
3609 len = (*mb_ptr2len)(p);
3610 else
Bram Moolenaar6529c102007-08-18 15:47:34 +00003611 len = 1;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003612 MB_PTR_ADV(p);
Bram Moolenaar6529c102007-08-18 15:47:34 +00003613 }
3614 if (in_quote)
3615 bow = p;
3616 else
3617 xp->xp_pattern = p;
3618 p -= len;
3619 }
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003620 MB_PTR_ADV(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003621 }
3622
3623 /*
3624 * If we are still inside the quotes, and we passed a space, just
3625 * expand from there.
3626 */
3627 if (bow != NULL && in_quote)
3628 xp->xp_pattern = bow;
3629 xp->xp_context = EXPAND_FILES;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003630
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003631 /* For a shell command more chars need to be escaped. */
Bram Moolenaar67883b42017-07-27 22:57:00 +02003632 if (usefilter || ea.cmdidx == CMD_bang || ea.cmdidx == CMD_terminal)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003633 {
Bram Moolenaarc0cba4d2010-08-07 17:07:21 +02003634#ifndef BACKSLASH_IN_FILENAME
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003635 xp->xp_shell = TRUE;
Bram Moolenaarc0cba4d2010-08-07 17:07:21 +02003636#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003637 /* When still after the command name expand executables. */
3638 if (xp->xp_pattern == skipwhite(arg))
Bram Moolenaar5c5b0942007-05-06 12:07:59 +00003639 xp->xp_context = EXPAND_SHELLCMD;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003640 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003641
3642 /* Check for environment variable */
3643 if (*xp->xp_pattern == '$'
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003644#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003645 || *xp->xp_pattern == '%'
3646#endif
3647 )
3648 {
3649 for (p = xp->xp_pattern + 1; *p != NUL; ++p)
3650 if (!vim_isIDc(*p))
3651 break;
3652 if (*p == NUL)
3653 {
3654 xp->xp_context = EXPAND_ENV_VARS;
3655 ++xp->xp_pattern;
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003656#if defined(FEAT_CMDL_COMPL)
Bram Moolenaar21cf8232004-07-16 20:18:37 +00003657 /* Avoid that the assignment uses EXPAND_FILES again. */
Bram Moolenaara466c992005-07-09 21:03:22 +00003658 if (compl != EXPAND_USER_DEFINED && compl != EXPAND_USER_LIST)
Bram Moolenaar21cf8232004-07-16 20:18:37 +00003659 compl = EXPAND_ENV_VARS;
3660#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003661 }
3662 }
Bram Moolenaar24305862012-08-15 14:05:05 +02003663#if defined(FEAT_CMDL_COMPL)
3664 /* Check for user names */
3665 if (*xp->xp_pattern == '~')
3666 {
3667 for (p = xp->xp_pattern + 1; *p != NUL && *p != '/'; ++p)
3668 ;
3669 /* Complete ~user only if it partially matches a user name.
3670 * A full match ~user<Tab> will be replaced by user's home
3671 * directory i.e. something like ~user<Tab> -> /home/user/ */
3672 if (*p == NUL && p > xp->xp_pattern + 1
Bram Moolenaar6c5d1042018-07-07 16:41:13 +02003673 && match_user(xp->xp_pattern + 1) >= 1)
Bram Moolenaar24305862012-08-15 14:05:05 +02003674 {
3675 xp->xp_context = EXPAND_USER;
3676 ++xp->xp_pattern;
3677 }
3678 }
3679#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003680 }
3681
3682/*
Bram Moolenaar1c40a662014-11-27 16:38:11 +01003683 * 6. Switch on command name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003684 */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003685 switch (ea.cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003686 {
Bram Moolenaarcc448b32010-07-14 16:52:17 +02003687 case CMD_find:
3688 case CMD_sfind:
3689 case CMD_tabfind:
Bram Moolenaarc24b6972010-08-16 22:34:29 +02003690 if (xp->xp_context == EXPAND_FILES)
3691 xp->xp_context = EXPAND_FILES_IN_PATH;
Bram Moolenaarcc448b32010-07-14 16:52:17 +02003692 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003693 case CMD_cd:
3694 case CMD_chdir:
3695 case CMD_lcd:
3696 case CMD_lchdir:
3697 if (xp->xp_context == EXPAND_FILES)
3698 xp->xp_context = EXPAND_DIRECTORIES;
3699 break;
3700 case CMD_help:
3701 xp->xp_context = EXPAND_HELP;
3702 xp->xp_pattern = arg;
3703 break;
3704
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003705 /* Command modifiers: return the argument.
3706 * Also for commands with an argument that is a command. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003707 case CMD_aboveleft:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003708 case CMD_argdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003709 case CMD_belowright:
3710 case CMD_botright:
3711 case CMD_browse:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003712 case CMD_bufdo:
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003713 case CMD_cdo:
3714 case CMD_cfdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003715 case CMD_confirm:
Bram Moolenaardf177f62005-02-22 08:39:57 +00003716 case CMD_debug:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003717 case CMD_folddoclosed:
3718 case CMD_folddoopen:
3719 case CMD_hide:
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003720 case CMD_keepalt:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003721 case CMD_keepjumps:
3722 case CMD_keepmarks:
Bram Moolenaara939e432013-11-09 05:30:26 +01003723 case CMD_keeppatterns:
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003724 case CMD_ldo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003725 case CMD_leftabove:
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003726 case CMD_lfdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003727 case CMD_lockmarks:
Bram Moolenaar5803ae62014-03-23 16:04:02 +01003728 case CMD_noautocmd:
3729 case CMD_noswapfile:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003730 case CMD_rightbelow:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003731 case CMD_sandbox:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003732 case CMD_silent:
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003733 case CMD_tab:
Bram Moolenaar70baa402013-06-16 16:14:03 +02003734 case CMD_tabdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003735 case CMD_topleft:
3736 case CMD_verbose:
3737 case CMD_vertical:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003738 case CMD_windo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003739 return arg;
3740
Bram Moolenaar7069bf12017-01-07 20:39:53 +01003741 case CMD_filter:
3742 if (*arg != NUL)
3743 arg = skip_vimgrep_pat(arg, NULL, NULL);
3744 if (arg == NULL || *arg == NUL)
3745 {
3746 xp->xp_context = EXPAND_NOTHING;
3747 return NULL;
3748 }
3749 return skipwhite(arg);
3750
Bram Moolenaar4f688582007-07-24 12:34:30 +00003751#ifdef FEAT_CMDL_COMPL
3752# ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar071d4272004-06-13 20:20:40 +00003753 case CMD_match:
3754 if (*arg == NUL || !ends_excmd(*arg))
3755 {
Bram Moolenaar4f688582007-07-24 12:34:30 +00003756 /* also complete "None" */
3757 set_context_in_echohl_cmd(xp, arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003758 arg = skipwhite(skiptowhite(arg));
3759 if (*arg != NUL)
3760 {
3761 xp->xp_context = EXPAND_NOTHING;
3762 arg = skip_regexp(arg + 1, *arg, p_magic, NULL);
3763 }
3764 }
3765 return find_nextcmd(arg);
Bram Moolenaar4f688582007-07-24 12:34:30 +00003766# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003767
Bram Moolenaar071d4272004-06-13 20:20:40 +00003768/*
3769 * All completion for the +cmdline_compl feature goes here.
3770 */
3771
Bram Moolenaar071d4272004-06-13 20:20:40 +00003772 case CMD_command:
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003773 return set_context_in_user_cmd(xp, arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003774
3775 case CMD_delcommand:
3776 xp->xp_context = EXPAND_USER_COMMANDS;
3777 xp->xp_pattern = arg;
3778 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003779
3780 case CMD_global:
3781 case CMD_vglobal:
3782 delim = *arg; /* get the delimiter */
3783 if (delim)
3784 ++arg; /* skip delimiter if there is one */
3785
3786 while (arg[0] != NUL && arg[0] != delim)
3787 {
3788 if (arg[0] == '\\' && arg[1] != NUL)
3789 ++arg;
3790 ++arg;
3791 }
3792 if (arg[0] != NUL)
3793 return arg + 1;
3794 break;
3795 case CMD_and:
3796 case CMD_substitute:
3797 delim = *arg;
3798 if (delim)
3799 {
3800 /* skip "from" part */
3801 ++arg;
3802 arg = skip_regexp(arg, delim, p_magic, NULL);
3803 }
3804 /* skip "to" part */
3805 while (arg[0] != NUL && arg[0] != delim)
3806 {
3807 if (arg[0] == '\\' && arg[1] != NUL)
3808 ++arg;
3809 ++arg;
3810 }
3811 if (arg[0] != NUL) /* skip delimiter */
3812 ++arg;
3813 while (arg[0] && vim_strchr((char_u *)"|\"#", arg[0]) == NULL)
3814 ++arg;
3815 if (arg[0] != NUL)
3816 return arg;
3817 break;
3818 case CMD_isearch:
3819 case CMD_dsearch:
3820 case CMD_ilist:
3821 case CMD_dlist:
3822 case CMD_ijump:
3823 case CMD_psearch:
3824 case CMD_djump:
3825 case CMD_isplit:
3826 case CMD_dsplit:
3827 arg = skipwhite(skipdigits(arg)); /* skip count */
3828 if (*arg == '/') /* Match regexp, not just whole words */
3829 {
3830 for (++arg; *arg && *arg != '/'; arg++)
3831 if (*arg == '\\' && arg[1] != NUL)
3832 arg++;
3833 if (*arg)
3834 {
3835 arg = skipwhite(arg + 1);
3836
3837 /* Check for trailing illegal characters */
3838 if (*arg && vim_strchr((char_u *)"|\"\n", *arg) == NULL)
3839 xp->xp_context = EXPAND_NOTHING;
3840 else
3841 return arg;
3842 }
3843 }
3844 break;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003845
Bram Moolenaar071d4272004-06-13 20:20:40 +00003846 case CMD_autocmd:
3847 return set_context_in_autocmd(xp, arg, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003848 case CMD_doautocmd:
Bram Moolenaar73a9d7b2008-11-06 16:16:44 +00003849 case CMD_doautoall:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003850 return set_context_in_autocmd(xp, arg, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003851 case CMD_set:
3852 set_context_in_set_cmd(xp, arg, 0);
3853 break;
3854 case CMD_setglobal:
3855 set_context_in_set_cmd(xp, arg, OPT_GLOBAL);
3856 break;
3857 case CMD_setlocal:
3858 set_context_in_set_cmd(xp, arg, OPT_LOCAL);
3859 break;
3860 case CMD_tag:
3861 case CMD_stag:
3862 case CMD_ptag:
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00003863 case CMD_ltag:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003864 case CMD_tselect:
3865 case CMD_stselect:
3866 case CMD_ptselect:
3867 case CMD_tjump:
3868 case CMD_stjump:
3869 case CMD_ptjump:
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00003870 if (*p_wop != NUL)
3871 xp->xp_context = EXPAND_TAGS_LISTFILES;
3872 else
3873 xp->xp_context = EXPAND_TAGS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003874 xp->xp_pattern = arg;
3875 break;
3876 case CMD_augroup:
3877 xp->xp_context = EXPAND_AUGROUP;
3878 xp->xp_pattern = arg;
3879 break;
3880#ifdef FEAT_SYN_HL
3881 case CMD_syntax:
3882 set_context_in_syntax_cmd(xp, arg);
3883 break;
3884#endif
3885#ifdef FEAT_EVAL
3886 case CMD_let:
3887 case CMD_if:
3888 case CMD_elseif:
3889 case CMD_while:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00003890 case CMD_for:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003891 case CMD_echo:
3892 case CMD_echon:
3893 case CMD_execute:
3894 case CMD_echomsg:
3895 case CMD_echoerr:
3896 case CMD_call:
3897 case CMD_return:
Bram Moolenaar2b2207b2017-01-22 16:46:56 +01003898 case CMD_cexpr:
3899 case CMD_caddexpr:
3900 case CMD_cgetexpr:
3901 case CMD_lexpr:
3902 case CMD_laddexpr:
3903 case CMD_lgetexpr:
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003904 set_context_for_expression(xp, arg, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003905 break;
3906
3907 case CMD_unlet:
3908 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
3909 arg = xp->xp_pattern + 1;
Bram Moolenaar19834012018-06-12 17:03:39 +02003910
Bram Moolenaar071d4272004-06-13 20:20:40 +00003911 xp->xp_context = EXPAND_USER_VARS;
3912 xp->xp_pattern = arg;
Bram Moolenaar19834012018-06-12 17:03:39 +02003913
3914 if (*xp->xp_pattern == '$')
3915 {
3916 xp->xp_context = EXPAND_ENV_VARS;
3917 ++xp->xp_pattern;
3918 }
3919
Bram Moolenaar071d4272004-06-13 20:20:40 +00003920 break;
3921
3922 case CMD_function:
3923 case CMD_delfunction:
3924 xp->xp_context = EXPAND_USER_FUNC;
3925 xp->xp_pattern = arg;
3926 break;
3927
3928 case CMD_echohl:
Bram Moolenaar4f688582007-07-24 12:34:30 +00003929 set_context_in_echohl_cmd(xp, arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003930 break;
3931#endif
3932 case CMD_highlight:
3933 set_context_in_highlight_cmd(xp, arg);
3934 break;
Bram Moolenaarf4580d82009-03-18 11:52:53 +00003935#ifdef FEAT_CSCOPE
3936 case CMD_cscope:
Bram Moolenaar7bfef802009-04-22 14:25:01 +00003937 case CMD_lcscope:
3938 case CMD_scscope:
3939 set_context_in_cscope_cmd(xp, arg, ea.cmdidx);
Bram Moolenaarf4580d82009-03-18 11:52:53 +00003940 break;
3941#endif
Bram Moolenaar3c65e312009-04-29 16:47:23 +00003942#ifdef FEAT_SIGNS
3943 case CMD_sign:
3944 set_context_in_sign_cmd(xp, arg);
3945 break;
3946#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003947 case CMD_bdelete:
3948 case CMD_bwipeout:
3949 case CMD_bunload:
3950 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
3951 arg = xp->xp_pattern + 1;
Bram Moolenaar2f40d122017-10-24 21:49:36 +02003952 /* FALLTHROUGH */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003953 case CMD_buffer:
3954 case CMD_sbuffer:
3955 case CMD_checktime:
3956 xp->xp_context = EXPAND_BUFFERS;
3957 xp->xp_pattern = arg;
3958 break;
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003959
Bram Moolenaar071d4272004-06-13 20:20:40 +00003960 case CMD_USER:
3961 case CMD_USER_BUF:
3962 if (compl != EXPAND_NOTHING)
3963 {
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003964 // XFILE: file names are handled above
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003965 if (!(ea.argt & XFILE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003966 {
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003967#ifdef FEAT_MENU
Bram Moolenaar071d4272004-06-13 20:20:40 +00003968 if (compl == EXPAND_MENUS)
3969 return set_context_in_menu_cmd(xp, cmd, arg, forceit);
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003970#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003971 if (compl == EXPAND_COMMANDS)
3972 return arg;
3973 if (compl == EXPAND_MAPPINGS)
3974 return set_context_in_map_cmd(xp, (char_u *)"map",
3975 arg, forceit, FALSE, FALSE, CMD_map);
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003976 // Find start of last argument.
Bram Moolenaar848f8762012-07-25 17:22:23 +02003977 p = arg;
3978 while (*p)
3979 {
3980 if (*p == ' ')
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003981 // argument starts after a space
Bram Moolenaar848f8762012-07-25 17:22:23 +02003982 arg = p + 1;
Bram Moolenaara07c8312012-07-27 21:12:07 +02003983 else if (*p == '\\' && *(p + 1) != NUL)
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003984 ++p; // skip over escaped character
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003985 MB_PTR_ADV(p);
Bram Moolenaar848f8762012-07-25 17:22:23 +02003986 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003987 xp->xp_pattern = arg;
3988 }
3989 xp->xp_context = compl;
3990 }
3991 break;
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003992
Bram Moolenaar071d4272004-06-13 20:20:40 +00003993 case CMD_map: case CMD_noremap:
3994 case CMD_nmap: case CMD_nnoremap:
3995 case CMD_vmap: case CMD_vnoremap:
3996 case CMD_omap: case CMD_onoremap:
3997 case CMD_imap: case CMD_inoremap:
3998 case CMD_cmap: case CMD_cnoremap:
Bram Moolenaar22b306f2010-07-25 13:50:33 +02003999 case CMD_lmap: case CMD_lnoremap:
Bram Moolenaar09bb33d2013-05-07 05:18:20 +02004000 case CMD_smap: case CMD_snoremap:
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +02004001 case CMD_tmap: case CMD_tnoremap:
Bram Moolenaar09bb33d2013-05-07 05:18:20 +02004002 case CMD_xmap: case CMD_xnoremap:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004003 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004004 FALSE, FALSE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004005 case CMD_unmap:
4006 case CMD_nunmap:
4007 case CMD_vunmap:
4008 case CMD_ounmap:
4009 case CMD_iunmap:
4010 case CMD_cunmap:
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004011 case CMD_lunmap:
Bram Moolenaar09bb33d2013-05-07 05:18:20 +02004012 case CMD_sunmap:
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +02004013 case CMD_tunmap:
Bram Moolenaar09bb33d2013-05-07 05:18:20 +02004014 case CMD_xunmap:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004015 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004016 FALSE, TRUE, ea.cmdidx);
Bram Moolenaarcae92dc2017-08-06 15:22:15 +02004017 case CMD_mapclear:
4018 case CMD_nmapclear:
4019 case CMD_vmapclear:
4020 case CMD_omapclear:
4021 case CMD_imapclear:
4022 case CMD_cmapclear:
4023 case CMD_lmapclear:
4024 case CMD_smapclear:
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +02004025 case CMD_tmapclear:
Bram Moolenaarcae92dc2017-08-06 15:22:15 +02004026 case CMD_xmapclear:
4027 xp->xp_context = EXPAND_MAPCLEAR;
4028 xp->xp_pattern = arg;
4029 break;
4030
Bram Moolenaar071d4272004-06-13 20:20:40 +00004031 case CMD_abbreviate: case CMD_noreabbrev:
4032 case CMD_cabbrev: case CMD_cnoreabbrev:
4033 case CMD_iabbrev: case CMD_inoreabbrev:
4034 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004035 TRUE, FALSE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004036 case CMD_unabbreviate:
4037 case CMD_cunabbrev:
4038 case CMD_iunabbrev:
4039 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004040 TRUE, TRUE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004041#ifdef FEAT_MENU
4042 case CMD_menu: case CMD_noremenu: case CMD_unmenu:
4043 case CMD_amenu: case CMD_anoremenu: case CMD_aunmenu:
4044 case CMD_nmenu: case CMD_nnoremenu: case CMD_nunmenu:
4045 case CMD_vmenu: case CMD_vnoremenu: case CMD_vunmenu:
4046 case CMD_omenu: case CMD_onoremenu: case CMD_ounmenu:
4047 case CMD_imenu: case CMD_inoremenu: case CMD_iunmenu:
4048 case CMD_cmenu: case CMD_cnoremenu: case CMD_cunmenu:
Bram Moolenaar4c5d8152018-10-19 22:36:53 +02004049 case CMD_tlmenu: case CMD_tlnoremenu: case CMD_tlunmenu:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004050 case CMD_tmenu: case CMD_tunmenu:
4051 case CMD_popup: case CMD_tearoff: case CMD_emenu:
4052 return set_context_in_menu_cmd(xp, cmd, arg, forceit);
4053#endif
4054
4055 case CMD_colorscheme:
4056 xp->xp_context = EXPAND_COLORS;
4057 xp->xp_pattern = arg;
4058 break;
4059
4060 case CMD_compiler:
4061 xp->xp_context = EXPAND_COMPILER;
4062 xp->xp_pattern = arg;
4063 break;
4064
Bram Moolenaar883f5d02010-06-21 06:24:34 +02004065 case CMD_ownsyntax:
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02004066 xp->xp_context = EXPAND_OWNSYNTAX;
4067 xp->xp_pattern = arg;
4068 break;
4069
4070 case CMD_setfiletype:
Bram Moolenaar883f5d02010-06-21 06:24:34 +02004071 xp->xp_context = EXPAND_FILETYPE;
4072 xp->xp_pattern = arg;
4073 break;
4074
Bram Moolenaar35ca0e72016-03-05 17:41:49 +01004075 case CMD_packadd:
4076 xp->xp_context = EXPAND_PACKADD;
4077 xp->xp_pattern = arg;
4078 break;
4079
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01004080#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004081 case CMD_language:
Bram Moolenaara660dc82011-05-25 12:51:22 +02004082 p = skiptowhite(arg);
4083 if (*p == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004084 {
4085 xp->xp_context = EXPAND_LANGUAGE;
4086 xp->xp_pattern = arg;
4087 }
4088 else
Bram Moolenaara660dc82011-05-25 12:51:22 +02004089 {
4090 if ( STRNCMP(arg, "messages", p - arg) == 0
4091 || STRNCMP(arg, "ctype", p - arg) == 0
4092 || STRNCMP(arg, "time", p - arg) == 0)
4093 {
4094 xp->xp_context = EXPAND_LOCALES;
4095 xp->xp_pattern = skipwhite(p);
4096 }
4097 else
4098 xp->xp_context = EXPAND_NOTHING;
4099 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004100 break;
4101#endif
Bram Moolenaarf86f26c2010-02-03 15:14:22 +01004102#if defined(FEAT_PROFILE)
4103 case CMD_profile:
4104 set_context_in_profile_cmd(xp, arg);
4105 break;
4106#endif
Bram Moolenaar42b4dda2010-03-02 15:56:05 +01004107 case CMD_behave:
4108 xp->xp_context = EXPAND_BEHAVE;
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02004109 xp->xp_pattern = arg;
Bram Moolenaar42b4dda2010-03-02 15:56:05 +01004110 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004111
Bram Moolenaar9e507ca2016-10-15 15:39:39 +02004112 case CMD_messages:
4113 xp->xp_context = EXPAND_MESSAGES;
4114 xp->xp_pattern = arg;
4115 break;
4116
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02004117#if defined(FEAT_CMDHIST)
4118 case CMD_history:
4119 xp->xp_context = EXPAND_HISTORY;
4120 xp->xp_pattern = arg;
4121 break;
4122#endif
Bram Moolenaarcd9c4622013-06-08 15:24:48 +02004123#if defined(FEAT_PROFILE)
4124 case CMD_syntime:
4125 xp->xp_context = EXPAND_SYNTIME;
4126 xp->xp_pattern = arg;
4127 break;
4128#endif
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02004129
Bram Moolenaarcd43eff2018-03-29 15:55:38 +02004130 case CMD_argdelete:
4131 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
4132 arg = xp->xp_pattern + 1;
4133 xp->xp_context = EXPAND_ARGLIST;
4134 xp->xp_pattern = arg;
4135 break;
4136
Bram Moolenaar071d4272004-06-13 20:20:40 +00004137#endif /* FEAT_CMDL_COMPL */
4138
4139 default:
4140 break;
4141 }
4142 return NULL;
4143}
4144
4145/*
Bram Moolenaaree8415b2018-08-10 23:13:12 +02004146 * Skip a range specifier of the form: addr [,addr] [;addr] ..
Bram Moolenaar071d4272004-06-13 20:20:40 +00004147 *
4148 * Backslashed delimiters after / or ? will be skipped, and commands will
4149 * not be expanded between /'s and ?'s or after "'".
4150 *
Bram Moolenaardf177f62005-02-22 08:39:57 +00004151 * Also skip white space and ":" characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004152 * Returns the "cmd" pointer advanced to beyond the range.
4153 */
4154 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004155skip_range(
4156 char_u *cmd,
4157 int *ctx) /* pointer to xp_context or NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004158{
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004159 unsigned delim;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004160
Bram Moolenaarcbf20fb2017-02-03 21:19:04 +01004161 while (vim_strchr((char_u *)" \t0123456789.$%'/?-+,;\\", *cmd) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004162 {
Bram Moolenaarcbf20fb2017-02-03 21:19:04 +01004163 if (*cmd == '\\')
4164 {
4165 if (cmd[1] == '?' || cmd[1] == '/' || cmd[1] == '&')
4166 ++cmd;
4167 else
4168 break;
4169 }
4170 else if (*cmd == '\'')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004171 {
4172 if (*++cmd == NUL && ctx != NULL)
4173 *ctx = EXPAND_NOTHING;
4174 }
4175 else if (*cmd == '/' || *cmd == '?')
4176 {
4177 delim = *cmd++;
4178 while (*cmd != NUL && *cmd != delim)
4179 if (*cmd++ == '\\' && *cmd != NUL)
4180 ++cmd;
4181 if (*cmd == NUL && ctx != NULL)
4182 *ctx = EXPAND_NOTHING;
4183 }
4184 if (*cmd != NUL)
4185 ++cmd;
4186 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00004187
4188 /* Skip ":" and white space. */
4189 while (*cmd == ':')
4190 cmd = skipwhite(cmd + 1);
4191
Bram Moolenaar071d4272004-06-13 20:20:40 +00004192 return cmd;
4193}
4194
4195/*
Bram Moolenaar198cb662018-09-06 21:44:17 +02004196 * Get a single EX address.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004197 *
4198 * Set ptr to the next character after the part that was interpreted.
4199 * Set ptr to NULL when an error is encountered.
Bram Moolenaar198cb662018-09-06 21:44:17 +02004200 * This may set the last used search pattern.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004201 *
4202 * Return MAXLNUM when no Ex address was found.
4203 */
4204 static linenr_T
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004205get_address(
4206 exarg_T *eap UNUSED,
4207 char_u **ptr,
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02004208 int addr_type, // flag: one of ADDR_LINES, ...
4209 int skip, // only skip the address, don't use it
4210 int silent, // no errors or side effects
4211 int to_other_file, // flag: may jump to other file
4212 int address_count UNUSED) // 1 for first address, >1 after comma
Bram Moolenaar071d4272004-06-13 20:20:40 +00004213{
4214 int c;
4215 int i;
4216 long n;
4217 char_u *cmd;
4218 pos_T pos;
4219 pos_T *fp;
4220 linenr_T lnum;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004221 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004222
4223 cmd = skipwhite(*ptr);
4224 lnum = MAXLNUM;
4225 do
4226 {
4227 switch (*cmd)
4228 {
4229 case '.': /* '.' - Cursor position */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004230 ++cmd;
4231 switch (addr_type)
4232 {
4233 case ADDR_LINES:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004234 lnum = curwin->w_cursor.lnum;
4235 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004236 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004237 lnum = CURRENT_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004238 break;
4239 case ADDR_ARGUMENTS:
4240 lnum = curwin->w_arg_idx + 1;
4241 break;
4242 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004243 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004244 lnum = curbuf->b_fnum;
4245 break;
4246 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004247 lnum = CURRENT_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004248 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004249 case ADDR_TABS_RELATIVE:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004250 emsg(_(e_invrange));
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004251 cmd = NULL;
4252 goto error;
4253 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004254#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004255 case ADDR_QUICKFIX:
4256 lnum = qf_get_cur_valid_idx(eap);
4257 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004258#endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004259 }
4260 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004261
4262 case '$': /* '$' - last line */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004263 ++cmd;
4264 switch (addr_type)
4265 {
4266 case ADDR_LINES:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004267 lnum = curbuf->b_ml.ml_line_count;
4268 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004269 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004270 lnum = LAST_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004271 break;
4272 case ADDR_ARGUMENTS:
4273 lnum = ARGCOUNT;
4274 break;
4275 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004276 buf = lastbuf;
4277 while (buf->b_ml.ml_mfp == NULL)
4278 {
4279 if (buf->b_prev == NULL)
4280 break;
4281 buf = buf->b_prev;
4282 }
4283 lnum = buf->b_fnum;
4284 break;
4285 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004286 lnum = lastbuf->b_fnum;
4287 break;
4288 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004289 lnum = LAST_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004290 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004291 case ADDR_TABS_RELATIVE:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004292 emsg(_(e_invrange));
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004293 cmd = NULL;
4294 goto error;
4295 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004296#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004297 case ADDR_QUICKFIX:
4298 lnum = qf_get_size(eap);
4299 if (lnum == 0)
4300 lnum = 1;
4301 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004302#endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004303 }
4304 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004305
4306 case '\'': /* ''' - mark */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004307 if (*++cmd == NUL)
4308 {
4309 cmd = NULL;
4310 goto error;
4311 }
4312 if (addr_type != ADDR_LINES)
4313 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004314 emsg(_(e_invaddr));
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004315 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004316 goto error;
4317 }
4318 if (skip)
4319 ++cmd;
4320 else
4321 {
4322 /* Only accept a mark in another file when it is
4323 * used by itself: ":'M". */
4324 fp = getmark(*cmd, to_other_file && cmd[1] == NUL);
4325 ++cmd;
4326 if (fp == (pos_T *)-1)
4327 /* Jumped to another file. */
4328 lnum = curwin->w_cursor.lnum;
4329 else
4330 {
4331 if (check_mark(fp) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004332 {
4333 cmd = NULL;
4334 goto error;
4335 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004336 lnum = fp->lnum;
4337 }
4338 }
4339 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004340
4341 case '/':
4342 case '?': /* '/' or '?' - search */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004343 c = *cmd++;
4344 if (addr_type != ADDR_LINES)
4345 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004346 emsg(_(e_invaddr));
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004347 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004348 goto error;
4349 }
4350 if (skip) /* skip "/pat/" */
4351 {
4352 cmd = skip_regexp(cmd, c, (int)p_magic, NULL);
4353 if (*cmd == c)
4354 ++cmd;
4355 }
4356 else
4357 {
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02004358 int flags;
4359
4360 pos = curwin->w_cursor; // save curwin->w_cursor
4361
4362 // When '/' or '?' follows another address, start from
4363 // there.
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004364 if (lnum != MAXLNUM)
4365 curwin->w_cursor.lnum = lnum;
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02004366
4367 // Start a forward search at the end of the line (unless
4368 // before the first line).
4369 // Start a backward search at the start of the line.
4370 // This makes sure we never match in the current
4371 // line, and can match anywhere in the
4372 // next/previous line.
Bram Moolenaar8ada6aa2017-12-19 21:23:21 +01004373 if (c == '/' && curwin->w_cursor.lnum > 0)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004374 curwin->w_cursor.col = MAXCOL;
4375 else
4376 curwin->w_cursor.col = 0;
4377 searchcmdlen = 0;
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02004378 flags = silent ? 0 : SEARCH_HIS | SEARCH_MSG;
4379 if (!do_search(NULL, c, cmd, 1L, flags, NULL, NULL))
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004380 {
4381 curwin->w_cursor = pos;
4382 cmd = NULL;
4383 goto error;
4384 }
4385 lnum = curwin->w_cursor.lnum;
4386 curwin->w_cursor = pos;
4387 /* adjust command string pointer */
4388 cmd += searchcmdlen;
4389 }
4390 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004391
4392 case '\\': /* "\?", "\/" or "\&", repeat search */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004393 ++cmd;
4394 if (addr_type != ADDR_LINES)
4395 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004396 emsg(_(e_invaddr));
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004397 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004398 goto error;
4399 }
4400 if (*cmd == '&')
4401 i = RE_SUBST;
4402 else if (*cmd == '?' || *cmd == '/')
4403 i = RE_SEARCH;
4404 else
4405 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004406 emsg(_(e_backslash));
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004407 cmd = NULL;
4408 goto error;
4409 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004410
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004411 if (!skip)
4412 {
4413 /*
4414 * When search follows another address, start from
4415 * there.
4416 */
4417 if (lnum != MAXLNUM)
4418 pos.lnum = lnum;
4419 else
4420 pos.lnum = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004421
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004422 /*
4423 * Start the search just like for the above
4424 * do_search().
4425 */
4426 if (*cmd != '?')
4427 pos.col = MAXCOL;
4428 else
4429 pos.col = 0;
Bram Moolenaarbd8539a2015-08-11 18:53:03 +02004430 pos.coladd = 0;
Bram Moolenaar5d24a222018-12-23 19:10:09 +01004431 if (searchit(curwin, curbuf, &pos, NULL,
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004432 *cmd == '?' ? BACKWARD : FORWARD,
4433 (char_u *)"", 1L, SEARCH_MSG,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02004434 i, (linenr_T)0, NULL, NULL) != FAIL)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004435 lnum = pos.lnum;
4436 else
4437 {
4438 cmd = NULL;
4439 goto error;
4440 }
4441 }
4442 ++cmd;
4443 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004444
4445 default:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004446 if (VIM_ISDIGIT(*cmd)) /* absolute line number */
4447 lnum = getdigits(&cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004448 }
4449
4450 for (;;)
4451 {
4452 cmd = skipwhite(cmd);
4453 if (*cmd != '-' && *cmd != '+' && !VIM_ISDIGIT(*cmd))
4454 break;
4455
4456 if (lnum == MAXLNUM)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004457 {
4458 switch (addr_type)
4459 {
4460 case ADDR_LINES:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004461 /* "+1" is same as ".+1" */
4462 lnum = curwin->w_cursor.lnum;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004463 break;
4464 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004465 lnum = CURRENT_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004466 break;
4467 case ADDR_ARGUMENTS:
4468 lnum = curwin->w_arg_idx + 1;
4469 break;
4470 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004471 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004472 lnum = curbuf->b_fnum;
4473 break;
4474 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004475 lnum = CURRENT_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004476 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004477 case ADDR_TABS_RELATIVE:
4478 lnum = 1;
4479 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004480#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004481 case ADDR_QUICKFIX:
4482 lnum = qf_get_cur_valid_idx(eap);
4483 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004484#endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004485 }
4486 }
4487
Bram Moolenaar071d4272004-06-13 20:20:40 +00004488 if (VIM_ISDIGIT(*cmd))
4489 i = '+'; /* "number" is same as "+number" */
4490 else
4491 i = *cmd++;
4492 if (!VIM_ISDIGIT(*cmd)) /* '+' is '+1', but '+0' is not '+1' */
4493 n = 1;
4494 else
4495 n = getdigits(&cmd);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004496
4497 if (addr_type == ADDR_TABS_RELATIVE)
4498 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004499 emsg(_(e_invrange));
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004500 cmd = NULL;
4501 goto error;
4502 }
4503 else if (addr_type == ADDR_LOADED_BUFFERS
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004504 || addr_type == ADDR_BUFFERS)
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004505 lnum = compute_buffer_local_count(
4506 addr_type, lnum, (i == '-') ? -1 * n : n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004507 else
Bram Moolenaarded27822017-01-02 14:27:34 +01004508 {
4509#ifdef FEAT_FOLDING
4510 /* Relative line addressing, need to adjust for folded lines
4511 * now, but only do it after the first address. */
4512 if (addr_type == ADDR_LINES && (i == '-' || i == '+')
4513 && address_count >= 2)
4514 (void)hasFolding(lnum, NULL, &lnum);
4515#endif
4516 if (i == '-')
4517 lnum -= n;
4518 else
4519 lnum += n;
4520 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004521 }
4522 } while (*cmd == '/' || *cmd == '?');
4523
4524error:
4525 *ptr = cmd;
4526 return lnum;
4527}
4528
4529/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00004530 * Get flags from an Ex command argument.
4531 */
4532 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004533get_flags(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00004534{
4535 while (vim_strchr((char_u *)"lp#", *eap->arg) != NULL)
4536 {
4537 if (*eap->arg == 'l')
4538 eap->flags |= EXFLAG_LIST;
4539 else if (*eap->arg == 'p')
4540 eap->flags |= EXFLAG_PRINT;
4541 else
4542 eap->flags |= EXFLAG_NR;
4543 eap->arg = skipwhite(eap->arg + 1);
4544 }
4545}
4546
4547/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004548 * Function called for command which is Not Implemented. NI!
4549 */
4550 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004551ex_ni(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004552{
4553 if (!eap->skip)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004554 eap->errmsg = N_("E319: Sorry, the command is not available in this version");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004555}
4556
Bram Moolenaar7bb75552007-07-16 18:39:49 +00004557#ifdef HAVE_EX_SCRIPT_NI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004558/*
4559 * Function called for script command which is Not Implemented. NI!
4560 * Skips over ":perl <<EOF" constructs.
4561 */
4562 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004563ex_script_ni(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004564{
4565 if (!eap->skip)
4566 ex_ni(eap);
4567 else
4568 vim_free(script_get(eap, eap->arg));
4569}
4570#endif
4571
4572/*
4573 * Check range in Ex command for validity.
4574 * Return NULL when valid, error message when invalid.
4575 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004576 static char *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004577invalid_range(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004578{
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004579 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004580 if ( eap->line1 < 0
4581 || eap->line2 < 0
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004582 || eap->line1 > eap->line2)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004583 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004584
4585 if (eap->argt & RANGE)
4586 {
4587 switch(eap->addr_type)
4588 {
4589 case ADDR_LINES:
4590 if (!(eap->argt & NOTADR)
4591 && eap->line2 > curbuf->b_ml.ml_line_count
4592#ifdef FEAT_DIFF
4593 + (eap->cmdidx == CMD_diffget)
4594#endif
4595 )
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004596 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004597 break;
4598 case ADDR_ARGUMENTS:
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004599 /* add 1 if ARGCOUNT is 0 */
4600 if (eap->line2 > ARGCOUNT + (!ARGCOUNT))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004601 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004602 break;
4603 case ADDR_BUFFERS:
4604 if (eap->line1 < firstbuf->b_fnum
4605 || eap->line2 > lastbuf->b_fnum)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004606 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004607 break;
4608 case ADDR_LOADED_BUFFERS:
4609 buf = firstbuf;
4610 while (buf->b_ml.ml_mfp == NULL)
4611 {
4612 if (buf->b_next == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004613 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004614 buf = buf->b_next;
4615 }
4616 if (eap->line1 < buf->b_fnum)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004617 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004618 buf = lastbuf;
4619 while (buf->b_ml.ml_mfp == NULL)
4620 {
4621 if (buf->b_prev == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004622 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004623 buf = buf->b_prev;
4624 }
4625 if (eap->line2 > buf->b_fnum)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004626 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004627 break;
4628 case ADDR_WINDOWS:
Bram Moolenaar8be63882015-01-14 11:25:05 +01004629 if (eap->line2 > LAST_WIN_NR)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004630 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004631 break;
4632 case ADDR_TABS:
4633 if (eap->line2 > LAST_TAB_NR)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004634 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004635 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004636 case ADDR_TABS_RELATIVE:
4637 /* Do nothing */
4638 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004639#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004640 case ADDR_QUICKFIX:
4641 if (eap->line2 != 1 && eap->line2 > qf_get_size(eap))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004642 return _(e_invrange);
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004643 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004644#endif
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004645 }
4646 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004647 return NULL;
4648}
4649
4650/*
4651 * Correct the range for zero line number, if required.
4652 */
4653 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004654correct_range(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004655{
4656 if (!(eap->argt & ZEROR)) /* zero in range not allowed */
4657 {
4658 if (eap->line1 == 0)
4659 eap->line1 = 1;
4660 if (eap->line2 == 0)
4661 eap->line2 = 1;
4662 }
4663}
4664
Bram Moolenaar748bf032005-02-02 23:04:36 +00004665#ifdef FEAT_QUICKFIX
Bram Moolenaar748bf032005-02-02 23:04:36 +00004666/*
4667 * For a ":vimgrep" or ":vimgrepadd" command return a pointer past the
4668 * pattern. Otherwise return eap->arg.
4669 */
4670 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004671skip_grep_pat(exarg_T *eap)
Bram Moolenaar748bf032005-02-02 23:04:36 +00004672{
4673 char_u *p = eap->arg;
4674
Bram Moolenaara37420f2006-02-04 22:37:47 +00004675 if (*p != NUL && (eap->cmdidx == CMD_vimgrep || eap->cmdidx == CMD_lvimgrep
4676 || eap->cmdidx == CMD_vimgrepadd
4677 || eap->cmdidx == CMD_lvimgrepadd
4678 || grep_internal(eap->cmdidx)))
Bram Moolenaar748bf032005-02-02 23:04:36 +00004679 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00004680 p = skip_vimgrep_pat(p, NULL, NULL);
Bram Moolenaar748bf032005-02-02 23:04:36 +00004681 if (p == NULL)
4682 p = eap->arg;
Bram Moolenaar748bf032005-02-02 23:04:36 +00004683 }
4684 return p;
4685}
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004686
4687/*
4688 * For the ":make" and ":grep" commands insert the 'makeprg'/'grepprg' option
4689 * in the command line, so that things like % get expanded.
4690 */
4691 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004692replace_makeprg(exarg_T *eap, char_u *p, char_u **cmdlinep)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004693{
4694 char_u *new_cmdline;
4695 char_u *program;
4696 char_u *pos;
4697 char_u *ptr;
4698 int len;
4699 int i;
4700
4701 /*
4702 * Don't do it when ":vimgrep" is used for ":grep".
4703 */
Bram Moolenaara37420f2006-02-04 22:37:47 +00004704 if ((eap->cmdidx == CMD_make || eap->cmdidx == CMD_lmake
4705 || eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4706 || eap->cmdidx == CMD_grepadd
4707 || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004708 && !grep_internal(eap->cmdidx))
4709 {
Bram Moolenaara37420f2006-02-04 22:37:47 +00004710 if (eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4711 || eap->cmdidx == CMD_grepadd || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004712 {
4713 if (*curbuf->b_p_gp == NUL)
4714 program = p_gp;
4715 else
4716 program = curbuf->b_p_gp;
4717 }
4718 else
4719 {
4720 if (*curbuf->b_p_mp == NUL)
4721 program = p_mp;
4722 else
4723 program = curbuf->b_p_mp;
4724 }
4725
4726 p = skipwhite(p);
4727
4728 if ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4729 {
4730 /* replace $* by given arguments */
4731 i = 1;
4732 while ((pos = (char_u *)strstr((char *)pos + 2, "$*")) != NULL)
4733 ++i;
4734 len = (int)STRLEN(p);
4735 new_cmdline = alloc((int)(STRLEN(program) + i * (len - 2) + 1));
4736 if (new_cmdline == NULL)
4737 return NULL; /* out of memory */
4738 ptr = new_cmdline;
4739 while ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4740 {
4741 i = (int)(pos - program);
4742 STRNCPY(ptr, program, i);
4743 STRCPY(ptr += i, p);
4744 ptr += len;
4745 program = pos + 2;
4746 }
4747 STRCPY(ptr, program);
4748 }
4749 else
4750 {
4751 new_cmdline = alloc((int)(STRLEN(program) + STRLEN(p) + 2));
4752 if (new_cmdline == NULL)
4753 return NULL; /* out of memory */
4754 STRCPY(new_cmdline, program);
4755 STRCAT(new_cmdline, " ");
4756 STRCAT(new_cmdline, p);
4757 }
4758 msg_make(p);
4759
4760 /* 'eap->cmd' is not set here, because it is not used at CMD_make */
4761 vim_free(*cmdlinep);
4762 *cmdlinep = new_cmdline;
4763 p = new_cmdline;
4764 }
4765 return p;
4766}
Bram Moolenaar748bf032005-02-02 23:04:36 +00004767#endif
4768
Bram Moolenaar071d4272004-06-13 20:20:40 +00004769/*
4770 * Expand file name in Ex command argument.
4771 * Return FAIL for failure, OK otherwise.
4772 */
4773 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004774expand_filename(
4775 exarg_T *eap,
4776 char_u **cmdlinep,
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004777 char **errormsgp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004778{
4779 int has_wildcards; /* need to expand wildcards */
4780 char_u *repl;
4781 int srclen;
4782 char_u *p;
4783 int n;
Bram Moolenaar63b92542007-03-27 14:57:09 +00004784 int escaped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004785
Bram Moolenaar748bf032005-02-02 23:04:36 +00004786#ifdef FEAT_QUICKFIX
4787 /* Skip a regexp pattern for ":vimgrep[add] pat file..." */
4788 p = skip_grep_pat(eap);
4789#else
4790 p = eap->arg;
4791#endif
4792
Bram Moolenaar071d4272004-06-13 20:20:40 +00004793 /*
4794 * Decide to expand wildcards *before* replacing '%', '#', etc. If
4795 * the file name contains a wildcard it should not cause expanding.
4796 * (it will be expanded anyway if there is a wildcard before replacing).
4797 */
Bram Moolenaar748bf032005-02-02 23:04:36 +00004798 has_wildcards = mch_has_wildcard(p);
4799 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004800 {
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004801#ifdef FEAT_EVAL
4802 /* Skip over `=expr`, wildcards in it are not expanded. */
4803 if (p[0] == '`' && p[1] == '=')
4804 {
4805 p += 2;
4806 (void)skip_expr(&p);
4807 if (*p == '`')
4808 ++p;
4809 continue;
4810 }
4811#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004812 /*
4813 * Quick check if this cannot be the start of a special string.
4814 * Also removes backslash before '%', '#' and '<'.
4815 */
4816 if (vim_strchr((char_u *)"%#<", *p) == NULL)
4817 {
4818 ++p;
4819 continue;
4820 }
4821
4822 /*
4823 * Try to find a match at this position.
4824 */
Bram Moolenaar63b92542007-03-27 14:57:09 +00004825 repl = eval_vars(p, eap->arg, &srclen, &(eap->do_ecmd_lnum),
4826 errormsgp, &escaped);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004827 if (*errormsgp != NULL) /* error detected */
4828 return FAIL;
4829 if (repl == NULL) /* no match found */
4830 {
4831 p += srclen;
4832 continue;
4833 }
4834
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00004835 /* Wildcards won't be expanded below, the replacement is taken
4836 * literally. But do expand "~/file", "~user/file" and "$HOME/file". */
4837 if (vim_strchr(repl, '$') != NULL || vim_strchr(repl, '~') != NULL)
4838 {
4839 char_u *l = repl;
4840
4841 repl = expand_env_save(repl);
4842 vim_free(l);
4843 }
4844
Bram Moolenaar63b92542007-03-27 14:57:09 +00004845 /* Need to escape white space et al. with a backslash.
4846 * Don't do this for:
4847 * - replacement that already has been escaped: "##"
4848 * - shell commands (may have to use quotes instead).
4849 * - non-unix systems when there is a single argument (spaces don't
4850 * separate arguments then).
4851 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004852 if (!eap->usefilter
Bram Moolenaar63b92542007-03-27 14:57:09 +00004853 && !escaped
Bram Moolenaar071d4272004-06-13 20:20:40 +00004854 && eap->cmdidx != CMD_bang
Bram Moolenaar071d4272004-06-13 20:20:40 +00004855 && eap->cmdidx != CMD_grep
4856 && eap->cmdidx != CMD_grepadd
Bram Moolenaarbf15b8d2017-06-04 20:43:48 +02004857 && eap->cmdidx != CMD_hardcopy
Bram Moolenaar67883b42017-07-27 22:57:00 +02004858 && eap->cmdidx != CMD_lgrep
4859 && eap->cmdidx != CMD_lgrepadd
4860 && eap->cmdidx != CMD_lmake
4861 && eap->cmdidx != CMD_make
4862 && eap->cmdidx != CMD_terminal
Bram Moolenaar071d4272004-06-13 20:20:40 +00004863#ifndef UNIX
4864 && !(eap->argt & NOSPC)
4865#endif
4866 )
4867 {
4868 char_u *l;
4869#ifdef BACKSLASH_IN_FILENAME
4870 /* Don't escape a backslash here, because rem_backslash() doesn't
4871 * remove it later. */
4872 static char_u *nobslash = (char_u *)" \t\"|";
4873# define ESCAPE_CHARS nobslash
4874#else
4875# define ESCAPE_CHARS escape_chars
4876#endif
4877
4878 for (l = repl; *l; ++l)
4879 if (vim_strchr(ESCAPE_CHARS, *l) != NULL)
4880 {
4881 l = vim_strsave_escaped(repl, ESCAPE_CHARS);
4882 if (l != NULL)
4883 {
4884 vim_free(repl);
4885 repl = l;
4886 }
4887 break;
4888 }
4889 }
4890
4891 /* For a shell command a '!' must be escaped. */
Bram Moolenaar67883b42017-07-27 22:57:00 +02004892 if ((eap->usefilter || eap->cmdidx == CMD_bang
4893 || eap->cmdidx == CMD_terminal)
Bram Moolenaar31b7d382014-04-01 18:54:48 +02004894 && vim_strpbrk(repl, (char_u *)"!") != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004895 {
4896 char_u *l;
4897
Bram Moolenaar31b7d382014-04-01 18:54:48 +02004898 l = vim_strsave_escaped(repl, (char_u *)"!");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004899 if (l != NULL)
4900 {
4901 vim_free(repl);
4902 repl = l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004903 }
4904 }
4905
4906 p = repl_cmdline(eap, p, srclen, repl, cmdlinep);
4907 vim_free(repl);
4908 if (p == NULL)
4909 return FAIL;
4910 }
4911
4912 /*
4913 * One file argument: Expand wildcards.
4914 * Don't do this with ":r !command" or ":w !command".
4915 */
4916 if ((eap->argt & NOSPC) && !eap->usefilter)
4917 {
4918 /*
4919 * May do this twice:
4920 * 1. Replace environment variables.
4921 * 2. Replace any other wildcards, remove backslashes.
4922 */
4923 for (n = 1; n <= 2; ++n)
4924 {
4925 if (n == 2)
4926 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004927 /*
4928 * Halve the number of backslashes (this is Vi compatible).
4929 * For Unix and OS/2, when wildcards are expanded, this is
4930 * done by ExpandOne() below.
4931 */
Bram Moolenaare7fedb62015-12-31 19:07:19 +01004932#if defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004933 if (!has_wildcards)
4934#endif
4935 backslash_halve(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004936 }
4937
4938 if (has_wildcards)
4939 {
4940 if (n == 1)
4941 {
4942 /*
4943 * First loop: May expand environment variables. This
4944 * can be done much faster with expand_env() than with
4945 * something else (e.g., calling a shell).
4946 * After expanding environment variables, check again
4947 * if there are still wildcards present.
4948 */
4949 if (vim_strchr(eap->arg, '$') != NULL
4950 || vim_strchr(eap->arg, '~') != NULL)
4951 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +00004952 expand_env_esc(eap->arg, NameBuff, MAXPATHL,
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00004953 TRUE, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004954 has_wildcards = mch_has_wildcard(NameBuff);
4955 p = NameBuff;
4956 }
4957 else
4958 p = NULL;
4959 }
4960 else /* n == 2 */
4961 {
4962 expand_T xpc;
Bram Moolenaar94950a92010-12-02 16:01:29 +01004963 int options = WILD_LIST_NOTFOUND|WILD_ADD_SLASH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004964
4965 ExpandInit(&xpc);
4966 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar94950a92010-12-02 16:01:29 +01004967 if (p_wic)
4968 options += WILD_ICASE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004969 p = ExpandOne(&xpc, eap->arg, NULL,
Bram Moolenaar94950a92010-12-02 16:01:29 +01004970 options, WILD_EXPAND_FREE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004971 if (p == NULL)
4972 return FAIL;
4973 }
4974 if (p != NULL)
4975 {
4976 (void)repl_cmdline(eap, eap->arg, (int)STRLEN(eap->arg),
4977 p, cmdlinep);
4978 if (n == 2) /* p came from ExpandOne() */
4979 vim_free(p);
4980 }
4981 }
4982 }
4983 }
4984 return OK;
4985}
4986
4987/*
4988 * Replace part of the command line, keeping eap->cmd, eap->arg and
4989 * eap->nextcmd correct.
4990 * "src" points to the part that is to be replaced, of length "srclen".
4991 * "repl" is the replacement string.
4992 * Returns a pointer to the character after the replaced string.
4993 * Returns NULL for failure.
4994 */
4995 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004996repl_cmdline(
4997 exarg_T *eap,
4998 char_u *src,
4999 int srclen,
5000 char_u *repl,
5001 char_u **cmdlinep)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005002{
5003 int len;
5004 int i;
5005 char_u *new_cmdline;
5006
5007 /*
5008 * The new command line is build in new_cmdline[].
5009 * First allocate it.
5010 * Careful: a "+cmd" argument may have been NUL terminated.
5011 */
5012 len = (int)STRLEN(repl);
5013 i = (int)(src - *cmdlinep) + (int)STRLEN(src + srclen) + len + 3;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005014 if (eap->nextcmd != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005015 i += (int)STRLEN(eap->nextcmd);/* add space for next command */
5016 if ((new_cmdline = alloc((unsigned)i)) == NULL)
5017 return NULL; /* out of memory! */
5018
5019 /*
5020 * Copy the stuff before the expanded part.
5021 * Copy the expanded stuff.
5022 * Copy what came after the expanded part.
5023 * Copy the next commands, if there are any.
5024 */
5025 i = (int)(src - *cmdlinep); /* length of part before match */
5026 mch_memmove(new_cmdline, *cmdlinep, (size_t)i);
Bram Moolenaara3ffd9c2005-07-21 21:03:15 +00005027
Bram Moolenaar071d4272004-06-13 20:20:40 +00005028 mch_memmove(new_cmdline + i, repl, (size_t)len);
5029 i += len; /* remember the end of the string */
5030 STRCPY(new_cmdline + i, src + srclen);
5031 src = new_cmdline + i; /* remember where to continue */
5032
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005033 if (eap->nextcmd != NULL) /* append next command */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005034 {
5035 i = (int)STRLEN(new_cmdline) + 1;
5036 STRCPY(new_cmdline + i, eap->nextcmd);
5037 eap->nextcmd = new_cmdline + i;
5038 }
5039 eap->cmd = new_cmdline + (eap->cmd - *cmdlinep);
5040 eap->arg = new_cmdline + (eap->arg - *cmdlinep);
5041 if (eap->do_ecmd_cmd != NULL && eap->do_ecmd_cmd != dollar_command)
5042 eap->do_ecmd_cmd = new_cmdline + (eap->do_ecmd_cmd - *cmdlinep);
5043 vim_free(*cmdlinep);
5044 *cmdlinep = new_cmdline;
5045
5046 return src;
5047}
5048
5049/*
5050 * Check for '|' to separate commands and '"' to start comments.
5051 */
5052 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005053separate_nextcmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005054{
5055 char_u *p;
5056
Bram Moolenaar86b68352004-12-27 21:59:20 +00005057#ifdef FEAT_QUICKFIX
Bram Moolenaar748bf032005-02-02 23:04:36 +00005058 p = skip_grep_pat(eap);
5059#else
5060 p = eap->arg;
Bram Moolenaar86b68352004-12-27 21:59:20 +00005061#endif
5062
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005063 for ( ; *p; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005064 {
5065 if (*p == Ctrl_V)
5066 {
5067 if (eap->argt & (USECTRLV | XFILE))
5068 ++p; /* skip CTRL-V and next char */
5069 else
Bram Moolenaarb0db5692007-08-14 20:54:49 +00005070 /* remove CTRL-V and skip next char */
Bram Moolenaara7241f52008-06-24 20:39:31 +00005071 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005072 if (*p == NUL) /* stop at NUL after CTRL-V */
5073 break;
5074 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005075
5076#ifdef FEAT_EVAL
5077 /* Skip over `=expr` when wildcards are expanded. */
Bram Moolenaardf177f62005-02-22 08:39:57 +00005078 else if (p[0] == '`' && p[1] == '=' && (eap->argt & XFILE))
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005079 {
5080 p += 2;
5081 (void)skip_expr(&p);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005082 }
5083#endif
5084
Bram Moolenaar071d4272004-06-13 20:20:40 +00005085 /* Check for '"': start of comment or '|': next command */
5086 /* :@" and :*" do not start a comment!
5087 * :redir @" doesn't either. */
5088 else if ((*p == '"' && !(eap->argt & NOTRLCOM)
5089 && ((eap->cmdidx != CMD_at && eap->cmdidx != CMD_star)
5090 || p != eap->arg)
5091 && (eap->cmdidx != CMD_redir
5092 || p != eap->arg + 1 || p[-1] != '@'))
5093 || *p == '|' || *p == '\n')
5094 {
5095 /*
5096 * We remove the '\' before the '|', unless USECTRLV is used
5097 * AND 'b' is present in 'cpoptions'.
5098 */
5099 if ((vim_strchr(p_cpo, CPO_BAR) == NULL
5100 || !(eap->argt & USECTRLV)) && *(p - 1) == '\\')
5101 {
Bram Moolenaara7241f52008-06-24 20:39:31 +00005102 STRMOVE(p - 1, p); /* remove the '\' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005103 --p;
5104 }
5105 else
5106 {
5107 eap->nextcmd = check_nextcmd(p);
5108 *p = NUL;
5109 break;
5110 }
5111 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005112 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00005113
Bram Moolenaar071d4272004-06-13 20:20:40 +00005114 if (!(eap->argt & NOTRLCOM)) /* remove trailing spaces */
5115 del_trailing_spaces(eap->arg);
5116}
5117
5118/*
5119 * get + command from ex argument
5120 */
5121 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005122getargcmd(char_u **argp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005123{
5124 char_u *arg = *argp;
5125 char_u *command = NULL;
5126
5127 if (*arg == '+') /* +[command] */
5128 {
5129 ++arg;
Bram Moolenaar3e451592014-04-02 14:22:05 +02005130 if (vim_isspace(*arg) || *arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005131 command = dollar_command;
5132 else
5133 {
5134 command = arg;
5135 arg = skip_cmd_arg(command, TRUE);
5136 if (*arg != NUL)
5137 *arg++ = NUL; /* terminate command with NUL */
5138 }
5139
5140 arg = skipwhite(arg); /* skip over spaces */
5141 *argp = arg;
5142 }
5143 return command;
5144}
5145
5146/*
5147 * Find end of "+command" argument. Skip over "\ " and "\\".
5148 */
5149 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005150skip_cmd_arg(
5151 char_u *p,
5152 int rembs) /* TRUE to halve the number of backslashes */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005153{
5154 while (*p && !vim_isspace(*p))
5155 {
5156 if (*p == '\\' && p[1] != NUL)
5157 {
5158 if (rembs)
Bram Moolenaara7241f52008-06-24 20:39:31 +00005159 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005160 else
5161 ++p;
5162 }
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005163 MB_PTR_ADV(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005164 }
5165 return p;
5166}
5167
Bram Moolenaar333b80a2018-04-04 22:57:29 +02005168 int
5169get_bad_opt(char_u *p, exarg_T *eap)
5170{
5171 if (STRICMP(p, "keep") == 0)
5172 eap->bad_char = BAD_KEEP;
5173 else if (STRICMP(p, "drop") == 0)
5174 eap->bad_char = BAD_DROP;
5175 else if (MB_BYTE2LEN(*p) == 1 && p[1] == NUL)
5176 eap->bad_char = *p;
Bram Moolenaar75808492018-06-12 12:39:41 +02005177 else
5178 return FAIL;
5179 return OK;
Bram Moolenaar333b80a2018-04-04 22:57:29 +02005180}
5181
Bram Moolenaar071d4272004-06-13 20:20:40 +00005182/*
5183 * Get "++opt=arg" argument.
5184 * Return FAIL or OK.
5185 */
5186 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005187getargopt(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005188{
5189 char_u *arg = eap->arg + 2;
5190 int *pp = NULL;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02005191 int bad_char_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005192 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005193
5194 /* ":edit ++[no]bin[ary] file" */
5195 if (STRNCMP(arg, "bin", 3) == 0 || STRNCMP(arg, "nobin", 5) == 0)
5196 {
5197 if (*arg == 'n')
5198 {
5199 arg += 2;
5200 eap->force_bin = FORCE_NOBIN;
5201 }
5202 else
5203 eap->force_bin = FORCE_BIN;
5204 if (!checkforcmd(&arg, "binary", 3))
5205 return FAIL;
5206 eap->arg = skipwhite(arg);
5207 return OK;
5208 }
5209
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005210 /* ":read ++edit file" */
5211 if (STRNCMP(arg, "edit", 4) == 0)
5212 {
5213 eap->read_edit = TRUE;
5214 eap->arg = skipwhite(arg + 4);
5215 return OK;
5216 }
5217
Bram Moolenaar071d4272004-06-13 20:20:40 +00005218 if (STRNCMP(arg, "ff", 2) == 0)
5219 {
5220 arg += 2;
5221 pp = &eap->force_ff;
5222 }
5223 else if (STRNCMP(arg, "fileformat", 10) == 0)
5224 {
5225 arg += 10;
5226 pp = &eap->force_ff;
5227 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005228 else if (STRNCMP(arg, "enc", 3) == 0)
5229 {
Bram Moolenaarb38e9ab2011-12-14 14:49:45 +01005230 if (STRNCMP(arg, "encoding", 8) == 0)
5231 arg += 8;
5232 else
5233 arg += 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005234 pp = &eap->force_enc;
5235 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005236 else if (STRNCMP(arg, "bad", 3) == 0)
5237 {
5238 arg += 3;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02005239 pp = &bad_char_idx;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005240 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005241
5242 if (pp == NULL || *arg != '=')
5243 return FAIL;
5244
5245 ++arg;
5246 *pp = (int)(arg - eap->cmd);
5247 arg = skip_cmd_arg(arg, FALSE);
5248 eap->arg = skipwhite(arg);
5249 *arg = NUL;
5250
Bram Moolenaar071d4272004-06-13 20:20:40 +00005251 if (pp == &eap->force_ff)
5252 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005253 if (check_ff_value(eap->cmd + eap->force_ff) == FAIL)
5254 return FAIL;
Bram Moolenaar333b80a2018-04-04 22:57:29 +02005255 eap->force_ff = eap->cmd[eap->force_ff];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005256 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005257 else if (pp == &eap->force_enc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005258 {
5259 /* Make 'fileencoding' lower case. */
5260 for (p = eap->cmd + eap->force_enc; *p != NUL; ++p)
5261 *p = TOLOWER_ASC(*p);
5262 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005263 else
5264 {
5265 /* Check ++bad= argument. Must be a single-byte character, "keep" or
5266 * "drop". */
Bram Moolenaar333b80a2018-04-04 22:57:29 +02005267 if (get_bad_opt(eap->cmd + bad_char_idx, eap) == FAIL)
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005268 return FAIL;
5269 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005270
5271 return OK;
5272}
5273
5274/*
5275 * ":abbreviate" and friends.
5276 */
5277 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005278ex_abbreviate(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005279{
5280 do_exmap(eap, TRUE); /* almost the same as mapping */
5281}
5282
5283/*
5284 * ":map" and friends.
5285 */
5286 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005287ex_map(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005288{
5289 /*
5290 * If we are sourcing .exrc or .vimrc in current directory we
5291 * print the mappings for security reasons.
5292 */
5293 if (secure)
5294 {
5295 secure = 2;
5296 msg_outtrans(eap->cmd);
5297 msg_putchar('\n');
5298 }
5299 do_exmap(eap, FALSE);
5300}
5301
5302/*
5303 * ":unmap" and friends.
5304 */
5305 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005306ex_unmap(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005307{
5308 do_exmap(eap, FALSE);
5309}
5310
5311/*
5312 * ":mapclear" and friends.
5313 */
5314 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005315ex_mapclear(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005316{
5317 map_clear(eap->cmd, eap->arg, eap->forceit, FALSE);
5318}
5319
5320/*
5321 * ":abclear" and friends.
5322 */
5323 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005324ex_abclear(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005325{
5326 map_clear(eap->cmd, eap->arg, TRUE, TRUE);
5327}
5328
Bram Moolenaar071d4272004-06-13 20:20:40 +00005329 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005330ex_autocmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005331{
5332 /*
Bram Moolenaar26d4b892018-07-04 22:26:28 +02005333 * Disallow autocommands from .exrc and .vimrc in current
Bram Moolenaar071d4272004-06-13 20:20:40 +00005334 * directory for security reasons.
5335 */
5336 if (secure)
5337 {
5338 secure = 2;
5339 eap->errmsg = e_curdir;
5340 }
5341 else if (eap->cmdidx == CMD_autocmd)
5342 do_autocmd(eap->arg, eap->forceit);
5343 else
5344 do_augroup(eap->arg, eap->forceit);
5345}
5346
5347/*
5348 * ":doautocmd": Apply the automatic commands to the current buffer.
5349 */
5350 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005351ex_doautocmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005352{
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005353 char_u *arg = eap->arg;
5354 int call_do_modelines = check_nomodeline(&arg);
Bram Moolenaar1610d052016-06-09 22:53:01 +02005355 int did_aucmd;
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005356
Bram Moolenaar1610d052016-06-09 22:53:01 +02005357 (void)do_doautocmd(arg, TRUE, &did_aucmd);
5358 /* Only when there is no <nomodeline>. */
5359 if (call_do_modelines && did_aucmd)
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005360 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005361}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005362
Bram Moolenaar071d4272004-06-13 20:20:40 +00005363/*
5364 * :[N]bunload[!] [N] [bufname] unload buffer
5365 * :[N]bdelete[!] [N] [bufname] delete buffer from buffer list
5366 * :[N]bwipeout[!] [N] [bufname] delete buffer really
5367 */
5368 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005369ex_bunload(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005370{
5371 eap->errmsg = do_bufdel(
5372 eap->cmdidx == CMD_bdelete ? DOBUF_DEL
5373 : eap->cmdidx == CMD_bwipeout ? DOBUF_WIPE
5374 : DOBUF_UNLOAD, eap->arg,
5375 eap->addr_count, (int)eap->line1, (int)eap->line2, eap->forceit);
5376}
5377
5378/*
5379 * :[N]buffer [N] to buffer N
5380 * :[N]sbuffer [N] to buffer N
5381 */
5382 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005383ex_buffer(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005384{
5385 if (*eap->arg)
5386 eap->errmsg = e_trailing;
5387 else
5388 {
5389 if (eap->addr_count == 0) /* default is current buffer */
5390 goto_buffer(eap, DOBUF_CURRENT, FORWARD, 0);
5391 else
5392 goto_buffer(eap, DOBUF_FIRST, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005393 if (eap->do_ecmd_cmd != NULL)
5394 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005395 }
5396}
5397
5398/*
5399 * :[N]bmodified [N] to next mod. buffer
5400 * :[N]sbmodified [N] to next mod. buffer
5401 */
5402 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005403ex_bmodified(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005404{
5405 goto_buffer(eap, DOBUF_MOD, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005406 if (eap->do_ecmd_cmd != NULL)
5407 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005408}
5409
5410/*
5411 * :[N]bnext [N] to next buffer
5412 * :[N]sbnext [N] split and to next buffer
5413 */
5414 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005415ex_bnext(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005416{
5417 goto_buffer(eap, DOBUF_CURRENT, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005418 if (eap->do_ecmd_cmd != NULL)
5419 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005420}
5421
5422/*
5423 * :[N]bNext [N] to previous buffer
5424 * :[N]bprevious [N] to previous buffer
5425 * :[N]sbNext [N] split and to previous buffer
5426 * :[N]sbprevious [N] split and to previous buffer
5427 */
5428 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005429ex_bprevious(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005430{
5431 goto_buffer(eap, DOBUF_CURRENT, BACKWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005432 if (eap->do_ecmd_cmd != NULL)
5433 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005434}
5435
5436/*
5437 * :brewind to first buffer
5438 * :bfirst to first buffer
5439 * :sbrewind split and to first buffer
5440 * :sbfirst split and to first buffer
5441 */
5442 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005443ex_brewind(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005444{
5445 goto_buffer(eap, DOBUF_FIRST, FORWARD, 0);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005446 if (eap->do_ecmd_cmd != NULL)
5447 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005448}
5449
5450/*
5451 * :blast to last buffer
5452 * :sblast split and to last buffer
5453 */
5454 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005455ex_blast(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005456{
5457 goto_buffer(eap, DOBUF_LAST, BACKWARD, 0);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005458 if (eap->do_ecmd_cmd != NULL)
5459 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005460}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005461
5462 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005463ends_excmd(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005464{
5465 return (c == NUL || c == '|' || c == '"' || c == '\n');
5466}
5467
5468#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA) || defined(FEAT_EVAL) \
5469 || defined(PROTO)
5470/*
5471 * Return the next command, after the first '|' or '\n'.
5472 * Return NULL if not found.
5473 */
5474 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005475find_nextcmd(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005476{
5477 while (*p != '|' && *p != '\n')
5478 {
5479 if (*p == NUL)
5480 return NULL;
5481 ++p;
5482 }
5483 return (p + 1);
5484}
5485#endif
5486
5487/*
Bram Moolenaar2256c992016-11-15 21:17:07 +01005488 * Check if *p is a separator between Ex commands, skipping over white space.
5489 * Return NULL if it isn't, the following character if it is.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005490 */
5491 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005492check_nextcmd(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005493{
Bram Moolenaar2256c992016-11-15 21:17:07 +01005494 char_u *s = skipwhite(p);
5495
5496 if (*s == '|' || *s == '\n')
5497 return (s + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005498 else
5499 return NULL;
5500}
5501
5502/*
5503 * - if there are more files to edit
5504 * - and this is the last window
5505 * - and forceit not used
5506 * - and not repeated twice on a row
5507 * return FAIL and give error message if 'message' TRUE
5508 * return OK otherwise
5509 */
5510 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005511check_more(
5512 int message, /* when FALSE check only, no messages */
5513 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005514{
5515 int n = ARGCOUNT - curwin->w_arg_idx - 1;
5516
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005517 if (!forceit && only_one_window()
5518 && ARGCOUNT > 1 && !arg_had_last && n >= 0 && quitmore == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005519 {
5520 if (message)
5521 {
5522#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
5523 if ((p_confirm || cmdmod.confirm) && curbuf->b_fname != NULL)
5524 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02005525 char_u buff[DIALOG_MSG_SIZE];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005526
Bram Moolenaarda6e8912018-08-21 15:12:14 +02005527 vim_snprintf((char *)buff, DIALOG_MSG_SIZE,
5528 NGETTEXT("%d more file to edit. Quit anyway?",
5529 "%d more files to edit. Quit anyway?", n), n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005530 if (vim_dialog_yesno(VIM_QUESTION, NULL, buff, 1) == VIM_YES)
5531 return OK;
5532 return FAIL;
5533 }
5534#endif
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01005535 semsg(NGETTEXT("E173: %d more file to edit",
5536 "E173: %d more files to edit", n), n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005537 quitmore = 2; /* next try to quit is allowed */
5538 }
5539 return FAIL;
5540 }
5541 return OK;
5542}
5543
Bram Moolenaarac9fb182019-04-27 13:04:13 +02005544#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005545/*
5546 * Function given to ExpandGeneric() to obtain the list of command names.
5547 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005548 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005549get_command_name(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005550{
5551 if (idx >= (int)CMD_SIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005552 return get_user_command_name(idx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005553 return cmdnames[idx].cmd_name;
5554}
5555#endif
5556
Bram Moolenaar071d4272004-06-13 20:20:40 +00005557 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005558ex_colorscheme(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005559{
Bram Moolenaare6850792010-05-14 15:28:44 +02005560 if (*eap->arg == NUL)
5561 {
5562#ifdef FEAT_EVAL
5563 char_u *expr = vim_strsave((char_u *)"g:colors_name");
5564 char_u *p = NULL;
5565
5566 if (expr != NULL)
5567 {
5568 ++emsg_off;
5569 p = eval_to_string(expr, NULL, FALSE);
5570 --emsg_off;
5571 vim_free(expr);
5572 }
5573 if (p != NULL)
5574 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01005575 msg((char *)p);
Bram Moolenaare6850792010-05-14 15:28:44 +02005576 vim_free(p);
5577 }
5578 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01005579 msg("default");
Bram Moolenaare6850792010-05-14 15:28:44 +02005580#else
Bram Moolenaar32526b32019-01-19 17:43:09 +01005581 msg(_("unknown"));
Bram Moolenaare6850792010-05-14 15:28:44 +02005582#endif
5583 }
5584 else if (load_colors(eap->arg) == FAIL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005585 semsg(_("E185: Cannot find color scheme '%s'"), eap->arg);
Bram Moolenaarf58d81a2019-01-28 20:19:05 +01005586
5587#ifdef FEAT_VTP
5588 else if (has_vtp_working())
5589 {
5590 // background color change requires clear + redraw
5591 update_screen(CLEAR);
5592 redrawcmd();
5593 }
5594#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005595}
5596
5597 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005598ex_highlight(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005599{
5600 if (*eap->arg == NUL && eap->cmd[2] == '!')
Bram Moolenaar32526b32019-01-19 17:43:09 +01005601 msg(_("Greetings, Vim user!"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005602 do_highlight(eap->arg, eap->forceit, FALSE);
5603}
5604
5605
5606/*
5607 * Call this function if we thought we were going to exit, but we won't
5608 * (because of an error). May need to restore the terminal mode.
5609 */
5610 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005611not_exiting(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005612{
5613 exiting = FALSE;
5614 settmode(TMODE_RAW);
5615}
5616
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005617 static int
5618before_quit_autocmds(win_T *wp, int quit_all, int forceit)
5619{
5620 apply_autocmds(EVENT_QUITPRE, NULL, NULL, FALSE, wp->w_buffer);
5621
5622 /* Bail out when autocommands closed the window.
5623 * Refuse to quit when the buffer in the last window is being closed (can
5624 * only happen in autocommands). */
5625 if (!win_valid(wp)
5626 || curbuf_locked()
5627 || (wp->w_buffer->b_nwindows == 1 && wp->w_buffer->b_locked > 0))
5628 return TRUE;
5629
5630 if (quit_all || (check_more(FALSE, forceit) == OK && only_one_window()))
5631 {
5632 apply_autocmds(EVENT_EXITPRE, NULL, NULL, FALSE, curbuf);
5633 /* Refuse to quit when locked or when the buffer in the last window is
5634 * being closed (can only happen in autocommands). */
5635 if (curbuf_locked()
5636 || (curbuf->b_nwindows == 1 && curbuf->b_locked > 0))
5637 return TRUE;
5638 }
5639
5640 return FALSE;
5641}
5642
Bram Moolenaar071d4272004-06-13 20:20:40 +00005643/*
Bram Moolenaarf240e182014-11-27 18:33:02 +01005644 * ":quit": quit current window, quit Vim if the last window is closed.
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005645 * ":{nr}quit": quit window {nr}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005646 */
5647 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005648ex_quit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005649{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005650 win_T *wp;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005651
Bram Moolenaar071d4272004-06-13 20:20:40 +00005652#ifdef FEAT_CMDWIN
5653 if (cmdwin_type != 0)
5654 {
5655 cmdwin_result = Ctrl_C;
5656 return;
5657 }
5658#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005659 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005660 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005661 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005662 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005663 return;
5664 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005665 if (eap->addr_count > 0)
5666 {
Bram Moolenaarf240e182014-11-27 18:33:02 +01005667 int wnr = eap->line2;
5668
5669 for (wp = firstwin; wp->w_next != NULL; wp = wp->w_next)
5670 if (--wnr <= 0)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005671 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005672 }
5673 else
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005674 wp = curwin;
Bram Moolenaarf240e182014-11-27 18:33:02 +01005675
Bram Moolenaar87ffb5c2017-10-19 12:37:42 +02005676 /* Refuse to quit when locked. */
5677 if (curbuf_locked())
5678 return;
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005679
5680 /* Trigger QuitPre and maybe ExitPre */
5681 if (before_quit_autocmds(wp, FALSE, eap->forceit))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005682 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005683
5684#ifdef FEAT_NETBEANS_INTG
5685 netbeansForcedQuit = eap->forceit;
5686#endif
5687
5688 /*
5689 * If there are more files or windows we won't exit.
5690 */
5691 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
5692 exiting = TRUE;
Bram Moolenaarff930ca2017-10-19 17:12:10 +02005693 if ((!buf_hide(wp->w_buffer)
5694 && check_changed(wp->w_buffer, (p_awa ? CCGD_AW : 0)
Bram Moolenaar45d3b142013-11-09 03:31:51 +01005695 | (eap->forceit ? CCGD_FORCEIT : 0)
5696 | CCGD_EXCMD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005697 || check_more(TRUE, eap->forceit) == FAIL
Bram Moolenaar027387f2016-01-02 22:25:52 +01005698 || (only_one_window() && check_changed_any(eap->forceit, TRUE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005699 {
5700 not_exiting();
5701 }
5702 else
5703 {
Bram Moolenaarc7a0d322015-06-19 12:43:07 +02005704 /* quit last window
5705 * Note: only_one_window() returns true, even so a help window is
5706 * still open. In that case only quit, if no address has been
5707 * specified. Example:
5708 * :h|wincmd w|1q - don't quit
5709 * :h|wincmd w|q - quit
5710 */
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01005711 if (only_one_window() && (ONE_WINDOW || eap->addr_count == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005712 getout(0);
Bram Moolenaar2c33d7b2017-10-14 16:06:20 +02005713 not_exiting();
Bram Moolenaar4033c552017-09-16 20:54:51 +02005714#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00005715 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005716#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005717 /* close window; may free buffer */
Bram Moolenaareb44a682017-08-03 22:44:55 +02005718 win_close(wp, !buf_hide(wp->w_buffer) || eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005719 }
5720}
5721
5722/*
5723 * ":cquit".
5724 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005725 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005726ex_cquit(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005727{
5728 getout(1); /* this does not always pass on the exit code to the Manx
5729 compiler. why? */
5730}
5731
5732/*
5733 * ":qall": try to quit all windows
5734 */
5735 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005736ex_quit_all(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005737{
5738# ifdef FEAT_CMDWIN
5739 if (cmdwin_type != 0)
5740 {
5741 if (eap->forceit)
5742 cmdwin_result = K_XF1; /* ex_window() takes care of this */
5743 else
5744 cmdwin_result = K_XF2;
5745 return;
5746 }
5747# endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005748
5749 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005750 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005751 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005752 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005753 return;
5754 }
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005755
5756 if (before_quit_autocmds(curwin, TRUE, eap->forceit))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005757 return;
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005758
Bram Moolenaar071d4272004-06-13 20:20:40 +00005759 exiting = TRUE;
Bram Moolenaar027387f2016-01-02 22:25:52 +01005760 if (eap->forceit || !check_changed_any(FALSE, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005761 getout(0);
5762 not_exiting();
5763}
5764
Bram Moolenaar071d4272004-06-13 20:20:40 +00005765/*
5766 * ":close": close current window, unless it is the last one
5767 */
5768 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005769ex_close(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005770{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005771 win_T *win;
5772 int winnr = 0;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005773#ifdef FEAT_CMDWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00005774 if (cmdwin_type != 0)
Bram Moolenaar9bd1a7e2011-05-19 14:50:54 +02005775 cmdwin_result = Ctrl_C;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005776 else
Bram Moolenaar4033c552017-09-16 20:54:51 +02005777#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005778 if (!text_locked() && !curbuf_locked())
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005779 {
5780 if (eap->addr_count == 0)
5781 ex_win_close(eap->forceit, curwin, NULL);
Bram Moolenaar6d41c782018-06-06 09:11:12 +02005782 else
5783 {
Bram Moolenaar29323592016-07-24 22:04:11 +02005784 FOR_ALL_WINDOWS(win)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005785 {
5786 winnr++;
5787 if (winnr == eap->line2)
5788 break;
5789 }
5790 if (win == NULL)
5791 win = lastwin;
5792 ex_win_close(eap->forceit, win, NULL);
5793 }
5794 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005795}
5796
Bram Moolenaar4033c552017-09-16 20:54:51 +02005797#ifdef FEAT_QUICKFIX
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005798/*
5799 * ":pclose": Close any preview window.
5800 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005801 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005802ex_pclose(exarg_T *eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005803{
5804 win_T *win;
5805
Bram Moolenaar29323592016-07-24 22:04:11 +02005806 FOR_ALL_WINDOWS(win)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005807 if (win->w_p_pvw)
5808 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00005809 ex_win_close(eap->forceit, win, NULL);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005810 break;
5811 }
5812}
Bram Moolenaar4033c552017-09-16 20:54:51 +02005813#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005814
Bram Moolenaarf740b292006-02-16 22:11:02 +00005815/*
5816 * Close window "win" and take care of handling closing the last window for a
5817 * modified buffer.
5818 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005819 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005820ex_win_close(
5821 int forceit,
5822 win_T *win,
5823 tabpage_T *tp) /* NULL or the tab page "win" is in */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005824{
5825 int need_hide;
5826 buf_T *buf = win->w_buffer;
5827
5828 need_hide = (bufIsChanged(buf) && buf->b_nwindows <= 1);
Bram Moolenaareb44a682017-08-03 22:44:55 +02005829 if (need_hide && !buf_hide(buf) && !forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005830 {
Bram Moolenaar4033c552017-09-16 20:54:51 +02005831#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005832 if ((p_confirm || cmdmod.confirm) && p_write)
5833 {
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005834 bufref_T bufref;
5835
5836 set_bufref(&bufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005837 dialog_changed(buf, FALSE);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005838 if (bufref_valid(&bufref) && bufIsChanged(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005839 return;
5840 need_hide = FALSE;
5841 }
5842 else
Bram Moolenaar4033c552017-09-16 20:54:51 +02005843#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005844 {
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02005845 no_write_message();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005846 return;
5847 }
5848 }
5849
Bram Moolenaar4033c552017-09-16 20:54:51 +02005850#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00005851 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005852#endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00005853
Bram Moolenaar071d4272004-06-13 20:20:40 +00005854 /* free buffer when not hiding it or when it's a scratch buffer */
Bram Moolenaarf740b292006-02-16 22:11:02 +00005855 if (tp == NULL)
Bram Moolenaareb44a682017-08-03 22:44:55 +02005856 win_close(win, !need_hide && !buf_hide(buf));
Bram Moolenaarf740b292006-02-16 22:11:02 +00005857 else
Bram Moolenaareb44a682017-08-03 22:44:55 +02005858 win_close_othertab(win, !need_hide && !buf_hide(buf), tp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005859}
5860
Bram Moolenaar071d4272004-06-13 20:20:40 +00005861/*
Bram Moolenaardea25702017-01-29 15:18:10 +01005862 * Handle the argument for a tabpage related ex command.
5863 * Returns a tabpage number.
5864 * When an error is encountered then eap->errmsg is set.
5865 */
5866 static int
5867get_tabpage_arg(exarg_T *eap)
5868{
5869 int tab_number;
5870 int unaccept_arg0 = (eap->cmdidx == CMD_tabmove) ? 0 : 1;
5871
5872 if (eap->arg && *eap->arg != NUL)
5873 {
5874 char_u *p = eap->arg;
5875 char_u *p_save;
5876 int relative = 0; /* argument +N/-N means: go to N places to the
5877 * right/left relative to the current position. */
5878
5879 if (*p == '-')
5880 {
5881 relative = -1;
5882 p++;
5883 }
5884 else if (*p == '+')
5885 {
5886 relative = 1;
5887 p++;
5888 }
5889
5890 p_save = p;
5891 tab_number = getdigits(&p);
5892
5893 if (relative == 0)
5894 {
5895 if (STRCMP(p, "$") == 0)
5896 tab_number = LAST_TAB_NR;
5897 else if (p == p_save || *p_save == '-' || *p != NUL
5898 || tab_number > LAST_TAB_NR)
5899 {
5900 /* No numbers as argument. */
5901 eap->errmsg = e_invarg;
5902 goto theend;
5903 }
5904 }
5905 else
5906 {
5907 if (*p_save == NUL)
5908 tab_number = 1;
5909 else if (p == p_save || *p_save == '-' || *p != NUL
5910 || tab_number == 0)
5911 {
5912 /* No numbers as argument. */
5913 eap->errmsg = e_invarg;
5914 goto theend;
5915 }
5916 tab_number = tab_number * relative + tabpage_index(curtab);
5917 if (!unaccept_arg0 && relative == -1)
5918 --tab_number;
5919 }
5920 if (tab_number < unaccept_arg0 || tab_number > LAST_TAB_NR)
5921 eap->errmsg = e_invarg;
5922 }
5923 else if (eap->addr_count > 0)
5924 {
5925 if (unaccept_arg0 && eap->line2 == 0)
Bram Moolenaarc6251552017-01-29 21:42:20 +01005926 {
Bram Moolenaardea25702017-01-29 15:18:10 +01005927 eap->errmsg = e_invrange;
Bram Moolenaarc6251552017-01-29 21:42:20 +01005928 tab_number = 0;
5929 }
Bram Moolenaardea25702017-01-29 15:18:10 +01005930 else
5931 {
5932 tab_number = eap->line2;
Bram Moolenaar82a12462019-01-22 22:41:42 +01005933 if (!unaccept_arg0 && *skipwhite(*eap->cmdlinep) == '-')
Bram Moolenaardea25702017-01-29 15:18:10 +01005934 {
5935 --tab_number;
5936 if (tab_number < unaccept_arg0)
5937 eap->errmsg = e_invarg;
5938 }
5939 }
5940 }
5941 else
5942 {
5943 switch (eap->cmdidx)
5944 {
5945 case CMD_tabnext:
5946 tab_number = tabpage_index(curtab) + 1;
5947 if (tab_number > LAST_TAB_NR)
5948 tab_number = 1;
5949 break;
5950 case CMD_tabmove:
5951 tab_number = LAST_TAB_NR;
5952 break;
5953 default:
5954 tab_number = tabpage_index(curtab);
5955 }
5956 }
5957
5958theend:
5959 return tab_number;
5960}
5961
5962/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00005963 * ":tabclose": close current tab page, unless it is the last one.
5964 * ":tabclose N": close tab page N.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005965 */
5966 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005967ex_tabclose(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005968{
Bram Moolenaarf740b292006-02-16 22:11:02 +00005969 tabpage_T *tp;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005970 int tab_number;
Bram Moolenaarf740b292006-02-16 22:11:02 +00005971
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005972# ifdef FEAT_CMDWIN
5973 if (cmdwin_type != 0)
5974 cmdwin_result = K_IGNORE;
5975 else
5976# endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00005977 if (first_tabpage->tp_next == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005978 emsg(_("E784: Cannot close last tab page"));
Bram Moolenaarf740b292006-02-16 22:11:02 +00005979 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005980 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005981 tab_number = get_tabpage_arg(eap);
5982 if (eap->errmsg == NULL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005983 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005984 tp = find_tabpage(tab_number);
Bram Moolenaarf740b292006-02-16 22:11:02 +00005985 if (tp == NULL)
5986 {
5987 beep_flush();
5988 return;
5989 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005990 if (tp != curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00005991 {
5992 tabpage_close_other(tp, eap->forceit);
5993 return;
5994 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005995 else if (!text_locked() && !curbuf_locked())
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005996 tabpage_close(eap->forceit);
5997 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005998 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005999}
6000
6001/*
6002 * ":tabonly": close all tab pages except the current one
6003 */
6004 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006005ex_tabonly(exarg_T *eap)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006006{
6007 tabpage_T *tp;
6008 int done;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006009 int tab_number;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006010
6011# ifdef FEAT_CMDWIN
6012 if (cmdwin_type != 0)
6013 cmdwin_result = K_IGNORE;
6014 else
6015# endif
6016 if (first_tabpage->tp_next == NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01006017 msg(_("Already only one tab page"));
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006018 else
6019 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006020 tab_number = get_tabpage_arg(eap);
6021 if (eap->errmsg == NULL)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006022 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006023 goto_tabpage(tab_number);
6024 /* Repeat this up to a 1000 times, because autocommands may
6025 * mess up the lists. */
6026 for (done = 0; done < 1000; ++done)
6027 {
6028 FOR_ALL_TABPAGES(tp)
6029 if (tp->tp_topframe != topframe)
6030 {
6031 tabpage_close_other(tp, eap->forceit);
6032 /* if we failed to close it quit */
6033 if (valid_tabpage(tp))
6034 done = 1000;
6035 /* start over, "tp" is now invalid */
6036 break;
6037 }
6038 if (first_tabpage->tp_next == NULL)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006039 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006040 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006041 }
6042 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006043}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006044
6045/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00006046 * Close the current tab page.
6047 */
6048 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006049tabpage_close(int forceit)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006050{
6051 /* First close all the windows but the current one. If that worked then
6052 * close the last window in this tab, that will close it. */
Bram Moolenaar459ca562016-11-10 18:16:33 +01006053 if (!ONE_WINDOW)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006054 close_others(TRUE, forceit);
Bram Moolenaar459ca562016-11-10 18:16:33 +01006055 if (ONE_WINDOW)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006056 ex_win_close(forceit, curwin, NULL);
6057# ifdef FEAT_GUI
6058 need_mouse_correct = TRUE;
6059# endif
6060}
6061
6062/*
6063 * Close tab page "tp", which is not the current tab page.
6064 * Note that autocommands may make "tp" invalid.
Bram Moolenaar7875acc2006-09-10 13:51:17 +00006065 * Also takes care of the tab pages line disappearing when closing the
6066 * last-but-one tab page.
Bram Moolenaarf740b292006-02-16 22:11:02 +00006067 */
6068 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006069tabpage_close_other(tabpage_T *tp, int forceit)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006070{
6071 int done = 0;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006072 win_T *wp;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00006073 int h = tabline_height();
Bram Moolenaarf740b292006-02-16 22:11:02 +00006074
6075 /* Limit to 1000 windows, autocommands may add a window while we close
6076 * one. OK, so I'm paranoid... */
6077 while (++done < 1000)
6078 {
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006079 wp = tp->tp_firstwin;
6080 ex_win_close(forceit, wp, tp);
Bram Moolenaarf740b292006-02-16 22:11:02 +00006081
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006082 /* Autocommands may delete the tab page under our fingers and we may
6083 * fail to close a window with a modified buffer. */
6084 if (!valid_tabpage(tp) || tp->tp_firstwin == wp)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006085 break;
6086 }
Bram Moolenaar7875acc2006-09-10 13:51:17 +00006087
Bram Moolenaar29323592016-07-24 22:04:11 +02006088 apply_autocmds(EVENT_TABCLOSED, NULL, NULL, FALSE, curbuf);
Bram Moolenaar29323592016-07-24 22:04:11 +02006089
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006090 redraw_tabline = TRUE;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00006091 if (h != tabline_height())
6092 shell_new_rows();
Bram Moolenaarf740b292006-02-16 22:11:02 +00006093}
6094
6095/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006096 * ":only".
6097 */
6098 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006099ex_only(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006100{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01006101 win_T *wp;
6102 int wnr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006103# ifdef FEAT_GUI
6104 need_mouse_correct = TRUE;
6105# endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01006106 if (eap->addr_count > 0)
6107 {
6108 wnr = eap->line2;
6109 for (wp = firstwin; --wnr > 0; )
6110 {
6111 if (wp->w_next == NULL)
6112 break;
6113 else
6114 wp = wp->w_next;
6115 }
6116 win_goto(wp);
6117 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006118 close_others(TRUE, eap->forceit);
6119}
6120
6121/*
6122 * ":all" and ":sall".
Bram Moolenaard9967712006-03-11 21:18:15 +00006123 * Also used for ":tab drop file ..." after setting the argument list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006124 */
Bram Moolenaard9967712006-03-11 21:18:15 +00006125 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006126ex_all(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006127{
6128 if (eap->addr_count == 0)
6129 eap->line2 = 9999;
Bram Moolenaard9967712006-03-11 21:18:15 +00006130 do_arg_all((int)eap->line2, eap->forceit, eap->cmdidx == CMD_drop);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006131}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006132
6133 static void
Bram Moolenaar5e1e6d22017-01-02 17:26:00 +01006134ex_hide(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006135{
Bram Moolenaar2256c992016-11-15 21:17:07 +01006136 /* ":hide" or ":hide | cmd": hide current window */
Bram Moolenaar2256c992016-11-15 21:17:07 +01006137 if (!eap->skip)
6138 {
Bram Moolenaar4033c552017-09-16 20:54:51 +02006139#ifdef FEAT_GUI
Bram Moolenaar2256c992016-11-15 21:17:07 +01006140 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006141#endif
Bram Moolenaar2256c992016-11-15 21:17:07 +01006142 if (eap->addr_count == 0)
6143 win_close(curwin, FALSE); /* don't free buffer */
6144 else
6145 {
6146 int winnr = 0;
6147 win_T *win;
Bram Moolenaarf240e182014-11-27 18:33:02 +01006148
Bram Moolenaar2256c992016-11-15 21:17:07 +01006149 FOR_ALL_WINDOWS(win)
6150 {
6151 winnr++;
6152 if (winnr == eap->line2)
6153 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01006154 }
Bram Moolenaar2256c992016-11-15 21:17:07 +01006155 if (win == NULL)
6156 win = lastwin;
6157 win_close(win, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006158 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006159 }
6160}
6161
6162/*
6163 * ":stop" and ":suspend": Suspend Vim.
6164 */
6165 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006166ex_stop(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006167{
6168 /*
6169 * Disallow suspending for "rvim".
6170 */
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006171 if (!check_restricted())
Bram Moolenaar071d4272004-06-13 20:20:40 +00006172 {
6173 if (!eap->forceit)
6174 autowrite_all();
6175 windgoto((int)Rows - 1, 0);
6176 out_char('\n');
6177 out_flush();
6178 stoptermcap();
6179 out_flush(); /* needed for SUN to restore xterm buffer */
6180#ifdef FEAT_TITLE
Bram Moolenaar40385db2018-08-07 22:31:44 +02006181 mch_restore_title(SAVE_RESTORE_BOTH); /* restore window titles */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006182#endif
6183 ui_suspend(); /* call machine specific function */
6184#ifdef FEAT_TITLE
6185 maketitle();
6186 resettitle(); /* force updating the title */
6187#endif
6188 starttermcap();
6189 scroll_start(); /* scroll screen before redrawing */
6190 redraw_later_clear();
6191 shell_resized(); /* may have resized window */
6192 }
6193}
6194
6195/*
Bram Moolenaar12a96de2018-03-11 14:44:18 +01006196 * ":exit", ":xit" and ":wq": Write file and quite the current window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006197 */
6198 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006199ex_exit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006200{
6201#ifdef FEAT_CMDWIN
6202 if (cmdwin_type != 0)
6203 {
6204 cmdwin_result = Ctrl_C;
6205 return;
6206 }
6207#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006208 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006209 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006210 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006211 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006212 return;
6213 }
Bram Moolenaar12a96de2018-03-11 14:44:18 +01006214
6215 if (before_quit_autocmds(curwin, FALSE, eap->forceit))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006216 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006217
6218 /*
6219 * if more files or windows we won't exit
6220 */
6221 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
6222 exiting = TRUE;
6223 if ( ((eap->cmdidx == CMD_wq
6224 || curbufIsChanged())
6225 && do_write(eap) == FAIL)
6226 || check_more(TRUE, eap->forceit) == FAIL
Bram Moolenaar027387f2016-01-02 22:25:52 +01006227 || (only_one_window() && check_changed_any(eap->forceit, FALSE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006228 {
6229 not_exiting();
6230 }
6231 else
6232 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006233 if (only_one_window()) /* quit last window, exit Vim */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006234 getout(0);
Bram Moolenaar2c33d7b2017-10-14 16:06:20 +02006235 not_exiting();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006236# ifdef FEAT_GUI
6237 need_mouse_correct = TRUE;
6238# endif
Bram Moolenaar1a4a75c2013-07-28 16:03:06 +02006239 /* Quit current window, may free the buffer. */
Bram Moolenaareb44a682017-08-03 22:44:55 +02006240 win_close(curwin, !buf_hide(curwin->w_buffer));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006241 }
6242}
6243
6244/*
6245 * ":print", ":list", ":number".
6246 */
6247 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006248ex_print(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006249{
Bram Moolenaardf177f62005-02-22 08:39:57 +00006250 if (curbuf->b_ml.ml_flags & ML_EMPTY)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006251 emsg(_(e_emptybuf));
Bram Moolenaardf177f62005-02-22 08:39:57 +00006252 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006253 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00006254 for ( ;!got_int; ui_breakcheck())
6255 {
6256 print_line(eap->line1,
6257 (eap->cmdidx == CMD_number || eap->cmdidx == CMD_pound
6258 || (eap->flags & EXFLAG_NR)),
6259 eap->cmdidx == CMD_list || (eap->flags & EXFLAG_LIST));
6260 if (++eap->line1 > eap->line2)
6261 break;
6262 out_flush(); /* show one line at a time */
6263 }
6264 setpcmark();
6265 /* put cursor at last line */
6266 curwin->w_cursor.lnum = eap->line2;
6267 beginline(BL_SOL | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006268 }
6269
Bram Moolenaar071d4272004-06-13 20:20:40 +00006270 ex_no_reprint = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006271}
6272
6273#ifdef FEAT_BYTEOFF
6274 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006275ex_goto(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006276{
6277 goto_byte(eap->line2);
6278}
6279#endif
6280
6281/*
6282 * ":shell".
6283 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006284 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006285ex_shell(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006286{
6287 do_shell(NULL, 0);
6288}
6289
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006290#if defined(HAVE_DROP_FILE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006291
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006292static int drop_busy = FALSE;
6293static int drop_filec;
6294static char_u **drop_filev = NULL;
6295static int drop_split;
6296static void (*drop_callback)(void *);
6297static void *drop_cookie;
6298
6299 static void
6300handle_drop_internal(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006301{
6302 exarg_T ea;
6303 int save_msg_scroll = msg_scroll;
6304
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006305 // Setting the argument list may cause screen updates and being called
6306 // recursively. Avoid that by setting drop_busy.
6307 drop_busy = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006308
6309 /* Check whether the current buffer is changed. If so, we will need
6310 * to split the current window or data could be lost.
6311 * We don't need to check if the 'hidden' option is set, as in this
6312 * case the buffer won't be lost.
6313 */
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006314 if (!buf_hide(curbuf) && !drop_split)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006315 {
6316 ++emsg_off;
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006317 drop_split = check_changed(curbuf, CCGD_AW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006318 --emsg_off;
6319 }
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006320 if (drop_split)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006321 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006322 if (win_split(0, 0) == FAIL)
6323 return;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02006324 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006325
6326 /* When splitting the window, create a new alist. Otherwise the
6327 * existing one is overwritten. */
6328 alist_unlink(curwin->w_alist);
6329 alist_new();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006330 }
6331
6332 /*
6333 * Set up the new argument list.
6334 */
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006335 alist_set(ALIST(curwin), drop_filec, drop_filev, FALSE, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006336
6337 /*
6338 * Move to the first file.
6339 */
6340 /* Fake up a minimal "next" command for do_argfile() */
6341 vim_memset(&ea, 0, sizeof(ea));
6342 ea.cmd = (char_u *)"next";
6343 do_argfile(&ea, 0);
6344
6345 /* do_ecmd() may set need_start_insertmode, but since we never left Insert
6346 * mode that is not needed here. */
6347 need_start_insertmode = FALSE;
6348
6349 /* Restore msg_scroll, otherwise a following command may cause scrolling
6350 * unexpectedly. The screen will be redrawn by the caller, thus
6351 * msg_scroll being set by displaying a message is irrelevant. */
6352 msg_scroll = save_msg_scroll;
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006353
6354 if (drop_callback != NULL)
6355 drop_callback(drop_cookie);
6356
6357 drop_filev = NULL;
6358 drop_busy = FALSE;
6359}
6360
6361/*
6362 * Handle a file drop. The code is here because a drop is *nearly* like an
6363 * :args command, but not quite (we have a list of exact filenames, so we
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01006364 * don't want to (a) parse a command line, or (b) expand wildcards). So the
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006365 * code is very similar to :args and hence needs access to a lot of the static
6366 * functions in this file.
6367 *
6368 * The "filev" list must have been allocated using alloc(), as should each item
6369 * in the list. This function takes over responsibility for freeing the "filev"
6370 * list.
6371 */
6372 void
6373handle_drop(
6374 int filec, // the number of files dropped
6375 char_u **filev, // the list of files dropped
6376 int split, // force splitting the window
6377 void (*callback)(void *), // to be called after setting the argument
6378 // list
6379 void *cookie) // argument for "callback" (allocated)
6380{
6381 // Cannot handle recursive drops, finish the pending one.
6382 if (drop_busy)
6383 {
6384 FreeWild(filec, filev);
6385 vim_free(cookie);
6386 return;
6387 }
6388
6389 // When calling handle_drop() more than once in a row we only use the last
6390 // one.
6391 if (drop_filev != NULL)
6392 {
6393 FreeWild(drop_filec, drop_filev);
6394 vim_free(drop_cookie);
6395 }
6396
6397 drop_filec = filec;
6398 drop_filev = filev;
6399 drop_split = split;
6400 drop_callback = callback;
6401 drop_cookie = cookie;
6402
6403 // Postpone this when:
6404 // - editing the command line
6405 // - not possible to change the current buffer
6406 // - updating the screen
6407 // As it may change buffers and window structures that are in use and cause
6408 // freed memory to be used.
6409 if (text_locked() || curbuf_locked() || updating_screen)
6410 return;
6411
6412 handle_drop_internal();
6413}
6414
6415/*
6416 * To be called when text is unlocked, curbuf is unlocked or updating_screen is
6417 * reset: Handle a postponed drop.
6418 */
6419 void
6420handle_any_postponed_drop(void)
6421{
6422 if (!drop_busy && drop_filev != NULL
6423 && !text_locked() && !curbuf_locked() && !updating_screen)
6424 handle_drop_internal();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006425}
6426#endif
6427
Bram Moolenaar071d4272004-06-13 20:20:40 +00006428/*
6429 * Clear an argument list: free all file names and reset it to zero entries.
6430 */
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00006431 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006432alist_clear(alist_T *al)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006433{
6434 while (--al->al_ga.ga_len >= 0)
6435 vim_free(AARGLIST(al)[al->al_ga.ga_len].ae_fname);
6436 ga_clear(&al->al_ga);
6437}
6438
6439/*
6440 * Init an argument list.
6441 */
6442 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006443alist_init(alist_T *al)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006444{
6445 ga_init2(&al->al_ga, (int)sizeof(aentry_T), 5);
6446}
6447
Bram Moolenaar071d4272004-06-13 20:20:40 +00006448/*
6449 * Remove a reference from an argument list.
6450 * Ignored when the argument list is the global one.
6451 * If the argument list is no longer used by any window, free it.
6452 */
6453 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006454alist_unlink(alist_T *al)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006455{
6456 if (al != &global_alist && --al->al_refcount <= 0)
6457 {
6458 alist_clear(al);
6459 vim_free(al);
6460 }
6461}
6462
Bram Moolenaar071d4272004-06-13 20:20:40 +00006463/*
6464 * Create a new argument list and use it for the current window.
6465 */
6466 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006467alist_new(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006468{
6469 curwin->w_alist = (alist_T *)alloc((unsigned)sizeof(alist_T));
6470 if (curwin->w_alist == NULL)
6471 {
6472 curwin->w_alist = &global_alist;
6473 ++global_alist.al_refcount;
6474 }
6475 else
6476 {
6477 curwin->w_alist->al_refcount = 1;
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02006478 curwin->w_alist->id = ++max_alist_id;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006479 alist_init(curwin->w_alist);
6480 }
6481}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006482
Bram Moolenaara06ecab2016-07-16 14:47:36 +02006483#if !defined(UNIX) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006484/*
6485 * Expand the file names in the global argument list.
Bram Moolenaar86b68352004-12-27 21:59:20 +00006486 * If "fnum_list" is not NULL, use "fnum_list[fnum_len]" as a list of buffer
6487 * numbers to be re-used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006488 */
6489 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006490alist_expand(int *fnum_list, int fnum_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006491{
6492 char_u **old_arg_files;
Bram Moolenaar86b68352004-12-27 21:59:20 +00006493 int old_arg_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006494 char_u **new_arg_files;
6495 int new_arg_file_count;
6496 char_u *save_p_su = p_su;
6497 int i;
6498
6499 /* Don't use 'suffixes' here. This should work like the shell did the
6500 * expansion. Also, the vimrc file isn't read yet, thus the user
6501 * can't set the options. */
6502 p_su = empty_option;
6503 old_arg_files = (char_u **)alloc((unsigned)(sizeof(char_u *) * GARGCOUNT));
6504 if (old_arg_files != NULL)
6505 {
6506 for (i = 0; i < GARGCOUNT; ++i)
Bram Moolenaar86b68352004-12-27 21:59:20 +00006507 old_arg_files[i] = vim_strsave(GARGLIST[i].ae_fname);
6508 old_arg_count = GARGCOUNT;
6509 if (expand_wildcards(old_arg_count, old_arg_files,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006510 &new_arg_file_count, &new_arg_files,
Bram Moolenaarb5609832011-07-20 15:04:58 +02006511 EW_FILE|EW_NOTFOUND|EW_ADDSLASH|EW_NOERROR) == OK
Bram Moolenaar071d4272004-06-13 20:20:40 +00006512 && new_arg_file_count > 0)
6513 {
Bram Moolenaar86b68352004-12-27 21:59:20 +00006514 alist_set(&global_alist, new_arg_file_count, new_arg_files,
6515 TRUE, fnum_list, fnum_len);
6516 FreeWild(old_arg_count, old_arg_files);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006517 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006518 }
6519 p_su = save_p_su;
6520}
6521#endif
6522
6523/*
6524 * Set the argument list for the current window.
6525 * Takes over the allocated files[] and the allocated fnames in it.
6526 */
6527 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006528alist_set(
6529 alist_T *al,
6530 int count,
6531 char_u **files,
6532 int use_curbuf,
6533 int *fnum_list,
6534 int fnum_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006535{
6536 int i;
Bram Moolenaar9e33efd2018-02-09 17:50:28 +01006537 static int recursive = 0;
6538
6539 if (recursive)
6540 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006541 emsg(_(e_au_recursive));
Bram Moolenaar9e33efd2018-02-09 17:50:28 +01006542 return;
6543 }
6544 ++recursive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006545
6546 alist_clear(al);
6547 if (ga_grow(&al->al_ga, count) == OK)
6548 {
6549 for (i = 0; i < count; ++i)
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00006550 {
6551 if (got_int)
6552 {
6553 /* When adding many buffers this can take a long time. Allow
6554 * interrupting here. */
6555 while (i < count)
6556 vim_free(files[i++]);
6557 break;
6558 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00006559
6560 /* May set buffer name of a buffer previously used for the
6561 * argument list, so that it's re-used by alist_add. */
6562 if (fnum_list != NULL && i < fnum_len)
6563 buf_set_name(fnum_list[i], files[i]);
6564
Bram Moolenaar071d4272004-06-13 20:20:40 +00006565 alist_add(al, files[i], use_curbuf ? 2 : 1);
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00006566 ui_breakcheck();
6567 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006568 vim_free(files);
6569 }
6570 else
6571 FreeWild(count, files);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006572 if (al == &global_alist)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006573 arg_had_last = FALSE;
Bram Moolenaar9e33efd2018-02-09 17:50:28 +01006574
6575 --recursive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006576}
6577
6578/*
6579 * Add file "fname" to argument list "al".
6580 * "fname" must have been allocated and "al" must have been checked for room.
6581 */
6582 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006583alist_add(
6584 alist_T *al,
6585 char_u *fname,
6586 int set_fnum) /* 1: set buffer number; 2: re-use curbuf */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006587{
6588 if (fname == NULL) /* don't add NULL file names */
6589 return;
6590#ifdef BACKSLASH_IN_FILENAME
6591 slash_adjust(fname);
6592#endif
6593 AARGLIST(al)[al->al_ga.ga_len].ae_fname = fname;
6594 if (set_fnum > 0)
6595 AARGLIST(al)[al->al_ga.ga_len].ae_fnum =
6596 buflist_add(fname, BLN_LISTED | (set_fnum == 2 ? BLN_CURBUF : 0));
6597 ++al->al_ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006598}
6599
6600#if defined(BACKSLASH_IN_FILENAME) || defined(PROTO)
6601/*
6602 * Adjust slashes in file names. Called after 'shellslash' was set.
6603 */
6604 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006605alist_slash_adjust(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006606{
6607 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006608 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00006609 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006610
6611 for (i = 0; i < GARGCOUNT; ++i)
6612 if (GARGLIST[i].ae_fname != NULL)
6613 slash_adjust(GARGLIST[i].ae_fname);
Bram Moolenaarf740b292006-02-16 22:11:02 +00006614 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006615 if (wp->w_alist != &global_alist)
6616 for (i = 0; i < WARGCOUNT(wp); ++i)
6617 if (WARGLIST(wp)[i].ae_fname != NULL)
6618 slash_adjust(WARGLIST(wp)[i].ae_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006619}
6620#endif
6621
6622/*
6623 * ":preserve".
6624 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006625 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006626ex_preserve(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006627{
Bram Moolenaar4399ef42005-02-12 14:29:27 +00006628 curbuf->b_flags |= BF_PRESERVED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006629 ml_preserve(curbuf, TRUE);
6630}
6631
6632/*
6633 * ":recover".
6634 */
6635 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006636ex_recover(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006637{
6638 /* Set recoverymode right away to avoid the ATTENTION prompt. */
6639 recoverymode = TRUE;
Bram Moolenaar45d3b142013-11-09 03:31:51 +01006640 if (!check_changed(curbuf, (p_awa ? CCGD_AW : 0)
6641 | CCGD_MULTWIN
6642 | (eap->forceit ? CCGD_FORCEIT : 0)
6643 | CCGD_EXCMD)
6644
Bram Moolenaar071d4272004-06-13 20:20:40 +00006645 && (*eap->arg == NUL
6646 || setfname(curbuf, eap->arg, NULL, TRUE) == OK))
6647 ml_recover();
6648 recoverymode = FALSE;
6649}
6650
6651/*
6652 * Command modifier used in a wrong way.
6653 */
6654 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006655ex_wrongmodifier(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006656{
6657 eap->errmsg = e_invcmd;
6658}
6659
Bram Moolenaar071d4272004-06-13 20:20:40 +00006660/*
6661 * :sview [+command] file split window with new file, read-only
6662 * :split [[+command] file] split window with current or new file
6663 * :vsplit [[+command] file] split window vertically with current or new file
6664 * :new [[+command] file] split window with no or new file
6665 * :vnew [[+command] file] split vertically window with no or new file
6666 * :sfind [+command] file split window with file in 'path'
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006667 *
6668 * :tabedit open new Tab page with empty window
6669 * :tabedit [+command] file open new Tab page and edit "file"
6670 * :tabnew [[+command] file] just like :tabedit
6671 * :tabfind [+command] file open new Tab page and find "file"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006672 */
6673 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006674ex_splitview(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006675{
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006676 win_T *old_curwin = curwin;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006677#if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006678 char_u *fname = NULL;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006679#endif
6680#ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00006681 int browse_flag = cmdmod.browse;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006682#endif
Bram Moolenaar39902a02018-06-21 22:10:08 +02006683 int use_tab = eap->cmdidx == CMD_tabedit
6684 || eap->cmdidx == CMD_tabfind
6685 || eap->cmdidx == CMD_tabnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006686
Bram Moolenaar4033c552017-09-16 20:54:51 +02006687#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00006688 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006689#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006690
Bram Moolenaar4033c552017-09-16 20:54:51 +02006691#ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00006692 /* A ":split" in the quickfix window works like ":new". Don't want two
Bram Moolenaar05bb9532008-07-04 09:44:11 +00006693 * quickfix windows. But it's OK when doing ":tab split". */
6694 if (bt_quickfix(curbuf) && cmdmod.tab == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006695 {
6696 if (eap->cmdidx == CMD_split)
6697 eap->cmdidx = CMD_new;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006698 if (eap->cmdidx == CMD_vsplit)
6699 eap->cmdidx = CMD_vnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006700 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02006701#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006702
Bram Moolenaar4033c552017-09-16 20:54:51 +02006703#ifdef FEAT_SEARCHPATH
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006704 if (eap->cmdidx == CMD_sfind || eap->cmdidx == CMD_tabfind)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006705 {
6706 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg),
6707 FNAME_MESS, TRUE, curbuf->b_ffname);
6708 if (fname == NULL)
6709 goto theend;
6710 eap->arg = fname;
6711 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006712# ifdef FEAT_BROWSE
Bram Moolenaar4033c552017-09-16 20:54:51 +02006713 else
6714# endif
6715#endif
6716#ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00006717 if (cmdmod.browse
Bram Moolenaar071d4272004-06-13 20:20:40 +00006718 && eap->cmdidx != CMD_vnew
Bram Moolenaar071d4272004-06-13 20:20:40 +00006719 && eap->cmdidx != CMD_new)
6720 {
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006721 if (
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006722# ifdef FEAT_GUI
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006723 !gui.in_use &&
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006724# endif
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006725 au_has_group((char_u *)"FileExplorer"))
6726 {
6727 /* No browsing supported but we do have the file explorer:
6728 * Edit the directory. */
6729 if (*eap->arg == NUL || !mch_isdir(eap->arg))
6730 eap->arg = (char_u *)".";
6731 }
6732 else
6733 {
Bram Moolenaar39902a02018-06-21 22:10:08 +02006734 fname = do_browse(0, (char_u *)(use_tab
6735 ? _("Edit File in new tab page")
6736 : _("Edit File in new window")),
Bram Moolenaar071d4272004-06-13 20:20:40 +00006737 eap->arg, NULL, NULL, NULL, curbuf);
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006738 if (fname == NULL)
6739 goto theend;
6740 eap->arg = fname;
6741 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006742 }
6743 cmdmod.browse = FALSE; /* Don't browse again in do_ecmd(). */
Bram Moolenaar4033c552017-09-16 20:54:51 +02006744#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006745
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006746 /*
6747 * Either open new tab page or split the window.
6748 */
Bram Moolenaar39902a02018-06-21 22:10:08 +02006749 if (use_tab)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006750 {
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006751 if (win_new_tabpage(cmdmod.tab != 0 ? cmdmod.tab
6752 : eap->addr_count == 0 ? 0
6753 : (int)eap->line2 + 1) != FAIL)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006754 {
Bram Moolenaard2b66012008-01-09 19:30:36 +00006755 do_exedit(eap, old_curwin);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006756
6757 /* set the alternate buffer for the window we came from */
6758 if (curwin != old_curwin
6759 && win_valid(old_curwin)
6760 && old_curwin->w_buffer != curbuf
6761 && !cmdmod.keepalt)
6762 old_curwin->w_alt_fnum = curbuf->b_fnum;
6763 }
6764 }
6765 else if (win_split(eap->addr_count > 0 ? (int)eap->line2 : 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006766 *eap->cmd == 'v' ? WSP_VERT : 0) != FAIL)
6767 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006768 /* Reset 'scrollbind' when editing another file, but keep it when
6769 * doing ":split" without arguments. */
6770 if (*eap->arg != NUL
Bram Moolenaar8a3bb562018-03-04 20:14:14 +01006771# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00006772 || cmdmod.browse
Bram Moolenaar8a3bb562018-03-04 20:14:14 +01006773# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006774 )
Bram Moolenaar3368ea22010-09-21 16:56:35 +02006775 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006776 else
6777 do_check_scrollbind(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006778 do_exedit(eap, old_curwin);
6779 }
6780
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006781# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00006782 cmdmod.browse = browse_flag;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006783# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006784
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006785# if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006786theend:
6787 vim_free(fname);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006788# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006789}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006790
6791/*
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006792 * Open a new tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006793 */
6794 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006795tabpage_new(void)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006796{
6797 exarg_T ea;
6798
6799 vim_memset(&ea, 0, sizeof(ea));
6800 ea.cmdidx = CMD_tabnew;
6801 ea.cmd = (char_u *)"tabn";
6802 ea.arg = (char_u *)"";
6803 ex_splitview(&ea);
6804}
6805
6806/*
6807 * :tabnext command
6808 */
6809 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006810ex_tabnext(exarg_T *eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006811{
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006812 int tab_number;
6813
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006814 switch (eap->cmdidx)
6815 {
6816 case CMD_tabfirst:
6817 case CMD_tabrewind:
6818 goto_tabpage(1);
6819 break;
6820 case CMD_tablast:
6821 goto_tabpage(9999);
6822 break;
6823 case CMD_tabprevious:
6824 case CMD_tabNext:
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006825 if (eap->arg && *eap->arg != NUL)
6826 {
6827 char_u *p = eap->arg;
6828 char_u *p_save = p;
6829
6830 tab_number = getdigits(&p);
6831 if (p == p_save || *p_save == '-' || *p != NUL
6832 || tab_number == 0)
6833 {
6834 /* No numbers as argument. */
6835 eap->errmsg = e_invarg;
6836 return;
6837 }
6838 }
6839 else
6840 {
6841 if (eap->addr_count == 0)
6842 tab_number = 1;
6843 else
6844 {
6845 tab_number = eap->line2;
6846 if (tab_number < 1)
6847 {
6848 eap->errmsg = e_invrange;
6849 return;
6850 }
6851 }
6852 }
6853 goto_tabpage(-tab_number);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006854 break;
6855 default: /* CMD_tabnext */
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006856 tab_number = get_tabpage_arg(eap);
6857 if (eap->errmsg == NULL)
6858 goto_tabpage(tab_number);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006859 break;
6860 }
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006861}
6862
6863/*
6864 * :tabmove command
6865 */
6866 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006867ex_tabmove(exarg_T *eap)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006868{
Bram Moolenaar40ce3a42015-04-21 18:08:39 +02006869 int tab_number;
Bram Moolenaar8cb8dca2012-07-06 18:27:39 +02006870
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006871 tab_number = get_tabpage_arg(eap);
6872 if (eap->errmsg == NULL)
6873 tabpage_move(tab_number);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006874}
6875
6876/*
6877 * :tabs command: List tabs and their contents.
6878 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006879 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006880ex_tabs(exarg_T *eap UNUSED)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006881{
6882 tabpage_T *tp;
6883 win_T *wp;
6884 int tabcount = 1;
6885
6886 msg_start();
6887 msg_scroll = TRUE;
6888 for (tp = first_tabpage; tp != NULL && !got_int; tp = tp->tp_next)
6889 {
6890 msg_putchar('\n');
6891 vim_snprintf((char *)IObuff, IOSIZE, _("Tab page %d"), tabcount++);
Bram Moolenaar8820b482017-03-16 17:23:31 +01006892 msg_outtrans_attr(IObuff, HL_ATTR(HLF_T));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006893 out_flush(); /* output one line at a time */
6894 ui_breakcheck();
6895
Bram Moolenaar030f0df2006-02-21 22:02:53 +00006896 if (tp == curtab)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006897 wp = firstwin;
6898 else
6899 wp = tp->tp_firstwin;
6900 for ( ; wp != NULL && !got_int; wp = wp->w_next)
6901 {
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006902 msg_putchar('\n');
6903 msg_putchar(wp == curwin ? '>' : ' ');
6904 msg_putchar(' ');
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006905 msg_putchar(bufIsChanged(wp->w_buffer) ? '+' : ' ');
6906 msg_putchar(' ');
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006907 if (buf_spname(wp->w_buffer) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02006908 vim_strncpy(IObuff, buf_spname(wp->w_buffer), IOSIZE - 1);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006909 else
6910 home_replace(wp->w_buffer, wp->w_buffer->b_fname,
6911 IObuff, IOSIZE, TRUE);
6912 msg_outtrans(IObuff);
6913 out_flush(); /* output one line at a time */
6914 ui_breakcheck();
6915 }
6916 }
6917}
6918
Bram Moolenaar071d4272004-06-13 20:20:40 +00006919/*
6920 * ":mode": Set screen mode.
6921 * If no argument given, just get the screen size and redraw.
6922 */
6923 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006924ex_mode(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006925{
6926 if (*eap->arg == NUL)
6927 shell_resized();
6928 else
6929 mch_screenmode(eap->arg);
6930}
6931
Bram Moolenaar071d4272004-06-13 20:20:40 +00006932/*
6933 * ":resize".
6934 * set, increment or decrement current window height
6935 */
6936 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006937ex_resize(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006938{
6939 int n;
6940 win_T *wp = curwin;
6941
6942 if (eap->addr_count > 0)
6943 {
6944 n = eap->line2;
6945 for (wp = firstwin; wp->w_next != NULL && --n > 0; wp = wp->w_next)
6946 ;
6947 }
6948
Bram Moolenaar44a2f922016-03-19 22:11:51 +01006949# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00006950 need_mouse_correct = TRUE;
Bram Moolenaar44a2f922016-03-19 22:11:51 +01006951# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006952 n = atol((char *)eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006953 if (cmdmod.split & WSP_VERT)
6954 {
6955 if (*eap->arg == '-' || *eap->arg == '+')
Bram Moolenaar02631462017-09-22 15:20:32 +02006956 n += curwin->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006957 else if (n == 0 && eap->arg[0] == NUL) /* default is very wide */
6958 n = 9999;
6959 win_setwidth_win((int)n, wp);
6960 }
6961 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006962 {
6963 if (*eap->arg == '-' || *eap->arg == '+')
6964 n += curwin->w_height;
Bram Moolenaar1f2903c2017-07-23 19:51:01 +02006965 else if (n == 0 && eap->arg[0] == NUL) /* default is very high */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006966 n = 9999;
6967 win_setheight_win((int)n, wp);
6968 }
6969}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006970
6971/*
6972 * ":find [+command] <file>" command.
6973 */
6974 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006975ex_find(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006976{
6977#ifdef FEAT_SEARCHPATH
6978 char_u *fname;
6979 int count;
6980
6981 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg), FNAME_MESS,
6982 TRUE, curbuf->b_ffname);
6983 if (eap->addr_count > 0)
6984 {
6985 /* Repeat finding the file "count" times. This matters when it
6986 * appears several times in the path. */
6987 count = eap->line2;
6988 while (fname != NULL && --count > 0)
6989 {
6990 vim_free(fname);
6991 fname = find_file_in_path(NULL, 0, FNAME_MESS,
6992 FALSE, curbuf->b_ffname);
6993 }
6994 }
6995
6996 if (fname != NULL)
6997 {
6998 eap->arg = fname;
6999#endif
7000 do_exedit(eap, NULL);
7001#ifdef FEAT_SEARCHPATH
7002 vim_free(fname);
7003 }
7004#endif
7005}
7006
7007/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00007008 * ":open" simulation: for now just work like ":visual".
7009 */
7010 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007011ex_open(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007012{
7013 regmatch_T regmatch;
7014 char_u *p;
7015
7016 curwin->w_cursor.lnum = eap->line2;
7017 beginline(BL_SOL | BL_FIX);
7018 if (*eap->arg == '/')
7019 {
7020 /* ":open /pattern/": put cursor in column found with pattern */
7021 ++eap->arg;
7022 p = skip_regexp(eap->arg, '/', p_magic, NULL);
7023 *p = NUL;
7024 regmatch.regprog = vim_regcomp(eap->arg, p_magic ? RE_MAGIC : 0);
7025 if (regmatch.regprog != NULL)
7026 {
7027 regmatch.rm_ic = p_ic;
7028 p = ml_get_curline();
7029 if (vim_regexec(&regmatch, p, (colnr_T)0))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007030 curwin->w_cursor.col = (colnr_T)(regmatch.startp[0] - p);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007031 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007032 emsg(_(e_nomatch));
Bram Moolenaar473de612013-06-08 18:19:48 +02007033 vim_regfree(regmatch.regprog);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007034 }
7035 /* Move to the NUL, ignore any other arguments. */
7036 eap->arg += STRLEN(eap->arg);
7037 }
7038 check_cursor();
7039
7040 eap->cmdidx = CMD_visual;
7041 do_exedit(eap, NULL);
7042}
7043
7044/*
7045 * ":edit", ":badd", ":visual".
Bram Moolenaar071d4272004-06-13 20:20:40 +00007046 */
7047 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007048ex_edit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007049{
7050 do_exedit(eap, NULL);
7051}
7052
7053/*
7054 * ":edit <file>" command and alikes.
7055 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007056 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007057do_exedit(
7058 exarg_T *eap,
7059 win_T *old_curwin) /* curwin before doing a split or NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007060{
7061 int n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007062 int need_hide;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007063 int exmode_was = exmode_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007064
7065 /*
7066 * ":vi" command ends Ex mode.
7067 */
7068 if (exmode_active && (eap->cmdidx == CMD_visual
7069 || eap->cmdidx == CMD_view))
7070 {
7071 exmode_active = FALSE;
7072 if (*eap->arg == NUL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007073 {
7074 /* Special case: ":global/pat/visual\NLvi-commands" */
7075 if (global_busy)
7076 {
7077 int rd = RedrawingDisabled;
7078 int nwr = no_wait_return;
7079 int ms = msg_scroll;
7080#ifdef FEAT_GUI
7081 int he = hold_gui_events;
7082#endif
7083
7084 if (eap->nextcmd != NULL)
7085 {
7086 stuffReadbuff(eap->nextcmd);
7087 eap->nextcmd = NULL;
7088 }
7089
7090 if (exmode_was != EXMODE_VIM)
7091 settmode(TMODE_RAW);
7092 RedrawingDisabled = 0;
7093 no_wait_return = 0;
7094 need_wait_return = FALSE;
7095 msg_scroll = 0;
7096#ifdef FEAT_GUI
7097 hold_gui_events = 0;
7098#endif
7099 must_redraw = CLEAR;
7100
7101 main_loop(FALSE, TRUE);
7102
7103 RedrawingDisabled = rd;
7104 no_wait_return = nwr;
7105 msg_scroll = ms;
7106#ifdef FEAT_GUI
7107 hold_gui_events = he;
7108#endif
7109 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007110 return;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007111 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007112 }
7113
7114 if ((eap->cmdidx == CMD_new
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007115 || eap->cmdidx == CMD_tabnew
7116 || eap->cmdidx == CMD_tabedit
Bram Moolenaar4033c552017-09-16 20:54:51 +02007117 || eap->cmdidx == CMD_vnew) && *eap->arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007118 {
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007119 /* ":new" or ":tabnew" without argument: edit an new empty buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007120 setpcmark();
7121 (void)do_ecmd(0, NULL, NULL, eap, ECMD_ONE,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00007122 ECMD_HIDE + (eap->forceit ? ECMD_FORCEIT : 0),
7123 old_curwin == NULL ? curwin : NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007124 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02007125 else if ((eap->cmdidx != CMD_split && eap->cmdidx != CMD_vsplit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007126 || *eap->arg != NUL
7127#ifdef FEAT_BROWSE
7128 || cmdmod.browse
7129#endif
7130 )
7131 {
Bram Moolenaar5555acc2006-04-07 21:33:12 +00007132 /* Can't edit another file when "curbuf_lock" is set. Only ":edit"
7133 * can bring us here, others are stopped earlier. */
7134 if (*eap->arg != NUL && curbuf_locked())
7135 return;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007136
Bram Moolenaar071d4272004-06-13 20:20:40 +00007137 n = readonlymode;
7138 if (eap->cmdidx == CMD_view || eap->cmdidx == CMD_sview)
7139 readonlymode = TRUE;
7140 else if (eap->cmdidx == CMD_enew)
7141 readonlymode = FALSE; /* 'readonly' doesn't make sense in an
7142 empty buffer */
7143 setpcmark();
7144 if (do_ecmd(0, (eap->cmdidx == CMD_enew ? NULL : eap->arg),
7145 NULL, eap,
7146 /* ":edit" goes to first line if Vi compatible */
7147 (*eap->arg == NUL && eap->do_ecmd_lnum == 0
7148 && vim_strchr(p_cpo, CPO_GOTO1) != NULL)
7149 ? ECMD_ONE : eap->do_ecmd_lnum,
Bram Moolenaareb44a682017-08-03 22:44:55 +02007150 (buf_hide(curbuf) ? ECMD_HIDE : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007151 + (eap->forceit ? ECMD_FORCEIT : 0)
Bram Moolenaar7b449342014-03-25 13:03:48 +01007152 /* after a split we can use an existing buffer */
7153 + (old_curwin != NULL ? ECMD_OLDBUF : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007154 + (eap->cmdidx == CMD_badd ? ECMD_ADDBUF : 0 )
Bram Moolenaar701f7af2008-11-15 13:12:07 +00007155 , old_curwin == NULL ? curwin : NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007156 {
7157 /* Editing the file failed. If the window was split, close it. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007158 if (old_curwin != NULL)
7159 {
7160 need_hide = (curbufIsChanged() && curbuf->b_nwindows <= 1);
Bram Moolenaareb44a682017-08-03 22:44:55 +02007161 if (!need_hide || buf_hide(curbuf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007162 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007163#if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007164 cleanup_T cs;
7165
7166 /* Reset the error/interrupt/exception state here so that
7167 * aborting() returns FALSE when closing a window. */
7168 enter_cleanup(&cs);
Bram Moolenaar4033c552017-09-16 20:54:51 +02007169#endif
7170#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007171 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02007172#endif
Bram Moolenaareb44a682017-08-03 22:44:55 +02007173 win_close(curwin, !need_hide && !buf_hide(curbuf));
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007174
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007175#if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007176 /* Restore the error/interrupt/exception state if not
7177 * discarded by a new aborting error, interrupt, or
7178 * uncaught exception. */
7179 leave_cleanup(&cs);
Bram Moolenaar4033c552017-09-16 20:54:51 +02007180#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007181 }
7182 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007183 }
7184 else if (readonlymode && curbuf->b_nwindows == 1)
7185 {
7186 /* When editing an already visited buffer, 'readonly' won't be set
7187 * but the previous value is kept. With ":view" and ":sview" we
7188 * want the file to be readonly, except when another window is
7189 * editing the same buffer. */
7190 curbuf->b_p_ro = TRUE;
7191 }
7192 readonlymode = n;
7193 }
7194 else
7195 {
7196 if (eap->do_ecmd_cmd != NULL)
7197 do_cmdline_cmd(eap->do_ecmd_cmd);
7198#ifdef FEAT_TITLE
7199 n = curwin->w_arg_idx_invalid;
7200#endif
7201 check_arg_idx(curwin);
7202#ifdef FEAT_TITLE
7203 if (n != curwin->w_arg_idx_invalid)
7204 maketitle();
7205#endif
7206 }
7207
Bram Moolenaar071d4272004-06-13 20:20:40 +00007208 /*
7209 * if ":split file" worked, set alternate file name in old window to new
7210 * file
7211 */
7212 if (old_curwin != NULL
7213 && *eap->arg != NUL
7214 && curwin != old_curwin
7215 && win_valid(old_curwin)
Bram Moolenaard4755bb2004-09-02 19:12:26 +00007216 && old_curwin->w_buffer != curbuf
7217 && !cmdmod.keepalt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007218 old_curwin->w_alt_fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007219
7220 ex_no_reprint = TRUE;
7221}
7222
7223#ifndef FEAT_GUI
7224/*
7225 * ":gui" and ":gvim" when there is no GUI.
7226 */
7227 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007228ex_nogui(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007229{
7230 eap->errmsg = e_nogvim;
7231}
7232#endif
7233
Bram Moolenaar4f974752019-02-17 17:44:42 +01007234#if defined(FEAT_GUI_MSWIN) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007235 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007236ex_tearoff(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007237{
7238 gui_make_tearoff(eap->arg);
7239}
7240#endif
7241
Bram Moolenaar29a2c082018-03-05 21:06:23 +01007242#if (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK) \
7243 || defined(FEAT_TERM_POPUP_MENU)) && defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007244 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007245ex_popup(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007246{
Bram Moolenaar29a2c082018-03-05 21:06:23 +01007247# if defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK)
7248 if (gui.in_use)
7249 gui_make_popup(eap->arg, eap->forceit);
7250# ifdef FEAT_TERM_POPUP_MENU
7251 else
7252# endif
7253# endif
7254# ifdef FEAT_TERM_POPUP_MENU
7255 pum_make_popup(eap->arg, eap->forceit);
7256# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007257}
7258#endif
7259
Bram Moolenaar071d4272004-06-13 20:20:40 +00007260 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007261ex_swapname(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007262{
7263 if (curbuf->b_ml.ml_mfp == NULL || curbuf->b_ml.ml_mfp->mf_fname == NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01007264 msg(_("No swap file"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007265 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01007266 msg((char *)curbuf->b_ml.ml_mfp->mf_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007267}
7268
7269/*
7270 * ":syncbind" forces all 'scrollbind' windows to have the same relative
7271 * offset.
7272 * (1998-11-02 16:21:01 R. Edward Ralston <eralston@computer.org>)
7273 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007274 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007275ex_syncbind(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007276{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007277 win_T *wp;
Bram Moolenaardedd1b02013-12-14 13:06:17 +01007278 win_T *save_curwin = curwin;
7279 buf_T *save_curbuf = curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007280 long topline;
7281 long y;
7282 linenr_T old_linenr = curwin->w_cursor.lnum;
7283
7284 setpcmark();
7285
7286 /*
7287 * determine max topline
7288 */
7289 if (curwin->w_p_scb)
7290 {
7291 topline = curwin->w_topline;
Bram Moolenaar29323592016-07-24 22:04:11 +02007292 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007293 {
7294 if (wp->w_p_scb && wp->w_buffer)
7295 {
Bram Moolenaar375e3392019-01-31 18:26:10 +01007296 y = wp->w_buffer->b_ml.ml_line_count - get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007297 if (topline > y)
7298 topline = y;
7299 }
7300 }
7301 if (topline < 1)
7302 topline = 1;
7303 }
7304 else
7305 {
7306 topline = 1;
7307 }
7308
7309
7310 /*
Bram Moolenaardedd1b02013-12-14 13:06:17 +01007311 * Set all scrollbind windows to the same topline.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007312 */
Bram Moolenaar29323592016-07-24 22:04:11 +02007313 FOR_ALL_WINDOWS(curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007314 {
7315 if (curwin->w_p_scb)
7316 {
Bram Moolenaardedd1b02013-12-14 13:06:17 +01007317 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007318 y = topline - curwin->w_topline;
7319 if (y > 0)
7320 scrollup(y, TRUE);
7321 else
7322 scrolldown(-y, TRUE);
7323 curwin->w_scbind_pos = topline;
7324 redraw_later(VALID);
7325 cursor_correct();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007326 curwin->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007327 }
7328 }
Bram Moolenaardedd1b02013-12-14 13:06:17 +01007329 curwin = save_curwin;
7330 curbuf = save_curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007331 if (curwin->w_p_scb)
7332 {
7333 did_syncbind = TRUE;
7334 checkpcmark();
7335 if (old_linenr != curwin->w_cursor.lnum)
7336 {
7337 char_u ctrl_o[2];
7338
7339 ctrl_o[0] = Ctrl_O;
7340 ctrl_o[1] = 0;
7341 ins_typebuf(ctrl_o, REMAP_NONE, 0, TRUE, FALSE);
7342 }
7343 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007344}
7345
7346
7347 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007348ex_read(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007349{
Bram Moolenaardf177f62005-02-22 08:39:57 +00007350 int i;
7351 int empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
7352 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007353
7354 if (eap->usefilter) /* :r!cmd */
7355 do_bang(1, eap, FALSE, FALSE, TRUE);
7356 else
7357 {
7358 if (u_save(eap->line2, (linenr_T)(eap->line2 + 1)) == FAIL)
7359 return;
7360
7361#ifdef FEAT_BROWSE
7362 if (cmdmod.browse)
7363 {
7364 char_u *browseFile;
7365
Bram Moolenaar7171abe2004-10-11 10:06:20 +00007366 browseFile = do_browse(0, (char_u *)_("Append File"), eap->arg,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007367 NULL, NULL, NULL, curbuf);
7368 if (browseFile != NULL)
7369 {
7370 i = readfile(browseFile, NULL,
7371 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
7372 vim_free(browseFile);
7373 }
7374 else
7375 i = OK;
7376 }
7377 else
7378#endif
7379 if (*eap->arg == NUL)
7380 {
7381 if (check_fname() == FAIL) /* check for no file name */
7382 return;
7383 i = readfile(curbuf->b_ffname, curbuf->b_fname,
7384 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
7385 }
7386 else
7387 {
7388 if (vim_strchr(p_cpo, CPO_ALTREAD) != NULL)
7389 (void)setaltfname(eap->arg, eap->arg, (linenr_T)1);
7390 i = readfile(eap->arg, NULL,
7391 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
7392
7393 }
Bram Moolenaare13b9af2017-01-13 22:01:02 +01007394 if (i != OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007395 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007396#if defined(FEAT_EVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007397 if (!aborting())
7398#endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007399 semsg(_(e_notopen), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007400 }
7401 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00007402 {
7403 if (empty && exmode_active)
7404 {
7405 /* Delete the empty line that remains. Historically ex does
7406 * this but vi doesn't. */
7407 if (eap->line2 == 0)
7408 lnum = curbuf->b_ml.ml_line_count;
7409 else
7410 lnum = 1;
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00007411 if (*ml_get(lnum) == NUL && u_savedel(lnum, 1L) == OK)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007412 {
7413 ml_delete(lnum, FALSE);
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00007414 if (curwin->w_cursor.lnum > 1
7415 && curwin->w_cursor.lnum >= lnum)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007416 --curwin->w_cursor.lnum;
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00007417 deleted_lines_mark(lnum, 1L);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007418 }
7419 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007420 redraw_curbuf_later(VALID);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007421 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007422 }
7423}
7424
Bram Moolenaarea408852005-06-25 22:49:46 +00007425static char_u *prev_dir = NULL;
7426
7427#if defined(EXITFREE) || defined(PROTO)
7428 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007429free_cd_dir(void)
Bram Moolenaarea408852005-06-25 22:49:46 +00007430{
Bram Moolenaard23a8232018-02-10 18:45:26 +01007431 VIM_CLEAR(prev_dir);
7432 VIM_CLEAR(globaldir);
Bram Moolenaarea408852005-06-25 22:49:46 +00007433}
7434#endif
7435
Bram Moolenaarf4258302013-06-02 18:20:17 +02007436/*
7437 * Deal with the side effects of changing the current directory.
7438 * When "local" is TRUE then this was after an ":lcd" command.
7439 */
7440 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007441post_chdir(int local)
Bram Moolenaarf4258302013-06-02 18:20:17 +02007442{
Bram Moolenaard23a8232018-02-10 18:45:26 +01007443 VIM_CLEAR(curwin->w_localdir);
Bram Moolenaarf4258302013-06-02 18:20:17 +02007444 if (local)
7445 {
7446 /* If still in global directory, need to remember current
7447 * directory as global directory. */
7448 if (globaldir == NULL && prev_dir != NULL)
7449 globaldir = vim_strsave(prev_dir);
7450 /* Remember this local directory for the window. */
7451 if (mch_dirname(NameBuff, MAXPATHL) == OK)
7452 curwin->w_localdir = vim_strsave(NameBuff);
7453 }
7454 else
7455 {
7456 /* We are now in the global directory, no need to remember its
7457 * name. */
Bram Moolenaard23a8232018-02-10 18:45:26 +01007458 VIM_CLEAR(globaldir);
Bram Moolenaarf4258302013-06-02 18:20:17 +02007459 }
7460
7461 shorten_fnames(TRUE);
7462}
7463
Bram Moolenaarea408852005-06-25 22:49:46 +00007464
Bram Moolenaar071d4272004-06-13 20:20:40 +00007465/*
7466 * ":cd", ":lcd", ":chdir" and ":lchdir".
7467 */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00007468 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007469ex_cd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007470{
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01007471 char_u *new_dir;
7472 char_u *tofree;
7473 int dir_differs;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007474
7475 new_dir = eap->arg;
7476#if !defined(UNIX) && !defined(VMS)
7477 /* for non-UNIX ":cd" means: print current directory */
7478 if (*new_dir == NUL)
7479 ex_pwd(NULL);
7480 else
7481#endif
7482 {
Bram Moolenaar8e8fe9b2009-03-11 14:37:32 +00007483 if (allbuf_locked())
7484 return;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007485 if (vim_strchr(p_cpo, CPO_CHDIR) != NULL && curbufIsChanged()
7486 && !eap->forceit)
7487 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007488 emsg(_("E747: Cannot change directory, buffer is modified (add ! to override)"));
Bram Moolenaardf177f62005-02-22 08:39:57 +00007489 return;
7490 }
7491
Bram Moolenaar071d4272004-06-13 20:20:40 +00007492 /* ":cd -": Change to previous directory */
7493 if (STRCMP(new_dir, "-") == 0)
7494 {
7495 if (prev_dir == NULL)
7496 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007497 emsg(_("E186: No previous directory"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007498 return;
7499 }
7500 new_dir = prev_dir;
7501 }
7502
7503 /* Save current directory for next ":cd -" */
7504 tofree = prev_dir;
7505 if (mch_dirname(NameBuff, MAXPATHL) == OK)
7506 prev_dir = vim_strsave(NameBuff);
7507 else
7508 prev_dir = NULL;
7509
7510#if defined(UNIX) || defined(VMS)
7511 /* for UNIX ":cd" means: go to home directory */
7512 if (*new_dir == NUL)
7513 {
7514 /* use NameBuff for home directory name */
7515# ifdef VMS
7516 char_u *p;
7517
7518 p = mch_getenv((char_u *)"SYS$LOGIN");
7519 if (p == NULL || *p == NUL) /* empty is the same as not set */
7520 NameBuff[0] = NUL;
7521 else
Bram Moolenaarb6356332005-07-18 21:40:44 +00007522 vim_strncpy(NameBuff, p, MAXPATHL - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007523# else
7524 expand_env((char_u *)"$HOME", NameBuff, MAXPATHL);
7525# endif
7526 new_dir = NameBuff;
7527 }
7528#endif
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01007529 dir_differs = new_dir == NULL || prev_dir == NULL
Bram Moolenaar9eb76af2018-12-16 16:30:21 +01007530 || pathcmp((char *)prev_dir, (char *)new_dir, -1) != 0;
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01007531 if (new_dir == NULL || (dir_differs && vim_chdir(new_dir)))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007532 emsg(_(e_failed));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007533 else
7534 {
Bram Moolenaarb7407d32018-02-03 17:36:27 +01007535 int is_local_chdir = eap->cmdidx == CMD_lcd
7536 || eap->cmdidx == CMD_lchdir;
7537
7538 post_chdir(is_local_chdir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007539
7540 /* Echo the new current directory if the command was typed. */
Bram Moolenaarfcfbc672009-07-09 18:13:49 +00007541 if (KeyTyped || p_verbose >= 5)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007542 ex_pwd(eap);
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01007543
7544 if (dir_differs)
7545 apply_autocmds(EVENT_DIRCHANGED,
7546 is_local_chdir ? (char_u *)"window" : (char_u *)"global",
7547 new_dir, FALSE, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007548 }
7549 vim_free(tofree);
7550 }
7551}
7552
7553/*
7554 * ":pwd".
7555 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007556 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007557ex_pwd(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007558{
7559 if (mch_dirname(NameBuff, MAXPATHL) == OK)
7560 {
7561#ifdef BACKSLASH_IN_FILENAME
7562 slash_adjust(NameBuff);
7563#endif
Bram Moolenaar32526b32019-01-19 17:43:09 +01007564 msg((char *)NameBuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007565 }
7566 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007567 emsg(_("E187: Unknown"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007568}
7569
7570/*
7571 * ":=".
7572 */
7573 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007574ex_equal(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007575{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007576 smsg("%ld", (long)eap->line2);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007577 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007578}
7579
7580 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007581ex_sleep(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007582{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007583 int n;
7584 long len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007585
7586 if (cursor_valid())
7587 {
7588 n = W_WINROW(curwin) + curwin->w_wrow - msg_scrolled;
7589 if (n >= 0)
Bram Moolenaar53f81742017-09-22 14:35:51 +02007590 windgoto((int)n, curwin->w_wincol + curwin->w_wcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007591 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007592
7593 len = eap->line2;
7594 switch (*eap->arg)
7595 {
7596 case 'm': break;
7597 case NUL: len *= 1000L; break;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007598 default: semsg(_(e_invarg2), eap->arg); return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007599 }
7600 do_sleep(len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007601}
7602
7603/*
7604 * Sleep for "msec" milliseconds, but keep checking for a CTRL-C every second.
7605 */
7606 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007607do_sleep(long msec)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007608{
7609 long done;
Bram Moolenaar975b5272016-03-15 23:10:59 +01007610 long wait_now;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007611
7612 cursor_on();
Bram Moolenaarf45d7472018-09-30 18:22:26 +02007613 out_flush_cursor(FALSE, FALSE);
Bram Moolenaar975b5272016-03-15 23:10:59 +01007614 for (done = 0; !got_int && done < msec; done += wait_now)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007615 {
Bram Moolenaar975b5272016-03-15 23:10:59 +01007616 wait_now = msec - done > 1000L ? 1000L : msec - done;
7617#ifdef FEAT_TIMERS
7618 {
7619 long due_time = check_due_timer();
7620
7621 if (due_time > 0 && due_time < wait_now)
7622 wait_now = due_time;
7623 }
7624#endif
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02007625#ifdef FEAT_JOB_CHANNEL
7626 if (has_any_channel() && wait_now > 100L)
7627 wait_now = 100L;
7628#endif
Bram Moolenaar975b5272016-03-15 23:10:59 +01007629 ui_delay(wait_now, TRUE);
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02007630#ifdef FEAT_JOB_CHANNEL
7631 if (has_any_channel())
7632 ui_breakcheck_force(TRUE);
7633 else
7634#endif
7635 ui_breakcheck();
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007636#ifdef MESSAGE_QUEUE
7637 /* Process the netbeans and clientserver messages that may have been
7638 * received in the call to ui_breakcheck() when the GUI is in use. This
7639 * may occur when running a test case. */
7640 parse_queued_messages();
Bram Moolenaare3cc6d42011-10-20 21:58:34 +02007641#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007642 }
Bram Moolenaar1ebff3d2018-07-07 16:18:13 +02007643
7644 // If CTRL-C was typed to interrupt the sleep, drop the CTRL-C from the
7645 // input buffer, otherwise a following call to input() fails.
7646 if (got_int)
7647 (void)vpeekc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007648}
7649
7650 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007651do_exmap(exarg_T *eap, int isabbrev)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007652{
7653 int mode;
7654 char_u *cmdp;
7655
7656 cmdp = eap->cmd;
7657 mode = get_map_mode(&cmdp, eap->forceit || isabbrev);
7658
7659 switch (do_map((*cmdp == 'n') ? 2 : (*cmdp == 'u'),
7660 eap->arg, mode, isabbrev))
7661 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007662 case 1: emsg(_(e_invarg));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007663 break;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007664 case 2: emsg((isabbrev ? _(e_noabbr) : _(e_nomap)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007665 break;
7666 }
7667}
7668
7669/*
7670 * ":winsize" command (obsolete).
7671 */
7672 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007673ex_winsize(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007674{
7675 int w, h;
7676 char_u *arg = eap->arg;
7677 char_u *p;
7678
7679 w = getdigits(&arg);
7680 arg = skipwhite(arg);
7681 p = arg;
7682 h = getdigits(&arg);
7683 if (*p != NUL && *arg == NUL)
7684 set_shellsize(w, h, TRUE);
7685 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007686 emsg(_("E465: :winsize requires two number arguments"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007687}
7688
Bram Moolenaar071d4272004-06-13 20:20:40 +00007689 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007690ex_wincmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007691{
7692 int xchar = NUL;
7693 char_u *p;
7694
7695 if (*eap->arg == 'g' || *eap->arg == Ctrl_G)
7696 {
7697 /* CTRL-W g and CTRL-W CTRL-G have an extra command character */
7698 if (eap->arg[1] == NUL)
7699 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007700 emsg(_(e_invarg));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007701 return;
7702 }
7703 xchar = eap->arg[1];
7704 p = eap->arg + 2;
7705 }
7706 else
7707 p = eap->arg + 1;
7708
7709 eap->nextcmd = check_nextcmd(p);
7710 p = skipwhite(p);
7711 if (*p != NUL && *p != '"' && eap->nextcmd == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007712 emsg(_(e_invarg));
Bram Moolenaar12bde492011-06-13 01:19:56 +02007713 else if (!eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007714 {
7715 /* Pass flags on for ":vertical wincmd ]". */
7716 postponed_split_flags = cmdmod.split;
Bram Moolenaard326ce82007-03-11 14:48:29 +00007717 postponed_split_tab = cmdmod.tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007718 do_window(*eap->arg, eap->addr_count > 0 ? eap->line2 : 0L, xchar);
7719 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00007720 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007721 }
7722}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007723
Bram Moolenaar843ee412004-06-30 16:16:41 +00007724#if defined(FEAT_GUI) || defined(UNIX) || defined(VMS) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007725/*
7726 * ":winpos".
7727 */
7728 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007729ex_winpos(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007730{
7731 int x, y;
7732 char_u *arg = eap->arg;
7733 char_u *p;
7734
7735 if (*arg == NUL)
7736 {
Bram Moolenaar843ee412004-06-30 16:16:41 +00007737# if defined(FEAT_GUI) || defined(MSWIN)
7738# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007739 if (gui.in_use && gui_mch_get_winpos(&x, &y) != FAIL)
Bram Moolenaar843ee412004-06-30 16:16:41 +00007740# else
7741 if (mch_get_winpos(&x, &y) != FAIL)
7742# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007743 {
7744 sprintf((char *)IObuff, _("Window position: X %d, Y %d"), x, y);
Bram Moolenaar32526b32019-01-19 17:43:09 +01007745 msg((char *)IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007746 }
7747 else
7748# endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007749 emsg(_("E188: Obtaining window position not implemented for this platform"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007750 }
7751 else
7752 {
7753 x = getdigits(&arg);
7754 arg = skipwhite(arg);
7755 p = arg;
7756 y = getdigits(&arg);
7757 if (*p == NUL || *arg != NUL)
7758 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007759 emsg(_("E466: :winpos requires two number arguments"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007760 return;
7761 }
7762# ifdef FEAT_GUI
7763 if (gui.in_use)
7764 gui_mch_set_winpos(x, y);
7765 else if (gui.starting)
7766 {
7767 /* Remember the coordinates for when the window is opened. */
7768 gui_win_x = x;
7769 gui_win_y = y;
7770 }
7771# ifdef HAVE_TGETENT
7772 else
7773# endif
Bram Moolenaar843ee412004-06-30 16:16:41 +00007774# else
7775# ifdef MSWIN
7776 mch_set_winpos(x, y);
7777# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007778# endif
7779# ifdef HAVE_TGETENT
7780 if (*T_CWP)
7781 term_set_winpos(x, y);
7782# endif
7783 }
7784}
7785#endif
7786
7787/*
7788 * Handle command that work like operators: ":delete", ":yank", ":>" and ":<".
7789 */
7790 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007791ex_operators(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007792{
7793 oparg_T oa;
7794
7795 clear_oparg(&oa);
7796 oa.regname = eap->regname;
7797 oa.start.lnum = eap->line1;
7798 oa.end.lnum = eap->line2;
7799 oa.line_count = eap->line2 - eap->line1 + 1;
7800 oa.motion_type = MLINE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007801 virtual_op = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007802 if (eap->cmdidx != CMD_yank) /* position cursor for undo */
7803 {
7804 setpcmark();
7805 curwin->w_cursor.lnum = eap->line1;
7806 beginline(BL_SOL | BL_FIX);
7807 }
7808
Bram Moolenaard07c6e12013-11-21 14:21:40 +01007809 if (VIsual_active)
7810 end_visual_mode();
Bram Moolenaard07c6e12013-11-21 14:21:40 +01007811
Bram Moolenaar071d4272004-06-13 20:20:40 +00007812 switch (eap->cmdidx)
7813 {
7814 case CMD_delete:
7815 oa.op_type = OP_DELETE;
7816 op_delete(&oa);
7817 break;
7818
7819 case CMD_yank:
7820 oa.op_type = OP_YANK;
7821 (void)op_yank(&oa, FALSE, TRUE);
7822 break;
7823
7824 default: /* CMD_rshift or CMD_lshift */
Bram Moolenaar6e707362013-06-14 19:15:58 +02007825 if (
Bram Moolenaar071d4272004-06-13 20:20:40 +00007826#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6e707362013-06-14 19:15:58 +02007827 (eap->cmdidx == CMD_rshift) ^ curwin->w_p_rl
7828#else
7829 eap->cmdidx == CMD_rshift
Bram Moolenaar071d4272004-06-13 20:20:40 +00007830#endif
Bram Moolenaar6e707362013-06-14 19:15:58 +02007831 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00007832 oa.op_type = OP_RSHIFT;
7833 else
7834 oa.op_type = OP_LSHIFT;
7835 op_shift(&oa, FALSE, eap->amount);
7836 break;
7837 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007838 virtual_op = MAYBE;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007839 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007840}
7841
7842/*
7843 * ":put".
7844 */
7845 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007846ex_put(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007847{
7848 /* ":0put" works like ":1put!". */
7849 if (eap->line2 == 0)
7850 {
7851 eap->line2 = 1;
7852 eap->forceit = TRUE;
7853 }
7854 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007855 do_put(eap->regname, eap->forceit ? BACKWARD : FORWARD, 1L,
7856 PUT_LINE|PUT_CURSLINE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007857}
7858
7859/*
7860 * Handle ":copy" and ":move".
7861 */
7862 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007863ex_copymove(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007864{
7865 long n;
7866
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02007867 n = get_address(eap, &eap->arg, eap->addr_type, FALSE, FALSE, FALSE, 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007868 if (eap->arg == NULL) /* error detected */
7869 {
7870 eap->nextcmd = NULL;
7871 return;
7872 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00007873 get_flags(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007874
7875 /*
7876 * move or copy lines from 'eap->line1'-'eap->line2' to below line 'n'
7877 */
7878 if (n == MAXLNUM || n < 0 || n > curbuf->b_ml.ml_line_count)
7879 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007880 emsg(_(e_invaddr));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007881 return;
7882 }
7883
7884 if (eap->cmdidx == CMD_move)
7885 {
7886 if (do_move(eap->line1, eap->line2, n) == FAIL)
7887 return;
7888 }
7889 else
7890 ex_copy(eap->line1, eap->line2, n);
7891 u_clearline();
7892 beginline(BL_SOL | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007893 ex_may_print(eap);
7894}
7895
7896/*
7897 * Print the current line if flags were given to the Ex command.
7898 */
Bram Moolenaarfd3fe982014-04-01 17:49:44 +02007899 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007900ex_may_print(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007901{
7902 if (eap->flags != 0)
7903 {
7904 print_line(curwin->w_cursor.lnum, (eap->flags & EXFLAG_NR),
7905 (eap->flags & EXFLAG_LIST));
7906 ex_no_reprint = TRUE;
7907 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007908}
7909
7910/*
7911 * ":smagic" and ":snomagic".
7912 */
7913 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007914ex_submagic(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007915{
7916 int magic_save = p_magic;
7917
7918 p_magic = (eap->cmdidx == CMD_smagic);
7919 do_sub(eap);
7920 p_magic = magic_save;
7921}
7922
7923/*
7924 * ":join".
7925 */
7926 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007927ex_join(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007928{
7929 curwin->w_cursor.lnum = eap->line1;
7930 if (eap->line1 == eap->line2)
7931 {
7932 if (eap->addr_count >= 2) /* :2,2join does nothing */
7933 return;
7934 if (eap->line2 == curbuf->b_ml.ml_line_count)
7935 {
7936 beep_flush();
7937 return;
7938 }
7939 ++eap->line2;
7940 }
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02007941 (void)do_join(eap->line2 - eap->line1 + 1, !eap->forceit, TRUE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007942 beginline(BL_WHITE | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007943 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007944}
7945
7946/*
7947 * ":[addr]@r" or ":[addr]*r": execute register
7948 */
7949 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007950ex_at(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007951{
7952 int c;
Bram Moolenaar60462872009-11-03 11:40:19 +00007953 int prev_len = typebuf.tb_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007954
7955 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaar4930a762016-09-11 14:39:53 +02007956 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007957
7958#ifdef USE_ON_FLY_SCROLL
7959 dont_scroll = TRUE; /* disallow scrolling here */
7960#endif
7961
7962 /* get the register name. No name means to use the previous one */
7963 c = *eap->arg;
7964 if (c == NUL || (c == '*' && *eap->cmd == '*'))
7965 c = '@';
Bram Moolenaard333d1e2006-11-07 17:43:47 +00007966 /* Put the register in the typeahead buffer with the "silent" flag. */
7967 if (do_execreg(c, TRUE, vim_strchr(p_cpo, CPO_EXECBUF) != NULL, TRUE)
7968 == FAIL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007969 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007970 beep_flush();
Bram Moolenaardf177f62005-02-22 08:39:57 +00007971 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007972 else
7973 {
7974 int save_efr = exec_from_reg;
7975
7976 exec_from_reg = TRUE;
7977
7978 /*
7979 * Execute from the typeahead buffer.
Bram Moolenaar60462872009-11-03 11:40:19 +00007980 * Continue until the stuff buffer is empty and all added characters
7981 * have been consumed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007982 */
Bram Moolenaar60462872009-11-03 11:40:19 +00007983 while (!stuff_empty() || typebuf.tb_len > prev_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007984 (void)do_cmdline(NULL, getexline, NULL, DOCMD_NOWAIT|DOCMD_VERBOSE);
7985
7986 exec_from_reg = save_efr;
7987 }
7988}
7989
7990/*
7991 * ":!".
7992 */
7993 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007994ex_bang(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007995{
7996 do_bang(eap->addr_count, eap, eap->forceit, TRUE, TRUE);
7997}
7998
7999/*
8000 * ":undo".
8001 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008002 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +01008003ex_undo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008004{
Bram Moolenaard3667a22006-03-16 21:35:52 +00008005 if (eap->addr_count == 1) /* :undo 123 */
Bram Moolenaar730cde92010-06-27 05:18:54 +02008006 undo_time(eap->line2, FALSE, FALSE, TRUE);
Bram Moolenaard3667a22006-03-16 21:35:52 +00008007 else
8008 u_undo(1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008009}
8010
Bram Moolenaar55debbe2010-05-23 23:34:36 +02008011#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar6df6f472010-07-18 18:04:50 +02008012 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008013ex_wundo(exarg_T *eap)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02008014{
8015 char_u hash[UNDO_HASH_SIZE];
8016
8017 u_compute_hash(hash);
8018 u_write_undo(eap->arg, eap->forceit, curbuf, hash);
8019}
8020
Bram Moolenaar6df6f472010-07-18 18:04:50 +02008021 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008022ex_rundo(exarg_T *eap)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02008023{
8024 char_u hash[UNDO_HASH_SIZE];
8025
8026 u_compute_hash(hash);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02008027 u_read_undo(eap->arg, hash, NULL);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02008028}
8029#endif
8030
Bram Moolenaar071d4272004-06-13 20:20:40 +00008031/*
8032 * ":redo".
8033 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008034 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008035ex_redo(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008036{
8037 u_redo(1);
8038}
8039
8040/*
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008041 * ":earlier" and ":later".
8042 */
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008043 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008044ex_later(exarg_T *eap)
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008045{
8046 long count = 0;
8047 int sec = FALSE;
Bram Moolenaar730cde92010-06-27 05:18:54 +02008048 int file = FALSE;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008049 char_u *p = eap->arg;
8050
8051 if (*p == NUL)
8052 count = 1;
8053 else if (isdigit(*p))
8054 {
8055 count = getdigits(&p);
8056 switch (*p)
8057 {
8058 case 's': ++p; sec = TRUE; break;
8059 case 'm': ++p; sec = TRUE; count *= 60; break;
8060 case 'h': ++p; sec = TRUE; count *= 60 * 60; break;
Bram Moolenaar730cde92010-06-27 05:18:54 +02008061 case 'd': ++p; sec = TRUE; count *= 24 * 60 * 60; break;
8062 case 'f': ++p; file = TRUE; break;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008063 }
8064 }
8065
8066 if (*p != NUL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008067 semsg(_(e_invarg2), eap->arg);
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008068 else
Bram Moolenaar730cde92010-06-27 05:18:54 +02008069 undo_time(eap->cmdidx == CMD_earlier ? -count : count,
8070 sec, file, FALSE);
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008071}
8072
8073/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008074 * ":redir": start/stop redirection.
8075 */
8076 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008077ex_redir(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008078{
8079 char *mode;
8080 char_u *fname;
Bram Moolenaarca472992005-01-21 11:46:23 +00008081 char_u *arg = eap->arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008082
Bram Moolenaarba768492016-07-08 15:32:54 +02008083#ifdef FEAT_EVAL
Bram Moolenaar79815f12016-07-09 17:07:29 +02008084 if (redir_execute)
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02008085 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008086 emsg(_("E930: Cannot use :redir inside execute()"));
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02008087 return;
8088 }
Bram Moolenaarba768492016-07-08 15:32:54 +02008089#endif
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02008090
Bram Moolenaar071d4272004-06-13 20:20:40 +00008091 if (STRICMP(eap->arg, "END") == 0)
8092 close_redir();
8093 else
8094 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008095 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008096 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008097 ++arg;
8098 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008099 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008100 ++arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008101 mode = "a";
8102 }
8103 else
8104 mode = "w";
Bram Moolenaarca472992005-01-21 11:46:23 +00008105 arg = skipwhite(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008106
8107 close_redir();
8108
8109 /* Expand environment variables and "~/". */
Bram Moolenaarca472992005-01-21 11:46:23 +00008110 fname = expand_env_save(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008111 if (fname == NULL)
8112 return;
8113#ifdef FEAT_BROWSE
8114 if (cmdmod.browse)
8115 {
8116 char_u *browseFile;
8117
Bram Moolenaar7171abe2004-10-11 10:06:20 +00008118 browseFile = do_browse(BROWSE_SAVE,
8119 (char_u *)_("Save Redirection"),
Bram Moolenaarc36651b2018-04-29 12:22:56 +02008120 fname, NULL, NULL,
8121 (char_u *)_(BROWSE_FILTER_ALL_FILES), curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008122 if (browseFile == NULL)
8123 return; /* operation cancelled */
8124 vim_free(fname);
8125 fname = browseFile;
8126 eap->forceit = TRUE; /* since dialog already asked */
8127 }
8128#endif
8129
8130 redir_fd = open_exfile(fname, eap->forceit, mode);
8131 vim_free(fname);
8132 }
8133#ifdef FEAT_EVAL
Bram Moolenaarca472992005-01-21 11:46:23 +00008134 else if (*arg == '@')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008135 {
8136 /* redirect to a register a-z (resp. A-Z for appending) */
8137 close_redir();
Bram Moolenaarca472992005-01-21 11:46:23 +00008138 ++arg;
8139 if (ASCII_ISALPHA(*arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008140# ifdef FEAT_CLIPBOARD
Bram Moolenaarca472992005-01-21 11:46:23 +00008141 || *arg == '*'
Bram Moolenaar9a51c6e2006-11-14 19:25:02 +00008142 || *arg == '+'
Bram Moolenaar071d4272004-06-13 20:20:40 +00008143# endif
Bram Moolenaarca472992005-01-21 11:46:23 +00008144 || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008145 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008146 redir_reg = *arg++;
Bram Moolenaarc188b882007-10-19 14:20:54 +00008147 if (*arg == '>' && arg[1] == '>') /* append */
Bram Moolenaard9d30582005-05-18 22:10:28 +00008148 arg += 2;
Bram Moolenaarc188b882007-10-19 14:20:54 +00008149 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008150 {
Bram Moolenaarc188b882007-10-19 14:20:54 +00008151 /* Can use both "@a" and "@a>". */
Bram Moolenaar2c29bee2005-06-01 21:46:07 +00008152 if (*arg == '>')
8153 arg++;
Bram Moolenaarc188b882007-10-19 14:20:54 +00008154 /* Make register empty when not using @A-@Z and the
8155 * command is valid. */
8156 if (*arg == NUL && !isupper(redir_reg))
8157 write_reg_contents(redir_reg, (char_u *)"", -1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008158 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00008159 }
8160 if (*arg != NUL)
8161 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00008162 redir_reg = 0;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008163 semsg(_(e_invarg2), eap->arg);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008164 }
8165 }
8166 else if (*arg == '=' && arg[1] == '>')
8167 {
8168 int append;
8169
8170 /* redirect to a variable */
8171 close_redir();
8172 arg += 2;
8173
8174 if (*arg == '>')
8175 {
8176 ++arg;
8177 append = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008178 }
8179 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00008180 append = FALSE;
8181
8182 if (var_redir_start(skipwhite(arg), append) == OK)
8183 redir_vname = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008184 }
8185#endif
8186
8187 /* TODO: redirect to a buffer */
8188
Bram Moolenaar071d4272004-06-13 20:20:40 +00008189 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008190 semsg(_(e_invarg2), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008191 }
Bram Moolenaar29b2d262006-09-10 19:07:28 +00008192
8193 /* Make sure redirection is not off. Can happen for cmdline completion
8194 * that indirectly invokes a command to catch its output. */
8195 if (redir_fd != NULL
8196#ifdef FEAT_EVAL
8197 || redir_reg || redir_vname
8198#endif
8199 )
8200 redir_off = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008201}
8202
8203/*
8204 * ":redraw": force redraw
8205 */
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01008206 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008207ex_redraw(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008208{
8209 int r = RedrawingDisabled;
8210 int p = p_lz;
8211
8212 RedrawingDisabled = 0;
8213 p_lz = FALSE;
Bram Moolenaarabc39ab2017-03-01 18:04:05 +01008214 validate_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008215 update_topline();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01008216 update_screen(eap->forceit ? CLEAR : VIsual_active ? INVERTED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008217#ifdef FEAT_TITLE
8218 if (need_maketitle)
8219 maketitle();
8220#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01008221#if defined(MSWIN) && !defined(FEAT_GUI_MSWIN)
Bram Moolenaar78d21da2019-02-17 15:00:52 +01008222 resize_console_buf();
8223#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008224 RedrawingDisabled = r;
8225 p_lz = p;
8226
8227 /* Reset msg_didout, so that a message that's there is overwritten. */
8228 msg_didout = FALSE;
8229 msg_col = 0;
8230
Bram Moolenaar56667a52013-07-17 11:54:28 +02008231 /* No need to wait after an intentional redraw. */
8232 need_wait_return = FALSE;
8233
Bram Moolenaar071d4272004-06-13 20:20:40 +00008234 out_flush();
8235}
8236
8237/*
8238 * ":redrawstatus": force redraw of status line(s)
8239 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008240 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008241ex_redrawstatus(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008242{
Bram Moolenaar071d4272004-06-13 20:20:40 +00008243 int r = RedrawingDisabled;
8244 int p = p_lz;
8245
8246 RedrawingDisabled = 0;
8247 p_lz = FALSE;
8248 if (eap->forceit)
8249 status_redraw_all();
8250 else
8251 status_redraw_curbuf();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01008252 update_screen(VIsual_active ? INVERTED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008253 RedrawingDisabled = r;
8254 p_lz = p;
8255 out_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008256}
8257
Bram Moolenaare12bab32019-01-08 22:02:56 +01008258/*
8259 * ":redrawtabline": force redraw of the tabline
8260 */
8261 static void
8262ex_redrawtabline(exarg_T *eap UNUSED)
8263{
8264 int r = RedrawingDisabled;
8265 int p = p_lz;
8266
8267 RedrawingDisabled = 0;
8268 p_lz = FALSE;
8269
8270 draw_tabline();
8271
8272 RedrawingDisabled = r;
8273 p_lz = p;
8274 out_flush();
8275}
8276
Bram Moolenaar071d4272004-06-13 20:20:40 +00008277 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008278close_redir(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008279{
8280 if (redir_fd != NULL)
8281 {
8282 fclose(redir_fd);
8283 redir_fd = NULL;
8284 }
8285#ifdef FEAT_EVAL
8286 redir_reg = 0;
Bram Moolenaardf177f62005-02-22 08:39:57 +00008287 if (redir_vname)
8288 {
8289 var_redir_stop();
8290 redir_vname = 0;
8291 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008292#endif
8293}
8294
8295#if defined(FEAT_SESSION) && defined(USE_CRNL)
8296# define MKSESSION_NL
8297static int mksession_nl = FALSE; /* use NL only in put_eol() */
8298#endif
8299
8300/*
8301 * ":mkexrc", ":mkvimrc", ":mkview" and ":mksession".
8302 */
8303 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008304ex_mkrc(
8305 exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008306{
8307 FILE *fd;
8308 int failed = FALSE;
8309 char_u *fname;
8310#ifdef FEAT_BROWSE
8311 char_u *browseFile = NULL;
8312#endif
8313#ifdef FEAT_SESSION
8314 int view_session = FALSE;
8315 int using_vdir = FALSE; /* using 'viewdir'? */
8316 char_u *viewFile = NULL;
8317 unsigned *flagp;
8318#endif
8319
8320 if (eap->cmdidx == CMD_mksession || eap->cmdidx == CMD_mkview)
8321 {
8322#ifdef FEAT_SESSION
8323 view_session = TRUE;
8324#else
8325 ex_ni(eap);
8326 return;
8327#endif
8328 }
8329
8330#ifdef FEAT_SESSION
Bram Moolenaar8d594672009-07-01 18:18:57 +00008331 /* Use the short file name until ":lcd" is used. We also don't use the
8332 * short file name when 'acd' is set, that is checked later. */
Bram Moolenaareeefcc72007-05-01 21:21:21 +00008333 did_lcd = FALSE;
8334
Bram Moolenaar071d4272004-06-13 20:20:40 +00008335 /* ":mkview" or ":mkview 9": generate file name with 'viewdir' */
8336 if (eap->cmdidx == CMD_mkview
8337 && (*eap->arg == NUL
8338 || (vim_isdigit(*eap->arg) && eap->arg[1] == NUL)))
8339 {
8340 eap->forceit = TRUE;
8341 fname = get_view_file(*eap->arg);
8342 if (fname == NULL)
8343 return;
8344 viewFile = fname;
8345 using_vdir = TRUE;
8346 }
8347 else
8348#endif
8349 if (*eap->arg != NUL)
8350 fname = eap->arg;
8351 else if (eap->cmdidx == CMD_mkvimrc)
8352 fname = (char_u *)VIMRC_FILE;
8353#ifdef FEAT_SESSION
8354 else if (eap->cmdidx == CMD_mksession)
8355 fname = (char_u *)SESSION_FILE;
8356#endif
8357 else
8358 fname = (char_u *)EXRC_FILE;
8359
8360#ifdef FEAT_BROWSE
8361 if (cmdmod.browse)
8362 {
Bram Moolenaar7171abe2004-10-11 10:06:20 +00008363 browseFile = do_browse(BROWSE_SAVE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008364# ifdef FEAT_SESSION
8365 eap->cmdidx == CMD_mkview ? (char_u *)_("Save View") :
8366 eap->cmdidx == CMD_mksession ? (char_u *)_("Save Session") :
8367# endif
8368 (char_u *)_("Save Setup"),
Bram Moolenaarc36651b2018-04-29 12:22:56 +02008369 fname, (char_u *)"vim", NULL,
8370 (char_u *)_(BROWSE_FILTER_MACROS), NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008371 if (browseFile == NULL)
8372 goto theend;
8373 fname = browseFile;
8374 eap->forceit = TRUE; /* since dialog already asked */
8375 }
8376#endif
8377
8378#if defined(FEAT_SESSION) && defined(vim_mkdir)
8379 /* When using 'viewdir' may have to create the directory. */
8380 if (using_vdir && !mch_isdir(p_vdir))
Bram Moolenaardf177f62005-02-22 08:39:57 +00008381 vim_mkdir_emsg(p_vdir, 0755);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008382#endif
8383
8384 fd = open_exfile(fname, eap->forceit, WRITEBIN);
8385 if (fd != NULL)
8386 {
8387#ifdef FEAT_SESSION
8388 if (eap->cmdidx == CMD_mkview)
8389 flagp = &vop_flags;
8390 else
8391 flagp = &ssop_flags;
8392#endif
8393
8394#ifdef MKSESSION_NL
8395 /* "unix" in 'sessionoptions': use NL line separator */
8396 if (view_session && (*flagp & SSOP_UNIX))
8397 mksession_nl = TRUE;
8398#endif
8399
8400 /* Write the version command for :mkvimrc */
8401 if (eap->cmdidx == CMD_mkvimrc)
8402 (void)put_line(fd, "version 6.0");
8403
8404#ifdef FEAT_SESSION
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00008405 if (eap->cmdidx == CMD_mksession)
8406 {
8407 if (put_line(fd, "let SessionLoad = 1") == FAIL)
8408 failed = TRUE;
8409 }
8410
Bram Moolenaar071d4272004-06-13 20:20:40 +00008411 if (eap->cmdidx != CMD_mkview)
8412#endif
8413 {
8414 /* Write setting 'compatible' first, because it has side effects.
8415 * For that same reason only do it when needed. */
8416 if (p_cp)
8417 (void)put_line(fd, "if !&cp | set cp | endif");
8418 else
8419 (void)put_line(fd, "if &cp | set nocp | endif");
8420 }
8421
8422#ifdef FEAT_SESSION
8423 if (!view_session
8424 || (eap->cmdidx == CMD_mksession
8425 && (*flagp & SSOP_OPTIONS)))
8426#endif
8427 failed |= (makemap(fd, NULL) == FAIL
8428 || makeset(fd, OPT_GLOBAL, FALSE) == FAIL);
8429
8430#ifdef FEAT_SESSION
8431 if (!failed && view_session)
8432 {
8433 if (put_line(fd, "let s:so_save = &so | let s:siso_save = &siso | set so=0 siso=0") == FAIL)
8434 failed = TRUE;
8435 if (eap->cmdidx == CMD_mksession)
8436 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02008437 char_u *dirnow; /* current directory */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008438
Bram Moolenaard9462e32011-04-11 21:35:11 +02008439 dirnow = alloc(MAXPATHL);
8440 if (dirnow == NULL)
8441 failed = TRUE;
8442 else
8443 {
8444 /*
8445 * Change to session file's dir.
8446 */
8447 if (mch_dirname(dirnow, MAXPATHL) == FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +00008448 || mch_chdir((char *)dirnow) != 0)
Bram Moolenaard9462e32011-04-11 21:35:11 +02008449 *dirnow = NUL;
8450 if (*dirnow != NUL && (ssop_flags & SSOP_SESDIR))
8451 {
Bram Moolenaarb7407d32018-02-03 17:36:27 +01008452 if (vim_chdirfile(fname, NULL) == OK)
Bram Moolenaard9462e32011-04-11 21:35:11 +02008453 shorten_fnames(TRUE);
8454 }
8455 else if (*dirnow != NUL
8456 && (ssop_flags & SSOP_CURDIR) && globaldir != NULL)
8457 {
8458 if (mch_chdir((char *)globaldir) == 0)
8459 shorten_fnames(TRUE);
8460 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008461
Bram Moolenaard9462e32011-04-11 21:35:11 +02008462 failed |= (makeopens(fd, dirnow) == FAIL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008463
Bram Moolenaard9462e32011-04-11 21:35:11 +02008464 /* restore original dir */
8465 if (*dirnow != NUL && ((ssop_flags & SSOP_SESDIR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008466 || ((ssop_flags & SSOP_CURDIR) && globaldir != NULL)))
Bram Moolenaard9462e32011-04-11 21:35:11 +02008467 {
8468 if (mch_chdir((char *)dirnow) != 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008469 emsg(_(e_prev_dir));
Bram Moolenaard9462e32011-04-11 21:35:11 +02008470 shorten_fnames(TRUE);
8471 }
8472 vim_free(dirnow);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008473 }
8474 }
8475 else
8476 {
Bram Moolenaarf13be0d2008-01-02 14:13:10 +00008477 failed |= (put_view(fd, curwin, !using_vdir, flagp,
8478 -1) == FAIL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008479 }
8480 if (put_line(fd, "let &so = s:so_save | let &siso = s:siso_save")
8481 == FAIL)
8482 failed = TRUE;
Bram Moolenaare3c74d22019-01-12 16:29:30 +01008483# ifdef FEAT_SEARCH_EXTRA
8484 if (no_hlsearch && put_line(fd, "nohlsearch") == FAIL)
8485 failed = TRUE;
8486# endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00008487 if (put_line(fd, "doautoall SessionLoadPost") == FAIL)
8488 failed = TRUE;
Bram Moolenaar4770d092006-01-12 23:22:24 +00008489 if (eap->cmdidx == CMD_mksession)
8490 {
8491 if (put_line(fd, "unlet SessionLoad") == FAIL)
8492 failed = TRUE;
8493 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008494 }
8495#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00008496 if (put_line(fd, "\" vim: set ft=vim :") == FAIL)
8497 failed = TRUE;
8498
Bram Moolenaar071d4272004-06-13 20:20:40 +00008499 failed |= fclose(fd);
8500
8501 if (failed)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008502 emsg(_(e_write));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008503#if defined(FEAT_EVAL) && defined(FEAT_SESSION)
8504 else if (eap->cmdidx == CMD_mksession)
8505 {
8506 /* successful session write - set this_session var */
Bram Moolenaard9462e32011-04-11 21:35:11 +02008507 char_u *tbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008508
Bram Moolenaard9462e32011-04-11 21:35:11 +02008509 tbuf = alloc(MAXPATHL);
8510 if (tbuf != NULL)
8511 {
8512 if (vim_FullName(fname, tbuf, MAXPATHL, FALSE) == OK)
8513 set_vim_var_string(VV_THIS_SESSION, tbuf, -1);
8514 vim_free(tbuf);
8515 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008516 }
8517#endif
8518#ifdef MKSESSION_NL
8519 mksession_nl = FALSE;
8520#endif
8521 }
8522
8523#ifdef FEAT_BROWSE
8524theend:
8525 vim_free(browseFile);
8526#endif
8527#ifdef FEAT_SESSION
8528 vim_free(viewFile);
8529#endif
8530}
8531
Bram Moolenaardf177f62005-02-22 08:39:57 +00008532#if ((defined(FEAT_SESSION) || defined(FEAT_EVAL)) && defined(vim_mkdir)) \
8533 || defined(PROTO)
8534 int
Bram Moolenaarf1d25012016-03-03 12:22:53 +01008535vim_mkdir_emsg(char_u *name, int prot)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008536{
8537 if (vim_mkdir(name, prot) != 0)
8538 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008539 semsg(_("E739: Cannot create directory: %s"), name);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008540 return FAIL;
8541 }
8542 return OK;
8543}
8544#endif
8545
Bram Moolenaar071d4272004-06-13 20:20:40 +00008546/*
8547 * Open a file for writing for an Ex command, with some checks.
8548 * Return file descriptor, or NULL on failure.
8549 */
8550 FILE *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008551open_exfile(
8552 char_u *fname,
8553 int forceit,
8554 char *mode) /* "w" for create new file or "a" for append */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008555{
8556 FILE *fd;
8557
8558#ifdef UNIX
8559 /* with Unix it is possible to open a directory */
8560 if (mch_isdir(fname))
8561 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008562 semsg(_(e_isadir2), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008563 return NULL;
8564 }
8565#endif
8566 if (!forceit && *mode != 'a' && vim_fexists(fname))
8567 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008568 semsg(_("E189: \"%s\" exists (add ! to override)"), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008569 return NULL;
8570 }
8571
8572 if ((fd = mch_fopen((char *)fname, mode)) == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008573 semsg(_("E190: Cannot open \"%s\" for writing"), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008574
8575 return fd;
8576}
8577
8578/*
8579 * ":mark" and ":k".
8580 */
8581 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008582ex_mark(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008583{
8584 pos_T pos;
8585
8586 if (*eap->arg == NUL) /* No argument? */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008587 emsg(_(e_argreq));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008588 else if (eap->arg[1] != NUL) /* more than one character? */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008589 emsg(_(e_trailing));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008590 else
8591 {
8592 pos = curwin->w_cursor; /* save curwin->w_cursor */
8593 curwin->w_cursor.lnum = eap->line2;
8594 beginline(BL_WHITE | BL_FIX);
8595 if (setmark(*eap->arg) == FAIL) /* set mark */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008596 emsg(_("E191: Argument must be a letter or forward/backward quote"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008597 curwin->w_cursor = pos; /* restore curwin->w_cursor */
8598 }
8599}
8600
8601/*
8602 * Update w_topline, w_leftcol and the cursor position.
8603 */
8604 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008605update_topline_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008606{
8607 check_cursor(); /* put cursor on valid line */
8608 update_topline();
8609 if (!curwin->w_p_wrap)
8610 validate_cursor();
8611 update_curswant();
8612}
8613
Bram Moolenaar071d4272004-06-13 20:20:40 +00008614/*
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008615 * Save the current State and go to Normal mode.
8616 * Return TRUE if the typeahead could be saved.
8617 */
8618 int
8619save_current_state(save_state_T *sst)
8620{
8621 sst->save_msg_scroll = msg_scroll;
8622 sst->save_restart_edit = restart_edit;
8623 sst->save_msg_didout = msg_didout;
8624 sst->save_State = State;
8625 sst->save_insertmode = p_im;
8626 sst->save_finish_op = finish_op;
8627 sst->save_opcount = opcount;
Bram Moolenaarcce713d2019-03-04 11:40:12 +01008628 sst->save_reg_executing = reg_executing;
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008629
8630 msg_scroll = FALSE; /* no msg scrolling in Normal mode */
8631 restart_edit = 0; /* don't go to Insert mode */
8632 p_im = FALSE; /* don't use 'insertmode' */
8633
8634 /*
8635 * Save the current typeahead. This is required to allow using ":normal"
8636 * from an event handler and makes sure we don't hang when the argument
8637 * ends with half a command.
8638 */
8639 save_typeahead(&sst->tabuf);
8640 return sst->tabuf.typebuf_valid;
8641}
8642
8643 void
8644restore_current_state(save_state_T *sst)
8645{
8646 /* Restore the previous typeahead. */
8647 restore_typeahead(&sst->tabuf);
8648
8649 msg_scroll = sst->save_msg_scroll;
8650 restart_edit = sst->save_restart_edit;
8651 p_im = sst->save_insertmode;
8652 finish_op = sst->save_finish_op;
8653 opcount = sst->save_opcount;
Bram Moolenaarcce713d2019-03-04 11:40:12 +01008654 reg_executing = sst->save_reg_executing;
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008655 msg_didout |= sst->save_msg_didout; /* don't reset msg_didout now */
8656
8657 /* Restore the state (needed when called from a function executed for
8658 * 'indentexpr'). Update the mouse and cursor, they may have changed. */
8659 State = sst->save_State;
8660#ifdef CURSOR_SHAPE
8661 ui_cursor_shape(); /* may show different cursor shape */
8662#endif
8663}
8664
8665/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008666 * ":normal[!] {commands}": Execute normal mode commands.
8667 */
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01008668 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008669ex_normal(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008670{
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008671 save_state_T save_state;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008672 char_u *arg = NULL;
8673 int l;
8674 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008675
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008676 if (ex_normal_lock > 0)
8677 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008678 emsg(_(e_secure));
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008679 return;
8680 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008681 if (ex_normal_busy >= p_mmd)
8682 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008683 emsg(_("E192: Recursive use of :normal too deep"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008684 return;
8685 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008686
Bram Moolenaar071d4272004-06-13 20:20:40 +00008687 /*
8688 * vgetc() expects a CSI and K_SPECIAL to have been escaped. Don't do
8689 * this for the K_SPECIAL leading byte, otherwise special keys will not
8690 * work.
8691 */
8692 if (has_mbyte)
8693 {
8694 int len = 0;
8695
8696 /* Count the number of characters to be escaped. */
8697 for (p = eap->arg; *p != NUL; ++p)
8698 {
Bram Moolenaar13505972019-01-24 15:04:48 +01008699#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008700 if (*p == CSI) /* leadbyte CSI */
8701 len += 2;
Bram Moolenaar13505972019-01-24 15:04:48 +01008702#endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008703 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008704 if (*++p == K_SPECIAL /* trailbyte K_SPECIAL or CSI */
Bram Moolenaar13505972019-01-24 15:04:48 +01008705#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008706 || *p == CSI
Bram Moolenaar13505972019-01-24 15:04:48 +01008707#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008708 )
8709 len += 2;
8710 }
8711 if (len > 0)
8712 {
8713 arg = alloc((unsigned)(STRLEN(eap->arg) + len + 1));
8714 if (arg != NULL)
8715 {
8716 len = 0;
8717 for (p = eap->arg; *p != NUL; ++p)
8718 {
8719 arg[len++] = *p;
Bram Moolenaar13505972019-01-24 15:04:48 +01008720#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008721 if (*p == CSI)
8722 {
8723 arg[len++] = KS_EXTRA;
8724 arg[len++] = (int)KE_CSI;
8725 }
Bram Moolenaar13505972019-01-24 15:04:48 +01008726#endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008727 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008728 {
8729 arg[len++] = *++p;
8730 if (*p == K_SPECIAL)
8731 {
8732 arg[len++] = KS_SPECIAL;
8733 arg[len++] = KE_FILLER;
8734 }
Bram Moolenaar13505972019-01-24 15:04:48 +01008735#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008736 else if (*p == CSI)
8737 {
8738 arg[len++] = KS_EXTRA;
8739 arg[len++] = (int)KE_CSI;
8740 }
Bram Moolenaar13505972019-01-24 15:04:48 +01008741#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008742 }
8743 arg[len] = NUL;
8744 }
8745 }
8746 }
8747 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008748
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008749 ++ex_normal_busy;
8750 if (save_current_state(&save_state))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008751 {
8752 /*
8753 * Repeat the :normal command for each line in the range. When no
8754 * range given, execute it just once, without positioning the cursor
8755 * first.
8756 */
8757 do
8758 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008759 if (eap->addr_count != 0)
8760 {
8761 curwin->w_cursor.lnum = eap->line1++;
8762 curwin->w_cursor.col = 0;
Bram Moolenaard5d37532017-03-27 23:02:07 +02008763 check_cursor_moved(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008764 }
8765
Bram Moolenaar13505972019-01-24 15:04:48 +01008766 exec_normal_cmd(arg != NULL
8767 ? arg
8768 : eap->arg, eap->forceit ? REMAP_NONE : REMAP_YES, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008769 }
8770 while (eap->addr_count > 0 && eap->line1 <= eap->line2 && !got_int);
8771 }
8772
8773 /* Might not return to the main loop when in an event handler. */
8774 update_topline_cursor();
8775
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008776 restore_current_state(&save_state);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008777 --ex_normal_busy;
Bram Moolenaareda73602014-11-05 09:53:23 +01008778#ifdef FEAT_MOUSE
8779 setmouse();
8780#endif
8781#ifdef CURSOR_SHAPE
8782 ui_cursor_shape(); /* may show different cursor shape */
8783#endif
8784
Bram Moolenaar071d4272004-06-13 20:20:40 +00008785 vim_free(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008786}
8787
8788/*
Bram Moolenaar64969662005-12-14 21:59:55 +00008789 * ":startinsert", ":startreplace" and ":startgreplace"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008790 */
8791 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008792ex_startinsert(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008793{
Bram Moolenaarfd371682005-01-14 21:42:54 +00008794 if (eap->forceit)
8795 {
Bram Moolenaar09ca9322017-09-26 17:40:45 +02008796 /* cursor line can be zero on startup */
8797 if (!curwin->w_cursor.lnum)
8798 curwin->w_cursor.lnum = 1;
Bram Moolenaarfd371682005-01-14 21:42:54 +00008799 coladvance((colnr_T)MAXCOL);
8800 curwin->w_curswant = MAXCOL;
8801 curwin->w_set_curswant = FALSE;
8802 }
8803
Bram Moolenaara40c5002005-01-09 21:16:21 +00008804 /* Ignore the command when already in Insert mode. Inserting an
8805 * expression register that invokes a function can do this. */
8806 if (State & INSERT)
8807 return;
8808
Bram Moolenaar64969662005-12-14 21:59:55 +00008809 if (eap->cmdidx == CMD_startinsert)
8810 restart_edit = 'a';
8811 else if (eap->cmdidx == CMD_startreplace)
8812 restart_edit = 'R';
Bram Moolenaar071d4272004-06-13 20:20:40 +00008813 else
Bram Moolenaar64969662005-12-14 21:59:55 +00008814 restart_edit = 'V';
8815
8816 if (!eap->forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008817 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00008818 if (eap->cmdidx == CMD_startinsert)
8819 restart_edit = 'i';
Bram Moolenaar071d4272004-06-13 20:20:40 +00008820 curwin->w_curswant = 0; /* avoid MAXCOL */
8821 }
8822}
8823
8824/*
8825 * ":stopinsert"
8826 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008827 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008828ex_stopinsert(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008829{
8830 restart_edit = 0;
8831 stop_insert_mode = TRUE;
Bram Moolenaarfd773e92016-04-02 19:39:16 +02008832 clearmode();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008833}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008834
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008835/*
8836 * Execute normal mode command "cmd".
8837 * "remap" can be REMAP_NONE or REMAP_YES.
8838 */
8839 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008840exec_normal_cmd(char_u *cmd, int remap, int silent)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008841{
Bram Moolenaar25281632016-01-21 23:32:32 +01008842 /* Stuff the argument into the typeahead buffer. */
8843 ins_typebuf(cmd, remap, 0, TRUE, silent);
Bram Moolenaar586c70c2018-10-02 16:23:58 +02008844 exec_normal(FALSE, FALSE, FALSE);
Bram Moolenaar25281632016-01-21 23:32:32 +01008845}
Bram Moolenaar25281632016-01-21 23:32:32 +01008846
Bram Moolenaar25281632016-01-21 23:32:32 +01008847/*
8848 * Execute normal_cmd() until there is no typeahead left.
Bram Moolenaar586c70c2018-10-02 16:23:58 +02008849 * When "use_vpeekc" is TRUE use vpeekc() to check for available chars.
Bram Moolenaar25281632016-01-21 23:32:32 +01008850 */
8851 void
Bram Moolenaar586c70c2018-10-02 16:23:58 +02008852exec_normal(int was_typed, int use_vpeekc, int may_use_terminal_loop UNUSED)
Bram Moolenaar25281632016-01-21 23:32:32 +01008853{
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008854 oparg_T oa;
Bram Moolenaar905dd902019-04-07 14:21:47 +02008855 int c;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008856
Bram Moolenaar905dd902019-04-07 14:21:47 +02008857 // When calling vpeekc() from feedkeys() it will return Ctrl_C when there
8858 // is nothing to get, so also check for Ctrl_C.
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008859 clear_oparg(&oa);
8860 finish_op = FALSE;
Bram Moolenaar586c70c2018-10-02 16:23:58 +02008861 while ((!stuff_empty()
8862 || ((was_typed || !typebuf_typed()) && typebuf.tb_len > 0)
Bram Moolenaar905dd902019-04-07 14:21:47 +02008863 || (use_vpeekc && (c = vpeekc()) != NUL && c != Ctrl_C))
Bram Moolenaar586c70c2018-10-02 16:23:58 +02008864 && !got_int)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008865 {
8866 update_topline_cursor();
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02008867#ifdef FEAT_TERMINAL
Bram Moolenaar655a82a2018-05-08 22:01:07 +02008868 if (may_use_terminal_loop && term_use_loop()
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02008869 && oa.op_type == OP_NOP && oa.regname == NUL
8870 && !VIsual_active)
8871 {
8872 /* If terminal_loop() returns OK we got a key that is handled
8873 * in Normal model. With FAIL we first need to position the
8874 * cursor and the screen needs to be redrawn. */
8875 if (terminal_loop(TRUE) == OK)
8876 normal_cmd(&oa, TRUE);
8877 }
8878 else
8879#endif
8880 /* execute a Normal mode cmd */
8881 normal_cmd(&oa, TRUE);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008882 }
8883}
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008884
Bram Moolenaar071d4272004-06-13 20:20:40 +00008885#ifdef FEAT_FIND_ID
8886 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008887ex_checkpath(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008888{
8889 find_pattern_in_path(NULL, 0, 0, FALSE, FALSE, CHECK_PATH, 1L,
8890 eap->forceit ? ACTION_SHOW_ALL : ACTION_SHOW,
8891 (linenr_T)1, (linenr_T)MAXLNUM);
8892}
8893
Bram Moolenaar4033c552017-09-16 20:54:51 +02008894#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008895/*
8896 * ":psearch"
8897 */
8898 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008899ex_psearch(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008900{
8901 g_do_tagpreview = p_pvh;
8902 ex_findpat(eap);
8903 g_do_tagpreview = 0;
8904}
8905#endif
8906
8907 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008908ex_findpat(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008909{
8910 int whole = TRUE;
8911 long n;
8912 char_u *p;
8913 int action;
8914
8915 switch (cmdnames[eap->cmdidx].cmd_name[2])
8916 {
8917 case 'e': /* ":psearch", ":isearch" and ":dsearch" */
8918 if (cmdnames[eap->cmdidx].cmd_name[0] == 'p')
8919 action = ACTION_GOTO;
8920 else
8921 action = ACTION_SHOW;
8922 break;
8923 case 'i': /* ":ilist" and ":dlist" */
8924 action = ACTION_SHOW_ALL;
8925 break;
8926 case 'u': /* ":ijump" and ":djump" */
8927 action = ACTION_GOTO;
8928 break;
8929 default: /* ":isplit" and ":dsplit" */
8930 action = ACTION_SPLIT;
8931 break;
8932 }
8933
8934 n = 1;
8935 if (vim_isdigit(*eap->arg)) /* get count */
8936 {
8937 n = getdigits(&eap->arg);
8938 eap->arg = skipwhite(eap->arg);
8939 }
8940 if (*eap->arg == '/') /* Match regexp, not just whole words */
8941 {
8942 whole = FALSE;
8943 ++eap->arg;
8944 p = skip_regexp(eap->arg, '/', p_magic, NULL);
8945 if (*p)
8946 {
8947 *p++ = NUL;
8948 p = skipwhite(p);
8949
8950 /* Check for trailing illegal characters */
8951 if (!ends_excmd(*p))
8952 eap->errmsg = e_trailing;
8953 else
8954 eap->nextcmd = check_nextcmd(p);
8955 }
8956 }
8957 if (!eap->skip)
8958 find_pattern_in_path(eap->arg, 0, (int)STRLEN(eap->arg),
8959 whole, !eap->forceit,
8960 *eap->cmd == 'd' ? FIND_DEFINE : FIND_ANY,
8961 n, action, eap->line1, eap->line2);
8962}
8963#endif
8964
Bram Moolenaar071d4272004-06-13 20:20:40 +00008965
Bram Moolenaar4033c552017-09-16 20:54:51 +02008966#ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00008967/*
8968 * ":ptag", ":ptselect", ":ptjump", ":ptnext", etc.
8969 */
8970 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008971ex_ptag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008972{
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01008973 g_do_tagpreview = p_pvh; /* will be reset to 0 in ex_tag_cmd() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008974 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
8975}
8976
8977/*
8978 * ":pedit"
8979 */
8980 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008981ex_pedit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008982{
8983 win_T *curwin_save = curwin;
8984
8985 g_do_tagpreview = p_pvh;
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00008986 prepare_tagpreview(TRUE);
Bram Moolenaar67883b42017-07-27 22:57:00 +02008987 keep_help_flag = bt_help(curwin_save->w_buffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008988 do_exedit(eap, NULL);
8989 keep_help_flag = FALSE;
8990 if (curwin != curwin_save && win_valid(curwin_save))
8991 {
8992 /* Return cursor to where we were */
8993 validate_cursor();
8994 redraw_later(VALID);
8995 win_enter(curwin_save, TRUE);
8996 }
8997 g_do_tagpreview = 0;
8998}
Bram Moolenaar4033c552017-09-16 20:54:51 +02008999#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009000
9001/*
9002 * ":stag", ":stselect" and ":stjump".
9003 */
9004 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009005ex_stag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009006{
9007 postponed_split = -1;
9008 postponed_split_flags = cmdmod.split;
Bram Moolenaard326ce82007-03-11 14:48:29 +00009009 postponed_split_tab = cmdmod.tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009010 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
9011 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00009012 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009013}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009014
9015/*
9016 * ":tag", ":tselect", ":tjump", ":tnext", etc.
9017 */
9018 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009019ex_tag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009020{
9021 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name);
9022}
9023
9024 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009025ex_tag_cmd(exarg_T *eap, char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009026{
9027 int cmd;
9028
9029 switch (name[1])
9030 {
9031 case 'j': cmd = DT_JUMP; /* ":tjump" */
9032 break;
9033 case 's': cmd = DT_SELECT; /* ":tselect" */
9034 break;
9035 case 'p': cmd = DT_PREV; /* ":tprevious" */
9036 break;
9037 case 'N': cmd = DT_PREV; /* ":tNext" */
9038 break;
9039 case 'n': cmd = DT_NEXT; /* ":tnext" */
9040 break;
9041 case 'o': cmd = DT_POP; /* ":pop" */
9042 break;
9043 case 'f': /* ":tfirst" */
9044 case 'r': cmd = DT_FIRST; /* ":trewind" */
9045 break;
9046 case 'l': cmd = DT_LAST; /* ":tlast" */
9047 break;
9048 default: /* ":tag" */
9049#ifdef FEAT_CSCOPE
Bram Moolenaar7c94c262008-06-20 09:11:34 +00009050 if (p_cst && *eap->arg != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009051 {
Bram Moolenaard4db7712016-11-12 19:16:46 +01009052 ex_cstag(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009053 return;
9054 }
9055#endif
9056 cmd = DT_TAG;
9057 break;
9058 }
9059
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00009060 if (name[0] == 'l')
9061 {
9062#ifndef FEAT_QUICKFIX
9063 ex_ni(eap);
9064 return;
9065#else
9066 cmd = DT_LTAG;
9067#endif
9068 }
9069
Bram Moolenaar071d4272004-06-13 20:20:40 +00009070 do_tag(eap->arg, cmd, eap->addr_count > 0 ? (int)eap->line2 : 1,
9071 eap->forceit, TRUE);
9072}
9073
9074/*
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009075 * Check "str" for starting with a special cmdline variable.
9076 * If found return one of the SPEC_ values and set "*usedlen" to the length of
9077 * the variable. Otherwise return -1 and "*usedlen" is unchanged.
9078 */
9079 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009080find_cmdline_var(char_u *src, int *usedlen)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009081{
9082 int len;
9083 int i;
Bram Moolenaar8f0b2d42009-05-16 14:41:10 +00009084 static char *(spec_str[]) = {
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009085 "%",
9086#define SPEC_PERC 0
9087 "#",
Bram Moolenaar65f08472017-09-10 18:16:20 +02009088#define SPEC_HASH (SPEC_PERC + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009089 "<cword>", /* cursor word */
Bram Moolenaar65f08472017-09-10 18:16:20 +02009090#define SPEC_CWORD (SPEC_HASH + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009091 "<cWORD>", /* cursor WORD */
Bram Moolenaar65f08472017-09-10 18:16:20 +02009092#define SPEC_CCWORD (SPEC_CWORD + 1)
9093 "<cexpr>", /* expr under cursor */
9094#define SPEC_CEXPR (SPEC_CCWORD + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009095 "<cfile>", /* cursor path name */
Bram Moolenaar65f08472017-09-10 18:16:20 +02009096#define SPEC_CFILE (SPEC_CEXPR + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009097 "<sfile>", /* ":so" file name */
Bram Moolenaar65f08472017-09-10 18:16:20 +02009098#define SPEC_SFILE (SPEC_CFILE + 1)
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009099 "<slnum>", /* ":so" file line number */
Bram Moolenaar65f08472017-09-10 18:16:20 +02009100#define SPEC_SLNUM (SPEC_SFILE + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009101 "<afile>", /* autocommand file name */
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009102#define SPEC_AFILE (SPEC_SLNUM + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009103 "<abuf>", /* autocommand buffer number */
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009104#define SPEC_ABUF (SPEC_AFILE + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009105 "<amatch>", /* autocommand match name */
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01009106#define SPEC_AMATCH (SPEC_ABUF + 1)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009107 "<sflnum>", /* script file line number */
9108#define SPEC_SFLNUM (SPEC_AMATCH + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009109#ifdef FEAT_CLIENTSERVER
9110 "<client>"
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009111# define SPEC_CLIENT (SPEC_SFLNUM + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009112#endif
9113 };
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009114
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00009115 for (i = 0; i < (int)(sizeof(spec_str) / sizeof(char *)); ++i)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009116 {
9117 len = (int)STRLEN(spec_str[i]);
9118 if (STRNCMP(src, spec_str[i], len) == 0)
9119 {
9120 *usedlen = len;
9121 return i;
9122 }
9123 }
9124 return -1;
9125}
9126
9127/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009128 * Evaluate cmdline variables.
9129 *
9130 * change '%' to curbuf->b_ffname
9131 * '#' to curwin->w_altfile
9132 * '<cword>' to word under the cursor
9133 * '<cWORD>' to WORD under the cursor
9134 * '<cfile>' to path name under the cursor
9135 * '<sfile>' to sourced file name
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009136 * '<slnum>' to sourced file line number
Bram Moolenaar071d4272004-06-13 20:20:40 +00009137 * '<afile>' to file name for autocommand
9138 * '<abuf>' to buffer number for autocommand
9139 * '<amatch>' to matching name for autocommand
9140 *
9141 * When an error is detected, "errormsg" is set to a non-NULL pointer (may be
9142 * "" for error without a message) and NULL is returned.
9143 * Returns an allocated string if a valid match was found.
9144 * Returns NULL if no match was found. "usedlen" then still contains the
9145 * number of characters to skip.
9146 */
9147 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009148eval_vars(
9149 char_u *src, /* pointer into commandline */
9150 char_u *srcstart, /* beginning of valid memory for src */
9151 int *usedlen, /* characters after src that are used */
9152 linenr_T *lnump, /* line number for :e command, or NULL */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009153 char **errormsg, /* pointer to error message */
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009154 int *escaped) /* return value has escaped white space (can
Bram Moolenaar63b92542007-03-27 14:57:09 +00009155 * be NULL) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009156{
9157 int i;
9158 char_u *s;
9159 char_u *result;
9160 char_u *resultbuf = NULL;
9161 int resultlen;
9162 buf_T *buf;
9163 int valid = VALID_HEAD + VALID_PATH; /* assume valid result */
9164 int spec_idx;
9165#ifdef FEAT_MODIFY_FNAME
Bram Moolenaarfd249462018-07-28 16:14:30 +02009166 int tilde_file = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009167 int skip_mod = FALSE;
9168#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009169 char_u strbuf[30];
Bram Moolenaar071d4272004-06-13 20:20:40 +00009170
9171 *errormsg = NULL;
Bram Moolenaar63b92542007-03-27 14:57:09 +00009172 if (escaped != NULL)
9173 *escaped = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009174
9175 /*
9176 * Check if there is something to do.
9177 */
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009178 spec_idx = find_cmdline_var(src, usedlen);
9179 if (spec_idx < 0) /* no match */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009180 {
9181 *usedlen = 1;
9182 return NULL;
9183 }
9184
9185 /*
9186 * Skip when preceded with a backslash "\%" and "\#".
9187 * Note: In "\\%" the % is also not recognized!
9188 */
9189 if (src > srcstart && src[-1] == '\\')
9190 {
9191 *usedlen = 0;
Bram Moolenaara7241f52008-06-24 20:39:31 +00009192 STRMOVE(src - 1, src); /* remove backslash */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009193 return NULL;
9194 }
9195
9196 /*
9197 * word or WORD under cursor
9198 */
Bram Moolenaar65f08472017-09-10 18:16:20 +02009199 if (spec_idx == SPEC_CWORD || spec_idx == SPEC_CCWORD
9200 || spec_idx == SPEC_CEXPR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009201 {
Bram Moolenaar65f08472017-09-10 18:16:20 +02009202 resultlen = find_ident_under_cursor(&result,
9203 spec_idx == SPEC_CWORD ? (FIND_IDENT | FIND_STRING)
9204 : spec_idx == SPEC_CEXPR ? (FIND_IDENT | FIND_STRING | FIND_EVAL)
9205 : FIND_STRING);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009206 if (resultlen == 0)
9207 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009208 *errormsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +00009209 return NULL;
9210 }
9211 }
9212
9213 /*
9214 * '#': Alternate file name
9215 * '%': Current file name
9216 * File name under the cursor
9217 * File name for autocommand
9218 * and following modifiers
9219 */
9220 else
9221 {
9222 switch (spec_idx)
9223 {
9224 case SPEC_PERC: /* '%': current file */
9225 if (curbuf->b_fname == NULL)
9226 {
9227 result = (char_u *)"";
9228 valid = 0; /* Must have ":p:h" to be valid */
9229 }
9230 else
Bram Moolenaar00136dc2018-07-25 21:19:13 +02009231 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009232 result = curbuf->b_fname;
Bram Moolenaarfd249462018-07-28 16:14:30 +02009233#ifdef FEAT_MODIFY_FNAME
Bram Moolenaar00136dc2018-07-25 21:19:13 +02009234 tilde_file = STRCMP(result, "~") == 0;
Bram Moolenaarfd249462018-07-28 16:14:30 +02009235#endif
Bram Moolenaar00136dc2018-07-25 21:19:13 +02009236 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009237 break;
9238
9239 case SPEC_HASH: /* '#' or "#99": alternate file */
9240 if (src[1] == '#') /* "##": the argument list */
9241 {
9242 result = arg_all();
9243 resultbuf = result;
9244 *usedlen = 2;
Bram Moolenaar63b92542007-03-27 14:57:09 +00009245 if (escaped != NULL)
9246 *escaped = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009247#ifdef FEAT_MODIFY_FNAME
9248 skip_mod = TRUE;
9249#endif
9250 break;
9251 }
9252 s = src + 1;
Bram Moolenaard812df62008-11-09 12:46:09 +00009253 if (*s == '<') /* "#<99" uses v:oldfiles */
9254 ++s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009255 i = (int)getdigits(&s);
Bram Moolenaarc312b8b2017-10-28 17:53:04 +02009256 if (s == src + 2 && src[1] == '-')
9257 /* just a minus sign, don't skip over it */
9258 s--;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009259 *usedlen = (int)(s - src); /* length of what we expand */
9260
Bram Moolenaarc312b8b2017-10-28 17:53:04 +02009261 if (src[1] == '<' && i != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009262 {
Bram Moolenaard812df62008-11-09 12:46:09 +00009263 if (*usedlen < 2)
9264 {
9265 /* Should we give an error message for #<text? */
9266 *usedlen = 1;
9267 return NULL;
9268 }
9269#ifdef FEAT_EVAL
9270 result = list_find_str(get_vim_var_list(VV_OLDFILES),
9271 (long)i);
9272 if (result == NULL)
9273 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009274 *errormsg = "";
Bram Moolenaard812df62008-11-09 12:46:09 +00009275 return NULL;
9276 }
9277#else
Bram Moolenaar8e481e82019-01-15 20:07:48 +01009278 *errormsg = _("E809: #< is not available without the +eval feature");
Bram Moolenaar071d4272004-06-13 20:20:40 +00009279 return NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +00009280#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009281 }
9282 else
Bram Moolenaard812df62008-11-09 12:46:09 +00009283 {
Bram Moolenaarc312b8b2017-10-28 17:53:04 +02009284 if (i == 0 && src[1] == '<' && *usedlen > 1)
9285 *usedlen = 1;
Bram Moolenaard812df62008-11-09 12:46:09 +00009286 buf = buflist_findnr(i);
9287 if (buf == NULL)
9288 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009289 *errormsg = _("E194: No alternate file name to substitute for '#'");
Bram Moolenaard812df62008-11-09 12:46:09 +00009290 return NULL;
9291 }
9292 if (lnump != NULL)
9293 *lnump = ECMD_LAST;
9294 if (buf->b_fname == NULL)
9295 {
9296 result = (char_u *)"";
9297 valid = 0; /* Must have ":p:h" to be valid */
9298 }
9299 else
Bram Moolenaar00136dc2018-07-25 21:19:13 +02009300 {
Bram Moolenaard812df62008-11-09 12:46:09 +00009301 result = buf->b_fname;
Bram Moolenaarfd249462018-07-28 16:14:30 +02009302#ifdef FEAT_MODIFY_FNAME
Bram Moolenaar00136dc2018-07-25 21:19:13 +02009303 tilde_file = STRCMP(result, "~") == 0;
Bram Moolenaarfd249462018-07-28 16:14:30 +02009304#endif
Bram Moolenaar00136dc2018-07-25 21:19:13 +02009305 }
Bram Moolenaard812df62008-11-09 12:46:09 +00009306 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009307 break;
9308
9309#ifdef FEAT_SEARCHPATH
9310 case SPEC_CFILE: /* file name under cursor */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00009311 result = file_name_at_cursor(FNAME_MESS|FNAME_HYP, 1L, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009312 if (result == NULL)
9313 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009314 *errormsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +00009315 return NULL;
9316 }
9317 resultbuf = result; /* remember allocated string */
9318 break;
9319#endif
9320
Bram Moolenaar071d4272004-06-13 20:20:40 +00009321 case SPEC_AFILE: /* file name for autocommand */
9322 result = autocmd_fname;
Bram Moolenaarf6dad432008-09-18 19:29:58 +00009323 if (result != NULL && !autocmd_fname_full)
9324 {
9325 /* Still need to turn the fname into a full path. It is
9326 * postponed to avoid a delay when <afile> is not used. */
9327 autocmd_fname_full = TRUE;
9328 result = FullName_save(autocmd_fname, FALSE);
9329 vim_free(autocmd_fname);
9330 autocmd_fname = result;
9331 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009332 if (result == NULL)
9333 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009334 *errormsg = _("E495: no autocommand file name to substitute for \"<afile>\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00009335 return NULL;
9336 }
Bram Moolenaara0174af2008-01-02 20:08:25 +00009337 result = shorten_fname1(result);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009338 break;
9339
9340 case SPEC_ABUF: /* buffer number for autocommand */
9341 if (autocmd_bufnr <= 0)
9342 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009343 *errormsg = _("E496: no autocommand buffer number to substitute for \"<abuf>\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00009344 return NULL;
9345 }
9346 sprintf((char *)strbuf, "%d", autocmd_bufnr);
9347 result = strbuf;
9348 break;
9349
9350 case SPEC_AMATCH: /* match name for autocommand */
9351 result = autocmd_match;
9352 if (result == NULL)
9353 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009354 *errormsg = _("E497: no autocommand match name to substitute for \"<amatch>\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00009355 return NULL;
9356 }
9357 break;
9358
Bram Moolenaar071d4272004-06-13 20:20:40 +00009359 case SPEC_SFILE: /* file name for ":so" command */
9360 result = sourcing_name;
9361 if (result == NULL)
9362 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009363 *errormsg = _("E498: no :source file name to substitute for \"<sfile>\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00009364 return NULL;
9365 }
9366 break;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009367
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009368 case SPEC_SLNUM: /* line in file for ":so" command */
9369 if (sourcing_name == NULL || sourcing_lnum == 0)
9370 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009371 *errormsg = _("E842: no line number to use for \"<slnum>\"");
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009372 return NULL;
9373 }
9374 sprintf((char *)strbuf, "%ld", (long)sourcing_lnum);
9375 result = strbuf;
9376 break;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009377
9378#ifdef FEAT_EVAL
9379 case SPEC_SFLNUM: /* line in script file */
9380 if (current_sctx.sc_lnum + sourcing_lnum == 0)
9381 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009382 *errormsg = _("E961: no line number to use for \"<sflnum>\"");
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009383 return NULL;
9384 }
9385 sprintf((char *)strbuf, "%ld",
9386 (long)(current_sctx.sc_lnum + sourcing_lnum));
9387 result = strbuf;
9388 break;
9389#endif
9390
9391#ifdef FEAT_CLIENTSERVER
Bram Moolenaar071d4272004-06-13 20:20:40 +00009392 case SPEC_CLIENT: /* Source of last submitted input */
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009393 sprintf((char *)strbuf, PRINTF_HEX_LONG_U,
9394 (long_u)clientWindow);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009395 result = strbuf;
9396 break;
9397#endif
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009398
Bram Moolenaar9e0f6ec2017-05-16 09:36:54 +02009399 default:
9400 result = (char_u *)""; /* avoid gcc warning */
9401 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009402 }
9403
9404 resultlen = (int)STRLEN(result); /* length of new string */
9405 if (src[*usedlen] == '<') /* remove the file name extension */
9406 {
9407 ++*usedlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009408 if ((s = vim_strrchr(result, '.')) != NULL && s >= gettail(result))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009409 resultlen = (int)(s - result);
9410 }
9411#ifdef FEAT_MODIFY_FNAME
9412 else if (!skip_mod)
9413 {
Bram Moolenaar00136dc2018-07-25 21:19:13 +02009414 valid |= modify_fname(src, tilde_file, usedlen, &result, &resultbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009415 &resultlen);
9416 if (result == NULL)
9417 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009418 *errormsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +00009419 return NULL;
9420 }
9421 }
9422#endif
9423 }
9424
9425 if (resultlen == 0 || valid != VALID_HEAD + VALID_PATH)
9426 {
9427 if (valid != VALID_HEAD + VALID_PATH)
9428 /* xgettext:no-c-format */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009429 *errormsg = _("E499: Empty file name for '%' or '#', only works with \":p:h\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00009430 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009431 *errormsg = _("E500: Evaluates to an empty string");
Bram Moolenaar071d4272004-06-13 20:20:40 +00009432 result = NULL;
9433 }
9434 else
9435 result = vim_strnsave(result, resultlen);
9436 vim_free(resultbuf);
9437 return result;
9438}
9439
9440/*
9441 * Concatenate all files in the argument list, separated by spaces, and return
9442 * it in one allocated string.
9443 * Spaces and backslashes in the file names are escaped with a backslash.
9444 * Returns NULL when out of memory.
9445 */
9446 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009447arg_all(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009448{
9449 int len;
9450 int idx;
9451 char_u *retval = NULL;
9452 char_u *p;
9453
9454 /*
9455 * Do this loop two times:
9456 * first time: compute the total length
9457 * second time: concatenate the names
9458 */
9459 for (;;)
9460 {
9461 len = 0;
9462 for (idx = 0; idx < ARGCOUNT; ++idx)
9463 {
9464 p = alist_name(&ARGLIST[idx]);
9465 if (p != NULL)
9466 {
9467 if (len > 0)
9468 {
9469 /* insert a space in between names */
9470 if (retval != NULL)
9471 retval[len] = ' ';
9472 ++len;
9473 }
9474 for ( ; *p != NUL; ++p)
9475 {
Bram Moolenaar6e8d3b02015-06-09 21:33:31 +02009476 if (*p == ' '
9477#ifndef BACKSLASH_IN_FILENAME
9478 || *p == '\\'
9479#endif
Bram Moolenaar2c8c6812018-07-28 17:07:52 +02009480 || *p == '`')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009481 {
9482 /* insert a backslash */
9483 if (retval != NULL)
9484 retval[len] = '\\';
9485 ++len;
9486 }
9487 if (retval != NULL)
9488 retval[len] = *p;
9489 ++len;
9490 }
9491 }
9492 }
9493
9494 /* second time: break here */
9495 if (retval != NULL)
9496 {
9497 retval[len] = NUL;
9498 break;
9499 }
9500
9501 /* allocate memory */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00009502 retval = alloc((unsigned)len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009503 if (retval == NULL)
9504 break;
9505 }
9506
9507 return retval;
9508}
9509
Bram Moolenaar071d4272004-06-13 20:20:40 +00009510/*
9511 * Expand the <sfile> string in "arg".
9512 *
9513 * Returns an allocated string, or NULL for any error.
9514 */
9515 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009516expand_sfile(char_u *arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009517{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009518 char *errormsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009519 int len;
9520 char_u *result;
9521 char_u *newres;
9522 char_u *repl;
9523 int srclen;
9524 char_u *p;
9525
9526 result = vim_strsave(arg);
9527 if (result == NULL)
9528 return NULL;
9529
9530 for (p = result; *p; )
9531 {
9532 if (STRNCMP(p, "<sfile>", 7) != 0)
9533 ++p;
9534 else
9535 {
9536 /* replace "<sfile>" with the sourced file name, and do ":" stuff */
Bram Moolenaar63b92542007-03-27 14:57:09 +00009537 repl = eval_vars(p, result, &srclen, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009538 if (errormsg != NULL)
9539 {
9540 if (*errormsg)
9541 emsg(errormsg);
9542 vim_free(result);
9543 return NULL;
9544 }
9545 if (repl == NULL) /* no match (cannot happen) */
9546 {
9547 p += srclen;
9548 continue;
9549 }
9550 len = (int)STRLEN(result) - srclen + (int)STRLEN(repl) + 1;
9551 newres = alloc(len);
9552 if (newres == NULL)
9553 {
9554 vim_free(repl);
9555 vim_free(result);
9556 return NULL;
9557 }
9558 mch_memmove(newres, result, (size_t)(p - result));
9559 STRCPY(newres + (p - result), repl);
9560 len = (int)STRLEN(newres);
9561 STRCAT(newres, p + srclen);
9562 vim_free(repl);
9563 vim_free(result);
9564 result = newres;
9565 p = newres + len; /* continue after the match */
9566 }
9567 }
9568
9569 return result;
9570}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009571
9572#ifdef FEAT_SESSION
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01009573static int ses_winsizes(FILE *fd, int restore_size,
9574 win_T *tab_firstwin);
9575static int ses_win_rec(FILE *fd, frame_T *fr);
9576static frame_T *ses_skipframe(frame_T *fr);
9577static int ses_do_frame(frame_T *fr);
9578static int ses_do_win(win_T *wp);
9579static int ses_arglist(FILE *fd, char *cmd, garray_T *gap, int fullname, unsigned *flagp);
9580static int ses_put_fname(FILE *fd, char_u *name, unsigned *flagp);
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +02009581static int ses_fname(FILE *fd, buf_T *buf, unsigned *flagp, int add_eol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009582
9583/*
9584 * Write openfile commands for the current buffers to an .exrc file.
9585 * Return FAIL on error, OK otherwise.
9586 */
9587 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009588makeopens(
9589 FILE *fd,
9590 char_u *dirnow) /* Current directory name */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009591{
9592 buf_T *buf;
9593 int only_save_windows = TRUE;
9594 int nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009595 int restore_size = TRUE;
9596 win_T *wp;
9597 char_u *sname;
9598 win_T *edited_win = NULL;
Bram Moolenaar18144c82006-04-12 21:52:12 +00009599 int tabnr;
Bram Moolenaar04ad7fe2014-05-07 21:14:47 +02009600 int restore_stal = FALSE;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +00009601 win_T *tab_firstwin;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +00009602 frame_T *tab_topframe;
Bram Moolenaarf13be0d2008-01-02 14:13:10 +00009603 int cur_arg_idx = 0;
Bram Moolenaar383c6f52008-01-04 15:01:07 +00009604 int next_arg_idx = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009605
9606 if (ssop_flags & SSOP_BUFFERS)
9607 only_save_windows = FALSE; /* Save ALL buffers */
9608
9609 /*
9610 * Begin by setting the this_session variable, and then other
9611 * sessionable variables.
9612 */
9613#ifdef FEAT_EVAL
9614 if (put_line(fd, "let v:this_session=expand(\"<sfile>:p\")") == FAIL)
9615 return FAIL;
9616 if (ssop_flags & SSOP_GLOBALS)
9617 if (store_session_globals(fd) == FAIL)
9618 return FAIL;
9619#endif
9620
9621 /*
Bram Moolenaar26d4b892018-07-04 22:26:28 +02009622 * Close all windows and tabs but one.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009623 */
9624 if (put_line(fd, "silent only") == FAIL)
9625 return FAIL;
Bram Moolenaar26d4b892018-07-04 22:26:28 +02009626 if ((ssop_flags & SSOP_TABPAGES)
9627 && put_line(fd, "silent tabonly") == FAIL)
9628 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009629
9630 /*
9631 * Now a :cd command to the session directory or the current directory
9632 */
9633 if (ssop_flags & SSOP_SESDIR)
9634 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009635 if (put_line(fd, "exe \"cd \" . escape(expand(\"<sfile>:p:h\"), ' ')")
9636 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009637 return FAIL;
9638 }
9639 else if (ssop_flags & SSOP_CURDIR)
9640 {
9641 sname = home_replace_save(NULL, globaldir != NULL ? globaldir : dirnow);
9642 if (sname == NULL
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009643 || fputs("cd ", fd) < 0
9644 || ses_put_fname(fd, sname, &ssop_flags) == FAIL
9645 || put_eol(fd) == FAIL)
9646 {
9647 vim_free(sname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009648 return FAIL;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009649 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009650 vim_free(sname);
9651 }
9652
9653 /*
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009654 * If there is an empty, unnamed buffer we will wipe it out later.
9655 * Remember the buffer number.
9656 */
9657 if (put_line(fd, "if expand('%') == '' && !&modified && line('$') <= 1 && getline(1) == ''") == FAIL)
9658 return FAIL;
9659 if (put_line(fd, " let s:wipebuf = bufnr('%')") == FAIL)
9660 return FAIL;
9661 if (put_line(fd, "endif") == FAIL)
9662 return FAIL;
9663
9664 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009665 * Now save the current files, current buffer first.
9666 */
9667 if (put_line(fd, "set shortmess=aoO") == FAIL)
9668 return FAIL;
9669
Bram Moolenaar071d4272004-06-13 20:20:40 +00009670 /* the global argument list */
Bram Moolenaarf0bdd2f2014-03-12 21:28:26 +01009671 if (ses_arglist(fd, "argglobal", &global_alist.al_ga,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009672 !(ssop_flags & SSOP_CURDIR), &ssop_flags) == FAIL)
9673 return FAIL;
9674
9675 if (ssop_flags & SSOP_RESIZE)
9676 {
9677 /* Note: after the restore we still check it worked!*/
9678 if (fprintf(fd, "set lines=%ld columns=%ld" , Rows, Columns) < 0
9679 || put_eol(fd) == FAIL)
9680 return FAIL;
9681 }
9682
9683#ifdef FEAT_GUI
9684 if (gui.in_use && (ssop_flags & SSOP_WINPOS))
9685 {
9686 int x, y;
9687
9688 if (gui_mch_get_winpos(&x, &y) == OK)
9689 {
9690 /* Note: after the restore we still check it worked!*/
9691 if (fprintf(fd, "winpos %d %d", x, y) < 0 || put_eol(fd) == FAIL)
9692 return FAIL;
9693 }
9694 }
9695#endif
9696
9697 /*
Bram Moolenaar04ad7fe2014-05-07 21:14:47 +02009698 * When there are two or more tabpages and 'showtabline' is 1 the tabline
9699 * will be displayed when creating the next tab. That resizes the windows
9700 * in the first tab, which may cause problems. Set 'showtabline' to 2
9701 * temporarily to avoid that.
9702 */
9703 if (p_stal == 1 && first_tabpage->tp_next != NULL)
9704 {
9705 if (put_line(fd, "set stal=2") == FAIL)
9706 return FAIL;
9707 restore_stal = TRUE;
9708 }
9709
9710 /*
Bram Moolenaar18144c82006-04-12 21:52:12 +00009711 * May repeat putting Windows for each tab, when "tabpages" is in
9712 * 'sessionoptions'.
Bram Moolenaarcba2ae52006-10-24 10:59:57 +00009713 * Don't use goto_tabpage(), it may change directory and trigger
9714 * autocommands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009715 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +00009716 tab_firstwin = firstwin; /* first window in tab page "tabnr" */
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +00009717 tab_topframe = topframe;
Bram Moolenaar26d4b892018-07-04 22:26:28 +02009718 if ((ssop_flags & SSOP_TABPAGES))
9719 {
Bram Moolenaar57a6bf02019-01-22 21:27:13 +01009720 tabpage_T *tp;
Bram Moolenaar26d4b892018-07-04 22:26:28 +02009721
Bram Moolenaar57a6bf02019-01-22 21:27:13 +01009722 // Similar to ses_win_rec() below, populate the tab pages first so
9723 // later local options won't be copied to the new tabs.
9724 FOR_ALL_TABPAGES(tp)
9725 if (tp->tp_next != NULL && put_line(fd, "tabnew") == FAIL)
Bram Moolenaar26d4b892018-07-04 22:26:28 +02009726 return FAIL;
Bram Moolenaar57a6bf02019-01-22 21:27:13 +01009727 if (first_tabpage->tp_next != NULL && put_line(fd, "tabrewind") == FAIL)
Bram Moolenaar26d4b892018-07-04 22:26:28 +02009728 return FAIL;
9729 }
Bram Moolenaar18144c82006-04-12 21:52:12 +00009730 for (tabnr = 1; ; ++tabnr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009731 {
Bram Moolenaar26d4b892018-07-04 22:26:28 +02009732 int need_tabnext = FALSE;
Bram Moolenaar695baee2015-04-13 12:39:22 +02009733 int cnr = 1;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +00009734
Bram Moolenaar18144c82006-04-12 21:52:12 +00009735 if ((ssop_flags & SSOP_TABPAGES))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009736 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +00009737 tabpage_T *tp = find_tabpage(tabnr);
9738
9739 if (tp == NULL)
9740 break; /* done all tab pages */
9741 if (tp == curtab)
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +00009742 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +00009743 tab_firstwin = firstwin;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +00009744 tab_topframe = topframe;
9745 }
Bram Moolenaarcba2ae52006-10-24 10:59:57 +00009746 else
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +00009747 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +00009748 tab_firstwin = tp->tp_firstwin;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +00009749 tab_topframe = tp->tp_topframe;
9750 }
Bram Moolenaarcba2ae52006-10-24 10:59:57 +00009751 if (tabnr > 1)
Bram Moolenaar26d4b892018-07-04 22:26:28 +02009752 need_tabnext = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009753 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009754
Bram Moolenaar18144c82006-04-12 21:52:12 +00009755 /*
9756 * Before creating the window layout, try loading one file. If this
9757 * is aborted we don't end up with a number of useless windows.
9758 * This may have side effects! (e.g., compressed or network file).
9759 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +00009760 for (wp = tab_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar18144c82006-04-12 21:52:12 +00009761 {
9762 if (ses_do_win(wp)
9763 && wp->w_buffer->b_ffname != NULL
Bram Moolenaar67883b42017-07-27 22:57:00 +02009764 && !bt_help(wp->w_buffer)
Bram Moolenaar18144c82006-04-12 21:52:12 +00009765#ifdef FEAT_QUICKFIX
9766 && !bt_nofile(wp->w_buffer)
9767#endif
9768 )
9769 {
Bram Moolenaar26d4b892018-07-04 22:26:28 +02009770 if (need_tabnext && put_line(fd, "tabnext") == FAIL)
9771 return FAIL;
9772 need_tabnext = FALSE;
9773
9774 if (fputs("edit ", fd) < 0
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +02009775 || ses_fname(fd, wp->w_buffer, &ssop_flags, TRUE)
9776 == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +00009777 return FAIL;
9778 if (!wp->w_arg_idx_invalid)
9779 edited_win = wp;
9780 break;
9781 }
9782 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009783
Bram Moolenaarcba2ae52006-10-24 10:59:57 +00009784 /* If no file got edited create an empty tab page. */
Bram Moolenaar26d4b892018-07-04 22:26:28 +02009785 if (need_tabnext && put_line(fd, "tabnext") == FAIL)
Bram Moolenaarcba2ae52006-10-24 10:59:57 +00009786 return FAIL;
9787
Bram Moolenaar18144c82006-04-12 21:52:12 +00009788 /*
9789 * Save current window layout.
9790 */
9791 if (put_line(fd, "set splitbelow splitright") == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009792 return FAIL;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +00009793 if (ses_win_rec(fd, tab_topframe) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009794 return FAIL;
Bram Moolenaar18144c82006-04-12 21:52:12 +00009795 if (!p_sb && put_line(fd, "set nosplitbelow") == FAIL)
9796 return FAIL;
9797 if (!p_spr && put_line(fd, "set nosplitright") == FAIL)
9798 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009799
Bram Moolenaar18144c82006-04-12 21:52:12 +00009800 /*
9801 * Check if window sizes can be restored (no windows omitted).
9802 * Remember the window number of the current window after restoring.
9803 */
9804 nr = 0;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +00009805 for (wp = tab_firstwin; wp != NULL; wp = W_NEXT(wp))
Bram Moolenaar18144c82006-04-12 21:52:12 +00009806 {
9807 if (ses_do_win(wp))
9808 ++nr;
9809 else
9810 restore_size = FALSE;
9811 if (curwin == wp)
9812 cnr = nr;
9813 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009814
Bram Moolenaar18144c82006-04-12 21:52:12 +00009815 /* Go to the first window. */
9816 if (put_line(fd, "wincmd t") == FAIL)
9817 return FAIL;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009818
Bram Moolenaar18144c82006-04-12 21:52:12 +00009819 /*
9820 * If more than one window, see if sizes can be restored.
9821 * First set 'winheight' and 'winwidth' to 1 to avoid the windows being
9822 * resized when moving between windows.
9823 * Do this before restoring the view, so that the topline and the
9824 * cursor can be set. This is done again below.
Bram Moolenaar36ae89c2017-01-28 17:11:14 +01009825 * winminheight and winminwidth need to be set to avoid an error if the
9826 * user has set winheight or winwidth.
Bram Moolenaar18144c82006-04-12 21:52:12 +00009827 */
Bram Moolenaar19834012018-06-12 17:03:39 +02009828 if (put_line(fd, "set winminheight=0") == FAIL
9829 || put_line(fd, "set winheight=1") == FAIL
9830 || put_line(fd, "set winminwidth=0") == FAIL
9831 || put_line(fd, "set winwidth=1") == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +00009832 return FAIL;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +00009833 if (nr > 1 && ses_winsizes(fd, restore_size, tab_firstwin) == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +00009834 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009835
Bram Moolenaar18144c82006-04-12 21:52:12 +00009836 /*
9837 * Restore the view of the window (options, file, cursor, etc.).
9838 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +00009839 for (wp = tab_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar18144c82006-04-12 21:52:12 +00009840 {
9841 if (!ses_do_win(wp))
9842 continue;
Bram Moolenaarf13be0d2008-01-02 14:13:10 +00009843 if (put_view(fd, wp, wp != edited_win, &ssop_flags,
9844 cur_arg_idx) == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +00009845 return FAIL;
9846 if (nr > 1 && put_line(fd, "wincmd w") == FAIL)
9847 return FAIL;
Bram Moolenaarf13be0d2008-01-02 14:13:10 +00009848 next_arg_idx = wp->w_arg_idx;
Bram Moolenaar18144c82006-04-12 21:52:12 +00009849 }
9850
Bram Moolenaarf13be0d2008-01-02 14:13:10 +00009851 /* The argument index in the first tab page is zero, need to set it in
9852 * each window. For further tab pages it's the window where we do
9853 * "tabedit". */
9854 cur_arg_idx = next_arg_idx;
9855
Bram Moolenaar18144c82006-04-12 21:52:12 +00009856 /*
9857 * Restore cursor to the current window if it's not the first one.
9858 */
9859 if (cnr > 1 && (fprintf(fd, "%dwincmd w", cnr) < 0
9860 || put_eol(fd) == FAIL))
9861 return FAIL;
9862
9863 /*
Bram Moolenaar18144c82006-04-12 21:52:12 +00009864 * Restore window sizes again after jumping around in windows, because
9865 * the current window has a minimum size while others may not.
9866 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +00009867 if (nr > 1 && ses_winsizes(fd, restore_size, tab_firstwin) == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +00009868 return FAIL;
9869
Bram Moolenaar18144c82006-04-12 21:52:12 +00009870 /* Don't continue in another tab page when doing only the current one
9871 * or when at the last tab page. */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +00009872 if (!(ssop_flags & SSOP_TABPAGES))
Bram Moolenaar18144c82006-04-12 21:52:12 +00009873 break;
9874 }
9875
9876 if (ssop_flags & SSOP_TABPAGES)
9877 {
Bram Moolenaar18144c82006-04-12 21:52:12 +00009878 if (fprintf(fd, "tabnext %d", tabpage_index(curtab)) < 0
9879 || put_eol(fd) == FAIL)
9880 return FAIL;
9881 }
Bram Moolenaar04ad7fe2014-05-07 21:14:47 +02009882 if (restore_stal && put_line(fd, "set stal=1") == FAIL)
9883 return FAIL;
Bram Moolenaar18144c82006-04-12 21:52:12 +00009884
Bram Moolenaard39e2752019-01-26 20:07:38 +01009885 // Now put the remaining buffers into the buffer list.
9886 // This is near the end, so that when 'hidden' is set we don't create extra
9887 // buffers. If the buffer was already created with another command the
9888 // ":badd" will have no effect.
9889 FOR_ALL_BUFFERS(buf)
9890 {
9891 if (!(only_save_windows && buf->b_nwindows == 0)
9892 && !(buf->b_help && !(ssop_flags & SSOP_HELP))
9893#ifdef FEAT_TERMINAL
9894 // Skip terminal buffers: finished ones are not useful, others
9895 // will be resurrected and result in a new buffer.
9896 && !bt_terminal(buf)
9897#endif
9898 && buf->b_fname != NULL
9899 && buf->b_p_bl)
9900 {
9901 if (fprintf(fd, "badd +%ld ", buf->b_wininfo == NULL ? 1L
9902 : buf->b_wininfo->wi_fpos.lnum) < 0
9903 || ses_fname(fd, buf, &ssop_flags, TRUE) == FAIL)
9904 return FAIL;
9905 }
9906 }
9907
Bram Moolenaar9c102382006-05-03 21:26:49 +00009908 /*
9909 * Wipe out an empty unnamed buffer we started in.
9910 */
Bram Moolenaar26d4b892018-07-04 22:26:28 +02009911 if (put_line(fd, "if exists('s:wipebuf') && len(win_findbuf(s:wipebuf)) == 0")
Bram Moolenaar4d8bac82018-03-09 21:33:34 +01009912 == FAIL)
Bram Moolenaar9c102382006-05-03 21:26:49 +00009913 return FAIL;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00009914 if (put_line(fd, " silent exe 'bwipe ' . s:wipebuf") == FAIL)
Bram Moolenaar9c102382006-05-03 21:26:49 +00009915 return FAIL;
9916 if (put_line(fd, "endif") == FAIL)
9917 return FAIL;
9918 if (put_line(fd, "unlet! s:wipebuf") == FAIL)
9919 return FAIL;
9920
9921 /* Re-apply 'winheight', 'winwidth' and 'shortmess'. */
9922 if (fprintf(fd, "set winheight=%ld winwidth=%ld shortmess=%s",
9923 p_wh, p_wiw, p_shm) < 0 || put_eol(fd) == FAIL)
9924 return FAIL;
Bram Moolenaar36ae89c2017-01-28 17:11:14 +01009925 /* Re-apply 'winminheight' and 'winminwidth'. */
9926 if (fprintf(fd, "set winminheight=%ld winminwidth=%ld",
9927 p_wmh, p_wmw) < 0 || put_eol(fd) == FAIL)
9928 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009929
9930 /*
9931 * Lastly, execute the x.vim file if it exists.
9932 */
9933 if (put_line(fd, "let s:sx = expand(\"<sfile>:p:r\").\"x.vim\"") == FAIL
9934 || put_line(fd, "if file_readable(s:sx)") == FAIL
Bram Moolenaar42ba1262008-12-09 10:18:03 +00009935 || put_line(fd, " exe \"source \" . fnameescape(s:sx)") == FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +00009936 || put_line(fd, "endif") == FAIL)
9937 return FAIL;
9938
9939 return OK;
9940}
9941
9942 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009943ses_winsizes(
9944 FILE *fd,
9945 int restore_size,
9946 win_T *tab_firstwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009947{
9948 int n = 0;
9949 win_T *wp;
9950
9951 if (restore_size && (ssop_flags & SSOP_WINSIZE))
9952 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +00009953 for (wp = tab_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009954 {
9955 if (!ses_do_win(wp))
9956 continue;
9957 ++n;
9958
9959 /* restore height when not full height */
9960 if (wp->w_height + wp->w_status_height < topframe->fr_height
9961 && (fprintf(fd,
9962 "exe '%dresize ' . ((&lines * %ld + %ld) / %ld)",
9963 n, (long)wp->w_height, Rows / 2, Rows) < 0
9964 || put_eol(fd) == FAIL))
9965 return FAIL;
9966
9967 /* restore width when not full width */
9968 if (wp->w_width < Columns && (fprintf(fd,
9969 "exe 'vert %dresize ' . ((&columns * %ld + %ld) / %ld)",
9970 n, (long)wp->w_width, Columns / 2, Columns) < 0
9971 || put_eol(fd) == FAIL))
9972 return FAIL;
9973 }
9974 }
9975 else
9976 {
9977 /* Just equalise window sizes */
9978 if (put_line(fd, "wincmd =") == FAIL)
9979 return FAIL;
9980 }
9981 return OK;
9982}
9983
9984/*
9985 * Write commands to "fd" to recursively create windows for frame "fr",
9986 * horizontally and vertically split.
9987 * After the commands the last window in the frame is the current window.
9988 * Returns FAIL when writing the commands to "fd" fails.
9989 */
9990 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009991ses_win_rec(FILE *fd, frame_T *fr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009992{
9993 frame_T *frc;
9994 int count = 0;
9995
9996 if (fr->fr_layout != FR_LEAF)
9997 {
9998 /* Find first frame that's not skipped and then create a window for
9999 * each following one (first frame is already there). */
10000 frc = ses_skipframe(fr->fr_child);
10001 if (frc != NULL)
10002 while ((frc = ses_skipframe(frc->fr_next)) != NULL)
10003 {
10004 /* Make window as big as possible so that we have lots of room
10005 * to split. */
10006 if (put_line(fd, "wincmd _ | wincmd |") == FAIL
10007 || put_line(fd, fr->fr_layout == FR_COL
10008 ? "split" : "vsplit") == FAIL)
10009 return FAIL;
10010 ++count;
10011 }
10012
10013 /* Go back to the first window. */
10014 if (count > 0 && (fprintf(fd, fr->fr_layout == FR_COL
10015 ? "%dwincmd k" : "%dwincmd h", count) < 0
10016 || put_eol(fd) == FAIL))
10017 return FAIL;
10018
10019 /* Recursively create frames/windows in each window of this column or
10020 * row. */
10021 frc = ses_skipframe(fr->fr_child);
10022 while (frc != NULL)
10023 {
10024 ses_win_rec(fd, frc);
10025 frc = ses_skipframe(frc->fr_next);
10026 /* Go to next window. */
10027 if (frc != NULL && put_line(fd, "wincmd w") == FAIL)
10028 return FAIL;
10029 }
10030 }
10031 return OK;
10032}
10033
10034/*
10035 * Skip frames that don't contain windows we want to save in the Session.
10036 * Returns NULL when there none.
10037 */
10038 static frame_T *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010039ses_skipframe(frame_T *fr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010040{
10041 frame_T *frc;
10042
Bram Moolenaar3d1491e2018-12-22 17:07:50 +010010043 FOR_ALL_FRAMES(frc, fr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010044 if (ses_do_frame(frc))
10045 break;
10046 return frc;
10047}
10048
10049/*
10050 * Return TRUE if frame "fr" has a window somewhere that we want to save in
10051 * the Session.
10052 */
10053 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010054ses_do_frame(frame_T *fr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010055{
10056 frame_T *frc;
10057
10058 if (fr->fr_layout == FR_LEAF)
10059 return ses_do_win(fr->fr_win);
Bram Moolenaar3d1491e2018-12-22 17:07:50 +010010060 FOR_ALL_FRAMES(frc, fr->fr_child)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010061 if (ses_do_frame(frc))
10062 return TRUE;
10063 return FALSE;
10064}
10065
10066/*
10067 * Return non-zero if window "wp" is to be stored in the Session.
10068 */
10069 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010070ses_do_win(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010071{
Bram Moolenaar4d8bac82018-03-09 21:33:34 +010010072#ifdef FEAT_TERMINAL
10073 if (bt_terminal(wp->w_buffer))
10074 return !term_is_finished(wp->w_buffer)
10075 && (ssop_flags & SSOP_TERMINAL)
10076 && term_should_restore(wp->w_buffer);
10077#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010078 if (wp->w_buffer->b_fname == NULL
10079#ifdef FEAT_QUICKFIX
10080 /* When 'buftype' is "nofile" can't restore the window contents. */
10081 || bt_nofile(wp->w_buffer)
10082#endif
10083 )
10084 return (ssop_flags & SSOP_BLANK);
Bram Moolenaar67883b42017-07-27 22:57:00 +020010085 if (bt_help(wp->w_buffer))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010086 return (ssop_flags & SSOP_HELP);
10087 return TRUE;
10088}
10089
Bram Moolenaar92c1b692018-08-29 21:42:42 +020010090 static int
10091put_view_curpos(FILE *fd, win_T *wp, char *spaces)
10092{
10093 int r;
10094
10095 if (wp->w_curswant == MAXCOL)
10096 r = fprintf(fd, "%snormal! $", spaces);
10097 else
10098 r = fprintf(fd, "%snormal! 0%d|", spaces, wp->w_virtcol + 1);
10099 return r < 0 || put_eol(fd) == FAIL ? FALSE : OK;
10100}
10101
Bram Moolenaar071d4272004-06-13 20:20:40 +000010102/*
10103 * Write commands to "fd" to restore the view of a window.
10104 * Caller must make sure 'scrolloff' is zero.
10105 */
10106 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010107put_view(
10108 FILE *fd,
10109 win_T *wp,
10110 int add_edit, /* add ":edit" command to view */
10111 unsigned *flagp, /* vop_flags or ssop_flags */
10112 int current_arg_idx) /* current argument index of the window, use
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010113 * -1 if unknown */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010114{
10115 win_T *save_curwin;
10116 int f;
10117 int do_cursor;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000010118 int did_next = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010119
10120 /* Always restore cursor position for ":mksession". For ":mkview" only
10121 * when 'viewoptions' contains "cursor". */
10122 do_cursor = (flagp == &ssop_flags || *flagp & SSOP_CURSOR);
10123
10124 /*
10125 * Local argument list.
10126 */
10127 if (wp->w_alist == &global_alist)
10128 {
10129 if (put_line(fd, "argglobal") == FAIL)
10130 return FAIL;
10131 }
10132 else
10133 {
10134 if (ses_arglist(fd, "arglocal", &wp->w_alist->al_ga,
10135 flagp == &vop_flags
10136 || !(*flagp & SSOP_CURDIR)
10137 || wp->w_localdir != NULL, flagp) == FAIL)
10138 return FAIL;
10139 }
10140
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000010141 /* Only when part of a session: restore the argument index. Some
10142 * arguments may have been deleted, check if the index is valid. */
Bram Moolenaar51f53df2010-06-26 05:25:54 +020010143 if (wp->w_arg_idx != current_arg_idx && wp->w_arg_idx < WARGCOUNT(wp)
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000010144 && flagp == &ssop_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010145 {
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010146 if (fprintf(fd, "%ldargu", (long)wp->w_arg_idx + 1) < 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000010147 || put_eol(fd) == FAIL)
10148 return FAIL;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000010149 did_next = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010150 }
10151
10152 /* Edit the file. Skip this when ":next" already did it. */
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000010153 if (add_edit && (!did_next || wp->w_arg_idx_invalid))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010154 {
Bram Moolenaar4d8bac82018-03-09 21:33:34 +010010155# ifdef FEAT_TERMINAL
10156 if (bt_terminal(wp->w_buffer))
10157 {
10158 if (term_write_session(fd, wp) == FAIL)
10159 return FAIL;
10160 }
10161 else
10162# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010163 /*
10164 * Load the file.
10165 */
10166 if (wp->w_buffer->b_ffname != NULL
Bram Moolenaar4d8bac82018-03-09 21:33:34 +010010167# ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +000010168 && !bt_nofile(wp->w_buffer)
Bram Moolenaar4d8bac82018-03-09 21:33:34 +010010169# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010170 )
10171 {
10172 /*
10173 * Editing a file in this buffer: use ":edit file".
10174 * This may have side effects! (e.g., compressed or network file).
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +020010175 *
10176 * Note, if a buffer for that file already exists, use :badd to
10177 * edit that buffer, to not lose folding information (:edit resets
10178 * folds in other buffers)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010179 */
Bram Moolenaarad36a352019-01-24 13:34:42 +010010180 if (fputs("if bufexists(\"", fd) < 0
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +020010181 || ses_fname(fd, wp->w_buffer, flagp, FALSE) == FAIL
Bram Moolenaarad36a352019-01-24 13:34:42 +010010182 || fputs("\") | buffer ", fd) < 0
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +020010183 || ses_fname(fd, wp->w_buffer, flagp, FALSE) == FAIL
10184 || fputs(" | else | edit ", fd) < 0
10185 || ses_fname(fd, wp->w_buffer, flagp, FALSE) == FAIL
10186 || fputs(" | endif", fd) < 0
Bram Moolenaar4d8bac82018-03-09 21:33:34 +010010187 || put_eol(fd) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010188 return FAIL;
10189 }
10190 else
10191 {
10192 /* No file in this buffer, just make it empty. */
10193 if (put_line(fd, "enew") == FAIL)
10194 return FAIL;
10195#ifdef FEAT_QUICKFIX
10196 if (wp->w_buffer->b_ffname != NULL)
10197 {
10198 /* The buffer does have a name, but it's not a file name. */
10199 if (fputs("file ", fd) < 0
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +020010200 || ses_fname(fd, wp->w_buffer, flagp, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010201 return FAIL;
10202 }
10203#endif
10204 do_cursor = FALSE;
10205 }
10206 }
10207
10208 /*
10209 * Local mappings and abbreviations.
10210 */
10211 if ((*flagp & (SSOP_OPTIONS | SSOP_LOCALOPTIONS))
10212 && makemap(fd, wp->w_buffer) == FAIL)
10213 return FAIL;
10214
10215 /*
10216 * Local options. Need to go to the window temporarily.
10217 * Store only local values when using ":mkview" and when ":mksession" is
10218 * used and 'sessionoptions' doesn't include "options".
10219 * Some folding options are always stored when "folds" is included,
10220 * otherwise the folds would not be restored correctly.
10221 */
10222 save_curwin = curwin;
10223 curwin = wp;
10224 curbuf = curwin->w_buffer;
10225 if (*flagp & (SSOP_OPTIONS | SSOP_LOCALOPTIONS))
10226 f = makeset(fd, OPT_LOCAL,
10227 flagp == &vop_flags || !(*flagp & SSOP_OPTIONS));
10228#ifdef FEAT_FOLDING
10229 else if (*flagp & SSOP_FOLDS)
10230 f = makefoldset(fd);
10231#endif
10232 else
10233 f = OK;
10234 curwin = save_curwin;
10235 curbuf = curwin->w_buffer;
10236 if (f == FAIL)
10237 return FAIL;
10238
10239#ifdef FEAT_FOLDING
10240 /*
10241 * Save Folds when 'buftype' is empty and for help files.
10242 */
10243 if ((*flagp & SSOP_FOLDS)
10244 && wp->w_buffer->b_ffname != NULL
Bram Moolenaar91335e52018-08-01 17:53:12 +020010245 && (bt_normal(wp->w_buffer) || bt_help(wp->w_buffer)))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010246 {
10247 if (put_folds(fd, wp) == FAIL)
10248 return FAIL;
10249 }
10250#endif
10251
10252 /*
10253 * Set the cursor after creating folds, since that moves the cursor.
10254 */
10255 if (do_cursor)
10256 {
10257
10258 /* Restore the cursor line in the file and relatively in the
10259 * window. Don't use "G", it changes the jumplist. */
10260 if (fprintf(fd, "let s:l = %ld - ((%ld * winheight(0) + %ld) / %ld)",
10261 (long)wp->w_cursor.lnum,
10262 (long)(wp->w_cursor.lnum - wp->w_topline),
10263 (long)wp->w_height / 2, (long)wp->w_height) < 0
10264 || put_eol(fd) == FAIL
10265 || put_line(fd, "if s:l < 1 | let s:l = 1 | endif") == FAIL
10266 || put_line(fd, "exe s:l") == FAIL
10267 || put_line(fd, "normal! zt") == FAIL
10268 || fprintf(fd, "%ld", (long)wp->w_cursor.lnum) < 0
10269 || put_eol(fd) == FAIL)
10270 return FAIL;
10271 /* Restore the cursor column and left offset when not wrapping. */
10272 if (wp->w_cursor.col == 0)
10273 {
10274 if (put_line(fd, "normal! 0") == FAIL)
10275 return FAIL;
10276 }
10277 else
10278 {
10279 if (!wp->w_p_wrap && wp->w_leftcol > 0 && wp->w_width > 0)
10280 {
10281 if (fprintf(fd,
10282 "let s:c = %ld - ((%ld * winwidth(0) + %ld) / %ld)",
Bram Moolenaar558ddad2013-02-20 19:26:29 +010010283 (long)wp->w_virtcol + 1,
10284 (long)(wp->w_virtcol - wp->w_leftcol),
Bram Moolenaar071d4272004-06-13 20:20:40 +000010285 (long)wp->w_width / 2, (long)wp->w_width) < 0
10286 || put_eol(fd) == FAIL
10287 || put_line(fd, "if s:c > 0") == FAIL
10288 || fprintf(fd,
Bram Moolenaar558ddad2013-02-20 19:26:29 +010010289 " exe 'normal! ' . s:c . '|zs' . %ld . '|'",
10290 (long)wp->w_virtcol + 1) < 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000010291 || put_eol(fd) == FAIL
10292 || put_line(fd, "else") == FAIL
Bram Moolenaar92c1b692018-08-29 21:42:42 +020010293 || put_view_curpos(fd, wp, " ") == FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +000010294 || put_line(fd, "endif") == FAIL)
10295 return FAIL;
10296 }
Bram Moolenaar92c1b692018-08-29 21:42:42 +020010297 else if (put_view_curpos(fd, wp, "") == FAIL)
10298 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010299 }
10300 }
10301
10302 /*
Bram Moolenaar13e90412017-11-11 18:16:48 +010010303 * Local directory, if the current flag is not view options or the "curdir"
10304 * option is included.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010305 */
Bram Moolenaar13e90412017-11-11 18:16:48 +010010306 if (wp->w_localdir != NULL
10307 && (flagp != &vop_flags || (*flagp & SSOP_CURDIR)))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010308 {
10309 if (fputs("lcd ", fd) < 0
10310 || ses_put_fname(fd, wp->w_localdir, flagp) == FAIL
10311 || put_eol(fd) == FAIL)
10312 return FAIL;
Bram Moolenaareeefcc72007-05-01 21:21:21 +000010313 did_lcd = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010314 }
10315
10316 return OK;
10317}
10318
10319/*
10320 * Write an argument list to the session file.
10321 * Returns FAIL if writing fails.
10322 */
10323 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010324ses_arglist(
10325 FILE *fd,
10326 char *cmd,
10327 garray_T *gap,
10328 int fullname, /* TRUE: use full path name */
10329 unsigned *flagp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010330{
10331 int i;
Bram Moolenaard9462e32011-04-11 21:35:11 +020010332 char_u *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010333 char_u *s;
10334
Bram Moolenaarf0bdd2f2014-03-12 21:28:26 +010010335 if (fputs(cmd, fd) < 0 || put_eol(fd) == FAIL)
10336 return FAIL;
Bram Moolenaar555de4e2019-01-21 23:03:49 +010010337 if (put_line(fd, "%argdel") == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010338 return FAIL;
10339 for (i = 0; i < gap->ga_len; ++i)
10340 {
10341 /* NULL file names are skipped (only happens when out of memory). */
10342 s = alist_name(&((aentry_T *)gap->ga_data)[i]);
10343 if (s != NULL)
10344 {
10345 if (fullname)
10346 {
Bram Moolenaard9462e32011-04-11 21:35:11 +020010347 buf = alloc(MAXPATHL);
10348 if (buf != NULL)
10349 {
10350 (void)vim_FullName(s, buf, MAXPATHL, FALSE);
10351 s = buf;
10352 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010353 }
Bram Moolenaar79da5632017-02-01 22:52:44 +010010354 if (fputs("$argadd ", fd) < 0
Bram Moolenaarf0bdd2f2014-03-12 21:28:26 +010010355 || ses_put_fname(fd, s, flagp) == FAIL
10356 || put_eol(fd) == FAIL)
Bram Moolenaard9462e32011-04-11 21:35:11 +020010357 {
10358 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010359 return FAIL;
Bram Moolenaard9462e32011-04-11 21:35:11 +020010360 }
10361 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010362 }
10363 }
Bram Moolenaarf0bdd2f2014-03-12 21:28:26 +010010364 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010365}
10366
10367/*
10368 * Write a buffer name to the session file.
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +020010369 * Also ends the line, if "add_eol" is TRUE.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010370 * Returns FAIL if writing fails.
10371 */
10372 static int
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +020010373ses_fname(FILE *fd, buf_T *buf, unsigned *flagp, int add_eol)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010374{
10375 char_u *name;
10376
10377 /* Use the short file name if the current directory is known at the time
Bram Moolenaareeefcc72007-05-01 21:21:21 +000010378 * the session file will be sourced.
10379 * Don't do this for ":mkview", we don't know the current directory.
10380 * Don't do this after ":lcd", we don't keep track of what the current
10381 * directory is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010382 if (buf->b_sfname != NULL
10383 && flagp == &ssop_flags
Bram Moolenaareeefcc72007-05-01 21:21:21 +000010384 && (ssop_flags & (SSOP_CURDIR | SSOP_SESDIR))
Bram Moolenaar8d594672009-07-01 18:18:57 +000010385#ifdef FEAT_AUTOCHDIR
10386 && !p_acd
10387#endif
Bram Moolenaareeefcc72007-05-01 21:21:21 +000010388 && !did_lcd)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010389 name = buf->b_sfname;
10390 else
10391 name = buf->b_ffname;
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +020010392 if (ses_put_fname(fd, name, flagp) == FAIL
10393 || (add_eol && put_eol(fd) == FAIL))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010394 return FAIL;
10395 return OK;
10396}
10397
10398/*
10399 * Write a file name to the session file.
10400 * Takes care of the "slash" option in 'sessionoptions' and escapes special
10401 * characters.
Bram Moolenaar78f74a92010-10-13 17:50:07 +020010402 * Returns FAIL if writing fails or out of memory.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010403 */
10404 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010405ses_put_fname(FILE *fd, char_u *name, unsigned *flagp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010406{
10407 char_u *sname;
Bram Moolenaar78f74a92010-10-13 17:50:07 +020010408 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010409 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010410
10411 sname = home_replace_save(NULL, name);
Bram Moolenaar78f74a92010-10-13 17:50:07 +020010412 if (sname == NULL)
10413 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010414
Bram Moolenaar78f74a92010-10-13 17:50:07 +020010415 if (*flagp & SSOP_SLASH)
10416 {
10417 /* change all backslashes to forward slashes */
Bram Moolenaar91acfff2017-03-12 19:22:36 +010010418 for (p = sname; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar78f74a92010-10-13 17:50:07 +020010419 if (*p == '\\')
10420 *p = '/';
Bram Moolenaar071d4272004-06-13 20:20:40 +000010421 }
Bram Moolenaar78f74a92010-10-13 17:50:07 +020010422
Bram Moolenaar84a05ac2013-05-06 04:24:17 +020010423 /* escape special characters */
Bram Moolenaar78f74a92010-10-13 17:50:07 +020010424 p = vim_strsave_fnameescape(sname, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010425 vim_free(sname);
Bram Moolenaar78f74a92010-10-13 17:50:07 +020010426 if (p == NULL)
10427 return FAIL;
10428
10429 /* write the result */
10430 if (fputs((char *)p, fd) < 0)
10431 retval = FAIL;
10432
10433 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010434 return retval;
10435}
10436
10437/*
10438 * ":loadview [nr]"
10439 */
10440 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010441ex_loadview(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010442{
10443 char_u *fname;
10444
10445 fname = get_view_file(*eap->arg);
10446 if (fname != NULL)
10447 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +000010448 do_source(fname, FALSE, DOSO_NONE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010449 vim_free(fname);
10450 }
10451}
10452
10453/*
10454 * Get the name of the view file for the current buffer.
10455 */
10456 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010457get_view_file(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010458{
10459 int len = 0;
10460 char_u *p, *s;
10461 char_u *retval;
10462 char_u *sname;
10463
10464 if (curbuf->b_ffname == NULL)
10465 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010466 emsg(_(e_noname));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010467 return NULL;
10468 }
10469 sname = home_replace_save(NULL, curbuf->b_ffname);
10470 if (sname == NULL)
10471 return NULL;
10472
10473 /*
10474 * We want a file name without separators, because we're not going to make
10475 * a directory.
10476 * "normal" path separator -> "=+"
10477 * "=" -> "=="
10478 * ":" path separator -> "=-"
10479 */
10480 for (p = sname; *p; ++p)
10481 if (*p == '=' || vim_ispathsep(*p))
10482 ++len;
10483 retval = alloc((unsigned)(STRLEN(sname) + len + STRLEN(p_vdir) + 9));
10484 if (retval != NULL)
10485 {
10486 STRCPY(retval, p_vdir);
10487 add_pathsep(retval);
10488 s = retval + STRLEN(retval);
10489 for (p = sname; *p; ++p)
10490 {
10491 if (*p == '=')
10492 {
10493 *s++ = '=';
10494 *s++ = '=';
10495 }
10496 else if (vim_ispathsep(*p))
10497 {
10498 *s++ = '=';
Bram Moolenaare60acc12011-05-10 16:41:25 +020010499#if defined(BACKSLASH_IN_FILENAME) || defined(AMIGA) || defined(VMS)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010500 if (*p == ':')
10501 *s++ = '-';
10502 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010503#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +000010504 *s++ = '+';
Bram Moolenaar071d4272004-06-13 20:20:40 +000010505 }
10506 else
10507 *s++ = *p;
10508 }
10509 *s++ = '=';
10510 *s++ = c;
10511 STRCPY(s, ".vim");
10512 }
10513
10514 vim_free(sname);
10515 return retval;
10516}
10517
10518#endif /* FEAT_SESSION */
10519
10520/*
10521 * Write end-of-line character(s) for ":mkexrc", ":mkvimrc" and ":mksession".
10522 * Return FAIL for a write error.
10523 */
10524 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010525put_eol(FILE *fd)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010526{
10527 if (
10528#ifdef USE_CRNL
10529 (
10530# ifdef MKSESSION_NL
10531 !mksession_nl &&
10532# endif
10533 (putc('\r', fd) < 0)) ||
10534#endif
10535 (putc('\n', fd) < 0))
10536 return FAIL;
10537 return OK;
10538}
10539
10540/*
10541 * Write a line to "fd".
10542 * Return FAIL for a write error.
10543 */
10544 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010545put_line(FILE *fd, char *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010546{
10547 if (fputs(s, fd) < 0 || put_eol(fd) == FAIL)
10548 return FAIL;
10549 return OK;
10550}
10551
10552#ifdef FEAT_VIMINFO
10553/*
10554 * ":rviminfo" and ":wviminfo".
10555 */
10556 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010557ex_viminfo(
10558 exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010559{
10560 char_u *save_viminfo;
10561
10562 save_viminfo = p_viminfo;
10563 if (*p_viminfo == NUL)
10564 p_viminfo = (char_u *)"'100";
10565 if (eap->cmdidx == CMD_rviminfo)
10566 {
Bram Moolenaard812df62008-11-09 12:46:09 +000010567 if (read_viminfo(eap->arg, VIF_WANT_INFO | VIF_WANT_MARKS
10568 | (eap->forceit ? VIF_FORCEIT : 0)) == FAIL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010569 emsg(_("E195: Cannot open viminfo file for reading"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010570 }
10571 else
10572 write_viminfo(eap->arg, eap->forceit);
10573 p_viminfo = save_viminfo;
10574}
10575#endif
10576
10577#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG) || defined(PROTO)
Bram Moolenaar9c13b352005-05-19 20:53:52 +000010578/*
Bram Moolenaard9462e32011-04-11 21:35:11 +020010579 * Make a dialog message in "buff[DIALOG_MSG_SIZE]".
Bram Moolenaarb765d632005-06-07 21:00:02 +000010580 * "format" must contain "%s".
Bram Moolenaar9c13b352005-05-19 20:53:52 +000010581 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010582 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010583dialog_msg(char_u *buff, char *format, char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010584{
Bram Moolenaar071d4272004-06-13 20:20:40 +000010585 if (fname == NULL)
10586 fname = (char_u *)_("Untitled");
Bram Moolenaard9462e32011-04-11 21:35:11 +020010587 vim_snprintf((char *)buff, DIALOG_MSG_SIZE, format, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010588}
10589#endif
10590
10591/*
10592 * ":behave {mswin,xterm}"
10593 */
10594 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010595ex_behave(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010596{
10597 if (STRCMP(eap->arg, "mswin") == 0)
10598 {
10599 set_option_value((char_u *)"selection", 0L, (char_u *)"exclusive", 0);
10600 set_option_value((char_u *)"selectmode", 0L, (char_u *)"mouse,key", 0);
10601 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"popup", 0);
10602 set_option_value((char_u *)"keymodel", 0L,
10603 (char_u *)"startsel,stopsel", 0);
10604 }
10605 else if (STRCMP(eap->arg, "xterm") == 0)
10606 {
10607 set_option_value((char_u *)"selection", 0L, (char_u *)"inclusive", 0);
10608 set_option_value((char_u *)"selectmode", 0L, (char_u *)"", 0);
10609 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"extend", 0);
10610 set_option_value((char_u *)"keymodel", 0L, (char_u *)"", 0);
10611 }
10612 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010613 semsg(_(e_invarg2), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010614}
10615
Bram Moolenaar42b4dda2010-03-02 15:56:05 +010010616#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
10617/*
10618 * Function given to ExpandGeneric() to obtain the possible arguments of the
10619 * ":behave {mswin,xterm}" command.
10620 */
10621 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010622get_behave_arg(expand_T *xp UNUSED, int idx)
Bram Moolenaar42b4dda2010-03-02 15:56:05 +010010623{
10624 if (idx == 0)
10625 return (char_u *)"mswin";
10626 if (idx == 1)
10627 return (char_u *)"xterm";
10628 return NULL;
10629}
Bram Moolenaar9e507ca2016-10-15 15:39:39 +020010630
10631/*
10632 * Function given to ExpandGeneric() to obtain the possible arguments of the
10633 * ":messages {clear}" command.
10634 */
10635 char_u *
10636get_messages_arg(expand_T *xp UNUSED, int idx)
10637{
10638 if (idx == 0)
10639 return (char_u *)"clear";
10640 return NULL;
10641}
Bram Moolenaar42b4dda2010-03-02 15:56:05 +010010642#endif
10643
Bram Moolenaar113e1072019-01-20 15:30:40 +010010644#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
Bram Moolenaarcae92dc2017-08-06 15:22:15 +020010645 char_u *
10646get_mapclear_arg(expand_T *xp UNUSED, int idx)
10647{
10648 if (idx == 0)
10649 return (char_u *)"<buffer>";
10650 return NULL;
10651}
Bram Moolenaar113e1072019-01-20 15:30:40 +010010652#endif
Bram Moolenaarcae92dc2017-08-06 15:22:15 +020010653
Bram Moolenaar071d4272004-06-13 20:20:40 +000010654static int filetype_detect = FALSE;
10655static int filetype_plugin = FALSE;
10656static int filetype_indent = FALSE;
10657
10658/*
10659 * ":filetype [plugin] [indent] {on,off,detect}"
10660 * on: Load the filetype.vim file to install autocommands for file types.
10661 * off: Load the ftoff.vim file to remove all autocommands for file types.
10662 * plugin on: load filetype.vim and ftplugin.vim
10663 * plugin off: load ftplugof.vim
10664 * indent on: load filetype.vim and indent.vim
10665 * indent off: load indoff.vim
10666 */
10667 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010668ex_filetype(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010669{
10670 char_u *arg = eap->arg;
10671 int plugin = FALSE;
10672 int indent = FALSE;
10673
10674 if (*eap->arg == NUL)
10675 {
10676 /* Print current status. */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010677 smsg("filetype detection:%s plugin:%s indent:%s",
Bram Moolenaar071d4272004-06-13 20:20:40 +000010678 filetype_detect ? "ON" : "OFF",
10679 filetype_plugin ? (filetype_detect ? "ON" : "(on)") : "OFF",
10680 filetype_indent ? (filetype_detect ? "ON" : "(on)") : "OFF");
10681 return;
10682 }
10683
10684 /* Accept "plugin" and "indent" in any order. */
10685 for (;;)
10686 {
10687 if (STRNCMP(arg, "plugin", 6) == 0)
10688 {
10689 plugin = TRUE;
10690 arg = skipwhite(arg + 6);
10691 continue;
10692 }
10693 if (STRNCMP(arg, "indent", 6) == 0)
10694 {
10695 indent = TRUE;
10696 arg = skipwhite(arg + 6);
10697 continue;
10698 }
10699 break;
10700 }
10701 if (STRCMP(arg, "on") == 0 || STRCMP(arg, "detect") == 0)
10702 {
10703 if (*arg == 'o' || !filetype_detect)
10704 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010010705 source_runtime((char_u *)FILETYPE_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010706 filetype_detect = TRUE;
10707 if (plugin)
10708 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010010709 source_runtime((char_u *)FTPLUGIN_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010710 filetype_plugin = TRUE;
10711 }
10712 if (indent)
10713 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010010714 source_runtime((char_u *)INDENT_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010715 filetype_indent = TRUE;
10716 }
10717 }
10718 if (*arg == 'd')
10719 {
Bram Moolenaar1610d052016-06-09 22:53:01 +020010720 (void)do_doautocmd((char_u *)"filetypedetect BufRead", TRUE, NULL);
Bram Moolenaara3227e22006-03-08 21:32:40 +000010721 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010722 }
10723 }
10724 else if (STRCMP(arg, "off") == 0)
10725 {
10726 if (plugin || indent)
10727 {
10728 if (plugin)
10729 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010010730 source_runtime((char_u *)FTPLUGOF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010731 filetype_plugin = FALSE;
10732 }
10733 if (indent)
10734 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010010735 source_runtime((char_u *)INDOFF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010736 filetype_indent = FALSE;
10737 }
10738 }
10739 else
10740 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010010741 source_runtime((char_u *)FTOFF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010742 filetype_detect = FALSE;
10743 }
10744 }
10745 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010746 semsg(_(e_invarg2), arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010747}
10748
10749/*
Bram Moolenaar3e545692017-06-04 19:00:32 +020010750 * ":setfiletype [FALLBACK] {name}"
Bram Moolenaar071d4272004-06-13 20:20:40 +000010751 */
10752 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010753ex_setfiletype(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010754{
10755 if (!did_filetype)
Bram Moolenaar3e545692017-06-04 19:00:32 +020010756 {
10757 char_u *arg = eap->arg;
10758
10759 if (STRNCMP(arg, "FALLBACK ", 9) == 0)
10760 arg += 9;
10761
10762 set_option_value((char_u *)"filetype", 0L, arg, OPT_LOCAL);
10763 if (arg != eap->arg)
10764 did_filetype = FALSE;
10765 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010766}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010767
Bram Moolenaar071d4272004-06-13 20:20:40 +000010768 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010769ex_digraphs(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010770{
10771#ifdef FEAT_DIGRAPHS
10772 if (*eap->arg != NUL)
10773 putdigraph(eap->arg);
10774 else
Bram Moolenaareae8ae12018-12-14 18:53:02 +010010775 listdigraphs(eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010776#else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010777 emsg(_("E196: No digraphs in this version"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010778#endif
10779}
10780
10781 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010782ex_set(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010783{
10784 int flags = 0;
10785
10786 if (eap->cmdidx == CMD_setlocal)
10787 flags = OPT_LOCAL;
10788 else if (eap->cmdidx == CMD_setglobal)
10789 flags = OPT_GLOBAL;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +010010790#if defined(FEAT_EVAL) && defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010791 if (cmdmod.browse && flags == 0)
10792 ex_options(eap);
10793 else
10794#endif
10795 (void)do_set(eap->arg, flags);
10796}
10797
Bram Moolenaar451fc7b2018-04-27 22:53:07 +020010798#if defined(FEAT_SEARCH_EXTRA) || defined(PROTO)
10799 void
10800set_no_hlsearch(int flag)
10801{
10802 no_hlsearch = flag;
10803# ifdef FEAT_EVAL
10804 set_vim_var_nr(VV_HLSEARCH, !no_hlsearch && p_hls);
10805# endif
10806}
10807
Bram Moolenaar071d4272004-06-13 20:20:40 +000010808/*
10809 * ":nohlsearch"
10810 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010811 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010812ex_nohlsearch(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010813{
Bram Moolenaar451fc7b2018-04-27 22:53:07 +020010814 set_no_hlsearch(TRUE);
Bram Moolenaarf71a3db2006-03-12 21:50:18 +000010815 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010816}
10817
10818/*
Bram Moolenaare1438bb2006-03-01 22:01:55 +000010819 * ":[N]match {group} {pattern}"
Bram Moolenaar071d4272004-06-13 20:20:40 +000010820 * Sets nextcmd to the start of the next command, if any. Also called when
10821 * skipping commands to find the next command.
10822 */
10823 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010824ex_match(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010825{
10826 char_u *p;
Bram Moolenaar52d36c82007-08-11 14:00:30 +000010827 char_u *g = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010828 char_u *end;
10829 int c;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000010830 int id;
Bram Moolenaare1438bb2006-03-01 22:01:55 +000010831
10832 if (eap->line2 <= 3)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000010833 id = eap->line2;
Bram Moolenaare1438bb2006-03-01 22:01:55 +000010834 else
10835 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010836 emsg(_(e_invcmd));
Bram Moolenaare1438bb2006-03-01 22:01:55 +000010837 return;
10838 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010839
10840 /* First clear any old pattern. */
10841 if (!eap->skip)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000010842 match_delete(curwin, id, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010843
10844 if (ends_excmd(*eap->arg))
10845 end = eap->arg;
10846 else if ((STRNICMP(eap->arg, "none", 4) == 0
Bram Moolenaar1c465442017-03-12 20:10:05 +010010847 && (VIM_ISWHITE(eap->arg[4]) || ends_excmd(eap->arg[4]))))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010848 end = eap->arg + 4;
10849 else
10850 {
10851 p = skiptowhite(eap->arg);
10852 if (!eap->skip)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000010853 g = vim_strnsave(eap->arg, (int)(p - eap->arg));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010854 p = skipwhite(p);
10855 if (*p == NUL)
10856 {
10857 /* There must be two arguments. */
Bram Moolenaar9a7d58e2015-11-24 17:23:56 +010010858 vim_free(g);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010859 semsg(_(e_invarg2), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010860 return;
10861 }
10862 end = skip_regexp(p + 1, *p, TRUE, NULL);
10863 if (!eap->skip)
10864 {
10865 if (*end != NUL && !ends_excmd(*skipwhite(end + 1)))
10866 {
Bram Moolenaar9a7d58e2015-11-24 17:23:56 +010010867 vim_free(g);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010868 eap->errmsg = e_trailing;
10869 return;
10870 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000010871 if (*end != *p)
10872 {
Bram Moolenaar9a7d58e2015-11-24 17:23:56 +010010873 vim_free(g);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010874 semsg(_(e_invarg2), p);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000010875 return;
10876 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010877
10878 c = *end;
10879 *end = NUL;
Bram Moolenaar6561d522015-07-21 15:48:27 +020010880 match_add(curwin, g, p + 1, 10, id, NULL, NULL);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000010881 vim_free(g);
Bram Moolenaar910f66f2006-04-05 20:41:53 +000010882 *end = c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010883 }
10884 }
10885 eap->nextcmd = find_nextcmd(end);
10886}
10887#endif
10888
10889#ifdef FEAT_CRYPT
10890/*
10891 * ":X": Get crypt key
10892 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010893 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010894ex_X(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010895{
Bram Moolenaar3a0c9082014-11-12 15:15:42 +010010896 crypt_check_current_method();
Bram Moolenaar8f4ac012014-08-10 13:38:34 +020010897 (void)crypt_get_key(TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010898}
10899#endif
10900
10901#ifdef FEAT_FOLDING
10902 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010903ex_fold(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010904{
10905 if (foldManualAllowed(TRUE))
10906 foldCreate(eap->line1, eap->line2);
10907}
10908
10909 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010910ex_foldopen(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010911{
10912 opFoldRange(eap->line1, eap->line2, eap->cmdidx == CMD_foldopen,
10913 eap->forceit, FALSE);
10914}
10915
10916 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010917ex_folddo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010918{
10919 linenr_T lnum;
10920
Bram Moolenaar6b1ee342014-08-06 18:17:11 +020010921#ifdef FEAT_CLIPBOARD
10922 start_global_changes();
10923#endif
10924
Bram Moolenaar071d4272004-06-13 20:20:40 +000010925 /* First set the marks for all lines closed/open. */
10926 for (lnum = eap->line1; lnum <= eap->line2; ++lnum)
10927 if (hasFolding(lnum, NULL, NULL) == (eap->cmdidx == CMD_folddoclosed))
10928 ml_setmarked(lnum);
10929
10930 /* Execute the command on the marked lines. */
10931 global_exe(eap->arg);
10932 ml_clearmarked(); /* clear rest of the marks */
Bram Moolenaar6b1ee342014-08-06 18:17:11 +020010933#ifdef FEAT_CLIPBOARD
10934 end_global_changes();
10935#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010936}
10937#endif
Bram Moolenaarf5291f32017-09-14 22:55:37 +020010938
Bram Moolenaar39665952018-08-15 20:59:48 +020010939#ifdef FEAT_QUICKFIX
10940/*
10941 * Returns TRUE if the supplied Ex cmdidx is for a location list command
10942 * instead of a quickfix command.
10943 */
10944 int
10945is_loclist_cmd(int cmdidx)
10946{
Bram Moolenaar74c8be22018-08-23 22:51:40 +020010947 if (cmdidx < 0 || cmdidx >= CMD_SIZE)
Bram Moolenaar39665952018-08-15 20:59:48 +020010948 return FALSE;
10949 return cmdnames[cmdidx].cmd_name[0] == 'l';
10950}
10951#endif
10952
Bram Moolenaarf5291f32017-09-14 22:55:37 +020010953# if defined(FEAT_TIMERS) || defined(PROTO)
10954 int
10955get_pressedreturn(void)
10956{
10957 return ex_pressedreturn;
10958}
10959
10960 void
10961set_pressedreturn(int val)
10962{
10963 ex_pressedreturn = val;
10964}
10965#endif