blob: 67d06fc8e110b78e2458af68adef13c1e158e9b5 [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 Moolenaarb7316892019-05-01 18:08:42 +020076static linenr_T get_address(exarg_T *, char_u **, cmd_addr_T addr_type, int skip, int silent, int to_other_file, int address_count);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +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 Moolenaarb7316892019-05-01 18:08:42 +02001580/*
1581 * Return the window number of "win".
1582 * When "win" is NULL return the number of windows.
1583 */
Bram Moolenaarf240e182014-11-27 18:33:02 +01001584 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001585current_win_nr(win_T *win)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001586{
1587 win_T *wp;
1588 int nr = 0;
1589
Bram Moolenaar29323592016-07-24 22:04:11 +02001590 FOR_ALL_WINDOWS(wp)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001591 {
1592 ++nr;
1593 if (wp == win)
1594 break;
1595 }
1596 return nr;
1597}
1598
1599 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001600current_tab_nr(tabpage_T *tab)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001601{
1602 tabpage_T *tp;
1603 int nr = 0;
1604
Bram Moolenaar29323592016-07-24 22:04:11 +02001605 FOR_ALL_TABPAGES(tp)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001606 {
1607 ++nr;
1608 if (tp == tab)
1609 break;
1610 }
1611 return nr;
1612}
1613
1614# define CURRENT_WIN_NR current_win_nr(curwin)
1615# define LAST_WIN_NR current_win_nr(NULL)
1616# define CURRENT_TAB_NR current_tab_nr(curtab)
1617# define LAST_TAB_NR current_tab_nr(NULL)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001618
Bram Moolenaar071d4272004-06-13 20:20:40 +00001619/*
1620 * Execute one Ex command.
1621 *
1622 * If 'sourcing' is TRUE, the command will be included in the error message.
1623 *
1624 * 1. skip comment lines and leading space
1625 * 2. handle command modifiers
Bram Moolenaar1c40a662014-11-27 16:38:11 +01001626 * 3. find the command
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001627 * 4. parse range
Bram Moolenaar1c40a662014-11-27 16:38:11 +01001628 * 5. Parse the command.
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001629 * 6. parse arguments
1630 * 7. switch on command name
Bram Moolenaar071d4272004-06-13 20:20:40 +00001631 *
Bram Moolenaar89d40322006-08-29 15:30:07 +00001632 * Note: "fgetline" can be NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001633 *
1634 * This function may be called recursively!
1635 */
1636#if (_MSC_VER == 1200)
1637/*
Bram Moolenaared203462004-06-16 11:19:22 +00001638 * Avoid optimisation bug in VC++ version 6.0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001639 */
Bram Moolenaar281bdce2005-01-25 21:53:18 +00001640 #pragma optimize( "g", off )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001641#endif
1642 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001643do_one_cmd(
1644 char_u **cmdlinep,
1645 int sourcing,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001646#ifdef FEAT_EVAL
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001647 struct condstack *cstack,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001648#endif
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001649 char_u *(*fgetline)(int, void *, int),
1650 void *cookie) /* argument for fgetline() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001651{
1652 char_u *p;
1653 linenr_T lnum;
1654 long n;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001655 char *errormsg = NULL; /* error message */
Bram Moolenaar7b668e82016-08-23 23:51:21 +02001656 char_u *after_modifier = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001657 exarg_T ea; /* Ex command arguments */
Bram Moolenaar8e258a42009-07-09 13:55:43 +00001658 int save_msg_scroll = msg_scroll;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001659 cmdmod_T save_cmdmod;
Bram Moolenaar9a2c0912019-03-30 14:26:18 +01001660 int save_reg_executing = reg_executing;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001661 int ni; /* set when Not Implemented */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001662 char_u *cmd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001663
1664 vim_memset(&ea, 0, sizeof(ea));
1665 ea.line1 = 1;
1666 ea.line2 = 1;
1667#ifdef FEAT_EVAL
1668 ++ex_nesting_level;
1669#endif
1670
Bram Moolenaar21691f82012-12-05 19:13:18 +01001671 /* When the last file has not been edited :q has to be typed twice. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001672 if (quitmore
1673#ifdef FEAT_EVAL
1674 /* avoid that a function call in 'statusline' does this */
Bram Moolenaar89d40322006-08-29 15:30:07 +00001675 && !getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001676#endif
Bram Moolenaar21691f82012-12-05 19:13:18 +01001677 /* avoid that an autocommand, e.g. QuitPre, does this */
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001678 && !getline_equal(fgetline, cookie, getnextac))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001679 --quitmore;
1680
1681 /*
1682 * Reset browse, confirm, etc.. They are restored when returning, for
1683 * recursive calls.
1684 */
1685 save_cmdmod = cmdmod;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001686
Bram Moolenaarcbb37ad2006-08-16 15:04:21 +00001687 /* "#!anything" is handled like a comment. */
1688 if ((*cmdlinep)[0] == '#' && (*cmdlinep)[1] == '!')
1689 goto doend;
1690
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02001691/*
1692 * 1. Skip comment lines and leading white space and colons.
1693 * 2. Handle command modifiers.
1694 */
1695 // The "ea" structure holds the arguments that can be used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001696 ea.cmd = *cmdlinep;
Bram Moolenaareffed932018-08-14 13:38:17 +02001697 ea.cmdlinep = cmdlinep;
1698 ea.getline = fgetline;
1699 ea.cookie = cookie;
1700#ifdef FEAT_EVAL
1701 ea.cstack = cstack;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001702#endif
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02001703 if (parse_command_modifiers(&ea, &errormsg, FALSE) == FAIL)
Bram Moolenaareffed932018-08-14 13:38:17 +02001704 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001705
Bram Moolenaar7b668e82016-08-23 23:51:21 +02001706 after_modifier = ea.cmd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001707
1708#ifdef FEAT_EVAL
1709 ea.skip = did_emsg || got_int || did_throw || (cstack->cs_idx >= 0
1710 && !(cstack->cs_flags[cstack->cs_idx] & CSF_ACTIVE));
1711#else
1712 ea.skip = (if_level > 0);
1713#endif
1714
Bram Moolenaar071d4272004-06-13 20:20:40 +00001715/*
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001716 * 3. Skip over the range to find the command. Let "p" point to after it.
1717 *
1718 * We need the command to know what kind of range it uses.
1719 */
1720 cmd = ea.cmd;
1721 ea.cmd = skip_range(ea.cmd, NULL);
1722 if (*ea.cmd == '*' && vim_strchr(p_cpo, CPO_STAR) == NULL)
1723 ea.cmd = skipwhite(ea.cmd + 1);
1724 p = find_command(&ea, NULL);
1725
Bram Moolenaar7feb35e2018-08-21 17:49:54 +02001726#ifdef FEAT_EVAL
1727# ifdef FEAT_PROFILE
1728 // Count this line for profiling if skip is TRUE.
1729 if (do_profiling == PROF_YES
1730 && (!ea.skip || cstack->cs_idx == 0 || (cstack->cs_idx > 0
1731 && (cstack->cs_flags[cstack->cs_idx - 1] & CSF_ACTIVE))))
1732 {
1733 int skip = did_emsg || got_int || did_throw;
1734
1735 if (ea.cmdidx == CMD_catch)
1736 skip = !skip && !(cstack->cs_idx >= 0
1737 && (cstack->cs_flags[cstack->cs_idx] & CSF_THROWN)
1738 && !(cstack->cs_flags[cstack->cs_idx] & CSF_CAUGHT));
1739 else if (ea.cmdidx == CMD_else || ea.cmdidx == CMD_elseif)
1740 skip = skip || !(cstack->cs_idx >= 0
1741 && !(cstack->cs_flags[cstack->cs_idx]
1742 & (CSF_ACTIVE | CSF_TRUE)));
1743 else if (ea.cmdidx == CMD_finally)
1744 skip = FALSE;
1745 else if (ea.cmdidx != CMD_endif
1746 && ea.cmdidx != CMD_endfor
1747 && ea.cmdidx != CMD_endtry
1748 && ea.cmdidx != CMD_endwhile)
1749 skip = ea.skip;
1750
1751 if (!skip)
1752 {
1753 if (getline_equal(fgetline, cookie, get_func_line))
1754 func_line_exec(getline_cookie(fgetline, cookie));
1755 else if (getline_equal(fgetline, cookie, getsourceline))
1756 script_line_exec();
1757 }
1758 }
1759# endif
1760
1761 /* May go to debug mode. If this happens and the ">quit" debug command is
1762 * used, throw an interrupt exception and skip the next command. */
1763 dbg_check_breakpoint(&ea);
1764 if (!ea.skip && got_int)
1765 {
1766 ea.skip = TRUE;
1767 (void)do_intthrow(cstack);
1768 }
1769#endif
1770
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001771/*
1772 * 4. parse a range specifier of the form: addr [,addr] [;addr] ..
Bram Moolenaar071d4272004-06-13 20:20:40 +00001773 *
1774 * where 'addr' is:
1775 *
1776 * % (entire file)
1777 * $ [+-NUM]
1778 * 'x [+-NUM] (where x denotes a currently defined mark)
1779 * . [+-NUM]
1780 * [+-NUM]..
1781 * NUM
1782 *
1783 * The ea.cmd pointer is updated to point to the first character following the
1784 * range spec. If an initial address is found, but no second, the upper bound
1785 * is equal to the lower.
1786 */
1787
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01001788 /* ea.addr_type for user commands is set by find_ucmd */
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01001789 if (!IS_USER_CMDIDX(ea.cmdidx))
1790 {
1791 if (ea.cmdidx != CMD_SIZE)
1792 ea.addr_type = cmdnames[(int)ea.cmdidx].cmd_addr_type;
1793 else
1794 ea.addr_type = ADDR_LINES;
1795
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01001796 /* :wincmd range depends on the argument. */
Bram Moolenaar164f3262015-01-14 21:22:01 +01001797 if (ea.cmdidx == CMD_wincmd && p != NULL)
1798 get_wincmd_addr_type(skipwhite(p), &ea);
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01001799 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001800
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01001801 ea.cmd = cmd;
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02001802 if (parse_cmd_address(&ea, &errormsg, FALSE) == FAIL)
Bram Moolenaaree8415b2018-08-10 23:13:12 +02001803 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001804
Bram Moolenaar071d4272004-06-13 20:20:40 +00001805/*
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001806 * 5. Parse the command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001807 */
1808
1809 /*
1810 * Skip ':' and any white space
1811 */
1812 ea.cmd = skipwhite(ea.cmd);
1813 while (*ea.cmd == ':')
1814 ea.cmd = skipwhite(ea.cmd + 1);
1815
1816 /*
1817 * If we got a line, but no command, then go to the line.
1818 * If we find a '|' or '\n' we set ea.nextcmd.
1819 */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001820 if (*ea.cmd == NUL || *ea.cmd == '"'
1821 || (ea.nextcmd = check_nextcmd(ea.cmd)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001822 {
1823 /*
1824 * strange vi behaviour:
1825 * ":3" jumps to line 3
1826 * ":3|..." prints line 3
1827 * ":|" prints current line
1828 */
1829 if (ea.skip) /* skip this if inside :if */
1830 goto doend;
Bram Moolenaardf177f62005-02-22 08:39:57 +00001831 if (*ea.cmd == '|' || (exmode_active && ea.line1 != ea.line2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001832 {
1833 ea.cmdidx = CMD_print;
1834 ea.argt = RANGE+COUNT+TRLBAR;
1835 if ((errormsg = invalid_range(&ea)) == NULL)
1836 {
1837 correct_range(&ea);
1838 ex_print(&ea);
1839 }
1840 }
1841 else if (ea.addr_count != 0)
1842 {
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00001843 if (ea.line2 > curbuf->b_ml.ml_line_count)
1844 {
1845 /* With '-' in 'cpoptions' a line number past the file is an
1846 * error, otherwise put it at the end of the file. */
1847 if (vim_strchr(p_cpo, CPO_MINUS) != NULL)
1848 ea.line2 = -1;
1849 else
1850 ea.line2 = curbuf->b_ml.ml_line_count;
1851 }
1852
1853 if (ea.line2 < 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001854 errormsg = _(e_invrange);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001855 else
1856 {
1857 if (ea.line2 == 0)
1858 curwin->w_cursor.lnum = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001859 else
1860 curwin->w_cursor.lnum = ea.line2;
1861 beginline(BL_SOL | BL_FIX);
1862 }
1863 }
1864 goto doend;
1865 }
1866
Bram Moolenaard5005162014-08-22 23:05:54 +02001867 /* If this looks like an undefined user command and there are CmdUndefined
1868 * autocommands defined, trigger the matching autocommands. */
1869 if (p != NULL && ea.cmdidx == CMD_SIZE && !ea.skip
1870 && ASCII_ISUPPER(*ea.cmd)
1871 && has_cmdundefined())
1872 {
Bram Moolenaard5005162014-08-22 23:05:54 +02001873 int ret;
1874
Bram Moolenaar5a31b462014-08-23 14:16:20 +02001875 p = ea.cmd;
Bram Moolenaard5005162014-08-22 23:05:54 +02001876 while (ASCII_ISALNUM(*p))
1877 ++p;
Bram Moolenaar120f4a82014-09-09 12:22:06 +02001878 p = vim_strnsave(ea.cmd, (int)(p - ea.cmd));
Bram Moolenaard5005162014-08-22 23:05:54 +02001879 ret = apply_autocmds(EVENT_CMDUNDEFINED, p, p, TRUE, NULL);
1880 vim_free(p);
Bram Moolenaar829aef12015-07-28 14:25:48 +02001881 /* If the autocommands did something and didn't cause an error, try
1882 * finding the command again. */
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001883 p = (ret
1884#ifdef FEAT_EVAL
1885 && !aborting()
Bram Moolenaard5005162014-08-22 23:05:54 +02001886#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001887 ) ? find_command(&ea, NULL) : ea.cmd;
1888 }
Bram Moolenaard5005162014-08-22 23:05:54 +02001889
Bram Moolenaar071d4272004-06-13 20:20:40 +00001890 if (p == NULL)
1891 {
1892 if (!ea.skip)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001893 errormsg = _("E464: Ambiguous use of user-defined command");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001894 goto doend;
1895 }
Bram Moolenaarac9fb182019-04-27 13:04:13 +02001896 // Check for wrong commands.
Bram Moolenaar6f9a4762017-06-22 20:39:17 +02001897 if (*p == '!' && ea.cmd[1] == 0151 && ea.cmd[0] == 78
1898 && !IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001899 {
1900 errormsg = uc_fun_cmd();
1901 goto doend;
1902 }
Bram Moolenaarac9fb182019-04-27 13:04:13 +02001903
Bram Moolenaar071d4272004-06-13 20:20:40 +00001904 if (ea.cmdidx == CMD_SIZE)
1905 {
1906 if (!ea.skip)
1907 {
1908 STRCPY(IObuff, _("E492: Not an editor command"));
1909 if (!sourcing)
Bram Moolenaar7b668e82016-08-23 23:51:21 +02001910 {
1911 /* If the modifier was parsed OK the error must be in the
1912 * following command */
1913 if (after_modifier != NULL)
1914 append_command(after_modifier);
1915 else
1916 append_command(*cmdlinep);
1917 }
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001918 errormsg = (char *)IObuff;
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001919 did_emsg_syntax = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001920 }
1921 goto doend;
1922 }
1923
Bram Moolenaar958636c2014-10-21 20:01:58 +02001924 ni = (!IS_USER_CMDIDX(ea.cmdidx)
1925 && (cmdnames[ea.cmdidx].cmd_func == ex_ni
Bram Moolenaar7bb75552007-07-16 18:39:49 +00001926#ifdef HAVE_EX_SCRIPT_NI
1927 || cmdnames[ea.cmdidx].cmd_func == ex_script_ni
1928#endif
1929 ));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001930
1931#ifndef FEAT_EVAL
1932 /*
1933 * When the expression evaluation is disabled, recognize the ":if" and
1934 * ":endif" commands and ignore everything in between it.
1935 */
1936 if (ea.cmdidx == CMD_if)
1937 ++if_level;
1938 if (if_level)
1939 {
1940 if (ea.cmdidx == CMD_endif)
1941 --if_level;
1942 goto doend;
1943 }
1944
1945#endif
1946
Bram Moolenaar196b3b02008-06-20 16:51:41 +00001947 /* forced commands */
1948 if (*p == '!' && ea.cmdidx != CMD_substitute
1949 && ea.cmdidx != CMD_smagic && ea.cmdidx != CMD_snomagic)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001950 {
1951 ++p;
1952 ea.forceit = TRUE;
1953 }
1954 else
1955 ea.forceit = FALSE;
1956
1957/*
Bram Moolenaarb7316892019-05-01 18:08:42 +02001958 * 6. Parse arguments. Then check for errors.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001959 */
Bram Moolenaar958636c2014-10-21 20:01:58 +02001960 if (!IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001961 ea.argt = (long)cmdnames[(int)ea.cmdidx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001962
1963 if (!ea.skip)
1964 {
1965#ifdef HAVE_SANDBOX
1966 if (sandbox != 0 && !(ea.argt & SBOXOK))
1967 {
Bram Moolenaar8c62a082019-02-08 14:34:10 +01001968 // Command not allowed in sandbox.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001969 errormsg = _(e_sandbox);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001970 goto doend;
1971 }
1972#endif
Bram Moolenaar8c62a082019-02-08 14:34:10 +01001973 if (restricted != 0 && (ea.argt & RESTRICT))
1974 {
1975 errormsg = _("E981: Command not allowed in rvim");
1976 goto doend;
1977 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001978 if (!curbuf->b_p_ma && (ea.argt & MODIFY))
1979 {
1980 /* Command not allowed in non-'modifiable' buffer */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001981 errormsg = _(e_modifiable);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001982 goto doend;
1983 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00001984
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00001985 if (text_locked() && !(ea.argt & CMDWIN)
Bram Moolenaar958636c2014-10-21 20:01:58 +02001986 && !IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001987 {
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00001988 /* Command not allowed when editing the command line. */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001989 errormsg = _(get_text_locked_msg());
Bram Moolenaar071d4272004-06-13 20:20:40 +00001990 goto doend;
1991 }
Bram Moolenaar379fb762018-08-30 15:58:28 +02001992
Bram Moolenaar5555acc2006-04-07 21:33:12 +00001993 /* Disallow editing another buffer when "curbuf_lock" is set.
Bram Moolenaar379fb762018-08-30 15:58:28 +02001994 * Do allow ":checktime" (it is postponed).
1995 * Do allow ":edit" (check for an argument later).
1996 * Do allow ":file" with no arguments (check for an argument later). */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001997 if (!(ea.argt & CMDWIN)
Bram Moolenaar5555acc2006-04-07 21:33:12 +00001998 && ea.cmdidx != CMD_checktime
Bram Moolenaar379fb762018-08-30 15:58:28 +02001999 && ea.cmdidx != CMD_edit
2000 && ea.cmdidx != CMD_file
Bram Moolenaar958636c2014-10-21 20:01:58 +02002001 && !IS_USER_CMDIDX(ea.cmdidx)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002002 && curbuf_locked())
2003 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002004
2005 if (!ni && !(ea.argt & RANGE) && ea.addr_count > 0)
2006 {
2007 /* no range allowed */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002008 errormsg = _(e_norange);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002009 goto doend;
2010 }
2011 }
2012
2013 if (!ni && !(ea.argt & BANG) && ea.forceit) /* no <!> allowed */
2014 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002015 errormsg = _(e_nobang);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002016 goto doend;
2017 }
2018
2019 /*
2020 * Don't complain about the range if it is not used
2021 * (could happen if line_count is accidentally set to 0).
2022 */
Bram Moolenaarb7316892019-05-01 18:08:42 +02002023 if (!ea.skip && !ni && (ea.argt & RANGE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002024 {
2025 /*
2026 * If the range is backwards, ask for confirmation and, if given, swap
2027 * ea.line1 & ea.line2 so it's forwards again.
2028 * When global command is busy, don't ask, will fail below.
2029 */
2030 if (!global_busy && ea.line1 > ea.line2)
2031 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002032 if (msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002033 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002034 if (sourcing || exmode_active)
2035 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002036 errormsg = _("E493: Backwards range given");
Bram Moolenaara5792f52005-11-23 21:25:05 +00002037 goto doend;
2038 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002039 if (ask_yesno((char_u *)
2040 _("Backwards range given, OK to swap"), FALSE) != 'y')
Bram Moolenaara5792f52005-11-23 21:25:05 +00002041 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002042 }
2043 lnum = ea.line1;
2044 ea.line1 = ea.line2;
2045 ea.line2 = lnum;
2046 }
2047 if ((errormsg = invalid_range(&ea)) != NULL)
2048 goto doend;
2049 }
2050
Bram Moolenaarb7316892019-05-01 18:08:42 +02002051 if ((ea.addr_type == ADDR_OTHER) && ea.addr_count == 0)
2052 // default is 1, not cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00002053 ea.line2 = 1;
2054
2055 correct_range(&ea);
2056
2057#ifdef FEAT_FOLDING
Bram Moolenaara3306952016-01-02 21:41:06 +01002058 if (((ea.argt & WHOLEFOLD) || ea.addr_count >= 2) && !global_busy
2059 && ea.addr_type == ADDR_LINES)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002060 {
2061 /* Put the first line at the start of a closed fold, put the last line
2062 * at the end of a closed fold. */
2063 (void)hasFolding(ea.line1, &ea.line1, NULL);
2064 (void)hasFolding(ea.line2, NULL, &ea.line2);
2065 }
2066#endif
2067
2068#ifdef FEAT_QUICKFIX
2069 /*
Bram Moolenaar86b68352004-12-27 21:59:20 +00002070 * For the ":make" and ":grep" commands we insert the 'makeprg'/'grepprg'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002071 * option here, so things like % get expanded.
2072 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002073 p = replace_makeprg(&ea, p, cmdlinep);
2074 if (p == NULL)
2075 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002076#endif
2077
2078 /*
2079 * Skip to start of argument.
2080 * Don't do this for the ":!" command, because ":!! -l" needs the space.
2081 */
2082 if (ea.cmdidx == CMD_bang)
2083 ea.arg = p;
2084 else
2085 ea.arg = skipwhite(p);
2086
Bram Moolenaar379fb762018-08-30 15:58:28 +02002087 // ":file" cannot be run with an argument when "curbuf_lock" is set
2088 if (ea.cmdidx == CMD_file && *ea.arg != NUL && curbuf_locked())
2089 goto doend;
2090
Bram Moolenaar071d4272004-06-13 20:20:40 +00002091 /*
2092 * Check for "++opt=val" argument.
2093 * Must be first, allow ":w ++enc=utf8 !cmd"
2094 */
2095 if (ea.argt & ARGOPT)
2096 while (ea.arg[0] == '+' && ea.arg[1] == '+')
2097 if (getargopt(&ea) == FAIL && !ni)
2098 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002099 errormsg = _(e_invarg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002100 goto doend;
2101 }
2102
2103 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
2104 {
2105 if (*ea.arg == '>') /* append */
2106 {
2107 if (*++ea.arg != '>') /* typed wrong */
2108 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002109 errormsg = _("E494: Use w or w>>");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002110 goto doend;
2111 }
2112 ea.arg = skipwhite(ea.arg + 1);
2113 ea.append = TRUE;
2114 }
2115 else if (*ea.arg == '!' && ea.cmdidx == CMD_write) /* :w !filter */
2116 {
2117 ++ea.arg;
2118 ea.usefilter = TRUE;
2119 }
2120 }
2121
2122 if (ea.cmdidx == CMD_read)
2123 {
2124 if (ea.forceit)
2125 {
2126 ea.usefilter = TRUE; /* :r! filter if ea.forceit */
2127 ea.forceit = FALSE;
2128 }
2129 else if (*ea.arg == '!') /* :r !filter */
2130 {
2131 ++ea.arg;
2132 ea.usefilter = TRUE;
2133 }
2134 }
2135
2136 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
2137 {
2138 ea.amount = 1;
2139 while (*ea.arg == *ea.cmd) /* count number of '>' or '<' */
2140 {
2141 ++ea.arg;
2142 ++ea.amount;
2143 }
2144 ea.arg = skipwhite(ea.arg);
2145 }
2146
2147 /*
2148 * Check for "+command" argument, before checking for next command.
2149 * Don't do this for ":read !cmd" and ":write !cmd".
2150 */
2151 if ((ea.argt & EDITCMD) && !ea.usefilter)
2152 ea.do_ecmd_cmd = getargcmd(&ea.arg);
2153
2154 /*
2155 * Check for '|' to separate commands and '"' to start comments.
2156 * Don't do this for ":read !cmd" and ":write !cmd".
2157 */
2158 if ((ea.argt & TRLBAR) && !ea.usefilter)
2159 separate_nextcmd(&ea);
2160
2161 /*
2162 * Check for <newline> to end a shell command.
Bram Moolenaardf177f62005-02-22 08:39:57 +00002163 * Also do this for ":read !cmd", ":write !cmd" and ":global".
2164 * Any others?
Bram Moolenaar071d4272004-06-13 20:20:40 +00002165 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00002166 else if (ea.cmdidx == CMD_bang
Bram Moolenaar67883b42017-07-27 22:57:00 +02002167 || ea.cmdidx == CMD_terminal
Bram Moolenaardf177f62005-02-22 08:39:57 +00002168 || ea.cmdidx == CMD_global
2169 || ea.cmdidx == CMD_vglobal
2170 || ea.usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002171 {
2172 for (p = ea.arg; *p; ++p)
2173 {
2174 /* Remove one backslash before a newline, so that it's possible to
2175 * pass a newline to the shell and also a newline that is preceded
2176 * with a backslash. This makes it impossible to end a shell
2177 * command in a backslash, but that doesn't appear useful.
2178 * Halving the number of backslashes is incompatible with previous
2179 * versions. */
2180 if (*p == '\\' && p[1] == '\n')
Bram Moolenaara7241f52008-06-24 20:39:31 +00002181 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002182 else if (*p == '\n')
2183 {
2184 ea.nextcmd = p + 1;
2185 *p = NUL;
2186 break;
2187 }
2188 }
2189 }
2190
2191 if ((ea.argt & DFLALL) && ea.addr_count == 0)
2192 {
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002193 buf_T *buf;
2194
Bram Moolenaar071d4272004-06-13 20:20:40 +00002195 ea.line1 = 1;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002196 switch (ea.addr_type)
2197 {
2198 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02002199 case ADDR_OTHER:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002200 ea.line2 = curbuf->b_ml.ml_line_count;
2201 break;
2202 case ADDR_LOADED_BUFFERS:
2203 buf = firstbuf;
2204 while (buf->b_next != NULL && buf->b_ml.ml_mfp == NULL)
2205 buf = buf->b_next;
2206 ea.line1 = buf->b_fnum;
2207 buf = lastbuf;
2208 while (buf->b_prev != NULL && buf->b_ml.ml_mfp == NULL)
2209 buf = buf->b_prev;
2210 ea.line2 = buf->b_fnum;
2211 break;
2212 case ADDR_BUFFERS:
2213 ea.line1 = firstbuf->b_fnum;
2214 ea.line2 = lastbuf->b_fnum;
2215 break;
2216 case ADDR_WINDOWS:
2217 ea.line2 = LAST_WIN_NR;
2218 break;
2219 case ADDR_TABS:
2220 ea.line2 = LAST_TAB_NR;
2221 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01002222 case ADDR_TABS_RELATIVE:
2223 ea.line2 = 1;
2224 break;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002225 case ADDR_ARGUMENTS:
2226 if (ARGCOUNT == 0)
2227 ea.line1 = ea.line2 = 0;
2228 else
2229 ea.line2 = ARGCOUNT;
2230 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02002231#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02002232 case ADDR_QUICKFIX:
2233 ea.line2 = qf_get_size(&ea);
2234 if (ea.line2 == 0)
2235 ea.line2 = 1;
2236 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02002237#endif
Bram Moolenaarb7316892019-05-01 18:08:42 +02002238 case ADDR_NONE:
2239 iemsg(_("INTERNAL: Cannot use DFLALL with ADDR_NONE"));
2240 break;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002241 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002242 }
2243
2244 /* accept numbered register only when no count allowed (:put) */
2245 if ( (ea.argt & REGSTR)
2246 && *ea.arg != NUL
Bram Moolenaar958636c2014-10-21 20:01:58 +02002247 /* Do not allow register = for user commands */
2248 && (!IS_USER_CMDIDX(ea.cmdidx) || *ea.arg != '=')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002249 && !((ea.argt & COUNT) && VIM_ISDIGIT(*ea.arg)))
2250 {
Bram Moolenaar85de2062011-05-05 14:26:41 +02002251#ifndef FEAT_CLIPBOARD
2252 /* check these explicitly for a more specific error message */
2253 if (*ea.arg == '*' || *ea.arg == '+')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002254 {
Bram Moolenaar99b12722019-01-14 20:16:40 +01002255 errormsg = _(e_invalidreg);
Bram Moolenaar85de2062011-05-05 14:26:41 +02002256 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002257 }
2258#endif
Bram Moolenaar958636c2014-10-21 20:01:58 +02002259 if (valid_yank_reg(*ea.arg, (ea.cmdidx != CMD_put
2260 && !IS_USER_CMDIDX(ea.cmdidx))))
Bram Moolenaar85de2062011-05-05 14:26:41 +02002261 {
2262 ea.regname = *ea.arg++;
2263#ifdef FEAT_EVAL
2264 /* for '=' register: accept the rest of the line as an expression */
2265 if (ea.arg[-1] == '=' && ea.arg[0] != NUL)
2266 {
2267 set_expr_line(vim_strsave(ea.arg));
2268 ea.arg += STRLEN(ea.arg);
2269 }
2270#endif
2271 ea.arg = skipwhite(ea.arg);
2272 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002273 }
2274
2275 /*
2276 * Check for a count. When accepting a BUFNAME, don't use "123foo" as a
2277 * count, it's a buffer name.
2278 */
2279 if ((ea.argt & COUNT) && VIM_ISDIGIT(*ea.arg)
2280 && (!(ea.argt & BUFNAME) || *(p = skipdigits(ea.arg)) == NUL
Bram Moolenaar1c465442017-03-12 20:10:05 +01002281 || VIM_ISWHITE(*p)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002282 {
2283 n = getdigits(&ea.arg);
2284 ea.arg = skipwhite(ea.arg);
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002285 if (n <= 0 && !ni && (ea.argt & ZEROR) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002286 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002287 errormsg = _(e_zerocount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002288 goto doend;
2289 }
Bram Moolenaarb7316892019-05-01 18:08:42 +02002290 if (ea.addr_type != ADDR_LINES) // e.g. :buffer 2, :sleep 3
Bram Moolenaar071d4272004-06-13 20:20:40 +00002291 {
2292 ea.line2 = n;
2293 if (ea.addr_count == 0)
2294 ea.addr_count = 1;
2295 }
2296 else
2297 {
2298 ea.line1 = ea.line2;
2299 ea.line2 += n - 1;
2300 ++ea.addr_count;
2301 /*
2302 * Be vi compatible: no error message for out of range.
2303 */
Bram Moolenaarb7316892019-05-01 18:08:42 +02002304 if (ea.line2 > curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002305 ea.line2 = curbuf->b_ml.ml_line_count;
2306 }
2307 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00002308
2309 /*
2310 * Check for flags: 'l', 'p' and '#'.
2311 */
2312 if (ea.argt & EXFLAGS)
2313 get_flags(&ea);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002314 /* no arguments allowed */
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002315 if (!ni && !(ea.argt & EXTRA) && *ea.arg != NUL
Bram Moolenaara2031822006-03-07 22:29:51 +00002316 && *ea.arg != '"' && (*ea.arg != '|' || (ea.argt & TRLBAR) == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002317 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002318 errormsg = _(e_trailing);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002319 goto doend;
2320 }
2321
2322 if (!ni && (ea.argt & NEEDARG) && *ea.arg == NUL)
2323 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002324 errormsg = _(e_argreq);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002325 goto doend;
2326 }
2327
2328#ifdef FEAT_EVAL
2329 /*
2330 * Skip the command when it's not going to be executed.
2331 * The commands like :if, :endif, etc. always need to be executed.
2332 * Also make an exception for commands that handle a trailing command
2333 * themselves.
2334 */
2335 if (ea.skip)
2336 {
2337 switch (ea.cmdidx)
2338 {
2339 /* commands that need evaluation */
2340 case CMD_while:
2341 case CMD_endwhile:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00002342 case CMD_for:
2343 case CMD_endfor:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002344 case CMD_if:
2345 case CMD_elseif:
2346 case CMD_else:
2347 case CMD_endif:
2348 case CMD_try:
2349 case CMD_catch:
2350 case CMD_finally:
2351 case CMD_endtry:
2352 case CMD_function:
2353 break;
2354
2355 /* Commands that handle '|' themselves. Check: A command should
2356 * either have the TRLBAR flag, appear in this list or appear in
2357 * the list at ":help :bar". */
2358 case CMD_aboveleft:
2359 case CMD_and:
2360 case CMD_belowright:
2361 case CMD_botright:
2362 case CMD_browse:
2363 case CMD_call:
2364 case CMD_confirm:
2365 case CMD_delfunction:
2366 case CMD_djump:
2367 case CMD_dlist:
2368 case CMD_dsearch:
2369 case CMD_dsplit:
2370 case CMD_echo:
2371 case CMD_echoerr:
2372 case CMD_echomsg:
2373 case CMD_echon:
2374 case CMD_execute:
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002375 case CMD_filter:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002376 case CMD_help:
2377 case CMD_hide:
2378 case CMD_ijump:
2379 case CMD_ilist:
2380 case CMD_isearch:
2381 case CMD_isplit:
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002382 case CMD_keepalt:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002383 case CMD_keepjumps:
2384 case CMD_keepmarks:
Bram Moolenaara939e432013-11-09 05:30:26 +01002385 case CMD_keeppatterns:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002386 case CMD_leftabove:
2387 case CMD_let:
2388 case CMD_lockmarks:
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002389 case CMD_lua:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002390 case CMD_match:
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002391 case CMD_mzscheme:
Bram Moolenaar5803ae62014-03-23 16:04:02 +01002392 case CMD_noautocmd:
2393 case CMD_noswapfile:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002394 case CMD_perl:
2395 case CMD_psearch:
2396 case CMD_python:
Bram Moolenaar368373e2010-07-19 20:46:22 +02002397 case CMD_py3:
Bram Moolenaarb6590522010-07-21 16:00:43 +02002398 case CMD_python3:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002399 case CMD_return:
2400 case CMD_rightbelow:
2401 case CMD_ruby:
2402 case CMD_silent:
2403 case CMD_smagic:
2404 case CMD_snomagic:
2405 case CMD_substitute:
2406 case CMD_syntax:
Bram Moolenaara226a6d2006-02-26 23:59:20 +00002407 case CMD_tab:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002408 case CMD_tcl:
2409 case CMD_throw:
2410 case CMD_tilde:
2411 case CMD_topleft:
2412 case CMD_unlet:
2413 case CMD_verbose:
2414 case CMD_vertical:
Bram Moolenaar12bde492011-06-13 01:19:56 +02002415 case CMD_wincmd:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002416 break;
2417
2418 default: goto doend;
2419 }
2420 }
2421#endif
2422
2423 if (ea.argt & XFILE)
2424 {
2425 if (expand_filename(&ea, cmdlinep, &errormsg) == FAIL)
2426 goto doend;
2427 }
2428
Bram Moolenaar071d4272004-06-13 20:20:40 +00002429 /*
2430 * Accept buffer name. Cannot be used at the same time with a buffer
2431 * number. Don't do this for a user command.
2432 */
2433 if ((ea.argt & BUFNAME) && *ea.arg != NUL && ea.addr_count == 0
Bram Moolenaar958636c2014-10-21 20:01:58 +02002434 && !IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002435 {
2436 /*
2437 * :bdelete, :bwipeout and :bunload take several arguments, separated
2438 * by spaces: find next space (skipping over escaped characters).
2439 * The others take one argument: ignore trailing spaces.
2440 */
2441 if (ea.cmdidx == CMD_bdelete || ea.cmdidx == CMD_bwipeout
2442 || ea.cmdidx == CMD_bunload)
2443 p = skiptowhite_esc(ea.arg);
2444 else
2445 {
2446 p = ea.arg + STRLEN(ea.arg);
Bram Moolenaar1c465442017-03-12 20:10:05 +01002447 while (p > ea.arg && VIM_ISWHITE(p[-1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002448 --p;
2449 }
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002450 ea.line2 = buflist_findpat(ea.arg, p, (ea.argt & BUFUNL) != 0,
2451 FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002452 if (ea.line2 < 0) /* failed */
2453 goto doend;
2454 ea.addr_count = 1;
2455 ea.arg = skipwhite(p);
2456 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002457
Bram Moolenaar2be57332018-02-13 18:05:18 +01002458 /* The :try command saves the emsg_silent flag, reset it here when
2459 * ":silent! try" was used, it should only apply to :try itself. */
Bram Moolenaareffed932018-08-14 13:38:17 +02002460 if (ea.cmdidx == CMD_try && ea.did_esilent > 0)
Bram Moolenaar2be57332018-02-13 18:05:18 +01002461 {
Bram Moolenaareffed932018-08-14 13:38:17 +02002462 emsg_silent -= ea.did_esilent;
Bram Moolenaar2be57332018-02-13 18:05:18 +01002463 if (emsg_silent < 0)
2464 emsg_silent = 0;
Bram Moolenaareffed932018-08-14 13:38:17 +02002465 ea.did_esilent = 0;
Bram Moolenaar2be57332018-02-13 18:05:18 +01002466 }
2467
Bram Moolenaar071d4272004-06-13 20:20:40 +00002468/*
Bram Moolenaar2be57332018-02-13 18:05:18 +01002469 * 7. Execute the command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002470 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002471
Bram Moolenaar958636c2014-10-21 20:01:58 +02002472 if (IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002473 {
2474 /*
2475 * Execute a user-defined command.
2476 */
2477 do_ucmd(&ea);
2478 }
2479 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002480 {
2481 /*
Bram Moolenaarac9fb182019-04-27 13:04:13 +02002482 * Call the function to execute the builtin command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002483 */
2484 ea.errmsg = NULL;
2485 (cmdnames[ea.cmdidx].cmd_func)(&ea);
2486 if (ea.errmsg != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002487 errormsg = _(ea.errmsg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002488 }
2489
2490#ifdef FEAT_EVAL
2491 /*
2492 * If the command just executed called do_cmdline(), any throw or ":return"
2493 * or ":finish" encountered there must also check the cstack of the still
2494 * active do_cmdline() that called this do_one_cmd(). Rethrow an uncaught
2495 * exception, or reanimate a returned function or finished script file and
2496 * return or finish it again.
2497 */
2498 if (need_rethrow)
2499 do_throw(cstack);
2500 else if (check_cstack)
2501 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002502 if (source_finished(fgetline, cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002503 do_finish(&ea, TRUE);
Bram Moolenaar89d40322006-08-29 15:30:07 +00002504 else if (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002505 && current_func_returned())
2506 do_return(&ea, TRUE, FALSE, NULL);
2507 }
2508 need_rethrow = check_cstack = FALSE;
2509#endif
2510
2511doend:
2512 if (curwin->w_cursor.lnum == 0) /* can happen with zero line number */
Bram Moolenaar6de5e122017-04-20 21:55:44 +02002513 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002514 curwin->w_cursor.lnum = 1;
Bram Moolenaar6de5e122017-04-20 21:55:44 +02002515 curwin->w_cursor.col = 0;
2516 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002517
2518 if (errormsg != NULL && *errormsg != NUL && !did_emsg)
2519 {
2520 if (sourcing)
2521 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002522 if (errormsg != (char *)IObuff)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002523 {
2524 STRCPY(IObuff, errormsg);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002525 errormsg = (char *)IObuff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002526 }
Bram Moolenaara6f4d612011-09-21 19:10:46 +02002527 append_command(*cmdlinep);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002528 }
2529 emsg(errormsg);
2530 }
2531#ifdef FEAT_EVAL
2532 do_errthrow(cstack,
Bram Moolenaar958636c2014-10-21 20:01:58 +02002533 (ea.cmdidx != CMD_SIZE && !IS_USER_CMDIDX(ea.cmdidx))
2534 ? cmdnames[(int)ea.cmdidx].cmd_name : (char_u *)NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002535#endif
2536
Bram Moolenaareffed932018-08-14 13:38:17 +02002537 if (ea.verbose_save >= 0)
2538 p_verbose = ea.verbose_save;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002539
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002540 free_cmdmod();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002541 cmdmod = save_cmdmod;
Bram Moolenaar9a2c0912019-03-30 14:26:18 +01002542 reg_executing = save_reg_executing;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002543
Bram Moolenaareffed932018-08-14 13:38:17 +02002544 if (ea.save_msg_silent != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002545 {
2546 /* messages could be enabled for a serious error, need to check if the
2547 * counters don't become negative */
Bram Moolenaareffed932018-08-14 13:38:17 +02002548 if (!did_emsg || msg_silent > ea.save_msg_silent)
2549 msg_silent = ea.save_msg_silent;
2550 emsg_silent -= ea.did_esilent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002551 if (emsg_silent < 0)
2552 emsg_silent = 0;
2553 /* Restore msg_scroll, it's set by file I/O commands, even when no
2554 * message is actually displayed. */
2555 msg_scroll = save_msg_scroll;
Bram Moolenaar77ab2802009-04-22 12:44:48 +00002556
2557 /* "silent reg" or "silent echo x" inside "redir" leaves msg_col
2558 * somewhere in the line. Put it back in the first column. */
2559 if (redirecting())
2560 msg_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002561 }
2562
Bram Moolenaar7171abe2004-10-11 10:06:20 +00002563#ifdef HAVE_SANDBOX
Bram Moolenaareffed932018-08-14 13:38:17 +02002564 if (ea.did_sandbox)
Bram Moolenaar7171abe2004-10-11 10:06:20 +00002565 --sandbox;
2566#endif
2567
Bram Moolenaar071d4272004-06-13 20:20:40 +00002568 if (ea.nextcmd && *ea.nextcmd == NUL) /* not really a next command */
2569 ea.nextcmd = NULL;
2570
2571#ifdef FEAT_EVAL
2572 --ex_nesting_level;
2573#endif
2574
2575 return ea.nextcmd;
2576}
2577#if (_MSC_VER == 1200)
Bram Moolenaar281bdce2005-01-25 21:53:18 +00002578 #pragma optimize( "", on )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002579#endif
2580
2581/*
Bram Moolenaareffed932018-08-14 13:38:17 +02002582 * Parse and skip over command modifiers:
2583 * - update eap->cmd
2584 * - store flags in "cmdmod".
2585 * - Set ex_pressedreturn for an empty command line.
2586 * - set msg_silent for ":silent"
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002587 * - set 'eventignore' to "all" for ":noautocmd"
Bram Moolenaareffed932018-08-14 13:38:17 +02002588 * - set p_verbose for ":verbose"
2589 * - Increment "sandbox" for ":sandbox"
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002590 * When "skip_only" is TRUE the global variables are not changed, except for
2591 * "cmdmod".
Bram Moolenaareffed932018-08-14 13:38:17 +02002592 * Return FAIL when the command is not to be executed.
2593 * May set "errormsg" to an error message.
2594 */
2595 int
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002596parse_command_modifiers(exarg_T *eap, char **errormsg, int skip_only)
Bram Moolenaareffed932018-08-14 13:38:17 +02002597{
2598 char_u *p;
2599
2600 vim_memset(&cmdmod, 0, sizeof(cmdmod));
2601 eap->verbose_save = -1;
2602 eap->save_msg_silent = -1;
2603
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002604 // Repeat until no more command modifiers are found.
Bram Moolenaareffed932018-08-14 13:38:17 +02002605 for (;;)
2606 {
Bram Moolenaareffed932018-08-14 13:38:17 +02002607 while (*eap->cmd == ' ' || *eap->cmd == '\t' || *eap->cmd == ':')
2608 ++eap->cmd;
2609
2610 /* in ex mode, an empty line works like :+ */
2611 if (*eap->cmd == NUL && exmode_active
2612 && (getline_equal(eap->getline, eap->cookie, getexmodeline)
2613 || getline_equal(eap->getline, eap->cookie, getexline))
2614 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
2615 {
2616 eap->cmd = (char_u *)"+";
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002617 if (!skip_only)
2618 ex_pressedreturn = TRUE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002619 }
2620
2621 /* ignore comment and empty lines */
2622 if (*eap->cmd == '"')
2623 return FAIL;
2624 if (*eap->cmd == NUL)
2625 {
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002626 if (!skip_only)
2627 ex_pressedreturn = TRUE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002628 return FAIL;
2629 }
2630
Bram Moolenaareffed932018-08-14 13:38:17 +02002631 p = skip_range(eap->cmd, NULL);
2632 switch (*p)
2633 {
2634 /* When adding an entry, also modify cmd_exists(). */
2635 case 'a': if (!checkforcmd(&eap->cmd, "aboveleft", 3))
2636 break;
2637 cmdmod.split |= WSP_ABOVE;
2638 continue;
2639
2640 case 'b': if (checkforcmd(&eap->cmd, "belowright", 3))
2641 {
2642 cmdmod.split |= WSP_BELOW;
2643 continue;
2644 }
2645 if (checkforcmd(&eap->cmd, "browse", 3))
2646 {
2647#ifdef FEAT_BROWSE_CMD
2648 cmdmod.browse = TRUE;
2649#endif
2650 continue;
2651 }
2652 if (!checkforcmd(&eap->cmd, "botright", 2))
2653 break;
2654 cmdmod.split |= WSP_BOT;
2655 continue;
2656
2657 case 'c': if (!checkforcmd(&eap->cmd, "confirm", 4))
2658 break;
2659#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
2660 cmdmod.confirm = TRUE;
2661#endif
2662 continue;
2663
2664 case 'k': if (checkforcmd(&eap->cmd, "keepmarks", 3))
2665 {
2666 cmdmod.keepmarks = TRUE;
2667 continue;
2668 }
2669 if (checkforcmd(&eap->cmd, "keepalt", 5))
2670 {
2671 cmdmod.keepalt = TRUE;
2672 continue;
2673 }
2674 if (checkforcmd(&eap->cmd, "keeppatterns", 5))
2675 {
2676 cmdmod.keeppatterns = TRUE;
2677 continue;
2678 }
2679 if (!checkforcmd(&eap->cmd, "keepjumps", 5))
2680 break;
2681 cmdmod.keepjumps = TRUE;
2682 continue;
2683
2684 case 'f': /* only accept ":filter {pat} cmd" */
2685 {
2686 char_u *reg_pat;
2687
2688 if (!checkforcmd(&p, "filter", 4)
2689 || *p == NUL || ends_excmd(*p))
2690 break;
2691 if (*p == '!')
2692 {
2693 cmdmod.filter_force = TRUE;
2694 p = skipwhite(p + 1);
2695 if (*p == NUL || ends_excmd(*p))
2696 break;
2697 }
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002698 if (skip_only)
2699 p = skip_vimgrep_pat(p, NULL, NULL);
2700 else
2701 // NOTE: This puts a NUL after the pattern.
2702 p = skip_vimgrep_pat(p, &reg_pat, NULL);
Bram Moolenaareffed932018-08-14 13:38:17 +02002703 if (p == NULL || *p == NUL)
2704 break;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002705 if (!skip_only)
2706 {
2707 cmdmod.filter_regmatch.regprog =
Bram Moolenaareffed932018-08-14 13:38:17 +02002708 vim_regcomp(reg_pat, RE_MAGIC);
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002709 if (cmdmod.filter_regmatch.regprog == NULL)
2710 break;
2711 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002712 eap->cmd = p;
2713 continue;
2714 }
2715
2716 /* ":hide" and ":hide | cmd" are not modifiers */
2717 case 'h': if (p != eap->cmd || !checkforcmd(&p, "hide", 3)
2718 || *p == NUL || ends_excmd(*p))
2719 break;
2720 eap->cmd = p;
2721 cmdmod.hide = TRUE;
2722 continue;
2723
2724 case 'l': if (checkforcmd(&eap->cmd, "lockmarks", 3))
2725 {
2726 cmdmod.lockmarks = TRUE;
2727 continue;
2728 }
2729
2730 if (!checkforcmd(&eap->cmd, "leftabove", 5))
2731 break;
2732 cmdmod.split |= WSP_ABOVE;
2733 continue;
2734
2735 case 'n': if (checkforcmd(&eap->cmd, "noautocmd", 3))
2736 {
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002737 if (cmdmod.save_ei == NULL && !skip_only)
Bram Moolenaareffed932018-08-14 13:38:17 +02002738 {
2739 /* Set 'eventignore' to "all". Restore the
2740 * existing option value later. */
2741 cmdmod.save_ei = vim_strsave(p_ei);
2742 set_string_option_direct((char_u *)"ei", -1,
2743 (char_u *)"all", OPT_FREE, SID_NONE);
2744 }
2745 continue;
2746 }
2747 if (!checkforcmd(&eap->cmd, "noswapfile", 3))
2748 break;
2749 cmdmod.noswapfile = TRUE;
2750 continue;
2751
2752 case 'r': if (!checkforcmd(&eap->cmd, "rightbelow", 6))
2753 break;
2754 cmdmod.split |= WSP_BELOW;
2755 continue;
2756
2757 case 's': if (checkforcmd(&eap->cmd, "sandbox", 3))
2758 {
2759#ifdef HAVE_SANDBOX
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002760 if (!skip_only)
2761 {
2762 if (!eap->did_sandbox)
2763 ++sandbox;
2764 eap->did_sandbox = TRUE;
2765 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002766#endif
2767 continue;
2768 }
2769 if (!checkforcmd(&eap->cmd, "silent", 3))
2770 break;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002771 if (!skip_only)
2772 {
2773 if (eap->save_msg_silent == -1)
2774 eap->save_msg_silent = msg_silent;
2775 ++msg_silent;
2776 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002777 if (*eap->cmd == '!' && !VIM_ISWHITE(eap->cmd[-1]))
2778 {
2779 /* ":silent!", but not "silent !cmd" */
2780 eap->cmd = skipwhite(eap->cmd + 1);
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002781 if (!skip_only)
2782 {
2783 ++emsg_silent;
2784 ++eap->did_esilent;
2785 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002786 }
2787 continue;
2788
2789 case 't': if (checkforcmd(&p, "tab", 3))
2790 {
Bram Moolenaar548e5982018-12-26 21:45:00 +01002791 if (!skip_only)
Bram Moolenaareffed932018-08-14 13:38:17 +02002792 {
Bram Moolenaar548e5982018-12-26 21:45:00 +01002793 long tabnr = get_address(eap, &eap->cmd,
2794 ADDR_TABS, eap->skip,
2795 skip_only, FALSE, 1);
2796 if (tabnr == MAXLNUM)
2797 cmdmod.tab = tabpage_index(curtab) + 1;
2798 else
Bram Moolenaareffed932018-08-14 13:38:17 +02002799 {
Bram Moolenaar548e5982018-12-26 21:45:00 +01002800 if (tabnr < 0 || tabnr > LAST_TAB_NR)
2801 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002802 *errormsg = _(e_invrange);
Bram Moolenaar548e5982018-12-26 21:45:00 +01002803 return FAIL;
2804 }
2805 cmdmod.tab = tabnr + 1;
Bram Moolenaareffed932018-08-14 13:38:17 +02002806 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002807 }
2808 eap->cmd = p;
2809 continue;
2810 }
2811 if (!checkforcmd(&eap->cmd, "topleft", 2))
2812 break;
2813 cmdmod.split |= WSP_TOP;
2814 continue;
2815
2816 case 'u': if (!checkforcmd(&eap->cmd, "unsilent", 3))
2817 break;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002818 if (!skip_only)
2819 {
2820 if (eap->save_msg_silent == -1)
2821 eap->save_msg_silent = msg_silent;
2822 msg_silent = 0;
2823 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002824 continue;
2825
2826 case 'v': if (checkforcmd(&eap->cmd, "vertical", 4))
2827 {
2828 cmdmod.split |= WSP_VERT;
2829 continue;
2830 }
2831 if (!checkforcmd(&p, "verbose", 4))
2832 break;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002833 if (!skip_only)
2834 {
2835 if (eap->verbose_save < 0)
2836 eap->verbose_save = p_verbose;
2837 if (vim_isdigit(*eap->cmd))
2838 p_verbose = atoi((char *)eap->cmd);
2839 else
2840 p_verbose = 1;
2841 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002842 eap->cmd = p;
2843 continue;
2844 }
2845 break;
2846 }
2847
2848 return OK;
2849}
2850
2851/*
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002852 * Free contents of "cmdmod".
2853 */
2854 static void
2855free_cmdmod(void)
2856{
2857 if (cmdmod.save_ei != NULL)
2858 {
2859 /* Restore 'eventignore' to the value before ":noautocmd". */
2860 set_string_option_direct((char_u *)"ei", -1, cmdmod.save_ei,
2861 OPT_FREE, SID_NONE);
2862 free_string_option(cmdmod.save_ei);
2863 }
2864
2865 if (cmdmod.filter_regmatch.regprog != NULL)
2866 vim_regfree(cmdmod.filter_regmatch.regprog);
2867}
2868
2869/*
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002870 * Parse the address range, if any, in "eap".
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02002871 * May set the last search pattern, unless "silent" is TRUE.
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002872 * Return FAIL and set "errormsg" or return OK.
2873 */
2874 int
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002875parse_cmd_address(exarg_T *eap, char **errormsg, int silent)
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002876{
2877 int address_count = 1;
2878 linenr_T lnum;
2879
2880 // Repeat for all ',' or ';' separated addresses.
2881 for (;;)
2882 {
2883 eap->line1 = eap->line2;
2884 switch (eap->addr_type)
2885 {
2886 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02002887 case ADDR_OTHER:
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002888 // default is current line number
2889 eap->line2 = curwin->w_cursor.lnum;
2890 break;
2891 case ADDR_WINDOWS:
2892 eap->line2 = CURRENT_WIN_NR;
2893 break;
2894 case ADDR_ARGUMENTS:
2895 eap->line2 = curwin->w_arg_idx + 1;
2896 if (eap->line2 > ARGCOUNT)
2897 eap->line2 = ARGCOUNT;
2898 break;
2899 case ADDR_LOADED_BUFFERS:
2900 case ADDR_BUFFERS:
2901 eap->line2 = curbuf->b_fnum;
2902 break;
2903 case ADDR_TABS:
2904 eap->line2 = CURRENT_TAB_NR;
2905 break;
2906 case ADDR_TABS_RELATIVE:
2907 eap->line2 = 1;
2908 break;
2909#ifdef FEAT_QUICKFIX
2910 case ADDR_QUICKFIX:
2911 eap->line2 = qf_get_cur_valid_idx(eap);
2912 break;
2913#endif
Bram Moolenaarb7316892019-05-01 18:08:42 +02002914 case ADDR_NONE:
2915 // Will give an error later if a range is found.
2916 break;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002917 }
2918 eap->cmd = skipwhite(eap->cmd);
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02002919 lnum = get_address(eap, &eap->cmd, eap->addr_type, eap->skip, silent,
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002920 eap->addr_count == 0, address_count++);
2921 if (eap->cmd == NULL) // error detected
2922 return FAIL;
2923 if (lnum == MAXLNUM)
2924 {
2925 if (*eap->cmd == '%') // '%' - all lines
2926 {
2927 ++eap->cmd;
2928 switch (eap->addr_type)
2929 {
2930 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02002931 case ADDR_OTHER:
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002932 eap->line1 = 1;
2933 eap->line2 = curbuf->b_ml.ml_line_count;
2934 break;
2935 case ADDR_LOADED_BUFFERS:
2936 {
2937 buf_T *buf = firstbuf;
2938
2939 while (buf->b_next != NULL
2940 && buf->b_ml.ml_mfp == NULL)
2941 buf = buf->b_next;
2942 eap->line1 = buf->b_fnum;
2943 buf = lastbuf;
2944 while (buf->b_prev != NULL
2945 && buf->b_ml.ml_mfp == NULL)
2946 buf = buf->b_prev;
2947 eap->line2 = buf->b_fnum;
2948 break;
2949 }
2950 case ADDR_BUFFERS:
2951 eap->line1 = firstbuf->b_fnum;
2952 eap->line2 = lastbuf->b_fnum;
2953 break;
2954 case ADDR_WINDOWS:
2955 case ADDR_TABS:
2956 if (IS_USER_CMDIDX(eap->cmdidx))
2957 {
2958 eap->line1 = 1;
2959 eap->line2 = eap->addr_type == ADDR_WINDOWS
2960 ? LAST_WIN_NR : LAST_TAB_NR;
2961 }
2962 else
2963 {
2964 // there is no Vim command which uses '%' and
2965 // ADDR_WINDOWS or ADDR_TABS
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002966 *errormsg = _(e_invrange);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002967 return FAIL;
2968 }
2969 break;
2970 case ADDR_TABS_RELATIVE:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002971 *errormsg = _(e_invrange);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002972 return FAIL;
2973 case ADDR_ARGUMENTS:
2974 if (ARGCOUNT == 0)
2975 eap->line1 = eap->line2 = 0;
2976 else
2977 {
2978 eap->line1 = 1;
2979 eap->line2 = ARGCOUNT;
2980 }
2981 break;
2982#ifdef FEAT_QUICKFIX
2983 case ADDR_QUICKFIX:
2984 eap->line1 = 1;
2985 eap->line2 = qf_get_size(eap);
2986 if (eap->line2 == 0)
2987 eap->line2 = 1;
2988 break;
2989#endif
Bram Moolenaarb7316892019-05-01 18:08:42 +02002990 case ADDR_NONE:
2991 // Will give an error later if a range is found.
2992 break;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002993 }
2994 ++eap->addr_count;
2995 }
2996 else if (*eap->cmd == '*' && vim_strchr(p_cpo, CPO_STAR) == NULL)
2997 {
2998 pos_T *fp;
2999
3000 // '*' - visual area
3001 if (eap->addr_type != ADDR_LINES)
3002 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003003 *errormsg = _(e_invrange);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003004 return FAIL;
3005 }
3006
3007 ++eap->cmd;
3008 if (!eap->skip)
3009 {
3010 fp = getmark('<', FALSE);
3011 if (check_mark(fp) == FAIL)
3012 return FAIL;
3013 eap->line1 = fp->lnum;
3014 fp = getmark('>', FALSE);
3015 if (check_mark(fp) == FAIL)
3016 return FAIL;
3017 eap->line2 = fp->lnum;
3018 ++eap->addr_count;
3019 }
3020 }
3021 }
3022 else
3023 eap->line2 = lnum;
3024 eap->addr_count++;
3025
3026 if (*eap->cmd == ';')
3027 {
3028 if (!eap->skip)
3029 {
3030 curwin->w_cursor.lnum = eap->line2;
3031 // don't leave the cursor on an illegal line or column
3032 check_cursor();
3033 }
3034 }
3035 else if (*eap->cmd != ',')
3036 break;
3037 ++eap->cmd;
3038 }
3039
3040 // One address given: set start and end lines.
3041 if (eap->addr_count == 1)
3042 {
3043 eap->line1 = eap->line2;
3044 // ... but only implicit: really no address given
3045 if (lnum == MAXLNUM)
3046 eap->addr_count = 0;
3047 }
3048 return OK;
3049}
3050
3051/*
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00003052 * Check for an Ex command with optional tail.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003053 * If there is a match advance "pp" to the argument and return TRUE.
3054 */
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00003055 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003056checkforcmd(
3057 char_u **pp, /* start of command */
3058 char *cmd, /* name of command */
3059 int len) /* required length */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003060{
3061 int i;
3062
3063 for (i = 0; cmd[i] != NUL; ++i)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003064 if (((char_u *)cmd)[i] != (*pp)[i])
Bram Moolenaar071d4272004-06-13 20:20:40 +00003065 break;
3066 if (i >= len && !isalpha((*pp)[i]))
3067 {
3068 *pp = skipwhite(*pp + i);
3069 return TRUE;
3070 }
3071 return FALSE;
3072}
3073
3074/*
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003075 * Append "cmd" to the error message in IObuff.
3076 * Takes care of limiting the length and handling 0xa0, which would be
3077 * invisible otherwise.
3078 */
3079 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003080append_command(char_u *cmd)
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003081{
3082 char_u *s = cmd;
3083 char_u *d;
3084
3085 STRCAT(IObuff, ": ");
3086 d = IObuff + STRLEN(IObuff);
3087 while (*s != NUL && d - IObuff < IOSIZE - 7)
3088 {
Bram Moolenaar13505972019-01-24 15:04:48 +01003089 if (enc_utf8 ? (s[0] == 0xc2 && s[1] == 0xa0) : *s == 0xa0)
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003090 {
Bram Moolenaar13505972019-01-24 15:04:48 +01003091 s += enc_utf8 ? 2 : 1;
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003092 STRCPY(d, "<a0>");
3093 d += 4;
3094 }
3095 else
3096 MB_COPY_CHAR(s, d);
3097 }
3098 *d = NUL;
3099}
3100
3101/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003102 * Find an Ex command by its name, either built-in or user.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003103 * Start of the name can be found at eap->cmd.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003104 * Returns pointer to char after the command name.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003105 * "full" is set to TRUE if the whole command name matched.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003106 * Returns NULL for an ambiguous user command.
3107 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003108 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003109find_command(exarg_T *eap, int *full UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003110{
3111 int len;
3112 char_u *p;
Bram Moolenaardf177f62005-02-22 08:39:57 +00003113 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003114
3115 /*
3116 * Isolate the command and search for it in the command table.
Bram Moolenaar81870892007-11-11 18:17:28 +00003117 * Exceptions:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003118 * - the 'k' command can directly be followed by any character.
3119 * - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003120 * but :sre[wind] is another command, as are :scr[iptnames],
Bram Moolenaar071d4272004-06-13 20:20:40 +00003121 * :scs[cope], :sim[alt], :sig[ns] and :sil[ent].
Bram Moolenaardf177f62005-02-22 08:39:57 +00003122 * - the "d" command can directly be followed by 'l' or 'p' flag.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003123 */
3124 p = eap->cmd;
3125 if (*p == 'k')
3126 {
3127 eap->cmdidx = CMD_k;
3128 ++p;
3129 }
3130 else if (p[0] == 's'
Bram Moolenaar204b93f2015-08-04 22:02:51 +02003131 && ((p[1] == 'c' && (p[2] == NUL || (p[2] != 's' && p[2] != 'r'
3132 && (p[3] == NUL || (p[3] != 'i' && p[4] != 'p')))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003133 || p[1] == 'g'
3134 || (p[1] == 'i' && p[2] != 'm' && p[2] != 'l' && p[2] != 'g')
3135 || p[1] == 'I'
3136 || (p[1] == 'r' && p[2] != 'e')))
3137 {
3138 eap->cmdidx = CMD_substitute;
3139 ++p;
3140 }
3141 else
3142 {
3143 while (ASCII_ISALPHA(*p))
3144 ++p;
Bram Moolenaarb6590522010-07-21 16:00:43 +02003145 /* for python 3.x support ":py3", ":python3", ":py3file", etc. */
Bram Moolenaar55d5c032010-07-17 23:52:29 +02003146 if (eap->cmd[0] == 'p' && eap->cmd[1] == 'y')
Bram Moolenaarb6590522010-07-21 16:00:43 +02003147 while (ASCII_ISALNUM(*p))
3148 ++p;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02003149
Bram Moolenaar071d4272004-06-13 20:20:40 +00003150 /* check for non-alpha command */
3151 if (p == eap->cmd && vim_strchr((char_u *)"@*!=><&~#", *p) != NULL)
3152 ++p;
3153 len = (int)(p - eap->cmd);
Bram Moolenaardf177f62005-02-22 08:39:57 +00003154 if (*eap->cmd == 'd' && (p[-1] == 'l' || p[-1] == 'p'))
3155 {
3156 /* Check for ":dl", ":dell", etc. to ":deletel": that's
3157 * :delete with the 'l' flag. Same for 'p'. */
3158 for (i = 0; i < len; ++i)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003159 if (eap->cmd[i] != ((char_u *)"delete")[i])
Bram Moolenaardf177f62005-02-22 08:39:57 +00003160 break;
3161 if (i == len - 1)
3162 {
3163 --len;
3164 if (p[-1] == 'l')
3165 eap->flags |= EXFLAG_LIST;
3166 else
3167 eap->flags |= EXFLAG_PRINT;
3168 }
3169 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003170
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003171 if (ASCII_ISLOWER(eap->cmd[0]))
3172 {
Bram Moolenaar6c0c1e82017-03-25 15:07:43 +01003173 int c1 = eap->cmd[0];
3174 int c2 = eap->cmd[1];
3175
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003176 if (command_count != (int)CMD_SIZE)
3177 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003178 iemsg(_("E943: Command table needs to be updated, run 'make cmdidxs'"));
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003179 getout(1);
3180 }
3181
3182 /* Use a precomputed index for fast look-up in cmdnames[]
3183 * taking into account the first 2 letters of eap->cmd. */
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003184 eap->cmdidx = cmdidxs1[CharOrdLow(c1)];
3185 if (ASCII_ISLOWER(c2))
3186 eap->cmdidx += cmdidxs2[CharOrdLow(c1)][CharOrdLow(c2)];
3187 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003188 else
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003189 eap->cmdidx = CMD_bang;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003190
3191 for ( ; (int)eap->cmdidx < (int)CMD_SIZE;
3192 eap->cmdidx = (cmdidx_T)((int)eap->cmdidx + 1))
3193 if (STRNCMP(cmdnames[(int)eap->cmdidx].cmd_name, (char *)eap->cmd,
3194 (size_t)len) == 0)
3195 {
3196#ifdef FEAT_EVAL
3197 if (full != NULL
3198 && cmdnames[(int)eap->cmdidx].cmd_name[len] == NUL)
3199 *full = TRUE;
3200#endif
3201 break;
3202 }
3203
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003204 // Look for a user defined command as a last resort. Let ":Print" be
3205 // overruled by a user defined command.
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01003206 if ((eap->cmdidx == CMD_SIZE || eap->cmdidx == CMD_Print)
3207 && *eap->cmd >= 'A' && *eap->cmd <= 'Z')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003208 {
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003209 // User defined commands may contain digits.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003210 while (ASCII_ISALNUM(*p))
3211 ++p;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003212 p = find_ucmd(eap, p, full, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003213 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003214 if (p == eap->cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003215 eap->cmdidx = CMD_SIZE;
3216 }
3217
3218 return p;
3219}
3220
3221#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003222static struct cmdmod
3223{
3224 char *name;
3225 int minlen;
3226 int has_count; /* :123verbose :3tab */
3227} cmdmods[] = {
3228 {"aboveleft", 3, FALSE},
3229 {"belowright", 3, FALSE},
3230 {"botright", 2, FALSE},
3231 {"browse", 3, FALSE},
3232 {"confirm", 4, FALSE},
Bram Moolenaar7b668e82016-08-23 23:51:21 +02003233 {"filter", 4, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003234 {"hide", 3, FALSE},
3235 {"keepalt", 5, FALSE},
3236 {"keepjumps", 5, FALSE},
3237 {"keepmarks", 3, FALSE},
Bram Moolenaara939e432013-11-09 05:30:26 +01003238 {"keeppatterns", 5, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003239 {"leftabove", 5, FALSE},
3240 {"lockmarks", 3, FALSE},
Bram Moolenaarca9f9582008-09-18 10:44:28 +00003241 {"noautocmd", 3, FALSE},
Bram Moolenaar5803ae62014-03-23 16:04:02 +01003242 {"noswapfile", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003243 {"rightbelow", 6, FALSE},
3244 {"sandbox", 3, FALSE},
3245 {"silent", 3, FALSE},
3246 {"tab", 3, TRUE},
3247 {"topleft", 2, FALSE},
Bram Moolenaar8e258a42009-07-09 13:55:43 +00003248 {"unsilent", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003249 {"verbose", 4, TRUE},
3250 {"vertical", 4, FALSE},
3251};
3252
3253/*
3254 * Return length of a command modifier (including optional count).
3255 * Return zero when it's not a modifier.
3256 */
3257 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003258modifier_len(char_u *cmd)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003259{
3260 int i, j;
3261 char_u *p = cmd;
3262
3263 if (VIM_ISDIGIT(*cmd))
3264 p = skipwhite(skipdigits(cmd));
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00003265 for (i = 0; i < (int)(sizeof(cmdmods) / sizeof(struct cmdmod)); ++i)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003266 {
3267 for (j = 0; p[j] != NUL; ++j)
3268 if (p[j] != cmdmods[i].name[j])
3269 break;
Bram Moolenaar2d473ab2013-06-12 17:12:24 +02003270 if (!ASCII_ISALPHA(p[j]) && j >= cmdmods[i].minlen
Bram Moolenaared53fb92007-11-24 20:50:24 +00003271 && (p == cmd || cmdmods[i].has_count))
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00003272 return j + (int)(p - cmd);
Bram Moolenaared53fb92007-11-24 20:50:24 +00003273 }
3274 return 0;
3275}
3276
Bram Moolenaar071d4272004-06-13 20:20:40 +00003277/*
3278 * Return > 0 if an Ex command "name" exists.
3279 * Return 2 if there is an exact match.
3280 * Return 3 if there is an ambiguous match.
3281 */
3282 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003283cmd_exists(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003284{
3285 exarg_T ea;
3286 int full = FALSE;
3287 int i;
3288 int j;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003289 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003290
3291 /* Check command modifiers. */
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00003292 for (i = 0; i < (int)(sizeof(cmdmods) / sizeof(struct cmdmod)); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003293 {
3294 for (j = 0; name[j] != NUL; ++j)
3295 if (name[j] != cmdmods[i].name[j])
3296 break;
3297 if (name[j] == NUL && j >= cmdmods[i].minlen)
3298 return (cmdmods[i].name[j] == NUL ? 2 : 1);
3299 }
3300
Bram Moolenaara9587612006-05-04 21:47:50 +00003301 /* Check built-in commands and user defined commands.
3302 * For ":2match" and ":3match" we need to skip the number. */
3303 ea.cmd = (*name == '2' || *name == '3') ? name + 1 : name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003304 ea.cmdidx = (cmdidx_T)0;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003305 p = find_command(&ea, &full);
3306 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003307 return 3;
Bram Moolenaara9587612006-05-04 21:47:50 +00003308 if (vim_isdigit(*name) && ea.cmdidx != CMD_match)
3309 return 0;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003310 if (*skipwhite(p) != NUL)
3311 return 0; /* trailing garbage */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003312 return (ea.cmdidx == CMD_SIZE ? 0 : (full ? 2 : 1));
3313}
3314#endif
3315
3316/*
3317 * This is all pretty much copied from do_one_cmd(), with all the extra stuff
3318 * we don't need/want deleted. Maybe this could be done better if we didn't
3319 * repeat all this stuff. The only problem is that they may not stay
3320 * perfectly compatible with each other, but then the command line syntax
3321 * probably won't change that much -- webb.
3322 */
3323 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003324set_one_cmd_context(
3325 expand_T *xp,
3326 char_u *buff) /* buffer for command string */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003327{
3328 char_u *p;
3329 char_u *cmd, *arg;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003330 int len = 0;
3331 exarg_T ea;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003332#ifdef FEAT_CMDL_COMPL
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003333 int compl = EXPAND_NOTHING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003334 int delim;
3335#endif
3336 int forceit = FALSE;
3337 int usefilter = FALSE; /* filter instead of file name */
3338
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003339 ExpandInit(xp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003340 xp->xp_pattern = buff;
3341 xp->xp_context = EXPAND_COMMANDS; /* Default until we get past command */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003342 ea.argt = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003343
3344/*
Bram Moolenaar1c40a662014-11-27 16:38:11 +01003345 * 1. skip comment lines and leading space, colons or bars
Bram Moolenaar071d4272004-06-13 20:20:40 +00003346 */
3347 for (cmd = buff; vim_strchr((char_u *)" \t:|", *cmd) != NULL; cmd++)
3348 ;
3349 xp->xp_pattern = cmd;
3350
3351 if (*cmd == NUL)
3352 return NULL;
3353 if (*cmd == '"') /* ignore comment lines */
3354 {
3355 xp->xp_context = EXPAND_NOTHING;
3356 return NULL;
3357 }
3358
3359/*
Bram Moolenaar1c40a662014-11-27 16:38:11 +01003360 * 3. Skip over the range to find the command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003361 */
3362 cmd = skip_range(cmd, &xp->xp_context);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003363 xp->xp_pattern = cmd;
3364 if (*cmd == NUL)
3365 return NULL;
3366 if (*cmd == '"')
3367 {
3368 xp->xp_context = EXPAND_NOTHING;
3369 return NULL;
3370 }
3371
3372 if (*cmd == '|' || *cmd == '\n')
3373 return cmd + 1; /* There's another command */
3374
3375 /*
3376 * Isolate the command and search for it in the command table.
3377 * Exceptions:
3378 * - the 'k' command can directly be followed by any character, but
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003379 * do accept "keepmarks", "keepalt" and "keepjumps".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003380 * - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
3381 */
3382 if (*cmd == 'k' && cmd[1] != 'e')
3383 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003384 ea.cmdidx = CMD_k;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003385 p = cmd + 1;
3386 }
3387 else
3388 {
3389 p = cmd;
3390 while (ASCII_ISALPHA(*p) || *p == '*') /* Allow * wild card */
3391 ++p;
Bram Moolenaar23d1b622015-10-13 19:18:04 +02003392 /* a user command may contain digits */
3393 if (ASCII_ISUPPER(cmd[0]))
3394 while (ASCII_ISALNUM(*p) || *p == '*')
3395 ++p;
Bram Moolenaar9f5d6002013-06-02 19:22:13 +02003396 /* for python 3.x: ":py3*" commands completion */
3397 if (cmd[0] == 'p' && cmd[1] == 'y' && p == cmd + 2 && *p == '3')
Bram Moolenaar893b2d72013-12-11 17:44:38 +01003398 {
Bram Moolenaar9f5d6002013-06-02 19:22:13 +02003399 ++p;
Bram Moolenaar893b2d72013-12-11 17:44:38 +01003400 while (ASCII_ISALPHA(*p) || *p == '*')
3401 ++p;
3402 }
Bram Moolenaar23d1b622015-10-13 19:18:04 +02003403 /* check for non-alpha command */
3404 if (p == cmd && vim_strchr((char_u *)"@*!=><&~#", *p) != NULL)
3405 ++p;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003406 len = (int)(p - cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003407
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003408 if (len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003409 {
3410 xp->xp_context = EXPAND_UNSUCCESSFUL;
3411 return NULL;
3412 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003413 for (ea.cmdidx = (cmdidx_T)0; (int)ea.cmdidx < (int)CMD_SIZE;
Bram Moolenaar42b4dda2010-03-02 15:56:05 +01003414 ea.cmdidx = (cmdidx_T)((int)ea.cmdidx + 1))
3415 if (STRNCMP(cmdnames[(int)ea.cmdidx].cmd_name, cmd,
3416 (size_t)len) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003417 break;
3418
Bram Moolenaar071d4272004-06-13 20:20:40 +00003419 if (cmd[0] >= 'A' && cmd[0] <= 'Z')
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003420 while (ASCII_ISALNUM(*p) || *p == '*') // Allow * wild card
Bram Moolenaar071d4272004-06-13 20:20:40 +00003421 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003422 }
3423
3424 /*
3425 * If the cursor is touching the command, and it ends in an alpha-numeric
3426 * character, complete the command name.
3427 */
3428 if (*p == NUL && ASCII_ISALNUM(p[-1]))
3429 return NULL;
3430
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003431 if (ea.cmdidx == CMD_SIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003432 {
3433 if (*cmd == 's' && vim_strchr((char_u *)"cgriI", cmd[1]) != NULL)
3434 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003435 ea.cmdidx = CMD_substitute;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003436 p = cmd + 1;
3437 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003438 else if (cmd[0] >= 'A' && cmd[0] <= 'Z')
3439 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003440 ea.cmd = cmd;
3441 p = find_ucmd(&ea, p, NULL, xp,
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003442#if defined(FEAT_CMDL_COMPL)
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003443 &compl
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003444#else
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003445 NULL
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003446#endif
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003447 );
Bram Moolenaarebefac62005-12-28 22:39:57 +00003448 if (p == NULL)
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003449 ea.cmdidx = CMD_SIZE; // ambiguous user command
Bram Moolenaar071d4272004-06-13 20:20:40 +00003450 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003451 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003452 if (ea.cmdidx == CMD_SIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003453 {
3454 /* Not still touching the command and it was an illegal one */
3455 xp->xp_context = EXPAND_UNSUCCESSFUL;
3456 return NULL;
3457 }
3458
3459 xp->xp_context = EXPAND_NOTHING; /* Default now that we're past command */
3460
3461 if (*p == '!') /* forced commands */
3462 {
3463 forceit = TRUE;
3464 ++p;
3465 }
3466
3467/*
Bram Moolenaar1c40a662014-11-27 16:38:11 +01003468 * 6. parse arguments
Bram Moolenaar071d4272004-06-13 20:20:40 +00003469 */
Bram Moolenaar958636c2014-10-21 20:01:58 +02003470 if (!IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003471 ea.argt = (long)cmdnames[(int)ea.cmdidx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003472
3473 arg = skipwhite(p);
3474
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003475 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003476 {
3477 if (*arg == '>') /* append */
3478 {
3479 if (*++arg == '>')
3480 ++arg;
3481 arg = skipwhite(arg);
3482 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003483 else if (*arg == '!' && ea.cmdidx == CMD_write) /* :w !filter */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003484 {
3485 ++arg;
3486 usefilter = TRUE;
3487 }
3488 }
3489
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003490 if (ea.cmdidx == CMD_read)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003491 {
3492 usefilter = forceit; /* :r! filter if forced */
3493 if (*arg == '!') /* :r !filter */
3494 {
3495 ++arg;
3496 usefilter = TRUE;
3497 }
3498 }
3499
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003500 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003501 {
3502 while (*arg == *cmd) /* allow any number of '>' or '<' */
3503 ++arg;
3504 arg = skipwhite(arg);
3505 }
3506
3507 /* Does command allow "+command"? */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003508 if ((ea.argt & EDITCMD) && !usefilter && *arg == '+')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003509 {
3510 /* Check if we're in the +command */
3511 p = arg + 1;
3512 arg = skip_cmd_arg(arg, FALSE);
3513
3514 /* Still touching the command after '+'? */
3515 if (*arg == NUL)
3516 return p;
3517
3518 /* Skip space(s) after +command to get to the real argument */
3519 arg = skipwhite(arg);
3520 }
3521
3522 /*
3523 * Check for '|' to separate commands and '"' to start comments.
3524 * Don't do this for ":read !cmd" and ":write !cmd".
3525 */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003526 if ((ea.argt & TRLBAR) && !usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003527 {
3528 p = arg;
3529 /* ":redir @" is not the start of a comment */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003530 if (ea.cmdidx == CMD_redir && p[0] == '@' && p[1] == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003531 p += 2;
3532 while (*p)
3533 {
3534 if (*p == Ctrl_V)
3535 {
3536 if (p[1] != NUL)
3537 ++p;
3538 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003539 else if ( (*p == '"' && !(ea.argt & NOTRLCOM))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003540 || *p == '|' || *p == '\n')
3541 {
3542 if (*(p - 1) != '\\')
3543 {
3544 if (*p == '|' || *p == '\n')
3545 return p + 1;
3546 return NULL; /* It's a comment */
3547 }
3548 }
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003549 MB_PTR_ADV(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003550 }
3551 }
3552
3553 /* no arguments allowed */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003554 if (!(ea.argt & EXTRA) && *arg != NUL &&
Bram Moolenaar071d4272004-06-13 20:20:40 +00003555 vim_strchr((char_u *)"|\"", *arg) == NULL)
3556 return NULL;
3557
3558 /* Find start of last argument (argument just before cursor): */
Bram Moolenaar848f8762012-07-25 17:22:23 +02003559 p = buff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003560 xp->xp_pattern = p;
Bram Moolenaar09168a72012-08-02 21:24:42 +02003561 len = (int)STRLEN(buff);
Bram Moolenaar848f8762012-07-25 17:22:23 +02003562 while (*p && p < buff + len)
3563 {
3564 if (*p == ' ' || *p == TAB)
3565 {
3566 /* argument starts after a space */
3567 xp->xp_pattern = ++p;
3568 }
3569 else
3570 {
3571 if (*p == '\\' && *(p + 1) != NUL)
3572 ++p; /* skip over escaped character */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003573 MB_PTR_ADV(p);
Bram Moolenaar848f8762012-07-25 17:22:23 +02003574 }
3575 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003576
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003577 if (ea.argt & XFILE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003578 {
Bram Moolenaar6529c102007-08-18 15:47:34 +00003579 int c;
3580 int in_quote = FALSE;
3581 char_u *bow = NULL; /* Beginning of word */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003582
3583 /*
3584 * Allow spaces within back-quotes to count as part of the argument
3585 * being expanded.
3586 */
3587 xp->xp_pattern = skipwhite(arg);
Bram Moolenaar6529c102007-08-18 15:47:34 +00003588 p = xp->xp_pattern;
3589 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003590 {
Bram Moolenaar6529c102007-08-18 15:47:34 +00003591 if (has_mbyte)
3592 c = mb_ptr2char(p);
3593 else
Bram Moolenaar6529c102007-08-18 15:47:34 +00003594 c = *p;
3595 if (c == '\\' && p[1] != NUL)
3596 ++p;
3597 else if (c == '`')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003598 {
3599 if (!in_quote)
3600 {
3601 xp->xp_pattern = p;
3602 bow = p + 1;
3603 }
3604 in_quote = !in_quote;
3605 }
Bram Moolenaar332fa0c2008-01-13 16:12:10 +00003606 /* An argument can contain just about everything, except
3607 * characters that end the command and white space. */
Bram Moolenaar1c465442017-03-12 20:10:05 +01003608 else if (c == '|' || c == '\n' || c == '"' || (VIM_ISWHITE(c)
Bram Moolenaar6529c102007-08-18 15:47:34 +00003609#ifdef SPACE_IN_FILENAME
Bram Moolenaar332fa0c2008-01-13 16:12:10 +00003610 && (!(ea.argt & NOSPC) || usefilter)
Bram Moolenaar6529c102007-08-18 15:47:34 +00003611#endif
Bram Moolenaar332fa0c2008-01-13 16:12:10 +00003612 ))
Bram Moolenaar6529c102007-08-18 15:47:34 +00003613 {
Bram Moolenaarcf5a5b82008-02-26 20:30:12 +00003614 len = 0; /* avoid getting stuck when space is in 'isfname' */
Bram Moolenaar6529c102007-08-18 15:47:34 +00003615 while (*p != NUL)
3616 {
Bram Moolenaar6529c102007-08-18 15:47:34 +00003617 if (has_mbyte)
3618 c = mb_ptr2char(p);
3619 else
Bram Moolenaar6529c102007-08-18 15:47:34 +00003620 c = *p;
Bram Moolenaardd87969c2007-08-21 13:07:12 +00003621 if (c == '`' || vim_isfilec_or_wc(c))
Bram Moolenaar6529c102007-08-18 15:47:34 +00003622 break;
Bram Moolenaar6529c102007-08-18 15:47:34 +00003623 if (has_mbyte)
3624 len = (*mb_ptr2len)(p);
3625 else
Bram Moolenaar6529c102007-08-18 15:47:34 +00003626 len = 1;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003627 MB_PTR_ADV(p);
Bram Moolenaar6529c102007-08-18 15:47:34 +00003628 }
3629 if (in_quote)
3630 bow = p;
3631 else
3632 xp->xp_pattern = p;
3633 p -= len;
3634 }
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003635 MB_PTR_ADV(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003636 }
3637
3638 /*
3639 * If we are still inside the quotes, and we passed a space, just
3640 * expand from there.
3641 */
3642 if (bow != NULL && in_quote)
3643 xp->xp_pattern = bow;
3644 xp->xp_context = EXPAND_FILES;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003645
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003646 /* For a shell command more chars need to be escaped. */
Bram Moolenaar67883b42017-07-27 22:57:00 +02003647 if (usefilter || ea.cmdidx == CMD_bang || ea.cmdidx == CMD_terminal)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003648 {
Bram Moolenaarc0cba4d2010-08-07 17:07:21 +02003649#ifndef BACKSLASH_IN_FILENAME
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003650 xp->xp_shell = TRUE;
Bram Moolenaarc0cba4d2010-08-07 17:07:21 +02003651#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003652 /* When still after the command name expand executables. */
3653 if (xp->xp_pattern == skipwhite(arg))
Bram Moolenaar5c5b0942007-05-06 12:07:59 +00003654 xp->xp_context = EXPAND_SHELLCMD;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003655 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003656
3657 /* Check for environment variable */
3658 if (*xp->xp_pattern == '$'
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003659#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003660 || *xp->xp_pattern == '%'
3661#endif
3662 )
3663 {
3664 for (p = xp->xp_pattern + 1; *p != NUL; ++p)
3665 if (!vim_isIDc(*p))
3666 break;
3667 if (*p == NUL)
3668 {
3669 xp->xp_context = EXPAND_ENV_VARS;
3670 ++xp->xp_pattern;
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003671#if defined(FEAT_CMDL_COMPL)
Bram Moolenaar21cf8232004-07-16 20:18:37 +00003672 /* Avoid that the assignment uses EXPAND_FILES again. */
Bram Moolenaara466c992005-07-09 21:03:22 +00003673 if (compl != EXPAND_USER_DEFINED && compl != EXPAND_USER_LIST)
Bram Moolenaar21cf8232004-07-16 20:18:37 +00003674 compl = EXPAND_ENV_VARS;
3675#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003676 }
3677 }
Bram Moolenaar24305862012-08-15 14:05:05 +02003678#if defined(FEAT_CMDL_COMPL)
3679 /* Check for user names */
3680 if (*xp->xp_pattern == '~')
3681 {
3682 for (p = xp->xp_pattern + 1; *p != NUL && *p != '/'; ++p)
3683 ;
3684 /* Complete ~user only if it partially matches a user name.
3685 * A full match ~user<Tab> will be replaced by user's home
3686 * directory i.e. something like ~user<Tab> -> /home/user/ */
3687 if (*p == NUL && p > xp->xp_pattern + 1
Bram Moolenaar6c5d1042018-07-07 16:41:13 +02003688 && match_user(xp->xp_pattern + 1) >= 1)
Bram Moolenaar24305862012-08-15 14:05:05 +02003689 {
3690 xp->xp_context = EXPAND_USER;
3691 ++xp->xp_pattern;
3692 }
3693 }
3694#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003695 }
3696
3697/*
Bram Moolenaar1c40a662014-11-27 16:38:11 +01003698 * 6. Switch on command name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003699 */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003700 switch (ea.cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003701 {
Bram Moolenaarcc448b32010-07-14 16:52:17 +02003702 case CMD_find:
3703 case CMD_sfind:
3704 case CMD_tabfind:
Bram Moolenaarc24b6972010-08-16 22:34:29 +02003705 if (xp->xp_context == EXPAND_FILES)
3706 xp->xp_context = EXPAND_FILES_IN_PATH;
Bram Moolenaarcc448b32010-07-14 16:52:17 +02003707 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003708 case CMD_cd:
3709 case CMD_chdir:
Bram Moolenaar00aa0692019-04-27 20:37:57 +02003710 case CMD_tcd:
3711 case CMD_tchdir:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003712 case CMD_lcd:
3713 case CMD_lchdir:
3714 if (xp->xp_context == EXPAND_FILES)
3715 xp->xp_context = EXPAND_DIRECTORIES;
3716 break;
3717 case CMD_help:
3718 xp->xp_context = EXPAND_HELP;
3719 xp->xp_pattern = arg;
3720 break;
3721
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003722 /* Command modifiers: return the argument.
3723 * Also for commands with an argument that is a command. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003724 case CMD_aboveleft:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003725 case CMD_argdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003726 case CMD_belowright:
3727 case CMD_botright:
3728 case CMD_browse:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003729 case CMD_bufdo:
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003730 case CMD_cdo:
3731 case CMD_cfdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003732 case CMD_confirm:
Bram Moolenaardf177f62005-02-22 08:39:57 +00003733 case CMD_debug:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003734 case CMD_folddoclosed:
3735 case CMD_folddoopen:
3736 case CMD_hide:
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003737 case CMD_keepalt:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003738 case CMD_keepjumps:
3739 case CMD_keepmarks:
Bram Moolenaara939e432013-11-09 05:30:26 +01003740 case CMD_keeppatterns:
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003741 case CMD_ldo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003742 case CMD_leftabove:
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003743 case CMD_lfdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003744 case CMD_lockmarks:
Bram Moolenaar5803ae62014-03-23 16:04:02 +01003745 case CMD_noautocmd:
3746 case CMD_noswapfile:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003747 case CMD_rightbelow:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003748 case CMD_sandbox:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003749 case CMD_silent:
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003750 case CMD_tab:
Bram Moolenaar70baa402013-06-16 16:14:03 +02003751 case CMD_tabdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003752 case CMD_topleft:
3753 case CMD_verbose:
3754 case CMD_vertical:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003755 case CMD_windo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003756 return arg;
3757
Bram Moolenaar7069bf12017-01-07 20:39:53 +01003758 case CMD_filter:
3759 if (*arg != NUL)
3760 arg = skip_vimgrep_pat(arg, NULL, NULL);
3761 if (arg == NULL || *arg == NUL)
3762 {
3763 xp->xp_context = EXPAND_NOTHING;
3764 return NULL;
3765 }
3766 return skipwhite(arg);
3767
Bram Moolenaar4f688582007-07-24 12:34:30 +00003768#ifdef FEAT_CMDL_COMPL
3769# ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar071d4272004-06-13 20:20:40 +00003770 case CMD_match:
3771 if (*arg == NUL || !ends_excmd(*arg))
3772 {
Bram Moolenaar4f688582007-07-24 12:34:30 +00003773 /* also complete "None" */
3774 set_context_in_echohl_cmd(xp, arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003775 arg = skipwhite(skiptowhite(arg));
3776 if (*arg != NUL)
3777 {
3778 xp->xp_context = EXPAND_NOTHING;
3779 arg = skip_regexp(arg + 1, *arg, p_magic, NULL);
3780 }
3781 }
3782 return find_nextcmd(arg);
Bram Moolenaar4f688582007-07-24 12:34:30 +00003783# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003784
Bram Moolenaar071d4272004-06-13 20:20:40 +00003785/*
3786 * All completion for the +cmdline_compl feature goes here.
3787 */
3788
Bram Moolenaar071d4272004-06-13 20:20:40 +00003789 case CMD_command:
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003790 return set_context_in_user_cmd(xp, arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003791
3792 case CMD_delcommand:
3793 xp->xp_context = EXPAND_USER_COMMANDS;
3794 xp->xp_pattern = arg;
3795 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003796
3797 case CMD_global:
3798 case CMD_vglobal:
3799 delim = *arg; /* get the delimiter */
3800 if (delim)
3801 ++arg; /* skip delimiter if there is one */
3802
3803 while (arg[0] != NUL && arg[0] != delim)
3804 {
3805 if (arg[0] == '\\' && arg[1] != NUL)
3806 ++arg;
3807 ++arg;
3808 }
3809 if (arg[0] != NUL)
3810 return arg + 1;
3811 break;
3812 case CMD_and:
3813 case CMD_substitute:
3814 delim = *arg;
3815 if (delim)
3816 {
3817 /* skip "from" part */
3818 ++arg;
3819 arg = skip_regexp(arg, delim, p_magic, NULL);
3820 }
3821 /* skip "to" part */
3822 while (arg[0] != NUL && arg[0] != delim)
3823 {
3824 if (arg[0] == '\\' && arg[1] != NUL)
3825 ++arg;
3826 ++arg;
3827 }
3828 if (arg[0] != NUL) /* skip delimiter */
3829 ++arg;
3830 while (arg[0] && vim_strchr((char_u *)"|\"#", arg[0]) == NULL)
3831 ++arg;
3832 if (arg[0] != NUL)
3833 return arg;
3834 break;
3835 case CMD_isearch:
3836 case CMD_dsearch:
3837 case CMD_ilist:
3838 case CMD_dlist:
3839 case CMD_ijump:
3840 case CMD_psearch:
3841 case CMD_djump:
3842 case CMD_isplit:
3843 case CMD_dsplit:
3844 arg = skipwhite(skipdigits(arg)); /* skip count */
3845 if (*arg == '/') /* Match regexp, not just whole words */
3846 {
3847 for (++arg; *arg && *arg != '/'; arg++)
3848 if (*arg == '\\' && arg[1] != NUL)
3849 arg++;
3850 if (*arg)
3851 {
3852 arg = skipwhite(arg + 1);
3853
3854 /* Check for trailing illegal characters */
3855 if (*arg && vim_strchr((char_u *)"|\"\n", *arg) == NULL)
3856 xp->xp_context = EXPAND_NOTHING;
3857 else
3858 return arg;
3859 }
3860 }
3861 break;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003862
Bram Moolenaar071d4272004-06-13 20:20:40 +00003863 case CMD_autocmd:
3864 return set_context_in_autocmd(xp, arg, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003865 case CMD_doautocmd:
Bram Moolenaar73a9d7b2008-11-06 16:16:44 +00003866 case CMD_doautoall:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003867 return set_context_in_autocmd(xp, arg, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003868 case CMD_set:
3869 set_context_in_set_cmd(xp, arg, 0);
3870 break;
3871 case CMD_setglobal:
3872 set_context_in_set_cmd(xp, arg, OPT_GLOBAL);
3873 break;
3874 case CMD_setlocal:
3875 set_context_in_set_cmd(xp, arg, OPT_LOCAL);
3876 break;
3877 case CMD_tag:
3878 case CMD_stag:
3879 case CMD_ptag:
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00003880 case CMD_ltag:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003881 case CMD_tselect:
3882 case CMD_stselect:
3883 case CMD_ptselect:
3884 case CMD_tjump:
3885 case CMD_stjump:
3886 case CMD_ptjump:
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00003887 if (*p_wop != NUL)
3888 xp->xp_context = EXPAND_TAGS_LISTFILES;
3889 else
3890 xp->xp_context = EXPAND_TAGS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003891 xp->xp_pattern = arg;
3892 break;
3893 case CMD_augroup:
3894 xp->xp_context = EXPAND_AUGROUP;
3895 xp->xp_pattern = arg;
3896 break;
3897#ifdef FEAT_SYN_HL
3898 case CMD_syntax:
3899 set_context_in_syntax_cmd(xp, arg);
3900 break;
3901#endif
3902#ifdef FEAT_EVAL
3903 case CMD_let:
3904 case CMD_if:
3905 case CMD_elseif:
3906 case CMD_while:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00003907 case CMD_for:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003908 case CMD_echo:
3909 case CMD_echon:
3910 case CMD_execute:
3911 case CMD_echomsg:
3912 case CMD_echoerr:
3913 case CMD_call:
3914 case CMD_return:
Bram Moolenaar2b2207b2017-01-22 16:46:56 +01003915 case CMD_cexpr:
3916 case CMD_caddexpr:
3917 case CMD_cgetexpr:
3918 case CMD_lexpr:
3919 case CMD_laddexpr:
3920 case CMD_lgetexpr:
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003921 set_context_for_expression(xp, arg, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003922 break;
3923
3924 case CMD_unlet:
3925 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
3926 arg = xp->xp_pattern + 1;
Bram Moolenaar19834012018-06-12 17:03:39 +02003927
Bram Moolenaar071d4272004-06-13 20:20:40 +00003928 xp->xp_context = EXPAND_USER_VARS;
3929 xp->xp_pattern = arg;
Bram Moolenaar19834012018-06-12 17:03:39 +02003930
3931 if (*xp->xp_pattern == '$')
3932 {
3933 xp->xp_context = EXPAND_ENV_VARS;
3934 ++xp->xp_pattern;
3935 }
3936
Bram Moolenaar071d4272004-06-13 20:20:40 +00003937 break;
3938
3939 case CMD_function:
3940 case CMD_delfunction:
3941 xp->xp_context = EXPAND_USER_FUNC;
3942 xp->xp_pattern = arg;
3943 break;
3944
3945 case CMD_echohl:
Bram Moolenaar4f688582007-07-24 12:34:30 +00003946 set_context_in_echohl_cmd(xp, arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003947 break;
3948#endif
3949 case CMD_highlight:
3950 set_context_in_highlight_cmd(xp, arg);
3951 break;
Bram Moolenaarf4580d82009-03-18 11:52:53 +00003952#ifdef FEAT_CSCOPE
3953 case CMD_cscope:
Bram Moolenaar7bfef802009-04-22 14:25:01 +00003954 case CMD_lcscope:
3955 case CMD_scscope:
3956 set_context_in_cscope_cmd(xp, arg, ea.cmdidx);
Bram Moolenaarf4580d82009-03-18 11:52:53 +00003957 break;
3958#endif
Bram Moolenaar3c65e312009-04-29 16:47:23 +00003959#ifdef FEAT_SIGNS
3960 case CMD_sign:
3961 set_context_in_sign_cmd(xp, arg);
3962 break;
3963#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003964 case CMD_bdelete:
3965 case CMD_bwipeout:
3966 case CMD_bunload:
3967 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
3968 arg = xp->xp_pattern + 1;
Bram Moolenaar2f40d122017-10-24 21:49:36 +02003969 /* FALLTHROUGH */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003970 case CMD_buffer:
3971 case CMD_sbuffer:
3972 case CMD_checktime:
3973 xp->xp_context = EXPAND_BUFFERS;
3974 xp->xp_pattern = arg;
3975 break;
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003976
Bram Moolenaar071d4272004-06-13 20:20:40 +00003977 case CMD_USER:
3978 case CMD_USER_BUF:
3979 if (compl != EXPAND_NOTHING)
3980 {
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003981 // XFILE: file names are handled above
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003982 if (!(ea.argt & XFILE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003983 {
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003984#ifdef FEAT_MENU
Bram Moolenaar071d4272004-06-13 20:20:40 +00003985 if (compl == EXPAND_MENUS)
3986 return set_context_in_menu_cmd(xp, cmd, arg, forceit);
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003987#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003988 if (compl == EXPAND_COMMANDS)
3989 return arg;
3990 if (compl == EXPAND_MAPPINGS)
3991 return set_context_in_map_cmd(xp, (char_u *)"map",
3992 arg, forceit, FALSE, FALSE, CMD_map);
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003993 // Find start of last argument.
Bram Moolenaar848f8762012-07-25 17:22:23 +02003994 p = arg;
3995 while (*p)
3996 {
3997 if (*p == ' ')
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003998 // argument starts after a space
Bram Moolenaar848f8762012-07-25 17:22:23 +02003999 arg = p + 1;
Bram Moolenaara07c8312012-07-27 21:12:07 +02004000 else if (*p == '\\' && *(p + 1) != NUL)
Bram Moolenaarac9fb182019-04-27 13:04:13 +02004001 ++p; // skip over escaped character
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004002 MB_PTR_ADV(p);
Bram Moolenaar848f8762012-07-25 17:22:23 +02004003 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004004 xp->xp_pattern = arg;
4005 }
4006 xp->xp_context = compl;
4007 }
4008 break;
Bram Moolenaarac9fb182019-04-27 13:04:13 +02004009
Bram Moolenaar071d4272004-06-13 20:20:40 +00004010 case CMD_map: case CMD_noremap:
4011 case CMD_nmap: case CMD_nnoremap:
4012 case CMD_vmap: case CMD_vnoremap:
4013 case CMD_omap: case CMD_onoremap:
4014 case CMD_imap: case CMD_inoremap:
4015 case CMD_cmap: case CMD_cnoremap:
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004016 case CMD_lmap: case CMD_lnoremap:
Bram Moolenaar09bb33d2013-05-07 05:18:20 +02004017 case CMD_smap: case CMD_snoremap:
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +02004018 case CMD_tmap: case CMD_tnoremap:
Bram Moolenaar09bb33d2013-05-07 05:18:20 +02004019 case CMD_xmap: case CMD_xnoremap:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004020 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004021 FALSE, FALSE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004022 case CMD_unmap:
4023 case CMD_nunmap:
4024 case CMD_vunmap:
4025 case CMD_ounmap:
4026 case CMD_iunmap:
4027 case CMD_cunmap:
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004028 case CMD_lunmap:
Bram Moolenaar09bb33d2013-05-07 05:18:20 +02004029 case CMD_sunmap:
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +02004030 case CMD_tunmap:
Bram Moolenaar09bb33d2013-05-07 05:18:20 +02004031 case CMD_xunmap:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004032 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004033 FALSE, TRUE, ea.cmdidx);
Bram Moolenaarcae92dc2017-08-06 15:22:15 +02004034 case CMD_mapclear:
4035 case CMD_nmapclear:
4036 case CMD_vmapclear:
4037 case CMD_omapclear:
4038 case CMD_imapclear:
4039 case CMD_cmapclear:
4040 case CMD_lmapclear:
4041 case CMD_smapclear:
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +02004042 case CMD_tmapclear:
Bram Moolenaarcae92dc2017-08-06 15:22:15 +02004043 case CMD_xmapclear:
4044 xp->xp_context = EXPAND_MAPCLEAR;
4045 xp->xp_pattern = arg;
4046 break;
4047
Bram Moolenaar071d4272004-06-13 20:20:40 +00004048 case CMD_abbreviate: case CMD_noreabbrev:
4049 case CMD_cabbrev: case CMD_cnoreabbrev:
4050 case CMD_iabbrev: case CMD_inoreabbrev:
4051 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004052 TRUE, FALSE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004053 case CMD_unabbreviate:
4054 case CMD_cunabbrev:
4055 case CMD_iunabbrev:
4056 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004057 TRUE, TRUE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004058#ifdef FEAT_MENU
4059 case CMD_menu: case CMD_noremenu: case CMD_unmenu:
4060 case CMD_amenu: case CMD_anoremenu: case CMD_aunmenu:
4061 case CMD_nmenu: case CMD_nnoremenu: case CMD_nunmenu:
4062 case CMD_vmenu: case CMD_vnoremenu: case CMD_vunmenu:
4063 case CMD_omenu: case CMD_onoremenu: case CMD_ounmenu:
4064 case CMD_imenu: case CMD_inoremenu: case CMD_iunmenu:
4065 case CMD_cmenu: case CMD_cnoremenu: case CMD_cunmenu:
Bram Moolenaar4c5d8152018-10-19 22:36:53 +02004066 case CMD_tlmenu: case CMD_tlnoremenu: case CMD_tlunmenu:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004067 case CMD_tmenu: case CMD_tunmenu:
4068 case CMD_popup: case CMD_tearoff: case CMD_emenu:
4069 return set_context_in_menu_cmd(xp, cmd, arg, forceit);
4070#endif
4071
4072 case CMD_colorscheme:
4073 xp->xp_context = EXPAND_COLORS;
4074 xp->xp_pattern = arg;
4075 break;
4076
4077 case CMD_compiler:
4078 xp->xp_context = EXPAND_COMPILER;
4079 xp->xp_pattern = arg;
4080 break;
4081
Bram Moolenaar883f5d02010-06-21 06:24:34 +02004082 case CMD_ownsyntax:
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02004083 xp->xp_context = EXPAND_OWNSYNTAX;
4084 xp->xp_pattern = arg;
4085 break;
4086
4087 case CMD_setfiletype:
Bram Moolenaar883f5d02010-06-21 06:24:34 +02004088 xp->xp_context = EXPAND_FILETYPE;
4089 xp->xp_pattern = arg;
4090 break;
4091
Bram Moolenaar35ca0e72016-03-05 17:41:49 +01004092 case CMD_packadd:
4093 xp->xp_context = EXPAND_PACKADD;
4094 xp->xp_pattern = arg;
4095 break;
4096
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01004097#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004098 case CMD_language:
Bram Moolenaara660dc82011-05-25 12:51:22 +02004099 p = skiptowhite(arg);
4100 if (*p == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004101 {
4102 xp->xp_context = EXPAND_LANGUAGE;
4103 xp->xp_pattern = arg;
4104 }
4105 else
Bram Moolenaara660dc82011-05-25 12:51:22 +02004106 {
4107 if ( STRNCMP(arg, "messages", p - arg) == 0
4108 || STRNCMP(arg, "ctype", p - arg) == 0
4109 || STRNCMP(arg, "time", p - arg) == 0)
4110 {
4111 xp->xp_context = EXPAND_LOCALES;
4112 xp->xp_pattern = skipwhite(p);
4113 }
4114 else
4115 xp->xp_context = EXPAND_NOTHING;
4116 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004117 break;
4118#endif
Bram Moolenaarf86f26c2010-02-03 15:14:22 +01004119#if defined(FEAT_PROFILE)
4120 case CMD_profile:
4121 set_context_in_profile_cmd(xp, arg);
4122 break;
4123#endif
Bram Moolenaar42b4dda2010-03-02 15:56:05 +01004124 case CMD_behave:
4125 xp->xp_context = EXPAND_BEHAVE;
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02004126 xp->xp_pattern = arg;
Bram Moolenaar42b4dda2010-03-02 15:56:05 +01004127 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004128
Bram Moolenaar9e507ca2016-10-15 15:39:39 +02004129 case CMD_messages:
4130 xp->xp_context = EXPAND_MESSAGES;
4131 xp->xp_pattern = arg;
4132 break;
4133
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02004134#if defined(FEAT_CMDHIST)
4135 case CMD_history:
4136 xp->xp_context = EXPAND_HISTORY;
4137 xp->xp_pattern = arg;
4138 break;
4139#endif
Bram Moolenaarcd9c4622013-06-08 15:24:48 +02004140#if defined(FEAT_PROFILE)
4141 case CMD_syntime:
4142 xp->xp_context = EXPAND_SYNTIME;
4143 xp->xp_pattern = arg;
4144 break;
4145#endif
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02004146
Bram Moolenaarcd43eff2018-03-29 15:55:38 +02004147 case CMD_argdelete:
4148 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
4149 arg = xp->xp_pattern + 1;
4150 xp->xp_context = EXPAND_ARGLIST;
4151 xp->xp_pattern = arg;
4152 break;
4153
Bram Moolenaar071d4272004-06-13 20:20:40 +00004154#endif /* FEAT_CMDL_COMPL */
4155
4156 default:
4157 break;
4158 }
4159 return NULL;
4160}
4161
4162/*
Bram Moolenaaree8415b2018-08-10 23:13:12 +02004163 * Skip a range specifier of the form: addr [,addr] [;addr] ..
Bram Moolenaar071d4272004-06-13 20:20:40 +00004164 *
4165 * Backslashed delimiters after / or ? will be skipped, and commands will
4166 * not be expanded between /'s and ?'s or after "'".
4167 *
Bram Moolenaardf177f62005-02-22 08:39:57 +00004168 * Also skip white space and ":" characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004169 * Returns the "cmd" pointer advanced to beyond the range.
4170 */
4171 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004172skip_range(
4173 char_u *cmd,
4174 int *ctx) /* pointer to xp_context or NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004175{
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004176 unsigned delim;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004177
Bram Moolenaarcbf20fb2017-02-03 21:19:04 +01004178 while (vim_strchr((char_u *)" \t0123456789.$%'/?-+,;\\", *cmd) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004179 {
Bram Moolenaarcbf20fb2017-02-03 21:19:04 +01004180 if (*cmd == '\\')
4181 {
4182 if (cmd[1] == '?' || cmd[1] == '/' || cmd[1] == '&')
4183 ++cmd;
4184 else
4185 break;
4186 }
4187 else if (*cmd == '\'')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004188 {
4189 if (*++cmd == NUL && ctx != NULL)
4190 *ctx = EXPAND_NOTHING;
4191 }
4192 else if (*cmd == '/' || *cmd == '?')
4193 {
4194 delim = *cmd++;
4195 while (*cmd != NUL && *cmd != delim)
4196 if (*cmd++ == '\\' && *cmd != NUL)
4197 ++cmd;
4198 if (*cmd == NUL && ctx != NULL)
4199 *ctx = EXPAND_NOTHING;
4200 }
4201 if (*cmd != NUL)
4202 ++cmd;
4203 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00004204
4205 /* Skip ":" and white space. */
4206 while (*cmd == ':')
4207 cmd = skipwhite(cmd + 1);
4208
Bram Moolenaar071d4272004-06-13 20:20:40 +00004209 return cmd;
4210}
4211
4212/*
Bram Moolenaar198cb662018-09-06 21:44:17 +02004213 * Get a single EX address.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004214 *
4215 * Set ptr to the next character after the part that was interpreted.
4216 * Set ptr to NULL when an error is encountered.
Bram Moolenaar198cb662018-09-06 21:44:17 +02004217 * This may set the last used search pattern.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004218 *
4219 * Return MAXLNUM when no Ex address was found.
4220 */
4221 static linenr_T
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004222get_address(
4223 exarg_T *eap UNUSED,
4224 char_u **ptr,
Bram Moolenaarb7316892019-05-01 18:08:42 +02004225 cmd_addr_T addr_type_arg,
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02004226 int skip, // only skip the address, don't use it
4227 int silent, // no errors or side effects
4228 int to_other_file, // flag: may jump to other file
4229 int address_count UNUSED) // 1 for first address, >1 after comma
Bram Moolenaar071d4272004-06-13 20:20:40 +00004230{
Bram Moolenaarb7316892019-05-01 18:08:42 +02004231 cmd_addr_T addr_type = addr_type_arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004232 int c;
4233 int i;
4234 long n;
4235 char_u *cmd;
4236 pos_T pos;
4237 pos_T *fp;
4238 linenr_T lnum;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004239 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004240
4241 cmd = skipwhite(*ptr);
4242 lnum = MAXLNUM;
4243 do
4244 {
4245 switch (*cmd)
4246 {
4247 case '.': /* '.' - Cursor position */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004248 ++cmd;
4249 switch (addr_type)
4250 {
4251 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004252 case ADDR_OTHER:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004253 lnum = curwin->w_cursor.lnum;
4254 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004255 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004256 lnum = CURRENT_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004257 break;
4258 case ADDR_ARGUMENTS:
4259 lnum = curwin->w_arg_idx + 1;
4260 break;
4261 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004262 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004263 lnum = curbuf->b_fnum;
4264 break;
4265 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004266 lnum = CURRENT_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004267 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004268 case ADDR_TABS_RELATIVE:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004269 case ADDR_NONE:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004270 emsg(_(e_invrange));
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004271 cmd = NULL;
4272 goto error;
4273 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004274#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004275 case ADDR_QUICKFIX:
4276 lnum = qf_get_cur_valid_idx(eap);
4277 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004278#endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004279 }
4280 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004281
4282 case '$': /* '$' - last line */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004283 ++cmd;
4284 switch (addr_type)
4285 {
4286 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004287 case ADDR_OTHER:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004288 lnum = curbuf->b_ml.ml_line_count;
4289 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004290 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004291 lnum = LAST_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004292 break;
4293 case ADDR_ARGUMENTS:
4294 lnum = ARGCOUNT;
4295 break;
4296 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004297 buf = lastbuf;
4298 while (buf->b_ml.ml_mfp == NULL)
4299 {
4300 if (buf->b_prev == NULL)
4301 break;
4302 buf = buf->b_prev;
4303 }
4304 lnum = buf->b_fnum;
4305 break;
4306 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004307 lnum = lastbuf->b_fnum;
4308 break;
4309 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004310 lnum = LAST_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004311 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004312 case ADDR_TABS_RELATIVE:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004313 case ADDR_NONE:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004314 emsg(_(e_invrange));
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004315 cmd = NULL;
4316 goto error;
4317 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004318#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004319 case ADDR_QUICKFIX:
4320 lnum = qf_get_size(eap);
4321 if (lnum == 0)
4322 lnum = 1;
4323 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004324#endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004325 }
4326 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004327
4328 case '\'': /* ''' - mark */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004329 if (*++cmd == NUL)
4330 {
4331 cmd = NULL;
4332 goto error;
4333 }
4334 if (addr_type != ADDR_LINES)
4335 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004336 emsg(_(e_invaddr));
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004337 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004338 goto error;
4339 }
4340 if (skip)
4341 ++cmd;
4342 else
4343 {
4344 /* Only accept a mark in another file when it is
4345 * used by itself: ":'M". */
4346 fp = getmark(*cmd, to_other_file && cmd[1] == NUL);
4347 ++cmd;
4348 if (fp == (pos_T *)-1)
4349 /* Jumped to another file. */
4350 lnum = curwin->w_cursor.lnum;
4351 else
4352 {
4353 if (check_mark(fp) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004354 {
4355 cmd = NULL;
4356 goto error;
4357 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004358 lnum = fp->lnum;
4359 }
4360 }
4361 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004362
4363 case '/':
4364 case '?': /* '/' or '?' - search */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004365 c = *cmd++;
4366 if (addr_type != ADDR_LINES)
4367 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004368 emsg(_(e_invaddr));
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004369 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004370 goto error;
4371 }
4372 if (skip) /* skip "/pat/" */
4373 {
4374 cmd = skip_regexp(cmd, c, (int)p_magic, NULL);
4375 if (*cmd == c)
4376 ++cmd;
4377 }
4378 else
4379 {
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02004380 int flags;
4381
4382 pos = curwin->w_cursor; // save curwin->w_cursor
4383
4384 // When '/' or '?' follows another address, start from
4385 // there.
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004386 if (lnum != MAXLNUM)
4387 curwin->w_cursor.lnum = lnum;
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02004388
4389 // Start a forward search at the end of the line (unless
4390 // before the first line).
4391 // Start a backward search at the start of the line.
4392 // This makes sure we never match in the current
4393 // line, and can match anywhere in the
4394 // next/previous line.
Bram Moolenaar8ada6aa2017-12-19 21:23:21 +01004395 if (c == '/' && curwin->w_cursor.lnum > 0)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004396 curwin->w_cursor.col = MAXCOL;
4397 else
4398 curwin->w_cursor.col = 0;
4399 searchcmdlen = 0;
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02004400 flags = silent ? 0 : SEARCH_HIS | SEARCH_MSG;
4401 if (!do_search(NULL, c, cmd, 1L, flags, NULL, NULL))
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004402 {
4403 curwin->w_cursor = pos;
4404 cmd = NULL;
4405 goto error;
4406 }
4407 lnum = curwin->w_cursor.lnum;
4408 curwin->w_cursor = pos;
4409 /* adjust command string pointer */
4410 cmd += searchcmdlen;
4411 }
4412 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004413
4414 case '\\': /* "\?", "\/" or "\&", repeat search */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004415 ++cmd;
4416 if (addr_type != ADDR_LINES)
4417 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004418 emsg(_(e_invaddr));
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004419 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004420 goto error;
4421 }
4422 if (*cmd == '&')
4423 i = RE_SUBST;
4424 else if (*cmd == '?' || *cmd == '/')
4425 i = RE_SEARCH;
4426 else
4427 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004428 emsg(_(e_backslash));
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004429 cmd = NULL;
4430 goto error;
4431 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004432
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004433 if (!skip)
4434 {
4435 /*
4436 * When search follows another address, start from
4437 * there.
4438 */
4439 if (lnum != MAXLNUM)
4440 pos.lnum = lnum;
4441 else
4442 pos.lnum = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004443
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004444 /*
4445 * Start the search just like for the above
4446 * do_search().
4447 */
4448 if (*cmd != '?')
4449 pos.col = MAXCOL;
4450 else
4451 pos.col = 0;
Bram Moolenaarbd8539a2015-08-11 18:53:03 +02004452 pos.coladd = 0;
Bram Moolenaar5d24a222018-12-23 19:10:09 +01004453 if (searchit(curwin, curbuf, &pos, NULL,
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004454 *cmd == '?' ? BACKWARD : FORWARD,
4455 (char_u *)"", 1L, SEARCH_MSG,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02004456 i, (linenr_T)0, NULL, NULL) != FAIL)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004457 lnum = pos.lnum;
4458 else
4459 {
4460 cmd = NULL;
4461 goto error;
4462 }
4463 }
4464 ++cmd;
4465 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004466
4467 default:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004468 if (VIM_ISDIGIT(*cmd)) /* absolute line number */
4469 lnum = getdigits(&cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004470 }
4471
4472 for (;;)
4473 {
4474 cmd = skipwhite(cmd);
4475 if (*cmd != '-' && *cmd != '+' && !VIM_ISDIGIT(*cmd))
4476 break;
4477
4478 if (lnum == MAXLNUM)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004479 {
4480 switch (addr_type)
4481 {
4482 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004483 case ADDR_OTHER:
4484 // "+1" is same as ".+1"
Bram Moolenaarf240e182014-11-27 18:33:02 +01004485 lnum = curwin->w_cursor.lnum;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004486 break;
4487 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004488 lnum = CURRENT_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004489 break;
4490 case ADDR_ARGUMENTS:
4491 lnum = curwin->w_arg_idx + 1;
4492 break;
4493 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004494 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004495 lnum = curbuf->b_fnum;
4496 break;
4497 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004498 lnum = CURRENT_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004499 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004500 case ADDR_TABS_RELATIVE:
4501 lnum = 1;
4502 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004503#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004504 case ADDR_QUICKFIX:
4505 lnum = qf_get_cur_valid_idx(eap);
4506 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004507#endif
Bram Moolenaarb7316892019-05-01 18:08:42 +02004508 case ADDR_NONE:
4509 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004510 }
4511 }
4512
Bram Moolenaar071d4272004-06-13 20:20:40 +00004513 if (VIM_ISDIGIT(*cmd))
4514 i = '+'; /* "number" is same as "+number" */
4515 else
4516 i = *cmd++;
4517 if (!VIM_ISDIGIT(*cmd)) /* '+' is '+1', but '+0' is not '+1' */
4518 n = 1;
4519 else
4520 n = getdigits(&cmd);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004521
4522 if (addr_type == ADDR_TABS_RELATIVE)
4523 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004524 emsg(_(e_invrange));
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004525 cmd = NULL;
4526 goto error;
4527 }
4528 else if (addr_type == ADDR_LOADED_BUFFERS
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004529 || addr_type == ADDR_BUFFERS)
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004530 lnum = compute_buffer_local_count(
4531 addr_type, lnum, (i == '-') ? -1 * n : n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004532 else
Bram Moolenaarded27822017-01-02 14:27:34 +01004533 {
4534#ifdef FEAT_FOLDING
4535 /* Relative line addressing, need to adjust for folded lines
4536 * now, but only do it after the first address. */
4537 if (addr_type == ADDR_LINES && (i == '-' || i == '+')
4538 && address_count >= 2)
4539 (void)hasFolding(lnum, NULL, &lnum);
4540#endif
4541 if (i == '-')
4542 lnum -= n;
4543 else
4544 lnum += n;
4545 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004546 }
4547 } while (*cmd == '/' || *cmd == '?');
4548
4549error:
4550 *ptr = cmd;
4551 return lnum;
4552}
4553
4554/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00004555 * Get flags from an Ex command argument.
4556 */
4557 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004558get_flags(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00004559{
4560 while (vim_strchr((char_u *)"lp#", *eap->arg) != NULL)
4561 {
4562 if (*eap->arg == 'l')
4563 eap->flags |= EXFLAG_LIST;
4564 else if (*eap->arg == 'p')
4565 eap->flags |= EXFLAG_PRINT;
4566 else
4567 eap->flags |= EXFLAG_NR;
4568 eap->arg = skipwhite(eap->arg + 1);
4569 }
4570}
4571
4572/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004573 * Function called for command which is Not Implemented. NI!
4574 */
4575 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004576ex_ni(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004577{
4578 if (!eap->skip)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004579 eap->errmsg = N_("E319: Sorry, the command is not available in this version");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004580}
4581
Bram Moolenaar7bb75552007-07-16 18:39:49 +00004582#ifdef HAVE_EX_SCRIPT_NI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004583/*
4584 * Function called for script command which is Not Implemented. NI!
4585 * Skips over ":perl <<EOF" constructs.
4586 */
4587 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004588ex_script_ni(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004589{
4590 if (!eap->skip)
4591 ex_ni(eap);
4592 else
4593 vim_free(script_get(eap, eap->arg));
4594}
4595#endif
4596
4597/*
4598 * Check range in Ex command for validity.
4599 * Return NULL when valid, error message when invalid.
4600 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004601 static char *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004602invalid_range(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004603{
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004604 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004605 if ( eap->line1 < 0
4606 || eap->line2 < 0
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004607 || eap->line1 > eap->line2)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004608 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004609
4610 if (eap->argt & RANGE)
4611 {
Bram Moolenaarb7316892019-05-01 18:08:42 +02004612 switch (eap->addr_type)
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004613 {
4614 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004615 if (eap->line2 > curbuf->b_ml.ml_line_count
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004616#ifdef FEAT_DIFF
4617 + (eap->cmdidx == CMD_diffget)
4618#endif
4619 )
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004620 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004621 break;
4622 case ADDR_ARGUMENTS:
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004623 /* add 1 if ARGCOUNT is 0 */
4624 if (eap->line2 > ARGCOUNT + (!ARGCOUNT))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004625 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004626 break;
4627 case ADDR_BUFFERS:
4628 if (eap->line1 < firstbuf->b_fnum
4629 || eap->line2 > lastbuf->b_fnum)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004630 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004631 break;
4632 case ADDR_LOADED_BUFFERS:
4633 buf = firstbuf;
4634 while (buf->b_ml.ml_mfp == NULL)
4635 {
4636 if (buf->b_next == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004637 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004638 buf = buf->b_next;
4639 }
4640 if (eap->line1 < buf->b_fnum)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004641 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004642 buf = lastbuf;
4643 while (buf->b_ml.ml_mfp == NULL)
4644 {
4645 if (buf->b_prev == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004646 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004647 buf = buf->b_prev;
4648 }
4649 if (eap->line2 > buf->b_fnum)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004650 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004651 break;
4652 case ADDR_WINDOWS:
Bram Moolenaar8be63882015-01-14 11:25:05 +01004653 if (eap->line2 > LAST_WIN_NR)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004654 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004655 break;
4656 case ADDR_TABS:
4657 if (eap->line2 > LAST_TAB_NR)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004658 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004659 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004660 case ADDR_TABS_RELATIVE:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004661 case ADDR_OTHER:
4662 // Any range is OK.
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004663 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004664#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004665 case ADDR_QUICKFIX:
4666 if (eap->line2 != 1 && eap->line2 > qf_get_size(eap))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004667 return _(e_invrange);
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004668 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004669#endif
Bram Moolenaarb7316892019-05-01 18:08:42 +02004670 case ADDR_NONE:
4671 // Will give an error elsewhere.
4672 break;
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004673 }
4674 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004675 return NULL;
4676}
4677
4678/*
4679 * Correct the range for zero line number, if required.
4680 */
4681 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004682correct_range(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004683{
4684 if (!(eap->argt & ZEROR)) /* zero in range not allowed */
4685 {
4686 if (eap->line1 == 0)
4687 eap->line1 = 1;
4688 if (eap->line2 == 0)
4689 eap->line2 = 1;
4690 }
4691}
4692
Bram Moolenaar748bf032005-02-02 23:04:36 +00004693#ifdef FEAT_QUICKFIX
Bram Moolenaar748bf032005-02-02 23:04:36 +00004694/*
4695 * For a ":vimgrep" or ":vimgrepadd" command return a pointer past the
4696 * pattern. Otherwise return eap->arg.
4697 */
4698 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004699skip_grep_pat(exarg_T *eap)
Bram Moolenaar748bf032005-02-02 23:04:36 +00004700{
4701 char_u *p = eap->arg;
4702
Bram Moolenaara37420f2006-02-04 22:37:47 +00004703 if (*p != NUL && (eap->cmdidx == CMD_vimgrep || eap->cmdidx == CMD_lvimgrep
4704 || eap->cmdidx == CMD_vimgrepadd
4705 || eap->cmdidx == CMD_lvimgrepadd
4706 || grep_internal(eap->cmdidx)))
Bram Moolenaar748bf032005-02-02 23:04:36 +00004707 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00004708 p = skip_vimgrep_pat(p, NULL, NULL);
Bram Moolenaar748bf032005-02-02 23:04:36 +00004709 if (p == NULL)
4710 p = eap->arg;
Bram Moolenaar748bf032005-02-02 23:04:36 +00004711 }
4712 return p;
4713}
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004714
4715/*
4716 * For the ":make" and ":grep" commands insert the 'makeprg'/'grepprg' option
4717 * in the command line, so that things like % get expanded.
4718 */
4719 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004720replace_makeprg(exarg_T *eap, char_u *p, char_u **cmdlinep)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004721{
4722 char_u *new_cmdline;
4723 char_u *program;
4724 char_u *pos;
4725 char_u *ptr;
4726 int len;
4727 int i;
4728
4729 /*
4730 * Don't do it when ":vimgrep" is used for ":grep".
4731 */
Bram Moolenaara37420f2006-02-04 22:37:47 +00004732 if ((eap->cmdidx == CMD_make || eap->cmdidx == CMD_lmake
4733 || eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4734 || eap->cmdidx == CMD_grepadd
4735 || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004736 && !grep_internal(eap->cmdidx))
4737 {
Bram Moolenaara37420f2006-02-04 22:37:47 +00004738 if (eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4739 || eap->cmdidx == CMD_grepadd || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004740 {
4741 if (*curbuf->b_p_gp == NUL)
4742 program = p_gp;
4743 else
4744 program = curbuf->b_p_gp;
4745 }
4746 else
4747 {
4748 if (*curbuf->b_p_mp == NUL)
4749 program = p_mp;
4750 else
4751 program = curbuf->b_p_mp;
4752 }
4753
4754 p = skipwhite(p);
4755
4756 if ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4757 {
4758 /* replace $* by given arguments */
4759 i = 1;
4760 while ((pos = (char_u *)strstr((char *)pos + 2, "$*")) != NULL)
4761 ++i;
4762 len = (int)STRLEN(p);
4763 new_cmdline = alloc((int)(STRLEN(program) + i * (len - 2) + 1));
4764 if (new_cmdline == NULL)
4765 return NULL; /* out of memory */
4766 ptr = new_cmdline;
4767 while ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4768 {
4769 i = (int)(pos - program);
4770 STRNCPY(ptr, program, i);
4771 STRCPY(ptr += i, p);
4772 ptr += len;
4773 program = pos + 2;
4774 }
4775 STRCPY(ptr, program);
4776 }
4777 else
4778 {
4779 new_cmdline = alloc((int)(STRLEN(program) + STRLEN(p) + 2));
4780 if (new_cmdline == NULL)
4781 return NULL; /* out of memory */
4782 STRCPY(new_cmdline, program);
4783 STRCAT(new_cmdline, " ");
4784 STRCAT(new_cmdline, p);
4785 }
4786 msg_make(p);
4787
4788 /* 'eap->cmd' is not set here, because it is not used at CMD_make */
4789 vim_free(*cmdlinep);
4790 *cmdlinep = new_cmdline;
4791 p = new_cmdline;
4792 }
4793 return p;
4794}
Bram Moolenaar748bf032005-02-02 23:04:36 +00004795#endif
4796
Bram Moolenaar071d4272004-06-13 20:20:40 +00004797/*
4798 * Expand file name in Ex command argument.
4799 * Return FAIL for failure, OK otherwise.
4800 */
4801 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004802expand_filename(
4803 exarg_T *eap,
4804 char_u **cmdlinep,
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004805 char **errormsgp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004806{
4807 int has_wildcards; /* need to expand wildcards */
4808 char_u *repl;
4809 int srclen;
4810 char_u *p;
4811 int n;
Bram Moolenaar63b92542007-03-27 14:57:09 +00004812 int escaped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004813
Bram Moolenaar748bf032005-02-02 23:04:36 +00004814#ifdef FEAT_QUICKFIX
4815 /* Skip a regexp pattern for ":vimgrep[add] pat file..." */
4816 p = skip_grep_pat(eap);
4817#else
4818 p = eap->arg;
4819#endif
4820
Bram Moolenaar071d4272004-06-13 20:20:40 +00004821 /*
4822 * Decide to expand wildcards *before* replacing '%', '#', etc. If
4823 * the file name contains a wildcard it should not cause expanding.
4824 * (it will be expanded anyway if there is a wildcard before replacing).
4825 */
Bram Moolenaar748bf032005-02-02 23:04:36 +00004826 has_wildcards = mch_has_wildcard(p);
4827 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004828 {
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004829#ifdef FEAT_EVAL
4830 /* Skip over `=expr`, wildcards in it are not expanded. */
4831 if (p[0] == '`' && p[1] == '=')
4832 {
4833 p += 2;
4834 (void)skip_expr(&p);
4835 if (*p == '`')
4836 ++p;
4837 continue;
4838 }
4839#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004840 /*
4841 * Quick check if this cannot be the start of a special string.
4842 * Also removes backslash before '%', '#' and '<'.
4843 */
4844 if (vim_strchr((char_u *)"%#<", *p) == NULL)
4845 {
4846 ++p;
4847 continue;
4848 }
4849
4850 /*
4851 * Try to find a match at this position.
4852 */
Bram Moolenaar63b92542007-03-27 14:57:09 +00004853 repl = eval_vars(p, eap->arg, &srclen, &(eap->do_ecmd_lnum),
4854 errormsgp, &escaped);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004855 if (*errormsgp != NULL) /* error detected */
4856 return FAIL;
4857 if (repl == NULL) /* no match found */
4858 {
4859 p += srclen;
4860 continue;
4861 }
4862
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00004863 /* Wildcards won't be expanded below, the replacement is taken
4864 * literally. But do expand "~/file", "~user/file" and "$HOME/file". */
4865 if (vim_strchr(repl, '$') != NULL || vim_strchr(repl, '~') != NULL)
4866 {
4867 char_u *l = repl;
4868
4869 repl = expand_env_save(repl);
4870 vim_free(l);
4871 }
4872
Bram Moolenaar63b92542007-03-27 14:57:09 +00004873 /* Need to escape white space et al. with a backslash.
4874 * Don't do this for:
4875 * - replacement that already has been escaped: "##"
4876 * - shell commands (may have to use quotes instead).
4877 * - non-unix systems when there is a single argument (spaces don't
4878 * separate arguments then).
4879 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004880 if (!eap->usefilter
Bram Moolenaar63b92542007-03-27 14:57:09 +00004881 && !escaped
Bram Moolenaar071d4272004-06-13 20:20:40 +00004882 && eap->cmdidx != CMD_bang
Bram Moolenaar071d4272004-06-13 20:20:40 +00004883 && eap->cmdidx != CMD_grep
4884 && eap->cmdidx != CMD_grepadd
Bram Moolenaarbf15b8d2017-06-04 20:43:48 +02004885 && eap->cmdidx != CMD_hardcopy
Bram Moolenaar67883b42017-07-27 22:57:00 +02004886 && eap->cmdidx != CMD_lgrep
4887 && eap->cmdidx != CMD_lgrepadd
4888 && eap->cmdidx != CMD_lmake
4889 && eap->cmdidx != CMD_make
4890 && eap->cmdidx != CMD_terminal
Bram Moolenaar071d4272004-06-13 20:20:40 +00004891#ifndef UNIX
4892 && !(eap->argt & NOSPC)
4893#endif
4894 )
4895 {
4896 char_u *l;
4897#ifdef BACKSLASH_IN_FILENAME
4898 /* Don't escape a backslash here, because rem_backslash() doesn't
4899 * remove it later. */
4900 static char_u *nobslash = (char_u *)" \t\"|";
4901# define ESCAPE_CHARS nobslash
4902#else
4903# define ESCAPE_CHARS escape_chars
4904#endif
4905
4906 for (l = repl; *l; ++l)
4907 if (vim_strchr(ESCAPE_CHARS, *l) != NULL)
4908 {
4909 l = vim_strsave_escaped(repl, ESCAPE_CHARS);
4910 if (l != NULL)
4911 {
4912 vim_free(repl);
4913 repl = l;
4914 }
4915 break;
4916 }
4917 }
4918
4919 /* For a shell command a '!' must be escaped. */
Bram Moolenaar67883b42017-07-27 22:57:00 +02004920 if ((eap->usefilter || eap->cmdidx == CMD_bang
4921 || eap->cmdidx == CMD_terminal)
Bram Moolenaar31b7d382014-04-01 18:54:48 +02004922 && vim_strpbrk(repl, (char_u *)"!") != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004923 {
4924 char_u *l;
4925
Bram Moolenaar31b7d382014-04-01 18:54:48 +02004926 l = vim_strsave_escaped(repl, (char_u *)"!");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004927 if (l != NULL)
4928 {
4929 vim_free(repl);
4930 repl = l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004931 }
4932 }
4933
4934 p = repl_cmdline(eap, p, srclen, repl, cmdlinep);
4935 vim_free(repl);
4936 if (p == NULL)
4937 return FAIL;
4938 }
4939
4940 /*
4941 * One file argument: Expand wildcards.
4942 * Don't do this with ":r !command" or ":w !command".
4943 */
4944 if ((eap->argt & NOSPC) && !eap->usefilter)
4945 {
4946 /*
4947 * May do this twice:
4948 * 1. Replace environment variables.
4949 * 2. Replace any other wildcards, remove backslashes.
4950 */
4951 for (n = 1; n <= 2; ++n)
4952 {
4953 if (n == 2)
4954 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004955 /*
4956 * Halve the number of backslashes (this is Vi compatible).
4957 * For Unix and OS/2, when wildcards are expanded, this is
4958 * done by ExpandOne() below.
4959 */
Bram Moolenaare7fedb62015-12-31 19:07:19 +01004960#if defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004961 if (!has_wildcards)
4962#endif
4963 backslash_halve(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004964 }
4965
4966 if (has_wildcards)
4967 {
4968 if (n == 1)
4969 {
4970 /*
4971 * First loop: May expand environment variables. This
4972 * can be done much faster with expand_env() than with
4973 * something else (e.g., calling a shell).
4974 * After expanding environment variables, check again
4975 * if there are still wildcards present.
4976 */
4977 if (vim_strchr(eap->arg, '$') != NULL
4978 || vim_strchr(eap->arg, '~') != NULL)
4979 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +00004980 expand_env_esc(eap->arg, NameBuff, MAXPATHL,
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00004981 TRUE, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004982 has_wildcards = mch_has_wildcard(NameBuff);
4983 p = NameBuff;
4984 }
4985 else
4986 p = NULL;
4987 }
4988 else /* n == 2 */
4989 {
4990 expand_T xpc;
Bram Moolenaar94950a92010-12-02 16:01:29 +01004991 int options = WILD_LIST_NOTFOUND|WILD_ADD_SLASH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004992
4993 ExpandInit(&xpc);
4994 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar94950a92010-12-02 16:01:29 +01004995 if (p_wic)
4996 options += WILD_ICASE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004997 p = ExpandOne(&xpc, eap->arg, NULL,
Bram Moolenaar94950a92010-12-02 16:01:29 +01004998 options, WILD_EXPAND_FREE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004999 if (p == NULL)
5000 return FAIL;
5001 }
5002 if (p != NULL)
5003 {
5004 (void)repl_cmdline(eap, eap->arg, (int)STRLEN(eap->arg),
5005 p, cmdlinep);
5006 if (n == 2) /* p came from ExpandOne() */
5007 vim_free(p);
5008 }
5009 }
5010 }
5011 }
5012 return OK;
5013}
5014
5015/*
5016 * Replace part of the command line, keeping eap->cmd, eap->arg and
5017 * eap->nextcmd correct.
5018 * "src" points to the part that is to be replaced, of length "srclen".
5019 * "repl" is the replacement string.
5020 * Returns a pointer to the character after the replaced string.
5021 * Returns NULL for failure.
5022 */
5023 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005024repl_cmdline(
5025 exarg_T *eap,
5026 char_u *src,
5027 int srclen,
5028 char_u *repl,
5029 char_u **cmdlinep)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005030{
5031 int len;
5032 int i;
5033 char_u *new_cmdline;
5034
5035 /*
5036 * The new command line is build in new_cmdline[].
5037 * First allocate it.
5038 * Careful: a "+cmd" argument may have been NUL terminated.
5039 */
5040 len = (int)STRLEN(repl);
5041 i = (int)(src - *cmdlinep) + (int)STRLEN(src + srclen) + len + 3;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005042 if (eap->nextcmd != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005043 i += (int)STRLEN(eap->nextcmd);/* add space for next command */
5044 if ((new_cmdline = alloc((unsigned)i)) == NULL)
5045 return NULL; /* out of memory! */
5046
5047 /*
5048 * Copy the stuff before the expanded part.
5049 * Copy the expanded stuff.
5050 * Copy what came after the expanded part.
5051 * Copy the next commands, if there are any.
5052 */
5053 i = (int)(src - *cmdlinep); /* length of part before match */
5054 mch_memmove(new_cmdline, *cmdlinep, (size_t)i);
Bram Moolenaara3ffd9c2005-07-21 21:03:15 +00005055
Bram Moolenaar071d4272004-06-13 20:20:40 +00005056 mch_memmove(new_cmdline + i, repl, (size_t)len);
5057 i += len; /* remember the end of the string */
5058 STRCPY(new_cmdline + i, src + srclen);
5059 src = new_cmdline + i; /* remember where to continue */
5060
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005061 if (eap->nextcmd != NULL) /* append next command */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005062 {
5063 i = (int)STRLEN(new_cmdline) + 1;
5064 STRCPY(new_cmdline + i, eap->nextcmd);
5065 eap->nextcmd = new_cmdline + i;
5066 }
5067 eap->cmd = new_cmdline + (eap->cmd - *cmdlinep);
5068 eap->arg = new_cmdline + (eap->arg - *cmdlinep);
5069 if (eap->do_ecmd_cmd != NULL && eap->do_ecmd_cmd != dollar_command)
5070 eap->do_ecmd_cmd = new_cmdline + (eap->do_ecmd_cmd - *cmdlinep);
5071 vim_free(*cmdlinep);
5072 *cmdlinep = new_cmdline;
5073
5074 return src;
5075}
5076
5077/*
5078 * Check for '|' to separate commands and '"' to start comments.
5079 */
5080 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005081separate_nextcmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005082{
5083 char_u *p;
5084
Bram Moolenaar86b68352004-12-27 21:59:20 +00005085#ifdef FEAT_QUICKFIX
Bram Moolenaar748bf032005-02-02 23:04:36 +00005086 p = skip_grep_pat(eap);
5087#else
5088 p = eap->arg;
Bram Moolenaar86b68352004-12-27 21:59:20 +00005089#endif
5090
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005091 for ( ; *p; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005092 {
5093 if (*p == Ctrl_V)
5094 {
5095 if (eap->argt & (USECTRLV | XFILE))
5096 ++p; /* skip CTRL-V and next char */
5097 else
Bram Moolenaarb0db5692007-08-14 20:54:49 +00005098 /* remove CTRL-V and skip next char */
Bram Moolenaara7241f52008-06-24 20:39:31 +00005099 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005100 if (*p == NUL) /* stop at NUL after CTRL-V */
5101 break;
5102 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005103
5104#ifdef FEAT_EVAL
5105 /* Skip over `=expr` when wildcards are expanded. */
Bram Moolenaardf177f62005-02-22 08:39:57 +00005106 else if (p[0] == '`' && p[1] == '=' && (eap->argt & XFILE))
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005107 {
5108 p += 2;
5109 (void)skip_expr(&p);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005110 }
5111#endif
5112
Bram Moolenaar071d4272004-06-13 20:20:40 +00005113 /* Check for '"': start of comment or '|': next command */
5114 /* :@" and :*" do not start a comment!
5115 * :redir @" doesn't either. */
5116 else if ((*p == '"' && !(eap->argt & NOTRLCOM)
5117 && ((eap->cmdidx != CMD_at && eap->cmdidx != CMD_star)
5118 || p != eap->arg)
5119 && (eap->cmdidx != CMD_redir
5120 || p != eap->arg + 1 || p[-1] != '@'))
5121 || *p == '|' || *p == '\n')
5122 {
5123 /*
5124 * We remove the '\' before the '|', unless USECTRLV is used
5125 * AND 'b' is present in 'cpoptions'.
5126 */
5127 if ((vim_strchr(p_cpo, CPO_BAR) == NULL
5128 || !(eap->argt & USECTRLV)) && *(p - 1) == '\\')
5129 {
Bram Moolenaara7241f52008-06-24 20:39:31 +00005130 STRMOVE(p - 1, p); /* remove the '\' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005131 --p;
5132 }
5133 else
5134 {
5135 eap->nextcmd = check_nextcmd(p);
5136 *p = NUL;
5137 break;
5138 }
5139 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005140 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00005141
Bram Moolenaar071d4272004-06-13 20:20:40 +00005142 if (!(eap->argt & NOTRLCOM)) /* remove trailing spaces */
5143 del_trailing_spaces(eap->arg);
5144}
5145
5146/*
5147 * get + command from ex argument
5148 */
5149 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005150getargcmd(char_u **argp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005151{
5152 char_u *arg = *argp;
5153 char_u *command = NULL;
5154
5155 if (*arg == '+') /* +[command] */
5156 {
5157 ++arg;
Bram Moolenaar3e451592014-04-02 14:22:05 +02005158 if (vim_isspace(*arg) || *arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005159 command = dollar_command;
5160 else
5161 {
5162 command = arg;
5163 arg = skip_cmd_arg(command, TRUE);
5164 if (*arg != NUL)
5165 *arg++ = NUL; /* terminate command with NUL */
5166 }
5167
5168 arg = skipwhite(arg); /* skip over spaces */
5169 *argp = arg;
5170 }
5171 return command;
5172}
5173
5174/*
5175 * Find end of "+command" argument. Skip over "\ " and "\\".
5176 */
5177 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005178skip_cmd_arg(
5179 char_u *p,
5180 int rembs) /* TRUE to halve the number of backslashes */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005181{
5182 while (*p && !vim_isspace(*p))
5183 {
5184 if (*p == '\\' && p[1] != NUL)
5185 {
5186 if (rembs)
Bram Moolenaara7241f52008-06-24 20:39:31 +00005187 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005188 else
5189 ++p;
5190 }
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005191 MB_PTR_ADV(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005192 }
5193 return p;
5194}
5195
Bram Moolenaar333b80a2018-04-04 22:57:29 +02005196 int
5197get_bad_opt(char_u *p, exarg_T *eap)
5198{
5199 if (STRICMP(p, "keep") == 0)
5200 eap->bad_char = BAD_KEEP;
5201 else if (STRICMP(p, "drop") == 0)
5202 eap->bad_char = BAD_DROP;
5203 else if (MB_BYTE2LEN(*p) == 1 && p[1] == NUL)
5204 eap->bad_char = *p;
Bram Moolenaar75808492018-06-12 12:39:41 +02005205 else
5206 return FAIL;
5207 return OK;
Bram Moolenaar333b80a2018-04-04 22:57:29 +02005208}
5209
Bram Moolenaar071d4272004-06-13 20:20:40 +00005210/*
5211 * Get "++opt=arg" argument.
5212 * Return FAIL or OK.
5213 */
5214 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005215getargopt(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005216{
5217 char_u *arg = eap->arg + 2;
5218 int *pp = NULL;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02005219 int bad_char_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005220 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005221
5222 /* ":edit ++[no]bin[ary] file" */
5223 if (STRNCMP(arg, "bin", 3) == 0 || STRNCMP(arg, "nobin", 5) == 0)
5224 {
5225 if (*arg == 'n')
5226 {
5227 arg += 2;
5228 eap->force_bin = FORCE_NOBIN;
5229 }
5230 else
5231 eap->force_bin = FORCE_BIN;
5232 if (!checkforcmd(&arg, "binary", 3))
5233 return FAIL;
5234 eap->arg = skipwhite(arg);
5235 return OK;
5236 }
5237
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005238 /* ":read ++edit file" */
5239 if (STRNCMP(arg, "edit", 4) == 0)
5240 {
5241 eap->read_edit = TRUE;
5242 eap->arg = skipwhite(arg + 4);
5243 return OK;
5244 }
5245
Bram Moolenaar071d4272004-06-13 20:20:40 +00005246 if (STRNCMP(arg, "ff", 2) == 0)
5247 {
5248 arg += 2;
5249 pp = &eap->force_ff;
5250 }
5251 else if (STRNCMP(arg, "fileformat", 10) == 0)
5252 {
5253 arg += 10;
5254 pp = &eap->force_ff;
5255 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005256 else if (STRNCMP(arg, "enc", 3) == 0)
5257 {
Bram Moolenaarb38e9ab2011-12-14 14:49:45 +01005258 if (STRNCMP(arg, "encoding", 8) == 0)
5259 arg += 8;
5260 else
5261 arg += 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005262 pp = &eap->force_enc;
5263 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005264 else if (STRNCMP(arg, "bad", 3) == 0)
5265 {
5266 arg += 3;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02005267 pp = &bad_char_idx;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005268 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005269
5270 if (pp == NULL || *arg != '=')
5271 return FAIL;
5272
5273 ++arg;
5274 *pp = (int)(arg - eap->cmd);
5275 arg = skip_cmd_arg(arg, FALSE);
5276 eap->arg = skipwhite(arg);
5277 *arg = NUL;
5278
Bram Moolenaar071d4272004-06-13 20:20:40 +00005279 if (pp == &eap->force_ff)
5280 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005281 if (check_ff_value(eap->cmd + eap->force_ff) == FAIL)
5282 return FAIL;
Bram Moolenaar333b80a2018-04-04 22:57:29 +02005283 eap->force_ff = eap->cmd[eap->force_ff];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005284 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005285 else if (pp == &eap->force_enc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005286 {
5287 /* Make 'fileencoding' lower case. */
5288 for (p = eap->cmd + eap->force_enc; *p != NUL; ++p)
5289 *p = TOLOWER_ASC(*p);
5290 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005291 else
5292 {
5293 /* Check ++bad= argument. Must be a single-byte character, "keep" or
5294 * "drop". */
Bram Moolenaar333b80a2018-04-04 22:57:29 +02005295 if (get_bad_opt(eap->cmd + bad_char_idx, eap) == FAIL)
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005296 return FAIL;
5297 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005298
5299 return OK;
5300}
5301
5302/*
5303 * ":abbreviate" and friends.
5304 */
5305 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005306ex_abbreviate(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005307{
5308 do_exmap(eap, TRUE); /* almost the same as mapping */
5309}
5310
5311/*
5312 * ":map" and friends.
5313 */
5314 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005315ex_map(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005316{
5317 /*
5318 * If we are sourcing .exrc or .vimrc in current directory we
5319 * print the mappings for security reasons.
5320 */
5321 if (secure)
5322 {
5323 secure = 2;
5324 msg_outtrans(eap->cmd);
5325 msg_putchar('\n');
5326 }
5327 do_exmap(eap, FALSE);
5328}
5329
5330/*
5331 * ":unmap" and friends.
5332 */
5333 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005334ex_unmap(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005335{
5336 do_exmap(eap, FALSE);
5337}
5338
5339/*
5340 * ":mapclear" and friends.
5341 */
5342 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005343ex_mapclear(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005344{
5345 map_clear(eap->cmd, eap->arg, eap->forceit, FALSE);
5346}
5347
5348/*
5349 * ":abclear" and friends.
5350 */
5351 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005352ex_abclear(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005353{
5354 map_clear(eap->cmd, eap->arg, TRUE, TRUE);
5355}
5356
Bram Moolenaar071d4272004-06-13 20:20:40 +00005357 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005358ex_autocmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005359{
5360 /*
Bram Moolenaar26d4b892018-07-04 22:26:28 +02005361 * Disallow autocommands from .exrc and .vimrc in current
Bram Moolenaar071d4272004-06-13 20:20:40 +00005362 * directory for security reasons.
5363 */
5364 if (secure)
5365 {
5366 secure = 2;
5367 eap->errmsg = e_curdir;
5368 }
5369 else if (eap->cmdidx == CMD_autocmd)
5370 do_autocmd(eap->arg, eap->forceit);
5371 else
5372 do_augroup(eap->arg, eap->forceit);
5373}
5374
5375/*
5376 * ":doautocmd": Apply the automatic commands to the current buffer.
5377 */
5378 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005379ex_doautocmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005380{
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005381 char_u *arg = eap->arg;
5382 int call_do_modelines = check_nomodeline(&arg);
Bram Moolenaar1610d052016-06-09 22:53:01 +02005383 int did_aucmd;
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005384
Bram Moolenaar1610d052016-06-09 22:53:01 +02005385 (void)do_doautocmd(arg, TRUE, &did_aucmd);
5386 /* Only when there is no <nomodeline>. */
5387 if (call_do_modelines && did_aucmd)
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005388 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005389}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005390
Bram Moolenaar071d4272004-06-13 20:20:40 +00005391/*
5392 * :[N]bunload[!] [N] [bufname] unload buffer
5393 * :[N]bdelete[!] [N] [bufname] delete buffer from buffer list
5394 * :[N]bwipeout[!] [N] [bufname] delete buffer really
5395 */
5396 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005397ex_bunload(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005398{
5399 eap->errmsg = do_bufdel(
5400 eap->cmdidx == CMD_bdelete ? DOBUF_DEL
5401 : eap->cmdidx == CMD_bwipeout ? DOBUF_WIPE
5402 : DOBUF_UNLOAD, eap->arg,
5403 eap->addr_count, (int)eap->line1, (int)eap->line2, eap->forceit);
5404}
5405
5406/*
5407 * :[N]buffer [N] to buffer N
5408 * :[N]sbuffer [N] to buffer N
5409 */
5410 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005411ex_buffer(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005412{
5413 if (*eap->arg)
5414 eap->errmsg = e_trailing;
5415 else
5416 {
5417 if (eap->addr_count == 0) /* default is current buffer */
5418 goto_buffer(eap, DOBUF_CURRENT, FORWARD, 0);
5419 else
5420 goto_buffer(eap, DOBUF_FIRST, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005421 if (eap->do_ecmd_cmd != NULL)
5422 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005423 }
5424}
5425
5426/*
5427 * :[N]bmodified [N] to next mod. buffer
5428 * :[N]sbmodified [N] to next mod. buffer
5429 */
5430 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005431ex_bmodified(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005432{
5433 goto_buffer(eap, DOBUF_MOD, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005434 if (eap->do_ecmd_cmd != NULL)
5435 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005436}
5437
5438/*
5439 * :[N]bnext [N] to next buffer
5440 * :[N]sbnext [N] split and to next buffer
5441 */
5442 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005443ex_bnext(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005444{
5445 goto_buffer(eap, DOBUF_CURRENT, FORWARD, (int)eap->line2);
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 * :[N]bNext [N] to previous buffer
5452 * :[N]bprevious [N] to previous buffer
5453 * :[N]sbNext [N] split and to previous buffer
5454 * :[N]sbprevious [N] split and to previous buffer
5455 */
5456 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005457ex_bprevious(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005458{
5459 goto_buffer(eap, DOBUF_CURRENT, BACKWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005460 if (eap->do_ecmd_cmd != NULL)
5461 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005462}
5463
5464/*
5465 * :brewind to first buffer
5466 * :bfirst to first buffer
5467 * :sbrewind split and to first buffer
5468 * :sbfirst split and to first buffer
5469 */
5470 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005471ex_brewind(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005472{
5473 goto_buffer(eap, DOBUF_FIRST, FORWARD, 0);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005474 if (eap->do_ecmd_cmd != NULL)
5475 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005476}
5477
5478/*
5479 * :blast to last buffer
5480 * :sblast split and to last buffer
5481 */
5482 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005483ex_blast(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005484{
5485 goto_buffer(eap, DOBUF_LAST, BACKWARD, 0);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005486 if (eap->do_ecmd_cmd != NULL)
5487 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005488}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005489
5490 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005491ends_excmd(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005492{
5493 return (c == NUL || c == '|' || c == '"' || c == '\n');
5494}
5495
5496#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA) || defined(FEAT_EVAL) \
5497 || defined(PROTO)
5498/*
5499 * Return the next command, after the first '|' or '\n'.
5500 * Return NULL if not found.
5501 */
5502 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005503find_nextcmd(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005504{
5505 while (*p != '|' && *p != '\n')
5506 {
5507 if (*p == NUL)
5508 return NULL;
5509 ++p;
5510 }
5511 return (p + 1);
5512}
5513#endif
5514
5515/*
Bram Moolenaar2256c992016-11-15 21:17:07 +01005516 * Check if *p is a separator between Ex commands, skipping over white space.
5517 * Return NULL if it isn't, the following character if it is.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005518 */
5519 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005520check_nextcmd(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005521{
Bram Moolenaar2256c992016-11-15 21:17:07 +01005522 char_u *s = skipwhite(p);
5523
5524 if (*s == '|' || *s == '\n')
5525 return (s + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005526 else
5527 return NULL;
5528}
5529
5530/*
5531 * - if there are more files to edit
5532 * - and this is the last window
5533 * - and forceit not used
5534 * - and not repeated twice on a row
5535 * return FAIL and give error message if 'message' TRUE
5536 * return OK otherwise
5537 */
5538 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005539check_more(
5540 int message, /* when FALSE check only, no messages */
5541 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005542{
5543 int n = ARGCOUNT - curwin->w_arg_idx - 1;
5544
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005545 if (!forceit && only_one_window()
5546 && ARGCOUNT > 1 && !arg_had_last && n >= 0 && quitmore == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005547 {
5548 if (message)
5549 {
5550#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
5551 if ((p_confirm || cmdmod.confirm) && curbuf->b_fname != NULL)
5552 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02005553 char_u buff[DIALOG_MSG_SIZE];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005554
Bram Moolenaarda6e8912018-08-21 15:12:14 +02005555 vim_snprintf((char *)buff, DIALOG_MSG_SIZE,
5556 NGETTEXT("%d more file to edit. Quit anyway?",
5557 "%d more files to edit. Quit anyway?", n), n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005558 if (vim_dialog_yesno(VIM_QUESTION, NULL, buff, 1) == VIM_YES)
5559 return OK;
5560 return FAIL;
5561 }
5562#endif
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01005563 semsg(NGETTEXT("E173: %d more file to edit",
5564 "E173: %d more files to edit", n), n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005565 quitmore = 2; /* next try to quit is allowed */
5566 }
5567 return FAIL;
5568 }
5569 return OK;
5570}
5571
Bram Moolenaarac9fb182019-04-27 13:04:13 +02005572#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005573/*
5574 * Function given to ExpandGeneric() to obtain the list of command names.
5575 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005576 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005577get_command_name(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005578{
5579 if (idx >= (int)CMD_SIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005580 return get_user_command_name(idx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005581 return cmdnames[idx].cmd_name;
5582}
5583#endif
5584
Bram Moolenaar071d4272004-06-13 20:20:40 +00005585 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005586ex_colorscheme(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005587{
Bram Moolenaare6850792010-05-14 15:28:44 +02005588 if (*eap->arg == NUL)
5589 {
5590#ifdef FEAT_EVAL
5591 char_u *expr = vim_strsave((char_u *)"g:colors_name");
5592 char_u *p = NULL;
5593
5594 if (expr != NULL)
5595 {
5596 ++emsg_off;
5597 p = eval_to_string(expr, NULL, FALSE);
5598 --emsg_off;
5599 vim_free(expr);
5600 }
5601 if (p != NULL)
5602 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01005603 msg((char *)p);
Bram Moolenaare6850792010-05-14 15:28:44 +02005604 vim_free(p);
5605 }
5606 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01005607 msg("default");
Bram Moolenaare6850792010-05-14 15:28:44 +02005608#else
Bram Moolenaar32526b32019-01-19 17:43:09 +01005609 msg(_("unknown"));
Bram Moolenaare6850792010-05-14 15:28:44 +02005610#endif
5611 }
5612 else if (load_colors(eap->arg) == FAIL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005613 semsg(_("E185: Cannot find color scheme '%s'"), eap->arg);
Bram Moolenaarf58d81a2019-01-28 20:19:05 +01005614
5615#ifdef FEAT_VTP
5616 else if (has_vtp_working())
5617 {
5618 // background color change requires clear + redraw
5619 update_screen(CLEAR);
5620 redrawcmd();
5621 }
5622#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005623}
5624
5625 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005626ex_highlight(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005627{
5628 if (*eap->arg == NUL && eap->cmd[2] == '!')
Bram Moolenaar32526b32019-01-19 17:43:09 +01005629 msg(_("Greetings, Vim user!"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005630 do_highlight(eap->arg, eap->forceit, FALSE);
5631}
5632
5633
5634/*
5635 * Call this function if we thought we were going to exit, but we won't
5636 * (because of an error). May need to restore the terminal mode.
5637 */
5638 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005639not_exiting(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005640{
5641 exiting = FALSE;
5642 settmode(TMODE_RAW);
5643}
5644
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005645 static int
5646before_quit_autocmds(win_T *wp, int quit_all, int forceit)
5647{
5648 apply_autocmds(EVENT_QUITPRE, NULL, NULL, FALSE, wp->w_buffer);
5649
5650 /* Bail out when autocommands closed the window.
5651 * Refuse to quit when the buffer in the last window is being closed (can
5652 * only happen in autocommands). */
5653 if (!win_valid(wp)
5654 || curbuf_locked()
5655 || (wp->w_buffer->b_nwindows == 1 && wp->w_buffer->b_locked > 0))
5656 return TRUE;
5657
5658 if (quit_all || (check_more(FALSE, forceit) == OK && only_one_window()))
5659 {
5660 apply_autocmds(EVENT_EXITPRE, NULL, NULL, FALSE, curbuf);
5661 /* Refuse to quit when locked or when the buffer in the last window is
5662 * being closed (can only happen in autocommands). */
5663 if (curbuf_locked()
5664 || (curbuf->b_nwindows == 1 && curbuf->b_locked > 0))
5665 return TRUE;
5666 }
5667
5668 return FALSE;
5669}
5670
Bram Moolenaar071d4272004-06-13 20:20:40 +00005671/*
Bram Moolenaarf240e182014-11-27 18:33:02 +01005672 * ":quit": quit current window, quit Vim if the last window is closed.
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005673 * ":{nr}quit": quit window {nr}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005674 */
5675 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005676ex_quit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005677{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005678 win_T *wp;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005679
Bram Moolenaar071d4272004-06-13 20:20:40 +00005680#ifdef FEAT_CMDWIN
5681 if (cmdwin_type != 0)
5682 {
5683 cmdwin_result = Ctrl_C;
5684 return;
5685 }
5686#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005687 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005688 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005689 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005690 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005691 return;
5692 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005693 if (eap->addr_count > 0)
5694 {
Bram Moolenaarf240e182014-11-27 18:33:02 +01005695 int wnr = eap->line2;
5696
5697 for (wp = firstwin; wp->w_next != NULL; wp = wp->w_next)
5698 if (--wnr <= 0)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005699 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005700 }
5701 else
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005702 wp = curwin;
Bram Moolenaarf240e182014-11-27 18:33:02 +01005703
Bram Moolenaar87ffb5c2017-10-19 12:37:42 +02005704 /* Refuse to quit when locked. */
5705 if (curbuf_locked())
5706 return;
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005707
5708 /* Trigger QuitPre and maybe ExitPre */
5709 if (before_quit_autocmds(wp, FALSE, eap->forceit))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005710 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005711
5712#ifdef FEAT_NETBEANS_INTG
5713 netbeansForcedQuit = eap->forceit;
5714#endif
5715
5716 /*
5717 * If there are more files or windows we won't exit.
5718 */
5719 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
5720 exiting = TRUE;
Bram Moolenaarff930ca2017-10-19 17:12:10 +02005721 if ((!buf_hide(wp->w_buffer)
5722 && check_changed(wp->w_buffer, (p_awa ? CCGD_AW : 0)
Bram Moolenaar45d3b142013-11-09 03:31:51 +01005723 | (eap->forceit ? CCGD_FORCEIT : 0)
5724 | CCGD_EXCMD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005725 || check_more(TRUE, eap->forceit) == FAIL
Bram Moolenaar027387f2016-01-02 22:25:52 +01005726 || (only_one_window() && check_changed_any(eap->forceit, TRUE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005727 {
5728 not_exiting();
5729 }
5730 else
5731 {
Bram Moolenaarc7a0d322015-06-19 12:43:07 +02005732 /* quit last window
5733 * Note: only_one_window() returns true, even so a help window is
5734 * still open. In that case only quit, if no address has been
5735 * specified. Example:
5736 * :h|wincmd w|1q - don't quit
5737 * :h|wincmd w|q - quit
5738 */
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01005739 if (only_one_window() && (ONE_WINDOW || eap->addr_count == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005740 getout(0);
Bram Moolenaar2c33d7b2017-10-14 16:06:20 +02005741 not_exiting();
Bram Moolenaar4033c552017-09-16 20:54:51 +02005742#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00005743 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005744#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005745 /* close window; may free buffer */
Bram Moolenaareb44a682017-08-03 22:44:55 +02005746 win_close(wp, !buf_hide(wp->w_buffer) || eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005747 }
5748}
5749
5750/*
5751 * ":cquit".
5752 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005753 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005754ex_cquit(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005755{
5756 getout(1); /* this does not always pass on the exit code to the Manx
5757 compiler. why? */
5758}
5759
5760/*
5761 * ":qall": try to quit all windows
5762 */
5763 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005764ex_quit_all(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005765{
5766# ifdef FEAT_CMDWIN
5767 if (cmdwin_type != 0)
5768 {
5769 if (eap->forceit)
5770 cmdwin_result = K_XF1; /* ex_window() takes care of this */
5771 else
5772 cmdwin_result = K_XF2;
5773 return;
5774 }
5775# endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005776
5777 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005778 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005779 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005780 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005781 return;
5782 }
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005783
5784 if (before_quit_autocmds(curwin, TRUE, eap->forceit))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005785 return;
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005786
Bram Moolenaar071d4272004-06-13 20:20:40 +00005787 exiting = TRUE;
Bram Moolenaar027387f2016-01-02 22:25:52 +01005788 if (eap->forceit || !check_changed_any(FALSE, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005789 getout(0);
5790 not_exiting();
5791}
5792
Bram Moolenaar071d4272004-06-13 20:20:40 +00005793/*
5794 * ":close": close current window, unless it is the last one
5795 */
5796 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005797ex_close(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005798{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005799 win_T *win;
5800 int winnr = 0;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005801#ifdef FEAT_CMDWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00005802 if (cmdwin_type != 0)
Bram Moolenaar9bd1a7e2011-05-19 14:50:54 +02005803 cmdwin_result = Ctrl_C;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005804 else
Bram Moolenaar4033c552017-09-16 20:54:51 +02005805#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005806 if (!text_locked() && !curbuf_locked())
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005807 {
5808 if (eap->addr_count == 0)
5809 ex_win_close(eap->forceit, curwin, NULL);
Bram Moolenaar6d41c782018-06-06 09:11:12 +02005810 else
5811 {
Bram Moolenaar29323592016-07-24 22:04:11 +02005812 FOR_ALL_WINDOWS(win)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005813 {
5814 winnr++;
5815 if (winnr == eap->line2)
5816 break;
5817 }
5818 if (win == NULL)
5819 win = lastwin;
5820 ex_win_close(eap->forceit, win, NULL);
5821 }
5822 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005823}
5824
Bram Moolenaar4033c552017-09-16 20:54:51 +02005825#ifdef FEAT_QUICKFIX
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005826/*
5827 * ":pclose": Close any preview window.
5828 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005829 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005830ex_pclose(exarg_T *eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005831{
5832 win_T *win;
5833
Bram Moolenaar29323592016-07-24 22:04:11 +02005834 FOR_ALL_WINDOWS(win)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005835 if (win->w_p_pvw)
5836 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00005837 ex_win_close(eap->forceit, win, NULL);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005838 break;
5839 }
5840}
Bram Moolenaar4033c552017-09-16 20:54:51 +02005841#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005842
Bram Moolenaarf740b292006-02-16 22:11:02 +00005843/*
5844 * Close window "win" and take care of handling closing the last window for a
5845 * modified buffer.
5846 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005847 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005848ex_win_close(
5849 int forceit,
5850 win_T *win,
5851 tabpage_T *tp) /* NULL or the tab page "win" is in */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005852{
5853 int need_hide;
5854 buf_T *buf = win->w_buffer;
5855
5856 need_hide = (bufIsChanged(buf) && buf->b_nwindows <= 1);
Bram Moolenaareb44a682017-08-03 22:44:55 +02005857 if (need_hide && !buf_hide(buf) && !forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005858 {
Bram Moolenaar4033c552017-09-16 20:54:51 +02005859#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005860 if ((p_confirm || cmdmod.confirm) && p_write)
5861 {
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005862 bufref_T bufref;
5863
5864 set_bufref(&bufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005865 dialog_changed(buf, FALSE);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005866 if (bufref_valid(&bufref) && bufIsChanged(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005867 return;
5868 need_hide = FALSE;
5869 }
5870 else
Bram Moolenaar4033c552017-09-16 20:54:51 +02005871#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005872 {
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02005873 no_write_message();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005874 return;
5875 }
5876 }
5877
Bram Moolenaar4033c552017-09-16 20:54:51 +02005878#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00005879 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005880#endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00005881
Bram Moolenaar071d4272004-06-13 20:20:40 +00005882 /* free buffer when not hiding it or when it's a scratch buffer */
Bram Moolenaarf740b292006-02-16 22:11:02 +00005883 if (tp == NULL)
Bram Moolenaareb44a682017-08-03 22:44:55 +02005884 win_close(win, !need_hide && !buf_hide(buf));
Bram Moolenaarf740b292006-02-16 22:11:02 +00005885 else
Bram Moolenaareb44a682017-08-03 22:44:55 +02005886 win_close_othertab(win, !need_hide && !buf_hide(buf), tp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005887}
5888
Bram Moolenaar071d4272004-06-13 20:20:40 +00005889/*
Bram Moolenaardea25702017-01-29 15:18:10 +01005890 * Handle the argument for a tabpage related ex command.
5891 * Returns a tabpage number.
5892 * When an error is encountered then eap->errmsg is set.
5893 */
5894 static int
5895get_tabpage_arg(exarg_T *eap)
5896{
5897 int tab_number;
5898 int unaccept_arg0 = (eap->cmdidx == CMD_tabmove) ? 0 : 1;
5899
5900 if (eap->arg && *eap->arg != NUL)
5901 {
5902 char_u *p = eap->arg;
5903 char_u *p_save;
5904 int relative = 0; /* argument +N/-N means: go to N places to the
5905 * right/left relative to the current position. */
5906
5907 if (*p == '-')
5908 {
5909 relative = -1;
5910 p++;
5911 }
5912 else if (*p == '+')
5913 {
5914 relative = 1;
5915 p++;
5916 }
5917
5918 p_save = p;
5919 tab_number = getdigits(&p);
5920
5921 if (relative == 0)
5922 {
5923 if (STRCMP(p, "$") == 0)
5924 tab_number = LAST_TAB_NR;
5925 else if (p == p_save || *p_save == '-' || *p != NUL
5926 || tab_number > LAST_TAB_NR)
5927 {
5928 /* No numbers as argument. */
5929 eap->errmsg = e_invarg;
5930 goto theend;
5931 }
5932 }
5933 else
5934 {
5935 if (*p_save == NUL)
5936 tab_number = 1;
5937 else if (p == p_save || *p_save == '-' || *p != NUL
5938 || tab_number == 0)
5939 {
5940 /* No numbers as argument. */
5941 eap->errmsg = e_invarg;
5942 goto theend;
5943 }
5944 tab_number = tab_number * relative + tabpage_index(curtab);
5945 if (!unaccept_arg0 && relative == -1)
5946 --tab_number;
5947 }
5948 if (tab_number < unaccept_arg0 || tab_number > LAST_TAB_NR)
5949 eap->errmsg = e_invarg;
5950 }
5951 else if (eap->addr_count > 0)
5952 {
5953 if (unaccept_arg0 && eap->line2 == 0)
Bram Moolenaarc6251552017-01-29 21:42:20 +01005954 {
Bram Moolenaardea25702017-01-29 15:18:10 +01005955 eap->errmsg = e_invrange;
Bram Moolenaarc6251552017-01-29 21:42:20 +01005956 tab_number = 0;
5957 }
Bram Moolenaardea25702017-01-29 15:18:10 +01005958 else
5959 {
5960 tab_number = eap->line2;
Bram Moolenaar82a12462019-01-22 22:41:42 +01005961 if (!unaccept_arg0 && *skipwhite(*eap->cmdlinep) == '-')
Bram Moolenaardea25702017-01-29 15:18:10 +01005962 {
5963 --tab_number;
5964 if (tab_number < unaccept_arg0)
5965 eap->errmsg = e_invarg;
5966 }
5967 }
5968 }
5969 else
5970 {
5971 switch (eap->cmdidx)
5972 {
5973 case CMD_tabnext:
5974 tab_number = tabpage_index(curtab) + 1;
5975 if (tab_number > LAST_TAB_NR)
5976 tab_number = 1;
5977 break;
5978 case CMD_tabmove:
5979 tab_number = LAST_TAB_NR;
5980 break;
5981 default:
5982 tab_number = tabpage_index(curtab);
5983 }
5984 }
5985
5986theend:
5987 return tab_number;
5988}
5989
5990/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00005991 * ":tabclose": close current tab page, unless it is the last one.
5992 * ":tabclose N": close tab page N.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005993 */
5994 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005995ex_tabclose(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005996{
Bram Moolenaarf740b292006-02-16 22:11:02 +00005997 tabpage_T *tp;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005998 int tab_number;
Bram Moolenaarf740b292006-02-16 22:11:02 +00005999
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006000# ifdef FEAT_CMDWIN
6001 if (cmdwin_type != 0)
6002 cmdwin_result = K_IGNORE;
6003 else
6004# endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00006005 if (first_tabpage->tp_next == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006006 emsg(_("E784: Cannot close last tab page"));
Bram Moolenaarf740b292006-02-16 22:11:02 +00006007 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006008 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006009 tab_number = get_tabpage_arg(eap);
6010 if (eap->errmsg == NULL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006011 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006012 tp = find_tabpage(tab_number);
Bram Moolenaarf740b292006-02-16 22:11:02 +00006013 if (tp == NULL)
6014 {
6015 beep_flush();
6016 return;
6017 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00006018 if (tp != curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006019 {
6020 tabpage_close_other(tp, eap->forceit);
6021 return;
6022 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006023 else if (!text_locked() && !curbuf_locked())
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006024 tabpage_close(eap->forceit);
6025 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006026 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006027}
6028
6029/*
6030 * ":tabonly": close all tab pages except the current one
6031 */
6032 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006033ex_tabonly(exarg_T *eap)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006034{
6035 tabpage_T *tp;
6036 int done;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006037 int tab_number;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006038
6039# ifdef FEAT_CMDWIN
6040 if (cmdwin_type != 0)
6041 cmdwin_result = K_IGNORE;
6042 else
6043# endif
6044 if (first_tabpage->tp_next == NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01006045 msg(_("Already only one tab page"));
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006046 else
6047 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006048 tab_number = get_tabpage_arg(eap);
6049 if (eap->errmsg == NULL)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006050 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006051 goto_tabpage(tab_number);
6052 /* Repeat this up to a 1000 times, because autocommands may
6053 * mess up the lists. */
6054 for (done = 0; done < 1000; ++done)
6055 {
6056 FOR_ALL_TABPAGES(tp)
6057 if (tp->tp_topframe != topframe)
6058 {
6059 tabpage_close_other(tp, eap->forceit);
6060 /* if we failed to close it quit */
6061 if (valid_tabpage(tp))
6062 done = 1000;
6063 /* start over, "tp" is now invalid */
6064 break;
6065 }
6066 if (first_tabpage->tp_next == NULL)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006067 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006068 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006069 }
6070 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006071}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006072
6073/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00006074 * Close the current tab page.
6075 */
6076 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006077tabpage_close(int forceit)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006078{
6079 /* First close all the windows but the current one. If that worked then
6080 * close the last window in this tab, that will close it. */
Bram Moolenaar459ca562016-11-10 18:16:33 +01006081 if (!ONE_WINDOW)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006082 close_others(TRUE, forceit);
Bram Moolenaar459ca562016-11-10 18:16:33 +01006083 if (ONE_WINDOW)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006084 ex_win_close(forceit, curwin, NULL);
6085# ifdef FEAT_GUI
6086 need_mouse_correct = TRUE;
6087# endif
6088}
6089
6090/*
6091 * Close tab page "tp", which is not the current tab page.
6092 * Note that autocommands may make "tp" invalid.
Bram Moolenaar7875acc2006-09-10 13:51:17 +00006093 * Also takes care of the tab pages line disappearing when closing the
6094 * last-but-one tab page.
Bram Moolenaarf740b292006-02-16 22:11:02 +00006095 */
6096 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006097tabpage_close_other(tabpage_T *tp, int forceit)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006098{
6099 int done = 0;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006100 win_T *wp;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00006101 int h = tabline_height();
Bram Moolenaarf740b292006-02-16 22:11:02 +00006102
6103 /* Limit to 1000 windows, autocommands may add a window while we close
6104 * one. OK, so I'm paranoid... */
6105 while (++done < 1000)
6106 {
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006107 wp = tp->tp_firstwin;
6108 ex_win_close(forceit, wp, tp);
Bram Moolenaarf740b292006-02-16 22:11:02 +00006109
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006110 /* Autocommands may delete the tab page under our fingers and we may
6111 * fail to close a window with a modified buffer. */
6112 if (!valid_tabpage(tp) || tp->tp_firstwin == wp)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006113 break;
6114 }
Bram Moolenaar7875acc2006-09-10 13:51:17 +00006115
Bram Moolenaar29323592016-07-24 22:04:11 +02006116 apply_autocmds(EVENT_TABCLOSED, NULL, NULL, FALSE, curbuf);
Bram Moolenaar29323592016-07-24 22:04:11 +02006117
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006118 redraw_tabline = TRUE;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00006119 if (h != tabline_height())
6120 shell_new_rows();
Bram Moolenaarf740b292006-02-16 22:11:02 +00006121}
6122
6123/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006124 * ":only".
6125 */
6126 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006127ex_only(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006128{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01006129 win_T *wp;
6130 int wnr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006131# ifdef FEAT_GUI
6132 need_mouse_correct = TRUE;
6133# endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01006134 if (eap->addr_count > 0)
6135 {
6136 wnr = eap->line2;
6137 for (wp = firstwin; --wnr > 0; )
6138 {
6139 if (wp->w_next == NULL)
6140 break;
6141 else
6142 wp = wp->w_next;
6143 }
6144 win_goto(wp);
6145 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006146 close_others(TRUE, eap->forceit);
6147}
6148
6149/*
6150 * ":all" and ":sall".
Bram Moolenaard9967712006-03-11 21:18:15 +00006151 * Also used for ":tab drop file ..." after setting the argument list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006152 */
Bram Moolenaard9967712006-03-11 21:18:15 +00006153 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006154ex_all(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006155{
6156 if (eap->addr_count == 0)
6157 eap->line2 = 9999;
Bram Moolenaard9967712006-03-11 21:18:15 +00006158 do_arg_all((int)eap->line2, eap->forceit, eap->cmdidx == CMD_drop);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006159}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006160
6161 static void
Bram Moolenaar5e1e6d22017-01-02 17:26:00 +01006162ex_hide(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006163{
Bram Moolenaar2256c992016-11-15 21:17:07 +01006164 /* ":hide" or ":hide | cmd": hide current window */
Bram Moolenaar2256c992016-11-15 21:17:07 +01006165 if (!eap->skip)
6166 {
Bram Moolenaar4033c552017-09-16 20:54:51 +02006167#ifdef FEAT_GUI
Bram Moolenaar2256c992016-11-15 21:17:07 +01006168 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006169#endif
Bram Moolenaar2256c992016-11-15 21:17:07 +01006170 if (eap->addr_count == 0)
6171 win_close(curwin, FALSE); /* don't free buffer */
6172 else
6173 {
6174 int winnr = 0;
6175 win_T *win;
Bram Moolenaarf240e182014-11-27 18:33:02 +01006176
Bram Moolenaar2256c992016-11-15 21:17:07 +01006177 FOR_ALL_WINDOWS(win)
6178 {
6179 winnr++;
6180 if (winnr == eap->line2)
6181 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01006182 }
Bram Moolenaar2256c992016-11-15 21:17:07 +01006183 if (win == NULL)
6184 win = lastwin;
6185 win_close(win, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006186 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006187 }
6188}
6189
6190/*
6191 * ":stop" and ":suspend": Suspend Vim.
6192 */
6193 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006194ex_stop(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006195{
6196 /*
6197 * Disallow suspending for "rvim".
6198 */
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006199 if (!check_restricted())
Bram Moolenaar071d4272004-06-13 20:20:40 +00006200 {
6201 if (!eap->forceit)
6202 autowrite_all();
6203 windgoto((int)Rows - 1, 0);
6204 out_char('\n');
6205 out_flush();
6206 stoptermcap();
6207 out_flush(); /* needed for SUN to restore xterm buffer */
6208#ifdef FEAT_TITLE
Bram Moolenaar40385db2018-08-07 22:31:44 +02006209 mch_restore_title(SAVE_RESTORE_BOTH); /* restore window titles */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006210#endif
6211 ui_suspend(); /* call machine specific function */
6212#ifdef FEAT_TITLE
6213 maketitle();
6214 resettitle(); /* force updating the title */
6215#endif
6216 starttermcap();
6217 scroll_start(); /* scroll screen before redrawing */
6218 redraw_later_clear();
6219 shell_resized(); /* may have resized window */
6220 }
6221}
6222
6223/*
Bram Moolenaar12a96de2018-03-11 14:44:18 +01006224 * ":exit", ":xit" and ":wq": Write file and quite the current window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006225 */
6226 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006227ex_exit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006228{
6229#ifdef FEAT_CMDWIN
6230 if (cmdwin_type != 0)
6231 {
6232 cmdwin_result = Ctrl_C;
6233 return;
6234 }
6235#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006236 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006237 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006238 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006239 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006240 return;
6241 }
Bram Moolenaar12a96de2018-03-11 14:44:18 +01006242
6243 if (before_quit_autocmds(curwin, FALSE, eap->forceit))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006244 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006245
6246 /*
6247 * if more files or windows we won't exit
6248 */
6249 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
6250 exiting = TRUE;
6251 if ( ((eap->cmdidx == CMD_wq
6252 || curbufIsChanged())
6253 && do_write(eap) == FAIL)
6254 || check_more(TRUE, eap->forceit) == FAIL
Bram Moolenaar027387f2016-01-02 22:25:52 +01006255 || (only_one_window() && check_changed_any(eap->forceit, FALSE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006256 {
6257 not_exiting();
6258 }
6259 else
6260 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006261 if (only_one_window()) /* quit last window, exit Vim */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006262 getout(0);
Bram Moolenaar2c33d7b2017-10-14 16:06:20 +02006263 not_exiting();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006264# ifdef FEAT_GUI
6265 need_mouse_correct = TRUE;
6266# endif
Bram Moolenaar1a4a75c2013-07-28 16:03:06 +02006267 /* Quit current window, may free the buffer. */
Bram Moolenaareb44a682017-08-03 22:44:55 +02006268 win_close(curwin, !buf_hide(curwin->w_buffer));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006269 }
6270}
6271
6272/*
6273 * ":print", ":list", ":number".
6274 */
6275 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006276ex_print(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006277{
Bram Moolenaardf177f62005-02-22 08:39:57 +00006278 if (curbuf->b_ml.ml_flags & ML_EMPTY)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006279 emsg(_(e_emptybuf));
Bram Moolenaardf177f62005-02-22 08:39:57 +00006280 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006281 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00006282 for ( ;!got_int; ui_breakcheck())
6283 {
6284 print_line(eap->line1,
6285 (eap->cmdidx == CMD_number || eap->cmdidx == CMD_pound
6286 || (eap->flags & EXFLAG_NR)),
6287 eap->cmdidx == CMD_list || (eap->flags & EXFLAG_LIST));
6288 if (++eap->line1 > eap->line2)
6289 break;
6290 out_flush(); /* show one line at a time */
6291 }
6292 setpcmark();
6293 /* put cursor at last line */
6294 curwin->w_cursor.lnum = eap->line2;
6295 beginline(BL_SOL | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006296 }
6297
Bram Moolenaar071d4272004-06-13 20:20:40 +00006298 ex_no_reprint = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006299}
6300
6301#ifdef FEAT_BYTEOFF
6302 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006303ex_goto(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006304{
6305 goto_byte(eap->line2);
6306}
6307#endif
6308
6309/*
6310 * ":shell".
6311 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006312 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006313ex_shell(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006314{
6315 do_shell(NULL, 0);
6316}
6317
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006318#if defined(HAVE_DROP_FILE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006319
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006320static int drop_busy = FALSE;
6321static int drop_filec;
6322static char_u **drop_filev = NULL;
6323static int drop_split;
6324static void (*drop_callback)(void *);
6325static void *drop_cookie;
6326
6327 static void
6328handle_drop_internal(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006329{
6330 exarg_T ea;
6331 int save_msg_scroll = msg_scroll;
6332
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006333 // Setting the argument list may cause screen updates and being called
6334 // recursively. Avoid that by setting drop_busy.
6335 drop_busy = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006336
6337 /* Check whether the current buffer is changed. If so, we will need
6338 * to split the current window or data could be lost.
6339 * We don't need to check if the 'hidden' option is set, as in this
6340 * case the buffer won't be lost.
6341 */
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006342 if (!buf_hide(curbuf) && !drop_split)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006343 {
6344 ++emsg_off;
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006345 drop_split = check_changed(curbuf, CCGD_AW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006346 --emsg_off;
6347 }
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006348 if (drop_split)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006349 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006350 if (win_split(0, 0) == FAIL)
6351 return;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02006352 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006353
6354 /* When splitting the window, create a new alist. Otherwise the
6355 * existing one is overwritten. */
6356 alist_unlink(curwin->w_alist);
6357 alist_new();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006358 }
6359
6360 /*
6361 * Set up the new argument list.
6362 */
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006363 alist_set(ALIST(curwin), drop_filec, drop_filev, FALSE, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006364
6365 /*
6366 * Move to the first file.
6367 */
6368 /* Fake up a minimal "next" command for do_argfile() */
6369 vim_memset(&ea, 0, sizeof(ea));
6370 ea.cmd = (char_u *)"next";
6371 do_argfile(&ea, 0);
6372
6373 /* do_ecmd() may set need_start_insertmode, but since we never left Insert
6374 * mode that is not needed here. */
6375 need_start_insertmode = FALSE;
6376
6377 /* Restore msg_scroll, otherwise a following command may cause scrolling
6378 * unexpectedly. The screen will be redrawn by the caller, thus
6379 * msg_scroll being set by displaying a message is irrelevant. */
6380 msg_scroll = save_msg_scroll;
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006381
6382 if (drop_callback != NULL)
6383 drop_callback(drop_cookie);
6384
6385 drop_filev = NULL;
6386 drop_busy = FALSE;
6387}
6388
6389/*
6390 * Handle a file drop. The code is here because a drop is *nearly* like an
6391 * :args command, but not quite (we have a list of exact filenames, so we
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01006392 * don't want to (a) parse a command line, or (b) expand wildcards). So the
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006393 * code is very similar to :args and hence needs access to a lot of the static
6394 * functions in this file.
6395 *
6396 * The "filev" list must have been allocated using alloc(), as should each item
6397 * in the list. This function takes over responsibility for freeing the "filev"
6398 * list.
6399 */
6400 void
6401handle_drop(
6402 int filec, // the number of files dropped
6403 char_u **filev, // the list of files dropped
6404 int split, // force splitting the window
6405 void (*callback)(void *), // to be called after setting the argument
6406 // list
6407 void *cookie) // argument for "callback" (allocated)
6408{
6409 // Cannot handle recursive drops, finish the pending one.
6410 if (drop_busy)
6411 {
6412 FreeWild(filec, filev);
6413 vim_free(cookie);
6414 return;
6415 }
6416
6417 // When calling handle_drop() more than once in a row we only use the last
6418 // one.
6419 if (drop_filev != NULL)
6420 {
6421 FreeWild(drop_filec, drop_filev);
6422 vim_free(drop_cookie);
6423 }
6424
6425 drop_filec = filec;
6426 drop_filev = filev;
6427 drop_split = split;
6428 drop_callback = callback;
6429 drop_cookie = cookie;
6430
6431 // Postpone this when:
6432 // - editing the command line
6433 // - not possible to change the current buffer
6434 // - updating the screen
6435 // As it may change buffers and window structures that are in use and cause
6436 // freed memory to be used.
6437 if (text_locked() || curbuf_locked() || updating_screen)
6438 return;
6439
6440 handle_drop_internal();
6441}
6442
6443/*
6444 * To be called when text is unlocked, curbuf is unlocked or updating_screen is
6445 * reset: Handle a postponed drop.
6446 */
6447 void
6448handle_any_postponed_drop(void)
6449{
6450 if (!drop_busy && drop_filev != NULL
6451 && !text_locked() && !curbuf_locked() && !updating_screen)
6452 handle_drop_internal();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006453}
6454#endif
6455
Bram Moolenaar071d4272004-06-13 20:20:40 +00006456/*
6457 * Clear an argument list: free all file names and reset it to zero entries.
6458 */
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00006459 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006460alist_clear(alist_T *al)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006461{
6462 while (--al->al_ga.ga_len >= 0)
6463 vim_free(AARGLIST(al)[al->al_ga.ga_len].ae_fname);
6464 ga_clear(&al->al_ga);
6465}
6466
6467/*
6468 * Init an argument list.
6469 */
6470 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006471alist_init(alist_T *al)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006472{
6473 ga_init2(&al->al_ga, (int)sizeof(aentry_T), 5);
6474}
6475
Bram Moolenaar071d4272004-06-13 20:20:40 +00006476/*
6477 * Remove a reference from an argument list.
6478 * Ignored when the argument list is the global one.
6479 * If the argument list is no longer used by any window, free it.
6480 */
6481 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006482alist_unlink(alist_T *al)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006483{
6484 if (al != &global_alist && --al->al_refcount <= 0)
6485 {
6486 alist_clear(al);
6487 vim_free(al);
6488 }
6489}
6490
Bram Moolenaar071d4272004-06-13 20:20:40 +00006491/*
6492 * Create a new argument list and use it for the current window.
6493 */
6494 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006495alist_new(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006496{
6497 curwin->w_alist = (alist_T *)alloc((unsigned)sizeof(alist_T));
6498 if (curwin->w_alist == NULL)
6499 {
6500 curwin->w_alist = &global_alist;
6501 ++global_alist.al_refcount;
6502 }
6503 else
6504 {
6505 curwin->w_alist->al_refcount = 1;
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02006506 curwin->w_alist->id = ++max_alist_id;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006507 alist_init(curwin->w_alist);
6508 }
6509}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006510
Bram Moolenaara06ecab2016-07-16 14:47:36 +02006511#if !defined(UNIX) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006512/*
6513 * Expand the file names in the global argument list.
Bram Moolenaar86b68352004-12-27 21:59:20 +00006514 * If "fnum_list" is not NULL, use "fnum_list[fnum_len]" as a list of buffer
6515 * numbers to be re-used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006516 */
6517 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006518alist_expand(int *fnum_list, int fnum_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006519{
6520 char_u **old_arg_files;
Bram Moolenaar86b68352004-12-27 21:59:20 +00006521 int old_arg_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006522 char_u **new_arg_files;
6523 int new_arg_file_count;
6524 char_u *save_p_su = p_su;
6525 int i;
6526
6527 /* Don't use 'suffixes' here. This should work like the shell did the
6528 * expansion. Also, the vimrc file isn't read yet, thus the user
6529 * can't set the options. */
6530 p_su = empty_option;
6531 old_arg_files = (char_u **)alloc((unsigned)(sizeof(char_u *) * GARGCOUNT));
6532 if (old_arg_files != NULL)
6533 {
6534 for (i = 0; i < GARGCOUNT; ++i)
Bram Moolenaar86b68352004-12-27 21:59:20 +00006535 old_arg_files[i] = vim_strsave(GARGLIST[i].ae_fname);
6536 old_arg_count = GARGCOUNT;
6537 if (expand_wildcards(old_arg_count, old_arg_files,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006538 &new_arg_file_count, &new_arg_files,
Bram Moolenaarb5609832011-07-20 15:04:58 +02006539 EW_FILE|EW_NOTFOUND|EW_ADDSLASH|EW_NOERROR) == OK
Bram Moolenaar071d4272004-06-13 20:20:40 +00006540 && new_arg_file_count > 0)
6541 {
Bram Moolenaar86b68352004-12-27 21:59:20 +00006542 alist_set(&global_alist, new_arg_file_count, new_arg_files,
6543 TRUE, fnum_list, fnum_len);
6544 FreeWild(old_arg_count, old_arg_files);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006545 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006546 }
6547 p_su = save_p_su;
6548}
6549#endif
6550
6551/*
6552 * Set the argument list for the current window.
6553 * Takes over the allocated files[] and the allocated fnames in it.
6554 */
6555 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006556alist_set(
6557 alist_T *al,
6558 int count,
6559 char_u **files,
6560 int use_curbuf,
6561 int *fnum_list,
6562 int fnum_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006563{
6564 int i;
Bram Moolenaar9e33efd2018-02-09 17:50:28 +01006565 static int recursive = 0;
6566
6567 if (recursive)
6568 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006569 emsg(_(e_au_recursive));
Bram Moolenaar9e33efd2018-02-09 17:50:28 +01006570 return;
6571 }
6572 ++recursive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006573
6574 alist_clear(al);
6575 if (ga_grow(&al->al_ga, count) == OK)
6576 {
6577 for (i = 0; i < count; ++i)
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00006578 {
6579 if (got_int)
6580 {
6581 /* When adding many buffers this can take a long time. Allow
6582 * interrupting here. */
6583 while (i < count)
6584 vim_free(files[i++]);
6585 break;
6586 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00006587
6588 /* May set buffer name of a buffer previously used for the
6589 * argument list, so that it's re-used by alist_add. */
6590 if (fnum_list != NULL && i < fnum_len)
6591 buf_set_name(fnum_list[i], files[i]);
6592
Bram Moolenaar071d4272004-06-13 20:20:40 +00006593 alist_add(al, files[i], use_curbuf ? 2 : 1);
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00006594 ui_breakcheck();
6595 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006596 vim_free(files);
6597 }
6598 else
6599 FreeWild(count, files);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006600 if (al == &global_alist)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006601 arg_had_last = FALSE;
Bram Moolenaar9e33efd2018-02-09 17:50:28 +01006602
6603 --recursive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006604}
6605
6606/*
6607 * Add file "fname" to argument list "al".
6608 * "fname" must have been allocated and "al" must have been checked for room.
6609 */
6610 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006611alist_add(
6612 alist_T *al,
6613 char_u *fname,
6614 int set_fnum) /* 1: set buffer number; 2: re-use curbuf */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006615{
6616 if (fname == NULL) /* don't add NULL file names */
6617 return;
6618#ifdef BACKSLASH_IN_FILENAME
6619 slash_adjust(fname);
6620#endif
6621 AARGLIST(al)[al->al_ga.ga_len].ae_fname = fname;
6622 if (set_fnum > 0)
6623 AARGLIST(al)[al->al_ga.ga_len].ae_fnum =
6624 buflist_add(fname, BLN_LISTED | (set_fnum == 2 ? BLN_CURBUF : 0));
6625 ++al->al_ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006626}
6627
6628#if defined(BACKSLASH_IN_FILENAME) || defined(PROTO)
6629/*
6630 * Adjust slashes in file names. Called after 'shellslash' was set.
6631 */
6632 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006633alist_slash_adjust(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006634{
6635 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006636 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00006637 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006638
6639 for (i = 0; i < GARGCOUNT; ++i)
6640 if (GARGLIST[i].ae_fname != NULL)
6641 slash_adjust(GARGLIST[i].ae_fname);
Bram Moolenaarf740b292006-02-16 22:11:02 +00006642 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006643 if (wp->w_alist != &global_alist)
6644 for (i = 0; i < WARGCOUNT(wp); ++i)
6645 if (WARGLIST(wp)[i].ae_fname != NULL)
6646 slash_adjust(WARGLIST(wp)[i].ae_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006647}
6648#endif
6649
6650/*
6651 * ":preserve".
6652 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006653 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006654ex_preserve(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006655{
Bram Moolenaar4399ef42005-02-12 14:29:27 +00006656 curbuf->b_flags |= BF_PRESERVED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006657 ml_preserve(curbuf, TRUE);
6658}
6659
6660/*
6661 * ":recover".
6662 */
6663 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006664ex_recover(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006665{
6666 /* Set recoverymode right away to avoid the ATTENTION prompt. */
6667 recoverymode = TRUE;
Bram Moolenaar45d3b142013-11-09 03:31:51 +01006668 if (!check_changed(curbuf, (p_awa ? CCGD_AW : 0)
6669 | CCGD_MULTWIN
6670 | (eap->forceit ? CCGD_FORCEIT : 0)
6671 | CCGD_EXCMD)
6672
Bram Moolenaar071d4272004-06-13 20:20:40 +00006673 && (*eap->arg == NUL
6674 || setfname(curbuf, eap->arg, NULL, TRUE) == OK))
6675 ml_recover();
6676 recoverymode = FALSE;
6677}
6678
6679/*
6680 * Command modifier used in a wrong way.
6681 */
6682 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006683ex_wrongmodifier(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006684{
6685 eap->errmsg = e_invcmd;
6686}
6687
Bram Moolenaar071d4272004-06-13 20:20:40 +00006688/*
6689 * :sview [+command] file split window with new file, read-only
6690 * :split [[+command] file] split window with current or new file
6691 * :vsplit [[+command] file] split window vertically with current or new file
6692 * :new [[+command] file] split window with no or new file
6693 * :vnew [[+command] file] split vertically window with no or new file
6694 * :sfind [+command] file split window with file in 'path'
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006695 *
6696 * :tabedit open new Tab page with empty window
6697 * :tabedit [+command] file open new Tab page and edit "file"
6698 * :tabnew [[+command] file] just like :tabedit
6699 * :tabfind [+command] file open new Tab page and find "file"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006700 */
6701 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006702ex_splitview(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006703{
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006704 win_T *old_curwin = curwin;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006705#if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006706 char_u *fname = NULL;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006707#endif
6708#ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00006709 int browse_flag = cmdmod.browse;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006710#endif
Bram Moolenaar39902a02018-06-21 22:10:08 +02006711 int use_tab = eap->cmdidx == CMD_tabedit
6712 || eap->cmdidx == CMD_tabfind
6713 || eap->cmdidx == CMD_tabnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006714
Bram Moolenaar4033c552017-09-16 20:54:51 +02006715#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00006716 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006717#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006718
Bram Moolenaar4033c552017-09-16 20:54:51 +02006719#ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00006720 /* A ":split" in the quickfix window works like ":new". Don't want two
Bram Moolenaar05bb9532008-07-04 09:44:11 +00006721 * quickfix windows. But it's OK when doing ":tab split". */
6722 if (bt_quickfix(curbuf) && cmdmod.tab == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006723 {
6724 if (eap->cmdidx == CMD_split)
6725 eap->cmdidx = CMD_new;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006726 if (eap->cmdidx == CMD_vsplit)
6727 eap->cmdidx = CMD_vnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006728 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02006729#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006730
Bram Moolenaar4033c552017-09-16 20:54:51 +02006731#ifdef FEAT_SEARCHPATH
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006732 if (eap->cmdidx == CMD_sfind || eap->cmdidx == CMD_tabfind)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006733 {
6734 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg),
6735 FNAME_MESS, TRUE, curbuf->b_ffname);
6736 if (fname == NULL)
6737 goto theend;
6738 eap->arg = fname;
6739 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006740# ifdef FEAT_BROWSE
Bram Moolenaar4033c552017-09-16 20:54:51 +02006741 else
6742# endif
6743#endif
6744#ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00006745 if (cmdmod.browse
Bram Moolenaar071d4272004-06-13 20:20:40 +00006746 && eap->cmdidx != CMD_vnew
Bram Moolenaar071d4272004-06-13 20:20:40 +00006747 && eap->cmdidx != CMD_new)
6748 {
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006749 if (
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006750# ifdef FEAT_GUI
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006751 !gui.in_use &&
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006752# endif
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006753 au_has_group((char_u *)"FileExplorer"))
6754 {
6755 /* No browsing supported but we do have the file explorer:
6756 * Edit the directory. */
6757 if (*eap->arg == NUL || !mch_isdir(eap->arg))
6758 eap->arg = (char_u *)".";
6759 }
6760 else
6761 {
Bram Moolenaar39902a02018-06-21 22:10:08 +02006762 fname = do_browse(0, (char_u *)(use_tab
6763 ? _("Edit File in new tab page")
6764 : _("Edit File in new window")),
Bram Moolenaar071d4272004-06-13 20:20:40 +00006765 eap->arg, NULL, NULL, NULL, curbuf);
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006766 if (fname == NULL)
6767 goto theend;
6768 eap->arg = fname;
6769 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006770 }
6771 cmdmod.browse = FALSE; /* Don't browse again in do_ecmd(). */
Bram Moolenaar4033c552017-09-16 20:54:51 +02006772#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006773
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006774 /*
6775 * Either open new tab page or split the window.
6776 */
Bram Moolenaar39902a02018-06-21 22:10:08 +02006777 if (use_tab)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006778 {
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006779 if (win_new_tabpage(cmdmod.tab != 0 ? cmdmod.tab
6780 : eap->addr_count == 0 ? 0
6781 : (int)eap->line2 + 1) != FAIL)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006782 {
Bram Moolenaard2b66012008-01-09 19:30:36 +00006783 do_exedit(eap, old_curwin);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006784
6785 /* set the alternate buffer for the window we came from */
6786 if (curwin != old_curwin
6787 && win_valid(old_curwin)
6788 && old_curwin->w_buffer != curbuf
6789 && !cmdmod.keepalt)
6790 old_curwin->w_alt_fnum = curbuf->b_fnum;
6791 }
6792 }
6793 else if (win_split(eap->addr_count > 0 ? (int)eap->line2 : 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006794 *eap->cmd == 'v' ? WSP_VERT : 0) != FAIL)
6795 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006796 /* Reset 'scrollbind' when editing another file, but keep it when
6797 * doing ":split" without arguments. */
6798 if (*eap->arg != NUL
Bram Moolenaar8a3bb562018-03-04 20:14:14 +01006799# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00006800 || cmdmod.browse
Bram Moolenaar8a3bb562018-03-04 20:14:14 +01006801# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006802 )
Bram Moolenaar3368ea22010-09-21 16:56:35 +02006803 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006804 else
6805 do_check_scrollbind(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006806 do_exedit(eap, old_curwin);
6807 }
6808
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006809# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00006810 cmdmod.browse = browse_flag;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006811# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006812
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006813# if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006814theend:
6815 vim_free(fname);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006816# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006817}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006818
6819/*
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006820 * Open a new tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006821 */
6822 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006823tabpage_new(void)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006824{
6825 exarg_T ea;
6826
6827 vim_memset(&ea, 0, sizeof(ea));
6828 ea.cmdidx = CMD_tabnew;
6829 ea.cmd = (char_u *)"tabn";
6830 ea.arg = (char_u *)"";
6831 ex_splitview(&ea);
6832}
6833
6834/*
6835 * :tabnext command
6836 */
6837 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006838ex_tabnext(exarg_T *eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006839{
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006840 int tab_number;
6841
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006842 switch (eap->cmdidx)
6843 {
6844 case CMD_tabfirst:
6845 case CMD_tabrewind:
6846 goto_tabpage(1);
6847 break;
6848 case CMD_tablast:
6849 goto_tabpage(9999);
6850 break;
6851 case CMD_tabprevious:
6852 case CMD_tabNext:
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006853 if (eap->arg && *eap->arg != NUL)
6854 {
6855 char_u *p = eap->arg;
6856 char_u *p_save = p;
6857
6858 tab_number = getdigits(&p);
6859 if (p == p_save || *p_save == '-' || *p != NUL
6860 || tab_number == 0)
6861 {
6862 /* No numbers as argument. */
6863 eap->errmsg = e_invarg;
6864 return;
6865 }
6866 }
6867 else
6868 {
6869 if (eap->addr_count == 0)
6870 tab_number = 1;
6871 else
6872 {
6873 tab_number = eap->line2;
6874 if (tab_number < 1)
6875 {
6876 eap->errmsg = e_invrange;
6877 return;
6878 }
6879 }
6880 }
6881 goto_tabpage(-tab_number);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006882 break;
6883 default: /* CMD_tabnext */
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006884 tab_number = get_tabpage_arg(eap);
6885 if (eap->errmsg == NULL)
6886 goto_tabpage(tab_number);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006887 break;
6888 }
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006889}
6890
6891/*
6892 * :tabmove command
6893 */
6894 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006895ex_tabmove(exarg_T *eap)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006896{
Bram Moolenaar40ce3a42015-04-21 18:08:39 +02006897 int tab_number;
Bram Moolenaar8cb8dca2012-07-06 18:27:39 +02006898
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006899 tab_number = get_tabpage_arg(eap);
6900 if (eap->errmsg == NULL)
6901 tabpage_move(tab_number);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006902}
6903
6904/*
6905 * :tabs command: List tabs and their contents.
6906 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006907 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006908ex_tabs(exarg_T *eap UNUSED)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006909{
6910 tabpage_T *tp;
6911 win_T *wp;
6912 int tabcount = 1;
6913
6914 msg_start();
6915 msg_scroll = TRUE;
6916 for (tp = first_tabpage; tp != NULL && !got_int; tp = tp->tp_next)
6917 {
6918 msg_putchar('\n');
6919 vim_snprintf((char *)IObuff, IOSIZE, _("Tab page %d"), tabcount++);
Bram Moolenaar8820b482017-03-16 17:23:31 +01006920 msg_outtrans_attr(IObuff, HL_ATTR(HLF_T));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006921 out_flush(); /* output one line at a time */
6922 ui_breakcheck();
6923
Bram Moolenaar030f0df2006-02-21 22:02:53 +00006924 if (tp == curtab)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006925 wp = firstwin;
6926 else
6927 wp = tp->tp_firstwin;
6928 for ( ; wp != NULL && !got_int; wp = wp->w_next)
6929 {
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006930 msg_putchar('\n');
6931 msg_putchar(wp == curwin ? '>' : ' ');
6932 msg_putchar(' ');
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006933 msg_putchar(bufIsChanged(wp->w_buffer) ? '+' : ' ');
6934 msg_putchar(' ');
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006935 if (buf_spname(wp->w_buffer) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02006936 vim_strncpy(IObuff, buf_spname(wp->w_buffer), IOSIZE - 1);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006937 else
6938 home_replace(wp->w_buffer, wp->w_buffer->b_fname,
6939 IObuff, IOSIZE, TRUE);
6940 msg_outtrans(IObuff);
6941 out_flush(); /* output one line at a time */
6942 ui_breakcheck();
6943 }
6944 }
6945}
6946
Bram Moolenaar071d4272004-06-13 20:20:40 +00006947/*
6948 * ":mode": Set screen mode.
6949 * If no argument given, just get the screen size and redraw.
6950 */
6951 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006952ex_mode(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006953{
6954 if (*eap->arg == NUL)
6955 shell_resized();
6956 else
6957 mch_screenmode(eap->arg);
6958}
6959
Bram Moolenaar071d4272004-06-13 20:20:40 +00006960/*
6961 * ":resize".
6962 * set, increment or decrement current window height
6963 */
6964 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006965ex_resize(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006966{
6967 int n;
6968 win_T *wp = curwin;
6969
6970 if (eap->addr_count > 0)
6971 {
6972 n = eap->line2;
6973 for (wp = firstwin; wp->w_next != NULL && --n > 0; wp = wp->w_next)
6974 ;
6975 }
6976
Bram Moolenaar44a2f922016-03-19 22:11:51 +01006977# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00006978 need_mouse_correct = TRUE;
Bram Moolenaar44a2f922016-03-19 22:11:51 +01006979# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006980 n = atol((char *)eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006981 if (cmdmod.split & WSP_VERT)
6982 {
6983 if (*eap->arg == '-' || *eap->arg == '+')
Bram Moolenaar02631462017-09-22 15:20:32 +02006984 n += curwin->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006985 else if (n == 0 && eap->arg[0] == NUL) /* default is very wide */
6986 n = 9999;
6987 win_setwidth_win((int)n, wp);
6988 }
6989 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006990 {
6991 if (*eap->arg == '-' || *eap->arg == '+')
6992 n += curwin->w_height;
Bram Moolenaar1f2903c2017-07-23 19:51:01 +02006993 else if (n == 0 && eap->arg[0] == NUL) /* default is very high */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006994 n = 9999;
6995 win_setheight_win((int)n, wp);
6996 }
6997}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006998
6999/*
7000 * ":find [+command] <file>" command.
7001 */
7002 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007003ex_find(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007004{
7005#ifdef FEAT_SEARCHPATH
7006 char_u *fname;
7007 int count;
7008
7009 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg), FNAME_MESS,
7010 TRUE, curbuf->b_ffname);
7011 if (eap->addr_count > 0)
7012 {
7013 /* Repeat finding the file "count" times. This matters when it
7014 * appears several times in the path. */
7015 count = eap->line2;
7016 while (fname != NULL && --count > 0)
7017 {
7018 vim_free(fname);
7019 fname = find_file_in_path(NULL, 0, FNAME_MESS,
7020 FALSE, curbuf->b_ffname);
7021 }
7022 }
7023
7024 if (fname != NULL)
7025 {
7026 eap->arg = fname;
7027#endif
7028 do_exedit(eap, NULL);
7029#ifdef FEAT_SEARCHPATH
7030 vim_free(fname);
7031 }
7032#endif
7033}
7034
7035/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00007036 * ":open" simulation: for now just work like ":visual".
7037 */
7038 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007039ex_open(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007040{
7041 regmatch_T regmatch;
7042 char_u *p;
7043
7044 curwin->w_cursor.lnum = eap->line2;
7045 beginline(BL_SOL | BL_FIX);
7046 if (*eap->arg == '/')
7047 {
7048 /* ":open /pattern/": put cursor in column found with pattern */
7049 ++eap->arg;
7050 p = skip_regexp(eap->arg, '/', p_magic, NULL);
7051 *p = NUL;
7052 regmatch.regprog = vim_regcomp(eap->arg, p_magic ? RE_MAGIC : 0);
7053 if (regmatch.regprog != NULL)
7054 {
7055 regmatch.rm_ic = p_ic;
7056 p = ml_get_curline();
7057 if (vim_regexec(&regmatch, p, (colnr_T)0))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007058 curwin->w_cursor.col = (colnr_T)(regmatch.startp[0] - p);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007059 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007060 emsg(_(e_nomatch));
Bram Moolenaar473de612013-06-08 18:19:48 +02007061 vim_regfree(regmatch.regprog);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007062 }
7063 /* Move to the NUL, ignore any other arguments. */
7064 eap->arg += STRLEN(eap->arg);
7065 }
7066 check_cursor();
7067
7068 eap->cmdidx = CMD_visual;
7069 do_exedit(eap, NULL);
7070}
7071
7072/*
7073 * ":edit", ":badd", ":visual".
Bram Moolenaar071d4272004-06-13 20:20:40 +00007074 */
7075 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007076ex_edit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007077{
7078 do_exedit(eap, NULL);
7079}
7080
7081/*
7082 * ":edit <file>" command and alikes.
7083 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007084 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007085do_exedit(
7086 exarg_T *eap,
7087 win_T *old_curwin) /* curwin before doing a split or NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007088{
7089 int n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007090 int need_hide;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007091 int exmode_was = exmode_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007092
7093 /*
7094 * ":vi" command ends Ex mode.
7095 */
7096 if (exmode_active && (eap->cmdidx == CMD_visual
7097 || eap->cmdidx == CMD_view))
7098 {
7099 exmode_active = FALSE;
7100 if (*eap->arg == NUL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007101 {
7102 /* Special case: ":global/pat/visual\NLvi-commands" */
7103 if (global_busy)
7104 {
7105 int rd = RedrawingDisabled;
7106 int nwr = no_wait_return;
7107 int ms = msg_scroll;
7108#ifdef FEAT_GUI
7109 int he = hold_gui_events;
7110#endif
7111
7112 if (eap->nextcmd != NULL)
7113 {
7114 stuffReadbuff(eap->nextcmd);
7115 eap->nextcmd = NULL;
7116 }
7117
7118 if (exmode_was != EXMODE_VIM)
7119 settmode(TMODE_RAW);
7120 RedrawingDisabled = 0;
7121 no_wait_return = 0;
7122 need_wait_return = FALSE;
7123 msg_scroll = 0;
7124#ifdef FEAT_GUI
7125 hold_gui_events = 0;
7126#endif
7127 must_redraw = CLEAR;
7128
7129 main_loop(FALSE, TRUE);
7130
7131 RedrawingDisabled = rd;
7132 no_wait_return = nwr;
7133 msg_scroll = ms;
7134#ifdef FEAT_GUI
7135 hold_gui_events = he;
7136#endif
7137 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007138 return;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007139 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007140 }
7141
7142 if ((eap->cmdidx == CMD_new
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007143 || eap->cmdidx == CMD_tabnew
7144 || eap->cmdidx == CMD_tabedit
Bram Moolenaar4033c552017-09-16 20:54:51 +02007145 || eap->cmdidx == CMD_vnew) && *eap->arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007146 {
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007147 /* ":new" or ":tabnew" without argument: edit an new empty buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007148 setpcmark();
7149 (void)do_ecmd(0, NULL, NULL, eap, ECMD_ONE,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00007150 ECMD_HIDE + (eap->forceit ? ECMD_FORCEIT : 0),
7151 old_curwin == NULL ? curwin : NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007152 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02007153 else if ((eap->cmdidx != CMD_split && eap->cmdidx != CMD_vsplit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007154 || *eap->arg != NUL
7155#ifdef FEAT_BROWSE
7156 || cmdmod.browse
7157#endif
7158 )
7159 {
Bram Moolenaar5555acc2006-04-07 21:33:12 +00007160 /* Can't edit another file when "curbuf_lock" is set. Only ":edit"
7161 * can bring us here, others are stopped earlier. */
7162 if (*eap->arg != NUL && curbuf_locked())
7163 return;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007164
Bram Moolenaar071d4272004-06-13 20:20:40 +00007165 n = readonlymode;
7166 if (eap->cmdidx == CMD_view || eap->cmdidx == CMD_sview)
7167 readonlymode = TRUE;
7168 else if (eap->cmdidx == CMD_enew)
7169 readonlymode = FALSE; /* 'readonly' doesn't make sense in an
7170 empty buffer */
7171 setpcmark();
7172 if (do_ecmd(0, (eap->cmdidx == CMD_enew ? NULL : eap->arg),
7173 NULL, eap,
7174 /* ":edit" goes to first line if Vi compatible */
7175 (*eap->arg == NUL && eap->do_ecmd_lnum == 0
7176 && vim_strchr(p_cpo, CPO_GOTO1) != NULL)
7177 ? ECMD_ONE : eap->do_ecmd_lnum,
Bram Moolenaareb44a682017-08-03 22:44:55 +02007178 (buf_hide(curbuf) ? ECMD_HIDE : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007179 + (eap->forceit ? ECMD_FORCEIT : 0)
Bram Moolenaar7b449342014-03-25 13:03:48 +01007180 /* after a split we can use an existing buffer */
7181 + (old_curwin != NULL ? ECMD_OLDBUF : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007182 + (eap->cmdidx == CMD_badd ? ECMD_ADDBUF : 0 )
Bram Moolenaar701f7af2008-11-15 13:12:07 +00007183 , old_curwin == NULL ? curwin : NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007184 {
7185 /* Editing the file failed. If the window was split, close it. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007186 if (old_curwin != NULL)
7187 {
7188 need_hide = (curbufIsChanged() && curbuf->b_nwindows <= 1);
Bram Moolenaareb44a682017-08-03 22:44:55 +02007189 if (!need_hide || buf_hide(curbuf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007190 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007191#if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007192 cleanup_T cs;
7193
7194 /* Reset the error/interrupt/exception state here so that
7195 * aborting() returns FALSE when closing a window. */
7196 enter_cleanup(&cs);
Bram Moolenaar4033c552017-09-16 20:54:51 +02007197#endif
7198#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007199 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02007200#endif
Bram Moolenaareb44a682017-08-03 22:44:55 +02007201 win_close(curwin, !need_hide && !buf_hide(curbuf));
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007202
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007203#if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007204 /* Restore the error/interrupt/exception state if not
7205 * discarded by a new aborting error, interrupt, or
7206 * uncaught exception. */
7207 leave_cleanup(&cs);
Bram Moolenaar4033c552017-09-16 20:54:51 +02007208#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007209 }
7210 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007211 }
7212 else if (readonlymode && curbuf->b_nwindows == 1)
7213 {
7214 /* When editing an already visited buffer, 'readonly' won't be set
7215 * but the previous value is kept. With ":view" and ":sview" we
7216 * want the file to be readonly, except when another window is
7217 * editing the same buffer. */
7218 curbuf->b_p_ro = TRUE;
7219 }
7220 readonlymode = n;
7221 }
7222 else
7223 {
7224 if (eap->do_ecmd_cmd != NULL)
7225 do_cmdline_cmd(eap->do_ecmd_cmd);
7226#ifdef FEAT_TITLE
7227 n = curwin->w_arg_idx_invalid;
7228#endif
7229 check_arg_idx(curwin);
7230#ifdef FEAT_TITLE
7231 if (n != curwin->w_arg_idx_invalid)
7232 maketitle();
7233#endif
7234 }
7235
Bram Moolenaar071d4272004-06-13 20:20:40 +00007236 /*
7237 * if ":split file" worked, set alternate file name in old window to new
7238 * file
7239 */
7240 if (old_curwin != NULL
7241 && *eap->arg != NUL
7242 && curwin != old_curwin
7243 && win_valid(old_curwin)
Bram Moolenaard4755bb2004-09-02 19:12:26 +00007244 && old_curwin->w_buffer != curbuf
7245 && !cmdmod.keepalt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007246 old_curwin->w_alt_fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007247
7248 ex_no_reprint = TRUE;
7249}
7250
7251#ifndef FEAT_GUI
7252/*
7253 * ":gui" and ":gvim" when there is no GUI.
7254 */
7255 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007256ex_nogui(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007257{
7258 eap->errmsg = e_nogvim;
7259}
7260#endif
7261
Bram Moolenaar4f974752019-02-17 17:44:42 +01007262#if defined(FEAT_GUI_MSWIN) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007263 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007264ex_tearoff(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007265{
7266 gui_make_tearoff(eap->arg);
7267}
7268#endif
7269
Bram Moolenaar29a2c082018-03-05 21:06:23 +01007270#if (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK) \
7271 || defined(FEAT_TERM_POPUP_MENU)) && defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007272 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007273ex_popup(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007274{
Bram Moolenaar29a2c082018-03-05 21:06:23 +01007275# if defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK)
7276 if (gui.in_use)
7277 gui_make_popup(eap->arg, eap->forceit);
7278# ifdef FEAT_TERM_POPUP_MENU
7279 else
7280# endif
7281# endif
7282# ifdef FEAT_TERM_POPUP_MENU
7283 pum_make_popup(eap->arg, eap->forceit);
7284# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007285}
7286#endif
7287
Bram Moolenaar071d4272004-06-13 20:20:40 +00007288 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007289ex_swapname(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007290{
7291 if (curbuf->b_ml.ml_mfp == NULL || curbuf->b_ml.ml_mfp->mf_fname == NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01007292 msg(_("No swap file"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007293 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01007294 msg((char *)curbuf->b_ml.ml_mfp->mf_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007295}
7296
7297/*
7298 * ":syncbind" forces all 'scrollbind' windows to have the same relative
7299 * offset.
7300 * (1998-11-02 16:21:01 R. Edward Ralston <eralston@computer.org>)
7301 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007302 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007303ex_syncbind(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007304{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007305 win_T *wp;
Bram Moolenaardedd1b02013-12-14 13:06:17 +01007306 win_T *save_curwin = curwin;
7307 buf_T *save_curbuf = curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007308 long topline;
7309 long y;
7310 linenr_T old_linenr = curwin->w_cursor.lnum;
7311
7312 setpcmark();
7313
7314 /*
7315 * determine max topline
7316 */
7317 if (curwin->w_p_scb)
7318 {
7319 topline = curwin->w_topline;
Bram Moolenaar29323592016-07-24 22:04:11 +02007320 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007321 {
7322 if (wp->w_p_scb && wp->w_buffer)
7323 {
Bram Moolenaar375e3392019-01-31 18:26:10 +01007324 y = wp->w_buffer->b_ml.ml_line_count - get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007325 if (topline > y)
7326 topline = y;
7327 }
7328 }
7329 if (topline < 1)
7330 topline = 1;
7331 }
7332 else
7333 {
7334 topline = 1;
7335 }
7336
7337
7338 /*
Bram Moolenaardedd1b02013-12-14 13:06:17 +01007339 * Set all scrollbind windows to the same topline.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007340 */
Bram Moolenaar29323592016-07-24 22:04:11 +02007341 FOR_ALL_WINDOWS(curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007342 {
7343 if (curwin->w_p_scb)
7344 {
Bram Moolenaardedd1b02013-12-14 13:06:17 +01007345 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007346 y = topline - curwin->w_topline;
7347 if (y > 0)
7348 scrollup(y, TRUE);
7349 else
7350 scrolldown(-y, TRUE);
7351 curwin->w_scbind_pos = topline;
7352 redraw_later(VALID);
7353 cursor_correct();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007354 curwin->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007355 }
7356 }
Bram Moolenaardedd1b02013-12-14 13:06:17 +01007357 curwin = save_curwin;
7358 curbuf = save_curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007359 if (curwin->w_p_scb)
7360 {
7361 did_syncbind = TRUE;
7362 checkpcmark();
7363 if (old_linenr != curwin->w_cursor.lnum)
7364 {
7365 char_u ctrl_o[2];
7366
7367 ctrl_o[0] = Ctrl_O;
7368 ctrl_o[1] = 0;
7369 ins_typebuf(ctrl_o, REMAP_NONE, 0, TRUE, FALSE);
7370 }
7371 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007372}
7373
7374
7375 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007376ex_read(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007377{
Bram Moolenaardf177f62005-02-22 08:39:57 +00007378 int i;
7379 int empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
7380 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007381
7382 if (eap->usefilter) /* :r!cmd */
7383 do_bang(1, eap, FALSE, FALSE, TRUE);
7384 else
7385 {
7386 if (u_save(eap->line2, (linenr_T)(eap->line2 + 1)) == FAIL)
7387 return;
7388
7389#ifdef FEAT_BROWSE
7390 if (cmdmod.browse)
7391 {
7392 char_u *browseFile;
7393
Bram Moolenaar7171abe2004-10-11 10:06:20 +00007394 browseFile = do_browse(0, (char_u *)_("Append File"), eap->arg,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007395 NULL, NULL, NULL, curbuf);
7396 if (browseFile != NULL)
7397 {
7398 i = readfile(browseFile, NULL,
7399 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
7400 vim_free(browseFile);
7401 }
7402 else
7403 i = OK;
7404 }
7405 else
7406#endif
7407 if (*eap->arg == NUL)
7408 {
7409 if (check_fname() == FAIL) /* check for no file name */
7410 return;
7411 i = readfile(curbuf->b_ffname, curbuf->b_fname,
7412 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
7413 }
7414 else
7415 {
7416 if (vim_strchr(p_cpo, CPO_ALTREAD) != NULL)
7417 (void)setaltfname(eap->arg, eap->arg, (linenr_T)1);
7418 i = readfile(eap->arg, NULL,
7419 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
7420
7421 }
Bram Moolenaare13b9af2017-01-13 22:01:02 +01007422 if (i != OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007423 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007424#if defined(FEAT_EVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007425 if (!aborting())
7426#endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007427 semsg(_(e_notopen), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007428 }
7429 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00007430 {
7431 if (empty && exmode_active)
7432 {
7433 /* Delete the empty line that remains. Historically ex does
7434 * this but vi doesn't. */
7435 if (eap->line2 == 0)
7436 lnum = curbuf->b_ml.ml_line_count;
7437 else
7438 lnum = 1;
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00007439 if (*ml_get(lnum) == NUL && u_savedel(lnum, 1L) == OK)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007440 {
7441 ml_delete(lnum, FALSE);
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00007442 if (curwin->w_cursor.lnum > 1
7443 && curwin->w_cursor.lnum >= lnum)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007444 --curwin->w_cursor.lnum;
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00007445 deleted_lines_mark(lnum, 1L);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007446 }
7447 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007448 redraw_curbuf_later(VALID);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007449 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007450 }
7451}
7452
Bram Moolenaarea408852005-06-25 22:49:46 +00007453static char_u *prev_dir = NULL;
7454
7455#if defined(EXITFREE) || defined(PROTO)
7456 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007457free_cd_dir(void)
Bram Moolenaarea408852005-06-25 22:49:46 +00007458{
Bram Moolenaard23a8232018-02-10 18:45:26 +01007459 VIM_CLEAR(prev_dir);
7460 VIM_CLEAR(globaldir);
Bram Moolenaarea408852005-06-25 22:49:46 +00007461}
7462#endif
7463
Bram Moolenaarf4258302013-06-02 18:20:17 +02007464/*
7465 * Deal with the side effects of changing the current directory.
Bram Moolenaar00aa0692019-04-27 20:37:57 +02007466 * When "tablocal" is TRUE then this was after an ":tcd" command.
7467 * When "winlocal" is TRUE then this was after an ":lcd" command.
Bram Moolenaarf4258302013-06-02 18:20:17 +02007468 */
7469 void
Bram Moolenaar00aa0692019-04-27 20:37:57 +02007470post_chdir(int tablocal, int winlocal)
Bram Moolenaarf4258302013-06-02 18:20:17 +02007471{
Bram Moolenaar00aa0692019-04-27 20:37:57 +02007472 if (!winlocal)
7473 // Clear tab local directory for both :cd and :tcd
7474 VIM_CLEAR(curtab->tp_localdir);
Bram Moolenaard23a8232018-02-10 18:45:26 +01007475 VIM_CLEAR(curwin->w_localdir);
Bram Moolenaar00aa0692019-04-27 20:37:57 +02007476 if (winlocal || tablocal)
Bram Moolenaarf4258302013-06-02 18:20:17 +02007477 {
7478 /* If still in global directory, need to remember current
7479 * directory as global directory. */
7480 if (globaldir == NULL && prev_dir != NULL)
7481 globaldir = vim_strsave(prev_dir);
7482 /* Remember this local directory for the window. */
7483 if (mch_dirname(NameBuff, MAXPATHL) == OK)
Bram Moolenaar00aa0692019-04-27 20:37:57 +02007484 {
7485 if (tablocal)
7486 curtab->tp_localdir = vim_strsave(NameBuff);
7487 else
7488 curwin->w_localdir = vim_strsave(NameBuff);
7489 }
Bram Moolenaarf4258302013-06-02 18:20:17 +02007490 }
7491 else
7492 {
7493 /* We are now in the global directory, no need to remember its
7494 * name. */
Bram Moolenaard23a8232018-02-10 18:45:26 +01007495 VIM_CLEAR(globaldir);
Bram Moolenaarf4258302013-06-02 18:20:17 +02007496 }
7497
7498 shorten_fnames(TRUE);
7499}
7500
Bram Moolenaarea408852005-06-25 22:49:46 +00007501
Bram Moolenaar071d4272004-06-13 20:20:40 +00007502/*
Bram Moolenaar00aa0692019-04-27 20:37:57 +02007503 * ":cd", ":tcd", ":lcd", ":chdir" ":tchdir" and ":lchdir".
Bram Moolenaar071d4272004-06-13 20:20:40 +00007504 */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00007505 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007506ex_cd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007507{
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01007508 char_u *new_dir;
7509 char_u *tofree;
7510 int dir_differs;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007511
7512 new_dir = eap->arg;
7513#if !defined(UNIX) && !defined(VMS)
7514 /* for non-UNIX ":cd" means: print current directory */
7515 if (*new_dir == NUL)
7516 ex_pwd(NULL);
7517 else
7518#endif
7519 {
Bram Moolenaar8e8fe9b2009-03-11 14:37:32 +00007520 if (allbuf_locked())
7521 return;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007522 if (vim_strchr(p_cpo, CPO_CHDIR) != NULL && curbufIsChanged()
7523 && !eap->forceit)
7524 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007525 emsg(_("E747: Cannot change directory, buffer is modified (add ! to override)"));
Bram Moolenaardf177f62005-02-22 08:39:57 +00007526 return;
7527 }
7528
Bram Moolenaar071d4272004-06-13 20:20:40 +00007529 /* ":cd -": Change to previous directory */
7530 if (STRCMP(new_dir, "-") == 0)
7531 {
7532 if (prev_dir == NULL)
7533 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007534 emsg(_("E186: No previous directory"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007535 return;
7536 }
7537 new_dir = prev_dir;
7538 }
7539
7540 /* Save current directory for next ":cd -" */
7541 tofree = prev_dir;
7542 if (mch_dirname(NameBuff, MAXPATHL) == OK)
7543 prev_dir = vim_strsave(NameBuff);
7544 else
7545 prev_dir = NULL;
7546
7547#if defined(UNIX) || defined(VMS)
7548 /* for UNIX ":cd" means: go to home directory */
7549 if (*new_dir == NUL)
7550 {
7551 /* use NameBuff for home directory name */
7552# ifdef VMS
7553 char_u *p;
7554
7555 p = mch_getenv((char_u *)"SYS$LOGIN");
7556 if (p == NULL || *p == NUL) /* empty is the same as not set */
7557 NameBuff[0] = NUL;
7558 else
Bram Moolenaarb6356332005-07-18 21:40:44 +00007559 vim_strncpy(NameBuff, p, MAXPATHL - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007560# else
7561 expand_env((char_u *)"$HOME", NameBuff, MAXPATHL);
7562# endif
7563 new_dir = NameBuff;
7564 }
7565#endif
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01007566 dir_differs = new_dir == NULL || prev_dir == NULL
Bram Moolenaar9eb76af2018-12-16 16:30:21 +01007567 || pathcmp((char *)prev_dir, (char *)new_dir, -1) != 0;
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01007568 if (new_dir == NULL || (dir_differs && vim_chdir(new_dir)))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007569 emsg(_(e_failed));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007570 else
7571 {
Bram Moolenaar00aa0692019-04-27 20:37:57 +02007572 char_u *acmd_fname;
7573 int is_winlocal_chdir = eap->cmdidx == CMD_lcd
Bram Moolenaarb7407d32018-02-03 17:36:27 +01007574 || eap->cmdidx == CMD_lchdir;
Bram Moolenaar00aa0692019-04-27 20:37:57 +02007575 int is_tablocal_chdir = eap->cmdidx == CMD_tcd
7576 || eap->cmdidx == CMD_tchdir;
Bram Moolenaarb7407d32018-02-03 17:36:27 +01007577
Bram Moolenaar00aa0692019-04-27 20:37:57 +02007578 post_chdir(is_tablocal_chdir, is_winlocal_chdir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007579
7580 /* Echo the new current directory if the command was typed. */
Bram Moolenaarfcfbc672009-07-09 18:13:49 +00007581 if (KeyTyped || p_verbose >= 5)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007582 ex_pwd(eap);
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01007583
7584 if (dir_differs)
Bram Moolenaar00aa0692019-04-27 20:37:57 +02007585 {
7586 if (is_winlocal_chdir)
7587 acmd_fname = (char_u *)"window";
7588 else if (is_tablocal_chdir)
7589 acmd_fname = (char_u *)"tabpage";
7590 else
7591 acmd_fname = (char_u *)"global";
7592 apply_autocmds(EVENT_DIRCHANGED, acmd_fname,
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01007593 new_dir, FALSE, curbuf);
Bram Moolenaar00aa0692019-04-27 20:37:57 +02007594 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007595 }
7596 vim_free(tofree);
7597 }
7598}
7599
7600/*
7601 * ":pwd".
7602 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007603 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007604ex_pwd(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007605{
7606 if (mch_dirname(NameBuff, MAXPATHL) == OK)
7607 {
7608#ifdef BACKSLASH_IN_FILENAME
7609 slash_adjust(NameBuff);
7610#endif
Bram Moolenaar32526b32019-01-19 17:43:09 +01007611 msg((char *)NameBuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007612 }
7613 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007614 emsg(_("E187: Unknown"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007615}
7616
7617/*
7618 * ":=".
7619 */
7620 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007621ex_equal(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007622{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007623 smsg("%ld", (long)eap->line2);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007624 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007625}
7626
7627 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007628ex_sleep(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007629{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007630 int n;
7631 long len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007632
7633 if (cursor_valid())
7634 {
7635 n = W_WINROW(curwin) + curwin->w_wrow - msg_scrolled;
7636 if (n >= 0)
Bram Moolenaar53f81742017-09-22 14:35:51 +02007637 windgoto((int)n, curwin->w_wincol + curwin->w_wcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007638 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007639
7640 len = eap->line2;
7641 switch (*eap->arg)
7642 {
7643 case 'm': break;
7644 case NUL: len *= 1000L; break;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007645 default: semsg(_(e_invarg2), eap->arg); return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007646 }
7647 do_sleep(len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007648}
7649
7650/*
7651 * Sleep for "msec" milliseconds, but keep checking for a CTRL-C every second.
7652 */
7653 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007654do_sleep(long msec)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007655{
7656 long done;
Bram Moolenaar975b5272016-03-15 23:10:59 +01007657 long wait_now;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007658
7659 cursor_on();
Bram Moolenaarf45d7472018-09-30 18:22:26 +02007660 out_flush_cursor(FALSE, FALSE);
Bram Moolenaar975b5272016-03-15 23:10:59 +01007661 for (done = 0; !got_int && done < msec; done += wait_now)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007662 {
Bram Moolenaar975b5272016-03-15 23:10:59 +01007663 wait_now = msec - done > 1000L ? 1000L : msec - done;
7664#ifdef FEAT_TIMERS
7665 {
7666 long due_time = check_due_timer();
7667
7668 if (due_time > 0 && due_time < wait_now)
7669 wait_now = due_time;
7670 }
7671#endif
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02007672#ifdef FEAT_JOB_CHANNEL
7673 if (has_any_channel() && wait_now > 100L)
7674 wait_now = 100L;
7675#endif
Bram Moolenaar975b5272016-03-15 23:10:59 +01007676 ui_delay(wait_now, TRUE);
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02007677#ifdef FEAT_JOB_CHANNEL
7678 if (has_any_channel())
7679 ui_breakcheck_force(TRUE);
7680 else
7681#endif
7682 ui_breakcheck();
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007683#ifdef MESSAGE_QUEUE
7684 /* Process the netbeans and clientserver messages that may have been
7685 * received in the call to ui_breakcheck() when the GUI is in use. This
7686 * may occur when running a test case. */
7687 parse_queued_messages();
Bram Moolenaare3cc6d42011-10-20 21:58:34 +02007688#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007689 }
Bram Moolenaar1ebff3d2018-07-07 16:18:13 +02007690
7691 // If CTRL-C was typed to interrupt the sleep, drop the CTRL-C from the
7692 // input buffer, otherwise a following call to input() fails.
7693 if (got_int)
7694 (void)vpeekc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007695}
7696
7697 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007698do_exmap(exarg_T *eap, int isabbrev)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007699{
7700 int mode;
7701 char_u *cmdp;
7702
7703 cmdp = eap->cmd;
7704 mode = get_map_mode(&cmdp, eap->forceit || isabbrev);
7705
7706 switch (do_map((*cmdp == 'n') ? 2 : (*cmdp == 'u'),
7707 eap->arg, mode, isabbrev))
7708 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007709 case 1: emsg(_(e_invarg));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007710 break;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007711 case 2: emsg((isabbrev ? _(e_noabbr) : _(e_nomap)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007712 break;
7713 }
7714}
7715
7716/*
7717 * ":winsize" command (obsolete).
7718 */
7719 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007720ex_winsize(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007721{
7722 int w, h;
7723 char_u *arg = eap->arg;
7724 char_u *p;
7725
7726 w = getdigits(&arg);
7727 arg = skipwhite(arg);
7728 p = arg;
7729 h = getdigits(&arg);
7730 if (*p != NUL && *arg == NUL)
7731 set_shellsize(w, h, TRUE);
7732 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007733 emsg(_("E465: :winsize requires two number arguments"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007734}
7735
Bram Moolenaar071d4272004-06-13 20:20:40 +00007736 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007737ex_wincmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007738{
7739 int xchar = NUL;
7740 char_u *p;
7741
7742 if (*eap->arg == 'g' || *eap->arg == Ctrl_G)
7743 {
7744 /* CTRL-W g and CTRL-W CTRL-G have an extra command character */
7745 if (eap->arg[1] == NUL)
7746 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007747 emsg(_(e_invarg));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007748 return;
7749 }
7750 xchar = eap->arg[1];
7751 p = eap->arg + 2;
7752 }
7753 else
7754 p = eap->arg + 1;
7755
7756 eap->nextcmd = check_nextcmd(p);
7757 p = skipwhite(p);
7758 if (*p != NUL && *p != '"' && eap->nextcmd == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007759 emsg(_(e_invarg));
Bram Moolenaar12bde492011-06-13 01:19:56 +02007760 else if (!eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007761 {
7762 /* Pass flags on for ":vertical wincmd ]". */
7763 postponed_split_flags = cmdmod.split;
Bram Moolenaard326ce82007-03-11 14:48:29 +00007764 postponed_split_tab = cmdmod.tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007765 do_window(*eap->arg, eap->addr_count > 0 ? eap->line2 : 0L, xchar);
7766 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00007767 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007768 }
7769}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007770
Bram Moolenaar843ee412004-06-30 16:16:41 +00007771#if defined(FEAT_GUI) || defined(UNIX) || defined(VMS) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007772/*
7773 * ":winpos".
7774 */
7775 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007776ex_winpos(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007777{
7778 int x, y;
7779 char_u *arg = eap->arg;
7780 char_u *p;
7781
7782 if (*arg == NUL)
7783 {
Bram Moolenaar843ee412004-06-30 16:16:41 +00007784# if defined(FEAT_GUI) || defined(MSWIN)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007785# ifdef VIMDLL
7786 if (gui.in_use ? gui_mch_get_winpos(&x, &y) != FAIL :
7787 mch_get_winpos(&x, &y) != FAIL)
7788# elif defined(FEAT_GUI)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007789 if (gui.in_use && gui_mch_get_winpos(&x, &y) != FAIL)
Bram Moolenaar843ee412004-06-30 16:16:41 +00007790# else
7791 if (mch_get_winpos(&x, &y) != FAIL)
7792# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007793 {
7794 sprintf((char *)IObuff, _("Window position: X %d, Y %d"), x, y);
Bram Moolenaar32526b32019-01-19 17:43:09 +01007795 msg((char *)IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007796 }
7797 else
7798# endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007799 emsg(_("E188: Obtaining window position not implemented for this platform"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007800 }
7801 else
7802 {
7803 x = getdigits(&arg);
7804 arg = skipwhite(arg);
7805 p = arg;
7806 y = getdigits(&arg);
7807 if (*p == NUL || *arg != NUL)
7808 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007809 emsg(_("E466: :winpos requires two number arguments"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007810 return;
7811 }
7812# ifdef FEAT_GUI
7813 if (gui.in_use)
7814 gui_mch_set_winpos(x, y);
7815 else if (gui.starting)
7816 {
7817 /* Remember the coordinates for when the window is opened. */
7818 gui_win_x = x;
7819 gui_win_y = y;
7820 }
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007821# if defined(HAVE_TGETENT) || defined(VIMDLL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007822 else
7823# endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007824# endif
7825# if defined(MSWIN) && (!defined(FEAT_GUI) || defined(VIMDLL))
Bram Moolenaar843ee412004-06-30 16:16:41 +00007826 mch_set_winpos(x, y);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007827# endif
7828# ifdef HAVE_TGETENT
7829 if (*T_CWP)
7830 term_set_winpos(x, y);
7831# endif
7832 }
7833}
7834#endif
7835
7836/*
7837 * Handle command that work like operators: ":delete", ":yank", ":>" and ":<".
7838 */
7839 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007840ex_operators(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007841{
7842 oparg_T oa;
7843
7844 clear_oparg(&oa);
7845 oa.regname = eap->regname;
7846 oa.start.lnum = eap->line1;
7847 oa.end.lnum = eap->line2;
7848 oa.line_count = eap->line2 - eap->line1 + 1;
7849 oa.motion_type = MLINE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007850 virtual_op = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007851 if (eap->cmdidx != CMD_yank) /* position cursor for undo */
7852 {
7853 setpcmark();
7854 curwin->w_cursor.lnum = eap->line1;
7855 beginline(BL_SOL | BL_FIX);
7856 }
7857
Bram Moolenaard07c6e12013-11-21 14:21:40 +01007858 if (VIsual_active)
7859 end_visual_mode();
Bram Moolenaard07c6e12013-11-21 14:21:40 +01007860
Bram Moolenaar071d4272004-06-13 20:20:40 +00007861 switch (eap->cmdidx)
7862 {
7863 case CMD_delete:
7864 oa.op_type = OP_DELETE;
7865 op_delete(&oa);
7866 break;
7867
7868 case CMD_yank:
7869 oa.op_type = OP_YANK;
7870 (void)op_yank(&oa, FALSE, TRUE);
7871 break;
7872
7873 default: /* CMD_rshift or CMD_lshift */
Bram Moolenaar6e707362013-06-14 19:15:58 +02007874 if (
Bram Moolenaar071d4272004-06-13 20:20:40 +00007875#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6e707362013-06-14 19:15:58 +02007876 (eap->cmdidx == CMD_rshift) ^ curwin->w_p_rl
7877#else
7878 eap->cmdidx == CMD_rshift
Bram Moolenaar071d4272004-06-13 20:20:40 +00007879#endif
Bram Moolenaar6e707362013-06-14 19:15:58 +02007880 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00007881 oa.op_type = OP_RSHIFT;
7882 else
7883 oa.op_type = OP_LSHIFT;
7884 op_shift(&oa, FALSE, eap->amount);
7885 break;
7886 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007887 virtual_op = MAYBE;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007888 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007889}
7890
7891/*
7892 * ":put".
7893 */
7894 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007895ex_put(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007896{
7897 /* ":0put" works like ":1put!". */
7898 if (eap->line2 == 0)
7899 {
7900 eap->line2 = 1;
7901 eap->forceit = TRUE;
7902 }
7903 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007904 do_put(eap->regname, eap->forceit ? BACKWARD : FORWARD, 1L,
7905 PUT_LINE|PUT_CURSLINE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007906}
7907
7908/*
7909 * Handle ":copy" and ":move".
7910 */
7911 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007912ex_copymove(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007913{
7914 long n;
7915
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02007916 n = get_address(eap, &eap->arg, eap->addr_type, FALSE, FALSE, FALSE, 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007917 if (eap->arg == NULL) /* error detected */
7918 {
7919 eap->nextcmd = NULL;
7920 return;
7921 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00007922 get_flags(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007923
7924 /*
7925 * move or copy lines from 'eap->line1'-'eap->line2' to below line 'n'
7926 */
7927 if (n == MAXLNUM || n < 0 || n > curbuf->b_ml.ml_line_count)
7928 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007929 emsg(_(e_invaddr));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007930 return;
7931 }
7932
7933 if (eap->cmdidx == CMD_move)
7934 {
7935 if (do_move(eap->line1, eap->line2, n) == FAIL)
7936 return;
7937 }
7938 else
7939 ex_copy(eap->line1, eap->line2, n);
7940 u_clearline();
7941 beginline(BL_SOL | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007942 ex_may_print(eap);
7943}
7944
7945/*
7946 * Print the current line if flags were given to the Ex command.
7947 */
Bram Moolenaarfd3fe982014-04-01 17:49:44 +02007948 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007949ex_may_print(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007950{
7951 if (eap->flags != 0)
7952 {
7953 print_line(curwin->w_cursor.lnum, (eap->flags & EXFLAG_NR),
7954 (eap->flags & EXFLAG_LIST));
7955 ex_no_reprint = TRUE;
7956 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007957}
7958
7959/*
7960 * ":smagic" and ":snomagic".
7961 */
7962 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007963ex_submagic(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007964{
7965 int magic_save = p_magic;
7966
7967 p_magic = (eap->cmdidx == CMD_smagic);
7968 do_sub(eap);
7969 p_magic = magic_save;
7970}
7971
7972/*
7973 * ":join".
7974 */
7975 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007976ex_join(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007977{
7978 curwin->w_cursor.lnum = eap->line1;
7979 if (eap->line1 == eap->line2)
7980 {
7981 if (eap->addr_count >= 2) /* :2,2join does nothing */
7982 return;
7983 if (eap->line2 == curbuf->b_ml.ml_line_count)
7984 {
7985 beep_flush();
7986 return;
7987 }
7988 ++eap->line2;
7989 }
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02007990 (void)do_join(eap->line2 - eap->line1 + 1, !eap->forceit, TRUE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007991 beginline(BL_WHITE | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007992 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007993}
7994
7995/*
7996 * ":[addr]@r" or ":[addr]*r": execute register
7997 */
7998 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007999ex_at(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008000{
8001 int c;
Bram Moolenaar60462872009-11-03 11:40:19 +00008002 int prev_len = typebuf.tb_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008003
8004 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaar4930a762016-09-11 14:39:53 +02008005 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008006
8007#ifdef USE_ON_FLY_SCROLL
8008 dont_scroll = TRUE; /* disallow scrolling here */
8009#endif
8010
8011 /* get the register name. No name means to use the previous one */
8012 c = *eap->arg;
8013 if (c == NUL || (c == '*' && *eap->cmd == '*'))
8014 c = '@';
Bram Moolenaard333d1e2006-11-07 17:43:47 +00008015 /* Put the register in the typeahead buffer with the "silent" flag. */
8016 if (do_execreg(c, TRUE, vim_strchr(p_cpo, CPO_EXECBUF) != NULL, TRUE)
8017 == FAIL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008018 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008019 beep_flush();
Bram Moolenaardf177f62005-02-22 08:39:57 +00008020 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008021 else
8022 {
8023 int save_efr = exec_from_reg;
8024
8025 exec_from_reg = TRUE;
8026
8027 /*
8028 * Execute from the typeahead buffer.
Bram Moolenaar60462872009-11-03 11:40:19 +00008029 * Continue until the stuff buffer is empty and all added characters
8030 * have been consumed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008031 */
Bram Moolenaar60462872009-11-03 11:40:19 +00008032 while (!stuff_empty() || typebuf.tb_len > prev_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008033 (void)do_cmdline(NULL, getexline, NULL, DOCMD_NOWAIT|DOCMD_VERBOSE);
8034
8035 exec_from_reg = save_efr;
8036 }
8037}
8038
8039/*
8040 * ":!".
8041 */
8042 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008043ex_bang(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008044{
8045 do_bang(eap->addr_count, eap, eap->forceit, TRUE, TRUE);
8046}
8047
8048/*
8049 * ":undo".
8050 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008051 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +01008052ex_undo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008053{
Bram Moolenaard3667a22006-03-16 21:35:52 +00008054 if (eap->addr_count == 1) /* :undo 123 */
Bram Moolenaar730cde92010-06-27 05:18:54 +02008055 undo_time(eap->line2, FALSE, FALSE, TRUE);
Bram Moolenaard3667a22006-03-16 21:35:52 +00008056 else
8057 u_undo(1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008058}
8059
Bram Moolenaar55debbe2010-05-23 23:34:36 +02008060#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar6df6f472010-07-18 18:04:50 +02008061 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008062ex_wundo(exarg_T *eap)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02008063{
8064 char_u hash[UNDO_HASH_SIZE];
8065
8066 u_compute_hash(hash);
8067 u_write_undo(eap->arg, eap->forceit, curbuf, hash);
8068}
8069
Bram Moolenaar6df6f472010-07-18 18:04:50 +02008070 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008071ex_rundo(exarg_T *eap)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02008072{
8073 char_u hash[UNDO_HASH_SIZE];
8074
8075 u_compute_hash(hash);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02008076 u_read_undo(eap->arg, hash, NULL);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02008077}
8078#endif
8079
Bram Moolenaar071d4272004-06-13 20:20:40 +00008080/*
8081 * ":redo".
8082 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008083 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008084ex_redo(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008085{
8086 u_redo(1);
8087}
8088
8089/*
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008090 * ":earlier" and ":later".
8091 */
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008092 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008093ex_later(exarg_T *eap)
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008094{
8095 long count = 0;
8096 int sec = FALSE;
Bram Moolenaar730cde92010-06-27 05:18:54 +02008097 int file = FALSE;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008098 char_u *p = eap->arg;
8099
8100 if (*p == NUL)
8101 count = 1;
8102 else if (isdigit(*p))
8103 {
8104 count = getdigits(&p);
8105 switch (*p)
8106 {
8107 case 's': ++p; sec = TRUE; break;
8108 case 'm': ++p; sec = TRUE; count *= 60; break;
8109 case 'h': ++p; sec = TRUE; count *= 60 * 60; break;
Bram Moolenaar730cde92010-06-27 05:18:54 +02008110 case 'd': ++p; sec = TRUE; count *= 24 * 60 * 60; break;
8111 case 'f': ++p; file = TRUE; break;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008112 }
8113 }
8114
8115 if (*p != NUL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008116 semsg(_(e_invarg2), eap->arg);
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008117 else
Bram Moolenaar730cde92010-06-27 05:18:54 +02008118 undo_time(eap->cmdidx == CMD_earlier ? -count : count,
8119 sec, file, FALSE);
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008120}
8121
8122/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008123 * ":redir": start/stop redirection.
8124 */
8125 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008126ex_redir(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008127{
8128 char *mode;
8129 char_u *fname;
Bram Moolenaarca472992005-01-21 11:46:23 +00008130 char_u *arg = eap->arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008131
Bram Moolenaarba768492016-07-08 15:32:54 +02008132#ifdef FEAT_EVAL
Bram Moolenaar79815f12016-07-09 17:07:29 +02008133 if (redir_execute)
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02008134 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008135 emsg(_("E930: Cannot use :redir inside execute()"));
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02008136 return;
8137 }
Bram Moolenaarba768492016-07-08 15:32:54 +02008138#endif
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02008139
Bram Moolenaar071d4272004-06-13 20:20:40 +00008140 if (STRICMP(eap->arg, "END") == 0)
8141 close_redir();
8142 else
8143 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008144 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008145 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008146 ++arg;
8147 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008148 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008149 ++arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008150 mode = "a";
8151 }
8152 else
8153 mode = "w";
Bram Moolenaarca472992005-01-21 11:46:23 +00008154 arg = skipwhite(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008155
8156 close_redir();
8157
8158 /* Expand environment variables and "~/". */
Bram Moolenaarca472992005-01-21 11:46:23 +00008159 fname = expand_env_save(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008160 if (fname == NULL)
8161 return;
8162#ifdef FEAT_BROWSE
8163 if (cmdmod.browse)
8164 {
8165 char_u *browseFile;
8166
Bram Moolenaar7171abe2004-10-11 10:06:20 +00008167 browseFile = do_browse(BROWSE_SAVE,
8168 (char_u *)_("Save Redirection"),
Bram Moolenaarc36651b2018-04-29 12:22:56 +02008169 fname, NULL, NULL,
8170 (char_u *)_(BROWSE_FILTER_ALL_FILES), curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008171 if (browseFile == NULL)
8172 return; /* operation cancelled */
8173 vim_free(fname);
8174 fname = browseFile;
8175 eap->forceit = TRUE; /* since dialog already asked */
8176 }
8177#endif
8178
8179 redir_fd = open_exfile(fname, eap->forceit, mode);
8180 vim_free(fname);
8181 }
8182#ifdef FEAT_EVAL
Bram Moolenaarca472992005-01-21 11:46:23 +00008183 else if (*arg == '@')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008184 {
8185 /* redirect to a register a-z (resp. A-Z for appending) */
8186 close_redir();
Bram Moolenaarca472992005-01-21 11:46:23 +00008187 ++arg;
8188 if (ASCII_ISALPHA(*arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008189# ifdef FEAT_CLIPBOARD
Bram Moolenaarca472992005-01-21 11:46:23 +00008190 || *arg == '*'
Bram Moolenaar9a51c6e2006-11-14 19:25:02 +00008191 || *arg == '+'
Bram Moolenaar071d4272004-06-13 20:20:40 +00008192# endif
Bram Moolenaarca472992005-01-21 11:46:23 +00008193 || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008194 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008195 redir_reg = *arg++;
Bram Moolenaarc188b882007-10-19 14:20:54 +00008196 if (*arg == '>' && arg[1] == '>') /* append */
Bram Moolenaard9d30582005-05-18 22:10:28 +00008197 arg += 2;
Bram Moolenaarc188b882007-10-19 14:20:54 +00008198 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008199 {
Bram Moolenaarc188b882007-10-19 14:20:54 +00008200 /* Can use both "@a" and "@a>". */
Bram Moolenaar2c29bee2005-06-01 21:46:07 +00008201 if (*arg == '>')
8202 arg++;
Bram Moolenaarc188b882007-10-19 14:20:54 +00008203 /* Make register empty when not using @A-@Z and the
8204 * command is valid. */
8205 if (*arg == NUL && !isupper(redir_reg))
8206 write_reg_contents(redir_reg, (char_u *)"", -1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008207 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00008208 }
8209 if (*arg != NUL)
8210 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00008211 redir_reg = 0;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008212 semsg(_(e_invarg2), eap->arg);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008213 }
8214 }
8215 else if (*arg == '=' && arg[1] == '>')
8216 {
8217 int append;
8218
8219 /* redirect to a variable */
8220 close_redir();
8221 arg += 2;
8222
8223 if (*arg == '>')
8224 {
8225 ++arg;
8226 append = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008227 }
8228 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00008229 append = FALSE;
8230
8231 if (var_redir_start(skipwhite(arg), append) == OK)
8232 redir_vname = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008233 }
8234#endif
8235
8236 /* TODO: redirect to a buffer */
8237
Bram Moolenaar071d4272004-06-13 20:20:40 +00008238 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008239 semsg(_(e_invarg2), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008240 }
Bram Moolenaar29b2d262006-09-10 19:07:28 +00008241
8242 /* Make sure redirection is not off. Can happen for cmdline completion
8243 * that indirectly invokes a command to catch its output. */
8244 if (redir_fd != NULL
8245#ifdef FEAT_EVAL
8246 || redir_reg || redir_vname
8247#endif
8248 )
8249 redir_off = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008250}
8251
8252/*
8253 * ":redraw": force redraw
8254 */
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01008255 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008256ex_redraw(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008257{
8258 int r = RedrawingDisabled;
8259 int p = p_lz;
8260
8261 RedrawingDisabled = 0;
8262 p_lz = FALSE;
Bram Moolenaarabc39ab2017-03-01 18:04:05 +01008263 validate_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008264 update_topline();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01008265 update_screen(eap->forceit ? CLEAR : VIsual_active ? INVERTED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008266#ifdef FEAT_TITLE
8267 if (need_maketitle)
8268 maketitle();
8269#endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02008270#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
8271# ifdef VIMDLL
8272 if (!gui.in_use)
8273# endif
8274 resize_console_buf();
Bram Moolenaar78d21da2019-02-17 15:00:52 +01008275#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008276 RedrawingDisabled = r;
8277 p_lz = p;
8278
8279 /* Reset msg_didout, so that a message that's there is overwritten. */
8280 msg_didout = FALSE;
8281 msg_col = 0;
8282
Bram Moolenaar56667a52013-07-17 11:54:28 +02008283 /* No need to wait after an intentional redraw. */
8284 need_wait_return = FALSE;
8285
Bram Moolenaar071d4272004-06-13 20:20:40 +00008286 out_flush();
8287}
8288
8289/*
8290 * ":redrawstatus": force redraw of status line(s)
8291 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008292 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008293ex_redrawstatus(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008294{
Bram Moolenaar071d4272004-06-13 20:20:40 +00008295 int r = RedrawingDisabled;
8296 int p = p_lz;
8297
8298 RedrawingDisabled = 0;
8299 p_lz = FALSE;
8300 if (eap->forceit)
8301 status_redraw_all();
8302 else
8303 status_redraw_curbuf();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01008304 update_screen(VIsual_active ? INVERTED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008305 RedrawingDisabled = r;
8306 p_lz = p;
8307 out_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008308}
8309
Bram Moolenaare12bab32019-01-08 22:02:56 +01008310/*
8311 * ":redrawtabline": force redraw of the tabline
8312 */
8313 static void
8314ex_redrawtabline(exarg_T *eap UNUSED)
8315{
8316 int r = RedrawingDisabled;
8317 int p = p_lz;
8318
8319 RedrawingDisabled = 0;
8320 p_lz = FALSE;
8321
8322 draw_tabline();
8323
8324 RedrawingDisabled = r;
8325 p_lz = p;
8326 out_flush();
8327}
8328
Bram Moolenaar071d4272004-06-13 20:20:40 +00008329 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008330close_redir(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008331{
8332 if (redir_fd != NULL)
8333 {
8334 fclose(redir_fd);
8335 redir_fd = NULL;
8336 }
8337#ifdef FEAT_EVAL
8338 redir_reg = 0;
Bram Moolenaardf177f62005-02-22 08:39:57 +00008339 if (redir_vname)
8340 {
8341 var_redir_stop();
8342 redir_vname = 0;
8343 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008344#endif
8345}
8346
8347#if defined(FEAT_SESSION) && defined(USE_CRNL)
8348# define MKSESSION_NL
8349static int mksession_nl = FALSE; /* use NL only in put_eol() */
8350#endif
8351
8352/*
8353 * ":mkexrc", ":mkvimrc", ":mkview" and ":mksession".
8354 */
8355 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008356ex_mkrc(
8357 exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008358{
8359 FILE *fd;
8360 int failed = FALSE;
8361 char_u *fname;
8362#ifdef FEAT_BROWSE
8363 char_u *browseFile = NULL;
8364#endif
8365#ifdef FEAT_SESSION
8366 int view_session = FALSE;
8367 int using_vdir = FALSE; /* using 'viewdir'? */
8368 char_u *viewFile = NULL;
8369 unsigned *flagp;
8370#endif
8371
8372 if (eap->cmdidx == CMD_mksession || eap->cmdidx == CMD_mkview)
8373 {
8374#ifdef FEAT_SESSION
8375 view_session = TRUE;
8376#else
8377 ex_ni(eap);
8378 return;
8379#endif
8380 }
8381
8382#ifdef FEAT_SESSION
Bram Moolenaar8d594672009-07-01 18:18:57 +00008383 /* Use the short file name until ":lcd" is used. We also don't use the
8384 * short file name when 'acd' is set, that is checked later. */
Bram Moolenaareeefcc72007-05-01 21:21:21 +00008385 did_lcd = FALSE;
8386
Bram Moolenaar071d4272004-06-13 20:20:40 +00008387 /* ":mkview" or ":mkview 9": generate file name with 'viewdir' */
8388 if (eap->cmdidx == CMD_mkview
8389 && (*eap->arg == NUL
8390 || (vim_isdigit(*eap->arg) && eap->arg[1] == NUL)))
8391 {
8392 eap->forceit = TRUE;
8393 fname = get_view_file(*eap->arg);
8394 if (fname == NULL)
8395 return;
8396 viewFile = fname;
8397 using_vdir = TRUE;
8398 }
8399 else
8400#endif
8401 if (*eap->arg != NUL)
8402 fname = eap->arg;
8403 else if (eap->cmdidx == CMD_mkvimrc)
8404 fname = (char_u *)VIMRC_FILE;
8405#ifdef FEAT_SESSION
8406 else if (eap->cmdidx == CMD_mksession)
8407 fname = (char_u *)SESSION_FILE;
8408#endif
8409 else
8410 fname = (char_u *)EXRC_FILE;
8411
8412#ifdef FEAT_BROWSE
8413 if (cmdmod.browse)
8414 {
Bram Moolenaar7171abe2004-10-11 10:06:20 +00008415 browseFile = do_browse(BROWSE_SAVE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008416# ifdef FEAT_SESSION
8417 eap->cmdidx == CMD_mkview ? (char_u *)_("Save View") :
8418 eap->cmdidx == CMD_mksession ? (char_u *)_("Save Session") :
8419# endif
8420 (char_u *)_("Save Setup"),
Bram Moolenaarc36651b2018-04-29 12:22:56 +02008421 fname, (char_u *)"vim", NULL,
8422 (char_u *)_(BROWSE_FILTER_MACROS), NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008423 if (browseFile == NULL)
8424 goto theend;
8425 fname = browseFile;
8426 eap->forceit = TRUE; /* since dialog already asked */
8427 }
8428#endif
8429
8430#if defined(FEAT_SESSION) && defined(vim_mkdir)
8431 /* When using 'viewdir' may have to create the directory. */
8432 if (using_vdir && !mch_isdir(p_vdir))
Bram Moolenaardf177f62005-02-22 08:39:57 +00008433 vim_mkdir_emsg(p_vdir, 0755);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008434#endif
8435
8436 fd = open_exfile(fname, eap->forceit, WRITEBIN);
8437 if (fd != NULL)
8438 {
8439#ifdef FEAT_SESSION
8440 if (eap->cmdidx == CMD_mkview)
8441 flagp = &vop_flags;
8442 else
8443 flagp = &ssop_flags;
8444#endif
8445
8446#ifdef MKSESSION_NL
8447 /* "unix" in 'sessionoptions': use NL line separator */
8448 if (view_session && (*flagp & SSOP_UNIX))
8449 mksession_nl = TRUE;
8450#endif
8451
8452 /* Write the version command for :mkvimrc */
8453 if (eap->cmdidx == CMD_mkvimrc)
8454 (void)put_line(fd, "version 6.0");
8455
8456#ifdef FEAT_SESSION
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00008457 if (eap->cmdidx == CMD_mksession)
8458 {
8459 if (put_line(fd, "let SessionLoad = 1") == FAIL)
8460 failed = TRUE;
8461 }
8462
Bram Moolenaar071d4272004-06-13 20:20:40 +00008463 if (eap->cmdidx != CMD_mkview)
8464#endif
8465 {
8466 /* Write setting 'compatible' first, because it has side effects.
8467 * For that same reason only do it when needed. */
8468 if (p_cp)
8469 (void)put_line(fd, "if !&cp | set cp | endif");
8470 else
8471 (void)put_line(fd, "if &cp | set nocp | endif");
8472 }
8473
8474#ifdef FEAT_SESSION
8475 if (!view_session
8476 || (eap->cmdidx == CMD_mksession
8477 && (*flagp & SSOP_OPTIONS)))
8478#endif
8479 failed |= (makemap(fd, NULL) == FAIL
8480 || makeset(fd, OPT_GLOBAL, FALSE) == FAIL);
8481
8482#ifdef FEAT_SESSION
8483 if (!failed && view_session)
8484 {
8485 if (put_line(fd, "let s:so_save = &so | let s:siso_save = &siso | set so=0 siso=0") == FAIL)
8486 failed = TRUE;
8487 if (eap->cmdidx == CMD_mksession)
8488 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02008489 char_u *dirnow; /* current directory */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008490
Bram Moolenaard9462e32011-04-11 21:35:11 +02008491 dirnow = alloc(MAXPATHL);
8492 if (dirnow == NULL)
8493 failed = TRUE;
8494 else
8495 {
8496 /*
8497 * Change to session file's dir.
8498 */
8499 if (mch_dirname(dirnow, MAXPATHL) == FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +00008500 || mch_chdir((char *)dirnow) != 0)
Bram Moolenaard9462e32011-04-11 21:35:11 +02008501 *dirnow = NUL;
8502 if (*dirnow != NUL && (ssop_flags & SSOP_SESDIR))
8503 {
Bram Moolenaarb7407d32018-02-03 17:36:27 +01008504 if (vim_chdirfile(fname, NULL) == OK)
Bram Moolenaard9462e32011-04-11 21:35:11 +02008505 shorten_fnames(TRUE);
8506 }
8507 else if (*dirnow != NUL
8508 && (ssop_flags & SSOP_CURDIR) && globaldir != NULL)
8509 {
8510 if (mch_chdir((char *)globaldir) == 0)
8511 shorten_fnames(TRUE);
8512 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008513
Bram Moolenaard9462e32011-04-11 21:35:11 +02008514 failed |= (makeopens(fd, dirnow) == FAIL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008515
Bram Moolenaard9462e32011-04-11 21:35:11 +02008516 /* restore original dir */
8517 if (*dirnow != NUL && ((ssop_flags & SSOP_SESDIR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008518 || ((ssop_flags & SSOP_CURDIR) && globaldir != NULL)))
Bram Moolenaard9462e32011-04-11 21:35:11 +02008519 {
8520 if (mch_chdir((char *)dirnow) != 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008521 emsg(_(e_prev_dir));
Bram Moolenaard9462e32011-04-11 21:35:11 +02008522 shorten_fnames(TRUE);
8523 }
8524 vim_free(dirnow);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008525 }
8526 }
8527 else
8528 {
Bram Moolenaarf13be0d2008-01-02 14:13:10 +00008529 failed |= (put_view(fd, curwin, !using_vdir, flagp,
8530 -1) == FAIL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008531 }
8532 if (put_line(fd, "let &so = s:so_save | let &siso = s:siso_save")
8533 == FAIL)
8534 failed = TRUE;
Bram Moolenaare3c74d22019-01-12 16:29:30 +01008535# ifdef FEAT_SEARCH_EXTRA
8536 if (no_hlsearch && put_line(fd, "nohlsearch") == FAIL)
8537 failed = TRUE;
8538# endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00008539 if (put_line(fd, "doautoall SessionLoadPost") == FAIL)
8540 failed = TRUE;
Bram Moolenaar4770d092006-01-12 23:22:24 +00008541 if (eap->cmdidx == CMD_mksession)
8542 {
8543 if (put_line(fd, "unlet SessionLoad") == FAIL)
8544 failed = TRUE;
8545 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008546 }
8547#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00008548 if (put_line(fd, "\" vim: set ft=vim :") == FAIL)
8549 failed = TRUE;
8550
Bram Moolenaar071d4272004-06-13 20:20:40 +00008551 failed |= fclose(fd);
8552
8553 if (failed)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008554 emsg(_(e_write));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008555#if defined(FEAT_EVAL) && defined(FEAT_SESSION)
8556 else if (eap->cmdidx == CMD_mksession)
8557 {
8558 /* successful session write - set this_session var */
Bram Moolenaard9462e32011-04-11 21:35:11 +02008559 char_u *tbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008560
Bram Moolenaard9462e32011-04-11 21:35:11 +02008561 tbuf = alloc(MAXPATHL);
8562 if (tbuf != NULL)
8563 {
8564 if (vim_FullName(fname, tbuf, MAXPATHL, FALSE) == OK)
8565 set_vim_var_string(VV_THIS_SESSION, tbuf, -1);
8566 vim_free(tbuf);
8567 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008568 }
8569#endif
8570#ifdef MKSESSION_NL
8571 mksession_nl = FALSE;
8572#endif
8573 }
8574
8575#ifdef FEAT_BROWSE
8576theend:
8577 vim_free(browseFile);
8578#endif
8579#ifdef FEAT_SESSION
8580 vim_free(viewFile);
8581#endif
8582}
8583
Bram Moolenaardf177f62005-02-22 08:39:57 +00008584#if ((defined(FEAT_SESSION) || defined(FEAT_EVAL)) && defined(vim_mkdir)) \
8585 || defined(PROTO)
8586 int
Bram Moolenaarf1d25012016-03-03 12:22:53 +01008587vim_mkdir_emsg(char_u *name, int prot)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008588{
8589 if (vim_mkdir(name, prot) != 0)
8590 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008591 semsg(_("E739: Cannot create directory: %s"), name);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008592 return FAIL;
8593 }
8594 return OK;
8595}
8596#endif
8597
Bram Moolenaar071d4272004-06-13 20:20:40 +00008598/*
8599 * Open a file for writing for an Ex command, with some checks.
8600 * Return file descriptor, or NULL on failure.
8601 */
8602 FILE *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008603open_exfile(
8604 char_u *fname,
8605 int forceit,
8606 char *mode) /* "w" for create new file or "a" for append */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008607{
8608 FILE *fd;
8609
8610#ifdef UNIX
8611 /* with Unix it is possible to open a directory */
8612 if (mch_isdir(fname))
8613 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008614 semsg(_(e_isadir2), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008615 return NULL;
8616 }
8617#endif
8618 if (!forceit && *mode != 'a' && vim_fexists(fname))
8619 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008620 semsg(_("E189: \"%s\" exists (add ! to override)"), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008621 return NULL;
8622 }
8623
8624 if ((fd = mch_fopen((char *)fname, mode)) == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008625 semsg(_("E190: Cannot open \"%s\" for writing"), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008626
8627 return fd;
8628}
8629
8630/*
8631 * ":mark" and ":k".
8632 */
8633 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008634ex_mark(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008635{
8636 pos_T pos;
8637
8638 if (*eap->arg == NUL) /* No argument? */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008639 emsg(_(e_argreq));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008640 else if (eap->arg[1] != NUL) /* more than one character? */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008641 emsg(_(e_trailing));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008642 else
8643 {
8644 pos = curwin->w_cursor; /* save curwin->w_cursor */
8645 curwin->w_cursor.lnum = eap->line2;
8646 beginline(BL_WHITE | BL_FIX);
8647 if (setmark(*eap->arg) == FAIL) /* set mark */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008648 emsg(_("E191: Argument must be a letter or forward/backward quote"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008649 curwin->w_cursor = pos; /* restore curwin->w_cursor */
8650 }
8651}
8652
8653/*
8654 * Update w_topline, w_leftcol and the cursor position.
8655 */
8656 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008657update_topline_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008658{
8659 check_cursor(); /* put cursor on valid line */
8660 update_topline();
8661 if (!curwin->w_p_wrap)
8662 validate_cursor();
8663 update_curswant();
8664}
8665
Bram Moolenaar071d4272004-06-13 20:20:40 +00008666/*
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008667 * Save the current State and go to Normal mode.
8668 * Return TRUE if the typeahead could be saved.
8669 */
8670 int
8671save_current_state(save_state_T *sst)
8672{
8673 sst->save_msg_scroll = msg_scroll;
8674 sst->save_restart_edit = restart_edit;
8675 sst->save_msg_didout = msg_didout;
8676 sst->save_State = State;
8677 sst->save_insertmode = p_im;
8678 sst->save_finish_op = finish_op;
8679 sst->save_opcount = opcount;
Bram Moolenaarcce713d2019-03-04 11:40:12 +01008680 sst->save_reg_executing = reg_executing;
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008681
8682 msg_scroll = FALSE; /* no msg scrolling in Normal mode */
8683 restart_edit = 0; /* don't go to Insert mode */
8684 p_im = FALSE; /* don't use 'insertmode' */
8685
8686 /*
8687 * Save the current typeahead. This is required to allow using ":normal"
8688 * from an event handler and makes sure we don't hang when the argument
8689 * ends with half a command.
8690 */
8691 save_typeahead(&sst->tabuf);
8692 return sst->tabuf.typebuf_valid;
8693}
8694
8695 void
8696restore_current_state(save_state_T *sst)
8697{
8698 /* Restore the previous typeahead. */
8699 restore_typeahead(&sst->tabuf);
8700
8701 msg_scroll = sst->save_msg_scroll;
8702 restart_edit = sst->save_restart_edit;
8703 p_im = sst->save_insertmode;
8704 finish_op = sst->save_finish_op;
8705 opcount = sst->save_opcount;
Bram Moolenaarcce713d2019-03-04 11:40:12 +01008706 reg_executing = sst->save_reg_executing;
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008707 msg_didout |= sst->save_msg_didout; /* don't reset msg_didout now */
8708
8709 /* Restore the state (needed when called from a function executed for
8710 * 'indentexpr'). Update the mouse and cursor, they may have changed. */
8711 State = sst->save_State;
8712#ifdef CURSOR_SHAPE
8713 ui_cursor_shape(); /* may show different cursor shape */
8714#endif
8715}
8716
8717/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008718 * ":normal[!] {commands}": Execute normal mode commands.
8719 */
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01008720 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008721ex_normal(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008722{
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008723 save_state_T save_state;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008724 char_u *arg = NULL;
8725 int l;
8726 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008727
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008728 if (ex_normal_lock > 0)
8729 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008730 emsg(_(e_secure));
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008731 return;
8732 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008733 if (ex_normal_busy >= p_mmd)
8734 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008735 emsg(_("E192: Recursive use of :normal too deep"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008736 return;
8737 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008738
Bram Moolenaar071d4272004-06-13 20:20:40 +00008739 /*
8740 * vgetc() expects a CSI and K_SPECIAL to have been escaped. Don't do
8741 * this for the K_SPECIAL leading byte, otherwise special keys will not
8742 * work.
8743 */
8744 if (has_mbyte)
8745 {
8746 int len = 0;
8747
8748 /* Count the number of characters to be escaped. */
8749 for (p = eap->arg; *p != NUL; ++p)
8750 {
Bram Moolenaar13505972019-01-24 15:04:48 +01008751#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008752 if (*p == CSI) /* leadbyte CSI */
8753 len += 2;
Bram Moolenaar13505972019-01-24 15:04:48 +01008754#endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008755 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008756 if (*++p == K_SPECIAL /* trailbyte K_SPECIAL or CSI */
Bram Moolenaar13505972019-01-24 15:04:48 +01008757#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008758 || *p == CSI
Bram Moolenaar13505972019-01-24 15:04:48 +01008759#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008760 )
8761 len += 2;
8762 }
8763 if (len > 0)
8764 {
8765 arg = alloc((unsigned)(STRLEN(eap->arg) + len + 1));
8766 if (arg != NULL)
8767 {
8768 len = 0;
8769 for (p = eap->arg; *p != NUL; ++p)
8770 {
8771 arg[len++] = *p;
Bram Moolenaar13505972019-01-24 15:04:48 +01008772#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008773 if (*p == CSI)
8774 {
8775 arg[len++] = KS_EXTRA;
8776 arg[len++] = (int)KE_CSI;
8777 }
Bram Moolenaar13505972019-01-24 15:04:48 +01008778#endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008779 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008780 {
8781 arg[len++] = *++p;
8782 if (*p == K_SPECIAL)
8783 {
8784 arg[len++] = KS_SPECIAL;
8785 arg[len++] = KE_FILLER;
8786 }
Bram Moolenaar13505972019-01-24 15:04:48 +01008787#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008788 else if (*p == CSI)
8789 {
8790 arg[len++] = KS_EXTRA;
8791 arg[len++] = (int)KE_CSI;
8792 }
Bram Moolenaar13505972019-01-24 15:04:48 +01008793#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008794 }
8795 arg[len] = NUL;
8796 }
8797 }
8798 }
8799 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008800
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008801 ++ex_normal_busy;
8802 if (save_current_state(&save_state))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008803 {
8804 /*
8805 * Repeat the :normal command for each line in the range. When no
8806 * range given, execute it just once, without positioning the cursor
8807 * first.
8808 */
8809 do
8810 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008811 if (eap->addr_count != 0)
8812 {
8813 curwin->w_cursor.lnum = eap->line1++;
8814 curwin->w_cursor.col = 0;
Bram Moolenaard5d37532017-03-27 23:02:07 +02008815 check_cursor_moved(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008816 }
8817
Bram Moolenaar13505972019-01-24 15:04:48 +01008818 exec_normal_cmd(arg != NULL
8819 ? arg
8820 : eap->arg, eap->forceit ? REMAP_NONE : REMAP_YES, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008821 }
8822 while (eap->addr_count > 0 && eap->line1 <= eap->line2 && !got_int);
8823 }
8824
8825 /* Might not return to the main loop when in an event handler. */
8826 update_topline_cursor();
8827
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008828 restore_current_state(&save_state);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008829 --ex_normal_busy;
Bram Moolenaareda73602014-11-05 09:53:23 +01008830#ifdef FEAT_MOUSE
8831 setmouse();
8832#endif
8833#ifdef CURSOR_SHAPE
8834 ui_cursor_shape(); /* may show different cursor shape */
8835#endif
8836
Bram Moolenaar071d4272004-06-13 20:20:40 +00008837 vim_free(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008838}
8839
8840/*
Bram Moolenaar64969662005-12-14 21:59:55 +00008841 * ":startinsert", ":startreplace" and ":startgreplace"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008842 */
8843 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008844ex_startinsert(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008845{
Bram Moolenaarfd371682005-01-14 21:42:54 +00008846 if (eap->forceit)
8847 {
Bram Moolenaar09ca9322017-09-26 17:40:45 +02008848 /* cursor line can be zero on startup */
8849 if (!curwin->w_cursor.lnum)
8850 curwin->w_cursor.lnum = 1;
Bram Moolenaarfd371682005-01-14 21:42:54 +00008851 coladvance((colnr_T)MAXCOL);
8852 curwin->w_curswant = MAXCOL;
8853 curwin->w_set_curswant = FALSE;
8854 }
8855
Bram Moolenaara40c5002005-01-09 21:16:21 +00008856 /* Ignore the command when already in Insert mode. Inserting an
8857 * expression register that invokes a function can do this. */
8858 if (State & INSERT)
8859 return;
8860
Bram Moolenaar64969662005-12-14 21:59:55 +00008861 if (eap->cmdidx == CMD_startinsert)
8862 restart_edit = 'a';
8863 else if (eap->cmdidx == CMD_startreplace)
8864 restart_edit = 'R';
Bram Moolenaar071d4272004-06-13 20:20:40 +00008865 else
Bram Moolenaar64969662005-12-14 21:59:55 +00008866 restart_edit = 'V';
8867
8868 if (!eap->forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008869 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00008870 if (eap->cmdidx == CMD_startinsert)
8871 restart_edit = 'i';
Bram Moolenaar071d4272004-06-13 20:20:40 +00008872 curwin->w_curswant = 0; /* avoid MAXCOL */
8873 }
8874}
8875
8876/*
8877 * ":stopinsert"
8878 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008879 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008880ex_stopinsert(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008881{
8882 restart_edit = 0;
8883 stop_insert_mode = TRUE;
Bram Moolenaarfd773e92016-04-02 19:39:16 +02008884 clearmode();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008885}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008886
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008887/*
8888 * Execute normal mode command "cmd".
8889 * "remap" can be REMAP_NONE or REMAP_YES.
8890 */
8891 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008892exec_normal_cmd(char_u *cmd, int remap, int silent)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008893{
Bram Moolenaar25281632016-01-21 23:32:32 +01008894 /* Stuff the argument into the typeahead buffer. */
8895 ins_typebuf(cmd, remap, 0, TRUE, silent);
Bram Moolenaar586c70c2018-10-02 16:23:58 +02008896 exec_normal(FALSE, FALSE, FALSE);
Bram Moolenaar25281632016-01-21 23:32:32 +01008897}
Bram Moolenaar25281632016-01-21 23:32:32 +01008898
Bram Moolenaar25281632016-01-21 23:32:32 +01008899/*
8900 * Execute normal_cmd() until there is no typeahead left.
Bram Moolenaar586c70c2018-10-02 16:23:58 +02008901 * When "use_vpeekc" is TRUE use vpeekc() to check for available chars.
Bram Moolenaar25281632016-01-21 23:32:32 +01008902 */
8903 void
Bram Moolenaar586c70c2018-10-02 16:23:58 +02008904exec_normal(int was_typed, int use_vpeekc, int may_use_terminal_loop UNUSED)
Bram Moolenaar25281632016-01-21 23:32:32 +01008905{
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008906 oparg_T oa;
Bram Moolenaar905dd902019-04-07 14:21:47 +02008907 int c;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008908
Bram Moolenaar905dd902019-04-07 14:21:47 +02008909 // When calling vpeekc() from feedkeys() it will return Ctrl_C when there
8910 // is nothing to get, so also check for Ctrl_C.
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008911 clear_oparg(&oa);
8912 finish_op = FALSE;
Bram Moolenaar586c70c2018-10-02 16:23:58 +02008913 while ((!stuff_empty()
8914 || ((was_typed || !typebuf_typed()) && typebuf.tb_len > 0)
Bram Moolenaar905dd902019-04-07 14:21:47 +02008915 || (use_vpeekc && (c = vpeekc()) != NUL && c != Ctrl_C))
Bram Moolenaar586c70c2018-10-02 16:23:58 +02008916 && !got_int)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008917 {
8918 update_topline_cursor();
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02008919#ifdef FEAT_TERMINAL
Bram Moolenaar655a82a2018-05-08 22:01:07 +02008920 if (may_use_terminal_loop && term_use_loop()
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02008921 && oa.op_type == OP_NOP && oa.regname == NUL
8922 && !VIsual_active)
8923 {
8924 /* If terminal_loop() returns OK we got a key that is handled
8925 * in Normal model. With FAIL we first need to position the
8926 * cursor and the screen needs to be redrawn. */
8927 if (terminal_loop(TRUE) == OK)
8928 normal_cmd(&oa, TRUE);
8929 }
8930 else
8931#endif
8932 /* execute a Normal mode cmd */
8933 normal_cmd(&oa, TRUE);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008934 }
8935}
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008936
Bram Moolenaar071d4272004-06-13 20:20:40 +00008937#ifdef FEAT_FIND_ID
8938 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008939ex_checkpath(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008940{
8941 find_pattern_in_path(NULL, 0, 0, FALSE, FALSE, CHECK_PATH, 1L,
8942 eap->forceit ? ACTION_SHOW_ALL : ACTION_SHOW,
8943 (linenr_T)1, (linenr_T)MAXLNUM);
8944}
8945
Bram Moolenaar4033c552017-09-16 20:54:51 +02008946#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008947/*
8948 * ":psearch"
8949 */
8950 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008951ex_psearch(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008952{
8953 g_do_tagpreview = p_pvh;
8954 ex_findpat(eap);
8955 g_do_tagpreview = 0;
8956}
8957#endif
8958
8959 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008960ex_findpat(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008961{
8962 int whole = TRUE;
8963 long n;
8964 char_u *p;
8965 int action;
8966
8967 switch (cmdnames[eap->cmdidx].cmd_name[2])
8968 {
8969 case 'e': /* ":psearch", ":isearch" and ":dsearch" */
8970 if (cmdnames[eap->cmdidx].cmd_name[0] == 'p')
8971 action = ACTION_GOTO;
8972 else
8973 action = ACTION_SHOW;
8974 break;
8975 case 'i': /* ":ilist" and ":dlist" */
8976 action = ACTION_SHOW_ALL;
8977 break;
8978 case 'u': /* ":ijump" and ":djump" */
8979 action = ACTION_GOTO;
8980 break;
8981 default: /* ":isplit" and ":dsplit" */
8982 action = ACTION_SPLIT;
8983 break;
8984 }
8985
8986 n = 1;
8987 if (vim_isdigit(*eap->arg)) /* get count */
8988 {
8989 n = getdigits(&eap->arg);
8990 eap->arg = skipwhite(eap->arg);
8991 }
8992 if (*eap->arg == '/') /* Match regexp, not just whole words */
8993 {
8994 whole = FALSE;
8995 ++eap->arg;
8996 p = skip_regexp(eap->arg, '/', p_magic, NULL);
8997 if (*p)
8998 {
8999 *p++ = NUL;
9000 p = skipwhite(p);
9001
9002 /* Check for trailing illegal characters */
9003 if (!ends_excmd(*p))
9004 eap->errmsg = e_trailing;
9005 else
9006 eap->nextcmd = check_nextcmd(p);
9007 }
9008 }
9009 if (!eap->skip)
9010 find_pattern_in_path(eap->arg, 0, (int)STRLEN(eap->arg),
9011 whole, !eap->forceit,
9012 *eap->cmd == 'd' ? FIND_DEFINE : FIND_ANY,
9013 n, action, eap->line1, eap->line2);
9014}
9015#endif
9016
Bram Moolenaar071d4272004-06-13 20:20:40 +00009017
Bram Moolenaar4033c552017-09-16 20:54:51 +02009018#ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00009019/*
9020 * ":ptag", ":ptselect", ":ptjump", ":ptnext", etc.
9021 */
9022 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009023ex_ptag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009024{
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01009025 g_do_tagpreview = p_pvh; /* will be reset to 0 in ex_tag_cmd() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009026 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
9027}
9028
9029/*
9030 * ":pedit"
9031 */
9032 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009033ex_pedit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009034{
9035 win_T *curwin_save = curwin;
9036
9037 g_do_tagpreview = p_pvh;
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00009038 prepare_tagpreview(TRUE);
Bram Moolenaar67883b42017-07-27 22:57:00 +02009039 keep_help_flag = bt_help(curwin_save->w_buffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009040 do_exedit(eap, NULL);
9041 keep_help_flag = FALSE;
9042 if (curwin != curwin_save && win_valid(curwin_save))
9043 {
9044 /* Return cursor to where we were */
9045 validate_cursor();
9046 redraw_later(VALID);
9047 win_enter(curwin_save, TRUE);
9048 }
9049 g_do_tagpreview = 0;
9050}
Bram Moolenaar4033c552017-09-16 20:54:51 +02009051#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009052
9053/*
9054 * ":stag", ":stselect" and ":stjump".
9055 */
9056 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009057ex_stag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009058{
9059 postponed_split = -1;
9060 postponed_split_flags = cmdmod.split;
Bram Moolenaard326ce82007-03-11 14:48:29 +00009061 postponed_split_tab = cmdmod.tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009062 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
9063 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00009064 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009065}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009066
9067/*
9068 * ":tag", ":tselect", ":tjump", ":tnext", etc.
9069 */
9070 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009071ex_tag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009072{
9073 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name);
9074}
9075
9076 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009077ex_tag_cmd(exarg_T *eap, char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009078{
9079 int cmd;
9080
9081 switch (name[1])
9082 {
9083 case 'j': cmd = DT_JUMP; /* ":tjump" */
9084 break;
9085 case 's': cmd = DT_SELECT; /* ":tselect" */
9086 break;
9087 case 'p': cmd = DT_PREV; /* ":tprevious" */
9088 break;
9089 case 'N': cmd = DT_PREV; /* ":tNext" */
9090 break;
9091 case 'n': cmd = DT_NEXT; /* ":tnext" */
9092 break;
9093 case 'o': cmd = DT_POP; /* ":pop" */
9094 break;
9095 case 'f': /* ":tfirst" */
9096 case 'r': cmd = DT_FIRST; /* ":trewind" */
9097 break;
9098 case 'l': cmd = DT_LAST; /* ":tlast" */
9099 break;
9100 default: /* ":tag" */
9101#ifdef FEAT_CSCOPE
Bram Moolenaar7c94c262008-06-20 09:11:34 +00009102 if (p_cst && *eap->arg != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009103 {
Bram Moolenaard4db7712016-11-12 19:16:46 +01009104 ex_cstag(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009105 return;
9106 }
9107#endif
9108 cmd = DT_TAG;
9109 break;
9110 }
9111
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00009112 if (name[0] == 'l')
9113 {
9114#ifndef FEAT_QUICKFIX
9115 ex_ni(eap);
9116 return;
9117#else
9118 cmd = DT_LTAG;
9119#endif
9120 }
9121
Bram Moolenaar071d4272004-06-13 20:20:40 +00009122 do_tag(eap->arg, cmd, eap->addr_count > 0 ? (int)eap->line2 : 1,
9123 eap->forceit, TRUE);
9124}
9125
9126/*
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009127 * Check "str" for starting with a special cmdline variable.
9128 * If found return one of the SPEC_ values and set "*usedlen" to the length of
9129 * the variable. Otherwise return -1 and "*usedlen" is unchanged.
9130 */
9131 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009132find_cmdline_var(char_u *src, int *usedlen)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009133{
9134 int len;
9135 int i;
Bram Moolenaar8f0b2d42009-05-16 14:41:10 +00009136 static char *(spec_str[]) = {
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009137 "%",
9138#define SPEC_PERC 0
9139 "#",
Bram Moolenaar65f08472017-09-10 18:16:20 +02009140#define SPEC_HASH (SPEC_PERC + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009141 "<cword>", /* cursor word */
Bram Moolenaar65f08472017-09-10 18:16:20 +02009142#define SPEC_CWORD (SPEC_HASH + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009143 "<cWORD>", /* cursor WORD */
Bram Moolenaar65f08472017-09-10 18:16:20 +02009144#define SPEC_CCWORD (SPEC_CWORD + 1)
9145 "<cexpr>", /* expr under cursor */
9146#define SPEC_CEXPR (SPEC_CCWORD + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009147 "<cfile>", /* cursor path name */
Bram Moolenaar65f08472017-09-10 18:16:20 +02009148#define SPEC_CFILE (SPEC_CEXPR + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009149 "<sfile>", /* ":so" file name */
Bram Moolenaar65f08472017-09-10 18:16:20 +02009150#define SPEC_SFILE (SPEC_CFILE + 1)
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009151 "<slnum>", /* ":so" file line number */
Bram Moolenaar65f08472017-09-10 18:16:20 +02009152#define SPEC_SLNUM (SPEC_SFILE + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009153 "<afile>", /* autocommand file name */
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009154#define SPEC_AFILE (SPEC_SLNUM + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009155 "<abuf>", /* autocommand buffer number */
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009156#define SPEC_ABUF (SPEC_AFILE + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009157 "<amatch>", /* autocommand match name */
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01009158#define SPEC_AMATCH (SPEC_ABUF + 1)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009159 "<sflnum>", /* script file line number */
9160#define SPEC_SFLNUM (SPEC_AMATCH + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009161#ifdef FEAT_CLIENTSERVER
9162 "<client>"
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009163# define SPEC_CLIENT (SPEC_SFLNUM + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009164#endif
9165 };
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009166
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00009167 for (i = 0; i < (int)(sizeof(spec_str) / sizeof(char *)); ++i)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009168 {
9169 len = (int)STRLEN(spec_str[i]);
9170 if (STRNCMP(src, spec_str[i], len) == 0)
9171 {
9172 *usedlen = len;
9173 return i;
9174 }
9175 }
9176 return -1;
9177}
9178
9179/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009180 * Evaluate cmdline variables.
9181 *
9182 * change '%' to curbuf->b_ffname
9183 * '#' to curwin->w_altfile
9184 * '<cword>' to word under the cursor
9185 * '<cWORD>' to WORD under the cursor
9186 * '<cfile>' to path name under the cursor
9187 * '<sfile>' to sourced file name
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009188 * '<slnum>' to sourced file line number
Bram Moolenaar071d4272004-06-13 20:20:40 +00009189 * '<afile>' to file name for autocommand
9190 * '<abuf>' to buffer number for autocommand
9191 * '<amatch>' to matching name for autocommand
9192 *
9193 * When an error is detected, "errormsg" is set to a non-NULL pointer (may be
9194 * "" for error without a message) and NULL is returned.
9195 * Returns an allocated string if a valid match was found.
9196 * Returns NULL if no match was found. "usedlen" then still contains the
9197 * number of characters to skip.
9198 */
9199 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009200eval_vars(
9201 char_u *src, /* pointer into commandline */
9202 char_u *srcstart, /* beginning of valid memory for src */
9203 int *usedlen, /* characters after src that are used */
9204 linenr_T *lnump, /* line number for :e command, or NULL */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009205 char **errormsg, /* pointer to error message */
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009206 int *escaped) /* return value has escaped white space (can
Bram Moolenaar63b92542007-03-27 14:57:09 +00009207 * be NULL) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009208{
9209 int i;
9210 char_u *s;
9211 char_u *result;
9212 char_u *resultbuf = NULL;
9213 int resultlen;
9214 buf_T *buf;
9215 int valid = VALID_HEAD + VALID_PATH; /* assume valid result */
9216 int spec_idx;
9217#ifdef FEAT_MODIFY_FNAME
Bram Moolenaarfd249462018-07-28 16:14:30 +02009218 int tilde_file = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009219 int skip_mod = FALSE;
9220#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009221 char_u strbuf[30];
Bram Moolenaar071d4272004-06-13 20:20:40 +00009222
9223 *errormsg = NULL;
Bram Moolenaar63b92542007-03-27 14:57:09 +00009224 if (escaped != NULL)
9225 *escaped = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009226
9227 /*
9228 * Check if there is something to do.
9229 */
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009230 spec_idx = find_cmdline_var(src, usedlen);
9231 if (spec_idx < 0) /* no match */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009232 {
9233 *usedlen = 1;
9234 return NULL;
9235 }
9236
9237 /*
9238 * Skip when preceded with a backslash "\%" and "\#".
9239 * Note: In "\\%" the % is also not recognized!
9240 */
9241 if (src > srcstart && src[-1] == '\\')
9242 {
9243 *usedlen = 0;
Bram Moolenaara7241f52008-06-24 20:39:31 +00009244 STRMOVE(src - 1, src); /* remove backslash */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009245 return NULL;
9246 }
9247
9248 /*
9249 * word or WORD under cursor
9250 */
Bram Moolenaar65f08472017-09-10 18:16:20 +02009251 if (spec_idx == SPEC_CWORD || spec_idx == SPEC_CCWORD
9252 || spec_idx == SPEC_CEXPR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009253 {
Bram Moolenaar65f08472017-09-10 18:16:20 +02009254 resultlen = find_ident_under_cursor(&result,
9255 spec_idx == SPEC_CWORD ? (FIND_IDENT | FIND_STRING)
9256 : spec_idx == SPEC_CEXPR ? (FIND_IDENT | FIND_STRING | FIND_EVAL)
9257 : FIND_STRING);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009258 if (resultlen == 0)
9259 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009260 *errormsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +00009261 return NULL;
9262 }
9263 }
9264
9265 /*
9266 * '#': Alternate file name
9267 * '%': Current file name
9268 * File name under the cursor
9269 * File name for autocommand
9270 * and following modifiers
9271 */
9272 else
9273 {
9274 switch (spec_idx)
9275 {
9276 case SPEC_PERC: /* '%': current file */
9277 if (curbuf->b_fname == NULL)
9278 {
9279 result = (char_u *)"";
9280 valid = 0; /* Must have ":p:h" to be valid */
9281 }
9282 else
Bram Moolenaar00136dc2018-07-25 21:19:13 +02009283 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009284 result = curbuf->b_fname;
Bram Moolenaarfd249462018-07-28 16:14:30 +02009285#ifdef FEAT_MODIFY_FNAME
Bram Moolenaar00136dc2018-07-25 21:19:13 +02009286 tilde_file = STRCMP(result, "~") == 0;
Bram Moolenaarfd249462018-07-28 16:14:30 +02009287#endif
Bram Moolenaar00136dc2018-07-25 21:19:13 +02009288 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009289 break;
9290
9291 case SPEC_HASH: /* '#' or "#99": alternate file */
9292 if (src[1] == '#') /* "##": the argument list */
9293 {
9294 result = arg_all();
9295 resultbuf = result;
9296 *usedlen = 2;
Bram Moolenaar63b92542007-03-27 14:57:09 +00009297 if (escaped != NULL)
9298 *escaped = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009299#ifdef FEAT_MODIFY_FNAME
9300 skip_mod = TRUE;
9301#endif
9302 break;
9303 }
9304 s = src + 1;
Bram Moolenaard812df62008-11-09 12:46:09 +00009305 if (*s == '<') /* "#<99" uses v:oldfiles */
9306 ++s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009307 i = (int)getdigits(&s);
Bram Moolenaarc312b8b2017-10-28 17:53:04 +02009308 if (s == src + 2 && src[1] == '-')
9309 /* just a minus sign, don't skip over it */
9310 s--;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009311 *usedlen = (int)(s - src); /* length of what we expand */
9312
Bram Moolenaarc312b8b2017-10-28 17:53:04 +02009313 if (src[1] == '<' && i != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009314 {
Bram Moolenaard812df62008-11-09 12:46:09 +00009315 if (*usedlen < 2)
9316 {
9317 /* Should we give an error message for #<text? */
9318 *usedlen = 1;
9319 return NULL;
9320 }
9321#ifdef FEAT_EVAL
9322 result = list_find_str(get_vim_var_list(VV_OLDFILES),
9323 (long)i);
9324 if (result == NULL)
9325 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009326 *errormsg = "";
Bram Moolenaard812df62008-11-09 12:46:09 +00009327 return NULL;
9328 }
9329#else
Bram Moolenaar8e481e82019-01-15 20:07:48 +01009330 *errormsg = _("E809: #< is not available without the +eval feature");
Bram Moolenaar071d4272004-06-13 20:20:40 +00009331 return NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +00009332#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009333 }
9334 else
Bram Moolenaard812df62008-11-09 12:46:09 +00009335 {
Bram Moolenaarc312b8b2017-10-28 17:53:04 +02009336 if (i == 0 && src[1] == '<' && *usedlen > 1)
9337 *usedlen = 1;
Bram Moolenaard812df62008-11-09 12:46:09 +00009338 buf = buflist_findnr(i);
9339 if (buf == NULL)
9340 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009341 *errormsg = _("E194: No alternate file name to substitute for '#'");
Bram Moolenaard812df62008-11-09 12:46:09 +00009342 return NULL;
9343 }
9344 if (lnump != NULL)
9345 *lnump = ECMD_LAST;
9346 if (buf->b_fname == NULL)
9347 {
9348 result = (char_u *)"";
9349 valid = 0; /* Must have ":p:h" to be valid */
9350 }
9351 else
Bram Moolenaar00136dc2018-07-25 21:19:13 +02009352 {
Bram Moolenaard812df62008-11-09 12:46:09 +00009353 result = buf->b_fname;
Bram Moolenaarfd249462018-07-28 16:14:30 +02009354#ifdef FEAT_MODIFY_FNAME
Bram Moolenaar00136dc2018-07-25 21:19:13 +02009355 tilde_file = STRCMP(result, "~") == 0;
Bram Moolenaarfd249462018-07-28 16:14:30 +02009356#endif
Bram Moolenaar00136dc2018-07-25 21:19:13 +02009357 }
Bram Moolenaard812df62008-11-09 12:46:09 +00009358 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009359 break;
9360
9361#ifdef FEAT_SEARCHPATH
9362 case SPEC_CFILE: /* file name under cursor */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00009363 result = file_name_at_cursor(FNAME_MESS|FNAME_HYP, 1L, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009364 if (result == NULL)
9365 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009366 *errormsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +00009367 return NULL;
9368 }
9369 resultbuf = result; /* remember allocated string */
9370 break;
9371#endif
9372
Bram Moolenaar071d4272004-06-13 20:20:40 +00009373 case SPEC_AFILE: /* file name for autocommand */
9374 result = autocmd_fname;
Bram Moolenaarf6dad432008-09-18 19:29:58 +00009375 if (result != NULL && !autocmd_fname_full)
9376 {
9377 /* Still need to turn the fname into a full path. It is
9378 * postponed to avoid a delay when <afile> is not used. */
9379 autocmd_fname_full = TRUE;
9380 result = FullName_save(autocmd_fname, FALSE);
9381 vim_free(autocmd_fname);
9382 autocmd_fname = result;
9383 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009384 if (result == NULL)
9385 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009386 *errormsg = _("E495: no autocommand file name to substitute for \"<afile>\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00009387 return NULL;
9388 }
Bram Moolenaara0174af2008-01-02 20:08:25 +00009389 result = shorten_fname1(result);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009390 break;
9391
9392 case SPEC_ABUF: /* buffer number for autocommand */
9393 if (autocmd_bufnr <= 0)
9394 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009395 *errormsg = _("E496: no autocommand buffer number to substitute for \"<abuf>\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00009396 return NULL;
9397 }
9398 sprintf((char *)strbuf, "%d", autocmd_bufnr);
9399 result = strbuf;
9400 break;
9401
9402 case SPEC_AMATCH: /* match name for autocommand */
9403 result = autocmd_match;
9404 if (result == NULL)
9405 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009406 *errormsg = _("E497: no autocommand match name to substitute for \"<amatch>\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00009407 return NULL;
9408 }
9409 break;
9410
Bram Moolenaar071d4272004-06-13 20:20:40 +00009411 case SPEC_SFILE: /* file name for ":so" command */
9412 result = sourcing_name;
9413 if (result == NULL)
9414 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009415 *errormsg = _("E498: no :source file name to substitute for \"<sfile>\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00009416 return NULL;
9417 }
9418 break;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009419
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009420 case SPEC_SLNUM: /* line in file for ":so" command */
9421 if (sourcing_name == NULL || sourcing_lnum == 0)
9422 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009423 *errormsg = _("E842: no line number to use for \"<slnum>\"");
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009424 return NULL;
9425 }
9426 sprintf((char *)strbuf, "%ld", (long)sourcing_lnum);
9427 result = strbuf;
9428 break;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009429
9430#ifdef FEAT_EVAL
9431 case SPEC_SFLNUM: /* line in script file */
9432 if (current_sctx.sc_lnum + sourcing_lnum == 0)
9433 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009434 *errormsg = _("E961: no line number to use for \"<sflnum>\"");
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009435 return NULL;
9436 }
9437 sprintf((char *)strbuf, "%ld",
9438 (long)(current_sctx.sc_lnum + sourcing_lnum));
9439 result = strbuf;
9440 break;
9441#endif
9442
9443#ifdef FEAT_CLIENTSERVER
Bram Moolenaar071d4272004-06-13 20:20:40 +00009444 case SPEC_CLIENT: /* Source of last submitted input */
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009445 sprintf((char *)strbuf, PRINTF_HEX_LONG_U,
9446 (long_u)clientWindow);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009447 result = strbuf;
9448 break;
9449#endif
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009450
Bram Moolenaar9e0f6ec2017-05-16 09:36:54 +02009451 default:
9452 result = (char_u *)""; /* avoid gcc warning */
9453 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009454 }
9455
9456 resultlen = (int)STRLEN(result); /* length of new string */
9457 if (src[*usedlen] == '<') /* remove the file name extension */
9458 {
9459 ++*usedlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009460 if ((s = vim_strrchr(result, '.')) != NULL && s >= gettail(result))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009461 resultlen = (int)(s - result);
9462 }
9463#ifdef FEAT_MODIFY_FNAME
9464 else if (!skip_mod)
9465 {
Bram Moolenaar00136dc2018-07-25 21:19:13 +02009466 valid |= modify_fname(src, tilde_file, usedlen, &result, &resultbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009467 &resultlen);
9468 if (result == NULL)
9469 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009470 *errormsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +00009471 return NULL;
9472 }
9473 }
9474#endif
9475 }
9476
9477 if (resultlen == 0 || valid != VALID_HEAD + VALID_PATH)
9478 {
9479 if (valid != VALID_HEAD + VALID_PATH)
9480 /* xgettext:no-c-format */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009481 *errormsg = _("E499: Empty file name for '%' or '#', only works with \":p:h\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00009482 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009483 *errormsg = _("E500: Evaluates to an empty string");
Bram Moolenaar071d4272004-06-13 20:20:40 +00009484 result = NULL;
9485 }
9486 else
9487 result = vim_strnsave(result, resultlen);
9488 vim_free(resultbuf);
9489 return result;
9490}
9491
9492/*
9493 * Concatenate all files in the argument list, separated by spaces, and return
9494 * it in one allocated string.
9495 * Spaces and backslashes in the file names are escaped with a backslash.
9496 * Returns NULL when out of memory.
9497 */
9498 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009499arg_all(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009500{
9501 int len;
9502 int idx;
9503 char_u *retval = NULL;
9504 char_u *p;
9505
9506 /*
9507 * Do this loop two times:
9508 * first time: compute the total length
9509 * second time: concatenate the names
9510 */
9511 for (;;)
9512 {
9513 len = 0;
9514 for (idx = 0; idx < ARGCOUNT; ++idx)
9515 {
9516 p = alist_name(&ARGLIST[idx]);
9517 if (p != NULL)
9518 {
9519 if (len > 0)
9520 {
9521 /* insert a space in between names */
9522 if (retval != NULL)
9523 retval[len] = ' ';
9524 ++len;
9525 }
9526 for ( ; *p != NUL; ++p)
9527 {
Bram Moolenaar6e8d3b02015-06-09 21:33:31 +02009528 if (*p == ' '
9529#ifndef BACKSLASH_IN_FILENAME
9530 || *p == '\\'
9531#endif
Bram Moolenaar2c8c6812018-07-28 17:07:52 +02009532 || *p == '`')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009533 {
9534 /* insert a backslash */
9535 if (retval != NULL)
9536 retval[len] = '\\';
9537 ++len;
9538 }
9539 if (retval != NULL)
9540 retval[len] = *p;
9541 ++len;
9542 }
9543 }
9544 }
9545
9546 /* second time: break here */
9547 if (retval != NULL)
9548 {
9549 retval[len] = NUL;
9550 break;
9551 }
9552
9553 /* allocate memory */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00009554 retval = alloc((unsigned)len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009555 if (retval == NULL)
9556 break;
9557 }
9558
9559 return retval;
9560}
9561
Bram Moolenaar071d4272004-06-13 20:20:40 +00009562/*
9563 * Expand the <sfile> string in "arg".
9564 *
9565 * Returns an allocated string, or NULL for any error.
9566 */
9567 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009568expand_sfile(char_u *arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009569{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009570 char *errormsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009571 int len;
9572 char_u *result;
9573 char_u *newres;
9574 char_u *repl;
9575 int srclen;
9576 char_u *p;
9577
9578 result = vim_strsave(arg);
9579 if (result == NULL)
9580 return NULL;
9581
9582 for (p = result; *p; )
9583 {
9584 if (STRNCMP(p, "<sfile>", 7) != 0)
9585 ++p;
9586 else
9587 {
9588 /* replace "<sfile>" with the sourced file name, and do ":" stuff */
Bram Moolenaar63b92542007-03-27 14:57:09 +00009589 repl = eval_vars(p, result, &srclen, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009590 if (errormsg != NULL)
9591 {
9592 if (*errormsg)
9593 emsg(errormsg);
9594 vim_free(result);
9595 return NULL;
9596 }
9597 if (repl == NULL) /* no match (cannot happen) */
9598 {
9599 p += srclen;
9600 continue;
9601 }
9602 len = (int)STRLEN(result) - srclen + (int)STRLEN(repl) + 1;
9603 newres = alloc(len);
9604 if (newres == NULL)
9605 {
9606 vim_free(repl);
9607 vim_free(result);
9608 return NULL;
9609 }
9610 mch_memmove(newres, result, (size_t)(p - result));
9611 STRCPY(newres + (p - result), repl);
9612 len = (int)STRLEN(newres);
9613 STRCAT(newres, p + srclen);
9614 vim_free(repl);
9615 vim_free(result);
9616 result = newres;
9617 p = newres + len; /* continue after the match */
9618 }
9619 }
9620
9621 return result;
9622}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009623
9624#ifdef FEAT_SESSION
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01009625static int ses_winsizes(FILE *fd, int restore_size,
9626 win_T *tab_firstwin);
9627static int ses_win_rec(FILE *fd, frame_T *fr);
9628static frame_T *ses_skipframe(frame_T *fr);
9629static int ses_do_frame(frame_T *fr);
9630static int ses_do_win(win_T *wp);
9631static int ses_arglist(FILE *fd, char *cmd, garray_T *gap, int fullname, unsigned *flagp);
9632static int ses_put_fname(FILE *fd, char_u *name, unsigned *flagp);
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +02009633static int ses_fname(FILE *fd, buf_T *buf, unsigned *flagp, int add_eol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009634
9635/*
9636 * Write openfile commands for the current buffers to an .exrc file.
9637 * Return FAIL on error, OK otherwise.
9638 */
9639 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009640makeopens(
9641 FILE *fd,
9642 char_u *dirnow) /* Current directory name */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009643{
9644 buf_T *buf;
9645 int only_save_windows = TRUE;
9646 int nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009647 int restore_size = TRUE;
9648 win_T *wp;
9649 char_u *sname;
9650 win_T *edited_win = NULL;
Bram Moolenaar18144c82006-04-12 21:52:12 +00009651 int tabnr;
Bram Moolenaar04ad7fe2014-05-07 21:14:47 +02009652 int restore_stal = FALSE;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +00009653 win_T *tab_firstwin;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +00009654 frame_T *tab_topframe;
Bram Moolenaarf13be0d2008-01-02 14:13:10 +00009655 int cur_arg_idx = 0;
Bram Moolenaar383c6f52008-01-04 15:01:07 +00009656 int next_arg_idx = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009657
9658 if (ssop_flags & SSOP_BUFFERS)
9659 only_save_windows = FALSE; /* Save ALL buffers */
9660
9661 /*
9662 * Begin by setting the this_session variable, and then other
9663 * sessionable variables.
9664 */
9665#ifdef FEAT_EVAL
9666 if (put_line(fd, "let v:this_session=expand(\"<sfile>:p\")") == FAIL)
9667 return FAIL;
9668 if (ssop_flags & SSOP_GLOBALS)
9669 if (store_session_globals(fd) == FAIL)
9670 return FAIL;
9671#endif
9672
9673 /*
Bram Moolenaar26d4b892018-07-04 22:26:28 +02009674 * Close all windows and tabs but one.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009675 */
9676 if (put_line(fd, "silent only") == FAIL)
9677 return FAIL;
Bram Moolenaar26d4b892018-07-04 22:26:28 +02009678 if ((ssop_flags & SSOP_TABPAGES)
9679 && put_line(fd, "silent tabonly") == FAIL)
9680 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009681
9682 /*
9683 * Now a :cd command to the session directory or the current directory
9684 */
9685 if (ssop_flags & SSOP_SESDIR)
9686 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009687 if (put_line(fd, "exe \"cd \" . escape(expand(\"<sfile>:p:h\"), ' ')")
9688 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009689 return FAIL;
9690 }
9691 else if (ssop_flags & SSOP_CURDIR)
9692 {
9693 sname = home_replace_save(NULL, globaldir != NULL ? globaldir : dirnow);
9694 if (sname == NULL
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009695 || fputs("cd ", fd) < 0
9696 || ses_put_fname(fd, sname, &ssop_flags) == FAIL
9697 || put_eol(fd) == FAIL)
9698 {
9699 vim_free(sname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009700 return FAIL;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009701 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009702 vim_free(sname);
9703 }
9704
9705 /*
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009706 * If there is an empty, unnamed buffer we will wipe it out later.
9707 * Remember the buffer number.
9708 */
9709 if (put_line(fd, "if expand('%') == '' && !&modified && line('$') <= 1 && getline(1) == ''") == FAIL)
9710 return FAIL;
9711 if (put_line(fd, " let s:wipebuf = bufnr('%')") == FAIL)
9712 return FAIL;
9713 if (put_line(fd, "endif") == FAIL)
9714 return FAIL;
9715
9716 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009717 * Now save the current files, current buffer first.
9718 */
9719 if (put_line(fd, "set shortmess=aoO") == FAIL)
9720 return FAIL;
9721
Bram Moolenaar071d4272004-06-13 20:20:40 +00009722 /* the global argument list */
Bram Moolenaarf0bdd2f2014-03-12 21:28:26 +01009723 if (ses_arglist(fd, "argglobal", &global_alist.al_ga,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009724 !(ssop_flags & SSOP_CURDIR), &ssop_flags) == FAIL)
9725 return FAIL;
9726
9727 if (ssop_flags & SSOP_RESIZE)
9728 {
9729 /* Note: after the restore we still check it worked!*/
9730 if (fprintf(fd, "set lines=%ld columns=%ld" , Rows, Columns) < 0
9731 || put_eol(fd) == FAIL)
9732 return FAIL;
9733 }
9734
9735#ifdef FEAT_GUI
9736 if (gui.in_use && (ssop_flags & SSOP_WINPOS))
9737 {
9738 int x, y;
9739
9740 if (gui_mch_get_winpos(&x, &y) == OK)
9741 {
9742 /* Note: after the restore we still check it worked!*/
9743 if (fprintf(fd, "winpos %d %d", x, y) < 0 || put_eol(fd) == FAIL)
9744 return FAIL;
9745 }
9746 }
9747#endif
9748
9749 /*
Bram Moolenaar04ad7fe2014-05-07 21:14:47 +02009750 * When there are two or more tabpages and 'showtabline' is 1 the tabline
9751 * will be displayed when creating the next tab. That resizes the windows
9752 * in the first tab, which may cause problems. Set 'showtabline' to 2
9753 * temporarily to avoid that.
9754 */
9755 if (p_stal == 1 && first_tabpage->tp_next != NULL)
9756 {
9757 if (put_line(fd, "set stal=2") == FAIL)
9758 return FAIL;
9759 restore_stal = TRUE;
9760 }
9761
9762 /*
Bram Moolenaar18144c82006-04-12 21:52:12 +00009763 * May repeat putting Windows for each tab, when "tabpages" is in
9764 * 'sessionoptions'.
Bram Moolenaarcba2ae52006-10-24 10:59:57 +00009765 * Don't use goto_tabpage(), it may change directory and trigger
9766 * autocommands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009767 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +00009768 tab_firstwin = firstwin; /* first window in tab page "tabnr" */
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +00009769 tab_topframe = topframe;
Bram Moolenaar26d4b892018-07-04 22:26:28 +02009770 if ((ssop_flags & SSOP_TABPAGES))
9771 {
Bram Moolenaar57a6bf02019-01-22 21:27:13 +01009772 tabpage_T *tp;
Bram Moolenaar26d4b892018-07-04 22:26:28 +02009773
Bram Moolenaar57a6bf02019-01-22 21:27:13 +01009774 // Similar to ses_win_rec() below, populate the tab pages first so
9775 // later local options won't be copied to the new tabs.
9776 FOR_ALL_TABPAGES(tp)
9777 if (tp->tp_next != NULL && put_line(fd, "tabnew") == FAIL)
Bram Moolenaar26d4b892018-07-04 22:26:28 +02009778 return FAIL;
Bram Moolenaar57a6bf02019-01-22 21:27:13 +01009779 if (first_tabpage->tp_next != NULL && put_line(fd, "tabrewind") == FAIL)
Bram Moolenaar26d4b892018-07-04 22:26:28 +02009780 return FAIL;
9781 }
Bram Moolenaar18144c82006-04-12 21:52:12 +00009782 for (tabnr = 1; ; ++tabnr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009783 {
Bram Moolenaar00aa0692019-04-27 20:37:57 +02009784 tabpage_T *tp = NULL;
Bram Moolenaar26d4b892018-07-04 22:26:28 +02009785 int need_tabnext = FALSE;
Bram Moolenaar695baee2015-04-13 12:39:22 +02009786 int cnr = 1;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +00009787
Bram Moolenaar18144c82006-04-12 21:52:12 +00009788 if ((ssop_flags & SSOP_TABPAGES))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009789 {
Bram Moolenaar00aa0692019-04-27 20:37:57 +02009790 tp = find_tabpage(tabnr);
Bram Moolenaarcba2ae52006-10-24 10:59:57 +00009791
9792 if (tp == NULL)
9793 break; /* done all tab pages */
9794 if (tp == curtab)
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +00009795 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +00009796 tab_firstwin = firstwin;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +00009797 tab_topframe = topframe;
9798 }
Bram Moolenaarcba2ae52006-10-24 10:59:57 +00009799 else
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +00009800 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +00009801 tab_firstwin = tp->tp_firstwin;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +00009802 tab_topframe = tp->tp_topframe;
9803 }
Bram Moolenaarcba2ae52006-10-24 10:59:57 +00009804 if (tabnr > 1)
Bram Moolenaar26d4b892018-07-04 22:26:28 +02009805 need_tabnext = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009806 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009807
Bram Moolenaar18144c82006-04-12 21:52:12 +00009808 /*
9809 * Before creating the window layout, try loading one file. If this
9810 * is aborted we don't end up with a number of useless windows.
9811 * This may have side effects! (e.g., compressed or network file).
9812 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +00009813 for (wp = tab_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar18144c82006-04-12 21:52:12 +00009814 {
9815 if (ses_do_win(wp)
9816 && wp->w_buffer->b_ffname != NULL
Bram Moolenaar67883b42017-07-27 22:57:00 +02009817 && !bt_help(wp->w_buffer)
Bram Moolenaar18144c82006-04-12 21:52:12 +00009818#ifdef FEAT_QUICKFIX
9819 && !bt_nofile(wp->w_buffer)
9820#endif
9821 )
9822 {
Bram Moolenaar26d4b892018-07-04 22:26:28 +02009823 if (need_tabnext && put_line(fd, "tabnext") == FAIL)
9824 return FAIL;
9825 need_tabnext = FALSE;
9826
9827 if (fputs("edit ", fd) < 0
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +02009828 || ses_fname(fd, wp->w_buffer, &ssop_flags, TRUE)
9829 == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +00009830 return FAIL;
9831 if (!wp->w_arg_idx_invalid)
9832 edited_win = wp;
9833 break;
9834 }
9835 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009836
Bram Moolenaarcba2ae52006-10-24 10:59:57 +00009837 /* If no file got edited create an empty tab page. */
Bram Moolenaar26d4b892018-07-04 22:26:28 +02009838 if (need_tabnext && put_line(fd, "tabnext") == FAIL)
Bram Moolenaarcba2ae52006-10-24 10:59:57 +00009839 return FAIL;
9840
Bram Moolenaar18144c82006-04-12 21:52:12 +00009841 /*
9842 * Save current window layout.
9843 */
9844 if (put_line(fd, "set splitbelow splitright") == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009845 return FAIL;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +00009846 if (ses_win_rec(fd, tab_topframe) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009847 return FAIL;
Bram Moolenaar18144c82006-04-12 21:52:12 +00009848 if (!p_sb && put_line(fd, "set nosplitbelow") == FAIL)
9849 return FAIL;
9850 if (!p_spr && put_line(fd, "set nosplitright") == FAIL)
9851 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009852
Bram Moolenaar18144c82006-04-12 21:52:12 +00009853 /*
9854 * Check if window sizes can be restored (no windows omitted).
9855 * Remember the window number of the current window after restoring.
9856 */
9857 nr = 0;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +00009858 for (wp = tab_firstwin; wp != NULL; wp = W_NEXT(wp))
Bram Moolenaar18144c82006-04-12 21:52:12 +00009859 {
9860 if (ses_do_win(wp))
9861 ++nr;
9862 else
9863 restore_size = FALSE;
9864 if (curwin == wp)
9865 cnr = nr;
9866 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009867
Bram Moolenaar18144c82006-04-12 21:52:12 +00009868 /* Go to the first window. */
9869 if (put_line(fd, "wincmd t") == FAIL)
9870 return FAIL;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009871
Bram Moolenaar18144c82006-04-12 21:52:12 +00009872 /*
9873 * If more than one window, see if sizes can be restored.
9874 * First set 'winheight' and 'winwidth' to 1 to avoid the windows being
9875 * resized when moving between windows.
9876 * Do this before restoring the view, so that the topline and the
9877 * cursor can be set. This is done again below.
Bram Moolenaar36ae89c2017-01-28 17:11:14 +01009878 * winminheight and winminwidth need to be set to avoid an error if the
9879 * user has set winheight or winwidth.
Bram Moolenaar18144c82006-04-12 21:52:12 +00009880 */
Bram Moolenaar19834012018-06-12 17:03:39 +02009881 if (put_line(fd, "set winminheight=0") == FAIL
9882 || put_line(fd, "set winheight=1") == FAIL
9883 || put_line(fd, "set winminwidth=0") == FAIL
9884 || put_line(fd, "set winwidth=1") == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +00009885 return FAIL;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +00009886 if (nr > 1 && ses_winsizes(fd, restore_size, tab_firstwin) == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +00009887 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009888
Bram Moolenaar00aa0692019-04-27 20:37:57 +02009889 // Restore the tab-local working directory if specified
9890 // Do this before the windows, so that the window-local directory can
9891 // override the tab-local directory.
9892 if (tp != NULL && tp->tp_localdir != NULL && ssop_flags & SSOP_CURDIR)
9893 {
9894 if (fputs("tcd ", fd) < 0
9895 || ses_put_fname(fd, tp->tp_localdir, &ssop_flags) == FAIL
9896 || put_eol(fd) == FAIL)
9897 return FAIL;
9898 did_lcd = TRUE;
9899 }
9900
Bram Moolenaar18144c82006-04-12 21:52:12 +00009901 /*
9902 * Restore the view of the window (options, file, cursor, etc.).
9903 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +00009904 for (wp = tab_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar18144c82006-04-12 21:52:12 +00009905 {
9906 if (!ses_do_win(wp))
9907 continue;
Bram Moolenaarf13be0d2008-01-02 14:13:10 +00009908 if (put_view(fd, wp, wp != edited_win, &ssop_flags,
9909 cur_arg_idx) == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +00009910 return FAIL;
9911 if (nr > 1 && put_line(fd, "wincmd w") == FAIL)
9912 return FAIL;
Bram Moolenaarf13be0d2008-01-02 14:13:10 +00009913 next_arg_idx = wp->w_arg_idx;
Bram Moolenaar18144c82006-04-12 21:52:12 +00009914 }
9915
Bram Moolenaarf13be0d2008-01-02 14:13:10 +00009916 /* The argument index in the first tab page is zero, need to set it in
9917 * each window. For further tab pages it's the window where we do
9918 * "tabedit". */
9919 cur_arg_idx = next_arg_idx;
9920
Bram Moolenaar18144c82006-04-12 21:52:12 +00009921 /*
9922 * Restore cursor to the current window if it's not the first one.
9923 */
9924 if (cnr > 1 && (fprintf(fd, "%dwincmd w", cnr) < 0
9925 || put_eol(fd) == FAIL))
9926 return FAIL;
9927
9928 /*
Bram Moolenaar18144c82006-04-12 21:52:12 +00009929 * Restore window sizes again after jumping around in windows, because
9930 * the current window has a minimum size while others may not.
9931 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +00009932 if (nr > 1 && ses_winsizes(fd, restore_size, tab_firstwin) == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +00009933 return FAIL;
9934
Bram Moolenaar18144c82006-04-12 21:52:12 +00009935 /* Don't continue in another tab page when doing only the current one
9936 * or when at the last tab page. */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +00009937 if (!(ssop_flags & SSOP_TABPAGES))
Bram Moolenaar18144c82006-04-12 21:52:12 +00009938 break;
9939 }
9940
9941 if (ssop_flags & SSOP_TABPAGES)
9942 {
Bram Moolenaar18144c82006-04-12 21:52:12 +00009943 if (fprintf(fd, "tabnext %d", tabpage_index(curtab)) < 0
9944 || put_eol(fd) == FAIL)
9945 return FAIL;
9946 }
Bram Moolenaar04ad7fe2014-05-07 21:14:47 +02009947 if (restore_stal && put_line(fd, "set stal=1") == FAIL)
9948 return FAIL;
Bram Moolenaar18144c82006-04-12 21:52:12 +00009949
Bram Moolenaard39e2752019-01-26 20:07:38 +01009950 // Now put the remaining buffers into the buffer list.
9951 // This is near the end, so that when 'hidden' is set we don't create extra
9952 // buffers. If the buffer was already created with another command the
9953 // ":badd" will have no effect.
9954 FOR_ALL_BUFFERS(buf)
9955 {
9956 if (!(only_save_windows && buf->b_nwindows == 0)
9957 && !(buf->b_help && !(ssop_flags & SSOP_HELP))
9958#ifdef FEAT_TERMINAL
9959 // Skip terminal buffers: finished ones are not useful, others
9960 // will be resurrected and result in a new buffer.
9961 && !bt_terminal(buf)
9962#endif
9963 && buf->b_fname != NULL
9964 && buf->b_p_bl)
9965 {
9966 if (fprintf(fd, "badd +%ld ", buf->b_wininfo == NULL ? 1L
9967 : buf->b_wininfo->wi_fpos.lnum) < 0
9968 || ses_fname(fd, buf, &ssop_flags, TRUE) == FAIL)
9969 return FAIL;
9970 }
9971 }
9972
Bram Moolenaar9c102382006-05-03 21:26:49 +00009973 /*
9974 * Wipe out an empty unnamed buffer we started in.
9975 */
Bram Moolenaar26d4b892018-07-04 22:26:28 +02009976 if (put_line(fd, "if exists('s:wipebuf') && len(win_findbuf(s:wipebuf)) == 0")
Bram Moolenaar4d8bac82018-03-09 21:33:34 +01009977 == FAIL)
Bram Moolenaar9c102382006-05-03 21:26:49 +00009978 return FAIL;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00009979 if (put_line(fd, " silent exe 'bwipe ' . s:wipebuf") == FAIL)
Bram Moolenaar9c102382006-05-03 21:26:49 +00009980 return FAIL;
9981 if (put_line(fd, "endif") == FAIL)
9982 return FAIL;
9983 if (put_line(fd, "unlet! s:wipebuf") == FAIL)
9984 return FAIL;
9985
9986 /* Re-apply 'winheight', 'winwidth' and 'shortmess'. */
9987 if (fprintf(fd, "set winheight=%ld winwidth=%ld shortmess=%s",
9988 p_wh, p_wiw, p_shm) < 0 || put_eol(fd) == FAIL)
9989 return FAIL;
Bram Moolenaar36ae89c2017-01-28 17:11:14 +01009990 /* Re-apply 'winminheight' and 'winminwidth'. */
9991 if (fprintf(fd, "set winminheight=%ld winminwidth=%ld",
9992 p_wmh, p_wmw) < 0 || put_eol(fd) == FAIL)
9993 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009994
9995 /*
9996 * Lastly, execute the x.vim file if it exists.
9997 */
9998 if (put_line(fd, "let s:sx = expand(\"<sfile>:p:r\").\"x.vim\"") == FAIL
9999 || put_line(fd, "if file_readable(s:sx)") == FAIL
Bram Moolenaar42ba1262008-12-09 10:18:03 +000010000 || put_line(fd, " exe \"source \" . fnameescape(s:sx)") == FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +000010001 || put_line(fd, "endif") == FAIL)
10002 return FAIL;
10003
10004 return OK;
10005}
10006
10007 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010008ses_winsizes(
10009 FILE *fd,
10010 int restore_size,
10011 win_T *tab_firstwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010012{
10013 int n = 0;
10014 win_T *wp;
10015
10016 if (restore_size && (ssop_flags & SSOP_WINSIZE))
10017 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010018 for (wp = tab_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010019 {
10020 if (!ses_do_win(wp))
10021 continue;
10022 ++n;
10023
10024 /* restore height when not full height */
10025 if (wp->w_height + wp->w_status_height < topframe->fr_height
10026 && (fprintf(fd,
10027 "exe '%dresize ' . ((&lines * %ld + %ld) / %ld)",
10028 n, (long)wp->w_height, Rows / 2, Rows) < 0
10029 || put_eol(fd) == FAIL))
10030 return FAIL;
10031
10032 /* restore width when not full width */
10033 if (wp->w_width < Columns && (fprintf(fd,
10034 "exe 'vert %dresize ' . ((&columns * %ld + %ld) / %ld)",
10035 n, (long)wp->w_width, Columns / 2, Columns) < 0
10036 || put_eol(fd) == FAIL))
10037 return FAIL;
10038 }
10039 }
10040 else
10041 {
10042 /* Just equalise window sizes */
10043 if (put_line(fd, "wincmd =") == FAIL)
10044 return FAIL;
10045 }
10046 return OK;
10047}
10048
10049/*
10050 * Write commands to "fd" to recursively create windows for frame "fr",
10051 * horizontally and vertically split.
10052 * After the commands the last window in the frame is the current window.
10053 * Returns FAIL when writing the commands to "fd" fails.
10054 */
10055 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010056ses_win_rec(FILE *fd, frame_T *fr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010057{
10058 frame_T *frc;
10059 int count = 0;
10060
10061 if (fr->fr_layout != FR_LEAF)
10062 {
10063 /* Find first frame that's not skipped and then create a window for
10064 * each following one (first frame is already there). */
10065 frc = ses_skipframe(fr->fr_child);
10066 if (frc != NULL)
10067 while ((frc = ses_skipframe(frc->fr_next)) != NULL)
10068 {
10069 /* Make window as big as possible so that we have lots of room
10070 * to split. */
10071 if (put_line(fd, "wincmd _ | wincmd |") == FAIL
10072 || put_line(fd, fr->fr_layout == FR_COL
10073 ? "split" : "vsplit") == FAIL)
10074 return FAIL;
10075 ++count;
10076 }
10077
10078 /* Go back to the first window. */
10079 if (count > 0 && (fprintf(fd, fr->fr_layout == FR_COL
10080 ? "%dwincmd k" : "%dwincmd h", count) < 0
10081 || put_eol(fd) == FAIL))
10082 return FAIL;
10083
10084 /* Recursively create frames/windows in each window of this column or
10085 * row. */
10086 frc = ses_skipframe(fr->fr_child);
10087 while (frc != NULL)
10088 {
10089 ses_win_rec(fd, frc);
10090 frc = ses_skipframe(frc->fr_next);
10091 /* Go to next window. */
10092 if (frc != NULL && put_line(fd, "wincmd w") == FAIL)
10093 return FAIL;
10094 }
10095 }
10096 return OK;
10097}
10098
10099/*
10100 * Skip frames that don't contain windows we want to save in the Session.
10101 * Returns NULL when there none.
10102 */
10103 static frame_T *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010104ses_skipframe(frame_T *fr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010105{
10106 frame_T *frc;
10107
Bram Moolenaar3d1491e2018-12-22 17:07:50 +010010108 FOR_ALL_FRAMES(frc, fr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010109 if (ses_do_frame(frc))
10110 break;
10111 return frc;
10112}
10113
10114/*
10115 * Return TRUE if frame "fr" has a window somewhere that we want to save in
10116 * the Session.
10117 */
10118 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010119ses_do_frame(frame_T *fr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010120{
10121 frame_T *frc;
10122
10123 if (fr->fr_layout == FR_LEAF)
10124 return ses_do_win(fr->fr_win);
Bram Moolenaar3d1491e2018-12-22 17:07:50 +010010125 FOR_ALL_FRAMES(frc, fr->fr_child)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010126 if (ses_do_frame(frc))
10127 return TRUE;
10128 return FALSE;
10129}
10130
10131/*
10132 * Return non-zero if window "wp" is to be stored in the Session.
10133 */
10134 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010135ses_do_win(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010136{
Bram Moolenaar4d8bac82018-03-09 21:33:34 +010010137#ifdef FEAT_TERMINAL
10138 if (bt_terminal(wp->w_buffer))
10139 return !term_is_finished(wp->w_buffer)
10140 && (ssop_flags & SSOP_TERMINAL)
10141 && term_should_restore(wp->w_buffer);
10142#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010143 if (wp->w_buffer->b_fname == NULL
10144#ifdef FEAT_QUICKFIX
10145 /* When 'buftype' is "nofile" can't restore the window contents. */
10146 || bt_nofile(wp->w_buffer)
10147#endif
10148 )
10149 return (ssop_flags & SSOP_BLANK);
Bram Moolenaar67883b42017-07-27 22:57:00 +020010150 if (bt_help(wp->w_buffer))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010151 return (ssop_flags & SSOP_HELP);
10152 return TRUE;
10153}
10154
Bram Moolenaar92c1b692018-08-29 21:42:42 +020010155 static int
10156put_view_curpos(FILE *fd, win_T *wp, char *spaces)
10157{
10158 int r;
10159
10160 if (wp->w_curswant == MAXCOL)
10161 r = fprintf(fd, "%snormal! $", spaces);
10162 else
10163 r = fprintf(fd, "%snormal! 0%d|", spaces, wp->w_virtcol + 1);
10164 return r < 0 || put_eol(fd) == FAIL ? FALSE : OK;
10165}
10166
Bram Moolenaar071d4272004-06-13 20:20:40 +000010167/*
10168 * Write commands to "fd" to restore the view of a window.
10169 * Caller must make sure 'scrolloff' is zero.
10170 */
10171 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010172put_view(
10173 FILE *fd,
10174 win_T *wp,
10175 int add_edit, /* add ":edit" command to view */
10176 unsigned *flagp, /* vop_flags or ssop_flags */
10177 int current_arg_idx) /* current argument index of the window, use
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010178 * -1 if unknown */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010179{
10180 win_T *save_curwin;
10181 int f;
10182 int do_cursor;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000010183 int did_next = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010184
10185 /* Always restore cursor position for ":mksession". For ":mkview" only
10186 * when 'viewoptions' contains "cursor". */
10187 do_cursor = (flagp == &ssop_flags || *flagp & SSOP_CURSOR);
10188
10189 /*
10190 * Local argument list.
10191 */
10192 if (wp->w_alist == &global_alist)
10193 {
10194 if (put_line(fd, "argglobal") == FAIL)
10195 return FAIL;
10196 }
10197 else
10198 {
10199 if (ses_arglist(fd, "arglocal", &wp->w_alist->al_ga,
10200 flagp == &vop_flags
10201 || !(*flagp & SSOP_CURDIR)
10202 || wp->w_localdir != NULL, flagp) == FAIL)
10203 return FAIL;
10204 }
10205
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000010206 /* Only when part of a session: restore the argument index. Some
10207 * arguments may have been deleted, check if the index is valid. */
Bram Moolenaar51f53df2010-06-26 05:25:54 +020010208 if (wp->w_arg_idx != current_arg_idx && wp->w_arg_idx < WARGCOUNT(wp)
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000010209 && flagp == &ssop_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010210 {
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010211 if (fprintf(fd, "%ldargu", (long)wp->w_arg_idx + 1) < 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000010212 || put_eol(fd) == FAIL)
10213 return FAIL;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000010214 did_next = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010215 }
10216
10217 /* Edit the file. Skip this when ":next" already did it. */
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000010218 if (add_edit && (!did_next || wp->w_arg_idx_invalid))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010219 {
Bram Moolenaar4d8bac82018-03-09 21:33:34 +010010220# ifdef FEAT_TERMINAL
10221 if (bt_terminal(wp->w_buffer))
10222 {
10223 if (term_write_session(fd, wp) == FAIL)
10224 return FAIL;
10225 }
10226 else
10227# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010228 /*
10229 * Load the file.
10230 */
10231 if (wp->w_buffer->b_ffname != NULL
Bram Moolenaar4d8bac82018-03-09 21:33:34 +010010232# ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +000010233 && !bt_nofile(wp->w_buffer)
Bram Moolenaar4d8bac82018-03-09 21:33:34 +010010234# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010235 )
10236 {
10237 /*
10238 * Editing a file in this buffer: use ":edit file".
10239 * This may have side effects! (e.g., compressed or network file).
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +020010240 *
10241 * Note, if a buffer for that file already exists, use :badd to
10242 * edit that buffer, to not lose folding information (:edit resets
10243 * folds in other buffers)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010244 */
Bram Moolenaarad36a352019-01-24 13:34:42 +010010245 if (fputs("if bufexists(\"", fd) < 0
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +020010246 || ses_fname(fd, wp->w_buffer, flagp, FALSE) == FAIL
Bram Moolenaarad36a352019-01-24 13:34:42 +010010247 || fputs("\") | buffer ", fd) < 0
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +020010248 || ses_fname(fd, wp->w_buffer, flagp, FALSE) == FAIL
10249 || fputs(" | else | edit ", fd) < 0
10250 || ses_fname(fd, wp->w_buffer, flagp, FALSE) == FAIL
10251 || fputs(" | endif", fd) < 0
Bram Moolenaar4d8bac82018-03-09 21:33:34 +010010252 || put_eol(fd) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010253 return FAIL;
10254 }
10255 else
10256 {
10257 /* No file in this buffer, just make it empty. */
10258 if (put_line(fd, "enew") == FAIL)
10259 return FAIL;
10260#ifdef FEAT_QUICKFIX
10261 if (wp->w_buffer->b_ffname != NULL)
10262 {
10263 /* The buffer does have a name, but it's not a file name. */
10264 if (fputs("file ", fd) < 0
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +020010265 || ses_fname(fd, wp->w_buffer, flagp, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010266 return FAIL;
10267 }
10268#endif
10269 do_cursor = FALSE;
10270 }
10271 }
10272
10273 /*
10274 * Local mappings and abbreviations.
10275 */
10276 if ((*flagp & (SSOP_OPTIONS | SSOP_LOCALOPTIONS))
10277 && makemap(fd, wp->w_buffer) == FAIL)
10278 return FAIL;
10279
10280 /*
10281 * Local options. Need to go to the window temporarily.
10282 * Store only local values when using ":mkview" and when ":mksession" is
10283 * used and 'sessionoptions' doesn't include "options".
10284 * Some folding options are always stored when "folds" is included,
10285 * otherwise the folds would not be restored correctly.
10286 */
10287 save_curwin = curwin;
10288 curwin = wp;
10289 curbuf = curwin->w_buffer;
10290 if (*flagp & (SSOP_OPTIONS | SSOP_LOCALOPTIONS))
10291 f = makeset(fd, OPT_LOCAL,
10292 flagp == &vop_flags || !(*flagp & SSOP_OPTIONS));
10293#ifdef FEAT_FOLDING
10294 else if (*flagp & SSOP_FOLDS)
10295 f = makefoldset(fd);
10296#endif
10297 else
10298 f = OK;
10299 curwin = save_curwin;
10300 curbuf = curwin->w_buffer;
10301 if (f == FAIL)
10302 return FAIL;
10303
10304#ifdef FEAT_FOLDING
10305 /*
10306 * Save Folds when 'buftype' is empty and for help files.
10307 */
10308 if ((*flagp & SSOP_FOLDS)
10309 && wp->w_buffer->b_ffname != NULL
Bram Moolenaar91335e52018-08-01 17:53:12 +020010310 && (bt_normal(wp->w_buffer) || bt_help(wp->w_buffer)))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010311 {
10312 if (put_folds(fd, wp) == FAIL)
10313 return FAIL;
10314 }
10315#endif
10316
10317 /*
10318 * Set the cursor after creating folds, since that moves the cursor.
10319 */
10320 if (do_cursor)
10321 {
10322
10323 /* Restore the cursor line in the file and relatively in the
10324 * window. Don't use "G", it changes the jumplist. */
10325 if (fprintf(fd, "let s:l = %ld - ((%ld * winheight(0) + %ld) / %ld)",
10326 (long)wp->w_cursor.lnum,
10327 (long)(wp->w_cursor.lnum - wp->w_topline),
10328 (long)wp->w_height / 2, (long)wp->w_height) < 0
10329 || put_eol(fd) == FAIL
10330 || put_line(fd, "if s:l < 1 | let s:l = 1 | endif") == FAIL
10331 || put_line(fd, "exe s:l") == FAIL
10332 || put_line(fd, "normal! zt") == FAIL
10333 || fprintf(fd, "%ld", (long)wp->w_cursor.lnum) < 0
10334 || put_eol(fd) == FAIL)
10335 return FAIL;
10336 /* Restore the cursor column and left offset when not wrapping. */
10337 if (wp->w_cursor.col == 0)
10338 {
10339 if (put_line(fd, "normal! 0") == FAIL)
10340 return FAIL;
10341 }
10342 else
10343 {
10344 if (!wp->w_p_wrap && wp->w_leftcol > 0 && wp->w_width > 0)
10345 {
10346 if (fprintf(fd,
10347 "let s:c = %ld - ((%ld * winwidth(0) + %ld) / %ld)",
Bram Moolenaar558ddad2013-02-20 19:26:29 +010010348 (long)wp->w_virtcol + 1,
10349 (long)(wp->w_virtcol - wp->w_leftcol),
Bram Moolenaar071d4272004-06-13 20:20:40 +000010350 (long)wp->w_width / 2, (long)wp->w_width) < 0
10351 || put_eol(fd) == FAIL
10352 || put_line(fd, "if s:c > 0") == FAIL
10353 || fprintf(fd,
Bram Moolenaar558ddad2013-02-20 19:26:29 +010010354 " exe 'normal! ' . s:c . '|zs' . %ld . '|'",
10355 (long)wp->w_virtcol + 1) < 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000010356 || put_eol(fd) == FAIL
10357 || put_line(fd, "else") == FAIL
Bram Moolenaar92c1b692018-08-29 21:42:42 +020010358 || put_view_curpos(fd, wp, " ") == FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +000010359 || put_line(fd, "endif") == FAIL)
10360 return FAIL;
10361 }
Bram Moolenaar92c1b692018-08-29 21:42:42 +020010362 else if (put_view_curpos(fd, wp, "") == FAIL)
10363 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010364 }
10365 }
10366
10367 /*
Bram Moolenaar13e90412017-11-11 18:16:48 +010010368 * Local directory, if the current flag is not view options or the "curdir"
10369 * option is included.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010370 */
Bram Moolenaar13e90412017-11-11 18:16:48 +010010371 if (wp->w_localdir != NULL
10372 && (flagp != &vop_flags || (*flagp & SSOP_CURDIR)))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010373 {
10374 if (fputs("lcd ", fd) < 0
10375 || ses_put_fname(fd, wp->w_localdir, flagp) == FAIL
10376 || put_eol(fd) == FAIL)
10377 return FAIL;
Bram Moolenaareeefcc72007-05-01 21:21:21 +000010378 did_lcd = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010379 }
10380
10381 return OK;
10382}
10383
10384/*
10385 * Write an argument list to the session file.
10386 * Returns FAIL if writing fails.
10387 */
10388 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010389ses_arglist(
10390 FILE *fd,
10391 char *cmd,
10392 garray_T *gap,
10393 int fullname, /* TRUE: use full path name */
10394 unsigned *flagp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010395{
10396 int i;
Bram Moolenaard9462e32011-04-11 21:35:11 +020010397 char_u *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010398 char_u *s;
10399
Bram Moolenaarf0bdd2f2014-03-12 21:28:26 +010010400 if (fputs(cmd, fd) < 0 || put_eol(fd) == FAIL)
10401 return FAIL;
Bram Moolenaar555de4e2019-01-21 23:03:49 +010010402 if (put_line(fd, "%argdel") == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010403 return FAIL;
10404 for (i = 0; i < gap->ga_len; ++i)
10405 {
10406 /* NULL file names are skipped (only happens when out of memory). */
10407 s = alist_name(&((aentry_T *)gap->ga_data)[i]);
10408 if (s != NULL)
10409 {
10410 if (fullname)
10411 {
Bram Moolenaard9462e32011-04-11 21:35:11 +020010412 buf = alloc(MAXPATHL);
10413 if (buf != NULL)
10414 {
10415 (void)vim_FullName(s, buf, MAXPATHL, FALSE);
10416 s = buf;
10417 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010418 }
Bram Moolenaar79da5632017-02-01 22:52:44 +010010419 if (fputs("$argadd ", fd) < 0
Bram Moolenaarf0bdd2f2014-03-12 21:28:26 +010010420 || ses_put_fname(fd, s, flagp) == FAIL
10421 || put_eol(fd) == FAIL)
Bram Moolenaard9462e32011-04-11 21:35:11 +020010422 {
10423 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010424 return FAIL;
Bram Moolenaard9462e32011-04-11 21:35:11 +020010425 }
10426 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010427 }
10428 }
Bram Moolenaarf0bdd2f2014-03-12 21:28:26 +010010429 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010430}
10431
10432/*
10433 * Write a buffer name to the session file.
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +020010434 * Also ends the line, if "add_eol" is TRUE.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010435 * Returns FAIL if writing fails.
10436 */
10437 static int
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +020010438ses_fname(FILE *fd, buf_T *buf, unsigned *flagp, int add_eol)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010439{
10440 char_u *name;
10441
10442 /* Use the short file name if the current directory is known at the time
Bram Moolenaareeefcc72007-05-01 21:21:21 +000010443 * the session file will be sourced.
10444 * Don't do this for ":mkview", we don't know the current directory.
10445 * Don't do this after ":lcd", we don't keep track of what the current
10446 * directory is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010447 if (buf->b_sfname != NULL
10448 && flagp == &ssop_flags
Bram Moolenaareeefcc72007-05-01 21:21:21 +000010449 && (ssop_flags & (SSOP_CURDIR | SSOP_SESDIR))
Bram Moolenaar8d594672009-07-01 18:18:57 +000010450#ifdef FEAT_AUTOCHDIR
10451 && !p_acd
10452#endif
Bram Moolenaareeefcc72007-05-01 21:21:21 +000010453 && !did_lcd)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010454 name = buf->b_sfname;
10455 else
10456 name = buf->b_ffname;
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +020010457 if (ses_put_fname(fd, name, flagp) == FAIL
10458 || (add_eol && put_eol(fd) == FAIL))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010459 return FAIL;
10460 return OK;
10461}
10462
10463/*
10464 * Write a file name to the session file.
10465 * Takes care of the "slash" option in 'sessionoptions' and escapes special
10466 * characters.
Bram Moolenaar78f74a92010-10-13 17:50:07 +020010467 * Returns FAIL if writing fails or out of memory.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010468 */
10469 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010470ses_put_fname(FILE *fd, char_u *name, unsigned *flagp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010471{
10472 char_u *sname;
Bram Moolenaar78f74a92010-10-13 17:50:07 +020010473 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010474 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010475
10476 sname = home_replace_save(NULL, name);
Bram Moolenaar78f74a92010-10-13 17:50:07 +020010477 if (sname == NULL)
10478 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010479
Bram Moolenaar78f74a92010-10-13 17:50:07 +020010480 if (*flagp & SSOP_SLASH)
10481 {
10482 /* change all backslashes to forward slashes */
Bram Moolenaar91acfff2017-03-12 19:22:36 +010010483 for (p = sname; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar78f74a92010-10-13 17:50:07 +020010484 if (*p == '\\')
10485 *p = '/';
Bram Moolenaar071d4272004-06-13 20:20:40 +000010486 }
Bram Moolenaar78f74a92010-10-13 17:50:07 +020010487
Bram Moolenaar84a05ac2013-05-06 04:24:17 +020010488 /* escape special characters */
Bram Moolenaar78f74a92010-10-13 17:50:07 +020010489 p = vim_strsave_fnameescape(sname, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010490 vim_free(sname);
Bram Moolenaar78f74a92010-10-13 17:50:07 +020010491 if (p == NULL)
10492 return FAIL;
10493
10494 /* write the result */
10495 if (fputs((char *)p, fd) < 0)
10496 retval = FAIL;
10497
10498 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010499 return retval;
10500}
10501
10502/*
10503 * ":loadview [nr]"
10504 */
10505 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010506ex_loadview(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010507{
10508 char_u *fname;
10509
10510 fname = get_view_file(*eap->arg);
10511 if (fname != NULL)
10512 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +000010513 do_source(fname, FALSE, DOSO_NONE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010514 vim_free(fname);
10515 }
10516}
10517
10518/*
10519 * Get the name of the view file for the current buffer.
10520 */
10521 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010522get_view_file(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010523{
10524 int len = 0;
10525 char_u *p, *s;
10526 char_u *retval;
10527 char_u *sname;
10528
10529 if (curbuf->b_ffname == NULL)
10530 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010531 emsg(_(e_noname));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010532 return NULL;
10533 }
10534 sname = home_replace_save(NULL, curbuf->b_ffname);
10535 if (sname == NULL)
10536 return NULL;
10537
10538 /*
10539 * We want a file name without separators, because we're not going to make
10540 * a directory.
10541 * "normal" path separator -> "=+"
10542 * "=" -> "=="
10543 * ":" path separator -> "=-"
10544 */
10545 for (p = sname; *p; ++p)
10546 if (*p == '=' || vim_ispathsep(*p))
10547 ++len;
10548 retval = alloc((unsigned)(STRLEN(sname) + len + STRLEN(p_vdir) + 9));
10549 if (retval != NULL)
10550 {
10551 STRCPY(retval, p_vdir);
10552 add_pathsep(retval);
10553 s = retval + STRLEN(retval);
10554 for (p = sname; *p; ++p)
10555 {
10556 if (*p == '=')
10557 {
10558 *s++ = '=';
10559 *s++ = '=';
10560 }
10561 else if (vim_ispathsep(*p))
10562 {
10563 *s++ = '=';
Bram Moolenaare60acc12011-05-10 16:41:25 +020010564#if defined(BACKSLASH_IN_FILENAME) || defined(AMIGA) || defined(VMS)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010565 if (*p == ':')
10566 *s++ = '-';
10567 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010568#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +000010569 *s++ = '+';
Bram Moolenaar071d4272004-06-13 20:20:40 +000010570 }
10571 else
10572 *s++ = *p;
10573 }
10574 *s++ = '=';
10575 *s++ = c;
10576 STRCPY(s, ".vim");
10577 }
10578
10579 vim_free(sname);
10580 return retval;
10581}
10582
10583#endif /* FEAT_SESSION */
10584
10585/*
10586 * Write end-of-line character(s) for ":mkexrc", ":mkvimrc" and ":mksession".
10587 * Return FAIL for a write error.
10588 */
10589 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010590put_eol(FILE *fd)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010591{
10592 if (
10593#ifdef USE_CRNL
10594 (
10595# ifdef MKSESSION_NL
10596 !mksession_nl &&
10597# endif
10598 (putc('\r', fd) < 0)) ||
10599#endif
10600 (putc('\n', fd) < 0))
10601 return FAIL;
10602 return OK;
10603}
10604
10605/*
10606 * Write a line to "fd".
10607 * Return FAIL for a write error.
10608 */
10609 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010610put_line(FILE *fd, char *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010611{
10612 if (fputs(s, fd) < 0 || put_eol(fd) == FAIL)
10613 return FAIL;
10614 return OK;
10615}
10616
10617#ifdef FEAT_VIMINFO
10618/*
10619 * ":rviminfo" and ":wviminfo".
10620 */
10621 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010622ex_viminfo(
10623 exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010624{
10625 char_u *save_viminfo;
10626
10627 save_viminfo = p_viminfo;
10628 if (*p_viminfo == NUL)
10629 p_viminfo = (char_u *)"'100";
10630 if (eap->cmdidx == CMD_rviminfo)
10631 {
Bram Moolenaard812df62008-11-09 12:46:09 +000010632 if (read_viminfo(eap->arg, VIF_WANT_INFO | VIF_WANT_MARKS
10633 | (eap->forceit ? VIF_FORCEIT : 0)) == FAIL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010634 emsg(_("E195: Cannot open viminfo file for reading"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010635 }
10636 else
10637 write_viminfo(eap->arg, eap->forceit);
10638 p_viminfo = save_viminfo;
10639}
10640#endif
10641
10642#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG) || defined(PROTO)
Bram Moolenaar9c13b352005-05-19 20:53:52 +000010643/*
Bram Moolenaard9462e32011-04-11 21:35:11 +020010644 * Make a dialog message in "buff[DIALOG_MSG_SIZE]".
Bram Moolenaarb765d632005-06-07 21:00:02 +000010645 * "format" must contain "%s".
Bram Moolenaar9c13b352005-05-19 20:53:52 +000010646 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010647 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010648dialog_msg(char_u *buff, char *format, char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010649{
Bram Moolenaar071d4272004-06-13 20:20:40 +000010650 if (fname == NULL)
10651 fname = (char_u *)_("Untitled");
Bram Moolenaard9462e32011-04-11 21:35:11 +020010652 vim_snprintf((char *)buff, DIALOG_MSG_SIZE, format, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010653}
10654#endif
10655
10656/*
10657 * ":behave {mswin,xterm}"
10658 */
10659 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010660ex_behave(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010661{
10662 if (STRCMP(eap->arg, "mswin") == 0)
10663 {
10664 set_option_value((char_u *)"selection", 0L, (char_u *)"exclusive", 0);
10665 set_option_value((char_u *)"selectmode", 0L, (char_u *)"mouse,key", 0);
10666 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"popup", 0);
10667 set_option_value((char_u *)"keymodel", 0L,
10668 (char_u *)"startsel,stopsel", 0);
10669 }
10670 else if (STRCMP(eap->arg, "xterm") == 0)
10671 {
10672 set_option_value((char_u *)"selection", 0L, (char_u *)"inclusive", 0);
10673 set_option_value((char_u *)"selectmode", 0L, (char_u *)"", 0);
10674 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"extend", 0);
10675 set_option_value((char_u *)"keymodel", 0L, (char_u *)"", 0);
10676 }
10677 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010678 semsg(_(e_invarg2), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010679}
10680
Bram Moolenaar42b4dda2010-03-02 15:56:05 +010010681#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
10682/*
10683 * Function given to ExpandGeneric() to obtain the possible arguments of the
10684 * ":behave {mswin,xterm}" command.
10685 */
10686 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010687get_behave_arg(expand_T *xp UNUSED, int idx)
Bram Moolenaar42b4dda2010-03-02 15:56:05 +010010688{
10689 if (idx == 0)
10690 return (char_u *)"mswin";
10691 if (idx == 1)
10692 return (char_u *)"xterm";
10693 return NULL;
10694}
Bram Moolenaar9e507ca2016-10-15 15:39:39 +020010695
10696/*
10697 * Function given to ExpandGeneric() to obtain the possible arguments of the
10698 * ":messages {clear}" command.
10699 */
10700 char_u *
10701get_messages_arg(expand_T *xp UNUSED, int idx)
10702{
10703 if (idx == 0)
10704 return (char_u *)"clear";
10705 return NULL;
10706}
Bram Moolenaar42b4dda2010-03-02 15:56:05 +010010707#endif
10708
Bram Moolenaar113e1072019-01-20 15:30:40 +010010709#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
Bram Moolenaarcae92dc2017-08-06 15:22:15 +020010710 char_u *
10711get_mapclear_arg(expand_T *xp UNUSED, int idx)
10712{
10713 if (idx == 0)
10714 return (char_u *)"<buffer>";
10715 return NULL;
10716}
Bram Moolenaar113e1072019-01-20 15:30:40 +010010717#endif
Bram Moolenaarcae92dc2017-08-06 15:22:15 +020010718
Bram Moolenaar071d4272004-06-13 20:20:40 +000010719static int filetype_detect = FALSE;
10720static int filetype_plugin = FALSE;
10721static int filetype_indent = FALSE;
10722
10723/*
10724 * ":filetype [plugin] [indent] {on,off,detect}"
10725 * on: Load the filetype.vim file to install autocommands for file types.
10726 * off: Load the ftoff.vim file to remove all autocommands for file types.
10727 * plugin on: load filetype.vim and ftplugin.vim
10728 * plugin off: load ftplugof.vim
10729 * indent on: load filetype.vim and indent.vim
10730 * indent off: load indoff.vim
10731 */
10732 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010733ex_filetype(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010734{
10735 char_u *arg = eap->arg;
10736 int plugin = FALSE;
10737 int indent = FALSE;
10738
10739 if (*eap->arg == NUL)
10740 {
10741 /* Print current status. */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010742 smsg("filetype detection:%s plugin:%s indent:%s",
Bram Moolenaar071d4272004-06-13 20:20:40 +000010743 filetype_detect ? "ON" : "OFF",
10744 filetype_plugin ? (filetype_detect ? "ON" : "(on)") : "OFF",
10745 filetype_indent ? (filetype_detect ? "ON" : "(on)") : "OFF");
10746 return;
10747 }
10748
10749 /* Accept "plugin" and "indent" in any order. */
10750 for (;;)
10751 {
10752 if (STRNCMP(arg, "plugin", 6) == 0)
10753 {
10754 plugin = TRUE;
10755 arg = skipwhite(arg + 6);
10756 continue;
10757 }
10758 if (STRNCMP(arg, "indent", 6) == 0)
10759 {
10760 indent = TRUE;
10761 arg = skipwhite(arg + 6);
10762 continue;
10763 }
10764 break;
10765 }
10766 if (STRCMP(arg, "on") == 0 || STRCMP(arg, "detect") == 0)
10767 {
10768 if (*arg == 'o' || !filetype_detect)
10769 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010010770 source_runtime((char_u *)FILETYPE_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010771 filetype_detect = TRUE;
10772 if (plugin)
10773 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010010774 source_runtime((char_u *)FTPLUGIN_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010775 filetype_plugin = TRUE;
10776 }
10777 if (indent)
10778 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010010779 source_runtime((char_u *)INDENT_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010780 filetype_indent = TRUE;
10781 }
10782 }
10783 if (*arg == 'd')
10784 {
Bram Moolenaar1610d052016-06-09 22:53:01 +020010785 (void)do_doautocmd((char_u *)"filetypedetect BufRead", TRUE, NULL);
Bram Moolenaara3227e22006-03-08 21:32:40 +000010786 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010787 }
10788 }
10789 else if (STRCMP(arg, "off") == 0)
10790 {
10791 if (plugin || indent)
10792 {
10793 if (plugin)
10794 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010010795 source_runtime((char_u *)FTPLUGOF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010796 filetype_plugin = FALSE;
10797 }
10798 if (indent)
10799 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010010800 source_runtime((char_u *)INDOFF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010801 filetype_indent = FALSE;
10802 }
10803 }
10804 else
10805 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010010806 source_runtime((char_u *)FTOFF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010807 filetype_detect = FALSE;
10808 }
10809 }
10810 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010811 semsg(_(e_invarg2), arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010812}
10813
10814/*
Bram Moolenaar3e545692017-06-04 19:00:32 +020010815 * ":setfiletype [FALLBACK] {name}"
Bram Moolenaar071d4272004-06-13 20:20:40 +000010816 */
10817 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010818ex_setfiletype(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010819{
10820 if (!did_filetype)
Bram Moolenaar3e545692017-06-04 19:00:32 +020010821 {
10822 char_u *arg = eap->arg;
10823
10824 if (STRNCMP(arg, "FALLBACK ", 9) == 0)
10825 arg += 9;
10826
10827 set_option_value((char_u *)"filetype", 0L, arg, OPT_LOCAL);
10828 if (arg != eap->arg)
10829 did_filetype = FALSE;
10830 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010831}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010832
Bram Moolenaar071d4272004-06-13 20:20:40 +000010833 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010834ex_digraphs(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010835{
10836#ifdef FEAT_DIGRAPHS
10837 if (*eap->arg != NUL)
10838 putdigraph(eap->arg);
10839 else
Bram Moolenaareae8ae12018-12-14 18:53:02 +010010840 listdigraphs(eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010841#else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010842 emsg(_("E196: No digraphs in this version"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010843#endif
10844}
10845
10846 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010847ex_set(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010848{
10849 int flags = 0;
10850
10851 if (eap->cmdidx == CMD_setlocal)
10852 flags = OPT_LOCAL;
10853 else if (eap->cmdidx == CMD_setglobal)
10854 flags = OPT_GLOBAL;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +010010855#if defined(FEAT_EVAL) && defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010856 if (cmdmod.browse && flags == 0)
10857 ex_options(eap);
10858 else
10859#endif
10860 (void)do_set(eap->arg, flags);
10861}
10862
Bram Moolenaar451fc7b2018-04-27 22:53:07 +020010863#if defined(FEAT_SEARCH_EXTRA) || defined(PROTO)
10864 void
10865set_no_hlsearch(int flag)
10866{
10867 no_hlsearch = flag;
10868# ifdef FEAT_EVAL
10869 set_vim_var_nr(VV_HLSEARCH, !no_hlsearch && p_hls);
10870# endif
10871}
10872
Bram Moolenaar071d4272004-06-13 20:20:40 +000010873/*
10874 * ":nohlsearch"
10875 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010876 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010877ex_nohlsearch(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010878{
Bram Moolenaar451fc7b2018-04-27 22:53:07 +020010879 set_no_hlsearch(TRUE);
Bram Moolenaarf71a3db2006-03-12 21:50:18 +000010880 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010881}
10882
10883/*
Bram Moolenaare1438bb2006-03-01 22:01:55 +000010884 * ":[N]match {group} {pattern}"
Bram Moolenaar071d4272004-06-13 20:20:40 +000010885 * Sets nextcmd to the start of the next command, if any. Also called when
10886 * skipping commands to find the next command.
10887 */
10888 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010889ex_match(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010890{
10891 char_u *p;
Bram Moolenaar52d36c82007-08-11 14:00:30 +000010892 char_u *g = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010893 char_u *end;
10894 int c;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000010895 int id;
Bram Moolenaare1438bb2006-03-01 22:01:55 +000010896
10897 if (eap->line2 <= 3)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000010898 id = eap->line2;
Bram Moolenaare1438bb2006-03-01 22:01:55 +000010899 else
10900 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010901 emsg(_(e_invcmd));
Bram Moolenaare1438bb2006-03-01 22:01:55 +000010902 return;
10903 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010904
10905 /* First clear any old pattern. */
10906 if (!eap->skip)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000010907 match_delete(curwin, id, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010908
10909 if (ends_excmd(*eap->arg))
10910 end = eap->arg;
10911 else if ((STRNICMP(eap->arg, "none", 4) == 0
Bram Moolenaar1c465442017-03-12 20:10:05 +010010912 && (VIM_ISWHITE(eap->arg[4]) || ends_excmd(eap->arg[4]))))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010913 end = eap->arg + 4;
10914 else
10915 {
10916 p = skiptowhite(eap->arg);
10917 if (!eap->skip)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000010918 g = vim_strnsave(eap->arg, (int)(p - eap->arg));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010919 p = skipwhite(p);
10920 if (*p == NUL)
10921 {
10922 /* There must be two arguments. */
Bram Moolenaar9a7d58e2015-11-24 17:23:56 +010010923 vim_free(g);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010924 semsg(_(e_invarg2), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010925 return;
10926 }
10927 end = skip_regexp(p + 1, *p, TRUE, NULL);
10928 if (!eap->skip)
10929 {
10930 if (*end != NUL && !ends_excmd(*skipwhite(end + 1)))
10931 {
Bram Moolenaar9a7d58e2015-11-24 17:23:56 +010010932 vim_free(g);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010933 eap->errmsg = e_trailing;
10934 return;
10935 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000010936 if (*end != *p)
10937 {
Bram Moolenaar9a7d58e2015-11-24 17:23:56 +010010938 vim_free(g);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010939 semsg(_(e_invarg2), p);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000010940 return;
10941 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010942
10943 c = *end;
10944 *end = NUL;
Bram Moolenaar6561d522015-07-21 15:48:27 +020010945 match_add(curwin, g, p + 1, 10, id, NULL, NULL);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000010946 vim_free(g);
Bram Moolenaar910f66f2006-04-05 20:41:53 +000010947 *end = c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010948 }
10949 }
10950 eap->nextcmd = find_nextcmd(end);
10951}
10952#endif
10953
10954#ifdef FEAT_CRYPT
10955/*
10956 * ":X": Get crypt key
10957 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010958 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010959ex_X(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010960{
Bram Moolenaar3a0c9082014-11-12 15:15:42 +010010961 crypt_check_current_method();
Bram Moolenaar8f4ac012014-08-10 13:38:34 +020010962 (void)crypt_get_key(TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010963}
10964#endif
10965
10966#ifdef FEAT_FOLDING
10967 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010968ex_fold(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010969{
10970 if (foldManualAllowed(TRUE))
10971 foldCreate(eap->line1, eap->line2);
10972}
10973
10974 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010975ex_foldopen(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010976{
10977 opFoldRange(eap->line1, eap->line2, eap->cmdidx == CMD_foldopen,
10978 eap->forceit, FALSE);
10979}
10980
10981 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010982ex_folddo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010983{
10984 linenr_T lnum;
10985
Bram Moolenaar6b1ee342014-08-06 18:17:11 +020010986#ifdef FEAT_CLIPBOARD
10987 start_global_changes();
10988#endif
10989
Bram Moolenaar071d4272004-06-13 20:20:40 +000010990 /* First set the marks for all lines closed/open. */
10991 for (lnum = eap->line1; lnum <= eap->line2; ++lnum)
10992 if (hasFolding(lnum, NULL, NULL) == (eap->cmdidx == CMD_folddoclosed))
10993 ml_setmarked(lnum);
10994
10995 /* Execute the command on the marked lines. */
10996 global_exe(eap->arg);
10997 ml_clearmarked(); /* clear rest of the marks */
Bram Moolenaar6b1ee342014-08-06 18:17:11 +020010998#ifdef FEAT_CLIPBOARD
10999 end_global_changes();
11000#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011001}
11002#endif
Bram Moolenaarf5291f32017-09-14 22:55:37 +020011003
Bram Moolenaar39665952018-08-15 20:59:48 +020011004#ifdef FEAT_QUICKFIX
11005/*
11006 * Returns TRUE if the supplied Ex cmdidx is for a location list command
11007 * instead of a quickfix command.
11008 */
11009 int
11010is_loclist_cmd(int cmdidx)
11011{
Bram Moolenaar74c8be22018-08-23 22:51:40 +020011012 if (cmdidx < 0 || cmdidx >= CMD_SIZE)
Bram Moolenaar39665952018-08-15 20:59:48 +020011013 return FALSE;
11014 return cmdnames[cmdidx].cmd_name[0] == 'l';
11015}
11016#endif
11017
Bram Moolenaarf5291f32017-09-14 22:55:37 +020011018# if defined(FEAT_TIMERS) || defined(PROTO)
11019 int
11020get_pressedreturn(void)
11021{
11022 return ex_pressedreturn;
11023}
11024
11025 void
11026set_pressedreturn(int val)
11027{
11028 ex_pressedreturn = val;
11029}
11030#endif