blob: 744469783467bc5409d2113f5e160a3e05c620c0 [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * ex_docmd.c: functions for executing an Ex command line.
12 */
13
14#include "vim.h"
15
Bram Moolenaar071d4272004-06-13 20:20:40 +000016static int quitmore = 0;
17static int ex_pressedreturn = FALSE;
18#ifndef FEAT_PRINTER
19# define ex_hardcopy ex_ni
20#endif
21
Bram Moolenaar071d4272004-06-13 20:20:40 +000022#ifdef FEAT_EVAL
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010023static char_u *do_one_cmd(char_u **, int, struct condstack *, char_u *(*fgetline)(int, void *, int), void *cookie);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024#else
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010025static char_u *do_one_cmd(char_u **, int, char_u *(*fgetline)(int, void *, int), void *cookie);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026static int if_level = 0; /* depth in :if */
27#endif
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +020028static void free_cmdmod(void);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010029static void append_command(char_u *cmd);
30static char_u *find_command(exarg_T *eap, int *full);
Bram Moolenaar071d4272004-06-13 20:20:40 +000031
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010032static void ex_abbreviate(exarg_T *eap);
33static void ex_map(exarg_T *eap);
34static void ex_unmap(exarg_T *eap);
35static void ex_mapclear(exarg_T *eap);
36static void ex_abclear(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +000037#ifndef FEAT_MENU
38# define ex_emenu ex_ni
39# define ex_menu ex_ni
40# define ex_menutranslate ex_ni
41#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010042static void ex_autocmd(exarg_T *eap);
43static void ex_doautocmd(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010044static void ex_bunload(exarg_T *eap);
45static void ex_buffer(exarg_T *eap);
46static void ex_bmodified(exarg_T *eap);
47static void ex_bnext(exarg_T *eap);
48static void ex_bprevious(exarg_T *eap);
49static void ex_brewind(exarg_T *eap);
50static void ex_blast(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010051static char_u *getargcmd(char_u **);
52static char_u *skip_cmd_arg(char_u *p, int rembs);
53static int getargopt(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +000054#ifndef FEAT_QUICKFIX
55# define ex_make ex_ni
Bram Moolenaar86b68352004-12-27 21:59:20 +000056# define ex_cbuffer ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +000057# define ex_cc ex_ni
58# define ex_cnext ex_ni
59# define ex_cfile ex_ni
60# define qf_list ex_ni
61# define qf_age ex_ni
Bram Moolenaarf6acffb2016-07-16 16:54:24 +020062# define qf_history ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +000063# define ex_helpgrep ex_ni
Bram Moolenaar86b68352004-12-27 21:59:20 +000064# define ex_vimgrep ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +000065#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +020066#if !defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +000067# define ex_cclose ex_ni
68# define ex_copen ex_ni
69# define ex_cwindow ex_ni
Bram Moolenaardcb17002016-07-07 18:58:59 +020070# define ex_cbottom ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +000071#endif
Bram Moolenaar1e015462005-09-25 22:16:38 +000072#if !defined(FEAT_QUICKFIX) || !defined(FEAT_EVAL)
73# define ex_cexpr ex_ni
74#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000075
Bram Moolenaar50eb16c2018-09-15 15:42:40 +020076static linenr_T get_address(exarg_T *, char_u **, int addr_type, int skip, int silent, int to_other_file, int address_count);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010077static void get_flags(exarg_T *eap);
Bram Moolenaar85363ab2010-07-18 13:58:26 +020078#if !defined(FEAT_PERL) \
79 || !defined(FEAT_PYTHON) || !defined(FEAT_PYTHON3) \
80 || !defined(FEAT_TCL) \
81 || !defined(FEAT_RUBY) \
82 || !defined(FEAT_LUA) \
83 || !defined(FEAT_MZSCHEME)
Bram Moolenaar7bb75552007-07-16 18:39:49 +000084# define HAVE_EX_SCRIPT_NI
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010085static void ex_script_ni(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +000086#endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010087static char *invalid_range(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010088static void correct_range(exarg_T *eap);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000089#ifdef FEAT_QUICKFIX
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010090static char_u *replace_makeprg(exarg_T *eap, char_u *p, char_u **cmdlinep);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000091#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010092static char_u *repl_cmdline(exarg_T *eap, char_u *src, int srclen, char_u *repl, char_u **cmdlinep);
93static void ex_highlight(exarg_T *eap);
94static void ex_colorscheme(exarg_T *eap);
95static void ex_quit(exarg_T *eap);
96static void ex_cquit(exarg_T *eap);
97static void ex_quit_all(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010098static void ex_close(exarg_T *eap);
99static void ex_win_close(int forceit, win_T *win, tabpage_T *tp);
100static void ex_only(exarg_T *eap);
101static void ex_resize(exarg_T *eap);
102static void ex_stag(exarg_T *eap);
103static void ex_tabclose(exarg_T *eap);
104static void ex_tabonly(exarg_T *eap);
105static void ex_tabnext(exarg_T *eap);
106static void ex_tabmove(exarg_T *eap);
107static void ex_tabs(exarg_T *eap);
Bram Moolenaar4033c552017-09-16 20:54:51 +0200108#if defined(FEAT_QUICKFIX)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100109static void ex_pclose(exarg_T *eap);
110static void ex_ptag(exarg_T *eap);
111static void ex_pedit(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000112#else
113# define ex_pclose ex_ni
114# define ex_ptag ex_ni
115# define ex_pedit ex_ni
116#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100117static void ex_hide(exarg_T *eap);
118static void ex_stop(exarg_T *eap);
119static void ex_exit(exarg_T *eap);
120static void ex_print(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000121#ifdef FEAT_BYTEOFF
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100122static void ex_goto(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000123#else
124# define ex_goto ex_ni
125#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100126static void ex_shell(exarg_T *eap);
127static void ex_preserve(exarg_T *eap);
128static void ex_recover(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100129static void ex_mode(exarg_T *eap);
130static void ex_wrongmodifier(exarg_T *eap);
131static void ex_find(exarg_T *eap);
132static void ex_open(exarg_T *eap);
133static void ex_edit(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000134#ifndef FEAT_GUI
135# define ex_gui ex_nogui
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100136static void ex_nogui(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000137#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +0100138#if defined(FEAT_GUI_MSWIN) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100139static void ex_tearoff(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000140#else
141# define ex_tearoff ex_ni
142#endif
Bram Moolenaar29a2c082018-03-05 21:06:23 +0100143#if (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK) \
144 || defined(FEAT_TERM_POPUP_MENU)) && defined(FEAT_MENU)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100145static void ex_popup(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000146#else
147# define ex_popup ex_ni
148#endif
149#ifndef FEAT_GUI_MSWIN
150# define ex_simalt ex_ni
151#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +0000152#if !defined(FEAT_GUI_MSWIN) && !defined(FEAT_GUI_GTK) && !defined(FEAT_GUI_MOTIF)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000153# define gui_mch_find_dialog ex_ni
154# define gui_mch_replace_dialog ex_ni
155#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +0000156#if !defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000157# define ex_helpfind ex_ni
158#endif
159#ifndef FEAT_CSCOPE
Bram Moolenaard4db7712016-11-12 19:16:46 +0100160# define ex_cscope ex_ni
161# define ex_scscope ex_ni
162# define ex_cstag ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000163#endif
164#ifndef FEAT_SYN_HL
165# define ex_syntax ex_ni
Bram Moolenaar860cae12010-06-05 23:22:07 +0200166# define ex_ownsyntax ex_ni
Bram Moolenaarf71a3db2006-03-12 21:50:18 +0000167#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100168#ifndef FEAT_EVAL
169# define ex_packadd ex_ni
170# define ex_packloadall ex_ni
171#endif
Bram Moolenaarf7512552013-06-06 14:55:19 +0200172#if !defined(FEAT_SYN_HL) || !defined(FEAT_PROFILE)
Bram Moolenaar8a7f5a22013-06-06 14:01:46 +0200173# define ex_syntime ex_ni
174#endif
Bram Moolenaarf71a3db2006-03-12 21:50:18 +0000175#ifndef FEAT_SPELL
Bram Moolenaarb765d632005-06-07 21:00:02 +0000176# define ex_spell ex_ni
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000177# define ex_mkspell ex_ni
Bram Moolenaarf417f2b2005-06-23 22:29:21 +0000178# define ex_spelldump ex_ni
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000179# define ex_spellinfo ex_ni
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000180# define ex_spellrepall ex_ni
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000181#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200182#ifndef FEAT_PERSISTENT_UNDO
183# define ex_rundo ex_ni
184# define ex_wundo ex_ni
185#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200186#ifndef FEAT_LUA
187# define ex_lua ex_script_ni
188# define ex_luado ex_ni
189# define ex_luafile ex_ni
190#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000191#ifndef FEAT_MZSCHEME
192# define ex_mzscheme ex_script_ni
193# define ex_mzfile ex_ni
194#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000195#ifndef FEAT_PERL
196# define ex_perl ex_script_ni
197# define ex_perldo ex_ni
198#endif
199#ifndef FEAT_PYTHON
200# define ex_python ex_script_ni
Bram Moolenaard620aa92013-05-17 16:40:06 +0200201# define ex_pydo ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000202# define ex_pyfile ex_ni
203#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200204#ifndef FEAT_PYTHON3
Bram Moolenaar368373e2010-07-19 20:46:22 +0200205# define ex_py3 ex_script_ni
Bram Moolenaar3dab2802013-05-15 18:28:13 +0200206# define ex_py3do ex_ni
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200207# define ex_py3file ex_ni
208#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +0100209#if !defined(FEAT_PYTHON) && !defined(FEAT_PYTHON3)
210# define ex_pyx ex_script_ni
211# define ex_pyxdo ex_ni
212# define ex_pyxfile ex_ni
213#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000214#ifndef FEAT_TCL
215# define ex_tcl ex_script_ni
216# define ex_tcldo ex_ni
217# define ex_tclfile ex_ni
218#endif
219#ifndef FEAT_RUBY
220# define ex_ruby ex_script_ni
221# define ex_rubydo ex_ni
222# define ex_rubyfile ex_ni
223#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000224#ifndef FEAT_KEYMAP
225# define ex_loadkeymap ex_ni
226#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100227static void ex_swapname(exarg_T *eap);
228static void ex_syncbind(exarg_T *eap);
229static void ex_read(exarg_T *eap);
230static void ex_pwd(exarg_T *eap);
231static void ex_equal(exarg_T *eap);
232static void ex_sleep(exarg_T *eap);
233static void do_exmap(exarg_T *eap, int isabbrev);
234static void ex_winsize(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100235static void ex_wincmd(exarg_T *eap);
Bram Moolenaar843ee412004-06-30 16:16:41 +0000236#if defined(FEAT_GUI) || defined(UNIX) || defined(VMS) || defined(MSWIN)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100237static void ex_winpos(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000238#else
239# define ex_winpos ex_ni
240#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100241static void ex_operators(exarg_T *eap);
242static void ex_put(exarg_T *eap);
243static void ex_copymove(exarg_T *eap);
244static void ex_submagic(exarg_T *eap);
245static void ex_join(exarg_T *eap);
246static void ex_at(exarg_T *eap);
247static void ex_bang(exarg_T *eap);
248static void ex_undo(exarg_T *eap);
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200249#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100250static void ex_wundo(exarg_T *eap);
251static void ex_rundo(exarg_T *eap);
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200252#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100253static void ex_redo(exarg_T *eap);
254static void ex_later(exarg_T *eap);
255static void ex_redir(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100256static void ex_redrawstatus(exarg_T *eap);
Bram Moolenaare12bab32019-01-08 22:02:56 +0100257static void ex_redrawtabline(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100258static void close_redir(void);
259static void ex_mkrc(exarg_T *eap);
260static void ex_mark(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100261static void ex_startinsert(exarg_T *eap);
262static void ex_stopinsert(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000263#ifdef FEAT_FIND_ID
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100264static void ex_checkpath(exarg_T *eap);
265static void ex_findpat(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000266#else
267# define ex_findpat ex_ni
268# define ex_checkpath ex_ni
269#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +0200270#if defined(FEAT_FIND_ID) && defined(FEAT_QUICKFIX)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100271static void ex_psearch(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000272#else
273# define ex_psearch ex_ni
274#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100275static void ex_tag(exarg_T *eap);
276static void ex_tag_cmd(exarg_T *eap, char_u *name);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000277#ifndef FEAT_EVAL
278# define ex_scriptnames ex_ni
279# define ex_finish ex_ni
280# define ex_echo ex_ni
281# define ex_echohl ex_ni
282# define ex_execute ex_ni
283# define ex_call ex_ni
284# define ex_if ex_ni
285# define ex_endif ex_ni
286# define ex_else ex_ni
287# define ex_while ex_ni
288# define ex_continue ex_ni
289# define ex_break ex_ni
290# define ex_endwhile ex_ni
291# define ex_throw ex_ni
292# define ex_try ex_ni
293# define ex_catch ex_ni
294# define ex_finally ex_ni
295# define ex_endtry ex_ni
296# define ex_endfunction ex_ni
297# define ex_let ex_ni
298# define ex_unlet ex_ni
Bram Moolenaar65c1b012005-01-31 19:02:28 +0000299# define ex_lockvar ex_ni
300# define ex_unlockvar ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000301# define ex_function ex_ni
302# define ex_delfunction ex_ni
303# define ex_return ex_ni
Bram Moolenaard812df62008-11-09 12:46:09 +0000304# define ex_oldfiles ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000305#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100306static char_u *arg_all(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000307#ifdef FEAT_SESSION
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100308static int makeopens(FILE *fd, char_u *dirnow);
309static int put_view(FILE *fd, win_T *wp, int add_edit, unsigned *flagp, int current_arg_idx);
310static void ex_loadview(exarg_T *eap);
311static char_u *get_view_file(int c);
Bram Moolenaareeefcc72007-05-01 21:21:21 +0000312static int did_lcd; /* whether ":lcd" was produced for a session */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000313#else
314# define ex_loadview ex_ni
315#endif
316#ifndef FEAT_EVAL
317# define ex_compiler ex_ni
318#endif
319#ifdef FEAT_VIMINFO
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100320static void ex_viminfo(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000321#else
322# define ex_viminfo ex_ni
323#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100324static void ex_behave(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100325static void ex_filetype(exarg_T *eap);
326static void ex_setfiletype(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000327#ifndef FEAT_DIFF
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000328# define ex_diffoff ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000329# define ex_diffpatch ex_ni
330# define ex_diffgetput ex_ni
331# define ex_diffsplit ex_ni
332# define ex_diffthis ex_ni
333# define ex_diffupdate ex_ni
334#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100335static void ex_digraphs(exarg_T *eap);
336static void ex_set(exarg_T *eap);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100337#if !defined(FEAT_EVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000338# define ex_options ex_ni
339#endif
340#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100341static void ex_nohlsearch(exarg_T *eap);
342static void ex_match(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000343#else
344# define ex_nohlsearch ex_ni
345# define ex_match ex_ni
346#endif
347#ifdef FEAT_CRYPT
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100348static void ex_X(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000349#else
350# define ex_X ex_ni
351#endif
352#ifdef FEAT_FOLDING
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100353static void ex_fold(exarg_T *eap);
354static void ex_foldopen(exarg_T *eap);
355static void ex_folddo(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000356#else
357# define ex_fold ex_ni
358# define ex_foldopen ex_ni
359# define ex_folddo ex_ni
360#endif
Bram Moolenaar13505972019-01-24 15:04:48 +0100361#if !(defined(HAVE_LOCALE_H) || defined(X_LOCALE))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000362# define ex_language ex_ni
363#endif
364#ifndef FEAT_SIGNS
365# define ex_sign ex_ni
366#endif
Bram Moolenaar009b2592004-10-24 19:18:58 +0000367#ifndef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200368# define ex_nbclose ex_ni
Bram Moolenaar009b2592004-10-24 19:18:58 +0000369# define ex_nbkey ex_ni
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200370# define ex_nbstart ex_ni
Bram Moolenaar009b2592004-10-24 19:18:58 +0000371#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000372
373#ifndef FEAT_EVAL
374# define ex_debug ex_ni
375# define ex_breakadd ex_ni
376# define ex_debuggreedy ex_ni
377# define ex_breakdel ex_ni
378# define ex_breaklist ex_ni
379#endif
380
381#ifndef FEAT_CMDHIST
382# define ex_history ex_ni
383#endif
384#ifndef FEAT_JUMPLIST
385# define ex_jumps ex_ni
Bram Moolenaar2d358992016-06-12 21:20:54 +0200386# define ex_clearjumps ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000387# define ex_changes ex_ni
388#endif
389
Bram Moolenaar05159a02005-02-26 23:04:13 +0000390#ifndef FEAT_PROFILE
391# define ex_profile ex_ni
392#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200393#ifndef FEAT_TERMINAL
394# define ex_terminal ex_ni
395#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +0000396
Bram Moolenaar071d4272004-06-13 20:20:40 +0000397/*
398 * Declare cmdnames[].
399 */
400#define DO_DECLARE_EXCMD
401#include "ex_cmds.h"
Bram Moolenaar6de5e122017-04-20 21:55:44 +0200402#include "ex_cmdidxs.h"
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +0100403
Bram Moolenaar071d4272004-06-13 20:20:40 +0000404static char_u dollar_command[2] = {'$', 0};
405
406
407#ifdef FEAT_EVAL
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000408/* Struct for storing a line inside a while/for loop */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000409typedef struct
410{
411 char_u *line; /* command line */
412 linenr_T lnum; /* sourcing_lnum of the line */
413} wcmd_T;
414
415/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000416 * Structure used to store info for line position in a while or for loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000417 * This is required, because do_one_cmd() may invoke ex_function(), which
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000418 * reads more lines that may come from the while/for loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000419 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000420struct loop_cookie
Bram Moolenaar071d4272004-06-13 20:20:40 +0000421{
422 garray_T *lines_gap; /* growarray with line info */
423 int current_line; /* last read line from growarray */
424 int repeating; /* TRUE when looping a second time */
425 /* When "repeating" is FALSE use "getline" and "cookie" to get lines */
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100426 char_u *(*getline)(int, void *, int);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000427 void *cookie;
428};
429
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100430static char_u *get_loop_line(int c, void *cookie, int indent);
431static int store_loop_line(garray_T *gap, char_u *line);
432static void free_cmdlines(garray_T *gap);
Bram Moolenaared203462004-06-16 11:19:22 +0000433
434/* Struct to save a few things while debugging. Used in do_cmdline() only. */
435struct dbg_stuff
436{
437 int trylevel;
438 int force_abort;
439 except_T *caught_stack;
440 char_u *vv_exception;
441 char_u *vv_throwpoint;
442 int did_emsg;
443 int got_int;
444 int did_throw;
445 int need_rethrow;
446 int check_cstack;
447 except_T *current_exception;
448};
449
Bram Moolenaared203462004-06-16 11:19:22 +0000450 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100451save_dbg_stuff(struct dbg_stuff *dsp)
Bram Moolenaared203462004-06-16 11:19:22 +0000452{
453 dsp->trylevel = trylevel; trylevel = 0;
454 dsp->force_abort = force_abort; force_abort = FALSE;
455 dsp->caught_stack = caught_stack; caught_stack = NULL;
456 dsp->vv_exception = v_exception(NULL);
457 dsp->vv_throwpoint = v_throwpoint(NULL);
458
459 /* Necessary for debugging an inactive ":catch", ":finally", ":endtry" */
460 dsp->did_emsg = did_emsg; did_emsg = FALSE;
461 dsp->got_int = got_int; got_int = FALSE;
462 dsp->did_throw = did_throw; did_throw = FALSE;
463 dsp->need_rethrow = need_rethrow; need_rethrow = FALSE;
464 dsp->check_cstack = check_cstack; check_cstack = FALSE;
465 dsp->current_exception = current_exception; current_exception = NULL;
466}
467
468 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100469restore_dbg_stuff(struct dbg_stuff *dsp)
Bram Moolenaared203462004-06-16 11:19:22 +0000470{
471 suppress_errthrow = FALSE;
472 trylevel = dsp->trylevel;
473 force_abort = dsp->force_abort;
474 caught_stack = dsp->caught_stack;
475 (void)v_exception(dsp->vv_exception);
476 (void)v_throwpoint(dsp->vv_throwpoint);
477 did_emsg = dsp->did_emsg;
478 got_int = dsp->got_int;
479 did_throw = dsp->did_throw;
480 need_rethrow = dsp->need_rethrow;
481 check_cstack = dsp->check_cstack;
482 current_exception = dsp->current_exception;
483}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000484#endif
485
Bram Moolenaar071d4272004-06-13 20:20:40 +0000486/*
487 * do_exmode(): Repeatedly get commands for the "Ex" mode, until the ":vi"
488 * command is given.
489 */
490 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100491do_exmode(
492 int improved) /* TRUE for "improved Ex" mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000493{
494 int save_msg_scroll;
495 int prev_msg_row;
496 linenr_T prev_line;
Bram Moolenaar79518e22017-02-17 16:31:35 +0100497 varnumber_T changedtick;
Bram Moolenaardf177f62005-02-22 08:39:57 +0000498
499 if (improved)
500 exmode_active = EXMODE_VIM;
501 else
502 exmode_active = EXMODE_NORMAL;
503 State = NORMAL;
504
505 /* When using ":global /pat/ visual" and then "Q" we return to continue
506 * the :global command. */
507 if (global_busy)
508 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000509
510 save_msg_scroll = msg_scroll;
511 ++RedrawingDisabled; /* don't redisplay the window */
512 ++no_wait_return; /* don't wait for return */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000513#ifdef FEAT_GUI
514 /* Ignore scrollbar and mouse events in Ex mode */
515 ++hold_gui_events;
516#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000517
Bram Moolenaar32526b32019-01-19 17:43:09 +0100518 msg(_("Entering Ex mode. Type \"visual\" to go to Normal mode."));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000519 while (exmode_active)
520 {
Bram Moolenaar7c626922005-02-07 22:01:03 +0000521 /* Check for a ":normal" command and no more characters left. */
522 if (ex_normal_busy > 0 && typebuf.tb_len == 0)
523 {
524 exmode_active = FALSE;
525 break;
526 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000527 msg_scroll = TRUE;
528 need_wait_return = FALSE;
529 ex_pressedreturn = FALSE;
530 ex_no_reprint = FALSE;
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100531 changedtick = CHANGEDTICK(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000532 prev_msg_row = msg_row;
533 prev_line = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000534 if (improved)
535 {
536 cmdline_row = msg_row;
537 do_cmdline(NULL, getexline, NULL, 0);
538 }
539 else
540 do_cmdline(NULL, getexmodeline, NULL, DOCMD_NOWAIT);
541 lines_left = Rows - 1;
542
Bram Moolenaardf177f62005-02-22 08:39:57 +0000543 if ((prev_line != curwin->w_cursor.lnum
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100544 || changedtick != CHANGEDTICK(curbuf)) && !ex_no_reprint)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000545 {
Bram Moolenaardf177f62005-02-22 08:39:57 +0000546 if (curbuf->b_ml.ml_flags & ML_EMPTY)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100547 emsg(_(e_emptybuf));
Bram Moolenaardf177f62005-02-22 08:39:57 +0000548 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000549 {
Bram Moolenaardf177f62005-02-22 08:39:57 +0000550 if (ex_pressedreturn)
551 {
552 /* go up one line, to overwrite the ":<CR>" line, so the
Bram Moolenaar81870892007-11-11 18:17:28 +0000553 * output doesn't contain empty lines. */
Bram Moolenaardf177f62005-02-22 08:39:57 +0000554 msg_row = prev_msg_row;
555 if (prev_msg_row == Rows - 1)
556 msg_row--;
557 }
558 msg_col = 0;
559 print_line_no_prefix(curwin->w_cursor.lnum, FALSE, FALSE);
560 msg_clr_eos();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000561 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000562 }
Bram Moolenaardf177f62005-02-22 08:39:57 +0000563 else if (ex_pressedreturn && !ex_no_reprint) /* must be at EOF */
564 {
565 if (curbuf->b_ml.ml_flags & ML_EMPTY)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100566 emsg(_(e_emptybuf));
Bram Moolenaardf177f62005-02-22 08:39:57 +0000567 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100568 emsg(_("E501: At end-of-file"));
Bram Moolenaardf177f62005-02-22 08:39:57 +0000569 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000570 }
571
572#ifdef FEAT_GUI
573 --hold_gui_events;
574#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000575 --RedrawingDisabled;
576 --no_wait_return;
577 update_screen(CLEAR);
578 need_wait_return = FALSE;
579 msg_scroll = save_msg_scroll;
580}
581
582/*
583 * Execute a simple command line. Used for translated commands like "*".
584 */
585 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100586do_cmdline_cmd(char_u *cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000587{
588 return do_cmdline(cmd, NULL, NULL,
589 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
590}
591
592/*
593 * do_cmdline(): execute one Ex command line
594 *
595 * 1. Execute "cmdline" when it is not NULL.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100596 * If "cmdline" is NULL, or more lines are needed, fgetline() is used.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000597 * 2. Split up in parts separated with '|'.
598 *
599 * This function can be called recursively!
600 *
601 * flags:
602 * DOCMD_VERBOSE - The command will be included in the error message.
603 * DOCMD_NOWAIT - Don't call wait_return() and friends.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100604 * DOCMD_REPEAT - Repeat execution until fgetline() returns NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000605 * DOCMD_KEYTYPED - Don't reset KeyTyped.
606 * DOCMD_EXCRESET - Reset the exception environment (used for debugging).
607 * DOCMD_KEEPLINE - Store first typed line (for repeating with ".").
608 *
609 * return FAIL if cmdline could not be executed, OK otherwise
610 */
611 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100612do_cmdline(
613 char_u *cmdline,
614 char_u *(*fgetline)(int, void *, int),
615 void *cookie, /* argument for fgetline() */
616 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000617{
618 char_u *next_cmdline; /* next cmd to execute */
619 char_u *cmdline_copy = NULL; /* copy of cmd line */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100620 int used_getline = FALSE; /* used "fgetline" to obtain command */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000621 static int recursive = 0; /* recursive depth */
622 int msg_didout_before_start = 0;
623 int count = 0; /* line number count */
624 int did_inc = FALSE; /* incremented RedrawingDisabled */
625 int retval = OK;
626#ifdef FEAT_EVAL
627 struct condstack cstack; /* conditional stack */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000628 garray_T lines_ga; /* keep lines for ":while"/":for" */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000629 int current_line = 0; /* active line in lines_ga */
630 char_u *fname = NULL; /* function or script name */
631 linenr_T *breakpoint = NULL; /* ptr to breakpoint field in cookie */
632 int *dbg_tick = NULL; /* ptr to dbg_tick field in cookie */
Bram Moolenaared203462004-06-16 11:19:22 +0000633 struct dbg_stuff debug_saved; /* saved things for debug mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000634 int initial_trylevel;
635 struct msglist **saved_msg_list = NULL;
636 struct msglist *private_msg_list;
637
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100638 /* "fgetline" and "cookie" passed to do_one_cmd() */
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100639 char_u *(*cmd_getline)(int, void *, int);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000640 void *cmd_cookie;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000641 struct loop_cookie cmd_loop_cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000642 void *real_cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +0000643 int getline_is_func;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000644#else
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100645# define cmd_getline fgetline
Bram Moolenaar071d4272004-06-13 20:20:40 +0000646# define cmd_cookie cookie
647#endif
648 static int call_depth = 0; /* recursiveness */
649
650#ifdef FEAT_EVAL
651 /* For every pair of do_cmdline()/do_one_cmd() calls, use an extra memory
652 * location for storing error messages to be converted to an exception.
Bram Moolenaarcf3630f2005-01-08 16:04:29 +0000653 * This ensures that the do_errthrow() call in do_one_cmd() does not
654 * combine the messages stored by an earlier invocation of do_one_cmd()
655 * with the command name of the later one. This would happen when
656 * BufWritePost autocommands are executed after a write error. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000657 saved_msg_list = msg_list;
658 msg_list = &private_msg_list;
659 private_msg_list = NULL;
660#endif
661
662 /* It's possible to create an endless loop with ":execute", catch that
Bram Moolenaar777b30f2017-01-02 15:26:27 +0100663 * here. The value of 200 allows nested function calls, ":source", etc.
664 * Allow 200 or 'maxfuncdepth', whatever is larger. */
Bram Moolenaarb094ff42017-01-02 16:16:39 +0100665 if (call_depth >= 200
666#ifdef FEAT_EVAL
667 && call_depth >= p_mfd
668#endif
669 )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000670 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100671 emsg(_("E169: Command too recursive"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000672#ifdef FEAT_EVAL
673 /* When converting to an exception, we do not include the command name
674 * since this is not an error of the specific command. */
675 do_errthrow((struct condstack *)NULL, (char_u *)NULL);
676 msg_list = saved_msg_list;
677#endif
678 return FAIL;
679 }
680 ++call_depth;
681
682#ifdef FEAT_EVAL
683 cstack.cs_idx = -1;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000684 cstack.cs_looplevel = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000685 cstack.cs_trylevel = 0;
686 cstack.cs_emsg_silent_list = NULL;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000687 cstack.cs_lflags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000688 ga_init2(&lines_ga, (int)sizeof(wcmd_T), 10);
689
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100690 real_cookie = getline_cookie(fgetline, cookie);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000691
692 /* Inside a function use a higher nesting level. */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100693 getline_is_func = getline_equal(fgetline, cookie, get_func_line);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000694 if (getline_is_func && ex_nesting_level == func_level(real_cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000695 ++ex_nesting_level;
696
697 /* Get the function or script name and the address where the next breakpoint
698 * line and the debug tick for a function or script are stored. */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000699 if (getline_is_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000700 {
701 fname = func_name(real_cookie);
702 breakpoint = func_breakpoint(real_cookie);
703 dbg_tick = func_dbg_tick(real_cookie);
704 }
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100705 else if (getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000706 {
707 fname = sourcing_name;
708 breakpoint = source_breakpoint(real_cookie);
709 dbg_tick = source_dbg_tick(real_cookie);
710 }
711
712 /*
713 * Initialize "force_abort" and "suppress_errthrow" at the top level.
714 */
715 if (!recursive)
716 {
717 force_abort = FALSE;
718 suppress_errthrow = FALSE;
719 }
720
721 /*
722 * If requested, store and reset the global values controlling the
Bram Moolenaar89d40322006-08-29 15:30:07 +0000723 * exception handling (used when debugging). Otherwise clear it to avoid
724 * a bogus compiler warning when the optimizer uses inline functions...
Bram Moolenaar071d4272004-06-13 20:20:40 +0000725 */
Bram Moolenaarb4872942006-05-13 10:32:52 +0000726 if (flags & DOCMD_EXCRESET)
Bram Moolenaared203462004-06-16 11:19:22 +0000727 save_dbg_stuff(&debug_saved);
Bram Moolenaar89d40322006-08-29 15:30:07 +0000728 else
Bram Moolenaar69b67f72015-09-25 16:59:47 +0200729 vim_memset(&debug_saved, 0, sizeof(debug_saved));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000730
731 initial_trylevel = trylevel;
732
733 /*
734 * "did_throw" will be set to TRUE when an exception is being thrown.
735 */
736 did_throw = FALSE;
737#endif
738 /*
739 * "did_emsg" will be set to TRUE when emsg() is used, in which case we
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000740 * cancel the whole command line, and any if/endif or loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000741 * If force_abort is set, we cancel everything.
742 */
743 did_emsg = FALSE;
744
745 /*
746 * KeyTyped is only set when calling vgetc(). Reset it here when not
747 * calling vgetc() (sourced command lines).
748 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100749 if (!(flags & DOCMD_KEYTYPED)
750 && !getline_equal(fgetline, cookie, getexline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000751 KeyTyped = FALSE;
752
753 /*
754 * Continue executing command lines:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000755 * - when inside an ":if", ":while" or ":for"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000756 * - for multiple commands on one line, separated with '|'
757 * - when repeating until there are no more lines (for ":source")
758 */
759 next_cmdline = cmdline;
760 do
761 {
Bram Moolenaar05159a02005-02-26 23:04:13 +0000762#ifdef FEAT_EVAL
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100763 getline_is_func = getline_equal(fgetline, cookie, get_func_line);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000764#endif
765
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000766 /* stop skipping cmds for an error msg after all endif/while/for */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000767 if (next_cmdline == NULL
768#ifdef FEAT_EVAL
769 && !force_abort
770 && cstack.cs_idx < 0
Bram Moolenaar05159a02005-02-26 23:04:13 +0000771 && !(getline_is_func && func_has_abort(real_cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000772#endif
773 )
774 did_emsg = FALSE;
775
776 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000777 * 1. If repeating a line in a loop, get a line from lines_ga.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100778 * 2. If no line given: Get an allocated line with fgetline().
Bram Moolenaar071d4272004-06-13 20:20:40 +0000779 * 3. If a line is given: Make a copy, so we can mess with it.
780 */
781
782#ifdef FEAT_EVAL
783 /* 1. If repeating, get a previous line from lines_ga. */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000784 if (cstack.cs_looplevel > 0 && current_line < lines_ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000785 {
786 /* Each '|' separated command is stored separately in lines_ga, to
787 * be able to jump to it. Don't use next_cmdline now. */
Bram Moolenaard23a8232018-02-10 18:45:26 +0100788 VIM_CLEAR(cmdline_copy);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000789
790 /* Check if a function has returned or, unless it has an unclosed
791 * try conditional, aborted. */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000792 if (getline_is_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000793 {
Bram Moolenaar05159a02005-02-26 23:04:13 +0000794# ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000795 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +0000796 func_line_end(real_cookie);
797# endif
798 if (func_has_ended(real_cookie))
799 {
800 retval = FAIL;
801 break;
802 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000803 }
Bram Moolenaar05159a02005-02-26 23:04:13 +0000804#ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000805 else if (do_profiling == PROF_YES
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100806 && getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar05159a02005-02-26 23:04:13 +0000807 script_line_end();
808#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000809
810 /* Check if a sourced file hit a ":finish" command. */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100811 if (source_finished(fgetline, cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000812 {
813 retval = FAIL;
814 break;
815 }
816
817 /* If breakpoints have been added/deleted need to check for it. */
818 if (breakpoint != NULL && dbg_tick != NULL
819 && *dbg_tick != debug_tick)
820 {
821 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100822 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar071d4272004-06-13 20:20:40 +0000823 fname, sourcing_lnum);
824 *dbg_tick = debug_tick;
825 }
826
827 next_cmdline = ((wcmd_T *)(lines_ga.ga_data))[current_line].line;
828 sourcing_lnum = ((wcmd_T *)(lines_ga.ga_data))[current_line].lnum;
829
830 /* Did we encounter a breakpoint? */
831 if (breakpoint != NULL && *breakpoint != 0
832 && *breakpoint <= sourcing_lnum)
833 {
834 dbg_breakpoint(fname, sourcing_lnum);
835 /* Find next breakpoint. */
836 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100837 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar071d4272004-06-13 20:20:40 +0000838 fname, sourcing_lnum);
839 *dbg_tick = debug_tick;
840 }
Bram Moolenaar05159a02005-02-26 23:04:13 +0000841# ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000842 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +0000843 {
844 if (getline_is_func)
845 func_line_start(real_cookie);
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100846 else if (getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar05159a02005-02-26 23:04:13 +0000847 script_line_start();
848 }
849# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000850 }
851
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000852 if (cstack.cs_looplevel > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000853 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000854 /* Inside a while/for loop we need to store the lines and use them
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100855 * again. Pass a different "fgetline" function to do_one_cmd()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000856 * below, so that it stores lines in or reads them from
857 * "lines_ga". Makes it possible to define a function inside a
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000858 * while/for loop. */
859 cmd_getline = get_loop_line;
860 cmd_cookie = (void *)&cmd_loop_cookie;
861 cmd_loop_cookie.lines_gap = &lines_ga;
862 cmd_loop_cookie.current_line = current_line;
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100863 cmd_loop_cookie.getline = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000864 cmd_loop_cookie.cookie = cookie;
865 cmd_loop_cookie.repeating = (current_line < lines_ga.ga_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000866 }
867 else
868 {
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100869 cmd_getline = fgetline;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000870 cmd_cookie = cookie;
871 }
872#endif
873
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100874 /* 2. If no line given, get an allocated line with fgetline(). */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000875 if (next_cmdline == NULL)
876 {
877 /*
878 * Need to set msg_didout for the first line after an ":if",
879 * otherwise the ":if" will be overwritten.
880 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100881 if (count == 1 && getline_equal(fgetline, cookie, getexline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000882 msg_didout = TRUE;
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100883 if (fgetline == NULL || (next_cmdline = fgetline(':', cookie,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000884#ifdef FEAT_EVAL
885 cstack.cs_idx < 0 ? 0 : (cstack.cs_idx + 1) * 2
886#else
887 0
888#endif
889 )) == NULL)
890 {
891 /* Don't call wait_return for aborted command line. The NULL
892 * returned for the end of a sourced file or executed function
893 * doesn't do this. */
894 if (KeyTyped && !(flags & DOCMD_REPEAT))
895 need_wait_return = FALSE;
896 retval = FAIL;
897 break;
898 }
899 used_getline = TRUE;
900
901 /*
902 * Keep the first typed line. Clear it when more lines are typed.
903 */
904 if (flags & DOCMD_KEEPLINE)
905 {
906 vim_free(repeat_cmdline);
907 if (count == 0)
908 repeat_cmdline = vim_strsave(next_cmdline);
909 else
910 repeat_cmdline = NULL;
911 }
912 }
913
914 /* 3. Make a copy of the command so we can mess with it. */
915 else if (cmdline_copy == NULL)
916 {
917 next_cmdline = vim_strsave(next_cmdline);
918 if (next_cmdline == NULL)
919 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100920 emsg(_(e_outofmem));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000921 retval = FAIL;
922 break;
923 }
924 }
925 cmdline_copy = next_cmdline;
926
927#ifdef FEAT_EVAL
928 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000929 * Save the current line when inside a ":while" or ":for", and when
930 * the command looks like a ":while" or ":for", because we may need it
931 * later. When there is a '|' and another command, it is stored
932 * separately, because we need to be able to jump back to it from an
933 * :endwhile/:endfor.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000934 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000935 if (current_line == lines_ga.ga_len
936 && (cstack.cs_looplevel || has_loop_cmd(next_cmdline)))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000937 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000938 if (store_loop_line(&lines_ga, next_cmdline) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000939 {
940 retval = FAIL;
941 break;
942 }
943 }
944 did_endif = FALSE;
945#endif
946
947 if (count++ == 0)
948 {
949 /*
950 * All output from the commands is put below each other, without
951 * waiting for a return. Don't do this when executing commands
952 * from a script or when being called recursive (e.g. for ":e
953 * +command file").
954 */
955 if (!(flags & DOCMD_NOWAIT) && !recursive)
956 {
957 msg_didout_before_start = msg_didout;
958 msg_didany = FALSE; /* no output yet */
959 msg_start();
960 msg_scroll = TRUE; /* put messages below each other */
Bram Moolenaar84a05ac2013-05-06 04:24:17 +0200961 ++no_wait_return; /* don't wait for return until finished */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000962 ++RedrawingDisabled;
963 did_inc = TRUE;
964 }
965 }
966
967 if (p_verbose >= 15 && sourcing_name != NULL)
968 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000969 ++no_wait_return;
Bram Moolenaar8b044b32005-05-31 22:05:58 +0000970 verbose_enter_scroll();
971
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100972 smsg(_("line %ld: %s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +0000973 (long)sourcing_lnum, cmdline_copy);
Bram Moolenaar8b044b32005-05-31 22:05:58 +0000974 if (msg_silent == 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +0100975 msg_puts("\n"); /* don't overwrite this */
Bram Moolenaar8b044b32005-05-31 22:05:58 +0000976
977 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000978 --no_wait_return;
979 }
980
981 /*
982 * 2. Execute one '|' separated command.
983 * do_one_cmd() will return NULL if there is no trailing '|'.
984 * "cmdline_copy" can change, e.g. for '%' and '#' expansion.
985 */
986 ++recursive;
987 next_cmdline = do_one_cmd(&cmdline_copy, flags & DOCMD_VERBOSE,
988#ifdef FEAT_EVAL
989 &cstack,
990#endif
991 cmd_getline, cmd_cookie);
992 --recursive;
993
994#ifdef FEAT_EVAL
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000995 if (cmd_cookie == (void *)&cmd_loop_cookie)
996 /* Use "current_line" from "cmd_loop_cookie", it may have been
Bram Moolenaar071d4272004-06-13 20:20:40 +0000997 * incremented when defining a function. */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000998 current_line = cmd_loop_cookie.current_line;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000999#endif
1000
1001 if (next_cmdline == NULL)
1002 {
Bram Moolenaard23a8232018-02-10 18:45:26 +01001003 VIM_CLEAR(cmdline_copy);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001004#ifdef FEAT_CMDHIST
1005 /*
1006 * If the command was typed, remember it for the ':' register.
1007 * Do this AFTER executing the command to make :@: work.
1008 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001009 if (getline_equal(fgetline, cookie, getexline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001010 && new_last_cmdline != NULL)
1011 {
1012 vim_free(last_cmdline);
1013 last_cmdline = new_last_cmdline;
1014 new_last_cmdline = NULL;
1015 }
1016#endif
1017 }
1018 else
1019 {
1020 /* need to copy the command after the '|' to cmdline_copy, for the
1021 * next do_one_cmd() */
Bram Moolenaara7241f52008-06-24 20:39:31 +00001022 STRMOVE(cmdline_copy, next_cmdline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001023 next_cmdline = cmdline_copy;
1024 }
1025
1026
1027#ifdef FEAT_EVAL
1028 /* reset did_emsg for a function that is not aborted by an error */
1029 if (did_emsg && !force_abort
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001030 && getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001031 && !func_has_abort(real_cookie))
1032 did_emsg = FALSE;
1033
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001034 if (cstack.cs_looplevel > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001035 {
1036 ++current_line;
1037
1038 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001039 * An ":endwhile", ":endfor" and ":continue" is handled here.
1040 * If we were executing commands, jump back to the ":while" or
1041 * ":for".
1042 * If we were not executing commands, decrement cs_looplevel.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001043 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001044 if (cstack.cs_lflags & (CSL_HAD_CONT | CSL_HAD_ENDLOOP))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001045 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001046 cstack.cs_lflags &= ~(CSL_HAD_CONT | CSL_HAD_ENDLOOP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001047
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001048 /* Jump back to the matching ":while" or ":for". Be careful
1049 * not to use a cs_line[] from an entry that isn't a ":while"
1050 * or ":for": It would make "current_line" invalid and can
1051 * cause a crash. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001052 if (!did_emsg && !got_int && !did_throw
1053 && cstack.cs_idx >= 0
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001054 && (cstack.cs_flags[cstack.cs_idx]
1055 & (CSF_WHILE | CSF_FOR))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001056 && cstack.cs_line[cstack.cs_idx] >= 0
1057 && (cstack.cs_flags[cstack.cs_idx] & CSF_ACTIVE))
1058 {
1059 current_line = cstack.cs_line[cstack.cs_idx];
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001060 /* remember we jumped there */
1061 cstack.cs_lflags |= CSL_HAD_LOOP;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001062 line_breakcheck(); /* check if CTRL-C typed */
1063
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001064 /* Check for the next breakpoint at or after the ":while"
1065 * or ":for". */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001066 if (breakpoint != NULL)
1067 {
1068 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001069 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001070 fname,
1071 ((wcmd_T *)lines_ga.ga_data)[current_line].lnum-1);
1072 *dbg_tick = debug_tick;
1073 }
1074 }
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001075 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001076 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001077 /* can only get here with ":endwhile" or ":endfor" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001078 if (cstack.cs_idx >= 0)
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001079 rewind_conditionals(&cstack, cstack.cs_idx - 1,
1080 CSF_WHILE | CSF_FOR, &cstack.cs_looplevel);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001081 }
1082 }
1083
1084 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001085 * For a ":while" or ":for" we need to remember the line number.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001086 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001087 else if (cstack.cs_lflags & CSL_HAD_LOOP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001088 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001089 cstack.cs_lflags &= ~CSL_HAD_LOOP;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001090 cstack.cs_line[cstack.cs_idx] = current_line - 1;
1091 }
1092 }
1093
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01001094 /* Check for the next breakpoint after a watchexpression */
1095 if (breakpoint != NULL && has_watchexpr())
1096 {
1097 *breakpoint = dbg_find_breakpoint(FALSE, fname, sourcing_lnum);
1098 *dbg_tick = debug_tick;
1099 }
1100
Bram Moolenaar071d4272004-06-13 20:20:40 +00001101 /*
1102 * When not inside any ":while" loop, clear remembered lines.
1103 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001104 if (cstack.cs_looplevel == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001105 {
1106 if (lines_ga.ga_len > 0)
1107 {
1108 sourcing_lnum =
1109 ((wcmd_T *)lines_ga.ga_data)[lines_ga.ga_len - 1].lnum;
1110 free_cmdlines(&lines_ga);
1111 }
1112 current_line = 0;
1113 }
1114
1115 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001116 * A ":finally" makes did_emsg, got_int, and did_throw pending for
1117 * being restored at the ":endtry". Reset them here and set the
1118 * ACTIVE and FINALLY flags, so that the finally clause gets executed.
1119 * This includes the case where a missing ":endif", ":endwhile" or
1120 * ":endfor" was detected by the ":finally" itself.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001121 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001122 if (cstack.cs_lflags & CSL_HAD_FINA)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001123 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001124 cstack.cs_lflags &= ~CSL_HAD_FINA;
1125 report_make_pending(cstack.cs_pending[cstack.cs_idx]
1126 & (CSTP_ERROR | CSTP_INTERRUPT | CSTP_THROW),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001127 did_throw ? (void *)current_exception : NULL);
1128 did_emsg = got_int = did_throw = FALSE;
1129 cstack.cs_flags[cstack.cs_idx] |= CSF_ACTIVE | CSF_FINALLY;
1130 }
1131
1132 /* Update global "trylevel" for recursive calls to do_cmdline() from
1133 * within this loop. */
1134 trylevel = initial_trylevel + cstack.cs_trylevel;
1135
1136 /*
Bram Moolenaarc1762cc2007-05-10 16:56:30 +00001137 * If the outermost try conditional (across function calls and sourced
Bram Moolenaar071d4272004-06-13 20:20:40 +00001138 * files) is aborted because of an error, an interrupt, or an uncaught
1139 * exception, cancel everything. If it is left normally, reset
1140 * force_abort to get the non-EH compatible abortion behavior for
1141 * the rest of the script.
1142 */
1143 if (trylevel == 0 && !did_emsg && !got_int && !did_throw)
1144 force_abort = FALSE;
1145
1146 /* Convert an interrupt to an exception if appropriate. */
1147 (void)do_intthrow(&cstack);
1148#endif /* FEAT_EVAL */
1149
1150 }
1151 /*
1152 * Continue executing command lines when:
1153 * - no CTRL-C typed, no aborting error, no exception thrown or try
1154 * conditionals need to be checked for executing finally clauses or
1155 * catching an interrupt exception
1156 * - didn't get an error message or lines are not typed
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001157 * - there is a command after '|', inside a :if, :while, :for or :try, or
Bram Moolenaar071d4272004-06-13 20:20:40 +00001158 * looping for ":source" command or function call.
1159 */
1160 while (!((got_int
1161#ifdef FEAT_EVAL
1162 || (did_emsg && force_abort) || did_throw
1163#endif
1164 )
1165#ifdef FEAT_EVAL
1166 && cstack.cs_trylevel == 0
1167#endif
1168 )
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001169 && !(did_emsg
1170#ifdef FEAT_EVAL
1171 /* Keep going when inside try/catch, so that the error can be
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02001172 * deal with, except when it is a syntax error, it may cause
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001173 * the :endtry to be missed. */
1174 && (cstack.cs_trylevel == 0 || did_emsg_syntax)
1175#endif
1176 && used_getline
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001177 && (getline_equal(fgetline, cookie, getexmodeline)
1178 || getline_equal(fgetline, cookie, getexline)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001179 && (next_cmdline != NULL
1180#ifdef FEAT_EVAL
1181 || cstack.cs_idx >= 0
1182#endif
1183 || (flags & DOCMD_REPEAT)));
1184
1185 vim_free(cmdline_copy);
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001186 did_emsg_syntax = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001187#ifdef FEAT_EVAL
1188 free_cmdlines(&lines_ga);
1189 ga_clear(&lines_ga);
1190
1191 if (cstack.cs_idx >= 0)
1192 {
1193 /*
1194 * If a sourced file or executed function ran to its end, report the
1195 * unclosed conditional.
1196 */
1197 if (!got_int && !did_throw
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001198 && ((getline_equal(fgetline, cookie, getsourceline)
1199 && !source_finished(fgetline, cookie))
1200 || (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001201 && !func_has_ended(real_cookie))))
1202 {
1203 if (cstack.cs_flags[cstack.cs_idx] & CSF_TRY)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001204 emsg(_(e_endtry));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001205 else if (cstack.cs_flags[cstack.cs_idx] & CSF_WHILE)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001206 emsg(_(e_endwhile));
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001207 else if (cstack.cs_flags[cstack.cs_idx] & CSF_FOR)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001208 emsg(_(e_endfor));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001209 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001210 emsg(_(e_endif));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001211 }
1212
1213 /*
1214 * Reset "trylevel" in case of a ":finish" or ":return" or a missing
1215 * ":endtry" in a sourced file or executed function. If the try
1216 * conditional is in its finally clause, ignore anything pending.
1217 * If it is in a catch clause, finish the caught exception.
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001218 * Also cleanup any "cs_forinfo" structures.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001219 */
1220 do
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001221 {
1222 int idx = cleanup_conditionals(&cstack, 0, TRUE);
1223
Bram Moolenaar89e5d682005-01-17 22:06:23 +00001224 if (idx >= 0)
1225 --idx; /* remove try block not in its finally clause */
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001226 rewind_conditionals(&cstack, idx, CSF_WHILE | CSF_FOR,
1227 &cstack.cs_looplevel);
1228 }
1229 while (cstack.cs_idx >= 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001230 trylevel = initial_trylevel;
1231 }
1232
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001233 /* If a missing ":endtry", ":endwhile", ":endfor", or ":endif" or a memory
1234 * lack was reported above and the error message is to be converted to an
Bram Moolenaar071d4272004-06-13 20:20:40 +00001235 * exception, do this now after rewinding the cstack. */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001236 do_errthrow(&cstack, getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001237 ? (char_u *)"endfunction" : (char_u *)NULL);
1238
1239 if (trylevel == 0)
1240 {
1241 /*
1242 * When an exception is being thrown out of the outermost try
1243 * conditional, discard the uncaught exception, disable the conversion
1244 * of interrupts or errors to exceptions, and ensure that no more
1245 * commands are executed.
1246 */
1247 if (did_throw)
1248 {
1249 void *p = NULL;
1250 char_u *saved_sourcing_name;
1251 int saved_sourcing_lnum;
1252 struct msglist *messages = NULL, *next;
1253
1254 /*
1255 * If the uncaught exception is a user exception, report it as an
1256 * error. If it is an error exception, display the saved error
1257 * message now. For an interrupt exception, do nothing; the
1258 * interrupt message is given elsewhere.
1259 */
1260 switch (current_exception->type)
1261 {
1262 case ET_USER:
Bram Moolenaar9c13b352005-05-19 20:53:52 +00001263 vim_snprintf((char *)IObuff, IOSIZE,
1264 _("E605: Exception not caught: %s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001265 current_exception->value);
1266 p = vim_strsave(IObuff);
1267 break;
1268 case ET_ERROR:
1269 messages = current_exception->messages;
1270 current_exception->messages = NULL;
1271 break;
1272 case ET_INTERRUPT:
1273 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001274 }
1275
1276 saved_sourcing_name = sourcing_name;
1277 saved_sourcing_lnum = sourcing_lnum;
1278 sourcing_name = current_exception->throw_name;
1279 sourcing_lnum = current_exception->throw_lnum;
1280 current_exception->throw_name = NULL;
1281
1282 discard_current_exception(); /* uses IObuff if 'verbose' */
1283 suppress_errthrow = TRUE;
1284 force_abort = TRUE;
1285
1286 if (messages != NULL)
1287 {
1288 do
1289 {
1290 next = messages->next;
1291 emsg(messages->msg);
1292 vim_free(messages->msg);
1293 vim_free(messages);
1294 messages = next;
1295 }
1296 while (messages != NULL);
1297 }
1298 else if (p != NULL)
1299 {
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01001300 emsg(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001301 vim_free(p);
1302 }
1303 vim_free(sourcing_name);
1304 sourcing_name = saved_sourcing_name;
1305 sourcing_lnum = saved_sourcing_lnum;
1306 }
1307
1308 /*
1309 * On an interrupt or an aborting error not converted to an exception,
1310 * disable the conversion of errors to exceptions. (Interrupts are not
1311 * converted any more, here.) This enables also the interrupt message
1312 * when force_abort is set and did_emsg unset in case of an interrupt
1313 * from a finally clause after an error.
1314 */
1315 else if (got_int || (did_emsg && force_abort))
1316 suppress_errthrow = TRUE;
1317 }
1318
1319 /*
1320 * The current cstack will be freed when do_cmdline() returns. An uncaught
1321 * exception will have to be rethrown in the previous cstack. If a function
1322 * has just returned or a script file was just finished and the previous
1323 * cstack belongs to the same function or, respectively, script file, it
1324 * will have to be checked for finally clauses to be executed due to the
1325 * ":return" or ":finish". This is done in do_one_cmd().
1326 */
1327 if (did_throw)
1328 need_rethrow = TRUE;
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001329 if ((getline_equal(fgetline, cookie, getsourceline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001330 && ex_nesting_level > source_level(real_cookie))
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001331 || (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001332 && ex_nesting_level > func_level(real_cookie) + 1))
1333 {
1334 if (!did_throw)
1335 check_cstack = TRUE;
1336 }
1337 else
1338 {
1339 /* When leaving a function, reduce nesting level. */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001340 if (getline_equal(fgetline, cookie, get_func_line))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001341 --ex_nesting_level;
1342 /*
1343 * Go to debug mode when returning from a function in which we are
1344 * single-stepping.
1345 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001346 if ((getline_equal(fgetline, cookie, getsourceline)
1347 || getline_equal(fgetline, cookie, get_func_line))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001348 && ex_nesting_level + 1 <= debug_break_level)
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001349 do_debug(getline_equal(fgetline, cookie, getsourceline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001350 ? (char_u *)_("End of sourced file")
1351 : (char_u *)_("End of function"));
1352 }
1353
1354 /*
1355 * Restore the exception environment (done after returning from the
1356 * debugger).
1357 */
1358 if (flags & DOCMD_EXCRESET)
Bram Moolenaared203462004-06-16 11:19:22 +00001359 restore_dbg_stuff(&debug_saved);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001360
1361 msg_list = saved_msg_list;
1362#endif /* FEAT_EVAL */
1363
1364 /*
1365 * If there was too much output to fit on the command line, ask the user to
1366 * hit return before redrawing the screen. With the ":global" command we do
1367 * this only once after the command is finished.
1368 */
1369 if (did_inc)
1370 {
1371 --RedrawingDisabled;
1372 --no_wait_return;
1373 msg_scroll = FALSE;
1374
1375 /*
1376 * When just finished an ":if"-":else" which was typed, no need to
1377 * wait for hit-return. Also for an error situation.
1378 */
1379 if (retval == FAIL
1380#ifdef FEAT_EVAL
1381 || (did_endif && KeyTyped && !did_emsg)
1382#endif
1383 )
1384 {
1385 need_wait_return = FALSE;
1386 msg_didany = FALSE; /* don't wait when restarting edit */
1387 }
1388 else if (need_wait_return)
1389 {
1390 /*
1391 * The msg_start() above clears msg_didout. The wait_return we do
1392 * here should not overwrite the command that may be shown before
1393 * doing that.
1394 */
1395 msg_didout |= msg_didout_before_start;
1396 wait_return(FALSE);
1397 }
1398 }
1399
Bram Moolenaar2a942252012-11-28 23:03:07 +01001400#ifdef FEAT_EVAL
1401 did_endif = FALSE; /* in case do_cmdline used recursively */
1402#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001403 /*
1404 * Reset if_level, in case a sourced script file contains more ":if" than
1405 * ":endif" (could be ":if x | foo | endif").
1406 */
1407 if_level = 0;
Bram Moolenaar2e18a122012-11-28 19:10:54 +01001408#endif
Bram Moolenaard4ad0d42012-11-28 17:34:48 +01001409
Bram Moolenaar071d4272004-06-13 20:20:40 +00001410 --call_depth;
1411 return retval;
1412}
1413
1414#ifdef FEAT_EVAL
1415/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001416 * Obtain a line when inside a ":while" or ":for" loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001417 */
1418 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001419get_loop_line(int c, void *cookie, int indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001420{
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001421 struct loop_cookie *cp = (struct loop_cookie *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001422 wcmd_T *wp;
1423 char_u *line;
1424
1425 if (cp->current_line + 1 >= cp->lines_gap->ga_len)
1426 {
1427 if (cp->repeating)
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001428 return NULL; /* trying to read past ":endwhile"/":endfor" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001429
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001430 /* First time inside the ":while"/":for": get line normally. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001431 if (cp->getline == NULL)
1432 line = getcmdline(c, 0L, indent);
1433 else
1434 line = cp->getline(c, cp->cookie, indent);
Bram Moolenaard68071d2006-05-02 22:08:30 +00001435 if (line != NULL && store_loop_line(cp->lines_gap, line) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001436 ++cp->current_line;
1437
1438 return line;
1439 }
1440
1441 KeyTyped = FALSE;
1442 ++cp->current_line;
1443 wp = (wcmd_T *)(cp->lines_gap->ga_data) + cp->current_line;
1444 sourcing_lnum = wp->lnum;
1445 return vim_strsave(wp->line);
1446}
1447
1448/*
1449 * Store a line in "gap" so that a ":while" loop can execute it again.
1450 */
1451 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001452store_loop_line(garray_T *gap, char_u *line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001453{
1454 if (ga_grow(gap, 1) == FAIL)
1455 return FAIL;
1456 ((wcmd_T *)(gap->ga_data))[gap->ga_len].line = vim_strsave(line);
1457 ((wcmd_T *)(gap->ga_data))[gap->ga_len].lnum = sourcing_lnum;
1458 ++gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001459 return OK;
1460}
1461
1462/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001463 * Free the lines stored for a ":while" or ":for" loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001464 */
1465 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001466free_cmdlines(garray_T *gap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001467{
1468 while (gap->ga_len > 0)
1469 {
1470 vim_free(((wcmd_T *)(gap->ga_data))[gap->ga_len - 1].line);
1471 --gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001472 }
1473}
1474#endif
1475
1476/*
Bram Moolenaar89d40322006-08-29 15:30:07 +00001477 * If "fgetline" is get_loop_line(), return TRUE if the getline it uses equals
1478 * "func". * Otherwise return TRUE when "fgetline" equals "func".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001479 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001480 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001481getline_equal(
1482 char_u *(*fgetline)(int, void *, int),
1483 void *cookie UNUSED, /* argument for fgetline() */
1484 char_u *(*func)(int, void *, int))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001485{
1486#ifdef FEAT_EVAL
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01001487 char_u *(*gp)(int, void *, int);
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001488 struct loop_cookie *cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001489
Bram Moolenaar89d40322006-08-29 15:30:07 +00001490 /* When "fgetline" is "get_loop_line()" use the "cookie" to find the
Bram Moolenaarc1762cc2007-05-10 16:56:30 +00001491 * function that's originally used to obtain the lines. This may be
1492 * nested several levels. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00001493 gp = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001494 cp = (struct loop_cookie *)cookie;
1495 while (gp == get_loop_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001496 {
1497 gp = cp->getline;
1498 cp = cp->cookie;
1499 }
1500 return gp == func;
1501#else
Bram Moolenaar89d40322006-08-29 15:30:07 +00001502 return fgetline == func;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001503#endif
1504}
1505
Bram Moolenaar071d4272004-06-13 20:20:40 +00001506/*
Bram Moolenaar89d40322006-08-29 15:30:07 +00001507 * If "fgetline" is get_loop_line(), return the cookie used by the original
Bram Moolenaar071d4272004-06-13 20:20:40 +00001508 * getline function. Otherwise return "cookie".
1509 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001510 void *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001511getline_cookie(
1512 char_u *(*fgetline)(int, void *, int) UNUSED,
1513 void *cookie) /* argument for fgetline() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001514{
Bram Moolenaar13505972019-01-24 15:04:48 +01001515#ifdef FEAT_EVAL
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01001516 char_u *(*gp)(int, void *, int);
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001517 struct loop_cookie *cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001518
Bram Moolenaar89d40322006-08-29 15:30:07 +00001519 /* When "fgetline" is "get_loop_line()" use the "cookie" to find the
Bram Moolenaarc1762cc2007-05-10 16:56:30 +00001520 * cookie that's originally used to obtain the lines. This may be nested
Bram Moolenaar071d4272004-06-13 20:20:40 +00001521 * several levels. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00001522 gp = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001523 cp = (struct loop_cookie *)cookie;
1524 while (gp == get_loop_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001525 {
1526 gp = cp->getline;
1527 cp = cp->cookie;
1528 }
1529 return cp;
Bram Moolenaar13505972019-01-24 15:04:48 +01001530#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001531 return cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001532#endif
Bram Moolenaar13505972019-01-24 15:04:48 +01001533}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001534
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001535
1536/*
1537 * Helper function to apply an offset for buffer commands, i.e. ":bdelete",
1538 * ":bwipeout", etc.
1539 * Returns the buffer number.
1540 */
1541 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001542compute_buffer_local_count(int addr_type, int lnum, int offset)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001543{
1544 buf_T *buf;
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001545 buf_T *nextbuf;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001546 int count = offset;
1547
1548 buf = firstbuf;
1549 while (buf->b_next != NULL && buf->b_fnum < lnum)
1550 buf = buf->b_next;
1551 while (count != 0)
1552 {
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001553 count += (offset < 0) ? 1 : -1;
1554 nextbuf = (offset < 0) ? buf->b_prev : buf->b_next;
1555 if (nextbuf == NULL)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001556 break;
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001557 buf = nextbuf;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001558 if (addr_type == ADDR_LOADED_BUFFERS)
1559 /* skip over unloaded buffers */
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001560 while (buf->b_ml.ml_mfp == NULL)
1561 {
1562 nextbuf = (offset < 0) ? buf->b_prev : buf->b_next;
1563 if (nextbuf == NULL)
1564 break;
1565 buf = nextbuf;
1566 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001567 }
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001568 /* we might have gone too far, last buffer is not loadedd */
1569 if (addr_type == ADDR_LOADED_BUFFERS)
1570 while (buf->b_ml.ml_mfp == NULL)
1571 {
1572 nextbuf = (offset >= 0) ? buf->b_prev : buf->b_next;
1573 if (nextbuf == NULL)
1574 break;
1575 buf = nextbuf;
1576 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001577 return buf->b_fnum;
1578}
1579
Bram Moolenaarf240e182014-11-27 18:33:02 +01001580 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001581current_win_nr(win_T *win)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001582{
1583 win_T *wp;
1584 int nr = 0;
1585
Bram Moolenaar29323592016-07-24 22:04:11 +02001586 FOR_ALL_WINDOWS(wp)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001587 {
1588 ++nr;
1589 if (wp == win)
1590 break;
1591 }
1592 return nr;
1593}
1594
1595 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001596current_tab_nr(tabpage_T *tab)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001597{
1598 tabpage_T *tp;
1599 int nr = 0;
1600
Bram Moolenaar29323592016-07-24 22:04:11 +02001601 FOR_ALL_TABPAGES(tp)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001602 {
1603 ++nr;
1604 if (tp == tab)
1605 break;
1606 }
1607 return nr;
1608}
1609
1610# define CURRENT_WIN_NR current_win_nr(curwin)
1611# define LAST_WIN_NR current_win_nr(NULL)
1612# define CURRENT_TAB_NR current_tab_nr(curtab)
1613# define LAST_TAB_NR current_tab_nr(NULL)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001614
Bram Moolenaar071d4272004-06-13 20:20:40 +00001615/*
1616 * Execute one Ex command.
1617 *
1618 * If 'sourcing' is TRUE, the command will be included in the error message.
1619 *
1620 * 1. skip comment lines and leading space
1621 * 2. handle command modifiers
Bram Moolenaar1c40a662014-11-27 16:38:11 +01001622 * 3. find the command
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001623 * 4. parse range
Bram Moolenaar1c40a662014-11-27 16:38:11 +01001624 * 5. Parse the command.
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001625 * 6. parse arguments
1626 * 7. switch on command name
Bram Moolenaar071d4272004-06-13 20:20:40 +00001627 *
Bram Moolenaar89d40322006-08-29 15:30:07 +00001628 * Note: "fgetline" can be NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001629 *
1630 * This function may be called recursively!
1631 */
1632#if (_MSC_VER == 1200)
1633/*
Bram Moolenaared203462004-06-16 11:19:22 +00001634 * Avoid optimisation bug in VC++ version 6.0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001635 */
Bram Moolenaar281bdce2005-01-25 21:53:18 +00001636 #pragma optimize( "g", off )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001637#endif
1638 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001639do_one_cmd(
1640 char_u **cmdlinep,
1641 int sourcing,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001642#ifdef FEAT_EVAL
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001643 struct condstack *cstack,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001644#endif
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001645 char_u *(*fgetline)(int, void *, int),
1646 void *cookie) /* argument for fgetline() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001647{
1648 char_u *p;
1649 linenr_T lnum;
1650 long n;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001651 char *errormsg = NULL; /* error message */
Bram Moolenaar7b668e82016-08-23 23:51:21 +02001652 char_u *after_modifier = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001653 exarg_T ea; /* Ex command arguments */
Bram Moolenaar8e258a42009-07-09 13:55:43 +00001654 int save_msg_scroll = msg_scroll;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001655 cmdmod_T save_cmdmod;
Bram Moolenaar9a2c0912019-03-30 14:26:18 +01001656 int save_reg_executing = reg_executing;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001657 int ni; /* set when Not Implemented */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001658 char_u *cmd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001659
1660 vim_memset(&ea, 0, sizeof(ea));
1661 ea.line1 = 1;
1662 ea.line2 = 1;
1663#ifdef FEAT_EVAL
1664 ++ex_nesting_level;
1665#endif
1666
Bram Moolenaar21691f82012-12-05 19:13:18 +01001667 /* When the last file has not been edited :q has to be typed twice. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001668 if (quitmore
1669#ifdef FEAT_EVAL
1670 /* avoid that a function call in 'statusline' does this */
Bram Moolenaar89d40322006-08-29 15:30:07 +00001671 && !getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001672#endif
Bram Moolenaar21691f82012-12-05 19:13:18 +01001673 /* avoid that an autocommand, e.g. QuitPre, does this */
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001674 && !getline_equal(fgetline, cookie, getnextac))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001675 --quitmore;
1676
1677 /*
1678 * Reset browse, confirm, etc.. They are restored when returning, for
1679 * recursive calls.
1680 */
1681 save_cmdmod = cmdmod;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001682
Bram Moolenaarcbb37ad2006-08-16 15:04:21 +00001683 /* "#!anything" is handled like a comment. */
1684 if ((*cmdlinep)[0] == '#' && (*cmdlinep)[1] == '!')
1685 goto doend;
1686
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02001687/*
1688 * 1. Skip comment lines and leading white space and colons.
1689 * 2. Handle command modifiers.
1690 */
1691 // The "ea" structure holds the arguments that can be used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001692 ea.cmd = *cmdlinep;
Bram Moolenaareffed932018-08-14 13:38:17 +02001693 ea.cmdlinep = cmdlinep;
1694 ea.getline = fgetline;
1695 ea.cookie = cookie;
1696#ifdef FEAT_EVAL
1697 ea.cstack = cstack;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001698#endif
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02001699 if (parse_command_modifiers(&ea, &errormsg, FALSE) == FAIL)
Bram Moolenaareffed932018-08-14 13:38:17 +02001700 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001701
Bram Moolenaar7b668e82016-08-23 23:51:21 +02001702 after_modifier = ea.cmd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001703
1704#ifdef FEAT_EVAL
1705 ea.skip = did_emsg || got_int || did_throw || (cstack->cs_idx >= 0
1706 && !(cstack->cs_flags[cstack->cs_idx] & CSF_ACTIVE));
1707#else
1708 ea.skip = (if_level > 0);
1709#endif
1710
Bram Moolenaar071d4272004-06-13 20:20:40 +00001711/*
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001712 * 3. Skip over the range to find the command. Let "p" point to after it.
1713 *
1714 * We need the command to know what kind of range it uses.
1715 */
1716 cmd = ea.cmd;
1717 ea.cmd = skip_range(ea.cmd, NULL);
1718 if (*ea.cmd == '*' && vim_strchr(p_cpo, CPO_STAR) == NULL)
1719 ea.cmd = skipwhite(ea.cmd + 1);
1720 p = find_command(&ea, NULL);
1721
Bram Moolenaar7feb35e2018-08-21 17:49:54 +02001722#ifdef FEAT_EVAL
1723# ifdef FEAT_PROFILE
1724 // Count this line for profiling if skip is TRUE.
1725 if (do_profiling == PROF_YES
1726 && (!ea.skip || cstack->cs_idx == 0 || (cstack->cs_idx > 0
1727 && (cstack->cs_flags[cstack->cs_idx - 1] & CSF_ACTIVE))))
1728 {
1729 int skip = did_emsg || got_int || did_throw;
1730
1731 if (ea.cmdidx == CMD_catch)
1732 skip = !skip && !(cstack->cs_idx >= 0
1733 && (cstack->cs_flags[cstack->cs_idx] & CSF_THROWN)
1734 && !(cstack->cs_flags[cstack->cs_idx] & CSF_CAUGHT));
1735 else if (ea.cmdidx == CMD_else || ea.cmdidx == CMD_elseif)
1736 skip = skip || !(cstack->cs_idx >= 0
1737 && !(cstack->cs_flags[cstack->cs_idx]
1738 & (CSF_ACTIVE | CSF_TRUE)));
1739 else if (ea.cmdidx == CMD_finally)
1740 skip = FALSE;
1741 else if (ea.cmdidx != CMD_endif
1742 && ea.cmdidx != CMD_endfor
1743 && ea.cmdidx != CMD_endtry
1744 && ea.cmdidx != CMD_endwhile)
1745 skip = ea.skip;
1746
1747 if (!skip)
1748 {
1749 if (getline_equal(fgetline, cookie, get_func_line))
1750 func_line_exec(getline_cookie(fgetline, cookie));
1751 else if (getline_equal(fgetline, cookie, getsourceline))
1752 script_line_exec();
1753 }
1754 }
1755# endif
1756
1757 /* May go to debug mode. If this happens and the ">quit" debug command is
1758 * used, throw an interrupt exception and skip the next command. */
1759 dbg_check_breakpoint(&ea);
1760 if (!ea.skip && got_int)
1761 {
1762 ea.skip = TRUE;
1763 (void)do_intthrow(cstack);
1764 }
1765#endif
1766
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001767/*
1768 * 4. parse a range specifier of the form: addr [,addr] [;addr] ..
Bram Moolenaar071d4272004-06-13 20:20:40 +00001769 *
1770 * where 'addr' is:
1771 *
1772 * % (entire file)
1773 * $ [+-NUM]
1774 * 'x [+-NUM] (where x denotes a currently defined mark)
1775 * . [+-NUM]
1776 * [+-NUM]..
1777 * NUM
1778 *
1779 * The ea.cmd pointer is updated to point to the first character following the
1780 * range spec. If an initial address is found, but no second, the upper bound
1781 * is equal to the lower.
1782 */
1783
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01001784 /* ea.addr_type for user commands is set by find_ucmd */
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01001785 if (!IS_USER_CMDIDX(ea.cmdidx))
1786 {
1787 if (ea.cmdidx != CMD_SIZE)
1788 ea.addr_type = cmdnames[(int)ea.cmdidx].cmd_addr_type;
1789 else
1790 ea.addr_type = ADDR_LINES;
1791
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01001792 /* :wincmd range depends on the argument. */
Bram Moolenaar164f3262015-01-14 21:22:01 +01001793 if (ea.cmdidx == CMD_wincmd && p != NULL)
1794 get_wincmd_addr_type(skipwhite(p), &ea);
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01001795 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001796
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01001797 ea.cmd = cmd;
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02001798 if (parse_cmd_address(&ea, &errormsg, FALSE) == FAIL)
Bram Moolenaaree8415b2018-08-10 23:13:12 +02001799 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001800
Bram Moolenaar071d4272004-06-13 20:20:40 +00001801/*
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001802 * 5. Parse the command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001803 */
1804
1805 /*
1806 * Skip ':' and any white space
1807 */
1808 ea.cmd = skipwhite(ea.cmd);
1809 while (*ea.cmd == ':')
1810 ea.cmd = skipwhite(ea.cmd + 1);
1811
1812 /*
1813 * If we got a line, but no command, then go to the line.
1814 * If we find a '|' or '\n' we set ea.nextcmd.
1815 */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001816 if (*ea.cmd == NUL || *ea.cmd == '"'
1817 || (ea.nextcmd = check_nextcmd(ea.cmd)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001818 {
1819 /*
1820 * strange vi behaviour:
1821 * ":3" jumps to line 3
1822 * ":3|..." prints line 3
1823 * ":|" prints current line
1824 */
1825 if (ea.skip) /* skip this if inside :if */
1826 goto doend;
Bram Moolenaardf177f62005-02-22 08:39:57 +00001827 if (*ea.cmd == '|' || (exmode_active && ea.line1 != ea.line2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001828 {
1829 ea.cmdidx = CMD_print;
1830 ea.argt = RANGE+COUNT+TRLBAR;
1831 if ((errormsg = invalid_range(&ea)) == NULL)
1832 {
1833 correct_range(&ea);
1834 ex_print(&ea);
1835 }
1836 }
1837 else if (ea.addr_count != 0)
1838 {
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00001839 if (ea.line2 > curbuf->b_ml.ml_line_count)
1840 {
1841 /* With '-' in 'cpoptions' a line number past the file is an
1842 * error, otherwise put it at the end of the file. */
1843 if (vim_strchr(p_cpo, CPO_MINUS) != NULL)
1844 ea.line2 = -1;
1845 else
1846 ea.line2 = curbuf->b_ml.ml_line_count;
1847 }
1848
1849 if (ea.line2 < 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001850 errormsg = _(e_invrange);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001851 else
1852 {
1853 if (ea.line2 == 0)
1854 curwin->w_cursor.lnum = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001855 else
1856 curwin->w_cursor.lnum = ea.line2;
1857 beginline(BL_SOL | BL_FIX);
1858 }
1859 }
1860 goto doend;
1861 }
1862
Bram Moolenaard5005162014-08-22 23:05:54 +02001863 /* If this looks like an undefined user command and there are CmdUndefined
1864 * autocommands defined, trigger the matching autocommands. */
1865 if (p != NULL && ea.cmdidx == CMD_SIZE && !ea.skip
1866 && ASCII_ISUPPER(*ea.cmd)
1867 && has_cmdundefined())
1868 {
Bram Moolenaard5005162014-08-22 23:05:54 +02001869 int ret;
1870
Bram Moolenaar5a31b462014-08-23 14:16:20 +02001871 p = ea.cmd;
Bram Moolenaard5005162014-08-22 23:05:54 +02001872 while (ASCII_ISALNUM(*p))
1873 ++p;
Bram Moolenaar120f4a82014-09-09 12:22:06 +02001874 p = vim_strnsave(ea.cmd, (int)(p - ea.cmd));
Bram Moolenaard5005162014-08-22 23:05:54 +02001875 ret = apply_autocmds(EVENT_CMDUNDEFINED, p, p, TRUE, NULL);
1876 vim_free(p);
Bram Moolenaar829aef12015-07-28 14:25:48 +02001877 /* If the autocommands did something and didn't cause an error, try
1878 * finding the command again. */
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001879 p = (ret
1880#ifdef FEAT_EVAL
1881 && !aborting()
Bram Moolenaard5005162014-08-22 23:05:54 +02001882#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001883 ) ? find_command(&ea, NULL) : ea.cmd;
1884 }
Bram Moolenaard5005162014-08-22 23:05:54 +02001885
Bram Moolenaar071d4272004-06-13 20:20:40 +00001886 if (p == NULL)
1887 {
1888 if (!ea.skip)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001889 errormsg = _("E464: Ambiguous use of user-defined command");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001890 goto doend;
1891 }
Bram Moolenaarac9fb182019-04-27 13:04:13 +02001892 // Check for wrong commands.
Bram Moolenaar6f9a4762017-06-22 20:39:17 +02001893 if (*p == '!' && ea.cmd[1] == 0151 && ea.cmd[0] == 78
1894 && !IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001895 {
1896 errormsg = uc_fun_cmd();
1897 goto doend;
1898 }
Bram Moolenaarac9fb182019-04-27 13:04:13 +02001899
Bram Moolenaar071d4272004-06-13 20:20:40 +00001900 if (ea.cmdidx == CMD_SIZE)
1901 {
1902 if (!ea.skip)
1903 {
1904 STRCPY(IObuff, _("E492: Not an editor command"));
1905 if (!sourcing)
Bram Moolenaar7b668e82016-08-23 23:51:21 +02001906 {
1907 /* If the modifier was parsed OK the error must be in the
1908 * following command */
1909 if (after_modifier != NULL)
1910 append_command(after_modifier);
1911 else
1912 append_command(*cmdlinep);
1913 }
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001914 errormsg = (char *)IObuff;
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001915 did_emsg_syntax = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001916 }
1917 goto doend;
1918 }
1919
Bram Moolenaar958636c2014-10-21 20:01:58 +02001920 ni = (!IS_USER_CMDIDX(ea.cmdidx)
1921 && (cmdnames[ea.cmdidx].cmd_func == ex_ni
Bram Moolenaar7bb75552007-07-16 18:39:49 +00001922#ifdef HAVE_EX_SCRIPT_NI
1923 || cmdnames[ea.cmdidx].cmd_func == ex_script_ni
1924#endif
1925 ));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001926
1927#ifndef FEAT_EVAL
1928 /*
1929 * When the expression evaluation is disabled, recognize the ":if" and
1930 * ":endif" commands and ignore everything in between it.
1931 */
1932 if (ea.cmdidx == CMD_if)
1933 ++if_level;
1934 if (if_level)
1935 {
1936 if (ea.cmdidx == CMD_endif)
1937 --if_level;
1938 goto doend;
1939 }
1940
1941#endif
1942
Bram Moolenaar196b3b02008-06-20 16:51:41 +00001943 /* forced commands */
1944 if (*p == '!' && ea.cmdidx != CMD_substitute
1945 && ea.cmdidx != CMD_smagic && ea.cmdidx != CMD_snomagic)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001946 {
1947 ++p;
1948 ea.forceit = TRUE;
1949 }
1950 else
1951 ea.forceit = FALSE;
1952
1953/*
Bram Moolenaar1c40a662014-11-27 16:38:11 +01001954 * 6. Parse arguments.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001955 */
Bram Moolenaar958636c2014-10-21 20:01:58 +02001956 if (!IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001957 ea.argt = (long)cmdnames[(int)ea.cmdidx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001958
1959 if (!ea.skip)
1960 {
1961#ifdef HAVE_SANDBOX
1962 if (sandbox != 0 && !(ea.argt & SBOXOK))
1963 {
Bram Moolenaar8c62a082019-02-08 14:34:10 +01001964 // Command not allowed in sandbox.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001965 errormsg = _(e_sandbox);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001966 goto doend;
1967 }
1968#endif
Bram Moolenaar8c62a082019-02-08 14:34:10 +01001969 if (restricted != 0 && (ea.argt & RESTRICT))
1970 {
1971 errormsg = _("E981: Command not allowed in rvim");
1972 goto doend;
1973 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001974 if (!curbuf->b_p_ma && (ea.argt & MODIFY))
1975 {
1976 /* Command not allowed in non-'modifiable' buffer */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001977 errormsg = _(e_modifiable);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001978 goto doend;
1979 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00001980
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00001981 if (text_locked() && !(ea.argt & CMDWIN)
Bram Moolenaar958636c2014-10-21 20:01:58 +02001982 && !IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001983 {
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00001984 /* Command not allowed when editing the command line. */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001985 errormsg = _(get_text_locked_msg());
Bram Moolenaar071d4272004-06-13 20:20:40 +00001986 goto doend;
1987 }
Bram Moolenaar379fb762018-08-30 15:58:28 +02001988
Bram Moolenaar5555acc2006-04-07 21:33:12 +00001989 /* Disallow editing another buffer when "curbuf_lock" is set.
Bram Moolenaar379fb762018-08-30 15:58:28 +02001990 * Do allow ":checktime" (it is postponed).
1991 * Do allow ":edit" (check for an argument later).
1992 * Do allow ":file" with no arguments (check for an argument later). */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001993 if (!(ea.argt & CMDWIN)
Bram Moolenaar5555acc2006-04-07 21:33:12 +00001994 && ea.cmdidx != CMD_checktime
Bram Moolenaar379fb762018-08-30 15:58:28 +02001995 && ea.cmdidx != CMD_edit
1996 && ea.cmdidx != CMD_file
Bram Moolenaar958636c2014-10-21 20:01:58 +02001997 && !IS_USER_CMDIDX(ea.cmdidx)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001998 && curbuf_locked())
1999 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002000
2001 if (!ni && !(ea.argt & RANGE) && ea.addr_count > 0)
2002 {
2003 /* no range allowed */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002004 errormsg = _(e_norange);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002005 goto doend;
2006 }
2007 }
2008
2009 if (!ni && !(ea.argt & BANG) && ea.forceit) /* no <!> allowed */
2010 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002011 errormsg = _(e_nobang);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002012 goto doend;
2013 }
2014
2015 /*
2016 * Don't complain about the range if it is not used
2017 * (could happen if line_count is accidentally set to 0).
2018 */
2019 if (!ea.skip && !ni)
2020 {
2021 /*
2022 * If the range is backwards, ask for confirmation and, if given, swap
2023 * ea.line1 & ea.line2 so it's forwards again.
2024 * When global command is busy, don't ask, will fail below.
2025 */
2026 if (!global_busy && ea.line1 > ea.line2)
2027 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002028 if (msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002029 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002030 if (sourcing || exmode_active)
2031 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002032 errormsg = _("E493: Backwards range given");
Bram Moolenaara5792f52005-11-23 21:25:05 +00002033 goto doend;
2034 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002035 if (ask_yesno((char_u *)
2036 _("Backwards range given, OK to swap"), FALSE) != 'y')
Bram Moolenaara5792f52005-11-23 21:25:05 +00002037 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002038 }
2039 lnum = ea.line1;
2040 ea.line1 = ea.line2;
2041 ea.line2 = lnum;
2042 }
2043 if ((errormsg = invalid_range(&ea)) != NULL)
2044 goto doend;
2045 }
2046
2047 if ((ea.argt & NOTADR) && ea.addr_count == 0) /* default is 1, not cursor */
2048 ea.line2 = 1;
2049
2050 correct_range(&ea);
2051
2052#ifdef FEAT_FOLDING
Bram Moolenaara3306952016-01-02 21:41:06 +01002053 if (((ea.argt & WHOLEFOLD) || ea.addr_count >= 2) && !global_busy
2054 && ea.addr_type == ADDR_LINES)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002055 {
2056 /* Put the first line at the start of a closed fold, put the last line
2057 * at the end of a closed fold. */
2058 (void)hasFolding(ea.line1, &ea.line1, NULL);
2059 (void)hasFolding(ea.line2, NULL, &ea.line2);
2060 }
2061#endif
2062
2063#ifdef FEAT_QUICKFIX
2064 /*
Bram Moolenaar86b68352004-12-27 21:59:20 +00002065 * For the ":make" and ":grep" commands we insert the 'makeprg'/'grepprg'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002066 * option here, so things like % get expanded.
2067 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002068 p = replace_makeprg(&ea, p, cmdlinep);
2069 if (p == NULL)
2070 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002071#endif
2072
2073 /*
2074 * Skip to start of argument.
2075 * Don't do this for the ":!" command, because ":!! -l" needs the space.
2076 */
2077 if (ea.cmdidx == CMD_bang)
2078 ea.arg = p;
2079 else
2080 ea.arg = skipwhite(p);
2081
Bram Moolenaar379fb762018-08-30 15:58:28 +02002082 // ":file" cannot be run with an argument when "curbuf_lock" is set
2083 if (ea.cmdidx == CMD_file && *ea.arg != NUL && curbuf_locked())
2084 goto doend;
2085
Bram Moolenaar071d4272004-06-13 20:20:40 +00002086 /*
2087 * Check for "++opt=val" argument.
2088 * Must be first, allow ":w ++enc=utf8 !cmd"
2089 */
2090 if (ea.argt & ARGOPT)
2091 while (ea.arg[0] == '+' && ea.arg[1] == '+')
2092 if (getargopt(&ea) == FAIL && !ni)
2093 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002094 errormsg = _(e_invarg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002095 goto doend;
2096 }
2097
2098 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
2099 {
2100 if (*ea.arg == '>') /* append */
2101 {
2102 if (*++ea.arg != '>') /* typed wrong */
2103 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002104 errormsg = _("E494: Use w or w>>");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002105 goto doend;
2106 }
2107 ea.arg = skipwhite(ea.arg + 1);
2108 ea.append = TRUE;
2109 }
2110 else if (*ea.arg == '!' && ea.cmdidx == CMD_write) /* :w !filter */
2111 {
2112 ++ea.arg;
2113 ea.usefilter = TRUE;
2114 }
2115 }
2116
2117 if (ea.cmdidx == CMD_read)
2118 {
2119 if (ea.forceit)
2120 {
2121 ea.usefilter = TRUE; /* :r! filter if ea.forceit */
2122 ea.forceit = FALSE;
2123 }
2124 else if (*ea.arg == '!') /* :r !filter */
2125 {
2126 ++ea.arg;
2127 ea.usefilter = TRUE;
2128 }
2129 }
2130
2131 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
2132 {
2133 ea.amount = 1;
2134 while (*ea.arg == *ea.cmd) /* count number of '>' or '<' */
2135 {
2136 ++ea.arg;
2137 ++ea.amount;
2138 }
2139 ea.arg = skipwhite(ea.arg);
2140 }
2141
2142 /*
2143 * Check for "+command" argument, before checking for next command.
2144 * Don't do this for ":read !cmd" and ":write !cmd".
2145 */
2146 if ((ea.argt & EDITCMD) && !ea.usefilter)
2147 ea.do_ecmd_cmd = getargcmd(&ea.arg);
2148
2149 /*
2150 * Check for '|' to separate commands and '"' to start comments.
2151 * Don't do this for ":read !cmd" and ":write !cmd".
2152 */
2153 if ((ea.argt & TRLBAR) && !ea.usefilter)
2154 separate_nextcmd(&ea);
2155
2156 /*
2157 * Check for <newline> to end a shell command.
Bram Moolenaardf177f62005-02-22 08:39:57 +00002158 * Also do this for ":read !cmd", ":write !cmd" and ":global".
2159 * Any others?
Bram Moolenaar071d4272004-06-13 20:20:40 +00002160 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00002161 else if (ea.cmdidx == CMD_bang
Bram Moolenaar67883b42017-07-27 22:57:00 +02002162 || ea.cmdidx == CMD_terminal
Bram Moolenaardf177f62005-02-22 08:39:57 +00002163 || ea.cmdidx == CMD_global
2164 || ea.cmdidx == CMD_vglobal
2165 || ea.usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002166 {
2167 for (p = ea.arg; *p; ++p)
2168 {
2169 /* Remove one backslash before a newline, so that it's possible to
2170 * pass a newline to the shell and also a newline that is preceded
2171 * with a backslash. This makes it impossible to end a shell
2172 * command in a backslash, but that doesn't appear useful.
2173 * Halving the number of backslashes is incompatible with previous
2174 * versions. */
2175 if (*p == '\\' && p[1] == '\n')
Bram Moolenaara7241f52008-06-24 20:39:31 +00002176 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002177 else if (*p == '\n')
2178 {
2179 ea.nextcmd = p + 1;
2180 *p = NUL;
2181 break;
2182 }
2183 }
2184 }
2185
2186 if ((ea.argt & DFLALL) && ea.addr_count == 0)
2187 {
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002188 buf_T *buf;
2189
Bram Moolenaar071d4272004-06-13 20:20:40 +00002190 ea.line1 = 1;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002191 switch (ea.addr_type)
2192 {
2193 case ADDR_LINES:
2194 ea.line2 = curbuf->b_ml.ml_line_count;
2195 break;
2196 case ADDR_LOADED_BUFFERS:
2197 buf = firstbuf;
2198 while (buf->b_next != NULL && buf->b_ml.ml_mfp == NULL)
2199 buf = buf->b_next;
2200 ea.line1 = buf->b_fnum;
2201 buf = lastbuf;
2202 while (buf->b_prev != NULL && buf->b_ml.ml_mfp == NULL)
2203 buf = buf->b_prev;
2204 ea.line2 = buf->b_fnum;
2205 break;
2206 case ADDR_BUFFERS:
2207 ea.line1 = firstbuf->b_fnum;
2208 ea.line2 = lastbuf->b_fnum;
2209 break;
2210 case ADDR_WINDOWS:
2211 ea.line2 = LAST_WIN_NR;
2212 break;
2213 case ADDR_TABS:
2214 ea.line2 = LAST_TAB_NR;
2215 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01002216 case ADDR_TABS_RELATIVE:
2217 ea.line2 = 1;
2218 break;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002219 case ADDR_ARGUMENTS:
2220 if (ARGCOUNT == 0)
2221 ea.line1 = ea.line2 = 0;
2222 else
2223 ea.line2 = ARGCOUNT;
2224 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02002225#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02002226 case ADDR_QUICKFIX:
2227 ea.line2 = qf_get_size(&ea);
2228 if (ea.line2 == 0)
2229 ea.line2 = 1;
2230 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02002231#endif
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002232 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002233 }
2234
2235 /* accept numbered register only when no count allowed (:put) */
2236 if ( (ea.argt & REGSTR)
2237 && *ea.arg != NUL
Bram Moolenaar958636c2014-10-21 20:01:58 +02002238 /* Do not allow register = for user commands */
2239 && (!IS_USER_CMDIDX(ea.cmdidx) || *ea.arg != '=')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002240 && !((ea.argt & COUNT) && VIM_ISDIGIT(*ea.arg)))
2241 {
Bram Moolenaar85de2062011-05-05 14:26:41 +02002242#ifndef FEAT_CLIPBOARD
2243 /* check these explicitly for a more specific error message */
2244 if (*ea.arg == '*' || *ea.arg == '+')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002245 {
Bram Moolenaar99b12722019-01-14 20:16:40 +01002246 errormsg = _(e_invalidreg);
Bram Moolenaar85de2062011-05-05 14:26:41 +02002247 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002248 }
2249#endif
Bram Moolenaar958636c2014-10-21 20:01:58 +02002250 if (valid_yank_reg(*ea.arg, (ea.cmdidx != CMD_put
2251 && !IS_USER_CMDIDX(ea.cmdidx))))
Bram Moolenaar85de2062011-05-05 14:26:41 +02002252 {
2253 ea.regname = *ea.arg++;
2254#ifdef FEAT_EVAL
2255 /* for '=' register: accept the rest of the line as an expression */
2256 if (ea.arg[-1] == '=' && ea.arg[0] != NUL)
2257 {
2258 set_expr_line(vim_strsave(ea.arg));
2259 ea.arg += STRLEN(ea.arg);
2260 }
2261#endif
2262 ea.arg = skipwhite(ea.arg);
2263 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002264 }
2265
2266 /*
2267 * Check for a count. When accepting a BUFNAME, don't use "123foo" as a
2268 * count, it's a buffer name.
2269 */
2270 if ((ea.argt & COUNT) && VIM_ISDIGIT(*ea.arg)
2271 && (!(ea.argt & BUFNAME) || *(p = skipdigits(ea.arg)) == NUL
Bram Moolenaar1c465442017-03-12 20:10:05 +01002272 || VIM_ISWHITE(*p)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002273 {
2274 n = getdigits(&ea.arg);
2275 ea.arg = skipwhite(ea.arg);
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002276 if (n <= 0 && !ni && (ea.argt & ZEROR) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002277 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002278 errormsg = _(e_zerocount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002279 goto doend;
2280 }
2281 if (ea.argt & NOTADR) /* e.g. :buffer 2, :sleep 3 */
2282 {
2283 ea.line2 = n;
2284 if (ea.addr_count == 0)
2285 ea.addr_count = 1;
2286 }
2287 else
2288 {
2289 ea.line1 = ea.line2;
2290 ea.line2 += n - 1;
2291 ++ea.addr_count;
2292 /*
2293 * Be vi compatible: no error message for out of range.
2294 */
Bram Moolenaar2f72c702017-01-29 14:48:10 +01002295 if (ea.addr_type == ADDR_LINES
2296 && ea.line2 > curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002297 ea.line2 = curbuf->b_ml.ml_line_count;
2298 }
2299 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00002300
2301 /*
2302 * Check for flags: 'l', 'p' and '#'.
2303 */
2304 if (ea.argt & EXFLAGS)
2305 get_flags(&ea);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002306 /* no arguments allowed */
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002307 if (!ni && !(ea.argt & EXTRA) && *ea.arg != NUL
Bram Moolenaara2031822006-03-07 22:29:51 +00002308 && *ea.arg != '"' && (*ea.arg != '|' || (ea.argt & TRLBAR) == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002309 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002310 errormsg = _(e_trailing);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002311 goto doend;
2312 }
2313
2314 if (!ni && (ea.argt & NEEDARG) && *ea.arg == NUL)
2315 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002316 errormsg = _(e_argreq);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002317 goto doend;
2318 }
2319
2320#ifdef FEAT_EVAL
2321 /*
2322 * Skip the command when it's not going to be executed.
2323 * The commands like :if, :endif, etc. always need to be executed.
2324 * Also make an exception for commands that handle a trailing command
2325 * themselves.
2326 */
2327 if (ea.skip)
2328 {
2329 switch (ea.cmdidx)
2330 {
2331 /* commands that need evaluation */
2332 case CMD_while:
2333 case CMD_endwhile:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00002334 case CMD_for:
2335 case CMD_endfor:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002336 case CMD_if:
2337 case CMD_elseif:
2338 case CMD_else:
2339 case CMD_endif:
2340 case CMD_try:
2341 case CMD_catch:
2342 case CMD_finally:
2343 case CMD_endtry:
2344 case CMD_function:
2345 break;
2346
2347 /* Commands that handle '|' themselves. Check: A command should
2348 * either have the TRLBAR flag, appear in this list or appear in
2349 * the list at ":help :bar". */
2350 case CMD_aboveleft:
2351 case CMD_and:
2352 case CMD_belowright:
2353 case CMD_botright:
2354 case CMD_browse:
2355 case CMD_call:
2356 case CMD_confirm:
2357 case CMD_delfunction:
2358 case CMD_djump:
2359 case CMD_dlist:
2360 case CMD_dsearch:
2361 case CMD_dsplit:
2362 case CMD_echo:
2363 case CMD_echoerr:
2364 case CMD_echomsg:
2365 case CMD_echon:
2366 case CMD_execute:
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002367 case CMD_filter:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002368 case CMD_help:
2369 case CMD_hide:
2370 case CMD_ijump:
2371 case CMD_ilist:
2372 case CMD_isearch:
2373 case CMD_isplit:
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002374 case CMD_keepalt:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002375 case CMD_keepjumps:
2376 case CMD_keepmarks:
Bram Moolenaara939e432013-11-09 05:30:26 +01002377 case CMD_keeppatterns:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002378 case CMD_leftabove:
2379 case CMD_let:
2380 case CMD_lockmarks:
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002381 case CMD_lua:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002382 case CMD_match:
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002383 case CMD_mzscheme:
Bram Moolenaar5803ae62014-03-23 16:04:02 +01002384 case CMD_noautocmd:
2385 case CMD_noswapfile:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002386 case CMD_perl:
2387 case CMD_psearch:
2388 case CMD_python:
Bram Moolenaar368373e2010-07-19 20:46:22 +02002389 case CMD_py3:
Bram Moolenaarb6590522010-07-21 16:00:43 +02002390 case CMD_python3:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002391 case CMD_return:
2392 case CMD_rightbelow:
2393 case CMD_ruby:
2394 case CMD_silent:
2395 case CMD_smagic:
2396 case CMD_snomagic:
2397 case CMD_substitute:
2398 case CMD_syntax:
Bram Moolenaara226a6d2006-02-26 23:59:20 +00002399 case CMD_tab:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002400 case CMD_tcl:
2401 case CMD_throw:
2402 case CMD_tilde:
2403 case CMD_topleft:
2404 case CMD_unlet:
2405 case CMD_verbose:
2406 case CMD_vertical:
Bram Moolenaar12bde492011-06-13 01:19:56 +02002407 case CMD_wincmd:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002408 break;
2409
2410 default: goto doend;
2411 }
2412 }
2413#endif
2414
2415 if (ea.argt & XFILE)
2416 {
2417 if (expand_filename(&ea, cmdlinep, &errormsg) == FAIL)
2418 goto doend;
2419 }
2420
Bram Moolenaar071d4272004-06-13 20:20:40 +00002421 /*
2422 * Accept buffer name. Cannot be used at the same time with a buffer
2423 * number. Don't do this for a user command.
2424 */
2425 if ((ea.argt & BUFNAME) && *ea.arg != NUL && ea.addr_count == 0
Bram Moolenaar958636c2014-10-21 20:01:58 +02002426 && !IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002427 {
2428 /*
2429 * :bdelete, :bwipeout and :bunload take several arguments, separated
2430 * by spaces: find next space (skipping over escaped characters).
2431 * The others take one argument: ignore trailing spaces.
2432 */
2433 if (ea.cmdidx == CMD_bdelete || ea.cmdidx == CMD_bwipeout
2434 || ea.cmdidx == CMD_bunload)
2435 p = skiptowhite_esc(ea.arg);
2436 else
2437 {
2438 p = ea.arg + STRLEN(ea.arg);
Bram Moolenaar1c465442017-03-12 20:10:05 +01002439 while (p > ea.arg && VIM_ISWHITE(p[-1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002440 --p;
2441 }
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002442 ea.line2 = buflist_findpat(ea.arg, p, (ea.argt & BUFUNL) != 0,
2443 FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002444 if (ea.line2 < 0) /* failed */
2445 goto doend;
2446 ea.addr_count = 1;
2447 ea.arg = skipwhite(p);
2448 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002449
Bram Moolenaar2be57332018-02-13 18:05:18 +01002450 /* The :try command saves the emsg_silent flag, reset it here when
2451 * ":silent! try" was used, it should only apply to :try itself. */
Bram Moolenaareffed932018-08-14 13:38:17 +02002452 if (ea.cmdidx == CMD_try && ea.did_esilent > 0)
Bram Moolenaar2be57332018-02-13 18:05:18 +01002453 {
Bram Moolenaareffed932018-08-14 13:38:17 +02002454 emsg_silent -= ea.did_esilent;
Bram Moolenaar2be57332018-02-13 18:05:18 +01002455 if (emsg_silent < 0)
2456 emsg_silent = 0;
Bram Moolenaareffed932018-08-14 13:38:17 +02002457 ea.did_esilent = 0;
Bram Moolenaar2be57332018-02-13 18:05:18 +01002458 }
2459
Bram Moolenaar071d4272004-06-13 20:20:40 +00002460/*
Bram Moolenaar2be57332018-02-13 18:05:18 +01002461 * 7. Execute the command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002462 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002463
Bram Moolenaar958636c2014-10-21 20:01:58 +02002464 if (IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002465 {
2466 /*
2467 * Execute a user-defined command.
2468 */
2469 do_ucmd(&ea);
2470 }
2471 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002472 {
2473 /*
Bram Moolenaarac9fb182019-04-27 13:04:13 +02002474 * Call the function to execute the builtin command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002475 */
2476 ea.errmsg = NULL;
2477 (cmdnames[ea.cmdidx].cmd_func)(&ea);
2478 if (ea.errmsg != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002479 errormsg = _(ea.errmsg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002480 }
2481
2482#ifdef FEAT_EVAL
2483 /*
2484 * If the command just executed called do_cmdline(), any throw or ":return"
2485 * or ":finish" encountered there must also check the cstack of the still
2486 * active do_cmdline() that called this do_one_cmd(). Rethrow an uncaught
2487 * exception, or reanimate a returned function or finished script file and
2488 * return or finish it again.
2489 */
2490 if (need_rethrow)
2491 do_throw(cstack);
2492 else if (check_cstack)
2493 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002494 if (source_finished(fgetline, cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002495 do_finish(&ea, TRUE);
Bram Moolenaar89d40322006-08-29 15:30:07 +00002496 else if (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002497 && current_func_returned())
2498 do_return(&ea, TRUE, FALSE, NULL);
2499 }
2500 need_rethrow = check_cstack = FALSE;
2501#endif
2502
2503doend:
2504 if (curwin->w_cursor.lnum == 0) /* can happen with zero line number */
Bram Moolenaar6de5e122017-04-20 21:55:44 +02002505 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002506 curwin->w_cursor.lnum = 1;
Bram Moolenaar6de5e122017-04-20 21:55:44 +02002507 curwin->w_cursor.col = 0;
2508 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002509
2510 if (errormsg != NULL && *errormsg != NUL && !did_emsg)
2511 {
2512 if (sourcing)
2513 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002514 if (errormsg != (char *)IObuff)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002515 {
2516 STRCPY(IObuff, errormsg);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002517 errormsg = (char *)IObuff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002518 }
Bram Moolenaara6f4d612011-09-21 19:10:46 +02002519 append_command(*cmdlinep);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002520 }
2521 emsg(errormsg);
2522 }
2523#ifdef FEAT_EVAL
2524 do_errthrow(cstack,
Bram Moolenaar958636c2014-10-21 20:01:58 +02002525 (ea.cmdidx != CMD_SIZE && !IS_USER_CMDIDX(ea.cmdidx))
2526 ? cmdnames[(int)ea.cmdidx].cmd_name : (char_u *)NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002527#endif
2528
Bram Moolenaareffed932018-08-14 13:38:17 +02002529 if (ea.verbose_save >= 0)
2530 p_verbose = ea.verbose_save;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002531
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002532 free_cmdmod();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002533 cmdmod = save_cmdmod;
Bram Moolenaar9a2c0912019-03-30 14:26:18 +01002534 reg_executing = save_reg_executing;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002535
Bram Moolenaareffed932018-08-14 13:38:17 +02002536 if (ea.save_msg_silent != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002537 {
2538 /* messages could be enabled for a serious error, need to check if the
2539 * counters don't become negative */
Bram Moolenaareffed932018-08-14 13:38:17 +02002540 if (!did_emsg || msg_silent > ea.save_msg_silent)
2541 msg_silent = ea.save_msg_silent;
2542 emsg_silent -= ea.did_esilent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002543 if (emsg_silent < 0)
2544 emsg_silent = 0;
2545 /* Restore msg_scroll, it's set by file I/O commands, even when no
2546 * message is actually displayed. */
2547 msg_scroll = save_msg_scroll;
Bram Moolenaar77ab2802009-04-22 12:44:48 +00002548
2549 /* "silent reg" or "silent echo x" inside "redir" leaves msg_col
2550 * somewhere in the line. Put it back in the first column. */
2551 if (redirecting())
2552 msg_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002553 }
2554
Bram Moolenaar7171abe2004-10-11 10:06:20 +00002555#ifdef HAVE_SANDBOX
Bram Moolenaareffed932018-08-14 13:38:17 +02002556 if (ea.did_sandbox)
Bram Moolenaar7171abe2004-10-11 10:06:20 +00002557 --sandbox;
2558#endif
2559
Bram Moolenaar071d4272004-06-13 20:20:40 +00002560 if (ea.nextcmd && *ea.nextcmd == NUL) /* not really a next command */
2561 ea.nextcmd = NULL;
2562
2563#ifdef FEAT_EVAL
2564 --ex_nesting_level;
2565#endif
2566
2567 return ea.nextcmd;
2568}
2569#if (_MSC_VER == 1200)
Bram Moolenaar281bdce2005-01-25 21:53:18 +00002570 #pragma optimize( "", on )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002571#endif
2572
2573/*
Bram Moolenaareffed932018-08-14 13:38:17 +02002574 * Parse and skip over command modifiers:
2575 * - update eap->cmd
2576 * - store flags in "cmdmod".
2577 * - Set ex_pressedreturn for an empty command line.
2578 * - set msg_silent for ":silent"
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002579 * - set 'eventignore' to "all" for ":noautocmd"
Bram Moolenaareffed932018-08-14 13:38:17 +02002580 * - set p_verbose for ":verbose"
2581 * - Increment "sandbox" for ":sandbox"
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002582 * When "skip_only" is TRUE the global variables are not changed, except for
2583 * "cmdmod".
Bram Moolenaareffed932018-08-14 13:38:17 +02002584 * Return FAIL when the command is not to be executed.
2585 * May set "errormsg" to an error message.
2586 */
2587 int
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002588parse_command_modifiers(exarg_T *eap, char **errormsg, int skip_only)
Bram Moolenaareffed932018-08-14 13:38:17 +02002589{
2590 char_u *p;
2591
2592 vim_memset(&cmdmod, 0, sizeof(cmdmod));
2593 eap->verbose_save = -1;
2594 eap->save_msg_silent = -1;
2595
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002596 // Repeat until no more command modifiers are found.
Bram Moolenaareffed932018-08-14 13:38:17 +02002597 for (;;)
2598 {
Bram Moolenaareffed932018-08-14 13:38:17 +02002599 while (*eap->cmd == ' ' || *eap->cmd == '\t' || *eap->cmd == ':')
2600 ++eap->cmd;
2601
2602 /* in ex mode, an empty line works like :+ */
2603 if (*eap->cmd == NUL && exmode_active
2604 && (getline_equal(eap->getline, eap->cookie, getexmodeline)
2605 || getline_equal(eap->getline, eap->cookie, getexline))
2606 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
2607 {
2608 eap->cmd = (char_u *)"+";
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002609 if (!skip_only)
2610 ex_pressedreturn = TRUE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002611 }
2612
2613 /* ignore comment and empty lines */
2614 if (*eap->cmd == '"')
2615 return FAIL;
2616 if (*eap->cmd == NUL)
2617 {
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002618 if (!skip_only)
2619 ex_pressedreturn = TRUE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002620 return FAIL;
2621 }
2622
Bram Moolenaareffed932018-08-14 13:38:17 +02002623 p = skip_range(eap->cmd, NULL);
2624 switch (*p)
2625 {
2626 /* When adding an entry, also modify cmd_exists(). */
2627 case 'a': if (!checkforcmd(&eap->cmd, "aboveleft", 3))
2628 break;
2629 cmdmod.split |= WSP_ABOVE;
2630 continue;
2631
2632 case 'b': if (checkforcmd(&eap->cmd, "belowright", 3))
2633 {
2634 cmdmod.split |= WSP_BELOW;
2635 continue;
2636 }
2637 if (checkforcmd(&eap->cmd, "browse", 3))
2638 {
2639#ifdef FEAT_BROWSE_CMD
2640 cmdmod.browse = TRUE;
2641#endif
2642 continue;
2643 }
2644 if (!checkforcmd(&eap->cmd, "botright", 2))
2645 break;
2646 cmdmod.split |= WSP_BOT;
2647 continue;
2648
2649 case 'c': if (!checkforcmd(&eap->cmd, "confirm", 4))
2650 break;
2651#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
2652 cmdmod.confirm = TRUE;
2653#endif
2654 continue;
2655
2656 case 'k': if (checkforcmd(&eap->cmd, "keepmarks", 3))
2657 {
2658 cmdmod.keepmarks = TRUE;
2659 continue;
2660 }
2661 if (checkforcmd(&eap->cmd, "keepalt", 5))
2662 {
2663 cmdmod.keepalt = TRUE;
2664 continue;
2665 }
2666 if (checkforcmd(&eap->cmd, "keeppatterns", 5))
2667 {
2668 cmdmod.keeppatterns = TRUE;
2669 continue;
2670 }
2671 if (!checkforcmd(&eap->cmd, "keepjumps", 5))
2672 break;
2673 cmdmod.keepjumps = TRUE;
2674 continue;
2675
2676 case 'f': /* only accept ":filter {pat} cmd" */
2677 {
2678 char_u *reg_pat;
2679
2680 if (!checkforcmd(&p, "filter", 4)
2681 || *p == NUL || ends_excmd(*p))
2682 break;
2683 if (*p == '!')
2684 {
2685 cmdmod.filter_force = TRUE;
2686 p = skipwhite(p + 1);
2687 if (*p == NUL || ends_excmd(*p))
2688 break;
2689 }
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002690 if (skip_only)
2691 p = skip_vimgrep_pat(p, NULL, NULL);
2692 else
2693 // NOTE: This puts a NUL after the pattern.
2694 p = skip_vimgrep_pat(p, &reg_pat, NULL);
Bram Moolenaareffed932018-08-14 13:38:17 +02002695 if (p == NULL || *p == NUL)
2696 break;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002697 if (!skip_only)
2698 {
2699 cmdmod.filter_regmatch.regprog =
Bram Moolenaareffed932018-08-14 13:38:17 +02002700 vim_regcomp(reg_pat, RE_MAGIC);
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002701 if (cmdmod.filter_regmatch.regprog == NULL)
2702 break;
2703 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002704 eap->cmd = p;
2705 continue;
2706 }
2707
2708 /* ":hide" and ":hide | cmd" are not modifiers */
2709 case 'h': if (p != eap->cmd || !checkforcmd(&p, "hide", 3)
2710 || *p == NUL || ends_excmd(*p))
2711 break;
2712 eap->cmd = p;
2713 cmdmod.hide = TRUE;
2714 continue;
2715
2716 case 'l': if (checkforcmd(&eap->cmd, "lockmarks", 3))
2717 {
2718 cmdmod.lockmarks = TRUE;
2719 continue;
2720 }
2721
2722 if (!checkforcmd(&eap->cmd, "leftabove", 5))
2723 break;
2724 cmdmod.split |= WSP_ABOVE;
2725 continue;
2726
2727 case 'n': if (checkforcmd(&eap->cmd, "noautocmd", 3))
2728 {
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002729 if (cmdmod.save_ei == NULL && !skip_only)
Bram Moolenaareffed932018-08-14 13:38:17 +02002730 {
2731 /* Set 'eventignore' to "all". Restore the
2732 * existing option value later. */
2733 cmdmod.save_ei = vim_strsave(p_ei);
2734 set_string_option_direct((char_u *)"ei", -1,
2735 (char_u *)"all", OPT_FREE, SID_NONE);
2736 }
2737 continue;
2738 }
2739 if (!checkforcmd(&eap->cmd, "noswapfile", 3))
2740 break;
2741 cmdmod.noswapfile = TRUE;
2742 continue;
2743
2744 case 'r': if (!checkforcmd(&eap->cmd, "rightbelow", 6))
2745 break;
2746 cmdmod.split |= WSP_BELOW;
2747 continue;
2748
2749 case 's': if (checkforcmd(&eap->cmd, "sandbox", 3))
2750 {
2751#ifdef HAVE_SANDBOX
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002752 if (!skip_only)
2753 {
2754 if (!eap->did_sandbox)
2755 ++sandbox;
2756 eap->did_sandbox = TRUE;
2757 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002758#endif
2759 continue;
2760 }
2761 if (!checkforcmd(&eap->cmd, "silent", 3))
2762 break;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002763 if (!skip_only)
2764 {
2765 if (eap->save_msg_silent == -1)
2766 eap->save_msg_silent = msg_silent;
2767 ++msg_silent;
2768 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002769 if (*eap->cmd == '!' && !VIM_ISWHITE(eap->cmd[-1]))
2770 {
2771 /* ":silent!", but not "silent !cmd" */
2772 eap->cmd = skipwhite(eap->cmd + 1);
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002773 if (!skip_only)
2774 {
2775 ++emsg_silent;
2776 ++eap->did_esilent;
2777 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002778 }
2779 continue;
2780
2781 case 't': if (checkforcmd(&p, "tab", 3))
2782 {
Bram Moolenaar548e5982018-12-26 21:45:00 +01002783 if (!skip_only)
Bram Moolenaareffed932018-08-14 13:38:17 +02002784 {
Bram Moolenaar548e5982018-12-26 21:45:00 +01002785 long tabnr = get_address(eap, &eap->cmd,
2786 ADDR_TABS, eap->skip,
2787 skip_only, FALSE, 1);
2788 if (tabnr == MAXLNUM)
2789 cmdmod.tab = tabpage_index(curtab) + 1;
2790 else
Bram Moolenaareffed932018-08-14 13:38:17 +02002791 {
Bram Moolenaar548e5982018-12-26 21:45:00 +01002792 if (tabnr < 0 || tabnr > LAST_TAB_NR)
2793 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002794 *errormsg = _(e_invrange);
Bram Moolenaar548e5982018-12-26 21:45:00 +01002795 return FAIL;
2796 }
2797 cmdmod.tab = tabnr + 1;
Bram Moolenaareffed932018-08-14 13:38:17 +02002798 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002799 }
2800 eap->cmd = p;
2801 continue;
2802 }
2803 if (!checkforcmd(&eap->cmd, "topleft", 2))
2804 break;
2805 cmdmod.split |= WSP_TOP;
2806 continue;
2807
2808 case 'u': if (!checkforcmd(&eap->cmd, "unsilent", 3))
2809 break;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002810 if (!skip_only)
2811 {
2812 if (eap->save_msg_silent == -1)
2813 eap->save_msg_silent = msg_silent;
2814 msg_silent = 0;
2815 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002816 continue;
2817
2818 case 'v': if (checkforcmd(&eap->cmd, "vertical", 4))
2819 {
2820 cmdmod.split |= WSP_VERT;
2821 continue;
2822 }
2823 if (!checkforcmd(&p, "verbose", 4))
2824 break;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002825 if (!skip_only)
2826 {
2827 if (eap->verbose_save < 0)
2828 eap->verbose_save = p_verbose;
2829 if (vim_isdigit(*eap->cmd))
2830 p_verbose = atoi((char *)eap->cmd);
2831 else
2832 p_verbose = 1;
2833 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002834 eap->cmd = p;
2835 continue;
2836 }
2837 break;
2838 }
2839
2840 return OK;
2841}
2842
2843/*
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002844 * Free contents of "cmdmod".
2845 */
2846 static void
2847free_cmdmod(void)
2848{
2849 if (cmdmod.save_ei != NULL)
2850 {
2851 /* Restore 'eventignore' to the value before ":noautocmd". */
2852 set_string_option_direct((char_u *)"ei", -1, cmdmod.save_ei,
2853 OPT_FREE, SID_NONE);
2854 free_string_option(cmdmod.save_ei);
2855 }
2856
2857 if (cmdmod.filter_regmatch.regprog != NULL)
2858 vim_regfree(cmdmod.filter_regmatch.regprog);
2859}
2860
2861/*
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002862 * Parse the address range, if any, in "eap".
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02002863 * May set the last search pattern, unless "silent" is TRUE.
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002864 * Return FAIL and set "errormsg" or return OK.
2865 */
2866 int
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002867parse_cmd_address(exarg_T *eap, char **errormsg, int silent)
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002868{
2869 int address_count = 1;
2870 linenr_T lnum;
2871
2872 // Repeat for all ',' or ';' separated addresses.
2873 for (;;)
2874 {
2875 eap->line1 = eap->line2;
2876 switch (eap->addr_type)
2877 {
2878 case ADDR_LINES:
2879 // default is current line number
2880 eap->line2 = curwin->w_cursor.lnum;
2881 break;
2882 case ADDR_WINDOWS:
2883 eap->line2 = CURRENT_WIN_NR;
2884 break;
2885 case ADDR_ARGUMENTS:
2886 eap->line2 = curwin->w_arg_idx + 1;
2887 if (eap->line2 > ARGCOUNT)
2888 eap->line2 = ARGCOUNT;
2889 break;
2890 case ADDR_LOADED_BUFFERS:
2891 case ADDR_BUFFERS:
2892 eap->line2 = curbuf->b_fnum;
2893 break;
2894 case ADDR_TABS:
2895 eap->line2 = CURRENT_TAB_NR;
2896 break;
2897 case ADDR_TABS_RELATIVE:
2898 eap->line2 = 1;
2899 break;
2900#ifdef FEAT_QUICKFIX
2901 case ADDR_QUICKFIX:
2902 eap->line2 = qf_get_cur_valid_idx(eap);
2903 break;
2904#endif
2905 }
2906 eap->cmd = skipwhite(eap->cmd);
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02002907 lnum = get_address(eap, &eap->cmd, eap->addr_type, eap->skip, silent,
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002908 eap->addr_count == 0, address_count++);
2909 if (eap->cmd == NULL) // error detected
2910 return FAIL;
2911 if (lnum == MAXLNUM)
2912 {
2913 if (*eap->cmd == '%') // '%' - all lines
2914 {
2915 ++eap->cmd;
2916 switch (eap->addr_type)
2917 {
2918 case ADDR_LINES:
2919 eap->line1 = 1;
2920 eap->line2 = curbuf->b_ml.ml_line_count;
2921 break;
2922 case ADDR_LOADED_BUFFERS:
2923 {
2924 buf_T *buf = firstbuf;
2925
2926 while (buf->b_next != NULL
2927 && buf->b_ml.ml_mfp == NULL)
2928 buf = buf->b_next;
2929 eap->line1 = buf->b_fnum;
2930 buf = lastbuf;
2931 while (buf->b_prev != NULL
2932 && buf->b_ml.ml_mfp == NULL)
2933 buf = buf->b_prev;
2934 eap->line2 = buf->b_fnum;
2935 break;
2936 }
2937 case ADDR_BUFFERS:
2938 eap->line1 = firstbuf->b_fnum;
2939 eap->line2 = lastbuf->b_fnum;
2940 break;
2941 case ADDR_WINDOWS:
2942 case ADDR_TABS:
2943 if (IS_USER_CMDIDX(eap->cmdidx))
2944 {
2945 eap->line1 = 1;
2946 eap->line2 = eap->addr_type == ADDR_WINDOWS
2947 ? LAST_WIN_NR : LAST_TAB_NR;
2948 }
2949 else
2950 {
2951 // there is no Vim command which uses '%' and
2952 // ADDR_WINDOWS or ADDR_TABS
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002953 *errormsg = _(e_invrange);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002954 return FAIL;
2955 }
2956 break;
2957 case ADDR_TABS_RELATIVE:
Bram Moolenaar51a74542018-12-02 18:21:49 +01002958 case ADDR_OTHER:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002959 *errormsg = _(e_invrange);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002960 return FAIL;
2961 case ADDR_ARGUMENTS:
2962 if (ARGCOUNT == 0)
2963 eap->line1 = eap->line2 = 0;
2964 else
2965 {
2966 eap->line1 = 1;
2967 eap->line2 = ARGCOUNT;
2968 }
2969 break;
2970#ifdef FEAT_QUICKFIX
2971 case ADDR_QUICKFIX:
2972 eap->line1 = 1;
2973 eap->line2 = qf_get_size(eap);
2974 if (eap->line2 == 0)
2975 eap->line2 = 1;
2976 break;
2977#endif
2978 }
2979 ++eap->addr_count;
2980 }
2981 else if (*eap->cmd == '*' && vim_strchr(p_cpo, CPO_STAR) == NULL)
2982 {
2983 pos_T *fp;
2984
2985 // '*' - visual area
2986 if (eap->addr_type != ADDR_LINES)
2987 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002988 *errormsg = _(e_invrange);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002989 return FAIL;
2990 }
2991
2992 ++eap->cmd;
2993 if (!eap->skip)
2994 {
2995 fp = getmark('<', FALSE);
2996 if (check_mark(fp) == FAIL)
2997 return FAIL;
2998 eap->line1 = fp->lnum;
2999 fp = getmark('>', FALSE);
3000 if (check_mark(fp) == FAIL)
3001 return FAIL;
3002 eap->line2 = fp->lnum;
3003 ++eap->addr_count;
3004 }
3005 }
3006 }
3007 else
3008 eap->line2 = lnum;
3009 eap->addr_count++;
3010
3011 if (*eap->cmd == ';')
3012 {
3013 if (!eap->skip)
3014 {
3015 curwin->w_cursor.lnum = eap->line2;
3016 // don't leave the cursor on an illegal line or column
3017 check_cursor();
3018 }
3019 }
3020 else if (*eap->cmd != ',')
3021 break;
3022 ++eap->cmd;
3023 }
3024
3025 // One address given: set start and end lines.
3026 if (eap->addr_count == 1)
3027 {
3028 eap->line1 = eap->line2;
3029 // ... but only implicit: really no address given
3030 if (lnum == MAXLNUM)
3031 eap->addr_count = 0;
3032 }
3033 return OK;
3034}
3035
3036/*
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00003037 * Check for an Ex command with optional tail.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003038 * If there is a match advance "pp" to the argument and return TRUE.
3039 */
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00003040 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003041checkforcmd(
3042 char_u **pp, /* start of command */
3043 char *cmd, /* name of command */
3044 int len) /* required length */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003045{
3046 int i;
3047
3048 for (i = 0; cmd[i] != NUL; ++i)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003049 if (((char_u *)cmd)[i] != (*pp)[i])
Bram Moolenaar071d4272004-06-13 20:20:40 +00003050 break;
3051 if (i >= len && !isalpha((*pp)[i]))
3052 {
3053 *pp = skipwhite(*pp + i);
3054 return TRUE;
3055 }
3056 return FALSE;
3057}
3058
3059/*
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003060 * Append "cmd" to the error message in IObuff.
3061 * Takes care of limiting the length and handling 0xa0, which would be
3062 * invisible otherwise.
3063 */
3064 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003065append_command(char_u *cmd)
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003066{
3067 char_u *s = cmd;
3068 char_u *d;
3069
3070 STRCAT(IObuff, ": ");
3071 d = IObuff + STRLEN(IObuff);
3072 while (*s != NUL && d - IObuff < IOSIZE - 7)
3073 {
Bram Moolenaar13505972019-01-24 15:04:48 +01003074 if (enc_utf8 ? (s[0] == 0xc2 && s[1] == 0xa0) : *s == 0xa0)
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003075 {
Bram Moolenaar13505972019-01-24 15:04:48 +01003076 s += enc_utf8 ? 2 : 1;
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003077 STRCPY(d, "<a0>");
3078 d += 4;
3079 }
3080 else
3081 MB_COPY_CHAR(s, d);
3082 }
3083 *d = NUL;
3084}
3085
3086/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003087 * Find an Ex command by its name, either built-in or user.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003088 * Start of the name can be found at eap->cmd.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003089 * Returns pointer to char after the command name.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003090 * "full" is set to TRUE if the whole command name matched.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003091 * Returns NULL for an ambiguous user command.
3092 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003093 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003094find_command(exarg_T *eap, int *full UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003095{
3096 int len;
3097 char_u *p;
Bram Moolenaardf177f62005-02-22 08:39:57 +00003098 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003099
3100 /*
3101 * Isolate the command and search for it in the command table.
Bram Moolenaar81870892007-11-11 18:17:28 +00003102 * Exceptions:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003103 * - the 'k' command can directly be followed by any character.
3104 * - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003105 * but :sre[wind] is another command, as are :scr[iptnames],
Bram Moolenaar071d4272004-06-13 20:20:40 +00003106 * :scs[cope], :sim[alt], :sig[ns] and :sil[ent].
Bram Moolenaardf177f62005-02-22 08:39:57 +00003107 * - the "d" command can directly be followed by 'l' or 'p' flag.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003108 */
3109 p = eap->cmd;
3110 if (*p == 'k')
3111 {
3112 eap->cmdidx = CMD_k;
3113 ++p;
3114 }
3115 else if (p[0] == 's'
Bram Moolenaar204b93f2015-08-04 22:02:51 +02003116 && ((p[1] == 'c' && (p[2] == NUL || (p[2] != 's' && p[2] != 'r'
3117 && (p[3] == NUL || (p[3] != 'i' && p[4] != 'p')))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003118 || p[1] == 'g'
3119 || (p[1] == 'i' && p[2] != 'm' && p[2] != 'l' && p[2] != 'g')
3120 || p[1] == 'I'
3121 || (p[1] == 'r' && p[2] != 'e')))
3122 {
3123 eap->cmdidx = CMD_substitute;
3124 ++p;
3125 }
3126 else
3127 {
3128 while (ASCII_ISALPHA(*p))
3129 ++p;
Bram Moolenaarb6590522010-07-21 16:00:43 +02003130 /* for python 3.x support ":py3", ":python3", ":py3file", etc. */
Bram Moolenaar55d5c032010-07-17 23:52:29 +02003131 if (eap->cmd[0] == 'p' && eap->cmd[1] == 'y')
Bram Moolenaarb6590522010-07-21 16:00:43 +02003132 while (ASCII_ISALNUM(*p))
3133 ++p;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02003134
Bram Moolenaar071d4272004-06-13 20:20:40 +00003135 /* check for non-alpha command */
3136 if (p == eap->cmd && vim_strchr((char_u *)"@*!=><&~#", *p) != NULL)
3137 ++p;
3138 len = (int)(p - eap->cmd);
Bram Moolenaardf177f62005-02-22 08:39:57 +00003139 if (*eap->cmd == 'd' && (p[-1] == 'l' || p[-1] == 'p'))
3140 {
3141 /* Check for ":dl", ":dell", etc. to ":deletel": that's
3142 * :delete with the 'l' flag. Same for 'p'. */
3143 for (i = 0; i < len; ++i)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003144 if (eap->cmd[i] != ((char_u *)"delete")[i])
Bram Moolenaardf177f62005-02-22 08:39:57 +00003145 break;
3146 if (i == len - 1)
3147 {
3148 --len;
3149 if (p[-1] == 'l')
3150 eap->flags |= EXFLAG_LIST;
3151 else
3152 eap->flags |= EXFLAG_PRINT;
3153 }
3154 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003155
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003156 if (ASCII_ISLOWER(eap->cmd[0]))
3157 {
Bram Moolenaar6c0c1e82017-03-25 15:07:43 +01003158 int c1 = eap->cmd[0];
3159 int c2 = eap->cmd[1];
3160
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003161 if (command_count != (int)CMD_SIZE)
3162 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003163 iemsg(_("E943: Command table needs to be updated, run 'make cmdidxs'"));
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003164 getout(1);
3165 }
3166
3167 /* Use a precomputed index for fast look-up in cmdnames[]
3168 * taking into account the first 2 letters of eap->cmd. */
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003169 eap->cmdidx = cmdidxs1[CharOrdLow(c1)];
3170 if (ASCII_ISLOWER(c2))
3171 eap->cmdidx += cmdidxs2[CharOrdLow(c1)][CharOrdLow(c2)];
3172 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003173 else
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003174 eap->cmdidx = CMD_bang;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003175
3176 for ( ; (int)eap->cmdidx < (int)CMD_SIZE;
3177 eap->cmdidx = (cmdidx_T)((int)eap->cmdidx + 1))
3178 if (STRNCMP(cmdnames[(int)eap->cmdidx].cmd_name, (char *)eap->cmd,
3179 (size_t)len) == 0)
3180 {
3181#ifdef FEAT_EVAL
3182 if (full != NULL
3183 && cmdnames[(int)eap->cmdidx].cmd_name[len] == NUL)
3184 *full = TRUE;
3185#endif
3186 break;
3187 }
3188
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003189 // Look for a user defined command as a last resort. Let ":Print" be
3190 // overruled by a user defined command.
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01003191 if ((eap->cmdidx == CMD_SIZE || eap->cmdidx == CMD_Print)
3192 && *eap->cmd >= 'A' && *eap->cmd <= 'Z')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003193 {
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003194 // User defined commands may contain digits.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003195 while (ASCII_ISALNUM(*p))
3196 ++p;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003197 p = find_ucmd(eap, p, full, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003198 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003199 if (p == eap->cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003200 eap->cmdidx = CMD_SIZE;
3201 }
3202
3203 return p;
3204}
3205
3206#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003207static struct cmdmod
3208{
3209 char *name;
3210 int minlen;
3211 int has_count; /* :123verbose :3tab */
3212} cmdmods[] = {
3213 {"aboveleft", 3, FALSE},
3214 {"belowright", 3, FALSE},
3215 {"botright", 2, FALSE},
3216 {"browse", 3, FALSE},
3217 {"confirm", 4, FALSE},
Bram Moolenaar7b668e82016-08-23 23:51:21 +02003218 {"filter", 4, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003219 {"hide", 3, FALSE},
3220 {"keepalt", 5, FALSE},
3221 {"keepjumps", 5, FALSE},
3222 {"keepmarks", 3, FALSE},
Bram Moolenaara939e432013-11-09 05:30:26 +01003223 {"keeppatterns", 5, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003224 {"leftabove", 5, FALSE},
3225 {"lockmarks", 3, FALSE},
Bram Moolenaarca9f9582008-09-18 10:44:28 +00003226 {"noautocmd", 3, FALSE},
Bram Moolenaar5803ae62014-03-23 16:04:02 +01003227 {"noswapfile", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003228 {"rightbelow", 6, FALSE},
3229 {"sandbox", 3, FALSE},
3230 {"silent", 3, FALSE},
3231 {"tab", 3, TRUE},
3232 {"topleft", 2, FALSE},
Bram Moolenaar8e258a42009-07-09 13:55:43 +00003233 {"unsilent", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003234 {"verbose", 4, TRUE},
3235 {"vertical", 4, FALSE},
3236};
3237
3238/*
3239 * Return length of a command modifier (including optional count).
3240 * Return zero when it's not a modifier.
3241 */
3242 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003243modifier_len(char_u *cmd)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003244{
3245 int i, j;
3246 char_u *p = cmd;
3247
3248 if (VIM_ISDIGIT(*cmd))
3249 p = skipwhite(skipdigits(cmd));
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00003250 for (i = 0; i < (int)(sizeof(cmdmods) / sizeof(struct cmdmod)); ++i)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003251 {
3252 for (j = 0; p[j] != NUL; ++j)
3253 if (p[j] != cmdmods[i].name[j])
3254 break;
Bram Moolenaar2d473ab2013-06-12 17:12:24 +02003255 if (!ASCII_ISALPHA(p[j]) && j >= cmdmods[i].minlen
Bram Moolenaared53fb92007-11-24 20:50:24 +00003256 && (p == cmd || cmdmods[i].has_count))
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00003257 return j + (int)(p - cmd);
Bram Moolenaared53fb92007-11-24 20:50:24 +00003258 }
3259 return 0;
3260}
3261
Bram Moolenaar071d4272004-06-13 20:20:40 +00003262/*
3263 * Return > 0 if an Ex command "name" exists.
3264 * Return 2 if there is an exact match.
3265 * Return 3 if there is an ambiguous match.
3266 */
3267 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003268cmd_exists(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003269{
3270 exarg_T ea;
3271 int full = FALSE;
3272 int i;
3273 int j;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003274 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003275
3276 /* Check command modifiers. */
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00003277 for (i = 0; i < (int)(sizeof(cmdmods) / sizeof(struct cmdmod)); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003278 {
3279 for (j = 0; name[j] != NUL; ++j)
3280 if (name[j] != cmdmods[i].name[j])
3281 break;
3282 if (name[j] == NUL && j >= cmdmods[i].minlen)
3283 return (cmdmods[i].name[j] == NUL ? 2 : 1);
3284 }
3285
Bram Moolenaara9587612006-05-04 21:47:50 +00003286 /* Check built-in commands and user defined commands.
3287 * For ":2match" and ":3match" we need to skip the number. */
3288 ea.cmd = (*name == '2' || *name == '3') ? name + 1 : name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003289 ea.cmdidx = (cmdidx_T)0;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003290 p = find_command(&ea, &full);
3291 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003292 return 3;
Bram Moolenaara9587612006-05-04 21:47:50 +00003293 if (vim_isdigit(*name) && ea.cmdidx != CMD_match)
3294 return 0;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003295 if (*skipwhite(p) != NUL)
3296 return 0; /* trailing garbage */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003297 return (ea.cmdidx == CMD_SIZE ? 0 : (full ? 2 : 1));
3298}
3299#endif
3300
3301/*
3302 * This is all pretty much copied from do_one_cmd(), with all the extra stuff
3303 * we don't need/want deleted. Maybe this could be done better if we didn't
3304 * repeat all this stuff. The only problem is that they may not stay
3305 * perfectly compatible with each other, but then the command line syntax
3306 * probably won't change that much -- webb.
3307 */
3308 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003309set_one_cmd_context(
3310 expand_T *xp,
3311 char_u *buff) /* buffer for command string */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003312{
3313 char_u *p;
3314 char_u *cmd, *arg;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003315 int len = 0;
3316 exarg_T ea;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003317#ifdef FEAT_CMDL_COMPL
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003318 int compl = EXPAND_NOTHING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003319 int delim;
3320#endif
3321 int forceit = FALSE;
3322 int usefilter = FALSE; /* filter instead of file name */
3323
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003324 ExpandInit(xp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003325 xp->xp_pattern = buff;
3326 xp->xp_context = EXPAND_COMMANDS; /* Default until we get past command */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003327 ea.argt = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003328
3329/*
Bram Moolenaar1c40a662014-11-27 16:38:11 +01003330 * 1. skip comment lines and leading space, colons or bars
Bram Moolenaar071d4272004-06-13 20:20:40 +00003331 */
3332 for (cmd = buff; vim_strchr((char_u *)" \t:|", *cmd) != NULL; cmd++)
3333 ;
3334 xp->xp_pattern = cmd;
3335
3336 if (*cmd == NUL)
3337 return NULL;
3338 if (*cmd == '"') /* ignore comment lines */
3339 {
3340 xp->xp_context = EXPAND_NOTHING;
3341 return NULL;
3342 }
3343
3344/*
Bram Moolenaar1c40a662014-11-27 16:38:11 +01003345 * 3. Skip over the range to find the command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003346 */
3347 cmd = skip_range(cmd, &xp->xp_context);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003348 xp->xp_pattern = cmd;
3349 if (*cmd == NUL)
3350 return NULL;
3351 if (*cmd == '"')
3352 {
3353 xp->xp_context = EXPAND_NOTHING;
3354 return NULL;
3355 }
3356
3357 if (*cmd == '|' || *cmd == '\n')
3358 return cmd + 1; /* There's another command */
3359
3360 /*
3361 * Isolate the command and search for it in the command table.
3362 * Exceptions:
3363 * - the 'k' command can directly be followed by any character, but
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003364 * do accept "keepmarks", "keepalt" and "keepjumps".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003365 * - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
3366 */
3367 if (*cmd == 'k' && cmd[1] != 'e')
3368 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003369 ea.cmdidx = CMD_k;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003370 p = cmd + 1;
3371 }
3372 else
3373 {
3374 p = cmd;
3375 while (ASCII_ISALPHA(*p) || *p == '*') /* Allow * wild card */
3376 ++p;
Bram Moolenaar23d1b622015-10-13 19:18:04 +02003377 /* a user command may contain digits */
3378 if (ASCII_ISUPPER(cmd[0]))
3379 while (ASCII_ISALNUM(*p) || *p == '*')
3380 ++p;
Bram Moolenaar9f5d6002013-06-02 19:22:13 +02003381 /* for python 3.x: ":py3*" commands completion */
3382 if (cmd[0] == 'p' && cmd[1] == 'y' && p == cmd + 2 && *p == '3')
Bram Moolenaar893b2d72013-12-11 17:44:38 +01003383 {
Bram Moolenaar9f5d6002013-06-02 19:22:13 +02003384 ++p;
Bram Moolenaar893b2d72013-12-11 17:44:38 +01003385 while (ASCII_ISALPHA(*p) || *p == '*')
3386 ++p;
3387 }
Bram Moolenaar23d1b622015-10-13 19:18:04 +02003388 /* check for non-alpha command */
3389 if (p == cmd && vim_strchr((char_u *)"@*!=><&~#", *p) != NULL)
3390 ++p;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003391 len = (int)(p - cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003392
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003393 if (len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003394 {
3395 xp->xp_context = EXPAND_UNSUCCESSFUL;
3396 return NULL;
3397 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003398 for (ea.cmdidx = (cmdidx_T)0; (int)ea.cmdidx < (int)CMD_SIZE;
Bram Moolenaar42b4dda2010-03-02 15:56:05 +01003399 ea.cmdidx = (cmdidx_T)((int)ea.cmdidx + 1))
3400 if (STRNCMP(cmdnames[(int)ea.cmdidx].cmd_name, cmd,
3401 (size_t)len) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003402 break;
3403
Bram Moolenaar071d4272004-06-13 20:20:40 +00003404 if (cmd[0] >= 'A' && cmd[0] <= 'Z')
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003405 while (ASCII_ISALNUM(*p) || *p == '*') // Allow * wild card
Bram Moolenaar071d4272004-06-13 20:20:40 +00003406 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003407 }
3408
3409 /*
3410 * If the cursor is touching the command, and it ends in an alpha-numeric
3411 * character, complete the command name.
3412 */
3413 if (*p == NUL && ASCII_ISALNUM(p[-1]))
3414 return NULL;
3415
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003416 if (ea.cmdidx == CMD_SIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003417 {
3418 if (*cmd == 's' && vim_strchr((char_u *)"cgriI", cmd[1]) != NULL)
3419 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003420 ea.cmdidx = CMD_substitute;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003421 p = cmd + 1;
3422 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003423 else if (cmd[0] >= 'A' && cmd[0] <= 'Z')
3424 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003425 ea.cmd = cmd;
3426 p = find_ucmd(&ea, p, NULL, xp,
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003427#if defined(FEAT_CMDL_COMPL)
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003428 &compl
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003429#else
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003430 NULL
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003431#endif
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003432 );
Bram Moolenaarebefac62005-12-28 22:39:57 +00003433 if (p == NULL)
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003434 ea.cmdidx = CMD_SIZE; // ambiguous user command
Bram Moolenaar071d4272004-06-13 20:20:40 +00003435 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003436 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003437 if (ea.cmdidx == CMD_SIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003438 {
3439 /* Not still touching the command and it was an illegal one */
3440 xp->xp_context = EXPAND_UNSUCCESSFUL;
3441 return NULL;
3442 }
3443
3444 xp->xp_context = EXPAND_NOTHING; /* Default now that we're past command */
3445
3446 if (*p == '!') /* forced commands */
3447 {
3448 forceit = TRUE;
3449 ++p;
3450 }
3451
3452/*
Bram Moolenaar1c40a662014-11-27 16:38:11 +01003453 * 6. parse arguments
Bram Moolenaar071d4272004-06-13 20:20:40 +00003454 */
Bram Moolenaar958636c2014-10-21 20:01:58 +02003455 if (!IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003456 ea.argt = (long)cmdnames[(int)ea.cmdidx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003457
3458 arg = skipwhite(p);
3459
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003460 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003461 {
3462 if (*arg == '>') /* append */
3463 {
3464 if (*++arg == '>')
3465 ++arg;
3466 arg = skipwhite(arg);
3467 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003468 else if (*arg == '!' && ea.cmdidx == CMD_write) /* :w !filter */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003469 {
3470 ++arg;
3471 usefilter = TRUE;
3472 }
3473 }
3474
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003475 if (ea.cmdidx == CMD_read)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003476 {
3477 usefilter = forceit; /* :r! filter if forced */
3478 if (*arg == '!') /* :r !filter */
3479 {
3480 ++arg;
3481 usefilter = TRUE;
3482 }
3483 }
3484
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003485 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003486 {
3487 while (*arg == *cmd) /* allow any number of '>' or '<' */
3488 ++arg;
3489 arg = skipwhite(arg);
3490 }
3491
3492 /* Does command allow "+command"? */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003493 if ((ea.argt & EDITCMD) && !usefilter && *arg == '+')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003494 {
3495 /* Check if we're in the +command */
3496 p = arg + 1;
3497 arg = skip_cmd_arg(arg, FALSE);
3498
3499 /* Still touching the command after '+'? */
3500 if (*arg == NUL)
3501 return p;
3502
3503 /* Skip space(s) after +command to get to the real argument */
3504 arg = skipwhite(arg);
3505 }
3506
3507 /*
3508 * Check for '|' to separate commands and '"' to start comments.
3509 * Don't do this for ":read !cmd" and ":write !cmd".
3510 */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003511 if ((ea.argt & TRLBAR) && !usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003512 {
3513 p = arg;
3514 /* ":redir @" is not the start of a comment */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003515 if (ea.cmdidx == CMD_redir && p[0] == '@' && p[1] == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003516 p += 2;
3517 while (*p)
3518 {
3519 if (*p == Ctrl_V)
3520 {
3521 if (p[1] != NUL)
3522 ++p;
3523 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003524 else if ( (*p == '"' && !(ea.argt & NOTRLCOM))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003525 || *p == '|' || *p == '\n')
3526 {
3527 if (*(p - 1) != '\\')
3528 {
3529 if (*p == '|' || *p == '\n')
3530 return p + 1;
3531 return NULL; /* It's a comment */
3532 }
3533 }
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003534 MB_PTR_ADV(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003535 }
3536 }
3537
3538 /* no arguments allowed */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003539 if (!(ea.argt & EXTRA) && *arg != NUL &&
Bram Moolenaar071d4272004-06-13 20:20:40 +00003540 vim_strchr((char_u *)"|\"", *arg) == NULL)
3541 return NULL;
3542
3543 /* Find start of last argument (argument just before cursor): */
Bram Moolenaar848f8762012-07-25 17:22:23 +02003544 p = buff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003545 xp->xp_pattern = p;
Bram Moolenaar09168a72012-08-02 21:24:42 +02003546 len = (int)STRLEN(buff);
Bram Moolenaar848f8762012-07-25 17:22:23 +02003547 while (*p && p < buff + len)
3548 {
3549 if (*p == ' ' || *p == TAB)
3550 {
3551 /* argument starts after a space */
3552 xp->xp_pattern = ++p;
3553 }
3554 else
3555 {
3556 if (*p == '\\' && *(p + 1) != NUL)
3557 ++p; /* skip over escaped character */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003558 MB_PTR_ADV(p);
Bram Moolenaar848f8762012-07-25 17:22:23 +02003559 }
3560 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003561
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003562 if (ea.argt & XFILE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003563 {
Bram Moolenaar6529c102007-08-18 15:47:34 +00003564 int c;
3565 int in_quote = FALSE;
3566 char_u *bow = NULL; /* Beginning of word */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003567
3568 /*
3569 * Allow spaces within back-quotes to count as part of the argument
3570 * being expanded.
3571 */
3572 xp->xp_pattern = skipwhite(arg);
Bram Moolenaar6529c102007-08-18 15:47:34 +00003573 p = xp->xp_pattern;
3574 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003575 {
Bram Moolenaar6529c102007-08-18 15:47:34 +00003576 if (has_mbyte)
3577 c = mb_ptr2char(p);
3578 else
Bram Moolenaar6529c102007-08-18 15:47:34 +00003579 c = *p;
3580 if (c == '\\' && p[1] != NUL)
3581 ++p;
3582 else if (c == '`')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003583 {
3584 if (!in_quote)
3585 {
3586 xp->xp_pattern = p;
3587 bow = p + 1;
3588 }
3589 in_quote = !in_quote;
3590 }
Bram Moolenaar332fa0c2008-01-13 16:12:10 +00003591 /* An argument can contain just about everything, except
3592 * characters that end the command and white space. */
Bram Moolenaar1c465442017-03-12 20:10:05 +01003593 else if (c == '|' || c == '\n' || c == '"' || (VIM_ISWHITE(c)
Bram Moolenaar6529c102007-08-18 15:47:34 +00003594#ifdef SPACE_IN_FILENAME
Bram Moolenaar332fa0c2008-01-13 16:12:10 +00003595 && (!(ea.argt & NOSPC) || usefilter)
Bram Moolenaar6529c102007-08-18 15:47:34 +00003596#endif
Bram Moolenaar332fa0c2008-01-13 16:12:10 +00003597 ))
Bram Moolenaar6529c102007-08-18 15:47:34 +00003598 {
Bram Moolenaarcf5a5b82008-02-26 20:30:12 +00003599 len = 0; /* avoid getting stuck when space is in 'isfname' */
Bram Moolenaar6529c102007-08-18 15:47:34 +00003600 while (*p != NUL)
3601 {
Bram Moolenaar6529c102007-08-18 15:47:34 +00003602 if (has_mbyte)
3603 c = mb_ptr2char(p);
3604 else
Bram Moolenaar6529c102007-08-18 15:47:34 +00003605 c = *p;
Bram Moolenaardd87969c2007-08-21 13:07:12 +00003606 if (c == '`' || vim_isfilec_or_wc(c))
Bram Moolenaar6529c102007-08-18 15:47:34 +00003607 break;
Bram Moolenaar6529c102007-08-18 15:47:34 +00003608 if (has_mbyte)
3609 len = (*mb_ptr2len)(p);
3610 else
Bram Moolenaar6529c102007-08-18 15:47:34 +00003611 len = 1;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003612 MB_PTR_ADV(p);
Bram Moolenaar6529c102007-08-18 15:47:34 +00003613 }
3614 if (in_quote)
3615 bow = p;
3616 else
3617 xp->xp_pattern = p;
3618 p -= len;
3619 }
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003620 MB_PTR_ADV(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003621 }
3622
3623 /*
3624 * If we are still inside the quotes, and we passed a space, just
3625 * expand from there.
3626 */
3627 if (bow != NULL && in_quote)
3628 xp->xp_pattern = bow;
3629 xp->xp_context = EXPAND_FILES;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003630
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003631 /* For a shell command more chars need to be escaped. */
Bram Moolenaar67883b42017-07-27 22:57:00 +02003632 if (usefilter || ea.cmdidx == CMD_bang || ea.cmdidx == CMD_terminal)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003633 {
Bram Moolenaarc0cba4d2010-08-07 17:07:21 +02003634#ifndef BACKSLASH_IN_FILENAME
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003635 xp->xp_shell = TRUE;
Bram Moolenaarc0cba4d2010-08-07 17:07:21 +02003636#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003637 /* When still after the command name expand executables. */
3638 if (xp->xp_pattern == skipwhite(arg))
Bram Moolenaar5c5b0942007-05-06 12:07:59 +00003639 xp->xp_context = EXPAND_SHELLCMD;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003640 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003641
3642 /* Check for environment variable */
3643 if (*xp->xp_pattern == '$'
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003644#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003645 || *xp->xp_pattern == '%'
3646#endif
3647 )
3648 {
3649 for (p = xp->xp_pattern + 1; *p != NUL; ++p)
3650 if (!vim_isIDc(*p))
3651 break;
3652 if (*p == NUL)
3653 {
3654 xp->xp_context = EXPAND_ENV_VARS;
3655 ++xp->xp_pattern;
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003656#if defined(FEAT_CMDL_COMPL)
Bram Moolenaar21cf8232004-07-16 20:18:37 +00003657 /* Avoid that the assignment uses EXPAND_FILES again. */
Bram Moolenaara466c992005-07-09 21:03:22 +00003658 if (compl != EXPAND_USER_DEFINED && compl != EXPAND_USER_LIST)
Bram Moolenaar21cf8232004-07-16 20:18:37 +00003659 compl = EXPAND_ENV_VARS;
3660#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003661 }
3662 }
Bram Moolenaar24305862012-08-15 14:05:05 +02003663#if defined(FEAT_CMDL_COMPL)
3664 /* Check for user names */
3665 if (*xp->xp_pattern == '~')
3666 {
3667 for (p = xp->xp_pattern + 1; *p != NUL && *p != '/'; ++p)
3668 ;
3669 /* Complete ~user only if it partially matches a user name.
3670 * A full match ~user<Tab> will be replaced by user's home
3671 * directory i.e. something like ~user<Tab> -> /home/user/ */
3672 if (*p == NUL && p > xp->xp_pattern + 1
Bram Moolenaar6c5d1042018-07-07 16:41:13 +02003673 && match_user(xp->xp_pattern + 1) >= 1)
Bram Moolenaar24305862012-08-15 14:05:05 +02003674 {
3675 xp->xp_context = EXPAND_USER;
3676 ++xp->xp_pattern;
3677 }
3678 }
3679#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003680 }
3681
3682/*
Bram Moolenaar1c40a662014-11-27 16:38:11 +01003683 * 6. Switch on command name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003684 */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003685 switch (ea.cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003686 {
Bram Moolenaarcc448b32010-07-14 16:52:17 +02003687 case CMD_find:
3688 case CMD_sfind:
3689 case CMD_tabfind:
Bram Moolenaarc24b6972010-08-16 22:34:29 +02003690 if (xp->xp_context == EXPAND_FILES)
3691 xp->xp_context = EXPAND_FILES_IN_PATH;
Bram Moolenaarcc448b32010-07-14 16:52:17 +02003692 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003693 case CMD_cd:
3694 case CMD_chdir:
Bram Moolenaar00aa0692019-04-27 20:37:57 +02003695 case CMD_tcd:
3696 case CMD_tchdir:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003697 case CMD_lcd:
3698 case CMD_lchdir:
3699 if (xp->xp_context == EXPAND_FILES)
3700 xp->xp_context = EXPAND_DIRECTORIES;
3701 break;
3702 case CMD_help:
3703 xp->xp_context = EXPAND_HELP;
3704 xp->xp_pattern = arg;
3705 break;
3706
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003707 /* Command modifiers: return the argument.
3708 * Also for commands with an argument that is a command. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003709 case CMD_aboveleft:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003710 case CMD_argdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003711 case CMD_belowright:
3712 case CMD_botright:
3713 case CMD_browse:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003714 case CMD_bufdo:
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003715 case CMD_cdo:
3716 case CMD_cfdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003717 case CMD_confirm:
Bram Moolenaardf177f62005-02-22 08:39:57 +00003718 case CMD_debug:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003719 case CMD_folddoclosed:
3720 case CMD_folddoopen:
3721 case CMD_hide:
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003722 case CMD_keepalt:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003723 case CMD_keepjumps:
3724 case CMD_keepmarks:
Bram Moolenaara939e432013-11-09 05:30:26 +01003725 case CMD_keeppatterns:
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003726 case CMD_ldo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003727 case CMD_leftabove:
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003728 case CMD_lfdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003729 case CMD_lockmarks:
Bram Moolenaar5803ae62014-03-23 16:04:02 +01003730 case CMD_noautocmd:
3731 case CMD_noswapfile:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003732 case CMD_rightbelow:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003733 case CMD_sandbox:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003734 case CMD_silent:
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003735 case CMD_tab:
Bram Moolenaar70baa402013-06-16 16:14:03 +02003736 case CMD_tabdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003737 case CMD_topleft:
3738 case CMD_verbose:
3739 case CMD_vertical:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003740 case CMD_windo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003741 return arg;
3742
Bram Moolenaar7069bf12017-01-07 20:39:53 +01003743 case CMD_filter:
3744 if (*arg != NUL)
3745 arg = skip_vimgrep_pat(arg, NULL, NULL);
3746 if (arg == NULL || *arg == NUL)
3747 {
3748 xp->xp_context = EXPAND_NOTHING;
3749 return NULL;
3750 }
3751 return skipwhite(arg);
3752
Bram Moolenaar4f688582007-07-24 12:34:30 +00003753#ifdef FEAT_CMDL_COMPL
3754# ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar071d4272004-06-13 20:20:40 +00003755 case CMD_match:
3756 if (*arg == NUL || !ends_excmd(*arg))
3757 {
Bram Moolenaar4f688582007-07-24 12:34:30 +00003758 /* also complete "None" */
3759 set_context_in_echohl_cmd(xp, arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003760 arg = skipwhite(skiptowhite(arg));
3761 if (*arg != NUL)
3762 {
3763 xp->xp_context = EXPAND_NOTHING;
3764 arg = skip_regexp(arg + 1, *arg, p_magic, NULL);
3765 }
3766 }
3767 return find_nextcmd(arg);
Bram Moolenaar4f688582007-07-24 12:34:30 +00003768# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003769
Bram Moolenaar071d4272004-06-13 20:20:40 +00003770/*
3771 * All completion for the +cmdline_compl feature goes here.
3772 */
3773
Bram Moolenaar071d4272004-06-13 20:20:40 +00003774 case CMD_command:
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003775 return set_context_in_user_cmd(xp, arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003776
3777 case CMD_delcommand:
3778 xp->xp_context = EXPAND_USER_COMMANDS;
3779 xp->xp_pattern = arg;
3780 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003781
3782 case CMD_global:
3783 case CMD_vglobal:
3784 delim = *arg; /* get the delimiter */
3785 if (delim)
3786 ++arg; /* skip delimiter if there is one */
3787
3788 while (arg[0] != NUL && arg[0] != delim)
3789 {
3790 if (arg[0] == '\\' && arg[1] != NUL)
3791 ++arg;
3792 ++arg;
3793 }
3794 if (arg[0] != NUL)
3795 return arg + 1;
3796 break;
3797 case CMD_and:
3798 case CMD_substitute:
3799 delim = *arg;
3800 if (delim)
3801 {
3802 /* skip "from" part */
3803 ++arg;
3804 arg = skip_regexp(arg, delim, p_magic, NULL);
3805 }
3806 /* skip "to" part */
3807 while (arg[0] != NUL && arg[0] != delim)
3808 {
3809 if (arg[0] == '\\' && arg[1] != NUL)
3810 ++arg;
3811 ++arg;
3812 }
3813 if (arg[0] != NUL) /* skip delimiter */
3814 ++arg;
3815 while (arg[0] && vim_strchr((char_u *)"|\"#", arg[0]) == NULL)
3816 ++arg;
3817 if (arg[0] != NUL)
3818 return arg;
3819 break;
3820 case CMD_isearch:
3821 case CMD_dsearch:
3822 case CMD_ilist:
3823 case CMD_dlist:
3824 case CMD_ijump:
3825 case CMD_psearch:
3826 case CMD_djump:
3827 case CMD_isplit:
3828 case CMD_dsplit:
3829 arg = skipwhite(skipdigits(arg)); /* skip count */
3830 if (*arg == '/') /* Match regexp, not just whole words */
3831 {
3832 for (++arg; *arg && *arg != '/'; arg++)
3833 if (*arg == '\\' && arg[1] != NUL)
3834 arg++;
3835 if (*arg)
3836 {
3837 arg = skipwhite(arg + 1);
3838
3839 /* Check for trailing illegal characters */
3840 if (*arg && vim_strchr((char_u *)"|\"\n", *arg) == NULL)
3841 xp->xp_context = EXPAND_NOTHING;
3842 else
3843 return arg;
3844 }
3845 }
3846 break;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003847
Bram Moolenaar071d4272004-06-13 20:20:40 +00003848 case CMD_autocmd:
3849 return set_context_in_autocmd(xp, arg, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003850 case CMD_doautocmd:
Bram Moolenaar73a9d7b2008-11-06 16:16:44 +00003851 case CMD_doautoall:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003852 return set_context_in_autocmd(xp, arg, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003853 case CMD_set:
3854 set_context_in_set_cmd(xp, arg, 0);
3855 break;
3856 case CMD_setglobal:
3857 set_context_in_set_cmd(xp, arg, OPT_GLOBAL);
3858 break;
3859 case CMD_setlocal:
3860 set_context_in_set_cmd(xp, arg, OPT_LOCAL);
3861 break;
3862 case CMD_tag:
3863 case CMD_stag:
3864 case CMD_ptag:
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00003865 case CMD_ltag:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003866 case CMD_tselect:
3867 case CMD_stselect:
3868 case CMD_ptselect:
3869 case CMD_tjump:
3870 case CMD_stjump:
3871 case CMD_ptjump:
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00003872 if (*p_wop != NUL)
3873 xp->xp_context = EXPAND_TAGS_LISTFILES;
3874 else
3875 xp->xp_context = EXPAND_TAGS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003876 xp->xp_pattern = arg;
3877 break;
3878 case CMD_augroup:
3879 xp->xp_context = EXPAND_AUGROUP;
3880 xp->xp_pattern = arg;
3881 break;
3882#ifdef FEAT_SYN_HL
3883 case CMD_syntax:
3884 set_context_in_syntax_cmd(xp, arg);
3885 break;
3886#endif
3887#ifdef FEAT_EVAL
3888 case CMD_let:
3889 case CMD_if:
3890 case CMD_elseif:
3891 case CMD_while:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00003892 case CMD_for:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003893 case CMD_echo:
3894 case CMD_echon:
3895 case CMD_execute:
3896 case CMD_echomsg:
3897 case CMD_echoerr:
3898 case CMD_call:
3899 case CMD_return:
Bram Moolenaar2b2207b2017-01-22 16:46:56 +01003900 case CMD_cexpr:
3901 case CMD_caddexpr:
3902 case CMD_cgetexpr:
3903 case CMD_lexpr:
3904 case CMD_laddexpr:
3905 case CMD_lgetexpr:
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003906 set_context_for_expression(xp, arg, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003907 break;
3908
3909 case CMD_unlet:
3910 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
3911 arg = xp->xp_pattern + 1;
Bram Moolenaar19834012018-06-12 17:03:39 +02003912
Bram Moolenaar071d4272004-06-13 20:20:40 +00003913 xp->xp_context = EXPAND_USER_VARS;
3914 xp->xp_pattern = arg;
Bram Moolenaar19834012018-06-12 17:03:39 +02003915
3916 if (*xp->xp_pattern == '$')
3917 {
3918 xp->xp_context = EXPAND_ENV_VARS;
3919 ++xp->xp_pattern;
3920 }
3921
Bram Moolenaar071d4272004-06-13 20:20:40 +00003922 break;
3923
3924 case CMD_function:
3925 case CMD_delfunction:
3926 xp->xp_context = EXPAND_USER_FUNC;
3927 xp->xp_pattern = arg;
3928 break;
3929
3930 case CMD_echohl:
Bram Moolenaar4f688582007-07-24 12:34:30 +00003931 set_context_in_echohl_cmd(xp, arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003932 break;
3933#endif
3934 case CMD_highlight:
3935 set_context_in_highlight_cmd(xp, arg);
3936 break;
Bram Moolenaarf4580d82009-03-18 11:52:53 +00003937#ifdef FEAT_CSCOPE
3938 case CMD_cscope:
Bram Moolenaar7bfef802009-04-22 14:25:01 +00003939 case CMD_lcscope:
3940 case CMD_scscope:
3941 set_context_in_cscope_cmd(xp, arg, ea.cmdidx);
Bram Moolenaarf4580d82009-03-18 11:52:53 +00003942 break;
3943#endif
Bram Moolenaar3c65e312009-04-29 16:47:23 +00003944#ifdef FEAT_SIGNS
3945 case CMD_sign:
3946 set_context_in_sign_cmd(xp, arg);
3947 break;
3948#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003949 case CMD_bdelete:
3950 case CMD_bwipeout:
3951 case CMD_bunload:
3952 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
3953 arg = xp->xp_pattern + 1;
Bram Moolenaar2f40d122017-10-24 21:49:36 +02003954 /* FALLTHROUGH */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003955 case CMD_buffer:
3956 case CMD_sbuffer:
3957 case CMD_checktime:
3958 xp->xp_context = EXPAND_BUFFERS;
3959 xp->xp_pattern = arg;
3960 break;
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003961
Bram Moolenaar071d4272004-06-13 20:20:40 +00003962 case CMD_USER:
3963 case CMD_USER_BUF:
3964 if (compl != EXPAND_NOTHING)
3965 {
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003966 // XFILE: file names are handled above
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003967 if (!(ea.argt & XFILE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003968 {
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003969#ifdef FEAT_MENU
Bram Moolenaar071d4272004-06-13 20:20:40 +00003970 if (compl == EXPAND_MENUS)
3971 return set_context_in_menu_cmd(xp, cmd, arg, forceit);
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003972#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003973 if (compl == EXPAND_COMMANDS)
3974 return arg;
3975 if (compl == EXPAND_MAPPINGS)
3976 return set_context_in_map_cmd(xp, (char_u *)"map",
3977 arg, forceit, FALSE, FALSE, CMD_map);
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003978 // Find start of last argument.
Bram Moolenaar848f8762012-07-25 17:22:23 +02003979 p = arg;
3980 while (*p)
3981 {
3982 if (*p == ' ')
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003983 // argument starts after a space
Bram Moolenaar848f8762012-07-25 17:22:23 +02003984 arg = p + 1;
Bram Moolenaara07c8312012-07-27 21:12:07 +02003985 else if (*p == '\\' && *(p + 1) != NUL)
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003986 ++p; // skip over escaped character
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003987 MB_PTR_ADV(p);
Bram Moolenaar848f8762012-07-25 17:22:23 +02003988 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003989 xp->xp_pattern = arg;
3990 }
3991 xp->xp_context = compl;
3992 }
3993 break;
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003994
Bram Moolenaar071d4272004-06-13 20:20:40 +00003995 case CMD_map: case CMD_noremap:
3996 case CMD_nmap: case CMD_nnoremap:
3997 case CMD_vmap: case CMD_vnoremap:
3998 case CMD_omap: case CMD_onoremap:
3999 case CMD_imap: case CMD_inoremap:
4000 case CMD_cmap: case CMD_cnoremap:
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004001 case CMD_lmap: case CMD_lnoremap:
Bram Moolenaar09bb33d2013-05-07 05:18:20 +02004002 case CMD_smap: case CMD_snoremap:
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +02004003 case CMD_tmap: case CMD_tnoremap:
Bram Moolenaar09bb33d2013-05-07 05:18:20 +02004004 case CMD_xmap: case CMD_xnoremap:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004005 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004006 FALSE, FALSE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004007 case CMD_unmap:
4008 case CMD_nunmap:
4009 case CMD_vunmap:
4010 case CMD_ounmap:
4011 case CMD_iunmap:
4012 case CMD_cunmap:
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004013 case CMD_lunmap:
Bram Moolenaar09bb33d2013-05-07 05:18:20 +02004014 case CMD_sunmap:
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +02004015 case CMD_tunmap:
Bram Moolenaar09bb33d2013-05-07 05:18:20 +02004016 case CMD_xunmap:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004017 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004018 FALSE, TRUE, ea.cmdidx);
Bram Moolenaarcae92dc2017-08-06 15:22:15 +02004019 case CMD_mapclear:
4020 case CMD_nmapclear:
4021 case CMD_vmapclear:
4022 case CMD_omapclear:
4023 case CMD_imapclear:
4024 case CMD_cmapclear:
4025 case CMD_lmapclear:
4026 case CMD_smapclear:
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +02004027 case CMD_tmapclear:
Bram Moolenaarcae92dc2017-08-06 15:22:15 +02004028 case CMD_xmapclear:
4029 xp->xp_context = EXPAND_MAPCLEAR;
4030 xp->xp_pattern = arg;
4031 break;
4032
Bram Moolenaar071d4272004-06-13 20:20:40 +00004033 case CMD_abbreviate: case CMD_noreabbrev:
4034 case CMD_cabbrev: case CMD_cnoreabbrev:
4035 case CMD_iabbrev: case CMD_inoreabbrev:
4036 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004037 TRUE, FALSE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004038 case CMD_unabbreviate:
4039 case CMD_cunabbrev:
4040 case CMD_iunabbrev:
4041 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004042 TRUE, TRUE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004043#ifdef FEAT_MENU
4044 case CMD_menu: case CMD_noremenu: case CMD_unmenu:
4045 case CMD_amenu: case CMD_anoremenu: case CMD_aunmenu:
4046 case CMD_nmenu: case CMD_nnoremenu: case CMD_nunmenu:
4047 case CMD_vmenu: case CMD_vnoremenu: case CMD_vunmenu:
4048 case CMD_omenu: case CMD_onoremenu: case CMD_ounmenu:
4049 case CMD_imenu: case CMD_inoremenu: case CMD_iunmenu:
4050 case CMD_cmenu: case CMD_cnoremenu: case CMD_cunmenu:
Bram Moolenaar4c5d8152018-10-19 22:36:53 +02004051 case CMD_tlmenu: case CMD_tlnoremenu: case CMD_tlunmenu:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004052 case CMD_tmenu: case CMD_tunmenu:
4053 case CMD_popup: case CMD_tearoff: case CMD_emenu:
4054 return set_context_in_menu_cmd(xp, cmd, arg, forceit);
4055#endif
4056
4057 case CMD_colorscheme:
4058 xp->xp_context = EXPAND_COLORS;
4059 xp->xp_pattern = arg;
4060 break;
4061
4062 case CMD_compiler:
4063 xp->xp_context = EXPAND_COMPILER;
4064 xp->xp_pattern = arg;
4065 break;
4066
Bram Moolenaar883f5d02010-06-21 06:24:34 +02004067 case CMD_ownsyntax:
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02004068 xp->xp_context = EXPAND_OWNSYNTAX;
4069 xp->xp_pattern = arg;
4070 break;
4071
4072 case CMD_setfiletype:
Bram Moolenaar883f5d02010-06-21 06:24:34 +02004073 xp->xp_context = EXPAND_FILETYPE;
4074 xp->xp_pattern = arg;
4075 break;
4076
Bram Moolenaar35ca0e72016-03-05 17:41:49 +01004077 case CMD_packadd:
4078 xp->xp_context = EXPAND_PACKADD;
4079 xp->xp_pattern = arg;
4080 break;
4081
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01004082#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004083 case CMD_language:
Bram Moolenaara660dc82011-05-25 12:51:22 +02004084 p = skiptowhite(arg);
4085 if (*p == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004086 {
4087 xp->xp_context = EXPAND_LANGUAGE;
4088 xp->xp_pattern = arg;
4089 }
4090 else
Bram Moolenaara660dc82011-05-25 12:51:22 +02004091 {
4092 if ( STRNCMP(arg, "messages", p - arg) == 0
4093 || STRNCMP(arg, "ctype", p - arg) == 0
4094 || STRNCMP(arg, "time", p - arg) == 0)
4095 {
4096 xp->xp_context = EXPAND_LOCALES;
4097 xp->xp_pattern = skipwhite(p);
4098 }
4099 else
4100 xp->xp_context = EXPAND_NOTHING;
4101 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004102 break;
4103#endif
Bram Moolenaarf86f26c2010-02-03 15:14:22 +01004104#if defined(FEAT_PROFILE)
4105 case CMD_profile:
4106 set_context_in_profile_cmd(xp, arg);
4107 break;
4108#endif
Bram Moolenaar42b4dda2010-03-02 15:56:05 +01004109 case CMD_behave:
4110 xp->xp_context = EXPAND_BEHAVE;
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02004111 xp->xp_pattern = arg;
Bram Moolenaar42b4dda2010-03-02 15:56:05 +01004112 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004113
Bram Moolenaar9e507ca2016-10-15 15:39:39 +02004114 case CMD_messages:
4115 xp->xp_context = EXPAND_MESSAGES;
4116 xp->xp_pattern = arg;
4117 break;
4118
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02004119#if defined(FEAT_CMDHIST)
4120 case CMD_history:
4121 xp->xp_context = EXPAND_HISTORY;
4122 xp->xp_pattern = arg;
4123 break;
4124#endif
Bram Moolenaarcd9c4622013-06-08 15:24:48 +02004125#if defined(FEAT_PROFILE)
4126 case CMD_syntime:
4127 xp->xp_context = EXPAND_SYNTIME;
4128 xp->xp_pattern = arg;
4129 break;
4130#endif
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02004131
Bram Moolenaarcd43eff2018-03-29 15:55:38 +02004132 case CMD_argdelete:
4133 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
4134 arg = xp->xp_pattern + 1;
4135 xp->xp_context = EXPAND_ARGLIST;
4136 xp->xp_pattern = arg;
4137 break;
4138
Bram Moolenaar071d4272004-06-13 20:20:40 +00004139#endif /* FEAT_CMDL_COMPL */
4140
4141 default:
4142 break;
4143 }
4144 return NULL;
4145}
4146
4147/*
Bram Moolenaaree8415b2018-08-10 23:13:12 +02004148 * Skip a range specifier of the form: addr [,addr] [;addr] ..
Bram Moolenaar071d4272004-06-13 20:20:40 +00004149 *
4150 * Backslashed delimiters after / or ? will be skipped, and commands will
4151 * not be expanded between /'s and ?'s or after "'".
4152 *
Bram Moolenaardf177f62005-02-22 08:39:57 +00004153 * Also skip white space and ":" characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004154 * Returns the "cmd" pointer advanced to beyond the range.
4155 */
4156 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004157skip_range(
4158 char_u *cmd,
4159 int *ctx) /* pointer to xp_context or NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004160{
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004161 unsigned delim;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004162
Bram Moolenaarcbf20fb2017-02-03 21:19:04 +01004163 while (vim_strchr((char_u *)" \t0123456789.$%'/?-+,;\\", *cmd) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004164 {
Bram Moolenaarcbf20fb2017-02-03 21:19:04 +01004165 if (*cmd == '\\')
4166 {
4167 if (cmd[1] == '?' || cmd[1] == '/' || cmd[1] == '&')
4168 ++cmd;
4169 else
4170 break;
4171 }
4172 else if (*cmd == '\'')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004173 {
4174 if (*++cmd == NUL && ctx != NULL)
4175 *ctx = EXPAND_NOTHING;
4176 }
4177 else if (*cmd == '/' || *cmd == '?')
4178 {
4179 delim = *cmd++;
4180 while (*cmd != NUL && *cmd != delim)
4181 if (*cmd++ == '\\' && *cmd != NUL)
4182 ++cmd;
4183 if (*cmd == NUL && ctx != NULL)
4184 *ctx = EXPAND_NOTHING;
4185 }
4186 if (*cmd != NUL)
4187 ++cmd;
4188 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00004189
4190 /* Skip ":" and white space. */
4191 while (*cmd == ':')
4192 cmd = skipwhite(cmd + 1);
4193
Bram Moolenaar071d4272004-06-13 20:20:40 +00004194 return cmd;
4195}
4196
4197/*
Bram Moolenaar198cb662018-09-06 21:44:17 +02004198 * Get a single EX address.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004199 *
4200 * Set ptr to the next character after the part that was interpreted.
4201 * Set ptr to NULL when an error is encountered.
Bram Moolenaar198cb662018-09-06 21:44:17 +02004202 * This may set the last used search pattern.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004203 *
4204 * Return MAXLNUM when no Ex address was found.
4205 */
4206 static linenr_T
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004207get_address(
4208 exarg_T *eap UNUSED,
4209 char_u **ptr,
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02004210 int addr_type, // flag: one of ADDR_LINES, ...
4211 int skip, // only skip the address, don't use it
4212 int silent, // no errors or side effects
4213 int to_other_file, // flag: may jump to other file
4214 int address_count UNUSED) // 1 for first address, >1 after comma
Bram Moolenaar071d4272004-06-13 20:20:40 +00004215{
4216 int c;
4217 int i;
4218 long n;
4219 char_u *cmd;
4220 pos_T pos;
4221 pos_T *fp;
4222 linenr_T lnum;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004223 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004224
4225 cmd = skipwhite(*ptr);
4226 lnum = MAXLNUM;
4227 do
4228 {
4229 switch (*cmd)
4230 {
4231 case '.': /* '.' - Cursor position */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004232 ++cmd;
4233 switch (addr_type)
4234 {
4235 case ADDR_LINES:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004236 lnum = curwin->w_cursor.lnum;
4237 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004238 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004239 lnum = CURRENT_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004240 break;
4241 case ADDR_ARGUMENTS:
4242 lnum = curwin->w_arg_idx + 1;
4243 break;
4244 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004245 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004246 lnum = curbuf->b_fnum;
4247 break;
4248 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004249 lnum = CURRENT_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004250 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004251 case ADDR_TABS_RELATIVE:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004252 emsg(_(e_invrange));
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004253 cmd = NULL;
4254 goto error;
4255 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004256#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004257 case ADDR_QUICKFIX:
4258 lnum = qf_get_cur_valid_idx(eap);
4259 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004260#endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004261 }
4262 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004263
4264 case '$': /* '$' - last line */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004265 ++cmd;
4266 switch (addr_type)
4267 {
4268 case ADDR_LINES:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004269 lnum = curbuf->b_ml.ml_line_count;
4270 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004271 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004272 lnum = LAST_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004273 break;
4274 case ADDR_ARGUMENTS:
4275 lnum = ARGCOUNT;
4276 break;
4277 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004278 buf = lastbuf;
4279 while (buf->b_ml.ml_mfp == NULL)
4280 {
4281 if (buf->b_prev == NULL)
4282 break;
4283 buf = buf->b_prev;
4284 }
4285 lnum = buf->b_fnum;
4286 break;
4287 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004288 lnum = lastbuf->b_fnum;
4289 break;
4290 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004291 lnum = LAST_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004292 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004293 case ADDR_TABS_RELATIVE:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004294 emsg(_(e_invrange));
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004295 cmd = NULL;
4296 goto error;
4297 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004298#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004299 case ADDR_QUICKFIX:
4300 lnum = qf_get_size(eap);
4301 if (lnum == 0)
4302 lnum = 1;
4303 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004304#endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004305 }
4306 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004307
4308 case '\'': /* ''' - mark */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004309 if (*++cmd == NUL)
4310 {
4311 cmd = NULL;
4312 goto error;
4313 }
4314 if (addr_type != ADDR_LINES)
4315 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004316 emsg(_(e_invaddr));
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004317 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004318 goto error;
4319 }
4320 if (skip)
4321 ++cmd;
4322 else
4323 {
4324 /* Only accept a mark in another file when it is
4325 * used by itself: ":'M". */
4326 fp = getmark(*cmd, to_other_file && cmd[1] == NUL);
4327 ++cmd;
4328 if (fp == (pos_T *)-1)
4329 /* Jumped to another file. */
4330 lnum = curwin->w_cursor.lnum;
4331 else
4332 {
4333 if (check_mark(fp) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004334 {
4335 cmd = NULL;
4336 goto error;
4337 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004338 lnum = fp->lnum;
4339 }
4340 }
4341 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004342
4343 case '/':
4344 case '?': /* '/' or '?' - search */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004345 c = *cmd++;
4346 if (addr_type != ADDR_LINES)
4347 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004348 emsg(_(e_invaddr));
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004349 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004350 goto error;
4351 }
4352 if (skip) /* skip "/pat/" */
4353 {
4354 cmd = skip_regexp(cmd, c, (int)p_magic, NULL);
4355 if (*cmd == c)
4356 ++cmd;
4357 }
4358 else
4359 {
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02004360 int flags;
4361
4362 pos = curwin->w_cursor; // save curwin->w_cursor
4363
4364 // When '/' or '?' follows another address, start from
4365 // there.
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004366 if (lnum != MAXLNUM)
4367 curwin->w_cursor.lnum = lnum;
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02004368
4369 // Start a forward search at the end of the line (unless
4370 // before the first line).
4371 // Start a backward search at the start of the line.
4372 // This makes sure we never match in the current
4373 // line, and can match anywhere in the
4374 // next/previous line.
Bram Moolenaar8ada6aa2017-12-19 21:23:21 +01004375 if (c == '/' && curwin->w_cursor.lnum > 0)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004376 curwin->w_cursor.col = MAXCOL;
4377 else
4378 curwin->w_cursor.col = 0;
4379 searchcmdlen = 0;
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02004380 flags = silent ? 0 : SEARCH_HIS | SEARCH_MSG;
4381 if (!do_search(NULL, c, cmd, 1L, flags, NULL, NULL))
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004382 {
4383 curwin->w_cursor = pos;
4384 cmd = NULL;
4385 goto error;
4386 }
4387 lnum = curwin->w_cursor.lnum;
4388 curwin->w_cursor = pos;
4389 /* adjust command string pointer */
4390 cmd += searchcmdlen;
4391 }
4392 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004393
4394 case '\\': /* "\?", "\/" or "\&", repeat search */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004395 ++cmd;
4396 if (addr_type != ADDR_LINES)
4397 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004398 emsg(_(e_invaddr));
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004399 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004400 goto error;
4401 }
4402 if (*cmd == '&')
4403 i = RE_SUBST;
4404 else if (*cmd == '?' || *cmd == '/')
4405 i = RE_SEARCH;
4406 else
4407 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004408 emsg(_(e_backslash));
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004409 cmd = NULL;
4410 goto error;
4411 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004412
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004413 if (!skip)
4414 {
4415 /*
4416 * When search follows another address, start from
4417 * there.
4418 */
4419 if (lnum != MAXLNUM)
4420 pos.lnum = lnum;
4421 else
4422 pos.lnum = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004423
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004424 /*
4425 * Start the search just like for the above
4426 * do_search().
4427 */
4428 if (*cmd != '?')
4429 pos.col = MAXCOL;
4430 else
4431 pos.col = 0;
Bram Moolenaarbd8539a2015-08-11 18:53:03 +02004432 pos.coladd = 0;
Bram Moolenaar5d24a222018-12-23 19:10:09 +01004433 if (searchit(curwin, curbuf, &pos, NULL,
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004434 *cmd == '?' ? BACKWARD : FORWARD,
4435 (char_u *)"", 1L, SEARCH_MSG,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02004436 i, (linenr_T)0, NULL, NULL) != FAIL)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004437 lnum = pos.lnum;
4438 else
4439 {
4440 cmd = NULL;
4441 goto error;
4442 }
4443 }
4444 ++cmd;
4445 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004446
4447 default:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004448 if (VIM_ISDIGIT(*cmd)) /* absolute line number */
4449 lnum = getdigits(&cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004450 }
4451
4452 for (;;)
4453 {
4454 cmd = skipwhite(cmd);
4455 if (*cmd != '-' && *cmd != '+' && !VIM_ISDIGIT(*cmd))
4456 break;
4457
4458 if (lnum == MAXLNUM)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004459 {
4460 switch (addr_type)
4461 {
4462 case ADDR_LINES:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004463 /* "+1" is same as ".+1" */
4464 lnum = curwin->w_cursor.lnum;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004465 break;
4466 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004467 lnum = CURRENT_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004468 break;
4469 case ADDR_ARGUMENTS:
4470 lnum = curwin->w_arg_idx + 1;
4471 break;
4472 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004473 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004474 lnum = curbuf->b_fnum;
4475 break;
4476 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004477 lnum = CURRENT_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004478 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004479 case ADDR_TABS_RELATIVE:
4480 lnum = 1;
4481 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004482#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004483 case ADDR_QUICKFIX:
4484 lnum = qf_get_cur_valid_idx(eap);
4485 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004486#endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004487 }
4488 }
4489
Bram Moolenaar071d4272004-06-13 20:20:40 +00004490 if (VIM_ISDIGIT(*cmd))
4491 i = '+'; /* "number" is same as "+number" */
4492 else
4493 i = *cmd++;
4494 if (!VIM_ISDIGIT(*cmd)) /* '+' is '+1', but '+0' is not '+1' */
4495 n = 1;
4496 else
4497 n = getdigits(&cmd);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004498
4499 if (addr_type == ADDR_TABS_RELATIVE)
4500 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004501 emsg(_(e_invrange));
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004502 cmd = NULL;
4503 goto error;
4504 }
4505 else if (addr_type == ADDR_LOADED_BUFFERS
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004506 || addr_type == ADDR_BUFFERS)
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004507 lnum = compute_buffer_local_count(
4508 addr_type, lnum, (i == '-') ? -1 * n : n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004509 else
Bram Moolenaarded27822017-01-02 14:27:34 +01004510 {
4511#ifdef FEAT_FOLDING
4512 /* Relative line addressing, need to adjust for folded lines
4513 * now, but only do it after the first address. */
4514 if (addr_type == ADDR_LINES && (i == '-' || i == '+')
4515 && address_count >= 2)
4516 (void)hasFolding(lnum, NULL, &lnum);
4517#endif
4518 if (i == '-')
4519 lnum -= n;
4520 else
4521 lnum += n;
4522 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004523 }
4524 } while (*cmd == '/' || *cmd == '?');
4525
4526error:
4527 *ptr = cmd;
4528 return lnum;
4529}
4530
4531/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00004532 * Get flags from an Ex command argument.
4533 */
4534 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004535get_flags(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00004536{
4537 while (vim_strchr((char_u *)"lp#", *eap->arg) != NULL)
4538 {
4539 if (*eap->arg == 'l')
4540 eap->flags |= EXFLAG_LIST;
4541 else if (*eap->arg == 'p')
4542 eap->flags |= EXFLAG_PRINT;
4543 else
4544 eap->flags |= EXFLAG_NR;
4545 eap->arg = skipwhite(eap->arg + 1);
4546 }
4547}
4548
4549/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004550 * Function called for command which is Not Implemented. NI!
4551 */
4552 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004553ex_ni(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004554{
4555 if (!eap->skip)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004556 eap->errmsg = N_("E319: Sorry, the command is not available in this version");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004557}
4558
Bram Moolenaar7bb75552007-07-16 18:39:49 +00004559#ifdef HAVE_EX_SCRIPT_NI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004560/*
4561 * Function called for script command which is Not Implemented. NI!
4562 * Skips over ":perl <<EOF" constructs.
4563 */
4564 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004565ex_script_ni(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004566{
4567 if (!eap->skip)
4568 ex_ni(eap);
4569 else
4570 vim_free(script_get(eap, eap->arg));
4571}
4572#endif
4573
4574/*
4575 * Check range in Ex command for validity.
4576 * Return NULL when valid, error message when invalid.
4577 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004578 static char *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004579invalid_range(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004580{
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004581 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004582 if ( eap->line1 < 0
4583 || eap->line2 < 0
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004584 || eap->line1 > eap->line2)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004585 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004586
4587 if (eap->argt & RANGE)
4588 {
4589 switch(eap->addr_type)
4590 {
4591 case ADDR_LINES:
4592 if (!(eap->argt & NOTADR)
4593 && eap->line2 > curbuf->b_ml.ml_line_count
4594#ifdef FEAT_DIFF
4595 + (eap->cmdidx == CMD_diffget)
4596#endif
4597 )
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004598 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004599 break;
4600 case ADDR_ARGUMENTS:
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004601 /* add 1 if ARGCOUNT is 0 */
4602 if (eap->line2 > ARGCOUNT + (!ARGCOUNT))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004603 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004604 break;
4605 case ADDR_BUFFERS:
4606 if (eap->line1 < firstbuf->b_fnum
4607 || eap->line2 > lastbuf->b_fnum)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004608 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004609 break;
4610 case ADDR_LOADED_BUFFERS:
4611 buf = firstbuf;
4612 while (buf->b_ml.ml_mfp == NULL)
4613 {
4614 if (buf->b_next == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004615 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004616 buf = buf->b_next;
4617 }
4618 if (eap->line1 < buf->b_fnum)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004619 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004620 buf = lastbuf;
4621 while (buf->b_ml.ml_mfp == NULL)
4622 {
4623 if (buf->b_prev == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004624 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004625 buf = buf->b_prev;
4626 }
4627 if (eap->line2 > buf->b_fnum)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004628 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004629 break;
4630 case ADDR_WINDOWS:
Bram Moolenaar8be63882015-01-14 11:25:05 +01004631 if (eap->line2 > LAST_WIN_NR)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004632 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004633 break;
4634 case ADDR_TABS:
4635 if (eap->line2 > LAST_TAB_NR)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004636 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004637 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004638 case ADDR_TABS_RELATIVE:
4639 /* Do nothing */
4640 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004641#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004642 case ADDR_QUICKFIX:
4643 if (eap->line2 != 1 && eap->line2 > qf_get_size(eap))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004644 return _(e_invrange);
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004645 break;
Bram Moolenaare906c502015-09-09 21:10:39 +02004646#endif
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004647 }
4648 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004649 return NULL;
4650}
4651
4652/*
4653 * Correct the range for zero line number, if required.
4654 */
4655 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004656correct_range(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004657{
4658 if (!(eap->argt & ZEROR)) /* zero in range not allowed */
4659 {
4660 if (eap->line1 == 0)
4661 eap->line1 = 1;
4662 if (eap->line2 == 0)
4663 eap->line2 = 1;
4664 }
4665}
4666
Bram Moolenaar748bf032005-02-02 23:04:36 +00004667#ifdef FEAT_QUICKFIX
Bram Moolenaar748bf032005-02-02 23:04:36 +00004668/*
4669 * For a ":vimgrep" or ":vimgrepadd" command return a pointer past the
4670 * pattern. Otherwise return eap->arg.
4671 */
4672 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004673skip_grep_pat(exarg_T *eap)
Bram Moolenaar748bf032005-02-02 23:04:36 +00004674{
4675 char_u *p = eap->arg;
4676
Bram Moolenaara37420f2006-02-04 22:37:47 +00004677 if (*p != NUL && (eap->cmdidx == CMD_vimgrep || eap->cmdidx == CMD_lvimgrep
4678 || eap->cmdidx == CMD_vimgrepadd
4679 || eap->cmdidx == CMD_lvimgrepadd
4680 || grep_internal(eap->cmdidx)))
Bram Moolenaar748bf032005-02-02 23:04:36 +00004681 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00004682 p = skip_vimgrep_pat(p, NULL, NULL);
Bram Moolenaar748bf032005-02-02 23:04:36 +00004683 if (p == NULL)
4684 p = eap->arg;
Bram Moolenaar748bf032005-02-02 23:04:36 +00004685 }
4686 return p;
4687}
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004688
4689/*
4690 * For the ":make" and ":grep" commands insert the 'makeprg'/'grepprg' option
4691 * in the command line, so that things like % get expanded.
4692 */
4693 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004694replace_makeprg(exarg_T *eap, char_u *p, char_u **cmdlinep)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004695{
4696 char_u *new_cmdline;
4697 char_u *program;
4698 char_u *pos;
4699 char_u *ptr;
4700 int len;
4701 int i;
4702
4703 /*
4704 * Don't do it when ":vimgrep" is used for ":grep".
4705 */
Bram Moolenaara37420f2006-02-04 22:37:47 +00004706 if ((eap->cmdidx == CMD_make || eap->cmdidx == CMD_lmake
4707 || eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4708 || eap->cmdidx == CMD_grepadd
4709 || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004710 && !grep_internal(eap->cmdidx))
4711 {
Bram Moolenaara37420f2006-02-04 22:37:47 +00004712 if (eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4713 || eap->cmdidx == CMD_grepadd || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004714 {
4715 if (*curbuf->b_p_gp == NUL)
4716 program = p_gp;
4717 else
4718 program = curbuf->b_p_gp;
4719 }
4720 else
4721 {
4722 if (*curbuf->b_p_mp == NUL)
4723 program = p_mp;
4724 else
4725 program = curbuf->b_p_mp;
4726 }
4727
4728 p = skipwhite(p);
4729
4730 if ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4731 {
4732 /* replace $* by given arguments */
4733 i = 1;
4734 while ((pos = (char_u *)strstr((char *)pos + 2, "$*")) != NULL)
4735 ++i;
4736 len = (int)STRLEN(p);
4737 new_cmdline = alloc((int)(STRLEN(program) + i * (len - 2) + 1));
4738 if (new_cmdline == NULL)
4739 return NULL; /* out of memory */
4740 ptr = new_cmdline;
4741 while ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4742 {
4743 i = (int)(pos - program);
4744 STRNCPY(ptr, program, i);
4745 STRCPY(ptr += i, p);
4746 ptr += len;
4747 program = pos + 2;
4748 }
4749 STRCPY(ptr, program);
4750 }
4751 else
4752 {
4753 new_cmdline = alloc((int)(STRLEN(program) + STRLEN(p) + 2));
4754 if (new_cmdline == NULL)
4755 return NULL; /* out of memory */
4756 STRCPY(new_cmdline, program);
4757 STRCAT(new_cmdline, " ");
4758 STRCAT(new_cmdline, p);
4759 }
4760 msg_make(p);
4761
4762 /* 'eap->cmd' is not set here, because it is not used at CMD_make */
4763 vim_free(*cmdlinep);
4764 *cmdlinep = new_cmdline;
4765 p = new_cmdline;
4766 }
4767 return p;
4768}
Bram Moolenaar748bf032005-02-02 23:04:36 +00004769#endif
4770
Bram Moolenaar071d4272004-06-13 20:20:40 +00004771/*
4772 * Expand file name in Ex command argument.
4773 * Return FAIL for failure, OK otherwise.
4774 */
4775 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004776expand_filename(
4777 exarg_T *eap,
4778 char_u **cmdlinep,
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004779 char **errormsgp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004780{
4781 int has_wildcards; /* need to expand wildcards */
4782 char_u *repl;
4783 int srclen;
4784 char_u *p;
4785 int n;
Bram Moolenaar63b92542007-03-27 14:57:09 +00004786 int escaped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004787
Bram Moolenaar748bf032005-02-02 23:04:36 +00004788#ifdef FEAT_QUICKFIX
4789 /* Skip a regexp pattern for ":vimgrep[add] pat file..." */
4790 p = skip_grep_pat(eap);
4791#else
4792 p = eap->arg;
4793#endif
4794
Bram Moolenaar071d4272004-06-13 20:20:40 +00004795 /*
4796 * Decide to expand wildcards *before* replacing '%', '#', etc. If
4797 * the file name contains a wildcard it should not cause expanding.
4798 * (it will be expanded anyway if there is a wildcard before replacing).
4799 */
Bram Moolenaar748bf032005-02-02 23:04:36 +00004800 has_wildcards = mch_has_wildcard(p);
4801 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004802 {
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004803#ifdef FEAT_EVAL
4804 /* Skip over `=expr`, wildcards in it are not expanded. */
4805 if (p[0] == '`' && p[1] == '=')
4806 {
4807 p += 2;
4808 (void)skip_expr(&p);
4809 if (*p == '`')
4810 ++p;
4811 continue;
4812 }
4813#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004814 /*
4815 * Quick check if this cannot be the start of a special string.
4816 * Also removes backslash before '%', '#' and '<'.
4817 */
4818 if (vim_strchr((char_u *)"%#<", *p) == NULL)
4819 {
4820 ++p;
4821 continue;
4822 }
4823
4824 /*
4825 * Try to find a match at this position.
4826 */
Bram Moolenaar63b92542007-03-27 14:57:09 +00004827 repl = eval_vars(p, eap->arg, &srclen, &(eap->do_ecmd_lnum),
4828 errormsgp, &escaped);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004829 if (*errormsgp != NULL) /* error detected */
4830 return FAIL;
4831 if (repl == NULL) /* no match found */
4832 {
4833 p += srclen;
4834 continue;
4835 }
4836
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00004837 /* Wildcards won't be expanded below, the replacement is taken
4838 * literally. But do expand "~/file", "~user/file" and "$HOME/file". */
4839 if (vim_strchr(repl, '$') != NULL || vim_strchr(repl, '~') != NULL)
4840 {
4841 char_u *l = repl;
4842
4843 repl = expand_env_save(repl);
4844 vim_free(l);
4845 }
4846
Bram Moolenaar63b92542007-03-27 14:57:09 +00004847 /* Need to escape white space et al. with a backslash.
4848 * Don't do this for:
4849 * - replacement that already has been escaped: "##"
4850 * - shell commands (may have to use quotes instead).
4851 * - non-unix systems when there is a single argument (spaces don't
4852 * separate arguments then).
4853 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004854 if (!eap->usefilter
Bram Moolenaar63b92542007-03-27 14:57:09 +00004855 && !escaped
Bram Moolenaar071d4272004-06-13 20:20:40 +00004856 && eap->cmdidx != CMD_bang
Bram Moolenaar071d4272004-06-13 20:20:40 +00004857 && eap->cmdidx != CMD_grep
4858 && eap->cmdidx != CMD_grepadd
Bram Moolenaarbf15b8d2017-06-04 20:43:48 +02004859 && eap->cmdidx != CMD_hardcopy
Bram Moolenaar67883b42017-07-27 22:57:00 +02004860 && eap->cmdidx != CMD_lgrep
4861 && eap->cmdidx != CMD_lgrepadd
4862 && eap->cmdidx != CMD_lmake
4863 && eap->cmdidx != CMD_make
4864 && eap->cmdidx != CMD_terminal
Bram Moolenaar071d4272004-06-13 20:20:40 +00004865#ifndef UNIX
4866 && !(eap->argt & NOSPC)
4867#endif
4868 )
4869 {
4870 char_u *l;
4871#ifdef BACKSLASH_IN_FILENAME
4872 /* Don't escape a backslash here, because rem_backslash() doesn't
4873 * remove it later. */
4874 static char_u *nobslash = (char_u *)" \t\"|";
4875# define ESCAPE_CHARS nobslash
4876#else
4877# define ESCAPE_CHARS escape_chars
4878#endif
4879
4880 for (l = repl; *l; ++l)
4881 if (vim_strchr(ESCAPE_CHARS, *l) != NULL)
4882 {
4883 l = vim_strsave_escaped(repl, ESCAPE_CHARS);
4884 if (l != NULL)
4885 {
4886 vim_free(repl);
4887 repl = l;
4888 }
4889 break;
4890 }
4891 }
4892
4893 /* For a shell command a '!' must be escaped. */
Bram Moolenaar67883b42017-07-27 22:57:00 +02004894 if ((eap->usefilter || eap->cmdidx == CMD_bang
4895 || eap->cmdidx == CMD_terminal)
Bram Moolenaar31b7d382014-04-01 18:54:48 +02004896 && vim_strpbrk(repl, (char_u *)"!") != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004897 {
4898 char_u *l;
4899
Bram Moolenaar31b7d382014-04-01 18:54:48 +02004900 l = vim_strsave_escaped(repl, (char_u *)"!");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004901 if (l != NULL)
4902 {
4903 vim_free(repl);
4904 repl = l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004905 }
4906 }
4907
4908 p = repl_cmdline(eap, p, srclen, repl, cmdlinep);
4909 vim_free(repl);
4910 if (p == NULL)
4911 return FAIL;
4912 }
4913
4914 /*
4915 * One file argument: Expand wildcards.
4916 * Don't do this with ":r !command" or ":w !command".
4917 */
4918 if ((eap->argt & NOSPC) && !eap->usefilter)
4919 {
4920 /*
4921 * May do this twice:
4922 * 1. Replace environment variables.
4923 * 2. Replace any other wildcards, remove backslashes.
4924 */
4925 for (n = 1; n <= 2; ++n)
4926 {
4927 if (n == 2)
4928 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004929 /*
4930 * Halve the number of backslashes (this is Vi compatible).
4931 * For Unix and OS/2, when wildcards are expanded, this is
4932 * done by ExpandOne() below.
4933 */
Bram Moolenaare7fedb62015-12-31 19:07:19 +01004934#if defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004935 if (!has_wildcards)
4936#endif
4937 backslash_halve(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004938 }
4939
4940 if (has_wildcards)
4941 {
4942 if (n == 1)
4943 {
4944 /*
4945 * First loop: May expand environment variables. This
4946 * can be done much faster with expand_env() than with
4947 * something else (e.g., calling a shell).
4948 * After expanding environment variables, check again
4949 * if there are still wildcards present.
4950 */
4951 if (vim_strchr(eap->arg, '$') != NULL
4952 || vim_strchr(eap->arg, '~') != NULL)
4953 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +00004954 expand_env_esc(eap->arg, NameBuff, MAXPATHL,
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00004955 TRUE, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004956 has_wildcards = mch_has_wildcard(NameBuff);
4957 p = NameBuff;
4958 }
4959 else
4960 p = NULL;
4961 }
4962 else /* n == 2 */
4963 {
4964 expand_T xpc;
Bram Moolenaar94950a92010-12-02 16:01:29 +01004965 int options = WILD_LIST_NOTFOUND|WILD_ADD_SLASH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004966
4967 ExpandInit(&xpc);
4968 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar94950a92010-12-02 16:01:29 +01004969 if (p_wic)
4970 options += WILD_ICASE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004971 p = ExpandOne(&xpc, eap->arg, NULL,
Bram Moolenaar94950a92010-12-02 16:01:29 +01004972 options, WILD_EXPAND_FREE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004973 if (p == NULL)
4974 return FAIL;
4975 }
4976 if (p != NULL)
4977 {
4978 (void)repl_cmdline(eap, eap->arg, (int)STRLEN(eap->arg),
4979 p, cmdlinep);
4980 if (n == 2) /* p came from ExpandOne() */
4981 vim_free(p);
4982 }
4983 }
4984 }
4985 }
4986 return OK;
4987}
4988
4989/*
4990 * Replace part of the command line, keeping eap->cmd, eap->arg and
4991 * eap->nextcmd correct.
4992 * "src" points to the part that is to be replaced, of length "srclen".
4993 * "repl" is the replacement string.
4994 * Returns a pointer to the character after the replaced string.
4995 * Returns NULL for failure.
4996 */
4997 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004998repl_cmdline(
4999 exarg_T *eap,
5000 char_u *src,
5001 int srclen,
5002 char_u *repl,
5003 char_u **cmdlinep)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005004{
5005 int len;
5006 int i;
5007 char_u *new_cmdline;
5008
5009 /*
5010 * The new command line is build in new_cmdline[].
5011 * First allocate it.
5012 * Careful: a "+cmd" argument may have been NUL terminated.
5013 */
5014 len = (int)STRLEN(repl);
5015 i = (int)(src - *cmdlinep) + (int)STRLEN(src + srclen) + len + 3;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005016 if (eap->nextcmd != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005017 i += (int)STRLEN(eap->nextcmd);/* add space for next command */
5018 if ((new_cmdline = alloc((unsigned)i)) == NULL)
5019 return NULL; /* out of memory! */
5020
5021 /*
5022 * Copy the stuff before the expanded part.
5023 * Copy the expanded stuff.
5024 * Copy what came after the expanded part.
5025 * Copy the next commands, if there are any.
5026 */
5027 i = (int)(src - *cmdlinep); /* length of part before match */
5028 mch_memmove(new_cmdline, *cmdlinep, (size_t)i);
Bram Moolenaara3ffd9c2005-07-21 21:03:15 +00005029
Bram Moolenaar071d4272004-06-13 20:20:40 +00005030 mch_memmove(new_cmdline + i, repl, (size_t)len);
5031 i += len; /* remember the end of the string */
5032 STRCPY(new_cmdline + i, src + srclen);
5033 src = new_cmdline + i; /* remember where to continue */
5034
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005035 if (eap->nextcmd != NULL) /* append next command */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005036 {
5037 i = (int)STRLEN(new_cmdline) + 1;
5038 STRCPY(new_cmdline + i, eap->nextcmd);
5039 eap->nextcmd = new_cmdline + i;
5040 }
5041 eap->cmd = new_cmdline + (eap->cmd - *cmdlinep);
5042 eap->arg = new_cmdline + (eap->arg - *cmdlinep);
5043 if (eap->do_ecmd_cmd != NULL && eap->do_ecmd_cmd != dollar_command)
5044 eap->do_ecmd_cmd = new_cmdline + (eap->do_ecmd_cmd - *cmdlinep);
5045 vim_free(*cmdlinep);
5046 *cmdlinep = new_cmdline;
5047
5048 return src;
5049}
5050
5051/*
5052 * Check for '|' to separate commands and '"' to start comments.
5053 */
5054 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005055separate_nextcmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005056{
5057 char_u *p;
5058
Bram Moolenaar86b68352004-12-27 21:59:20 +00005059#ifdef FEAT_QUICKFIX
Bram Moolenaar748bf032005-02-02 23:04:36 +00005060 p = skip_grep_pat(eap);
5061#else
5062 p = eap->arg;
Bram Moolenaar86b68352004-12-27 21:59:20 +00005063#endif
5064
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005065 for ( ; *p; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005066 {
5067 if (*p == Ctrl_V)
5068 {
5069 if (eap->argt & (USECTRLV | XFILE))
5070 ++p; /* skip CTRL-V and next char */
5071 else
Bram Moolenaarb0db5692007-08-14 20:54:49 +00005072 /* remove CTRL-V and skip next char */
Bram Moolenaara7241f52008-06-24 20:39:31 +00005073 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005074 if (*p == NUL) /* stop at NUL after CTRL-V */
5075 break;
5076 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005077
5078#ifdef FEAT_EVAL
5079 /* Skip over `=expr` when wildcards are expanded. */
Bram Moolenaardf177f62005-02-22 08:39:57 +00005080 else if (p[0] == '`' && p[1] == '=' && (eap->argt & XFILE))
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005081 {
5082 p += 2;
5083 (void)skip_expr(&p);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005084 }
5085#endif
5086
Bram Moolenaar071d4272004-06-13 20:20:40 +00005087 /* Check for '"': start of comment or '|': next command */
5088 /* :@" and :*" do not start a comment!
5089 * :redir @" doesn't either. */
5090 else if ((*p == '"' && !(eap->argt & NOTRLCOM)
5091 && ((eap->cmdidx != CMD_at && eap->cmdidx != CMD_star)
5092 || p != eap->arg)
5093 && (eap->cmdidx != CMD_redir
5094 || p != eap->arg + 1 || p[-1] != '@'))
5095 || *p == '|' || *p == '\n')
5096 {
5097 /*
5098 * We remove the '\' before the '|', unless USECTRLV is used
5099 * AND 'b' is present in 'cpoptions'.
5100 */
5101 if ((vim_strchr(p_cpo, CPO_BAR) == NULL
5102 || !(eap->argt & USECTRLV)) && *(p - 1) == '\\')
5103 {
Bram Moolenaara7241f52008-06-24 20:39:31 +00005104 STRMOVE(p - 1, p); /* remove the '\' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005105 --p;
5106 }
5107 else
5108 {
5109 eap->nextcmd = check_nextcmd(p);
5110 *p = NUL;
5111 break;
5112 }
5113 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005114 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00005115
Bram Moolenaar071d4272004-06-13 20:20:40 +00005116 if (!(eap->argt & NOTRLCOM)) /* remove trailing spaces */
5117 del_trailing_spaces(eap->arg);
5118}
5119
5120/*
5121 * get + command from ex argument
5122 */
5123 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005124getargcmd(char_u **argp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005125{
5126 char_u *arg = *argp;
5127 char_u *command = NULL;
5128
5129 if (*arg == '+') /* +[command] */
5130 {
5131 ++arg;
Bram Moolenaar3e451592014-04-02 14:22:05 +02005132 if (vim_isspace(*arg) || *arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005133 command = dollar_command;
5134 else
5135 {
5136 command = arg;
5137 arg = skip_cmd_arg(command, TRUE);
5138 if (*arg != NUL)
5139 *arg++ = NUL; /* terminate command with NUL */
5140 }
5141
5142 arg = skipwhite(arg); /* skip over spaces */
5143 *argp = arg;
5144 }
5145 return command;
5146}
5147
5148/*
5149 * Find end of "+command" argument. Skip over "\ " and "\\".
5150 */
5151 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005152skip_cmd_arg(
5153 char_u *p,
5154 int rembs) /* TRUE to halve the number of backslashes */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005155{
5156 while (*p && !vim_isspace(*p))
5157 {
5158 if (*p == '\\' && p[1] != NUL)
5159 {
5160 if (rembs)
Bram Moolenaara7241f52008-06-24 20:39:31 +00005161 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005162 else
5163 ++p;
5164 }
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005165 MB_PTR_ADV(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005166 }
5167 return p;
5168}
5169
Bram Moolenaar333b80a2018-04-04 22:57:29 +02005170 int
5171get_bad_opt(char_u *p, exarg_T *eap)
5172{
5173 if (STRICMP(p, "keep") == 0)
5174 eap->bad_char = BAD_KEEP;
5175 else if (STRICMP(p, "drop") == 0)
5176 eap->bad_char = BAD_DROP;
5177 else if (MB_BYTE2LEN(*p) == 1 && p[1] == NUL)
5178 eap->bad_char = *p;
Bram Moolenaar75808492018-06-12 12:39:41 +02005179 else
5180 return FAIL;
5181 return OK;
Bram Moolenaar333b80a2018-04-04 22:57:29 +02005182}
5183
Bram Moolenaar071d4272004-06-13 20:20:40 +00005184/*
5185 * Get "++opt=arg" argument.
5186 * Return FAIL or OK.
5187 */
5188 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005189getargopt(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005190{
5191 char_u *arg = eap->arg + 2;
5192 int *pp = NULL;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02005193 int bad_char_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005194 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005195
5196 /* ":edit ++[no]bin[ary] file" */
5197 if (STRNCMP(arg, "bin", 3) == 0 || STRNCMP(arg, "nobin", 5) == 0)
5198 {
5199 if (*arg == 'n')
5200 {
5201 arg += 2;
5202 eap->force_bin = FORCE_NOBIN;
5203 }
5204 else
5205 eap->force_bin = FORCE_BIN;
5206 if (!checkforcmd(&arg, "binary", 3))
5207 return FAIL;
5208 eap->arg = skipwhite(arg);
5209 return OK;
5210 }
5211
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005212 /* ":read ++edit file" */
5213 if (STRNCMP(arg, "edit", 4) == 0)
5214 {
5215 eap->read_edit = TRUE;
5216 eap->arg = skipwhite(arg + 4);
5217 return OK;
5218 }
5219
Bram Moolenaar071d4272004-06-13 20:20:40 +00005220 if (STRNCMP(arg, "ff", 2) == 0)
5221 {
5222 arg += 2;
5223 pp = &eap->force_ff;
5224 }
5225 else if (STRNCMP(arg, "fileformat", 10) == 0)
5226 {
5227 arg += 10;
5228 pp = &eap->force_ff;
5229 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005230 else if (STRNCMP(arg, "enc", 3) == 0)
5231 {
Bram Moolenaarb38e9ab2011-12-14 14:49:45 +01005232 if (STRNCMP(arg, "encoding", 8) == 0)
5233 arg += 8;
5234 else
5235 arg += 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005236 pp = &eap->force_enc;
5237 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005238 else if (STRNCMP(arg, "bad", 3) == 0)
5239 {
5240 arg += 3;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02005241 pp = &bad_char_idx;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005242 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005243
5244 if (pp == NULL || *arg != '=')
5245 return FAIL;
5246
5247 ++arg;
5248 *pp = (int)(arg - eap->cmd);
5249 arg = skip_cmd_arg(arg, FALSE);
5250 eap->arg = skipwhite(arg);
5251 *arg = NUL;
5252
Bram Moolenaar071d4272004-06-13 20:20:40 +00005253 if (pp == &eap->force_ff)
5254 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005255 if (check_ff_value(eap->cmd + eap->force_ff) == FAIL)
5256 return FAIL;
Bram Moolenaar333b80a2018-04-04 22:57:29 +02005257 eap->force_ff = eap->cmd[eap->force_ff];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005258 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005259 else if (pp == &eap->force_enc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005260 {
5261 /* Make 'fileencoding' lower case. */
5262 for (p = eap->cmd + eap->force_enc; *p != NUL; ++p)
5263 *p = TOLOWER_ASC(*p);
5264 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005265 else
5266 {
5267 /* Check ++bad= argument. Must be a single-byte character, "keep" or
5268 * "drop". */
Bram Moolenaar333b80a2018-04-04 22:57:29 +02005269 if (get_bad_opt(eap->cmd + bad_char_idx, eap) == FAIL)
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005270 return FAIL;
5271 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005272
5273 return OK;
5274}
5275
5276/*
5277 * ":abbreviate" and friends.
5278 */
5279 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005280ex_abbreviate(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005281{
5282 do_exmap(eap, TRUE); /* almost the same as mapping */
5283}
5284
5285/*
5286 * ":map" and friends.
5287 */
5288 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005289ex_map(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005290{
5291 /*
5292 * If we are sourcing .exrc or .vimrc in current directory we
5293 * print the mappings for security reasons.
5294 */
5295 if (secure)
5296 {
5297 secure = 2;
5298 msg_outtrans(eap->cmd);
5299 msg_putchar('\n');
5300 }
5301 do_exmap(eap, FALSE);
5302}
5303
5304/*
5305 * ":unmap" and friends.
5306 */
5307 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005308ex_unmap(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005309{
5310 do_exmap(eap, FALSE);
5311}
5312
5313/*
5314 * ":mapclear" and friends.
5315 */
5316 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005317ex_mapclear(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005318{
5319 map_clear(eap->cmd, eap->arg, eap->forceit, FALSE);
5320}
5321
5322/*
5323 * ":abclear" and friends.
5324 */
5325 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005326ex_abclear(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005327{
5328 map_clear(eap->cmd, eap->arg, TRUE, TRUE);
5329}
5330
Bram Moolenaar071d4272004-06-13 20:20:40 +00005331 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005332ex_autocmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005333{
5334 /*
Bram Moolenaar26d4b892018-07-04 22:26:28 +02005335 * Disallow autocommands from .exrc and .vimrc in current
Bram Moolenaar071d4272004-06-13 20:20:40 +00005336 * directory for security reasons.
5337 */
5338 if (secure)
5339 {
5340 secure = 2;
5341 eap->errmsg = e_curdir;
5342 }
5343 else if (eap->cmdidx == CMD_autocmd)
5344 do_autocmd(eap->arg, eap->forceit);
5345 else
5346 do_augroup(eap->arg, eap->forceit);
5347}
5348
5349/*
5350 * ":doautocmd": Apply the automatic commands to the current buffer.
5351 */
5352 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005353ex_doautocmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005354{
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005355 char_u *arg = eap->arg;
5356 int call_do_modelines = check_nomodeline(&arg);
Bram Moolenaar1610d052016-06-09 22:53:01 +02005357 int did_aucmd;
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005358
Bram Moolenaar1610d052016-06-09 22:53:01 +02005359 (void)do_doautocmd(arg, TRUE, &did_aucmd);
5360 /* Only when there is no <nomodeline>. */
5361 if (call_do_modelines && did_aucmd)
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005362 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005363}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005364
Bram Moolenaar071d4272004-06-13 20:20:40 +00005365/*
5366 * :[N]bunload[!] [N] [bufname] unload buffer
5367 * :[N]bdelete[!] [N] [bufname] delete buffer from buffer list
5368 * :[N]bwipeout[!] [N] [bufname] delete buffer really
5369 */
5370 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005371ex_bunload(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005372{
5373 eap->errmsg = do_bufdel(
5374 eap->cmdidx == CMD_bdelete ? DOBUF_DEL
5375 : eap->cmdidx == CMD_bwipeout ? DOBUF_WIPE
5376 : DOBUF_UNLOAD, eap->arg,
5377 eap->addr_count, (int)eap->line1, (int)eap->line2, eap->forceit);
5378}
5379
5380/*
5381 * :[N]buffer [N] to buffer N
5382 * :[N]sbuffer [N] to buffer N
5383 */
5384 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005385ex_buffer(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005386{
5387 if (*eap->arg)
5388 eap->errmsg = e_trailing;
5389 else
5390 {
5391 if (eap->addr_count == 0) /* default is current buffer */
5392 goto_buffer(eap, DOBUF_CURRENT, FORWARD, 0);
5393 else
5394 goto_buffer(eap, DOBUF_FIRST, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005395 if (eap->do_ecmd_cmd != NULL)
5396 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005397 }
5398}
5399
5400/*
5401 * :[N]bmodified [N] to next mod. buffer
5402 * :[N]sbmodified [N] to next mod. buffer
5403 */
5404 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005405ex_bmodified(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005406{
5407 goto_buffer(eap, DOBUF_MOD, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005408 if (eap->do_ecmd_cmd != NULL)
5409 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005410}
5411
5412/*
5413 * :[N]bnext [N] to next buffer
5414 * :[N]sbnext [N] split and to next buffer
5415 */
5416 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005417ex_bnext(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005418{
5419 goto_buffer(eap, DOBUF_CURRENT, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005420 if (eap->do_ecmd_cmd != NULL)
5421 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005422}
5423
5424/*
5425 * :[N]bNext [N] to previous buffer
5426 * :[N]bprevious [N] to previous buffer
5427 * :[N]sbNext [N] split and to previous buffer
5428 * :[N]sbprevious [N] split and to previous buffer
5429 */
5430 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005431ex_bprevious(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005432{
5433 goto_buffer(eap, DOBUF_CURRENT, BACKWARD, (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 * :brewind to first buffer
5440 * :bfirst to first buffer
5441 * :sbrewind split and to first buffer
5442 * :sbfirst split and to first buffer
5443 */
5444 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005445ex_brewind(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005446{
5447 goto_buffer(eap, DOBUF_FIRST, FORWARD, 0);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005448 if (eap->do_ecmd_cmd != NULL)
5449 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005450}
5451
5452/*
5453 * :blast to last buffer
5454 * :sblast split and to last buffer
5455 */
5456 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005457ex_blast(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005458{
5459 goto_buffer(eap, DOBUF_LAST, BACKWARD, 0);
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}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005463
5464 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005465ends_excmd(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005466{
5467 return (c == NUL || c == '|' || c == '"' || c == '\n');
5468}
5469
5470#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA) || defined(FEAT_EVAL) \
5471 || defined(PROTO)
5472/*
5473 * Return the next command, after the first '|' or '\n'.
5474 * Return NULL if not found.
5475 */
5476 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005477find_nextcmd(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005478{
5479 while (*p != '|' && *p != '\n')
5480 {
5481 if (*p == NUL)
5482 return NULL;
5483 ++p;
5484 }
5485 return (p + 1);
5486}
5487#endif
5488
5489/*
Bram Moolenaar2256c992016-11-15 21:17:07 +01005490 * Check if *p is a separator between Ex commands, skipping over white space.
5491 * Return NULL if it isn't, the following character if it is.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005492 */
5493 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005494check_nextcmd(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005495{
Bram Moolenaar2256c992016-11-15 21:17:07 +01005496 char_u *s = skipwhite(p);
5497
5498 if (*s == '|' || *s == '\n')
5499 return (s + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005500 else
5501 return NULL;
5502}
5503
5504/*
5505 * - if there are more files to edit
5506 * - and this is the last window
5507 * - and forceit not used
5508 * - and not repeated twice on a row
5509 * return FAIL and give error message if 'message' TRUE
5510 * return OK otherwise
5511 */
5512 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005513check_more(
5514 int message, /* when FALSE check only, no messages */
5515 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005516{
5517 int n = ARGCOUNT - curwin->w_arg_idx - 1;
5518
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005519 if (!forceit && only_one_window()
5520 && ARGCOUNT > 1 && !arg_had_last && n >= 0 && quitmore == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005521 {
5522 if (message)
5523 {
5524#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
5525 if ((p_confirm || cmdmod.confirm) && curbuf->b_fname != NULL)
5526 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02005527 char_u buff[DIALOG_MSG_SIZE];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005528
Bram Moolenaarda6e8912018-08-21 15:12:14 +02005529 vim_snprintf((char *)buff, DIALOG_MSG_SIZE,
5530 NGETTEXT("%d more file to edit. Quit anyway?",
5531 "%d more files to edit. Quit anyway?", n), n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005532 if (vim_dialog_yesno(VIM_QUESTION, NULL, buff, 1) == VIM_YES)
5533 return OK;
5534 return FAIL;
5535 }
5536#endif
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01005537 semsg(NGETTEXT("E173: %d more file to edit",
5538 "E173: %d more files to edit", n), n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005539 quitmore = 2; /* next try to quit is allowed */
5540 }
5541 return FAIL;
5542 }
5543 return OK;
5544}
5545
Bram Moolenaarac9fb182019-04-27 13:04:13 +02005546#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005547/*
5548 * Function given to ExpandGeneric() to obtain the list of command names.
5549 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005550 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005551get_command_name(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005552{
5553 if (idx >= (int)CMD_SIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005554 return get_user_command_name(idx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005555 return cmdnames[idx].cmd_name;
5556}
5557#endif
5558
Bram Moolenaar071d4272004-06-13 20:20:40 +00005559 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005560ex_colorscheme(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005561{
Bram Moolenaare6850792010-05-14 15:28:44 +02005562 if (*eap->arg == NUL)
5563 {
5564#ifdef FEAT_EVAL
5565 char_u *expr = vim_strsave((char_u *)"g:colors_name");
5566 char_u *p = NULL;
5567
5568 if (expr != NULL)
5569 {
5570 ++emsg_off;
5571 p = eval_to_string(expr, NULL, FALSE);
5572 --emsg_off;
5573 vim_free(expr);
5574 }
5575 if (p != NULL)
5576 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01005577 msg((char *)p);
Bram Moolenaare6850792010-05-14 15:28:44 +02005578 vim_free(p);
5579 }
5580 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01005581 msg("default");
Bram Moolenaare6850792010-05-14 15:28:44 +02005582#else
Bram Moolenaar32526b32019-01-19 17:43:09 +01005583 msg(_("unknown"));
Bram Moolenaare6850792010-05-14 15:28:44 +02005584#endif
5585 }
5586 else if (load_colors(eap->arg) == FAIL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005587 semsg(_("E185: Cannot find color scheme '%s'"), eap->arg);
Bram Moolenaarf58d81a2019-01-28 20:19:05 +01005588
5589#ifdef FEAT_VTP
5590 else if (has_vtp_working())
5591 {
5592 // background color change requires clear + redraw
5593 update_screen(CLEAR);
5594 redrawcmd();
5595 }
5596#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005597}
5598
5599 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005600ex_highlight(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005601{
5602 if (*eap->arg == NUL && eap->cmd[2] == '!')
Bram Moolenaar32526b32019-01-19 17:43:09 +01005603 msg(_("Greetings, Vim user!"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005604 do_highlight(eap->arg, eap->forceit, FALSE);
5605}
5606
5607
5608/*
5609 * Call this function if we thought we were going to exit, but we won't
5610 * (because of an error). May need to restore the terminal mode.
5611 */
5612 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005613not_exiting(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005614{
5615 exiting = FALSE;
5616 settmode(TMODE_RAW);
5617}
5618
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005619 static int
5620before_quit_autocmds(win_T *wp, int quit_all, int forceit)
5621{
5622 apply_autocmds(EVENT_QUITPRE, NULL, NULL, FALSE, wp->w_buffer);
5623
5624 /* Bail out when autocommands closed the window.
5625 * Refuse to quit when the buffer in the last window is being closed (can
5626 * only happen in autocommands). */
5627 if (!win_valid(wp)
5628 || curbuf_locked()
5629 || (wp->w_buffer->b_nwindows == 1 && wp->w_buffer->b_locked > 0))
5630 return TRUE;
5631
5632 if (quit_all || (check_more(FALSE, forceit) == OK && only_one_window()))
5633 {
5634 apply_autocmds(EVENT_EXITPRE, NULL, NULL, FALSE, curbuf);
5635 /* Refuse to quit when locked or when the buffer in the last window is
5636 * being closed (can only happen in autocommands). */
5637 if (curbuf_locked()
5638 || (curbuf->b_nwindows == 1 && curbuf->b_locked > 0))
5639 return TRUE;
5640 }
5641
5642 return FALSE;
5643}
5644
Bram Moolenaar071d4272004-06-13 20:20:40 +00005645/*
Bram Moolenaarf240e182014-11-27 18:33:02 +01005646 * ":quit": quit current window, quit Vim if the last window is closed.
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005647 * ":{nr}quit": quit window {nr}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005648 */
5649 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005650ex_quit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005651{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005652 win_T *wp;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005653
Bram Moolenaar071d4272004-06-13 20:20:40 +00005654#ifdef FEAT_CMDWIN
5655 if (cmdwin_type != 0)
5656 {
5657 cmdwin_result = Ctrl_C;
5658 return;
5659 }
5660#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005661 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005662 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005663 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005664 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005665 return;
5666 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005667 if (eap->addr_count > 0)
5668 {
Bram Moolenaarf240e182014-11-27 18:33:02 +01005669 int wnr = eap->line2;
5670
5671 for (wp = firstwin; wp->w_next != NULL; wp = wp->w_next)
5672 if (--wnr <= 0)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005673 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005674 }
5675 else
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005676 wp = curwin;
Bram Moolenaarf240e182014-11-27 18:33:02 +01005677
Bram Moolenaar87ffb5c2017-10-19 12:37:42 +02005678 /* Refuse to quit when locked. */
5679 if (curbuf_locked())
5680 return;
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005681
5682 /* Trigger QuitPre and maybe ExitPre */
5683 if (before_quit_autocmds(wp, FALSE, eap->forceit))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005684 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005685
5686#ifdef FEAT_NETBEANS_INTG
5687 netbeansForcedQuit = eap->forceit;
5688#endif
5689
5690 /*
5691 * If there are more files or windows we won't exit.
5692 */
5693 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
5694 exiting = TRUE;
Bram Moolenaarff930ca2017-10-19 17:12:10 +02005695 if ((!buf_hide(wp->w_buffer)
5696 && check_changed(wp->w_buffer, (p_awa ? CCGD_AW : 0)
Bram Moolenaar45d3b142013-11-09 03:31:51 +01005697 | (eap->forceit ? CCGD_FORCEIT : 0)
5698 | CCGD_EXCMD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005699 || check_more(TRUE, eap->forceit) == FAIL
Bram Moolenaar027387f2016-01-02 22:25:52 +01005700 || (only_one_window() && check_changed_any(eap->forceit, TRUE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005701 {
5702 not_exiting();
5703 }
5704 else
5705 {
Bram Moolenaarc7a0d322015-06-19 12:43:07 +02005706 /* quit last window
5707 * Note: only_one_window() returns true, even so a help window is
5708 * still open. In that case only quit, if no address has been
5709 * specified. Example:
5710 * :h|wincmd w|1q - don't quit
5711 * :h|wincmd w|q - quit
5712 */
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01005713 if (only_one_window() && (ONE_WINDOW || eap->addr_count == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005714 getout(0);
Bram Moolenaar2c33d7b2017-10-14 16:06:20 +02005715 not_exiting();
Bram Moolenaar4033c552017-09-16 20:54:51 +02005716#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00005717 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005718#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005719 /* close window; may free buffer */
Bram Moolenaareb44a682017-08-03 22:44:55 +02005720 win_close(wp, !buf_hide(wp->w_buffer) || eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005721 }
5722}
5723
5724/*
5725 * ":cquit".
5726 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005727 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005728ex_cquit(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005729{
5730 getout(1); /* this does not always pass on the exit code to the Manx
5731 compiler. why? */
5732}
5733
5734/*
5735 * ":qall": try to quit all windows
5736 */
5737 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005738ex_quit_all(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005739{
5740# ifdef FEAT_CMDWIN
5741 if (cmdwin_type != 0)
5742 {
5743 if (eap->forceit)
5744 cmdwin_result = K_XF1; /* ex_window() takes care of this */
5745 else
5746 cmdwin_result = K_XF2;
5747 return;
5748 }
5749# endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005750
5751 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005752 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005753 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005754 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005755 return;
5756 }
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005757
5758 if (before_quit_autocmds(curwin, TRUE, eap->forceit))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005759 return;
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005760
Bram Moolenaar071d4272004-06-13 20:20:40 +00005761 exiting = TRUE;
Bram Moolenaar027387f2016-01-02 22:25:52 +01005762 if (eap->forceit || !check_changed_any(FALSE, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005763 getout(0);
5764 not_exiting();
5765}
5766
Bram Moolenaar071d4272004-06-13 20:20:40 +00005767/*
5768 * ":close": close current window, unless it is the last one
5769 */
5770 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005771ex_close(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005772{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005773 win_T *win;
5774 int winnr = 0;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005775#ifdef FEAT_CMDWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00005776 if (cmdwin_type != 0)
Bram Moolenaar9bd1a7e2011-05-19 14:50:54 +02005777 cmdwin_result = Ctrl_C;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005778 else
Bram Moolenaar4033c552017-09-16 20:54:51 +02005779#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005780 if (!text_locked() && !curbuf_locked())
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005781 {
5782 if (eap->addr_count == 0)
5783 ex_win_close(eap->forceit, curwin, NULL);
Bram Moolenaar6d41c782018-06-06 09:11:12 +02005784 else
5785 {
Bram Moolenaar29323592016-07-24 22:04:11 +02005786 FOR_ALL_WINDOWS(win)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005787 {
5788 winnr++;
5789 if (winnr == eap->line2)
5790 break;
5791 }
5792 if (win == NULL)
5793 win = lastwin;
5794 ex_win_close(eap->forceit, win, NULL);
5795 }
5796 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005797}
5798
Bram Moolenaar4033c552017-09-16 20:54:51 +02005799#ifdef FEAT_QUICKFIX
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005800/*
5801 * ":pclose": Close any preview window.
5802 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005803 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005804ex_pclose(exarg_T *eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005805{
5806 win_T *win;
5807
Bram Moolenaar29323592016-07-24 22:04:11 +02005808 FOR_ALL_WINDOWS(win)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005809 if (win->w_p_pvw)
5810 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00005811 ex_win_close(eap->forceit, win, NULL);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005812 break;
5813 }
5814}
Bram Moolenaar4033c552017-09-16 20:54:51 +02005815#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005816
Bram Moolenaarf740b292006-02-16 22:11:02 +00005817/*
5818 * Close window "win" and take care of handling closing the last window for a
5819 * modified buffer.
5820 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005821 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005822ex_win_close(
5823 int forceit,
5824 win_T *win,
5825 tabpage_T *tp) /* NULL or the tab page "win" is in */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005826{
5827 int need_hide;
5828 buf_T *buf = win->w_buffer;
5829
5830 need_hide = (bufIsChanged(buf) && buf->b_nwindows <= 1);
Bram Moolenaareb44a682017-08-03 22:44:55 +02005831 if (need_hide && !buf_hide(buf) && !forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005832 {
Bram Moolenaar4033c552017-09-16 20:54:51 +02005833#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005834 if ((p_confirm || cmdmod.confirm) && p_write)
5835 {
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005836 bufref_T bufref;
5837
5838 set_bufref(&bufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005839 dialog_changed(buf, FALSE);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005840 if (bufref_valid(&bufref) && bufIsChanged(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005841 return;
5842 need_hide = FALSE;
5843 }
5844 else
Bram Moolenaar4033c552017-09-16 20:54:51 +02005845#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005846 {
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02005847 no_write_message();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005848 return;
5849 }
5850 }
5851
Bram Moolenaar4033c552017-09-16 20:54:51 +02005852#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00005853 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005854#endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00005855
Bram Moolenaar071d4272004-06-13 20:20:40 +00005856 /* free buffer when not hiding it or when it's a scratch buffer */
Bram Moolenaarf740b292006-02-16 22:11:02 +00005857 if (tp == NULL)
Bram Moolenaareb44a682017-08-03 22:44:55 +02005858 win_close(win, !need_hide && !buf_hide(buf));
Bram Moolenaarf740b292006-02-16 22:11:02 +00005859 else
Bram Moolenaareb44a682017-08-03 22:44:55 +02005860 win_close_othertab(win, !need_hide && !buf_hide(buf), tp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005861}
5862
Bram Moolenaar071d4272004-06-13 20:20:40 +00005863/*
Bram Moolenaardea25702017-01-29 15:18:10 +01005864 * Handle the argument for a tabpage related ex command.
5865 * Returns a tabpage number.
5866 * When an error is encountered then eap->errmsg is set.
5867 */
5868 static int
5869get_tabpage_arg(exarg_T *eap)
5870{
5871 int tab_number;
5872 int unaccept_arg0 = (eap->cmdidx == CMD_tabmove) ? 0 : 1;
5873
5874 if (eap->arg && *eap->arg != NUL)
5875 {
5876 char_u *p = eap->arg;
5877 char_u *p_save;
5878 int relative = 0; /* argument +N/-N means: go to N places to the
5879 * right/left relative to the current position. */
5880
5881 if (*p == '-')
5882 {
5883 relative = -1;
5884 p++;
5885 }
5886 else if (*p == '+')
5887 {
5888 relative = 1;
5889 p++;
5890 }
5891
5892 p_save = p;
5893 tab_number = getdigits(&p);
5894
5895 if (relative == 0)
5896 {
5897 if (STRCMP(p, "$") == 0)
5898 tab_number = LAST_TAB_NR;
5899 else if (p == p_save || *p_save == '-' || *p != NUL
5900 || tab_number > LAST_TAB_NR)
5901 {
5902 /* No numbers as argument. */
5903 eap->errmsg = e_invarg;
5904 goto theend;
5905 }
5906 }
5907 else
5908 {
5909 if (*p_save == NUL)
5910 tab_number = 1;
5911 else if (p == p_save || *p_save == '-' || *p != NUL
5912 || tab_number == 0)
5913 {
5914 /* No numbers as argument. */
5915 eap->errmsg = e_invarg;
5916 goto theend;
5917 }
5918 tab_number = tab_number * relative + tabpage_index(curtab);
5919 if (!unaccept_arg0 && relative == -1)
5920 --tab_number;
5921 }
5922 if (tab_number < unaccept_arg0 || tab_number > LAST_TAB_NR)
5923 eap->errmsg = e_invarg;
5924 }
5925 else if (eap->addr_count > 0)
5926 {
5927 if (unaccept_arg0 && eap->line2 == 0)
Bram Moolenaarc6251552017-01-29 21:42:20 +01005928 {
Bram Moolenaardea25702017-01-29 15:18:10 +01005929 eap->errmsg = e_invrange;
Bram Moolenaarc6251552017-01-29 21:42:20 +01005930 tab_number = 0;
5931 }
Bram Moolenaardea25702017-01-29 15:18:10 +01005932 else
5933 {
5934 tab_number = eap->line2;
Bram Moolenaar82a12462019-01-22 22:41:42 +01005935 if (!unaccept_arg0 && *skipwhite(*eap->cmdlinep) == '-')
Bram Moolenaardea25702017-01-29 15:18:10 +01005936 {
5937 --tab_number;
5938 if (tab_number < unaccept_arg0)
5939 eap->errmsg = e_invarg;
5940 }
5941 }
5942 }
5943 else
5944 {
5945 switch (eap->cmdidx)
5946 {
5947 case CMD_tabnext:
5948 tab_number = tabpage_index(curtab) + 1;
5949 if (tab_number > LAST_TAB_NR)
5950 tab_number = 1;
5951 break;
5952 case CMD_tabmove:
5953 tab_number = LAST_TAB_NR;
5954 break;
5955 default:
5956 tab_number = tabpage_index(curtab);
5957 }
5958 }
5959
5960theend:
5961 return tab_number;
5962}
5963
5964/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00005965 * ":tabclose": close current tab page, unless it is the last one.
5966 * ":tabclose N": close tab page N.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005967 */
5968 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005969ex_tabclose(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005970{
Bram Moolenaarf740b292006-02-16 22:11:02 +00005971 tabpage_T *tp;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005972 int tab_number;
Bram Moolenaarf740b292006-02-16 22:11:02 +00005973
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005974# ifdef FEAT_CMDWIN
5975 if (cmdwin_type != 0)
5976 cmdwin_result = K_IGNORE;
5977 else
5978# endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00005979 if (first_tabpage->tp_next == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005980 emsg(_("E784: Cannot close last tab page"));
Bram Moolenaarf740b292006-02-16 22:11:02 +00005981 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005982 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005983 tab_number = get_tabpage_arg(eap);
5984 if (eap->errmsg == NULL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005985 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005986 tp = find_tabpage(tab_number);
Bram Moolenaarf740b292006-02-16 22:11:02 +00005987 if (tp == NULL)
5988 {
5989 beep_flush();
5990 return;
5991 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00005992 if (tp != curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00005993 {
5994 tabpage_close_other(tp, eap->forceit);
5995 return;
5996 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005997 else if (!text_locked() && !curbuf_locked())
Bram Moolenaar2f72c702017-01-29 14:48:10 +01005998 tabpage_close(eap->forceit);
5999 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006000 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006001}
6002
6003/*
6004 * ":tabonly": close all tab pages except the current one
6005 */
6006 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006007ex_tabonly(exarg_T *eap)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006008{
6009 tabpage_T *tp;
6010 int done;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006011 int tab_number;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006012
6013# ifdef FEAT_CMDWIN
6014 if (cmdwin_type != 0)
6015 cmdwin_result = K_IGNORE;
6016 else
6017# endif
6018 if (first_tabpage->tp_next == NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01006019 msg(_("Already only one tab page"));
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006020 else
6021 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006022 tab_number = get_tabpage_arg(eap);
6023 if (eap->errmsg == NULL)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006024 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006025 goto_tabpage(tab_number);
6026 /* Repeat this up to a 1000 times, because autocommands may
6027 * mess up the lists. */
6028 for (done = 0; done < 1000; ++done)
6029 {
6030 FOR_ALL_TABPAGES(tp)
6031 if (tp->tp_topframe != topframe)
6032 {
6033 tabpage_close_other(tp, eap->forceit);
6034 /* if we failed to close it quit */
6035 if (valid_tabpage(tp))
6036 done = 1000;
6037 /* start over, "tp" is now invalid */
6038 break;
6039 }
6040 if (first_tabpage->tp_next == NULL)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006041 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006042 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006043 }
6044 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006045}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006046
6047/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00006048 * Close the current tab page.
6049 */
6050 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006051tabpage_close(int forceit)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006052{
6053 /* First close all the windows but the current one. If that worked then
6054 * close the last window in this tab, that will close it. */
Bram Moolenaar459ca562016-11-10 18:16:33 +01006055 if (!ONE_WINDOW)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006056 close_others(TRUE, forceit);
Bram Moolenaar459ca562016-11-10 18:16:33 +01006057 if (ONE_WINDOW)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006058 ex_win_close(forceit, curwin, NULL);
6059# ifdef FEAT_GUI
6060 need_mouse_correct = TRUE;
6061# endif
6062}
6063
6064/*
6065 * Close tab page "tp", which is not the current tab page.
6066 * Note that autocommands may make "tp" invalid.
Bram Moolenaar7875acc2006-09-10 13:51:17 +00006067 * Also takes care of the tab pages line disappearing when closing the
6068 * last-but-one tab page.
Bram Moolenaarf740b292006-02-16 22:11:02 +00006069 */
6070 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006071tabpage_close_other(tabpage_T *tp, int forceit)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006072{
6073 int done = 0;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006074 win_T *wp;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00006075 int h = tabline_height();
Bram Moolenaarf740b292006-02-16 22:11:02 +00006076
6077 /* Limit to 1000 windows, autocommands may add a window while we close
6078 * one. OK, so I'm paranoid... */
6079 while (++done < 1000)
6080 {
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006081 wp = tp->tp_firstwin;
6082 ex_win_close(forceit, wp, tp);
Bram Moolenaarf740b292006-02-16 22:11:02 +00006083
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006084 /* Autocommands may delete the tab page under our fingers and we may
6085 * fail to close a window with a modified buffer. */
6086 if (!valid_tabpage(tp) || tp->tp_firstwin == wp)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006087 break;
6088 }
Bram Moolenaar7875acc2006-09-10 13:51:17 +00006089
Bram Moolenaar29323592016-07-24 22:04:11 +02006090 apply_autocmds(EVENT_TABCLOSED, NULL, NULL, FALSE, curbuf);
Bram Moolenaar29323592016-07-24 22:04:11 +02006091
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006092 redraw_tabline = TRUE;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00006093 if (h != tabline_height())
6094 shell_new_rows();
Bram Moolenaarf740b292006-02-16 22:11:02 +00006095}
6096
6097/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006098 * ":only".
6099 */
6100 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006101ex_only(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006102{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01006103 win_T *wp;
6104 int wnr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006105# ifdef FEAT_GUI
6106 need_mouse_correct = TRUE;
6107# endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01006108 if (eap->addr_count > 0)
6109 {
6110 wnr = eap->line2;
6111 for (wp = firstwin; --wnr > 0; )
6112 {
6113 if (wp->w_next == NULL)
6114 break;
6115 else
6116 wp = wp->w_next;
6117 }
6118 win_goto(wp);
6119 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006120 close_others(TRUE, eap->forceit);
6121}
6122
6123/*
6124 * ":all" and ":sall".
Bram Moolenaard9967712006-03-11 21:18:15 +00006125 * Also used for ":tab drop file ..." after setting the argument list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006126 */
Bram Moolenaard9967712006-03-11 21:18:15 +00006127 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006128ex_all(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006129{
6130 if (eap->addr_count == 0)
6131 eap->line2 = 9999;
Bram Moolenaard9967712006-03-11 21:18:15 +00006132 do_arg_all((int)eap->line2, eap->forceit, eap->cmdidx == CMD_drop);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006133}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006134
6135 static void
Bram Moolenaar5e1e6d22017-01-02 17:26:00 +01006136ex_hide(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006137{
Bram Moolenaar2256c992016-11-15 21:17:07 +01006138 /* ":hide" or ":hide | cmd": hide current window */
Bram Moolenaar2256c992016-11-15 21:17:07 +01006139 if (!eap->skip)
6140 {
Bram Moolenaar4033c552017-09-16 20:54:51 +02006141#ifdef FEAT_GUI
Bram Moolenaar2256c992016-11-15 21:17:07 +01006142 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006143#endif
Bram Moolenaar2256c992016-11-15 21:17:07 +01006144 if (eap->addr_count == 0)
6145 win_close(curwin, FALSE); /* don't free buffer */
6146 else
6147 {
6148 int winnr = 0;
6149 win_T *win;
Bram Moolenaarf240e182014-11-27 18:33:02 +01006150
Bram Moolenaar2256c992016-11-15 21:17:07 +01006151 FOR_ALL_WINDOWS(win)
6152 {
6153 winnr++;
6154 if (winnr == eap->line2)
6155 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01006156 }
Bram Moolenaar2256c992016-11-15 21:17:07 +01006157 if (win == NULL)
6158 win = lastwin;
6159 win_close(win, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006160 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006161 }
6162}
6163
6164/*
6165 * ":stop" and ":suspend": Suspend Vim.
6166 */
6167 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006168ex_stop(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006169{
6170 /*
6171 * Disallow suspending for "rvim".
6172 */
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006173 if (!check_restricted())
Bram Moolenaar071d4272004-06-13 20:20:40 +00006174 {
6175 if (!eap->forceit)
6176 autowrite_all();
6177 windgoto((int)Rows - 1, 0);
6178 out_char('\n');
6179 out_flush();
6180 stoptermcap();
6181 out_flush(); /* needed for SUN to restore xterm buffer */
6182#ifdef FEAT_TITLE
Bram Moolenaar40385db2018-08-07 22:31:44 +02006183 mch_restore_title(SAVE_RESTORE_BOTH); /* restore window titles */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006184#endif
6185 ui_suspend(); /* call machine specific function */
6186#ifdef FEAT_TITLE
6187 maketitle();
6188 resettitle(); /* force updating the title */
6189#endif
6190 starttermcap();
6191 scroll_start(); /* scroll screen before redrawing */
6192 redraw_later_clear();
6193 shell_resized(); /* may have resized window */
6194 }
6195}
6196
6197/*
Bram Moolenaar12a96de2018-03-11 14:44:18 +01006198 * ":exit", ":xit" and ":wq": Write file and quite the current window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006199 */
6200 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006201ex_exit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006202{
6203#ifdef FEAT_CMDWIN
6204 if (cmdwin_type != 0)
6205 {
6206 cmdwin_result = Ctrl_C;
6207 return;
6208 }
6209#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006210 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006211 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006212 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006213 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006214 return;
6215 }
Bram Moolenaar12a96de2018-03-11 14:44:18 +01006216
6217 if (before_quit_autocmds(curwin, FALSE, eap->forceit))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006218 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006219
6220 /*
6221 * if more files or windows we won't exit
6222 */
6223 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
6224 exiting = TRUE;
6225 if ( ((eap->cmdidx == CMD_wq
6226 || curbufIsChanged())
6227 && do_write(eap) == FAIL)
6228 || check_more(TRUE, eap->forceit) == FAIL
Bram Moolenaar027387f2016-01-02 22:25:52 +01006229 || (only_one_window() && check_changed_any(eap->forceit, FALSE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006230 {
6231 not_exiting();
6232 }
6233 else
6234 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006235 if (only_one_window()) /* quit last window, exit Vim */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006236 getout(0);
Bram Moolenaar2c33d7b2017-10-14 16:06:20 +02006237 not_exiting();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006238# ifdef FEAT_GUI
6239 need_mouse_correct = TRUE;
6240# endif
Bram Moolenaar1a4a75c2013-07-28 16:03:06 +02006241 /* Quit current window, may free the buffer. */
Bram Moolenaareb44a682017-08-03 22:44:55 +02006242 win_close(curwin, !buf_hide(curwin->w_buffer));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006243 }
6244}
6245
6246/*
6247 * ":print", ":list", ":number".
6248 */
6249 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006250ex_print(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006251{
Bram Moolenaardf177f62005-02-22 08:39:57 +00006252 if (curbuf->b_ml.ml_flags & ML_EMPTY)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006253 emsg(_(e_emptybuf));
Bram Moolenaardf177f62005-02-22 08:39:57 +00006254 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006255 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00006256 for ( ;!got_int; ui_breakcheck())
6257 {
6258 print_line(eap->line1,
6259 (eap->cmdidx == CMD_number || eap->cmdidx == CMD_pound
6260 || (eap->flags & EXFLAG_NR)),
6261 eap->cmdidx == CMD_list || (eap->flags & EXFLAG_LIST));
6262 if (++eap->line1 > eap->line2)
6263 break;
6264 out_flush(); /* show one line at a time */
6265 }
6266 setpcmark();
6267 /* put cursor at last line */
6268 curwin->w_cursor.lnum = eap->line2;
6269 beginline(BL_SOL | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006270 }
6271
Bram Moolenaar071d4272004-06-13 20:20:40 +00006272 ex_no_reprint = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006273}
6274
6275#ifdef FEAT_BYTEOFF
6276 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006277ex_goto(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006278{
6279 goto_byte(eap->line2);
6280}
6281#endif
6282
6283/*
6284 * ":shell".
6285 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006286 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006287ex_shell(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006288{
6289 do_shell(NULL, 0);
6290}
6291
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006292#if defined(HAVE_DROP_FILE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006293
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006294static int drop_busy = FALSE;
6295static int drop_filec;
6296static char_u **drop_filev = NULL;
6297static int drop_split;
6298static void (*drop_callback)(void *);
6299static void *drop_cookie;
6300
6301 static void
6302handle_drop_internal(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006303{
6304 exarg_T ea;
6305 int save_msg_scroll = msg_scroll;
6306
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006307 // Setting the argument list may cause screen updates and being called
6308 // recursively. Avoid that by setting drop_busy.
6309 drop_busy = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006310
6311 /* Check whether the current buffer is changed. If so, we will need
6312 * to split the current window or data could be lost.
6313 * We don't need to check if the 'hidden' option is set, as in this
6314 * case the buffer won't be lost.
6315 */
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006316 if (!buf_hide(curbuf) && !drop_split)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006317 {
6318 ++emsg_off;
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006319 drop_split = check_changed(curbuf, CCGD_AW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006320 --emsg_off;
6321 }
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006322 if (drop_split)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006323 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006324 if (win_split(0, 0) == FAIL)
6325 return;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02006326 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006327
6328 /* When splitting the window, create a new alist. Otherwise the
6329 * existing one is overwritten. */
6330 alist_unlink(curwin->w_alist);
6331 alist_new();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006332 }
6333
6334 /*
6335 * Set up the new argument list.
6336 */
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006337 alist_set(ALIST(curwin), drop_filec, drop_filev, FALSE, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006338
6339 /*
6340 * Move to the first file.
6341 */
6342 /* Fake up a minimal "next" command for do_argfile() */
6343 vim_memset(&ea, 0, sizeof(ea));
6344 ea.cmd = (char_u *)"next";
6345 do_argfile(&ea, 0);
6346
6347 /* do_ecmd() may set need_start_insertmode, but since we never left Insert
6348 * mode that is not needed here. */
6349 need_start_insertmode = FALSE;
6350
6351 /* Restore msg_scroll, otherwise a following command may cause scrolling
6352 * unexpectedly. The screen will be redrawn by the caller, thus
6353 * msg_scroll being set by displaying a message is irrelevant. */
6354 msg_scroll = save_msg_scroll;
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006355
6356 if (drop_callback != NULL)
6357 drop_callback(drop_cookie);
6358
6359 drop_filev = NULL;
6360 drop_busy = FALSE;
6361}
6362
6363/*
6364 * Handle a file drop. The code is here because a drop is *nearly* like an
6365 * :args command, but not quite (we have a list of exact filenames, so we
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01006366 * don't want to (a) parse a command line, or (b) expand wildcards). So the
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006367 * code is very similar to :args and hence needs access to a lot of the static
6368 * functions in this file.
6369 *
6370 * The "filev" list must have been allocated using alloc(), as should each item
6371 * in the list. This function takes over responsibility for freeing the "filev"
6372 * list.
6373 */
6374 void
6375handle_drop(
6376 int filec, // the number of files dropped
6377 char_u **filev, // the list of files dropped
6378 int split, // force splitting the window
6379 void (*callback)(void *), // to be called after setting the argument
6380 // list
6381 void *cookie) // argument for "callback" (allocated)
6382{
6383 // Cannot handle recursive drops, finish the pending one.
6384 if (drop_busy)
6385 {
6386 FreeWild(filec, filev);
6387 vim_free(cookie);
6388 return;
6389 }
6390
6391 // When calling handle_drop() more than once in a row we only use the last
6392 // one.
6393 if (drop_filev != NULL)
6394 {
6395 FreeWild(drop_filec, drop_filev);
6396 vim_free(drop_cookie);
6397 }
6398
6399 drop_filec = filec;
6400 drop_filev = filev;
6401 drop_split = split;
6402 drop_callback = callback;
6403 drop_cookie = cookie;
6404
6405 // Postpone this when:
6406 // - editing the command line
6407 // - not possible to change the current buffer
6408 // - updating the screen
6409 // As it may change buffers and window structures that are in use and cause
6410 // freed memory to be used.
6411 if (text_locked() || curbuf_locked() || updating_screen)
6412 return;
6413
6414 handle_drop_internal();
6415}
6416
6417/*
6418 * To be called when text is unlocked, curbuf is unlocked or updating_screen is
6419 * reset: Handle a postponed drop.
6420 */
6421 void
6422handle_any_postponed_drop(void)
6423{
6424 if (!drop_busy && drop_filev != NULL
6425 && !text_locked() && !curbuf_locked() && !updating_screen)
6426 handle_drop_internal();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006427}
6428#endif
6429
Bram Moolenaar071d4272004-06-13 20:20:40 +00006430/*
6431 * Clear an argument list: free all file names and reset it to zero entries.
6432 */
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00006433 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006434alist_clear(alist_T *al)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006435{
6436 while (--al->al_ga.ga_len >= 0)
6437 vim_free(AARGLIST(al)[al->al_ga.ga_len].ae_fname);
6438 ga_clear(&al->al_ga);
6439}
6440
6441/*
6442 * Init an argument list.
6443 */
6444 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006445alist_init(alist_T *al)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006446{
6447 ga_init2(&al->al_ga, (int)sizeof(aentry_T), 5);
6448}
6449
Bram Moolenaar071d4272004-06-13 20:20:40 +00006450/*
6451 * Remove a reference from an argument list.
6452 * Ignored when the argument list is the global one.
6453 * If the argument list is no longer used by any window, free it.
6454 */
6455 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006456alist_unlink(alist_T *al)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006457{
6458 if (al != &global_alist && --al->al_refcount <= 0)
6459 {
6460 alist_clear(al);
6461 vim_free(al);
6462 }
6463}
6464
Bram Moolenaar071d4272004-06-13 20:20:40 +00006465/*
6466 * Create a new argument list and use it for the current window.
6467 */
6468 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006469alist_new(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006470{
6471 curwin->w_alist = (alist_T *)alloc((unsigned)sizeof(alist_T));
6472 if (curwin->w_alist == NULL)
6473 {
6474 curwin->w_alist = &global_alist;
6475 ++global_alist.al_refcount;
6476 }
6477 else
6478 {
6479 curwin->w_alist->al_refcount = 1;
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02006480 curwin->w_alist->id = ++max_alist_id;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006481 alist_init(curwin->w_alist);
6482 }
6483}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006484
Bram Moolenaara06ecab2016-07-16 14:47:36 +02006485#if !defined(UNIX) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006486/*
6487 * Expand the file names in the global argument list.
Bram Moolenaar86b68352004-12-27 21:59:20 +00006488 * If "fnum_list" is not NULL, use "fnum_list[fnum_len]" as a list of buffer
6489 * numbers to be re-used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006490 */
6491 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006492alist_expand(int *fnum_list, int fnum_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006493{
6494 char_u **old_arg_files;
Bram Moolenaar86b68352004-12-27 21:59:20 +00006495 int old_arg_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006496 char_u **new_arg_files;
6497 int new_arg_file_count;
6498 char_u *save_p_su = p_su;
6499 int i;
6500
6501 /* Don't use 'suffixes' here. This should work like the shell did the
6502 * expansion. Also, the vimrc file isn't read yet, thus the user
6503 * can't set the options. */
6504 p_su = empty_option;
6505 old_arg_files = (char_u **)alloc((unsigned)(sizeof(char_u *) * GARGCOUNT));
6506 if (old_arg_files != NULL)
6507 {
6508 for (i = 0; i < GARGCOUNT; ++i)
Bram Moolenaar86b68352004-12-27 21:59:20 +00006509 old_arg_files[i] = vim_strsave(GARGLIST[i].ae_fname);
6510 old_arg_count = GARGCOUNT;
6511 if (expand_wildcards(old_arg_count, old_arg_files,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006512 &new_arg_file_count, &new_arg_files,
Bram Moolenaarb5609832011-07-20 15:04:58 +02006513 EW_FILE|EW_NOTFOUND|EW_ADDSLASH|EW_NOERROR) == OK
Bram Moolenaar071d4272004-06-13 20:20:40 +00006514 && new_arg_file_count > 0)
6515 {
Bram Moolenaar86b68352004-12-27 21:59:20 +00006516 alist_set(&global_alist, new_arg_file_count, new_arg_files,
6517 TRUE, fnum_list, fnum_len);
6518 FreeWild(old_arg_count, old_arg_files);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006519 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006520 }
6521 p_su = save_p_su;
6522}
6523#endif
6524
6525/*
6526 * Set the argument list for the current window.
6527 * Takes over the allocated files[] and the allocated fnames in it.
6528 */
6529 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006530alist_set(
6531 alist_T *al,
6532 int count,
6533 char_u **files,
6534 int use_curbuf,
6535 int *fnum_list,
6536 int fnum_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006537{
6538 int i;
Bram Moolenaar9e33efd2018-02-09 17:50:28 +01006539 static int recursive = 0;
6540
6541 if (recursive)
6542 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006543 emsg(_(e_au_recursive));
Bram Moolenaar9e33efd2018-02-09 17:50:28 +01006544 return;
6545 }
6546 ++recursive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006547
6548 alist_clear(al);
6549 if (ga_grow(&al->al_ga, count) == OK)
6550 {
6551 for (i = 0; i < count; ++i)
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00006552 {
6553 if (got_int)
6554 {
6555 /* When adding many buffers this can take a long time. Allow
6556 * interrupting here. */
6557 while (i < count)
6558 vim_free(files[i++]);
6559 break;
6560 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00006561
6562 /* May set buffer name of a buffer previously used for the
6563 * argument list, so that it's re-used by alist_add. */
6564 if (fnum_list != NULL && i < fnum_len)
6565 buf_set_name(fnum_list[i], files[i]);
6566
Bram Moolenaar071d4272004-06-13 20:20:40 +00006567 alist_add(al, files[i], use_curbuf ? 2 : 1);
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00006568 ui_breakcheck();
6569 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006570 vim_free(files);
6571 }
6572 else
6573 FreeWild(count, files);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006574 if (al == &global_alist)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006575 arg_had_last = FALSE;
Bram Moolenaar9e33efd2018-02-09 17:50:28 +01006576
6577 --recursive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006578}
6579
6580/*
6581 * Add file "fname" to argument list "al".
6582 * "fname" must have been allocated and "al" must have been checked for room.
6583 */
6584 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006585alist_add(
6586 alist_T *al,
6587 char_u *fname,
6588 int set_fnum) /* 1: set buffer number; 2: re-use curbuf */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006589{
6590 if (fname == NULL) /* don't add NULL file names */
6591 return;
6592#ifdef BACKSLASH_IN_FILENAME
6593 slash_adjust(fname);
6594#endif
6595 AARGLIST(al)[al->al_ga.ga_len].ae_fname = fname;
6596 if (set_fnum > 0)
6597 AARGLIST(al)[al->al_ga.ga_len].ae_fnum =
6598 buflist_add(fname, BLN_LISTED | (set_fnum == 2 ? BLN_CURBUF : 0));
6599 ++al->al_ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006600}
6601
6602#if defined(BACKSLASH_IN_FILENAME) || defined(PROTO)
6603/*
6604 * Adjust slashes in file names. Called after 'shellslash' was set.
6605 */
6606 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006607alist_slash_adjust(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006608{
6609 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006610 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00006611 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006612
6613 for (i = 0; i < GARGCOUNT; ++i)
6614 if (GARGLIST[i].ae_fname != NULL)
6615 slash_adjust(GARGLIST[i].ae_fname);
Bram Moolenaarf740b292006-02-16 22:11:02 +00006616 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006617 if (wp->w_alist != &global_alist)
6618 for (i = 0; i < WARGCOUNT(wp); ++i)
6619 if (WARGLIST(wp)[i].ae_fname != NULL)
6620 slash_adjust(WARGLIST(wp)[i].ae_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006621}
6622#endif
6623
6624/*
6625 * ":preserve".
6626 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006627 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006628ex_preserve(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006629{
Bram Moolenaar4399ef42005-02-12 14:29:27 +00006630 curbuf->b_flags |= BF_PRESERVED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006631 ml_preserve(curbuf, TRUE);
6632}
6633
6634/*
6635 * ":recover".
6636 */
6637 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006638ex_recover(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006639{
6640 /* Set recoverymode right away to avoid the ATTENTION prompt. */
6641 recoverymode = TRUE;
Bram Moolenaar45d3b142013-11-09 03:31:51 +01006642 if (!check_changed(curbuf, (p_awa ? CCGD_AW : 0)
6643 | CCGD_MULTWIN
6644 | (eap->forceit ? CCGD_FORCEIT : 0)
6645 | CCGD_EXCMD)
6646
Bram Moolenaar071d4272004-06-13 20:20:40 +00006647 && (*eap->arg == NUL
6648 || setfname(curbuf, eap->arg, NULL, TRUE) == OK))
6649 ml_recover();
6650 recoverymode = FALSE;
6651}
6652
6653/*
6654 * Command modifier used in a wrong way.
6655 */
6656 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006657ex_wrongmodifier(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006658{
6659 eap->errmsg = e_invcmd;
6660}
6661
Bram Moolenaar071d4272004-06-13 20:20:40 +00006662/*
6663 * :sview [+command] file split window with new file, read-only
6664 * :split [[+command] file] split window with current or new file
6665 * :vsplit [[+command] file] split window vertically with current or new file
6666 * :new [[+command] file] split window with no or new file
6667 * :vnew [[+command] file] split vertically window with no or new file
6668 * :sfind [+command] file split window with file in 'path'
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006669 *
6670 * :tabedit open new Tab page with empty window
6671 * :tabedit [+command] file open new Tab page and edit "file"
6672 * :tabnew [[+command] file] just like :tabedit
6673 * :tabfind [+command] file open new Tab page and find "file"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006674 */
6675 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006676ex_splitview(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006677{
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006678 win_T *old_curwin = curwin;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006679#if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006680 char_u *fname = NULL;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006681#endif
6682#ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00006683 int browse_flag = cmdmod.browse;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006684#endif
Bram Moolenaar39902a02018-06-21 22:10:08 +02006685 int use_tab = eap->cmdidx == CMD_tabedit
6686 || eap->cmdidx == CMD_tabfind
6687 || eap->cmdidx == CMD_tabnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006688
Bram Moolenaar4033c552017-09-16 20:54:51 +02006689#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00006690 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006691#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006692
Bram Moolenaar4033c552017-09-16 20:54:51 +02006693#ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00006694 /* A ":split" in the quickfix window works like ":new". Don't want two
Bram Moolenaar05bb9532008-07-04 09:44:11 +00006695 * quickfix windows. But it's OK when doing ":tab split". */
6696 if (bt_quickfix(curbuf) && cmdmod.tab == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006697 {
6698 if (eap->cmdidx == CMD_split)
6699 eap->cmdidx = CMD_new;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006700 if (eap->cmdidx == CMD_vsplit)
6701 eap->cmdidx = CMD_vnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006702 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02006703#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006704
Bram Moolenaar4033c552017-09-16 20:54:51 +02006705#ifdef FEAT_SEARCHPATH
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006706 if (eap->cmdidx == CMD_sfind || eap->cmdidx == CMD_tabfind)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006707 {
6708 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg),
6709 FNAME_MESS, TRUE, curbuf->b_ffname);
6710 if (fname == NULL)
6711 goto theend;
6712 eap->arg = fname;
6713 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006714# ifdef FEAT_BROWSE
Bram Moolenaar4033c552017-09-16 20:54:51 +02006715 else
6716# endif
6717#endif
6718#ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00006719 if (cmdmod.browse
Bram Moolenaar071d4272004-06-13 20:20:40 +00006720 && eap->cmdidx != CMD_vnew
Bram Moolenaar071d4272004-06-13 20:20:40 +00006721 && eap->cmdidx != CMD_new)
6722 {
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006723 if (
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006724# ifdef FEAT_GUI
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006725 !gui.in_use &&
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006726# endif
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006727 au_has_group((char_u *)"FileExplorer"))
6728 {
6729 /* No browsing supported but we do have the file explorer:
6730 * Edit the directory. */
6731 if (*eap->arg == NUL || !mch_isdir(eap->arg))
6732 eap->arg = (char_u *)".";
6733 }
6734 else
6735 {
Bram Moolenaar39902a02018-06-21 22:10:08 +02006736 fname = do_browse(0, (char_u *)(use_tab
6737 ? _("Edit File in new tab page")
6738 : _("Edit File in new window")),
Bram Moolenaar071d4272004-06-13 20:20:40 +00006739 eap->arg, NULL, NULL, NULL, curbuf);
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006740 if (fname == NULL)
6741 goto theend;
6742 eap->arg = fname;
6743 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006744 }
6745 cmdmod.browse = FALSE; /* Don't browse again in do_ecmd(). */
Bram Moolenaar4033c552017-09-16 20:54:51 +02006746#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006747
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006748 /*
6749 * Either open new tab page or split the window.
6750 */
Bram Moolenaar39902a02018-06-21 22:10:08 +02006751 if (use_tab)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006752 {
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006753 if (win_new_tabpage(cmdmod.tab != 0 ? cmdmod.tab
6754 : eap->addr_count == 0 ? 0
6755 : (int)eap->line2 + 1) != FAIL)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006756 {
Bram Moolenaard2b66012008-01-09 19:30:36 +00006757 do_exedit(eap, old_curwin);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006758
6759 /* set the alternate buffer for the window we came from */
6760 if (curwin != old_curwin
6761 && win_valid(old_curwin)
6762 && old_curwin->w_buffer != curbuf
6763 && !cmdmod.keepalt)
6764 old_curwin->w_alt_fnum = curbuf->b_fnum;
6765 }
6766 }
6767 else if (win_split(eap->addr_count > 0 ? (int)eap->line2 : 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006768 *eap->cmd == 'v' ? WSP_VERT : 0) != FAIL)
6769 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006770 /* Reset 'scrollbind' when editing another file, but keep it when
6771 * doing ":split" without arguments. */
6772 if (*eap->arg != NUL
Bram Moolenaar8a3bb562018-03-04 20:14:14 +01006773# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00006774 || cmdmod.browse
Bram Moolenaar8a3bb562018-03-04 20:14:14 +01006775# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006776 )
Bram Moolenaar3368ea22010-09-21 16:56:35 +02006777 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006778 else
6779 do_check_scrollbind(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006780 do_exedit(eap, old_curwin);
6781 }
6782
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006783# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00006784 cmdmod.browse = browse_flag;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006785# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006786
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006787# if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006788theend:
6789 vim_free(fname);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006790# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006791}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006792
6793/*
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006794 * Open a new tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006795 */
6796 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006797tabpage_new(void)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006798{
6799 exarg_T ea;
6800
6801 vim_memset(&ea, 0, sizeof(ea));
6802 ea.cmdidx = CMD_tabnew;
6803 ea.cmd = (char_u *)"tabn";
6804 ea.arg = (char_u *)"";
6805 ex_splitview(&ea);
6806}
6807
6808/*
6809 * :tabnext command
6810 */
6811 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006812ex_tabnext(exarg_T *eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006813{
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006814 int tab_number;
6815
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006816 switch (eap->cmdidx)
6817 {
6818 case CMD_tabfirst:
6819 case CMD_tabrewind:
6820 goto_tabpage(1);
6821 break;
6822 case CMD_tablast:
6823 goto_tabpage(9999);
6824 break;
6825 case CMD_tabprevious:
6826 case CMD_tabNext:
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006827 if (eap->arg && *eap->arg != NUL)
6828 {
6829 char_u *p = eap->arg;
6830 char_u *p_save = p;
6831
6832 tab_number = getdigits(&p);
6833 if (p == p_save || *p_save == '-' || *p != NUL
6834 || tab_number == 0)
6835 {
6836 /* No numbers as argument. */
6837 eap->errmsg = e_invarg;
6838 return;
6839 }
6840 }
6841 else
6842 {
6843 if (eap->addr_count == 0)
6844 tab_number = 1;
6845 else
6846 {
6847 tab_number = eap->line2;
6848 if (tab_number < 1)
6849 {
6850 eap->errmsg = e_invrange;
6851 return;
6852 }
6853 }
6854 }
6855 goto_tabpage(-tab_number);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006856 break;
6857 default: /* CMD_tabnext */
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006858 tab_number = get_tabpage_arg(eap);
6859 if (eap->errmsg == NULL)
6860 goto_tabpage(tab_number);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006861 break;
6862 }
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006863}
6864
6865/*
6866 * :tabmove command
6867 */
6868 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006869ex_tabmove(exarg_T *eap)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006870{
Bram Moolenaar40ce3a42015-04-21 18:08:39 +02006871 int tab_number;
Bram Moolenaar8cb8dca2012-07-06 18:27:39 +02006872
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006873 tab_number = get_tabpage_arg(eap);
6874 if (eap->errmsg == NULL)
6875 tabpage_move(tab_number);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006876}
6877
6878/*
6879 * :tabs command: List tabs and their contents.
6880 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006881 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006882ex_tabs(exarg_T *eap UNUSED)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006883{
6884 tabpage_T *tp;
6885 win_T *wp;
6886 int tabcount = 1;
6887
6888 msg_start();
6889 msg_scroll = TRUE;
6890 for (tp = first_tabpage; tp != NULL && !got_int; tp = tp->tp_next)
6891 {
6892 msg_putchar('\n');
6893 vim_snprintf((char *)IObuff, IOSIZE, _("Tab page %d"), tabcount++);
Bram Moolenaar8820b482017-03-16 17:23:31 +01006894 msg_outtrans_attr(IObuff, HL_ATTR(HLF_T));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006895 out_flush(); /* output one line at a time */
6896 ui_breakcheck();
6897
Bram Moolenaar030f0df2006-02-21 22:02:53 +00006898 if (tp == curtab)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006899 wp = firstwin;
6900 else
6901 wp = tp->tp_firstwin;
6902 for ( ; wp != NULL && !got_int; wp = wp->w_next)
6903 {
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006904 msg_putchar('\n');
6905 msg_putchar(wp == curwin ? '>' : ' ');
6906 msg_putchar(' ');
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006907 msg_putchar(bufIsChanged(wp->w_buffer) ? '+' : ' ');
6908 msg_putchar(' ');
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006909 if (buf_spname(wp->w_buffer) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02006910 vim_strncpy(IObuff, buf_spname(wp->w_buffer), IOSIZE - 1);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006911 else
6912 home_replace(wp->w_buffer, wp->w_buffer->b_fname,
6913 IObuff, IOSIZE, TRUE);
6914 msg_outtrans(IObuff);
6915 out_flush(); /* output one line at a time */
6916 ui_breakcheck();
6917 }
6918 }
6919}
6920
Bram Moolenaar071d4272004-06-13 20:20:40 +00006921/*
6922 * ":mode": Set screen mode.
6923 * If no argument given, just get the screen size and redraw.
6924 */
6925 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006926ex_mode(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006927{
6928 if (*eap->arg == NUL)
6929 shell_resized();
6930 else
6931 mch_screenmode(eap->arg);
6932}
6933
Bram Moolenaar071d4272004-06-13 20:20:40 +00006934/*
6935 * ":resize".
6936 * set, increment or decrement current window height
6937 */
6938 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006939ex_resize(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006940{
6941 int n;
6942 win_T *wp = curwin;
6943
6944 if (eap->addr_count > 0)
6945 {
6946 n = eap->line2;
6947 for (wp = firstwin; wp->w_next != NULL && --n > 0; wp = wp->w_next)
6948 ;
6949 }
6950
Bram Moolenaar44a2f922016-03-19 22:11:51 +01006951# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00006952 need_mouse_correct = TRUE;
Bram Moolenaar44a2f922016-03-19 22:11:51 +01006953# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006954 n = atol((char *)eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006955 if (cmdmod.split & WSP_VERT)
6956 {
6957 if (*eap->arg == '-' || *eap->arg == '+')
Bram Moolenaar02631462017-09-22 15:20:32 +02006958 n += curwin->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006959 else if (n == 0 && eap->arg[0] == NUL) /* default is very wide */
6960 n = 9999;
6961 win_setwidth_win((int)n, wp);
6962 }
6963 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006964 {
6965 if (*eap->arg == '-' || *eap->arg == '+')
6966 n += curwin->w_height;
Bram Moolenaar1f2903c2017-07-23 19:51:01 +02006967 else if (n == 0 && eap->arg[0] == NUL) /* default is very high */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006968 n = 9999;
6969 win_setheight_win((int)n, wp);
6970 }
6971}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006972
6973/*
6974 * ":find [+command] <file>" command.
6975 */
6976 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006977ex_find(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006978{
6979#ifdef FEAT_SEARCHPATH
6980 char_u *fname;
6981 int count;
6982
6983 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg), FNAME_MESS,
6984 TRUE, curbuf->b_ffname);
6985 if (eap->addr_count > 0)
6986 {
6987 /* Repeat finding the file "count" times. This matters when it
6988 * appears several times in the path. */
6989 count = eap->line2;
6990 while (fname != NULL && --count > 0)
6991 {
6992 vim_free(fname);
6993 fname = find_file_in_path(NULL, 0, FNAME_MESS,
6994 FALSE, curbuf->b_ffname);
6995 }
6996 }
6997
6998 if (fname != NULL)
6999 {
7000 eap->arg = fname;
7001#endif
7002 do_exedit(eap, NULL);
7003#ifdef FEAT_SEARCHPATH
7004 vim_free(fname);
7005 }
7006#endif
7007}
7008
7009/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00007010 * ":open" simulation: for now just work like ":visual".
7011 */
7012 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007013ex_open(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007014{
7015 regmatch_T regmatch;
7016 char_u *p;
7017
7018 curwin->w_cursor.lnum = eap->line2;
7019 beginline(BL_SOL | BL_FIX);
7020 if (*eap->arg == '/')
7021 {
7022 /* ":open /pattern/": put cursor in column found with pattern */
7023 ++eap->arg;
7024 p = skip_regexp(eap->arg, '/', p_magic, NULL);
7025 *p = NUL;
7026 regmatch.regprog = vim_regcomp(eap->arg, p_magic ? RE_MAGIC : 0);
7027 if (regmatch.regprog != NULL)
7028 {
7029 regmatch.rm_ic = p_ic;
7030 p = ml_get_curline();
7031 if (vim_regexec(&regmatch, p, (colnr_T)0))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007032 curwin->w_cursor.col = (colnr_T)(regmatch.startp[0] - p);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007033 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007034 emsg(_(e_nomatch));
Bram Moolenaar473de612013-06-08 18:19:48 +02007035 vim_regfree(regmatch.regprog);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007036 }
7037 /* Move to the NUL, ignore any other arguments. */
7038 eap->arg += STRLEN(eap->arg);
7039 }
7040 check_cursor();
7041
7042 eap->cmdidx = CMD_visual;
7043 do_exedit(eap, NULL);
7044}
7045
7046/*
7047 * ":edit", ":badd", ":visual".
Bram Moolenaar071d4272004-06-13 20:20:40 +00007048 */
7049 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007050ex_edit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007051{
7052 do_exedit(eap, NULL);
7053}
7054
7055/*
7056 * ":edit <file>" command and alikes.
7057 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007058 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007059do_exedit(
7060 exarg_T *eap,
7061 win_T *old_curwin) /* curwin before doing a split or NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007062{
7063 int n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007064 int need_hide;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007065 int exmode_was = exmode_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007066
7067 /*
7068 * ":vi" command ends Ex mode.
7069 */
7070 if (exmode_active && (eap->cmdidx == CMD_visual
7071 || eap->cmdidx == CMD_view))
7072 {
7073 exmode_active = FALSE;
7074 if (*eap->arg == NUL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007075 {
7076 /* Special case: ":global/pat/visual\NLvi-commands" */
7077 if (global_busy)
7078 {
7079 int rd = RedrawingDisabled;
7080 int nwr = no_wait_return;
7081 int ms = msg_scroll;
7082#ifdef FEAT_GUI
7083 int he = hold_gui_events;
7084#endif
7085
7086 if (eap->nextcmd != NULL)
7087 {
7088 stuffReadbuff(eap->nextcmd);
7089 eap->nextcmd = NULL;
7090 }
7091
7092 if (exmode_was != EXMODE_VIM)
7093 settmode(TMODE_RAW);
7094 RedrawingDisabled = 0;
7095 no_wait_return = 0;
7096 need_wait_return = FALSE;
7097 msg_scroll = 0;
7098#ifdef FEAT_GUI
7099 hold_gui_events = 0;
7100#endif
7101 must_redraw = CLEAR;
7102
7103 main_loop(FALSE, TRUE);
7104
7105 RedrawingDisabled = rd;
7106 no_wait_return = nwr;
7107 msg_scroll = ms;
7108#ifdef FEAT_GUI
7109 hold_gui_events = he;
7110#endif
7111 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007112 return;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007113 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007114 }
7115
7116 if ((eap->cmdidx == CMD_new
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007117 || eap->cmdidx == CMD_tabnew
7118 || eap->cmdidx == CMD_tabedit
Bram Moolenaar4033c552017-09-16 20:54:51 +02007119 || eap->cmdidx == CMD_vnew) && *eap->arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007120 {
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007121 /* ":new" or ":tabnew" without argument: edit an new empty buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007122 setpcmark();
7123 (void)do_ecmd(0, NULL, NULL, eap, ECMD_ONE,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00007124 ECMD_HIDE + (eap->forceit ? ECMD_FORCEIT : 0),
7125 old_curwin == NULL ? curwin : NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007126 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02007127 else if ((eap->cmdidx != CMD_split && eap->cmdidx != CMD_vsplit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007128 || *eap->arg != NUL
7129#ifdef FEAT_BROWSE
7130 || cmdmod.browse
7131#endif
7132 )
7133 {
Bram Moolenaar5555acc2006-04-07 21:33:12 +00007134 /* Can't edit another file when "curbuf_lock" is set. Only ":edit"
7135 * can bring us here, others are stopped earlier. */
7136 if (*eap->arg != NUL && curbuf_locked())
7137 return;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007138
Bram Moolenaar071d4272004-06-13 20:20:40 +00007139 n = readonlymode;
7140 if (eap->cmdidx == CMD_view || eap->cmdidx == CMD_sview)
7141 readonlymode = TRUE;
7142 else if (eap->cmdidx == CMD_enew)
7143 readonlymode = FALSE; /* 'readonly' doesn't make sense in an
7144 empty buffer */
7145 setpcmark();
7146 if (do_ecmd(0, (eap->cmdidx == CMD_enew ? NULL : eap->arg),
7147 NULL, eap,
7148 /* ":edit" goes to first line if Vi compatible */
7149 (*eap->arg == NUL && eap->do_ecmd_lnum == 0
7150 && vim_strchr(p_cpo, CPO_GOTO1) != NULL)
7151 ? ECMD_ONE : eap->do_ecmd_lnum,
Bram Moolenaareb44a682017-08-03 22:44:55 +02007152 (buf_hide(curbuf) ? ECMD_HIDE : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007153 + (eap->forceit ? ECMD_FORCEIT : 0)
Bram Moolenaar7b449342014-03-25 13:03:48 +01007154 /* after a split we can use an existing buffer */
7155 + (old_curwin != NULL ? ECMD_OLDBUF : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007156 + (eap->cmdidx == CMD_badd ? ECMD_ADDBUF : 0 )
Bram Moolenaar701f7af2008-11-15 13:12:07 +00007157 , old_curwin == NULL ? curwin : NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007158 {
7159 /* Editing the file failed. If the window was split, close it. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007160 if (old_curwin != NULL)
7161 {
7162 need_hide = (curbufIsChanged() && curbuf->b_nwindows <= 1);
Bram Moolenaareb44a682017-08-03 22:44:55 +02007163 if (!need_hide || buf_hide(curbuf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007164 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007165#if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007166 cleanup_T cs;
7167
7168 /* Reset the error/interrupt/exception state here so that
7169 * aborting() returns FALSE when closing a window. */
7170 enter_cleanup(&cs);
Bram Moolenaar4033c552017-09-16 20:54:51 +02007171#endif
7172#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007173 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02007174#endif
Bram Moolenaareb44a682017-08-03 22:44:55 +02007175 win_close(curwin, !need_hide && !buf_hide(curbuf));
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007176
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007177#if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007178 /* Restore the error/interrupt/exception state if not
7179 * discarded by a new aborting error, interrupt, or
7180 * uncaught exception. */
7181 leave_cleanup(&cs);
Bram Moolenaar4033c552017-09-16 20:54:51 +02007182#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007183 }
7184 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007185 }
7186 else if (readonlymode && curbuf->b_nwindows == 1)
7187 {
7188 /* When editing an already visited buffer, 'readonly' won't be set
7189 * but the previous value is kept. With ":view" and ":sview" we
7190 * want the file to be readonly, except when another window is
7191 * editing the same buffer. */
7192 curbuf->b_p_ro = TRUE;
7193 }
7194 readonlymode = n;
7195 }
7196 else
7197 {
7198 if (eap->do_ecmd_cmd != NULL)
7199 do_cmdline_cmd(eap->do_ecmd_cmd);
7200#ifdef FEAT_TITLE
7201 n = curwin->w_arg_idx_invalid;
7202#endif
7203 check_arg_idx(curwin);
7204#ifdef FEAT_TITLE
7205 if (n != curwin->w_arg_idx_invalid)
7206 maketitle();
7207#endif
7208 }
7209
Bram Moolenaar071d4272004-06-13 20:20:40 +00007210 /*
7211 * if ":split file" worked, set alternate file name in old window to new
7212 * file
7213 */
7214 if (old_curwin != NULL
7215 && *eap->arg != NUL
7216 && curwin != old_curwin
7217 && win_valid(old_curwin)
Bram Moolenaard4755bb2004-09-02 19:12:26 +00007218 && old_curwin->w_buffer != curbuf
7219 && !cmdmod.keepalt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007220 old_curwin->w_alt_fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007221
7222 ex_no_reprint = TRUE;
7223}
7224
7225#ifndef FEAT_GUI
7226/*
7227 * ":gui" and ":gvim" when there is no GUI.
7228 */
7229 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007230ex_nogui(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007231{
7232 eap->errmsg = e_nogvim;
7233}
7234#endif
7235
Bram Moolenaar4f974752019-02-17 17:44:42 +01007236#if defined(FEAT_GUI_MSWIN) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007237 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007238ex_tearoff(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007239{
7240 gui_make_tearoff(eap->arg);
7241}
7242#endif
7243
Bram Moolenaar29a2c082018-03-05 21:06:23 +01007244#if (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK) \
7245 || defined(FEAT_TERM_POPUP_MENU)) && defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007246 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007247ex_popup(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007248{
Bram Moolenaar29a2c082018-03-05 21:06:23 +01007249# if defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK)
7250 if (gui.in_use)
7251 gui_make_popup(eap->arg, eap->forceit);
7252# ifdef FEAT_TERM_POPUP_MENU
7253 else
7254# endif
7255# endif
7256# ifdef FEAT_TERM_POPUP_MENU
7257 pum_make_popup(eap->arg, eap->forceit);
7258# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007259}
7260#endif
7261
Bram Moolenaar071d4272004-06-13 20:20:40 +00007262 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007263ex_swapname(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007264{
7265 if (curbuf->b_ml.ml_mfp == NULL || curbuf->b_ml.ml_mfp->mf_fname == NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01007266 msg(_("No swap file"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007267 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01007268 msg((char *)curbuf->b_ml.ml_mfp->mf_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007269}
7270
7271/*
7272 * ":syncbind" forces all 'scrollbind' windows to have the same relative
7273 * offset.
7274 * (1998-11-02 16:21:01 R. Edward Ralston <eralston@computer.org>)
7275 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007276 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007277ex_syncbind(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007278{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007279 win_T *wp;
Bram Moolenaardedd1b02013-12-14 13:06:17 +01007280 win_T *save_curwin = curwin;
7281 buf_T *save_curbuf = curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007282 long topline;
7283 long y;
7284 linenr_T old_linenr = curwin->w_cursor.lnum;
7285
7286 setpcmark();
7287
7288 /*
7289 * determine max topline
7290 */
7291 if (curwin->w_p_scb)
7292 {
7293 topline = curwin->w_topline;
Bram Moolenaar29323592016-07-24 22:04:11 +02007294 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007295 {
7296 if (wp->w_p_scb && wp->w_buffer)
7297 {
Bram Moolenaar375e3392019-01-31 18:26:10 +01007298 y = wp->w_buffer->b_ml.ml_line_count - get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007299 if (topline > y)
7300 topline = y;
7301 }
7302 }
7303 if (topline < 1)
7304 topline = 1;
7305 }
7306 else
7307 {
7308 topline = 1;
7309 }
7310
7311
7312 /*
Bram Moolenaardedd1b02013-12-14 13:06:17 +01007313 * Set all scrollbind windows to the same topline.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007314 */
Bram Moolenaar29323592016-07-24 22:04:11 +02007315 FOR_ALL_WINDOWS(curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007316 {
7317 if (curwin->w_p_scb)
7318 {
Bram Moolenaardedd1b02013-12-14 13:06:17 +01007319 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007320 y = topline - curwin->w_topline;
7321 if (y > 0)
7322 scrollup(y, TRUE);
7323 else
7324 scrolldown(-y, TRUE);
7325 curwin->w_scbind_pos = topline;
7326 redraw_later(VALID);
7327 cursor_correct();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007328 curwin->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007329 }
7330 }
Bram Moolenaardedd1b02013-12-14 13:06:17 +01007331 curwin = save_curwin;
7332 curbuf = save_curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007333 if (curwin->w_p_scb)
7334 {
7335 did_syncbind = TRUE;
7336 checkpcmark();
7337 if (old_linenr != curwin->w_cursor.lnum)
7338 {
7339 char_u ctrl_o[2];
7340
7341 ctrl_o[0] = Ctrl_O;
7342 ctrl_o[1] = 0;
7343 ins_typebuf(ctrl_o, REMAP_NONE, 0, TRUE, FALSE);
7344 }
7345 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007346}
7347
7348
7349 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007350ex_read(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007351{
Bram Moolenaardf177f62005-02-22 08:39:57 +00007352 int i;
7353 int empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
7354 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007355
7356 if (eap->usefilter) /* :r!cmd */
7357 do_bang(1, eap, FALSE, FALSE, TRUE);
7358 else
7359 {
7360 if (u_save(eap->line2, (linenr_T)(eap->line2 + 1)) == FAIL)
7361 return;
7362
7363#ifdef FEAT_BROWSE
7364 if (cmdmod.browse)
7365 {
7366 char_u *browseFile;
7367
Bram Moolenaar7171abe2004-10-11 10:06:20 +00007368 browseFile = do_browse(0, (char_u *)_("Append File"), eap->arg,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007369 NULL, NULL, NULL, curbuf);
7370 if (browseFile != NULL)
7371 {
7372 i = readfile(browseFile, NULL,
7373 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
7374 vim_free(browseFile);
7375 }
7376 else
7377 i = OK;
7378 }
7379 else
7380#endif
7381 if (*eap->arg == NUL)
7382 {
7383 if (check_fname() == FAIL) /* check for no file name */
7384 return;
7385 i = readfile(curbuf->b_ffname, curbuf->b_fname,
7386 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
7387 }
7388 else
7389 {
7390 if (vim_strchr(p_cpo, CPO_ALTREAD) != NULL)
7391 (void)setaltfname(eap->arg, eap->arg, (linenr_T)1);
7392 i = readfile(eap->arg, NULL,
7393 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
7394
7395 }
Bram Moolenaare13b9af2017-01-13 22:01:02 +01007396 if (i != OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007397 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007398#if defined(FEAT_EVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007399 if (!aborting())
7400#endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007401 semsg(_(e_notopen), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007402 }
7403 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00007404 {
7405 if (empty && exmode_active)
7406 {
7407 /* Delete the empty line that remains. Historically ex does
7408 * this but vi doesn't. */
7409 if (eap->line2 == 0)
7410 lnum = curbuf->b_ml.ml_line_count;
7411 else
7412 lnum = 1;
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00007413 if (*ml_get(lnum) == NUL && u_savedel(lnum, 1L) == OK)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007414 {
7415 ml_delete(lnum, FALSE);
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00007416 if (curwin->w_cursor.lnum > 1
7417 && curwin->w_cursor.lnum >= lnum)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007418 --curwin->w_cursor.lnum;
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00007419 deleted_lines_mark(lnum, 1L);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007420 }
7421 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007422 redraw_curbuf_later(VALID);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007423 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007424 }
7425}
7426
Bram Moolenaarea408852005-06-25 22:49:46 +00007427static char_u *prev_dir = NULL;
7428
7429#if defined(EXITFREE) || defined(PROTO)
7430 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007431free_cd_dir(void)
Bram Moolenaarea408852005-06-25 22:49:46 +00007432{
Bram Moolenaard23a8232018-02-10 18:45:26 +01007433 VIM_CLEAR(prev_dir);
7434 VIM_CLEAR(globaldir);
Bram Moolenaarea408852005-06-25 22:49:46 +00007435}
7436#endif
7437
Bram Moolenaarf4258302013-06-02 18:20:17 +02007438/*
7439 * Deal with the side effects of changing the current directory.
Bram Moolenaar00aa0692019-04-27 20:37:57 +02007440 * When "tablocal" is TRUE then this was after an ":tcd" command.
7441 * When "winlocal" is TRUE then this was after an ":lcd" command.
Bram Moolenaarf4258302013-06-02 18:20:17 +02007442 */
7443 void
Bram Moolenaar00aa0692019-04-27 20:37:57 +02007444post_chdir(int tablocal, int winlocal)
Bram Moolenaarf4258302013-06-02 18:20:17 +02007445{
Bram Moolenaar00aa0692019-04-27 20:37:57 +02007446 if (!winlocal)
7447 // Clear tab local directory for both :cd and :tcd
7448 VIM_CLEAR(curtab->tp_localdir);
Bram Moolenaard23a8232018-02-10 18:45:26 +01007449 VIM_CLEAR(curwin->w_localdir);
Bram Moolenaar00aa0692019-04-27 20:37:57 +02007450 if (winlocal || tablocal)
Bram Moolenaarf4258302013-06-02 18:20:17 +02007451 {
7452 /* If still in global directory, need to remember current
7453 * directory as global directory. */
7454 if (globaldir == NULL && prev_dir != NULL)
7455 globaldir = vim_strsave(prev_dir);
7456 /* Remember this local directory for the window. */
7457 if (mch_dirname(NameBuff, MAXPATHL) == OK)
Bram Moolenaar00aa0692019-04-27 20:37:57 +02007458 {
7459 if (tablocal)
7460 curtab->tp_localdir = vim_strsave(NameBuff);
7461 else
7462 curwin->w_localdir = vim_strsave(NameBuff);
7463 }
Bram Moolenaarf4258302013-06-02 18:20:17 +02007464 }
7465 else
7466 {
7467 /* We are now in the global directory, no need to remember its
7468 * name. */
Bram Moolenaard23a8232018-02-10 18:45:26 +01007469 VIM_CLEAR(globaldir);
Bram Moolenaarf4258302013-06-02 18:20:17 +02007470 }
7471
7472 shorten_fnames(TRUE);
7473}
7474
Bram Moolenaarea408852005-06-25 22:49:46 +00007475
Bram Moolenaar071d4272004-06-13 20:20:40 +00007476/*
Bram Moolenaar00aa0692019-04-27 20:37:57 +02007477 * ":cd", ":tcd", ":lcd", ":chdir" ":tchdir" and ":lchdir".
Bram Moolenaar071d4272004-06-13 20:20:40 +00007478 */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00007479 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007480ex_cd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007481{
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01007482 char_u *new_dir;
7483 char_u *tofree;
7484 int dir_differs;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007485
7486 new_dir = eap->arg;
7487#if !defined(UNIX) && !defined(VMS)
7488 /* for non-UNIX ":cd" means: print current directory */
7489 if (*new_dir == NUL)
7490 ex_pwd(NULL);
7491 else
7492#endif
7493 {
Bram Moolenaar8e8fe9b2009-03-11 14:37:32 +00007494 if (allbuf_locked())
7495 return;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007496 if (vim_strchr(p_cpo, CPO_CHDIR) != NULL && curbufIsChanged()
7497 && !eap->forceit)
7498 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007499 emsg(_("E747: Cannot change directory, buffer is modified (add ! to override)"));
Bram Moolenaardf177f62005-02-22 08:39:57 +00007500 return;
7501 }
7502
Bram Moolenaar071d4272004-06-13 20:20:40 +00007503 /* ":cd -": Change to previous directory */
7504 if (STRCMP(new_dir, "-") == 0)
7505 {
7506 if (prev_dir == NULL)
7507 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007508 emsg(_("E186: No previous directory"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007509 return;
7510 }
7511 new_dir = prev_dir;
7512 }
7513
7514 /* Save current directory for next ":cd -" */
7515 tofree = prev_dir;
7516 if (mch_dirname(NameBuff, MAXPATHL) == OK)
7517 prev_dir = vim_strsave(NameBuff);
7518 else
7519 prev_dir = NULL;
7520
7521#if defined(UNIX) || defined(VMS)
7522 /* for UNIX ":cd" means: go to home directory */
7523 if (*new_dir == NUL)
7524 {
7525 /* use NameBuff for home directory name */
7526# ifdef VMS
7527 char_u *p;
7528
7529 p = mch_getenv((char_u *)"SYS$LOGIN");
7530 if (p == NULL || *p == NUL) /* empty is the same as not set */
7531 NameBuff[0] = NUL;
7532 else
Bram Moolenaarb6356332005-07-18 21:40:44 +00007533 vim_strncpy(NameBuff, p, MAXPATHL - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007534# else
7535 expand_env((char_u *)"$HOME", NameBuff, MAXPATHL);
7536# endif
7537 new_dir = NameBuff;
7538 }
7539#endif
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01007540 dir_differs = new_dir == NULL || prev_dir == NULL
Bram Moolenaar9eb76af2018-12-16 16:30:21 +01007541 || pathcmp((char *)prev_dir, (char *)new_dir, -1) != 0;
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01007542 if (new_dir == NULL || (dir_differs && vim_chdir(new_dir)))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007543 emsg(_(e_failed));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007544 else
7545 {
Bram Moolenaar00aa0692019-04-27 20:37:57 +02007546 char_u *acmd_fname;
7547 int is_winlocal_chdir = eap->cmdidx == CMD_lcd
Bram Moolenaarb7407d32018-02-03 17:36:27 +01007548 || eap->cmdidx == CMD_lchdir;
Bram Moolenaar00aa0692019-04-27 20:37:57 +02007549 int is_tablocal_chdir = eap->cmdidx == CMD_tcd
7550 || eap->cmdidx == CMD_tchdir;
Bram Moolenaarb7407d32018-02-03 17:36:27 +01007551
Bram Moolenaar00aa0692019-04-27 20:37:57 +02007552 post_chdir(is_tablocal_chdir, is_winlocal_chdir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007553
7554 /* Echo the new current directory if the command was typed. */
Bram Moolenaarfcfbc672009-07-09 18:13:49 +00007555 if (KeyTyped || p_verbose >= 5)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007556 ex_pwd(eap);
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01007557
7558 if (dir_differs)
Bram Moolenaar00aa0692019-04-27 20:37:57 +02007559 {
7560 if (is_winlocal_chdir)
7561 acmd_fname = (char_u *)"window";
7562 else if (is_tablocal_chdir)
7563 acmd_fname = (char_u *)"tabpage";
7564 else
7565 acmd_fname = (char_u *)"global";
7566 apply_autocmds(EVENT_DIRCHANGED, acmd_fname,
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01007567 new_dir, FALSE, curbuf);
Bram Moolenaar00aa0692019-04-27 20:37:57 +02007568 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007569 }
7570 vim_free(tofree);
7571 }
7572}
7573
7574/*
7575 * ":pwd".
7576 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007577 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007578ex_pwd(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007579{
7580 if (mch_dirname(NameBuff, MAXPATHL) == OK)
7581 {
7582#ifdef BACKSLASH_IN_FILENAME
7583 slash_adjust(NameBuff);
7584#endif
Bram Moolenaar32526b32019-01-19 17:43:09 +01007585 msg((char *)NameBuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007586 }
7587 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007588 emsg(_("E187: Unknown"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007589}
7590
7591/*
7592 * ":=".
7593 */
7594 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007595ex_equal(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007596{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007597 smsg("%ld", (long)eap->line2);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007598 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007599}
7600
7601 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007602ex_sleep(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007603{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007604 int n;
7605 long len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007606
7607 if (cursor_valid())
7608 {
7609 n = W_WINROW(curwin) + curwin->w_wrow - msg_scrolled;
7610 if (n >= 0)
Bram Moolenaar53f81742017-09-22 14:35:51 +02007611 windgoto((int)n, curwin->w_wincol + curwin->w_wcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007612 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007613
7614 len = eap->line2;
7615 switch (*eap->arg)
7616 {
7617 case 'm': break;
7618 case NUL: len *= 1000L; break;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007619 default: semsg(_(e_invarg2), eap->arg); return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007620 }
7621 do_sleep(len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007622}
7623
7624/*
7625 * Sleep for "msec" milliseconds, but keep checking for a CTRL-C every second.
7626 */
7627 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007628do_sleep(long msec)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007629{
7630 long done;
Bram Moolenaar975b5272016-03-15 23:10:59 +01007631 long wait_now;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007632
7633 cursor_on();
Bram Moolenaarf45d7472018-09-30 18:22:26 +02007634 out_flush_cursor(FALSE, FALSE);
Bram Moolenaar975b5272016-03-15 23:10:59 +01007635 for (done = 0; !got_int && done < msec; done += wait_now)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007636 {
Bram Moolenaar975b5272016-03-15 23:10:59 +01007637 wait_now = msec - done > 1000L ? 1000L : msec - done;
7638#ifdef FEAT_TIMERS
7639 {
7640 long due_time = check_due_timer();
7641
7642 if (due_time > 0 && due_time < wait_now)
7643 wait_now = due_time;
7644 }
7645#endif
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02007646#ifdef FEAT_JOB_CHANNEL
7647 if (has_any_channel() && wait_now > 100L)
7648 wait_now = 100L;
7649#endif
Bram Moolenaar975b5272016-03-15 23:10:59 +01007650 ui_delay(wait_now, TRUE);
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02007651#ifdef FEAT_JOB_CHANNEL
7652 if (has_any_channel())
7653 ui_breakcheck_force(TRUE);
7654 else
7655#endif
7656 ui_breakcheck();
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007657#ifdef MESSAGE_QUEUE
7658 /* Process the netbeans and clientserver messages that may have been
7659 * received in the call to ui_breakcheck() when the GUI is in use. This
7660 * may occur when running a test case. */
7661 parse_queued_messages();
Bram Moolenaare3cc6d42011-10-20 21:58:34 +02007662#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007663 }
Bram Moolenaar1ebff3d2018-07-07 16:18:13 +02007664
7665 // If CTRL-C was typed to interrupt the sleep, drop the CTRL-C from the
7666 // input buffer, otherwise a following call to input() fails.
7667 if (got_int)
7668 (void)vpeekc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007669}
7670
7671 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007672do_exmap(exarg_T *eap, int isabbrev)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007673{
7674 int mode;
7675 char_u *cmdp;
7676
7677 cmdp = eap->cmd;
7678 mode = get_map_mode(&cmdp, eap->forceit || isabbrev);
7679
7680 switch (do_map((*cmdp == 'n') ? 2 : (*cmdp == 'u'),
7681 eap->arg, mode, isabbrev))
7682 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007683 case 1: emsg(_(e_invarg));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007684 break;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007685 case 2: emsg((isabbrev ? _(e_noabbr) : _(e_nomap)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007686 break;
7687 }
7688}
7689
7690/*
7691 * ":winsize" command (obsolete).
7692 */
7693 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007694ex_winsize(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007695{
7696 int w, h;
7697 char_u *arg = eap->arg;
7698 char_u *p;
7699
7700 w = getdigits(&arg);
7701 arg = skipwhite(arg);
7702 p = arg;
7703 h = getdigits(&arg);
7704 if (*p != NUL && *arg == NUL)
7705 set_shellsize(w, h, TRUE);
7706 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007707 emsg(_("E465: :winsize requires two number arguments"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007708}
7709
Bram Moolenaar071d4272004-06-13 20:20:40 +00007710 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007711ex_wincmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007712{
7713 int xchar = NUL;
7714 char_u *p;
7715
7716 if (*eap->arg == 'g' || *eap->arg == Ctrl_G)
7717 {
7718 /* CTRL-W g and CTRL-W CTRL-G have an extra command character */
7719 if (eap->arg[1] == NUL)
7720 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007721 emsg(_(e_invarg));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007722 return;
7723 }
7724 xchar = eap->arg[1];
7725 p = eap->arg + 2;
7726 }
7727 else
7728 p = eap->arg + 1;
7729
7730 eap->nextcmd = check_nextcmd(p);
7731 p = skipwhite(p);
7732 if (*p != NUL && *p != '"' && eap->nextcmd == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007733 emsg(_(e_invarg));
Bram Moolenaar12bde492011-06-13 01:19:56 +02007734 else if (!eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007735 {
7736 /* Pass flags on for ":vertical wincmd ]". */
7737 postponed_split_flags = cmdmod.split;
Bram Moolenaard326ce82007-03-11 14:48:29 +00007738 postponed_split_tab = cmdmod.tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007739 do_window(*eap->arg, eap->addr_count > 0 ? eap->line2 : 0L, xchar);
7740 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00007741 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007742 }
7743}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007744
Bram Moolenaar843ee412004-06-30 16:16:41 +00007745#if defined(FEAT_GUI) || defined(UNIX) || defined(VMS) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007746/*
7747 * ":winpos".
7748 */
7749 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007750ex_winpos(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007751{
7752 int x, y;
7753 char_u *arg = eap->arg;
7754 char_u *p;
7755
7756 if (*arg == NUL)
7757 {
Bram Moolenaar843ee412004-06-30 16:16:41 +00007758# if defined(FEAT_GUI) || defined(MSWIN)
7759# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007760 if (gui.in_use && gui_mch_get_winpos(&x, &y) != FAIL)
Bram Moolenaar843ee412004-06-30 16:16:41 +00007761# else
7762 if (mch_get_winpos(&x, &y) != FAIL)
7763# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007764 {
7765 sprintf((char *)IObuff, _("Window position: X %d, Y %d"), x, y);
Bram Moolenaar32526b32019-01-19 17:43:09 +01007766 msg((char *)IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007767 }
7768 else
7769# endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007770 emsg(_("E188: Obtaining window position not implemented for this platform"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007771 }
7772 else
7773 {
7774 x = getdigits(&arg);
7775 arg = skipwhite(arg);
7776 p = arg;
7777 y = getdigits(&arg);
7778 if (*p == NUL || *arg != NUL)
7779 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007780 emsg(_("E466: :winpos requires two number arguments"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007781 return;
7782 }
7783# ifdef FEAT_GUI
7784 if (gui.in_use)
7785 gui_mch_set_winpos(x, y);
7786 else if (gui.starting)
7787 {
7788 /* Remember the coordinates for when the window is opened. */
7789 gui_win_x = x;
7790 gui_win_y = y;
7791 }
7792# ifdef HAVE_TGETENT
7793 else
7794# endif
Bram Moolenaar843ee412004-06-30 16:16:41 +00007795# else
7796# ifdef MSWIN
7797 mch_set_winpos(x, y);
7798# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007799# endif
7800# ifdef HAVE_TGETENT
7801 if (*T_CWP)
7802 term_set_winpos(x, y);
7803# endif
7804 }
7805}
7806#endif
7807
7808/*
7809 * Handle command that work like operators: ":delete", ":yank", ":>" and ":<".
7810 */
7811 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007812ex_operators(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007813{
7814 oparg_T oa;
7815
7816 clear_oparg(&oa);
7817 oa.regname = eap->regname;
7818 oa.start.lnum = eap->line1;
7819 oa.end.lnum = eap->line2;
7820 oa.line_count = eap->line2 - eap->line1 + 1;
7821 oa.motion_type = MLINE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007822 virtual_op = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007823 if (eap->cmdidx != CMD_yank) /* position cursor for undo */
7824 {
7825 setpcmark();
7826 curwin->w_cursor.lnum = eap->line1;
7827 beginline(BL_SOL | BL_FIX);
7828 }
7829
Bram Moolenaard07c6e12013-11-21 14:21:40 +01007830 if (VIsual_active)
7831 end_visual_mode();
Bram Moolenaard07c6e12013-11-21 14:21:40 +01007832
Bram Moolenaar071d4272004-06-13 20:20:40 +00007833 switch (eap->cmdidx)
7834 {
7835 case CMD_delete:
7836 oa.op_type = OP_DELETE;
7837 op_delete(&oa);
7838 break;
7839
7840 case CMD_yank:
7841 oa.op_type = OP_YANK;
7842 (void)op_yank(&oa, FALSE, TRUE);
7843 break;
7844
7845 default: /* CMD_rshift or CMD_lshift */
Bram Moolenaar6e707362013-06-14 19:15:58 +02007846 if (
Bram Moolenaar071d4272004-06-13 20:20:40 +00007847#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6e707362013-06-14 19:15:58 +02007848 (eap->cmdidx == CMD_rshift) ^ curwin->w_p_rl
7849#else
7850 eap->cmdidx == CMD_rshift
Bram Moolenaar071d4272004-06-13 20:20:40 +00007851#endif
Bram Moolenaar6e707362013-06-14 19:15:58 +02007852 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00007853 oa.op_type = OP_RSHIFT;
7854 else
7855 oa.op_type = OP_LSHIFT;
7856 op_shift(&oa, FALSE, eap->amount);
7857 break;
7858 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007859 virtual_op = MAYBE;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007860 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007861}
7862
7863/*
7864 * ":put".
7865 */
7866 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007867ex_put(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007868{
7869 /* ":0put" works like ":1put!". */
7870 if (eap->line2 == 0)
7871 {
7872 eap->line2 = 1;
7873 eap->forceit = TRUE;
7874 }
7875 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007876 do_put(eap->regname, eap->forceit ? BACKWARD : FORWARD, 1L,
7877 PUT_LINE|PUT_CURSLINE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007878}
7879
7880/*
7881 * Handle ":copy" and ":move".
7882 */
7883 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007884ex_copymove(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007885{
7886 long n;
7887
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02007888 n = get_address(eap, &eap->arg, eap->addr_type, FALSE, FALSE, FALSE, 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007889 if (eap->arg == NULL) /* error detected */
7890 {
7891 eap->nextcmd = NULL;
7892 return;
7893 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00007894 get_flags(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007895
7896 /*
7897 * move or copy lines from 'eap->line1'-'eap->line2' to below line 'n'
7898 */
7899 if (n == MAXLNUM || n < 0 || n > curbuf->b_ml.ml_line_count)
7900 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007901 emsg(_(e_invaddr));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007902 return;
7903 }
7904
7905 if (eap->cmdidx == CMD_move)
7906 {
7907 if (do_move(eap->line1, eap->line2, n) == FAIL)
7908 return;
7909 }
7910 else
7911 ex_copy(eap->line1, eap->line2, n);
7912 u_clearline();
7913 beginline(BL_SOL | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007914 ex_may_print(eap);
7915}
7916
7917/*
7918 * Print the current line if flags were given to the Ex command.
7919 */
Bram Moolenaarfd3fe982014-04-01 17:49:44 +02007920 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007921ex_may_print(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007922{
7923 if (eap->flags != 0)
7924 {
7925 print_line(curwin->w_cursor.lnum, (eap->flags & EXFLAG_NR),
7926 (eap->flags & EXFLAG_LIST));
7927 ex_no_reprint = TRUE;
7928 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007929}
7930
7931/*
7932 * ":smagic" and ":snomagic".
7933 */
7934 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007935ex_submagic(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007936{
7937 int magic_save = p_magic;
7938
7939 p_magic = (eap->cmdidx == CMD_smagic);
7940 do_sub(eap);
7941 p_magic = magic_save;
7942}
7943
7944/*
7945 * ":join".
7946 */
7947 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007948ex_join(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007949{
7950 curwin->w_cursor.lnum = eap->line1;
7951 if (eap->line1 == eap->line2)
7952 {
7953 if (eap->addr_count >= 2) /* :2,2join does nothing */
7954 return;
7955 if (eap->line2 == curbuf->b_ml.ml_line_count)
7956 {
7957 beep_flush();
7958 return;
7959 }
7960 ++eap->line2;
7961 }
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02007962 (void)do_join(eap->line2 - eap->line1 + 1, !eap->forceit, TRUE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007963 beginline(BL_WHITE | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007964 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007965}
7966
7967/*
7968 * ":[addr]@r" or ":[addr]*r": execute register
7969 */
7970 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007971ex_at(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007972{
7973 int c;
Bram Moolenaar60462872009-11-03 11:40:19 +00007974 int prev_len = typebuf.tb_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007975
7976 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaar4930a762016-09-11 14:39:53 +02007977 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007978
7979#ifdef USE_ON_FLY_SCROLL
7980 dont_scroll = TRUE; /* disallow scrolling here */
7981#endif
7982
7983 /* get the register name. No name means to use the previous one */
7984 c = *eap->arg;
7985 if (c == NUL || (c == '*' && *eap->cmd == '*'))
7986 c = '@';
Bram Moolenaard333d1e2006-11-07 17:43:47 +00007987 /* Put the register in the typeahead buffer with the "silent" flag. */
7988 if (do_execreg(c, TRUE, vim_strchr(p_cpo, CPO_EXECBUF) != NULL, TRUE)
7989 == FAIL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007990 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007991 beep_flush();
Bram Moolenaardf177f62005-02-22 08:39:57 +00007992 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007993 else
7994 {
7995 int save_efr = exec_from_reg;
7996
7997 exec_from_reg = TRUE;
7998
7999 /*
8000 * Execute from the typeahead buffer.
Bram Moolenaar60462872009-11-03 11:40:19 +00008001 * Continue until the stuff buffer is empty and all added characters
8002 * have been consumed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008003 */
Bram Moolenaar60462872009-11-03 11:40:19 +00008004 while (!stuff_empty() || typebuf.tb_len > prev_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008005 (void)do_cmdline(NULL, getexline, NULL, DOCMD_NOWAIT|DOCMD_VERBOSE);
8006
8007 exec_from_reg = save_efr;
8008 }
8009}
8010
8011/*
8012 * ":!".
8013 */
8014 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008015ex_bang(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008016{
8017 do_bang(eap->addr_count, eap, eap->forceit, TRUE, TRUE);
8018}
8019
8020/*
8021 * ":undo".
8022 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008023 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +01008024ex_undo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008025{
Bram Moolenaard3667a22006-03-16 21:35:52 +00008026 if (eap->addr_count == 1) /* :undo 123 */
Bram Moolenaar730cde92010-06-27 05:18:54 +02008027 undo_time(eap->line2, FALSE, FALSE, TRUE);
Bram Moolenaard3667a22006-03-16 21:35:52 +00008028 else
8029 u_undo(1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008030}
8031
Bram Moolenaar55debbe2010-05-23 23:34:36 +02008032#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar6df6f472010-07-18 18:04:50 +02008033 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008034ex_wundo(exarg_T *eap)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02008035{
8036 char_u hash[UNDO_HASH_SIZE];
8037
8038 u_compute_hash(hash);
8039 u_write_undo(eap->arg, eap->forceit, curbuf, hash);
8040}
8041
Bram Moolenaar6df6f472010-07-18 18:04:50 +02008042 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008043ex_rundo(exarg_T *eap)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02008044{
8045 char_u hash[UNDO_HASH_SIZE];
8046
8047 u_compute_hash(hash);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02008048 u_read_undo(eap->arg, hash, NULL);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02008049}
8050#endif
8051
Bram Moolenaar071d4272004-06-13 20:20:40 +00008052/*
8053 * ":redo".
8054 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008055 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008056ex_redo(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008057{
8058 u_redo(1);
8059}
8060
8061/*
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008062 * ":earlier" and ":later".
8063 */
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008064 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008065ex_later(exarg_T *eap)
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008066{
8067 long count = 0;
8068 int sec = FALSE;
Bram Moolenaar730cde92010-06-27 05:18:54 +02008069 int file = FALSE;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008070 char_u *p = eap->arg;
8071
8072 if (*p == NUL)
8073 count = 1;
8074 else if (isdigit(*p))
8075 {
8076 count = getdigits(&p);
8077 switch (*p)
8078 {
8079 case 's': ++p; sec = TRUE; break;
8080 case 'm': ++p; sec = TRUE; count *= 60; break;
8081 case 'h': ++p; sec = TRUE; count *= 60 * 60; break;
Bram Moolenaar730cde92010-06-27 05:18:54 +02008082 case 'd': ++p; sec = TRUE; count *= 24 * 60 * 60; break;
8083 case 'f': ++p; file = TRUE; break;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008084 }
8085 }
8086
8087 if (*p != NUL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008088 semsg(_(e_invarg2), eap->arg);
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008089 else
Bram Moolenaar730cde92010-06-27 05:18:54 +02008090 undo_time(eap->cmdidx == CMD_earlier ? -count : count,
8091 sec, file, FALSE);
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008092}
8093
8094/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008095 * ":redir": start/stop redirection.
8096 */
8097 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008098ex_redir(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008099{
8100 char *mode;
8101 char_u *fname;
Bram Moolenaarca472992005-01-21 11:46:23 +00008102 char_u *arg = eap->arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008103
Bram Moolenaarba768492016-07-08 15:32:54 +02008104#ifdef FEAT_EVAL
Bram Moolenaar79815f12016-07-09 17:07:29 +02008105 if (redir_execute)
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02008106 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008107 emsg(_("E930: Cannot use :redir inside execute()"));
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02008108 return;
8109 }
Bram Moolenaarba768492016-07-08 15:32:54 +02008110#endif
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02008111
Bram Moolenaar071d4272004-06-13 20:20:40 +00008112 if (STRICMP(eap->arg, "END") == 0)
8113 close_redir();
8114 else
8115 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008116 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008117 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008118 ++arg;
8119 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008120 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008121 ++arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008122 mode = "a";
8123 }
8124 else
8125 mode = "w";
Bram Moolenaarca472992005-01-21 11:46:23 +00008126 arg = skipwhite(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008127
8128 close_redir();
8129
8130 /* Expand environment variables and "~/". */
Bram Moolenaarca472992005-01-21 11:46:23 +00008131 fname = expand_env_save(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008132 if (fname == NULL)
8133 return;
8134#ifdef FEAT_BROWSE
8135 if (cmdmod.browse)
8136 {
8137 char_u *browseFile;
8138
Bram Moolenaar7171abe2004-10-11 10:06:20 +00008139 browseFile = do_browse(BROWSE_SAVE,
8140 (char_u *)_("Save Redirection"),
Bram Moolenaarc36651b2018-04-29 12:22:56 +02008141 fname, NULL, NULL,
8142 (char_u *)_(BROWSE_FILTER_ALL_FILES), curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008143 if (browseFile == NULL)
8144 return; /* operation cancelled */
8145 vim_free(fname);
8146 fname = browseFile;
8147 eap->forceit = TRUE; /* since dialog already asked */
8148 }
8149#endif
8150
8151 redir_fd = open_exfile(fname, eap->forceit, mode);
8152 vim_free(fname);
8153 }
8154#ifdef FEAT_EVAL
Bram Moolenaarca472992005-01-21 11:46:23 +00008155 else if (*arg == '@')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008156 {
8157 /* redirect to a register a-z (resp. A-Z for appending) */
8158 close_redir();
Bram Moolenaarca472992005-01-21 11:46:23 +00008159 ++arg;
8160 if (ASCII_ISALPHA(*arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008161# ifdef FEAT_CLIPBOARD
Bram Moolenaarca472992005-01-21 11:46:23 +00008162 || *arg == '*'
Bram Moolenaar9a51c6e2006-11-14 19:25:02 +00008163 || *arg == '+'
Bram Moolenaar071d4272004-06-13 20:20:40 +00008164# endif
Bram Moolenaarca472992005-01-21 11:46:23 +00008165 || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008166 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008167 redir_reg = *arg++;
Bram Moolenaarc188b882007-10-19 14:20:54 +00008168 if (*arg == '>' && arg[1] == '>') /* append */
Bram Moolenaard9d30582005-05-18 22:10:28 +00008169 arg += 2;
Bram Moolenaarc188b882007-10-19 14:20:54 +00008170 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008171 {
Bram Moolenaarc188b882007-10-19 14:20:54 +00008172 /* Can use both "@a" and "@a>". */
Bram Moolenaar2c29bee2005-06-01 21:46:07 +00008173 if (*arg == '>')
8174 arg++;
Bram Moolenaarc188b882007-10-19 14:20:54 +00008175 /* Make register empty when not using @A-@Z and the
8176 * command is valid. */
8177 if (*arg == NUL && !isupper(redir_reg))
8178 write_reg_contents(redir_reg, (char_u *)"", -1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008179 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00008180 }
8181 if (*arg != NUL)
8182 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00008183 redir_reg = 0;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008184 semsg(_(e_invarg2), eap->arg);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008185 }
8186 }
8187 else if (*arg == '=' && arg[1] == '>')
8188 {
8189 int append;
8190
8191 /* redirect to a variable */
8192 close_redir();
8193 arg += 2;
8194
8195 if (*arg == '>')
8196 {
8197 ++arg;
8198 append = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008199 }
8200 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00008201 append = FALSE;
8202
8203 if (var_redir_start(skipwhite(arg), append) == OK)
8204 redir_vname = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008205 }
8206#endif
8207
8208 /* TODO: redirect to a buffer */
8209
Bram Moolenaar071d4272004-06-13 20:20:40 +00008210 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008211 semsg(_(e_invarg2), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008212 }
Bram Moolenaar29b2d262006-09-10 19:07:28 +00008213
8214 /* Make sure redirection is not off. Can happen for cmdline completion
8215 * that indirectly invokes a command to catch its output. */
8216 if (redir_fd != NULL
8217#ifdef FEAT_EVAL
8218 || redir_reg || redir_vname
8219#endif
8220 )
8221 redir_off = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008222}
8223
8224/*
8225 * ":redraw": force redraw
8226 */
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01008227 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008228ex_redraw(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008229{
8230 int r = RedrawingDisabled;
8231 int p = p_lz;
8232
8233 RedrawingDisabled = 0;
8234 p_lz = FALSE;
Bram Moolenaarabc39ab2017-03-01 18:04:05 +01008235 validate_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008236 update_topline();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01008237 update_screen(eap->forceit ? CLEAR : VIsual_active ? INVERTED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008238#ifdef FEAT_TITLE
8239 if (need_maketitle)
8240 maketitle();
8241#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +01008242#if defined(MSWIN) && !defined(FEAT_GUI_MSWIN)
Bram Moolenaar78d21da2019-02-17 15:00:52 +01008243 resize_console_buf();
8244#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008245 RedrawingDisabled = r;
8246 p_lz = p;
8247
8248 /* Reset msg_didout, so that a message that's there is overwritten. */
8249 msg_didout = FALSE;
8250 msg_col = 0;
8251
Bram Moolenaar56667a52013-07-17 11:54:28 +02008252 /* No need to wait after an intentional redraw. */
8253 need_wait_return = FALSE;
8254
Bram Moolenaar071d4272004-06-13 20:20:40 +00008255 out_flush();
8256}
8257
8258/*
8259 * ":redrawstatus": force redraw of status line(s)
8260 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008261 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008262ex_redrawstatus(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008263{
Bram Moolenaar071d4272004-06-13 20:20:40 +00008264 int r = RedrawingDisabled;
8265 int p = p_lz;
8266
8267 RedrawingDisabled = 0;
8268 p_lz = FALSE;
8269 if (eap->forceit)
8270 status_redraw_all();
8271 else
8272 status_redraw_curbuf();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01008273 update_screen(VIsual_active ? INVERTED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008274 RedrawingDisabled = r;
8275 p_lz = p;
8276 out_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008277}
8278
Bram Moolenaare12bab32019-01-08 22:02:56 +01008279/*
8280 * ":redrawtabline": force redraw of the tabline
8281 */
8282 static void
8283ex_redrawtabline(exarg_T *eap UNUSED)
8284{
8285 int r = RedrawingDisabled;
8286 int p = p_lz;
8287
8288 RedrawingDisabled = 0;
8289 p_lz = FALSE;
8290
8291 draw_tabline();
8292
8293 RedrawingDisabled = r;
8294 p_lz = p;
8295 out_flush();
8296}
8297
Bram Moolenaar071d4272004-06-13 20:20:40 +00008298 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008299close_redir(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008300{
8301 if (redir_fd != NULL)
8302 {
8303 fclose(redir_fd);
8304 redir_fd = NULL;
8305 }
8306#ifdef FEAT_EVAL
8307 redir_reg = 0;
Bram Moolenaardf177f62005-02-22 08:39:57 +00008308 if (redir_vname)
8309 {
8310 var_redir_stop();
8311 redir_vname = 0;
8312 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008313#endif
8314}
8315
8316#if defined(FEAT_SESSION) && defined(USE_CRNL)
8317# define MKSESSION_NL
8318static int mksession_nl = FALSE; /* use NL only in put_eol() */
8319#endif
8320
8321/*
8322 * ":mkexrc", ":mkvimrc", ":mkview" and ":mksession".
8323 */
8324 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008325ex_mkrc(
8326 exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008327{
8328 FILE *fd;
8329 int failed = FALSE;
8330 char_u *fname;
8331#ifdef FEAT_BROWSE
8332 char_u *browseFile = NULL;
8333#endif
8334#ifdef FEAT_SESSION
8335 int view_session = FALSE;
8336 int using_vdir = FALSE; /* using 'viewdir'? */
8337 char_u *viewFile = NULL;
8338 unsigned *flagp;
8339#endif
8340
8341 if (eap->cmdidx == CMD_mksession || eap->cmdidx == CMD_mkview)
8342 {
8343#ifdef FEAT_SESSION
8344 view_session = TRUE;
8345#else
8346 ex_ni(eap);
8347 return;
8348#endif
8349 }
8350
8351#ifdef FEAT_SESSION
Bram Moolenaar8d594672009-07-01 18:18:57 +00008352 /* Use the short file name until ":lcd" is used. We also don't use the
8353 * short file name when 'acd' is set, that is checked later. */
Bram Moolenaareeefcc72007-05-01 21:21:21 +00008354 did_lcd = FALSE;
8355
Bram Moolenaar071d4272004-06-13 20:20:40 +00008356 /* ":mkview" or ":mkview 9": generate file name with 'viewdir' */
8357 if (eap->cmdidx == CMD_mkview
8358 && (*eap->arg == NUL
8359 || (vim_isdigit(*eap->arg) && eap->arg[1] == NUL)))
8360 {
8361 eap->forceit = TRUE;
8362 fname = get_view_file(*eap->arg);
8363 if (fname == NULL)
8364 return;
8365 viewFile = fname;
8366 using_vdir = TRUE;
8367 }
8368 else
8369#endif
8370 if (*eap->arg != NUL)
8371 fname = eap->arg;
8372 else if (eap->cmdidx == CMD_mkvimrc)
8373 fname = (char_u *)VIMRC_FILE;
8374#ifdef FEAT_SESSION
8375 else if (eap->cmdidx == CMD_mksession)
8376 fname = (char_u *)SESSION_FILE;
8377#endif
8378 else
8379 fname = (char_u *)EXRC_FILE;
8380
8381#ifdef FEAT_BROWSE
8382 if (cmdmod.browse)
8383 {
Bram Moolenaar7171abe2004-10-11 10:06:20 +00008384 browseFile = do_browse(BROWSE_SAVE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008385# ifdef FEAT_SESSION
8386 eap->cmdidx == CMD_mkview ? (char_u *)_("Save View") :
8387 eap->cmdidx == CMD_mksession ? (char_u *)_("Save Session") :
8388# endif
8389 (char_u *)_("Save Setup"),
Bram Moolenaarc36651b2018-04-29 12:22:56 +02008390 fname, (char_u *)"vim", NULL,
8391 (char_u *)_(BROWSE_FILTER_MACROS), NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008392 if (browseFile == NULL)
8393 goto theend;
8394 fname = browseFile;
8395 eap->forceit = TRUE; /* since dialog already asked */
8396 }
8397#endif
8398
8399#if defined(FEAT_SESSION) && defined(vim_mkdir)
8400 /* When using 'viewdir' may have to create the directory. */
8401 if (using_vdir && !mch_isdir(p_vdir))
Bram Moolenaardf177f62005-02-22 08:39:57 +00008402 vim_mkdir_emsg(p_vdir, 0755);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008403#endif
8404
8405 fd = open_exfile(fname, eap->forceit, WRITEBIN);
8406 if (fd != NULL)
8407 {
8408#ifdef FEAT_SESSION
8409 if (eap->cmdidx == CMD_mkview)
8410 flagp = &vop_flags;
8411 else
8412 flagp = &ssop_flags;
8413#endif
8414
8415#ifdef MKSESSION_NL
8416 /* "unix" in 'sessionoptions': use NL line separator */
8417 if (view_session && (*flagp & SSOP_UNIX))
8418 mksession_nl = TRUE;
8419#endif
8420
8421 /* Write the version command for :mkvimrc */
8422 if (eap->cmdidx == CMD_mkvimrc)
8423 (void)put_line(fd, "version 6.0");
8424
8425#ifdef FEAT_SESSION
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00008426 if (eap->cmdidx == CMD_mksession)
8427 {
8428 if (put_line(fd, "let SessionLoad = 1") == FAIL)
8429 failed = TRUE;
8430 }
8431
Bram Moolenaar071d4272004-06-13 20:20:40 +00008432 if (eap->cmdidx != CMD_mkview)
8433#endif
8434 {
8435 /* Write setting 'compatible' first, because it has side effects.
8436 * For that same reason only do it when needed. */
8437 if (p_cp)
8438 (void)put_line(fd, "if !&cp | set cp | endif");
8439 else
8440 (void)put_line(fd, "if &cp | set nocp | endif");
8441 }
8442
8443#ifdef FEAT_SESSION
8444 if (!view_session
8445 || (eap->cmdidx == CMD_mksession
8446 && (*flagp & SSOP_OPTIONS)))
8447#endif
8448 failed |= (makemap(fd, NULL) == FAIL
8449 || makeset(fd, OPT_GLOBAL, FALSE) == FAIL);
8450
8451#ifdef FEAT_SESSION
8452 if (!failed && view_session)
8453 {
8454 if (put_line(fd, "let s:so_save = &so | let s:siso_save = &siso | set so=0 siso=0") == FAIL)
8455 failed = TRUE;
8456 if (eap->cmdidx == CMD_mksession)
8457 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02008458 char_u *dirnow; /* current directory */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008459
Bram Moolenaard9462e32011-04-11 21:35:11 +02008460 dirnow = alloc(MAXPATHL);
8461 if (dirnow == NULL)
8462 failed = TRUE;
8463 else
8464 {
8465 /*
8466 * Change to session file's dir.
8467 */
8468 if (mch_dirname(dirnow, MAXPATHL) == FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +00008469 || mch_chdir((char *)dirnow) != 0)
Bram Moolenaard9462e32011-04-11 21:35:11 +02008470 *dirnow = NUL;
8471 if (*dirnow != NUL && (ssop_flags & SSOP_SESDIR))
8472 {
Bram Moolenaarb7407d32018-02-03 17:36:27 +01008473 if (vim_chdirfile(fname, NULL) == OK)
Bram Moolenaard9462e32011-04-11 21:35:11 +02008474 shorten_fnames(TRUE);
8475 }
8476 else if (*dirnow != NUL
8477 && (ssop_flags & SSOP_CURDIR) && globaldir != NULL)
8478 {
8479 if (mch_chdir((char *)globaldir) == 0)
8480 shorten_fnames(TRUE);
8481 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008482
Bram Moolenaard9462e32011-04-11 21:35:11 +02008483 failed |= (makeopens(fd, dirnow) == FAIL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008484
Bram Moolenaard9462e32011-04-11 21:35:11 +02008485 /* restore original dir */
8486 if (*dirnow != NUL && ((ssop_flags & SSOP_SESDIR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008487 || ((ssop_flags & SSOP_CURDIR) && globaldir != NULL)))
Bram Moolenaard9462e32011-04-11 21:35:11 +02008488 {
8489 if (mch_chdir((char *)dirnow) != 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008490 emsg(_(e_prev_dir));
Bram Moolenaard9462e32011-04-11 21:35:11 +02008491 shorten_fnames(TRUE);
8492 }
8493 vim_free(dirnow);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008494 }
8495 }
8496 else
8497 {
Bram Moolenaarf13be0d2008-01-02 14:13:10 +00008498 failed |= (put_view(fd, curwin, !using_vdir, flagp,
8499 -1) == FAIL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008500 }
8501 if (put_line(fd, "let &so = s:so_save | let &siso = s:siso_save")
8502 == FAIL)
8503 failed = TRUE;
Bram Moolenaare3c74d22019-01-12 16:29:30 +01008504# ifdef FEAT_SEARCH_EXTRA
8505 if (no_hlsearch && put_line(fd, "nohlsearch") == FAIL)
8506 failed = TRUE;
8507# endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00008508 if (put_line(fd, "doautoall SessionLoadPost") == FAIL)
8509 failed = TRUE;
Bram Moolenaar4770d092006-01-12 23:22:24 +00008510 if (eap->cmdidx == CMD_mksession)
8511 {
8512 if (put_line(fd, "unlet SessionLoad") == FAIL)
8513 failed = TRUE;
8514 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008515 }
8516#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00008517 if (put_line(fd, "\" vim: set ft=vim :") == FAIL)
8518 failed = TRUE;
8519
Bram Moolenaar071d4272004-06-13 20:20:40 +00008520 failed |= fclose(fd);
8521
8522 if (failed)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008523 emsg(_(e_write));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008524#if defined(FEAT_EVAL) && defined(FEAT_SESSION)
8525 else if (eap->cmdidx == CMD_mksession)
8526 {
8527 /* successful session write - set this_session var */
Bram Moolenaard9462e32011-04-11 21:35:11 +02008528 char_u *tbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008529
Bram Moolenaard9462e32011-04-11 21:35:11 +02008530 tbuf = alloc(MAXPATHL);
8531 if (tbuf != NULL)
8532 {
8533 if (vim_FullName(fname, tbuf, MAXPATHL, FALSE) == OK)
8534 set_vim_var_string(VV_THIS_SESSION, tbuf, -1);
8535 vim_free(tbuf);
8536 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008537 }
8538#endif
8539#ifdef MKSESSION_NL
8540 mksession_nl = FALSE;
8541#endif
8542 }
8543
8544#ifdef FEAT_BROWSE
8545theend:
8546 vim_free(browseFile);
8547#endif
8548#ifdef FEAT_SESSION
8549 vim_free(viewFile);
8550#endif
8551}
8552
Bram Moolenaardf177f62005-02-22 08:39:57 +00008553#if ((defined(FEAT_SESSION) || defined(FEAT_EVAL)) && defined(vim_mkdir)) \
8554 || defined(PROTO)
8555 int
Bram Moolenaarf1d25012016-03-03 12:22:53 +01008556vim_mkdir_emsg(char_u *name, int prot)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008557{
8558 if (vim_mkdir(name, prot) != 0)
8559 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008560 semsg(_("E739: Cannot create directory: %s"), name);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008561 return FAIL;
8562 }
8563 return OK;
8564}
8565#endif
8566
Bram Moolenaar071d4272004-06-13 20:20:40 +00008567/*
8568 * Open a file for writing for an Ex command, with some checks.
8569 * Return file descriptor, or NULL on failure.
8570 */
8571 FILE *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008572open_exfile(
8573 char_u *fname,
8574 int forceit,
8575 char *mode) /* "w" for create new file or "a" for append */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008576{
8577 FILE *fd;
8578
8579#ifdef UNIX
8580 /* with Unix it is possible to open a directory */
8581 if (mch_isdir(fname))
8582 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008583 semsg(_(e_isadir2), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008584 return NULL;
8585 }
8586#endif
8587 if (!forceit && *mode != 'a' && vim_fexists(fname))
8588 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008589 semsg(_("E189: \"%s\" exists (add ! to override)"), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008590 return NULL;
8591 }
8592
8593 if ((fd = mch_fopen((char *)fname, mode)) == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008594 semsg(_("E190: Cannot open \"%s\" for writing"), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008595
8596 return fd;
8597}
8598
8599/*
8600 * ":mark" and ":k".
8601 */
8602 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008603ex_mark(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008604{
8605 pos_T pos;
8606
8607 if (*eap->arg == NUL) /* No argument? */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008608 emsg(_(e_argreq));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008609 else if (eap->arg[1] != NUL) /* more than one character? */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008610 emsg(_(e_trailing));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008611 else
8612 {
8613 pos = curwin->w_cursor; /* save curwin->w_cursor */
8614 curwin->w_cursor.lnum = eap->line2;
8615 beginline(BL_WHITE | BL_FIX);
8616 if (setmark(*eap->arg) == FAIL) /* set mark */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008617 emsg(_("E191: Argument must be a letter or forward/backward quote"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008618 curwin->w_cursor = pos; /* restore curwin->w_cursor */
8619 }
8620}
8621
8622/*
8623 * Update w_topline, w_leftcol and the cursor position.
8624 */
8625 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008626update_topline_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008627{
8628 check_cursor(); /* put cursor on valid line */
8629 update_topline();
8630 if (!curwin->w_p_wrap)
8631 validate_cursor();
8632 update_curswant();
8633}
8634
Bram Moolenaar071d4272004-06-13 20:20:40 +00008635/*
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008636 * Save the current State and go to Normal mode.
8637 * Return TRUE if the typeahead could be saved.
8638 */
8639 int
8640save_current_state(save_state_T *sst)
8641{
8642 sst->save_msg_scroll = msg_scroll;
8643 sst->save_restart_edit = restart_edit;
8644 sst->save_msg_didout = msg_didout;
8645 sst->save_State = State;
8646 sst->save_insertmode = p_im;
8647 sst->save_finish_op = finish_op;
8648 sst->save_opcount = opcount;
Bram Moolenaarcce713d2019-03-04 11:40:12 +01008649 sst->save_reg_executing = reg_executing;
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008650
8651 msg_scroll = FALSE; /* no msg scrolling in Normal mode */
8652 restart_edit = 0; /* don't go to Insert mode */
8653 p_im = FALSE; /* don't use 'insertmode' */
8654
8655 /*
8656 * Save the current typeahead. This is required to allow using ":normal"
8657 * from an event handler and makes sure we don't hang when the argument
8658 * ends with half a command.
8659 */
8660 save_typeahead(&sst->tabuf);
8661 return sst->tabuf.typebuf_valid;
8662}
8663
8664 void
8665restore_current_state(save_state_T *sst)
8666{
8667 /* Restore the previous typeahead. */
8668 restore_typeahead(&sst->tabuf);
8669
8670 msg_scroll = sst->save_msg_scroll;
8671 restart_edit = sst->save_restart_edit;
8672 p_im = sst->save_insertmode;
8673 finish_op = sst->save_finish_op;
8674 opcount = sst->save_opcount;
Bram Moolenaarcce713d2019-03-04 11:40:12 +01008675 reg_executing = sst->save_reg_executing;
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008676 msg_didout |= sst->save_msg_didout; /* don't reset msg_didout now */
8677
8678 /* Restore the state (needed when called from a function executed for
8679 * 'indentexpr'). Update the mouse and cursor, they may have changed. */
8680 State = sst->save_State;
8681#ifdef CURSOR_SHAPE
8682 ui_cursor_shape(); /* may show different cursor shape */
8683#endif
8684}
8685
8686/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008687 * ":normal[!] {commands}": Execute normal mode commands.
8688 */
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01008689 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008690ex_normal(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008691{
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008692 save_state_T save_state;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008693 char_u *arg = NULL;
8694 int l;
8695 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008696
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008697 if (ex_normal_lock > 0)
8698 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008699 emsg(_(e_secure));
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008700 return;
8701 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008702 if (ex_normal_busy >= p_mmd)
8703 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008704 emsg(_("E192: Recursive use of :normal too deep"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008705 return;
8706 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008707
Bram Moolenaar071d4272004-06-13 20:20:40 +00008708 /*
8709 * vgetc() expects a CSI and K_SPECIAL to have been escaped. Don't do
8710 * this for the K_SPECIAL leading byte, otherwise special keys will not
8711 * work.
8712 */
8713 if (has_mbyte)
8714 {
8715 int len = 0;
8716
8717 /* Count the number of characters to be escaped. */
8718 for (p = eap->arg; *p != NUL; ++p)
8719 {
Bram Moolenaar13505972019-01-24 15:04:48 +01008720#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008721 if (*p == CSI) /* leadbyte CSI */
8722 len += 2;
Bram Moolenaar13505972019-01-24 15:04:48 +01008723#endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008724 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008725 if (*++p == K_SPECIAL /* trailbyte K_SPECIAL or CSI */
Bram Moolenaar13505972019-01-24 15:04:48 +01008726#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008727 || *p == CSI
Bram Moolenaar13505972019-01-24 15:04:48 +01008728#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008729 )
8730 len += 2;
8731 }
8732 if (len > 0)
8733 {
8734 arg = alloc((unsigned)(STRLEN(eap->arg) + len + 1));
8735 if (arg != NULL)
8736 {
8737 len = 0;
8738 for (p = eap->arg; *p != NUL; ++p)
8739 {
8740 arg[len++] = *p;
Bram Moolenaar13505972019-01-24 15:04:48 +01008741#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008742 if (*p == CSI)
8743 {
8744 arg[len++] = KS_EXTRA;
8745 arg[len++] = (int)KE_CSI;
8746 }
Bram Moolenaar13505972019-01-24 15:04:48 +01008747#endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008748 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008749 {
8750 arg[len++] = *++p;
8751 if (*p == K_SPECIAL)
8752 {
8753 arg[len++] = KS_SPECIAL;
8754 arg[len++] = KE_FILLER;
8755 }
Bram Moolenaar13505972019-01-24 15:04:48 +01008756#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008757 else if (*p == CSI)
8758 {
8759 arg[len++] = KS_EXTRA;
8760 arg[len++] = (int)KE_CSI;
8761 }
Bram Moolenaar13505972019-01-24 15:04:48 +01008762#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008763 }
8764 arg[len] = NUL;
8765 }
8766 }
8767 }
8768 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008769
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008770 ++ex_normal_busy;
8771 if (save_current_state(&save_state))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008772 {
8773 /*
8774 * Repeat the :normal command for each line in the range. When no
8775 * range given, execute it just once, without positioning the cursor
8776 * first.
8777 */
8778 do
8779 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008780 if (eap->addr_count != 0)
8781 {
8782 curwin->w_cursor.lnum = eap->line1++;
8783 curwin->w_cursor.col = 0;
Bram Moolenaard5d37532017-03-27 23:02:07 +02008784 check_cursor_moved(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008785 }
8786
Bram Moolenaar13505972019-01-24 15:04:48 +01008787 exec_normal_cmd(arg != NULL
8788 ? arg
8789 : eap->arg, eap->forceit ? REMAP_NONE : REMAP_YES, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008790 }
8791 while (eap->addr_count > 0 && eap->line1 <= eap->line2 && !got_int);
8792 }
8793
8794 /* Might not return to the main loop when in an event handler. */
8795 update_topline_cursor();
8796
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008797 restore_current_state(&save_state);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008798 --ex_normal_busy;
Bram Moolenaareda73602014-11-05 09:53:23 +01008799#ifdef FEAT_MOUSE
8800 setmouse();
8801#endif
8802#ifdef CURSOR_SHAPE
8803 ui_cursor_shape(); /* may show different cursor shape */
8804#endif
8805
Bram Moolenaar071d4272004-06-13 20:20:40 +00008806 vim_free(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008807}
8808
8809/*
Bram Moolenaar64969662005-12-14 21:59:55 +00008810 * ":startinsert", ":startreplace" and ":startgreplace"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008811 */
8812 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008813ex_startinsert(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008814{
Bram Moolenaarfd371682005-01-14 21:42:54 +00008815 if (eap->forceit)
8816 {
Bram Moolenaar09ca9322017-09-26 17:40:45 +02008817 /* cursor line can be zero on startup */
8818 if (!curwin->w_cursor.lnum)
8819 curwin->w_cursor.lnum = 1;
Bram Moolenaarfd371682005-01-14 21:42:54 +00008820 coladvance((colnr_T)MAXCOL);
8821 curwin->w_curswant = MAXCOL;
8822 curwin->w_set_curswant = FALSE;
8823 }
8824
Bram Moolenaara40c5002005-01-09 21:16:21 +00008825 /* Ignore the command when already in Insert mode. Inserting an
8826 * expression register that invokes a function can do this. */
8827 if (State & INSERT)
8828 return;
8829
Bram Moolenaar64969662005-12-14 21:59:55 +00008830 if (eap->cmdidx == CMD_startinsert)
8831 restart_edit = 'a';
8832 else if (eap->cmdidx == CMD_startreplace)
8833 restart_edit = 'R';
Bram Moolenaar071d4272004-06-13 20:20:40 +00008834 else
Bram Moolenaar64969662005-12-14 21:59:55 +00008835 restart_edit = 'V';
8836
8837 if (!eap->forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008838 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00008839 if (eap->cmdidx == CMD_startinsert)
8840 restart_edit = 'i';
Bram Moolenaar071d4272004-06-13 20:20:40 +00008841 curwin->w_curswant = 0; /* avoid MAXCOL */
8842 }
8843}
8844
8845/*
8846 * ":stopinsert"
8847 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008848 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008849ex_stopinsert(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008850{
8851 restart_edit = 0;
8852 stop_insert_mode = TRUE;
Bram Moolenaarfd773e92016-04-02 19:39:16 +02008853 clearmode();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008854}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008855
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008856/*
8857 * Execute normal mode command "cmd".
8858 * "remap" can be REMAP_NONE or REMAP_YES.
8859 */
8860 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008861exec_normal_cmd(char_u *cmd, int remap, int silent)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008862{
Bram Moolenaar25281632016-01-21 23:32:32 +01008863 /* Stuff the argument into the typeahead buffer. */
8864 ins_typebuf(cmd, remap, 0, TRUE, silent);
Bram Moolenaar586c70c2018-10-02 16:23:58 +02008865 exec_normal(FALSE, FALSE, FALSE);
Bram Moolenaar25281632016-01-21 23:32:32 +01008866}
Bram Moolenaar25281632016-01-21 23:32:32 +01008867
Bram Moolenaar25281632016-01-21 23:32:32 +01008868/*
8869 * Execute normal_cmd() until there is no typeahead left.
Bram Moolenaar586c70c2018-10-02 16:23:58 +02008870 * When "use_vpeekc" is TRUE use vpeekc() to check for available chars.
Bram Moolenaar25281632016-01-21 23:32:32 +01008871 */
8872 void
Bram Moolenaar586c70c2018-10-02 16:23:58 +02008873exec_normal(int was_typed, int use_vpeekc, int may_use_terminal_loop UNUSED)
Bram Moolenaar25281632016-01-21 23:32:32 +01008874{
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008875 oparg_T oa;
Bram Moolenaar905dd902019-04-07 14:21:47 +02008876 int c;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008877
Bram Moolenaar905dd902019-04-07 14:21:47 +02008878 // When calling vpeekc() from feedkeys() it will return Ctrl_C when there
8879 // is nothing to get, so also check for Ctrl_C.
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008880 clear_oparg(&oa);
8881 finish_op = FALSE;
Bram Moolenaar586c70c2018-10-02 16:23:58 +02008882 while ((!stuff_empty()
8883 || ((was_typed || !typebuf_typed()) && typebuf.tb_len > 0)
Bram Moolenaar905dd902019-04-07 14:21:47 +02008884 || (use_vpeekc && (c = vpeekc()) != NUL && c != Ctrl_C))
Bram Moolenaar586c70c2018-10-02 16:23:58 +02008885 && !got_int)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008886 {
8887 update_topline_cursor();
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02008888#ifdef FEAT_TERMINAL
Bram Moolenaar655a82a2018-05-08 22:01:07 +02008889 if (may_use_terminal_loop && term_use_loop()
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02008890 && oa.op_type == OP_NOP && oa.regname == NUL
8891 && !VIsual_active)
8892 {
8893 /* If terminal_loop() returns OK we got a key that is handled
8894 * in Normal model. With FAIL we first need to position the
8895 * cursor and the screen needs to be redrawn. */
8896 if (terminal_loop(TRUE) == OK)
8897 normal_cmd(&oa, TRUE);
8898 }
8899 else
8900#endif
8901 /* execute a Normal mode cmd */
8902 normal_cmd(&oa, TRUE);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008903 }
8904}
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008905
Bram Moolenaar071d4272004-06-13 20:20:40 +00008906#ifdef FEAT_FIND_ID
8907 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008908ex_checkpath(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008909{
8910 find_pattern_in_path(NULL, 0, 0, FALSE, FALSE, CHECK_PATH, 1L,
8911 eap->forceit ? ACTION_SHOW_ALL : ACTION_SHOW,
8912 (linenr_T)1, (linenr_T)MAXLNUM);
8913}
8914
Bram Moolenaar4033c552017-09-16 20:54:51 +02008915#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008916/*
8917 * ":psearch"
8918 */
8919 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008920ex_psearch(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008921{
8922 g_do_tagpreview = p_pvh;
8923 ex_findpat(eap);
8924 g_do_tagpreview = 0;
8925}
8926#endif
8927
8928 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008929ex_findpat(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008930{
8931 int whole = TRUE;
8932 long n;
8933 char_u *p;
8934 int action;
8935
8936 switch (cmdnames[eap->cmdidx].cmd_name[2])
8937 {
8938 case 'e': /* ":psearch", ":isearch" and ":dsearch" */
8939 if (cmdnames[eap->cmdidx].cmd_name[0] == 'p')
8940 action = ACTION_GOTO;
8941 else
8942 action = ACTION_SHOW;
8943 break;
8944 case 'i': /* ":ilist" and ":dlist" */
8945 action = ACTION_SHOW_ALL;
8946 break;
8947 case 'u': /* ":ijump" and ":djump" */
8948 action = ACTION_GOTO;
8949 break;
8950 default: /* ":isplit" and ":dsplit" */
8951 action = ACTION_SPLIT;
8952 break;
8953 }
8954
8955 n = 1;
8956 if (vim_isdigit(*eap->arg)) /* get count */
8957 {
8958 n = getdigits(&eap->arg);
8959 eap->arg = skipwhite(eap->arg);
8960 }
8961 if (*eap->arg == '/') /* Match regexp, not just whole words */
8962 {
8963 whole = FALSE;
8964 ++eap->arg;
8965 p = skip_regexp(eap->arg, '/', p_magic, NULL);
8966 if (*p)
8967 {
8968 *p++ = NUL;
8969 p = skipwhite(p);
8970
8971 /* Check for trailing illegal characters */
8972 if (!ends_excmd(*p))
8973 eap->errmsg = e_trailing;
8974 else
8975 eap->nextcmd = check_nextcmd(p);
8976 }
8977 }
8978 if (!eap->skip)
8979 find_pattern_in_path(eap->arg, 0, (int)STRLEN(eap->arg),
8980 whole, !eap->forceit,
8981 *eap->cmd == 'd' ? FIND_DEFINE : FIND_ANY,
8982 n, action, eap->line1, eap->line2);
8983}
8984#endif
8985
Bram Moolenaar071d4272004-06-13 20:20:40 +00008986
Bram Moolenaar4033c552017-09-16 20:54:51 +02008987#ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00008988/*
8989 * ":ptag", ":ptselect", ":ptjump", ":ptnext", etc.
8990 */
8991 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008992ex_ptag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008993{
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01008994 g_do_tagpreview = p_pvh; /* will be reset to 0 in ex_tag_cmd() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008995 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
8996}
8997
8998/*
8999 * ":pedit"
9000 */
9001 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009002ex_pedit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009003{
9004 win_T *curwin_save = curwin;
9005
9006 g_do_tagpreview = p_pvh;
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00009007 prepare_tagpreview(TRUE);
Bram Moolenaar67883b42017-07-27 22:57:00 +02009008 keep_help_flag = bt_help(curwin_save->w_buffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009009 do_exedit(eap, NULL);
9010 keep_help_flag = FALSE;
9011 if (curwin != curwin_save && win_valid(curwin_save))
9012 {
9013 /* Return cursor to where we were */
9014 validate_cursor();
9015 redraw_later(VALID);
9016 win_enter(curwin_save, TRUE);
9017 }
9018 g_do_tagpreview = 0;
9019}
Bram Moolenaar4033c552017-09-16 20:54:51 +02009020#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009021
9022/*
9023 * ":stag", ":stselect" and ":stjump".
9024 */
9025 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009026ex_stag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009027{
9028 postponed_split = -1;
9029 postponed_split_flags = cmdmod.split;
Bram Moolenaard326ce82007-03-11 14:48:29 +00009030 postponed_split_tab = cmdmod.tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009031 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
9032 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00009033 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009034}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009035
9036/*
9037 * ":tag", ":tselect", ":tjump", ":tnext", etc.
9038 */
9039 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009040ex_tag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009041{
9042 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name);
9043}
9044
9045 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009046ex_tag_cmd(exarg_T *eap, char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009047{
9048 int cmd;
9049
9050 switch (name[1])
9051 {
9052 case 'j': cmd = DT_JUMP; /* ":tjump" */
9053 break;
9054 case 's': cmd = DT_SELECT; /* ":tselect" */
9055 break;
9056 case 'p': cmd = DT_PREV; /* ":tprevious" */
9057 break;
9058 case 'N': cmd = DT_PREV; /* ":tNext" */
9059 break;
9060 case 'n': cmd = DT_NEXT; /* ":tnext" */
9061 break;
9062 case 'o': cmd = DT_POP; /* ":pop" */
9063 break;
9064 case 'f': /* ":tfirst" */
9065 case 'r': cmd = DT_FIRST; /* ":trewind" */
9066 break;
9067 case 'l': cmd = DT_LAST; /* ":tlast" */
9068 break;
9069 default: /* ":tag" */
9070#ifdef FEAT_CSCOPE
Bram Moolenaar7c94c262008-06-20 09:11:34 +00009071 if (p_cst && *eap->arg != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009072 {
Bram Moolenaard4db7712016-11-12 19:16:46 +01009073 ex_cstag(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009074 return;
9075 }
9076#endif
9077 cmd = DT_TAG;
9078 break;
9079 }
9080
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00009081 if (name[0] == 'l')
9082 {
9083#ifndef FEAT_QUICKFIX
9084 ex_ni(eap);
9085 return;
9086#else
9087 cmd = DT_LTAG;
9088#endif
9089 }
9090
Bram Moolenaar071d4272004-06-13 20:20:40 +00009091 do_tag(eap->arg, cmd, eap->addr_count > 0 ? (int)eap->line2 : 1,
9092 eap->forceit, TRUE);
9093}
9094
9095/*
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009096 * Check "str" for starting with a special cmdline variable.
9097 * If found return one of the SPEC_ values and set "*usedlen" to the length of
9098 * the variable. Otherwise return -1 and "*usedlen" is unchanged.
9099 */
9100 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009101find_cmdline_var(char_u *src, int *usedlen)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009102{
9103 int len;
9104 int i;
Bram Moolenaar8f0b2d42009-05-16 14:41:10 +00009105 static char *(spec_str[]) = {
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009106 "%",
9107#define SPEC_PERC 0
9108 "#",
Bram Moolenaar65f08472017-09-10 18:16:20 +02009109#define SPEC_HASH (SPEC_PERC + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009110 "<cword>", /* cursor word */
Bram Moolenaar65f08472017-09-10 18:16:20 +02009111#define SPEC_CWORD (SPEC_HASH + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009112 "<cWORD>", /* cursor WORD */
Bram Moolenaar65f08472017-09-10 18:16:20 +02009113#define SPEC_CCWORD (SPEC_CWORD + 1)
9114 "<cexpr>", /* expr under cursor */
9115#define SPEC_CEXPR (SPEC_CCWORD + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009116 "<cfile>", /* cursor path name */
Bram Moolenaar65f08472017-09-10 18:16:20 +02009117#define SPEC_CFILE (SPEC_CEXPR + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009118 "<sfile>", /* ":so" file name */
Bram Moolenaar65f08472017-09-10 18:16:20 +02009119#define SPEC_SFILE (SPEC_CFILE + 1)
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009120 "<slnum>", /* ":so" file line number */
Bram Moolenaar65f08472017-09-10 18:16:20 +02009121#define SPEC_SLNUM (SPEC_SFILE + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009122 "<afile>", /* autocommand file name */
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009123#define SPEC_AFILE (SPEC_SLNUM + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009124 "<abuf>", /* autocommand buffer number */
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009125#define SPEC_ABUF (SPEC_AFILE + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009126 "<amatch>", /* autocommand match name */
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01009127#define SPEC_AMATCH (SPEC_ABUF + 1)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009128 "<sflnum>", /* script file line number */
9129#define SPEC_SFLNUM (SPEC_AMATCH + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009130#ifdef FEAT_CLIENTSERVER
9131 "<client>"
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009132# define SPEC_CLIENT (SPEC_SFLNUM + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009133#endif
9134 };
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009135
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00009136 for (i = 0; i < (int)(sizeof(spec_str) / sizeof(char *)); ++i)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009137 {
9138 len = (int)STRLEN(spec_str[i]);
9139 if (STRNCMP(src, spec_str[i], len) == 0)
9140 {
9141 *usedlen = len;
9142 return i;
9143 }
9144 }
9145 return -1;
9146}
9147
9148/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009149 * Evaluate cmdline variables.
9150 *
9151 * change '%' to curbuf->b_ffname
9152 * '#' to curwin->w_altfile
9153 * '<cword>' to word under the cursor
9154 * '<cWORD>' to WORD under the cursor
9155 * '<cfile>' to path name under the cursor
9156 * '<sfile>' to sourced file name
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009157 * '<slnum>' to sourced file line number
Bram Moolenaar071d4272004-06-13 20:20:40 +00009158 * '<afile>' to file name for autocommand
9159 * '<abuf>' to buffer number for autocommand
9160 * '<amatch>' to matching name for autocommand
9161 *
9162 * When an error is detected, "errormsg" is set to a non-NULL pointer (may be
9163 * "" for error without a message) and NULL is returned.
9164 * Returns an allocated string if a valid match was found.
9165 * Returns NULL if no match was found. "usedlen" then still contains the
9166 * number of characters to skip.
9167 */
9168 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009169eval_vars(
9170 char_u *src, /* pointer into commandline */
9171 char_u *srcstart, /* beginning of valid memory for src */
9172 int *usedlen, /* characters after src that are used */
9173 linenr_T *lnump, /* line number for :e command, or NULL */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009174 char **errormsg, /* pointer to error message */
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009175 int *escaped) /* return value has escaped white space (can
Bram Moolenaar63b92542007-03-27 14:57:09 +00009176 * be NULL) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009177{
9178 int i;
9179 char_u *s;
9180 char_u *result;
9181 char_u *resultbuf = NULL;
9182 int resultlen;
9183 buf_T *buf;
9184 int valid = VALID_HEAD + VALID_PATH; /* assume valid result */
9185 int spec_idx;
9186#ifdef FEAT_MODIFY_FNAME
Bram Moolenaarfd249462018-07-28 16:14:30 +02009187 int tilde_file = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009188 int skip_mod = FALSE;
9189#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009190 char_u strbuf[30];
Bram Moolenaar071d4272004-06-13 20:20:40 +00009191
9192 *errormsg = NULL;
Bram Moolenaar63b92542007-03-27 14:57:09 +00009193 if (escaped != NULL)
9194 *escaped = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009195
9196 /*
9197 * Check if there is something to do.
9198 */
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009199 spec_idx = find_cmdline_var(src, usedlen);
9200 if (spec_idx < 0) /* no match */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009201 {
9202 *usedlen = 1;
9203 return NULL;
9204 }
9205
9206 /*
9207 * Skip when preceded with a backslash "\%" and "\#".
9208 * Note: In "\\%" the % is also not recognized!
9209 */
9210 if (src > srcstart && src[-1] == '\\')
9211 {
9212 *usedlen = 0;
Bram Moolenaara7241f52008-06-24 20:39:31 +00009213 STRMOVE(src - 1, src); /* remove backslash */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009214 return NULL;
9215 }
9216
9217 /*
9218 * word or WORD under cursor
9219 */
Bram Moolenaar65f08472017-09-10 18:16:20 +02009220 if (spec_idx == SPEC_CWORD || spec_idx == SPEC_CCWORD
9221 || spec_idx == SPEC_CEXPR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009222 {
Bram Moolenaar65f08472017-09-10 18:16:20 +02009223 resultlen = find_ident_under_cursor(&result,
9224 spec_idx == SPEC_CWORD ? (FIND_IDENT | FIND_STRING)
9225 : spec_idx == SPEC_CEXPR ? (FIND_IDENT | FIND_STRING | FIND_EVAL)
9226 : FIND_STRING);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009227 if (resultlen == 0)
9228 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009229 *errormsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +00009230 return NULL;
9231 }
9232 }
9233
9234 /*
9235 * '#': Alternate file name
9236 * '%': Current file name
9237 * File name under the cursor
9238 * File name for autocommand
9239 * and following modifiers
9240 */
9241 else
9242 {
9243 switch (spec_idx)
9244 {
9245 case SPEC_PERC: /* '%': current file */
9246 if (curbuf->b_fname == NULL)
9247 {
9248 result = (char_u *)"";
9249 valid = 0; /* Must have ":p:h" to be valid */
9250 }
9251 else
Bram Moolenaar00136dc2018-07-25 21:19:13 +02009252 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009253 result = curbuf->b_fname;
Bram Moolenaarfd249462018-07-28 16:14:30 +02009254#ifdef FEAT_MODIFY_FNAME
Bram Moolenaar00136dc2018-07-25 21:19:13 +02009255 tilde_file = STRCMP(result, "~") == 0;
Bram Moolenaarfd249462018-07-28 16:14:30 +02009256#endif
Bram Moolenaar00136dc2018-07-25 21:19:13 +02009257 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009258 break;
9259
9260 case SPEC_HASH: /* '#' or "#99": alternate file */
9261 if (src[1] == '#') /* "##": the argument list */
9262 {
9263 result = arg_all();
9264 resultbuf = result;
9265 *usedlen = 2;
Bram Moolenaar63b92542007-03-27 14:57:09 +00009266 if (escaped != NULL)
9267 *escaped = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009268#ifdef FEAT_MODIFY_FNAME
9269 skip_mod = TRUE;
9270#endif
9271 break;
9272 }
9273 s = src + 1;
Bram Moolenaard812df62008-11-09 12:46:09 +00009274 if (*s == '<') /* "#<99" uses v:oldfiles */
9275 ++s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009276 i = (int)getdigits(&s);
Bram Moolenaarc312b8b2017-10-28 17:53:04 +02009277 if (s == src + 2 && src[1] == '-')
9278 /* just a minus sign, don't skip over it */
9279 s--;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009280 *usedlen = (int)(s - src); /* length of what we expand */
9281
Bram Moolenaarc312b8b2017-10-28 17:53:04 +02009282 if (src[1] == '<' && i != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009283 {
Bram Moolenaard812df62008-11-09 12:46:09 +00009284 if (*usedlen < 2)
9285 {
9286 /* Should we give an error message for #<text? */
9287 *usedlen = 1;
9288 return NULL;
9289 }
9290#ifdef FEAT_EVAL
9291 result = list_find_str(get_vim_var_list(VV_OLDFILES),
9292 (long)i);
9293 if (result == NULL)
9294 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009295 *errormsg = "";
Bram Moolenaard812df62008-11-09 12:46:09 +00009296 return NULL;
9297 }
9298#else
Bram Moolenaar8e481e82019-01-15 20:07:48 +01009299 *errormsg = _("E809: #< is not available without the +eval feature");
Bram Moolenaar071d4272004-06-13 20:20:40 +00009300 return NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +00009301#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009302 }
9303 else
Bram Moolenaard812df62008-11-09 12:46:09 +00009304 {
Bram Moolenaarc312b8b2017-10-28 17:53:04 +02009305 if (i == 0 && src[1] == '<' && *usedlen > 1)
9306 *usedlen = 1;
Bram Moolenaard812df62008-11-09 12:46:09 +00009307 buf = buflist_findnr(i);
9308 if (buf == NULL)
9309 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009310 *errormsg = _("E194: No alternate file name to substitute for '#'");
Bram Moolenaard812df62008-11-09 12:46:09 +00009311 return NULL;
9312 }
9313 if (lnump != NULL)
9314 *lnump = ECMD_LAST;
9315 if (buf->b_fname == NULL)
9316 {
9317 result = (char_u *)"";
9318 valid = 0; /* Must have ":p:h" to be valid */
9319 }
9320 else
Bram Moolenaar00136dc2018-07-25 21:19:13 +02009321 {
Bram Moolenaard812df62008-11-09 12:46:09 +00009322 result = buf->b_fname;
Bram Moolenaarfd249462018-07-28 16:14:30 +02009323#ifdef FEAT_MODIFY_FNAME
Bram Moolenaar00136dc2018-07-25 21:19:13 +02009324 tilde_file = STRCMP(result, "~") == 0;
Bram Moolenaarfd249462018-07-28 16:14:30 +02009325#endif
Bram Moolenaar00136dc2018-07-25 21:19:13 +02009326 }
Bram Moolenaard812df62008-11-09 12:46:09 +00009327 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009328 break;
9329
9330#ifdef FEAT_SEARCHPATH
9331 case SPEC_CFILE: /* file name under cursor */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00009332 result = file_name_at_cursor(FNAME_MESS|FNAME_HYP, 1L, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009333 if (result == NULL)
9334 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009335 *errormsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +00009336 return NULL;
9337 }
9338 resultbuf = result; /* remember allocated string */
9339 break;
9340#endif
9341
Bram Moolenaar071d4272004-06-13 20:20:40 +00009342 case SPEC_AFILE: /* file name for autocommand */
9343 result = autocmd_fname;
Bram Moolenaarf6dad432008-09-18 19:29:58 +00009344 if (result != NULL && !autocmd_fname_full)
9345 {
9346 /* Still need to turn the fname into a full path. It is
9347 * postponed to avoid a delay when <afile> is not used. */
9348 autocmd_fname_full = TRUE;
9349 result = FullName_save(autocmd_fname, FALSE);
9350 vim_free(autocmd_fname);
9351 autocmd_fname = result;
9352 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009353 if (result == NULL)
9354 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009355 *errormsg = _("E495: no autocommand file name to substitute for \"<afile>\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00009356 return NULL;
9357 }
Bram Moolenaara0174af2008-01-02 20:08:25 +00009358 result = shorten_fname1(result);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009359 break;
9360
9361 case SPEC_ABUF: /* buffer number for autocommand */
9362 if (autocmd_bufnr <= 0)
9363 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009364 *errormsg = _("E496: no autocommand buffer number to substitute for \"<abuf>\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00009365 return NULL;
9366 }
9367 sprintf((char *)strbuf, "%d", autocmd_bufnr);
9368 result = strbuf;
9369 break;
9370
9371 case SPEC_AMATCH: /* match name for autocommand */
9372 result = autocmd_match;
9373 if (result == NULL)
9374 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009375 *errormsg = _("E497: no autocommand match name to substitute for \"<amatch>\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00009376 return NULL;
9377 }
9378 break;
9379
Bram Moolenaar071d4272004-06-13 20:20:40 +00009380 case SPEC_SFILE: /* file name for ":so" command */
9381 result = sourcing_name;
9382 if (result == NULL)
9383 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009384 *errormsg = _("E498: no :source file name to substitute for \"<sfile>\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00009385 return NULL;
9386 }
9387 break;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009388
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009389 case SPEC_SLNUM: /* line in file for ":so" command */
9390 if (sourcing_name == NULL || sourcing_lnum == 0)
9391 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009392 *errormsg = _("E842: no line number to use for \"<slnum>\"");
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009393 return NULL;
9394 }
9395 sprintf((char *)strbuf, "%ld", (long)sourcing_lnum);
9396 result = strbuf;
9397 break;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009398
9399#ifdef FEAT_EVAL
9400 case SPEC_SFLNUM: /* line in script file */
9401 if (current_sctx.sc_lnum + sourcing_lnum == 0)
9402 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009403 *errormsg = _("E961: no line number to use for \"<sflnum>\"");
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009404 return NULL;
9405 }
9406 sprintf((char *)strbuf, "%ld",
9407 (long)(current_sctx.sc_lnum + sourcing_lnum));
9408 result = strbuf;
9409 break;
9410#endif
9411
9412#ifdef FEAT_CLIENTSERVER
Bram Moolenaar071d4272004-06-13 20:20:40 +00009413 case SPEC_CLIENT: /* Source of last submitted input */
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009414 sprintf((char *)strbuf, PRINTF_HEX_LONG_U,
9415 (long_u)clientWindow);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009416 result = strbuf;
9417 break;
9418#endif
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009419
Bram Moolenaar9e0f6ec2017-05-16 09:36:54 +02009420 default:
9421 result = (char_u *)""; /* avoid gcc warning */
9422 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009423 }
9424
9425 resultlen = (int)STRLEN(result); /* length of new string */
9426 if (src[*usedlen] == '<') /* remove the file name extension */
9427 {
9428 ++*usedlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009429 if ((s = vim_strrchr(result, '.')) != NULL && s >= gettail(result))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009430 resultlen = (int)(s - result);
9431 }
9432#ifdef FEAT_MODIFY_FNAME
9433 else if (!skip_mod)
9434 {
Bram Moolenaar00136dc2018-07-25 21:19:13 +02009435 valid |= modify_fname(src, tilde_file, usedlen, &result, &resultbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009436 &resultlen);
9437 if (result == NULL)
9438 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009439 *errormsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +00009440 return NULL;
9441 }
9442 }
9443#endif
9444 }
9445
9446 if (resultlen == 0 || valid != VALID_HEAD + VALID_PATH)
9447 {
9448 if (valid != VALID_HEAD + VALID_PATH)
9449 /* xgettext:no-c-format */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009450 *errormsg = _("E499: Empty file name for '%' or '#', only works with \":p:h\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00009451 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009452 *errormsg = _("E500: Evaluates to an empty string");
Bram Moolenaar071d4272004-06-13 20:20:40 +00009453 result = NULL;
9454 }
9455 else
9456 result = vim_strnsave(result, resultlen);
9457 vim_free(resultbuf);
9458 return result;
9459}
9460
9461/*
9462 * Concatenate all files in the argument list, separated by spaces, and return
9463 * it in one allocated string.
9464 * Spaces and backslashes in the file names are escaped with a backslash.
9465 * Returns NULL when out of memory.
9466 */
9467 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009468arg_all(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009469{
9470 int len;
9471 int idx;
9472 char_u *retval = NULL;
9473 char_u *p;
9474
9475 /*
9476 * Do this loop two times:
9477 * first time: compute the total length
9478 * second time: concatenate the names
9479 */
9480 for (;;)
9481 {
9482 len = 0;
9483 for (idx = 0; idx < ARGCOUNT; ++idx)
9484 {
9485 p = alist_name(&ARGLIST[idx]);
9486 if (p != NULL)
9487 {
9488 if (len > 0)
9489 {
9490 /* insert a space in between names */
9491 if (retval != NULL)
9492 retval[len] = ' ';
9493 ++len;
9494 }
9495 for ( ; *p != NUL; ++p)
9496 {
Bram Moolenaar6e8d3b02015-06-09 21:33:31 +02009497 if (*p == ' '
9498#ifndef BACKSLASH_IN_FILENAME
9499 || *p == '\\'
9500#endif
Bram Moolenaar2c8c6812018-07-28 17:07:52 +02009501 || *p == '`')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009502 {
9503 /* insert a backslash */
9504 if (retval != NULL)
9505 retval[len] = '\\';
9506 ++len;
9507 }
9508 if (retval != NULL)
9509 retval[len] = *p;
9510 ++len;
9511 }
9512 }
9513 }
9514
9515 /* second time: break here */
9516 if (retval != NULL)
9517 {
9518 retval[len] = NUL;
9519 break;
9520 }
9521
9522 /* allocate memory */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00009523 retval = alloc((unsigned)len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009524 if (retval == NULL)
9525 break;
9526 }
9527
9528 return retval;
9529}
9530
Bram Moolenaar071d4272004-06-13 20:20:40 +00009531/*
9532 * Expand the <sfile> string in "arg".
9533 *
9534 * Returns an allocated string, or NULL for any error.
9535 */
9536 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009537expand_sfile(char_u *arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009538{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009539 char *errormsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009540 int len;
9541 char_u *result;
9542 char_u *newres;
9543 char_u *repl;
9544 int srclen;
9545 char_u *p;
9546
9547 result = vim_strsave(arg);
9548 if (result == NULL)
9549 return NULL;
9550
9551 for (p = result; *p; )
9552 {
9553 if (STRNCMP(p, "<sfile>", 7) != 0)
9554 ++p;
9555 else
9556 {
9557 /* replace "<sfile>" with the sourced file name, and do ":" stuff */
Bram Moolenaar63b92542007-03-27 14:57:09 +00009558 repl = eval_vars(p, result, &srclen, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009559 if (errormsg != NULL)
9560 {
9561 if (*errormsg)
9562 emsg(errormsg);
9563 vim_free(result);
9564 return NULL;
9565 }
9566 if (repl == NULL) /* no match (cannot happen) */
9567 {
9568 p += srclen;
9569 continue;
9570 }
9571 len = (int)STRLEN(result) - srclen + (int)STRLEN(repl) + 1;
9572 newres = alloc(len);
9573 if (newres == NULL)
9574 {
9575 vim_free(repl);
9576 vim_free(result);
9577 return NULL;
9578 }
9579 mch_memmove(newres, result, (size_t)(p - result));
9580 STRCPY(newres + (p - result), repl);
9581 len = (int)STRLEN(newres);
9582 STRCAT(newres, p + srclen);
9583 vim_free(repl);
9584 vim_free(result);
9585 result = newres;
9586 p = newres + len; /* continue after the match */
9587 }
9588 }
9589
9590 return result;
9591}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009592
9593#ifdef FEAT_SESSION
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01009594static int ses_winsizes(FILE *fd, int restore_size,
9595 win_T *tab_firstwin);
9596static int ses_win_rec(FILE *fd, frame_T *fr);
9597static frame_T *ses_skipframe(frame_T *fr);
9598static int ses_do_frame(frame_T *fr);
9599static int ses_do_win(win_T *wp);
9600static int ses_arglist(FILE *fd, char *cmd, garray_T *gap, int fullname, unsigned *flagp);
9601static int ses_put_fname(FILE *fd, char_u *name, unsigned *flagp);
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +02009602static int ses_fname(FILE *fd, buf_T *buf, unsigned *flagp, int add_eol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009603
9604/*
9605 * Write openfile commands for the current buffers to an .exrc file.
9606 * Return FAIL on error, OK otherwise.
9607 */
9608 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009609makeopens(
9610 FILE *fd,
9611 char_u *dirnow) /* Current directory name */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009612{
9613 buf_T *buf;
9614 int only_save_windows = TRUE;
9615 int nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009616 int restore_size = TRUE;
9617 win_T *wp;
9618 char_u *sname;
9619 win_T *edited_win = NULL;
Bram Moolenaar18144c82006-04-12 21:52:12 +00009620 int tabnr;
Bram Moolenaar04ad7fe2014-05-07 21:14:47 +02009621 int restore_stal = FALSE;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +00009622 win_T *tab_firstwin;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +00009623 frame_T *tab_topframe;
Bram Moolenaarf13be0d2008-01-02 14:13:10 +00009624 int cur_arg_idx = 0;
Bram Moolenaar383c6f52008-01-04 15:01:07 +00009625 int next_arg_idx = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009626
9627 if (ssop_flags & SSOP_BUFFERS)
9628 only_save_windows = FALSE; /* Save ALL buffers */
9629
9630 /*
9631 * Begin by setting the this_session variable, and then other
9632 * sessionable variables.
9633 */
9634#ifdef FEAT_EVAL
9635 if (put_line(fd, "let v:this_session=expand(\"<sfile>:p\")") == FAIL)
9636 return FAIL;
9637 if (ssop_flags & SSOP_GLOBALS)
9638 if (store_session_globals(fd) == FAIL)
9639 return FAIL;
9640#endif
9641
9642 /*
Bram Moolenaar26d4b892018-07-04 22:26:28 +02009643 * Close all windows and tabs but one.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009644 */
9645 if (put_line(fd, "silent only") == FAIL)
9646 return FAIL;
Bram Moolenaar26d4b892018-07-04 22:26:28 +02009647 if ((ssop_flags & SSOP_TABPAGES)
9648 && put_line(fd, "silent tabonly") == FAIL)
9649 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009650
9651 /*
9652 * Now a :cd command to the session directory or the current directory
9653 */
9654 if (ssop_flags & SSOP_SESDIR)
9655 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009656 if (put_line(fd, "exe \"cd \" . escape(expand(\"<sfile>:p:h\"), ' ')")
9657 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009658 return FAIL;
9659 }
9660 else if (ssop_flags & SSOP_CURDIR)
9661 {
9662 sname = home_replace_save(NULL, globaldir != NULL ? globaldir : dirnow);
9663 if (sname == NULL
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009664 || fputs("cd ", fd) < 0
9665 || ses_put_fname(fd, sname, &ssop_flags) == FAIL
9666 || put_eol(fd) == FAIL)
9667 {
9668 vim_free(sname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009669 return FAIL;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009670 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009671 vim_free(sname);
9672 }
9673
9674 /*
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009675 * If there is an empty, unnamed buffer we will wipe it out later.
9676 * Remember the buffer number.
9677 */
9678 if (put_line(fd, "if expand('%') == '' && !&modified && line('$') <= 1 && getline(1) == ''") == FAIL)
9679 return FAIL;
9680 if (put_line(fd, " let s:wipebuf = bufnr('%')") == FAIL)
9681 return FAIL;
9682 if (put_line(fd, "endif") == FAIL)
9683 return FAIL;
9684
9685 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009686 * Now save the current files, current buffer first.
9687 */
9688 if (put_line(fd, "set shortmess=aoO") == FAIL)
9689 return FAIL;
9690
Bram Moolenaar071d4272004-06-13 20:20:40 +00009691 /* the global argument list */
Bram Moolenaarf0bdd2f2014-03-12 21:28:26 +01009692 if (ses_arglist(fd, "argglobal", &global_alist.al_ga,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009693 !(ssop_flags & SSOP_CURDIR), &ssop_flags) == FAIL)
9694 return FAIL;
9695
9696 if (ssop_flags & SSOP_RESIZE)
9697 {
9698 /* Note: after the restore we still check it worked!*/
9699 if (fprintf(fd, "set lines=%ld columns=%ld" , Rows, Columns) < 0
9700 || put_eol(fd) == FAIL)
9701 return FAIL;
9702 }
9703
9704#ifdef FEAT_GUI
9705 if (gui.in_use && (ssop_flags & SSOP_WINPOS))
9706 {
9707 int x, y;
9708
9709 if (gui_mch_get_winpos(&x, &y) == OK)
9710 {
9711 /* Note: after the restore we still check it worked!*/
9712 if (fprintf(fd, "winpos %d %d", x, y) < 0 || put_eol(fd) == FAIL)
9713 return FAIL;
9714 }
9715 }
9716#endif
9717
9718 /*
Bram Moolenaar04ad7fe2014-05-07 21:14:47 +02009719 * When there are two or more tabpages and 'showtabline' is 1 the tabline
9720 * will be displayed when creating the next tab. That resizes the windows
9721 * in the first tab, which may cause problems. Set 'showtabline' to 2
9722 * temporarily to avoid that.
9723 */
9724 if (p_stal == 1 && first_tabpage->tp_next != NULL)
9725 {
9726 if (put_line(fd, "set stal=2") == FAIL)
9727 return FAIL;
9728 restore_stal = TRUE;
9729 }
9730
9731 /*
Bram Moolenaar18144c82006-04-12 21:52:12 +00009732 * May repeat putting Windows for each tab, when "tabpages" is in
9733 * 'sessionoptions'.
Bram Moolenaarcba2ae52006-10-24 10:59:57 +00009734 * Don't use goto_tabpage(), it may change directory and trigger
9735 * autocommands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009736 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +00009737 tab_firstwin = firstwin; /* first window in tab page "tabnr" */
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +00009738 tab_topframe = topframe;
Bram Moolenaar26d4b892018-07-04 22:26:28 +02009739 if ((ssop_flags & SSOP_TABPAGES))
9740 {
Bram Moolenaar57a6bf02019-01-22 21:27:13 +01009741 tabpage_T *tp;
Bram Moolenaar26d4b892018-07-04 22:26:28 +02009742
Bram Moolenaar57a6bf02019-01-22 21:27:13 +01009743 // Similar to ses_win_rec() below, populate the tab pages first so
9744 // later local options won't be copied to the new tabs.
9745 FOR_ALL_TABPAGES(tp)
9746 if (tp->tp_next != NULL && put_line(fd, "tabnew") == FAIL)
Bram Moolenaar26d4b892018-07-04 22:26:28 +02009747 return FAIL;
Bram Moolenaar57a6bf02019-01-22 21:27:13 +01009748 if (first_tabpage->tp_next != NULL && put_line(fd, "tabrewind") == FAIL)
Bram Moolenaar26d4b892018-07-04 22:26:28 +02009749 return FAIL;
9750 }
Bram Moolenaar18144c82006-04-12 21:52:12 +00009751 for (tabnr = 1; ; ++tabnr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009752 {
Bram Moolenaar00aa0692019-04-27 20:37:57 +02009753 tabpage_T *tp = NULL;
Bram Moolenaar26d4b892018-07-04 22:26:28 +02009754 int need_tabnext = FALSE;
Bram Moolenaar695baee2015-04-13 12:39:22 +02009755 int cnr = 1;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +00009756
Bram Moolenaar18144c82006-04-12 21:52:12 +00009757 if ((ssop_flags & SSOP_TABPAGES))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009758 {
Bram Moolenaar00aa0692019-04-27 20:37:57 +02009759 tp = find_tabpage(tabnr);
Bram Moolenaarcba2ae52006-10-24 10:59:57 +00009760
9761 if (tp == NULL)
9762 break; /* done all tab pages */
9763 if (tp == curtab)
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +00009764 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +00009765 tab_firstwin = firstwin;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +00009766 tab_topframe = topframe;
9767 }
Bram Moolenaarcba2ae52006-10-24 10:59:57 +00009768 else
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +00009769 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +00009770 tab_firstwin = tp->tp_firstwin;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +00009771 tab_topframe = tp->tp_topframe;
9772 }
Bram Moolenaarcba2ae52006-10-24 10:59:57 +00009773 if (tabnr > 1)
Bram Moolenaar26d4b892018-07-04 22:26:28 +02009774 need_tabnext = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009775 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009776
Bram Moolenaar18144c82006-04-12 21:52:12 +00009777 /*
9778 * Before creating the window layout, try loading one file. If this
9779 * is aborted we don't end up with a number of useless windows.
9780 * This may have side effects! (e.g., compressed or network file).
9781 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +00009782 for (wp = tab_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar18144c82006-04-12 21:52:12 +00009783 {
9784 if (ses_do_win(wp)
9785 && wp->w_buffer->b_ffname != NULL
Bram Moolenaar67883b42017-07-27 22:57:00 +02009786 && !bt_help(wp->w_buffer)
Bram Moolenaar18144c82006-04-12 21:52:12 +00009787#ifdef FEAT_QUICKFIX
9788 && !bt_nofile(wp->w_buffer)
9789#endif
9790 )
9791 {
Bram Moolenaar26d4b892018-07-04 22:26:28 +02009792 if (need_tabnext && put_line(fd, "tabnext") == FAIL)
9793 return FAIL;
9794 need_tabnext = FALSE;
9795
9796 if (fputs("edit ", fd) < 0
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +02009797 || ses_fname(fd, wp->w_buffer, &ssop_flags, TRUE)
9798 == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +00009799 return FAIL;
9800 if (!wp->w_arg_idx_invalid)
9801 edited_win = wp;
9802 break;
9803 }
9804 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009805
Bram Moolenaarcba2ae52006-10-24 10:59:57 +00009806 /* If no file got edited create an empty tab page. */
Bram Moolenaar26d4b892018-07-04 22:26:28 +02009807 if (need_tabnext && put_line(fd, "tabnext") == FAIL)
Bram Moolenaarcba2ae52006-10-24 10:59:57 +00009808 return FAIL;
9809
Bram Moolenaar18144c82006-04-12 21:52:12 +00009810 /*
9811 * Save current window layout.
9812 */
9813 if (put_line(fd, "set splitbelow splitright") == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009814 return FAIL;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +00009815 if (ses_win_rec(fd, tab_topframe) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009816 return FAIL;
Bram Moolenaar18144c82006-04-12 21:52:12 +00009817 if (!p_sb && put_line(fd, "set nosplitbelow") == FAIL)
9818 return FAIL;
9819 if (!p_spr && put_line(fd, "set nosplitright") == FAIL)
9820 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009821
Bram Moolenaar18144c82006-04-12 21:52:12 +00009822 /*
9823 * Check if window sizes can be restored (no windows omitted).
9824 * Remember the window number of the current window after restoring.
9825 */
9826 nr = 0;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +00009827 for (wp = tab_firstwin; wp != NULL; wp = W_NEXT(wp))
Bram Moolenaar18144c82006-04-12 21:52:12 +00009828 {
9829 if (ses_do_win(wp))
9830 ++nr;
9831 else
9832 restore_size = FALSE;
9833 if (curwin == wp)
9834 cnr = nr;
9835 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009836
Bram Moolenaar18144c82006-04-12 21:52:12 +00009837 /* Go to the first window. */
9838 if (put_line(fd, "wincmd t") == FAIL)
9839 return FAIL;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009840
Bram Moolenaar18144c82006-04-12 21:52:12 +00009841 /*
9842 * If more than one window, see if sizes can be restored.
9843 * First set 'winheight' and 'winwidth' to 1 to avoid the windows being
9844 * resized when moving between windows.
9845 * Do this before restoring the view, so that the topline and the
9846 * cursor can be set. This is done again below.
Bram Moolenaar36ae89c2017-01-28 17:11:14 +01009847 * winminheight and winminwidth need to be set to avoid an error if the
9848 * user has set winheight or winwidth.
Bram Moolenaar18144c82006-04-12 21:52:12 +00009849 */
Bram Moolenaar19834012018-06-12 17:03:39 +02009850 if (put_line(fd, "set winminheight=0") == FAIL
9851 || put_line(fd, "set winheight=1") == FAIL
9852 || put_line(fd, "set winminwidth=0") == FAIL
9853 || put_line(fd, "set winwidth=1") == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +00009854 return FAIL;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +00009855 if (nr > 1 && ses_winsizes(fd, restore_size, tab_firstwin) == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +00009856 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009857
Bram Moolenaar00aa0692019-04-27 20:37:57 +02009858 // Restore the tab-local working directory if specified
9859 // Do this before the windows, so that the window-local directory can
9860 // override the tab-local directory.
9861 if (tp != NULL && tp->tp_localdir != NULL && ssop_flags & SSOP_CURDIR)
9862 {
9863 if (fputs("tcd ", fd) < 0
9864 || ses_put_fname(fd, tp->tp_localdir, &ssop_flags) == FAIL
9865 || put_eol(fd) == FAIL)
9866 return FAIL;
9867 did_lcd = TRUE;
9868 }
9869
Bram Moolenaar18144c82006-04-12 21:52:12 +00009870 /*
9871 * Restore the view of the window (options, file, cursor, etc.).
9872 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +00009873 for (wp = tab_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar18144c82006-04-12 21:52:12 +00009874 {
9875 if (!ses_do_win(wp))
9876 continue;
Bram Moolenaarf13be0d2008-01-02 14:13:10 +00009877 if (put_view(fd, wp, wp != edited_win, &ssop_flags,
9878 cur_arg_idx) == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +00009879 return FAIL;
9880 if (nr > 1 && put_line(fd, "wincmd w") == FAIL)
9881 return FAIL;
Bram Moolenaarf13be0d2008-01-02 14:13:10 +00009882 next_arg_idx = wp->w_arg_idx;
Bram Moolenaar18144c82006-04-12 21:52:12 +00009883 }
9884
Bram Moolenaarf13be0d2008-01-02 14:13:10 +00009885 /* The argument index in the first tab page is zero, need to set it in
9886 * each window. For further tab pages it's the window where we do
9887 * "tabedit". */
9888 cur_arg_idx = next_arg_idx;
9889
Bram Moolenaar18144c82006-04-12 21:52:12 +00009890 /*
9891 * Restore cursor to the current window if it's not the first one.
9892 */
9893 if (cnr > 1 && (fprintf(fd, "%dwincmd w", cnr) < 0
9894 || put_eol(fd) == FAIL))
9895 return FAIL;
9896
9897 /*
Bram Moolenaar18144c82006-04-12 21:52:12 +00009898 * Restore window sizes again after jumping around in windows, because
9899 * the current window has a minimum size while others may not.
9900 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +00009901 if (nr > 1 && ses_winsizes(fd, restore_size, tab_firstwin) == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +00009902 return FAIL;
9903
Bram Moolenaar18144c82006-04-12 21:52:12 +00009904 /* Don't continue in another tab page when doing only the current one
9905 * or when at the last tab page. */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +00009906 if (!(ssop_flags & SSOP_TABPAGES))
Bram Moolenaar18144c82006-04-12 21:52:12 +00009907 break;
9908 }
9909
9910 if (ssop_flags & SSOP_TABPAGES)
9911 {
Bram Moolenaar18144c82006-04-12 21:52:12 +00009912 if (fprintf(fd, "tabnext %d", tabpage_index(curtab)) < 0
9913 || put_eol(fd) == FAIL)
9914 return FAIL;
9915 }
Bram Moolenaar04ad7fe2014-05-07 21:14:47 +02009916 if (restore_stal && put_line(fd, "set stal=1") == FAIL)
9917 return FAIL;
Bram Moolenaar18144c82006-04-12 21:52:12 +00009918
Bram Moolenaard39e2752019-01-26 20:07:38 +01009919 // Now put the remaining buffers into the buffer list.
9920 // This is near the end, so that when 'hidden' is set we don't create extra
9921 // buffers. If the buffer was already created with another command the
9922 // ":badd" will have no effect.
9923 FOR_ALL_BUFFERS(buf)
9924 {
9925 if (!(only_save_windows && buf->b_nwindows == 0)
9926 && !(buf->b_help && !(ssop_flags & SSOP_HELP))
9927#ifdef FEAT_TERMINAL
9928 // Skip terminal buffers: finished ones are not useful, others
9929 // will be resurrected and result in a new buffer.
9930 && !bt_terminal(buf)
9931#endif
9932 && buf->b_fname != NULL
9933 && buf->b_p_bl)
9934 {
9935 if (fprintf(fd, "badd +%ld ", buf->b_wininfo == NULL ? 1L
9936 : buf->b_wininfo->wi_fpos.lnum) < 0
9937 || ses_fname(fd, buf, &ssop_flags, TRUE) == FAIL)
9938 return FAIL;
9939 }
9940 }
9941
Bram Moolenaar9c102382006-05-03 21:26:49 +00009942 /*
9943 * Wipe out an empty unnamed buffer we started in.
9944 */
Bram Moolenaar26d4b892018-07-04 22:26:28 +02009945 if (put_line(fd, "if exists('s:wipebuf') && len(win_findbuf(s:wipebuf)) == 0")
Bram Moolenaar4d8bac82018-03-09 21:33:34 +01009946 == FAIL)
Bram Moolenaar9c102382006-05-03 21:26:49 +00009947 return FAIL;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00009948 if (put_line(fd, " silent exe 'bwipe ' . s:wipebuf") == FAIL)
Bram Moolenaar9c102382006-05-03 21:26:49 +00009949 return FAIL;
9950 if (put_line(fd, "endif") == FAIL)
9951 return FAIL;
9952 if (put_line(fd, "unlet! s:wipebuf") == FAIL)
9953 return FAIL;
9954
9955 /* Re-apply 'winheight', 'winwidth' and 'shortmess'. */
9956 if (fprintf(fd, "set winheight=%ld winwidth=%ld shortmess=%s",
9957 p_wh, p_wiw, p_shm) < 0 || put_eol(fd) == FAIL)
9958 return FAIL;
Bram Moolenaar36ae89c2017-01-28 17:11:14 +01009959 /* Re-apply 'winminheight' and 'winminwidth'. */
9960 if (fprintf(fd, "set winminheight=%ld winminwidth=%ld",
9961 p_wmh, p_wmw) < 0 || put_eol(fd) == FAIL)
9962 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009963
9964 /*
9965 * Lastly, execute the x.vim file if it exists.
9966 */
9967 if (put_line(fd, "let s:sx = expand(\"<sfile>:p:r\").\"x.vim\"") == FAIL
9968 || put_line(fd, "if file_readable(s:sx)") == FAIL
Bram Moolenaar42ba1262008-12-09 10:18:03 +00009969 || put_line(fd, " exe \"source \" . fnameescape(s:sx)") == FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +00009970 || put_line(fd, "endif") == FAIL)
9971 return FAIL;
9972
9973 return OK;
9974}
9975
9976 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009977ses_winsizes(
9978 FILE *fd,
9979 int restore_size,
9980 win_T *tab_firstwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009981{
9982 int n = 0;
9983 win_T *wp;
9984
9985 if (restore_size && (ssop_flags & SSOP_WINSIZE))
9986 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +00009987 for (wp = tab_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009988 {
9989 if (!ses_do_win(wp))
9990 continue;
9991 ++n;
9992
9993 /* restore height when not full height */
9994 if (wp->w_height + wp->w_status_height < topframe->fr_height
9995 && (fprintf(fd,
9996 "exe '%dresize ' . ((&lines * %ld + %ld) / %ld)",
9997 n, (long)wp->w_height, Rows / 2, Rows) < 0
9998 || put_eol(fd) == FAIL))
9999 return FAIL;
10000
10001 /* restore width when not full width */
10002 if (wp->w_width < Columns && (fprintf(fd,
10003 "exe 'vert %dresize ' . ((&columns * %ld + %ld) / %ld)",
10004 n, (long)wp->w_width, Columns / 2, Columns) < 0
10005 || put_eol(fd) == FAIL))
10006 return FAIL;
10007 }
10008 }
10009 else
10010 {
10011 /* Just equalise window sizes */
10012 if (put_line(fd, "wincmd =") == FAIL)
10013 return FAIL;
10014 }
10015 return OK;
10016}
10017
10018/*
10019 * Write commands to "fd" to recursively create windows for frame "fr",
10020 * horizontally and vertically split.
10021 * After the commands the last window in the frame is the current window.
10022 * Returns FAIL when writing the commands to "fd" fails.
10023 */
10024 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010025ses_win_rec(FILE *fd, frame_T *fr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010026{
10027 frame_T *frc;
10028 int count = 0;
10029
10030 if (fr->fr_layout != FR_LEAF)
10031 {
10032 /* Find first frame that's not skipped and then create a window for
10033 * each following one (first frame is already there). */
10034 frc = ses_skipframe(fr->fr_child);
10035 if (frc != NULL)
10036 while ((frc = ses_skipframe(frc->fr_next)) != NULL)
10037 {
10038 /* Make window as big as possible so that we have lots of room
10039 * to split. */
10040 if (put_line(fd, "wincmd _ | wincmd |") == FAIL
10041 || put_line(fd, fr->fr_layout == FR_COL
10042 ? "split" : "vsplit") == FAIL)
10043 return FAIL;
10044 ++count;
10045 }
10046
10047 /* Go back to the first window. */
10048 if (count > 0 && (fprintf(fd, fr->fr_layout == FR_COL
10049 ? "%dwincmd k" : "%dwincmd h", count) < 0
10050 || put_eol(fd) == FAIL))
10051 return FAIL;
10052
10053 /* Recursively create frames/windows in each window of this column or
10054 * row. */
10055 frc = ses_skipframe(fr->fr_child);
10056 while (frc != NULL)
10057 {
10058 ses_win_rec(fd, frc);
10059 frc = ses_skipframe(frc->fr_next);
10060 /* Go to next window. */
10061 if (frc != NULL && put_line(fd, "wincmd w") == FAIL)
10062 return FAIL;
10063 }
10064 }
10065 return OK;
10066}
10067
10068/*
10069 * Skip frames that don't contain windows we want to save in the Session.
10070 * Returns NULL when there none.
10071 */
10072 static frame_T *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010073ses_skipframe(frame_T *fr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010074{
10075 frame_T *frc;
10076
Bram Moolenaar3d1491e2018-12-22 17:07:50 +010010077 FOR_ALL_FRAMES(frc, fr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010078 if (ses_do_frame(frc))
10079 break;
10080 return frc;
10081}
10082
10083/*
10084 * Return TRUE if frame "fr" has a window somewhere that we want to save in
10085 * the Session.
10086 */
10087 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010088ses_do_frame(frame_T *fr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010089{
10090 frame_T *frc;
10091
10092 if (fr->fr_layout == FR_LEAF)
10093 return ses_do_win(fr->fr_win);
Bram Moolenaar3d1491e2018-12-22 17:07:50 +010010094 FOR_ALL_FRAMES(frc, fr->fr_child)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010095 if (ses_do_frame(frc))
10096 return TRUE;
10097 return FALSE;
10098}
10099
10100/*
10101 * Return non-zero if window "wp" is to be stored in the Session.
10102 */
10103 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010104ses_do_win(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010105{
Bram Moolenaar4d8bac82018-03-09 21:33:34 +010010106#ifdef FEAT_TERMINAL
10107 if (bt_terminal(wp->w_buffer))
10108 return !term_is_finished(wp->w_buffer)
10109 && (ssop_flags & SSOP_TERMINAL)
10110 && term_should_restore(wp->w_buffer);
10111#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010112 if (wp->w_buffer->b_fname == NULL
10113#ifdef FEAT_QUICKFIX
10114 /* When 'buftype' is "nofile" can't restore the window contents. */
10115 || bt_nofile(wp->w_buffer)
10116#endif
10117 )
10118 return (ssop_flags & SSOP_BLANK);
Bram Moolenaar67883b42017-07-27 22:57:00 +020010119 if (bt_help(wp->w_buffer))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010120 return (ssop_flags & SSOP_HELP);
10121 return TRUE;
10122}
10123
Bram Moolenaar92c1b692018-08-29 21:42:42 +020010124 static int
10125put_view_curpos(FILE *fd, win_T *wp, char *spaces)
10126{
10127 int r;
10128
10129 if (wp->w_curswant == MAXCOL)
10130 r = fprintf(fd, "%snormal! $", spaces);
10131 else
10132 r = fprintf(fd, "%snormal! 0%d|", spaces, wp->w_virtcol + 1);
10133 return r < 0 || put_eol(fd) == FAIL ? FALSE : OK;
10134}
10135
Bram Moolenaar071d4272004-06-13 20:20:40 +000010136/*
10137 * Write commands to "fd" to restore the view of a window.
10138 * Caller must make sure 'scrolloff' is zero.
10139 */
10140 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010141put_view(
10142 FILE *fd,
10143 win_T *wp,
10144 int add_edit, /* add ":edit" command to view */
10145 unsigned *flagp, /* vop_flags or ssop_flags */
10146 int current_arg_idx) /* current argument index of the window, use
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010147 * -1 if unknown */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010148{
10149 win_T *save_curwin;
10150 int f;
10151 int do_cursor;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000010152 int did_next = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010153
10154 /* Always restore cursor position for ":mksession". For ":mkview" only
10155 * when 'viewoptions' contains "cursor". */
10156 do_cursor = (flagp == &ssop_flags || *flagp & SSOP_CURSOR);
10157
10158 /*
10159 * Local argument list.
10160 */
10161 if (wp->w_alist == &global_alist)
10162 {
10163 if (put_line(fd, "argglobal") == FAIL)
10164 return FAIL;
10165 }
10166 else
10167 {
10168 if (ses_arglist(fd, "arglocal", &wp->w_alist->al_ga,
10169 flagp == &vop_flags
10170 || !(*flagp & SSOP_CURDIR)
10171 || wp->w_localdir != NULL, flagp) == FAIL)
10172 return FAIL;
10173 }
10174
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000010175 /* Only when part of a session: restore the argument index. Some
10176 * arguments may have been deleted, check if the index is valid. */
Bram Moolenaar51f53df2010-06-26 05:25:54 +020010177 if (wp->w_arg_idx != current_arg_idx && wp->w_arg_idx < WARGCOUNT(wp)
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000010178 && flagp == &ssop_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010179 {
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010180 if (fprintf(fd, "%ldargu", (long)wp->w_arg_idx + 1) < 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000010181 || put_eol(fd) == FAIL)
10182 return FAIL;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000010183 did_next = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010184 }
10185
10186 /* Edit the file. Skip this when ":next" already did it. */
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000010187 if (add_edit && (!did_next || wp->w_arg_idx_invalid))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010188 {
Bram Moolenaar4d8bac82018-03-09 21:33:34 +010010189# ifdef FEAT_TERMINAL
10190 if (bt_terminal(wp->w_buffer))
10191 {
10192 if (term_write_session(fd, wp) == FAIL)
10193 return FAIL;
10194 }
10195 else
10196# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010197 /*
10198 * Load the file.
10199 */
10200 if (wp->w_buffer->b_ffname != NULL
Bram Moolenaar4d8bac82018-03-09 21:33:34 +010010201# ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +000010202 && !bt_nofile(wp->w_buffer)
Bram Moolenaar4d8bac82018-03-09 21:33:34 +010010203# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010204 )
10205 {
10206 /*
10207 * Editing a file in this buffer: use ":edit file".
10208 * This may have side effects! (e.g., compressed or network file).
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +020010209 *
10210 * Note, if a buffer for that file already exists, use :badd to
10211 * edit that buffer, to not lose folding information (:edit resets
10212 * folds in other buffers)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010213 */
Bram Moolenaarad36a352019-01-24 13:34:42 +010010214 if (fputs("if bufexists(\"", fd) < 0
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +020010215 || ses_fname(fd, wp->w_buffer, flagp, FALSE) == FAIL
Bram Moolenaarad36a352019-01-24 13:34:42 +010010216 || fputs("\") | buffer ", fd) < 0
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +020010217 || ses_fname(fd, wp->w_buffer, flagp, FALSE) == FAIL
10218 || fputs(" | else | edit ", fd) < 0
10219 || ses_fname(fd, wp->w_buffer, flagp, FALSE) == FAIL
10220 || fputs(" | endif", fd) < 0
Bram Moolenaar4d8bac82018-03-09 21:33:34 +010010221 || put_eol(fd) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010222 return FAIL;
10223 }
10224 else
10225 {
10226 /* No file in this buffer, just make it empty. */
10227 if (put_line(fd, "enew") == FAIL)
10228 return FAIL;
10229#ifdef FEAT_QUICKFIX
10230 if (wp->w_buffer->b_ffname != NULL)
10231 {
10232 /* The buffer does have a name, but it's not a file name. */
10233 if (fputs("file ", fd) < 0
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +020010234 || ses_fname(fd, wp->w_buffer, flagp, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010235 return FAIL;
10236 }
10237#endif
10238 do_cursor = FALSE;
10239 }
10240 }
10241
10242 /*
10243 * Local mappings and abbreviations.
10244 */
10245 if ((*flagp & (SSOP_OPTIONS | SSOP_LOCALOPTIONS))
10246 && makemap(fd, wp->w_buffer) == FAIL)
10247 return FAIL;
10248
10249 /*
10250 * Local options. Need to go to the window temporarily.
10251 * Store only local values when using ":mkview" and when ":mksession" is
10252 * used and 'sessionoptions' doesn't include "options".
10253 * Some folding options are always stored when "folds" is included,
10254 * otherwise the folds would not be restored correctly.
10255 */
10256 save_curwin = curwin;
10257 curwin = wp;
10258 curbuf = curwin->w_buffer;
10259 if (*flagp & (SSOP_OPTIONS | SSOP_LOCALOPTIONS))
10260 f = makeset(fd, OPT_LOCAL,
10261 flagp == &vop_flags || !(*flagp & SSOP_OPTIONS));
10262#ifdef FEAT_FOLDING
10263 else if (*flagp & SSOP_FOLDS)
10264 f = makefoldset(fd);
10265#endif
10266 else
10267 f = OK;
10268 curwin = save_curwin;
10269 curbuf = curwin->w_buffer;
10270 if (f == FAIL)
10271 return FAIL;
10272
10273#ifdef FEAT_FOLDING
10274 /*
10275 * Save Folds when 'buftype' is empty and for help files.
10276 */
10277 if ((*flagp & SSOP_FOLDS)
10278 && wp->w_buffer->b_ffname != NULL
Bram Moolenaar91335e52018-08-01 17:53:12 +020010279 && (bt_normal(wp->w_buffer) || bt_help(wp->w_buffer)))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010280 {
10281 if (put_folds(fd, wp) == FAIL)
10282 return FAIL;
10283 }
10284#endif
10285
10286 /*
10287 * Set the cursor after creating folds, since that moves the cursor.
10288 */
10289 if (do_cursor)
10290 {
10291
10292 /* Restore the cursor line in the file and relatively in the
10293 * window. Don't use "G", it changes the jumplist. */
10294 if (fprintf(fd, "let s:l = %ld - ((%ld * winheight(0) + %ld) / %ld)",
10295 (long)wp->w_cursor.lnum,
10296 (long)(wp->w_cursor.lnum - wp->w_topline),
10297 (long)wp->w_height / 2, (long)wp->w_height) < 0
10298 || put_eol(fd) == FAIL
10299 || put_line(fd, "if s:l < 1 | let s:l = 1 | endif") == FAIL
10300 || put_line(fd, "exe s:l") == FAIL
10301 || put_line(fd, "normal! zt") == FAIL
10302 || fprintf(fd, "%ld", (long)wp->w_cursor.lnum) < 0
10303 || put_eol(fd) == FAIL)
10304 return FAIL;
10305 /* Restore the cursor column and left offset when not wrapping. */
10306 if (wp->w_cursor.col == 0)
10307 {
10308 if (put_line(fd, "normal! 0") == FAIL)
10309 return FAIL;
10310 }
10311 else
10312 {
10313 if (!wp->w_p_wrap && wp->w_leftcol > 0 && wp->w_width > 0)
10314 {
10315 if (fprintf(fd,
10316 "let s:c = %ld - ((%ld * winwidth(0) + %ld) / %ld)",
Bram Moolenaar558ddad2013-02-20 19:26:29 +010010317 (long)wp->w_virtcol + 1,
10318 (long)(wp->w_virtcol - wp->w_leftcol),
Bram Moolenaar071d4272004-06-13 20:20:40 +000010319 (long)wp->w_width / 2, (long)wp->w_width) < 0
10320 || put_eol(fd) == FAIL
10321 || put_line(fd, "if s:c > 0") == FAIL
10322 || fprintf(fd,
Bram Moolenaar558ddad2013-02-20 19:26:29 +010010323 " exe 'normal! ' . s:c . '|zs' . %ld . '|'",
10324 (long)wp->w_virtcol + 1) < 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000010325 || put_eol(fd) == FAIL
10326 || put_line(fd, "else") == FAIL
Bram Moolenaar92c1b692018-08-29 21:42:42 +020010327 || put_view_curpos(fd, wp, " ") == FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +000010328 || put_line(fd, "endif") == FAIL)
10329 return FAIL;
10330 }
Bram Moolenaar92c1b692018-08-29 21:42:42 +020010331 else if (put_view_curpos(fd, wp, "") == FAIL)
10332 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010333 }
10334 }
10335
10336 /*
Bram Moolenaar13e90412017-11-11 18:16:48 +010010337 * Local directory, if the current flag is not view options or the "curdir"
10338 * option is included.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010339 */
Bram Moolenaar13e90412017-11-11 18:16:48 +010010340 if (wp->w_localdir != NULL
10341 && (flagp != &vop_flags || (*flagp & SSOP_CURDIR)))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010342 {
10343 if (fputs("lcd ", fd) < 0
10344 || ses_put_fname(fd, wp->w_localdir, flagp) == FAIL
10345 || put_eol(fd) == FAIL)
10346 return FAIL;
Bram Moolenaareeefcc72007-05-01 21:21:21 +000010347 did_lcd = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010348 }
10349
10350 return OK;
10351}
10352
10353/*
10354 * Write an argument list to the session file.
10355 * Returns FAIL if writing fails.
10356 */
10357 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010358ses_arglist(
10359 FILE *fd,
10360 char *cmd,
10361 garray_T *gap,
10362 int fullname, /* TRUE: use full path name */
10363 unsigned *flagp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010364{
10365 int i;
Bram Moolenaard9462e32011-04-11 21:35:11 +020010366 char_u *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010367 char_u *s;
10368
Bram Moolenaarf0bdd2f2014-03-12 21:28:26 +010010369 if (fputs(cmd, fd) < 0 || put_eol(fd) == FAIL)
10370 return FAIL;
Bram Moolenaar555de4e2019-01-21 23:03:49 +010010371 if (put_line(fd, "%argdel") == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010372 return FAIL;
10373 for (i = 0; i < gap->ga_len; ++i)
10374 {
10375 /* NULL file names are skipped (only happens when out of memory). */
10376 s = alist_name(&((aentry_T *)gap->ga_data)[i]);
10377 if (s != NULL)
10378 {
10379 if (fullname)
10380 {
Bram Moolenaard9462e32011-04-11 21:35:11 +020010381 buf = alloc(MAXPATHL);
10382 if (buf != NULL)
10383 {
10384 (void)vim_FullName(s, buf, MAXPATHL, FALSE);
10385 s = buf;
10386 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010387 }
Bram Moolenaar79da5632017-02-01 22:52:44 +010010388 if (fputs("$argadd ", fd) < 0
Bram Moolenaarf0bdd2f2014-03-12 21:28:26 +010010389 || ses_put_fname(fd, s, flagp) == FAIL
10390 || put_eol(fd) == FAIL)
Bram Moolenaard9462e32011-04-11 21:35:11 +020010391 {
10392 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010393 return FAIL;
Bram Moolenaard9462e32011-04-11 21:35:11 +020010394 }
10395 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010396 }
10397 }
Bram Moolenaarf0bdd2f2014-03-12 21:28:26 +010010398 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010399}
10400
10401/*
10402 * Write a buffer name to the session file.
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +020010403 * Also ends the line, if "add_eol" is TRUE.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010404 * Returns FAIL if writing fails.
10405 */
10406 static int
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +020010407ses_fname(FILE *fd, buf_T *buf, unsigned *flagp, int add_eol)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010408{
10409 char_u *name;
10410
10411 /* Use the short file name if the current directory is known at the time
Bram Moolenaareeefcc72007-05-01 21:21:21 +000010412 * the session file will be sourced.
10413 * Don't do this for ":mkview", we don't know the current directory.
10414 * Don't do this after ":lcd", we don't keep track of what the current
10415 * directory is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010416 if (buf->b_sfname != NULL
10417 && flagp == &ssop_flags
Bram Moolenaareeefcc72007-05-01 21:21:21 +000010418 && (ssop_flags & (SSOP_CURDIR | SSOP_SESDIR))
Bram Moolenaar8d594672009-07-01 18:18:57 +000010419#ifdef FEAT_AUTOCHDIR
10420 && !p_acd
10421#endif
Bram Moolenaareeefcc72007-05-01 21:21:21 +000010422 && !did_lcd)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010423 name = buf->b_sfname;
10424 else
10425 name = buf->b_ffname;
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +020010426 if (ses_put_fname(fd, name, flagp) == FAIL
10427 || (add_eol && put_eol(fd) == FAIL))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010428 return FAIL;
10429 return OK;
10430}
10431
10432/*
10433 * Write a file name to the session file.
10434 * Takes care of the "slash" option in 'sessionoptions' and escapes special
10435 * characters.
Bram Moolenaar78f74a92010-10-13 17:50:07 +020010436 * Returns FAIL if writing fails or out of memory.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010437 */
10438 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010439ses_put_fname(FILE *fd, char_u *name, unsigned *flagp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010440{
10441 char_u *sname;
Bram Moolenaar78f74a92010-10-13 17:50:07 +020010442 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010443 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010444
10445 sname = home_replace_save(NULL, name);
Bram Moolenaar78f74a92010-10-13 17:50:07 +020010446 if (sname == NULL)
10447 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010448
Bram Moolenaar78f74a92010-10-13 17:50:07 +020010449 if (*flagp & SSOP_SLASH)
10450 {
10451 /* change all backslashes to forward slashes */
Bram Moolenaar91acfff2017-03-12 19:22:36 +010010452 for (p = sname; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar78f74a92010-10-13 17:50:07 +020010453 if (*p == '\\')
10454 *p = '/';
Bram Moolenaar071d4272004-06-13 20:20:40 +000010455 }
Bram Moolenaar78f74a92010-10-13 17:50:07 +020010456
Bram Moolenaar84a05ac2013-05-06 04:24:17 +020010457 /* escape special characters */
Bram Moolenaar78f74a92010-10-13 17:50:07 +020010458 p = vim_strsave_fnameescape(sname, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010459 vim_free(sname);
Bram Moolenaar78f74a92010-10-13 17:50:07 +020010460 if (p == NULL)
10461 return FAIL;
10462
10463 /* write the result */
10464 if (fputs((char *)p, fd) < 0)
10465 retval = FAIL;
10466
10467 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010468 return retval;
10469}
10470
10471/*
10472 * ":loadview [nr]"
10473 */
10474 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010475ex_loadview(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010476{
10477 char_u *fname;
10478
10479 fname = get_view_file(*eap->arg);
10480 if (fname != NULL)
10481 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +000010482 do_source(fname, FALSE, DOSO_NONE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010483 vim_free(fname);
10484 }
10485}
10486
10487/*
10488 * Get the name of the view file for the current buffer.
10489 */
10490 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010491get_view_file(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010492{
10493 int len = 0;
10494 char_u *p, *s;
10495 char_u *retval;
10496 char_u *sname;
10497
10498 if (curbuf->b_ffname == NULL)
10499 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010500 emsg(_(e_noname));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010501 return NULL;
10502 }
10503 sname = home_replace_save(NULL, curbuf->b_ffname);
10504 if (sname == NULL)
10505 return NULL;
10506
10507 /*
10508 * We want a file name without separators, because we're not going to make
10509 * a directory.
10510 * "normal" path separator -> "=+"
10511 * "=" -> "=="
10512 * ":" path separator -> "=-"
10513 */
10514 for (p = sname; *p; ++p)
10515 if (*p == '=' || vim_ispathsep(*p))
10516 ++len;
10517 retval = alloc((unsigned)(STRLEN(sname) + len + STRLEN(p_vdir) + 9));
10518 if (retval != NULL)
10519 {
10520 STRCPY(retval, p_vdir);
10521 add_pathsep(retval);
10522 s = retval + STRLEN(retval);
10523 for (p = sname; *p; ++p)
10524 {
10525 if (*p == '=')
10526 {
10527 *s++ = '=';
10528 *s++ = '=';
10529 }
10530 else if (vim_ispathsep(*p))
10531 {
10532 *s++ = '=';
Bram Moolenaare60acc12011-05-10 16:41:25 +020010533#if defined(BACKSLASH_IN_FILENAME) || defined(AMIGA) || defined(VMS)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010534 if (*p == ':')
10535 *s++ = '-';
10536 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010537#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +000010538 *s++ = '+';
Bram Moolenaar071d4272004-06-13 20:20:40 +000010539 }
10540 else
10541 *s++ = *p;
10542 }
10543 *s++ = '=';
10544 *s++ = c;
10545 STRCPY(s, ".vim");
10546 }
10547
10548 vim_free(sname);
10549 return retval;
10550}
10551
10552#endif /* FEAT_SESSION */
10553
10554/*
10555 * Write end-of-line character(s) for ":mkexrc", ":mkvimrc" and ":mksession".
10556 * Return FAIL for a write error.
10557 */
10558 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010559put_eol(FILE *fd)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010560{
10561 if (
10562#ifdef USE_CRNL
10563 (
10564# ifdef MKSESSION_NL
10565 !mksession_nl &&
10566# endif
10567 (putc('\r', fd) < 0)) ||
10568#endif
10569 (putc('\n', fd) < 0))
10570 return FAIL;
10571 return OK;
10572}
10573
10574/*
10575 * Write a line to "fd".
10576 * Return FAIL for a write error.
10577 */
10578 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010579put_line(FILE *fd, char *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010580{
10581 if (fputs(s, fd) < 0 || put_eol(fd) == FAIL)
10582 return FAIL;
10583 return OK;
10584}
10585
10586#ifdef FEAT_VIMINFO
10587/*
10588 * ":rviminfo" and ":wviminfo".
10589 */
10590 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010591ex_viminfo(
10592 exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010593{
10594 char_u *save_viminfo;
10595
10596 save_viminfo = p_viminfo;
10597 if (*p_viminfo == NUL)
10598 p_viminfo = (char_u *)"'100";
10599 if (eap->cmdidx == CMD_rviminfo)
10600 {
Bram Moolenaard812df62008-11-09 12:46:09 +000010601 if (read_viminfo(eap->arg, VIF_WANT_INFO | VIF_WANT_MARKS
10602 | (eap->forceit ? VIF_FORCEIT : 0)) == FAIL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010603 emsg(_("E195: Cannot open viminfo file for reading"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010604 }
10605 else
10606 write_viminfo(eap->arg, eap->forceit);
10607 p_viminfo = save_viminfo;
10608}
10609#endif
10610
10611#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG) || defined(PROTO)
Bram Moolenaar9c13b352005-05-19 20:53:52 +000010612/*
Bram Moolenaard9462e32011-04-11 21:35:11 +020010613 * Make a dialog message in "buff[DIALOG_MSG_SIZE]".
Bram Moolenaarb765d632005-06-07 21:00:02 +000010614 * "format" must contain "%s".
Bram Moolenaar9c13b352005-05-19 20:53:52 +000010615 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010616 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010617dialog_msg(char_u *buff, char *format, char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010618{
Bram Moolenaar071d4272004-06-13 20:20:40 +000010619 if (fname == NULL)
10620 fname = (char_u *)_("Untitled");
Bram Moolenaard9462e32011-04-11 21:35:11 +020010621 vim_snprintf((char *)buff, DIALOG_MSG_SIZE, format, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010622}
10623#endif
10624
10625/*
10626 * ":behave {mswin,xterm}"
10627 */
10628 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010629ex_behave(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010630{
10631 if (STRCMP(eap->arg, "mswin") == 0)
10632 {
10633 set_option_value((char_u *)"selection", 0L, (char_u *)"exclusive", 0);
10634 set_option_value((char_u *)"selectmode", 0L, (char_u *)"mouse,key", 0);
10635 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"popup", 0);
10636 set_option_value((char_u *)"keymodel", 0L,
10637 (char_u *)"startsel,stopsel", 0);
10638 }
10639 else if (STRCMP(eap->arg, "xterm") == 0)
10640 {
10641 set_option_value((char_u *)"selection", 0L, (char_u *)"inclusive", 0);
10642 set_option_value((char_u *)"selectmode", 0L, (char_u *)"", 0);
10643 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"extend", 0);
10644 set_option_value((char_u *)"keymodel", 0L, (char_u *)"", 0);
10645 }
10646 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010647 semsg(_(e_invarg2), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010648}
10649
Bram Moolenaar42b4dda2010-03-02 15:56:05 +010010650#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
10651/*
10652 * Function given to ExpandGeneric() to obtain the possible arguments of the
10653 * ":behave {mswin,xterm}" command.
10654 */
10655 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010656get_behave_arg(expand_T *xp UNUSED, int idx)
Bram Moolenaar42b4dda2010-03-02 15:56:05 +010010657{
10658 if (idx == 0)
10659 return (char_u *)"mswin";
10660 if (idx == 1)
10661 return (char_u *)"xterm";
10662 return NULL;
10663}
Bram Moolenaar9e507ca2016-10-15 15:39:39 +020010664
10665/*
10666 * Function given to ExpandGeneric() to obtain the possible arguments of the
10667 * ":messages {clear}" command.
10668 */
10669 char_u *
10670get_messages_arg(expand_T *xp UNUSED, int idx)
10671{
10672 if (idx == 0)
10673 return (char_u *)"clear";
10674 return NULL;
10675}
Bram Moolenaar42b4dda2010-03-02 15:56:05 +010010676#endif
10677
Bram Moolenaar113e1072019-01-20 15:30:40 +010010678#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
Bram Moolenaarcae92dc2017-08-06 15:22:15 +020010679 char_u *
10680get_mapclear_arg(expand_T *xp UNUSED, int idx)
10681{
10682 if (idx == 0)
10683 return (char_u *)"<buffer>";
10684 return NULL;
10685}
Bram Moolenaar113e1072019-01-20 15:30:40 +010010686#endif
Bram Moolenaarcae92dc2017-08-06 15:22:15 +020010687
Bram Moolenaar071d4272004-06-13 20:20:40 +000010688static int filetype_detect = FALSE;
10689static int filetype_plugin = FALSE;
10690static int filetype_indent = FALSE;
10691
10692/*
10693 * ":filetype [plugin] [indent] {on,off,detect}"
10694 * on: Load the filetype.vim file to install autocommands for file types.
10695 * off: Load the ftoff.vim file to remove all autocommands for file types.
10696 * plugin on: load filetype.vim and ftplugin.vim
10697 * plugin off: load ftplugof.vim
10698 * indent on: load filetype.vim and indent.vim
10699 * indent off: load indoff.vim
10700 */
10701 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010702ex_filetype(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010703{
10704 char_u *arg = eap->arg;
10705 int plugin = FALSE;
10706 int indent = FALSE;
10707
10708 if (*eap->arg == NUL)
10709 {
10710 /* Print current status. */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010711 smsg("filetype detection:%s plugin:%s indent:%s",
Bram Moolenaar071d4272004-06-13 20:20:40 +000010712 filetype_detect ? "ON" : "OFF",
10713 filetype_plugin ? (filetype_detect ? "ON" : "(on)") : "OFF",
10714 filetype_indent ? (filetype_detect ? "ON" : "(on)") : "OFF");
10715 return;
10716 }
10717
10718 /* Accept "plugin" and "indent" in any order. */
10719 for (;;)
10720 {
10721 if (STRNCMP(arg, "plugin", 6) == 0)
10722 {
10723 plugin = TRUE;
10724 arg = skipwhite(arg + 6);
10725 continue;
10726 }
10727 if (STRNCMP(arg, "indent", 6) == 0)
10728 {
10729 indent = TRUE;
10730 arg = skipwhite(arg + 6);
10731 continue;
10732 }
10733 break;
10734 }
10735 if (STRCMP(arg, "on") == 0 || STRCMP(arg, "detect") == 0)
10736 {
10737 if (*arg == 'o' || !filetype_detect)
10738 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010010739 source_runtime((char_u *)FILETYPE_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010740 filetype_detect = TRUE;
10741 if (plugin)
10742 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010010743 source_runtime((char_u *)FTPLUGIN_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010744 filetype_plugin = TRUE;
10745 }
10746 if (indent)
10747 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010010748 source_runtime((char_u *)INDENT_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010749 filetype_indent = TRUE;
10750 }
10751 }
10752 if (*arg == 'd')
10753 {
Bram Moolenaar1610d052016-06-09 22:53:01 +020010754 (void)do_doautocmd((char_u *)"filetypedetect BufRead", TRUE, NULL);
Bram Moolenaara3227e22006-03-08 21:32:40 +000010755 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010756 }
10757 }
10758 else if (STRCMP(arg, "off") == 0)
10759 {
10760 if (plugin || indent)
10761 {
10762 if (plugin)
10763 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010010764 source_runtime((char_u *)FTPLUGOF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010765 filetype_plugin = FALSE;
10766 }
10767 if (indent)
10768 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010010769 source_runtime((char_u *)INDOFF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010770 filetype_indent = FALSE;
10771 }
10772 }
10773 else
10774 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010010775 source_runtime((char_u *)FTOFF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010776 filetype_detect = FALSE;
10777 }
10778 }
10779 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010780 semsg(_(e_invarg2), arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010781}
10782
10783/*
Bram Moolenaar3e545692017-06-04 19:00:32 +020010784 * ":setfiletype [FALLBACK] {name}"
Bram Moolenaar071d4272004-06-13 20:20:40 +000010785 */
10786 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010787ex_setfiletype(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010788{
10789 if (!did_filetype)
Bram Moolenaar3e545692017-06-04 19:00:32 +020010790 {
10791 char_u *arg = eap->arg;
10792
10793 if (STRNCMP(arg, "FALLBACK ", 9) == 0)
10794 arg += 9;
10795
10796 set_option_value((char_u *)"filetype", 0L, arg, OPT_LOCAL);
10797 if (arg != eap->arg)
10798 did_filetype = FALSE;
10799 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010800}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010801
Bram Moolenaar071d4272004-06-13 20:20:40 +000010802 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010803ex_digraphs(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010804{
10805#ifdef FEAT_DIGRAPHS
10806 if (*eap->arg != NUL)
10807 putdigraph(eap->arg);
10808 else
Bram Moolenaareae8ae12018-12-14 18:53:02 +010010809 listdigraphs(eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010810#else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010811 emsg(_("E196: No digraphs in this version"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010812#endif
10813}
10814
10815 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010816ex_set(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010817{
10818 int flags = 0;
10819
10820 if (eap->cmdidx == CMD_setlocal)
10821 flags = OPT_LOCAL;
10822 else if (eap->cmdidx == CMD_setglobal)
10823 flags = OPT_GLOBAL;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +010010824#if defined(FEAT_EVAL) && defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010825 if (cmdmod.browse && flags == 0)
10826 ex_options(eap);
10827 else
10828#endif
10829 (void)do_set(eap->arg, flags);
10830}
10831
Bram Moolenaar451fc7b2018-04-27 22:53:07 +020010832#if defined(FEAT_SEARCH_EXTRA) || defined(PROTO)
10833 void
10834set_no_hlsearch(int flag)
10835{
10836 no_hlsearch = flag;
10837# ifdef FEAT_EVAL
10838 set_vim_var_nr(VV_HLSEARCH, !no_hlsearch && p_hls);
10839# endif
10840}
10841
Bram Moolenaar071d4272004-06-13 20:20:40 +000010842/*
10843 * ":nohlsearch"
10844 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010845 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010846ex_nohlsearch(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010847{
Bram Moolenaar451fc7b2018-04-27 22:53:07 +020010848 set_no_hlsearch(TRUE);
Bram Moolenaarf71a3db2006-03-12 21:50:18 +000010849 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010850}
10851
10852/*
Bram Moolenaare1438bb2006-03-01 22:01:55 +000010853 * ":[N]match {group} {pattern}"
Bram Moolenaar071d4272004-06-13 20:20:40 +000010854 * Sets nextcmd to the start of the next command, if any. Also called when
10855 * skipping commands to find the next command.
10856 */
10857 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010858ex_match(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010859{
10860 char_u *p;
Bram Moolenaar52d36c82007-08-11 14:00:30 +000010861 char_u *g = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010862 char_u *end;
10863 int c;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000010864 int id;
Bram Moolenaare1438bb2006-03-01 22:01:55 +000010865
10866 if (eap->line2 <= 3)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000010867 id = eap->line2;
Bram Moolenaare1438bb2006-03-01 22:01:55 +000010868 else
10869 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010870 emsg(_(e_invcmd));
Bram Moolenaare1438bb2006-03-01 22:01:55 +000010871 return;
10872 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010873
10874 /* First clear any old pattern. */
10875 if (!eap->skip)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000010876 match_delete(curwin, id, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010877
10878 if (ends_excmd(*eap->arg))
10879 end = eap->arg;
10880 else if ((STRNICMP(eap->arg, "none", 4) == 0
Bram Moolenaar1c465442017-03-12 20:10:05 +010010881 && (VIM_ISWHITE(eap->arg[4]) || ends_excmd(eap->arg[4]))))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010882 end = eap->arg + 4;
10883 else
10884 {
10885 p = skiptowhite(eap->arg);
10886 if (!eap->skip)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000010887 g = vim_strnsave(eap->arg, (int)(p - eap->arg));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010888 p = skipwhite(p);
10889 if (*p == NUL)
10890 {
10891 /* There must be two arguments. */
Bram Moolenaar9a7d58e2015-11-24 17:23:56 +010010892 vim_free(g);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010893 semsg(_(e_invarg2), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010894 return;
10895 }
10896 end = skip_regexp(p + 1, *p, TRUE, NULL);
10897 if (!eap->skip)
10898 {
10899 if (*end != NUL && !ends_excmd(*skipwhite(end + 1)))
10900 {
Bram Moolenaar9a7d58e2015-11-24 17:23:56 +010010901 vim_free(g);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010902 eap->errmsg = e_trailing;
10903 return;
10904 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000010905 if (*end != *p)
10906 {
Bram Moolenaar9a7d58e2015-11-24 17:23:56 +010010907 vim_free(g);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010908 semsg(_(e_invarg2), p);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000010909 return;
10910 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010911
10912 c = *end;
10913 *end = NUL;
Bram Moolenaar6561d522015-07-21 15:48:27 +020010914 match_add(curwin, g, p + 1, 10, id, NULL, NULL);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000010915 vim_free(g);
Bram Moolenaar910f66f2006-04-05 20:41:53 +000010916 *end = c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010917 }
10918 }
10919 eap->nextcmd = find_nextcmd(end);
10920}
10921#endif
10922
10923#ifdef FEAT_CRYPT
10924/*
10925 * ":X": Get crypt key
10926 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010927 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010928ex_X(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010929{
Bram Moolenaar3a0c9082014-11-12 15:15:42 +010010930 crypt_check_current_method();
Bram Moolenaar8f4ac012014-08-10 13:38:34 +020010931 (void)crypt_get_key(TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010932}
10933#endif
10934
10935#ifdef FEAT_FOLDING
10936 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010937ex_fold(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010938{
10939 if (foldManualAllowed(TRUE))
10940 foldCreate(eap->line1, eap->line2);
10941}
10942
10943 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010944ex_foldopen(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010945{
10946 opFoldRange(eap->line1, eap->line2, eap->cmdidx == CMD_foldopen,
10947 eap->forceit, FALSE);
10948}
10949
10950 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010951ex_folddo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010952{
10953 linenr_T lnum;
10954
Bram Moolenaar6b1ee342014-08-06 18:17:11 +020010955#ifdef FEAT_CLIPBOARD
10956 start_global_changes();
10957#endif
10958
Bram Moolenaar071d4272004-06-13 20:20:40 +000010959 /* First set the marks for all lines closed/open. */
10960 for (lnum = eap->line1; lnum <= eap->line2; ++lnum)
10961 if (hasFolding(lnum, NULL, NULL) == (eap->cmdidx == CMD_folddoclosed))
10962 ml_setmarked(lnum);
10963
10964 /* Execute the command on the marked lines. */
10965 global_exe(eap->arg);
10966 ml_clearmarked(); /* clear rest of the marks */
Bram Moolenaar6b1ee342014-08-06 18:17:11 +020010967#ifdef FEAT_CLIPBOARD
10968 end_global_changes();
10969#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010970}
10971#endif
Bram Moolenaarf5291f32017-09-14 22:55:37 +020010972
Bram Moolenaar39665952018-08-15 20:59:48 +020010973#ifdef FEAT_QUICKFIX
10974/*
10975 * Returns TRUE if the supplied Ex cmdidx is for a location list command
10976 * instead of a quickfix command.
10977 */
10978 int
10979is_loclist_cmd(int cmdidx)
10980{
Bram Moolenaar74c8be22018-08-23 22:51:40 +020010981 if (cmdidx < 0 || cmdidx >= CMD_SIZE)
Bram Moolenaar39665952018-08-15 20:59:48 +020010982 return FALSE;
10983 return cmdnames[cmdidx].cmd_name[0] == 'l';
10984}
10985#endif
10986
Bram Moolenaarf5291f32017-09-14 22:55:37 +020010987# if defined(FEAT_TIMERS) || defined(PROTO)
10988 int
10989get_pressedreturn(void)
10990{
10991 return ex_pressedreturn;
10992}
10993
10994 void
10995set_pressedreturn(int val)
10996{
10997 ex_pressedreturn = val;
10998}
10999#endif