blob: cc6a920a23a0e036cd35e8113edf4c64d1ea9d64 [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
Bram Moolenaar3ff33112019-05-03 21:56:35 +020059# define ex_cbelow ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +000060# define ex_cfile ex_ni
61# define qf_list ex_ni
62# define qf_age ex_ni
Bram Moolenaarf6acffb2016-07-16 16:54:24 +020063# define qf_history ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +000064# define ex_helpgrep ex_ni
Bram Moolenaar86b68352004-12-27 21:59:20 +000065# define ex_vimgrep ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +000066#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +020067#if !defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +000068# define ex_cclose ex_ni
69# define ex_copen ex_ni
70# define ex_cwindow ex_ni
Bram Moolenaardcb17002016-07-07 18:58:59 +020071# define ex_cbottom ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +000072#endif
Bram Moolenaar1e015462005-09-25 22:16:38 +000073#if !defined(FEAT_QUICKFIX) || !defined(FEAT_EVAL)
74# define ex_cexpr ex_ni
75#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000076
Bram Moolenaarb7316892019-05-01 18:08:42 +020077static linenr_T get_address(exarg_T *, char_u **, cmd_addr_T addr_type, int skip, int silent, int to_other_file, int address_count);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010078static void get_flags(exarg_T *eap);
Bram Moolenaar85363ab2010-07-18 13:58:26 +020079#if !defined(FEAT_PERL) \
80 || !defined(FEAT_PYTHON) || !defined(FEAT_PYTHON3) \
81 || !defined(FEAT_TCL) \
82 || !defined(FEAT_RUBY) \
83 || !defined(FEAT_LUA) \
84 || !defined(FEAT_MZSCHEME)
Bram Moolenaar7bb75552007-07-16 18:39:49 +000085# define HAVE_EX_SCRIPT_NI
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010086static void ex_script_ni(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +000087#endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010088static char *invalid_range(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010089static void correct_range(exarg_T *eap);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000090#ifdef FEAT_QUICKFIX
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010091static char_u *replace_makeprg(exarg_T *eap, char_u *p, char_u **cmdlinep);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000092#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010093static char_u *repl_cmdline(exarg_T *eap, char_u *src, int srclen, char_u *repl, char_u **cmdlinep);
94static void ex_highlight(exarg_T *eap);
95static void ex_colorscheme(exarg_T *eap);
96static void ex_quit(exarg_T *eap);
97static void ex_cquit(exarg_T *eap);
98static void ex_quit_all(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010099static void ex_close(exarg_T *eap);
100static void ex_win_close(int forceit, win_T *win, tabpage_T *tp);
101static void ex_only(exarg_T *eap);
102static void ex_resize(exarg_T *eap);
103static void ex_stag(exarg_T *eap);
104static void ex_tabclose(exarg_T *eap);
105static void ex_tabonly(exarg_T *eap);
106static void ex_tabnext(exarg_T *eap);
107static void ex_tabmove(exarg_T *eap);
108static void ex_tabs(exarg_T *eap);
Bram Moolenaar4033c552017-09-16 20:54:51 +0200109#if defined(FEAT_QUICKFIX)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100110static void ex_pclose(exarg_T *eap);
111static void ex_ptag(exarg_T *eap);
112static void ex_pedit(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000113#else
114# define ex_pclose ex_ni
115# define ex_ptag ex_ni
116# define ex_pedit ex_ni
117#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100118static void ex_hide(exarg_T *eap);
119static void ex_stop(exarg_T *eap);
120static void ex_exit(exarg_T *eap);
121static void ex_print(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000122#ifdef FEAT_BYTEOFF
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100123static void ex_goto(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000124#else
125# define ex_goto ex_ni
126#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100127static void ex_shell(exarg_T *eap);
128static void ex_preserve(exarg_T *eap);
129static void ex_recover(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100130static void ex_mode(exarg_T *eap);
131static void ex_wrongmodifier(exarg_T *eap);
132static void ex_find(exarg_T *eap);
133static void ex_open(exarg_T *eap);
134static void ex_edit(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000135#ifndef FEAT_GUI
136# define ex_gui ex_nogui
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100137static void ex_nogui(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000138#endif
Bram Moolenaar4f974752019-02-17 17:44:42 +0100139#if defined(FEAT_GUI_MSWIN) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100140static void ex_tearoff(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000141#else
142# define ex_tearoff ex_ni
143#endif
Bram Moolenaar29a2c082018-03-05 21:06:23 +0100144#if (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK) \
145 || defined(FEAT_TERM_POPUP_MENU)) && defined(FEAT_MENU)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100146static void ex_popup(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000147#else
148# define ex_popup ex_ni
149#endif
150#ifndef FEAT_GUI_MSWIN
151# define ex_simalt ex_ni
152#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +0000153#if !defined(FEAT_GUI_MSWIN) && !defined(FEAT_GUI_GTK) && !defined(FEAT_GUI_MOTIF)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000154# define gui_mch_find_dialog ex_ni
155# define gui_mch_replace_dialog ex_ni
156#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +0000157#if !defined(FEAT_GUI_GTK)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000158# define ex_helpfind ex_ni
159#endif
160#ifndef FEAT_CSCOPE
Bram Moolenaard4db7712016-11-12 19:16:46 +0100161# define ex_cscope ex_ni
162# define ex_scscope ex_ni
163# define ex_cstag ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000164#endif
165#ifndef FEAT_SYN_HL
166# define ex_syntax ex_ni
Bram Moolenaar860cae12010-06-05 23:22:07 +0200167# define ex_ownsyntax ex_ni
Bram Moolenaarf71a3db2006-03-12 21:50:18 +0000168#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100169#ifndef FEAT_EVAL
170# define ex_packadd ex_ni
171# define ex_packloadall ex_ni
172#endif
Bram Moolenaarf7512552013-06-06 14:55:19 +0200173#if !defined(FEAT_SYN_HL) || !defined(FEAT_PROFILE)
Bram Moolenaar8a7f5a22013-06-06 14:01:46 +0200174# define ex_syntime ex_ni
175#endif
Bram Moolenaarf71a3db2006-03-12 21:50:18 +0000176#ifndef FEAT_SPELL
Bram Moolenaarb765d632005-06-07 21:00:02 +0000177# define ex_spell ex_ni
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000178# define ex_mkspell ex_ni
Bram Moolenaarf417f2b2005-06-23 22:29:21 +0000179# define ex_spelldump ex_ni
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000180# define ex_spellinfo ex_ni
Bram Moolenaara1ba8112005-06-28 23:23:32 +0000181# define ex_spellrepall ex_ni
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000182#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200183#ifndef FEAT_PERSISTENT_UNDO
184# define ex_rundo ex_ni
185# define ex_wundo ex_ni
186#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200187#ifndef FEAT_LUA
188# define ex_lua ex_script_ni
189# define ex_luado ex_ni
190# define ex_luafile ex_ni
191#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000192#ifndef FEAT_MZSCHEME
193# define ex_mzscheme ex_script_ni
194# define ex_mzfile ex_ni
195#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000196#ifndef FEAT_PERL
197# define ex_perl ex_script_ni
198# define ex_perldo ex_ni
199#endif
200#ifndef FEAT_PYTHON
201# define ex_python ex_script_ni
Bram Moolenaard620aa92013-05-17 16:40:06 +0200202# define ex_pydo ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000203# define ex_pyfile ex_ni
204#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200205#ifndef FEAT_PYTHON3
Bram Moolenaar368373e2010-07-19 20:46:22 +0200206# define ex_py3 ex_script_ni
Bram Moolenaar3dab2802013-05-15 18:28:13 +0200207# define ex_py3do ex_ni
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200208# define ex_py3file ex_ni
209#endif
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +0100210#if !defined(FEAT_PYTHON) && !defined(FEAT_PYTHON3)
211# define ex_pyx ex_script_ni
212# define ex_pyxdo ex_ni
213# define ex_pyxfile ex_ni
214#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000215#ifndef FEAT_TCL
216# define ex_tcl ex_script_ni
217# define ex_tcldo ex_ni
218# define ex_tclfile ex_ni
219#endif
220#ifndef FEAT_RUBY
221# define ex_ruby ex_script_ni
222# define ex_rubydo ex_ni
223# define ex_rubyfile ex_ni
224#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000225#ifndef FEAT_KEYMAP
226# define ex_loadkeymap ex_ni
227#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100228static void ex_swapname(exarg_T *eap);
229static void ex_syncbind(exarg_T *eap);
230static void ex_read(exarg_T *eap);
231static void ex_pwd(exarg_T *eap);
232static void ex_equal(exarg_T *eap);
233static void ex_sleep(exarg_T *eap);
234static void do_exmap(exarg_T *eap, int isabbrev);
235static void ex_winsize(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100236static void ex_wincmd(exarg_T *eap);
Bram Moolenaar843ee412004-06-30 16:16:41 +0000237#if defined(FEAT_GUI) || defined(UNIX) || defined(VMS) || defined(MSWIN)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100238static void ex_winpos(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000239#else
240# define ex_winpos ex_ni
241#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100242static void ex_operators(exarg_T *eap);
243static void ex_put(exarg_T *eap);
244static void ex_copymove(exarg_T *eap);
245static void ex_submagic(exarg_T *eap);
246static void ex_join(exarg_T *eap);
247static void ex_at(exarg_T *eap);
248static void ex_bang(exarg_T *eap);
249static void ex_undo(exarg_T *eap);
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200250#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100251static void ex_wundo(exarg_T *eap);
252static void ex_rundo(exarg_T *eap);
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200253#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100254static void ex_redo(exarg_T *eap);
255static void ex_later(exarg_T *eap);
256static void ex_redir(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100257static void ex_redrawstatus(exarg_T *eap);
Bram Moolenaare12bab32019-01-08 22:02:56 +0100258static void ex_redrawtabline(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100259static void close_redir(void);
260static void ex_mkrc(exarg_T *eap);
261static void ex_mark(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100262static void ex_startinsert(exarg_T *eap);
263static void ex_stopinsert(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000264#ifdef FEAT_FIND_ID
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100265static void ex_checkpath(exarg_T *eap);
266static void ex_findpat(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000267#else
268# define ex_findpat ex_ni
269# define ex_checkpath ex_ni
270#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +0200271#if defined(FEAT_FIND_ID) && defined(FEAT_QUICKFIX)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100272static void ex_psearch(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000273#else
274# define ex_psearch ex_ni
275#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100276static void ex_tag(exarg_T *eap);
277static void ex_tag_cmd(exarg_T *eap, char_u *name);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000278#ifndef FEAT_EVAL
279# define ex_scriptnames ex_ni
280# define ex_finish ex_ni
281# define ex_echo ex_ni
282# define ex_echohl ex_ni
283# define ex_execute ex_ni
284# define ex_call ex_ni
285# define ex_if ex_ni
286# define ex_endif ex_ni
287# define ex_else ex_ni
288# define ex_while ex_ni
289# define ex_continue ex_ni
290# define ex_break ex_ni
291# define ex_endwhile ex_ni
292# define ex_throw ex_ni
293# define ex_try ex_ni
294# define ex_catch ex_ni
295# define ex_finally ex_ni
296# define ex_endtry ex_ni
297# define ex_endfunction ex_ni
298# define ex_let ex_ni
299# define ex_unlet ex_ni
Bram Moolenaar65c1b012005-01-31 19:02:28 +0000300# define ex_lockvar ex_ni
301# define ex_unlockvar ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000302# define ex_function ex_ni
303# define ex_delfunction ex_ni
304# define ex_return ex_ni
Bram Moolenaard812df62008-11-09 12:46:09 +0000305# define ex_oldfiles ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000306#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100307static char_u *arg_all(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000308#ifdef FEAT_SESSION
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100309static int makeopens(FILE *fd, char_u *dirnow);
310static int put_view(FILE *fd, win_T *wp, int add_edit, unsigned *flagp, int current_arg_idx);
311static void ex_loadview(exarg_T *eap);
312static char_u *get_view_file(int c);
Bram Moolenaareeefcc72007-05-01 21:21:21 +0000313static int did_lcd; /* whether ":lcd" was produced for a session */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000314#else
315# define ex_loadview ex_ni
316#endif
317#ifndef FEAT_EVAL
318# define ex_compiler ex_ni
319#endif
320#ifdef FEAT_VIMINFO
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100321static void ex_viminfo(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000322#else
323# define ex_viminfo ex_ni
324#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100325static void ex_behave(exarg_T *eap);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100326static void ex_filetype(exarg_T *eap);
327static void ex_setfiletype(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000328#ifndef FEAT_DIFF
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000329# define ex_diffoff ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000330# define ex_diffpatch ex_ni
331# define ex_diffgetput ex_ni
332# define ex_diffsplit ex_ni
333# define ex_diffthis ex_ni
334# define ex_diffupdate ex_ni
335#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100336static void ex_digraphs(exarg_T *eap);
337static void ex_set(exarg_T *eap);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100338#if !defined(FEAT_EVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000339# define ex_options ex_ni
340#endif
341#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100342static void ex_nohlsearch(exarg_T *eap);
343static void ex_match(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000344#else
345# define ex_nohlsearch ex_ni
346# define ex_match ex_ni
347#endif
348#ifdef FEAT_CRYPT
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100349static void ex_X(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000350#else
351# define ex_X ex_ni
352#endif
353#ifdef FEAT_FOLDING
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100354static void ex_fold(exarg_T *eap);
355static void ex_foldopen(exarg_T *eap);
356static void ex_folddo(exarg_T *eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000357#else
358# define ex_fold ex_ni
359# define ex_foldopen ex_ni
360# define ex_folddo ex_ni
361#endif
Bram Moolenaar13505972019-01-24 15:04:48 +0100362#if !(defined(HAVE_LOCALE_H) || defined(X_LOCALE))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000363# define ex_language ex_ni
364#endif
365#ifndef FEAT_SIGNS
366# define ex_sign ex_ni
367#endif
Bram Moolenaar009b2592004-10-24 19:18:58 +0000368#ifndef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200369# define ex_nbclose ex_ni
Bram Moolenaar009b2592004-10-24 19:18:58 +0000370# define ex_nbkey ex_ni
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200371# define ex_nbstart ex_ni
Bram Moolenaar009b2592004-10-24 19:18:58 +0000372#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000373
374#ifndef FEAT_EVAL
375# define ex_debug ex_ni
376# define ex_breakadd ex_ni
377# define ex_debuggreedy ex_ni
378# define ex_breakdel ex_ni
379# define ex_breaklist ex_ni
380#endif
381
382#ifndef FEAT_CMDHIST
383# define ex_history ex_ni
384#endif
385#ifndef FEAT_JUMPLIST
386# define ex_jumps ex_ni
Bram Moolenaar2d358992016-06-12 21:20:54 +0200387# define ex_clearjumps ex_ni
Bram Moolenaar071d4272004-06-13 20:20:40 +0000388# define ex_changes ex_ni
389#endif
390
Bram Moolenaar05159a02005-02-26 23:04:13 +0000391#ifndef FEAT_PROFILE
392# define ex_profile ex_ni
393#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200394#ifndef FEAT_TERMINAL
395# define ex_terminal ex_ni
396#endif
Bram Moolenaard4aa83a2019-05-09 18:59:31 +0200397#if !defined(FEAT_X11) || !defined(FEAT_XCLIPBOARD)
398# define ex_xrestore ex_ni
399#endif
Bram Moolenaar682725c2019-05-25 20:10:37 +0200400#if !defined(FEAT_TEXT_PROP)
401# define ex_popupclear ex_ni
402#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +0000403
Bram Moolenaar071d4272004-06-13 20:20:40 +0000404/*
405 * Declare cmdnames[].
406 */
407#define DO_DECLARE_EXCMD
408#include "ex_cmds.h"
Bram Moolenaar6de5e122017-04-20 21:55:44 +0200409#include "ex_cmdidxs.h"
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +0100410
Bram Moolenaar071d4272004-06-13 20:20:40 +0000411static char_u dollar_command[2] = {'$', 0};
412
413
414#ifdef FEAT_EVAL
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000415/* Struct for storing a line inside a while/for loop */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000416typedef struct
417{
418 char_u *line; /* command line */
419 linenr_T lnum; /* sourcing_lnum of the line */
420} wcmd_T;
421
422/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000423 * Structure used to store info for line position in a while or for loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000424 * This is required, because do_one_cmd() may invoke ex_function(), which
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000425 * reads more lines that may come from the while/for loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000426 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000427struct loop_cookie
Bram Moolenaar071d4272004-06-13 20:20:40 +0000428{
429 garray_T *lines_gap; /* growarray with line info */
430 int current_line; /* last read line from growarray */
431 int repeating; /* TRUE when looping a second time */
432 /* When "repeating" is FALSE use "getline" and "cookie" to get lines */
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100433 char_u *(*getline)(int, void *, int);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000434 void *cookie;
435};
436
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100437static char_u *get_loop_line(int c, void *cookie, int indent);
438static int store_loop_line(garray_T *gap, char_u *line);
439static void free_cmdlines(garray_T *gap);
Bram Moolenaared203462004-06-16 11:19:22 +0000440
441/* Struct to save a few things while debugging. Used in do_cmdline() only. */
442struct dbg_stuff
443{
444 int trylevel;
445 int force_abort;
446 except_T *caught_stack;
447 char_u *vv_exception;
448 char_u *vv_throwpoint;
449 int did_emsg;
450 int got_int;
451 int did_throw;
452 int need_rethrow;
453 int check_cstack;
454 except_T *current_exception;
455};
456
Bram Moolenaared203462004-06-16 11:19:22 +0000457 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100458save_dbg_stuff(struct dbg_stuff *dsp)
Bram Moolenaared203462004-06-16 11:19:22 +0000459{
460 dsp->trylevel = trylevel; trylevel = 0;
461 dsp->force_abort = force_abort; force_abort = FALSE;
462 dsp->caught_stack = caught_stack; caught_stack = NULL;
463 dsp->vv_exception = v_exception(NULL);
464 dsp->vv_throwpoint = v_throwpoint(NULL);
465
466 /* Necessary for debugging an inactive ":catch", ":finally", ":endtry" */
467 dsp->did_emsg = did_emsg; did_emsg = FALSE;
468 dsp->got_int = got_int; got_int = FALSE;
469 dsp->did_throw = did_throw; did_throw = FALSE;
470 dsp->need_rethrow = need_rethrow; need_rethrow = FALSE;
471 dsp->check_cstack = check_cstack; check_cstack = FALSE;
472 dsp->current_exception = current_exception; current_exception = NULL;
473}
474
475 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100476restore_dbg_stuff(struct dbg_stuff *dsp)
Bram Moolenaared203462004-06-16 11:19:22 +0000477{
478 suppress_errthrow = FALSE;
479 trylevel = dsp->trylevel;
480 force_abort = dsp->force_abort;
481 caught_stack = dsp->caught_stack;
482 (void)v_exception(dsp->vv_exception);
483 (void)v_throwpoint(dsp->vv_throwpoint);
484 did_emsg = dsp->did_emsg;
485 got_int = dsp->got_int;
486 did_throw = dsp->did_throw;
487 need_rethrow = dsp->need_rethrow;
488 check_cstack = dsp->check_cstack;
489 current_exception = dsp->current_exception;
490}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000491#endif
492
Bram Moolenaar071d4272004-06-13 20:20:40 +0000493/*
494 * do_exmode(): Repeatedly get commands for the "Ex" mode, until the ":vi"
495 * command is given.
496 */
497 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100498do_exmode(
499 int improved) /* TRUE for "improved Ex" mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000500{
501 int save_msg_scroll;
502 int prev_msg_row;
503 linenr_T prev_line;
Bram Moolenaar79518e22017-02-17 16:31:35 +0100504 varnumber_T changedtick;
Bram Moolenaardf177f62005-02-22 08:39:57 +0000505
506 if (improved)
507 exmode_active = EXMODE_VIM;
508 else
509 exmode_active = EXMODE_NORMAL;
510 State = NORMAL;
511
512 /* When using ":global /pat/ visual" and then "Q" we return to continue
513 * the :global command. */
514 if (global_busy)
515 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000516
517 save_msg_scroll = msg_scroll;
518 ++RedrawingDisabled; /* don't redisplay the window */
519 ++no_wait_return; /* don't wait for return */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000520#ifdef FEAT_GUI
521 /* Ignore scrollbar and mouse events in Ex mode */
522 ++hold_gui_events;
523#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000524
Bram Moolenaar32526b32019-01-19 17:43:09 +0100525 msg(_("Entering Ex mode. Type \"visual\" to go to Normal mode."));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000526 while (exmode_active)
527 {
Bram Moolenaar7c626922005-02-07 22:01:03 +0000528 /* Check for a ":normal" command and no more characters left. */
529 if (ex_normal_busy > 0 && typebuf.tb_len == 0)
530 {
531 exmode_active = FALSE;
532 break;
533 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000534 msg_scroll = TRUE;
535 need_wait_return = FALSE;
536 ex_pressedreturn = FALSE;
537 ex_no_reprint = FALSE;
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100538 changedtick = CHANGEDTICK(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000539 prev_msg_row = msg_row;
540 prev_line = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000541 if (improved)
542 {
543 cmdline_row = msg_row;
544 do_cmdline(NULL, getexline, NULL, 0);
545 }
546 else
547 do_cmdline(NULL, getexmodeline, NULL, DOCMD_NOWAIT);
548 lines_left = Rows - 1;
549
Bram Moolenaardf177f62005-02-22 08:39:57 +0000550 if ((prev_line != curwin->w_cursor.lnum
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100551 || changedtick != CHANGEDTICK(curbuf)) && !ex_no_reprint)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000552 {
Bram Moolenaardf177f62005-02-22 08:39:57 +0000553 if (curbuf->b_ml.ml_flags & ML_EMPTY)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100554 emsg(_(e_emptybuf));
Bram Moolenaardf177f62005-02-22 08:39:57 +0000555 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000556 {
Bram Moolenaardf177f62005-02-22 08:39:57 +0000557 if (ex_pressedreturn)
558 {
559 /* go up one line, to overwrite the ":<CR>" line, so the
Bram Moolenaar81870892007-11-11 18:17:28 +0000560 * output doesn't contain empty lines. */
Bram Moolenaardf177f62005-02-22 08:39:57 +0000561 msg_row = prev_msg_row;
562 if (prev_msg_row == Rows - 1)
563 msg_row--;
564 }
565 msg_col = 0;
566 print_line_no_prefix(curwin->w_cursor.lnum, FALSE, FALSE);
567 msg_clr_eos();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000568 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000569 }
Bram Moolenaardf177f62005-02-22 08:39:57 +0000570 else if (ex_pressedreturn && !ex_no_reprint) /* must be at EOF */
571 {
572 if (curbuf->b_ml.ml_flags & ML_EMPTY)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100573 emsg(_(e_emptybuf));
Bram Moolenaardf177f62005-02-22 08:39:57 +0000574 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100575 emsg(_("E501: At end-of-file"));
Bram Moolenaardf177f62005-02-22 08:39:57 +0000576 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000577 }
578
579#ifdef FEAT_GUI
580 --hold_gui_events;
581#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000582 --RedrawingDisabled;
583 --no_wait_return;
584 update_screen(CLEAR);
585 need_wait_return = FALSE;
586 msg_scroll = save_msg_scroll;
587}
588
589/*
590 * Execute a simple command line. Used for translated commands like "*".
591 */
592 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100593do_cmdline_cmd(char_u *cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000594{
595 return do_cmdline(cmd, NULL, NULL,
596 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
597}
598
599/*
600 * do_cmdline(): execute one Ex command line
601 *
602 * 1. Execute "cmdline" when it is not NULL.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100603 * If "cmdline" is NULL, or more lines are needed, fgetline() is used.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000604 * 2. Split up in parts separated with '|'.
605 *
606 * This function can be called recursively!
607 *
608 * flags:
609 * DOCMD_VERBOSE - The command will be included in the error message.
610 * DOCMD_NOWAIT - Don't call wait_return() and friends.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100611 * DOCMD_REPEAT - Repeat execution until fgetline() returns NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000612 * DOCMD_KEYTYPED - Don't reset KeyTyped.
613 * DOCMD_EXCRESET - Reset the exception environment (used for debugging).
614 * DOCMD_KEEPLINE - Store first typed line (for repeating with ".").
615 *
616 * return FAIL if cmdline could not be executed, OK otherwise
617 */
618 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100619do_cmdline(
620 char_u *cmdline,
621 char_u *(*fgetline)(int, void *, int),
622 void *cookie, /* argument for fgetline() */
623 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000624{
625 char_u *next_cmdline; /* next cmd to execute */
626 char_u *cmdline_copy = NULL; /* copy of cmd line */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100627 int used_getline = FALSE; /* used "fgetline" to obtain command */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000628 static int recursive = 0; /* recursive depth */
629 int msg_didout_before_start = 0;
630 int count = 0; /* line number count */
631 int did_inc = FALSE; /* incremented RedrawingDisabled */
632 int retval = OK;
633#ifdef FEAT_EVAL
634 struct condstack cstack; /* conditional stack */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000635 garray_T lines_ga; /* keep lines for ":while"/":for" */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000636 int current_line = 0; /* active line in lines_ga */
637 char_u *fname = NULL; /* function or script name */
638 linenr_T *breakpoint = NULL; /* ptr to breakpoint field in cookie */
639 int *dbg_tick = NULL; /* ptr to dbg_tick field in cookie */
Bram Moolenaared203462004-06-16 11:19:22 +0000640 struct dbg_stuff debug_saved; /* saved things for debug mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000641 int initial_trylevel;
642 struct msglist **saved_msg_list = NULL;
643 struct msglist *private_msg_list;
644
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100645 /* "fgetline" and "cookie" passed to do_one_cmd() */
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100646 char_u *(*cmd_getline)(int, void *, int);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000647 void *cmd_cookie;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000648 struct loop_cookie cmd_loop_cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000649 void *real_cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +0000650 int getline_is_func;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000651#else
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100652# define cmd_getline fgetline
Bram Moolenaar071d4272004-06-13 20:20:40 +0000653# define cmd_cookie cookie
654#endif
655 static int call_depth = 0; /* recursiveness */
656
657#ifdef FEAT_EVAL
658 /* For every pair of do_cmdline()/do_one_cmd() calls, use an extra memory
659 * location for storing error messages to be converted to an exception.
Bram Moolenaarcf3630f2005-01-08 16:04:29 +0000660 * This ensures that the do_errthrow() call in do_one_cmd() does not
661 * combine the messages stored by an earlier invocation of do_one_cmd()
662 * with the command name of the later one. This would happen when
663 * BufWritePost autocommands are executed after a write error. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000664 saved_msg_list = msg_list;
665 msg_list = &private_msg_list;
666 private_msg_list = NULL;
667#endif
668
669 /* It's possible to create an endless loop with ":execute", catch that
Bram Moolenaar777b30f2017-01-02 15:26:27 +0100670 * here. The value of 200 allows nested function calls, ":source", etc.
671 * Allow 200 or 'maxfuncdepth', whatever is larger. */
Bram Moolenaarb094ff42017-01-02 16:16:39 +0100672 if (call_depth >= 200
673#ifdef FEAT_EVAL
674 && call_depth >= p_mfd
675#endif
676 )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000677 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100678 emsg(_("E169: Command too recursive"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000679#ifdef FEAT_EVAL
680 /* When converting to an exception, we do not include the command name
681 * since this is not an error of the specific command. */
682 do_errthrow((struct condstack *)NULL, (char_u *)NULL);
683 msg_list = saved_msg_list;
684#endif
685 return FAIL;
686 }
687 ++call_depth;
688
689#ifdef FEAT_EVAL
690 cstack.cs_idx = -1;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000691 cstack.cs_looplevel = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000692 cstack.cs_trylevel = 0;
693 cstack.cs_emsg_silent_list = NULL;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000694 cstack.cs_lflags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000695 ga_init2(&lines_ga, (int)sizeof(wcmd_T), 10);
696
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100697 real_cookie = getline_cookie(fgetline, cookie);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000698
699 /* Inside a function use a higher nesting level. */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100700 getline_is_func = getline_equal(fgetline, cookie, get_func_line);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000701 if (getline_is_func && ex_nesting_level == func_level(real_cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000702 ++ex_nesting_level;
703
704 /* Get the function or script name and the address where the next breakpoint
705 * line and the debug tick for a function or script are stored. */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000706 if (getline_is_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000707 {
708 fname = func_name(real_cookie);
709 breakpoint = func_breakpoint(real_cookie);
710 dbg_tick = func_dbg_tick(real_cookie);
711 }
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100712 else if (getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000713 {
714 fname = sourcing_name;
715 breakpoint = source_breakpoint(real_cookie);
716 dbg_tick = source_dbg_tick(real_cookie);
717 }
718
719 /*
720 * Initialize "force_abort" and "suppress_errthrow" at the top level.
721 */
722 if (!recursive)
723 {
724 force_abort = FALSE;
725 suppress_errthrow = FALSE;
726 }
727
728 /*
729 * If requested, store and reset the global values controlling the
Bram Moolenaar89d40322006-08-29 15:30:07 +0000730 * exception handling (used when debugging). Otherwise clear it to avoid
731 * a bogus compiler warning when the optimizer uses inline functions...
Bram Moolenaar071d4272004-06-13 20:20:40 +0000732 */
Bram Moolenaarb4872942006-05-13 10:32:52 +0000733 if (flags & DOCMD_EXCRESET)
Bram Moolenaared203462004-06-16 11:19:22 +0000734 save_dbg_stuff(&debug_saved);
Bram Moolenaar89d40322006-08-29 15:30:07 +0000735 else
Bram Moolenaar69b67f72015-09-25 16:59:47 +0200736 vim_memset(&debug_saved, 0, sizeof(debug_saved));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000737
738 initial_trylevel = trylevel;
739
740 /*
741 * "did_throw" will be set to TRUE when an exception is being thrown.
742 */
743 did_throw = FALSE;
744#endif
745 /*
746 * "did_emsg" will be set to TRUE when emsg() is used, in which case we
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000747 * cancel the whole command line, and any if/endif or loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000748 * If force_abort is set, we cancel everything.
749 */
750 did_emsg = FALSE;
751
752 /*
753 * KeyTyped is only set when calling vgetc(). Reset it here when not
754 * calling vgetc() (sourced command lines).
755 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100756 if (!(flags & DOCMD_KEYTYPED)
757 && !getline_equal(fgetline, cookie, getexline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000758 KeyTyped = FALSE;
759
760 /*
761 * Continue executing command lines:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000762 * - when inside an ":if", ":while" or ":for"
Bram Moolenaar071d4272004-06-13 20:20:40 +0000763 * - for multiple commands on one line, separated with '|'
764 * - when repeating until there are no more lines (for ":source")
765 */
766 next_cmdline = cmdline;
767 do
768 {
Bram Moolenaar05159a02005-02-26 23:04:13 +0000769#ifdef FEAT_EVAL
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100770 getline_is_func = getline_equal(fgetline, cookie, get_func_line);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000771#endif
772
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000773 /* stop skipping cmds for an error msg after all endif/while/for */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000774 if (next_cmdline == NULL
775#ifdef FEAT_EVAL
776 && !force_abort
777 && cstack.cs_idx < 0
Bram Moolenaar05159a02005-02-26 23:04:13 +0000778 && !(getline_is_func && func_has_abort(real_cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000779#endif
780 )
781 did_emsg = FALSE;
782
783 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000784 * 1. If repeating a line in a loop, get a line from lines_ga.
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100785 * 2. If no line given: Get an allocated line with fgetline().
Bram Moolenaar071d4272004-06-13 20:20:40 +0000786 * 3. If a line is given: Make a copy, so we can mess with it.
787 */
788
789#ifdef FEAT_EVAL
790 /* 1. If repeating, get a previous line from lines_ga. */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000791 if (cstack.cs_looplevel > 0 && current_line < lines_ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000792 {
793 /* Each '|' separated command is stored separately in lines_ga, to
794 * be able to jump to it. Don't use next_cmdline now. */
Bram Moolenaard23a8232018-02-10 18:45:26 +0100795 VIM_CLEAR(cmdline_copy);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000796
797 /* Check if a function has returned or, unless it has an unclosed
798 * try conditional, aborted. */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000799 if (getline_is_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000800 {
Bram Moolenaar05159a02005-02-26 23:04:13 +0000801# ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000802 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +0000803 func_line_end(real_cookie);
804# endif
805 if (func_has_ended(real_cookie))
806 {
807 retval = FAIL;
808 break;
809 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000810 }
Bram Moolenaar05159a02005-02-26 23:04:13 +0000811#ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000812 else if (do_profiling == PROF_YES
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100813 && getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar05159a02005-02-26 23:04:13 +0000814 script_line_end();
815#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000816
817 /* Check if a sourced file hit a ":finish" command. */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100818 if (source_finished(fgetline, cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000819 {
820 retval = FAIL;
821 break;
822 }
823
824 /* If breakpoints have been added/deleted need to check for it. */
825 if (breakpoint != NULL && dbg_tick != NULL
826 && *dbg_tick != debug_tick)
827 {
828 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100829 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar071d4272004-06-13 20:20:40 +0000830 fname, sourcing_lnum);
831 *dbg_tick = debug_tick;
832 }
833
834 next_cmdline = ((wcmd_T *)(lines_ga.ga_data))[current_line].line;
835 sourcing_lnum = ((wcmd_T *)(lines_ga.ga_data))[current_line].lnum;
836
837 /* Did we encounter a breakpoint? */
838 if (breakpoint != NULL && *breakpoint != 0
839 && *breakpoint <= sourcing_lnum)
840 {
841 dbg_breakpoint(fname, sourcing_lnum);
842 /* Find next breakpoint. */
843 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100844 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar071d4272004-06-13 20:20:40 +0000845 fname, sourcing_lnum);
846 *dbg_tick = debug_tick;
847 }
Bram Moolenaar05159a02005-02-26 23:04:13 +0000848# ifdef FEAT_PROFILE
Bram Moolenaar371d5402006-03-20 21:47:49 +0000849 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +0000850 {
851 if (getline_is_func)
852 func_line_start(real_cookie);
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100853 else if (getline_equal(fgetline, cookie, getsourceline))
Bram Moolenaar05159a02005-02-26 23:04:13 +0000854 script_line_start();
855 }
856# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000857 }
858
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000859 if (cstack.cs_looplevel > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000860 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000861 /* Inside a while/for loop we need to store the lines and use them
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100862 * again. Pass a different "fgetline" function to do_one_cmd()
Bram Moolenaar071d4272004-06-13 20:20:40 +0000863 * below, so that it stores lines in or reads them from
864 * "lines_ga". Makes it possible to define a function inside a
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000865 * while/for loop. */
866 cmd_getline = get_loop_line;
867 cmd_cookie = (void *)&cmd_loop_cookie;
868 cmd_loop_cookie.lines_gap = &lines_ga;
869 cmd_loop_cookie.current_line = current_line;
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100870 cmd_loop_cookie.getline = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000871 cmd_loop_cookie.cookie = cookie;
872 cmd_loop_cookie.repeating = (current_line < lines_ga.ga_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000873 }
874 else
875 {
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100876 cmd_getline = fgetline;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000877 cmd_cookie = cookie;
878 }
879#endif
880
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100881 /* 2. If no line given, get an allocated line with fgetline(). */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000882 if (next_cmdline == NULL)
883 {
884 /*
885 * Need to set msg_didout for the first line after an ":if",
886 * otherwise the ":if" will be overwritten.
887 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100888 if (count == 1 && getline_equal(fgetline, cookie, getexline))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000889 msg_didout = TRUE;
Bram Moolenaarbf55e142010-11-16 11:32:01 +0100890 if (fgetline == NULL || (next_cmdline = fgetline(':', cookie,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000891#ifdef FEAT_EVAL
892 cstack.cs_idx < 0 ? 0 : (cstack.cs_idx + 1) * 2
893#else
894 0
895#endif
896 )) == NULL)
897 {
898 /* Don't call wait_return for aborted command line. The NULL
899 * returned for the end of a sourced file or executed function
900 * doesn't do this. */
901 if (KeyTyped && !(flags & DOCMD_REPEAT))
902 need_wait_return = FALSE;
903 retval = FAIL;
904 break;
905 }
906 used_getline = TRUE;
907
908 /*
909 * Keep the first typed line. Clear it when more lines are typed.
910 */
911 if (flags & DOCMD_KEEPLINE)
912 {
913 vim_free(repeat_cmdline);
914 if (count == 0)
915 repeat_cmdline = vim_strsave(next_cmdline);
916 else
917 repeat_cmdline = NULL;
918 }
919 }
920
921 /* 3. Make a copy of the command so we can mess with it. */
922 else if (cmdline_copy == NULL)
923 {
924 next_cmdline = vim_strsave(next_cmdline);
925 if (next_cmdline == NULL)
926 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100927 emsg(_(e_outofmem));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000928 retval = FAIL;
929 break;
930 }
931 }
932 cmdline_copy = next_cmdline;
933
934#ifdef FEAT_EVAL
935 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000936 * Save the current line when inside a ":while" or ":for", and when
937 * the command looks like a ":while" or ":for", because we may need it
938 * later. When there is a '|' and another command, it is stored
939 * separately, because we need to be able to jump back to it from an
940 * :endwhile/:endfor.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000941 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000942 if (current_line == lines_ga.ga_len
943 && (cstack.cs_looplevel || has_loop_cmd(next_cmdline)))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000944 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +0000945 if (store_loop_line(&lines_ga, next_cmdline) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000946 {
947 retval = FAIL;
948 break;
949 }
950 }
951 did_endif = FALSE;
952#endif
953
954 if (count++ == 0)
955 {
956 /*
957 * All output from the commands is put below each other, without
958 * waiting for a return. Don't do this when executing commands
959 * from a script or when being called recursive (e.g. for ":e
960 * +command file").
961 */
962 if (!(flags & DOCMD_NOWAIT) && !recursive)
963 {
964 msg_didout_before_start = msg_didout;
965 msg_didany = FALSE; /* no output yet */
966 msg_start();
967 msg_scroll = TRUE; /* put messages below each other */
Bram Moolenaar84a05ac2013-05-06 04:24:17 +0200968 ++no_wait_return; /* don't wait for return until finished */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000969 ++RedrawingDisabled;
970 did_inc = TRUE;
971 }
972 }
973
974 if (p_verbose >= 15 && sourcing_name != NULL)
975 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000976 ++no_wait_return;
Bram Moolenaar8b044b32005-05-31 22:05:58 +0000977 verbose_enter_scroll();
978
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100979 smsg(_("line %ld: %s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +0000980 (long)sourcing_lnum, cmdline_copy);
Bram Moolenaar8b044b32005-05-31 22:05:58 +0000981 if (msg_silent == 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +0100982 msg_puts("\n"); /* don't overwrite this */
Bram Moolenaar8b044b32005-05-31 22:05:58 +0000983
984 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000985 --no_wait_return;
986 }
987
988 /*
989 * 2. Execute one '|' separated command.
990 * do_one_cmd() will return NULL if there is no trailing '|'.
991 * "cmdline_copy" can change, e.g. for '%' and '#' expansion.
992 */
993 ++recursive;
994 next_cmdline = do_one_cmd(&cmdline_copy, flags & DOCMD_VERBOSE,
995#ifdef FEAT_EVAL
996 &cstack,
997#endif
998 cmd_getline, cmd_cookie);
999 --recursive;
1000
1001#ifdef FEAT_EVAL
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001002 if (cmd_cookie == (void *)&cmd_loop_cookie)
1003 /* Use "current_line" from "cmd_loop_cookie", it may have been
Bram Moolenaar071d4272004-06-13 20:20:40 +00001004 * incremented when defining a function. */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001005 current_line = cmd_loop_cookie.current_line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001006#endif
1007
1008 if (next_cmdline == NULL)
1009 {
Bram Moolenaard23a8232018-02-10 18:45:26 +01001010 VIM_CLEAR(cmdline_copy);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001011#ifdef FEAT_CMDHIST
1012 /*
1013 * If the command was typed, remember it for the ':' register.
1014 * Do this AFTER executing the command to make :@: work.
1015 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001016 if (getline_equal(fgetline, cookie, getexline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001017 && new_last_cmdline != NULL)
1018 {
1019 vim_free(last_cmdline);
1020 last_cmdline = new_last_cmdline;
1021 new_last_cmdline = NULL;
1022 }
1023#endif
1024 }
1025 else
1026 {
1027 /* need to copy the command after the '|' to cmdline_copy, for the
1028 * next do_one_cmd() */
Bram Moolenaara7241f52008-06-24 20:39:31 +00001029 STRMOVE(cmdline_copy, next_cmdline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001030 next_cmdline = cmdline_copy;
1031 }
1032
1033
1034#ifdef FEAT_EVAL
1035 /* reset did_emsg for a function that is not aborted by an error */
1036 if (did_emsg && !force_abort
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001037 && getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001038 && !func_has_abort(real_cookie))
1039 did_emsg = FALSE;
1040
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001041 if (cstack.cs_looplevel > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001042 {
1043 ++current_line;
1044
1045 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001046 * An ":endwhile", ":endfor" and ":continue" is handled here.
1047 * If we were executing commands, jump back to the ":while" or
1048 * ":for".
1049 * If we were not executing commands, decrement cs_looplevel.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001050 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001051 if (cstack.cs_lflags & (CSL_HAD_CONT | CSL_HAD_ENDLOOP))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001052 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001053 cstack.cs_lflags &= ~(CSL_HAD_CONT | CSL_HAD_ENDLOOP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001054
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001055 /* Jump back to the matching ":while" or ":for". Be careful
1056 * not to use a cs_line[] from an entry that isn't a ":while"
1057 * or ":for": It would make "current_line" invalid and can
1058 * cause a crash. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001059 if (!did_emsg && !got_int && !did_throw
1060 && cstack.cs_idx >= 0
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001061 && (cstack.cs_flags[cstack.cs_idx]
1062 & (CSF_WHILE | CSF_FOR))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001063 && cstack.cs_line[cstack.cs_idx] >= 0
1064 && (cstack.cs_flags[cstack.cs_idx] & CSF_ACTIVE))
1065 {
1066 current_line = cstack.cs_line[cstack.cs_idx];
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001067 /* remember we jumped there */
1068 cstack.cs_lflags |= CSL_HAD_LOOP;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001069 line_breakcheck(); /* check if CTRL-C typed */
1070
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001071 /* Check for the next breakpoint at or after the ":while"
1072 * or ":for". */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001073 if (breakpoint != NULL)
1074 {
1075 *breakpoint = dbg_find_breakpoint(
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001076 getline_equal(fgetline, cookie, getsourceline),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001077 fname,
1078 ((wcmd_T *)lines_ga.ga_data)[current_line].lnum-1);
1079 *dbg_tick = debug_tick;
1080 }
1081 }
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001082 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001083 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001084 /* can only get here with ":endwhile" or ":endfor" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001085 if (cstack.cs_idx >= 0)
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001086 rewind_conditionals(&cstack, cstack.cs_idx - 1,
1087 CSF_WHILE | CSF_FOR, &cstack.cs_looplevel);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001088 }
1089 }
1090
1091 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001092 * For a ":while" or ":for" we need to remember the line number.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001093 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001094 else if (cstack.cs_lflags & CSL_HAD_LOOP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001095 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001096 cstack.cs_lflags &= ~CSL_HAD_LOOP;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001097 cstack.cs_line[cstack.cs_idx] = current_line - 1;
1098 }
1099 }
1100
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01001101 /* Check for the next breakpoint after a watchexpression */
1102 if (breakpoint != NULL && has_watchexpr())
1103 {
1104 *breakpoint = dbg_find_breakpoint(FALSE, fname, sourcing_lnum);
1105 *dbg_tick = debug_tick;
1106 }
1107
Bram Moolenaar071d4272004-06-13 20:20:40 +00001108 /*
1109 * When not inside any ":while" loop, clear remembered lines.
1110 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001111 if (cstack.cs_looplevel == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001112 {
1113 if (lines_ga.ga_len > 0)
1114 {
1115 sourcing_lnum =
1116 ((wcmd_T *)lines_ga.ga_data)[lines_ga.ga_len - 1].lnum;
1117 free_cmdlines(&lines_ga);
1118 }
1119 current_line = 0;
1120 }
1121
1122 /*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001123 * A ":finally" makes did_emsg, got_int, and did_throw pending for
1124 * being restored at the ":endtry". Reset them here and set the
1125 * ACTIVE and FINALLY flags, so that the finally clause gets executed.
1126 * This includes the case where a missing ":endif", ":endwhile" or
1127 * ":endfor" was detected by the ":finally" itself.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001128 */
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001129 if (cstack.cs_lflags & CSL_HAD_FINA)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001130 {
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001131 cstack.cs_lflags &= ~CSL_HAD_FINA;
1132 report_make_pending(cstack.cs_pending[cstack.cs_idx]
1133 & (CSTP_ERROR | CSTP_INTERRUPT | CSTP_THROW),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001134 did_throw ? (void *)current_exception : NULL);
1135 did_emsg = got_int = did_throw = FALSE;
1136 cstack.cs_flags[cstack.cs_idx] |= CSF_ACTIVE | CSF_FINALLY;
1137 }
1138
1139 /* Update global "trylevel" for recursive calls to do_cmdline() from
1140 * within this loop. */
1141 trylevel = initial_trylevel + cstack.cs_trylevel;
1142
1143 /*
Bram Moolenaarc1762cc2007-05-10 16:56:30 +00001144 * If the outermost try conditional (across function calls and sourced
Bram Moolenaar071d4272004-06-13 20:20:40 +00001145 * files) is aborted because of an error, an interrupt, or an uncaught
1146 * exception, cancel everything. If it is left normally, reset
1147 * force_abort to get the non-EH compatible abortion behavior for
1148 * the rest of the script.
1149 */
1150 if (trylevel == 0 && !did_emsg && !got_int && !did_throw)
1151 force_abort = FALSE;
1152
1153 /* Convert an interrupt to an exception if appropriate. */
1154 (void)do_intthrow(&cstack);
1155#endif /* FEAT_EVAL */
1156
1157 }
1158 /*
1159 * Continue executing command lines when:
1160 * - no CTRL-C typed, no aborting error, no exception thrown or try
1161 * conditionals need to be checked for executing finally clauses or
1162 * catching an interrupt exception
1163 * - didn't get an error message or lines are not typed
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001164 * - there is a command after '|', inside a :if, :while, :for or :try, or
Bram Moolenaar071d4272004-06-13 20:20:40 +00001165 * looping for ":source" command or function call.
1166 */
1167 while (!((got_int
1168#ifdef FEAT_EVAL
1169 || (did_emsg && force_abort) || did_throw
1170#endif
1171 )
1172#ifdef FEAT_EVAL
1173 && cstack.cs_trylevel == 0
1174#endif
1175 )
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001176 && !(did_emsg
1177#ifdef FEAT_EVAL
1178 /* Keep going when inside try/catch, so that the error can be
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02001179 * deal with, except when it is a syntax error, it may cause
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001180 * the :endtry to be missed. */
1181 && (cstack.cs_trylevel == 0 || did_emsg_syntax)
1182#endif
1183 && used_getline
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001184 && (getline_equal(fgetline, cookie, getexmodeline)
1185 || getline_equal(fgetline, cookie, getexline)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001186 && (next_cmdline != NULL
1187#ifdef FEAT_EVAL
1188 || cstack.cs_idx >= 0
1189#endif
1190 || (flags & DOCMD_REPEAT)));
1191
1192 vim_free(cmdline_copy);
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001193 did_emsg_syntax = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001194#ifdef FEAT_EVAL
1195 free_cmdlines(&lines_ga);
1196 ga_clear(&lines_ga);
1197
1198 if (cstack.cs_idx >= 0)
1199 {
1200 /*
1201 * If a sourced file or executed function ran to its end, report the
1202 * unclosed conditional.
1203 */
1204 if (!got_int && !did_throw
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001205 && ((getline_equal(fgetline, cookie, getsourceline)
1206 && !source_finished(fgetline, cookie))
1207 || (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001208 && !func_has_ended(real_cookie))))
1209 {
1210 if (cstack.cs_flags[cstack.cs_idx] & CSF_TRY)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001211 emsg(_(e_endtry));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001212 else if (cstack.cs_flags[cstack.cs_idx] & CSF_WHILE)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001213 emsg(_(e_endwhile));
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001214 else if (cstack.cs_flags[cstack.cs_idx] & CSF_FOR)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001215 emsg(_(e_endfor));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001216 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001217 emsg(_(e_endif));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001218 }
1219
1220 /*
1221 * Reset "trylevel" in case of a ":finish" or ":return" or a missing
1222 * ":endtry" in a sourced file or executed function. If the try
1223 * conditional is in its finally clause, ignore anything pending.
1224 * If it is in a catch clause, finish the caught exception.
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001225 * Also cleanup any "cs_forinfo" structures.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001226 */
1227 do
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001228 {
1229 int idx = cleanup_conditionals(&cstack, 0, TRUE);
1230
Bram Moolenaar89e5d682005-01-17 22:06:23 +00001231 if (idx >= 0)
1232 --idx; /* remove try block not in its finally clause */
Bram Moolenaarbb761a72005-01-06 23:19:09 +00001233 rewind_conditionals(&cstack, idx, CSF_WHILE | CSF_FOR,
1234 &cstack.cs_looplevel);
1235 }
1236 while (cstack.cs_idx >= 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001237 trylevel = initial_trylevel;
1238 }
1239
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001240 /* If a missing ":endtry", ":endwhile", ":endfor", or ":endif" or a memory
1241 * lack was reported above and the error message is to be converted to an
Bram Moolenaar071d4272004-06-13 20:20:40 +00001242 * exception, do this now after rewinding the cstack. */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001243 do_errthrow(&cstack, getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001244 ? (char_u *)"endfunction" : (char_u *)NULL);
1245
1246 if (trylevel == 0)
1247 {
1248 /*
1249 * When an exception is being thrown out of the outermost try
1250 * conditional, discard the uncaught exception, disable the conversion
1251 * of interrupts or errors to exceptions, and ensure that no more
1252 * commands are executed.
1253 */
1254 if (did_throw)
1255 {
1256 void *p = NULL;
1257 char_u *saved_sourcing_name;
1258 int saved_sourcing_lnum;
1259 struct msglist *messages = NULL, *next;
1260
1261 /*
1262 * If the uncaught exception is a user exception, report it as an
1263 * error. If it is an error exception, display the saved error
1264 * message now. For an interrupt exception, do nothing; the
1265 * interrupt message is given elsewhere.
1266 */
1267 switch (current_exception->type)
1268 {
1269 case ET_USER:
Bram Moolenaar9c13b352005-05-19 20:53:52 +00001270 vim_snprintf((char *)IObuff, IOSIZE,
1271 _("E605: Exception not caught: %s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001272 current_exception->value);
1273 p = vim_strsave(IObuff);
1274 break;
1275 case ET_ERROR:
1276 messages = current_exception->messages;
1277 current_exception->messages = NULL;
1278 break;
1279 case ET_INTERRUPT:
1280 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001281 }
1282
1283 saved_sourcing_name = sourcing_name;
1284 saved_sourcing_lnum = sourcing_lnum;
1285 sourcing_name = current_exception->throw_name;
1286 sourcing_lnum = current_exception->throw_lnum;
1287 current_exception->throw_name = NULL;
1288
1289 discard_current_exception(); /* uses IObuff if 'verbose' */
1290 suppress_errthrow = TRUE;
1291 force_abort = TRUE;
1292
1293 if (messages != NULL)
1294 {
1295 do
1296 {
1297 next = messages->next;
1298 emsg(messages->msg);
1299 vim_free(messages->msg);
1300 vim_free(messages);
1301 messages = next;
1302 }
1303 while (messages != NULL);
1304 }
1305 else if (p != NULL)
1306 {
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01001307 emsg(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001308 vim_free(p);
1309 }
1310 vim_free(sourcing_name);
1311 sourcing_name = saved_sourcing_name;
1312 sourcing_lnum = saved_sourcing_lnum;
1313 }
1314
1315 /*
1316 * On an interrupt or an aborting error not converted to an exception,
1317 * disable the conversion of errors to exceptions. (Interrupts are not
1318 * converted any more, here.) This enables also the interrupt message
1319 * when force_abort is set and did_emsg unset in case of an interrupt
1320 * from a finally clause after an error.
1321 */
1322 else if (got_int || (did_emsg && force_abort))
1323 suppress_errthrow = TRUE;
1324 }
1325
1326 /*
1327 * The current cstack will be freed when do_cmdline() returns. An uncaught
1328 * exception will have to be rethrown in the previous cstack. If a function
1329 * has just returned or a script file was just finished and the previous
1330 * cstack belongs to the same function or, respectively, script file, it
1331 * will have to be checked for finally clauses to be executed due to the
1332 * ":return" or ":finish". This is done in do_one_cmd().
1333 */
1334 if (did_throw)
1335 need_rethrow = TRUE;
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001336 if ((getline_equal(fgetline, cookie, getsourceline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001337 && ex_nesting_level > source_level(real_cookie))
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001338 || (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001339 && ex_nesting_level > func_level(real_cookie) + 1))
1340 {
1341 if (!did_throw)
1342 check_cstack = TRUE;
1343 }
1344 else
1345 {
1346 /* When leaving a function, reduce nesting level. */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001347 if (getline_equal(fgetline, cookie, get_func_line))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001348 --ex_nesting_level;
1349 /*
1350 * Go to debug mode when returning from a function in which we are
1351 * single-stepping.
1352 */
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001353 if ((getline_equal(fgetline, cookie, getsourceline)
1354 || getline_equal(fgetline, cookie, get_func_line))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001355 && ex_nesting_level + 1 <= debug_break_level)
Bram Moolenaarbf55e142010-11-16 11:32:01 +01001356 do_debug(getline_equal(fgetline, cookie, getsourceline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001357 ? (char_u *)_("End of sourced file")
1358 : (char_u *)_("End of function"));
1359 }
1360
1361 /*
1362 * Restore the exception environment (done after returning from the
1363 * debugger).
1364 */
1365 if (flags & DOCMD_EXCRESET)
Bram Moolenaared203462004-06-16 11:19:22 +00001366 restore_dbg_stuff(&debug_saved);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001367
1368 msg_list = saved_msg_list;
1369#endif /* FEAT_EVAL */
1370
1371 /*
1372 * If there was too much output to fit on the command line, ask the user to
1373 * hit return before redrawing the screen. With the ":global" command we do
1374 * this only once after the command is finished.
1375 */
1376 if (did_inc)
1377 {
1378 --RedrawingDisabled;
1379 --no_wait_return;
1380 msg_scroll = FALSE;
1381
1382 /*
1383 * When just finished an ":if"-":else" which was typed, no need to
1384 * wait for hit-return. Also for an error situation.
1385 */
1386 if (retval == FAIL
1387#ifdef FEAT_EVAL
1388 || (did_endif && KeyTyped && !did_emsg)
1389#endif
1390 )
1391 {
1392 need_wait_return = FALSE;
1393 msg_didany = FALSE; /* don't wait when restarting edit */
1394 }
1395 else if (need_wait_return)
1396 {
1397 /*
1398 * The msg_start() above clears msg_didout. The wait_return we do
1399 * here should not overwrite the command that may be shown before
1400 * doing that.
1401 */
1402 msg_didout |= msg_didout_before_start;
1403 wait_return(FALSE);
1404 }
1405 }
1406
Bram Moolenaar2a942252012-11-28 23:03:07 +01001407#ifdef FEAT_EVAL
1408 did_endif = FALSE; /* in case do_cmdline used recursively */
1409#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001410 /*
1411 * Reset if_level, in case a sourced script file contains more ":if" than
1412 * ":endif" (could be ":if x | foo | endif").
1413 */
1414 if_level = 0;
Bram Moolenaar2e18a122012-11-28 19:10:54 +01001415#endif
Bram Moolenaard4ad0d42012-11-28 17:34:48 +01001416
Bram Moolenaar071d4272004-06-13 20:20:40 +00001417 --call_depth;
1418 return retval;
1419}
1420
1421#ifdef FEAT_EVAL
1422/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001423 * Obtain a line when inside a ":while" or ":for" loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001424 */
1425 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001426get_loop_line(int c, void *cookie, int indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001427{
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001428 struct loop_cookie *cp = (struct loop_cookie *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001429 wcmd_T *wp;
1430 char_u *line;
1431
1432 if (cp->current_line + 1 >= cp->lines_gap->ga_len)
1433 {
1434 if (cp->repeating)
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001435 return NULL; /* trying to read past ":endwhile"/":endfor" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001436
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001437 /* First time inside the ":while"/":for": get line normally. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001438 if (cp->getline == NULL)
1439 line = getcmdline(c, 0L, indent);
1440 else
1441 line = cp->getline(c, cp->cookie, indent);
Bram Moolenaard68071d2006-05-02 22:08:30 +00001442 if (line != NULL && store_loop_line(cp->lines_gap, line) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001443 ++cp->current_line;
1444
1445 return line;
1446 }
1447
1448 KeyTyped = FALSE;
1449 ++cp->current_line;
1450 wp = (wcmd_T *)(cp->lines_gap->ga_data) + cp->current_line;
1451 sourcing_lnum = wp->lnum;
1452 return vim_strsave(wp->line);
1453}
1454
1455/*
1456 * Store a line in "gap" so that a ":while" loop can execute it again.
1457 */
1458 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001459store_loop_line(garray_T *gap, char_u *line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001460{
1461 if (ga_grow(gap, 1) == FAIL)
1462 return FAIL;
1463 ((wcmd_T *)(gap->ga_data))[gap->ga_len].line = vim_strsave(line);
1464 ((wcmd_T *)(gap->ga_data))[gap->ga_len].lnum = sourcing_lnum;
1465 ++gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001466 return OK;
1467}
1468
1469/*
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001470 * Free the lines stored for a ":while" or ":for" loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001471 */
1472 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001473free_cmdlines(garray_T *gap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001474{
1475 while (gap->ga_len > 0)
1476 {
1477 vim_free(((wcmd_T *)(gap->ga_data))[gap->ga_len - 1].line);
1478 --gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001479 }
1480}
1481#endif
1482
1483/*
Bram Moolenaar89d40322006-08-29 15:30:07 +00001484 * If "fgetline" is get_loop_line(), return TRUE if the getline it uses equals
1485 * "func". * Otherwise return TRUE when "fgetline" equals "func".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001486 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001487 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001488getline_equal(
1489 char_u *(*fgetline)(int, void *, int),
1490 void *cookie UNUSED, /* argument for fgetline() */
1491 char_u *(*func)(int, void *, int))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001492{
1493#ifdef FEAT_EVAL
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01001494 char_u *(*gp)(int, void *, int);
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001495 struct loop_cookie *cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001496
Bram Moolenaar89d40322006-08-29 15:30:07 +00001497 /* When "fgetline" is "get_loop_line()" use the "cookie" to find the
Bram Moolenaarc1762cc2007-05-10 16:56:30 +00001498 * function that's originally used to obtain the lines. This may be
1499 * nested several levels. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00001500 gp = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001501 cp = (struct loop_cookie *)cookie;
1502 while (gp == get_loop_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001503 {
1504 gp = cp->getline;
1505 cp = cp->cookie;
1506 }
1507 return gp == func;
1508#else
Bram Moolenaar89d40322006-08-29 15:30:07 +00001509 return fgetline == func;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001510#endif
1511}
1512
Bram Moolenaar071d4272004-06-13 20:20:40 +00001513/*
Bram Moolenaar89d40322006-08-29 15:30:07 +00001514 * If "fgetline" is get_loop_line(), return the cookie used by the original
Bram Moolenaar071d4272004-06-13 20:20:40 +00001515 * getline function. Otherwise return "cookie".
1516 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001517 void *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001518getline_cookie(
1519 char_u *(*fgetline)(int, void *, int) UNUSED,
1520 void *cookie) /* argument for fgetline() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001521{
Bram Moolenaar13505972019-01-24 15:04:48 +01001522#ifdef FEAT_EVAL
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01001523 char_u *(*gp)(int, void *, int);
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001524 struct loop_cookie *cp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001525
Bram Moolenaar89d40322006-08-29 15:30:07 +00001526 /* When "fgetline" is "get_loop_line()" use the "cookie" to find the
Bram Moolenaarc1762cc2007-05-10 16:56:30 +00001527 * cookie that's originally used to obtain the lines. This may be nested
Bram Moolenaar071d4272004-06-13 20:20:40 +00001528 * several levels. */
Bram Moolenaar89d40322006-08-29 15:30:07 +00001529 gp = fgetline;
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00001530 cp = (struct loop_cookie *)cookie;
1531 while (gp == get_loop_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001532 {
1533 gp = cp->getline;
1534 cp = cp->cookie;
1535 }
1536 return cp;
Bram Moolenaar13505972019-01-24 15:04:48 +01001537#else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001538 return cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001539#endif
Bram Moolenaar13505972019-01-24 15:04:48 +01001540}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001541
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001542
1543/*
1544 * Helper function to apply an offset for buffer commands, i.e. ":bdelete",
1545 * ":bwipeout", etc.
1546 * Returns the buffer number.
1547 */
1548 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001549compute_buffer_local_count(int addr_type, int lnum, int offset)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001550{
1551 buf_T *buf;
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001552 buf_T *nextbuf;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001553 int count = offset;
1554
1555 buf = firstbuf;
1556 while (buf->b_next != NULL && buf->b_fnum < lnum)
1557 buf = buf->b_next;
1558 while (count != 0)
1559 {
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001560 count += (offset < 0) ? 1 : -1;
1561 nextbuf = (offset < 0) ? buf->b_prev : buf->b_next;
1562 if (nextbuf == NULL)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001563 break;
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001564 buf = nextbuf;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001565 if (addr_type == ADDR_LOADED_BUFFERS)
1566 /* skip over unloaded buffers */
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001567 while (buf->b_ml.ml_mfp == NULL)
1568 {
1569 nextbuf = (offset < 0) ? buf->b_prev : buf->b_next;
1570 if (nextbuf == NULL)
1571 break;
1572 buf = nextbuf;
1573 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001574 }
Bram Moolenaar4d84d932014-11-30 14:50:16 +01001575 /* we might have gone too far, last buffer is not loadedd */
1576 if (addr_type == ADDR_LOADED_BUFFERS)
1577 while (buf->b_ml.ml_mfp == NULL)
1578 {
1579 nextbuf = (offset >= 0) ? buf->b_prev : buf->b_next;
1580 if (nextbuf == NULL)
1581 break;
1582 buf = nextbuf;
1583 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001584 return buf->b_fnum;
1585}
1586
Bram Moolenaarb7316892019-05-01 18:08:42 +02001587/*
1588 * Return the window number of "win".
1589 * When "win" is NULL return the number of windows.
1590 */
Bram Moolenaarf240e182014-11-27 18:33:02 +01001591 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001592current_win_nr(win_T *win)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001593{
1594 win_T *wp;
1595 int nr = 0;
1596
Bram Moolenaar29323592016-07-24 22:04:11 +02001597 FOR_ALL_WINDOWS(wp)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001598 {
1599 ++nr;
1600 if (wp == win)
1601 break;
1602 }
1603 return nr;
1604}
1605
1606 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001607current_tab_nr(tabpage_T *tab)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001608{
1609 tabpage_T *tp;
1610 int nr = 0;
1611
Bram Moolenaar29323592016-07-24 22:04:11 +02001612 FOR_ALL_TABPAGES(tp)
Bram Moolenaarf240e182014-11-27 18:33:02 +01001613 {
1614 ++nr;
1615 if (tp == tab)
1616 break;
1617 }
1618 return nr;
1619}
1620
1621# define CURRENT_WIN_NR current_win_nr(curwin)
1622# define LAST_WIN_NR current_win_nr(NULL)
1623# define CURRENT_TAB_NR current_tab_nr(curtab)
1624# define LAST_TAB_NR current_tab_nr(NULL)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001625
Bram Moolenaar071d4272004-06-13 20:20:40 +00001626/*
1627 * Execute one Ex command.
1628 *
1629 * If 'sourcing' is TRUE, the command will be included in the error message.
1630 *
1631 * 1. skip comment lines and leading space
1632 * 2. handle command modifiers
Bram Moolenaar1c40a662014-11-27 16:38:11 +01001633 * 3. find the command
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001634 * 4. parse range
Bram Moolenaar1c40a662014-11-27 16:38:11 +01001635 * 5. Parse the command.
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001636 * 6. parse arguments
1637 * 7. switch on command name
Bram Moolenaar071d4272004-06-13 20:20:40 +00001638 *
Bram Moolenaar89d40322006-08-29 15:30:07 +00001639 * Note: "fgetline" can be NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001640 *
1641 * This function may be called recursively!
1642 */
1643#if (_MSC_VER == 1200)
1644/*
Bram Moolenaared203462004-06-16 11:19:22 +00001645 * Avoid optimisation bug in VC++ version 6.0
Bram Moolenaar071d4272004-06-13 20:20:40 +00001646 */
Bram Moolenaar281bdce2005-01-25 21:53:18 +00001647 #pragma optimize( "g", off )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001648#endif
1649 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001650do_one_cmd(
1651 char_u **cmdlinep,
1652 int sourcing,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001653#ifdef FEAT_EVAL
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001654 struct condstack *cstack,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001655#endif
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001656 char_u *(*fgetline)(int, void *, int),
1657 void *cookie) /* argument for fgetline() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001658{
1659 char_u *p;
1660 linenr_T lnum;
1661 long n;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001662 char *errormsg = NULL; /* error message */
Bram Moolenaar7b668e82016-08-23 23:51:21 +02001663 char_u *after_modifier = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001664 exarg_T ea; /* Ex command arguments */
Bram Moolenaar8e258a42009-07-09 13:55:43 +00001665 int save_msg_scroll = msg_scroll;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001666 cmdmod_T save_cmdmod;
Bram Moolenaar9a2c0912019-03-30 14:26:18 +01001667 int save_reg_executing = reg_executing;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001668 int ni; /* set when Not Implemented */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001669 char_u *cmd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001670
1671 vim_memset(&ea, 0, sizeof(ea));
1672 ea.line1 = 1;
1673 ea.line2 = 1;
1674#ifdef FEAT_EVAL
1675 ++ex_nesting_level;
1676#endif
1677
Bram Moolenaar21691f82012-12-05 19:13:18 +01001678 /* When the last file has not been edited :q has to be typed twice. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001679 if (quitmore
1680#ifdef FEAT_EVAL
1681 /* avoid that a function call in 'statusline' does this */
Bram Moolenaar89d40322006-08-29 15:30:07 +00001682 && !getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001683#endif
Bram Moolenaar21691f82012-12-05 19:13:18 +01001684 /* avoid that an autocommand, e.g. QuitPre, does this */
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001685 && !getline_equal(fgetline, cookie, getnextac))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001686 --quitmore;
1687
1688 /*
1689 * Reset browse, confirm, etc.. They are restored when returning, for
1690 * recursive calls.
1691 */
1692 save_cmdmod = cmdmod;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001693
Bram Moolenaarcbb37ad2006-08-16 15:04:21 +00001694 /* "#!anything" is handled like a comment. */
1695 if ((*cmdlinep)[0] == '#' && (*cmdlinep)[1] == '!')
1696 goto doend;
1697
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02001698/*
1699 * 1. Skip comment lines and leading white space and colons.
1700 * 2. Handle command modifiers.
1701 */
1702 // The "ea" structure holds the arguments that can be used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001703 ea.cmd = *cmdlinep;
Bram Moolenaareffed932018-08-14 13:38:17 +02001704 ea.cmdlinep = cmdlinep;
1705 ea.getline = fgetline;
1706 ea.cookie = cookie;
1707#ifdef FEAT_EVAL
1708 ea.cstack = cstack;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001709#endif
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02001710 if (parse_command_modifiers(&ea, &errormsg, FALSE) == FAIL)
Bram Moolenaareffed932018-08-14 13:38:17 +02001711 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001712
Bram Moolenaar7b668e82016-08-23 23:51:21 +02001713 after_modifier = ea.cmd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001714
1715#ifdef FEAT_EVAL
1716 ea.skip = did_emsg || got_int || did_throw || (cstack->cs_idx >= 0
1717 && !(cstack->cs_flags[cstack->cs_idx] & CSF_ACTIVE));
1718#else
1719 ea.skip = (if_level > 0);
1720#endif
1721
Bram Moolenaar071d4272004-06-13 20:20:40 +00001722/*
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001723 * 3. Skip over the range to find the command. Let "p" point to after it.
1724 *
1725 * We need the command to know what kind of range it uses.
1726 */
1727 cmd = ea.cmd;
1728 ea.cmd = skip_range(ea.cmd, NULL);
1729 if (*ea.cmd == '*' && vim_strchr(p_cpo, CPO_STAR) == NULL)
1730 ea.cmd = skipwhite(ea.cmd + 1);
1731 p = find_command(&ea, NULL);
1732
Bram Moolenaar7feb35e2018-08-21 17:49:54 +02001733#ifdef FEAT_EVAL
1734# ifdef FEAT_PROFILE
1735 // Count this line for profiling if skip is TRUE.
1736 if (do_profiling == PROF_YES
1737 && (!ea.skip || cstack->cs_idx == 0 || (cstack->cs_idx > 0
1738 && (cstack->cs_flags[cstack->cs_idx - 1] & CSF_ACTIVE))))
1739 {
1740 int skip = did_emsg || got_int || did_throw;
1741
1742 if (ea.cmdidx == CMD_catch)
1743 skip = !skip && !(cstack->cs_idx >= 0
1744 && (cstack->cs_flags[cstack->cs_idx] & CSF_THROWN)
1745 && !(cstack->cs_flags[cstack->cs_idx] & CSF_CAUGHT));
1746 else if (ea.cmdidx == CMD_else || ea.cmdidx == CMD_elseif)
1747 skip = skip || !(cstack->cs_idx >= 0
1748 && !(cstack->cs_flags[cstack->cs_idx]
1749 & (CSF_ACTIVE | CSF_TRUE)));
1750 else if (ea.cmdidx == CMD_finally)
1751 skip = FALSE;
1752 else if (ea.cmdidx != CMD_endif
1753 && ea.cmdidx != CMD_endfor
1754 && ea.cmdidx != CMD_endtry
1755 && ea.cmdidx != CMD_endwhile)
1756 skip = ea.skip;
1757
1758 if (!skip)
1759 {
1760 if (getline_equal(fgetline, cookie, get_func_line))
1761 func_line_exec(getline_cookie(fgetline, cookie));
1762 else if (getline_equal(fgetline, cookie, getsourceline))
1763 script_line_exec();
1764 }
1765 }
1766# endif
1767
1768 /* May go to debug mode. If this happens and the ">quit" debug command is
1769 * used, throw an interrupt exception and skip the next command. */
1770 dbg_check_breakpoint(&ea);
1771 if (!ea.skip && got_int)
1772 {
1773 ea.skip = TRUE;
1774 (void)do_intthrow(cstack);
1775 }
1776#endif
1777
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001778/*
1779 * 4. parse a range specifier of the form: addr [,addr] [;addr] ..
Bram Moolenaar071d4272004-06-13 20:20:40 +00001780 *
1781 * where 'addr' is:
1782 *
1783 * % (entire file)
1784 * $ [+-NUM]
1785 * 'x [+-NUM] (where x denotes a currently defined mark)
1786 * . [+-NUM]
1787 * [+-NUM]..
1788 * NUM
1789 *
1790 * The ea.cmd pointer is updated to point to the first character following the
1791 * range spec. If an initial address is found, but no second, the upper bound
1792 * is equal to the lower.
1793 */
1794
Bram Moolenaar25190db2019-05-04 15:05:28 +02001795 // ea.addr_type for user commands is set by find_ucmd
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01001796 if (!IS_USER_CMDIDX(ea.cmdidx))
1797 {
1798 if (ea.cmdidx != CMD_SIZE)
1799 ea.addr_type = cmdnames[(int)ea.cmdidx].cmd_addr_type;
1800 else
1801 ea.addr_type = ADDR_LINES;
1802
Bram Moolenaar25190db2019-05-04 15:05:28 +02001803 // :wincmd range depends on the argument.
Bram Moolenaar164f3262015-01-14 21:22:01 +01001804 if (ea.cmdidx == CMD_wincmd && p != NULL)
1805 get_wincmd_addr_type(skipwhite(p), &ea);
Bram Moolenaar25190db2019-05-04 15:05:28 +02001806#ifdef FEAT_QUICKFIX
1807 // :.cc in quickfix window uses line number
1808 if ((ea.cmdidx == CMD_cc || ea.cmdidx == CMD_ll) && bt_quickfix(curbuf))
1809 ea.addr_type = ADDR_OTHER;
1810#endif
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01001811 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001812
Bram Moolenaar84c8e5a2015-01-14 15:47:36 +01001813 ea.cmd = cmd;
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02001814 if (parse_cmd_address(&ea, &errormsg, FALSE) == FAIL)
Bram Moolenaaree8415b2018-08-10 23:13:12 +02001815 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001816
Bram Moolenaar071d4272004-06-13 20:20:40 +00001817/*
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001818 * 5. Parse the command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001819 */
1820
1821 /*
1822 * Skip ':' and any white space
1823 */
1824 ea.cmd = skipwhite(ea.cmd);
1825 while (*ea.cmd == ':')
1826 ea.cmd = skipwhite(ea.cmd + 1);
1827
1828 /*
1829 * If we got a line, but no command, then go to the line.
1830 * If we find a '|' or '\n' we set ea.nextcmd.
1831 */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01001832 if (*ea.cmd == NUL || *ea.cmd == '"'
1833 || (ea.nextcmd = check_nextcmd(ea.cmd)) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001834 {
1835 /*
1836 * strange vi behaviour:
1837 * ":3" jumps to line 3
1838 * ":3|..." prints line 3
1839 * ":|" prints current line
1840 */
1841 if (ea.skip) /* skip this if inside :if */
1842 goto doend;
Bram Moolenaardf177f62005-02-22 08:39:57 +00001843 if (*ea.cmd == '|' || (exmode_active && ea.line1 != ea.line2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001844 {
1845 ea.cmdidx = CMD_print;
1846 ea.argt = RANGE+COUNT+TRLBAR;
1847 if ((errormsg = invalid_range(&ea)) == NULL)
1848 {
1849 correct_range(&ea);
1850 ex_print(&ea);
1851 }
1852 }
1853 else if (ea.addr_count != 0)
1854 {
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00001855 if (ea.line2 > curbuf->b_ml.ml_line_count)
1856 {
1857 /* With '-' in 'cpoptions' a line number past the file is an
1858 * error, otherwise put it at the end of the file. */
1859 if (vim_strchr(p_cpo, CPO_MINUS) != NULL)
1860 ea.line2 = -1;
1861 else
1862 ea.line2 = curbuf->b_ml.ml_line_count;
1863 }
1864
1865 if (ea.line2 < 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001866 errormsg = _(e_invrange);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001867 else
1868 {
1869 if (ea.line2 == 0)
1870 curwin->w_cursor.lnum = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001871 else
1872 curwin->w_cursor.lnum = ea.line2;
1873 beginline(BL_SOL | BL_FIX);
1874 }
1875 }
1876 goto doend;
1877 }
1878
Bram Moolenaard5005162014-08-22 23:05:54 +02001879 /* If this looks like an undefined user command and there are CmdUndefined
1880 * autocommands defined, trigger the matching autocommands. */
1881 if (p != NULL && ea.cmdidx == CMD_SIZE && !ea.skip
1882 && ASCII_ISUPPER(*ea.cmd)
1883 && has_cmdundefined())
1884 {
Bram Moolenaard5005162014-08-22 23:05:54 +02001885 int ret;
1886
Bram Moolenaar5a31b462014-08-23 14:16:20 +02001887 p = ea.cmd;
Bram Moolenaard5005162014-08-22 23:05:54 +02001888 while (ASCII_ISALNUM(*p))
1889 ++p;
Bram Moolenaar120f4a82014-09-09 12:22:06 +02001890 p = vim_strnsave(ea.cmd, (int)(p - ea.cmd));
Bram Moolenaard5005162014-08-22 23:05:54 +02001891 ret = apply_autocmds(EVENT_CMDUNDEFINED, p, p, TRUE, NULL);
1892 vim_free(p);
Bram Moolenaar829aef12015-07-28 14:25:48 +02001893 /* If the autocommands did something and didn't cause an error, try
1894 * finding the command again. */
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001895 p = (ret
1896#ifdef FEAT_EVAL
1897 && !aborting()
Bram Moolenaard5005162014-08-22 23:05:54 +02001898#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001899 ) ? find_command(&ea, NULL) : ea.cmd;
1900 }
Bram Moolenaard5005162014-08-22 23:05:54 +02001901
Bram Moolenaar071d4272004-06-13 20:20:40 +00001902 if (p == NULL)
1903 {
1904 if (!ea.skip)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001905 errormsg = _("E464: Ambiguous use of user-defined command");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001906 goto doend;
1907 }
Bram Moolenaarac9fb182019-04-27 13:04:13 +02001908 // Check for wrong commands.
Bram Moolenaar6f9a4762017-06-22 20:39:17 +02001909 if (*p == '!' && ea.cmd[1] == 0151 && ea.cmd[0] == 78
1910 && !IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001911 {
1912 errormsg = uc_fun_cmd();
1913 goto doend;
1914 }
Bram Moolenaarac9fb182019-04-27 13:04:13 +02001915
Bram Moolenaar071d4272004-06-13 20:20:40 +00001916 if (ea.cmdidx == CMD_SIZE)
1917 {
1918 if (!ea.skip)
1919 {
1920 STRCPY(IObuff, _("E492: Not an editor command"));
1921 if (!sourcing)
Bram Moolenaar7b668e82016-08-23 23:51:21 +02001922 {
1923 /* If the modifier was parsed OK the error must be in the
1924 * following command */
1925 if (after_modifier != NULL)
1926 append_command(after_modifier);
1927 else
1928 append_command(*cmdlinep);
1929 }
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001930 errormsg = (char *)IObuff;
Bram Moolenaar00b8ae02012-08-23 18:43:10 +02001931 did_emsg_syntax = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001932 }
1933 goto doend;
1934 }
1935
Bram Moolenaar958636c2014-10-21 20:01:58 +02001936 ni = (!IS_USER_CMDIDX(ea.cmdidx)
1937 && (cmdnames[ea.cmdidx].cmd_func == ex_ni
Bram Moolenaar7bb75552007-07-16 18:39:49 +00001938#ifdef HAVE_EX_SCRIPT_NI
1939 || cmdnames[ea.cmdidx].cmd_func == ex_script_ni
1940#endif
1941 ));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001942
1943#ifndef FEAT_EVAL
1944 /*
1945 * When the expression evaluation is disabled, recognize the ":if" and
1946 * ":endif" commands and ignore everything in between it.
1947 */
1948 if (ea.cmdidx == CMD_if)
1949 ++if_level;
1950 if (if_level)
1951 {
1952 if (ea.cmdidx == CMD_endif)
1953 --if_level;
1954 goto doend;
1955 }
1956
1957#endif
1958
Bram Moolenaar196b3b02008-06-20 16:51:41 +00001959 /* forced commands */
1960 if (*p == '!' && ea.cmdidx != CMD_substitute
1961 && ea.cmdidx != CMD_smagic && ea.cmdidx != CMD_snomagic)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001962 {
1963 ++p;
1964 ea.forceit = TRUE;
1965 }
1966 else
1967 ea.forceit = FALSE;
1968
1969/*
Bram Moolenaarb7316892019-05-01 18:08:42 +02001970 * 6. Parse arguments. Then check for errors.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001971 */
Bram Moolenaar958636c2014-10-21 20:01:58 +02001972 if (!IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001973 ea.argt = (long)cmdnames[(int)ea.cmdidx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001974
1975 if (!ea.skip)
1976 {
1977#ifdef HAVE_SANDBOX
1978 if (sandbox != 0 && !(ea.argt & SBOXOK))
1979 {
Bram Moolenaar8c62a082019-02-08 14:34:10 +01001980 // Command not allowed in sandbox.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001981 errormsg = _(e_sandbox);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001982 goto doend;
1983 }
1984#endif
Bram Moolenaar8c62a082019-02-08 14:34:10 +01001985 if (restricted != 0 && (ea.argt & RESTRICT))
1986 {
1987 errormsg = _("E981: Command not allowed in rvim");
1988 goto doend;
1989 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001990 if (!curbuf->b_p_ma && (ea.argt & MODIFY))
1991 {
1992 /* Command not allowed in non-'modifiable' buffer */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001993 errormsg = _(e_modifiable);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001994 goto doend;
1995 }
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00001996
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00001997 if (text_locked() && !(ea.argt & CMDWIN)
Bram Moolenaar958636c2014-10-21 20:01:58 +02001998 && !IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001999 {
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002000 /* Command not allowed when editing the command line. */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002001 errormsg = _(get_text_locked_msg());
Bram Moolenaar071d4272004-06-13 20:20:40 +00002002 goto doend;
2003 }
Bram Moolenaar379fb762018-08-30 15:58:28 +02002004
Bram Moolenaar5555acc2006-04-07 21:33:12 +00002005 /* Disallow editing another buffer when "curbuf_lock" is set.
Bram Moolenaar379fb762018-08-30 15:58:28 +02002006 * Do allow ":checktime" (it is postponed).
2007 * Do allow ":edit" (check for an argument later).
2008 * Do allow ":file" with no arguments (check for an argument later). */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002009 if (!(ea.argt & CMDWIN)
Bram Moolenaar5555acc2006-04-07 21:33:12 +00002010 && ea.cmdidx != CMD_checktime
Bram Moolenaar379fb762018-08-30 15:58:28 +02002011 && ea.cmdidx != CMD_edit
2012 && ea.cmdidx != CMD_file
Bram Moolenaar958636c2014-10-21 20:01:58 +02002013 && !IS_USER_CMDIDX(ea.cmdidx)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002014 && curbuf_locked())
2015 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002016
2017 if (!ni && !(ea.argt & RANGE) && ea.addr_count > 0)
2018 {
2019 /* no range allowed */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002020 errormsg = _(e_norange);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002021 goto doend;
2022 }
2023 }
2024
2025 if (!ni && !(ea.argt & BANG) && ea.forceit) /* no <!> allowed */
2026 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002027 errormsg = _(e_nobang);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002028 goto doend;
2029 }
2030
2031 /*
2032 * Don't complain about the range if it is not used
2033 * (could happen if line_count is accidentally set to 0).
2034 */
Bram Moolenaarb7316892019-05-01 18:08:42 +02002035 if (!ea.skip && !ni && (ea.argt & RANGE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002036 {
2037 /*
2038 * If the range is backwards, ask for confirmation and, if given, swap
2039 * ea.line1 & ea.line2 so it's forwards again.
2040 * When global command is busy, don't ask, will fail below.
2041 */
2042 if (!global_busy && ea.line1 > ea.line2)
2043 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002044 if (msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002045 {
Bram Moolenaara5792f52005-11-23 21:25:05 +00002046 if (sourcing || exmode_active)
2047 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002048 errormsg = _("E493: Backwards range given");
Bram Moolenaara5792f52005-11-23 21:25:05 +00002049 goto doend;
2050 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002051 if (ask_yesno((char_u *)
2052 _("Backwards range given, OK to swap"), FALSE) != 'y')
Bram Moolenaara5792f52005-11-23 21:25:05 +00002053 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002054 }
2055 lnum = ea.line1;
2056 ea.line1 = ea.line2;
2057 ea.line2 = lnum;
2058 }
2059 if ((errormsg = invalid_range(&ea)) != NULL)
2060 goto doend;
2061 }
2062
Bram Moolenaarb7316892019-05-01 18:08:42 +02002063 if ((ea.addr_type == ADDR_OTHER) && ea.addr_count == 0)
2064 // default is 1, not cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00002065 ea.line2 = 1;
2066
2067 correct_range(&ea);
2068
2069#ifdef FEAT_FOLDING
Bram Moolenaara3306952016-01-02 21:41:06 +01002070 if (((ea.argt & WHOLEFOLD) || ea.addr_count >= 2) && !global_busy
2071 && ea.addr_type == ADDR_LINES)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002072 {
2073 /* Put the first line at the start of a closed fold, put the last line
2074 * at the end of a closed fold. */
2075 (void)hasFolding(ea.line1, &ea.line1, NULL);
2076 (void)hasFolding(ea.line2, NULL, &ea.line2);
2077 }
2078#endif
2079
2080#ifdef FEAT_QUICKFIX
2081 /*
Bram Moolenaar86b68352004-12-27 21:59:20 +00002082 * For the ":make" and ":grep" commands we insert the 'makeprg'/'grepprg'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002083 * option here, so things like % get expanded.
2084 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +00002085 p = replace_makeprg(&ea, p, cmdlinep);
2086 if (p == NULL)
2087 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002088#endif
2089
2090 /*
2091 * Skip to start of argument.
2092 * Don't do this for the ":!" command, because ":!! -l" needs the space.
2093 */
2094 if (ea.cmdidx == CMD_bang)
2095 ea.arg = p;
2096 else
2097 ea.arg = skipwhite(p);
2098
Bram Moolenaar379fb762018-08-30 15:58:28 +02002099 // ":file" cannot be run with an argument when "curbuf_lock" is set
2100 if (ea.cmdidx == CMD_file && *ea.arg != NUL && curbuf_locked())
2101 goto doend;
2102
Bram Moolenaar071d4272004-06-13 20:20:40 +00002103 /*
2104 * Check for "++opt=val" argument.
2105 * Must be first, allow ":w ++enc=utf8 !cmd"
2106 */
2107 if (ea.argt & ARGOPT)
2108 while (ea.arg[0] == '+' && ea.arg[1] == '+')
2109 if (getargopt(&ea) == FAIL && !ni)
2110 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002111 errormsg = _(e_invarg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002112 goto doend;
2113 }
2114
2115 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
2116 {
2117 if (*ea.arg == '>') /* append */
2118 {
2119 if (*++ea.arg != '>') /* typed wrong */
2120 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002121 errormsg = _("E494: Use w or w>>");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002122 goto doend;
2123 }
2124 ea.arg = skipwhite(ea.arg + 1);
2125 ea.append = TRUE;
2126 }
2127 else if (*ea.arg == '!' && ea.cmdidx == CMD_write) /* :w !filter */
2128 {
2129 ++ea.arg;
2130 ea.usefilter = TRUE;
2131 }
2132 }
2133
2134 if (ea.cmdidx == CMD_read)
2135 {
2136 if (ea.forceit)
2137 {
2138 ea.usefilter = TRUE; /* :r! filter if ea.forceit */
2139 ea.forceit = FALSE;
2140 }
2141 else if (*ea.arg == '!') /* :r !filter */
2142 {
2143 ++ea.arg;
2144 ea.usefilter = TRUE;
2145 }
2146 }
2147
2148 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
2149 {
2150 ea.amount = 1;
2151 while (*ea.arg == *ea.cmd) /* count number of '>' or '<' */
2152 {
2153 ++ea.arg;
2154 ++ea.amount;
2155 }
2156 ea.arg = skipwhite(ea.arg);
2157 }
2158
2159 /*
2160 * Check for "+command" argument, before checking for next command.
2161 * Don't do this for ":read !cmd" and ":write !cmd".
2162 */
2163 if ((ea.argt & EDITCMD) && !ea.usefilter)
2164 ea.do_ecmd_cmd = getargcmd(&ea.arg);
2165
2166 /*
2167 * Check for '|' to separate commands and '"' to start comments.
2168 * Don't do this for ":read !cmd" and ":write !cmd".
2169 */
2170 if ((ea.argt & TRLBAR) && !ea.usefilter)
2171 separate_nextcmd(&ea);
2172
2173 /*
2174 * Check for <newline> to end a shell command.
Bram Moolenaardf177f62005-02-22 08:39:57 +00002175 * Also do this for ":read !cmd", ":write !cmd" and ":global".
2176 * Any others?
Bram Moolenaar071d4272004-06-13 20:20:40 +00002177 */
Bram Moolenaardf177f62005-02-22 08:39:57 +00002178 else if (ea.cmdidx == CMD_bang
Bram Moolenaar67883b42017-07-27 22:57:00 +02002179 || ea.cmdidx == CMD_terminal
Bram Moolenaardf177f62005-02-22 08:39:57 +00002180 || ea.cmdidx == CMD_global
2181 || ea.cmdidx == CMD_vglobal
2182 || ea.usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002183 {
2184 for (p = ea.arg; *p; ++p)
2185 {
2186 /* Remove one backslash before a newline, so that it's possible to
2187 * pass a newline to the shell and also a newline that is preceded
2188 * with a backslash. This makes it impossible to end a shell
2189 * command in a backslash, but that doesn't appear useful.
2190 * Halving the number of backslashes is incompatible with previous
2191 * versions. */
2192 if (*p == '\\' && p[1] == '\n')
Bram Moolenaara7241f52008-06-24 20:39:31 +00002193 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002194 else if (*p == '\n')
2195 {
2196 ea.nextcmd = p + 1;
2197 *p = NUL;
2198 break;
2199 }
2200 }
2201 }
2202
2203 if ((ea.argt & DFLALL) && ea.addr_count == 0)
2204 {
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002205 buf_T *buf;
2206
Bram Moolenaar071d4272004-06-13 20:20:40 +00002207 ea.line1 = 1;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002208 switch (ea.addr_type)
2209 {
2210 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02002211 case ADDR_OTHER:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002212 ea.line2 = curbuf->b_ml.ml_line_count;
2213 break;
2214 case ADDR_LOADED_BUFFERS:
2215 buf = firstbuf;
2216 while (buf->b_next != NULL && buf->b_ml.ml_mfp == NULL)
2217 buf = buf->b_next;
2218 ea.line1 = buf->b_fnum;
2219 buf = lastbuf;
2220 while (buf->b_prev != NULL && buf->b_ml.ml_mfp == NULL)
2221 buf = buf->b_prev;
2222 ea.line2 = buf->b_fnum;
2223 break;
2224 case ADDR_BUFFERS:
2225 ea.line1 = firstbuf->b_fnum;
2226 ea.line2 = lastbuf->b_fnum;
2227 break;
2228 case ADDR_WINDOWS:
2229 ea.line2 = LAST_WIN_NR;
2230 break;
2231 case ADDR_TABS:
2232 ea.line2 = LAST_TAB_NR;
2233 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01002234 case ADDR_TABS_RELATIVE:
2235 ea.line2 = 1;
2236 break;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002237 case ADDR_ARGUMENTS:
2238 if (ARGCOUNT == 0)
2239 ea.line1 = ea.line2 = 0;
2240 else
2241 ea.line2 = ARGCOUNT;
2242 break;
Bram Moolenaar25190db2019-05-04 15:05:28 +02002243 case ADDR_QUICKFIX_VALID:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02002244#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02002245 ea.line2 = qf_get_valid_size(&ea);
Bram Moolenaaraa23b372015-09-08 18:46:31 +02002246 if (ea.line2 == 0)
2247 ea.line2 = 1;
Bram Moolenaare906c502015-09-09 21:10:39 +02002248#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02002249 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02002250 case ADDR_NONE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02002251 case ADDR_UNSIGNED:
2252 case ADDR_QUICKFIX:
2253 iemsg(_("INTERNAL: Cannot use DFLALL with ADDR_NONE, ADDR_UNSIGNED or ADDR_QUICKFIX"));
Bram Moolenaarb7316892019-05-01 18:08:42 +02002254 break;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01002255 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002256 }
2257
2258 /* accept numbered register only when no count allowed (:put) */
2259 if ( (ea.argt & REGSTR)
2260 && *ea.arg != NUL
Bram Moolenaar958636c2014-10-21 20:01:58 +02002261 /* Do not allow register = for user commands */
2262 && (!IS_USER_CMDIDX(ea.cmdidx) || *ea.arg != '=')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002263 && !((ea.argt & COUNT) && VIM_ISDIGIT(*ea.arg)))
2264 {
Bram Moolenaar85de2062011-05-05 14:26:41 +02002265#ifndef FEAT_CLIPBOARD
2266 /* check these explicitly for a more specific error message */
2267 if (*ea.arg == '*' || *ea.arg == '+')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002268 {
Bram Moolenaar99b12722019-01-14 20:16:40 +01002269 errormsg = _(e_invalidreg);
Bram Moolenaar85de2062011-05-05 14:26:41 +02002270 goto doend;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002271 }
2272#endif
Bram Moolenaar958636c2014-10-21 20:01:58 +02002273 if (valid_yank_reg(*ea.arg, (ea.cmdidx != CMD_put
2274 && !IS_USER_CMDIDX(ea.cmdidx))))
Bram Moolenaar85de2062011-05-05 14:26:41 +02002275 {
2276 ea.regname = *ea.arg++;
2277#ifdef FEAT_EVAL
2278 /* for '=' register: accept the rest of the line as an expression */
2279 if (ea.arg[-1] == '=' && ea.arg[0] != NUL)
2280 {
2281 set_expr_line(vim_strsave(ea.arg));
2282 ea.arg += STRLEN(ea.arg);
2283 }
2284#endif
2285 ea.arg = skipwhite(ea.arg);
2286 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002287 }
2288
2289 /*
2290 * Check for a count. When accepting a BUFNAME, don't use "123foo" as a
2291 * count, it's a buffer name.
2292 */
2293 if ((ea.argt & COUNT) && VIM_ISDIGIT(*ea.arg)
2294 && (!(ea.argt & BUFNAME) || *(p = skipdigits(ea.arg)) == NUL
Bram Moolenaar1c465442017-03-12 20:10:05 +01002295 || VIM_ISWHITE(*p)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002296 {
2297 n = getdigits(&ea.arg);
2298 ea.arg = skipwhite(ea.arg);
Bram Moolenaar80a94a52006-02-23 21:26:58 +00002299 if (n <= 0 && !ni && (ea.argt & ZEROR) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002300 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002301 errormsg = _(e_zerocount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002302 goto doend;
2303 }
Bram Moolenaarb7316892019-05-01 18:08:42 +02002304 if (ea.addr_type != ADDR_LINES) // e.g. :buffer 2, :sleep 3
Bram Moolenaar071d4272004-06-13 20:20:40 +00002305 {
2306 ea.line2 = n;
2307 if (ea.addr_count == 0)
2308 ea.addr_count = 1;
2309 }
2310 else
2311 {
2312 ea.line1 = ea.line2;
2313 ea.line2 += n - 1;
2314 ++ea.addr_count;
2315 /*
2316 * Be vi compatible: no error message for out of range.
2317 */
Bram Moolenaarb7316892019-05-01 18:08:42 +02002318 if (ea.line2 > curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002319 ea.line2 = curbuf->b_ml.ml_line_count;
2320 }
2321 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00002322
2323 /*
2324 * Check for flags: 'l', 'p' and '#'.
2325 */
2326 if (ea.argt & EXFLAGS)
2327 get_flags(&ea);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002328 /* no arguments allowed */
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00002329 if (!ni && !(ea.argt & EXTRA) && *ea.arg != NUL
Bram Moolenaara2031822006-03-07 22:29:51 +00002330 && *ea.arg != '"' && (*ea.arg != '|' || (ea.argt & TRLBAR) == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002331 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002332 errormsg = _(e_trailing);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002333 goto doend;
2334 }
2335
2336 if (!ni && (ea.argt & NEEDARG) && *ea.arg == NUL)
2337 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002338 errormsg = _(e_argreq);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002339 goto doend;
2340 }
2341
2342#ifdef FEAT_EVAL
2343 /*
2344 * Skip the command when it's not going to be executed.
2345 * The commands like :if, :endif, etc. always need to be executed.
2346 * Also make an exception for commands that handle a trailing command
2347 * themselves.
2348 */
2349 if (ea.skip)
2350 {
2351 switch (ea.cmdidx)
2352 {
2353 /* commands that need evaluation */
2354 case CMD_while:
2355 case CMD_endwhile:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00002356 case CMD_for:
2357 case CMD_endfor:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002358 case CMD_if:
2359 case CMD_elseif:
2360 case CMD_else:
2361 case CMD_endif:
2362 case CMD_try:
2363 case CMD_catch:
2364 case CMD_finally:
2365 case CMD_endtry:
2366 case CMD_function:
2367 break;
2368
2369 /* Commands that handle '|' themselves. Check: A command should
2370 * either have the TRLBAR flag, appear in this list or appear in
2371 * the list at ":help :bar". */
2372 case CMD_aboveleft:
2373 case CMD_and:
2374 case CMD_belowright:
2375 case CMD_botright:
2376 case CMD_browse:
2377 case CMD_call:
2378 case CMD_confirm:
2379 case CMD_delfunction:
2380 case CMD_djump:
2381 case CMD_dlist:
2382 case CMD_dsearch:
2383 case CMD_dsplit:
2384 case CMD_echo:
2385 case CMD_echoerr:
2386 case CMD_echomsg:
2387 case CMD_echon:
2388 case CMD_execute:
Bram Moolenaar7b668e82016-08-23 23:51:21 +02002389 case CMD_filter:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002390 case CMD_help:
2391 case CMD_hide:
2392 case CMD_ijump:
2393 case CMD_ilist:
2394 case CMD_isearch:
2395 case CMD_isplit:
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002396 case CMD_keepalt:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002397 case CMD_keepjumps:
2398 case CMD_keepmarks:
Bram Moolenaara939e432013-11-09 05:30:26 +01002399 case CMD_keeppatterns:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002400 case CMD_leftabove:
2401 case CMD_let:
2402 case CMD_lockmarks:
Bram Moolenaar0ba04292010-07-14 23:23:17 +02002403 case CMD_lua:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002404 case CMD_match:
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002405 case CMD_mzscheme:
Bram Moolenaar5803ae62014-03-23 16:04:02 +01002406 case CMD_noautocmd:
2407 case CMD_noswapfile:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002408 case CMD_perl:
2409 case CMD_psearch:
2410 case CMD_python:
Bram Moolenaar368373e2010-07-19 20:46:22 +02002411 case CMD_py3:
Bram Moolenaarb6590522010-07-21 16:00:43 +02002412 case CMD_python3:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002413 case CMD_return:
2414 case CMD_rightbelow:
2415 case CMD_ruby:
2416 case CMD_silent:
2417 case CMD_smagic:
2418 case CMD_snomagic:
2419 case CMD_substitute:
2420 case CMD_syntax:
Bram Moolenaara226a6d2006-02-26 23:59:20 +00002421 case CMD_tab:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002422 case CMD_tcl:
2423 case CMD_throw:
2424 case CMD_tilde:
2425 case CMD_topleft:
2426 case CMD_unlet:
2427 case CMD_verbose:
2428 case CMD_vertical:
Bram Moolenaar12bde492011-06-13 01:19:56 +02002429 case CMD_wincmd:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002430 break;
2431
2432 default: goto doend;
2433 }
2434 }
2435#endif
2436
2437 if (ea.argt & XFILE)
2438 {
2439 if (expand_filename(&ea, cmdlinep, &errormsg) == FAIL)
2440 goto doend;
2441 }
2442
Bram Moolenaar071d4272004-06-13 20:20:40 +00002443 /*
2444 * Accept buffer name. Cannot be used at the same time with a buffer
2445 * number. Don't do this for a user command.
2446 */
2447 if ((ea.argt & BUFNAME) && *ea.arg != NUL && ea.addr_count == 0
Bram Moolenaar958636c2014-10-21 20:01:58 +02002448 && !IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002449 {
2450 /*
2451 * :bdelete, :bwipeout and :bunload take several arguments, separated
2452 * by spaces: find next space (skipping over escaped characters).
2453 * The others take one argument: ignore trailing spaces.
2454 */
2455 if (ea.cmdidx == CMD_bdelete || ea.cmdidx == CMD_bwipeout
2456 || ea.cmdidx == CMD_bunload)
2457 p = skiptowhite_esc(ea.arg);
2458 else
2459 {
2460 p = ea.arg + STRLEN(ea.arg);
Bram Moolenaar1c465442017-03-12 20:10:05 +01002461 while (p > ea.arg && VIM_ISWHITE(p[-1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002462 --p;
2463 }
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002464 ea.line2 = buflist_findpat(ea.arg, p, (ea.argt & BUFUNL) != 0,
2465 FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002466 if (ea.line2 < 0) /* failed */
2467 goto doend;
2468 ea.addr_count = 1;
2469 ea.arg = skipwhite(p);
2470 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002471
Bram Moolenaar2be57332018-02-13 18:05:18 +01002472 /* The :try command saves the emsg_silent flag, reset it here when
2473 * ":silent! try" was used, it should only apply to :try itself. */
Bram Moolenaareffed932018-08-14 13:38:17 +02002474 if (ea.cmdidx == CMD_try && ea.did_esilent > 0)
Bram Moolenaar2be57332018-02-13 18:05:18 +01002475 {
Bram Moolenaareffed932018-08-14 13:38:17 +02002476 emsg_silent -= ea.did_esilent;
Bram Moolenaar2be57332018-02-13 18:05:18 +01002477 if (emsg_silent < 0)
2478 emsg_silent = 0;
Bram Moolenaareffed932018-08-14 13:38:17 +02002479 ea.did_esilent = 0;
Bram Moolenaar2be57332018-02-13 18:05:18 +01002480 }
2481
Bram Moolenaar071d4272004-06-13 20:20:40 +00002482/*
Bram Moolenaar2be57332018-02-13 18:05:18 +01002483 * 7. Execute the command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002484 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002485
Bram Moolenaar958636c2014-10-21 20:01:58 +02002486 if (IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002487 {
2488 /*
2489 * Execute a user-defined command.
2490 */
2491 do_ucmd(&ea);
2492 }
2493 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002494 {
2495 /*
Bram Moolenaarac9fb182019-04-27 13:04:13 +02002496 * Call the function to execute the builtin command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002497 */
2498 ea.errmsg = NULL;
2499 (cmdnames[ea.cmdidx].cmd_func)(&ea);
2500 if (ea.errmsg != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002501 errormsg = _(ea.errmsg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002502 }
2503
2504#ifdef FEAT_EVAL
2505 /*
2506 * If the command just executed called do_cmdline(), any throw or ":return"
2507 * or ":finish" encountered there must also check the cstack of the still
2508 * active do_cmdline() that called this do_one_cmd(). Rethrow an uncaught
2509 * exception, or reanimate a returned function or finished script file and
2510 * return or finish it again.
2511 */
2512 if (need_rethrow)
2513 do_throw(cstack);
2514 else if (check_cstack)
2515 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002516 if (source_finished(fgetline, cookie))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002517 do_finish(&ea, TRUE);
Bram Moolenaar89d40322006-08-29 15:30:07 +00002518 else if (getline_equal(fgetline, cookie, get_func_line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002519 && current_func_returned())
2520 do_return(&ea, TRUE, FALSE, NULL);
2521 }
2522 need_rethrow = check_cstack = FALSE;
2523#endif
2524
2525doend:
2526 if (curwin->w_cursor.lnum == 0) /* can happen with zero line number */
Bram Moolenaar6de5e122017-04-20 21:55:44 +02002527 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002528 curwin->w_cursor.lnum = 1;
Bram Moolenaar6de5e122017-04-20 21:55:44 +02002529 curwin->w_cursor.col = 0;
2530 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002531
2532 if (errormsg != NULL && *errormsg != NUL && !did_emsg)
2533 {
2534 if (sourcing)
2535 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002536 if (errormsg != (char *)IObuff)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002537 {
2538 STRCPY(IObuff, errormsg);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002539 errormsg = (char *)IObuff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002540 }
Bram Moolenaara6f4d612011-09-21 19:10:46 +02002541 append_command(*cmdlinep);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002542 }
2543 emsg(errormsg);
2544 }
2545#ifdef FEAT_EVAL
2546 do_errthrow(cstack,
Bram Moolenaar958636c2014-10-21 20:01:58 +02002547 (ea.cmdidx != CMD_SIZE && !IS_USER_CMDIDX(ea.cmdidx))
2548 ? cmdnames[(int)ea.cmdidx].cmd_name : (char_u *)NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002549#endif
2550
Bram Moolenaareffed932018-08-14 13:38:17 +02002551 if (ea.verbose_save >= 0)
2552 p_verbose = ea.verbose_save;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002553
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002554 free_cmdmod();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002555 cmdmod = save_cmdmod;
Bram Moolenaar9a2c0912019-03-30 14:26:18 +01002556 reg_executing = save_reg_executing;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002557
Bram Moolenaareffed932018-08-14 13:38:17 +02002558 if (ea.save_msg_silent != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002559 {
2560 /* messages could be enabled for a serious error, need to check if the
2561 * counters don't become negative */
Bram Moolenaareffed932018-08-14 13:38:17 +02002562 if (!did_emsg || msg_silent > ea.save_msg_silent)
2563 msg_silent = ea.save_msg_silent;
2564 emsg_silent -= ea.did_esilent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002565 if (emsg_silent < 0)
2566 emsg_silent = 0;
2567 /* Restore msg_scroll, it's set by file I/O commands, even when no
2568 * message is actually displayed. */
2569 msg_scroll = save_msg_scroll;
Bram Moolenaar77ab2802009-04-22 12:44:48 +00002570
2571 /* "silent reg" or "silent echo x" inside "redir" leaves msg_col
2572 * somewhere in the line. Put it back in the first column. */
2573 if (redirecting())
2574 msg_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002575 }
2576
Bram Moolenaar7171abe2004-10-11 10:06:20 +00002577#ifdef HAVE_SANDBOX
Bram Moolenaareffed932018-08-14 13:38:17 +02002578 if (ea.did_sandbox)
Bram Moolenaar7171abe2004-10-11 10:06:20 +00002579 --sandbox;
2580#endif
2581
Bram Moolenaar071d4272004-06-13 20:20:40 +00002582 if (ea.nextcmd && *ea.nextcmd == NUL) /* not really a next command */
2583 ea.nextcmd = NULL;
2584
2585#ifdef FEAT_EVAL
2586 --ex_nesting_level;
2587#endif
2588
2589 return ea.nextcmd;
2590}
2591#if (_MSC_VER == 1200)
Bram Moolenaar281bdce2005-01-25 21:53:18 +00002592 #pragma optimize( "", on )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002593#endif
2594
2595/*
Bram Moolenaareffed932018-08-14 13:38:17 +02002596 * Parse and skip over command modifiers:
2597 * - update eap->cmd
2598 * - store flags in "cmdmod".
2599 * - Set ex_pressedreturn for an empty command line.
2600 * - set msg_silent for ":silent"
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002601 * - set 'eventignore' to "all" for ":noautocmd"
Bram Moolenaareffed932018-08-14 13:38:17 +02002602 * - set p_verbose for ":verbose"
2603 * - Increment "sandbox" for ":sandbox"
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002604 * When "skip_only" is TRUE the global variables are not changed, except for
2605 * "cmdmod".
Bram Moolenaareffed932018-08-14 13:38:17 +02002606 * Return FAIL when the command is not to be executed.
2607 * May set "errormsg" to an error message.
2608 */
2609 int
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002610parse_command_modifiers(exarg_T *eap, char **errormsg, int skip_only)
Bram Moolenaareffed932018-08-14 13:38:17 +02002611{
2612 char_u *p;
2613
2614 vim_memset(&cmdmod, 0, sizeof(cmdmod));
2615 eap->verbose_save = -1;
2616 eap->save_msg_silent = -1;
2617
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002618 // Repeat until no more command modifiers are found.
Bram Moolenaareffed932018-08-14 13:38:17 +02002619 for (;;)
2620 {
Bram Moolenaareffed932018-08-14 13:38:17 +02002621 while (*eap->cmd == ' ' || *eap->cmd == '\t' || *eap->cmd == ':')
2622 ++eap->cmd;
2623
2624 /* in ex mode, an empty line works like :+ */
2625 if (*eap->cmd == NUL && exmode_active
2626 && (getline_equal(eap->getline, eap->cookie, getexmodeline)
2627 || getline_equal(eap->getline, eap->cookie, getexline))
2628 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
2629 {
2630 eap->cmd = (char_u *)"+";
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002631 if (!skip_only)
2632 ex_pressedreturn = TRUE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002633 }
2634
2635 /* ignore comment and empty lines */
2636 if (*eap->cmd == '"')
2637 return FAIL;
2638 if (*eap->cmd == NUL)
2639 {
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002640 if (!skip_only)
2641 ex_pressedreturn = TRUE;
Bram Moolenaareffed932018-08-14 13:38:17 +02002642 return FAIL;
2643 }
2644
Bram Moolenaareffed932018-08-14 13:38:17 +02002645 p = skip_range(eap->cmd, NULL);
2646 switch (*p)
2647 {
2648 /* When adding an entry, also modify cmd_exists(). */
2649 case 'a': if (!checkforcmd(&eap->cmd, "aboveleft", 3))
2650 break;
2651 cmdmod.split |= WSP_ABOVE;
2652 continue;
2653
2654 case 'b': if (checkforcmd(&eap->cmd, "belowright", 3))
2655 {
2656 cmdmod.split |= WSP_BELOW;
2657 continue;
2658 }
2659 if (checkforcmd(&eap->cmd, "browse", 3))
2660 {
2661#ifdef FEAT_BROWSE_CMD
2662 cmdmod.browse = TRUE;
2663#endif
2664 continue;
2665 }
2666 if (!checkforcmd(&eap->cmd, "botright", 2))
2667 break;
2668 cmdmod.split |= WSP_BOT;
2669 continue;
2670
2671 case 'c': if (!checkforcmd(&eap->cmd, "confirm", 4))
2672 break;
2673#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
2674 cmdmod.confirm = TRUE;
2675#endif
2676 continue;
2677
2678 case 'k': if (checkforcmd(&eap->cmd, "keepmarks", 3))
2679 {
2680 cmdmod.keepmarks = TRUE;
2681 continue;
2682 }
2683 if (checkforcmd(&eap->cmd, "keepalt", 5))
2684 {
2685 cmdmod.keepalt = TRUE;
2686 continue;
2687 }
2688 if (checkforcmd(&eap->cmd, "keeppatterns", 5))
2689 {
2690 cmdmod.keeppatterns = TRUE;
2691 continue;
2692 }
2693 if (!checkforcmd(&eap->cmd, "keepjumps", 5))
2694 break;
2695 cmdmod.keepjumps = TRUE;
2696 continue;
2697
2698 case 'f': /* only accept ":filter {pat} cmd" */
2699 {
2700 char_u *reg_pat;
2701
2702 if (!checkforcmd(&p, "filter", 4)
2703 || *p == NUL || ends_excmd(*p))
2704 break;
2705 if (*p == '!')
2706 {
2707 cmdmod.filter_force = TRUE;
2708 p = skipwhite(p + 1);
2709 if (*p == NUL || ends_excmd(*p))
2710 break;
2711 }
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002712 if (skip_only)
2713 p = skip_vimgrep_pat(p, NULL, NULL);
2714 else
2715 // NOTE: This puts a NUL after the pattern.
2716 p = skip_vimgrep_pat(p, &reg_pat, NULL);
Bram Moolenaareffed932018-08-14 13:38:17 +02002717 if (p == NULL || *p == NUL)
2718 break;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002719 if (!skip_only)
2720 {
2721 cmdmod.filter_regmatch.regprog =
Bram Moolenaareffed932018-08-14 13:38:17 +02002722 vim_regcomp(reg_pat, RE_MAGIC);
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002723 if (cmdmod.filter_regmatch.regprog == NULL)
2724 break;
2725 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002726 eap->cmd = p;
2727 continue;
2728 }
2729
2730 /* ":hide" and ":hide | cmd" are not modifiers */
2731 case 'h': if (p != eap->cmd || !checkforcmd(&p, "hide", 3)
2732 || *p == NUL || ends_excmd(*p))
2733 break;
2734 eap->cmd = p;
2735 cmdmod.hide = TRUE;
2736 continue;
2737
2738 case 'l': if (checkforcmd(&eap->cmd, "lockmarks", 3))
2739 {
2740 cmdmod.lockmarks = TRUE;
2741 continue;
2742 }
2743
2744 if (!checkforcmd(&eap->cmd, "leftabove", 5))
2745 break;
2746 cmdmod.split |= WSP_ABOVE;
2747 continue;
2748
2749 case 'n': if (checkforcmd(&eap->cmd, "noautocmd", 3))
2750 {
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002751 if (cmdmod.save_ei == NULL && !skip_only)
Bram Moolenaareffed932018-08-14 13:38:17 +02002752 {
2753 /* Set 'eventignore' to "all". Restore the
2754 * existing option value later. */
2755 cmdmod.save_ei = vim_strsave(p_ei);
2756 set_string_option_direct((char_u *)"ei", -1,
2757 (char_u *)"all", OPT_FREE, SID_NONE);
2758 }
2759 continue;
2760 }
2761 if (!checkforcmd(&eap->cmd, "noswapfile", 3))
2762 break;
2763 cmdmod.noswapfile = TRUE;
2764 continue;
2765
2766 case 'r': if (!checkforcmd(&eap->cmd, "rightbelow", 6))
2767 break;
2768 cmdmod.split |= WSP_BELOW;
2769 continue;
2770
2771 case 's': if (checkforcmd(&eap->cmd, "sandbox", 3))
2772 {
2773#ifdef HAVE_SANDBOX
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002774 if (!skip_only)
2775 {
2776 if (!eap->did_sandbox)
2777 ++sandbox;
2778 eap->did_sandbox = TRUE;
2779 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002780#endif
2781 continue;
2782 }
2783 if (!checkforcmd(&eap->cmd, "silent", 3))
2784 break;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002785 if (!skip_only)
2786 {
2787 if (eap->save_msg_silent == -1)
2788 eap->save_msg_silent = msg_silent;
2789 ++msg_silent;
2790 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002791 if (*eap->cmd == '!' && !VIM_ISWHITE(eap->cmd[-1]))
2792 {
2793 /* ":silent!", but not "silent !cmd" */
2794 eap->cmd = skipwhite(eap->cmd + 1);
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002795 if (!skip_only)
2796 {
2797 ++emsg_silent;
2798 ++eap->did_esilent;
2799 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002800 }
2801 continue;
2802
2803 case 't': if (checkforcmd(&p, "tab", 3))
2804 {
Bram Moolenaar548e5982018-12-26 21:45:00 +01002805 if (!skip_only)
Bram Moolenaareffed932018-08-14 13:38:17 +02002806 {
Bram Moolenaar548e5982018-12-26 21:45:00 +01002807 long tabnr = get_address(eap, &eap->cmd,
2808 ADDR_TABS, eap->skip,
2809 skip_only, FALSE, 1);
2810 if (tabnr == MAXLNUM)
2811 cmdmod.tab = tabpage_index(curtab) + 1;
2812 else
Bram Moolenaareffed932018-08-14 13:38:17 +02002813 {
Bram Moolenaar548e5982018-12-26 21:45:00 +01002814 if (tabnr < 0 || tabnr > LAST_TAB_NR)
2815 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002816 *errormsg = _(e_invrange);
Bram Moolenaar548e5982018-12-26 21:45:00 +01002817 return FAIL;
2818 }
2819 cmdmod.tab = tabnr + 1;
Bram Moolenaareffed932018-08-14 13:38:17 +02002820 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002821 }
2822 eap->cmd = p;
2823 continue;
2824 }
2825 if (!checkforcmd(&eap->cmd, "topleft", 2))
2826 break;
2827 cmdmod.split |= WSP_TOP;
2828 continue;
2829
2830 case 'u': if (!checkforcmd(&eap->cmd, "unsilent", 3))
2831 break;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002832 if (!skip_only)
2833 {
2834 if (eap->save_msg_silent == -1)
2835 eap->save_msg_silent = msg_silent;
2836 msg_silent = 0;
2837 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002838 continue;
2839
2840 case 'v': if (checkforcmd(&eap->cmd, "vertical", 4))
2841 {
2842 cmdmod.split |= WSP_VERT;
2843 continue;
2844 }
2845 if (!checkforcmd(&p, "verbose", 4))
2846 break;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002847 if (!skip_only)
2848 {
2849 if (eap->verbose_save < 0)
2850 eap->verbose_save = p_verbose;
2851 if (vim_isdigit(*eap->cmd))
2852 p_verbose = atoi((char *)eap->cmd);
2853 else
2854 p_verbose = 1;
2855 }
Bram Moolenaareffed932018-08-14 13:38:17 +02002856 eap->cmd = p;
2857 continue;
2858 }
2859 break;
2860 }
2861
2862 return OK;
2863}
2864
2865/*
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +02002866 * Free contents of "cmdmod".
2867 */
2868 static void
2869free_cmdmod(void)
2870{
2871 if (cmdmod.save_ei != NULL)
2872 {
2873 /* Restore 'eventignore' to the value before ":noautocmd". */
2874 set_string_option_direct((char_u *)"ei", -1, cmdmod.save_ei,
2875 OPT_FREE, SID_NONE);
2876 free_string_option(cmdmod.save_ei);
2877 }
2878
2879 if (cmdmod.filter_regmatch.regprog != NULL)
2880 vim_regfree(cmdmod.filter_regmatch.regprog);
2881}
2882
2883/*
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002884 * Parse the address range, if any, in "eap".
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02002885 * May set the last search pattern, unless "silent" is TRUE.
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002886 * Return FAIL and set "errormsg" or return OK.
2887 */
2888 int
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002889parse_cmd_address(exarg_T *eap, char **errormsg, int silent)
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002890{
2891 int address_count = 1;
2892 linenr_T lnum;
2893
2894 // Repeat for all ',' or ';' separated addresses.
2895 for (;;)
2896 {
2897 eap->line1 = eap->line2;
2898 switch (eap->addr_type)
2899 {
2900 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02002901 case ADDR_OTHER:
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002902 // default is current line number
2903 eap->line2 = curwin->w_cursor.lnum;
2904 break;
2905 case ADDR_WINDOWS:
2906 eap->line2 = CURRENT_WIN_NR;
2907 break;
2908 case ADDR_ARGUMENTS:
2909 eap->line2 = curwin->w_arg_idx + 1;
2910 if (eap->line2 > ARGCOUNT)
2911 eap->line2 = ARGCOUNT;
2912 break;
2913 case ADDR_LOADED_BUFFERS:
2914 case ADDR_BUFFERS:
2915 eap->line2 = curbuf->b_fnum;
2916 break;
2917 case ADDR_TABS:
2918 eap->line2 = CURRENT_TAB_NR;
2919 break;
2920 case ADDR_TABS_RELATIVE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02002921 case ADDR_UNSIGNED:
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002922 eap->line2 = 1;
2923 break;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002924 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02002925#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02002926 eap->line2 = qf_get_cur_idx(eap);
2927#endif
2928 break;
2929 case ADDR_QUICKFIX_VALID:
2930#ifdef FEAT_QUICKFIX
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002931 eap->line2 = qf_get_cur_valid_idx(eap);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002932#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02002933 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02002934 case ADDR_NONE:
2935 // Will give an error later if a range is found.
2936 break;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002937 }
2938 eap->cmd = skipwhite(eap->cmd);
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02002939 lnum = get_address(eap, &eap->cmd, eap->addr_type, eap->skip, silent,
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002940 eap->addr_count == 0, address_count++);
2941 if (eap->cmd == NULL) // error detected
2942 return FAIL;
2943 if (lnum == MAXLNUM)
2944 {
2945 if (*eap->cmd == '%') // '%' - all lines
2946 {
2947 ++eap->cmd;
2948 switch (eap->addr_type)
2949 {
2950 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02002951 case ADDR_OTHER:
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002952 eap->line1 = 1;
2953 eap->line2 = curbuf->b_ml.ml_line_count;
2954 break;
2955 case ADDR_LOADED_BUFFERS:
2956 {
2957 buf_T *buf = firstbuf;
2958
2959 while (buf->b_next != NULL
2960 && buf->b_ml.ml_mfp == NULL)
2961 buf = buf->b_next;
2962 eap->line1 = buf->b_fnum;
2963 buf = lastbuf;
2964 while (buf->b_prev != NULL
2965 && buf->b_ml.ml_mfp == NULL)
2966 buf = buf->b_prev;
2967 eap->line2 = buf->b_fnum;
2968 break;
2969 }
2970 case ADDR_BUFFERS:
2971 eap->line1 = firstbuf->b_fnum;
2972 eap->line2 = lastbuf->b_fnum;
2973 break;
2974 case ADDR_WINDOWS:
2975 case ADDR_TABS:
2976 if (IS_USER_CMDIDX(eap->cmdidx))
2977 {
2978 eap->line1 = 1;
2979 eap->line2 = eap->addr_type == ADDR_WINDOWS
2980 ? LAST_WIN_NR : LAST_TAB_NR;
2981 }
2982 else
2983 {
2984 // there is no Vim command which uses '%' and
2985 // ADDR_WINDOWS or ADDR_TABS
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002986 *errormsg = _(e_invrange);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002987 return FAIL;
2988 }
2989 break;
2990 case ADDR_TABS_RELATIVE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02002991 case ADDR_UNSIGNED:
2992 case ADDR_QUICKFIX:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002993 *errormsg = _(e_invrange);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02002994 return FAIL;
2995 case ADDR_ARGUMENTS:
2996 if (ARGCOUNT == 0)
2997 eap->line1 = eap->line2 = 0;
2998 else
2999 {
3000 eap->line1 = 1;
3001 eap->line2 = ARGCOUNT;
3002 }
3003 break;
Bram Moolenaar25190db2019-05-04 15:05:28 +02003004 case ADDR_QUICKFIX_VALID:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003005#ifdef FEAT_QUICKFIX
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003006 eap->line1 = 1;
Bram Moolenaar25190db2019-05-04 15:05:28 +02003007 eap->line2 = qf_get_valid_size(eap);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003008 if (eap->line2 == 0)
3009 eap->line2 = 1;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003010#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02003011 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02003012 case ADDR_NONE:
3013 // Will give an error later if a range is found.
3014 break;
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003015 }
3016 ++eap->addr_count;
3017 }
3018 else if (*eap->cmd == '*' && vim_strchr(p_cpo, CPO_STAR) == NULL)
3019 {
3020 pos_T *fp;
3021
3022 // '*' - visual area
3023 if (eap->addr_type != ADDR_LINES)
3024 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003025 *errormsg = _(e_invrange);
Bram Moolenaaree8415b2018-08-10 23:13:12 +02003026 return FAIL;
3027 }
3028
3029 ++eap->cmd;
3030 if (!eap->skip)
3031 {
3032 fp = getmark('<', FALSE);
3033 if (check_mark(fp) == FAIL)
3034 return FAIL;
3035 eap->line1 = fp->lnum;
3036 fp = getmark('>', FALSE);
3037 if (check_mark(fp) == FAIL)
3038 return FAIL;
3039 eap->line2 = fp->lnum;
3040 ++eap->addr_count;
3041 }
3042 }
3043 }
3044 else
3045 eap->line2 = lnum;
3046 eap->addr_count++;
3047
3048 if (*eap->cmd == ';')
3049 {
3050 if (!eap->skip)
3051 {
3052 curwin->w_cursor.lnum = eap->line2;
3053 // don't leave the cursor on an illegal line or column
3054 check_cursor();
3055 }
3056 }
3057 else if (*eap->cmd != ',')
3058 break;
3059 ++eap->cmd;
3060 }
3061
3062 // One address given: set start and end lines.
3063 if (eap->addr_count == 1)
3064 {
3065 eap->line1 = eap->line2;
3066 // ... but only implicit: really no address given
3067 if (lnum == MAXLNUM)
3068 eap->addr_count = 0;
3069 }
3070 return OK;
3071}
3072
3073/*
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00003074 * Check for an Ex command with optional tail.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003075 * If there is a match advance "pp" to the argument and return TRUE.
3076 */
Bram Moolenaarc5a1e802005-01-11 21:21:40 +00003077 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003078checkforcmd(
3079 char_u **pp, /* start of command */
3080 char *cmd, /* name of command */
3081 int len) /* required length */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003082{
3083 int i;
3084
3085 for (i = 0; cmd[i] != NUL; ++i)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003086 if (((char_u *)cmd)[i] != (*pp)[i])
Bram Moolenaar071d4272004-06-13 20:20:40 +00003087 break;
3088 if (i >= len && !isalpha((*pp)[i]))
3089 {
3090 *pp = skipwhite(*pp + i);
3091 return TRUE;
3092 }
3093 return FALSE;
3094}
3095
3096/*
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003097 * Append "cmd" to the error message in IObuff.
3098 * Takes care of limiting the length and handling 0xa0, which would be
3099 * invisible otherwise.
3100 */
3101 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003102append_command(char_u *cmd)
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003103{
3104 char_u *s = cmd;
3105 char_u *d;
3106
3107 STRCAT(IObuff, ": ");
3108 d = IObuff + STRLEN(IObuff);
3109 while (*s != NUL && d - IObuff < IOSIZE - 7)
3110 {
Bram Moolenaar13505972019-01-24 15:04:48 +01003111 if (enc_utf8 ? (s[0] == 0xc2 && s[1] == 0xa0) : *s == 0xa0)
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003112 {
Bram Moolenaar13505972019-01-24 15:04:48 +01003113 s += enc_utf8 ? 2 : 1;
Bram Moolenaara6f4d612011-09-21 19:10:46 +02003114 STRCPY(d, "<a0>");
3115 d += 4;
3116 }
3117 else
3118 MB_COPY_CHAR(s, d);
3119 }
3120 *d = NUL;
3121}
3122
3123/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003124 * Find an Ex command by its name, either built-in or user.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003125 * Start of the name can be found at eap->cmd.
Bram Moolenaar25190db2019-05-04 15:05:28 +02003126 * Sets eap->cmdidx and returns a pointer to char after the command name.
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003127 * "full" is set to TRUE if the whole command name matched.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003128 * Returns NULL for an ambiguous user command.
3129 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003130 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003131find_command(exarg_T *eap, int *full UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003132{
3133 int len;
3134 char_u *p;
Bram Moolenaardf177f62005-02-22 08:39:57 +00003135 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003136
3137 /*
3138 * Isolate the command and search for it in the command table.
Bram Moolenaar81870892007-11-11 18:17:28 +00003139 * Exceptions:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003140 * - the 'k' command can directly be followed by any character.
3141 * - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01003142 * but :sre[wind] is another command, as are :scr[iptnames],
Bram Moolenaar071d4272004-06-13 20:20:40 +00003143 * :scs[cope], :sim[alt], :sig[ns] and :sil[ent].
Bram Moolenaardf177f62005-02-22 08:39:57 +00003144 * - the "d" command can directly be followed by 'l' or 'p' flag.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003145 */
3146 p = eap->cmd;
3147 if (*p == 'k')
3148 {
3149 eap->cmdidx = CMD_k;
3150 ++p;
3151 }
3152 else if (p[0] == 's'
Bram Moolenaar204b93f2015-08-04 22:02:51 +02003153 && ((p[1] == 'c' && (p[2] == NUL || (p[2] != 's' && p[2] != 'r'
3154 && (p[3] == NUL || (p[3] != 'i' && p[4] != 'p')))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003155 || p[1] == 'g'
3156 || (p[1] == 'i' && p[2] != 'm' && p[2] != 'l' && p[2] != 'g')
3157 || p[1] == 'I'
3158 || (p[1] == 'r' && p[2] != 'e')))
3159 {
3160 eap->cmdidx = CMD_substitute;
3161 ++p;
3162 }
3163 else
3164 {
3165 while (ASCII_ISALPHA(*p))
3166 ++p;
Bram Moolenaarb6590522010-07-21 16:00:43 +02003167 /* for python 3.x support ":py3", ":python3", ":py3file", etc. */
Bram Moolenaar55d5c032010-07-17 23:52:29 +02003168 if (eap->cmd[0] == 'p' && eap->cmd[1] == 'y')
Bram Moolenaarb6590522010-07-21 16:00:43 +02003169 while (ASCII_ISALNUM(*p))
3170 ++p;
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02003171
Bram Moolenaar071d4272004-06-13 20:20:40 +00003172 /* check for non-alpha command */
3173 if (p == eap->cmd && vim_strchr((char_u *)"@*!=><&~#", *p) != NULL)
3174 ++p;
3175 len = (int)(p - eap->cmd);
Bram Moolenaardf177f62005-02-22 08:39:57 +00003176 if (*eap->cmd == 'd' && (p[-1] == 'l' || p[-1] == 'p'))
3177 {
3178 /* Check for ":dl", ":dell", etc. to ":deletel": that's
3179 * :delete with the 'l' flag. Same for 'p'. */
3180 for (i = 0; i < len; ++i)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00003181 if (eap->cmd[i] != ((char_u *)"delete")[i])
Bram Moolenaardf177f62005-02-22 08:39:57 +00003182 break;
3183 if (i == len - 1)
3184 {
3185 --len;
3186 if (p[-1] == 'l')
3187 eap->flags |= EXFLAG_LIST;
3188 else
3189 eap->flags |= EXFLAG_PRINT;
3190 }
3191 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003192
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003193 if (ASCII_ISLOWER(eap->cmd[0]))
3194 {
Bram Moolenaar6c0c1e82017-03-25 15:07:43 +01003195 int c1 = eap->cmd[0];
3196 int c2 = eap->cmd[1];
3197
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003198 if (command_count != (int)CMD_SIZE)
3199 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003200 iemsg(_("E943: Command table needs to be updated, run 'make cmdidxs'"));
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003201 getout(1);
3202 }
3203
3204 /* Use a precomputed index for fast look-up in cmdnames[]
3205 * taking into account the first 2 letters of eap->cmd. */
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003206 eap->cmdidx = cmdidxs1[CharOrdLow(c1)];
3207 if (ASCII_ISLOWER(c2))
3208 eap->cmdidx += cmdidxs2[CharOrdLow(c1)][CharOrdLow(c2)];
3209 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003210 else
Bram Moolenaare5e0fbc2017-03-25 14:51:01 +01003211 eap->cmdidx = CMD_bang;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003212
3213 for ( ; (int)eap->cmdidx < (int)CMD_SIZE;
3214 eap->cmdidx = (cmdidx_T)((int)eap->cmdidx + 1))
3215 if (STRNCMP(cmdnames[(int)eap->cmdidx].cmd_name, (char *)eap->cmd,
3216 (size_t)len) == 0)
3217 {
3218#ifdef FEAT_EVAL
3219 if (full != NULL
3220 && cmdnames[(int)eap->cmdidx].cmd_name[len] == NUL)
3221 *full = TRUE;
3222#endif
3223 break;
3224 }
3225
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003226 // Look for a user defined command as a last resort. Let ":Print" be
3227 // overruled by a user defined command.
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01003228 if ((eap->cmdidx == CMD_SIZE || eap->cmdidx == CMD_Print)
3229 && *eap->cmd >= 'A' && *eap->cmd <= 'Z')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003230 {
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003231 // User defined commands may contain digits.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003232 while (ASCII_ISALNUM(*p))
3233 ++p;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003234 p = find_ucmd(eap, p, full, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003235 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003236 if (p == eap->cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003237 eap->cmdidx = CMD_SIZE;
3238 }
3239
3240 return p;
3241}
3242
3243#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003244static struct cmdmod
3245{
3246 char *name;
3247 int minlen;
3248 int has_count; /* :123verbose :3tab */
3249} cmdmods[] = {
3250 {"aboveleft", 3, FALSE},
3251 {"belowright", 3, FALSE},
3252 {"botright", 2, FALSE},
3253 {"browse", 3, FALSE},
3254 {"confirm", 4, FALSE},
Bram Moolenaar7b668e82016-08-23 23:51:21 +02003255 {"filter", 4, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003256 {"hide", 3, FALSE},
3257 {"keepalt", 5, FALSE},
3258 {"keepjumps", 5, FALSE},
3259 {"keepmarks", 3, FALSE},
Bram Moolenaara939e432013-11-09 05:30:26 +01003260 {"keeppatterns", 5, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003261 {"leftabove", 5, FALSE},
3262 {"lockmarks", 3, FALSE},
Bram Moolenaarca9f9582008-09-18 10:44:28 +00003263 {"noautocmd", 3, FALSE},
Bram Moolenaar5803ae62014-03-23 16:04:02 +01003264 {"noswapfile", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003265 {"rightbelow", 6, FALSE},
3266 {"sandbox", 3, FALSE},
3267 {"silent", 3, FALSE},
3268 {"tab", 3, TRUE},
3269 {"topleft", 2, FALSE},
Bram Moolenaar8e258a42009-07-09 13:55:43 +00003270 {"unsilent", 3, FALSE},
Bram Moolenaared53fb92007-11-24 20:50:24 +00003271 {"verbose", 4, TRUE},
3272 {"vertical", 4, FALSE},
3273};
3274
3275/*
3276 * Return length of a command modifier (including optional count).
3277 * Return zero when it's not a modifier.
3278 */
3279 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003280modifier_len(char_u *cmd)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003281{
3282 int i, j;
3283 char_u *p = cmd;
3284
3285 if (VIM_ISDIGIT(*cmd))
3286 p = skipwhite(skipdigits(cmd));
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00003287 for (i = 0; i < (int)(sizeof(cmdmods) / sizeof(struct cmdmod)); ++i)
Bram Moolenaared53fb92007-11-24 20:50:24 +00003288 {
3289 for (j = 0; p[j] != NUL; ++j)
3290 if (p[j] != cmdmods[i].name[j])
3291 break;
Bram Moolenaar2d473ab2013-06-12 17:12:24 +02003292 if (!ASCII_ISALPHA(p[j]) && j >= cmdmods[i].minlen
Bram Moolenaared53fb92007-11-24 20:50:24 +00003293 && (p == cmd || cmdmods[i].has_count))
Bram Moolenaarcb4cef22008-03-16 15:04:34 +00003294 return j + (int)(p - cmd);
Bram Moolenaared53fb92007-11-24 20:50:24 +00003295 }
3296 return 0;
3297}
3298
Bram Moolenaar071d4272004-06-13 20:20:40 +00003299/*
3300 * Return > 0 if an Ex command "name" exists.
3301 * Return 2 if there is an exact match.
3302 * Return 3 if there is an ambiguous match.
3303 */
3304 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003305cmd_exists(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003306{
3307 exarg_T ea;
3308 int full = FALSE;
3309 int i;
3310 int j;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003311 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003312
3313 /* Check command modifiers. */
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00003314 for (i = 0; i < (int)(sizeof(cmdmods) / sizeof(struct cmdmod)); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003315 {
3316 for (j = 0; name[j] != NUL; ++j)
3317 if (name[j] != cmdmods[i].name[j])
3318 break;
3319 if (name[j] == NUL && j >= cmdmods[i].minlen)
3320 return (cmdmods[i].name[j] == NUL ? 2 : 1);
3321 }
3322
Bram Moolenaara9587612006-05-04 21:47:50 +00003323 /* Check built-in commands and user defined commands.
3324 * For ":2match" and ":3match" we need to skip the number. */
3325 ea.cmd = (*name == '2' || *name == '3') ? name + 1 : name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003326 ea.cmdidx = (cmdidx_T)0;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003327 p = find_command(&ea, &full);
3328 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003329 return 3;
Bram Moolenaara9587612006-05-04 21:47:50 +00003330 if (vim_isdigit(*name) && ea.cmdidx != CMD_match)
3331 return 0;
Bram Moolenaarf3a67882006-05-05 21:09:41 +00003332 if (*skipwhite(p) != NUL)
3333 return 0; /* trailing garbage */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003334 return (ea.cmdidx == CMD_SIZE ? 0 : (full ? 2 : 1));
3335}
3336#endif
3337
3338/*
3339 * This is all pretty much copied from do_one_cmd(), with all the extra stuff
3340 * we don't need/want deleted. Maybe this could be done better if we didn't
3341 * repeat all this stuff. The only problem is that they may not stay
3342 * perfectly compatible with each other, but then the command line syntax
3343 * probably won't change that much -- webb.
3344 */
3345 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003346set_one_cmd_context(
3347 expand_T *xp,
3348 char_u *buff) /* buffer for command string */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003349{
3350 char_u *p;
3351 char_u *cmd, *arg;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003352 int len = 0;
3353 exarg_T ea;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003354#ifdef FEAT_CMDL_COMPL
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003355 int compl = EXPAND_NOTHING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003356 int delim;
3357#endif
3358 int forceit = FALSE;
3359 int usefilter = FALSE; /* filter instead of file name */
3360
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003361 ExpandInit(xp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003362 xp->xp_pattern = buff;
3363 xp->xp_context = EXPAND_COMMANDS; /* Default until we get past command */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003364 ea.argt = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003365
3366/*
Bram Moolenaar1c40a662014-11-27 16:38:11 +01003367 * 1. skip comment lines and leading space, colons or bars
Bram Moolenaar071d4272004-06-13 20:20:40 +00003368 */
3369 for (cmd = buff; vim_strchr((char_u *)" \t:|", *cmd) != NULL; cmd++)
3370 ;
3371 xp->xp_pattern = cmd;
3372
3373 if (*cmd == NUL)
3374 return NULL;
3375 if (*cmd == '"') /* ignore comment lines */
3376 {
3377 xp->xp_context = EXPAND_NOTHING;
3378 return NULL;
3379 }
3380
3381/*
Bram Moolenaar1c40a662014-11-27 16:38:11 +01003382 * 3. Skip over the range to find the command.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003383 */
3384 cmd = skip_range(cmd, &xp->xp_context);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003385 xp->xp_pattern = cmd;
3386 if (*cmd == NUL)
3387 return NULL;
3388 if (*cmd == '"')
3389 {
3390 xp->xp_context = EXPAND_NOTHING;
3391 return NULL;
3392 }
3393
3394 if (*cmd == '|' || *cmd == '\n')
3395 return cmd + 1; /* There's another command */
3396
3397 /*
3398 * Isolate the command and search for it in the command table.
3399 * Exceptions:
3400 * - the 'k' command can directly be followed by any character, but
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003401 * do accept "keepmarks", "keepalt" and "keepjumps".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003402 * - the 's' command can be followed directly by 'c', 'g', 'i', 'I' or 'r'
3403 */
3404 if (*cmd == 'k' && cmd[1] != 'e')
3405 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003406 ea.cmdidx = CMD_k;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003407 p = cmd + 1;
3408 }
3409 else
3410 {
3411 p = cmd;
3412 while (ASCII_ISALPHA(*p) || *p == '*') /* Allow * wild card */
3413 ++p;
Bram Moolenaar23d1b622015-10-13 19:18:04 +02003414 /* a user command may contain digits */
3415 if (ASCII_ISUPPER(cmd[0]))
3416 while (ASCII_ISALNUM(*p) || *p == '*')
3417 ++p;
Bram Moolenaar9f5d6002013-06-02 19:22:13 +02003418 /* for python 3.x: ":py3*" commands completion */
3419 if (cmd[0] == 'p' && cmd[1] == 'y' && p == cmd + 2 && *p == '3')
Bram Moolenaar893b2d72013-12-11 17:44:38 +01003420 {
Bram Moolenaar9f5d6002013-06-02 19:22:13 +02003421 ++p;
Bram Moolenaar893b2d72013-12-11 17:44:38 +01003422 while (ASCII_ISALPHA(*p) || *p == '*')
3423 ++p;
3424 }
Bram Moolenaar23d1b622015-10-13 19:18:04 +02003425 /* check for non-alpha command */
3426 if (p == cmd && vim_strchr((char_u *)"@*!=><&~#", *p) != NULL)
3427 ++p;
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003428 len = (int)(p - cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003429
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003430 if (len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003431 {
3432 xp->xp_context = EXPAND_UNSUCCESSFUL;
3433 return NULL;
3434 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003435 for (ea.cmdidx = (cmdidx_T)0; (int)ea.cmdidx < (int)CMD_SIZE;
Bram Moolenaar42b4dda2010-03-02 15:56:05 +01003436 ea.cmdidx = (cmdidx_T)((int)ea.cmdidx + 1))
3437 if (STRNCMP(cmdnames[(int)ea.cmdidx].cmd_name, cmd,
3438 (size_t)len) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003439 break;
3440
Bram Moolenaar071d4272004-06-13 20:20:40 +00003441 if (cmd[0] >= 'A' && cmd[0] <= 'Z')
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003442 while (ASCII_ISALNUM(*p) || *p == '*') // Allow * wild card
Bram Moolenaar071d4272004-06-13 20:20:40 +00003443 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003444 }
3445
3446 /*
3447 * If the cursor is touching the command, and it ends in an alpha-numeric
3448 * character, complete the command name.
3449 */
3450 if (*p == NUL && ASCII_ISALNUM(p[-1]))
3451 return NULL;
3452
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003453 if (ea.cmdidx == CMD_SIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003454 {
3455 if (*cmd == 's' && vim_strchr((char_u *)"cgriI", cmd[1]) != NULL)
3456 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003457 ea.cmdidx = CMD_substitute;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003458 p = cmd + 1;
3459 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003460 else if (cmd[0] >= 'A' && cmd[0] <= 'Z')
3461 {
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003462 ea.cmd = cmd;
3463 p = find_ucmd(&ea, p, NULL, xp,
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003464#if defined(FEAT_CMDL_COMPL)
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003465 &compl
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003466#else
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003467 NULL
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003468#endif
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003469 );
Bram Moolenaarebefac62005-12-28 22:39:57 +00003470 if (p == NULL)
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003471 ea.cmdidx = CMD_SIZE; // ambiguous user command
Bram Moolenaar071d4272004-06-13 20:20:40 +00003472 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003473 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003474 if (ea.cmdidx == CMD_SIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003475 {
3476 /* Not still touching the command and it was an illegal one */
3477 xp->xp_context = EXPAND_UNSUCCESSFUL;
3478 return NULL;
3479 }
3480
3481 xp->xp_context = EXPAND_NOTHING; /* Default now that we're past command */
3482
3483 if (*p == '!') /* forced commands */
3484 {
3485 forceit = TRUE;
3486 ++p;
3487 }
3488
3489/*
Bram Moolenaar1c40a662014-11-27 16:38:11 +01003490 * 6. parse arguments
Bram Moolenaar071d4272004-06-13 20:20:40 +00003491 */
Bram Moolenaar958636c2014-10-21 20:01:58 +02003492 if (!IS_USER_CMDIDX(ea.cmdidx))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003493 ea.argt = (long)cmdnames[(int)ea.cmdidx].cmd_argt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003494
3495 arg = skipwhite(p);
3496
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003497 if (ea.cmdidx == CMD_write || ea.cmdidx == CMD_update)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003498 {
3499 if (*arg == '>') /* append */
3500 {
3501 if (*++arg == '>')
3502 ++arg;
3503 arg = skipwhite(arg);
3504 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003505 else if (*arg == '!' && ea.cmdidx == CMD_write) /* :w !filter */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003506 {
3507 ++arg;
3508 usefilter = TRUE;
3509 }
3510 }
3511
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003512 if (ea.cmdidx == CMD_read)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003513 {
3514 usefilter = forceit; /* :r! filter if forced */
3515 if (*arg == '!') /* :r !filter */
3516 {
3517 ++arg;
3518 usefilter = TRUE;
3519 }
3520 }
3521
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003522 if (ea.cmdidx == CMD_lshift || ea.cmdidx == CMD_rshift)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003523 {
3524 while (*arg == *cmd) /* allow any number of '>' or '<' */
3525 ++arg;
3526 arg = skipwhite(arg);
3527 }
3528
3529 /* Does command allow "+command"? */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003530 if ((ea.argt & EDITCMD) && !usefilter && *arg == '+')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003531 {
3532 /* Check if we're in the +command */
3533 p = arg + 1;
3534 arg = skip_cmd_arg(arg, FALSE);
3535
3536 /* Still touching the command after '+'? */
3537 if (*arg == NUL)
3538 return p;
3539
3540 /* Skip space(s) after +command to get to the real argument */
3541 arg = skipwhite(arg);
3542 }
3543
3544 /*
3545 * Check for '|' to separate commands and '"' to start comments.
3546 * Don't do this for ":read !cmd" and ":write !cmd".
3547 */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003548 if ((ea.argt & TRLBAR) && !usefilter)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003549 {
3550 p = arg;
3551 /* ":redir @" is not the start of a comment */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003552 if (ea.cmdidx == CMD_redir && p[0] == '@' && p[1] == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003553 p += 2;
3554 while (*p)
3555 {
3556 if (*p == Ctrl_V)
3557 {
3558 if (p[1] != NUL)
3559 ++p;
3560 }
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003561 else if ( (*p == '"' && !(ea.argt & NOTRLCOM))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003562 || *p == '|' || *p == '\n')
3563 {
3564 if (*(p - 1) != '\\')
3565 {
3566 if (*p == '|' || *p == '\n')
3567 return p + 1;
3568 return NULL; /* It's a comment */
3569 }
3570 }
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003571 MB_PTR_ADV(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003572 }
3573 }
3574
3575 /* no arguments allowed */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003576 if (!(ea.argt & EXTRA) && *arg != NUL &&
Bram Moolenaar071d4272004-06-13 20:20:40 +00003577 vim_strchr((char_u *)"|\"", *arg) == NULL)
3578 return NULL;
3579
3580 /* Find start of last argument (argument just before cursor): */
Bram Moolenaar848f8762012-07-25 17:22:23 +02003581 p = buff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003582 xp->xp_pattern = p;
Bram Moolenaar09168a72012-08-02 21:24:42 +02003583 len = (int)STRLEN(buff);
Bram Moolenaar848f8762012-07-25 17:22:23 +02003584 while (*p && p < buff + len)
3585 {
3586 if (*p == ' ' || *p == TAB)
3587 {
3588 /* argument starts after a space */
3589 xp->xp_pattern = ++p;
3590 }
3591 else
3592 {
3593 if (*p == '\\' && *(p + 1) != NUL)
3594 ++p; /* skip over escaped character */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003595 MB_PTR_ADV(p);
Bram Moolenaar848f8762012-07-25 17:22:23 +02003596 }
3597 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003598
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003599 if (ea.argt & XFILE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003600 {
Bram Moolenaar6529c102007-08-18 15:47:34 +00003601 int c;
3602 int in_quote = FALSE;
3603 char_u *bow = NULL; /* Beginning of word */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003604
3605 /*
3606 * Allow spaces within back-quotes to count as part of the argument
3607 * being expanded.
3608 */
3609 xp->xp_pattern = skipwhite(arg);
Bram Moolenaar6529c102007-08-18 15:47:34 +00003610 p = xp->xp_pattern;
3611 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003612 {
Bram Moolenaar6529c102007-08-18 15:47:34 +00003613 if (has_mbyte)
3614 c = mb_ptr2char(p);
3615 else
Bram Moolenaar6529c102007-08-18 15:47:34 +00003616 c = *p;
3617 if (c == '\\' && p[1] != NUL)
3618 ++p;
3619 else if (c == '`')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003620 {
3621 if (!in_quote)
3622 {
3623 xp->xp_pattern = p;
3624 bow = p + 1;
3625 }
3626 in_quote = !in_quote;
3627 }
Bram Moolenaar332fa0c2008-01-13 16:12:10 +00003628 /* An argument can contain just about everything, except
3629 * characters that end the command and white space. */
Bram Moolenaar1c465442017-03-12 20:10:05 +01003630 else if (c == '|' || c == '\n' || c == '"' || (VIM_ISWHITE(c)
Bram Moolenaar6529c102007-08-18 15:47:34 +00003631#ifdef SPACE_IN_FILENAME
Bram Moolenaar332fa0c2008-01-13 16:12:10 +00003632 && (!(ea.argt & NOSPC) || usefilter)
Bram Moolenaar6529c102007-08-18 15:47:34 +00003633#endif
Bram Moolenaar332fa0c2008-01-13 16:12:10 +00003634 ))
Bram Moolenaar6529c102007-08-18 15:47:34 +00003635 {
Bram Moolenaarcf5a5b82008-02-26 20:30:12 +00003636 len = 0; /* avoid getting stuck when space is in 'isfname' */
Bram Moolenaar6529c102007-08-18 15:47:34 +00003637 while (*p != NUL)
3638 {
Bram Moolenaar6529c102007-08-18 15:47:34 +00003639 if (has_mbyte)
3640 c = mb_ptr2char(p);
3641 else
Bram Moolenaar6529c102007-08-18 15:47:34 +00003642 c = *p;
Bram Moolenaardd87969c2007-08-21 13:07:12 +00003643 if (c == '`' || vim_isfilec_or_wc(c))
Bram Moolenaar6529c102007-08-18 15:47:34 +00003644 break;
Bram Moolenaar6529c102007-08-18 15:47:34 +00003645 if (has_mbyte)
3646 len = (*mb_ptr2len)(p);
3647 else
Bram Moolenaar6529c102007-08-18 15:47:34 +00003648 len = 1;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003649 MB_PTR_ADV(p);
Bram Moolenaar6529c102007-08-18 15:47:34 +00003650 }
3651 if (in_quote)
3652 bow = p;
3653 else
3654 xp->xp_pattern = p;
3655 p -= len;
3656 }
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003657 MB_PTR_ADV(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003658 }
3659
3660 /*
3661 * If we are still inside the quotes, and we passed a space, just
3662 * expand from there.
3663 */
3664 if (bow != NULL && in_quote)
3665 xp->xp_pattern = bow;
3666 xp->xp_context = EXPAND_FILES;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003667
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003668 /* For a shell command more chars need to be escaped. */
Bram Moolenaar67883b42017-07-27 22:57:00 +02003669 if (usefilter || ea.cmdidx == CMD_bang || ea.cmdidx == CMD_terminal)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003670 {
Bram Moolenaarc0cba4d2010-08-07 17:07:21 +02003671#ifndef BACKSLASH_IN_FILENAME
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00003672 xp->xp_shell = TRUE;
Bram Moolenaarc0cba4d2010-08-07 17:07:21 +02003673#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003674 /* When still after the command name expand executables. */
3675 if (xp->xp_pattern == skipwhite(arg))
Bram Moolenaar5c5b0942007-05-06 12:07:59 +00003676 xp->xp_context = EXPAND_SHELLCMD;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003677 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003678
3679 /* Check for environment variable */
3680 if (*xp->xp_pattern == '$'
Bram Moolenaar48e330a2016-02-23 14:53:34 +01003681#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003682 || *xp->xp_pattern == '%'
3683#endif
3684 )
3685 {
3686 for (p = xp->xp_pattern + 1; *p != NUL; ++p)
3687 if (!vim_isIDc(*p))
3688 break;
3689 if (*p == NUL)
3690 {
3691 xp->xp_context = EXPAND_ENV_VARS;
3692 ++xp->xp_pattern;
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003693#if defined(FEAT_CMDL_COMPL)
Bram Moolenaar21cf8232004-07-16 20:18:37 +00003694 /* Avoid that the assignment uses EXPAND_FILES again. */
Bram Moolenaara466c992005-07-09 21:03:22 +00003695 if (compl != EXPAND_USER_DEFINED && compl != EXPAND_USER_LIST)
Bram Moolenaar21cf8232004-07-16 20:18:37 +00003696 compl = EXPAND_ENV_VARS;
3697#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003698 }
3699 }
Bram Moolenaar24305862012-08-15 14:05:05 +02003700#if defined(FEAT_CMDL_COMPL)
3701 /* Check for user names */
3702 if (*xp->xp_pattern == '~')
3703 {
3704 for (p = xp->xp_pattern + 1; *p != NUL && *p != '/'; ++p)
3705 ;
3706 /* Complete ~user only if it partially matches a user name.
3707 * A full match ~user<Tab> will be replaced by user's home
3708 * directory i.e. something like ~user<Tab> -> /home/user/ */
3709 if (*p == NUL && p > xp->xp_pattern + 1
Bram Moolenaar6c5d1042018-07-07 16:41:13 +02003710 && match_user(xp->xp_pattern + 1) >= 1)
Bram Moolenaar24305862012-08-15 14:05:05 +02003711 {
3712 xp->xp_context = EXPAND_USER;
3713 ++xp->xp_pattern;
3714 }
3715 }
3716#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003717 }
3718
3719/*
Bram Moolenaar1c40a662014-11-27 16:38:11 +01003720 * 6. Switch on command name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003721 */
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003722 switch (ea.cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003723 {
Bram Moolenaarcc448b32010-07-14 16:52:17 +02003724 case CMD_find:
3725 case CMD_sfind:
3726 case CMD_tabfind:
Bram Moolenaarc24b6972010-08-16 22:34:29 +02003727 if (xp->xp_context == EXPAND_FILES)
3728 xp->xp_context = EXPAND_FILES_IN_PATH;
Bram Moolenaarcc448b32010-07-14 16:52:17 +02003729 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003730 case CMD_cd:
3731 case CMD_chdir:
Bram Moolenaar00aa0692019-04-27 20:37:57 +02003732 case CMD_tcd:
3733 case CMD_tchdir:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003734 case CMD_lcd:
3735 case CMD_lchdir:
3736 if (xp->xp_context == EXPAND_FILES)
3737 xp->xp_context = EXPAND_DIRECTORIES;
3738 break;
3739 case CMD_help:
3740 xp->xp_context = EXPAND_HELP;
3741 xp->xp_pattern = arg;
3742 break;
3743
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003744 /* Command modifiers: return the argument.
3745 * Also for commands with an argument that is a command. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003746 case CMD_aboveleft:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003747 case CMD_argdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003748 case CMD_belowright:
3749 case CMD_botright:
3750 case CMD_browse:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003751 case CMD_bufdo:
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003752 case CMD_cdo:
3753 case CMD_cfdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003754 case CMD_confirm:
Bram Moolenaardf177f62005-02-22 08:39:57 +00003755 case CMD_debug:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003756 case CMD_folddoclosed:
3757 case CMD_folddoopen:
3758 case CMD_hide:
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003759 case CMD_keepalt:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003760 case CMD_keepjumps:
3761 case CMD_keepmarks:
Bram Moolenaara939e432013-11-09 05:30:26 +01003762 case CMD_keeppatterns:
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003763 case CMD_ldo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003764 case CMD_leftabove:
Bram Moolenaaraa23b372015-09-08 18:46:31 +02003765 case CMD_lfdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003766 case CMD_lockmarks:
Bram Moolenaar5803ae62014-03-23 16:04:02 +01003767 case CMD_noautocmd:
3768 case CMD_noswapfile:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003769 case CMD_rightbelow:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003770 case CMD_sandbox:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003771 case CMD_silent:
Bram Moolenaara226a6d2006-02-26 23:59:20 +00003772 case CMD_tab:
Bram Moolenaar70baa402013-06-16 16:14:03 +02003773 case CMD_tabdo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003774 case CMD_topleft:
3775 case CMD_verbose:
3776 case CMD_vertical:
Bram Moolenaardf7b1ff2005-03-11 22:40:50 +00003777 case CMD_windo:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003778 return arg;
3779
Bram Moolenaar7069bf12017-01-07 20:39:53 +01003780 case CMD_filter:
3781 if (*arg != NUL)
3782 arg = skip_vimgrep_pat(arg, NULL, NULL);
3783 if (arg == NULL || *arg == NUL)
3784 {
3785 xp->xp_context = EXPAND_NOTHING;
3786 return NULL;
3787 }
3788 return skipwhite(arg);
3789
Bram Moolenaar4f688582007-07-24 12:34:30 +00003790#ifdef FEAT_CMDL_COMPL
3791# ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar071d4272004-06-13 20:20:40 +00003792 case CMD_match:
3793 if (*arg == NUL || !ends_excmd(*arg))
3794 {
Bram Moolenaar4f688582007-07-24 12:34:30 +00003795 /* also complete "None" */
3796 set_context_in_echohl_cmd(xp, arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003797 arg = skipwhite(skiptowhite(arg));
3798 if (*arg != NUL)
3799 {
3800 xp->xp_context = EXPAND_NOTHING;
3801 arg = skip_regexp(arg + 1, *arg, p_magic, NULL);
3802 }
3803 }
3804 return find_nextcmd(arg);
Bram Moolenaar4f688582007-07-24 12:34:30 +00003805# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003806
Bram Moolenaar071d4272004-06-13 20:20:40 +00003807/*
3808 * All completion for the +cmdline_compl feature goes here.
3809 */
3810
Bram Moolenaar071d4272004-06-13 20:20:40 +00003811 case CMD_command:
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003812 return set_context_in_user_cmd(xp, arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003813
3814 case CMD_delcommand:
3815 xp->xp_context = EXPAND_USER_COMMANDS;
3816 xp->xp_pattern = arg;
3817 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003818
3819 case CMD_global:
3820 case CMD_vglobal:
3821 delim = *arg; /* get the delimiter */
3822 if (delim)
3823 ++arg; /* skip delimiter if there is one */
3824
3825 while (arg[0] != NUL && arg[0] != delim)
3826 {
3827 if (arg[0] == '\\' && arg[1] != NUL)
3828 ++arg;
3829 ++arg;
3830 }
3831 if (arg[0] != NUL)
3832 return arg + 1;
3833 break;
3834 case CMD_and:
3835 case CMD_substitute:
3836 delim = *arg;
3837 if (delim)
3838 {
3839 /* skip "from" part */
3840 ++arg;
3841 arg = skip_regexp(arg, delim, p_magic, NULL);
3842 }
3843 /* skip "to" part */
3844 while (arg[0] != NUL && arg[0] != delim)
3845 {
3846 if (arg[0] == '\\' && arg[1] != NUL)
3847 ++arg;
3848 ++arg;
3849 }
3850 if (arg[0] != NUL) /* skip delimiter */
3851 ++arg;
3852 while (arg[0] && vim_strchr((char_u *)"|\"#", arg[0]) == NULL)
3853 ++arg;
3854 if (arg[0] != NUL)
3855 return arg;
3856 break;
3857 case CMD_isearch:
3858 case CMD_dsearch:
3859 case CMD_ilist:
3860 case CMD_dlist:
3861 case CMD_ijump:
3862 case CMD_psearch:
3863 case CMD_djump:
3864 case CMD_isplit:
3865 case CMD_dsplit:
3866 arg = skipwhite(skipdigits(arg)); /* skip count */
3867 if (*arg == '/') /* Match regexp, not just whole words */
3868 {
3869 for (++arg; *arg && *arg != '/'; arg++)
3870 if (*arg == '\\' && arg[1] != NUL)
3871 arg++;
3872 if (*arg)
3873 {
3874 arg = skipwhite(arg + 1);
3875
3876 /* Check for trailing illegal characters */
3877 if (*arg && vim_strchr((char_u *)"|\"\n", *arg) == NULL)
3878 xp->xp_context = EXPAND_NOTHING;
3879 else
3880 return arg;
3881 }
3882 }
3883 break;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01003884
Bram Moolenaar071d4272004-06-13 20:20:40 +00003885 case CMD_autocmd:
3886 return set_context_in_autocmd(xp, arg, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003887 case CMD_doautocmd:
Bram Moolenaar73a9d7b2008-11-06 16:16:44 +00003888 case CMD_doautoall:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003889 return set_context_in_autocmd(xp, arg, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003890 case CMD_set:
3891 set_context_in_set_cmd(xp, arg, 0);
3892 break;
3893 case CMD_setglobal:
3894 set_context_in_set_cmd(xp, arg, OPT_GLOBAL);
3895 break;
3896 case CMD_setlocal:
3897 set_context_in_set_cmd(xp, arg, OPT_LOCAL);
3898 break;
3899 case CMD_tag:
3900 case CMD_stag:
3901 case CMD_ptag:
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00003902 case CMD_ltag:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003903 case CMD_tselect:
3904 case CMD_stselect:
3905 case CMD_ptselect:
3906 case CMD_tjump:
3907 case CMD_stjump:
3908 case CMD_ptjump:
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +00003909 if (*p_wop != NUL)
3910 xp->xp_context = EXPAND_TAGS_LISTFILES;
3911 else
3912 xp->xp_context = EXPAND_TAGS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003913 xp->xp_pattern = arg;
3914 break;
3915 case CMD_augroup:
3916 xp->xp_context = EXPAND_AUGROUP;
3917 xp->xp_pattern = arg;
3918 break;
3919#ifdef FEAT_SYN_HL
3920 case CMD_syntax:
3921 set_context_in_syntax_cmd(xp, arg);
3922 break;
3923#endif
3924#ifdef FEAT_EVAL
3925 case CMD_let:
3926 case CMD_if:
3927 case CMD_elseif:
3928 case CMD_while:
Bram Moolenaarb32ce2d2005-01-05 22:07:01 +00003929 case CMD_for:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003930 case CMD_echo:
3931 case CMD_echon:
3932 case CMD_execute:
3933 case CMD_echomsg:
3934 case CMD_echoerr:
3935 case CMD_call:
3936 case CMD_return:
Bram Moolenaar2b2207b2017-01-22 16:46:56 +01003937 case CMD_cexpr:
3938 case CMD_caddexpr:
3939 case CMD_cgetexpr:
3940 case CMD_lexpr:
3941 case CMD_laddexpr:
3942 case CMD_lgetexpr:
Bram Moolenaar52b4b552005-03-07 23:00:57 +00003943 set_context_for_expression(xp, arg, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003944 break;
3945
3946 case CMD_unlet:
3947 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
3948 arg = xp->xp_pattern + 1;
Bram Moolenaar19834012018-06-12 17:03:39 +02003949
Bram Moolenaar071d4272004-06-13 20:20:40 +00003950 xp->xp_context = EXPAND_USER_VARS;
3951 xp->xp_pattern = arg;
Bram Moolenaar19834012018-06-12 17:03:39 +02003952
3953 if (*xp->xp_pattern == '$')
3954 {
3955 xp->xp_context = EXPAND_ENV_VARS;
3956 ++xp->xp_pattern;
3957 }
3958
Bram Moolenaar071d4272004-06-13 20:20:40 +00003959 break;
3960
3961 case CMD_function:
3962 case CMD_delfunction:
3963 xp->xp_context = EXPAND_USER_FUNC;
3964 xp->xp_pattern = arg;
3965 break;
3966
3967 case CMD_echohl:
Bram Moolenaar4f688582007-07-24 12:34:30 +00003968 set_context_in_echohl_cmd(xp, arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003969 break;
3970#endif
3971 case CMD_highlight:
3972 set_context_in_highlight_cmd(xp, arg);
3973 break;
Bram Moolenaarf4580d82009-03-18 11:52:53 +00003974#ifdef FEAT_CSCOPE
3975 case CMD_cscope:
Bram Moolenaar7bfef802009-04-22 14:25:01 +00003976 case CMD_lcscope:
3977 case CMD_scscope:
3978 set_context_in_cscope_cmd(xp, arg, ea.cmdidx);
Bram Moolenaarf4580d82009-03-18 11:52:53 +00003979 break;
3980#endif
Bram Moolenaar3c65e312009-04-29 16:47:23 +00003981#ifdef FEAT_SIGNS
3982 case CMD_sign:
3983 set_context_in_sign_cmd(xp, arg);
3984 break;
3985#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003986 case CMD_bdelete:
3987 case CMD_bwipeout:
3988 case CMD_bunload:
3989 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
3990 arg = xp->xp_pattern + 1;
Bram Moolenaar2f40d122017-10-24 21:49:36 +02003991 /* FALLTHROUGH */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003992 case CMD_buffer:
3993 case CMD_sbuffer:
3994 case CMD_checktime:
3995 xp->xp_context = EXPAND_BUFFERS;
3996 xp->xp_pattern = arg;
3997 break;
Bram Moolenaarac9fb182019-04-27 13:04:13 +02003998
Bram Moolenaar071d4272004-06-13 20:20:40 +00003999 case CMD_USER:
4000 case CMD_USER_BUF:
4001 if (compl != EXPAND_NOTHING)
4002 {
Bram Moolenaarac9fb182019-04-27 13:04:13 +02004003 // XFILE: file names are handled above
Bram Moolenaar52b4b552005-03-07 23:00:57 +00004004 if (!(ea.argt & XFILE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004005 {
Bram Moolenaarac9fb182019-04-27 13:04:13 +02004006#ifdef FEAT_MENU
Bram Moolenaar071d4272004-06-13 20:20:40 +00004007 if (compl == EXPAND_MENUS)
4008 return set_context_in_menu_cmd(xp, cmd, arg, forceit);
Bram Moolenaarac9fb182019-04-27 13:04:13 +02004009#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004010 if (compl == EXPAND_COMMANDS)
4011 return arg;
4012 if (compl == EXPAND_MAPPINGS)
4013 return set_context_in_map_cmd(xp, (char_u *)"map",
4014 arg, forceit, FALSE, FALSE, CMD_map);
Bram Moolenaarac9fb182019-04-27 13:04:13 +02004015 // Find start of last argument.
Bram Moolenaar848f8762012-07-25 17:22:23 +02004016 p = arg;
4017 while (*p)
4018 {
4019 if (*p == ' ')
Bram Moolenaarac9fb182019-04-27 13:04:13 +02004020 // argument starts after a space
Bram Moolenaar848f8762012-07-25 17:22:23 +02004021 arg = p + 1;
Bram Moolenaara07c8312012-07-27 21:12:07 +02004022 else if (*p == '\\' && *(p + 1) != NUL)
Bram Moolenaarac9fb182019-04-27 13:04:13 +02004023 ++p; // skip over escaped character
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004024 MB_PTR_ADV(p);
Bram Moolenaar848f8762012-07-25 17:22:23 +02004025 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004026 xp->xp_pattern = arg;
4027 }
4028 xp->xp_context = compl;
4029 }
4030 break;
Bram Moolenaarac9fb182019-04-27 13:04:13 +02004031
Bram Moolenaar071d4272004-06-13 20:20:40 +00004032 case CMD_map: case CMD_noremap:
4033 case CMD_nmap: case CMD_nnoremap:
4034 case CMD_vmap: case CMD_vnoremap:
4035 case CMD_omap: case CMD_onoremap:
4036 case CMD_imap: case CMD_inoremap:
4037 case CMD_cmap: case CMD_cnoremap:
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004038 case CMD_lmap: case CMD_lnoremap:
Bram Moolenaar09bb33d2013-05-07 05:18:20 +02004039 case CMD_smap: case CMD_snoremap:
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +02004040 case CMD_tmap: case CMD_tnoremap:
Bram Moolenaar09bb33d2013-05-07 05:18:20 +02004041 case CMD_xmap: case CMD_xnoremap:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004042 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004043 FALSE, FALSE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004044 case CMD_unmap:
4045 case CMD_nunmap:
4046 case CMD_vunmap:
4047 case CMD_ounmap:
4048 case CMD_iunmap:
4049 case CMD_cunmap:
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004050 case CMD_lunmap:
Bram Moolenaar09bb33d2013-05-07 05:18:20 +02004051 case CMD_sunmap:
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +02004052 case CMD_tunmap:
Bram Moolenaar09bb33d2013-05-07 05:18:20 +02004053 case CMD_xunmap:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004054 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004055 FALSE, TRUE, ea.cmdidx);
Bram Moolenaarcae92dc2017-08-06 15:22:15 +02004056 case CMD_mapclear:
4057 case CMD_nmapclear:
4058 case CMD_vmapclear:
4059 case CMD_omapclear:
4060 case CMD_imapclear:
4061 case CMD_cmapclear:
4062 case CMD_lmapclear:
4063 case CMD_smapclear:
Bram Moolenaar69fbc9e2017-09-14 20:37:57 +02004064 case CMD_tmapclear:
Bram Moolenaarcae92dc2017-08-06 15:22:15 +02004065 case CMD_xmapclear:
4066 xp->xp_context = EXPAND_MAPCLEAR;
4067 xp->xp_pattern = arg;
4068 break;
4069
Bram Moolenaar071d4272004-06-13 20:20:40 +00004070 case CMD_abbreviate: case CMD_noreabbrev:
4071 case CMD_cabbrev: case CMD_cnoreabbrev:
4072 case CMD_iabbrev: case CMD_inoreabbrev:
4073 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004074 TRUE, FALSE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004075 case CMD_unabbreviate:
4076 case CMD_cunabbrev:
4077 case CMD_iunabbrev:
4078 return set_context_in_map_cmd(xp, cmd, arg, forceit,
Bram Moolenaar22b306f2010-07-25 13:50:33 +02004079 TRUE, TRUE, ea.cmdidx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004080#ifdef FEAT_MENU
4081 case CMD_menu: case CMD_noremenu: case CMD_unmenu:
4082 case CMD_amenu: case CMD_anoremenu: case CMD_aunmenu:
4083 case CMD_nmenu: case CMD_nnoremenu: case CMD_nunmenu:
4084 case CMD_vmenu: case CMD_vnoremenu: case CMD_vunmenu:
4085 case CMD_omenu: case CMD_onoremenu: case CMD_ounmenu:
4086 case CMD_imenu: case CMD_inoremenu: case CMD_iunmenu:
4087 case CMD_cmenu: case CMD_cnoremenu: case CMD_cunmenu:
Bram Moolenaar4c5d8152018-10-19 22:36:53 +02004088 case CMD_tlmenu: case CMD_tlnoremenu: case CMD_tlunmenu:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004089 case CMD_tmenu: case CMD_tunmenu:
4090 case CMD_popup: case CMD_tearoff: case CMD_emenu:
4091 return set_context_in_menu_cmd(xp, cmd, arg, forceit);
4092#endif
4093
4094 case CMD_colorscheme:
4095 xp->xp_context = EXPAND_COLORS;
4096 xp->xp_pattern = arg;
4097 break;
4098
4099 case CMD_compiler:
4100 xp->xp_context = EXPAND_COMPILER;
4101 xp->xp_pattern = arg;
4102 break;
4103
Bram Moolenaar883f5d02010-06-21 06:24:34 +02004104 case CMD_ownsyntax:
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02004105 xp->xp_context = EXPAND_OWNSYNTAX;
4106 xp->xp_pattern = arg;
4107 break;
4108
4109 case CMD_setfiletype:
Bram Moolenaar883f5d02010-06-21 06:24:34 +02004110 xp->xp_context = EXPAND_FILETYPE;
4111 xp->xp_pattern = arg;
4112 break;
4113
Bram Moolenaar35ca0e72016-03-05 17:41:49 +01004114 case CMD_packadd:
4115 xp->xp_context = EXPAND_PACKADD;
4116 xp->xp_pattern = arg;
4117 break;
4118
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01004119#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004120 case CMD_language:
Bram Moolenaara660dc82011-05-25 12:51:22 +02004121 p = skiptowhite(arg);
4122 if (*p == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004123 {
4124 xp->xp_context = EXPAND_LANGUAGE;
4125 xp->xp_pattern = arg;
4126 }
4127 else
Bram Moolenaara660dc82011-05-25 12:51:22 +02004128 {
4129 if ( STRNCMP(arg, "messages", p - arg) == 0
4130 || STRNCMP(arg, "ctype", p - arg) == 0
4131 || STRNCMP(arg, "time", p - arg) == 0)
4132 {
4133 xp->xp_context = EXPAND_LOCALES;
4134 xp->xp_pattern = skipwhite(p);
4135 }
4136 else
4137 xp->xp_context = EXPAND_NOTHING;
4138 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004139 break;
4140#endif
Bram Moolenaarf86f26c2010-02-03 15:14:22 +01004141#if defined(FEAT_PROFILE)
4142 case CMD_profile:
4143 set_context_in_profile_cmd(xp, arg);
4144 break;
4145#endif
Bram Moolenaar42b4dda2010-03-02 15:56:05 +01004146 case CMD_behave:
4147 xp->xp_context = EXPAND_BEHAVE;
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02004148 xp->xp_pattern = arg;
Bram Moolenaar42b4dda2010-03-02 15:56:05 +01004149 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004150
Bram Moolenaar9e507ca2016-10-15 15:39:39 +02004151 case CMD_messages:
4152 xp->xp_context = EXPAND_MESSAGES;
4153 xp->xp_pattern = arg;
4154 break;
4155
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02004156#if defined(FEAT_CMDHIST)
4157 case CMD_history:
4158 xp->xp_context = EXPAND_HISTORY;
4159 xp->xp_pattern = arg;
4160 break;
4161#endif
Bram Moolenaarcd9c4622013-06-08 15:24:48 +02004162#if defined(FEAT_PROFILE)
4163 case CMD_syntime:
4164 xp->xp_context = EXPAND_SYNTIME;
4165 xp->xp_pattern = arg;
4166 break;
4167#endif
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02004168
Bram Moolenaarcd43eff2018-03-29 15:55:38 +02004169 case CMD_argdelete:
4170 while ((xp->xp_pattern = vim_strchr(arg, ' ')) != NULL)
4171 arg = xp->xp_pattern + 1;
4172 xp->xp_context = EXPAND_ARGLIST;
4173 xp->xp_pattern = arg;
4174 break;
4175
Bram Moolenaar071d4272004-06-13 20:20:40 +00004176#endif /* FEAT_CMDL_COMPL */
4177
4178 default:
4179 break;
4180 }
4181 return NULL;
4182}
4183
4184/*
Bram Moolenaaree8415b2018-08-10 23:13:12 +02004185 * Skip a range specifier of the form: addr [,addr] [;addr] ..
Bram Moolenaar071d4272004-06-13 20:20:40 +00004186 *
4187 * Backslashed delimiters after / or ? will be skipped, and commands will
4188 * not be expanded between /'s and ?'s or after "'".
4189 *
Bram Moolenaardf177f62005-02-22 08:39:57 +00004190 * Also skip white space and ":" characters.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004191 * Returns the "cmd" pointer advanced to beyond the range.
4192 */
4193 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004194skip_range(
4195 char_u *cmd,
4196 int *ctx) /* pointer to xp_context or NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004197{
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004198 unsigned delim;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004199
Bram Moolenaarcbf20fb2017-02-03 21:19:04 +01004200 while (vim_strchr((char_u *)" \t0123456789.$%'/?-+,;\\", *cmd) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004201 {
Bram Moolenaarcbf20fb2017-02-03 21:19:04 +01004202 if (*cmd == '\\')
4203 {
4204 if (cmd[1] == '?' || cmd[1] == '/' || cmd[1] == '&')
4205 ++cmd;
4206 else
4207 break;
4208 }
4209 else if (*cmd == '\'')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004210 {
4211 if (*++cmd == NUL && ctx != NULL)
4212 *ctx = EXPAND_NOTHING;
4213 }
4214 else if (*cmd == '/' || *cmd == '?')
4215 {
4216 delim = *cmd++;
4217 while (*cmd != NUL && *cmd != delim)
4218 if (*cmd++ == '\\' && *cmd != NUL)
4219 ++cmd;
4220 if (*cmd == NUL && ctx != NULL)
4221 *ctx = EXPAND_NOTHING;
4222 }
4223 if (*cmd != NUL)
4224 ++cmd;
4225 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00004226
4227 /* Skip ":" and white space. */
4228 while (*cmd == ':')
4229 cmd = skipwhite(cmd + 1);
4230
Bram Moolenaar071d4272004-06-13 20:20:40 +00004231 return cmd;
4232}
4233
4234/*
Bram Moolenaar198cb662018-09-06 21:44:17 +02004235 * Get a single EX address.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004236 *
4237 * Set ptr to the next character after the part that was interpreted.
4238 * Set ptr to NULL when an error is encountered.
Bram Moolenaar198cb662018-09-06 21:44:17 +02004239 * This may set the last used search pattern.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004240 *
4241 * Return MAXLNUM when no Ex address was found.
4242 */
4243 static linenr_T
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004244get_address(
4245 exarg_T *eap UNUSED,
4246 char_u **ptr,
Bram Moolenaarb7316892019-05-01 18:08:42 +02004247 cmd_addr_T addr_type_arg,
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02004248 int skip, // only skip the address, don't use it
4249 int silent, // no errors or side effects
4250 int to_other_file, // flag: may jump to other file
4251 int address_count UNUSED) // 1 for first address, >1 after comma
Bram Moolenaar071d4272004-06-13 20:20:40 +00004252{
Bram Moolenaarb7316892019-05-01 18:08:42 +02004253 cmd_addr_T addr_type = addr_type_arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004254 int c;
4255 int i;
4256 long n;
4257 char_u *cmd;
4258 pos_T pos;
4259 pos_T *fp;
4260 linenr_T lnum;
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004261 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004262
4263 cmd = skipwhite(*ptr);
4264 lnum = MAXLNUM;
4265 do
4266 {
4267 switch (*cmd)
4268 {
4269 case '.': /* '.' - Cursor position */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004270 ++cmd;
4271 switch (addr_type)
4272 {
4273 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004274 case ADDR_OTHER:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004275 lnum = curwin->w_cursor.lnum;
4276 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004277 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004278 lnum = CURRENT_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004279 break;
4280 case ADDR_ARGUMENTS:
4281 lnum = curwin->w_arg_idx + 1;
4282 break;
4283 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004284 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004285 lnum = curbuf->b_fnum;
4286 break;
4287 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004288 lnum = CURRENT_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004289 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004290 case ADDR_TABS_RELATIVE:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004291 case ADDR_NONE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02004292 case ADDR_UNSIGNED:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004293 emsg(_(e_invrange));
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004294 cmd = NULL;
4295 goto error;
4296 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004297 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004298#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02004299 lnum = qf_get_cur_idx(eap);
4300#endif
4301 break;
4302 case ADDR_QUICKFIX_VALID:
4303#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004304 lnum = qf_get_cur_valid_idx(eap);
Bram Moolenaare906c502015-09-09 21:10:39 +02004305#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004306 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004307 }
4308 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004309
4310 case '$': /* '$' - last line */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004311 ++cmd;
4312 switch (addr_type)
4313 {
4314 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004315 case ADDR_OTHER:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004316 lnum = curbuf->b_ml.ml_line_count;
4317 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004318 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004319 lnum = LAST_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004320 break;
4321 case ADDR_ARGUMENTS:
4322 lnum = ARGCOUNT;
4323 break;
4324 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004325 buf = lastbuf;
4326 while (buf->b_ml.ml_mfp == NULL)
4327 {
4328 if (buf->b_prev == NULL)
4329 break;
4330 buf = buf->b_prev;
4331 }
4332 lnum = buf->b_fnum;
4333 break;
4334 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004335 lnum = lastbuf->b_fnum;
4336 break;
4337 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004338 lnum = LAST_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004339 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004340 case ADDR_TABS_RELATIVE:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004341 case ADDR_NONE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02004342 case ADDR_UNSIGNED:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004343 emsg(_(e_invrange));
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004344 cmd = NULL;
4345 goto error;
4346 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004347 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004348#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004349 lnum = qf_get_size(eap);
4350 if (lnum == 0)
4351 lnum = 1;
Bram Moolenaare906c502015-09-09 21:10:39 +02004352#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004353 break;
Bram Moolenaar25190db2019-05-04 15:05:28 +02004354 case ADDR_QUICKFIX_VALID:
4355#ifdef FEAT_QUICKFIX
4356 lnum = qf_get_valid_size(eap);
4357 if (lnum == 0)
4358 lnum = 1;
4359#endif
4360 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004361 }
4362 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004363
4364 case '\'': /* ''' - mark */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004365 if (*++cmd == NUL)
4366 {
4367 cmd = NULL;
4368 goto error;
4369 }
4370 if (addr_type != ADDR_LINES)
4371 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004372 emsg(_(e_invaddr));
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004373 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004374 goto error;
4375 }
4376 if (skip)
4377 ++cmd;
4378 else
4379 {
4380 /* Only accept a mark in another file when it is
4381 * used by itself: ":'M". */
4382 fp = getmark(*cmd, to_other_file && cmd[1] == NUL);
4383 ++cmd;
4384 if (fp == (pos_T *)-1)
4385 /* Jumped to another file. */
4386 lnum = curwin->w_cursor.lnum;
4387 else
4388 {
4389 if (check_mark(fp) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004390 {
4391 cmd = NULL;
4392 goto error;
4393 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004394 lnum = fp->lnum;
4395 }
4396 }
4397 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004398
4399 case '/':
4400 case '?': /* '/' or '?' - search */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004401 c = *cmd++;
4402 if (addr_type != ADDR_LINES)
4403 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004404 emsg(_(e_invaddr));
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004405 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004406 goto error;
4407 }
4408 if (skip) /* skip "/pat/" */
4409 {
4410 cmd = skip_regexp(cmd, c, (int)p_magic, NULL);
4411 if (*cmd == c)
4412 ++cmd;
4413 }
4414 else
4415 {
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02004416 int flags;
4417
4418 pos = curwin->w_cursor; // save curwin->w_cursor
4419
4420 // When '/' or '?' follows another address, start from
4421 // there.
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004422 if (lnum != MAXLNUM)
4423 curwin->w_cursor.lnum = lnum;
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02004424
4425 // Start a forward search at the end of the line (unless
4426 // before the first line).
4427 // Start a backward search at the start of the line.
4428 // This makes sure we never match in the current
4429 // line, and can match anywhere in the
4430 // next/previous line.
Bram Moolenaar8ada6aa2017-12-19 21:23:21 +01004431 if (c == '/' && curwin->w_cursor.lnum > 0)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004432 curwin->w_cursor.col = MAXCOL;
4433 else
4434 curwin->w_cursor.col = 0;
4435 searchcmdlen = 0;
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02004436 flags = silent ? 0 : SEARCH_HIS | SEARCH_MSG;
4437 if (!do_search(NULL, c, cmd, 1L, flags, NULL, NULL))
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004438 {
4439 curwin->w_cursor = pos;
4440 cmd = NULL;
4441 goto error;
4442 }
4443 lnum = curwin->w_cursor.lnum;
4444 curwin->w_cursor = pos;
4445 /* adjust command string pointer */
4446 cmd += searchcmdlen;
4447 }
4448 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004449
4450 case '\\': /* "\?", "\/" or "\&", repeat search */
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004451 ++cmd;
4452 if (addr_type != ADDR_LINES)
4453 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004454 emsg(_(e_invaddr));
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004455 cmd = NULL;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004456 goto error;
4457 }
4458 if (*cmd == '&')
4459 i = RE_SUBST;
4460 else if (*cmd == '?' || *cmd == '/')
4461 i = RE_SEARCH;
4462 else
4463 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004464 emsg(_(e_backslash));
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004465 cmd = NULL;
4466 goto error;
4467 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004468
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004469 if (!skip)
4470 {
4471 /*
4472 * When search follows another address, start from
4473 * there.
4474 */
4475 if (lnum != MAXLNUM)
4476 pos.lnum = lnum;
4477 else
4478 pos.lnum = curwin->w_cursor.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004479
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004480 /*
4481 * Start the search just like for the above
4482 * do_search().
4483 */
4484 if (*cmd != '?')
4485 pos.col = MAXCOL;
4486 else
4487 pos.col = 0;
Bram Moolenaarbd8539a2015-08-11 18:53:03 +02004488 pos.coladd = 0;
Bram Moolenaar5d24a222018-12-23 19:10:09 +01004489 if (searchit(curwin, curbuf, &pos, NULL,
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004490 *cmd == '?' ? BACKWARD : FORWARD,
4491 (char_u *)"", 1L, SEARCH_MSG,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02004492 i, (linenr_T)0, NULL, NULL) != FAIL)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004493 lnum = pos.lnum;
4494 else
4495 {
4496 cmd = NULL;
4497 goto error;
4498 }
4499 }
4500 ++cmd;
4501 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004502
4503 default:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004504 if (VIM_ISDIGIT(*cmd)) /* absolute line number */
4505 lnum = getdigits(&cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004506 }
4507
4508 for (;;)
4509 {
4510 cmd = skipwhite(cmd);
4511 if (*cmd != '-' && *cmd != '+' && !VIM_ISDIGIT(*cmd))
4512 break;
4513
4514 if (lnum == MAXLNUM)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004515 {
4516 switch (addr_type)
4517 {
4518 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004519 case ADDR_OTHER:
4520 // "+1" is same as ".+1"
Bram Moolenaarf240e182014-11-27 18:33:02 +01004521 lnum = curwin->w_cursor.lnum;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004522 break;
4523 case ADDR_WINDOWS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004524 lnum = CURRENT_WIN_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004525 break;
4526 case ADDR_ARGUMENTS:
4527 lnum = curwin->w_arg_idx + 1;
4528 break;
4529 case ADDR_LOADED_BUFFERS:
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004530 case ADDR_BUFFERS:
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004531 lnum = curbuf->b_fnum;
4532 break;
4533 case ADDR_TABS:
Bram Moolenaarf240e182014-11-27 18:33:02 +01004534 lnum = CURRENT_TAB_NR;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004535 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004536 case ADDR_TABS_RELATIVE:
4537 lnum = 1;
4538 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004539 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004540#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02004541 lnum = qf_get_cur_idx(eap);
4542#endif
4543 break;
4544 case ADDR_QUICKFIX_VALID:
4545#ifdef FEAT_QUICKFIX
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004546 lnum = qf_get_cur_valid_idx(eap);
Bram Moolenaare906c502015-09-09 21:10:39 +02004547#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004548 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02004549 case ADDR_NONE:
Bram Moolenaar25190db2019-05-04 15:05:28 +02004550 case ADDR_UNSIGNED:
4551 lnum = 0;
Bram Moolenaarb7316892019-05-01 18:08:42 +02004552 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01004553 }
4554 }
4555
Bram Moolenaar071d4272004-06-13 20:20:40 +00004556 if (VIM_ISDIGIT(*cmd))
4557 i = '+'; /* "number" is same as "+number" */
4558 else
4559 i = *cmd++;
4560 if (!VIM_ISDIGIT(*cmd)) /* '+' is '+1', but '+0' is not '+1' */
4561 n = 1;
4562 else
4563 n = getdigits(&cmd);
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004564
4565 if (addr_type == ADDR_TABS_RELATIVE)
4566 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004567 emsg(_(e_invrange));
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004568 cmd = NULL;
4569 goto error;
4570 }
4571 else if (addr_type == ADDR_LOADED_BUFFERS
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004572 || addr_type == ADDR_BUFFERS)
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004573 lnum = compute_buffer_local_count(
4574 addr_type, lnum, (i == '-') ? -1 * n : n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004575 else
Bram Moolenaarded27822017-01-02 14:27:34 +01004576 {
4577#ifdef FEAT_FOLDING
4578 /* Relative line addressing, need to adjust for folded lines
4579 * now, but only do it after the first address. */
4580 if (addr_type == ADDR_LINES && (i == '-' || i == '+')
4581 && address_count >= 2)
4582 (void)hasFolding(lnum, NULL, &lnum);
4583#endif
4584 if (i == '-')
4585 lnum -= n;
4586 else
4587 lnum += n;
4588 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004589 }
4590 } while (*cmd == '/' || *cmd == '?');
4591
4592error:
4593 *ptr = cmd;
4594 return lnum;
4595}
4596
4597/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00004598 * Get flags from an Ex command argument.
4599 */
4600 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004601get_flags(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00004602{
4603 while (vim_strchr((char_u *)"lp#", *eap->arg) != NULL)
4604 {
4605 if (*eap->arg == 'l')
4606 eap->flags |= EXFLAG_LIST;
4607 else if (*eap->arg == 'p')
4608 eap->flags |= EXFLAG_PRINT;
4609 else
4610 eap->flags |= EXFLAG_NR;
4611 eap->arg = skipwhite(eap->arg + 1);
4612 }
4613}
4614
4615/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004616 * Function called for command which is Not Implemented. NI!
4617 */
4618 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004619ex_ni(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004620{
4621 if (!eap->skip)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004622 eap->errmsg = N_("E319: Sorry, the command is not available in this version");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004623}
4624
Bram Moolenaar7bb75552007-07-16 18:39:49 +00004625#ifdef HAVE_EX_SCRIPT_NI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004626/*
4627 * Function called for script command which is Not Implemented. NI!
4628 * Skips over ":perl <<EOF" constructs.
4629 */
4630 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004631ex_script_ni(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004632{
4633 if (!eap->skip)
4634 ex_ni(eap);
4635 else
4636 vim_free(script_get(eap, eap->arg));
4637}
4638#endif
4639
4640/*
4641 * Check range in Ex command for validity.
4642 * Return NULL when valid, error message when invalid.
4643 */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004644 static char *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004645invalid_range(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004646{
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004647 buf_T *buf;
Bram Moolenaar25190db2019-05-04 15:05:28 +02004648
Bram Moolenaar071d4272004-06-13 20:20:40 +00004649 if ( eap->line1 < 0
4650 || eap->line2 < 0
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004651 || eap->line1 > eap->line2)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004652 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004653
4654 if (eap->argt & RANGE)
4655 {
Bram Moolenaarb7316892019-05-01 18:08:42 +02004656 switch (eap->addr_type)
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004657 {
4658 case ADDR_LINES:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004659 if (eap->line2 > curbuf->b_ml.ml_line_count
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004660#ifdef FEAT_DIFF
4661 + (eap->cmdidx == CMD_diffget)
4662#endif
4663 )
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004664 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004665 break;
4666 case ADDR_ARGUMENTS:
Bram Moolenaarc0a37b92015-02-03 19:10:53 +01004667 /* add 1 if ARGCOUNT is 0 */
4668 if (eap->line2 > ARGCOUNT + (!ARGCOUNT))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004669 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004670 break;
4671 case ADDR_BUFFERS:
4672 if (eap->line1 < firstbuf->b_fnum
4673 || eap->line2 > lastbuf->b_fnum)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004674 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004675 break;
4676 case ADDR_LOADED_BUFFERS:
4677 buf = firstbuf;
4678 while (buf->b_ml.ml_mfp == NULL)
4679 {
4680 if (buf->b_next == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004681 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004682 buf = buf->b_next;
4683 }
4684 if (eap->line1 < buf->b_fnum)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004685 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004686 buf = lastbuf;
4687 while (buf->b_ml.ml_mfp == NULL)
4688 {
4689 if (buf->b_prev == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004690 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004691 buf = buf->b_prev;
4692 }
4693 if (eap->line2 > buf->b_fnum)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004694 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004695 break;
4696 case ADDR_WINDOWS:
Bram Moolenaar8be63882015-01-14 11:25:05 +01004697 if (eap->line2 > LAST_WIN_NR)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004698 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004699 break;
4700 case ADDR_TABS:
4701 if (eap->line2 > LAST_TAB_NR)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004702 return _(e_invrange);
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004703 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004704 case ADDR_TABS_RELATIVE:
Bram Moolenaarb7316892019-05-01 18:08:42 +02004705 case ADDR_OTHER:
4706 // Any range is OK.
Bram Moolenaar2f72c702017-01-29 14:48:10 +01004707 break;
Bram Moolenaaraa23b372015-09-08 18:46:31 +02004708 case ADDR_QUICKFIX:
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004709#ifdef FEAT_QUICKFIX
Bram Moolenaar25190db2019-05-04 15:05:28 +02004710 // No error for value that is too big, will use the last entry.
4711 if (eap->line2 <= 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004712 return _(e_invrange);
Bram Moolenaare906c502015-09-09 21:10:39 +02004713#endif
Bram Moolenaar26f0cb12019-05-01 21:43:42 +02004714 break;
Bram Moolenaar25190db2019-05-04 15:05:28 +02004715 case ADDR_QUICKFIX_VALID:
4716#ifdef FEAT_QUICKFIX
4717 if ((eap->line2 != 1 && eap->line2 > qf_get_valid_size(eap))
4718 || eap->line2 < 0)
4719 return _(e_invrange);
4720#endif
4721 break;
4722 case ADDR_UNSIGNED:
4723 if (eap->line2 < 0)
4724 return _(e_invrange);
4725 break;
Bram Moolenaarb7316892019-05-01 18:08:42 +02004726 case ADDR_NONE:
4727 // Will give an error elsewhere.
4728 break;
Bram Moolenaar3ffc79a2015-01-07 15:57:17 +01004729 }
4730 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004731 return NULL;
4732}
4733
4734/*
4735 * Correct the range for zero line number, if required.
4736 */
4737 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004738correct_range(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004739{
4740 if (!(eap->argt & ZEROR)) /* zero in range not allowed */
4741 {
4742 if (eap->line1 == 0)
4743 eap->line1 = 1;
4744 if (eap->line2 == 0)
4745 eap->line2 = 1;
4746 }
4747}
4748
Bram Moolenaar748bf032005-02-02 23:04:36 +00004749#ifdef FEAT_QUICKFIX
Bram Moolenaar748bf032005-02-02 23:04:36 +00004750/*
4751 * For a ":vimgrep" or ":vimgrepadd" command return a pointer past the
4752 * pattern. Otherwise return eap->arg.
4753 */
4754 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004755skip_grep_pat(exarg_T *eap)
Bram Moolenaar748bf032005-02-02 23:04:36 +00004756{
4757 char_u *p = eap->arg;
4758
Bram Moolenaara37420f2006-02-04 22:37:47 +00004759 if (*p != NUL && (eap->cmdidx == CMD_vimgrep || eap->cmdidx == CMD_lvimgrep
4760 || eap->cmdidx == CMD_vimgrepadd
4761 || eap->cmdidx == CMD_lvimgrepadd
4762 || grep_internal(eap->cmdidx)))
Bram Moolenaar748bf032005-02-02 23:04:36 +00004763 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00004764 p = skip_vimgrep_pat(p, NULL, NULL);
Bram Moolenaar748bf032005-02-02 23:04:36 +00004765 if (p == NULL)
4766 p = eap->arg;
Bram Moolenaar748bf032005-02-02 23:04:36 +00004767 }
4768 return p;
4769}
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004770
4771/*
4772 * For the ":make" and ":grep" commands insert the 'makeprg'/'grepprg' option
4773 * in the command line, so that things like % get expanded.
4774 */
4775 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004776replace_makeprg(exarg_T *eap, char_u *p, char_u **cmdlinep)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004777{
4778 char_u *new_cmdline;
4779 char_u *program;
4780 char_u *pos;
4781 char_u *ptr;
4782 int len;
4783 int i;
4784
4785 /*
4786 * Don't do it when ":vimgrep" is used for ":grep".
4787 */
Bram Moolenaara37420f2006-02-04 22:37:47 +00004788 if ((eap->cmdidx == CMD_make || eap->cmdidx == CMD_lmake
4789 || eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4790 || eap->cmdidx == CMD_grepadd
4791 || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004792 && !grep_internal(eap->cmdidx))
4793 {
Bram Moolenaara37420f2006-02-04 22:37:47 +00004794 if (eap->cmdidx == CMD_grep || eap->cmdidx == CMD_lgrep
4795 || eap->cmdidx == CMD_grepadd || eap->cmdidx == CMD_lgrepadd)
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004796 {
4797 if (*curbuf->b_p_gp == NUL)
4798 program = p_gp;
4799 else
4800 program = curbuf->b_p_gp;
4801 }
4802 else
4803 {
4804 if (*curbuf->b_p_mp == NUL)
4805 program = p_mp;
4806 else
4807 program = curbuf->b_p_mp;
4808 }
4809
4810 p = skipwhite(p);
4811
4812 if ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4813 {
4814 /* replace $* by given arguments */
4815 i = 1;
4816 while ((pos = (char_u *)strstr((char *)pos + 2, "$*")) != NULL)
4817 ++i;
4818 len = (int)STRLEN(p);
Bram Moolenaar51e14382019-05-25 20:21:28 +02004819 new_cmdline = alloc(STRLEN(program) + i * (len - 2) + 1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004820 if (new_cmdline == NULL)
4821 return NULL; /* out of memory */
4822 ptr = new_cmdline;
4823 while ((pos = (char_u *)strstr((char *)program, "$*")) != NULL)
4824 {
4825 i = (int)(pos - program);
4826 STRNCPY(ptr, program, i);
4827 STRCPY(ptr += i, p);
4828 ptr += len;
4829 program = pos + 2;
4830 }
4831 STRCPY(ptr, program);
4832 }
4833 else
4834 {
Bram Moolenaar51e14382019-05-25 20:21:28 +02004835 new_cmdline = alloc(STRLEN(program) + STRLEN(p) + 2);
Bram Moolenaard857f0e2005-06-21 22:37:39 +00004836 if (new_cmdline == NULL)
4837 return NULL; /* out of memory */
4838 STRCPY(new_cmdline, program);
4839 STRCAT(new_cmdline, " ");
4840 STRCAT(new_cmdline, p);
4841 }
4842 msg_make(p);
4843
4844 /* 'eap->cmd' is not set here, because it is not used at CMD_make */
4845 vim_free(*cmdlinep);
4846 *cmdlinep = new_cmdline;
4847 p = new_cmdline;
4848 }
4849 return p;
4850}
Bram Moolenaar748bf032005-02-02 23:04:36 +00004851#endif
4852
Bram Moolenaar071d4272004-06-13 20:20:40 +00004853/*
4854 * Expand file name in Ex command argument.
4855 * Return FAIL for failure, OK otherwise.
4856 */
4857 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004858expand_filename(
4859 exarg_T *eap,
4860 char_u **cmdlinep,
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004861 char **errormsgp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004862{
4863 int has_wildcards; /* need to expand wildcards */
4864 char_u *repl;
4865 int srclen;
4866 char_u *p;
4867 int n;
Bram Moolenaar63b92542007-03-27 14:57:09 +00004868 int escaped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004869
Bram Moolenaar748bf032005-02-02 23:04:36 +00004870#ifdef FEAT_QUICKFIX
4871 /* Skip a regexp pattern for ":vimgrep[add] pat file..." */
4872 p = skip_grep_pat(eap);
4873#else
4874 p = eap->arg;
4875#endif
4876
Bram Moolenaar071d4272004-06-13 20:20:40 +00004877 /*
4878 * Decide to expand wildcards *before* replacing '%', '#', etc. If
4879 * the file name contains a wildcard it should not cause expanding.
4880 * (it will be expanded anyway if there is a wildcard before replacing).
4881 */
Bram Moolenaar748bf032005-02-02 23:04:36 +00004882 has_wildcards = mch_has_wildcard(p);
4883 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004884 {
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00004885#ifdef FEAT_EVAL
4886 /* Skip over `=expr`, wildcards in it are not expanded. */
4887 if (p[0] == '`' && p[1] == '=')
4888 {
4889 p += 2;
4890 (void)skip_expr(&p);
4891 if (*p == '`')
4892 ++p;
4893 continue;
4894 }
4895#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004896 /*
4897 * Quick check if this cannot be the start of a special string.
4898 * Also removes backslash before '%', '#' and '<'.
4899 */
4900 if (vim_strchr((char_u *)"%#<", *p) == NULL)
4901 {
4902 ++p;
4903 continue;
4904 }
4905
4906 /*
4907 * Try to find a match at this position.
4908 */
Bram Moolenaar63b92542007-03-27 14:57:09 +00004909 repl = eval_vars(p, eap->arg, &srclen, &(eap->do_ecmd_lnum),
4910 errormsgp, &escaped);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004911 if (*errormsgp != NULL) /* error detected */
4912 return FAIL;
4913 if (repl == NULL) /* no match found */
4914 {
4915 p += srclen;
4916 continue;
4917 }
4918
Bram Moolenaard8b0cf12004-12-12 11:33:30 +00004919 /* Wildcards won't be expanded below, the replacement is taken
4920 * literally. But do expand "~/file", "~user/file" and "$HOME/file". */
4921 if (vim_strchr(repl, '$') != NULL || vim_strchr(repl, '~') != NULL)
4922 {
4923 char_u *l = repl;
4924
4925 repl = expand_env_save(repl);
4926 vim_free(l);
4927 }
4928
Bram Moolenaar63b92542007-03-27 14:57:09 +00004929 /* Need to escape white space et al. with a backslash.
4930 * Don't do this for:
4931 * - replacement that already has been escaped: "##"
4932 * - shell commands (may have to use quotes instead).
4933 * - non-unix systems when there is a single argument (spaces don't
4934 * separate arguments then).
4935 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004936 if (!eap->usefilter
Bram Moolenaar63b92542007-03-27 14:57:09 +00004937 && !escaped
Bram Moolenaar071d4272004-06-13 20:20:40 +00004938 && eap->cmdidx != CMD_bang
Bram Moolenaar071d4272004-06-13 20:20:40 +00004939 && eap->cmdidx != CMD_grep
4940 && eap->cmdidx != CMD_grepadd
Bram Moolenaarbf15b8d2017-06-04 20:43:48 +02004941 && eap->cmdidx != CMD_hardcopy
Bram Moolenaar67883b42017-07-27 22:57:00 +02004942 && eap->cmdidx != CMD_lgrep
4943 && eap->cmdidx != CMD_lgrepadd
4944 && eap->cmdidx != CMD_lmake
4945 && eap->cmdidx != CMD_make
4946 && eap->cmdidx != CMD_terminal
Bram Moolenaar071d4272004-06-13 20:20:40 +00004947#ifndef UNIX
4948 && !(eap->argt & NOSPC)
4949#endif
4950 )
4951 {
4952 char_u *l;
4953#ifdef BACKSLASH_IN_FILENAME
4954 /* Don't escape a backslash here, because rem_backslash() doesn't
4955 * remove it later. */
4956 static char_u *nobslash = (char_u *)" \t\"|";
4957# define ESCAPE_CHARS nobslash
4958#else
4959# define ESCAPE_CHARS escape_chars
4960#endif
4961
4962 for (l = repl; *l; ++l)
4963 if (vim_strchr(ESCAPE_CHARS, *l) != NULL)
4964 {
4965 l = vim_strsave_escaped(repl, ESCAPE_CHARS);
4966 if (l != NULL)
4967 {
4968 vim_free(repl);
4969 repl = l;
4970 }
4971 break;
4972 }
4973 }
4974
4975 /* For a shell command a '!' must be escaped. */
Bram Moolenaar67883b42017-07-27 22:57:00 +02004976 if ((eap->usefilter || eap->cmdidx == CMD_bang
4977 || eap->cmdidx == CMD_terminal)
Bram Moolenaar31b7d382014-04-01 18:54:48 +02004978 && vim_strpbrk(repl, (char_u *)"!") != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004979 {
4980 char_u *l;
4981
Bram Moolenaar31b7d382014-04-01 18:54:48 +02004982 l = vim_strsave_escaped(repl, (char_u *)"!");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004983 if (l != NULL)
4984 {
4985 vim_free(repl);
4986 repl = l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004987 }
4988 }
4989
4990 p = repl_cmdline(eap, p, srclen, repl, cmdlinep);
4991 vim_free(repl);
4992 if (p == NULL)
4993 return FAIL;
4994 }
4995
4996 /*
4997 * One file argument: Expand wildcards.
4998 * Don't do this with ":r !command" or ":w !command".
4999 */
5000 if ((eap->argt & NOSPC) && !eap->usefilter)
5001 {
5002 /*
5003 * May do this twice:
5004 * 1. Replace environment variables.
5005 * 2. Replace any other wildcards, remove backslashes.
5006 */
5007 for (n = 1; n <= 2; ++n)
5008 {
5009 if (n == 2)
5010 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005011 /*
5012 * Halve the number of backslashes (this is Vi compatible).
5013 * For Unix and OS/2, when wildcards are expanded, this is
5014 * done by ExpandOne() below.
5015 */
Bram Moolenaare7fedb62015-12-31 19:07:19 +01005016#if defined(UNIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005017 if (!has_wildcards)
5018#endif
5019 backslash_halve(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005020 }
5021
5022 if (has_wildcards)
5023 {
5024 if (n == 1)
5025 {
5026 /*
5027 * First loop: May expand environment variables. This
5028 * can be done much faster with expand_env() than with
5029 * something else (e.g., calling a shell).
5030 * After expanding environment variables, check again
5031 * if there are still wildcards present.
5032 */
5033 if (vim_strchr(eap->arg, '$') != NULL
5034 || vim_strchr(eap->arg, '~') != NULL)
5035 {
Bram Moolenaara1ba8112005-06-28 23:23:32 +00005036 expand_env_esc(eap->arg, NameBuff, MAXPATHL,
Bram Moolenaar9f0545d2007-09-26 20:36:32 +00005037 TRUE, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005038 has_wildcards = mch_has_wildcard(NameBuff);
5039 p = NameBuff;
5040 }
5041 else
5042 p = NULL;
5043 }
5044 else /* n == 2 */
5045 {
5046 expand_T xpc;
Bram Moolenaar94950a92010-12-02 16:01:29 +01005047 int options = WILD_LIST_NOTFOUND|WILD_ADD_SLASH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005048
5049 ExpandInit(&xpc);
5050 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar94950a92010-12-02 16:01:29 +01005051 if (p_wic)
5052 options += WILD_ICASE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005053 p = ExpandOne(&xpc, eap->arg, NULL,
Bram Moolenaar94950a92010-12-02 16:01:29 +01005054 options, WILD_EXPAND_FREE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005055 if (p == NULL)
5056 return FAIL;
5057 }
5058 if (p != NULL)
5059 {
5060 (void)repl_cmdline(eap, eap->arg, (int)STRLEN(eap->arg),
5061 p, cmdlinep);
5062 if (n == 2) /* p came from ExpandOne() */
5063 vim_free(p);
5064 }
5065 }
5066 }
5067 }
5068 return OK;
5069}
5070
5071/*
5072 * Replace part of the command line, keeping eap->cmd, eap->arg and
5073 * eap->nextcmd correct.
5074 * "src" points to the part that is to be replaced, of length "srclen".
5075 * "repl" is the replacement string.
5076 * Returns a pointer to the character after the replaced string.
5077 * Returns NULL for failure.
5078 */
5079 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005080repl_cmdline(
5081 exarg_T *eap,
5082 char_u *src,
5083 int srclen,
5084 char_u *repl,
5085 char_u **cmdlinep)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005086{
5087 int len;
5088 int i;
5089 char_u *new_cmdline;
5090
5091 /*
5092 * The new command line is build in new_cmdline[].
5093 * First allocate it.
5094 * Careful: a "+cmd" argument may have been NUL terminated.
5095 */
5096 len = (int)STRLEN(repl);
5097 i = (int)(src - *cmdlinep) + (int)STRLEN(src + srclen) + len + 3;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005098 if (eap->nextcmd != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005099 i += (int)STRLEN(eap->nextcmd);/* add space for next command */
Bram Moolenaar964b3742019-05-24 18:54:09 +02005100 if ((new_cmdline = alloc(i)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005101 return NULL; /* out of memory! */
5102
5103 /*
5104 * Copy the stuff before the expanded part.
5105 * Copy the expanded stuff.
5106 * Copy what came after the expanded part.
5107 * Copy the next commands, if there are any.
5108 */
5109 i = (int)(src - *cmdlinep); /* length of part before match */
5110 mch_memmove(new_cmdline, *cmdlinep, (size_t)i);
Bram Moolenaara3ffd9c2005-07-21 21:03:15 +00005111
Bram Moolenaar071d4272004-06-13 20:20:40 +00005112 mch_memmove(new_cmdline + i, repl, (size_t)len);
5113 i += len; /* remember the end of the string */
5114 STRCPY(new_cmdline + i, src + srclen);
5115 src = new_cmdline + i; /* remember where to continue */
5116
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005117 if (eap->nextcmd != NULL) /* append next command */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005118 {
5119 i = (int)STRLEN(new_cmdline) + 1;
5120 STRCPY(new_cmdline + i, eap->nextcmd);
5121 eap->nextcmd = new_cmdline + i;
5122 }
5123 eap->cmd = new_cmdline + (eap->cmd - *cmdlinep);
5124 eap->arg = new_cmdline + (eap->arg - *cmdlinep);
5125 if (eap->do_ecmd_cmd != NULL && eap->do_ecmd_cmd != dollar_command)
5126 eap->do_ecmd_cmd = new_cmdline + (eap->do_ecmd_cmd - *cmdlinep);
5127 vim_free(*cmdlinep);
5128 *cmdlinep = new_cmdline;
5129
5130 return src;
5131}
5132
5133/*
5134 * Check for '|' to separate commands and '"' to start comments.
5135 */
5136 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005137separate_nextcmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005138{
5139 char_u *p;
5140
Bram Moolenaar86b68352004-12-27 21:59:20 +00005141#ifdef FEAT_QUICKFIX
Bram Moolenaar748bf032005-02-02 23:04:36 +00005142 p = skip_grep_pat(eap);
5143#else
5144 p = eap->arg;
Bram Moolenaar86b68352004-12-27 21:59:20 +00005145#endif
5146
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005147 for ( ; *p; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005148 {
5149 if (*p == Ctrl_V)
5150 {
5151 if (eap->argt & (USECTRLV | XFILE))
5152 ++p; /* skip CTRL-V and next char */
5153 else
Bram Moolenaarb0db5692007-08-14 20:54:49 +00005154 /* remove CTRL-V and skip next char */
Bram Moolenaara7241f52008-06-24 20:39:31 +00005155 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005156 if (*p == NUL) /* stop at NUL after CTRL-V */
5157 break;
5158 }
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005159
5160#ifdef FEAT_EVAL
5161 /* Skip over `=expr` when wildcards are expanded. */
Bram Moolenaardf177f62005-02-22 08:39:57 +00005162 else if (p[0] == '`' && p[1] == '=' && (eap->argt & XFILE))
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005163 {
5164 p += 2;
5165 (void)skip_expr(&p);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005166 }
5167#endif
5168
Bram Moolenaar071d4272004-06-13 20:20:40 +00005169 /* Check for '"': start of comment or '|': next command */
5170 /* :@" and :*" do not start a comment!
5171 * :redir @" doesn't either. */
5172 else if ((*p == '"' && !(eap->argt & NOTRLCOM)
5173 && ((eap->cmdidx != CMD_at && eap->cmdidx != CMD_star)
5174 || p != eap->arg)
5175 && (eap->cmdidx != CMD_redir
5176 || p != eap->arg + 1 || p[-1] != '@'))
5177 || *p == '|' || *p == '\n')
5178 {
5179 /*
5180 * We remove the '\' before the '|', unless USECTRLV is used
5181 * AND 'b' is present in 'cpoptions'.
5182 */
5183 if ((vim_strchr(p_cpo, CPO_BAR) == NULL
5184 || !(eap->argt & USECTRLV)) && *(p - 1) == '\\')
5185 {
Bram Moolenaara7241f52008-06-24 20:39:31 +00005186 STRMOVE(p - 1, p); /* remove the '\' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005187 --p;
5188 }
5189 else
5190 {
5191 eap->nextcmd = check_nextcmd(p);
5192 *p = NUL;
5193 break;
5194 }
5195 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005196 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00005197
Bram Moolenaar071d4272004-06-13 20:20:40 +00005198 if (!(eap->argt & NOTRLCOM)) /* remove trailing spaces */
5199 del_trailing_spaces(eap->arg);
5200}
5201
5202/*
5203 * get + command from ex argument
5204 */
5205 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005206getargcmd(char_u **argp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005207{
5208 char_u *arg = *argp;
5209 char_u *command = NULL;
5210
5211 if (*arg == '+') /* +[command] */
5212 {
5213 ++arg;
Bram Moolenaar3e451592014-04-02 14:22:05 +02005214 if (vim_isspace(*arg) || *arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005215 command = dollar_command;
5216 else
5217 {
5218 command = arg;
5219 arg = skip_cmd_arg(command, TRUE);
5220 if (*arg != NUL)
5221 *arg++ = NUL; /* terminate command with NUL */
5222 }
5223
5224 arg = skipwhite(arg); /* skip over spaces */
5225 *argp = arg;
5226 }
5227 return command;
5228}
5229
5230/*
5231 * Find end of "+command" argument. Skip over "\ " and "\\".
5232 */
5233 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005234skip_cmd_arg(
5235 char_u *p,
5236 int rembs) /* TRUE to halve the number of backslashes */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005237{
5238 while (*p && !vim_isspace(*p))
5239 {
5240 if (*p == '\\' && p[1] != NUL)
5241 {
5242 if (rembs)
Bram Moolenaara7241f52008-06-24 20:39:31 +00005243 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005244 else
5245 ++p;
5246 }
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005247 MB_PTR_ADV(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005248 }
5249 return p;
5250}
5251
Bram Moolenaar333b80a2018-04-04 22:57:29 +02005252 int
5253get_bad_opt(char_u *p, exarg_T *eap)
5254{
5255 if (STRICMP(p, "keep") == 0)
5256 eap->bad_char = BAD_KEEP;
5257 else if (STRICMP(p, "drop") == 0)
5258 eap->bad_char = BAD_DROP;
5259 else if (MB_BYTE2LEN(*p) == 1 && p[1] == NUL)
5260 eap->bad_char = *p;
Bram Moolenaar75808492018-06-12 12:39:41 +02005261 else
5262 return FAIL;
5263 return OK;
Bram Moolenaar333b80a2018-04-04 22:57:29 +02005264}
5265
Bram Moolenaar071d4272004-06-13 20:20:40 +00005266/*
5267 * Get "++opt=arg" argument.
5268 * Return FAIL or OK.
5269 */
5270 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005271getargopt(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005272{
5273 char_u *arg = eap->arg + 2;
5274 int *pp = NULL;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02005275 int bad_char_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005276 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005277
5278 /* ":edit ++[no]bin[ary] file" */
5279 if (STRNCMP(arg, "bin", 3) == 0 || STRNCMP(arg, "nobin", 5) == 0)
5280 {
5281 if (*arg == 'n')
5282 {
5283 arg += 2;
5284 eap->force_bin = FORCE_NOBIN;
5285 }
5286 else
5287 eap->force_bin = FORCE_BIN;
5288 if (!checkforcmd(&arg, "binary", 3))
5289 return FAIL;
5290 eap->arg = skipwhite(arg);
5291 return OK;
5292 }
5293
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005294 /* ":read ++edit file" */
5295 if (STRNCMP(arg, "edit", 4) == 0)
5296 {
5297 eap->read_edit = TRUE;
5298 eap->arg = skipwhite(arg + 4);
5299 return OK;
5300 }
5301
Bram Moolenaar071d4272004-06-13 20:20:40 +00005302 if (STRNCMP(arg, "ff", 2) == 0)
5303 {
5304 arg += 2;
5305 pp = &eap->force_ff;
5306 }
5307 else if (STRNCMP(arg, "fileformat", 10) == 0)
5308 {
5309 arg += 10;
5310 pp = &eap->force_ff;
5311 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005312 else if (STRNCMP(arg, "enc", 3) == 0)
5313 {
Bram Moolenaarb38e9ab2011-12-14 14:49:45 +01005314 if (STRNCMP(arg, "encoding", 8) == 0)
5315 arg += 8;
5316 else
5317 arg += 3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005318 pp = &eap->force_enc;
5319 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005320 else if (STRNCMP(arg, "bad", 3) == 0)
5321 {
5322 arg += 3;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02005323 pp = &bad_char_idx;
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005324 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005325
5326 if (pp == NULL || *arg != '=')
5327 return FAIL;
5328
5329 ++arg;
5330 *pp = (int)(arg - eap->cmd);
5331 arg = skip_cmd_arg(arg, FALSE);
5332 eap->arg = skipwhite(arg);
5333 *arg = NUL;
5334
Bram Moolenaar071d4272004-06-13 20:20:40 +00005335 if (pp == &eap->force_ff)
5336 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005337 if (check_ff_value(eap->cmd + eap->force_ff) == FAIL)
5338 return FAIL;
Bram Moolenaar333b80a2018-04-04 22:57:29 +02005339 eap->force_ff = eap->cmd[eap->force_ff];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005340 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005341 else if (pp == &eap->force_enc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005342 {
5343 /* Make 'fileencoding' lower case. */
5344 for (p = eap->cmd + eap->force_enc; *p != NUL; ++p)
5345 *p = TOLOWER_ASC(*p);
5346 }
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005347 else
5348 {
5349 /* Check ++bad= argument. Must be a single-byte character, "keep" or
5350 * "drop". */
Bram Moolenaar333b80a2018-04-04 22:57:29 +02005351 if (get_bad_opt(eap->cmd + bad_char_idx, eap) == FAIL)
Bram Moolenaarb0bf8582005-12-13 20:02:15 +00005352 return FAIL;
5353 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005354
5355 return OK;
5356}
5357
5358/*
5359 * ":abbreviate" and friends.
5360 */
5361 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005362ex_abbreviate(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005363{
5364 do_exmap(eap, TRUE); /* almost the same as mapping */
5365}
5366
5367/*
5368 * ":map" and friends.
5369 */
5370 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005371ex_map(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005372{
5373 /*
5374 * If we are sourcing .exrc or .vimrc in current directory we
5375 * print the mappings for security reasons.
5376 */
5377 if (secure)
5378 {
5379 secure = 2;
5380 msg_outtrans(eap->cmd);
5381 msg_putchar('\n');
5382 }
5383 do_exmap(eap, FALSE);
5384}
5385
5386/*
5387 * ":unmap" and friends.
5388 */
5389 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005390ex_unmap(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005391{
5392 do_exmap(eap, FALSE);
5393}
5394
5395/*
5396 * ":mapclear" and friends.
5397 */
5398 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005399ex_mapclear(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005400{
5401 map_clear(eap->cmd, eap->arg, eap->forceit, FALSE);
5402}
5403
5404/*
5405 * ":abclear" and friends.
5406 */
5407 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005408ex_abclear(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005409{
5410 map_clear(eap->cmd, eap->arg, TRUE, TRUE);
5411}
5412
Bram Moolenaar071d4272004-06-13 20:20:40 +00005413 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005414ex_autocmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005415{
5416 /*
Bram Moolenaar26d4b892018-07-04 22:26:28 +02005417 * Disallow autocommands from .exrc and .vimrc in current
Bram Moolenaar071d4272004-06-13 20:20:40 +00005418 * directory for security reasons.
5419 */
5420 if (secure)
5421 {
5422 secure = 2;
5423 eap->errmsg = e_curdir;
5424 }
5425 else if (eap->cmdidx == CMD_autocmd)
5426 do_autocmd(eap->arg, eap->forceit);
5427 else
5428 do_augroup(eap->arg, eap->forceit);
5429}
5430
5431/*
5432 * ":doautocmd": Apply the automatic commands to the current buffer.
5433 */
5434 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005435ex_doautocmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005436{
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005437 char_u *arg = eap->arg;
5438 int call_do_modelines = check_nomodeline(&arg);
Bram Moolenaar1610d052016-06-09 22:53:01 +02005439 int did_aucmd;
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005440
Bram Moolenaar1610d052016-06-09 22:53:01 +02005441 (void)do_doautocmd(arg, TRUE, &did_aucmd);
5442 /* Only when there is no <nomodeline>. */
5443 if (call_do_modelines && did_aucmd)
Bram Moolenaar60542ac2012-02-12 20:14:01 +01005444 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005445}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005446
Bram Moolenaar071d4272004-06-13 20:20:40 +00005447/*
5448 * :[N]bunload[!] [N] [bufname] unload buffer
5449 * :[N]bdelete[!] [N] [bufname] delete buffer from buffer list
5450 * :[N]bwipeout[!] [N] [bufname] delete buffer really
5451 */
5452 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005453ex_bunload(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005454{
Bram Moolenaar815b76b2019-06-01 14:15:52 +02005455 if (NOT_IN_POPUP_WINDOW)
5456 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005457 eap->errmsg = do_bufdel(
5458 eap->cmdidx == CMD_bdelete ? DOBUF_DEL
5459 : eap->cmdidx == CMD_bwipeout ? DOBUF_WIPE
5460 : DOBUF_UNLOAD, eap->arg,
5461 eap->addr_count, (int)eap->line1, (int)eap->line2, eap->forceit);
5462}
5463
5464/*
5465 * :[N]buffer [N] to buffer N
5466 * :[N]sbuffer [N] to buffer N
5467 */
5468 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005469ex_buffer(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005470{
Bram Moolenaar815b76b2019-06-01 14:15:52 +02005471 if (NOT_IN_POPUP_WINDOW)
5472 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005473 if (*eap->arg)
5474 eap->errmsg = e_trailing;
5475 else
5476 {
5477 if (eap->addr_count == 0) /* default is current buffer */
5478 goto_buffer(eap, DOBUF_CURRENT, FORWARD, 0);
5479 else
5480 goto_buffer(eap, DOBUF_FIRST, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005481 if (eap->do_ecmd_cmd != NULL)
5482 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005483 }
5484}
5485
5486/*
5487 * :[N]bmodified [N] to next mod. buffer
5488 * :[N]sbmodified [N] to next mod. buffer
5489 */
5490 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005491ex_bmodified(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005492{
5493 goto_buffer(eap, DOBUF_MOD, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005494 if (eap->do_ecmd_cmd != NULL)
5495 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005496}
5497
5498/*
5499 * :[N]bnext [N] to next buffer
5500 * :[N]sbnext [N] split and to next buffer
5501 */
5502 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005503ex_bnext(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005504{
5505 goto_buffer(eap, DOBUF_CURRENT, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005506 if (eap->do_ecmd_cmd != NULL)
5507 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005508}
5509
5510/*
5511 * :[N]bNext [N] to previous buffer
5512 * :[N]bprevious [N] to previous buffer
5513 * :[N]sbNext [N] split and to previous buffer
5514 * :[N]sbprevious [N] split and to previous buffer
5515 */
5516 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005517ex_bprevious(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005518{
5519 goto_buffer(eap, DOBUF_CURRENT, BACKWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005520 if (eap->do_ecmd_cmd != NULL)
5521 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005522}
5523
5524/*
5525 * :brewind to first buffer
5526 * :bfirst to first buffer
5527 * :sbrewind split and to first buffer
5528 * :sbfirst split and to first buffer
5529 */
5530 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005531ex_brewind(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005532{
5533 goto_buffer(eap, DOBUF_FIRST, FORWARD, 0);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005534 if (eap->do_ecmd_cmd != NULL)
5535 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005536}
5537
5538/*
5539 * :blast to last buffer
5540 * :sblast split and to last buffer
5541 */
5542 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005543ex_blast(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005544{
5545 goto_buffer(eap, DOBUF_LAST, BACKWARD, 0);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005546 if (eap->do_ecmd_cmd != NULL)
5547 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005548}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005549
5550 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005551ends_excmd(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005552{
5553 return (c == NUL || c == '|' || c == '"' || c == '\n');
5554}
5555
5556#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA) || defined(FEAT_EVAL) \
5557 || defined(PROTO)
5558/*
5559 * Return the next command, after the first '|' or '\n'.
5560 * Return NULL if not found.
5561 */
5562 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005563find_nextcmd(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005564{
5565 while (*p != '|' && *p != '\n')
5566 {
5567 if (*p == NUL)
5568 return NULL;
5569 ++p;
5570 }
5571 return (p + 1);
5572}
5573#endif
5574
5575/*
Bram Moolenaar2256c992016-11-15 21:17:07 +01005576 * Check if *p is a separator between Ex commands, skipping over white space.
5577 * Return NULL if it isn't, the following character if it is.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005578 */
5579 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005580check_nextcmd(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005581{
Bram Moolenaar2256c992016-11-15 21:17:07 +01005582 char_u *s = skipwhite(p);
5583
5584 if (*s == '|' || *s == '\n')
5585 return (s + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005586 else
5587 return NULL;
5588}
5589
5590/*
5591 * - if there are more files to edit
5592 * - and this is the last window
5593 * - and forceit not used
5594 * - and not repeated twice on a row
5595 * return FAIL and give error message if 'message' TRUE
5596 * return OK otherwise
5597 */
5598 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005599check_more(
5600 int message, /* when FALSE check only, no messages */
5601 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005602{
5603 int n = ARGCOUNT - curwin->w_arg_idx - 1;
5604
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005605 if (!forceit && only_one_window()
5606 && ARGCOUNT > 1 && !arg_had_last && n >= 0 && quitmore == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005607 {
5608 if (message)
5609 {
5610#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
5611 if ((p_confirm || cmdmod.confirm) && curbuf->b_fname != NULL)
5612 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02005613 char_u buff[DIALOG_MSG_SIZE];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005614
Bram Moolenaarda6e8912018-08-21 15:12:14 +02005615 vim_snprintf((char *)buff, DIALOG_MSG_SIZE,
5616 NGETTEXT("%d more file to edit. Quit anyway?",
5617 "%d more files to edit. Quit anyway?", n), n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005618 if (vim_dialog_yesno(VIM_QUESTION, NULL, buff, 1) == VIM_YES)
5619 return OK;
5620 return FAIL;
5621 }
5622#endif
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01005623 semsg(NGETTEXT("E173: %d more file to edit",
5624 "E173: %d more files to edit", n), n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005625 quitmore = 2; /* next try to quit is allowed */
5626 }
5627 return FAIL;
5628 }
5629 return OK;
5630}
5631
Bram Moolenaarac9fb182019-04-27 13:04:13 +02005632#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005633/*
5634 * Function given to ExpandGeneric() to obtain the list of command names.
5635 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005636 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005637get_command_name(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005638{
5639 if (idx >= (int)CMD_SIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005640 return get_user_command_name(idx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005641 return cmdnames[idx].cmd_name;
5642}
5643#endif
5644
Bram Moolenaar071d4272004-06-13 20:20:40 +00005645 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005646ex_colorscheme(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005647{
Bram Moolenaare6850792010-05-14 15:28:44 +02005648 if (*eap->arg == NUL)
5649 {
5650#ifdef FEAT_EVAL
5651 char_u *expr = vim_strsave((char_u *)"g:colors_name");
5652 char_u *p = NULL;
5653
5654 if (expr != NULL)
5655 {
5656 ++emsg_off;
5657 p = eval_to_string(expr, NULL, FALSE);
5658 --emsg_off;
5659 vim_free(expr);
5660 }
5661 if (p != NULL)
5662 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01005663 msg((char *)p);
Bram Moolenaare6850792010-05-14 15:28:44 +02005664 vim_free(p);
5665 }
5666 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01005667 msg("default");
Bram Moolenaare6850792010-05-14 15:28:44 +02005668#else
Bram Moolenaar32526b32019-01-19 17:43:09 +01005669 msg(_("unknown"));
Bram Moolenaare6850792010-05-14 15:28:44 +02005670#endif
5671 }
5672 else if (load_colors(eap->arg) == FAIL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005673 semsg(_("E185: Cannot find color scheme '%s'"), eap->arg);
Bram Moolenaarf58d81a2019-01-28 20:19:05 +01005674
5675#ifdef FEAT_VTP
5676 else if (has_vtp_working())
5677 {
5678 // background color change requires clear + redraw
5679 update_screen(CLEAR);
5680 redrawcmd();
5681 }
5682#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005683}
5684
5685 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005686ex_highlight(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005687{
5688 if (*eap->arg == NUL && eap->cmd[2] == '!')
Bram Moolenaar32526b32019-01-19 17:43:09 +01005689 msg(_("Greetings, Vim user!"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005690 do_highlight(eap->arg, eap->forceit, FALSE);
5691}
5692
5693
5694/*
5695 * Call this function if we thought we were going to exit, but we won't
5696 * (because of an error). May need to restore the terminal mode.
5697 */
5698 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005699not_exiting(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005700{
5701 exiting = FALSE;
5702 settmode(TMODE_RAW);
5703}
5704
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005705 static int
5706before_quit_autocmds(win_T *wp, int quit_all, int forceit)
5707{
5708 apply_autocmds(EVENT_QUITPRE, NULL, NULL, FALSE, wp->w_buffer);
5709
5710 /* Bail out when autocommands closed the window.
5711 * Refuse to quit when the buffer in the last window is being closed (can
5712 * only happen in autocommands). */
5713 if (!win_valid(wp)
5714 || curbuf_locked()
5715 || (wp->w_buffer->b_nwindows == 1 && wp->w_buffer->b_locked > 0))
5716 return TRUE;
5717
5718 if (quit_all || (check_more(FALSE, forceit) == OK && only_one_window()))
5719 {
5720 apply_autocmds(EVENT_EXITPRE, NULL, NULL, FALSE, curbuf);
5721 /* Refuse to quit when locked or when the buffer in the last window is
5722 * being closed (can only happen in autocommands). */
5723 if (curbuf_locked()
5724 || (curbuf->b_nwindows == 1 && curbuf->b_locked > 0))
5725 return TRUE;
5726 }
5727
5728 return FALSE;
5729}
5730
Bram Moolenaar071d4272004-06-13 20:20:40 +00005731/*
Bram Moolenaarf240e182014-11-27 18:33:02 +01005732 * ":quit": quit current window, quit Vim if the last window is closed.
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005733 * ":{nr}quit": quit window {nr}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005734 */
5735 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005736ex_quit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005737{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005738 win_T *wp;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005739
Bram Moolenaar071d4272004-06-13 20:20:40 +00005740#ifdef FEAT_CMDWIN
5741 if (cmdwin_type != 0)
5742 {
5743 cmdwin_result = Ctrl_C;
5744 return;
5745 }
5746#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005747 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005748 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005749 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005750 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005751 return;
5752 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005753 if (eap->addr_count > 0)
5754 {
Bram Moolenaarf240e182014-11-27 18:33:02 +01005755 int wnr = eap->line2;
5756
5757 for (wp = firstwin; wp->w_next != NULL; wp = wp->w_next)
5758 if (--wnr <= 0)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005759 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005760 }
5761 else
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005762 wp = curwin;
Bram Moolenaarf240e182014-11-27 18:33:02 +01005763
Bram Moolenaar87ffb5c2017-10-19 12:37:42 +02005764 /* Refuse to quit when locked. */
5765 if (curbuf_locked())
5766 return;
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005767
5768 /* Trigger QuitPre and maybe ExitPre */
5769 if (before_quit_autocmds(wp, FALSE, eap->forceit))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005770 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005771
5772#ifdef FEAT_NETBEANS_INTG
5773 netbeansForcedQuit = eap->forceit;
5774#endif
5775
5776 /*
5777 * If there are more files or windows we won't exit.
5778 */
5779 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
5780 exiting = TRUE;
Bram Moolenaarff930ca2017-10-19 17:12:10 +02005781 if ((!buf_hide(wp->w_buffer)
5782 && check_changed(wp->w_buffer, (p_awa ? CCGD_AW : 0)
Bram Moolenaar45d3b142013-11-09 03:31:51 +01005783 | (eap->forceit ? CCGD_FORCEIT : 0)
5784 | CCGD_EXCMD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005785 || check_more(TRUE, eap->forceit) == FAIL
Bram Moolenaar027387f2016-01-02 22:25:52 +01005786 || (only_one_window() && check_changed_any(eap->forceit, TRUE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005787 {
5788 not_exiting();
5789 }
5790 else
5791 {
Bram Moolenaarc7a0d322015-06-19 12:43:07 +02005792 /* quit last window
5793 * Note: only_one_window() returns true, even so a help window is
5794 * still open. In that case only quit, if no address has been
5795 * specified. Example:
5796 * :h|wincmd w|1q - don't quit
5797 * :h|wincmd w|q - quit
5798 */
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01005799 if (only_one_window() && (ONE_WINDOW || eap->addr_count == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005800 getout(0);
Bram Moolenaar2c33d7b2017-10-14 16:06:20 +02005801 not_exiting();
Bram Moolenaar4033c552017-09-16 20:54:51 +02005802#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00005803 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005804#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005805 /* close window; may free buffer */
Bram Moolenaareb44a682017-08-03 22:44:55 +02005806 win_close(wp, !buf_hide(wp->w_buffer) || eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005807 }
5808}
5809
5810/*
5811 * ":cquit".
5812 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005813 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005814ex_cquit(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005815{
5816 getout(1); /* this does not always pass on the exit code to the Manx
5817 compiler. why? */
5818}
5819
5820/*
5821 * ":qall": try to quit all windows
5822 */
5823 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005824ex_quit_all(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005825{
5826# ifdef FEAT_CMDWIN
5827 if (cmdwin_type != 0)
5828 {
5829 if (eap->forceit)
5830 cmdwin_result = K_XF1; /* ex_window() takes care of this */
5831 else
5832 cmdwin_result = K_XF2;
5833 return;
5834 }
5835# endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005836
5837 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005838 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005839 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005840 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005841 return;
5842 }
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005843
5844 if (before_quit_autocmds(curwin, TRUE, eap->forceit))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005845 return;
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005846
Bram Moolenaar071d4272004-06-13 20:20:40 +00005847 exiting = TRUE;
Bram Moolenaar027387f2016-01-02 22:25:52 +01005848 if (eap->forceit || !check_changed_any(FALSE, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005849 getout(0);
5850 not_exiting();
5851}
5852
Bram Moolenaar071d4272004-06-13 20:20:40 +00005853/*
5854 * ":close": close current window, unless it is the last one
5855 */
5856 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005857ex_close(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005858{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005859 win_T *win;
5860 int winnr = 0;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005861#ifdef FEAT_CMDWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00005862 if (cmdwin_type != 0)
Bram Moolenaar9bd1a7e2011-05-19 14:50:54 +02005863 cmdwin_result = Ctrl_C;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005864 else
Bram Moolenaar4033c552017-09-16 20:54:51 +02005865#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005866 if (!text_locked() && !curbuf_locked())
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005867 {
5868 if (eap->addr_count == 0)
5869 ex_win_close(eap->forceit, curwin, NULL);
Bram Moolenaar6d41c782018-06-06 09:11:12 +02005870 else
5871 {
Bram Moolenaar29323592016-07-24 22:04:11 +02005872 FOR_ALL_WINDOWS(win)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005873 {
5874 winnr++;
5875 if (winnr == eap->line2)
5876 break;
5877 }
5878 if (win == NULL)
5879 win = lastwin;
5880 ex_win_close(eap->forceit, win, NULL);
5881 }
5882 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005883}
5884
Bram Moolenaar4033c552017-09-16 20:54:51 +02005885#ifdef FEAT_QUICKFIX
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005886/*
5887 * ":pclose": Close any preview window.
5888 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005889 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005890ex_pclose(exarg_T *eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005891{
5892 win_T *win;
5893
Bram Moolenaar29323592016-07-24 22:04:11 +02005894 FOR_ALL_WINDOWS(win)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005895 if (win->w_p_pvw)
5896 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00005897 ex_win_close(eap->forceit, win, NULL);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005898 break;
5899 }
5900}
Bram Moolenaar4033c552017-09-16 20:54:51 +02005901#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005902
Bram Moolenaarf740b292006-02-16 22:11:02 +00005903/*
5904 * Close window "win" and take care of handling closing the last window for a
5905 * modified buffer.
5906 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005907 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005908ex_win_close(
5909 int forceit,
5910 win_T *win,
5911 tabpage_T *tp) /* NULL or the tab page "win" is in */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005912{
5913 int need_hide;
5914 buf_T *buf = win->w_buffer;
5915
5916 need_hide = (bufIsChanged(buf) && buf->b_nwindows <= 1);
Bram Moolenaareb44a682017-08-03 22:44:55 +02005917 if (need_hide && !buf_hide(buf) && !forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005918 {
Bram Moolenaar4033c552017-09-16 20:54:51 +02005919#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005920 if ((p_confirm || cmdmod.confirm) && p_write)
5921 {
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005922 bufref_T bufref;
5923
5924 set_bufref(&bufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005925 dialog_changed(buf, FALSE);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005926 if (bufref_valid(&bufref) && bufIsChanged(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005927 return;
5928 need_hide = FALSE;
5929 }
5930 else
Bram Moolenaar4033c552017-09-16 20:54:51 +02005931#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005932 {
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02005933 no_write_message();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005934 return;
5935 }
5936 }
5937
Bram Moolenaar4033c552017-09-16 20:54:51 +02005938#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00005939 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005940#endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00005941
Bram Moolenaar071d4272004-06-13 20:20:40 +00005942 /* free buffer when not hiding it or when it's a scratch buffer */
Bram Moolenaarf740b292006-02-16 22:11:02 +00005943 if (tp == NULL)
Bram Moolenaareb44a682017-08-03 22:44:55 +02005944 win_close(win, !need_hide && !buf_hide(buf));
Bram Moolenaarf740b292006-02-16 22:11:02 +00005945 else
Bram Moolenaareb44a682017-08-03 22:44:55 +02005946 win_close_othertab(win, !need_hide && !buf_hide(buf), tp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005947}
5948
Bram Moolenaar071d4272004-06-13 20:20:40 +00005949/*
Bram Moolenaardea25702017-01-29 15:18:10 +01005950 * Handle the argument for a tabpage related ex command.
5951 * Returns a tabpage number.
5952 * When an error is encountered then eap->errmsg is set.
5953 */
5954 static int
5955get_tabpage_arg(exarg_T *eap)
5956{
5957 int tab_number;
5958 int unaccept_arg0 = (eap->cmdidx == CMD_tabmove) ? 0 : 1;
5959
5960 if (eap->arg && *eap->arg != NUL)
5961 {
5962 char_u *p = eap->arg;
5963 char_u *p_save;
5964 int relative = 0; /* argument +N/-N means: go to N places to the
5965 * right/left relative to the current position. */
5966
5967 if (*p == '-')
5968 {
5969 relative = -1;
5970 p++;
5971 }
5972 else if (*p == '+')
5973 {
5974 relative = 1;
5975 p++;
5976 }
5977
5978 p_save = p;
5979 tab_number = getdigits(&p);
5980
5981 if (relative == 0)
5982 {
5983 if (STRCMP(p, "$") == 0)
5984 tab_number = LAST_TAB_NR;
5985 else if (p == p_save || *p_save == '-' || *p != NUL
5986 || tab_number > LAST_TAB_NR)
5987 {
5988 /* No numbers as argument. */
5989 eap->errmsg = e_invarg;
5990 goto theend;
5991 }
5992 }
5993 else
5994 {
5995 if (*p_save == NUL)
5996 tab_number = 1;
5997 else if (p == p_save || *p_save == '-' || *p != NUL
5998 || tab_number == 0)
5999 {
6000 /* No numbers as argument. */
6001 eap->errmsg = e_invarg;
6002 goto theend;
6003 }
6004 tab_number = tab_number * relative + tabpage_index(curtab);
6005 if (!unaccept_arg0 && relative == -1)
6006 --tab_number;
6007 }
6008 if (tab_number < unaccept_arg0 || tab_number > LAST_TAB_NR)
6009 eap->errmsg = e_invarg;
6010 }
6011 else if (eap->addr_count > 0)
6012 {
6013 if (unaccept_arg0 && eap->line2 == 0)
Bram Moolenaarc6251552017-01-29 21:42:20 +01006014 {
Bram Moolenaardea25702017-01-29 15:18:10 +01006015 eap->errmsg = e_invrange;
Bram Moolenaarc6251552017-01-29 21:42:20 +01006016 tab_number = 0;
6017 }
Bram Moolenaardea25702017-01-29 15:18:10 +01006018 else
6019 {
6020 tab_number = eap->line2;
Bram Moolenaar82a12462019-01-22 22:41:42 +01006021 if (!unaccept_arg0 && *skipwhite(*eap->cmdlinep) == '-')
Bram Moolenaardea25702017-01-29 15:18:10 +01006022 {
6023 --tab_number;
6024 if (tab_number < unaccept_arg0)
6025 eap->errmsg = e_invarg;
6026 }
6027 }
6028 }
6029 else
6030 {
6031 switch (eap->cmdidx)
6032 {
6033 case CMD_tabnext:
6034 tab_number = tabpage_index(curtab) + 1;
6035 if (tab_number > LAST_TAB_NR)
6036 tab_number = 1;
6037 break;
6038 case CMD_tabmove:
6039 tab_number = LAST_TAB_NR;
6040 break;
6041 default:
6042 tab_number = tabpage_index(curtab);
6043 }
6044 }
6045
6046theend:
6047 return tab_number;
6048}
6049
6050/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00006051 * ":tabclose": close current tab page, unless it is the last one.
6052 * ":tabclose N": close tab page N.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006053 */
6054 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006055ex_tabclose(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006056{
Bram Moolenaarf740b292006-02-16 22:11:02 +00006057 tabpage_T *tp;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006058 int tab_number;
Bram Moolenaarf740b292006-02-16 22:11:02 +00006059
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006060# ifdef FEAT_CMDWIN
6061 if (cmdwin_type != 0)
6062 cmdwin_result = K_IGNORE;
6063 else
6064# endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00006065 if (first_tabpage->tp_next == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006066 emsg(_("E784: Cannot close last tab page"));
Bram Moolenaarf740b292006-02-16 22:11:02 +00006067 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006068 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006069 tab_number = get_tabpage_arg(eap);
6070 if (eap->errmsg == NULL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006071 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006072 tp = find_tabpage(tab_number);
Bram Moolenaarf740b292006-02-16 22:11:02 +00006073 if (tp == NULL)
6074 {
6075 beep_flush();
6076 return;
6077 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00006078 if (tp != curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006079 {
6080 tabpage_close_other(tp, eap->forceit);
6081 return;
6082 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006083 else if (!text_locked() && !curbuf_locked())
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006084 tabpage_close(eap->forceit);
6085 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006086 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006087}
6088
6089/*
6090 * ":tabonly": close all tab pages except the current one
6091 */
6092 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006093ex_tabonly(exarg_T *eap)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006094{
6095 tabpage_T *tp;
6096 int done;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006097 int tab_number;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006098
6099# ifdef FEAT_CMDWIN
6100 if (cmdwin_type != 0)
6101 cmdwin_result = K_IGNORE;
6102 else
6103# endif
6104 if (first_tabpage->tp_next == NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01006105 msg(_("Already only one tab page"));
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006106 else
6107 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006108 tab_number = get_tabpage_arg(eap);
6109 if (eap->errmsg == NULL)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006110 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006111 goto_tabpage(tab_number);
6112 /* Repeat this up to a 1000 times, because autocommands may
6113 * mess up the lists. */
6114 for (done = 0; done < 1000; ++done)
6115 {
6116 FOR_ALL_TABPAGES(tp)
6117 if (tp->tp_topframe != topframe)
6118 {
6119 tabpage_close_other(tp, eap->forceit);
6120 /* if we failed to close it quit */
6121 if (valid_tabpage(tp))
6122 done = 1000;
6123 /* start over, "tp" is now invalid */
6124 break;
6125 }
6126 if (first_tabpage->tp_next == NULL)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006127 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006128 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006129 }
6130 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006131}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006132
6133/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00006134 * Close the current tab page.
6135 */
6136 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006137tabpage_close(int forceit)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006138{
6139 /* First close all the windows but the current one. If that worked then
6140 * close the last window in this tab, that will close it. */
Bram Moolenaar459ca562016-11-10 18:16:33 +01006141 if (!ONE_WINDOW)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006142 close_others(TRUE, forceit);
Bram Moolenaar459ca562016-11-10 18:16:33 +01006143 if (ONE_WINDOW)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006144 ex_win_close(forceit, curwin, NULL);
6145# ifdef FEAT_GUI
6146 need_mouse_correct = TRUE;
6147# endif
6148}
6149
6150/*
6151 * Close tab page "tp", which is not the current tab page.
6152 * Note that autocommands may make "tp" invalid.
Bram Moolenaar7875acc2006-09-10 13:51:17 +00006153 * Also takes care of the tab pages line disappearing when closing the
6154 * last-but-one tab page.
Bram Moolenaarf740b292006-02-16 22:11:02 +00006155 */
6156 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006157tabpage_close_other(tabpage_T *tp, int forceit)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006158{
6159 int done = 0;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006160 win_T *wp;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00006161 int h = tabline_height();
Bram Moolenaarf740b292006-02-16 22:11:02 +00006162
6163 /* Limit to 1000 windows, autocommands may add a window while we close
6164 * one. OK, so I'm paranoid... */
6165 while (++done < 1000)
6166 {
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006167 wp = tp->tp_firstwin;
6168 ex_win_close(forceit, wp, tp);
Bram Moolenaarf740b292006-02-16 22:11:02 +00006169
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006170 /* Autocommands may delete the tab page under our fingers and we may
6171 * fail to close a window with a modified buffer. */
6172 if (!valid_tabpage(tp) || tp->tp_firstwin == wp)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006173 break;
6174 }
Bram Moolenaar7875acc2006-09-10 13:51:17 +00006175
Bram Moolenaar29323592016-07-24 22:04:11 +02006176 apply_autocmds(EVENT_TABCLOSED, NULL, NULL, FALSE, curbuf);
Bram Moolenaar29323592016-07-24 22:04:11 +02006177
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006178 redraw_tabline = TRUE;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00006179 if (h != tabline_height())
6180 shell_new_rows();
Bram Moolenaarf740b292006-02-16 22:11:02 +00006181}
6182
6183/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006184 * ":only".
6185 */
6186 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006187ex_only(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006188{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01006189 win_T *wp;
6190 int wnr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006191# ifdef FEAT_GUI
6192 need_mouse_correct = TRUE;
6193# endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01006194 if (eap->addr_count > 0)
6195 {
6196 wnr = eap->line2;
6197 for (wp = firstwin; --wnr > 0; )
6198 {
6199 if (wp->w_next == NULL)
6200 break;
6201 else
6202 wp = wp->w_next;
6203 }
6204 win_goto(wp);
6205 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006206 close_others(TRUE, eap->forceit);
6207}
6208
6209/*
6210 * ":all" and ":sall".
Bram Moolenaard9967712006-03-11 21:18:15 +00006211 * Also used for ":tab drop file ..." after setting the argument list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006212 */
Bram Moolenaard9967712006-03-11 21:18:15 +00006213 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006214ex_all(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006215{
6216 if (eap->addr_count == 0)
6217 eap->line2 = 9999;
Bram Moolenaard9967712006-03-11 21:18:15 +00006218 do_arg_all((int)eap->line2, eap->forceit, eap->cmdidx == CMD_drop);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006219}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006220
6221 static void
Bram Moolenaar5e1e6d22017-01-02 17:26:00 +01006222ex_hide(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006223{
Bram Moolenaar2256c992016-11-15 21:17:07 +01006224 /* ":hide" or ":hide | cmd": hide current window */
Bram Moolenaar2256c992016-11-15 21:17:07 +01006225 if (!eap->skip)
6226 {
Bram Moolenaar4033c552017-09-16 20:54:51 +02006227#ifdef FEAT_GUI
Bram Moolenaar2256c992016-11-15 21:17:07 +01006228 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006229#endif
Bram Moolenaar2256c992016-11-15 21:17:07 +01006230 if (eap->addr_count == 0)
6231 win_close(curwin, FALSE); /* don't free buffer */
6232 else
6233 {
6234 int winnr = 0;
6235 win_T *win;
Bram Moolenaarf240e182014-11-27 18:33:02 +01006236
Bram Moolenaar2256c992016-11-15 21:17:07 +01006237 FOR_ALL_WINDOWS(win)
6238 {
6239 winnr++;
6240 if (winnr == eap->line2)
6241 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01006242 }
Bram Moolenaar2256c992016-11-15 21:17:07 +01006243 if (win == NULL)
6244 win = lastwin;
6245 win_close(win, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006246 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006247 }
6248}
6249
6250/*
6251 * ":stop" and ":suspend": Suspend Vim.
6252 */
6253 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006254ex_stop(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006255{
6256 /*
6257 * Disallow suspending for "rvim".
6258 */
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006259 if (!check_restricted())
Bram Moolenaar071d4272004-06-13 20:20:40 +00006260 {
6261 if (!eap->forceit)
6262 autowrite_all();
6263 windgoto((int)Rows - 1, 0);
6264 out_char('\n');
6265 out_flush();
6266 stoptermcap();
6267 out_flush(); /* needed for SUN to restore xterm buffer */
6268#ifdef FEAT_TITLE
Bram Moolenaar40385db2018-08-07 22:31:44 +02006269 mch_restore_title(SAVE_RESTORE_BOTH); /* restore window titles */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006270#endif
6271 ui_suspend(); /* call machine specific function */
6272#ifdef FEAT_TITLE
6273 maketitle();
6274 resettitle(); /* force updating the title */
6275#endif
6276 starttermcap();
6277 scroll_start(); /* scroll screen before redrawing */
6278 redraw_later_clear();
6279 shell_resized(); /* may have resized window */
6280 }
6281}
6282
6283/*
Bram Moolenaar12a96de2018-03-11 14:44:18 +01006284 * ":exit", ":xit" and ":wq": Write file and quite the current window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006285 */
6286 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006287ex_exit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006288{
6289#ifdef FEAT_CMDWIN
6290 if (cmdwin_type != 0)
6291 {
6292 cmdwin_result = Ctrl_C;
6293 return;
6294 }
6295#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006296 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006297 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006298 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006299 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006300 return;
6301 }
Bram Moolenaar12a96de2018-03-11 14:44:18 +01006302
6303 if (before_quit_autocmds(curwin, FALSE, eap->forceit))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006304 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006305
6306 /*
6307 * if more files or windows we won't exit
6308 */
6309 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
6310 exiting = TRUE;
6311 if ( ((eap->cmdidx == CMD_wq
6312 || curbufIsChanged())
6313 && do_write(eap) == FAIL)
6314 || check_more(TRUE, eap->forceit) == FAIL
Bram Moolenaar027387f2016-01-02 22:25:52 +01006315 || (only_one_window() && check_changed_any(eap->forceit, FALSE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006316 {
6317 not_exiting();
6318 }
6319 else
6320 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006321 if (only_one_window()) /* quit last window, exit Vim */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006322 getout(0);
Bram Moolenaar2c33d7b2017-10-14 16:06:20 +02006323 not_exiting();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006324# ifdef FEAT_GUI
6325 need_mouse_correct = TRUE;
6326# endif
Bram Moolenaar1a4a75c2013-07-28 16:03:06 +02006327 /* Quit current window, may free the buffer. */
Bram Moolenaareb44a682017-08-03 22:44:55 +02006328 win_close(curwin, !buf_hide(curwin->w_buffer));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006329 }
6330}
6331
6332/*
6333 * ":print", ":list", ":number".
6334 */
6335 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006336ex_print(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006337{
Bram Moolenaardf177f62005-02-22 08:39:57 +00006338 if (curbuf->b_ml.ml_flags & ML_EMPTY)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006339 emsg(_(e_emptybuf));
Bram Moolenaardf177f62005-02-22 08:39:57 +00006340 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006341 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00006342 for ( ;!got_int; ui_breakcheck())
6343 {
6344 print_line(eap->line1,
6345 (eap->cmdidx == CMD_number || eap->cmdidx == CMD_pound
6346 || (eap->flags & EXFLAG_NR)),
6347 eap->cmdidx == CMD_list || (eap->flags & EXFLAG_LIST));
6348 if (++eap->line1 > eap->line2)
6349 break;
6350 out_flush(); /* show one line at a time */
6351 }
6352 setpcmark();
6353 /* put cursor at last line */
6354 curwin->w_cursor.lnum = eap->line2;
6355 beginline(BL_SOL | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006356 }
6357
Bram Moolenaar071d4272004-06-13 20:20:40 +00006358 ex_no_reprint = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006359}
6360
6361#ifdef FEAT_BYTEOFF
6362 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006363ex_goto(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006364{
6365 goto_byte(eap->line2);
6366}
6367#endif
6368
6369/*
6370 * ":shell".
6371 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006372 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006373ex_shell(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006374{
6375 do_shell(NULL, 0);
6376}
6377
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006378#if defined(HAVE_DROP_FILE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006379
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006380static int drop_busy = FALSE;
6381static int drop_filec;
6382static char_u **drop_filev = NULL;
6383static int drop_split;
6384static void (*drop_callback)(void *);
6385static void *drop_cookie;
6386
6387 static void
6388handle_drop_internal(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006389{
6390 exarg_T ea;
6391 int save_msg_scroll = msg_scroll;
6392
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006393 // Setting the argument list may cause screen updates and being called
6394 // recursively. Avoid that by setting drop_busy.
6395 drop_busy = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006396
6397 /* Check whether the current buffer is changed. If so, we will need
6398 * to split the current window or data could be lost.
6399 * We don't need to check if the 'hidden' option is set, as in this
6400 * case the buffer won't be lost.
6401 */
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006402 if (!buf_hide(curbuf) && !drop_split)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006403 {
6404 ++emsg_off;
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006405 drop_split = check_changed(curbuf, CCGD_AW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006406 --emsg_off;
6407 }
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006408 if (drop_split)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006409 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006410 if (win_split(0, 0) == FAIL)
6411 return;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02006412 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006413
6414 /* When splitting the window, create a new alist. Otherwise the
6415 * existing one is overwritten. */
6416 alist_unlink(curwin->w_alist);
6417 alist_new();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006418 }
6419
6420 /*
6421 * Set up the new argument list.
6422 */
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006423 alist_set(ALIST(curwin), drop_filec, drop_filev, FALSE, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006424
6425 /*
6426 * Move to the first file.
6427 */
6428 /* Fake up a minimal "next" command for do_argfile() */
6429 vim_memset(&ea, 0, sizeof(ea));
6430 ea.cmd = (char_u *)"next";
6431 do_argfile(&ea, 0);
6432
6433 /* do_ecmd() may set need_start_insertmode, but since we never left Insert
6434 * mode that is not needed here. */
6435 need_start_insertmode = FALSE;
6436
6437 /* Restore msg_scroll, otherwise a following command may cause scrolling
6438 * unexpectedly. The screen will be redrawn by the caller, thus
6439 * msg_scroll being set by displaying a message is irrelevant. */
6440 msg_scroll = save_msg_scroll;
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006441
6442 if (drop_callback != NULL)
6443 drop_callback(drop_cookie);
6444
6445 drop_filev = NULL;
6446 drop_busy = FALSE;
6447}
6448
6449/*
6450 * Handle a file drop. The code is here because a drop is *nearly* like an
6451 * :args command, but not quite (we have a list of exact filenames, so we
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01006452 * don't want to (a) parse a command line, or (b) expand wildcards). So the
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006453 * code is very similar to :args and hence needs access to a lot of the static
6454 * functions in this file.
6455 *
6456 * The "filev" list must have been allocated using alloc(), as should each item
6457 * in the list. This function takes over responsibility for freeing the "filev"
6458 * list.
6459 */
6460 void
6461handle_drop(
6462 int filec, // the number of files dropped
6463 char_u **filev, // the list of files dropped
6464 int split, // force splitting the window
6465 void (*callback)(void *), // to be called after setting the argument
6466 // list
6467 void *cookie) // argument for "callback" (allocated)
6468{
6469 // Cannot handle recursive drops, finish the pending one.
6470 if (drop_busy)
6471 {
6472 FreeWild(filec, filev);
6473 vim_free(cookie);
6474 return;
6475 }
6476
6477 // When calling handle_drop() more than once in a row we only use the last
6478 // one.
6479 if (drop_filev != NULL)
6480 {
6481 FreeWild(drop_filec, drop_filev);
6482 vim_free(drop_cookie);
6483 }
6484
6485 drop_filec = filec;
6486 drop_filev = filev;
6487 drop_split = split;
6488 drop_callback = callback;
6489 drop_cookie = cookie;
6490
6491 // Postpone this when:
6492 // - editing the command line
6493 // - not possible to change the current buffer
6494 // - updating the screen
6495 // As it may change buffers and window structures that are in use and cause
6496 // freed memory to be used.
6497 if (text_locked() || curbuf_locked() || updating_screen)
6498 return;
6499
6500 handle_drop_internal();
6501}
6502
6503/*
6504 * To be called when text is unlocked, curbuf is unlocked or updating_screen is
6505 * reset: Handle a postponed drop.
6506 */
6507 void
6508handle_any_postponed_drop(void)
6509{
6510 if (!drop_busy && drop_filev != NULL
6511 && !text_locked() && !curbuf_locked() && !updating_screen)
6512 handle_drop_internal();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006513}
6514#endif
6515
Bram Moolenaar071d4272004-06-13 20:20:40 +00006516/*
6517 * Clear an argument list: free all file names and reset it to zero entries.
6518 */
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00006519 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006520alist_clear(alist_T *al)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006521{
6522 while (--al->al_ga.ga_len >= 0)
6523 vim_free(AARGLIST(al)[al->al_ga.ga_len].ae_fname);
6524 ga_clear(&al->al_ga);
6525}
6526
6527/*
6528 * Init an argument list.
6529 */
6530 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006531alist_init(alist_T *al)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006532{
6533 ga_init2(&al->al_ga, (int)sizeof(aentry_T), 5);
6534}
6535
Bram Moolenaar071d4272004-06-13 20:20:40 +00006536/*
6537 * Remove a reference from an argument list.
6538 * Ignored when the argument list is the global one.
6539 * If the argument list is no longer used by any window, free it.
6540 */
6541 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006542alist_unlink(alist_T *al)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006543{
6544 if (al != &global_alist && --al->al_refcount <= 0)
6545 {
6546 alist_clear(al);
6547 vim_free(al);
6548 }
6549}
6550
Bram Moolenaar071d4272004-06-13 20:20:40 +00006551/*
6552 * Create a new argument list and use it for the current window.
6553 */
6554 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006555alist_new(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006556{
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006557 curwin->w_alist = ALLOC_ONE(alist_T);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006558 if (curwin->w_alist == NULL)
6559 {
6560 curwin->w_alist = &global_alist;
6561 ++global_alist.al_refcount;
6562 }
6563 else
6564 {
6565 curwin->w_alist->al_refcount = 1;
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02006566 curwin->w_alist->id = ++max_alist_id;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006567 alist_init(curwin->w_alist);
6568 }
6569}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006570
Bram Moolenaara06ecab2016-07-16 14:47:36 +02006571#if !defined(UNIX) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006572/*
6573 * Expand the file names in the global argument list.
Bram Moolenaar86b68352004-12-27 21:59:20 +00006574 * If "fnum_list" is not NULL, use "fnum_list[fnum_len]" as a list of buffer
6575 * numbers to be re-used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006576 */
6577 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006578alist_expand(int *fnum_list, int fnum_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006579{
6580 char_u **old_arg_files;
Bram Moolenaar86b68352004-12-27 21:59:20 +00006581 int old_arg_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006582 char_u **new_arg_files;
6583 int new_arg_file_count;
6584 char_u *save_p_su = p_su;
6585 int i;
6586
6587 /* Don't use 'suffixes' here. This should work like the shell did the
6588 * expansion. Also, the vimrc file isn't read yet, thus the user
6589 * can't set the options. */
6590 p_su = empty_option;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006591 old_arg_files = ALLOC_MULT(char_u *, GARGCOUNT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006592 if (old_arg_files != NULL)
6593 {
6594 for (i = 0; i < GARGCOUNT; ++i)
Bram Moolenaar86b68352004-12-27 21:59:20 +00006595 old_arg_files[i] = vim_strsave(GARGLIST[i].ae_fname);
6596 old_arg_count = GARGCOUNT;
6597 if (expand_wildcards(old_arg_count, old_arg_files,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006598 &new_arg_file_count, &new_arg_files,
Bram Moolenaarb5609832011-07-20 15:04:58 +02006599 EW_FILE|EW_NOTFOUND|EW_ADDSLASH|EW_NOERROR) == OK
Bram Moolenaar071d4272004-06-13 20:20:40 +00006600 && new_arg_file_count > 0)
6601 {
Bram Moolenaar86b68352004-12-27 21:59:20 +00006602 alist_set(&global_alist, new_arg_file_count, new_arg_files,
6603 TRUE, fnum_list, fnum_len);
6604 FreeWild(old_arg_count, old_arg_files);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006605 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006606 }
6607 p_su = save_p_su;
6608}
6609#endif
6610
6611/*
6612 * Set the argument list for the current window.
6613 * Takes over the allocated files[] and the allocated fnames in it.
6614 */
6615 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006616alist_set(
6617 alist_T *al,
6618 int count,
6619 char_u **files,
6620 int use_curbuf,
6621 int *fnum_list,
6622 int fnum_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006623{
6624 int i;
Bram Moolenaar9e33efd2018-02-09 17:50:28 +01006625 static int recursive = 0;
6626
6627 if (recursive)
6628 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006629 emsg(_(e_au_recursive));
Bram Moolenaar9e33efd2018-02-09 17:50:28 +01006630 return;
6631 }
6632 ++recursive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006633
6634 alist_clear(al);
6635 if (ga_grow(&al->al_ga, count) == OK)
6636 {
6637 for (i = 0; i < count; ++i)
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00006638 {
6639 if (got_int)
6640 {
6641 /* When adding many buffers this can take a long time. Allow
6642 * interrupting here. */
6643 while (i < count)
6644 vim_free(files[i++]);
6645 break;
6646 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00006647
6648 /* May set buffer name of a buffer previously used for the
6649 * argument list, so that it's re-used by alist_add. */
6650 if (fnum_list != NULL && i < fnum_len)
6651 buf_set_name(fnum_list[i], files[i]);
6652
Bram Moolenaar071d4272004-06-13 20:20:40 +00006653 alist_add(al, files[i], use_curbuf ? 2 : 1);
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00006654 ui_breakcheck();
6655 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006656 vim_free(files);
6657 }
6658 else
6659 FreeWild(count, files);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006660 if (al == &global_alist)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006661 arg_had_last = FALSE;
Bram Moolenaar9e33efd2018-02-09 17:50:28 +01006662
6663 --recursive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006664}
6665
6666/*
6667 * Add file "fname" to argument list "al".
6668 * "fname" must have been allocated and "al" must have been checked for room.
6669 */
6670 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006671alist_add(
6672 alist_T *al,
6673 char_u *fname,
6674 int set_fnum) /* 1: set buffer number; 2: re-use curbuf */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006675{
6676 if (fname == NULL) /* don't add NULL file names */
6677 return;
6678#ifdef BACKSLASH_IN_FILENAME
6679 slash_adjust(fname);
6680#endif
6681 AARGLIST(al)[al->al_ga.ga_len].ae_fname = fname;
6682 if (set_fnum > 0)
6683 AARGLIST(al)[al->al_ga.ga_len].ae_fnum =
6684 buflist_add(fname, BLN_LISTED | (set_fnum == 2 ? BLN_CURBUF : 0));
6685 ++al->al_ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006686}
6687
6688#if defined(BACKSLASH_IN_FILENAME) || defined(PROTO)
6689/*
6690 * Adjust slashes in file names. Called after 'shellslash' was set.
6691 */
6692 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006693alist_slash_adjust(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006694{
6695 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006696 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00006697 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006698
6699 for (i = 0; i < GARGCOUNT; ++i)
6700 if (GARGLIST[i].ae_fname != NULL)
6701 slash_adjust(GARGLIST[i].ae_fname);
Bram Moolenaarf740b292006-02-16 22:11:02 +00006702 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006703 if (wp->w_alist != &global_alist)
6704 for (i = 0; i < WARGCOUNT(wp); ++i)
6705 if (WARGLIST(wp)[i].ae_fname != NULL)
6706 slash_adjust(WARGLIST(wp)[i].ae_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006707}
6708#endif
6709
6710/*
6711 * ":preserve".
6712 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006713 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006714ex_preserve(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006715{
Bram Moolenaar4399ef42005-02-12 14:29:27 +00006716 curbuf->b_flags |= BF_PRESERVED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006717 ml_preserve(curbuf, TRUE);
6718}
6719
6720/*
6721 * ":recover".
6722 */
6723 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006724ex_recover(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006725{
6726 /* Set recoverymode right away to avoid the ATTENTION prompt. */
6727 recoverymode = TRUE;
Bram Moolenaar45d3b142013-11-09 03:31:51 +01006728 if (!check_changed(curbuf, (p_awa ? CCGD_AW : 0)
6729 | CCGD_MULTWIN
6730 | (eap->forceit ? CCGD_FORCEIT : 0)
6731 | CCGD_EXCMD)
6732
Bram Moolenaar071d4272004-06-13 20:20:40 +00006733 && (*eap->arg == NUL
6734 || setfname(curbuf, eap->arg, NULL, TRUE) == OK))
Bram Moolenaar99499b12019-05-23 21:35:48 +02006735 ml_recover(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006736 recoverymode = FALSE;
6737}
6738
6739/*
6740 * Command modifier used in a wrong way.
6741 */
6742 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006743ex_wrongmodifier(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006744{
6745 eap->errmsg = e_invcmd;
6746}
6747
Bram Moolenaar071d4272004-06-13 20:20:40 +00006748/*
6749 * :sview [+command] file split window with new file, read-only
6750 * :split [[+command] file] split window with current or new file
6751 * :vsplit [[+command] file] split window vertically with current or new file
6752 * :new [[+command] file] split window with no or new file
6753 * :vnew [[+command] file] split vertically window with no or new file
6754 * :sfind [+command] file split window with file in 'path'
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006755 *
6756 * :tabedit open new Tab page with empty window
6757 * :tabedit [+command] file open new Tab page and edit "file"
6758 * :tabnew [[+command] file] just like :tabedit
6759 * :tabfind [+command] file open new Tab page and find "file"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006760 */
6761 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006762ex_splitview(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006763{
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006764 win_T *old_curwin = curwin;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006765#if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006766 char_u *fname = NULL;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006767#endif
6768#ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00006769 int browse_flag = cmdmod.browse;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006770#endif
Bram Moolenaar39902a02018-06-21 22:10:08 +02006771 int use_tab = eap->cmdidx == CMD_tabedit
6772 || eap->cmdidx == CMD_tabfind
6773 || eap->cmdidx == CMD_tabnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006774
Bram Moolenaar815b76b2019-06-01 14:15:52 +02006775 if (NOT_IN_POPUP_WINDOW)
6776 return;
6777
Bram Moolenaar4033c552017-09-16 20:54:51 +02006778#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00006779 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006780#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006781
Bram Moolenaar4033c552017-09-16 20:54:51 +02006782#ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00006783 /* A ":split" in the quickfix window works like ":new". Don't want two
Bram Moolenaar05bb9532008-07-04 09:44:11 +00006784 * quickfix windows. But it's OK when doing ":tab split". */
6785 if (bt_quickfix(curbuf) && cmdmod.tab == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006786 {
6787 if (eap->cmdidx == CMD_split)
6788 eap->cmdidx = CMD_new;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006789 if (eap->cmdidx == CMD_vsplit)
6790 eap->cmdidx = CMD_vnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006791 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02006792#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006793
Bram Moolenaar4033c552017-09-16 20:54:51 +02006794#ifdef FEAT_SEARCHPATH
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006795 if (eap->cmdidx == CMD_sfind || eap->cmdidx == CMD_tabfind)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006796 {
6797 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg),
6798 FNAME_MESS, TRUE, curbuf->b_ffname);
6799 if (fname == NULL)
6800 goto theend;
6801 eap->arg = fname;
6802 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006803# ifdef FEAT_BROWSE
Bram Moolenaar4033c552017-09-16 20:54:51 +02006804 else
6805# endif
6806#endif
6807#ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00006808 if (cmdmod.browse
Bram Moolenaar071d4272004-06-13 20:20:40 +00006809 && eap->cmdidx != CMD_vnew
Bram Moolenaar071d4272004-06-13 20:20:40 +00006810 && eap->cmdidx != CMD_new)
6811 {
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006812 if (
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006813# ifdef FEAT_GUI
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006814 !gui.in_use &&
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006815# endif
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006816 au_has_group((char_u *)"FileExplorer"))
6817 {
6818 /* No browsing supported but we do have the file explorer:
6819 * Edit the directory. */
6820 if (*eap->arg == NUL || !mch_isdir(eap->arg))
6821 eap->arg = (char_u *)".";
6822 }
6823 else
6824 {
Bram Moolenaar39902a02018-06-21 22:10:08 +02006825 fname = do_browse(0, (char_u *)(use_tab
6826 ? _("Edit File in new tab page")
6827 : _("Edit File in new window")),
Bram Moolenaar071d4272004-06-13 20:20:40 +00006828 eap->arg, NULL, NULL, NULL, curbuf);
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006829 if (fname == NULL)
6830 goto theend;
6831 eap->arg = fname;
6832 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006833 }
6834 cmdmod.browse = FALSE; /* Don't browse again in do_ecmd(). */
Bram Moolenaar4033c552017-09-16 20:54:51 +02006835#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006836
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006837 /*
6838 * Either open new tab page or split the window.
6839 */
Bram Moolenaar39902a02018-06-21 22:10:08 +02006840 if (use_tab)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006841 {
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006842 if (win_new_tabpage(cmdmod.tab != 0 ? cmdmod.tab
6843 : eap->addr_count == 0 ? 0
6844 : (int)eap->line2 + 1) != FAIL)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006845 {
Bram Moolenaard2b66012008-01-09 19:30:36 +00006846 do_exedit(eap, old_curwin);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006847
6848 /* set the alternate buffer for the window we came from */
6849 if (curwin != old_curwin
6850 && win_valid(old_curwin)
6851 && old_curwin->w_buffer != curbuf
6852 && !cmdmod.keepalt)
6853 old_curwin->w_alt_fnum = curbuf->b_fnum;
6854 }
6855 }
6856 else if (win_split(eap->addr_count > 0 ? (int)eap->line2 : 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006857 *eap->cmd == 'v' ? WSP_VERT : 0) != FAIL)
6858 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006859 /* Reset 'scrollbind' when editing another file, but keep it when
6860 * doing ":split" without arguments. */
6861 if (*eap->arg != NUL
Bram Moolenaar8a3bb562018-03-04 20:14:14 +01006862# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00006863 || cmdmod.browse
Bram Moolenaar8a3bb562018-03-04 20:14:14 +01006864# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006865 )
Bram Moolenaar3368ea22010-09-21 16:56:35 +02006866 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006867 else
6868 do_check_scrollbind(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006869 do_exedit(eap, old_curwin);
6870 }
6871
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006872# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00006873 cmdmod.browse = browse_flag;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006874# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006875
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006876# if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006877theend:
6878 vim_free(fname);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006879# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006880}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006881
6882/*
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006883 * Open a new tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006884 */
6885 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006886tabpage_new(void)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006887{
6888 exarg_T ea;
6889
6890 vim_memset(&ea, 0, sizeof(ea));
6891 ea.cmdidx = CMD_tabnew;
6892 ea.cmd = (char_u *)"tabn";
6893 ea.arg = (char_u *)"";
6894 ex_splitview(&ea);
6895}
6896
6897/*
6898 * :tabnext command
6899 */
6900 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006901ex_tabnext(exarg_T *eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006902{
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006903 int tab_number;
6904
Bram Moolenaar815b76b2019-06-01 14:15:52 +02006905 if (NOT_IN_POPUP_WINDOW)
6906 return;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006907 switch (eap->cmdidx)
6908 {
6909 case CMD_tabfirst:
6910 case CMD_tabrewind:
6911 goto_tabpage(1);
6912 break;
6913 case CMD_tablast:
6914 goto_tabpage(9999);
6915 break;
6916 case CMD_tabprevious:
6917 case CMD_tabNext:
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006918 if (eap->arg && *eap->arg != NUL)
6919 {
6920 char_u *p = eap->arg;
6921 char_u *p_save = p;
6922
6923 tab_number = getdigits(&p);
6924 if (p == p_save || *p_save == '-' || *p != NUL
6925 || tab_number == 0)
6926 {
6927 /* No numbers as argument. */
6928 eap->errmsg = e_invarg;
6929 return;
6930 }
6931 }
6932 else
6933 {
6934 if (eap->addr_count == 0)
6935 tab_number = 1;
6936 else
6937 {
6938 tab_number = eap->line2;
6939 if (tab_number < 1)
6940 {
6941 eap->errmsg = e_invrange;
6942 return;
6943 }
6944 }
6945 }
6946 goto_tabpage(-tab_number);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006947 break;
6948 default: /* CMD_tabnext */
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006949 tab_number = get_tabpage_arg(eap);
6950 if (eap->errmsg == NULL)
6951 goto_tabpage(tab_number);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006952 break;
6953 }
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006954}
6955
6956/*
6957 * :tabmove command
6958 */
6959 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006960ex_tabmove(exarg_T *eap)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006961{
Bram Moolenaar40ce3a42015-04-21 18:08:39 +02006962 int tab_number;
Bram Moolenaar8cb8dca2012-07-06 18:27:39 +02006963
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006964 tab_number = get_tabpage_arg(eap);
6965 if (eap->errmsg == NULL)
6966 tabpage_move(tab_number);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006967}
6968
6969/*
6970 * :tabs command: List tabs and their contents.
6971 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006972 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006973ex_tabs(exarg_T *eap UNUSED)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006974{
6975 tabpage_T *tp;
6976 win_T *wp;
6977 int tabcount = 1;
6978
6979 msg_start();
6980 msg_scroll = TRUE;
6981 for (tp = first_tabpage; tp != NULL && !got_int; tp = tp->tp_next)
6982 {
6983 msg_putchar('\n');
6984 vim_snprintf((char *)IObuff, IOSIZE, _("Tab page %d"), tabcount++);
Bram Moolenaar8820b482017-03-16 17:23:31 +01006985 msg_outtrans_attr(IObuff, HL_ATTR(HLF_T));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006986 out_flush(); /* output one line at a time */
6987 ui_breakcheck();
6988
Bram Moolenaar030f0df2006-02-21 22:02:53 +00006989 if (tp == curtab)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006990 wp = firstwin;
6991 else
6992 wp = tp->tp_firstwin;
6993 for ( ; wp != NULL && !got_int; wp = wp->w_next)
6994 {
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006995 msg_putchar('\n');
6996 msg_putchar(wp == curwin ? '>' : ' ');
6997 msg_putchar(' ');
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006998 msg_putchar(bufIsChanged(wp->w_buffer) ? '+' : ' ');
6999 msg_putchar(' ');
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007000 if (buf_spname(wp->w_buffer) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02007001 vim_strncpy(IObuff, buf_spname(wp->w_buffer), IOSIZE - 1);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00007002 else
7003 home_replace(wp->w_buffer, wp->w_buffer->b_fname,
7004 IObuff, IOSIZE, TRUE);
7005 msg_outtrans(IObuff);
7006 out_flush(); /* output one line at a time */
7007 ui_breakcheck();
7008 }
7009 }
7010}
7011
Bram Moolenaar071d4272004-06-13 20:20:40 +00007012/*
7013 * ":mode": Set screen mode.
7014 * If no argument given, just get the screen size and redraw.
7015 */
7016 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007017ex_mode(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007018{
7019 if (*eap->arg == NUL)
7020 shell_resized();
7021 else
7022 mch_screenmode(eap->arg);
7023}
7024
Bram Moolenaar071d4272004-06-13 20:20:40 +00007025/*
7026 * ":resize".
7027 * set, increment or decrement current window height
7028 */
7029 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007030ex_resize(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007031{
7032 int n;
7033 win_T *wp = curwin;
7034
7035 if (eap->addr_count > 0)
7036 {
7037 n = eap->line2;
7038 for (wp = firstwin; wp->w_next != NULL && --n > 0; wp = wp->w_next)
7039 ;
7040 }
7041
Bram Moolenaar44a2f922016-03-19 22:11:51 +01007042# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007043 need_mouse_correct = TRUE;
Bram Moolenaar44a2f922016-03-19 22:11:51 +01007044# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007045 n = atol((char *)eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007046 if (cmdmod.split & WSP_VERT)
7047 {
7048 if (*eap->arg == '-' || *eap->arg == '+')
Bram Moolenaar02631462017-09-22 15:20:32 +02007049 n += curwin->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007050 else if (n == 0 && eap->arg[0] == NUL) /* default is very wide */
7051 n = 9999;
7052 win_setwidth_win((int)n, wp);
7053 }
7054 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007055 {
7056 if (*eap->arg == '-' || *eap->arg == '+')
7057 n += curwin->w_height;
Bram Moolenaar1f2903c2017-07-23 19:51:01 +02007058 else if (n == 0 && eap->arg[0] == NUL) /* default is very high */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007059 n = 9999;
7060 win_setheight_win((int)n, wp);
7061 }
7062}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007063
7064/*
7065 * ":find [+command] <file>" command.
7066 */
7067 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007068ex_find(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007069{
7070#ifdef FEAT_SEARCHPATH
7071 char_u *fname;
7072 int count;
7073
7074 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg), FNAME_MESS,
7075 TRUE, curbuf->b_ffname);
7076 if (eap->addr_count > 0)
7077 {
7078 /* Repeat finding the file "count" times. This matters when it
7079 * appears several times in the path. */
7080 count = eap->line2;
7081 while (fname != NULL && --count > 0)
7082 {
7083 vim_free(fname);
7084 fname = find_file_in_path(NULL, 0, FNAME_MESS,
7085 FALSE, curbuf->b_ffname);
7086 }
7087 }
7088
7089 if (fname != NULL)
7090 {
7091 eap->arg = fname;
7092#endif
7093 do_exedit(eap, NULL);
7094#ifdef FEAT_SEARCHPATH
7095 vim_free(fname);
7096 }
7097#endif
7098}
7099
7100/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00007101 * ":open" simulation: for now just work like ":visual".
7102 */
7103 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007104ex_open(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007105{
7106 regmatch_T regmatch;
7107 char_u *p;
7108
7109 curwin->w_cursor.lnum = eap->line2;
7110 beginline(BL_SOL | BL_FIX);
7111 if (*eap->arg == '/')
7112 {
7113 /* ":open /pattern/": put cursor in column found with pattern */
7114 ++eap->arg;
7115 p = skip_regexp(eap->arg, '/', p_magic, NULL);
7116 *p = NUL;
7117 regmatch.regprog = vim_regcomp(eap->arg, p_magic ? RE_MAGIC : 0);
7118 if (regmatch.regprog != NULL)
7119 {
7120 regmatch.rm_ic = p_ic;
7121 p = ml_get_curline();
7122 if (vim_regexec(&regmatch, p, (colnr_T)0))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007123 curwin->w_cursor.col = (colnr_T)(regmatch.startp[0] - p);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007124 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007125 emsg(_(e_nomatch));
Bram Moolenaar473de612013-06-08 18:19:48 +02007126 vim_regfree(regmatch.regprog);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007127 }
7128 /* Move to the NUL, ignore any other arguments. */
7129 eap->arg += STRLEN(eap->arg);
7130 }
7131 check_cursor();
7132
7133 eap->cmdidx = CMD_visual;
7134 do_exedit(eap, NULL);
7135}
7136
7137/*
7138 * ":edit", ":badd", ":visual".
Bram Moolenaar071d4272004-06-13 20:20:40 +00007139 */
7140 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007141ex_edit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007142{
7143 do_exedit(eap, NULL);
7144}
7145
7146/*
7147 * ":edit <file>" command and alikes.
7148 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007149 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007150do_exedit(
7151 exarg_T *eap,
7152 win_T *old_curwin) /* curwin before doing a split or NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007153{
7154 int n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007155 int need_hide;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007156 int exmode_was = exmode_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007157
Bram Moolenaar815b76b2019-06-01 14:15:52 +02007158 if (NOT_IN_POPUP_WINDOW)
7159 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007160 /*
7161 * ":vi" command ends Ex mode.
7162 */
7163 if (exmode_active && (eap->cmdidx == CMD_visual
7164 || eap->cmdidx == CMD_view))
7165 {
7166 exmode_active = FALSE;
7167 if (*eap->arg == NUL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007168 {
7169 /* Special case: ":global/pat/visual\NLvi-commands" */
7170 if (global_busy)
7171 {
7172 int rd = RedrawingDisabled;
7173 int nwr = no_wait_return;
7174 int ms = msg_scroll;
7175#ifdef FEAT_GUI
7176 int he = hold_gui_events;
7177#endif
7178
7179 if (eap->nextcmd != NULL)
7180 {
7181 stuffReadbuff(eap->nextcmd);
7182 eap->nextcmd = NULL;
7183 }
7184
7185 if (exmode_was != EXMODE_VIM)
7186 settmode(TMODE_RAW);
7187 RedrawingDisabled = 0;
7188 no_wait_return = 0;
7189 need_wait_return = FALSE;
7190 msg_scroll = 0;
7191#ifdef FEAT_GUI
7192 hold_gui_events = 0;
7193#endif
7194 must_redraw = CLEAR;
7195
7196 main_loop(FALSE, TRUE);
7197
7198 RedrawingDisabled = rd;
7199 no_wait_return = nwr;
7200 msg_scroll = ms;
7201#ifdef FEAT_GUI
7202 hold_gui_events = he;
7203#endif
7204 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007205 return;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007206 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007207 }
7208
7209 if ((eap->cmdidx == CMD_new
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007210 || eap->cmdidx == CMD_tabnew
7211 || eap->cmdidx == CMD_tabedit
Bram Moolenaar4033c552017-09-16 20:54:51 +02007212 || eap->cmdidx == CMD_vnew) && *eap->arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007213 {
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007214 /* ":new" or ":tabnew" without argument: edit an new empty buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007215 setpcmark();
7216 (void)do_ecmd(0, NULL, NULL, eap, ECMD_ONE,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00007217 ECMD_HIDE + (eap->forceit ? ECMD_FORCEIT : 0),
7218 old_curwin == NULL ? curwin : NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007219 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02007220 else if ((eap->cmdidx != CMD_split && eap->cmdidx != CMD_vsplit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007221 || *eap->arg != NUL
7222#ifdef FEAT_BROWSE
7223 || cmdmod.browse
7224#endif
7225 )
7226 {
Bram Moolenaar5555acc2006-04-07 21:33:12 +00007227 /* Can't edit another file when "curbuf_lock" is set. Only ":edit"
7228 * can bring us here, others are stopped earlier. */
7229 if (*eap->arg != NUL && curbuf_locked())
7230 return;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007231
Bram Moolenaar071d4272004-06-13 20:20:40 +00007232 n = readonlymode;
7233 if (eap->cmdidx == CMD_view || eap->cmdidx == CMD_sview)
7234 readonlymode = TRUE;
7235 else if (eap->cmdidx == CMD_enew)
7236 readonlymode = FALSE; /* 'readonly' doesn't make sense in an
7237 empty buffer */
7238 setpcmark();
7239 if (do_ecmd(0, (eap->cmdidx == CMD_enew ? NULL : eap->arg),
7240 NULL, eap,
7241 /* ":edit" goes to first line if Vi compatible */
7242 (*eap->arg == NUL && eap->do_ecmd_lnum == 0
7243 && vim_strchr(p_cpo, CPO_GOTO1) != NULL)
7244 ? ECMD_ONE : eap->do_ecmd_lnum,
Bram Moolenaareb44a682017-08-03 22:44:55 +02007245 (buf_hide(curbuf) ? ECMD_HIDE : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007246 + (eap->forceit ? ECMD_FORCEIT : 0)
Bram Moolenaar7b449342014-03-25 13:03:48 +01007247 /* after a split we can use an existing buffer */
7248 + (old_curwin != NULL ? ECMD_OLDBUF : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007249 + (eap->cmdidx == CMD_badd ? ECMD_ADDBUF : 0 )
Bram Moolenaar701f7af2008-11-15 13:12:07 +00007250 , old_curwin == NULL ? curwin : NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007251 {
7252 /* Editing the file failed. If the window was split, close it. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007253 if (old_curwin != NULL)
7254 {
7255 need_hide = (curbufIsChanged() && curbuf->b_nwindows <= 1);
Bram Moolenaareb44a682017-08-03 22:44:55 +02007256 if (!need_hide || buf_hide(curbuf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007257 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007258#if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007259 cleanup_T cs;
7260
7261 /* Reset the error/interrupt/exception state here so that
7262 * aborting() returns FALSE when closing a window. */
7263 enter_cleanup(&cs);
Bram Moolenaar4033c552017-09-16 20:54:51 +02007264#endif
7265#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007266 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02007267#endif
Bram Moolenaareb44a682017-08-03 22:44:55 +02007268 win_close(curwin, !need_hide && !buf_hide(curbuf));
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007269
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007270#if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007271 /* Restore the error/interrupt/exception state if not
7272 * discarded by a new aborting error, interrupt, or
7273 * uncaught exception. */
7274 leave_cleanup(&cs);
Bram Moolenaar4033c552017-09-16 20:54:51 +02007275#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007276 }
7277 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007278 }
7279 else if (readonlymode && curbuf->b_nwindows == 1)
7280 {
7281 /* When editing an already visited buffer, 'readonly' won't be set
7282 * but the previous value is kept. With ":view" and ":sview" we
7283 * want the file to be readonly, except when another window is
7284 * editing the same buffer. */
7285 curbuf->b_p_ro = TRUE;
7286 }
7287 readonlymode = n;
7288 }
7289 else
7290 {
7291 if (eap->do_ecmd_cmd != NULL)
7292 do_cmdline_cmd(eap->do_ecmd_cmd);
7293#ifdef FEAT_TITLE
7294 n = curwin->w_arg_idx_invalid;
7295#endif
7296 check_arg_idx(curwin);
7297#ifdef FEAT_TITLE
7298 if (n != curwin->w_arg_idx_invalid)
7299 maketitle();
7300#endif
7301 }
7302
Bram Moolenaar071d4272004-06-13 20:20:40 +00007303 /*
7304 * if ":split file" worked, set alternate file name in old window to new
7305 * file
7306 */
7307 if (old_curwin != NULL
7308 && *eap->arg != NUL
7309 && curwin != old_curwin
7310 && win_valid(old_curwin)
Bram Moolenaard4755bb2004-09-02 19:12:26 +00007311 && old_curwin->w_buffer != curbuf
7312 && !cmdmod.keepalt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007313 old_curwin->w_alt_fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007314
7315 ex_no_reprint = TRUE;
7316}
7317
7318#ifndef FEAT_GUI
7319/*
7320 * ":gui" and ":gvim" when there is no GUI.
7321 */
7322 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007323ex_nogui(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007324{
7325 eap->errmsg = e_nogvim;
7326}
7327#endif
7328
Bram Moolenaar4f974752019-02-17 17:44:42 +01007329#if defined(FEAT_GUI_MSWIN) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007330 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007331ex_tearoff(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007332{
7333 gui_make_tearoff(eap->arg);
7334}
7335#endif
7336
Bram Moolenaar29a2c082018-03-05 21:06:23 +01007337#if (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK) \
7338 || defined(FEAT_TERM_POPUP_MENU)) && defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007339 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007340ex_popup(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007341{
Bram Moolenaar29a2c082018-03-05 21:06:23 +01007342# if defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK)
7343 if (gui.in_use)
7344 gui_make_popup(eap->arg, eap->forceit);
7345# ifdef FEAT_TERM_POPUP_MENU
7346 else
7347# endif
7348# endif
7349# ifdef FEAT_TERM_POPUP_MENU
7350 pum_make_popup(eap->arg, eap->forceit);
7351# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007352}
7353#endif
7354
Bram Moolenaar071d4272004-06-13 20:20:40 +00007355 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007356ex_swapname(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007357{
7358 if (curbuf->b_ml.ml_mfp == NULL || curbuf->b_ml.ml_mfp->mf_fname == NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01007359 msg(_("No swap file"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007360 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01007361 msg((char *)curbuf->b_ml.ml_mfp->mf_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007362}
7363
7364/*
7365 * ":syncbind" forces all 'scrollbind' windows to have the same relative
7366 * offset.
7367 * (1998-11-02 16:21:01 R. Edward Ralston <eralston@computer.org>)
7368 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007369 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007370ex_syncbind(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007371{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007372 win_T *wp;
Bram Moolenaardedd1b02013-12-14 13:06:17 +01007373 win_T *save_curwin = curwin;
7374 buf_T *save_curbuf = curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007375 long topline;
7376 long y;
7377 linenr_T old_linenr = curwin->w_cursor.lnum;
7378
7379 setpcmark();
7380
7381 /*
7382 * determine max topline
7383 */
7384 if (curwin->w_p_scb)
7385 {
7386 topline = curwin->w_topline;
Bram Moolenaar29323592016-07-24 22:04:11 +02007387 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007388 {
7389 if (wp->w_p_scb && wp->w_buffer)
7390 {
Bram Moolenaar375e3392019-01-31 18:26:10 +01007391 y = wp->w_buffer->b_ml.ml_line_count - get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007392 if (topline > y)
7393 topline = y;
7394 }
7395 }
7396 if (topline < 1)
7397 topline = 1;
7398 }
7399 else
7400 {
7401 topline = 1;
7402 }
7403
7404
7405 /*
Bram Moolenaardedd1b02013-12-14 13:06:17 +01007406 * Set all scrollbind windows to the same topline.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007407 */
Bram Moolenaar29323592016-07-24 22:04:11 +02007408 FOR_ALL_WINDOWS(curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007409 {
7410 if (curwin->w_p_scb)
7411 {
Bram Moolenaardedd1b02013-12-14 13:06:17 +01007412 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007413 y = topline - curwin->w_topline;
7414 if (y > 0)
7415 scrollup(y, TRUE);
7416 else
7417 scrolldown(-y, TRUE);
7418 curwin->w_scbind_pos = topline;
7419 redraw_later(VALID);
7420 cursor_correct();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007421 curwin->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007422 }
7423 }
Bram Moolenaardedd1b02013-12-14 13:06:17 +01007424 curwin = save_curwin;
7425 curbuf = save_curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007426 if (curwin->w_p_scb)
7427 {
7428 did_syncbind = TRUE;
7429 checkpcmark();
7430 if (old_linenr != curwin->w_cursor.lnum)
7431 {
7432 char_u ctrl_o[2];
7433
7434 ctrl_o[0] = Ctrl_O;
7435 ctrl_o[1] = 0;
7436 ins_typebuf(ctrl_o, REMAP_NONE, 0, TRUE, FALSE);
7437 }
7438 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007439}
7440
7441
7442 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007443ex_read(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007444{
Bram Moolenaardf177f62005-02-22 08:39:57 +00007445 int i;
7446 int empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
7447 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007448
7449 if (eap->usefilter) /* :r!cmd */
7450 do_bang(1, eap, FALSE, FALSE, TRUE);
7451 else
7452 {
7453 if (u_save(eap->line2, (linenr_T)(eap->line2 + 1)) == FAIL)
7454 return;
7455
7456#ifdef FEAT_BROWSE
7457 if (cmdmod.browse)
7458 {
7459 char_u *browseFile;
7460
Bram Moolenaar7171abe2004-10-11 10:06:20 +00007461 browseFile = do_browse(0, (char_u *)_("Append File"), eap->arg,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007462 NULL, NULL, NULL, curbuf);
7463 if (browseFile != NULL)
7464 {
7465 i = readfile(browseFile, NULL,
7466 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
7467 vim_free(browseFile);
7468 }
7469 else
7470 i = OK;
7471 }
7472 else
7473#endif
7474 if (*eap->arg == NUL)
7475 {
7476 if (check_fname() == FAIL) /* check for no file name */
7477 return;
7478 i = readfile(curbuf->b_ffname, curbuf->b_fname,
7479 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
7480 }
7481 else
7482 {
7483 if (vim_strchr(p_cpo, CPO_ALTREAD) != NULL)
7484 (void)setaltfname(eap->arg, eap->arg, (linenr_T)1);
7485 i = readfile(eap->arg, NULL,
7486 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
7487
7488 }
Bram Moolenaare13b9af2017-01-13 22:01:02 +01007489 if (i != OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007490 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007491#if defined(FEAT_EVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007492 if (!aborting())
7493#endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007494 semsg(_(e_notopen), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007495 }
7496 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00007497 {
7498 if (empty && exmode_active)
7499 {
7500 /* Delete the empty line that remains. Historically ex does
7501 * this but vi doesn't. */
7502 if (eap->line2 == 0)
7503 lnum = curbuf->b_ml.ml_line_count;
7504 else
7505 lnum = 1;
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00007506 if (*ml_get(lnum) == NUL && u_savedel(lnum, 1L) == OK)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007507 {
7508 ml_delete(lnum, FALSE);
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00007509 if (curwin->w_cursor.lnum > 1
7510 && curwin->w_cursor.lnum >= lnum)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007511 --curwin->w_cursor.lnum;
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00007512 deleted_lines_mark(lnum, 1L);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007513 }
7514 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007515 redraw_curbuf_later(VALID);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007516 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007517 }
7518}
7519
Bram Moolenaarea408852005-06-25 22:49:46 +00007520static char_u *prev_dir = NULL;
7521
7522#if defined(EXITFREE) || defined(PROTO)
7523 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007524free_cd_dir(void)
Bram Moolenaarea408852005-06-25 22:49:46 +00007525{
Bram Moolenaard23a8232018-02-10 18:45:26 +01007526 VIM_CLEAR(prev_dir);
7527 VIM_CLEAR(globaldir);
Bram Moolenaarea408852005-06-25 22:49:46 +00007528}
7529#endif
7530
Bram Moolenaarf4258302013-06-02 18:20:17 +02007531/*
7532 * Deal with the side effects of changing the current directory.
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007533 * When 'scope' is CDSCOPE_TABPAGE then this was after an ":tcd" command.
7534 * When 'scope' is CDSCOPE_WINDOW then this was after an ":lcd" command.
Bram Moolenaarf4258302013-06-02 18:20:17 +02007535 */
7536 void
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007537post_chdir(cdscope_T scope)
Bram Moolenaarf4258302013-06-02 18:20:17 +02007538{
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007539 if (scope != CDSCOPE_WINDOW)
Bram Moolenaar00aa0692019-04-27 20:37:57 +02007540 // Clear tab local directory for both :cd and :tcd
7541 VIM_CLEAR(curtab->tp_localdir);
Bram Moolenaard23a8232018-02-10 18:45:26 +01007542 VIM_CLEAR(curwin->w_localdir);
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007543 if (scope != CDSCOPE_GLOBAL)
Bram Moolenaarf4258302013-06-02 18:20:17 +02007544 {
7545 /* If still in global directory, need to remember current
7546 * directory as global directory. */
7547 if (globaldir == NULL && prev_dir != NULL)
7548 globaldir = vim_strsave(prev_dir);
7549 /* Remember this local directory for the window. */
7550 if (mch_dirname(NameBuff, MAXPATHL) == OK)
Bram Moolenaar00aa0692019-04-27 20:37:57 +02007551 {
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007552 if (scope == CDSCOPE_TABPAGE)
Bram Moolenaar00aa0692019-04-27 20:37:57 +02007553 curtab->tp_localdir = vim_strsave(NameBuff);
7554 else
7555 curwin->w_localdir = vim_strsave(NameBuff);
7556 }
Bram Moolenaarf4258302013-06-02 18:20:17 +02007557 }
7558 else
7559 {
7560 /* We are now in the global directory, no need to remember its
7561 * name. */
Bram Moolenaard23a8232018-02-10 18:45:26 +01007562 VIM_CLEAR(globaldir);
Bram Moolenaarf4258302013-06-02 18:20:17 +02007563 }
7564
7565 shorten_fnames(TRUE);
7566}
7567
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007568/*
7569 * Change directory function used by :cd/:tcd/:lcd Ex commands and the
7570 * chdir() function. If 'winlocaldir' is TRUE, then changes the window-local
7571 * directory. If 'tablocaldir' is TRUE, then changes the tab-local directory.
7572 * Otherwise changes the global directory.
7573 * Returns TRUE if the directory is successfully changed.
7574 */
7575 int
7576changedir_func(
7577 char_u *new_dir,
7578 int forceit,
7579 cdscope_T scope)
7580{
7581 char_u *tofree;
7582 int dir_differs;
7583 int retval = FALSE;
7584
7585 if (allbuf_locked())
7586 return FALSE;
7587
7588 if (vim_strchr(p_cpo, CPO_CHDIR) != NULL && curbufIsChanged() && !forceit)
7589 {
7590 emsg(_("E747: Cannot change directory, buffer is modified (add ! to override)"));
7591 return FALSE;
7592 }
7593
7594 // ":cd -": Change to previous directory
7595 if (STRCMP(new_dir, "-") == 0)
7596 {
7597 if (prev_dir == NULL)
7598 {
7599 emsg(_("E186: No previous directory"));
7600 return FALSE;
7601 }
7602 new_dir = prev_dir;
7603 }
7604
7605 // Save current directory for next ":cd -"
7606 tofree = prev_dir;
7607 if (mch_dirname(NameBuff, MAXPATHL) == OK)
7608 prev_dir = vim_strsave(NameBuff);
7609 else
7610 prev_dir = NULL;
7611
7612#if defined(UNIX) || defined(VMS)
7613 // for UNIX ":cd" means: go to home directory
7614 if (*new_dir == NUL)
7615 {
7616 // use NameBuff for home directory name
7617# ifdef VMS
7618 char_u *p;
7619
7620 p = mch_getenv((char_u *)"SYS$LOGIN");
7621 if (p == NULL || *p == NUL) // empty is the same as not set
7622 NameBuff[0] = NUL;
7623 else
7624 vim_strncpy(NameBuff, p, MAXPATHL - 1);
7625# else
7626 expand_env((char_u *)"$HOME", NameBuff, MAXPATHL);
7627# endif
7628 new_dir = NameBuff;
7629 }
7630#endif
7631 dir_differs = new_dir == NULL || prev_dir == NULL
7632 || pathcmp((char *)prev_dir, (char *)new_dir, -1) != 0;
7633 if (new_dir == NULL || (dir_differs && vim_chdir(new_dir)))
7634 emsg(_(e_failed));
7635 else
7636 {
7637 char_u *acmd_fname;
7638
7639 post_chdir(scope);
7640
7641 if (dir_differs)
7642 {
7643 if (scope == CDSCOPE_WINDOW)
7644 acmd_fname = (char_u *)"window";
7645 else if (scope == CDSCOPE_TABPAGE)
7646 acmd_fname = (char_u *)"tabpage";
7647 else
7648 acmd_fname = (char_u *)"global";
7649 apply_autocmds(EVENT_DIRCHANGED, acmd_fname, new_dir, FALSE,
7650 curbuf);
7651 }
7652 retval = TRUE;
7653 }
7654 vim_free(tofree);
7655
7656 return retval;
7657}
Bram Moolenaarea408852005-06-25 22:49:46 +00007658
Bram Moolenaar071d4272004-06-13 20:20:40 +00007659/*
Bram Moolenaar00aa0692019-04-27 20:37:57 +02007660 * ":cd", ":tcd", ":lcd", ":chdir" ":tchdir" and ":lchdir".
Bram Moolenaar071d4272004-06-13 20:20:40 +00007661 */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00007662 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007663ex_cd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007664{
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01007665 char_u *new_dir;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007666
7667 new_dir = eap->arg;
7668#if !defined(UNIX) && !defined(VMS)
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007669 // for non-UNIX ":cd" means: print current directory
Bram Moolenaar071d4272004-06-13 20:20:40 +00007670 if (*new_dir == NUL)
7671 ex_pwd(NULL);
7672 else
7673#endif
7674 {
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007675 cdscope_T scope = CDSCOPE_GLOBAL;
7676
7677 if (eap->cmdidx == CMD_lcd || eap->cmdidx == CMD_lchdir)
7678 scope = CDSCOPE_WINDOW;
7679 else if (eap->cmdidx == CMD_tcd || eap->cmdidx == CMD_tchdir)
7680 scope = CDSCOPE_TABPAGE;
7681
7682 if (changedir_func(new_dir, eap->forceit, scope))
Bram Moolenaardf177f62005-02-22 08:39:57 +00007683 {
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007684 // Echo the new current directory if the command was typed.
Bram Moolenaarfcfbc672009-07-09 18:13:49 +00007685 if (KeyTyped || p_verbose >= 5)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007686 ex_pwd(eap);
7687 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007688 }
7689}
7690
7691/*
7692 * ":pwd".
7693 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007694 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007695ex_pwd(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007696{
7697 if (mch_dirname(NameBuff, MAXPATHL) == OK)
7698 {
7699#ifdef BACKSLASH_IN_FILENAME
7700 slash_adjust(NameBuff);
7701#endif
Bram Moolenaar32526b32019-01-19 17:43:09 +01007702 msg((char *)NameBuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007703 }
7704 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007705 emsg(_("E187: Unknown"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007706}
7707
7708/*
7709 * ":=".
7710 */
7711 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007712ex_equal(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007713{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007714 smsg("%ld", (long)eap->line2);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007715 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007716}
7717
7718 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007719ex_sleep(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007720{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007721 int n;
7722 long len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007723
7724 if (cursor_valid())
7725 {
7726 n = W_WINROW(curwin) + curwin->w_wrow - msg_scrolled;
7727 if (n >= 0)
Bram Moolenaar53f81742017-09-22 14:35:51 +02007728 windgoto((int)n, curwin->w_wincol + curwin->w_wcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007729 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007730
7731 len = eap->line2;
7732 switch (*eap->arg)
7733 {
7734 case 'm': break;
7735 case NUL: len *= 1000L; break;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007736 default: semsg(_(e_invarg2), eap->arg); return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007737 }
7738 do_sleep(len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007739}
7740
7741/*
7742 * Sleep for "msec" milliseconds, but keep checking for a CTRL-C every second.
7743 */
7744 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007745do_sleep(long msec)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007746{
7747 long done;
Bram Moolenaar975b5272016-03-15 23:10:59 +01007748 long wait_now;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007749
7750 cursor_on();
Bram Moolenaarf45d7472018-09-30 18:22:26 +02007751 out_flush_cursor(FALSE, FALSE);
Bram Moolenaar975b5272016-03-15 23:10:59 +01007752 for (done = 0; !got_int && done < msec; done += wait_now)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007753 {
Bram Moolenaar975b5272016-03-15 23:10:59 +01007754 wait_now = msec - done > 1000L ? 1000L : msec - done;
7755#ifdef FEAT_TIMERS
7756 {
7757 long due_time = check_due_timer();
7758
7759 if (due_time > 0 && due_time < wait_now)
7760 wait_now = due_time;
7761 }
7762#endif
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02007763#ifdef FEAT_JOB_CHANNEL
7764 if (has_any_channel() && wait_now > 100L)
7765 wait_now = 100L;
7766#endif
Bram Moolenaar975b5272016-03-15 23:10:59 +01007767 ui_delay(wait_now, TRUE);
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02007768#ifdef FEAT_JOB_CHANNEL
7769 if (has_any_channel())
7770 ui_breakcheck_force(TRUE);
7771 else
7772#endif
7773 ui_breakcheck();
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007774#ifdef MESSAGE_QUEUE
7775 /* Process the netbeans and clientserver messages that may have been
7776 * received in the call to ui_breakcheck() when the GUI is in use. This
7777 * may occur when running a test case. */
7778 parse_queued_messages();
Bram Moolenaare3cc6d42011-10-20 21:58:34 +02007779#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007780 }
Bram Moolenaar1ebff3d2018-07-07 16:18:13 +02007781
7782 // If CTRL-C was typed to interrupt the sleep, drop the CTRL-C from the
7783 // input buffer, otherwise a following call to input() fails.
7784 if (got_int)
7785 (void)vpeekc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007786}
7787
7788 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007789do_exmap(exarg_T *eap, int isabbrev)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007790{
7791 int mode;
7792 char_u *cmdp;
7793
7794 cmdp = eap->cmd;
7795 mode = get_map_mode(&cmdp, eap->forceit || isabbrev);
7796
7797 switch (do_map((*cmdp == 'n') ? 2 : (*cmdp == 'u'),
7798 eap->arg, mode, isabbrev))
7799 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007800 case 1: emsg(_(e_invarg));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007801 break;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007802 case 2: emsg((isabbrev ? _(e_noabbr) : _(e_nomap)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007803 break;
7804 }
7805}
7806
7807/*
7808 * ":winsize" command (obsolete).
7809 */
7810 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007811ex_winsize(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007812{
7813 int w, h;
7814 char_u *arg = eap->arg;
7815 char_u *p;
7816
7817 w = getdigits(&arg);
7818 arg = skipwhite(arg);
7819 p = arg;
7820 h = getdigits(&arg);
7821 if (*p != NUL && *arg == NUL)
7822 set_shellsize(w, h, TRUE);
7823 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007824 emsg(_("E465: :winsize requires two number arguments"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007825}
7826
Bram Moolenaar071d4272004-06-13 20:20:40 +00007827 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007828ex_wincmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007829{
7830 int xchar = NUL;
7831 char_u *p;
7832
7833 if (*eap->arg == 'g' || *eap->arg == Ctrl_G)
7834 {
7835 /* CTRL-W g and CTRL-W CTRL-G have an extra command character */
7836 if (eap->arg[1] == NUL)
7837 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007838 emsg(_(e_invarg));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007839 return;
7840 }
7841 xchar = eap->arg[1];
7842 p = eap->arg + 2;
7843 }
7844 else
7845 p = eap->arg + 1;
7846
7847 eap->nextcmd = check_nextcmd(p);
7848 p = skipwhite(p);
7849 if (*p != NUL && *p != '"' && eap->nextcmd == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007850 emsg(_(e_invarg));
Bram Moolenaar12bde492011-06-13 01:19:56 +02007851 else if (!eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007852 {
7853 /* Pass flags on for ":vertical wincmd ]". */
7854 postponed_split_flags = cmdmod.split;
Bram Moolenaard326ce82007-03-11 14:48:29 +00007855 postponed_split_tab = cmdmod.tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007856 do_window(*eap->arg, eap->addr_count > 0 ? eap->line2 : 0L, xchar);
7857 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00007858 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007859 }
7860}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007861
Bram Moolenaar843ee412004-06-30 16:16:41 +00007862#if defined(FEAT_GUI) || defined(UNIX) || defined(VMS) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007863/*
7864 * ":winpos".
7865 */
7866 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007867ex_winpos(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007868{
7869 int x, y;
7870 char_u *arg = eap->arg;
7871 char_u *p;
7872
7873 if (*arg == NUL)
7874 {
Bram Moolenaar843ee412004-06-30 16:16:41 +00007875# if defined(FEAT_GUI) || defined(MSWIN)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007876# ifdef VIMDLL
7877 if (gui.in_use ? gui_mch_get_winpos(&x, &y) != FAIL :
7878 mch_get_winpos(&x, &y) != FAIL)
7879# elif defined(FEAT_GUI)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007880 if (gui.in_use && gui_mch_get_winpos(&x, &y) != FAIL)
Bram Moolenaar843ee412004-06-30 16:16:41 +00007881# else
7882 if (mch_get_winpos(&x, &y) != FAIL)
7883# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007884 {
7885 sprintf((char *)IObuff, _("Window position: X %d, Y %d"), x, y);
Bram Moolenaar32526b32019-01-19 17:43:09 +01007886 msg((char *)IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007887 }
7888 else
7889# endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007890 emsg(_("E188: Obtaining window position not implemented for this platform"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007891 }
7892 else
7893 {
7894 x = getdigits(&arg);
7895 arg = skipwhite(arg);
7896 p = arg;
7897 y = getdigits(&arg);
7898 if (*p == NUL || *arg != NUL)
7899 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007900 emsg(_("E466: :winpos requires two number arguments"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007901 return;
7902 }
7903# ifdef FEAT_GUI
7904 if (gui.in_use)
7905 gui_mch_set_winpos(x, y);
7906 else if (gui.starting)
7907 {
7908 /* Remember the coordinates for when the window is opened. */
7909 gui_win_x = x;
7910 gui_win_y = y;
7911 }
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007912# if defined(HAVE_TGETENT) || defined(VIMDLL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007913 else
7914# endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007915# endif
7916# if defined(MSWIN) && (!defined(FEAT_GUI) || defined(VIMDLL))
Bram Moolenaar843ee412004-06-30 16:16:41 +00007917 mch_set_winpos(x, y);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007918# endif
7919# ifdef HAVE_TGETENT
7920 if (*T_CWP)
7921 term_set_winpos(x, y);
7922# endif
7923 }
7924}
7925#endif
7926
7927/*
7928 * Handle command that work like operators: ":delete", ":yank", ":>" and ":<".
7929 */
7930 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007931ex_operators(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007932{
7933 oparg_T oa;
7934
7935 clear_oparg(&oa);
7936 oa.regname = eap->regname;
7937 oa.start.lnum = eap->line1;
7938 oa.end.lnum = eap->line2;
7939 oa.line_count = eap->line2 - eap->line1 + 1;
7940 oa.motion_type = MLINE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007941 virtual_op = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007942 if (eap->cmdidx != CMD_yank) /* position cursor for undo */
7943 {
7944 setpcmark();
7945 curwin->w_cursor.lnum = eap->line1;
7946 beginline(BL_SOL | BL_FIX);
7947 }
7948
Bram Moolenaard07c6e12013-11-21 14:21:40 +01007949 if (VIsual_active)
7950 end_visual_mode();
Bram Moolenaard07c6e12013-11-21 14:21:40 +01007951
Bram Moolenaar071d4272004-06-13 20:20:40 +00007952 switch (eap->cmdidx)
7953 {
7954 case CMD_delete:
7955 oa.op_type = OP_DELETE;
7956 op_delete(&oa);
7957 break;
7958
7959 case CMD_yank:
7960 oa.op_type = OP_YANK;
7961 (void)op_yank(&oa, FALSE, TRUE);
7962 break;
7963
7964 default: /* CMD_rshift or CMD_lshift */
Bram Moolenaar6e707362013-06-14 19:15:58 +02007965 if (
Bram Moolenaar071d4272004-06-13 20:20:40 +00007966#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6e707362013-06-14 19:15:58 +02007967 (eap->cmdidx == CMD_rshift) ^ curwin->w_p_rl
7968#else
7969 eap->cmdidx == CMD_rshift
Bram Moolenaar071d4272004-06-13 20:20:40 +00007970#endif
Bram Moolenaar6e707362013-06-14 19:15:58 +02007971 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00007972 oa.op_type = OP_RSHIFT;
7973 else
7974 oa.op_type = OP_LSHIFT;
7975 op_shift(&oa, FALSE, eap->amount);
7976 break;
7977 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007978 virtual_op = MAYBE;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007979 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007980}
7981
7982/*
7983 * ":put".
7984 */
7985 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007986ex_put(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007987{
7988 /* ":0put" works like ":1put!". */
7989 if (eap->line2 == 0)
7990 {
7991 eap->line2 = 1;
7992 eap->forceit = TRUE;
7993 }
7994 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007995 do_put(eap->regname, eap->forceit ? BACKWARD : FORWARD, 1L,
7996 PUT_LINE|PUT_CURSLINE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007997}
7998
7999/*
8000 * Handle ":copy" and ":move".
8001 */
8002 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008003ex_copymove(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008004{
8005 long n;
8006
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02008007 n = get_address(eap, &eap->arg, eap->addr_type, FALSE, FALSE, FALSE, 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008008 if (eap->arg == NULL) /* error detected */
8009 {
8010 eap->nextcmd = NULL;
8011 return;
8012 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00008013 get_flags(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008014
8015 /*
8016 * move or copy lines from 'eap->line1'-'eap->line2' to below line 'n'
8017 */
8018 if (n == MAXLNUM || n < 0 || n > curbuf->b_ml.ml_line_count)
8019 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008020 emsg(_(e_invaddr));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008021 return;
8022 }
8023
8024 if (eap->cmdidx == CMD_move)
8025 {
8026 if (do_move(eap->line1, eap->line2, n) == FAIL)
8027 return;
8028 }
8029 else
8030 ex_copy(eap->line1, eap->line2, n);
8031 u_clearline();
8032 beginline(BL_SOL | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008033 ex_may_print(eap);
8034}
8035
8036/*
8037 * Print the current line if flags were given to the Ex command.
8038 */
Bram Moolenaarfd3fe982014-04-01 17:49:44 +02008039 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008040ex_may_print(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008041{
8042 if (eap->flags != 0)
8043 {
8044 print_line(curwin->w_cursor.lnum, (eap->flags & EXFLAG_NR),
8045 (eap->flags & EXFLAG_LIST));
8046 ex_no_reprint = TRUE;
8047 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008048}
8049
8050/*
8051 * ":smagic" and ":snomagic".
8052 */
8053 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008054ex_submagic(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008055{
8056 int magic_save = p_magic;
8057
8058 p_magic = (eap->cmdidx == CMD_smagic);
8059 do_sub(eap);
8060 p_magic = magic_save;
8061}
8062
8063/*
8064 * ":join".
8065 */
8066 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008067ex_join(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008068{
8069 curwin->w_cursor.lnum = eap->line1;
8070 if (eap->line1 == eap->line2)
8071 {
8072 if (eap->addr_count >= 2) /* :2,2join does nothing */
8073 return;
8074 if (eap->line2 == curbuf->b_ml.ml_line_count)
8075 {
8076 beep_flush();
8077 return;
8078 }
8079 ++eap->line2;
8080 }
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02008081 (void)do_join(eap->line2 - eap->line1 + 1, !eap->forceit, TRUE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008082 beginline(BL_WHITE | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008083 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008084}
8085
8086/*
8087 * ":[addr]@r" or ":[addr]*r": execute register
8088 */
8089 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008090ex_at(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008091{
8092 int c;
Bram Moolenaar60462872009-11-03 11:40:19 +00008093 int prev_len = typebuf.tb_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008094
8095 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaar4930a762016-09-11 14:39:53 +02008096 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008097
8098#ifdef USE_ON_FLY_SCROLL
8099 dont_scroll = TRUE; /* disallow scrolling here */
8100#endif
8101
8102 /* get the register name. No name means to use the previous one */
8103 c = *eap->arg;
8104 if (c == NUL || (c == '*' && *eap->cmd == '*'))
8105 c = '@';
Bram Moolenaard333d1e2006-11-07 17:43:47 +00008106 /* Put the register in the typeahead buffer with the "silent" flag. */
8107 if (do_execreg(c, TRUE, vim_strchr(p_cpo, CPO_EXECBUF) != NULL, TRUE)
8108 == FAIL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008109 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008110 beep_flush();
Bram Moolenaardf177f62005-02-22 08:39:57 +00008111 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008112 else
8113 {
8114 int save_efr = exec_from_reg;
8115
8116 exec_from_reg = TRUE;
8117
8118 /*
8119 * Execute from the typeahead buffer.
Bram Moolenaar60462872009-11-03 11:40:19 +00008120 * Continue until the stuff buffer is empty and all added characters
8121 * have been consumed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008122 */
Bram Moolenaar60462872009-11-03 11:40:19 +00008123 while (!stuff_empty() || typebuf.tb_len > prev_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008124 (void)do_cmdline(NULL, getexline, NULL, DOCMD_NOWAIT|DOCMD_VERBOSE);
8125
8126 exec_from_reg = save_efr;
8127 }
8128}
8129
8130/*
8131 * ":!".
8132 */
8133 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008134ex_bang(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008135{
8136 do_bang(eap->addr_count, eap, eap->forceit, TRUE, TRUE);
8137}
8138
8139/*
8140 * ":undo".
8141 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008142 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +01008143ex_undo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008144{
Bram Moolenaard3667a22006-03-16 21:35:52 +00008145 if (eap->addr_count == 1) /* :undo 123 */
Bram Moolenaar730cde92010-06-27 05:18:54 +02008146 undo_time(eap->line2, FALSE, FALSE, TRUE);
Bram Moolenaard3667a22006-03-16 21:35:52 +00008147 else
8148 u_undo(1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008149}
8150
Bram Moolenaar55debbe2010-05-23 23:34:36 +02008151#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar6df6f472010-07-18 18:04:50 +02008152 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008153ex_wundo(exarg_T *eap)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02008154{
8155 char_u hash[UNDO_HASH_SIZE];
8156
8157 u_compute_hash(hash);
8158 u_write_undo(eap->arg, eap->forceit, curbuf, hash);
8159}
8160
Bram Moolenaar6df6f472010-07-18 18:04:50 +02008161 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008162ex_rundo(exarg_T *eap)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02008163{
8164 char_u hash[UNDO_HASH_SIZE];
8165
8166 u_compute_hash(hash);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02008167 u_read_undo(eap->arg, hash, NULL);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02008168}
8169#endif
8170
Bram Moolenaar071d4272004-06-13 20:20:40 +00008171/*
8172 * ":redo".
8173 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008174 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008175ex_redo(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008176{
8177 u_redo(1);
8178}
8179
8180/*
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008181 * ":earlier" and ":later".
8182 */
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008183 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008184ex_later(exarg_T *eap)
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008185{
8186 long count = 0;
8187 int sec = FALSE;
Bram Moolenaar730cde92010-06-27 05:18:54 +02008188 int file = FALSE;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008189 char_u *p = eap->arg;
8190
8191 if (*p == NUL)
8192 count = 1;
8193 else if (isdigit(*p))
8194 {
8195 count = getdigits(&p);
8196 switch (*p)
8197 {
8198 case 's': ++p; sec = TRUE; break;
8199 case 'm': ++p; sec = TRUE; count *= 60; break;
8200 case 'h': ++p; sec = TRUE; count *= 60 * 60; break;
Bram Moolenaar730cde92010-06-27 05:18:54 +02008201 case 'd': ++p; sec = TRUE; count *= 24 * 60 * 60; break;
8202 case 'f': ++p; file = TRUE; break;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008203 }
8204 }
8205
8206 if (*p != NUL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008207 semsg(_(e_invarg2), eap->arg);
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008208 else
Bram Moolenaar730cde92010-06-27 05:18:54 +02008209 undo_time(eap->cmdidx == CMD_earlier ? -count : count,
8210 sec, file, FALSE);
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008211}
8212
8213/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008214 * ":redir": start/stop redirection.
8215 */
8216 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008217ex_redir(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008218{
8219 char *mode;
8220 char_u *fname;
Bram Moolenaarca472992005-01-21 11:46:23 +00008221 char_u *arg = eap->arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008222
Bram Moolenaarba768492016-07-08 15:32:54 +02008223#ifdef FEAT_EVAL
Bram Moolenaar79815f12016-07-09 17:07:29 +02008224 if (redir_execute)
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02008225 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008226 emsg(_("E930: Cannot use :redir inside execute()"));
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02008227 return;
8228 }
Bram Moolenaarba768492016-07-08 15:32:54 +02008229#endif
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02008230
Bram Moolenaar071d4272004-06-13 20:20:40 +00008231 if (STRICMP(eap->arg, "END") == 0)
8232 close_redir();
8233 else
8234 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008235 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008236 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008237 ++arg;
8238 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008239 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008240 ++arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008241 mode = "a";
8242 }
8243 else
8244 mode = "w";
Bram Moolenaarca472992005-01-21 11:46:23 +00008245 arg = skipwhite(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008246
8247 close_redir();
8248
8249 /* Expand environment variables and "~/". */
Bram Moolenaarca472992005-01-21 11:46:23 +00008250 fname = expand_env_save(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008251 if (fname == NULL)
8252 return;
8253#ifdef FEAT_BROWSE
8254 if (cmdmod.browse)
8255 {
8256 char_u *browseFile;
8257
Bram Moolenaar7171abe2004-10-11 10:06:20 +00008258 browseFile = do_browse(BROWSE_SAVE,
8259 (char_u *)_("Save Redirection"),
Bram Moolenaarc36651b2018-04-29 12:22:56 +02008260 fname, NULL, NULL,
8261 (char_u *)_(BROWSE_FILTER_ALL_FILES), curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008262 if (browseFile == NULL)
8263 return; /* operation cancelled */
8264 vim_free(fname);
8265 fname = browseFile;
8266 eap->forceit = TRUE; /* since dialog already asked */
8267 }
8268#endif
8269
8270 redir_fd = open_exfile(fname, eap->forceit, mode);
8271 vim_free(fname);
8272 }
8273#ifdef FEAT_EVAL
Bram Moolenaarca472992005-01-21 11:46:23 +00008274 else if (*arg == '@')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008275 {
8276 /* redirect to a register a-z (resp. A-Z for appending) */
8277 close_redir();
Bram Moolenaarca472992005-01-21 11:46:23 +00008278 ++arg;
8279 if (ASCII_ISALPHA(*arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008280# ifdef FEAT_CLIPBOARD
Bram Moolenaarca472992005-01-21 11:46:23 +00008281 || *arg == '*'
Bram Moolenaar9a51c6e2006-11-14 19:25:02 +00008282 || *arg == '+'
Bram Moolenaar071d4272004-06-13 20:20:40 +00008283# endif
Bram Moolenaarca472992005-01-21 11:46:23 +00008284 || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008285 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008286 redir_reg = *arg++;
Bram Moolenaarc188b882007-10-19 14:20:54 +00008287 if (*arg == '>' && arg[1] == '>') /* append */
Bram Moolenaard9d30582005-05-18 22:10:28 +00008288 arg += 2;
Bram Moolenaarc188b882007-10-19 14:20:54 +00008289 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008290 {
Bram Moolenaarc188b882007-10-19 14:20:54 +00008291 /* Can use both "@a" and "@a>". */
Bram Moolenaar2c29bee2005-06-01 21:46:07 +00008292 if (*arg == '>')
8293 arg++;
Bram Moolenaarc188b882007-10-19 14:20:54 +00008294 /* Make register empty when not using @A-@Z and the
8295 * command is valid. */
8296 if (*arg == NUL && !isupper(redir_reg))
8297 write_reg_contents(redir_reg, (char_u *)"", -1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008298 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00008299 }
8300 if (*arg != NUL)
8301 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00008302 redir_reg = 0;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008303 semsg(_(e_invarg2), eap->arg);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008304 }
8305 }
8306 else if (*arg == '=' && arg[1] == '>')
8307 {
8308 int append;
8309
8310 /* redirect to a variable */
8311 close_redir();
8312 arg += 2;
8313
8314 if (*arg == '>')
8315 {
8316 ++arg;
8317 append = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008318 }
8319 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00008320 append = FALSE;
8321
8322 if (var_redir_start(skipwhite(arg), append) == OK)
8323 redir_vname = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008324 }
8325#endif
8326
8327 /* TODO: redirect to a buffer */
8328
Bram Moolenaar071d4272004-06-13 20:20:40 +00008329 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008330 semsg(_(e_invarg2), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008331 }
Bram Moolenaar29b2d262006-09-10 19:07:28 +00008332
8333 /* Make sure redirection is not off. Can happen for cmdline completion
8334 * that indirectly invokes a command to catch its output. */
8335 if (redir_fd != NULL
8336#ifdef FEAT_EVAL
8337 || redir_reg || redir_vname
8338#endif
8339 )
8340 redir_off = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008341}
8342
8343/*
8344 * ":redraw": force redraw
8345 */
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01008346 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008347ex_redraw(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008348{
8349 int r = RedrawingDisabled;
8350 int p = p_lz;
8351
8352 RedrawingDisabled = 0;
8353 p_lz = FALSE;
Bram Moolenaarabc39ab2017-03-01 18:04:05 +01008354 validate_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008355 update_topline();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01008356 update_screen(eap->forceit ? CLEAR : VIsual_active ? INVERTED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008357#ifdef FEAT_TITLE
8358 if (need_maketitle)
8359 maketitle();
8360#endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02008361#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
8362# ifdef VIMDLL
8363 if (!gui.in_use)
8364# endif
8365 resize_console_buf();
Bram Moolenaar78d21da2019-02-17 15:00:52 +01008366#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008367 RedrawingDisabled = r;
8368 p_lz = p;
8369
8370 /* Reset msg_didout, so that a message that's there is overwritten. */
8371 msg_didout = FALSE;
8372 msg_col = 0;
8373
Bram Moolenaar56667a52013-07-17 11:54:28 +02008374 /* No need to wait after an intentional redraw. */
8375 need_wait_return = FALSE;
8376
Bram Moolenaar071d4272004-06-13 20:20:40 +00008377 out_flush();
8378}
8379
8380/*
8381 * ":redrawstatus": force redraw of status line(s)
8382 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008383 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008384ex_redrawstatus(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008385{
Bram Moolenaar071d4272004-06-13 20:20:40 +00008386 int r = RedrawingDisabled;
8387 int p = p_lz;
8388
8389 RedrawingDisabled = 0;
8390 p_lz = FALSE;
8391 if (eap->forceit)
8392 status_redraw_all();
8393 else
8394 status_redraw_curbuf();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01008395 update_screen(VIsual_active ? INVERTED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008396 RedrawingDisabled = r;
8397 p_lz = p;
8398 out_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008399}
8400
Bram Moolenaare12bab32019-01-08 22:02:56 +01008401/*
8402 * ":redrawtabline": force redraw of the tabline
8403 */
8404 static void
8405ex_redrawtabline(exarg_T *eap UNUSED)
8406{
8407 int r = RedrawingDisabled;
8408 int p = p_lz;
8409
8410 RedrawingDisabled = 0;
8411 p_lz = FALSE;
8412
8413 draw_tabline();
8414
8415 RedrawingDisabled = r;
8416 p_lz = p;
8417 out_flush();
8418}
8419
Bram Moolenaar071d4272004-06-13 20:20:40 +00008420 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008421close_redir(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008422{
8423 if (redir_fd != NULL)
8424 {
8425 fclose(redir_fd);
8426 redir_fd = NULL;
8427 }
8428#ifdef FEAT_EVAL
8429 redir_reg = 0;
Bram Moolenaardf177f62005-02-22 08:39:57 +00008430 if (redir_vname)
8431 {
8432 var_redir_stop();
8433 redir_vname = 0;
8434 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008435#endif
8436}
8437
8438#if defined(FEAT_SESSION) && defined(USE_CRNL)
8439# define MKSESSION_NL
8440static int mksession_nl = FALSE; /* use NL only in put_eol() */
8441#endif
8442
8443/*
8444 * ":mkexrc", ":mkvimrc", ":mkview" and ":mksession".
8445 */
8446 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008447ex_mkrc(
8448 exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008449{
8450 FILE *fd;
8451 int failed = FALSE;
8452 char_u *fname;
8453#ifdef FEAT_BROWSE
8454 char_u *browseFile = NULL;
8455#endif
8456#ifdef FEAT_SESSION
8457 int view_session = FALSE;
8458 int using_vdir = FALSE; /* using 'viewdir'? */
8459 char_u *viewFile = NULL;
8460 unsigned *flagp;
8461#endif
8462
8463 if (eap->cmdidx == CMD_mksession || eap->cmdidx == CMD_mkview)
8464 {
8465#ifdef FEAT_SESSION
8466 view_session = TRUE;
8467#else
8468 ex_ni(eap);
8469 return;
8470#endif
8471 }
8472
8473#ifdef FEAT_SESSION
Bram Moolenaar8d594672009-07-01 18:18:57 +00008474 /* Use the short file name until ":lcd" is used. We also don't use the
8475 * short file name when 'acd' is set, that is checked later. */
Bram Moolenaareeefcc72007-05-01 21:21:21 +00008476 did_lcd = FALSE;
8477
Bram Moolenaar071d4272004-06-13 20:20:40 +00008478 /* ":mkview" or ":mkview 9": generate file name with 'viewdir' */
8479 if (eap->cmdidx == CMD_mkview
8480 && (*eap->arg == NUL
8481 || (vim_isdigit(*eap->arg) && eap->arg[1] == NUL)))
8482 {
8483 eap->forceit = TRUE;
8484 fname = get_view_file(*eap->arg);
8485 if (fname == NULL)
8486 return;
8487 viewFile = fname;
8488 using_vdir = TRUE;
8489 }
8490 else
8491#endif
8492 if (*eap->arg != NUL)
8493 fname = eap->arg;
8494 else if (eap->cmdidx == CMD_mkvimrc)
8495 fname = (char_u *)VIMRC_FILE;
8496#ifdef FEAT_SESSION
8497 else if (eap->cmdidx == CMD_mksession)
8498 fname = (char_u *)SESSION_FILE;
8499#endif
8500 else
8501 fname = (char_u *)EXRC_FILE;
8502
8503#ifdef FEAT_BROWSE
8504 if (cmdmod.browse)
8505 {
Bram Moolenaar7171abe2004-10-11 10:06:20 +00008506 browseFile = do_browse(BROWSE_SAVE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008507# ifdef FEAT_SESSION
8508 eap->cmdidx == CMD_mkview ? (char_u *)_("Save View") :
8509 eap->cmdidx == CMD_mksession ? (char_u *)_("Save Session") :
8510# endif
8511 (char_u *)_("Save Setup"),
Bram Moolenaarc36651b2018-04-29 12:22:56 +02008512 fname, (char_u *)"vim", NULL,
8513 (char_u *)_(BROWSE_FILTER_MACROS), NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008514 if (browseFile == NULL)
8515 goto theend;
8516 fname = browseFile;
8517 eap->forceit = TRUE; /* since dialog already asked */
8518 }
8519#endif
8520
8521#if defined(FEAT_SESSION) && defined(vim_mkdir)
8522 /* When using 'viewdir' may have to create the directory. */
8523 if (using_vdir && !mch_isdir(p_vdir))
Bram Moolenaardf177f62005-02-22 08:39:57 +00008524 vim_mkdir_emsg(p_vdir, 0755);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008525#endif
8526
8527 fd = open_exfile(fname, eap->forceit, WRITEBIN);
8528 if (fd != NULL)
8529 {
8530#ifdef FEAT_SESSION
8531 if (eap->cmdidx == CMD_mkview)
8532 flagp = &vop_flags;
8533 else
8534 flagp = &ssop_flags;
8535#endif
8536
8537#ifdef MKSESSION_NL
8538 /* "unix" in 'sessionoptions': use NL line separator */
8539 if (view_session && (*flagp & SSOP_UNIX))
8540 mksession_nl = TRUE;
8541#endif
8542
8543 /* Write the version command for :mkvimrc */
8544 if (eap->cmdidx == CMD_mkvimrc)
8545 (void)put_line(fd, "version 6.0");
8546
8547#ifdef FEAT_SESSION
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00008548 if (eap->cmdidx == CMD_mksession)
8549 {
8550 if (put_line(fd, "let SessionLoad = 1") == FAIL)
8551 failed = TRUE;
8552 }
8553
Bram Moolenaar071d4272004-06-13 20:20:40 +00008554 if (eap->cmdidx != CMD_mkview)
8555#endif
8556 {
8557 /* Write setting 'compatible' first, because it has side effects.
8558 * For that same reason only do it when needed. */
8559 if (p_cp)
8560 (void)put_line(fd, "if !&cp | set cp | endif");
8561 else
8562 (void)put_line(fd, "if &cp | set nocp | endif");
8563 }
8564
8565#ifdef FEAT_SESSION
8566 if (!view_session
8567 || (eap->cmdidx == CMD_mksession
8568 && (*flagp & SSOP_OPTIONS)))
8569#endif
8570 failed |= (makemap(fd, NULL) == FAIL
8571 || makeset(fd, OPT_GLOBAL, FALSE) == FAIL);
8572
8573#ifdef FEAT_SESSION
8574 if (!failed && view_session)
8575 {
8576 if (put_line(fd, "let s:so_save = &so | let s:siso_save = &siso | set so=0 siso=0") == FAIL)
8577 failed = TRUE;
8578 if (eap->cmdidx == CMD_mksession)
8579 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02008580 char_u *dirnow; /* current directory */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008581
Bram Moolenaard9462e32011-04-11 21:35:11 +02008582 dirnow = alloc(MAXPATHL);
8583 if (dirnow == NULL)
8584 failed = TRUE;
8585 else
8586 {
8587 /*
8588 * Change to session file's dir.
8589 */
8590 if (mch_dirname(dirnow, MAXPATHL) == FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +00008591 || mch_chdir((char *)dirnow) != 0)
Bram Moolenaard9462e32011-04-11 21:35:11 +02008592 *dirnow = NUL;
8593 if (*dirnow != NUL && (ssop_flags & SSOP_SESDIR))
8594 {
Bram Moolenaarb7407d32018-02-03 17:36:27 +01008595 if (vim_chdirfile(fname, NULL) == OK)
Bram Moolenaard9462e32011-04-11 21:35:11 +02008596 shorten_fnames(TRUE);
8597 }
8598 else if (*dirnow != NUL
8599 && (ssop_flags & SSOP_CURDIR) && globaldir != NULL)
8600 {
8601 if (mch_chdir((char *)globaldir) == 0)
8602 shorten_fnames(TRUE);
8603 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008604
Bram Moolenaard9462e32011-04-11 21:35:11 +02008605 failed |= (makeopens(fd, dirnow) == FAIL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008606
Bram Moolenaard9462e32011-04-11 21:35:11 +02008607 /* restore original dir */
8608 if (*dirnow != NUL && ((ssop_flags & SSOP_SESDIR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008609 || ((ssop_flags & SSOP_CURDIR) && globaldir != NULL)))
Bram Moolenaard9462e32011-04-11 21:35:11 +02008610 {
8611 if (mch_chdir((char *)dirnow) != 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008612 emsg(_(e_prev_dir));
Bram Moolenaard9462e32011-04-11 21:35:11 +02008613 shorten_fnames(TRUE);
8614 }
8615 vim_free(dirnow);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008616 }
8617 }
8618 else
8619 {
Bram Moolenaarf13be0d2008-01-02 14:13:10 +00008620 failed |= (put_view(fd, curwin, !using_vdir, flagp,
8621 -1) == FAIL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008622 }
8623 if (put_line(fd, "let &so = s:so_save | let &siso = s:siso_save")
8624 == FAIL)
8625 failed = TRUE;
Bram Moolenaare3c74d22019-01-12 16:29:30 +01008626# ifdef FEAT_SEARCH_EXTRA
8627 if (no_hlsearch && put_line(fd, "nohlsearch") == FAIL)
8628 failed = TRUE;
8629# endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00008630 if (put_line(fd, "doautoall SessionLoadPost") == FAIL)
8631 failed = TRUE;
Bram Moolenaar4770d092006-01-12 23:22:24 +00008632 if (eap->cmdidx == CMD_mksession)
8633 {
8634 if (put_line(fd, "unlet SessionLoad") == FAIL)
8635 failed = TRUE;
8636 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008637 }
8638#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00008639 if (put_line(fd, "\" vim: set ft=vim :") == FAIL)
8640 failed = TRUE;
8641
Bram Moolenaar071d4272004-06-13 20:20:40 +00008642 failed |= fclose(fd);
8643
8644 if (failed)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008645 emsg(_(e_write));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008646#if defined(FEAT_EVAL) && defined(FEAT_SESSION)
8647 else if (eap->cmdidx == CMD_mksession)
8648 {
8649 /* successful session write - set this_session var */
Bram Moolenaard9462e32011-04-11 21:35:11 +02008650 char_u *tbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008651
Bram Moolenaard9462e32011-04-11 21:35:11 +02008652 tbuf = alloc(MAXPATHL);
8653 if (tbuf != NULL)
8654 {
8655 if (vim_FullName(fname, tbuf, MAXPATHL, FALSE) == OK)
8656 set_vim_var_string(VV_THIS_SESSION, tbuf, -1);
8657 vim_free(tbuf);
8658 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008659 }
8660#endif
8661#ifdef MKSESSION_NL
8662 mksession_nl = FALSE;
8663#endif
8664 }
8665
8666#ifdef FEAT_BROWSE
8667theend:
8668 vim_free(browseFile);
8669#endif
8670#ifdef FEAT_SESSION
8671 vim_free(viewFile);
8672#endif
8673}
8674
Bram Moolenaardf177f62005-02-22 08:39:57 +00008675#if ((defined(FEAT_SESSION) || defined(FEAT_EVAL)) && defined(vim_mkdir)) \
8676 || defined(PROTO)
8677 int
Bram Moolenaarf1d25012016-03-03 12:22:53 +01008678vim_mkdir_emsg(char_u *name, int prot)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008679{
8680 if (vim_mkdir(name, prot) != 0)
8681 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008682 semsg(_("E739: Cannot create directory: %s"), name);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008683 return FAIL;
8684 }
8685 return OK;
8686}
8687#endif
8688
Bram Moolenaar071d4272004-06-13 20:20:40 +00008689/*
8690 * Open a file for writing for an Ex command, with some checks.
8691 * Return file descriptor, or NULL on failure.
8692 */
8693 FILE *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008694open_exfile(
8695 char_u *fname,
8696 int forceit,
8697 char *mode) /* "w" for create new file or "a" for append */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008698{
8699 FILE *fd;
8700
8701#ifdef UNIX
8702 /* with Unix it is possible to open a directory */
8703 if (mch_isdir(fname))
8704 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008705 semsg(_(e_isadir2), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008706 return NULL;
8707 }
8708#endif
8709 if (!forceit && *mode != 'a' && vim_fexists(fname))
8710 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008711 semsg(_("E189: \"%s\" exists (add ! to override)"), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008712 return NULL;
8713 }
8714
8715 if ((fd = mch_fopen((char *)fname, mode)) == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008716 semsg(_("E190: Cannot open \"%s\" for writing"), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008717
8718 return fd;
8719}
8720
8721/*
8722 * ":mark" and ":k".
8723 */
8724 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008725ex_mark(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008726{
8727 pos_T pos;
8728
8729 if (*eap->arg == NUL) /* No argument? */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008730 emsg(_(e_argreq));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008731 else if (eap->arg[1] != NUL) /* more than one character? */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008732 emsg(_(e_trailing));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008733 else
8734 {
8735 pos = curwin->w_cursor; /* save curwin->w_cursor */
8736 curwin->w_cursor.lnum = eap->line2;
8737 beginline(BL_WHITE | BL_FIX);
8738 if (setmark(*eap->arg) == FAIL) /* set mark */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008739 emsg(_("E191: Argument must be a letter or forward/backward quote"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008740 curwin->w_cursor = pos; /* restore curwin->w_cursor */
8741 }
8742}
8743
8744/*
8745 * Update w_topline, w_leftcol and the cursor position.
8746 */
8747 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008748update_topline_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008749{
8750 check_cursor(); /* put cursor on valid line */
8751 update_topline();
8752 if (!curwin->w_p_wrap)
8753 validate_cursor();
8754 update_curswant();
8755}
8756
Bram Moolenaar071d4272004-06-13 20:20:40 +00008757/*
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008758 * Save the current State and go to Normal mode.
8759 * Return TRUE if the typeahead could be saved.
8760 */
8761 int
8762save_current_state(save_state_T *sst)
8763{
8764 sst->save_msg_scroll = msg_scroll;
8765 sst->save_restart_edit = restart_edit;
8766 sst->save_msg_didout = msg_didout;
8767 sst->save_State = State;
8768 sst->save_insertmode = p_im;
8769 sst->save_finish_op = finish_op;
8770 sst->save_opcount = opcount;
Bram Moolenaarcce713d2019-03-04 11:40:12 +01008771 sst->save_reg_executing = reg_executing;
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008772
8773 msg_scroll = FALSE; /* no msg scrolling in Normal mode */
8774 restart_edit = 0; /* don't go to Insert mode */
8775 p_im = FALSE; /* don't use 'insertmode' */
8776
8777 /*
8778 * Save the current typeahead. This is required to allow using ":normal"
8779 * from an event handler and makes sure we don't hang when the argument
8780 * ends with half a command.
8781 */
8782 save_typeahead(&sst->tabuf);
8783 return sst->tabuf.typebuf_valid;
8784}
8785
8786 void
8787restore_current_state(save_state_T *sst)
8788{
8789 /* Restore the previous typeahead. */
8790 restore_typeahead(&sst->tabuf);
8791
8792 msg_scroll = sst->save_msg_scroll;
8793 restart_edit = sst->save_restart_edit;
8794 p_im = sst->save_insertmode;
8795 finish_op = sst->save_finish_op;
8796 opcount = sst->save_opcount;
Bram Moolenaarcce713d2019-03-04 11:40:12 +01008797 reg_executing = sst->save_reg_executing;
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008798 msg_didout |= sst->save_msg_didout; /* don't reset msg_didout now */
8799
8800 /* Restore the state (needed when called from a function executed for
8801 * 'indentexpr'). Update the mouse and cursor, they may have changed. */
8802 State = sst->save_State;
8803#ifdef CURSOR_SHAPE
8804 ui_cursor_shape(); /* may show different cursor shape */
8805#endif
8806}
8807
8808/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008809 * ":normal[!] {commands}": Execute normal mode commands.
8810 */
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01008811 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008812ex_normal(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008813{
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008814 save_state_T save_state;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008815 char_u *arg = NULL;
8816 int l;
8817 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008818
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008819 if (ex_normal_lock > 0)
8820 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008821 emsg(_(e_secure));
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008822 return;
8823 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008824 if (ex_normal_busy >= p_mmd)
8825 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008826 emsg(_("E192: Recursive use of :normal too deep"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008827 return;
8828 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008829
Bram Moolenaar071d4272004-06-13 20:20:40 +00008830 /*
8831 * vgetc() expects a CSI and K_SPECIAL to have been escaped. Don't do
8832 * this for the K_SPECIAL leading byte, otherwise special keys will not
8833 * work.
8834 */
8835 if (has_mbyte)
8836 {
8837 int len = 0;
8838
8839 /* Count the number of characters to be escaped. */
8840 for (p = eap->arg; *p != NUL; ++p)
8841 {
Bram Moolenaar13505972019-01-24 15:04:48 +01008842#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008843 if (*p == CSI) /* leadbyte CSI */
8844 len += 2;
Bram Moolenaar13505972019-01-24 15:04:48 +01008845#endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008846 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008847 if (*++p == K_SPECIAL /* trailbyte K_SPECIAL or CSI */
Bram Moolenaar13505972019-01-24 15:04:48 +01008848#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008849 || *p == CSI
Bram Moolenaar13505972019-01-24 15:04:48 +01008850#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008851 )
8852 len += 2;
8853 }
8854 if (len > 0)
8855 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02008856 arg = alloc(STRLEN(eap->arg) + len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008857 if (arg != NULL)
8858 {
8859 len = 0;
8860 for (p = eap->arg; *p != NUL; ++p)
8861 {
8862 arg[len++] = *p;
Bram Moolenaar13505972019-01-24 15:04:48 +01008863#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008864 if (*p == CSI)
8865 {
8866 arg[len++] = KS_EXTRA;
8867 arg[len++] = (int)KE_CSI;
8868 }
Bram Moolenaar13505972019-01-24 15:04:48 +01008869#endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008870 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008871 {
8872 arg[len++] = *++p;
8873 if (*p == K_SPECIAL)
8874 {
8875 arg[len++] = KS_SPECIAL;
8876 arg[len++] = KE_FILLER;
8877 }
Bram Moolenaar13505972019-01-24 15:04:48 +01008878#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008879 else if (*p == CSI)
8880 {
8881 arg[len++] = KS_EXTRA;
8882 arg[len++] = (int)KE_CSI;
8883 }
Bram Moolenaar13505972019-01-24 15:04:48 +01008884#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008885 }
8886 arg[len] = NUL;
8887 }
8888 }
8889 }
8890 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008891
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008892 ++ex_normal_busy;
8893 if (save_current_state(&save_state))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008894 {
8895 /*
8896 * Repeat the :normal command for each line in the range. When no
8897 * range given, execute it just once, without positioning the cursor
8898 * first.
8899 */
8900 do
8901 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008902 if (eap->addr_count != 0)
8903 {
8904 curwin->w_cursor.lnum = eap->line1++;
8905 curwin->w_cursor.col = 0;
Bram Moolenaard5d37532017-03-27 23:02:07 +02008906 check_cursor_moved(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008907 }
8908
Bram Moolenaar13505972019-01-24 15:04:48 +01008909 exec_normal_cmd(arg != NULL
8910 ? arg
8911 : eap->arg, eap->forceit ? REMAP_NONE : REMAP_YES, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008912 }
8913 while (eap->addr_count > 0 && eap->line1 <= eap->line2 && !got_int);
8914 }
8915
8916 /* Might not return to the main loop when in an event handler. */
8917 update_topline_cursor();
8918
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008919 restore_current_state(&save_state);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008920 --ex_normal_busy;
Bram Moolenaareda73602014-11-05 09:53:23 +01008921#ifdef FEAT_MOUSE
8922 setmouse();
8923#endif
8924#ifdef CURSOR_SHAPE
8925 ui_cursor_shape(); /* may show different cursor shape */
8926#endif
8927
Bram Moolenaar071d4272004-06-13 20:20:40 +00008928 vim_free(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008929}
8930
8931/*
Bram Moolenaar64969662005-12-14 21:59:55 +00008932 * ":startinsert", ":startreplace" and ":startgreplace"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008933 */
8934 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008935ex_startinsert(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008936{
Bram Moolenaarfd371682005-01-14 21:42:54 +00008937 if (eap->forceit)
8938 {
Bram Moolenaar09ca9322017-09-26 17:40:45 +02008939 /* cursor line can be zero on startup */
8940 if (!curwin->w_cursor.lnum)
8941 curwin->w_cursor.lnum = 1;
Bram Moolenaarfd371682005-01-14 21:42:54 +00008942 coladvance((colnr_T)MAXCOL);
8943 curwin->w_curswant = MAXCOL;
8944 curwin->w_set_curswant = FALSE;
8945 }
8946
Bram Moolenaara40c5002005-01-09 21:16:21 +00008947 /* Ignore the command when already in Insert mode. Inserting an
8948 * expression register that invokes a function can do this. */
8949 if (State & INSERT)
8950 return;
8951
Bram Moolenaar64969662005-12-14 21:59:55 +00008952 if (eap->cmdidx == CMD_startinsert)
8953 restart_edit = 'a';
8954 else if (eap->cmdidx == CMD_startreplace)
8955 restart_edit = 'R';
Bram Moolenaar071d4272004-06-13 20:20:40 +00008956 else
Bram Moolenaar64969662005-12-14 21:59:55 +00008957 restart_edit = 'V';
8958
8959 if (!eap->forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008960 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00008961 if (eap->cmdidx == CMD_startinsert)
8962 restart_edit = 'i';
Bram Moolenaar071d4272004-06-13 20:20:40 +00008963 curwin->w_curswant = 0; /* avoid MAXCOL */
8964 }
8965}
8966
8967/*
8968 * ":stopinsert"
8969 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008970 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008971ex_stopinsert(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008972{
8973 restart_edit = 0;
8974 stop_insert_mode = TRUE;
Bram Moolenaarfd773e92016-04-02 19:39:16 +02008975 clearmode();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008976}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008977
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008978/*
8979 * Execute normal mode command "cmd".
8980 * "remap" can be REMAP_NONE or REMAP_YES.
8981 */
8982 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008983exec_normal_cmd(char_u *cmd, int remap, int silent)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008984{
Bram Moolenaar25281632016-01-21 23:32:32 +01008985 /* Stuff the argument into the typeahead buffer. */
8986 ins_typebuf(cmd, remap, 0, TRUE, silent);
Bram Moolenaar586c70c2018-10-02 16:23:58 +02008987 exec_normal(FALSE, FALSE, FALSE);
Bram Moolenaar25281632016-01-21 23:32:32 +01008988}
Bram Moolenaar25281632016-01-21 23:32:32 +01008989
Bram Moolenaar25281632016-01-21 23:32:32 +01008990/*
8991 * Execute normal_cmd() until there is no typeahead left.
Bram Moolenaar586c70c2018-10-02 16:23:58 +02008992 * When "use_vpeekc" is TRUE use vpeekc() to check for available chars.
Bram Moolenaar25281632016-01-21 23:32:32 +01008993 */
8994 void
Bram Moolenaar586c70c2018-10-02 16:23:58 +02008995exec_normal(int was_typed, int use_vpeekc, int may_use_terminal_loop UNUSED)
Bram Moolenaar25281632016-01-21 23:32:32 +01008996{
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008997 oparg_T oa;
Bram Moolenaar905dd902019-04-07 14:21:47 +02008998 int c;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008999
Bram Moolenaar905dd902019-04-07 14:21:47 +02009000 // When calling vpeekc() from feedkeys() it will return Ctrl_C when there
9001 // is nothing to get, so also check for Ctrl_C.
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009002 clear_oparg(&oa);
9003 finish_op = FALSE;
Bram Moolenaar586c70c2018-10-02 16:23:58 +02009004 while ((!stuff_empty()
9005 || ((was_typed || !typebuf_typed()) && typebuf.tb_len > 0)
Bram Moolenaar905dd902019-04-07 14:21:47 +02009006 || (use_vpeekc && (c = vpeekc()) != NUL && c != Ctrl_C))
Bram Moolenaar586c70c2018-10-02 16:23:58 +02009007 && !got_int)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009008 {
9009 update_topline_cursor();
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02009010#ifdef FEAT_TERMINAL
Bram Moolenaar655a82a2018-05-08 22:01:07 +02009011 if (may_use_terminal_loop && term_use_loop()
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02009012 && oa.op_type == OP_NOP && oa.regname == NUL
9013 && !VIsual_active)
9014 {
9015 /* If terminal_loop() returns OK we got a key that is handled
9016 * in Normal model. With FAIL we first need to position the
9017 * cursor and the screen needs to be redrawn. */
9018 if (terminal_loop(TRUE) == OK)
9019 normal_cmd(&oa, TRUE);
9020 }
9021 else
9022#endif
9023 /* execute a Normal mode cmd */
9024 normal_cmd(&oa, TRUE);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009025 }
9026}
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009027
Bram Moolenaar071d4272004-06-13 20:20:40 +00009028#ifdef FEAT_FIND_ID
9029 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009030ex_checkpath(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009031{
9032 find_pattern_in_path(NULL, 0, 0, FALSE, FALSE, CHECK_PATH, 1L,
9033 eap->forceit ? ACTION_SHOW_ALL : ACTION_SHOW,
9034 (linenr_T)1, (linenr_T)MAXLNUM);
9035}
9036
Bram Moolenaar4033c552017-09-16 20:54:51 +02009037#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009038/*
9039 * ":psearch"
9040 */
9041 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009042ex_psearch(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009043{
9044 g_do_tagpreview = p_pvh;
9045 ex_findpat(eap);
9046 g_do_tagpreview = 0;
9047}
9048#endif
9049
9050 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009051ex_findpat(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009052{
9053 int whole = TRUE;
9054 long n;
9055 char_u *p;
9056 int action;
9057
9058 switch (cmdnames[eap->cmdidx].cmd_name[2])
9059 {
9060 case 'e': /* ":psearch", ":isearch" and ":dsearch" */
9061 if (cmdnames[eap->cmdidx].cmd_name[0] == 'p')
9062 action = ACTION_GOTO;
9063 else
9064 action = ACTION_SHOW;
9065 break;
9066 case 'i': /* ":ilist" and ":dlist" */
9067 action = ACTION_SHOW_ALL;
9068 break;
9069 case 'u': /* ":ijump" and ":djump" */
9070 action = ACTION_GOTO;
9071 break;
9072 default: /* ":isplit" and ":dsplit" */
9073 action = ACTION_SPLIT;
9074 break;
9075 }
9076
9077 n = 1;
9078 if (vim_isdigit(*eap->arg)) /* get count */
9079 {
9080 n = getdigits(&eap->arg);
9081 eap->arg = skipwhite(eap->arg);
9082 }
9083 if (*eap->arg == '/') /* Match regexp, not just whole words */
9084 {
9085 whole = FALSE;
9086 ++eap->arg;
9087 p = skip_regexp(eap->arg, '/', p_magic, NULL);
9088 if (*p)
9089 {
9090 *p++ = NUL;
9091 p = skipwhite(p);
9092
9093 /* Check for trailing illegal characters */
9094 if (!ends_excmd(*p))
9095 eap->errmsg = e_trailing;
9096 else
9097 eap->nextcmd = check_nextcmd(p);
9098 }
9099 }
9100 if (!eap->skip)
9101 find_pattern_in_path(eap->arg, 0, (int)STRLEN(eap->arg),
9102 whole, !eap->forceit,
9103 *eap->cmd == 'd' ? FIND_DEFINE : FIND_ANY,
9104 n, action, eap->line1, eap->line2);
9105}
9106#endif
9107
Bram Moolenaar071d4272004-06-13 20:20:40 +00009108
Bram Moolenaar4033c552017-09-16 20:54:51 +02009109#ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00009110/*
9111 * ":ptag", ":ptselect", ":ptjump", ":ptnext", etc.
9112 */
9113 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009114ex_ptag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009115{
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01009116 g_do_tagpreview = p_pvh; /* will be reset to 0 in ex_tag_cmd() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009117 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
9118}
9119
9120/*
9121 * ":pedit"
9122 */
9123 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009124ex_pedit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009125{
9126 win_T *curwin_save = curwin;
9127
9128 g_do_tagpreview = p_pvh;
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00009129 prepare_tagpreview(TRUE);
Bram Moolenaar67883b42017-07-27 22:57:00 +02009130 keep_help_flag = bt_help(curwin_save->w_buffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009131 do_exedit(eap, NULL);
9132 keep_help_flag = FALSE;
9133 if (curwin != curwin_save && win_valid(curwin_save))
9134 {
9135 /* Return cursor to where we were */
9136 validate_cursor();
9137 redraw_later(VALID);
9138 win_enter(curwin_save, TRUE);
9139 }
9140 g_do_tagpreview = 0;
9141}
Bram Moolenaar4033c552017-09-16 20:54:51 +02009142#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009143
9144/*
9145 * ":stag", ":stselect" and ":stjump".
9146 */
9147 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009148ex_stag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009149{
9150 postponed_split = -1;
9151 postponed_split_flags = cmdmod.split;
Bram Moolenaard326ce82007-03-11 14:48:29 +00009152 postponed_split_tab = cmdmod.tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009153 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
9154 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00009155 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009156}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009157
9158/*
9159 * ":tag", ":tselect", ":tjump", ":tnext", etc.
9160 */
9161 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009162ex_tag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009163{
9164 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name);
9165}
9166
9167 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009168ex_tag_cmd(exarg_T *eap, char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009169{
9170 int cmd;
9171
9172 switch (name[1])
9173 {
9174 case 'j': cmd = DT_JUMP; /* ":tjump" */
9175 break;
9176 case 's': cmd = DT_SELECT; /* ":tselect" */
9177 break;
9178 case 'p': cmd = DT_PREV; /* ":tprevious" */
9179 break;
9180 case 'N': cmd = DT_PREV; /* ":tNext" */
9181 break;
9182 case 'n': cmd = DT_NEXT; /* ":tnext" */
9183 break;
9184 case 'o': cmd = DT_POP; /* ":pop" */
9185 break;
9186 case 'f': /* ":tfirst" */
9187 case 'r': cmd = DT_FIRST; /* ":trewind" */
9188 break;
9189 case 'l': cmd = DT_LAST; /* ":tlast" */
9190 break;
9191 default: /* ":tag" */
9192#ifdef FEAT_CSCOPE
Bram Moolenaar7c94c262008-06-20 09:11:34 +00009193 if (p_cst && *eap->arg != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009194 {
Bram Moolenaard4db7712016-11-12 19:16:46 +01009195 ex_cstag(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009196 return;
9197 }
9198#endif
9199 cmd = DT_TAG;
9200 break;
9201 }
9202
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00009203 if (name[0] == 'l')
9204 {
9205#ifndef FEAT_QUICKFIX
9206 ex_ni(eap);
9207 return;
9208#else
9209 cmd = DT_LTAG;
9210#endif
9211 }
9212
Bram Moolenaar071d4272004-06-13 20:20:40 +00009213 do_tag(eap->arg, cmd, eap->addr_count > 0 ? (int)eap->line2 : 1,
9214 eap->forceit, TRUE);
9215}
9216
9217/*
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009218 * Check "str" for starting with a special cmdline variable.
9219 * If found return one of the SPEC_ values and set "*usedlen" to the length of
9220 * the variable. Otherwise return -1 and "*usedlen" is unchanged.
9221 */
9222 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009223find_cmdline_var(char_u *src, int *usedlen)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009224{
9225 int len;
9226 int i;
Bram Moolenaar8f0b2d42009-05-16 14:41:10 +00009227 static char *(spec_str[]) = {
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009228 "%",
9229#define SPEC_PERC 0
9230 "#",
Bram Moolenaar65f08472017-09-10 18:16:20 +02009231#define SPEC_HASH (SPEC_PERC + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009232 "<cword>", /* cursor word */
Bram Moolenaar65f08472017-09-10 18:16:20 +02009233#define SPEC_CWORD (SPEC_HASH + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009234 "<cWORD>", /* cursor WORD */
Bram Moolenaar65f08472017-09-10 18:16:20 +02009235#define SPEC_CCWORD (SPEC_CWORD + 1)
9236 "<cexpr>", /* expr under cursor */
9237#define SPEC_CEXPR (SPEC_CCWORD + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009238 "<cfile>", /* cursor path name */
Bram Moolenaar65f08472017-09-10 18:16:20 +02009239#define SPEC_CFILE (SPEC_CEXPR + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009240 "<sfile>", /* ":so" file name */
Bram Moolenaar65f08472017-09-10 18:16:20 +02009241#define SPEC_SFILE (SPEC_CFILE + 1)
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009242 "<slnum>", /* ":so" file line number */
Bram Moolenaar65f08472017-09-10 18:16:20 +02009243#define SPEC_SLNUM (SPEC_SFILE + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009244 "<afile>", /* autocommand file name */
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009245#define SPEC_AFILE (SPEC_SLNUM + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009246 "<abuf>", /* autocommand buffer number */
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009247#define SPEC_ABUF (SPEC_AFILE + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009248 "<amatch>", /* autocommand match name */
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01009249#define SPEC_AMATCH (SPEC_ABUF + 1)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009250 "<sflnum>", /* script file line number */
9251#define SPEC_SFLNUM (SPEC_AMATCH + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009252#ifdef FEAT_CLIENTSERVER
9253 "<client>"
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009254# define SPEC_CLIENT (SPEC_SFLNUM + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009255#endif
9256 };
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009257
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00009258 for (i = 0; i < (int)(sizeof(spec_str) / sizeof(char *)); ++i)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009259 {
9260 len = (int)STRLEN(spec_str[i]);
9261 if (STRNCMP(src, spec_str[i], len) == 0)
9262 {
9263 *usedlen = len;
9264 return i;
9265 }
9266 }
9267 return -1;
9268}
9269
9270/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009271 * Evaluate cmdline variables.
9272 *
9273 * change '%' to curbuf->b_ffname
9274 * '#' to curwin->w_altfile
9275 * '<cword>' to word under the cursor
9276 * '<cWORD>' to WORD under the cursor
9277 * '<cfile>' to path name under the cursor
9278 * '<sfile>' to sourced file name
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009279 * '<slnum>' to sourced file line number
Bram Moolenaar071d4272004-06-13 20:20:40 +00009280 * '<afile>' to file name for autocommand
9281 * '<abuf>' to buffer number for autocommand
9282 * '<amatch>' to matching name for autocommand
9283 *
9284 * When an error is detected, "errormsg" is set to a non-NULL pointer (may be
9285 * "" for error without a message) and NULL is returned.
9286 * Returns an allocated string if a valid match was found.
9287 * Returns NULL if no match was found. "usedlen" then still contains the
9288 * number of characters to skip.
9289 */
9290 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009291eval_vars(
9292 char_u *src, /* pointer into commandline */
9293 char_u *srcstart, /* beginning of valid memory for src */
9294 int *usedlen, /* characters after src that are used */
9295 linenr_T *lnump, /* line number for :e command, or NULL */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009296 char **errormsg, /* pointer to error message */
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009297 int *escaped) /* return value has escaped white space (can
Bram Moolenaar63b92542007-03-27 14:57:09 +00009298 * be NULL) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009299{
9300 int i;
9301 char_u *s;
9302 char_u *result;
9303 char_u *resultbuf = NULL;
9304 int resultlen;
9305 buf_T *buf;
9306 int valid = VALID_HEAD + VALID_PATH; /* assume valid result */
9307 int spec_idx;
9308#ifdef FEAT_MODIFY_FNAME
Bram Moolenaarfd249462018-07-28 16:14:30 +02009309 int tilde_file = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009310 int skip_mod = FALSE;
9311#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009312 char_u strbuf[30];
Bram Moolenaar071d4272004-06-13 20:20:40 +00009313
9314 *errormsg = NULL;
Bram Moolenaar63b92542007-03-27 14:57:09 +00009315 if (escaped != NULL)
9316 *escaped = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009317
9318 /*
9319 * Check if there is something to do.
9320 */
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009321 spec_idx = find_cmdline_var(src, usedlen);
9322 if (spec_idx < 0) /* no match */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009323 {
9324 *usedlen = 1;
9325 return NULL;
9326 }
9327
9328 /*
9329 * Skip when preceded with a backslash "\%" and "\#".
9330 * Note: In "\\%" the % is also not recognized!
9331 */
9332 if (src > srcstart && src[-1] == '\\')
9333 {
9334 *usedlen = 0;
Bram Moolenaara7241f52008-06-24 20:39:31 +00009335 STRMOVE(src - 1, src); /* remove backslash */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009336 return NULL;
9337 }
9338
9339 /*
9340 * word or WORD under cursor
9341 */
Bram Moolenaar65f08472017-09-10 18:16:20 +02009342 if (spec_idx == SPEC_CWORD || spec_idx == SPEC_CCWORD
9343 || spec_idx == SPEC_CEXPR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009344 {
Bram Moolenaar65f08472017-09-10 18:16:20 +02009345 resultlen = find_ident_under_cursor(&result,
9346 spec_idx == SPEC_CWORD ? (FIND_IDENT | FIND_STRING)
9347 : spec_idx == SPEC_CEXPR ? (FIND_IDENT | FIND_STRING | FIND_EVAL)
9348 : FIND_STRING);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009349 if (resultlen == 0)
9350 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009351 *errormsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +00009352 return NULL;
9353 }
9354 }
9355
9356 /*
9357 * '#': Alternate file name
9358 * '%': Current file name
9359 * File name under the cursor
9360 * File name for autocommand
9361 * and following modifiers
9362 */
9363 else
9364 {
9365 switch (spec_idx)
9366 {
9367 case SPEC_PERC: /* '%': current file */
9368 if (curbuf->b_fname == NULL)
9369 {
9370 result = (char_u *)"";
9371 valid = 0; /* Must have ":p:h" to be valid */
9372 }
9373 else
Bram Moolenaar00136dc2018-07-25 21:19:13 +02009374 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009375 result = curbuf->b_fname;
Bram Moolenaarfd249462018-07-28 16:14:30 +02009376#ifdef FEAT_MODIFY_FNAME
Bram Moolenaar00136dc2018-07-25 21:19:13 +02009377 tilde_file = STRCMP(result, "~") == 0;
Bram Moolenaarfd249462018-07-28 16:14:30 +02009378#endif
Bram Moolenaar00136dc2018-07-25 21:19:13 +02009379 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009380 break;
9381
9382 case SPEC_HASH: /* '#' or "#99": alternate file */
9383 if (src[1] == '#') /* "##": the argument list */
9384 {
9385 result = arg_all();
9386 resultbuf = result;
9387 *usedlen = 2;
Bram Moolenaar63b92542007-03-27 14:57:09 +00009388 if (escaped != NULL)
9389 *escaped = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009390#ifdef FEAT_MODIFY_FNAME
9391 skip_mod = TRUE;
9392#endif
9393 break;
9394 }
9395 s = src + 1;
Bram Moolenaard812df62008-11-09 12:46:09 +00009396 if (*s == '<') /* "#<99" uses v:oldfiles */
9397 ++s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009398 i = (int)getdigits(&s);
Bram Moolenaarc312b8b2017-10-28 17:53:04 +02009399 if (s == src + 2 && src[1] == '-')
9400 /* just a minus sign, don't skip over it */
9401 s--;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009402 *usedlen = (int)(s - src); /* length of what we expand */
9403
Bram Moolenaarc312b8b2017-10-28 17:53:04 +02009404 if (src[1] == '<' && i != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009405 {
Bram Moolenaard812df62008-11-09 12:46:09 +00009406 if (*usedlen < 2)
9407 {
9408 /* Should we give an error message for #<text? */
9409 *usedlen = 1;
9410 return NULL;
9411 }
9412#ifdef FEAT_EVAL
9413 result = list_find_str(get_vim_var_list(VV_OLDFILES),
9414 (long)i);
9415 if (result == NULL)
9416 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009417 *errormsg = "";
Bram Moolenaard812df62008-11-09 12:46:09 +00009418 return NULL;
9419 }
9420#else
Bram Moolenaar8e481e82019-01-15 20:07:48 +01009421 *errormsg = _("E809: #< is not available without the +eval feature");
Bram Moolenaar071d4272004-06-13 20:20:40 +00009422 return NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +00009423#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009424 }
9425 else
Bram Moolenaard812df62008-11-09 12:46:09 +00009426 {
Bram Moolenaarc312b8b2017-10-28 17:53:04 +02009427 if (i == 0 && src[1] == '<' && *usedlen > 1)
9428 *usedlen = 1;
Bram Moolenaard812df62008-11-09 12:46:09 +00009429 buf = buflist_findnr(i);
9430 if (buf == NULL)
9431 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009432 *errormsg = _("E194: No alternate file name to substitute for '#'");
Bram Moolenaard812df62008-11-09 12:46:09 +00009433 return NULL;
9434 }
9435 if (lnump != NULL)
9436 *lnump = ECMD_LAST;
9437 if (buf->b_fname == NULL)
9438 {
9439 result = (char_u *)"";
9440 valid = 0; /* Must have ":p:h" to be valid */
9441 }
9442 else
Bram Moolenaar00136dc2018-07-25 21:19:13 +02009443 {
Bram Moolenaard812df62008-11-09 12:46:09 +00009444 result = buf->b_fname;
Bram Moolenaarfd249462018-07-28 16:14:30 +02009445#ifdef FEAT_MODIFY_FNAME
Bram Moolenaar00136dc2018-07-25 21:19:13 +02009446 tilde_file = STRCMP(result, "~") == 0;
Bram Moolenaarfd249462018-07-28 16:14:30 +02009447#endif
Bram Moolenaar00136dc2018-07-25 21:19:13 +02009448 }
Bram Moolenaard812df62008-11-09 12:46:09 +00009449 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009450 break;
9451
9452#ifdef FEAT_SEARCHPATH
9453 case SPEC_CFILE: /* file name under cursor */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00009454 result = file_name_at_cursor(FNAME_MESS|FNAME_HYP, 1L, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009455 if (result == NULL)
9456 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009457 *errormsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +00009458 return NULL;
9459 }
9460 resultbuf = result; /* remember allocated string */
9461 break;
9462#endif
9463
Bram Moolenaar071d4272004-06-13 20:20:40 +00009464 case SPEC_AFILE: /* file name for autocommand */
9465 result = autocmd_fname;
Bram Moolenaarf6dad432008-09-18 19:29:58 +00009466 if (result != NULL && !autocmd_fname_full)
9467 {
9468 /* Still need to turn the fname into a full path. It is
9469 * postponed to avoid a delay when <afile> is not used. */
9470 autocmd_fname_full = TRUE;
9471 result = FullName_save(autocmd_fname, FALSE);
9472 vim_free(autocmd_fname);
9473 autocmd_fname = result;
9474 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009475 if (result == NULL)
9476 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009477 *errormsg = _("E495: no autocommand file name to substitute for \"<afile>\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00009478 return NULL;
9479 }
Bram Moolenaara0174af2008-01-02 20:08:25 +00009480 result = shorten_fname1(result);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009481 break;
9482
9483 case SPEC_ABUF: /* buffer number for autocommand */
9484 if (autocmd_bufnr <= 0)
9485 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009486 *errormsg = _("E496: no autocommand buffer number to substitute for \"<abuf>\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00009487 return NULL;
9488 }
9489 sprintf((char *)strbuf, "%d", autocmd_bufnr);
9490 result = strbuf;
9491 break;
9492
9493 case SPEC_AMATCH: /* match name for autocommand */
9494 result = autocmd_match;
9495 if (result == NULL)
9496 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009497 *errormsg = _("E497: no autocommand match name to substitute for \"<amatch>\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00009498 return NULL;
9499 }
9500 break;
9501
Bram Moolenaar071d4272004-06-13 20:20:40 +00009502 case SPEC_SFILE: /* file name for ":so" command */
9503 result = sourcing_name;
9504 if (result == NULL)
9505 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009506 *errormsg = _("E498: no :source file name to substitute for \"<sfile>\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00009507 return NULL;
9508 }
9509 break;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009510
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009511 case SPEC_SLNUM: /* line in file for ":so" command */
9512 if (sourcing_name == NULL || sourcing_lnum == 0)
9513 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009514 *errormsg = _("E842: no line number to use for \"<slnum>\"");
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009515 return NULL;
9516 }
9517 sprintf((char *)strbuf, "%ld", (long)sourcing_lnum);
9518 result = strbuf;
9519 break;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009520
9521#ifdef FEAT_EVAL
9522 case SPEC_SFLNUM: /* line in script file */
9523 if (current_sctx.sc_lnum + sourcing_lnum == 0)
9524 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009525 *errormsg = _("E961: no line number to use for \"<sflnum>\"");
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009526 return NULL;
9527 }
9528 sprintf((char *)strbuf, "%ld",
9529 (long)(current_sctx.sc_lnum + sourcing_lnum));
9530 result = strbuf;
9531 break;
9532#endif
9533
9534#ifdef FEAT_CLIENTSERVER
Bram Moolenaar071d4272004-06-13 20:20:40 +00009535 case SPEC_CLIENT: /* Source of last submitted input */
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009536 sprintf((char *)strbuf, PRINTF_HEX_LONG_U,
9537 (long_u)clientWindow);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009538 result = strbuf;
9539 break;
9540#endif
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009541
Bram Moolenaar9e0f6ec2017-05-16 09:36:54 +02009542 default:
9543 result = (char_u *)""; /* avoid gcc warning */
9544 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009545 }
9546
9547 resultlen = (int)STRLEN(result); /* length of new string */
9548 if (src[*usedlen] == '<') /* remove the file name extension */
9549 {
9550 ++*usedlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009551 if ((s = vim_strrchr(result, '.')) != NULL && s >= gettail(result))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009552 resultlen = (int)(s - result);
9553 }
9554#ifdef FEAT_MODIFY_FNAME
9555 else if (!skip_mod)
9556 {
Bram Moolenaar00136dc2018-07-25 21:19:13 +02009557 valid |= modify_fname(src, tilde_file, usedlen, &result, &resultbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009558 &resultlen);
9559 if (result == NULL)
9560 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009561 *errormsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +00009562 return NULL;
9563 }
9564 }
9565#endif
9566 }
9567
9568 if (resultlen == 0 || valid != VALID_HEAD + VALID_PATH)
9569 {
9570 if (valid != VALID_HEAD + VALID_PATH)
9571 /* xgettext:no-c-format */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009572 *errormsg = _("E499: Empty file name for '%' or '#', only works with \":p:h\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00009573 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009574 *errormsg = _("E500: Evaluates to an empty string");
Bram Moolenaar071d4272004-06-13 20:20:40 +00009575 result = NULL;
9576 }
9577 else
9578 result = vim_strnsave(result, resultlen);
9579 vim_free(resultbuf);
9580 return result;
9581}
9582
9583/*
9584 * Concatenate all files in the argument list, separated by spaces, and return
9585 * it in one allocated string.
9586 * Spaces and backslashes in the file names are escaped with a backslash.
9587 * Returns NULL when out of memory.
9588 */
9589 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009590arg_all(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009591{
9592 int len;
9593 int idx;
9594 char_u *retval = NULL;
9595 char_u *p;
9596
9597 /*
9598 * Do this loop two times:
9599 * first time: compute the total length
9600 * second time: concatenate the names
9601 */
9602 for (;;)
9603 {
9604 len = 0;
9605 for (idx = 0; idx < ARGCOUNT; ++idx)
9606 {
9607 p = alist_name(&ARGLIST[idx]);
9608 if (p != NULL)
9609 {
9610 if (len > 0)
9611 {
9612 /* insert a space in between names */
9613 if (retval != NULL)
9614 retval[len] = ' ';
9615 ++len;
9616 }
9617 for ( ; *p != NUL; ++p)
9618 {
Bram Moolenaar6e8d3b02015-06-09 21:33:31 +02009619 if (*p == ' '
9620#ifndef BACKSLASH_IN_FILENAME
9621 || *p == '\\'
9622#endif
Bram Moolenaar2c8c6812018-07-28 17:07:52 +02009623 || *p == '`')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009624 {
9625 /* insert a backslash */
9626 if (retval != NULL)
9627 retval[len] = '\\';
9628 ++len;
9629 }
9630 if (retval != NULL)
9631 retval[len] = *p;
9632 ++len;
9633 }
9634 }
9635 }
9636
9637 /* second time: break here */
9638 if (retval != NULL)
9639 {
9640 retval[len] = NUL;
9641 break;
9642 }
9643
9644 /* allocate memory */
Bram Moolenaar964b3742019-05-24 18:54:09 +02009645 retval = alloc(len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009646 if (retval == NULL)
9647 break;
9648 }
9649
9650 return retval;
9651}
9652
Bram Moolenaar071d4272004-06-13 20:20:40 +00009653/*
9654 * Expand the <sfile> string in "arg".
9655 *
9656 * Returns an allocated string, or NULL for any error.
9657 */
9658 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009659expand_sfile(char_u *arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009660{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009661 char *errormsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009662 int len;
9663 char_u *result;
9664 char_u *newres;
9665 char_u *repl;
9666 int srclen;
9667 char_u *p;
9668
9669 result = vim_strsave(arg);
9670 if (result == NULL)
9671 return NULL;
9672
9673 for (p = result; *p; )
9674 {
9675 if (STRNCMP(p, "<sfile>", 7) != 0)
9676 ++p;
9677 else
9678 {
9679 /* replace "<sfile>" with the sourced file name, and do ":" stuff */
Bram Moolenaar63b92542007-03-27 14:57:09 +00009680 repl = eval_vars(p, result, &srclen, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009681 if (errormsg != NULL)
9682 {
9683 if (*errormsg)
9684 emsg(errormsg);
9685 vim_free(result);
9686 return NULL;
9687 }
9688 if (repl == NULL) /* no match (cannot happen) */
9689 {
9690 p += srclen;
9691 continue;
9692 }
9693 len = (int)STRLEN(result) - srclen + (int)STRLEN(repl) + 1;
9694 newres = alloc(len);
9695 if (newres == NULL)
9696 {
9697 vim_free(repl);
9698 vim_free(result);
9699 return NULL;
9700 }
9701 mch_memmove(newres, result, (size_t)(p - result));
9702 STRCPY(newres + (p - result), repl);
9703 len = (int)STRLEN(newres);
9704 STRCAT(newres, p + srclen);
9705 vim_free(repl);
9706 vim_free(result);
9707 result = newres;
9708 p = newres + len; /* continue after the match */
9709 }
9710 }
9711
9712 return result;
9713}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009714
9715#ifdef FEAT_SESSION
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01009716static int ses_winsizes(FILE *fd, int restore_size,
9717 win_T *tab_firstwin);
9718static int ses_win_rec(FILE *fd, frame_T *fr);
9719static frame_T *ses_skipframe(frame_T *fr);
9720static int ses_do_frame(frame_T *fr);
9721static int ses_do_win(win_T *wp);
9722static int ses_arglist(FILE *fd, char *cmd, garray_T *gap, int fullname, unsigned *flagp);
9723static int ses_put_fname(FILE *fd, char_u *name, unsigned *flagp);
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +02009724static int ses_fname(FILE *fd, buf_T *buf, unsigned *flagp, int add_eol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009725
9726/*
9727 * Write openfile commands for the current buffers to an .exrc file.
9728 * Return FAIL on error, OK otherwise.
9729 */
9730 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009731makeopens(
9732 FILE *fd,
9733 char_u *dirnow) /* Current directory name */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009734{
9735 buf_T *buf;
9736 int only_save_windows = TRUE;
9737 int nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009738 int restore_size = TRUE;
9739 win_T *wp;
9740 char_u *sname;
9741 win_T *edited_win = NULL;
Bram Moolenaar18144c82006-04-12 21:52:12 +00009742 int tabnr;
Bram Moolenaar04ad7fe2014-05-07 21:14:47 +02009743 int restore_stal = FALSE;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +00009744 win_T *tab_firstwin;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +00009745 frame_T *tab_topframe;
Bram Moolenaarf13be0d2008-01-02 14:13:10 +00009746 int cur_arg_idx = 0;
Bram Moolenaar383c6f52008-01-04 15:01:07 +00009747 int next_arg_idx = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009748
9749 if (ssop_flags & SSOP_BUFFERS)
9750 only_save_windows = FALSE; /* Save ALL buffers */
9751
9752 /*
9753 * Begin by setting the this_session variable, and then other
9754 * sessionable variables.
9755 */
9756#ifdef FEAT_EVAL
9757 if (put_line(fd, "let v:this_session=expand(\"<sfile>:p\")") == FAIL)
9758 return FAIL;
9759 if (ssop_flags & SSOP_GLOBALS)
9760 if (store_session_globals(fd) == FAIL)
9761 return FAIL;
9762#endif
9763
9764 /*
Bram Moolenaar26d4b892018-07-04 22:26:28 +02009765 * Close all windows and tabs but one.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009766 */
9767 if (put_line(fd, "silent only") == FAIL)
9768 return FAIL;
Bram Moolenaar26d4b892018-07-04 22:26:28 +02009769 if ((ssop_flags & SSOP_TABPAGES)
9770 && put_line(fd, "silent tabonly") == FAIL)
9771 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009772
9773 /*
9774 * Now a :cd command to the session directory or the current directory
9775 */
9776 if (ssop_flags & SSOP_SESDIR)
9777 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009778 if (put_line(fd, "exe \"cd \" . escape(expand(\"<sfile>:p:h\"), ' ')")
9779 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009780 return FAIL;
9781 }
9782 else if (ssop_flags & SSOP_CURDIR)
9783 {
9784 sname = home_replace_save(NULL, globaldir != NULL ? globaldir : dirnow);
9785 if (sname == NULL
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009786 || fputs("cd ", fd) < 0
9787 || ses_put_fname(fd, sname, &ssop_flags) == FAIL
9788 || put_eol(fd) == FAIL)
9789 {
9790 vim_free(sname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009791 return FAIL;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009792 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009793 vim_free(sname);
9794 }
9795
9796 /*
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009797 * If there is an empty, unnamed buffer we will wipe it out later.
9798 * Remember the buffer number.
9799 */
9800 if (put_line(fd, "if expand('%') == '' && !&modified && line('$') <= 1 && getline(1) == ''") == FAIL)
9801 return FAIL;
9802 if (put_line(fd, " let s:wipebuf = bufnr('%')") == FAIL)
9803 return FAIL;
9804 if (put_line(fd, "endif") == FAIL)
9805 return FAIL;
9806
9807 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009808 * Now save the current files, current buffer first.
9809 */
9810 if (put_line(fd, "set shortmess=aoO") == FAIL)
9811 return FAIL;
9812
Bram Moolenaar071d4272004-06-13 20:20:40 +00009813 /* the global argument list */
Bram Moolenaarf0bdd2f2014-03-12 21:28:26 +01009814 if (ses_arglist(fd, "argglobal", &global_alist.al_ga,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009815 !(ssop_flags & SSOP_CURDIR), &ssop_flags) == FAIL)
9816 return FAIL;
9817
9818 if (ssop_flags & SSOP_RESIZE)
9819 {
9820 /* Note: after the restore we still check it worked!*/
9821 if (fprintf(fd, "set lines=%ld columns=%ld" , Rows, Columns) < 0
9822 || put_eol(fd) == FAIL)
9823 return FAIL;
9824 }
9825
9826#ifdef FEAT_GUI
9827 if (gui.in_use && (ssop_flags & SSOP_WINPOS))
9828 {
9829 int x, y;
9830
9831 if (gui_mch_get_winpos(&x, &y) == OK)
9832 {
9833 /* Note: after the restore we still check it worked!*/
9834 if (fprintf(fd, "winpos %d %d", x, y) < 0 || put_eol(fd) == FAIL)
9835 return FAIL;
9836 }
9837 }
9838#endif
9839
9840 /*
Bram Moolenaar04ad7fe2014-05-07 21:14:47 +02009841 * When there are two or more tabpages and 'showtabline' is 1 the tabline
9842 * will be displayed when creating the next tab. That resizes the windows
9843 * in the first tab, which may cause problems. Set 'showtabline' to 2
9844 * temporarily to avoid that.
9845 */
9846 if (p_stal == 1 && first_tabpage->tp_next != NULL)
9847 {
9848 if (put_line(fd, "set stal=2") == FAIL)
9849 return FAIL;
9850 restore_stal = TRUE;
9851 }
9852
9853 /*
Bram Moolenaar18144c82006-04-12 21:52:12 +00009854 * May repeat putting Windows for each tab, when "tabpages" is in
9855 * 'sessionoptions'.
Bram Moolenaarcba2ae52006-10-24 10:59:57 +00009856 * Don't use goto_tabpage(), it may change directory and trigger
9857 * autocommands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009858 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +00009859 tab_firstwin = firstwin; /* first window in tab page "tabnr" */
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +00009860 tab_topframe = topframe;
Bram Moolenaar26d4b892018-07-04 22:26:28 +02009861 if ((ssop_flags & SSOP_TABPAGES))
9862 {
Bram Moolenaar57a6bf02019-01-22 21:27:13 +01009863 tabpage_T *tp;
Bram Moolenaar26d4b892018-07-04 22:26:28 +02009864
Bram Moolenaar57a6bf02019-01-22 21:27:13 +01009865 // Similar to ses_win_rec() below, populate the tab pages first so
9866 // later local options won't be copied to the new tabs.
9867 FOR_ALL_TABPAGES(tp)
9868 if (tp->tp_next != NULL && put_line(fd, "tabnew") == FAIL)
Bram Moolenaar26d4b892018-07-04 22:26:28 +02009869 return FAIL;
Bram Moolenaar57a6bf02019-01-22 21:27:13 +01009870 if (first_tabpage->tp_next != NULL && put_line(fd, "tabrewind") == FAIL)
Bram Moolenaar26d4b892018-07-04 22:26:28 +02009871 return FAIL;
9872 }
Bram Moolenaar18144c82006-04-12 21:52:12 +00009873 for (tabnr = 1; ; ++tabnr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009874 {
Bram Moolenaar00aa0692019-04-27 20:37:57 +02009875 tabpage_T *tp = NULL;
Bram Moolenaar26d4b892018-07-04 22:26:28 +02009876 int need_tabnext = FALSE;
Bram Moolenaar695baee2015-04-13 12:39:22 +02009877 int cnr = 1;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +00009878
Bram Moolenaar18144c82006-04-12 21:52:12 +00009879 if ((ssop_flags & SSOP_TABPAGES))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009880 {
Bram Moolenaar00aa0692019-04-27 20:37:57 +02009881 tp = find_tabpage(tabnr);
Bram Moolenaarcba2ae52006-10-24 10:59:57 +00009882
9883 if (tp == NULL)
9884 break; /* done all tab pages */
9885 if (tp == curtab)
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +00009886 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +00009887 tab_firstwin = firstwin;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +00009888 tab_topframe = topframe;
9889 }
Bram Moolenaarcba2ae52006-10-24 10:59:57 +00009890 else
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +00009891 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +00009892 tab_firstwin = tp->tp_firstwin;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +00009893 tab_topframe = tp->tp_topframe;
9894 }
Bram Moolenaarcba2ae52006-10-24 10:59:57 +00009895 if (tabnr > 1)
Bram Moolenaar26d4b892018-07-04 22:26:28 +02009896 need_tabnext = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009897 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009898
Bram Moolenaar18144c82006-04-12 21:52:12 +00009899 /*
9900 * Before creating the window layout, try loading one file. If this
9901 * is aborted we don't end up with a number of useless windows.
9902 * This may have side effects! (e.g., compressed or network file).
9903 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +00009904 for (wp = tab_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar18144c82006-04-12 21:52:12 +00009905 {
9906 if (ses_do_win(wp)
9907 && wp->w_buffer->b_ffname != NULL
Bram Moolenaar67883b42017-07-27 22:57:00 +02009908 && !bt_help(wp->w_buffer)
Bram Moolenaar18144c82006-04-12 21:52:12 +00009909#ifdef FEAT_QUICKFIX
9910 && !bt_nofile(wp->w_buffer)
9911#endif
9912 )
9913 {
Bram Moolenaar26d4b892018-07-04 22:26:28 +02009914 if (need_tabnext && put_line(fd, "tabnext") == FAIL)
9915 return FAIL;
9916 need_tabnext = FALSE;
9917
9918 if (fputs("edit ", fd) < 0
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +02009919 || ses_fname(fd, wp->w_buffer, &ssop_flags, TRUE)
9920 == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +00009921 return FAIL;
9922 if (!wp->w_arg_idx_invalid)
9923 edited_win = wp;
9924 break;
9925 }
9926 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009927
Bram Moolenaarcba2ae52006-10-24 10:59:57 +00009928 /* If no file got edited create an empty tab page. */
Bram Moolenaar26d4b892018-07-04 22:26:28 +02009929 if (need_tabnext && put_line(fd, "tabnext") == FAIL)
Bram Moolenaarcba2ae52006-10-24 10:59:57 +00009930 return FAIL;
9931
Bram Moolenaar18144c82006-04-12 21:52:12 +00009932 /*
9933 * Save current window layout.
9934 */
9935 if (put_line(fd, "set splitbelow splitright") == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009936 return FAIL;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +00009937 if (ses_win_rec(fd, tab_topframe) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009938 return FAIL;
Bram Moolenaar18144c82006-04-12 21:52:12 +00009939 if (!p_sb && put_line(fd, "set nosplitbelow") == FAIL)
9940 return FAIL;
9941 if (!p_spr && put_line(fd, "set nosplitright") == FAIL)
9942 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009943
Bram Moolenaar18144c82006-04-12 21:52:12 +00009944 /*
9945 * Check if window sizes can be restored (no windows omitted).
9946 * Remember the window number of the current window after restoring.
9947 */
9948 nr = 0;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +00009949 for (wp = tab_firstwin; wp != NULL; wp = W_NEXT(wp))
Bram Moolenaar18144c82006-04-12 21:52:12 +00009950 {
9951 if (ses_do_win(wp))
9952 ++nr;
9953 else
9954 restore_size = FALSE;
9955 if (curwin == wp)
9956 cnr = nr;
9957 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009958
Bram Moolenaar18144c82006-04-12 21:52:12 +00009959 /* Go to the first window. */
9960 if (put_line(fd, "wincmd t") == FAIL)
9961 return FAIL;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009962
Bram Moolenaar18144c82006-04-12 21:52:12 +00009963 /*
9964 * If more than one window, see if sizes can be restored.
9965 * First set 'winheight' and 'winwidth' to 1 to avoid the windows being
9966 * resized when moving between windows.
9967 * Do this before restoring the view, so that the topline and the
9968 * cursor can be set. This is done again below.
Bram Moolenaar36ae89c2017-01-28 17:11:14 +01009969 * winminheight and winminwidth need to be set to avoid an error if the
9970 * user has set winheight or winwidth.
Bram Moolenaar18144c82006-04-12 21:52:12 +00009971 */
Bram Moolenaar19834012018-06-12 17:03:39 +02009972 if (put_line(fd, "set winminheight=0") == FAIL
9973 || put_line(fd, "set winheight=1") == FAIL
9974 || put_line(fd, "set winminwidth=0") == FAIL
9975 || put_line(fd, "set winwidth=1") == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +00009976 return FAIL;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +00009977 if (nr > 1 && ses_winsizes(fd, restore_size, tab_firstwin) == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +00009978 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009979
Bram Moolenaar00aa0692019-04-27 20:37:57 +02009980 // Restore the tab-local working directory if specified
9981 // Do this before the windows, so that the window-local directory can
9982 // override the tab-local directory.
9983 if (tp != NULL && tp->tp_localdir != NULL && ssop_flags & SSOP_CURDIR)
9984 {
9985 if (fputs("tcd ", fd) < 0
9986 || ses_put_fname(fd, tp->tp_localdir, &ssop_flags) == FAIL
9987 || put_eol(fd) == FAIL)
9988 return FAIL;
9989 did_lcd = TRUE;
9990 }
9991
Bram Moolenaar18144c82006-04-12 21:52:12 +00009992 /*
9993 * Restore the view of the window (options, file, cursor, etc.).
9994 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +00009995 for (wp = tab_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar18144c82006-04-12 21:52:12 +00009996 {
9997 if (!ses_do_win(wp))
9998 continue;
Bram Moolenaarf13be0d2008-01-02 14:13:10 +00009999 if (put_view(fd, wp, wp != edited_win, &ssop_flags,
10000 cur_arg_idx) == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000010001 return FAIL;
10002 if (nr > 1 && put_line(fd, "wincmd w") == FAIL)
10003 return FAIL;
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010004 next_arg_idx = wp->w_arg_idx;
Bram Moolenaar18144c82006-04-12 21:52:12 +000010005 }
10006
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010007 /* The argument index in the first tab page is zero, need to set it in
10008 * each window. For further tab pages it's the window where we do
10009 * "tabedit". */
10010 cur_arg_idx = next_arg_idx;
10011
Bram Moolenaar18144c82006-04-12 21:52:12 +000010012 /*
10013 * Restore cursor to the current window if it's not the first one.
10014 */
10015 if (cnr > 1 && (fprintf(fd, "%dwincmd w", cnr) < 0
10016 || put_eol(fd) == FAIL))
10017 return FAIL;
10018
10019 /*
Bram Moolenaar18144c82006-04-12 21:52:12 +000010020 * Restore window sizes again after jumping around in windows, because
10021 * the current window has a minimum size while others may not.
10022 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010023 if (nr > 1 && ses_winsizes(fd, restore_size, tab_firstwin) == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000010024 return FAIL;
10025
Bram Moolenaar18144c82006-04-12 21:52:12 +000010026 /* Don't continue in another tab page when doing only the current one
10027 * or when at the last tab page. */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010028 if (!(ssop_flags & SSOP_TABPAGES))
Bram Moolenaar18144c82006-04-12 21:52:12 +000010029 break;
10030 }
10031
10032 if (ssop_flags & SSOP_TABPAGES)
10033 {
Bram Moolenaar18144c82006-04-12 21:52:12 +000010034 if (fprintf(fd, "tabnext %d", tabpage_index(curtab)) < 0
10035 || put_eol(fd) == FAIL)
10036 return FAIL;
10037 }
Bram Moolenaar04ad7fe2014-05-07 21:14:47 +020010038 if (restore_stal && put_line(fd, "set stal=1") == FAIL)
10039 return FAIL;
Bram Moolenaar18144c82006-04-12 21:52:12 +000010040
Bram Moolenaard39e2752019-01-26 20:07:38 +010010041 // Now put the remaining buffers into the buffer list.
10042 // This is near the end, so that when 'hidden' is set we don't create extra
10043 // buffers. If the buffer was already created with another command the
10044 // ":badd" will have no effect.
10045 FOR_ALL_BUFFERS(buf)
10046 {
10047 if (!(only_save_windows && buf->b_nwindows == 0)
10048 && !(buf->b_help && !(ssop_flags & SSOP_HELP))
10049#ifdef FEAT_TERMINAL
10050 // Skip terminal buffers: finished ones are not useful, others
10051 // will be resurrected and result in a new buffer.
10052 && !bt_terminal(buf)
10053#endif
10054 && buf->b_fname != NULL
10055 && buf->b_p_bl)
10056 {
10057 if (fprintf(fd, "badd +%ld ", buf->b_wininfo == NULL ? 1L
10058 : buf->b_wininfo->wi_fpos.lnum) < 0
10059 || ses_fname(fd, buf, &ssop_flags, TRUE) == FAIL)
10060 return FAIL;
10061 }
10062 }
10063
Bram Moolenaar9c102382006-05-03 21:26:49 +000010064 /*
10065 * Wipe out an empty unnamed buffer we started in.
10066 */
Bram Moolenaar26d4b892018-07-04 22:26:28 +020010067 if (put_line(fd, "if exists('s:wipebuf') && len(win_findbuf(s:wipebuf)) == 0")
Bram Moolenaar4d8bac82018-03-09 21:33:34 +010010068 == FAIL)
Bram Moolenaar9c102382006-05-03 21:26:49 +000010069 return FAIL;
Bram Moolenaarf3a67882006-05-05 21:09:41 +000010070 if (put_line(fd, " silent exe 'bwipe ' . s:wipebuf") == FAIL)
Bram Moolenaar9c102382006-05-03 21:26:49 +000010071 return FAIL;
10072 if (put_line(fd, "endif") == FAIL)
10073 return FAIL;
10074 if (put_line(fd, "unlet! s:wipebuf") == FAIL)
10075 return FAIL;
10076
10077 /* Re-apply 'winheight', 'winwidth' and 'shortmess'. */
10078 if (fprintf(fd, "set winheight=%ld winwidth=%ld shortmess=%s",
10079 p_wh, p_wiw, p_shm) < 0 || put_eol(fd) == FAIL)
10080 return FAIL;
Bram Moolenaar36ae89c2017-01-28 17:11:14 +010010081 /* Re-apply 'winminheight' and 'winminwidth'. */
10082 if (fprintf(fd, "set winminheight=%ld winminwidth=%ld",
10083 p_wmh, p_wmw) < 0 || put_eol(fd) == FAIL)
10084 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010085
10086 /*
10087 * Lastly, execute the x.vim file if it exists.
10088 */
10089 if (put_line(fd, "let s:sx = expand(\"<sfile>:p:r\").\"x.vim\"") == FAIL
10090 || put_line(fd, "if file_readable(s:sx)") == FAIL
Bram Moolenaar42ba1262008-12-09 10:18:03 +000010091 || put_line(fd, " exe \"source \" . fnameescape(s:sx)") == FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +000010092 || put_line(fd, "endif") == FAIL)
10093 return FAIL;
10094
10095 return OK;
10096}
10097
10098 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010099ses_winsizes(
10100 FILE *fd,
10101 int restore_size,
10102 win_T *tab_firstwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010103{
10104 int n = 0;
10105 win_T *wp;
10106
10107 if (restore_size && (ssop_flags & SSOP_WINSIZE))
10108 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010109 for (wp = tab_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010110 {
10111 if (!ses_do_win(wp))
10112 continue;
10113 ++n;
10114
10115 /* restore height when not full height */
10116 if (wp->w_height + wp->w_status_height < topframe->fr_height
10117 && (fprintf(fd,
10118 "exe '%dresize ' . ((&lines * %ld + %ld) / %ld)",
10119 n, (long)wp->w_height, Rows / 2, Rows) < 0
10120 || put_eol(fd) == FAIL))
10121 return FAIL;
10122
10123 /* restore width when not full width */
10124 if (wp->w_width < Columns && (fprintf(fd,
10125 "exe 'vert %dresize ' . ((&columns * %ld + %ld) / %ld)",
10126 n, (long)wp->w_width, Columns / 2, Columns) < 0
10127 || put_eol(fd) == FAIL))
10128 return FAIL;
10129 }
10130 }
10131 else
10132 {
10133 /* Just equalise window sizes */
10134 if (put_line(fd, "wincmd =") == FAIL)
10135 return FAIL;
10136 }
10137 return OK;
10138}
10139
10140/*
10141 * Write commands to "fd" to recursively create windows for frame "fr",
10142 * horizontally and vertically split.
10143 * After the commands the last window in the frame is the current window.
10144 * Returns FAIL when writing the commands to "fd" fails.
10145 */
10146 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010147ses_win_rec(FILE *fd, frame_T *fr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010148{
10149 frame_T *frc;
10150 int count = 0;
10151
10152 if (fr->fr_layout != FR_LEAF)
10153 {
10154 /* Find first frame that's not skipped and then create a window for
10155 * each following one (first frame is already there). */
10156 frc = ses_skipframe(fr->fr_child);
10157 if (frc != NULL)
10158 while ((frc = ses_skipframe(frc->fr_next)) != NULL)
10159 {
10160 /* Make window as big as possible so that we have lots of room
10161 * to split. */
10162 if (put_line(fd, "wincmd _ | wincmd |") == FAIL
10163 || put_line(fd, fr->fr_layout == FR_COL
10164 ? "split" : "vsplit") == FAIL)
10165 return FAIL;
10166 ++count;
10167 }
10168
10169 /* Go back to the first window. */
10170 if (count > 0 && (fprintf(fd, fr->fr_layout == FR_COL
10171 ? "%dwincmd k" : "%dwincmd h", count) < 0
10172 || put_eol(fd) == FAIL))
10173 return FAIL;
10174
10175 /* Recursively create frames/windows in each window of this column or
10176 * row. */
10177 frc = ses_skipframe(fr->fr_child);
10178 while (frc != NULL)
10179 {
10180 ses_win_rec(fd, frc);
10181 frc = ses_skipframe(frc->fr_next);
10182 /* Go to next window. */
10183 if (frc != NULL && put_line(fd, "wincmd w") == FAIL)
10184 return FAIL;
10185 }
10186 }
10187 return OK;
10188}
10189
10190/*
10191 * Skip frames that don't contain windows we want to save in the Session.
10192 * Returns NULL when there none.
10193 */
10194 static frame_T *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010195ses_skipframe(frame_T *fr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010196{
10197 frame_T *frc;
10198
Bram Moolenaar3d1491e2018-12-22 17:07:50 +010010199 FOR_ALL_FRAMES(frc, fr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010200 if (ses_do_frame(frc))
10201 break;
10202 return frc;
10203}
10204
10205/*
10206 * Return TRUE if frame "fr" has a window somewhere that we want to save in
10207 * the Session.
10208 */
10209 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010210ses_do_frame(frame_T *fr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010211{
10212 frame_T *frc;
10213
10214 if (fr->fr_layout == FR_LEAF)
10215 return ses_do_win(fr->fr_win);
Bram Moolenaar3d1491e2018-12-22 17:07:50 +010010216 FOR_ALL_FRAMES(frc, fr->fr_child)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010217 if (ses_do_frame(frc))
10218 return TRUE;
10219 return FALSE;
10220}
10221
10222/*
10223 * Return non-zero if window "wp" is to be stored in the Session.
10224 */
10225 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010226ses_do_win(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010227{
Bram Moolenaar4d8bac82018-03-09 21:33:34 +010010228#ifdef FEAT_TERMINAL
10229 if (bt_terminal(wp->w_buffer))
10230 return !term_is_finished(wp->w_buffer)
10231 && (ssop_flags & SSOP_TERMINAL)
10232 && term_should_restore(wp->w_buffer);
10233#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010234 if (wp->w_buffer->b_fname == NULL
10235#ifdef FEAT_QUICKFIX
10236 /* When 'buftype' is "nofile" can't restore the window contents. */
10237 || bt_nofile(wp->w_buffer)
10238#endif
10239 )
10240 return (ssop_flags & SSOP_BLANK);
Bram Moolenaar67883b42017-07-27 22:57:00 +020010241 if (bt_help(wp->w_buffer))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010242 return (ssop_flags & SSOP_HELP);
10243 return TRUE;
10244}
10245
Bram Moolenaar92c1b692018-08-29 21:42:42 +020010246 static int
10247put_view_curpos(FILE *fd, win_T *wp, char *spaces)
10248{
10249 int r;
10250
10251 if (wp->w_curswant == MAXCOL)
10252 r = fprintf(fd, "%snormal! $", spaces);
10253 else
10254 r = fprintf(fd, "%snormal! 0%d|", spaces, wp->w_virtcol + 1);
10255 return r < 0 || put_eol(fd) == FAIL ? FALSE : OK;
10256}
10257
Bram Moolenaar071d4272004-06-13 20:20:40 +000010258/*
10259 * Write commands to "fd" to restore the view of a window.
10260 * Caller must make sure 'scrolloff' is zero.
10261 */
10262 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010263put_view(
10264 FILE *fd,
10265 win_T *wp,
10266 int add_edit, /* add ":edit" command to view */
10267 unsigned *flagp, /* vop_flags or ssop_flags */
10268 int current_arg_idx) /* current argument index of the window, use
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010269 * -1 if unknown */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010270{
10271 win_T *save_curwin;
10272 int f;
10273 int do_cursor;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000010274 int did_next = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010275
10276 /* Always restore cursor position for ":mksession". For ":mkview" only
10277 * when 'viewoptions' contains "cursor". */
10278 do_cursor = (flagp == &ssop_flags || *flagp & SSOP_CURSOR);
10279
10280 /*
10281 * Local argument list.
10282 */
10283 if (wp->w_alist == &global_alist)
10284 {
10285 if (put_line(fd, "argglobal") == FAIL)
10286 return FAIL;
10287 }
10288 else
10289 {
10290 if (ses_arglist(fd, "arglocal", &wp->w_alist->al_ga,
10291 flagp == &vop_flags
10292 || !(*flagp & SSOP_CURDIR)
10293 || wp->w_localdir != NULL, flagp) == FAIL)
10294 return FAIL;
10295 }
10296
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000010297 /* Only when part of a session: restore the argument index. Some
10298 * arguments may have been deleted, check if the index is valid. */
Bram Moolenaar51f53df2010-06-26 05:25:54 +020010299 if (wp->w_arg_idx != current_arg_idx && wp->w_arg_idx < WARGCOUNT(wp)
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000010300 && flagp == &ssop_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010301 {
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010302 if (fprintf(fd, "%ldargu", (long)wp->w_arg_idx + 1) < 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000010303 || put_eol(fd) == FAIL)
10304 return FAIL;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000010305 did_next = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010306 }
10307
10308 /* Edit the file. Skip this when ":next" already did it. */
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000010309 if (add_edit && (!did_next || wp->w_arg_idx_invalid))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010310 {
Bram Moolenaar4d8bac82018-03-09 21:33:34 +010010311# ifdef FEAT_TERMINAL
10312 if (bt_terminal(wp->w_buffer))
10313 {
10314 if (term_write_session(fd, wp) == FAIL)
10315 return FAIL;
10316 }
10317 else
10318# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010319 /*
10320 * Load the file.
10321 */
10322 if (wp->w_buffer->b_ffname != NULL
Bram Moolenaar4d8bac82018-03-09 21:33:34 +010010323# ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +000010324 && !bt_nofile(wp->w_buffer)
Bram Moolenaar4d8bac82018-03-09 21:33:34 +010010325# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010326 )
10327 {
10328 /*
10329 * Editing a file in this buffer: use ":edit file".
10330 * This may have side effects! (e.g., compressed or network file).
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +020010331 *
10332 * Note, if a buffer for that file already exists, use :badd to
10333 * edit that buffer, to not lose folding information (:edit resets
10334 * folds in other buffers)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010335 */
Bram Moolenaarad36a352019-01-24 13:34:42 +010010336 if (fputs("if bufexists(\"", fd) < 0
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +020010337 || ses_fname(fd, wp->w_buffer, flagp, FALSE) == FAIL
Bram Moolenaarad36a352019-01-24 13:34:42 +010010338 || fputs("\") | buffer ", fd) < 0
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +020010339 || ses_fname(fd, wp->w_buffer, flagp, FALSE) == FAIL
10340 || fputs(" | else | edit ", fd) < 0
10341 || ses_fname(fd, wp->w_buffer, flagp, FALSE) == FAIL
10342 || fputs(" | endif", fd) < 0
Bram Moolenaar4d8bac82018-03-09 21:33:34 +010010343 || put_eol(fd) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010344 return FAIL;
10345 }
10346 else
10347 {
10348 /* No file in this buffer, just make it empty. */
10349 if (put_line(fd, "enew") == FAIL)
10350 return FAIL;
10351#ifdef FEAT_QUICKFIX
10352 if (wp->w_buffer->b_ffname != NULL)
10353 {
10354 /* The buffer does have a name, but it's not a file name. */
10355 if (fputs("file ", fd) < 0
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +020010356 || ses_fname(fd, wp->w_buffer, flagp, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010357 return FAIL;
10358 }
10359#endif
10360 do_cursor = FALSE;
10361 }
10362 }
10363
10364 /*
10365 * Local mappings and abbreviations.
10366 */
10367 if ((*flagp & (SSOP_OPTIONS | SSOP_LOCALOPTIONS))
10368 && makemap(fd, wp->w_buffer) == FAIL)
10369 return FAIL;
10370
10371 /*
10372 * Local options. Need to go to the window temporarily.
10373 * Store only local values when using ":mkview" and when ":mksession" is
10374 * used and 'sessionoptions' doesn't include "options".
10375 * Some folding options are always stored when "folds" is included,
10376 * otherwise the folds would not be restored correctly.
10377 */
10378 save_curwin = curwin;
10379 curwin = wp;
10380 curbuf = curwin->w_buffer;
10381 if (*flagp & (SSOP_OPTIONS | SSOP_LOCALOPTIONS))
10382 f = makeset(fd, OPT_LOCAL,
10383 flagp == &vop_flags || !(*flagp & SSOP_OPTIONS));
10384#ifdef FEAT_FOLDING
10385 else if (*flagp & SSOP_FOLDS)
10386 f = makefoldset(fd);
10387#endif
10388 else
10389 f = OK;
10390 curwin = save_curwin;
10391 curbuf = curwin->w_buffer;
10392 if (f == FAIL)
10393 return FAIL;
10394
10395#ifdef FEAT_FOLDING
10396 /*
10397 * Save Folds when 'buftype' is empty and for help files.
10398 */
10399 if ((*flagp & SSOP_FOLDS)
10400 && wp->w_buffer->b_ffname != NULL
Bram Moolenaar91335e52018-08-01 17:53:12 +020010401 && (bt_normal(wp->w_buffer) || bt_help(wp->w_buffer)))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010402 {
10403 if (put_folds(fd, wp) == FAIL)
10404 return FAIL;
10405 }
10406#endif
10407
10408 /*
10409 * Set the cursor after creating folds, since that moves the cursor.
10410 */
10411 if (do_cursor)
10412 {
10413
10414 /* Restore the cursor line in the file and relatively in the
10415 * window. Don't use "G", it changes the jumplist. */
10416 if (fprintf(fd, "let s:l = %ld - ((%ld * winheight(0) + %ld) / %ld)",
10417 (long)wp->w_cursor.lnum,
10418 (long)(wp->w_cursor.lnum - wp->w_topline),
10419 (long)wp->w_height / 2, (long)wp->w_height) < 0
10420 || put_eol(fd) == FAIL
10421 || put_line(fd, "if s:l < 1 | let s:l = 1 | endif") == FAIL
10422 || put_line(fd, "exe s:l") == FAIL
10423 || put_line(fd, "normal! zt") == FAIL
10424 || fprintf(fd, "%ld", (long)wp->w_cursor.lnum) < 0
10425 || put_eol(fd) == FAIL)
10426 return FAIL;
10427 /* Restore the cursor column and left offset when not wrapping. */
10428 if (wp->w_cursor.col == 0)
10429 {
10430 if (put_line(fd, "normal! 0") == FAIL)
10431 return FAIL;
10432 }
10433 else
10434 {
10435 if (!wp->w_p_wrap && wp->w_leftcol > 0 && wp->w_width > 0)
10436 {
10437 if (fprintf(fd,
10438 "let s:c = %ld - ((%ld * winwidth(0) + %ld) / %ld)",
Bram Moolenaar558ddad2013-02-20 19:26:29 +010010439 (long)wp->w_virtcol + 1,
10440 (long)(wp->w_virtcol - wp->w_leftcol),
Bram Moolenaar071d4272004-06-13 20:20:40 +000010441 (long)wp->w_width / 2, (long)wp->w_width) < 0
10442 || put_eol(fd) == FAIL
10443 || put_line(fd, "if s:c > 0") == FAIL
10444 || fprintf(fd,
Bram Moolenaar558ddad2013-02-20 19:26:29 +010010445 " exe 'normal! ' . s:c . '|zs' . %ld . '|'",
10446 (long)wp->w_virtcol + 1) < 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000010447 || put_eol(fd) == FAIL
10448 || put_line(fd, "else") == FAIL
Bram Moolenaar92c1b692018-08-29 21:42:42 +020010449 || put_view_curpos(fd, wp, " ") == FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +000010450 || put_line(fd, "endif") == FAIL)
10451 return FAIL;
10452 }
Bram Moolenaar92c1b692018-08-29 21:42:42 +020010453 else if (put_view_curpos(fd, wp, "") == FAIL)
10454 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010455 }
10456 }
10457
10458 /*
Bram Moolenaar13e90412017-11-11 18:16:48 +010010459 * Local directory, if the current flag is not view options or the "curdir"
10460 * option is included.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010461 */
Bram Moolenaar13e90412017-11-11 18:16:48 +010010462 if (wp->w_localdir != NULL
10463 && (flagp != &vop_flags || (*flagp & SSOP_CURDIR)))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010464 {
10465 if (fputs("lcd ", fd) < 0
10466 || ses_put_fname(fd, wp->w_localdir, flagp) == FAIL
10467 || put_eol(fd) == FAIL)
10468 return FAIL;
Bram Moolenaareeefcc72007-05-01 21:21:21 +000010469 did_lcd = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010470 }
10471
10472 return OK;
10473}
10474
10475/*
10476 * Write an argument list to the session file.
10477 * Returns FAIL if writing fails.
10478 */
10479 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010480ses_arglist(
10481 FILE *fd,
10482 char *cmd,
10483 garray_T *gap,
10484 int fullname, /* TRUE: use full path name */
10485 unsigned *flagp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010486{
10487 int i;
Bram Moolenaard9462e32011-04-11 21:35:11 +020010488 char_u *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010489 char_u *s;
10490
Bram Moolenaarf0bdd2f2014-03-12 21:28:26 +010010491 if (fputs(cmd, fd) < 0 || put_eol(fd) == FAIL)
10492 return FAIL;
Bram Moolenaar555de4e2019-01-21 23:03:49 +010010493 if (put_line(fd, "%argdel") == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010494 return FAIL;
10495 for (i = 0; i < gap->ga_len; ++i)
10496 {
10497 /* NULL file names are skipped (only happens when out of memory). */
10498 s = alist_name(&((aentry_T *)gap->ga_data)[i]);
10499 if (s != NULL)
10500 {
10501 if (fullname)
10502 {
Bram Moolenaard9462e32011-04-11 21:35:11 +020010503 buf = alloc(MAXPATHL);
10504 if (buf != NULL)
10505 {
10506 (void)vim_FullName(s, buf, MAXPATHL, FALSE);
10507 s = buf;
10508 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010509 }
Bram Moolenaar79da5632017-02-01 22:52:44 +010010510 if (fputs("$argadd ", fd) < 0
Bram Moolenaarf0bdd2f2014-03-12 21:28:26 +010010511 || ses_put_fname(fd, s, flagp) == FAIL
10512 || put_eol(fd) == FAIL)
Bram Moolenaard9462e32011-04-11 21:35:11 +020010513 {
10514 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010515 return FAIL;
Bram Moolenaard9462e32011-04-11 21:35:11 +020010516 }
10517 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010518 }
10519 }
Bram Moolenaarf0bdd2f2014-03-12 21:28:26 +010010520 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010521}
10522
10523/*
10524 * Write a buffer name to the session file.
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +020010525 * Also ends the line, if "add_eol" is TRUE.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010526 * Returns FAIL if writing fails.
10527 */
10528 static int
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +020010529ses_fname(FILE *fd, buf_T *buf, unsigned *flagp, int add_eol)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010530{
10531 char_u *name;
10532
10533 /* Use the short file name if the current directory is known at the time
Bram Moolenaareeefcc72007-05-01 21:21:21 +000010534 * the session file will be sourced.
10535 * Don't do this for ":mkview", we don't know the current directory.
10536 * Don't do this after ":lcd", we don't keep track of what the current
10537 * directory is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010538 if (buf->b_sfname != NULL
10539 && flagp == &ssop_flags
Bram Moolenaareeefcc72007-05-01 21:21:21 +000010540 && (ssop_flags & (SSOP_CURDIR | SSOP_SESDIR))
Bram Moolenaar8d594672009-07-01 18:18:57 +000010541#ifdef FEAT_AUTOCHDIR
10542 && !p_acd
10543#endif
Bram Moolenaareeefcc72007-05-01 21:21:21 +000010544 && !did_lcd)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010545 name = buf->b_sfname;
10546 else
10547 name = buf->b_ffname;
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +020010548 if (ses_put_fname(fd, name, flagp) == FAIL
10549 || (add_eol && put_eol(fd) == FAIL))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010550 return FAIL;
10551 return OK;
10552}
10553
10554/*
10555 * Write a file name to the session file.
10556 * Takes care of the "slash" option in 'sessionoptions' and escapes special
10557 * characters.
Bram Moolenaar78f74a92010-10-13 17:50:07 +020010558 * Returns FAIL if writing fails or out of memory.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010559 */
10560 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010561ses_put_fname(FILE *fd, char_u *name, unsigned *flagp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010562{
10563 char_u *sname;
Bram Moolenaar78f74a92010-10-13 17:50:07 +020010564 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010565 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010566
10567 sname = home_replace_save(NULL, name);
Bram Moolenaar78f74a92010-10-13 17:50:07 +020010568 if (sname == NULL)
10569 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010570
Bram Moolenaar78f74a92010-10-13 17:50:07 +020010571 if (*flagp & SSOP_SLASH)
10572 {
10573 /* change all backslashes to forward slashes */
Bram Moolenaar91acfff2017-03-12 19:22:36 +010010574 for (p = sname; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar78f74a92010-10-13 17:50:07 +020010575 if (*p == '\\')
10576 *p = '/';
Bram Moolenaar071d4272004-06-13 20:20:40 +000010577 }
Bram Moolenaar78f74a92010-10-13 17:50:07 +020010578
Bram Moolenaar84a05ac2013-05-06 04:24:17 +020010579 /* escape special characters */
Bram Moolenaar78f74a92010-10-13 17:50:07 +020010580 p = vim_strsave_fnameescape(sname, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010581 vim_free(sname);
Bram Moolenaar78f74a92010-10-13 17:50:07 +020010582 if (p == NULL)
10583 return FAIL;
10584
10585 /* write the result */
10586 if (fputs((char *)p, fd) < 0)
10587 retval = FAIL;
10588
10589 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010590 return retval;
10591}
10592
10593/*
10594 * ":loadview [nr]"
10595 */
10596 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010597ex_loadview(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010598{
10599 char_u *fname;
10600
10601 fname = get_view_file(*eap->arg);
10602 if (fname != NULL)
10603 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +000010604 do_source(fname, FALSE, DOSO_NONE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010605 vim_free(fname);
10606 }
10607}
10608
10609/*
10610 * Get the name of the view file for the current buffer.
10611 */
10612 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010613get_view_file(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010614{
10615 int len = 0;
10616 char_u *p, *s;
10617 char_u *retval;
10618 char_u *sname;
10619
10620 if (curbuf->b_ffname == NULL)
10621 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010622 emsg(_(e_noname));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010623 return NULL;
10624 }
10625 sname = home_replace_save(NULL, curbuf->b_ffname);
10626 if (sname == NULL)
10627 return NULL;
10628
10629 /*
10630 * We want a file name without separators, because we're not going to make
10631 * a directory.
10632 * "normal" path separator -> "=+"
10633 * "=" -> "=="
10634 * ":" path separator -> "=-"
10635 */
10636 for (p = sname; *p; ++p)
10637 if (*p == '=' || vim_ispathsep(*p))
10638 ++len;
Bram Moolenaar964b3742019-05-24 18:54:09 +020010639 retval = alloc(STRLEN(sname) + len + STRLEN(p_vdir) + 9);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010640 if (retval != NULL)
10641 {
10642 STRCPY(retval, p_vdir);
10643 add_pathsep(retval);
10644 s = retval + STRLEN(retval);
10645 for (p = sname; *p; ++p)
10646 {
10647 if (*p == '=')
10648 {
10649 *s++ = '=';
10650 *s++ = '=';
10651 }
10652 else if (vim_ispathsep(*p))
10653 {
10654 *s++ = '=';
Bram Moolenaare60acc12011-05-10 16:41:25 +020010655#if defined(BACKSLASH_IN_FILENAME) || defined(AMIGA) || defined(VMS)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010656 if (*p == ':')
10657 *s++ = '-';
10658 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010659#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +000010660 *s++ = '+';
Bram Moolenaar071d4272004-06-13 20:20:40 +000010661 }
10662 else
10663 *s++ = *p;
10664 }
10665 *s++ = '=';
10666 *s++ = c;
10667 STRCPY(s, ".vim");
10668 }
10669
10670 vim_free(sname);
10671 return retval;
10672}
10673
10674#endif /* FEAT_SESSION */
10675
10676/*
10677 * Write end-of-line character(s) for ":mkexrc", ":mkvimrc" and ":mksession".
10678 * Return FAIL for a write error.
10679 */
10680 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010681put_eol(FILE *fd)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010682{
10683 if (
10684#ifdef USE_CRNL
10685 (
10686# ifdef MKSESSION_NL
10687 !mksession_nl &&
10688# endif
10689 (putc('\r', fd) < 0)) ||
10690#endif
10691 (putc('\n', fd) < 0))
10692 return FAIL;
10693 return OK;
10694}
10695
10696/*
10697 * Write a line to "fd".
10698 * Return FAIL for a write error.
10699 */
10700 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010701put_line(FILE *fd, char *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010702{
10703 if (fputs(s, fd) < 0 || put_eol(fd) == FAIL)
10704 return FAIL;
10705 return OK;
10706}
10707
10708#ifdef FEAT_VIMINFO
10709/*
10710 * ":rviminfo" and ":wviminfo".
10711 */
10712 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010713ex_viminfo(
10714 exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010715{
10716 char_u *save_viminfo;
10717
10718 save_viminfo = p_viminfo;
10719 if (*p_viminfo == NUL)
10720 p_viminfo = (char_u *)"'100";
10721 if (eap->cmdidx == CMD_rviminfo)
10722 {
Bram Moolenaard812df62008-11-09 12:46:09 +000010723 if (read_viminfo(eap->arg, VIF_WANT_INFO | VIF_WANT_MARKS
10724 | (eap->forceit ? VIF_FORCEIT : 0)) == FAIL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010725 emsg(_("E195: Cannot open viminfo file for reading"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010726 }
10727 else
10728 write_viminfo(eap->arg, eap->forceit);
10729 p_viminfo = save_viminfo;
10730}
10731#endif
10732
10733#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG) || defined(PROTO)
Bram Moolenaar9c13b352005-05-19 20:53:52 +000010734/*
Bram Moolenaard9462e32011-04-11 21:35:11 +020010735 * Make a dialog message in "buff[DIALOG_MSG_SIZE]".
Bram Moolenaarb765d632005-06-07 21:00:02 +000010736 * "format" must contain "%s".
Bram Moolenaar9c13b352005-05-19 20:53:52 +000010737 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010738 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010739dialog_msg(char_u *buff, char *format, char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010740{
Bram Moolenaar071d4272004-06-13 20:20:40 +000010741 if (fname == NULL)
10742 fname = (char_u *)_("Untitled");
Bram Moolenaard9462e32011-04-11 21:35:11 +020010743 vim_snprintf((char *)buff, DIALOG_MSG_SIZE, format, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010744}
10745#endif
10746
10747/*
10748 * ":behave {mswin,xterm}"
10749 */
10750 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010751ex_behave(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010752{
10753 if (STRCMP(eap->arg, "mswin") == 0)
10754 {
10755 set_option_value((char_u *)"selection", 0L, (char_u *)"exclusive", 0);
10756 set_option_value((char_u *)"selectmode", 0L, (char_u *)"mouse,key", 0);
10757 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"popup", 0);
10758 set_option_value((char_u *)"keymodel", 0L,
10759 (char_u *)"startsel,stopsel", 0);
10760 }
10761 else if (STRCMP(eap->arg, "xterm") == 0)
10762 {
10763 set_option_value((char_u *)"selection", 0L, (char_u *)"inclusive", 0);
10764 set_option_value((char_u *)"selectmode", 0L, (char_u *)"", 0);
10765 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"extend", 0);
10766 set_option_value((char_u *)"keymodel", 0L, (char_u *)"", 0);
10767 }
10768 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010769 semsg(_(e_invarg2), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010770}
10771
Bram Moolenaar42b4dda2010-03-02 15:56:05 +010010772#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
10773/*
10774 * Function given to ExpandGeneric() to obtain the possible arguments of the
10775 * ":behave {mswin,xterm}" command.
10776 */
10777 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010778get_behave_arg(expand_T *xp UNUSED, int idx)
Bram Moolenaar42b4dda2010-03-02 15:56:05 +010010779{
10780 if (idx == 0)
10781 return (char_u *)"mswin";
10782 if (idx == 1)
10783 return (char_u *)"xterm";
10784 return NULL;
10785}
Bram Moolenaar9e507ca2016-10-15 15:39:39 +020010786
10787/*
10788 * Function given to ExpandGeneric() to obtain the possible arguments of the
10789 * ":messages {clear}" command.
10790 */
10791 char_u *
10792get_messages_arg(expand_T *xp UNUSED, int idx)
10793{
10794 if (idx == 0)
10795 return (char_u *)"clear";
10796 return NULL;
10797}
Bram Moolenaar42b4dda2010-03-02 15:56:05 +010010798#endif
10799
Bram Moolenaar113e1072019-01-20 15:30:40 +010010800#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
Bram Moolenaarcae92dc2017-08-06 15:22:15 +020010801 char_u *
10802get_mapclear_arg(expand_T *xp UNUSED, int idx)
10803{
10804 if (idx == 0)
10805 return (char_u *)"<buffer>";
10806 return NULL;
10807}
Bram Moolenaar113e1072019-01-20 15:30:40 +010010808#endif
Bram Moolenaarcae92dc2017-08-06 15:22:15 +020010809
Bram Moolenaar071d4272004-06-13 20:20:40 +000010810static int filetype_detect = FALSE;
10811static int filetype_plugin = FALSE;
10812static int filetype_indent = FALSE;
10813
10814/*
10815 * ":filetype [plugin] [indent] {on,off,detect}"
10816 * on: Load the filetype.vim file to install autocommands for file types.
10817 * off: Load the ftoff.vim file to remove all autocommands for file types.
10818 * plugin on: load filetype.vim and ftplugin.vim
10819 * plugin off: load ftplugof.vim
10820 * indent on: load filetype.vim and indent.vim
10821 * indent off: load indoff.vim
10822 */
10823 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010824ex_filetype(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010825{
10826 char_u *arg = eap->arg;
10827 int plugin = FALSE;
10828 int indent = FALSE;
10829
10830 if (*eap->arg == NUL)
10831 {
10832 /* Print current status. */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010833 smsg("filetype detection:%s plugin:%s indent:%s",
Bram Moolenaar071d4272004-06-13 20:20:40 +000010834 filetype_detect ? "ON" : "OFF",
10835 filetype_plugin ? (filetype_detect ? "ON" : "(on)") : "OFF",
10836 filetype_indent ? (filetype_detect ? "ON" : "(on)") : "OFF");
10837 return;
10838 }
10839
10840 /* Accept "plugin" and "indent" in any order. */
10841 for (;;)
10842 {
10843 if (STRNCMP(arg, "plugin", 6) == 0)
10844 {
10845 plugin = TRUE;
10846 arg = skipwhite(arg + 6);
10847 continue;
10848 }
10849 if (STRNCMP(arg, "indent", 6) == 0)
10850 {
10851 indent = TRUE;
10852 arg = skipwhite(arg + 6);
10853 continue;
10854 }
10855 break;
10856 }
10857 if (STRCMP(arg, "on") == 0 || STRCMP(arg, "detect") == 0)
10858 {
10859 if (*arg == 'o' || !filetype_detect)
10860 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010010861 source_runtime((char_u *)FILETYPE_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010862 filetype_detect = TRUE;
10863 if (plugin)
10864 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010010865 source_runtime((char_u *)FTPLUGIN_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010866 filetype_plugin = TRUE;
10867 }
10868 if (indent)
10869 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010010870 source_runtime((char_u *)INDENT_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010871 filetype_indent = TRUE;
10872 }
10873 }
10874 if (*arg == 'd')
10875 {
Bram Moolenaar1610d052016-06-09 22:53:01 +020010876 (void)do_doautocmd((char_u *)"filetypedetect BufRead", TRUE, NULL);
Bram Moolenaara3227e22006-03-08 21:32:40 +000010877 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010878 }
10879 }
10880 else if (STRCMP(arg, "off") == 0)
10881 {
10882 if (plugin || indent)
10883 {
10884 if (plugin)
10885 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010010886 source_runtime((char_u *)FTPLUGOF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010887 filetype_plugin = FALSE;
10888 }
10889 if (indent)
10890 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010010891 source_runtime((char_u *)INDOFF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010892 filetype_indent = FALSE;
10893 }
10894 }
10895 else
10896 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010010897 source_runtime((char_u *)FTOFF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010898 filetype_detect = FALSE;
10899 }
10900 }
10901 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010902 semsg(_(e_invarg2), arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010903}
10904
10905/*
Bram Moolenaar3e545692017-06-04 19:00:32 +020010906 * ":setfiletype [FALLBACK] {name}"
Bram Moolenaar071d4272004-06-13 20:20:40 +000010907 */
10908 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010909ex_setfiletype(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010910{
10911 if (!did_filetype)
Bram Moolenaar3e545692017-06-04 19:00:32 +020010912 {
10913 char_u *arg = eap->arg;
10914
10915 if (STRNCMP(arg, "FALLBACK ", 9) == 0)
10916 arg += 9;
10917
10918 set_option_value((char_u *)"filetype", 0L, arg, OPT_LOCAL);
10919 if (arg != eap->arg)
10920 did_filetype = FALSE;
10921 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010922}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010923
Bram Moolenaar071d4272004-06-13 20:20:40 +000010924 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010925ex_digraphs(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010926{
10927#ifdef FEAT_DIGRAPHS
10928 if (*eap->arg != NUL)
10929 putdigraph(eap->arg);
10930 else
Bram Moolenaareae8ae12018-12-14 18:53:02 +010010931 listdigraphs(eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010932#else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010933 emsg(_("E196: No digraphs in this version"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010934#endif
10935}
10936
10937 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010938ex_set(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010939{
10940 int flags = 0;
10941
10942 if (eap->cmdidx == CMD_setlocal)
10943 flags = OPT_LOCAL;
10944 else if (eap->cmdidx == CMD_setglobal)
10945 flags = OPT_GLOBAL;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +010010946#if defined(FEAT_EVAL) && defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010947 if (cmdmod.browse && flags == 0)
10948 ex_options(eap);
10949 else
10950#endif
10951 (void)do_set(eap->arg, flags);
10952}
10953
Bram Moolenaar451fc7b2018-04-27 22:53:07 +020010954#if defined(FEAT_SEARCH_EXTRA) || defined(PROTO)
10955 void
10956set_no_hlsearch(int flag)
10957{
10958 no_hlsearch = flag;
10959# ifdef FEAT_EVAL
10960 set_vim_var_nr(VV_HLSEARCH, !no_hlsearch && p_hls);
10961# endif
10962}
10963
Bram Moolenaar071d4272004-06-13 20:20:40 +000010964/*
10965 * ":nohlsearch"
10966 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010967 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010968ex_nohlsearch(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010969{
Bram Moolenaar451fc7b2018-04-27 22:53:07 +020010970 set_no_hlsearch(TRUE);
Bram Moolenaarf71a3db2006-03-12 21:50:18 +000010971 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010972}
10973
10974/*
Bram Moolenaare1438bb2006-03-01 22:01:55 +000010975 * ":[N]match {group} {pattern}"
Bram Moolenaar071d4272004-06-13 20:20:40 +000010976 * Sets nextcmd to the start of the next command, if any. Also called when
10977 * skipping commands to find the next command.
10978 */
10979 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010980ex_match(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010981{
10982 char_u *p;
Bram Moolenaar52d36c82007-08-11 14:00:30 +000010983 char_u *g = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010984 char_u *end;
10985 int c;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000010986 int id;
Bram Moolenaare1438bb2006-03-01 22:01:55 +000010987
10988 if (eap->line2 <= 3)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000010989 id = eap->line2;
Bram Moolenaare1438bb2006-03-01 22:01:55 +000010990 else
10991 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010992 emsg(_(e_invcmd));
Bram Moolenaare1438bb2006-03-01 22:01:55 +000010993 return;
10994 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010995
10996 /* First clear any old pattern. */
10997 if (!eap->skip)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000010998 match_delete(curwin, id, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010999
11000 if (ends_excmd(*eap->arg))
11001 end = eap->arg;
11002 else if ((STRNICMP(eap->arg, "none", 4) == 0
Bram Moolenaar1c465442017-03-12 20:10:05 +010011003 && (VIM_ISWHITE(eap->arg[4]) || ends_excmd(eap->arg[4]))))
Bram Moolenaar071d4272004-06-13 20:20:40 +000011004 end = eap->arg + 4;
11005 else
11006 {
11007 p = skiptowhite(eap->arg);
11008 if (!eap->skip)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011009 g = vim_strnsave(eap->arg, (int)(p - eap->arg));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011010 p = skipwhite(p);
11011 if (*p == NUL)
11012 {
11013 /* There must be two arguments. */
Bram Moolenaar9a7d58e2015-11-24 17:23:56 +010011014 vim_free(g);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011015 semsg(_(e_invarg2), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011016 return;
11017 }
11018 end = skip_regexp(p + 1, *p, TRUE, NULL);
11019 if (!eap->skip)
11020 {
11021 if (*end != NUL && !ends_excmd(*skipwhite(end + 1)))
11022 {
Bram Moolenaar9a7d58e2015-11-24 17:23:56 +010011023 vim_free(g);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011024 eap->errmsg = e_trailing;
11025 return;
11026 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000011027 if (*end != *p)
11028 {
Bram Moolenaar9a7d58e2015-11-24 17:23:56 +010011029 vim_free(g);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011030 semsg(_(e_invarg2), p);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000011031 return;
11032 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011033
11034 c = *end;
11035 *end = NUL;
Bram Moolenaar6561d522015-07-21 15:48:27 +020011036 match_add(curwin, g, p + 1, 10, id, NULL, NULL);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011037 vim_free(g);
Bram Moolenaar910f66f2006-04-05 20:41:53 +000011038 *end = c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011039 }
11040 }
11041 eap->nextcmd = find_nextcmd(end);
11042}
11043#endif
11044
11045#ifdef FEAT_CRYPT
11046/*
11047 * ":X": Get crypt key
11048 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011049 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011050ex_X(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011051{
Bram Moolenaar3a0c9082014-11-12 15:15:42 +010011052 crypt_check_current_method();
Bram Moolenaar8f4ac012014-08-10 13:38:34 +020011053 (void)crypt_get_key(TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011054}
11055#endif
11056
11057#ifdef FEAT_FOLDING
11058 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011059ex_fold(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011060{
11061 if (foldManualAllowed(TRUE))
11062 foldCreate(eap->line1, eap->line2);
11063}
11064
11065 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011066ex_foldopen(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011067{
11068 opFoldRange(eap->line1, eap->line2, eap->cmdidx == CMD_foldopen,
11069 eap->forceit, FALSE);
11070}
11071
11072 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011073ex_folddo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011074{
11075 linenr_T lnum;
11076
Bram Moolenaar6b1ee342014-08-06 18:17:11 +020011077#ifdef FEAT_CLIPBOARD
11078 start_global_changes();
11079#endif
11080
Bram Moolenaar071d4272004-06-13 20:20:40 +000011081 /* First set the marks for all lines closed/open. */
11082 for (lnum = eap->line1; lnum <= eap->line2; ++lnum)
11083 if (hasFolding(lnum, NULL, NULL) == (eap->cmdidx == CMD_folddoclosed))
11084 ml_setmarked(lnum);
11085
11086 /* Execute the command on the marked lines. */
11087 global_exe(eap->arg);
11088 ml_clearmarked(); /* clear rest of the marks */
Bram Moolenaar6b1ee342014-08-06 18:17:11 +020011089#ifdef FEAT_CLIPBOARD
11090 end_global_changes();
11091#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011092}
11093#endif
Bram Moolenaarf5291f32017-09-14 22:55:37 +020011094
Bram Moolenaar39665952018-08-15 20:59:48 +020011095#ifdef FEAT_QUICKFIX
11096/*
11097 * Returns TRUE if the supplied Ex cmdidx is for a location list command
11098 * instead of a quickfix command.
11099 */
11100 int
11101is_loclist_cmd(int cmdidx)
11102{
Bram Moolenaar74c8be22018-08-23 22:51:40 +020011103 if (cmdidx < 0 || cmdidx >= CMD_SIZE)
Bram Moolenaar39665952018-08-15 20:59:48 +020011104 return FALSE;
11105 return cmdnames[cmdidx].cmd_name[0] == 'l';
11106}
11107#endif
11108
Bram Moolenaarf5291f32017-09-14 22:55:37 +020011109# if defined(FEAT_TIMERS) || defined(PROTO)
11110 int
11111get_pressedreturn(void)
11112{
11113 return ex_pressedreturn;
11114}
11115
11116 void
11117set_pressedreturn(int val)
11118{
11119 ex_pressedreturn = val;
11120}
11121#endif