blob: 9f127ec72a15fdf34d3dfd331809b8f53fd142b1 [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);
4819 new_cmdline = alloc((int)(STRLEN(program) + i * (len - 2) + 1));
4820 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 {
4835 new_cmdline = alloc((int)(STRLEN(program) + STRLEN(p) + 2));
4836 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{
5455 eap->errmsg = do_bufdel(
5456 eap->cmdidx == CMD_bdelete ? DOBUF_DEL
5457 : eap->cmdidx == CMD_bwipeout ? DOBUF_WIPE
5458 : DOBUF_UNLOAD, eap->arg,
5459 eap->addr_count, (int)eap->line1, (int)eap->line2, eap->forceit);
5460}
5461
5462/*
5463 * :[N]buffer [N] to buffer N
5464 * :[N]sbuffer [N] to buffer N
5465 */
5466 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005467ex_buffer(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005468{
5469 if (*eap->arg)
5470 eap->errmsg = e_trailing;
5471 else
5472 {
5473 if (eap->addr_count == 0) /* default is current buffer */
5474 goto_buffer(eap, DOBUF_CURRENT, FORWARD, 0);
5475 else
5476 goto_buffer(eap, DOBUF_FIRST, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005477 if (eap->do_ecmd_cmd != NULL)
5478 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005479 }
5480}
5481
5482/*
5483 * :[N]bmodified [N] to next mod. buffer
5484 * :[N]sbmodified [N] to next mod. buffer
5485 */
5486 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005487ex_bmodified(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005488{
5489 goto_buffer(eap, DOBUF_MOD, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005490 if (eap->do_ecmd_cmd != NULL)
5491 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005492}
5493
5494/*
5495 * :[N]bnext [N] to next buffer
5496 * :[N]sbnext [N] split and to next buffer
5497 */
5498 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005499ex_bnext(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005500{
5501 goto_buffer(eap, DOBUF_CURRENT, FORWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005502 if (eap->do_ecmd_cmd != NULL)
5503 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005504}
5505
5506/*
5507 * :[N]bNext [N] to previous buffer
5508 * :[N]bprevious [N] to previous buffer
5509 * :[N]sbNext [N] split and to previous buffer
5510 * :[N]sbprevious [N] split and to previous buffer
5511 */
5512 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005513ex_bprevious(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005514{
5515 goto_buffer(eap, DOBUF_CURRENT, BACKWARD, (int)eap->line2);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005516 if (eap->do_ecmd_cmd != NULL)
5517 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005518}
5519
5520/*
5521 * :brewind to first buffer
5522 * :bfirst to first buffer
5523 * :sbrewind split and to first buffer
5524 * :sbfirst split and to first buffer
5525 */
5526 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005527ex_brewind(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005528{
5529 goto_buffer(eap, DOBUF_FIRST, FORWARD, 0);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005530 if (eap->do_ecmd_cmd != NULL)
5531 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005532}
5533
5534/*
5535 * :blast to last buffer
5536 * :sblast split and to last buffer
5537 */
5538 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005539ex_blast(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005540{
5541 goto_buffer(eap, DOBUF_LAST, BACKWARD, 0);
Bram Moolenaar9c8d9e12014-09-19 20:07:26 +02005542 if (eap->do_ecmd_cmd != NULL)
5543 do_cmdline_cmd(eap->do_ecmd_cmd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005544}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005545
5546 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005547ends_excmd(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005548{
5549 return (c == NUL || c == '|' || c == '"' || c == '\n');
5550}
5551
5552#if defined(FEAT_SYN_HL) || defined(FEAT_SEARCH_EXTRA) || defined(FEAT_EVAL) \
5553 || defined(PROTO)
5554/*
5555 * Return the next command, after the first '|' or '\n'.
5556 * Return NULL if not found.
5557 */
5558 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005559find_nextcmd(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005560{
5561 while (*p != '|' && *p != '\n')
5562 {
5563 if (*p == NUL)
5564 return NULL;
5565 ++p;
5566 }
5567 return (p + 1);
5568}
5569#endif
5570
5571/*
Bram Moolenaar2256c992016-11-15 21:17:07 +01005572 * Check if *p is a separator between Ex commands, skipping over white space.
5573 * Return NULL if it isn't, the following character if it is.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005574 */
5575 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005576check_nextcmd(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005577{
Bram Moolenaar2256c992016-11-15 21:17:07 +01005578 char_u *s = skipwhite(p);
5579
5580 if (*s == '|' || *s == '\n')
5581 return (s + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005582 else
5583 return NULL;
5584}
5585
5586/*
5587 * - if there are more files to edit
5588 * - and this is the last window
5589 * - and forceit not used
5590 * - and not repeated twice on a row
5591 * return FAIL and give error message if 'message' TRUE
5592 * return OK otherwise
5593 */
5594 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005595check_more(
5596 int message, /* when FALSE check only, no messages */
5597 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005598{
5599 int n = ARGCOUNT - curwin->w_arg_idx - 1;
5600
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00005601 if (!forceit && only_one_window()
5602 && ARGCOUNT > 1 && !arg_had_last && n >= 0 && quitmore == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005603 {
5604 if (message)
5605 {
5606#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
5607 if ((p_confirm || cmdmod.confirm) && curbuf->b_fname != NULL)
5608 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02005609 char_u buff[DIALOG_MSG_SIZE];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005610
Bram Moolenaarda6e8912018-08-21 15:12:14 +02005611 vim_snprintf((char *)buff, DIALOG_MSG_SIZE,
5612 NGETTEXT("%d more file to edit. Quit anyway?",
5613 "%d more files to edit. Quit anyway?", n), n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005614 if (vim_dialog_yesno(VIM_QUESTION, NULL, buff, 1) == VIM_YES)
5615 return OK;
5616 return FAIL;
5617 }
5618#endif
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01005619 semsg(NGETTEXT("E173: %d more file to edit",
5620 "E173: %d more files to edit", n), n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005621 quitmore = 2; /* next try to quit is allowed */
5622 }
5623 return FAIL;
5624 }
5625 return OK;
5626}
5627
Bram Moolenaarac9fb182019-04-27 13:04:13 +02005628#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005629/*
5630 * Function given to ExpandGeneric() to obtain the list of command names.
5631 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005632 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005633get_command_name(expand_T *xp UNUSED, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005634{
5635 if (idx >= (int)CMD_SIZE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005636 return get_user_command_name(idx);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005637 return cmdnames[idx].cmd_name;
5638}
5639#endif
5640
Bram Moolenaar071d4272004-06-13 20:20:40 +00005641 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005642ex_colorscheme(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005643{
Bram Moolenaare6850792010-05-14 15:28:44 +02005644 if (*eap->arg == NUL)
5645 {
5646#ifdef FEAT_EVAL
5647 char_u *expr = vim_strsave((char_u *)"g:colors_name");
5648 char_u *p = NULL;
5649
5650 if (expr != NULL)
5651 {
5652 ++emsg_off;
5653 p = eval_to_string(expr, NULL, FALSE);
5654 --emsg_off;
5655 vim_free(expr);
5656 }
5657 if (p != NULL)
5658 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01005659 msg((char *)p);
Bram Moolenaare6850792010-05-14 15:28:44 +02005660 vim_free(p);
5661 }
5662 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01005663 msg("default");
Bram Moolenaare6850792010-05-14 15:28:44 +02005664#else
Bram Moolenaar32526b32019-01-19 17:43:09 +01005665 msg(_("unknown"));
Bram Moolenaare6850792010-05-14 15:28:44 +02005666#endif
5667 }
5668 else if (load_colors(eap->arg) == FAIL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005669 semsg(_("E185: Cannot find color scheme '%s'"), eap->arg);
Bram Moolenaarf58d81a2019-01-28 20:19:05 +01005670
5671#ifdef FEAT_VTP
5672 else if (has_vtp_working())
5673 {
5674 // background color change requires clear + redraw
5675 update_screen(CLEAR);
5676 redrawcmd();
5677 }
5678#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005679}
5680
5681 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005682ex_highlight(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005683{
5684 if (*eap->arg == NUL && eap->cmd[2] == '!')
Bram Moolenaar32526b32019-01-19 17:43:09 +01005685 msg(_("Greetings, Vim user!"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005686 do_highlight(eap->arg, eap->forceit, FALSE);
5687}
5688
5689
5690/*
5691 * Call this function if we thought we were going to exit, but we won't
5692 * (because of an error). May need to restore the terminal mode.
5693 */
5694 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005695not_exiting(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005696{
5697 exiting = FALSE;
5698 settmode(TMODE_RAW);
5699}
5700
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005701 static int
5702before_quit_autocmds(win_T *wp, int quit_all, int forceit)
5703{
5704 apply_autocmds(EVENT_QUITPRE, NULL, NULL, FALSE, wp->w_buffer);
5705
5706 /* Bail out when autocommands closed the window.
5707 * Refuse to quit when the buffer in the last window is being closed (can
5708 * only happen in autocommands). */
5709 if (!win_valid(wp)
5710 || curbuf_locked()
5711 || (wp->w_buffer->b_nwindows == 1 && wp->w_buffer->b_locked > 0))
5712 return TRUE;
5713
5714 if (quit_all || (check_more(FALSE, forceit) == OK && only_one_window()))
5715 {
5716 apply_autocmds(EVENT_EXITPRE, NULL, NULL, FALSE, curbuf);
5717 /* Refuse to quit when locked or when the buffer in the last window is
5718 * being closed (can only happen in autocommands). */
5719 if (curbuf_locked()
5720 || (curbuf->b_nwindows == 1 && curbuf->b_locked > 0))
5721 return TRUE;
5722 }
5723
5724 return FALSE;
5725}
5726
Bram Moolenaar071d4272004-06-13 20:20:40 +00005727/*
Bram Moolenaarf240e182014-11-27 18:33:02 +01005728 * ":quit": quit current window, quit Vim if the last window is closed.
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005729 * ":{nr}quit": quit window {nr}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005730 */
5731 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005732ex_quit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005733{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005734 win_T *wp;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005735
Bram Moolenaar071d4272004-06-13 20:20:40 +00005736#ifdef FEAT_CMDWIN
5737 if (cmdwin_type != 0)
5738 {
5739 cmdwin_result = Ctrl_C;
5740 return;
5741 }
5742#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005743 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005744 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005745 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005746 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005747 return;
5748 }
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005749 if (eap->addr_count > 0)
5750 {
Bram Moolenaarf240e182014-11-27 18:33:02 +01005751 int wnr = eap->line2;
5752
5753 for (wp = firstwin; wp->w_next != NULL; wp = wp->w_next)
5754 if (--wnr <= 0)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005755 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005756 }
5757 else
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005758 wp = curwin;
Bram Moolenaarf240e182014-11-27 18:33:02 +01005759
Bram Moolenaar87ffb5c2017-10-19 12:37:42 +02005760 /* Refuse to quit when locked. */
5761 if (curbuf_locked())
5762 return;
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005763
5764 /* Trigger QuitPre and maybe ExitPre */
5765 if (before_quit_autocmds(wp, FALSE, eap->forceit))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005766 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005767
5768#ifdef FEAT_NETBEANS_INTG
5769 netbeansForcedQuit = eap->forceit;
5770#endif
5771
5772 /*
5773 * If there are more files or windows we won't exit.
5774 */
5775 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
5776 exiting = TRUE;
Bram Moolenaarff930ca2017-10-19 17:12:10 +02005777 if ((!buf_hide(wp->w_buffer)
5778 && check_changed(wp->w_buffer, (p_awa ? CCGD_AW : 0)
Bram Moolenaar45d3b142013-11-09 03:31:51 +01005779 | (eap->forceit ? CCGD_FORCEIT : 0)
5780 | CCGD_EXCMD))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005781 || check_more(TRUE, eap->forceit) == FAIL
Bram Moolenaar027387f2016-01-02 22:25:52 +01005782 || (only_one_window() && check_changed_any(eap->forceit, TRUE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005783 {
5784 not_exiting();
5785 }
5786 else
5787 {
Bram Moolenaarc7a0d322015-06-19 12:43:07 +02005788 /* quit last window
5789 * Note: only_one_window() returns true, even so a help window is
5790 * still open. In that case only quit, if no address has been
5791 * specified. Example:
5792 * :h|wincmd w|1q - don't quit
5793 * :h|wincmd w|q - quit
5794 */
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01005795 if (only_one_window() && (ONE_WINDOW || eap->addr_count == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005796 getout(0);
Bram Moolenaar2c33d7b2017-10-14 16:06:20 +02005797 not_exiting();
Bram Moolenaar4033c552017-09-16 20:54:51 +02005798#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00005799 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005800#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005801 /* close window; may free buffer */
Bram Moolenaareb44a682017-08-03 22:44:55 +02005802 win_close(wp, !buf_hide(wp->w_buffer) || eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005803 }
5804}
5805
5806/*
5807 * ":cquit".
5808 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005809 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005810ex_cquit(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005811{
5812 getout(1); /* this does not always pass on the exit code to the Manx
5813 compiler. why? */
5814}
5815
5816/*
5817 * ":qall": try to quit all windows
5818 */
5819 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005820ex_quit_all(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005821{
5822# ifdef FEAT_CMDWIN
5823 if (cmdwin_type != 0)
5824 {
5825 if (eap->forceit)
5826 cmdwin_result = K_XF1; /* ex_window() takes care of this */
5827 else
5828 cmdwin_result = K_XF2;
5829 return;
5830 }
5831# endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005832
5833 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005834 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005835 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00005836 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005837 return;
5838 }
Bram Moolenaar12a96de2018-03-11 14:44:18 +01005839
5840 if (before_quit_autocmds(curwin, TRUE, eap->forceit))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005841 return;
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00005842
Bram Moolenaar071d4272004-06-13 20:20:40 +00005843 exiting = TRUE;
Bram Moolenaar027387f2016-01-02 22:25:52 +01005844 if (eap->forceit || !check_changed_any(FALSE, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005845 getout(0);
5846 not_exiting();
5847}
5848
Bram Moolenaar071d4272004-06-13 20:20:40 +00005849/*
5850 * ":close": close current window, unless it is the last one
5851 */
5852 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005853ex_close(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005854{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005855 win_T *win;
5856 int winnr = 0;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005857#ifdef FEAT_CMDWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00005858 if (cmdwin_type != 0)
Bram Moolenaar9bd1a7e2011-05-19 14:50:54 +02005859 cmdwin_result = Ctrl_C;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005860 else
Bram Moolenaar4033c552017-09-16 20:54:51 +02005861#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005862 if (!text_locked() && !curbuf_locked())
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005863 {
5864 if (eap->addr_count == 0)
5865 ex_win_close(eap->forceit, curwin, NULL);
Bram Moolenaar6d41c782018-06-06 09:11:12 +02005866 else
5867 {
Bram Moolenaar29323592016-07-24 22:04:11 +02005868 FOR_ALL_WINDOWS(win)
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01005869 {
5870 winnr++;
5871 if (winnr == eap->line2)
5872 break;
5873 }
5874 if (win == NULL)
5875 win = lastwin;
5876 ex_win_close(eap->forceit, win, NULL);
5877 }
5878 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005879}
5880
Bram Moolenaar4033c552017-09-16 20:54:51 +02005881#ifdef FEAT_QUICKFIX
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005882/*
5883 * ":pclose": Close any preview window.
5884 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005885 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005886ex_pclose(exarg_T *eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005887{
5888 win_T *win;
5889
Bram Moolenaar29323592016-07-24 22:04:11 +02005890 FOR_ALL_WINDOWS(win)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005891 if (win->w_p_pvw)
5892 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00005893 ex_win_close(eap->forceit, win, NULL);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005894 break;
5895 }
5896}
Bram Moolenaar4033c552017-09-16 20:54:51 +02005897#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005898
Bram Moolenaarf740b292006-02-16 22:11:02 +00005899/*
5900 * Close window "win" and take care of handling closing the last window for a
5901 * modified buffer.
5902 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005903 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005904ex_win_close(
5905 int forceit,
5906 win_T *win,
5907 tabpage_T *tp) /* NULL or the tab page "win" is in */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005908{
5909 int need_hide;
5910 buf_T *buf = win->w_buffer;
5911
5912 need_hide = (bufIsChanged(buf) && buf->b_nwindows <= 1);
Bram Moolenaareb44a682017-08-03 22:44:55 +02005913 if (need_hide && !buf_hide(buf) && !forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005914 {
Bram Moolenaar4033c552017-09-16 20:54:51 +02005915#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005916 if ((p_confirm || cmdmod.confirm) && p_write)
5917 {
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005918 bufref_T bufref;
5919
5920 set_bufref(&bufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005921 dialog_changed(buf, FALSE);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02005922 if (bufref_valid(&bufref) && bufIsChanged(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005923 return;
5924 need_hide = FALSE;
5925 }
5926 else
Bram Moolenaar4033c552017-09-16 20:54:51 +02005927#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005928 {
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02005929 no_write_message();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005930 return;
5931 }
5932 }
5933
Bram Moolenaar4033c552017-09-16 20:54:51 +02005934#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00005935 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02005936#endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00005937
Bram Moolenaar071d4272004-06-13 20:20:40 +00005938 /* free buffer when not hiding it or when it's a scratch buffer */
Bram Moolenaarf740b292006-02-16 22:11:02 +00005939 if (tp == NULL)
Bram Moolenaareb44a682017-08-03 22:44:55 +02005940 win_close(win, !need_hide && !buf_hide(buf));
Bram Moolenaarf740b292006-02-16 22:11:02 +00005941 else
Bram Moolenaareb44a682017-08-03 22:44:55 +02005942 win_close_othertab(win, !need_hide && !buf_hide(buf), tp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005943}
5944
Bram Moolenaar071d4272004-06-13 20:20:40 +00005945/*
Bram Moolenaardea25702017-01-29 15:18:10 +01005946 * Handle the argument for a tabpage related ex command.
5947 * Returns a tabpage number.
5948 * When an error is encountered then eap->errmsg is set.
5949 */
5950 static int
5951get_tabpage_arg(exarg_T *eap)
5952{
5953 int tab_number;
5954 int unaccept_arg0 = (eap->cmdidx == CMD_tabmove) ? 0 : 1;
5955
5956 if (eap->arg && *eap->arg != NUL)
5957 {
5958 char_u *p = eap->arg;
5959 char_u *p_save;
5960 int relative = 0; /* argument +N/-N means: go to N places to the
5961 * right/left relative to the current position. */
5962
5963 if (*p == '-')
5964 {
5965 relative = -1;
5966 p++;
5967 }
5968 else if (*p == '+')
5969 {
5970 relative = 1;
5971 p++;
5972 }
5973
5974 p_save = p;
5975 tab_number = getdigits(&p);
5976
5977 if (relative == 0)
5978 {
5979 if (STRCMP(p, "$") == 0)
5980 tab_number = LAST_TAB_NR;
5981 else if (p == p_save || *p_save == '-' || *p != NUL
5982 || tab_number > LAST_TAB_NR)
5983 {
5984 /* No numbers as argument. */
5985 eap->errmsg = e_invarg;
5986 goto theend;
5987 }
5988 }
5989 else
5990 {
5991 if (*p_save == NUL)
5992 tab_number = 1;
5993 else if (p == p_save || *p_save == '-' || *p != NUL
5994 || tab_number == 0)
5995 {
5996 /* No numbers as argument. */
5997 eap->errmsg = e_invarg;
5998 goto theend;
5999 }
6000 tab_number = tab_number * relative + tabpage_index(curtab);
6001 if (!unaccept_arg0 && relative == -1)
6002 --tab_number;
6003 }
6004 if (tab_number < unaccept_arg0 || tab_number > LAST_TAB_NR)
6005 eap->errmsg = e_invarg;
6006 }
6007 else if (eap->addr_count > 0)
6008 {
6009 if (unaccept_arg0 && eap->line2 == 0)
Bram Moolenaarc6251552017-01-29 21:42:20 +01006010 {
Bram Moolenaardea25702017-01-29 15:18:10 +01006011 eap->errmsg = e_invrange;
Bram Moolenaarc6251552017-01-29 21:42:20 +01006012 tab_number = 0;
6013 }
Bram Moolenaardea25702017-01-29 15:18:10 +01006014 else
6015 {
6016 tab_number = eap->line2;
Bram Moolenaar82a12462019-01-22 22:41:42 +01006017 if (!unaccept_arg0 && *skipwhite(*eap->cmdlinep) == '-')
Bram Moolenaardea25702017-01-29 15:18:10 +01006018 {
6019 --tab_number;
6020 if (tab_number < unaccept_arg0)
6021 eap->errmsg = e_invarg;
6022 }
6023 }
6024 }
6025 else
6026 {
6027 switch (eap->cmdidx)
6028 {
6029 case CMD_tabnext:
6030 tab_number = tabpage_index(curtab) + 1;
6031 if (tab_number > LAST_TAB_NR)
6032 tab_number = 1;
6033 break;
6034 case CMD_tabmove:
6035 tab_number = LAST_TAB_NR;
6036 break;
6037 default:
6038 tab_number = tabpage_index(curtab);
6039 }
6040 }
6041
6042theend:
6043 return tab_number;
6044}
6045
6046/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00006047 * ":tabclose": close current tab page, unless it is the last one.
6048 * ":tabclose N": close tab page N.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006049 */
6050 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006051ex_tabclose(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006052{
Bram Moolenaarf740b292006-02-16 22:11:02 +00006053 tabpage_T *tp;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006054 int tab_number;
Bram Moolenaarf740b292006-02-16 22:11:02 +00006055
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006056# ifdef FEAT_CMDWIN
6057 if (cmdwin_type != 0)
6058 cmdwin_result = K_IGNORE;
6059 else
6060# endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00006061 if (first_tabpage->tp_next == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006062 emsg(_("E784: Cannot close last tab page"));
Bram Moolenaarf740b292006-02-16 22:11:02 +00006063 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006064 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006065 tab_number = get_tabpage_arg(eap);
6066 if (eap->errmsg == NULL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006067 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006068 tp = find_tabpage(tab_number);
Bram Moolenaarf740b292006-02-16 22:11:02 +00006069 if (tp == NULL)
6070 {
6071 beep_flush();
6072 return;
6073 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00006074 if (tp != curtab)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006075 {
6076 tabpage_close_other(tp, eap->forceit);
6077 return;
6078 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006079 else if (!text_locked() && !curbuf_locked())
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006080 tabpage_close(eap->forceit);
6081 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006082 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006083}
6084
6085/*
6086 * ":tabonly": close all tab pages except the current one
6087 */
6088 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006089ex_tabonly(exarg_T *eap)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006090{
6091 tabpage_T *tp;
6092 int done;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006093 int tab_number;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006094
6095# ifdef FEAT_CMDWIN
6096 if (cmdwin_type != 0)
6097 cmdwin_result = K_IGNORE;
6098 else
6099# endif
6100 if (first_tabpage->tp_next == NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01006101 msg(_("Already only one tab page"));
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006102 else
6103 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006104 tab_number = get_tabpage_arg(eap);
6105 if (eap->errmsg == NULL)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006106 {
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006107 goto_tabpage(tab_number);
6108 /* Repeat this up to a 1000 times, because autocommands may
6109 * mess up the lists. */
6110 for (done = 0; done < 1000; ++done)
6111 {
6112 FOR_ALL_TABPAGES(tp)
6113 if (tp->tp_topframe != topframe)
6114 {
6115 tabpage_close_other(tp, eap->forceit);
6116 /* if we failed to close it quit */
6117 if (valid_tabpage(tp))
6118 done = 1000;
6119 /* start over, "tp" is now invalid */
6120 break;
6121 }
6122 if (first_tabpage->tp_next == NULL)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006123 break;
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006124 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006125 }
6126 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006127}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006128
6129/*
Bram Moolenaarf740b292006-02-16 22:11:02 +00006130 * Close the current tab page.
6131 */
6132 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006133tabpage_close(int forceit)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006134{
6135 /* First close all the windows but the current one. If that worked then
6136 * close the last window in this tab, that will close it. */
Bram Moolenaar459ca562016-11-10 18:16:33 +01006137 if (!ONE_WINDOW)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006138 close_others(TRUE, forceit);
Bram Moolenaar459ca562016-11-10 18:16:33 +01006139 if (ONE_WINDOW)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006140 ex_win_close(forceit, curwin, NULL);
6141# ifdef FEAT_GUI
6142 need_mouse_correct = TRUE;
6143# endif
6144}
6145
6146/*
6147 * Close tab page "tp", which is not the current tab page.
6148 * Note that autocommands may make "tp" invalid.
Bram Moolenaar7875acc2006-09-10 13:51:17 +00006149 * Also takes care of the tab pages line disappearing when closing the
6150 * last-but-one tab page.
Bram Moolenaarf740b292006-02-16 22:11:02 +00006151 */
6152 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006153tabpage_close_other(tabpage_T *tp, int forceit)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006154{
6155 int done = 0;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006156 win_T *wp;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00006157 int h = tabline_height();
Bram Moolenaarf740b292006-02-16 22:11:02 +00006158
6159 /* Limit to 1000 windows, autocommands may add a window while we close
6160 * one. OK, so I'm paranoid... */
6161 while (++done < 1000)
6162 {
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006163 wp = tp->tp_firstwin;
6164 ex_win_close(forceit, wp, tp);
Bram Moolenaarf740b292006-02-16 22:11:02 +00006165
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006166 /* Autocommands may delete the tab page under our fingers and we may
6167 * fail to close a window with a modified buffer. */
6168 if (!valid_tabpage(tp) || tp->tp_firstwin == wp)
Bram Moolenaarf740b292006-02-16 22:11:02 +00006169 break;
6170 }
Bram Moolenaar7875acc2006-09-10 13:51:17 +00006171
Bram Moolenaar29323592016-07-24 22:04:11 +02006172 apply_autocmds(EVENT_TABCLOSED, NULL, NULL, FALSE, curbuf);
Bram Moolenaar29323592016-07-24 22:04:11 +02006173
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006174 redraw_tabline = TRUE;
Bram Moolenaar7875acc2006-09-10 13:51:17 +00006175 if (h != tabline_height())
6176 shell_new_rows();
Bram Moolenaarf740b292006-02-16 22:11:02 +00006177}
6178
6179/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006180 * ":only".
6181 */
6182 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006183ex_only(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006184{
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01006185 win_T *wp;
6186 int wnr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006187# ifdef FEAT_GUI
6188 need_mouse_correct = TRUE;
6189# endif
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01006190 if (eap->addr_count > 0)
6191 {
6192 wnr = eap->line2;
6193 for (wp = firstwin; --wnr > 0; )
6194 {
6195 if (wp->w_next == NULL)
6196 break;
6197 else
6198 wp = wp->w_next;
6199 }
6200 win_goto(wp);
6201 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006202 close_others(TRUE, eap->forceit);
6203}
6204
6205/*
6206 * ":all" and ":sall".
Bram Moolenaard9967712006-03-11 21:18:15 +00006207 * Also used for ":tab drop file ..." after setting the argument list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006208 */
Bram Moolenaard9967712006-03-11 21:18:15 +00006209 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006210ex_all(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006211{
6212 if (eap->addr_count == 0)
6213 eap->line2 = 9999;
Bram Moolenaard9967712006-03-11 21:18:15 +00006214 do_arg_all((int)eap->line2, eap->forceit, eap->cmdidx == CMD_drop);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006215}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006216
6217 static void
Bram Moolenaar5e1e6d22017-01-02 17:26:00 +01006218ex_hide(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006219{
Bram Moolenaar2256c992016-11-15 21:17:07 +01006220 /* ":hide" or ":hide | cmd": hide current window */
Bram Moolenaar2256c992016-11-15 21:17:07 +01006221 if (!eap->skip)
6222 {
Bram Moolenaar4033c552017-09-16 20:54:51 +02006223#ifdef FEAT_GUI
Bram Moolenaar2256c992016-11-15 21:17:07 +01006224 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006225#endif
Bram Moolenaar2256c992016-11-15 21:17:07 +01006226 if (eap->addr_count == 0)
6227 win_close(curwin, FALSE); /* don't free buffer */
6228 else
6229 {
6230 int winnr = 0;
6231 win_T *win;
Bram Moolenaarf240e182014-11-27 18:33:02 +01006232
Bram Moolenaar2256c992016-11-15 21:17:07 +01006233 FOR_ALL_WINDOWS(win)
6234 {
6235 winnr++;
6236 if (winnr == eap->line2)
6237 break;
Bram Moolenaarb96a7f32014-11-27 16:22:48 +01006238 }
Bram Moolenaar2256c992016-11-15 21:17:07 +01006239 if (win == NULL)
6240 win = lastwin;
6241 win_close(win, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006242 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006243 }
6244}
6245
6246/*
6247 * ":stop" and ":suspend": Suspend Vim.
6248 */
6249 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006250ex_stop(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006251{
6252 /*
6253 * Disallow suspending for "rvim".
6254 */
Bram Moolenaarcea912a2016-10-12 14:20:24 +02006255 if (!check_restricted())
Bram Moolenaar071d4272004-06-13 20:20:40 +00006256 {
6257 if (!eap->forceit)
6258 autowrite_all();
6259 windgoto((int)Rows - 1, 0);
6260 out_char('\n');
6261 out_flush();
6262 stoptermcap();
6263 out_flush(); /* needed for SUN to restore xterm buffer */
6264#ifdef FEAT_TITLE
Bram Moolenaar40385db2018-08-07 22:31:44 +02006265 mch_restore_title(SAVE_RESTORE_BOTH); /* restore window titles */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006266#endif
6267 ui_suspend(); /* call machine specific function */
6268#ifdef FEAT_TITLE
6269 maketitle();
6270 resettitle(); /* force updating the title */
6271#endif
6272 starttermcap();
6273 scroll_start(); /* scroll screen before redrawing */
6274 redraw_later_clear();
6275 shell_resized(); /* may have resized window */
6276 }
6277}
6278
6279/*
Bram Moolenaar12a96de2018-03-11 14:44:18 +01006280 * ":exit", ":xit" and ":wq": Write file and quite the current window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006281 */
6282 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006283ex_exit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006284{
6285#ifdef FEAT_CMDWIN
6286 if (cmdwin_type != 0)
6287 {
6288 cmdwin_result = Ctrl_C;
6289 return;
6290 }
6291#endif
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006292 /* Don't quit while editing the command line. */
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006293 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006294 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00006295 text_locked_msg();
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00006296 return;
6297 }
Bram Moolenaar12a96de2018-03-11 14:44:18 +01006298
6299 if (before_quit_autocmds(curwin, FALSE, eap->forceit))
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006300 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006301
6302 /*
6303 * if more files or windows we won't exit
6304 */
6305 if (check_more(FALSE, eap->forceit) == OK && only_one_window())
6306 exiting = TRUE;
6307 if ( ((eap->cmdidx == CMD_wq
6308 || curbufIsChanged())
6309 && do_write(eap) == FAIL)
6310 || check_more(TRUE, eap->forceit) == FAIL
Bram Moolenaar027387f2016-01-02 22:25:52 +01006311 || (only_one_window() && check_changed_any(eap->forceit, FALSE)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006312 {
6313 not_exiting();
6314 }
6315 else
6316 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006317 if (only_one_window()) /* quit last window, exit Vim */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006318 getout(0);
Bram Moolenaar2c33d7b2017-10-14 16:06:20 +02006319 not_exiting();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006320# ifdef FEAT_GUI
6321 need_mouse_correct = TRUE;
6322# endif
Bram Moolenaar1a4a75c2013-07-28 16:03:06 +02006323 /* Quit current window, may free the buffer. */
Bram Moolenaareb44a682017-08-03 22:44:55 +02006324 win_close(curwin, !buf_hide(curwin->w_buffer));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006325 }
6326}
6327
6328/*
6329 * ":print", ":list", ":number".
6330 */
6331 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006332ex_print(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006333{
Bram Moolenaardf177f62005-02-22 08:39:57 +00006334 if (curbuf->b_ml.ml_flags & ML_EMPTY)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006335 emsg(_(e_emptybuf));
Bram Moolenaardf177f62005-02-22 08:39:57 +00006336 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006337 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00006338 for ( ;!got_int; ui_breakcheck())
6339 {
6340 print_line(eap->line1,
6341 (eap->cmdidx == CMD_number || eap->cmdidx == CMD_pound
6342 || (eap->flags & EXFLAG_NR)),
6343 eap->cmdidx == CMD_list || (eap->flags & EXFLAG_LIST));
6344 if (++eap->line1 > eap->line2)
6345 break;
6346 out_flush(); /* show one line at a time */
6347 }
6348 setpcmark();
6349 /* put cursor at last line */
6350 curwin->w_cursor.lnum = eap->line2;
6351 beginline(BL_SOL | BL_FIX);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006352 }
6353
Bram Moolenaar071d4272004-06-13 20:20:40 +00006354 ex_no_reprint = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006355}
6356
6357#ifdef FEAT_BYTEOFF
6358 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006359ex_goto(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006360{
6361 goto_byte(eap->line2);
6362}
6363#endif
6364
6365/*
6366 * ":shell".
6367 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006368 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006369ex_shell(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006370{
6371 do_shell(NULL, 0);
6372}
6373
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006374#if defined(HAVE_DROP_FILE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006375
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006376static int drop_busy = FALSE;
6377static int drop_filec;
6378static char_u **drop_filev = NULL;
6379static int drop_split;
6380static void (*drop_callback)(void *);
6381static void *drop_cookie;
6382
6383 static void
6384handle_drop_internal(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006385{
6386 exarg_T ea;
6387 int save_msg_scroll = msg_scroll;
6388
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006389 // Setting the argument list may cause screen updates and being called
6390 // recursively. Avoid that by setting drop_busy.
6391 drop_busy = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006392
6393 /* Check whether the current buffer is changed. If so, we will need
6394 * to split the current window or data could be lost.
6395 * We don't need to check if the 'hidden' option is set, as in this
6396 * case the buffer won't be lost.
6397 */
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006398 if (!buf_hide(curbuf) && !drop_split)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006399 {
6400 ++emsg_off;
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006401 drop_split = check_changed(curbuf, CCGD_AW);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006402 --emsg_off;
6403 }
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006404 if (drop_split)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006405 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006406 if (win_split(0, 0) == FAIL)
6407 return;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02006408 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006409
6410 /* When splitting the window, create a new alist. Otherwise the
6411 * existing one is overwritten. */
6412 alist_unlink(curwin->w_alist);
6413 alist_new();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006414 }
6415
6416 /*
6417 * Set up the new argument list.
6418 */
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006419 alist_set(ALIST(curwin), drop_filec, drop_filev, FALSE, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006420
6421 /*
6422 * Move to the first file.
6423 */
6424 /* Fake up a minimal "next" command for do_argfile() */
6425 vim_memset(&ea, 0, sizeof(ea));
6426 ea.cmd = (char_u *)"next";
6427 do_argfile(&ea, 0);
6428
6429 /* do_ecmd() may set need_start_insertmode, but since we never left Insert
6430 * mode that is not needed here. */
6431 need_start_insertmode = FALSE;
6432
6433 /* Restore msg_scroll, otherwise a following command may cause scrolling
6434 * unexpectedly. The screen will be redrawn by the caller, thus
6435 * msg_scroll being set by displaying a message is irrelevant. */
6436 msg_scroll = save_msg_scroll;
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006437
6438 if (drop_callback != NULL)
6439 drop_callback(drop_cookie);
6440
6441 drop_filev = NULL;
6442 drop_busy = FALSE;
6443}
6444
6445/*
6446 * Handle a file drop. The code is here because a drop is *nearly* like an
6447 * :args command, but not quite (we have a list of exact filenames, so we
Bram Moolenaarb5443cc2019-01-15 20:19:40 +01006448 * don't want to (a) parse a command line, or (b) expand wildcards). So the
Bram Moolenaar92d147b2018-07-29 17:35:23 +02006449 * code is very similar to :args and hence needs access to a lot of the static
6450 * functions in this file.
6451 *
6452 * The "filev" list must have been allocated using alloc(), as should each item
6453 * in the list. This function takes over responsibility for freeing the "filev"
6454 * list.
6455 */
6456 void
6457handle_drop(
6458 int filec, // the number of files dropped
6459 char_u **filev, // the list of files dropped
6460 int split, // force splitting the window
6461 void (*callback)(void *), // to be called after setting the argument
6462 // list
6463 void *cookie) // argument for "callback" (allocated)
6464{
6465 // Cannot handle recursive drops, finish the pending one.
6466 if (drop_busy)
6467 {
6468 FreeWild(filec, filev);
6469 vim_free(cookie);
6470 return;
6471 }
6472
6473 // When calling handle_drop() more than once in a row we only use the last
6474 // one.
6475 if (drop_filev != NULL)
6476 {
6477 FreeWild(drop_filec, drop_filev);
6478 vim_free(drop_cookie);
6479 }
6480
6481 drop_filec = filec;
6482 drop_filev = filev;
6483 drop_split = split;
6484 drop_callback = callback;
6485 drop_cookie = cookie;
6486
6487 // Postpone this when:
6488 // - editing the command line
6489 // - not possible to change the current buffer
6490 // - updating the screen
6491 // As it may change buffers and window structures that are in use and cause
6492 // freed memory to be used.
6493 if (text_locked() || curbuf_locked() || updating_screen)
6494 return;
6495
6496 handle_drop_internal();
6497}
6498
6499/*
6500 * To be called when text is unlocked, curbuf is unlocked or updating_screen is
6501 * reset: Handle a postponed drop.
6502 */
6503 void
6504handle_any_postponed_drop(void)
6505{
6506 if (!drop_busy && drop_filev != NULL
6507 && !text_locked() && !curbuf_locked() && !updating_screen)
6508 handle_drop_internal();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006509}
6510#endif
6511
Bram Moolenaar071d4272004-06-13 20:20:40 +00006512/*
6513 * Clear an argument list: free all file names and reset it to zero entries.
6514 */
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00006515 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006516alist_clear(alist_T *al)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006517{
6518 while (--al->al_ga.ga_len >= 0)
6519 vim_free(AARGLIST(al)[al->al_ga.ga_len].ae_fname);
6520 ga_clear(&al->al_ga);
6521}
6522
6523/*
6524 * Init an argument list.
6525 */
6526 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006527alist_init(alist_T *al)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006528{
6529 ga_init2(&al->al_ga, (int)sizeof(aentry_T), 5);
6530}
6531
Bram Moolenaar071d4272004-06-13 20:20:40 +00006532/*
6533 * Remove a reference from an argument list.
6534 * Ignored when the argument list is the global one.
6535 * If the argument list is no longer used by any window, free it.
6536 */
6537 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006538alist_unlink(alist_T *al)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006539{
6540 if (al != &global_alist && --al->al_refcount <= 0)
6541 {
6542 alist_clear(al);
6543 vim_free(al);
6544 }
6545}
6546
Bram Moolenaar071d4272004-06-13 20:20:40 +00006547/*
6548 * Create a new argument list and use it for the current window.
6549 */
6550 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006551alist_new(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006552{
Bram Moolenaar964b3742019-05-24 18:54:09 +02006553 curwin->w_alist = (alist_T *)alloc(sizeof(alist_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006554 if (curwin->w_alist == NULL)
6555 {
6556 curwin->w_alist = &global_alist;
6557 ++global_alist.al_refcount;
6558 }
6559 else
6560 {
6561 curwin->w_alist->al_refcount = 1;
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02006562 curwin->w_alist->id = ++max_alist_id;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006563 alist_init(curwin->w_alist);
6564 }
6565}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006566
Bram Moolenaara06ecab2016-07-16 14:47:36 +02006567#if !defined(UNIX) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006568/*
6569 * Expand the file names in the global argument list.
Bram Moolenaar86b68352004-12-27 21:59:20 +00006570 * If "fnum_list" is not NULL, use "fnum_list[fnum_len]" as a list of buffer
6571 * numbers to be re-used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006572 */
6573 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006574alist_expand(int *fnum_list, int fnum_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006575{
6576 char_u **old_arg_files;
Bram Moolenaar86b68352004-12-27 21:59:20 +00006577 int old_arg_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006578 char_u **new_arg_files;
6579 int new_arg_file_count;
6580 char_u *save_p_su = p_su;
6581 int i;
6582
6583 /* Don't use 'suffixes' here. This should work like the shell did the
6584 * expansion. Also, the vimrc file isn't read yet, thus the user
6585 * can't set the options. */
6586 p_su = empty_option;
Bram Moolenaar964b3742019-05-24 18:54:09 +02006587 old_arg_files = (char_u **)alloc(sizeof(char_u *) * GARGCOUNT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006588 if (old_arg_files != NULL)
6589 {
6590 for (i = 0; i < GARGCOUNT; ++i)
Bram Moolenaar86b68352004-12-27 21:59:20 +00006591 old_arg_files[i] = vim_strsave(GARGLIST[i].ae_fname);
6592 old_arg_count = GARGCOUNT;
6593 if (expand_wildcards(old_arg_count, old_arg_files,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006594 &new_arg_file_count, &new_arg_files,
Bram Moolenaarb5609832011-07-20 15:04:58 +02006595 EW_FILE|EW_NOTFOUND|EW_ADDSLASH|EW_NOERROR) == OK
Bram Moolenaar071d4272004-06-13 20:20:40 +00006596 && new_arg_file_count > 0)
6597 {
Bram Moolenaar86b68352004-12-27 21:59:20 +00006598 alist_set(&global_alist, new_arg_file_count, new_arg_files,
6599 TRUE, fnum_list, fnum_len);
6600 FreeWild(old_arg_count, old_arg_files);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006601 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006602 }
6603 p_su = save_p_su;
6604}
6605#endif
6606
6607/*
6608 * Set the argument list for the current window.
6609 * Takes over the allocated files[] and the allocated fnames in it.
6610 */
6611 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006612alist_set(
6613 alist_T *al,
6614 int count,
6615 char_u **files,
6616 int use_curbuf,
6617 int *fnum_list,
6618 int fnum_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006619{
6620 int i;
Bram Moolenaar9e33efd2018-02-09 17:50:28 +01006621 static int recursive = 0;
6622
6623 if (recursive)
6624 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006625 emsg(_(e_au_recursive));
Bram Moolenaar9e33efd2018-02-09 17:50:28 +01006626 return;
6627 }
6628 ++recursive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006629
6630 alist_clear(al);
6631 if (ga_grow(&al->al_ga, count) == OK)
6632 {
6633 for (i = 0; i < count; ++i)
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00006634 {
6635 if (got_int)
6636 {
6637 /* When adding many buffers this can take a long time. Allow
6638 * interrupting here. */
6639 while (i < count)
6640 vim_free(files[i++]);
6641 break;
6642 }
Bram Moolenaar86b68352004-12-27 21:59:20 +00006643
6644 /* May set buffer name of a buffer previously used for the
6645 * argument list, so that it's re-used by alist_add. */
6646 if (fnum_list != NULL && i < fnum_len)
6647 buf_set_name(fnum_list[i], files[i]);
6648
Bram Moolenaar071d4272004-06-13 20:20:40 +00006649 alist_add(al, files[i], use_curbuf ? 2 : 1);
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00006650 ui_breakcheck();
6651 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006652 vim_free(files);
6653 }
6654 else
6655 FreeWild(count, files);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006656 if (al == &global_alist)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006657 arg_had_last = FALSE;
Bram Moolenaar9e33efd2018-02-09 17:50:28 +01006658
6659 --recursive;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006660}
6661
6662/*
6663 * Add file "fname" to argument list "al".
6664 * "fname" must have been allocated and "al" must have been checked for room.
6665 */
6666 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006667alist_add(
6668 alist_T *al,
6669 char_u *fname,
6670 int set_fnum) /* 1: set buffer number; 2: re-use curbuf */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006671{
6672 if (fname == NULL) /* don't add NULL file names */
6673 return;
6674#ifdef BACKSLASH_IN_FILENAME
6675 slash_adjust(fname);
6676#endif
6677 AARGLIST(al)[al->al_ga.ga_len].ae_fname = fname;
6678 if (set_fnum > 0)
6679 AARGLIST(al)[al->al_ga.ga_len].ae_fnum =
6680 buflist_add(fname, BLN_LISTED | (set_fnum == 2 ? BLN_CURBUF : 0));
6681 ++al->al_ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006682}
6683
6684#if defined(BACKSLASH_IN_FILENAME) || defined(PROTO)
6685/*
6686 * Adjust slashes in file names. Called after 'shellslash' was set.
6687 */
6688 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006689alist_slash_adjust(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006690{
6691 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006692 win_T *wp;
Bram Moolenaarf740b292006-02-16 22:11:02 +00006693 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006694
6695 for (i = 0; i < GARGCOUNT; ++i)
6696 if (GARGLIST[i].ae_fname != NULL)
6697 slash_adjust(GARGLIST[i].ae_fname);
Bram Moolenaarf740b292006-02-16 22:11:02 +00006698 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006699 if (wp->w_alist != &global_alist)
6700 for (i = 0; i < WARGCOUNT(wp); ++i)
6701 if (WARGLIST(wp)[i].ae_fname != NULL)
6702 slash_adjust(WARGLIST(wp)[i].ae_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006703}
6704#endif
6705
6706/*
6707 * ":preserve".
6708 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006709 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006710ex_preserve(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006711{
Bram Moolenaar4399ef42005-02-12 14:29:27 +00006712 curbuf->b_flags |= BF_PRESERVED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006713 ml_preserve(curbuf, TRUE);
6714}
6715
6716/*
6717 * ":recover".
6718 */
6719 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006720ex_recover(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006721{
6722 /* Set recoverymode right away to avoid the ATTENTION prompt. */
6723 recoverymode = TRUE;
Bram Moolenaar45d3b142013-11-09 03:31:51 +01006724 if (!check_changed(curbuf, (p_awa ? CCGD_AW : 0)
6725 | CCGD_MULTWIN
6726 | (eap->forceit ? CCGD_FORCEIT : 0)
6727 | CCGD_EXCMD)
6728
Bram Moolenaar071d4272004-06-13 20:20:40 +00006729 && (*eap->arg == NUL
6730 || setfname(curbuf, eap->arg, NULL, TRUE) == OK))
Bram Moolenaar99499b12019-05-23 21:35:48 +02006731 ml_recover(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006732 recoverymode = FALSE;
6733}
6734
6735/*
6736 * Command modifier used in a wrong way.
6737 */
6738 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006739ex_wrongmodifier(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006740{
6741 eap->errmsg = e_invcmd;
6742}
6743
Bram Moolenaar071d4272004-06-13 20:20:40 +00006744/*
6745 * :sview [+command] file split window with new file, read-only
6746 * :split [[+command] file] split window with current or new file
6747 * :vsplit [[+command] file] split window vertically with current or new file
6748 * :new [[+command] file] split window with no or new file
6749 * :vnew [[+command] file] split vertically window with no or new file
6750 * :sfind [+command] file split window with file in 'path'
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006751 *
6752 * :tabedit open new Tab page with empty window
6753 * :tabedit [+command] file open new Tab page and edit "file"
6754 * :tabnew [[+command] file] just like :tabedit
6755 * :tabfind [+command] file open new Tab page and find "file"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006756 */
6757 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006758ex_splitview(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006759{
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006760 win_T *old_curwin = curwin;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006761#if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006762 char_u *fname = NULL;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006763#endif
6764#ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00006765 int browse_flag = cmdmod.browse;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006766#endif
Bram Moolenaar39902a02018-06-21 22:10:08 +02006767 int use_tab = eap->cmdidx == CMD_tabedit
6768 || eap->cmdidx == CMD_tabfind
6769 || eap->cmdidx == CMD_tabnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006770
Bram Moolenaar4033c552017-09-16 20:54:51 +02006771#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00006772 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02006773#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006774
Bram Moolenaar4033c552017-09-16 20:54:51 +02006775#ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00006776 /* A ":split" in the quickfix window works like ":new". Don't want two
Bram Moolenaar05bb9532008-07-04 09:44:11 +00006777 * quickfix windows. But it's OK when doing ":tab split". */
6778 if (bt_quickfix(curbuf) && cmdmod.tab == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006779 {
6780 if (eap->cmdidx == CMD_split)
6781 eap->cmdidx = CMD_new;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006782 if (eap->cmdidx == CMD_vsplit)
6783 eap->cmdidx = CMD_vnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006784 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02006785#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006786
Bram Moolenaar4033c552017-09-16 20:54:51 +02006787#ifdef FEAT_SEARCHPATH
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006788 if (eap->cmdidx == CMD_sfind || eap->cmdidx == CMD_tabfind)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006789 {
6790 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg),
6791 FNAME_MESS, TRUE, curbuf->b_ffname);
6792 if (fname == NULL)
6793 goto theend;
6794 eap->arg = fname;
6795 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006796# ifdef FEAT_BROWSE
Bram Moolenaar4033c552017-09-16 20:54:51 +02006797 else
6798# endif
6799#endif
6800#ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00006801 if (cmdmod.browse
Bram Moolenaar071d4272004-06-13 20:20:40 +00006802 && eap->cmdidx != CMD_vnew
Bram Moolenaar071d4272004-06-13 20:20:40 +00006803 && eap->cmdidx != CMD_new)
6804 {
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006805 if (
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006806# ifdef FEAT_GUI
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006807 !gui.in_use &&
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006808# endif
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006809 au_has_group((char_u *)"FileExplorer"))
6810 {
6811 /* No browsing supported but we do have the file explorer:
6812 * Edit the directory. */
6813 if (*eap->arg == NUL || !mch_isdir(eap->arg))
6814 eap->arg = (char_u *)".";
6815 }
6816 else
6817 {
Bram Moolenaar39902a02018-06-21 22:10:08 +02006818 fname = do_browse(0, (char_u *)(use_tab
6819 ? _("Edit File in new tab page")
6820 : _("Edit File in new window")),
Bram Moolenaar071d4272004-06-13 20:20:40 +00006821 eap->arg, NULL, NULL, NULL, curbuf);
Bram Moolenaar1d94f9b2005-08-04 21:29:45 +00006822 if (fname == NULL)
6823 goto theend;
6824 eap->arg = fname;
6825 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006826 }
6827 cmdmod.browse = FALSE; /* Don't browse again in do_ecmd(). */
Bram Moolenaar4033c552017-09-16 20:54:51 +02006828#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006829
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006830 /*
6831 * Either open new tab page or split the window.
6832 */
Bram Moolenaar39902a02018-06-21 22:10:08 +02006833 if (use_tab)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006834 {
Bram Moolenaar8dff8182006-04-06 20:18:50 +00006835 if (win_new_tabpage(cmdmod.tab != 0 ? cmdmod.tab
6836 : eap->addr_count == 0 ? 0
6837 : (int)eap->line2 + 1) != FAIL)
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006838 {
Bram Moolenaard2b66012008-01-09 19:30:36 +00006839 do_exedit(eap, old_curwin);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00006840
6841 /* set the alternate buffer for the window we came from */
6842 if (curwin != old_curwin
6843 && win_valid(old_curwin)
6844 && old_curwin->w_buffer != curbuf
6845 && !cmdmod.keepalt)
6846 old_curwin->w_alt_fnum = curbuf->b_fnum;
6847 }
6848 }
6849 else if (win_split(eap->addr_count > 0 ? (int)eap->line2 : 0,
Bram Moolenaar071d4272004-06-13 20:20:40 +00006850 *eap->cmd == 'v' ? WSP_VERT : 0) != FAIL)
6851 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00006852 /* Reset 'scrollbind' when editing another file, but keep it when
6853 * doing ":split" without arguments. */
6854 if (*eap->arg != NUL
Bram Moolenaar8a3bb562018-03-04 20:14:14 +01006855# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00006856 || cmdmod.browse
Bram Moolenaar8a3bb562018-03-04 20:14:14 +01006857# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006858 )
Bram Moolenaar3368ea22010-09-21 16:56:35 +02006859 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006860 else
6861 do_check_scrollbind(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006862 do_exedit(eap, old_curwin);
6863 }
6864
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006865# ifdef FEAT_BROWSE
Bram Moolenaar071d4272004-06-13 20:20:40 +00006866 cmdmod.browse = browse_flag;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006867# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006868
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006869# if defined(FEAT_SEARCHPATH) || defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006870theend:
6871 vim_free(fname);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006872# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006873}
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006874
6875/*
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006876 * Open a new tab page.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006877 */
6878 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006879tabpage_new(void)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006880{
6881 exarg_T ea;
6882
6883 vim_memset(&ea, 0, sizeof(ea));
6884 ea.cmdidx = CMD_tabnew;
6885 ea.cmd = (char_u *)"tabn";
6886 ea.arg = (char_u *)"";
6887 ex_splitview(&ea);
6888}
6889
6890/*
6891 * :tabnext command
6892 */
6893 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006894ex_tabnext(exarg_T *eap)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006895{
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006896 int tab_number;
6897
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006898 switch (eap->cmdidx)
6899 {
6900 case CMD_tabfirst:
6901 case CMD_tabrewind:
6902 goto_tabpage(1);
6903 break;
6904 case CMD_tablast:
6905 goto_tabpage(9999);
6906 break;
6907 case CMD_tabprevious:
6908 case CMD_tabNext:
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006909 if (eap->arg && *eap->arg != NUL)
6910 {
6911 char_u *p = eap->arg;
6912 char_u *p_save = p;
6913
6914 tab_number = getdigits(&p);
6915 if (p == p_save || *p_save == '-' || *p != NUL
6916 || tab_number == 0)
6917 {
6918 /* No numbers as argument. */
6919 eap->errmsg = e_invarg;
6920 return;
6921 }
6922 }
6923 else
6924 {
6925 if (eap->addr_count == 0)
6926 tab_number = 1;
6927 else
6928 {
6929 tab_number = eap->line2;
6930 if (tab_number < 1)
6931 {
6932 eap->errmsg = e_invrange;
6933 return;
6934 }
6935 }
6936 }
6937 goto_tabpage(-tab_number);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006938 break;
6939 default: /* CMD_tabnext */
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006940 tab_number = get_tabpage_arg(eap);
6941 if (eap->errmsg == NULL)
6942 goto_tabpage(tab_number);
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006943 break;
6944 }
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006945}
6946
6947/*
6948 * :tabmove command
6949 */
6950 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006951ex_tabmove(exarg_T *eap)
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006952{
Bram Moolenaar40ce3a42015-04-21 18:08:39 +02006953 int tab_number;
Bram Moolenaar8cb8dca2012-07-06 18:27:39 +02006954
Bram Moolenaar2f72c702017-01-29 14:48:10 +01006955 tab_number = get_tabpage_arg(eap);
6956 if (eap->errmsg == NULL)
6957 tabpage_move(tab_number);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006958}
6959
6960/*
6961 * :tabs command: List tabs and their contents.
6962 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006963 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006964ex_tabs(exarg_T *eap UNUSED)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006965{
6966 tabpage_T *tp;
6967 win_T *wp;
6968 int tabcount = 1;
6969
6970 msg_start();
6971 msg_scroll = TRUE;
6972 for (tp = first_tabpage; tp != NULL && !got_int; tp = tp->tp_next)
6973 {
6974 msg_putchar('\n');
6975 vim_snprintf((char *)IObuff, IOSIZE, _("Tab page %d"), tabcount++);
Bram Moolenaar8820b482017-03-16 17:23:31 +01006976 msg_outtrans_attr(IObuff, HL_ATTR(HLF_T));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006977 out_flush(); /* output one line at a time */
6978 ui_breakcheck();
6979
Bram Moolenaar030f0df2006-02-21 22:02:53 +00006980 if (tp == curtab)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006981 wp = firstwin;
6982 else
6983 wp = tp->tp_firstwin;
6984 for ( ; wp != NULL && !got_int; wp = wp->w_next)
6985 {
Bram Moolenaar80a94a52006-02-23 21:26:58 +00006986 msg_putchar('\n');
6987 msg_putchar(wp == curwin ? '>' : ' ');
6988 msg_putchar(' ');
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00006989 msg_putchar(bufIsChanged(wp->w_buffer) ? '+' : ' ');
6990 msg_putchar(' ');
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006991 if (buf_spname(wp->w_buffer) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02006992 vim_strncpy(IObuff, buf_spname(wp->w_buffer), IOSIZE - 1);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006993 else
6994 home_replace(wp->w_buffer, wp->w_buffer->b_fname,
6995 IObuff, IOSIZE, TRUE);
6996 msg_outtrans(IObuff);
6997 out_flush(); /* output one line at a time */
6998 ui_breakcheck();
6999 }
7000 }
7001}
7002
Bram Moolenaar071d4272004-06-13 20:20:40 +00007003/*
7004 * ":mode": Set screen mode.
7005 * If no argument given, just get the screen size and redraw.
7006 */
7007 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007008ex_mode(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007009{
7010 if (*eap->arg == NUL)
7011 shell_resized();
7012 else
7013 mch_screenmode(eap->arg);
7014}
7015
Bram Moolenaar071d4272004-06-13 20:20:40 +00007016/*
7017 * ":resize".
7018 * set, increment or decrement current window height
7019 */
7020 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007021ex_resize(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007022{
7023 int n;
7024 win_T *wp = curwin;
7025
7026 if (eap->addr_count > 0)
7027 {
7028 n = eap->line2;
7029 for (wp = firstwin; wp->w_next != NULL && --n > 0; wp = wp->w_next)
7030 ;
7031 }
7032
Bram Moolenaar44a2f922016-03-19 22:11:51 +01007033# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007034 need_mouse_correct = TRUE;
Bram Moolenaar44a2f922016-03-19 22:11:51 +01007035# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007036 n = atol((char *)eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007037 if (cmdmod.split & WSP_VERT)
7038 {
7039 if (*eap->arg == '-' || *eap->arg == '+')
Bram Moolenaar02631462017-09-22 15:20:32 +02007040 n += curwin->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007041 else if (n == 0 && eap->arg[0] == NUL) /* default is very wide */
7042 n = 9999;
7043 win_setwidth_win((int)n, wp);
7044 }
7045 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007046 {
7047 if (*eap->arg == '-' || *eap->arg == '+')
7048 n += curwin->w_height;
Bram Moolenaar1f2903c2017-07-23 19:51:01 +02007049 else if (n == 0 && eap->arg[0] == NUL) /* default is very high */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007050 n = 9999;
7051 win_setheight_win((int)n, wp);
7052 }
7053}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007054
7055/*
7056 * ":find [+command] <file>" command.
7057 */
7058 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007059ex_find(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007060{
7061#ifdef FEAT_SEARCHPATH
7062 char_u *fname;
7063 int count;
7064
7065 fname = find_file_in_path(eap->arg, (int)STRLEN(eap->arg), FNAME_MESS,
7066 TRUE, curbuf->b_ffname);
7067 if (eap->addr_count > 0)
7068 {
7069 /* Repeat finding the file "count" times. This matters when it
7070 * appears several times in the path. */
7071 count = eap->line2;
7072 while (fname != NULL && --count > 0)
7073 {
7074 vim_free(fname);
7075 fname = find_file_in_path(NULL, 0, FNAME_MESS,
7076 FALSE, curbuf->b_ffname);
7077 }
7078 }
7079
7080 if (fname != NULL)
7081 {
7082 eap->arg = fname;
7083#endif
7084 do_exedit(eap, NULL);
7085#ifdef FEAT_SEARCHPATH
7086 vim_free(fname);
7087 }
7088#endif
7089}
7090
7091/*
Bram Moolenaardf177f62005-02-22 08:39:57 +00007092 * ":open" simulation: for now just work like ":visual".
7093 */
7094 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007095ex_open(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007096{
7097 regmatch_T regmatch;
7098 char_u *p;
7099
7100 curwin->w_cursor.lnum = eap->line2;
7101 beginline(BL_SOL | BL_FIX);
7102 if (*eap->arg == '/')
7103 {
7104 /* ":open /pattern/": put cursor in column found with pattern */
7105 ++eap->arg;
7106 p = skip_regexp(eap->arg, '/', p_magic, NULL);
7107 *p = NUL;
7108 regmatch.regprog = vim_regcomp(eap->arg, p_magic ? RE_MAGIC : 0);
7109 if (regmatch.regprog != NULL)
7110 {
7111 regmatch.rm_ic = p_ic;
7112 p = ml_get_curline();
7113 if (vim_regexec(&regmatch, p, (colnr_T)0))
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007114 curwin->w_cursor.col = (colnr_T)(regmatch.startp[0] - p);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007115 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007116 emsg(_(e_nomatch));
Bram Moolenaar473de612013-06-08 18:19:48 +02007117 vim_regfree(regmatch.regprog);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007118 }
7119 /* Move to the NUL, ignore any other arguments. */
7120 eap->arg += STRLEN(eap->arg);
7121 }
7122 check_cursor();
7123
7124 eap->cmdidx = CMD_visual;
7125 do_exedit(eap, NULL);
7126}
7127
7128/*
7129 * ":edit", ":badd", ":visual".
Bram Moolenaar071d4272004-06-13 20:20:40 +00007130 */
7131 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007132ex_edit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007133{
7134 do_exedit(eap, NULL);
7135}
7136
7137/*
7138 * ":edit <file>" command and alikes.
7139 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007140 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007141do_exedit(
7142 exarg_T *eap,
7143 win_T *old_curwin) /* curwin before doing a split or NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007144{
7145 int n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007146 int need_hide;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007147 int exmode_was = exmode_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007148
7149 /*
7150 * ":vi" command ends Ex mode.
7151 */
7152 if (exmode_active && (eap->cmdidx == CMD_visual
7153 || eap->cmdidx == CMD_view))
7154 {
7155 exmode_active = FALSE;
7156 if (*eap->arg == NUL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007157 {
7158 /* Special case: ":global/pat/visual\NLvi-commands" */
7159 if (global_busy)
7160 {
7161 int rd = RedrawingDisabled;
7162 int nwr = no_wait_return;
7163 int ms = msg_scroll;
7164#ifdef FEAT_GUI
7165 int he = hold_gui_events;
7166#endif
7167
7168 if (eap->nextcmd != NULL)
7169 {
7170 stuffReadbuff(eap->nextcmd);
7171 eap->nextcmd = NULL;
7172 }
7173
7174 if (exmode_was != EXMODE_VIM)
7175 settmode(TMODE_RAW);
7176 RedrawingDisabled = 0;
7177 no_wait_return = 0;
7178 need_wait_return = FALSE;
7179 msg_scroll = 0;
7180#ifdef FEAT_GUI
7181 hold_gui_events = 0;
7182#endif
7183 must_redraw = CLEAR;
7184
7185 main_loop(FALSE, TRUE);
7186
7187 RedrawingDisabled = rd;
7188 no_wait_return = nwr;
7189 msg_scroll = ms;
7190#ifdef FEAT_GUI
7191 hold_gui_events = he;
7192#endif
7193 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007194 return;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007195 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007196 }
7197
7198 if ((eap->cmdidx == CMD_new
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007199 || eap->cmdidx == CMD_tabnew
7200 || eap->cmdidx == CMD_tabedit
Bram Moolenaar4033c552017-09-16 20:54:51 +02007201 || eap->cmdidx == CMD_vnew) && *eap->arg == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007202 {
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00007203 /* ":new" or ":tabnew" without argument: edit an new empty buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007204 setpcmark();
7205 (void)do_ecmd(0, NULL, NULL, eap, ECMD_ONE,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00007206 ECMD_HIDE + (eap->forceit ? ECMD_FORCEIT : 0),
7207 old_curwin == NULL ? curwin : NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007208 }
Bram Moolenaar4033c552017-09-16 20:54:51 +02007209 else if ((eap->cmdidx != CMD_split && eap->cmdidx != CMD_vsplit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007210 || *eap->arg != NUL
7211#ifdef FEAT_BROWSE
7212 || cmdmod.browse
7213#endif
7214 )
7215 {
Bram Moolenaar5555acc2006-04-07 21:33:12 +00007216 /* Can't edit another file when "curbuf_lock" is set. Only ":edit"
7217 * can bring us here, others are stopped earlier. */
7218 if (*eap->arg != NUL && curbuf_locked())
7219 return;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007220
Bram Moolenaar071d4272004-06-13 20:20:40 +00007221 n = readonlymode;
7222 if (eap->cmdidx == CMD_view || eap->cmdidx == CMD_sview)
7223 readonlymode = TRUE;
7224 else if (eap->cmdidx == CMD_enew)
7225 readonlymode = FALSE; /* 'readonly' doesn't make sense in an
7226 empty buffer */
7227 setpcmark();
7228 if (do_ecmd(0, (eap->cmdidx == CMD_enew ? NULL : eap->arg),
7229 NULL, eap,
7230 /* ":edit" goes to first line if Vi compatible */
7231 (*eap->arg == NUL && eap->do_ecmd_lnum == 0
7232 && vim_strchr(p_cpo, CPO_GOTO1) != NULL)
7233 ? ECMD_ONE : eap->do_ecmd_lnum,
Bram Moolenaareb44a682017-08-03 22:44:55 +02007234 (buf_hide(curbuf) ? ECMD_HIDE : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007235 + (eap->forceit ? ECMD_FORCEIT : 0)
Bram Moolenaar7b449342014-03-25 13:03:48 +01007236 /* after a split we can use an existing buffer */
7237 + (old_curwin != NULL ? ECMD_OLDBUF : 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007238 + (eap->cmdidx == CMD_badd ? ECMD_ADDBUF : 0 )
Bram Moolenaar701f7af2008-11-15 13:12:07 +00007239 , old_curwin == NULL ? curwin : NULL) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007240 {
7241 /* Editing the file failed. If the window was split, close it. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007242 if (old_curwin != NULL)
7243 {
7244 need_hide = (curbufIsChanged() && curbuf->b_nwindows <= 1);
Bram Moolenaareb44a682017-08-03 22:44:55 +02007245 if (!need_hide || buf_hide(curbuf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007246 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007247#if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007248 cleanup_T cs;
7249
7250 /* Reset the error/interrupt/exception state here so that
7251 * aborting() returns FALSE when closing a window. */
7252 enter_cleanup(&cs);
Bram Moolenaar4033c552017-09-16 20:54:51 +02007253#endif
7254#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00007255 need_mouse_correct = TRUE;
Bram Moolenaar4033c552017-09-16 20:54:51 +02007256#endif
Bram Moolenaareb44a682017-08-03 22:44:55 +02007257 win_close(curwin, !need_hide && !buf_hide(curbuf));
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007258
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007259#if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00007260 /* Restore the error/interrupt/exception state if not
7261 * discarded by a new aborting error, interrupt, or
7262 * uncaught exception. */
7263 leave_cleanup(&cs);
Bram Moolenaar4033c552017-09-16 20:54:51 +02007264#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007265 }
7266 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007267 }
7268 else if (readonlymode && curbuf->b_nwindows == 1)
7269 {
7270 /* When editing an already visited buffer, 'readonly' won't be set
7271 * but the previous value is kept. With ":view" and ":sview" we
7272 * want the file to be readonly, except when another window is
7273 * editing the same buffer. */
7274 curbuf->b_p_ro = TRUE;
7275 }
7276 readonlymode = n;
7277 }
7278 else
7279 {
7280 if (eap->do_ecmd_cmd != NULL)
7281 do_cmdline_cmd(eap->do_ecmd_cmd);
7282#ifdef FEAT_TITLE
7283 n = curwin->w_arg_idx_invalid;
7284#endif
7285 check_arg_idx(curwin);
7286#ifdef FEAT_TITLE
7287 if (n != curwin->w_arg_idx_invalid)
7288 maketitle();
7289#endif
7290 }
7291
Bram Moolenaar071d4272004-06-13 20:20:40 +00007292 /*
7293 * if ":split file" worked, set alternate file name in old window to new
7294 * file
7295 */
7296 if (old_curwin != NULL
7297 && *eap->arg != NUL
7298 && curwin != old_curwin
7299 && win_valid(old_curwin)
Bram Moolenaard4755bb2004-09-02 19:12:26 +00007300 && old_curwin->w_buffer != curbuf
7301 && !cmdmod.keepalt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007302 old_curwin->w_alt_fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007303
7304 ex_no_reprint = TRUE;
7305}
7306
7307#ifndef FEAT_GUI
7308/*
7309 * ":gui" and ":gvim" when there is no GUI.
7310 */
7311 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007312ex_nogui(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007313{
7314 eap->errmsg = e_nogvim;
7315}
7316#endif
7317
Bram Moolenaar4f974752019-02-17 17:44:42 +01007318#if defined(FEAT_GUI_MSWIN) && defined(FEAT_MENU) && defined(FEAT_TEAROFF)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007319 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007320ex_tearoff(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007321{
7322 gui_make_tearoff(eap->arg);
7323}
7324#endif
7325
Bram Moolenaar29a2c082018-03-05 21:06:23 +01007326#if (defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK) \
7327 || defined(FEAT_TERM_POPUP_MENU)) && defined(FEAT_MENU)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007328 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007329ex_popup(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007330{
Bram Moolenaar29a2c082018-03-05 21:06:23 +01007331# if defined(FEAT_GUI_MSWIN) || defined(FEAT_GUI_GTK)
7332 if (gui.in_use)
7333 gui_make_popup(eap->arg, eap->forceit);
7334# ifdef FEAT_TERM_POPUP_MENU
7335 else
7336# endif
7337# endif
7338# ifdef FEAT_TERM_POPUP_MENU
7339 pum_make_popup(eap->arg, eap->forceit);
7340# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007341}
7342#endif
7343
Bram Moolenaar071d4272004-06-13 20:20:40 +00007344 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007345ex_swapname(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007346{
7347 if (curbuf->b_ml.ml_mfp == NULL || curbuf->b_ml.ml_mfp->mf_fname == NULL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01007348 msg(_("No swap file"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007349 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01007350 msg((char *)curbuf->b_ml.ml_mfp->mf_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007351}
7352
7353/*
7354 * ":syncbind" forces all 'scrollbind' windows to have the same relative
7355 * offset.
7356 * (1998-11-02 16:21:01 R. Edward Ralston <eralston@computer.org>)
7357 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007358 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007359ex_syncbind(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007360{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007361 win_T *wp;
Bram Moolenaardedd1b02013-12-14 13:06:17 +01007362 win_T *save_curwin = curwin;
7363 buf_T *save_curbuf = curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007364 long topline;
7365 long y;
7366 linenr_T old_linenr = curwin->w_cursor.lnum;
7367
7368 setpcmark();
7369
7370 /*
7371 * determine max topline
7372 */
7373 if (curwin->w_p_scb)
7374 {
7375 topline = curwin->w_topline;
Bram Moolenaar29323592016-07-24 22:04:11 +02007376 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007377 {
7378 if (wp->w_p_scb && wp->w_buffer)
7379 {
Bram Moolenaar375e3392019-01-31 18:26:10 +01007380 y = wp->w_buffer->b_ml.ml_line_count - get_scrolloff_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007381 if (topline > y)
7382 topline = y;
7383 }
7384 }
7385 if (topline < 1)
7386 topline = 1;
7387 }
7388 else
7389 {
7390 topline = 1;
7391 }
7392
7393
7394 /*
Bram Moolenaardedd1b02013-12-14 13:06:17 +01007395 * Set all scrollbind windows to the same topline.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007396 */
Bram Moolenaar29323592016-07-24 22:04:11 +02007397 FOR_ALL_WINDOWS(curwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007398 {
7399 if (curwin->w_p_scb)
7400 {
Bram Moolenaardedd1b02013-12-14 13:06:17 +01007401 curbuf = curwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007402 y = topline - curwin->w_topline;
7403 if (y > 0)
7404 scrollup(y, TRUE);
7405 else
7406 scrolldown(-y, TRUE);
7407 curwin->w_scbind_pos = topline;
7408 redraw_later(VALID);
7409 cursor_correct();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007410 curwin->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007411 }
7412 }
Bram Moolenaardedd1b02013-12-14 13:06:17 +01007413 curwin = save_curwin;
7414 curbuf = save_curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007415 if (curwin->w_p_scb)
7416 {
7417 did_syncbind = TRUE;
7418 checkpcmark();
7419 if (old_linenr != curwin->w_cursor.lnum)
7420 {
7421 char_u ctrl_o[2];
7422
7423 ctrl_o[0] = Ctrl_O;
7424 ctrl_o[1] = 0;
7425 ins_typebuf(ctrl_o, REMAP_NONE, 0, TRUE, FALSE);
7426 }
7427 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007428}
7429
7430
7431 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007432ex_read(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007433{
Bram Moolenaardf177f62005-02-22 08:39:57 +00007434 int i;
7435 int empty = (curbuf->b_ml.ml_flags & ML_EMPTY);
7436 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007437
7438 if (eap->usefilter) /* :r!cmd */
7439 do_bang(1, eap, FALSE, FALSE, TRUE);
7440 else
7441 {
7442 if (u_save(eap->line2, (linenr_T)(eap->line2 + 1)) == FAIL)
7443 return;
7444
7445#ifdef FEAT_BROWSE
7446 if (cmdmod.browse)
7447 {
7448 char_u *browseFile;
7449
Bram Moolenaar7171abe2004-10-11 10:06:20 +00007450 browseFile = do_browse(0, (char_u *)_("Append File"), eap->arg,
Bram Moolenaar071d4272004-06-13 20:20:40 +00007451 NULL, NULL, NULL, curbuf);
7452 if (browseFile != NULL)
7453 {
7454 i = readfile(browseFile, NULL,
7455 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
7456 vim_free(browseFile);
7457 }
7458 else
7459 i = OK;
7460 }
7461 else
7462#endif
7463 if (*eap->arg == NUL)
7464 {
7465 if (check_fname() == FAIL) /* check for no file name */
7466 return;
7467 i = readfile(curbuf->b_ffname, curbuf->b_fname,
7468 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
7469 }
7470 else
7471 {
7472 if (vim_strchr(p_cpo, CPO_ALTREAD) != NULL)
7473 (void)setaltfname(eap->arg, eap->arg, (linenr_T)1);
7474 i = readfile(eap->arg, NULL,
7475 eap->line2, (linenr_T)0, (linenr_T)MAXLNUM, eap, 0);
7476
7477 }
Bram Moolenaare13b9af2017-01-13 22:01:02 +01007478 if (i != OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007479 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007480#if defined(FEAT_EVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007481 if (!aborting())
7482#endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007483 semsg(_(e_notopen), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007484 }
7485 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00007486 {
7487 if (empty && exmode_active)
7488 {
7489 /* Delete the empty line that remains. Historically ex does
7490 * this but vi doesn't. */
7491 if (eap->line2 == 0)
7492 lnum = curbuf->b_ml.ml_line_count;
7493 else
7494 lnum = 1;
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00007495 if (*ml_get(lnum) == NUL && u_savedel(lnum, 1L) == OK)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007496 {
7497 ml_delete(lnum, FALSE);
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00007498 if (curwin->w_cursor.lnum > 1
7499 && curwin->w_cursor.lnum >= lnum)
Bram Moolenaardf177f62005-02-22 08:39:57 +00007500 --curwin->w_cursor.lnum;
Bram Moolenaarcdcaa582009-07-09 18:06:49 +00007501 deleted_lines_mark(lnum, 1L);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007502 }
7503 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007504 redraw_curbuf_later(VALID);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007505 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007506 }
7507}
7508
Bram Moolenaarea408852005-06-25 22:49:46 +00007509static char_u *prev_dir = NULL;
7510
7511#if defined(EXITFREE) || defined(PROTO)
7512 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007513free_cd_dir(void)
Bram Moolenaarea408852005-06-25 22:49:46 +00007514{
Bram Moolenaard23a8232018-02-10 18:45:26 +01007515 VIM_CLEAR(prev_dir);
7516 VIM_CLEAR(globaldir);
Bram Moolenaarea408852005-06-25 22:49:46 +00007517}
7518#endif
7519
Bram Moolenaarf4258302013-06-02 18:20:17 +02007520/*
7521 * Deal with the side effects of changing the current directory.
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007522 * When 'scope' is CDSCOPE_TABPAGE then this was after an ":tcd" command.
7523 * When 'scope' is CDSCOPE_WINDOW then this was after an ":lcd" command.
Bram Moolenaarf4258302013-06-02 18:20:17 +02007524 */
7525 void
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007526post_chdir(cdscope_T scope)
Bram Moolenaarf4258302013-06-02 18:20:17 +02007527{
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007528 if (scope != CDSCOPE_WINDOW)
Bram Moolenaar00aa0692019-04-27 20:37:57 +02007529 // Clear tab local directory for both :cd and :tcd
7530 VIM_CLEAR(curtab->tp_localdir);
Bram Moolenaard23a8232018-02-10 18:45:26 +01007531 VIM_CLEAR(curwin->w_localdir);
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007532 if (scope != CDSCOPE_GLOBAL)
Bram Moolenaarf4258302013-06-02 18:20:17 +02007533 {
7534 /* If still in global directory, need to remember current
7535 * directory as global directory. */
7536 if (globaldir == NULL && prev_dir != NULL)
7537 globaldir = vim_strsave(prev_dir);
7538 /* Remember this local directory for the window. */
7539 if (mch_dirname(NameBuff, MAXPATHL) == OK)
Bram Moolenaar00aa0692019-04-27 20:37:57 +02007540 {
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007541 if (scope == CDSCOPE_TABPAGE)
Bram Moolenaar00aa0692019-04-27 20:37:57 +02007542 curtab->tp_localdir = vim_strsave(NameBuff);
7543 else
7544 curwin->w_localdir = vim_strsave(NameBuff);
7545 }
Bram Moolenaarf4258302013-06-02 18:20:17 +02007546 }
7547 else
7548 {
7549 /* We are now in the global directory, no need to remember its
7550 * name. */
Bram Moolenaard23a8232018-02-10 18:45:26 +01007551 VIM_CLEAR(globaldir);
Bram Moolenaarf4258302013-06-02 18:20:17 +02007552 }
7553
7554 shorten_fnames(TRUE);
7555}
7556
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007557/*
7558 * Change directory function used by :cd/:tcd/:lcd Ex commands and the
7559 * chdir() function. If 'winlocaldir' is TRUE, then changes the window-local
7560 * directory. If 'tablocaldir' is TRUE, then changes the tab-local directory.
7561 * Otherwise changes the global directory.
7562 * Returns TRUE if the directory is successfully changed.
7563 */
7564 int
7565changedir_func(
7566 char_u *new_dir,
7567 int forceit,
7568 cdscope_T scope)
7569{
7570 char_u *tofree;
7571 int dir_differs;
7572 int retval = FALSE;
7573
7574 if (allbuf_locked())
7575 return FALSE;
7576
7577 if (vim_strchr(p_cpo, CPO_CHDIR) != NULL && curbufIsChanged() && !forceit)
7578 {
7579 emsg(_("E747: Cannot change directory, buffer is modified (add ! to override)"));
7580 return FALSE;
7581 }
7582
7583 // ":cd -": Change to previous directory
7584 if (STRCMP(new_dir, "-") == 0)
7585 {
7586 if (prev_dir == NULL)
7587 {
7588 emsg(_("E186: No previous directory"));
7589 return FALSE;
7590 }
7591 new_dir = prev_dir;
7592 }
7593
7594 // Save current directory for next ":cd -"
7595 tofree = prev_dir;
7596 if (mch_dirname(NameBuff, MAXPATHL) == OK)
7597 prev_dir = vim_strsave(NameBuff);
7598 else
7599 prev_dir = NULL;
7600
7601#if defined(UNIX) || defined(VMS)
7602 // for UNIX ":cd" means: go to home directory
7603 if (*new_dir == NUL)
7604 {
7605 // use NameBuff for home directory name
7606# ifdef VMS
7607 char_u *p;
7608
7609 p = mch_getenv((char_u *)"SYS$LOGIN");
7610 if (p == NULL || *p == NUL) // empty is the same as not set
7611 NameBuff[0] = NUL;
7612 else
7613 vim_strncpy(NameBuff, p, MAXPATHL - 1);
7614# else
7615 expand_env((char_u *)"$HOME", NameBuff, MAXPATHL);
7616# endif
7617 new_dir = NameBuff;
7618 }
7619#endif
7620 dir_differs = new_dir == NULL || prev_dir == NULL
7621 || pathcmp((char *)prev_dir, (char *)new_dir, -1) != 0;
7622 if (new_dir == NULL || (dir_differs && vim_chdir(new_dir)))
7623 emsg(_(e_failed));
7624 else
7625 {
7626 char_u *acmd_fname;
7627
7628 post_chdir(scope);
7629
7630 if (dir_differs)
7631 {
7632 if (scope == CDSCOPE_WINDOW)
7633 acmd_fname = (char_u *)"window";
7634 else if (scope == CDSCOPE_TABPAGE)
7635 acmd_fname = (char_u *)"tabpage";
7636 else
7637 acmd_fname = (char_u *)"global";
7638 apply_autocmds(EVENT_DIRCHANGED, acmd_fname, new_dir, FALSE,
7639 curbuf);
7640 }
7641 retval = TRUE;
7642 }
7643 vim_free(tofree);
7644
7645 return retval;
7646}
Bram Moolenaarea408852005-06-25 22:49:46 +00007647
Bram Moolenaar071d4272004-06-13 20:20:40 +00007648/*
Bram Moolenaar00aa0692019-04-27 20:37:57 +02007649 * ":cd", ":tcd", ":lcd", ":chdir" ":tchdir" and ":lchdir".
Bram Moolenaar071d4272004-06-13 20:20:40 +00007650 */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00007651 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007652ex_cd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007653{
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01007654 char_u *new_dir;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007655
7656 new_dir = eap->arg;
7657#if !defined(UNIX) && !defined(VMS)
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007658 // for non-UNIX ":cd" means: print current directory
Bram Moolenaar071d4272004-06-13 20:20:40 +00007659 if (*new_dir == NUL)
7660 ex_pwd(NULL);
7661 else
7662#endif
7663 {
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007664 cdscope_T scope = CDSCOPE_GLOBAL;
7665
7666 if (eap->cmdidx == CMD_lcd || eap->cmdidx == CMD_lchdir)
7667 scope = CDSCOPE_WINDOW;
7668 else if (eap->cmdidx == CMD_tcd || eap->cmdidx == CMD_tchdir)
7669 scope = CDSCOPE_TABPAGE;
7670
7671 if (changedir_func(new_dir, eap->forceit, scope))
Bram Moolenaardf177f62005-02-22 08:39:57 +00007672 {
Bram Moolenaar1063f3d2019-05-07 22:06:52 +02007673 // Echo the new current directory if the command was typed.
Bram Moolenaarfcfbc672009-07-09 18:13:49 +00007674 if (KeyTyped || p_verbose >= 5)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007675 ex_pwd(eap);
7676 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007677 }
7678}
7679
7680/*
7681 * ":pwd".
7682 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007683 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007684ex_pwd(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007685{
7686 if (mch_dirname(NameBuff, MAXPATHL) == OK)
7687 {
7688#ifdef BACKSLASH_IN_FILENAME
7689 slash_adjust(NameBuff);
7690#endif
Bram Moolenaar32526b32019-01-19 17:43:09 +01007691 msg((char *)NameBuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007692 }
7693 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007694 emsg(_("E187: Unknown"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007695}
7696
7697/*
7698 * ":=".
7699 */
7700 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007701ex_equal(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007702{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007703 smsg("%ld", (long)eap->line2);
Bram Moolenaardf177f62005-02-22 08:39:57 +00007704 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007705}
7706
7707 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007708ex_sleep(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007709{
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007710 int n;
7711 long len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007712
7713 if (cursor_valid())
7714 {
7715 n = W_WINROW(curwin) + curwin->w_wrow - msg_scrolled;
7716 if (n >= 0)
Bram Moolenaar53f81742017-09-22 14:35:51 +02007717 windgoto((int)n, curwin->w_wincol + curwin->w_wcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007718 }
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007719
7720 len = eap->line2;
7721 switch (*eap->arg)
7722 {
7723 case 'm': break;
7724 case NUL: len *= 1000L; break;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007725 default: semsg(_(e_invarg2), eap->arg); return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00007726 }
7727 do_sleep(len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007728}
7729
7730/*
7731 * Sleep for "msec" milliseconds, but keep checking for a CTRL-C every second.
7732 */
7733 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007734do_sleep(long msec)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007735{
7736 long done;
Bram Moolenaar975b5272016-03-15 23:10:59 +01007737 long wait_now;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007738
7739 cursor_on();
Bram Moolenaarf45d7472018-09-30 18:22:26 +02007740 out_flush_cursor(FALSE, FALSE);
Bram Moolenaar975b5272016-03-15 23:10:59 +01007741 for (done = 0; !got_int && done < msec; done += wait_now)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007742 {
Bram Moolenaar975b5272016-03-15 23:10:59 +01007743 wait_now = msec - done > 1000L ? 1000L : msec - done;
7744#ifdef FEAT_TIMERS
7745 {
7746 long due_time = check_due_timer();
7747
7748 if (due_time > 0 && due_time < wait_now)
7749 wait_now = due_time;
7750 }
7751#endif
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02007752#ifdef FEAT_JOB_CHANNEL
7753 if (has_any_channel() && wait_now > 100L)
7754 wait_now = 100L;
7755#endif
Bram Moolenaar975b5272016-03-15 23:10:59 +01007756 ui_delay(wait_now, TRUE);
Bram Moolenaarb9c31e72016-09-29 15:18:57 +02007757#ifdef FEAT_JOB_CHANNEL
7758 if (has_any_channel())
7759 ui_breakcheck_force(TRUE);
7760 else
7761#endif
7762 ui_breakcheck();
Bram Moolenaar93c88e02015-09-15 14:12:05 +02007763#ifdef MESSAGE_QUEUE
7764 /* Process the netbeans and clientserver messages that may have been
7765 * received in the call to ui_breakcheck() when the GUI is in use. This
7766 * may occur when running a test case. */
7767 parse_queued_messages();
Bram Moolenaare3cc6d42011-10-20 21:58:34 +02007768#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007769 }
Bram Moolenaar1ebff3d2018-07-07 16:18:13 +02007770
7771 // If CTRL-C was typed to interrupt the sleep, drop the CTRL-C from the
7772 // input buffer, otherwise a following call to input() fails.
7773 if (got_int)
7774 (void)vpeekc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007775}
7776
7777 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007778do_exmap(exarg_T *eap, int isabbrev)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007779{
7780 int mode;
7781 char_u *cmdp;
7782
7783 cmdp = eap->cmd;
7784 mode = get_map_mode(&cmdp, eap->forceit || isabbrev);
7785
7786 switch (do_map((*cmdp == 'n') ? 2 : (*cmdp == 'u'),
7787 eap->arg, mode, isabbrev))
7788 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007789 case 1: emsg(_(e_invarg));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007790 break;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007791 case 2: emsg((isabbrev ? _(e_noabbr) : _(e_nomap)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007792 break;
7793 }
7794}
7795
7796/*
7797 * ":winsize" command (obsolete).
7798 */
7799 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007800ex_winsize(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007801{
7802 int w, h;
7803 char_u *arg = eap->arg;
7804 char_u *p;
7805
7806 w = getdigits(&arg);
7807 arg = skipwhite(arg);
7808 p = arg;
7809 h = getdigits(&arg);
7810 if (*p != NUL && *arg == NUL)
7811 set_shellsize(w, h, TRUE);
7812 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007813 emsg(_("E465: :winsize requires two number arguments"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007814}
7815
Bram Moolenaar071d4272004-06-13 20:20:40 +00007816 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007817ex_wincmd(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007818{
7819 int xchar = NUL;
7820 char_u *p;
7821
7822 if (*eap->arg == 'g' || *eap->arg == Ctrl_G)
7823 {
7824 /* CTRL-W g and CTRL-W CTRL-G have an extra command character */
7825 if (eap->arg[1] == NUL)
7826 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007827 emsg(_(e_invarg));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007828 return;
7829 }
7830 xchar = eap->arg[1];
7831 p = eap->arg + 2;
7832 }
7833 else
7834 p = eap->arg + 1;
7835
7836 eap->nextcmd = check_nextcmd(p);
7837 p = skipwhite(p);
7838 if (*p != NUL && *p != '"' && eap->nextcmd == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007839 emsg(_(e_invarg));
Bram Moolenaar12bde492011-06-13 01:19:56 +02007840 else if (!eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007841 {
7842 /* Pass flags on for ":vertical wincmd ]". */
7843 postponed_split_flags = cmdmod.split;
Bram Moolenaard326ce82007-03-11 14:48:29 +00007844 postponed_split_tab = cmdmod.tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007845 do_window(*eap->arg, eap->addr_count > 0 ? eap->line2 : 0L, xchar);
7846 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00007847 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007848 }
7849}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007850
Bram Moolenaar843ee412004-06-30 16:16:41 +00007851#if defined(FEAT_GUI) || defined(UNIX) || defined(VMS) || defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007852/*
7853 * ":winpos".
7854 */
7855 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007856ex_winpos(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007857{
7858 int x, y;
7859 char_u *arg = eap->arg;
7860 char_u *p;
7861
7862 if (*arg == NUL)
7863 {
Bram Moolenaar843ee412004-06-30 16:16:41 +00007864# if defined(FEAT_GUI) || defined(MSWIN)
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007865# ifdef VIMDLL
7866 if (gui.in_use ? gui_mch_get_winpos(&x, &y) != FAIL :
7867 mch_get_winpos(&x, &y) != FAIL)
7868# elif defined(FEAT_GUI)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007869 if (gui.in_use && gui_mch_get_winpos(&x, &y) != FAIL)
Bram Moolenaar843ee412004-06-30 16:16:41 +00007870# else
7871 if (mch_get_winpos(&x, &y) != FAIL)
7872# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007873 {
7874 sprintf((char *)IObuff, _("Window position: X %d, Y %d"), x, y);
Bram Moolenaar32526b32019-01-19 17:43:09 +01007875 msg((char *)IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007876 }
7877 else
7878# endif
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007879 emsg(_("E188: Obtaining window position not implemented for this platform"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007880 }
7881 else
7882 {
7883 x = getdigits(&arg);
7884 arg = skipwhite(arg);
7885 p = arg;
7886 y = getdigits(&arg);
7887 if (*p == NUL || *arg != NUL)
7888 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007889 emsg(_("E466: :winpos requires two number arguments"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007890 return;
7891 }
7892# ifdef FEAT_GUI
7893 if (gui.in_use)
7894 gui_mch_set_winpos(x, y);
7895 else if (gui.starting)
7896 {
7897 /* Remember the coordinates for when the window is opened. */
7898 gui_win_x = x;
7899 gui_win_y = y;
7900 }
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007901# if defined(HAVE_TGETENT) || defined(VIMDLL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007902 else
7903# endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02007904# endif
7905# if defined(MSWIN) && (!defined(FEAT_GUI) || defined(VIMDLL))
Bram Moolenaar843ee412004-06-30 16:16:41 +00007906 mch_set_winpos(x, y);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007907# endif
7908# ifdef HAVE_TGETENT
7909 if (*T_CWP)
7910 term_set_winpos(x, y);
7911# endif
7912 }
7913}
7914#endif
7915
7916/*
7917 * Handle command that work like operators: ":delete", ":yank", ":>" and ":<".
7918 */
7919 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007920ex_operators(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007921{
7922 oparg_T oa;
7923
7924 clear_oparg(&oa);
7925 oa.regname = eap->regname;
7926 oa.start.lnum = eap->line1;
7927 oa.end.lnum = eap->line2;
7928 oa.line_count = eap->line2 - eap->line1 + 1;
7929 oa.motion_type = MLINE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007930 virtual_op = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007931 if (eap->cmdidx != CMD_yank) /* position cursor for undo */
7932 {
7933 setpcmark();
7934 curwin->w_cursor.lnum = eap->line1;
7935 beginline(BL_SOL | BL_FIX);
7936 }
7937
Bram Moolenaard07c6e12013-11-21 14:21:40 +01007938 if (VIsual_active)
7939 end_visual_mode();
Bram Moolenaard07c6e12013-11-21 14:21:40 +01007940
Bram Moolenaar071d4272004-06-13 20:20:40 +00007941 switch (eap->cmdidx)
7942 {
7943 case CMD_delete:
7944 oa.op_type = OP_DELETE;
7945 op_delete(&oa);
7946 break;
7947
7948 case CMD_yank:
7949 oa.op_type = OP_YANK;
7950 (void)op_yank(&oa, FALSE, TRUE);
7951 break;
7952
7953 default: /* CMD_rshift or CMD_lshift */
Bram Moolenaar6e707362013-06-14 19:15:58 +02007954 if (
Bram Moolenaar071d4272004-06-13 20:20:40 +00007955#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6e707362013-06-14 19:15:58 +02007956 (eap->cmdidx == CMD_rshift) ^ curwin->w_p_rl
7957#else
7958 eap->cmdidx == CMD_rshift
Bram Moolenaar071d4272004-06-13 20:20:40 +00007959#endif
Bram Moolenaar6e707362013-06-14 19:15:58 +02007960 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00007961 oa.op_type = OP_RSHIFT;
7962 else
7963 oa.op_type = OP_LSHIFT;
7964 op_shift(&oa, FALSE, eap->amount);
7965 break;
7966 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007967 virtual_op = MAYBE;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007968 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007969}
7970
7971/*
7972 * ":put".
7973 */
7974 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007975ex_put(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007976{
7977 /* ":0put" works like ":1put!". */
7978 if (eap->line2 == 0)
7979 {
7980 eap->line2 = 1;
7981 eap->forceit = TRUE;
7982 }
7983 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaardf177f62005-02-22 08:39:57 +00007984 do_put(eap->regname, eap->forceit ? BACKWARD : FORWARD, 1L,
7985 PUT_LINE|PUT_CURSLINE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007986}
7987
7988/*
7989 * Handle ":copy" and ":move".
7990 */
7991 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007992ex_copymove(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007993{
7994 long n;
7995
Bram Moolenaar50eb16c2018-09-15 15:42:40 +02007996 n = get_address(eap, &eap->arg, eap->addr_type, FALSE, FALSE, FALSE, 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007997 if (eap->arg == NULL) /* error detected */
7998 {
7999 eap->nextcmd = NULL;
8000 return;
8001 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00008002 get_flags(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008003
8004 /*
8005 * move or copy lines from 'eap->line1'-'eap->line2' to below line 'n'
8006 */
8007 if (n == MAXLNUM || n < 0 || n > curbuf->b_ml.ml_line_count)
8008 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008009 emsg(_(e_invaddr));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008010 return;
8011 }
8012
8013 if (eap->cmdidx == CMD_move)
8014 {
8015 if (do_move(eap->line1, eap->line2, n) == FAIL)
8016 return;
8017 }
8018 else
8019 ex_copy(eap->line1, eap->line2, n);
8020 u_clearline();
8021 beginline(BL_SOL | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008022 ex_may_print(eap);
8023}
8024
8025/*
8026 * Print the current line if flags were given to the Ex command.
8027 */
Bram Moolenaarfd3fe982014-04-01 17:49:44 +02008028 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008029ex_may_print(exarg_T *eap)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008030{
8031 if (eap->flags != 0)
8032 {
8033 print_line(curwin->w_cursor.lnum, (eap->flags & EXFLAG_NR),
8034 (eap->flags & EXFLAG_LIST));
8035 ex_no_reprint = TRUE;
8036 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008037}
8038
8039/*
8040 * ":smagic" and ":snomagic".
8041 */
8042 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008043ex_submagic(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008044{
8045 int magic_save = p_magic;
8046
8047 p_magic = (eap->cmdidx == CMD_smagic);
8048 do_sub(eap);
8049 p_magic = magic_save;
8050}
8051
8052/*
8053 * ":join".
8054 */
8055 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008056ex_join(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008057{
8058 curwin->w_cursor.lnum = eap->line1;
8059 if (eap->line1 == eap->line2)
8060 {
8061 if (eap->addr_count >= 2) /* :2,2join does nothing */
8062 return;
8063 if (eap->line2 == curbuf->b_ml.ml_line_count)
8064 {
8065 beep_flush();
8066 return;
8067 }
8068 ++eap->line2;
8069 }
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02008070 (void)do_join(eap->line2 - eap->line1 + 1, !eap->forceit, TRUE, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008071 beginline(BL_WHITE | BL_FIX);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008072 ex_may_print(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008073}
8074
8075/*
8076 * ":[addr]@r" or ":[addr]*r": execute register
8077 */
8078 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008079ex_at(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008080{
8081 int c;
Bram Moolenaar60462872009-11-03 11:40:19 +00008082 int prev_len = typebuf.tb_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008083
8084 curwin->w_cursor.lnum = eap->line2;
Bram Moolenaar4930a762016-09-11 14:39:53 +02008085 check_cursor_col();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008086
8087#ifdef USE_ON_FLY_SCROLL
8088 dont_scroll = TRUE; /* disallow scrolling here */
8089#endif
8090
8091 /* get the register name. No name means to use the previous one */
8092 c = *eap->arg;
8093 if (c == NUL || (c == '*' && *eap->cmd == '*'))
8094 c = '@';
Bram Moolenaard333d1e2006-11-07 17:43:47 +00008095 /* Put the register in the typeahead buffer with the "silent" flag. */
8096 if (do_execreg(c, TRUE, vim_strchr(p_cpo, CPO_EXECBUF) != NULL, TRUE)
8097 == FAIL)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008098 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008099 beep_flush();
Bram Moolenaardf177f62005-02-22 08:39:57 +00008100 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008101 else
8102 {
8103 int save_efr = exec_from_reg;
8104
8105 exec_from_reg = TRUE;
8106
8107 /*
8108 * Execute from the typeahead buffer.
Bram Moolenaar60462872009-11-03 11:40:19 +00008109 * Continue until the stuff buffer is empty and all added characters
8110 * have been consumed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008111 */
Bram Moolenaar60462872009-11-03 11:40:19 +00008112 while (!stuff_empty() || typebuf.tb_len > prev_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008113 (void)do_cmdline(NULL, getexline, NULL, DOCMD_NOWAIT|DOCMD_VERBOSE);
8114
8115 exec_from_reg = save_efr;
8116 }
8117}
8118
8119/*
8120 * ":!".
8121 */
8122 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008123ex_bang(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008124{
8125 do_bang(eap->addr_count, eap, eap->forceit, TRUE, TRUE);
8126}
8127
8128/*
8129 * ":undo".
8130 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008131 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +01008132ex_undo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008133{
Bram Moolenaard3667a22006-03-16 21:35:52 +00008134 if (eap->addr_count == 1) /* :undo 123 */
Bram Moolenaar730cde92010-06-27 05:18:54 +02008135 undo_time(eap->line2, FALSE, FALSE, TRUE);
Bram Moolenaard3667a22006-03-16 21:35:52 +00008136 else
8137 u_undo(1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008138}
8139
Bram Moolenaar55debbe2010-05-23 23:34:36 +02008140#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar6df6f472010-07-18 18:04:50 +02008141 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008142ex_wundo(exarg_T *eap)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02008143{
8144 char_u hash[UNDO_HASH_SIZE];
8145
8146 u_compute_hash(hash);
8147 u_write_undo(eap->arg, eap->forceit, curbuf, hash);
8148}
8149
Bram Moolenaar6df6f472010-07-18 18:04:50 +02008150 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008151ex_rundo(exarg_T *eap)
Bram Moolenaar55debbe2010-05-23 23:34:36 +02008152{
8153 char_u hash[UNDO_HASH_SIZE];
8154
8155 u_compute_hash(hash);
Bram Moolenaar6ed8ed82010-05-30 20:40:11 +02008156 u_read_undo(eap->arg, hash, NULL);
Bram Moolenaar55debbe2010-05-23 23:34:36 +02008157}
8158#endif
8159
Bram Moolenaar071d4272004-06-13 20:20:40 +00008160/*
8161 * ":redo".
8162 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008163 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008164ex_redo(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008165{
8166 u_redo(1);
8167}
8168
8169/*
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008170 * ":earlier" and ":later".
8171 */
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008172 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008173ex_later(exarg_T *eap)
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008174{
8175 long count = 0;
8176 int sec = FALSE;
Bram Moolenaar730cde92010-06-27 05:18:54 +02008177 int file = FALSE;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008178 char_u *p = eap->arg;
8179
8180 if (*p == NUL)
8181 count = 1;
8182 else if (isdigit(*p))
8183 {
8184 count = getdigits(&p);
8185 switch (*p)
8186 {
8187 case 's': ++p; sec = TRUE; break;
8188 case 'm': ++p; sec = TRUE; count *= 60; break;
8189 case 'h': ++p; sec = TRUE; count *= 60 * 60; break;
Bram Moolenaar730cde92010-06-27 05:18:54 +02008190 case 'd': ++p; sec = TRUE; count *= 24 * 60 * 60; break;
8191 case 'f': ++p; file = TRUE; break;
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008192 }
8193 }
8194
8195 if (*p != NUL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008196 semsg(_(e_invarg2), eap->arg);
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008197 else
Bram Moolenaar730cde92010-06-27 05:18:54 +02008198 undo_time(eap->cmdidx == CMD_earlier ? -count : count,
8199 sec, file, FALSE);
Bram Moolenaarc7d89352006-03-14 22:53:34 +00008200}
8201
8202/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008203 * ":redir": start/stop redirection.
8204 */
8205 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008206ex_redir(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008207{
8208 char *mode;
8209 char_u *fname;
Bram Moolenaarca472992005-01-21 11:46:23 +00008210 char_u *arg = eap->arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008211
Bram Moolenaarba768492016-07-08 15:32:54 +02008212#ifdef FEAT_EVAL
Bram Moolenaar79815f12016-07-09 17:07:29 +02008213 if (redir_execute)
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02008214 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008215 emsg(_("E930: Cannot use :redir inside execute()"));
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02008216 return;
8217 }
Bram Moolenaarba768492016-07-08 15:32:54 +02008218#endif
Bram Moolenaar245a7cb2016-07-08 10:53:12 +02008219
Bram Moolenaar071d4272004-06-13 20:20:40 +00008220 if (STRICMP(eap->arg, "END") == 0)
8221 close_redir();
8222 else
8223 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008224 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008225 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008226 ++arg;
8227 if (*arg == '>')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008228 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008229 ++arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008230 mode = "a";
8231 }
8232 else
8233 mode = "w";
Bram Moolenaarca472992005-01-21 11:46:23 +00008234 arg = skipwhite(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008235
8236 close_redir();
8237
8238 /* Expand environment variables and "~/". */
Bram Moolenaarca472992005-01-21 11:46:23 +00008239 fname = expand_env_save(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008240 if (fname == NULL)
8241 return;
8242#ifdef FEAT_BROWSE
8243 if (cmdmod.browse)
8244 {
8245 char_u *browseFile;
8246
Bram Moolenaar7171abe2004-10-11 10:06:20 +00008247 browseFile = do_browse(BROWSE_SAVE,
8248 (char_u *)_("Save Redirection"),
Bram Moolenaarc36651b2018-04-29 12:22:56 +02008249 fname, NULL, NULL,
8250 (char_u *)_(BROWSE_FILTER_ALL_FILES), curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008251 if (browseFile == NULL)
8252 return; /* operation cancelled */
8253 vim_free(fname);
8254 fname = browseFile;
8255 eap->forceit = TRUE; /* since dialog already asked */
8256 }
8257#endif
8258
8259 redir_fd = open_exfile(fname, eap->forceit, mode);
8260 vim_free(fname);
8261 }
8262#ifdef FEAT_EVAL
Bram Moolenaarca472992005-01-21 11:46:23 +00008263 else if (*arg == '@')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008264 {
8265 /* redirect to a register a-z (resp. A-Z for appending) */
8266 close_redir();
Bram Moolenaarca472992005-01-21 11:46:23 +00008267 ++arg;
8268 if (ASCII_ISALPHA(*arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008269# ifdef FEAT_CLIPBOARD
Bram Moolenaarca472992005-01-21 11:46:23 +00008270 || *arg == '*'
Bram Moolenaar9a51c6e2006-11-14 19:25:02 +00008271 || *arg == '+'
Bram Moolenaar071d4272004-06-13 20:20:40 +00008272# endif
Bram Moolenaarca472992005-01-21 11:46:23 +00008273 || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00008274 {
Bram Moolenaarca472992005-01-21 11:46:23 +00008275 redir_reg = *arg++;
Bram Moolenaarc188b882007-10-19 14:20:54 +00008276 if (*arg == '>' && arg[1] == '>') /* append */
Bram Moolenaard9d30582005-05-18 22:10:28 +00008277 arg += 2;
Bram Moolenaarc188b882007-10-19 14:20:54 +00008278 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00008279 {
Bram Moolenaarc188b882007-10-19 14:20:54 +00008280 /* Can use both "@a" and "@a>". */
Bram Moolenaar2c29bee2005-06-01 21:46:07 +00008281 if (*arg == '>')
8282 arg++;
Bram Moolenaarc188b882007-10-19 14:20:54 +00008283 /* Make register empty when not using @A-@Z and the
8284 * command is valid. */
8285 if (*arg == NUL && !isupper(redir_reg))
8286 write_reg_contents(redir_reg, (char_u *)"", -1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008287 }
Bram Moolenaardf177f62005-02-22 08:39:57 +00008288 }
8289 if (*arg != NUL)
8290 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00008291 redir_reg = 0;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008292 semsg(_(e_invarg2), eap->arg);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008293 }
8294 }
8295 else if (*arg == '=' && arg[1] == '>')
8296 {
8297 int append;
8298
8299 /* redirect to a variable */
8300 close_redir();
8301 arg += 2;
8302
8303 if (*arg == '>')
8304 {
8305 ++arg;
8306 append = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008307 }
8308 else
Bram Moolenaardf177f62005-02-22 08:39:57 +00008309 append = FALSE;
8310
8311 if (var_redir_start(skipwhite(arg), append) == OK)
8312 redir_vname = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008313 }
8314#endif
8315
8316 /* TODO: redirect to a buffer */
8317
Bram Moolenaar071d4272004-06-13 20:20:40 +00008318 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008319 semsg(_(e_invarg2), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008320 }
Bram Moolenaar29b2d262006-09-10 19:07:28 +00008321
8322 /* Make sure redirection is not off. Can happen for cmdline completion
8323 * that indirectly invokes a command to catch its output. */
8324 if (redir_fd != NULL
8325#ifdef FEAT_EVAL
8326 || redir_reg || redir_vname
8327#endif
8328 )
8329 redir_off = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008330}
8331
8332/*
8333 * ":redraw": force redraw
8334 */
Bram Moolenaarfb1f6262016-01-31 20:24:32 +01008335 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008336ex_redraw(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008337{
8338 int r = RedrawingDisabled;
8339 int p = p_lz;
8340
8341 RedrawingDisabled = 0;
8342 p_lz = FALSE;
Bram Moolenaarabc39ab2017-03-01 18:04:05 +01008343 validate_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008344 update_topline();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01008345 update_screen(eap->forceit ? CLEAR : VIsual_active ? INVERTED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008346#ifdef FEAT_TITLE
8347 if (need_maketitle)
8348 maketitle();
8349#endif
Bram Moolenaarafde13b2019-04-28 19:46:49 +02008350#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
8351# ifdef VIMDLL
8352 if (!gui.in_use)
8353# endif
8354 resize_console_buf();
Bram Moolenaar78d21da2019-02-17 15:00:52 +01008355#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008356 RedrawingDisabled = r;
8357 p_lz = p;
8358
8359 /* Reset msg_didout, so that a message that's there is overwritten. */
8360 msg_didout = FALSE;
8361 msg_col = 0;
8362
Bram Moolenaar56667a52013-07-17 11:54:28 +02008363 /* No need to wait after an intentional redraw. */
8364 need_wait_return = FALSE;
8365
Bram Moolenaar071d4272004-06-13 20:20:40 +00008366 out_flush();
8367}
8368
8369/*
8370 * ":redrawstatus": force redraw of status line(s)
8371 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008372 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008373ex_redrawstatus(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008374{
Bram Moolenaar071d4272004-06-13 20:20:40 +00008375 int r = RedrawingDisabled;
8376 int p = p_lz;
8377
8378 RedrawingDisabled = 0;
8379 p_lz = FALSE;
8380 if (eap->forceit)
8381 status_redraw_all();
8382 else
8383 status_redraw_curbuf();
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01008384 update_screen(VIsual_active ? INVERTED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008385 RedrawingDisabled = r;
8386 p_lz = p;
8387 out_flush();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008388}
8389
Bram Moolenaare12bab32019-01-08 22:02:56 +01008390/*
8391 * ":redrawtabline": force redraw of the tabline
8392 */
8393 static void
8394ex_redrawtabline(exarg_T *eap UNUSED)
8395{
8396 int r = RedrawingDisabled;
8397 int p = p_lz;
8398
8399 RedrawingDisabled = 0;
8400 p_lz = FALSE;
8401
8402 draw_tabline();
8403
8404 RedrawingDisabled = r;
8405 p_lz = p;
8406 out_flush();
8407}
8408
Bram Moolenaar071d4272004-06-13 20:20:40 +00008409 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008410close_redir(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008411{
8412 if (redir_fd != NULL)
8413 {
8414 fclose(redir_fd);
8415 redir_fd = NULL;
8416 }
8417#ifdef FEAT_EVAL
8418 redir_reg = 0;
Bram Moolenaardf177f62005-02-22 08:39:57 +00008419 if (redir_vname)
8420 {
8421 var_redir_stop();
8422 redir_vname = 0;
8423 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008424#endif
8425}
8426
8427#if defined(FEAT_SESSION) && defined(USE_CRNL)
8428# define MKSESSION_NL
8429static int mksession_nl = FALSE; /* use NL only in put_eol() */
8430#endif
8431
8432/*
8433 * ":mkexrc", ":mkvimrc", ":mkview" and ":mksession".
8434 */
8435 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008436ex_mkrc(
8437 exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008438{
8439 FILE *fd;
8440 int failed = FALSE;
8441 char_u *fname;
8442#ifdef FEAT_BROWSE
8443 char_u *browseFile = NULL;
8444#endif
8445#ifdef FEAT_SESSION
8446 int view_session = FALSE;
8447 int using_vdir = FALSE; /* using 'viewdir'? */
8448 char_u *viewFile = NULL;
8449 unsigned *flagp;
8450#endif
8451
8452 if (eap->cmdidx == CMD_mksession || eap->cmdidx == CMD_mkview)
8453 {
8454#ifdef FEAT_SESSION
8455 view_session = TRUE;
8456#else
8457 ex_ni(eap);
8458 return;
8459#endif
8460 }
8461
8462#ifdef FEAT_SESSION
Bram Moolenaar8d594672009-07-01 18:18:57 +00008463 /* Use the short file name until ":lcd" is used. We also don't use the
8464 * short file name when 'acd' is set, that is checked later. */
Bram Moolenaareeefcc72007-05-01 21:21:21 +00008465 did_lcd = FALSE;
8466
Bram Moolenaar071d4272004-06-13 20:20:40 +00008467 /* ":mkview" or ":mkview 9": generate file name with 'viewdir' */
8468 if (eap->cmdidx == CMD_mkview
8469 && (*eap->arg == NUL
8470 || (vim_isdigit(*eap->arg) && eap->arg[1] == NUL)))
8471 {
8472 eap->forceit = TRUE;
8473 fname = get_view_file(*eap->arg);
8474 if (fname == NULL)
8475 return;
8476 viewFile = fname;
8477 using_vdir = TRUE;
8478 }
8479 else
8480#endif
8481 if (*eap->arg != NUL)
8482 fname = eap->arg;
8483 else if (eap->cmdidx == CMD_mkvimrc)
8484 fname = (char_u *)VIMRC_FILE;
8485#ifdef FEAT_SESSION
8486 else if (eap->cmdidx == CMD_mksession)
8487 fname = (char_u *)SESSION_FILE;
8488#endif
8489 else
8490 fname = (char_u *)EXRC_FILE;
8491
8492#ifdef FEAT_BROWSE
8493 if (cmdmod.browse)
8494 {
Bram Moolenaar7171abe2004-10-11 10:06:20 +00008495 browseFile = do_browse(BROWSE_SAVE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00008496# ifdef FEAT_SESSION
8497 eap->cmdidx == CMD_mkview ? (char_u *)_("Save View") :
8498 eap->cmdidx == CMD_mksession ? (char_u *)_("Save Session") :
8499# endif
8500 (char_u *)_("Save Setup"),
Bram Moolenaarc36651b2018-04-29 12:22:56 +02008501 fname, (char_u *)"vim", NULL,
8502 (char_u *)_(BROWSE_FILTER_MACROS), NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008503 if (browseFile == NULL)
8504 goto theend;
8505 fname = browseFile;
8506 eap->forceit = TRUE; /* since dialog already asked */
8507 }
8508#endif
8509
8510#if defined(FEAT_SESSION) && defined(vim_mkdir)
8511 /* When using 'viewdir' may have to create the directory. */
8512 if (using_vdir && !mch_isdir(p_vdir))
Bram Moolenaardf177f62005-02-22 08:39:57 +00008513 vim_mkdir_emsg(p_vdir, 0755);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008514#endif
8515
8516 fd = open_exfile(fname, eap->forceit, WRITEBIN);
8517 if (fd != NULL)
8518 {
8519#ifdef FEAT_SESSION
8520 if (eap->cmdidx == CMD_mkview)
8521 flagp = &vop_flags;
8522 else
8523 flagp = &ssop_flags;
8524#endif
8525
8526#ifdef MKSESSION_NL
8527 /* "unix" in 'sessionoptions': use NL line separator */
8528 if (view_session && (*flagp & SSOP_UNIX))
8529 mksession_nl = TRUE;
8530#endif
8531
8532 /* Write the version command for :mkvimrc */
8533 if (eap->cmdidx == CMD_mkvimrc)
8534 (void)put_line(fd, "version 6.0");
8535
8536#ifdef FEAT_SESSION
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00008537 if (eap->cmdidx == CMD_mksession)
8538 {
8539 if (put_line(fd, "let SessionLoad = 1") == FAIL)
8540 failed = TRUE;
8541 }
8542
Bram Moolenaar071d4272004-06-13 20:20:40 +00008543 if (eap->cmdidx != CMD_mkview)
8544#endif
8545 {
8546 /* Write setting 'compatible' first, because it has side effects.
8547 * For that same reason only do it when needed. */
8548 if (p_cp)
8549 (void)put_line(fd, "if !&cp | set cp | endif");
8550 else
8551 (void)put_line(fd, "if &cp | set nocp | endif");
8552 }
8553
8554#ifdef FEAT_SESSION
8555 if (!view_session
8556 || (eap->cmdidx == CMD_mksession
8557 && (*flagp & SSOP_OPTIONS)))
8558#endif
8559 failed |= (makemap(fd, NULL) == FAIL
8560 || makeset(fd, OPT_GLOBAL, FALSE) == FAIL);
8561
8562#ifdef FEAT_SESSION
8563 if (!failed && view_session)
8564 {
8565 if (put_line(fd, "let s:so_save = &so | let s:siso_save = &siso | set so=0 siso=0") == FAIL)
8566 failed = TRUE;
8567 if (eap->cmdidx == CMD_mksession)
8568 {
Bram Moolenaard9462e32011-04-11 21:35:11 +02008569 char_u *dirnow; /* current directory */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008570
Bram Moolenaard9462e32011-04-11 21:35:11 +02008571 dirnow = alloc(MAXPATHL);
8572 if (dirnow == NULL)
8573 failed = TRUE;
8574 else
8575 {
8576 /*
8577 * Change to session file's dir.
8578 */
8579 if (mch_dirname(dirnow, MAXPATHL) == FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +00008580 || mch_chdir((char *)dirnow) != 0)
Bram Moolenaard9462e32011-04-11 21:35:11 +02008581 *dirnow = NUL;
8582 if (*dirnow != NUL && (ssop_flags & SSOP_SESDIR))
8583 {
Bram Moolenaarb7407d32018-02-03 17:36:27 +01008584 if (vim_chdirfile(fname, NULL) == OK)
Bram Moolenaard9462e32011-04-11 21:35:11 +02008585 shorten_fnames(TRUE);
8586 }
8587 else if (*dirnow != NUL
8588 && (ssop_flags & SSOP_CURDIR) && globaldir != NULL)
8589 {
8590 if (mch_chdir((char *)globaldir) == 0)
8591 shorten_fnames(TRUE);
8592 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008593
Bram Moolenaard9462e32011-04-11 21:35:11 +02008594 failed |= (makeopens(fd, dirnow) == FAIL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008595
Bram Moolenaard9462e32011-04-11 21:35:11 +02008596 /* restore original dir */
8597 if (*dirnow != NUL && ((ssop_flags & SSOP_SESDIR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008598 || ((ssop_flags & SSOP_CURDIR) && globaldir != NULL)))
Bram Moolenaard9462e32011-04-11 21:35:11 +02008599 {
8600 if (mch_chdir((char *)dirnow) != 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008601 emsg(_(e_prev_dir));
Bram Moolenaard9462e32011-04-11 21:35:11 +02008602 shorten_fnames(TRUE);
8603 }
8604 vim_free(dirnow);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008605 }
8606 }
8607 else
8608 {
Bram Moolenaarf13be0d2008-01-02 14:13:10 +00008609 failed |= (put_view(fd, curwin, !using_vdir, flagp,
8610 -1) == FAIL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008611 }
8612 if (put_line(fd, "let &so = s:so_save | let &siso = s:siso_save")
8613 == FAIL)
8614 failed = TRUE;
Bram Moolenaare3c74d22019-01-12 16:29:30 +01008615# ifdef FEAT_SEARCH_EXTRA
8616 if (no_hlsearch && put_line(fd, "nohlsearch") == FAIL)
8617 failed = TRUE;
8618# endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00008619 if (put_line(fd, "doautoall SessionLoadPost") == FAIL)
8620 failed = TRUE;
Bram Moolenaar4770d092006-01-12 23:22:24 +00008621 if (eap->cmdidx == CMD_mksession)
8622 {
8623 if (put_line(fd, "unlet SessionLoad") == FAIL)
8624 failed = TRUE;
8625 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008626 }
8627#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +00008628 if (put_line(fd, "\" vim: set ft=vim :") == FAIL)
8629 failed = TRUE;
8630
Bram Moolenaar071d4272004-06-13 20:20:40 +00008631 failed |= fclose(fd);
8632
8633 if (failed)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008634 emsg(_(e_write));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008635#if defined(FEAT_EVAL) && defined(FEAT_SESSION)
8636 else if (eap->cmdidx == CMD_mksession)
8637 {
8638 /* successful session write - set this_session var */
Bram Moolenaard9462e32011-04-11 21:35:11 +02008639 char_u *tbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008640
Bram Moolenaard9462e32011-04-11 21:35:11 +02008641 tbuf = alloc(MAXPATHL);
8642 if (tbuf != NULL)
8643 {
8644 if (vim_FullName(fname, tbuf, MAXPATHL, FALSE) == OK)
8645 set_vim_var_string(VV_THIS_SESSION, tbuf, -1);
8646 vim_free(tbuf);
8647 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008648 }
8649#endif
8650#ifdef MKSESSION_NL
8651 mksession_nl = FALSE;
8652#endif
8653 }
8654
8655#ifdef FEAT_BROWSE
8656theend:
8657 vim_free(browseFile);
8658#endif
8659#ifdef FEAT_SESSION
8660 vim_free(viewFile);
8661#endif
8662}
8663
Bram Moolenaardf177f62005-02-22 08:39:57 +00008664#if ((defined(FEAT_SESSION) || defined(FEAT_EVAL)) && defined(vim_mkdir)) \
8665 || defined(PROTO)
8666 int
Bram Moolenaarf1d25012016-03-03 12:22:53 +01008667vim_mkdir_emsg(char_u *name, int prot)
Bram Moolenaardf177f62005-02-22 08:39:57 +00008668{
8669 if (vim_mkdir(name, prot) != 0)
8670 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008671 semsg(_("E739: Cannot create directory: %s"), name);
Bram Moolenaardf177f62005-02-22 08:39:57 +00008672 return FAIL;
8673 }
8674 return OK;
8675}
8676#endif
8677
Bram Moolenaar071d4272004-06-13 20:20:40 +00008678/*
8679 * Open a file for writing for an Ex command, with some checks.
8680 * Return file descriptor, or NULL on failure.
8681 */
8682 FILE *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008683open_exfile(
8684 char_u *fname,
8685 int forceit,
8686 char *mode) /* "w" for create new file or "a" for append */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008687{
8688 FILE *fd;
8689
8690#ifdef UNIX
8691 /* with Unix it is possible to open a directory */
8692 if (mch_isdir(fname))
8693 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008694 semsg(_(e_isadir2), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008695 return NULL;
8696 }
8697#endif
8698 if (!forceit && *mode != 'a' && vim_fexists(fname))
8699 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008700 semsg(_("E189: \"%s\" exists (add ! to override)"), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008701 return NULL;
8702 }
8703
8704 if ((fd = mch_fopen((char *)fname, mode)) == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008705 semsg(_("E190: Cannot open \"%s\" for writing"), fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008706
8707 return fd;
8708}
8709
8710/*
8711 * ":mark" and ":k".
8712 */
8713 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008714ex_mark(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008715{
8716 pos_T pos;
8717
8718 if (*eap->arg == NUL) /* No argument? */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008719 emsg(_(e_argreq));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008720 else if (eap->arg[1] != NUL) /* more than one character? */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008721 emsg(_(e_trailing));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008722 else
8723 {
8724 pos = curwin->w_cursor; /* save curwin->w_cursor */
8725 curwin->w_cursor.lnum = eap->line2;
8726 beginline(BL_WHITE | BL_FIX);
8727 if (setmark(*eap->arg) == FAIL) /* set mark */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008728 emsg(_("E191: Argument must be a letter or forward/backward quote"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008729 curwin->w_cursor = pos; /* restore curwin->w_cursor */
8730 }
8731}
8732
8733/*
8734 * Update w_topline, w_leftcol and the cursor position.
8735 */
8736 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008737update_topline_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008738{
8739 check_cursor(); /* put cursor on valid line */
8740 update_topline();
8741 if (!curwin->w_p_wrap)
8742 validate_cursor();
8743 update_curswant();
8744}
8745
Bram Moolenaar071d4272004-06-13 20:20:40 +00008746/*
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008747 * Save the current State and go to Normal mode.
8748 * Return TRUE if the typeahead could be saved.
8749 */
8750 int
8751save_current_state(save_state_T *sst)
8752{
8753 sst->save_msg_scroll = msg_scroll;
8754 sst->save_restart_edit = restart_edit;
8755 sst->save_msg_didout = msg_didout;
8756 sst->save_State = State;
8757 sst->save_insertmode = p_im;
8758 sst->save_finish_op = finish_op;
8759 sst->save_opcount = opcount;
Bram Moolenaarcce713d2019-03-04 11:40:12 +01008760 sst->save_reg_executing = reg_executing;
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008761
8762 msg_scroll = FALSE; /* no msg scrolling in Normal mode */
8763 restart_edit = 0; /* don't go to Insert mode */
8764 p_im = FALSE; /* don't use 'insertmode' */
8765
8766 /*
8767 * Save the current typeahead. This is required to allow using ":normal"
8768 * from an event handler and makes sure we don't hang when the argument
8769 * ends with half a command.
8770 */
8771 save_typeahead(&sst->tabuf);
8772 return sst->tabuf.typebuf_valid;
8773}
8774
8775 void
8776restore_current_state(save_state_T *sst)
8777{
8778 /* Restore the previous typeahead. */
8779 restore_typeahead(&sst->tabuf);
8780
8781 msg_scroll = sst->save_msg_scroll;
8782 restart_edit = sst->save_restart_edit;
8783 p_im = sst->save_insertmode;
8784 finish_op = sst->save_finish_op;
8785 opcount = sst->save_opcount;
Bram Moolenaarcce713d2019-03-04 11:40:12 +01008786 reg_executing = sst->save_reg_executing;
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008787 msg_didout |= sst->save_msg_didout; /* don't reset msg_didout now */
8788
8789 /* Restore the state (needed when called from a function executed for
8790 * 'indentexpr'). Update the mouse and cursor, they may have changed. */
8791 State = sst->save_State;
8792#ifdef CURSOR_SHAPE
8793 ui_cursor_shape(); /* may show different cursor shape */
8794#endif
8795}
8796
8797/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008798 * ":normal[!] {commands}": Execute normal mode commands.
8799 */
Bram Moolenaar20fb9f32016-01-30 23:20:33 +01008800 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008801ex_normal(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008802{
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008803 save_state_T save_state;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008804 char_u *arg = NULL;
8805 int l;
8806 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008807
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008808 if (ex_normal_lock > 0)
8809 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008810 emsg(_(e_secure));
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00008811 return;
8812 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008813 if (ex_normal_busy >= p_mmd)
8814 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008815 emsg(_("E192: Recursive use of :normal too deep"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00008816 return;
8817 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008818
Bram Moolenaar071d4272004-06-13 20:20:40 +00008819 /*
8820 * vgetc() expects a CSI and K_SPECIAL to have been escaped. Don't do
8821 * this for the K_SPECIAL leading byte, otherwise special keys will not
8822 * work.
8823 */
8824 if (has_mbyte)
8825 {
8826 int len = 0;
8827
8828 /* Count the number of characters to be escaped. */
8829 for (p = eap->arg; *p != NUL; ++p)
8830 {
Bram Moolenaar13505972019-01-24 15:04:48 +01008831#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008832 if (*p == CSI) /* leadbyte CSI */
8833 len += 2;
Bram Moolenaar13505972019-01-24 15:04:48 +01008834#endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008835 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008836 if (*++p == K_SPECIAL /* trailbyte K_SPECIAL or CSI */
Bram Moolenaar13505972019-01-24 15:04:48 +01008837#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008838 || *p == CSI
Bram Moolenaar13505972019-01-24 15:04:48 +01008839#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008840 )
8841 len += 2;
8842 }
8843 if (len > 0)
8844 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02008845 arg = alloc(STRLEN(eap->arg) + len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008846 if (arg != NULL)
8847 {
8848 len = 0;
8849 for (p = eap->arg; *p != NUL; ++p)
8850 {
8851 arg[len++] = *p;
Bram Moolenaar13505972019-01-24 15:04:48 +01008852#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008853 if (*p == CSI)
8854 {
8855 arg[len++] = KS_EXTRA;
8856 arg[len++] = (int)KE_CSI;
8857 }
Bram Moolenaar13505972019-01-24 15:04:48 +01008858#endif
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008859 for (l = (*mb_ptr2len)(p) - 1; l > 0; --l)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008860 {
8861 arg[len++] = *++p;
8862 if (*p == K_SPECIAL)
8863 {
8864 arg[len++] = KS_SPECIAL;
8865 arg[len++] = KE_FILLER;
8866 }
Bram Moolenaar13505972019-01-24 15:04:48 +01008867#ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00008868 else if (*p == CSI)
8869 {
8870 arg[len++] = KS_EXTRA;
8871 arg[len++] = (int)KE_CSI;
8872 }
Bram Moolenaar13505972019-01-24 15:04:48 +01008873#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008874 }
8875 arg[len] = NUL;
8876 }
8877 }
8878 }
8879 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008880
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008881 ++ex_normal_busy;
8882 if (save_current_state(&save_state))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008883 {
8884 /*
8885 * Repeat the :normal command for each line in the range. When no
8886 * range given, execute it just once, without positioning the cursor
8887 * first.
8888 */
8889 do
8890 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00008891 if (eap->addr_count != 0)
8892 {
8893 curwin->w_cursor.lnum = eap->line1++;
8894 curwin->w_cursor.col = 0;
Bram Moolenaard5d37532017-03-27 23:02:07 +02008895 check_cursor_moved(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008896 }
8897
Bram Moolenaar13505972019-01-24 15:04:48 +01008898 exec_normal_cmd(arg != NULL
8899 ? arg
8900 : eap->arg, eap->forceit ? REMAP_NONE : REMAP_YES, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008901 }
8902 while (eap->addr_count > 0 && eap->line1 <= eap->line2 && !got_int);
8903 }
8904
8905 /* Might not return to the main loop when in an event handler. */
8906 update_topline_cursor();
8907
Bram Moolenaara21a6a92017-09-23 16:33:50 +02008908 restore_current_state(&save_state);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008909 --ex_normal_busy;
Bram Moolenaareda73602014-11-05 09:53:23 +01008910#ifdef FEAT_MOUSE
8911 setmouse();
8912#endif
8913#ifdef CURSOR_SHAPE
8914 ui_cursor_shape(); /* may show different cursor shape */
8915#endif
8916
Bram Moolenaar071d4272004-06-13 20:20:40 +00008917 vim_free(arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008918}
8919
8920/*
Bram Moolenaar64969662005-12-14 21:59:55 +00008921 * ":startinsert", ":startreplace" and ":startgreplace"
Bram Moolenaar071d4272004-06-13 20:20:40 +00008922 */
8923 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008924ex_startinsert(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008925{
Bram Moolenaarfd371682005-01-14 21:42:54 +00008926 if (eap->forceit)
8927 {
Bram Moolenaar09ca9322017-09-26 17:40:45 +02008928 /* cursor line can be zero on startup */
8929 if (!curwin->w_cursor.lnum)
8930 curwin->w_cursor.lnum = 1;
Bram Moolenaarfd371682005-01-14 21:42:54 +00008931 coladvance((colnr_T)MAXCOL);
8932 curwin->w_curswant = MAXCOL;
8933 curwin->w_set_curswant = FALSE;
8934 }
8935
Bram Moolenaara40c5002005-01-09 21:16:21 +00008936 /* Ignore the command when already in Insert mode. Inserting an
8937 * expression register that invokes a function can do this. */
8938 if (State & INSERT)
8939 return;
8940
Bram Moolenaar64969662005-12-14 21:59:55 +00008941 if (eap->cmdidx == CMD_startinsert)
8942 restart_edit = 'a';
8943 else if (eap->cmdidx == CMD_startreplace)
8944 restart_edit = 'R';
Bram Moolenaar071d4272004-06-13 20:20:40 +00008945 else
Bram Moolenaar64969662005-12-14 21:59:55 +00008946 restart_edit = 'V';
8947
8948 if (!eap->forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008949 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00008950 if (eap->cmdidx == CMD_startinsert)
8951 restart_edit = 'i';
Bram Moolenaar071d4272004-06-13 20:20:40 +00008952 curwin->w_curswant = 0; /* avoid MAXCOL */
8953 }
8954}
8955
8956/*
8957 * ":stopinsert"
8958 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008959 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008960ex_stopinsert(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008961{
8962 restart_edit = 0;
8963 stop_insert_mode = TRUE;
Bram Moolenaarfd773e92016-04-02 19:39:16 +02008964 clearmode();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008965}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008966
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008967/*
8968 * Execute normal mode command "cmd".
8969 * "remap" can be REMAP_NONE or REMAP_YES.
8970 */
8971 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01008972exec_normal_cmd(char_u *cmd, int remap, int silent)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008973{
Bram Moolenaar25281632016-01-21 23:32:32 +01008974 /* Stuff the argument into the typeahead buffer. */
8975 ins_typebuf(cmd, remap, 0, TRUE, silent);
Bram Moolenaar586c70c2018-10-02 16:23:58 +02008976 exec_normal(FALSE, FALSE, FALSE);
Bram Moolenaar25281632016-01-21 23:32:32 +01008977}
Bram Moolenaar25281632016-01-21 23:32:32 +01008978
Bram Moolenaar25281632016-01-21 23:32:32 +01008979/*
8980 * Execute normal_cmd() until there is no typeahead left.
Bram Moolenaar586c70c2018-10-02 16:23:58 +02008981 * When "use_vpeekc" is TRUE use vpeekc() to check for available chars.
Bram Moolenaar25281632016-01-21 23:32:32 +01008982 */
8983 void
Bram Moolenaar586c70c2018-10-02 16:23:58 +02008984exec_normal(int was_typed, int use_vpeekc, int may_use_terminal_loop UNUSED)
Bram Moolenaar25281632016-01-21 23:32:32 +01008985{
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008986 oparg_T oa;
Bram Moolenaar905dd902019-04-07 14:21:47 +02008987 int c;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008988
Bram Moolenaar905dd902019-04-07 14:21:47 +02008989 // When calling vpeekc() from feedkeys() it will return Ctrl_C when there
8990 // is nothing to get, so also check for Ctrl_C.
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008991 clear_oparg(&oa);
8992 finish_op = FALSE;
Bram Moolenaar586c70c2018-10-02 16:23:58 +02008993 while ((!stuff_empty()
8994 || ((was_typed || !typebuf_typed()) && typebuf.tb_len > 0)
Bram Moolenaar905dd902019-04-07 14:21:47 +02008995 || (use_vpeekc && (c = vpeekc()) != NUL && c != Ctrl_C))
Bram Moolenaar586c70c2018-10-02 16:23:58 +02008996 && !got_int)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00008997 {
8998 update_topline_cursor();
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02008999#ifdef FEAT_TERMINAL
Bram Moolenaar655a82a2018-05-08 22:01:07 +02009000 if (may_use_terminal_loop && term_use_loop()
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02009001 && oa.op_type == OP_NOP && oa.regname == NUL
9002 && !VIsual_active)
9003 {
9004 /* If terminal_loop() returns OK we got a key that is handled
9005 * in Normal model. With FAIL we first need to position the
9006 * cursor and the screen needs to be redrawn. */
9007 if (terminal_loop(TRUE) == OK)
9008 normal_cmd(&oa, TRUE);
9009 }
9010 else
9011#endif
9012 /* execute a Normal mode cmd */
9013 normal_cmd(&oa, TRUE);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009014 }
9015}
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009016
Bram Moolenaar071d4272004-06-13 20:20:40 +00009017#ifdef FEAT_FIND_ID
9018 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009019ex_checkpath(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009020{
9021 find_pattern_in_path(NULL, 0, 0, FALSE, FALSE, CHECK_PATH, 1L,
9022 eap->forceit ? ACTION_SHOW_ALL : ACTION_SHOW,
9023 (linenr_T)1, (linenr_T)MAXLNUM);
9024}
9025
Bram Moolenaar4033c552017-09-16 20:54:51 +02009026#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009027/*
9028 * ":psearch"
9029 */
9030 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009031ex_psearch(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009032{
9033 g_do_tagpreview = p_pvh;
9034 ex_findpat(eap);
9035 g_do_tagpreview = 0;
9036}
9037#endif
9038
9039 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009040ex_findpat(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009041{
9042 int whole = TRUE;
9043 long n;
9044 char_u *p;
9045 int action;
9046
9047 switch (cmdnames[eap->cmdidx].cmd_name[2])
9048 {
9049 case 'e': /* ":psearch", ":isearch" and ":dsearch" */
9050 if (cmdnames[eap->cmdidx].cmd_name[0] == 'p')
9051 action = ACTION_GOTO;
9052 else
9053 action = ACTION_SHOW;
9054 break;
9055 case 'i': /* ":ilist" and ":dlist" */
9056 action = ACTION_SHOW_ALL;
9057 break;
9058 case 'u': /* ":ijump" and ":djump" */
9059 action = ACTION_GOTO;
9060 break;
9061 default: /* ":isplit" and ":dsplit" */
9062 action = ACTION_SPLIT;
9063 break;
9064 }
9065
9066 n = 1;
9067 if (vim_isdigit(*eap->arg)) /* get count */
9068 {
9069 n = getdigits(&eap->arg);
9070 eap->arg = skipwhite(eap->arg);
9071 }
9072 if (*eap->arg == '/') /* Match regexp, not just whole words */
9073 {
9074 whole = FALSE;
9075 ++eap->arg;
9076 p = skip_regexp(eap->arg, '/', p_magic, NULL);
9077 if (*p)
9078 {
9079 *p++ = NUL;
9080 p = skipwhite(p);
9081
9082 /* Check for trailing illegal characters */
9083 if (!ends_excmd(*p))
9084 eap->errmsg = e_trailing;
9085 else
9086 eap->nextcmd = check_nextcmd(p);
9087 }
9088 }
9089 if (!eap->skip)
9090 find_pattern_in_path(eap->arg, 0, (int)STRLEN(eap->arg),
9091 whole, !eap->forceit,
9092 *eap->cmd == 'd' ? FIND_DEFINE : FIND_ANY,
9093 n, action, eap->line1, eap->line2);
9094}
9095#endif
9096
Bram Moolenaar071d4272004-06-13 20:20:40 +00009097
Bram Moolenaar4033c552017-09-16 20:54:51 +02009098#ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00009099/*
9100 * ":ptag", ":ptselect", ":ptjump", ":ptnext", etc.
9101 */
9102 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009103ex_ptag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009104{
Bram Moolenaara3e7b1f2010-11-10 19:00:01 +01009105 g_do_tagpreview = p_pvh; /* will be reset to 0 in ex_tag_cmd() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009106 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
9107}
9108
9109/*
9110 * ":pedit"
9111 */
9112 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009113ex_pedit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009114{
9115 win_T *curwin_save = curwin;
9116
9117 g_do_tagpreview = p_pvh;
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00009118 prepare_tagpreview(TRUE);
Bram Moolenaar67883b42017-07-27 22:57:00 +02009119 keep_help_flag = bt_help(curwin_save->w_buffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009120 do_exedit(eap, NULL);
9121 keep_help_flag = FALSE;
9122 if (curwin != curwin_save && win_valid(curwin_save))
9123 {
9124 /* Return cursor to where we were */
9125 validate_cursor();
9126 redraw_later(VALID);
9127 win_enter(curwin_save, TRUE);
9128 }
9129 g_do_tagpreview = 0;
9130}
Bram Moolenaar4033c552017-09-16 20:54:51 +02009131#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009132
9133/*
9134 * ":stag", ":stselect" and ":stjump".
9135 */
9136 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009137ex_stag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009138{
9139 postponed_split = -1;
9140 postponed_split_flags = cmdmod.split;
Bram Moolenaard326ce82007-03-11 14:48:29 +00009141 postponed_split_tab = cmdmod.tab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009142 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name + 1);
9143 postponed_split_flags = 0;
Bram Moolenaard326ce82007-03-11 14:48:29 +00009144 postponed_split_tab = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009145}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009146
9147/*
9148 * ":tag", ":tselect", ":tjump", ":tnext", etc.
9149 */
9150 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009151ex_tag(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009152{
9153 ex_tag_cmd(eap, cmdnames[eap->cmdidx].cmd_name);
9154}
9155
9156 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009157ex_tag_cmd(exarg_T *eap, char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009158{
9159 int cmd;
9160
9161 switch (name[1])
9162 {
9163 case 'j': cmd = DT_JUMP; /* ":tjump" */
9164 break;
9165 case 's': cmd = DT_SELECT; /* ":tselect" */
9166 break;
9167 case 'p': cmd = DT_PREV; /* ":tprevious" */
9168 break;
9169 case 'N': cmd = DT_PREV; /* ":tNext" */
9170 break;
9171 case 'n': cmd = DT_NEXT; /* ":tnext" */
9172 break;
9173 case 'o': cmd = DT_POP; /* ":pop" */
9174 break;
9175 case 'f': /* ":tfirst" */
9176 case 'r': cmd = DT_FIRST; /* ":trewind" */
9177 break;
9178 case 'l': cmd = DT_LAST; /* ":tlast" */
9179 break;
9180 default: /* ":tag" */
9181#ifdef FEAT_CSCOPE
Bram Moolenaar7c94c262008-06-20 09:11:34 +00009182 if (p_cst && *eap->arg != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009183 {
Bram Moolenaard4db7712016-11-12 19:16:46 +01009184 ex_cstag(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009185 return;
9186 }
9187#endif
9188 cmd = DT_TAG;
9189 break;
9190 }
9191
Bram Moolenaarb8a7b562006-02-01 21:47:16 +00009192 if (name[0] == 'l')
9193 {
9194#ifndef FEAT_QUICKFIX
9195 ex_ni(eap);
9196 return;
9197#else
9198 cmd = DT_LTAG;
9199#endif
9200 }
9201
Bram Moolenaar071d4272004-06-13 20:20:40 +00009202 do_tag(eap->arg, cmd, eap->addr_count > 0 ? (int)eap->line2 : 1,
9203 eap->forceit, TRUE);
9204}
9205
9206/*
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009207 * Check "str" for starting with a special cmdline variable.
9208 * If found return one of the SPEC_ values and set "*usedlen" to the length of
9209 * the variable. Otherwise return -1 and "*usedlen" is unchanged.
9210 */
9211 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009212find_cmdline_var(char_u *src, int *usedlen)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009213{
9214 int len;
9215 int i;
Bram Moolenaar8f0b2d42009-05-16 14:41:10 +00009216 static char *(spec_str[]) = {
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009217 "%",
9218#define SPEC_PERC 0
9219 "#",
Bram Moolenaar65f08472017-09-10 18:16:20 +02009220#define SPEC_HASH (SPEC_PERC + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009221 "<cword>", /* cursor word */
Bram Moolenaar65f08472017-09-10 18:16:20 +02009222#define SPEC_CWORD (SPEC_HASH + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009223 "<cWORD>", /* cursor WORD */
Bram Moolenaar65f08472017-09-10 18:16:20 +02009224#define SPEC_CCWORD (SPEC_CWORD + 1)
9225 "<cexpr>", /* expr under cursor */
9226#define SPEC_CEXPR (SPEC_CCWORD + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009227 "<cfile>", /* cursor path name */
Bram Moolenaar65f08472017-09-10 18:16:20 +02009228#define SPEC_CFILE (SPEC_CEXPR + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009229 "<sfile>", /* ":so" file name */
Bram Moolenaar65f08472017-09-10 18:16:20 +02009230#define SPEC_SFILE (SPEC_CFILE + 1)
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009231 "<slnum>", /* ":so" file line number */
Bram Moolenaar65f08472017-09-10 18:16:20 +02009232#define SPEC_SLNUM (SPEC_SFILE + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009233 "<afile>", /* autocommand file name */
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009234#define SPEC_AFILE (SPEC_SLNUM + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009235 "<abuf>", /* autocommand buffer number */
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009236#define SPEC_ABUF (SPEC_AFILE + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009237 "<amatch>", /* autocommand match name */
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01009238#define SPEC_AMATCH (SPEC_ABUF + 1)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009239 "<sflnum>", /* script file line number */
9240#define SPEC_SFLNUM (SPEC_AMATCH + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009241#ifdef FEAT_CLIENTSERVER
9242 "<client>"
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009243# define SPEC_CLIENT (SPEC_SFLNUM + 1)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009244#endif
9245 };
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009246
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00009247 for (i = 0; i < (int)(sizeof(spec_str) / sizeof(char *)); ++i)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009248 {
9249 len = (int)STRLEN(spec_str[i]);
9250 if (STRNCMP(src, spec_str[i], len) == 0)
9251 {
9252 *usedlen = len;
9253 return i;
9254 }
9255 }
9256 return -1;
9257}
9258
9259/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009260 * Evaluate cmdline variables.
9261 *
9262 * change '%' to curbuf->b_ffname
9263 * '#' to curwin->w_altfile
9264 * '<cword>' to word under the cursor
9265 * '<cWORD>' to WORD under the cursor
9266 * '<cfile>' to path name under the cursor
9267 * '<sfile>' to sourced file name
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009268 * '<slnum>' to sourced file line number
Bram Moolenaar071d4272004-06-13 20:20:40 +00009269 * '<afile>' to file name for autocommand
9270 * '<abuf>' to buffer number for autocommand
9271 * '<amatch>' to matching name for autocommand
9272 *
9273 * When an error is detected, "errormsg" is set to a non-NULL pointer (may be
9274 * "" for error without a message) and NULL is returned.
9275 * Returns an allocated string if a valid match was found.
9276 * Returns NULL if no match was found. "usedlen" then still contains the
9277 * number of characters to skip.
9278 */
9279 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009280eval_vars(
9281 char_u *src, /* pointer into commandline */
9282 char_u *srcstart, /* beginning of valid memory for src */
9283 int *usedlen, /* characters after src that are used */
9284 linenr_T *lnump, /* line number for :e command, or NULL */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009285 char **errormsg, /* pointer to error message */
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009286 int *escaped) /* return value has escaped white space (can
Bram Moolenaar63b92542007-03-27 14:57:09 +00009287 * be NULL) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009288{
9289 int i;
9290 char_u *s;
9291 char_u *result;
9292 char_u *resultbuf = NULL;
9293 int resultlen;
9294 buf_T *buf;
9295 int valid = VALID_HEAD + VALID_PATH; /* assume valid result */
9296 int spec_idx;
9297#ifdef FEAT_MODIFY_FNAME
Bram Moolenaarfd249462018-07-28 16:14:30 +02009298 int tilde_file = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009299 int skip_mod = FALSE;
9300#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009301 char_u strbuf[30];
Bram Moolenaar071d4272004-06-13 20:20:40 +00009302
9303 *errormsg = NULL;
Bram Moolenaar63b92542007-03-27 14:57:09 +00009304 if (escaped != NULL)
9305 *escaped = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009306
9307 /*
9308 * Check if there is something to do.
9309 */
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009310 spec_idx = find_cmdline_var(src, usedlen);
9311 if (spec_idx < 0) /* no match */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009312 {
9313 *usedlen = 1;
9314 return NULL;
9315 }
9316
9317 /*
9318 * Skip when preceded with a backslash "\%" and "\#".
9319 * Note: In "\\%" the % is also not recognized!
9320 */
9321 if (src > srcstart && src[-1] == '\\')
9322 {
9323 *usedlen = 0;
Bram Moolenaara7241f52008-06-24 20:39:31 +00009324 STRMOVE(src - 1, src); /* remove backslash */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009325 return NULL;
9326 }
9327
9328 /*
9329 * word or WORD under cursor
9330 */
Bram Moolenaar65f08472017-09-10 18:16:20 +02009331 if (spec_idx == SPEC_CWORD || spec_idx == SPEC_CCWORD
9332 || spec_idx == SPEC_CEXPR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009333 {
Bram Moolenaar65f08472017-09-10 18:16:20 +02009334 resultlen = find_ident_under_cursor(&result,
9335 spec_idx == SPEC_CWORD ? (FIND_IDENT | FIND_STRING)
9336 : spec_idx == SPEC_CEXPR ? (FIND_IDENT | FIND_STRING | FIND_EVAL)
9337 : FIND_STRING);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009338 if (resultlen == 0)
9339 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009340 *errormsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +00009341 return NULL;
9342 }
9343 }
9344
9345 /*
9346 * '#': Alternate file name
9347 * '%': Current file name
9348 * File name under the cursor
9349 * File name for autocommand
9350 * and following modifiers
9351 */
9352 else
9353 {
9354 switch (spec_idx)
9355 {
9356 case SPEC_PERC: /* '%': current file */
9357 if (curbuf->b_fname == NULL)
9358 {
9359 result = (char_u *)"";
9360 valid = 0; /* Must have ":p:h" to be valid */
9361 }
9362 else
Bram Moolenaar00136dc2018-07-25 21:19:13 +02009363 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009364 result = curbuf->b_fname;
Bram Moolenaarfd249462018-07-28 16:14:30 +02009365#ifdef FEAT_MODIFY_FNAME
Bram Moolenaar00136dc2018-07-25 21:19:13 +02009366 tilde_file = STRCMP(result, "~") == 0;
Bram Moolenaarfd249462018-07-28 16:14:30 +02009367#endif
Bram Moolenaar00136dc2018-07-25 21:19:13 +02009368 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009369 break;
9370
9371 case SPEC_HASH: /* '#' or "#99": alternate file */
9372 if (src[1] == '#') /* "##": the argument list */
9373 {
9374 result = arg_all();
9375 resultbuf = result;
9376 *usedlen = 2;
Bram Moolenaar63b92542007-03-27 14:57:09 +00009377 if (escaped != NULL)
9378 *escaped = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009379#ifdef FEAT_MODIFY_FNAME
9380 skip_mod = TRUE;
9381#endif
9382 break;
9383 }
9384 s = src + 1;
Bram Moolenaard812df62008-11-09 12:46:09 +00009385 if (*s == '<') /* "#<99" uses v:oldfiles */
9386 ++s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009387 i = (int)getdigits(&s);
Bram Moolenaarc312b8b2017-10-28 17:53:04 +02009388 if (s == src + 2 && src[1] == '-')
9389 /* just a minus sign, don't skip over it */
9390 s--;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009391 *usedlen = (int)(s - src); /* length of what we expand */
9392
Bram Moolenaarc312b8b2017-10-28 17:53:04 +02009393 if (src[1] == '<' && i != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009394 {
Bram Moolenaard812df62008-11-09 12:46:09 +00009395 if (*usedlen < 2)
9396 {
9397 /* Should we give an error message for #<text? */
9398 *usedlen = 1;
9399 return NULL;
9400 }
9401#ifdef FEAT_EVAL
9402 result = list_find_str(get_vim_var_list(VV_OLDFILES),
9403 (long)i);
9404 if (result == NULL)
9405 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009406 *errormsg = "";
Bram Moolenaard812df62008-11-09 12:46:09 +00009407 return NULL;
9408 }
9409#else
Bram Moolenaar8e481e82019-01-15 20:07:48 +01009410 *errormsg = _("E809: #< is not available without the +eval feature");
Bram Moolenaar071d4272004-06-13 20:20:40 +00009411 return NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +00009412#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009413 }
9414 else
Bram Moolenaard812df62008-11-09 12:46:09 +00009415 {
Bram Moolenaarc312b8b2017-10-28 17:53:04 +02009416 if (i == 0 && src[1] == '<' && *usedlen > 1)
9417 *usedlen = 1;
Bram Moolenaard812df62008-11-09 12:46:09 +00009418 buf = buflist_findnr(i);
9419 if (buf == NULL)
9420 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009421 *errormsg = _("E194: No alternate file name to substitute for '#'");
Bram Moolenaard812df62008-11-09 12:46:09 +00009422 return NULL;
9423 }
9424 if (lnump != NULL)
9425 *lnump = ECMD_LAST;
9426 if (buf->b_fname == NULL)
9427 {
9428 result = (char_u *)"";
9429 valid = 0; /* Must have ":p:h" to be valid */
9430 }
9431 else
Bram Moolenaar00136dc2018-07-25 21:19:13 +02009432 {
Bram Moolenaard812df62008-11-09 12:46:09 +00009433 result = buf->b_fname;
Bram Moolenaarfd249462018-07-28 16:14:30 +02009434#ifdef FEAT_MODIFY_FNAME
Bram Moolenaar00136dc2018-07-25 21:19:13 +02009435 tilde_file = STRCMP(result, "~") == 0;
Bram Moolenaarfd249462018-07-28 16:14:30 +02009436#endif
Bram Moolenaar00136dc2018-07-25 21:19:13 +02009437 }
Bram Moolenaard812df62008-11-09 12:46:09 +00009438 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009439 break;
9440
9441#ifdef FEAT_SEARCHPATH
9442 case SPEC_CFILE: /* file name under cursor */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00009443 result = file_name_at_cursor(FNAME_MESS|FNAME_HYP, 1L, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009444 if (result == NULL)
9445 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009446 *errormsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +00009447 return NULL;
9448 }
9449 resultbuf = result; /* remember allocated string */
9450 break;
9451#endif
9452
Bram Moolenaar071d4272004-06-13 20:20:40 +00009453 case SPEC_AFILE: /* file name for autocommand */
9454 result = autocmd_fname;
Bram Moolenaarf6dad432008-09-18 19:29:58 +00009455 if (result != NULL && !autocmd_fname_full)
9456 {
9457 /* Still need to turn the fname into a full path. It is
9458 * postponed to avoid a delay when <afile> is not used. */
9459 autocmd_fname_full = TRUE;
9460 result = FullName_save(autocmd_fname, FALSE);
9461 vim_free(autocmd_fname);
9462 autocmd_fname = result;
9463 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009464 if (result == NULL)
9465 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009466 *errormsg = _("E495: no autocommand file name to substitute for \"<afile>\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00009467 return NULL;
9468 }
Bram Moolenaara0174af2008-01-02 20:08:25 +00009469 result = shorten_fname1(result);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009470 break;
9471
9472 case SPEC_ABUF: /* buffer number for autocommand */
9473 if (autocmd_bufnr <= 0)
9474 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009475 *errormsg = _("E496: no autocommand buffer number to substitute for \"<abuf>\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00009476 return NULL;
9477 }
9478 sprintf((char *)strbuf, "%d", autocmd_bufnr);
9479 result = strbuf;
9480 break;
9481
9482 case SPEC_AMATCH: /* match name for autocommand */
9483 result = autocmd_match;
9484 if (result == NULL)
9485 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009486 *errormsg = _("E497: no autocommand match name to substitute for \"<amatch>\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00009487 return NULL;
9488 }
9489 break;
9490
Bram Moolenaar071d4272004-06-13 20:20:40 +00009491 case SPEC_SFILE: /* file name for ":so" command */
9492 result = sourcing_name;
9493 if (result == NULL)
9494 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009495 *errormsg = _("E498: no :source file name to substitute for \"<sfile>\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00009496 return NULL;
9497 }
9498 break;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009499
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009500 case SPEC_SLNUM: /* line in file for ":so" command */
9501 if (sourcing_name == NULL || sourcing_lnum == 0)
9502 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009503 *errormsg = _("E842: no line number to use for \"<slnum>\"");
Bram Moolenaar4dbbff52010-11-24 15:50:59 +01009504 return NULL;
9505 }
9506 sprintf((char *)strbuf, "%ld", (long)sourcing_lnum);
9507 result = strbuf;
9508 break;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009509
9510#ifdef FEAT_EVAL
9511 case SPEC_SFLNUM: /* line in script file */
9512 if (current_sctx.sc_lnum + sourcing_lnum == 0)
9513 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009514 *errormsg = _("E961: no line number to use for \"<sflnum>\"");
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009515 return NULL;
9516 }
9517 sprintf((char *)strbuf, "%ld",
9518 (long)(current_sctx.sc_lnum + sourcing_lnum));
9519 result = strbuf;
9520 break;
9521#endif
9522
9523#ifdef FEAT_CLIENTSERVER
Bram Moolenaar071d4272004-06-13 20:20:40 +00009524 case SPEC_CLIENT: /* Source of last submitted input */
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009525 sprintf((char *)strbuf, PRINTF_HEX_LONG_U,
9526 (long_u)clientWindow);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009527 result = strbuf;
9528 break;
9529#endif
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009530
Bram Moolenaar9e0f6ec2017-05-16 09:36:54 +02009531 default:
9532 result = (char_u *)""; /* avoid gcc warning */
9533 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009534 }
9535
9536 resultlen = (int)STRLEN(result); /* length of new string */
9537 if (src[*usedlen] == '<') /* remove the file name extension */
9538 {
9539 ++*usedlen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009540 if ((s = vim_strrchr(result, '.')) != NULL && s >= gettail(result))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009541 resultlen = (int)(s - result);
9542 }
9543#ifdef FEAT_MODIFY_FNAME
9544 else if (!skip_mod)
9545 {
Bram Moolenaar00136dc2018-07-25 21:19:13 +02009546 valid |= modify_fname(src, tilde_file, usedlen, &result, &resultbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009547 &resultlen);
9548 if (result == NULL)
9549 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009550 *errormsg = "";
Bram Moolenaar071d4272004-06-13 20:20:40 +00009551 return NULL;
9552 }
9553 }
9554#endif
9555 }
9556
9557 if (resultlen == 0 || valid != VALID_HEAD + VALID_PATH)
9558 {
9559 if (valid != VALID_HEAD + VALID_PATH)
9560 /* xgettext:no-c-format */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009561 *errormsg = _("E499: Empty file name for '%' or '#', only works with \":p:h\"");
Bram Moolenaar071d4272004-06-13 20:20:40 +00009562 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009563 *errormsg = _("E500: Evaluates to an empty string");
Bram Moolenaar071d4272004-06-13 20:20:40 +00009564 result = NULL;
9565 }
9566 else
9567 result = vim_strnsave(result, resultlen);
9568 vim_free(resultbuf);
9569 return result;
9570}
9571
9572/*
9573 * Concatenate all files in the argument list, separated by spaces, and return
9574 * it in one allocated string.
9575 * Spaces and backslashes in the file names are escaped with a backslash.
9576 * Returns NULL when out of memory.
9577 */
9578 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009579arg_all(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009580{
9581 int len;
9582 int idx;
9583 char_u *retval = NULL;
9584 char_u *p;
9585
9586 /*
9587 * Do this loop two times:
9588 * first time: compute the total length
9589 * second time: concatenate the names
9590 */
9591 for (;;)
9592 {
9593 len = 0;
9594 for (idx = 0; idx < ARGCOUNT; ++idx)
9595 {
9596 p = alist_name(&ARGLIST[idx]);
9597 if (p != NULL)
9598 {
9599 if (len > 0)
9600 {
9601 /* insert a space in between names */
9602 if (retval != NULL)
9603 retval[len] = ' ';
9604 ++len;
9605 }
9606 for ( ; *p != NUL; ++p)
9607 {
Bram Moolenaar6e8d3b02015-06-09 21:33:31 +02009608 if (*p == ' '
9609#ifndef BACKSLASH_IN_FILENAME
9610 || *p == '\\'
9611#endif
Bram Moolenaar2c8c6812018-07-28 17:07:52 +02009612 || *p == '`')
Bram Moolenaar071d4272004-06-13 20:20:40 +00009613 {
9614 /* insert a backslash */
9615 if (retval != NULL)
9616 retval[len] = '\\';
9617 ++len;
9618 }
9619 if (retval != NULL)
9620 retval[len] = *p;
9621 ++len;
9622 }
9623 }
9624 }
9625
9626 /* second time: break here */
9627 if (retval != NULL)
9628 {
9629 retval[len] = NUL;
9630 break;
9631 }
9632
9633 /* allocate memory */
Bram Moolenaar964b3742019-05-24 18:54:09 +02009634 retval = alloc(len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009635 if (retval == NULL)
9636 break;
9637 }
9638
9639 return retval;
9640}
9641
Bram Moolenaar071d4272004-06-13 20:20:40 +00009642/*
9643 * Expand the <sfile> string in "arg".
9644 *
9645 * Returns an allocated string, or NULL for any error.
9646 */
9647 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009648expand_sfile(char_u *arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009649{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009650 char *errormsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009651 int len;
9652 char_u *result;
9653 char_u *newres;
9654 char_u *repl;
9655 int srclen;
9656 char_u *p;
9657
9658 result = vim_strsave(arg);
9659 if (result == NULL)
9660 return NULL;
9661
9662 for (p = result; *p; )
9663 {
9664 if (STRNCMP(p, "<sfile>", 7) != 0)
9665 ++p;
9666 else
9667 {
9668 /* replace "<sfile>" with the sourced file name, and do ":" stuff */
Bram Moolenaar63b92542007-03-27 14:57:09 +00009669 repl = eval_vars(p, result, &srclen, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009670 if (errormsg != NULL)
9671 {
9672 if (*errormsg)
9673 emsg(errormsg);
9674 vim_free(result);
9675 return NULL;
9676 }
9677 if (repl == NULL) /* no match (cannot happen) */
9678 {
9679 p += srclen;
9680 continue;
9681 }
9682 len = (int)STRLEN(result) - srclen + (int)STRLEN(repl) + 1;
9683 newres = alloc(len);
9684 if (newres == NULL)
9685 {
9686 vim_free(repl);
9687 vim_free(result);
9688 return NULL;
9689 }
9690 mch_memmove(newres, result, (size_t)(p - result));
9691 STRCPY(newres + (p - result), repl);
9692 len = (int)STRLEN(newres);
9693 STRCAT(newres, p + srclen);
9694 vim_free(repl);
9695 vim_free(result);
9696 result = newres;
9697 p = newres + len; /* continue after the match */
9698 }
9699 }
9700
9701 return result;
9702}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009703
9704#ifdef FEAT_SESSION
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01009705static int ses_winsizes(FILE *fd, int restore_size,
9706 win_T *tab_firstwin);
9707static int ses_win_rec(FILE *fd, frame_T *fr);
9708static frame_T *ses_skipframe(frame_T *fr);
9709static int ses_do_frame(frame_T *fr);
9710static int ses_do_win(win_T *wp);
9711static int ses_arglist(FILE *fd, char *cmd, garray_T *gap, int fullname, unsigned *flagp);
9712static int ses_put_fname(FILE *fd, char_u *name, unsigned *flagp);
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +02009713static int ses_fname(FILE *fd, buf_T *buf, unsigned *flagp, int add_eol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009714
9715/*
9716 * Write openfile commands for the current buffers to an .exrc file.
9717 * Return FAIL on error, OK otherwise.
9718 */
9719 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01009720makeopens(
9721 FILE *fd,
9722 char_u *dirnow) /* Current directory name */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009723{
9724 buf_T *buf;
9725 int only_save_windows = TRUE;
9726 int nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009727 int restore_size = TRUE;
9728 win_T *wp;
9729 char_u *sname;
9730 win_T *edited_win = NULL;
Bram Moolenaar18144c82006-04-12 21:52:12 +00009731 int tabnr;
Bram Moolenaar04ad7fe2014-05-07 21:14:47 +02009732 int restore_stal = FALSE;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +00009733 win_T *tab_firstwin;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +00009734 frame_T *tab_topframe;
Bram Moolenaarf13be0d2008-01-02 14:13:10 +00009735 int cur_arg_idx = 0;
Bram Moolenaar383c6f52008-01-04 15:01:07 +00009736 int next_arg_idx = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009737
9738 if (ssop_flags & SSOP_BUFFERS)
9739 only_save_windows = FALSE; /* Save ALL buffers */
9740
9741 /*
9742 * Begin by setting the this_session variable, and then other
9743 * sessionable variables.
9744 */
9745#ifdef FEAT_EVAL
9746 if (put_line(fd, "let v:this_session=expand(\"<sfile>:p\")") == FAIL)
9747 return FAIL;
9748 if (ssop_flags & SSOP_GLOBALS)
9749 if (store_session_globals(fd) == FAIL)
9750 return FAIL;
9751#endif
9752
9753 /*
Bram Moolenaar26d4b892018-07-04 22:26:28 +02009754 * Close all windows and tabs but one.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009755 */
9756 if (put_line(fd, "silent only") == FAIL)
9757 return FAIL;
Bram Moolenaar26d4b892018-07-04 22:26:28 +02009758 if ((ssop_flags & SSOP_TABPAGES)
9759 && put_line(fd, "silent tabonly") == FAIL)
9760 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009761
9762 /*
9763 * Now a :cd command to the session directory or the current directory
9764 */
9765 if (ssop_flags & SSOP_SESDIR)
9766 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009767 if (put_line(fd, "exe \"cd \" . escape(expand(\"<sfile>:p:h\"), ' ')")
9768 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009769 return FAIL;
9770 }
9771 else if (ssop_flags & SSOP_CURDIR)
9772 {
9773 sname = home_replace_save(NULL, globaldir != NULL ? globaldir : dirnow);
9774 if (sname == NULL
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009775 || fputs("cd ", fd) < 0
9776 || ses_put_fname(fd, sname, &ssop_flags) == FAIL
9777 || put_eol(fd) == FAIL)
9778 {
9779 vim_free(sname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009780 return FAIL;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009781 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009782 vim_free(sname);
9783 }
9784
9785 /*
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009786 * If there is an empty, unnamed buffer we will wipe it out later.
9787 * Remember the buffer number.
9788 */
9789 if (put_line(fd, "if expand('%') == '' && !&modified && line('$') <= 1 && getline(1) == ''") == FAIL)
9790 return FAIL;
9791 if (put_line(fd, " let s:wipebuf = bufnr('%')") == FAIL)
9792 return FAIL;
9793 if (put_line(fd, "endif") == FAIL)
9794 return FAIL;
9795
9796 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009797 * Now save the current files, current buffer first.
9798 */
9799 if (put_line(fd, "set shortmess=aoO") == FAIL)
9800 return FAIL;
9801
Bram Moolenaar071d4272004-06-13 20:20:40 +00009802 /* the global argument list */
Bram Moolenaarf0bdd2f2014-03-12 21:28:26 +01009803 if (ses_arglist(fd, "argglobal", &global_alist.al_ga,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009804 !(ssop_flags & SSOP_CURDIR), &ssop_flags) == FAIL)
9805 return FAIL;
9806
9807 if (ssop_flags & SSOP_RESIZE)
9808 {
9809 /* Note: after the restore we still check it worked!*/
9810 if (fprintf(fd, "set lines=%ld columns=%ld" , Rows, Columns) < 0
9811 || put_eol(fd) == FAIL)
9812 return FAIL;
9813 }
9814
9815#ifdef FEAT_GUI
9816 if (gui.in_use && (ssop_flags & SSOP_WINPOS))
9817 {
9818 int x, y;
9819
9820 if (gui_mch_get_winpos(&x, &y) == OK)
9821 {
9822 /* Note: after the restore we still check it worked!*/
9823 if (fprintf(fd, "winpos %d %d", x, y) < 0 || put_eol(fd) == FAIL)
9824 return FAIL;
9825 }
9826 }
9827#endif
9828
9829 /*
Bram Moolenaar04ad7fe2014-05-07 21:14:47 +02009830 * When there are two or more tabpages and 'showtabline' is 1 the tabline
9831 * will be displayed when creating the next tab. That resizes the windows
9832 * in the first tab, which may cause problems. Set 'showtabline' to 2
9833 * temporarily to avoid that.
9834 */
9835 if (p_stal == 1 && first_tabpage->tp_next != NULL)
9836 {
9837 if (put_line(fd, "set stal=2") == FAIL)
9838 return FAIL;
9839 restore_stal = TRUE;
9840 }
9841
9842 /*
Bram Moolenaar18144c82006-04-12 21:52:12 +00009843 * May repeat putting Windows for each tab, when "tabpages" is in
9844 * 'sessionoptions'.
Bram Moolenaarcba2ae52006-10-24 10:59:57 +00009845 * Don't use goto_tabpage(), it may change directory and trigger
9846 * autocommands.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009847 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +00009848 tab_firstwin = firstwin; /* first window in tab page "tabnr" */
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +00009849 tab_topframe = topframe;
Bram Moolenaar26d4b892018-07-04 22:26:28 +02009850 if ((ssop_flags & SSOP_TABPAGES))
9851 {
Bram Moolenaar57a6bf02019-01-22 21:27:13 +01009852 tabpage_T *tp;
Bram Moolenaar26d4b892018-07-04 22:26:28 +02009853
Bram Moolenaar57a6bf02019-01-22 21:27:13 +01009854 // Similar to ses_win_rec() below, populate the tab pages first so
9855 // later local options won't be copied to the new tabs.
9856 FOR_ALL_TABPAGES(tp)
9857 if (tp->tp_next != NULL && put_line(fd, "tabnew") == FAIL)
Bram Moolenaar26d4b892018-07-04 22:26:28 +02009858 return FAIL;
Bram Moolenaar57a6bf02019-01-22 21:27:13 +01009859 if (first_tabpage->tp_next != NULL && put_line(fd, "tabrewind") == FAIL)
Bram Moolenaar26d4b892018-07-04 22:26:28 +02009860 return FAIL;
9861 }
Bram Moolenaar18144c82006-04-12 21:52:12 +00009862 for (tabnr = 1; ; ++tabnr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009863 {
Bram Moolenaar00aa0692019-04-27 20:37:57 +02009864 tabpage_T *tp = NULL;
Bram Moolenaar26d4b892018-07-04 22:26:28 +02009865 int need_tabnext = FALSE;
Bram Moolenaar695baee2015-04-13 12:39:22 +02009866 int cnr = 1;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +00009867
Bram Moolenaar18144c82006-04-12 21:52:12 +00009868 if ((ssop_flags & SSOP_TABPAGES))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009869 {
Bram Moolenaar00aa0692019-04-27 20:37:57 +02009870 tp = find_tabpage(tabnr);
Bram Moolenaarcba2ae52006-10-24 10:59:57 +00009871
9872 if (tp == NULL)
9873 break; /* done all tab pages */
9874 if (tp == curtab)
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +00009875 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +00009876 tab_firstwin = firstwin;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +00009877 tab_topframe = topframe;
9878 }
Bram Moolenaarcba2ae52006-10-24 10:59:57 +00009879 else
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +00009880 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +00009881 tab_firstwin = tp->tp_firstwin;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +00009882 tab_topframe = tp->tp_topframe;
9883 }
Bram Moolenaarcba2ae52006-10-24 10:59:57 +00009884 if (tabnr > 1)
Bram Moolenaar26d4b892018-07-04 22:26:28 +02009885 need_tabnext = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009886 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009887
Bram Moolenaar18144c82006-04-12 21:52:12 +00009888 /*
9889 * Before creating the window layout, try loading one file. If this
9890 * is aborted we don't end up with a number of useless windows.
9891 * This may have side effects! (e.g., compressed or network file).
9892 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +00009893 for (wp = tab_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar18144c82006-04-12 21:52:12 +00009894 {
9895 if (ses_do_win(wp)
9896 && wp->w_buffer->b_ffname != NULL
Bram Moolenaar67883b42017-07-27 22:57:00 +02009897 && !bt_help(wp->w_buffer)
Bram Moolenaar18144c82006-04-12 21:52:12 +00009898#ifdef FEAT_QUICKFIX
9899 && !bt_nofile(wp->w_buffer)
9900#endif
9901 )
9902 {
Bram Moolenaar26d4b892018-07-04 22:26:28 +02009903 if (need_tabnext && put_line(fd, "tabnext") == FAIL)
9904 return FAIL;
9905 need_tabnext = FALSE;
9906
9907 if (fputs("edit ", fd) < 0
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +02009908 || ses_fname(fd, wp->w_buffer, &ssop_flags, TRUE)
9909 == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +00009910 return FAIL;
9911 if (!wp->w_arg_idx_invalid)
9912 edited_win = wp;
9913 break;
9914 }
9915 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009916
Bram Moolenaarcba2ae52006-10-24 10:59:57 +00009917 /* If no file got edited create an empty tab page. */
Bram Moolenaar26d4b892018-07-04 22:26:28 +02009918 if (need_tabnext && put_line(fd, "tabnext") == FAIL)
Bram Moolenaarcba2ae52006-10-24 10:59:57 +00009919 return FAIL;
9920
Bram Moolenaar18144c82006-04-12 21:52:12 +00009921 /*
9922 * Save current window layout.
9923 */
9924 if (put_line(fd, "set splitbelow splitright") == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009925 return FAIL;
Bram Moolenaar3b1b6c62006-11-28 20:40:00 +00009926 if (ses_win_rec(fd, tab_topframe) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009927 return FAIL;
Bram Moolenaar18144c82006-04-12 21:52:12 +00009928 if (!p_sb && put_line(fd, "set nosplitbelow") == FAIL)
9929 return FAIL;
9930 if (!p_spr && put_line(fd, "set nosplitright") == FAIL)
9931 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009932
Bram Moolenaar18144c82006-04-12 21:52:12 +00009933 /*
9934 * Check if window sizes can be restored (no windows omitted).
9935 * Remember the window number of the current window after restoring.
9936 */
9937 nr = 0;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +00009938 for (wp = tab_firstwin; wp != NULL; wp = W_NEXT(wp))
Bram Moolenaar18144c82006-04-12 21:52:12 +00009939 {
9940 if (ses_do_win(wp))
9941 ++nr;
9942 else
9943 restore_size = FALSE;
9944 if (curwin == wp)
9945 cnr = nr;
9946 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009947
Bram Moolenaar18144c82006-04-12 21:52:12 +00009948 /* Go to the first window. */
9949 if (put_line(fd, "wincmd t") == FAIL)
9950 return FAIL;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00009951
Bram Moolenaar18144c82006-04-12 21:52:12 +00009952 /*
9953 * If more than one window, see if sizes can be restored.
9954 * First set 'winheight' and 'winwidth' to 1 to avoid the windows being
9955 * resized when moving between windows.
9956 * Do this before restoring the view, so that the topline and the
9957 * cursor can be set. This is done again below.
Bram Moolenaar36ae89c2017-01-28 17:11:14 +01009958 * winminheight and winminwidth need to be set to avoid an error if the
9959 * user has set winheight or winwidth.
Bram Moolenaar18144c82006-04-12 21:52:12 +00009960 */
Bram Moolenaar19834012018-06-12 17:03:39 +02009961 if (put_line(fd, "set winminheight=0") == FAIL
9962 || put_line(fd, "set winheight=1") == FAIL
9963 || put_line(fd, "set winminwidth=0") == FAIL
9964 || put_line(fd, "set winwidth=1") == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +00009965 return FAIL;
Bram Moolenaarcba2ae52006-10-24 10:59:57 +00009966 if (nr > 1 && ses_winsizes(fd, restore_size, tab_firstwin) == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +00009967 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009968
Bram Moolenaar00aa0692019-04-27 20:37:57 +02009969 // Restore the tab-local working directory if specified
9970 // Do this before the windows, so that the window-local directory can
9971 // override the tab-local directory.
9972 if (tp != NULL && tp->tp_localdir != NULL && ssop_flags & SSOP_CURDIR)
9973 {
9974 if (fputs("tcd ", fd) < 0
9975 || ses_put_fname(fd, tp->tp_localdir, &ssop_flags) == FAIL
9976 || put_eol(fd) == FAIL)
9977 return FAIL;
9978 did_lcd = TRUE;
9979 }
9980
Bram Moolenaar18144c82006-04-12 21:52:12 +00009981 /*
9982 * Restore the view of the window (options, file, cursor, etc.).
9983 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +00009984 for (wp = tab_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar18144c82006-04-12 21:52:12 +00009985 {
9986 if (!ses_do_win(wp))
9987 continue;
Bram Moolenaarf13be0d2008-01-02 14:13:10 +00009988 if (put_view(fd, wp, wp != edited_win, &ssop_flags,
9989 cur_arg_idx) == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +00009990 return FAIL;
9991 if (nr > 1 && put_line(fd, "wincmd w") == FAIL)
9992 return FAIL;
Bram Moolenaarf13be0d2008-01-02 14:13:10 +00009993 next_arg_idx = wp->w_arg_idx;
Bram Moolenaar18144c82006-04-12 21:52:12 +00009994 }
9995
Bram Moolenaarf13be0d2008-01-02 14:13:10 +00009996 /* The argument index in the first tab page is zero, need to set it in
9997 * each window. For further tab pages it's the window where we do
9998 * "tabedit". */
9999 cur_arg_idx = next_arg_idx;
10000
Bram Moolenaar18144c82006-04-12 21:52:12 +000010001 /*
10002 * Restore cursor to the current window if it's not the first one.
10003 */
10004 if (cnr > 1 && (fprintf(fd, "%dwincmd w", cnr) < 0
10005 || put_eol(fd) == FAIL))
10006 return FAIL;
10007
10008 /*
Bram Moolenaar18144c82006-04-12 21:52:12 +000010009 * Restore window sizes again after jumping around in windows, because
10010 * the current window has a minimum size while others may not.
10011 */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010012 if (nr > 1 && ses_winsizes(fd, restore_size, tab_firstwin) == FAIL)
Bram Moolenaar18144c82006-04-12 21:52:12 +000010013 return FAIL;
10014
Bram Moolenaar18144c82006-04-12 21:52:12 +000010015 /* Don't continue in another tab page when doing only the current one
10016 * or when at the last tab page. */
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010017 if (!(ssop_flags & SSOP_TABPAGES))
Bram Moolenaar18144c82006-04-12 21:52:12 +000010018 break;
10019 }
10020
10021 if (ssop_flags & SSOP_TABPAGES)
10022 {
Bram Moolenaar18144c82006-04-12 21:52:12 +000010023 if (fprintf(fd, "tabnext %d", tabpage_index(curtab)) < 0
10024 || put_eol(fd) == FAIL)
10025 return FAIL;
10026 }
Bram Moolenaar04ad7fe2014-05-07 21:14:47 +020010027 if (restore_stal && put_line(fd, "set stal=1") == FAIL)
10028 return FAIL;
Bram Moolenaar18144c82006-04-12 21:52:12 +000010029
Bram Moolenaard39e2752019-01-26 20:07:38 +010010030 // Now put the remaining buffers into the buffer list.
10031 // This is near the end, so that when 'hidden' is set we don't create extra
10032 // buffers. If the buffer was already created with another command the
10033 // ":badd" will have no effect.
10034 FOR_ALL_BUFFERS(buf)
10035 {
10036 if (!(only_save_windows && buf->b_nwindows == 0)
10037 && !(buf->b_help && !(ssop_flags & SSOP_HELP))
10038#ifdef FEAT_TERMINAL
10039 // Skip terminal buffers: finished ones are not useful, others
10040 // will be resurrected and result in a new buffer.
10041 && !bt_terminal(buf)
10042#endif
10043 && buf->b_fname != NULL
10044 && buf->b_p_bl)
10045 {
10046 if (fprintf(fd, "badd +%ld ", buf->b_wininfo == NULL ? 1L
10047 : buf->b_wininfo->wi_fpos.lnum) < 0
10048 || ses_fname(fd, buf, &ssop_flags, TRUE) == FAIL)
10049 return FAIL;
10050 }
10051 }
10052
Bram Moolenaar9c102382006-05-03 21:26:49 +000010053 /*
10054 * Wipe out an empty unnamed buffer we started in.
10055 */
Bram Moolenaar26d4b892018-07-04 22:26:28 +020010056 if (put_line(fd, "if exists('s:wipebuf') && len(win_findbuf(s:wipebuf)) == 0")
Bram Moolenaar4d8bac82018-03-09 21:33:34 +010010057 == FAIL)
Bram Moolenaar9c102382006-05-03 21:26:49 +000010058 return FAIL;
Bram Moolenaarf3a67882006-05-05 21:09:41 +000010059 if (put_line(fd, " silent exe 'bwipe ' . s:wipebuf") == FAIL)
Bram Moolenaar9c102382006-05-03 21:26:49 +000010060 return FAIL;
10061 if (put_line(fd, "endif") == FAIL)
10062 return FAIL;
10063 if (put_line(fd, "unlet! s:wipebuf") == FAIL)
10064 return FAIL;
10065
10066 /* Re-apply 'winheight', 'winwidth' and 'shortmess'. */
10067 if (fprintf(fd, "set winheight=%ld winwidth=%ld shortmess=%s",
10068 p_wh, p_wiw, p_shm) < 0 || put_eol(fd) == FAIL)
10069 return FAIL;
Bram Moolenaar36ae89c2017-01-28 17:11:14 +010010070 /* Re-apply 'winminheight' and 'winminwidth'. */
10071 if (fprintf(fd, "set winminheight=%ld winminwidth=%ld",
10072 p_wmh, p_wmw) < 0 || put_eol(fd) == FAIL)
10073 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010074
10075 /*
10076 * Lastly, execute the x.vim file if it exists.
10077 */
10078 if (put_line(fd, "let s:sx = expand(\"<sfile>:p:r\").\"x.vim\"") == FAIL
10079 || put_line(fd, "if file_readable(s:sx)") == FAIL
Bram Moolenaar42ba1262008-12-09 10:18:03 +000010080 || put_line(fd, " exe \"source \" . fnameescape(s:sx)") == FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +000010081 || put_line(fd, "endif") == FAIL)
10082 return FAIL;
10083
10084 return OK;
10085}
10086
10087 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010088ses_winsizes(
10089 FILE *fd,
10090 int restore_size,
10091 win_T *tab_firstwin)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010092{
10093 int n = 0;
10094 win_T *wp;
10095
10096 if (restore_size && (ssop_flags & SSOP_WINSIZE))
10097 {
Bram Moolenaarcba2ae52006-10-24 10:59:57 +000010098 for (wp = tab_firstwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010099 {
10100 if (!ses_do_win(wp))
10101 continue;
10102 ++n;
10103
10104 /* restore height when not full height */
10105 if (wp->w_height + wp->w_status_height < topframe->fr_height
10106 && (fprintf(fd,
10107 "exe '%dresize ' . ((&lines * %ld + %ld) / %ld)",
10108 n, (long)wp->w_height, Rows / 2, Rows) < 0
10109 || put_eol(fd) == FAIL))
10110 return FAIL;
10111
10112 /* restore width when not full width */
10113 if (wp->w_width < Columns && (fprintf(fd,
10114 "exe 'vert %dresize ' . ((&columns * %ld + %ld) / %ld)",
10115 n, (long)wp->w_width, Columns / 2, Columns) < 0
10116 || put_eol(fd) == FAIL))
10117 return FAIL;
10118 }
10119 }
10120 else
10121 {
10122 /* Just equalise window sizes */
10123 if (put_line(fd, "wincmd =") == FAIL)
10124 return FAIL;
10125 }
10126 return OK;
10127}
10128
10129/*
10130 * Write commands to "fd" to recursively create windows for frame "fr",
10131 * horizontally and vertically split.
10132 * After the commands the last window in the frame is the current window.
10133 * Returns FAIL when writing the commands to "fd" fails.
10134 */
10135 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010136ses_win_rec(FILE *fd, frame_T *fr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010137{
10138 frame_T *frc;
10139 int count = 0;
10140
10141 if (fr->fr_layout != FR_LEAF)
10142 {
10143 /* Find first frame that's not skipped and then create a window for
10144 * each following one (first frame is already there). */
10145 frc = ses_skipframe(fr->fr_child);
10146 if (frc != NULL)
10147 while ((frc = ses_skipframe(frc->fr_next)) != NULL)
10148 {
10149 /* Make window as big as possible so that we have lots of room
10150 * to split. */
10151 if (put_line(fd, "wincmd _ | wincmd |") == FAIL
10152 || put_line(fd, fr->fr_layout == FR_COL
10153 ? "split" : "vsplit") == FAIL)
10154 return FAIL;
10155 ++count;
10156 }
10157
10158 /* Go back to the first window. */
10159 if (count > 0 && (fprintf(fd, fr->fr_layout == FR_COL
10160 ? "%dwincmd k" : "%dwincmd h", count) < 0
10161 || put_eol(fd) == FAIL))
10162 return FAIL;
10163
10164 /* Recursively create frames/windows in each window of this column or
10165 * row. */
10166 frc = ses_skipframe(fr->fr_child);
10167 while (frc != NULL)
10168 {
10169 ses_win_rec(fd, frc);
10170 frc = ses_skipframe(frc->fr_next);
10171 /* Go to next window. */
10172 if (frc != NULL && put_line(fd, "wincmd w") == FAIL)
10173 return FAIL;
10174 }
10175 }
10176 return OK;
10177}
10178
10179/*
10180 * Skip frames that don't contain windows we want to save in the Session.
10181 * Returns NULL when there none.
10182 */
10183 static frame_T *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010184ses_skipframe(frame_T *fr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010185{
10186 frame_T *frc;
10187
Bram Moolenaar3d1491e2018-12-22 17:07:50 +010010188 FOR_ALL_FRAMES(frc, fr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010189 if (ses_do_frame(frc))
10190 break;
10191 return frc;
10192}
10193
10194/*
10195 * Return TRUE if frame "fr" has a window somewhere that we want to save in
10196 * the Session.
10197 */
10198 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010199ses_do_frame(frame_T *fr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010200{
10201 frame_T *frc;
10202
10203 if (fr->fr_layout == FR_LEAF)
10204 return ses_do_win(fr->fr_win);
Bram Moolenaar3d1491e2018-12-22 17:07:50 +010010205 FOR_ALL_FRAMES(frc, fr->fr_child)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010206 if (ses_do_frame(frc))
10207 return TRUE;
10208 return FALSE;
10209}
10210
10211/*
10212 * Return non-zero if window "wp" is to be stored in the Session.
10213 */
10214 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010215ses_do_win(win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010216{
Bram Moolenaar4d8bac82018-03-09 21:33:34 +010010217#ifdef FEAT_TERMINAL
10218 if (bt_terminal(wp->w_buffer))
10219 return !term_is_finished(wp->w_buffer)
10220 && (ssop_flags & SSOP_TERMINAL)
10221 && term_should_restore(wp->w_buffer);
10222#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010223 if (wp->w_buffer->b_fname == NULL
10224#ifdef FEAT_QUICKFIX
10225 /* When 'buftype' is "nofile" can't restore the window contents. */
10226 || bt_nofile(wp->w_buffer)
10227#endif
10228 )
10229 return (ssop_flags & SSOP_BLANK);
Bram Moolenaar67883b42017-07-27 22:57:00 +020010230 if (bt_help(wp->w_buffer))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010231 return (ssop_flags & SSOP_HELP);
10232 return TRUE;
10233}
10234
Bram Moolenaar92c1b692018-08-29 21:42:42 +020010235 static int
10236put_view_curpos(FILE *fd, win_T *wp, char *spaces)
10237{
10238 int r;
10239
10240 if (wp->w_curswant == MAXCOL)
10241 r = fprintf(fd, "%snormal! $", spaces);
10242 else
10243 r = fprintf(fd, "%snormal! 0%d|", spaces, wp->w_virtcol + 1);
10244 return r < 0 || put_eol(fd) == FAIL ? FALSE : OK;
10245}
10246
Bram Moolenaar071d4272004-06-13 20:20:40 +000010247/*
10248 * Write commands to "fd" to restore the view of a window.
10249 * Caller must make sure 'scrolloff' is zero.
10250 */
10251 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010252put_view(
10253 FILE *fd,
10254 win_T *wp,
10255 int add_edit, /* add ":edit" command to view */
10256 unsigned *flagp, /* vop_flags or ssop_flags */
10257 int current_arg_idx) /* current argument index of the window, use
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010258 * -1 if unknown */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010259{
10260 win_T *save_curwin;
10261 int f;
10262 int do_cursor;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000010263 int did_next = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010264
10265 /* Always restore cursor position for ":mksession". For ":mkview" only
10266 * when 'viewoptions' contains "cursor". */
10267 do_cursor = (flagp == &ssop_flags || *flagp & SSOP_CURSOR);
10268
10269 /*
10270 * Local argument list.
10271 */
10272 if (wp->w_alist == &global_alist)
10273 {
10274 if (put_line(fd, "argglobal") == FAIL)
10275 return FAIL;
10276 }
10277 else
10278 {
10279 if (ses_arglist(fd, "arglocal", &wp->w_alist->al_ga,
10280 flagp == &vop_flags
10281 || !(*flagp & SSOP_CURDIR)
10282 || wp->w_localdir != NULL, flagp) == FAIL)
10283 return FAIL;
10284 }
10285
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000010286 /* Only when part of a session: restore the argument index. Some
10287 * arguments may have been deleted, check if the index is valid. */
Bram Moolenaar51f53df2010-06-26 05:25:54 +020010288 if (wp->w_arg_idx != current_arg_idx && wp->w_arg_idx < WARGCOUNT(wp)
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000010289 && flagp == &ssop_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010290 {
Bram Moolenaarf13be0d2008-01-02 14:13:10 +000010291 if (fprintf(fd, "%ldargu", (long)wp->w_arg_idx + 1) < 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000010292 || put_eol(fd) == FAIL)
10293 return FAIL;
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000010294 did_next = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010295 }
10296
10297 /* Edit the file. Skip this when ":next" already did it. */
Bram Moolenaarf95dc3b2005-05-22 22:02:25 +000010298 if (add_edit && (!did_next || wp->w_arg_idx_invalid))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010299 {
Bram Moolenaar4d8bac82018-03-09 21:33:34 +010010300# ifdef FEAT_TERMINAL
10301 if (bt_terminal(wp->w_buffer))
10302 {
10303 if (term_write_session(fd, wp) == FAIL)
10304 return FAIL;
10305 }
10306 else
10307# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010308 /*
10309 * Load the file.
10310 */
10311 if (wp->w_buffer->b_ffname != NULL
Bram Moolenaar4d8bac82018-03-09 21:33:34 +010010312# ifdef FEAT_QUICKFIX
Bram Moolenaar071d4272004-06-13 20:20:40 +000010313 && !bt_nofile(wp->w_buffer)
Bram Moolenaar4d8bac82018-03-09 21:33:34 +010010314# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010315 )
10316 {
10317 /*
10318 * Editing a file in this buffer: use ":edit file".
10319 * This may have side effects! (e.g., compressed or network file).
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +020010320 *
10321 * Note, if a buffer for that file already exists, use :badd to
10322 * edit that buffer, to not lose folding information (:edit resets
10323 * folds in other buffers)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010324 */
Bram Moolenaarad36a352019-01-24 13:34:42 +010010325 if (fputs("if bufexists(\"", fd) < 0
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +020010326 || ses_fname(fd, wp->w_buffer, flagp, FALSE) == FAIL
Bram Moolenaarad36a352019-01-24 13:34:42 +010010327 || fputs("\") | buffer ", fd) < 0
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +020010328 || ses_fname(fd, wp->w_buffer, flagp, FALSE) == FAIL
10329 || fputs(" | else | edit ", fd) < 0
10330 || ses_fname(fd, wp->w_buffer, flagp, FALSE) == FAIL
10331 || fputs(" | endif", fd) < 0
Bram Moolenaar4d8bac82018-03-09 21:33:34 +010010332 || put_eol(fd) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010333 return FAIL;
10334 }
10335 else
10336 {
10337 /* No file in this buffer, just make it empty. */
10338 if (put_line(fd, "enew") == FAIL)
10339 return FAIL;
10340#ifdef FEAT_QUICKFIX
10341 if (wp->w_buffer->b_ffname != NULL)
10342 {
10343 /* The buffer does have a name, but it's not a file name. */
10344 if (fputs("file ", fd) < 0
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +020010345 || ses_fname(fd, wp->w_buffer, flagp, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010346 return FAIL;
10347 }
10348#endif
10349 do_cursor = FALSE;
10350 }
10351 }
10352
10353 /*
10354 * Local mappings and abbreviations.
10355 */
10356 if ((*flagp & (SSOP_OPTIONS | SSOP_LOCALOPTIONS))
10357 && makemap(fd, wp->w_buffer) == FAIL)
10358 return FAIL;
10359
10360 /*
10361 * Local options. Need to go to the window temporarily.
10362 * Store only local values when using ":mkview" and when ":mksession" is
10363 * used and 'sessionoptions' doesn't include "options".
10364 * Some folding options are always stored when "folds" is included,
10365 * otherwise the folds would not be restored correctly.
10366 */
10367 save_curwin = curwin;
10368 curwin = wp;
10369 curbuf = curwin->w_buffer;
10370 if (*flagp & (SSOP_OPTIONS | SSOP_LOCALOPTIONS))
10371 f = makeset(fd, OPT_LOCAL,
10372 flagp == &vop_flags || !(*flagp & SSOP_OPTIONS));
10373#ifdef FEAT_FOLDING
10374 else if (*flagp & SSOP_FOLDS)
10375 f = makefoldset(fd);
10376#endif
10377 else
10378 f = OK;
10379 curwin = save_curwin;
10380 curbuf = curwin->w_buffer;
10381 if (f == FAIL)
10382 return FAIL;
10383
10384#ifdef FEAT_FOLDING
10385 /*
10386 * Save Folds when 'buftype' is empty and for help files.
10387 */
10388 if ((*flagp & SSOP_FOLDS)
10389 && wp->w_buffer->b_ffname != NULL
Bram Moolenaar91335e52018-08-01 17:53:12 +020010390 && (bt_normal(wp->w_buffer) || bt_help(wp->w_buffer)))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010391 {
10392 if (put_folds(fd, wp) == FAIL)
10393 return FAIL;
10394 }
10395#endif
10396
10397 /*
10398 * Set the cursor after creating folds, since that moves the cursor.
10399 */
10400 if (do_cursor)
10401 {
10402
10403 /* Restore the cursor line in the file and relatively in the
10404 * window. Don't use "G", it changes the jumplist. */
10405 if (fprintf(fd, "let s:l = %ld - ((%ld * winheight(0) + %ld) / %ld)",
10406 (long)wp->w_cursor.lnum,
10407 (long)(wp->w_cursor.lnum - wp->w_topline),
10408 (long)wp->w_height / 2, (long)wp->w_height) < 0
10409 || put_eol(fd) == FAIL
10410 || put_line(fd, "if s:l < 1 | let s:l = 1 | endif") == FAIL
10411 || put_line(fd, "exe s:l") == FAIL
10412 || put_line(fd, "normal! zt") == FAIL
10413 || fprintf(fd, "%ld", (long)wp->w_cursor.lnum) < 0
10414 || put_eol(fd) == FAIL)
10415 return FAIL;
10416 /* Restore the cursor column and left offset when not wrapping. */
10417 if (wp->w_cursor.col == 0)
10418 {
10419 if (put_line(fd, "normal! 0") == FAIL)
10420 return FAIL;
10421 }
10422 else
10423 {
10424 if (!wp->w_p_wrap && wp->w_leftcol > 0 && wp->w_width > 0)
10425 {
10426 if (fprintf(fd,
10427 "let s:c = %ld - ((%ld * winwidth(0) + %ld) / %ld)",
Bram Moolenaar558ddad2013-02-20 19:26:29 +010010428 (long)wp->w_virtcol + 1,
10429 (long)(wp->w_virtcol - wp->w_leftcol),
Bram Moolenaar071d4272004-06-13 20:20:40 +000010430 (long)wp->w_width / 2, (long)wp->w_width) < 0
10431 || put_eol(fd) == FAIL
10432 || put_line(fd, "if s:c > 0") == FAIL
10433 || fprintf(fd,
Bram Moolenaar558ddad2013-02-20 19:26:29 +010010434 " exe 'normal! ' . s:c . '|zs' . %ld . '|'",
10435 (long)wp->w_virtcol + 1) < 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000010436 || put_eol(fd) == FAIL
10437 || put_line(fd, "else") == FAIL
Bram Moolenaar92c1b692018-08-29 21:42:42 +020010438 || put_view_curpos(fd, wp, " ") == FAIL
Bram Moolenaar071d4272004-06-13 20:20:40 +000010439 || put_line(fd, "endif") == FAIL)
10440 return FAIL;
10441 }
Bram Moolenaar92c1b692018-08-29 21:42:42 +020010442 else if (put_view_curpos(fd, wp, "") == FAIL)
10443 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010444 }
10445 }
10446
10447 /*
Bram Moolenaar13e90412017-11-11 18:16:48 +010010448 * Local directory, if the current flag is not view options or the "curdir"
10449 * option is included.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010450 */
Bram Moolenaar13e90412017-11-11 18:16:48 +010010451 if (wp->w_localdir != NULL
10452 && (flagp != &vop_flags || (*flagp & SSOP_CURDIR)))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010453 {
10454 if (fputs("lcd ", fd) < 0
10455 || ses_put_fname(fd, wp->w_localdir, flagp) == FAIL
10456 || put_eol(fd) == FAIL)
10457 return FAIL;
Bram Moolenaareeefcc72007-05-01 21:21:21 +000010458 did_lcd = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010459 }
10460
10461 return OK;
10462}
10463
10464/*
10465 * Write an argument list to the session file.
10466 * Returns FAIL if writing fails.
10467 */
10468 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010469ses_arglist(
10470 FILE *fd,
10471 char *cmd,
10472 garray_T *gap,
10473 int fullname, /* TRUE: use full path name */
10474 unsigned *flagp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010475{
10476 int i;
Bram Moolenaard9462e32011-04-11 21:35:11 +020010477 char_u *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010478 char_u *s;
10479
Bram Moolenaarf0bdd2f2014-03-12 21:28:26 +010010480 if (fputs(cmd, fd) < 0 || put_eol(fd) == FAIL)
10481 return FAIL;
Bram Moolenaar555de4e2019-01-21 23:03:49 +010010482 if (put_line(fd, "%argdel") == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010483 return FAIL;
10484 for (i = 0; i < gap->ga_len; ++i)
10485 {
10486 /* NULL file names are skipped (only happens when out of memory). */
10487 s = alist_name(&((aentry_T *)gap->ga_data)[i]);
10488 if (s != NULL)
10489 {
10490 if (fullname)
10491 {
Bram Moolenaard9462e32011-04-11 21:35:11 +020010492 buf = alloc(MAXPATHL);
10493 if (buf != NULL)
10494 {
10495 (void)vim_FullName(s, buf, MAXPATHL, FALSE);
10496 s = buf;
10497 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010498 }
Bram Moolenaar79da5632017-02-01 22:52:44 +010010499 if (fputs("$argadd ", fd) < 0
Bram Moolenaarf0bdd2f2014-03-12 21:28:26 +010010500 || ses_put_fname(fd, s, flagp) == FAIL
10501 || put_eol(fd) == FAIL)
Bram Moolenaard9462e32011-04-11 21:35:11 +020010502 {
10503 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010504 return FAIL;
Bram Moolenaard9462e32011-04-11 21:35:11 +020010505 }
10506 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010507 }
10508 }
Bram Moolenaarf0bdd2f2014-03-12 21:28:26 +010010509 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010510}
10511
10512/*
10513 * Write a buffer name to the session file.
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +020010514 * Also ends the line, if "add_eol" is TRUE.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010515 * Returns FAIL if writing fails.
10516 */
10517 static int
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +020010518ses_fname(FILE *fd, buf_T *buf, unsigned *flagp, int add_eol)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010519{
10520 char_u *name;
10521
10522 /* Use the short file name if the current directory is known at the time
Bram Moolenaareeefcc72007-05-01 21:21:21 +000010523 * the session file will be sourced.
10524 * Don't do this for ":mkview", we don't know the current directory.
10525 * Don't do this after ":lcd", we don't keep track of what the current
10526 * directory is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010527 if (buf->b_sfname != NULL
10528 && flagp == &ssop_flags
Bram Moolenaareeefcc72007-05-01 21:21:21 +000010529 && (ssop_flags & (SSOP_CURDIR | SSOP_SESDIR))
Bram Moolenaar8d594672009-07-01 18:18:57 +000010530#ifdef FEAT_AUTOCHDIR
10531 && !p_acd
10532#endif
Bram Moolenaareeefcc72007-05-01 21:21:21 +000010533 && !did_lcd)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010534 name = buf->b_sfname;
10535 else
10536 name = buf->b_ffname;
Bram Moolenaar4bebc9a2017-08-30 21:07:38 +020010537 if (ses_put_fname(fd, name, flagp) == FAIL
10538 || (add_eol && put_eol(fd) == FAIL))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010539 return FAIL;
10540 return OK;
10541}
10542
10543/*
10544 * Write a file name to the session file.
10545 * Takes care of the "slash" option in 'sessionoptions' and escapes special
10546 * characters.
Bram Moolenaar78f74a92010-10-13 17:50:07 +020010547 * Returns FAIL if writing fails or out of memory.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010548 */
10549 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010550ses_put_fname(FILE *fd, char_u *name, unsigned *flagp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010551{
10552 char_u *sname;
Bram Moolenaar78f74a92010-10-13 17:50:07 +020010553 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010554 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010555
10556 sname = home_replace_save(NULL, name);
Bram Moolenaar78f74a92010-10-13 17:50:07 +020010557 if (sname == NULL)
10558 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010559
Bram Moolenaar78f74a92010-10-13 17:50:07 +020010560 if (*flagp & SSOP_SLASH)
10561 {
10562 /* change all backslashes to forward slashes */
Bram Moolenaar91acfff2017-03-12 19:22:36 +010010563 for (p = sname; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar78f74a92010-10-13 17:50:07 +020010564 if (*p == '\\')
10565 *p = '/';
Bram Moolenaar071d4272004-06-13 20:20:40 +000010566 }
Bram Moolenaar78f74a92010-10-13 17:50:07 +020010567
Bram Moolenaar84a05ac2013-05-06 04:24:17 +020010568 /* escape special characters */
Bram Moolenaar78f74a92010-10-13 17:50:07 +020010569 p = vim_strsave_fnameescape(sname, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010570 vim_free(sname);
Bram Moolenaar78f74a92010-10-13 17:50:07 +020010571 if (p == NULL)
10572 return FAIL;
10573
10574 /* write the result */
10575 if (fputs((char *)p, fd) < 0)
10576 retval = FAIL;
10577
10578 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010579 return retval;
10580}
10581
10582/*
10583 * ":loadview [nr]"
10584 */
10585 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010586ex_loadview(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010587{
10588 char_u *fname;
10589
10590 fname = get_view_file(*eap->arg);
10591 if (fname != NULL)
10592 {
Bram Moolenaar910f66f2006-04-05 20:41:53 +000010593 do_source(fname, FALSE, DOSO_NONE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010594 vim_free(fname);
10595 }
10596}
10597
10598/*
10599 * Get the name of the view file for the current buffer.
10600 */
10601 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010602get_view_file(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010603{
10604 int len = 0;
10605 char_u *p, *s;
10606 char_u *retval;
10607 char_u *sname;
10608
10609 if (curbuf->b_ffname == NULL)
10610 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010611 emsg(_(e_noname));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010612 return NULL;
10613 }
10614 sname = home_replace_save(NULL, curbuf->b_ffname);
10615 if (sname == NULL)
10616 return NULL;
10617
10618 /*
10619 * We want a file name without separators, because we're not going to make
10620 * a directory.
10621 * "normal" path separator -> "=+"
10622 * "=" -> "=="
10623 * ":" path separator -> "=-"
10624 */
10625 for (p = sname; *p; ++p)
10626 if (*p == '=' || vim_ispathsep(*p))
10627 ++len;
Bram Moolenaar964b3742019-05-24 18:54:09 +020010628 retval = alloc(STRLEN(sname) + len + STRLEN(p_vdir) + 9);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010629 if (retval != NULL)
10630 {
10631 STRCPY(retval, p_vdir);
10632 add_pathsep(retval);
10633 s = retval + STRLEN(retval);
10634 for (p = sname; *p; ++p)
10635 {
10636 if (*p == '=')
10637 {
10638 *s++ = '=';
10639 *s++ = '=';
10640 }
10641 else if (vim_ispathsep(*p))
10642 {
10643 *s++ = '=';
Bram Moolenaare60acc12011-05-10 16:41:25 +020010644#if defined(BACKSLASH_IN_FILENAME) || defined(AMIGA) || defined(VMS)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010645 if (*p == ':')
10646 *s++ = '-';
10647 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010648#endif
Bram Moolenaarcef9dcc2005-12-06 19:50:41 +000010649 *s++ = '+';
Bram Moolenaar071d4272004-06-13 20:20:40 +000010650 }
10651 else
10652 *s++ = *p;
10653 }
10654 *s++ = '=';
10655 *s++ = c;
10656 STRCPY(s, ".vim");
10657 }
10658
10659 vim_free(sname);
10660 return retval;
10661}
10662
10663#endif /* FEAT_SESSION */
10664
10665/*
10666 * Write end-of-line character(s) for ":mkexrc", ":mkvimrc" and ":mksession".
10667 * Return FAIL for a write error.
10668 */
10669 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010670put_eol(FILE *fd)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010671{
10672 if (
10673#ifdef USE_CRNL
10674 (
10675# ifdef MKSESSION_NL
10676 !mksession_nl &&
10677# endif
10678 (putc('\r', fd) < 0)) ||
10679#endif
10680 (putc('\n', fd) < 0))
10681 return FAIL;
10682 return OK;
10683}
10684
10685/*
10686 * Write a line to "fd".
10687 * Return FAIL for a write error.
10688 */
10689 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010690put_line(FILE *fd, char *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010691{
10692 if (fputs(s, fd) < 0 || put_eol(fd) == FAIL)
10693 return FAIL;
10694 return OK;
10695}
10696
10697#ifdef FEAT_VIMINFO
10698/*
10699 * ":rviminfo" and ":wviminfo".
10700 */
10701 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010702ex_viminfo(
10703 exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010704{
10705 char_u *save_viminfo;
10706
10707 save_viminfo = p_viminfo;
10708 if (*p_viminfo == NUL)
10709 p_viminfo = (char_u *)"'100";
10710 if (eap->cmdidx == CMD_rviminfo)
10711 {
Bram Moolenaard812df62008-11-09 12:46:09 +000010712 if (read_viminfo(eap->arg, VIF_WANT_INFO | VIF_WANT_MARKS
10713 | (eap->forceit ? VIF_FORCEIT : 0)) == FAIL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010714 emsg(_("E195: Cannot open viminfo file for reading"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010715 }
10716 else
10717 write_viminfo(eap->arg, eap->forceit);
10718 p_viminfo = save_viminfo;
10719}
10720#endif
10721
10722#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG) || defined(PROTO)
Bram Moolenaar9c13b352005-05-19 20:53:52 +000010723/*
Bram Moolenaard9462e32011-04-11 21:35:11 +020010724 * Make a dialog message in "buff[DIALOG_MSG_SIZE]".
Bram Moolenaarb765d632005-06-07 21:00:02 +000010725 * "format" must contain "%s".
Bram Moolenaar9c13b352005-05-19 20:53:52 +000010726 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010727 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010728dialog_msg(char_u *buff, char *format, char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010729{
Bram Moolenaar071d4272004-06-13 20:20:40 +000010730 if (fname == NULL)
10731 fname = (char_u *)_("Untitled");
Bram Moolenaard9462e32011-04-11 21:35:11 +020010732 vim_snprintf((char *)buff, DIALOG_MSG_SIZE, format, fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010733}
10734#endif
10735
10736/*
10737 * ":behave {mswin,xterm}"
10738 */
10739 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010740ex_behave(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010741{
10742 if (STRCMP(eap->arg, "mswin") == 0)
10743 {
10744 set_option_value((char_u *)"selection", 0L, (char_u *)"exclusive", 0);
10745 set_option_value((char_u *)"selectmode", 0L, (char_u *)"mouse,key", 0);
10746 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"popup", 0);
10747 set_option_value((char_u *)"keymodel", 0L,
10748 (char_u *)"startsel,stopsel", 0);
10749 }
10750 else if (STRCMP(eap->arg, "xterm") == 0)
10751 {
10752 set_option_value((char_u *)"selection", 0L, (char_u *)"inclusive", 0);
10753 set_option_value((char_u *)"selectmode", 0L, (char_u *)"", 0);
10754 set_option_value((char_u *)"mousemodel", 0L, (char_u *)"extend", 0);
10755 set_option_value((char_u *)"keymodel", 0L, (char_u *)"", 0);
10756 }
10757 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010758 semsg(_(e_invarg2), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010759}
10760
Bram Moolenaar42b4dda2010-03-02 15:56:05 +010010761#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
10762/*
10763 * Function given to ExpandGeneric() to obtain the possible arguments of the
10764 * ":behave {mswin,xterm}" command.
10765 */
10766 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010767get_behave_arg(expand_T *xp UNUSED, int idx)
Bram Moolenaar42b4dda2010-03-02 15:56:05 +010010768{
10769 if (idx == 0)
10770 return (char_u *)"mswin";
10771 if (idx == 1)
10772 return (char_u *)"xterm";
10773 return NULL;
10774}
Bram Moolenaar9e507ca2016-10-15 15:39:39 +020010775
10776/*
10777 * Function given to ExpandGeneric() to obtain the possible arguments of the
10778 * ":messages {clear}" command.
10779 */
10780 char_u *
10781get_messages_arg(expand_T *xp UNUSED, int idx)
10782{
10783 if (idx == 0)
10784 return (char_u *)"clear";
10785 return NULL;
10786}
Bram Moolenaar42b4dda2010-03-02 15:56:05 +010010787#endif
10788
Bram Moolenaar113e1072019-01-20 15:30:40 +010010789#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
Bram Moolenaarcae92dc2017-08-06 15:22:15 +020010790 char_u *
10791get_mapclear_arg(expand_T *xp UNUSED, int idx)
10792{
10793 if (idx == 0)
10794 return (char_u *)"<buffer>";
10795 return NULL;
10796}
Bram Moolenaar113e1072019-01-20 15:30:40 +010010797#endif
Bram Moolenaarcae92dc2017-08-06 15:22:15 +020010798
Bram Moolenaar071d4272004-06-13 20:20:40 +000010799static int filetype_detect = FALSE;
10800static int filetype_plugin = FALSE;
10801static int filetype_indent = FALSE;
10802
10803/*
10804 * ":filetype [plugin] [indent] {on,off,detect}"
10805 * on: Load the filetype.vim file to install autocommands for file types.
10806 * off: Load the ftoff.vim file to remove all autocommands for file types.
10807 * plugin on: load filetype.vim and ftplugin.vim
10808 * plugin off: load ftplugof.vim
10809 * indent on: load filetype.vim and indent.vim
10810 * indent off: load indoff.vim
10811 */
10812 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010813ex_filetype(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010814{
10815 char_u *arg = eap->arg;
10816 int plugin = FALSE;
10817 int indent = FALSE;
10818
10819 if (*eap->arg == NUL)
10820 {
10821 /* Print current status. */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010822 smsg("filetype detection:%s plugin:%s indent:%s",
Bram Moolenaar071d4272004-06-13 20:20:40 +000010823 filetype_detect ? "ON" : "OFF",
10824 filetype_plugin ? (filetype_detect ? "ON" : "(on)") : "OFF",
10825 filetype_indent ? (filetype_detect ? "ON" : "(on)") : "OFF");
10826 return;
10827 }
10828
10829 /* Accept "plugin" and "indent" in any order. */
10830 for (;;)
10831 {
10832 if (STRNCMP(arg, "plugin", 6) == 0)
10833 {
10834 plugin = TRUE;
10835 arg = skipwhite(arg + 6);
10836 continue;
10837 }
10838 if (STRNCMP(arg, "indent", 6) == 0)
10839 {
10840 indent = TRUE;
10841 arg = skipwhite(arg + 6);
10842 continue;
10843 }
10844 break;
10845 }
10846 if (STRCMP(arg, "on") == 0 || STRCMP(arg, "detect") == 0)
10847 {
10848 if (*arg == 'o' || !filetype_detect)
10849 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010010850 source_runtime((char_u *)FILETYPE_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010851 filetype_detect = TRUE;
10852 if (plugin)
10853 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010010854 source_runtime((char_u *)FTPLUGIN_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010855 filetype_plugin = TRUE;
10856 }
10857 if (indent)
10858 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010010859 source_runtime((char_u *)INDENT_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010860 filetype_indent = TRUE;
10861 }
10862 }
10863 if (*arg == 'd')
10864 {
Bram Moolenaar1610d052016-06-09 22:53:01 +020010865 (void)do_doautocmd((char_u *)"filetypedetect BufRead", TRUE, NULL);
Bram Moolenaara3227e22006-03-08 21:32:40 +000010866 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010867 }
10868 }
10869 else if (STRCMP(arg, "off") == 0)
10870 {
10871 if (plugin || indent)
10872 {
10873 if (plugin)
10874 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010010875 source_runtime((char_u *)FTPLUGOF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010876 filetype_plugin = FALSE;
10877 }
10878 if (indent)
10879 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010010880 source_runtime((char_u *)INDOFF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010881 filetype_indent = FALSE;
10882 }
10883 }
10884 else
10885 {
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010010886 source_runtime((char_u *)FTOFF_FILE, DIP_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010887 filetype_detect = FALSE;
10888 }
10889 }
10890 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010891 semsg(_(e_invarg2), arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010892}
10893
10894/*
Bram Moolenaar3e545692017-06-04 19:00:32 +020010895 * ":setfiletype [FALLBACK] {name}"
Bram Moolenaar071d4272004-06-13 20:20:40 +000010896 */
10897 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010898ex_setfiletype(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010899{
10900 if (!did_filetype)
Bram Moolenaar3e545692017-06-04 19:00:32 +020010901 {
10902 char_u *arg = eap->arg;
10903
10904 if (STRNCMP(arg, "FALLBACK ", 9) == 0)
10905 arg += 9;
10906
10907 set_option_value((char_u *)"filetype", 0L, arg, OPT_LOCAL);
10908 if (arg != eap->arg)
10909 did_filetype = FALSE;
10910 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010911}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010912
Bram Moolenaar071d4272004-06-13 20:20:40 +000010913 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010914ex_digraphs(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010915{
10916#ifdef FEAT_DIGRAPHS
10917 if (*eap->arg != NUL)
10918 putdigraph(eap->arg);
10919 else
Bram Moolenaareae8ae12018-12-14 18:53:02 +010010920 listdigraphs(eap->forceit);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010921#else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010922 emsg(_("E196: No digraphs in this version"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010923#endif
10924}
10925
10926 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010927ex_set(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010928{
10929 int flags = 0;
10930
10931 if (eap->cmdidx == CMD_setlocal)
10932 flags = OPT_LOCAL;
10933 else if (eap->cmdidx == CMD_setglobal)
10934 flags = OPT_GLOBAL;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +010010935#if defined(FEAT_EVAL) && defined(FEAT_BROWSE)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010936 if (cmdmod.browse && flags == 0)
10937 ex_options(eap);
10938 else
10939#endif
10940 (void)do_set(eap->arg, flags);
10941}
10942
Bram Moolenaar451fc7b2018-04-27 22:53:07 +020010943#if defined(FEAT_SEARCH_EXTRA) || defined(PROTO)
10944 void
10945set_no_hlsearch(int flag)
10946{
10947 no_hlsearch = flag;
10948# ifdef FEAT_EVAL
10949 set_vim_var_nr(VV_HLSEARCH, !no_hlsearch && p_hls);
10950# endif
10951}
10952
Bram Moolenaar071d4272004-06-13 20:20:40 +000010953/*
10954 * ":nohlsearch"
10955 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010956 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010957ex_nohlsearch(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010958{
Bram Moolenaar451fc7b2018-04-27 22:53:07 +020010959 set_no_hlsearch(TRUE);
Bram Moolenaarf71a3db2006-03-12 21:50:18 +000010960 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010961}
10962
10963/*
Bram Moolenaare1438bb2006-03-01 22:01:55 +000010964 * ":[N]match {group} {pattern}"
Bram Moolenaar071d4272004-06-13 20:20:40 +000010965 * Sets nextcmd to the start of the next command, if any. Also called when
10966 * skipping commands to find the next command.
10967 */
10968 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010010969ex_match(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010970{
10971 char_u *p;
Bram Moolenaar52d36c82007-08-11 14:00:30 +000010972 char_u *g = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010973 char_u *end;
10974 int c;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000010975 int id;
Bram Moolenaare1438bb2006-03-01 22:01:55 +000010976
10977 if (eap->line2 <= 3)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000010978 id = eap->line2;
Bram Moolenaare1438bb2006-03-01 22:01:55 +000010979 else
10980 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010981 emsg(_(e_invcmd));
Bram Moolenaare1438bb2006-03-01 22:01:55 +000010982 return;
10983 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010984
10985 /* First clear any old pattern. */
10986 if (!eap->skip)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000010987 match_delete(curwin, id, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010988
10989 if (ends_excmd(*eap->arg))
10990 end = eap->arg;
10991 else if ((STRNICMP(eap->arg, "none", 4) == 0
Bram Moolenaar1c465442017-03-12 20:10:05 +010010992 && (VIM_ISWHITE(eap->arg[4]) || ends_excmd(eap->arg[4]))))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010993 end = eap->arg + 4;
10994 else
10995 {
10996 p = skiptowhite(eap->arg);
10997 if (!eap->skip)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000010998 g = vim_strnsave(eap->arg, (int)(p - eap->arg));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010999 p = skipwhite(p);
11000 if (*p == NUL)
11001 {
11002 /* There must be two arguments. */
Bram Moolenaar9a7d58e2015-11-24 17:23:56 +010011003 vim_free(g);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011004 semsg(_(e_invarg2), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011005 return;
11006 }
11007 end = skip_regexp(p + 1, *p, TRUE, NULL);
11008 if (!eap->skip)
11009 {
11010 if (*end != NUL && !ends_excmd(*skipwhite(end + 1)))
11011 {
Bram Moolenaar9a7d58e2015-11-24 17:23:56 +010011012 vim_free(g);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011013 eap->errmsg = e_trailing;
11014 return;
11015 }
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000011016 if (*end != *p)
11017 {
Bram Moolenaar9a7d58e2015-11-24 17:23:56 +010011018 vim_free(g);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010011019 semsg(_(e_invarg2), p);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000011020 return;
11021 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011022
11023 c = *end;
11024 *end = NUL;
Bram Moolenaar6561d522015-07-21 15:48:27 +020011025 match_add(curwin, g, p + 1, 10, id, NULL, NULL);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011026 vim_free(g);
Bram Moolenaar910f66f2006-04-05 20:41:53 +000011027 *end = c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011028 }
11029 }
11030 eap->nextcmd = find_nextcmd(end);
11031}
11032#endif
11033
11034#ifdef FEAT_CRYPT
11035/*
11036 * ":X": Get crypt key
11037 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011038 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011039ex_X(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011040{
Bram Moolenaar3a0c9082014-11-12 15:15:42 +010011041 crypt_check_current_method();
Bram Moolenaar8f4ac012014-08-10 13:38:34 +020011042 (void)crypt_get_key(TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011043}
11044#endif
11045
11046#ifdef FEAT_FOLDING
11047 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011048ex_fold(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011049{
11050 if (foldManualAllowed(TRUE))
11051 foldCreate(eap->line1, eap->line2);
11052}
11053
11054 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011055ex_foldopen(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011056{
11057 opFoldRange(eap->line1, eap->line2, eap->cmdidx == CMD_foldopen,
11058 eap->forceit, FALSE);
11059}
11060
11061 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +010011062ex_folddo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011063{
11064 linenr_T lnum;
11065
Bram Moolenaar6b1ee342014-08-06 18:17:11 +020011066#ifdef FEAT_CLIPBOARD
11067 start_global_changes();
11068#endif
11069
Bram Moolenaar071d4272004-06-13 20:20:40 +000011070 /* First set the marks for all lines closed/open. */
11071 for (lnum = eap->line1; lnum <= eap->line2; ++lnum)
11072 if (hasFolding(lnum, NULL, NULL) == (eap->cmdidx == CMD_folddoclosed))
11073 ml_setmarked(lnum);
11074
11075 /* Execute the command on the marked lines. */
11076 global_exe(eap->arg);
11077 ml_clearmarked(); /* clear rest of the marks */
Bram Moolenaar6b1ee342014-08-06 18:17:11 +020011078#ifdef FEAT_CLIPBOARD
11079 end_global_changes();
11080#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011081}
11082#endif
Bram Moolenaarf5291f32017-09-14 22:55:37 +020011083
Bram Moolenaar39665952018-08-15 20:59:48 +020011084#ifdef FEAT_QUICKFIX
11085/*
11086 * Returns TRUE if the supplied Ex cmdidx is for a location list command
11087 * instead of a quickfix command.
11088 */
11089 int
11090is_loclist_cmd(int cmdidx)
11091{
Bram Moolenaar74c8be22018-08-23 22:51:40 +020011092 if (cmdidx < 0 || cmdidx >= CMD_SIZE)
Bram Moolenaar39665952018-08-15 20:59:48 +020011093 return FALSE;
11094 return cmdnames[cmdidx].cmd_name[0] == 'l';
11095}
11096#endif
11097
Bram Moolenaarf5291f32017-09-14 22:55:37 +020011098# if defined(FEAT_TIMERS) || defined(PROTO)
11099 int
11100get_pressedreturn(void)
11101{
11102 return ex_pressedreturn;
11103}
11104
11105 void
11106set_pressedreturn(int val)
11107{
11108 ex_pressedreturn = val;
11109}
11110#endif